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