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