Home | History | Annotate | Download | only in iscsi
      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 - iu.iu_ucmd.uscsi_rqresid,
   3961 			    mode) != 0) {
   3962 				rtn = EFAULT;
   3963 			}
   3964 			kmem_free(iu.iu_ucmd.uscsi_rqbuf,
   3965 			    iu.iu_ucmd.uscsi_rqlen);
   3966 		}
   3967 
   3968 #ifdef _MULTI_DATAMODEL
   3969 		switch (model = ddi_model_convert_from(mode & FMODELS)) {
   3970 		case DDI_MODEL_ILP32:
   3971 			if (iu.iu_ucmd.uscsi_status != 0) {
   3972 				iu32_caller.iu_ucmd.uscsi_status =
   3973 				    iu.iu_ucmd.uscsi_status;
   3974 				iu32_caller.iu_ucmd.uscsi_rqresid =
   3975 				    iu.iu_ucmd.uscsi_rqresid;
   3976 			}
   3977 			iu32_caller.iu_ucmd.uscsi_resid =
   3978 			    iu.iu_ucmd.uscsi_resid;
   3979 			if (ddi_copyout((void *)&iu32_caller, (caddr_t)arg,
   3980 			    sizeof (iscsi_uscsi32_t), mode) != 0) {
   3981 				rtn = EFAULT;
   3982 			}
   3983 			break;
   3984 		case DDI_MODEL_NONE:
   3985 			if (iu.iu_ucmd.uscsi_status != 0) {
   3986 				iu_caller.iu_ucmd.uscsi_status =
   3987 				    iu.iu_ucmd.uscsi_status;
   3988 				iu_caller.iu_ucmd.uscsi_rqresid =
   3989 				    iu.iu_ucmd.uscsi_rqresid;
   3990 			}
   3991 			iu_caller.iu_ucmd.uscsi_resid = iu.iu_ucmd.uscsi_resid;
   3992 			if (ddi_copyout((void *)&iu_caller, (caddr_t)arg,
   3993 			    sizeof (iscsi_uscsi_t), mode) != 0) {
   3994 				rtn = EFAULT;
   3995 			}
   3996 			break;
   3997 		default:
   3998 			ASSERT(FALSE);
   3999 		}
   4000 #endif /* _MULTI_DATAMODEL */
   4001 		rw_exit(&ihp->hba_sess_list_rwlock);
   4002 		break;
   4003 
   4004 	case ISCSI_SMF_ONLINE:
   4005 		if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) {
   4006 			rtn = EFAULT;
   4007 			break;
   4008 		}
   4009 		/* just a theoretical case */
   4010 		if (ihp->hba_persistent_loaded == B_FALSE) {
   4011 			rtn = EFAULT;
   4012 			break;
   4013 		}
   4014 
   4015 		/* doesn't need to overwrite the status anymore */
   4016 		mutex_enter(&ihp->hba_service_lock);
   4017 		if (ihp->hba_service_status_overwrite == B_TRUE) {
   4018 			ihp->hba_service_status = ISCSI_SERVICE_DISABLED;
   4019 			ihp->hba_service_status_overwrite = B_FALSE;
   4020 		}
   4021 		mutex_exit(&ihp->hba_service_lock);
   4022 
   4023 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_ENABLED) ==
   4024 		    B_FALSE) {
   4025 			break;
   4026 		}
   4027 
   4028 		rval = iscsi_door_bind(did);
   4029 		if (rval == B_TRUE) {
   4030 			rval = iscsid_start(ihp);
   4031 			if (rval == B_FALSE) {
   4032 				iscsi_door_unbind();
   4033 			}
   4034 		}
   4035 
   4036 		if (rval == B_TRUE) {
   4037 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
   4038 		} else {
   4039 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
   4040 			rtn = EFAULT;
   4041 		}
   4042 
   4043 		break;
   4044 
   4045 	case ISCSI_SMF_OFFLINE:
   4046 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_DISABLED)
   4047 		    == B_FALSE) {
   4048 			break;
   4049 		}
   4050 
   4051 		rval = iscsid_stop(ihp);
   4052 
   4053 		if (rval == B_TRUE) {
   4054 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
   4055 			iscsi_door_unbind();
   4056 		} else {
   4057 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
   4058 			rtn = EFAULT;
   4059 		}
   4060 		break;
   4061 
   4062 	case ISCSI_SMF_GET:
   4063 		mutex_enter(&ihp->hba_service_lock);
   4064 		while (ihp->hba_service_status ==
   4065 		    ISCSI_SERVICE_TRANSITION) {
   4066 			cv_wait(&ihp->hba_service_cv,
   4067 			    &ihp->hba_service_lock);
   4068 		}
   4069 		if (ddi_copyout((void *)&ihp->hba_service_status,
   4070 		    (caddr_t)arg, sizeof (boolean_t), mode) != 0) {
   4071 			rtn = EFAULT;
   4072 		}
   4073 		mutex_exit(&ihp->hba_service_lock);
   4074 		break;
   4075 
   4076 	case ISCSI_DISCOVERY_EVENTS:
   4077 		/*
   4078 		 * If discovery has not been completed and not in progress,
   4079 		 * poke the discovery methods
   4080 		 */
   4081 		mutex_enter(&ihp->hba_discovery_events_mutex);
   4082 		method = ihp->hba_discovery_events;
   4083 		if ((method != ISCSI_ALL_DISCOVERY_METHODS) &&
   4084 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
   4085 			ihp->hba_discovery_in_progress = B_TRUE;
   4086 			mutex_exit(&ihp->hba_discovery_events_mutex);
   4087 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
   4088 			mutex_enter(&ihp->hba_discovery_events_mutex);
   4089 			ihp->hba_discovery_in_progress = B_FALSE;
   4090 			method = ihp->hba_discovery_events;
   4091 		}
   4092 		mutex_exit(&ihp->hba_discovery_events_mutex);
   4093 
   4094 		if (ddi_copyout((void *)&method, (caddr_t)arg,
   4095 		    sizeof (method), mode) != 0)
   4096 			rtn = EFAULT;
   4097 		break;
   4098 
   4099 	/*
   4100 	 * ISCSI_SENDTGTS_GET --
   4101 	 */
   4102 	case ISCSI_SENDTGTS_GET:
   4103 		stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
   4104 		    sizeof (*stl_hdr));
   4105 		if (stl_hdr == NULL) {
   4106 			rtn = EFAULT;
   4107 			break;
   4108 		}
   4109 
   4110 		if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) {
   4111 			rtn = EINVAL;
   4112 			kmem_free(stl_hdr, sizeof (*stl_hdr));
   4113 			break;
   4114 		}
   4115 
   4116 		/* calculate how much memory user allocated for SendTgts */
   4117 		stl_sz = sizeof (*stl_hdr);
   4118 		if (stl_hdr->stl_in_cnt > 0) {
   4119 			stl_sz += ((stl_hdr->stl_in_cnt - 1) *
   4120 			    sizeof (iscsi_sendtgts_entry_t));
   4121 		}
   4122 
   4123 		/* allocate local SendTgts list of the same size */
   4124 		istl = kmem_zalloc(stl_sz, KM_SLEEP);
   4125 		bcopy(stl_hdr, istl, sizeof (*stl_hdr));
   4126 		kmem_free(stl_hdr, sizeof (*stl_hdr));
   4127 
   4128 		/* lock interface so only one SendTargets operation occurs */
   4129 		sema_p(&ihp->hba_sendtgts_semaphore);
   4130 
   4131 		rtn = iscsi_ioctl_sendtgts_get(ihp, istl);
   4132 
   4133 		if (rtn == 0) {
   4134 			rtn = iscsi_ioctl_copyout(istl, stl_sz,
   4135 			    (caddr_t)arg, mode);
   4136 		}
   4137 
   4138 		/* release lock to allow another SendTargets discovery */
   4139 		sema_v(&ihp->hba_sendtgts_semaphore);
   4140 
   4141 		break;
   4142 
   4143 		/*
   4144 		 * ISCSI_ISNS_SERVER_GET --
   4145 		 */
   4146 	case ISCSI_ISNS_SERVER_GET:
   4147 		server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
   4148 		    sizeof (*server_pg_list_hdr));
   4149 		if (server_pg_list_hdr == NULL) {
   4150 			rtn = EFAULT;
   4151 			break;
   4152 		}
   4153 
   4154 		/* If iSNS discovery mode is not set, return with zero entry */
   4155 		method = persistent_disc_meth_get();
   4156 		if ((method & iSCSIDiscoveryMethodISNS) == 0) {
   4157 			kmem_free(server_pg_list_hdr,
   4158 			    sizeof (*server_pg_list_hdr));
   4159 			server_pg_list_hdr = NULL;
   4160 			rtn = EACCES;
   4161 			break;
   4162 		}
   4163 
   4164 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
   4165 		if (persistent_initiator_name_get(initiator_node_name,
   4166 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
   4167 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
   4168 			initiator_node_name = NULL;
   4169 			kmem_free(server_pg_list_hdr,
   4170 			    sizeof (*server_pg_list_hdr));
   4171 			server_pg_list_hdr = NULL;
   4172 			rtn = EIO;
   4173 			break;
   4174 		}
   4175 		if (strlen(initiator_node_name) == 0) {
   4176 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
   4177 			initiator_node_name = NULL;
   4178 			kmem_free(server_pg_list_hdr,
   4179 			    sizeof (*server_pg_list_hdr));
   4180 			server_pg_list_hdr = NULL;
   4181 			rtn = EIO;
   4182 			break;
   4183 		}
   4184 
   4185 		initiator_node_alias = kmem_zalloc(
   4186 		    ISCSI_MAX_NAME_LEN, KM_SLEEP);
   4187 		if (persistent_alias_name_get(initiator_node_alias,
   4188 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
   4189 			initiator_node_alias[0] = '\0';
   4190 		}
   4191 		rtn = isns_query_one_server(&(server_pg_list_hdr->addr),
   4192 		    ihp->hba_isid,
   4193 		    (uint8_t *)initiator_node_name,
   4194 		    (uint8_t *)initiator_node_alias,
   4195 		    ISNS_INITIATOR_NODE_TYPE,
   4196 		    &pg_list);
   4197 		if (rtn != isns_ok || pg_list == NULL) {
   4198 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
   4199 			initiator_node_name = NULL;
   4200 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
   4201 			initiator_node_alias = NULL;
   4202 			kmem_free(server_pg_list_hdr,
   4203 			    sizeof (*server_pg_list_hdr));
   4204 			server_pg_list_hdr = NULL;
   4205 			rtn = EIO;
   4206 			break;
   4207 		}
   4208 
   4209 		/*
   4210 		 * pg_list_sz is the size of the pg_list returned from the
   4211 		 *	isns_query_all
   4212 		 *
   4213 		 * pg_sz_copy_out is the size of the pg_list we are going to
   4214 		 *	return back to the caller
   4215 		 *
   4216 		 * server_pg_list_sz is total amount of data we are returning
   4217 		 *	back to the caller
   4218 		 */
   4219 		pg_list->pg_in_cnt =
   4220 		    server_pg_list_hdr->addr_port_list.pg_in_cnt;
   4221 		pg_list_sz = sizeof (isns_portal_group_list_t);
   4222 		if (pg_list->pg_out_cnt > 0) {
   4223 			pg_list_sz += (pg_list->pg_out_cnt - 1) *
   4224 			    sizeof (isns_portal_group_t);
   4225 		}
   4226 		/*
   4227 		 * check if caller passed in a buffer with enough space
   4228 		 * if there isn't enough space, fill the caller's buffer with
   4229 		 * as much information as possible.
   4230 		 *
   4231 		 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with
   4232 		 * the total number of targets found
   4233 		 *
   4234 		 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number
   4235 		 * of targets returned
   4236 		 */
   4237 		if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) {
   4238 			pg_sz_copy_out = sizeof (isns_portal_group_list_t);
   4239 			if (pg_list->pg_in_cnt > 0) {
   4240 				pg_sz_copy_out += (pg_list->pg_in_cnt - 1) *
   4241 				    sizeof (isns_portal_group_t);
   4242 			}
   4243 			server_pg_list_sz =
   4244 			    sizeof (isns_server_portal_group_list_t);
   4245 			if (pg_list->pg_in_cnt > 0) {
   4246 				server_pg_list_sz += (pg_list->pg_in_cnt - 1) *
   4247 				    sizeof (isns_portal_group_t);
   4248 			}
   4249 		} else {
   4250 			pg_sz_copy_out = pg_list_sz;
   4251 			server_pg_list_sz =
   4252 			    sizeof (isns_server_portal_group_list_t);
   4253 			if (pg_list->pg_out_cnt > 0) {
   4254 				server_pg_list_sz += (pg_list->pg_out_cnt - 1) *
   4255 				    sizeof (isns_portal_group_t);
   4256 			}
   4257 		}
   4258 
   4259 		server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc(
   4260 		    server_pg_list_sz, KM_SLEEP);
   4261 
   4262 		bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr),
   4263 		    sizeof (server_pg_list->addr));
   4264 		bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out);
   4265 
   4266 		if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz,
   4267 		    mode) != 0) {
   4268 			rtn = EFAULT;
   4269 		}
   4270 		DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz,
   4271 		    int, pg_list_sz);
   4272 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
   4273 		initiator_node_name = NULL;
   4274 		kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
   4275 		initiator_node_alias = NULL;
   4276 		kmem_free(pg_list, pg_list_sz);
   4277 		pg_list = NULL;
   4278 		kmem_free(server_pg_list, server_pg_list_sz);
   4279 		server_pg_list = NULL;
   4280 		kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr));
   4281 		server_pg_list_hdr = NULL;
   4282 		break;
   4283 
   4284 	/*
   4285 	 * ISCSI_GET_CONFIG_SESSIONS --
   4286 	 */
   4287 	case ISCSI_GET_CONFIG_SESSIONS:
   4288 		/* FALLTHRU */
   4289 
   4290 	case ISCSI_SET_CONFIG_SESSIONS:
   4291 		size = sizeof (*ics);
   4292 		ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
   4293 		if (ics == NULL) {
   4294 			rtn = EFAULT;
   4295 			break;
   4296 		}
   4297 
   4298 		/* verify version infomration */
   4299 		if (ics->ics_ver != ISCSI_INTERFACE_VERSION) {
   4300 			rtn = EINVAL;
   4301 			kmem_free(ics, size);
   4302 			ics = NULL;
   4303 			break;
   4304 		}
   4305 
   4306 		/* Check to see if we need to copy in more memory */
   4307 		if (ics->ics_in > 1) {
   4308 			/* record correct size */
   4309 			size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in);
   4310 			/* free old buffer */
   4311 			kmem_free(ics, sizeof (*ics));
   4312 
   4313 			/* copy in complete buffer size */
   4314 			ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
   4315 			if (ics == NULL) {
   4316 				rtn = EFAULT;
   4317 				break;
   4318 			}
   4319 		}
   4320 
   4321 		/* switch action based on get or set */
   4322 		if (cmd == ISCSI_GET_CONFIG_SESSIONS) {
   4323 			/* get */
   4324 			rtn = iscsi_ioctl_get_config_sess(ihp, ics);
   4325 			if (rtn == 0) {
   4326 				/* copyout data for gets */
   4327 				rtn = iscsi_ioctl_copyout(ics, size,
   4328 				    (caddr_t)arg, mode);
   4329 			} else {
   4330 				kmem_free(ics, size);
   4331 				ics = NULL;
   4332 			}
   4333 		} else {
   4334 			/* set */
   4335 			rtn = iscsi_ioctl_set_config_sess(ihp, ics);
   4336 			if (iscsiboot_prop) {
   4337 				if (iscsi_cmp_boot_sess_oid(ihp,
   4338 				    ics->ics_oid)) {
   4339 					/*
   4340 					 * found active session for this object
   4341 					 * or this is initiator object
   4342 					 * with mpxio enabled
   4343 					 */
   4344 					if (!iscsi_reconfig_boot_sess(ihp)) {
   4345 						kmem_free(ics, size);
   4346 						ics = NULL;
   4347 						rtn = EINVAL;
   4348 						break;
   4349 					}
   4350 				}
   4351 			}
   4352 			kmem_free(ics, size);
   4353 			ics = NULL;
   4354 		}
   4355 		break;
   4356 
   4357 	case ISCSI_IS_ACTIVE:
   4358 		/*
   4359 		 * dhcpagent calls here to check if there are
   4360 		 * active iSCSI sessions
   4361 		 */
   4362 		instance = 0;
   4363 		if (iscsiboot_prop) {
   4364 			instance = 1;
   4365 		}
   4366 		if (!instance) {
   4367 			rw_enter(&ihp->hba_sess_list_rwlock,
   4368 			    RW_READER);
   4369 			for (isp = ihp->hba_sess_list; isp;
   4370 			    isp = isp->sess_next) {
   4371 				if ((isp->sess_state ==
   4372 				    ISCSI_SESS_STATE_LOGGED_IN) &&
   4373 				    (isp->sess_lun_list !=
   4374 				    NULL)) {
   4375 					instance = 1;
   4376 					break;
   4377 				}
   4378 			}
   4379 			rw_exit(&ihp->hba_sess_list_rwlock);
   4380 		}
   4381 		size = sizeof (instance);
   4382 		if (ddi_copyout(&instance, (caddr_t)arg, size,
   4383 		    mode) != 0) {
   4384 			rtn = EFAULT;
   4385 		}
   4386 		break;
   4387 
   4388 	case ISCSI_BOOTPROP_GET:
   4389 		size = sizeof (*bootProp);
   4390 		bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
   4391 		if (bootProp == NULL) {
   4392 			rtn = EFAULT;
   4393 			break;
   4394 		}
   4395 		bootProp->hba_mpxio_enabled =
   4396 		    iscsi_chk_bootlun_mpxio(ihp);
   4397 		if (iscsiboot_prop == NULL) {
   4398 			bootProp->iscsiboot = 0;
   4399 			rtn = iscsi_ioctl_copyout(bootProp, size,
   4400 			    (caddr_t)arg, mode);
   4401 			break;
   4402 		} else {
   4403 			bootProp->iscsiboot = 1;
   4404 		}
   4405 
   4406 		if (iscsiboot_prop->boot_init.ini_name != NULL) {
   4407 			(void) strncpy((char *)bootProp->ini_name.n_name,
   4408 			    (char *)iscsiboot_prop->boot_init.ini_name,
   4409 			    ISCSI_MAX_NAME_LEN);
   4410 		}
   4411 		if (iscsiboot_prop->boot_init.ini_chap_name != NULL) {
   4412 			bootProp->auth.a_auth_method = authMethodCHAP;
   4413 			(void) strncpy((char *)bootProp->ini_chap.c_user,
   4414 			    (char *)iscsiboot_prop->boot_init.ini_chap_name,
   4415 			    ISCSI_MAX_NAME_LEN);
   4416 			(void) strncpy((char *)bootProp->ini_chap.c_secret,
   4417 			    (char *)iscsiboot_prop->boot_init.ini_chap_sec,
   4418 			    ISCSI_CHAP_SECRET_LEN);
   4419 			if (iscsiboot_prop->boot_tgt.tgt_chap_name !=
   4420 			    NULL) {
   4421 				bootProp->auth.a_bi_auth = B_TRUE;
   4422 			} else {
   4423 				bootProp->auth.a_bi_auth = B_FALSE;
   4424 			}
   4425 		}
   4426 		if (iscsiboot_prop->boot_tgt.tgt_name != NULL) {
   4427 			(void) strncpy((char *)bootProp->tgt_name.n_name,
   4428 			    (char *)iscsiboot_prop->boot_tgt.tgt_name,
   4429 			    ISCSI_MAX_NAME_LEN);
   4430 		}
   4431 		if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) {
   4432 			(void) strncpy((char *)bootProp->tgt_chap.c_user,
   4433 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_name,
   4434 			    ISCSI_MAX_NAME_LEN);
   4435 			(void) strncpy((char *)bootProp->tgt_chap.c_secret,
   4436 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec,
   4437 			    ISCSI_CHAP_SECRET_LEN);
   4438 		}
   4439 
   4440 		rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode);
   4441 		break;
   4442 
   4443 	case ISCSI_TUNABLE_PARAM_SET:
   4444 		tpss = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpss),
   4445 		    KM_SLEEP);
   4446 		if (ddi_copyin((caddr_t)arg, tpss, sizeof (*tpss), mode)) {
   4447 			rtn = EFAULT;
   4448 			kmem_free(tpss, sizeof (*tpss));
   4449 			break;
   4450 		}
   4451 		rtn = iscsi_ioctl_set_tunable_param(ihp, tpss);
   4452 		kmem_free(tpss, sizeof (*tpss));
   4453 		break;
   4454 
   4455 	case ISCSI_TUNABLE_PARAM_GET:
   4456 		tpsg = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpsg),
   4457 		    KM_SLEEP);
   4458 		if (ddi_copyin((caddr_t)arg, tpsg, sizeof (*tpsg), mode)) {
   4459 			rtn = EFAULT;
   4460 			kmem_free(tpsg, sizeof (*tpsg));
   4461 			break;
   4462 		}
   4463 		if (tpsg->t_oid == ihp->hba_oid) {
   4464 			/* initiator */
   4465 			name = ihp->hba_name;
   4466 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
   4467 			    tpsg) == 1) {
   4468 				/*
   4469 				 * no persisted tunable parameters found
   4470 				 * for iscsi initiator, use default tunable
   4471 				 * params for initiator node.
   4472 				 */
   4473 				iscsi_get_tunable_default(tpsg);
   4474 			}
   4475 		} else {
   4476 			/* check whether it is a target oid */
   4477 			name = iscsi_targetparam_get_name(tpsg->t_oid);
   4478 			if (name == NULL) {
   4479 				/* invalid node name */
   4480 				rtn = EINVAL;
   4481 				kmem_free(tpsg, sizeof (*tpsg));
   4482 				break;
   4483 			}
   4484 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
   4485 			    tpsg) == 1) {
   4486 				/*
   4487 				 * no persisted tunable parameters found for
   4488 				 * iscsi target, use initiator's configure.
   4489 				 */
   4490 				if (iscsi_get_persisted_tunable_param(
   4491 				    (uchar_t *)ihp->hba_name, tpsg) == -1) {
   4492 					/*
   4493 					 * No initiator tunable parameters set
   4494 					 * use default value for target
   4495 					 */
   4496 					iscsi_get_tunable_default(tpsg);
   4497 				}
   4498 			}
   4499 		}
   4500 
   4501 		if (ddi_copyout(tpsg, (caddr_t)arg,
   4502 		    sizeof (iscsi_tunable_object_t), mode) != 0) {
   4503 			rtn = EFAULT;
   4504 		}
   4505 		kmem_free(tpsg, sizeof (*tpsg));
   4506 		break;
   4507 
   4508 	default:
   4509 		rtn = ENOTTY;
   4510 		cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd);
   4511 	} /* end of ioctl type switch/cases */
   4512 
   4513 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
   4514 	    (cmd != ISCSI_SMF_GET)) {
   4515 		/* other cmds need to release the service */
   4516 		iscsi_client_release_service(ihp);
   4517 	}
   4518 
   4519 	return (rtn);
   4520 }
   4521 
   4522 /*
   4523  * +--------------------------------------------------------------------+
   4524  * | End of cb_ops routines					     |
   4525  * +--------------------------------------------------------------------+
   4526  */
   4527 
   4528 
   4529 /*
   4530  * +--------------------------------------------------------------------+
   4531  * | Common scsi_tran support routines				  |
   4532  * +--------------------------------------------------------------------+
   4533  */
   4534 
   4535 /*
   4536  * iscsi_i_commoncap -- SCSA host adapter get/set capability routines.
   4537  *
   4538  * Need to determine if any of these can be determined through the iSCSI
   4539  * protocol. For now just return error on most.
   4540  */
   4541 /* ARGSUSED */
   4542 static int
   4543 iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val,
   4544     int tgtonly, int doset)
   4545 {
   4546 	int		rtn;
   4547 	int		cidx;
   4548 	iscsi_lun_t	*ilp;
   4549 
   4550 	ASSERT((ap)->a_hba_tran->tran_hba_private != NULL);
   4551 	ilp	= (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private);
   4552 	ASSERT(ilp != NULL);
   4553 
   4554 	if (cap == (char *)0) {
   4555 		return (FALSE);
   4556 	}
   4557 
   4558 	cidx = scsi_hba_lookup_capstr(cap);
   4559 	if (cidx == -1) {
   4560 		return (cidx);
   4561 	}
   4562 
   4563 	/*
   4564 	 * Process setcap request.
   4565 	 */
   4566 	if (doset) {
   4567 		/*
   4568 		 * At present, we can only set binary (0/1) values
   4569 		 */
   4570 		switch (cidx) {
   4571 		case SCSI_CAP_LUN_RESET:
   4572 			if (val) {
   4573 				ilp->lun_cap |= ISCSI_LUN_CAP_RESET;
   4574 			} else {
   4575 				ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET;
   4576 			}
   4577 			rtn = TRUE;
   4578 			break;
   4579 		default:
   4580 			/*
   4581 			 * None of these are settable via
   4582 			 * the capability interface.
   4583 			 */
   4584 			rtn = FALSE;
   4585 			break;
   4586 		}
   4587 
   4588 		/*
   4589 		 * Process getcap request.
   4590 		 */
   4591 	} else {
   4592 		switch (cidx) {
   4593 		case SCSI_CAP_DMA_MAX:
   4594 			/* no DMA, Psuedo value */
   4595 			rtn = INT32_MAX;
   4596 			break;
   4597 		case SCSI_CAP_INITIATOR_ID:
   4598 			rtn = 7;
   4599 			break;
   4600 		case SCSI_CAP_ARQ:
   4601 		case SCSI_CAP_RESET_NOTIFICATION:
   4602 		case SCSI_CAP_TAGGED_QING:
   4603 			rtn = TRUE;
   4604 			break;
   4605 		case SCSI_CAP_SCSI_VERSION:
   4606 			rtn = SCSI_VERSION_3;
   4607 			break;
   4608 		case SCSI_CAP_INTERCONNECT_TYPE:
   4609 			rtn = INTERCONNECT_FABRIC;
   4610 			break;
   4611 		case SCSI_CAP_LUN_RESET:
   4612 			rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ?
   4613 			    TRUE : FALSE;
   4614 			break;
   4615 		case SCSI_CAP_CDB_LEN:
   4616 			/*
   4617 			 * iSCSI RFC 3720 defines a default 16 byte
   4618 			 * CDB as part of the Basic Header Segment
   4619 			 * (BHS) (10.2.1) and allows for an Additional
   4620 			 * Header Segment (AHS) Length of 255 * 4
   4621 			 * (10.2.1.5).  The AHS length can be used
   4622 			 * for different purposes two of which are
   4623 			 * Extended CDB ADS (10.2.2.3) and Bidirectional
   4624 			 * Expected Read-Data Length AHS (10.2.2.4).
   4625 			 * The largest header of these consumes is
   4626 			 * 32 bytes.  So the total Max CDB Length is
   4627 			 * 16 + ((255 * 4 ) - 32) = 1004.
   4628 			 */
   4629 			rtn = 1004;
   4630 			break;
   4631 		default:
   4632 			rtn = UNDEFINED;
   4633 			break;
   4634 		}
   4635 	}
   4636 	return (rtn);
   4637 }
   4638 
   4639 /*
   4640  * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding
   4641  *
   4642  * This routine is used to associate the tran_tgt_private to our ilp
   4643  * structure.  This function is indirectly called from our
   4644  * iscsi_lun_create_xxx routines.  These routines must prevent
   4645  * the session and lun lists from changing during this call.
   4646  */
   4647 /* ARGSUSED */
   4648 static int
   4649 iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
   4650     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
   4651 {
   4652 	iscsi_lun_t	*ilp		= NULL;
   4653 	iscsi_lun_t	*ilp_check	= NULL;
   4654 	iscsi_sess_t	*isp		= NULL;
   4655 	char		*lun_guid	= NULL;
   4656 	mdi_pathinfo_t	*pip		= NULL;
   4657 	iscsi_hba_t	*ihp    = (iscsi_hba_t *)hba_tran->tran_hba_private;
   4658 	char		target_port_name[MAX_NAME_PROP_SIZE];
   4659 
   4660 	/*
   4661 	 * Here's a nice little piece of undocumented stuff.
   4662 	 */
   4663 	if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) {
   4664 		/*
   4665 		 * Very bad news if this occurs. Somehow SCSI_vhci has
   4666 		 * lost the pathinfo node for this target.
   4667 		 */
   4668 		return (DDI_NOT_WELL_FORMED);
   4669 	}
   4670 
   4671 	ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip);
   4672 
   4673 	/*
   4674 	 * +----------------------------------------------------+
   4675 	 * | Looking to find the target device via the property |
   4676 	 * | is not required since the driver can easily get    |
   4677 	 * | this information from the mdi_phci_get_private()   |
   4678 	 * | call above.  This is just a consistency check	|
   4679 	 * | which can be removed.				|
   4680 	 */
   4681 	if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) !=
   4682 	    DDI_PROP_SUCCESS) {
   4683 		return (DDI_NOT_WELL_FORMED);
   4684 	}
   4685 
   4686 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
   4687 
   4688 		/* If this isn't the matching session continue */
   4689 		if (ilp->lun_sess != isp) {
   4690 			continue;
   4691 		}
   4692 
   4693 		/*
   4694 		 * We are already holding the lun list rwlock
   4695 		 * for this thread on the callers side of mdi_pi_online
   4696 		 * or ndi_devi_online.  Which lead to this functions
   4697 		 * call.
   4698 		 */
   4699 		for (ilp_check = isp->sess_lun_list; ilp_check;
   4700 		    ilp_check = ilp_check->lun_next) {
   4701 
   4702 			/*
   4703 			 * If this is the matching LUN and contains
   4704 			 * the same LUN GUID then break we found our
   4705 			 * match.
   4706 			 */
   4707 			if ((ilp == ilp_check) &&
   4708 			    (strcmp(lun_guid, ilp_check->lun_guid) == 0)) {
   4709 				break;
   4710 			}
   4711 		}
   4712 		if (ilp_check != NULL) {
   4713 			break;
   4714 		}
   4715 	}
   4716 
   4717 	/*
   4718 	 * Free resource that's no longer required.
   4719 	 */
   4720 	if (lun_guid != NULL)
   4721 		(void) mdi_prop_free(lun_guid);
   4722 
   4723 	if (ilp_check == NULL) {
   4724 		/*
   4725 		 * Failed to find iSCSI LUN in HBA chain based
   4726 		 * on the GUID that was stored as a property on
   4727 		 * the pathinfo node.
   4728 		 */
   4729 		return (DDI_NOT_WELL_FORMED);
   4730 	}
   4731 
   4732 	if (ilp != ilp_check) {
   4733 		/*
   4734 		 * The iSCSI target that we found on the HBA link is
   4735 		 * different than the iSCSI target that was stored as
   4736 		 * private data on the pathinfo node.
   4737 		 */
   4738 		return (DDI_NOT_WELL_FORMED);
   4739 	}
   4740 	/*
   4741 	 * | End of consistency check				|
   4742 	 * +----------------------------------------------------+
   4743 	 */
   4744 
   4745 	hba_tran->tran_tgt_private = ilp;
   4746 
   4747 	target_port_name[0] = '\0';
   4748 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
   4749 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
   4750 		    "%02x%02x%02x%02x%02x%02x,%s",
   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);
   4755 	} else {
   4756 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
   4757 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
   4758 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
   4759 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
   4760 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
   4761 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
   4762 	}
   4763 
   4764 	if (mdi_prop_update_string(pip,
   4765 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
   4766 		cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating '"
   4767 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Path(%p) "
   4768 		    "for Target(%s), Lun(%d) Failed",
   4769 		    (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num);
   4770 	}
   4771 
   4772 	return (DDI_SUCCESS);
   4773 }
   4774 
   4775 /*
   4776  * iscsi_phys_lun_init - attempts to complete a ndi binding
   4777  *
   4778  * This routine is used to associate the tran_tgt_private to our
   4779  * ilp structure.  This function is indirectly called from our
   4780  * iscsi_lun_create_xxx routines.  These routines must prevent
   4781  * the session and lun lists from changing during this call.
   4782  */
   4783 static int
   4784 iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
   4785     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
   4786 {
   4787 	int		rtn	= DDI_SUCCESS;
   4788 	iscsi_hba_t	*ihp	= NULL;
   4789 	iscsi_sess_t	*isp	= NULL;
   4790 	iscsi_lun_t	*ilp	= NULL;
   4791 	char		target_port_name[MAX_NAME_PROP_SIZE];
   4792 	int		*words = NULL;
   4793 	uint_t		nwords = 0;
   4794 
   4795 	ASSERT(hba_dip);
   4796 	ASSERT(lun_dip);
   4797 	ASSERT(hba_tran);
   4798 	ASSERT(sd);
   4799 	ihp = (iscsi_hba_t *)hba_tran->tran_hba_private;
   4800 	ASSERT(ihp);
   4801 
   4802 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip,
   4803 	    DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) {
   4804 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:"
   4805 		    "lun for %s (instance %d)", ddi_get_name(lun_dip),
   4806 		    ddi_get_instance(lun_dip));
   4807 		return (DDI_FAILURE);
   4808 	}
   4809 
   4810 	if (nwords == 0) {
   4811 		ddi_prop_free(words);
   4812 		return (DDI_FAILURE);
   4813 	}
   4814 
   4815 	ASSERT(words != NULL);
   4816 
   4817 	/* See if we already created this session */
   4818 
   4819 	/* Walk the HBA's session list */
   4820 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
   4821 		/* compare target name as the unique identifier */
   4822 		if (sd->sd_address.a_target == isp->sess_oid) {
   4823 			/* found match */
   4824 			break;
   4825 		}
   4826 	}
   4827 
   4828 	/* If we found matching session continue searching for tgt */
   4829 	if (isp != NULL) {
   4830 		/*
   4831 		 * Search for the matching iscsi lun structure.  We don't
   4832 		 * need to hold the READER for the lun list at this point.
   4833 		 * because the tran_get_name is being called from the online
   4834 		 * function which is already holding a reader on the lun
   4835 		 * list.
   4836 		 */
   4837 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
   4838 			if (*words == ilp->lun_num) {
   4839 				/* found match */
   4840 				break;
   4841 			}
   4842 		}
   4843 
   4844 		if (ilp != NULL) {
   4845 			/*
   4846 			 * tgt found path it to the tran_lun_private
   4847 			 * this is used later for fast access on
   4848 			 * init_pkt and start
   4849 			 */
   4850 			hba_tran->tran_tgt_private = ilp;
   4851 		} else {
   4852 			/* tgt not found */
   4853 			ddi_prop_free(words);
   4854 			return (DDI_FAILURE);
   4855 		}
   4856 	} else {
   4857 		/* sess not found */
   4858 		ddi_prop_free(words);
   4859 		return (DDI_FAILURE);
   4860 	}
   4861 	ddi_prop_free(words);
   4862 
   4863 	target_port_name[0] = '\0';
   4864 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
   4865 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
   4866 		    "%02x%02x%02x%02x%02x%02x,%s",
   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);
   4871 	} else {
   4872 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
   4873 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
   4874 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
   4875 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
   4876 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
   4877 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
   4878 	}
   4879 
   4880 	if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip,
   4881 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
   4882 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating '"
   4883 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Target(%s), "
   4884 		    "Lun(%d) Failed", ilp->lun_sess->sess_name, ilp->lun_num);
   4885 	}
   4886 
   4887 	return (rtn);
   4888 }
   4889 
   4890 /*
   4891  * +--------------------------------------------------------------------+
   4892  * | End of scsi_tran support routines					|
   4893  * +--------------------------------------------------------------------+
   4894  */
   4895 
   4896 /*
   4897  * +--------------------------------------------------------------------+
   4898  * | Begin of struct utility routines					|
   4899  * +--------------------------------------------------------------------+
   4900  */
   4901 
   4902 
   4903 /*
   4904  * iscsi_set_default_login_params - This function sets the
   4905  * driver default login params.  This is using during the
   4906  * creation of our iSCSI HBA structure initialization by
   4907  * could be used at other times to reset back to the defaults.
   4908  */
   4909 void
   4910 iscsi_set_default_login_params(iscsi_login_params_t *params)
   4911 {
   4912 	params->immediate_data		= ISCSI_DEFAULT_IMMEDIATE_DATA;
   4913 	params->initial_r2t		= ISCSI_DEFAULT_INITIALR2T;
   4914 	params->first_burst_length	= ISCSI_DEFAULT_FIRST_BURST_LENGTH;
   4915 	params->max_burst_length	= ISCSI_DEFAULT_MAX_BURST_LENGTH;
   4916 	params->data_pdu_in_order	= ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
   4917 	params->data_sequence_in_order	= ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
   4918 	params->default_time_to_wait	= ISCSI_DEFAULT_TIME_TO_WAIT;
   4919 	params->default_time_to_retain	= ISCSI_DEFAULT_TIME_TO_RETAIN;
   4920 	params->header_digest		= ISCSI_DEFAULT_HEADER_DIGEST;
   4921 	params->data_digest		= ISCSI_DEFAULT_DATA_DIGEST;
   4922 	params->max_recv_data_seg_len	= ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
   4923 	params->max_xmit_data_seg_len	= ISCSI_DEFAULT_MAX_XMIT_SEG_LEN;
   4924 	params->max_connections		= ISCSI_DEFAULT_MAX_CONNECTIONS;
   4925 	params->max_outstanding_r2t	= ISCSI_DEFAULT_MAX_OUT_R2T;
   4926 	params->error_recovery_level	= ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
   4927 	params->ifmarker		= ISCSI_DEFAULT_IFMARKER;
   4928 	params->ofmarker		= ISCSI_DEFAULT_OFMARKER;
   4929 }
   4930 
   4931 /* Helper function to sets the driver default tunable parameters */
   4932 static void
   4933 iscsi_set_default_tunable_params(iscsi_tunable_params_t *params)
   4934 {
   4935 	params->recv_login_rsp_timeout = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
   4936 	params->conn_login_max = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
   4937 	params->polling_login_delay = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
   4938 }
   4939 
   4940 /*
   4941  * +--------------------------------------------------------------------+
   4942  * | End of struct utility routines				     |
   4943  * +--------------------------------------------------------------------+
   4944  */
   4945 
   4946 /*
   4947  * +--------------------------------------------------------------------+
   4948  * | Begin of ioctl utility routines				    |
   4949  * +--------------------------------------------------------------------+
   4950  */
   4951 
   4952 /*
   4953  * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM
   4954  * IOCTL
   4955  */
   4956 int
   4957 iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag,
   4958     iscsi_param_get_t *ipgp) {
   4959 	int rtn = 0;
   4960 
   4961 	/* ---- Default to settable, possibly changed later ---- */
   4962 	ipgp->g_value.v_valid    = valid_flag;
   4963 	ipgp->g_value.v_settable = B_TRUE;
   4964 
   4965 	switch (ipgp->g_param) {
   4966 	/*
   4967 	 * Boolean parameters
   4968 	 */
   4969 	case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
   4970 		ipgp->g_value.v_bool.b_current =
   4971 		    params->data_sequence_in_order;
   4972 		ipgp->g_value.v_bool.b_default =
   4973 		    ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
   4974 		break;
   4975 	case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
   4976 		ipgp->g_value.v_bool.b_current =
   4977 		    params->immediate_data;
   4978 		ipgp->g_value.v_bool.b_default =
   4979 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
   4980 		break;
   4981 	case ISCSI_LOGIN_PARAM_INITIAL_R2T:
   4982 		ipgp->g_value.v_bool.b_current =
   4983 		    params->initial_r2t;
   4984 		ipgp->g_value.v_bool.b_default =
   4985 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
   4986 		break;
   4987 	case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
   4988 		ipgp->g_value.v_bool.b_current =
   4989 		    params->data_pdu_in_order;
   4990 		ipgp->g_value.v_bool.b_default =
   4991 		    ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
   4992 		break;
   4993 
   4994 	/*
   4995 	 * Integer parameters
   4996 	 */
   4997 	case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
   4998 		ipgp->g_value.v_integer.i_current = params->header_digest;
   4999 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST;
   5000 		ipgp->g_value.v_integer.i_min = 0;
   5001 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST;
   5002 		ipgp->g_value.v_integer.i_incr = 1;
   5003 		break;
   5004 	case ISCSI_LOGIN_PARAM_DATA_DIGEST:
   5005 		ipgp->g_value.v_integer.i_current = params->data_digest;
   5006 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST;
   5007 		ipgp->g_value.v_integer.i_min = 0;
   5008 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST;
   5009 		ipgp->g_value.v_integer.i_incr = 1;
   5010 		break;
   5011 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
   5012 		ipgp->g_value.v_integer.i_current =
   5013 		    params->default_time_to_retain;
   5014 		ipgp->g_value.v_integer.i_default =
   5015 		    ISCSI_DEFAULT_TIME_TO_RETAIN;
   5016 		ipgp->g_value.v_integer.i_min = 0;
   5017 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN;
   5018 		ipgp->g_value.v_integer.i_incr = 1;
   5019 		break;
   5020 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
   5021 		ipgp->g_value.v_integer.i_current =
   5022 		    params->default_time_to_wait;
   5023 		ipgp->g_value.v_integer.i_default =
   5024 		    ISCSI_DEFAULT_TIME_TO_WAIT;
   5025 		ipgp->g_value.v_integer.i_min = 0;
   5026 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT;
   5027 		ipgp->g_value.v_integer.i_incr = 1;
   5028 		break;
   5029 	case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
   5030 		ipgp->g_value.v_integer.i_current =
   5031 		    params->error_recovery_level;
   5032 		ipgp->g_value.v_integer.i_default =
   5033 		    ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
   5034 		ipgp->g_value.v_integer.i_min = 0;
   5035 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL;
   5036 		ipgp->g_value.v_integer.i_incr = 1;
   5037 		ipgp->g_value.v_settable = B_FALSE;
   5038 		break;
   5039 	case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
   5040 		ipgp->g_value.v_integer.i_current =
   5041 		    params->first_burst_length;
   5042 		ipgp->g_value.v_integer.i_default =
   5043 		    ISCSI_DEFAULT_FIRST_BURST_LENGTH;
   5044 		ipgp->g_value.v_integer.i_min = 512;
   5045 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH;
   5046 		ipgp->g_value.v_integer.i_incr = 1;
   5047 		break;
   5048 	case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
   5049 		ipgp->g_value.v_integer.i_current =
   5050 		    params->max_burst_length;
   5051 		ipgp->g_value.v_integer.i_default =
   5052 		    ISCSI_DEFAULT_MAX_BURST_LENGTH;
   5053 		ipgp->g_value.v_integer.i_min = 512;
   5054 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH;
   5055 		ipgp->g_value.v_integer.i_incr = 1;
   5056 		break;
   5057 	case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
   5058 		ipgp->g_value.v_integer.i_current =
   5059 		    params->max_connections;
   5060 		ipgp->g_value.v_settable = B_FALSE;
   5061 		ipgp->g_value.v_integer.i_default =
   5062 		    ISCSI_DEFAULT_MAX_CONNECTIONS;
   5063 		ipgp->g_value.v_integer.i_min = 1;
   5064 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS;
   5065 		ipgp->g_value.v_integer.i_incr = 1;
   5066 		break;
   5067 	case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
   5068 		ipgp->g_value.v_integer.i_current =
   5069 		    params->max_outstanding_r2t;
   5070 		ipgp->g_value.v_settable = B_FALSE;
   5071 		ipgp->g_value.v_integer.i_default =
   5072 		    ISCSI_DEFAULT_MAX_OUT_R2T;
   5073 		ipgp->g_value.v_integer.i_min = 1;
   5074 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T;
   5075 		ipgp->g_value.v_integer.i_incr = 1;
   5076 		break;
   5077 	case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
   5078 		ipgp->g_value.v_integer.i_current =
   5079 		    params->max_recv_data_seg_len;
   5080 		ipgp->g_value.v_integer.i_default =
   5081 		    ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
   5082 		ipgp->g_value.v_integer.i_min = 512;
   5083 		ipgp->g_value.v_integer.i_max =
   5084 		    ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
   5085 		ipgp->g_value.v_integer.i_incr = 1;
   5086 		break;
   5087 	default:
   5088 		rtn = EINVAL;
   5089 	}
   5090 
   5091 	return (rtn);
   5092 }
   5093 
   5094 /*
   5095  * +--------------------------------------------------------------------+
   5096  * | End of ioctl utility routines                                      |
   5097  * +--------------------------------------------------------------------+
   5098  */
   5099 
   5100 /*
   5101  * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a
   5102  * IEEE safe address.  IEEE addresses have a number of characters
   5103  * set aside as reserved.
   5104  */
   5105 static void
   5106 iscsi_get_name_from_iqn(char *name, int name_max_len)
   5107 {
   5108 	char	*tmp		= NULL;
   5109 	char	*oldch		= NULL;
   5110 	char	*newch		= NULL;
   5111 
   5112 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
   5113 
   5114 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
   5115 	    oldch++, newch++) {
   5116 		switch (*oldch) {
   5117 		case ':':
   5118 			*newch++ = '%';
   5119 			*newch++ = '3';
   5120 			*newch = 'A';
   5121 			break;
   5122 		case ' ':
   5123 			*newch++ = '%';
   5124 			*newch++ = '2';
   5125 			*newch = '0';
   5126 			break;
   5127 		case '@':
   5128 			*newch++ = '%';
   5129 			*newch++ = '4';
   5130 			*newch = '0';
   5131 			break;
   5132 		case '/':
   5133 			*newch++ = '%';
   5134 			*newch++ = '2';
   5135 			*newch = 'F';
   5136 			break;
   5137 		default:
   5138 			*newch = *oldch;
   5139 		}
   5140 	}
   5141 	(void) strncpy(name, tmp, name_max_len);
   5142 	kmem_free(tmp, MAX_GET_NAME_SIZE);
   5143 }
   5144 
   5145 /*
   5146  * iscsi_get_name_to_iqn - Converts IEEE safe address back
   5147  * into a iscsi iqn/eui.
   5148  */
   5149 static void
   5150 iscsi_get_name_to_iqn(char *name, int name_max_len)
   5151 {
   5152 	char	*tmp		= NULL;
   5153 	char	*oldch		= NULL;
   5154 	char	*newch		= NULL;
   5155 
   5156 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
   5157 
   5158 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
   5159 	    oldch++, newch++) {
   5160 		if (*oldch == '%') {
   5161 			switch (*(oldch+1)) {
   5162 			case '2':
   5163 				if (*(oldch+2) == '0') {
   5164 					*newch = ' ';
   5165 					oldch += 2;
   5166 				} else if (*(oldch+2) == 'F') {
   5167 					*newch = '/';
   5168 					oldch += 2;
   5169 				} else {
   5170 					*newch = *oldch;
   5171 				}
   5172 				break;
   5173 			case '3':
   5174 				if (*(oldch+2) == 'A') {
   5175 					*newch = ':';
   5176 					oldch += 2;
   5177 				} else {
   5178 					*newch = *oldch;
   5179 				}
   5180 				break;
   5181 			case '4':
   5182 				if (*(oldch+2) == '0') {
   5183 					*newch = '@';
   5184 					oldch += 2;
   5185 				} else {
   5186 					*newch = *oldch;
   5187 				}
   5188 				break;
   5189 			default:
   5190 				*newch = *oldch;
   5191 			}
   5192 		} else {
   5193 			*newch = *oldch;
   5194 		}
   5195 	}
   5196 	(void) strncpy(name, tmp, name_max_len);
   5197 	kmem_free(tmp, MAX_GET_NAME_SIZE);
   5198 }
   5199 
   5200 /*
   5201  * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl
   5202  *
   5203  * On return 0 means persisted parameter found
   5204  */
   5205 int
   5206 iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp,
   5207     iscsi_login_params_t *params)
   5208 {
   5209 	int rtn = 1;
   5210 	persistent_param_t *pparam;
   5211 
   5212 	if (name == NULL || strlen((char *)name) == 0) {
   5213 		return (rtn);
   5214 	}
   5215 
   5216 	pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP);
   5217 
   5218 	if (persistent_param_get((char *)name, pparam) == B_TRUE) {
   5219 		if (pparam->p_bitmap & (1 << ipgp->g_param)) {
   5220 			/* Found configured parameter. */
   5221 			bcopy(&pparam->p_params, params, sizeof (*params));
   5222 			rtn = 0;
   5223 		}
   5224 	}
   5225 
   5226 	kmem_free(pparam, sizeof (*pparam));
   5227 
   5228 	return (rtn);
   5229 }
   5230 
   5231 /*
   5232  * iscsi_override_target_default - helper function set the target's default
   5233  * login parameter if there is a configured initiator parameter.
   5234  *
   5235  */
   5236 static void
   5237 iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg)
   5238 {
   5239 	persistent_param_t *pp;
   5240 	iscsi_login_params_t *params;
   5241 
   5242 	pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP);
   5243 	if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) {
   5244 		if (pp->p_bitmap & (1 << ipg->g_param)) {
   5245 			params = &pp->p_params;
   5246 			switch (ipg->g_param) {
   5247 			case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
   5248 				ipg->g_value.v_bool.b_default =
   5249 				    params->data_sequence_in_order;
   5250 				break;
   5251 			case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
   5252 				ipg->g_value.v_bool.b_default =
   5253 				    params->immediate_data;
   5254 				break;
   5255 			case ISCSI_LOGIN_PARAM_INITIAL_R2T:
   5256 				ipg->g_value.v_bool.b_default =
   5257 				    params->initial_r2t;
   5258 				break;
   5259 			case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
   5260 				ipg->g_value.v_bool.b_default =
   5261 				    params->data_pdu_in_order;
   5262 				break;
   5263 			case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
   5264 				ipg->g_value.v_integer.i_default =
   5265 				    params->header_digest;
   5266 				break;
   5267 			case ISCSI_LOGIN_PARAM_DATA_DIGEST:
   5268 				ipg->g_value.v_integer.i_default =
   5269 				    params->data_digest;
   5270 				break;
   5271 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
   5272 				ipg->g_value.v_integer.i_default =
   5273 				    params->default_time_to_retain;
   5274 				break;
   5275 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
   5276 				ipg->g_value.v_integer.i_default =
   5277 				    params->default_time_to_wait;
   5278 				break;
   5279 			case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
   5280 				ipg->g_value.v_integer.i_default =
   5281 				    params->error_recovery_level;
   5282 				break;
   5283 			case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
   5284 				ipg->g_value.v_integer.i_default =
   5285 				    params->first_burst_length;
   5286 				break;
   5287 			case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
   5288 				ipg->g_value.v_integer.i_default =
   5289 				    params->max_burst_length;
   5290 				break;
   5291 			case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
   5292 				ipg->g_value.v_integer.i_default =
   5293 				    params->max_connections;
   5294 				break;
   5295 			case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
   5296 				ipg->g_value.v_integer.i_default =
   5297 				    params->max_outstanding_r2t;
   5298 				break;
   5299 			case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
   5300 				ipg->g_value.v_integer.i_default =
   5301 				    params->max_xmit_data_seg_len;
   5302 				break;
   5303 			default:
   5304 				break;
   5305 			}
   5306 		}
   5307 	}
   5308 	kmem_free(pp, sizeof (*pp));
   5309 }
   5310 
   5311 static boolean_t
   5312 iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid)
   5313 {
   5314 	iscsi_sess_t *isp = NULL;
   5315 
   5316 	if (iscsi_chk_bootlun_mpxio(ihp)) {
   5317 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
   5318 			if ((isp->sess_oid == oid) && isp->sess_boot) {
   5319 				/* oid is session object */
   5320 				break;
   5321 			}
   5322 			if ((isp->sess_target_oid == oid) && isp->sess_boot) {
   5323 				/*
   5324 				 * oid is target object while
   5325 				 * this session is boot session
   5326 				 */
   5327 				break;
   5328 			}
   5329 		}
   5330 		if (oid == ihp->hba_oid) {
   5331 			/* oid is initiator object id */
   5332 			return (B_TRUE);
   5333 		} else if ((isp != NULL) && (isp->sess_boot)) {
   5334 			/* oid is boot session object id */
   5335 			return (B_TRUE);
   5336 		}
   5337 	}
   5338 	return (B_FALSE);
   5339 }
   5340 
   5341 /*
   5342  * iscsi_client_request_service - request the iSCSI service
   5343  *     returns true if the service is enabled and increases the count
   5344  *     returns false if the service is disabled
   5345  *     blocks until the service status is either enabled or disabled
   5346  */
   5347 boolean_t
   5348 iscsi_client_request_service(iscsi_hba_t *ihp) {
   5349 	boolean_t	rval = B_TRUE;
   5350 
   5351 	mutex_enter(&ihp->hba_service_lock);
   5352 	while ((ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) ||
   5353 	    (ihp->hba_service_client_count == UINT_MAX)) {
   5354 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
   5355 	}
   5356 	if (ihp->hba_service_status == ISCSI_SERVICE_ENABLED) {
   5357 		ihp->hba_service_client_count++;
   5358 	} else {
   5359 		rval = B_FALSE;
   5360 	}
   5361 	mutex_exit(&ihp->hba_service_lock);
   5362 
   5363 	return (rval);
   5364 }
   5365 
   5366 /*
   5367  * iscsi_client_release_service - decrease the count and wake up
   5368  *     blocking threads if the count reaches zero
   5369  */
   5370 void
   5371 iscsi_client_release_service(iscsi_hba_t *ihp) {
   5372 	mutex_enter(&ihp->hba_service_lock);
   5373 	ASSERT(ihp->hba_service_client_count > 0);
   5374 	ihp->hba_service_client_count--;
   5375 	if (ihp->hba_service_client_count == 0) {
   5376 		cv_broadcast(&ihp->hba_service_cv);
   5377 	}
   5378 	mutex_exit(&ihp->hba_service_lock);
   5379 }
   5380 
   5381 /*
   5382  * iscsi_enter_service_zone - enter the service zone, should be called
   5383  * before doing any modifications to the service status
   5384  * return TRUE if the zone is entered
   5385  *	  FALSE if no need to enter the zone
   5386  */
   5387 static boolean_t
   5388 iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status) {
   5389 	if ((status != ISCSI_SERVICE_ENABLED) &&
   5390 	    (status != ISCSI_SERVICE_DISABLED)) {
   5391 		return (B_FALSE);
   5392 	}
   5393 
   5394 	mutex_enter(&ihp->hba_service_lock);
   5395 	while (ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) {
   5396 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
   5397 	}
   5398 	if (ihp->hba_service_status == status) {
   5399 		mutex_exit(&ihp->hba_service_lock);
   5400 		return (B_FALSE);
   5401 	}
   5402 	ihp->hba_service_status = ISCSI_SERVICE_TRANSITION;
   5403 	while (ihp->hba_service_client_count > 0) {
   5404 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
   5405 	}
   5406 	mutex_exit(&ihp->hba_service_lock);
   5407 	return (B_TRUE);
   5408 }
   5409 
   5410 /*
   5411  * iscsi_exit_service_zone - exits the service zone and wakes up waiters
   5412  */
   5413 static void
   5414 iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status) {
   5415 	if ((status != ISCSI_SERVICE_ENABLED) &&
   5416 	    (status != ISCSI_SERVICE_DISABLED)) {
   5417 		return;
   5418 	}
   5419 
   5420 	mutex_enter(&ihp->hba_service_lock);
   5421 	ASSERT(ihp->hba_service_status == ISCSI_SERVICE_TRANSITION);
   5422 	ihp->hba_service_status = status;
   5423 	cv_broadcast(&ihp->hba_service_cv);
   5424 	mutex_exit(&ihp->hba_service_lock);
   5425 }
   5426 
   5427 static void
   5428 iscsi_check_miniroot(iscsi_hba_t *ihp) {
   5429 	if (strncmp(rootfs.bo_name, "/ramdisk", 8) == 0) {
   5430 		/*
   5431 		 * in miniroot we don't have the persistent store
   5432 		 * so just to need to ensure an enabled status
   5433 		 */
   5434 		ihp->hba_service_status = ISCSI_SERVICE_ENABLED;
   5435 	}
   5436 }
   5437 
   5438 static void
   5439 iscsi_get_tunable_default(iscsi_tunable_object_t *param) {
   5440 	int	param_id = 0;
   5441 
   5442 	param_id = 1 << (param->t_param - 1);
   5443 	param->t_set = B_FALSE;
   5444 	switch (param_id) {
   5445 	case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
   5446 		param->t_value.v_integer = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
   5447 		break;
   5448 	case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
   5449 		param->t_value.v_integer = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
   5450 		break;
   5451 	case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
   5452 		param->t_value.v_integer = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
   5453 		break;
   5454 	default:
   5455 		break;
   5456 	}
   5457 }
   5458 
   5459 /*
   5460  * iscsi_get_persisted_tunable_param * - a helper to ISCSI_TUNABLE_PARAM_GET
   5461  * ioctl
   5462  * return:
   5463  *    0 	persisted tunable parameter found
   5464  *    1		persisted tunable parameter not found
   5465  */
   5466 static int
   5467 iscsi_get_persisted_tunable_param(uchar_t *name, iscsi_tunable_object_t *tpsg)
   5468 {
   5469 	int rtn = 1;
   5470 	int param_id = 0;
   5471 	persistent_tunable_param_t *pparam;
   5472 
   5473 	if ((name == NULL) || strlen((char *)name) == 0) {
   5474 		return (rtn);
   5475 	}
   5476 
   5477 	tpsg->t_set = B_FALSE;
   5478 	pparam = (persistent_tunable_param_t *)kmem_zalloc(sizeof (*pparam),
   5479 	    KM_SLEEP);
   5480 	if (persistent_get_tunable_param((char *)name, pparam) == B_TRUE) {
   5481 		if (pparam->p_bitmap & (1 << (tpsg->t_param - 1))) {
   5482 			tpsg->t_set = B_TRUE;
   5483 			param_id = 1 << (tpsg->t_param - 1);
   5484 			switch (param_id) {
   5485 			case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
   5486 				tpsg->t_value.v_integer =
   5487 				    pparam->p_params.recv_login_rsp_timeout;
   5488 				break;
   5489 			case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
   5490 				tpsg->t_value.v_integer =
   5491 				    pparam->p_params.polling_login_delay;
   5492 				break;
   5493 			case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
   5494 				tpsg->t_value.v_integer =
   5495 				    pparam->p_params.conn_login_max;
   5496 				break;
   5497 			default:
   5498 				break;
   5499 			}
   5500 			rtn = 0;
   5501 		}
   5502 	}
   5503 
   5504 	kmem_free(pparam, sizeof (*pparam));
   5505 
   5506 	return (rtn);
   5507 }
   5508