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