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.
23904e51f6SJack Meng  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24*61dfa509SRick McNeal  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
257284664aSJoshua M. Clulow  * Copyright 2019 Joshua M. Clulow <josh@sysmgr.org>
26fcf3ce44SJohn Forte  *
27fcf3ce44SJohn Forte  * iSCSI Software Initiator
28fcf3ce44SJohn Forte  */
29fcf3ce44SJohn Forte 
30fcf3ce44SJohn Forte /*
31fcf3ce44SJohn Forte  * Framework interface routines for iSCSI
32fcf3ce44SJohn Forte  */
33fcf3ce44SJohn Forte 
3430e7468fSPeter Dunlap #include "iscsi.h"				/* main header */
3530e7468fSPeter Dunlap #include <sys/iscsi_protocol.h>	/* protocol structs */
3630e7468fSPeter Dunlap #include <sys/scsi/adapters/iscsi_if.h>		/* ioctl interfaces */
37fcf3ce44SJohn Forte #include "iscsi_targetparam.h"
38fcf3ce44SJohn Forte #include "persistent.h"
39fcf3ce44SJohn Forte #include <sys/scsi/adapters/iscsi_door.h>
40fcf3ce44SJohn Forte #include <sys/dlpi.h>
41fcf3ce44SJohn Forte #include <sys/utsname.h>
42fcf3ce44SJohn Forte #include "isns_client.h"
43fcf3ce44SJohn Forte #include "isns_protocol.h"
446cefaae1SJack Meng #include <sys/bootprops.h>
454246c8e9SJack Meng #include <sys/types.h>
464246c8e9SJack Meng #include <sys/bootconf.h>
47fcf3ce44SJohn Forte 
48fcf3ce44SJohn Forte #define	ISCSI_NAME_VERSION	"iSCSI Initiator v-1.55"
49fcf3ce44SJohn Forte 
50fcf3ce44SJohn Forte #define	MAX_GET_NAME_SIZE	1024
51fcf3ce44SJohn Forte #define	MAX_NAME_PROP_SIZE	256
52fcf3ce44SJohn Forte #define	UNDEFINED		-1
53aff4bce5Syi zhang - Sun Microsystems - Beijing China #define	ISCSI_DISC_DELAY	2	/* seconds */
54fcf3ce44SJohn Forte 
55fcf3ce44SJohn Forte /*
56fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
57fcf3ce44SJohn Forte  * | iscsi globals                                                      |
58fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
59fcf3ce44SJohn Forte  */
60fcf3ce44SJohn Forte void		*iscsi_state;
61fcf3ce44SJohn Forte kmutex_t	iscsi_oid_mutex;
62fcf3ce44SJohn Forte uint32_t	iscsi_oid;
63fcf3ce44SJohn Forte int		iscsi_nop_delay		= ISCSI_DEFAULT_NOP_DELAY;
64fcf3ce44SJohn Forte int		iscsi_rx_window		= ISCSI_DEFAULT_RX_WINDOW;
65fcf3ce44SJohn Forte int		iscsi_rx_max_window	= ISCSI_DEFAULT_RX_MAX_WINDOW;
6630e7468fSPeter Dunlap boolean_t	iscsi_logging		= B_FALSE;
67fcf3ce44SJohn Forte 
686cefaae1SJack Meng extern ib_boot_prop_t	*iscsiboot_prop;
694246c8e9SJack Meng extern int		modrootloaded;
704246c8e9SJack Meng extern struct bootobj	rootfs;
716cefaae1SJack Meng 
72fcf3ce44SJohn Forte /*
73fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
74fcf3ce44SJohn Forte  * | iscsi.c prototypes							|
75fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
76fcf3ce44SJohn Forte  */
77fcf3ce44SJohn Forte static int iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
78fcf3ce44SJohn Forte     void *arg, void **result);
79fcf3ce44SJohn Forte static int iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
80fcf3ce44SJohn Forte static int iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
81fcf3ce44SJohn Forte 
82fcf3ce44SJohn Forte /* scsi_tran prototypes */
83fcf3ce44SJohn Forte static int iscsi_tran_lun_init(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_lun_probe(struct scsi_device *sd, int (*callback) ());
86fcf3ce44SJohn Forte static struct scsi_pkt *iscsi_tran_init_pkt(struct scsi_address *ap,
87fcf3ce44SJohn Forte     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
88fcf3ce44SJohn Forte     int tgtlen, int flags, int (*callback) (), caddr_t arg);
89fcf3ce44SJohn Forte static void iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip,
90fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
91fcf3ce44SJohn Forte static int iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt);
92fcf3ce44SJohn Forte static int iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
93fcf3ce44SJohn Forte static int iscsi_tran_reset(struct scsi_address *ap, int level);
94fcf3ce44SJohn Forte static int iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom);
95fcf3ce44SJohn Forte static int iscsi_tran_setcap(struct scsi_address *ap, char *cap,
96fcf3ce44SJohn Forte     int value, int whom);
97fcf3ce44SJohn Forte static void iscsi_tran_destroy_pkt(struct scsi_address *ap,
98fcf3ce44SJohn Forte     struct scsi_pkt *pkt);
99fcf3ce44SJohn Forte static void iscsi_tran_dmafree(struct scsi_address *ap,
100fcf3ce44SJohn Forte     struct scsi_pkt *pkt);
101fcf3ce44SJohn Forte static void iscsi_tran_sync_pkt(struct scsi_address *ap,
102fcf3ce44SJohn Forte     struct scsi_pkt *pkt);
103fcf3ce44SJohn Forte static void iscsi_tran_sync_pkt(struct scsi_address *ap,
104fcf3ce44SJohn Forte     struct scsi_pkt *pkt);
105fcf3ce44SJohn Forte static int iscsi_tran_reset_notify(struct scsi_address *ap, int flag,
106fcf3ce44SJohn Forte     void (*callback) (caddr_t), caddr_t arg);
107fcf3ce44SJohn Forte static int iscsi_tran_bus_config(dev_info_t *parent, uint_t flags,
108fcf3ce44SJohn Forte     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
109fcf3ce44SJohn Forte static int iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flags,
110fcf3ce44SJohn Forte     ddi_bus_config_op_t op, void *arg);
111fcf3ce44SJohn Forte static int iscsi_tran_get_name(struct scsi_device *sd, char *name, int len);
112fcf3ce44SJohn Forte static int iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len);
113fcf3ce44SJohn Forte 
114fcf3ce44SJohn Forte /* bus_ops prototypes */
115fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
116fcf3ce44SJohn Forte static ddi_intrspec_t iscsi_get_intrspec(dev_info_t *dip, dev_info_t *rdip,
117fcf3ce44SJohn Forte     uint_t inumber);
118fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
119fcf3ce44SJohn Forte static int iscsi_add_intrspec(dev_info_t *dip, dev_info_t *rdip,
120fcf3ce44SJohn Forte     ddi_intrspec_t intrspec, ddi_iblock_cookie_t *iblock_cookiep,
121fcf3ce44SJohn Forte     ddi_idevice_cookie_t *idevice_cookiep, uint_t (*int_handler)(caddr_t
122fcf3ce44SJohn Forte     int_handler_arg), caddr_t int_handler_arg, int kind);
123fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
124fcf3ce44SJohn Forte static void iscsi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
125fcf3ce44SJohn Forte     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie);
126fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
127fcf3ce44SJohn Forte static int iscsi_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
128fcf3ce44SJohn Forte     void *arg, void *result);
129fcf3ce44SJohn Forte 
130fcf3ce44SJohn Forte /* cb_ops prototypes */
131fcf3ce44SJohn Forte static int iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp);
132fcf3ce44SJohn Forte static int iscsi_close(dev_t dev, int flag, int otyp, cred_t *credp);
1334246c8e9SJack Meng static int iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
134fcf3ce44SJohn Forte     cred_t *credp, int *rvalp);
135fcf3ce44SJohn Forte 
136fcf3ce44SJohn Forte int iscsi_get_persisted_param(uchar_t *name,
137fcf3ce44SJohn Forte     iscsi_param_get_t *ipgp,
138fcf3ce44SJohn Forte     iscsi_login_params_t *params);
139fcf3ce44SJohn Forte static void iscsi_override_target_default(iscsi_hba_t *ihp,
140fcf3ce44SJohn Forte     iscsi_param_get_t *ipg);
141fcf3ce44SJohn Forte 
142fcf3ce44SJohn Forte /* scsi_tran helpers */
143fcf3ce44SJohn Forte static int iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
144fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
145fcf3ce44SJohn Forte static int iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
146fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
147fcf3ce44SJohn Forte static int iscsi_i_commoncap(struct scsi_address *ap, char *cap,
148fcf3ce44SJohn Forte     int val, int lunonly, int doset);
149fcf3ce44SJohn Forte static void iscsi_get_name_to_iqn(char *name, int name_max_len);
150fcf3ce44SJohn Forte static void iscsi_get_name_from_iqn(char *name, int name_max_len);
1516cefaae1SJack Meng static boolean_t iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid);
152fcf3ce44SJohn Forte 
1534246c8e9SJack Meng /* iscsi initiator service helpers */
1544246c8e9SJack Meng static boolean_t iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status);
1554246c8e9SJack Meng static void iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status);
1564246c8e9SJack Meng static void iscsi_check_miniroot(iscsi_hba_t *ihp);
157aff4bce5Syi zhang - Sun Microsystems - Beijing China static void iscsi_get_tunable_default(iscsi_tunable_object_t *param);
158aff4bce5Syi zhang - Sun Microsystems - Beijing China static int iscsi_get_persisted_tunable_param(uchar_t *name,
159aff4bce5Syi zhang - Sun Microsystems - Beijing China     iscsi_tunable_object_t *tpsg);
160aff4bce5Syi zhang - Sun Microsystems - Beijing China static void iscsi_set_default_tunable_params(iscsi_tunable_params_t *params);
1614246c8e9SJack Meng 
162fcf3ce44SJohn Forte /* struct helpers prototypes */
163fcf3ce44SJohn Forte 
164fcf3ce44SJohn Forte /*
165fcf3ce44SJohn Forte  * At this point this driver doesn't need this structure because nothing
166fcf3ce44SJohn Forte  * is done during the open, close or ioctl. Code put in place because
167fcf3ce44SJohn Forte  * some admin related work might be done in the ioctl routine.
168fcf3ce44SJohn Forte  */
169fcf3ce44SJohn Forte static struct cb_ops iscsi_cb_ops = {
170fcf3ce44SJohn Forte 	iscsi_open,			/* open */
171fcf3ce44SJohn Forte 	iscsi_close,			/* close */
172fcf3ce44SJohn Forte 	nodev,				/* strategy */
173fcf3ce44SJohn Forte 	nodev,				/* print */
174fcf3ce44SJohn Forte 	nodev,				/* dump */
175fcf3ce44SJohn Forte 	nodev,				/* read */
176fcf3ce44SJohn Forte 	nodev,				/* write */
177fcf3ce44SJohn Forte 	iscsi_ioctl,			/* ioctl */
178fcf3ce44SJohn Forte 	nodev,				/* devmap */
179fcf3ce44SJohn Forte 	nodev,				/* mmap */
180fcf3ce44SJohn Forte 	nodev,				/* segmap */
181fcf3ce44SJohn Forte 	nochpoll,			/* poll */
182fcf3ce44SJohn Forte 	ddi_prop_op,			/* prop_op */
183fcf3ce44SJohn Forte 	NULL,				/* streamtab */
184fcf3ce44SJohn Forte 	D_NEW | D_MP | D_HOTPLUG,	/* flags */
185fcf3ce44SJohn Forte 	CB_REV,				/* cb_rev */
186fcf3ce44SJohn Forte 	nodev,				/* aread */
187fcf3ce44SJohn Forte 	nodev,				/* awrite */
188fcf3ce44SJohn Forte };
189fcf3ce44SJohn Forte 
190fcf3ce44SJohn Forte static struct dev_ops iscsi_dev_ops = {
191fcf3ce44SJohn Forte 	DEVO_REV,		/* devo_rev */
192fcf3ce44SJohn Forte 	0,			/* refcnt */
193fcf3ce44SJohn Forte 	iscsi_getinfo,		/* getinfo */
194fcf3ce44SJohn Forte 	nulldev,		/* identify */
195fcf3ce44SJohn Forte 	nulldev,		/* probe */
196fcf3ce44SJohn Forte 	iscsi_attach,		/* attach */
197fcf3ce44SJohn Forte 	iscsi_detach,		/* detach */
198fcf3ce44SJohn Forte 	nodev,			/* reset */
199fcf3ce44SJohn Forte 	&iscsi_cb_ops,		/* driver operations */
200fcf3ce44SJohn Forte 	NULL,			/* bus ops */
201fcf3ce44SJohn Forte 	NULL,			/* power management */
202edd20d23Syi zhang - Sun Microsystems - Beijing China 	ddi_quiesce_not_needed,	/* quiesce */
203fcf3ce44SJohn Forte };
204fcf3ce44SJohn Forte 
205fcf3ce44SJohn Forte static struct modldrv modldrv = {
206fcf3ce44SJohn Forte 	&mod_driverops,		/* drv_modops */
207fcf3ce44SJohn Forte 	ISCSI_NAME_VERSION,	/* drv_linkinfo */
208fcf3ce44SJohn Forte 	&iscsi_dev_ops		/* drv_dev_ops */
209fcf3ce44SJohn Forte };
210fcf3ce44SJohn Forte 
211fcf3ce44SJohn Forte static struct modlinkage modlinkage = {
212fcf3ce44SJohn Forte 	MODREV_1,		/* ml_rev */
213fcf3ce44SJohn Forte 	&modldrv,		/* ml_linkage[] */
214fcf3ce44SJohn Forte 	NULL			/* NULL termination */
215fcf3ce44SJohn Forte };
216fcf3ce44SJohn Forte 
217fcf3ce44SJohn Forte /*
218fcf3ce44SJohn Forte  * This structure is bogus. scsi_hba_attach_setup() requires, as in the kernel
219fcf3ce44SJohn Forte  * will panic if you don't pass this in to the routine, this information.
220fcf3ce44SJohn Forte  * Need to determine what the actual impact to the system is by providing
221fcf3ce44SJohn Forte  * this information if any. Since dma allocation is done in pkt_init it may
222fcf3ce44SJohn Forte  * not have any impact. These values are straight from the Writing Device
223fcf3ce44SJohn Forte  * Driver manual.
224fcf3ce44SJohn Forte  */
225fcf3ce44SJohn Forte static ddi_dma_attr_t iscsi_dma_attr = {
226fcf3ce44SJohn Forte 	DMA_ATTR_V0,	/* ddi_dma_attr version */
227fcf3ce44SJohn Forte 	0,		/* low address */
228fcf3ce44SJohn Forte 	0xffffffff,	/* high address */
229fcf3ce44SJohn Forte 	0x00ffffff,	/* counter upper bound */
230fcf3ce44SJohn Forte 	1,		/* alignment requirements */
231fcf3ce44SJohn Forte 	0x3f,		/* burst sizes */
232fcf3ce44SJohn Forte 	1,		/* minimum DMA access */
233fcf3ce44SJohn Forte 	0xffffffff,	/* maximum DMA access */
234fcf3ce44SJohn Forte 	(1 << 24) - 1,	/* segment boundary restrictions */
235fcf3ce44SJohn Forte 	1,		/* scater/gather list length */
236fcf3ce44SJohn Forte 	512,		/* device granularity */
237fcf3ce44SJohn Forte 	0		/* DMA flags */
238fcf3ce44SJohn Forte };
239fcf3ce44SJohn Forte 
240fcf3ce44SJohn Forte /*
241fcf3ce44SJohn Forte  * _init - General driver init entry
242fcf3ce44SJohn Forte  */
243fcf3ce44SJohn Forte int
_init(void)244fcf3ce44SJohn Forte _init(void)
245fcf3ce44SJohn Forte {
246fcf3ce44SJohn Forte 	int rval = 0;
247fcf3ce44SJohn Forte 
248fcf3ce44SJohn Forte 	iscsi_net_init();
249fcf3ce44SJohn Forte 
250fcf3ce44SJohn Forte 	mutex_init(&iscsi_oid_mutex, NULL, MUTEX_DRIVER, NULL);
251fcf3ce44SJohn Forte 	iscsi_oid = ISCSI_INITIATOR_OID;
252fcf3ce44SJohn Forte 
253fcf3ce44SJohn Forte 	/*
254fcf3ce44SJohn Forte 	 * Set up the soft state structures. If this driver is actually
255fcf3ce44SJohn Forte 	 * being attached to the system then we'll have at least one
256fcf3ce44SJohn Forte 	 * HBA/NIC used.
257fcf3ce44SJohn Forte 	 */
258fcf3ce44SJohn Forte 	rval = ddi_soft_state_init(&iscsi_state,
259fcf3ce44SJohn Forte 	    sizeof (iscsi_hba_t), 1);
260fcf3ce44SJohn Forte 	if (rval != 0) {
261fcf3ce44SJohn Forte 		iscsi_net_fini();
262fcf3ce44SJohn Forte 		goto init_done;
263fcf3ce44SJohn Forte 	}
264fcf3ce44SJohn Forte 
265fcf3ce44SJohn Forte 	rval = scsi_hba_init(&modlinkage);
266fcf3ce44SJohn Forte 	if (rval != 0) {
267fcf3ce44SJohn Forte 		ddi_soft_state_fini(&iscsi_state);
268fcf3ce44SJohn Forte 		iscsi_net_fini();
269fcf3ce44SJohn Forte 		goto init_done;
270fcf3ce44SJohn Forte 	}
271fcf3ce44SJohn Forte 
272fcf3ce44SJohn Forte 	rval = mod_install(&modlinkage);
273fcf3ce44SJohn Forte 	if (rval != 0) {
274fcf3ce44SJohn Forte 		ddi_soft_state_fini(&iscsi_state);
275fcf3ce44SJohn Forte 		scsi_hba_fini(&modlinkage);
276fcf3ce44SJohn Forte 		iscsi_net_fini();
277fcf3ce44SJohn Forte 		goto init_done;
278fcf3ce44SJohn Forte 	}
279fcf3ce44SJohn Forte 	(void) iscsi_door_ini();
280fcf3ce44SJohn Forte 
281fcf3ce44SJohn Forte init_done:
282fcf3ce44SJohn Forte 	return (rval);
283fcf3ce44SJohn Forte }
284fcf3ce44SJohn Forte 
285fcf3ce44SJohn Forte /*
286fcf3ce44SJohn Forte  * _fini - General driver destructor entry
287fcf3ce44SJohn Forte  */
288fcf3ce44SJohn Forte int
_fini(void)289fcf3ce44SJohn Forte _fini(void)
290fcf3ce44SJohn Forte {
291fcf3ce44SJohn Forte 	int rval = 0;
292fcf3ce44SJohn Forte 
293fcf3ce44SJohn Forte 	rval = mod_remove(&modlinkage);
294fcf3ce44SJohn Forte 	if (rval == 0) {
295fcf3ce44SJohn Forte 		scsi_hba_fini(&modlinkage);
296fcf3ce44SJohn Forte 		ddi_soft_state_fini(&iscsi_state);
297fcf3ce44SJohn Forte 		mutex_destroy(&iscsi_oid_mutex);
298fcf3ce44SJohn Forte 		(void) iscsi_door_term();
299fcf3ce44SJohn Forte 		iscsi_net_fini();
300fcf3ce44SJohn Forte 	}
301fcf3ce44SJohn Forte 	return (rval);
302fcf3ce44SJohn Forte }
303fcf3ce44SJohn Forte 
304fcf3ce44SJohn Forte /*
305fcf3ce44SJohn Forte  * _info - General driver info entry
306fcf3ce44SJohn Forte  */
307fcf3ce44SJohn Forte int
_info(struct modinfo * mp)308fcf3ce44SJohn Forte _info(struct modinfo *mp)
309fcf3ce44SJohn Forte {
310fcf3ce44SJohn Forte 	int rval = 0;
311fcf3ce44SJohn Forte 
312fcf3ce44SJohn Forte 	rval = mod_info(&modlinkage, mp);
313fcf3ce44SJohn Forte 
314fcf3ce44SJohn Forte 	return (rval);
315fcf3ce44SJohn Forte }
316fcf3ce44SJohn Forte 
317fcf3ce44SJohn Forte 
318fcf3ce44SJohn Forte /*
319fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
320fcf3ce44SJohn Forte  * | Start of dev_ops routines					  |
321fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
322fcf3ce44SJohn Forte  */
323fcf3ce44SJohn Forte 
324fcf3ce44SJohn Forte /*
325fcf3ce44SJohn Forte  * iscsi_getinfo - returns general driver information
326fcf3ce44SJohn Forte  */
327fcf3ce44SJohn Forte /* ARGSUSED */
328fcf3ce44SJohn Forte static int
iscsi_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)329fcf3ce44SJohn Forte iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
330fcf3ce44SJohn Forte     void *arg, void **result)
331fcf3ce44SJohn Forte {
332fcf3ce44SJohn Forte 	int		rval		= DDI_SUCCESS;
333fcf3ce44SJohn Forte 	int		instance	= getminor((dev_t)arg);
334fcf3ce44SJohn Forte 	iscsi_hba_t	*ip;
335fcf3ce44SJohn Forte 
336fcf3ce44SJohn Forte 	switch (infocmd) {
337fcf3ce44SJohn Forte 	case DDI_INFO_DEVT2DEVINFO:
338fcf3ce44SJohn Forte 		if ((ip = ddi_get_soft_state(iscsi_state, instance)) == NULL) {
339fcf3ce44SJohn Forte 			return (DDI_FAILURE);
340fcf3ce44SJohn Forte 		}
341fcf3ce44SJohn Forte 		*result = ip->hba_dip;
342fcf3ce44SJohn Forte 		if (ip->hba_dip == NULL)
343fcf3ce44SJohn Forte 			rval = DDI_FAILURE;
344fcf3ce44SJohn Forte 		else
345fcf3ce44SJohn Forte 			rval = DDI_SUCCESS;
346fcf3ce44SJohn Forte 		break;
347fcf3ce44SJohn Forte 
348fcf3ce44SJohn Forte 	case DDI_INFO_DEVT2INSTANCE:
349fcf3ce44SJohn Forte 		*result = (void *)(uintptr_t)instance;
350fcf3ce44SJohn Forte 		rval = DDI_SUCCESS;
351fcf3ce44SJohn Forte 		break;
352fcf3ce44SJohn Forte 
353fcf3ce44SJohn Forte 	default:
354fcf3ce44SJohn Forte 		rval = DDI_FAILURE;
355fcf3ce44SJohn Forte 		break;
356fcf3ce44SJohn Forte 	}
357fcf3ce44SJohn Forte 	return (rval);
358fcf3ce44SJohn Forte }
359fcf3ce44SJohn Forte 
360fcf3ce44SJohn Forte 
361fcf3ce44SJohn Forte /*
362fcf3ce44SJohn Forte  * iscsi_attach -- Attach instance of an iSCSI HBA.  We
363fcf3ce44SJohn Forte  * will attempt to create our HBA and register it with
364fcf3ce44SJohn Forte  * scsi_vhci.  If it's not possible to create the HBA
365fcf3ce44SJohn Forte  * or register with vhci we will fail the attach.
366fcf3ce44SJohn Forte  */
367fcf3ce44SJohn Forte static int
iscsi_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)368fcf3ce44SJohn Forte iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
369fcf3ce44SJohn Forte {
370fcf3ce44SJohn Forte 	int			instance	= ddi_get_instance(dip);
371fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp		= NULL;
372fcf3ce44SJohn Forte 	scsi_hba_tran_t		*tran		= NULL;
373fcf3ce44SJohn Forte 	char			init_port_name[MAX_NAME_PROP_SIZE];
374fcf3ce44SJohn Forte 
3757284664aSJoshua M. Clulow 	if (cmd == DDI_RESUME) {
3767284664aSJoshua M. Clulow 		return (DDI_SUCCESS);
3777284664aSJoshua M. Clulow 	} else if (cmd != DDI_ATTACH) {
3787284664aSJoshua M. Clulow 		return (DDI_FAILURE);
3797284664aSJoshua M. Clulow 	}
380fcf3ce44SJohn Forte 
3817284664aSJoshua M. Clulow 	if (!modrootloaded && iscsiboot_prop == NULL) {
3827284664aSJoshua M. Clulow 		/*
3837284664aSJoshua M. Clulow 		 * The root file system has not yet been mounted, and we're not
3847284664aSJoshua M. Clulow 		 * trying to boot from an iSCSI device.  Fail to attach now so
3857284664aSJoshua M. Clulow 		 * that we can retry after root has been mounted.
3867284664aSJoshua M. Clulow 		 */
3877284664aSJoshua M. Clulow 		return (DDI_FAILURE);
3887284664aSJoshua M. Clulow 	}
389fcf3ce44SJohn Forte 
3907284664aSJoshua M. Clulow 	/* create iSCSI HBA devctl device node */
3917284664aSJoshua M. Clulow 	if (ddi_create_minor_node(dip, ISCSI_DEVCTL, S_IFCHR, 0,
3927284664aSJoshua M. Clulow 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
3937284664aSJoshua M. Clulow 		goto iscsi_attach_failed3;
3947284664aSJoshua M. Clulow 	}
395fcf3ce44SJohn Forte 
3967284664aSJoshua M. Clulow 	/* allocate HBA soft state */
3977284664aSJoshua M. Clulow 	if (ddi_soft_state_zalloc(iscsi_state, instance) !=
3987284664aSJoshua M. Clulow 	    DDI_SUCCESS) {
3997284664aSJoshua M. Clulow 		ddi_remove_minor_node(dip, NULL);
4007284664aSJoshua M. Clulow 		goto iscsi_attach_failed3;
4017284664aSJoshua M. Clulow 	}
40230e7468fSPeter Dunlap 
4037284664aSJoshua M. Clulow 	/* get reference to soft state */
4047284664aSJoshua M. Clulow 	if ((ihp = (iscsi_hba_t *)ddi_get_soft_state(
4057284664aSJoshua M. Clulow 	    iscsi_state, instance)) == NULL) {
4067284664aSJoshua M. Clulow 		ddi_remove_minor_node(dip, NULL);
4077284664aSJoshua M. Clulow 		ddi_soft_state_free(iscsi_state, instance);
4087284664aSJoshua M. Clulow 		goto iscsi_attach_failed3;
4097284664aSJoshua M. Clulow 	}
4104246c8e9SJack Meng 
4117284664aSJoshua M. Clulow 	/* init HBA mutex used to protect discovery events */
4127284664aSJoshua M. Clulow 	mutex_init(&ihp->hba_discovery_events_mutex, NULL,
4137284664aSJoshua M. Clulow 	    MUTEX_DRIVER, NULL);
414fcf3ce44SJohn Forte 
4157284664aSJoshua M. Clulow 	VERIFY0(ldi_ident_from_dip(dip, &ihp->hba_li));
416fcf3ce44SJohn Forte 
4177284664aSJoshua M. Clulow 	/* init HBA mutex used to protect service status */
4187284664aSJoshua M. Clulow 	mutex_init(&ihp->hba_service_lock, NULL,
4197284664aSJoshua M. Clulow 	    MUTEX_DRIVER, NULL);
4207284664aSJoshua M. Clulow 	cv_init(&ihp->hba_service_cv, NULL, CV_DRIVER, NULL);
421fcf3ce44SJohn Forte 
4227284664aSJoshua M. Clulow 	/*
4237284664aSJoshua M. Clulow 	 * init SendTargets semaphore that is used to allow
4247284664aSJoshua M. Clulow 	 * only one operation at a time
4257284664aSJoshua M. Clulow 	 */
4267284664aSJoshua M. Clulow 	sema_init(&ihp->hba_sendtgts_semaphore, 1, NULL,
4277284664aSJoshua M. Clulow 	    SEMA_DRIVER, NULL);
428fcf3ce44SJohn Forte 
4297284664aSJoshua M. Clulow 	ihp->hba_sess_list = NULL;
4307284664aSJoshua M. Clulow 	rw_init(&ihp->hba_sess_list_rwlock, NULL,
4317284664aSJoshua M. Clulow 	    RW_DRIVER, NULL);
432fcf3ce44SJohn Forte 
4337284664aSJoshua M. Clulow 	/* allocate scsi_hba_tran */
4347284664aSJoshua M. Clulow 	if ((tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP))
4357284664aSJoshua M. Clulow 	    == NULL) {
4367284664aSJoshua M. Clulow 		ddi_remove_minor_node(dip, NULL);
4377284664aSJoshua M. Clulow 		goto iscsi_attach_failed2;
4387284664aSJoshua M. Clulow 	}
439fcf3ce44SJohn Forte 
4407284664aSJoshua M. Clulow 	/* soft state setup */
4417284664aSJoshua M. Clulow 	ihp->hba_sig	= ISCSI_SIG_HBA;
4427284664aSJoshua M. Clulow 	ihp->hba_tran	= tran;
4437284664aSJoshua M. Clulow 	ihp->hba_dip	= dip;
4447284664aSJoshua M. Clulow 	if (iscsiboot_prop == NULL) {
4457284664aSJoshua M. Clulow 		ihp->hba_service_status =
4467284664aSJoshua M. Clulow 		    ISCSI_SERVICE_DISABLED;
4477284664aSJoshua M. Clulow 		ihp->hba_service_status_overwrite = B_FALSE;
4487284664aSJoshua M. Clulow 	} else {
4497284664aSJoshua M. Clulow 		ihp->hba_service_status =
4507284664aSJoshua M. Clulow 		    ISCSI_SERVICE_ENABLED;
4517284664aSJoshua M. Clulow 		ihp->hba_service_status_overwrite = B_TRUE;
4527284664aSJoshua M. Clulow 	}
4537284664aSJoshua M. Clulow 	ihp->hba_service_client_count = 0;
4547284664aSJoshua M. Clulow 
4557284664aSJoshua M. Clulow 	mutex_enter(&iscsi_oid_mutex);
4567284664aSJoshua M. Clulow 	ihp->hba_oid		  = iscsi_oid++;
4577284664aSJoshua M. Clulow 	mutex_exit(&iscsi_oid_mutex);
4587284664aSJoshua M. Clulow 
4597284664aSJoshua M. Clulow 	ihp->hba_name[0]	  = '\0';
4607284664aSJoshua M. Clulow 	ihp->hba_name_length	  = 0;
4617284664aSJoshua M. Clulow 	ihp->hba_alias_length	  = 0;
4627284664aSJoshua M. Clulow 	ihp->hba_alias[0]	  = '\0';
4637284664aSJoshua M. Clulow 
4647284664aSJoshua M. Clulow 	iscsi_net->tweaks.rcvbuf = ddi_prop_get_int(
4657284664aSJoshua M. Clulow 	    DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-rcvbuf",
4667284664aSJoshua M. Clulow 	    ISCSI_SOCKET_RCVBUF_SIZE);
4677284664aSJoshua M. Clulow 
4687284664aSJoshua M. Clulow 	iscsi_net->tweaks.sndbuf = ddi_prop_get_int(
4697284664aSJoshua M. Clulow 	    DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-sndbuf",
4707284664aSJoshua M. Clulow 	    ISCSI_SOCKET_SNDBUF_SIZE);
4717284664aSJoshua M. Clulow 
4727284664aSJoshua M. Clulow 	iscsi_net->tweaks.nodelay = ddi_prop_get_int(
4737284664aSJoshua M. Clulow 	    DDI_DEV_T_ANY, ihp->hba_dip, 0, "tcp-nodelay",
4747284664aSJoshua M. Clulow 	    ISCSI_TCP_NODELAY_DEFAULT);
4757284664aSJoshua M. Clulow 
4767284664aSJoshua M. Clulow 	iscsi_net->tweaks.conn_notify_threshold =
4777284664aSJoshua M. Clulow 	    ddi_prop_get_int(DDI_DEV_T_ANY,
4787284664aSJoshua M. Clulow 	    ihp->hba_dip, 0, "tcp-conn-notify-threshold",
4797284664aSJoshua M. Clulow 	    ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT);
4807284664aSJoshua M. Clulow 
4817284664aSJoshua M. Clulow 	iscsi_net->tweaks.conn_abort_threshold =
4827284664aSJoshua M. Clulow 	    ddi_prop_get_int(DDI_DEV_T_ANY, ihp->hba_dip,
4837284664aSJoshua M. Clulow 	    0, "tcp-conn-abort-threshold",
4847284664aSJoshua M. Clulow 	    ISCSI_TCP_CABORT_THRESHOLD_DEFAULT);
4857284664aSJoshua M. Clulow 
4867284664aSJoshua M. Clulow 	iscsi_net->tweaks.abort_threshold = ddi_prop_get_int(
4877284664aSJoshua M. Clulow 	    DDI_DEV_T_ANY, ihp->hba_dip, 0,
4887284664aSJoshua M. Clulow 	    "tcp-abort-threshold",
4897284664aSJoshua M. Clulow 	    ISCSI_TCP_ABORT_THRESHOLD_DEFAULT);
4907284664aSJoshua M. Clulow 
4917284664aSJoshua M. Clulow 	ihp->hba_config_storm_delay = ddi_prop_get_int(
4927284664aSJoshua M. Clulow 	    DDI_DEV_T_ANY, ihp->hba_dip, 0,
4937284664aSJoshua M. Clulow 	    "config-storm-delay",
4947284664aSJoshua M. Clulow 	    ISCSI_CONFIG_STORM_DELAY_DEFAULT);
4957284664aSJoshua M. Clulow 
4967284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
4977284664aSJoshua M. Clulow 	    "so-rcvbuf", iscsi_net->tweaks.rcvbuf);
4987284664aSJoshua M. Clulow 
4997284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5007284664aSJoshua M. Clulow 	    "so-sndbuf", iscsi_net->tweaks.sndbuf);
5017284664aSJoshua M. Clulow 
5027284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5037284664aSJoshua M. Clulow 	    "tcp-nodelay", iscsi_net->tweaks.nodelay);
5047284664aSJoshua M. Clulow 
5057284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5067284664aSJoshua M. Clulow 	    "tcp-conn-notify-threshold",
5077284664aSJoshua M. Clulow 	    iscsi_net->tweaks.conn_notify_threshold);
5087284664aSJoshua M. Clulow 
5097284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5107284664aSJoshua M. Clulow 	    "tcp-conn-abort-threshold",
5117284664aSJoshua M. Clulow 	    iscsi_net->tweaks.conn_abort_threshold);
5127284664aSJoshua M. Clulow 
5137284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5147284664aSJoshua M. Clulow 	    "tcp-abort-threshold",
5157284664aSJoshua M. Clulow 	    iscsi_net->tweaks.abort_threshold);
5167284664aSJoshua M. Clulow 
5177284664aSJoshua M. Clulow 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
5187284664aSJoshua M. Clulow 	    "config-storm-delay",
5197284664aSJoshua M. Clulow 	    ihp->hba_config_storm_delay);
5207284664aSJoshua M. Clulow 
5217284664aSJoshua M. Clulow 	/* setup hba defaults */
5227284664aSJoshua M. Clulow 	iscsi_set_default_login_params(&ihp->hba_params);
5237284664aSJoshua M. Clulow 	iscsi_set_default_tunable_params(
5247284664aSJoshua M. Clulow 	    &ihp->hba_tunable_params);
5257284664aSJoshua M. Clulow 
5267284664aSJoshua M. Clulow 	/* setup minimal initiator params */
5277284664aSJoshua M. Clulow 	iscsid_set_default_initiator_node_settings(ihp, B_TRUE);
5287284664aSJoshua M. Clulow 
5297284664aSJoshua M. Clulow 	/* hba set up */
5307284664aSJoshua M. Clulow 	tran->tran_hba_private  = ihp;
5317284664aSJoshua M. Clulow 	tran->tran_tgt_private  = NULL;
5327284664aSJoshua M. Clulow 	tran->tran_tgt_init	= iscsi_tran_lun_init;
5337284664aSJoshua M. Clulow 	tran->tran_tgt_probe	= iscsi_tran_lun_probe;
5347284664aSJoshua M. Clulow 	tran->tran_tgt_free	= iscsi_tran_lun_free;
5357284664aSJoshua M. Clulow 	tran->tran_start	= iscsi_tran_start;
5367284664aSJoshua M. Clulow 	tran->tran_abort	= iscsi_tran_abort;
5377284664aSJoshua M. Clulow 	tran->tran_reset	= iscsi_tran_reset;
5387284664aSJoshua M. Clulow 	tran->tran_getcap	= iscsi_tran_getcap;
5397284664aSJoshua M. Clulow 	tran->tran_setcap	= iscsi_tran_setcap;
5407284664aSJoshua M. Clulow 	tran->tran_init_pkt	= iscsi_tran_init_pkt;
5417284664aSJoshua M. Clulow 	tran->tran_destroy_pkt	= iscsi_tran_destroy_pkt;
5427284664aSJoshua M. Clulow 	tran->tran_dmafree	= iscsi_tran_dmafree;
5437284664aSJoshua M. Clulow 	tran->tran_sync_pkt	= iscsi_tran_sync_pkt;
5447284664aSJoshua M. Clulow 	tran->tran_reset_notify	= iscsi_tran_reset_notify;
5457284664aSJoshua M. Clulow 	tran->tran_bus_config	= iscsi_tran_bus_config;
5467284664aSJoshua M. Clulow 	tran->tran_bus_unconfig	= iscsi_tran_bus_unconfig;
5477284664aSJoshua M. Clulow 
5487284664aSJoshua M. Clulow 	tran->tran_get_name	= iscsi_tran_get_name;
5497284664aSJoshua M. Clulow 	tran->tran_get_bus_addr	= iscsi_tran_get_bus_addr;
5507284664aSJoshua M. Clulow 	tran->tran_interconnect_type = INTERCONNECT_ISCSI;
5517284664aSJoshua M. Clulow 
5527284664aSJoshua M. Clulow 	/* register scsi hba with scsa */
5537284664aSJoshua M. Clulow 	if (scsi_hba_attach_setup(dip, &iscsi_dma_attr,
5547284664aSJoshua M. Clulow 	    tran, SCSI_HBA_TRAN_CLONE) != DDI_SUCCESS) {
5557284664aSJoshua M. Clulow 		goto iscsi_attach_failed1;
5567284664aSJoshua M. Clulow 	}
557fcf3ce44SJohn Forte 
5587284664aSJoshua M. Clulow 	/* register scsi hba with mdi (MPxIO/vhci) */
5597284664aSJoshua M. Clulow 	if (mdi_phci_register(MDI_HCI_CLASS_SCSI, dip, 0) !=
5607284664aSJoshua M. Clulow 	    MDI_SUCCESS) {
5617284664aSJoshua M. Clulow 		ihp->hba_mpxio_enabled = B_FALSE;
5627284664aSJoshua M. Clulow 	} else {
5637284664aSJoshua M. Clulow 		ihp->hba_mpxio_enabled = B_TRUE;
5647284664aSJoshua M. Clulow 	}
565fcf3ce44SJohn Forte 
5667284664aSJoshua M. Clulow 	(void) iscsi_hba_kstat_init(ihp);
567fcf3ce44SJohn Forte 
5687284664aSJoshua M. Clulow 	/* Initialize targetparam list */
5697284664aSJoshua M. Clulow 	iscsi_targetparam_init();
570fcf3ce44SJohn Forte 
5717284664aSJoshua M. Clulow 	/* Initialize ISID */
5727284664aSJoshua M. Clulow 	ihp->hba_isid[0] = ISCSI_SUN_ISID_0;
5737284664aSJoshua M. Clulow 	ihp->hba_isid[1] = ISCSI_SUN_ISID_1;
5747284664aSJoshua M. Clulow 	ihp->hba_isid[2] = ISCSI_SUN_ISID_2;
5757284664aSJoshua M. Clulow 	ihp->hba_isid[3] = ISCSI_SUN_ISID_3;
5767284664aSJoshua M. Clulow 	ihp->hba_isid[4] = ISCSI_SUN_ISID_4;
5777284664aSJoshua M. Clulow 	ihp->hba_isid[5] = ISCSI_SUN_ISID_5;
578fcf3ce44SJohn Forte 
5797284664aSJoshua M. Clulow 	/* Setup iSNS transport services and client */
5807284664aSJoshua M. Clulow 	isns_client_init();
581fcf3ce44SJohn Forte 
5827284664aSJoshua M. Clulow 	/*
5837284664aSJoshua M. Clulow 	 * initialize persistent store,
5847284664aSJoshua M. Clulow 	 * or boot target info in case of iscsi boot
5857284664aSJoshua M. Clulow 	 */
5867284664aSJoshua M. Clulow 	ihp->hba_persistent_loaded = B_FALSE;
5877284664aSJoshua M. Clulow 	if (iscsid_init(ihp) == B_FALSE) {
5887284664aSJoshua M. Clulow 		goto iscsi_attach_failed0;
5897284664aSJoshua M. Clulow 	}
590fcf3ce44SJohn Forte 
5917284664aSJoshua M. Clulow 	/* Setup init_port_name for MPAPI */
5927284664aSJoshua M. Clulow 	(void) snprintf(init_port_name, MAX_NAME_PROP_SIZE,
5937284664aSJoshua M. Clulow 	    "%s,%02x%02x%02x%02x%02x%02x",
5947284664aSJoshua M. Clulow 	    (char *)ihp->hba_name, ihp->hba_isid[0],
5957284664aSJoshua M. Clulow 	    ihp->hba_isid[1], ihp->hba_isid[2],
5967284664aSJoshua M. Clulow 	    ihp->hba_isid[3], ihp->hba_isid[4],
5977284664aSJoshua M. Clulow 	    ihp->hba_isid[5]);
598fcf3ce44SJohn Forte 
5997284664aSJoshua M. Clulow 	if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
6007284664aSJoshua M. Clulow 	    SCSI_ADDR_PROP_INITIATOR_PORT, init_port_name) !=
6017284664aSJoshua M. Clulow 	    DDI_PROP_SUCCESS) {
6027284664aSJoshua M. Clulow 		cmn_err(CE_WARN, "iscsi_attach: Creating "
6037284664aSJoshua M. Clulow 		    SCSI_ADDR_PROP_INITIATOR_PORT
6047284664aSJoshua M. Clulow 		    " property on iSCSI "
6057284664aSJoshua M. Clulow 		    "HBA(%s) with dip(%d) Failed",
6067284664aSJoshua M. Clulow 		    (char *)ihp->hba_name,
6077284664aSJoshua M. Clulow 		    ddi_get_instance(dip));
608fcf3ce44SJohn Forte 	}
609fcf3ce44SJohn Forte 
6107284664aSJoshua M. Clulow 	ddi_report_dev(dip);
6117284664aSJoshua M. Clulow 	return (DDI_SUCCESS);
612fcf3ce44SJohn Forte 
6137284664aSJoshua M. Clulow iscsi_attach_failed0:
6147284664aSJoshua M. Clulow 	isns_client_cleanup();
6157284664aSJoshua M. Clulow 	if (ihp->stats.ks) {
6167284664aSJoshua M. Clulow 		(void) iscsi_hba_kstat_term(ihp);
6177284664aSJoshua M. Clulow 	}
6187284664aSJoshua M. Clulow 	if (ihp->hba_mpxio_enabled == B_TRUE) {
6197284664aSJoshua M. Clulow 		(void) mdi_phci_unregister(dip, 0);
6207284664aSJoshua M. Clulow 	}
6217284664aSJoshua M. Clulow 	(void) scsi_hba_detach(ihp->hba_dip);
6227284664aSJoshua M. Clulow iscsi_attach_failed1:
6237284664aSJoshua M. Clulow 	ddi_remove_minor_node(dip, NULL);
6247284664aSJoshua M. Clulow 	ddi_prop_remove_all(ihp->hba_dip);
6257284664aSJoshua M. Clulow 	scsi_hba_tran_free(tran);
6267284664aSJoshua M. Clulow iscsi_attach_failed2:
6277284664aSJoshua M. Clulow 	cv_destroy(&ihp->hba_service_cv);
6287284664aSJoshua M. Clulow 	mutex_destroy(&ihp->hba_service_lock);
6297284664aSJoshua M. Clulow 	mutex_destroy(&ihp->hba_discovery_events_mutex);
6307284664aSJoshua M. Clulow 	sema_destroy(&ihp->hba_sendtgts_semaphore);
6317284664aSJoshua M. Clulow 	rw_destroy(&ihp->hba_sess_list_rwlock);
6327284664aSJoshua M. Clulow 	ddi_soft_state_free(iscsi_state, instance);
6337284664aSJoshua M. Clulow iscsi_attach_failed3:
6347284664aSJoshua M. Clulow 	cmn_err(CE_WARN, "iscsi driver unable to attach "
6357284664aSJoshua M. Clulow 	    "hba instance %d", instance);
6367284664aSJoshua M. Clulow 	return (DDI_FAILURE);
637fcf3ce44SJohn Forte }
638fcf3ce44SJohn Forte 
639fcf3ce44SJohn Forte /*
640fcf3ce44SJohn Forte  * iscsi_detach - called on unload of hba instance
641fcf3ce44SJohn Forte  */
642fcf3ce44SJohn Forte static int
iscsi_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)643fcf3ce44SJohn Forte iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
644fcf3ce44SJohn Forte {
645fcf3ce44SJohn Forte 	int			rval		= DDI_SUCCESS;
646fcf3ce44SJohn Forte 	scsi_hba_tran_t		*tran		= NULL;
647fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp		= NULL;
648fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp_check	= NULL;
649fcf3ce44SJohn Forte 	int			instance;
650fcf3ce44SJohn Forte 	char			*init_node_name;
651fcf3ce44SJohn Forte 
652fcf3ce44SJohn Forte 	instance = ddi_get_instance(dip);
653fcf3ce44SJohn Forte 
654fcf3ce44SJohn Forte 	switch (cmd) {
655fcf3ce44SJohn Forte 	case DDI_DETACH:
656fcf3ce44SJohn Forte 		if (!(tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip))) {
657fcf3ce44SJohn Forte 			rval = DDI_SUCCESS;
658fcf3ce44SJohn Forte 			break;
659fcf3ce44SJohn Forte 		}
660fcf3ce44SJohn Forte 
661fcf3ce44SJohn Forte 		if ((ihp = (iscsi_hba_t *)tran->tran_hba_private) == NULL) {
662fcf3ce44SJohn Forte 			rval =  DDI_FAILURE;
663fcf3ce44SJohn Forte 			break;
664fcf3ce44SJohn Forte 		}
665fcf3ce44SJohn Forte 
666fcf3ce44SJohn Forte 		/*
667fcf3ce44SJohn Forte 		 * Validate that what is stored by the DDI framework is still
668fcf3ce44SJohn Forte 		 * the same state structure referenced by the SCSI framework
669fcf3ce44SJohn Forte 		 */
670fcf3ce44SJohn Forte 		ihp_check = ddi_get_soft_state(iscsi_state, instance);
671fcf3ce44SJohn Forte 		if (ihp_check != ihp) {
672fcf3ce44SJohn Forte 			rval = DDI_FAILURE;
673fcf3ce44SJohn Forte 			break;
674fcf3ce44SJohn Forte 		}
675fcf3ce44SJohn Forte 
676fcf3ce44SJohn Forte 		/* If a session exists we can't safely detach */
677fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
678fcf3ce44SJohn Forte 		if (ihp->hba_sess_list != NULL) {
679fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
680fcf3ce44SJohn Forte 			rval = DDI_FAILURE;
681fcf3ce44SJohn Forte 			break;
682fcf3ce44SJohn Forte 		}
683fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
684fcf3ce44SJohn Forte 
685fcf3ce44SJohn Forte 		/* Disable all discovery services */
686fcf3ce44SJohn Forte 		if (iscsid_disable_discovery(ihp,
687fcf3ce44SJohn Forte 		    ISCSI_ALL_DISCOVERY_METHODS) == B_FALSE) {
688fcf3ce44SJohn Forte 			/* Disable failed.  Fail detach */
689fcf3ce44SJohn Forte 			rval = DDI_FAILURE;
690fcf3ce44SJohn Forte 			break;
691fcf3ce44SJohn Forte 		}
692fcf3ce44SJohn Forte 
693fcf3ce44SJohn Forte 		/* Deregister from iSNS server(s). */
694fcf3ce44SJohn Forte 		init_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
695fcf3ce44SJohn Forte 		if (persistent_initiator_name_get(init_node_name,
696fcf3ce44SJohn Forte 		    ISCSI_MAX_NAME_LEN) == B_TRUE) {
697fcf3ce44SJohn Forte 			if (strlen(init_node_name) > 0) {
698fcf3ce44SJohn Forte 				(void) isns_dereg(ihp->hba_isid,
699fcf3ce44SJohn Forte 				    (uint8_t *)init_node_name);
700fcf3ce44SJohn Forte 			}
701fcf3ce44SJohn Forte 		}
702fcf3ce44SJohn Forte 		kmem_free(init_node_name, ISCSI_MAX_NAME_LEN);
703fcf3ce44SJohn Forte 		init_node_name = NULL;
704fcf3ce44SJohn Forte 
705fcf3ce44SJohn Forte 		/* Cleanup iSNS Client */
706fcf3ce44SJohn Forte 		isns_client_cleanup();
707fcf3ce44SJohn Forte 
708fcf3ce44SJohn Forte 		iscsi_targetparam_cleanup();
709fcf3ce44SJohn Forte 
710fcf3ce44SJohn Forte 		/* Cleanup iscsid resources */
711fcf3ce44SJohn Forte 		iscsid_fini();
712fcf3ce44SJohn Forte 
713fcf3ce44SJohn Forte 		if (rval != DDI_SUCCESS) {
714fcf3ce44SJohn Forte 			break;
715fcf3ce44SJohn Forte 		}
716fcf3ce44SJohn Forte 		/* kstat hba. destroy */
717fcf3ce44SJohn Forte 		KSTAT_DEC_HBA_CNTR_SESS(ihp);
718fcf3ce44SJohn Forte 
719fcf3ce44SJohn Forte 		if (ihp->hba_mpxio_enabled == B_TRUE) {
720fcf3ce44SJohn Forte 			(void) mdi_phci_unregister(dip, 0);
721fcf3ce44SJohn Forte 		}
722fcf3ce44SJohn Forte 		ddi_remove_minor_node(dip, NULL);
723fcf3ce44SJohn Forte 
724fcf3ce44SJohn Forte 		ddi_prop_remove_all(ihp->hba_dip);
72530e7468fSPeter Dunlap 
72630e7468fSPeter Dunlap 		ldi_ident_release(ihp->hba_li);
72730e7468fSPeter Dunlap 
7284246c8e9SJack Meng 		cv_destroy(&ihp->hba_service_cv);
7294246c8e9SJack Meng 		mutex_destroy(&ihp->hba_service_lock);
730fcf3ce44SJohn Forte 		mutex_destroy(&ihp->hba_discovery_events_mutex);
731fcf3ce44SJohn Forte 		rw_destroy(&ihp->hba_sess_list_rwlock);
732fcf3ce44SJohn Forte 		(void) iscsi_hba_kstat_term(ihp);
733fcf3ce44SJohn Forte 
734fcf3ce44SJohn Forte 		(void) scsi_hba_detach(dip);
735fcf3ce44SJohn Forte 		if (tran != NULL) {
736fcf3ce44SJohn Forte 			scsi_hba_tran_free(tran);
737fcf3ce44SJohn Forte 		}
738fcf3ce44SJohn Forte 		ddi_soft_state_free(iscsi_state, instance);
739fcf3ce44SJohn Forte 		break;
740fcf3ce44SJohn Forte 	default:
741fcf3ce44SJohn Forte 		break;
742fcf3ce44SJohn Forte 	}
743fcf3ce44SJohn Forte 
744fcf3ce44SJohn Forte 	if (rval != DDI_SUCCESS) {
745fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi driver unable to "
746fcf3ce44SJohn Forte 		    "detach hba instance %d", instance);
747fcf3ce44SJohn Forte 	}
748fcf3ce44SJohn Forte 
749fcf3ce44SJohn Forte 	return (rval);
750fcf3ce44SJohn Forte }
751fcf3ce44SJohn Forte 
752fcf3ce44SJohn Forte /*
753fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
754fcf3ce44SJohn Forte  * | End of dev_ops routines						|
755fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
756fcf3ce44SJohn Forte  */
757fcf3ce44SJohn Forte 
758fcf3ce44SJohn Forte /*
759fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
760fcf3ce44SJohn Forte  * | scsi_tran(9E) routines						|
761fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
762fcf3ce44SJohn Forte  */
763fcf3ce44SJohn Forte 
764fcf3ce44SJohn Forte /*
765fcf3ce44SJohn Forte  * iscsi_tran_lun_init - Find target device based on SCSI device
766fcf3ce44SJohn Forte  * Based on the information given (SCSI device, target dev_info) find
767fcf3ce44SJohn Forte  * the target iSCSI device and put a pointer to that information in
768fcf3ce44SJohn Forte  * the scsi_hba_tran_t structure.
769fcf3ce44SJohn Forte  */
770fcf3ce44SJohn Forte static int
iscsi_tran_lun_init(dev_info_t * hba_dip,dev_info_t * lun_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)771fcf3ce44SJohn Forte iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
772fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
773fcf3ce44SJohn Forte {
774fcf3ce44SJohn Forte 	int		rval	= 0;
775fcf3ce44SJohn Forte 	int		type	= 0;
776fcf3ce44SJohn Forte 
777fcf3ce44SJohn Forte 	ASSERT(hba_tran->tran_hba_private != NULL);
778fcf3ce44SJohn Forte 
779fcf3ce44SJohn Forte 	/*
780fcf3ce44SJohn Forte 	 * Child node is getting initialized.  Look at the mpxio component
781fcf3ce44SJohn Forte 	 * type on the child device to see if this device is mpxio managed
782fcf3ce44SJohn Forte 	 * or not.
783fcf3ce44SJohn Forte 	 */
784fcf3ce44SJohn Forte 	type = mdi_get_component_type(lun_dip);
785fcf3ce44SJohn Forte 	if (type != MDI_COMPONENT_CLIENT) {
786fcf3ce44SJohn Forte 		rval = iscsi_phys_lun_init(hba_dip, lun_dip, hba_tran, sd);
787fcf3ce44SJohn Forte 	} else {
788fcf3ce44SJohn Forte 		rval = iscsi_virt_lun_init(hba_dip, lun_dip, hba_tran, sd);
789fcf3ce44SJohn Forte 	}
790fcf3ce44SJohn Forte 
791fcf3ce44SJohn Forte 	return (rval);
792fcf3ce44SJohn Forte }
793fcf3ce44SJohn Forte 
794fcf3ce44SJohn Forte /*
795fcf3ce44SJohn Forte  * iscsi_tran_lun_probe - This function didn't need to be implemented.
796fcf3ce44SJohn Forte  * We could have left NULL in the tran table.  Since this isn't a
797fcf3ce44SJohn Forte  * performance path this seems safe.  We are just wrappering the
798fcf3ce44SJohn Forte  * function so we can see the call go through if we have debugging
799fcf3ce44SJohn Forte  * enabled.
800fcf3ce44SJohn Forte  */
801fcf3ce44SJohn Forte static int
iscsi_tran_lun_probe(struct scsi_device * sd,int (* callback)())802fcf3ce44SJohn Forte iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ())
803fcf3ce44SJohn Forte {
804fcf3ce44SJohn Forte 	int rval = 0;
805fcf3ce44SJohn Forte 
806fcf3ce44SJohn Forte 	rval = scsi_hba_probe(sd, callback);
807fcf3ce44SJohn Forte 
808fcf3ce44SJohn Forte 	return (rval);
809fcf3ce44SJohn Forte }
810fcf3ce44SJohn Forte 
811fcf3ce44SJohn Forte /*
812fcf3ce44SJohn Forte  * iscsi_init_pkt - Allocate SCSI packet and fill in required info.
813fcf3ce44SJohn Forte  */
814fcf3ce44SJohn Forte /* ARGSUSED */
815fcf3ce44SJohn Forte static struct scsi_pkt *
iscsi_tran_init_pkt(struct scsi_address * ap,struct scsi_pkt * pkt,struct buf * bp,int cmdlen,int statuslen,int tgtlen,int flags,int (* callback)(),caddr_t arg)816fcf3ce44SJohn Forte iscsi_tran_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
817fcf3ce44SJohn Forte     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
818fcf3ce44SJohn Forte     int (*callback) (), caddr_t arg)
819fcf3ce44SJohn Forte {
820fcf3ce44SJohn Forte 	iscsi_lun_t *ilp;
821fcf3ce44SJohn Forte 	iscsi_cmd_t *icmdp;
822fcf3ce44SJohn Forte 
823fcf3ce44SJohn Forte 	ASSERT(ap != NULL);
824fcf3ce44SJohn Forte 	ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
825fcf3ce44SJohn Forte 
826fcf3ce44SJohn Forte 	/*
827fcf3ce44SJohn Forte 	 * The software stack doesn't have DMA which means the iSCSI
828fcf3ce44SJohn Forte 	 * protocol layer will be doing a bcopy from bp to outgoing
829fcf3ce44SJohn Forte 	 * streams buffers. Make sure that the buffer is mapped in
830fcf3ce44SJohn Forte 	 * so that the copy won't panic the system.
831fcf3ce44SJohn Forte 	 */
8325e5f9182SJack Meng 	if (bp && (bp->b_bcount != 0) &&
8335e5f9182SJack Meng 	    bp_mapin_common(bp, (callback == NULL_FUNC) ?
834fcf3ce44SJohn Forte 	    VM_NOSLEEP : VM_SLEEP) == NULL) {
835fcf3ce44SJohn Forte 		return (NULL);
836fcf3ce44SJohn Forte 	}
837fcf3ce44SJohn Forte 
838fcf3ce44SJohn Forte 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
839fcf3ce44SJohn Forte 	ASSERT(ilp != NULL);
840fcf3ce44SJohn Forte 
841fcf3ce44SJohn Forte 	if (pkt == NULL) {
842fcf3ce44SJohn Forte 		pkt = scsi_hba_pkt_alloc(ilp->lun_sess->sess_hba->hba_dip,
843fcf3ce44SJohn Forte 		    ap, cmdlen, statuslen, tgtlen, sizeof (iscsi_cmd_t),
844fcf3ce44SJohn Forte 		    callback, arg);
845fcf3ce44SJohn Forte 		if (pkt == NULL) {
846fcf3ce44SJohn Forte 			return (NULL);
847fcf3ce44SJohn Forte 		}
848fcf3ce44SJohn Forte 		icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
849fcf3ce44SJohn Forte 		icmdp->cmd_sig			= ISCSI_SIG_CMD;
850fcf3ce44SJohn Forte 		icmdp->cmd_state		= ISCSI_CMD_STATE_FREE;
851fcf3ce44SJohn Forte 		icmdp->cmd_lun			= ilp;
852*61dfa509SRick McNeal 		iscsi_lun_hold(ilp);
853fcf3ce44SJohn Forte 		icmdp->cmd_type			= ISCSI_CMD_TYPE_SCSI;
854fcf3ce44SJohn Forte 		/* add the report lun addressing type on to the lun */
855fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.lun		= ilp->lun_addr_type << 14;
856fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.lun		= icmdp->cmd_un.scsi.lun |
857fcf3ce44SJohn Forte 		    ilp->lun_num;
858fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.pkt		= pkt;
859fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.bp		= bp;
860fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.cmdlen	= cmdlen;
861fcf3ce44SJohn Forte 		icmdp->cmd_un.scsi.statuslen	= statuslen;
862fcf3ce44SJohn Forte 		icmdp->cmd_crc_error_seen	= B_FALSE;
863d233de7eSJack Meng 		icmdp->cmd_misc_flags		= 0;
864b4243054SSheng-Liang Eric Zhang 		if (flags & PKT_XARQ) {
865b4243054SSheng-Liang Eric Zhang 			icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_XARQ;
866b4243054SSheng-Liang Eric Zhang 		}
867b4243054SSheng-Liang Eric Zhang 
86830e7468fSPeter Dunlap 
86930e7468fSPeter Dunlap 		idm_sm_audit_init(&icmdp->cmd_state_audit);
87030e7468fSPeter Dunlap 
871fcf3ce44SJohn Forte 		mutex_init(&icmdp->cmd_mutex, NULL, MUTEX_DRIVER, NULL);
872fcf3ce44SJohn Forte 		cv_init(&icmdp->cmd_completion, NULL, CV_DRIVER, NULL);
873fcf3ce44SJohn Forte 
874fcf3ce44SJohn Forte 		pkt->pkt_address		= *ap;
875fcf3ce44SJohn Forte 		pkt->pkt_comp			= (void (*)())NULL;
876fcf3ce44SJohn Forte 		pkt->pkt_flags			= 0;
877fcf3ce44SJohn Forte 		pkt->pkt_time			= 0;
878fcf3ce44SJohn Forte 		pkt->pkt_resid			= 0;
879fcf3ce44SJohn Forte 		pkt->pkt_statistics		= 0;
880fcf3ce44SJohn Forte 		pkt->pkt_reason			= 0;
881fcf3ce44SJohn Forte 	}
882fcf3ce44SJohn Forte 	return (pkt);
883fcf3ce44SJohn Forte }
884fcf3ce44SJohn Forte 
885fcf3ce44SJohn Forte /*
886fcf3ce44SJohn Forte  * iscsi_tran_lun_free - Free a SCSI LUN
887fcf3ce44SJohn Forte  */
888fcf3ce44SJohn Forte static void
iscsi_tran_lun_free(dev_info_t * hba_dip,dev_info_t * lun_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)889fcf3ce44SJohn Forte iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip,
890fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
891fcf3ce44SJohn Forte {
892fcf3ce44SJohn Forte 	iscsi_lun_t *ilp = NULL;
893fcf3ce44SJohn Forte 
894fcf3ce44SJohn Forte 	ASSERT(hba_dip != NULL);
895fcf3ce44SJohn Forte 	ASSERT(lun_dip != NULL);
896fcf3ce44SJohn Forte 	ASSERT(hba_tran != NULL);
897fcf3ce44SJohn Forte 	ASSERT(sd != NULL);
898fcf3ce44SJohn Forte 	ilp = (iscsi_lun_t *)hba_tran->tran_tgt_private;
899fcf3ce44SJohn Forte 	ASSERT(ilp != NULL);
900fcf3ce44SJohn Forte 
901fcf3ce44SJohn Forte 	(void) mdi_prop_remove(ilp->lun_pip, NULL);
902fcf3ce44SJohn Forte }
903fcf3ce44SJohn Forte 
904fcf3ce44SJohn Forte /*
905fcf3ce44SJohn Forte  * iscsi_start -- Start a SCSI transaction based on the packet
906fcf3ce44SJohn Forte  * This will attempt to add the icmdp to the pending queue
907fcf3ce44SJohn Forte  * for the connection and kick the queue.  If the enqueue
908fcf3ce44SJohn Forte  * fails that means the queue is full.
909fcf3ce44SJohn Forte  */
910fcf3ce44SJohn Forte static int
iscsi_tran_start(struct scsi_address * ap,struct scsi_pkt * pkt)911fcf3ce44SJohn Forte iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt)
912fcf3ce44SJohn Forte {
913fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp		= NULL;
914fcf3ce44SJohn Forte 	iscsi_sess_t	*isp		= NULL;
915fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp		= NULL;
916fcf3ce44SJohn Forte 	uint_t		flags;
917fcf3ce44SJohn Forte 
918fcf3ce44SJohn Forte 	ASSERT(ap != NULL);
919fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
920fcf3ce44SJohn Forte 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
921fcf3ce44SJohn Forte 	isp = (iscsi_sess_t *)ilp->lun_sess;
922fcf3ce44SJohn Forte 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
923fcf3ce44SJohn Forte 	flags = pkt->pkt_flags;
924fcf3ce44SJohn Forte 	ASSERT(ilp != NULL);
925fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
926fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
927fcf3ce44SJohn Forte 
928fcf3ce44SJohn Forte 	/*
929fcf3ce44SJohn Forte 	 * If the session is in the FREE state then
930fcf3ce44SJohn Forte 	 * all connections are down and retries have
931fcf3ce44SJohn Forte 	 * been exhausted.  Fail command with fatal error.
932fcf3ce44SJohn Forte 	 */
933904e51f6SJack Meng 	rw_enter(&isp->sess_state_rwlock, RW_READER);
934fcf3ce44SJohn Forte 	if (isp->sess_state == ISCSI_SESS_STATE_FREE) {
935904e51f6SJack Meng 		rw_exit(&isp->sess_state_rwlock);
936904e51f6SJack Meng 		return (TRAN_FATAL_ERROR);
937904e51f6SJack Meng 	}
938904e51f6SJack Meng 
939904e51f6SJack Meng 	/*
940904e51f6SJack Meng 	 * If we haven't received data from the target in the
941904e51f6SJack Meng 	 * max specified period something is wrong with the
942904e51f6SJack Meng 	 * transport.  Fail IO with FATAL_ERROR.
943904e51f6SJack Meng 	 */
944904e51f6SJack Meng 	if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_max_window) <
945904e51f6SJack Meng 	    ddi_get_lbolt()) {
946904e51f6SJack Meng 		rw_exit(&isp->sess_state_rwlock);
947fcf3ce44SJohn Forte 		return (TRAN_FATAL_ERROR);
948fcf3ce44SJohn Forte 	}
949fcf3ce44SJohn Forte 
950fcf3ce44SJohn Forte 	/*
951fcf3ce44SJohn Forte 	 * If the session is not in LOGGED_IN then we have
952fcf3ce44SJohn Forte 	 * no connections LOGGED_IN, but we haven't exhuasted
953fcf3ce44SJohn Forte 	 * our retries.  Fail the command with busy so the
954fcf3ce44SJohn Forte 	 * caller might try again later.  Once retries are
955fcf3ce44SJohn Forte 	 * exhausted the state machine will move us to FREE.
956fcf3ce44SJohn Forte 	 */
957fcf3ce44SJohn Forte 	if (isp->sess_state != ISCSI_SESS_STATE_LOGGED_IN) {
958904e51f6SJack Meng 		rw_exit(&isp->sess_state_rwlock);
959fcf3ce44SJohn Forte 		return (TRAN_BUSY);
960fcf3ce44SJohn Forte 	}
961fcf3ce44SJohn Forte 
962fcf3ce44SJohn Forte 	/*
963fcf3ce44SJohn Forte 	 * If we haven't received data from the target in the
964fcf3ce44SJohn Forte 	 * specified period something is probably wrong with
965fcf3ce44SJohn Forte 	 * the transport.  Just return back BUSY until either
966fcf3ce44SJohn Forte 	 * the problem is resolved of the transport fails.
967fcf3ce44SJohn Forte 	 */
968fcf3ce44SJohn Forte 	if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_window) <
969fcf3ce44SJohn Forte 	    ddi_get_lbolt()) {
970904e51f6SJack Meng 		rw_exit(&isp->sess_state_rwlock);
971fcf3ce44SJohn Forte 		return (TRAN_BUSY);
972fcf3ce44SJohn Forte 	}
973fcf3ce44SJohn Forte 
974fcf3ce44SJohn Forte 
975fcf3ce44SJohn Forte 	/* reset cmd values in case upper level driver is retrying cmd */
976fcf3ce44SJohn Forte 	icmdp->cmd_prev = icmdp->cmd_next = NULL;
977fcf3ce44SJohn Forte 	icmdp->cmd_crc_error_seen = B_FALSE;
978fcf3ce44SJohn Forte 	icmdp->cmd_lbolt_pending = icmdp->cmd_lbolt_active =
979fcf3ce44SJohn Forte 	    icmdp->cmd_lbolt_aborting = icmdp->cmd_lbolt_timeout =
980fcf3ce44SJohn Forte 	    (clock_t)NULL;
981fcf3ce44SJohn Forte 	icmdp->cmd_itt = icmdp->cmd_ttt = 0;
982fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.abort_icmdp = NULL;
983fcf3ce44SJohn Forte 
984fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
985fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
986fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
987904e51f6SJack Meng 	rw_exit(&isp->sess_state_rwlock);
988fcf3ce44SJohn Forte 
989fcf3ce44SJohn Forte 	/*
990fcf3ce44SJohn Forte 	 * If this packet doesn't have FLAG_NOINTR set, it could have
991fcf3ce44SJohn Forte 	 * already run to completion (and the memory freed) at this
992fcf3ce44SJohn Forte 	 * point, so check our local copy of pkt_flags.  Otherwise we
993fcf3ce44SJohn Forte 	 * have to wait for completion before returning to the caller.
994fcf3ce44SJohn Forte 	 */
995fcf3ce44SJohn Forte 	if (flags & FLAG_NOINTR) {
996fcf3ce44SJohn Forte 		mutex_enter(&icmdp->cmd_mutex);
997fcf3ce44SJohn Forte 		while ((icmdp->cmd_state != ISCSI_CMD_STATE_COMPLETED) ||
998fcf3ce44SJohn Forte 		    (icmdp->cmd_un.scsi.r2t_icmdp != NULL) ||
999e790cde8Sbing zhao - Sun Microsystems - Beijing China 		    (icmdp->cmd_un.scsi.abort_icmdp != NULL) ||
1000e790cde8Sbing zhao - Sun Microsystems - Beijing China 		    (icmdp->cmd_un.scsi.r2t_more == B_TRUE)) {
1001fcf3ce44SJohn Forte 			cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
1002fcf3ce44SJohn Forte 		}
1003fcf3ce44SJohn Forte 		icmdp->cmd_state = ISCSI_CMD_STATE_FREE;
1004fcf3ce44SJohn Forte 		mutex_exit(&icmdp->cmd_mutex);
1005fcf3ce44SJohn Forte 	}
1006fcf3ce44SJohn Forte 
1007fcf3ce44SJohn Forte 	return (TRAN_ACCEPT);
1008fcf3ce44SJohn Forte }
1009fcf3ce44SJohn Forte 
1010fcf3ce44SJohn Forte /*
1011fcf3ce44SJohn Forte  * iscsi_tran_abort - Called when an upper level application
1012fcf3ce44SJohn Forte  * or driver wants to kill a scsi_pkt that was already sent to
1013fcf3ce44SJohn Forte  * this driver.
1014fcf3ce44SJohn Forte  */
1015fcf3ce44SJohn Forte /* ARGSUSED */
1016fcf3ce44SJohn Forte static int
iscsi_tran_abort(struct scsi_address * ap,struct scsi_pkt * pkt)1017fcf3ce44SJohn Forte iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1018fcf3ce44SJohn Forte {
1019fcf3ce44SJohn Forte 	return (0);
1020fcf3ce44SJohn Forte }
1021fcf3ce44SJohn Forte 
1022fcf3ce44SJohn Forte /*
1023fcf3ce44SJohn Forte  * iscsi_tran_reset - Reset target at either BUS, TARGET, or LUN
1024fcf3ce44SJohn Forte  * level.  This will require the issuing of a task management
1025fcf3ce44SJohn Forte  * command down to the target/lun.
1026fcf3ce44SJohn Forte  */
1027fcf3ce44SJohn Forte static int
iscsi_tran_reset(struct scsi_address * ap,int level)1028fcf3ce44SJohn Forte iscsi_tran_reset(struct scsi_address *ap, int level)
1029fcf3ce44SJohn Forte {
1030fcf3ce44SJohn Forte 	int		rval    = ISCSI_STATUS_INTERNAL_ERROR;
1031fcf3ce44SJohn Forte 	iscsi_sess_t	*isp    = NULL;
1032fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp    = NULL;
1033fcf3ce44SJohn Forte 
1034fcf3ce44SJohn Forte 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
1035fcf3ce44SJohn Forte 	ASSERT(ilp != NULL);
1036fcf3ce44SJohn Forte 	isp = ilp->lun_sess;
1037fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1038fcf3ce44SJohn Forte 
1039fcf3ce44SJohn Forte 	switch (level) {
1040fcf3ce44SJohn Forte 	case RESET_LUN:
1041fcf3ce44SJohn Forte 		/* reset attempt will block until attempt is complete */
1042fcf3ce44SJohn Forte 		rval = iscsi_handle_reset(isp, level, ilp);
1043fcf3ce44SJohn Forte 		break;
1044fcf3ce44SJohn Forte 	case RESET_BUS:
1045fcf3ce44SJohn Forte 		/*
1046fcf3ce44SJohn Forte 		 * What are we going to realy reset the ethernet
1047fcf3ce44SJohn Forte 		 * network!?  Just fall through to a target reset.
1048fcf3ce44SJohn Forte 		 */
1049fcf3ce44SJohn Forte 	case RESET_TARGET:
1050fcf3ce44SJohn Forte 		/* reset attempt will block until attempt is complete */
1051fcf3ce44SJohn Forte 		rval = iscsi_handle_reset(isp, level, NULL);
1052fcf3ce44SJohn Forte 		break;
1053fcf3ce44SJohn Forte 	case RESET_ALL:
1054fcf3ce44SJohn Forte 	default:
1055fcf3ce44SJohn Forte 		break;
1056fcf3ce44SJohn Forte 	}
1057fcf3ce44SJohn Forte 
1058fcf3ce44SJohn Forte 	return (ISCSI_SUCCESS(rval) ? 1 : 0);
1059fcf3ce44SJohn Forte }
1060fcf3ce44SJohn Forte 
1061fcf3ce44SJohn Forte /*
1062fcf3ce44SJohn Forte  * iscsi_tran_getcap - Get target/lun capabilities.
1063fcf3ce44SJohn Forte  */
1064fcf3ce44SJohn Forte static int
iscsi_tran_getcap(struct scsi_address * ap,char * cap,int whom)1065fcf3ce44SJohn Forte iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom)
1066fcf3ce44SJohn Forte {
1067fcf3ce44SJohn Forte 	return (iscsi_i_commoncap(ap, cap, 0, whom, 0));
1068fcf3ce44SJohn Forte }
1069fcf3ce44SJohn Forte 
1070fcf3ce44SJohn Forte 
1071fcf3ce44SJohn Forte /*
1072fcf3ce44SJohn Forte  * iscsi_tran_setcap - Set target/lun capabilities.
1073fcf3ce44SJohn Forte  */
1074fcf3ce44SJohn Forte /* ARGSUSED */
1075fcf3ce44SJohn Forte static int
iscsi_tran_setcap(struct scsi_address * ap,char * cap,int value,int whom)1076fcf3ce44SJohn Forte iscsi_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
1077fcf3ce44SJohn Forte {
10782b79d384Sbing zhao - Sun Microsystems - Beijing China 	return (iscsi_i_commoncap(ap, cap, value, whom, 1));
1079fcf3ce44SJohn Forte }
1080fcf3ce44SJohn Forte 
1081fcf3ce44SJohn Forte 
1082fcf3ce44SJohn Forte /*
1083fcf3ce44SJohn Forte  * iscsi_tran_destroy_pkt - Clean up packet
1084fcf3ce44SJohn Forte  */
1085fcf3ce44SJohn Forte static void
iscsi_tran_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)1086fcf3ce44SJohn Forte iscsi_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1087fcf3ce44SJohn Forte {
1088fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
1089fcf3ce44SJohn Forte 
1090fcf3ce44SJohn Forte 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
1091fcf3ce44SJohn Forte 
1092fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
1093fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_sig == ISCSI_SIG_CMD);
1094fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_state == ISCSI_CMD_STATE_FREE);
1095fcf3ce44SJohn Forte 
1096*61dfa509SRick McNeal 	iscsi_lun_rele(icmdp->cmd_lun);
1097fcf3ce44SJohn Forte 	mutex_destroy(&icmdp->cmd_mutex);
1098fcf3ce44SJohn Forte 	cv_destroy(&icmdp->cmd_completion);
1099fcf3ce44SJohn Forte 	scsi_hba_pkt_free(ap, pkt);
1100fcf3ce44SJohn Forte }
1101fcf3ce44SJohn Forte 
1102fcf3ce44SJohn Forte /*
1103fcf3ce44SJohn Forte  * iscsi_tran_dmafree - This is a software driver, NO DMA
1104fcf3ce44SJohn Forte  */
1105fcf3ce44SJohn Forte /* ARGSUSED */
1106fcf3ce44SJohn Forte static void
iscsi_tran_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)1107fcf3ce44SJohn Forte iscsi_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
1108fcf3ce44SJohn Forte {
1109fcf3ce44SJohn Forte 	/*
1110fcf3ce44SJohn Forte 	 * The iSCSI interface doesn't deal with DMA
1111fcf3ce44SJohn Forte 	 */
1112fcf3ce44SJohn Forte }
1113fcf3ce44SJohn Forte 
1114fcf3ce44SJohn Forte /*
1115fcf3ce44SJohn Forte  * iscsi_tran_sync_pkt - This is a software driver, NO DMA
1116fcf3ce44SJohn Forte  */
1117fcf3ce44SJohn Forte /* ARGSUSED */
1118fcf3ce44SJohn Forte static void
iscsi_tran_sync_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)1119fcf3ce44SJohn Forte iscsi_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1120fcf3ce44SJohn Forte {
1121fcf3ce44SJohn Forte 	/*
1122fcf3ce44SJohn Forte 	 * The iSCSI interface doesn't deal with DMA
1123fcf3ce44SJohn Forte 	 */
1124fcf3ce44SJohn Forte }
1125fcf3ce44SJohn Forte 
1126fcf3ce44SJohn Forte /*
1127fcf3ce44SJohn Forte  * iscsi_tran_reset_notify - We don't support BUS_RESET so there
1128fcf3ce44SJohn Forte  * is no point in support callback.
1129fcf3ce44SJohn Forte  */
1130fcf3ce44SJohn Forte /* ARGSUSED */
1131fcf3ce44SJohn Forte static int
iscsi_tran_reset_notify(struct scsi_address * ap,int flag,void (* callback)(caddr_t),caddr_t arg)1132fcf3ce44SJohn Forte iscsi_tran_reset_notify(struct scsi_address *ap, int flag,
1133fcf3ce44SJohn Forte     void (*callback) (caddr_t), caddr_t arg)
1134fcf3ce44SJohn Forte {
1135fcf3ce44SJohn Forte 
1136fcf3ce44SJohn Forte 	/*
1137fcf3ce44SJohn Forte 	 * We never do BUS_RESETS so allowing this call
1138fcf3ce44SJohn Forte 	 * back to register has no point?
1139fcf3ce44SJohn Forte 	 */
1140fcf3ce44SJohn Forte 	return (DDI_SUCCESS);
1141fcf3ce44SJohn Forte }
1142fcf3ce44SJohn Forte 
1143fcf3ce44SJohn Forte 
1144fcf3ce44SJohn Forte /*
1145fcf3ce44SJohn Forte  * iscsi_tran_bus_config - on demand device configuration
1146fcf3ce44SJohn Forte  *
1147fcf3ce44SJohn Forte  * iscsi_tran_bus_config is called by the NDI layer at the completion
1148fcf3ce44SJohn Forte  * of a dev_node creation.  There are two primary cases defined in this
1149fcf3ce44SJohn Forte  * function.  The first is BUS_CONFIG_ALL.  In this case the NDI is trying
1150fcf3ce44SJohn Forte  * to identify that targets/luns are available configured at that point
1151fcf3ce44SJohn Forte  * in time.  It is safe to just complete the process succcessfully.  The
1152fcf3ce44SJohn Forte  * second case is a new case that was defined in S10 for devfs.  BUS_CONFIG_ONE
1153fcf3ce44SJohn Forte  * this is to help driver the top down discovery instead of bottom up.  If
1154fcf3ce44SJohn Forte  * we receive a BUS_CONFIG_ONE we should check to see if the <addr> exists
1155fcf3ce44SJohn Forte  * if so complete successfull processing.  Otherwise we should call the
1156fcf3ce44SJohn Forte  * deamon and see if we can plumb the <addr>.  If it is possible to plumb the
1157fcf3ce44SJohn Forte  * <addr> block until plumbing is complete.  In both cases of being able to
1158fcf3ce44SJohn Forte  * plumb <addr> or not continue with successfull processing.
1159fcf3ce44SJohn Forte  */
1160fcf3ce44SJohn Forte static int
iscsi_tran_bus_config(dev_info_t * parent,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** childp)1161fcf3ce44SJohn Forte iscsi_tran_bus_config(dev_info_t *parent, uint_t flags,
1162fcf3ce44SJohn Forte     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
1163fcf3ce44SJohn Forte {
1164fcf3ce44SJohn Forte 	int		rval	= NDI_SUCCESS;
1165fcf3ce44SJohn Forte 	iscsi_hba_t	*ihp	= NULL;
1166fcf3ce44SJohn Forte 	int		iflags	= flags;
1167fcf3ce44SJohn Forte 	char		*name	= NULL;
1168fcf3ce44SJohn Forte 	char		*ptr	= NULL;
11694246c8e9SJack Meng 	boolean_t	config_root = B_FALSE;
1170fcf3ce44SJohn Forte 
1171fcf3ce44SJohn Forte 	/* get reference to soft state */
1172fcf3ce44SJohn Forte 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
1173fcf3ce44SJohn Forte 	    ddi_get_instance(parent));
1174fcf3ce44SJohn Forte 	if (ihp == NULL) {
1175fcf3ce44SJohn Forte 		return (NDI_FAILURE);
1176fcf3ce44SJohn Forte 	}
1177fcf3ce44SJohn Forte 
11784246c8e9SJack Meng 	iscsi_check_miniroot(ihp);
11794246c8e9SJack Meng 	if ((modrootloaded == 0) && (iscsiboot_prop != NULL)) {
11804246c8e9SJack Meng 		config_root = B_TRUE;
11814246c8e9SJack Meng 	}
11824246c8e9SJack Meng 
11834246c8e9SJack Meng 	if (config_root == B_FALSE) {
11844246c8e9SJack Meng 		if (iscsi_client_request_service(ihp) == B_FALSE) {
11854246c8e9SJack Meng 			return (NDI_FAILURE);
11864246c8e9SJack Meng 		}
11874246c8e9SJack Meng 	}
11884246c8e9SJack Meng 
1189fcf3ce44SJohn Forte 	/* lock so only one config operation occrs */
1190fcf3ce44SJohn Forte 	sema_p(&iscsid_config_semaphore);
1191fcf3ce44SJohn Forte 
1192fcf3ce44SJohn Forte 	switch (op) {
1193fcf3ce44SJohn Forte 	case BUS_CONFIG_ONE:
1194fcf3ce44SJohn Forte 		/* parse target name out of name given */
1195fcf3ce44SJohn Forte 		if ((ptr = strchr((char *)arg, '@')) == NULL) {
1196fcf3ce44SJohn Forte 			rval = NDI_FAILURE;
1197fcf3ce44SJohn Forte 			break;
1198fcf3ce44SJohn Forte 		}
1199fcf3ce44SJohn Forte 		ptr++;		/* move past '@' */
1200fcf3ce44SJohn Forte 		name = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
1201fcf3ce44SJohn Forte 		(void) strncpy(name, ptr, MAX_GET_NAME_SIZE);
1202fcf3ce44SJohn Forte 		/* We need to strip the LUN */
1203fcf3ce44SJohn Forte 		if ((ptr = strchr(name, ',')) == NULL) {
1204fcf3ce44SJohn Forte 			rval = NDI_FAILURE;
1205fcf3ce44SJohn Forte 			kmem_free(name, MAX_GET_NAME_SIZE);
1206fcf3ce44SJohn Forte 			name = NULL;
1207fcf3ce44SJohn Forte 			break;
1208fcf3ce44SJohn Forte 		}
1209fcf3ce44SJohn Forte 		/* We also need to strip the 4 bytes of hex TPGT */
1210fcf3ce44SJohn Forte 		ptr -= 4;
1211fcf3ce44SJohn Forte 		if (ptr <= name) {
1212fcf3ce44SJohn Forte 			rval = NDI_FAILURE;
1213fcf3ce44SJohn Forte 			kmem_free(name, MAX_GET_NAME_SIZE);
1214fcf3ce44SJohn Forte 			name = NULL;
1215fcf3ce44SJohn Forte 			break;
1216fcf3ce44SJohn Forte 		}
1217fcf3ce44SJohn Forte 		*ptr = '\0';		/* NULL terminate */
1218fcf3ce44SJohn Forte 
1219fcf3ce44SJohn Forte 		/* translate name back to original iSCSI name */
1220fcf3ce44SJohn Forte 		iscsi_get_name_to_iqn(name, MAX_GET_NAME_SIZE);
1221fcf3ce44SJohn Forte 
1222fcf3ce44SJohn Forte 		/* configure target, skip 4 byte ISID */
1223fcf3ce44SJohn Forte 		iscsid_config_one(ihp, (name+4), B_TRUE);
1224fcf3ce44SJohn Forte 
1225fcf3ce44SJohn Forte 		kmem_free(name, MAX_GET_NAME_SIZE);
1226fcf3ce44SJohn Forte 		name = NULL;
1227fcf3ce44SJohn Forte 
1228fcf3ce44SJohn Forte 		/*
1229fcf3ce44SJohn Forte 		 * DDI group instructed us to use this flag.
1230fcf3ce44SJohn Forte 		 */
1231fcf3ce44SJohn Forte 		iflags |= NDI_MDI_FALLBACK;
1232fcf3ce44SJohn Forte 		break;
1233fcf3ce44SJohn Forte 	case BUS_CONFIG_DRIVER:
1234fcf3ce44SJohn Forte 		/* FALLTHRU */
1235fcf3ce44SJohn Forte 	case BUS_CONFIG_ALL:
1236fcf3ce44SJohn Forte 		iscsid_config_all(ihp, B_TRUE);
1237fcf3ce44SJohn Forte 		break;
1238fcf3ce44SJohn Forte 	default:
1239fcf3ce44SJohn Forte 		rval = NDI_FAILURE;
1240fcf3ce44SJohn Forte 		break;
1241fcf3ce44SJohn Forte 	}
1242fcf3ce44SJohn Forte 
1243fcf3ce44SJohn Forte 	if (rval == NDI_SUCCESS) {
1244fcf3ce44SJohn Forte 		rval = ndi_busop_bus_config(parent, iflags,
1245fcf3ce44SJohn Forte 		    op, arg, childp, 0);
1246fcf3ce44SJohn Forte 	}
1247fcf3ce44SJohn Forte 	sema_v(&iscsid_config_semaphore);
1248fcf3ce44SJohn Forte 
12494246c8e9SJack Meng 	if (config_root == B_FALSE) {
12504246c8e9SJack Meng 		iscsi_client_release_service(ihp);
12514246c8e9SJack Meng 	}
12524246c8e9SJack Meng 
1253fcf3ce44SJohn Forte 	return (rval);
1254fcf3ce44SJohn Forte }
1255fcf3ce44SJohn Forte 
1256fcf3ce44SJohn Forte /*
1257fcf3ce44SJohn Forte  * iscsi_tran_bus_unconfig - on demand device unconfiguration
1258fcf3ce44SJohn Forte  *
1259fcf3ce44SJohn Forte  * Called by the os framework under low resource situations.
1260fcf3ce44SJohn Forte  * It will attempt to unload our minor nodes (logical units
1261fcf3ce44SJohn Forte  * ndi/mdi nodes).
1262fcf3ce44SJohn Forte  */
1263fcf3ce44SJohn Forte static int
iscsi_tran_bus_unconfig(dev_info_t * parent,uint_t flag,ddi_bus_config_op_t op,void * arg)1264fcf3ce44SJohn Forte iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flag,
1265fcf3ce44SJohn Forte     ddi_bus_config_op_t op, void *arg)
1266fcf3ce44SJohn Forte {
12674246c8e9SJack Meng 	int		rval = NDI_SUCCESS;
12684246c8e9SJack Meng 	iscsi_hba_t	*ihp = NULL;
12694246c8e9SJack Meng 
12704246c8e9SJack Meng 	/* get reference to soft state */
12714246c8e9SJack Meng 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
12724246c8e9SJack Meng 	    ddi_get_instance(parent));
12734246c8e9SJack Meng 	if (ihp == NULL) {
12744246c8e9SJack Meng 		return (NDI_FAILURE);
12754246c8e9SJack Meng 	}
12764246c8e9SJack Meng 
12774246c8e9SJack Meng 	if (iscsi_client_request_service(ihp) == B_FALSE) {
127808dcd69cSJack Meng 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
127908dcd69cSJack Meng 		if (ihp->hba_sess_list != NULL) {
128008dcd69cSJack Meng 			rval = NDI_FAILURE;
128108dcd69cSJack Meng 		}
128208dcd69cSJack Meng 		rw_exit(&ihp->hba_sess_list_rwlock);
128308dcd69cSJack Meng 		return (rval);
12844246c8e9SJack Meng 	}
12854246c8e9SJack Meng 
12864246c8e9SJack Meng 	rval = ndi_busop_bus_unconfig(parent, flag, op, arg);
12874246c8e9SJack Meng 
12884246c8e9SJack Meng 	iscsi_client_release_service(ihp);
12894246c8e9SJack Meng 
12904246c8e9SJack Meng 	return (rval);
1291fcf3ce44SJohn Forte }
1292fcf3ce44SJohn Forte 
1293fcf3ce44SJohn Forte 
1294fcf3ce44SJohn Forte /*
1295fcf3ce44SJohn Forte  * iscsi_tran_get_name - create private /devices name for LUN
1296fcf3ce44SJohn Forte  *
1297fcf3ce44SJohn Forte  * This creates the <addr> in /devices/iscsi/<driver>@<addr>
1298fcf3ce44SJohn Forte  * path.  For this <addr> we return the <session/target_name>,<lun num>
1299fcf3ce44SJohn Forte  * Where <target_name> is an <iqn/eui/...> as defined by the iSCSI
1300fcf3ce44SJohn Forte  * specification.  We do modify the name slightly so that it still
1301fcf3ce44SJohn Forte  * complies with the IEEE <addr> naming scheme.  This means that we
1302fcf3ce44SJohn Forte  * will substitute out the ':', '@', ... and other reserved characters
1303fcf3ce44SJohn Forte  * defined in the IEEE definition with '%<hex value of special char>'
1304fcf3ce44SJohn Forte  * This routine is indirectly called by iscsi_lun_create_xxx.  These
1305fcf3ce44SJohn Forte  * calling routines must prevent the session and lun lists from changing
1306fcf3ce44SJohn Forte  * during this routine.
1307fcf3ce44SJohn Forte  */
1308fcf3ce44SJohn Forte static int
iscsi_tran_get_name(struct scsi_device * sd,char * name,int len)1309fcf3ce44SJohn Forte iscsi_tran_get_name(struct scsi_device *sd, char *name, int len)
1310fcf3ce44SJohn Forte {
1311fcf3ce44SJohn Forte 	int		target		= 0;
1312fcf3ce44SJohn Forte 	int		lun		= 0;
1313fcf3ce44SJohn Forte 	iscsi_hba_t	*ihp		= NULL;
1314fcf3ce44SJohn Forte 	iscsi_sess_t	*isp		= NULL;
1315fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp		= NULL;
1316fcf3ce44SJohn Forte 	dev_info_t	*lun_dip	= NULL;
1317fcf3ce44SJohn Forte 
1318fcf3ce44SJohn Forte 	ASSERT(sd != NULL);
1319fcf3ce44SJohn Forte 	ASSERT(name != NULL);
1320fcf3ce44SJohn Forte 	lun_dip = sd->sd_dev;
1321fcf3ce44SJohn Forte 	ASSERT(lun_dip != NULL);
1322fcf3ce44SJohn Forte 
1323fcf3ce44SJohn Forte 	/* get reference to soft state */
1324fcf3ce44SJohn Forte 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
1325fcf3ce44SJohn Forte 	    ddi_get_instance(ddi_get_parent(lun_dip)));
1326fcf3ce44SJohn Forte 	if (ihp == NULL) {
1327fcf3ce44SJohn Forte 		name[0] = '\0';
1328fcf3ce44SJohn Forte 		return (0);
1329fcf3ce44SJohn Forte 	}
1330fcf3ce44SJohn Forte 
1331fcf3ce44SJohn Forte 	/* Get the target num */
1332fcf3ce44SJohn Forte 	target = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
1333fcf3ce44SJohn Forte 	    DDI_PROP_DONTPASS, TARGET_PROP, 0);
1334fcf3ce44SJohn Forte 
1335fcf3ce44SJohn Forte 	/* Get the target num */
1336fcf3ce44SJohn Forte 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
1337fcf3ce44SJohn Forte 	    DDI_PROP_DONTPASS, LUN_PROP, 0);
1338fcf3ce44SJohn Forte 
1339fcf3ce44SJohn Forte 	/*
1340fcf3ce44SJohn Forte 	 * Now we need to find our ilp by walking the lists
1341fcf3ce44SJohn Forte 	 * off the ihp and isp.
1342fcf3ce44SJohn Forte 	 */
1343fcf3ce44SJohn Forte 	/* See if we already created this session */
1344fcf3ce44SJohn Forte 
1345fcf3ce44SJohn Forte 	/* Walk the HBA's session list */
1346fcf3ce44SJohn Forte 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
1347fcf3ce44SJohn Forte 		/* compare target name as the unique identifier */
1348fcf3ce44SJohn Forte 		if (target == isp->sess_oid) {
1349fcf3ce44SJohn Forte 			/* found match */
1350fcf3ce44SJohn Forte 			break;
1351fcf3ce44SJohn Forte 		}
1352fcf3ce44SJohn Forte 	}
1353fcf3ce44SJohn Forte 
1354fcf3ce44SJohn Forte 	/* If we found matching session continue searching for tgt */
1355fcf3ce44SJohn Forte 	if (isp == NULL) {
1356fcf3ce44SJohn Forte 		/* sess not found */
1357fcf3ce44SJohn Forte 		name[0] = '\0';
1358fcf3ce44SJohn Forte 		return (0);
1359fcf3ce44SJohn Forte 	}
1360fcf3ce44SJohn Forte 
1361fcf3ce44SJohn Forte 	/*
1362fcf3ce44SJohn Forte 	 * Search for the matching iscsi lun structure.  We don't
1363fcf3ce44SJohn Forte 	 * need to hold the READER for the lun list at this point.
1364fcf3ce44SJohn Forte 	 * because the tran_get_name is being called from the online
1365fcf3ce44SJohn Forte 	 * function which is already holding a reader on the lun
1366fcf3ce44SJohn Forte 	 * list.
1367fcf3ce44SJohn Forte 	 */
1368fcf3ce44SJohn Forte 	for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
1369fcf3ce44SJohn Forte 		if (lun == ilp->lun_num) {
1370fcf3ce44SJohn Forte 			/* found match */
1371fcf3ce44SJohn Forte 			break;
1372fcf3ce44SJohn Forte 		}
1373fcf3ce44SJohn Forte 	}
1374fcf3ce44SJohn Forte 
1375fcf3ce44SJohn Forte 	if (ilp == NULL) {
1376fcf3ce44SJohn Forte 		/* tgt not found */
1377fcf3ce44SJohn Forte 		name[0] = '\0';
1378fcf3ce44SJohn Forte 		return (0);
1379fcf3ce44SJohn Forte 	}
1380fcf3ce44SJohn Forte 
1381fcf3ce44SJohn Forte 	/* Ensure enough space for lun_addr is available */
1382fcf3ce44SJohn Forte 	ASSERT(ilp->lun_addr != NULL);
1383fcf3ce44SJohn Forte 	if ((strlen(ilp->lun_addr) + 1) > len) {
1384fcf3ce44SJohn Forte 		return (0);
1385fcf3ce44SJohn Forte 	}
1386fcf3ce44SJohn Forte 
1387fcf3ce44SJohn Forte 	/* copy lun_addr name */
1388fcf3ce44SJohn Forte 	(void) strcpy(name, ilp->lun_addr);
1389fcf3ce44SJohn Forte 
1390fcf3ce44SJohn Forte 	/*
1391fcf3ce44SJohn Forte 	 * Based on IEEE-1275 we can't have any ':', ' ', '@', or '/'
1392fcf3ce44SJohn Forte 	 * characters in our naming.  So replace all those characters
1393fcf3ce44SJohn Forte 	 * with '-'
1394fcf3ce44SJohn Forte 	 */
1395fcf3ce44SJohn Forte 	iscsi_get_name_from_iqn(name, len);
1396fcf3ce44SJohn Forte 
1397fcf3ce44SJohn Forte 	return (1);
1398fcf3ce44SJohn Forte }
1399fcf3ce44SJohn Forte 
1400fcf3ce44SJohn Forte /*
1401fcf3ce44SJohn Forte  * iscsi_tran_get_bus_addr - This returns a human readable string
1402fcf3ce44SJohn Forte  * for the bus address.  Examining most other drivers fcp, etc.  They
1403fcf3ce44SJohn Forte  * all just return the same string as tran_get_name.  In our case
1404fcf3ce44SJohn Forte  * our tran get name is already some what usable so leave alone.
1405fcf3ce44SJohn Forte  */
1406fcf3ce44SJohn Forte static int
iscsi_tran_get_bus_addr(struct scsi_device * sd,char * name,int len)1407fcf3ce44SJohn Forte iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len)
1408fcf3ce44SJohn Forte {
1409fcf3ce44SJohn Forte 	return (iscsi_tran_get_name(sd, name, len));
1410fcf3ce44SJohn Forte }
1411fcf3ce44SJohn Forte 
1412fcf3ce44SJohn Forte 
1413fcf3ce44SJohn Forte /*
1414fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1415fcf3ce44SJohn Forte  * | End of scsi_tran routines					  |
1416fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1417fcf3ce44SJohn Forte  */
1418fcf3ce44SJohn Forte 
1419fcf3ce44SJohn Forte /*
1420fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1421fcf3ce44SJohn Forte  * | Start of cb_ops routines					   |
1422fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1423fcf3ce44SJohn Forte  */
1424fcf3ce44SJohn Forte 
1425fcf3ce44SJohn Forte /*
1426fcf3ce44SJohn Forte  * iscsi_open - Driver should be made IOCTL MT safe.  Otherwise
1427fcf3ce44SJohn Forte  * this function needs updated.
1428fcf3ce44SJohn Forte  */
1429fcf3ce44SJohn Forte /* ARGSUSED */
1430fcf3ce44SJohn Forte static int
iscsi_open(dev_t * devp,int flags,int otyp,cred_t * credp)1431fcf3ce44SJohn Forte iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1432fcf3ce44SJohn Forte {
1433fcf3ce44SJohn Forte 	return (0);
1434fcf3ce44SJohn Forte }
1435fcf3ce44SJohn Forte 
1436fcf3ce44SJohn Forte /*
1437fcf3ce44SJohn Forte  * iscsi_close -
1438fcf3ce44SJohn Forte  */
1439fcf3ce44SJohn Forte /* ARGSUSED */
1440fcf3ce44SJohn Forte static int
iscsi_close(dev_t dev,int flags,int otyp,cred_t * credp)1441fcf3ce44SJohn Forte iscsi_close(dev_t dev, int flags, int otyp, cred_t *credp)
1442fcf3ce44SJohn Forte {
1443fcf3ce44SJohn Forte 	return (0);
1444fcf3ce44SJohn Forte }
1445fcf3ce44SJohn Forte 
1446fcf3ce44SJohn Forte /*
1447fcf3ce44SJohn Forte  * iscsi_ioctl -
1448fcf3ce44SJohn Forte  */
1449fcf3ce44SJohn Forte /* ARGSUSED */
14504246c8e9SJack Meng static int
iscsi_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)1451fcf3ce44SJohn Forte iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1452fcf3ce44SJohn Forte     cred_t *credp, int *rvalp)
1453fcf3ce44SJohn Forte {
1454fcf3ce44SJohn Forte 	int			rtn		= 0;
1455fcf3ce44SJohn Forte 	int			instance	= 0;
1456fcf3ce44SJohn Forte 	int			list_space	= 0;
1457fcf3ce44SJohn Forte 	int			lun_sz		= 0;
1458fcf3ce44SJohn Forte 	int			did;
1459c21c6c26Syi zhang - Sun Microsystems - Beijing China 	int			retry;
1460fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp		= NULL;
1461fcf3ce44SJohn Forte 	iscsi_sess_t		*isp		= NULL;
1462fcf3ce44SJohn Forte 	iscsi_conn_t		*icp		= NULL;
1463fcf3ce44SJohn Forte 	iscsi_login_params_t	*params		= NULL;
1464fcf3ce44SJohn Forte 	iscsi_login_params_t	*tmpParams	= NULL;
1465fcf3ce44SJohn Forte 	uchar_t			*name		= NULL;
1466fcf3ce44SJohn Forte 	dev_info_t		*lun_dip	= NULL;
1467fcf3ce44SJohn Forte 
1468fcf3ce44SJohn Forte 	entry_t			    e;
1469fcf3ce44SJohn Forte 	iscsi_oid_t		    oid;
1470fcf3ce44SJohn Forte 	iscsi_property_t	    *ipp;
1471fcf3ce44SJohn Forte 	iscsi_static_property_t	    *ispp;
1472fcf3ce44SJohn Forte 	iscsi_param_get_t	    *ilg;
1473fcf3ce44SJohn Forte 	iscsi_param_set_t	    *ils;
1474fcf3ce44SJohn Forte 	iscsi_target_list_t	    idl, *idlp		= NULL;
1475fcf3ce44SJohn Forte 	iscsi_addr_list_t	    ial, *ialp		= NULL;
1476fcf3ce44SJohn Forte 	iscsi_chap_props_t	    *chap		= NULL;
1477fcf3ce44SJohn Forte 	iscsi_radius_props_t	    *radius		= NULL;
1478fcf3ce44SJohn Forte 	iscsi_auth_props_t	    *auth		= NULL;
1479fcf3ce44SJohn Forte 	iscsi_lun_list_t	    *ll, *llp		= NULL;
1480fcf3ce44SJohn Forte 	iscsi_lun_props_t	    *lun		= NULL;
14817284664aSJoshua M. Clulow 	iscsi_lun_t		    *ilp		= NULL;
1482fcf3ce44SJohn Forte 	iSCSIDiscoveryMethod_t	    method;
1483fcf3ce44SJohn Forte 	iSCSIDiscoveryProperties_t  discovery_props;
1484fcf3ce44SJohn Forte 	iscsi_uscsi_t		    iu;
1485fcf3ce44SJohn Forte 	iscsi_uscsi_t		    iu_caller;
1486fcf3ce44SJohn Forte #ifdef _MULTI_DATAMODEL
1487fcf3ce44SJohn Forte 	/* For use when a 32 bit app makes a call into a 64 bit ioctl */
1488fcf3ce44SJohn Forte 	iscsi_uscsi32_t		    iu32_caller;
1489fcf3ce44SJohn Forte 	model_t			    model;
1490fcf3ce44SJohn Forte #endif /* _MULTI_DATAMODEL */
1491fcf3ce44SJohn Forte 	void			    *void_p;
1492fcf3ce44SJohn Forte 	iscsi_sendtgts_list_t	*stl_hdr;
1493fcf3ce44SJohn Forte 	iscsi_sendtgts_list_t	*istl;
1494fcf3ce44SJohn Forte 	int			stl_sz;
1495fcf3ce44SJohn Forte 	iscsi_target_entry_t	*target;
1496fcf3ce44SJohn Forte 	uint32_t		old_oid;
1497fcf3ce44SJohn Forte 	uint32_t		target_oid;
1498fcf3ce44SJohn Forte 	iscsi_targetparam_entry_t *curr_entry;
1499fcf3ce44SJohn Forte 	char			*initiator_node_name;
1500fcf3ce44SJohn Forte 	char			*initiator_node_alias;
1501fcf3ce44SJohn Forte 	isns_portal_group_list_t    *pg_list = NULL;
1502fcf3ce44SJohn Forte 	isns_server_portal_group_list_t    *server_pg_list_hdr = NULL;
1503fcf3ce44SJohn Forte 	isns_server_portal_group_list_t    *server_pg_list = NULL;
1504fcf3ce44SJohn Forte 	int			pg_list_sz, pg_sz_copy_out, server_pg_list_sz;
1505fcf3ce44SJohn Forte 	iscsi_config_sess_t	*ics;
1506fcf3ce44SJohn Forte 	int			size;
1507fcf3ce44SJohn Forte 	boolean_t		rval;
1508fcf3ce44SJohn Forte 	char			init_port_name[MAX_NAME_PROP_SIZE];
1509fcf3ce44SJohn Forte 	iscsi_sockaddr_t	addr_dsc;
15106cefaae1SJack Meng 	iscsi_boot_property_t	*bootProp;
1511b4243054SSheng-Liang Eric Zhang 	boolean_t		discovered = B_TRUE;
1512aff4bce5Syi zhang - Sun Microsystems - Beijing China 	iscsi_tunable_object_t	*tpsg;
1513aff4bce5Syi zhang - Sun Microsystems - Beijing China 	iscsi_tunable_object_t	*tpss;
1514904e51f6SJack Meng 	iscsi_reen_t	*reenum;
1515fcf3ce44SJohn Forte 
1516fcf3ce44SJohn Forte 	instance = getminor(dev);
1517fcf3ce44SJohn Forte 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, instance);
1518fcf3ce44SJohn Forte 	if (ihp == NULL)
1519fcf3ce44SJohn Forte 		return (EFAULT);
1520fcf3ce44SJohn Forte 
15214246c8e9SJack Meng 	iscsi_check_miniroot(ihp);
15224246c8e9SJack Meng 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
15234246c8e9SJack Meng 	    (cmd != ISCSI_SMF_GET)) {
15244246c8e9SJack Meng 		/* other cmd needs to acquire the service */
15254246c8e9SJack Meng 		if (iscsi_client_request_service(ihp) == B_FALSE) {
15264246c8e9SJack Meng 			return (EFAULT);
15274246c8e9SJack Meng 		}
15284246c8e9SJack Meng 	}
15294246c8e9SJack Meng 
1530fcf3ce44SJohn Forte 	switch (cmd) {
1531fcf3ce44SJohn Forte 	/*
1532fcf3ce44SJohn Forte 	 * ISCSI_CREATE_OID - Create a Object IDentifier for a TargetName
1533fcf3ce44SJohn Forte 	 */
1534fcf3ce44SJohn Forte 	case ISCSI_CREATE_OID:
1535fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &oid, sizeof (oid), mode)) {
1536fcf3ce44SJohn Forte 			rtn = EFAULT;
1537fcf3ce44SJohn Forte 			break;
1538fcf3ce44SJohn Forte 		}
1539fcf3ce44SJohn Forte 		if (oid.o_vers != ISCSI_INTERFACE_VERSION) {
1540fcf3ce44SJohn Forte 			rtn = EINVAL;
1541fcf3ce44SJohn Forte 			break;
1542fcf3ce44SJohn Forte 		}
1543fcf3ce44SJohn Forte 
1544fcf3ce44SJohn Forte 		/* Set the target that this session is associated with */
1545fcf3ce44SJohn Forte 		oid.o_oid = iscsi_targetparam_get_oid(oid.o_name);
1546fcf3ce44SJohn Forte 
1547fcf3ce44SJohn Forte 		if (ddi_copyout(&oid, (caddr_t)arg, sizeof (oid), mode)) {
1548fcf3ce44SJohn Forte 			rtn = EFAULT;
1549fcf3ce44SJohn Forte 			break;
1550fcf3ce44SJohn Forte 		}
1551fcf3ce44SJohn Forte 		break;
1552fcf3ce44SJohn Forte 	/*
1553fcf3ce44SJohn Forte 	 * ISCSI_PARAM_GET - Get param for specified
1554fcf3ce44SJohn Forte 	 * connection/session.
1555fcf3ce44SJohn Forte 	 */
1556fcf3ce44SJohn Forte 	case ISCSI_PARAM_GET:
1557fcf3ce44SJohn Forte 		/* copyin user args */
1558fcf3ce44SJohn Forte 		ilg = (iscsi_param_get_t *)kmem_alloc(sizeof (*ilg), KM_SLEEP);
1559fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ilg, sizeof (*ilg), mode)) {
1560fcf3ce44SJohn Forte 			rtn = EFAULT;
1561fcf3ce44SJohn Forte 			kmem_free(ilg, sizeof (*ilg));
1562fcf3ce44SJohn Forte 			break;
1563fcf3ce44SJohn Forte 		}
1564fcf3ce44SJohn Forte 
1565fcf3ce44SJohn Forte 		if (ilg->g_vers != ISCSI_INTERFACE_VERSION) {
1566fcf3ce44SJohn Forte 			rtn = EINVAL;
1567fcf3ce44SJohn Forte 			kmem_free(ilg, sizeof (*ilg));
1568fcf3ce44SJohn Forte 			break;
1569fcf3ce44SJohn Forte 		}
1570fcf3ce44SJohn Forte 
1571fcf3ce44SJohn Forte 		/* handle special case for Initiator name */
1572fcf3ce44SJohn Forte 		if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_NAME) {
1573fcf3ce44SJohn Forte 			(void) strlcpy((char *)ilg->g_value.v_name,
1574fcf3ce44SJohn Forte 			    (char *)ihp->hba_name, ISCSI_MAX_NAME_LEN);
1575fcf3ce44SJohn Forte 		} else if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_ALIAS) {
1576fcf3ce44SJohn Forte 			if (ihp->hba_alias_length == 0) {
1577fcf3ce44SJohn Forte 				rtn = EINVAL;
1578fcf3ce44SJohn Forte 			} else {
1579fcf3ce44SJohn Forte 				(void) strlcpy((char *)ilg->g_value.v_name,
1580fcf3ce44SJohn Forte 				    (char *)ihp->hba_alias, ISCSI_MAX_NAME_LEN);
1581fcf3ce44SJohn Forte 			}
1582fcf3ce44SJohn Forte 		} else {
1583fcf3ce44SJohn Forte 			/* To describe the validity of the requested param */
1584fcf3ce44SJohn Forte 			boolean_t valid_flag = B_TRUE;
1585fcf3ce44SJohn Forte 
1586fcf3ce44SJohn Forte 			name = NULL;
1587fcf3ce44SJohn Forte 
1588fcf3ce44SJohn Forte 			/*
1589fcf3ce44SJohn Forte 			 * switch login based if looking for initiator
1590fcf3ce44SJohn Forte 			 * params
1591fcf3ce44SJohn Forte 			 */
1592fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
1593fcf3ce44SJohn Forte 			if (ilg->g_oid == ihp->hba_oid) {
1594fcf3ce44SJohn Forte 				/* initiator */
1595fcf3ce44SJohn Forte 				params = &ihp->hba_params;
1596fcf3ce44SJohn Forte 				name = ihp->hba_name;
1597fcf3ce44SJohn Forte 				if (iscsi_get_persisted_param(name,
1598fcf3ce44SJohn Forte 				    ilg, params) != 0) {
1599fcf3ce44SJohn Forte 					valid_flag = B_FALSE;
1600fcf3ce44SJohn Forte 				}
1601fcf3ce44SJohn Forte 			} else {
1602fcf3ce44SJohn Forte 				/*
1603fcf3ce44SJohn Forte 				 * If the oid does represent a session check
1604fcf3ce44SJohn Forte 				 * to see if it is a target oid.  If so,
1605fcf3ce44SJohn Forte 				 * return the target's associated session.
1606fcf3ce44SJohn Forte 				 */
1607fcf3ce44SJohn Forte 				rtn = iscsi_sess_get(ilg->g_oid, ihp, &isp);
1608fcf3ce44SJohn Forte 				if (rtn != 0) {
1609fcf3ce44SJohn Forte 					rtn = iscsi_sess_get_by_target(
1610fcf3ce44SJohn Forte 					    ilg->g_oid, ihp, &isp);
1611fcf3ce44SJohn Forte 				}
1612fcf3ce44SJohn Forte 
1613fcf3ce44SJohn Forte 				/*
1614fcf3ce44SJohn Forte 				 * If rtn is zero then we have found an
1615fcf3ce44SJohn Forte 				 * existing session.  Use the session name to
1616fcf3ce44SJohn Forte 				 * do param lookup.  If rtn is non-zero then
1617fcf3ce44SJohn Forte 				 * create a targetparam object and use its name
1618fcf3ce44SJohn Forte 				 * for param lookup.
1619fcf3ce44SJohn Forte 				 */
1620fcf3ce44SJohn Forte 				if (rtn == 0) {
1621fcf3ce44SJohn Forte 					name = isp->sess_name;
1622fcf3ce44SJohn Forte 					params = &isp->sess_params;
1623fcf3ce44SJohn Forte 				} else {
1624fcf3ce44SJohn Forte 					name =
1625fcf3ce44SJohn Forte 					    iscsi_targetparam_get_name(
1626fcf3ce44SJohn Forte 					    ilg->g_oid);
1627fcf3ce44SJohn Forte 					if (ilg->g_param_type ==
1628fcf3ce44SJohn Forte 					    ISCSI_SESS_PARAM) {
1629fcf3ce44SJohn Forte 						tmpParams =
1630fcf3ce44SJohn Forte 						    (iscsi_login_params_t *)
1631fcf3ce44SJohn Forte 						    kmem_alloc(
1632fcf3ce44SJohn Forte 						    sizeof (*tmpParams),
1633fcf3ce44SJohn Forte 						    KM_SLEEP);
1634fcf3ce44SJohn Forte 						params = tmpParams;
1635fcf3ce44SJohn Forte 					}
1636fcf3ce44SJohn Forte 					rtn = 0;
1637fcf3ce44SJohn Forte 				}
1638fcf3ce44SJohn Forte 
1639fcf3ce44SJohn Forte 				if (name == NULL) {
1640fcf3ce44SJohn Forte 					rw_exit(
1641fcf3ce44SJohn Forte 					    &ihp->hba_sess_list_rwlock);
1642fcf3ce44SJohn Forte 					rtn = EFAULT;
1643fcf3ce44SJohn Forte 					kmem_free(ilg, sizeof (*ilg));
1644fcf3ce44SJohn Forte 					if (tmpParams != NULL)
1645fcf3ce44SJohn Forte 						kmem_free(tmpParams,
1646fcf3ce44SJohn Forte 						    sizeof (*tmpParams));
1647fcf3ce44SJohn Forte 
1648fcf3ce44SJohn Forte 					break;
1649fcf3ce44SJohn Forte 				}
1650fcf3ce44SJohn Forte 
1651fcf3ce44SJohn Forte 				if (ilg->g_param_type == ISCSI_SESS_PARAM) {
1652fcf3ce44SJohn Forte 					/* session */
1653fcf3ce44SJohn Forte 					/*
1654fcf3ce44SJohn Forte 					 * Update sess_params with the
1655fcf3ce44SJohn Forte 					 * latest params from the
1656fcf3ce44SJohn Forte 					 * persistent store.
1657fcf3ce44SJohn Forte 					 */
1658fcf3ce44SJohn Forte 					if (iscsi_get_persisted_param(name,
1659fcf3ce44SJohn Forte 					    ilg, params) != 0) {
1660fcf3ce44SJohn Forte 						/*
1661fcf3ce44SJohn Forte 						 * If the parameter in
1662fcf3ce44SJohn Forte 						 * question is not
1663fcf3ce44SJohn Forte 						 * overriden, no effect
1664fcf3ce44SJohn Forte 						 * on existing session
1665fcf3ce44SJohn Forte 						 * parameters. However,
1666fcf3ce44SJohn Forte 						 * the parameter is
1667fcf3ce44SJohn Forte 						 * marked invalid
1668fcf3ce44SJohn Forte 						 * (from the standpoint
1669fcf3ce44SJohn Forte 						 * of whether it is
1670fcf3ce44SJohn Forte 						 * overriden).
1671fcf3ce44SJohn Forte 						 */
1672fcf3ce44SJohn Forte 						valid_flag = B_FALSE;
1673fcf3ce44SJohn Forte 					}
1674fcf3ce44SJohn Forte 				} else if (ilg->g_param_type ==
1675fcf3ce44SJohn Forte 				    ISCSI_CONN_PARAM && isp != NULL) {
1676fcf3ce44SJohn Forte 					/* connection */
1677fcf3ce44SJohn Forte 					rw_enter(&isp->sess_conn_list_rwlock,
1678fcf3ce44SJohn Forte 					    RW_READER);
1679fcf3ce44SJohn Forte 					/* Assuming 1 conn per sess. */
1680fcf3ce44SJohn Forte 					/*
1681fcf3ce44SJohn Forte 					 * MC/S - Need to be modified to
1682fcf3ce44SJohn Forte 					 * take g_conn_cid into account when
1683fcf3ce44SJohn Forte 					 * we go multi-connection.
1684fcf3ce44SJohn Forte 					 */
1685fcf3ce44SJohn Forte 					if ((isp->sess_conn_act != NULL) &&
1686fcf3ce44SJohn Forte 					    (isp->sess_conn_act->conn_state ==
1687fcf3ce44SJohn Forte 					    ISCSI_CONN_STATE_LOGGED_IN)) {
1688fcf3ce44SJohn Forte 						params = &(isp->
1689fcf3ce44SJohn Forte 						    sess_conn_act->
1690fcf3ce44SJohn Forte 						    conn_params);
1691fcf3ce44SJohn Forte 					} else {
1692fcf3ce44SJohn Forte 						valid_flag = B_FALSE;
1693fcf3ce44SJohn Forte 					}
1694fcf3ce44SJohn Forte 					rw_exit(&isp->sess_conn_list_rwlock);
1695fcf3ce44SJohn Forte 				}
1696fcf3ce44SJohn Forte 			}
1697fcf3ce44SJohn Forte 
1698fcf3ce44SJohn Forte 			/* make sure we have params to get info from */
1699fcf3ce44SJohn Forte 			if (params) {
1700fcf3ce44SJohn Forte 				rtn = iscsi_get_param(params, valid_flag, ilg);
1701fcf3ce44SJohn Forte 
1702fcf3ce44SJohn Forte 				/*
1703fcf3ce44SJohn Forte 				 * for target parameters, check if any
1704fcf3ce44SJohn Forte 				 * parameters were overridden at the initiator
1705fcf3ce44SJohn Forte 				 * level. If so, then change the default value
1706fcf3ce44SJohn Forte 				 * to the initiator's overridden value
1707fcf3ce44SJohn Forte 				 */
1708fcf3ce44SJohn Forte 				if ((rtn == 0) &&
1709fcf3ce44SJohn Forte 				    (ilg->g_oid != ihp->hba_oid)) {
1710fcf3ce44SJohn Forte 					iscsi_override_target_default(ihp,
1711fcf3ce44SJohn Forte 					    ilg);
1712fcf3ce44SJohn Forte 				}
1713fcf3ce44SJohn Forte 			}
1714fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
1715fcf3ce44SJohn Forte 		}
1716fcf3ce44SJohn Forte 
1717fcf3ce44SJohn Forte 		if (rtn == 0) {
1718fcf3ce44SJohn Forte 			rtn = ddi_copyout(ilg, (caddr_t)arg,
1719fcf3ce44SJohn Forte 			    sizeof (iscsi_param_get_t), mode);
1720fcf3ce44SJohn Forte 		}
1721fcf3ce44SJohn Forte 		kmem_free(ilg, sizeof (*ilg));
1722fcf3ce44SJohn Forte 		if (tmpParams != NULL)
1723fcf3ce44SJohn Forte 			kmem_free(tmpParams, sizeof (*tmpParams));
1724fcf3ce44SJohn Forte 		break;
1725fcf3ce44SJohn Forte 
1726fcf3ce44SJohn Forte 	/*
1727fcf3ce44SJohn Forte 	 * ISCSI_INIT_NODE_NAME_SET - Change the initiator-node name for
1728fcf3ce44SJohn Forte 	 * the specified connection/session.
1729fcf3ce44SJohn Forte 	 */
1730fcf3ce44SJohn Forte 	case ISCSI_INIT_NODE_NAME_SET:
1731fcf3ce44SJohn Forte 		/* copyin user args */
1732fcf3ce44SJohn Forte 		ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP);
1733fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) {
1734fcf3ce44SJohn Forte 			rtn = EFAULT;
1735fcf3ce44SJohn Forte 			kmem_free(ils, sizeof (*ils));
1736fcf3ce44SJohn Forte 			break;
1737fcf3ce44SJohn Forte 		}
1738fcf3ce44SJohn Forte 
1739fcf3ce44SJohn Forte 		if (ils->s_vers != ISCSI_INTERFACE_VERSION) {
1740fcf3ce44SJohn Forte 			rtn = EINVAL;
1741fcf3ce44SJohn Forte 			kmem_free(ils, sizeof (*ils));
1742fcf3ce44SJohn Forte 			break;
1743fcf3ce44SJohn Forte 		}
1744fcf3ce44SJohn Forte 
1745fcf3ce44SJohn Forte 		/* saving off the old initiator-node name */
1746fcf3ce44SJohn Forte 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
1747fcf3ce44SJohn Forte 		rval = persistent_initiator_name_get(initiator_node_name,
1748fcf3ce44SJohn Forte 		    ISCSI_MAX_NAME_LEN);
1749fcf3ce44SJohn Forte 
1750fcf3ce44SJohn Forte 		rtn = iscsi_set_params(ils, ihp, B_TRUE);
1751fcf3ce44SJohn Forte 		kmem_free(ils, sizeof (*ils));
1752fcf3ce44SJohn Forte 		if (rtn != 0) {
1753fcf3ce44SJohn Forte 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
17545279807dSJack Meng 			initiator_node_name = NULL;
17554246c8e9SJack Meng 			break;
1756fcf3ce44SJohn Forte 		}
1757fcf3ce44SJohn Forte 
1758fcf3ce44SJohn Forte 		(void) snprintf(init_port_name, MAX_NAME_PROP_SIZE,
1759fcf3ce44SJohn Forte 		    "%s,%02x%02x%02x%02x%02x%02x",
1760fcf3ce44SJohn Forte 		    (char *)ihp->hba_name, ihp->hba_isid[0],
1761fcf3ce44SJohn Forte 		    ihp->hba_isid[1], ihp->hba_isid[2],
1762fcf3ce44SJohn Forte 		    ihp->hba_isid[3], ihp->hba_isid[4],
1763fcf3ce44SJohn Forte 		    ihp->hba_isid[5]);
1764fcf3ce44SJohn Forte 
1765fcf3ce44SJohn Forte 		if (ddi_prop_update_string(DDI_DEV_T_NONE,
17664c06356bSdh 		    ihp->hba_dip, SCSI_ADDR_PROP_INITIATOR_PORT,
1767fcf3ce44SJohn Forte 		    init_port_name) != DDI_PROP_SUCCESS) {
1768fcf3ce44SJohn Forte 			cmn_err(CE_WARN, "iscsi_ioctl: Updating "
17694c06356bSdh 			    SCSI_ADDR_PROP_INITIATOR_PORT " property on iSCSI "
1770fcf3ce44SJohn Forte 			    "HBA(%s) with dip(%d) Failed",
1771fcf3ce44SJohn Forte 			    (char *)ihp->hba_name,
1772fcf3ce44SJohn Forte 			    ddi_get_instance(ihp->hba_dip));
1773fcf3ce44SJohn Forte 		}
1774fcf3ce44SJohn Forte 
1775fcf3ce44SJohn Forte 		/*
1776fcf3ce44SJohn Forte 		 * Deregister the old initiator-node name from the iSNS
1777fcf3ce44SJohn Forte 		 * server
1778fcf3ce44SJohn Forte 		 * Register the new initiator-node name with the iSNS server
1779fcf3ce44SJohn Forte 		 */
1780fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
1781fcf3ce44SJohn Forte 		if (method & iSCSIDiscoveryMethodISNS) {
1782fcf3ce44SJohn Forte 			if (rval == B_TRUE) {
1783fcf3ce44SJohn Forte 				if (strlen(initiator_node_name) > 0) {
1784fcf3ce44SJohn Forte 				/*
1785fcf3ce44SJohn Forte 				 * we will attempt to offline the targets.
1786fcf3ce44SJohn Forte 				 * if logouts fail, we will still continue
1787fcf3ce44SJohn Forte 				 */
1788fcf3ce44SJohn Forte #define	STRING_INNO "initiator-node name - Offline "
1789fcf3ce44SJohn Forte #define	STRING_FFOMD "failed for one or more devices"
1790fcf3ce44SJohn Forte 					if ((iscsid_del(
1791fcf3ce44SJohn Forte 					    ihp, NULL, method, NULL))
1792fcf3ce44SJohn Forte 					    != B_TRUE) {
1793fcf3ce44SJohn Forte 						cmn_err(CE_NOTE,
1794fcf3ce44SJohn Forte 						    "Attempting to change "
1795fcf3ce44SJohn Forte 						    STRING_INNO
1796fcf3ce44SJohn Forte 						    STRING_FFOMD);
1797fcf3ce44SJohn Forte 					}
1798fcf3ce44SJohn Forte 					(void) isns_dereg(ihp->hba_isid,
1799fcf3ce44SJohn Forte 					    (uint8_t *)initiator_node_name);
1800fcf3ce44SJohn Forte #undef STRING_INNO
1801fcf3ce44SJohn Forte #undef STRING_FFOMD
1802fcf3ce44SJohn Forte 				}
1803fcf3ce44SJohn Forte 			}
1804fcf3ce44SJohn Forte 			if (persistent_initiator_name_get(initiator_node_name,
1805fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
1806fcf3ce44SJohn Forte 				kmem_free(initiator_node_name,
1807fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN);
1808fcf3ce44SJohn Forte 				initiator_node_name = NULL;
1809fcf3ce44SJohn Forte 				rtn = EIO;
1810fcf3ce44SJohn Forte 				break;
1811fcf3ce44SJohn Forte 			}
1812fcf3ce44SJohn Forte 			if (strlen(initiator_node_name) == 0) {
1813fcf3ce44SJohn Forte 				kmem_free(initiator_node_name,
1814fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN);
1815fcf3ce44SJohn Forte 				initiator_node_name = NULL;
1816fcf3ce44SJohn Forte 				rtn = EIO;
1817fcf3ce44SJohn Forte 				break;
1818fcf3ce44SJohn Forte 			}
1819fcf3ce44SJohn Forte 
1820fcf3ce44SJohn Forte 			initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN,
1821fcf3ce44SJohn Forte 			    KM_SLEEP);
1822fcf3ce44SJohn Forte 			if (persistent_alias_name_get(initiator_node_alias,
1823fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
1824fcf3ce44SJohn Forte 				initiator_node_alias[0] = '\0';
1825fcf3ce44SJohn Forte 			}
1826fcf3ce44SJohn Forte 
1827fcf3ce44SJohn Forte 			(void) isns_reg(ihp->hba_isid,
1828fcf3ce44SJohn Forte 			    (uint8_t *)initiator_node_name,
1829fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN,
1830fcf3ce44SJohn Forte 			    (uint8_t *)initiator_node_alias,
1831fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN,
1832fcf3ce44SJohn Forte 			    ISNS_INITIATOR_NODE_TYPE,
1833fcf3ce44SJohn Forte 			    isns_scn_callback);
1834fcf3ce44SJohn Forte 			iscsid_do_isns_query(ihp);
1835fcf3ce44SJohn Forte 
1836fcf3ce44SJohn Forte 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
1837fcf3ce44SJohn Forte 			initiator_node_alias = NULL;
1838fcf3ce44SJohn Forte 		}
18395279807dSJack Meng 
18405279807dSJack Meng 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
18415279807dSJack Meng 		initiator_node_name = NULL;
1842fcf3ce44SJohn Forte 		break;
1843fcf3ce44SJohn Forte 
1844fcf3ce44SJohn Forte 	/*
1845fcf3ce44SJohn Forte 	 * ISCSI_PARAM_SET - Set param for specified connection/session.
1846fcf3ce44SJohn Forte 	 */
1847fcf3ce44SJohn Forte 	case ISCSI_PARAM_SET:
1848fcf3ce44SJohn Forte 		/* copyin user args */
1849fcf3ce44SJohn Forte 		ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP);
1850fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) {
1851fcf3ce44SJohn Forte 			rtn = EFAULT;
1852fcf3ce44SJohn Forte 			kmem_free(ils, sizeof (*ils));
1853fcf3ce44SJohn Forte 			break;
1854fcf3ce44SJohn Forte 		}
1855fcf3ce44SJohn Forte 
1856fcf3ce44SJohn Forte 		if (ils->s_vers != ISCSI_INTERFACE_VERSION) {
1857fcf3ce44SJohn Forte 			rtn = EINVAL;
1858fcf3ce44SJohn Forte 			kmem_free(ils, sizeof (*ils));
1859fcf3ce44SJohn Forte 			break;
1860fcf3ce44SJohn Forte 		}
1861fcf3ce44SJohn Forte 		rtn = iscsi_set_params(ils, ihp, B_TRUE);
18626cefaae1SJack Meng 		if (iscsiboot_prop) {
18636cefaae1SJack Meng 			if (iscsi_cmp_boot_sess_oid(ihp, ils->s_oid)) {
18646cefaae1SJack Meng 				/*
18656cefaae1SJack Meng 				 * found active session for this object
18666cefaae1SJack Meng 				 * or this is initiator's object
18676cefaae1SJack Meng 				 * with mpxio enabled
18686cefaae1SJack Meng 				 */
18696cefaae1SJack Meng 				if (!iscsi_reconfig_boot_sess(ihp)) {
18706cefaae1SJack Meng 					rtn = EINVAL;
18716cefaae1SJack Meng 					kmem_free(ils, sizeof (*ils));
18726cefaae1SJack Meng 					break;
18736cefaae1SJack Meng 				}
18746cefaae1SJack Meng 			}
18756cefaae1SJack Meng 		}
1876fcf3ce44SJohn Forte 		kmem_free(ils, sizeof (*ils));
1877fcf3ce44SJohn Forte 		break;
1878fcf3ce44SJohn Forte 
1879fcf3ce44SJohn Forte 	/*
1880fcf3ce44SJohn Forte 	 * ISCSI_TARGET_PARAM_CLEAR
1881fcf3ce44SJohn Forte 	 * - remove custom parameter settings for a target.
1882fcf3ce44SJohn Forte 	 */
1883fcf3ce44SJohn Forte 	case ISCSI_TARGET_PARAM_CLEAR:
1884fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
1885fcf3ce44SJohn Forte 			rtn = EFAULT;
1886fcf3ce44SJohn Forte 			break;
1887fcf3ce44SJohn Forte 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
1888fcf3ce44SJohn Forte 			rtn = EINVAL;
1889fcf3ce44SJohn Forte 			break;
1890fcf3ce44SJohn Forte 		}
1891fcf3ce44SJohn Forte 
1892fcf3ce44SJohn Forte 		if ((e.e_oid != ihp->hba_oid) &&
1893fcf3ce44SJohn Forte 		    (e.e_oid != ISCSI_OID_NOTSET)) {
18948a29b80eSyi zhang - Sun Microsystems - Beijing China 			boolean_t rval1, rval2, rval3;
1895fcf3ce44SJohn Forte 			uchar_t	    *t_name;
1896fcf3ce44SJohn Forte 			iscsi_sess_t *t_isp;
18978a29b80eSyi zhang - Sun Microsystems - Beijing China 			boolean_t    t_rtn = B_TRUE;
18988a29b80eSyi zhang - Sun Microsystems - Beijing China 			persistent_param_t  t_param;
18998a29b80eSyi zhang - Sun Microsystems - Beijing China 			iscsi_config_sess_t t_ics;
19008a29b80eSyi zhang - Sun Microsystems - Beijing China 			persistent_tunable_param_t t_tpsg;
1901fcf3ce44SJohn Forte 
1902fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
1903fcf3ce44SJohn Forte 			/*
1904fcf3ce44SJohn Forte 			 * If the oid does represent a session check to see
1905fcf3ce44SJohn Forte 			 * if it is a target oid.  If so, return the target's
1906fcf3ce44SJohn Forte 			 * associated session.
1907fcf3ce44SJohn Forte 			 */
1908fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(e.e_oid, ihp, &isp);
1909fcf3ce44SJohn Forte 			if (rtn != 0) {
1910fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(e.e_oid, ihp,
1911fcf3ce44SJohn Forte 				    &isp);
1912fcf3ce44SJohn Forte 			}
1913fcf3ce44SJohn Forte 
1914fcf3ce44SJohn Forte 			/*
1915fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
1916fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
1917fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
1918fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
1919fcf3ce44SJohn Forte 			 * for param lookup.
1920fcf3ce44SJohn Forte 			 */
1921fcf3ce44SJohn Forte 			if (rtn == 0) {
1922fcf3ce44SJohn Forte 				t_name = isp->sess_name;
1923fcf3ce44SJohn Forte 			} else {
1924fcf3ce44SJohn Forte 				t_name = iscsi_targetparam_get_name(e.e_oid);
1925fcf3ce44SJohn Forte 				rtn = 0;
1926fcf3ce44SJohn Forte 			}
1927fcf3ce44SJohn Forte 
1928fcf3ce44SJohn Forte 			if (t_name == NULL) {
1929fcf3ce44SJohn Forte 				rw_exit(&ihp->hba_sess_list_rwlock);
1930fcf3ce44SJohn Forte 				rtn = EFAULT;
1931fcf3ce44SJohn Forte 				break;
1932fcf3ce44SJohn Forte 			}
1933fcf3ce44SJohn Forte 
1934fcf3ce44SJohn Forte 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
1935fcf3ce44SJohn Forte 			(void) strncpy((char *)name, (char *)t_name,
1936fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN);
1937fcf3ce44SJohn Forte 
19388a29b80eSyi zhang - Sun Microsystems - Beijing China 			t_ics.ics_in = 1;
19398a29b80eSyi zhang - Sun Microsystems - Beijing China 			rval1 = persistent_param_get((char *)name, &t_param);
19408a29b80eSyi zhang - Sun Microsystems - Beijing China 			rval2 = persistent_get_config_session((char *)name,
19418a29b80eSyi zhang - Sun Microsystems - Beijing China 			    &t_ics);
19428a29b80eSyi zhang - Sun Microsystems - Beijing China 			rval3 = persistent_get_tunable_param((char *)name,
19438a29b80eSyi zhang - Sun Microsystems - Beijing China 			    &t_tpsg);
19448a29b80eSyi zhang - Sun Microsystems - Beijing China 
19458a29b80eSyi zhang - Sun Microsystems - Beijing China 			if ((rval1 == B_FALSE) && (rval2 == B_FALSE) &&
19468a29b80eSyi zhang - Sun Microsystems - Beijing China 			    (rval3 == B_FALSE)) {
19478a29b80eSyi zhang - Sun Microsystems - Beijing China 				/* no any target parameters get */
19488a29b80eSyi zhang - Sun Microsystems - Beijing China 				kmem_free(name, ISCSI_MAX_NAME_LEN);
19498a29b80eSyi zhang - Sun Microsystems - Beijing China 				rw_exit(&ihp->hba_sess_list_rwlock);
19508a29b80eSyi zhang - Sun Microsystems - Beijing China 				rtn = EIO;
19518a29b80eSyi zhang - Sun Microsystems - Beijing China 				break;
19528a29b80eSyi zhang - Sun Microsystems - Beijing China 			}
19538a29b80eSyi zhang - Sun Microsystems - Beijing China 
1954fcf3ce44SJohn Forte 			if (persistent_param_clear((char *)name) == B_FALSE) {
1955fcf3ce44SJohn Forte 				kmem_free(name, ISCSI_MAX_NAME_LEN);
1956fcf3ce44SJohn Forte 				rw_exit(&ihp->hba_sess_list_rwlock);
1957fcf3ce44SJohn Forte 				rtn = EIO;
1958fcf3ce44SJohn Forte 				break;
1959fcf3ce44SJohn Forte 			}
1960fcf3ce44SJohn Forte 
1961fcf3ce44SJohn Forte 			ics = kmem_zalloc(sizeof (*ics), KM_SLEEP);
1962fcf3ce44SJohn Forte 			ics->ics_ver = ISCSI_INTERFACE_VERSION;
1963fcf3ce44SJohn Forte 			ics->ics_oid = ISCSI_INITIATOR_OID;
1964fcf3ce44SJohn Forte 			ics->ics_in  = 1;
1965fcf3ce44SJohn Forte 
1966fcf3ce44SJohn Forte 			/*
1967fcf3ce44SJohn Forte 			 * We may have multiple sessions with different
1968fcf3ce44SJohn Forte 			 * tpgt values.  So we need to loop through
1969fcf3ce44SJohn Forte 			 * the sessions and update all sessions.
1970fcf3ce44SJohn Forte 			 */
1971fcf3ce44SJohn Forte 			for (isp = ihp->hba_sess_list; isp;
1972fcf3ce44SJohn Forte 			    isp = t_isp) {
1973fcf3ce44SJohn Forte 				t_isp = isp->sess_next;
1974fcf3ce44SJohn Forte 
1975fcf3ce44SJohn Forte 				if (strncmp((char *)isp->sess_name,
1976fcf3ce44SJohn Forte 				    (char *)name, ISCSI_MAX_NAME_LEN) == 0) {
1977fcf3ce44SJohn Forte 					/*
1978fcf3ce44SJohn Forte 					 * When removing target-params we need
1979fcf3ce44SJohn Forte 					 * slightly different actions depending
1980fcf3ce44SJohn Forte 					 * on if the session should still exist.
1981fcf3ce44SJohn Forte 					 * Get the initiator-node value for
1982fcf3ce44SJohn Forte 					 * MS/T.  If there is no initiator
1983fcf3ce44SJohn Forte 					 * value then assume the default value
1984fcf3ce44SJohn Forte 					 * of 1.  If the initiator value is
1985fcf3ce44SJohn Forte 					 * less than this ISID then we need to
1986fcf3ce44SJohn Forte 					 * destroy the session.  Otherwise
1987fcf3ce44SJohn Forte 					 * update the session information and
1988fcf3ce44SJohn Forte 					 * resync (N7 event).
1989fcf3ce44SJohn Forte 					 */
1990fcf3ce44SJohn Forte 					rtn = iscsi_ioctl_get_config_sess(
1991fcf3ce44SJohn Forte 					    ihp, ics);
1992fcf3ce44SJohn Forte 					if (((rtn != 0) &&
1993fcf3ce44SJohn Forte 					    (isp->sess_isid[5] > 0)) ||
1994fcf3ce44SJohn Forte 					    ((rtn == 0) &&
1995fcf3ce44SJohn Forte 					    (ics->ics_out <=
1996fcf3ce44SJohn Forte 					    isp->sess_isid[5]))) {
1997fcf3ce44SJohn Forte 
1998fcf3ce44SJohn Forte 						/*
1999fcf3ce44SJohn Forte 						 * This session should no
2000fcf3ce44SJohn Forte 						 * longer exist.  Remove
2001fcf3ce44SJohn Forte 						 * session.
2002fcf3ce44SJohn Forte 						 */
2003fcf3ce44SJohn Forte 						if (!ISCSI_SUCCESS(
2004fcf3ce44SJohn Forte 						    iscsi_sess_destroy(isp))) {
20058a29b80eSyi zhang - Sun Microsystems - Beijing China 							t_rtn = B_FALSE;
20068a29b80eSyi zhang - Sun Microsystems - Beijing China 							continue;
2007fcf3ce44SJohn Forte 						}
2008fcf3ce44SJohn Forte 						isp = ihp->hba_sess_list;
2009fcf3ce44SJohn Forte 					} else {
2010904e51f6SJack Meng 						uint32_t event_count;
2011fcf3ce44SJohn Forte 						/*
2012fcf3ce44SJohn Forte 						 * Reset the session
2013fcf3ce44SJohn Forte 						 * parameters.
2014fcf3ce44SJohn Forte 						 */
2015fcf3ce44SJohn Forte 						bcopy(&(isp->sess_hba->
2016fcf3ce44SJohn Forte 						    hba_params),
2017fcf3ce44SJohn Forte 						    &(isp->sess_params),
2018fcf3ce44SJohn Forte 						    sizeof (isp->sess_params));
20196cefaae1SJack Meng 						if (iscsiboot_prop &&
20206cefaae1SJack Meng 						    isp->sess_boot) {
20216cefaae1SJack Meng 							/*
20226cefaae1SJack Meng 							 * reconfig boot
20236cefaae1SJack Meng 							 * session later
20246cefaae1SJack Meng 							 */
20256cefaae1SJack Meng 							continue;
20266cefaae1SJack Meng 						}
2027fcf3ce44SJohn Forte 						/*
2028fcf3ce44SJohn Forte 						 * Notify the session that the
2029fcf3ce44SJohn Forte 						 * login parameters have
2030fcf3ce44SJohn Forte 						 * changed.
2031fcf3ce44SJohn Forte 						 */
2032904e51f6SJack Meng 						event_count = atomic_inc_32_nv(
2033904e51f6SJack Meng 						    &isp->
2034904e51f6SJack Meng 						    sess_state_event_count);
2035904e51f6SJack Meng 						iscsi_sess_enter_state_zone(
2036904e51f6SJack Meng 						    isp);
2037904e51f6SJack Meng 
2038fcf3ce44SJohn Forte 						iscsi_sess_state_machine(isp,
2039904e51f6SJack Meng 						    ISCSI_SESS_EVENT_N7,
2040904e51f6SJack Meng 						    event_count);
2041904e51f6SJack Meng 
2042904e51f6SJack Meng 						iscsi_sess_exit_state_zone(
2043904e51f6SJack Meng 						    isp);
2044fcf3ce44SJohn Forte 					}
2045fcf3ce44SJohn Forte 				}
2046fcf3ce44SJohn Forte 			}
20478a29b80eSyi zhang - Sun Microsystems - Beijing China 			if (t_rtn == B_FALSE) {
20488a29b80eSyi zhang - Sun Microsystems - Beijing China 				boolean_t t_rval = B_TRUE;
20498a29b80eSyi zhang - Sun Microsystems - Beijing China 				/* Failure!, restore target's parameters */
20508a29b80eSyi zhang - Sun Microsystems - Beijing China 				if (rval1 == B_TRUE) {
20518a29b80eSyi zhang - Sun Microsystems - Beijing China 					rval1 = persistent_param_set(
20528a29b80eSyi zhang - Sun Microsystems - Beijing China 					    (char *)name, &t_param);
20538a29b80eSyi zhang - Sun Microsystems - Beijing China 					if (rval1 == B_FALSE) {
20548a29b80eSyi zhang - Sun Microsystems - Beijing China 						t_rval = B_FALSE;
20558a29b80eSyi zhang - Sun Microsystems - Beijing China 					}
20568a29b80eSyi zhang - Sun Microsystems - Beijing China 				}
20578a29b80eSyi zhang - Sun Microsystems - Beijing China 				if (rval2 == B_TRUE) {
20588a29b80eSyi zhang - Sun Microsystems - Beijing China 					rval2 = persistent_set_config_session(
20598a29b80eSyi zhang - Sun Microsystems - Beijing China 					    (char *)name, &t_ics);
20608a29b80eSyi zhang - Sun Microsystems - Beijing China 					if (rval2 == B_FALSE) {
20618a29b80eSyi zhang - Sun Microsystems - Beijing China 						t_rval = B_FALSE;
20628a29b80eSyi zhang - Sun Microsystems - Beijing China 					}
20638a29b80eSyi zhang - Sun Microsystems - Beijing China 				}
20648a29b80eSyi zhang - Sun Microsystems - Beijing China 				if (rval3 == B_TRUE) {
20658a29b80eSyi zhang - Sun Microsystems - Beijing China 					rval3 = persistent_set_tunable_param(
20668a29b80eSyi zhang - Sun Microsystems - Beijing China 					    (char *)name, &t_tpsg);
20678a29b80eSyi zhang - Sun Microsystems - Beijing China 					if (rval3 == B_FALSE) {
20688a29b80eSyi zhang - Sun Microsystems - Beijing China 						t_rval = B_FALSE;
20698a29b80eSyi zhang - Sun Microsystems - Beijing China 					}
20708a29b80eSyi zhang - Sun Microsystems - Beijing China 				}
20718a29b80eSyi zhang - Sun Microsystems - Beijing China 				if (t_rval == B_FALSE) {
20728a29b80eSyi zhang - Sun Microsystems - Beijing China 					cmn_err(CE_WARN, "Failed to restore "
20738a29b80eSyi zhang - Sun Microsystems - Beijing China 					    "target's parameters after remove "
20748a29b80eSyi zhang - Sun Microsystems - Beijing China 					    "session related to target "
20758a29b80eSyi zhang - Sun Microsystems - Beijing China 					    "parameters failure.");
20768a29b80eSyi zhang - Sun Microsystems - Beijing China 				}
20778a29b80eSyi zhang - Sun Microsystems - Beijing China 				rtn = EBUSY;
20788a29b80eSyi zhang - Sun Microsystems - Beijing China 			}
2079fcf3ce44SJohn Forte 			kmem_free(ics, sizeof (*ics));
2080fcf3ce44SJohn Forte 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2081fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
20826cefaae1SJack Meng 			if (iscsiboot_prop) {
20836cefaae1SJack Meng 				if (iscsi_cmp_boot_sess_oid(ihp, e.e_oid)) {
20846cefaae1SJack Meng 					/*
20856cefaae1SJack Meng 					 * found active session for this object
20866cefaae1SJack Meng 					 * or this is initiator object
20876cefaae1SJack Meng 					 * with mpxio enabled
20886cefaae1SJack Meng 					 */
20896cefaae1SJack Meng 					if (!iscsi_reconfig_boot_sess(ihp)) {
20906cefaae1SJack Meng 						rtn = EINVAL;
20916cefaae1SJack Meng 						break;
20926cefaae1SJack Meng 					}
20936cefaae1SJack Meng 				}
20946cefaae1SJack Meng 			}
2095fcf3ce44SJohn Forte 		}
2096fcf3ce44SJohn Forte 		break;
2097fcf3ce44SJohn Forte 
2098fcf3ce44SJohn Forte 	/*
2099fcf3ce44SJohn Forte 	 * ISCSI_TARGET_OID_LIST_GET -
2100fcf3ce44SJohn Forte 	 */
2101fcf3ce44SJohn Forte 	case ISCSI_TARGET_OID_LIST_GET:
2102fcf3ce44SJohn Forte 		/* copyin user args */
2103fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &idl,
2104fcf3ce44SJohn Forte 		    sizeof (idl), mode)) {
2105fcf3ce44SJohn Forte 			rtn = EFAULT;
2106fcf3ce44SJohn Forte 			break;
2107fcf3ce44SJohn Forte 		}
2108fcf3ce44SJohn Forte 
2109fcf3ce44SJohn Forte 		if (idl.tl_vers != ISCSI_INTERFACE_VERSION) {
2110fcf3ce44SJohn Forte 			rtn = EINVAL;
2111fcf3ce44SJohn Forte 			break;
2112fcf3ce44SJohn Forte 		}
2113fcf3ce44SJohn Forte 
2114fcf3ce44SJohn Forte 		list_space = sizeof (iscsi_target_list_t);
2115fcf3ce44SJohn Forte 		if (idl.tl_in_cnt != 0)
2116fcf3ce44SJohn Forte 			list_space += (sizeof (uint32_t) *
2117fcf3ce44SJohn Forte 			    (idl.tl_in_cnt - 1));
2118fcf3ce44SJohn Forte 
2119fcf3ce44SJohn Forte 		idlp = kmem_zalloc(list_space, KM_SLEEP);
2120fcf3ce44SJohn Forte 		bcopy(&idl, idlp, sizeof (idl));
2121fcf3ce44SJohn Forte 		idlp->tl_out_cnt = 0;
2122fcf3ce44SJohn Forte 
2123fcf3ce44SJohn Forte 		/*
2124fcf3ce44SJohn Forte 		 * If target list type is ISCSI_TGT_OID_LIST and discovery
2125fcf3ce44SJohn Forte 		 * has not been completed or in progress, poke the discovery
2126fcf3ce44SJohn Forte 		 * methods so target information is returned
2127fcf3ce44SJohn Forte 		 */
2128fcf3ce44SJohn Forte 		mutex_enter(&ihp->hba_discovery_events_mutex);
2129fcf3ce44SJohn Forte 		method = ihp->hba_discovery_events;
2130fcf3ce44SJohn Forte 		if ((idl.tl_tgt_list_type == ISCSI_TGT_OID_LIST) &&
2131fcf3ce44SJohn Forte 		    (method != ISCSI_ALL_DISCOVERY_METHODS) &&
2132fcf3ce44SJohn Forte 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
2133fcf3ce44SJohn Forte 			ihp->hba_discovery_in_progress = B_TRUE;
2134fcf3ce44SJohn Forte 			mutex_exit(&ihp->hba_discovery_events_mutex);
2135fcf3ce44SJohn Forte 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
2136fcf3ce44SJohn Forte 			mutex_enter(&ihp->hba_discovery_events_mutex);
2137fcf3ce44SJohn Forte 			ihp->hba_discovery_in_progress = B_FALSE;
2138fcf3ce44SJohn Forte 		}
2139fcf3ce44SJohn Forte 		mutex_exit(&ihp->hba_discovery_events_mutex);
2140fcf3ce44SJohn Forte 
2141fcf3ce44SJohn Forte 		/*
2142fcf3ce44SJohn Forte 		 * Return the correct list information based on the type
2143fcf3ce44SJohn Forte 		 */
2144fcf3ce44SJohn Forte 		switch (idl.tl_tgt_list_type) {
2145fcf3ce44SJohn Forte 		/* ISCSI_TGT_PARAM_OID_LIST - iscsiadm list target-params */
2146fcf3ce44SJohn Forte 		case ISCSI_TGT_PARAM_OID_LIST:
2147fcf3ce44SJohn Forte 			/* get params from persistent store */
2148fcf3ce44SJohn Forte 			iscsi_targetparam_lock_list(RW_READER);
2149fcf3ce44SJohn Forte 			curr_entry = iscsi_targetparam_get_next_entry(NULL);
2150fcf3ce44SJohn Forte 			while (curr_entry != NULL) {
2151fcf3ce44SJohn Forte 				if (idlp->tl_out_cnt < idlp->tl_in_cnt) {
2152fcf3ce44SJohn Forte 					idlp->tl_oid_list[idlp->tl_out_cnt] =
2153fcf3ce44SJohn Forte 					    curr_entry->target_oid;
2154fcf3ce44SJohn Forte 				}
2155fcf3ce44SJohn Forte 				idlp->tl_out_cnt++;
2156fcf3ce44SJohn Forte 				curr_entry = iscsi_targetparam_get_next_entry(
2157fcf3ce44SJohn Forte 				    curr_entry);
2158fcf3ce44SJohn Forte 			}
2159fcf3ce44SJohn Forte 			iscsi_targetparam_unlock_list();
2160fcf3ce44SJohn Forte 			break;
2161fcf3ce44SJohn Forte 
2162fcf3ce44SJohn Forte 		/* ISCSI_STATIC_TGT_OID_LIST - iscsiadm list static-config */
2163fcf3ce44SJohn Forte 		case ISCSI_STATIC_TGT_OID_LIST:
2164fcf3ce44SJohn Forte 		{
2165fcf3ce44SJohn Forte 			char *target_name = NULL;
2166fcf3ce44SJohn Forte 			void *v = NULL;
2167fcf3ce44SJohn Forte 
2168fcf3ce44SJohn Forte 			/* get static-config from persistent store */
2169fcf3ce44SJohn Forte 			target_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2170fcf3ce44SJohn Forte 			persistent_static_addr_lock();
2171fcf3ce44SJohn Forte 			while (persistent_static_addr_next(&v,
2172fcf3ce44SJohn Forte 			    (char *)target_name, &e) == B_TRUE) {
2173fcf3ce44SJohn Forte 
2174fcf3ce44SJohn Forte 				if (idlp->tl_out_cnt < idlp->tl_in_cnt) {
2175fcf3ce44SJohn Forte 					idlp->tl_oid_list[idlp->tl_out_cnt] =
2176fcf3ce44SJohn Forte 					    e.e_oid;
2177fcf3ce44SJohn Forte 				}
2178fcf3ce44SJohn Forte 				idlp->tl_out_cnt++;
2179fcf3ce44SJohn Forte 
2180fcf3ce44SJohn Forte 			}
2181fcf3ce44SJohn Forte 
2182fcf3ce44SJohn Forte 			persistent_static_addr_unlock();
2183fcf3ce44SJohn Forte 			kmem_free(target_name, ISCSI_MAX_NAME_LEN);
2184fcf3ce44SJohn Forte 			break;
2185fcf3ce44SJohn Forte 		}
2186fcf3ce44SJohn Forte 
2187fcf3ce44SJohn Forte 		/* ISCSI_TGT_OID_LIST - iscsiadm list target */
2188fcf3ce44SJohn Forte 		case ISCSI_TGT_OID_LIST:
2189fcf3ce44SJohn Forte 
2190fcf3ce44SJohn Forte 			/* get sessions from hba's session list */
2191fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2192fcf3ce44SJohn Forte 			for (isp = ihp->hba_sess_list; isp;
2193fcf3ce44SJohn Forte 			    isp = isp->sess_next) {
2194fcf3ce44SJohn Forte 
2195fcf3ce44SJohn Forte 				if (((isp->sess_state !=
2196fcf3ce44SJohn Forte 				    ISCSI_SESS_STATE_FREE) ||
2197fcf3ce44SJohn Forte 				    (isp->sess_discovered_by !=
2198fcf3ce44SJohn Forte 				    iSCSIDiscoveryMethodUnknown)) &&
2199fcf3ce44SJohn Forte 				    (isp->sess_type ==
2200fcf3ce44SJohn Forte 				    ISCSI_SESS_TYPE_NORMAL)) {
2201fcf3ce44SJohn Forte 					if (idlp->tl_out_cnt <
2202fcf3ce44SJohn Forte 					    idlp->tl_in_cnt) {
2203fcf3ce44SJohn Forte 						idlp->tl_oid_list[
2204fcf3ce44SJohn Forte 						    idlp->tl_out_cnt] =
2205fcf3ce44SJohn Forte 						    isp->sess_oid;
2206fcf3ce44SJohn Forte 					}
2207fcf3ce44SJohn Forte 					idlp->tl_out_cnt++;
2208fcf3ce44SJohn Forte 				}
2209fcf3ce44SJohn Forte 
2210fcf3ce44SJohn Forte 			}
2211fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
2212fcf3ce44SJohn Forte 			break;
2213fcf3ce44SJohn Forte 
2214fcf3ce44SJohn Forte 		default:
2215fcf3ce44SJohn Forte 			ASSERT(FALSE);
2216fcf3ce44SJohn Forte 		}
2217fcf3ce44SJohn Forte 
2218fcf3ce44SJohn Forte 		rtn = ddi_copyout(idlp, (caddr_t)arg, list_space, mode);
2219fcf3ce44SJohn Forte 		kmem_free(idlp, list_space);
2220fcf3ce44SJohn Forte 		break;
2221fcf3ce44SJohn Forte 
2222fcf3ce44SJohn Forte 	/*
2223fcf3ce44SJohn Forte 	 * ISCSI_TARGET_PROPS_GET -
2224fcf3ce44SJohn Forte 	 */
2225fcf3ce44SJohn Forte 	case ISCSI_TARGET_PROPS_GET:
2226fcf3ce44SJohn Forte 		/* ---- fall through sense the code is almost the same ---- */
2227fcf3ce44SJohn Forte 
2228fcf3ce44SJohn Forte 	/*
2229fcf3ce44SJohn Forte 	 * ISCSI_TARGET_PROPS_SET -
2230fcf3ce44SJohn Forte 	 */
2231fcf3ce44SJohn Forte 	case ISCSI_TARGET_PROPS_SET:
2232fcf3ce44SJohn Forte 		/* copyin user args */
2233fcf3ce44SJohn Forte 		ipp = (iscsi_property_t *)kmem_alloc(sizeof (*ipp),
2234fcf3ce44SJohn Forte 		    KM_SLEEP);
2235fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ipp, sizeof (*ipp), mode)) {
2236fcf3ce44SJohn Forte 			rtn = EFAULT;
2237fcf3ce44SJohn Forte 			kmem_free(ipp, sizeof (*ipp));
2238fcf3ce44SJohn Forte 			break;
2239fcf3ce44SJohn Forte 		}
2240fcf3ce44SJohn Forte 
2241fcf3ce44SJohn Forte 		if (ipp->p_vers != ISCSI_INTERFACE_VERSION) {
2242fcf3ce44SJohn Forte 			rtn = EINVAL;
2243fcf3ce44SJohn Forte 			kmem_free(ipp, sizeof (*ipp));
2244fcf3ce44SJohn Forte 			break;
2245fcf3ce44SJohn Forte 		}
2246fcf3ce44SJohn Forte 
2247fcf3ce44SJohn Forte 		rtn = iscsi_target_prop_mod(ihp, ipp, cmd);
2248fcf3ce44SJohn Forte 		if (rtn == 0)
2249fcf3ce44SJohn Forte 			rtn = ddi_copyout(ipp, (caddr_t)arg,
2250fcf3ce44SJohn Forte 			    sizeof (*ipp), mode);
2251fcf3ce44SJohn Forte 		kmem_free(ipp, sizeof (*ipp));
2252fcf3ce44SJohn Forte 		break;
2253fcf3ce44SJohn Forte 
2254fcf3ce44SJohn Forte 	/*
2255fcf3ce44SJohn Forte 	 * ISCSI_TARGET_ADDRESS_GET -
2256fcf3ce44SJohn Forte 	 */
2257fcf3ce44SJohn Forte 	case ISCSI_TARGET_ADDRESS_GET:
2258fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2259fcf3ce44SJohn Forte 			rtn = EFAULT;
2260fcf3ce44SJohn Forte 			break;
2261fcf3ce44SJohn Forte 		}
2262fcf3ce44SJohn Forte 
2263fcf3ce44SJohn Forte 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2264fcf3ce44SJohn Forte 			rtn = EINVAL;
2265fcf3ce44SJohn Forte 			break;
2266fcf3ce44SJohn Forte 		}
2267fcf3ce44SJohn Forte 
2268fcf3ce44SJohn Forte 		/*
2269fcf3ce44SJohn Forte 		 * Find out how much space we need to malloc for the users
2270fcf3ce44SJohn Forte 		 * request.
2271fcf3ce44SJohn Forte 		 */
2272fcf3ce44SJohn Forte 		list_space = sizeof (iscsi_addr_list_t);
2273fcf3ce44SJohn Forte 		if (ial.al_in_cnt != 0) {
2274fcf3ce44SJohn Forte 			list_space += (sizeof (iscsi_addr_t) *
2275fcf3ce44SJohn Forte 			    (ial.al_in_cnt - 1));
2276fcf3ce44SJohn Forte 		}
2277fcf3ce44SJohn Forte 		ialp = (iscsi_addr_list_t *)kmem_zalloc(list_space, KM_SLEEP);
2278fcf3ce44SJohn Forte 
2279fcf3ce44SJohn Forte 		/* Copy in the header portion */
2280fcf3ce44SJohn Forte 		bcopy(&ial, ialp, sizeof (ial));
2281fcf3ce44SJohn Forte 
2282fcf3ce44SJohn Forte 		/* session */
2283fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2284fcf3ce44SJohn Forte 		rtn = iscsi_sess_get(ialp->al_oid, ihp, &isp);
2285fcf3ce44SJohn Forte 		if (rtn != 0) {
2286fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
2287fcf3ce44SJohn Forte 			rtn = EFAULT;
2288fcf3ce44SJohn Forte 			break;
2289fcf3ce44SJohn Forte 		}
2290fcf3ce44SJohn Forte 
2291fcf3ce44SJohn Forte 		ialp->al_out_cnt	= 0;
2292fcf3ce44SJohn Forte 		ialp->al_tpgt		= isp->sess_tpgt_conf;
2293fcf3ce44SJohn Forte 		rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
2294fcf3ce44SJohn Forte 		for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) {
2295fcf3ce44SJohn Forte 			if (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN) {
2296fcf3ce44SJohn Forte 				continue;
2297fcf3ce44SJohn Forte 			}
2298fcf3ce44SJohn Forte 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2299fcf3ce44SJohn Forte 				iscsi_addr_t		*ap;
2300fcf3ce44SJohn Forte 
2301fcf3ce44SJohn Forte 				ap = &ialp->al_addrs[ialp->al_out_cnt];
2302fcf3ce44SJohn Forte 				if (icp->conn_base_addr.sin.sa_family
2303fcf3ce44SJohn Forte 				    == AF_INET) {
2304fcf3ce44SJohn Forte 
2305fcf3ce44SJohn Forte 					struct sockaddr_in *addr_in =
2306fcf3ce44SJohn Forte 					    (struct sockaddr_in *)&icp->
2307fcf3ce44SJohn Forte 					    conn_base_addr.sin4;
2308fcf3ce44SJohn Forte 					ap->a_addr.i_insize =
2309fcf3ce44SJohn Forte 					    sizeof (struct in_addr);
2310fcf3ce44SJohn Forte 					bcopy(&addr_in->sin_addr.s_addr,
2311fcf3ce44SJohn Forte 					    &ap->a_addr.i_addr.in4.s_addr,
2312fcf3ce44SJohn Forte 					    sizeof (struct in_addr));
2313fcf3ce44SJohn Forte 					ap->a_port = addr_in->sin_port;
2314fcf3ce44SJohn Forte 
2315fcf3ce44SJohn Forte 				} else {
2316fcf3ce44SJohn Forte 
2317fcf3ce44SJohn Forte 					struct sockaddr_in6 *addr_in6 =
2318fcf3ce44SJohn Forte 					    (struct sockaddr_in6 *)&icp->
2319fcf3ce44SJohn Forte 					    conn_base_addr.sin6;
2320fcf3ce44SJohn Forte 					ap->a_addr.i_insize =
2321fcf3ce44SJohn Forte 					    sizeof (struct in6_addr);
2322fcf3ce44SJohn Forte 					bcopy(&addr_in6->sin6_addr.s6_addr,
2323fcf3ce44SJohn Forte 					    &ap->a_addr.i_addr.in6.s6_addr,
2324fcf3ce44SJohn Forte 					    sizeof (struct in6_addr));
2325fcf3ce44SJohn Forte 					ap->a_port = addr_in6->sin6_port;
2326fcf3ce44SJohn Forte 
2327fcf3ce44SJohn Forte 				}
2328fcf3ce44SJohn Forte 			}
2329fcf3ce44SJohn Forte 			ialp->al_out_cnt++;
2330fcf3ce44SJohn Forte 		}
2331fcf3ce44SJohn Forte 		rw_exit(&isp->sess_conn_list_rwlock);
2332fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
2333fcf3ce44SJohn Forte 
2334fcf3ce44SJohn Forte 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
2335fcf3ce44SJohn Forte 		kmem_free(ialp, list_space);
2336fcf3ce44SJohn Forte 		break;
2337fcf3ce44SJohn Forte 
2338fcf3ce44SJohn Forte 	/*
2339fcf3ce44SJohn Forte 	 * ISCSI_CHAP_SET -
2340fcf3ce44SJohn Forte 	 */
2341fcf3ce44SJohn Forte 	case ISCSI_CHAP_SET:
2342fcf3ce44SJohn Forte 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2343fcf3ce44SJohn Forte 		    KM_SLEEP);
2344fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2345fcf3ce44SJohn Forte 			rtn = EFAULT;
2346fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2347fcf3ce44SJohn Forte 			break;
2348fcf3ce44SJohn Forte 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2349fcf3ce44SJohn Forte 			rtn = EINVAL;
2350fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2351fcf3ce44SJohn Forte 			break;
2352fcf3ce44SJohn Forte 		}
2353fcf3ce44SJohn Forte 
2354fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2355fcf3ce44SJohn Forte 		if (chap->c_oid == ihp->hba_oid)
2356fcf3ce44SJohn Forte 			name = ihp->hba_name;
2357fcf3ce44SJohn Forte 		else {
2358fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2359fcf3ce44SJohn Forte 			if (rtn != 0) {
2360fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(
2361fcf3ce44SJohn Forte 				    chap->c_oid, ihp, &isp);
2362fcf3ce44SJohn Forte 			}
2363fcf3ce44SJohn Forte 
2364fcf3ce44SJohn Forte 			/*
2365fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
2366fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
2367fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
2368fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
2369fcf3ce44SJohn Forte 			 * for param lookup.
2370fcf3ce44SJohn Forte 			 */
2371fcf3ce44SJohn Forte 			if (rtn == 0) {
2372fcf3ce44SJohn Forte 				name = isp->sess_name;
2373fcf3ce44SJohn Forte 			} else {
2374fcf3ce44SJohn Forte 				name =
2375fcf3ce44SJohn Forte 				    iscsi_targetparam_get_name(chap->c_oid);
2376fcf3ce44SJohn Forte 				rtn = 0;
2377fcf3ce44SJohn Forte 			}
2378fcf3ce44SJohn Forte 		}
2379fcf3ce44SJohn Forte 
2380fcf3ce44SJohn Forte 		if (name == NULL) {
2381fcf3ce44SJohn Forte 			rw_exit(
2382fcf3ce44SJohn Forte 			    &ihp->hba_sess_list_rwlock);
2383fcf3ce44SJohn Forte 			rtn = EFAULT;
2384fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2385fcf3ce44SJohn Forte 			break;
2386fcf3ce44SJohn Forte 		}
2387fcf3ce44SJohn Forte 
2388fcf3ce44SJohn Forte 		if (persistent_chap_set((char *)name, chap) ==
2389fcf3ce44SJohn Forte 		    B_FALSE) {
2390fcf3ce44SJohn Forte 			rtn = EIO;
2391fcf3ce44SJohn Forte 		}
2392fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
2393fcf3ce44SJohn Forte 		kmem_free(chap, sizeof (*chap));
2394fcf3ce44SJohn Forte 		break;
2395fcf3ce44SJohn Forte 
2396fcf3ce44SJohn Forte 	/*
2397fcf3ce44SJohn Forte 	 * ISCSI_CHAP_GET -
2398fcf3ce44SJohn Forte 	 */
2399fcf3ce44SJohn Forte 	case ISCSI_CHAP_GET:
2400fcf3ce44SJohn Forte 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2401fcf3ce44SJohn Forte 		    KM_SLEEP);
2402fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2403fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2404fcf3ce44SJohn Forte 			rtn = EFAULT;
2405fcf3ce44SJohn Forte 			break;
2406fcf3ce44SJohn Forte 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2407fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2408fcf3ce44SJohn Forte 			rtn = EINVAL;
2409fcf3ce44SJohn Forte 			break;
2410fcf3ce44SJohn Forte 		}
2411fcf3ce44SJohn Forte 
2412fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2413fcf3ce44SJohn Forte 		if (chap->c_oid == ihp->hba_oid)
2414fcf3ce44SJohn Forte 			name = ihp->hba_name;
2415fcf3ce44SJohn Forte 		else {
2416fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2417fcf3ce44SJohn Forte 			if (rtn != 0) {
2418fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(
2419fcf3ce44SJohn Forte 				    chap->c_oid, ihp, &isp);
2420fcf3ce44SJohn Forte 			}
2421fcf3ce44SJohn Forte 
2422fcf3ce44SJohn Forte 			/*
2423fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
2424fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
2425fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
2426fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
2427fcf3ce44SJohn Forte 			 * for param lookup.
2428fcf3ce44SJohn Forte 			 */
2429fcf3ce44SJohn Forte 			if (rtn == 0) {
2430fcf3ce44SJohn Forte 				name = isp->sess_name;
2431fcf3ce44SJohn Forte 			} else {
2432fcf3ce44SJohn Forte 				rtn = 0;
2433fcf3ce44SJohn Forte 				name =
2434fcf3ce44SJohn Forte 				    iscsi_targetparam_get_name(chap->c_oid);
2435fcf3ce44SJohn Forte 			}
2436fcf3ce44SJohn Forte 
2437f5fc5c04SJack Meng 			if (name == NULL) {
2438f5fc5c04SJack Meng 				rw_exit(&ihp->hba_sess_list_rwlock);
2439f5fc5c04SJack Meng 				rtn = EFAULT;
2440f5fc5c04SJack Meng 				break;
2441f5fc5c04SJack Meng 			}
2442fcf3ce44SJohn Forte 			/*
2443fcf3ce44SJohn Forte 			 * Initialize the target-side chap name to the
2444fcf3ce44SJohn Forte 			 * session name if no chap settings have been
2445fcf3ce44SJohn Forte 			 * saved for the current session.
2446fcf3ce44SJohn Forte 			 */
2447fcf3ce44SJohn Forte 			if (persistent_chap_get((char *)name,
2448fcf3ce44SJohn Forte 			    chap) == B_FALSE) {
2449fcf3ce44SJohn Forte 				int name_len = strlen((char *)name);
2450fcf3ce44SJohn Forte 				iscsi_chap_props_t *chap = NULL;
2451fcf3ce44SJohn Forte 				chap = (iscsi_chap_props_t *)kmem_zalloc
2452fcf3ce44SJohn Forte 				    (sizeof (iscsi_chap_props_t), KM_SLEEP);
2453fcf3ce44SJohn Forte 				bcopy((char *)name, chap->c_user, name_len);
2454fcf3ce44SJohn Forte 				chap->c_user_len = name_len;
2455fcf3ce44SJohn Forte 				(void) (persistent_chap_set((char *)name,
2456fcf3ce44SJohn Forte 				    chap));
2457fcf3ce44SJohn Forte 				kmem_free(chap, sizeof (*chap));
2458fcf3ce44SJohn Forte 			}
2459fcf3ce44SJohn Forte 		}
2460fcf3ce44SJohn Forte 
2461fcf3ce44SJohn Forte 		if (name == NULL) {
2462fcf3ce44SJohn Forte 			rw_exit(
2463fcf3ce44SJohn Forte 			    &ihp->hba_sess_list_rwlock);
2464fcf3ce44SJohn Forte 			rtn = EFAULT;
2465fcf3ce44SJohn Forte 			break;
2466fcf3ce44SJohn Forte 		}
2467fcf3ce44SJohn Forte 
2468fcf3ce44SJohn Forte 		if (persistent_chap_get((char *)name, chap) == B_FALSE) {
2469fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
2470fcf3ce44SJohn Forte 			rtn = EIO;
2471fcf3ce44SJohn Forte 			break;
2472fcf3ce44SJohn Forte 		}
2473fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
2474fcf3ce44SJohn Forte 
2475fcf3ce44SJohn Forte 		rtn = ddi_copyout(chap, (caddr_t)arg, sizeof (*chap), mode);
2476fcf3ce44SJohn Forte 		kmem_free(chap, sizeof (*chap));
2477fcf3ce44SJohn Forte 		break;
2478fcf3ce44SJohn Forte 
2479fcf3ce44SJohn Forte 	/*
2480fcf3ce44SJohn Forte 	 * ISCSI_CHAP_CLEAR -
2481fcf3ce44SJohn Forte 	 */
2482fcf3ce44SJohn Forte 	case ISCSI_CHAP_CLEAR:
2483fcf3ce44SJohn Forte 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2484fcf3ce44SJohn Forte 		    KM_SLEEP);
2485fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2486fcf3ce44SJohn Forte 			rtn = EFAULT;
2487fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2488fcf3ce44SJohn Forte 			break;
2489fcf3ce44SJohn Forte 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2490fcf3ce44SJohn Forte 			rtn = EINVAL;
2491fcf3ce44SJohn Forte 			kmem_free(chap, sizeof (*chap));
2492fcf3ce44SJohn Forte 			break;
2493fcf3ce44SJohn Forte 		}
2494fcf3ce44SJohn Forte 
2495fcf3ce44SJohn Forte 		if (chap->c_oid == ihp->hba_oid) {
2496fcf3ce44SJohn Forte 			iscsi_sess_t *sessp;
2497fcf3ce44SJohn Forte 
2498fcf3ce44SJohn Forte 			name = ihp->hba_name;
2499fcf3ce44SJohn Forte 
2500fcf3ce44SJohn Forte 			if (persistent_chap_clear(
2501fcf3ce44SJohn Forte 			    (char *)name) == B_FALSE) {
2502fcf3ce44SJohn Forte 				rtn = EIO;
2503fcf3ce44SJohn Forte 			}
2504fcf3ce44SJohn Forte 
2505fcf3ce44SJohn Forte 			/*
2506fcf3ce44SJohn Forte 			 * Loop through all sessions and memset their
2507fcf3ce44SJohn Forte 			 * (initiator's) passwords
2508fcf3ce44SJohn Forte 			 */
2509fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2510fcf3ce44SJohn Forte 			for (sessp = ihp->hba_sess_list; sessp;
2511fcf3ce44SJohn Forte 			    sessp = sessp->sess_next) {
2512fcf3ce44SJohn Forte 				(void) memset(sessp->sess_auth.password,
2513fcf3ce44SJohn Forte 				    0, iscsiAuthStringMaxLength);
2514fcf3ce44SJohn Forte 				sessp->sess_auth.password_length = 0;
2515fcf3ce44SJohn Forte 			}
2516fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
2517fcf3ce44SJohn Forte 
2518fcf3ce44SJohn Forte 		} else {
2519fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2520fcf3ce44SJohn Forte 			/*
2521fcf3ce44SJohn Forte 			 * If the oid does represent a session check to see
2522fcf3ce44SJohn Forte 			 * if it is a target oid.  If so, return the target's
2523fcf3ce44SJohn Forte 			 * associated session.
2524fcf3ce44SJohn Forte 			 */
2525fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2526fcf3ce44SJohn Forte 			if (rtn != 0) {
2527fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(chap->c_oid,
2528fcf3ce44SJohn Forte 				    ihp, &isp);
2529fcf3ce44SJohn Forte 			}
2530fcf3ce44SJohn Forte 
2531fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
2532fcf3ce44SJohn Forte 
2533fcf3ce44SJohn Forte 			/*
2534fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
2535fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
2536fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
2537fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
2538fcf3ce44SJohn Forte 			 * for param lookup.
2539fcf3ce44SJohn Forte 			 */
2540fcf3ce44SJohn Forte 			if (rtn == 0) {
2541fcf3ce44SJohn Forte 				name = isp->sess_name;
2542fcf3ce44SJohn Forte 			} else {
2543fcf3ce44SJohn Forte 				name =
2544fcf3ce44SJohn Forte 				    iscsi_targetparam_get_name(chap->c_oid);
2545fcf3ce44SJohn Forte 				rtn = 0;
2546fcf3ce44SJohn Forte 			}
2547fcf3ce44SJohn Forte 
2548fcf3ce44SJohn Forte 			if (name == NULL) {
2549fcf3ce44SJohn Forte 				rtn = EFAULT;
2550fcf3ce44SJohn Forte 				break;
2551fcf3ce44SJohn Forte 			}
2552fcf3ce44SJohn Forte 
2553fcf3ce44SJohn Forte 			if (persistent_chap_clear(
2554fcf3ce44SJohn Forte 			    (char *)name) == B_FALSE) {
2555fcf3ce44SJohn Forte 				rtn = EIO;
2556fcf3ce44SJohn Forte 			}
2557fcf3ce44SJohn Forte 
2558fcf3ce44SJohn Forte 			/*
2559fcf3ce44SJohn Forte 			 * Clear out session chap password if we found a
2560fcf3ce44SJohn Forte 			 * session above.
2561fcf3ce44SJohn Forte 			 */
2562fcf3ce44SJohn Forte 			if (isp != NULL) {
2563fcf3ce44SJohn Forte 				(void) memset(isp->sess_auth.password_in,
2564fcf3ce44SJohn Forte 				    0, iscsiAuthStringMaxLength);
2565fcf3ce44SJohn Forte 				isp->sess_auth.password_length_in = 0;
2566fcf3ce44SJohn Forte 			}
2567fcf3ce44SJohn Forte 
2568fcf3ce44SJohn Forte 		}
2569fcf3ce44SJohn Forte 
2570fcf3ce44SJohn Forte 		kmem_free(chap, sizeof (*chap));
2571fcf3ce44SJohn Forte 		break;
2572fcf3ce44SJohn Forte 
2573fcf3ce44SJohn Forte 	/*
2574fcf3ce44SJohn Forte 	 * ISCSI_STATIC_GET -
2575fcf3ce44SJohn Forte 	 */
2576fcf3ce44SJohn Forte 	case ISCSI_STATIC_GET:
2577fcf3ce44SJohn Forte 		ispp = (iscsi_static_property_t *)kmem_alloc(
2578fcf3ce44SJohn Forte 		    sizeof (*ispp), KM_SLEEP);
2579fcf3ce44SJohn Forte 
2580fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ispp, sizeof (*ispp), mode)) {
2581fcf3ce44SJohn Forte 			rtn = EFAULT;
2582fcf3ce44SJohn Forte 			kmem_free(ispp, sizeof (*ispp));
2583fcf3ce44SJohn Forte 			break;
2584fcf3ce44SJohn Forte 		}
2585fcf3ce44SJohn Forte 
2586fcf3ce44SJohn Forte 		if (ispp->p_vers != ISCSI_INTERFACE_VERSION) {
2587fcf3ce44SJohn Forte 			rtn = EINVAL;
2588fcf3ce44SJohn Forte 			kmem_free(ispp, sizeof (*ispp));
2589fcf3ce44SJohn Forte 			break;
2590fcf3ce44SJohn Forte 		}
2591fcf3ce44SJohn Forte 
2592fcf3ce44SJohn Forte 		{
2593fcf3ce44SJohn Forte 			void *v = NULL;
2594fcf3ce44SJohn Forte 			boolean_t found = B_FALSE;
2595fcf3ce44SJohn Forte 
2596fcf3ce44SJohn Forte 			persistent_static_addr_lock();
2597fcf3ce44SJohn Forte 			while (persistent_static_addr_next(&v,
2598fcf3ce44SJohn Forte 			    (char *)ispp->p_name, &e) == B_TRUE) {
2599fcf3ce44SJohn Forte 
2600fcf3ce44SJohn Forte 				if (ispp->p_oid == e.e_oid) {
2601fcf3ce44SJohn Forte 					/*
2602fcf3ce44SJohn Forte 					 * In case there are multiple
2603fcf3ce44SJohn Forte 					 * addresses associated with the
2604fcf3ce44SJohn Forte 					 * given target OID, pick the first
2605fcf3ce44SJohn Forte 					 * one.
2606fcf3ce44SJohn Forte 					 */
2607fcf3ce44SJohn Forte 					iscsi_addr_t *ap;
2608fcf3ce44SJohn Forte 
2609fcf3ce44SJohn Forte 					ap = &(ispp->p_addr_list.al_addrs[0]);
2610fcf3ce44SJohn Forte 					ap->a_port = e.e_port;
2611fcf3ce44SJohn Forte 					ap->a_addr.i_insize = e.e_insize;
2612fcf3ce44SJohn Forte 					bcopy(e.e_u.u_in6.s6_addr,
2613fcf3ce44SJohn Forte 					    ap->a_addr.i_addr.in6.s6_addr,
2614fcf3ce44SJohn Forte 					    e.e_insize);
2615fcf3ce44SJohn Forte 					ispp->p_name_len =
2616fcf3ce44SJohn Forte 					    strlen((char *)ispp->p_name);
2617fcf3ce44SJohn Forte 					ispp->p_addr_list.al_tpgt = e.e_tpgt;
2618fcf3ce44SJohn Forte 					ispp->p_addr_list.al_out_cnt = 1;
2619fcf3ce44SJohn Forte 
2620fcf3ce44SJohn Forte 					found = B_TRUE;
2621fcf3ce44SJohn Forte 					break;
2622fcf3ce44SJohn Forte 				}
2623fcf3ce44SJohn Forte 			}
2624fcf3ce44SJohn Forte 			persistent_static_addr_unlock();
2625fcf3ce44SJohn Forte 
2626fcf3ce44SJohn Forte 			if (found == B_TRUE) {
2627fcf3ce44SJohn Forte 				rtn = ddi_copyout(ispp, (caddr_t)arg,
2628fcf3ce44SJohn Forte 				    sizeof (*ispp), mode);
2629fcf3ce44SJohn Forte 			} else {
2630fcf3ce44SJohn Forte 				rtn = ENOENT;
2631fcf3ce44SJohn Forte 			}
2632fcf3ce44SJohn Forte 		}
2633fcf3ce44SJohn Forte 		kmem_free(ispp, sizeof (*ispp));
2634fcf3ce44SJohn Forte 
2635fcf3ce44SJohn Forte 		break;
2636fcf3ce44SJohn Forte 
2637fcf3ce44SJohn Forte 	/*
2638fcf3ce44SJohn Forte 	 * ISCSI_STATIC_SET -
2639fcf3ce44SJohn Forte 	 */
2640fcf3ce44SJohn Forte 	case ISCSI_STATIC_SET:
2641fcf3ce44SJohn Forte 		target = iscsi_ioctl_copyin((caddr_t)arg, mode,
2642fcf3ce44SJohn Forte 		    sizeof (*target));
2643fcf3ce44SJohn Forte 		if (target == NULL) {
2644fcf3ce44SJohn Forte 			rtn = EFAULT;
2645fcf3ce44SJohn Forte 			break;
2646fcf3ce44SJohn Forte 		}
2647fcf3ce44SJohn Forte 
2648d3e3772eSyi zhang - Sun Microsystems - Beijing China 		if ((target->te_entry.e_vers != ISCSI_INTERFACE_VERSION) ||
2649d3e3772eSyi zhang - Sun Microsystems - Beijing China 		    (target->te_entry.e_insize == 0)) {
2650fcf3ce44SJohn Forte 			kmem_free(target, sizeof (*target));
2651fcf3ce44SJohn Forte 			rtn = EINVAL;
2652fcf3ce44SJohn Forte 			break;
2653fcf3ce44SJohn Forte 		}
2654fcf3ce44SJohn Forte 
2655fcf3ce44SJohn Forte 		/* Check if the target's already been added */
2656fcf3ce44SJohn Forte 		{
2657fcf3ce44SJohn Forte 			boolean_t static_target_found = B_FALSE;
2658fcf3ce44SJohn Forte 			void *v = NULL;
2659fcf3ce44SJohn Forte 
2660fcf3ce44SJohn Forte 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2661fcf3ce44SJohn Forte 			persistent_static_addr_lock();
2662fcf3ce44SJohn Forte 			while (persistent_static_addr_next(&v, (char *)name,
2663fcf3ce44SJohn Forte 			    &e) == B_TRUE) {
2664fcf3ce44SJohn Forte 				/*
2665fcf3ce44SJohn Forte 				 * MC/S - Need to check IP address and port
2666fcf3ce44SJohn Forte 				 * number as well when we support MC/S.
2667fcf3ce44SJohn Forte 				 */
2668fcf3ce44SJohn Forte 				if ((strncmp((char *)name,
2669fcf3ce44SJohn Forte 				    (char *)target->te_name,
2670fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN) == 0) &&
2671fcf3ce44SJohn Forte 				    (target->te_entry.e_tpgt == e.e_tpgt) &&
2672fcf3ce44SJohn Forte 				    (target->te_entry.e_insize == e.e_insize) &&
2673fcf3ce44SJohn Forte 				    (bcmp(&target->te_entry.e_u, &e.e_u,
2674fcf3ce44SJohn Forte 				    e.e_insize) == 0)) {
2675fcf3ce44SJohn Forte 					/*
2676fcf3ce44SJohn Forte 					 * We don't allow MC/S for now but
2677fcf3ce44SJohn Forte 					 * we do allow adding the same target
2678fcf3ce44SJohn Forte 					 * with different TPGTs (hence,
2679fcf3ce44SJohn Forte 					 * different sessions).
2680fcf3ce44SJohn Forte 					 */
2681fcf3ce44SJohn Forte 					static_target_found = B_TRUE;
2682fcf3ce44SJohn Forte 					break;
2683fcf3ce44SJohn Forte 				}
2684fcf3ce44SJohn Forte 			}
2685fcf3ce44SJohn Forte 			persistent_static_addr_unlock();
2686fcf3ce44SJohn Forte 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2687fcf3ce44SJohn Forte 
2688fcf3ce44SJohn Forte 			if (static_target_found == B_TRUE) {
2689fcf3ce44SJohn Forte 				/* Duplicate entry */
2690fcf3ce44SJohn Forte 				kmem_free(target, sizeof (*target));
2691fcf3ce44SJohn Forte 				rtn = EEXIST;
2692fcf3ce44SJohn Forte 				break;
2693fcf3ce44SJohn Forte 			}
2694fcf3ce44SJohn Forte 		}
2695fcf3ce44SJohn Forte 
2696fcf3ce44SJohn Forte 		if (target->te_entry.e_oid == ISCSI_OID_NOTSET) {
2697fcf3ce44SJohn Forte 			mutex_enter(&iscsi_oid_mutex);
2698fcf3ce44SJohn Forte 			target->te_entry.e_oid = iscsi_oid++;
2699fcf3ce44SJohn Forte 			mutex_exit(&iscsi_oid_mutex);
2700fcf3ce44SJohn Forte 		}
2701fcf3ce44SJohn Forte 
2702fcf3ce44SJohn Forte 		persistent_static_addr_lock();
2703fcf3ce44SJohn Forte 		if (persistent_static_addr_set((char *)target->te_name,
2704fcf3ce44SJohn Forte 		    &target->te_entry) == B_FALSE) {
2705fcf3ce44SJohn Forte 			persistent_static_addr_unlock();
2706fcf3ce44SJohn Forte 			kmem_free(target, sizeof (*target));
2707fcf3ce44SJohn Forte 			rtn = EIO;
2708fcf3ce44SJohn Forte 			break;
2709fcf3ce44SJohn Forte 		}
2710fcf3ce44SJohn Forte 		persistent_static_addr_unlock();
2711fcf3ce44SJohn Forte 
2712fcf3ce44SJohn Forte 		/*
2713fcf3ce44SJohn Forte 		 * If Static Targets discovery is enabled, then add
2714fcf3ce44SJohn Forte 		 * target to discovery queue. Otherwise, just create
2715fcf3ce44SJohn Forte 		 * the session for potential future use.
2716fcf3ce44SJohn Forte 		 */
2717fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
2718fcf3ce44SJohn Forte 		if (method & iSCSIDiscoveryMethodStatic) {
2719fcf3ce44SJohn Forte 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodStatic);
2720fcf3ce44SJohn Forte 			(void) iscsid_login_tgt(ihp, (char *)target->te_name,
2721fcf3ce44SJohn Forte 			    iSCSIDiscoveryMethodStatic, NULL);
2722fcf3ce44SJohn Forte 		}
2723fcf3ce44SJohn Forte 
2724fcf3ce44SJohn Forte 		rtn = iscsi_ioctl_copyout(target, sizeof (*target),
2725fcf3ce44SJohn Forte 		    (caddr_t)arg, mode);
2726fcf3ce44SJohn Forte 		break;
2727fcf3ce44SJohn Forte 
2728fcf3ce44SJohn Forte 	/*
2729fcf3ce44SJohn Forte 	 * ISCSI_STATIC_CLEAR -
2730fcf3ce44SJohn Forte 	 */
2731fcf3ce44SJohn Forte 	case ISCSI_STATIC_CLEAR:
2732fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2733fcf3ce44SJohn Forte 			rtn = EFAULT;
2734fcf3ce44SJohn Forte 			break;
27354717ef6cSyi zhang - Sun Microsystems - Beijing China 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2736fcf3ce44SJohn Forte 			rtn = EINVAL;
2737fcf3ce44SJohn Forte 			break;
2738fcf3ce44SJohn Forte 		}
2739fcf3ce44SJohn Forte 
2740fcf3ce44SJohn Forte 		{
2741fcf3ce44SJohn Forte 			boolean_t	found = B_FALSE;
2742fcf3ce44SJohn Forte 			void		*v = NULL;
2743fcf3ce44SJohn Forte 			entry_t		tmp_e;
2744fcf3ce44SJohn Forte 			char		*name = NULL;
2745fcf3ce44SJohn Forte 
2746fcf3ce44SJohn Forte 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2747fcf3ce44SJohn Forte 
2748fcf3ce44SJohn Forte 			/* Find name for matching static_tgt oid */
2749fcf3ce44SJohn Forte 			persistent_static_addr_lock();
2750fcf3ce44SJohn Forte 			while (persistent_static_addr_next(&v,
2751fcf3ce44SJohn Forte 			    (char *)name, &tmp_e) == B_TRUE) {
2752fcf3ce44SJohn Forte 				if (e.e_oid == tmp_e.e_oid) {
2753fcf3ce44SJohn Forte 					found = B_TRUE;
2754fcf3ce44SJohn Forte 					break;
2755fcf3ce44SJohn Forte 				}
2756fcf3ce44SJohn Forte 			}
2757fcf3ce44SJohn Forte 
2758fcf3ce44SJohn Forte 			/* If static_tgt found logout and remove it */
2759fcf3ce44SJohn Forte 			if (found == B_TRUE) {
2760fcf3ce44SJohn Forte 
2761fcf3ce44SJohn Forte 				iscsid_addr_to_sockaddr(tmp_e.e_insize,
2762fcf3ce44SJohn Forte 				    &tmp_e.e_u, tmp_e.e_port, &addr_dsc.sin);
2763fcf3ce44SJohn Forte 
2764aff4bce5Syi zhang - Sun Microsystems - Beijing China 				persistent_static_addr_unlock();
2765aff4bce5Syi zhang - Sun Microsystems - Beijing China 
2766aff4bce5Syi zhang - Sun Microsystems - Beijing China 				/*
2767aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * If discovery in progress, try few times
2768aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * before return busy
2769aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 */
2770aff4bce5Syi zhang - Sun Microsystems - Beijing China 				retry = 0;
2771aff4bce5Syi zhang - Sun Microsystems - Beijing China 				mutex_enter(&ihp->hba_discovery_events_mutex);
2772aff4bce5Syi zhang - Sun Microsystems - Beijing China 				while (ihp->hba_discovery_in_progress ==
2773aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    B_TRUE) {
2774aff4bce5Syi zhang - Sun Microsystems - Beijing China 					if (++retry == 5) {
2775aff4bce5Syi zhang - Sun Microsystems - Beijing China 						rtn = EBUSY;
2776aff4bce5Syi zhang - Sun Microsystems - Beijing China 						break;
2777aff4bce5Syi zhang - Sun Microsystems - Beijing China 					}
2778aff4bce5Syi zhang - Sun Microsystems - Beijing China 					mutex_exit(
2779aff4bce5Syi zhang - Sun Microsystems - Beijing China 					    &ihp->hba_discovery_events_mutex);
2780aff4bce5Syi zhang - Sun Microsystems - Beijing China 					delay(SEC_TO_TICK(
2781aff4bce5Syi zhang - Sun Microsystems - Beijing China 					    ISCSI_DISC_DELAY));
2782aff4bce5Syi zhang - Sun Microsystems - Beijing China 					mutex_enter(
2783aff4bce5Syi zhang - Sun Microsystems - Beijing China 					    &ihp->hba_discovery_events_mutex);
2784aff4bce5Syi zhang - Sun Microsystems - Beijing China 				}
2785aff4bce5Syi zhang - Sun Microsystems - Beijing China 				/* remove from persistent store */
2786aff4bce5Syi zhang - Sun Microsystems - Beijing China 				if (rtn == 0 && persistent_static_addr_clear(
2787aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    e.e_oid) == B_FALSE) {
2788aff4bce5Syi zhang - Sun Microsystems - Beijing China 					rtn = EIO;
2789aff4bce5Syi zhang - Sun Microsystems - Beijing China 				}
2790aff4bce5Syi zhang - Sun Microsystems - Beijing China 				mutex_exit(&ihp->hba_discovery_events_mutex);
2791aff4bce5Syi zhang - Sun Microsystems - Beijing China 
2792aff4bce5Syi zhang - Sun Microsystems - Beijing China 				if (rtn != 0) {
2793aff4bce5Syi zhang - Sun Microsystems - Beijing China 					kmem_free(name, ISCSI_MAX_NAME_LEN);
2794aff4bce5Syi zhang - Sun Microsystems - Beijing China 					break;
2795aff4bce5Syi zhang - Sun Microsystems - Beijing China 				}
2796aff4bce5Syi zhang - Sun Microsystems - Beijing China 
2797fcf3ce44SJohn Forte 				/* Attempt to logout of target */
2798fcf3ce44SJohn Forte 				if (iscsid_del(ihp, (char *)name,
2799fcf3ce44SJohn Forte 				    iSCSIDiscoveryMethodStatic, &addr_dsc.sin)
2800aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    == B_FALSE) {
2801aff4bce5Syi zhang - Sun Microsystems - Beijing China 					persistent_static_addr_lock();
2802fcf3ce44SJohn Forte 
2803aff4bce5Syi zhang - Sun Microsystems - Beijing China 					/*
2804aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 * Restore static_tgt to
2805aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 * persistent store
2806aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 */
2807aff4bce5Syi zhang - Sun Microsystems - Beijing China 					if (persistent_static_addr_set(
2808aff4bce5Syi zhang - Sun Microsystems - Beijing China 					    (char *)name,
28095305b1e5Syi zhang - Sun Microsystems - Beijing China 					    &tmp_e) == B_FALSE) {
2810aff4bce5Syi zhang - Sun Microsystems - Beijing China 						cmn_err(CE_WARN, "Failed to "
2811aff4bce5Syi zhang - Sun Microsystems - Beijing China 						    "restore static target "
2812aff4bce5Syi zhang - Sun Microsystems - Beijing China 						    "address after logout "
2813aff4bce5Syi zhang - Sun Microsystems - Beijing China 						    "target failure.");
2814fcf3ce44SJohn Forte 					}
2815aff4bce5Syi zhang - Sun Microsystems - Beijing China 					persistent_static_addr_unlock();
2816aff4bce5Syi zhang - Sun Microsystems - Beijing China 					rtn = EBUSY;
2817aff4bce5Syi zhang - Sun Microsystems - Beijing China 				} else {
2818fcf3ce44SJohn Forte 					iscsid_poke_discovery(ihp,
2819fcf3ce44SJohn Forte 					    iSCSIDiscoveryMethodStatic);
2820fcf3ce44SJohn Forte 					(void) iscsid_login_tgt(ihp,
2821fcf3ce44SJohn Forte 					    (char *)name,
2822fcf3ce44SJohn Forte 					    iSCSIDiscoveryMethodStatic,
2823fcf3ce44SJohn Forte 					    NULL);
2824fcf3ce44SJohn Forte 
2825fcf3ce44SJohn Forte 				}
2826fcf3ce44SJohn Forte 			} else {
2827fcf3ce44SJohn Forte 				persistent_static_addr_unlock();
2828fcf3ce44SJohn Forte 				rtn = EIO;
2829fcf3ce44SJohn Forte 			}
2830fcf3ce44SJohn Forte 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2831fcf3ce44SJohn Forte 		}
2832fcf3ce44SJohn Forte 		break;
2833fcf3ce44SJohn Forte 
2834fcf3ce44SJohn Forte 	/*
2835fcf3ce44SJohn Forte 	 * ISCSI_ISNS_SERVER_ADDR_SET:
2836fcf3ce44SJohn Forte 	 */
2837fcf3ce44SJohn Forte 	case ISCSI_ISNS_SERVER_ADDR_SET:
2838fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2839fcf3ce44SJohn Forte 			rtn = EFAULT;
2840fcf3ce44SJohn Forte 			break;
2841fcf3ce44SJohn Forte 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2842fcf3ce44SJohn Forte 			rtn = EINVAL;
2843fcf3ce44SJohn Forte 			break;
2844fcf3ce44SJohn Forte 		}
2845fcf3ce44SJohn Forte 
2846fcf3ce44SJohn Forte 		if (persistent_isns_addr_set(&e) == B_FALSE) {
2847fcf3ce44SJohn Forte 			rtn = EIO;
2848fcf3ce44SJohn Forte 			break;
2849fcf3ce44SJohn Forte 		}
2850fcf3ce44SJohn Forte 
2851fcf3ce44SJohn Forte 		/*
2852fcf3ce44SJohn Forte 		 * If iSNS server discovery is enabled, then kickoff
2853fcf3ce44SJohn Forte 		 * discovery of the targets advertised by the recently
2854fcf3ce44SJohn Forte 		 * added iSNS server address.
2855fcf3ce44SJohn Forte 		 */
2856fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
2857fcf3ce44SJohn Forte 		if (method & iSCSIDiscoveryMethodISNS) {
2858fcf3ce44SJohn Forte 			initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2859fcf3ce44SJohn Forte 			    KM_SLEEP);
2860fcf3ce44SJohn Forte 			if (persistent_initiator_name_get(initiator_node_name,
2861fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2862fcf3ce44SJohn Forte 				kmem_free(initiator_node_name,
2863fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN);
2864fcf3ce44SJohn Forte 				initiator_node_name = NULL;
2865fcf3ce44SJohn Forte 				rtn = EIO;
2866fcf3ce44SJohn Forte 				break;
2867fcf3ce44SJohn Forte 			}
2868fcf3ce44SJohn Forte 			if (strlen(initiator_node_name) == 0) {
2869fcf3ce44SJohn Forte 				kmem_free(initiator_node_name,
2870fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN);
2871fcf3ce44SJohn Forte 				initiator_node_name = NULL;
2872fcf3ce44SJohn Forte 				rtn = EIO;
2873fcf3ce44SJohn Forte 				break;
2874fcf3ce44SJohn Forte 			}
2875fcf3ce44SJohn Forte 
2876fcf3ce44SJohn Forte 			initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2877fcf3ce44SJohn Forte 			    KM_SLEEP);
2878fcf3ce44SJohn Forte 			if (persistent_alias_name_get(initiator_node_alias,
2879fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2880fcf3ce44SJohn Forte 				initiator_node_alias[0] = '\0';
2881fcf3ce44SJohn Forte 			}
2882fcf3ce44SJohn Forte 
2883fcf3ce44SJohn Forte 			/*
2884fcf3ce44SJohn Forte 			 * Register this initiator node against this iSNS
2885fcf3ce44SJohn Forte 			 * server.
2886fcf3ce44SJohn Forte 			 */
2887fcf3ce44SJohn Forte 			(void) isns_reg_one_server(&e, ihp->hba_isid,
2888fcf3ce44SJohn Forte 			    (uint8_t *)initiator_node_name,
2889fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN,
2890fcf3ce44SJohn Forte 			    (uint8_t *)initiator_node_alias,
2891fcf3ce44SJohn Forte 			    ISCSI_MAX_NAME_LEN,
2892fcf3ce44SJohn Forte 			    ISNS_INITIATOR_NODE_TYPE,
2893fcf3ce44SJohn Forte 			    isns_scn_callback);
2894fcf3ce44SJohn Forte 
2895fcf3ce44SJohn Forte 			iscsid_do_isns_query_one_server(ihp, &e);
2896fcf3ce44SJohn Forte 
2897fcf3ce44SJohn Forte 			iscsid_addr_to_sockaddr(e.e_insize,
2898fcf3ce44SJohn Forte 			    &e.e_u, e.e_port, &addr_dsc.sin);
2899fcf3ce44SJohn Forte 
2900fcf3ce44SJohn Forte 			(void) iscsid_login_tgt(ihp, NULL,
2901fcf3ce44SJohn Forte 			    iSCSIDiscoveryMethodISNS,
2902fcf3ce44SJohn Forte 			    &addr_dsc.sin);
2903fcf3ce44SJohn Forte 
2904fcf3ce44SJohn Forte 			/* Done using the name and alias - free them. */
2905fcf3ce44SJohn Forte 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
2906fcf3ce44SJohn Forte 			initiator_node_name = NULL;
2907fcf3ce44SJohn Forte 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
2908fcf3ce44SJohn Forte 			initiator_node_alias = NULL;
2909fcf3ce44SJohn Forte 		}
2910fcf3ce44SJohn Forte 		break;
2911fcf3ce44SJohn Forte 
2912fcf3ce44SJohn Forte 	/*
2913fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_ADDR_SET:
2914fcf3ce44SJohn Forte 	 */
2915fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_ADDR_SET:
2916fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2917fcf3ce44SJohn Forte 			rtn = EFAULT;
2918fcf3ce44SJohn Forte 			break;
2919fcf3ce44SJohn Forte 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2920fcf3ce44SJohn Forte 			rtn = EINVAL;
2921fcf3ce44SJohn Forte 			break;
2922fcf3ce44SJohn Forte 		}
2923fcf3ce44SJohn Forte 
2924fcf3ce44SJohn Forte 		if (e.e_oid == ISCSI_OID_NOTSET) {
2925fcf3ce44SJohn Forte 			mutex_enter(&iscsi_oid_mutex);
2926fcf3ce44SJohn Forte 			e.e_oid = iscsi_oid++;
2927fcf3ce44SJohn Forte 			mutex_exit(&iscsi_oid_mutex);
2928fcf3ce44SJohn Forte 		}
2929fcf3ce44SJohn Forte 
2930fcf3ce44SJohn Forte 		if (persistent_disc_addr_set(&e) == B_FALSE) {
2931fcf3ce44SJohn Forte 			rtn = EIO;
2932fcf3ce44SJohn Forte 			break;
2933fcf3ce44SJohn Forte 		}
2934fcf3ce44SJohn Forte 
2935fcf3ce44SJohn Forte 		/*
2936fcf3ce44SJohn Forte 		 * If Send Targets discovery is enabled, then kickoff
2937fcf3ce44SJohn Forte 		 * discovery of the targets advertised by the recently
2938fcf3ce44SJohn Forte 		 * added discovery address.
2939fcf3ce44SJohn Forte 		 */
2940fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
2941fcf3ce44SJohn Forte 		if (method & iSCSIDiscoveryMethodSendTargets) {
2942fcf3ce44SJohn Forte 
2943fcf3ce44SJohn Forte 			iscsid_addr_to_sockaddr(e.e_insize,
2944fcf3ce44SJohn Forte 			    &e.e_u, e.e_port, &addr_dsc.sin);
2945fcf3ce44SJohn Forte 			iscsid_do_sendtgts(&e);
2946fcf3ce44SJohn Forte 			(void) iscsid_login_tgt(ihp, NULL,
2947fcf3ce44SJohn Forte 			    iSCSIDiscoveryMethodSendTargets,
2948fcf3ce44SJohn Forte 			    &addr_dsc.sin);
2949fcf3ce44SJohn Forte 
2950fcf3ce44SJohn Forte 		}
2951fcf3ce44SJohn Forte 		break;
2952fcf3ce44SJohn Forte 
2953fcf3ce44SJohn Forte 	/*
2954fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_ADDR_LIST_GET
2955fcf3ce44SJohn Forte 	 */
2956fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_ADDR_LIST_GET:
2957fcf3ce44SJohn Forte 		/* copyin user args */
2958fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2959fcf3ce44SJohn Forte 			rtn = EFAULT;
2960fcf3ce44SJohn Forte 			break;
2961fcf3ce44SJohn Forte 		}
2962fcf3ce44SJohn Forte 
2963fcf3ce44SJohn Forte 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2964fcf3ce44SJohn Forte 			rtn = EINVAL;
2965fcf3ce44SJohn Forte 			break;
2966fcf3ce44SJohn Forte 		}
2967fcf3ce44SJohn Forte 
2968fcf3ce44SJohn Forte 		list_space = sizeof (iscsi_addr_list_t);
2969fcf3ce44SJohn Forte 		if (ial.al_in_cnt != 0) {
2970fcf3ce44SJohn Forte 			list_space += (sizeof (iscsi_addr_t) *
2971fcf3ce44SJohn Forte 			    (ial.al_in_cnt - 1));
2972fcf3ce44SJohn Forte 		}
2973fcf3ce44SJohn Forte 
2974fcf3ce44SJohn Forte 		ialp = kmem_zalloc(list_space, KM_SLEEP);
2975fcf3ce44SJohn Forte 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
2976fcf3ce44SJohn Forte 
2977fcf3ce44SJohn Forte 		void_p = NULL;
2978fcf3ce44SJohn Forte 		ialp->al_out_cnt = 0;
2979fcf3ce44SJohn Forte 		persistent_disc_addr_lock();
2980fcf3ce44SJohn Forte 		while (persistent_disc_addr_next(&void_p, &e) == B_TRUE) {
2981fcf3ce44SJohn Forte 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2982fcf3ce44SJohn Forte 				int		i = ialp->al_out_cnt;
2983fcf3ce44SJohn Forte 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
2984fcf3ce44SJohn Forte 
2985fcf3ce44SJohn Forte 				addr->a_port = e.e_port;
2986fcf3ce44SJohn Forte 				addr->a_addr.i_insize = e.e_insize;
2987fcf3ce44SJohn Forte 				addr->a_oid = e.e_oid;
2988fcf3ce44SJohn Forte 
2989fcf3ce44SJohn Forte 				if (e.e_insize == sizeof (struct in_addr)) {
2990fcf3ce44SJohn Forte 					/* IPv4 */
2991fcf3ce44SJohn Forte 					addr->a_addr.i_addr.in4.s_addr =
2992fcf3ce44SJohn Forte 					    e.e_u.u_in4.s_addr;
2993fcf3ce44SJohn Forte 				} else if (e.e_insize ==
2994fcf3ce44SJohn Forte 					    sizeof (struct in6_addr)) {
2995fcf3ce44SJohn Forte 					/* IPv6 */
2996fcf3ce44SJohn Forte 					bcopy(e.e_u.u_in6.s6_addr,
2997fcf3ce44SJohn Forte 					    addr->a_addr.i_addr.in6.s6_addr,
2998fcf3ce44SJohn Forte 					    16);
2999fcf3ce44SJohn Forte 				}
3000fcf3ce44SJohn Forte 			}
3001fcf3ce44SJohn Forte 			ialp->al_out_cnt++;
3002fcf3ce44SJohn Forte 		}
3003fcf3ce44SJohn Forte 		persistent_disc_addr_unlock();
3004fcf3ce44SJohn Forte 
3005fcf3ce44SJohn Forte 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
3006fcf3ce44SJohn Forte 		kmem_free(ialp, list_space);
3007fcf3ce44SJohn Forte 		break;
3008fcf3ce44SJohn Forte 
3009fcf3ce44SJohn Forte 	/*
3010fcf3ce44SJohn Forte 	 * ISCSI_ISNS_SERVER_ADDR_LIST_GET
3011fcf3ce44SJohn Forte 	 */
3012fcf3ce44SJohn Forte 	case ISCSI_ISNS_SERVER_ADDR_LIST_GET:
3013fcf3ce44SJohn Forte 		/* copyin user args */
3014fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
3015fcf3ce44SJohn Forte 			rtn = EFAULT;
3016fcf3ce44SJohn Forte 			break;
3017fcf3ce44SJohn Forte 		}
3018fcf3ce44SJohn Forte 
3019fcf3ce44SJohn Forte 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
3020fcf3ce44SJohn Forte 			rtn = EINVAL;
3021fcf3ce44SJohn Forte 			break;
3022fcf3ce44SJohn Forte 		}
3023fcf3ce44SJohn Forte 
3024fcf3ce44SJohn Forte 		list_space = sizeof (iscsi_addr_list_t);
3025fcf3ce44SJohn Forte 		if (ial.al_in_cnt != 0) {
3026fcf3ce44SJohn Forte 			list_space += (sizeof (iscsi_addr_t) *
3027fcf3ce44SJohn Forte 			    (ial.al_in_cnt - 1));
3028fcf3ce44SJohn Forte 		}
3029fcf3ce44SJohn Forte 
3030fcf3ce44SJohn Forte 		ialp = kmem_zalloc(list_space, KM_SLEEP);
3031fcf3ce44SJohn Forte 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
3032fcf3ce44SJohn Forte 
3033fcf3ce44SJohn Forte 		void_p = NULL;
3034fcf3ce44SJohn Forte 		ialp->al_out_cnt = 0;
3035fcf3ce44SJohn Forte 		persistent_isns_addr_lock();
3036fcf3ce44SJohn Forte 		while (persistent_isns_addr_next(&void_p, &e) == B_TRUE) {
3037fcf3ce44SJohn Forte 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
3038fcf3ce44SJohn Forte 				int		i = ialp->al_out_cnt;
3039fcf3ce44SJohn Forte 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
3040fcf3ce44SJohn Forte 
3041fcf3ce44SJohn Forte 				addr->a_port = e.e_port;
3042fcf3ce44SJohn Forte 				addr->a_addr.i_insize = e.e_insize;
3043fcf3ce44SJohn Forte 				if (e.e_insize == sizeof (struct in_addr)) {
3044fcf3ce44SJohn Forte 					/* IPv4 */
3045fcf3ce44SJohn Forte 					addr->a_addr.i_addr.in4.s_addr =
3046fcf3ce44SJohn Forte 					    e.e_u.u_in4.s_addr;
3047fcf3ce44SJohn Forte 				} else if (e.e_insize ==
3048fcf3ce44SJohn Forte 					    sizeof (struct in6_addr)) {
3049fcf3ce44SJohn Forte 					/* IPv6 */
3050fcf3ce44SJohn Forte 					bcopy(e.e_u.u_in6.s6_addr,
3051fcf3ce44SJohn Forte 					    addr->a_addr.i_addr.in6.s6_addr,
3052fcf3ce44SJohn Forte 					    16);
3053fcf3ce44SJohn Forte 				}
3054fcf3ce44SJohn Forte 			}
3055fcf3ce44SJohn Forte 			ialp->al_out_cnt++;
3056fcf3ce44SJohn Forte 		}
3057fcf3ce44SJohn Forte 		persistent_isns_addr_unlock();
3058fcf3ce44SJohn Forte 
3059fcf3ce44SJohn Forte 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
3060fcf3ce44SJohn Forte 		kmem_free(ialp, list_space);
3061fcf3ce44SJohn Forte 		break;
3062fcf3ce44SJohn Forte 
3063fcf3ce44SJohn Forte 	/*
3064fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_ADDR_CLEAR:
3065fcf3ce44SJohn Forte 	 */
3066fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_ADDR_CLEAR:
3067fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3068fcf3ce44SJohn Forte 			rtn = EFAULT;
3069fcf3ce44SJohn Forte 			break;
3070fcf3ce44SJohn Forte 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3071fcf3ce44SJohn Forte 			rtn = EINVAL;
3072fcf3ce44SJohn Forte 			break;
3073fcf3ce44SJohn Forte 		}
3074fcf3ce44SJohn Forte 
3075fcf3ce44SJohn Forte 		iscsid_addr_to_sockaddr(e.e_insize,
3076fcf3ce44SJohn Forte 		    &e.e_u, e.e_port, &addr_dsc.sin);
3077fcf3ce44SJohn Forte 
3078aff4bce5Syi zhang - Sun Microsystems - Beijing China 		/* If discovery in progress, try few times before return busy */
3079aff4bce5Syi zhang - Sun Microsystems - Beijing China 		retry = 0;
3080aff4bce5Syi zhang - Sun Microsystems - Beijing China 		mutex_enter(&ihp->hba_discovery_events_mutex);
3081aff4bce5Syi zhang - Sun Microsystems - Beijing China 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3082aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (++retry == 5) {
3083aff4bce5Syi zhang - Sun Microsystems - Beijing China 				rtn = EBUSY;
3084aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
3085aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
3086aff4bce5Syi zhang - Sun Microsystems - Beijing China 			mutex_exit(&ihp->hba_discovery_events_mutex);
3087aff4bce5Syi zhang - Sun Microsystems - Beijing China 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3088aff4bce5Syi zhang - Sun Microsystems - Beijing China 			mutex_enter(&ihp->hba_discovery_events_mutex);
3089aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3090aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3091aff4bce5Syi zhang - Sun Microsystems - Beijing China 		/*
3092aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 * Clear discovery address first, so that any bus config
3093aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 * will ignore this discovery address
3094aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 */
3095aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (rtn == 0 && persistent_disc_addr_clear(&e) == B_FALSE) {
3096aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EIO;
3097aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3098aff4bce5Syi zhang - Sun Microsystems - Beijing China 		mutex_exit(&ihp->hba_discovery_events_mutex);
3099aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3100aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (rtn != 0) {
3101aff4bce5Syi zhang - Sun Microsystems - Beijing China 			break;
3102aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3103fcf3ce44SJohn Forte 		/* Attempt to logout of associated targets */
3104fcf3ce44SJohn Forte 		if (iscsid_del(ihp, NULL,
3105fcf3ce44SJohn Forte 		    iSCSIDiscoveryMethodSendTargets, &addr_dsc.sin) ==
3106aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    B_FALSE) {
3107aff4bce5Syi zhang - Sun Microsystems - Beijing China 			/* Failure!, restore the discovery addr. */
3108aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (persistent_disc_addr_set(&e) == B_FALSE) {
3109aff4bce5Syi zhang - Sun Microsystems - Beijing China 				cmn_err(CE_WARN, "Failed to restore sendtgt "
3110aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    "discovery address after logout associated "
3111aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    "targets failures.");
3112fcf3ce44SJohn Forte 			}
3113fcf3ce44SJohn Forte 			rtn = EBUSY;
3114fcf3ce44SJohn Forte 		}
3115fcf3ce44SJohn Forte 		break;
3116fcf3ce44SJohn Forte 
3117fcf3ce44SJohn Forte 	/*
3118fcf3ce44SJohn Forte 	 * ISCSI_ISNS_SERVER_CLEAR:
3119fcf3ce44SJohn Forte 	 */
3120fcf3ce44SJohn Forte 	case ISCSI_ISNS_SERVER_ADDR_CLEAR:
3121fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3122fcf3ce44SJohn Forte 			rtn = EFAULT;
3123fcf3ce44SJohn Forte 			break;
3124fcf3ce44SJohn Forte 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3125fcf3ce44SJohn Forte 			rtn = EINVAL;
3126fcf3ce44SJohn Forte 			break;
3127fcf3ce44SJohn Forte 		}
3128fcf3ce44SJohn Forte 
3129fcf3ce44SJohn Forte 		iscsid_addr_to_sockaddr(e.e_insize,
3130fcf3ce44SJohn Forte 		    &e.e_u, e.e_port, &addr_dsc.sin);
3131fcf3ce44SJohn Forte 
3132aff4bce5Syi zhang - Sun Microsystems - Beijing China 		/* If discovery in progress, try few times before return busy */
3133aff4bce5Syi zhang - Sun Microsystems - Beijing China 		retry = 0;
3134aff4bce5Syi zhang - Sun Microsystems - Beijing China 		mutex_enter(&ihp->hba_discovery_events_mutex);
3135aff4bce5Syi zhang - Sun Microsystems - Beijing China 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3136aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (++retry == 5) {
3137aff4bce5Syi zhang - Sun Microsystems - Beijing China 				rtn = EBUSY;
3138aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
3139aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
3140aff4bce5Syi zhang - Sun Microsystems - Beijing China 			mutex_exit(&ihp->hba_discovery_events_mutex);
3141aff4bce5Syi zhang - Sun Microsystems - Beijing China 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3142aff4bce5Syi zhang - Sun Microsystems - Beijing China 			mutex_enter(&ihp->hba_discovery_events_mutex);
3143aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3144aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3145aff4bce5Syi zhang - Sun Microsystems - Beijing China 		/*
3146aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 * Clear isns server address first, so that any bus config
3147aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 * will ignore any target registerd on this isns server
3148aff4bce5Syi zhang - Sun Microsystems - Beijing China 		 */
3149aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (rtn == 0 && persistent_isns_addr_clear(&e) == B_FALSE) {
3150aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EIO;
3151aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3152aff4bce5Syi zhang - Sun Microsystems - Beijing China 		mutex_exit(&ihp->hba_discovery_events_mutex);
3153aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3154aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (rtn != 0) {
3155aff4bce5Syi zhang - Sun Microsystems - Beijing China 			break;
3156aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3157aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3158fcf3ce44SJohn Forte 		/* Attempt logout of associated targets */
3159fcf3ce44SJohn Forte 		if (iscsid_del(ihp, NULL, iSCSIDiscoveryMethodISNS,
3160aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    &addr_dsc.sin) == B_FALSE) {
3161aff4bce5Syi zhang - Sun Microsystems - Beijing China 			/* Failure!, restore the isns server addr. */
3162fcf3ce44SJohn Forte 
3163aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (persistent_isns_addr_set(&e) == B_FALSE) {
3164aff4bce5Syi zhang - Sun Microsystems - Beijing China 				cmn_err(CE_WARN, "Failed to restore isns server"
3165aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    " address after logout associated targets"
3166aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    " failures.");
3167fcf3ce44SJohn Forte 			}
3168aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EBUSY;
3169aff4bce5Syi zhang - Sun Microsystems - Beijing China 		} else {
3170fcf3ce44SJohn Forte 			method = persistent_disc_meth_get();
3171fcf3ce44SJohn Forte 			if (method & iSCSIDiscoveryMethodISNS) {
3172fcf3ce44SJohn Forte 				boolean_t is_last_isns_server_b =
3173fcf3ce44SJohn Forte 				    B_FALSE;
3174fcf3ce44SJohn Forte 				int isns_server_count = 0;
3175fcf3ce44SJohn Forte 				void *void_p = NULL;
3176fcf3ce44SJohn Forte 
3177fcf3ce44SJohn Forte 				/*
3178fcf3ce44SJohn Forte 				 * Check if the last iSNS server's been
3179fcf3ce44SJohn Forte 				 * removed.
3180fcf3ce44SJohn Forte 				 */
3181fcf3ce44SJohn Forte 				{
3182fcf3ce44SJohn Forte 					entry_t tmp_e;
3183fcf3ce44SJohn Forte 					persistent_isns_addr_lock();
3184fcf3ce44SJohn Forte 					while (persistent_isns_addr_next(
3185fcf3ce44SJohn Forte 					    &void_p, &tmp_e) == B_TRUE) {
3186fcf3ce44SJohn Forte 						isns_server_count++;
3187fcf3ce44SJohn Forte 					}
3188fcf3ce44SJohn Forte 				}
3189fcf3ce44SJohn Forte 				persistent_isns_addr_unlock();
3190fcf3ce44SJohn Forte 				if (isns_server_count == 0) {
3191fcf3ce44SJohn Forte 					is_last_isns_server_b = B_TRUE;
3192fcf3ce44SJohn Forte 				}
3193fcf3ce44SJohn Forte 
3194fcf3ce44SJohn Forte 				/*
3195fcf3ce44SJohn Forte 				 * Deregister this node from this iSNS
3196fcf3ce44SJohn Forte 				 * server.
3197fcf3ce44SJohn Forte 				 */
3198fcf3ce44SJohn Forte 				initiator_node_name = kmem_zalloc(
3199fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN, KM_SLEEP);
3200fcf3ce44SJohn Forte 				if (persistent_initiator_name_get(
3201fcf3ce44SJohn Forte 				    initiator_node_name,
3202fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN) == B_TRUE) {
3203fcf3ce44SJohn Forte 
3204fcf3ce44SJohn Forte 					if (strlen(initiator_node_name) > 0) {
3205fcf3ce44SJohn Forte 						(void) isns_dereg_one_server(
3206fcf3ce44SJohn Forte 						    &e, (uint8_t *)
3207fcf3ce44SJohn Forte 						    initiator_node_name,
3208fcf3ce44SJohn Forte 						    is_last_isns_server_b);
3209fcf3ce44SJohn Forte 					}
3210fcf3ce44SJohn Forte 				}
3211fcf3ce44SJohn Forte 				kmem_free(initiator_node_name,
3212fcf3ce44SJohn Forte 				    ISCSI_MAX_NAME_LEN);
3213fcf3ce44SJohn Forte 				initiator_node_name = NULL;
3214fcf3ce44SJohn Forte 			}
3215fcf3ce44SJohn Forte 		}
3216fcf3ce44SJohn Forte 		break;
3217fcf3ce44SJohn Forte 
3218fcf3ce44SJohn Forte 	/*
3219fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_SET -
3220fcf3ce44SJohn Forte 	 */
3221fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_SET:
3222fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3223fcf3ce44SJohn Forte 			rtn = EFAULT;
3224fcf3ce44SJohn Forte 			break;
3225fcf3ce44SJohn Forte 		}
3226fcf3ce44SJohn Forte 
3227fcf3ce44SJohn Forte 		if (persistent_disc_meth_set(method) == B_FALSE) {
3228fcf3ce44SJohn Forte 			rtn = EIO;
3229fcf3ce44SJohn Forte 		} else {
32307f848965Sbing zhao - Sun Microsystems - Beijing China 			(void) iscsid_enable_discovery(ihp, method, B_FALSE);
3231fcf3ce44SJohn Forte 			iscsid_poke_discovery(ihp, method);
3232fcf3ce44SJohn Forte 			(void) iscsid_login_tgt(ihp, NULL, method, NULL);
3233fcf3ce44SJohn Forte 		}
3234fcf3ce44SJohn Forte 		break;
3235fcf3ce44SJohn Forte 
3236fcf3ce44SJohn Forte 	/*
3237fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_GET -
3238fcf3ce44SJohn Forte 	 */
3239fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_GET:
3240fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
3241fcf3ce44SJohn Forte 		rtn = ddi_copyout(&method, (caddr_t)arg,
3242fcf3ce44SJohn Forte 		    sizeof (method), mode);
3243fcf3ce44SJohn Forte 		break;
3244fcf3ce44SJohn Forte 
3245fcf3ce44SJohn Forte 	/*
3246fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_CLEAR -
3247fcf3ce44SJohn Forte 	 */
3248fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_CLEAR:
3249fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3250fcf3ce44SJohn Forte 			rtn = EFAULT;
3251fcf3ce44SJohn Forte 			break;
3252fcf3ce44SJohn Forte 		}
3253fcf3ce44SJohn Forte 
3254c21c6c26Syi zhang - Sun Microsystems - Beijing China 		/* If discovery in progress, try few times before return busy */
3255c21c6c26Syi zhang - Sun Microsystems - Beijing China 		retry = 0;
3256c21c6c26Syi zhang - Sun Microsystems - Beijing China 		mutex_enter(&ihp->hba_discovery_events_mutex);
3257c21c6c26Syi zhang - Sun Microsystems - Beijing China 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3258c21c6c26Syi zhang - Sun Microsystems - Beijing China 			if (++retry == 5) {
3259c21c6c26Syi zhang - Sun Microsystems - Beijing China 				rtn = EBUSY;
3260c21c6c26Syi zhang - Sun Microsystems - Beijing China 				break;
3261c21c6c26Syi zhang - Sun Microsystems - Beijing China 			}
3262c21c6c26Syi zhang - Sun Microsystems - Beijing China 			mutex_exit(&ihp->hba_discovery_events_mutex);
3263aff4bce5Syi zhang - Sun Microsystems - Beijing China 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3264c21c6c26Syi zhang - Sun Microsystems - Beijing China 			mutex_enter(&ihp->hba_discovery_events_mutex);
3265fcf3ce44SJohn Forte 		}
3266fcf3ce44SJohn Forte 
3267fcf3ce44SJohn Forte 		/*
3268c21c6c26Syi zhang - Sun Microsystems - Beijing China 		 * Clear discovery first, so that any bus config or
3269c21c6c26Syi zhang - Sun Microsystems - Beijing China 		 * discovery requests will ignore this discovery method
3270fcf3ce44SJohn Forte 		 */
3271c21c6c26Syi zhang - Sun Microsystems - Beijing China 		if (rtn == 0 && persistent_disc_meth_clear(method) == B_FALSE) {
3272fcf3ce44SJohn Forte 			rtn = EIO;
3273c21c6c26Syi zhang - Sun Microsystems - Beijing China 		}
3274c21c6c26Syi zhang - Sun Microsystems - Beijing China 		mutex_exit(&ihp->hba_discovery_events_mutex);
3275c21c6c26Syi zhang - Sun Microsystems - Beijing China 
3276c21c6c26Syi zhang - Sun Microsystems - Beijing China 		if (rtn != 0) {
3277fcf3ce44SJohn Forte 			break;
3278fcf3ce44SJohn Forte 		}
3279c21c6c26Syi zhang - Sun Microsystems - Beijing China 
3280c21c6c26Syi zhang - Sun Microsystems - Beijing China 		/* Attempt to logout from all associated targets */
3281c21c6c26Syi zhang - Sun Microsystems - Beijing China 		if (iscsid_disable_discovery(ihp, method) == B_FALSE) {
3282c21c6c26Syi zhang - Sun Microsystems - Beijing China 			/* Failure!, reset the discovery */
3283c21c6c26Syi zhang - Sun Microsystems - Beijing China 			if (persistent_disc_meth_set(method) == B_FALSE) {
3284c21c6c26Syi zhang - Sun Microsystems - Beijing China 				cmn_err(CE_WARN, "Failed to reset discovery "
3285c21c6c26Syi zhang - Sun Microsystems - Beijing China 				    "method after discovery disable failure.");
3286c21c6c26Syi zhang - Sun Microsystems - Beijing China 			}
3287c21c6c26Syi zhang - Sun Microsystems - Beijing China 			rtn = EBUSY;
3288c21c6c26Syi zhang - Sun Microsystems - Beijing China 		}
3289fcf3ce44SJohn Forte 		break;
3290fcf3ce44SJohn Forte 
3291fcf3ce44SJohn Forte 	/*
3292fcf3ce44SJohn Forte 	 * ISCSI_DISCOVERY_PROPS -
3293fcf3ce44SJohn Forte 	 */
3294fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_PROPS:
3295fcf3ce44SJohn Forte 		iscsid_props(&discovery_props);
3296fcf3ce44SJohn Forte 		if (ddi_copyout(&discovery_props, (caddr_t)arg,
3297fcf3ce44SJohn Forte 		    sizeof (discovery_props), mode))
3298fcf3ce44SJohn Forte 			rtn = EFAULT;
3299fcf3ce44SJohn Forte 		break;
3300fcf3ce44SJohn Forte 
3301fcf3ce44SJohn Forte 	/*
3302fcf3ce44SJohn Forte 	 * ISCSI_LUN_OID_LIST --
3303fcf3ce44SJohn Forte 	 */
3304fcf3ce44SJohn Forte 	case ISCSI_LUN_OID_LIST_GET:
3305fcf3ce44SJohn Forte 		ll = (iscsi_lun_list_t *)kmem_alloc(sizeof (*ll), KM_SLEEP);
3306fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, ll, sizeof (*ll), mode)) {
3307fcf3ce44SJohn Forte 			rtn = EFAULT;
3308fcf3ce44SJohn Forte 			kmem_free(ll, sizeof (*ll));
3309fcf3ce44SJohn Forte 			break;
3310fcf3ce44SJohn Forte 		}
3311fcf3ce44SJohn Forte 
3312fcf3ce44SJohn Forte 		if (ll->ll_vers != ISCSI_INTERFACE_VERSION) {
3313fcf3ce44SJohn Forte 			rtn = EINVAL;
3314fcf3ce44SJohn Forte 			kmem_free(ll, sizeof (*ll));
3315fcf3ce44SJohn Forte 			break;
3316fcf3ce44SJohn Forte 		}
3317fcf3ce44SJohn Forte 
3318fcf3ce44SJohn Forte 		/*
3319fcf3ce44SJohn Forte 		 * Find out how much space the user has allocated in their
3320fcf3ce44SJohn Forte 		 * structure. Match the same space for our structure.
3321fcf3ce44SJohn Forte 		 */
3322fcf3ce44SJohn Forte 		lun_sz = sizeof (iscsi_lun_list_t);
3323fcf3ce44SJohn Forte 		if (ll->ll_in_cnt > 0) {
3324fcf3ce44SJohn Forte 			lun_sz += (ll->ll_in_cnt - 1) * sizeof (iscsi_if_lun_t);
3325fcf3ce44SJohn Forte 		}
3326fcf3ce44SJohn Forte 
3327fcf3ce44SJohn Forte 		llp = kmem_zalloc(lun_sz, KM_SLEEP);
3328fcf3ce44SJohn Forte 		bcopy(ll, llp, sizeof (*ll));
3329fcf3ce44SJohn Forte 		kmem_free(ll, sizeof (*ll));
3330fcf3ce44SJohn Forte 
3331fcf3ce44SJohn Forte 		/*
3332fcf3ce44SJohn Forte 		 * Check to see if oid references a target-param oid.  If so,
3333fcf3ce44SJohn Forte 		 * find the associated  session oid before getting lu list.
3334fcf3ce44SJohn Forte 		 */
3335fcf3ce44SJohn Forte 		if (iscsi_targetparam_get_name(llp->ll_tgt_oid) != NULL) {
3336fcf3ce44SJohn Forte 			for (isp = ihp->hba_sess_list; isp;
3337fcf3ce44SJohn Forte 			    isp = isp->sess_next) {
3338fcf3ce44SJohn Forte 				if (isp->sess_target_oid == llp->ll_tgt_oid) {
3339fcf3ce44SJohn Forte 					target_oid  = isp->sess_oid;
3340fcf3ce44SJohn Forte 					break;
3341fcf3ce44SJohn Forte 				}
3342fcf3ce44SJohn Forte 			}
3343fcf3ce44SJohn Forte 		} else {
3344fcf3ce44SJohn Forte 			target_oid = llp->ll_tgt_oid;
3345fcf3ce44SJohn Forte 		}
3346fcf3ce44SJohn Forte 
3347fcf3ce44SJohn Forte 
3348fcf3ce44SJohn Forte 		/*
3349fcf3ce44SJohn Forte 		 * Look at the LUNs attached to the specified target. If there
3350fcf3ce44SJohn Forte 		 * is space in the user structure save that information locally.
3351fcf3ce44SJohn Forte 		 * Always add up the count to the total. By always adding
3352fcf3ce44SJohn Forte 		 * the count this code can be used if ll_in_cnt == 0 and
3353fcf3ce44SJohn Forte 		 * the user just wishes to know the appropriate size to
3354fcf3ce44SJohn Forte 		 * allocate.
3355fcf3ce44SJohn Forte 		 */
3356fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3357fcf3ce44SJohn Forte 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
3358fcf3ce44SJohn Forte 			if ((llp->ll_all_tgts == B_FALSE) &&
3359fcf3ce44SJohn Forte 			    (isp->sess_oid != target_oid)) {
3360fcf3ce44SJohn Forte 				continue;
3361fcf3ce44SJohn Forte 			}
3362fcf3ce44SJohn Forte 			rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3363fcf3ce44SJohn Forte 			for (ilp = isp->sess_lun_list; ilp;
3364fcf3ce44SJohn Forte 			    ilp = ilp->lun_next) {
336591dae09cSbing zhao - Sun Microsystems - Beijing China 				if ((ilp->lun_state &
336691dae09cSbing zhao - Sun Microsystems - Beijing China 				    ISCSI_LUN_STATE_ONLINE) &&
336791dae09cSbing zhao - Sun Microsystems - Beijing China 				    !(ilp->lun_state &
336891dae09cSbing zhao - Sun Microsystems - Beijing China 				    ISCSI_LUN_STATE_INVALID)) {
3369fcf3ce44SJohn Forte 					if (llp->ll_out_cnt <
3370fcf3ce44SJohn Forte 					    llp->ll_in_cnt) {
3371fcf3ce44SJohn Forte 						iscsi_if_lun_t *lp;
3372fcf3ce44SJohn Forte 						lp = &llp->ll_luns[
3373fcf3ce44SJohn Forte 						    llp->ll_out_cnt];
3374fcf3ce44SJohn Forte 
3375fcf3ce44SJohn Forte 						lp->l_tgt_oid =
3376fcf3ce44SJohn Forte 						    isp->sess_oid;
3377fcf3ce44SJohn Forte 						lp->l_oid = ilp->lun_oid;
3378fcf3ce44SJohn Forte 						lp->l_num = ilp->lun_num;
3379fcf3ce44SJohn Forte 					}
3380fcf3ce44SJohn Forte 				llp->ll_out_cnt++;
3381fcf3ce44SJohn Forte 				}
3382fcf3ce44SJohn Forte 			}
3383fcf3ce44SJohn Forte 			rw_exit(&isp->sess_lun_list_rwlock);
3384fcf3ce44SJohn Forte 		}
3385fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
3386fcf3ce44SJohn Forte 
3387fcf3ce44SJohn Forte 		if (ddi_copyout(llp, (caddr_t)arg, lun_sz, mode)) {
3388fcf3ce44SJohn Forte 			rtn = EFAULT;
3389fcf3ce44SJohn Forte 		}
3390fcf3ce44SJohn Forte 
3391fcf3ce44SJohn Forte 		kmem_free(llp, lun_sz);
3392fcf3ce44SJohn Forte 		break;
3393fcf3ce44SJohn Forte 
3394fcf3ce44SJohn Forte 	/*
3395fcf3ce44SJohn Forte 	 * ISCSI_LUN_PROPS_GET --
3396fcf3ce44SJohn Forte 	 */
3397fcf3ce44SJohn Forte 	case ISCSI_LUN_PROPS_GET:
3398fcf3ce44SJohn Forte 		lun = (iscsi_lun_props_t *)kmem_zalloc(sizeof (*lun), KM_SLEEP);
3399fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, lun, sizeof (*lun), mode)) {
3400fcf3ce44SJohn Forte 			rtn = EFAULT;
3401fcf3ce44SJohn Forte 			kmem_free(lun, sizeof (*lun));
3402fcf3ce44SJohn Forte 			break;
3403fcf3ce44SJohn Forte 		}
3404fcf3ce44SJohn Forte 
3405fcf3ce44SJohn Forte 		if (lun->lp_vers != ISCSI_INTERFACE_VERSION) {
3406fcf3ce44SJohn Forte 			rtn = EINVAL;
3407fcf3ce44SJohn Forte 			kmem_free(lun, sizeof (*lun));
3408fcf3ce44SJohn Forte 			break;
3409fcf3ce44SJohn Forte 		}
3410fcf3ce44SJohn Forte 
3411fcf3ce44SJohn Forte 		/*
3412fcf3ce44SJohn Forte 		 * For the target specified, find the LUN specified and
3413fcf3ce44SJohn Forte 		 * return its properties
3414fcf3ce44SJohn Forte 		 */
3415fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3416fcf3ce44SJohn Forte 		rtn = iscsi_sess_get(lun->lp_tgt_oid, ihp, &isp);
3417fcf3ce44SJohn Forte 		if (rtn != 0) {
3418fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
3419fcf3ce44SJohn Forte 			rtn = EFAULT;
3420fcf3ce44SJohn Forte 			kmem_free(lun, sizeof (*lun));
3421fcf3ce44SJohn Forte 			break;
3422fcf3ce44SJohn Forte 		}
3423fcf3ce44SJohn Forte 		rtn = EINVAL;	/* Set bad rtn, correct only if found */
3424fcf3ce44SJohn Forte 		rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3425fcf3ce44SJohn Forte 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
3426fcf3ce44SJohn Forte 			if (ilp->lun_oid == lun->lp_oid) {
3427fcf3ce44SJohn Forte 				lun->lp_num	= ilp->lun_num;
3428fcf3ce44SJohn Forte 				lun->lp_status	= LunValid;
3429fcf3ce44SJohn Forte 				lun->lp_time_online = ilp->lun_time_online;
3430fcf3ce44SJohn Forte 
3431fcf3ce44SJohn Forte 				if (ilp->lun_pip != NULL) {
3432fcf3ce44SJohn Forte 					lun_dip = mdi_pi_get_client(
3433fcf3ce44SJohn Forte 					    ilp->lun_pip);
3434fcf3ce44SJohn Forte 				} else {
3435fcf3ce44SJohn Forte 					lun_dip = ilp->lun_dip;
3436fcf3ce44SJohn Forte 				}
3437fcf3ce44SJohn Forte 
3438fcf3ce44SJohn Forte 				if (lun_dip != NULL &&
343964d0f0cbSyi zhang - Sun Microsystems - Beijing China 				    ((i_ddi_devi_attached(lun_dip)) ||
344064d0f0cbSyi zhang - Sun Microsystems - Beijing China 				    (ddi_get_devstate(lun_dip) ==
344164d0f0cbSyi zhang - Sun Microsystems - Beijing China 				    DDI_DEVSTATE_UP))) {
3442fcf3ce44SJohn Forte 					(void) ddi_pathname(lun_dip,
3443fcf3ce44SJohn Forte 					    lun->lp_pathname);
3444fcf3ce44SJohn Forte 				} else {
3445fcf3ce44SJohn Forte 					/*
3446fcf3ce44SJohn Forte 					 * The LUN is not exported to the
3447fcf3ce44SJohn Forte 					 * OS yet.  It is in the process
3448fcf3ce44SJohn Forte 					 * of being added.
3449fcf3ce44SJohn Forte 					 */
3450fcf3ce44SJohn Forte 					lun->lp_status	= LunDoesNotExist;
3451fcf3ce44SJohn Forte 				}
3452fcf3ce44SJohn Forte 				bcopy(ilp->lun_vid, lun->lp_vid,
3453fcf3ce44SJohn Forte 				    sizeof (lun->lp_vid));
3454fcf3ce44SJohn Forte 				bcopy(ilp->lun_pid, lun->lp_pid,
3455fcf3ce44SJohn Forte 				    sizeof (lun->lp_pid));
3456fcf3ce44SJohn Forte 				rtn = ddi_copyout(lun, (caddr_t)arg,
3457fcf3ce44SJohn Forte 				    sizeof (*lun), mode);
3458fcf3ce44SJohn Forte 				if (rtn == -1) {
3459fcf3ce44SJohn Forte 					rtn = EFAULT;
3460fcf3ce44SJohn Forte 				}
3461fcf3ce44SJohn Forte 				break;
3462fcf3ce44SJohn Forte 			}
3463fcf3ce44SJohn Forte 		}
3464fcf3ce44SJohn Forte 		rw_exit(&isp->sess_lun_list_rwlock);
3465fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
3466fcf3ce44SJohn Forte 
3467fcf3ce44SJohn Forte 		kmem_free(lun, sizeof (*lun));
3468fcf3ce44SJohn Forte 		break;
3469fcf3ce44SJohn Forte 
3470fcf3ce44SJohn Forte 	/*
3471fcf3ce44SJohn Forte 	 * ISCSI_CONN_OID_LIST_GET --
3472fcf3ce44SJohn Forte 	 */
3473fcf3ce44SJohn Forte #define	ISCSIIOCOLGC iscsi_ioctl_conn_oid_list_get_copyout
3474fcf3ce44SJohn Forte 	case ISCSI_CONN_OID_LIST_GET:
3475fcf3ce44SJohn Forte 		{
3476fcf3ce44SJohn Forte 			iscsi_conn_list_t	*cl;
3477fcf3ce44SJohn Forte 
3478fcf3ce44SJohn Forte 			/* Asuume the worst */
3479fcf3ce44SJohn Forte 			rtn = EFAULT;
3480fcf3ce44SJohn Forte 
3481fcf3ce44SJohn Forte 			/* Copy the input argument into kernel world. */
3482fcf3ce44SJohn Forte 			cl = iscsi_ioctl_conn_oid_list_get_copyin(
3483fcf3ce44SJohn Forte 			    (caddr_t)arg,
3484fcf3ce44SJohn Forte 			    mode);
3485fcf3ce44SJohn Forte 			if (cl != NULL) {
3486fcf3ce44SJohn Forte 				if (iscsi_ioctl_conn_oid_list_get(ihp, cl) ==
3487fcf3ce44SJohn Forte 				    B_TRUE) {
3488fcf3ce44SJohn Forte 					rtn =
3489fcf3ce44SJohn Forte 					    ISCSIIOCOLGC(
3490fcf3ce44SJohn Forte 					    cl, (caddr_t)arg, mode);
3491fcf3ce44SJohn Forte 				}
3492fcf3ce44SJohn Forte 			}
3493fcf3ce44SJohn Forte 			break;
3494fcf3ce44SJohn Forte 		}
3495fcf3ce44SJohn Forte #undef ISCSIIOCOLGC
3496fcf3ce44SJohn Forte 	/*
3497fcf3ce44SJohn Forte 	 * ISCSI_CONN_OID_LIST_GET --
3498fcf3ce44SJohn Forte 	 */
3499fcf3ce44SJohn Forte 	case ISCSI_CONN_PROPS_GET:
3500fcf3ce44SJohn Forte 		{
3501fcf3ce44SJohn Forte 			iscsi_conn_props_t	*cp;
3502fcf3ce44SJohn Forte 
3503fcf3ce44SJohn Forte 			/* Asuume the worst */
3504fcf3ce44SJohn Forte 			rtn = EFAULT;
3505fcf3ce44SJohn Forte 
3506fcf3ce44SJohn Forte 			/* Copy the input argument into kernel world. */
3507fcf3ce44SJohn Forte 			cp = iscsi_ioctl_copyin(
3508fcf3ce44SJohn Forte 			    (caddr_t)arg,
3509fcf3ce44SJohn Forte 			    mode,
3510fcf3ce44SJohn Forte 			    sizeof (iscsi_conn_props_t));
3511fcf3ce44SJohn Forte 
3512fcf3ce44SJohn Forte 			if (cp != NULL) {
3513fcf3ce44SJohn Forte 				/* Get the propereties. */
3514fcf3ce44SJohn Forte 				if (iscsi_ioctl_conn_props_get(ihp, cp) ==
3515fcf3ce44SJohn Forte 				    B_TRUE) {
3516fcf3ce44SJohn Forte 					rtn =
3517fcf3ce44SJohn Forte 					    iscsi_ioctl_copyout(
3518fcf3ce44SJohn Forte 					    cp,
3519fcf3ce44SJohn Forte 					    sizeof (*cp),
3520fcf3ce44SJohn Forte 					    (caddr_t)arg,
3521fcf3ce44SJohn Forte 					    mode);
35229ad4dd06SSheng-Liang Eric Zhang 				} else {
35239ad4dd06SSheng-Liang Eric Zhang 					kmem_free(cp, sizeof (*cp));
35249ad4dd06SSheng-Liang Eric Zhang 					cp = NULL;
3525fcf3ce44SJohn Forte 				}
3526fcf3ce44SJohn Forte 			}
3527fcf3ce44SJohn Forte 			break;
3528fcf3ce44SJohn Forte 		}
3529fcf3ce44SJohn Forte 
3530fcf3ce44SJohn Forte 	/*
3531fcf3ce44SJohn Forte 	 * ISCSI_RADIUS_GET -
3532fcf3ce44SJohn Forte 	 */
3533fcf3ce44SJohn Forte 	case ISCSI_RADIUS_GET:
3534fcf3ce44SJohn Forte 	{
3535fcf3ce44SJohn Forte 		iscsi_nvfile_status_t	status;
3536fcf3ce44SJohn Forte 
3537fcf3ce44SJohn Forte 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3538fcf3ce44SJohn Forte 		    KM_SLEEP);
3539fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3540fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3541fcf3ce44SJohn Forte 			rtn = EFAULT;
3542fcf3ce44SJohn Forte 			break;
3543fcf3ce44SJohn Forte 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3544fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3545fcf3ce44SJohn Forte 			rtn = EINVAL;
3546fcf3ce44SJohn Forte 			break;
3547fcf3ce44SJohn Forte 		}
3548fcf3ce44SJohn Forte 
3549fcf3ce44SJohn Forte 		old_oid = radius->r_oid;
3550fcf3ce44SJohn Forte 
3551fcf3ce44SJohn Forte 		if (radius->r_oid == ihp->hba_oid) {
3552fcf3ce44SJohn Forte 			name = ihp->hba_name;
3553fcf3ce44SJohn Forte 		} else {
3554fcf3ce44SJohn Forte 			/*
3555fcf3ce44SJohn Forte 			 * RADIUS configuration should be done on a per
3556fcf3ce44SJohn Forte 			 * initiator basis.
3557fcf3ce44SJohn Forte 			 */
3558fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3559fcf3ce44SJohn Forte 			rtn = EINVAL;
3560fcf3ce44SJohn Forte 			break;
3561fcf3ce44SJohn Forte 		}
3562fcf3ce44SJohn Forte 
3563fcf3ce44SJohn Forte 		status = persistent_radius_get(radius);
3564fcf3ce44SJohn Forte 		if (status == ISCSI_NVFILE_SUCCESS) {
3565fcf3ce44SJohn Forte 			/*
3566fcf3ce44SJohn Forte 			 * Restore the value for overridden (and bogus) oid.
3567fcf3ce44SJohn Forte 			 */
3568fcf3ce44SJohn Forte 			radius->r_oid = old_oid;
3569fcf3ce44SJohn Forte 			rtn = ddi_copyout(radius, (caddr_t)arg,
3570fcf3ce44SJohn Forte 			    sizeof (*radius), mode);
3571fcf3ce44SJohn Forte 		} else if (status == ISCSI_NVFILE_NAMEVAL_NOT_FOUND) {
3572fcf3ce44SJohn Forte 			rtn = ENOENT;
3573fcf3ce44SJohn Forte 		} else {
3574fcf3ce44SJohn Forte 			rtn = EIO;
3575fcf3ce44SJohn Forte 		}
3576fcf3ce44SJohn Forte 		kmem_free(radius, sizeof (*radius));
3577fcf3ce44SJohn Forte 		break;
3578fcf3ce44SJohn Forte 	}
3579fcf3ce44SJohn Forte 
3580fcf3ce44SJohn Forte 	/*
3581fcf3ce44SJohn Forte 	 * ISCSI_RADIUS_SET -
3582fcf3ce44SJohn Forte 	 */
3583fcf3ce44SJohn Forte 	case ISCSI_RADIUS_SET:
3584fcf3ce44SJohn Forte 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3585fcf3ce44SJohn Forte 		    KM_SLEEP);
3586fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3587fcf3ce44SJohn Forte 			rtn = EFAULT;
3588fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3589fcf3ce44SJohn Forte 			break;
3590fcf3ce44SJohn Forte 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3591fcf3ce44SJohn Forte 			rtn = EINVAL;
3592fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3593fcf3ce44SJohn Forte 			break;
3594fcf3ce44SJohn Forte 		}
3595fcf3ce44SJohn Forte 
3596fcf3ce44SJohn Forte 		if (radius->r_oid == ihp->hba_oid) {
3597fcf3ce44SJohn Forte 			name = ihp->hba_name;
3598fcf3ce44SJohn Forte 		} else {
3599fcf3ce44SJohn Forte 			/*
3600fcf3ce44SJohn Forte 			 * RADIUS configuration should be done on a per
3601fcf3ce44SJohn Forte 			 * initiator basis.
3602fcf3ce44SJohn Forte 			 */
3603fcf3ce44SJohn Forte 			kmem_free(radius, sizeof (*radius));
3604fcf3ce44SJohn Forte 			rtn = EINVAL;
3605fcf3ce44SJohn Forte 			break;
3606fcf3ce44SJohn Forte 		}
3607fcf3ce44SJohn Forte 
3608fcf3ce44SJohn Forte 		if (persistent_radius_set(radius) == B_FALSE) {
3609fcf3ce44SJohn Forte 			rtn = EIO;
3610fcf3ce44SJohn Forte 		}
3611fcf3ce44SJohn Forte 
3612fcf3ce44SJohn Forte 		kmem_free(radius, sizeof (*radius));
3613fcf3ce44SJohn Forte 		break;
3614fcf3ce44SJohn Forte 
3615fcf3ce44SJohn Forte 	/*
3616fcf3ce44SJohn Forte 	 *  ISCSI_AUTH_GET -
3617fcf3ce44SJohn Forte 	 */
3618fcf3ce44SJohn Forte 	case ISCSI_AUTH_GET:
3619fcf3ce44SJohn Forte 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3620fcf3ce44SJohn Forte 		    KM_SLEEP);
3621fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3622fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3623fcf3ce44SJohn Forte 			rtn = EFAULT;
3624fcf3ce44SJohn Forte 			break;
3625fcf3ce44SJohn Forte 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3626fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3627fcf3ce44SJohn Forte 			rtn = EINVAL;
3628fcf3ce44SJohn Forte 			break;
3629fcf3ce44SJohn Forte 		}
3630fcf3ce44SJohn Forte 
3631fcf3ce44SJohn Forte 		old_oid = auth->a_oid;
3632fcf3ce44SJohn Forte 
3633fcf3ce44SJohn Forte 		if (auth->a_oid == ihp->hba_oid) {
3634fcf3ce44SJohn Forte 			name = ihp->hba_name;
3635fcf3ce44SJohn Forte 		} else {
3636fcf3ce44SJohn Forte 
3637fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3638fcf3ce44SJohn Forte 			/*
3639fcf3ce44SJohn Forte 			 * If the oid does represent a session check to see
3640fcf3ce44SJohn Forte 			 * if it is a target oid.  If so, return the target's
3641fcf3ce44SJohn Forte 			 * associated session.
3642fcf3ce44SJohn Forte 			 */
3643fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3644fcf3ce44SJohn Forte 			if (rtn != 0) {
3645fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3646fcf3ce44SJohn Forte 				    ihp, &isp);
3647fcf3ce44SJohn Forte 			}
3648fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
3649fcf3ce44SJohn Forte 
3650fcf3ce44SJohn Forte 			/*
3651fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
3652fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
3653fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
3654fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
3655fcf3ce44SJohn Forte 			 * for param lookup.
3656fcf3ce44SJohn Forte 			 */
3657fcf3ce44SJohn Forte 			if (rtn == 0) {
3658fcf3ce44SJohn Forte 				name = isp->sess_name;
3659fcf3ce44SJohn Forte 			} else {
3660fcf3ce44SJohn Forte 				name =
3661fcf3ce44SJohn Forte 				    iscsi_targetparam_get_name(auth->a_oid);
3662fcf3ce44SJohn Forte 			}
3663fcf3ce44SJohn Forte 		}
3664fcf3ce44SJohn Forte 
3665fcf3ce44SJohn Forte 		if (name == NULL) {
3666fcf3ce44SJohn Forte 			rtn = EFAULT;
3667fcf3ce44SJohn Forte 			break;
3668fcf3ce44SJohn Forte 		}
3669fcf3ce44SJohn Forte 
3670fcf3ce44SJohn Forte 		if (persistent_auth_get((char *)name, auth) == B_TRUE) {
3671fcf3ce44SJohn Forte 			/*
3672fcf3ce44SJohn Forte 			 * Restore the value for overridden (and bogus) oid.
3673fcf3ce44SJohn Forte 			 */
3674fcf3ce44SJohn Forte 			auth->a_oid = old_oid;
3675fcf3ce44SJohn Forte 			rtn = ddi_copyout(auth, (caddr_t)arg,
3676fcf3ce44SJohn Forte 			    sizeof (*auth), mode);
3677fcf3ce44SJohn Forte 		} else {
3678fcf3ce44SJohn Forte 			rtn = EIO;
3679fcf3ce44SJohn Forte 		}
3680fcf3ce44SJohn Forte 
3681fcf3ce44SJohn Forte 		kmem_free(auth, sizeof (*auth));
3682fcf3ce44SJohn Forte 		break;
3683fcf3ce44SJohn Forte 
3684fcf3ce44SJohn Forte 	/*
3685fcf3ce44SJohn Forte 	 *  ISCSI_AUTH_SET -
3686fcf3ce44SJohn Forte 	 */
3687fcf3ce44SJohn Forte 	case ISCSI_AUTH_SET:
3688fcf3ce44SJohn Forte 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3689fcf3ce44SJohn Forte 		    KM_SLEEP);
3690fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3691fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3692fcf3ce44SJohn Forte 			rtn = EFAULT;
3693fcf3ce44SJohn Forte 			break;
3694fcf3ce44SJohn Forte 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3695fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3696fcf3ce44SJohn Forte 			rtn = EINVAL;
3697fcf3ce44SJohn Forte 			break;
3698fcf3ce44SJohn Forte 		}
3699fcf3ce44SJohn Forte 
3700fcf3ce44SJohn Forte 		if (auth->a_oid == ihp->hba_oid) {
3701fcf3ce44SJohn Forte 			name = ihp->hba_name;
3702fcf3ce44SJohn Forte 		} else {
3703fcf3ce44SJohn Forte 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3704fcf3ce44SJohn Forte 			/*
3705fcf3ce44SJohn Forte 			 * If the oid does represent a session check to see
3706fcf3ce44SJohn Forte 			 * if it is a target oid.  If so, return the target's
3707fcf3ce44SJohn Forte 			 * associated session.
3708fcf3ce44SJohn Forte 			 */
3709fcf3ce44SJohn Forte 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3710fcf3ce44SJohn Forte 			if (rtn != 0) {
3711fcf3ce44SJohn Forte 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3712fcf3ce44SJohn Forte 				    ihp, &isp);
3713fcf3ce44SJohn Forte 			}
3714fcf3ce44SJohn Forte 			rw_exit(&ihp->hba_sess_list_rwlock);
3715fcf3ce44SJohn Forte 
3716fcf3ce44SJohn Forte 			/*
3717fcf3ce44SJohn Forte 			 * If rtn is zero then we have found an
3718fcf3ce44SJohn Forte 			 * existing session.  Use the session name to
3719fcf3ce44SJohn Forte 			 * do param lookup.  If rtn is non-zero then
3720fcf3ce44SJohn Forte 			 * create a targetparam object and use its name
3721fcf3ce44SJohn Forte 			 * for param lookup.
3722fcf3ce44SJohn Forte 			 */
3723fcf3ce44SJohn Forte 			if (rtn == 0) {
3724fcf3ce44SJohn Forte 				name = isp->sess_name;
3725fcf3ce44SJohn Forte 			} else {
3726fcf3ce44SJohn Forte 				name =
3727fcf3ce44SJohn Forte 				    iscsi_targetparam_get_name(auth->a_oid);
3728fcf3ce44SJohn Forte 				rtn = 0;
3729fcf3ce44SJohn Forte 			}
3730fcf3ce44SJohn Forte 		}
3731fcf3ce44SJohn Forte 
3732f5fc5c04SJack Meng 		if (name == NULL) {
3733f5fc5c04SJack Meng 			rtn = EFAULT;
3734f5fc5c04SJack Meng 		} else if (persistent_auth_set((char *)name, auth)
3735f5fc5c04SJack Meng 		    == B_FALSE) {
3736fcf3ce44SJohn Forte 			rtn = EIO;
3737fcf3ce44SJohn Forte 		}
3738fcf3ce44SJohn Forte 
3739fcf3ce44SJohn Forte 		kmem_free(auth, sizeof (*auth));
3740fcf3ce44SJohn Forte 		break;
3741fcf3ce44SJohn Forte 
3742fcf3ce44SJohn Forte 	/*
3743fcf3ce44SJohn Forte 	 *  ISCSI_AUTH_CLEAR -
3744fcf3ce44SJohn Forte 	 */
3745fcf3ce44SJohn Forte 	case ISCSI_AUTH_CLEAR:
3746fcf3ce44SJohn Forte 		auth = (iscsi_auth_props_t *)kmem_alloc(sizeof (*auth),
3747fcf3ce44SJohn Forte 		    KM_SLEEP);
3748fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3749fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3750fcf3ce44SJohn Forte 			rtn = EFAULT;
3751fcf3ce44SJohn Forte 			break;
3752fcf3ce44SJohn Forte 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3753fcf3ce44SJohn Forte 			kmem_free(auth, sizeof (*auth));
3754fcf3ce44SJohn Forte 			rtn = EINVAL;
3755fcf3ce44SJohn Forte 			break;
3756fcf3ce44SJohn Forte 		}
3757fcf3ce44SJohn Forte 
3758fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3759fcf3ce44SJohn Forte 		/*
3760fcf3ce44SJohn Forte 		 * If the oid does represent a session check to see
3761fcf3ce44SJohn Forte 		 * if it is a target oid.  If so, return the target's
3762fcf3ce44SJohn Forte 		 * associated session.
3763fcf3ce44SJohn Forte 		 */
3764fcf3ce44SJohn Forte 		rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3765fcf3ce44SJohn Forte 		if (rtn != 0) {
3766fcf3ce44SJohn Forte 			rtn = iscsi_sess_get_by_target(auth->a_oid, ihp, &isp);
3767fcf3ce44SJohn Forte 		}
3768fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
3769fcf3ce44SJohn Forte 
3770fcf3ce44SJohn Forte 		/*
3771fcf3ce44SJohn Forte 		 * If rtn is zero then we have found an
3772fcf3ce44SJohn Forte 		 * existing session.  Use the session name to
3773fcf3ce44SJohn Forte 		 * do param lookup.  If rtn is non-zero then
3774fcf3ce44SJohn Forte 		 * create a targetparam object and use its name
3775fcf3ce44SJohn Forte 		 * for param lookup.
3776fcf3ce44SJohn Forte 		 */
3777fcf3ce44SJohn Forte 		if (rtn == 0) {
3778fcf3ce44SJohn Forte 			name = isp->sess_name;
3779fcf3ce44SJohn Forte 		} else {
3780fcf3ce44SJohn Forte 			name =
3781fcf3ce44SJohn Forte 			    iscsi_targetparam_get_name(auth->a_oid);
3782fcf3ce44SJohn Forte 			rtn = 0;
3783b4243054SSheng-Liang Eric Zhang 			discovered = B_FALSE;
3784fcf3ce44SJohn Forte 		}
3785fcf3ce44SJohn Forte 
3786fcf3ce44SJohn Forte 		if (name == NULL) {
3787fcf3ce44SJohn Forte 			rtn = EFAULT;
3788fcf3ce44SJohn Forte 			break;
3789fcf3ce44SJohn Forte 		}
3790fcf3ce44SJohn Forte 
3791fcf3ce44SJohn Forte 		if (persistent_auth_clear((char *)name) == B_FALSE) {
3792fcf3ce44SJohn Forte 			rtn = EIO;
3793fcf3ce44SJohn Forte 		}
3794b4243054SSheng-Liang Eric Zhang 
3795b4243054SSheng-Liang Eric Zhang 		/*
3796b4243054SSheng-Liang Eric Zhang 		 * ISCSI_TARGET_PARAM_CLEAR, ISCSI_CHAP_CLEAR and
3797b4243054SSheng-Liang Eric Zhang 		 * ISCSI_AUTH_CLEAR ioctl are called sequentially to remove
3798b4243054SSheng-Liang Eric Zhang 		 * target parameters. Here, the target that is not discovered
3799b4243054SSheng-Liang Eric Zhang 		 * by initiator should be removed from the iscsi_targets list
3800b4243054SSheng-Liang Eric Zhang 		 * residing in the memory.
3801b4243054SSheng-Liang Eric Zhang 		 */
3802b4243054SSheng-Liang Eric Zhang 		if (discovered == B_FALSE) {
3803b4243054SSheng-Liang Eric Zhang 			(void) iscsi_targetparam_remove_target(auth->a_oid);
3804b4243054SSheng-Liang Eric Zhang 		}
3805b4243054SSheng-Liang Eric Zhang 
3806fcf3ce44SJohn Forte 		kmem_free(auth, sizeof (*auth));
3807fcf3ce44SJohn Forte 		break;
3808fcf3ce44SJohn Forte 
3809fcf3ce44SJohn Forte 	/*
3810fcf3ce44SJohn Forte 	 * ISCSI_DB_DUMP -
3811fcf3ce44SJohn Forte 	 */
3812fcf3ce44SJohn Forte 	case ISCSI_DB_DUMP:
3813fcf3ce44SJohn Forte 		persistent_dump_data();
3814fcf3ce44SJohn Forte 		break;
3815fcf3ce44SJohn Forte 
3816fcf3ce44SJohn Forte 	case ISCSI_USCSI:
3817fcf3ce44SJohn Forte 
3818fcf3ce44SJohn Forte #ifdef _MULTI_DATAMODEL
3819fcf3ce44SJohn Forte 		model = ddi_model_convert_from(mode & FMODELS);
3820fcf3ce44SJohn Forte 		switch (model) {
3821fcf3ce44SJohn Forte 		case DDI_MODEL_ILP32:
3822fcf3ce44SJohn Forte 
3823fcf3ce44SJohn Forte 			if (ddi_copyin((caddr_t)arg, &iu32_caller,
3824fcf3ce44SJohn Forte 			    sizeof (iscsi_uscsi32_t), mode)) {
3825fcf3ce44SJohn Forte 				rtn = EFAULT;
3826fcf3ce44SJohn Forte 				break;
3827fcf3ce44SJohn Forte 			}
3828fcf3ce44SJohn Forte 
3829fcf3ce44SJohn Forte 			/* perform conversion from 32 -> 64 */
3830fcf3ce44SJohn Forte 			iu_caller.iu_vers = iu32_caller.iu_vers;
3831fcf3ce44SJohn Forte 			iu_caller.iu_oid = iu32_caller.iu_oid;
3832fcf3ce44SJohn Forte 			iu_caller.iu_tpgt = iu32_caller.iu_tpgt;
3833fcf3ce44SJohn Forte 			iu_caller.iu_len = iu32_caller.iu_len;
3834fcf3ce44SJohn Forte 			iu_caller.iu_lun = iu32_caller.iu_lun;
3835fcf3ce44SJohn Forte 			uscsi_cmd32touscsi_cmd((&iu32_caller.iu_ucmd),
3836fcf3ce44SJohn Forte 			    (&iu_caller.iu_ucmd));
3837fcf3ce44SJohn Forte 
3838fcf3ce44SJohn Forte 			break;
3839fcf3ce44SJohn Forte 		case DDI_MODEL_NONE:
3840fcf3ce44SJohn Forte 			if (ddi_copyin((caddr_t)arg, &iu_caller,
3841fcf3ce44SJohn Forte 			    sizeof (iscsi_uscsi_t), mode)) {
3842fcf3ce44SJohn Forte 				rtn = EFAULT;
3843fcf3ce44SJohn Forte 				break;
3844fcf3ce44SJohn Forte 			}
3845fcf3ce44SJohn Forte 			break;
3846fcf3ce44SJohn Forte 		default:
3847fcf3ce44SJohn Forte 			ASSERT(FALSE);
3848fcf3ce44SJohn Forte 			rtn = EINVAL;
3849fcf3ce44SJohn Forte 			break;
3850fcf3ce44SJohn Forte 		}
3851fcf3ce44SJohn Forte #endif /* _MULTI_DATAMODEL */
3852fcf3ce44SJohn Forte 
3853fcf3ce44SJohn Forte 		/* If failures earlier break */
3854fcf3ce44SJohn Forte 		if (rtn != 0) {
3855fcf3ce44SJohn Forte 			break;
3856fcf3ce44SJohn Forte 		}
3857fcf3ce44SJohn Forte 
3858fcf3ce44SJohn Forte 		/* copy from caller to internel cmd */
3859fcf3ce44SJohn Forte 		bcopy(&iu_caller, &iu, sizeof (iu));
3860fcf3ce44SJohn Forte 
3861fcf3ce44SJohn Forte 		if (iu.iu_vers != ISCSI_INTERFACE_VERSION) {
3862fcf3ce44SJohn Forte 			rtn = EINVAL;
3863fcf3ce44SJohn Forte 			break;
3864fcf3ce44SJohn Forte 		}
3865fcf3ce44SJohn Forte 		/*
3866fcf3ce44SJohn Forte 		 * Check to see if oid references a target-param oid.  If so,
3867fcf3ce44SJohn Forte 		 * find the associated  session oid before getting lu list.
3868fcf3ce44SJohn Forte 		 */
3869fcf3ce44SJohn Forte 		if (iscsi_targetparam_get_name(iu.iu_oid) != NULL) {
3870fcf3ce44SJohn Forte 			for (isp = ihp->hba_sess_list; isp; isp =
3871fcf3ce44SJohn Forte 			    isp->sess_next) {
3872fcf3ce44SJohn Forte 				if (isp->sess_target_oid == iu.iu_oid) {
3873fcf3ce44SJohn Forte 					target_oid  = isp->sess_oid;
3874fcf3ce44SJohn Forte 					break;
3875fcf3ce44SJohn Forte 				}
3876fcf3ce44SJohn Forte 			}
3877fcf3ce44SJohn Forte 		} else {
3878fcf3ce44SJohn Forte 			target_oid = iu.iu_oid;
3879fcf3ce44SJohn Forte 		}
3880fcf3ce44SJohn Forte 
3881fcf3ce44SJohn Forte 		/* make sure we have a matching session for this command */
3882fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3883fcf3ce44SJohn Forte 		rtn = iscsi_sess_get(target_oid, ihp, &isp);
3884fcf3ce44SJohn Forte 		if (rtn != 0) {
3885fcf3ce44SJohn Forte 			rtn = iscsi_sess_get_by_target(target_oid, ihp,
3886fcf3ce44SJohn Forte 			    &isp);
3887fcf3ce44SJohn Forte 			if (rtn != 0) {
3888fcf3ce44SJohn Forte 				rw_exit(&ihp->hba_sess_list_rwlock);
3889fcf3ce44SJohn Forte 				rtn = EFAULT;
3890fcf3ce44SJohn Forte 				break;
3891fcf3ce44SJohn Forte 			}
3892fcf3ce44SJohn Forte 		}
3893fcf3ce44SJohn Forte 		/*
3894fcf3ce44SJohn Forte 		 * If a caller buffer is present allocate duplicate
3895fcf3ce44SJohn Forte 		 * kernel space and copyin caller memory.
3896fcf3ce44SJohn Forte 		 */
3897fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3898fcf3ce44SJohn Forte 			iu.iu_ucmd.uscsi_bufaddr = (caddr_t)kmem_alloc(
3899fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_buflen, KM_SLEEP);
3900fcf3ce44SJohn Forte 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_bufaddr,
3901fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_bufaddr,
3902fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_buflen, mode)) {
3903fcf3ce44SJohn Forte 				rw_exit(&ihp->hba_sess_list_rwlock);
3904fcf3ce44SJohn Forte 				rtn = EFAULT;
3905fcf3ce44SJohn Forte 				break;
3906fcf3ce44SJohn Forte 			}
3907fcf3ce44SJohn Forte 		}
3908fcf3ce44SJohn Forte 
3909fcf3ce44SJohn Forte 		/*
3910fcf3ce44SJohn Forte 		 * If a caller cdb is present allocate duplicate
3911fcf3ce44SJohn Forte 		 * kernel space and copyin caller memory.
3912fcf3ce44SJohn Forte 		 */
3913fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3914fcf3ce44SJohn Forte 			iu.iu_ucmd.uscsi_cdb = (caddr_t)kmem_alloc(
3915fcf3ce44SJohn Forte 			    iu_caller.iu_ucmd.uscsi_cdblen, KM_SLEEP);
3916fcf3ce44SJohn Forte 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_cdb,
3917fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_cdb,
3918fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_cdblen, mode)) {
3919fcf3ce44SJohn Forte 				if (iu.iu_ucmd.uscsi_buflen > 0) {
3920fcf3ce44SJohn Forte 					kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3921fcf3ce44SJohn Forte 					    iu_caller.iu_ucmd.uscsi_buflen);
3922fcf3ce44SJohn Forte 				}
3923fcf3ce44SJohn Forte 				rw_exit(&ihp->hba_sess_list_rwlock);
3924fcf3ce44SJohn Forte 				rtn = EFAULT;
3925fcf3ce44SJohn Forte 				break;
3926fcf3ce44SJohn Forte 			}
3927fcf3ce44SJohn Forte 		}
3928fcf3ce44SJohn Forte 
3929fcf3ce44SJohn Forte 		/*
3930fcf3ce44SJohn Forte 		 * If a caller request sense is present allocate
3931fcf3ce44SJohn Forte 		 * duplicate kernel space.  No need to copyin.
3932fcf3ce44SJohn Forte 		 */
3933fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3934fcf3ce44SJohn Forte 			iu.iu_ucmd.uscsi_rqbuf = (caddr_t)kmem_alloc(
3935fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_rqlen, KM_SLEEP);
3936fcf3ce44SJohn Forte 		}
3937fcf3ce44SJohn Forte 
3938fcf3ce44SJohn Forte 		/* issue passthru to io path handler */
3939fcf3ce44SJohn Forte 		rtn = iscsi_handle_passthru(isp, iu.iu_lun, &iu.iu_ucmd);
3940fcf3ce44SJohn Forte 		if (rtn != 0) {
3941fcf3ce44SJohn Forte 			rtn = EFAULT;
3942fcf3ce44SJohn Forte 		}
3943fcf3ce44SJohn Forte 
3944fcf3ce44SJohn Forte 		/*
3945fcf3ce44SJohn Forte 		 * If the caller had a buf we need to do a copyout
3946fcf3ce44SJohn Forte 		 * and free the kernel memory
3947fcf3ce44SJohn Forte 		 */
3948fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3949fcf3ce44SJohn Forte 			if (ddi_copyout(iu.iu_ucmd.uscsi_bufaddr,
3950fcf3ce44SJohn Forte 			    iu_caller.iu_ucmd.uscsi_bufaddr,
3951fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_buflen, mode) != 0) {
3952fcf3ce44SJohn Forte 				rtn = EFAULT;
3953fcf3ce44SJohn Forte 			}
3954fcf3ce44SJohn Forte 			kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3955fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_buflen);
3956fcf3ce44SJohn Forte 		}
3957fcf3ce44SJohn Forte 
3958fcf3ce44SJohn Forte 		/* We need to free kernel cdb, no need to copyout */
3959fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3960fcf3ce44SJohn Forte 			kmem_free(iu.iu_ucmd.uscsi_cdb,
3961fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_cdblen);
3962fcf3ce44SJohn Forte 		}
3963fcf3ce44SJohn Forte 
3964fcf3ce44SJohn Forte 		/*
3965fcf3ce44SJohn Forte 		 * If the caller had a request sense we need to
3966fcf3ce44SJohn Forte 		 * do a copyout and free the kernel memory
3967fcf3ce44SJohn Forte 		 */
3968fcf3ce44SJohn Forte 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3969fcf3ce44SJohn Forte 			if (ddi_copyout(iu.iu_ucmd.uscsi_rqbuf,
3970fcf3ce44SJohn Forte 			    iu_caller.iu_ucmd.uscsi_rqbuf,
3971146832dbSMilos Muzik 			    iu.iu_ucmd.uscsi_rqlen - iu.iu_ucmd.uscsi_rqresid,
3972146832dbSMilos Muzik 			    mode) != 0) {
3973fcf3ce44SJohn Forte 				rtn = EFAULT;
3974fcf3ce44SJohn Forte 			}
3975fcf3ce44SJohn Forte 			kmem_free(iu.iu_ucmd.uscsi_rqbuf,
3976fcf3ce44SJohn Forte 			    iu.iu_ucmd.uscsi_rqlen);
3977fcf3ce44SJohn Forte 		}
3978fcf3ce44SJohn Forte 
3979fcf3ce44SJohn Forte #ifdef _MULTI_DATAMODEL
3980146832dbSMilos Muzik 		switch (model = ddi_model_convert_from(mode & FMODELS)) {
3981146832dbSMilos Muzik 		case DDI_MODEL_ILP32:
3982146832dbSMilos Muzik 			if (iu.iu_ucmd.uscsi_status != 0) {
3983fcf3ce44SJohn Forte 				iu32_caller.iu_ucmd.uscsi_status =
3984fcf3ce44SJohn Forte 				    iu.iu_ucmd.uscsi_status;
3985146832dbSMilos Muzik 				iu32_caller.iu_ucmd.uscsi_rqresid =
3986146832dbSMilos Muzik 				    iu.iu_ucmd.uscsi_rqresid;
3987146832dbSMilos Muzik 			}
3988146832dbSMilos Muzik 			iu32_caller.iu_ucmd.uscsi_resid =
3989146832dbSMilos Muzik 			    iu.iu_ucmd.uscsi_resid;
3990146832dbSMilos Muzik 			if (ddi_copyout((void *)&iu32_caller, (caddr_t)arg,
3991146832dbSMilos Muzik 			    sizeof (iscsi_uscsi32_t), mode) != 0) {
3992146832dbSMilos Muzik 				rtn = EFAULT;
3993146832dbSMilos Muzik 			}
3994146832dbSMilos Muzik 			break;
3995146832dbSMilos Muzik 		case DDI_MODEL_NONE:
3996146832dbSMilos Muzik 			if (iu.iu_ucmd.uscsi_status != 0) {
3997fcf3ce44SJohn Forte 				iu_caller.iu_ucmd.uscsi_status =
3998fcf3ce44SJohn Forte 				    iu.iu_ucmd.uscsi_status;
3999146832dbSMilos Muzik 				iu_caller.iu_ucmd.uscsi_rqresid =
4000146832dbSMilos Muzik 				    iu.iu_ucmd.uscsi_rqresid;
4001146832dbSMilos Muzik 			}
4002146832dbSMilos Muzik 			iu_caller.iu_ucmd.uscsi_resid = iu.iu_ucmd.uscsi_resid;
4003146832dbSMilos Muzik 			if (ddi_copyout((void *)&iu_caller, (caddr_t)arg,
4004146832dbSMilos Muzik 			    sizeof (iscsi_uscsi_t), mode) != 0) {
4005146832dbSMilos Muzik 				rtn = EFAULT;
4006fcf3ce44SJohn Forte 			}
4007146832dbSMilos Muzik 			break;
4008146832dbSMilos Muzik 		default:
4009146832dbSMilos Muzik 			ASSERT(FALSE);
4010fcf3ce44SJohn Forte 		}
4011fcf3ce44SJohn Forte #endif /* _MULTI_DATAMODEL */
4012fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
4013fcf3ce44SJohn Forte 		break;
4014fcf3ce44SJohn Forte 
40154246c8e9SJack Meng 	case ISCSI_SMF_ONLINE:
4016fcf3ce44SJohn Forte 		if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) {
4017fcf3ce44SJohn Forte 			rtn = EFAULT;
40184246c8e9SJack Meng 			break;
4019fcf3ce44SJohn Forte 		}
40204246c8e9SJack Meng 		/* just a theoretical case */
40214246c8e9SJack Meng 		if (ihp->hba_persistent_loaded == B_FALSE) {
4022fcf3ce44SJohn Forte 			rtn = EFAULT;
40234246c8e9SJack Meng 			break;
4024fcf3ce44SJohn Forte 		}
40254246c8e9SJack Meng 
402649311b35SJack Meng 		/* doesn't need to overwrite the status anymore */
402749311b35SJack Meng 		mutex_enter(&ihp->hba_service_lock);
402849311b35SJack Meng 		if (ihp->hba_service_status_overwrite == B_TRUE) {
402949311b35SJack Meng 			ihp->hba_service_status = ISCSI_SERVICE_DISABLED;
403049311b35SJack Meng 			ihp->hba_service_status_overwrite = B_FALSE;
403149311b35SJack Meng 		}
403249311b35SJack Meng 		mutex_exit(&ihp->hba_service_lock);
403349311b35SJack Meng 
40344246c8e9SJack Meng 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_ENABLED) ==
40354246c8e9SJack Meng 		    B_FALSE) {
40364246c8e9SJack Meng 			break;
40374246c8e9SJack Meng 		}
40384246c8e9SJack Meng 
40394246c8e9SJack Meng 		rval = iscsi_door_bind(did);
40404246c8e9SJack Meng 		if (rval == B_TRUE) {
40414246c8e9SJack Meng 			rval = iscsid_start(ihp);
40424246c8e9SJack Meng 			if (rval == B_FALSE) {
40434246c8e9SJack Meng 				iscsi_door_unbind();
40444246c8e9SJack Meng 			}
40454246c8e9SJack Meng 		}
40464246c8e9SJack Meng 
40474246c8e9SJack Meng 		if (rval == B_TRUE) {
40484246c8e9SJack Meng 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
40494246c8e9SJack Meng 		} else {
40504246c8e9SJack Meng 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
40514246c8e9SJack Meng 			rtn = EFAULT;
40524246c8e9SJack Meng 		}
40534246c8e9SJack Meng 
40544246c8e9SJack Meng 		break;
40554246c8e9SJack Meng 
40564246c8e9SJack Meng 	case ISCSI_SMF_OFFLINE:
40574246c8e9SJack Meng 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_DISABLED)
40584246c8e9SJack Meng 		    == B_FALSE) {
40594246c8e9SJack Meng 			break;
40604246c8e9SJack Meng 		}
40614246c8e9SJack Meng 
40624246c8e9SJack Meng 		rval = iscsid_stop(ihp);
406308dcd69cSJack Meng 		iscsi_door_unbind();
40644246c8e9SJack Meng 
406508dcd69cSJack Meng 		iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
406608dcd69cSJack Meng 
406708dcd69cSJack Meng 		if (ddi_copyout((void *)&rval, (caddr_t)arg,
406808dcd69cSJack Meng 		    sizeof (boolean_t), mode) != 0) {
40694246c8e9SJack Meng 			rtn = EFAULT;
40704246c8e9SJack Meng 		}
407108dcd69cSJack Meng 
40724246c8e9SJack Meng 		break;
40734246c8e9SJack Meng 
40744246c8e9SJack Meng 	case ISCSI_SMF_GET:
40754246c8e9SJack Meng 		mutex_enter(&ihp->hba_service_lock);
40764246c8e9SJack Meng 		while (ihp->hba_service_status ==
40774246c8e9SJack Meng 		    ISCSI_SERVICE_TRANSITION) {
40784246c8e9SJack Meng 			cv_wait(&ihp->hba_service_cv,
40794246c8e9SJack Meng 			    &ihp->hba_service_lock);
40804246c8e9SJack Meng 		}
40814246c8e9SJack Meng 		if (ddi_copyout((void *)&ihp->hba_service_status,
40824246c8e9SJack Meng 		    (caddr_t)arg, sizeof (boolean_t), mode) != 0) {
40834246c8e9SJack Meng 			rtn = EFAULT;
40844246c8e9SJack Meng 		}
40854246c8e9SJack Meng 		mutex_exit(&ihp->hba_service_lock);
4086fcf3ce44SJohn Forte 		break;
4087fcf3ce44SJohn Forte 
4088fcf3ce44SJohn Forte 	case ISCSI_DISCOVERY_EVENTS:
4089fcf3ce44SJohn Forte 		/*
4090fcf3ce44SJohn Forte 		 * If discovery has not been completed and not in progress,
4091fcf3ce44SJohn Forte 		 * poke the discovery methods
4092fcf3ce44SJohn Forte 		 */
4093fcf3ce44SJohn Forte 		mutex_enter(&ihp->hba_discovery_events_mutex);
4094fcf3ce44SJohn Forte 		method = ihp->hba_discovery_events;
4095fcf3ce44SJohn Forte 		if ((method != ISCSI_ALL_DISCOVERY_METHODS) &&
4096fcf3ce44SJohn Forte 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
4097fcf3ce44SJohn Forte 			ihp->hba_discovery_in_progress = B_TRUE;
4098fcf3ce44SJohn Forte 			mutex_exit(&ihp->hba_discovery_events_mutex);
4099fcf3ce44SJohn Forte 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
4100fcf3ce44SJohn Forte 			mutex_enter(&ihp->hba_discovery_events_mutex);
4101fcf3ce44SJohn Forte 			ihp->hba_discovery_in_progress = B_FALSE;
4102fcf3ce44SJohn Forte 			method = ihp->hba_discovery_events;
4103fcf3ce44SJohn Forte 		}
4104fcf3ce44SJohn Forte 		mutex_exit(&ihp->hba_discovery_events_mutex);
4105fcf3ce44SJohn Forte 
4106fcf3ce44SJohn Forte 		if (ddi_copyout((void *)&method, (caddr_t)arg,
4107fcf3ce44SJohn Forte 		    sizeof (method), mode) != 0)
4108fcf3ce44SJohn Forte 			rtn = EFAULT;
4109fcf3ce44SJohn Forte 		break;
4110fcf3ce44SJohn Forte 
4111fcf3ce44SJohn Forte 	/*
4112fcf3ce44SJohn Forte 	 * ISCSI_SENDTGTS_GET --
4113fcf3ce44SJohn Forte 	 */
4114fcf3ce44SJohn Forte 	case ISCSI_SENDTGTS_GET:
4115fcf3ce44SJohn Forte 		stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4116fcf3ce44SJohn Forte 		    sizeof (*stl_hdr));
4117fcf3ce44SJohn Forte 		if (stl_hdr == NULL) {
4118fcf3ce44SJohn Forte 			rtn = EFAULT;
4119fcf3ce44SJohn Forte 			break;
4120fcf3ce44SJohn Forte 		}
4121fcf3ce44SJohn Forte 
4122fcf3ce44SJohn Forte 		if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) {
4123fcf3ce44SJohn Forte 			rtn = EINVAL;
4124fcf3ce44SJohn Forte 			kmem_free(stl_hdr, sizeof (*stl_hdr));
4125fcf3ce44SJohn Forte 			break;
4126fcf3ce44SJohn Forte 		}
4127fcf3ce44SJohn Forte 
4128fcf3ce44SJohn Forte 		/* calculate how much memory user allocated for SendTgts */
4129fcf3ce44SJohn Forte 		stl_sz = sizeof (*stl_hdr);
4130fcf3ce44SJohn Forte 		if (stl_hdr->stl_in_cnt > 0) {
4131fcf3ce44SJohn Forte 			stl_sz += ((stl_hdr->stl_in_cnt - 1) *
4132fcf3ce44SJohn Forte 			    sizeof (iscsi_sendtgts_entry_t));
4133fcf3ce44SJohn Forte 		}
4134fcf3ce44SJohn Forte 
4135fcf3ce44SJohn Forte 		/* allocate local SendTgts list of the same size */
4136fcf3ce44SJohn Forte 		istl = kmem_zalloc(stl_sz, KM_SLEEP);
4137fcf3ce44SJohn Forte 		bcopy(stl_hdr, istl, sizeof (*stl_hdr));
4138fcf3ce44SJohn Forte 		kmem_free(stl_hdr, sizeof (*stl_hdr));
4139fcf3ce44SJohn Forte 
4140fcf3ce44SJohn Forte 		/* lock interface so only one SendTargets operation occurs */
4141fcf3ce44SJohn Forte 		sema_p(&ihp->hba_sendtgts_semaphore);
4142fcf3ce44SJohn Forte 
4143fcf3ce44SJohn Forte 		rtn = iscsi_ioctl_sendtgts_get(ihp, istl);
4144fcf3ce44SJohn Forte 
4145fcf3ce44SJohn Forte 		if (rtn == 0) {
4146fcf3ce44SJohn Forte 			rtn = iscsi_ioctl_copyout(istl, stl_sz,
4147fcf3ce44SJohn Forte 			    (caddr_t)arg, mode);
4148fcf3ce44SJohn Forte 		}
4149fcf3ce44SJohn Forte 
4150fcf3ce44SJohn Forte 		/* release lock to allow another SendTargets discovery */
4151fcf3ce44SJohn Forte 		sema_v(&ihp->hba_sendtgts_semaphore);
4152fcf3ce44SJohn Forte 
4153fcf3ce44SJohn Forte 		break;
4154fcf3ce44SJohn Forte 
4155fcf3ce44SJohn Forte 		/*
4156fcf3ce44SJohn Forte 		 * ISCSI_ISNS_SERVER_GET --
4157fcf3ce44SJohn Forte 		 */
4158fcf3ce44SJohn Forte 	case ISCSI_ISNS_SERVER_GET:
4159fcf3ce44SJohn Forte 		server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4160fcf3ce44SJohn Forte 		    sizeof (*server_pg_list_hdr));
4161fcf3ce44SJohn Forte 		if (server_pg_list_hdr == NULL) {
4162fcf3ce44SJohn Forte 			rtn = EFAULT;
4163fcf3ce44SJohn Forte 			break;
4164fcf3ce44SJohn Forte 		}
4165fcf3ce44SJohn Forte 
4166fcf3ce44SJohn Forte 		/* If iSNS discovery mode is not set, return with zero entry */
4167fcf3ce44SJohn Forte 		method = persistent_disc_meth_get();
4168fcf3ce44SJohn Forte 		if ((method & iSCSIDiscoveryMethodISNS) == 0) {
41699ad4dd06SSheng-Liang Eric Zhang 			kmem_free(server_pg_list_hdr,
41709ad4dd06SSheng-Liang Eric Zhang 			    sizeof (*server_pg_list_hdr));
41719ad4dd06SSheng-Liang Eric Zhang 			server_pg_list_hdr = NULL;
4172fcf3ce44SJohn Forte 			rtn = EACCES;
4173fcf3ce44SJohn Forte 			break;
4174fcf3ce44SJohn Forte 		}
4175fcf3ce44SJohn Forte 
4176fcf3ce44SJohn Forte 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
4177fcf3ce44SJohn Forte 		if (persistent_initiator_name_get(initiator_node_name,
4178fcf3ce44SJohn Forte 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4179fcf3ce44SJohn Forte 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4180fcf3ce44SJohn Forte 			initiator_node_name = NULL;
4181fcf3ce44SJohn Forte 			kmem_free(server_pg_list_hdr,
4182fcf3ce44SJohn Forte 			    sizeof (*server_pg_list_hdr));
41839ad4dd06SSheng-Liang Eric Zhang 			server_pg_list_hdr = NULL;
4184fcf3ce44SJohn Forte 			rtn = EIO;
4185fcf3ce44SJohn Forte 			break;
4186fcf3ce44SJohn Forte 		}
4187fcf3ce44SJohn Forte 		if (strlen(initiator_node_name) == 0) {
4188fcf3ce44SJohn Forte 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4189fcf3ce44SJohn Forte 			initiator_node_name = NULL;
4190fcf3ce44SJohn Forte 			kmem_free(server_pg_list_hdr,
4191fcf3ce44SJohn Forte 			    sizeof (*server_pg_list_hdr));
41929ad4dd06SSheng-Liang Eric Zhang 			server_pg_list_hdr = NULL;
4193fcf3ce44SJohn Forte 			rtn = EIO;
4194fcf3ce44SJohn Forte 			break;
4195fcf3ce44SJohn Forte 		}
4196fcf3ce44SJohn Forte 
4197fcf3ce44SJohn Forte 		initiator_node_alias = kmem_zalloc(
4198fcf3ce44SJohn Forte 		    ISCSI_MAX_NAME_LEN, KM_SLEEP);
4199fcf3ce44SJohn Forte 		if (persistent_alias_name_get(initiator_node_alias,
4200fcf3ce44SJohn Forte 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4201fcf3ce44SJohn Forte 			initiator_node_alias[0] = '\0';
4202fcf3ce44SJohn Forte 		}
4203fcf3ce44SJohn Forte 		rtn = isns_query_one_server(&(server_pg_list_hdr->addr),
4204fcf3ce44SJohn Forte 		    ihp->hba_isid,
4205fcf3ce44SJohn Forte 		    (uint8_t *)initiator_node_name,
4206fcf3ce44SJohn Forte 		    (uint8_t *)initiator_node_alias,
4207fcf3ce44SJohn Forte 		    ISNS_INITIATOR_NODE_TYPE,
4208fcf3ce44SJohn Forte 		    &pg_list);
4209fcf3ce44SJohn Forte 		if (rtn != isns_ok || pg_list == NULL) {
4210fcf3ce44SJohn Forte 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4211fcf3ce44SJohn Forte 			initiator_node_name = NULL;
4212fcf3ce44SJohn Forte 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
4213fcf3ce44SJohn Forte 			initiator_node_alias = NULL;
4214fcf3ce44SJohn Forte 			kmem_free(server_pg_list_hdr,
4215fcf3ce44SJohn Forte 			    sizeof (*server_pg_list_hdr));
4216fcf3ce44SJohn Forte 			server_pg_list_hdr = NULL;
4217fcf3ce44SJohn Forte 			rtn = EIO;
4218fcf3ce44SJohn Forte 			break;
4219fcf3ce44SJohn Forte 		}
4220fcf3ce44SJohn Forte 
4221fcf3ce44SJohn Forte 		/*
4222fcf3ce44SJohn Forte 		 * pg_list_sz is the size of the pg_list returned from the
4223fcf3ce44SJohn Forte 		 *	isns_query_all
4224fcf3ce44SJohn Forte 		 *
4225fcf3ce44SJohn Forte 		 * pg_sz_copy_out is the size of the pg_list we are going to
4226fcf3ce44SJohn Forte 		 *	return back to the caller
4227fcf3ce44SJohn Forte 		 *
4228fcf3ce44SJohn Forte 		 * server_pg_list_sz is total amount of data we are returning
4229fcf3ce44SJohn Forte 		 *	back to the caller
4230fcf3ce44SJohn Forte 		 */
4231fcf3ce44SJohn Forte 		pg_list->pg_in_cnt =
4232fcf3ce44SJohn Forte 		    server_pg_list_hdr->addr_port_list.pg_in_cnt;
4233fcf3ce44SJohn Forte 		pg_list_sz = sizeof (isns_portal_group_list_t);
4234fcf3ce44SJohn Forte 		if (pg_list->pg_out_cnt > 0) {
4235fcf3ce44SJohn Forte 			pg_list_sz += (pg_list->pg_out_cnt - 1) *
4236fcf3ce44SJohn Forte 			    sizeof (isns_portal_group_t);
4237fcf3ce44SJohn Forte 		}
4238fcf3ce44SJohn Forte 		/*
4239fcf3ce44SJohn Forte 		 * check if caller passed in a buffer with enough space
4240fcf3ce44SJohn Forte 		 * if there isn't enough space, fill the caller's buffer with
4241fcf3ce44SJohn Forte 		 * as much information as possible.
4242fcf3ce44SJohn Forte 		 *
4243fcf3ce44SJohn Forte 		 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with
4244fcf3ce44SJohn Forte 		 * the total number of targets found
4245fcf3ce44SJohn Forte 		 *
4246fcf3ce44SJohn Forte 		 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number
4247fcf3ce44SJohn Forte 		 * of targets returned
4248fcf3ce44SJohn Forte 		 */
4249fcf3ce44SJohn Forte 		if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) {
4250fcf3ce44SJohn Forte 			pg_sz_copy_out = sizeof (isns_portal_group_list_t);
4251fcf3ce44SJohn Forte 			if (pg_list->pg_in_cnt > 0) {
4252fcf3ce44SJohn Forte 				pg_sz_copy_out += (pg_list->pg_in_cnt - 1) *
4253fcf3ce44SJohn Forte 				    sizeof (isns_portal_group_t);
4254fcf3ce44SJohn Forte 			}
4255fcf3ce44SJohn Forte 			server_pg_list_sz =
4256fcf3ce44SJohn Forte 			    sizeof (isns_server_portal_group_list_t);
4257fcf3ce44SJohn Forte 			if (pg_list->pg_in_cnt > 0) {
4258fcf3ce44SJohn Forte 				server_pg_list_sz += (pg_list->pg_in_cnt - 1) *
4259fcf3ce44SJohn Forte 				    sizeof (isns_portal_group_t);
4260fcf3ce44SJohn Forte 			}
4261fcf3ce44SJohn Forte 		} else {
4262fcf3ce44SJohn Forte 			pg_sz_copy_out = pg_list_sz;
4263fcf3ce44SJohn Forte 			server_pg_list_sz =
4264fcf3ce44SJohn Forte 			    sizeof (isns_server_portal_group_list_t);
4265fcf3ce44SJohn Forte 			if (pg_list->pg_out_cnt > 0) {
4266fcf3ce44SJohn Forte 				server_pg_list_sz += (pg_list->pg_out_cnt - 1) *
4267fcf3ce44SJohn Forte 				    sizeof (isns_portal_group_t);
4268fcf3ce44SJohn Forte 			}
4269fcf3ce44SJohn Forte 		}
4270fcf3ce44SJohn Forte 
4271fcf3ce44SJohn Forte 		server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc(
4272fcf3ce44SJohn Forte 		    server_pg_list_sz, KM_SLEEP);
4273fcf3ce44SJohn Forte 
4274fcf3ce44SJohn Forte 		bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr),
4275fcf3ce44SJohn Forte 		    sizeof (server_pg_list->addr));
4276fcf3ce44SJohn Forte 		bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out);
4277fcf3ce44SJohn Forte 
4278fcf3ce44SJohn Forte 		if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz,
4279fcf3ce44SJohn Forte 		    mode) != 0) {
4280fcf3ce44SJohn Forte 			rtn = EFAULT;
4281fcf3ce44SJohn Forte 		}
4282fcf3ce44SJohn Forte 		DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz,
4283fcf3ce44SJohn Forte 		    int, pg_list_sz);
42849ad4dd06SSheng-Liang Eric Zhang 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
42859ad4dd06SSheng-Liang Eric Zhang 		initiator_node_name = NULL;
42869ad4dd06SSheng-Liang Eric Zhang 		kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
42879ad4dd06SSheng-Liang Eric Zhang 		initiator_node_alias = NULL;
4288fcf3ce44SJohn Forte 		kmem_free(pg_list, pg_list_sz);
42899ad4dd06SSheng-Liang Eric Zhang 		pg_list = NULL;
4290fcf3ce44SJohn Forte 		kmem_free(server_pg_list, server_pg_list_sz);
42919ad4dd06SSheng-Liang Eric Zhang 		server_pg_list = NULL;
4292fcf3ce44SJohn Forte 		kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr));
42939ad4dd06SSheng-Liang Eric Zhang 		server_pg_list_hdr = NULL;
4294fcf3ce44SJohn Forte 		break;
4295fcf3ce44SJohn Forte 
4296fcf3ce44SJohn Forte 	/*
4297fcf3ce44SJohn Forte 	 * ISCSI_GET_CONFIG_SESSIONS --
4298fcf3ce44SJohn Forte 	 */
4299fcf3ce44SJohn Forte 	case ISCSI_GET_CONFIG_SESSIONS:
4300fcf3ce44SJohn Forte 		/* FALLTHRU */
4301fcf3ce44SJohn Forte 
4302fcf3ce44SJohn Forte 	case ISCSI_SET_CONFIG_SESSIONS:
4303fcf3ce44SJohn Forte 		size = sizeof (*ics);
4304fcf3ce44SJohn Forte 		ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4305fcf3ce44SJohn Forte 		if (ics == NULL) {
4306fcf3ce44SJohn Forte 			rtn = EFAULT;
4307fcf3ce44SJohn Forte 			break;
4308fcf3ce44SJohn Forte 		}
4309fcf3ce44SJohn Forte 
4310fcf3ce44SJohn Forte 		/* verify version infomration */
4311fcf3ce44SJohn Forte 		if (ics->ics_ver != ISCSI_INTERFACE_VERSION) {
4312fcf3ce44SJohn Forte 			rtn = EINVAL;
4313fcf3ce44SJohn Forte 			kmem_free(ics, size);
43149ad4dd06SSheng-Liang Eric Zhang 			ics = NULL;
4315fcf3ce44SJohn Forte 			break;
4316fcf3ce44SJohn Forte 		}
4317fcf3ce44SJohn Forte 
4318fcf3ce44SJohn Forte 		/* Check to see if we need to copy in more memory */
4319fcf3ce44SJohn Forte 		if (ics->ics_in > 1) {
4320fcf3ce44SJohn Forte 			/* record correct size */
4321fcf3ce44SJohn Forte 			size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in);
4322fcf3ce44SJohn Forte 			/* free old buffer */
4323fcf3ce44SJohn Forte 			kmem_free(ics, sizeof (*ics));
4324fcf3ce44SJohn Forte 
4325fcf3ce44SJohn Forte 			/* copy in complete buffer size */
4326fcf3ce44SJohn Forte 			ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4327fcf3ce44SJohn Forte 			if (ics == NULL) {
4328fcf3ce44SJohn Forte 				rtn = EFAULT;
4329fcf3ce44SJohn Forte 				break;
4330fcf3ce44SJohn Forte 			}
4331fcf3ce44SJohn Forte 		}
4332fcf3ce44SJohn Forte 
4333fcf3ce44SJohn Forte 		/* switch action based on get or set */
4334fcf3ce44SJohn Forte 		if (cmd == ISCSI_GET_CONFIG_SESSIONS) {
4335fcf3ce44SJohn Forte 			/* get */
4336fcf3ce44SJohn Forte 			rtn = iscsi_ioctl_get_config_sess(ihp, ics);
4337fcf3ce44SJohn Forte 			if (rtn == 0) {
4338fcf3ce44SJohn Forte 				/* copyout data for gets */
4339fcf3ce44SJohn Forte 				rtn = iscsi_ioctl_copyout(ics, size,
4340fcf3ce44SJohn Forte 				    (caddr_t)arg, mode);
43419ad4dd06SSheng-Liang Eric Zhang 			} else {
43429ad4dd06SSheng-Liang Eric Zhang 				kmem_free(ics, size);
43439ad4dd06SSheng-Liang Eric Zhang 				ics = NULL;
4344fcf3ce44SJohn Forte 			}
4345fcf3ce44SJohn Forte 		} else {
4346fcf3ce44SJohn Forte 			/* set */
4347fcf3ce44SJohn Forte 			rtn = iscsi_ioctl_set_config_sess(ihp, ics);
43486cefaae1SJack Meng 			if (iscsiboot_prop) {
43496cefaae1SJack Meng 				if (iscsi_cmp_boot_sess_oid(ihp,
43506cefaae1SJack Meng 				    ics->ics_oid)) {
43516cefaae1SJack Meng 					/*
43526cefaae1SJack Meng 					 * found active session for this object
43536cefaae1SJack Meng 					 * or this is initiator object
43546cefaae1SJack Meng 					 * with mpxio enabled
43556cefaae1SJack Meng 					 */
43566cefaae1SJack Meng 					if (!iscsi_reconfig_boot_sess(ihp)) {
43579ad4dd06SSheng-Liang Eric Zhang 						kmem_free(ics, size);
43589ad4dd06SSheng-Liang Eric Zhang 						ics = NULL;
43596cefaae1SJack Meng 						rtn = EINVAL;
43606cefaae1SJack Meng 						break;
43616cefaae1SJack Meng 					}
43626cefaae1SJack Meng 				}
43636cefaae1SJack Meng 			}
43649ad4dd06SSheng-Liang Eric Zhang 			kmem_free(ics, size);
43659ad4dd06SSheng-Liang Eric Zhang 			ics = NULL;
4366fcf3ce44SJohn Forte 		}
4367fcf3ce44SJohn Forte 		break;
4368fcf3ce44SJohn Forte 
43696cefaae1SJack Meng 	case ISCSI_IS_ACTIVE:
43706cefaae1SJack Meng 		/*
43716cefaae1SJack Meng 		 * dhcpagent calls here to check if there are
43726cefaae1SJack Meng 		 * active iSCSI sessions
43736cefaae1SJack Meng 		 */
43746cefaae1SJack Meng 		instance = 0;
43756cefaae1SJack Meng 		if (iscsiboot_prop) {
43766cefaae1SJack Meng 			instance = 1;
43776cefaae1SJack Meng 		}
43786cefaae1SJack Meng 		if (!instance) {
43796cefaae1SJack Meng 			rw_enter(&ihp->hba_sess_list_rwlock,
43806cefaae1SJack Meng 			    RW_READER);
43816cefaae1SJack Meng 			for (isp = ihp->hba_sess_list; isp;
43826cefaae1SJack Meng 			    isp = isp->sess_next) {
43836cefaae1SJack Meng 				if ((isp->sess_state ==
43846cefaae1SJack Meng 				    ISCSI_SESS_STATE_LOGGED_IN) &&
43856cefaae1SJack Meng 				    (isp->sess_lun_list !=
43866cefaae1SJack Meng 				    NULL)) {
43876cefaae1SJack Meng 					instance = 1;
43886cefaae1SJack Meng 					break;
43896cefaae1SJack Meng 				}
43906cefaae1SJack Meng 			}
43916cefaae1SJack Meng 			rw_exit(&ihp->hba_sess_list_rwlock);
43926cefaae1SJack Meng 		}
43936cefaae1SJack Meng 		size = sizeof (instance);
43946cefaae1SJack Meng 		if (ddi_copyout(&instance, (caddr_t)arg, size,
43956cefaae1SJack Meng 		    mode) != 0) {
43966cefaae1SJack Meng 			rtn = EFAULT;
43976cefaae1SJack Meng 		}
43986cefaae1SJack Meng 		break;
43996cefaae1SJack Meng 
44006cefaae1SJack Meng 	case ISCSI_BOOTPROP_GET:
44016cefaae1SJack Meng 		size = sizeof (*bootProp);
44026cefaae1SJack Meng 		bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
44036cefaae1SJack Meng 		if (bootProp == NULL) {
44046cefaae1SJack Meng 			rtn = EFAULT;
44056cefaae1SJack Meng 			break;
44066cefaae1SJack Meng 		}
44076cefaae1SJack Meng 		bootProp->hba_mpxio_enabled =
44086cefaae1SJack Meng 		    iscsi_chk_bootlun_mpxio(ihp);
44096cefaae1SJack Meng 		if (iscsiboot_prop == NULL) {
44106cefaae1SJack Meng 			bootProp->iscsiboot = 0;
44116cefaae1SJack Meng 			rtn = iscsi_ioctl_copyout(bootProp, size,
44126cefaae1SJack Meng 			    (caddr_t)arg, mode);
44136cefaae1SJack Meng 			break;
44146cefaae1SJack Meng 		} else {
44156cefaae1SJack Meng 			bootProp->iscsiboot = 1;
44166cefaae1SJack Meng 		}
44176cefaae1SJack Meng 
44186cefaae1SJack Meng 		if (iscsiboot_prop->boot_init.ini_name != NULL) {
44196cefaae1SJack Meng 			(void) strncpy((char *)bootProp->ini_name.n_name,
44206cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_init.ini_name,
44216cefaae1SJack Meng 			    ISCSI_MAX_NAME_LEN);
44226cefaae1SJack Meng 		}
44236cefaae1SJack Meng 		if (iscsiboot_prop->boot_init.ini_chap_name != NULL) {
44246cefaae1SJack Meng 			bootProp->auth.a_auth_method = authMethodCHAP;
44256cefaae1SJack Meng 			(void) strncpy((char *)bootProp->ini_chap.c_user,
44266cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_init.ini_chap_name,
44276cefaae1SJack Meng 			    ISCSI_MAX_NAME_LEN);
44286cefaae1SJack Meng 			(void) strncpy((char *)bootProp->ini_chap.c_secret,
44296cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_init.ini_chap_sec,
44306cefaae1SJack Meng 			    ISCSI_CHAP_SECRET_LEN);
44316cefaae1SJack Meng 			if (iscsiboot_prop->boot_tgt.tgt_chap_name !=
44326cefaae1SJack Meng 			    NULL) {
44336cefaae1SJack Meng 				bootProp->auth.a_bi_auth = B_TRUE;
44346cefaae1SJack Meng 			} else {
44356cefaae1SJack Meng 				bootProp->auth.a_bi_auth = B_FALSE;
44366cefaae1SJack Meng 			}
44376cefaae1SJack Meng 		}
44386cefaae1SJack Meng 		if (iscsiboot_prop->boot_tgt.tgt_name != NULL) {
44396cefaae1SJack Meng 			(void) strncpy((char *)bootProp->tgt_name.n_name,
44406cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_tgt.tgt_name,
44416cefaae1SJack Meng 			    ISCSI_MAX_NAME_LEN);
44426cefaae1SJack Meng 		}
44436cefaae1SJack Meng 		if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) {
44446cefaae1SJack Meng 			(void) strncpy((char *)bootProp->tgt_chap.c_user,
44456cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_name,
44466cefaae1SJack Meng 			    ISCSI_MAX_NAME_LEN);
44476cefaae1SJack Meng 			(void) strncpy((char *)bootProp->tgt_chap.c_secret,
44486cefaae1SJack Meng 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec,
44496cefaae1SJack Meng 			    ISCSI_CHAP_SECRET_LEN);
44506cefaae1SJack Meng 		}
44516cefaae1SJack Meng 
44526cefaae1SJack Meng 		rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode);
44536cefaae1SJack Meng 		break;
44546cefaae1SJack Meng 
4455904e51f6SJack Meng 	case ISCSI_TARGET_REENUM:
4456904e51f6SJack Meng 		size = sizeof (iscsi_reen_t);
4457904e51f6SJack Meng 		reenum = (iscsi_reen_t *)kmem_alloc(size, KM_SLEEP);
4458904e51f6SJack Meng 
4459904e51f6SJack Meng 		if (ddi_copyin((caddr_t)arg, reenum, size, mode) != 0) {
4460904e51f6SJack Meng 			rtn = EFAULT;
4461904e51f6SJack Meng 			kmem_free(reenum, size);
4462904e51f6SJack Meng 			break;
4463904e51f6SJack Meng 		}
4464904e51f6SJack Meng 		if (reenum->re_ver != ISCSI_INTERFACE_VERSION) {
4465904e51f6SJack Meng 			rtn = EINVAL;
4466904e51f6SJack Meng 			kmem_free(reenum, size);
4467904e51f6SJack Meng 			break;
4468904e51f6SJack Meng 		}
4469904e51f6SJack Meng 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
4470904e51f6SJack Meng 		rtn = iscsi_sess_get(reenum->re_oid, ihp, &isp);
4471904e51f6SJack Meng 		if (rtn != 0) {
4472904e51f6SJack Meng 			rtn = iscsi_sess_get_by_target(
4473904e51f6SJack Meng 			    reenum->re_oid, ihp, &isp);
4474904e51f6SJack Meng 		}
4475904e51f6SJack Meng 
4476904e51f6SJack Meng 		if (rtn != 0) {
4477904e51f6SJack Meng 			rw_exit(&ihp->hba_sess_list_rwlock);
4478904e51f6SJack Meng 			kmem_free(reenum, size);
4479904e51f6SJack Meng 			break;
4480904e51f6SJack Meng 		}
4481904e51f6SJack Meng 		kmem_free(reenum, size);
4482904e51f6SJack Meng 		if (isp->sess_type == ISCSI_SESS_TYPE_NORMAL) {
4483904e51f6SJack Meng 			rw_enter(&isp->sess_state_rwlock, RW_READER);
4484904e51f6SJack Meng 			if ((isp->sess_state ==
4485904e51f6SJack Meng 			    ISCSI_SESS_STATE_LOGGED_IN) &&
4486904e51f6SJack Meng 			    (iscsi_sess_enum_request(isp, B_TRUE,
4487904e51f6SJack Meng 			    isp->sess_state_event_count)
4488904e51f6SJack Meng 			    == ISCSI_SESS_ENUM_SUBMITTED)) {
4489904e51f6SJack Meng 				(void) iscsi_sess_enum_query(isp);
4490904e51f6SJack Meng 			}
4491904e51f6SJack Meng 			rw_exit(&isp->sess_state_rwlock);
4492904e51f6SJack Meng 		}
4493904e51f6SJack Meng 		rw_exit(&ihp->hba_sess_list_rwlock);
4494904e51f6SJack Meng 		break;
4495904e51f6SJack Meng 
4496aff4bce5Syi zhang - Sun Microsystems - Beijing China 	case ISCSI_TUNABLE_PARAM_SET:
4497aff4bce5Syi zhang - Sun Microsystems - Beijing China 		tpss = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpss),
4498aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    KM_SLEEP);
4499aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (ddi_copyin((caddr_t)arg, tpss, sizeof (*tpss), mode)) {
4500aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EFAULT;
4501aff4bce5Syi zhang - Sun Microsystems - Beijing China 			kmem_free(tpss, sizeof (*tpss));
4502aff4bce5Syi zhang - Sun Microsystems - Beijing China 			break;
4503aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
4504aff4bce5Syi zhang - Sun Microsystems - Beijing China 		rtn = iscsi_ioctl_set_tunable_param(ihp, tpss);
4505aff4bce5Syi zhang - Sun Microsystems - Beijing China 		kmem_free(tpss, sizeof (*tpss));
4506aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
4507aff4bce5Syi zhang - Sun Microsystems - Beijing China 
4508aff4bce5Syi zhang - Sun Microsystems - Beijing China 	case ISCSI_TUNABLE_PARAM_GET:
4509aff4bce5Syi zhang - Sun Microsystems - Beijing China 		tpsg = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpsg),
4510aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    KM_SLEEP);
4511aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (ddi_copyin((caddr_t)arg, tpsg, sizeof (*tpsg), mode)) {
4512aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EFAULT;
4513aff4bce5Syi zhang - Sun Microsystems - Beijing China 			kmem_free(tpsg, sizeof (*tpsg));
4514aff4bce5Syi zhang - Sun Microsystems - Beijing China 			break;
4515aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
4516aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (tpsg->t_oid == ihp->hba_oid) {
4517aff4bce5Syi zhang - Sun Microsystems - Beijing China 			/* initiator */
4518aff4bce5Syi zhang - Sun Microsystems - Beijing China 			name = ihp->hba_name;
4519aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4520aff4bce5Syi zhang - Sun Microsystems - Beijing China 			    tpsg) == 1) {
4521aff4bce5Syi zhang - Sun Microsystems - Beijing China 				/*
4522aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * no persisted tunable parameters found
4523aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * for iscsi initiator, use default tunable
4524aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * params for initiator node.
4525aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 */
4526aff4bce5Syi zhang - Sun Microsystems - Beijing China 				iscsi_get_tunable_default(tpsg);
4527aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
4528aff4bce5Syi zhang - Sun Microsystems - Beijing China 		} else {
4529aff4bce5Syi zhang - Sun Microsystems - Beijing China 			/* check whether it is a target oid */
4530aff4bce5Syi zhang - Sun Microsystems - Beijing China 			name = iscsi_targetparam_get_name(tpsg->t_oid);
4531aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (name == NULL) {
4532aff4bce5Syi zhang - Sun Microsystems - Beijing China 				/* invalid node name */
4533aff4bce5Syi zhang - Sun Microsystems - Beijing China 				rtn = EINVAL;
4534aff4bce5Syi zhang - Sun Microsystems - Beijing China 				kmem_free(tpsg, sizeof (*tpsg));
4535aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
4536aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
4537aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4538aff4bce5Syi zhang - Sun Microsystems - Beijing China 			    tpsg) == 1) {
4539aff4bce5Syi zhang - Sun Microsystems - Beijing China 				/*
4540aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * no persisted tunable parameters found for
4541aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 * iscsi target, use initiator's configure.
4542aff4bce5Syi zhang - Sun Microsystems - Beijing China 				 */
4543aff4bce5Syi zhang - Sun Microsystems - Beijing China 				if (iscsi_get_persisted_tunable_param(
4544aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    (uchar_t *)ihp->hba_name, tpsg) == -1) {
4545aff4bce5Syi zhang - Sun Microsystems - Beijing China 					/*
4546aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 * No initiator tunable parameters set
4547aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 * use default value for target
4548aff4bce5Syi zhang - Sun Microsystems - Beijing China 					 */
4549aff4bce5Syi zhang - Sun Microsystems - Beijing China 					iscsi_get_tunable_default(tpsg);
4550aff4bce5Syi zhang - Sun Microsystems - Beijing China 				}
4551aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
4552aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
4553aff4bce5Syi zhang - Sun Microsystems - Beijing China 
4554aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (ddi_copyout(tpsg, (caddr_t)arg,
4555aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    sizeof (iscsi_tunable_object_t), mode) != 0) {
4556aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = EFAULT;
4557aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
4558aff4bce5Syi zhang - Sun Microsystems - Beijing China 		kmem_free(tpsg, sizeof (*tpsg));
4559aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
4560aff4bce5Syi zhang - Sun Microsystems - Beijing China 
4561fcf3ce44SJohn Forte 	default:
4562fcf3ce44SJohn Forte 		rtn = ENOTTY;
4563fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd);
4564fcf3ce44SJohn Forte 	} /* end of ioctl type switch/cases */
4565fcf3ce44SJohn Forte 
45664246c8e9SJack Meng 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
45674246c8e9SJack Meng 	    (cmd != ISCSI_SMF_GET)) {
45684246c8e9SJack Meng 		/* other cmds need to release the service */
45694246c8e9SJack Meng 		iscsi_client_release_service(ihp);
45704246c8e9SJack Meng 	}
45714246c8e9SJack Meng 
4572fcf3ce44SJohn Forte 	return (rtn);
4573fcf3ce44SJohn Forte }
4574fcf3ce44SJohn Forte 
4575fcf3ce44SJohn Forte /*
4576fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4577fcf3ce44SJohn Forte  * | End of cb_ops routines					     |
4578fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4579fcf3ce44SJohn Forte  */
4580fcf3ce44SJohn Forte 
4581fcf3ce44SJohn Forte 
4582fcf3ce44SJohn Forte /*
4583fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4584fcf3ce44SJohn Forte  * | Common scsi_tran support routines				  |
4585fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4586fcf3ce44SJohn Forte  */
4587fcf3ce44SJohn Forte 
4588fcf3ce44SJohn Forte /*
4589fcf3ce44SJohn Forte  * iscsi_i_commoncap -- SCSA host adapter get/set capability routines.
4590fcf3ce44SJohn Forte  *
4591fcf3ce44SJohn Forte  * Need to determine if any of these can be determined through the iSCSI
4592fcf3ce44SJohn Forte  * protocol. For now just return error on most.
4593fcf3ce44SJohn Forte  */
4594fcf3ce44SJohn Forte /* ARGSUSED */
4595fcf3ce44SJohn Forte static int
iscsi_i_commoncap(struct scsi_address * ap,char * cap,int val,int tgtonly,int doset)4596fcf3ce44SJohn Forte iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val,
4597fcf3ce44SJohn Forte     int tgtonly, int doset)
4598fcf3ce44SJohn Forte {
4599fcf3ce44SJohn Forte 	int		rtn;
4600fcf3ce44SJohn Forte 	int		cidx;
4601fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp;
4602fcf3ce44SJohn Forte 
4603fcf3ce44SJohn Forte 	ASSERT((ap)->a_hba_tran->tran_hba_private != NULL);
4604fcf3ce44SJohn Forte 	ilp	= (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private);
4605fcf3ce44SJohn Forte 	ASSERT(ilp != NULL);
4606fcf3ce44SJohn Forte 
4607fcf3ce44SJohn Forte 	if (cap == (char *)0) {
4608fcf3ce44SJohn Forte 		return (FALSE);
4609fcf3ce44SJohn Forte 	}
4610fcf3ce44SJohn Forte 
4611fcf3ce44SJohn Forte 	cidx = scsi_hba_lookup_capstr(cap);
4612fcf3ce44SJohn Forte 	if (cidx == -1) {
4613fcf3ce44SJohn Forte 		return (cidx);
4614fcf3ce44SJohn Forte 	}
4615fcf3ce44SJohn Forte 
4616fcf3ce44SJohn Forte 	/*
4617fcf3ce44SJohn Forte 	 * Process setcap request.
4618fcf3ce44SJohn Forte 	 */
4619fcf3ce44SJohn Forte 	if (doset) {
4620fcf3ce44SJohn Forte 		/*
4621fcf3ce44SJohn Forte 		 * At present, we can only set binary (0/1) values
4622fcf3ce44SJohn Forte 		 */
4623fcf3ce44SJohn Forte 		switch (cidx) {
4624fcf3ce44SJohn Forte 		case SCSI_CAP_LUN_RESET:
4625fcf3ce44SJohn Forte 			if (val) {
4626fcf3ce44SJohn Forte 				ilp->lun_cap |= ISCSI_LUN_CAP_RESET;
4627fcf3ce44SJohn Forte 			} else {
4628fcf3ce44SJohn Forte 				ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET;
4629fcf3ce44SJohn Forte 			}
4630fcf3ce44SJohn Forte 			rtn = TRUE;
4631fcf3ce44SJohn Forte 			break;
4632fcf3ce44SJohn Forte 		default:
4633fcf3ce44SJohn Forte 			/*
4634fcf3ce44SJohn Forte 			 * None of these are settable via
4635fcf3ce44SJohn Forte 			 * the capability interface.
4636fcf3ce44SJohn Forte 			 */
4637fcf3ce44SJohn Forte 			rtn = FALSE;
4638fcf3ce44SJohn Forte 			break;
4639fcf3ce44SJohn Forte 		}
4640fcf3ce44SJohn Forte 
4641fcf3ce44SJohn Forte 		/*
4642fcf3ce44SJohn Forte 		 * Process getcap request.
4643fcf3ce44SJohn Forte 		 */
4644fcf3ce44SJohn Forte 	} else {
4645fcf3ce44SJohn Forte 		switch (cidx) {
4646fcf3ce44SJohn Forte 		case SCSI_CAP_DMA_MAX:
4647fcf3ce44SJohn Forte 			/* no DMA, Psuedo value */
4648fcf3ce44SJohn Forte 			rtn = INT32_MAX;
4649fcf3ce44SJohn Forte 			break;
4650fcf3ce44SJohn Forte 		case SCSI_CAP_INITIATOR_ID:
4651fcf3ce44SJohn Forte 			rtn = 7;
4652fcf3ce44SJohn Forte 			break;
4653fcf3ce44SJohn Forte 		case SCSI_CAP_ARQ:
4654fcf3ce44SJohn Forte 		case SCSI_CAP_RESET_NOTIFICATION:
4655fcf3ce44SJohn Forte 		case SCSI_CAP_TAGGED_QING:
4656fcf3ce44SJohn Forte 			rtn = TRUE;
4657fcf3ce44SJohn Forte 			break;
4658fcf3ce44SJohn Forte 		case SCSI_CAP_SCSI_VERSION:
4659fcf3ce44SJohn Forte 			rtn = SCSI_VERSION_3;
4660fcf3ce44SJohn Forte 			break;
4661fcf3ce44SJohn Forte 		case SCSI_CAP_INTERCONNECT_TYPE:
4662fcf3ce44SJohn Forte 			rtn = INTERCONNECT_FABRIC;
4663fcf3ce44SJohn Forte 			break;
4664fcf3ce44SJohn Forte 		case SCSI_CAP_LUN_RESET:
4665fcf3ce44SJohn Forte 			rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ?
4666fcf3ce44SJohn Forte 			    TRUE : FALSE;
4667fcf3ce44SJohn Forte 			break;
4668fcf3ce44SJohn Forte 		case SCSI_CAP_CDB_LEN:
4669fcf3ce44SJohn Forte 			/*
4670fcf3ce44SJohn Forte 			 * iSCSI RFC 3720 defines a default 16 byte
4671fcf3ce44SJohn Forte 			 * CDB as part of the Basic Header Segment
4672fcf3ce44SJohn Forte 			 * (BHS) (10.2.1) and allows for an Additional
4673fcf3ce44SJohn Forte 			 * Header Segment (AHS) Length of 255 * 4
4674fcf3ce44SJohn Forte 			 * (10.2.1.5).  The AHS length can be used
4675fcf3ce44SJohn Forte 			 * for different purposes two of which are
4676fcf3ce44SJohn Forte 			 * Extended CDB ADS (10.2.2.3) and Bidirectional
4677fcf3ce44SJohn Forte 			 * Expected Read-Data Length AHS (10.2.2.4).
4678fcf3ce44SJohn Forte 			 * The largest header of these consumes is
4679fcf3ce44SJohn Forte 			 * 32 bytes.  So the total Max CDB Length is
4680fcf3ce44SJohn Forte 			 * 16 + ((255 * 4 ) - 32) = 1004.
4681fcf3ce44SJohn Forte 			 */
4682fcf3ce44SJohn Forte 			rtn = 1004;
4683fcf3ce44SJohn Forte 			break;
4684fcf3ce44SJohn Forte 		default:
4685fcf3ce44SJohn Forte 			rtn = UNDEFINED;
4686fcf3ce44SJohn Forte 			break;
4687fcf3ce44SJohn Forte 		}
4688fcf3ce44SJohn Forte 	}
4689fcf3ce44SJohn Forte 	return (rtn);
4690fcf3ce44SJohn Forte }
4691fcf3ce44SJohn Forte 
4692fcf3ce44SJohn Forte /*
4693fcf3ce44SJohn Forte  * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding
4694fcf3ce44SJohn Forte  *
4695fcf3ce44SJohn Forte  * This routine is used to associate the tran_tgt_private to our ilp
4696fcf3ce44SJohn Forte  * structure.  This function is indirectly called from our
4697fcf3ce44SJohn Forte  * iscsi_lun_create_xxx routines.  These routines must prevent
4698fcf3ce44SJohn Forte  * the session and lun lists from changing during this call.
4699fcf3ce44SJohn Forte  */
4700fcf3ce44SJohn Forte /* ARGSUSED */
4701fcf3ce44SJohn Forte static int
iscsi_virt_lun_init(dev_info_t * hba_dip,dev_info_t * lun_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)4702fcf3ce44SJohn Forte iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4703fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4704fcf3ce44SJohn Forte {
4705fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp		= NULL;
4706fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp_check	= NULL;
4707fcf3ce44SJohn Forte 	iscsi_sess_t	*isp		= NULL;
4708fcf3ce44SJohn Forte 	char		*lun_guid	= NULL;
4709fcf3ce44SJohn Forte 	mdi_pathinfo_t	*pip		= NULL;
4710fcf3ce44SJohn Forte 	iscsi_hba_t	*ihp    = (iscsi_hba_t *)hba_tran->tran_hba_private;
4711fcf3ce44SJohn Forte 	char		target_port_name[MAX_NAME_PROP_SIZE];
4712fcf3ce44SJohn Forte 
4713fcf3ce44SJohn Forte 	/*
4714fcf3ce44SJohn Forte 	 * Here's a nice little piece of undocumented stuff.
4715fcf3ce44SJohn Forte 	 */
4716fcf3ce44SJohn Forte 	if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) {
4717fcf3ce44SJohn Forte 		/*
4718fcf3ce44SJohn Forte 		 * Very bad news if this occurs. Somehow SCSI_vhci has
4719fcf3ce44SJohn Forte 		 * lost the pathinfo node for this target.
4720fcf3ce44SJohn Forte 		 */
4721fcf3ce44SJohn Forte 		return (DDI_NOT_WELL_FORMED);
4722fcf3ce44SJohn Forte 	}
4723fcf3ce44SJohn Forte 
4724fcf3ce44SJohn Forte 	ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip);
4725fcf3ce44SJohn Forte 
4726fcf3ce44SJohn Forte 	/*
4727fcf3ce44SJohn Forte 	 * +----------------------------------------------------+
4728fcf3ce44SJohn Forte 	 * | Looking to find the target device via the property |
4729fcf3ce44SJohn Forte 	 * | is not required since the driver can easily get    |
4730fcf3ce44SJohn Forte 	 * | this information from the mdi_phci_get_private()   |
4731fcf3ce44SJohn Forte 	 * | call above.  This is just a consistency check	|
4732fcf3ce44SJohn Forte 	 * | which can be removed.				|
4733fcf3ce44SJohn Forte 	 */
4734fcf3ce44SJohn Forte 	if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) !=
4735fcf3ce44SJohn Forte 	    DDI_PROP_SUCCESS) {
4736fcf3ce44SJohn Forte 		return (DDI_NOT_WELL_FORMED);
4737fcf3ce44SJohn Forte 	}
4738fcf3ce44SJohn Forte 
4739fcf3ce44SJohn Forte 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4740fcf3ce44SJohn Forte 
4741fcf3ce44SJohn Forte 		/* If this isn't the matching session continue */
4742fcf3ce44SJohn Forte 		if (ilp->lun_sess != isp) {
4743fcf3ce44SJohn Forte 			continue;
4744fcf3ce44SJohn Forte 		}
4745fcf3ce44SJohn Forte 
4746fcf3ce44SJohn Forte 		/*
4747fcf3ce44SJohn Forte 		 * We are already holding the lun list rwlock
4748fcf3ce44SJohn Forte 		 * for this thread on the callers side of mdi_pi_online
4749fcf3ce44SJohn Forte 		 * or ndi_devi_online.  Which lead to this functions
4750fcf3ce44SJohn Forte 		 * call.
4751fcf3ce44SJohn Forte 		 */
4752fcf3ce44SJohn Forte 		for (ilp_check = isp->sess_lun_list; ilp_check;
4753fcf3ce44SJohn Forte 		    ilp_check = ilp_check->lun_next) {
4754fcf3ce44SJohn Forte 
4755fcf3ce44SJohn Forte 			/*
4756fcf3ce44SJohn Forte 			 * If this is the matching LUN and contains
4757fcf3ce44SJohn Forte 			 * the same LUN GUID then break we found our
4758fcf3ce44SJohn Forte 			 * match.
4759fcf3ce44SJohn Forte 			 */
4760fcf3ce44SJohn Forte 			if ((ilp == ilp_check) &&
4761fcf3ce44SJohn Forte 			    (strcmp(lun_guid, ilp_check->lun_guid) == 0)) {
4762fcf3ce44SJohn Forte 				break;
4763fcf3ce44SJohn Forte 			}
4764fcf3ce44SJohn Forte 		}
4765fcf3ce44SJohn Forte 		if (ilp_check != NULL) {
4766fcf3ce44SJohn Forte 			break;
4767fcf3ce44SJohn Forte 		}
4768fcf3ce44SJohn Forte 	}
4769fcf3ce44SJohn Forte 
4770fcf3ce44SJohn Forte 	/*
4771fcf3ce44SJohn Forte 	 * Free resource that's no longer required.
4772fcf3ce44SJohn Forte 	 */
4773fcf3ce44SJohn Forte 	if (lun_guid != NULL)
4774fcf3ce44SJohn Forte 		(void) mdi_prop_free(lun_guid);
4775fcf3ce44SJohn Forte 
4776fcf3ce44SJohn Forte 	if (ilp_check == NULL) {
4777fcf3ce44SJohn Forte 		/*
4778fcf3ce44SJohn Forte 		 * Failed to find iSCSI LUN in HBA chain based
4779fcf3ce44SJohn Forte 		 * on the GUID that was stored as a property on
4780fcf3ce44SJohn Forte 		 * the pathinfo node.
4781fcf3ce44SJohn Forte 		 */
4782fcf3ce44SJohn Forte 		return (DDI_NOT_WELL_FORMED);
4783fcf3ce44SJohn Forte 	}
4784fcf3ce44SJohn Forte 
4785fcf3ce44SJohn Forte 	if (ilp != ilp_check) {
4786fcf3ce44SJohn Forte 		/*
4787fcf3ce44SJohn Forte 		 * The iSCSI target that we found on the HBA link is
4788fcf3ce44SJohn Forte 		 * different than the iSCSI target that was stored as
4789fcf3ce44SJohn Forte 		 * private data on the pathinfo node.
4790fcf3ce44SJohn Forte 		 */
4791fcf3ce44SJohn Forte 		return (DDI_NOT_WELL_FORMED);
4792fcf3ce44SJohn Forte 	}
4793fcf3ce44SJohn Forte 	/*
4794fcf3ce44SJohn Forte 	 * | End of consistency check				|
4795fcf3ce44SJohn Forte 	 * +----------------------------------------------------+
4796fcf3ce44SJohn Forte 	 */
4797fcf3ce44SJohn Forte 
4798fcf3ce44SJohn Forte 	hba_tran->tran_tgt_private = ilp;
4799fcf3ce44SJohn Forte 
4800fcf3ce44SJohn Forte 	target_port_name[0] = '\0';
4801fcf3ce44SJohn Forte 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4802fcf3ce44SJohn Forte 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4803fcf3ce44SJohn Forte 		    "%02x%02x%02x%02x%02x%02x,%s",
4804fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4805fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4806fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4807fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_name);
4808fcf3ce44SJohn Forte 	} else {
4809fcf3ce44SJohn Forte 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4810fcf3ce44SJohn Forte 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4811fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4812fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4813fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4814fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4815fcf3ce44SJohn Forte 	}
4816fcf3ce44SJohn Forte 
48174c06356bSdh 	if (mdi_prop_update_string(pip,
48184c06356bSdh 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
48194c06356bSdh 		cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating '"
48204c06356bSdh 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Path(%p) "
48214c06356bSdh 		    "for Target(%s), Lun(%d) Failed",
4822fcf3ce44SJohn Forte 		    (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num);
4823fcf3ce44SJohn Forte 	}
4824fcf3ce44SJohn Forte 
4825fcf3ce44SJohn Forte 	return (DDI_SUCCESS);
4826fcf3ce44SJohn Forte }
4827fcf3ce44SJohn Forte 
4828fcf3ce44SJohn Forte /*
4829fcf3ce44SJohn Forte  * iscsi_phys_lun_init - attempts to complete a ndi binding
4830fcf3ce44SJohn Forte  *
4831fcf3ce44SJohn Forte  * This routine is used to associate the tran_tgt_private to our
4832fcf3ce44SJohn Forte  * ilp structure.  This function is indirectly called from our
4833fcf3ce44SJohn Forte  * iscsi_lun_create_xxx routines.  These routines must prevent
4834fcf3ce44SJohn Forte  * the session and lun lists from changing during this call.
4835fcf3ce44SJohn Forte  */
4836fcf3ce44SJohn Forte static int
iscsi_phys_lun_init(dev_info_t * hba_dip,dev_info_t * lun_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)4837fcf3ce44SJohn Forte iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4838fcf3ce44SJohn Forte     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4839fcf3ce44SJohn Forte {
4840fcf3ce44SJohn Forte 	int		rtn	= DDI_SUCCESS;
4841fcf3ce44SJohn Forte 	iscsi_hba_t	*ihp	= NULL;
4842fcf3ce44SJohn Forte 	iscsi_sess_t	*isp	= NULL;
4843fcf3ce44SJohn Forte 	iscsi_lun_t	*ilp	= NULL;
4844fcf3ce44SJohn Forte 	char		target_port_name[MAX_NAME_PROP_SIZE];
4845fcf3ce44SJohn Forte 	int		*words = NULL;
4846fcf3ce44SJohn Forte 	uint_t		nwords = 0;
4847fcf3ce44SJohn Forte 
4848fcf3ce44SJohn Forte 	ASSERT(hba_dip);
4849fcf3ce44SJohn Forte 	ASSERT(lun_dip);
4850fcf3ce44SJohn Forte 	ASSERT(hba_tran);
4851fcf3ce44SJohn Forte 	ASSERT(sd);
4852fcf3ce44SJohn Forte 	ihp = (iscsi_hba_t *)hba_tran->tran_hba_private;
4853fcf3ce44SJohn Forte 	ASSERT(ihp);
4854fcf3ce44SJohn Forte 
4855fcf3ce44SJohn Forte 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip,
4856fcf3ce44SJohn Forte 	    DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) {
4857fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:"
4858fcf3ce44SJohn Forte 		    "lun for %s (instance %d)", ddi_get_name(lun_dip),
4859fcf3ce44SJohn Forte 		    ddi_get_instance(lun_dip));
4860fcf3ce44SJohn Forte 		return (DDI_FAILURE);
4861fcf3ce44SJohn Forte 	}
4862fcf3ce44SJohn Forte 
4863fcf3ce44SJohn Forte 	if (nwords == 0) {
4864fcf3ce44SJohn Forte 		ddi_prop_free(words);
4865fcf3ce44SJohn Forte 		return (DDI_FAILURE);
4866fcf3ce44SJohn Forte 	}
4867fcf3ce44SJohn Forte 
4868fcf3ce44SJohn Forte 	ASSERT(words != NULL);
4869fcf3ce44SJohn Forte 
4870fcf3ce44SJohn Forte 	/* See if we already created this session */
4871fcf3ce44SJohn Forte 
4872fcf3ce44SJohn Forte 	/* Walk the HBA's session list */
4873fcf3ce44SJohn Forte 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4874fcf3ce44SJohn Forte 		/* compare target name as the unique identifier */
4875fcf3ce44SJohn Forte 		if (sd->sd_address.a_target == isp->sess_oid) {
4876fcf3ce44SJohn Forte 			/* found match */
4877fcf3ce44SJohn Forte 			break;
4878fcf3ce44SJohn Forte 		}
4879fcf3ce44SJohn Forte 	}
4880fcf3ce44SJohn Forte 
4881fcf3ce44SJohn Forte 	/* If we found matching session continue searching for tgt */
4882fcf3ce44SJohn Forte 	if (isp != NULL) {
4883fcf3ce44SJohn Forte 		/*
4884fcf3ce44SJohn Forte 		 * Search for the matching iscsi lun structure.  We don't
4885fcf3ce44SJohn Forte 		 * need to hold the READER for the lun list at this point.
4886fcf3ce44SJohn Forte 		 * because the tran_get_name is being called from the online
4887fcf3ce44SJohn Forte 		 * function which is already holding a reader on the lun
4888fcf3ce44SJohn Forte 		 * list.
4889fcf3ce44SJohn Forte 		 */
4890fcf3ce44SJohn Forte 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
4891fcf3ce44SJohn Forte 			if (*words == ilp->lun_num) {
4892fcf3ce44SJohn Forte 				/* found match */
4893fcf3ce44SJohn Forte 				break;
4894fcf3ce44SJohn Forte 			}
4895fcf3ce44SJohn Forte 		}
4896fcf3ce44SJohn Forte 
4897fcf3ce44SJohn Forte 		if (ilp != NULL) {
4898fcf3ce44SJohn Forte 			/*
4899fcf3ce44SJohn Forte 			 * tgt found path it to the tran_lun_private
4900fcf3ce44SJohn Forte 			 * this is used later for fast access on
4901fcf3ce44SJohn Forte 			 * init_pkt and start
4902fcf3ce44SJohn Forte 			 */
4903fcf3ce44SJohn Forte 			hba_tran->tran_tgt_private = ilp;
4904fcf3ce44SJohn Forte 		} else {
4905fcf3ce44SJohn Forte 			/* tgt not found */
4906fcf3ce44SJohn Forte 			ddi_prop_free(words);
4907fcf3ce44SJohn Forte 			return (DDI_FAILURE);
4908fcf3ce44SJohn Forte 		}
4909fcf3ce44SJohn Forte 	} else {
4910fcf3ce44SJohn Forte 		/* sess not found */
4911fcf3ce44SJohn Forte 		ddi_prop_free(words);
4912fcf3ce44SJohn Forte 		return (DDI_FAILURE);
4913fcf3ce44SJohn Forte 	}
4914fcf3ce44SJohn Forte 	ddi_prop_free(words);
4915fcf3ce44SJohn Forte 
4916fcf3ce44SJohn Forte 	target_port_name[0] = '\0';
4917fcf3ce44SJohn Forte 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4918fcf3ce44SJohn Forte 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4919fcf3ce44SJohn Forte 		    "%02x%02x%02x%02x%02x%02x,%s",
4920fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4921fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4922fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4923fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_name);
4924fcf3ce44SJohn Forte 	} else {
4925fcf3ce44SJohn Forte 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4926fcf3ce44SJohn Forte 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4927fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4928fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4929fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4930fcf3ce44SJohn Forte 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4931fcf3ce44SJohn Forte 	}
4932fcf3ce44SJohn Forte 
4933fcf3ce44SJohn Forte 	if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip,
49344c06356bSdh 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
49354c06356bSdh 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating '"
49364c06356bSdh 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Target(%s), "
49374c06356bSdh 		    "Lun(%d) Failed", ilp->lun_sess->sess_name, ilp->lun_num);
4938fcf3ce44SJohn Forte 	}
4939fcf3ce44SJohn Forte 
4940fcf3ce44SJohn Forte 	return (rtn);
4941fcf3ce44SJohn Forte }
4942fcf3ce44SJohn Forte 
4943fcf3ce44SJohn Forte /*
4944fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4945fcf3ce44SJohn Forte  * | End of scsi_tran support routines					|
4946fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4947fcf3ce44SJohn Forte  */
4948fcf3ce44SJohn Forte 
4949fcf3ce44SJohn Forte /*
4950fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4951fcf3ce44SJohn Forte  * | Begin of struct utility routines					|
4952fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4953fcf3ce44SJohn Forte  */
4954fcf3ce44SJohn Forte 
4955fcf3ce44SJohn Forte 
4956fcf3ce44SJohn Forte /*
4957fcf3ce44SJohn Forte  * iscsi_set_default_login_params - This function sets the
4958fcf3ce44SJohn Forte  * driver default login params.  This is using during the
4959fcf3ce44SJohn Forte  * creation of our iSCSI HBA structure initialization by
4960fcf3ce44SJohn Forte  * could be used at other times to reset back to the defaults.
4961fcf3ce44SJohn Forte  */
4962fcf3ce44SJohn Forte void
iscsi_set_default_login_params(iscsi_login_params_t * params)4963fcf3ce44SJohn Forte iscsi_set_default_login_params(iscsi_login_params_t *params)
4964fcf3ce44SJohn Forte {
4965fcf3ce44SJohn Forte 	params->immediate_data		= ISCSI_DEFAULT_IMMEDIATE_DATA;
4966fcf3ce44SJohn Forte 	params->initial_r2t		= ISCSI_DEFAULT_INITIALR2T;
4967fcf3ce44SJohn Forte 	params->first_burst_length	= ISCSI_DEFAULT_FIRST_BURST_LENGTH;
4968fcf3ce44SJohn Forte 	params->max_burst_length	= ISCSI_DEFAULT_MAX_BURST_LENGTH;
4969fcf3ce44SJohn Forte 	params->data_pdu_in_order	= ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
4970fcf3ce44SJohn Forte 	params->data_sequence_in_order	= ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
4971fcf3ce44SJohn Forte 	params->default_time_to_wait	= ISCSI_DEFAULT_TIME_TO_WAIT;
4972fcf3ce44SJohn Forte 	params->default_time_to_retain	= ISCSI_DEFAULT_TIME_TO_RETAIN;
4973fcf3ce44SJohn Forte 	params->header_digest		= ISCSI_DEFAULT_HEADER_DIGEST;
4974fcf3ce44SJohn Forte 	params->data_digest		= ISCSI_DEFAULT_DATA_DIGEST;
4975fcf3ce44SJohn Forte 	params->max_recv_data_seg_len	= ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
4976fcf3ce44SJohn Forte 	params->max_xmit_data_seg_len	= ISCSI_DEFAULT_MAX_XMIT_SEG_LEN;
4977fcf3ce44SJohn Forte 	params->max_connections		= ISCSI_DEFAULT_MAX_CONNECTIONS;
4978fcf3ce44SJohn Forte 	params->max_outstanding_r2t	= ISCSI_DEFAULT_MAX_OUT_R2T;
4979fcf3ce44SJohn Forte 	params->error_recovery_level	= ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
4980fcf3ce44SJohn Forte 	params->ifmarker		= ISCSI_DEFAULT_IFMARKER;
4981fcf3ce44SJohn Forte 	params->ofmarker		= ISCSI_DEFAULT_OFMARKER;
4982fcf3ce44SJohn Forte }
4983fcf3ce44SJohn Forte 
4984aff4bce5Syi zhang - Sun Microsystems - Beijing China /* Helper function to sets the driver default tunable parameters */
4985aff4bce5Syi zhang - Sun Microsystems - Beijing China static void
iscsi_set_default_tunable_params(iscsi_tunable_params_t * params)4986aff4bce5Syi zhang - Sun Microsystems - Beijing China iscsi_set_default_tunable_params(iscsi_tunable_params_t *params)
4987aff4bce5Syi zhang - Sun Microsystems - Beijing China {
4988aff4bce5Syi zhang - Sun Microsystems - Beijing China 	params->recv_login_rsp_timeout = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
4989aff4bce5Syi zhang - Sun Microsystems - Beijing China 	params->conn_login_max = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
4990aff4bce5Syi zhang - Sun Microsystems - Beijing China 	params->polling_login_delay = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
4991aff4bce5Syi zhang - Sun Microsystems - Beijing China }
4992fcf3ce44SJohn Forte 
4993fcf3ce44SJohn Forte /*
4994fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4995fcf3ce44SJohn Forte  * | End of struct utility routines				     |
4996fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
4997fcf3ce44SJohn Forte  */
4998fcf3ce44SJohn Forte 
4999fcf3ce44SJohn Forte /*
5000fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
5001fcf3ce44SJohn Forte  * | Begin of ioctl utility routines				    |
5002fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
5003fcf3ce44SJohn Forte  */
5004fcf3ce44SJohn Forte 
5005fcf3ce44SJohn Forte /*
5006fcf3ce44SJohn Forte  * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM
5007fcf3ce44SJohn Forte  * IOCTL
5008fcf3ce44SJohn Forte  */
5009fcf3ce44SJohn Forte int
iscsi_get_param(iscsi_login_params_t * params,boolean_t valid_flag,iscsi_param_get_t * ipgp)5010fcf3ce44SJohn Forte iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag,
50117284664aSJoshua M. Clulow     iscsi_param_get_t *ipgp)
50127284664aSJoshua M. Clulow {
5013fcf3ce44SJohn Forte 	int rtn = 0;
5014fcf3ce44SJohn Forte 
5015fcf3ce44SJohn Forte 	/* ---- Default to settable, possibly changed later ---- */
5016fcf3ce44SJohn Forte 	ipgp->g_value.v_valid    = valid_flag;
5017fcf3ce44SJohn Forte 	ipgp->g_value.v_settable = B_TRUE;
5018fcf3ce44SJohn Forte 
5019fcf3ce44SJohn Forte 	switch (ipgp->g_param) {
5020fcf3ce44SJohn Forte 	/*
5021fcf3ce44SJohn Forte 	 * Boolean parameters
5022fcf3ce44SJohn Forte 	 */
5023fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
5024fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_current =
5025fcf3ce44SJohn Forte 		    params->data_sequence_in_order;
5026fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_default =
5027fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
5028fcf3ce44SJohn Forte 		break;
5029fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
5030fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_current =
5031fcf3ce44SJohn Forte 		    params->immediate_data;
5032fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_default =
5033fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
5034fcf3ce44SJohn Forte 		break;
5035fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_INITIAL_R2T:
5036fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_current =
5037fcf3ce44SJohn Forte 		    params->initial_r2t;
5038fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_default =
5039fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
5040fcf3ce44SJohn Forte 		break;
5041fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
5042fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_current =
5043fcf3ce44SJohn Forte 		    params->data_pdu_in_order;
5044fcf3ce44SJohn Forte 		ipgp->g_value.v_bool.b_default =
5045fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
5046fcf3ce44SJohn Forte 		break;
5047fcf3ce44SJohn Forte 
5048fcf3ce44SJohn Forte 	/*
5049fcf3ce44SJohn Forte 	 * Integer parameters
5050fcf3ce44SJohn Forte 	 */
5051fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
5052fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current = params->header_digest;
5053fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST;
5054fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 0;
5055fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST;
5056fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5057fcf3ce44SJohn Forte 		break;
5058fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_DATA_DIGEST:
5059fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current = params->data_digest;
5060fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST;
5061fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 0;
5062fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST;
5063fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5064fcf3ce44SJohn Forte 		break;
5065fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
5066fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5067fcf3ce44SJohn Forte 		    params->default_time_to_retain;
5068fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5069fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_TIME_TO_RETAIN;
5070fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 0;
5071fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN;
5072fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5073fcf3ce44SJohn Forte 		break;
5074fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
5075fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5076fcf3ce44SJohn Forte 		    params->default_time_to_wait;
5077fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5078fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_TIME_TO_WAIT;
5079fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 0;
5080fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT;
5081fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5082fcf3ce44SJohn Forte 		break;
5083fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
5084fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5085fcf3ce44SJohn Forte 		    params->error_recovery_level;
5086fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5087fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
5088fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 0;
5089fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL;
5090fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5091fcf3ce44SJohn Forte 		ipgp->g_value.v_settable = B_FALSE;
5092fcf3ce44SJohn Forte 		break;
5093fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
5094fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5095fcf3ce44SJohn Forte 		    params->first_burst_length;
5096fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5097fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_FIRST_BURST_LENGTH;
5098fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 512;
5099fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH;
5100fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5101fcf3ce44SJohn Forte 		break;
5102fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
5103fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5104fcf3ce44SJohn Forte 		    params->max_burst_length;
5105fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5106fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_MAX_BURST_LENGTH;
5107fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 512;
5108fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH;
5109fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5110fcf3ce44SJohn Forte 		break;
5111fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5112fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5113fcf3ce44SJohn Forte 		    params->max_connections;
5114fcf3ce44SJohn Forte 		ipgp->g_value.v_settable = B_FALSE;
5115fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5116fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_MAX_CONNECTIONS;
5117fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 1;
5118fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS;
5119fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5120fcf3ce44SJohn Forte 		break;
5121fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5122fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5123fcf3ce44SJohn Forte 		    params->max_outstanding_r2t;
5124fcf3ce44SJohn Forte 		ipgp->g_value.v_settable = B_FALSE;
5125fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5126fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_MAX_OUT_R2T;
5127fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 1;
5128fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T;
5129fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5130fcf3ce44SJohn Forte 		break;
5131fcf3ce44SJohn Forte 	case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5132fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_current =
5133fcf3ce44SJohn Forte 		    params->max_recv_data_seg_len;
5134fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_default =
5135fcf3ce44SJohn Forte 		    ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
5136fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_min = 512;
5137fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_max =
5138fcf3ce44SJohn Forte 		    ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
5139fcf3ce44SJohn Forte 		ipgp->g_value.v_integer.i_incr = 1;
5140fcf3ce44SJohn Forte 		break;
5141fcf3ce44SJohn Forte 	default:
5142fcf3ce44SJohn Forte 		rtn = EINVAL;
5143fcf3ce44SJohn Forte 	}
5144fcf3ce44SJohn Forte 
5145fcf3ce44SJohn Forte 	return (rtn);
5146fcf3ce44SJohn Forte }
5147fcf3ce44SJohn Forte 
5148fcf3ce44SJohn Forte /*
5149fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
5150fcf3ce44SJohn Forte  * | End of ioctl utility routines                                      |
5151fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
5152fcf3ce44SJohn Forte  */
5153fcf3ce44SJohn Forte 
5154fcf3ce44SJohn Forte /*
5155fcf3ce44SJohn Forte  * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a
5156fcf3ce44SJohn Forte  * IEEE safe address.  IEEE addresses have a number of characters
5157fcf3ce44SJohn Forte  * set aside as reserved.
5158fcf3ce44SJohn Forte  */
5159fcf3ce44SJohn Forte static void
iscsi_get_name_from_iqn(char * name,int name_max_len)5160fcf3ce44SJohn Forte iscsi_get_name_from_iqn(char *name, int name_max_len)
5161fcf3ce44SJohn Forte {
5162fcf3ce44SJohn Forte 	char	*tmp		= NULL;
5163fcf3ce44SJohn Forte 	char	*oldch		= NULL;
5164fcf3ce44SJohn Forte 	char	*newch		= NULL;
5165fcf3ce44SJohn Forte 
5166fcf3ce44SJohn Forte 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5167fcf3ce44SJohn Forte 
5168fcf3ce44SJohn Forte 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5169fcf3ce44SJohn Forte 	    oldch++, newch++) {
5170fcf3ce44SJohn Forte 		switch (*oldch) {
5171fcf3ce44SJohn Forte 		case ':':
5172fcf3ce44SJohn Forte 			*newch++ = '%';
5173fcf3ce44SJohn Forte 			*newch++ = '3';
5174fcf3ce44SJohn Forte 			*newch = 'A';
5175fcf3ce44SJohn Forte 			break;
5176fcf3ce44SJohn Forte 		case ' ':
5177fcf3ce44SJohn Forte 			*newch++ = '%';
5178fcf3ce44SJohn Forte 			*newch++ = '2';
5179fcf3ce44SJohn Forte 			*newch = '0';
5180fcf3ce44SJohn Forte 			break;
5181fcf3ce44SJohn Forte 		case '@':
5182fcf3ce44SJohn Forte 			*newch++ = '%';
5183fcf3ce44SJohn Forte 			*newch++ = '4';
5184fcf3ce44SJohn Forte 			*newch = '0';
5185fcf3ce44SJohn Forte 			break;
5186fcf3ce44SJohn Forte 		case '/':
5187fcf3ce44SJohn Forte 			*newch++ = '%';
5188fcf3ce44SJohn Forte 			*newch++ = '2';
5189fcf3ce44SJohn Forte 			*newch = 'F';
5190fcf3ce44SJohn Forte 			break;
5191fcf3ce44SJohn Forte 		default:
5192fcf3ce44SJohn Forte 			*newch = *oldch;
5193fcf3ce44SJohn Forte 		}
5194fcf3ce44SJohn Forte 	}
5195fcf3ce44SJohn Forte 	(void) strncpy(name, tmp, name_max_len);
5196fcf3ce44SJohn Forte 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5197fcf3ce44SJohn Forte }
5198fcf3ce44SJohn Forte 
5199fcf3ce44SJohn Forte /*
5200fcf3ce44SJohn Forte  * iscsi_get_name_to_iqn - Converts IEEE safe address back
5201fcf3ce44SJohn Forte  * into a iscsi iqn/eui.
5202fcf3ce44SJohn Forte  */
5203fcf3ce44SJohn Forte static void
iscsi_get_name_to_iqn(char * name,int name_max_len)5204fcf3ce44SJohn Forte iscsi_get_name_to_iqn(char *name, int name_max_len)
5205fcf3ce44SJohn Forte {
5206fcf3ce44SJohn Forte 	char	*tmp		= NULL;
5207fcf3ce44SJohn Forte 	char	*oldch		= NULL;
5208fcf3ce44SJohn Forte 	char	*newch		= NULL;
5209fcf3ce44SJohn Forte 
5210fcf3ce44SJohn Forte 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5211fcf3ce44SJohn Forte 
5212fcf3ce44SJohn Forte 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5213fcf3ce44SJohn Forte 	    oldch++, newch++) {
5214fcf3ce44SJohn Forte 		if (*oldch == '%') {
5215fcf3ce44SJohn Forte 			switch (*(oldch+1)) {
5216fcf3ce44SJohn Forte 			case '2':
5217fcf3ce44SJohn Forte 				if (*(oldch+2) == '0') {
5218fcf3ce44SJohn Forte 					*newch = ' ';
5219fcf3ce44SJohn Forte 					oldch += 2;
5220fcf3ce44SJohn Forte 				} else if (*(oldch+2) == 'F') {
5221fcf3ce44SJohn Forte 					*newch = '/';
5222fcf3ce44SJohn Forte 					oldch += 2;
5223fcf3ce44SJohn Forte 				} else {
5224fcf3ce44SJohn Forte 					*newch = *oldch;
5225fcf3ce44SJohn Forte 				}
5226fcf3ce44SJohn Forte 				break;
5227fcf3ce44SJohn Forte 			case '3':
5228fcf3ce44SJohn Forte 				if (*(oldch+2) == 'A') {
5229fcf3ce44SJohn Forte 					*newch = ':';
5230fcf3ce44SJohn Forte 					oldch += 2;
5231fcf3ce44SJohn Forte 				} else {
5232fcf3ce44SJohn Forte 					*newch = *oldch;
5233fcf3ce44SJohn Forte 				}
5234fcf3ce44SJohn Forte 				break;
5235fcf3ce44SJohn Forte 			case '4':
5236fcf3ce44SJohn Forte 				if (*(oldch+2) == '0') {
5237fcf3ce44SJohn Forte 					*newch = '@';
5238fcf3ce44SJohn Forte 					oldch += 2;
5239fcf3ce44SJohn Forte 				} else {
5240fcf3ce44SJohn Forte 					*newch = *oldch;
5241fcf3ce44SJohn Forte 				}
5242fcf3ce44SJohn Forte 				break;
5243fcf3ce44SJohn Forte 			default:
5244fcf3ce44SJohn Forte 				*newch = *oldch;
5245fcf3ce44SJohn Forte 			}
5246fcf3ce44SJohn Forte 		} else {
5247fcf3ce44SJohn Forte 			*newch = *oldch;
5248fcf3ce44SJohn Forte 		}
5249fcf3ce44SJohn Forte 	}
5250fcf3ce44SJohn Forte 	(void) strncpy(name, tmp, name_max_len);
5251fcf3ce44SJohn Forte 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5252fcf3ce44SJohn Forte }
5253fcf3ce44SJohn Forte 
5254fcf3ce44SJohn Forte /*
5255fcf3ce44SJohn Forte  * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl
5256fcf3ce44SJohn Forte  *
5257fcf3ce44SJohn Forte  * On return 0 means persisted parameter found
5258fcf3ce44SJohn Forte  */
5259fcf3ce44SJohn Forte int
iscsi_get_persisted_param(uchar_t * name,iscsi_param_get_t * ipgp,iscsi_login_params_t * params)5260fcf3ce44SJohn Forte iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp,
5261fcf3ce44SJohn Forte     iscsi_login_params_t *params)
5262fcf3ce44SJohn Forte {
5263fcf3ce44SJohn Forte 	int rtn = 1;
5264fcf3ce44SJohn Forte 	persistent_param_t *pparam;
5265fcf3ce44SJohn Forte 
5266fcf3ce44SJohn Forte 	if (name == NULL || strlen((char *)name) == 0) {
5267fcf3ce44SJohn Forte 		return (rtn);
5268fcf3ce44SJohn Forte 	}
5269fcf3ce44SJohn Forte 
5270fcf3ce44SJohn Forte 	pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP);
5271fcf3ce44SJohn Forte 
5272fcf3ce44SJohn Forte 	if (persistent_param_get((char *)name, pparam) == B_TRUE) {
5273fcf3ce44SJohn Forte 		if (pparam->p_bitmap & (1 << ipgp->g_param)) {
5274fcf3ce44SJohn Forte 			/* Found configured parameter. */
5275fcf3ce44SJohn Forte 			bcopy(&pparam->p_params, params, sizeof (*params));
5276fcf3ce44SJohn Forte 			rtn = 0;
5277fcf3ce44SJohn Forte 		}
5278fcf3ce44SJohn Forte 	}
5279fcf3ce44SJohn Forte 
5280fcf3ce44SJohn Forte 	kmem_free(pparam, sizeof (*pparam));
5281fcf3ce44SJohn Forte 
5282fcf3ce44SJohn Forte 	return (rtn);
5283fcf3ce44SJohn Forte }
5284fcf3ce44SJohn Forte 
5285fcf3ce44SJohn Forte /*
5286fcf3ce44SJohn Forte  * iscsi_override_target_default - helper function set the target's default
5287fcf3ce44SJohn Forte  * login parameter if there is a configured initiator parameter.
5288fcf3ce44SJohn Forte  *
5289fcf3ce44SJohn Forte  */
5290fcf3ce44SJohn Forte static void
iscsi_override_target_default(iscsi_hba_t * ihp,iscsi_param_get_t * ipg)5291fcf3ce44SJohn Forte iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg)
5292fcf3ce44SJohn Forte {
5293fcf3ce44SJohn Forte 	persistent_param_t *pp;
5294fcf3ce44SJohn Forte 	iscsi_login_params_t *params;
5295fcf3ce44SJohn Forte 
5296fcf3ce44SJohn Forte 	pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP);
5297fcf3ce44SJohn Forte 	if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) {
5298fcf3ce44SJohn Forte 		if (pp->p_bitmap & (1 << ipg->g_param)) {
5299fcf3ce44SJohn Forte 			params = &pp->p_params;
5300fcf3ce44SJohn Forte 			switch (ipg->g_param) {
5301fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
5302fcf3ce44SJohn Forte 				ipg->g_value.v_bool.b_default =
5303fcf3ce44SJohn Forte 				    params->data_sequence_in_order;
5304fcf3ce44SJohn Forte 				break;
5305fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
5306fcf3ce44SJohn Forte 				ipg->g_value.v_bool.b_default =
5307fcf3ce44SJohn Forte 				    params->immediate_data;
5308fcf3ce44SJohn Forte 				break;
5309fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_INITIAL_R2T:
5310fcf3ce44SJohn Forte 				ipg->g_value.v_bool.b_default =
5311fcf3ce44SJohn Forte 				    params->initial_r2t;
5312fcf3ce44SJohn Forte 				break;
5313fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
5314fcf3ce44SJohn Forte 				ipg->g_value.v_bool.b_default =
5315fcf3ce44SJohn Forte 				    params->data_pdu_in_order;
5316fcf3ce44SJohn Forte 				break;
5317fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
5318fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5319fcf3ce44SJohn Forte 				    params->header_digest;
5320fcf3ce44SJohn Forte 				break;
5321fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_DATA_DIGEST:
5322fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5323fcf3ce44SJohn Forte 				    params->data_digest;
5324fcf3ce44SJohn Forte 				break;
5325fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
5326fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5327fcf3ce44SJohn Forte 				    params->default_time_to_retain;
5328fcf3ce44SJohn Forte 				break;
5329fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
5330fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5331fcf3ce44SJohn Forte 				    params->default_time_to_wait;
5332fcf3ce44SJohn Forte 				break;
5333fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
5334fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5335fcf3ce44SJohn Forte 				    params->error_recovery_level;
5336fcf3ce44SJohn Forte 				break;
5337fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
5338fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5339fcf3ce44SJohn Forte 				    params->first_burst_length;
5340fcf3ce44SJohn Forte 				break;
5341fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
5342fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5343fcf3ce44SJohn Forte 				    params->max_burst_length;
5344fcf3ce44SJohn Forte 				break;
5345fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5346fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5347fcf3ce44SJohn Forte 				    params->max_connections;
5348fcf3ce44SJohn Forte 				break;
5349fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5350fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5351fcf3ce44SJohn Forte 				    params->max_outstanding_r2t;
5352fcf3ce44SJohn Forte 				break;
5353fcf3ce44SJohn Forte 			case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5354fcf3ce44SJohn Forte 				ipg->g_value.v_integer.i_default =
5355fcf3ce44SJohn Forte 				    params->max_xmit_data_seg_len;
5356fcf3ce44SJohn Forte 				break;
5357fcf3ce44SJohn Forte 			default:
5358fcf3ce44SJohn Forte 				break;
5359fcf3ce44SJohn Forte 			}
5360fcf3ce44SJohn Forte 		}
5361fcf3ce44SJohn Forte 	}
5362fcf3ce44SJohn Forte 	kmem_free(pp, sizeof (*pp));
5363fcf3ce44SJohn Forte }
53646cefaae1SJack Meng 
53656cefaae1SJack Meng static boolean_t
iscsi_cmp_boot_sess_oid(iscsi_hba_t * ihp,uint32_t oid)53666cefaae1SJack Meng iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid)
53676cefaae1SJack Meng {
53686cefaae1SJack Meng 	iscsi_sess_t *isp = NULL;
53696cefaae1SJack Meng 
53706cefaae1SJack Meng 	if (iscsi_chk_bootlun_mpxio(ihp)) {
53716cefaae1SJack Meng 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
53726cefaae1SJack Meng 			if ((isp->sess_oid == oid) && isp->sess_boot) {
53736cefaae1SJack Meng 				/* oid is session object */
53746cefaae1SJack Meng 				break;
53756cefaae1SJack Meng 			}
53766cefaae1SJack Meng 			if ((isp->sess_target_oid == oid) && isp->sess_boot) {
53776cefaae1SJack Meng 				/*
53786cefaae1SJack Meng 				 * oid is target object while
53796cefaae1SJack Meng 				 * this session is boot session
53806cefaae1SJack Meng 				 */
53816cefaae1SJack Meng 				break;
53826cefaae1SJack Meng 			}
53836cefaae1SJack Meng 		}
53846cefaae1SJack Meng 		if (oid == ihp->hba_oid) {
53856cefaae1SJack Meng 			/* oid is initiator object id */
53866cefaae1SJack Meng 			return (B_TRUE);
53876cefaae1SJack Meng 		} else if ((isp != NULL) && (isp->sess_boot)) {
53886cefaae1SJack Meng 			/* oid is boot session object id */
53896cefaae1SJack Meng 			return (B_TRUE);
53906cefaae1SJack Meng 		}
53916cefaae1SJack Meng 	}
53926cefaae1SJack Meng 	return (B_FALSE);
53936cefaae1SJack Meng }
53944246c8e9SJack Meng 
53954246c8e9SJack Meng /*
53964246c8e9SJack Meng  * iscsi_client_request_service - request the iSCSI service
53974246c8e9SJack Meng  *     returns true if the service is enabled and increases the count
53984246c8e9SJack Meng  *     returns false if the service is disabled
53994246c8e9SJack Meng  *     blocks until the service status is either enabled or disabled
54004246c8e9SJack Meng  */
54014246c8e9SJack Meng boolean_t
iscsi_client_request_service(iscsi_hba_t * ihp)54027284664aSJoshua M. Clulow iscsi_client_request_service(iscsi_hba_t *ihp)
54037284664aSJoshua M. Clulow {
54044246c8e9SJack Meng 	boolean_t	rval = B_TRUE;
54054246c8e9SJack Meng 
54064246c8e9SJack Meng 	mutex_enter(&ihp->hba_service_lock);
54074246c8e9SJack Meng 	while ((ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) ||
54084246c8e9SJack Meng 	    (ihp->hba_service_client_count == UINT_MAX)) {
54094246c8e9SJack Meng 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
54104246c8e9SJack Meng 	}
54114246c8e9SJack Meng 	if (ihp->hba_service_status == ISCSI_SERVICE_ENABLED) {
54124246c8e9SJack Meng 		ihp->hba_service_client_count++;
54134246c8e9SJack Meng 	} else {
54144246c8e9SJack Meng 		rval = B_FALSE;
54154246c8e9SJack Meng 	}
54164246c8e9SJack Meng 	mutex_exit(&ihp->hba_service_lock);
54174246c8e9SJack Meng 
54184246c8e9SJack Meng 	return (rval);
54194246c8e9SJack Meng }
54204246c8e9SJack Meng 
54214246c8e9SJack Meng /*
54224246c8e9SJack Meng  * iscsi_client_release_service - decrease the count and wake up
54234246c8e9SJack Meng  *     blocking threads if the count reaches zero
54244246c8e9SJack Meng  */
54254246c8e9SJack Meng void
iscsi_client_release_service(iscsi_hba_t * ihp)54267284664aSJoshua M. Clulow iscsi_client_release_service(iscsi_hba_t *ihp)
54277284664aSJoshua M. Clulow {
54284246c8e9SJack Meng 	mutex_enter(&ihp->hba_service_lock);
54294246c8e9SJack Meng 	ASSERT(ihp->hba_service_client_count > 0);
54304246c8e9SJack Meng 	ihp->hba_service_client_count--;
54314246c8e9SJack Meng 	if (ihp->hba_service_client_count == 0) {
54324246c8e9SJack Meng 		cv_broadcast(&ihp->hba_service_cv);
54334246c8e9SJack Meng 	}
54344246c8e9SJack Meng 	mutex_exit(&ihp->hba_service_lock);
54354246c8e9SJack Meng }
54364246c8e9SJack Meng 
54374246c8e9SJack Meng /*
54384246c8e9SJack Meng  * iscsi_enter_service_zone - enter the service zone, should be called
54394246c8e9SJack Meng  * before doing any modifications to the service status
54404246c8e9SJack Meng  * return TRUE if the zone is entered
54414246c8e9SJack Meng  *	  FALSE if no need to enter the zone
54424246c8e9SJack Meng  */
54434246c8e9SJack Meng static boolean_t
iscsi_enter_service_zone(iscsi_hba_t * ihp,uint32_t status)54447284664aSJoshua M. Clulow iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status)
54457284664aSJoshua M. Clulow {
54464246c8e9SJack Meng 	if ((status != ISCSI_SERVICE_ENABLED) &&
54474246c8e9SJack Meng 	    (status != ISCSI_SERVICE_DISABLED)) {
54484246c8e9SJack Meng 		return (B_FALSE);
54494246c8e9SJack Meng 	}
54504246c8e9SJack Meng 
54514246c8e9SJack Meng 	mutex_enter(&ihp->hba_service_lock);
54524246c8e9SJack Meng 	while (ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) {
54534246c8e9SJack Meng 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
54544246c8e9SJack Meng 	}
54554246c8e9SJack Meng 	if (ihp->hba_service_status == status) {
54564246c8e9SJack Meng 		mutex_exit(&ihp->hba_service_lock);
54574246c8e9SJack Meng 		return (B_FALSE);
54584246c8e9SJack Meng 	}
54594246c8e9SJack Meng 	ihp->hba_service_status = ISCSI_SERVICE_TRANSITION;
54604246c8e9SJack Meng 	while (ihp->hba_service_client_count > 0) {
54614246c8e9SJack Meng 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
54624246c8e9SJack Meng 	}
54634246c8e9SJack Meng 	mutex_exit(&ihp->hba_service_lock);
54644246c8e9SJack Meng 	return (B_TRUE);
54654246c8e9SJack Meng }
54664246c8e9SJack Meng 
54674246c8e9SJack Meng /*
54684246c8e9SJack Meng  * iscsi_exit_service_zone - exits the service zone and wakes up waiters
54694246c8e9SJack Meng  */
54704246c8e9SJack Meng static void
iscsi_exit_service_zone(iscsi_hba_t * ihp,uint32_t status)54717284664aSJoshua M. Clulow iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status)
54727284664aSJoshua M. Clulow {
54734246c8e9SJack Meng 	if ((status != ISCSI_SERVICE_ENABLED) &&
54744246c8e9SJack Meng 	    (status != ISCSI_SERVICE_DISABLED)) {
54754246c8e9SJack Meng 		return;
54764246c8e9SJack Meng 	}
54774246c8e9SJack Meng 
54784246c8e9SJack Meng 	mutex_enter(&ihp->hba_service_lock);
54794246c8e9SJack Meng 	ASSERT(ihp->hba_service_status == ISCSI_SERVICE_TRANSITION);
54804246c8e9SJack Meng 	ihp->hba_service_status = status;
54814246c8e9SJack Meng 	cv_broadcast(&ihp->hba_service_cv);
54824246c8e9SJack Meng 	mutex_exit(&ihp->hba_service_lock);
54834246c8e9SJack Meng }
54844246c8e9SJack Meng 
54854246c8e9SJack Meng static void
iscsi_check_miniroot(iscsi_hba_t * ihp)54867284664aSJoshua M. Clulow iscsi_check_miniroot(iscsi_hba_t *ihp)
54877284664aSJoshua M. Clulow {
54884246c8e9SJack Meng 	if (strncmp(rootfs.bo_name, "/ramdisk", 8) == 0) {
54894246c8e9SJack Meng 		/*
54904246c8e9SJack Meng 		 * in miniroot we don't have the persistent store
54914246c8e9SJack Meng 		 * so just to need to ensure an enabled status
54924246c8e9SJack Meng 		 */
54934246c8e9SJack Meng 		ihp->hba_service_status = ISCSI_SERVICE_ENABLED;
54944246c8e9SJack Meng 	}
54954246c8e9SJack Meng }
5496aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5497aff4bce5Syi zhang - Sun Microsystems - Beijing China static void
iscsi_get_tunable_default(iscsi_tunable_object_t * param)54987284664aSJoshua M. Clulow iscsi_get_tunable_default(iscsi_tunable_object_t *param)
54997284664aSJoshua M. Clulow {
5500aff4bce5Syi zhang - Sun Microsystems - Beijing China 	int	param_id = 0;
5501aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5502aff4bce5Syi zhang - Sun Microsystems - Beijing China 	param_id = 1 << (param->t_param - 1);
5503aff4bce5Syi zhang - Sun Microsystems - Beijing China 	param->t_set = B_FALSE;
5504aff4bce5Syi zhang - Sun Microsystems - Beijing China 	switch (param_id) {
5505aff4bce5Syi zhang - Sun Microsystems - Beijing China 	case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5506aff4bce5Syi zhang - Sun Microsystems - Beijing China 		param->t_value.v_integer = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
5507aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
5508aff4bce5Syi zhang - Sun Microsystems - Beijing China 	case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5509aff4bce5Syi zhang - Sun Microsystems - Beijing China 		param->t_value.v_integer = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
5510aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
5511aff4bce5Syi zhang - Sun Microsystems - Beijing China 	case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5512aff4bce5Syi zhang - Sun Microsystems - Beijing China 		param->t_value.v_integer = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
5513aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
5514aff4bce5Syi zhang - Sun Microsystems - Beijing China 	default:
5515aff4bce5Syi zhang - Sun Microsystems - Beijing China 		break;
5516aff4bce5Syi zhang - Sun Microsystems - Beijing China 	}
5517aff4bce5Syi zhang - Sun Microsystems - Beijing China }
5518aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5519aff4bce5Syi zhang - Sun Microsystems - Beijing China /*
5520aff4bce5Syi zhang - Sun Microsystems - Beijing China  * iscsi_get_persisted_tunable_param * - a helper to ISCSI_TUNABLE_PARAM_GET
5521aff4bce5Syi zhang - Sun Microsystems - Beijing China  * ioctl
5522aff4bce5Syi zhang - Sun Microsystems - Beijing China  * return:
55237284664aSJoshua M. Clulow  *    0		persisted tunable parameter found
5524aff4bce5Syi zhang - Sun Microsystems - Beijing China  *    1		persisted tunable parameter not found
5525aff4bce5Syi zhang - Sun Microsystems - Beijing China  */
5526aff4bce5Syi zhang - Sun Microsystems - Beijing China static int
iscsi_get_persisted_tunable_param(uchar_t * name,iscsi_tunable_object_t * tpsg)5527aff4bce5Syi zhang - Sun Microsystems - Beijing China iscsi_get_persisted_tunable_param(uchar_t *name, iscsi_tunable_object_t *tpsg)
5528aff4bce5Syi zhang - Sun Microsystems - Beijing China {
5529aff4bce5Syi zhang - Sun Microsystems - Beijing China 	int rtn = 1;
5530aff4bce5Syi zhang - Sun Microsystems - Beijing China 	int param_id = 0;
5531aff4bce5Syi zhang - Sun Microsystems - Beijing China 	persistent_tunable_param_t *pparam;
5532aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5533aff4bce5Syi zhang - Sun Microsystems - Beijing China 	if ((name == NULL) || strlen((char *)name) == 0) {
5534aff4bce5Syi zhang - Sun Microsystems - Beijing China 		return (rtn);
5535aff4bce5Syi zhang - Sun Microsystems - Beijing China 	}
5536aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5537aff4bce5Syi zhang - Sun Microsystems - Beijing China 	tpsg->t_set = B_FALSE;
5538aff4bce5Syi zhang - Sun Microsystems - Beijing China 	pparam = (persistent_tunable_param_t *)kmem_zalloc(sizeof (*pparam),
5539aff4bce5Syi zhang - Sun Microsystems - Beijing China 	    KM_SLEEP);
5540aff4bce5Syi zhang - Sun Microsystems - Beijing China 	if (persistent_get_tunable_param((char *)name, pparam) == B_TRUE) {
5541aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (pparam->p_bitmap & (1 << (tpsg->t_param - 1))) {
5542aff4bce5Syi zhang - Sun Microsystems - Beijing China 			tpsg->t_set = B_TRUE;
5543aff4bce5Syi zhang - Sun Microsystems - Beijing China 			param_id = 1 << (tpsg->t_param - 1);
5544aff4bce5Syi zhang - Sun Microsystems - Beijing China 			switch (param_id) {
5545aff4bce5Syi zhang - Sun Microsystems - Beijing China 			case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5546aff4bce5Syi zhang - Sun Microsystems - Beijing China 				tpsg->t_value.v_integer =
5547aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    pparam->p_params.recv_login_rsp_timeout;
5548aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
5549aff4bce5Syi zhang - Sun Microsystems - Beijing China 			case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5550aff4bce5Syi zhang - Sun Microsystems - Beijing China 				tpsg->t_value.v_integer =
5551aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    pparam->p_params.polling_login_delay;
5552aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
5553aff4bce5Syi zhang - Sun Microsystems - Beijing China 			case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5554aff4bce5Syi zhang - Sun Microsystems - Beijing China 				tpsg->t_value.v_integer =
5555aff4bce5Syi zhang - Sun Microsystems - Beijing China 				    pparam->p_params.conn_login_max;
5556aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
5557aff4bce5Syi zhang - Sun Microsystems - Beijing China 			default:
5558aff4bce5Syi zhang - Sun Microsystems - Beijing China 				break;
5559aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
5560aff4bce5Syi zhang - Sun Microsystems - Beijing China 			rtn = 0;
5561aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
5562aff4bce5Syi zhang - Sun Microsystems - Beijing China 	}
5563aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5564aff4bce5Syi zhang - Sun Microsystems - Beijing China 	kmem_free(pparam, sizeof (*pparam));
5565aff4bce5Syi zhang - Sun Microsystems - Beijing China 
5566aff4bce5Syi zhang - Sun Microsystems - Beijing China 	return (rtn);
5567aff4bce5Syi zhang - Sun Microsystems - Beijing China }
5568