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