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