xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/iscsi/iscsi.c (revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9)
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 			rtn = EINVAL;
2681 			break;
2682 		}
2683 
2684 		{
2685 			boolean_t	found = B_FALSE;
2686 			void		*v = NULL;
2687 			entry_t		tmp_e;
2688 			char		*name = NULL;
2689 
2690 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2691 
2692 			/* Find name for matching static_tgt oid */
2693 			persistent_static_addr_lock();
2694 			while (persistent_static_addr_next(&v,
2695 			    (char *)name, &tmp_e) == B_TRUE) {
2696 				if (e.e_oid == tmp_e.e_oid) {
2697 					found = B_TRUE;
2698 					break;
2699 				}
2700 			}
2701 
2702 			/* If static_tgt found logout and remove it */
2703 			if (found == B_TRUE) {
2704 
2705 				iscsid_addr_to_sockaddr(tmp_e.e_insize,
2706 				    &tmp_e.e_u, tmp_e.e_port, &addr_dsc.sin);
2707 
2708 				persistent_static_addr_unlock();
2709 
2710 				/*
2711 				 * If discovery in progress, try few times
2712 				 * before return busy
2713 				 */
2714 				retry = 0;
2715 				mutex_enter(&ihp->hba_discovery_events_mutex);
2716 				while (ihp->hba_discovery_in_progress ==
2717 				    B_TRUE) {
2718 					if (++retry == 5) {
2719 						rtn = EBUSY;
2720 						break;
2721 					}
2722 					mutex_exit(
2723 					    &ihp->hba_discovery_events_mutex);
2724 					delay(SEC_TO_TICK(
2725 					    ISCSI_DISC_DELAY));
2726 					mutex_enter(
2727 					    &ihp->hba_discovery_events_mutex);
2728 				}
2729 				/* remove from persistent store */
2730 				if (rtn == 0 && persistent_static_addr_clear(
2731 				    e.e_oid) == B_FALSE) {
2732 					rtn = EIO;
2733 				}
2734 				mutex_exit(&ihp->hba_discovery_events_mutex);
2735 
2736 				if (rtn != 0) {
2737 					kmem_free(name, ISCSI_MAX_NAME_LEN);
2738 					break;
2739 				}
2740 
2741 				/* Attempt to logout of target */
2742 				if (iscsid_del(ihp, (char *)name,
2743 				    iSCSIDiscoveryMethodStatic, &addr_dsc.sin)
2744 				    == B_FALSE) {
2745 					persistent_static_addr_lock();
2746 
2747 					/*
2748 					 * Restore static_tgt to
2749 					 * persistent store
2750 					 */
2751 					if (persistent_static_addr_set(
2752 					    (char *)name,
2753 					    &e) == B_FALSE) {
2754 						cmn_err(CE_WARN, "Failed to "
2755 						    "restore static target "
2756 						    "address after logout "
2757 						    "target failure.");
2758 					}
2759 					persistent_static_addr_unlock();
2760 					rtn = EBUSY;
2761 				} else {
2762 					iscsid_poke_discovery(ihp,
2763 					    iSCSIDiscoveryMethodStatic);
2764 					(void) iscsid_login_tgt(ihp,
2765 					    (char *)name,
2766 					    iSCSIDiscoveryMethodStatic,
2767 					    NULL);
2768 
2769 				}
2770 			} else {
2771 				persistent_static_addr_unlock();
2772 				rtn = EIO;
2773 			}
2774 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2775 		}
2776 		break;
2777 
2778 	/*
2779 	 * ISCSI_ISNS_SERVER_ADDR_SET:
2780 	 */
2781 	case ISCSI_ISNS_SERVER_ADDR_SET:
2782 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2783 			rtn = EFAULT;
2784 			break;
2785 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2786 			rtn = EINVAL;
2787 			break;
2788 		}
2789 
2790 		if (persistent_isns_addr_set(&e) == B_FALSE) {
2791 			rtn = EIO;
2792 			break;
2793 		}
2794 
2795 		/*
2796 		 * If iSNS server discovery is enabled, then kickoff
2797 		 * discovery of the targets advertised by the recently
2798 		 * added iSNS server address.
2799 		 */
2800 		method = persistent_disc_meth_get();
2801 		if (method & iSCSIDiscoveryMethodISNS) {
2802 			initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2803 			    KM_SLEEP);
2804 			if (persistent_initiator_name_get(initiator_node_name,
2805 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2806 				kmem_free(initiator_node_name,
2807 				    ISCSI_MAX_NAME_LEN);
2808 				initiator_node_name = NULL;
2809 				rtn = EIO;
2810 				break;
2811 			}
2812 			if (strlen(initiator_node_name) == 0) {
2813 				kmem_free(initiator_node_name,
2814 				    ISCSI_MAX_NAME_LEN);
2815 				initiator_node_name = NULL;
2816 				rtn = EIO;
2817 				break;
2818 			}
2819 
2820 			initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2821 			    KM_SLEEP);
2822 			if (persistent_alias_name_get(initiator_node_alias,
2823 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2824 				initiator_node_alias[0] = '\0';
2825 			}
2826 
2827 			/*
2828 			 * Register this initiator node against this iSNS
2829 			 * server.
2830 			 */
2831 			(void) isns_reg_one_server(&e, ihp->hba_isid,
2832 			    (uint8_t *)initiator_node_name,
2833 			    ISCSI_MAX_NAME_LEN,
2834 			    (uint8_t *)initiator_node_alias,
2835 			    ISCSI_MAX_NAME_LEN,
2836 			    ISNS_INITIATOR_NODE_TYPE,
2837 			    isns_scn_callback);
2838 
2839 			iscsid_do_isns_query_one_server(ihp, &e);
2840 
2841 			iscsid_addr_to_sockaddr(e.e_insize,
2842 			    &e.e_u, e.e_port, &addr_dsc.sin);
2843 
2844 			(void) iscsid_login_tgt(ihp, NULL,
2845 			    iSCSIDiscoveryMethodISNS,
2846 			    &addr_dsc.sin);
2847 
2848 			/* Done using the name and alias - free them. */
2849 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
2850 			initiator_node_name = NULL;
2851 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
2852 			initiator_node_alias = NULL;
2853 		}
2854 		break;
2855 
2856 	/*
2857 	 * ISCSI_DISCOVERY_ADDR_SET:
2858 	 */
2859 	case ISCSI_DISCOVERY_ADDR_SET:
2860 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2861 			rtn = EFAULT;
2862 			break;
2863 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2864 			rtn = EINVAL;
2865 			break;
2866 		}
2867 
2868 		if (e.e_oid == ISCSI_OID_NOTSET) {
2869 			mutex_enter(&iscsi_oid_mutex);
2870 			e.e_oid = iscsi_oid++;
2871 			mutex_exit(&iscsi_oid_mutex);
2872 		}
2873 
2874 		if (persistent_disc_addr_set(&e) == B_FALSE) {
2875 			rtn = EIO;
2876 			break;
2877 		}
2878 
2879 		/*
2880 		 * If Send Targets discovery is enabled, then kickoff
2881 		 * discovery of the targets advertised by the recently
2882 		 * added discovery address.
2883 		 */
2884 		method = persistent_disc_meth_get();
2885 		if (method & iSCSIDiscoveryMethodSendTargets) {
2886 
2887 			iscsid_addr_to_sockaddr(e.e_insize,
2888 			    &e.e_u, e.e_port, &addr_dsc.sin);
2889 			iscsid_do_sendtgts(&e);
2890 			(void) iscsid_login_tgt(ihp, NULL,
2891 			    iSCSIDiscoveryMethodSendTargets,
2892 			    &addr_dsc.sin);
2893 
2894 		}
2895 		break;
2896 
2897 	/*
2898 	 * ISCSI_DISCOVERY_ADDR_LIST_GET
2899 	 */
2900 	case ISCSI_DISCOVERY_ADDR_LIST_GET:
2901 		/* copyin user args */
2902 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2903 			rtn = EFAULT;
2904 			break;
2905 		}
2906 
2907 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2908 			rtn = EINVAL;
2909 			break;
2910 		}
2911 
2912 		list_space = sizeof (iscsi_addr_list_t);
2913 		if (ial.al_in_cnt != 0) {
2914 			list_space += (sizeof (iscsi_addr_t) *
2915 			    (ial.al_in_cnt - 1));
2916 		}
2917 
2918 		ialp = kmem_zalloc(list_space, KM_SLEEP);
2919 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
2920 
2921 		void_p = NULL;
2922 		ialp->al_out_cnt = 0;
2923 		persistent_disc_addr_lock();
2924 		while (persistent_disc_addr_next(&void_p, &e) == B_TRUE) {
2925 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2926 				int		i = ialp->al_out_cnt;
2927 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
2928 
2929 				addr->a_port = e.e_port;
2930 				addr->a_addr.i_insize = e.e_insize;
2931 				addr->a_oid = e.e_oid;
2932 
2933 				if (e.e_insize == sizeof (struct in_addr)) {
2934 					/* IPv4 */
2935 					addr->a_addr.i_addr.in4.s_addr =
2936 					    e.e_u.u_in4.s_addr;
2937 				} else if (e.e_insize ==
2938 					    sizeof (struct in6_addr)) {
2939 					/* IPv6 */
2940 					bcopy(e.e_u.u_in6.s6_addr,
2941 					    addr->a_addr.i_addr.in6.s6_addr,
2942 					    16);
2943 				}
2944 			}
2945 			ialp->al_out_cnt++;
2946 		}
2947 		persistent_disc_addr_unlock();
2948 
2949 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
2950 		kmem_free(ialp, list_space);
2951 		break;
2952 
2953 	/*
2954 	 * ISCSI_ISNS_SERVER_ADDR_LIST_GET
2955 	 */
2956 	case ISCSI_ISNS_SERVER_ADDR_LIST_GET:
2957 		/* copyin user args */
2958 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2959 			rtn = EFAULT;
2960 			break;
2961 		}
2962 
2963 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2964 			rtn = EINVAL;
2965 			break;
2966 		}
2967 
2968 		list_space = sizeof (iscsi_addr_list_t);
2969 		if (ial.al_in_cnt != 0) {
2970 			list_space += (sizeof (iscsi_addr_t) *
2971 			    (ial.al_in_cnt - 1));
2972 		}
2973 
2974 		ialp = kmem_zalloc(list_space, KM_SLEEP);
2975 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
2976 
2977 		void_p = NULL;
2978 		ialp->al_out_cnt = 0;
2979 		persistent_isns_addr_lock();
2980 		while (persistent_isns_addr_next(&void_p, &e) == B_TRUE) {
2981 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2982 				int		i = ialp->al_out_cnt;
2983 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
2984 
2985 				addr->a_port = e.e_port;
2986 				addr->a_addr.i_insize = e.e_insize;
2987 				if (e.e_insize == sizeof (struct in_addr)) {
2988 					/* IPv4 */
2989 					addr->a_addr.i_addr.in4.s_addr =
2990 					    e.e_u.u_in4.s_addr;
2991 				} else if (e.e_insize ==
2992 					    sizeof (struct in6_addr)) {
2993 					/* IPv6 */
2994 					bcopy(e.e_u.u_in6.s6_addr,
2995 					    addr->a_addr.i_addr.in6.s6_addr,
2996 					    16);
2997 				}
2998 			}
2999 			ialp->al_out_cnt++;
3000 		}
3001 		persistent_isns_addr_unlock();
3002 
3003 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
3004 		kmem_free(ialp, list_space);
3005 		break;
3006 
3007 	/*
3008 	 * ISCSI_DISCOVERY_ADDR_CLEAR:
3009 	 */
3010 	case ISCSI_DISCOVERY_ADDR_CLEAR:
3011 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3012 			rtn = EFAULT;
3013 			break;
3014 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3015 			rtn = EINVAL;
3016 			break;
3017 		}
3018 
3019 		iscsid_addr_to_sockaddr(e.e_insize,
3020 		    &e.e_u, e.e_port, &addr_dsc.sin);
3021 
3022 		/* If discovery in progress, try few times before return busy */
3023 		retry = 0;
3024 		mutex_enter(&ihp->hba_discovery_events_mutex);
3025 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3026 			if (++retry == 5) {
3027 				rtn = EBUSY;
3028 				break;
3029 			}
3030 			mutex_exit(&ihp->hba_discovery_events_mutex);
3031 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3032 			mutex_enter(&ihp->hba_discovery_events_mutex);
3033 		}
3034 
3035 		/*
3036 		 * Clear discovery address first, so that any bus config
3037 		 * will ignore this discovery address
3038 		 */
3039 		if (rtn == 0 && persistent_disc_addr_clear(&e) == B_FALSE) {
3040 			rtn = EIO;
3041 		}
3042 		mutex_exit(&ihp->hba_discovery_events_mutex);
3043 
3044 		if (rtn != 0) {
3045 			break;
3046 		}
3047 		/* Attempt to logout of associated targets */
3048 		if (iscsid_del(ihp, NULL,
3049 		    iSCSIDiscoveryMethodSendTargets, &addr_dsc.sin) ==
3050 		    B_FALSE) {
3051 			/* Failure!, restore the discovery addr. */
3052 			if (persistent_disc_addr_set(&e) == B_FALSE) {
3053 				cmn_err(CE_WARN, "Failed to restore sendtgt "
3054 				    "discovery address after logout associated "
3055 				    "targets failures.");
3056 			}
3057 			rtn = EBUSY;
3058 		}
3059 		break;
3060 
3061 	/*
3062 	 * ISCSI_ISNS_SERVER_CLEAR:
3063 	 */
3064 	case ISCSI_ISNS_SERVER_ADDR_CLEAR:
3065 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3066 			rtn = EFAULT;
3067 			break;
3068 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3069 			rtn = EINVAL;
3070 			break;
3071 		}
3072 
3073 		iscsid_addr_to_sockaddr(e.e_insize,
3074 		    &e.e_u, e.e_port, &addr_dsc.sin);
3075 
3076 		/* If discovery in progress, try few times before return busy */
3077 		retry = 0;
3078 		mutex_enter(&ihp->hba_discovery_events_mutex);
3079 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3080 			if (++retry == 5) {
3081 				rtn = EBUSY;
3082 				break;
3083 			}
3084 			mutex_exit(&ihp->hba_discovery_events_mutex);
3085 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3086 			mutex_enter(&ihp->hba_discovery_events_mutex);
3087 		}
3088 
3089 		/*
3090 		 * Clear isns server address first, so that any bus config
3091 		 * will ignore any target registerd on this isns server
3092 		 */
3093 		if (rtn == 0 && persistent_isns_addr_clear(&e) == B_FALSE) {
3094 			rtn = EIO;
3095 		}
3096 		mutex_exit(&ihp->hba_discovery_events_mutex);
3097 
3098 		if (rtn != 0) {
3099 			break;
3100 		}
3101 
3102 		/* Attempt logout of associated targets */
3103 		if (iscsid_del(ihp, NULL, iSCSIDiscoveryMethodISNS,
3104 		    &addr_dsc.sin) == B_FALSE) {
3105 			/* Failure!, restore the isns server addr. */
3106 
3107 			if (persistent_isns_addr_set(&e) == B_FALSE) {
3108 				cmn_err(CE_WARN, "Failed to restore isns server"
3109 				    " address after logout associated targets"
3110 				    " failures.");
3111 			}
3112 			rtn = EBUSY;
3113 		} else {
3114 			method = persistent_disc_meth_get();
3115 			if (method & iSCSIDiscoveryMethodISNS) {
3116 				boolean_t is_last_isns_server_b =
3117 				    B_FALSE;
3118 				int isns_server_count = 0;
3119 				void *void_p = NULL;
3120 
3121 				/*
3122 				 * Check if the last iSNS server's been
3123 				 * removed.
3124 				 */
3125 				{
3126 					entry_t tmp_e;
3127 					persistent_isns_addr_lock();
3128 					while (persistent_isns_addr_next(
3129 					    &void_p, &tmp_e) == B_TRUE) {
3130 						isns_server_count++;
3131 					}
3132 				}
3133 				persistent_isns_addr_unlock();
3134 				if (isns_server_count == 0) {
3135 					is_last_isns_server_b = B_TRUE;
3136 				}
3137 
3138 				/*
3139 				 * Deregister this node from this iSNS
3140 				 * server.
3141 				 */
3142 				initiator_node_name = kmem_zalloc(
3143 				    ISCSI_MAX_NAME_LEN, KM_SLEEP);
3144 				if (persistent_initiator_name_get(
3145 				    initiator_node_name,
3146 				    ISCSI_MAX_NAME_LEN) == B_TRUE) {
3147 
3148 					if (strlen(initiator_node_name) > 0) {
3149 						(void) isns_dereg_one_server(
3150 						    &e, (uint8_t *)
3151 						    initiator_node_name,
3152 						    is_last_isns_server_b);
3153 					}
3154 				}
3155 				kmem_free(initiator_node_name,
3156 				    ISCSI_MAX_NAME_LEN);
3157 				initiator_node_name = NULL;
3158 			}
3159 		}
3160 		break;
3161 
3162 	/*
3163 	 * ISCSI_DISCOVERY_SET -
3164 	 */
3165 	case ISCSI_DISCOVERY_SET:
3166 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3167 			rtn = EFAULT;
3168 			break;
3169 		}
3170 
3171 		if (persistent_disc_meth_set(method) == B_FALSE) {
3172 			rtn = EIO;
3173 		} else {
3174 			(void) iscsid_enable_discovery(ihp, method, B_FALSE);
3175 			iscsid_poke_discovery(ihp, method);
3176 			(void) iscsid_login_tgt(ihp, NULL, method, NULL);
3177 		}
3178 		break;
3179 
3180 	/*
3181 	 * ISCSI_DISCOVERY_GET -
3182 	 */
3183 	case ISCSI_DISCOVERY_GET:
3184 		method = persistent_disc_meth_get();
3185 		rtn = ddi_copyout(&method, (caddr_t)arg,
3186 		    sizeof (method), mode);
3187 		break;
3188 
3189 	/*
3190 	 * ISCSI_DISCOVERY_CLEAR -
3191 	 */
3192 	case ISCSI_DISCOVERY_CLEAR:
3193 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3194 			rtn = EFAULT;
3195 			break;
3196 		}
3197 
3198 		/* If discovery in progress, try few times before return busy */
3199 		retry = 0;
3200 		mutex_enter(&ihp->hba_discovery_events_mutex);
3201 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3202 			if (++retry == 5) {
3203 				rtn = EBUSY;
3204 				break;
3205 			}
3206 			mutex_exit(&ihp->hba_discovery_events_mutex);
3207 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3208 			mutex_enter(&ihp->hba_discovery_events_mutex);
3209 		}
3210 
3211 		/*
3212 		 * Clear discovery first, so that any bus config or
3213 		 * discovery requests will ignore this discovery method
3214 		 */
3215 		if (rtn == 0 && persistent_disc_meth_clear(method) == B_FALSE) {
3216 			rtn = EIO;
3217 		}
3218 		mutex_exit(&ihp->hba_discovery_events_mutex);
3219 
3220 		if (rtn != 0) {
3221 			break;
3222 		}
3223 
3224 		/* Attempt to logout from all associated targets */
3225 		if (iscsid_disable_discovery(ihp, method) == B_FALSE) {
3226 			/* Failure!, reset the discovery */
3227 			if (persistent_disc_meth_set(method) == B_FALSE) {
3228 				cmn_err(CE_WARN, "Failed to reset discovery "
3229 				    "method after discovery disable failure.");
3230 			}
3231 			rtn = EBUSY;
3232 		}
3233 		break;
3234 
3235 	/*
3236 	 * ISCSI_DISCOVERY_PROPS -
3237 	 */
3238 	case ISCSI_DISCOVERY_PROPS:
3239 		iscsid_props(&discovery_props);
3240 		if (ddi_copyout(&discovery_props, (caddr_t)arg,
3241 		    sizeof (discovery_props), mode))
3242 			rtn = EFAULT;
3243 		break;
3244 
3245 	/*
3246 	 * ISCSI_LUN_OID_LIST --
3247 	 */
3248 	case ISCSI_LUN_OID_LIST_GET:
3249 		ll = (iscsi_lun_list_t *)kmem_alloc(sizeof (*ll), KM_SLEEP);
3250 		if (ddi_copyin((caddr_t)arg, ll, sizeof (*ll), mode)) {
3251 			rtn = EFAULT;
3252 			kmem_free(ll, sizeof (*ll));
3253 			break;
3254 		}
3255 
3256 		if (ll->ll_vers != ISCSI_INTERFACE_VERSION) {
3257 			rtn = EINVAL;
3258 			kmem_free(ll, sizeof (*ll));
3259 			break;
3260 		}
3261 
3262 		/*
3263 		 * Find out how much space the user has allocated in their
3264 		 * structure. Match the same space for our structure.
3265 		 */
3266 		lun_sz = sizeof (iscsi_lun_list_t);
3267 		if (ll->ll_in_cnt > 0) {
3268 			lun_sz += (ll->ll_in_cnt - 1) * sizeof (iscsi_if_lun_t);
3269 		}
3270 
3271 		llp = kmem_zalloc(lun_sz, KM_SLEEP);
3272 		bcopy(ll, llp, sizeof (*ll));
3273 		kmem_free(ll, sizeof (*ll));
3274 
3275 		/*
3276 		 * Check to see if oid references a target-param oid.  If so,
3277 		 * find the associated  session oid before getting lu list.
3278 		 */
3279 		if (iscsi_targetparam_get_name(llp->ll_tgt_oid) != NULL) {
3280 			for (isp = ihp->hba_sess_list; isp;
3281 			    isp = isp->sess_next) {
3282 				if (isp->sess_target_oid == llp->ll_tgt_oid) {
3283 					target_oid  = isp->sess_oid;
3284 					break;
3285 				}
3286 			}
3287 		} else {
3288 			target_oid = llp->ll_tgt_oid;
3289 		}
3290 
3291 
3292 		/*
3293 		 * Look at the LUNs attached to the specified target. If there
3294 		 * is space in the user structure save that information locally.
3295 		 * Always add up the count to the total. By always adding
3296 		 * the count this code can be used if ll_in_cnt == 0 and
3297 		 * the user just wishes to know the appropriate size to
3298 		 * allocate.
3299 		 */
3300 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3301 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
3302 			if ((llp->ll_all_tgts == B_FALSE) &&
3303 			    (isp->sess_oid != target_oid)) {
3304 				continue;
3305 			}
3306 			rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3307 			for (ilp = isp->sess_lun_list; ilp;
3308 			    ilp = ilp->lun_next) {
3309 				if ((ilp->lun_state &
3310 				    ISCSI_LUN_STATE_ONLINE) &&
3311 				    !(ilp->lun_state &
3312 				    ISCSI_LUN_STATE_INVALID)) {
3313 					if (llp->ll_out_cnt <
3314 					    llp->ll_in_cnt) {
3315 						iscsi_if_lun_t *lp;
3316 						lp = &llp->ll_luns[
3317 						    llp->ll_out_cnt];
3318 
3319 						lp->l_tgt_oid =
3320 						    isp->sess_oid;
3321 						lp->l_oid = ilp->lun_oid;
3322 						lp->l_num = ilp->lun_num;
3323 					}
3324 				llp->ll_out_cnt++;
3325 				}
3326 			}
3327 			rw_exit(&isp->sess_lun_list_rwlock);
3328 		}
3329 		rw_exit(&ihp->hba_sess_list_rwlock);
3330 
3331 		if (ddi_copyout(llp, (caddr_t)arg, lun_sz, mode)) {
3332 			rtn = EFAULT;
3333 		}
3334 
3335 		kmem_free(llp, lun_sz);
3336 		break;
3337 
3338 	/*
3339 	 * ISCSI_LUN_PROPS_GET --
3340 	 */
3341 	case ISCSI_LUN_PROPS_GET:
3342 		lun = (iscsi_lun_props_t *)kmem_zalloc(sizeof (*lun), KM_SLEEP);
3343 		if (ddi_copyin((caddr_t)arg, lun, sizeof (*lun), mode)) {
3344 			rtn = EFAULT;
3345 			kmem_free(lun, sizeof (*lun));
3346 			break;
3347 		}
3348 
3349 		if (lun->lp_vers != ISCSI_INTERFACE_VERSION) {
3350 			rtn = EINVAL;
3351 			kmem_free(lun, sizeof (*lun));
3352 			break;
3353 		}
3354 
3355 		/*
3356 		 * For the target specified, find the LUN specified and
3357 		 * return its properties
3358 		 */
3359 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3360 		rtn = iscsi_sess_get(lun->lp_tgt_oid, ihp, &isp);
3361 		if (rtn != 0) {
3362 			rw_exit(&ihp->hba_sess_list_rwlock);
3363 			rtn = EFAULT;
3364 			kmem_free(lun, sizeof (*lun));
3365 			break;
3366 		}
3367 		rtn = EINVAL;	/* Set bad rtn, correct only if found */
3368 		rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3369 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
3370 			if (ilp->lun_oid == lun->lp_oid) {
3371 				lun->lp_num	= ilp->lun_num;
3372 				lun->lp_status	= LunValid;
3373 				lun->lp_time_online = ilp->lun_time_online;
3374 
3375 				if (ilp->lun_pip != NULL) {
3376 					lun_dip = mdi_pi_get_client(
3377 					    ilp->lun_pip);
3378 				} else {
3379 					lun_dip = ilp->lun_dip;
3380 				}
3381 
3382 				if (lun_dip != NULL &&
3383 				    ((i_ddi_devi_attached(lun_dip)) ||
3384 				    (ddi_get_devstate(lun_dip) ==
3385 				    DDI_DEVSTATE_UP))) {
3386 					(void) ddi_pathname(lun_dip,
3387 					    lun->lp_pathname);
3388 				} else {
3389 					/*
3390 					 * The LUN is not exported to the
3391 					 * OS yet.  It is in the process
3392 					 * of being added.
3393 					 */
3394 					lun->lp_status	= LunDoesNotExist;
3395 				}
3396 				bcopy(ilp->lun_vid, lun->lp_vid,
3397 				    sizeof (lun->lp_vid));
3398 				bcopy(ilp->lun_pid, lun->lp_pid,
3399 				    sizeof (lun->lp_pid));
3400 				rtn = ddi_copyout(lun, (caddr_t)arg,
3401 				    sizeof (*lun), mode);
3402 				if (rtn == -1) {
3403 					rtn = EFAULT;
3404 				}
3405 				break;
3406 			}
3407 		}
3408 		rw_exit(&isp->sess_lun_list_rwlock);
3409 		rw_exit(&ihp->hba_sess_list_rwlock);
3410 
3411 		kmem_free(lun, sizeof (*lun));
3412 		break;
3413 
3414 	/*
3415 	 * ISCSI_CONN_OID_LIST_GET --
3416 	 */
3417 #define	ISCSIIOCOLGC iscsi_ioctl_conn_oid_list_get_copyout
3418 	case ISCSI_CONN_OID_LIST_GET:
3419 		{
3420 			iscsi_conn_list_t	*cl;
3421 
3422 			/* Asuume the worst */
3423 			rtn = EFAULT;
3424 
3425 			/* Copy the input argument into kernel world. */
3426 			cl = iscsi_ioctl_conn_oid_list_get_copyin(
3427 			    (caddr_t)arg,
3428 			    mode);
3429 			if (cl != NULL) {
3430 				if (iscsi_ioctl_conn_oid_list_get(ihp, cl) ==
3431 				    B_TRUE) {
3432 					rtn =
3433 					    ISCSIIOCOLGC(
3434 					    cl, (caddr_t)arg, mode);
3435 				}
3436 			}
3437 			break;
3438 		}
3439 #undef ISCSIIOCOLGC
3440 	/*
3441 	 * ISCSI_CONN_OID_LIST_GET --
3442 	 */
3443 	case ISCSI_CONN_PROPS_GET:
3444 		{
3445 			iscsi_conn_props_t	*cp;
3446 
3447 			/* Asuume the worst */
3448 			rtn = EFAULT;
3449 
3450 			/* Copy the input argument into kernel world. */
3451 			cp = iscsi_ioctl_copyin(
3452 			    (caddr_t)arg,
3453 			    mode,
3454 			    sizeof (iscsi_conn_props_t));
3455 
3456 			if (cp != NULL) {
3457 				/* Get the propereties. */
3458 				if (iscsi_ioctl_conn_props_get(ihp, cp) ==
3459 				    B_TRUE) {
3460 					rtn =
3461 					    iscsi_ioctl_copyout(
3462 					    cp,
3463 					    sizeof (*cp),
3464 					    (caddr_t)arg,
3465 					    mode);
3466 				} else {
3467 					kmem_free(cp, sizeof (*cp));
3468 					cp = NULL;
3469 				}
3470 			}
3471 			break;
3472 		}
3473 
3474 	/*
3475 	 * ISCSI_RADIUS_GET -
3476 	 */
3477 	case ISCSI_RADIUS_GET:
3478 	{
3479 		iscsi_nvfile_status_t	status;
3480 
3481 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3482 		    KM_SLEEP);
3483 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3484 			kmem_free(radius, sizeof (*radius));
3485 			rtn = EFAULT;
3486 			break;
3487 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3488 			kmem_free(radius, sizeof (*radius));
3489 			rtn = EINVAL;
3490 			break;
3491 		}
3492 
3493 		old_oid = radius->r_oid;
3494 
3495 		if (radius->r_oid == ihp->hba_oid) {
3496 			name = ihp->hba_name;
3497 		} else {
3498 			/*
3499 			 * RADIUS configuration should be done on a per
3500 			 * initiator basis.
3501 			 */
3502 			kmem_free(radius, sizeof (*radius));
3503 			rtn = EINVAL;
3504 			break;
3505 		}
3506 
3507 		status = persistent_radius_get(radius);
3508 		if (status == ISCSI_NVFILE_SUCCESS) {
3509 			/*
3510 			 * Restore the value for overridden (and bogus) oid.
3511 			 */
3512 			radius->r_oid = old_oid;
3513 			rtn = ddi_copyout(radius, (caddr_t)arg,
3514 			    sizeof (*radius), mode);
3515 		} else if (status == ISCSI_NVFILE_NAMEVAL_NOT_FOUND) {
3516 			rtn = ENOENT;
3517 		} else {
3518 			rtn = EIO;
3519 		}
3520 		kmem_free(radius, sizeof (*radius));
3521 		break;
3522 	}
3523 
3524 	/*
3525 	 * ISCSI_RADIUS_SET -
3526 	 */
3527 	case ISCSI_RADIUS_SET:
3528 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3529 		    KM_SLEEP);
3530 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3531 			rtn = EFAULT;
3532 			kmem_free(radius, sizeof (*radius));
3533 			break;
3534 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3535 			rtn = EINVAL;
3536 			kmem_free(radius, sizeof (*radius));
3537 			break;
3538 		}
3539 
3540 		if (radius->r_oid == ihp->hba_oid) {
3541 			name = ihp->hba_name;
3542 		} else {
3543 			/*
3544 			 * RADIUS configuration should be done on a per
3545 			 * initiator basis.
3546 			 */
3547 			kmem_free(radius, sizeof (*radius));
3548 			rtn = EINVAL;
3549 			break;
3550 		}
3551 
3552 		if (persistent_radius_set(radius) == B_FALSE) {
3553 			rtn = EIO;
3554 		}
3555 
3556 		kmem_free(radius, sizeof (*radius));
3557 		break;
3558 
3559 	/*
3560 	 *  ISCSI_AUTH_GET -
3561 	 */
3562 	case ISCSI_AUTH_GET:
3563 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3564 		    KM_SLEEP);
3565 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3566 			kmem_free(auth, sizeof (*auth));
3567 			rtn = EFAULT;
3568 			break;
3569 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3570 			kmem_free(auth, sizeof (*auth));
3571 			rtn = EINVAL;
3572 			break;
3573 		}
3574 
3575 		old_oid = auth->a_oid;
3576 
3577 		if (auth->a_oid == ihp->hba_oid) {
3578 			name = ihp->hba_name;
3579 		} else {
3580 
3581 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3582 			/*
3583 			 * If the oid does represent a session check to see
3584 			 * if it is a target oid.  If so, return the target's
3585 			 * associated session.
3586 			 */
3587 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3588 			if (rtn != 0) {
3589 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3590 				    ihp, &isp);
3591 			}
3592 			rw_exit(&ihp->hba_sess_list_rwlock);
3593 
3594 			/*
3595 			 * If rtn is zero then we have found an
3596 			 * existing session.  Use the session name to
3597 			 * do param lookup.  If rtn is non-zero then
3598 			 * create a targetparam object and use its name
3599 			 * for param lookup.
3600 			 */
3601 			if (rtn == 0) {
3602 				name = isp->sess_name;
3603 			} else {
3604 				name =
3605 				    iscsi_targetparam_get_name(auth->a_oid);
3606 			}
3607 		}
3608 
3609 		if (name == NULL) {
3610 			rtn = EFAULT;
3611 			break;
3612 		}
3613 
3614 		if (persistent_auth_get((char *)name, auth) == B_TRUE) {
3615 			/*
3616 			 * Restore the value for overridden (and bogus) oid.
3617 			 */
3618 			auth->a_oid = old_oid;
3619 			rtn = ddi_copyout(auth, (caddr_t)arg,
3620 			    sizeof (*auth), mode);
3621 		} else {
3622 			rtn = EIO;
3623 		}
3624 
3625 		kmem_free(auth, sizeof (*auth));
3626 		break;
3627 
3628 	/*
3629 	 *  ISCSI_AUTH_SET -
3630 	 */
3631 	case ISCSI_AUTH_SET:
3632 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3633 		    KM_SLEEP);
3634 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3635 			kmem_free(auth, sizeof (*auth));
3636 			rtn = EFAULT;
3637 			break;
3638 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3639 			kmem_free(auth, sizeof (*auth));
3640 			rtn = EINVAL;
3641 			break;
3642 		}
3643 
3644 		if (auth->a_oid == ihp->hba_oid) {
3645 			name = ihp->hba_name;
3646 		} else {
3647 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3648 			/*
3649 			 * If the oid does represent a session check to see
3650 			 * if it is a target oid.  If so, return the target's
3651 			 * associated session.
3652 			 */
3653 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3654 			if (rtn != 0) {
3655 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3656 				    ihp, &isp);
3657 			}
3658 			rw_exit(&ihp->hba_sess_list_rwlock);
3659 
3660 			/*
3661 			 * If rtn is zero then we have found an
3662 			 * existing session.  Use the session name to
3663 			 * do param lookup.  If rtn is non-zero then
3664 			 * create a targetparam object and use its name
3665 			 * for param lookup.
3666 			 */
3667 			if (rtn == 0) {
3668 				name = isp->sess_name;
3669 			} else {
3670 				name =
3671 				    iscsi_targetparam_get_name(auth->a_oid);
3672 				rtn = 0;
3673 			}
3674 		}
3675 
3676 		if (name == NULL) {
3677 			rtn = EFAULT;
3678 		} else if (persistent_auth_set((char *)name, auth)
3679 		    == B_FALSE) {
3680 			rtn = EIO;
3681 		}
3682 
3683 		kmem_free(auth, sizeof (*auth));
3684 		break;
3685 
3686 	/*
3687 	 *  ISCSI_AUTH_CLEAR -
3688 	 */
3689 	case ISCSI_AUTH_CLEAR:
3690 		auth = (iscsi_auth_props_t *)kmem_alloc(sizeof (*auth),
3691 		    KM_SLEEP);
3692 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3693 			kmem_free(auth, sizeof (*auth));
3694 			rtn = EFAULT;
3695 			break;
3696 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3697 			kmem_free(auth, sizeof (*auth));
3698 			rtn = EINVAL;
3699 			break;
3700 		}
3701 
3702 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3703 		/*
3704 		 * If the oid does represent a session check to see
3705 		 * if it is a target oid.  If so, return the target's
3706 		 * associated session.
3707 		 */
3708 		rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3709 		if (rtn != 0) {
3710 			rtn = iscsi_sess_get_by_target(auth->a_oid, ihp, &isp);
3711 		}
3712 		rw_exit(&ihp->hba_sess_list_rwlock);
3713 
3714 		/*
3715 		 * If rtn is zero then we have found an
3716 		 * existing session.  Use the session name to
3717 		 * do param lookup.  If rtn is non-zero then
3718 		 * create a targetparam object and use its name
3719 		 * for param lookup.
3720 		 */
3721 		if (rtn == 0) {
3722 			name = isp->sess_name;
3723 		} else {
3724 			name =
3725 			    iscsi_targetparam_get_name(auth->a_oid);
3726 			rtn = 0;
3727 			discovered = B_FALSE;
3728 		}
3729 
3730 		if (name == NULL) {
3731 			rw_exit(
3732 			    &ihp->hba_sess_list_rwlock);
3733 			rtn = EFAULT;
3734 			break;
3735 		}
3736 
3737 		if (persistent_auth_clear((char *)name) == B_FALSE) {
3738 			rtn = EIO;
3739 		}
3740 
3741 		/*
3742 		 * ISCSI_TARGET_PARAM_CLEAR, ISCSI_CHAP_CLEAR and
3743 		 * ISCSI_AUTH_CLEAR ioctl are called sequentially to remove
3744 		 * target parameters. Here, the target that is not discovered
3745 		 * by initiator should be removed from the iscsi_targets list
3746 		 * residing in the memory.
3747 		 */
3748 		if (discovered == B_FALSE) {
3749 			(void) iscsi_targetparam_remove_target(auth->a_oid);
3750 		}
3751 
3752 		kmem_free(auth, sizeof (*auth));
3753 		break;
3754 
3755 	/*
3756 	 * ISCSI_DB_DUMP -
3757 	 */
3758 	case ISCSI_DB_DUMP:
3759 		persistent_dump_data();
3760 		break;
3761 
3762 	case ISCSI_USCSI:
3763 
3764 #ifdef _MULTI_DATAMODEL
3765 		model = ddi_model_convert_from(mode & FMODELS);
3766 		switch (model) {
3767 		case DDI_MODEL_ILP32:
3768 
3769 			if (ddi_copyin((caddr_t)arg, &iu32_caller,
3770 			    sizeof (iscsi_uscsi32_t), mode)) {
3771 				rtn = EFAULT;
3772 				break;
3773 			}
3774 
3775 			/* perform conversion from 32 -> 64 */
3776 			iu_caller.iu_vers = iu32_caller.iu_vers;
3777 			iu_caller.iu_oid = iu32_caller.iu_oid;
3778 			iu_caller.iu_tpgt = iu32_caller.iu_tpgt;
3779 			iu_caller.iu_len = iu32_caller.iu_len;
3780 			iu_caller.iu_lun = iu32_caller.iu_lun;
3781 			uscsi_cmd32touscsi_cmd((&iu32_caller.iu_ucmd),
3782 			    (&iu_caller.iu_ucmd));
3783 
3784 			break;
3785 		case DDI_MODEL_NONE:
3786 			if (ddi_copyin((caddr_t)arg, &iu_caller,
3787 			    sizeof (iscsi_uscsi_t), mode)) {
3788 				rtn = EFAULT;
3789 				break;
3790 			}
3791 			break;
3792 		default:
3793 			ASSERT(FALSE);
3794 			rtn = EINVAL;
3795 			break;
3796 		}
3797 #endif /* _MULTI_DATAMODEL */
3798 
3799 		/* If failures earlier break */
3800 		if (rtn != 0) {
3801 			break;
3802 		}
3803 
3804 		/* copy from caller to internel cmd */
3805 		bcopy(&iu_caller, &iu, sizeof (iu));
3806 
3807 		if (iu.iu_vers != ISCSI_INTERFACE_VERSION) {
3808 			rtn = EINVAL;
3809 			break;
3810 		}
3811 		/*
3812 		 * Check to see if oid references a target-param oid.  If so,
3813 		 * find the associated  session oid before getting lu list.
3814 		 */
3815 		if (iscsi_targetparam_get_name(iu.iu_oid) != NULL) {
3816 			for (isp = ihp->hba_sess_list; isp; isp =
3817 			    isp->sess_next) {
3818 				if (isp->sess_target_oid == iu.iu_oid) {
3819 					target_oid  = isp->sess_oid;
3820 					break;
3821 				}
3822 			}
3823 		} else {
3824 			target_oid = iu.iu_oid;
3825 		}
3826 
3827 		/* make sure we have a matching session for this command */
3828 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3829 		rtn = iscsi_sess_get(target_oid, ihp, &isp);
3830 		if (rtn != 0) {
3831 			rtn = iscsi_sess_get_by_target(target_oid, ihp,
3832 			    &isp);
3833 			if (rtn != 0) {
3834 				rw_exit(&ihp->hba_sess_list_rwlock);
3835 				rtn = EFAULT;
3836 				break;
3837 			}
3838 		}
3839 		/*
3840 		 * If a caller buffer is present allocate duplicate
3841 		 * kernel space and copyin caller memory.
3842 		 */
3843 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3844 			iu.iu_ucmd.uscsi_bufaddr = (caddr_t)kmem_alloc(
3845 			    iu.iu_ucmd.uscsi_buflen, KM_SLEEP);
3846 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_bufaddr,
3847 			    iu.iu_ucmd.uscsi_bufaddr,
3848 			    iu.iu_ucmd.uscsi_buflen, mode)) {
3849 				rw_exit(&ihp->hba_sess_list_rwlock);
3850 				rtn = EFAULT;
3851 				break;
3852 			}
3853 		}
3854 
3855 		/*
3856 		 * If a caller cdb is present allocate duplicate
3857 		 * kernel space and copyin caller memory.
3858 		 */
3859 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3860 			iu.iu_ucmd.uscsi_cdb = (caddr_t)kmem_alloc(
3861 			    iu_caller.iu_ucmd.uscsi_cdblen, KM_SLEEP);
3862 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_cdb,
3863 			    iu.iu_ucmd.uscsi_cdb,
3864 			    iu.iu_ucmd.uscsi_cdblen, mode)) {
3865 				if (iu.iu_ucmd.uscsi_buflen > 0) {
3866 					kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3867 					    iu_caller.iu_ucmd.uscsi_buflen);
3868 				}
3869 				rw_exit(&ihp->hba_sess_list_rwlock);
3870 				rtn = EFAULT;
3871 				break;
3872 			}
3873 		}
3874 
3875 		/*
3876 		 * If a caller request sense is present allocate
3877 		 * duplicate kernel space.  No need to copyin.
3878 		 */
3879 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3880 			iu.iu_ucmd.uscsi_rqbuf = (caddr_t)kmem_alloc(
3881 			    iu.iu_ucmd.uscsi_rqlen, KM_SLEEP);
3882 		}
3883 
3884 		/* issue passthru to io path handler */
3885 		rtn = iscsi_handle_passthru(isp, iu.iu_lun, &iu.iu_ucmd);
3886 		if (rtn != 0) {
3887 			rtn = EFAULT;
3888 		}
3889 
3890 		/*
3891 		 * If the caller had a buf we need to do a copyout
3892 		 * and free the kernel memory
3893 		 */
3894 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3895 			if (ddi_copyout(iu.iu_ucmd.uscsi_bufaddr,
3896 			    iu_caller.iu_ucmd.uscsi_bufaddr,
3897 			    iu.iu_ucmd.uscsi_buflen, mode) != 0) {
3898 				rtn = EFAULT;
3899 			}
3900 			kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3901 			    iu.iu_ucmd.uscsi_buflen);
3902 		}
3903 
3904 		/* We need to free kernel cdb, no need to copyout */
3905 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3906 			kmem_free(iu.iu_ucmd.uscsi_cdb,
3907 			    iu.iu_ucmd.uscsi_cdblen);
3908 		}
3909 
3910 		/*
3911 		 * If the caller had a request sense we need to
3912 		 * do a copyout and free the kernel memory
3913 		 */
3914 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3915 			if (ddi_copyout(iu.iu_ucmd.uscsi_rqbuf,
3916 			    iu_caller.iu_ucmd.uscsi_rqbuf,
3917 			    iu.iu_ucmd.uscsi_rqlen, mode) != 0) {
3918 				rtn = EFAULT;
3919 			}
3920 			kmem_free(iu.iu_ucmd.uscsi_rqbuf,
3921 			    iu.iu_ucmd.uscsi_rqlen);
3922 		}
3923 
3924 #ifdef _MULTI_DATAMODEL
3925 		if (iu.iu_ucmd.uscsi_status != 0) {
3926 			switch (model = ddi_model_convert_from(
3927 			    mode & FMODELS)) {
3928 			case DDI_MODEL_ILP32:
3929 				iu32_caller.iu_ucmd.uscsi_status =
3930 				    iu.iu_ucmd.uscsi_status;
3931 				if (ddi_copyout((void *)&iu32_caller,
3932 				    (caddr_t)arg, sizeof (iscsi_uscsi32_t),
3933 				    mode) != 0) {
3934 					rtn = EFAULT;
3935 				}
3936 				break;
3937 			case DDI_MODEL_NONE:
3938 				iu_caller.iu_ucmd.uscsi_status =
3939 				    iu.iu_ucmd.uscsi_status;
3940 				if (ddi_copyout((void *)&iu_caller,
3941 				    (caddr_t)arg, sizeof (iscsi_uscsi_t),
3942 				    mode) != 0) {
3943 					rtn = EFAULT;
3944 				}
3945 				break;
3946 			default:
3947 				ASSERT(FALSE);
3948 			}
3949 		}
3950 #endif /* _MULTI_DATAMODEL */
3951 		rw_exit(&ihp->hba_sess_list_rwlock);
3952 		break;
3953 
3954 	case ISCSI_SMF_ONLINE:
3955 		if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) {
3956 			rtn = EFAULT;
3957 			break;
3958 		}
3959 		/* just a theoretical case */
3960 		if (ihp->hba_persistent_loaded == B_FALSE) {
3961 			rtn = EFAULT;
3962 			break;
3963 		}
3964 
3965 		/* doesn't need to overwrite the status anymore */
3966 		mutex_enter(&ihp->hba_service_lock);
3967 		if (ihp->hba_service_status_overwrite == B_TRUE) {
3968 			ihp->hba_service_status = ISCSI_SERVICE_DISABLED;
3969 			ihp->hba_service_status_overwrite = B_FALSE;
3970 		}
3971 		mutex_exit(&ihp->hba_service_lock);
3972 
3973 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_ENABLED) ==
3974 		    B_FALSE) {
3975 			break;
3976 		}
3977 
3978 		rval = iscsi_door_bind(did);
3979 		if (rval == B_TRUE) {
3980 			rval = iscsid_start(ihp);
3981 			if (rval == B_FALSE) {
3982 				iscsi_door_unbind();
3983 			}
3984 		}
3985 
3986 		if (rval == B_TRUE) {
3987 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
3988 		} else {
3989 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
3990 			rtn = EFAULT;
3991 		}
3992 
3993 		break;
3994 
3995 	case ISCSI_SMF_OFFLINE:
3996 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_DISABLED)
3997 		    == B_FALSE) {
3998 			break;
3999 		}
4000 
4001 		rval = iscsid_stop(ihp);
4002 
4003 		if (rval == B_TRUE) {
4004 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
4005 			iscsi_door_unbind();
4006 		} else {
4007 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
4008 			rtn = EFAULT;
4009 		}
4010 		break;
4011 
4012 	case ISCSI_SMF_GET:
4013 		mutex_enter(&ihp->hba_service_lock);
4014 		while (ihp->hba_service_status ==
4015 		    ISCSI_SERVICE_TRANSITION) {
4016 			cv_wait(&ihp->hba_service_cv,
4017 			    &ihp->hba_service_lock);
4018 		}
4019 		if (ddi_copyout((void *)&ihp->hba_service_status,
4020 		    (caddr_t)arg, sizeof (boolean_t), mode) != 0) {
4021 			rtn = EFAULT;
4022 		}
4023 		mutex_exit(&ihp->hba_service_lock);
4024 		break;
4025 
4026 	case ISCSI_DISCOVERY_EVENTS:
4027 		/*
4028 		 * If discovery has not been completed and not in progress,
4029 		 * poke the discovery methods
4030 		 */
4031 		mutex_enter(&ihp->hba_discovery_events_mutex);
4032 		method = ihp->hba_discovery_events;
4033 		if ((method != ISCSI_ALL_DISCOVERY_METHODS) &&
4034 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
4035 			ihp->hba_discovery_in_progress = B_TRUE;
4036 			mutex_exit(&ihp->hba_discovery_events_mutex);
4037 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
4038 			mutex_enter(&ihp->hba_discovery_events_mutex);
4039 			ihp->hba_discovery_in_progress = B_FALSE;
4040 			method = ihp->hba_discovery_events;
4041 		}
4042 		mutex_exit(&ihp->hba_discovery_events_mutex);
4043 
4044 		if (ddi_copyout((void *)&method, (caddr_t)arg,
4045 		    sizeof (method), mode) != 0)
4046 			rtn = EFAULT;
4047 		break;
4048 
4049 	/*
4050 	 * ISCSI_SENDTGTS_GET --
4051 	 */
4052 	case ISCSI_SENDTGTS_GET:
4053 		stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4054 		    sizeof (*stl_hdr));
4055 		if (stl_hdr == NULL) {
4056 			rtn = EFAULT;
4057 			break;
4058 		}
4059 
4060 		if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) {
4061 			rtn = EINVAL;
4062 			kmem_free(stl_hdr, sizeof (*stl_hdr));
4063 			break;
4064 		}
4065 
4066 		/* calculate how much memory user allocated for SendTgts */
4067 		stl_sz = sizeof (*stl_hdr);
4068 		if (stl_hdr->stl_in_cnt > 0) {
4069 			stl_sz += ((stl_hdr->stl_in_cnt - 1) *
4070 			    sizeof (iscsi_sendtgts_entry_t));
4071 		}
4072 
4073 		/* allocate local SendTgts list of the same size */
4074 		istl = kmem_zalloc(stl_sz, KM_SLEEP);
4075 		bcopy(stl_hdr, istl, sizeof (*stl_hdr));
4076 		kmem_free(stl_hdr, sizeof (*stl_hdr));
4077 
4078 		/* lock interface so only one SendTargets operation occurs */
4079 		sema_p(&ihp->hba_sendtgts_semaphore);
4080 
4081 		rtn = iscsi_ioctl_sendtgts_get(ihp, istl);
4082 
4083 		if (rtn == 0) {
4084 			rtn = iscsi_ioctl_copyout(istl, stl_sz,
4085 			    (caddr_t)arg, mode);
4086 		}
4087 
4088 		/* release lock to allow another SendTargets discovery */
4089 		sema_v(&ihp->hba_sendtgts_semaphore);
4090 
4091 		break;
4092 
4093 		/*
4094 		 * ISCSI_ISNS_SERVER_GET --
4095 		 */
4096 	case ISCSI_ISNS_SERVER_GET:
4097 		server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4098 		    sizeof (*server_pg_list_hdr));
4099 		if (server_pg_list_hdr == NULL) {
4100 			rtn = EFAULT;
4101 			break;
4102 		}
4103 
4104 		/* If iSNS discovery mode is not set, return with zero entry */
4105 		method = persistent_disc_meth_get();
4106 		if ((method & iSCSIDiscoveryMethodISNS) == 0) {
4107 			kmem_free(server_pg_list_hdr,
4108 			    sizeof (*server_pg_list_hdr));
4109 			server_pg_list_hdr = NULL;
4110 			rtn = EACCES;
4111 			break;
4112 		}
4113 
4114 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
4115 		if (persistent_initiator_name_get(initiator_node_name,
4116 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4117 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4118 			initiator_node_name = NULL;
4119 			kmem_free(server_pg_list_hdr,
4120 			    sizeof (*server_pg_list_hdr));
4121 			server_pg_list_hdr = NULL;
4122 			rtn = EIO;
4123 			break;
4124 		}
4125 		if (strlen(initiator_node_name) == 0) {
4126 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4127 			initiator_node_name = NULL;
4128 			kmem_free(server_pg_list_hdr,
4129 			    sizeof (*server_pg_list_hdr));
4130 			server_pg_list_hdr = NULL;
4131 			rtn = EIO;
4132 			break;
4133 		}
4134 
4135 		initiator_node_alias = kmem_zalloc(
4136 		    ISCSI_MAX_NAME_LEN, KM_SLEEP);
4137 		if (persistent_alias_name_get(initiator_node_alias,
4138 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4139 			initiator_node_alias[0] = '\0';
4140 		}
4141 		rtn = isns_query_one_server(&(server_pg_list_hdr->addr),
4142 		    ihp->hba_isid,
4143 		    (uint8_t *)initiator_node_name,
4144 		    (uint8_t *)initiator_node_alias,
4145 		    ISNS_INITIATOR_NODE_TYPE,
4146 		    &pg_list);
4147 		if (rtn != isns_ok || pg_list == NULL) {
4148 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4149 			initiator_node_name = NULL;
4150 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
4151 			initiator_node_alias = NULL;
4152 			kmem_free(server_pg_list_hdr,
4153 			    sizeof (*server_pg_list_hdr));
4154 			server_pg_list_hdr = NULL;
4155 			rtn = EIO;
4156 			break;
4157 		}
4158 
4159 		/*
4160 		 * pg_list_sz is the size of the pg_list returned from the
4161 		 *	isns_query_all
4162 		 *
4163 		 * pg_sz_copy_out is the size of the pg_list we are going to
4164 		 *	return back to the caller
4165 		 *
4166 		 * server_pg_list_sz is total amount of data we are returning
4167 		 *	back to the caller
4168 		 */
4169 		pg_list->pg_in_cnt =
4170 		    server_pg_list_hdr->addr_port_list.pg_in_cnt;
4171 		pg_list_sz = sizeof (isns_portal_group_list_t);
4172 		if (pg_list->pg_out_cnt > 0) {
4173 			pg_list_sz += (pg_list->pg_out_cnt - 1) *
4174 			    sizeof (isns_portal_group_t);
4175 		}
4176 		/*
4177 		 * check if caller passed in a buffer with enough space
4178 		 * if there isn't enough space, fill the caller's buffer with
4179 		 * as much information as possible.
4180 		 *
4181 		 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with
4182 		 * the total number of targets found
4183 		 *
4184 		 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number
4185 		 * of targets returned
4186 		 */
4187 		if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) {
4188 			pg_sz_copy_out = sizeof (isns_portal_group_list_t);
4189 			if (pg_list->pg_in_cnt > 0) {
4190 				pg_sz_copy_out += (pg_list->pg_in_cnt - 1) *
4191 				    sizeof (isns_portal_group_t);
4192 			}
4193 			server_pg_list_sz =
4194 			    sizeof (isns_server_portal_group_list_t);
4195 			if (pg_list->pg_in_cnt > 0) {
4196 				server_pg_list_sz += (pg_list->pg_in_cnt - 1) *
4197 				    sizeof (isns_portal_group_t);
4198 			}
4199 		} else {
4200 			pg_sz_copy_out = pg_list_sz;
4201 			server_pg_list_sz =
4202 			    sizeof (isns_server_portal_group_list_t);
4203 			if (pg_list->pg_out_cnt > 0) {
4204 				server_pg_list_sz += (pg_list->pg_out_cnt - 1) *
4205 				    sizeof (isns_portal_group_t);
4206 			}
4207 		}
4208 
4209 		server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc(
4210 		    server_pg_list_sz, KM_SLEEP);
4211 
4212 		bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr),
4213 		    sizeof (server_pg_list->addr));
4214 		bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out);
4215 
4216 		if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz,
4217 		    mode) != 0) {
4218 			rtn = EFAULT;
4219 		}
4220 		DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz,
4221 		    int, pg_list_sz);
4222 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4223 		initiator_node_name = NULL;
4224 		kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
4225 		initiator_node_alias = NULL;
4226 		kmem_free(pg_list, pg_list_sz);
4227 		pg_list = NULL;
4228 		kmem_free(server_pg_list, server_pg_list_sz);
4229 		server_pg_list = NULL;
4230 		kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr));
4231 		server_pg_list_hdr = NULL;
4232 		break;
4233 
4234 	/*
4235 	 * ISCSI_GET_CONFIG_SESSIONS --
4236 	 */
4237 	case ISCSI_GET_CONFIG_SESSIONS:
4238 		/* FALLTHRU */
4239 
4240 	case ISCSI_SET_CONFIG_SESSIONS:
4241 		size = sizeof (*ics);
4242 		ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4243 		if (ics == NULL) {
4244 			rtn = EFAULT;
4245 			break;
4246 		}
4247 
4248 		/* verify version infomration */
4249 		if (ics->ics_ver != ISCSI_INTERFACE_VERSION) {
4250 			rtn = EINVAL;
4251 			kmem_free(ics, size);
4252 			ics = NULL;
4253 			break;
4254 		}
4255 
4256 		/* Check to see if we need to copy in more memory */
4257 		if (ics->ics_in > 1) {
4258 			/* record correct size */
4259 			size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in);
4260 			/* free old buffer */
4261 			kmem_free(ics, sizeof (*ics));
4262 
4263 			/* copy in complete buffer size */
4264 			ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4265 			if (ics == NULL) {
4266 				rtn = EFAULT;
4267 				break;
4268 			}
4269 		}
4270 
4271 		/* switch action based on get or set */
4272 		if (cmd == ISCSI_GET_CONFIG_SESSIONS) {
4273 			/* get */
4274 			rtn = iscsi_ioctl_get_config_sess(ihp, ics);
4275 			if (rtn == 0) {
4276 				/* copyout data for gets */
4277 				rtn = iscsi_ioctl_copyout(ics, size,
4278 				    (caddr_t)arg, mode);
4279 			} else {
4280 				kmem_free(ics, size);
4281 				ics = NULL;
4282 			}
4283 		} else {
4284 			/* set */
4285 			rtn = iscsi_ioctl_set_config_sess(ihp, ics);
4286 			if (iscsiboot_prop) {
4287 				if (iscsi_cmp_boot_sess_oid(ihp,
4288 				    ics->ics_oid)) {
4289 					/*
4290 					 * found active session for this object
4291 					 * or this is initiator object
4292 					 * with mpxio enabled
4293 					 */
4294 					if (!iscsi_reconfig_boot_sess(ihp)) {
4295 						kmem_free(ics, size);
4296 						ics = NULL;
4297 						rtn = EINVAL;
4298 						break;
4299 					}
4300 				}
4301 			}
4302 			kmem_free(ics, size);
4303 			ics = NULL;
4304 		}
4305 		break;
4306 
4307 	case ISCSI_IS_ACTIVE:
4308 		/*
4309 		 * dhcpagent calls here to check if there are
4310 		 * active iSCSI sessions
4311 		 */
4312 		instance = 0;
4313 		if (iscsiboot_prop) {
4314 			instance = 1;
4315 		}
4316 		if (!instance) {
4317 			rw_enter(&ihp->hba_sess_list_rwlock,
4318 			    RW_READER);
4319 			for (isp = ihp->hba_sess_list; isp;
4320 			    isp = isp->sess_next) {
4321 				if ((isp->sess_state ==
4322 				    ISCSI_SESS_STATE_LOGGED_IN) &&
4323 				    (isp->sess_lun_list !=
4324 				    NULL)) {
4325 					instance = 1;
4326 					break;
4327 				}
4328 			}
4329 			rw_exit(&ihp->hba_sess_list_rwlock);
4330 		}
4331 		size = sizeof (instance);
4332 		if (ddi_copyout(&instance, (caddr_t)arg, size,
4333 		    mode) != 0) {
4334 			rtn = EFAULT;
4335 		}
4336 		break;
4337 
4338 	case ISCSI_BOOTPROP_GET:
4339 		size = sizeof (*bootProp);
4340 		bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4341 		if (bootProp == NULL) {
4342 			rtn = EFAULT;
4343 			break;
4344 		}
4345 		bootProp->hba_mpxio_enabled =
4346 		    iscsi_chk_bootlun_mpxio(ihp);
4347 		if (iscsiboot_prop == NULL) {
4348 			bootProp->iscsiboot = 0;
4349 			rtn = iscsi_ioctl_copyout(bootProp, size,
4350 			    (caddr_t)arg, mode);
4351 			break;
4352 		} else {
4353 			bootProp->iscsiboot = 1;
4354 		}
4355 
4356 		if (iscsiboot_prop->boot_init.ini_name != NULL) {
4357 			(void) strncpy((char *)bootProp->ini_name.n_name,
4358 			    (char *)iscsiboot_prop->boot_init.ini_name,
4359 			    ISCSI_MAX_NAME_LEN);
4360 		}
4361 		if (iscsiboot_prop->boot_init.ini_chap_name != NULL) {
4362 			bootProp->auth.a_auth_method = authMethodCHAP;
4363 			(void) strncpy((char *)bootProp->ini_chap.c_user,
4364 			    (char *)iscsiboot_prop->boot_init.ini_chap_name,
4365 			    ISCSI_MAX_NAME_LEN);
4366 			(void) strncpy((char *)bootProp->ini_chap.c_secret,
4367 			    (char *)iscsiboot_prop->boot_init.ini_chap_sec,
4368 			    ISCSI_CHAP_SECRET_LEN);
4369 			if (iscsiboot_prop->boot_tgt.tgt_chap_name !=
4370 			    NULL) {
4371 				bootProp->auth.a_bi_auth = B_TRUE;
4372 			} else {
4373 				bootProp->auth.a_bi_auth = B_FALSE;
4374 			}
4375 		}
4376 		if (iscsiboot_prop->boot_tgt.tgt_name != NULL) {
4377 			(void) strncpy((char *)bootProp->tgt_name.n_name,
4378 			    (char *)iscsiboot_prop->boot_tgt.tgt_name,
4379 			    ISCSI_MAX_NAME_LEN);
4380 		}
4381 		if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) {
4382 			(void) strncpy((char *)bootProp->tgt_chap.c_user,
4383 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_name,
4384 			    ISCSI_MAX_NAME_LEN);
4385 			(void) strncpy((char *)bootProp->tgt_chap.c_secret,
4386 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec,
4387 			    ISCSI_CHAP_SECRET_LEN);
4388 		}
4389 
4390 		rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode);
4391 		break;
4392 
4393 	case ISCSI_TUNABLE_PARAM_SET:
4394 		tpss = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpss),
4395 		    KM_SLEEP);
4396 		if (ddi_copyin((caddr_t)arg, tpss, sizeof (*tpss), mode)) {
4397 			rtn = EFAULT;
4398 			kmem_free(tpss, sizeof (*tpss));
4399 			break;
4400 		}
4401 		rtn = iscsi_ioctl_set_tunable_param(ihp, tpss);
4402 		kmem_free(tpss, sizeof (*tpss));
4403 		break;
4404 
4405 	case ISCSI_TUNABLE_PARAM_GET:
4406 		tpsg = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpsg),
4407 		    KM_SLEEP);
4408 		if (ddi_copyin((caddr_t)arg, tpsg, sizeof (*tpsg), mode)) {
4409 			rtn = EFAULT;
4410 			kmem_free(tpsg, sizeof (*tpsg));
4411 			break;
4412 		}
4413 		if (tpsg->t_oid == ihp->hba_oid) {
4414 			/* initiator */
4415 			name = ihp->hba_name;
4416 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4417 			    tpsg) == 1) {
4418 				/*
4419 				 * no persisted tunable parameters found
4420 				 * for iscsi initiator, use default tunable
4421 				 * params for initiator node.
4422 				 */
4423 				iscsi_get_tunable_default(tpsg);
4424 			}
4425 		} else {
4426 			/* check whether it is a target oid */
4427 			name = iscsi_targetparam_get_name(tpsg->t_oid);
4428 			if (name == NULL) {
4429 				/* invalid node name */
4430 				rtn = EINVAL;
4431 				kmem_free(tpsg, sizeof (*tpsg));
4432 				break;
4433 			}
4434 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4435 			    tpsg) == 1) {
4436 				/*
4437 				 * no persisted tunable parameters found for
4438 				 * iscsi target, use initiator's configure.
4439 				 */
4440 				if (iscsi_get_persisted_tunable_param(
4441 				    (uchar_t *)ihp->hba_name, tpsg) == -1) {
4442 					/*
4443 					 * No initiator tunable parameters set
4444 					 * use default value for target
4445 					 */
4446 					iscsi_get_tunable_default(tpsg);
4447 				}
4448 			}
4449 		}
4450 
4451 		if (ddi_copyout(tpsg, (caddr_t)arg,
4452 		    sizeof (iscsi_tunable_object_t), mode) != 0) {
4453 			rtn = EFAULT;
4454 		}
4455 		kmem_free(tpsg, sizeof (*tpsg));
4456 		break;
4457 
4458 	default:
4459 		rtn = ENOTTY;
4460 		cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd);
4461 	} /* end of ioctl type switch/cases */
4462 
4463 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
4464 	    (cmd != ISCSI_SMF_GET)) {
4465 		/* other cmds need to release the service */
4466 		iscsi_client_release_service(ihp);
4467 	}
4468 
4469 	return (rtn);
4470 }
4471 
4472 /*
4473  * +--------------------------------------------------------------------+
4474  * | End of cb_ops routines					     |
4475  * +--------------------------------------------------------------------+
4476  */
4477 
4478 
4479 /*
4480  * +--------------------------------------------------------------------+
4481  * | Common scsi_tran support routines				  |
4482  * +--------------------------------------------------------------------+
4483  */
4484 
4485 /*
4486  * iscsi_i_commoncap -- SCSA host adapter get/set capability routines.
4487  *
4488  * Need to determine if any of these can be determined through the iSCSI
4489  * protocol. For now just return error on most.
4490  */
4491 /* ARGSUSED */
4492 static int
4493 iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val,
4494     int tgtonly, int doset)
4495 {
4496 	int		rtn;
4497 	int		cidx;
4498 	iscsi_lun_t	*ilp;
4499 
4500 	ASSERT((ap)->a_hba_tran->tran_hba_private != NULL);
4501 	ilp	= (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private);
4502 	ASSERT(ilp != NULL);
4503 
4504 	if (cap == (char *)0) {
4505 		return (FALSE);
4506 	}
4507 
4508 	cidx = scsi_hba_lookup_capstr(cap);
4509 	if (cidx == -1) {
4510 		return (cidx);
4511 	}
4512 
4513 	/*
4514 	 * Process setcap request.
4515 	 */
4516 	if (doset) {
4517 		/*
4518 		 * At present, we can only set binary (0/1) values
4519 		 */
4520 		switch (cidx) {
4521 		case SCSI_CAP_LUN_RESET:
4522 			if (val) {
4523 				ilp->lun_cap |= ISCSI_LUN_CAP_RESET;
4524 			} else {
4525 				ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET;
4526 			}
4527 			rtn = TRUE;
4528 			break;
4529 		default:
4530 			/*
4531 			 * None of these are settable via
4532 			 * the capability interface.
4533 			 */
4534 			rtn = FALSE;
4535 			break;
4536 		}
4537 
4538 		/*
4539 		 * Process getcap request.
4540 		 */
4541 	} else {
4542 		switch (cidx) {
4543 		case SCSI_CAP_DMA_MAX:
4544 			/* no DMA, Psuedo value */
4545 			rtn = INT32_MAX;
4546 			break;
4547 		case SCSI_CAP_INITIATOR_ID:
4548 			rtn = 7;
4549 			break;
4550 		case SCSI_CAP_ARQ:
4551 		case SCSI_CAP_RESET_NOTIFICATION:
4552 		case SCSI_CAP_TAGGED_QING:
4553 			rtn = TRUE;
4554 			break;
4555 		case SCSI_CAP_SCSI_VERSION:
4556 			rtn = SCSI_VERSION_3;
4557 			break;
4558 		case SCSI_CAP_INTERCONNECT_TYPE:
4559 			rtn = INTERCONNECT_FABRIC;
4560 			break;
4561 		case SCSI_CAP_LUN_RESET:
4562 			rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ?
4563 			    TRUE : FALSE;
4564 			break;
4565 		case SCSI_CAP_CDB_LEN:
4566 			/*
4567 			 * iSCSI RFC 3720 defines a default 16 byte
4568 			 * CDB as part of the Basic Header Segment
4569 			 * (BHS) (10.2.1) and allows for an Additional
4570 			 * Header Segment (AHS) Length of 255 * 4
4571 			 * (10.2.1.5).  The AHS length can be used
4572 			 * for different purposes two of which are
4573 			 * Extended CDB ADS (10.2.2.3) and Bidirectional
4574 			 * Expected Read-Data Length AHS (10.2.2.4).
4575 			 * The largest header of these consumes is
4576 			 * 32 bytes.  So the total Max CDB Length is
4577 			 * 16 + ((255 * 4 ) - 32) = 1004.
4578 			 */
4579 			rtn = 1004;
4580 			break;
4581 		default:
4582 			rtn = UNDEFINED;
4583 			break;
4584 		}
4585 	}
4586 	return (rtn);
4587 }
4588 
4589 /*
4590  * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding
4591  *
4592  * This routine is used to associate the tran_tgt_private to our ilp
4593  * structure.  This function is indirectly called from our
4594  * iscsi_lun_create_xxx routines.  These routines must prevent
4595  * the session and lun lists from changing during this call.
4596  */
4597 /* ARGSUSED */
4598 static int
4599 iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4600     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4601 {
4602 	iscsi_lun_t	*ilp		= NULL;
4603 	iscsi_lun_t	*ilp_check	= NULL;
4604 	iscsi_sess_t	*isp		= NULL;
4605 	char		*lun_guid	= NULL;
4606 	mdi_pathinfo_t	*pip		= NULL;
4607 	iscsi_hba_t	*ihp    = (iscsi_hba_t *)hba_tran->tran_hba_private;
4608 	char		target_port_name[MAX_NAME_PROP_SIZE];
4609 
4610 	/*
4611 	 * Here's a nice little piece of undocumented stuff.
4612 	 */
4613 	if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) {
4614 		/*
4615 		 * Very bad news if this occurs. Somehow SCSI_vhci has
4616 		 * lost the pathinfo node for this target.
4617 		 */
4618 		return (DDI_NOT_WELL_FORMED);
4619 	}
4620 
4621 	ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip);
4622 
4623 	/*
4624 	 * +----------------------------------------------------+
4625 	 * | Looking to find the target device via the property |
4626 	 * | is not required since the driver can easily get    |
4627 	 * | this information from the mdi_phci_get_private()   |
4628 	 * | call above.  This is just a consistency check	|
4629 	 * | which can be removed.				|
4630 	 */
4631 	if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) !=
4632 	    DDI_PROP_SUCCESS) {
4633 		return (DDI_NOT_WELL_FORMED);
4634 	}
4635 
4636 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4637 
4638 		/* If this isn't the matching session continue */
4639 		if (ilp->lun_sess != isp) {
4640 			continue;
4641 		}
4642 
4643 		/*
4644 		 * We are already holding the lun list rwlock
4645 		 * for this thread on the callers side of mdi_pi_online
4646 		 * or ndi_devi_online.  Which lead to this functions
4647 		 * call.
4648 		 */
4649 		for (ilp_check = isp->sess_lun_list; ilp_check;
4650 		    ilp_check = ilp_check->lun_next) {
4651 
4652 			/*
4653 			 * If this is the matching LUN and contains
4654 			 * the same LUN GUID then break we found our
4655 			 * match.
4656 			 */
4657 			if ((ilp == ilp_check) &&
4658 			    (strcmp(lun_guid, ilp_check->lun_guid) == 0)) {
4659 				break;
4660 			}
4661 		}
4662 		if (ilp_check != NULL) {
4663 			break;
4664 		}
4665 	}
4666 
4667 	/*
4668 	 * Free resource that's no longer required.
4669 	 */
4670 	if (lun_guid != NULL)
4671 		(void) mdi_prop_free(lun_guid);
4672 
4673 	if (ilp_check == NULL) {
4674 		/*
4675 		 * Failed to find iSCSI LUN in HBA chain based
4676 		 * on the GUID that was stored as a property on
4677 		 * the pathinfo node.
4678 		 */
4679 		return (DDI_NOT_WELL_FORMED);
4680 	}
4681 
4682 	if (ilp != ilp_check) {
4683 		/*
4684 		 * The iSCSI target that we found on the HBA link is
4685 		 * different than the iSCSI target that was stored as
4686 		 * private data on the pathinfo node.
4687 		 */
4688 		return (DDI_NOT_WELL_FORMED);
4689 	}
4690 	/*
4691 	 * | End of consistency check				|
4692 	 * +----------------------------------------------------+
4693 	 */
4694 
4695 	hba_tran->tran_tgt_private = ilp;
4696 
4697 	target_port_name[0] = '\0';
4698 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4699 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4700 		    "%02x%02x%02x%02x%02x%02x,%s",
4701 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4702 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4703 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4704 		    ilp->lun_sess->sess_name);
4705 	} else {
4706 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4707 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4708 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4709 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4710 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4711 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4712 	}
4713 
4714 	if (mdi_prop_update_string(pip,
4715 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
4716 		cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating '"
4717 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Path(%p) "
4718 		    "for Target(%s), Lun(%d) Failed",
4719 		    (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num);
4720 	}
4721 
4722 	return (DDI_SUCCESS);
4723 }
4724 
4725 /*
4726  * iscsi_phys_lun_init - attempts to complete a ndi binding
4727  *
4728  * This routine is used to associate the tran_tgt_private to our
4729  * ilp structure.  This function is indirectly called from our
4730  * iscsi_lun_create_xxx routines.  These routines must prevent
4731  * the session and lun lists from changing during this call.
4732  */
4733 static int
4734 iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4735     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4736 {
4737 	int		rtn	= DDI_SUCCESS;
4738 	iscsi_hba_t	*ihp	= NULL;
4739 	iscsi_sess_t	*isp	= NULL;
4740 	iscsi_lun_t	*ilp	= NULL;
4741 	char		target_port_name[MAX_NAME_PROP_SIZE];
4742 	int		*words = NULL;
4743 	uint_t		nwords = 0;
4744 
4745 	ASSERT(hba_dip);
4746 	ASSERT(lun_dip);
4747 	ASSERT(hba_tran);
4748 	ASSERT(sd);
4749 	ihp = (iscsi_hba_t *)hba_tran->tran_hba_private;
4750 	ASSERT(ihp);
4751 
4752 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip,
4753 	    DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) {
4754 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:"
4755 		    "lun for %s (instance %d)", ddi_get_name(lun_dip),
4756 		    ddi_get_instance(lun_dip));
4757 		return (DDI_FAILURE);
4758 	}
4759 
4760 	if (nwords == 0) {
4761 		ddi_prop_free(words);
4762 		return (DDI_FAILURE);
4763 	}
4764 
4765 	ASSERT(words != NULL);
4766 
4767 	/* See if we already created this session */
4768 
4769 	/* Walk the HBA's session list */
4770 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4771 		/* compare target name as the unique identifier */
4772 		if (sd->sd_address.a_target == isp->sess_oid) {
4773 			/* found match */
4774 			break;
4775 		}
4776 	}
4777 
4778 	/* If we found matching session continue searching for tgt */
4779 	if (isp != NULL) {
4780 		/*
4781 		 * Search for the matching iscsi lun structure.  We don't
4782 		 * need to hold the READER for the lun list at this point.
4783 		 * because the tran_get_name is being called from the online
4784 		 * function which is already holding a reader on the lun
4785 		 * list.
4786 		 */
4787 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
4788 			if (*words == ilp->lun_num) {
4789 				/* found match */
4790 				break;
4791 			}
4792 		}
4793 
4794 		if (ilp != NULL) {
4795 			/*
4796 			 * tgt found path it to the tran_lun_private
4797 			 * this is used later for fast access on
4798 			 * init_pkt and start
4799 			 */
4800 			hba_tran->tran_tgt_private = ilp;
4801 		} else {
4802 			/* tgt not found */
4803 			ddi_prop_free(words);
4804 			return (DDI_FAILURE);
4805 		}
4806 	} else {
4807 		/* sess not found */
4808 		ddi_prop_free(words);
4809 		return (DDI_FAILURE);
4810 	}
4811 	ddi_prop_free(words);
4812 
4813 	target_port_name[0] = '\0';
4814 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4815 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4816 		    "%02x%02x%02x%02x%02x%02x,%s",
4817 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4818 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4819 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4820 		    ilp->lun_sess->sess_name);
4821 	} else {
4822 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4823 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4824 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4825 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4826 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4827 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4828 	}
4829 
4830 	if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip,
4831 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
4832 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating '"
4833 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Target(%s), "
4834 		    "Lun(%d) Failed", ilp->lun_sess->sess_name, ilp->lun_num);
4835 	}
4836 
4837 	return (rtn);
4838 }
4839 
4840 /*
4841  * +--------------------------------------------------------------------+
4842  * | End of scsi_tran support routines					|
4843  * +--------------------------------------------------------------------+
4844  */
4845 
4846 /*
4847  * +--------------------------------------------------------------------+
4848  * | Begin of struct utility routines					|
4849  * +--------------------------------------------------------------------+
4850  */
4851 
4852 
4853 /*
4854  * iscsi_set_default_login_params - This function sets the
4855  * driver default login params.  This is using during the
4856  * creation of our iSCSI HBA structure initialization by
4857  * could be used at other times to reset back to the defaults.
4858  */
4859 void
4860 iscsi_set_default_login_params(iscsi_login_params_t *params)
4861 {
4862 	params->immediate_data		= ISCSI_DEFAULT_IMMEDIATE_DATA;
4863 	params->initial_r2t		= ISCSI_DEFAULT_INITIALR2T;
4864 	params->first_burst_length	= ISCSI_DEFAULT_FIRST_BURST_LENGTH;
4865 	params->max_burst_length	= ISCSI_DEFAULT_MAX_BURST_LENGTH;
4866 	params->data_pdu_in_order	= ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
4867 	params->data_sequence_in_order	= ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
4868 	params->default_time_to_wait	= ISCSI_DEFAULT_TIME_TO_WAIT;
4869 	params->default_time_to_retain	= ISCSI_DEFAULT_TIME_TO_RETAIN;
4870 	params->header_digest		= ISCSI_DEFAULT_HEADER_DIGEST;
4871 	params->data_digest		= ISCSI_DEFAULT_DATA_DIGEST;
4872 	params->max_recv_data_seg_len	= ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
4873 	params->max_xmit_data_seg_len	= ISCSI_DEFAULT_MAX_XMIT_SEG_LEN;
4874 	params->max_connections		= ISCSI_DEFAULT_MAX_CONNECTIONS;
4875 	params->max_outstanding_r2t	= ISCSI_DEFAULT_MAX_OUT_R2T;
4876 	params->error_recovery_level	= ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
4877 	params->ifmarker		= ISCSI_DEFAULT_IFMARKER;
4878 	params->ofmarker		= ISCSI_DEFAULT_OFMARKER;
4879 }
4880 
4881 /* Helper function to sets the driver default tunable parameters */
4882 static void
4883 iscsi_set_default_tunable_params(iscsi_tunable_params_t *params)
4884 {
4885 	params->recv_login_rsp_timeout = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
4886 	params->conn_login_max = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
4887 	params->polling_login_delay = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
4888 }
4889 
4890 /*
4891  * +--------------------------------------------------------------------+
4892  * | End of struct utility routines				     |
4893  * +--------------------------------------------------------------------+
4894  */
4895 
4896 /*
4897  * +--------------------------------------------------------------------+
4898  * | Begin of ioctl utility routines				    |
4899  * +--------------------------------------------------------------------+
4900  */
4901 
4902 /*
4903  * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM
4904  * IOCTL
4905  */
4906 int
4907 iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag,
4908     iscsi_param_get_t *ipgp) {
4909 	int rtn = 0;
4910 
4911 	/* ---- Default to settable, possibly changed later ---- */
4912 	ipgp->g_value.v_valid    = valid_flag;
4913 	ipgp->g_value.v_settable = B_TRUE;
4914 
4915 	switch (ipgp->g_param) {
4916 	/*
4917 	 * Boolean parameters
4918 	 */
4919 	case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
4920 		ipgp->g_value.v_bool.b_current =
4921 		    params->data_sequence_in_order;
4922 		ipgp->g_value.v_bool.b_default =
4923 		    ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
4924 		break;
4925 	case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
4926 		ipgp->g_value.v_bool.b_current =
4927 		    params->immediate_data;
4928 		ipgp->g_value.v_bool.b_default =
4929 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
4930 		break;
4931 	case ISCSI_LOGIN_PARAM_INITIAL_R2T:
4932 		ipgp->g_value.v_bool.b_current =
4933 		    params->initial_r2t;
4934 		ipgp->g_value.v_bool.b_default =
4935 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
4936 		break;
4937 	case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
4938 		ipgp->g_value.v_bool.b_current =
4939 		    params->data_pdu_in_order;
4940 		ipgp->g_value.v_bool.b_default =
4941 		    ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
4942 		break;
4943 
4944 	/*
4945 	 * Integer parameters
4946 	 */
4947 	case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
4948 		ipgp->g_value.v_integer.i_current = params->header_digest;
4949 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST;
4950 		ipgp->g_value.v_integer.i_min = 0;
4951 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST;
4952 		ipgp->g_value.v_integer.i_incr = 1;
4953 		break;
4954 	case ISCSI_LOGIN_PARAM_DATA_DIGEST:
4955 		ipgp->g_value.v_integer.i_current = params->data_digest;
4956 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST;
4957 		ipgp->g_value.v_integer.i_min = 0;
4958 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST;
4959 		ipgp->g_value.v_integer.i_incr = 1;
4960 		break;
4961 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
4962 		ipgp->g_value.v_integer.i_current =
4963 		    params->default_time_to_retain;
4964 		ipgp->g_value.v_integer.i_default =
4965 		    ISCSI_DEFAULT_TIME_TO_RETAIN;
4966 		ipgp->g_value.v_integer.i_min = 0;
4967 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN;
4968 		ipgp->g_value.v_integer.i_incr = 1;
4969 		break;
4970 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
4971 		ipgp->g_value.v_integer.i_current =
4972 		    params->default_time_to_wait;
4973 		ipgp->g_value.v_integer.i_default =
4974 		    ISCSI_DEFAULT_TIME_TO_WAIT;
4975 		ipgp->g_value.v_integer.i_min = 0;
4976 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT;
4977 		ipgp->g_value.v_integer.i_incr = 1;
4978 		break;
4979 	case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
4980 		ipgp->g_value.v_integer.i_current =
4981 		    params->error_recovery_level;
4982 		ipgp->g_value.v_integer.i_default =
4983 		    ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
4984 		ipgp->g_value.v_integer.i_min = 0;
4985 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL;
4986 		ipgp->g_value.v_integer.i_incr = 1;
4987 		ipgp->g_value.v_settable = B_FALSE;
4988 		break;
4989 	case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
4990 		ipgp->g_value.v_integer.i_current =
4991 		    params->first_burst_length;
4992 		ipgp->g_value.v_integer.i_default =
4993 		    ISCSI_DEFAULT_FIRST_BURST_LENGTH;
4994 		ipgp->g_value.v_integer.i_min = 512;
4995 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH;
4996 		ipgp->g_value.v_integer.i_incr = 1;
4997 		break;
4998 	case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
4999 		ipgp->g_value.v_integer.i_current =
5000 		    params->max_burst_length;
5001 		ipgp->g_value.v_integer.i_default =
5002 		    ISCSI_DEFAULT_MAX_BURST_LENGTH;
5003 		ipgp->g_value.v_integer.i_min = 512;
5004 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH;
5005 		ipgp->g_value.v_integer.i_incr = 1;
5006 		break;
5007 	case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5008 		ipgp->g_value.v_integer.i_current =
5009 		    params->max_connections;
5010 		ipgp->g_value.v_settable = B_FALSE;
5011 		ipgp->g_value.v_integer.i_default =
5012 		    ISCSI_DEFAULT_MAX_CONNECTIONS;
5013 		ipgp->g_value.v_integer.i_min = 1;
5014 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS;
5015 		ipgp->g_value.v_integer.i_incr = 1;
5016 		break;
5017 	case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5018 		ipgp->g_value.v_integer.i_current =
5019 		    params->max_outstanding_r2t;
5020 		ipgp->g_value.v_settable = B_FALSE;
5021 		ipgp->g_value.v_integer.i_default =
5022 		    ISCSI_DEFAULT_MAX_OUT_R2T;
5023 		ipgp->g_value.v_integer.i_min = 1;
5024 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T;
5025 		ipgp->g_value.v_integer.i_incr = 1;
5026 		break;
5027 	case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5028 		ipgp->g_value.v_integer.i_current =
5029 		    params->max_recv_data_seg_len;
5030 		ipgp->g_value.v_integer.i_default =
5031 		    ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
5032 		ipgp->g_value.v_integer.i_min = 512;
5033 		ipgp->g_value.v_integer.i_max =
5034 		    ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
5035 		ipgp->g_value.v_integer.i_incr = 1;
5036 		break;
5037 	default:
5038 		rtn = EINVAL;
5039 	}
5040 
5041 	return (rtn);
5042 }
5043 
5044 /*
5045  * +--------------------------------------------------------------------+
5046  * | End of ioctl utility routines                                      |
5047  * +--------------------------------------------------------------------+
5048  */
5049 
5050 /*
5051  * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a
5052  * IEEE safe address.  IEEE addresses have a number of characters
5053  * set aside as reserved.
5054  */
5055 static void
5056 iscsi_get_name_from_iqn(char *name, int name_max_len)
5057 {
5058 	char	*tmp		= NULL;
5059 	char	*oldch		= NULL;
5060 	char	*newch		= NULL;
5061 
5062 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5063 
5064 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5065 	    oldch++, newch++) {
5066 		switch (*oldch) {
5067 		case ':':
5068 			*newch++ = '%';
5069 			*newch++ = '3';
5070 			*newch = 'A';
5071 			break;
5072 		case ' ':
5073 			*newch++ = '%';
5074 			*newch++ = '2';
5075 			*newch = '0';
5076 			break;
5077 		case '@':
5078 			*newch++ = '%';
5079 			*newch++ = '4';
5080 			*newch = '0';
5081 			break;
5082 		case '/':
5083 			*newch++ = '%';
5084 			*newch++ = '2';
5085 			*newch = 'F';
5086 			break;
5087 		default:
5088 			*newch = *oldch;
5089 		}
5090 	}
5091 	(void) strncpy(name, tmp, name_max_len);
5092 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5093 }
5094 
5095 /*
5096  * iscsi_get_name_to_iqn - Converts IEEE safe address back
5097  * into a iscsi iqn/eui.
5098  */
5099 static void
5100 iscsi_get_name_to_iqn(char *name, int name_max_len)
5101 {
5102 	char	*tmp		= NULL;
5103 	char	*oldch		= NULL;
5104 	char	*newch		= NULL;
5105 
5106 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5107 
5108 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5109 	    oldch++, newch++) {
5110 		if (*oldch == '%') {
5111 			switch (*(oldch+1)) {
5112 			case '2':
5113 				if (*(oldch+2) == '0') {
5114 					*newch = ' ';
5115 					oldch += 2;
5116 				} else if (*(oldch+2) == 'F') {
5117 					*newch = '/';
5118 					oldch += 2;
5119 				} else {
5120 					*newch = *oldch;
5121 				}
5122 				break;
5123 			case '3':
5124 				if (*(oldch+2) == 'A') {
5125 					*newch = ':';
5126 					oldch += 2;
5127 				} else {
5128 					*newch = *oldch;
5129 				}
5130 				break;
5131 			case '4':
5132 				if (*(oldch+2) == '0') {
5133 					*newch = '@';
5134 					oldch += 2;
5135 				} else {
5136 					*newch = *oldch;
5137 				}
5138 				break;
5139 			default:
5140 				*newch = *oldch;
5141 			}
5142 		} else {
5143 			*newch = *oldch;
5144 		}
5145 	}
5146 	(void) strncpy(name, tmp, name_max_len);
5147 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5148 }
5149 
5150 /*
5151  * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl
5152  *
5153  * On return 0 means persisted parameter found
5154  */
5155 int
5156 iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp,
5157     iscsi_login_params_t *params)
5158 {
5159 	int rtn = 1;
5160 	persistent_param_t *pparam;
5161 
5162 	if (name == NULL || strlen((char *)name) == 0) {
5163 		return (rtn);
5164 	}
5165 
5166 	pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP);
5167 
5168 	if (persistent_param_get((char *)name, pparam) == B_TRUE) {
5169 		if (pparam->p_bitmap & (1 << ipgp->g_param)) {
5170 			/* Found configured parameter. */
5171 			bcopy(&pparam->p_params, params, sizeof (*params));
5172 			rtn = 0;
5173 		}
5174 	}
5175 
5176 	kmem_free(pparam, sizeof (*pparam));
5177 
5178 	return (rtn);
5179 }
5180 
5181 /*
5182  * iscsi_override_target_default - helper function set the target's default
5183  * login parameter if there is a configured initiator parameter.
5184  *
5185  */
5186 static void
5187 iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg)
5188 {
5189 	persistent_param_t *pp;
5190 	iscsi_login_params_t *params;
5191 
5192 	pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP);
5193 	if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) {
5194 		if (pp->p_bitmap & (1 << ipg->g_param)) {
5195 			params = &pp->p_params;
5196 			switch (ipg->g_param) {
5197 			case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
5198 				ipg->g_value.v_bool.b_default =
5199 				    params->data_sequence_in_order;
5200 				break;
5201 			case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
5202 				ipg->g_value.v_bool.b_default =
5203 				    params->immediate_data;
5204 				break;
5205 			case ISCSI_LOGIN_PARAM_INITIAL_R2T:
5206 				ipg->g_value.v_bool.b_default =
5207 				    params->initial_r2t;
5208 				break;
5209 			case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
5210 				ipg->g_value.v_bool.b_default =
5211 				    params->data_pdu_in_order;
5212 				break;
5213 			case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
5214 				ipg->g_value.v_integer.i_default =
5215 				    params->header_digest;
5216 				break;
5217 			case ISCSI_LOGIN_PARAM_DATA_DIGEST:
5218 				ipg->g_value.v_integer.i_default =
5219 				    params->data_digest;
5220 				break;
5221 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
5222 				ipg->g_value.v_integer.i_default =
5223 				    params->default_time_to_retain;
5224 				break;
5225 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
5226 				ipg->g_value.v_integer.i_default =
5227 				    params->default_time_to_wait;
5228 				break;
5229 			case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
5230 				ipg->g_value.v_integer.i_default =
5231 				    params->error_recovery_level;
5232 				break;
5233 			case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
5234 				ipg->g_value.v_integer.i_default =
5235 				    params->first_burst_length;
5236 				break;
5237 			case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
5238 				ipg->g_value.v_integer.i_default =
5239 				    params->max_burst_length;
5240 				break;
5241 			case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5242 				ipg->g_value.v_integer.i_default =
5243 				    params->max_connections;
5244 				break;
5245 			case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5246 				ipg->g_value.v_integer.i_default =
5247 				    params->max_outstanding_r2t;
5248 				break;
5249 			case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5250 				ipg->g_value.v_integer.i_default =
5251 				    params->max_xmit_data_seg_len;
5252 				break;
5253 			default:
5254 				break;
5255 			}
5256 		}
5257 	}
5258 	kmem_free(pp, sizeof (*pp));
5259 }
5260 
5261 static boolean_t
5262 iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid)
5263 {
5264 	iscsi_sess_t *isp = NULL;
5265 
5266 	if (iscsi_chk_bootlun_mpxio(ihp)) {
5267 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
5268 			if ((isp->sess_oid == oid) && isp->sess_boot) {
5269 				/* oid is session object */
5270 				break;
5271 			}
5272 			if ((isp->sess_target_oid == oid) && isp->sess_boot) {
5273 				/*
5274 				 * oid is target object while
5275 				 * this session is boot session
5276 				 */
5277 				break;
5278 			}
5279 		}
5280 		if (oid == ihp->hba_oid) {
5281 			/* oid is initiator object id */
5282 			return (B_TRUE);
5283 		} else if ((isp != NULL) && (isp->sess_boot)) {
5284 			/* oid is boot session object id */
5285 			return (B_TRUE);
5286 		}
5287 	}
5288 	return (B_FALSE);
5289 }
5290 
5291 /*
5292  * iscsi_client_request_service - request the iSCSI service
5293  *     returns true if the service is enabled and increases the count
5294  *     returns false if the service is disabled
5295  *     blocks until the service status is either enabled or disabled
5296  */
5297 boolean_t
5298 iscsi_client_request_service(iscsi_hba_t *ihp) {
5299 	boolean_t	rval = B_TRUE;
5300 
5301 	mutex_enter(&ihp->hba_service_lock);
5302 	while ((ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) ||
5303 	    (ihp->hba_service_client_count == UINT_MAX)) {
5304 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5305 	}
5306 	if (ihp->hba_service_status == ISCSI_SERVICE_ENABLED) {
5307 		ihp->hba_service_client_count++;
5308 	} else {
5309 		rval = B_FALSE;
5310 	}
5311 	mutex_exit(&ihp->hba_service_lock);
5312 
5313 	return (rval);
5314 }
5315 
5316 /*
5317  * iscsi_client_release_service - decrease the count and wake up
5318  *     blocking threads if the count reaches zero
5319  */
5320 void
5321 iscsi_client_release_service(iscsi_hba_t *ihp) {
5322 	mutex_enter(&ihp->hba_service_lock);
5323 	ASSERT(ihp->hba_service_client_count > 0);
5324 	ihp->hba_service_client_count--;
5325 	if (ihp->hba_service_client_count == 0) {
5326 		cv_broadcast(&ihp->hba_service_cv);
5327 	}
5328 	mutex_exit(&ihp->hba_service_lock);
5329 }
5330 
5331 /*
5332  * iscsi_enter_service_zone - enter the service zone, should be called
5333  * before doing any modifications to the service status
5334  * return TRUE if the zone is entered
5335  *	  FALSE if no need to enter the zone
5336  */
5337 static boolean_t
5338 iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status) {
5339 	if ((status != ISCSI_SERVICE_ENABLED) &&
5340 	    (status != ISCSI_SERVICE_DISABLED)) {
5341 		return (B_FALSE);
5342 	}
5343 
5344 	mutex_enter(&ihp->hba_service_lock);
5345 	while (ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) {
5346 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5347 	}
5348 	if (ihp->hba_service_status == status) {
5349 		mutex_exit(&ihp->hba_service_lock);
5350 		return (B_FALSE);
5351 	}
5352 	ihp->hba_service_status = ISCSI_SERVICE_TRANSITION;
5353 	while (ihp->hba_service_client_count > 0) {
5354 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5355 	}
5356 	mutex_exit(&ihp->hba_service_lock);
5357 	return (B_TRUE);
5358 }
5359 
5360 /*
5361  * iscsi_exit_service_zone - exits the service zone and wakes up waiters
5362  */
5363 static void
5364 iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status) {
5365 	if ((status != ISCSI_SERVICE_ENABLED) &&
5366 	    (status != ISCSI_SERVICE_DISABLED)) {
5367 		return;
5368 	}
5369 
5370 	mutex_enter(&ihp->hba_service_lock);
5371 	ASSERT(ihp->hba_service_status == ISCSI_SERVICE_TRANSITION);
5372 	ihp->hba_service_status = status;
5373 	cv_broadcast(&ihp->hba_service_cv);
5374 	mutex_exit(&ihp->hba_service_lock);
5375 }
5376 
5377 static void
5378 iscsi_check_miniroot(iscsi_hba_t *ihp) {
5379 	if (strncmp(rootfs.bo_name, "/ramdisk", 8) == 0) {
5380 		/*
5381 		 * in miniroot we don't have the persistent store
5382 		 * so just to need to ensure an enabled status
5383 		 */
5384 		ihp->hba_service_status = ISCSI_SERVICE_ENABLED;
5385 	}
5386 }
5387 
5388 static void
5389 iscsi_get_tunable_default(iscsi_tunable_object_t *param) {
5390 	int	param_id = 0;
5391 
5392 	param_id = 1 << (param->t_param - 1);
5393 	param->t_set = B_FALSE;
5394 	switch (param_id) {
5395 	case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5396 		param->t_value.v_integer = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
5397 		break;
5398 	case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5399 		param->t_value.v_integer = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
5400 		break;
5401 	case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5402 		param->t_value.v_integer = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
5403 		break;
5404 	default:
5405 		break;
5406 	}
5407 }
5408 
5409 /*
5410  * iscsi_get_persisted_tunable_param * - a helper to ISCSI_TUNABLE_PARAM_GET
5411  * ioctl
5412  * return:
5413  *    0 	persisted tunable parameter found
5414  *    1		persisted tunable parameter not found
5415  */
5416 static int
5417 iscsi_get_persisted_tunable_param(uchar_t *name, iscsi_tunable_object_t *tpsg)
5418 {
5419 	int rtn = 1;
5420 	int param_id = 0;
5421 	persistent_tunable_param_t *pparam;
5422 
5423 	if ((name == NULL) || strlen((char *)name) == 0) {
5424 		return (rtn);
5425 	}
5426 
5427 	tpsg->t_set = B_FALSE;
5428 	pparam = (persistent_tunable_param_t *)kmem_zalloc(sizeof (*pparam),
5429 	    KM_SLEEP);
5430 	if (persistent_get_tunable_param((char *)name, pparam) == B_TRUE) {
5431 		if (pparam->p_bitmap & (1 << (tpsg->t_param - 1))) {
5432 			tpsg->t_set = B_TRUE;
5433 			param_id = 1 << (tpsg->t_param - 1);
5434 			switch (param_id) {
5435 			case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5436 				tpsg->t_value.v_integer =
5437 				    pparam->p_params.recv_login_rsp_timeout;
5438 				break;
5439 			case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5440 				tpsg->t_value.v_integer =
5441 				    pparam->p_params.polling_login_delay;
5442 				break;
5443 			case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5444 				tpsg->t_value.v_integer =
5445 				    pparam->p_params.conn_login_max;
5446 				break;
5447 			default:
5448 				break;
5449 			}
5450 			rtn = 0;
5451 		}
5452 	}
5453 
5454 	kmem_free(pparam, sizeof (*pparam));
5455 
5456 	return (rtn);
5457 }
5458