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