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