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