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