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