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