xref: /onnv-gate/usr/src/uts/common/io/comstar/stmf/stmf.c (revision 12750:f559965e4a2f)
17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM  * CDDL HEADER START
37836SJohn.Forte@Sun.COM  *
47836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM  *
87836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM  * and limitations under the License.
127836SJohn.Forte@Sun.COM  *
137836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM  *
197836SJohn.Forte@Sun.COM  * CDDL HEADER END
207836SJohn.Forte@Sun.COM  */
217836SJohn.Forte@Sun.COM /*
2212314SJames.Moore@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
237836SJohn.Forte@Sun.COM  */
247836SJohn.Forte@Sun.COM 
257836SJohn.Forte@Sun.COM #include <sys/conf.h>
267836SJohn.Forte@Sun.COM #include <sys/file.h>
277836SJohn.Forte@Sun.COM #include <sys/ddi.h>
287836SJohn.Forte@Sun.COM #include <sys/sunddi.h>
297836SJohn.Forte@Sun.COM #include <sys/modctl.h>
307836SJohn.Forte@Sun.COM #include <sys/scsi/scsi.h>
31*12750SNattuvetty.Bhavyan@Sun.COM #include <sys/scsi/generic/persist.h>
327836SJohn.Forte@Sun.COM #include <sys/scsi/impl/scsi_reset_notify.h>
337836SJohn.Forte@Sun.COM #include <sys/disp.h>
347836SJohn.Forte@Sun.COM #include <sys/byteorder.h>
357836SJohn.Forte@Sun.COM #include <sys/atomic.h>
367836SJohn.Forte@Sun.COM #include <sys/ethernet.h>
377836SJohn.Forte@Sun.COM #include <sys/sdt.h>
387836SJohn.Forte@Sun.COM #include <sys/nvpair.h>
398662SJordan.Vaughan@Sun.com #include <sys/zone.h>
4011773STim.Szeto@Sun.COM #include <sys/id_space.h>
417836SJohn.Forte@Sun.COM 
4212571SViswanathan.Kannappan@Sun.COM #include <sys/stmf.h>
4312571SViswanathan.Kannappan@Sun.COM #include <sys/lpif.h>
4412571SViswanathan.Kannappan@Sun.COM #include <sys/portif.h>
4512571SViswanathan.Kannappan@Sun.COM #include <sys/stmf_ioctl.h>
4612571SViswanathan.Kannappan@Sun.COM #include <sys/pppt_ic_if.h>
4712571SViswanathan.Kannappan@Sun.COM 
4812571SViswanathan.Kannappan@Sun.COM #include "stmf_impl.h"
4912571SViswanathan.Kannappan@Sun.COM #include "lun_map.h"
5012571SViswanathan.Kannappan@Sun.COM #include "stmf_state.h"
5112571SViswanathan.Kannappan@Sun.COM #include "stmf_stats.h"
527836SJohn.Forte@Sun.COM 
5311978STim.Szeto@Sun.COM /*
5411978STim.Szeto@Sun.COM  * Lock order:
5511978STim.Szeto@Sun.COM  * stmf_state_lock --> ilport_lock/iss_lockp --> ilu_task_lock
5611978STim.Szeto@Sun.COM  */
5711978STim.Szeto@Sun.COM 
587836SJohn.Forte@Sun.COM static uint64_t stmf_session_counter = 0;
597836SJohn.Forte@Sun.COM static uint16_t stmf_rtpid_counter = 0;
6010725SJohn.Forte@Sun.COM /* start messages at 1 */
6110725SJohn.Forte@Sun.COM static uint64_t stmf_proxy_msg_id = 1;
6211096SJohn.Forte@Sun.COM #define	MSG_ID_TM_BIT	0x8000000000000000
63*12750SNattuvetty.Bhavyan@Sun.COM #define	ALIGNED_TO_8BYTE_BOUNDARY(i)	(((i) + 7) & ~7)
647836SJohn.Forte@Sun.COM 
657836SJohn.Forte@Sun.COM static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
667836SJohn.Forte@Sun.COM static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
677836SJohn.Forte@Sun.COM static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
687836SJohn.Forte@Sun.COM 	void **result);
697836SJohn.Forte@Sun.COM static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp);
707836SJohn.Forte@Sun.COM static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp);
717836SJohn.Forte@Sun.COM static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
727836SJohn.Forte@Sun.COM 	cred_t *credp, int *rval);
737836SJohn.Forte@Sun.COM static int stmf_get_stmf_state(stmf_state_desc_t *std);
747836SJohn.Forte@Sun.COM static int stmf_set_stmf_state(stmf_state_desc_t *std);
757836SJohn.Forte@Sun.COM static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu,
767836SJohn.Forte@Sun.COM     char *info);
7710725SJohn.Forte@Sun.COM static int stmf_set_alua_state(stmf_alua_state_desc_t *alua_state);
7810725SJohn.Forte@Sun.COM static void stmf_get_alua_state(stmf_alua_state_desc_t *alua_state);
7912625SJohn.Forte@Sun.COM 
8012625SJohn.Forte@Sun.COM static void stmf_task_audit(stmf_i_scsi_task_t *itask,
8112625SJohn.Forte@Sun.COM     task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf);
8212625SJohn.Forte@Sun.COM 
83*12750SNattuvetty.Bhavyan@Sun.COM static boolean_t stmf_base16_str_to_binary(char *c, int dplen, uint8_t *dp);
84*12750SNattuvetty.Bhavyan@Sun.COM static char stmf_ctoi(char c);
8510725SJohn.Forte@Sun.COM stmf_xfer_data_t *stmf_prepare_tpgs_data(uint8_t ilu_alua);
867836SJohn.Forte@Sun.COM void stmf_svc_init();
877836SJohn.Forte@Sun.COM stmf_status_t stmf_svc_fini();
887836SJohn.Forte@Sun.COM void stmf_svc(void *arg);
897836SJohn.Forte@Sun.COM void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info);
907836SJohn.Forte@Sun.COM void stmf_check_freetask();
917836SJohn.Forte@Sun.COM void stmf_abort_target_reset(scsi_task_t *task);
927836SJohn.Forte@Sun.COM stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task,
937836SJohn.Forte@Sun.COM 							int target_reset);
947836SJohn.Forte@Sun.COM void stmf_target_reset_poll(struct scsi_task *task);
957836SJohn.Forte@Sun.COM void stmf_handle_lun_reset(scsi_task_t *task);
967836SJohn.Forte@Sun.COM void stmf_handle_target_reset(scsi_task_t *task);
9712340SJohn.Forte@Sun.COM void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off);
989585STim.Szeto@Sun.COM int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
999585STim.Szeto@Sun.COM     uint32_t *err_ret);
1007836SJohn.Forte@Sun.COM int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi);
1019585STim.Szeto@Sun.COM int stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
1029585STim.Szeto@Sun.COM     uint32_t *err_ret);
1037836SJohn.Forte@Sun.COM void stmf_delete_ppd(stmf_pp_data_t *ppd);
1047836SJohn.Forte@Sun.COM void stmf_delete_all_ppds();
1057836SJohn.Forte@Sun.COM void stmf_trace_clear();
1067836SJohn.Forte@Sun.COM void stmf_worker_init();
1077836SJohn.Forte@Sun.COM stmf_status_t stmf_worker_fini();
1087836SJohn.Forte@Sun.COM void stmf_worker_mgmt();
1097836SJohn.Forte@Sun.COM void stmf_worker_task(void *arg);
11010725SJohn.Forte@Sun.COM static void stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss);
11110725SJohn.Forte@Sun.COM static stmf_status_t stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg,
11210725SJohn.Forte@Sun.COM     uint32_t type);
11310725SJohn.Forte@Sun.COM static stmf_status_t stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg);
11410725SJohn.Forte@Sun.COM static stmf_status_t stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg);
11510725SJohn.Forte@Sun.COM static stmf_status_t stmf_ic_rx_status(stmf_ic_status_msg_t *msg);
11610725SJohn.Forte@Sun.COM static stmf_status_t stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg);
11710725SJohn.Forte@Sun.COM void stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s);
11810725SJohn.Forte@Sun.COM 
11910725SJohn.Forte@Sun.COM /* pppt modhandle */
12010725SJohn.Forte@Sun.COM ddi_modhandle_t pppt_mod;
12110725SJohn.Forte@Sun.COM 
12210725SJohn.Forte@Sun.COM /* pppt modload imported functions */
12310725SJohn.Forte@Sun.COM stmf_ic_reg_port_msg_alloc_func_t ic_reg_port_msg_alloc;
12410725SJohn.Forte@Sun.COM stmf_ic_dereg_port_msg_alloc_func_t ic_dereg_port_msg_alloc;
12510725SJohn.Forte@Sun.COM stmf_ic_reg_lun_msg_alloc_func_t ic_reg_lun_msg_alloc;
12610725SJohn.Forte@Sun.COM stmf_ic_dereg_lun_msg_alloc_func_t ic_dereg_lun_msg_alloc;
12710725SJohn.Forte@Sun.COM stmf_ic_lun_active_msg_alloc_func_t ic_lun_active_msg_alloc;
12810725SJohn.Forte@Sun.COM stmf_ic_scsi_cmd_msg_alloc_func_t ic_scsi_cmd_msg_alloc;
12910725SJohn.Forte@Sun.COM stmf_ic_scsi_data_xfer_done_msg_alloc_func_t ic_scsi_data_xfer_done_msg_alloc;
13010725SJohn.Forte@Sun.COM stmf_ic_session_create_msg_alloc_func_t ic_session_reg_msg_alloc;
13110725SJohn.Forte@Sun.COM stmf_ic_session_destroy_msg_alloc_func_t ic_session_dereg_msg_alloc;
13210725SJohn.Forte@Sun.COM stmf_ic_tx_msg_func_t ic_tx_msg;
13310725SJohn.Forte@Sun.COM stmf_ic_msg_free_func_t ic_msg_free;
1347836SJohn.Forte@Sun.COM 
13511773STim.Szeto@Sun.COM static void stmf_itl_task_start(stmf_i_scsi_task_t *itask);
13611773STim.Szeto@Sun.COM static void stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask);
13711773STim.Szeto@Sun.COM static void stmf_itl_task_done(stmf_i_scsi_task_t *itask);
13811773STim.Szeto@Sun.COM 
13911773STim.Szeto@Sun.COM static void stmf_lport_xfer_start(stmf_i_scsi_task_t *itask,
14011773STim.Szeto@Sun.COM     stmf_data_buf_t *dbuf);
14111773STim.Szeto@Sun.COM static void stmf_lport_xfer_done(stmf_i_scsi_task_t *itask,
14211773STim.Szeto@Sun.COM     stmf_data_buf_t *dbuf);
14311773STim.Szeto@Sun.COM 
1449435STim.Szeto@Sun.COM static void stmf_update_kstat_lu_q(scsi_task_t *, void());
1459435STim.Szeto@Sun.COM static void stmf_update_kstat_lport_q(scsi_task_t *, void());
1469435STim.Szeto@Sun.COM static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *);
1479435STim.Szeto@Sun.COM static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *);
1489435STim.Szeto@Sun.COM 
14911773STim.Szeto@Sun.COM static int stmf_irport_compare(const void *void_irport1,
15011773STim.Szeto@Sun.COM     const void *void_irport2);
15111773STim.Szeto@Sun.COM static stmf_i_remote_port_t *stmf_irport_create(scsi_devid_desc_t *rport_devid);
15211773STim.Szeto@Sun.COM static void stmf_irport_destroy(stmf_i_remote_port_t *irport);
15311773STim.Szeto@Sun.COM static stmf_i_remote_port_t *stmf_irport_register(
15411773STim.Szeto@Sun.COM     scsi_devid_desc_t *rport_devid);
15511773STim.Szeto@Sun.COM static stmf_i_remote_port_t *stmf_irport_lookup_locked(
15611773STim.Szeto@Sun.COM     scsi_devid_desc_t *rport_devid);
15711773STim.Szeto@Sun.COM static void stmf_irport_deregister(stmf_i_remote_port_t *irport);
15811773STim.Szeto@Sun.COM 
15911773STim.Szeto@Sun.COM static void stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks);
16011773STim.Szeto@Sun.COM static void stmf_delete_itl_kstat_by_lport(char *);
16111773STim.Szeto@Sun.COM static void stmf_delete_itl_kstat_by_guid(char *);
16211773STim.Szeto@Sun.COM static int stmf_itl_kstat_compare(const void*, const void*);
16311773STim.Szeto@Sun.COM static stmf_i_itl_kstat_t *stmf_itl_kstat_lookup(char *kstat_nm);
16411773STim.Szeto@Sun.COM static stmf_i_itl_kstat_t *stmf_itl_kstat_create(stmf_itl_data_t *itl,
16511773STim.Szeto@Sun.COM     char *nm, scsi_devid_desc_t *lport, scsi_devid_desc_t *lun);
16611773STim.Szeto@Sun.COM 
1677836SJohn.Forte@Sun.COM extern struct mod_ops mod_driverops;
1687836SJohn.Forte@Sun.COM 
1697836SJohn.Forte@Sun.COM /* =====[ Tunables ]===== */
1707836SJohn.Forte@Sun.COM /* Internal tracing */
1717836SJohn.Forte@Sun.COM volatile int	stmf_trace_on = 1;
1727836SJohn.Forte@Sun.COM volatile int	stmf_trace_buf_size = (1 * 1024 * 1024);
1737836SJohn.Forte@Sun.COM /*
1747836SJohn.Forte@Sun.COM  * The reason default task timeout is 75 is because we want the
1757836SJohn.Forte@Sun.COM  * host to timeout 1st and mostly host timeout is 60 seconds.
1767836SJohn.Forte@Sun.COM  */
1777836SJohn.Forte@Sun.COM volatile int	stmf_default_task_timeout = 75;
1787836SJohn.Forte@Sun.COM /*
1797836SJohn.Forte@Sun.COM  * Setting this to one means, you are responsible for config load and keeping
1807836SJohn.Forte@Sun.COM  * things in sync with persistent database.
1817836SJohn.Forte@Sun.COM  */
1827836SJohn.Forte@Sun.COM volatile int	stmf_allow_modunload = 0;
1837836SJohn.Forte@Sun.COM 
1847836SJohn.Forte@Sun.COM volatile int stmf_max_nworkers = 256;
1857836SJohn.Forte@Sun.COM volatile int stmf_min_nworkers = 4;
1867836SJohn.Forte@Sun.COM volatile int stmf_worker_scale_down_delay = 20;
1877836SJohn.Forte@Sun.COM 
1887836SJohn.Forte@Sun.COM /* === [ Debugging and fault injection ] === */
1897836SJohn.Forte@Sun.COM #ifdef	DEBUG
1907836SJohn.Forte@Sun.COM volatile int stmf_drop_task_counter = 0;
1917836SJohn.Forte@Sun.COM volatile int stmf_drop_buf_counter = 0;
1927836SJohn.Forte@Sun.COM 
1937836SJohn.Forte@Sun.COM #endif
1947836SJohn.Forte@Sun.COM 
1957836SJohn.Forte@Sun.COM stmf_state_t		stmf_state;
1967836SJohn.Forte@Sun.COM static stmf_lu_t	*dlun0;
1977836SJohn.Forte@Sun.COM 
1987836SJohn.Forte@Sun.COM static uint8_t stmf_first_zero[] =
1997836SJohn.Forte@Sun.COM 	{ 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff };
2007836SJohn.Forte@Sun.COM static uint8_t stmf_first_one[] =
2017836SJohn.Forte@Sun.COM 	{ 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 };
2027836SJohn.Forte@Sun.COM 
2037836SJohn.Forte@Sun.COM static kmutex_t	trace_buf_lock;
2047836SJohn.Forte@Sun.COM static int	trace_buf_size;
2057836SJohn.Forte@Sun.COM static int	trace_buf_curndx;
2067836SJohn.Forte@Sun.COM caddr_t	stmf_trace_buf;
2077836SJohn.Forte@Sun.COM 
2087836SJohn.Forte@Sun.COM static enum {
2097836SJohn.Forte@Sun.COM 	STMF_WORKERS_DISABLED = 0,
2107836SJohn.Forte@Sun.COM 	STMF_WORKERS_ENABLING,
2117836SJohn.Forte@Sun.COM 	STMF_WORKERS_ENABLED
2127836SJohn.Forte@Sun.COM } stmf_workers_state = STMF_WORKERS_DISABLED;
2137836SJohn.Forte@Sun.COM static int stmf_i_max_nworkers;
2147836SJohn.Forte@Sun.COM static int stmf_i_min_nworkers;
2157836SJohn.Forte@Sun.COM static int stmf_nworkers_cur;		/* # of workers currently running */
2167836SJohn.Forte@Sun.COM static int stmf_nworkers_needed;	/* # of workers need to be running */
2177836SJohn.Forte@Sun.COM static int stmf_worker_sel_counter = 0;
2187836SJohn.Forte@Sun.COM static uint32_t stmf_cur_ntasks = 0;
2197836SJohn.Forte@Sun.COM static clock_t stmf_wm_last = 0;
2207836SJohn.Forte@Sun.COM /*
2217836SJohn.Forte@Sun.COM  * This is equal to stmf_nworkers_cur while we are increasing # workers and
2227836SJohn.Forte@Sun.COM  * stmf_nworkers_needed while we are decreasing the worker count.
2237836SJohn.Forte@Sun.COM  */
2247836SJohn.Forte@Sun.COM static int stmf_nworkers_accepting_cmds;
2257836SJohn.Forte@Sun.COM static stmf_worker_t *stmf_workers = NULL;
2267836SJohn.Forte@Sun.COM static clock_t stmf_worker_mgmt_delay = 2;
2277836SJohn.Forte@Sun.COM static clock_t stmf_worker_scale_down_timer = 0;
2287836SJohn.Forte@Sun.COM static int stmf_worker_scale_down_qd = 0;
2297836SJohn.Forte@Sun.COM 
2307836SJohn.Forte@Sun.COM static struct cb_ops stmf_cb_ops = {
2317836SJohn.Forte@Sun.COM 	stmf_open,			/* open */
2327836SJohn.Forte@Sun.COM 	stmf_close,			/* close */
2337836SJohn.Forte@Sun.COM 	nodev,				/* strategy */
2347836SJohn.Forte@Sun.COM 	nodev,				/* print */
2357836SJohn.Forte@Sun.COM 	nodev,				/* dump */
2367836SJohn.Forte@Sun.COM 	nodev,				/* read */
2377836SJohn.Forte@Sun.COM 	nodev,				/* write */
2387836SJohn.Forte@Sun.COM 	stmf_ioctl,			/* ioctl */
2397836SJohn.Forte@Sun.COM 	nodev,				/* devmap */
2407836SJohn.Forte@Sun.COM 	nodev,				/* mmap */
2417836SJohn.Forte@Sun.COM 	nodev,				/* segmap */
2427836SJohn.Forte@Sun.COM 	nochpoll,			/* chpoll */
2437836SJohn.Forte@Sun.COM 	ddi_prop_op,			/* cb_prop_op */
2447836SJohn.Forte@Sun.COM 	0,				/* streamtab */
2457836SJohn.Forte@Sun.COM 	D_NEW | D_MP,			/* cb_flag */
2467836SJohn.Forte@Sun.COM 	CB_REV,				/* rev */
2477836SJohn.Forte@Sun.COM 	nodev,				/* aread */
2487836SJohn.Forte@Sun.COM 	nodev				/* awrite */
2497836SJohn.Forte@Sun.COM };
2507836SJohn.Forte@Sun.COM 
2517836SJohn.Forte@Sun.COM static struct dev_ops stmf_ops = {
2527836SJohn.Forte@Sun.COM 	DEVO_REV,
2537836SJohn.Forte@Sun.COM 	0,
2547836SJohn.Forte@Sun.COM 	stmf_getinfo,
2557836SJohn.Forte@Sun.COM 	nulldev,		/* identify */
2567836SJohn.Forte@Sun.COM 	nulldev,		/* probe */
2577836SJohn.Forte@Sun.COM 	stmf_attach,
2587836SJohn.Forte@Sun.COM 	stmf_detach,
2597836SJohn.Forte@Sun.COM 	nodev,			/* reset */
2607836SJohn.Forte@Sun.COM 	&stmf_cb_ops,
2617836SJohn.Forte@Sun.COM 	NULL,			/* bus_ops */
2627836SJohn.Forte@Sun.COM 	NULL			/* power */
2637836SJohn.Forte@Sun.COM };
2647836SJohn.Forte@Sun.COM 
2659435STim.Szeto@Sun.COM #define	STMF_NAME		"COMSTAR STMF"
2669435STim.Szeto@Sun.COM #define	STMF_MODULE_NAME	"stmf"
2677836SJohn.Forte@Sun.COM 
2687836SJohn.Forte@Sun.COM static struct modldrv modldrv = {
2697836SJohn.Forte@Sun.COM 	&mod_driverops,
2707836SJohn.Forte@Sun.COM 	STMF_NAME,
2717836SJohn.Forte@Sun.COM 	&stmf_ops
2727836SJohn.Forte@Sun.COM };
2737836SJohn.Forte@Sun.COM 
2747836SJohn.Forte@Sun.COM static struct modlinkage modlinkage = {
2757836SJohn.Forte@Sun.COM 	MODREV_1,
2767836SJohn.Forte@Sun.COM 	&modldrv,
2777836SJohn.Forte@Sun.COM 	NULL
2787836SJohn.Forte@Sun.COM };
2797836SJohn.Forte@Sun.COM 
2807836SJohn.Forte@Sun.COM int
_init(void)2817836SJohn.Forte@Sun.COM _init(void)
2827836SJohn.Forte@Sun.COM {
2837836SJohn.Forte@Sun.COM 	int ret;
2847836SJohn.Forte@Sun.COM 
2857836SJohn.Forte@Sun.COM 	ret = mod_install(&modlinkage);
2867836SJohn.Forte@Sun.COM 	if (ret)
2877836SJohn.Forte@Sun.COM 		return (ret);
2887836SJohn.Forte@Sun.COM 	stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP);
2897836SJohn.Forte@Sun.COM 	trace_buf_size = stmf_trace_buf_size;
2907836SJohn.Forte@Sun.COM 	trace_buf_curndx = 0;
2917836SJohn.Forte@Sun.COM 	mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0);
2927836SJohn.Forte@Sun.COM 	bzero(&stmf_state, sizeof (stmf_state_t));
2937836SJohn.Forte@Sun.COM 	/* STMF service is off by default */
2947836SJohn.Forte@Sun.COM 	stmf_state.stmf_service_running = 0;
29512682SSrivijitha.Dugganapalli@Sun.COM 	/* default lu/lport states are online */
29612682SSrivijitha.Dugganapalli@Sun.COM 	stmf_state.stmf_default_lu_state = STMF_STATE_ONLINE;
29712682SSrivijitha.Dugganapalli@Sun.COM 	stmf_state.stmf_default_lport_state = STMF_STATE_ONLINE;
2987836SJohn.Forte@Sun.COM 	mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL);
2997836SJohn.Forte@Sun.COM 	cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL);
3007836SJohn.Forte@Sun.COM 	stmf_session_counter = (uint64_t)ddi_get_lbolt();
30111773STim.Szeto@Sun.COM 	avl_create(&stmf_state.stmf_irportlist,
30211773STim.Szeto@Sun.COM 	    stmf_irport_compare, sizeof (stmf_i_remote_port_t),
30311773STim.Szeto@Sun.COM 	    offsetof(stmf_i_remote_port_t, irport_ln));
30411773STim.Szeto@Sun.COM 	stmf_state.stmf_ilport_inst_space =
30511773STim.Szeto@Sun.COM 	    id_space_create("lport-instances", 0, MAX_ILPORT);
30611773STim.Szeto@Sun.COM 	stmf_state.stmf_irport_inst_space =
30711773STim.Szeto@Sun.COM 	    id_space_create("rport-instances", 0, MAX_IRPORT);
30811773STim.Szeto@Sun.COM 	avl_create(&stmf_state.stmf_itl_kstat_list,
30911773STim.Szeto@Sun.COM 	    stmf_itl_kstat_compare, sizeof (stmf_i_itl_kstat_t),
31011773STim.Szeto@Sun.COM 	    offsetof(stmf_i_itl_kstat_t, iitl_kstat_ln));
3117836SJohn.Forte@Sun.COM 	stmf_view_init();
3127836SJohn.Forte@Sun.COM 	stmf_svc_init();
3137836SJohn.Forte@Sun.COM 	stmf_dlun_init();
3147836SJohn.Forte@Sun.COM 	return (ret);
3157836SJohn.Forte@Sun.COM }
3167836SJohn.Forte@Sun.COM 
3177836SJohn.Forte@Sun.COM int
_fini(void)3187836SJohn.Forte@Sun.COM _fini(void)
3197836SJohn.Forte@Sun.COM {
3207836SJohn.Forte@Sun.COM 	int ret;
32111773STim.Szeto@Sun.COM 	stmf_i_remote_port_t	*irport;
32211773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	*ks_itl;
32311773STim.Szeto@Sun.COM 	void			*avl_dest_cookie = NULL;
3247836SJohn.Forte@Sun.COM 
3257836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_service_running)
3267836SJohn.Forte@Sun.COM 		return (EBUSY);
3277836SJohn.Forte@Sun.COM 	if ((!stmf_allow_modunload) &&
3287836SJohn.Forte@Sun.COM 	    (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) {
3297836SJohn.Forte@Sun.COM 		return (EBUSY);
3307836SJohn.Forte@Sun.COM 	}
3317836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_nlps || stmf_state.stmf_npps) {
3327836SJohn.Forte@Sun.COM 		return (EBUSY);
3337836SJohn.Forte@Sun.COM 	}
3347836SJohn.Forte@Sun.COM 	if (stmf_dlun_fini() != STMF_SUCCESS)
3357836SJohn.Forte@Sun.COM 		return (EBUSY);
3367836SJohn.Forte@Sun.COM 	if (stmf_worker_fini() != STMF_SUCCESS) {
3377836SJohn.Forte@Sun.COM 		stmf_dlun_init();
3387836SJohn.Forte@Sun.COM 		return (EBUSY);
3397836SJohn.Forte@Sun.COM 	}
3407836SJohn.Forte@Sun.COM 	if (stmf_svc_fini() != STMF_SUCCESS) {
3417836SJohn.Forte@Sun.COM 		stmf_dlun_init();
3427836SJohn.Forte@Sun.COM 		stmf_worker_init();
3437836SJohn.Forte@Sun.COM 		return (EBUSY);
3447836SJohn.Forte@Sun.COM 	}
3457836SJohn.Forte@Sun.COM 
3467836SJohn.Forte@Sun.COM 	ret = mod_remove(&modlinkage);
3477836SJohn.Forte@Sun.COM 	if (ret) {
3487836SJohn.Forte@Sun.COM 		stmf_svc_init();
3497836SJohn.Forte@Sun.COM 		stmf_dlun_init();
3507836SJohn.Forte@Sun.COM 		stmf_worker_init();
3517836SJohn.Forte@Sun.COM 		return (ret);
3527836SJohn.Forte@Sun.COM 	}
3537836SJohn.Forte@Sun.COM 
3547836SJohn.Forte@Sun.COM 	stmf_view_clear_config();
35511773STim.Szeto@Sun.COM 
35611773STim.Szeto@Sun.COM 	while ((irport = avl_destroy_nodes(&stmf_state.stmf_irportlist,
35711773STim.Szeto@Sun.COM 	    &avl_dest_cookie)) != NULL)
35811773STim.Szeto@Sun.COM 		stmf_irport_destroy(irport);
35911773STim.Szeto@Sun.COM 	avl_destroy(&stmf_state.stmf_irportlist);
36011773STim.Szeto@Sun.COM 	id_space_destroy(stmf_state.stmf_ilport_inst_space);
36111773STim.Szeto@Sun.COM 	id_space_destroy(stmf_state.stmf_irport_inst_space);
36211773STim.Szeto@Sun.COM 
36311773STim.Szeto@Sun.COM 	avl_dest_cookie = NULL;
36411773STim.Szeto@Sun.COM 	while ((ks_itl = avl_destroy_nodes(&stmf_state.stmf_itl_kstat_list,
36511773STim.Szeto@Sun.COM 	    &avl_dest_cookie)) != NULL) {
36611773STim.Szeto@Sun.COM 		stmf_teardown_itl_kstats(ks_itl);
36711773STim.Szeto@Sun.COM 		kmem_free(ks_itl, sizeof (ks_itl));
36811773STim.Szeto@Sun.COM 	}
36911773STim.Szeto@Sun.COM 	avl_destroy(&stmf_state.stmf_itl_kstat_list);
37011773STim.Szeto@Sun.COM 
3717836SJohn.Forte@Sun.COM 	kmem_free(stmf_trace_buf, stmf_trace_buf_size);
3727836SJohn.Forte@Sun.COM 	mutex_destroy(&trace_buf_lock);
3737836SJohn.Forte@Sun.COM 	mutex_destroy(&stmf_state.stmf_lock);
3747836SJohn.Forte@Sun.COM 	cv_destroy(&stmf_state.stmf_cv);
3757836SJohn.Forte@Sun.COM 	return (ret);
3767836SJohn.Forte@Sun.COM }
3777836SJohn.Forte@Sun.COM 
3787836SJohn.Forte@Sun.COM int
_info(struct modinfo * modinfop)3797836SJohn.Forte@Sun.COM _info(struct modinfo *modinfop)
3807836SJohn.Forte@Sun.COM {
3817836SJohn.Forte@Sun.COM 	return (mod_info(&modlinkage, modinfop));
3827836SJohn.Forte@Sun.COM }
3837836SJohn.Forte@Sun.COM 
3847836SJohn.Forte@Sun.COM /* ARGSUSED */
3857836SJohn.Forte@Sun.COM static int
stmf_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3867836SJohn.Forte@Sun.COM stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3877836SJohn.Forte@Sun.COM {
3887836SJohn.Forte@Sun.COM 	switch (cmd) {
3897836SJohn.Forte@Sun.COM 	case DDI_INFO_DEVT2DEVINFO:
3907836SJohn.Forte@Sun.COM 		*result = stmf_state.stmf_dip;
3917836SJohn.Forte@Sun.COM 		break;
3927836SJohn.Forte@Sun.COM 	case DDI_INFO_DEVT2INSTANCE:
3939585STim.Szeto@Sun.COM 		*result =
3949585STim.Szeto@Sun.COM 		    (void *)(uintptr_t)ddi_get_instance(stmf_state.stmf_dip);
3957836SJohn.Forte@Sun.COM 		break;
3967836SJohn.Forte@Sun.COM 	default:
3977836SJohn.Forte@Sun.COM 		return (DDI_FAILURE);
3987836SJohn.Forte@Sun.COM 	}
3997836SJohn.Forte@Sun.COM 
4007836SJohn.Forte@Sun.COM 	return (DDI_SUCCESS);
4017836SJohn.Forte@Sun.COM }
4027836SJohn.Forte@Sun.COM 
4037836SJohn.Forte@Sun.COM static int
stmf_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4047836SJohn.Forte@Sun.COM stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4057836SJohn.Forte@Sun.COM {
4067836SJohn.Forte@Sun.COM 	switch (cmd) {
4077836SJohn.Forte@Sun.COM 	case DDI_ATTACH:
4087836SJohn.Forte@Sun.COM 		stmf_state.stmf_dip = dip;
4097836SJohn.Forte@Sun.COM 
4107836SJohn.Forte@Sun.COM 		if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0,
4117836SJohn.Forte@Sun.COM 		    DDI_NT_STMF, 0) != DDI_SUCCESS) {
4127836SJohn.Forte@Sun.COM 			break;
4137836SJohn.Forte@Sun.COM 		}
4147836SJohn.Forte@Sun.COM 		ddi_report_dev(dip);
4157836SJohn.Forte@Sun.COM 		return (DDI_SUCCESS);
4167836SJohn.Forte@Sun.COM 	}
4177836SJohn.Forte@Sun.COM 
4187836SJohn.Forte@Sun.COM 	return (DDI_FAILURE);
4197836SJohn.Forte@Sun.COM }
4207836SJohn.Forte@Sun.COM 
4217836SJohn.Forte@Sun.COM static int
stmf_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4227836SJohn.Forte@Sun.COM stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4237836SJohn.Forte@Sun.COM {
4247836SJohn.Forte@Sun.COM 	switch (cmd) {
4257836SJohn.Forte@Sun.COM 	case DDI_DETACH:
4267836SJohn.Forte@Sun.COM 		ddi_remove_minor_node(dip, 0);
4277836SJohn.Forte@Sun.COM 		return (DDI_SUCCESS);
4287836SJohn.Forte@Sun.COM 	}
4297836SJohn.Forte@Sun.COM 
4307836SJohn.Forte@Sun.COM 	return (DDI_FAILURE);
4317836SJohn.Forte@Sun.COM }
4327836SJohn.Forte@Sun.COM 
4337836SJohn.Forte@Sun.COM /* ARGSUSED */
4347836SJohn.Forte@Sun.COM static int
stmf_open(dev_t * devp,int flag,int otype,cred_t * credp)4357836SJohn.Forte@Sun.COM stmf_open(dev_t *devp, int flag, int otype, cred_t *credp)
4367836SJohn.Forte@Sun.COM {
4377836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
4387836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_exclusive_open) {
4397836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
4407836SJohn.Forte@Sun.COM 		return (EBUSY);
4417836SJohn.Forte@Sun.COM 	}
4427836SJohn.Forte@Sun.COM 	if (flag & FEXCL) {
4437836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_opened) {
4447836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
4457836SJohn.Forte@Sun.COM 			return (EBUSY);
4467836SJohn.Forte@Sun.COM 		}
4477836SJohn.Forte@Sun.COM 		stmf_state.stmf_exclusive_open = 1;
4487836SJohn.Forte@Sun.COM 	}
4497836SJohn.Forte@Sun.COM 	stmf_state.stmf_opened = 1;
4507836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
4517836SJohn.Forte@Sun.COM 	return (0);
4527836SJohn.Forte@Sun.COM }
4537836SJohn.Forte@Sun.COM 
4547836SJohn.Forte@Sun.COM /* ARGSUSED */
4557836SJohn.Forte@Sun.COM static int
stmf_close(dev_t dev,int flag,int otype,cred_t * credp)4567836SJohn.Forte@Sun.COM stmf_close(dev_t dev, int flag, int otype, cred_t *credp)
4577836SJohn.Forte@Sun.COM {
4587836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
4597836SJohn.Forte@Sun.COM 	stmf_state.stmf_opened = 0;
4607836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_exclusive_open &&
4617836SJohn.Forte@Sun.COM 	    (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) {
4627836SJohn.Forte@Sun.COM 		stmf_state.stmf_config_state = STMF_CONFIG_NONE;
4637836SJohn.Forte@Sun.COM 		stmf_delete_all_ppds();
4647836SJohn.Forte@Sun.COM 		stmf_view_clear_config();
4657836SJohn.Forte@Sun.COM 		stmf_view_init();
4667836SJohn.Forte@Sun.COM 	}
4677836SJohn.Forte@Sun.COM 	stmf_state.stmf_exclusive_open = 0;
4687836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
4697836SJohn.Forte@Sun.COM 	return (0);
4707836SJohn.Forte@Sun.COM }
4717836SJohn.Forte@Sun.COM 
4727836SJohn.Forte@Sun.COM int
stmf_copyin_iocdata(intptr_t data,int mode,stmf_iocdata_t ** iocd,void ** ibuf,void ** obuf)4737836SJohn.Forte@Sun.COM stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd,
4747836SJohn.Forte@Sun.COM 						void **ibuf, void **obuf)
4757836SJohn.Forte@Sun.COM {
4767836SJohn.Forte@Sun.COM 	int ret;
4777836SJohn.Forte@Sun.COM 
4787836SJohn.Forte@Sun.COM 	*ibuf = NULL;
4797836SJohn.Forte@Sun.COM 	*obuf = NULL;
4807836SJohn.Forte@Sun.COM 	*iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP);
4817836SJohn.Forte@Sun.COM 
4827836SJohn.Forte@Sun.COM 	ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode);
4837836SJohn.Forte@Sun.COM 	if (ret)
4847836SJohn.Forte@Sun.COM 		return (EFAULT);
4857836SJohn.Forte@Sun.COM 	if ((*iocd)->stmf_version != STMF_VERSION_1) {
4867836SJohn.Forte@Sun.COM 		ret = EINVAL;
4877836SJohn.Forte@Sun.COM 		goto copyin_iocdata_done;
4887836SJohn.Forte@Sun.COM 	}
4897836SJohn.Forte@Sun.COM 	if ((*iocd)->stmf_ibuf_size) {
4907836SJohn.Forte@Sun.COM 		*ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP);
4917836SJohn.Forte@Sun.COM 		ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf),
4927836SJohn.Forte@Sun.COM 		    *ibuf, (*iocd)->stmf_ibuf_size, mode);
4937836SJohn.Forte@Sun.COM 	}
4947836SJohn.Forte@Sun.COM 	if ((*iocd)->stmf_obuf_size)
4957836SJohn.Forte@Sun.COM 		*obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP);
4967836SJohn.Forte@Sun.COM 
4977836SJohn.Forte@Sun.COM 	if (ret == 0)
4987836SJohn.Forte@Sun.COM 		return (0);
4997836SJohn.Forte@Sun.COM 	ret = EFAULT;
5007836SJohn.Forte@Sun.COM copyin_iocdata_done:;
5017836SJohn.Forte@Sun.COM 	if (*obuf) {
5027836SJohn.Forte@Sun.COM 		kmem_free(*obuf, (*iocd)->stmf_obuf_size);
5037836SJohn.Forte@Sun.COM 		*obuf = NULL;
5047836SJohn.Forte@Sun.COM 	}
5057836SJohn.Forte@Sun.COM 	if (*ibuf) {
5067836SJohn.Forte@Sun.COM 		kmem_free(*ibuf, (*iocd)->stmf_ibuf_size);
5077836SJohn.Forte@Sun.COM 		*ibuf = NULL;
5087836SJohn.Forte@Sun.COM 	}
5097836SJohn.Forte@Sun.COM 	kmem_free(*iocd, sizeof (stmf_iocdata_t));
5107836SJohn.Forte@Sun.COM 	return (ret);
5117836SJohn.Forte@Sun.COM }
5127836SJohn.Forte@Sun.COM 
5137836SJohn.Forte@Sun.COM int
stmf_copyout_iocdata(intptr_t data,int mode,stmf_iocdata_t * iocd,void * obuf)5147836SJohn.Forte@Sun.COM stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf)
5157836SJohn.Forte@Sun.COM {
5167836SJohn.Forte@Sun.COM 	int ret;
5177836SJohn.Forte@Sun.COM 
5187836SJohn.Forte@Sun.COM 	if (iocd->stmf_obuf_size) {
5197836SJohn.Forte@Sun.COM 		ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf,
5207836SJohn.Forte@Sun.COM 		    iocd->stmf_obuf_size, mode);
5217836SJohn.Forte@Sun.COM 		if (ret)
5227836SJohn.Forte@Sun.COM 			return (EFAULT);
5237836SJohn.Forte@Sun.COM 	}
5247836SJohn.Forte@Sun.COM 	ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode);
5257836SJohn.Forte@Sun.COM 	if (ret)
5267836SJohn.Forte@Sun.COM 		return (EFAULT);
5277836SJohn.Forte@Sun.COM 	return (0);
5287836SJohn.Forte@Sun.COM }
5297836SJohn.Forte@Sun.COM 
5307836SJohn.Forte@Sun.COM /* ARGSUSED */
5317836SJohn.Forte@Sun.COM static int
stmf_ioctl(dev_t dev,int cmd,intptr_t data,int mode,cred_t * credp,int * rval)5327836SJohn.Forte@Sun.COM stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
5337836SJohn.Forte@Sun.COM 	cred_t *credp, int *rval)
5347836SJohn.Forte@Sun.COM {
5357836SJohn.Forte@Sun.COM 	stmf_iocdata_t *iocd;
5367836SJohn.Forte@Sun.COM 	void *ibuf = NULL, *obuf = NULL;
5377836SJohn.Forte@Sun.COM 	slist_lu_t *luid_list;
5387836SJohn.Forte@Sun.COM 	slist_target_port_t *lportid_list;
5397836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
5407836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
5417836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
5427836SJohn.Forte@Sun.COM 	slist_scsi_session_t *iss_list;
5437836SJohn.Forte@Sun.COM 	sioc_lu_props_t *lup;
5447836SJohn.Forte@Sun.COM 	sioc_target_port_props_t *lportp;
5459585STim.Szeto@Sun.COM 	stmf_ppioctl_data_t *ppi, *ppi_out = NULL;
5469585STim.Szeto@Sun.COM 	uint64_t *ppi_token = NULL;
5479585STim.Szeto@Sun.COM 	uint8_t *p_id, *id;
5487836SJohn.Forte@Sun.COM 	stmf_state_desc_t *std;
5497836SJohn.Forte@Sun.COM 	stmf_status_t ctl_ret;
5507836SJohn.Forte@Sun.COM 	stmf_state_change_info_t ssi;
5517836SJohn.Forte@Sun.COM 	int ret = 0;
5527836SJohn.Forte@Sun.COM 	uint32_t n;
5537836SJohn.Forte@Sun.COM 	int i;
5547836SJohn.Forte@Sun.COM 	stmf_group_op_data_t *grp_entry;
5557836SJohn.Forte@Sun.COM 	stmf_group_name_t *grpname;
5567836SJohn.Forte@Sun.COM 	stmf_view_op_entry_t *ve;
5577836SJohn.Forte@Sun.COM 	stmf_id_type_t idtype;
5587836SJohn.Forte@Sun.COM 	stmf_id_data_t *id_entry;
5597836SJohn.Forte@Sun.COM 	stmf_id_list_t	*id_list;
5607836SJohn.Forte@Sun.COM 	stmf_view_entry_t *view_entry;
56112682SSrivijitha.Dugganapalli@Sun.COM 	stmf_set_props_t *stmf_set_props;
5627836SJohn.Forte@Sun.COM 	uint32_t	veid;
5637836SJohn.Forte@Sun.COM 	if ((cmd & 0xff000000) != STMF_IOCTL) {
5647836SJohn.Forte@Sun.COM 		return (ENOTTY);
5657836SJohn.Forte@Sun.COM 	}
5667836SJohn.Forte@Sun.COM 
5677836SJohn.Forte@Sun.COM 	if (drv_priv(credp) != 0) {
5687836SJohn.Forte@Sun.COM 		return (EPERM);
5697836SJohn.Forte@Sun.COM 	}
5707836SJohn.Forte@Sun.COM 
5717836SJohn.Forte@Sun.COM 	ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf);
5727836SJohn.Forte@Sun.COM 	if (ret)
5737836SJohn.Forte@Sun.COM 		return (ret);
5747836SJohn.Forte@Sun.COM 	iocd->stmf_error = 0;
5757836SJohn.Forte@Sun.COM 
5767836SJohn.Forte@Sun.COM 	switch (cmd) {
5777836SJohn.Forte@Sun.COM 	case STMF_IOCTL_LU_LIST:
5789585STim.Szeto@Sun.COM 		/* retrieves both registered/unregistered */
5799585STim.Szeto@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
5809585STim.Szeto@Sun.COM 		id_list = &stmf_state.stmf_luid_list;
5819585STim.Szeto@Sun.COM 		n = min(id_list->id_count,
5829585STim.Szeto@Sun.COM 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
5839585STim.Szeto@Sun.COM 		iocd->stmf_obuf_max_nentries = id_list->id_count;
5849585STim.Szeto@Sun.COM 		luid_list = (slist_lu_t *)obuf;
5859585STim.Szeto@Sun.COM 		id_entry = id_list->idl_head;
5869585STim.Szeto@Sun.COM 		for (i = 0; i < n; i++) {
5879585STim.Szeto@Sun.COM 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
5889585STim.Szeto@Sun.COM 			id_entry = id_entry->id_next;
5899585STim.Szeto@Sun.COM 		}
5909585STim.Szeto@Sun.COM 
5919585STim.Szeto@Sun.COM 		n = iocd->stmf_obuf_size/sizeof (slist_lu_t);
5929585STim.Szeto@Sun.COM 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
5939585STim.Szeto@Sun.COM 			id = (uint8_t *)ilu->ilu_lu->lu_id;
5949585STim.Szeto@Sun.COM 			if (stmf_lookup_id(id_list, 16, id + 4) == NULL) {
5959585STim.Szeto@Sun.COM 				iocd->stmf_obuf_max_nentries++;
5969585STim.Szeto@Sun.COM 				if (i < n) {
5979585STim.Szeto@Sun.COM 					bcopy(id + 4, luid_list[i].lu_guid,
5989585STim.Szeto@Sun.COM 					    sizeof (slist_lu_t));
5999585STim.Szeto@Sun.COM 					i++;
6009585STim.Szeto@Sun.COM 				}
6019585STim.Szeto@Sun.COM 			}
6029585STim.Szeto@Sun.COM 		}
6039585STim.Szeto@Sun.COM 		iocd->stmf_obuf_nentries = i;
6049585STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
6059585STim.Szeto@Sun.COM 		break;
6069585STim.Szeto@Sun.COM 
6079585STim.Szeto@Sun.COM 	case STMF_IOCTL_REG_LU_LIST:
6087836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
6097836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus;
6107836SJohn.Forte@Sun.COM 		n = min(stmf_state.stmf_nlus,
6118662SJordan.Vaughan@Sun.com 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
6127836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_nentries = n;
6137836SJohn.Forte@Sun.COM 		ilu = stmf_state.stmf_ilulist;
6147836SJohn.Forte@Sun.COM 		luid_list = (slist_lu_t *)obuf;
6157836SJohn.Forte@Sun.COM 		for (i = 0; i < n; i++) {
6167836SJohn.Forte@Sun.COM 			uint8_t *id;
6177836SJohn.Forte@Sun.COM 			id = (uint8_t *)ilu->ilu_lu->lu_id;
6187836SJohn.Forte@Sun.COM 			bcopy(id + 4, luid_list[i].lu_guid, 16);
6197836SJohn.Forte@Sun.COM 			ilu = ilu->ilu_next;
6207836SJohn.Forte@Sun.COM 		}
6217836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
6227836SJohn.Forte@Sun.COM 		break;
6237836SJohn.Forte@Sun.COM 
6249585STim.Szeto@Sun.COM 	case STMF_IOCTL_VE_LU_LIST:
6259585STim.Szeto@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
6269585STim.Szeto@Sun.COM 		id_list = &stmf_state.stmf_luid_list;
6279585STim.Szeto@Sun.COM 		n = min(id_list->id_count,
6289585STim.Szeto@Sun.COM 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
6299585STim.Szeto@Sun.COM 		iocd->stmf_obuf_max_nentries = id_list->id_count;
6309585STim.Szeto@Sun.COM 		iocd->stmf_obuf_nentries = n;
6319585STim.Szeto@Sun.COM 		luid_list = (slist_lu_t *)obuf;
6329585STim.Szeto@Sun.COM 		id_entry = id_list->idl_head;
6339585STim.Szeto@Sun.COM 		for (i = 0; i < n; i++) {
6349585STim.Szeto@Sun.COM 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
6359585STim.Szeto@Sun.COM 			id_entry = id_entry->id_next;
6369585STim.Szeto@Sun.COM 		}
6379585STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
6389585STim.Szeto@Sun.COM 		break;
6399585STim.Szeto@Sun.COM 
6407836SJohn.Forte@Sun.COM 	case STMF_IOCTL_TARGET_PORT_LIST:
6417836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
6427836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports;
6437836SJohn.Forte@Sun.COM 		n = min(stmf_state.stmf_nlports,
6448662SJordan.Vaughan@Sun.com 		    (iocd->stmf_obuf_size)/sizeof (slist_target_port_t));
6457836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_nentries = n;
6467836SJohn.Forte@Sun.COM 		ilport = stmf_state.stmf_ilportlist;
6477836SJohn.Forte@Sun.COM 		lportid_list = (slist_target_port_t *)obuf;
6487836SJohn.Forte@Sun.COM 		for (i = 0; i < n; i++) {
6497836SJohn.Forte@Sun.COM 			uint8_t *id;
6507836SJohn.Forte@Sun.COM 			id = (uint8_t *)ilport->ilport_lport->lport_id;
6517836SJohn.Forte@Sun.COM 			bcopy(id, lportid_list[i].target, id[3] + 4);
6527836SJohn.Forte@Sun.COM 			ilport = ilport->ilport_next;
6537836SJohn.Forte@Sun.COM 		}
6547836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
6557836SJohn.Forte@Sun.COM 		break;
6567836SJohn.Forte@Sun.COM 
6577836SJohn.Forte@Sun.COM 	case STMF_IOCTL_SESSION_LIST:
6587836SJohn.Forte@Sun.COM 		p_id = (uint8_t *)ibuf;
6597836SJohn.Forte@Sun.COM 		if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) ||
6607836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < (p_id[3] + 4))) {
6617836SJohn.Forte@Sun.COM 			ret = EINVAL;
6627836SJohn.Forte@Sun.COM 			break;
6637836SJohn.Forte@Sun.COM 		}
6647836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
6657836SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport; ilport =
6668662SJordan.Vaughan@Sun.com 		    ilport->ilport_next) {
6677836SJohn.Forte@Sun.COM 			uint8_t *id;
6687836SJohn.Forte@Sun.COM 			id = (uint8_t *)ilport->ilport_lport->lport_id;
6697836SJohn.Forte@Sun.COM 			if ((p_id[3] == id[3]) &&
6707836SJohn.Forte@Sun.COM 			    (bcmp(p_id + 4, id + 4, id[3]) == 0)) {
6717836SJohn.Forte@Sun.COM 				break;
6727836SJohn.Forte@Sun.COM 			}
6737836SJohn.Forte@Sun.COM 		}
6747836SJohn.Forte@Sun.COM 		if (ilport == NULL) {
6757836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
6767836SJohn.Forte@Sun.COM 			ret = ENOENT;
6777836SJohn.Forte@Sun.COM 			break;
6787836SJohn.Forte@Sun.COM 		}
6797836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions;
6807836SJohn.Forte@Sun.COM 		n = min(ilport->ilport_nsessions,
6817836SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t));
6827836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_nentries = n;
6837836SJohn.Forte@Sun.COM 		iss = ilport->ilport_ss_list;
6847836SJohn.Forte@Sun.COM 		iss_list = (slist_scsi_session_t *)obuf;
6857836SJohn.Forte@Sun.COM 		for (i = 0; i < n; i++) {
6867836SJohn.Forte@Sun.COM 			uint8_t *id;
6877836SJohn.Forte@Sun.COM 			id = (uint8_t *)iss->iss_ss->ss_rport_id;
6887836SJohn.Forte@Sun.COM 			bcopy(id, iss_list[i].initiator, id[3] + 4);
6897836SJohn.Forte@Sun.COM 			iss_list[i].creation_time = (uint32_t)
6907836SJohn.Forte@Sun.COM 			    iss->iss_creation_time;
6917836SJohn.Forte@Sun.COM 			if (iss->iss_ss->ss_rport_alias) {
6927836SJohn.Forte@Sun.COM 				(void) strncpy(iss_list[i].alias,
6937836SJohn.Forte@Sun.COM 				    iss->iss_ss->ss_rport_alias, 255);
6947836SJohn.Forte@Sun.COM 				iss_list[i].alias[255] = 0;
6957836SJohn.Forte@Sun.COM 			} else {
6967836SJohn.Forte@Sun.COM 				iss_list[i].alias[0] = 0;
6977836SJohn.Forte@Sun.COM 			}
6987836SJohn.Forte@Sun.COM 			iss = iss->iss_next;
6997836SJohn.Forte@Sun.COM 		}
7007836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
7017836SJohn.Forte@Sun.COM 		break;
7027836SJohn.Forte@Sun.COM 
7037836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_LU_PROPERTIES:
7047836SJohn.Forte@Sun.COM 		p_id = (uint8_t *)ibuf;
7057836SJohn.Forte@Sun.COM 		if ((iocd->stmf_ibuf_size < 16) ||
7067836SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) ||
7077836SJohn.Forte@Sun.COM 		    (p_id[0] == 0)) {
7087836SJohn.Forte@Sun.COM 			ret = EINVAL;
7097836SJohn.Forte@Sun.COM 			break;
7107836SJohn.Forte@Sun.COM 		}
7117836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
7127836SJohn.Forte@Sun.COM 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
7137836SJohn.Forte@Sun.COM 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
7147836SJohn.Forte@Sun.COM 				break;
7157836SJohn.Forte@Sun.COM 		}
7167836SJohn.Forte@Sun.COM 		if (ilu == NULL) {
7177836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
7187836SJohn.Forte@Sun.COM 			ret = ENOENT;
7197836SJohn.Forte@Sun.COM 			break;
7207836SJohn.Forte@Sun.COM 		}
7217836SJohn.Forte@Sun.COM 		lup = (sioc_lu_props_t *)obuf;
7227836SJohn.Forte@Sun.COM 		bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16);
7237836SJohn.Forte@Sun.COM 		lup->lu_state = ilu->ilu_state & 0x0f;
7247836SJohn.Forte@Sun.COM 		lup->lu_present = 1; /* XXX */
7257836SJohn.Forte@Sun.COM 		(void) strncpy(lup->lu_provider_name,
7267836SJohn.Forte@Sun.COM 		    ilu->ilu_lu->lu_lp->lp_name, 255);
7277836SJohn.Forte@Sun.COM 		lup->lu_provider_name[254] = 0;
7287836SJohn.Forte@Sun.COM 		if (ilu->ilu_lu->lu_alias) {
7297836SJohn.Forte@Sun.COM 			(void) strncpy(lup->lu_alias,
7307836SJohn.Forte@Sun.COM 			    ilu->ilu_lu->lu_alias, 255);
7317836SJohn.Forte@Sun.COM 			lup->lu_alias[255] = 0;
7327836SJohn.Forte@Sun.COM 		} else {
7337836SJohn.Forte@Sun.COM 			lup->lu_alias[0] = 0;
7347836SJohn.Forte@Sun.COM 		}
7357836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
7367836SJohn.Forte@Sun.COM 		break;
7377836SJohn.Forte@Sun.COM 
7387836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES:
7397836SJohn.Forte@Sun.COM 		p_id = (uint8_t *)ibuf;
7407836SJohn.Forte@Sun.COM 		if ((p_id == NULL) ||
7417836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < (p_id[3] + 4)) ||
7427836SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size <
7437836SJohn.Forte@Sun.COM 		    sizeof (sioc_target_port_props_t))) {
7447836SJohn.Forte@Sun.COM 			ret = EINVAL;
7457836SJohn.Forte@Sun.COM 			break;
7467836SJohn.Forte@Sun.COM 		}
7477836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
7487836SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport;
7497836SJohn.Forte@Sun.COM 		    ilport = ilport->ilport_next) {
7507836SJohn.Forte@Sun.COM 			uint8_t *id;
7517836SJohn.Forte@Sun.COM 			id = (uint8_t *)ilport->ilport_lport->lport_id;
7527836SJohn.Forte@Sun.COM 			if ((p_id[3] == id[3]) &&
7537836SJohn.Forte@Sun.COM 			    (bcmp(p_id+4, id+4, id[3]) == 0))
7547836SJohn.Forte@Sun.COM 				break;
7557836SJohn.Forte@Sun.COM 		}
7567836SJohn.Forte@Sun.COM 		if (ilport == NULL) {
7577836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
7587836SJohn.Forte@Sun.COM 			ret = ENOENT;
7597836SJohn.Forte@Sun.COM 			break;
7607836SJohn.Forte@Sun.COM 		}
7617836SJohn.Forte@Sun.COM 		lportp = (sioc_target_port_props_t *)obuf;
7627836SJohn.Forte@Sun.COM 		bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id,
7637836SJohn.Forte@Sun.COM 		    ilport->ilport_lport->lport_id->ident_length + 4);
7647836SJohn.Forte@Sun.COM 		lportp->tgt_state = ilport->ilport_state & 0x0f;
7657836SJohn.Forte@Sun.COM 		lportp->tgt_present = 1; /* XXX */
7667836SJohn.Forte@Sun.COM 		(void) strncpy(lportp->tgt_provider_name,
7677836SJohn.Forte@Sun.COM 		    ilport->ilport_lport->lport_pp->pp_name, 255);
7687836SJohn.Forte@Sun.COM 		lportp->tgt_provider_name[254] = 0;
7697836SJohn.Forte@Sun.COM 		if (ilport->ilport_lport->lport_alias) {
7707836SJohn.Forte@Sun.COM 			(void) strncpy(lportp->tgt_alias,
7717836SJohn.Forte@Sun.COM 			    ilport->ilport_lport->lport_alias, 255);
7727836SJohn.Forte@Sun.COM 			lportp->tgt_alias[255] = 0;
7737836SJohn.Forte@Sun.COM 		} else {
7747836SJohn.Forte@Sun.COM 			lportp->tgt_alias[0] = 0;
7757836SJohn.Forte@Sun.COM 		}
7767836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
7777836SJohn.Forte@Sun.COM 		break;
7787836SJohn.Forte@Sun.COM 
7797836SJohn.Forte@Sun.COM 	case STMF_IOCTL_SET_STMF_STATE:
7807836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
7817836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
7827836SJohn.Forte@Sun.COM 			ret = EINVAL;
7837836SJohn.Forte@Sun.COM 			break;
7847836SJohn.Forte@Sun.COM 		}
7857836SJohn.Forte@Sun.COM 		ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf);
7867836SJohn.Forte@Sun.COM 		break;
7877836SJohn.Forte@Sun.COM 
7887836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_STMF_STATE:
7897836SJohn.Forte@Sun.COM 		if ((obuf == NULL) ||
7907836SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) {
7917836SJohn.Forte@Sun.COM 			ret = EINVAL;
7927836SJohn.Forte@Sun.COM 			break;
7937836SJohn.Forte@Sun.COM 		}
7947836SJohn.Forte@Sun.COM 		ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf);
7957836SJohn.Forte@Sun.COM 		break;
7967836SJohn.Forte@Sun.COM 
79710725SJohn.Forte@Sun.COM 	case STMF_IOCTL_SET_ALUA_STATE:
79810725SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
79910725SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_alua_state_desc_t))) {
80010725SJohn.Forte@Sun.COM 			ret = EINVAL;
80110725SJohn.Forte@Sun.COM 			break;
80210725SJohn.Forte@Sun.COM 		}
80310725SJohn.Forte@Sun.COM 		ret = stmf_set_alua_state((stmf_alua_state_desc_t *)ibuf);
80410725SJohn.Forte@Sun.COM 		break;
80510725SJohn.Forte@Sun.COM 
80610725SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_ALUA_STATE:
80710725SJohn.Forte@Sun.COM 		if ((obuf == NULL) ||
80810725SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (stmf_alua_state_desc_t))) {
80910725SJohn.Forte@Sun.COM 			ret = EINVAL;
81010725SJohn.Forte@Sun.COM 			break;
81110725SJohn.Forte@Sun.COM 		}
81210725SJohn.Forte@Sun.COM 		stmf_get_alua_state((stmf_alua_state_desc_t *)obuf);
81310725SJohn.Forte@Sun.COM 		break;
81410725SJohn.Forte@Sun.COM 
8157836SJohn.Forte@Sun.COM 	case STMF_IOCTL_SET_LU_STATE:
8167836SJohn.Forte@Sun.COM 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
8177836SJohn.Forte@Sun.COM 		ssi.st_additional_info = NULL;
8187836SJohn.Forte@Sun.COM 		std = (stmf_state_desc_t *)ibuf;
8197836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
8207836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
8217836SJohn.Forte@Sun.COM 			ret = EINVAL;
8227836SJohn.Forte@Sun.COM 			break;
8237836SJohn.Forte@Sun.COM 		}
8247836SJohn.Forte@Sun.COM 		p_id = std->ident;
8257836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
8267836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_inventory_locked) {
8277836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
8287836SJohn.Forte@Sun.COM 			ret = EBUSY;
8297836SJohn.Forte@Sun.COM 			break;
8307836SJohn.Forte@Sun.COM 		}
8317836SJohn.Forte@Sun.COM 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
8327836SJohn.Forte@Sun.COM 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
8337836SJohn.Forte@Sun.COM 				break;
8347836SJohn.Forte@Sun.COM 		}
8357836SJohn.Forte@Sun.COM 		if (ilu == NULL) {
8367836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
8377836SJohn.Forte@Sun.COM 			ret = ENOENT;
8387836SJohn.Forte@Sun.COM 			break;
8397836SJohn.Forte@Sun.COM 		}
8407836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 1;
8417836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
8427836SJohn.Forte@Sun.COM 		cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE :
8437836SJohn.Forte@Sun.COM 		    STMF_CMD_LU_OFFLINE;
8447836SJohn.Forte@Sun.COM 		ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi);
8457836SJohn.Forte@Sun.COM 		if (ctl_ret == STMF_ALREADY)
8467836SJohn.Forte@Sun.COM 			ret = 0;
84711698SNattuvetty.Bhavyan@Sun.COM 		else if (ctl_ret == STMF_BUSY)
84811698SNattuvetty.Bhavyan@Sun.COM 			ret = EBUSY;
8497836SJohn.Forte@Sun.COM 		else if (ctl_ret != STMF_SUCCESS)
8507836SJohn.Forte@Sun.COM 			ret = EIO;
8517836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
8527836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 0;
8537836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
8547836SJohn.Forte@Sun.COM 		break;
8557836SJohn.Forte@Sun.COM 
85612682SSrivijitha.Dugganapalli@Sun.COM 	case STMF_IOCTL_SET_STMF_PROPS:
85712682SSrivijitha.Dugganapalli@Sun.COM 		if ((ibuf == NULL) ||
85812682SSrivijitha.Dugganapalli@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_set_props_t))) {
85912682SSrivijitha.Dugganapalli@Sun.COM 			ret = EINVAL;
86012682SSrivijitha.Dugganapalli@Sun.COM 			break;
86112682SSrivijitha.Dugganapalli@Sun.COM 		}
86212682SSrivijitha.Dugganapalli@Sun.COM 		stmf_set_props = (stmf_set_props_t *)ibuf;
86312682SSrivijitha.Dugganapalli@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
86412682SSrivijitha.Dugganapalli@Sun.COM 		if ((stmf_set_props->default_lu_state_value ==
86512682SSrivijitha.Dugganapalli@Sun.COM 		    STMF_STATE_OFFLINE) ||
86612682SSrivijitha.Dugganapalli@Sun.COM 		    (stmf_set_props->default_lu_state_value ==
86712682SSrivijitha.Dugganapalli@Sun.COM 		    STMF_STATE_ONLINE)) {
86812682SSrivijitha.Dugganapalli@Sun.COM 			stmf_state.stmf_default_lu_state =
86912682SSrivijitha.Dugganapalli@Sun.COM 			    stmf_set_props->default_lu_state_value;
87012682SSrivijitha.Dugganapalli@Sun.COM 		}
87112682SSrivijitha.Dugganapalli@Sun.COM 		if ((stmf_set_props->default_target_state_value ==
87212682SSrivijitha.Dugganapalli@Sun.COM 		    STMF_STATE_OFFLINE) ||
87312682SSrivijitha.Dugganapalli@Sun.COM 		    (stmf_set_props->default_target_state_value ==
87412682SSrivijitha.Dugganapalli@Sun.COM 		    STMF_STATE_ONLINE)) {
87512682SSrivijitha.Dugganapalli@Sun.COM 			stmf_state.stmf_default_lport_state =
87612682SSrivijitha.Dugganapalli@Sun.COM 			    stmf_set_props->default_target_state_value;
87712682SSrivijitha.Dugganapalli@Sun.COM 		}
87812682SSrivijitha.Dugganapalli@Sun.COM 
87912682SSrivijitha.Dugganapalli@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
88012682SSrivijitha.Dugganapalli@Sun.COM 		break;
88112682SSrivijitha.Dugganapalli@Sun.COM 
8827836SJohn.Forte@Sun.COM 	case STMF_IOCTL_SET_TARGET_PORT_STATE:
8837836SJohn.Forte@Sun.COM 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
8847836SJohn.Forte@Sun.COM 		ssi.st_additional_info = NULL;
8857836SJohn.Forte@Sun.COM 		std = (stmf_state_desc_t *)ibuf;
8867836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
8877836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
8887836SJohn.Forte@Sun.COM 			ret = EINVAL;
8897836SJohn.Forte@Sun.COM 			break;
8907836SJohn.Forte@Sun.COM 		}
8917836SJohn.Forte@Sun.COM 		p_id = std->ident;
8927836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
8937836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_inventory_locked) {
8947836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
8957836SJohn.Forte@Sun.COM 			ret = EBUSY;
8967836SJohn.Forte@Sun.COM 			break;
8977836SJohn.Forte@Sun.COM 		}
8987836SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport;
8997836SJohn.Forte@Sun.COM 		    ilport = ilport->ilport_next) {
9007836SJohn.Forte@Sun.COM 			uint8_t *id;
9017836SJohn.Forte@Sun.COM 			id = (uint8_t *)ilport->ilport_lport->lport_id;
9027836SJohn.Forte@Sun.COM 			if ((id[3] == p_id[3]) &&
9037836SJohn.Forte@Sun.COM 			    (bcmp(id+4, p_id+4, id[3]) == 0)) {
9047836SJohn.Forte@Sun.COM 				break;
9057836SJohn.Forte@Sun.COM 			}
9067836SJohn.Forte@Sun.COM 		}
9077836SJohn.Forte@Sun.COM 		if (ilport == NULL) {
9087836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
9097836SJohn.Forte@Sun.COM 			ret = ENOENT;
9107836SJohn.Forte@Sun.COM 			break;
9117836SJohn.Forte@Sun.COM 		}
9127836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 1;
9137836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
9147836SJohn.Forte@Sun.COM 		cmd = (std->state == STMF_STATE_ONLINE) ?
9157836SJohn.Forte@Sun.COM 		    STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE;
9167836SJohn.Forte@Sun.COM 		ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi);
9177836SJohn.Forte@Sun.COM 		if (ctl_ret == STMF_ALREADY)
9187836SJohn.Forte@Sun.COM 			ret = 0;
91911698SNattuvetty.Bhavyan@Sun.COM 		else if (ctl_ret == STMF_BUSY)
92011698SNattuvetty.Bhavyan@Sun.COM 			ret = EBUSY;
9217836SJohn.Forte@Sun.COM 		else if (ctl_ret != STMF_SUCCESS)
9227836SJohn.Forte@Sun.COM 			ret = EIO;
9237836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
9247836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 0;
9257836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
9267836SJohn.Forte@Sun.COM 		break;
9277836SJohn.Forte@Sun.COM 
9287836SJohn.Forte@Sun.COM 	case STMF_IOCTL_ADD_HG_ENTRY:
9297836SJohn.Forte@Sun.COM 		idtype = STMF_ID_TYPE_HOST;
9307836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
9317836SJohn.Forte@Sun.COM 	case STMF_IOCTL_ADD_TG_ENTRY:
9327836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
9337836SJohn.Forte@Sun.COM 			ret = EACCES;
9347836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
9357836SJohn.Forte@Sun.COM 			break;
9367836SJohn.Forte@Sun.COM 		}
9377836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_ADD_TG_ENTRY) {
9387836SJohn.Forte@Sun.COM 			idtype = STMF_ID_TYPE_TARGET;
9397836SJohn.Forte@Sun.COM 		}
9407836SJohn.Forte@Sun.COM 		grp_entry = (stmf_group_op_data_t *)ibuf;
9417836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
9427836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
9437836SJohn.Forte@Sun.COM 			ret = EINVAL;
9447836SJohn.Forte@Sun.COM 			break;
9457836SJohn.Forte@Sun.COM 		}
9467836SJohn.Forte@Sun.COM 		if (grp_entry->group.name[0] == '*') {
9477836SJohn.Forte@Sun.COM 			ret = EINVAL;
9487836SJohn.Forte@Sun.COM 			break; /* not allowed */
9497836SJohn.Forte@Sun.COM 		}
9507836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
9517836SJohn.Forte@Sun.COM 		ret = stmf_add_group_member(grp_entry->group.name,
9527836SJohn.Forte@Sun.COM 		    grp_entry->group.name_size,
9537836SJohn.Forte@Sun.COM 		    grp_entry->ident + 4,
9547836SJohn.Forte@Sun.COM 		    grp_entry->ident[3],
9557836SJohn.Forte@Sun.COM 		    idtype,
9567836SJohn.Forte@Sun.COM 		    &iocd->stmf_error);
9577836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
9587836SJohn.Forte@Sun.COM 		break;
9597836SJohn.Forte@Sun.COM 	case STMF_IOCTL_REMOVE_HG_ENTRY:
9607836SJohn.Forte@Sun.COM 		idtype = STMF_ID_TYPE_HOST;
9617836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
9627836SJohn.Forte@Sun.COM 	case STMF_IOCTL_REMOVE_TG_ENTRY:
9637836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
9647836SJohn.Forte@Sun.COM 			ret = EACCES;
9657836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
9667836SJohn.Forte@Sun.COM 			break;
9677836SJohn.Forte@Sun.COM 		}
9687836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) {
9697836SJohn.Forte@Sun.COM 			idtype = STMF_ID_TYPE_TARGET;
9707836SJohn.Forte@Sun.COM 		}
9717836SJohn.Forte@Sun.COM 		grp_entry = (stmf_group_op_data_t *)ibuf;
9727836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
9737836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
9747836SJohn.Forte@Sun.COM 			ret = EINVAL;
9757836SJohn.Forte@Sun.COM 			break;
9767836SJohn.Forte@Sun.COM 		}
9777836SJohn.Forte@Sun.COM 		if (grp_entry->group.name[0] == '*') {
9787836SJohn.Forte@Sun.COM 			ret = EINVAL;
9797836SJohn.Forte@Sun.COM 			break; /* not allowed */
9807836SJohn.Forte@Sun.COM 		}
9817836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
9827836SJohn.Forte@Sun.COM 		ret = stmf_remove_group_member(grp_entry->group.name,
9837836SJohn.Forte@Sun.COM 		    grp_entry->group.name_size,
9847836SJohn.Forte@Sun.COM 		    grp_entry->ident + 4,
9857836SJohn.Forte@Sun.COM 		    grp_entry->ident[3],
9867836SJohn.Forte@Sun.COM 		    idtype,
9877836SJohn.Forte@Sun.COM 		    &iocd->stmf_error);
9887836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
9897836SJohn.Forte@Sun.COM 		break;
9907836SJohn.Forte@Sun.COM 	case STMF_IOCTL_CREATE_HOST_GROUP:
9917836SJohn.Forte@Sun.COM 		idtype = STMF_ID_TYPE_HOST_GROUP;
9927836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
9937836SJohn.Forte@Sun.COM 	case STMF_IOCTL_CREATE_TARGET_GROUP:
9947836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
9957836SJohn.Forte@Sun.COM 			ret = EACCES;
9967836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
9977836SJohn.Forte@Sun.COM 			break;
9987836SJohn.Forte@Sun.COM 		}
9997836SJohn.Forte@Sun.COM 		grpname = (stmf_group_name_t *)ibuf;
10007836SJohn.Forte@Sun.COM 
10017836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP)
10027836SJohn.Forte@Sun.COM 			idtype = STMF_ID_TYPE_TARGET_GROUP;
10037836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
10047836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
10057836SJohn.Forte@Sun.COM 			ret = EINVAL;
10067836SJohn.Forte@Sun.COM 			break;
10077836SJohn.Forte@Sun.COM 		}
10087836SJohn.Forte@Sun.COM 		if (grpname->name[0] == '*') {
10097836SJohn.Forte@Sun.COM 			ret = EINVAL;
10107836SJohn.Forte@Sun.COM 			break; /* not allowed */
10117836SJohn.Forte@Sun.COM 		}
10127836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
10137836SJohn.Forte@Sun.COM 		ret = stmf_add_group(grpname->name,
10147836SJohn.Forte@Sun.COM 		    grpname->name_size, idtype, &iocd->stmf_error);
10157836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
10167836SJohn.Forte@Sun.COM 		break;
10177836SJohn.Forte@Sun.COM 	case STMF_IOCTL_REMOVE_HOST_GROUP:
10187836SJohn.Forte@Sun.COM 		idtype = STMF_ID_TYPE_HOST_GROUP;
10197836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
10207836SJohn.Forte@Sun.COM 	case STMF_IOCTL_REMOVE_TARGET_GROUP:
10217836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
10227836SJohn.Forte@Sun.COM 			ret = EACCES;
10237836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
10247836SJohn.Forte@Sun.COM 			break;
10257836SJohn.Forte@Sun.COM 		}
10267836SJohn.Forte@Sun.COM 		grpname = (stmf_group_name_t *)ibuf;
10277836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP)
10287836SJohn.Forte@Sun.COM 			idtype = STMF_ID_TYPE_TARGET_GROUP;
10297836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
10307836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
10317836SJohn.Forte@Sun.COM 			ret = EINVAL;
10327836SJohn.Forte@Sun.COM 			break;
10337836SJohn.Forte@Sun.COM 		}
10347836SJohn.Forte@Sun.COM 		if (grpname->name[0] == '*') {
10357836SJohn.Forte@Sun.COM 			ret = EINVAL;
10367836SJohn.Forte@Sun.COM 			break; /* not allowed */
10377836SJohn.Forte@Sun.COM 		}
10387836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
10397836SJohn.Forte@Sun.COM 		ret = stmf_remove_group(grpname->name,
10407836SJohn.Forte@Sun.COM 		    grpname->name_size, idtype, &iocd->stmf_error);
10417836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
10427836SJohn.Forte@Sun.COM 		break;
104310691STim.Szeto@Sun.COM 	case STMF_IOCTL_VALIDATE_VIEW:
10447836SJohn.Forte@Sun.COM 	case STMF_IOCTL_ADD_VIEW_ENTRY:
10457836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
10467836SJohn.Forte@Sun.COM 			ret = EACCES;
10477836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
10487836SJohn.Forte@Sun.COM 			break;
10497836SJohn.Forte@Sun.COM 		}
10507836SJohn.Forte@Sun.COM 		ve = (stmf_view_op_entry_t *)ibuf;
10517836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
10527836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
10537836SJohn.Forte@Sun.COM 			ret = EINVAL;
10547836SJohn.Forte@Sun.COM 			break;
10557836SJohn.Forte@Sun.COM 		}
10567836SJohn.Forte@Sun.COM 		if (!ve->ve_lu_number_valid)
10577836SJohn.Forte@Sun.COM 			ve->ve_lu_nbr[2] = 0xFF;
10587836SJohn.Forte@Sun.COM 		if (ve->ve_all_hosts) {
10597836SJohn.Forte@Sun.COM 			ve->ve_host_group.name[0] = '*';
10607836SJohn.Forte@Sun.COM 			ve->ve_host_group.name_size = 1;
10617836SJohn.Forte@Sun.COM 		}
10627836SJohn.Forte@Sun.COM 		if (ve->ve_all_targets) {
10637836SJohn.Forte@Sun.COM 			ve->ve_target_group.name[0] = '*';
10647836SJohn.Forte@Sun.COM 			ve->ve_target_group.name_size = 1;
10657836SJohn.Forte@Sun.COM 		}
10667836SJohn.Forte@Sun.COM 		if (ve->ve_ndx_valid)
10677836SJohn.Forte@Sun.COM 			veid = ve->ve_ndx;
10687836SJohn.Forte@Sun.COM 		else
10697836SJohn.Forte@Sun.COM 			veid = 0xffffffff;
10707836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
107110691STim.Szeto@Sun.COM 		if (cmd == STMF_IOCTL_ADD_VIEW_ENTRY) {
107210691STim.Szeto@Sun.COM 			ret = stmf_add_ve(ve->ve_host_group.name,
107310691STim.Szeto@Sun.COM 			    ve->ve_host_group.name_size,
107410691STim.Szeto@Sun.COM 			    ve->ve_target_group.name,
107510691STim.Szeto@Sun.COM 			    ve->ve_target_group.name_size,
107610691STim.Szeto@Sun.COM 			    ve->ve_guid,
107710691STim.Szeto@Sun.COM 			    &veid,
107810691STim.Szeto@Sun.COM 			    ve->ve_lu_nbr,
107910691STim.Szeto@Sun.COM 			    &iocd->stmf_error);
108010691STim.Szeto@Sun.COM 		} else {  /* STMF_IOCTL_VALIDATE_VIEW */
108110691STim.Szeto@Sun.COM 			ret = stmf_validate_lun_ve(ve->ve_host_group.name,
108210691STim.Szeto@Sun.COM 			    ve->ve_host_group.name_size,
108310691STim.Szeto@Sun.COM 			    ve->ve_target_group.name,
108410691STim.Szeto@Sun.COM 			    ve->ve_target_group.name_size,
108510691STim.Szeto@Sun.COM 			    ve->ve_lu_nbr,
108610691STim.Szeto@Sun.COM 			    &iocd->stmf_error);
108710691STim.Szeto@Sun.COM 		}
10887836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
10897836SJohn.Forte@Sun.COM 		if (ret == 0 &&
10907836SJohn.Forte@Sun.COM 		    (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) &&
10917836SJohn.Forte@Sun.COM 		    iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) {
10927836SJohn.Forte@Sun.COM 			stmf_view_op_entry_t *ve_ret =
10937836SJohn.Forte@Sun.COM 			    (stmf_view_op_entry_t *)obuf;
10947836SJohn.Forte@Sun.COM 			iocd->stmf_obuf_nentries = 1;
10957836SJohn.Forte@Sun.COM 			iocd->stmf_obuf_max_nentries = 1;
10967836SJohn.Forte@Sun.COM 			if (!ve->ve_ndx_valid) {
10977836SJohn.Forte@Sun.COM 				ve_ret->ve_ndx = veid;
10987836SJohn.Forte@Sun.COM 				ve_ret->ve_ndx_valid = 1;
10997836SJohn.Forte@Sun.COM 			}
11007836SJohn.Forte@Sun.COM 			if (!ve->ve_lu_number_valid) {
11017836SJohn.Forte@Sun.COM 				ve_ret->ve_lu_number_valid = 1;
11027836SJohn.Forte@Sun.COM 				bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8);
11037836SJohn.Forte@Sun.COM 			}
11047836SJohn.Forte@Sun.COM 		}
11057836SJohn.Forte@Sun.COM 		break;
11067836SJohn.Forte@Sun.COM 	case STMF_IOCTL_REMOVE_VIEW_ENTRY:
11077836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
11087836SJohn.Forte@Sun.COM 			ret = EACCES;
11097836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
11107836SJohn.Forte@Sun.COM 			break;
11117836SJohn.Forte@Sun.COM 		}
11127836SJohn.Forte@Sun.COM 		ve = (stmf_view_op_entry_t *)ibuf;
11137836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
11147836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
11157836SJohn.Forte@Sun.COM 			ret = EINVAL;
11167836SJohn.Forte@Sun.COM 			break;
11177836SJohn.Forte@Sun.COM 		}
11187836SJohn.Forte@Sun.COM 		if (!ve->ve_ndx_valid) {
11197836SJohn.Forte@Sun.COM 			ret = EINVAL;
11207836SJohn.Forte@Sun.COM 			break;
11217836SJohn.Forte@Sun.COM 		}
11227836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
11237836SJohn.Forte@Sun.COM 		ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx,
11247836SJohn.Forte@Sun.COM 		    &iocd->stmf_error);
11257836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
11267836SJohn.Forte@Sun.COM 		break;
11277836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_HG_LIST:
11287836SJohn.Forte@Sun.COM 		id_list = &stmf_state.stmf_hg_list;
11297836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
11307836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_TG_LIST:
11317836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_GET_TG_LIST)
11327836SJohn.Forte@Sun.COM 			id_list = &stmf_state.stmf_tg_list;
11337836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
11347836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_max_nentries = id_list->id_count;
11357836SJohn.Forte@Sun.COM 		n = min(id_list->id_count,
11367836SJohn.Forte@Sun.COM 		    (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t));
11377836SJohn.Forte@Sun.COM 		iocd->stmf_obuf_nentries = n;
11387836SJohn.Forte@Sun.COM 		id_entry = id_list->idl_head;
11397836SJohn.Forte@Sun.COM 		grpname = (stmf_group_name_t *)obuf;
11407836SJohn.Forte@Sun.COM 		for (i = 0; i < n; i++) {
11419585STim.Szeto@Sun.COM 			if (id_entry->id_data[0] == '*') {
11429585STim.Szeto@Sun.COM 				if (iocd->stmf_obuf_nentries > 0) {
11439585STim.Szeto@Sun.COM 					iocd->stmf_obuf_nentries--;
11449585STim.Szeto@Sun.COM 				}
11459585STim.Szeto@Sun.COM 				id_entry = id_entry->id_next;
11469585STim.Szeto@Sun.COM 				continue;
11479585STim.Szeto@Sun.COM 			}
11489585STim.Szeto@Sun.COM 			grpname->name_size = id_entry->id_data_size;
11499585STim.Szeto@Sun.COM 			bcopy(id_entry->id_data, grpname->name,
11507836SJohn.Forte@Sun.COM 			    id_entry->id_data_size);
11519585STim.Szeto@Sun.COM 			grpname++;
11527836SJohn.Forte@Sun.COM 			id_entry = id_entry->id_next;
11537836SJohn.Forte@Sun.COM 		}
11547836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
11557836SJohn.Forte@Sun.COM 		break;
11567836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_HG_ENTRIES:
11577836SJohn.Forte@Sun.COM 		id_list = &stmf_state.stmf_hg_list;
11587836SJohn.Forte@Sun.COM 		/* FALLTHROUGH */
11597836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_TG_ENTRIES:
11607836SJohn.Forte@Sun.COM 		grpname = (stmf_group_name_t *)ibuf;
11617836SJohn.Forte@Sun.COM 		if ((ibuf == NULL) ||
11627836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
11637836SJohn.Forte@Sun.COM 			ret = EINVAL;
11647836SJohn.Forte@Sun.COM 			break;
11657836SJohn.Forte@Sun.COM 		}
11667836SJohn.Forte@Sun.COM 		if (cmd == STMF_IOCTL_GET_TG_ENTRIES) {
11677836SJohn.Forte@Sun.COM 			id_list = &stmf_state.stmf_tg_list;
11687836SJohn.Forte@Sun.COM 		}
11697836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
11707836SJohn.Forte@Sun.COM 		id_entry = stmf_lookup_id(id_list, grpname->name_size,
11717836SJohn.Forte@Sun.COM 		    grpname->name);
11727836SJohn.Forte@Sun.COM 		if (!id_entry)
11737836SJohn.Forte@Sun.COM 			ret = ENODEV;
11747836SJohn.Forte@Sun.COM 		else {
11757836SJohn.Forte@Sun.COM 			stmf_ge_ident_t *grp_entry;
11767836SJohn.Forte@Sun.COM 			id_list = (stmf_id_list_t *)id_entry->id_impl_specific;
11777836SJohn.Forte@Sun.COM 			iocd->stmf_obuf_max_nentries = id_list->id_count;
11787836SJohn.Forte@Sun.COM 			n = min(id_list->id_count,
11797836SJohn.Forte@Sun.COM 			    iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t));
11807836SJohn.Forte@Sun.COM 			iocd->stmf_obuf_nentries = n;
11817836SJohn.Forte@Sun.COM 			id_entry = id_list->idl_head;
11827836SJohn.Forte@Sun.COM 			grp_entry = (stmf_ge_ident_t *)obuf;
11837836SJohn.Forte@Sun.COM 			for (i = 0; i < n; i++) {
11849585STim.Szeto@Sun.COM 				bcopy(id_entry->id_data, grp_entry->ident,
11857836SJohn.Forte@Sun.COM 				    id_entry->id_data_size);
11869585STim.Szeto@Sun.COM 				grp_entry->ident_size = id_entry->id_data_size;
11877836SJohn.Forte@Sun.COM 				id_entry = id_entry->id_next;
11889585STim.Szeto@Sun.COM 				grp_entry++;
11897836SJohn.Forte@Sun.COM 			}
11907836SJohn.Forte@Sun.COM 		}
11917836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
11927836SJohn.Forte@Sun.COM 		break;
11939585STim.Szeto@Sun.COM 
11947836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_VE_LIST:
11957836SJohn.Forte@Sun.COM 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
11967836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
11977836SJohn.Forte@Sun.COM 		ve = (stmf_view_op_entry_t *)obuf;
11989585STim.Szeto@Sun.COM 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
11999585STim.Szeto@Sun.COM 		    id_entry; id_entry = id_entry->id_next) {
12009585STim.Szeto@Sun.COM 			for (view_entry = (stmf_view_entry_t *)
12019585STim.Szeto@Sun.COM 			    id_entry->id_impl_specific; view_entry;
12029585STim.Szeto@Sun.COM 			    view_entry = view_entry->ve_next) {
12039585STim.Szeto@Sun.COM 				iocd->stmf_obuf_max_nentries++;
12049585STim.Szeto@Sun.COM 				if (iocd->stmf_obuf_nentries >= n)
12059585STim.Szeto@Sun.COM 					continue;
12067836SJohn.Forte@Sun.COM 				ve->ve_ndx_valid = 1;
12077836SJohn.Forte@Sun.COM 				ve->ve_ndx = view_entry->ve_id;
12087836SJohn.Forte@Sun.COM 				ve->ve_lu_number_valid = 1;
12097836SJohn.Forte@Sun.COM 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
12107836SJohn.Forte@Sun.COM 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
12117836SJohn.Forte@Sun.COM 				    view_entry->ve_luid->id_data_size);
12129585STim.Szeto@Sun.COM 				if (view_entry->ve_hg->id_data[0] == '*') {
12137836SJohn.Forte@Sun.COM 					ve->ve_all_hosts = 1;
12149585STim.Szeto@Sun.COM 				} else {
12157836SJohn.Forte@Sun.COM 					bcopy(view_entry->ve_hg->id_data,
12167836SJohn.Forte@Sun.COM 					    ve->ve_host_group.name,
12177836SJohn.Forte@Sun.COM 					    view_entry->ve_hg->id_data_size);
12189585STim.Szeto@Sun.COM 					ve->ve_host_group.name_size =
12199585STim.Szeto@Sun.COM 					    view_entry->ve_hg->id_data_size;
12209585STim.Szeto@Sun.COM 				}
12219585STim.Szeto@Sun.COM 
12229585STim.Szeto@Sun.COM 				if (view_entry->ve_tg->id_data[0] == '*') {
12237836SJohn.Forte@Sun.COM 					ve->ve_all_targets = 1;
12249585STim.Szeto@Sun.COM 				} else {
12257836SJohn.Forte@Sun.COM 					bcopy(view_entry->ve_tg->id_data,
12267836SJohn.Forte@Sun.COM 					    ve->ve_target_group.name,
12277836SJohn.Forte@Sun.COM 					    view_entry->ve_tg->id_data_size);
12289585STim.Szeto@Sun.COM 					ve->ve_target_group.name_size =
12299585STim.Szeto@Sun.COM 					    view_entry->ve_tg->id_data_size;
12309585STim.Szeto@Sun.COM 				}
12319585STim.Szeto@Sun.COM 				ve++;
12327836SJohn.Forte@Sun.COM 				iocd->stmf_obuf_nentries++;
12337836SJohn.Forte@Sun.COM 			}
12347836SJohn.Forte@Sun.COM 		}
12357836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
12367836SJohn.Forte@Sun.COM 		break;
12379585STim.Szeto@Sun.COM 
12389585STim.Szeto@Sun.COM 	case STMF_IOCTL_LU_VE_LIST:
12399585STim.Szeto@Sun.COM 		p_id = (uint8_t *)ibuf;
12409585STim.Szeto@Sun.COM 		if ((iocd->stmf_ibuf_size != 16) ||
12419585STim.Szeto@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (stmf_view_op_entry_t))) {
12429585STim.Szeto@Sun.COM 			ret = EINVAL;
12439585STim.Szeto@Sun.COM 			break;
12449585STim.Szeto@Sun.COM 		}
12459585STim.Szeto@Sun.COM 
12469585STim.Szeto@Sun.COM 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
12479585STim.Szeto@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
12489585STim.Szeto@Sun.COM 		ve = (stmf_view_op_entry_t *)obuf;
12499585STim.Szeto@Sun.COM 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
12509585STim.Szeto@Sun.COM 		    id_entry; id_entry = id_entry->id_next) {
12519585STim.Szeto@Sun.COM 			if (bcmp(id_entry->id_data, p_id, 16) != 0)
12529585STim.Szeto@Sun.COM 				continue;
12539585STim.Szeto@Sun.COM 			for (view_entry = (stmf_view_entry_t *)
12549585STim.Szeto@Sun.COM 			    id_entry->id_impl_specific; view_entry;
12559585STim.Szeto@Sun.COM 			    view_entry = view_entry->ve_next) {
12569585STim.Szeto@Sun.COM 				iocd->stmf_obuf_max_nentries++;
12579585STim.Szeto@Sun.COM 				if (iocd->stmf_obuf_nentries >= n)
12589585STim.Szeto@Sun.COM 					continue;
12599585STim.Szeto@Sun.COM 				ve->ve_ndx_valid = 1;
12609585STim.Szeto@Sun.COM 				ve->ve_ndx = view_entry->ve_id;
12619585STim.Szeto@Sun.COM 				ve->ve_lu_number_valid = 1;
12629585STim.Szeto@Sun.COM 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
12639585STim.Szeto@Sun.COM 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
12649585STim.Szeto@Sun.COM 				    view_entry->ve_luid->id_data_size);
12659585STim.Szeto@Sun.COM 				if (view_entry->ve_hg->id_data[0] == '*') {
12669585STim.Szeto@Sun.COM 					ve->ve_all_hosts = 1;
12679585STim.Szeto@Sun.COM 				} else {
12689585STim.Szeto@Sun.COM 					bcopy(view_entry->ve_hg->id_data,
12699585STim.Szeto@Sun.COM 					    ve->ve_host_group.name,
12709585STim.Szeto@Sun.COM 					    view_entry->ve_hg->id_data_size);
12719585STim.Szeto@Sun.COM 					ve->ve_host_group.name_size =
12729585STim.Szeto@Sun.COM 					    view_entry->ve_hg->id_data_size;
12739585STim.Szeto@Sun.COM 				}
12749585STim.Szeto@Sun.COM 
12759585STim.Szeto@Sun.COM 				if (view_entry->ve_tg->id_data[0] == '*') {
12769585STim.Szeto@Sun.COM 					ve->ve_all_targets = 1;
12779585STim.Szeto@Sun.COM 				} else {
12789585STim.Szeto@Sun.COM 					bcopy(view_entry->ve_tg->id_data,
12799585STim.Szeto@Sun.COM 					    ve->ve_target_group.name,
12809585STim.Szeto@Sun.COM 					    view_entry->ve_tg->id_data_size);
12819585STim.Szeto@Sun.COM 					ve->ve_target_group.name_size =
12829585STim.Szeto@Sun.COM 					    view_entry->ve_tg->id_data_size;
12839585STim.Szeto@Sun.COM 				}
12849585STim.Szeto@Sun.COM 				ve++;
12859585STim.Szeto@Sun.COM 				iocd->stmf_obuf_nentries++;
12869585STim.Szeto@Sun.COM 			}
12879585STim.Szeto@Sun.COM 			break;
12889585STim.Szeto@Sun.COM 		}
12899585STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
12909585STim.Szeto@Sun.COM 		break;
12919585STim.Szeto@Sun.COM 
12927836SJohn.Forte@Sun.COM 	case STMF_IOCTL_LOAD_PP_DATA:
12937836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
12947836SJohn.Forte@Sun.COM 			ret = EACCES;
12957836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
12967836SJohn.Forte@Sun.COM 			break;
12977836SJohn.Forte@Sun.COM 		}
12987836SJohn.Forte@Sun.COM 		ppi = (stmf_ppioctl_data_t *)ibuf;
12997836SJohn.Forte@Sun.COM 		if ((ppi == NULL) ||
13007836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
13017836SJohn.Forte@Sun.COM 			ret = EINVAL;
13027836SJohn.Forte@Sun.COM 			break;
13037836SJohn.Forte@Sun.COM 		}
13049585STim.Szeto@Sun.COM 		/* returned token */
13059585STim.Szeto@Sun.COM 		ppi_token = (uint64_t *)obuf;
13069585STim.Szeto@Sun.COM 		if ((ppi_token == NULL) ||
13079585STim.Szeto@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (uint64_t))) {
13089585STim.Szeto@Sun.COM 			ret = EINVAL;
13099585STim.Szeto@Sun.COM 			break;
13109585STim.Szeto@Sun.COM 		}
13119585STim.Szeto@Sun.COM 		ret = stmf_load_ppd_ioctl(ppi, ppi_token, &iocd->stmf_error);
13129585STim.Szeto@Sun.COM 		break;
13139585STim.Szeto@Sun.COM 
13149585STim.Szeto@Sun.COM 	case STMF_IOCTL_GET_PP_DATA:
13159585STim.Szeto@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
13169585STim.Szeto@Sun.COM 			ret = EACCES;
13179585STim.Szeto@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
13189585STim.Szeto@Sun.COM 			break;
13199585STim.Szeto@Sun.COM 		}
13209585STim.Szeto@Sun.COM 		ppi = (stmf_ppioctl_data_t *)ibuf;
13219585STim.Szeto@Sun.COM 		if (ppi == NULL ||
13229585STim.Szeto@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
13239585STim.Szeto@Sun.COM 			ret = EINVAL;
13249585STim.Szeto@Sun.COM 			break;
13259585STim.Szeto@Sun.COM 		}
13269585STim.Szeto@Sun.COM 		ppi_out = (stmf_ppioctl_data_t *)obuf;
13279585STim.Szeto@Sun.COM 		if ((ppi_out == NULL) ||
13289585STim.Szeto@Sun.COM 		    (iocd->stmf_obuf_size < sizeof (stmf_ppioctl_data_t))) {
13299585STim.Szeto@Sun.COM 			ret = EINVAL;
13309585STim.Szeto@Sun.COM 			break;
13319585STim.Szeto@Sun.COM 		}
13329585STim.Szeto@Sun.COM 		ret = stmf_get_ppd_ioctl(ppi, ppi_out, &iocd->stmf_error);
13337836SJohn.Forte@Sun.COM 		break;
13347836SJohn.Forte@Sun.COM 
13357836SJohn.Forte@Sun.COM 	case STMF_IOCTL_CLEAR_PP_DATA:
13367836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
13377836SJohn.Forte@Sun.COM 			ret = EACCES;
13387836SJohn.Forte@Sun.COM 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
13397836SJohn.Forte@Sun.COM 			break;
13407836SJohn.Forte@Sun.COM 		}
13417836SJohn.Forte@Sun.COM 		ppi = (stmf_ppioctl_data_t *)ibuf;
13427836SJohn.Forte@Sun.COM 		if ((ppi == NULL) ||
13437836SJohn.Forte@Sun.COM 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
13447836SJohn.Forte@Sun.COM 			ret = EINVAL;
13457836SJohn.Forte@Sun.COM 			break;
13467836SJohn.Forte@Sun.COM 		}
13477836SJohn.Forte@Sun.COM 		ret = stmf_delete_ppd_ioctl(ppi);
13487836SJohn.Forte@Sun.COM 		break;
13497836SJohn.Forte@Sun.COM 
13507836SJohn.Forte@Sun.COM 	case STMF_IOCTL_CLEAR_TRACE:
13517836SJohn.Forte@Sun.COM 		stmf_trace_clear();
13527836SJohn.Forte@Sun.COM 		break;
13537836SJohn.Forte@Sun.COM 
13547836SJohn.Forte@Sun.COM 	case STMF_IOCTL_ADD_TRACE:
13557836SJohn.Forte@Sun.COM 		if (iocd->stmf_ibuf_size && ibuf) {
13567836SJohn.Forte@Sun.COM 			((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0;
13577836SJohn.Forte@Sun.COM 			stmf_trace("\nstradm", "%s\n", ibuf);
13587836SJohn.Forte@Sun.COM 		}
13597836SJohn.Forte@Sun.COM 		break;
13607836SJohn.Forte@Sun.COM 
13617836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_TRACE_POSITION:
13627836SJohn.Forte@Sun.COM 		if (obuf && (iocd->stmf_obuf_size > 3)) {
13637836SJohn.Forte@Sun.COM 			mutex_enter(&trace_buf_lock);
13647836SJohn.Forte@Sun.COM 			*((int *)obuf) = trace_buf_curndx;
13657836SJohn.Forte@Sun.COM 			mutex_exit(&trace_buf_lock);
13667836SJohn.Forte@Sun.COM 		} else {
13677836SJohn.Forte@Sun.COM 			ret = EINVAL;
13687836SJohn.Forte@Sun.COM 		}
13697836SJohn.Forte@Sun.COM 		break;
13707836SJohn.Forte@Sun.COM 
13717836SJohn.Forte@Sun.COM 	case STMF_IOCTL_GET_TRACE:
13727836SJohn.Forte@Sun.COM 		if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) {
13737836SJohn.Forte@Sun.COM 			ret = EINVAL;
13747836SJohn.Forte@Sun.COM 			break;
13757836SJohn.Forte@Sun.COM 		}
13767836SJohn.Forte@Sun.COM 		i = *((int *)ibuf);
13777836SJohn.Forte@Sun.COM 		if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) >
13787836SJohn.Forte@Sun.COM 		    trace_buf_size)) {
13797836SJohn.Forte@Sun.COM 			ret = EINVAL;
13807836SJohn.Forte@Sun.COM 			break;
13817836SJohn.Forte@Sun.COM 		}
13827836SJohn.Forte@Sun.COM 		mutex_enter(&trace_buf_lock);
13837836SJohn.Forte@Sun.COM 		bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size);
13847836SJohn.Forte@Sun.COM 		mutex_exit(&trace_buf_lock);
13857836SJohn.Forte@Sun.COM 		break;
13867836SJohn.Forte@Sun.COM 
13877836SJohn.Forte@Sun.COM 	default:
13887836SJohn.Forte@Sun.COM 		ret = ENOTTY;
13897836SJohn.Forte@Sun.COM 	}
13907836SJohn.Forte@Sun.COM 
13917836SJohn.Forte@Sun.COM 	if (ret == 0) {
13927836SJohn.Forte@Sun.COM 		ret = stmf_copyout_iocdata(data, mode, iocd, obuf);
13937836SJohn.Forte@Sun.COM 	} else if (iocd->stmf_error) {
13947836SJohn.Forte@Sun.COM 		(void) stmf_copyout_iocdata(data, mode, iocd, obuf);
13957836SJohn.Forte@Sun.COM 	}
13967836SJohn.Forte@Sun.COM 	if (obuf) {
13977836SJohn.Forte@Sun.COM 		kmem_free(obuf, iocd->stmf_obuf_size);
13987836SJohn.Forte@Sun.COM 		obuf = NULL;
13997836SJohn.Forte@Sun.COM 	}
14007836SJohn.Forte@Sun.COM 	if (ibuf) {
14017836SJohn.Forte@Sun.COM 		kmem_free(ibuf, iocd->stmf_ibuf_size);
14027836SJohn.Forte@Sun.COM 		ibuf = NULL;
14037836SJohn.Forte@Sun.COM 	}
14047836SJohn.Forte@Sun.COM 	kmem_free(iocd, sizeof (stmf_iocdata_t));
14057836SJohn.Forte@Sun.COM 	return (ret);
14067836SJohn.Forte@Sun.COM }
14077836SJohn.Forte@Sun.COM 
14087836SJohn.Forte@Sun.COM static int
stmf_get_service_state()14097836SJohn.Forte@Sun.COM stmf_get_service_state()
14107836SJohn.Forte@Sun.COM {
14117836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
14127836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
14137836SJohn.Forte@Sun.COM 	int online = 0;
14147836SJohn.Forte@Sun.COM 	int offline = 0;
14157836SJohn.Forte@Sun.COM 	int onlining = 0;
14167836SJohn.Forte@Sun.COM 	int offlining = 0;
14177836SJohn.Forte@Sun.COM 
14187836SJohn.Forte@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
14197836SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
14207836SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
14217836SJohn.Forte@Sun.COM 		if (ilport->ilport_state == STMF_STATE_OFFLINE)
14227836SJohn.Forte@Sun.COM 			offline++;
14237836SJohn.Forte@Sun.COM 		else if (ilport->ilport_state == STMF_STATE_ONLINE)
14247836SJohn.Forte@Sun.COM 			online++;
14257836SJohn.Forte@Sun.COM 		else if (ilport->ilport_state == STMF_STATE_ONLINING)
14267836SJohn.Forte@Sun.COM 			onlining++;
14277836SJohn.Forte@Sun.COM 		else if (ilport->ilport_state == STMF_STATE_OFFLINING)
14287836SJohn.Forte@Sun.COM 			offlining++;
14297836SJohn.Forte@Sun.COM 	}
14307836SJohn.Forte@Sun.COM 
14317836SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
14327836SJohn.Forte@Sun.COM 	    ilu = ilu->ilu_next) {
14337836SJohn.Forte@Sun.COM 		if (ilu->ilu_state == STMF_STATE_OFFLINE)
14347836SJohn.Forte@Sun.COM 			offline++;
14357836SJohn.Forte@Sun.COM 		else if (ilu->ilu_state == STMF_STATE_ONLINE)
14367836SJohn.Forte@Sun.COM 			online++;
14377836SJohn.Forte@Sun.COM 		else if (ilu->ilu_state == STMF_STATE_ONLINING)
14387836SJohn.Forte@Sun.COM 			onlining++;
14397836SJohn.Forte@Sun.COM 		else if (ilu->ilu_state == STMF_STATE_OFFLINING)
14407836SJohn.Forte@Sun.COM 			offlining++;
14417836SJohn.Forte@Sun.COM 	}
14427836SJohn.Forte@Sun.COM 
14437836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_service_running) {
14447836SJohn.Forte@Sun.COM 		if (onlining)
14457836SJohn.Forte@Sun.COM 			return (STMF_STATE_ONLINING);
14467836SJohn.Forte@Sun.COM 		else
14477836SJohn.Forte@Sun.COM 			return (STMF_STATE_ONLINE);
14487836SJohn.Forte@Sun.COM 	}
14497836SJohn.Forte@Sun.COM 
14507836SJohn.Forte@Sun.COM 	if (offlining) {
14517836SJohn.Forte@Sun.COM 		return (STMF_STATE_OFFLINING);
14527836SJohn.Forte@Sun.COM 	}
14537836SJohn.Forte@Sun.COM 
14547836SJohn.Forte@Sun.COM 	return (STMF_STATE_OFFLINE);
14557836SJohn.Forte@Sun.COM }
14567836SJohn.Forte@Sun.COM 
14577836SJohn.Forte@Sun.COM static int
stmf_set_stmf_state(stmf_state_desc_t * std)14587836SJohn.Forte@Sun.COM stmf_set_stmf_state(stmf_state_desc_t *std)
14597836SJohn.Forte@Sun.COM {
14607836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
14617836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
14627836SJohn.Forte@Sun.COM 	stmf_state_change_info_t ssi;
14637836SJohn.Forte@Sun.COM 	int svc_state;
14647836SJohn.Forte@Sun.COM 
14657836SJohn.Forte@Sun.COM 	ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
14667836SJohn.Forte@Sun.COM 	ssi.st_additional_info = NULL;
14677836SJohn.Forte@Sun.COM 
14687836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
14697836SJohn.Forte@Sun.COM 	if (!stmf_state.stmf_exclusive_open) {
14707836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
14717836SJohn.Forte@Sun.COM 		return (EACCES);
14727836SJohn.Forte@Sun.COM 	}
14737836SJohn.Forte@Sun.COM 
14747836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
14757836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
14767836SJohn.Forte@Sun.COM 		return (EBUSY);
14777836SJohn.Forte@Sun.COM 	}
14787836SJohn.Forte@Sun.COM 
14797836SJohn.Forte@Sun.COM 	if ((std->state != STMF_STATE_ONLINE) &&
14807836SJohn.Forte@Sun.COM 	    (std->state != STMF_STATE_OFFLINE)) {
14817836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
14827836SJohn.Forte@Sun.COM 		return (EINVAL);
14837836SJohn.Forte@Sun.COM 	}
14847836SJohn.Forte@Sun.COM 
14857836SJohn.Forte@Sun.COM 	svc_state = stmf_get_service_state();
14867836SJohn.Forte@Sun.COM 	if ((svc_state == STMF_STATE_OFFLINING) ||
14877836SJohn.Forte@Sun.COM 	    (svc_state == STMF_STATE_ONLINING)) {
14887836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
14897836SJohn.Forte@Sun.COM 		return (EBUSY);
14907836SJohn.Forte@Sun.COM 	}
14917836SJohn.Forte@Sun.COM 
14927836SJohn.Forte@Sun.COM 	if (svc_state == STMF_STATE_OFFLINE) {
14937836SJohn.Forte@Sun.COM 		if (std->config_state == STMF_CONFIG_INIT) {
14947836SJohn.Forte@Sun.COM 			if (std->state != STMF_STATE_OFFLINE) {
14957836SJohn.Forte@Sun.COM 				mutex_exit(&stmf_state.stmf_lock);
14967836SJohn.Forte@Sun.COM 				return (EINVAL);
14977836SJohn.Forte@Sun.COM 			}
14987836SJohn.Forte@Sun.COM 			stmf_state.stmf_config_state = STMF_CONFIG_INIT;
14997836SJohn.Forte@Sun.COM 			stmf_delete_all_ppds();
15007836SJohn.Forte@Sun.COM 			stmf_view_clear_config();
15017836SJohn.Forte@Sun.COM 			stmf_view_init();
15027836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
15037836SJohn.Forte@Sun.COM 			return (0);
15047836SJohn.Forte@Sun.COM 		}
150510716SSusan.Gleeson@Sun.COM 		if ((stmf_state.stmf_config_state == STMF_CONFIG_INIT) ||
150610716SSusan.Gleeson@Sun.COM 		    (stmf_state.stmf_config_state == STMF_CONFIG_NONE)) {
15077836SJohn.Forte@Sun.COM 			if (std->config_state != STMF_CONFIG_INIT_DONE) {
15087836SJohn.Forte@Sun.COM 				mutex_exit(&stmf_state.stmf_lock);
15097836SJohn.Forte@Sun.COM 				return (EINVAL);
15107836SJohn.Forte@Sun.COM 			}
15117836SJohn.Forte@Sun.COM 			stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE;
15127836SJohn.Forte@Sun.COM 		}
15137836SJohn.Forte@Sun.COM 		if (std->state == STMF_STATE_OFFLINE) {
15147836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
15157836SJohn.Forte@Sun.COM 			return (0);
15167836SJohn.Forte@Sun.COM 		}
15177836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) {
15187836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
15197836SJohn.Forte@Sun.COM 			return (EINVAL);
15207836SJohn.Forte@Sun.COM 		}
15217836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 1;
15227836SJohn.Forte@Sun.COM 		stmf_state.stmf_service_running = 1;
15237836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
15247836SJohn.Forte@Sun.COM 
15257836SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
15267836SJohn.Forte@Sun.COM 		    ilport = ilport->ilport_next) {
152712682SSrivijitha.Dugganapalli@Sun.COM 			if (stmf_state.stmf_default_lport_state !=
152812682SSrivijitha.Dugganapalli@Sun.COM 			    STMF_STATE_ONLINE)
15297836SJohn.Forte@Sun.COM 				continue;
15307836SJohn.Forte@Sun.COM 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE,
15318662SJordan.Vaughan@Sun.com 			    ilport->ilport_lport, &ssi);
15327836SJohn.Forte@Sun.COM 		}
15337836SJohn.Forte@Sun.COM 
15347836SJohn.Forte@Sun.COM 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
15357836SJohn.Forte@Sun.COM 		    ilu = ilu->ilu_next) {
153612682SSrivijitha.Dugganapalli@Sun.COM 			if (stmf_state.stmf_default_lu_state !=
153712682SSrivijitha.Dugganapalli@Sun.COM 			    STMF_STATE_ONLINE)
15387836SJohn.Forte@Sun.COM 				continue;
15397836SJohn.Forte@Sun.COM 			(void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi);
15407836SJohn.Forte@Sun.COM 		}
15417836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
15427836SJohn.Forte@Sun.COM 		stmf_state.stmf_inventory_locked = 0;
15437836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
15447836SJohn.Forte@Sun.COM 		return (0);
15457836SJohn.Forte@Sun.COM 	}
15467836SJohn.Forte@Sun.COM 
15477836SJohn.Forte@Sun.COM 	/* svc_state is STMF_STATE_ONLINE here */
15487836SJohn.Forte@Sun.COM 	if ((std->state != STMF_STATE_OFFLINE) ||
15497836SJohn.Forte@Sun.COM 	    (std->config_state == STMF_CONFIG_INIT)) {
15507836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
15517836SJohn.Forte@Sun.COM 		return (EACCES);
15527836SJohn.Forte@Sun.COM 	}
15537836SJohn.Forte@Sun.COM 
15547836SJohn.Forte@Sun.COM 	stmf_state.stmf_inventory_locked = 1;
15557836SJohn.Forte@Sun.COM 	stmf_state.stmf_service_running = 0;
155610560SSusan.Gleeson@Sun.COM 
15577836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
15587836SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
15597836SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
15607836SJohn.Forte@Sun.COM 		if (ilport->ilport_state != STMF_STATE_ONLINE)
15617836SJohn.Forte@Sun.COM 			continue;
15627836SJohn.Forte@Sun.COM 		(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE,
15637836SJohn.Forte@Sun.COM 		    ilport->ilport_lport, &ssi);
15647836SJohn.Forte@Sun.COM 	}
15657836SJohn.Forte@Sun.COM 
15667836SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
15677836SJohn.Forte@Sun.COM 	    ilu = ilu->ilu_next) {
15687836SJohn.Forte@Sun.COM 		if (ilu->ilu_state != STMF_STATE_ONLINE)
15697836SJohn.Forte@Sun.COM 			continue;
15707836SJohn.Forte@Sun.COM 		(void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi);
15717836SJohn.Forte@Sun.COM 	}
15727836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
15737836SJohn.Forte@Sun.COM 	stmf_state.stmf_inventory_locked = 0;
15747836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
15757836SJohn.Forte@Sun.COM 	return (0);
15767836SJohn.Forte@Sun.COM }
15777836SJohn.Forte@Sun.COM 
15787836SJohn.Forte@Sun.COM static int
stmf_get_stmf_state(stmf_state_desc_t * std)15797836SJohn.Forte@Sun.COM stmf_get_stmf_state(stmf_state_desc_t *std)
15807836SJohn.Forte@Sun.COM {
15817836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
15827836SJohn.Forte@Sun.COM 	std->state = stmf_get_service_state();
15837836SJohn.Forte@Sun.COM 	std->config_state = stmf_state.stmf_config_state;
15847836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
15857836SJohn.Forte@Sun.COM 
15867836SJohn.Forte@Sun.COM 	return (0);
15877836SJohn.Forte@Sun.COM }
158810725SJohn.Forte@Sun.COM /*
158910725SJohn.Forte@Sun.COM  * handles registration message from pppt for a logical unit
159010725SJohn.Forte@Sun.COM  */
159110725SJohn.Forte@Sun.COM stmf_status_t
stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t * msg,uint32_t type)159210725SJohn.Forte@Sun.COM stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg, uint32_t type)
159310725SJohn.Forte@Sun.COM {
159410725SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t	*ilp;
159510725SJohn.Forte@Sun.COM 	stmf_lu_provider_t	*lp;
159610725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
159710725SJohn.Forte@Sun.COM 	for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
159810725SJohn.Forte@Sun.COM 		if (strcmp(msg->icrl_lu_provider_name,
159910725SJohn.Forte@Sun.COM 		    ilp->ilp_lp->lp_name) == 0) {
160010725SJohn.Forte@Sun.COM 			lp = ilp->ilp_lp;
160110725SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
160210725SJohn.Forte@Sun.COM 			lp->lp_proxy_msg(msg->icrl_lun_id, msg->icrl_cb_arg,
160310725SJohn.Forte@Sun.COM 			    msg->icrl_cb_arg_len, type);
160410725SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
160510725SJohn.Forte@Sun.COM 		}
160610725SJohn.Forte@Sun.COM 	}
160710725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
160810725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
160910725SJohn.Forte@Sun.COM }
161010725SJohn.Forte@Sun.COM 
161110725SJohn.Forte@Sun.COM /*
161210725SJohn.Forte@Sun.COM  * handles de-registration message from pppt for a logical unit
161310725SJohn.Forte@Sun.COM  */
161410725SJohn.Forte@Sun.COM stmf_status_t
stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t * msg)161510725SJohn.Forte@Sun.COM stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg)
161610725SJohn.Forte@Sun.COM {
161710725SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t	*ilp;
161810725SJohn.Forte@Sun.COM 	stmf_lu_provider_t	*lp;
161910725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
162010725SJohn.Forte@Sun.COM 	for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
162110725SJohn.Forte@Sun.COM 		if (strcmp(msg->icrl_lu_provider_name,
162210725SJohn.Forte@Sun.COM 		    ilp->ilp_lp->lp_name) == 0) {
162310725SJohn.Forte@Sun.COM 			lp = ilp->ilp_lp;
162410725SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
162510725SJohn.Forte@Sun.COM 			lp->lp_proxy_msg(msg->icrl_lun_id, NULL, 0,
162610725SJohn.Forte@Sun.COM 			    STMF_MSG_LU_DEREGISTER);
162710725SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
162810725SJohn.Forte@Sun.COM 		}
162910725SJohn.Forte@Sun.COM 	}
163010725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
163110725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
163210725SJohn.Forte@Sun.COM }
163310725SJohn.Forte@Sun.COM 
163410725SJohn.Forte@Sun.COM /*
163510725SJohn.Forte@Sun.COM  * helper function to find a task that matches a task_msgid
163610725SJohn.Forte@Sun.COM  */
163710725SJohn.Forte@Sun.COM scsi_task_t *
find_task_from_msgid(uint8_t * lu_id,stmf_ic_msgid_t task_msgid)163810725SJohn.Forte@Sun.COM find_task_from_msgid(uint8_t *lu_id, stmf_ic_msgid_t task_msgid)
163910725SJohn.Forte@Sun.COM {
164010725SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
164110725SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
164210725SJohn.Forte@Sun.COM 
164310725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
164410725SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
164510725SJohn.Forte@Sun.COM 		if (bcmp(lu_id, ilu->ilu_lu->lu_id->ident, 16) == 0) {
164610725SJohn.Forte@Sun.COM 			break;
164710725SJohn.Forte@Sun.COM 		}
164810725SJohn.Forte@Sun.COM 	}
164910725SJohn.Forte@Sun.COM 
165010725SJohn.Forte@Sun.COM 	if (ilu == NULL) {
165110725SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
165210725SJohn.Forte@Sun.COM 		return (NULL);
165310725SJohn.Forte@Sun.COM 	}
165410725SJohn.Forte@Sun.COM 
165510725SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
165610725SJohn.Forte@Sun.COM 	for (itask = ilu->ilu_tasks; itask != NULL;
165710725SJohn.Forte@Sun.COM 	    itask = itask->itask_lu_next) {
165810725SJohn.Forte@Sun.COM 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
165910725SJohn.Forte@Sun.COM 		    ITASK_BEING_ABORTED)) {
166010725SJohn.Forte@Sun.COM 			continue;
166110725SJohn.Forte@Sun.COM 		}
166210725SJohn.Forte@Sun.COM 		if (itask->itask_proxy_msg_id == task_msgid) {
166310725SJohn.Forte@Sun.COM 			break;
166410725SJohn.Forte@Sun.COM 		}
166510725SJohn.Forte@Sun.COM 	}
166610725SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
166710725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
166810725SJohn.Forte@Sun.COM 
166910725SJohn.Forte@Sun.COM 	if (itask != NULL) {
167010725SJohn.Forte@Sun.COM 		return (itask->itask_task);
167110725SJohn.Forte@Sun.COM 	} else {
167210725SJohn.Forte@Sun.COM 		/* task not found. Likely already aborted. */
167310725SJohn.Forte@Sun.COM 		return (NULL);
167410725SJohn.Forte@Sun.COM 	}
167510725SJohn.Forte@Sun.COM }
167610725SJohn.Forte@Sun.COM 
167710725SJohn.Forte@Sun.COM /*
167810725SJohn.Forte@Sun.COM  * message received from pppt/ic
167910725SJohn.Forte@Sun.COM  */
168010725SJohn.Forte@Sun.COM stmf_status_t
stmf_msg_rx(stmf_ic_msg_t * msg)168110725SJohn.Forte@Sun.COM stmf_msg_rx(stmf_ic_msg_t *msg)
168210725SJohn.Forte@Sun.COM {
168310725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
168410725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state != 1) {
168510725SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
168610725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "stmf alua state is disabled");
168710725SJohn.Forte@Sun.COM 		ic_msg_free(msg);
168810725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
168910725SJohn.Forte@Sun.COM 	}
169010725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
169110725SJohn.Forte@Sun.COM 
169210725SJohn.Forte@Sun.COM 	switch (msg->icm_msg_type) {
169310725SJohn.Forte@Sun.COM 		case STMF_ICM_REGISTER_LUN:
169410725SJohn.Forte@Sun.COM 			(void) stmf_ic_lu_reg(
169510725SJohn.Forte@Sun.COM 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
169610725SJohn.Forte@Sun.COM 			    STMF_MSG_LU_REGISTER);
169710725SJohn.Forte@Sun.COM 			break;
169810725SJohn.Forte@Sun.COM 		case STMF_ICM_LUN_ACTIVE:
169910725SJohn.Forte@Sun.COM 			(void) stmf_ic_lu_reg(
170010725SJohn.Forte@Sun.COM 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
170110725SJohn.Forte@Sun.COM 			    STMF_MSG_LU_ACTIVE);
170210725SJohn.Forte@Sun.COM 			break;
170310725SJohn.Forte@Sun.COM 		case STMF_ICM_DEREGISTER_LUN:
170410725SJohn.Forte@Sun.COM 			(void) stmf_ic_lu_dereg(
170510725SJohn.Forte@Sun.COM 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg);
170610725SJohn.Forte@Sun.COM 			break;
170710725SJohn.Forte@Sun.COM 		case STMF_ICM_SCSI_DATA:
170810725SJohn.Forte@Sun.COM 			(void) stmf_ic_rx_scsi_data(
170910725SJohn.Forte@Sun.COM 			    (stmf_ic_scsi_data_msg_t *)msg->icm_msg);
171010725SJohn.Forte@Sun.COM 			break;
171110725SJohn.Forte@Sun.COM 		case STMF_ICM_SCSI_STATUS:
171210725SJohn.Forte@Sun.COM 			(void) stmf_ic_rx_scsi_status(
171310725SJohn.Forte@Sun.COM 			    (stmf_ic_scsi_status_msg_t *)msg->icm_msg);
171410725SJohn.Forte@Sun.COM 			break;
171510725SJohn.Forte@Sun.COM 		case STMF_ICM_STATUS:
171610725SJohn.Forte@Sun.COM 			(void) stmf_ic_rx_status(
171710725SJohn.Forte@Sun.COM 			    (stmf_ic_status_msg_t *)msg->icm_msg);
171810725SJohn.Forte@Sun.COM 			break;
171910725SJohn.Forte@Sun.COM 		default:
172010725SJohn.Forte@Sun.COM 			cmn_err(CE_WARN, "unknown message received %d",
172110725SJohn.Forte@Sun.COM 			    msg->icm_msg_type);
172210725SJohn.Forte@Sun.COM 			ic_msg_free(msg);
172310725SJohn.Forte@Sun.COM 			return (STMF_FAILURE);
172410725SJohn.Forte@Sun.COM 	}
172510725SJohn.Forte@Sun.COM 	ic_msg_free(msg);
172610725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
172710725SJohn.Forte@Sun.COM }
172810725SJohn.Forte@Sun.COM 
172910725SJohn.Forte@Sun.COM stmf_status_t
stmf_ic_rx_status(stmf_ic_status_msg_t * msg)173010725SJohn.Forte@Sun.COM stmf_ic_rx_status(stmf_ic_status_msg_t *msg)
173110725SJohn.Forte@Sun.COM {
173210725SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
173310725SJohn.Forte@Sun.COM 
173410725SJohn.Forte@Sun.COM 	if (msg->ics_msg_type != STMF_ICM_REGISTER_PROXY_PORT) {
173510725SJohn.Forte@Sun.COM 		/* for now, ignore other message status */
173610725SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
173710725SJohn.Forte@Sun.COM 	}
173810725SJohn.Forte@Sun.COM 
173910725SJohn.Forte@Sun.COM 	if (msg->ics_status != STMF_SUCCESS) {
174010725SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
174110725SJohn.Forte@Sun.COM 	}
174210725SJohn.Forte@Sun.COM 
174310725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
174410725SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
174510725SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
174610725SJohn.Forte@Sun.COM 		if (msg->ics_msgid == ilport->ilport_reg_msgid) {
174710725SJohn.Forte@Sun.COM 			ilport->ilport_proxy_registered = 1;
174810725SJohn.Forte@Sun.COM 			break;
174910725SJohn.Forte@Sun.COM 		}
175010725SJohn.Forte@Sun.COM 	}
175110725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
175210725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
175310725SJohn.Forte@Sun.COM }
175410725SJohn.Forte@Sun.COM 
175510725SJohn.Forte@Sun.COM /*
175610725SJohn.Forte@Sun.COM  * handles scsi status message from pppt
175710725SJohn.Forte@Sun.COM  */
175810725SJohn.Forte@Sun.COM stmf_status_t
stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t * msg)175910725SJohn.Forte@Sun.COM stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg)
176010725SJohn.Forte@Sun.COM {
176110725SJohn.Forte@Sun.COM 	scsi_task_t *task;
176210725SJohn.Forte@Sun.COM 
176311096SJohn.Forte@Sun.COM 	/* is this a task management command */
176411096SJohn.Forte@Sun.COM 	if (msg->icss_task_msgid & MSG_ID_TM_BIT) {
176511096SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
176611096SJohn.Forte@Sun.COM 	}
176711096SJohn.Forte@Sun.COM 
176810725SJohn.Forte@Sun.COM 	task = find_task_from_msgid(msg->icss_lun_id, msg->icss_task_msgid);
176910725SJohn.Forte@Sun.COM 
177010725SJohn.Forte@Sun.COM 	if (task == NULL) {
177110725SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
177210725SJohn.Forte@Sun.COM 	}
177310725SJohn.Forte@Sun.COM 
177410725SJohn.Forte@Sun.COM 	task->task_scsi_status = msg->icss_status;
177510725SJohn.Forte@Sun.COM 	task->task_sense_data = msg->icss_sense;
177610725SJohn.Forte@Sun.COM 	task->task_sense_length = msg->icss_sense_len;
177710725SJohn.Forte@Sun.COM 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
177810725SJohn.Forte@Sun.COM 
177910725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
178010725SJohn.Forte@Sun.COM }
178110725SJohn.Forte@Sun.COM 
178210725SJohn.Forte@Sun.COM /*
178310725SJohn.Forte@Sun.COM  * handles scsi data message from pppt
178410725SJohn.Forte@Sun.COM  */
178510725SJohn.Forte@Sun.COM stmf_status_t
stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t * msg)178610725SJohn.Forte@Sun.COM stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg)
178710725SJohn.Forte@Sun.COM {
178810725SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
178910725SJohn.Forte@Sun.COM 	scsi_task_t *task;
179010725SJohn.Forte@Sun.COM 	stmf_xfer_data_t *xd = NULL;
179110725SJohn.Forte@Sun.COM 	stmf_data_buf_t *dbuf;
179210725SJohn.Forte@Sun.COM 	uint32_t sz, minsz, xd_sz, asz;
179310725SJohn.Forte@Sun.COM 
179411096SJohn.Forte@Sun.COM 	/* is this a task management command */
179511096SJohn.Forte@Sun.COM 	if (msg->icsd_task_msgid & MSG_ID_TM_BIT) {
179611096SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
179711096SJohn.Forte@Sun.COM 	}
179811096SJohn.Forte@Sun.COM 
179910725SJohn.Forte@Sun.COM 	task = find_task_from_msgid(msg->icsd_lun_id, msg->icsd_task_msgid);
180010725SJohn.Forte@Sun.COM 	if (task == NULL) {
180110725SJohn.Forte@Sun.COM 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
180210725SJohn.Forte@Sun.COM 		static uint64_t data_msg_id;
180310725SJohn.Forte@Sun.COM 		stmf_status_t ic_ret = STMF_FAILURE;
180410725SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
180510725SJohn.Forte@Sun.COM 		data_msg_id = stmf_proxy_msg_id++;
180610725SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
180710725SJohn.Forte@Sun.COM 		/*
180810725SJohn.Forte@Sun.COM 		 * send xfer done status to pppt
180910725SJohn.Forte@Sun.COM 		 * for now, set the session id to 0 as we cannot
181010725SJohn.Forte@Sun.COM 		 * ascertain it since we cannot find the task
181110725SJohn.Forte@Sun.COM 		 */
181210725SJohn.Forte@Sun.COM 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
181310725SJohn.Forte@Sun.COM 		    msg->icsd_task_msgid, 0, STMF_FAILURE, data_msg_id);
181410725SJohn.Forte@Sun.COM 		if (ic_xfer_done_msg) {
181510725SJohn.Forte@Sun.COM 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
181610725SJohn.Forte@Sun.COM 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
181710725SJohn.Forte@Sun.COM 				cmn_err(CE_WARN, "unable to xmit proxy msg");
181810725SJohn.Forte@Sun.COM 			}
181910725SJohn.Forte@Sun.COM 		}
182010725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
182110725SJohn.Forte@Sun.COM 	}
182210725SJohn.Forte@Sun.COM 
182310725SJohn.Forte@Sun.COM 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
182410725SJohn.Forte@Sun.COM 	dbuf = itask->itask_proxy_dbuf;
182510725SJohn.Forte@Sun.COM 
182612340SJohn.Forte@Sun.COM 	task->task_cmd_xfer_length += msg->icsd_data_len;
182710725SJohn.Forte@Sun.COM 
182810725SJohn.Forte@Sun.COM 	if (task->task_additional_flags &
182910725SJohn.Forte@Sun.COM 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
183010725SJohn.Forte@Sun.COM 		task->task_expected_xfer_length =
183110725SJohn.Forte@Sun.COM 		    task->task_cmd_xfer_length;
183210725SJohn.Forte@Sun.COM 	}
183310725SJohn.Forte@Sun.COM 
183410725SJohn.Forte@Sun.COM 	sz = min(task->task_expected_xfer_length,
183510725SJohn.Forte@Sun.COM 	    task->task_cmd_xfer_length);
183610725SJohn.Forte@Sun.COM 
183710725SJohn.Forte@Sun.COM 	xd_sz = msg->icsd_data_len;
183810725SJohn.Forte@Sun.COM 	asz = xd_sz + sizeof (*xd) - 4;
183910725SJohn.Forte@Sun.COM 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
184010725SJohn.Forte@Sun.COM 
184110725SJohn.Forte@Sun.COM 	if (xd == NULL) {
184210725SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
184310725SJohn.Forte@Sun.COM 		    STMF_ALLOC_FAILURE, NULL);
184410725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
184510725SJohn.Forte@Sun.COM 	}
184610725SJohn.Forte@Sun.COM 
184710725SJohn.Forte@Sun.COM 	xd->alloc_size = asz;
184810725SJohn.Forte@Sun.COM 	xd->size_left = xd_sz;
184910725SJohn.Forte@Sun.COM 	bcopy(msg->icsd_data, xd->buf, xd_sz);
185010725SJohn.Forte@Sun.COM 
185110725SJohn.Forte@Sun.COM 	sz = min(sz, xd->size_left);
185210725SJohn.Forte@Sun.COM 	xd->size_left = sz;
185310725SJohn.Forte@Sun.COM 	minsz = min(512, sz);
185410725SJohn.Forte@Sun.COM 
185510725SJohn.Forte@Sun.COM 	if (dbuf == NULL)
185610725SJohn.Forte@Sun.COM 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
185710725SJohn.Forte@Sun.COM 	if (dbuf == NULL) {
185810725SJohn.Forte@Sun.COM 		kmem_free(xd, xd->alloc_size);
185910725SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
186010725SJohn.Forte@Sun.COM 		    STMF_ALLOC_FAILURE, NULL);
186110725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
186210725SJohn.Forte@Sun.COM 	}
186310725SJohn.Forte@Sun.COM 	dbuf->db_lu_private = xd;
186412340SJohn.Forte@Sun.COM 	dbuf->db_relative_offset = task->task_nbytes_transferred;
186512340SJohn.Forte@Sun.COM 	stmf_xd_to_dbuf(dbuf, 0);
186610725SJohn.Forte@Sun.COM 
186710725SJohn.Forte@Sun.COM 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
186810725SJohn.Forte@Sun.COM 	(void) stmf_xfer_data(task, dbuf, 0);
186910725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
187010725SJohn.Forte@Sun.COM }
187110725SJohn.Forte@Sun.COM 
187210725SJohn.Forte@Sun.COM stmf_status_t
stmf_proxy_scsi_cmd(scsi_task_t * task,stmf_data_buf_t * dbuf)187310725SJohn.Forte@Sun.COM stmf_proxy_scsi_cmd(scsi_task_t *task, stmf_data_buf_t *dbuf)
187410725SJohn.Forte@Sun.COM {
187510725SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
187610725SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
187710725SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport =
187810725SJohn.Forte@Sun.COM 	    (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
187910725SJohn.Forte@Sun.COM 	stmf_ic_msg_t *ic_cmd_msg;
188010725SJohn.Forte@Sun.COM 	stmf_ic_msg_status_t ic_ret;
188110725SJohn.Forte@Sun.COM 	stmf_status_t ret = STMF_FAILURE;
188210725SJohn.Forte@Sun.COM 
188310725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state != 1) {
188410725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "stmf alua state is disabled");
188510725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
188610725SJohn.Forte@Sun.COM 	}
188710725SJohn.Forte@Sun.COM 
188810878SJohn.Forte@Sun.COM 	if (ilport->ilport_proxy_registered == 0) {
188910878SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
189010878SJohn.Forte@Sun.COM 	}
189110878SJohn.Forte@Sun.COM 
189210725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
189310725SJohn.Forte@Sun.COM 	itask->itask_proxy_msg_id = stmf_proxy_msg_id++;
189410725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
189510725SJohn.Forte@Sun.COM 	itask->itask_proxy_dbuf = dbuf;
189610878SJohn.Forte@Sun.COM 
189710878SJohn.Forte@Sun.COM 	/*
189810878SJohn.Forte@Sun.COM 	 * stmf will now take over the task handling for this task
189910878SJohn.Forte@Sun.COM 	 * but it still needs to be treated differently from other
190011096SJohn.Forte@Sun.COM 	 * default handled tasks, hence the ITASK_PROXY_TASK.
190111096SJohn.Forte@Sun.COM 	 * If this is a task management function, we're really just
190211096SJohn.Forte@Sun.COM 	 * duping the command to the peer. Set the TM bit so that
190311096SJohn.Forte@Sun.COM 	 * we can recognize this on return since we won't be completing
190411096SJohn.Forte@Sun.COM 	 * the proxied task in that case.
190510878SJohn.Forte@Sun.COM 	 */
190611096SJohn.Forte@Sun.COM 	if (task->task_mgmt_function) {
190711096SJohn.Forte@Sun.COM 		itask->itask_proxy_msg_id |= MSG_ID_TM_BIT;
190811096SJohn.Forte@Sun.COM 	} else {
190912404SJohn.Forte@Sun.COM 		uint32_t new, old;
191012404SJohn.Forte@Sun.COM 		do {
191112404SJohn.Forte@Sun.COM 			new = old = itask->itask_flags;
191212404SJohn.Forte@Sun.COM 			if (new & ITASK_BEING_ABORTED)
191312404SJohn.Forte@Sun.COM 				return (STMF_FAILURE);
191412404SJohn.Forte@Sun.COM 			new |= ITASK_DEFAULT_HANDLING | ITASK_PROXY_TASK;
191512404SJohn.Forte@Sun.COM 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
191611096SJohn.Forte@Sun.COM 	}
191710725SJohn.Forte@Sun.COM 	if (dbuf) {
191810725SJohn.Forte@Sun.COM 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
191910725SJohn.Forte@Sun.COM 		    task, dbuf->db_data_size, dbuf->db_sglist[0].seg_addr,
192010725SJohn.Forte@Sun.COM 		    itask->itask_proxy_msg_id);
192110725SJohn.Forte@Sun.COM 	} else {
192210725SJohn.Forte@Sun.COM 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
192310725SJohn.Forte@Sun.COM 		    task, 0, NULL, itask->itask_proxy_msg_id);
192410725SJohn.Forte@Sun.COM 	}
192510725SJohn.Forte@Sun.COM 	if (ic_cmd_msg) {
192610725SJohn.Forte@Sun.COM 		ic_ret = ic_tx_msg(ic_cmd_msg);
192710725SJohn.Forte@Sun.COM 		if (ic_ret == STMF_IC_MSG_SUCCESS) {
192810725SJohn.Forte@Sun.COM 			ret = STMF_SUCCESS;
192910725SJohn.Forte@Sun.COM 		}
193010725SJohn.Forte@Sun.COM 	}
193110725SJohn.Forte@Sun.COM 	return (ret);
193210725SJohn.Forte@Sun.COM }
193310725SJohn.Forte@Sun.COM 
193410725SJohn.Forte@Sun.COM 
193510725SJohn.Forte@Sun.COM stmf_status_t
pppt_modload()193610725SJohn.Forte@Sun.COM pppt_modload()
193710725SJohn.Forte@Sun.COM {
193810725SJohn.Forte@Sun.COM 	int error;
193910725SJohn.Forte@Sun.COM 
194010725SJohn.Forte@Sun.COM 	if (pppt_mod == NULL && ((pppt_mod =
194110725SJohn.Forte@Sun.COM 	    ddi_modopen("drv/pppt", KRTLD_MODE_FIRST, &error)) == NULL)) {
194210725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "Unable to load pppt");
194310725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
194410725SJohn.Forte@Sun.COM 	}
194510725SJohn.Forte@Sun.COM 
194610725SJohn.Forte@Sun.COM 	if (ic_reg_port_msg_alloc == NULL && ((ic_reg_port_msg_alloc =
194710725SJohn.Forte@Sun.COM 	    (stmf_ic_reg_port_msg_alloc_func_t)
194810725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_reg_port_msg_alloc",
194910725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
195010725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
195110725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_reg_port_msg_alloc");
195210725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
195310725SJohn.Forte@Sun.COM 	}
195410725SJohn.Forte@Sun.COM 
195510725SJohn.Forte@Sun.COM 
195610725SJohn.Forte@Sun.COM 	if (ic_dereg_port_msg_alloc == NULL && ((ic_dereg_port_msg_alloc =
195710725SJohn.Forte@Sun.COM 	    (stmf_ic_dereg_port_msg_alloc_func_t)
195810725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_port_msg_alloc",
195910725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
196010725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
196110725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_dereg_port_msg_alloc");
196210725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
196310725SJohn.Forte@Sun.COM 	}
196410725SJohn.Forte@Sun.COM 
196510725SJohn.Forte@Sun.COM 	if (ic_reg_lun_msg_alloc == NULL && ((ic_reg_lun_msg_alloc =
196610725SJohn.Forte@Sun.COM 	    (stmf_ic_reg_lun_msg_alloc_func_t)
196710725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_reg_lun_msg_alloc",
196810725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
196910725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
197010725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_reg_lun_msg_alloc");
197110725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
197210725SJohn.Forte@Sun.COM 	}
197310725SJohn.Forte@Sun.COM 
197410725SJohn.Forte@Sun.COM 	if (ic_lun_active_msg_alloc == NULL && ((ic_lun_active_msg_alloc =
197510725SJohn.Forte@Sun.COM 	    (stmf_ic_lun_active_msg_alloc_func_t)
197610725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_lun_active_msg_alloc",
197710725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
197810725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
197910725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_lun_active_msg_alloc");
198010725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
198110725SJohn.Forte@Sun.COM 	}
198210725SJohn.Forte@Sun.COM 
198310725SJohn.Forte@Sun.COM 	if (ic_dereg_lun_msg_alloc == NULL && ((ic_dereg_lun_msg_alloc =
198410725SJohn.Forte@Sun.COM 	    (stmf_ic_dereg_lun_msg_alloc_func_t)
198510725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_lun_msg_alloc",
198610725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
198710725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
198810725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_dereg_lun_msg_alloc");
198910725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
199010725SJohn.Forte@Sun.COM 	}
199110725SJohn.Forte@Sun.COM 
199210725SJohn.Forte@Sun.COM 	if (ic_scsi_cmd_msg_alloc == NULL && ((ic_scsi_cmd_msg_alloc =
199310725SJohn.Forte@Sun.COM 	    (stmf_ic_scsi_cmd_msg_alloc_func_t)
199410725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_cmd_msg_alloc",
199510725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
199610725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
199710725SJohn.Forte@Sun.COM 		    "Unable to find symbol - stmf_ic_scsi_cmd_msg_alloc");
199810725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
199910725SJohn.Forte@Sun.COM 	}
200010725SJohn.Forte@Sun.COM 
200110725SJohn.Forte@Sun.COM 	if (ic_scsi_data_xfer_done_msg_alloc == NULL &&
200210725SJohn.Forte@Sun.COM 	    ((ic_scsi_data_xfer_done_msg_alloc =
200310725SJohn.Forte@Sun.COM 	    (stmf_ic_scsi_data_xfer_done_msg_alloc_func_t)
200410725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_data_xfer_done_msg_alloc",
200510725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
200610725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
200710725SJohn.Forte@Sun.COM 		    "Unable to find symbol -"
200810725SJohn.Forte@Sun.COM 		    "stmf_ic_scsi_data_xfer_done_msg_alloc");
200910725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
201010725SJohn.Forte@Sun.COM 	}
201110725SJohn.Forte@Sun.COM 
201210725SJohn.Forte@Sun.COM 	if (ic_session_reg_msg_alloc == NULL &&
201310725SJohn.Forte@Sun.COM 	    ((ic_session_reg_msg_alloc =
201410725SJohn.Forte@Sun.COM 	    (stmf_ic_session_create_msg_alloc_func_t)
201510725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_session_create_msg_alloc",
201610725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
201710725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
201810725SJohn.Forte@Sun.COM 		    "Unable to find symbol -"
201910725SJohn.Forte@Sun.COM 		    "stmf_ic_session_create_msg_alloc");
202010725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
202110725SJohn.Forte@Sun.COM 	}
202210725SJohn.Forte@Sun.COM 
202310725SJohn.Forte@Sun.COM 	if (ic_session_dereg_msg_alloc == NULL &&
202410725SJohn.Forte@Sun.COM 	    ((ic_session_dereg_msg_alloc =
202510725SJohn.Forte@Sun.COM 	    (stmf_ic_session_destroy_msg_alloc_func_t)
202610725SJohn.Forte@Sun.COM 	    ddi_modsym(pppt_mod, "stmf_ic_session_destroy_msg_alloc",
202710725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
202810725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN,
202910725SJohn.Forte@Sun.COM 		    "Unable to find symbol -"
203010725SJohn.Forte@Sun.COM 		    "stmf_ic_session_destroy_msg_alloc");
203110725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
203210725SJohn.Forte@Sun.COM 	}
203310725SJohn.Forte@Sun.COM 
203410725SJohn.Forte@Sun.COM 	if (ic_tx_msg == NULL && ((ic_tx_msg =
203510725SJohn.Forte@Sun.COM 	    (stmf_ic_tx_msg_func_t)ddi_modsym(pppt_mod, "stmf_ic_tx_msg",
203610725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
203710725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_tx_msg");
203810725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
203910725SJohn.Forte@Sun.COM 	}
204010725SJohn.Forte@Sun.COM 
204110725SJohn.Forte@Sun.COM 	if (ic_msg_free == NULL && ((ic_msg_free =
204210725SJohn.Forte@Sun.COM 	    (stmf_ic_msg_free_func_t)ddi_modsym(pppt_mod, "stmf_ic_msg_free",
204310725SJohn.Forte@Sun.COM 	    &error)) == NULL)) {
204410725SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_msg_free");
204510725SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
204610725SJohn.Forte@Sun.COM 	}
204710725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
204810725SJohn.Forte@Sun.COM }
204910725SJohn.Forte@Sun.COM 
205010725SJohn.Forte@Sun.COM static void
stmf_get_alua_state(stmf_alua_state_desc_t * alua_state)205110725SJohn.Forte@Sun.COM stmf_get_alua_state(stmf_alua_state_desc_t *alua_state)
205210725SJohn.Forte@Sun.COM {
205310725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
205410725SJohn.Forte@Sun.COM 	alua_state->alua_node = stmf_state.stmf_alua_node;
205510725SJohn.Forte@Sun.COM 	alua_state->alua_state = stmf_state.stmf_alua_state;
205610725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
205710725SJohn.Forte@Sun.COM }
205810725SJohn.Forte@Sun.COM 
205910725SJohn.Forte@Sun.COM 
206010725SJohn.Forte@Sun.COM static int
stmf_set_alua_state(stmf_alua_state_desc_t * alua_state)206110725SJohn.Forte@Sun.COM stmf_set_alua_state(stmf_alua_state_desc_t *alua_state)
206210725SJohn.Forte@Sun.COM {
206310725SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
206410725SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
206510725SJohn.Forte@Sun.COM 	stmf_lu_t *lu;
206610725SJohn.Forte@Sun.COM 	stmf_ic_msg_status_t ic_ret;
206710725SJohn.Forte@Sun.COM 	stmf_ic_msg_t *ic_reg_lun, *ic_reg_port;
206810725SJohn.Forte@Sun.COM 	stmf_local_port_t *lport;
206910725SJohn.Forte@Sun.COM 	int ret = 0;
207010725SJohn.Forte@Sun.COM 
207110725SJohn.Forte@Sun.COM 	if (alua_state->alua_state > 1 || alua_state->alua_node > 1) {
207210725SJohn.Forte@Sun.COM 		return (EINVAL);
207310725SJohn.Forte@Sun.COM 	}
207410725SJohn.Forte@Sun.COM 
207510725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
207610725SJohn.Forte@Sun.COM 	if (alua_state->alua_state == 1) {
207710725SJohn.Forte@Sun.COM 		if (pppt_modload() == STMF_FAILURE) {
207810725SJohn.Forte@Sun.COM 			ret = EIO;
207910725SJohn.Forte@Sun.COM 			goto err;
208010725SJohn.Forte@Sun.COM 		}
208110725SJohn.Forte@Sun.COM 		if (alua_state->alua_node != 0) {
208210725SJohn.Forte@Sun.COM 			/* reset existing rtpids to new base */
208310725SJohn.Forte@Sun.COM 			stmf_rtpid_counter = 255;
208410725SJohn.Forte@Sun.COM 		}
208510725SJohn.Forte@Sun.COM 		stmf_state.stmf_alua_node = alua_state->alua_node;
208610725SJohn.Forte@Sun.COM 		stmf_state.stmf_alua_state = 1;
208710725SJohn.Forte@Sun.COM 		/* register existing local ports with ppp */
208810725SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
208910725SJohn.Forte@Sun.COM 		    ilport = ilport->ilport_next) {
209011116SJohn.Forte@Sun.COM 			/* skip standby ports and non-alua participants */
209111116SJohn.Forte@Sun.COM 			if (ilport->ilport_standby == 1 ||
209211116SJohn.Forte@Sun.COM 			    ilport->ilport_alua == 0) {
209310725SJohn.Forte@Sun.COM 				continue;
209410725SJohn.Forte@Sun.COM 			}
209510725SJohn.Forte@Sun.COM 			if (alua_state->alua_node != 0) {
209610725SJohn.Forte@Sun.COM 				ilport->ilport_rtpid =
209710725SJohn.Forte@Sun.COM 				    atomic_add_16_nv(&stmf_rtpid_counter, 1);
209810725SJohn.Forte@Sun.COM 			}
209910725SJohn.Forte@Sun.COM 			lport = ilport->ilport_lport;
210010725SJohn.Forte@Sun.COM 			ic_reg_port = ic_reg_port_msg_alloc(
210110725SJohn.Forte@Sun.COM 			    lport->lport_id, ilport->ilport_rtpid,
210210725SJohn.Forte@Sun.COM 			    0, NULL, stmf_proxy_msg_id);
210310725SJohn.Forte@Sun.COM 			if (ic_reg_port) {
210410725SJohn.Forte@Sun.COM 				ic_ret = ic_tx_msg(ic_reg_port);
210510725SJohn.Forte@Sun.COM 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
210610725SJohn.Forte@Sun.COM 					ilport->ilport_reg_msgid =
210710725SJohn.Forte@Sun.COM 					    stmf_proxy_msg_id++;
210810725SJohn.Forte@Sun.COM 				} else {
210910725SJohn.Forte@Sun.COM 					cmn_err(CE_WARN,
211010725SJohn.Forte@Sun.COM 					    "error on port registration "
211110725SJohn.Forte@Sun.COM 					    "port - %s",
211210725SJohn.Forte@Sun.COM 					    ilport->ilport_kstat_tgt_name);
211310725SJohn.Forte@Sun.COM 				}
211410725SJohn.Forte@Sun.COM 			}
211510725SJohn.Forte@Sun.COM 		}
211610725SJohn.Forte@Sun.COM 		/* register existing logical units */
211710725SJohn.Forte@Sun.COM 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
211810725SJohn.Forte@Sun.COM 		    ilu = ilu->ilu_next) {
211910725SJohn.Forte@Sun.COM 			if (ilu->ilu_access != STMF_LU_ACTIVE) {
212010725SJohn.Forte@Sun.COM 				continue;
212110725SJohn.Forte@Sun.COM 			}
212210725SJohn.Forte@Sun.COM 			/* register with proxy module */
212310725SJohn.Forte@Sun.COM 			lu = ilu->ilu_lu;
212410725SJohn.Forte@Sun.COM 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
212510725SJohn.Forte@Sun.COM 			    lu->lu_lp->lp_alua_support) {
212610725SJohn.Forte@Sun.COM 				ilu->ilu_alua = 1;
212710725SJohn.Forte@Sun.COM 				/* allocate the register message */
212810725SJohn.Forte@Sun.COM 				ic_reg_lun = ic_reg_lun_msg_alloc(
212910725SJohn.Forte@Sun.COM 				    lu->lu_id->ident, lu->lu_lp->lp_name,
213010725SJohn.Forte@Sun.COM 				    lu->lu_proxy_reg_arg_len,
213110725SJohn.Forte@Sun.COM 				    (uint8_t *)lu->lu_proxy_reg_arg,
213210725SJohn.Forte@Sun.COM 				    stmf_proxy_msg_id);
213310725SJohn.Forte@Sun.COM 				/* send the message */
213410725SJohn.Forte@Sun.COM 				if (ic_reg_lun) {
213510725SJohn.Forte@Sun.COM 					ic_ret = ic_tx_msg(ic_reg_lun);
213610725SJohn.Forte@Sun.COM 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
213710725SJohn.Forte@Sun.COM 						stmf_proxy_msg_id++;
213810725SJohn.Forte@Sun.COM 					}
213910725SJohn.Forte@Sun.COM 				}
214010725SJohn.Forte@Sun.COM 			}
214110725SJohn.Forte@Sun.COM 		}
214210725SJohn.Forte@Sun.COM 	} else {
214310725SJohn.Forte@Sun.COM 		stmf_state.stmf_alua_state = 0;
214410725SJohn.Forte@Sun.COM 	}
214510725SJohn.Forte@Sun.COM 
214610725SJohn.Forte@Sun.COM err:
214710725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
214810725SJohn.Forte@Sun.COM 	return (ret);
214910725SJohn.Forte@Sun.COM }
215010725SJohn.Forte@Sun.COM 
215110725SJohn.Forte@Sun.COM 
21527836SJohn.Forte@Sun.COM typedef struct {
21537836SJohn.Forte@Sun.COM 	void	*bp;	/* back pointer from internal struct to main struct */
21547836SJohn.Forte@Sun.COM 	int	alloc_size;
21557836SJohn.Forte@Sun.COM } __istmf_t;
21567836SJohn.Forte@Sun.COM 
21577836SJohn.Forte@Sun.COM typedef struct {
21587836SJohn.Forte@Sun.COM 	__istmf_t	*fp;	/* Framework private */
21597836SJohn.Forte@Sun.COM 	void		*cp;	/* Caller private */
21607836SJohn.Forte@Sun.COM 	void		*ss;	/* struct specific */
21617836SJohn.Forte@Sun.COM } __stmf_t;
21627836SJohn.Forte@Sun.COM 
21637836SJohn.Forte@Sun.COM static struct {
21647836SJohn.Forte@Sun.COM 	int shared;
21657836SJohn.Forte@Sun.COM 	int fw_private;
21667836SJohn.Forte@Sun.COM } stmf_sizes[] = { { 0, 0 },
21677836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_lu_provider_t),
21687836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_lu_provider_t) },
21697836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_port_provider_t),
21707836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_port_provider_t) },
21717836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_local_port_t),
21727836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_local_port_t) },
21737836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_lu_t),
21747836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_lu_t) },
21757836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_scsi_session_t),
21767836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_scsi_session_t) },
21777836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(scsi_task_t),
21787836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(stmf_i_scsi_task_t) },
21797836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_data_buf_t),
21807836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(__istmf_t) },
21817836SJohn.Forte@Sun.COM 	{ GET_STRUCT_SIZE(stmf_dbuf_store_t),
21827836SJohn.Forte@Sun.COM 		GET_STRUCT_SIZE(__istmf_t) }
21837836SJohn.Forte@Sun.COM 
21847836SJohn.Forte@Sun.COM };
21857836SJohn.Forte@Sun.COM 
21867836SJohn.Forte@Sun.COM void *
stmf_alloc(stmf_struct_id_t struct_id,int additional_size,int flags)21877836SJohn.Forte@Sun.COM stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags)
21887836SJohn.Forte@Sun.COM {
21897836SJohn.Forte@Sun.COM 	int stmf_size;
21907836SJohn.Forte@Sun.COM 	int kmem_flag;
21917836SJohn.Forte@Sun.COM 	__stmf_t *sh;
21927836SJohn.Forte@Sun.COM 
21937836SJohn.Forte@Sun.COM 	if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS))
21947836SJohn.Forte@Sun.COM 		return (NULL);
21957836SJohn.Forte@Sun.COM 
21967836SJohn.Forte@Sun.COM 	if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
21977836SJohn.Forte@Sun.COM 		kmem_flag = KM_NOSLEEP;
21987836SJohn.Forte@Sun.COM 	} else {
21997836SJohn.Forte@Sun.COM 		kmem_flag = KM_SLEEP;
22007836SJohn.Forte@Sun.COM 	}
22017836SJohn.Forte@Sun.COM 
22027836SJohn.Forte@Sun.COM 	additional_size = (additional_size + 7) & (~7);
22037836SJohn.Forte@Sun.COM 	stmf_size = stmf_sizes[struct_id].shared +
22048662SJordan.Vaughan@Sun.com 	    stmf_sizes[struct_id].fw_private + additional_size;
22057836SJohn.Forte@Sun.COM 
220612314SJames.Moore@Sun.COM 	if (flags & AF_DONTZERO)
220712314SJames.Moore@Sun.COM 		sh = (__stmf_t *)kmem_alloc(stmf_size, kmem_flag);
220812314SJames.Moore@Sun.COM 	else
220912314SJames.Moore@Sun.COM 		sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag);
22107836SJohn.Forte@Sun.COM 
22117836SJohn.Forte@Sun.COM 	if (sh == NULL)
22127836SJohn.Forte@Sun.COM 		return (NULL);
22137836SJohn.Forte@Sun.COM 
22149087SZhong.Wang@Sun.COM 	/*
22159087SZhong.Wang@Sun.COM 	 * In principle, the implementation inside stmf_alloc should not
22169087SZhong.Wang@Sun.COM 	 * be changed anyway. But the original order of framework private
22179087SZhong.Wang@Sun.COM 	 * data and caller private data does not support sglist in the caller
22189087SZhong.Wang@Sun.COM 	 * private data.
22199087SZhong.Wang@Sun.COM 	 * To work around this, the memory segments of framework private
22209087SZhong.Wang@Sun.COM 	 * data and caller private data are re-ordered here.
22219087SZhong.Wang@Sun.COM 	 * A better solution is to provide a specific interface to allocate
22229087SZhong.Wang@Sun.COM 	 * the sglist, then we will not need this workaround any more.
22239087SZhong.Wang@Sun.COM 	 * But before the new interface is available, the memory segment
22249087SZhong.Wang@Sun.COM 	 * ordering should be kept as is.
22259087SZhong.Wang@Sun.COM 	 */
22269087SZhong.Wang@Sun.COM 	sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
22279087SZhong.Wang@Sun.COM 	sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh,
22289087SZhong.Wang@Sun.COM 	    stmf_sizes[struct_id].shared + additional_size);
22297836SJohn.Forte@Sun.COM 
22307836SJohn.Forte@Sun.COM 	sh->fp->bp = sh;
22317836SJohn.Forte@Sun.COM 	/* Just store the total size instead of storing additional size */
22327836SJohn.Forte@Sun.COM 	sh->fp->alloc_size = stmf_size;
22337836SJohn.Forte@Sun.COM 
22347836SJohn.Forte@Sun.COM 	return (sh);
22357836SJohn.Forte@Sun.COM }
22367836SJohn.Forte@Sun.COM 
22377836SJohn.Forte@Sun.COM void
stmf_free(void * ptr)22387836SJohn.Forte@Sun.COM stmf_free(void *ptr)
22397836SJohn.Forte@Sun.COM {
22407836SJohn.Forte@Sun.COM 	__stmf_t *sh = (__stmf_t *)ptr;
22417836SJohn.Forte@Sun.COM 
22427836SJohn.Forte@Sun.COM 	/*
22437836SJohn.Forte@Sun.COM 	 * So far we dont need any struct specific processing. If such
22447836SJohn.Forte@Sun.COM 	 * a need ever arises, then store the struct id in the framework
22457836SJohn.Forte@Sun.COM 	 * private section and get it here as sh->fp->struct_id.
22467836SJohn.Forte@Sun.COM 	 */
22477836SJohn.Forte@Sun.COM 	kmem_free(ptr, sh->fp->alloc_size);
22487836SJohn.Forte@Sun.COM }
22497836SJohn.Forte@Sun.COM 
22507836SJohn.Forte@Sun.COM /*
22517836SJohn.Forte@Sun.COM  * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
22527836SJohn.Forte@Sun.COM  * framework and returns a pointer to framework private data for the lu.
22537836SJohn.Forte@Sun.COM  * Returns NULL if the lu was not found.
22547836SJohn.Forte@Sun.COM  */
22557836SJohn.Forte@Sun.COM stmf_i_lu_t *
stmf_lookup_lu(stmf_lu_t * lu)22567836SJohn.Forte@Sun.COM stmf_lookup_lu(stmf_lu_t *lu)
22577836SJohn.Forte@Sun.COM {
22587836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
22597836SJohn.Forte@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
22607836SJohn.Forte@Sun.COM 
22617836SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
22627836SJohn.Forte@Sun.COM 		if (ilu->ilu_lu == lu)
22637836SJohn.Forte@Sun.COM 			return (ilu);
22647836SJohn.Forte@Sun.COM 	}
22657836SJohn.Forte@Sun.COM 	return (NULL);
22667836SJohn.Forte@Sun.COM }
22677836SJohn.Forte@Sun.COM 
22687836SJohn.Forte@Sun.COM /*
22698818STim.Szeto@Sun.COM  * Given a pointer to stmf_local_port_t, verifies if this lport is registered
22708818STim.Szeto@Sun.COM  * with the framework and returns a pointer to framework private data for
22718818STim.Szeto@Sun.COM  * the lport.
22728818STim.Szeto@Sun.COM  * Returns NULL if the lport was not found.
22737836SJohn.Forte@Sun.COM  */
22747836SJohn.Forte@Sun.COM stmf_i_local_port_t *
stmf_lookup_lport(stmf_local_port_t * lport)22757836SJohn.Forte@Sun.COM stmf_lookup_lport(stmf_local_port_t *lport)
22767836SJohn.Forte@Sun.COM {
22777836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
22787836SJohn.Forte@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
22797836SJohn.Forte@Sun.COM 
22807836SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
22817836SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
22827836SJohn.Forte@Sun.COM 		if (ilport->ilport_lport == lport)
22837836SJohn.Forte@Sun.COM 			return (ilport);
22847836SJohn.Forte@Sun.COM 	}
22857836SJohn.Forte@Sun.COM 	return (NULL);
22867836SJohn.Forte@Sun.COM }
22877836SJohn.Forte@Sun.COM 
22887836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_lu_provider(stmf_lu_provider_t * lp)22897836SJohn.Forte@Sun.COM stmf_register_lu_provider(stmf_lu_provider_t *lp)
22907836SJohn.Forte@Sun.COM {
22917836SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
22927836SJohn.Forte@Sun.COM 	stmf_pp_data_t *ppd;
22937836SJohn.Forte@Sun.COM 	uint32_t cb_flags;
22947836SJohn.Forte@Sun.COM 
229510725SJohn.Forte@Sun.COM 	if (lp->lp_lpif_rev != LPIF_REV_1 && lp->lp_lpif_rev != LPIF_REV_2)
22967836SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
22977836SJohn.Forte@Sun.COM 
22987836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
22997836SJohn.Forte@Sun.COM 	ilp->ilp_next = stmf_state.stmf_ilplist;
23007836SJohn.Forte@Sun.COM 	stmf_state.stmf_ilplist = ilp;
23017836SJohn.Forte@Sun.COM 	stmf_state.stmf_nlps++;
23027836SJohn.Forte@Sun.COM 
23037836SJohn.Forte@Sun.COM 	/* See if we need to do a callback */
23047836SJohn.Forte@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
23057836SJohn.Forte@Sun.COM 		if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
23067836SJohn.Forte@Sun.COM 			break;
23077836SJohn.Forte@Sun.COM 		}
23087836SJohn.Forte@Sun.COM 	}
23097836SJohn.Forte@Sun.COM 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
23107836SJohn.Forte@Sun.COM 		goto rlp_bail_out;
23117836SJohn.Forte@Sun.COM 	}
23127836SJohn.Forte@Sun.COM 	ilp->ilp_ppd = ppd;
23137836SJohn.Forte@Sun.COM 	ppd->ppd_provider = ilp;
23147836SJohn.Forte@Sun.COM 	if (lp->lp_cb == NULL)
23157836SJohn.Forte@Sun.COM 		goto rlp_bail_out;
23167836SJohn.Forte@Sun.COM 	ilp->ilp_cb_in_progress = 1;
23177836SJohn.Forte@Sun.COM 	cb_flags = STMF_PCB_PREG_COMPLETE;
23187836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
23197836SJohn.Forte@Sun.COM 		cb_flags |= STMF_PCB_STMF_ONLINING;
23207836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
23217836SJohn.Forte@Sun.COM 	lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
23227836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
23237836SJohn.Forte@Sun.COM 	ilp->ilp_cb_in_progress = 0;
23247836SJohn.Forte@Sun.COM 
23257836SJohn.Forte@Sun.COM rlp_bail_out:
23267836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
23277836SJohn.Forte@Sun.COM 
23287836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
23297836SJohn.Forte@Sun.COM }
23307836SJohn.Forte@Sun.COM 
23317836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_lu_provider(stmf_lu_provider_t * lp)23327836SJohn.Forte@Sun.COM stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
23337836SJohn.Forte@Sun.COM {
23347836SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t	**ppilp;
23357836SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
23367836SJohn.Forte@Sun.COM 
23377836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
23387836SJohn.Forte@Sun.COM 	if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
23397836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
23407836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
23417836SJohn.Forte@Sun.COM 	}
23427836SJohn.Forte@Sun.COM 	for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
23437836SJohn.Forte@Sun.COM 	    ppilp = &((*ppilp)->ilp_next)) {
23447836SJohn.Forte@Sun.COM 		if (*ppilp == ilp) {
23457836SJohn.Forte@Sun.COM 			*ppilp = ilp->ilp_next;
23467836SJohn.Forte@Sun.COM 			stmf_state.stmf_nlps--;
23477836SJohn.Forte@Sun.COM 			if (ilp->ilp_ppd) {
23487836SJohn.Forte@Sun.COM 				ilp->ilp_ppd->ppd_provider = NULL;
23497836SJohn.Forte@Sun.COM 				ilp->ilp_ppd = NULL;
23507836SJohn.Forte@Sun.COM 			}
23517836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
23527836SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
23537836SJohn.Forte@Sun.COM 		}
23547836SJohn.Forte@Sun.COM 	}
23557836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
23567836SJohn.Forte@Sun.COM 	return (STMF_NOT_FOUND);
23577836SJohn.Forte@Sun.COM }
23587836SJohn.Forte@Sun.COM 
23597836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_port_provider(stmf_port_provider_t * pp)23607836SJohn.Forte@Sun.COM stmf_register_port_provider(stmf_port_provider_t *pp)
23617836SJohn.Forte@Sun.COM {
23627836SJohn.Forte@Sun.COM 	stmf_i_port_provider_t *ipp =
23637836SJohn.Forte@Sun.COM 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
23647836SJohn.Forte@Sun.COM 	stmf_pp_data_t *ppd;
23657836SJohn.Forte@Sun.COM 	uint32_t cb_flags;
23667836SJohn.Forte@Sun.COM 
23677836SJohn.Forte@Sun.COM 	if (pp->pp_portif_rev != PORTIF_REV_1)
23687836SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
23697836SJohn.Forte@Sun.COM 
23707836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
23717836SJohn.Forte@Sun.COM 	ipp->ipp_next = stmf_state.stmf_ipplist;
23727836SJohn.Forte@Sun.COM 	stmf_state.stmf_ipplist = ipp;
23737836SJohn.Forte@Sun.COM 	stmf_state.stmf_npps++;
23747836SJohn.Forte@Sun.COM 	/* See if we need to do a callback */
23757836SJohn.Forte@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
23767836SJohn.Forte@Sun.COM 		if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
23777836SJohn.Forte@Sun.COM 			break;
23787836SJohn.Forte@Sun.COM 		}
23797836SJohn.Forte@Sun.COM 	}
23807836SJohn.Forte@Sun.COM 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
23817836SJohn.Forte@Sun.COM 		goto rpp_bail_out;
23827836SJohn.Forte@Sun.COM 	}
23837836SJohn.Forte@Sun.COM 	ipp->ipp_ppd = ppd;
23847836SJohn.Forte@Sun.COM 	ppd->ppd_provider = ipp;
23857836SJohn.Forte@Sun.COM 	if (pp->pp_cb == NULL)
23867836SJohn.Forte@Sun.COM 		goto rpp_bail_out;
23877836SJohn.Forte@Sun.COM 	ipp->ipp_cb_in_progress = 1;
23887836SJohn.Forte@Sun.COM 	cb_flags = STMF_PCB_PREG_COMPLETE;
23897836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
23907836SJohn.Forte@Sun.COM 		cb_flags |= STMF_PCB_STMF_ONLINING;
23917836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
23927836SJohn.Forte@Sun.COM 	pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
23937836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
23947836SJohn.Forte@Sun.COM 	ipp->ipp_cb_in_progress = 0;
23957836SJohn.Forte@Sun.COM 
23967836SJohn.Forte@Sun.COM rpp_bail_out:
23977836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
23987836SJohn.Forte@Sun.COM 
23997836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
24007836SJohn.Forte@Sun.COM }
24017836SJohn.Forte@Sun.COM 
24027836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_port_provider(stmf_port_provider_t * pp)24037836SJohn.Forte@Sun.COM stmf_deregister_port_provider(stmf_port_provider_t *pp)
24047836SJohn.Forte@Sun.COM {
24057836SJohn.Forte@Sun.COM 	stmf_i_port_provider_t *ipp =
24067836SJohn.Forte@Sun.COM 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
24077836SJohn.Forte@Sun.COM 	stmf_i_port_provider_t **ppipp;
24087836SJohn.Forte@Sun.COM 
24097836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
24107836SJohn.Forte@Sun.COM 	if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
24117836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
24127836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
24137836SJohn.Forte@Sun.COM 	}
24147836SJohn.Forte@Sun.COM 	for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
24157836SJohn.Forte@Sun.COM 	    ppipp = &((*ppipp)->ipp_next)) {
24167836SJohn.Forte@Sun.COM 		if (*ppipp == ipp) {
24177836SJohn.Forte@Sun.COM 			*ppipp = ipp->ipp_next;
24187836SJohn.Forte@Sun.COM 			stmf_state.stmf_npps--;
24197836SJohn.Forte@Sun.COM 			if (ipp->ipp_ppd) {
24207836SJohn.Forte@Sun.COM 				ipp->ipp_ppd->ppd_provider = NULL;
24217836SJohn.Forte@Sun.COM 				ipp->ipp_ppd = NULL;
24227836SJohn.Forte@Sun.COM 			}
24237836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
24247836SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
24257836SJohn.Forte@Sun.COM 		}
24267836SJohn.Forte@Sun.COM 	}
24277836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
24287836SJohn.Forte@Sun.COM 	return (STMF_NOT_FOUND);
24297836SJohn.Forte@Sun.COM }
24307836SJohn.Forte@Sun.COM 
24317836SJohn.Forte@Sun.COM int
stmf_load_ppd_ioctl(stmf_ppioctl_data_t * ppi,uint64_t * ppi_token,uint32_t * err_ret)24329585STim.Szeto@Sun.COM stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
24339585STim.Szeto@Sun.COM     uint32_t *err_ret)
24347836SJohn.Forte@Sun.COM {
24357836SJohn.Forte@Sun.COM 	stmf_i_port_provider_t		*ipp;
24367836SJohn.Forte@Sun.COM 	stmf_i_lu_provider_t		*ilp;
24377836SJohn.Forte@Sun.COM 	stmf_pp_data_t			*ppd;
24387836SJohn.Forte@Sun.COM 	nvlist_t			*nv;
24397836SJohn.Forte@Sun.COM 	int				s;
24407836SJohn.Forte@Sun.COM 	int				ret;
24417836SJohn.Forte@Sun.COM 
24429585STim.Szeto@Sun.COM 	*err_ret = 0;
24439585STim.Szeto@Sun.COM 
24447836SJohn.Forte@Sun.COM 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
24457836SJohn.Forte@Sun.COM 		return (EINVAL);
24467836SJohn.Forte@Sun.COM 	}
24477836SJohn.Forte@Sun.COM 
24487836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
24497836SJohn.Forte@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
24507836SJohn.Forte@Sun.COM 		if (ppi->ppi_lu_provider) {
24517836SJohn.Forte@Sun.COM 			if (!ppd->ppd_lu_provider)
24527836SJohn.Forte@Sun.COM 				continue;
24537836SJohn.Forte@Sun.COM 		} else if (ppi->ppi_port_provider) {
24547836SJohn.Forte@Sun.COM 			if (!ppd->ppd_port_provider)
24557836SJohn.Forte@Sun.COM 				continue;
24567836SJohn.Forte@Sun.COM 		}
24577836SJohn.Forte@Sun.COM 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
24587836SJohn.Forte@Sun.COM 			break;
24597836SJohn.Forte@Sun.COM 	}
24607836SJohn.Forte@Sun.COM 
24617836SJohn.Forte@Sun.COM 	if (ppd == NULL) {
24627836SJohn.Forte@Sun.COM 		/* New provider */
24637836SJohn.Forte@Sun.COM 		s = strlen(ppi->ppi_name);
24647836SJohn.Forte@Sun.COM 		if (s > 254) {
24657836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
24667836SJohn.Forte@Sun.COM 			return (EINVAL);
24677836SJohn.Forte@Sun.COM 		}
24687836SJohn.Forte@Sun.COM 		s += sizeof (stmf_pp_data_t) - 7;
24697836SJohn.Forte@Sun.COM 
24707836SJohn.Forte@Sun.COM 		ppd = kmem_zalloc(s, KM_NOSLEEP);
24717836SJohn.Forte@Sun.COM 		if (ppd == NULL) {
24727836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
24737836SJohn.Forte@Sun.COM 			return (ENOMEM);
24747836SJohn.Forte@Sun.COM 		}
24757836SJohn.Forte@Sun.COM 		ppd->ppd_alloc_size = s;
24767836SJohn.Forte@Sun.COM 		(void) strcpy(ppd->ppd_name, ppi->ppi_name);
24777836SJohn.Forte@Sun.COM 
24787836SJohn.Forte@Sun.COM 		/* See if this provider already exists */
24797836SJohn.Forte@Sun.COM 		if (ppi->ppi_lu_provider) {
24807836SJohn.Forte@Sun.COM 			ppd->ppd_lu_provider = 1;
24817836SJohn.Forte@Sun.COM 			for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
24827836SJohn.Forte@Sun.COM 			    ilp = ilp->ilp_next) {
24837836SJohn.Forte@Sun.COM 				if (strcmp(ppi->ppi_name,
24847836SJohn.Forte@Sun.COM 				    ilp->ilp_lp->lp_name) == 0) {
24857836SJohn.Forte@Sun.COM 					ppd->ppd_provider = ilp;
24867836SJohn.Forte@Sun.COM 					ilp->ilp_ppd = ppd;
24877836SJohn.Forte@Sun.COM 					break;
24887836SJohn.Forte@Sun.COM 				}
24897836SJohn.Forte@Sun.COM 			}
24907836SJohn.Forte@Sun.COM 		} else {
24917836SJohn.Forte@Sun.COM 			ppd->ppd_port_provider = 1;
24927836SJohn.Forte@Sun.COM 			for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
24937836SJohn.Forte@Sun.COM 			    ipp = ipp->ipp_next) {
24947836SJohn.Forte@Sun.COM 				if (strcmp(ppi->ppi_name,
24957836SJohn.Forte@Sun.COM 				    ipp->ipp_pp->pp_name) == 0) {
24967836SJohn.Forte@Sun.COM 					ppd->ppd_provider = ipp;
24977836SJohn.Forte@Sun.COM 					ipp->ipp_ppd = ppd;
24987836SJohn.Forte@Sun.COM 					break;
24997836SJohn.Forte@Sun.COM 				}
25007836SJohn.Forte@Sun.COM 			}
25017836SJohn.Forte@Sun.COM 		}
25027836SJohn.Forte@Sun.COM 
25037836SJohn.Forte@Sun.COM 		/* Link this ppd in */
25047836SJohn.Forte@Sun.COM 		ppd->ppd_next = stmf_state.stmf_ppdlist;
25057836SJohn.Forte@Sun.COM 		stmf_state.stmf_ppdlist = ppd;
25067836SJohn.Forte@Sun.COM 	}
25077836SJohn.Forte@Sun.COM 
25089585STim.Szeto@Sun.COM 	/*
25099585STim.Szeto@Sun.COM 	 * User is requesting that the token be checked.
25109585STim.Szeto@Sun.COM 	 * If there was another set after the user's get
25119585STim.Szeto@Sun.COM 	 * it's an error
25129585STim.Szeto@Sun.COM 	 */
25139585STim.Szeto@Sun.COM 	if (ppi->ppi_token_valid) {
25149585STim.Szeto@Sun.COM 		if (ppi->ppi_token != ppd->ppd_token) {
25159585STim.Szeto@Sun.COM 			*err_ret = STMF_IOCERR_PPD_UPDATED;
25169585STim.Szeto@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
25179585STim.Szeto@Sun.COM 			return (EINVAL);
25189585STim.Szeto@Sun.COM 		}
25199585STim.Szeto@Sun.COM 	}
25209585STim.Szeto@Sun.COM 
25217836SJohn.Forte@Sun.COM 	if ((ret = nvlist_unpack((char *)ppi->ppi_data,
25227836SJohn.Forte@Sun.COM 	    (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
25237836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
25247836SJohn.Forte@Sun.COM 		return (ret);
25257836SJohn.Forte@Sun.COM 	}
25267836SJohn.Forte@Sun.COM 
25277836SJohn.Forte@Sun.COM 	/* Free any existing lists and add this one to the ppd */
25287836SJohn.Forte@Sun.COM 	if (ppd->ppd_nv)
25297836SJohn.Forte@Sun.COM 		nvlist_free(ppd->ppd_nv);
25307836SJohn.Forte@Sun.COM 	ppd->ppd_nv = nv;
25317836SJohn.Forte@Sun.COM 
25329585STim.Szeto@Sun.COM 	/* set the token for writes */
25339585STim.Szeto@Sun.COM 	ppd->ppd_token++;
25349585STim.Szeto@Sun.COM 	/* return token to caller */
25359585STim.Szeto@Sun.COM 	if (ppi_token) {
25369585STim.Szeto@Sun.COM 		*ppi_token = ppd->ppd_token;
25379585STim.Szeto@Sun.COM 	}
25389585STim.Szeto@Sun.COM 
25397836SJohn.Forte@Sun.COM 	/* If there is a provider registered, do the notifications */
25407836SJohn.Forte@Sun.COM 	if (ppd->ppd_provider) {
25417836SJohn.Forte@Sun.COM 		uint32_t cb_flags = 0;
25427836SJohn.Forte@Sun.COM 
25437836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
25447836SJohn.Forte@Sun.COM 			cb_flags |= STMF_PCB_STMF_ONLINING;
25457836SJohn.Forte@Sun.COM 		if (ppi->ppi_lu_provider) {
25467836SJohn.Forte@Sun.COM 			ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
25477836SJohn.Forte@Sun.COM 			if (ilp->ilp_lp->lp_cb == NULL)
25487836SJohn.Forte@Sun.COM 				goto bail_out;
25497836SJohn.Forte@Sun.COM 			ilp->ilp_cb_in_progress = 1;
25507836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
25517836SJohn.Forte@Sun.COM 			ilp->ilp_lp->lp_cb(ilp->ilp_lp,
25527836SJohn.Forte@Sun.COM 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
25537836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
25547836SJohn.Forte@Sun.COM 			ilp->ilp_cb_in_progress = 0;
25557836SJohn.Forte@Sun.COM 		} else {
25567836SJohn.Forte@Sun.COM 			ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
25577836SJohn.Forte@Sun.COM 			if (ipp->ipp_pp->pp_cb == NULL)
25587836SJohn.Forte@Sun.COM 				goto bail_out;
25597836SJohn.Forte@Sun.COM 			ipp->ipp_cb_in_progress = 1;
25607836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
25617836SJohn.Forte@Sun.COM 			ipp->ipp_pp->pp_cb(ipp->ipp_pp,
25627836SJohn.Forte@Sun.COM 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
25637836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
25647836SJohn.Forte@Sun.COM 			ipp->ipp_cb_in_progress = 0;
25657836SJohn.Forte@Sun.COM 		}
25667836SJohn.Forte@Sun.COM 	}
25677836SJohn.Forte@Sun.COM 
25687836SJohn.Forte@Sun.COM bail_out:
25697836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
25707836SJohn.Forte@Sun.COM 
25717836SJohn.Forte@Sun.COM 	return (0);
25727836SJohn.Forte@Sun.COM }
25737836SJohn.Forte@Sun.COM 
25747836SJohn.Forte@Sun.COM void
stmf_delete_ppd(stmf_pp_data_t * ppd)25757836SJohn.Forte@Sun.COM stmf_delete_ppd(stmf_pp_data_t *ppd)
25767836SJohn.Forte@Sun.COM {
25777836SJohn.Forte@Sun.COM 	stmf_pp_data_t **pppd;
25787836SJohn.Forte@Sun.COM 
25797836SJohn.Forte@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
25807836SJohn.Forte@Sun.COM 	if (ppd->ppd_provider) {
25817836SJohn.Forte@Sun.COM 		if (ppd->ppd_lu_provider) {
25827836SJohn.Forte@Sun.COM 			((stmf_i_lu_provider_t *)
25837836SJohn.Forte@Sun.COM 			    ppd->ppd_provider)->ilp_ppd = NULL;
25847836SJohn.Forte@Sun.COM 		} else {
25857836SJohn.Forte@Sun.COM 			((stmf_i_port_provider_t *)
25867836SJohn.Forte@Sun.COM 			    ppd->ppd_provider)->ipp_ppd = NULL;
25877836SJohn.Forte@Sun.COM 		}
25887836SJohn.Forte@Sun.COM 		ppd->ppd_provider = NULL;
25897836SJohn.Forte@Sun.COM 	}
25907836SJohn.Forte@Sun.COM 
25917836SJohn.Forte@Sun.COM 	for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
25928662SJordan.Vaughan@Sun.com 	    pppd = &((*pppd)->ppd_next)) {
25937836SJohn.Forte@Sun.COM 		if (*pppd == ppd)
25947836SJohn.Forte@Sun.COM 			break;
25957836SJohn.Forte@Sun.COM 	}
25967836SJohn.Forte@Sun.COM 
25977836SJohn.Forte@Sun.COM 	if (*pppd == NULL)
25987836SJohn.Forte@Sun.COM 		return;
25997836SJohn.Forte@Sun.COM 
26007836SJohn.Forte@Sun.COM 	*pppd = ppd->ppd_next;
26017836SJohn.Forte@Sun.COM 	if (ppd->ppd_nv)
26027836SJohn.Forte@Sun.COM 		nvlist_free(ppd->ppd_nv);
26037836SJohn.Forte@Sun.COM 
26047836SJohn.Forte@Sun.COM 	kmem_free(ppd, ppd->ppd_alloc_size);
26057836SJohn.Forte@Sun.COM }
26067836SJohn.Forte@Sun.COM 
26077836SJohn.Forte@Sun.COM int
stmf_delete_ppd_ioctl(stmf_ppioctl_data_t * ppi)26087836SJohn.Forte@Sun.COM stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
26097836SJohn.Forte@Sun.COM {
26107836SJohn.Forte@Sun.COM 	stmf_pp_data_t *ppd;
26117836SJohn.Forte@Sun.COM 	int ret = ENOENT;
26127836SJohn.Forte@Sun.COM 
26137836SJohn.Forte@Sun.COM 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
26147836SJohn.Forte@Sun.COM 		return (EINVAL);
26157836SJohn.Forte@Sun.COM 	}
26167836SJohn.Forte@Sun.COM 
26177836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
26187836SJohn.Forte@Sun.COM 
26197836SJohn.Forte@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
26207836SJohn.Forte@Sun.COM 		if (ppi->ppi_lu_provider) {
26217836SJohn.Forte@Sun.COM 			if (!ppd->ppd_lu_provider)
26227836SJohn.Forte@Sun.COM 				continue;
26237836SJohn.Forte@Sun.COM 		} else if (ppi->ppi_port_provider) {
26247836SJohn.Forte@Sun.COM 			if (!ppd->ppd_port_provider)
26257836SJohn.Forte@Sun.COM 				continue;
26267836SJohn.Forte@Sun.COM 		}
26277836SJohn.Forte@Sun.COM 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
26287836SJohn.Forte@Sun.COM 			break;
26297836SJohn.Forte@Sun.COM 	}
26307836SJohn.Forte@Sun.COM 
26317836SJohn.Forte@Sun.COM 	if (ppd) {
26327836SJohn.Forte@Sun.COM 		ret = 0;
26337836SJohn.Forte@Sun.COM 		stmf_delete_ppd(ppd);
26347836SJohn.Forte@Sun.COM 	}
26357836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
26367836SJohn.Forte@Sun.COM 
26377836SJohn.Forte@Sun.COM 	return (ret);
26387836SJohn.Forte@Sun.COM }
26397836SJohn.Forte@Sun.COM 
26409585STim.Szeto@Sun.COM int
stmf_get_ppd_ioctl(stmf_ppioctl_data_t * ppi,stmf_ppioctl_data_t * ppi_out,uint32_t * err_ret)26419585STim.Szeto@Sun.COM stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
26429585STim.Szeto@Sun.COM     uint32_t *err_ret)
26439585STim.Szeto@Sun.COM {
26449585STim.Szeto@Sun.COM 	stmf_pp_data_t *ppd;
26459585STim.Szeto@Sun.COM 	size_t req_size;
26469585STim.Szeto@Sun.COM 	int ret = ENOENT;
26479585STim.Szeto@Sun.COM 	char *bufp = (char *)ppi_out->ppi_data;
26489585STim.Szeto@Sun.COM 
26499585STim.Szeto@Sun.COM 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
26509585STim.Szeto@Sun.COM 		return (EINVAL);
26519585STim.Szeto@Sun.COM 	}
26529585STim.Szeto@Sun.COM 
26539585STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
26549585STim.Szeto@Sun.COM 
26559585STim.Szeto@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
26569585STim.Szeto@Sun.COM 		if (ppi->ppi_lu_provider) {
26579585STim.Szeto@Sun.COM 			if (!ppd->ppd_lu_provider)
26589585STim.Szeto@Sun.COM 				continue;
26599585STim.Szeto@Sun.COM 		} else if (ppi->ppi_port_provider) {
26609585STim.Szeto@Sun.COM 			if (!ppd->ppd_port_provider)
26619585STim.Szeto@Sun.COM 				continue;
26629585STim.Szeto@Sun.COM 		}
26639585STim.Szeto@Sun.COM 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
26649585STim.Szeto@Sun.COM 			break;
26659585STim.Szeto@Sun.COM 	}
26669585STim.Szeto@Sun.COM 
26679585STim.Szeto@Sun.COM 	if (ppd && ppd->ppd_nv) {
26689585STim.Szeto@Sun.COM 		ppi_out->ppi_token = ppd->ppd_token;
26699585STim.Szeto@Sun.COM 		if ((ret = nvlist_size(ppd->ppd_nv, &req_size,
26709585STim.Szeto@Sun.COM 		    NV_ENCODE_XDR)) != 0) {
26719585STim.Szeto@Sun.COM 			goto done;
26729585STim.Szeto@Sun.COM 		}
26739585STim.Szeto@Sun.COM 		ppi_out->ppi_data_size = req_size;
26749585STim.Szeto@Sun.COM 		if (req_size > ppi->ppi_data_size) {
26759585STim.Szeto@Sun.COM 			*err_ret = STMF_IOCERR_INSUFFICIENT_BUF;
26769585STim.Szeto@Sun.COM 			ret = EINVAL;
26779585STim.Szeto@Sun.COM 			goto done;
26789585STim.Szeto@Sun.COM 		}
26799585STim.Szeto@Sun.COM 
26809585STim.Szeto@Sun.COM 		if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size,
26819585STim.Szeto@Sun.COM 		    NV_ENCODE_XDR, 0)) != 0) {
26829585STim.Szeto@Sun.COM 			goto done;
26839585STim.Szeto@Sun.COM 		}
26849585STim.Szeto@Sun.COM 		ret = 0;
26859585STim.Szeto@Sun.COM 	}
26869585STim.Szeto@Sun.COM 
26879585STim.Szeto@Sun.COM done:
26889585STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
26899585STim.Szeto@Sun.COM 
26909585STim.Szeto@Sun.COM 	return (ret);
26919585STim.Szeto@Sun.COM }
26929585STim.Szeto@Sun.COM 
26937836SJohn.Forte@Sun.COM void
stmf_delete_all_ppds()26947836SJohn.Forte@Sun.COM stmf_delete_all_ppds()
26957836SJohn.Forte@Sun.COM {
26967836SJohn.Forte@Sun.COM 	stmf_pp_data_t *ppd, *nppd;
26977836SJohn.Forte@Sun.COM 
26987836SJohn.Forte@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
26997836SJohn.Forte@Sun.COM 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
27007836SJohn.Forte@Sun.COM 		nppd = ppd->ppd_next;
27017836SJohn.Forte@Sun.COM 		stmf_delete_ppd(ppd);
27027836SJohn.Forte@Sun.COM 	}
27037836SJohn.Forte@Sun.COM }
27047836SJohn.Forte@Sun.COM 
27059435STim.Szeto@Sun.COM /*
27069435STim.Szeto@Sun.COM  * 16 is the max string length of a protocol_ident, increase
27079435STim.Szeto@Sun.COM  * the size if needed.
27089435STim.Szeto@Sun.COM  */
27099435STim.Szeto@Sun.COM #define	STMF_KSTAT_LU_SZ	(STMF_GUID_INPUT + 1 + 256)
27109435STim.Szeto@Sun.COM #define	STMF_KSTAT_TGT_SZ	(256 * 2 + 16)
27119435STim.Szeto@Sun.COM 
27129435STim.Szeto@Sun.COM /*
27139435STim.Szeto@Sun.COM  * This array matches the Protocol Identifier in stmf_ioctl.h
27149435STim.Szeto@Sun.COM  */
271511773STim.Szeto@Sun.COM #define	MAX_PROTO_STR_LEN	32
271611773STim.Szeto@Sun.COM 
27179435STim.Szeto@Sun.COM char *protocol_ident[PROTOCOL_ANY] = {
27189435STim.Szeto@Sun.COM 	"Fibre Channel",
27199435STim.Szeto@Sun.COM 	"Parallel SCSI",
27209435STim.Szeto@Sun.COM 	"SSA",
27219435STim.Szeto@Sun.COM 	"IEEE_1394",
27229435STim.Szeto@Sun.COM 	"SRP",
27239435STim.Szeto@Sun.COM 	"iSCSI",
27249435STim.Szeto@Sun.COM 	"SAS",
27259435STim.Szeto@Sun.COM 	"ADT",
27269435STim.Szeto@Sun.COM 	"ATAPI",
27279435STim.Szeto@Sun.COM 	"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"
27289435STim.Szeto@Sun.COM };
27299435STim.Szeto@Sun.COM 
27309435STim.Szeto@Sun.COM /*
27319435STim.Szeto@Sun.COM  * Update the lun wait/run queue count
27329435STim.Szeto@Sun.COM  */
27339435STim.Szeto@Sun.COM static void
stmf_update_kstat_lu_q(scsi_task_t * task,void func ())27349435STim.Szeto@Sun.COM stmf_update_kstat_lu_q(scsi_task_t *task, void func())
27359435STim.Szeto@Sun.COM {
27369435STim.Szeto@Sun.COM 	stmf_i_lu_t		*ilu;
27379435STim.Szeto@Sun.COM 	kstat_io_t		*kip;
27389435STim.Szeto@Sun.COM 
27399435STim.Szeto@Sun.COM 	if (task->task_lu == dlun0)
27409435STim.Szeto@Sun.COM 		return;
27419435STim.Szeto@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
27429435STim.Szeto@Sun.COM 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
27439435STim.Szeto@Sun.COM 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
27449435STim.Szeto@Sun.COM 		if (kip != NULL) {
27459435STim.Szeto@Sun.COM 			func(kip);
27469435STim.Szeto@Sun.COM 		}
27479435STim.Szeto@Sun.COM 	}
27489435STim.Szeto@Sun.COM }
27499435STim.Szeto@Sun.COM 
27509435STim.Szeto@Sun.COM /*
27519435STim.Szeto@Sun.COM  * Update the target(lport) wait/run queue count
27529435STim.Szeto@Sun.COM  */
27539435STim.Szeto@Sun.COM static void
stmf_update_kstat_lport_q(scsi_task_t * task,void func ())27549435STim.Szeto@Sun.COM stmf_update_kstat_lport_q(scsi_task_t *task, void func())
27559435STim.Szeto@Sun.COM {
27569435STim.Szeto@Sun.COM 	stmf_i_local_port_t	*ilp;
27579435STim.Szeto@Sun.COM 	kstat_io_t		*kip;
27589435STim.Szeto@Sun.COM 
27599435STim.Szeto@Sun.COM 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
27609435STim.Szeto@Sun.COM 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
27619435STim.Szeto@Sun.COM 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
27629435STim.Szeto@Sun.COM 		if (kip != NULL) {
27639435STim.Szeto@Sun.COM 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
27649435STim.Szeto@Sun.COM 			func(kip);
27659435STim.Szeto@Sun.COM 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
27669435STim.Szeto@Sun.COM 		}
27679435STim.Szeto@Sun.COM 	}
27689435STim.Szeto@Sun.COM }
27699435STim.Szeto@Sun.COM 
27709435STim.Szeto@Sun.COM static void
stmf_update_kstat_lport_io(scsi_task_t * task,stmf_data_buf_t * dbuf)27719435STim.Szeto@Sun.COM stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
27729435STim.Szeto@Sun.COM {
27739435STim.Szeto@Sun.COM 	stmf_i_local_port_t	*ilp;
27749435STim.Szeto@Sun.COM 	kstat_io_t		*kip;
27759435STim.Szeto@Sun.COM 
27769435STim.Szeto@Sun.COM 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
27779435STim.Szeto@Sun.COM 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
27789435STim.Szeto@Sun.COM 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
27799435STim.Szeto@Sun.COM 		if (kip != NULL) {
27809435STim.Szeto@Sun.COM 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
27819435STim.Szeto@Sun.COM 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
27829435STim.Szeto@Sun.COM 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
27839435STim.Szeto@Sun.COM 		}
27849435STim.Szeto@Sun.COM 	}
27859435STim.Szeto@Sun.COM }
27869435STim.Szeto@Sun.COM 
27879435STim.Szeto@Sun.COM static void
stmf_update_kstat_lu_io(scsi_task_t * task,stmf_data_buf_t * dbuf)27889435STim.Szeto@Sun.COM stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
27899435STim.Szeto@Sun.COM {
27909435STim.Szeto@Sun.COM 	stmf_i_lu_t		*ilu;
27919435STim.Szeto@Sun.COM 	kstat_io_t		*kip;
27929435STim.Szeto@Sun.COM 
27939435STim.Szeto@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
27949435STim.Szeto@Sun.COM 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
27959435STim.Szeto@Sun.COM 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
27969435STim.Szeto@Sun.COM 		if (kip != NULL) {
27979435STim.Szeto@Sun.COM 			mutex_enter(ilu->ilu_kstat_io->ks_lock);
27989435STim.Szeto@Sun.COM 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
27999435STim.Szeto@Sun.COM 			mutex_exit(ilu->ilu_kstat_io->ks_lock);
28009435STim.Szeto@Sun.COM 		}
28019435STim.Szeto@Sun.COM 	}
28029435STim.Szeto@Sun.COM }
28039435STim.Szeto@Sun.COM 
28049435STim.Szeto@Sun.COM static void
stmf_create_kstat_lu(stmf_i_lu_t * ilu)28059435STim.Szeto@Sun.COM stmf_create_kstat_lu(stmf_i_lu_t *ilu)
28069435STim.Szeto@Sun.COM {
28079435STim.Szeto@Sun.COM 	char				ks_nm[KSTAT_STRLEN];
28089435STim.Szeto@Sun.COM 	stmf_kstat_lu_info_t		*ks_lu;
28099435STim.Szeto@Sun.COM 
28109435STim.Szeto@Sun.COM 	/* create kstat lun info */
28119435STim.Szeto@Sun.COM 	ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ,
28129435STim.Szeto@Sun.COM 	    KM_NOSLEEP);
28139435STim.Szeto@Sun.COM 	if (ks_lu == NULL) {
28149435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
28159435STim.Szeto@Sun.COM 		return;
28169435STim.Szeto@Sun.COM 	}
28179435STim.Szeto@Sun.COM 
28189435STim.Szeto@Sun.COM 	bzero(ks_nm, sizeof (ks_nm));
28199435STim.Szeto@Sun.COM 	(void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu);
28209435STim.Szeto@Sun.COM 	if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0,
28219435STim.Szeto@Sun.COM 	    ks_nm, "misc", KSTAT_TYPE_NAMED,
28229435STim.Szeto@Sun.COM 	    sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t),
28239435STim.Szeto@Sun.COM 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
28249435STim.Szeto@Sun.COM 		kmem_free(ks_lu, STMF_KSTAT_LU_SZ);
28259435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kstat_create lu failed");
28269435STim.Szeto@Sun.COM 		return;
28279435STim.Szeto@Sun.COM 	}
28289435STim.Szeto@Sun.COM 
28299435STim.Szeto@Sun.COM 	ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ;
28309435STim.Szeto@Sun.COM 	ilu->ilu_kstat_info->ks_data = ks_lu;
28319435STim.Szeto@Sun.COM 
28329435STim.Szeto@Sun.COM 	kstat_named_init(&ks_lu->i_lun_guid, "lun-guid",
28339435STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
28349435STim.Szeto@Sun.COM 	kstat_named_init(&ks_lu->i_lun_alias, "lun-alias",
28359435STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
28369435STim.Szeto@Sun.COM 
28379435STim.Szeto@Sun.COM 	/* convert guid to hex string */
28389435STim.Szeto@Sun.COM 	int		i;
28399435STim.Szeto@Sun.COM 	uint8_t		*p = ilu->ilu_lu->lu_id->ident;
28409435STim.Szeto@Sun.COM 	bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid));
28419435STim.Szeto@Sun.COM 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
28429435STim.Szeto@Sun.COM 		(void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]);
28439435STim.Szeto@Sun.COM 	}
28449435STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_lu->i_lun_guid,
28459435STim.Szeto@Sun.COM 	    (const char *)ilu->ilu_ascii_hex_guid);
28469435STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_lu->i_lun_alias,
28479435STim.Szeto@Sun.COM 	    (const char *)ilu->ilu_lu->lu_alias);
28489435STim.Szeto@Sun.COM 	kstat_install(ilu->ilu_kstat_info);
28499435STim.Szeto@Sun.COM 
28509435STim.Szeto@Sun.COM 	/* create kstat lun io */
28519435STim.Szeto@Sun.COM 	bzero(ks_nm, sizeof (ks_nm));
28529435STim.Szeto@Sun.COM 	(void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu);
28539435STim.Szeto@Sun.COM 	if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
28549435STim.Szeto@Sun.COM 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
28559435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kstat_create lu_io failed");
28569435STim.Szeto@Sun.COM 		return;
28579435STim.Szeto@Sun.COM 	}
28589435STim.Szeto@Sun.COM 	mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0);
28599435STim.Szeto@Sun.COM 	ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock;
28609435STim.Szeto@Sun.COM 	kstat_install(ilu->ilu_kstat_io);
28619435STim.Szeto@Sun.COM }
28629435STim.Szeto@Sun.COM 
28639435STim.Szeto@Sun.COM static void
stmf_create_kstat_lport(stmf_i_local_port_t * ilport)28649435STim.Szeto@Sun.COM stmf_create_kstat_lport(stmf_i_local_port_t *ilport)
28659435STim.Szeto@Sun.COM {
28669435STim.Szeto@Sun.COM 	char				ks_nm[KSTAT_STRLEN];
28679435STim.Szeto@Sun.COM 	stmf_kstat_tgt_info_t		*ks_tgt;
28689435STim.Szeto@Sun.COM 	int				id, len;
28699435STim.Szeto@Sun.COM 
28709435STim.Szeto@Sun.COM 	/* create kstat lport info */
28719435STim.Szeto@Sun.COM 	ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ,
28729435STim.Szeto@Sun.COM 	    KM_NOSLEEP);
28739435STim.Szeto@Sun.COM 	if (ks_tgt == NULL) {
28749435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
28759435STim.Szeto@Sun.COM 		return;
28769435STim.Szeto@Sun.COM 	}
28779435STim.Szeto@Sun.COM 
28789435STim.Szeto@Sun.COM 	bzero(ks_nm, sizeof (ks_nm));
28799435STim.Szeto@Sun.COM 	(void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport);
28809435STim.Szeto@Sun.COM 	if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME,
28819435STim.Szeto@Sun.COM 	    0, ks_nm, "misc", KSTAT_TYPE_NAMED,
28829435STim.Szeto@Sun.COM 	    sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t),
28839435STim.Szeto@Sun.COM 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
28849435STim.Szeto@Sun.COM 		kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ);
28859435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kstat_create target failed");
28869435STim.Szeto@Sun.COM 		return;
28879435STim.Szeto@Sun.COM 	}
28889435STim.Szeto@Sun.COM 
28899435STim.Szeto@Sun.COM 	ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ;
28909435STim.Szeto@Sun.COM 	ilport->ilport_kstat_info->ks_data = ks_tgt;
28919435STim.Szeto@Sun.COM 
28929435STim.Szeto@Sun.COM 	kstat_named_init(&ks_tgt->i_tgt_name, "target-name",
28939435STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
28949435STim.Szeto@Sun.COM 	kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias",
28959435STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
28969435STim.Szeto@Sun.COM 	kstat_named_init(&ks_tgt->i_protocol, "protocol",
28979435STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
28989435STim.Szeto@Sun.COM 
28999435STim.Szeto@Sun.COM 	/* ident might not be null terminated */
29009435STim.Szeto@Sun.COM 	len = ilport->ilport_lport->lport_id->ident_length;
29019435STim.Szeto@Sun.COM 	bcopy(ilport->ilport_lport->lport_id->ident,
29029435STim.Szeto@Sun.COM 	    ilport->ilport_kstat_tgt_name, len);
29039435STim.Szeto@Sun.COM 	ilport->ilport_kstat_tgt_name[len + 1] = NULL;
29049435STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_tgt->i_tgt_name,
29059435STim.Szeto@Sun.COM 	    (const char *)ilport->ilport_kstat_tgt_name);
29069435STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_tgt->i_tgt_alias,
29079435STim.Szeto@Sun.COM 	    (const char *)ilport->ilport_lport->lport_alias);
29089435STim.Szeto@Sun.COM 	/* protocol */
29099435STim.Szeto@Sun.COM 	if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) {
29109435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: protocol_id out of bound");
29119435STim.Szeto@Sun.COM 		id = PROTOCOL_ANY;
29129435STim.Szeto@Sun.COM 	}
29139435STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]);
29149435STim.Szeto@Sun.COM 	kstat_install(ilport->ilport_kstat_info);
29159435STim.Szeto@Sun.COM 
29169435STim.Szeto@Sun.COM 	/* create kstat lport io */
29179435STim.Szeto@Sun.COM 	bzero(ks_nm, sizeof (ks_nm));
29189435STim.Szeto@Sun.COM 	(void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport);
29199435STim.Szeto@Sun.COM 	if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
29209435STim.Szeto@Sun.COM 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
29219435STim.Szeto@Sun.COM 		cmn_err(CE_WARN, "STMF: kstat_create target_io failed");
29229435STim.Szeto@Sun.COM 		return;
29239435STim.Szeto@Sun.COM 	}
29249435STim.Szeto@Sun.COM 	mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0);
29259435STim.Szeto@Sun.COM 	ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock;
29269435STim.Szeto@Sun.COM 	kstat_install(ilport->ilport_kstat_io);
29279435STim.Szeto@Sun.COM }
29289435STim.Szeto@Sun.COM 
292910725SJohn.Forte@Sun.COM /*
293010725SJohn.Forte@Sun.COM  * set the asymmetric access state for a logical unit
293110725SJohn.Forte@Sun.COM  * caller is responsible for establishing SCSI unit attention on
293210725SJohn.Forte@Sun.COM  * state change
293310725SJohn.Forte@Sun.COM  */
293410725SJohn.Forte@Sun.COM stmf_status_t
stmf_set_lu_access(stmf_lu_t * lu,uint8_t access_state)293510725SJohn.Forte@Sun.COM stmf_set_lu_access(stmf_lu_t *lu, uint8_t access_state)
293610725SJohn.Forte@Sun.COM {
293710725SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
293810725SJohn.Forte@Sun.COM 	uint8_t *p1, *p2;
293910725SJohn.Forte@Sun.COM 
294010725SJohn.Forte@Sun.COM 	if ((access_state != STMF_LU_STANDBY) &&
294110725SJohn.Forte@Sun.COM 	    (access_state != STMF_LU_ACTIVE)) {
294210725SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
294310725SJohn.Forte@Sun.COM 	}
294410725SJohn.Forte@Sun.COM 
294510725SJohn.Forte@Sun.COM 	p1 = &lu->lu_id->ident[0];
294610725SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
294710725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
294810725SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
294910725SJohn.Forte@Sun.COM 		return (STMF_BUSY);
295010725SJohn.Forte@Sun.COM 	}
295110725SJohn.Forte@Sun.COM 
295210725SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
295310725SJohn.Forte@Sun.COM 		p2 = &ilu->ilu_lu->lu_id->ident[0];
295410725SJohn.Forte@Sun.COM 		if (bcmp(p1, p2, 16) == 0) {
295510725SJohn.Forte@Sun.COM 			break;
295610725SJohn.Forte@Sun.COM 		}
295710725SJohn.Forte@Sun.COM 	}
295810725SJohn.Forte@Sun.COM 
295910725SJohn.Forte@Sun.COM 	if (!ilu) {
296010725SJohn.Forte@Sun.COM 		ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
296110725SJohn.Forte@Sun.COM 	} else {
296210725SJohn.Forte@Sun.COM 		/*
296310725SJohn.Forte@Sun.COM 		 * We're changing access state on an existing logical unit
296410725SJohn.Forte@Sun.COM 		 * Send the proxy registration message for this logical unit
296510725SJohn.Forte@Sun.COM 		 * if we're in alua mode.
296610725SJohn.Forte@Sun.COM 		 * If the requested state is STMF_LU_ACTIVE, we want to register
296710725SJohn.Forte@Sun.COM 		 * this logical unit.
296810725SJohn.Forte@Sun.COM 		 * If the requested state is STMF_LU_STANDBY, we're going to
296910725SJohn.Forte@Sun.COM 		 * abort all tasks for this logical unit.
297010725SJohn.Forte@Sun.COM 		 */
297110725SJohn.Forte@Sun.COM 		if (stmf_state.stmf_alua_state == 1 &&
297210725SJohn.Forte@Sun.COM 		    access_state == STMF_LU_ACTIVE) {
297310725SJohn.Forte@Sun.COM 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
297410725SJohn.Forte@Sun.COM 			stmf_ic_msg_t *ic_reg_lun;
297510725SJohn.Forte@Sun.COM 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
297610725SJohn.Forte@Sun.COM 			    lu->lu_lp->lp_alua_support) {
297710725SJohn.Forte@Sun.COM 				ilu->ilu_alua = 1;
297810725SJohn.Forte@Sun.COM 				/* allocate the register message */
297910725SJohn.Forte@Sun.COM 				ic_reg_lun = ic_lun_active_msg_alloc(p1,
298010725SJohn.Forte@Sun.COM 				    lu->lu_lp->lp_name,
298110725SJohn.Forte@Sun.COM 				    lu->lu_proxy_reg_arg_len,
298210725SJohn.Forte@Sun.COM 				    (uint8_t *)lu->lu_proxy_reg_arg,
298310725SJohn.Forte@Sun.COM 				    stmf_proxy_msg_id);
298410725SJohn.Forte@Sun.COM 				/* send the message */
298510725SJohn.Forte@Sun.COM 				if (ic_reg_lun) {
298610725SJohn.Forte@Sun.COM 					ic_ret = ic_tx_msg(ic_reg_lun);
298710725SJohn.Forte@Sun.COM 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
298810725SJohn.Forte@Sun.COM 						stmf_proxy_msg_id++;
298910725SJohn.Forte@Sun.COM 					}
299010725SJohn.Forte@Sun.COM 				}
299110725SJohn.Forte@Sun.COM 			}
299210725SJohn.Forte@Sun.COM 		} else if (stmf_state.stmf_alua_state == 1 &&
299310725SJohn.Forte@Sun.COM 		    access_state == STMF_LU_STANDBY) {
299410725SJohn.Forte@Sun.COM 			/* abort all tasks for this lu */
299510725SJohn.Forte@Sun.COM 			stmf_task_lu_killall(lu, NULL, STMF_ABORTED);
299610725SJohn.Forte@Sun.COM 		}
299710725SJohn.Forte@Sun.COM 	}
299810725SJohn.Forte@Sun.COM 
299910725SJohn.Forte@Sun.COM 	ilu->ilu_access = access_state;
300010725SJohn.Forte@Sun.COM 
300110725SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
300210725SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
300310725SJohn.Forte@Sun.COM }
300410725SJohn.Forte@Sun.COM 
300510725SJohn.Forte@Sun.COM 
30067836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_lu(stmf_lu_t * lu)30077836SJohn.Forte@Sun.COM stmf_register_lu(stmf_lu_t *lu)
30087836SJohn.Forte@Sun.COM {
30097836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
30107836SJohn.Forte@Sun.COM 	uint8_t *p1, *p2;
30117836SJohn.Forte@Sun.COM 	stmf_state_change_info_t ssci;
30127836SJohn.Forte@Sun.COM 	stmf_id_data_t *luid;
30137836SJohn.Forte@Sun.COM 
30147836SJohn.Forte@Sun.COM 	if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
30157836SJohn.Forte@Sun.COM 	    (lu->lu_id->ident_length != 16) ||
30167836SJohn.Forte@Sun.COM 	    ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
30177836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
30187836SJohn.Forte@Sun.COM 	}
30197836SJohn.Forte@Sun.COM 	p1 = &lu->lu_id->ident[0];
30207836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
30217836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
30227836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
30237836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
30247836SJohn.Forte@Sun.COM 	}
30257836SJohn.Forte@Sun.COM 
30267836SJohn.Forte@Sun.COM 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
30277836SJohn.Forte@Sun.COM 		p2 = &ilu->ilu_lu->lu_id->ident[0];
30287836SJohn.Forte@Sun.COM 		if (bcmp(p1, p2, 16) == 0) {
30297836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
30307836SJohn.Forte@Sun.COM 			return (STMF_ALREADY);
30317836SJohn.Forte@Sun.COM 		}
30327836SJohn.Forte@Sun.COM 	}
30337836SJohn.Forte@Sun.COM 
30347836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
30357836SJohn.Forte@Sun.COM 	luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
30367836SJohn.Forte@Sun.COM 	    lu->lu_id->ident_length, lu->lu_id->ident);
30377836SJohn.Forte@Sun.COM 	if (luid) {
30387836SJohn.Forte@Sun.COM 		luid->id_pt_to_object = (void *)ilu;
30397836SJohn.Forte@Sun.COM 		ilu->ilu_luid = luid;
30407836SJohn.Forte@Sun.COM 	}
30417836SJohn.Forte@Sun.COM 	ilu->ilu_alias = NULL;
30427836SJohn.Forte@Sun.COM 
30437836SJohn.Forte@Sun.COM 	ilu->ilu_next = stmf_state.stmf_ilulist;
30447836SJohn.Forte@Sun.COM 	ilu->ilu_prev = NULL;
30457836SJohn.Forte@Sun.COM 	if (ilu->ilu_next)
30467836SJohn.Forte@Sun.COM 		ilu->ilu_next->ilu_prev = ilu;
30477836SJohn.Forte@Sun.COM 	stmf_state.stmf_ilulist = ilu;
30487836SJohn.Forte@Sun.COM 	stmf_state.stmf_nlus++;
30497836SJohn.Forte@Sun.COM 	if (lu->lu_lp) {
30507836SJohn.Forte@Sun.COM 		((stmf_i_lu_provider_t *)
30517836SJohn.Forte@Sun.COM 		    (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
30527836SJohn.Forte@Sun.COM 	}
30537836SJohn.Forte@Sun.COM 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
30547836SJohn.Forte@Sun.COM 	STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
30559435STim.Szeto@Sun.COM 	stmf_create_kstat_lu(ilu);
305610725SJohn.Forte@Sun.COM 	/*
305710725SJohn.Forte@Sun.COM 	 * register with proxy module if available and logical unit
305810725SJohn.Forte@Sun.COM 	 * is in active state
305910725SJohn.Forte@Sun.COM 	 */
306010725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state == 1 &&
306110725SJohn.Forte@Sun.COM 	    ilu->ilu_access == STMF_LU_ACTIVE) {
306210725SJohn.Forte@Sun.COM 		stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
306310725SJohn.Forte@Sun.COM 		stmf_ic_msg_t *ic_reg_lun;
306410725SJohn.Forte@Sun.COM 		if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
306510725SJohn.Forte@Sun.COM 		    lu->lu_lp->lp_alua_support) {
306610725SJohn.Forte@Sun.COM 			ilu->ilu_alua = 1;
306710725SJohn.Forte@Sun.COM 			/* allocate the register message */
306810725SJohn.Forte@Sun.COM 			ic_reg_lun = ic_reg_lun_msg_alloc(p1,
306910725SJohn.Forte@Sun.COM 			    lu->lu_lp->lp_name, lu->lu_proxy_reg_arg_len,
307010725SJohn.Forte@Sun.COM 			    (uint8_t *)lu->lu_proxy_reg_arg, stmf_proxy_msg_id);
307110725SJohn.Forte@Sun.COM 			/* send the message */
307210725SJohn.Forte@Sun.COM 			if (ic_reg_lun) {
307310725SJohn.Forte@Sun.COM 				ic_ret = ic_tx_msg(ic_reg_lun);
307410725SJohn.Forte@Sun.COM 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
307510725SJohn.Forte@Sun.COM 					stmf_proxy_msg_id++;
307610725SJohn.Forte@Sun.COM 				}
307710725SJohn.Forte@Sun.COM 			}
307810725SJohn.Forte@Sun.COM 		}
307910725SJohn.Forte@Sun.COM 	}
30807836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
30817836SJohn.Forte@Sun.COM 
308212682SSrivijitha.Dugganapalli@Sun.COM 	/*  check the default state for lu */
308312682SSrivijitha.Dugganapalli@Sun.COM 	if (stmf_state.stmf_default_lu_state == STMF_STATE_OFFLINE) {
308412682SSrivijitha.Dugganapalli@Sun.COM 		ilu->ilu_prev_state = STMF_STATE_OFFLINE;
308512682SSrivijitha.Dugganapalli@Sun.COM 	} else {
308612682SSrivijitha.Dugganapalli@Sun.COM 		ilu->ilu_prev_state = STMF_STATE_ONLINE;
308712682SSrivijitha.Dugganapalli@Sun.COM 		if (stmf_state.stmf_service_running) {
308812682SSrivijitha.Dugganapalli@Sun.COM 			ssci.st_rflags = 0;
308912682SSrivijitha.Dugganapalli@Sun.COM 			ssci.st_additional_info = NULL;
309012682SSrivijitha.Dugganapalli@Sun.COM 			(void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
309112682SSrivijitha.Dugganapalli@Sun.COM 		}
30927836SJohn.Forte@Sun.COM 	}
30937836SJohn.Forte@Sun.COM 
30947836SJohn.Forte@Sun.COM 	/* XXX: Generate event */
30957836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
30967836SJohn.Forte@Sun.COM }
30977836SJohn.Forte@Sun.COM 
30987836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_lu(stmf_lu_t * lu)30997836SJohn.Forte@Sun.COM stmf_deregister_lu(stmf_lu_t *lu)
31007836SJohn.Forte@Sun.COM {
31017836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
31027836SJohn.Forte@Sun.COM 
31037836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
31047836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
31057836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
31067836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
31077836SJohn.Forte@Sun.COM 	}
31087836SJohn.Forte@Sun.COM 	ilu = stmf_lookup_lu(lu);
31097836SJohn.Forte@Sun.COM 	if (ilu == NULL) {
31107836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
31117836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
31127836SJohn.Forte@Sun.COM 	}
31137836SJohn.Forte@Sun.COM 	if (ilu->ilu_state == STMF_STATE_OFFLINE) {
31147836SJohn.Forte@Sun.COM 		ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
31157836SJohn.Forte@Sun.COM 		while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
31167836SJohn.Forte@Sun.COM 			cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
31177836SJohn.Forte@Sun.COM 		}
31187836SJohn.Forte@Sun.COM 		if (ilu->ilu_ntasks) {
31197836SJohn.Forte@Sun.COM 			stmf_i_scsi_task_t *itask, *nitask;
31207836SJohn.Forte@Sun.COM 
31217836SJohn.Forte@Sun.COM 			nitask = ilu->ilu_tasks;
31227836SJohn.Forte@Sun.COM 			do {
31237836SJohn.Forte@Sun.COM 				itask = nitask;
31247836SJohn.Forte@Sun.COM 				nitask = itask->itask_lu_next;
31257836SJohn.Forte@Sun.COM 				lu->lu_task_free(itask->itask_task);
31267836SJohn.Forte@Sun.COM 				stmf_free(itask->itask_task);
31277836SJohn.Forte@Sun.COM 			} while (nitask != NULL);
31287836SJohn.Forte@Sun.COM 
31297836SJohn.Forte@Sun.COM 			ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
31307836SJohn.Forte@Sun.COM 			ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
31317836SJohn.Forte@Sun.COM 		}
313210725SJohn.Forte@Sun.COM 		/* de-register with proxy if available */
313310725SJohn.Forte@Sun.COM 		if (ilu->ilu_access == STMF_LU_ACTIVE &&
313410725SJohn.Forte@Sun.COM 		    stmf_state.stmf_alua_state == 1) {
313510725SJohn.Forte@Sun.COM 			/* de-register with proxy module */
313610725SJohn.Forte@Sun.COM 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
313710725SJohn.Forte@Sun.COM 			stmf_ic_msg_t *ic_dereg_lun;
313810725SJohn.Forte@Sun.COM 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
313910725SJohn.Forte@Sun.COM 			    lu->lu_lp->lp_alua_support) {
314010725SJohn.Forte@Sun.COM 				ilu->ilu_alua = 1;
314110725SJohn.Forte@Sun.COM 				/* allocate the de-register message */
314210725SJohn.Forte@Sun.COM 				ic_dereg_lun = ic_dereg_lun_msg_alloc(
314310725SJohn.Forte@Sun.COM 				    lu->lu_id->ident, lu->lu_lp->lp_name, 0,
314410725SJohn.Forte@Sun.COM 				    NULL, stmf_proxy_msg_id);
314510725SJohn.Forte@Sun.COM 				/* send the message */
314610725SJohn.Forte@Sun.COM 				if (ic_dereg_lun) {
314710725SJohn.Forte@Sun.COM 					ic_ret = ic_tx_msg(ic_dereg_lun);
314810725SJohn.Forte@Sun.COM 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
314910725SJohn.Forte@Sun.COM 						stmf_proxy_msg_id++;
315010725SJohn.Forte@Sun.COM 					}
315110725SJohn.Forte@Sun.COM 				}
315210725SJohn.Forte@Sun.COM 			}
315310725SJohn.Forte@Sun.COM 		}
31547836SJohn.Forte@Sun.COM 
31557836SJohn.Forte@Sun.COM 		if (ilu->ilu_next)
31567836SJohn.Forte@Sun.COM 			ilu->ilu_next->ilu_prev = ilu->ilu_prev;
31577836SJohn.Forte@Sun.COM 		if (ilu->ilu_prev)
31587836SJohn.Forte@Sun.COM 			ilu->ilu_prev->ilu_next = ilu->ilu_next;
31597836SJohn.Forte@Sun.COM 		else
31607836SJohn.Forte@Sun.COM 			stmf_state.stmf_ilulist = ilu->ilu_next;
31617836SJohn.Forte@Sun.COM 		stmf_state.stmf_nlus--;
31627836SJohn.Forte@Sun.COM 
31637836SJohn.Forte@Sun.COM 		if (ilu == stmf_state.stmf_svc_ilu_draining) {
31647836SJohn.Forte@Sun.COM 			stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
31657836SJohn.Forte@Sun.COM 		}
31667836SJohn.Forte@Sun.COM 		if (ilu == stmf_state.stmf_svc_ilu_timing) {
31677836SJohn.Forte@Sun.COM 			stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
31687836SJohn.Forte@Sun.COM 		}
31697836SJohn.Forte@Sun.COM 		if (lu->lu_lp) {
31707836SJohn.Forte@Sun.COM 			((stmf_i_lu_provider_t *)
31717836SJohn.Forte@Sun.COM 			    (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
31727836SJohn.Forte@Sun.COM 		}
31737836SJohn.Forte@Sun.COM 		if (ilu->ilu_luid) {
31747836SJohn.Forte@Sun.COM 			((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
31757836SJohn.Forte@Sun.COM 			    NULL;
31767836SJohn.Forte@Sun.COM 			ilu->ilu_luid = NULL;
31777836SJohn.Forte@Sun.COM 		}
31787836SJohn.Forte@Sun.COM 		STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
31797836SJohn.Forte@Sun.COM 	} else {
31807836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
31817836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
31827836SJohn.Forte@Sun.COM 	}
31839435STim.Szeto@Sun.COM 	if (ilu->ilu_kstat_info) {
318410421STim.Szeto@Sun.COM 		kmem_free(ilu->ilu_kstat_info->ks_data,
318510421STim.Szeto@Sun.COM 		    ilu->ilu_kstat_info->ks_data_size);
31869435STim.Szeto@Sun.COM 		kstat_delete(ilu->ilu_kstat_info);
31879435STim.Szeto@Sun.COM 	}
31889435STim.Szeto@Sun.COM 	if (ilu->ilu_kstat_io) {
31899435STim.Szeto@Sun.COM 		kstat_delete(ilu->ilu_kstat_io);
31909435STim.Szeto@Sun.COM 		mutex_destroy(&ilu->ilu_kstat_lock);
31919435STim.Szeto@Sun.COM 	}
319211773STim.Szeto@Sun.COM 	stmf_delete_itl_kstat_by_guid(ilu->ilu_ascii_hex_guid);
31937836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
31947836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
31957836SJohn.Forte@Sun.COM }
31967836SJohn.Forte@Sun.COM 
319710725SJohn.Forte@Sun.COM void
stmf_set_port_standby(stmf_local_port_t * lport,uint16_t rtpid)319810725SJohn.Forte@Sun.COM stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid)
319910725SJohn.Forte@Sun.COM {
320010725SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport =
320110725SJohn.Forte@Sun.COM 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
320210725SJohn.Forte@Sun.COM 	ilport->ilport_rtpid = rtpid;
320310725SJohn.Forte@Sun.COM 	ilport->ilport_standby = 1;
320410725SJohn.Forte@Sun.COM }
320510725SJohn.Forte@Sun.COM 
320611116SJohn.Forte@Sun.COM void
stmf_set_port_alua(stmf_local_port_t * lport)320711116SJohn.Forte@Sun.COM stmf_set_port_alua(stmf_local_port_t *lport)
320811116SJohn.Forte@Sun.COM {
320911116SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport =
321011116SJohn.Forte@Sun.COM 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
321111116SJohn.Forte@Sun.COM 	ilport->ilport_alua = 1;
321211116SJohn.Forte@Sun.COM }
321311116SJohn.Forte@Sun.COM 
32147836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_local_port(stmf_local_port_t * lport)32157836SJohn.Forte@Sun.COM stmf_register_local_port(stmf_local_port_t *lport)
32167836SJohn.Forte@Sun.COM {
32177836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
32187836SJohn.Forte@Sun.COM 	stmf_state_change_info_t ssci;
32197836SJohn.Forte@Sun.COM 	int start_workers = 0;
32207836SJohn.Forte@Sun.COM 
32217836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
32227836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
32237836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
32247836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
32257836SJohn.Forte@Sun.COM 	}
32267836SJohn.Forte@Sun.COM 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
32277836SJohn.Forte@Sun.COM 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
32287836SJohn.Forte@Sun.COM 
322911773STim.Szeto@Sun.COM 	ilport->ilport_instance =
323011773STim.Szeto@Sun.COM 	    id_alloc_nosleep(stmf_state.stmf_ilport_inst_space);
323111773STim.Szeto@Sun.COM 	if (ilport->ilport_instance == -1) {
323211773STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
323311773STim.Szeto@Sun.COM 		return (STMF_FAILURE);
323411773STim.Szeto@Sun.COM 	}
32357836SJohn.Forte@Sun.COM 	ilport->ilport_next = stmf_state.stmf_ilportlist;
32367836SJohn.Forte@Sun.COM 	ilport->ilport_prev = NULL;
32377836SJohn.Forte@Sun.COM 	if (ilport->ilport_next)
32387836SJohn.Forte@Sun.COM 		ilport->ilport_next->ilport_prev = ilport;
32397836SJohn.Forte@Sun.COM 	stmf_state.stmf_ilportlist = ilport;
32407836SJohn.Forte@Sun.COM 	stmf_state.stmf_nlports++;
32417836SJohn.Forte@Sun.COM 	if (lport->lport_pp) {
32427836SJohn.Forte@Sun.COM 		((stmf_i_port_provider_t *)
32437836SJohn.Forte@Sun.COM 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
32447836SJohn.Forte@Sun.COM 	}
32457836SJohn.Forte@Sun.COM 	ilport->ilport_tg =
32467836SJohn.Forte@Sun.COM 	    stmf_lookup_group_for_target(lport->lport_id->ident,
32478662SJordan.Vaughan@Sun.com 	    lport->lport_id->ident_length);
324810725SJohn.Forte@Sun.COM 
324910725SJohn.Forte@Sun.COM 	/*
325010725SJohn.Forte@Sun.COM 	 * rtpid will/must be set if this is a standby port
325110725SJohn.Forte@Sun.COM 	 * only register ports that are not standby (proxy) ports
325211116SJohn.Forte@Sun.COM 	 * and ports that are alua participants (ilport_alua == 1)
325310725SJohn.Forte@Sun.COM 	 */
325410725SJohn.Forte@Sun.COM 	if (ilport->ilport_standby == 0) {
325510725SJohn.Forte@Sun.COM 		ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1);
325610725SJohn.Forte@Sun.COM 	}
325710725SJohn.Forte@Sun.COM 
325810725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state == 1 &&
325911116SJohn.Forte@Sun.COM 	    ilport->ilport_standby == 0 &&
326011116SJohn.Forte@Sun.COM 	    ilport->ilport_alua == 1) {
326110725SJohn.Forte@Sun.COM 		stmf_ic_msg_t *ic_reg_port;
326210725SJohn.Forte@Sun.COM 		stmf_ic_msg_status_t ic_ret;
326310725SJohn.Forte@Sun.COM 		stmf_local_port_t *lport;
326410725SJohn.Forte@Sun.COM 		lport = ilport->ilport_lport;
326510725SJohn.Forte@Sun.COM 		ic_reg_port = ic_reg_port_msg_alloc(
326610725SJohn.Forte@Sun.COM 		    lport->lport_id, ilport->ilport_rtpid,
326710725SJohn.Forte@Sun.COM 		    0, NULL, stmf_proxy_msg_id);
326810725SJohn.Forte@Sun.COM 		if (ic_reg_port) {
326910725SJohn.Forte@Sun.COM 			ic_ret = ic_tx_msg(ic_reg_port);
327010725SJohn.Forte@Sun.COM 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
327110725SJohn.Forte@Sun.COM 				ilport->ilport_reg_msgid = stmf_proxy_msg_id++;
327210725SJohn.Forte@Sun.COM 			} else {
327310725SJohn.Forte@Sun.COM 				cmn_err(CE_WARN, "error on port registration "
327410725SJohn.Forte@Sun.COM 				"port - %s", ilport->ilport_kstat_tgt_name);
327510725SJohn.Forte@Sun.COM 			}
327610725SJohn.Forte@Sun.COM 		}
327710725SJohn.Forte@Sun.COM 	}
32787836SJohn.Forte@Sun.COM 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
32799435STim.Szeto@Sun.COM 	stmf_create_kstat_lport(ilport);
32807836SJohn.Forte@Sun.COM 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
32817836SJohn.Forte@Sun.COM 		stmf_workers_state = STMF_WORKERS_ENABLING;
32827836SJohn.Forte@Sun.COM 		start_workers = 1;
32837836SJohn.Forte@Sun.COM 	}
32847836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
32857836SJohn.Forte@Sun.COM 
32867836SJohn.Forte@Sun.COM 	if (start_workers)
32877836SJohn.Forte@Sun.COM 		stmf_worker_init();
32887836SJohn.Forte@Sun.COM 
328912682SSrivijitha.Dugganapalli@Sun.COM 	/*  the default state of LPORT */
329012682SSrivijitha.Dugganapalli@Sun.COM 
329112682SSrivijitha.Dugganapalli@Sun.COM 	if (stmf_state.stmf_default_lport_state == STMF_STATE_OFFLINE) {
329212682SSrivijitha.Dugganapalli@Sun.COM 		ilport->ilport_prev_state = STMF_STATE_OFFLINE;
329312682SSrivijitha.Dugganapalli@Sun.COM 	} else {
329412682SSrivijitha.Dugganapalli@Sun.COM 		ilport->ilport_prev_state = STMF_STATE_ONLINE;
329512682SSrivijitha.Dugganapalli@Sun.COM 		if (stmf_state.stmf_service_running) {
329612682SSrivijitha.Dugganapalli@Sun.COM 			ssci.st_rflags = 0;
329712682SSrivijitha.Dugganapalli@Sun.COM 			ssci.st_additional_info = NULL;
329812682SSrivijitha.Dugganapalli@Sun.COM 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
329912682SSrivijitha.Dugganapalli@Sun.COM 		}
33007836SJohn.Forte@Sun.COM 	}
33017836SJohn.Forte@Sun.COM 
33027836SJohn.Forte@Sun.COM 	/* XXX: Generate event */
33037836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
33047836SJohn.Forte@Sun.COM }
33057836SJohn.Forte@Sun.COM 
33067836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_local_port(stmf_local_port_t * lport)33077836SJohn.Forte@Sun.COM stmf_deregister_local_port(stmf_local_port_t *lport)
33087836SJohn.Forte@Sun.COM {
33097836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
33107836SJohn.Forte@Sun.COM 
33117836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
33127836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_inventory_locked) {
33137836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
33147836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
33157836SJohn.Forte@Sun.COM 	}
331610725SJohn.Forte@Sun.COM 
33177836SJohn.Forte@Sun.COM 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
331810725SJohn.Forte@Sun.COM 
331910725SJohn.Forte@Sun.COM 	/*
332010725SJohn.Forte@Sun.COM 	 * deregister ports that are not standby (proxy)
332110725SJohn.Forte@Sun.COM 	 */
332210725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state == 1 &&
332311116SJohn.Forte@Sun.COM 	    ilport->ilport_standby == 0 &&
332411116SJohn.Forte@Sun.COM 	    ilport->ilport_alua == 1) {
332510725SJohn.Forte@Sun.COM 		stmf_ic_msg_t *ic_dereg_port;
332610725SJohn.Forte@Sun.COM 		stmf_ic_msg_status_t ic_ret;
332710725SJohn.Forte@Sun.COM 		ic_dereg_port = ic_dereg_port_msg_alloc(
332810725SJohn.Forte@Sun.COM 		    lport->lport_id, 0, NULL, stmf_proxy_msg_id);
332910725SJohn.Forte@Sun.COM 		if (ic_dereg_port) {
333010725SJohn.Forte@Sun.COM 			ic_ret = ic_tx_msg(ic_dereg_port);
333110725SJohn.Forte@Sun.COM 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
333210725SJohn.Forte@Sun.COM 				stmf_proxy_msg_id++;
333310725SJohn.Forte@Sun.COM 			}
333410725SJohn.Forte@Sun.COM 		}
333510725SJohn.Forte@Sun.COM 	}
333610725SJohn.Forte@Sun.COM 
33377836SJohn.Forte@Sun.COM 	if (ilport->ilport_nsessions == 0) {
33387836SJohn.Forte@Sun.COM 		if (ilport->ilport_next)
33397836SJohn.Forte@Sun.COM 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
33407836SJohn.Forte@Sun.COM 		if (ilport->ilport_prev)
33417836SJohn.Forte@Sun.COM 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
33427836SJohn.Forte@Sun.COM 		else
33437836SJohn.Forte@Sun.COM 			stmf_state.stmf_ilportlist = ilport->ilport_next;
334411773STim.Szeto@Sun.COM 		id_free(stmf_state.stmf_ilport_inst_space,
334511773STim.Szeto@Sun.COM 		    ilport->ilport_instance);
33467836SJohn.Forte@Sun.COM 		rw_destroy(&ilport->ilport_lock);
33477836SJohn.Forte@Sun.COM 		stmf_state.stmf_nlports--;
33487836SJohn.Forte@Sun.COM 		if (lport->lport_pp) {
33497836SJohn.Forte@Sun.COM 			((stmf_i_port_provider_t *)
33507836SJohn.Forte@Sun.COM 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
33517836SJohn.Forte@Sun.COM 		}
33527836SJohn.Forte@Sun.COM 		ilport->ilport_tg = NULL;
33537836SJohn.Forte@Sun.COM 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
33547836SJohn.Forte@Sun.COM 	} else {
33557836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
33567836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
33577836SJohn.Forte@Sun.COM 	}
33589435STim.Szeto@Sun.COM 	if (ilport->ilport_kstat_info) {
335910421STim.Szeto@Sun.COM 		kmem_free(ilport->ilport_kstat_info->ks_data,
336010421STim.Szeto@Sun.COM 		    ilport->ilport_kstat_info->ks_data_size);
33619435STim.Szeto@Sun.COM 		kstat_delete(ilport->ilport_kstat_info);
33629435STim.Szeto@Sun.COM 	}
33639435STim.Szeto@Sun.COM 	if (ilport->ilport_kstat_io) {
33649435STim.Szeto@Sun.COM 		kstat_delete(ilport->ilport_kstat_io);
33659435STim.Szeto@Sun.COM 		mutex_destroy(&ilport->ilport_kstat_lock);
33669435STim.Szeto@Sun.COM 	}
336711773STim.Szeto@Sun.COM 	stmf_delete_itl_kstat_by_lport(ilport->ilport_kstat_tgt_name);
33687836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
33697836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
33707836SJohn.Forte@Sun.COM }
33717836SJohn.Forte@Sun.COM 
33727836SJohn.Forte@Sun.COM /*
337311773STim.Szeto@Sun.COM  * Rport id/instance mappings remain valid until STMF is unloaded
337411773STim.Szeto@Sun.COM  */
337511773STim.Szeto@Sun.COM static int
stmf_irport_compare(const void * void_irport1,const void * void_irport2)337611773STim.Szeto@Sun.COM stmf_irport_compare(const void *void_irport1, const void *void_irport2)
337711773STim.Szeto@Sun.COM {
337811773STim.Szeto@Sun.COM 	const	stmf_i_remote_port_t	*irport1 = void_irport1;
337911773STim.Szeto@Sun.COM 	const	stmf_i_remote_port_t	*irport2 = void_irport2;
338011773STim.Szeto@Sun.COM 	int			result;
338111773STim.Szeto@Sun.COM 
338211773STim.Szeto@Sun.COM 	/* Sort by code set then ident */
338311773STim.Szeto@Sun.COM 	if (irport1->irport_id->code_set <
338411773STim.Szeto@Sun.COM 	    irport2->irport_id->code_set) {
338511773STim.Szeto@Sun.COM 		return (-1);
338611773STim.Szeto@Sun.COM 	} else if (irport1->irport_id->code_set >
338711773STim.Szeto@Sun.COM 	    irport2->irport_id->code_set) {
338811773STim.Szeto@Sun.COM 		return (1);
338911773STim.Szeto@Sun.COM 	}
339011773STim.Szeto@Sun.COM 
339111773STim.Szeto@Sun.COM 	/* Next by ident length */
339211773STim.Szeto@Sun.COM 	if (irport1->irport_id->ident_length <
339311773STim.Szeto@Sun.COM 	    irport2->irport_id->ident_length) {
339411773STim.Szeto@Sun.COM 		return (-1);
339511773STim.Szeto@Sun.COM 	} else if (irport1->irport_id->ident_length >
339611773STim.Szeto@Sun.COM 	    irport2->irport_id->ident_length) {
339711773STim.Szeto@Sun.COM 		return (1);
339811773STim.Szeto@Sun.COM 	}
339911773STim.Szeto@Sun.COM 
340011773STim.Szeto@Sun.COM 	/* Code set and ident length both match, now compare idents */
340111773STim.Szeto@Sun.COM 	result = memcmp(irport1->irport_id->ident,
340211773STim.Szeto@Sun.COM 	    irport2->irport_id->ident,
340311773STim.Szeto@Sun.COM 	    irport1->irport_id->ident_length);
340411773STim.Szeto@Sun.COM 
340511773STim.Szeto@Sun.COM 	if (result < 0) {
340611773STim.Szeto@Sun.COM 		return (-1);
340711773STim.Szeto@Sun.COM 	} else if (result > 0) {
340811773STim.Szeto@Sun.COM 		return (1);
340911773STim.Szeto@Sun.COM 	}
341011773STim.Szeto@Sun.COM 
341111773STim.Szeto@Sun.COM 	return (0);
341211773STim.Szeto@Sun.COM }
341311773STim.Szeto@Sun.COM 
341411773STim.Szeto@Sun.COM static stmf_i_remote_port_t *
stmf_irport_create(scsi_devid_desc_t * rport_devid)341511773STim.Szeto@Sun.COM stmf_irport_create(scsi_devid_desc_t *rport_devid)
341611773STim.Szeto@Sun.COM {
341711773STim.Szeto@Sun.COM 	int			alloc_len;
341811773STim.Szeto@Sun.COM 	stmf_i_remote_port_t	*irport;
341911773STim.Szeto@Sun.COM 
342011773STim.Szeto@Sun.COM 	/*
342111773STim.Szeto@Sun.COM 	 * Lookup will bump the refcnt if there's an existing rport
342211773STim.Szeto@Sun.COM 	 * context for this identifier.
342311773STim.Szeto@Sun.COM 	 */
342411773STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
342511773STim.Szeto@Sun.COM 
342611773STim.Szeto@Sun.COM 	alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) +
342711773STim.Szeto@Sun.COM 	    rport_devid->ident_length - 1;
342811773STim.Szeto@Sun.COM 	irport = kmem_zalloc(alloc_len, KM_NOSLEEP);
342911773STim.Szeto@Sun.COM 	if (irport == NULL) {
343011773STim.Szeto@Sun.COM 		return (NULL);
343111773STim.Szeto@Sun.COM 	}
343211773STim.Szeto@Sun.COM 
343311773STim.Szeto@Sun.COM 	irport->irport_instance =
343411773STim.Szeto@Sun.COM 	    id_alloc_nosleep(stmf_state.stmf_irport_inst_space);
343511773STim.Szeto@Sun.COM 	if (irport->irport_instance == -1) {
343611773STim.Szeto@Sun.COM 		kmem_free(irport, alloc_len);
343711773STim.Szeto@Sun.COM 		return (NULL);
343811773STim.Szeto@Sun.COM 	}
343911773STim.Szeto@Sun.COM 
344011773STim.Szeto@Sun.COM 	irport->irport_id =
344111773STim.Szeto@Sun.COM 	    (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */
344211773STim.Szeto@Sun.COM 	bcopy(rport_devid, irport->irport_id,
344311773STim.Szeto@Sun.COM 	    sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1);
344411773STim.Szeto@Sun.COM 	irport->irport_refcnt = 1;
344511773STim.Szeto@Sun.COM 	mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL);
344611773STim.Szeto@Sun.COM 
344711773STim.Szeto@Sun.COM 	return (irport);
344811773STim.Szeto@Sun.COM }
344911773STim.Szeto@Sun.COM 
345011773STim.Szeto@Sun.COM static void
stmf_irport_destroy(stmf_i_remote_port_t * irport)345111773STim.Szeto@Sun.COM stmf_irport_destroy(stmf_i_remote_port_t *irport)
345211773STim.Szeto@Sun.COM {
345311773STim.Szeto@Sun.COM 	id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance);
345411773STim.Szeto@Sun.COM 	mutex_destroy(&irport->irport_mutex);
345511773STim.Szeto@Sun.COM 	kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) +
345611773STim.Szeto@Sun.COM 	    irport->irport_id->ident_length - 1);
345711773STim.Szeto@Sun.COM }
345811773STim.Szeto@Sun.COM 
345911773STim.Szeto@Sun.COM static stmf_i_remote_port_t *
stmf_irport_register(scsi_devid_desc_t * rport_devid)346011773STim.Szeto@Sun.COM stmf_irport_register(scsi_devid_desc_t *rport_devid)
346111773STim.Szeto@Sun.COM {
346211773STim.Szeto@Sun.COM 	stmf_i_remote_port_t	*irport;
346311773STim.Szeto@Sun.COM 
346411773STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
346511773STim.Szeto@Sun.COM 
346611773STim.Szeto@Sun.COM 	/*
346711773STim.Szeto@Sun.COM 	 * Lookup will bump the refcnt if there's an existing rport
346811773STim.Szeto@Sun.COM 	 * context for this identifier.
346911773STim.Szeto@Sun.COM 	 */
347011773STim.Szeto@Sun.COM 	if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) {
347111773STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
347211773STim.Szeto@Sun.COM 		return (irport);
347311773STim.Szeto@Sun.COM 	}
347411773STim.Szeto@Sun.COM 
347511773STim.Szeto@Sun.COM 	irport = stmf_irport_create(rport_devid);
347611773STim.Szeto@Sun.COM 	if (irport == NULL) {
347711773STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
347811773STim.Szeto@Sun.COM 		return (NULL);
347911773STim.Szeto@Sun.COM 	}
348011773STim.Szeto@Sun.COM 
348111773STim.Szeto@Sun.COM 	avl_add(&stmf_state.stmf_irportlist, irport);
348211773STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
348311773STim.Szeto@Sun.COM 
348411773STim.Szeto@Sun.COM 	return (irport);
348511773STim.Szeto@Sun.COM }
348611773STim.Szeto@Sun.COM 
348711773STim.Szeto@Sun.COM static stmf_i_remote_port_t *
stmf_irport_lookup_locked(scsi_devid_desc_t * rport_devid)348811773STim.Szeto@Sun.COM stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid)
348911773STim.Szeto@Sun.COM {
349011773STim.Szeto@Sun.COM 	stmf_i_remote_port_t	*irport;
349111773STim.Szeto@Sun.COM 	stmf_i_remote_port_t	tmp_irport;
349211773STim.Szeto@Sun.COM 
349311773STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
349411773STim.Szeto@Sun.COM 	tmp_irport.irport_id = rport_devid;
349511773STim.Szeto@Sun.COM 	irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL);
349611773STim.Szeto@Sun.COM 	if (irport != NULL) {
349711773STim.Szeto@Sun.COM 		mutex_enter(&irport->irport_mutex);
349811773STim.Szeto@Sun.COM 		irport->irport_refcnt++;
349911773STim.Szeto@Sun.COM 		mutex_exit(&irport->irport_mutex);
350011773STim.Szeto@Sun.COM 	}
350111773STim.Szeto@Sun.COM 
350211773STim.Szeto@Sun.COM 	return (irport);
350311773STim.Szeto@Sun.COM }
350411773STim.Szeto@Sun.COM 
350511773STim.Szeto@Sun.COM static void
stmf_irport_deregister(stmf_i_remote_port_t * irport)350611773STim.Szeto@Sun.COM stmf_irport_deregister(stmf_i_remote_port_t *irport)
350711773STim.Szeto@Sun.COM {
350811773STim.Szeto@Sun.COM 	/*
350911773STim.Szeto@Sun.COM 	 * If we were actually going to remove unreferenced remote ports
351011773STim.Szeto@Sun.COM 	 * we would want to acquire stmf_state.stmf_lock before getting
351111773STim.Szeto@Sun.COM 	 * the irport mutex.
351211773STim.Szeto@Sun.COM 	 *
351311773STim.Szeto@Sun.COM 	 * Instead we're just going to leave it there even if unreferenced.
351411773STim.Szeto@Sun.COM 	 */
351511773STim.Szeto@Sun.COM 	mutex_enter(&irport->irport_mutex);
351611773STim.Szeto@Sun.COM 	irport->irport_refcnt--;
351711773STim.Szeto@Sun.COM 	mutex_exit(&irport->irport_mutex);
351811773STim.Szeto@Sun.COM }
351911773STim.Szeto@Sun.COM 
352011773STim.Szeto@Sun.COM /*
35217836SJohn.Forte@Sun.COM  * Port provider has to make sure that register/deregister session and
35227836SJohn.Forte@Sun.COM  * port are serialized calls.
35237836SJohn.Forte@Sun.COM  */
35247836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_scsi_session(stmf_local_port_t * lport,stmf_scsi_session_t * ss)35257836SJohn.Forte@Sun.COM stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
35267836SJohn.Forte@Sun.COM {
35277836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
35287836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
35297836SJohn.Forte@Sun.COM 	    lport->lport_stmf_private;
35307836SJohn.Forte@Sun.COM 	uint8_t		lun[8];
35317836SJohn.Forte@Sun.COM 
35327836SJohn.Forte@Sun.COM 	/*
35337836SJohn.Forte@Sun.COM 	 * Port state has to be online to register a scsi session. It is
35347836SJohn.Forte@Sun.COM 	 * possible that we started an offline operation and a new SCSI
35357836SJohn.Forte@Sun.COM 	 * session started at the same time (in that case also we are going
35367836SJohn.Forte@Sun.COM 	 * to fail the registeration). But any other state is simply
35377836SJohn.Forte@Sun.COM 	 * a bad port provider implementation.
35387836SJohn.Forte@Sun.COM 	 */
35397836SJohn.Forte@Sun.COM 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
35407836SJohn.Forte@Sun.COM 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
35417836SJohn.Forte@Sun.COM 			stmf_trace(lport->lport_alias, "Port is trying to "
35427836SJohn.Forte@Sun.COM 			    "register a session while the state is neither "
35437836SJohn.Forte@Sun.COM 			    "online nor offlining");
35447836SJohn.Forte@Sun.COM 		}
35457836SJohn.Forte@Sun.COM 		return (STMF_FAILURE);
35467836SJohn.Forte@Sun.COM 	}
35477836SJohn.Forte@Sun.COM 	bzero(lun, 8);
35487836SJohn.Forte@Sun.COM 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
354911773STim.Szeto@Sun.COM 	if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) {
355011773STim.Szeto@Sun.COM 		stmf_trace(lport->lport_alias, "Could not register "
355111773STim.Szeto@Sun.COM 		    "remote port during session registration");
355211773STim.Szeto@Sun.COM 		return (STMF_FAILURE);
355311773STim.Szeto@Sun.COM 	}
355411773STim.Szeto@Sun.COM 
35557836SJohn.Forte@Sun.COM 	iss->iss_flags |= ISS_BEING_CREATED;
35567836SJohn.Forte@Sun.COM 
3557*12750SNattuvetty.Bhavyan@Sun.COM 	if (ss->ss_rport == NULL) {
3558*12750SNattuvetty.Bhavyan@Sun.COM 		iss->iss_flags |= ISS_NULL_TPTID;
3559*12750SNattuvetty.Bhavyan@Sun.COM 		ss->ss_rport = stmf_scsilib_devid_to_remote_port(
3560*12750SNattuvetty.Bhavyan@Sun.COM 		    ss->ss_rport_id);
3561*12750SNattuvetty.Bhavyan@Sun.COM 		if (ss->ss_rport == NULL) {
3562*12750SNattuvetty.Bhavyan@Sun.COM 			iss->iss_flags &= ~(ISS_NULL_TPTID | ISS_BEING_CREATED);
3563*12750SNattuvetty.Bhavyan@Sun.COM 			stmf_trace(lport->lport_alias, "Device id to "
3564*12750SNattuvetty.Bhavyan@Sun.COM 			    "remote port conversion failed");
3565*12750SNattuvetty.Bhavyan@Sun.COM 			return (STMF_FAILURE);
3566*12750SNattuvetty.Bhavyan@Sun.COM 		}
3567*12750SNattuvetty.Bhavyan@Sun.COM 	} else {
3568*12750SNattuvetty.Bhavyan@Sun.COM 		if (!stmf_scsilib_tptid_validate(ss->ss_rport->rport_tptid,
3569*12750SNattuvetty.Bhavyan@Sun.COM 		    ss->ss_rport->rport_tptid_sz, NULL)) {
3570*12750SNattuvetty.Bhavyan@Sun.COM 			iss->iss_flags &= ~ISS_BEING_CREATED;
3571*12750SNattuvetty.Bhavyan@Sun.COM 			stmf_trace(lport->lport_alias, "Remote port "
3572*12750SNattuvetty.Bhavyan@Sun.COM 			    "transport id validation failed");
3573*12750SNattuvetty.Bhavyan@Sun.COM 			return (STMF_FAILURE);
3574*12750SNattuvetty.Bhavyan@Sun.COM 		}
3575*12750SNattuvetty.Bhavyan@Sun.COM 	}
3576*12750SNattuvetty.Bhavyan@Sun.COM 
35777836SJohn.Forte@Sun.COM 	/* sessions use the ilport_lock. No separate lock is required */
35787836SJohn.Forte@Sun.COM 	iss->iss_lockp = &ilport->ilport_lock;
357911978STim.Szeto@Sun.COM 
358011978STim.Szeto@Sun.COM 	if (iss->iss_sm != NULL)
358111978STim.Szeto@Sun.COM 		cmn_err(CE_PANIC, "create lun map called with non NULL map");
358211978STim.Szeto@Sun.COM 	iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t),
358311978STim.Szeto@Sun.COM 	    KM_SLEEP);
358411978STim.Szeto@Sun.COM 
358511978STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
358611978STim.Szeto@Sun.COM 	rw_enter(&ilport->ilport_lock, RW_WRITER);
35877836SJohn.Forte@Sun.COM 	(void) stmf_session_create_lun_map(ilport, iss);
35887836SJohn.Forte@Sun.COM 	ilport->ilport_nsessions++;
35897836SJohn.Forte@Sun.COM 	iss->iss_next = ilport->ilport_ss_list;
35907836SJohn.Forte@Sun.COM 	ilport->ilport_ss_list = iss;
35917836SJohn.Forte@Sun.COM 	rw_exit(&ilport->ilport_lock);
359211978STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
35937836SJohn.Forte@Sun.COM 
35947836SJohn.Forte@Sun.COM 	iss->iss_creation_time = ddi_get_time();
35957836SJohn.Forte@Sun.COM 	ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1);
35967836SJohn.Forte@Sun.COM 	iss->iss_flags &= ~ISS_BEING_CREATED;
359710725SJohn.Forte@Sun.COM 	/* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */
359810725SJohn.Forte@Sun.COM 	iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED;
35998859STim.Szeto@Sun.COM 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
36008859STim.Szeto@Sun.COM 	    stmf_scsi_session_t *, ss);
36017836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
36027836SJohn.Forte@Sun.COM }
36037836SJohn.Forte@Sun.COM 
36047836SJohn.Forte@Sun.COM void
stmf_deregister_scsi_session(stmf_local_port_t * lport,stmf_scsi_session_t * ss)36057836SJohn.Forte@Sun.COM stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
36067836SJohn.Forte@Sun.COM {
36077836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
36088662SJordan.Vaughan@Sun.com 	    lport->lport_stmf_private;
36097836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss, **ppss;
36107836SJohn.Forte@Sun.COM 	int found = 0;
361110725SJohn.Forte@Sun.COM 	stmf_ic_msg_t *ic_session_dereg;
361210725SJohn.Forte@Sun.COM 	stmf_status_t ic_ret = STMF_FAILURE;
36137836SJohn.Forte@Sun.COM 
36148859STim.Szeto@Sun.COM 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
36158859STim.Szeto@Sun.COM 	    stmf_scsi_session_t *, ss);
36168859STim.Szeto@Sun.COM 
36177836SJohn.Forte@Sun.COM 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
36187836SJohn.Forte@Sun.COM 	if (ss->ss_rport_alias) {
36197836SJohn.Forte@Sun.COM 		ss->ss_rport_alias = NULL;
36207836SJohn.Forte@Sun.COM 	}
36217836SJohn.Forte@Sun.COM 
36227836SJohn.Forte@Sun.COM try_dereg_ss_again:
36237836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
36247836SJohn.Forte@Sun.COM 	atomic_and_32(&iss->iss_flags,
36258662SJordan.Vaughan@Sun.com 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
36267836SJohn.Forte@Sun.COM 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
36277836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
36287836SJohn.Forte@Sun.COM 		delay(1);
36297836SJohn.Forte@Sun.COM 		goto try_dereg_ss_again;
36307836SJohn.Forte@Sun.COM 	}
363110725SJohn.Forte@Sun.COM 
363210725SJohn.Forte@Sun.COM 	/* dereg proxy session if not standby port */
363311116SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_state == 1 &&
363411116SJohn.Forte@Sun.COM 	    ilport->ilport_standby == 0 &&
363511116SJohn.Forte@Sun.COM 	    ilport->ilport_alua == 1) {
363610725SJohn.Forte@Sun.COM 		ic_session_dereg = ic_session_dereg_msg_alloc(
363710725SJohn.Forte@Sun.COM 		    ss, stmf_proxy_msg_id);
363810725SJohn.Forte@Sun.COM 		if (ic_session_dereg) {
363910725SJohn.Forte@Sun.COM 			ic_ret = ic_tx_msg(ic_session_dereg);
364010725SJohn.Forte@Sun.COM 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
364110725SJohn.Forte@Sun.COM 				stmf_proxy_msg_id++;
364210725SJohn.Forte@Sun.COM 			}
364310725SJohn.Forte@Sun.COM 		}
364410725SJohn.Forte@Sun.COM 	}
364510725SJohn.Forte@Sun.COM 
36467836SJohn.Forte@Sun.COM 	rw_enter(&ilport->ilport_lock, RW_WRITER);
36477836SJohn.Forte@Sun.COM 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
36487836SJohn.Forte@Sun.COM 	    ppss = &((*ppss)->iss_next)) {
36497836SJohn.Forte@Sun.COM 		if (iss == (*ppss)) {
36507836SJohn.Forte@Sun.COM 			*ppss = (*ppss)->iss_next;
36517836SJohn.Forte@Sun.COM 			found = 1;
36527836SJohn.Forte@Sun.COM 			break;
36537836SJohn.Forte@Sun.COM 		}
36547836SJohn.Forte@Sun.COM 	}
36557836SJohn.Forte@Sun.COM 	if (!found) {
36567836SJohn.Forte@Sun.COM 		cmn_err(CE_PANIC, "Deregister session called for non existent"
36577836SJohn.Forte@Sun.COM 		    " session");
36587836SJohn.Forte@Sun.COM 	}
36597836SJohn.Forte@Sun.COM 	ilport->ilport_nsessions--;
36607836SJohn.Forte@Sun.COM 
366111773STim.Szeto@Sun.COM 	stmf_irport_deregister(iss->iss_irport);
36627836SJohn.Forte@Sun.COM 	(void) stmf_session_destroy_lun_map(ilport, iss);
366311978STim.Szeto@Sun.COM 	rw_exit(&ilport->ilport_lock);
366411978STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
3665*12750SNattuvetty.Bhavyan@Sun.COM 
3666*12750SNattuvetty.Bhavyan@Sun.COM 	if (iss->iss_flags & ISS_NULL_TPTID) {
3667*12750SNattuvetty.Bhavyan@Sun.COM 		stmf_remote_port_free(ss->ss_rport);
3668*12750SNattuvetty.Bhavyan@Sun.COM 	}
36697836SJohn.Forte@Sun.COM }
36707836SJohn.Forte@Sun.COM 
36717836SJohn.Forte@Sun.COM stmf_i_scsi_session_t *
stmf_session_id_to_issptr(uint64_t session_id,int stay_locked)36727836SJohn.Forte@Sun.COM stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
36737836SJohn.Forte@Sun.COM {
36747836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
36757836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
36767836SJohn.Forte@Sun.COM 
36777836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
36787836SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
36798662SJordan.Vaughan@Sun.com 	    ilport = ilport->ilport_next) {
36807836SJohn.Forte@Sun.COM 		rw_enter(&ilport->ilport_lock, RW_WRITER);
36817836SJohn.Forte@Sun.COM 		for (iss = ilport->ilport_ss_list; iss != NULL;
36828662SJordan.Vaughan@Sun.com 		    iss = iss->iss_next) {
36837836SJohn.Forte@Sun.COM 			if (iss->iss_ss->ss_session_id == session_id) {
36847836SJohn.Forte@Sun.COM 				if (!stay_locked)
36857836SJohn.Forte@Sun.COM 					rw_exit(&ilport->ilport_lock);
36867836SJohn.Forte@Sun.COM 				mutex_exit(&stmf_state.stmf_lock);
36877836SJohn.Forte@Sun.COM 				return (iss);
36887836SJohn.Forte@Sun.COM 			}
36897836SJohn.Forte@Sun.COM 		}
36907836SJohn.Forte@Sun.COM 		rw_exit(&ilport->ilport_lock);
36917836SJohn.Forte@Sun.COM 	}
36927836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
36937836SJohn.Forte@Sun.COM 	return (NULL);
36947836SJohn.Forte@Sun.COM }
36957836SJohn.Forte@Sun.COM 
369611773STim.Szeto@Sun.COM #define	MAX_ALIAS		128
369711773STim.Szeto@Sun.COM 
369811773STim.Szeto@Sun.COM static int
stmf_itl_kstat_compare(const void * itl_kstat_1,const void * itl_kstat_2)369911773STim.Szeto@Sun.COM stmf_itl_kstat_compare(const void *itl_kstat_1, const void *itl_kstat_2)
370011773STim.Szeto@Sun.COM {
370111773STim.Szeto@Sun.COM 	const	stmf_i_itl_kstat_t	*kstat_nm1 = itl_kstat_1;
370211773STim.Szeto@Sun.COM 	const	stmf_i_itl_kstat_t	*kstat_nm2 = itl_kstat_2;
370311773STim.Szeto@Sun.COM 	int	ret;
370411773STim.Szeto@Sun.COM 
370511773STim.Szeto@Sun.COM 	ret = strcmp(kstat_nm1->iitl_kstat_nm, kstat_nm2->iitl_kstat_nm);
370611773STim.Szeto@Sun.COM 	if (ret < 0) {
370711773STim.Szeto@Sun.COM 		return (-1);
370811773STim.Szeto@Sun.COM 	} else if (ret > 0) {
370911773STim.Szeto@Sun.COM 		return (1);
371011773STim.Szeto@Sun.COM 	}
371111773STim.Szeto@Sun.COM 	return (0);
371211773STim.Szeto@Sun.COM }
371311773STim.Szeto@Sun.COM 
371411773STim.Szeto@Sun.COM static stmf_i_itl_kstat_t *
stmf_itl_kstat_lookup(char * kstat_nm)371511773STim.Szeto@Sun.COM stmf_itl_kstat_lookup(char *kstat_nm)
371611773STim.Szeto@Sun.COM {
371711773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	tmp;
371811773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	*itl_kstat;
371911773STim.Szeto@Sun.COM 
372012181STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
372111773STim.Szeto@Sun.COM 	(void) strcpy(tmp.iitl_kstat_nm, kstat_nm);
372211773STim.Szeto@Sun.COM 	itl_kstat = avl_find(&stmf_state.stmf_itl_kstat_list, &tmp, NULL);
372311773STim.Szeto@Sun.COM 	return (itl_kstat);
372411773STim.Szeto@Sun.COM }
372511773STim.Szeto@Sun.COM 
372611773STim.Szeto@Sun.COM static void
stmf_delete_itl_kstat_by_lport(char * tgt)372711773STim.Szeto@Sun.COM stmf_delete_itl_kstat_by_lport(char *tgt)
372811773STim.Szeto@Sun.COM {
372911773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	*ks_itl, *next;
373011773STim.Szeto@Sun.COM 
373111773STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
373211773STim.Szeto@Sun.COM 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
373311773STim.Szeto@Sun.COM 	for (; ks_itl != NULL; ks_itl = next) {
373411773STim.Szeto@Sun.COM 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
373511773STim.Szeto@Sun.COM 		if (strcmp(ks_itl->iitl_kstat_lport, tgt) == 0) {
373611773STim.Szeto@Sun.COM 			stmf_teardown_itl_kstats(ks_itl);
373711773STim.Szeto@Sun.COM 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
373811773STim.Szeto@Sun.COM 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
373911773STim.Szeto@Sun.COM 		}
374011773STim.Szeto@Sun.COM 	}
374111773STim.Szeto@Sun.COM }
374211773STim.Szeto@Sun.COM 
374311773STim.Szeto@Sun.COM static void
stmf_delete_itl_kstat_by_guid(char * guid)374411773STim.Szeto@Sun.COM stmf_delete_itl_kstat_by_guid(char *guid)
374511773STim.Szeto@Sun.COM {
374611773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	*ks_itl, *next;
374711773STim.Szeto@Sun.COM 
374811773STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
374911773STim.Szeto@Sun.COM 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
375011773STim.Szeto@Sun.COM 	for (; ks_itl != NULL; ks_itl = next) {
375111773STim.Szeto@Sun.COM 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
375211773STim.Szeto@Sun.COM 		if (strcmp(ks_itl->iitl_kstat_guid, guid) == 0) {
375311773STim.Szeto@Sun.COM 			stmf_teardown_itl_kstats(ks_itl);
375411773STim.Szeto@Sun.COM 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
375511773STim.Szeto@Sun.COM 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
375611773STim.Szeto@Sun.COM 		}
375711773STim.Szeto@Sun.COM 	}
375811773STim.Szeto@Sun.COM }
375911773STim.Szeto@Sun.COM 
376011773STim.Szeto@Sun.COM static stmf_i_itl_kstat_t *
stmf_itl_kstat_create(stmf_itl_data_t * itl,char * nm,scsi_devid_desc_t * lport,scsi_devid_desc_t * lun)376111773STim.Szeto@Sun.COM stmf_itl_kstat_create(stmf_itl_data_t *itl, char *nm,
376211773STim.Szeto@Sun.COM     scsi_devid_desc_t *lport, scsi_devid_desc_t *lun)
376311773STim.Szeto@Sun.COM {
376411773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t	*ks_itl;
376511773STim.Szeto@Sun.COM 	int			i, len;
376611773STim.Szeto@Sun.COM 
376712181STim.Szeto@Sun.COM 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
376811773STim.Szeto@Sun.COM 	if ((ks_itl = stmf_itl_kstat_lookup(nm)) != NULL)
376911773STim.Szeto@Sun.COM 		return (ks_itl);
377011773STim.Szeto@Sun.COM 
377111773STim.Szeto@Sun.COM 	len = sizeof (stmf_i_itl_kstat_t);
377211773STim.Szeto@Sun.COM 	ks_itl = kmem_zalloc(len, KM_NOSLEEP);
377311773STim.Szeto@Sun.COM 	if (ks_itl == NULL)
377411773STim.Szeto@Sun.COM 		return (NULL);
377511773STim.Szeto@Sun.COM 
377611773STim.Szeto@Sun.COM 	(void) strcpy(ks_itl->iitl_kstat_nm, nm);
377711773STim.Szeto@Sun.COM 	bcopy(lport->ident, ks_itl->iitl_kstat_lport, lport->ident_length);
377811773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_lport[lport->ident_length] = '\0';
377911773STim.Szeto@Sun.COM 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
378011773STim.Szeto@Sun.COM 		(void) sprintf(&ks_itl->iitl_kstat_guid[i * 2], "%02x",
378111773STim.Szeto@Sun.COM 		    lun->ident[i]);
378211773STim.Szeto@Sun.COM 	}
378311773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_strbuf = itl->itl_kstat_strbuf;
378411773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_strbuflen = itl->itl_kstat_strbuflen;
378511773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_info = itl->itl_kstat_info;
378611773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_taskq = itl->itl_kstat_taskq;
378711773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_lu_xfer = itl->itl_kstat_lu_xfer;
378811773STim.Szeto@Sun.COM 	ks_itl->iitl_kstat_lport_xfer = itl->itl_kstat_lport_xfer;
378911773STim.Szeto@Sun.COM 	avl_add(&stmf_state.stmf_itl_kstat_list, ks_itl);
379011773STim.Szeto@Sun.COM 
379111773STim.Szeto@Sun.COM 	return (ks_itl);
379211773STim.Szeto@Sun.COM }
379311773STim.Szeto@Sun.COM 
379411773STim.Szeto@Sun.COM stmf_status_t
stmf_setup_itl_kstats(stmf_itl_data_t * itl)379511773STim.Szeto@Sun.COM stmf_setup_itl_kstats(stmf_itl_data_t *itl)
379611773STim.Szeto@Sun.COM {
379711773STim.Szeto@Sun.COM 	char				ks_itl_id[32];
379811773STim.Szeto@Sun.COM 	char				ks_nm[KSTAT_STRLEN];
379911773STim.Szeto@Sun.COM 	char				ks_itl_nm[KSTAT_STRLEN];
380011773STim.Szeto@Sun.COM 	stmf_kstat_itl_info_t		*ks_itl;
380111773STim.Szeto@Sun.COM 	stmf_scsi_session_t		*ss;
380211773STim.Szeto@Sun.COM 	stmf_i_scsi_session_t		*iss;
380311773STim.Szeto@Sun.COM 	stmf_i_local_port_t		*ilport;
380411773STim.Szeto@Sun.COM 	char				*strbuf;
380511773STim.Szeto@Sun.COM 	int				id, len, i;
380611773STim.Szeto@Sun.COM 	char				*rport_alias;
380711773STim.Szeto@Sun.COM 	char				*lport_alias;
380811773STim.Szeto@Sun.COM 	char				*lu_alias;
380911773STim.Szeto@Sun.COM 	stmf_i_itl_kstat_t		*tmp_kstat;
381011773STim.Szeto@Sun.COM 
381111773STim.Szeto@Sun.COM 	/*
381211773STim.Szeto@Sun.COM 	 * Allocate enough memory in the ITL to hold the relevant
381311773STim.Szeto@Sun.COM 	 * identifiers.
381411773STim.Szeto@Sun.COM 	 * rport and lport identifiers come from the stmf_scsi_session_t.
381511773STim.Szeto@Sun.COM 	 * ident might not be null terminated.
381611773STim.Szeto@Sun.COM 	 */
381711773STim.Szeto@Sun.COM 	ss = itl->itl_session->iss_ss;
381811773STim.Szeto@Sun.COM 	iss = ss->ss_stmf_private;
381911773STim.Szeto@Sun.COM 	ilport = ss->ss_lport->lport_stmf_private;
382011773STim.Szeto@Sun.COM 	(void) snprintf(ks_itl_id, 32, "%d.%d.%d",
382111773STim.Szeto@Sun.COM 	    iss->iss_irport->irport_instance, ilport->ilport_instance,
382211773STim.Szeto@Sun.COM 	    itl->itl_lun);
382311773STim.Szeto@Sun.COM 
382411773STim.Szeto@Sun.COM 	(void) snprintf(ks_itl_nm, KSTAT_STRLEN, "itl_%s", ks_itl_id);
382511773STim.Szeto@Sun.COM 	/*
382611773STim.Szeto@Sun.COM 	 * let's verify this itl_kstat already exist
382711773STim.Szeto@Sun.COM 	 */
382811773STim.Szeto@Sun.COM 	if ((tmp_kstat = stmf_itl_kstat_lookup(ks_itl_nm)) != NULL) {
382911773STim.Szeto@Sun.COM 		itl->itl_kstat_strbuf = tmp_kstat->iitl_kstat_strbuf;
383011773STim.Szeto@Sun.COM 		itl->itl_kstat_strbuflen = tmp_kstat->iitl_kstat_strbuflen;
383111773STim.Szeto@Sun.COM 		itl->itl_kstat_info = tmp_kstat->iitl_kstat_info;
383211773STim.Szeto@Sun.COM 		itl->itl_kstat_taskq = tmp_kstat->iitl_kstat_taskq;
383311773STim.Szeto@Sun.COM 		itl->itl_kstat_lu_xfer = tmp_kstat->iitl_kstat_lu_xfer;
383411773STim.Szeto@Sun.COM 		itl->itl_kstat_lport_xfer = tmp_kstat->iitl_kstat_lport_xfer;
383511773STim.Szeto@Sun.COM 		return (STMF_SUCCESS);
383611773STim.Szeto@Sun.COM 	}
383711773STim.Szeto@Sun.COM 
383811773STim.Szeto@Sun.COM 	/* New itl_kstat */
383911773STim.Szeto@Sun.COM 	rport_alias = (ss->ss_rport_alias == NULL) ?
384011773STim.Szeto@Sun.COM 	    "" : ss->ss_rport_alias;
384111773STim.Szeto@Sun.COM 	lport_alias = (ss->ss_lport->lport_alias == NULL) ?
384211773STim.Szeto@Sun.COM 	    "" : ss->ss_lport->lport_alias;
384311773STim.Szeto@Sun.COM 	lu_alias = (itl->itl_ilu->ilu_lu->lu_alias == NULL) ?
384411773STim.Szeto@Sun.COM 	    "" : itl->itl_ilu->ilu_lu->lu_alias;
384511773STim.Szeto@Sun.COM 
384611773STim.Szeto@Sun.COM 	itl->itl_kstat_strbuflen = (ss->ss_rport_id->ident_length + 1) +
384711773STim.Szeto@Sun.COM 	    (strnlen(rport_alias, MAX_ALIAS) + 1) +
384811773STim.Szeto@Sun.COM 	    (ss->ss_lport->lport_id->ident_length + 1) +
384911773STim.Szeto@Sun.COM 	    (strnlen(lport_alias, MAX_ALIAS) + 1) +
385011773STim.Szeto@Sun.COM 	    (STMF_GUID_INPUT + 1) +
385111773STim.Szeto@Sun.COM 	    (strnlen(lu_alias, MAX_ALIAS) + 1) +
385211773STim.Szeto@Sun.COM 	    MAX_PROTO_STR_LEN;
385311773STim.Szeto@Sun.COM 	itl->itl_kstat_strbuf = kmem_zalloc(itl->itl_kstat_strbuflen,
385411773STim.Szeto@Sun.COM 	    KM_NOSLEEP);
385511773STim.Szeto@Sun.COM 	if (itl->itl_kstat_strbuf == NULL) {
385611773STim.Szeto@Sun.COM 		return (STMF_ALLOC_FAILURE);
385711773STim.Szeto@Sun.COM 	}
385811773STim.Szeto@Sun.COM 
385911773STim.Szeto@Sun.COM 	ks_itl = (stmf_kstat_itl_info_t *)kmem_zalloc(sizeof (*ks_itl),
386011773STim.Szeto@Sun.COM 	    KM_NOSLEEP);
386111773STim.Szeto@Sun.COM 	if (ks_itl == NULL) {
386211773STim.Szeto@Sun.COM 		kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
386311773STim.Szeto@Sun.COM 		return (STMF_ALLOC_FAILURE);
386411773STim.Szeto@Sun.COM 	}
386511773STim.Szeto@Sun.COM 
386611773STim.Szeto@Sun.COM 	if ((itl->itl_kstat_info = kstat_create(STMF_MODULE_NAME,
386711773STim.Szeto@Sun.COM 	    0, ks_itl_nm, "misc", KSTAT_TYPE_NAMED,
386811773STim.Szeto@Sun.COM 	    sizeof (stmf_kstat_itl_info_t) / sizeof (kstat_named_t),
386911773STim.Szeto@Sun.COM 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
387011773STim.Szeto@Sun.COM 		goto itl_kstat_cleanup;
387111773STim.Szeto@Sun.COM 	}
387211773STim.Szeto@Sun.COM 
387311773STim.Szeto@Sun.COM 	itl->itl_kstat_info->ks_data_size += itl->itl_kstat_strbuflen;
387411773STim.Szeto@Sun.COM 	itl->itl_kstat_info->ks_data = ks_itl;
387511773STim.Szeto@Sun.COM 
387611773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_rport_name, "rport-name",
387711773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
387811773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_rport_alias, "rport-alias",
387911773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
388011773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lport_name, "lport-name",
388111773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
388211773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lport_alias, "lport-alias",
388311773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
388411773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_protocol, "protocol",
388511773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
388611773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lu_guid, "lu-guid",
388711773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
388811773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lu_alias, "lu-alias",
388911773STim.Szeto@Sun.COM 	    KSTAT_DATA_STRING);
389011773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lu_number, "lu-number",
389111773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
389211773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_task_waitq_elapsed, "task-waitq-elapsed",
389311773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
389411773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_task_read_elapsed, "task-read-elapsed",
389511773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
389611773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_task_write_elapsed, "task-write-elapsed",
389711773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
389811773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lu_read_elapsed, "lu-read-elapsed",
389911773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
390011773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lu_write_elapsed, "lu-write-elapsed",
390111773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
390211773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lport_read_elapsed, "lport-read-elapsed",
390311773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
390411773STim.Szeto@Sun.COM 	kstat_named_init(&ks_itl->i_lport_write_elapsed, "lport-write-elapsed",
390511773STim.Szeto@Sun.COM 	    KSTAT_DATA_UINT64);
390611773STim.Szeto@Sun.COM 
390711773STim.Szeto@Sun.COM 	strbuf = itl->itl_kstat_strbuf;
390811773STim.Szeto@Sun.COM 
390911773STim.Szeto@Sun.COM 	/* Rport */
391011773STim.Szeto@Sun.COM 	len = ss->ss_rport_id->ident_length;
391111773STim.Szeto@Sun.COM 	bcopy(ss->ss_rport_id->ident, strbuf, len);
391211773STim.Szeto@Sun.COM 	strbuf += len;
391311773STim.Szeto@Sun.COM 	*strbuf = '\0';
391411773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_rport_name, strbuf - len);
391511773STim.Szeto@Sun.COM 	strbuf++;
391611773STim.Szeto@Sun.COM 
391711773STim.Szeto@Sun.COM 	len = strnlen(rport_alias, MAX_ALIAS);
391811773STim.Szeto@Sun.COM 	(void) strncpy(strbuf, rport_alias, len + 1);
391911773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_rport_alias, strbuf);
392011773STim.Szeto@Sun.COM 	strbuf += len + 1;
392111773STim.Szeto@Sun.COM 
392211773STim.Szeto@Sun.COM 	/* Lport */
392311773STim.Szeto@Sun.COM 	len = ss->ss_lport->lport_id->ident_length;
392411773STim.Szeto@Sun.COM 	bcopy(ss->ss_lport->lport_id->ident, strbuf, len);
392511773STim.Szeto@Sun.COM 	strbuf += len;
392611773STim.Szeto@Sun.COM 	*strbuf = '\0';
392711773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_lport_name, strbuf - len);
392811773STim.Szeto@Sun.COM 	strbuf++;
392911773STim.Szeto@Sun.COM 
393011773STim.Szeto@Sun.COM 	len = strnlen(lport_alias, MAX_ALIAS);
393111773STim.Szeto@Sun.COM 	(void) strncpy(strbuf, lport_alias, len + 1);
393211773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_lport_alias, strbuf);
393311773STim.Szeto@Sun.COM 	strbuf += len + 1;
393411773STim.Szeto@Sun.COM 
393511773STim.Szeto@Sun.COM 	id = (ss->ss_lport->lport_id->protocol_id > PROTOCOL_ANY) ?
393611773STim.Szeto@Sun.COM 	    PROTOCOL_ANY : ss->ss_lport->lport_id->protocol_id;
393711773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_protocol, protocol_ident[id]);
393811773STim.Szeto@Sun.COM 
393911773STim.Szeto@Sun.COM 	/* LU */
394011773STim.Szeto@Sun.COM 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
394111773STim.Szeto@Sun.COM 		(void) sprintf(&strbuf[i * 2], "%02x",
394211773STim.Szeto@Sun.COM 		    itl->itl_ilu->ilu_lu->lu_id->ident[i]);
394311773STim.Szeto@Sun.COM 	}
394411773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_lu_guid, strbuf);
394511773STim.Szeto@Sun.COM 	strbuf += STMF_GUID_INPUT + 1;
394611773STim.Szeto@Sun.COM 
394711773STim.Szeto@Sun.COM 	len = strnlen(lu_alias, MAX_ALIAS);
394811773STim.Szeto@Sun.COM 	(void) strncpy(strbuf, lu_alias, len + 1);
394911773STim.Szeto@Sun.COM 	kstat_named_setstr(&ks_itl->i_lu_alias, strbuf);
395011773STim.Szeto@Sun.COM 	strbuf += len + 1;
395111773STim.Szeto@Sun.COM 
395211773STim.Szeto@Sun.COM 	ks_itl->i_lu_number.value.ui64 = itl->itl_lun;
395311773STim.Szeto@Sun.COM 
395411773STim.Szeto@Sun.COM 	/* Now create the I/O kstats */
395511773STim.Szeto@Sun.COM 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_tasks_%s",  ks_itl_id);
395611773STim.Szeto@Sun.COM 	if ((itl->itl_kstat_taskq = kstat_create(STMF_MODULE_NAME, 0,
395711773STim.Szeto@Sun.COM 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
395811773STim.Szeto@Sun.COM 		goto itl_kstat_cleanup;
395911773STim.Szeto@Sun.COM 	}
396011773STim.Szeto@Sun.COM 
396111773STim.Szeto@Sun.COM 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lu_%s",  ks_itl_id);
396211773STim.Szeto@Sun.COM 	if ((itl->itl_kstat_lu_xfer = kstat_create(STMF_MODULE_NAME, 0,
396311773STim.Szeto@Sun.COM 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
396411773STim.Szeto@Sun.COM 		goto itl_kstat_cleanup;
396511773STim.Szeto@Sun.COM 	}
396611773STim.Szeto@Sun.COM 
396711773STim.Szeto@Sun.COM 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lport_%s",  ks_itl_id);
396811773STim.Szeto@Sun.COM 	if ((itl->itl_kstat_lport_xfer = kstat_create(STMF_MODULE_NAME, 0,
396911773STim.Szeto@Sun.COM 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
397011773STim.Szeto@Sun.COM 		goto itl_kstat_cleanup;
397111773STim.Szeto@Sun.COM 	}
397211773STim.Szeto@Sun.COM 
397311773STim.Szeto@Sun.COM 	/* Install all the kstats */
397411773STim.Szeto@Sun.COM 	kstat_install(itl->itl_kstat_info);
397511773STim.Szeto@Sun.COM 	kstat_install(itl->itl_kstat_taskq);
397611773STim.Szeto@Sun.COM 	kstat_install(itl->itl_kstat_lu_xfer);
397711773STim.Szeto@Sun.COM 	kstat_install(itl->itl_kstat_lport_xfer);
397811773STim.Szeto@Sun.COM 
397911773STim.Szeto@Sun.COM 	/* Add new itl_kstat to stmf_itl_kstat_list */
398011773STim.Szeto@Sun.COM 	if (stmf_itl_kstat_create(itl, ks_itl_nm, ss->ss_lport->lport_id,
398111773STim.Szeto@Sun.COM 	    itl->itl_ilu->ilu_lu->lu_id) != NULL)
398211773STim.Szeto@Sun.COM 		return (STMF_SUCCESS);
398311773STim.Szeto@Sun.COM 
398411773STim.Szeto@Sun.COM itl_kstat_cleanup:
398511773STim.Szeto@Sun.COM 	if (itl->itl_kstat_taskq)
398611773STim.Szeto@Sun.COM 		kstat_delete(itl->itl_kstat_taskq);
398711773STim.Szeto@Sun.COM 	if (itl->itl_kstat_lu_xfer)
398811773STim.Szeto@Sun.COM 		kstat_delete(itl->itl_kstat_lu_xfer);
398911773STim.Szeto@Sun.COM 	if (itl->itl_kstat_lport_xfer)
399011773STim.Szeto@Sun.COM 		kstat_delete(itl->itl_kstat_lport_xfer);
399111773STim.Szeto@Sun.COM 	if (itl->itl_kstat_info)
399211773STim.Szeto@Sun.COM 		kstat_delete(itl->itl_kstat_info);
399311773STim.Szeto@Sun.COM 	kmem_free(ks_itl, sizeof (*ks_itl));
399411773STim.Szeto@Sun.COM 	kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
399511773STim.Szeto@Sun.COM 	cmn_err(CE_WARN, "STMF: kstat_create itl failed");
399611773STim.Szeto@Sun.COM 	return (STMF_ALLOC_FAILURE);
399711773STim.Szeto@Sun.COM }
399811773STim.Szeto@Sun.COM 
399911773STim.Szeto@Sun.COM static void
stmf_teardown_itl_kstats(stmf_i_itl_kstat_t * ks)400011773STim.Szeto@Sun.COM stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks)
400111773STim.Szeto@Sun.COM {
400211773STim.Szeto@Sun.COM 	kstat_delete(ks->iitl_kstat_lport_xfer);
400311773STim.Szeto@Sun.COM 	kstat_delete(ks->iitl_kstat_lu_xfer);
400411773STim.Szeto@Sun.COM 	kstat_delete(ks->iitl_kstat_taskq);
400511773STim.Szeto@Sun.COM 	kmem_free(ks->iitl_kstat_info->ks_data, sizeof (stmf_kstat_itl_info_t));
400611773STim.Szeto@Sun.COM 	kstat_delete(ks->iitl_kstat_info);
400711773STim.Szeto@Sun.COM 	kmem_free(ks->iitl_kstat_strbuf, ks->iitl_kstat_strbuflen);
400811773STim.Szeto@Sun.COM }
400911773STim.Szeto@Sun.COM 
40107836SJohn.Forte@Sun.COM void
stmf_release_itl_handle(stmf_lu_t * lu,stmf_itl_data_t * itl)40117836SJohn.Forte@Sun.COM stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
40127836SJohn.Forte@Sun.COM {
40137836SJohn.Forte@Sun.COM 	stmf_itl_data_t **itlpp;
40147836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
40157836SJohn.Forte@Sun.COM 
40167836SJohn.Forte@Sun.COM 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
40177836SJohn.Forte@Sun.COM 
40187836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
40197836SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
40207836SJohn.Forte@Sun.COM 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
40218662SJordan.Vaughan@Sun.com 	    itlpp = &(*itlpp)->itl_next) {
40227836SJohn.Forte@Sun.COM 		if ((*itlpp) == itl)
40237836SJohn.Forte@Sun.COM 			break;
40247836SJohn.Forte@Sun.COM 	}
40257836SJohn.Forte@Sun.COM 	ASSERT((*itlpp) != NULL);
40267836SJohn.Forte@Sun.COM 	*itlpp = itl->itl_next;
40277836SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
40287836SJohn.Forte@Sun.COM 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
40298662SJordan.Vaughan@Sun.com 	    (uint32_t)itl->itl_hdlrm_reason);
403011773STim.Szeto@Sun.COM 
40317836SJohn.Forte@Sun.COM 	kmem_free(itl, sizeof (*itl));
40327836SJohn.Forte@Sun.COM }
40337836SJohn.Forte@Sun.COM 
40347836SJohn.Forte@Sun.COM stmf_status_t
stmf_register_itl_handle(stmf_lu_t * lu,uint8_t * lun,stmf_scsi_session_t * ss,uint64_t session_id,void * itl_handle)40357836SJohn.Forte@Sun.COM stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
40367836SJohn.Forte@Sun.COM     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
40377836SJohn.Forte@Sun.COM {
40387836SJohn.Forte@Sun.COM 	stmf_itl_data_t *itl;
40397836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
40407836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *lun_map_ent;
40417836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
40427836SJohn.Forte@Sun.COM 	uint16_t n;
40437836SJohn.Forte@Sun.COM 
40447836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
40457836SJohn.Forte@Sun.COM 	if (ss == NULL) {
40467836SJohn.Forte@Sun.COM 		iss = stmf_session_id_to_issptr(session_id, 1);
40477836SJohn.Forte@Sun.COM 		if (iss == NULL)
40487836SJohn.Forte@Sun.COM 			return (STMF_NOT_FOUND);
40497836SJohn.Forte@Sun.COM 	} else {
40507836SJohn.Forte@Sun.COM 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
405111773STim.Szeto@Sun.COM 	}
405211773STim.Szeto@Sun.COM 
405312181STim.Szeto@Sun.COM 	/*
405412181STim.Szeto@Sun.COM 	 * Acquire stmf_lock for stmf_itl_kstat_lookup.
405512181STim.Szeto@Sun.COM 	 */
405612181STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
405711773STim.Szeto@Sun.COM 	rw_enter(iss->iss_lockp, RW_WRITER);
40587836SJohn.Forte@Sun.COM 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
40597836SJohn.Forte@Sun.COM 	lun_map_ent = (stmf_lun_map_ent_t *)
40608662SJordan.Vaughan@Sun.com 	    stmf_get_ent_from_map(iss->iss_sm, n);
40617836SJohn.Forte@Sun.COM 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
40627836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
406312181STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
40647836SJohn.Forte@Sun.COM 		return (STMF_NOT_FOUND);
40657836SJohn.Forte@Sun.COM 	}
40667836SJohn.Forte@Sun.COM 	if (lun_map_ent->ent_itl_datap != NULL) {
40677836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
406812181STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
40697836SJohn.Forte@Sun.COM 		return (STMF_ALREADY);
40707836SJohn.Forte@Sun.COM 	}
40717836SJohn.Forte@Sun.COM 
40727836SJohn.Forte@Sun.COM 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
40737836SJohn.Forte@Sun.COM 	if (itl == NULL) {
40747836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
407512181STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
40767836SJohn.Forte@Sun.COM 		return (STMF_ALLOC_FAILURE);
40777836SJohn.Forte@Sun.COM 	}
40787836SJohn.Forte@Sun.COM 
407911773STim.Szeto@Sun.COM 	itl->itl_ilu = ilu;
408011773STim.Szeto@Sun.COM 	itl->itl_session = iss;
40817836SJohn.Forte@Sun.COM 	itl->itl_counter = 1;
40827836SJohn.Forte@Sun.COM 	itl->itl_lun = n;
40837836SJohn.Forte@Sun.COM 	itl->itl_handle = itl_handle;
408411773STim.Szeto@Sun.COM 
408511773STim.Szeto@Sun.COM 	if (stmf_setup_itl_kstats(itl) != STMF_SUCCESS) {
408611773STim.Szeto@Sun.COM 		kmem_free(itl, sizeof (*itl));
408711773STim.Szeto@Sun.COM 		rw_exit(iss->iss_lockp);
408812181STim.Szeto@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
408911773STim.Szeto@Sun.COM 		return (STMF_ALLOC_FAILURE);
409011773STim.Szeto@Sun.COM 	}
409111773STim.Szeto@Sun.COM 
40927836SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
40937836SJohn.Forte@Sun.COM 	itl->itl_next = ilu->ilu_itl_list;
40947836SJohn.Forte@Sun.COM 	ilu->ilu_itl_list = itl;
40957836SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
40967836SJohn.Forte@Sun.COM 	lun_map_ent->ent_itl_datap = itl;
40977836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
409812181STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
40997836SJohn.Forte@Sun.COM 
41007836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
41017836SJohn.Forte@Sun.COM }
41027836SJohn.Forte@Sun.COM 
41037836SJohn.Forte@Sun.COM void
stmf_do_itl_dereg(stmf_lu_t * lu,stmf_itl_data_t * itl,uint8_t hdlrm_reason)41047836SJohn.Forte@Sun.COM stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
41057836SJohn.Forte@Sun.COM {
41067836SJohn.Forte@Sun.COM 	uint8_t old, new;
41077836SJohn.Forte@Sun.COM 
41087836SJohn.Forte@Sun.COM 	do {
41097836SJohn.Forte@Sun.COM 		old = new = itl->itl_flags;
41107836SJohn.Forte@Sun.COM 		if (old & STMF_ITL_BEING_TERMINATED)
41117836SJohn.Forte@Sun.COM 			return;
41127836SJohn.Forte@Sun.COM 		new |= STMF_ITL_BEING_TERMINATED;
41137836SJohn.Forte@Sun.COM 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
41147836SJohn.Forte@Sun.COM 	itl->itl_hdlrm_reason = hdlrm_reason;
41157836SJohn.Forte@Sun.COM 
41167836SJohn.Forte@Sun.COM 	ASSERT(itl->itl_counter);
41177836SJohn.Forte@Sun.COM 
41187836SJohn.Forte@Sun.COM 	if (atomic_add_32_nv(&itl->itl_counter, -1))
41197836SJohn.Forte@Sun.COM 		return;
41207836SJohn.Forte@Sun.COM 
41217836SJohn.Forte@Sun.COM 	drv_usecwait(10);
41227836SJohn.Forte@Sun.COM 	if (itl->itl_counter)
41237836SJohn.Forte@Sun.COM 		return;
41247836SJohn.Forte@Sun.COM 
41257836SJohn.Forte@Sun.COM 	stmf_release_itl_handle(lu, itl);
41267836SJohn.Forte@Sun.COM }
41277836SJohn.Forte@Sun.COM 
41287836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_all_lu_itl_handles(stmf_lu_t * lu)41297836SJohn.Forte@Sun.COM stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
41307836SJohn.Forte@Sun.COM {
41317836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
41327836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
41337836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
41347836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
41357836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *ent;
41367836SJohn.Forte@Sun.COM 	uint32_t nmaps, nu;
41377836SJohn.Forte@Sun.COM 	stmf_itl_data_t **itl_list;
41387836SJohn.Forte@Sun.COM 	int i;
41397836SJohn.Forte@Sun.COM 
41407836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
41417836SJohn.Forte@Sun.COM 
41427836SJohn.Forte@Sun.COM dereg_itl_start:;
41437836SJohn.Forte@Sun.COM 	nmaps = ilu->ilu_ref_cnt;
41447836SJohn.Forte@Sun.COM 	if (nmaps == 0)
41457836SJohn.Forte@Sun.COM 		return (STMF_NOT_FOUND);
41467836SJohn.Forte@Sun.COM 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
41478662SJordan.Vaughan@Sun.com 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
41487836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
41497836SJohn.Forte@Sun.COM 	if (nmaps != ilu->ilu_ref_cnt) {
41507836SJohn.Forte@Sun.COM 		/* Something changed, start all over */
41517836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
41527836SJohn.Forte@Sun.COM 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
41537836SJohn.Forte@Sun.COM 		goto dereg_itl_start;
41547836SJohn.Forte@Sun.COM 	}
41557836SJohn.Forte@Sun.COM 	nu = 0;
41567836SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
41578662SJordan.Vaughan@Sun.com 	    ilport = ilport->ilport_next) {
41587836SJohn.Forte@Sun.COM 		rw_enter(&ilport->ilport_lock, RW_WRITER);
41597836SJohn.Forte@Sun.COM 		for (iss = ilport->ilport_ss_list; iss != NULL;
41608662SJordan.Vaughan@Sun.com 		    iss = iss->iss_next) {
41617836SJohn.Forte@Sun.COM 			lm = iss->iss_sm;
41627836SJohn.Forte@Sun.COM 			if (!lm)
41637836SJohn.Forte@Sun.COM 				continue;
41647836SJohn.Forte@Sun.COM 			for (i = 0; i < lm->lm_nentries; i++) {
41657836SJohn.Forte@Sun.COM 				if (lm->lm_plus[i] == NULL)
41667836SJohn.Forte@Sun.COM 					continue;
41677836SJohn.Forte@Sun.COM 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
41687836SJohn.Forte@Sun.COM 				if ((ent->ent_lu == lu) &&
41698662SJordan.Vaughan@Sun.com 				    (ent->ent_itl_datap)) {
41707836SJohn.Forte@Sun.COM 					itl_list[nu++] = ent->ent_itl_datap;
41717836SJohn.Forte@Sun.COM 					ent->ent_itl_datap = NULL;
41727836SJohn.Forte@Sun.COM 					if (nu == nmaps) {
41737836SJohn.Forte@Sun.COM 						rw_exit(&ilport->ilport_lock);
41747836SJohn.Forte@Sun.COM 						goto dai_scan_done;
41757836SJohn.Forte@Sun.COM 					}
41767836SJohn.Forte@Sun.COM 				}
41777836SJohn.Forte@Sun.COM 			} /* lun table for a session */
41787836SJohn.Forte@Sun.COM 		} /* sessions */
41797836SJohn.Forte@Sun.COM 		rw_exit(&ilport->ilport_lock);
41807836SJohn.Forte@Sun.COM 	} /* ports */
41817836SJohn.Forte@Sun.COM 
41827836SJohn.Forte@Sun.COM dai_scan_done:
41837836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
41847836SJohn.Forte@Sun.COM 
41857836SJohn.Forte@Sun.COM 	for (i = 0; i < nu; i++) {
41867836SJohn.Forte@Sun.COM 		stmf_do_itl_dereg(lu, itl_list[i],
41878662SJordan.Vaughan@Sun.com 		    STMF_ITL_REASON_DEREG_REQUEST);
41887836SJohn.Forte@Sun.COM 	}
41897836SJohn.Forte@Sun.COM 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
41907836SJohn.Forte@Sun.COM 
41917836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
41927836SJohn.Forte@Sun.COM }
41937836SJohn.Forte@Sun.COM 
41947836SJohn.Forte@Sun.COM stmf_status_t
stmf_deregister_itl_handle(stmf_lu_t * lu,uint8_t * lun,stmf_scsi_session_t * ss,uint64_t session_id,void * itl_handle)41957836SJohn.Forte@Sun.COM stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun,
41967836SJohn.Forte@Sun.COM     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
41977836SJohn.Forte@Sun.COM {
41987836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
41997836SJohn.Forte@Sun.COM 	stmf_itl_data_t *itl;
42007836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *ent;
42017836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
42027836SJohn.Forte@Sun.COM 	int i;
42037836SJohn.Forte@Sun.COM 	uint16_t n;
42047836SJohn.Forte@Sun.COM 
42057836SJohn.Forte@Sun.COM 	if (ss == NULL) {
42067836SJohn.Forte@Sun.COM 		if (session_id == STMF_SESSION_ID_NONE)
42077836SJohn.Forte@Sun.COM 			return (STMF_INVALID_ARG);
42087836SJohn.Forte@Sun.COM 		iss = stmf_session_id_to_issptr(session_id, 1);
42097836SJohn.Forte@Sun.COM 		if (iss == NULL)
42107836SJohn.Forte@Sun.COM 			return (STMF_NOT_FOUND);
42117836SJohn.Forte@Sun.COM 	} else {
42127836SJohn.Forte@Sun.COM 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
42137836SJohn.Forte@Sun.COM 		rw_enter(iss->iss_lockp, RW_WRITER);
42147836SJohn.Forte@Sun.COM 	}
42157836SJohn.Forte@Sun.COM 	lm = iss->iss_sm;
42167836SJohn.Forte@Sun.COM 	if (lm == NULL) {
42177836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
42187836SJohn.Forte@Sun.COM 		return (STMF_NOT_FOUND);
42197836SJohn.Forte@Sun.COM 	}
42207836SJohn.Forte@Sun.COM 
42217836SJohn.Forte@Sun.COM 	if (lun) {
42227836SJohn.Forte@Sun.COM 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
42237836SJohn.Forte@Sun.COM 		ent = (stmf_lun_map_ent_t *)
42248662SJordan.Vaughan@Sun.com 		    stmf_get_ent_from_map(iss->iss_sm, n);
42257836SJohn.Forte@Sun.COM 	} else {
42267836SJohn.Forte@Sun.COM 		if (itl_handle == NULL) {
42277836SJohn.Forte@Sun.COM 			rw_exit(iss->iss_lockp);
42287836SJohn.Forte@Sun.COM 			return (STMF_INVALID_ARG);
42297836SJohn.Forte@Sun.COM 		}
42307836SJohn.Forte@Sun.COM 		ent = NULL;
42317836SJohn.Forte@Sun.COM 		for (i = 0; i < lm->lm_nentries; i++) {
42327836SJohn.Forte@Sun.COM 			if (lm->lm_plus[i] == NULL)
42337836SJohn.Forte@Sun.COM 				continue;
42347836SJohn.Forte@Sun.COM 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
42357836SJohn.Forte@Sun.COM 			if (ent->ent_itl_datap &&
42367836SJohn.Forte@Sun.COM 			    (ent->ent_itl_datap->itl_handle == itl_handle)) {
42377836SJohn.Forte@Sun.COM 				break;
42387836SJohn.Forte@Sun.COM 			}
42397836SJohn.Forte@Sun.COM 		}
42407836SJohn.Forte@Sun.COM 	}
42417836SJohn.Forte@Sun.COM 	if ((ent == NULL) || (ent->ent_lu != lu) ||
42428662SJordan.Vaughan@Sun.com 	    (ent->ent_itl_datap == NULL)) {
42437836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
42447836SJohn.Forte@Sun.COM 		return (STMF_NOT_FOUND);
42457836SJohn.Forte@Sun.COM 	}
42467836SJohn.Forte@Sun.COM 	itl = ent->ent_itl_datap;
42477836SJohn.Forte@Sun.COM 	ent->ent_itl_datap = NULL;
42487836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
42497836SJohn.Forte@Sun.COM 	stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST);
42507836SJohn.Forte@Sun.COM 
42517836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
42527836SJohn.Forte@Sun.COM }
42537836SJohn.Forte@Sun.COM 
42547836SJohn.Forte@Sun.COM stmf_status_t
stmf_get_itl_handle(stmf_lu_t * lu,uint8_t * lun,stmf_scsi_session_t * ss,uint64_t session_id,void ** itl_handle_retp)42557836SJohn.Forte@Sun.COM stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
42567836SJohn.Forte@Sun.COM     uint64_t session_id, void **itl_handle_retp)
42577836SJohn.Forte@Sun.COM {
42587836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
42597836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *ent;
42607836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
42617836SJohn.Forte@Sun.COM 	stmf_status_t ret;
42627836SJohn.Forte@Sun.COM 	int i;
42637836SJohn.Forte@Sun.COM 	uint16_t n;
42647836SJohn.Forte@Sun.COM 
42657836SJohn.Forte@Sun.COM 	if (ss == NULL) {
42667836SJohn.Forte@Sun.COM 		iss = stmf_session_id_to_issptr(session_id, 1);
42677836SJohn.Forte@Sun.COM 		if (iss == NULL)
42687836SJohn.Forte@Sun.COM 			return (STMF_NOT_FOUND);
42697836SJohn.Forte@Sun.COM 	} else {
42707836SJohn.Forte@Sun.COM 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
42717836SJohn.Forte@Sun.COM 		rw_enter(iss->iss_lockp, RW_WRITER);
42727836SJohn.Forte@Sun.COM 	}
42737836SJohn.Forte@Sun.COM 
42747836SJohn.Forte@Sun.COM 	ent = NULL;
42757836SJohn.Forte@Sun.COM 	if (lun == NULL) {
42767836SJohn.Forte@Sun.COM 		lm = iss->iss_sm;
42777836SJohn.Forte@Sun.COM 		for (i = 0; i < lm->lm_nentries; i++) {
42787836SJohn.Forte@Sun.COM 			if (lm->lm_plus[i] == NULL)
42797836SJohn.Forte@Sun.COM 				continue;
42807836SJohn.Forte@Sun.COM 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
42817836SJohn.Forte@Sun.COM 			if (ent->ent_lu == lu)
42827836SJohn.Forte@Sun.COM 				break;
42837836SJohn.Forte@Sun.COM 		}
42847836SJohn.Forte@Sun.COM 	} else {
42857836SJohn.Forte@Sun.COM 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
42867836SJohn.Forte@Sun.COM 		ent = (stmf_lun_map_ent_t *)
42878662SJordan.Vaughan@Sun.com 		    stmf_get_ent_from_map(iss->iss_sm, n);
42887836SJohn.Forte@Sun.COM 		if (lu && (ent->ent_lu != lu))
42897836SJohn.Forte@Sun.COM 			ent = NULL;
42907836SJohn.Forte@Sun.COM 	}
42917836SJohn.Forte@Sun.COM 	if (ent && ent->ent_itl_datap) {
42927836SJohn.Forte@Sun.COM 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
42937836SJohn.Forte@Sun.COM 		ret = STMF_SUCCESS;
42947836SJohn.Forte@Sun.COM 	} else {
42957836SJohn.Forte@Sun.COM 		ret = STMF_NOT_FOUND;
42967836SJohn.Forte@Sun.COM 	}
42977836SJohn.Forte@Sun.COM 
42987836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
42997836SJohn.Forte@Sun.COM 	return (ret);
43007836SJohn.Forte@Sun.COM }
43017836SJohn.Forte@Sun.COM 
43027836SJohn.Forte@Sun.COM stmf_data_buf_t *
stmf_alloc_dbuf(scsi_task_t * task,uint32_t size,uint32_t * pminsize,uint32_t flags)43037836SJohn.Forte@Sun.COM stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
43047836SJohn.Forte@Sun.COM     uint32_t flags)
43057836SJohn.Forte@Sun.COM {
43067836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
43077836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
43087836SJohn.Forte@Sun.COM 	stmf_local_port_t *lport = task->task_lport;
43097836SJohn.Forte@Sun.COM 	stmf_data_buf_t *dbuf;
43107836SJohn.Forte@Sun.COM 	uint8_t ndx;
43117836SJohn.Forte@Sun.COM 
43127836SJohn.Forte@Sun.COM 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
43137836SJohn.Forte@Sun.COM 	if (ndx == 0xff)
43147836SJohn.Forte@Sun.COM 		return (NULL);
43157836SJohn.Forte@Sun.COM 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
43167836SJohn.Forte@Sun.COM 	    task, size, pminsize, flags);
43177836SJohn.Forte@Sun.COM 	if (dbuf) {
43187836SJohn.Forte@Sun.COM 		task->task_cur_nbufs++;
43197836SJohn.Forte@Sun.COM 		itask->itask_allocated_buf_map |= (1 << ndx);
432012625SJohn.Forte@Sun.COM 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
43217836SJohn.Forte@Sun.COM 		dbuf->db_handle = ndx;
43227836SJohn.Forte@Sun.COM 		return (dbuf);
43237836SJohn.Forte@Sun.COM 	}
43247836SJohn.Forte@Sun.COM 
43257836SJohn.Forte@Sun.COM 	return (NULL);
43267836SJohn.Forte@Sun.COM }
43277836SJohn.Forte@Sun.COM 
432812314SJames.Moore@Sun.COM stmf_status_t
stmf_setup_dbuf(scsi_task_t * task,stmf_data_buf_t * dbuf,uint32_t flags)432912314SJames.Moore@Sun.COM stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags)
433012314SJames.Moore@Sun.COM {
433112314SJames.Moore@Sun.COM 	stmf_i_scsi_task_t *itask =
433212314SJames.Moore@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
433312314SJames.Moore@Sun.COM 	stmf_local_port_t *lport = task->task_lport;
433412314SJames.Moore@Sun.COM 	uint8_t ndx;
433512314SJames.Moore@Sun.COM 	stmf_status_t ret;
433612314SJames.Moore@Sun.COM 
433712314SJames.Moore@Sun.COM 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
433812314SJames.Moore@Sun.COM 	ASSERT(lport->lport_ds->ds_setup_dbuf != NULL);
433912314SJames.Moore@Sun.COM 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
434012314SJames.Moore@Sun.COM 
434112314SJames.Moore@Sun.COM 	if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0)
434212314SJames.Moore@Sun.COM 		return (STMF_FAILURE);
434312314SJames.Moore@Sun.COM 	if (lport->lport_ds->ds_setup_dbuf == NULL)
434412314SJames.Moore@Sun.COM 		return (STMF_FAILURE);
434512314SJames.Moore@Sun.COM 
434612314SJames.Moore@Sun.COM 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
434712314SJames.Moore@Sun.COM 	if (ndx == 0xff)
434812314SJames.Moore@Sun.COM 		return (STMF_FAILURE);
434912314SJames.Moore@Sun.COM 	ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags);
435012314SJames.Moore@Sun.COM 	if (ret == STMF_FAILURE)
435112314SJames.Moore@Sun.COM 		return (STMF_FAILURE);
435212314SJames.Moore@Sun.COM 	itask->itask_dbufs[ndx] = dbuf;
435312314SJames.Moore@Sun.COM 	task->task_cur_nbufs++;
435412314SJames.Moore@Sun.COM 	itask->itask_allocated_buf_map |= (1 << ndx);
435512314SJames.Moore@Sun.COM 	dbuf->db_handle = ndx;
435612314SJames.Moore@Sun.COM 
435712314SJames.Moore@Sun.COM 	return (STMF_SUCCESS);
435812314SJames.Moore@Sun.COM }
435912314SJames.Moore@Sun.COM 
436012314SJames.Moore@Sun.COM void
stmf_teardown_dbuf(scsi_task_t * task,stmf_data_buf_t * dbuf)436112314SJames.Moore@Sun.COM stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
436212314SJames.Moore@Sun.COM {
436312314SJames.Moore@Sun.COM 	stmf_i_scsi_task_t *itask =
436412314SJames.Moore@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
436512314SJames.Moore@Sun.COM 	stmf_local_port_t *lport = task->task_lport;
436612314SJames.Moore@Sun.COM 
436712314SJames.Moore@Sun.COM 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
436812314SJames.Moore@Sun.COM 	ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL);
436912314SJames.Moore@Sun.COM 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
437012314SJames.Moore@Sun.COM 
437112314SJames.Moore@Sun.COM 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
437212314SJames.Moore@Sun.COM 	task->task_cur_nbufs--;
437312314SJames.Moore@Sun.COM 	lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf);
437412314SJames.Moore@Sun.COM }
437512314SJames.Moore@Sun.COM 
43767836SJohn.Forte@Sun.COM void
stmf_free_dbuf(scsi_task_t * task,stmf_data_buf_t * dbuf)43777836SJohn.Forte@Sun.COM stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
43787836SJohn.Forte@Sun.COM {
43797836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
43807836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
43817836SJohn.Forte@Sun.COM 	stmf_local_port_t *lport = task->task_lport;
43827836SJohn.Forte@Sun.COM 
43837836SJohn.Forte@Sun.COM 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
43847836SJohn.Forte@Sun.COM 	task->task_cur_nbufs--;
43857836SJohn.Forte@Sun.COM 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
43867836SJohn.Forte@Sun.COM }
43877836SJohn.Forte@Sun.COM 
43887836SJohn.Forte@Sun.COM stmf_data_buf_t *
stmf_handle_to_buf(scsi_task_t * task,uint8_t h)43897836SJohn.Forte@Sun.COM stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
43907836SJohn.Forte@Sun.COM {
43917836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
43927836SJohn.Forte@Sun.COM 
43937836SJohn.Forte@Sun.COM 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
43947836SJohn.Forte@Sun.COM 	if (h > 3)
43957836SJohn.Forte@Sun.COM 		return (NULL);
43967836SJohn.Forte@Sun.COM 	return (itask->itask_dbufs[h]);
43977836SJohn.Forte@Sun.COM }
43987836SJohn.Forte@Sun.COM 
43997836SJohn.Forte@Sun.COM /* ARGSUSED */
44007836SJohn.Forte@Sun.COM struct scsi_task *
stmf_task_alloc(struct stmf_local_port * lport,stmf_scsi_session_t * ss,uint8_t * lun,uint16_t cdb_length_in,uint16_t ext_id)44017836SJohn.Forte@Sun.COM stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
44027836SJohn.Forte@Sun.COM 			uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
44037836SJohn.Forte@Sun.COM {
44047836SJohn.Forte@Sun.COM 	stmf_lu_t *lu;
44057836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
44067836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
44077836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
44087836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t **ppitask;
44097836SJohn.Forte@Sun.COM 	scsi_task_t *task;
44107836SJohn.Forte@Sun.COM 	uint8_t	*l;
44117836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *lun_map_ent;
44127836SJohn.Forte@Sun.COM 	uint16_t cdb_length;
44137836SJohn.Forte@Sun.COM 	uint16_t luNbr;
44147836SJohn.Forte@Sun.COM 	uint8_t new_task = 0;
44157836SJohn.Forte@Sun.COM 
44167836SJohn.Forte@Sun.COM 	/*
44177836SJohn.Forte@Sun.COM 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
44187836SJohn.Forte@Sun.COM 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
44197836SJohn.Forte@Sun.COM 	 * depend upon this alignment.
44207836SJohn.Forte@Sun.COM 	 */
44217836SJohn.Forte@Sun.COM 	if (cdb_length_in >= 16)
44227836SJohn.Forte@Sun.COM 		cdb_length = cdb_length_in + 7;
44237836SJohn.Forte@Sun.COM 	else
44247836SJohn.Forte@Sun.COM 		cdb_length = 16 + 7;
44257836SJohn.Forte@Sun.COM 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
44267836SJohn.Forte@Sun.COM 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
44277836SJohn.Forte@Sun.COM 	rw_enter(iss->iss_lockp, RW_READER);
44287836SJohn.Forte@Sun.COM 	lun_map_ent =
44297836SJohn.Forte@Sun.COM 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
44307836SJohn.Forte@Sun.COM 	if (!lun_map_ent) {
44317836SJohn.Forte@Sun.COM 		lu = dlun0;
44327836SJohn.Forte@Sun.COM 	} else {
44337836SJohn.Forte@Sun.COM 		lu = lun_map_ent->ent_lu;
44347836SJohn.Forte@Sun.COM 	}
44357836SJohn.Forte@Sun.COM 	ilu = lu->lu_stmf_private;
44367836SJohn.Forte@Sun.COM 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
44377836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
44387836SJohn.Forte@Sun.COM 		return (NULL);
44397836SJohn.Forte@Sun.COM 	}
44407836SJohn.Forte@Sun.COM 	do {
44417836SJohn.Forte@Sun.COM 		if (ilu->ilu_free_tasks == NULL) {
44427836SJohn.Forte@Sun.COM 			new_task = 1;
44437836SJohn.Forte@Sun.COM 			break;
44447836SJohn.Forte@Sun.COM 		}
44457836SJohn.Forte@Sun.COM 		mutex_enter(&ilu->ilu_task_lock);
44467836SJohn.Forte@Sun.COM 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
44477836SJohn.Forte@Sun.COM 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
44488662SJordan.Vaughan@Sun.com 		    ppitask = &((*ppitask)->itask_lu_free_next))
44498662SJordan.Vaughan@Sun.com 			;
44507836SJohn.Forte@Sun.COM 		if (*ppitask) {
44517836SJohn.Forte@Sun.COM 			itask = *ppitask;
44527836SJohn.Forte@Sun.COM 			*ppitask = (*ppitask)->itask_lu_free_next;
44537836SJohn.Forte@Sun.COM 			ilu->ilu_ntasks_free--;
44547836SJohn.Forte@Sun.COM 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
44557836SJohn.Forte@Sun.COM 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
44567836SJohn.Forte@Sun.COM 		} else {
44577836SJohn.Forte@Sun.COM 			new_task = 1;
44587836SJohn.Forte@Sun.COM 		}
44597836SJohn.Forte@Sun.COM 		mutex_exit(&ilu->ilu_task_lock);
44607836SJohn.Forte@Sun.COM 	/* CONSTCOND */
44617836SJohn.Forte@Sun.COM 	} while (0);
44627836SJohn.Forte@Sun.COM 
44637836SJohn.Forte@Sun.COM 	if (!new_task) {
446412314SJames.Moore@Sun.COM 		/*
446512314SJames.Moore@Sun.COM 		 * Save the task_cdb pointer and zero per cmd fields.
446612314SJames.Moore@Sun.COM 		 * We know the task_cdb_length is large enough by task
446712314SJames.Moore@Sun.COM 		 * selection process above.
446812314SJames.Moore@Sun.COM 		 */
446912314SJames.Moore@Sun.COM 		uint8_t *save_cdb;
447012314SJames.Moore@Sun.COM 		uintptr_t t_start, t_end;
447112314SJames.Moore@Sun.COM 
44727836SJohn.Forte@Sun.COM 		task = itask->itask_task;
447312314SJames.Moore@Sun.COM 		save_cdb = task->task_cdb;	/* save */
447412314SJames.Moore@Sun.COM 		t_start = (uintptr_t)&task->task_flags;
447512314SJames.Moore@Sun.COM 		t_end = (uintptr_t)&task->task_extended_cmd;
447612314SJames.Moore@Sun.COM 		bzero((void *)t_start, (size_t)(t_end - t_start));
447712314SJames.Moore@Sun.COM 		task->task_cdb = save_cdb;	/* restore */
44787836SJohn.Forte@Sun.COM 		itask->itask_ncmds = 0;
44797836SJohn.Forte@Sun.COM 	} else {
44807836SJohn.Forte@Sun.COM 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
44817836SJohn.Forte@Sun.COM 		    cdb_length, AF_FORCE_NOSLEEP);
44827836SJohn.Forte@Sun.COM 		if (task == NULL) {
44837836SJohn.Forte@Sun.COM 			rw_exit(iss->iss_lockp);
44847836SJohn.Forte@Sun.COM 			return (NULL);
44857836SJohn.Forte@Sun.COM 		}
44867836SJohn.Forte@Sun.COM 		task->task_lu = lu;
44877836SJohn.Forte@Sun.COM 		l = task->task_lun_no;
44887836SJohn.Forte@Sun.COM 		l[0] = lun[0];
44897836SJohn.Forte@Sun.COM 		l[1] = lun[1];
44907836SJohn.Forte@Sun.COM 		l[2] = lun[2];
44917836SJohn.Forte@Sun.COM 		l[3] = lun[3];
44927836SJohn.Forte@Sun.COM 		l[4] = lun[4];
44937836SJohn.Forte@Sun.COM 		l[5] = lun[5];
44947836SJohn.Forte@Sun.COM 		l[6] = lun[6];
44957836SJohn.Forte@Sun.COM 		l[7] = lun[7];
44967836SJohn.Forte@Sun.COM 		task->task_cdb = (uint8_t *)task->task_port_private;
44977836SJohn.Forte@Sun.COM 		if ((ulong_t)(task->task_cdb) & 7ul) {
44987836SJohn.Forte@Sun.COM 			task->task_cdb = (uint8_t *)(((ulong_t)
44997836SJohn.Forte@Sun.COM 			    (task->task_cdb) + 7ul) & ~(7ul));
45007836SJohn.Forte@Sun.COM 		}
45017836SJohn.Forte@Sun.COM 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
45027836SJohn.Forte@Sun.COM 		itask->itask_cdb_buf_size = cdb_length;
450312625SJohn.Forte@Sun.COM 		mutex_init(&itask->itask_audit_mutex, NULL, MUTEX_DRIVER, NULL);
45047836SJohn.Forte@Sun.COM 	}
45057836SJohn.Forte@Sun.COM 	task->task_session = ss;
45067836SJohn.Forte@Sun.COM 	task->task_lport = lport;
45077836SJohn.Forte@Sun.COM 	task->task_cdb_length = cdb_length_in;
45087836SJohn.Forte@Sun.COM 	itask->itask_flags = ITASK_IN_TRANSITION;
450911773STim.Szeto@Sun.COM 	itask->itask_waitq_time = 0;
451011773STim.Szeto@Sun.COM 	itask->itask_lu_read_time = itask->itask_lu_write_time = 0;
451111773STim.Szeto@Sun.COM 	itask->itask_lport_read_time = itask->itask_lport_write_time = 0;
451211773STim.Szeto@Sun.COM 	itask->itask_read_xfer = itask->itask_write_xfer = 0;
451312625SJohn.Forte@Sun.COM 	itask->itask_audit_index = 0;
45147836SJohn.Forte@Sun.COM 
45157836SJohn.Forte@Sun.COM 	if (new_task) {
45167836SJohn.Forte@Sun.COM 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
45177836SJohn.Forte@Sun.COM 			rw_exit(iss->iss_lockp);
45187836SJohn.Forte@Sun.COM 			stmf_free(task);
45197836SJohn.Forte@Sun.COM 			return (NULL);
45207836SJohn.Forte@Sun.COM 		}
45217836SJohn.Forte@Sun.COM 		mutex_enter(&ilu->ilu_task_lock);
45227836SJohn.Forte@Sun.COM 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
45237836SJohn.Forte@Sun.COM 			mutex_exit(&ilu->ilu_task_lock);
45247836SJohn.Forte@Sun.COM 			rw_exit(iss->iss_lockp);
45257836SJohn.Forte@Sun.COM 			stmf_free(task);
45267836SJohn.Forte@Sun.COM 			return (NULL);
45277836SJohn.Forte@Sun.COM 		}
45287836SJohn.Forte@Sun.COM 		itask->itask_lu_next = ilu->ilu_tasks;
45297836SJohn.Forte@Sun.COM 		if (ilu->ilu_tasks)
45307836SJohn.Forte@Sun.COM 			ilu->ilu_tasks->itask_lu_prev = itask;
45317836SJohn.Forte@Sun.COM 		ilu->ilu_tasks = itask;
45327836SJohn.Forte@Sun.COM 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
45337836SJohn.Forte@Sun.COM 		ilu->ilu_ntasks++;
45347836SJohn.Forte@Sun.COM 		mutex_exit(&ilu->ilu_task_lock);
45357836SJohn.Forte@Sun.COM 	}
45367836SJohn.Forte@Sun.COM 
45377836SJohn.Forte@Sun.COM 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
45387836SJohn.Forte@Sun.COM 	atomic_add_32(itask->itask_ilu_task_cntr, 1);
45397836SJohn.Forte@Sun.COM 	itask->itask_start_time = ddi_get_lbolt();
45407836SJohn.Forte@Sun.COM 
45417836SJohn.Forte@Sun.COM 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
45427836SJohn.Forte@Sun.COM 	    lun_map_ent->ent_itl_datap) != NULL)) {
45437836SJohn.Forte@Sun.COM 		atomic_add_32(&itask->itask_itl_datap->itl_counter, 1);
45447836SJohn.Forte@Sun.COM 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
45457836SJohn.Forte@Sun.COM 	} else {
45467836SJohn.Forte@Sun.COM 		itask->itask_itl_datap = NULL;
45477836SJohn.Forte@Sun.COM 		task->task_lu_itl_handle = NULL;
45487836SJohn.Forte@Sun.COM 	}
45497836SJohn.Forte@Sun.COM 
45507836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
45517836SJohn.Forte@Sun.COM 	return (task);
45527836SJohn.Forte@Sun.COM }
45537836SJohn.Forte@Sun.COM 
455410725SJohn.Forte@Sun.COM static void
stmf_task_lu_free(scsi_task_t * task,stmf_i_scsi_session_t * iss)455510725SJohn.Forte@Sun.COM stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss)
45567836SJohn.Forte@Sun.COM {
45577836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
45587836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
45597836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
45607836SJohn.Forte@Sun.COM 
456110725SJohn.Forte@Sun.COM 	ASSERT(rw_lock_held(iss->iss_lockp));
45627836SJohn.Forte@Sun.COM 	itask->itask_flags = ITASK_IN_FREE_LIST;
456310878SJohn.Forte@Sun.COM 	itask->itask_proxy_msg_id = 0;
45647836SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
45657836SJohn.Forte@Sun.COM 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
45667836SJohn.Forte@Sun.COM 	ilu->ilu_free_tasks = itask;
45677836SJohn.Forte@Sun.COM 	ilu->ilu_ntasks_free++;
45687836SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
45697836SJohn.Forte@Sun.COM 	atomic_add_32(itask->itask_ilu_task_cntr, -1);
45707836SJohn.Forte@Sun.COM }
45717836SJohn.Forte@Sun.COM 
45727836SJohn.Forte@Sun.COM void
stmf_task_lu_check_freelist(stmf_i_lu_t * ilu)45737836SJohn.Forte@Sun.COM stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
45747836SJohn.Forte@Sun.COM {
45757836SJohn.Forte@Sun.COM 	uint32_t	num_to_release, ndx;
45767836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
45777836SJohn.Forte@Sun.COM 	stmf_lu_t	*lu = ilu->ilu_lu;
45787836SJohn.Forte@Sun.COM 
45797836SJohn.Forte@Sun.COM 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
45807836SJohn.Forte@Sun.COM 
45817836SJohn.Forte@Sun.COM 	/* free half of the minimal free of the free tasks */
45827836SJohn.Forte@Sun.COM 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
45837836SJohn.Forte@Sun.COM 	if (!num_to_release) {
45847836SJohn.Forte@Sun.COM 		return;
45857836SJohn.Forte@Sun.COM 	}
45867836SJohn.Forte@Sun.COM 	for (ndx = 0; ndx < num_to_release; ndx++) {
45877836SJohn.Forte@Sun.COM 		mutex_enter(&ilu->ilu_task_lock);
45887836SJohn.Forte@Sun.COM 		itask = ilu->ilu_free_tasks;
45897836SJohn.Forte@Sun.COM 		if (itask == NULL) {
45907836SJohn.Forte@Sun.COM 			mutex_exit(&ilu->ilu_task_lock);
45917836SJohn.Forte@Sun.COM 			break;
45927836SJohn.Forte@Sun.COM 		}
45937836SJohn.Forte@Sun.COM 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
45947836SJohn.Forte@Sun.COM 		ilu->ilu_ntasks_free--;
45957836SJohn.Forte@Sun.COM 		mutex_exit(&ilu->ilu_task_lock);
45967836SJohn.Forte@Sun.COM 
45977836SJohn.Forte@Sun.COM 		lu->lu_task_free(itask->itask_task);
45987836SJohn.Forte@Sun.COM 		mutex_enter(&ilu->ilu_task_lock);
45997836SJohn.Forte@Sun.COM 		if (itask->itask_lu_next)
46007836SJohn.Forte@Sun.COM 			itask->itask_lu_next->itask_lu_prev =
46017836SJohn.Forte@Sun.COM 			    itask->itask_lu_prev;
46027836SJohn.Forte@Sun.COM 		if (itask->itask_lu_prev)
46037836SJohn.Forte@Sun.COM 			itask->itask_lu_prev->itask_lu_next =
46047836SJohn.Forte@Sun.COM 			    itask->itask_lu_next;
46057836SJohn.Forte@Sun.COM 		else
46067836SJohn.Forte@Sun.COM 			ilu->ilu_tasks = itask->itask_lu_next;
46077836SJohn.Forte@Sun.COM 
46087836SJohn.Forte@Sun.COM 		ilu->ilu_ntasks--;
46097836SJohn.Forte@Sun.COM 		mutex_exit(&ilu->ilu_task_lock);
46107836SJohn.Forte@Sun.COM 		stmf_free(itask->itask_task);
46117836SJohn.Forte@Sun.COM 	}
46127836SJohn.Forte@Sun.COM }
46137836SJohn.Forte@Sun.COM 
46147836SJohn.Forte@Sun.COM /*
46157836SJohn.Forte@Sun.COM  * Called with stmf_lock held
46167836SJohn.Forte@Sun.COM  */
46177836SJohn.Forte@Sun.COM void
stmf_check_freetask()46187836SJohn.Forte@Sun.COM stmf_check_freetask()
46197836SJohn.Forte@Sun.COM {
46207836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
46217836SJohn.Forte@Sun.COM 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
46227836SJohn.Forte@Sun.COM 
46237836SJohn.Forte@Sun.COM 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
46247836SJohn.Forte@Sun.COM 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
46257836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
46267836SJohn.Forte@Sun.COM 		if (!ilu->ilu_ntasks_min_free) {
46277836SJohn.Forte@Sun.COM 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
46287836SJohn.Forte@Sun.COM 			continue;
46297836SJohn.Forte@Sun.COM 		}
46307836SJohn.Forte@Sun.COM 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
46317836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
46327836SJohn.Forte@Sun.COM 		stmf_task_lu_check_freelist(ilu);
46337836SJohn.Forte@Sun.COM 		/*
46347836SJohn.Forte@Sun.COM 		 * we do not care about the accuracy of
46357836SJohn.Forte@Sun.COM 		 * ilu_ntasks_min_free, so we don't lock here
46367836SJohn.Forte@Sun.COM 		 */
46377836SJohn.Forte@Sun.COM 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
46387836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
46397836SJohn.Forte@Sun.COM 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
46407836SJohn.Forte@Sun.COM 		cv_broadcast(&stmf_state.stmf_cv);
46417836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() >= endtime)
46427836SJohn.Forte@Sun.COM 			break;
46437836SJohn.Forte@Sun.COM 	}
46447836SJohn.Forte@Sun.COM }
46457836SJohn.Forte@Sun.COM 
46467836SJohn.Forte@Sun.COM void
stmf_do_ilu_timeouts(stmf_i_lu_t * ilu)46477836SJohn.Forte@Sun.COM stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
46487836SJohn.Forte@Sun.COM {
46497836SJohn.Forte@Sun.COM 	clock_t l = ddi_get_lbolt();
46507836SJohn.Forte@Sun.COM 	clock_t ps = drv_usectohz(1000000);
46517836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
46527836SJohn.Forte@Sun.COM 	scsi_task_t *task;
46537836SJohn.Forte@Sun.COM 	uint32_t to;
46547836SJohn.Forte@Sun.COM 
46557836SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
46567836SJohn.Forte@Sun.COM 	for (itask = ilu->ilu_tasks; itask != NULL;
46577836SJohn.Forte@Sun.COM 	    itask = itask->itask_lu_next) {
46587836SJohn.Forte@Sun.COM 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
46597836SJohn.Forte@Sun.COM 		    ITASK_BEING_ABORTED)) {
46607836SJohn.Forte@Sun.COM 			continue;
46617836SJohn.Forte@Sun.COM 		}
46627836SJohn.Forte@Sun.COM 		task = itask->itask_task;
46637836SJohn.Forte@Sun.COM 		if (task->task_timeout == 0)
46647836SJohn.Forte@Sun.COM 			to = stmf_default_task_timeout;
46657836SJohn.Forte@Sun.COM 		else
46667836SJohn.Forte@Sun.COM 			to = task->task_timeout;
46677836SJohn.Forte@Sun.COM 		if ((itask->itask_start_time + (to * ps)) > l)
46687836SJohn.Forte@Sun.COM 			continue;
46697836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
46707836SJohn.Forte@Sun.COM 		    STMF_TIMEOUT, NULL);
46717836SJohn.Forte@Sun.COM 	}
46727836SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
46737836SJohn.Forte@Sun.COM }
46747836SJohn.Forte@Sun.COM 
46757836SJohn.Forte@Sun.COM /*
46767836SJohn.Forte@Sun.COM  * Called with stmf_lock held
46777836SJohn.Forte@Sun.COM  */
46787836SJohn.Forte@Sun.COM void
stmf_check_ilu_timing()46797836SJohn.Forte@Sun.COM stmf_check_ilu_timing()
46807836SJohn.Forte@Sun.COM {
46817836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
46827836SJohn.Forte@Sun.COM 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
46837836SJohn.Forte@Sun.COM 
46847836SJohn.Forte@Sun.COM 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
46857836SJohn.Forte@Sun.COM 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
46867836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
46877836SJohn.Forte@Sun.COM 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
46887836SJohn.Forte@Sun.COM 			if (ilu->ilu_task_cntr2 == 0) {
46897836SJohn.Forte@Sun.COM 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
46907836SJohn.Forte@Sun.COM 				continue;
46917836SJohn.Forte@Sun.COM 			}
46927836SJohn.Forte@Sun.COM 		} else {
46937836SJohn.Forte@Sun.COM 			if (ilu->ilu_task_cntr1 == 0) {
46947836SJohn.Forte@Sun.COM 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
46957836SJohn.Forte@Sun.COM 				continue;
46967836SJohn.Forte@Sun.COM 			}
46977836SJohn.Forte@Sun.COM 		}
46987836SJohn.Forte@Sun.COM 		/*
46997836SJohn.Forte@Sun.COM 		 * If we are here then it means that there is some slowdown
47007836SJohn.Forte@Sun.COM 		 * in tasks on this lu. We need to check.
47017836SJohn.Forte@Sun.COM 		 */
47027836SJohn.Forte@Sun.COM 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
47037836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
47047836SJohn.Forte@Sun.COM 		stmf_do_ilu_timeouts(ilu);
47057836SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
47067836SJohn.Forte@Sun.COM 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
47077836SJohn.Forte@Sun.COM 		cv_broadcast(&stmf_state.stmf_cv);
47087836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() >= endtime)
47097836SJohn.Forte@Sun.COM 			break;
47107836SJohn.Forte@Sun.COM 	}
47117836SJohn.Forte@Sun.COM }
47127836SJohn.Forte@Sun.COM 
47137836SJohn.Forte@Sun.COM /*
47147836SJohn.Forte@Sun.COM  * Kills all tasks on a lu except tm_task
47157836SJohn.Forte@Sun.COM  */
47167836SJohn.Forte@Sun.COM void
stmf_task_lu_killall(stmf_lu_t * lu,scsi_task_t * tm_task,stmf_status_t s)47177836SJohn.Forte@Sun.COM stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
47187836SJohn.Forte@Sun.COM {
47197836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
47207836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
47217836SJohn.Forte@Sun.COM 
47227836SJohn.Forte@Sun.COM 	mutex_enter(&ilu->ilu_task_lock);
47237836SJohn.Forte@Sun.COM 
47247836SJohn.Forte@Sun.COM 	for (itask = ilu->ilu_tasks; itask != NULL;
47257836SJohn.Forte@Sun.COM 	    itask = itask->itask_lu_next) {
47267836SJohn.Forte@Sun.COM 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
47277836SJohn.Forte@Sun.COM 			continue;
47287836SJohn.Forte@Sun.COM 		if (itask->itask_task == tm_task)
47297836SJohn.Forte@Sun.COM 			continue;
47307836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
47317836SJohn.Forte@Sun.COM 	}
47327836SJohn.Forte@Sun.COM 	mutex_exit(&ilu->ilu_task_lock);
47337836SJohn.Forte@Sun.COM }
47347836SJohn.Forte@Sun.COM 
47357836SJohn.Forte@Sun.COM void
stmf_free_task_bufs(stmf_i_scsi_task_t * itask,stmf_local_port_t * lport)47367836SJohn.Forte@Sun.COM stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
47377836SJohn.Forte@Sun.COM {
47387836SJohn.Forte@Sun.COM 	int i;
47397836SJohn.Forte@Sun.COM 	uint8_t map;
47407836SJohn.Forte@Sun.COM 
474112314SJames.Moore@Sun.COM 	if ((map = itask->itask_allocated_buf_map) == 0)
474212314SJames.Moore@Sun.COM 		return;
474312314SJames.Moore@Sun.COM 	for (i = 0; i < 4; i++) {
474412314SJames.Moore@Sun.COM 		if (map & 1) {
474512314SJames.Moore@Sun.COM 			stmf_data_buf_t *dbuf;
474612314SJames.Moore@Sun.COM 
474712314SJames.Moore@Sun.COM 			dbuf = itask->itask_dbufs[i];
474812314SJames.Moore@Sun.COM 			if (dbuf->db_xfer_start_timestamp) {
474912314SJames.Moore@Sun.COM 				stmf_lport_xfer_done(itask, dbuf);
475012314SJames.Moore@Sun.COM 			}
475112314SJames.Moore@Sun.COM 			if (dbuf->db_flags & DB_LU_DATA_BUF) {
475212314SJames.Moore@Sun.COM 				/*
475312314SJames.Moore@Sun.COM 				 * LU needs to clean up buffer.
475412314SJames.Moore@Sun.COM 				 * LU is required to free the buffer
475512314SJames.Moore@Sun.COM 				 * in the xfer_done handler.
475612314SJames.Moore@Sun.COM 				 */
475712314SJames.Moore@Sun.COM 				scsi_task_t *task = itask->itask_task;
475812314SJames.Moore@Sun.COM 				stmf_lu_t *lu = task->task_lu;
475912314SJames.Moore@Sun.COM 
476012314SJames.Moore@Sun.COM 				lu->lu_dbuf_free(task, dbuf);
476112314SJames.Moore@Sun.COM 				ASSERT(((itask->itask_allocated_buf_map>>i)
476212314SJames.Moore@Sun.COM 				    & 1) == 0); /* must be gone */
476312314SJames.Moore@Sun.COM 			} else {
476412314SJames.Moore@Sun.COM 				ASSERT(dbuf->db_lu_private == NULL);
476512314SJames.Moore@Sun.COM 				dbuf->db_lu_private = NULL;
47667836SJohn.Forte@Sun.COM 				lport->lport_ds->ds_free_data_buf(
47677836SJohn.Forte@Sun.COM 				    lport->lport_ds, dbuf);
47687836SJohn.Forte@Sun.COM 			}
476912314SJames.Moore@Sun.COM 		}
477012314SJames.Moore@Sun.COM 		map >>= 1;
477112314SJames.Moore@Sun.COM 	}
477212314SJames.Moore@Sun.COM 	itask->itask_allocated_buf_map = 0;
47737836SJohn.Forte@Sun.COM }
47747836SJohn.Forte@Sun.COM 
47757836SJohn.Forte@Sun.COM void
stmf_task_free(scsi_task_t * task)47767836SJohn.Forte@Sun.COM stmf_task_free(scsi_task_t *task)
47777836SJohn.Forte@Sun.COM {
47787836SJohn.Forte@Sun.COM 	stmf_local_port_t *lport = task->task_lport;
47797836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
47808662SJordan.Vaughan@Sun.com 	    task->task_stmf_private;
478110725SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
478210725SJohn.Forte@Sun.COM 	    task->task_session->ss_stmf_private;
47837836SJohn.Forte@Sun.COM 
478412625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_TASK_FREE, CMD_OR_IOF_NA, NULL);
478512625SJohn.Forte@Sun.COM 
47867836SJohn.Forte@Sun.COM 	stmf_free_task_bufs(itask, lport);
478711773STim.Szeto@Sun.COM 	stmf_itl_task_done(itask);
478811773STim.Szeto@Sun.COM 	DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task,
478911773STim.Szeto@Sun.COM 	    hrtime_t,
479011773STim.Szeto@Sun.COM 	    itask->itask_done_timestamp - itask->itask_start_timestamp);
47917836SJohn.Forte@Sun.COM 	if (itask->itask_itl_datap) {
47927836SJohn.Forte@Sun.COM 		if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter,
47937836SJohn.Forte@Sun.COM 		    -1) == 0) {
47947836SJohn.Forte@Sun.COM 			stmf_release_itl_handle(task->task_lu,
47958662SJordan.Vaughan@Sun.com 			    itask->itask_itl_datap);
47967836SJohn.Forte@Sun.COM 		}
47977836SJohn.Forte@Sun.COM 	}
479810725SJohn.Forte@Sun.COM 
479910725SJohn.Forte@Sun.COM 	rw_enter(iss->iss_lockp, RW_READER);
48007836SJohn.Forte@Sun.COM 	lport->lport_task_free(task);
48017836SJohn.Forte@Sun.COM 	if (itask->itask_worker) {
48027836SJohn.Forte@Sun.COM 		atomic_add_32(&stmf_cur_ntasks, -1);
48037836SJohn.Forte@Sun.COM 		atomic_add_32(&itask->itask_worker->worker_ref_count, -1);
48047836SJohn.Forte@Sun.COM 	}
48057836SJohn.Forte@Sun.COM 	/*
48067836SJohn.Forte@Sun.COM 	 * After calling stmf_task_lu_free, the task pointer can no longer
48077836SJohn.Forte@Sun.COM 	 * be trusted.
48087836SJohn.Forte@Sun.COM 	 */
480910725SJohn.Forte@Sun.COM 	stmf_task_lu_free(task, iss);
481010725SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
48117836SJohn.Forte@Sun.COM }
48127836SJohn.Forte@Sun.COM 
48137836SJohn.Forte@Sun.COM void
stmf_post_task(scsi_task_t * task,stmf_data_buf_t * dbuf)48147836SJohn.Forte@Sun.COM stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
48157836SJohn.Forte@Sun.COM {
48167836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
48177836SJohn.Forte@Sun.COM 	    task->task_stmf_private;
48187836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
48197836SJohn.Forte@Sun.COM 	int nv;
48207836SJohn.Forte@Sun.COM 	uint32_t old, new;
48217836SJohn.Forte@Sun.COM 	uint32_t ct;
48227836SJohn.Forte@Sun.COM 	stmf_worker_t *w, *w1;
48237836SJohn.Forte@Sun.COM 	uint8_t tm;
48247836SJohn.Forte@Sun.COM 
48257836SJohn.Forte@Sun.COM 	if (task->task_max_nbufs > 4)
48267836SJohn.Forte@Sun.COM 		task->task_max_nbufs = 4;
48277836SJohn.Forte@Sun.COM 	task->task_cur_nbufs = 0;
48287836SJohn.Forte@Sun.COM 	/* Latest value of currently running tasks */
48297836SJohn.Forte@Sun.COM 	ct = atomic_add_32_nv(&stmf_cur_ntasks, 1);
48307836SJohn.Forte@Sun.COM 
48317836SJohn.Forte@Sun.COM 	/* Select the next worker using round robin */
48327836SJohn.Forte@Sun.COM 	nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1);
48337836SJohn.Forte@Sun.COM 	if (nv >= stmf_nworkers_accepting_cmds) {
48347836SJohn.Forte@Sun.COM 		int s = nv;
48357836SJohn.Forte@Sun.COM 		do {
48367836SJohn.Forte@Sun.COM 			nv -= stmf_nworkers_accepting_cmds;
48377836SJohn.Forte@Sun.COM 		} while (nv >= stmf_nworkers_accepting_cmds);
48387836SJohn.Forte@Sun.COM 		if (nv < 0)
48397836SJohn.Forte@Sun.COM 			nv = 0;
48407836SJohn.Forte@Sun.COM 		/* Its ok if this cas fails */
48417836SJohn.Forte@Sun.COM 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
48427836SJohn.Forte@Sun.COM 		    s, nv);
48437836SJohn.Forte@Sun.COM 	}
48447836SJohn.Forte@Sun.COM 	w = &stmf_workers[nv];
48457836SJohn.Forte@Sun.COM 
48467836SJohn.Forte@Sun.COM 	/*
48477836SJohn.Forte@Sun.COM 	 * A worker can be pinned by interrupt. So select the next one
48487836SJohn.Forte@Sun.COM 	 * if it has lower load.
48497836SJohn.Forte@Sun.COM 	 */
48507836SJohn.Forte@Sun.COM 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
48517836SJohn.Forte@Sun.COM 		w1 = stmf_workers;
48527836SJohn.Forte@Sun.COM 	} else {
48537836SJohn.Forte@Sun.COM 		w1 = &stmf_workers[nv + 1];
48547836SJohn.Forte@Sun.COM 	}
48557836SJohn.Forte@Sun.COM 	if (w1->worker_queue_depth < w->worker_queue_depth)
48567836SJohn.Forte@Sun.COM 		w = w1;
48579435STim.Szeto@Sun.COM 
48587836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
48597836SJohn.Forte@Sun.COM 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
48607836SJohn.Forte@Sun.COM 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
48617836SJohn.Forte@Sun.COM 		/*
48627836SJohn.Forte@Sun.COM 		 * Maybe we are in the middle of a change. Just go to
48637836SJohn.Forte@Sun.COM 		 * the 1st worker.
48647836SJohn.Forte@Sun.COM 		 */
48657836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
48667836SJohn.Forte@Sun.COM 		w = stmf_workers;
48677836SJohn.Forte@Sun.COM 		mutex_enter(&w->worker_lock);
48687836SJohn.Forte@Sun.COM 	}
48697836SJohn.Forte@Sun.COM 	itask->itask_worker = w;
48707836SJohn.Forte@Sun.COM 	/*
48717836SJohn.Forte@Sun.COM 	 * Track max system load inside the worker as we already have the
48727836SJohn.Forte@Sun.COM 	 * worker lock (no point implementing another lock). The service
48737836SJohn.Forte@Sun.COM 	 * thread will do the comparisons and figure out the max overall
48747836SJohn.Forte@Sun.COM 	 * system load.
48757836SJohn.Forte@Sun.COM 	 */
48767836SJohn.Forte@Sun.COM 	if (w->worker_max_sys_qdepth_pu < ct)
48777836SJohn.Forte@Sun.COM 		w->worker_max_sys_qdepth_pu = ct;
48787836SJohn.Forte@Sun.COM 
48797836SJohn.Forte@Sun.COM 	do {
48807836SJohn.Forte@Sun.COM 		old = new = itask->itask_flags;
48817836SJohn.Forte@Sun.COM 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
48827836SJohn.Forte@Sun.COM 		if (task->task_mgmt_function) {
48837836SJohn.Forte@Sun.COM 			tm = task->task_mgmt_function;
48847836SJohn.Forte@Sun.COM 			if ((tm == TM_TARGET_RESET) ||
48857836SJohn.Forte@Sun.COM 			    (tm == TM_TARGET_COLD_RESET) ||
48867836SJohn.Forte@Sun.COM 			    (tm == TM_TARGET_WARM_RESET)) {
48877836SJohn.Forte@Sun.COM 				new |= ITASK_DEFAULT_HANDLING;
48887836SJohn.Forte@Sun.COM 			}
48897836SJohn.Forte@Sun.COM 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
48907836SJohn.Forte@Sun.COM 			new |= ITASK_DEFAULT_HANDLING;
48917836SJohn.Forte@Sun.COM 		}
48927836SJohn.Forte@Sun.COM 		new &= ~ITASK_IN_TRANSITION;
48937836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
489411773STim.Szeto@Sun.COM 
489511773STim.Szeto@Sun.COM 	stmf_itl_task_start(itask);
489611773STim.Szeto@Sun.COM 
48977836SJohn.Forte@Sun.COM 	itask->itask_worker_next = NULL;
48987836SJohn.Forte@Sun.COM 	if (w->worker_task_tail) {
48997836SJohn.Forte@Sun.COM 		w->worker_task_tail->itask_worker_next = itask;
49007836SJohn.Forte@Sun.COM 	} else {
49017836SJohn.Forte@Sun.COM 		w->worker_task_head = itask;
49027836SJohn.Forte@Sun.COM 	}
49037836SJohn.Forte@Sun.COM 	w->worker_task_tail = itask;
49047836SJohn.Forte@Sun.COM 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
49057836SJohn.Forte@Sun.COM 		w->worker_max_qdepth_pu = w->worker_queue_depth;
49067836SJohn.Forte@Sun.COM 	}
490711773STim.Szeto@Sun.COM 	/* Measure task waitq time */
490811773STim.Szeto@Sun.COM 	itask->itask_waitq_enter_timestamp = gethrtime();
49097836SJohn.Forte@Sun.COM 	atomic_add_32(&w->worker_ref_count, 1);
49107836SJohn.Forte@Sun.COM 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
49117836SJohn.Forte@Sun.COM 	itask->itask_ncmds = 1;
491212625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_TASK_START, CMD_OR_IOF_NA, dbuf);
49137836SJohn.Forte@Sun.COM 	if (dbuf) {
49147836SJohn.Forte@Sun.COM 		itask->itask_allocated_buf_map = 1;
49157836SJohn.Forte@Sun.COM 		itask->itask_dbufs[0] = dbuf;
49167836SJohn.Forte@Sun.COM 		dbuf->db_handle = 0;
49177836SJohn.Forte@Sun.COM 	} else {
49187836SJohn.Forte@Sun.COM 		itask->itask_allocated_buf_map = 0;
49197836SJohn.Forte@Sun.COM 		itask->itask_dbufs[0] = NULL;
49207836SJohn.Forte@Sun.COM 	}
49219435STim.Szeto@Sun.COM 
492211773STim.Szeto@Sun.COM 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) {
492311773STim.Szeto@Sun.COM 		w->worker_signal_timestamp = gethrtime();
492411773STim.Szeto@Sun.COM 		DTRACE_PROBE2(worker__signal, stmf_worker_t *, w,
492511773STim.Szeto@Sun.COM 		    scsi_task_t *, task);
49267836SJohn.Forte@Sun.COM 		cv_signal(&w->worker_cv);
492711773STim.Szeto@Sun.COM 	}
49287836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
49297836SJohn.Forte@Sun.COM 
49307836SJohn.Forte@Sun.COM 	/*
49317836SJohn.Forte@Sun.COM 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
49327836SJohn.Forte@Sun.COM 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
49337836SJohn.Forte@Sun.COM 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
49347836SJohn.Forte@Sun.COM 	 */
49357836SJohn.Forte@Sun.COM 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
49367836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
49377836SJohn.Forte@Sun.COM 	}
49387836SJohn.Forte@Sun.COM }
49397836SJohn.Forte@Sun.COM 
494012625SJohn.Forte@Sun.COM static void
stmf_task_audit(stmf_i_scsi_task_t * itask,task_audit_event_t te,uint32_t cmd_or_iof,stmf_data_buf_t * dbuf)494112625SJohn.Forte@Sun.COM stmf_task_audit(stmf_i_scsi_task_t *itask,
494212625SJohn.Forte@Sun.COM     task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf)
494312625SJohn.Forte@Sun.COM {
494412625SJohn.Forte@Sun.COM 	stmf_task_audit_rec_t *ar;
494512625SJohn.Forte@Sun.COM 
494612625SJohn.Forte@Sun.COM 	mutex_enter(&itask->itask_audit_mutex);
494712625SJohn.Forte@Sun.COM 	ar = &itask->itask_audit_records[itask->itask_audit_index++];
494812625SJohn.Forte@Sun.COM 	itask->itask_audit_index &= (ITASK_TASK_AUDIT_DEPTH - 1);
494912625SJohn.Forte@Sun.COM 	ar->ta_event = te;
495012625SJohn.Forte@Sun.COM 	ar->ta_cmd_or_iof = cmd_or_iof;
495112625SJohn.Forte@Sun.COM 	ar->ta_itask_flags = itask->itask_flags;
495212625SJohn.Forte@Sun.COM 	ar->ta_dbuf = dbuf;
495312625SJohn.Forte@Sun.COM 	gethrestime(&ar->ta_timestamp);
495412625SJohn.Forte@Sun.COM 	mutex_exit(&itask->itask_audit_mutex);
495512625SJohn.Forte@Sun.COM }
495612625SJohn.Forte@Sun.COM 
495712625SJohn.Forte@Sun.COM 
49587836SJohn.Forte@Sun.COM /*
49597836SJohn.Forte@Sun.COM  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
49607836SJohn.Forte@Sun.COM  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
49617836SJohn.Forte@Sun.COM  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
49627836SJohn.Forte@Sun.COM  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
49637836SJohn.Forte@Sun.COM  * the LU will make this call only if we call the LU's abort entry point.
49647836SJohn.Forte@Sun.COM  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
49657836SJohn.Forte@Sun.COM  *
49667836SJohn.Forte@Sun.COM  * Same logic applies for the port.
49677836SJohn.Forte@Sun.COM  *
49687836SJohn.Forte@Sun.COM  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
49697836SJohn.Forte@Sun.COM  * and KNOWN_TO_TGT_PORT are reset.
49707836SJohn.Forte@Sun.COM  *
49717836SJohn.Forte@Sun.COM  * +++++++++++++++++++++++++++++++++++++++++++++++
49727836SJohn.Forte@Sun.COM  */
49737836SJohn.Forte@Sun.COM 
49747836SJohn.Forte@Sun.COM stmf_status_t
stmf_xfer_data(scsi_task_t * task,stmf_data_buf_t * dbuf,uint32_t ioflags)49757836SJohn.Forte@Sun.COM stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
49767836SJohn.Forte@Sun.COM {
497712625SJohn.Forte@Sun.COM 	stmf_status_t ret = STMF_SUCCESS;
49788859STim.Szeto@Sun.COM 
49797836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
49807836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
49817836SJohn.Forte@Sun.COM 
498212625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_XFER_START, ioflags, dbuf);
498312625SJohn.Forte@Sun.COM 
49847836SJohn.Forte@Sun.COM 	if (ioflags & STMF_IOF_LU_DONE) {
49857836SJohn.Forte@Sun.COM 		uint32_t new, old;
49867836SJohn.Forte@Sun.COM 		do {
49877836SJohn.Forte@Sun.COM 			new = old = itask->itask_flags;
49887836SJohn.Forte@Sun.COM 			if (new & ITASK_BEING_ABORTED)
49897836SJohn.Forte@Sun.COM 				return (STMF_ABORTED);
49907836SJohn.Forte@Sun.COM 			new &= ~ITASK_KNOWN_TO_LU;
49917836SJohn.Forte@Sun.COM 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
49927836SJohn.Forte@Sun.COM 	}
49937836SJohn.Forte@Sun.COM 	if (itask->itask_flags & ITASK_BEING_ABORTED)
49947836SJohn.Forte@Sun.COM 		return (STMF_ABORTED);
49957836SJohn.Forte@Sun.COM #ifdef	DEBUG
499611773STim.Szeto@Sun.COM 	if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) {
49978662SJordan.Vaughan@Sun.com 		if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) ==
49988662SJordan.Vaughan@Sun.com 		    1)
49997836SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
50007836SJohn.Forte@Sun.COM 	}
50017836SJohn.Forte@Sun.COM #endif
50029435STim.Szeto@Sun.COM 
50039435STim.Szeto@Sun.COM 	stmf_update_kstat_lu_io(task, dbuf);
50049435STim.Szeto@Sun.COM 	stmf_update_kstat_lport_io(task, dbuf);
500511773STim.Szeto@Sun.COM 	stmf_lport_xfer_start(itask, dbuf);
500611773STim.Szeto@Sun.COM 	if (ioflags & STMF_IOF_STATS_ONLY) {
500711773STim.Szeto@Sun.COM 		stmf_lport_xfer_done(itask, dbuf);
500811773STim.Szeto@Sun.COM 		return (STMF_SUCCESS);
500911773STim.Szeto@Sun.COM 	}
501011773STim.Szeto@Sun.COM 
501112625SJohn.Forte@Sun.COM 	dbuf->db_flags |= DB_LPORT_XFER_ACTIVE;
50128859STim.Szeto@Sun.COM 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
501311773STim.Szeto@Sun.COM 
501411773STim.Szeto@Sun.COM 	/*
501511773STim.Szeto@Sun.COM 	 * Port provider may have already called the buffer callback in
501611773STim.Szeto@Sun.COM 	 * which case dbuf->db_xfer_start_timestamp will be 0.
501711773STim.Szeto@Sun.COM 	 */
501812625SJohn.Forte@Sun.COM 	if (ret != STMF_SUCCESS) {
501912625SJohn.Forte@Sun.COM 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
502012625SJohn.Forte@Sun.COM 		if (dbuf->db_xfer_start_timestamp != 0)
502112625SJohn.Forte@Sun.COM 			stmf_lport_xfer_done(itask, dbuf);
502211773STim.Szeto@Sun.COM 	}
502311773STim.Szeto@Sun.COM 
50248859STim.Szeto@Sun.COM 	return (ret);
50257836SJohn.Forte@Sun.COM }
50267836SJohn.Forte@Sun.COM 
50277836SJohn.Forte@Sun.COM void
stmf_data_xfer_done(scsi_task_t * task,stmf_data_buf_t * dbuf,uint32_t iof)50287836SJohn.Forte@Sun.COM stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
50297836SJohn.Forte@Sun.COM {
50307836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
50317836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
503212625SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
50337836SJohn.Forte@Sun.COM 	stmf_worker_t *w = itask->itask_worker;
50347836SJohn.Forte@Sun.COM 	uint32_t new, old;
503511773STim.Szeto@Sun.COM 	uint8_t update_queue_flags, free_it, queue_it;
503611773STim.Szeto@Sun.COM 
503711773STim.Szeto@Sun.COM 	stmf_lport_xfer_done(itask, dbuf);
50387836SJohn.Forte@Sun.COM 
503912625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_XFER_DONE, iof, dbuf);
504012625SJohn.Forte@Sun.COM 
504112625SJohn.Forte@Sun.COM 	/* Guard against unexpected completions from the lport */
504212625SJohn.Forte@Sun.COM 	if (dbuf->db_flags & DB_LPORT_XFER_ACTIVE) {
504312625SJohn.Forte@Sun.COM 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
504412625SJohn.Forte@Sun.COM 	} else {
504512625SJohn.Forte@Sun.COM 		/*
504612625SJohn.Forte@Sun.COM 		 * This should never happen.
504712625SJohn.Forte@Sun.COM 		 */
504812625SJohn.Forte@Sun.COM 		ilport = task->task_lport->lport_stmf_private;
504912625SJohn.Forte@Sun.COM 		ilport->ilport_unexpected_comp++;
505012625SJohn.Forte@Sun.COM 		cmn_err(CE_PANIC, "Unexpected xfer completion task %p dbuf %p",
505112625SJohn.Forte@Sun.COM 		    (void *)task, (void *)dbuf);
505212625SJohn.Forte@Sun.COM 		return;
505312625SJohn.Forte@Sun.COM 	}
505412625SJohn.Forte@Sun.COM 
50557836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
50567836SJohn.Forte@Sun.COM 	do {
50577836SJohn.Forte@Sun.COM 		new = old = itask->itask_flags;
50587836SJohn.Forte@Sun.COM 		if (old & ITASK_BEING_ABORTED) {
50597836SJohn.Forte@Sun.COM 			mutex_exit(&w->worker_lock);
50607836SJohn.Forte@Sun.COM 			return;
50617836SJohn.Forte@Sun.COM 		}
50627836SJohn.Forte@Sun.COM 		free_it = 0;
50637836SJohn.Forte@Sun.COM 		if (iof & STMF_IOF_LPORT_DONE) {
50647836SJohn.Forte@Sun.COM 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
50657836SJohn.Forte@Sun.COM 			task->task_completion_status = dbuf->db_xfer_status;
50667836SJohn.Forte@Sun.COM 			free_it = 1;
50677836SJohn.Forte@Sun.COM 		}
50687836SJohn.Forte@Sun.COM 		/*
50697836SJohn.Forte@Sun.COM 		 * If the task is known to LU then queue it. But if
50707836SJohn.Forte@Sun.COM 		 * it is already queued (multiple completions) then
50717836SJohn.Forte@Sun.COM 		 * just update the buffer information by grabbing the
50727836SJohn.Forte@Sun.COM 		 * worker lock. If the task is not known to LU,
50737836SJohn.Forte@Sun.COM 		 * completed/aborted, then see if we need to
50747836SJohn.Forte@Sun.COM 		 * free this task.
50757836SJohn.Forte@Sun.COM 		 */
50767836SJohn.Forte@Sun.COM 		if (old & ITASK_KNOWN_TO_LU) {
50777836SJohn.Forte@Sun.COM 			free_it = 0;
50787836SJohn.Forte@Sun.COM 			update_queue_flags = 1;
50797836SJohn.Forte@Sun.COM 			if (old & ITASK_IN_WORKER_QUEUE) {
50807836SJohn.Forte@Sun.COM 				queue_it = 0;
50817836SJohn.Forte@Sun.COM 			} else {
50827836SJohn.Forte@Sun.COM 				queue_it = 1;
50837836SJohn.Forte@Sun.COM 				new |= ITASK_IN_WORKER_QUEUE;
50847836SJohn.Forte@Sun.COM 			}
50857836SJohn.Forte@Sun.COM 		} else {
50867836SJohn.Forte@Sun.COM 			update_queue_flags = 0;
50877836SJohn.Forte@Sun.COM 			queue_it = 0;
50887836SJohn.Forte@Sun.COM 		}
50897836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
50907836SJohn.Forte@Sun.COM 
50917836SJohn.Forte@Sun.COM 	if (update_queue_flags) {
50927836SJohn.Forte@Sun.COM 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
50937836SJohn.Forte@Sun.COM 
50947836SJohn.Forte@Sun.COM 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
50957836SJohn.Forte@Sun.COM 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
50967836SJohn.Forte@Sun.COM 		if (queue_it) {
50977836SJohn.Forte@Sun.COM 			itask->itask_worker_next = NULL;
50987836SJohn.Forte@Sun.COM 			if (w->worker_task_tail) {
50997836SJohn.Forte@Sun.COM 				w->worker_task_tail->itask_worker_next = itask;
51007836SJohn.Forte@Sun.COM 			} else {
51017836SJohn.Forte@Sun.COM 				w->worker_task_head = itask;
51027836SJohn.Forte@Sun.COM 			}
51037836SJohn.Forte@Sun.COM 			w->worker_task_tail = itask;
510411773STim.Szeto@Sun.COM 			/* Measure task waitq time */
510511773STim.Szeto@Sun.COM 			itask->itask_waitq_enter_timestamp = gethrtime();
51067836SJohn.Forte@Sun.COM 			if (++(w->worker_queue_depth) >
51077836SJohn.Forte@Sun.COM 			    w->worker_max_qdepth_pu) {
51087836SJohn.Forte@Sun.COM 				w->worker_max_qdepth_pu = w->worker_queue_depth;
51097836SJohn.Forte@Sun.COM 			}
51107836SJohn.Forte@Sun.COM 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
51117836SJohn.Forte@Sun.COM 				cv_signal(&w->worker_cv);
51127836SJohn.Forte@Sun.COM 		}
51137836SJohn.Forte@Sun.COM 	}
51147836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
51157836SJohn.Forte@Sun.COM 
51167836SJohn.Forte@Sun.COM 	if (free_it) {
51177836SJohn.Forte@Sun.COM 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
51187836SJohn.Forte@Sun.COM 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
51197836SJohn.Forte@Sun.COM 		    ITASK_BEING_ABORTED)) == 0) {
51207836SJohn.Forte@Sun.COM 			stmf_task_free(task);
51217836SJohn.Forte@Sun.COM 		}
51227836SJohn.Forte@Sun.COM 	}
51237836SJohn.Forte@Sun.COM }
51247836SJohn.Forte@Sun.COM 
51257836SJohn.Forte@Sun.COM stmf_status_t
stmf_send_scsi_status(scsi_task_t * task,uint32_t ioflags)51267836SJohn.Forte@Sun.COM stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
51277836SJohn.Forte@Sun.COM {
51288859STim.Szeto@Sun.COM 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
51298859STim.Szeto@Sun.COM 
51307836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
51318662SJordan.Vaughan@Sun.com 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
513212625SJohn.Forte@Sun.COM 
513312625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_SEND_STATUS, ioflags, NULL);
513412625SJohn.Forte@Sun.COM 
51357836SJohn.Forte@Sun.COM 	if (ioflags & STMF_IOF_LU_DONE) {
51367836SJohn.Forte@Sun.COM 		uint32_t new, old;
51377836SJohn.Forte@Sun.COM 		do {
51387836SJohn.Forte@Sun.COM 			new = old = itask->itask_flags;
51397836SJohn.Forte@Sun.COM 			if (new & ITASK_BEING_ABORTED)
51407836SJohn.Forte@Sun.COM 				return (STMF_ABORTED);
51417836SJohn.Forte@Sun.COM 			new &= ~ITASK_KNOWN_TO_LU;
51427836SJohn.Forte@Sun.COM 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
51437836SJohn.Forte@Sun.COM 	}
51447836SJohn.Forte@Sun.COM 
51457836SJohn.Forte@Sun.COM 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
51467836SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
51477836SJohn.Forte@Sun.COM 	}
51487836SJohn.Forte@Sun.COM 
51497836SJohn.Forte@Sun.COM 	if (itask->itask_flags & ITASK_BEING_ABORTED)
51507836SJohn.Forte@Sun.COM 		return (STMF_ABORTED);
51517836SJohn.Forte@Sun.COM 
51527836SJohn.Forte@Sun.COM 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
51537836SJohn.Forte@Sun.COM 		task->task_status_ctrl = 0;
51547836SJohn.Forte@Sun.COM 		task->task_resid = 0;
51557836SJohn.Forte@Sun.COM 	} else if (task->task_cmd_xfer_length >
51567836SJohn.Forte@Sun.COM 	    task->task_expected_xfer_length) {
51577836SJohn.Forte@Sun.COM 		task->task_status_ctrl = TASK_SCTRL_OVER;
51587836SJohn.Forte@Sun.COM 		task->task_resid = task->task_cmd_xfer_length -
51598662SJordan.Vaughan@Sun.com 		    task->task_expected_xfer_length;
51607836SJohn.Forte@Sun.COM 	} else if (task->task_nbytes_transferred <
51618662SJordan.Vaughan@Sun.com 	    task->task_expected_xfer_length) {
51627836SJohn.Forte@Sun.COM 		task->task_status_ctrl = TASK_SCTRL_UNDER;
51637836SJohn.Forte@Sun.COM 		task->task_resid = task->task_expected_xfer_length -
51648662SJordan.Vaughan@Sun.com 		    task->task_nbytes_transferred;
51657836SJohn.Forte@Sun.COM 	} else {
51667836SJohn.Forte@Sun.COM 		task->task_status_ctrl = 0;
51677836SJohn.Forte@Sun.COM 		task->task_resid = 0;
51687836SJohn.Forte@Sun.COM 	}
51697836SJohn.Forte@Sun.COM 	return (task->task_lport->lport_send_status(task, ioflags));
51707836SJohn.Forte@Sun.COM }
51717836SJohn.Forte@Sun.COM 
51727836SJohn.Forte@Sun.COM void
stmf_send_status_done(scsi_task_t * task,stmf_status_t s,uint32_t iof)51737836SJohn.Forte@Sun.COM stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
51747836SJohn.Forte@Sun.COM {
51757836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
51767836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
51777836SJohn.Forte@Sun.COM 	stmf_worker_t *w = itask->itask_worker;
51787836SJohn.Forte@Sun.COM 	uint32_t new, old;
517911773STim.Szeto@Sun.COM 	uint8_t free_it, queue_it;
51807836SJohn.Forte@Sun.COM 
518112625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_SEND_STATUS_DONE, iof, NULL);
518212625SJohn.Forte@Sun.COM 
51837836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
51847836SJohn.Forte@Sun.COM 	do {
51857836SJohn.Forte@Sun.COM 		new = old = itask->itask_flags;
51867836SJohn.Forte@Sun.COM 		if (old & ITASK_BEING_ABORTED) {
51877836SJohn.Forte@Sun.COM 			mutex_exit(&w->worker_lock);
51887836SJohn.Forte@Sun.COM 			return;
51897836SJohn.Forte@Sun.COM 		}
51907836SJohn.Forte@Sun.COM 		free_it = 0;
51917836SJohn.Forte@Sun.COM 		if (iof & STMF_IOF_LPORT_DONE) {
51927836SJohn.Forte@Sun.COM 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
51937836SJohn.Forte@Sun.COM 			free_it = 1;
51947836SJohn.Forte@Sun.COM 		}
51957836SJohn.Forte@Sun.COM 		/*
51967836SJohn.Forte@Sun.COM 		 * If the task is known to LU then queue it. But if
51977836SJohn.Forte@Sun.COM 		 * it is already queued (multiple completions) then
51987836SJohn.Forte@Sun.COM 		 * just update the buffer information by grabbing the
51997836SJohn.Forte@Sun.COM 		 * worker lock. If the task is not known to LU,
52007836SJohn.Forte@Sun.COM 		 * completed/aborted, then see if we need to
52017836SJohn.Forte@Sun.COM 		 * free this task.
52027836SJohn.Forte@Sun.COM 		 */
52037836SJohn.Forte@Sun.COM 		if (old & ITASK_KNOWN_TO_LU) {
52047836SJohn.Forte@Sun.COM 			free_it = 0;
52057836SJohn.Forte@Sun.COM 			queue_it = 1;
52067836SJohn.Forte@Sun.COM 			if (old & ITASK_IN_WORKER_QUEUE) {
52077836SJohn.Forte@Sun.COM 				cmn_err(CE_PANIC, "status completion received"
52087836SJohn.Forte@Sun.COM 				    " when task is already in worker queue "
52097836SJohn.Forte@Sun.COM 				    " task = %p", (void *)task);
52107836SJohn.Forte@Sun.COM 			}
52117836SJohn.Forte@Sun.COM 			new |= ITASK_IN_WORKER_QUEUE;
52127836SJohn.Forte@Sun.COM 		} else {
52137836SJohn.Forte@Sun.COM 			queue_it = 0;
52147836SJohn.Forte@Sun.COM 		}
52157836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
52167836SJohn.Forte@Sun.COM 	task->task_completion_status = s;
52177836SJohn.Forte@Sun.COM 
52189435STim.Szeto@Sun.COM 
52197836SJohn.Forte@Sun.COM 	if (queue_it) {
52207836SJohn.Forte@Sun.COM 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
52217836SJohn.Forte@Sun.COM 		itask->itask_cmd_stack[itask->itask_ncmds++] =
52227836SJohn.Forte@Sun.COM 		    ITASK_CMD_STATUS_DONE;
52237836SJohn.Forte@Sun.COM 		itask->itask_worker_next = NULL;
52247836SJohn.Forte@Sun.COM 		if (w->worker_task_tail) {
52257836SJohn.Forte@Sun.COM 			w->worker_task_tail->itask_worker_next = itask;
52267836SJohn.Forte@Sun.COM 		} else {
52277836SJohn.Forte@Sun.COM 			w->worker_task_head = itask;
52287836SJohn.Forte@Sun.COM 		}
52297836SJohn.Forte@Sun.COM 		w->worker_task_tail = itask;
523011773STim.Szeto@Sun.COM 		/* Measure task waitq time */
523111773STim.Szeto@Sun.COM 		itask->itask_waitq_enter_timestamp = gethrtime();
52327836SJohn.Forte@Sun.COM 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
52337836SJohn.Forte@Sun.COM 			w->worker_max_qdepth_pu = w->worker_queue_depth;
52347836SJohn.Forte@Sun.COM 		}
52357836SJohn.Forte@Sun.COM 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
52367836SJohn.Forte@Sun.COM 			cv_signal(&w->worker_cv);
52377836SJohn.Forte@Sun.COM 	}
52387836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
52397836SJohn.Forte@Sun.COM 
52407836SJohn.Forte@Sun.COM 	if (free_it) {
52417836SJohn.Forte@Sun.COM 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
52427836SJohn.Forte@Sun.COM 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
52437836SJohn.Forte@Sun.COM 		    ITASK_BEING_ABORTED)) == 0) {
52447836SJohn.Forte@Sun.COM 			stmf_task_free(task);
52457836SJohn.Forte@Sun.COM 		} else {
52467836SJohn.Forte@Sun.COM 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
524710878SJohn.Forte@Sun.COM 			    " is not done, itask %p itask_flags %x",
524810878SJohn.Forte@Sun.COM 			    (void *)itask, itask->itask_flags);
52497836SJohn.Forte@Sun.COM 		}
52507836SJohn.Forte@Sun.COM 	}
52517836SJohn.Forte@Sun.COM }
52527836SJohn.Forte@Sun.COM 
52537836SJohn.Forte@Sun.COM void
stmf_task_lu_done(scsi_task_t * task)52547836SJohn.Forte@Sun.COM stmf_task_lu_done(scsi_task_t *task)
52557836SJohn.Forte@Sun.COM {
52567836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
52578662SJordan.Vaughan@Sun.com 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
52587836SJohn.Forte@Sun.COM 	stmf_worker_t *w = itask->itask_worker;
52597836SJohn.Forte@Sun.COM 	uint32_t new, old;
52607836SJohn.Forte@Sun.COM 
52617836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
52627836SJohn.Forte@Sun.COM 	do {
52637836SJohn.Forte@Sun.COM 		new = old = itask->itask_flags;
52647836SJohn.Forte@Sun.COM 		if (old & ITASK_BEING_ABORTED) {
52657836SJohn.Forte@Sun.COM 			mutex_exit(&w->worker_lock);
52667836SJohn.Forte@Sun.COM 			return;
52677836SJohn.Forte@Sun.COM 		}
52687836SJohn.Forte@Sun.COM 		if (old & ITASK_IN_WORKER_QUEUE) {
52697836SJohn.Forte@Sun.COM 			cmn_err(CE_PANIC, "task_lu_done received"
52707836SJohn.Forte@Sun.COM 			    " when task is in worker queue "
52717836SJohn.Forte@Sun.COM 			    " task = %p", (void *)task);
52727836SJohn.Forte@Sun.COM 		}
52737836SJohn.Forte@Sun.COM 		new &= ~ITASK_KNOWN_TO_LU;
52747836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
52757836SJohn.Forte@Sun.COM 
52767836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
52777836SJohn.Forte@Sun.COM 
52787836SJohn.Forte@Sun.COM 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
52797836SJohn.Forte@Sun.COM 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
52807836SJohn.Forte@Sun.COM 	    ITASK_BEING_ABORTED)) == 0) {
52817836SJohn.Forte@Sun.COM 		stmf_task_free(task);
52827836SJohn.Forte@Sun.COM 	} else {
52837836SJohn.Forte@Sun.COM 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
52847836SJohn.Forte@Sun.COM 		    " the task is still not done, task = %p", (void *)task);
52857836SJohn.Forte@Sun.COM 	}
52867836SJohn.Forte@Sun.COM }
52877836SJohn.Forte@Sun.COM 
52887836SJohn.Forte@Sun.COM void
stmf_queue_task_for_abort(scsi_task_t * task,stmf_status_t s)52897836SJohn.Forte@Sun.COM stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
52907836SJohn.Forte@Sun.COM {
52917836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
52927836SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
52937836SJohn.Forte@Sun.COM 	stmf_worker_t *w;
52947836SJohn.Forte@Sun.COM 	uint32_t old, new;
52957836SJohn.Forte@Sun.COM 
529612625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_TASK_ABORT, CMD_OR_IOF_NA, NULL);
529712625SJohn.Forte@Sun.COM 
52987836SJohn.Forte@Sun.COM 	do {
52997836SJohn.Forte@Sun.COM 		old = new = itask->itask_flags;
53007836SJohn.Forte@Sun.COM 		if ((old & ITASK_BEING_ABORTED) ||
53017836SJohn.Forte@Sun.COM 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
53027836SJohn.Forte@Sun.COM 		    ITASK_KNOWN_TO_LU)) == 0)) {
53037836SJohn.Forte@Sun.COM 			return;
53047836SJohn.Forte@Sun.COM 		}
53057836SJohn.Forte@Sun.COM 		new |= ITASK_BEING_ABORTED;
53067836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
53077836SJohn.Forte@Sun.COM 	task->task_completion_status = s;
53087836SJohn.Forte@Sun.COM 	itask->itask_start_time = ddi_get_lbolt();
53097836SJohn.Forte@Sun.COM 
53107836SJohn.Forte@Sun.COM 	if (((w = itask->itask_worker) == NULL) ||
53117836SJohn.Forte@Sun.COM 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
53127836SJohn.Forte@Sun.COM 		return;
53137836SJohn.Forte@Sun.COM 	}
53147836SJohn.Forte@Sun.COM 
53157836SJohn.Forte@Sun.COM 	/* Queue it and get out */
53167836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
53177836SJohn.Forte@Sun.COM 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
53187836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
53197836SJohn.Forte@Sun.COM 		return;
53207836SJohn.Forte@Sun.COM 	}
53217836SJohn.Forte@Sun.COM 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
53227836SJohn.Forte@Sun.COM 	itask->itask_worker_next = NULL;
53237836SJohn.Forte@Sun.COM 	if (w->worker_task_tail) {
53247836SJohn.Forte@Sun.COM 		w->worker_task_tail->itask_worker_next = itask;
53257836SJohn.Forte@Sun.COM 	} else {
53267836SJohn.Forte@Sun.COM 		w->worker_task_head = itask;
53277836SJohn.Forte@Sun.COM 	}
53287836SJohn.Forte@Sun.COM 	w->worker_task_tail = itask;
53297836SJohn.Forte@Sun.COM 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
53307836SJohn.Forte@Sun.COM 		w->worker_max_qdepth_pu = w->worker_queue_depth;
53317836SJohn.Forte@Sun.COM 	}
53327836SJohn.Forte@Sun.COM 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
53337836SJohn.Forte@Sun.COM 		cv_signal(&w->worker_cv);
53347836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
53357836SJohn.Forte@Sun.COM }
53367836SJohn.Forte@Sun.COM 
53377836SJohn.Forte@Sun.COM void
stmf_abort(int abort_cmd,scsi_task_t * task,stmf_status_t s,void * arg)53387836SJohn.Forte@Sun.COM stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
53397836SJohn.Forte@Sun.COM {
53407836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask = NULL;
53417836SJohn.Forte@Sun.COM 	uint32_t old, new, f, rf;
53427836SJohn.Forte@Sun.COM 
53438859STim.Szeto@Sun.COM 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
53448859STim.Szeto@Sun.COM 	    stmf_status_t, s);
53458859STim.Szeto@Sun.COM 
53467836SJohn.Forte@Sun.COM 	switch (abort_cmd) {
53477836SJohn.Forte@Sun.COM 	case STMF_QUEUE_ABORT_LU:
53487836SJohn.Forte@Sun.COM 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
53497836SJohn.Forte@Sun.COM 		return;
53507836SJohn.Forte@Sun.COM 	case STMF_QUEUE_TASK_ABORT:
53517836SJohn.Forte@Sun.COM 		stmf_queue_task_for_abort(task, s);
53527836SJohn.Forte@Sun.COM 		return;
53537836SJohn.Forte@Sun.COM 	case STMF_REQUEUE_TASK_ABORT_LPORT:
53547836SJohn.Forte@Sun.COM 		rf = ITASK_TGT_PORT_ABORT_CALLED;
53557836SJohn.Forte@Sun.COM 		f = ITASK_KNOWN_TO_TGT_PORT;
53567836SJohn.Forte@Sun.COM 		break;
53577836SJohn.Forte@Sun.COM 	case STMF_REQUEUE_TASK_ABORT_LU:
53587836SJohn.Forte@Sun.COM 		rf = ITASK_LU_ABORT_CALLED;
53597836SJohn.Forte@Sun.COM 		f = ITASK_KNOWN_TO_LU;
53607836SJohn.Forte@Sun.COM 		break;
53617836SJohn.Forte@Sun.COM 	default:
53627836SJohn.Forte@Sun.COM 		return;
53637836SJohn.Forte@Sun.COM 	}
53647836SJohn.Forte@Sun.COM 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
53657836SJohn.Forte@Sun.COM 	f |= ITASK_BEING_ABORTED | rf;
53667836SJohn.Forte@Sun.COM 	do {
53677836SJohn.Forte@Sun.COM 		old = new = itask->itask_flags;
53687836SJohn.Forte@Sun.COM 		if ((old & f) != f) {
53697836SJohn.Forte@Sun.COM 			return;
53707836SJohn.Forte@Sun.COM 		}
53717836SJohn.Forte@Sun.COM 		new &= ~rf;
53727836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
53737836SJohn.Forte@Sun.COM }
53747836SJohn.Forte@Sun.COM 
53757836SJohn.Forte@Sun.COM void
stmf_task_lu_aborted(scsi_task_t * task,stmf_status_t s,uint32_t iof)53767836SJohn.Forte@Sun.COM stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
53777836SJohn.Forte@Sun.COM {
53787836SJohn.Forte@Sun.COM 	char			 info[STMF_CHANGE_INFO_LEN];
53797836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
53807836SJohn.Forte@Sun.COM 	unsigned long long	st;
53817836SJohn.Forte@Sun.COM 
538212625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_TASK_LU_ABORTED, iof, NULL);
538312625SJohn.Forte@Sun.COM 
53847836SJohn.Forte@Sun.COM 	st = s;	/* gcc fix */
53857836SJohn.Forte@Sun.COM 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
538612571SViswanathan.Kannappan@Sun.COM 		(void) snprintf(info, sizeof (info),
53877836SJohn.Forte@Sun.COM 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
53887836SJohn.Forte@Sun.COM 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
538912571SViswanathan.Kannappan@Sun.COM 		(void) snprintf(info, sizeof (info),
53907836SJohn.Forte@Sun.COM 		    "Task aborted but LU is not finished, task ="
53917836SJohn.Forte@Sun.COM 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
53927836SJohn.Forte@Sun.COM 	} else {
53937836SJohn.Forte@Sun.COM 		/*
53947836SJohn.Forte@Sun.COM 		 * LU abort successfully
53957836SJohn.Forte@Sun.COM 		 */
53967836SJohn.Forte@Sun.COM 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
53977836SJohn.Forte@Sun.COM 		return;
53987836SJohn.Forte@Sun.COM 	}
53997836SJohn.Forte@Sun.COM 
54007836SJohn.Forte@Sun.COM 	stmf_abort_task_offline(task, 1, info);
54017836SJohn.Forte@Sun.COM }
54027836SJohn.Forte@Sun.COM 
54037836SJohn.Forte@Sun.COM void
stmf_task_lport_aborted(scsi_task_t * task,stmf_status_t s,uint32_t iof)54047836SJohn.Forte@Sun.COM stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
54057836SJohn.Forte@Sun.COM {
540610421STim.Szeto@Sun.COM 	char			info[STMF_CHANGE_INFO_LEN];
54077836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
54087836SJohn.Forte@Sun.COM 	unsigned long long	st;
540910421STim.Szeto@Sun.COM 	uint32_t		old, new;
54107836SJohn.Forte@Sun.COM 
541112625SJohn.Forte@Sun.COM 	stmf_task_audit(itask, TE_TASK_LPORT_ABORTED, iof, NULL);
541212625SJohn.Forte@Sun.COM 
54137836SJohn.Forte@Sun.COM 	st = s;
54147836SJohn.Forte@Sun.COM 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
541512571SViswanathan.Kannappan@Sun.COM 		(void) snprintf(info, sizeof (info),
54167836SJohn.Forte@Sun.COM 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
54177836SJohn.Forte@Sun.COM 		    st);
54187836SJohn.Forte@Sun.COM 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
541912571SViswanathan.Kannappan@Sun.COM 		(void) snprintf(info, sizeof (info),
54207836SJohn.Forte@Sun.COM 		    "Task aborted but tgt port is not finished, "
54217836SJohn.Forte@Sun.COM 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
54227836SJohn.Forte@Sun.COM 	} else {
54237836SJohn.Forte@Sun.COM 		/*
542410421STim.Szeto@Sun.COM 		 * LPORT abort successfully
54257836SJohn.Forte@Sun.COM 		 */
542610421STim.Szeto@Sun.COM 		do {
542710421STim.Szeto@Sun.COM 			old = new = itask->itask_flags;
542810421STim.Szeto@Sun.COM 			if (!(old & ITASK_KNOWN_TO_TGT_PORT))
542910421STim.Szeto@Sun.COM 				return;
543010421STim.Szeto@Sun.COM 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
543110421STim.Szeto@Sun.COM 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
54327836SJohn.Forte@Sun.COM 		return;
54337836SJohn.Forte@Sun.COM 	}
54347836SJohn.Forte@Sun.COM 
54357836SJohn.Forte@Sun.COM 	stmf_abort_task_offline(task, 0, info);
54367836SJohn.Forte@Sun.COM }
54377836SJohn.Forte@Sun.COM 
54387836SJohn.Forte@Sun.COM stmf_status_t
stmf_task_poll_lu(scsi_task_t * task,uint32_t timeout)54397836SJohn.Forte@Sun.COM stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
54407836SJohn.Forte@Sun.COM {
54417836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
54427836SJohn.Forte@Sun.COM 	    task->task_stmf_private;
54437836SJohn.Forte@Sun.COM 	stmf_worker_t *w = itask->itask_worker;
54447836SJohn.Forte@Sun.COM 	int i;
54457836SJohn.Forte@Sun.COM 
54467836SJohn.Forte@Sun.COM 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
54477836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
54487836SJohn.Forte@Sun.COM 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
54497836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
54507836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
54517836SJohn.Forte@Sun.COM 	}
54527836SJohn.Forte@Sun.COM 	for (i = 0; i < itask->itask_ncmds; i++) {
54537836SJohn.Forte@Sun.COM 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
54547836SJohn.Forte@Sun.COM 			mutex_exit(&w->worker_lock);
54557836SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
54567836SJohn.Forte@Sun.COM 		}
54577836SJohn.Forte@Sun.COM 	}
54587836SJohn.Forte@Sun.COM 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
54597836SJohn.Forte@Sun.COM 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
54607836SJohn.Forte@Sun.COM 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
54617836SJohn.Forte@Sun.COM 	} else {
54627836SJohn.Forte@Sun.COM 		clock_t t = drv_usectohz(timeout * 1000);
54637836SJohn.Forte@Sun.COM 		if (t == 0)
54647836SJohn.Forte@Sun.COM 			t = 1;
54657836SJohn.Forte@Sun.COM 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
54667836SJohn.Forte@Sun.COM 	}
54677836SJohn.Forte@Sun.COM 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
54687836SJohn.Forte@Sun.COM 		itask->itask_worker_next = NULL;
54697836SJohn.Forte@Sun.COM 		if (w->worker_task_tail) {
54707836SJohn.Forte@Sun.COM 			w->worker_task_tail->itask_worker_next = itask;
54717836SJohn.Forte@Sun.COM 		} else {
54727836SJohn.Forte@Sun.COM 			w->worker_task_head = itask;
54737836SJohn.Forte@Sun.COM 		}
54747836SJohn.Forte@Sun.COM 		w->worker_task_tail = itask;
54757836SJohn.Forte@Sun.COM 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
54767836SJohn.Forte@Sun.COM 			w->worker_max_qdepth_pu = w->worker_queue_depth;
54777836SJohn.Forte@Sun.COM 		}
54787836SJohn.Forte@Sun.COM 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
54797836SJohn.Forte@Sun.COM 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
54807836SJohn.Forte@Sun.COM 			cv_signal(&w->worker_cv);
54817836SJohn.Forte@Sun.COM 	}
54827836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
54837836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
54847836SJohn.Forte@Sun.COM }
54857836SJohn.Forte@Sun.COM 
54867836SJohn.Forte@Sun.COM stmf_status_t
stmf_task_poll_lport(scsi_task_t * task,uint32_t timeout)54877836SJohn.Forte@Sun.COM stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
54887836SJohn.Forte@Sun.COM {
54897836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
54907836SJohn.Forte@Sun.COM 	    task->task_stmf_private;
54917836SJohn.Forte@Sun.COM 	stmf_worker_t *w = itask->itask_worker;
54927836SJohn.Forte@Sun.COM 	int i;
54937836SJohn.Forte@Sun.COM 
54947836SJohn.Forte@Sun.COM 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
54957836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
54967836SJohn.Forte@Sun.COM 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
54977836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
54987836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
54997836SJohn.Forte@Sun.COM 	}
55007836SJohn.Forte@Sun.COM 	for (i = 0; i < itask->itask_ncmds; i++) {
55017836SJohn.Forte@Sun.COM 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
55027836SJohn.Forte@Sun.COM 			mutex_exit(&w->worker_lock);
55037836SJohn.Forte@Sun.COM 			return (STMF_SUCCESS);
55047836SJohn.Forte@Sun.COM 		}
55057836SJohn.Forte@Sun.COM 	}
55067836SJohn.Forte@Sun.COM 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
55077836SJohn.Forte@Sun.COM 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
55087836SJohn.Forte@Sun.COM 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
55097836SJohn.Forte@Sun.COM 	} else {
55107836SJohn.Forte@Sun.COM 		clock_t t = drv_usectohz(timeout * 1000);
55117836SJohn.Forte@Sun.COM 		if (t == 0)
55127836SJohn.Forte@Sun.COM 			t = 1;
55137836SJohn.Forte@Sun.COM 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
55147836SJohn.Forte@Sun.COM 	}
55157836SJohn.Forte@Sun.COM 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
55167836SJohn.Forte@Sun.COM 		itask->itask_worker_next = NULL;
55177836SJohn.Forte@Sun.COM 		if (w->worker_task_tail) {
55187836SJohn.Forte@Sun.COM 			w->worker_task_tail->itask_worker_next = itask;
55197836SJohn.Forte@Sun.COM 		} else {
55207836SJohn.Forte@Sun.COM 			w->worker_task_head = itask;
55217836SJohn.Forte@Sun.COM 		}
55227836SJohn.Forte@Sun.COM 		w->worker_task_tail = itask;
55237836SJohn.Forte@Sun.COM 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
55247836SJohn.Forte@Sun.COM 			w->worker_max_qdepth_pu = w->worker_queue_depth;
55257836SJohn.Forte@Sun.COM 		}
55267836SJohn.Forte@Sun.COM 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
55277836SJohn.Forte@Sun.COM 			cv_signal(&w->worker_cv);
55287836SJohn.Forte@Sun.COM 	}
55297836SJohn.Forte@Sun.COM 	mutex_exit(&w->worker_lock);
55307836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
55317836SJohn.Forte@Sun.COM }
55327836SJohn.Forte@Sun.COM 
55337836SJohn.Forte@Sun.COM void
stmf_do_task_abort(scsi_task_t * task)55347836SJohn.Forte@Sun.COM stmf_do_task_abort(scsi_task_t *task)
55357836SJohn.Forte@Sun.COM {
55367836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
55377836SJohn.Forte@Sun.COM 	stmf_lu_t		*lu;
55387836SJohn.Forte@Sun.COM 	stmf_local_port_t	*lport;
55397836SJohn.Forte@Sun.COM 	unsigned long long	 ret;
55407836SJohn.Forte@Sun.COM 	uint32_t		 old, new;
55417836SJohn.Forte@Sun.COM 	uint8_t			 call_lu_abort, call_port_abort;
55427836SJohn.Forte@Sun.COM 	char			 info[STMF_CHANGE_INFO_LEN];
55437836SJohn.Forte@Sun.COM 
55447836SJohn.Forte@Sun.COM 	lu = task->task_lu;
55457836SJohn.Forte@Sun.COM 	lport = task->task_lport;
55467836SJohn.Forte@Sun.COM 	do {
55477836SJohn.Forte@Sun.COM 		old = new = itask->itask_flags;
55487836SJohn.Forte@Sun.COM 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
55497836SJohn.Forte@Sun.COM 		    ITASK_KNOWN_TO_LU) {
55507836SJohn.Forte@Sun.COM 			new |= ITASK_LU_ABORT_CALLED;
55517836SJohn.Forte@Sun.COM 			call_lu_abort = 1;
55527836SJohn.Forte@Sun.COM 		} else {
55537836SJohn.Forte@Sun.COM 			call_lu_abort = 0;
55547836SJohn.Forte@Sun.COM 		}
55557836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
55567836SJohn.Forte@Sun.COM 
55577836SJohn.Forte@Sun.COM 	if (call_lu_abort) {
55587836SJohn.Forte@Sun.COM 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
55597836SJohn.Forte@Sun.COM 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
55607836SJohn.Forte@Sun.COM 		} else {
55617836SJohn.Forte@Sun.COM 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
55627836SJohn.Forte@Sun.COM 		}
55637836SJohn.Forte@Sun.COM 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
55647836SJohn.Forte@Sun.COM 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
55657836SJohn.Forte@Sun.COM 		} else if (ret == STMF_BUSY) {
55667836SJohn.Forte@Sun.COM 			atomic_and_32(&itask->itask_flags,
55677836SJohn.Forte@Sun.COM 			    ~ITASK_LU_ABORT_CALLED);
55687836SJohn.Forte@Sun.COM 		} else if (ret != STMF_SUCCESS) {
556912571SViswanathan.Kannappan@Sun.COM 			(void) snprintf(info, sizeof (info),
55707836SJohn.Forte@Sun.COM 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
55717836SJohn.Forte@Sun.COM 			stmf_abort_task_offline(task, 1, info);
55727836SJohn.Forte@Sun.COM 		}
55737836SJohn.Forte@Sun.COM 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
55747836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() > (itask->itask_start_time +
55757836SJohn.Forte@Sun.COM 		    STMF_SEC2TICK(lu->lu_abort_timeout?
55767836SJohn.Forte@Sun.COM 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
557712571SViswanathan.Kannappan@Sun.COM 			(void) snprintf(info, sizeof (info),
55787836SJohn.Forte@Sun.COM 			    "lu abort timed out");
55797836SJohn.Forte@Sun.COM 			stmf_abort_task_offline(itask->itask_task, 1, info);
55807836SJohn.Forte@Sun.COM 		}
55817836SJohn.Forte@Sun.COM 	}
55827836SJohn.Forte@Sun.COM 
55837836SJohn.Forte@Sun.COM 	do {
55847836SJohn.Forte@Sun.COM 		old = new = itask->itask_flags;
55857836SJohn.Forte@Sun.COM 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
55867836SJohn.Forte@Sun.COM 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
55877836SJohn.Forte@Sun.COM 			new |= ITASK_TGT_PORT_ABORT_CALLED;
55887836SJohn.Forte@Sun.COM 			call_port_abort = 1;
55897836SJohn.Forte@Sun.COM 		} else {
55907836SJohn.Forte@Sun.COM 			call_port_abort = 0;
55917836SJohn.Forte@Sun.COM 		}
55927836SJohn.Forte@Sun.COM 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
55937836SJohn.Forte@Sun.COM 	if (call_port_abort) {
55948662SJordan.Vaughan@Sun.com 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
55957836SJohn.Forte@Sun.COM 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
55967836SJohn.Forte@Sun.COM 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
55977836SJohn.Forte@Sun.COM 		} else if (ret == STMF_BUSY) {
55987836SJohn.Forte@Sun.COM 			atomic_and_32(&itask->itask_flags,
55997836SJohn.Forte@Sun.COM 			    ~ITASK_TGT_PORT_ABORT_CALLED);
56007836SJohn.Forte@Sun.COM 		} else if (ret != STMF_SUCCESS) {
560112571SViswanathan.Kannappan@Sun.COM 			(void) snprintf(info, sizeof (info),
56027836SJohn.Forte@Sun.COM 			    "Abort failed by tgt port %p ret %llx",
56037836SJohn.Forte@Sun.COM 			    (void *)lport, ret);
56047836SJohn.Forte@Sun.COM 			stmf_abort_task_offline(task, 0, info);
56057836SJohn.Forte@Sun.COM 		}
56067836SJohn.Forte@Sun.COM 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
56077836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() > (itask->itask_start_time +
56087836SJohn.Forte@Sun.COM 		    STMF_SEC2TICK(lport->lport_abort_timeout?
56097836SJohn.Forte@Sun.COM 		    lport->lport_abort_timeout :
56107836SJohn.Forte@Sun.COM 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
561112571SViswanathan.Kannappan@Sun.COM 			(void) snprintf(info, sizeof (info),
56127836SJohn.Forte@Sun.COM 			    "lport abort timed out");
56137836SJohn.Forte@Sun.COM 			stmf_abort_task_offline(itask->itask_task, 0, info);
56147836SJohn.Forte@Sun.COM 		}
56157836SJohn.Forte@Sun.COM 	}
56167836SJohn.Forte@Sun.COM }
56177836SJohn.Forte@Sun.COM 
56187836SJohn.Forte@Sun.COM stmf_status_t
stmf_ctl(int cmd,void * obj,void * arg)56197836SJohn.Forte@Sun.COM stmf_ctl(int cmd, void *obj, void *arg)
56207836SJohn.Forte@Sun.COM {
56217836SJohn.Forte@Sun.COM 	stmf_status_t			ret;
56227836SJohn.Forte@Sun.COM 	stmf_i_lu_t			*ilu;
56237836SJohn.Forte@Sun.COM 	stmf_i_local_port_t		*ilport;
56247836SJohn.Forte@Sun.COM 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
56257836SJohn.Forte@Sun.COM 
56267836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
56277836SJohn.Forte@Sun.COM 	ret = STMF_INVALID_ARG;
56287836SJohn.Forte@Sun.COM 	if (cmd & STMF_CMD_LU_OP) {
56297836SJohn.Forte@Sun.COM 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
56307836SJohn.Forte@Sun.COM 		if (ilu == NULL) {
56317836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
56327836SJohn.Forte@Sun.COM 		}
56338859STim.Szeto@Sun.COM 		DTRACE_PROBE3(lu__state__change,
56348859STim.Szeto@Sun.COM 		    stmf_lu_t *, ilu->ilu_lu,
56358859STim.Szeto@Sun.COM 		    int, cmd, stmf_state_change_info_t *, ssci);
56367836SJohn.Forte@Sun.COM 	} else if (cmd & STMF_CMD_LPORT_OP) {
56377836SJohn.Forte@Sun.COM 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
56387836SJohn.Forte@Sun.COM 		if (ilport == NULL) {
56397836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
56407836SJohn.Forte@Sun.COM 		}
56418859STim.Szeto@Sun.COM 		DTRACE_PROBE3(lport__state__change,
56428859STim.Szeto@Sun.COM 		    stmf_local_port_t *, ilport->ilport_lport,
56438859STim.Szeto@Sun.COM 		    int, cmd, stmf_state_change_info_t *, ssci);
56447836SJohn.Forte@Sun.COM 	} else {
56457836SJohn.Forte@Sun.COM 		goto stmf_ctl_lock_exit;
56467836SJohn.Forte@Sun.COM 	}
56477836SJohn.Forte@Sun.COM 
56487836SJohn.Forte@Sun.COM 	switch (cmd) {
56497836SJohn.Forte@Sun.COM 	case STMF_CMD_LU_ONLINE:
565011698SNattuvetty.Bhavyan@Sun.COM 		switch (ilu->ilu_state) {
565111698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINE:
565211698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_SUCCESS;
565311698SNattuvetty.Bhavyan@Sun.COM 				break;
565411698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINE:
565511698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINING:
565611698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_ALREADY;
565711698SNattuvetty.Bhavyan@Sun.COM 				break;
565811698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINING:
565911698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BUSY;
566011698SNattuvetty.Bhavyan@Sun.COM 				break;
566111698SNattuvetty.Bhavyan@Sun.COM 			default:
566211698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BADSTATE;
566311698SNattuvetty.Bhavyan@Sun.COM 				break;
566411698SNattuvetty.Bhavyan@Sun.COM 		}
566511698SNattuvetty.Bhavyan@Sun.COM 		if (ret != STMF_SUCCESS)
56667836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
566711698SNattuvetty.Bhavyan@Sun.COM 
56687836SJohn.Forte@Sun.COM 		ilu->ilu_state = STMF_STATE_ONLINING;
56697836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
56707836SJohn.Forte@Sun.COM 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
56717836SJohn.Forte@Sun.COM 		break;
56727836SJohn.Forte@Sun.COM 
56737836SJohn.Forte@Sun.COM 	case STMF_CMD_LU_ONLINE_COMPLETE:
56747836SJohn.Forte@Sun.COM 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
567511698SNattuvetty.Bhavyan@Sun.COM 			ret = STMF_BADSTATE;
56767836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
56777836SJohn.Forte@Sun.COM 		}
56787836SJohn.Forte@Sun.COM 		if (((stmf_change_status_t *)arg)->st_completion_status ==
56797836SJohn.Forte@Sun.COM 		    STMF_SUCCESS) {
56807836SJohn.Forte@Sun.COM 			ilu->ilu_state = STMF_STATE_ONLINE;
56817836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
56827836SJohn.Forte@Sun.COM 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
56837836SJohn.Forte@Sun.COM 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
56847836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
56857836SJohn.Forte@Sun.COM 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
56867836SJohn.Forte@Sun.COM 		} else {
56877836SJohn.Forte@Sun.COM 			/* XXX: should throw a meesage an record more data */
56887836SJohn.Forte@Sun.COM 			ilu->ilu_state = STMF_STATE_OFFLINE;
56897836SJohn.Forte@Sun.COM 		}
56907836SJohn.Forte@Sun.COM 		ret = STMF_SUCCESS;
56917836SJohn.Forte@Sun.COM 		goto stmf_ctl_lock_exit;
56927836SJohn.Forte@Sun.COM 
56937836SJohn.Forte@Sun.COM 	case STMF_CMD_LU_OFFLINE:
569411698SNattuvetty.Bhavyan@Sun.COM 		switch (ilu->ilu_state) {
569511698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINE:
569611698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_SUCCESS;
569711698SNattuvetty.Bhavyan@Sun.COM 				break;
569811698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINE:
569911698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINING:
570011698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_ALREADY;
570111698SNattuvetty.Bhavyan@Sun.COM 				break;
570211698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINING:
570311698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BUSY;
570411698SNattuvetty.Bhavyan@Sun.COM 				break;
570511698SNattuvetty.Bhavyan@Sun.COM 			default:
570611698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BADSTATE;
570711698SNattuvetty.Bhavyan@Sun.COM 				break;
570811698SNattuvetty.Bhavyan@Sun.COM 		}
570911698SNattuvetty.Bhavyan@Sun.COM 		if (ret != STMF_SUCCESS)
57107836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
57117836SJohn.Forte@Sun.COM 		ilu->ilu_state = STMF_STATE_OFFLINING;
57127836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
57137836SJohn.Forte@Sun.COM 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
57147836SJohn.Forte@Sun.COM 		break;
57157836SJohn.Forte@Sun.COM 
57167836SJohn.Forte@Sun.COM 	case STMF_CMD_LU_OFFLINE_COMPLETE:
57177836SJohn.Forte@Sun.COM 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
571811698SNattuvetty.Bhavyan@Sun.COM 			ret = STMF_BADSTATE;
57197836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
57207836SJohn.Forte@Sun.COM 		}
57217836SJohn.Forte@Sun.COM 		if (((stmf_change_status_t *)arg)->st_completion_status ==
57227836SJohn.Forte@Sun.COM 		    STMF_SUCCESS) {
57237836SJohn.Forte@Sun.COM 			ilu->ilu_state = STMF_STATE_OFFLINE;
57247836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
57257836SJohn.Forte@Sun.COM 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
57267836SJohn.Forte@Sun.COM 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
57277836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
57287836SJohn.Forte@Sun.COM 		} else {
57297836SJohn.Forte@Sun.COM 			ilu->ilu_state = STMF_STATE_ONLINE;
57307836SJohn.Forte@Sun.COM 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
57317836SJohn.Forte@Sun.COM 		}
57327836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
57337836SJohn.Forte@Sun.COM 		break;
57347836SJohn.Forte@Sun.COM 
57357836SJohn.Forte@Sun.COM 	/*
57367836SJohn.Forte@Sun.COM 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
57377836SJohn.Forte@Sun.COM 	 * It's related with hardware disable/enable.
57387836SJohn.Forte@Sun.COM 	 */
57397836SJohn.Forte@Sun.COM 	case STMF_CMD_LPORT_ONLINE:
574011698SNattuvetty.Bhavyan@Sun.COM 		switch (ilport->ilport_state) {
574111698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINE:
574211698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_SUCCESS;
574311698SNattuvetty.Bhavyan@Sun.COM 				break;
574411698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINE:
574511698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINING:
574611698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_ALREADY;
574711698SNattuvetty.Bhavyan@Sun.COM 				break;
574811698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINING:
574911698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BUSY;
575011698SNattuvetty.Bhavyan@Sun.COM 				break;
575111698SNattuvetty.Bhavyan@Sun.COM 			default:
575211698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BADSTATE;
575311698SNattuvetty.Bhavyan@Sun.COM 				break;
575411698SNattuvetty.Bhavyan@Sun.COM 		}
575511698SNattuvetty.Bhavyan@Sun.COM 		if (ret != STMF_SUCCESS)
57567836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
57577836SJohn.Forte@Sun.COM 
57587836SJohn.Forte@Sun.COM 		/*
57597836SJohn.Forte@Sun.COM 		 * Only user request can recover the port from the
57607836SJohn.Forte@Sun.COM 		 * FORCED_OFFLINE state
57617836SJohn.Forte@Sun.COM 		 */
57627836SJohn.Forte@Sun.COM 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
57637836SJohn.Forte@Sun.COM 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
57647836SJohn.Forte@Sun.COM 				ret = STMF_FAILURE;
57657836SJohn.Forte@Sun.COM 				goto stmf_ctl_lock_exit;
57667836SJohn.Forte@Sun.COM 			}
57677836SJohn.Forte@Sun.COM 		}
57687836SJohn.Forte@Sun.COM 
57697836SJohn.Forte@Sun.COM 		/*
57707836SJohn.Forte@Sun.COM 		 * Avoid too frequent request to online
57717836SJohn.Forte@Sun.COM 		 */
57727836SJohn.Forte@Sun.COM 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
57737836SJohn.Forte@Sun.COM 			ilport->ilport_online_times = 0;
57747836SJohn.Forte@Sun.COM 			ilport->ilport_avg_interval = 0;
57757836SJohn.Forte@Sun.COM 		}
57767836SJohn.Forte@Sun.COM 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
57777836SJohn.Forte@Sun.COM 		    (ilport->ilport_online_times >= 4)) {
57787836SJohn.Forte@Sun.COM 			ret = STMF_FAILURE;
57797836SJohn.Forte@Sun.COM 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
57807836SJohn.Forte@Sun.COM 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
57817836SJohn.Forte@Sun.COM 			    "online the port");
57827836SJohn.Forte@Sun.COM 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
57837836SJohn.Forte@Sun.COM 			    "online the port, set FORCED_OFFLINE now");
57847836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
57857836SJohn.Forte@Sun.COM 		}
57867836SJohn.Forte@Sun.COM 		if (ilport->ilport_online_times > 0) {
57877836SJohn.Forte@Sun.COM 			if (ilport->ilport_online_times == 1) {
57887836SJohn.Forte@Sun.COM 				ilport->ilport_avg_interval = ddi_get_lbolt() -
57897836SJohn.Forte@Sun.COM 				    ilport->ilport_last_online_clock;
57907836SJohn.Forte@Sun.COM 			} else {
57917836SJohn.Forte@Sun.COM 				ilport->ilport_avg_interval =
57927836SJohn.Forte@Sun.COM 				    (ilport->ilport_avg_interval +
57937836SJohn.Forte@Sun.COM 				    ddi_get_lbolt() -
57947836SJohn.Forte@Sun.COM 				    ilport->ilport_last_online_clock) >> 1;
57957836SJohn.Forte@Sun.COM 			}
57967836SJohn.Forte@Sun.COM 		}
57977836SJohn.Forte@Sun.COM 		ilport->ilport_last_online_clock = ddi_get_lbolt();
57987836SJohn.Forte@Sun.COM 		ilport->ilport_online_times++;
57997836SJohn.Forte@Sun.COM 
58007836SJohn.Forte@Sun.COM 		/*
58017836SJohn.Forte@Sun.COM 		 * Submit online service request
58027836SJohn.Forte@Sun.COM 		 */
58037836SJohn.Forte@Sun.COM 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
58047836SJohn.Forte@Sun.COM 		ilport->ilport_state = STMF_STATE_ONLINING;
58057836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
58067836SJohn.Forte@Sun.COM 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
58077836SJohn.Forte@Sun.COM 		break;
58087836SJohn.Forte@Sun.COM 
58097836SJohn.Forte@Sun.COM 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
58107836SJohn.Forte@Sun.COM 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
581111698SNattuvetty.Bhavyan@Sun.COM 			ret = STMF_BADSTATE;
58127836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
58137836SJohn.Forte@Sun.COM 		}
58147836SJohn.Forte@Sun.COM 		if (((stmf_change_status_t *)arg)->st_completion_status ==
58157836SJohn.Forte@Sun.COM 		    STMF_SUCCESS) {
58167836SJohn.Forte@Sun.COM 			ilport->ilport_state = STMF_STATE_ONLINE;
58177836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
58187836SJohn.Forte@Sun.COM 			((stmf_local_port_t *)obj)->lport_ctl(
58197836SJohn.Forte@Sun.COM 			    (stmf_local_port_t *)obj,
58207836SJohn.Forte@Sun.COM 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
58217836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
58227836SJohn.Forte@Sun.COM 		} else {
58237836SJohn.Forte@Sun.COM 			ilport->ilport_state = STMF_STATE_OFFLINE;
58247836SJohn.Forte@Sun.COM 		}
58257836SJohn.Forte@Sun.COM 		ret = STMF_SUCCESS;
58267836SJohn.Forte@Sun.COM 		goto stmf_ctl_lock_exit;
58277836SJohn.Forte@Sun.COM 
58287836SJohn.Forte@Sun.COM 	case STMF_CMD_LPORT_OFFLINE:
582911698SNattuvetty.Bhavyan@Sun.COM 		switch (ilport->ilport_state) {
583011698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINE:
583111698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_SUCCESS;
583211698SNattuvetty.Bhavyan@Sun.COM 				break;
583311698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINE:
583411698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_OFFLINING:
583511698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_ALREADY;
583611698SNattuvetty.Bhavyan@Sun.COM 				break;
583711698SNattuvetty.Bhavyan@Sun.COM 			case STMF_STATE_ONLINING:
583811698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BUSY;
583911698SNattuvetty.Bhavyan@Sun.COM 				break;
584011698SNattuvetty.Bhavyan@Sun.COM 			default:
584111698SNattuvetty.Bhavyan@Sun.COM 				ret = STMF_BADSTATE;
584211698SNattuvetty.Bhavyan@Sun.COM 				break;
584311698SNattuvetty.Bhavyan@Sun.COM 		}
584411698SNattuvetty.Bhavyan@Sun.COM 		if (ret != STMF_SUCCESS)
58457836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
584611698SNattuvetty.Bhavyan@Sun.COM 
58477836SJohn.Forte@Sun.COM 		ilport->ilport_state = STMF_STATE_OFFLINING;
58487836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
58497836SJohn.Forte@Sun.COM 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
58507836SJohn.Forte@Sun.COM 		break;
58517836SJohn.Forte@Sun.COM 
58527836SJohn.Forte@Sun.COM 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
58537836SJohn.Forte@Sun.COM 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
585411698SNattuvetty.Bhavyan@Sun.COM 			ret = STMF_BADSTATE;
58557836SJohn.Forte@Sun.COM 			goto stmf_ctl_lock_exit;
58567836SJohn.Forte@Sun.COM 		}
58577836SJohn.Forte@Sun.COM 		if (((stmf_change_status_t *)arg)->st_completion_status ==
58587836SJohn.Forte@Sun.COM 		    STMF_SUCCESS) {
58597836SJohn.Forte@Sun.COM 			ilport->ilport_state = STMF_STATE_OFFLINE;
58607836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
58617836SJohn.Forte@Sun.COM 			((stmf_local_port_t *)obj)->lport_ctl(
58627836SJohn.Forte@Sun.COM 			    (stmf_local_port_t *)obj,
58637836SJohn.Forte@Sun.COM 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
58647836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
58657836SJohn.Forte@Sun.COM 		} else {
58667836SJohn.Forte@Sun.COM 			ilport->ilport_state = STMF_STATE_ONLINE;
58677836SJohn.Forte@Sun.COM 		}
58687836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
58697836SJohn.Forte@Sun.COM 		break;
58707836SJohn.Forte@Sun.COM 
58717836SJohn.Forte@Sun.COM 	default:
58727836SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
58737836SJohn.Forte@Sun.COM 		ret = STMF_INVALID_ARG;
58747836SJohn.Forte@Sun.COM 		goto stmf_ctl_lock_exit;
58757836SJohn.Forte@Sun.COM 	}
58767836SJohn.Forte@Sun.COM 
58777836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
58787836SJohn.Forte@Sun.COM 
58797836SJohn.Forte@Sun.COM stmf_ctl_lock_exit:;
58807836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
58817836SJohn.Forte@Sun.COM 	return (ret);
58827836SJohn.Forte@Sun.COM }
58837836SJohn.Forte@Sun.COM 
58847836SJohn.Forte@Sun.COM /* ARGSUSED */
58857836SJohn.Forte@Sun.COM stmf_status_t
stmf_info_impl(uint32_t cmd,void * arg1,void * arg2,uint8_t * buf,uint32_t * bufsizep)58867836SJohn.Forte@Sun.COM stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
58877836SJohn.Forte@Sun.COM 						uint32_t *bufsizep)
58887836SJohn.Forte@Sun.COM {
58897836SJohn.Forte@Sun.COM 	return (STMF_NOT_SUPPORTED);
58907836SJohn.Forte@Sun.COM }
58917836SJohn.Forte@Sun.COM 
58927836SJohn.Forte@Sun.COM /* ARGSUSED */
58937836SJohn.Forte@Sun.COM stmf_status_t
stmf_info(uint32_t cmd,void * arg1,void * arg2,uint8_t * buf,uint32_t * bufsizep)58947836SJohn.Forte@Sun.COM stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
58957836SJohn.Forte@Sun.COM 						uint32_t *bufsizep)
58967836SJohn.Forte@Sun.COM {
58977836SJohn.Forte@Sun.COM 	uint32_t cl = SI_GET_CLASS(cmd);
58987836SJohn.Forte@Sun.COM 
58997836SJohn.Forte@Sun.COM 	if (cl == SI_STMF) {
59007836SJohn.Forte@Sun.COM 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
59017836SJohn.Forte@Sun.COM 	}
59027836SJohn.Forte@Sun.COM 	if (cl == SI_LPORT) {
59038662SJordan.Vaughan@Sun.com 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
59048662SJordan.Vaughan@Sun.com 		    arg2, buf, bufsizep));
59057836SJohn.Forte@Sun.COM 	} else if (cl == SI_LU) {
59068662SJordan.Vaughan@Sun.com 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
59078662SJordan.Vaughan@Sun.com 		    bufsizep));
59087836SJohn.Forte@Sun.COM 	}
59097836SJohn.Forte@Sun.COM 
59107836SJohn.Forte@Sun.COM 	return (STMF_NOT_SUPPORTED);
59117836SJohn.Forte@Sun.COM }
59127836SJohn.Forte@Sun.COM 
59137836SJohn.Forte@Sun.COM /*
59149857SPeter.Cudhea@Sun.COM  * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
59157836SJohn.Forte@Sun.COM  * stmf to register local ports. The ident should have 20 bytes in buffer
59167836SJohn.Forte@Sun.COM  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
59177836SJohn.Forte@Sun.COM  */
59187836SJohn.Forte@Sun.COM void
stmf_wwn_to_devid_desc(scsi_devid_desc_t * sdid,uint8_t * wwn,uint8_t protocol_id)59197836SJohn.Forte@Sun.COM stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
59207836SJohn.Forte@Sun.COM     uint8_t protocol_id)
59217836SJohn.Forte@Sun.COM {
59229857SPeter.Cudhea@Sun.COM 	char wwn_str[20+1];
59239857SPeter.Cudhea@Sun.COM 
59247836SJohn.Forte@Sun.COM 	sdid->protocol_id = protocol_id;
59257836SJohn.Forte@Sun.COM 	sdid->piv = 1;
59267836SJohn.Forte@Sun.COM 	sdid->code_set = CODE_SET_ASCII;
59277836SJohn.Forte@Sun.COM 	sdid->association = ID_IS_TARGET_PORT;
59287836SJohn.Forte@Sun.COM 	sdid->ident_length = 20;
59299857SPeter.Cudhea@Sun.COM 	/* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
59309857SPeter.Cudhea@Sun.COM 	(void) snprintf(wwn_str, sizeof (wwn_str),
59317836SJohn.Forte@Sun.COM 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
59327836SJohn.Forte@Sun.COM 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
59339857SPeter.Cudhea@Sun.COM 	bcopy(wwn_str, (char *)sdid->ident, 20);
59347836SJohn.Forte@Sun.COM }
59357836SJohn.Forte@Sun.COM 
59369857SPeter.Cudhea@Sun.COM 
59377836SJohn.Forte@Sun.COM stmf_xfer_data_t *
stmf_prepare_tpgs_data(uint8_t ilu_alua)593810725SJohn.Forte@Sun.COM stmf_prepare_tpgs_data(uint8_t ilu_alua)
59397836SJohn.Forte@Sun.COM {
59407836SJohn.Forte@Sun.COM 	stmf_xfer_data_t *xd;
59417836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport;
59427836SJohn.Forte@Sun.COM 	uint8_t *p;
594310725SJohn.Forte@Sun.COM 	uint32_t sz, asz, nports = 0, nports_standby = 0;
59447836SJohn.Forte@Sun.COM 
59457836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
594610725SJohn.Forte@Sun.COM 	/* check if any ports are standby and create second group */
594710725SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport;
594810725SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
594910725SJohn.Forte@Sun.COM 		if (ilport->ilport_standby == 1) {
595010725SJohn.Forte@Sun.COM 			nports_standby++;
595110725SJohn.Forte@Sun.COM 		} else {
595210725SJohn.Forte@Sun.COM 			nports++;
595310725SJohn.Forte@Sun.COM 		}
595410725SJohn.Forte@Sun.COM 	}
595510725SJohn.Forte@Sun.COM 
595610725SJohn.Forte@Sun.COM 	/* The spec only allows for 255 ports to be reported per group */
595710725SJohn.Forte@Sun.COM 	nports = min(nports, 255);
595810725SJohn.Forte@Sun.COM 	nports_standby = min(nports_standby, 255);
59597836SJohn.Forte@Sun.COM 	sz = (nports * 4) + 12;
596010725SJohn.Forte@Sun.COM 	if (nports_standby && ilu_alua) {
596110725SJohn.Forte@Sun.COM 		sz += (nports_standby * 4) + 8;
596210725SJohn.Forte@Sun.COM 	}
59637836SJohn.Forte@Sun.COM 	asz = sz + sizeof (*xd) - 4;
59647836SJohn.Forte@Sun.COM 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
59657836SJohn.Forte@Sun.COM 	if (xd == NULL) {
59667836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
59677836SJohn.Forte@Sun.COM 		return (NULL);
59687836SJohn.Forte@Sun.COM 	}
59697836SJohn.Forte@Sun.COM 	xd->alloc_size = asz;
59707836SJohn.Forte@Sun.COM 	xd->size_left = sz;
59717836SJohn.Forte@Sun.COM 
59727836SJohn.Forte@Sun.COM 	p = xd->buf;
59737836SJohn.Forte@Sun.COM 
59747836SJohn.Forte@Sun.COM 	*((uint32_t *)p) = BE_32(sz - 4);
59757836SJohn.Forte@Sun.COM 	p += 4;
59767836SJohn.Forte@Sun.COM 	p[0] = 0x80;	/* PREF */
597710725SJohn.Forte@Sun.COM 	p[1] = 5;	/* AO_SUP, S_SUP */
597810725SJohn.Forte@Sun.COM 	if (stmf_state.stmf_alua_node == 1) {
597910725SJohn.Forte@Sun.COM 		p[3] = 1;	/* Group 1 */
598010725SJohn.Forte@Sun.COM 	} else {
598110725SJohn.Forte@Sun.COM 		p[3] = 0;	/* Group 0 */
598210725SJohn.Forte@Sun.COM 	}
59837836SJohn.Forte@Sun.COM 	p[7] = nports & 0xff;
59847836SJohn.Forte@Sun.COM 	p += 8;
598510725SJohn.Forte@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport;
598610725SJohn.Forte@Sun.COM 	    ilport = ilport->ilport_next) {
598710725SJohn.Forte@Sun.COM 		if (ilport->ilport_standby == 1) {
598810725SJohn.Forte@Sun.COM 			continue;
598910725SJohn.Forte@Sun.COM 		}
59907836SJohn.Forte@Sun.COM 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
599110725SJohn.Forte@Sun.COM 		p += 4;
599210725SJohn.Forte@Sun.COM 	}
599310725SJohn.Forte@Sun.COM 	if (nports_standby && ilu_alua) {
599410725SJohn.Forte@Sun.COM 		p[0] = 0x02;	/* Non PREF, Standby */
599510725SJohn.Forte@Sun.COM 		p[1] = 5;	/* AO_SUP, S_SUP */
599610725SJohn.Forte@Sun.COM 		if (stmf_state.stmf_alua_node == 1) {
599710725SJohn.Forte@Sun.COM 			p[3] = 0;	/* Group 0 */
599810725SJohn.Forte@Sun.COM 		} else {
599910725SJohn.Forte@Sun.COM 			p[3] = 1;	/* Group 1 */
600010725SJohn.Forte@Sun.COM 		}
600110725SJohn.Forte@Sun.COM 		p[7] = nports_standby & 0xff;
600210725SJohn.Forte@Sun.COM 		p += 8;
600310725SJohn.Forte@Sun.COM 		for (ilport = stmf_state.stmf_ilportlist; ilport;
600410725SJohn.Forte@Sun.COM 		    ilport = ilport->ilport_next) {
600510725SJohn.Forte@Sun.COM 			if (ilport->ilport_standby == 0) {
600610725SJohn.Forte@Sun.COM 				continue;
600710725SJohn.Forte@Sun.COM 			}
600810725SJohn.Forte@Sun.COM 			((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
600910725SJohn.Forte@Sun.COM 			p += 4;
601010725SJohn.Forte@Sun.COM 		}
601110725SJohn.Forte@Sun.COM 	}
601210725SJohn.Forte@Sun.COM 
60137836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
60147836SJohn.Forte@Sun.COM 
60157836SJohn.Forte@Sun.COM 	return (xd);
60167836SJohn.Forte@Sun.COM }
60177836SJohn.Forte@Sun.COM 
60189585STim.Szeto@Sun.COM struct scsi_devid_desc *
stmf_scsilib_get_devid_desc(uint16_t rtpid)60199585STim.Szeto@Sun.COM stmf_scsilib_get_devid_desc(uint16_t rtpid)
60209585STim.Szeto@Sun.COM {
60219585STim.Szeto@Sun.COM 	scsi_devid_desc_t *devid = NULL;
60229585STim.Szeto@Sun.COM 	stmf_i_local_port_t *ilport;
60239585STim.Szeto@Sun.COM 
60249585STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
60259585STim.Szeto@Sun.COM 
60269585STim.Szeto@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport;
60279585STim.Szeto@Sun.COM 	    ilport = ilport->ilport_next) {
60289585STim.Szeto@Sun.COM 		if (ilport->ilport_rtpid == rtpid) {
60299585STim.Szeto@Sun.COM 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
6030*12750SNattuvetty.Bhavyan@Sun.COM 			uint32_t id_sz = sizeof (scsi_devid_desc_t) +
60319585STim.Szeto@Sun.COM 			    id->ident_length;
60329585STim.Szeto@Sun.COM 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
60339585STim.Szeto@Sun.COM 			    KM_NOSLEEP);
60349585STim.Szeto@Sun.COM 			if (devid != NULL) {
60359585STim.Szeto@Sun.COM 				bcopy(id, devid, id_sz);
60369585STim.Szeto@Sun.COM 			}
60379585STim.Szeto@Sun.COM 			break;
60389585STim.Szeto@Sun.COM 		}
60399585STim.Szeto@Sun.COM 	}
60409585STim.Szeto@Sun.COM 
60419585STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
60429585STim.Szeto@Sun.COM 	return (devid);
60439585STim.Szeto@Sun.COM }
60449585STim.Szeto@Sun.COM 
60459585STim.Szeto@Sun.COM uint16_t
stmf_scsilib_get_lport_rtid(struct scsi_devid_desc * devid)60469585STim.Szeto@Sun.COM stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
60479585STim.Szeto@Sun.COM {
60489585STim.Szeto@Sun.COM 	stmf_i_local_port_t	*ilport;
60499585STim.Szeto@Sun.COM 	scsi_devid_desc_t	*id;
60509585STim.Szeto@Sun.COM 	uint16_t		rtpid = 0;
60519585STim.Szeto@Sun.COM 
60529585STim.Szeto@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
60539585STim.Szeto@Sun.COM 	for (ilport = stmf_state.stmf_ilportlist; ilport;
60549585STim.Szeto@Sun.COM 	    ilport = ilport->ilport_next) {
60559585STim.Szeto@Sun.COM 		id = ilport->ilport_lport->lport_id;
60569585STim.Szeto@Sun.COM 		if ((devid->ident_length == id->ident_length) &&
60579585STim.Szeto@Sun.COM 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
60589585STim.Szeto@Sun.COM 			rtpid = ilport->ilport_rtpid;
60599585STim.Szeto@Sun.COM 			break;
60609585STim.Szeto@Sun.COM 		}
60619585STim.Szeto@Sun.COM 	}
60629585STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
60639585STim.Szeto@Sun.COM 	return (rtpid);
60649585STim.Szeto@Sun.COM }
60657836SJohn.Forte@Sun.COM 
60667836SJohn.Forte@Sun.COM static uint16_t stmf_lu_id_gen_number = 0;
60677836SJohn.Forte@Sun.COM 
60687836SJohn.Forte@Sun.COM stmf_status_t
stmf_scsilib_uniq_lu_id(uint32_t company_id,scsi_devid_desc_t * lu_id)60697836SJohn.Forte@Sun.COM stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
60707836SJohn.Forte@Sun.COM {
607110765SJohn.Forte@Sun.COM 	return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id));
607210765SJohn.Forte@Sun.COM }
607310765SJohn.Forte@Sun.COM 
607410765SJohn.Forte@Sun.COM stmf_status_t
stmf_scsilib_uniq_lu_id2(uint32_t company_id,uint32_t host_id,scsi_devid_desc_t * lu_id)607510765SJohn.Forte@Sun.COM stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id,
607610765SJohn.Forte@Sun.COM     scsi_devid_desc_t *lu_id)
607710765SJohn.Forte@Sun.COM {
60787836SJohn.Forte@Sun.COM 	uint8_t *p;
60797836SJohn.Forte@Sun.COM 	struct timeval32 timestamp32;
60807836SJohn.Forte@Sun.COM 	uint32_t *t = (uint32_t *)&timestamp32;
60817836SJohn.Forte@Sun.COM 	struct ether_addr mac;
60827836SJohn.Forte@Sun.COM 	uint8_t *e = (uint8_t *)&mac;
608310765SJohn.Forte@Sun.COM 	int hid = (int)host_id;
60847836SJohn.Forte@Sun.COM 
60857836SJohn.Forte@Sun.COM 	if (company_id == COMPANY_ID_NONE)
60867836SJohn.Forte@Sun.COM 		company_id = COMPANY_ID_SUN;
60877836SJohn.Forte@Sun.COM 
60887836SJohn.Forte@Sun.COM 	if (lu_id->ident_length != 0x10)
60897836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
60907836SJohn.Forte@Sun.COM 
60917836SJohn.Forte@Sun.COM 	p = (uint8_t *)lu_id;
60927836SJohn.Forte@Sun.COM 
60937836SJohn.Forte@Sun.COM 	atomic_add_16(&stmf_lu_id_gen_number, 1);
60947836SJohn.Forte@Sun.COM 
60957836SJohn.Forte@Sun.COM 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
60967836SJohn.Forte@Sun.COM 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
60977836SJohn.Forte@Sun.COM 	p[5] = (company_id >> 12) & 0xff;
60987836SJohn.Forte@Sun.COM 	p[6] = (company_id >> 4) & 0xff;
60997836SJohn.Forte@Sun.COM 	p[7] = (company_id << 4) & 0xf0;
610010765SJohn.Forte@Sun.COM 	if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) {
610110765SJohn.Forte@Sun.COM 		hid = BE_32((int)zone_get_hostid(NULL));
610210765SJohn.Forte@Sun.COM 	}
610310765SJohn.Forte@Sun.COM 	if (hid != 0) {
61047836SJohn.Forte@Sun.COM 		e[0] = (hid >> 24) & 0xff;
61057836SJohn.Forte@Sun.COM 		e[1] = (hid >> 16) & 0xff;
61067836SJohn.Forte@Sun.COM 		e[2] = (hid >> 8) & 0xff;
61077836SJohn.Forte@Sun.COM 		e[3] = hid & 0xff;
61087836SJohn.Forte@Sun.COM 		e[4] = e[5] = 0;
61097836SJohn.Forte@Sun.COM 	}
61107836SJohn.Forte@Sun.COM 	bcopy(e, p+8, 6);
61117836SJohn.Forte@Sun.COM 	uniqtime32(&timestamp32);
61127836SJohn.Forte@Sun.COM 	*t = BE_32(*t);
61137836SJohn.Forte@Sun.COM 	bcopy(t, p+14, 4);
61147836SJohn.Forte@Sun.COM 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
61157836SJohn.Forte@Sun.COM 	p[19] = stmf_lu_id_gen_number & 0xff;
61167836SJohn.Forte@Sun.COM 
61177836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
61187836SJohn.Forte@Sun.COM }
61197836SJohn.Forte@Sun.COM 
61207836SJohn.Forte@Sun.COM /*
61217836SJohn.Forte@Sun.COM  * saa is sense key, ASC, ASCQ
61227836SJohn.Forte@Sun.COM  */
61237836SJohn.Forte@Sun.COM void
stmf_scsilib_send_status(scsi_task_t * task,uint8_t st,uint32_t saa)61247836SJohn.Forte@Sun.COM stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
61257836SJohn.Forte@Sun.COM {
61267836SJohn.Forte@Sun.COM 	uint8_t sd[18];
61277836SJohn.Forte@Sun.COM 	task->task_scsi_status = st;
61287836SJohn.Forte@Sun.COM 	if (st == 2) {
61297836SJohn.Forte@Sun.COM 		bzero(sd, 18);
61307836SJohn.Forte@Sun.COM 		sd[0] = 0x70;
61317836SJohn.Forte@Sun.COM 		sd[2] = (saa >> 16) & 0xf;
61327836SJohn.Forte@Sun.COM 		sd[7] = 10;
61337836SJohn.Forte@Sun.COM 		sd[12] = (saa >> 8) & 0xff;
61347836SJohn.Forte@Sun.COM 		sd[13] = saa & 0xff;
61357836SJohn.Forte@Sun.COM 		task->task_sense_data = sd;
61367836SJohn.Forte@Sun.COM 		task->task_sense_length = 18;
61377836SJohn.Forte@Sun.COM 	} else {
61387836SJohn.Forte@Sun.COM 		task->task_sense_data = NULL;
61397836SJohn.Forte@Sun.COM 		task->task_sense_length = 0;
61407836SJohn.Forte@Sun.COM 	}
61417836SJohn.Forte@Sun.COM 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
61427836SJohn.Forte@Sun.COM }
61437836SJohn.Forte@Sun.COM 
61447836SJohn.Forte@Sun.COM uint32_t
stmf_scsilib_prepare_vpd_page83(scsi_task_t * task,uint8_t * page,uint32_t page_len,uint8_t byte0,uint32_t vpd_mask)61457836SJohn.Forte@Sun.COM stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
61467836SJohn.Forte@Sun.COM     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
61477836SJohn.Forte@Sun.COM {
61487836SJohn.Forte@Sun.COM 	uint8_t		*p = NULL;
61497836SJohn.Forte@Sun.COM 	uint8_t		small_buf[32];
61507836SJohn.Forte@Sun.COM 	uint32_t	sz = 0;
61517836SJohn.Forte@Sun.COM 	uint32_t	n = 4;
61527836SJohn.Forte@Sun.COM 	uint32_t	m = 0;
61537836SJohn.Forte@Sun.COM 	uint32_t	last_bit = 0;
61547836SJohn.Forte@Sun.COM 
61557836SJohn.Forte@Sun.COM 	if (page_len < 4)
61567836SJohn.Forte@Sun.COM 		return (0);
61577836SJohn.Forte@Sun.COM 	if (page_len > 65535)
61587836SJohn.Forte@Sun.COM 		page_len = 65535;
61597836SJohn.Forte@Sun.COM 
61607836SJohn.Forte@Sun.COM 	page[0] = byte0;
61617836SJohn.Forte@Sun.COM 	page[1] = 0x83;
61627836SJohn.Forte@Sun.COM 
61637836SJohn.Forte@Sun.COM 	/* CONSTCOND */
61647836SJohn.Forte@Sun.COM 	while (1) {
61657836SJohn.Forte@Sun.COM 		m += sz;
61667836SJohn.Forte@Sun.COM 		if (sz && (page_len > n)) {
61677836SJohn.Forte@Sun.COM 			uint32_t copysz;
61687836SJohn.Forte@Sun.COM 			copysz = page_len > (n + sz) ? sz : page_len - n;
61697836SJohn.Forte@Sun.COM 			bcopy(p, page + n, copysz);
61707836SJohn.Forte@Sun.COM 			n += copysz;
61717836SJohn.Forte@Sun.COM 		}
61727836SJohn.Forte@Sun.COM 		vpd_mask &= ~last_bit;
61737836SJohn.Forte@Sun.COM 		if (vpd_mask == 0)
61747836SJohn.Forte@Sun.COM 			break;
61757836SJohn.Forte@Sun.COM 
61767836SJohn.Forte@Sun.COM 		if (vpd_mask & STMF_VPD_LU_ID) {
61777836SJohn.Forte@Sun.COM 			last_bit = STMF_VPD_LU_ID;
61787836SJohn.Forte@Sun.COM 			sz = task->task_lu->lu_id->ident_length + 4;
61797836SJohn.Forte@Sun.COM 			p = (uint8_t *)task->task_lu->lu_id;
61807836SJohn.Forte@Sun.COM 			continue;
61817836SJohn.Forte@Sun.COM 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
61827836SJohn.Forte@Sun.COM 			last_bit = STMF_VPD_TARGET_ID;
61837836SJohn.Forte@Sun.COM 			sz = task->task_lport->lport_id->ident_length + 4;
61847836SJohn.Forte@Sun.COM 			p = (uint8_t *)task->task_lport->lport_id;
61857836SJohn.Forte@Sun.COM 			continue;
61867836SJohn.Forte@Sun.COM 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
618710725SJohn.Forte@Sun.COM 			stmf_i_local_port_t *ilport;
61887836SJohn.Forte@Sun.COM 			last_bit = STMF_VPD_TP_GROUP;
61897836SJohn.Forte@Sun.COM 			p = small_buf;
61907836SJohn.Forte@Sun.COM 			bzero(p, 8);
61917836SJohn.Forte@Sun.COM 			p[0] = 1;
61927836SJohn.Forte@Sun.COM 			p[1] = 0x15;
61937836SJohn.Forte@Sun.COM 			p[3] = 4;
619410725SJohn.Forte@Sun.COM 			ilport = (stmf_i_local_port_t *)
619510725SJohn.Forte@Sun.COM 			    task->task_lport->lport_stmf_private;
619611481SJohn.Forte@Sun.COM 			/*
619711481SJohn.Forte@Sun.COM 			 * If we're in alua mode, group 1 contains all alua
619811481SJohn.Forte@Sun.COM 			 * participating ports and all standby ports
619911481SJohn.Forte@Sun.COM 			 * > 255. Otherwise, if we're in alua mode, any local
620011481SJohn.Forte@Sun.COM 			 * ports (non standby/pppt) are also in group 1 if the
620111481SJohn.Forte@Sun.COM 			 * alua node is 1. Otherwise the group is 0.
620211481SJohn.Forte@Sun.COM 			 */
620311481SJohn.Forte@Sun.COM 			if ((stmf_state.stmf_alua_state &&
620411481SJohn.Forte@Sun.COM 			    (ilport->ilport_alua || ilport->ilport_standby) &&
620511481SJohn.Forte@Sun.COM 			    ilport->ilport_rtpid > 255) ||
620611481SJohn.Forte@Sun.COM 			    (stmf_state.stmf_alua_node == 1 &&
620711481SJohn.Forte@Sun.COM 			    ilport->ilport_standby != 1)) {
620810725SJohn.Forte@Sun.COM 				p[7] = 1;	/* Group 1 */
620910725SJohn.Forte@Sun.COM 			}
62107836SJohn.Forte@Sun.COM 			sz = 8;
62117836SJohn.Forte@Sun.COM 			continue;
62127836SJohn.Forte@Sun.COM 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
62137836SJohn.Forte@Sun.COM 			stmf_i_local_port_t *ilport;
62147836SJohn.Forte@Sun.COM 
62157836SJohn.Forte@Sun.COM 			last_bit = STMF_VPD_RELATIVE_TP_ID;
62167836SJohn.Forte@Sun.COM 			p = small_buf;
62177836SJohn.Forte@Sun.COM 			bzero(p, 8);
62187836SJohn.Forte@Sun.COM 			p[0] = 1;
62197836SJohn.Forte@Sun.COM 			p[1] = 0x14;
62207836SJohn.Forte@Sun.COM 			p[3] = 4;
62217836SJohn.Forte@Sun.COM 			ilport = (stmf_i_local_port_t *)
62228662SJordan.Vaughan@Sun.com 			    task->task_lport->lport_stmf_private;
62237836SJohn.Forte@Sun.COM 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
62247836SJohn.Forte@Sun.COM 			p[7] = ilport->ilport_rtpid & 0xff;
62257836SJohn.Forte@Sun.COM 			sz = 8;
62267836SJohn.Forte@Sun.COM 			continue;
62277836SJohn.Forte@Sun.COM 		} else {
62287836SJohn.Forte@Sun.COM 			cmn_err(CE_WARN, "Invalid vpd_mask");
62297836SJohn.Forte@Sun.COM 			break;
62307836SJohn.Forte@Sun.COM 		}
62317836SJohn.Forte@Sun.COM 	}
62327836SJohn.Forte@Sun.COM 
62337836SJohn.Forte@Sun.COM 	page[2] = (m >> 8) & 0xff;
62347836SJohn.Forte@Sun.COM 	page[3] = m & 0xff;
62357836SJohn.Forte@Sun.COM 
62367836SJohn.Forte@Sun.COM 	return (n);
62377836SJohn.Forte@Sun.COM }
62387836SJohn.Forte@Sun.COM 
62397836SJohn.Forte@Sun.COM void
stmf_scsilib_handle_report_tpgs(scsi_task_t * task,stmf_data_buf_t * dbuf)62407836SJohn.Forte@Sun.COM stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
62417836SJohn.Forte@Sun.COM {
62427836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
62438662SJordan.Vaughan@Sun.com 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
624410725SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu =
624510725SJohn.Forte@Sun.COM 	    (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
62467836SJohn.Forte@Sun.COM 	stmf_xfer_data_t *xd;
62477836SJohn.Forte@Sun.COM 	uint32_t sz, minsz;
62487836SJohn.Forte@Sun.COM 
62497836SJohn.Forte@Sun.COM 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
62507836SJohn.Forte@Sun.COM 	task->task_cmd_xfer_length =
62517836SJohn.Forte@Sun.COM 	    ((((uint32_t)task->task_cdb[6]) << 24) |
62527836SJohn.Forte@Sun.COM 	    (((uint32_t)task->task_cdb[7]) << 16) |
62537836SJohn.Forte@Sun.COM 	    (((uint32_t)task->task_cdb[8]) << 8) |
62547836SJohn.Forte@Sun.COM 	    ((uint32_t)task->task_cdb[9]));
62557836SJohn.Forte@Sun.COM 
62567836SJohn.Forte@Sun.COM 	if (task->task_additional_flags &
62577836SJohn.Forte@Sun.COM 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
62587836SJohn.Forte@Sun.COM 		task->task_expected_xfer_length =
62597836SJohn.Forte@Sun.COM 		    task->task_cmd_xfer_length;
62607836SJohn.Forte@Sun.COM 	}
62617836SJohn.Forte@Sun.COM 
62629962SWayne.Ihde@Sun.COM 	if (task->task_cmd_xfer_length == 0) {
62639962SWayne.Ihde@Sun.COM 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
62649962SWayne.Ihde@Sun.COM 		return;
62659962SWayne.Ihde@Sun.COM 	}
62669962SWayne.Ihde@Sun.COM 	if (task->task_cmd_xfer_length < 4) {
62677836SJohn.Forte@Sun.COM 		stmf_scsilib_send_status(task, STATUS_CHECK,
62688662SJordan.Vaughan@Sun.com 		    STMF_SAA_INVALID_FIELD_IN_CDB);
62697836SJohn.Forte@Sun.COM 		return;
62707836SJohn.Forte@Sun.COM 	}
62717836SJohn.Forte@Sun.COM 
62727836SJohn.Forte@Sun.COM 	sz = min(task->task_expected_xfer_length,
62737836SJohn.Forte@Sun.COM 	    task->task_cmd_xfer_length);
62747836SJohn.Forte@Sun.COM 
627510725SJohn.Forte@Sun.COM 	xd = stmf_prepare_tpgs_data(ilu->ilu_alua);
62767836SJohn.Forte@Sun.COM 
62777836SJohn.Forte@Sun.COM 	if (xd == NULL) {
62787836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
62797836SJohn.Forte@Sun.COM 		    STMF_ALLOC_FAILURE, NULL);
62807836SJohn.Forte@Sun.COM 		return;
62817836SJohn.Forte@Sun.COM 	}
62827836SJohn.Forte@Sun.COM 
62837836SJohn.Forte@Sun.COM 	sz = min(sz, xd->size_left);
62847836SJohn.Forte@Sun.COM 	xd->size_left = sz;
62857836SJohn.Forte@Sun.COM 	minsz = min(512, sz);
62867836SJohn.Forte@Sun.COM 
62877836SJohn.Forte@Sun.COM 	if (dbuf == NULL)
62887836SJohn.Forte@Sun.COM 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
62897836SJohn.Forte@Sun.COM 	if (dbuf == NULL) {
62907836SJohn.Forte@Sun.COM 		kmem_free(xd, xd->alloc_size);
62917836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
62927836SJohn.Forte@Sun.COM 		    STMF_ALLOC_FAILURE, NULL);
62937836SJohn.Forte@Sun.COM 		return;
62947836SJohn.Forte@Sun.COM 	}
62957836SJohn.Forte@Sun.COM 	dbuf->db_lu_private = xd;
629612340SJohn.Forte@Sun.COM 	stmf_xd_to_dbuf(dbuf, 1);
62977836SJohn.Forte@Sun.COM 
62987836SJohn.Forte@Sun.COM 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
62997836SJohn.Forte@Sun.COM 	(void) stmf_xfer_data(task, dbuf, 0);
63007836SJohn.Forte@Sun.COM 
63017836SJohn.Forte@Sun.COM }
63027836SJohn.Forte@Sun.COM 
63037836SJohn.Forte@Sun.COM void
stmf_scsilib_handle_task_mgmt(scsi_task_t * task)63047836SJohn.Forte@Sun.COM stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
63057836SJohn.Forte@Sun.COM {
630610725SJohn.Forte@Sun.COM 
63077836SJohn.Forte@Sun.COM 	switch (task->task_mgmt_function) {
63087836SJohn.Forte@Sun.COM 	/*
63097836SJohn.Forte@Sun.COM 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
63107836SJohn.Forte@Sun.COM 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
63117836SJohn.Forte@Sun.COM 	 * in these cases. This needs to be changed to abort only the required
63127836SJohn.Forte@Sun.COM 	 * set.
63137836SJohn.Forte@Sun.COM 	 */
63147836SJohn.Forte@Sun.COM 	case TM_ABORT_TASK:
63157836SJohn.Forte@Sun.COM 	case TM_ABORT_TASK_SET:
63167836SJohn.Forte@Sun.COM 	case TM_CLEAR_TASK_SET:
63177836SJohn.Forte@Sun.COM 	case TM_LUN_RESET:
63187836SJohn.Forte@Sun.COM 		stmf_handle_lun_reset(task);
631910725SJohn.Forte@Sun.COM 		/* issue the reset to the proxy node as well */
632011103SJohn.Forte@Sun.COM 		if (stmf_state.stmf_alua_state == 1) {
632111103SJohn.Forte@Sun.COM 			(void) stmf_proxy_scsi_cmd(task, NULL);
632211103SJohn.Forte@Sun.COM 		}
63237836SJohn.Forte@Sun.COM 		return;
63247836SJohn.Forte@Sun.COM 	case TM_TARGET_RESET:
63257836SJohn.Forte@Sun.COM 	case TM_TARGET_COLD_RESET:
63267836SJohn.Forte@Sun.COM 	case TM_TARGET_WARM_RESET:
63277836SJohn.Forte@Sun.COM 		stmf_handle_target_reset(task);
63287836SJohn.Forte@Sun.COM 		return;
63297836SJohn.Forte@Sun.COM 	default:
63307836SJohn.Forte@Sun.COM 		/* We dont support this task mgmt function */
63317836SJohn.Forte@Sun.COM 		stmf_scsilib_send_status(task, STATUS_CHECK,
63327836SJohn.Forte@Sun.COM 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
63337836SJohn.Forte@Sun.COM 		return;
63347836SJohn.Forte@Sun.COM 	}
63357836SJohn.Forte@Sun.COM }
63367836SJohn.Forte@Sun.COM 
63377836SJohn.Forte@Sun.COM void
stmf_handle_lun_reset(scsi_task_t * task)63387836SJohn.Forte@Sun.COM stmf_handle_lun_reset(scsi_task_t *task)
63397836SJohn.Forte@Sun.COM {
63407836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
63417836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
63427836SJohn.Forte@Sun.COM 
63437836SJohn.Forte@Sun.COM 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
63447836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
63457836SJohn.Forte@Sun.COM 
63467836SJohn.Forte@Sun.COM 	/*
63477836SJohn.Forte@Sun.COM 	 * To sync with target reset, grab this lock. The LU is not going
63487836SJohn.Forte@Sun.COM 	 * anywhere as there is atleast one task pending (this task).
63497836SJohn.Forte@Sun.COM 	 */
63507836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
63517836SJohn.Forte@Sun.COM 
63527836SJohn.Forte@Sun.COM 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
63537836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
63547836SJohn.Forte@Sun.COM 		stmf_scsilib_send_status(task, STATUS_CHECK,
63558662SJordan.Vaughan@Sun.com 		    STMF_SAA_OPERATION_IN_PROGRESS);
63567836SJohn.Forte@Sun.COM 		return;
63577836SJohn.Forte@Sun.COM 	}
63587836SJohn.Forte@Sun.COM 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
63597836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
63607836SJohn.Forte@Sun.COM 
63617836SJohn.Forte@Sun.COM 	/*
63627836SJohn.Forte@Sun.COM 	 * Mark this task as the one causing LU reset so that we know who
63637836SJohn.Forte@Sun.COM 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
63647836SJohn.Forte@Sun.COM 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
63657836SJohn.Forte@Sun.COM 	 */
63667836SJohn.Forte@Sun.COM 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
63677836SJohn.Forte@Sun.COM 
63687836SJohn.Forte@Sun.COM 	/* Initiatiate abort on all commands on this LU except this one */
63697836SJohn.Forte@Sun.COM 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
63707836SJohn.Forte@Sun.COM 
63717836SJohn.Forte@Sun.COM 	/* Start polling on this task */
63727836SJohn.Forte@Sun.COM 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
63737836SJohn.Forte@Sun.COM 	    != STMF_SUCCESS) {
63747836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
63758662SJordan.Vaughan@Sun.com 		    NULL);
63767836SJohn.Forte@Sun.COM 		return;
63777836SJohn.Forte@Sun.COM 	}
63787836SJohn.Forte@Sun.COM }
63797836SJohn.Forte@Sun.COM 
63807836SJohn.Forte@Sun.COM void
stmf_handle_target_reset(scsi_task_t * task)63817836SJohn.Forte@Sun.COM stmf_handle_target_reset(scsi_task_t *task)
63827836SJohn.Forte@Sun.COM {
63837836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
63847836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
63857836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
63867836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
63877836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *lm_ent;
63887836SJohn.Forte@Sun.COM 	int i, lf;
63897836SJohn.Forte@Sun.COM 
63907836SJohn.Forte@Sun.COM 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
63917836SJohn.Forte@Sun.COM 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
63927836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
63937836SJohn.Forte@Sun.COM 
63947836SJohn.Forte@Sun.COM 	/*
63957836SJohn.Forte@Sun.COM 	 * To sync with LUN reset, grab this lock. The session is not going
63967836SJohn.Forte@Sun.COM 	 * anywhere as there is atleast one task pending (this task).
63977836SJohn.Forte@Sun.COM 	 */
63987836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
63997979SSumit.Gupta@Sun.COM 
64007979SSumit.Gupta@Sun.COM 	/* Grab the session lock as a writer to prevent any changes in it */
64017979SSumit.Gupta@Sun.COM 	rw_enter(iss->iss_lockp, RW_WRITER);
64027979SSumit.Gupta@Sun.COM 
64037836SJohn.Forte@Sun.COM 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
64047979SSumit.Gupta@Sun.COM 		rw_exit(iss->iss_lockp);
64057836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
64067836SJohn.Forte@Sun.COM 		stmf_scsilib_send_status(task, STATUS_CHECK,
64078662SJordan.Vaughan@Sun.com 		    STMF_SAA_OPERATION_IN_PROGRESS);
64087836SJohn.Forte@Sun.COM 		return;
64097836SJohn.Forte@Sun.COM 	}
64107836SJohn.Forte@Sun.COM 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
64117836SJohn.Forte@Sun.COM 
64127836SJohn.Forte@Sun.COM 	/*
64137836SJohn.Forte@Sun.COM 	 * Now go through each LUN in this session and make sure all of them
64147836SJohn.Forte@Sun.COM 	 * can be reset.
64157836SJohn.Forte@Sun.COM 	 */
64167836SJohn.Forte@Sun.COM 	lm = iss->iss_sm;
64177836SJohn.Forte@Sun.COM 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
64187836SJohn.Forte@Sun.COM 		if (lm->lm_plus[i] == NULL)
64197836SJohn.Forte@Sun.COM 			continue;
64207836SJohn.Forte@Sun.COM 		lf++;
64217836SJohn.Forte@Sun.COM 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
64227836SJohn.Forte@Sun.COM 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
64237836SJohn.Forte@Sun.COM 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
64247836SJohn.Forte@Sun.COM 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
64257979SSumit.Gupta@Sun.COM 			rw_exit(iss->iss_lockp);
64267836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
64277836SJohn.Forte@Sun.COM 			stmf_scsilib_send_status(task, STATUS_CHECK,
64288662SJordan.Vaughan@Sun.com 			    STMF_SAA_OPERATION_IN_PROGRESS);
64297836SJohn.Forte@Sun.COM 			return;
64307836SJohn.Forte@Sun.COM 		}
64317836SJohn.Forte@Sun.COM 	}
64327836SJohn.Forte@Sun.COM 	if (lf == 0) {
64337836SJohn.Forte@Sun.COM 		/* No luns in this session */
64347836SJohn.Forte@Sun.COM 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
64357979SSumit.Gupta@Sun.COM 		rw_exit(iss->iss_lockp);
64367836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
64377836SJohn.Forte@Sun.COM 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
64387836SJohn.Forte@Sun.COM 		return;
64397836SJohn.Forte@Sun.COM 	}
64407836SJohn.Forte@Sun.COM 
64417836SJohn.Forte@Sun.COM 	/* ok, start the damage */
64427836SJohn.Forte@Sun.COM 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
64438662SJordan.Vaughan@Sun.com 	    ITASK_CAUSING_TARGET_RESET;
64447836SJohn.Forte@Sun.COM 	for (i = 0; i < lm->lm_nentries; i++) {
64457836SJohn.Forte@Sun.COM 		if (lm->lm_plus[i] == NULL)
64467836SJohn.Forte@Sun.COM 			continue;
64477836SJohn.Forte@Sun.COM 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
64487836SJohn.Forte@Sun.COM 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
64497836SJohn.Forte@Sun.COM 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
64507836SJohn.Forte@Sun.COM 	}
64517836SJohn.Forte@Sun.COM 
64527836SJohn.Forte@Sun.COM 	for (i = 0; i < lm->lm_nentries; i++) {
64537836SJohn.Forte@Sun.COM 		if (lm->lm_plus[i] == NULL)
64547836SJohn.Forte@Sun.COM 			continue;
64557836SJohn.Forte@Sun.COM 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
64567836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
64578662SJordan.Vaughan@Sun.com 		    lm_ent->ent_lu);
64587836SJohn.Forte@Sun.COM 	}
64597836SJohn.Forte@Sun.COM 
646011978STim.Szeto@Sun.COM 	rw_exit(iss->iss_lockp);
646111978STim.Szeto@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
646211978STim.Szeto@Sun.COM 
64637836SJohn.Forte@Sun.COM 	/* Start polling on this task */
64647836SJohn.Forte@Sun.COM 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
64657836SJohn.Forte@Sun.COM 	    != STMF_SUCCESS) {
64667836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
64678662SJordan.Vaughan@Sun.com 		    NULL);
64687836SJohn.Forte@Sun.COM 		return;
64697836SJohn.Forte@Sun.COM 	}
64707836SJohn.Forte@Sun.COM }
64717836SJohn.Forte@Sun.COM 
64727836SJohn.Forte@Sun.COM int
stmf_handle_cmd_during_ic(stmf_i_scsi_task_t * itask)64737836SJohn.Forte@Sun.COM stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
64747836SJohn.Forte@Sun.COM {
64757836SJohn.Forte@Sun.COM 	scsi_task_t *task = itask->itask_task;
64767836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
64777836SJohn.Forte@Sun.COM 	    task->task_session->ss_stmf_private;
64787836SJohn.Forte@Sun.COM 
64797836SJohn.Forte@Sun.COM 	rw_enter(iss->iss_lockp, RW_WRITER);
64807836SJohn.Forte@Sun.COM 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
64817836SJohn.Forte@Sun.COM 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
64827836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
64837836SJohn.Forte@Sun.COM 		return (0);
64847836SJohn.Forte@Sun.COM 	}
64857836SJohn.Forte@Sun.COM 	atomic_and_32(&iss->iss_flags,
64868662SJordan.Vaughan@Sun.com 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
64877836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
64887836SJohn.Forte@Sun.COM 
64897836SJohn.Forte@Sun.COM 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
64907836SJohn.Forte@Sun.COM 		return (0);
64917836SJohn.Forte@Sun.COM 	}
64927836SJohn.Forte@Sun.COM 	stmf_scsilib_send_status(task, STATUS_CHECK,
64937836SJohn.Forte@Sun.COM 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
64947836SJohn.Forte@Sun.COM 	return (1);
64957836SJohn.Forte@Sun.COM }
64967836SJohn.Forte@Sun.COM 
64977836SJohn.Forte@Sun.COM void
stmf_worker_init()64987836SJohn.Forte@Sun.COM stmf_worker_init()
64997836SJohn.Forte@Sun.COM {
65007836SJohn.Forte@Sun.COM 	uint32_t i;
65017836SJohn.Forte@Sun.COM 
65027836SJohn.Forte@Sun.COM 	/* Make local copy of global tunables */
65037836SJohn.Forte@Sun.COM 	stmf_i_max_nworkers = stmf_max_nworkers;
65047836SJohn.Forte@Sun.COM 	stmf_i_min_nworkers = stmf_min_nworkers;
65057836SJohn.Forte@Sun.COM 
65067836SJohn.Forte@Sun.COM 	ASSERT(stmf_workers == NULL);
65077836SJohn.Forte@Sun.COM 	if (stmf_i_min_nworkers < 4) {
65087836SJohn.Forte@Sun.COM 		stmf_i_min_nworkers = 4;
65097836SJohn.Forte@Sun.COM 	}
65107836SJohn.Forte@Sun.COM 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
65117836SJohn.Forte@Sun.COM 		stmf_i_max_nworkers = stmf_i_min_nworkers;
65127836SJohn.Forte@Sun.COM 	}
65137836SJohn.Forte@Sun.COM 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
65147836SJohn.Forte@Sun.COM 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
65157836SJohn.Forte@Sun.COM 	for (i = 0; i < stmf_i_max_nworkers; i++) {
65167836SJohn.Forte@Sun.COM 		stmf_worker_t *w = &stmf_workers[i];
65177836SJohn.Forte@Sun.COM 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
65187836SJohn.Forte@Sun.COM 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
65197836SJohn.Forte@Sun.COM 	}
65207836SJohn.Forte@Sun.COM 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
65217836SJohn.Forte@Sun.COM 	stmf_workers_state = STMF_WORKERS_ENABLED;
65227836SJohn.Forte@Sun.COM 
65237836SJohn.Forte@Sun.COM 	/* Workers will be started by stmf_worker_mgmt() */
65247836SJohn.Forte@Sun.COM 
65257836SJohn.Forte@Sun.COM 	/* Lets wait for atleast one worker to start */
65267836SJohn.Forte@Sun.COM 	while (stmf_nworkers_cur == 0)
65277836SJohn.Forte@Sun.COM 		delay(drv_usectohz(20 * 1000));
65287836SJohn.Forte@Sun.COM 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
65297836SJohn.Forte@Sun.COM }
65307836SJohn.Forte@Sun.COM 
65317836SJohn.Forte@Sun.COM stmf_status_t
stmf_worker_fini()65327836SJohn.Forte@Sun.COM stmf_worker_fini()
65337836SJohn.Forte@Sun.COM {
65347836SJohn.Forte@Sun.COM 	int i;
65357836SJohn.Forte@Sun.COM 	clock_t sb;
65367836SJohn.Forte@Sun.COM 
65377836SJohn.Forte@Sun.COM 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
65387836SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
65397836SJohn.Forte@Sun.COM 	ASSERT(stmf_workers);
65407836SJohn.Forte@Sun.COM 	stmf_workers_state = STMF_WORKERS_DISABLED;
65417836SJohn.Forte@Sun.COM 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
65427836SJohn.Forte@Sun.COM 	cv_signal(&stmf_state.stmf_cv);
65437836SJohn.Forte@Sun.COM 
65447836SJohn.Forte@Sun.COM 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
65457836SJohn.Forte@Sun.COM 	/* Wait for all the threads to die */
65467836SJohn.Forte@Sun.COM 	while (stmf_nworkers_cur != 0) {
65477836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() > sb) {
65487836SJohn.Forte@Sun.COM 			stmf_workers_state = STMF_WORKERS_ENABLED;
65497836SJohn.Forte@Sun.COM 			return (STMF_BUSY);
65507836SJohn.Forte@Sun.COM 		}
65517836SJohn.Forte@Sun.COM 		delay(drv_usectohz(100 * 1000));
65527836SJohn.Forte@Sun.COM 	}
65537836SJohn.Forte@Sun.COM 	for (i = 0; i < stmf_i_max_nworkers; i++) {
65547836SJohn.Forte@Sun.COM 		stmf_worker_t *w = &stmf_workers[i];
65557836SJohn.Forte@Sun.COM 		mutex_destroy(&w->worker_lock);
65567836SJohn.Forte@Sun.COM 		cv_destroy(&w->worker_cv);
65577836SJohn.Forte@Sun.COM 	}
65587836SJohn.Forte@Sun.COM 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
65597836SJohn.Forte@Sun.COM 	stmf_workers = NULL;
65607836SJohn.Forte@Sun.COM 
65617836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
65627836SJohn.Forte@Sun.COM }
65637836SJohn.Forte@Sun.COM 
65647836SJohn.Forte@Sun.COM void
stmf_worker_task(void * arg)65657836SJohn.Forte@Sun.COM stmf_worker_task(void *arg)
65667836SJohn.Forte@Sun.COM {
65677836SJohn.Forte@Sun.COM 	stmf_worker_t *w;
65687836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
65697836SJohn.Forte@Sun.COM 	scsi_task_t *task;
65707836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask;
65717836SJohn.Forte@Sun.COM 	stmf_data_buf_t *dbuf;
65727836SJohn.Forte@Sun.COM 	stmf_lu_t *lu;
65737836SJohn.Forte@Sun.COM 	clock_t wait_timer = 0;
657411066Srafael.vanoni@sun.com 	clock_t wait_ticks, wait_delta = 0;
65757836SJohn.Forte@Sun.COM 	uint32_t old, new;
65767836SJohn.Forte@Sun.COM 	uint8_t curcmd;
65777836SJohn.Forte@Sun.COM 	uint8_t abort_free;
65787836SJohn.Forte@Sun.COM 	uint8_t wait_queue;
65797836SJohn.Forte@Sun.COM 	uint8_t dec_qdepth;
65807836SJohn.Forte@Sun.COM 
65817836SJohn.Forte@Sun.COM 	w = (stmf_worker_t *)arg;
65827836SJohn.Forte@Sun.COM 	wait_ticks = drv_usectohz(10000);
65837836SJohn.Forte@Sun.COM 
658411773STim.Szeto@Sun.COM 	DTRACE_PROBE1(worker__create, stmf_worker_t, w);
65857836SJohn.Forte@Sun.COM 	mutex_enter(&w->worker_lock);
65867836SJohn.Forte@Sun.COM 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
65877836SJohn.Forte@Sun.COM stmf_worker_loop:;
65887836SJohn.Forte@Sun.COM 	if ((w->worker_ref_count == 0) &&
65897836SJohn.Forte@Sun.COM 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
65907836SJohn.Forte@Sun.COM 		w->worker_flags &= ~(STMF_WORKER_STARTED |
65917836SJohn.Forte@Sun.COM 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
65927836SJohn.Forte@Sun.COM 		w->worker_tid = NULL;
65937836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
659411773STim.Szeto@Sun.COM 		DTRACE_PROBE1(worker__destroy, stmf_worker_t, w);
65957836SJohn.Forte@Sun.COM 		thread_exit();
65967836SJohn.Forte@Sun.COM 	}
65977836SJohn.Forte@Sun.COM 	/* CONSTCOND */
65987836SJohn.Forte@Sun.COM 	while (1) {
65997836SJohn.Forte@Sun.COM 		dec_qdepth = 0;
66007836SJohn.Forte@Sun.COM 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
66017836SJohn.Forte@Sun.COM 			wait_timer = 0;
660211066Srafael.vanoni@sun.com 			wait_delta = 0;
66037979SSumit.Gupta@Sun.COM 			if (w->worker_wait_head) {
66047979SSumit.Gupta@Sun.COM 				ASSERT(w->worker_wait_tail);
66057979SSumit.Gupta@Sun.COM 				if (w->worker_task_head == NULL)
66067979SSumit.Gupta@Sun.COM 					w->worker_task_head =
66077979SSumit.Gupta@Sun.COM 					    w->worker_wait_head;
66087979SSumit.Gupta@Sun.COM 				else
66097979SSumit.Gupta@Sun.COM 					w->worker_task_tail->itask_worker_next =
66108662SJordan.Vaughan@Sun.com 					    w->worker_wait_head;
66117979SSumit.Gupta@Sun.COM 				w->worker_task_tail = w->worker_wait_tail;
66127979SSumit.Gupta@Sun.COM 				w->worker_wait_head = w->worker_wait_tail =
66137979SSumit.Gupta@Sun.COM 				    NULL;
66147979SSumit.Gupta@Sun.COM 			}
66157836SJohn.Forte@Sun.COM 		}
66167836SJohn.Forte@Sun.COM 		if ((itask = w->worker_task_head) == NULL) {
66177836SJohn.Forte@Sun.COM 			break;
66187836SJohn.Forte@Sun.COM 		}
66197836SJohn.Forte@Sun.COM 		task = itask->itask_task;
662011773STim.Szeto@Sun.COM 		DTRACE_PROBE2(worker__active, stmf_worker_t, w,
662111773STim.Szeto@Sun.COM 		    scsi_task_t *, task);
66227836SJohn.Forte@Sun.COM 		w->worker_task_head = itask->itask_worker_next;
66237836SJohn.Forte@Sun.COM 		if (w->worker_task_head == NULL)
66247836SJohn.Forte@Sun.COM 			w->worker_task_tail = NULL;
66257836SJohn.Forte@Sun.COM 
66267836SJohn.Forte@Sun.COM 		wait_queue = 0;
66277836SJohn.Forte@Sun.COM 		abort_free = 0;
66287836SJohn.Forte@Sun.COM 		if (itask->itask_ncmds > 0) {
66297836SJohn.Forte@Sun.COM 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
66307836SJohn.Forte@Sun.COM 		} else {
66317836SJohn.Forte@Sun.COM 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
66327836SJohn.Forte@Sun.COM 		}
66337836SJohn.Forte@Sun.COM 		do {
66347836SJohn.Forte@Sun.COM 			old = itask->itask_flags;
66357836SJohn.Forte@Sun.COM 			if (old & ITASK_BEING_ABORTED) {
66367836SJohn.Forte@Sun.COM 				itask->itask_ncmds = 1;
66377836SJohn.Forte@Sun.COM 				curcmd = itask->itask_cmd_stack[0] =
66387836SJohn.Forte@Sun.COM 				    ITASK_CMD_ABORT;
66397836SJohn.Forte@Sun.COM 				goto out_itask_flag_loop;
66407836SJohn.Forte@Sun.COM 			} else if ((curcmd & ITASK_CMD_MASK) ==
66417836SJohn.Forte@Sun.COM 			    ITASK_CMD_NEW_TASK) {
664210421STim.Szeto@Sun.COM 				/*
664310421STim.Szeto@Sun.COM 				 * set ITASK_KSTAT_IN_RUNQ, this flag
664410421STim.Szeto@Sun.COM 				 * will not reset until task completed
664510421STim.Szeto@Sun.COM 				 */
664610421STim.Szeto@Sun.COM 				new = old | ITASK_KNOWN_TO_LU |
664710421STim.Szeto@Sun.COM 				    ITASK_KSTAT_IN_RUNQ;
66487836SJohn.Forte@Sun.COM 			} else {
66497836SJohn.Forte@Sun.COM 				goto out_itask_flag_loop;
66507836SJohn.Forte@Sun.COM 			}
66517836SJohn.Forte@Sun.COM 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
66527836SJohn.Forte@Sun.COM 
66537836SJohn.Forte@Sun.COM out_itask_flag_loop:
66547836SJohn.Forte@Sun.COM 
66557836SJohn.Forte@Sun.COM 		/*
66567836SJohn.Forte@Sun.COM 		 * Decide if this task needs to go to a queue and/or if
66577836SJohn.Forte@Sun.COM 		 * we can decrement the itask_cmd_stack.
66587836SJohn.Forte@Sun.COM 		 */
66597836SJohn.Forte@Sun.COM 		if (curcmd == ITASK_CMD_ABORT) {
66607836SJohn.Forte@Sun.COM 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
66617836SJohn.Forte@Sun.COM 			    ITASK_KNOWN_TO_TGT_PORT)) {
66627836SJohn.Forte@Sun.COM 				wait_queue = 1;
66637836SJohn.Forte@Sun.COM 			} else {
66647836SJohn.Forte@Sun.COM 				abort_free = 1;
66657836SJohn.Forte@Sun.COM 			}
66667836SJohn.Forte@Sun.COM 		} else if ((curcmd & ITASK_CMD_POLL) &&
66677836SJohn.Forte@Sun.COM 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
66687836SJohn.Forte@Sun.COM 			wait_queue = 1;
66697836SJohn.Forte@Sun.COM 		}
66707836SJohn.Forte@Sun.COM 
66717836SJohn.Forte@Sun.COM 		if (wait_queue) {
66727836SJohn.Forte@Sun.COM 			itask->itask_worker_next = NULL;
66737836SJohn.Forte@Sun.COM 			if (w->worker_wait_tail) {
66747836SJohn.Forte@Sun.COM 				w->worker_wait_tail->itask_worker_next = itask;
66757836SJohn.Forte@Sun.COM 			} else {
66767836SJohn.Forte@Sun.COM 				w->worker_wait_head = itask;
66777836SJohn.Forte@Sun.COM 			}
66787836SJohn.Forte@Sun.COM 			w->worker_wait_tail = itask;
66797836SJohn.Forte@Sun.COM 			if (wait_timer == 0) {
66807836SJohn.Forte@Sun.COM 				wait_timer = ddi_get_lbolt() + wait_ticks;
668111066Srafael.vanoni@sun.com 				wait_delta = wait_ticks;
66827836SJohn.Forte@Sun.COM 			}
66837836SJohn.Forte@Sun.COM 		} else if ((--(itask->itask_ncmds)) != 0) {
66847836SJohn.Forte@Sun.COM 			itask->itask_worker_next = NULL;
66857836SJohn.Forte@Sun.COM 			if (w->worker_task_tail) {
66867836SJohn.Forte@Sun.COM 				w->worker_task_tail->itask_worker_next = itask;
66877836SJohn.Forte@Sun.COM 			} else {
66887836SJohn.Forte@Sun.COM 				w->worker_task_head = itask;
66897836SJohn.Forte@Sun.COM 			}
66907836SJohn.Forte@Sun.COM 			w->worker_task_tail = itask;
66917836SJohn.Forte@Sun.COM 		} else {
66927836SJohn.Forte@Sun.COM 			atomic_and_32(&itask->itask_flags,
66937836SJohn.Forte@Sun.COM 			    ~ITASK_IN_WORKER_QUEUE);
66947836SJohn.Forte@Sun.COM 			/*
66957836SJohn.Forte@Sun.COM 			 * This is where the queue depth should go down by
66967836SJohn.Forte@Sun.COM 			 * one but we delay that on purpose to account for
66977836SJohn.Forte@Sun.COM 			 * the call into the provider. The actual decrement
66987836SJohn.Forte@Sun.COM 			 * happens after the worker has done its job.
66997836SJohn.Forte@Sun.COM 			 */
67007836SJohn.Forte@Sun.COM 			dec_qdepth = 1;
670111773STim.Szeto@Sun.COM 			itask->itask_waitq_time +=
670211773STim.Szeto@Sun.COM 			    gethrtime() - itask->itask_waitq_enter_timestamp;
67037836SJohn.Forte@Sun.COM 		}
67047836SJohn.Forte@Sun.COM 
67057836SJohn.Forte@Sun.COM 		/* We made it here means we are going to call LU */
67067836SJohn.Forte@Sun.COM 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
67077836SJohn.Forte@Sun.COM 			lu = task->task_lu;
67087836SJohn.Forte@Sun.COM 		else
67097836SJohn.Forte@Sun.COM 			lu = dlun0;
67107836SJohn.Forte@Sun.COM 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
67117836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
67127836SJohn.Forte@Sun.COM 		curcmd &= ITASK_CMD_MASK;
671312625SJohn.Forte@Sun.COM 		stmf_task_audit(itask, TE_PROCESS_CMD, curcmd, dbuf);
67147836SJohn.Forte@Sun.COM 		switch (curcmd) {
67157836SJohn.Forte@Sun.COM 		case ITASK_CMD_NEW_TASK:
67167836SJohn.Forte@Sun.COM 			iss = (stmf_i_scsi_session_t *)
67177836SJohn.Forte@Sun.COM 			    task->task_session->ss_stmf_private;
671811773STim.Szeto@Sun.COM 			stmf_itl_lu_new_task(itask);
67197836SJohn.Forte@Sun.COM 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
67207836SJohn.Forte@Sun.COM 				if (stmf_handle_cmd_during_ic(itask))
67217836SJohn.Forte@Sun.COM 					break;
67227836SJohn.Forte@Sun.COM 			}
67237836SJohn.Forte@Sun.COM #ifdef	DEBUG
67247836SJohn.Forte@Sun.COM 			if (stmf_drop_task_counter > 0) {
67257836SJohn.Forte@Sun.COM 				if (atomic_add_32_nv(
67267836SJohn.Forte@Sun.COM 				    (uint32_t *)&stmf_drop_task_counter,
67277836SJohn.Forte@Sun.COM 				    -1) == 1) {
67287836SJohn.Forte@Sun.COM 					break;
67297836SJohn.Forte@Sun.COM 				}
67307836SJohn.Forte@Sun.COM 			}
67317836SJohn.Forte@Sun.COM #endif
67328859STim.Szeto@Sun.COM 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
67337836SJohn.Forte@Sun.COM 			lu->lu_new_task(task, dbuf);
67347836SJohn.Forte@Sun.COM 			break;
67357836SJohn.Forte@Sun.COM 		case ITASK_CMD_DATA_XFER_DONE:
67367836SJohn.Forte@Sun.COM 			lu->lu_dbuf_xfer_done(task, dbuf);
67377836SJohn.Forte@Sun.COM 			break;
67387836SJohn.Forte@Sun.COM 		case ITASK_CMD_STATUS_DONE:
67397836SJohn.Forte@Sun.COM 			lu->lu_send_status_done(task);
67407836SJohn.Forte@Sun.COM 			break;
67417836SJohn.Forte@Sun.COM 		case ITASK_CMD_ABORT:
67427836SJohn.Forte@Sun.COM 			if (abort_free) {
67437836SJohn.Forte@Sun.COM 				stmf_task_free(task);
67447836SJohn.Forte@Sun.COM 			} else {
67457836SJohn.Forte@Sun.COM 				stmf_do_task_abort(task);
67467836SJohn.Forte@Sun.COM 			}
67477836SJohn.Forte@Sun.COM 			break;
67487836SJohn.Forte@Sun.COM 		case ITASK_CMD_POLL_LU:
67497836SJohn.Forte@Sun.COM 			if (!wait_queue) {
67507836SJohn.Forte@Sun.COM 				lu->lu_task_poll(task);
67517836SJohn.Forte@Sun.COM 			}
67527836SJohn.Forte@Sun.COM 			break;
67537836SJohn.Forte@Sun.COM 		case ITASK_CMD_POLL_LPORT:
67547836SJohn.Forte@Sun.COM 			if (!wait_queue)
67557836SJohn.Forte@Sun.COM 				task->task_lport->lport_task_poll(task);
67567836SJohn.Forte@Sun.COM 			break;
67577836SJohn.Forte@Sun.COM 		case ITASK_CMD_SEND_STATUS:
67587836SJohn.Forte@Sun.COM 		/* case ITASK_CMD_XFER_DATA: */
67597836SJohn.Forte@Sun.COM 			break;
67607836SJohn.Forte@Sun.COM 		}
67617836SJohn.Forte@Sun.COM 		mutex_enter(&w->worker_lock);
67627836SJohn.Forte@Sun.COM 		if (dec_qdepth) {
67637836SJohn.Forte@Sun.COM 			w->worker_queue_depth--;
67647836SJohn.Forte@Sun.COM 		}
67657836SJohn.Forte@Sun.COM 	}
67667836SJohn.Forte@Sun.COM 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
67677836SJohn.Forte@Sun.COM 		if (w->worker_ref_count == 0)
67687836SJohn.Forte@Sun.COM 			goto stmf_worker_loop;
676911066Srafael.vanoni@sun.com 		else {
67707836SJohn.Forte@Sun.COM 			wait_timer = ddi_get_lbolt() + 1;
677111066Srafael.vanoni@sun.com 			wait_delta = 1;
677211066Srafael.vanoni@sun.com 		}
67737836SJohn.Forte@Sun.COM 	}
67747836SJohn.Forte@Sun.COM 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
67757836SJohn.Forte@Sun.COM 	if (wait_timer) {
677611773STim.Szeto@Sun.COM 		DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w);
677711066Srafael.vanoni@sun.com 		(void) cv_reltimedwait(&w->worker_cv, &w->worker_lock,
677811066Srafael.vanoni@sun.com 		    wait_delta, TR_CLOCK_TICK);
67797836SJohn.Forte@Sun.COM 	} else {
678011773STim.Szeto@Sun.COM 		DTRACE_PROBE1(worker__sleep, stmf_worker_t, w);
67817836SJohn.Forte@Sun.COM 		cv_wait(&w->worker_cv, &w->worker_lock);
67827836SJohn.Forte@Sun.COM 	}
678311773STim.Szeto@Sun.COM 	DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w);
67847836SJohn.Forte@Sun.COM 	w->worker_flags |= STMF_WORKER_ACTIVE;
67857836SJohn.Forte@Sun.COM 	goto stmf_worker_loop;
67867836SJohn.Forte@Sun.COM }
67877836SJohn.Forte@Sun.COM 
67887836SJohn.Forte@Sun.COM void
stmf_worker_mgmt()67897836SJohn.Forte@Sun.COM stmf_worker_mgmt()
67907836SJohn.Forte@Sun.COM {
67917836SJohn.Forte@Sun.COM 	int i;
67927836SJohn.Forte@Sun.COM 	int workers_needed;
67937836SJohn.Forte@Sun.COM 	uint32_t qd;
67947836SJohn.Forte@Sun.COM 	clock_t tps, d = 0;
67957836SJohn.Forte@Sun.COM 	uint32_t cur_max_ntasks = 0;
67967836SJohn.Forte@Sun.COM 	stmf_worker_t *w;
67977836SJohn.Forte@Sun.COM 
67987836SJohn.Forte@Sun.COM 	/* Check if we are trying to increase the # of threads */
67997836SJohn.Forte@Sun.COM 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
68007836SJohn.Forte@Sun.COM 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
68017836SJohn.Forte@Sun.COM 			stmf_nworkers_cur++;
68027836SJohn.Forte@Sun.COM 			stmf_nworkers_accepting_cmds++;
68037836SJohn.Forte@Sun.COM 		} else {
68047836SJohn.Forte@Sun.COM 			/* Wait for transition to complete */
68057836SJohn.Forte@Sun.COM 			return;
68067836SJohn.Forte@Sun.COM 		}
68077836SJohn.Forte@Sun.COM 	}
68087836SJohn.Forte@Sun.COM 	/* Check if we are trying to decrease the # of workers */
68097836SJohn.Forte@Sun.COM 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
68107836SJohn.Forte@Sun.COM 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
68117836SJohn.Forte@Sun.COM 			stmf_nworkers_cur--;
68127836SJohn.Forte@Sun.COM 			/*
68137836SJohn.Forte@Sun.COM 			 * stmf_nworkers_accepting_cmds has already been
68147836SJohn.Forte@Sun.COM 			 * updated by the request to reduce the # of workers.
68157836SJohn.Forte@Sun.COM 			 */
68167836SJohn.Forte@Sun.COM 		} else {
68177836SJohn.Forte@Sun.COM 			/* Wait for transition to complete */
68187836SJohn.Forte@Sun.COM 			return;
68197836SJohn.Forte@Sun.COM 		}
68207836SJohn.Forte@Sun.COM 	}
68217836SJohn.Forte@Sun.COM 	/* Check if we are being asked to quit */
68227836SJohn.Forte@Sun.COM 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
68237836SJohn.Forte@Sun.COM 		if (stmf_nworkers_cur) {
68247836SJohn.Forte@Sun.COM 			workers_needed = 0;
68257836SJohn.Forte@Sun.COM 			goto worker_mgmt_trigger_change;
68267836SJohn.Forte@Sun.COM 		}
68277836SJohn.Forte@Sun.COM 		return;
68287836SJohn.Forte@Sun.COM 	}
68297836SJohn.Forte@Sun.COM 	/* Check if we are starting */
68307836SJohn.Forte@Sun.COM 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
68317836SJohn.Forte@Sun.COM 		workers_needed = stmf_i_min_nworkers;
68327836SJohn.Forte@Sun.COM 		goto worker_mgmt_trigger_change;
68337836SJohn.Forte@Sun.COM 	}
68347836SJohn.Forte@Sun.COM 
68357836SJohn.Forte@Sun.COM 	tps = drv_usectohz(1 * 1000 * 1000);
68367836SJohn.Forte@Sun.COM 	if ((stmf_wm_last != 0) &&
68377836SJohn.Forte@Sun.COM 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
68387836SJohn.Forte@Sun.COM 		qd = 0;
68397836SJohn.Forte@Sun.COM 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
68407836SJohn.Forte@Sun.COM 			qd += stmf_workers[i].worker_max_qdepth_pu;
68417836SJohn.Forte@Sun.COM 			stmf_workers[i].worker_max_qdepth_pu = 0;
68427836SJohn.Forte@Sun.COM 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
68437836SJohn.Forte@Sun.COM 			    cur_max_ntasks) {
68447836SJohn.Forte@Sun.COM 				cur_max_ntasks =
68457836SJohn.Forte@Sun.COM 				    stmf_workers[i].worker_max_sys_qdepth_pu;
68467836SJohn.Forte@Sun.COM 			}
68477836SJohn.Forte@Sun.COM 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
68487836SJohn.Forte@Sun.COM 		}
68497836SJohn.Forte@Sun.COM 	}
68507836SJohn.Forte@Sun.COM 	stmf_wm_last = ddi_get_lbolt();
68517836SJohn.Forte@Sun.COM 	if (d <= tps) {
68527836SJohn.Forte@Sun.COM 		/* still ramping up */
68537836SJohn.Forte@Sun.COM 		return;
68547836SJohn.Forte@Sun.COM 	}
68557836SJohn.Forte@Sun.COM 	/* max qdepth cannot be more than max tasks */
68567836SJohn.Forte@Sun.COM 	if (qd > cur_max_ntasks)
68577836SJohn.Forte@Sun.COM 		qd = cur_max_ntasks;
68587836SJohn.Forte@Sun.COM 
68597836SJohn.Forte@Sun.COM 	/* See if we have more workers */
68607836SJohn.Forte@Sun.COM 	if (qd < stmf_nworkers_accepting_cmds) {
68617836SJohn.Forte@Sun.COM 		/*
68627836SJohn.Forte@Sun.COM 		 * Since we dont reduce the worker count right away, monitor
68637836SJohn.Forte@Sun.COM 		 * the highest load during the scale_down_delay.
68647836SJohn.Forte@Sun.COM 		 */
68657836SJohn.Forte@Sun.COM 		if (qd > stmf_worker_scale_down_qd)
68667836SJohn.Forte@Sun.COM 			stmf_worker_scale_down_qd = qd;
68677836SJohn.Forte@Sun.COM 		if (stmf_worker_scale_down_timer == 0) {
68687836SJohn.Forte@Sun.COM 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
68697836SJohn.Forte@Sun.COM 			    drv_usectohz(stmf_worker_scale_down_delay *
68707836SJohn.Forte@Sun.COM 			    1000 * 1000);
68717836SJohn.Forte@Sun.COM 			return;
68727836SJohn.Forte@Sun.COM 		}
68737836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
68747836SJohn.Forte@Sun.COM 			return;
68757836SJohn.Forte@Sun.COM 		}
68767836SJohn.Forte@Sun.COM 		/* Its time to reduce the workers */
68777836SJohn.Forte@Sun.COM 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
68787836SJohn.Forte@Sun.COM 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
68797836SJohn.Forte@Sun.COM 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
68807836SJohn.Forte@Sun.COM 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
68817836SJohn.Forte@Sun.COM 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
68827836SJohn.Forte@Sun.COM 			return;
68837836SJohn.Forte@Sun.COM 		workers_needed = stmf_worker_scale_down_qd;
68847836SJohn.Forte@Sun.COM 		stmf_worker_scale_down_qd = 0;
68857836SJohn.Forte@Sun.COM 		goto worker_mgmt_trigger_change;
68867836SJohn.Forte@Sun.COM 	}
68877836SJohn.Forte@Sun.COM 	stmf_worker_scale_down_qd = 0;
68887836SJohn.Forte@Sun.COM 	stmf_worker_scale_down_timer = 0;
68897836SJohn.Forte@Sun.COM 	if (qd > stmf_i_max_nworkers)
68907836SJohn.Forte@Sun.COM 		qd = stmf_i_max_nworkers;
68917836SJohn.Forte@Sun.COM 	if (qd < stmf_i_min_nworkers)
68927836SJohn.Forte@Sun.COM 		qd = stmf_i_min_nworkers;
68937836SJohn.Forte@Sun.COM 	if (qd == stmf_nworkers_cur)
68947836SJohn.Forte@Sun.COM 		return;
68957836SJohn.Forte@Sun.COM 	workers_needed = qd;
68967836SJohn.Forte@Sun.COM 	goto worker_mgmt_trigger_change;
68977836SJohn.Forte@Sun.COM 
68987836SJohn.Forte@Sun.COM 	/* NOTREACHED */
68997836SJohn.Forte@Sun.COM 	return;
69007836SJohn.Forte@Sun.COM 
69017836SJohn.Forte@Sun.COM worker_mgmt_trigger_change:
69027836SJohn.Forte@Sun.COM 	ASSERT(workers_needed != stmf_nworkers_cur);
69037836SJohn.Forte@Sun.COM 	if (workers_needed > stmf_nworkers_cur) {
69047836SJohn.Forte@Sun.COM 		stmf_nworkers_needed = workers_needed;
69057836SJohn.Forte@Sun.COM 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
69067836SJohn.Forte@Sun.COM 			w = &stmf_workers[i];
69077836SJohn.Forte@Sun.COM 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
69087836SJohn.Forte@Sun.COM 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
69097836SJohn.Forte@Sun.COM 			    minclsyspri);
69107836SJohn.Forte@Sun.COM 		}
69117836SJohn.Forte@Sun.COM 		return;
69127836SJohn.Forte@Sun.COM 	}
69137836SJohn.Forte@Sun.COM 	/* At this point we know that we are decreasing the # of workers */
69147836SJohn.Forte@Sun.COM 	stmf_nworkers_accepting_cmds = workers_needed;
69157836SJohn.Forte@Sun.COM 	stmf_nworkers_needed = workers_needed;
69167836SJohn.Forte@Sun.COM 	/* Signal the workers that its time to quit */
69177836SJohn.Forte@Sun.COM 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
69187836SJohn.Forte@Sun.COM 		w = &stmf_workers[i];
69197836SJohn.Forte@Sun.COM 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
69207836SJohn.Forte@Sun.COM 		mutex_enter(&w->worker_lock);
69217836SJohn.Forte@Sun.COM 		w->worker_flags |= STMF_WORKER_TERMINATE;
69227836SJohn.Forte@Sun.COM 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
69237836SJohn.Forte@Sun.COM 			cv_signal(&w->worker_cv);
69247836SJohn.Forte@Sun.COM 		mutex_exit(&w->worker_lock);
69257836SJohn.Forte@Sun.COM 	}
69267836SJohn.Forte@Sun.COM }
69277836SJohn.Forte@Sun.COM 
69287836SJohn.Forte@Sun.COM /*
69297836SJohn.Forte@Sun.COM  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
69307836SJohn.Forte@Sun.COM  * If all the data has been filled out, frees the xd and makes
69317836SJohn.Forte@Sun.COM  * db_lu_private NULL.
69327836SJohn.Forte@Sun.COM  */
69337836SJohn.Forte@Sun.COM void
stmf_xd_to_dbuf(stmf_data_buf_t * dbuf,int set_rel_off)693412340SJohn.Forte@Sun.COM stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off)
69357836SJohn.Forte@Sun.COM {
69367836SJohn.Forte@Sun.COM 	stmf_xfer_data_t *xd;
69377836SJohn.Forte@Sun.COM 	uint8_t *p;
69387836SJohn.Forte@Sun.COM 	int i;
69397836SJohn.Forte@Sun.COM 	uint32_t s;
69407836SJohn.Forte@Sun.COM 
69417836SJohn.Forte@Sun.COM 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
69427836SJohn.Forte@Sun.COM 	dbuf->db_data_size = 0;
694312340SJohn.Forte@Sun.COM 	if (set_rel_off)
694412340SJohn.Forte@Sun.COM 		dbuf->db_relative_offset = xd->size_done;
69457836SJohn.Forte@Sun.COM 	for (i = 0; i < dbuf->db_sglist_length; i++) {
69467836SJohn.Forte@Sun.COM 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
69477836SJohn.Forte@Sun.COM 		p = &xd->buf[xd->size_done];
69487836SJohn.Forte@Sun.COM 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
69497836SJohn.Forte@Sun.COM 		xd->size_left -= s;
69507836SJohn.Forte@Sun.COM 		xd->size_done += s;
69517836SJohn.Forte@Sun.COM 		dbuf->db_data_size += s;
69527836SJohn.Forte@Sun.COM 		if (xd->size_left == 0) {
69537836SJohn.Forte@Sun.COM 			kmem_free(xd, xd->alloc_size);
69547836SJohn.Forte@Sun.COM 			dbuf->db_lu_private = NULL;
69557836SJohn.Forte@Sun.COM 			return;
69567836SJohn.Forte@Sun.COM 		}
69577836SJohn.Forte@Sun.COM 	}
69587836SJohn.Forte@Sun.COM }
69597836SJohn.Forte@Sun.COM 
69607836SJohn.Forte@Sun.COM /* ARGSUSED */
69617836SJohn.Forte@Sun.COM stmf_status_t
stmf_dlun0_task_alloc(scsi_task_t * task)69627836SJohn.Forte@Sun.COM stmf_dlun0_task_alloc(scsi_task_t *task)
69637836SJohn.Forte@Sun.COM {
69647836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
69657836SJohn.Forte@Sun.COM }
69667836SJohn.Forte@Sun.COM 
69677836SJohn.Forte@Sun.COM void
stmf_dlun0_new_task(scsi_task_t * task,stmf_data_buf_t * dbuf)69687836SJohn.Forte@Sun.COM stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
69697836SJohn.Forte@Sun.COM {
69707836SJohn.Forte@Sun.COM 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
69717836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
69727836SJohn.Forte@Sun.COM 	uint32_t sz, minsz;
69737836SJohn.Forte@Sun.COM 	uint8_t *p;
69747836SJohn.Forte@Sun.COM 	stmf_xfer_data_t *xd;
69757836SJohn.Forte@Sun.COM 	uint8_t inq_page_length = 31;
69767836SJohn.Forte@Sun.COM 
69777836SJohn.Forte@Sun.COM 	if (task->task_mgmt_function) {
69787836SJohn.Forte@Sun.COM 		stmf_scsilib_handle_task_mgmt(task);
69797836SJohn.Forte@Sun.COM 		return;
69807836SJohn.Forte@Sun.COM 	}
69817836SJohn.Forte@Sun.COM 
69827836SJohn.Forte@Sun.COM 	switch (cdbp[0]) {
69837836SJohn.Forte@Sun.COM 	case SCMD_INQUIRY:
69847836SJohn.Forte@Sun.COM 		/*
69857836SJohn.Forte@Sun.COM 		 * Basic protocol checks.  In addition, only reply to
69867836SJohn.Forte@Sun.COM 		 * standard inquiry.  Otherwise, the LU provider needs
69877836SJohn.Forte@Sun.COM 		 * to respond.
69887836SJohn.Forte@Sun.COM 		 */
69897836SJohn.Forte@Sun.COM 
69907836SJohn.Forte@Sun.COM 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
69917836SJohn.Forte@Sun.COM 			stmf_scsilib_send_status(task, STATUS_CHECK,
69927836SJohn.Forte@Sun.COM 			    STMF_SAA_INVALID_FIELD_IN_CDB);
69937836SJohn.Forte@Sun.COM 			return;
69947836SJohn.Forte@Sun.COM 		}
69957836SJohn.Forte@Sun.COM 
69967836SJohn.Forte@Sun.COM 		task->task_cmd_xfer_length =
69977836SJohn.Forte@Sun.COM 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
69987836SJohn.Forte@Sun.COM 
69997836SJohn.Forte@Sun.COM 		if (task->task_additional_flags &
70007836SJohn.Forte@Sun.COM 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
70017836SJohn.Forte@Sun.COM 			task->task_expected_xfer_length =
70027836SJohn.Forte@Sun.COM 			    task->task_cmd_xfer_length;
70037836SJohn.Forte@Sun.COM 		}
70047836SJohn.Forte@Sun.COM 
70057836SJohn.Forte@Sun.COM 		sz = min(task->task_expected_xfer_length,
70067836SJohn.Forte@Sun.COM 		    min(36, task->task_cmd_xfer_length));
70077836SJohn.Forte@Sun.COM 		minsz = 36;
70087836SJohn.Forte@Sun.COM 
70097836SJohn.Forte@Sun.COM 		if (sz == 0) {
70107836SJohn.Forte@Sun.COM 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
70117836SJohn.Forte@Sun.COM 			return;
70127836SJohn.Forte@Sun.COM 		}
70137836SJohn.Forte@Sun.COM 
70147836SJohn.Forte@Sun.COM 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
70157836SJohn.Forte@Sun.COM 			/*
70167836SJohn.Forte@Sun.COM 			 * Ignore any preallocated dbuf if the size is less
70177836SJohn.Forte@Sun.COM 			 * than 36. It will be freed during the task_free.
70187836SJohn.Forte@Sun.COM 			 */
70197836SJohn.Forte@Sun.COM 			dbuf = NULL;
70207836SJohn.Forte@Sun.COM 		}
70217836SJohn.Forte@Sun.COM 		if (dbuf == NULL)
70227836SJohn.Forte@Sun.COM 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
70237836SJohn.Forte@Sun.COM 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
70247836SJohn.Forte@Sun.COM 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
70257836SJohn.Forte@Sun.COM 			    STMF_ALLOC_FAILURE, NULL);
70267836SJohn.Forte@Sun.COM 			return;
70277836SJohn.Forte@Sun.COM 		}
70287836SJohn.Forte@Sun.COM 		dbuf->db_lu_private = NULL;
70297836SJohn.Forte@Sun.COM 
70307836SJohn.Forte@Sun.COM 		p = dbuf->db_sglist[0].seg_addr;
70317836SJohn.Forte@Sun.COM 
70327836SJohn.Forte@Sun.COM 		/*
70337836SJohn.Forte@Sun.COM 		 * Standard inquiry handling only.
70347836SJohn.Forte@Sun.COM 		 */
70357836SJohn.Forte@Sun.COM 
70367836SJohn.Forte@Sun.COM 		bzero(p, inq_page_length + 5);
70377836SJohn.Forte@Sun.COM 
70387836SJohn.Forte@Sun.COM 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
70397836SJohn.Forte@Sun.COM 		p[2] = 5;
70407836SJohn.Forte@Sun.COM 		p[3] = 0x12;
70417836SJohn.Forte@Sun.COM 		p[4] = inq_page_length;
70427836SJohn.Forte@Sun.COM 		p[6] = 0x80;
70437836SJohn.Forte@Sun.COM 
70447836SJohn.Forte@Sun.COM 		(void) strncpy((char *)p+8, "SUN     ", 8);
70457836SJohn.Forte@Sun.COM 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
70467836SJohn.Forte@Sun.COM 		(void) strncpy((char *)p+32, "1.0 ", 4);
70477836SJohn.Forte@Sun.COM 
70487836SJohn.Forte@Sun.COM 		dbuf->db_data_size = sz;
70497836SJohn.Forte@Sun.COM 		dbuf->db_relative_offset = 0;
70507836SJohn.Forte@Sun.COM 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
70517836SJohn.Forte@Sun.COM 		(void) stmf_xfer_data(task, dbuf, 0);
70527836SJohn.Forte@Sun.COM 
70537836SJohn.Forte@Sun.COM 		return;
70547836SJohn.Forte@Sun.COM 
70557836SJohn.Forte@Sun.COM 	case SCMD_REPORT_LUNS:
70567836SJohn.Forte@Sun.COM 		task->task_cmd_xfer_length =
70577836SJohn.Forte@Sun.COM 		    ((((uint32_t)task->task_cdb[6]) << 24) |
70587836SJohn.Forte@Sun.COM 		    (((uint32_t)task->task_cdb[7]) << 16) |
70597836SJohn.Forte@Sun.COM 		    (((uint32_t)task->task_cdb[8]) << 8) |
70607836SJohn.Forte@Sun.COM 		    ((uint32_t)task->task_cdb[9]));
70617836SJohn.Forte@Sun.COM 
70627836SJohn.Forte@Sun.COM 		if (task->task_additional_flags &
70637836SJohn.Forte@Sun.COM 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
70647836SJohn.Forte@Sun.COM 			task->task_expected_xfer_length =
70657836SJohn.Forte@Sun.COM 			    task->task_cmd_xfer_length;
70667836SJohn.Forte@Sun.COM 		}
70677836SJohn.Forte@Sun.COM 
70687836SJohn.Forte@Sun.COM 		sz = min(task->task_expected_xfer_length,
70697836SJohn.Forte@Sun.COM 		    task->task_cmd_xfer_length);
70707836SJohn.Forte@Sun.COM 
70717836SJohn.Forte@Sun.COM 		if (sz < 16) {
70727836SJohn.Forte@Sun.COM 			stmf_scsilib_send_status(task, STATUS_CHECK,
70738662SJordan.Vaughan@Sun.com 			    STMF_SAA_INVALID_FIELD_IN_CDB);
70747836SJohn.Forte@Sun.COM 			return;
70757836SJohn.Forte@Sun.COM 		}
70767836SJohn.Forte@Sun.COM 
70777836SJohn.Forte@Sun.COM 		iss = (stmf_i_scsi_session_t *)
70787836SJohn.Forte@Sun.COM 		    task->task_session->ss_stmf_private;
70797836SJohn.Forte@Sun.COM 		rw_enter(iss->iss_lockp, RW_WRITER);
70807836SJohn.Forte@Sun.COM 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
70817836SJohn.Forte@Sun.COM 		rw_exit(iss->iss_lockp);
70827836SJohn.Forte@Sun.COM 
70837836SJohn.Forte@Sun.COM 		if (xd == NULL) {
70847836SJohn.Forte@Sun.COM 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
70857836SJohn.Forte@Sun.COM 			    STMF_ALLOC_FAILURE, NULL);
70867836SJohn.Forte@Sun.COM 			return;
70877836SJohn.Forte@Sun.COM 		}
70887836SJohn.Forte@Sun.COM 
70897836SJohn.Forte@Sun.COM 		sz = min(sz, xd->size_left);
70907836SJohn.Forte@Sun.COM 		xd->size_left = sz;
70917836SJohn.Forte@Sun.COM 		minsz = min(512, sz);
70927836SJohn.Forte@Sun.COM 
70937836SJohn.Forte@Sun.COM 		if (dbuf == NULL)
70947836SJohn.Forte@Sun.COM 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
70957836SJohn.Forte@Sun.COM 		if (dbuf == NULL) {
70967836SJohn.Forte@Sun.COM 			kmem_free(xd, xd->alloc_size);
70977836SJohn.Forte@Sun.COM 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
70987836SJohn.Forte@Sun.COM 			    STMF_ALLOC_FAILURE, NULL);
70997836SJohn.Forte@Sun.COM 			return;
71007836SJohn.Forte@Sun.COM 		}
71017836SJohn.Forte@Sun.COM 		dbuf->db_lu_private = xd;
710212340SJohn.Forte@Sun.COM 		stmf_xd_to_dbuf(dbuf, 1);
71037836SJohn.Forte@Sun.COM 
71047836SJohn.Forte@Sun.COM 		atomic_and_32(&iss->iss_flags,
71058662SJordan.Vaughan@Sun.com 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
71067836SJohn.Forte@Sun.COM 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
71077836SJohn.Forte@Sun.COM 		(void) stmf_xfer_data(task, dbuf, 0);
71087836SJohn.Forte@Sun.COM 		return;
71097836SJohn.Forte@Sun.COM 	}
71107836SJohn.Forte@Sun.COM 
71117836SJohn.Forte@Sun.COM 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
71127836SJohn.Forte@Sun.COM }
71137836SJohn.Forte@Sun.COM 
71147836SJohn.Forte@Sun.COM void
stmf_dlun0_dbuf_done(scsi_task_t * task,stmf_data_buf_t * dbuf)71157836SJohn.Forte@Sun.COM stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
71167836SJohn.Forte@Sun.COM {
711710725SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
711810725SJohn.Forte@Sun.COM 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
711910725SJohn.Forte@Sun.COM 
71207836SJohn.Forte@Sun.COM 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
71217836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
71227836SJohn.Forte@Sun.COM 		    dbuf->db_xfer_status, NULL);
71237836SJohn.Forte@Sun.COM 		return;
71247836SJohn.Forte@Sun.COM 	}
712512340SJohn.Forte@Sun.COM 	task->task_nbytes_transferred += dbuf->db_data_size;
71267836SJohn.Forte@Sun.COM 	if (dbuf->db_lu_private) {
71277836SJohn.Forte@Sun.COM 		/* There is more */
712812340SJohn.Forte@Sun.COM 		stmf_xd_to_dbuf(dbuf, 1);
71297836SJohn.Forte@Sun.COM 		(void) stmf_xfer_data(task, dbuf, 0);
71307836SJohn.Forte@Sun.COM 		return;
71317836SJohn.Forte@Sun.COM 	}
713212340SJohn.Forte@Sun.COM 
713312340SJohn.Forte@Sun.COM 	stmf_free_dbuf(task, dbuf);
713410725SJohn.Forte@Sun.COM 	/*
713510725SJohn.Forte@Sun.COM 	 * If this is a proxy task, it will need to be completed from the
713610725SJohn.Forte@Sun.COM 	 * proxy port provider. This message lets pppt know that the xfer
713710725SJohn.Forte@Sun.COM 	 * is complete. When we receive the status from pppt, we will
713810725SJohn.Forte@Sun.COM 	 * then relay that status back to the lport.
713910725SJohn.Forte@Sun.COM 	 */
714010725SJohn.Forte@Sun.COM 	if (itask->itask_flags & ITASK_PROXY_TASK) {
714110725SJohn.Forte@Sun.COM 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
714210725SJohn.Forte@Sun.COM 		stmf_status_t ic_ret = STMF_FAILURE;
714310725SJohn.Forte@Sun.COM 		uint64_t session_msg_id;
714410725SJohn.Forte@Sun.COM 		mutex_enter(&stmf_state.stmf_lock);
714510725SJohn.Forte@Sun.COM 		session_msg_id = stmf_proxy_msg_id++;
714610725SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
714710725SJohn.Forte@Sun.COM 		/* send xfer done status to pppt */
714810725SJohn.Forte@Sun.COM 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
714910725SJohn.Forte@Sun.COM 		    itask->itask_proxy_msg_id,
715010725SJohn.Forte@Sun.COM 		    task->task_session->ss_session_id,
715110725SJohn.Forte@Sun.COM 		    STMF_SUCCESS, session_msg_id);
715210725SJohn.Forte@Sun.COM 		if (ic_xfer_done_msg) {
715310725SJohn.Forte@Sun.COM 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
715410725SJohn.Forte@Sun.COM 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
715510725SJohn.Forte@Sun.COM 				cmn_err(CE_WARN, "unable to xmit session msg");
715610725SJohn.Forte@Sun.COM 			}
715710725SJohn.Forte@Sun.COM 		}
715810725SJohn.Forte@Sun.COM 		/* task will be completed from pppt */
715910725SJohn.Forte@Sun.COM 		return;
716010725SJohn.Forte@Sun.COM 	}
71617836SJohn.Forte@Sun.COM 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
71627836SJohn.Forte@Sun.COM }
71637836SJohn.Forte@Sun.COM 
71647836SJohn.Forte@Sun.COM /* ARGSUSED */
71657836SJohn.Forte@Sun.COM void
stmf_dlun0_status_done(scsi_task_t * task)71667836SJohn.Forte@Sun.COM stmf_dlun0_status_done(scsi_task_t *task)
71677836SJohn.Forte@Sun.COM {
71687836SJohn.Forte@Sun.COM }
71697836SJohn.Forte@Sun.COM 
71707836SJohn.Forte@Sun.COM /* ARGSUSED */
71717836SJohn.Forte@Sun.COM void
stmf_dlun0_task_free(scsi_task_t * task)71727836SJohn.Forte@Sun.COM stmf_dlun0_task_free(scsi_task_t *task)
71737836SJohn.Forte@Sun.COM {
71747836SJohn.Forte@Sun.COM }
71757836SJohn.Forte@Sun.COM 
71767836SJohn.Forte@Sun.COM /* ARGSUSED */
71777836SJohn.Forte@Sun.COM stmf_status_t
stmf_dlun0_abort(struct stmf_lu * lu,int abort_cmd,void * arg,uint32_t flags)71787836SJohn.Forte@Sun.COM stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
71797836SJohn.Forte@Sun.COM {
71807836SJohn.Forte@Sun.COM 	scsi_task_t *task = (scsi_task_t *)arg;
71817836SJohn.Forte@Sun.COM 	stmf_i_scsi_task_t *itask =
71828662SJordan.Vaughan@Sun.com 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
71837836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
71847836SJohn.Forte@Sun.COM 	int i;
71857836SJohn.Forte@Sun.COM 	uint8_t map;
71867836SJohn.Forte@Sun.COM 
71877836SJohn.Forte@Sun.COM 	if ((task->task_mgmt_function) && (itask->itask_flags &
71887836SJohn.Forte@Sun.COM 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
71897836SJohn.Forte@Sun.COM 		switch (task->task_mgmt_function) {
71907836SJohn.Forte@Sun.COM 		case TM_ABORT_TASK:
71917836SJohn.Forte@Sun.COM 		case TM_ABORT_TASK_SET:
71927836SJohn.Forte@Sun.COM 		case TM_CLEAR_TASK_SET:
71937836SJohn.Forte@Sun.COM 		case TM_LUN_RESET:
71947836SJohn.Forte@Sun.COM 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
71957836SJohn.Forte@Sun.COM 			break;
71967836SJohn.Forte@Sun.COM 		case TM_TARGET_RESET:
71977836SJohn.Forte@Sun.COM 		case TM_TARGET_COLD_RESET:
71987836SJohn.Forte@Sun.COM 		case TM_TARGET_WARM_RESET:
71997836SJohn.Forte@Sun.COM 			stmf_abort_target_reset(task);
72007836SJohn.Forte@Sun.COM 			break;
72017836SJohn.Forte@Sun.COM 		}
72027836SJohn.Forte@Sun.COM 		return (STMF_ABORT_SUCCESS);
72037836SJohn.Forte@Sun.COM 	}
72047836SJohn.Forte@Sun.COM 
72057836SJohn.Forte@Sun.COM 	/*
72067836SJohn.Forte@Sun.COM 	 * OK so its not a task mgmt. Make sure we free any xd sitting
72077836SJohn.Forte@Sun.COM 	 * inside any dbuf.
72087836SJohn.Forte@Sun.COM 	 */
72097836SJohn.Forte@Sun.COM 	if ((map = itask->itask_allocated_buf_map) != 0) {
72107836SJohn.Forte@Sun.COM 		for (i = 0; i < 4; i++) {
72117836SJohn.Forte@Sun.COM 			if ((map & 1) &&
72127836SJohn.Forte@Sun.COM 			    ((itask->itask_dbufs[i])->db_lu_private)) {
72137836SJohn.Forte@Sun.COM 				stmf_xfer_data_t *xd;
72147836SJohn.Forte@Sun.COM 				stmf_data_buf_t *dbuf;
72157836SJohn.Forte@Sun.COM 
72167836SJohn.Forte@Sun.COM 				dbuf = itask->itask_dbufs[i];
72177836SJohn.Forte@Sun.COM 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
72187836SJohn.Forte@Sun.COM 				dbuf->db_lu_private = NULL;
72197836SJohn.Forte@Sun.COM 				kmem_free(xd, xd->alloc_size);
72207836SJohn.Forte@Sun.COM 			}
72217836SJohn.Forte@Sun.COM 			map >>= 1;
72227836SJohn.Forte@Sun.COM 		}
72237836SJohn.Forte@Sun.COM 	}
72247836SJohn.Forte@Sun.COM 	return (STMF_ABORT_SUCCESS);
72257836SJohn.Forte@Sun.COM }
72267836SJohn.Forte@Sun.COM 
72277836SJohn.Forte@Sun.COM void
stmf_dlun0_task_poll(struct scsi_task * task)72287836SJohn.Forte@Sun.COM stmf_dlun0_task_poll(struct scsi_task *task)
72297836SJohn.Forte@Sun.COM {
72307836SJohn.Forte@Sun.COM 	/* Right now we only do this for handling task management functions */
72317836SJohn.Forte@Sun.COM 	ASSERT(task->task_mgmt_function);
72327836SJohn.Forte@Sun.COM 
72337836SJohn.Forte@Sun.COM 	switch (task->task_mgmt_function) {
72347836SJohn.Forte@Sun.COM 	case TM_ABORT_TASK:
72357836SJohn.Forte@Sun.COM 	case TM_ABORT_TASK_SET:
72367836SJohn.Forte@Sun.COM 	case TM_CLEAR_TASK_SET:
72377836SJohn.Forte@Sun.COM 	case TM_LUN_RESET:
72387836SJohn.Forte@Sun.COM 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
72397836SJohn.Forte@Sun.COM 		return;
72407836SJohn.Forte@Sun.COM 	case TM_TARGET_RESET:
72417836SJohn.Forte@Sun.COM 	case TM_TARGET_COLD_RESET:
72427836SJohn.Forte@Sun.COM 	case TM_TARGET_WARM_RESET:
72437836SJohn.Forte@Sun.COM 		stmf_target_reset_poll(task);
72447836SJohn.Forte@Sun.COM 		return;
72457836SJohn.Forte@Sun.COM 	}
72467836SJohn.Forte@Sun.COM }
72477836SJohn.Forte@Sun.COM 
72487836SJohn.Forte@Sun.COM /* ARGSUSED */
72497836SJohn.Forte@Sun.COM void
stmf_dlun0_ctl(struct stmf_lu * lu,int cmd,void * arg)72507836SJohn.Forte@Sun.COM stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
72517836SJohn.Forte@Sun.COM {
72527836SJohn.Forte@Sun.COM 	/* This function will never be called */
72537836SJohn.Forte@Sun.COM 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
72547836SJohn.Forte@Sun.COM }
72557836SJohn.Forte@Sun.COM 
72567836SJohn.Forte@Sun.COM void
stmf_dlun_init()72577836SJohn.Forte@Sun.COM stmf_dlun_init()
72587836SJohn.Forte@Sun.COM {
72597836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
72607836SJohn.Forte@Sun.COM 
72617836SJohn.Forte@Sun.COM 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
72627836SJohn.Forte@Sun.COM 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
72637836SJohn.Forte@Sun.COM 	dlun0->lu_new_task = stmf_dlun0_new_task;
72647836SJohn.Forte@Sun.COM 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
72657836SJohn.Forte@Sun.COM 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
72667836SJohn.Forte@Sun.COM 	dlun0->lu_task_free = stmf_dlun0_task_free;
72677836SJohn.Forte@Sun.COM 	dlun0->lu_abort = stmf_dlun0_abort;
72687836SJohn.Forte@Sun.COM 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
72697836SJohn.Forte@Sun.COM 	dlun0->lu_ctl = stmf_dlun0_ctl;
72707836SJohn.Forte@Sun.COM 
72717836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
72727836SJohn.Forte@Sun.COM 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
72737836SJohn.Forte@Sun.COM }
72747836SJohn.Forte@Sun.COM 
72757836SJohn.Forte@Sun.COM stmf_status_t
stmf_dlun_fini()72767836SJohn.Forte@Sun.COM stmf_dlun_fini()
72777836SJohn.Forte@Sun.COM {
72787836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
72797836SJohn.Forte@Sun.COM 
72807836SJohn.Forte@Sun.COM 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
72817836SJohn.Forte@Sun.COM 
72827836SJohn.Forte@Sun.COM 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
72837836SJohn.Forte@Sun.COM 	if (ilu->ilu_ntasks) {
72847836SJohn.Forte@Sun.COM 		stmf_i_scsi_task_t *itask, *nitask;
72857836SJohn.Forte@Sun.COM 
72867836SJohn.Forte@Sun.COM 		nitask = ilu->ilu_tasks;
72877836SJohn.Forte@Sun.COM 		do {
72887836SJohn.Forte@Sun.COM 			itask = nitask;
72897836SJohn.Forte@Sun.COM 			nitask = itask->itask_lu_next;
72907836SJohn.Forte@Sun.COM 			dlun0->lu_task_free(itask->itask_task);
72917836SJohn.Forte@Sun.COM 			stmf_free(itask->itask_task);
72927836SJohn.Forte@Sun.COM 		} while (nitask != NULL);
72937836SJohn.Forte@Sun.COM 
72947836SJohn.Forte@Sun.COM 	}
72957836SJohn.Forte@Sun.COM 	stmf_free(dlun0);
72967836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
72977836SJohn.Forte@Sun.COM }
72987836SJohn.Forte@Sun.COM 
72997836SJohn.Forte@Sun.COM void
stmf_abort_target_reset(scsi_task_t * task)73007836SJohn.Forte@Sun.COM stmf_abort_target_reset(scsi_task_t *task)
73017836SJohn.Forte@Sun.COM {
73027836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
73038662SJordan.Vaughan@Sun.com 	    task->task_session->ss_stmf_private;
73047836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
73057836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *lm_ent;
73067836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
73077836SJohn.Forte@Sun.COM 	int i;
73087836SJohn.Forte@Sun.COM 
73097836SJohn.Forte@Sun.COM 	rw_enter(iss->iss_lockp, RW_READER);
73107836SJohn.Forte@Sun.COM 	lm = iss->iss_sm;
73117836SJohn.Forte@Sun.COM 	for (i = 0; i < lm->lm_nentries; i++) {
73127836SJohn.Forte@Sun.COM 		if (lm->lm_plus[i] == NULL)
73137836SJohn.Forte@Sun.COM 			continue;
73147836SJohn.Forte@Sun.COM 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
73157836SJohn.Forte@Sun.COM 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
73167836SJohn.Forte@Sun.COM 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
73177836SJohn.Forte@Sun.COM 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
73187836SJohn.Forte@Sun.COM 		}
73197836SJohn.Forte@Sun.COM 	}
73207836SJohn.Forte@Sun.COM 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
73217836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
73227836SJohn.Forte@Sun.COM }
73237836SJohn.Forte@Sun.COM 
73247836SJohn.Forte@Sun.COM /*
73257836SJohn.Forte@Sun.COM  * The return value is only used by function managing target reset.
73267836SJohn.Forte@Sun.COM  */
73277836SJohn.Forte@Sun.COM stmf_status_t
stmf_lun_reset_poll(stmf_lu_t * lu,struct scsi_task * task,int target_reset)73287836SJohn.Forte@Sun.COM stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
73297836SJohn.Forte@Sun.COM {
73307836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
73317836SJohn.Forte@Sun.COM 	int ntasks_pending;
73327836SJohn.Forte@Sun.COM 
73337836SJohn.Forte@Sun.COM 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
73347836SJohn.Forte@Sun.COM 	/*
73357836SJohn.Forte@Sun.COM 	 * This function is also used during Target reset. The idea is that
73367836SJohn.Forte@Sun.COM 	 * once all the commands are aborted, call the LU's reset entry
73377836SJohn.Forte@Sun.COM 	 * point (abort entry point with a reset flag). But if this Task
73387836SJohn.Forte@Sun.COM 	 * mgmt is running on this LU then all the tasks cannot be aborted.
73397836SJohn.Forte@Sun.COM 	 * one task (this task) will still be running which is OK.
73407836SJohn.Forte@Sun.COM 	 */
73417836SJohn.Forte@Sun.COM 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
73427836SJohn.Forte@Sun.COM 	    (ntasks_pending == 1))) {
73437836SJohn.Forte@Sun.COM 		stmf_status_t ret;
73447836SJohn.Forte@Sun.COM 
73457836SJohn.Forte@Sun.COM 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
73467836SJohn.Forte@Sun.COM 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
73477836SJohn.Forte@Sun.COM 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
73487836SJohn.Forte@Sun.COM 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
73497836SJohn.Forte@Sun.COM 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
73507836SJohn.Forte@Sun.COM 		} else {
73517836SJohn.Forte@Sun.COM 			ret = STMF_SUCCESS;
73527836SJohn.Forte@Sun.COM 		}
73537836SJohn.Forte@Sun.COM 		if (ret == STMF_SUCCESS) {
73547836SJohn.Forte@Sun.COM 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
73557836SJohn.Forte@Sun.COM 		}
73567836SJohn.Forte@Sun.COM 		if (target_reset) {
73577836SJohn.Forte@Sun.COM 			return (ret);
73587836SJohn.Forte@Sun.COM 		}
73597836SJohn.Forte@Sun.COM 		if (ret == STMF_SUCCESS) {
73607836SJohn.Forte@Sun.COM 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
73617836SJohn.Forte@Sun.COM 			return (ret);
73627836SJohn.Forte@Sun.COM 		}
73637836SJohn.Forte@Sun.COM 		if (ret != STMF_BUSY) {
73647836SJohn.Forte@Sun.COM 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
73657836SJohn.Forte@Sun.COM 			return (ret);
73667836SJohn.Forte@Sun.COM 		}
73677836SJohn.Forte@Sun.COM 	}
73687836SJohn.Forte@Sun.COM 
73697836SJohn.Forte@Sun.COM 	if (target_reset) {
73707836SJohn.Forte@Sun.COM 		/* Tell target reset polling code that we are not done */
73717836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
73727836SJohn.Forte@Sun.COM 	}
73737836SJohn.Forte@Sun.COM 
73747836SJohn.Forte@Sun.COM 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
73757836SJohn.Forte@Sun.COM 	    != STMF_SUCCESS) {
73767836SJohn.Forte@Sun.COM 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
73777836SJohn.Forte@Sun.COM 		    STMF_ALLOC_FAILURE, NULL);
73787836SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
73797836SJohn.Forte@Sun.COM 	}
73807836SJohn.Forte@Sun.COM 
73817836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
73827836SJohn.Forte@Sun.COM }
73837836SJohn.Forte@Sun.COM 
73847836SJohn.Forte@Sun.COM void
stmf_target_reset_poll(struct scsi_task * task)73857836SJohn.Forte@Sun.COM stmf_target_reset_poll(struct scsi_task *task)
73867836SJohn.Forte@Sun.COM {
73877836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
73888662SJordan.Vaughan@Sun.com 	    task->task_session->ss_stmf_private;
73897836SJohn.Forte@Sun.COM 	stmf_lun_map_t *lm;
73907836SJohn.Forte@Sun.COM 	stmf_lun_map_ent_t *lm_ent;
73917836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
73927836SJohn.Forte@Sun.COM 	stmf_status_t ret;
73937836SJohn.Forte@Sun.COM 	int i;
73947836SJohn.Forte@Sun.COM 	int not_done = 0;
73957836SJohn.Forte@Sun.COM 
73967836SJohn.Forte@Sun.COM 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
73977836SJohn.Forte@Sun.COM 
73987836SJohn.Forte@Sun.COM 	rw_enter(iss->iss_lockp, RW_READER);
73997836SJohn.Forte@Sun.COM 	lm = iss->iss_sm;
74007836SJohn.Forte@Sun.COM 	for (i = 0; i < lm->lm_nentries; i++) {
74017836SJohn.Forte@Sun.COM 		if (lm->lm_plus[i] == NULL)
74027836SJohn.Forte@Sun.COM 			continue;
74037836SJohn.Forte@Sun.COM 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
74047836SJohn.Forte@Sun.COM 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
74057836SJohn.Forte@Sun.COM 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
74067836SJohn.Forte@Sun.COM 			rw_exit(iss->iss_lockp);
74077836SJohn.Forte@Sun.COM 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
74087836SJohn.Forte@Sun.COM 			rw_enter(iss->iss_lockp, RW_READER);
74097836SJohn.Forte@Sun.COM 			if (ret == STMF_SUCCESS)
74107836SJohn.Forte@Sun.COM 				continue;
74117836SJohn.Forte@Sun.COM 			not_done = 1;
74127836SJohn.Forte@Sun.COM 			if (ret != STMF_BUSY) {
74137836SJohn.Forte@Sun.COM 				rw_exit(iss->iss_lockp);
74147836SJohn.Forte@Sun.COM 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
74157836SJohn.Forte@Sun.COM 				    STMF_ABORTED, NULL);
74167836SJohn.Forte@Sun.COM 				return;
74177836SJohn.Forte@Sun.COM 			}
74187836SJohn.Forte@Sun.COM 		}
74197836SJohn.Forte@Sun.COM 	}
74207836SJohn.Forte@Sun.COM 	rw_exit(iss->iss_lockp);
74217836SJohn.Forte@Sun.COM 
74227836SJohn.Forte@Sun.COM 	if (not_done) {
74237836SJohn.Forte@Sun.COM 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
74247836SJohn.Forte@Sun.COM 		    != STMF_SUCCESS) {
74257836SJohn.Forte@Sun.COM 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
74267836SJohn.Forte@Sun.COM 			    STMF_ALLOC_FAILURE, NULL);
74277836SJohn.Forte@Sun.COM 			return;
74287836SJohn.Forte@Sun.COM 		}
74297836SJohn.Forte@Sun.COM 		return;
74307836SJohn.Forte@Sun.COM 	}
74317836SJohn.Forte@Sun.COM 
74327836SJohn.Forte@Sun.COM 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
74337836SJohn.Forte@Sun.COM 
74347836SJohn.Forte@Sun.COM 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
74357836SJohn.Forte@Sun.COM }
74367836SJohn.Forte@Sun.COM 
74377836SJohn.Forte@Sun.COM stmf_status_t
stmf_lu_add_event(stmf_lu_t * lu,int eventid)74387836SJohn.Forte@Sun.COM stmf_lu_add_event(stmf_lu_t *lu, int eventid)
74397836SJohn.Forte@Sun.COM {
74407836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
74417836SJohn.Forte@Sun.COM 
74427836SJohn.Forte@Sun.COM 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
74437836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
74447836SJohn.Forte@Sun.COM 	}
74457836SJohn.Forte@Sun.COM 
74467836SJohn.Forte@Sun.COM 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
74477836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
74487836SJohn.Forte@Sun.COM }
74497836SJohn.Forte@Sun.COM 
74507836SJohn.Forte@Sun.COM stmf_status_t
stmf_lu_remove_event(stmf_lu_t * lu,int eventid)74517836SJohn.Forte@Sun.COM stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
74527836SJohn.Forte@Sun.COM {
74537836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
74547836SJohn.Forte@Sun.COM 
74557836SJohn.Forte@Sun.COM 	if (eventid == STMF_EVENT_ALL) {
74567836SJohn.Forte@Sun.COM 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
74577836SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
74587836SJohn.Forte@Sun.COM 	}
74597836SJohn.Forte@Sun.COM 
74607836SJohn.Forte@Sun.COM 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
74617836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
74627836SJohn.Forte@Sun.COM 	}
74637836SJohn.Forte@Sun.COM 
74647836SJohn.Forte@Sun.COM 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
74657836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
74667836SJohn.Forte@Sun.COM }
74677836SJohn.Forte@Sun.COM 
74687836SJohn.Forte@Sun.COM stmf_status_t
stmf_lport_add_event(stmf_local_port_t * lport,int eventid)74697836SJohn.Forte@Sun.COM stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
74707836SJohn.Forte@Sun.COM {
74717836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport =
74728662SJordan.Vaughan@Sun.com 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
74737836SJohn.Forte@Sun.COM 
74747836SJohn.Forte@Sun.COM 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
74757836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
74767836SJohn.Forte@Sun.COM 	}
74777836SJohn.Forte@Sun.COM 
74787836SJohn.Forte@Sun.COM 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
74797836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
74807836SJohn.Forte@Sun.COM }
74817836SJohn.Forte@Sun.COM 
74827836SJohn.Forte@Sun.COM stmf_status_t
stmf_lport_remove_event(stmf_local_port_t * lport,int eventid)74837836SJohn.Forte@Sun.COM stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
74847836SJohn.Forte@Sun.COM {
74857836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport =
74868662SJordan.Vaughan@Sun.com 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
74877836SJohn.Forte@Sun.COM 
74887836SJohn.Forte@Sun.COM 	if (eventid == STMF_EVENT_ALL) {
74897836SJohn.Forte@Sun.COM 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
74907836SJohn.Forte@Sun.COM 		return (STMF_SUCCESS);
74917836SJohn.Forte@Sun.COM 	}
74927836SJohn.Forte@Sun.COM 
74937836SJohn.Forte@Sun.COM 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
74947836SJohn.Forte@Sun.COM 		return (STMF_INVALID_ARG);
74957836SJohn.Forte@Sun.COM 	}
74967836SJohn.Forte@Sun.COM 
74977836SJohn.Forte@Sun.COM 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
74987836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
74997836SJohn.Forte@Sun.COM }
75007836SJohn.Forte@Sun.COM 
75017836SJohn.Forte@Sun.COM void
stmf_generate_lu_event(stmf_i_lu_t * ilu,int eventid,void * arg,uint32_t flags)75027836SJohn.Forte@Sun.COM stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
75037836SJohn.Forte@Sun.COM {
75047836SJohn.Forte@Sun.COM 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
75057836SJohn.Forte@Sun.COM 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
75067836SJohn.Forte@Sun.COM 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
75077836SJohn.Forte@Sun.COM 	}
75087836SJohn.Forte@Sun.COM }
75097836SJohn.Forte@Sun.COM 
75107836SJohn.Forte@Sun.COM void
stmf_generate_lport_event(stmf_i_local_port_t * ilport,int eventid,void * arg,uint32_t flags)75117836SJohn.Forte@Sun.COM stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
75127836SJohn.Forte@Sun.COM 				uint32_t flags)
75137836SJohn.Forte@Sun.COM {
75147836SJohn.Forte@Sun.COM 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
75157836SJohn.Forte@Sun.COM 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
75167836SJohn.Forte@Sun.COM 		ilport->ilport_lport->lport_event_handler(
75177836SJohn.Forte@Sun.COM 		    ilport->ilport_lport, eventid, arg, flags);
75187836SJohn.Forte@Sun.COM 	}
75197836SJohn.Forte@Sun.COM }
75207836SJohn.Forte@Sun.COM 
752111773STim.Szeto@Sun.COM /*
752211773STim.Szeto@Sun.COM  * With the possibility of having multiple itl sessions pointing to the
752311773STim.Szeto@Sun.COM  * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize
752411773STim.Szeto@Sun.COM  * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer
752511773STim.Szeto@Sun.COM  * statistics.
752611773STim.Szeto@Sun.COM  */
752711773STim.Szeto@Sun.COM void
stmf_itl_task_start(stmf_i_scsi_task_t * itask)752811773STim.Szeto@Sun.COM stmf_itl_task_start(stmf_i_scsi_task_t *itask)
752911773STim.Szeto@Sun.COM {
753011773STim.Szeto@Sun.COM 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
753111773STim.Szeto@Sun.COM 	scsi_task_t	*task = itask->itask_task;
753211773STim.Szeto@Sun.COM 	stmf_i_lu_t	*ilu;
753311773STim.Szeto@Sun.COM 
753411773STim.Szeto@Sun.COM 	if (itl == NULL || task->task_lu == dlun0)
753511773STim.Szeto@Sun.COM 		return;
753611773STim.Szeto@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
753711773STim.Szeto@Sun.COM 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
753811773STim.Szeto@Sun.COM 	itask->itask_start_timestamp = gethrtime();
753911773STim.Szeto@Sun.COM 	kstat_waitq_enter(KSTAT_IO_PTR(itl->itl_kstat_taskq));
754011773STim.Szeto@Sun.COM 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter);
754111773STim.Szeto@Sun.COM 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
754211773STim.Szeto@Sun.COM 
754311773STim.Szeto@Sun.COM 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter);
754411773STim.Szeto@Sun.COM }
754511773STim.Szeto@Sun.COM 
754611773STim.Szeto@Sun.COM void
stmf_itl_lu_new_task(stmf_i_scsi_task_t * itask)754711773STim.Szeto@Sun.COM stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask)
754811773STim.Szeto@Sun.COM {
754911773STim.Szeto@Sun.COM 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
755011773STim.Szeto@Sun.COM 	scsi_task_t	*task = itask->itask_task;
755111773STim.Szeto@Sun.COM 	stmf_i_lu_t	*ilu;
755211773STim.Szeto@Sun.COM 
755311773STim.Szeto@Sun.COM 	if (itl == NULL || task->task_lu == dlun0)
755411773STim.Szeto@Sun.COM 		return;
755511773STim.Szeto@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
755611773STim.Szeto@Sun.COM 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
755711773STim.Szeto@Sun.COM 	kstat_waitq_to_runq(KSTAT_IO_PTR(itl->itl_kstat_taskq));
755811773STim.Szeto@Sun.COM 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq);
755911773STim.Szeto@Sun.COM 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
756011773STim.Szeto@Sun.COM 
756111773STim.Szeto@Sun.COM 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq);
756211773STim.Szeto@Sun.COM }
756311773STim.Szeto@Sun.COM 
756411773STim.Szeto@Sun.COM void
stmf_itl_task_done(stmf_i_scsi_task_t * itask)756511773STim.Szeto@Sun.COM stmf_itl_task_done(stmf_i_scsi_task_t *itask)
756611773STim.Szeto@Sun.COM {
756711773STim.Szeto@Sun.COM 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
756811773STim.Szeto@Sun.COM 	scsi_task_t		*task = itask->itask_task;
756911773STim.Szeto@Sun.COM 	kstat_io_t		*kip;
757011773STim.Szeto@Sun.COM 	hrtime_t		elapsed_time;
757111773STim.Szeto@Sun.COM 	stmf_kstat_itl_info_t	*itli;
757211773STim.Szeto@Sun.COM 	stmf_i_lu_t	*ilu;
757311773STim.Szeto@Sun.COM 
757411773STim.Szeto@Sun.COM 	if (itl == NULL || task->task_lu == dlun0)
757511773STim.Szeto@Sun.COM 		return;
757611773STim.Szeto@Sun.COM 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
757711773STim.Szeto@Sun.COM 
757811773STim.Szeto@Sun.COM 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
757911773STim.Szeto@Sun.COM 	itli = (stmf_kstat_itl_info_t *)KSTAT_NAMED_PTR(itl->itl_kstat_info);
758011773STim.Szeto@Sun.COM 	kip = KSTAT_IO_PTR(itl->itl_kstat_taskq);
758111773STim.Szeto@Sun.COM 
758211773STim.Szeto@Sun.COM 	itli->i_task_waitq_elapsed.value.ui64 += itask->itask_waitq_time;
758311773STim.Szeto@Sun.COM 
758411773STim.Szeto@Sun.COM 	itask->itask_done_timestamp = gethrtime();
758511773STim.Szeto@Sun.COM 	elapsed_time =
758611773STim.Szeto@Sun.COM 	    itask->itask_done_timestamp - itask->itask_start_timestamp;
758711773STim.Szeto@Sun.COM 
758811773STim.Szeto@Sun.COM 	if (task->task_flags & TF_READ_DATA) {
758911773STim.Szeto@Sun.COM 		kip->reads++;
759011773STim.Szeto@Sun.COM 		kip->nread += itask->itask_read_xfer;
759111773STim.Szeto@Sun.COM 		itli->i_task_read_elapsed.value.ui64 += elapsed_time;
759211773STim.Szeto@Sun.COM 		itli->i_lu_read_elapsed.value.ui64 +=
759311773STim.Szeto@Sun.COM 		    itask->itask_lu_read_time;
759411773STim.Szeto@Sun.COM 		itli->i_lport_read_elapsed.value.ui64 +=
759511773STim.Szeto@Sun.COM 		    itask->itask_lport_read_time;
759611773STim.Szeto@Sun.COM 	}
759711773STim.Szeto@Sun.COM 
759811773STim.Szeto@Sun.COM 	if (task->task_flags & TF_WRITE_DATA) {
759911773STim.Szeto@Sun.COM 		kip->writes++;
760011773STim.Szeto@Sun.COM 		kip->nwritten += itask->itask_write_xfer;
760111773STim.Szeto@Sun.COM 		itli->i_task_write_elapsed.value.ui64 += elapsed_time;
760211773STim.Szeto@Sun.COM 		itli->i_lu_write_elapsed.value.ui64 +=
760311773STim.Szeto@Sun.COM 		    itask->itask_lu_write_time;
760411773STim.Szeto@Sun.COM 		itli->i_lport_write_elapsed.value.ui64 +=
760511773STim.Szeto@Sun.COM 		    itask->itask_lport_write_time;
760611773STim.Szeto@Sun.COM 	}
760711773STim.Szeto@Sun.COM 
760811773STim.Szeto@Sun.COM 	if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) {
760911773STim.Szeto@Sun.COM 		kstat_runq_exit(kip);
761011773STim.Szeto@Sun.COM 		stmf_update_kstat_lu_q(task, kstat_runq_exit);
761111773STim.Szeto@Sun.COM 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
761211773STim.Szeto@Sun.COM 		stmf_update_kstat_lport_q(task, kstat_runq_exit);
761311773STim.Szeto@Sun.COM 	} else {
761411773STim.Szeto@Sun.COM 		kstat_waitq_exit(kip);
761511773STim.Szeto@Sun.COM 		stmf_update_kstat_lu_q(task, kstat_waitq_exit);
761611773STim.Szeto@Sun.COM 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
761711773STim.Szeto@Sun.COM 		stmf_update_kstat_lport_q(task, kstat_waitq_exit);
761811773STim.Szeto@Sun.COM 	}
761911773STim.Szeto@Sun.COM }
762011773STim.Szeto@Sun.COM 
762111773STim.Szeto@Sun.COM void
stmf_lu_xfer_start(scsi_task_t * task)762211773STim.Szeto@Sun.COM stmf_lu_xfer_start(scsi_task_t *task)
762311773STim.Szeto@Sun.COM {
762411773STim.Szeto@Sun.COM 	stmf_i_scsi_task_t *itask = task->task_stmf_private;
762511773STim.Szeto@Sun.COM 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
762611773STim.Szeto@Sun.COM 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
762711773STim.Szeto@Sun.COM 	kstat_io_t		*kip;
762811773STim.Szeto@Sun.COM 
762911773STim.Szeto@Sun.COM 	if (itl == NULL || task->task_lu == dlun0)
763011773STim.Szeto@Sun.COM 		return;
763111773STim.Szeto@Sun.COM 
763211773STim.Szeto@Sun.COM 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
763311773STim.Szeto@Sun.COM 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
763411773STim.Szeto@Sun.COM 	kstat_runq_enter(kip);
763511773STim.Szeto@Sun.COM 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
763611773STim.Szeto@Sun.COM }
763711773STim.Szeto@Sun.COM 
763811773STim.Szeto@Sun.COM void
stmf_lu_xfer_done(scsi_task_t * task,boolean_t read,uint64_t xfer_bytes,hrtime_t elapsed_time)763911773STim.Szeto@Sun.COM stmf_lu_xfer_done(scsi_task_t *task, boolean_t read, uint64_t xfer_bytes,
764011773STim.Szeto@Sun.COM     hrtime_t elapsed_time)
764111773STim.Szeto@Sun.COM {
764211773STim.Szeto@Sun.COM 	stmf_i_scsi_task_t	*itask = task->task_stmf_private;
764311773STim.Szeto@Sun.COM 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
764411773STim.Szeto@Sun.COM 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
764511773STim.Szeto@Sun.COM 	kstat_io_t		*kip;
764611773STim.Szeto@Sun.COM 
764711773STim.Szeto@Sun.COM 	if (itl == NULL || task->task_lu == dlun0)
764811773STim.Szeto@Sun.COM 		return;
764911773STim.Szeto@Sun.COM 
765011773STim.Szeto@Sun.COM 	if (read) {
765111773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_lu_read_time,
765211773STim.Szeto@Sun.COM 		    elapsed_time);
765311773STim.Szeto@Sun.COM 	} else {
765411773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_lu_write_time,
765511773STim.Szeto@Sun.COM 		    elapsed_time);
765611773STim.Szeto@Sun.COM 	}
765711773STim.Szeto@Sun.COM 
765811773STim.Szeto@Sun.COM 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
765911773STim.Szeto@Sun.COM 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
766011773STim.Szeto@Sun.COM 	kstat_runq_exit(kip);
766111773STim.Szeto@Sun.COM 	if (read) {
766211773STim.Szeto@Sun.COM 		kip->reads++;
766311773STim.Szeto@Sun.COM 		kip->nread += xfer_bytes;
766411773STim.Szeto@Sun.COM 	} else {
766511773STim.Szeto@Sun.COM 		kip->writes++;
766611773STim.Szeto@Sun.COM 		kip->nwritten += xfer_bytes;
766711773STim.Szeto@Sun.COM 	}
766811773STim.Szeto@Sun.COM 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
766911773STim.Szeto@Sun.COM }
767011773STim.Szeto@Sun.COM 
767111773STim.Szeto@Sun.COM static void
stmf_lport_xfer_start(stmf_i_scsi_task_t * itask,stmf_data_buf_t * dbuf)767211773STim.Szeto@Sun.COM stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
767311773STim.Szeto@Sun.COM {
767411773STim.Szeto@Sun.COM 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
767511773STim.Szeto@Sun.COM 
767611773STim.Szeto@Sun.COM 	if (itl == NULL)
767711773STim.Szeto@Sun.COM 		return;
767811773STim.Szeto@Sun.COM 
767911773STim.Szeto@Sun.COM 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task,
768011773STim.Szeto@Sun.COM 	    stmf_data_buf_t *, dbuf);
768111773STim.Szeto@Sun.COM 
768211773STim.Szeto@Sun.COM 	dbuf->db_xfer_start_timestamp = gethrtime();
768311773STim.Szeto@Sun.COM }
768411773STim.Szeto@Sun.COM 
768511773STim.Szeto@Sun.COM static void
stmf_lport_xfer_done(stmf_i_scsi_task_t * itask,stmf_data_buf_t * dbuf)768611773STim.Szeto@Sun.COM stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
768711773STim.Szeto@Sun.COM {
768811773STim.Szeto@Sun.COM 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
768911773STim.Szeto@Sun.COM 	scsi_task_t		*task;
769011773STim.Szeto@Sun.COM 	stmf_i_local_port_t	*ilp;
769111773STim.Szeto@Sun.COM 	kstat_io_t		*kip;
769211773STim.Szeto@Sun.COM 	hrtime_t		elapsed_time;
769311773STim.Szeto@Sun.COM 	uint64_t		xfer_size;
769411773STim.Szeto@Sun.COM 
769511773STim.Szeto@Sun.COM 	if (itl == NULL)
769611773STim.Szeto@Sun.COM 		return;
769711773STim.Szeto@Sun.COM 
769811773STim.Szeto@Sun.COM 	task = (scsi_task_t *)itask->itask_task;
769911773STim.Szeto@Sun.COM 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
770011773STim.Szeto@Sun.COM 	xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ?
770111773STim.Szeto@Sun.COM 	    dbuf->db_data_size : 0;
770211773STim.Szeto@Sun.COM 
770311773STim.Szeto@Sun.COM 	elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp;
770411773STim.Szeto@Sun.COM 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
770511773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_lport_read_time,
770611773STim.Szeto@Sun.COM 		    elapsed_time);
770711773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_read_xfer,
770811773STim.Szeto@Sun.COM 		    xfer_size);
770911773STim.Szeto@Sun.COM 	} else {
771011773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_lport_write_time,
771111773STim.Szeto@Sun.COM 		    elapsed_time);
771211773STim.Szeto@Sun.COM 		atomic_add_64((uint64_t *)&itask->itask_write_xfer,
771311773STim.Szeto@Sun.COM 		    xfer_size);
771411773STim.Szeto@Sun.COM 	}
771511773STim.Szeto@Sun.COM 
771611773STim.Szeto@Sun.COM 	DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task,
771711773STim.Szeto@Sun.COM 	    stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time);
771811773STim.Szeto@Sun.COM 
771911773STim.Szeto@Sun.COM 	kip = KSTAT_IO_PTR(itl->itl_kstat_lport_xfer);
772011773STim.Szeto@Sun.COM 	mutex_enter(ilp->ilport_kstat_io->ks_lock);
772111773STim.Szeto@Sun.COM 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
772211773STim.Szeto@Sun.COM 		kip->reads++;
772311773STim.Szeto@Sun.COM 		kip->nread += xfer_size;
772411773STim.Szeto@Sun.COM 	} else {
772511773STim.Szeto@Sun.COM 		kip->writes++;
772611773STim.Szeto@Sun.COM 		kip->nwritten += xfer_size;
772711773STim.Szeto@Sun.COM 	}
772811773STim.Szeto@Sun.COM 	mutex_exit(ilp->ilport_kstat_io->ks_lock);
772911773STim.Szeto@Sun.COM 
773011773STim.Szeto@Sun.COM 	dbuf->db_xfer_start_timestamp = 0;
773111773STim.Szeto@Sun.COM }
773211773STim.Szeto@Sun.COM 
77337836SJohn.Forte@Sun.COM void
stmf_svc_init()77347836SJohn.Forte@Sun.COM stmf_svc_init()
77357836SJohn.Forte@Sun.COM {
77367836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
77377836SJohn.Forte@Sun.COM 		return;
77387836SJohn.Forte@Sun.COM 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
77397836SJohn.Forte@Sun.COM 	    TASKQ_DEFAULTPRI, 0);
77407836SJohn.Forte@Sun.COM 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
77417836SJohn.Forte@Sun.COM 	    stmf_svc, 0, DDI_SLEEP);
77427836SJohn.Forte@Sun.COM }
77437836SJohn.Forte@Sun.COM 
77447836SJohn.Forte@Sun.COM stmf_status_t
stmf_svc_fini()77457836SJohn.Forte@Sun.COM stmf_svc_fini()
77467836SJohn.Forte@Sun.COM {
77477836SJohn.Forte@Sun.COM 	uint32_t i;
77487836SJohn.Forte@Sun.COM 
77497836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
77507836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
77517836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
77527836SJohn.Forte@Sun.COM 		cv_signal(&stmf_state.stmf_cv);
77537836SJohn.Forte@Sun.COM 	}
77547836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
77557836SJohn.Forte@Sun.COM 
77567836SJohn.Forte@Sun.COM 	/* Wait for 5 seconds */
77577836SJohn.Forte@Sun.COM 	for (i = 0; i < 500; i++) {
77587836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
77597836SJohn.Forte@Sun.COM 			delay(drv_usectohz(10000));
77607836SJohn.Forte@Sun.COM 		else
77617836SJohn.Forte@Sun.COM 			break;
77627836SJohn.Forte@Sun.COM 	}
77637836SJohn.Forte@Sun.COM 	if (i == 500)
77647836SJohn.Forte@Sun.COM 		return (STMF_BUSY);
77657836SJohn.Forte@Sun.COM 
77667836SJohn.Forte@Sun.COM 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
77677836SJohn.Forte@Sun.COM 
77687836SJohn.Forte@Sun.COM 	return (STMF_SUCCESS);
77697836SJohn.Forte@Sun.COM }
77707836SJohn.Forte@Sun.COM 
77717836SJohn.Forte@Sun.COM /* ARGSUSED */
77727836SJohn.Forte@Sun.COM void
stmf_svc(void * arg)77737836SJohn.Forte@Sun.COM stmf_svc(void *arg)
77747836SJohn.Forte@Sun.COM {
77757836SJohn.Forte@Sun.COM 	stmf_svc_req_t *req, **preq;
77767836SJohn.Forte@Sun.COM 	clock_t td;
77777836SJohn.Forte@Sun.COM 	clock_t	drain_start, drain_next = 0;
77787836SJohn.Forte@Sun.COM 	clock_t	timing_start, timing_next = 0;
77797836SJohn.Forte@Sun.COM 	clock_t worker_delay = 0;
77807836SJohn.Forte@Sun.COM 	int deq;
77817836SJohn.Forte@Sun.COM 	stmf_lu_t *lu;
77827836SJohn.Forte@Sun.COM 	stmf_i_lu_t *ilu;
77837836SJohn.Forte@Sun.COM 	stmf_local_port_t *lport;
77847836SJohn.Forte@Sun.COM 	stmf_i_local_port_t *ilport, *next_ilport;
77857836SJohn.Forte@Sun.COM 	stmf_i_scsi_session_t *iss;
77867836SJohn.Forte@Sun.COM 
77877836SJohn.Forte@Sun.COM 	td = drv_usectohz(20000);
77887836SJohn.Forte@Sun.COM 
77897836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
77907836SJohn.Forte@Sun.COM 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
77917836SJohn.Forte@Sun.COM 
77927836SJohn.Forte@Sun.COM stmf_svc_loop:
77937836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
77947836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_flags &=
77957836SJohn.Forte@Sun.COM 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
77967836SJohn.Forte@Sun.COM 		mutex_exit(&stmf_state.stmf_lock);
77977836SJohn.Forte@Sun.COM 		return;
77987836SJohn.Forte@Sun.COM 	}
77997836SJohn.Forte@Sun.COM 
78007836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_svc_active) {
78017836SJohn.Forte@Sun.COM 		int waitq_add = 0;
78027836SJohn.Forte@Sun.COM 		req = stmf_state.stmf_svc_active;
78037836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_active = req->svc_next;
78047836SJohn.Forte@Sun.COM 
78057836SJohn.Forte@Sun.COM 		switch (req->svc_cmd) {
78067836SJohn.Forte@Sun.COM 		case STMF_CMD_LPORT_ONLINE:
78077836SJohn.Forte@Sun.COM 			/* Fallthrough */
78087836SJohn.Forte@Sun.COM 		case STMF_CMD_LPORT_OFFLINE:
78097836SJohn.Forte@Sun.COM 			/* Fallthrough */
78107836SJohn.Forte@Sun.COM 		case STMF_CMD_LU_ONLINE:
78117836SJohn.Forte@Sun.COM 			/* Nothing to do */
78127836SJohn.Forte@Sun.COM 			waitq_add = 1;
78137836SJohn.Forte@Sun.COM 			break;
78147836SJohn.Forte@Sun.COM 
78157836SJohn.Forte@Sun.COM 		case STMF_CMD_LU_OFFLINE:
78167836SJohn.Forte@Sun.COM 			/* Remove all mappings of this LU */
78177836SJohn.Forte@Sun.COM 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
78187836SJohn.Forte@Sun.COM 			/* Kill all the pending I/Os for this LU */
78197836SJohn.Forte@Sun.COM 			mutex_exit(&stmf_state.stmf_lock);
78207836SJohn.Forte@Sun.COM 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
78218662SJordan.Vaughan@Sun.com 			    STMF_ABORTED);
78227836SJohn.Forte@Sun.COM 			mutex_enter(&stmf_state.stmf_lock);
78237836SJohn.Forte@Sun.COM 			waitq_add = 1;
78247836SJohn.Forte@Sun.COM 			break;
78257836SJohn.Forte@Sun.COM 		default:
78267836SJohn.Forte@Sun.COM 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
78277836SJohn.Forte@Sun.COM 			    req->svc_cmd);
78287836SJohn.Forte@Sun.COM 		}
78297836SJohn.Forte@Sun.COM 
78307836SJohn.Forte@Sun.COM 		if (waitq_add) {
78317836SJohn.Forte@Sun.COM 			/* Put it in the wait queue */
78327836SJohn.Forte@Sun.COM 			req->svc_next = stmf_state.stmf_svc_waiting;
78337836SJohn.Forte@Sun.COM 			stmf_state.stmf_svc_waiting = req;
78347836SJohn.Forte@Sun.COM 		}
78357836SJohn.Forte@Sun.COM 	}
78367836SJohn.Forte@Sun.COM 
78377836SJohn.Forte@Sun.COM 	/* The waiting list is not going to be modified by anybody else */
78387836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
78397836SJohn.Forte@Sun.COM 
78407836SJohn.Forte@Sun.COM 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
78417836SJohn.Forte@Sun.COM 		req = *preq;
78427836SJohn.Forte@Sun.COM 		deq = 0;
784311698SNattuvetty.Bhavyan@Sun.COM 
78447836SJohn.Forte@Sun.COM 		switch (req->svc_cmd) {
78457836SJohn.Forte@Sun.COM 		case STMF_CMD_LU_ONLINE:
78467836SJohn.Forte@Sun.COM 			lu = (stmf_lu_t *)req->svc_obj;
78477836SJohn.Forte@Sun.COM 			deq = 1;
78487836SJohn.Forte@Sun.COM 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
78497836SJohn.Forte@Sun.COM 			break;
78507836SJohn.Forte@Sun.COM 
78517836SJohn.Forte@Sun.COM 		case STMF_CMD_LU_OFFLINE:
78527836SJohn.Forte@Sun.COM 			lu = (stmf_lu_t *)req->svc_obj;
78537836SJohn.Forte@Sun.COM 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
78547836SJohn.Forte@Sun.COM 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
78557836SJohn.Forte@Sun.COM 				break;
78567836SJohn.Forte@Sun.COM 			deq = 1;
78577836SJohn.Forte@Sun.COM 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
78587836SJohn.Forte@Sun.COM 			break;
78597836SJohn.Forte@Sun.COM 
78607836SJohn.Forte@Sun.COM 		case STMF_CMD_LPORT_OFFLINE:
78617836SJohn.Forte@Sun.COM 			/* Fallthrough */
78627836SJohn.Forte@Sun.COM 		case STMF_CMD_LPORT_ONLINE:
78637836SJohn.Forte@Sun.COM 			lport = (stmf_local_port_t *)req->svc_obj;
78647836SJohn.Forte@Sun.COM 			deq = 1;
78657836SJohn.Forte@Sun.COM 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
78667836SJohn.Forte@Sun.COM 			break;
78677836SJohn.Forte@Sun.COM 		}
78687836SJohn.Forte@Sun.COM 		if (deq) {
78697836SJohn.Forte@Sun.COM 			*preq = req->svc_next;
78707836SJohn.Forte@Sun.COM 			kmem_free(req, req->svc_req_alloc_size);
78717836SJohn.Forte@Sun.COM 		} else {
78727836SJohn.Forte@Sun.COM 			preq = &req->svc_next;
78737836SJohn.Forte@Sun.COM 		}
78747836SJohn.Forte@Sun.COM 	}
78757836SJohn.Forte@Sun.COM 
78767836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
78777836SJohn.Forte@Sun.COM 	if (stmf_state.stmf_svc_active == NULL) {
78787836SJohn.Forte@Sun.COM 		/* Do timeouts */
78797836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_nlus &&
78807836SJohn.Forte@Sun.COM 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
78817836SJohn.Forte@Sun.COM 			if (!stmf_state.stmf_svc_ilu_timing) {
78827836SJohn.Forte@Sun.COM 				/* we are starting a new round */
78837836SJohn.Forte@Sun.COM 				stmf_state.stmf_svc_ilu_timing =
78847836SJohn.Forte@Sun.COM 				    stmf_state.stmf_ilulist;
78857836SJohn.Forte@Sun.COM 				timing_start = ddi_get_lbolt();
78867836SJohn.Forte@Sun.COM 			}
78877836SJohn.Forte@Sun.COM 			stmf_check_ilu_timing();
78887836SJohn.Forte@Sun.COM 			if (!stmf_state.stmf_svc_ilu_timing) {
78897836SJohn.Forte@Sun.COM 				/* we finished a complete round */
78907836SJohn.Forte@Sun.COM 				timing_next =
78917836SJohn.Forte@Sun.COM 				    timing_start + drv_usectohz(5*1000*1000);
78927836SJohn.Forte@Sun.COM 			} else {
78937836SJohn.Forte@Sun.COM 				/* we still have some ilu items to check */
78947836SJohn.Forte@Sun.COM 				timing_next =
78957836SJohn.Forte@Sun.COM 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
78967836SJohn.Forte@Sun.COM 			}
78977836SJohn.Forte@Sun.COM 			if (stmf_state.stmf_svc_active)
78987836SJohn.Forte@Sun.COM 				goto stmf_svc_loop;
78997836SJohn.Forte@Sun.COM 		}
79007836SJohn.Forte@Sun.COM 		/* Check if there are free tasks to clear */
79017836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_nlus &&
79027836SJohn.Forte@Sun.COM 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
79037836SJohn.Forte@Sun.COM 			if (!stmf_state.stmf_svc_ilu_draining) {
79047836SJohn.Forte@Sun.COM 				/* we are starting a new round */
79057836SJohn.Forte@Sun.COM 				stmf_state.stmf_svc_ilu_draining =
79067836SJohn.Forte@Sun.COM 				    stmf_state.stmf_ilulist;
79077836SJohn.Forte@Sun.COM 				drain_start = ddi_get_lbolt();
79087836SJohn.Forte@Sun.COM 			}
79097836SJohn.Forte@Sun.COM 			stmf_check_freetask();
79107836SJohn.Forte@Sun.COM 			if (!stmf_state.stmf_svc_ilu_draining) {
79117836SJohn.Forte@Sun.COM 				/* we finished a complete round */
79127836SJohn.Forte@Sun.COM 				drain_next =
79137836SJohn.Forte@Sun.COM 				    drain_start + drv_usectohz(10*1000*1000);
79147836SJohn.Forte@Sun.COM 			} else {
79157836SJohn.Forte@Sun.COM 				/* we still have some ilu items to check */
79167836SJohn.Forte@Sun.COM 				drain_next =
79177836SJohn.Forte@Sun.COM 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
79187836SJohn.Forte@Sun.COM 			}
79197836SJohn.Forte@Sun.COM 			if (stmf_state.stmf_svc_active)
79207836SJohn.Forte@Sun.COM 				goto stmf_svc_loop;
79217836SJohn.Forte@Sun.COM 		}
79227836SJohn.Forte@Sun.COM 
79237836SJohn.Forte@Sun.COM 		/* Check if we need to run worker_mgmt */
79247836SJohn.Forte@Sun.COM 		if (ddi_get_lbolt() > worker_delay) {
79257836SJohn.Forte@Sun.COM 			stmf_worker_mgmt();
79267836SJohn.Forte@Sun.COM 			worker_delay = ddi_get_lbolt() +
79277836SJohn.Forte@Sun.COM 			    stmf_worker_mgmt_delay;
79287836SJohn.Forte@Sun.COM 		}
79297836SJohn.Forte@Sun.COM 
79307836SJohn.Forte@Sun.COM 		/* Check if any active session got its 1st LUN */
79317836SJohn.Forte@Sun.COM 		if (stmf_state.stmf_process_initial_luns) {
79327836SJohn.Forte@Sun.COM 			int stmf_level = 0;
79337836SJohn.Forte@Sun.COM 			int port_level;
79347836SJohn.Forte@Sun.COM 			for (ilport = stmf_state.stmf_ilportlist; ilport;
79357836SJohn.Forte@Sun.COM 			    ilport = next_ilport) {
793611766SJohn.Forte@Sun.COM 				int ilport_lock_held;
79377836SJohn.Forte@Sun.COM 				next_ilport = ilport->ilport_next;
79387836SJohn.Forte@Sun.COM 				if ((ilport->ilport_flags &
79397836SJohn.Forte@Sun.COM 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
79407836SJohn.Forte@Sun.COM 					continue;
79417836SJohn.Forte@Sun.COM 				}
79427836SJohn.Forte@Sun.COM 				port_level = 0;
79437836SJohn.Forte@Sun.COM 				rw_enter(&ilport->ilport_lock, RW_READER);
794411766SJohn.Forte@Sun.COM 				ilport_lock_held = 1;
79457836SJohn.Forte@Sun.COM 				for (iss = ilport->ilport_ss_list; iss;
79467836SJohn.Forte@Sun.COM 				    iss = iss->iss_next) {
79477836SJohn.Forte@Sun.COM 					if ((iss->iss_flags &
79487836SJohn.Forte@Sun.COM 					    ISS_GOT_INITIAL_LUNS) == 0) {
79497836SJohn.Forte@Sun.COM 						continue;
79507836SJohn.Forte@Sun.COM 					}
79517836SJohn.Forte@Sun.COM 					port_level++;
79527836SJohn.Forte@Sun.COM 					stmf_level++;
79537836SJohn.Forte@Sun.COM 					atomic_and_32(&iss->iss_flags,
79548662SJordan.Vaughan@Sun.com 					    ~ISS_GOT_INITIAL_LUNS);
79557836SJohn.Forte@Sun.COM 					atomic_or_32(&iss->iss_flags,
79568662SJordan.Vaughan@Sun.com 					    ISS_EVENT_ACTIVE);
79577836SJohn.Forte@Sun.COM 					rw_exit(&ilport->ilport_lock);
795811766SJohn.Forte@Sun.COM 					ilport_lock_held = 0;
79597836SJohn.Forte@Sun.COM 					mutex_exit(&stmf_state.stmf_lock);
79607836SJohn.Forte@Sun.COM 					stmf_generate_lport_event(ilport,
79617836SJohn.Forte@Sun.COM 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
79627836SJohn.Forte@Sun.COM 					    iss->iss_ss, 0);
79637836SJohn.Forte@Sun.COM 					atomic_and_32(&iss->iss_flags,
79648662SJordan.Vaughan@Sun.com 					    ~ISS_EVENT_ACTIVE);
79657836SJohn.Forte@Sun.COM 					mutex_enter(&stmf_state.stmf_lock);
79667836SJohn.Forte@Sun.COM 					/*
79677836SJohn.Forte@Sun.COM 					 * scan all the ilports again as the
79687836SJohn.Forte@Sun.COM 					 * ilport list might have changed.
79697836SJohn.Forte@Sun.COM 					 */
79707836SJohn.Forte@Sun.COM 					next_ilport =
79718662SJordan.Vaughan@Sun.com 					    stmf_state.stmf_ilportlist;
79727836SJohn.Forte@Sun.COM 					break;
79737836SJohn.Forte@Sun.COM 				}
79747836SJohn.Forte@Sun.COM 				if (port_level == 0) {
79757836SJohn.Forte@Sun.COM 					atomic_and_32(&ilport->ilport_flags,
79768662SJordan.Vaughan@Sun.com 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
79777836SJohn.Forte@Sun.COM 				}
79787836SJohn.Forte@Sun.COM 				/* drop the lock if we are holding it. */
797911766SJohn.Forte@Sun.COM 				if (ilport_lock_held == 1)
79807836SJohn.Forte@Sun.COM 					rw_exit(&ilport->ilport_lock);
79817836SJohn.Forte@Sun.COM 
79827836SJohn.Forte@Sun.COM 				/* Max 4 session at a time */
79837836SJohn.Forte@Sun.COM 				if (stmf_level >= 4) {
79847836SJohn.Forte@Sun.COM 					break;
79857836SJohn.Forte@Sun.COM 				}
79867836SJohn.Forte@Sun.COM 			}
79877836SJohn.Forte@Sun.COM 			if (stmf_level == 0) {
79887836SJohn.Forte@Sun.COM 				stmf_state.stmf_process_initial_luns = 0;
79897836SJohn.Forte@Sun.COM 			}
79907836SJohn.Forte@Sun.COM 		}
79917836SJohn.Forte@Sun.COM 
79927836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
799311066Srafael.vanoni@sun.com 		(void) cv_reltimedwait(&stmf_state.stmf_cv,
799411066Srafael.vanoni@sun.com 		    &stmf_state.stmf_lock, td, TR_CLOCK_TICK);
79957836SJohn.Forte@Sun.COM 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
79967836SJohn.Forte@Sun.COM 	}
79977836SJohn.Forte@Sun.COM 	goto stmf_svc_loop;
79987836SJohn.Forte@Sun.COM }
79997836SJohn.Forte@Sun.COM 
80007836SJohn.Forte@Sun.COM void
stmf_svc_queue(int cmd,void * obj,stmf_state_change_info_t * info)80017836SJohn.Forte@Sun.COM stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
80027836SJohn.Forte@Sun.COM {
80037836SJohn.Forte@Sun.COM 	stmf_svc_req_t *req;
80047836SJohn.Forte@Sun.COM 	int s;
80057836SJohn.Forte@Sun.COM 
80067836SJohn.Forte@Sun.COM 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
80077836SJohn.Forte@Sun.COM 	s = sizeof (stmf_svc_req_t);
80087836SJohn.Forte@Sun.COM 	if (info->st_additional_info) {
80097836SJohn.Forte@Sun.COM 		s += strlen(info->st_additional_info) + 1;
80107836SJohn.Forte@Sun.COM 	}
80117836SJohn.Forte@Sun.COM 	req = kmem_zalloc(s, KM_SLEEP);
80127836SJohn.Forte@Sun.COM 
80137836SJohn.Forte@Sun.COM 	req->svc_cmd = cmd;
80147836SJohn.Forte@Sun.COM 	req->svc_obj = obj;
80157836SJohn.Forte@Sun.COM 	req->svc_info.st_rflags = info->st_rflags;
80167836SJohn.Forte@Sun.COM 	if (info->st_additional_info) {
80177836SJohn.Forte@Sun.COM 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
80187836SJohn.Forte@Sun.COM 		    sizeof (stmf_svc_req_t)));
80197836SJohn.Forte@Sun.COM 		(void) strcpy(req->svc_info.st_additional_info,
80207836SJohn.Forte@Sun.COM 		    info->st_additional_info);
80217836SJohn.Forte@Sun.COM 	}
80227836SJohn.Forte@Sun.COM 	req->svc_req_alloc_size = s;
80237836SJohn.Forte@Sun.COM 
80247836SJohn.Forte@Sun.COM 	mutex_enter(&stmf_state.stmf_lock);
80257836SJohn.Forte@Sun.COM 	req->svc_next = stmf_state.stmf_svc_active;
80267836SJohn.Forte@Sun.COM 	stmf_state.stmf_svc_active = req;
80277836SJohn.Forte@Sun.COM 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
80287836SJohn.Forte@Sun.COM 		cv_signal(&stmf_state.stmf_cv);
80297836SJohn.Forte@Sun.COM 	}
80307836SJohn.Forte@Sun.COM 	mutex_exit(&stmf_state.stmf_lock);
80317836SJohn.Forte@Sun.COM }
80327836SJohn.Forte@Sun.COM 
80337836SJohn.Forte@Sun.COM void
stmf_trace(caddr_t ident,const char * fmt,...)80347836SJohn.Forte@Sun.COM stmf_trace(caddr_t ident, const char *fmt, ...)
80357836SJohn.Forte@Sun.COM {
80367836SJohn.Forte@Sun.COM 	va_list args;
80377836SJohn.Forte@Sun.COM 	char tbuf[160];
80387836SJohn.Forte@Sun.COM 	int len;
80397836SJohn.Forte@Sun.COM 
80407836SJohn.Forte@Sun.COM 	if (!stmf_trace_on)
80417836SJohn.Forte@Sun.COM 		return;
80427836SJohn.Forte@Sun.COM 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
80437836SJohn.Forte@Sun.COM 	    ddi_get_lbolt());
80447836SJohn.Forte@Sun.COM 	va_start(args, fmt);
80457836SJohn.Forte@Sun.COM 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
80467836SJohn.Forte@Sun.COM 	va_end(args);
80477836SJohn.Forte@Sun.COM 
80487836SJohn.Forte@Sun.COM 	if (len > 158) {
80497836SJohn.Forte@Sun.COM 		len = 158;
80507836SJohn.Forte@Sun.COM 	}
80517836SJohn.Forte@Sun.COM 	tbuf[len++] = '\n';
80527836SJohn.Forte@Sun.COM 	tbuf[len] = 0;
80537836SJohn.Forte@Sun.COM 
80547836SJohn.Forte@Sun.COM 	mutex_enter(&trace_buf_lock);
80557836SJohn.Forte@Sun.COM 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
80567836SJohn.Forte@Sun.COM 	trace_buf_curndx += len;
80577836SJohn.Forte@Sun.COM 	if (trace_buf_curndx > (trace_buf_size - 320))
80587836SJohn.Forte@Sun.COM 		trace_buf_curndx = 0;
80597836SJohn.Forte@Sun.COM 	mutex_exit(&trace_buf_lock);
80607836SJohn.Forte@Sun.COM }
80617836SJohn.Forte@Sun.COM 
80627836SJohn.Forte@Sun.COM void
stmf_trace_clear()80637836SJohn.Forte@Sun.COM stmf_trace_clear()
80647836SJohn.Forte@Sun.COM {
80657836SJohn.Forte@Sun.COM 	if (!stmf_trace_on)
80667836SJohn.Forte@Sun.COM 		return;
80677836SJohn.Forte@Sun.COM 	mutex_enter(&trace_buf_lock);
80687836SJohn.Forte@Sun.COM 	trace_buf_curndx = 0;
80697836SJohn.Forte@Sun.COM 	if (trace_buf_size > 0)
80707836SJohn.Forte@Sun.COM 		stmf_trace_buf[0] = 0;
80717836SJohn.Forte@Sun.COM 	mutex_exit(&trace_buf_lock);
80727836SJohn.Forte@Sun.COM }
80737836SJohn.Forte@Sun.COM 
80747836SJohn.Forte@Sun.COM static void
stmf_abort_task_offline(scsi_task_t * task,int offline_lu,char * info)80757836SJohn.Forte@Sun.COM stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
80767836SJohn.Forte@Sun.COM {
807710421STim.Szeto@Sun.COM 	stmf_state_change_info_t	change_info;
80787836SJohn.Forte@Sun.COM 	void				*ctl_private;
807910421STim.Szeto@Sun.COM 	uint32_t			ctl_cmd;
80807836SJohn.Forte@Sun.COM 	int				msg = 0;
80817836SJohn.Forte@Sun.COM 
80827836SJohn.Forte@Sun.COM 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
80837836SJohn.Forte@Sun.COM 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
80847836SJohn.Forte@Sun.COM 	change_info.st_additional_info = info;
80857836SJohn.Forte@Sun.COM 	if (offline_lu) {
80867836SJohn.Forte@Sun.COM 		change_info.st_rflags = STMF_RFLAG_RESET |
80877836SJohn.Forte@Sun.COM 		    STMF_RFLAG_LU_ABORT;
80887836SJohn.Forte@Sun.COM 		ctl_private = task->task_lu;
80897836SJohn.Forte@Sun.COM 		if (((stmf_i_lu_t *)
80907836SJohn.Forte@Sun.COM 		    task->task_lu->lu_stmf_private)->ilu_state ==
80917836SJohn.Forte@Sun.COM 		    STMF_STATE_ONLINE) {
80927836SJohn.Forte@Sun.COM 			msg = 1;
80937836SJohn.Forte@Sun.COM 		}
80947836SJohn.Forte@Sun.COM 		ctl_cmd = STMF_CMD_LU_OFFLINE;
80957836SJohn.Forte@Sun.COM 	} else {
80967836SJohn.Forte@Sun.COM 		change_info.st_rflags = STMF_RFLAG_RESET |
80977836SJohn.Forte@Sun.COM 		    STMF_RFLAG_LPORT_ABORT;
80987836SJohn.Forte@Sun.COM 		ctl_private = task->task_lport;
80997836SJohn.Forte@Sun.COM 		if (((stmf_i_local_port_t *)
81007836SJohn.Forte@Sun.COM 		    task->task_lport->lport_stmf_private)->ilport_state ==
81017836SJohn.Forte@Sun.COM 		    STMF_STATE_ONLINE) {
81027836SJohn.Forte@Sun.COM 			msg = 1;
81037836SJohn.Forte@Sun.COM 		}
81047836SJohn.Forte@Sun.COM 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
81057836SJohn.Forte@Sun.COM 	}
81067836SJohn.Forte@Sun.COM 
81077836SJohn.Forte@Sun.COM 	if (msg) {
81087836SJohn.Forte@Sun.COM 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
81097836SJohn.Forte@Sun.COM 		    offline_lu ? "LU" : "LPORT", info ? info :
81107836SJohn.Forte@Sun.COM 		    "<no additional info>");
81117836SJohn.Forte@Sun.COM 	}
81127836SJohn.Forte@Sun.COM 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
81137836SJohn.Forte@Sun.COM }
8114*12750SNattuvetty.Bhavyan@Sun.COM 
8115*12750SNattuvetty.Bhavyan@Sun.COM static char
stmf_ctoi(char c)8116*12750SNattuvetty.Bhavyan@Sun.COM stmf_ctoi(char c)
8117*12750SNattuvetty.Bhavyan@Sun.COM {
8118*12750SNattuvetty.Bhavyan@Sun.COM 	if ((c >= '0') && (c <= '9'))
8119*12750SNattuvetty.Bhavyan@Sun.COM 		c -= '0';
8120*12750SNattuvetty.Bhavyan@Sun.COM 	else if ((c >= 'A') && (c <= 'F'))
8121*12750SNattuvetty.Bhavyan@Sun.COM 		c = c - 'A' + 10;
8122*12750SNattuvetty.Bhavyan@Sun.COM 	else if ((c >= 'a') && (c <= 'f'))
8123*12750SNattuvetty.Bhavyan@Sun.COM 		c = c - 'a' + 10;
8124*12750SNattuvetty.Bhavyan@Sun.COM 	else
8125*12750SNattuvetty.Bhavyan@Sun.COM 		c = -1;
8126*12750SNattuvetty.Bhavyan@Sun.COM 	return (c);
8127*12750SNattuvetty.Bhavyan@Sun.COM }
8128*12750SNattuvetty.Bhavyan@Sun.COM 
8129*12750SNattuvetty.Bhavyan@Sun.COM /* Convert from Hex value in ASCII format to the equivalent bytes */
8130*12750SNattuvetty.Bhavyan@Sun.COM static boolean_t
stmf_base16_str_to_binary(char * c,int dplen,uint8_t * dp)8131*12750SNattuvetty.Bhavyan@Sun.COM stmf_base16_str_to_binary(char *c, int dplen, uint8_t *dp)
8132*12750SNattuvetty.Bhavyan@Sun.COM {
8133*12750SNattuvetty.Bhavyan@Sun.COM 	int		ii;
8134*12750SNattuvetty.Bhavyan@Sun.COM 
8135*12750SNattuvetty.Bhavyan@Sun.COM 	for (ii = 0; ii < dplen; ii++) {
8136*12750SNattuvetty.Bhavyan@Sun.COM 		char nibble1, nibble2;
8137*12750SNattuvetty.Bhavyan@Sun.COM 		char enc_char = *c++;
8138*12750SNattuvetty.Bhavyan@Sun.COM 		nibble1 = stmf_ctoi(enc_char);
8139*12750SNattuvetty.Bhavyan@Sun.COM 
8140*12750SNattuvetty.Bhavyan@Sun.COM 		enc_char = *c++;
8141*12750SNattuvetty.Bhavyan@Sun.COM 		nibble2 = stmf_ctoi(enc_char);
8142*12750SNattuvetty.Bhavyan@Sun.COM 		if (nibble1 == -1 || nibble2 == -1)
8143*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8144*12750SNattuvetty.Bhavyan@Sun.COM 
8145*12750SNattuvetty.Bhavyan@Sun.COM 		dp[ii] = (nibble1 << 4) | nibble2;
8146*12750SNattuvetty.Bhavyan@Sun.COM 	}
8147*12750SNattuvetty.Bhavyan@Sun.COM 	return (B_TRUE);
8148*12750SNattuvetty.Bhavyan@Sun.COM }
8149*12750SNattuvetty.Bhavyan@Sun.COM 
8150*12750SNattuvetty.Bhavyan@Sun.COM boolean_t
stmf_scsilib_tptid_validate(scsi_transport_id_t * tptid,uint32_t total_sz,uint16_t * tptid_sz)8151*12750SNattuvetty.Bhavyan@Sun.COM stmf_scsilib_tptid_validate(scsi_transport_id_t *tptid, uint32_t total_sz,
8152*12750SNattuvetty.Bhavyan@Sun.COM 				uint16_t *tptid_sz)
8153*12750SNattuvetty.Bhavyan@Sun.COM {
8154*12750SNattuvetty.Bhavyan@Sun.COM 	uint16_t tpd_len = SCSI_TPTID_SIZE;
8155*12750SNattuvetty.Bhavyan@Sun.COM 
8156*12750SNattuvetty.Bhavyan@Sun.COM 	if (tptid_sz)
8157*12750SNattuvetty.Bhavyan@Sun.COM 		*tptid_sz = 0;
8158*12750SNattuvetty.Bhavyan@Sun.COM 	if (total_sz < sizeof (scsi_transport_id_t))
8159*12750SNattuvetty.Bhavyan@Sun.COM 		return (B_FALSE);
8160*12750SNattuvetty.Bhavyan@Sun.COM 
8161*12750SNattuvetty.Bhavyan@Sun.COM 	switch (tptid->protocol_id) {
8162*12750SNattuvetty.Bhavyan@Sun.COM 
8163*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_FIBRE_CHANNEL:
8164*12750SNattuvetty.Bhavyan@Sun.COM 		/* FC Transport ID validation checks. SPC3 rev23, Table 284 */
8165*12750SNattuvetty.Bhavyan@Sun.COM 		if (total_sz < tpd_len || tptid->format_code != 0)
8166*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8167*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8168*12750SNattuvetty.Bhavyan@Sun.COM 
8169*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_iSCSI:
8170*12750SNattuvetty.Bhavyan@Sun.COM 		{
8171*12750SNattuvetty.Bhavyan@Sun.COM 		iscsi_transport_id_t	*iscsiid;
8172*12750SNattuvetty.Bhavyan@Sun.COM 		uint16_t		adn_len, name_len;
8173*12750SNattuvetty.Bhavyan@Sun.COM 
8174*12750SNattuvetty.Bhavyan@Sun.COM 		/* Check for valid format code, SPC3 rev 23 Table 288 */
8175*12750SNattuvetty.Bhavyan@Sun.COM 		if ((total_sz < tpd_len) ||
8176*12750SNattuvetty.Bhavyan@Sun.COM 		    (tptid->format_code != 0 && tptid->format_code != 1))
8177*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8178*12750SNattuvetty.Bhavyan@Sun.COM 
8179*12750SNattuvetty.Bhavyan@Sun.COM 		iscsiid = (iscsi_transport_id_t *)tptid;
8180*12750SNattuvetty.Bhavyan@Sun.COM 		adn_len = READ_SCSI16(iscsiid->add_len, uint16_t);
8181*12750SNattuvetty.Bhavyan@Sun.COM 		tpd_len = sizeof (iscsi_transport_id_t) + adn_len - 1;
8182*12750SNattuvetty.Bhavyan@Sun.COM 
8183*12750SNattuvetty.Bhavyan@Sun.COM 		/*
8184*12750SNattuvetty.Bhavyan@Sun.COM 		 * iSCSI Transport ID validation checks.
8185*12750SNattuvetty.Bhavyan@Sun.COM 		 * As per SPC3 rev 23 Section 7.5.4.6 and Table 289 & Table 290
8186*12750SNattuvetty.Bhavyan@Sun.COM 		 */
8187*12750SNattuvetty.Bhavyan@Sun.COM 		if (adn_len < 20 || (adn_len % 4 != 0))
8188*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8189*12750SNattuvetty.Bhavyan@Sun.COM 
8190*12750SNattuvetty.Bhavyan@Sun.COM 		name_len = strnlen(iscsiid->iscsi_name, adn_len);
8191*12750SNattuvetty.Bhavyan@Sun.COM 		if (name_len == 0 || name_len >= adn_len)
8192*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8193*12750SNattuvetty.Bhavyan@Sun.COM 
8194*12750SNattuvetty.Bhavyan@Sun.COM 		/* If the format_code is 1 check for ISID seperator */
8195*12750SNattuvetty.Bhavyan@Sun.COM 		if ((tptid->format_code == 1) && (strstr(iscsiid->iscsi_name,
8196*12750SNattuvetty.Bhavyan@Sun.COM 		    SCSI_TPTID_ISCSI_ISID_SEPERATOR) == NULL))
8197*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8198*12750SNattuvetty.Bhavyan@Sun.COM 
8199*12750SNattuvetty.Bhavyan@Sun.COM 		}
8200*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8201*12750SNattuvetty.Bhavyan@Sun.COM 
8202*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SRP:
8203*12750SNattuvetty.Bhavyan@Sun.COM 		/* SRP Transport ID validation checks. SPC3 rev23, Table 287 */
8204*12750SNattuvetty.Bhavyan@Sun.COM 		if (total_sz < tpd_len || tptid->format_code != 0)
8205*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8206*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8207*12750SNattuvetty.Bhavyan@Sun.COM 
8208*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_PARALLEL_SCSI:
8209*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SSA:
8210*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_IEEE_1394:
8211*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SAS:
8212*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ADT:
8213*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ATAPI:
8214*12750SNattuvetty.Bhavyan@Sun.COM 	default:
8215*12750SNattuvetty.Bhavyan@Sun.COM 		{
8216*12750SNattuvetty.Bhavyan@Sun.COM 		stmf_dflt_scsi_tptid_t *dflttpd;
8217*12750SNattuvetty.Bhavyan@Sun.COM 
8218*12750SNattuvetty.Bhavyan@Sun.COM 		tpd_len = sizeof (stmf_dflt_scsi_tptid_t);
8219*12750SNattuvetty.Bhavyan@Sun.COM 		if (total_sz < tpd_len)
8220*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8221*12750SNattuvetty.Bhavyan@Sun.COM 		dflttpd = (stmf_dflt_scsi_tptid_t *)tptid;
8222*12750SNattuvetty.Bhavyan@Sun.COM 		tpd_len = tpd_len + SCSI_READ16(&dflttpd->ident_len) - 1;
8223*12750SNattuvetty.Bhavyan@Sun.COM 		if (total_sz < tpd_len)
8224*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8225*12750SNattuvetty.Bhavyan@Sun.COM 		}
8226*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8227*12750SNattuvetty.Bhavyan@Sun.COM 	}
8228*12750SNattuvetty.Bhavyan@Sun.COM 	if (tptid_sz)
8229*12750SNattuvetty.Bhavyan@Sun.COM 		*tptid_sz = tpd_len;
8230*12750SNattuvetty.Bhavyan@Sun.COM 	return (B_TRUE);
8231*12750SNattuvetty.Bhavyan@Sun.COM }
8232*12750SNattuvetty.Bhavyan@Sun.COM 
8233*12750SNattuvetty.Bhavyan@Sun.COM boolean_t
stmf_scsilib_tptid_compare(scsi_transport_id_t * tpd1,scsi_transport_id_t * tpd2)8234*12750SNattuvetty.Bhavyan@Sun.COM stmf_scsilib_tptid_compare(scsi_transport_id_t *tpd1,
8235*12750SNattuvetty.Bhavyan@Sun.COM 				scsi_transport_id_t *tpd2)
8236*12750SNattuvetty.Bhavyan@Sun.COM {
8237*12750SNattuvetty.Bhavyan@Sun.COM 	if ((tpd1->protocol_id != tpd2->protocol_id) ||
8238*12750SNattuvetty.Bhavyan@Sun.COM 	    (tpd1->format_code != tpd2->format_code))
8239*12750SNattuvetty.Bhavyan@Sun.COM 		return (B_FALSE);
8240*12750SNattuvetty.Bhavyan@Sun.COM 
8241*12750SNattuvetty.Bhavyan@Sun.COM 	switch (tpd1->protocol_id) {
8242*12750SNattuvetty.Bhavyan@Sun.COM 
8243*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_iSCSI:
8244*12750SNattuvetty.Bhavyan@Sun.COM 		{
8245*12750SNattuvetty.Bhavyan@Sun.COM 		iscsi_transport_id_t *iscsitpd1, *iscsitpd2;
8246*12750SNattuvetty.Bhavyan@Sun.COM 		uint16_t len;
8247*12750SNattuvetty.Bhavyan@Sun.COM 
8248*12750SNattuvetty.Bhavyan@Sun.COM 		iscsitpd1 = (iscsi_transport_id_t *)tpd1;
8249*12750SNattuvetty.Bhavyan@Sun.COM 		iscsitpd2 = (iscsi_transport_id_t *)tpd2;
8250*12750SNattuvetty.Bhavyan@Sun.COM 		len = SCSI_READ16(&iscsitpd1->add_len);
8251*12750SNattuvetty.Bhavyan@Sun.COM 		if ((memcmp(iscsitpd1->add_len, iscsitpd2->add_len, 2) != 0) ||
8252*12750SNattuvetty.Bhavyan@Sun.COM 		    (memcmp(iscsitpd1->iscsi_name, iscsitpd2->iscsi_name, len)
8253*12750SNattuvetty.Bhavyan@Sun.COM 		    != 0))
8254*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8255*12750SNattuvetty.Bhavyan@Sun.COM 		}
8256*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8257*12750SNattuvetty.Bhavyan@Sun.COM 
8258*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SRP:
8259*12750SNattuvetty.Bhavyan@Sun.COM 		{
8260*12750SNattuvetty.Bhavyan@Sun.COM 		scsi_srp_transport_id_t *srptpd1, *srptpd2;
8261*12750SNattuvetty.Bhavyan@Sun.COM 
8262*12750SNattuvetty.Bhavyan@Sun.COM 		srptpd1 = (scsi_srp_transport_id_t *)tpd1;
8263*12750SNattuvetty.Bhavyan@Sun.COM 		srptpd2 = (scsi_srp_transport_id_t *)tpd2;
8264*12750SNattuvetty.Bhavyan@Sun.COM 		if (memcmp(srptpd1->srp_name, srptpd2->srp_name,
8265*12750SNattuvetty.Bhavyan@Sun.COM 		    sizeof (srptpd1->srp_name)) != 0)
8266*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8267*12750SNattuvetty.Bhavyan@Sun.COM 		}
8268*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8269*12750SNattuvetty.Bhavyan@Sun.COM 
8270*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_FIBRE_CHANNEL:
8271*12750SNattuvetty.Bhavyan@Sun.COM 		{
8272*12750SNattuvetty.Bhavyan@Sun.COM 		scsi_fc_transport_id_t *fctpd1, *fctpd2;
8273*12750SNattuvetty.Bhavyan@Sun.COM 
8274*12750SNattuvetty.Bhavyan@Sun.COM 		fctpd1 = (scsi_fc_transport_id_t *)tpd1;
8275*12750SNattuvetty.Bhavyan@Sun.COM 		fctpd2 = (scsi_fc_transport_id_t *)tpd2;
8276*12750SNattuvetty.Bhavyan@Sun.COM 		if (memcmp(fctpd1->port_name, fctpd2->port_name,
8277*12750SNattuvetty.Bhavyan@Sun.COM 		    sizeof (fctpd1->port_name)) != 0)
8278*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8279*12750SNattuvetty.Bhavyan@Sun.COM 		}
8280*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8281*12750SNattuvetty.Bhavyan@Sun.COM 
8282*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_PARALLEL_SCSI:
8283*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SSA:
8284*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_IEEE_1394:
8285*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SAS:
8286*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ADT:
8287*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ATAPI:
8288*12750SNattuvetty.Bhavyan@Sun.COM 	default:
8289*12750SNattuvetty.Bhavyan@Sun.COM 		{
8290*12750SNattuvetty.Bhavyan@Sun.COM 		stmf_dflt_scsi_tptid_t *dflt1, *dflt2;
8291*12750SNattuvetty.Bhavyan@Sun.COM 		uint16_t len;
8292*12750SNattuvetty.Bhavyan@Sun.COM 
8293*12750SNattuvetty.Bhavyan@Sun.COM 		dflt1 = (stmf_dflt_scsi_tptid_t *)tpd1;
8294*12750SNattuvetty.Bhavyan@Sun.COM 		dflt2 = (stmf_dflt_scsi_tptid_t *)tpd2;
8295*12750SNattuvetty.Bhavyan@Sun.COM 		len = SCSI_READ16(&dflt1->ident_len);
8296*12750SNattuvetty.Bhavyan@Sun.COM 		if ((memcmp(dflt1->ident_len, dflt2->ident_len, 2) != 0) ||
8297*12750SNattuvetty.Bhavyan@Sun.COM 		    (memcmp(dflt1->ident, dflt2->ident, len) != 0))
8298*12750SNattuvetty.Bhavyan@Sun.COM 			return (B_FALSE);
8299*12750SNattuvetty.Bhavyan@Sun.COM 		}
8300*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8301*12750SNattuvetty.Bhavyan@Sun.COM 	}
8302*12750SNattuvetty.Bhavyan@Sun.COM 	return (B_TRUE);
8303*12750SNattuvetty.Bhavyan@Sun.COM }
8304*12750SNattuvetty.Bhavyan@Sun.COM 
8305*12750SNattuvetty.Bhavyan@Sun.COM /*
8306*12750SNattuvetty.Bhavyan@Sun.COM  * Changes devid_desc to corresponding TransportID format
8307*12750SNattuvetty.Bhavyan@Sun.COM  * Returns :- pointer to stmf_remote_port_t
8308*12750SNattuvetty.Bhavyan@Sun.COM  * Note    :- Allocates continous memory for stmf_remote_port_t and TransportID,
8309*12750SNattuvetty.Bhavyan@Sun.COM  *            This memory need to be freed when this remote_port is no longer
8310*12750SNattuvetty.Bhavyan@Sun.COM  *            used.
8311*12750SNattuvetty.Bhavyan@Sun.COM  */
8312*12750SNattuvetty.Bhavyan@Sun.COM stmf_remote_port_t *
stmf_scsilib_devid_to_remote_port(scsi_devid_desc_t * devid)8313*12750SNattuvetty.Bhavyan@Sun.COM stmf_scsilib_devid_to_remote_port(scsi_devid_desc_t *devid)
8314*12750SNattuvetty.Bhavyan@Sun.COM {
8315*12750SNattuvetty.Bhavyan@Sun.COM 	struct scsi_fc_transport_id	*fc_tpd;
8316*12750SNattuvetty.Bhavyan@Sun.COM 	struct iscsi_transport_id	*iscsi_tpd;
8317*12750SNattuvetty.Bhavyan@Sun.COM 	struct scsi_srp_transport_id	*srp_tpd;
8318*12750SNattuvetty.Bhavyan@Sun.COM 	struct stmf_dflt_scsi_tptid	*dflt_tpd;
8319*12750SNattuvetty.Bhavyan@Sun.COM 	uint16_t ident_len,  sz = 0;
8320*12750SNattuvetty.Bhavyan@Sun.COM 	stmf_remote_port_t *rpt = NULL;
8321*12750SNattuvetty.Bhavyan@Sun.COM 
8322*12750SNattuvetty.Bhavyan@Sun.COM 	ident_len = devid->ident_length;
8323*12750SNattuvetty.Bhavyan@Sun.COM 	ASSERT(ident_len);
8324*12750SNattuvetty.Bhavyan@Sun.COM 	switch (devid->protocol_id) {
8325*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_FIBRE_CHANNEL:
8326*12750SNattuvetty.Bhavyan@Sun.COM 		sz = sizeof (scsi_fc_transport_id_t);
8327*12750SNattuvetty.Bhavyan@Sun.COM 		rpt = stmf_remote_port_alloc(sz);
8328*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->format_code = 0;
8329*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->protocol_id = devid->protocol_id;
8330*12750SNattuvetty.Bhavyan@Sun.COM 		fc_tpd = (scsi_fc_transport_id_t *)rpt->rport_tptid;
8331*12750SNattuvetty.Bhavyan@Sun.COM 		/*
8332*12750SNattuvetty.Bhavyan@Sun.COM 		 * convert from "wwn.xxxxxxxxxxxxxxxx" to 8-byte binary
8333*12750SNattuvetty.Bhavyan@Sun.COM 		 * skip first 4 byte for "wwn."
8334*12750SNattuvetty.Bhavyan@Sun.COM 		 */
8335*12750SNattuvetty.Bhavyan@Sun.COM 		ASSERT(strncmp("wwn.", (char *)devid->ident, 4) == 0);
8336*12750SNattuvetty.Bhavyan@Sun.COM 		if ((ident_len < SCSI_TPTID_FC_PORT_NAME_SIZE * 2 + 4) ||
8337*12750SNattuvetty.Bhavyan@Sun.COM 		    !stmf_base16_str_to_binary((char *)devid->ident + 4,
8338*12750SNattuvetty.Bhavyan@Sun.COM 		    SCSI_TPTID_FC_PORT_NAME_SIZE, fc_tpd->port_name))
8339*12750SNattuvetty.Bhavyan@Sun.COM 			goto devid_to_remote_port_fail;
8340*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8341*12750SNattuvetty.Bhavyan@Sun.COM 
8342*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_iSCSI:
8343*12750SNattuvetty.Bhavyan@Sun.COM 		sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (iscsi_transport_id_t) +
8344*12750SNattuvetty.Bhavyan@Sun.COM 		    ident_len - 1);
8345*12750SNattuvetty.Bhavyan@Sun.COM 		rpt = stmf_remote_port_alloc(sz);
8346*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->format_code = 0;
8347*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->protocol_id = devid->protocol_id;
8348*12750SNattuvetty.Bhavyan@Sun.COM 		iscsi_tpd = (iscsi_transport_id_t *)rpt->rport_tptid;
8349*12750SNattuvetty.Bhavyan@Sun.COM 		SCSI_WRITE16(iscsi_tpd->add_len, ident_len);
8350*12750SNattuvetty.Bhavyan@Sun.COM 		(void) memcpy(iscsi_tpd->iscsi_name, devid->ident, ident_len);
8351*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8352*12750SNattuvetty.Bhavyan@Sun.COM 
8353*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SRP:
8354*12750SNattuvetty.Bhavyan@Sun.COM 		sz = sizeof (scsi_srp_transport_id_t);
8355*12750SNattuvetty.Bhavyan@Sun.COM 		rpt = stmf_remote_port_alloc(sz);
8356*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->format_code = 0;
8357*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->protocol_id = devid->protocol_id;
8358*12750SNattuvetty.Bhavyan@Sun.COM 		srp_tpd = (scsi_srp_transport_id_t *)rpt->rport_tptid;
8359*12750SNattuvetty.Bhavyan@Sun.COM 		/*
8360*12750SNattuvetty.Bhavyan@Sun.COM 		 * convert from "eui.xxxxxxxxxxxxxxx" to 8-byte binary
8361*12750SNattuvetty.Bhavyan@Sun.COM 		 * skip first 4 byte for "eui."
8362*12750SNattuvetty.Bhavyan@Sun.COM 		 * Assume 8-byte initiator-extension part of srp_name is NOT
8363*12750SNattuvetty.Bhavyan@Sun.COM 		 * stored in devid and hence will be set as zero
8364*12750SNattuvetty.Bhavyan@Sun.COM 		 */
8365*12750SNattuvetty.Bhavyan@Sun.COM 		ASSERT(strncmp("eui.", (char *)devid->ident, 4) == 0);
8366*12750SNattuvetty.Bhavyan@Sun.COM 		if ((ident_len < (SCSI_TPTID_SRP_PORT_NAME_LEN - 8) * 2 + 4) ||
8367*12750SNattuvetty.Bhavyan@Sun.COM 		    !stmf_base16_str_to_binary((char *)devid->ident+4,
8368*12750SNattuvetty.Bhavyan@Sun.COM 		    SCSI_TPTID_SRP_PORT_NAME_LEN, srp_tpd->srp_name))
8369*12750SNattuvetty.Bhavyan@Sun.COM 			goto devid_to_remote_port_fail;
8370*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8371*12750SNattuvetty.Bhavyan@Sun.COM 
8372*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_PARALLEL_SCSI:
8373*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SSA:
8374*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_IEEE_1394:
8375*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_SAS:
8376*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ADT:
8377*12750SNattuvetty.Bhavyan@Sun.COM 	case PROTOCOL_ATAPI:
8378*12750SNattuvetty.Bhavyan@Sun.COM 	default :
8379*12750SNattuvetty.Bhavyan@Sun.COM 		ident_len = devid->ident_length;
8380*12750SNattuvetty.Bhavyan@Sun.COM 		sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (stmf_dflt_scsi_tptid_t) +
8381*12750SNattuvetty.Bhavyan@Sun.COM 		    ident_len - 1);
8382*12750SNattuvetty.Bhavyan@Sun.COM 		rpt = stmf_remote_port_alloc(sz);
8383*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->format_code = 0;
8384*12750SNattuvetty.Bhavyan@Sun.COM 		rpt->rport_tptid->protocol_id = devid->protocol_id;
8385*12750SNattuvetty.Bhavyan@Sun.COM 		dflt_tpd = (stmf_dflt_scsi_tptid_t *)rpt->rport_tptid;
8386*12750SNattuvetty.Bhavyan@Sun.COM 		SCSI_WRITE16(dflt_tpd->ident_len, ident_len);
8387*12750SNattuvetty.Bhavyan@Sun.COM 		(void) memcpy(dflt_tpd->ident, devid->ident, ident_len);
8388*12750SNattuvetty.Bhavyan@Sun.COM 		break;
8389*12750SNattuvetty.Bhavyan@Sun.COM 	}
8390*12750SNattuvetty.Bhavyan@Sun.COM 	return (rpt);
8391*12750SNattuvetty.Bhavyan@Sun.COM 
8392*12750SNattuvetty.Bhavyan@Sun.COM devid_to_remote_port_fail:
8393*12750SNattuvetty.Bhavyan@Sun.COM 	stmf_remote_port_free(rpt);
8394*12750SNattuvetty.Bhavyan@Sun.COM 	return (NULL);
8395*12750SNattuvetty.Bhavyan@Sun.COM 
8396*12750SNattuvetty.Bhavyan@Sun.COM }
8397*12750SNattuvetty.Bhavyan@Sun.COM 
8398*12750SNattuvetty.Bhavyan@Sun.COM stmf_remote_port_t *
stmf_remote_port_alloc(uint16_t tptid_sz)8399*12750SNattuvetty.Bhavyan@Sun.COM stmf_remote_port_alloc(uint16_t tptid_sz) {
8400*12750SNattuvetty.Bhavyan@Sun.COM 	stmf_remote_port_t *rpt;
8401*12750SNattuvetty.Bhavyan@Sun.COM 	rpt = (stmf_remote_port_t *)kmem_zalloc(
8402*12750SNattuvetty.Bhavyan@Sun.COM 	    sizeof (stmf_remote_port_t) + tptid_sz, KM_SLEEP);
8403*12750SNattuvetty.Bhavyan@Sun.COM 	rpt->rport_tptid_sz = tptid_sz;
8404*12750SNattuvetty.Bhavyan@Sun.COM 	rpt->rport_tptid = (scsi_transport_id_t *)(rpt + 1);
8405*12750SNattuvetty.Bhavyan@Sun.COM 	return (rpt);
8406*12750SNattuvetty.Bhavyan@Sun.COM }
8407*12750SNattuvetty.Bhavyan@Sun.COM 
8408*12750SNattuvetty.Bhavyan@Sun.COM void
stmf_remote_port_free(stmf_remote_port_t * rpt)8409*12750SNattuvetty.Bhavyan@Sun.COM stmf_remote_port_free(stmf_remote_port_t *rpt)
8410*12750SNattuvetty.Bhavyan@Sun.COM {
8411*12750SNattuvetty.Bhavyan@Sun.COM 	/*
8412*12750SNattuvetty.Bhavyan@Sun.COM 	 * Note: stmf_scsilib_devid_to_remote_port() function allocates
8413*12750SNattuvetty.Bhavyan@Sun.COM 	 *	remote port structures for all transports in the same way, So
8414*12750SNattuvetty.Bhavyan@Sun.COM 	 *	it is safe to deallocate it in a protocol independent manner.
8415*12750SNattuvetty.Bhavyan@Sun.COM 	 *	If any of the allocation method changes, corresponding changes
8416*12750SNattuvetty.Bhavyan@Sun.COM 	 *	need to be made here too.
8417*12750SNattuvetty.Bhavyan@Sun.COM 	 */
8418*12750SNattuvetty.Bhavyan@Sun.COM 	kmem_free(rpt, sizeof (stmf_remote_port_t) + rpt->rport_tptid_sz);
8419*12750SNattuvetty.Bhavyan@Sun.COM }
8420