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