xref: /titanic_50/usr/src/uts/common/io/comstar/stmf/stmf.c (revision e27732d8ead160782f0326b20fe46c0e9126df9b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <sys/conf.h>
26 #include <sys/file.h>
27 #include <sys/ddi.h>
28 #include <sys/sunddi.h>
29 #include <sys/modctl.h>
30 #include <sys/scsi/scsi.h>
31 #include <sys/scsi/impl/scsi_reset_notify.h>
32 #include <sys/disp.h>
33 #include <sys/byteorder.h>
34 #include <sys/atomic.h>
35 #include <sys/ethernet.h>
36 #include <sys/sdt.h>
37 #include <sys/nvpair.h>
38 #include <sys/zone.h>
39 #include <sys/id_space.h>
40 
41 #include <sys/stmf.h>
42 #include <sys/lpif.h>
43 #include <sys/portif.h>
44 #include <sys/stmf_ioctl.h>
45 #include <sys/pppt_ic_if.h>
46 
47 #include "stmf_impl.h"
48 #include "lun_map.h"
49 #include "stmf_state.h"
50 #include "stmf_stats.h"
51 
52 /*
53  * Lock order:
54  * stmf_state_lock --> ilport_lock/iss_lockp --> ilu_task_lock
55  */
56 
57 static uint64_t stmf_session_counter = 0;
58 static uint16_t stmf_rtpid_counter = 0;
59 /* start messages at 1 */
60 static uint64_t stmf_proxy_msg_id = 1;
61 #define	MSG_ID_TM_BIT	0x8000000000000000
62 
63 static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
64 static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
65 static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
66 	void **result);
67 static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp);
68 static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp);
69 static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
70 	cred_t *credp, int *rval);
71 static int stmf_get_stmf_state(stmf_state_desc_t *std);
72 static int stmf_set_stmf_state(stmf_state_desc_t *std);
73 static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu,
74     char *info);
75 static int stmf_set_alua_state(stmf_alua_state_desc_t *alua_state);
76 static void stmf_get_alua_state(stmf_alua_state_desc_t *alua_state);
77 
78 static void stmf_task_audit(stmf_i_scsi_task_t *itask,
79     task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf);
80 
81 stmf_xfer_data_t *stmf_prepare_tpgs_data(uint8_t ilu_alua);
82 void stmf_svc_init();
83 stmf_status_t stmf_svc_fini();
84 void stmf_svc(void *arg);
85 void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info);
86 void stmf_check_freetask();
87 void stmf_abort_target_reset(scsi_task_t *task);
88 stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task,
89 							int target_reset);
90 void stmf_target_reset_poll(struct scsi_task *task);
91 void stmf_handle_lun_reset(scsi_task_t *task);
92 void stmf_handle_target_reset(scsi_task_t *task);
93 void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off);
94 int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
95     uint32_t *err_ret);
96 int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi);
97 int stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
98     uint32_t *err_ret);
99 void stmf_delete_ppd(stmf_pp_data_t *ppd);
100 void stmf_delete_all_ppds();
101 void stmf_trace_clear();
102 void stmf_worker_init();
103 stmf_status_t stmf_worker_fini();
104 void stmf_worker_mgmt();
105 void stmf_worker_task(void *arg);
106 static void stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss);
107 static stmf_status_t stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg,
108     uint32_t type);
109 static stmf_status_t stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg);
110 static stmf_status_t stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg);
111 static stmf_status_t stmf_ic_rx_status(stmf_ic_status_msg_t *msg);
112 static stmf_status_t stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg);
113 void stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s);
114 
115 /* pppt modhandle */
116 ddi_modhandle_t pppt_mod;
117 
118 /* pppt modload imported functions */
119 stmf_ic_reg_port_msg_alloc_func_t ic_reg_port_msg_alloc;
120 stmf_ic_dereg_port_msg_alloc_func_t ic_dereg_port_msg_alloc;
121 stmf_ic_reg_lun_msg_alloc_func_t ic_reg_lun_msg_alloc;
122 stmf_ic_dereg_lun_msg_alloc_func_t ic_dereg_lun_msg_alloc;
123 stmf_ic_lun_active_msg_alloc_func_t ic_lun_active_msg_alloc;
124 stmf_ic_scsi_cmd_msg_alloc_func_t ic_scsi_cmd_msg_alloc;
125 stmf_ic_scsi_data_xfer_done_msg_alloc_func_t ic_scsi_data_xfer_done_msg_alloc;
126 stmf_ic_session_create_msg_alloc_func_t ic_session_reg_msg_alloc;
127 stmf_ic_session_destroy_msg_alloc_func_t ic_session_dereg_msg_alloc;
128 stmf_ic_tx_msg_func_t ic_tx_msg;
129 stmf_ic_msg_free_func_t ic_msg_free;
130 
131 static void stmf_itl_task_start(stmf_i_scsi_task_t *itask);
132 static void stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask);
133 static void stmf_itl_task_done(stmf_i_scsi_task_t *itask);
134 
135 static void stmf_lport_xfer_start(stmf_i_scsi_task_t *itask,
136     stmf_data_buf_t *dbuf);
137 static void stmf_lport_xfer_done(stmf_i_scsi_task_t *itask,
138     stmf_data_buf_t *dbuf);
139 
140 static void stmf_update_kstat_lu_q(scsi_task_t *, void());
141 static void stmf_update_kstat_lport_q(scsi_task_t *, void());
142 static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *);
143 static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *);
144 
145 static int stmf_irport_compare(const void *void_irport1,
146     const void *void_irport2);
147 static stmf_i_remote_port_t *stmf_irport_create(scsi_devid_desc_t *rport_devid);
148 static void stmf_irport_destroy(stmf_i_remote_port_t *irport);
149 static stmf_i_remote_port_t *stmf_irport_register(
150     scsi_devid_desc_t *rport_devid);
151 static stmf_i_remote_port_t *stmf_irport_lookup_locked(
152     scsi_devid_desc_t *rport_devid);
153 static void stmf_irport_deregister(stmf_i_remote_port_t *irport);
154 
155 static void stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks);
156 static void stmf_delete_itl_kstat_by_lport(char *);
157 static void stmf_delete_itl_kstat_by_guid(char *);
158 static int stmf_itl_kstat_compare(const void*, const void*);
159 static stmf_i_itl_kstat_t *stmf_itl_kstat_lookup(char *kstat_nm);
160 static stmf_i_itl_kstat_t *stmf_itl_kstat_create(stmf_itl_data_t *itl,
161     char *nm, scsi_devid_desc_t *lport, scsi_devid_desc_t *lun);
162 
163 extern struct mod_ops mod_driverops;
164 
165 /* =====[ Tunables ]===== */
166 /* Internal tracing */
167 volatile int	stmf_trace_on = 1;
168 volatile int	stmf_trace_buf_size = (1 * 1024 * 1024);
169 /*
170  * The reason default task timeout is 75 is because we want the
171  * host to timeout 1st and mostly host timeout is 60 seconds.
172  */
173 volatile int	stmf_default_task_timeout = 75;
174 /*
175  * Setting this to one means, you are responsible for config load and keeping
176  * things in sync with persistent database.
177  */
178 volatile int	stmf_allow_modunload = 0;
179 
180 volatile int stmf_max_nworkers = 256;
181 volatile int stmf_min_nworkers = 4;
182 volatile int stmf_worker_scale_down_delay = 20;
183 
184 /* === [ Debugging and fault injection ] === */
185 #ifdef	DEBUG
186 volatile int stmf_drop_task_counter = 0;
187 volatile int stmf_drop_buf_counter = 0;
188 
189 #endif
190 
191 stmf_state_t		stmf_state;
192 static stmf_lu_t	*dlun0;
193 
194 static uint8_t stmf_first_zero[] =
195 	{ 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff };
196 static uint8_t stmf_first_one[] =
197 	{ 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 };
198 
199 static kmutex_t	trace_buf_lock;
200 static int	trace_buf_size;
201 static int	trace_buf_curndx;
202 caddr_t	stmf_trace_buf;
203 
204 static enum {
205 	STMF_WORKERS_DISABLED = 0,
206 	STMF_WORKERS_ENABLING,
207 	STMF_WORKERS_ENABLED
208 } stmf_workers_state = STMF_WORKERS_DISABLED;
209 static int stmf_i_max_nworkers;
210 static int stmf_i_min_nworkers;
211 static int stmf_nworkers_cur;		/* # of workers currently running */
212 static int stmf_nworkers_needed;	/* # of workers need to be running */
213 static int stmf_worker_sel_counter = 0;
214 static uint32_t stmf_cur_ntasks = 0;
215 static clock_t stmf_wm_last = 0;
216 /*
217  * This is equal to stmf_nworkers_cur while we are increasing # workers and
218  * stmf_nworkers_needed while we are decreasing the worker count.
219  */
220 static int stmf_nworkers_accepting_cmds;
221 static stmf_worker_t *stmf_workers = NULL;
222 static clock_t stmf_worker_mgmt_delay = 2;
223 static clock_t stmf_worker_scale_down_timer = 0;
224 static int stmf_worker_scale_down_qd = 0;
225 
226 static struct cb_ops stmf_cb_ops = {
227 	stmf_open,			/* open */
228 	stmf_close,			/* close */
229 	nodev,				/* strategy */
230 	nodev,				/* print */
231 	nodev,				/* dump */
232 	nodev,				/* read */
233 	nodev,				/* write */
234 	stmf_ioctl,			/* ioctl */
235 	nodev,				/* devmap */
236 	nodev,				/* mmap */
237 	nodev,				/* segmap */
238 	nochpoll,			/* chpoll */
239 	ddi_prop_op,			/* cb_prop_op */
240 	0,				/* streamtab */
241 	D_NEW | D_MP,			/* cb_flag */
242 	CB_REV,				/* rev */
243 	nodev,				/* aread */
244 	nodev				/* awrite */
245 };
246 
247 static struct dev_ops stmf_ops = {
248 	DEVO_REV,
249 	0,
250 	stmf_getinfo,
251 	nulldev,		/* identify */
252 	nulldev,		/* probe */
253 	stmf_attach,
254 	stmf_detach,
255 	nodev,			/* reset */
256 	&stmf_cb_ops,
257 	NULL,			/* bus_ops */
258 	NULL			/* power */
259 };
260 
261 #define	STMF_NAME		"COMSTAR STMF"
262 #define	STMF_MODULE_NAME	"stmf"
263 
264 static struct modldrv modldrv = {
265 	&mod_driverops,
266 	STMF_NAME,
267 	&stmf_ops
268 };
269 
270 static struct modlinkage modlinkage = {
271 	MODREV_1,
272 	&modldrv,
273 	NULL
274 };
275 
276 int
277 _init(void)
278 {
279 	int ret;
280 
281 	ret = mod_install(&modlinkage);
282 	if (ret)
283 		return (ret);
284 	stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP);
285 	trace_buf_size = stmf_trace_buf_size;
286 	trace_buf_curndx = 0;
287 	mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0);
288 	bzero(&stmf_state, sizeof (stmf_state_t));
289 	/* STMF service is off by default */
290 	stmf_state.stmf_service_running = 0;
291 	/* default lu/lport states are online */
292 	stmf_state.stmf_default_lu_state = STMF_STATE_ONLINE;
293 	stmf_state.stmf_default_lport_state = STMF_STATE_ONLINE;
294 	mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL);
295 	cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL);
296 	stmf_session_counter = (uint64_t)ddi_get_lbolt();
297 	avl_create(&stmf_state.stmf_irportlist,
298 	    stmf_irport_compare, sizeof (stmf_i_remote_port_t),
299 	    offsetof(stmf_i_remote_port_t, irport_ln));
300 	stmf_state.stmf_ilport_inst_space =
301 	    id_space_create("lport-instances", 0, MAX_ILPORT);
302 	stmf_state.stmf_irport_inst_space =
303 	    id_space_create("rport-instances", 0, MAX_IRPORT);
304 	avl_create(&stmf_state.stmf_itl_kstat_list,
305 	    stmf_itl_kstat_compare, sizeof (stmf_i_itl_kstat_t),
306 	    offsetof(stmf_i_itl_kstat_t, iitl_kstat_ln));
307 	stmf_view_init();
308 	stmf_svc_init();
309 	stmf_dlun_init();
310 	return (ret);
311 }
312 
313 int
314 _fini(void)
315 {
316 	int ret;
317 	stmf_i_remote_port_t	*irport;
318 	stmf_i_itl_kstat_t	*ks_itl;
319 	void			*avl_dest_cookie = NULL;
320 
321 	if (stmf_state.stmf_service_running)
322 		return (EBUSY);
323 	if ((!stmf_allow_modunload) &&
324 	    (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) {
325 		return (EBUSY);
326 	}
327 	if (stmf_state.stmf_nlps || stmf_state.stmf_npps) {
328 		return (EBUSY);
329 	}
330 	if (stmf_dlun_fini() != STMF_SUCCESS)
331 		return (EBUSY);
332 	if (stmf_worker_fini() != STMF_SUCCESS) {
333 		stmf_dlun_init();
334 		return (EBUSY);
335 	}
336 	if (stmf_svc_fini() != STMF_SUCCESS) {
337 		stmf_dlun_init();
338 		stmf_worker_init();
339 		return (EBUSY);
340 	}
341 
342 	ret = mod_remove(&modlinkage);
343 	if (ret) {
344 		stmf_svc_init();
345 		stmf_dlun_init();
346 		stmf_worker_init();
347 		return (ret);
348 	}
349 
350 	stmf_view_clear_config();
351 
352 	while ((irport = avl_destroy_nodes(&stmf_state.stmf_irportlist,
353 	    &avl_dest_cookie)) != NULL)
354 		stmf_irport_destroy(irport);
355 	avl_destroy(&stmf_state.stmf_irportlist);
356 	id_space_destroy(stmf_state.stmf_ilport_inst_space);
357 	id_space_destroy(stmf_state.stmf_irport_inst_space);
358 
359 	avl_dest_cookie = NULL;
360 	while ((ks_itl = avl_destroy_nodes(&stmf_state.stmf_itl_kstat_list,
361 	    &avl_dest_cookie)) != NULL) {
362 		stmf_teardown_itl_kstats(ks_itl);
363 		kmem_free(ks_itl, sizeof (ks_itl));
364 	}
365 	avl_destroy(&stmf_state.stmf_itl_kstat_list);
366 
367 	kmem_free(stmf_trace_buf, stmf_trace_buf_size);
368 	mutex_destroy(&trace_buf_lock);
369 	mutex_destroy(&stmf_state.stmf_lock);
370 	cv_destroy(&stmf_state.stmf_cv);
371 	return (ret);
372 }
373 
374 int
375 _info(struct modinfo *modinfop)
376 {
377 	return (mod_info(&modlinkage, modinfop));
378 }
379 
380 /* ARGSUSED */
381 static int
382 stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
383 {
384 	switch (cmd) {
385 	case DDI_INFO_DEVT2DEVINFO:
386 		*result = stmf_state.stmf_dip;
387 		break;
388 	case DDI_INFO_DEVT2INSTANCE:
389 		*result =
390 		    (void *)(uintptr_t)ddi_get_instance(stmf_state.stmf_dip);
391 		break;
392 	default:
393 		return (DDI_FAILURE);
394 	}
395 
396 	return (DDI_SUCCESS);
397 }
398 
399 static int
400 stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
401 {
402 	switch (cmd) {
403 	case DDI_ATTACH:
404 		stmf_state.stmf_dip = dip;
405 
406 		if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0,
407 		    DDI_NT_STMF, 0) != DDI_SUCCESS) {
408 			break;
409 		}
410 		ddi_report_dev(dip);
411 		return (DDI_SUCCESS);
412 	}
413 
414 	return (DDI_FAILURE);
415 }
416 
417 static int
418 stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
419 {
420 	switch (cmd) {
421 	case DDI_DETACH:
422 		ddi_remove_minor_node(dip, 0);
423 		return (DDI_SUCCESS);
424 	}
425 
426 	return (DDI_FAILURE);
427 }
428 
429 /* ARGSUSED */
430 static int
431 stmf_open(dev_t *devp, int flag, int otype, cred_t *credp)
432 {
433 	mutex_enter(&stmf_state.stmf_lock);
434 	if (stmf_state.stmf_exclusive_open) {
435 		mutex_exit(&stmf_state.stmf_lock);
436 		return (EBUSY);
437 	}
438 	if (flag & FEXCL) {
439 		if (stmf_state.stmf_opened) {
440 			mutex_exit(&stmf_state.stmf_lock);
441 			return (EBUSY);
442 		}
443 		stmf_state.stmf_exclusive_open = 1;
444 	}
445 	stmf_state.stmf_opened = 1;
446 	mutex_exit(&stmf_state.stmf_lock);
447 	return (0);
448 }
449 
450 /* ARGSUSED */
451 static int
452 stmf_close(dev_t dev, int flag, int otype, cred_t *credp)
453 {
454 	mutex_enter(&stmf_state.stmf_lock);
455 	stmf_state.stmf_opened = 0;
456 	if (stmf_state.stmf_exclusive_open &&
457 	    (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) {
458 		stmf_state.stmf_config_state = STMF_CONFIG_NONE;
459 		stmf_delete_all_ppds();
460 		stmf_view_clear_config();
461 		stmf_view_init();
462 	}
463 	stmf_state.stmf_exclusive_open = 0;
464 	mutex_exit(&stmf_state.stmf_lock);
465 	return (0);
466 }
467 
468 int
469 stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd,
470 						void **ibuf, void **obuf)
471 {
472 	int ret;
473 
474 	*ibuf = NULL;
475 	*obuf = NULL;
476 	*iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP);
477 
478 	ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode);
479 	if (ret)
480 		return (EFAULT);
481 	if ((*iocd)->stmf_version != STMF_VERSION_1) {
482 		ret = EINVAL;
483 		goto copyin_iocdata_done;
484 	}
485 	if ((*iocd)->stmf_ibuf_size) {
486 		*ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP);
487 		ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf),
488 		    *ibuf, (*iocd)->stmf_ibuf_size, mode);
489 	}
490 	if ((*iocd)->stmf_obuf_size)
491 		*obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP);
492 
493 	if (ret == 0)
494 		return (0);
495 	ret = EFAULT;
496 copyin_iocdata_done:;
497 	if (*obuf) {
498 		kmem_free(*obuf, (*iocd)->stmf_obuf_size);
499 		*obuf = NULL;
500 	}
501 	if (*ibuf) {
502 		kmem_free(*ibuf, (*iocd)->stmf_ibuf_size);
503 		*ibuf = NULL;
504 	}
505 	kmem_free(*iocd, sizeof (stmf_iocdata_t));
506 	return (ret);
507 }
508 
509 int
510 stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf)
511 {
512 	int ret;
513 
514 	if (iocd->stmf_obuf_size) {
515 		ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf,
516 		    iocd->stmf_obuf_size, mode);
517 		if (ret)
518 			return (EFAULT);
519 	}
520 	ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode);
521 	if (ret)
522 		return (EFAULT);
523 	return (0);
524 }
525 
526 /* ARGSUSED */
527 static int
528 stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
529 	cred_t *credp, int *rval)
530 {
531 	stmf_iocdata_t *iocd;
532 	void *ibuf = NULL, *obuf = NULL;
533 	slist_lu_t *luid_list;
534 	slist_target_port_t *lportid_list;
535 	stmf_i_lu_t *ilu;
536 	stmf_i_local_port_t *ilport;
537 	stmf_i_scsi_session_t *iss;
538 	slist_scsi_session_t *iss_list;
539 	sioc_lu_props_t *lup;
540 	sioc_target_port_props_t *lportp;
541 	stmf_ppioctl_data_t *ppi, *ppi_out = NULL;
542 	uint64_t *ppi_token = NULL;
543 	uint8_t *p_id, *id;
544 	stmf_state_desc_t *std;
545 	stmf_status_t ctl_ret;
546 	stmf_state_change_info_t ssi;
547 	int ret = 0;
548 	uint32_t n;
549 	int i;
550 	stmf_group_op_data_t *grp_entry;
551 	stmf_group_name_t *grpname;
552 	stmf_view_op_entry_t *ve;
553 	stmf_id_type_t idtype;
554 	stmf_id_data_t *id_entry;
555 	stmf_id_list_t	*id_list;
556 	stmf_view_entry_t *view_entry;
557 	stmf_set_props_t *stmf_set_props;
558 	uint32_t	veid;
559 	if ((cmd & 0xff000000) != STMF_IOCTL) {
560 		return (ENOTTY);
561 	}
562 
563 	if (drv_priv(credp) != 0) {
564 		return (EPERM);
565 	}
566 
567 	ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf);
568 	if (ret)
569 		return (ret);
570 	iocd->stmf_error = 0;
571 
572 	switch (cmd) {
573 	case STMF_IOCTL_LU_LIST:
574 		/* retrieves both registered/unregistered */
575 		mutex_enter(&stmf_state.stmf_lock);
576 		id_list = &stmf_state.stmf_luid_list;
577 		n = min(id_list->id_count,
578 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
579 		iocd->stmf_obuf_max_nentries = id_list->id_count;
580 		luid_list = (slist_lu_t *)obuf;
581 		id_entry = id_list->idl_head;
582 		for (i = 0; i < n; i++) {
583 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
584 			id_entry = id_entry->id_next;
585 		}
586 
587 		n = iocd->stmf_obuf_size/sizeof (slist_lu_t);
588 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
589 			id = (uint8_t *)ilu->ilu_lu->lu_id;
590 			if (stmf_lookup_id(id_list, 16, id + 4) == NULL) {
591 				iocd->stmf_obuf_max_nentries++;
592 				if (i < n) {
593 					bcopy(id + 4, luid_list[i].lu_guid,
594 					    sizeof (slist_lu_t));
595 					i++;
596 				}
597 			}
598 		}
599 		iocd->stmf_obuf_nentries = i;
600 		mutex_exit(&stmf_state.stmf_lock);
601 		break;
602 
603 	case STMF_IOCTL_REG_LU_LIST:
604 		mutex_enter(&stmf_state.stmf_lock);
605 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus;
606 		n = min(stmf_state.stmf_nlus,
607 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
608 		iocd->stmf_obuf_nentries = n;
609 		ilu = stmf_state.stmf_ilulist;
610 		luid_list = (slist_lu_t *)obuf;
611 		for (i = 0; i < n; i++) {
612 			uint8_t *id;
613 			id = (uint8_t *)ilu->ilu_lu->lu_id;
614 			bcopy(id + 4, luid_list[i].lu_guid, 16);
615 			ilu = ilu->ilu_next;
616 		}
617 		mutex_exit(&stmf_state.stmf_lock);
618 		break;
619 
620 	case STMF_IOCTL_VE_LU_LIST:
621 		mutex_enter(&stmf_state.stmf_lock);
622 		id_list = &stmf_state.stmf_luid_list;
623 		n = min(id_list->id_count,
624 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
625 		iocd->stmf_obuf_max_nentries = id_list->id_count;
626 		iocd->stmf_obuf_nentries = n;
627 		luid_list = (slist_lu_t *)obuf;
628 		id_entry = id_list->idl_head;
629 		for (i = 0; i < n; i++) {
630 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
631 			id_entry = id_entry->id_next;
632 		}
633 		mutex_exit(&stmf_state.stmf_lock);
634 		break;
635 
636 	case STMF_IOCTL_TARGET_PORT_LIST:
637 		mutex_enter(&stmf_state.stmf_lock);
638 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports;
639 		n = min(stmf_state.stmf_nlports,
640 		    (iocd->stmf_obuf_size)/sizeof (slist_target_port_t));
641 		iocd->stmf_obuf_nentries = n;
642 		ilport = stmf_state.stmf_ilportlist;
643 		lportid_list = (slist_target_port_t *)obuf;
644 		for (i = 0; i < n; i++) {
645 			uint8_t *id;
646 			id = (uint8_t *)ilport->ilport_lport->lport_id;
647 			bcopy(id, lportid_list[i].target, id[3] + 4);
648 			ilport = ilport->ilport_next;
649 		}
650 		mutex_exit(&stmf_state.stmf_lock);
651 		break;
652 
653 	case STMF_IOCTL_SESSION_LIST:
654 		p_id = (uint8_t *)ibuf;
655 		if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) ||
656 		    (iocd->stmf_ibuf_size < (p_id[3] + 4))) {
657 			ret = EINVAL;
658 			break;
659 		}
660 		mutex_enter(&stmf_state.stmf_lock);
661 		for (ilport = stmf_state.stmf_ilportlist; ilport; ilport =
662 		    ilport->ilport_next) {
663 			uint8_t *id;
664 			id = (uint8_t *)ilport->ilport_lport->lport_id;
665 			if ((p_id[3] == id[3]) &&
666 			    (bcmp(p_id + 4, id + 4, id[3]) == 0)) {
667 				break;
668 			}
669 		}
670 		if (ilport == NULL) {
671 			mutex_exit(&stmf_state.stmf_lock);
672 			ret = ENOENT;
673 			break;
674 		}
675 		iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions;
676 		n = min(ilport->ilport_nsessions,
677 		    (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t));
678 		iocd->stmf_obuf_nentries = n;
679 		iss = ilport->ilport_ss_list;
680 		iss_list = (slist_scsi_session_t *)obuf;
681 		for (i = 0; i < n; i++) {
682 			uint8_t *id;
683 			id = (uint8_t *)iss->iss_ss->ss_rport_id;
684 			bcopy(id, iss_list[i].initiator, id[3] + 4);
685 			iss_list[i].creation_time = (uint32_t)
686 			    iss->iss_creation_time;
687 			if (iss->iss_ss->ss_rport_alias) {
688 				(void) strncpy(iss_list[i].alias,
689 				    iss->iss_ss->ss_rport_alias, 255);
690 				iss_list[i].alias[255] = 0;
691 			} else {
692 				iss_list[i].alias[0] = 0;
693 			}
694 			iss = iss->iss_next;
695 		}
696 		mutex_exit(&stmf_state.stmf_lock);
697 		break;
698 
699 	case STMF_IOCTL_GET_LU_PROPERTIES:
700 		p_id = (uint8_t *)ibuf;
701 		if ((iocd->stmf_ibuf_size < 16) ||
702 		    (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) ||
703 		    (p_id[0] == 0)) {
704 			ret = EINVAL;
705 			break;
706 		}
707 		mutex_enter(&stmf_state.stmf_lock);
708 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
709 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
710 				break;
711 		}
712 		if (ilu == NULL) {
713 			mutex_exit(&stmf_state.stmf_lock);
714 			ret = ENOENT;
715 			break;
716 		}
717 		lup = (sioc_lu_props_t *)obuf;
718 		bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16);
719 		lup->lu_state = ilu->ilu_state & 0x0f;
720 		lup->lu_present = 1; /* XXX */
721 		(void) strncpy(lup->lu_provider_name,
722 		    ilu->ilu_lu->lu_lp->lp_name, 255);
723 		lup->lu_provider_name[254] = 0;
724 		if (ilu->ilu_lu->lu_alias) {
725 			(void) strncpy(lup->lu_alias,
726 			    ilu->ilu_lu->lu_alias, 255);
727 			lup->lu_alias[255] = 0;
728 		} else {
729 			lup->lu_alias[0] = 0;
730 		}
731 		mutex_exit(&stmf_state.stmf_lock);
732 		break;
733 
734 	case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES:
735 		p_id = (uint8_t *)ibuf;
736 		if ((p_id == NULL) ||
737 		    (iocd->stmf_ibuf_size < (p_id[3] + 4)) ||
738 		    (iocd->stmf_obuf_size <
739 		    sizeof (sioc_target_port_props_t))) {
740 			ret = EINVAL;
741 			break;
742 		}
743 		mutex_enter(&stmf_state.stmf_lock);
744 		for (ilport = stmf_state.stmf_ilportlist; ilport;
745 		    ilport = ilport->ilport_next) {
746 			uint8_t *id;
747 			id = (uint8_t *)ilport->ilport_lport->lport_id;
748 			if ((p_id[3] == id[3]) &&
749 			    (bcmp(p_id+4, id+4, id[3]) == 0))
750 				break;
751 		}
752 		if (ilport == NULL) {
753 			mutex_exit(&stmf_state.stmf_lock);
754 			ret = ENOENT;
755 			break;
756 		}
757 		lportp = (sioc_target_port_props_t *)obuf;
758 		bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id,
759 		    ilport->ilport_lport->lport_id->ident_length + 4);
760 		lportp->tgt_state = ilport->ilport_state & 0x0f;
761 		lportp->tgt_present = 1; /* XXX */
762 		(void) strncpy(lportp->tgt_provider_name,
763 		    ilport->ilport_lport->lport_pp->pp_name, 255);
764 		lportp->tgt_provider_name[254] = 0;
765 		if (ilport->ilport_lport->lport_alias) {
766 			(void) strncpy(lportp->tgt_alias,
767 			    ilport->ilport_lport->lport_alias, 255);
768 			lportp->tgt_alias[255] = 0;
769 		} else {
770 			lportp->tgt_alias[0] = 0;
771 		}
772 		mutex_exit(&stmf_state.stmf_lock);
773 		break;
774 
775 	case STMF_IOCTL_SET_STMF_STATE:
776 		if ((ibuf == NULL) ||
777 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
778 			ret = EINVAL;
779 			break;
780 		}
781 		ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf);
782 		break;
783 
784 	case STMF_IOCTL_GET_STMF_STATE:
785 		if ((obuf == NULL) ||
786 		    (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) {
787 			ret = EINVAL;
788 			break;
789 		}
790 		ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf);
791 		break;
792 
793 	case STMF_IOCTL_SET_ALUA_STATE:
794 		if ((ibuf == NULL) ||
795 		    (iocd->stmf_ibuf_size < sizeof (stmf_alua_state_desc_t))) {
796 			ret = EINVAL;
797 			break;
798 		}
799 		ret = stmf_set_alua_state((stmf_alua_state_desc_t *)ibuf);
800 		break;
801 
802 	case STMF_IOCTL_GET_ALUA_STATE:
803 		if ((obuf == NULL) ||
804 		    (iocd->stmf_obuf_size < sizeof (stmf_alua_state_desc_t))) {
805 			ret = EINVAL;
806 			break;
807 		}
808 		stmf_get_alua_state((stmf_alua_state_desc_t *)obuf);
809 		break;
810 
811 	case STMF_IOCTL_SET_LU_STATE:
812 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
813 		ssi.st_additional_info = NULL;
814 		std = (stmf_state_desc_t *)ibuf;
815 		if ((ibuf == NULL) ||
816 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
817 			ret = EINVAL;
818 			break;
819 		}
820 		p_id = std->ident;
821 		mutex_enter(&stmf_state.stmf_lock);
822 		if (stmf_state.stmf_inventory_locked) {
823 			mutex_exit(&stmf_state.stmf_lock);
824 			ret = EBUSY;
825 			break;
826 		}
827 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
828 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
829 				break;
830 		}
831 		if (ilu == NULL) {
832 			mutex_exit(&stmf_state.stmf_lock);
833 			ret = ENOENT;
834 			break;
835 		}
836 		stmf_state.stmf_inventory_locked = 1;
837 		mutex_exit(&stmf_state.stmf_lock);
838 		cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE :
839 		    STMF_CMD_LU_OFFLINE;
840 		ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi);
841 		if (ctl_ret == STMF_ALREADY)
842 			ret = 0;
843 		else if (ctl_ret == STMF_BUSY)
844 			ret = EBUSY;
845 		else if (ctl_ret != STMF_SUCCESS)
846 			ret = EIO;
847 		mutex_enter(&stmf_state.stmf_lock);
848 		stmf_state.stmf_inventory_locked = 0;
849 		mutex_exit(&stmf_state.stmf_lock);
850 		break;
851 
852 	case STMF_IOCTL_SET_STMF_PROPS:
853 		if ((ibuf == NULL) ||
854 		    (iocd->stmf_ibuf_size < sizeof (stmf_set_props_t))) {
855 			ret = EINVAL;
856 			break;
857 		}
858 		stmf_set_props = (stmf_set_props_t *)ibuf;
859 		mutex_enter(&stmf_state.stmf_lock);
860 		if ((stmf_set_props->default_lu_state_value ==
861 		    STMF_STATE_OFFLINE) ||
862 		    (stmf_set_props->default_lu_state_value ==
863 		    STMF_STATE_ONLINE)) {
864 			stmf_state.stmf_default_lu_state =
865 			    stmf_set_props->default_lu_state_value;
866 		}
867 		if ((stmf_set_props->default_target_state_value ==
868 		    STMF_STATE_OFFLINE) ||
869 		    (stmf_set_props->default_target_state_value ==
870 		    STMF_STATE_ONLINE)) {
871 			stmf_state.stmf_default_lport_state =
872 			    stmf_set_props->default_target_state_value;
873 		}
874 
875 		mutex_exit(&stmf_state.stmf_lock);
876 		break;
877 
878 	case STMF_IOCTL_SET_TARGET_PORT_STATE:
879 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
880 		ssi.st_additional_info = NULL;
881 		std = (stmf_state_desc_t *)ibuf;
882 		if ((ibuf == NULL) ||
883 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
884 			ret = EINVAL;
885 			break;
886 		}
887 		p_id = std->ident;
888 		mutex_enter(&stmf_state.stmf_lock);
889 		if (stmf_state.stmf_inventory_locked) {
890 			mutex_exit(&stmf_state.stmf_lock);
891 			ret = EBUSY;
892 			break;
893 		}
894 		for (ilport = stmf_state.stmf_ilportlist; ilport;
895 		    ilport = ilport->ilport_next) {
896 			uint8_t *id;
897 			id = (uint8_t *)ilport->ilport_lport->lport_id;
898 			if ((id[3] == p_id[3]) &&
899 			    (bcmp(id+4, p_id+4, id[3]) == 0)) {
900 				break;
901 			}
902 		}
903 		if (ilport == NULL) {
904 			mutex_exit(&stmf_state.stmf_lock);
905 			ret = ENOENT;
906 			break;
907 		}
908 		stmf_state.stmf_inventory_locked = 1;
909 		mutex_exit(&stmf_state.stmf_lock);
910 		cmd = (std->state == STMF_STATE_ONLINE) ?
911 		    STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE;
912 		ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi);
913 		if (ctl_ret == STMF_ALREADY)
914 			ret = 0;
915 		else if (ctl_ret == STMF_BUSY)
916 			ret = EBUSY;
917 		else if (ctl_ret != STMF_SUCCESS)
918 			ret = EIO;
919 		mutex_enter(&stmf_state.stmf_lock);
920 		stmf_state.stmf_inventory_locked = 0;
921 		mutex_exit(&stmf_state.stmf_lock);
922 		break;
923 
924 	case STMF_IOCTL_ADD_HG_ENTRY:
925 		idtype = STMF_ID_TYPE_HOST;
926 		/* FALLTHROUGH */
927 	case STMF_IOCTL_ADD_TG_ENTRY:
928 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
929 			ret = EACCES;
930 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
931 			break;
932 		}
933 		if (cmd == STMF_IOCTL_ADD_TG_ENTRY) {
934 			idtype = STMF_ID_TYPE_TARGET;
935 		}
936 		grp_entry = (stmf_group_op_data_t *)ibuf;
937 		if ((ibuf == NULL) ||
938 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
939 			ret = EINVAL;
940 			break;
941 		}
942 		if (grp_entry->group.name[0] == '*') {
943 			ret = EINVAL;
944 			break; /* not allowed */
945 		}
946 		mutex_enter(&stmf_state.stmf_lock);
947 		ret = stmf_add_group_member(grp_entry->group.name,
948 		    grp_entry->group.name_size,
949 		    grp_entry->ident + 4,
950 		    grp_entry->ident[3],
951 		    idtype,
952 		    &iocd->stmf_error);
953 		mutex_exit(&stmf_state.stmf_lock);
954 		break;
955 	case STMF_IOCTL_REMOVE_HG_ENTRY:
956 		idtype = STMF_ID_TYPE_HOST;
957 		/* FALLTHROUGH */
958 	case STMF_IOCTL_REMOVE_TG_ENTRY:
959 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
960 			ret = EACCES;
961 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
962 			break;
963 		}
964 		if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) {
965 			idtype = STMF_ID_TYPE_TARGET;
966 		}
967 		grp_entry = (stmf_group_op_data_t *)ibuf;
968 		if ((ibuf == NULL) ||
969 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
970 			ret = EINVAL;
971 			break;
972 		}
973 		if (grp_entry->group.name[0] == '*') {
974 			ret = EINVAL;
975 			break; /* not allowed */
976 		}
977 		mutex_enter(&stmf_state.stmf_lock);
978 		ret = stmf_remove_group_member(grp_entry->group.name,
979 		    grp_entry->group.name_size,
980 		    grp_entry->ident + 4,
981 		    grp_entry->ident[3],
982 		    idtype,
983 		    &iocd->stmf_error);
984 		mutex_exit(&stmf_state.stmf_lock);
985 		break;
986 	case STMF_IOCTL_CREATE_HOST_GROUP:
987 		idtype = STMF_ID_TYPE_HOST_GROUP;
988 		/* FALLTHROUGH */
989 	case STMF_IOCTL_CREATE_TARGET_GROUP:
990 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
991 			ret = EACCES;
992 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
993 			break;
994 		}
995 		grpname = (stmf_group_name_t *)ibuf;
996 
997 		if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP)
998 			idtype = STMF_ID_TYPE_TARGET_GROUP;
999 		if ((ibuf == NULL) ||
1000 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1001 			ret = EINVAL;
1002 			break;
1003 		}
1004 		if (grpname->name[0] == '*') {
1005 			ret = EINVAL;
1006 			break; /* not allowed */
1007 		}
1008 		mutex_enter(&stmf_state.stmf_lock);
1009 		ret = stmf_add_group(grpname->name,
1010 		    grpname->name_size, idtype, &iocd->stmf_error);
1011 		mutex_exit(&stmf_state.stmf_lock);
1012 		break;
1013 	case STMF_IOCTL_REMOVE_HOST_GROUP:
1014 		idtype = STMF_ID_TYPE_HOST_GROUP;
1015 		/* FALLTHROUGH */
1016 	case STMF_IOCTL_REMOVE_TARGET_GROUP:
1017 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1018 			ret = EACCES;
1019 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1020 			break;
1021 		}
1022 		grpname = (stmf_group_name_t *)ibuf;
1023 		if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP)
1024 			idtype = STMF_ID_TYPE_TARGET_GROUP;
1025 		if ((ibuf == NULL) ||
1026 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1027 			ret = EINVAL;
1028 			break;
1029 		}
1030 		if (grpname->name[0] == '*') {
1031 			ret = EINVAL;
1032 			break; /* not allowed */
1033 		}
1034 		mutex_enter(&stmf_state.stmf_lock);
1035 		ret = stmf_remove_group(grpname->name,
1036 		    grpname->name_size, idtype, &iocd->stmf_error);
1037 		mutex_exit(&stmf_state.stmf_lock);
1038 		break;
1039 	case STMF_IOCTL_VALIDATE_VIEW:
1040 	case STMF_IOCTL_ADD_VIEW_ENTRY:
1041 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1042 			ret = EACCES;
1043 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1044 			break;
1045 		}
1046 		ve = (stmf_view_op_entry_t *)ibuf;
1047 		if ((ibuf == NULL) ||
1048 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
1049 			ret = EINVAL;
1050 			break;
1051 		}
1052 		if (!ve->ve_lu_number_valid)
1053 			ve->ve_lu_nbr[2] = 0xFF;
1054 		if (ve->ve_all_hosts) {
1055 			ve->ve_host_group.name[0] = '*';
1056 			ve->ve_host_group.name_size = 1;
1057 		}
1058 		if (ve->ve_all_targets) {
1059 			ve->ve_target_group.name[0] = '*';
1060 			ve->ve_target_group.name_size = 1;
1061 		}
1062 		if (ve->ve_ndx_valid)
1063 			veid = ve->ve_ndx;
1064 		else
1065 			veid = 0xffffffff;
1066 		mutex_enter(&stmf_state.stmf_lock);
1067 		if (cmd == STMF_IOCTL_ADD_VIEW_ENTRY) {
1068 			ret = stmf_add_ve(ve->ve_host_group.name,
1069 			    ve->ve_host_group.name_size,
1070 			    ve->ve_target_group.name,
1071 			    ve->ve_target_group.name_size,
1072 			    ve->ve_guid,
1073 			    &veid,
1074 			    ve->ve_lu_nbr,
1075 			    &iocd->stmf_error);
1076 		} else {  /* STMF_IOCTL_VALIDATE_VIEW */
1077 			ret = stmf_validate_lun_ve(ve->ve_host_group.name,
1078 			    ve->ve_host_group.name_size,
1079 			    ve->ve_target_group.name,
1080 			    ve->ve_target_group.name_size,
1081 			    ve->ve_lu_nbr,
1082 			    &iocd->stmf_error);
1083 		}
1084 		mutex_exit(&stmf_state.stmf_lock);
1085 		if (ret == 0 &&
1086 		    (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) &&
1087 		    iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) {
1088 			stmf_view_op_entry_t *ve_ret =
1089 			    (stmf_view_op_entry_t *)obuf;
1090 			iocd->stmf_obuf_nentries = 1;
1091 			iocd->stmf_obuf_max_nentries = 1;
1092 			if (!ve->ve_ndx_valid) {
1093 				ve_ret->ve_ndx = veid;
1094 				ve_ret->ve_ndx_valid = 1;
1095 			}
1096 			if (!ve->ve_lu_number_valid) {
1097 				ve_ret->ve_lu_number_valid = 1;
1098 				bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8);
1099 			}
1100 		}
1101 		break;
1102 	case STMF_IOCTL_REMOVE_VIEW_ENTRY:
1103 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1104 			ret = EACCES;
1105 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1106 			break;
1107 		}
1108 		ve = (stmf_view_op_entry_t *)ibuf;
1109 		if ((ibuf == NULL) ||
1110 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
1111 			ret = EINVAL;
1112 			break;
1113 		}
1114 		if (!ve->ve_ndx_valid) {
1115 			ret = EINVAL;
1116 			break;
1117 		}
1118 		mutex_enter(&stmf_state.stmf_lock);
1119 		ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx,
1120 		    &iocd->stmf_error);
1121 		mutex_exit(&stmf_state.stmf_lock);
1122 		break;
1123 	case STMF_IOCTL_GET_HG_LIST:
1124 		id_list = &stmf_state.stmf_hg_list;
1125 		/* FALLTHROUGH */
1126 	case STMF_IOCTL_GET_TG_LIST:
1127 		if (cmd == STMF_IOCTL_GET_TG_LIST)
1128 			id_list = &stmf_state.stmf_tg_list;
1129 		mutex_enter(&stmf_state.stmf_lock);
1130 		iocd->stmf_obuf_max_nentries = id_list->id_count;
1131 		n = min(id_list->id_count,
1132 		    (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t));
1133 		iocd->stmf_obuf_nentries = n;
1134 		id_entry = id_list->idl_head;
1135 		grpname = (stmf_group_name_t *)obuf;
1136 		for (i = 0; i < n; i++) {
1137 			if (id_entry->id_data[0] == '*') {
1138 				if (iocd->stmf_obuf_nentries > 0) {
1139 					iocd->stmf_obuf_nentries--;
1140 				}
1141 				id_entry = id_entry->id_next;
1142 				continue;
1143 			}
1144 			grpname->name_size = id_entry->id_data_size;
1145 			bcopy(id_entry->id_data, grpname->name,
1146 			    id_entry->id_data_size);
1147 			grpname++;
1148 			id_entry = id_entry->id_next;
1149 		}
1150 		mutex_exit(&stmf_state.stmf_lock);
1151 		break;
1152 	case STMF_IOCTL_GET_HG_ENTRIES:
1153 		id_list = &stmf_state.stmf_hg_list;
1154 		/* FALLTHROUGH */
1155 	case STMF_IOCTL_GET_TG_ENTRIES:
1156 		grpname = (stmf_group_name_t *)ibuf;
1157 		if ((ibuf == NULL) ||
1158 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1159 			ret = EINVAL;
1160 			break;
1161 		}
1162 		if (cmd == STMF_IOCTL_GET_TG_ENTRIES) {
1163 			id_list = &stmf_state.stmf_tg_list;
1164 		}
1165 		mutex_enter(&stmf_state.stmf_lock);
1166 		id_entry = stmf_lookup_id(id_list, grpname->name_size,
1167 		    grpname->name);
1168 		if (!id_entry)
1169 			ret = ENODEV;
1170 		else {
1171 			stmf_ge_ident_t *grp_entry;
1172 			id_list = (stmf_id_list_t *)id_entry->id_impl_specific;
1173 			iocd->stmf_obuf_max_nentries = id_list->id_count;
1174 			n = min(id_list->id_count,
1175 			    iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t));
1176 			iocd->stmf_obuf_nentries = n;
1177 			id_entry = id_list->idl_head;
1178 			grp_entry = (stmf_ge_ident_t *)obuf;
1179 			for (i = 0; i < n; i++) {
1180 				bcopy(id_entry->id_data, grp_entry->ident,
1181 				    id_entry->id_data_size);
1182 				grp_entry->ident_size = id_entry->id_data_size;
1183 				id_entry = id_entry->id_next;
1184 				grp_entry++;
1185 			}
1186 		}
1187 		mutex_exit(&stmf_state.stmf_lock);
1188 		break;
1189 
1190 	case STMF_IOCTL_GET_VE_LIST:
1191 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1192 		mutex_enter(&stmf_state.stmf_lock);
1193 		ve = (stmf_view_op_entry_t *)obuf;
1194 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
1195 		    id_entry; id_entry = id_entry->id_next) {
1196 			for (view_entry = (stmf_view_entry_t *)
1197 			    id_entry->id_impl_specific; view_entry;
1198 			    view_entry = view_entry->ve_next) {
1199 				iocd->stmf_obuf_max_nentries++;
1200 				if (iocd->stmf_obuf_nentries >= n)
1201 					continue;
1202 				ve->ve_ndx_valid = 1;
1203 				ve->ve_ndx = view_entry->ve_id;
1204 				ve->ve_lu_number_valid = 1;
1205 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1206 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1207 				    view_entry->ve_luid->id_data_size);
1208 				if (view_entry->ve_hg->id_data[0] == '*') {
1209 					ve->ve_all_hosts = 1;
1210 				} else {
1211 					bcopy(view_entry->ve_hg->id_data,
1212 					    ve->ve_host_group.name,
1213 					    view_entry->ve_hg->id_data_size);
1214 					ve->ve_host_group.name_size =
1215 					    view_entry->ve_hg->id_data_size;
1216 				}
1217 
1218 				if (view_entry->ve_tg->id_data[0] == '*') {
1219 					ve->ve_all_targets = 1;
1220 				} else {
1221 					bcopy(view_entry->ve_tg->id_data,
1222 					    ve->ve_target_group.name,
1223 					    view_entry->ve_tg->id_data_size);
1224 					ve->ve_target_group.name_size =
1225 					    view_entry->ve_tg->id_data_size;
1226 				}
1227 				ve++;
1228 				iocd->stmf_obuf_nentries++;
1229 			}
1230 		}
1231 		mutex_exit(&stmf_state.stmf_lock);
1232 		break;
1233 
1234 	case STMF_IOCTL_LU_VE_LIST:
1235 		p_id = (uint8_t *)ibuf;
1236 		if ((iocd->stmf_ibuf_size != 16) ||
1237 		    (iocd->stmf_obuf_size < sizeof (stmf_view_op_entry_t))) {
1238 			ret = EINVAL;
1239 			break;
1240 		}
1241 
1242 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1243 		mutex_enter(&stmf_state.stmf_lock);
1244 		ve = (stmf_view_op_entry_t *)obuf;
1245 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
1246 		    id_entry; id_entry = id_entry->id_next) {
1247 			if (bcmp(id_entry->id_data, p_id, 16) != 0)
1248 				continue;
1249 			for (view_entry = (stmf_view_entry_t *)
1250 			    id_entry->id_impl_specific; view_entry;
1251 			    view_entry = view_entry->ve_next) {
1252 				iocd->stmf_obuf_max_nentries++;
1253 				if (iocd->stmf_obuf_nentries >= n)
1254 					continue;
1255 				ve->ve_ndx_valid = 1;
1256 				ve->ve_ndx = view_entry->ve_id;
1257 				ve->ve_lu_number_valid = 1;
1258 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1259 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1260 				    view_entry->ve_luid->id_data_size);
1261 				if (view_entry->ve_hg->id_data[0] == '*') {
1262 					ve->ve_all_hosts = 1;
1263 				} else {
1264 					bcopy(view_entry->ve_hg->id_data,
1265 					    ve->ve_host_group.name,
1266 					    view_entry->ve_hg->id_data_size);
1267 					ve->ve_host_group.name_size =
1268 					    view_entry->ve_hg->id_data_size;
1269 				}
1270 
1271 				if (view_entry->ve_tg->id_data[0] == '*') {
1272 					ve->ve_all_targets = 1;
1273 				} else {
1274 					bcopy(view_entry->ve_tg->id_data,
1275 					    ve->ve_target_group.name,
1276 					    view_entry->ve_tg->id_data_size);
1277 					ve->ve_target_group.name_size =
1278 					    view_entry->ve_tg->id_data_size;
1279 				}
1280 				ve++;
1281 				iocd->stmf_obuf_nentries++;
1282 			}
1283 			break;
1284 		}
1285 		mutex_exit(&stmf_state.stmf_lock);
1286 		break;
1287 
1288 	case STMF_IOCTL_LOAD_PP_DATA:
1289 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1290 			ret = EACCES;
1291 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1292 			break;
1293 		}
1294 		ppi = (stmf_ppioctl_data_t *)ibuf;
1295 		if ((ppi == NULL) ||
1296 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1297 			ret = EINVAL;
1298 			break;
1299 		}
1300 		/* returned token */
1301 		ppi_token = (uint64_t *)obuf;
1302 		if ((ppi_token == NULL) ||
1303 		    (iocd->stmf_obuf_size < sizeof (uint64_t))) {
1304 			ret = EINVAL;
1305 			break;
1306 		}
1307 		ret = stmf_load_ppd_ioctl(ppi, ppi_token, &iocd->stmf_error);
1308 		break;
1309 
1310 	case STMF_IOCTL_GET_PP_DATA:
1311 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1312 			ret = EACCES;
1313 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1314 			break;
1315 		}
1316 		ppi = (stmf_ppioctl_data_t *)ibuf;
1317 		if (ppi == NULL ||
1318 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1319 			ret = EINVAL;
1320 			break;
1321 		}
1322 		ppi_out = (stmf_ppioctl_data_t *)obuf;
1323 		if ((ppi_out == NULL) ||
1324 		    (iocd->stmf_obuf_size < sizeof (stmf_ppioctl_data_t))) {
1325 			ret = EINVAL;
1326 			break;
1327 		}
1328 		ret = stmf_get_ppd_ioctl(ppi, ppi_out, &iocd->stmf_error);
1329 		break;
1330 
1331 	case STMF_IOCTL_CLEAR_PP_DATA:
1332 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1333 			ret = EACCES;
1334 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1335 			break;
1336 		}
1337 		ppi = (stmf_ppioctl_data_t *)ibuf;
1338 		if ((ppi == NULL) ||
1339 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1340 			ret = EINVAL;
1341 			break;
1342 		}
1343 		ret = stmf_delete_ppd_ioctl(ppi);
1344 		break;
1345 
1346 	case STMF_IOCTL_CLEAR_TRACE:
1347 		stmf_trace_clear();
1348 		break;
1349 
1350 	case STMF_IOCTL_ADD_TRACE:
1351 		if (iocd->stmf_ibuf_size && ibuf) {
1352 			((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0;
1353 			stmf_trace("\nstradm", "%s\n", ibuf);
1354 		}
1355 		break;
1356 
1357 	case STMF_IOCTL_GET_TRACE_POSITION:
1358 		if (obuf && (iocd->stmf_obuf_size > 3)) {
1359 			mutex_enter(&trace_buf_lock);
1360 			*((int *)obuf) = trace_buf_curndx;
1361 			mutex_exit(&trace_buf_lock);
1362 		} else {
1363 			ret = EINVAL;
1364 		}
1365 		break;
1366 
1367 	case STMF_IOCTL_GET_TRACE:
1368 		if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) {
1369 			ret = EINVAL;
1370 			break;
1371 		}
1372 		i = *((int *)ibuf);
1373 		if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) >
1374 		    trace_buf_size)) {
1375 			ret = EINVAL;
1376 			break;
1377 		}
1378 		mutex_enter(&trace_buf_lock);
1379 		bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size);
1380 		mutex_exit(&trace_buf_lock);
1381 		break;
1382 
1383 	default:
1384 		ret = ENOTTY;
1385 	}
1386 
1387 	if (ret == 0) {
1388 		ret = stmf_copyout_iocdata(data, mode, iocd, obuf);
1389 	} else if (iocd->stmf_error) {
1390 		(void) stmf_copyout_iocdata(data, mode, iocd, obuf);
1391 	}
1392 	if (obuf) {
1393 		kmem_free(obuf, iocd->stmf_obuf_size);
1394 		obuf = NULL;
1395 	}
1396 	if (ibuf) {
1397 		kmem_free(ibuf, iocd->stmf_ibuf_size);
1398 		ibuf = NULL;
1399 	}
1400 	kmem_free(iocd, sizeof (stmf_iocdata_t));
1401 	return (ret);
1402 }
1403 
1404 static int
1405 stmf_get_service_state()
1406 {
1407 	stmf_i_local_port_t *ilport;
1408 	stmf_i_lu_t *ilu;
1409 	int online = 0;
1410 	int offline = 0;
1411 	int onlining = 0;
1412 	int offlining = 0;
1413 
1414 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1415 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1416 	    ilport = ilport->ilport_next) {
1417 		if (ilport->ilport_state == STMF_STATE_OFFLINE)
1418 			offline++;
1419 		else if (ilport->ilport_state == STMF_STATE_ONLINE)
1420 			online++;
1421 		else if (ilport->ilport_state == STMF_STATE_ONLINING)
1422 			onlining++;
1423 		else if (ilport->ilport_state == STMF_STATE_OFFLINING)
1424 			offlining++;
1425 	}
1426 
1427 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1428 	    ilu = ilu->ilu_next) {
1429 		if (ilu->ilu_state == STMF_STATE_OFFLINE)
1430 			offline++;
1431 		else if (ilu->ilu_state == STMF_STATE_ONLINE)
1432 			online++;
1433 		else if (ilu->ilu_state == STMF_STATE_ONLINING)
1434 			onlining++;
1435 		else if (ilu->ilu_state == STMF_STATE_OFFLINING)
1436 			offlining++;
1437 	}
1438 
1439 	if (stmf_state.stmf_service_running) {
1440 		if (onlining)
1441 			return (STMF_STATE_ONLINING);
1442 		else
1443 			return (STMF_STATE_ONLINE);
1444 	}
1445 
1446 	if (offlining) {
1447 		return (STMF_STATE_OFFLINING);
1448 	}
1449 
1450 	return (STMF_STATE_OFFLINE);
1451 }
1452 
1453 static int
1454 stmf_set_stmf_state(stmf_state_desc_t *std)
1455 {
1456 	stmf_i_local_port_t *ilport;
1457 	stmf_i_lu_t *ilu;
1458 	stmf_state_change_info_t ssi;
1459 	int svc_state;
1460 
1461 	ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
1462 	ssi.st_additional_info = NULL;
1463 
1464 	mutex_enter(&stmf_state.stmf_lock);
1465 	if (!stmf_state.stmf_exclusive_open) {
1466 		mutex_exit(&stmf_state.stmf_lock);
1467 		return (EACCES);
1468 	}
1469 
1470 	if (stmf_state.stmf_inventory_locked) {
1471 		mutex_exit(&stmf_state.stmf_lock);
1472 		return (EBUSY);
1473 	}
1474 
1475 	if ((std->state != STMF_STATE_ONLINE) &&
1476 	    (std->state != STMF_STATE_OFFLINE)) {
1477 		mutex_exit(&stmf_state.stmf_lock);
1478 		return (EINVAL);
1479 	}
1480 
1481 	svc_state = stmf_get_service_state();
1482 	if ((svc_state == STMF_STATE_OFFLINING) ||
1483 	    (svc_state == STMF_STATE_ONLINING)) {
1484 		mutex_exit(&stmf_state.stmf_lock);
1485 		return (EBUSY);
1486 	}
1487 
1488 	if (svc_state == STMF_STATE_OFFLINE) {
1489 		if (std->config_state == STMF_CONFIG_INIT) {
1490 			if (std->state != STMF_STATE_OFFLINE) {
1491 				mutex_exit(&stmf_state.stmf_lock);
1492 				return (EINVAL);
1493 			}
1494 			stmf_state.stmf_config_state = STMF_CONFIG_INIT;
1495 			stmf_delete_all_ppds();
1496 			stmf_view_clear_config();
1497 			stmf_view_init();
1498 			mutex_exit(&stmf_state.stmf_lock);
1499 			return (0);
1500 		}
1501 		if ((stmf_state.stmf_config_state == STMF_CONFIG_INIT) ||
1502 		    (stmf_state.stmf_config_state == STMF_CONFIG_NONE)) {
1503 			if (std->config_state != STMF_CONFIG_INIT_DONE) {
1504 				mutex_exit(&stmf_state.stmf_lock);
1505 				return (EINVAL);
1506 			}
1507 			stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE;
1508 		}
1509 		if (std->state == STMF_STATE_OFFLINE) {
1510 			mutex_exit(&stmf_state.stmf_lock);
1511 			return (0);
1512 		}
1513 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) {
1514 			mutex_exit(&stmf_state.stmf_lock);
1515 			return (EINVAL);
1516 		}
1517 		stmf_state.stmf_inventory_locked = 1;
1518 		stmf_state.stmf_service_running = 1;
1519 		mutex_exit(&stmf_state.stmf_lock);
1520 
1521 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1522 		    ilport = ilport->ilport_next) {
1523 			if (stmf_state.stmf_default_lport_state !=
1524 			    STMF_STATE_ONLINE)
1525 				continue;
1526 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE,
1527 			    ilport->ilport_lport, &ssi);
1528 		}
1529 
1530 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1531 		    ilu = ilu->ilu_next) {
1532 			if (stmf_state.stmf_default_lu_state !=
1533 			    STMF_STATE_ONLINE)
1534 				continue;
1535 			(void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi);
1536 		}
1537 		mutex_enter(&stmf_state.stmf_lock);
1538 		stmf_state.stmf_inventory_locked = 0;
1539 		mutex_exit(&stmf_state.stmf_lock);
1540 		return (0);
1541 	}
1542 
1543 	/* svc_state is STMF_STATE_ONLINE here */
1544 	if ((std->state != STMF_STATE_OFFLINE) ||
1545 	    (std->config_state == STMF_CONFIG_INIT)) {
1546 		mutex_exit(&stmf_state.stmf_lock);
1547 		return (EACCES);
1548 	}
1549 
1550 	stmf_state.stmf_inventory_locked = 1;
1551 	stmf_state.stmf_service_running = 0;
1552 
1553 	mutex_exit(&stmf_state.stmf_lock);
1554 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1555 	    ilport = ilport->ilport_next) {
1556 		if (ilport->ilport_state != STMF_STATE_ONLINE)
1557 			continue;
1558 		(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE,
1559 		    ilport->ilport_lport, &ssi);
1560 	}
1561 
1562 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1563 	    ilu = ilu->ilu_next) {
1564 		if (ilu->ilu_state != STMF_STATE_ONLINE)
1565 			continue;
1566 		(void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi);
1567 	}
1568 	mutex_enter(&stmf_state.stmf_lock);
1569 	stmf_state.stmf_inventory_locked = 0;
1570 	mutex_exit(&stmf_state.stmf_lock);
1571 	return (0);
1572 }
1573 
1574 static int
1575 stmf_get_stmf_state(stmf_state_desc_t *std)
1576 {
1577 	mutex_enter(&stmf_state.stmf_lock);
1578 	std->state = stmf_get_service_state();
1579 	std->config_state = stmf_state.stmf_config_state;
1580 	mutex_exit(&stmf_state.stmf_lock);
1581 
1582 	return (0);
1583 }
1584 /*
1585  * handles registration message from pppt for a logical unit
1586  */
1587 stmf_status_t
1588 stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg, uint32_t type)
1589 {
1590 	stmf_i_lu_provider_t	*ilp;
1591 	stmf_lu_provider_t	*lp;
1592 	mutex_enter(&stmf_state.stmf_lock);
1593 	for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
1594 		if (strcmp(msg->icrl_lu_provider_name,
1595 		    ilp->ilp_lp->lp_name) == 0) {
1596 			lp = ilp->ilp_lp;
1597 			mutex_exit(&stmf_state.stmf_lock);
1598 			lp->lp_proxy_msg(msg->icrl_lun_id, msg->icrl_cb_arg,
1599 			    msg->icrl_cb_arg_len, type);
1600 			return (STMF_SUCCESS);
1601 		}
1602 	}
1603 	mutex_exit(&stmf_state.stmf_lock);
1604 	return (STMF_SUCCESS);
1605 }
1606 
1607 /*
1608  * handles de-registration message from pppt for a logical unit
1609  */
1610 stmf_status_t
1611 stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg)
1612 {
1613 	stmf_i_lu_provider_t	*ilp;
1614 	stmf_lu_provider_t	*lp;
1615 	mutex_enter(&stmf_state.stmf_lock);
1616 	for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
1617 		if (strcmp(msg->icrl_lu_provider_name,
1618 		    ilp->ilp_lp->lp_name) == 0) {
1619 			lp = ilp->ilp_lp;
1620 			mutex_exit(&stmf_state.stmf_lock);
1621 			lp->lp_proxy_msg(msg->icrl_lun_id, NULL, 0,
1622 			    STMF_MSG_LU_DEREGISTER);
1623 			return (STMF_SUCCESS);
1624 		}
1625 	}
1626 	mutex_exit(&stmf_state.stmf_lock);
1627 	return (STMF_SUCCESS);
1628 }
1629 
1630 /*
1631  * helper function to find a task that matches a task_msgid
1632  */
1633 scsi_task_t *
1634 find_task_from_msgid(uint8_t *lu_id, stmf_ic_msgid_t task_msgid)
1635 {
1636 	stmf_i_lu_t *ilu;
1637 	stmf_i_scsi_task_t *itask;
1638 
1639 	mutex_enter(&stmf_state.stmf_lock);
1640 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
1641 		if (bcmp(lu_id, ilu->ilu_lu->lu_id->ident, 16) == 0) {
1642 			break;
1643 		}
1644 	}
1645 
1646 	if (ilu == NULL) {
1647 		mutex_exit(&stmf_state.stmf_lock);
1648 		return (NULL);
1649 	}
1650 
1651 	mutex_enter(&ilu->ilu_task_lock);
1652 	for (itask = ilu->ilu_tasks; itask != NULL;
1653 	    itask = itask->itask_lu_next) {
1654 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
1655 		    ITASK_BEING_ABORTED)) {
1656 			continue;
1657 		}
1658 		if (itask->itask_proxy_msg_id == task_msgid) {
1659 			break;
1660 		}
1661 	}
1662 	mutex_exit(&ilu->ilu_task_lock);
1663 	mutex_exit(&stmf_state.stmf_lock);
1664 
1665 	if (itask != NULL) {
1666 		return (itask->itask_task);
1667 	} else {
1668 		/* task not found. Likely already aborted. */
1669 		return (NULL);
1670 	}
1671 }
1672 
1673 /*
1674  * message received from pppt/ic
1675  */
1676 stmf_status_t
1677 stmf_msg_rx(stmf_ic_msg_t *msg)
1678 {
1679 	mutex_enter(&stmf_state.stmf_lock);
1680 	if (stmf_state.stmf_alua_state != 1) {
1681 		mutex_exit(&stmf_state.stmf_lock);
1682 		cmn_err(CE_WARN, "stmf alua state is disabled");
1683 		ic_msg_free(msg);
1684 		return (STMF_FAILURE);
1685 	}
1686 	mutex_exit(&stmf_state.stmf_lock);
1687 
1688 	switch (msg->icm_msg_type) {
1689 		case STMF_ICM_REGISTER_LUN:
1690 			(void) stmf_ic_lu_reg(
1691 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
1692 			    STMF_MSG_LU_REGISTER);
1693 			break;
1694 		case STMF_ICM_LUN_ACTIVE:
1695 			(void) stmf_ic_lu_reg(
1696 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
1697 			    STMF_MSG_LU_ACTIVE);
1698 			break;
1699 		case STMF_ICM_DEREGISTER_LUN:
1700 			(void) stmf_ic_lu_dereg(
1701 			    (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg);
1702 			break;
1703 		case STMF_ICM_SCSI_DATA:
1704 			(void) stmf_ic_rx_scsi_data(
1705 			    (stmf_ic_scsi_data_msg_t *)msg->icm_msg);
1706 			break;
1707 		case STMF_ICM_SCSI_STATUS:
1708 			(void) stmf_ic_rx_scsi_status(
1709 			    (stmf_ic_scsi_status_msg_t *)msg->icm_msg);
1710 			break;
1711 		case STMF_ICM_STATUS:
1712 			(void) stmf_ic_rx_status(
1713 			    (stmf_ic_status_msg_t *)msg->icm_msg);
1714 			break;
1715 		default:
1716 			cmn_err(CE_WARN, "unknown message received %d",
1717 			    msg->icm_msg_type);
1718 			ic_msg_free(msg);
1719 			return (STMF_FAILURE);
1720 	}
1721 	ic_msg_free(msg);
1722 	return (STMF_SUCCESS);
1723 }
1724 
1725 stmf_status_t
1726 stmf_ic_rx_status(stmf_ic_status_msg_t *msg)
1727 {
1728 	stmf_i_local_port_t *ilport;
1729 
1730 	if (msg->ics_msg_type != STMF_ICM_REGISTER_PROXY_PORT) {
1731 		/* for now, ignore other message status */
1732 		return (STMF_SUCCESS);
1733 	}
1734 
1735 	if (msg->ics_status != STMF_SUCCESS) {
1736 		return (STMF_SUCCESS);
1737 	}
1738 
1739 	mutex_enter(&stmf_state.stmf_lock);
1740 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1741 	    ilport = ilport->ilport_next) {
1742 		if (msg->ics_msgid == ilport->ilport_reg_msgid) {
1743 			ilport->ilport_proxy_registered = 1;
1744 			break;
1745 		}
1746 	}
1747 	mutex_exit(&stmf_state.stmf_lock);
1748 	return (STMF_SUCCESS);
1749 }
1750 
1751 /*
1752  * handles scsi status message from pppt
1753  */
1754 stmf_status_t
1755 stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg)
1756 {
1757 	scsi_task_t *task;
1758 
1759 	/* is this a task management command */
1760 	if (msg->icss_task_msgid & MSG_ID_TM_BIT) {
1761 		return (STMF_SUCCESS);
1762 	}
1763 
1764 	task = find_task_from_msgid(msg->icss_lun_id, msg->icss_task_msgid);
1765 
1766 	if (task == NULL) {
1767 		return (STMF_SUCCESS);
1768 	}
1769 
1770 	task->task_scsi_status = msg->icss_status;
1771 	task->task_sense_data = msg->icss_sense;
1772 	task->task_sense_length = msg->icss_sense_len;
1773 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
1774 
1775 	return (STMF_SUCCESS);
1776 }
1777 
1778 /*
1779  * handles scsi data message from pppt
1780  */
1781 stmf_status_t
1782 stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg)
1783 {
1784 	stmf_i_scsi_task_t *itask;
1785 	scsi_task_t *task;
1786 	stmf_xfer_data_t *xd = NULL;
1787 	stmf_data_buf_t *dbuf;
1788 	uint32_t sz, minsz, xd_sz, asz;
1789 
1790 	/* is this a task management command */
1791 	if (msg->icsd_task_msgid & MSG_ID_TM_BIT) {
1792 		return (STMF_SUCCESS);
1793 	}
1794 
1795 	task = find_task_from_msgid(msg->icsd_lun_id, msg->icsd_task_msgid);
1796 	if (task == NULL) {
1797 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
1798 		static uint64_t data_msg_id;
1799 		stmf_status_t ic_ret = STMF_FAILURE;
1800 		mutex_enter(&stmf_state.stmf_lock);
1801 		data_msg_id = stmf_proxy_msg_id++;
1802 		mutex_exit(&stmf_state.stmf_lock);
1803 		/*
1804 		 * send xfer done status to pppt
1805 		 * for now, set the session id to 0 as we cannot
1806 		 * ascertain it since we cannot find the task
1807 		 */
1808 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
1809 		    msg->icsd_task_msgid, 0, STMF_FAILURE, data_msg_id);
1810 		if (ic_xfer_done_msg) {
1811 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
1812 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
1813 				cmn_err(CE_WARN, "unable to xmit proxy msg");
1814 			}
1815 		}
1816 		return (STMF_FAILURE);
1817 	}
1818 
1819 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
1820 	dbuf = itask->itask_proxy_dbuf;
1821 
1822 	task->task_cmd_xfer_length += msg->icsd_data_len;
1823 
1824 	if (task->task_additional_flags &
1825 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
1826 		task->task_expected_xfer_length =
1827 		    task->task_cmd_xfer_length;
1828 	}
1829 
1830 	sz = min(task->task_expected_xfer_length,
1831 	    task->task_cmd_xfer_length);
1832 
1833 	xd_sz = msg->icsd_data_len;
1834 	asz = xd_sz + sizeof (*xd) - 4;
1835 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
1836 
1837 	if (xd == NULL) {
1838 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
1839 		    STMF_ALLOC_FAILURE, NULL);
1840 		return (STMF_FAILURE);
1841 	}
1842 
1843 	xd->alloc_size = asz;
1844 	xd->size_left = xd_sz;
1845 	bcopy(msg->icsd_data, xd->buf, xd_sz);
1846 
1847 	sz = min(sz, xd->size_left);
1848 	xd->size_left = sz;
1849 	minsz = min(512, sz);
1850 
1851 	if (dbuf == NULL)
1852 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
1853 	if (dbuf == NULL) {
1854 		kmem_free(xd, xd->alloc_size);
1855 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
1856 		    STMF_ALLOC_FAILURE, NULL);
1857 		return (STMF_FAILURE);
1858 	}
1859 	dbuf->db_lu_private = xd;
1860 	dbuf->db_relative_offset = task->task_nbytes_transferred;
1861 	stmf_xd_to_dbuf(dbuf, 0);
1862 
1863 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
1864 	(void) stmf_xfer_data(task, dbuf, 0);
1865 	return (STMF_SUCCESS);
1866 }
1867 
1868 stmf_status_t
1869 stmf_proxy_scsi_cmd(scsi_task_t *task, stmf_data_buf_t *dbuf)
1870 {
1871 	stmf_i_scsi_task_t *itask =
1872 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
1873 	stmf_i_local_port_t *ilport =
1874 	    (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
1875 	stmf_ic_msg_t *ic_cmd_msg;
1876 	stmf_ic_msg_status_t ic_ret;
1877 	stmf_status_t ret = STMF_FAILURE;
1878 
1879 	if (stmf_state.stmf_alua_state != 1) {
1880 		cmn_err(CE_WARN, "stmf alua state is disabled");
1881 		return (STMF_FAILURE);
1882 	}
1883 
1884 	if (ilport->ilport_proxy_registered == 0) {
1885 		return (STMF_FAILURE);
1886 	}
1887 
1888 	mutex_enter(&stmf_state.stmf_lock);
1889 	itask->itask_proxy_msg_id = stmf_proxy_msg_id++;
1890 	mutex_exit(&stmf_state.stmf_lock);
1891 	itask->itask_proxy_dbuf = dbuf;
1892 
1893 	/*
1894 	 * stmf will now take over the task handling for this task
1895 	 * but it still needs to be treated differently from other
1896 	 * default handled tasks, hence the ITASK_PROXY_TASK.
1897 	 * If this is a task management function, we're really just
1898 	 * duping the command to the peer. Set the TM bit so that
1899 	 * we can recognize this on return since we won't be completing
1900 	 * the proxied task in that case.
1901 	 */
1902 	if (task->task_mgmt_function) {
1903 		itask->itask_proxy_msg_id |= MSG_ID_TM_BIT;
1904 	} else {
1905 		uint32_t new, old;
1906 		do {
1907 			new = old = itask->itask_flags;
1908 			if (new & ITASK_BEING_ABORTED)
1909 				return (STMF_FAILURE);
1910 			new |= ITASK_DEFAULT_HANDLING | ITASK_PROXY_TASK;
1911 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
1912 	}
1913 	if (dbuf) {
1914 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1915 		    task, dbuf->db_data_size, dbuf->db_sglist[0].seg_addr,
1916 		    itask->itask_proxy_msg_id);
1917 	} else {
1918 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1919 		    task, 0, NULL, itask->itask_proxy_msg_id);
1920 	}
1921 	if (ic_cmd_msg) {
1922 		ic_ret = ic_tx_msg(ic_cmd_msg);
1923 		if (ic_ret == STMF_IC_MSG_SUCCESS) {
1924 			ret = STMF_SUCCESS;
1925 		}
1926 	}
1927 	return (ret);
1928 }
1929 
1930 
1931 stmf_status_t
1932 pppt_modload()
1933 {
1934 	int error;
1935 
1936 	if (pppt_mod == NULL && ((pppt_mod =
1937 	    ddi_modopen("drv/pppt", KRTLD_MODE_FIRST, &error)) == NULL)) {
1938 		cmn_err(CE_WARN, "Unable to load pppt");
1939 		return (STMF_FAILURE);
1940 	}
1941 
1942 	if (ic_reg_port_msg_alloc == NULL && ((ic_reg_port_msg_alloc =
1943 	    (stmf_ic_reg_port_msg_alloc_func_t)
1944 	    ddi_modsym(pppt_mod, "stmf_ic_reg_port_msg_alloc",
1945 	    &error)) == NULL)) {
1946 		cmn_err(CE_WARN,
1947 		    "Unable to find symbol - stmf_ic_reg_port_msg_alloc");
1948 		return (STMF_FAILURE);
1949 	}
1950 
1951 
1952 	if (ic_dereg_port_msg_alloc == NULL && ((ic_dereg_port_msg_alloc =
1953 	    (stmf_ic_dereg_port_msg_alloc_func_t)
1954 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_port_msg_alloc",
1955 	    &error)) == NULL)) {
1956 		cmn_err(CE_WARN,
1957 		    "Unable to find symbol - stmf_ic_dereg_port_msg_alloc");
1958 		return (STMF_FAILURE);
1959 	}
1960 
1961 	if (ic_reg_lun_msg_alloc == NULL && ((ic_reg_lun_msg_alloc =
1962 	    (stmf_ic_reg_lun_msg_alloc_func_t)
1963 	    ddi_modsym(pppt_mod, "stmf_ic_reg_lun_msg_alloc",
1964 	    &error)) == NULL)) {
1965 		cmn_err(CE_WARN,
1966 		    "Unable to find symbol - stmf_ic_reg_lun_msg_alloc");
1967 		return (STMF_FAILURE);
1968 	}
1969 
1970 	if (ic_lun_active_msg_alloc == NULL && ((ic_lun_active_msg_alloc =
1971 	    (stmf_ic_lun_active_msg_alloc_func_t)
1972 	    ddi_modsym(pppt_mod, "stmf_ic_lun_active_msg_alloc",
1973 	    &error)) == NULL)) {
1974 		cmn_err(CE_WARN,
1975 		    "Unable to find symbol - stmf_ic_lun_active_msg_alloc");
1976 		return (STMF_FAILURE);
1977 	}
1978 
1979 	if (ic_dereg_lun_msg_alloc == NULL && ((ic_dereg_lun_msg_alloc =
1980 	    (stmf_ic_dereg_lun_msg_alloc_func_t)
1981 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_lun_msg_alloc",
1982 	    &error)) == NULL)) {
1983 		cmn_err(CE_WARN,
1984 		    "Unable to find symbol - stmf_ic_dereg_lun_msg_alloc");
1985 		return (STMF_FAILURE);
1986 	}
1987 
1988 	if (ic_scsi_cmd_msg_alloc == NULL && ((ic_scsi_cmd_msg_alloc =
1989 	    (stmf_ic_scsi_cmd_msg_alloc_func_t)
1990 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_cmd_msg_alloc",
1991 	    &error)) == NULL)) {
1992 		cmn_err(CE_WARN,
1993 		    "Unable to find symbol - stmf_ic_scsi_cmd_msg_alloc");
1994 		return (STMF_FAILURE);
1995 	}
1996 
1997 	if (ic_scsi_data_xfer_done_msg_alloc == NULL &&
1998 	    ((ic_scsi_data_xfer_done_msg_alloc =
1999 	    (stmf_ic_scsi_data_xfer_done_msg_alloc_func_t)
2000 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_data_xfer_done_msg_alloc",
2001 	    &error)) == NULL)) {
2002 		cmn_err(CE_WARN,
2003 		    "Unable to find symbol -"
2004 		    "stmf_ic_scsi_data_xfer_done_msg_alloc");
2005 		return (STMF_FAILURE);
2006 	}
2007 
2008 	if (ic_session_reg_msg_alloc == NULL &&
2009 	    ((ic_session_reg_msg_alloc =
2010 	    (stmf_ic_session_create_msg_alloc_func_t)
2011 	    ddi_modsym(pppt_mod, "stmf_ic_session_create_msg_alloc",
2012 	    &error)) == NULL)) {
2013 		cmn_err(CE_WARN,
2014 		    "Unable to find symbol -"
2015 		    "stmf_ic_session_create_msg_alloc");
2016 		return (STMF_FAILURE);
2017 	}
2018 
2019 	if (ic_session_dereg_msg_alloc == NULL &&
2020 	    ((ic_session_dereg_msg_alloc =
2021 	    (stmf_ic_session_destroy_msg_alloc_func_t)
2022 	    ddi_modsym(pppt_mod, "stmf_ic_session_destroy_msg_alloc",
2023 	    &error)) == NULL)) {
2024 		cmn_err(CE_WARN,
2025 		    "Unable to find symbol -"
2026 		    "stmf_ic_session_destroy_msg_alloc");
2027 		return (STMF_FAILURE);
2028 	}
2029 
2030 	if (ic_tx_msg == NULL && ((ic_tx_msg =
2031 	    (stmf_ic_tx_msg_func_t)ddi_modsym(pppt_mod, "stmf_ic_tx_msg",
2032 	    &error)) == NULL)) {
2033 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_tx_msg");
2034 		return (STMF_FAILURE);
2035 	}
2036 
2037 	if (ic_msg_free == NULL && ((ic_msg_free =
2038 	    (stmf_ic_msg_free_func_t)ddi_modsym(pppt_mod, "stmf_ic_msg_free",
2039 	    &error)) == NULL)) {
2040 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_msg_free");
2041 		return (STMF_FAILURE);
2042 	}
2043 	return (STMF_SUCCESS);
2044 }
2045 
2046 static void
2047 stmf_get_alua_state(stmf_alua_state_desc_t *alua_state)
2048 {
2049 	mutex_enter(&stmf_state.stmf_lock);
2050 	alua_state->alua_node = stmf_state.stmf_alua_node;
2051 	alua_state->alua_state = stmf_state.stmf_alua_state;
2052 	mutex_exit(&stmf_state.stmf_lock);
2053 }
2054 
2055 
2056 static int
2057 stmf_set_alua_state(stmf_alua_state_desc_t *alua_state)
2058 {
2059 	stmf_i_local_port_t *ilport;
2060 	stmf_i_lu_t *ilu;
2061 	stmf_lu_t *lu;
2062 	stmf_ic_msg_status_t ic_ret;
2063 	stmf_ic_msg_t *ic_reg_lun, *ic_reg_port;
2064 	stmf_local_port_t *lport;
2065 	int ret = 0;
2066 
2067 	if (alua_state->alua_state > 1 || alua_state->alua_node > 1) {
2068 		return (EINVAL);
2069 	}
2070 
2071 	mutex_enter(&stmf_state.stmf_lock);
2072 	if (alua_state->alua_state == 1) {
2073 		if (pppt_modload() == STMF_FAILURE) {
2074 			ret = EIO;
2075 			goto err;
2076 		}
2077 		if (alua_state->alua_node != 0) {
2078 			/* reset existing rtpids to new base */
2079 			stmf_rtpid_counter = 255;
2080 		}
2081 		stmf_state.stmf_alua_node = alua_state->alua_node;
2082 		stmf_state.stmf_alua_state = 1;
2083 		/* register existing local ports with ppp */
2084 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2085 		    ilport = ilport->ilport_next) {
2086 			/* skip standby ports and non-alua participants */
2087 			if (ilport->ilport_standby == 1 ||
2088 			    ilport->ilport_alua == 0) {
2089 				continue;
2090 			}
2091 			if (alua_state->alua_node != 0) {
2092 				ilport->ilport_rtpid =
2093 				    atomic_add_16_nv(&stmf_rtpid_counter, 1);
2094 			}
2095 			lport = ilport->ilport_lport;
2096 			ic_reg_port = ic_reg_port_msg_alloc(
2097 			    lport->lport_id, ilport->ilport_rtpid,
2098 			    0, NULL, stmf_proxy_msg_id);
2099 			if (ic_reg_port) {
2100 				ic_ret = ic_tx_msg(ic_reg_port);
2101 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
2102 					ilport->ilport_reg_msgid =
2103 					    stmf_proxy_msg_id++;
2104 				} else {
2105 					cmn_err(CE_WARN,
2106 					    "error on port registration "
2107 					    "port - %s",
2108 					    ilport->ilport_kstat_tgt_name);
2109 				}
2110 			}
2111 		}
2112 		/* register existing logical units */
2113 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
2114 		    ilu = ilu->ilu_next) {
2115 			if (ilu->ilu_access != STMF_LU_ACTIVE) {
2116 				continue;
2117 			}
2118 			/* register with proxy module */
2119 			lu = ilu->ilu_lu;
2120 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2121 			    lu->lu_lp->lp_alua_support) {
2122 				ilu->ilu_alua = 1;
2123 				/* allocate the register message */
2124 				ic_reg_lun = ic_reg_lun_msg_alloc(
2125 				    lu->lu_id->ident, lu->lu_lp->lp_name,
2126 				    lu->lu_proxy_reg_arg_len,
2127 				    (uint8_t *)lu->lu_proxy_reg_arg,
2128 				    stmf_proxy_msg_id);
2129 				/* send the message */
2130 				if (ic_reg_lun) {
2131 					ic_ret = ic_tx_msg(ic_reg_lun);
2132 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
2133 						stmf_proxy_msg_id++;
2134 					}
2135 				}
2136 			}
2137 		}
2138 	} else {
2139 		stmf_state.stmf_alua_state = 0;
2140 	}
2141 
2142 err:
2143 	mutex_exit(&stmf_state.stmf_lock);
2144 	return (ret);
2145 }
2146 
2147 
2148 typedef struct {
2149 	void	*bp;	/* back pointer from internal struct to main struct */
2150 	int	alloc_size;
2151 } __istmf_t;
2152 
2153 typedef struct {
2154 	__istmf_t	*fp;	/* Framework private */
2155 	void		*cp;	/* Caller private */
2156 	void		*ss;	/* struct specific */
2157 } __stmf_t;
2158 
2159 static struct {
2160 	int shared;
2161 	int fw_private;
2162 } stmf_sizes[] = { { 0, 0 },
2163 	{ GET_STRUCT_SIZE(stmf_lu_provider_t),
2164 		GET_STRUCT_SIZE(stmf_i_lu_provider_t) },
2165 	{ GET_STRUCT_SIZE(stmf_port_provider_t),
2166 		GET_STRUCT_SIZE(stmf_i_port_provider_t) },
2167 	{ GET_STRUCT_SIZE(stmf_local_port_t),
2168 		GET_STRUCT_SIZE(stmf_i_local_port_t) },
2169 	{ GET_STRUCT_SIZE(stmf_lu_t),
2170 		GET_STRUCT_SIZE(stmf_i_lu_t) },
2171 	{ GET_STRUCT_SIZE(stmf_scsi_session_t),
2172 		GET_STRUCT_SIZE(stmf_i_scsi_session_t) },
2173 	{ GET_STRUCT_SIZE(scsi_task_t),
2174 		GET_STRUCT_SIZE(stmf_i_scsi_task_t) },
2175 	{ GET_STRUCT_SIZE(stmf_data_buf_t),
2176 		GET_STRUCT_SIZE(__istmf_t) },
2177 	{ GET_STRUCT_SIZE(stmf_dbuf_store_t),
2178 		GET_STRUCT_SIZE(__istmf_t) }
2179 
2180 };
2181 
2182 void *
2183 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags)
2184 {
2185 	int stmf_size;
2186 	int kmem_flag;
2187 	__stmf_t *sh;
2188 
2189 	if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS))
2190 		return (NULL);
2191 
2192 	if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
2193 		kmem_flag = KM_NOSLEEP;
2194 	} else {
2195 		kmem_flag = KM_SLEEP;
2196 	}
2197 
2198 	additional_size = (additional_size + 7) & (~7);
2199 	stmf_size = stmf_sizes[struct_id].shared +
2200 	    stmf_sizes[struct_id].fw_private + additional_size;
2201 
2202 	if (flags & AF_DONTZERO)
2203 		sh = (__stmf_t *)kmem_alloc(stmf_size, kmem_flag);
2204 	else
2205 		sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag);
2206 
2207 	if (sh == NULL)
2208 		return (NULL);
2209 
2210 	/*
2211 	 * In principle, the implementation inside stmf_alloc should not
2212 	 * be changed anyway. But the original order of framework private
2213 	 * data and caller private data does not support sglist in the caller
2214 	 * private data.
2215 	 * To work around this, the memory segments of framework private
2216 	 * data and caller private data are re-ordered here.
2217 	 * A better solution is to provide a specific interface to allocate
2218 	 * the sglist, then we will not need this workaround any more.
2219 	 * But before the new interface is available, the memory segment
2220 	 * ordering should be kept as is.
2221 	 */
2222 	sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
2223 	sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh,
2224 	    stmf_sizes[struct_id].shared + additional_size);
2225 
2226 	sh->fp->bp = sh;
2227 	/* Just store the total size instead of storing additional size */
2228 	sh->fp->alloc_size = stmf_size;
2229 
2230 	return (sh);
2231 }
2232 
2233 void
2234 stmf_free(void *ptr)
2235 {
2236 	__stmf_t *sh = (__stmf_t *)ptr;
2237 
2238 	/*
2239 	 * So far we dont need any struct specific processing. If such
2240 	 * a need ever arises, then store the struct id in the framework
2241 	 * private section and get it here as sh->fp->struct_id.
2242 	 */
2243 	kmem_free(ptr, sh->fp->alloc_size);
2244 }
2245 
2246 /*
2247  * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
2248  * framework and returns a pointer to framework private data for the lu.
2249  * Returns NULL if the lu was not found.
2250  */
2251 stmf_i_lu_t *
2252 stmf_lookup_lu(stmf_lu_t *lu)
2253 {
2254 	stmf_i_lu_t *ilu;
2255 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2256 
2257 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2258 		if (ilu->ilu_lu == lu)
2259 			return (ilu);
2260 	}
2261 	return (NULL);
2262 }
2263 
2264 /*
2265  * Given a pointer to stmf_local_port_t, verifies if this lport is registered
2266  * with the framework and returns a pointer to framework private data for
2267  * the lport.
2268  * Returns NULL if the lport was not found.
2269  */
2270 stmf_i_local_port_t *
2271 stmf_lookup_lport(stmf_local_port_t *lport)
2272 {
2273 	stmf_i_local_port_t *ilport;
2274 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2275 
2276 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2277 	    ilport = ilport->ilport_next) {
2278 		if (ilport->ilport_lport == lport)
2279 			return (ilport);
2280 	}
2281 	return (NULL);
2282 }
2283 
2284 stmf_status_t
2285 stmf_register_lu_provider(stmf_lu_provider_t *lp)
2286 {
2287 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2288 	stmf_pp_data_t *ppd;
2289 	uint32_t cb_flags;
2290 
2291 	if (lp->lp_lpif_rev != LPIF_REV_1 && lp->lp_lpif_rev != LPIF_REV_2)
2292 		return (STMF_FAILURE);
2293 
2294 	mutex_enter(&stmf_state.stmf_lock);
2295 	ilp->ilp_next = stmf_state.stmf_ilplist;
2296 	stmf_state.stmf_ilplist = ilp;
2297 	stmf_state.stmf_nlps++;
2298 
2299 	/* See if we need to do a callback */
2300 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2301 		if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
2302 			break;
2303 		}
2304 	}
2305 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2306 		goto rlp_bail_out;
2307 	}
2308 	ilp->ilp_ppd = ppd;
2309 	ppd->ppd_provider = ilp;
2310 	if (lp->lp_cb == NULL)
2311 		goto rlp_bail_out;
2312 	ilp->ilp_cb_in_progress = 1;
2313 	cb_flags = STMF_PCB_PREG_COMPLETE;
2314 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2315 		cb_flags |= STMF_PCB_STMF_ONLINING;
2316 	mutex_exit(&stmf_state.stmf_lock);
2317 	lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2318 	mutex_enter(&stmf_state.stmf_lock);
2319 	ilp->ilp_cb_in_progress = 0;
2320 
2321 rlp_bail_out:
2322 	mutex_exit(&stmf_state.stmf_lock);
2323 
2324 	return (STMF_SUCCESS);
2325 }
2326 
2327 stmf_status_t
2328 stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
2329 {
2330 	stmf_i_lu_provider_t	**ppilp;
2331 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2332 
2333 	mutex_enter(&stmf_state.stmf_lock);
2334 	if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
2335 		mutex_exit(&stmf_state.stmf_lock);
2336 		return (STMF_BUSY);
2337 	}
2338 	for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
2339 	    ppilp = &((*ppilp)->ilp_next)) {
2340 		if (*ppilp == ilp) {
2341 			*ppilp = ilp->ilp_next;
2342 			stmf_state.stmf_nlps--;
2343 			if (ilp->ilp_ppd) {
2344 				ilp->ilp_ppd->ppd_provider = NULL;
2345 				ilp->ilp_ppd = NULL;
2346 			}
2347 			mutex_exit(&stmf_state.stmf_lock);
2348 			return (STMF_SUCCESS);
2349 		}
2350 	}
2351 	mutex_exit(&stmf_state.stmf_lock);
2352 	return (STMF_NOT_FOUND);
2353 }
2354 
2355 stmf_status_t
2356 stmf_register_port_provider(stmf_port_provider_t *pp)
2357 {
2358 	stmf_i_port_provider_t *ipp =
2359 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
2360 	stmf_pp_data_t *ppd;
2361 	uint32_t cb_flags;
2362 
2363 	if (pp->pp_portif_rev != PORTIF_REV_1)
2364 		return (STMF_FAILURE);
2365 
2366 	mutex_enter(&stmf_state.stmf_lock);
2367 	ipp->ipp_next = stmf_state.stmf_ipplist;
2368 	stmf_state.stmf_ipplist = ipp;
2369 	stmf_state.stmf_npps++;
2370 	/* See if we need to do a callback */
2371 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2372 		if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
2373 			break;
2374 		}
2375 	}
2376 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2377 		goto rpp_bail_out;
2378 	}
2379 	ipp->ipp_ppd = ppd;
2380 	ppd->ppd_provider = ipp;
2381 	if (pp->pp_cb == NULL)
2382 		goto rpp_bail_out;
2383 	ipp->ipp_cb_in_progress = 1;
2384 	cb_flags = STMF_PCB_PREG_COMPLETE;
2385 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2386 		cb_flags |= STMF_PCB_STMF_ONLINING;
2387 	mutex_exit(&stmf_state.stmf_lock);
2388 	pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2389 	mutex_enter(&stmf_state.stmf_lock);
2390 	ipp->ipp_cb_in_progress = 0;
2391 
2392 rpp_bail_out:
2393 	mutex_exit(&stmf_state.stmf_lock);
2394 
2395 	return (STMF_SUCCESS);
2396 }
2397 
2398 stmf_status_t
2399 stmf_deregister_port_provider(stmf_port_provider_t *pp)
2400 {
2401 	stmf_i_port_provider_t *ipp =
2402 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
2403 	stmf_i_port_provider_t **ppipp;
2404 
2405 	mutex_enter(&stmf_state.stmf_lock);
2406 	if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
2407 		mutex_exit(&stmf_state.stmf_lock);
2408 		return (STMF_BUSY);
2409 	}
2410 	for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
2411 	    ppipp = &((*ppipp)->ipp_next)) {
2412 		if (*ppipp == ipp) {
2413 			*ppipp = ipp->ipp_next;
2414 			stmf_state.stmf_npps--;
2415 			if (ipp->ipp_ppd) {
2416 				ipp->ipp_ppd->ppd_provider = NULL;
2417 				ipp->ipp_ppd = NULL;
2418 			}
2419 			mutex_exit(&stmf_state.stmf_lock);
2420 			return (STMF_SUCCESS);
2421 		}
2422 	}
2423 	mutex_exit(&stmf_state.stmf_lock);
2424 	return (STMF_NOT_FOUND);
2425 }
2426 
2427 int
2428 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
2429     uint32_t *err_ret)
2430 {
2431 	stmf_i_port_provider_t		*ipp;
2432 	stmf_i_lu_provider_t		*ilp;
2433 	stmf_pp_data_t			*ppd;
2434 	nvlist_t			*nv;
2435 	int				s;
2436 	int				ret;
2437 
2438 	*err_ret = 0;
2439 
2440 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2441 		return (EINVAL);
2442 	}
2443 
2444 	mutex_enter(&stmf_state.stmf_lock);
2445 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2446 		if (ppi->ppi_lu_provider) {
2447 			if (!ppd->ppd_lu_provider)
2448 				continue;
2449 		} else if (ppi->ppi_port_provider) {
2450 			if (!ppd->ppd_port_provider)
2451 				continue;
2452 		}
2453 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2454 			break;
2455 	}
2456 
2457 	if (ppd == NULL) {
2458 		/* New provider */
2459 		s = strlen(ppi->ppi_name);
2460 		if (s > 254) {
2461 			mutex_exit(&stmf_state.stmf_lock);
2462 			return (EINVAL);
2463 		}
2464 		s += sizeof (stmf_pp_data_t) - 7;
2465 
2466 		ppd = kmem_zalloc(s, KM_NOSLEEP);
2467 		if (ppd == NULL) {
2468 			mutex_exit(&stmf_state.stmf_lock);
2469 			return (ENOMEM);
2470 		}
2471 		ppd->ppd_alloc_size = s;
2472 		(void) strcpy(ppd->ppd_name, ppi->ppi_name);
2473 
2474 		/* See if this provider already exists */
2475 		if (ppi->ppi_lu_provider) {
2476 			ppd->ppd_lu_provider = 1;
2477 			for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
2478 			    ilp = ilp->ilp_next) {
2479 				if (strcmp(ppi->ppi_name,
2480 				    ilp->ilp_lp->lp_name) == 0) {
2481 					ppd->ppd_provider = ilp;
2482 					ilp->ilp_ppd = ppd;
2483 					break;
2484 				}
2485 			}
2486 		} else {
2487 			ppd->ppd_port_provider = 1;
2488 			for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
2489 			    ipp = ipp->ipp_next) {
2490 				if (strcmp(ppi->ppi_name,
2491 				    ipp->ipp_pp->pp_name) == 0) {
2492 					ppd->ppd_provider = ipp;
2493 					ipp->ipp_ppd = ppd;
2494 					break;
2495 				}
2496 			}
2497 		}
2498 
2499 		/* Link this ppd in */
2500 		ppd->ppd_next = stmf_state.stmf_ppdlist;
2501 		stmf_state.stmf_ppdlist = ppd;
2502 	}
2503 
2504 	/*
2505 	 * User is requesting that the token be checked.
2506 	 * If there was another set after the user's get
2507 	 * it's an error
2508 	 */
2509 	if (ppi->ppi_token_valid) {
2510 		if (ppi->ppi_token != ppd->ppd_token) {
2511 			*err_ret = STMF_IOCERR_PPD_UPDATED;
2512 			mutex_exit(&stmf_state.stmf_lock);
2513 			return (EINVAL);
2514 		}
2515 	}
2516 
2517 	if ((ret = nvlist_unpack((char *)ppi->ppi_data,
2518 	    (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
2519 		mutex_exit(&stmf_state.stmf_lock);
2520 		return (ret);
2521 	}
2522 
2523 	/* Free any existing lists and add this one to the ppd */
2524 	if (ppd->ppd_nv)
2525 		nvlist_free(ppd->ppd_nv);
2526 	ppd->ppd_nv = nv;
2527 
2528 	/* set the token for writes */
2529 	ppd->ppd_token++;
2530 	/* return token to caller */
2531 	if (ppi_token) {
2532 		*ppi_token = ppd->ppd_token;
2533 	}
2534 
2535 	/* If there is a provider registered, do the notifications */
2536 	if (ppd->ppd_provider) {
2537 		uint32_t cb_flags = 0;
2538 
2539 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2540 			cb_flags |= STMF_PCB_STMF_ONLINING;
2541 		if (ppi->ppi_lu_provider) {
2542 			ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
2543 			if (ilp->ilp_lp->lp_cb == NULL)
2544 				goto bail_out;
2545 			ilp->ilp_cb_in_progress = 1;
2546 			mutex_exit(&stmf_state.stmf_lock);
2547 			ilp->ilp_lp->lp_cb(ilp->ilp_lp,
2548 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2549 			mutex_enter(&stmf_state.stmf_lock);
2550 			ilp->ilp_cb_in_progress = 0;
2551 		} else {
2552 			ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
2553 			if (ipp->ipp_pp->pp_cb == NULL)
2554 				goto bail_out;
2555 			ipp->ipp_cb_in_progress = 1;
2556 			mutex_exit(&stmf_state.stmf_lock);
2557 			ipp->ipp_pp->pp_cb(ipp->ipp_pp,
2558 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2559 			mutex_enter(&stmf_state.stmf_lock);
2560 			ipp->ipp_cb_in_progress = 0;
2561 		}
2562 	}
2563 
2564 bail_out:
2565 	mutex_exit(&stmf_state.stmf_lock);
2566 
2567 	return (0);
2568 }
2569 
2570 void
2571 stmf_delete_ppd(stmf_pp_data_t *ppd)
2572 {
2573 	stmf_pp_data_t **pppd;
2574 
2575 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2576 	if (ppd->ppd_provider) {
2577 		if (ppd->ppd_lu_provider) {
2578 			((stmf_i_lu_provider_t *)
2579 			    ppd->ppd_provider)->ilp_ppd = NULL;
2580 		} else {
2581 			((stmf_i_port_provider_t *)
2582 			    ppd->ppd_provider)->ipp_ppd = NULL;
2583 		}
2584 		ppd->ppd_provider = NULL;
2585 	}
2586 
2587 	for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
2588 	    pppd = &((*pppd)->ppd_next)) {
2589 		if (*pppd == ppd)
2590 			break;
2591 	}
2592 
2593 	if (*pppd == NULL)
2594 		return;
2595 
2596 	*pppd = ppd->ppd_next;
2597 	if (ppd->ppd_nv)
2598 		nvlist_free(ppd->ppd_nv);
2599 
2600 	kmem_free(ppd, ppd->ppd_alloc_size);
2601 }
2602 
2603 int
2604 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
2605 {
2606 	stmf_pp_data_t *ppd;
2607 	int ret = ENOENT;
2608 
2609 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2610 		return (EINVAL);
2611 	}
2612 
2613 	mutex_enter(&stmf_state.stmf_lock);
2614 
2615 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2616 		if (ppi->ppi_lu_provider) {
2617 			if (!ppd->ppd_lu_provider)
2618 				continue;
2619 		} else if (ppi->ppi_port_provider) {
2620 			if (!ppd->ppd_port_provider)
2621 				continue;
2622 		}
2623 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2624 			break;
2625 	}
2626 
2627 	if (ppd) {
2628 		ret = 0;
2629 		stmf_delete_ppd(ppd);
2630 	}
2631 	mutex_exit(&stmf_state.stmf_lock);
2632 
2633 	return (ret);
2634 }
2635 
2636 int
2637 stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
2638     uint32_t *err_ret)
2639 {
2640 	stmf_pp_data_t *ppd;
2641 	size_t req_size;
2642 	int ret = ENOENT;
2643 	char *bufp = (char *)ppi_out->ppi_data;
2644 
2645 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2646 		return (EINVAL);
2647 	}
2648 
2649 	mutex_enter(&stmf_state.stmf_lock);
2650 
2651 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2652 		if (ppi->ppi_lu_provider) {
2653 			if (!ppd->ppd_lu_provider)
2654 				continue;
2655 		} else if (ppi->ppi_port_provider) {
2656 			if (!ppd->ppd_port_provider)
2657 				continue;
2658 		}
2659 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2660 			break;
2661 	}
2662 
2663 	if (ppd && ppd->ppd_nv) {
2664 		ppi_out->ppi_token = ppd->ppd_token;
2665 		if ((ret = nvlist_size(ppd->ppd_nv, &req_size,
2666 		    NV_ENCODE_XDR)) != 0) {
2667 			goto done;
2668 		}
2669 		ppi_out->ppi_data_size = req_size;
2670 		if (req_size > ppi->ppi_data_size) {
2671 			*err_ret = STMF_IOCERR_INSUFFICIENT_BUF;
2672 			ret = EINVAL;
2673 			goto done;
2674 		}
2675 
2676 		if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size,
2677 		    NV_ENCODE_XDR, 0)) != 0) {
2678 			goto done;
2679 		}
2680 		ret = 0;
2681 	}
2682 
2683 done:
2684 	mutex_exit(&stmf_state.stmf_lock);
2685 
2686 	return (ret);
2687 }
2688 
2689 void
2690 stmf_delete_all_ppds()
2691 {
2692 	stmf_pp_data_t *ppd, *nppd;
2693 
2694 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2695 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
2696 		nppd = ppd->ppd_next;
2697 		stmf_delete_ppd(ppd);
2698 	}
2699 }
2700 
2701 /*
2702  * 16 is the max string length of a protocol_ident, increase
2703  * the size if needed.
2704  */
2705 #define	STMF_KSTAT_LU_SZ	(STMF_GUID_INPUT + 1 + 256)
2706 #define	STMF_KSTAT_TGT_SZ	(256 * 2 + 16)
2707 
2708 /*
2709  * This array matches the Protocol Identifier in stmf_ioctl.h
2710  */
2711 #define	MAX_PROTO_STR_LEN	32
2712 
2713 char *protocol_ident[PROTOCOL_ANY] = {
2714 	"Fibre Channel",
2715 	"Parallel SCSI",
2716 	"SSA",
2717 	"IEEE_1394",
2718 	"SRP",
2719 	"iSCSI",
2720 	"SAS",
2721 	"ADT",
2722 	"ATAPI",
2723 	"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"
2724 };
2725 
2726 /*
2727  * Update the lun wait/run queue count
2728  */
2729 static void
2730 stmf_update_kstat_lu_q(scsi_task_t *task, void func())
2731 {
2732 	stmf_i_lu_t		*ilu;
2733 	kstat_io_t		*kip;
2734 
2735 	if (task->task_lu == dlun0)
2736 		return;
2737 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2738 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2739 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2740 		if (kip != NULL) {
2741 			func(kip);
2742 		}
2743 	}
2744 }
2745 
2746 /*
2747  * Update the target(lport) wait/run queue count
2748  */
2749 static void
2750 stmf_update_kstat_lport_q(scsi_task_t *task, void func())
2751 {
2752 	stmf_i_local_port_t	*ilp;
2753 	kstat_io_t		*kip;
2754 
2755 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2756 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2757 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2758 		if (kip != NULL) {
2759 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2760 			func(kip);
2761 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2762 		}
2763 	}
2764 }
2765 
2766 static void
2767 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2768 {
2769 	stmf_i_local_port_t	*ilp;
2770 	kstat_io_t		*kip;
2771 
2772 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2773 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2774 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2775 		if (kip != NULL) {
2776 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2777 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2778 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2779 		}
2780 	}
2781 }
2782 
2783 static void
2784 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2785 {
2786 	stmf_i_lu_t		*ilu;
2787 	kstat_io_t		*kip;
2788 
2789 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2790 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2791 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2792 		if (kip != NULL) {
2793 			mutex_enter(ilu->ilu_kstat_io->ks_lock);
2794 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2795 			mutex_exit(ilu->ilu_kstat_io->ks_lock);
2796 		}
2797 	}
2798 }
2799 
2800 static void
2801 stmf_create_kstat_lu(stmf_i_lu_t *ilu)
2802 {
2803 	char				ks_nm[KSTAT_STRLEN];
2804 	stmf_kstat_lu_info_t		*ks_lu;
2805 
2806 	/* create kstat lun info */
2807 	ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ,
2808 	    KM_NOSLEEP);
2809 	if (ks_lu == NULL) {
2810 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2811 		return;
2812 	}
2813 
2814 	bzero(ks_nm, sizeof (ks_nm));
2815 	(void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu);
2816 	if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0,
2817 	    ks_nm, "misc", KSTAT_TYPE_NAMED,
2818 	    sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t),
2819 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2820 		kmem_free(ks_lu, STMF_KSTAT_LU_SZ);
2821 		cmn_err(CE_WARN, "STMF: kstat_create lu failed");
2822 		return;
2823 	}
2824 
2825 	ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ;
2826 	ilu->ilu_kstat_info->ks_data = ks_lu;
2827 
2828 	kstat_named_init(&ks_lu->i_lun_guid, "lun-guid",
2829 	    KSTAT_DATA_STRING);
2830 	kstat_named_init(&ks_lu->i_lun_alias, "lun-alias",
2831 	    KSTAT_DATA_STRING);
2832 
2833 	/* convert guid to hex string */
2834 	int		i;
2835 	uint8_t		*p = ilu->ilu_lu->lu_id->ident;
2836 	bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid));
2837 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
2838 		(void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]);
2839 	}
2840 	kstat_named_setstr(&ks_lu->i_lun_guid,
2841 	    (const char *)ilu->ilu_ascii_hex_guid);
2842 	kstat_named_setstr(&ks_lu->i_lun_alias,
2843 	    (const char *)ilu->ilu_lu->lu_alias);
2844 	kstat_install(ilu->ilu_kstat_info);
2845 
2846 	/* create kstat lun io */
2847 	bzero(ks_nm, sizeof (ks_nm));
2848 	(void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu);
2849 	if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2850 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2851 		cmn_err(CE_WARN, "STMF: kstat_create lu_io failed");
2852 		return;
2853 	}
2854 	mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0);
2855 	ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock;
2856 	kstat_install(ilu->ilu_kstat_io);
2857 }
2858 
2859 static void
2860 stmf_create_kstat_lport(stmf_i_local_port_t *ilport)
2861 {
2862 	char				ks_nm[KSTAT_STRLEN];
2863 	stmf_kstat_tgt_info_t		*ks_tgt;
2864 	int				id, len;
2865 
2866 	/* create kstat lport info */
2867 	ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ,
2868 	    KM_NOSLEEP);
2869 	if (ks_tgt == NULL) {
2870 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2871 		return;
2872 	}
2873 
2874 	bzero(ks_nm, sizeof (ks_nm));
2875 	(void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport);
2876 	if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME,
2877 	    0, ks_nm, "misc", KSTAT_TYPE_NAMED,
2878 	    sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t),
2879 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2880 		kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ);
2881 		cmn_err(CE_WARN, "STMF: kstat_create target failed");
2882 		return;
2883 	}
2884 
2885 	ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ;
2886 	ilport->ilport_kstat_info->ks_data = ks_tgt;
2887 
2888 	kstat_named_init(&ks_tgt->i_tgt_name, "target-name",
2889 	    KSTAT_DATA_STRING);
2890 	kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias",
2891 	    KSTAT_DATA_STRING);
2892 	kstat_named_init(&ks_tgt->i_protocol, "protocol",
2893 	    KSTAT_DATA_STRING);
2894 
2895 	/* ident might not be null terminated */
2896 	len = ilport->ilport_lport->lport_id->ident_length;
2897 	bcopy(ilport->ilport_lport->lport_id->ident,
2898 	    ilport->ilport_kstat_tgt_name, len);
2899 	ilport->ilport_kstat_tgt_name[len + 1] = NULL;
2900 	kstat_named_setstr(&ks_tgt->i_tgt_name,
2901 	    (const char *)ilport->ilport_kstat_tgt_name);
2902 	kstat_named_setstr(&ks_tgt->i_tgt_alias,
2903 	    (const char *)ilport->ilport_lport->lport_alias);
2904 	/* protocol */
2905 	if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) {
2906 		cmn_err(CE_WARN, "STMF: protocol_id out of bound");
2907 		id = PROTOCOL_ANY;
2908 	}
2909 	kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]);
2910 	kstat_install(ilport->ilport_kstat_info);
2911 
2912 	/* create kstat lport io */
2913 	bzero(ks_nm, sizeof (ks_nm));
2914 	(void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport);
2915 	if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2916 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2917 		cmn_err(CE_WARN, "STMF: kstat_create target_io failed");
2918 		return;
2919 	}
2920 	mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0);
2921 	ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock;
2922 	kstat_install(ilport->ilport_kstat_io);
2923 }
2924 
2925 /*
2926  * set the asymmetric access state for a logical unit
2927  * caller is responsible for establishing SCSI unit attention on
2928  * state change
2929  */
2930 stmf_status_t
2931 stmf_set_lu_access(stmf_lu_t *lu, uint8_t access_state)
2932 {
2933 	stmf_i_lu_t *ilu;
2934 	uint8_t *p1, *p2;
2935 
2936 	if ((access_state != STMF_LU_STANDBY) &&
2937 	    (access_state != STMF_LU_ACTIVE)) {
2938 		return (STMF_INVALID_ARG);
2939 	}
2940 
2941 	p1 = &lu->lu_id->ident[0];
2942 	mutex_enter(&stmf_state.stmf_lock);
2943 	if (stmf_state.stmf_inventory_locked) {
2944 		mutex_exit(&stmf_state.stmf_lock);
2945 		return (STMF_BUSY);
2946 	}
2947 
2948 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2949 		p2 = &ilu->ilu_lu->lu_id->ident[0];
2950 		if (bcmp(p1, p2, 16) == 0) {
2951 			break;
2952 		}
2953 	}
2954 
2955 	if (!ilu) {
2956 		ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2957 	} else {
2958 		/*
2959 		 * We're changing access state on an existing logical unit
2960 		 * Send the proxy registration message for this logical unit
2961 		 * if we're in alua mode.
2962 		 * If the requested state is STMF_LU_ACTIVE, we want to register
2963 		 * this logical unit.
2964 		 * If the requested state is STMF_LU_STANDBY, we're going to
2965 		 * abort all tasks for this logical unit.
2966 		 */
2967 		if (stmf_state.stmf_alua_state == 1 &&
2968 		    access_state == STMF_LU_ACTIVE) {
2969 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
2970 			stmf_ic_msg_t *ic_reg_lun;
2971 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2972 			    lu->lu_lp->lp_alua_support) {
2973 				ilu->ilu_alua = 1;
2974 				/* allocate the register message */
2975 				ic_reg_lun = ic_lun_active_msg_alloc(p1,
2976 				    lu->lu_lp->lp_name,
2977 				    lu->lu_proxy_reg_arg_len,
2978 				    (uint8_t *)lu->lu_proxy_reg_arg,
2979 				    stmf_proxy_msg_id);
2980 				/* send the message */
2981 				if (ic_reg_lun) {
2982 					ic_ret = ic_tx_msg(ic_reg_lun);
2983 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
2984 						stmf_proxy_msg_id++;
2985 					}
2986 				}
2987 			}
2988 		} else if (stmf_state.stmf_alua_state == 1 &&
2989 		    access_state == STMF_LU_STANDBY) {
2990 			/* abort all tasks for this lu */
2991 			stmf_task_lu_killall(lu, NULL, STMF_ABORTED);
2992 		}
2993 	}
2994 
2995 	ilu->ilu_access = access_state;
2996 
2997 	mutex_exit(&stmf_state.stmf_lock);
2998 	return (STMF_SUCCESS);
2999 }
3000 
3001 
3002 stmf_status_t
3003 stmf_register_lu(stmf_lu_t *lu)
3004 {
3005 	stmf_i_lu_t *ilu;
3006 	uint8_t *p1, *p2;
3007 	stmf_state_change_info_t ssci;
3008 	stmf_id_data_t *luid;
3009 
3010 	if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
3011 	    (lu->lu_id->ident_length != 16) ||
3012 	    ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
3013 		return (STMF_INVALID_ARG);
3014 	}
3015 	p1 = &lu->lu_id->ident[0];
3016 	mutex_enter(&stmf_state.stmf_lock);
3017 	if (stmf_state.stmf_inventory_locked) {
3018 		mutex_exit(&stmf_state.stmf_lock);
3019 		return (STMF_BUSY);
3020 	}
3021 
3022 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
3023 		p2 = &ilu->ilu_lu->lu_id->ident[0];
3024 		if (bcmp(p1, p2, 16) == 0) {
3025 			mutex_exit(&stmf_state.stmf_lock);
3026 			return (STMF_ALREADY);
3027 		}
3028 	}
3029 
3030 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3031 	luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
3032 	    lu->lu_id->ident_length, lu->lu_id->ident);
3033 	if (luid) {
3034 		luid->id_pt_to_object = (void *)ilu;
3035 		ilu->ilu_luid = luid;
3036 	}
3037 	ilu->ilu_alias = NULL;
3038 
3039 	ilu->ilu_next = stmf_state.stmf_ilulist;
3040 	ilu->ilu_prev = NULL;
3041 	if (ilu->ilu_next)
3042 		ilu->ilu_next->ilu_prev = ilu;
3043 	stmf_state.stmf_ilulist = ilu;
3044 	stmf_state.stmf_nlus++;
3045 	if (lu->lu_lp) {
3046 		((stmf_i_lu_provider_t *)
3047 		    (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
3048 	}
3049 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
3050 	STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
3051 	stmf_create_kstat_lu(ilu);
3052 	/*
3053 	 * register with proxy module if available and logical unit
3054 	 * is in active state
3055 	 */
3056 	if (stmf_state.stmf_alua_state == 1 &&
3057 	    ilu->ilu_access == STMF_LU_ACTIVE) {
3058 		stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3059 		stmf_ic_msg_t *ic_reg_lun;
3060 		if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3061 		    lu->lu_lp->lp_alua_support) {
3062 			ilu->ilu_alua = 1;
3063 			/* allocate the register message */
3064 			ic_reg_lun = ic_reg_lun_msg_alloc(p1,
3065 			    lu->lu_lp->lp_name, lu->lu_proxy_reg_arg_len,
3066 			    (uint8_t *)lu->lu_proxy_reg_arg, stmf_proxy_msg_id);
3067 			/* send the message */
3068 			if (ic_reg_lun) {
3069 				ic_ret = ic_tx_msg(ic_reg_lun);
3070 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
3071 					stmf_proxy_msg_id++;
3072 				}
3073 			}
3074 		}
3075 	}
3076 	mutex_exit(&stmf_state.stmf_lock);
3077 
3078 	/*  check the default state for lu */
3079 	if (stmf_state.stmf_default_lu_state == STMF_STATE_OFFLINE) {
3080 		ilu->ilu_prev_state = STMF_STATE_OFFLINE;
3081 	} else {
3082 		ilu->ilu_prev_state = STMF_STATE_ONLINE;
3083 		if (stmf_state.stmf_service_running) {
3084 			ssci.st_rflags = 0;
3085 			ssci.st_additional_info = NULL;
3086 			(void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
3087 		}
3088 	}
3089 
3090 	/* XXX: Generate event */
3091 	return (STMF_SUCCESS);
3092 }
3093 
3094 stmf_status_t
3095 stmf_deregister_lu(stmf_lu_t *lu)
3096 {
3097 	stmf_i_lu_t *ilu;
3098 
3099 	mutex_enter(&stmf_state.stmf_lock);
3100 	if (stmf_state.stmf_inventory_locked) {
3101 		mutex_exit(&stmf_state.stmf_lock);
3102 		return (STMF_BUSY);
3103 	}
3104 	ilu = stmf_lookup_lu(lu);
3105 	if (ilu == NULL) {
3106 		mutex_exit(&stmf_state.stmf_lock);
3107 		return (STMF_INVALID_ARG);
3108 	}
3109 	if (ilu->ilu_state == STMF_STATE_OFFLINE) {
3110 		ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
3111 		while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
3112 			cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
3113 		}
3114 		if (ilu->ilu_ntasks) {
3115 			stmf_i_scsi_task_t *itask, *nitask;
3116 
3117 			nitask = ilu->ilu_tasks;
3118 			do {
3119 				itask = nitask;
3120 				nitask = itask->itask_lu_next;
3121 				lu->lu_task_free(itask->itask_task);
3122 				stmf_free(itask->itask_task);
3123 			} while (nitask != NULL);
3124 
3125 			ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
3126 			ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
3127 		}
3128 		/* de-register with proxy if available */
3129 		if (ilu->ilu_access == STMF_LU_ACTIVE &&
3130 		    stmf_state.stmf_alua_state == 1) {
3131 			/* de-register with proxy module */
3132 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3133 			stmf_ic_msg_t *ic_dereg_lun;
3134 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3135 			    lu->lu_lp->lp_alua_support) {
3136 				ilu->ilu_alua = 1;
3137 				/* allocate the de-register message */
3138 				ic_dereg_lun = ic_dereg_lun_msg_alloc(
3139 				    lu->lu_id->ident, lu->lu_lp->lp_name, 0,
3140 				    NULL, stmf_proxy_msg_id);
3141 				/* send the message */
3142 				if (ic_dereg_lun) {
3143 					ic_ret = ic_tx_msg(ic_dereg_lun);
3144 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
3145 						stmf_proxy_msg_id++;
3146 					}
3147 				}
3148 			}
3149 		}
3150 
3151 		if (ilu->ilu_next)
3152 			ilu->ilu_next->ilu_prev = ilu->ilu_prev;
3153 		if (ilu->ilu_prev)
3154 			ilu->ilu_prev->ilu_next = ilu->ilu_next;
3155 		else
3156 			stmf_state.stmf_ilulist = ilu->ilu_next;
3157 		stmf_state.stmf_nlus--;
3158 
3159 		if (ilu == stmf_state.stmf_svc_ilu_draining) {
3160 			stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
3161 		}
3162 		if (ilu == stmf_state.stmf_svc_ilu_timing) {
3163 			stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
3164 		}
3165 		if (lu->lu_lp) {
3166 			((stmf_i_lu_provider_t *)
3167 			    (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
3168 		}
3169 		if (ilu->ilu_luid) {
3170 			((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
3171 			    NULL;
3172 			ilu->ilu_luid = NULL;
3173 		}
3174 		STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
3175 	} else {
3176 		mutex_exit(&stmf_state.stmf_lock);
3177 		return (STMF_BUSY);
3178 	}
3179 	if (ilu->ilu_kstat_info) {
3180 		kmem_free(ilu->ilu_kstat_info->ks_data,
3181 		    ilu->ilu_kstat_info->ks_data_size);
3182 		kstat_delete(ilu->ilu_kstat_info);
3183 	}
3184 	if (ilu->ilu_kstat_io) {
3185 		kstat_delete(ilu->ilu_kstat_io);
3186 		mutex_destroy(&ilu->ilu_kstat_lock);
3187 	}
3188 	stmf_delete_itl_kstat_by_guid(ilu->ilu_ascii_hex_guid);
3189 	mutex_exit(&stmf_state.stmf_lock);
3190 	return (STMF_SUCCESS);
3191 }
3192 
3193 void
3194 stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid)
3195 {
3196 	stmf_i_local_port_t *ilport =
3197 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3198 	ilport->ilport_rtpid = rtpid;
3199 	ilport->ilport_standby = 1;
3200 }
3201 
3202 void
3203 stmf_set_port_alua(stmf_local_port_t *lport)
3204 {
3205 	stmf_i_local_port_t *ilport =
3206 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3207 	ilport->ilport_alua = 1;
3208 }
3209 
3210 stmf_status_t
3211 stmf_register_local_port(stmf_local_port_t *lport)
3212 {
3213 	stmf_i_local_port_t *ilport;
3214 	stmf_state_change_info_t ssci;
3215 	int start_workers = 0;
3216 
3217 	mutex_enter(&stmf_state.stmf_lock);
3218 	if (stmf_state.stmf_inventory_locked) {
3219 		mutex_exit(&stmf_state.stmf_lock);
3220 		return (STMF_BUSY);
3221 	}
3222 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3223 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
3224 
3225 	ilport->ilport_instance =
3226 	    id_alloc_nosleep(stmf_state.stmf_ilport_inst_space);
3227 	if (ilport->ilport_instance == -1) {
3228 		mutex_exit(&stmf_state.stmf_lock);
3229 		return (STMF_FAILURE);
3230 	}
3231 	ilport->ilport_next = stmf_state.stmf_ilportlist;
3232 	ilport->ilport_prev = NULL;
3233 	if (ilport->ilport_next)
3234 		ilport->ilport_next->ilport_prev = ilport;
3235 	stmf_state.stmf_ilportlist = ilport;
3236 	stmf_state.stmf_nlports++;
3237 	if (lport->lport_pp) {
3238 		((stmf_i_port_provider_t *)
3239 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
3240 	}
3241 	ilport->ilport_tg =
3242 	    stmf_lookup_group_for_target(lport->lport_id->ident,
3243 	    lport->lport_id->ident_length);
3244 
3245 	/*
3246 	 * rtpid will/must be set if this is a standby port
3247 	 * only register ports that are not standby (proxy) ports
3248 	 * and ports that are alua participants (ilport_alua == 1)
3249 	 */
3250 	if (ilport->ilport_standby == 0) {
3251 		ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1);
3252 	}
3253 
3254 	if (stmf_state.stmf_alua_state == 1 &&
3255 	    ilport->ilport_standby == 0 &&
3256 	    ilport->ilport_alua == 1) {
3257 		stmf_ic_msg_t *ic_reg_port;
3258 		stmf_ic_msg_status_t ic_ret;
3259 		stmf_local_port_t *lport;
3260 		lport = ilport->ilport_lport;
3261 		ic_reg_port = ic_reg_port_msg_alloc(
3262 		    lport->lport_id, ilport->ilport_rtpid,
3263 		    0, NULL, stmf_proxy_msg_id);
3264 		if (ic_reg_port) {
3265 			ic_ret = ic_tx_msg(ic_reg_port);
3266 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3267 				ilport->ilport_reg_msgid = stmf_proxy_msg_id++;
3268 			} else {
3269 				cmn_err(CE_WARN, "error on port registration "
3270 				"port - %s", ilport->ilport_kstat_tgt_name);
3271 			}
3272 		}
3273 	}
3274 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
3275 	stmf_create_kstat_lport(ilport);
3276 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
3277 		stmf_workers_state = STMF_WORKERS_ENABLING;
3278 		start_workers = 1;
3279 	}
3280 	mutex_exit(&stmf_state.stmf_lock);
3281 
3282 	if (start_workers)
3283 		stmf_worker_init();
3284 
3285 	/*  the default state of LPORT */
3286 
3287 	if (stmf_state.stmf_default_lport_state == STMF_STATE_OFFLINE) {
3288 		ilport->ilport_prev_state = STMF_STATE_OFFLINE;
3289 	} else {
3290 		ilport->ilport_prev_state = STMF_STATE_ONLINE;
3291 		if (stmf_state.stmf_service_running) {
3292 			ssci.st_rflags = 0;
3293 			ssci.st_additional_info = NULL;
3294 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
3295 		}
3296 	}
3297 
3298 	/* XXX: Generate event */
3299 	return (STMF_SUCCESS);
3300 }
3301 
3302 stmf_status_t
3303 stmf_deregister_local_port(stmf_local_port_t *lport)
3304 {
3305 	stmf_i_local_port_t *ilport;
3306 
3307 	mutex_enter(&stmf_state.stmf_lock);
3308 	if (stmf_state.stmf_inventory_locked) {
3309 		mutex_exit(&stmf_state.stmf_lock);
3310 		return (STMF_BUSY);
3311 	}
3312 
3313 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3314 
3315 	/*
3316 	 * deregister ports that are not standby (proxy)
3317 	 */
3318 	if (stmf_state.stmf_alua_state == 1 &&
3319 	    ilport->ilport_standby == 0 &&
3320 	    ilport->ilport_alua == 1) {
3321 		stmf_ic_msg_t *ic_dereg_port;
3322 		stmf_ic_msg_status_t ic_ret;
3323 		ic_dereg_port = ic_dereg_port_msg_alloc(
3324 		    lport->lport_id, 0, NULL, stmf_proxy_msg_id);
3325 		if (ic_dereg_port) {
3326 			ic_ret = ic_tx_msg(ic_dereg_port);
3327 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3328 				stmf_proxy_msg_id++;
3329 			}
3330 		}
3331 	}
3332 
3333 	if (ilport->ilport_nsessions == 0) {
3334 		if (ilport->ilport_next)
3335 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
3336 		if (ilport->ilport_prev)
3337 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
3338 		else
3339 			stmf_state.stmf_ilportlist = ilport->ilport_next;
3340 		id_free(stmf_state.stmf_ilport_inst_space,
3341 		    ilport->ilport_instance);
3342 		rw_destroy(&ilport->ilport_lock);
3343 		stmf_state.stmf_nlports--;
3344 		if (lport->lport_pp) {
3345 			((stmf_i_port_provider_t *)
3346 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
3347 		}
3348 		ilport->ilport_tg = NULL;
3349 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
3350 	} else {
3351 		mutex_exit(&stmf_state.stmf_lock);
3352 		return (STMF_BUSY);
3353 	}
3354 	if (ilport->ilport_kstat_info) {
3355 		kmem_free(ilport->ilport_kstat_info->ks_data,
3356 		    ilport->ilport_kstat_info->ks_data_size);
3357 		kstat_delete(ilport->ilport_kstat_info);
3358 	}
3359 	if (ilport->ilport_kstat_io) {
3360 		kstat_delete(ilport->ilport_kstat_io);
3361 		mutex_destroy(&ilport->ilport_kstat_lock);
3362 	}
3363 	stmf_delete_itl_kstat_by_lport(ilport->ilport_kstat_tgt_name);
3364 	mutex_exit(&stmf_state.stmf_lock);
3365 	return (STMF_SUCCESS);
3366 }
3367 
3368 /*
3369  * Rport id/instance mappings remain valid until STMF is unloaded
3370  */
3371 static int
3372 stmf_irport_compare(const void *void_irport1, const void *void_irport2)
3373 {
3374 	const	stmf_i_remote_port_t	*irport1 = void_irport1;
3375 	const	stmf_i_remote_port_t	*irport2 = void_irport2;
3376 	int			result;
3377 
3378 	/* Sort by code set then ident */
3379 	if (irport1->irport_id->code_set <
3380 	    irport2->irport_id->code_set) {
3381 		return (-1);
3382 	} else if (irport1->irport_id->code_set >
3383 	    irport2->irport_id->code_set) {
3384 		return (1);
3385 	}
3386 
3387 	/* Next by ident length */
3388 	if (irport1->irport_id->ident_length <
3389 	    irport2->irport_id->ident_length) {
3390 		return (-1);
3391 	} else if (irport1->irport_id->ident_length >
3392 	    irport2->irport_id->ident_length) {
3393 		return (1);
3394 	}
3395 
3396 	/* Code set and ident length both match, now compare idents */
3397 	result = memcmp(irport1->irport_id->ident,
3398 	    irport2->irport_id->ident,
3399 	    irport1->irport_id->ident_length);
3400 
3401 	if (result < 0) {
3402 		return (-1);
3403 	} else if (result > 0) {
3404 		return (1);
3405 	}
3406 
3407 	return (0);
3408 }
3409 
3410 static stmf_i_remote_port_t *
3411 stmf_irport_create(scsi_devid_desc_t *rport_devid)
3412 {
3413 	int			alloc_len;
3414 	stmf_i_remote_port_t	*irport;
3415 
3416 	/*
3417 	 * Lookup will bump the refcnt if there's an existing rport
3418 	 * context for this identifier.
3419 	 */
3420 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3421 
3422 	alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3423 	    rport_devid->ident_length - 1;
3424 	irport = kmem_zalloc(alloc_len, KM_NOSLEEP);
3425 	if (irport == NULL) {
3426 		return (NULL);
3427 	}
3428 
3429 	irport->irport_instance =
3430 	    id_alloc_nosleep(stmf_state.stmf_irport_inst_space);
3431 	if (irport->irport_instance == -1) {
3432 		kmem_free(irport, alloc_len);
3433 		return (NULL);
3434 	}
3435 
3436 	irport->irport_id =
3437 	    (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */
3438 	bcopy(rport_devid, irport->irport_id,
3439 	    sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1);
3440 	irport->irport_refcnt = 1;
3441 	mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL);
3442 
3443 	return (irport);
3444 }
3445 
3446 static void
3447 stmf_irport_destroy(stmf_i_remote_port_t *irport)
3448 {
3449 	id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance);
3450 	mutex_destroy(&irport->irport_mutex);
3451 	kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3452 	    irport->irport_id->ident_length - 1);
3453 }
3454 
3455 static stmf_i_remote_port_t *
3456 stmf_irport_register(scsi_devid_desc_t *rport_devid)
3457 {
3458 	stmf_i_remote_port_t	*irport;
3459 
3460 	mutex_enter(&stmf_state.stmf_lock);
3461 
3462 	/*
3463 	 * Lookup will bump the refcnt if there's an existing rport
3464 	 * context for this identifier.
3465 	 */
3466 	if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) {
3467 		mutex_exit(&stmf_state.stmf_lock);
3468 		return (irport);
3469 	}
3470 
3471 	irport = stmf_irport_create(rport_devid);
3472 	if (irport == NULL) {
3473 		mutex_exit(&stmf_state.stmf_lock);
3474 		return (NULL);
3475 	}
3476 
3477 	avl_add(&stmf_state.stmf_irportlist, irport);
3478 	mutex_exit(&stmf_state.stmf_lock);
3479 
3480 	return (irport);
3481 }
3482 
3483 static stmf_i_remote_port_t *
3484 stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid)
3485 {
3486 	stmf_i_remote_port_t	*irport;
3487 	stmf_i_remote_port_t	tmp_irport;
3488 
3489 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3490 	tmp_irport.irport_id = rport_devid;
3491 	irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL);
3492 	if (irport != NULL) {
3493 		mutex_enter(&irport->irport_mutex);
3494 		irport->irport_refcnt++;
3495 		mutex_exit(&irport->irport_mutex);
3496 	}
3497 
3498 	return (irport);
3499 }
3500 
3501 static void
3502 stmf_irport_deregister(stmf_i_remote_port_t *irport)
3503 {
3504 	/*
3505 	 * If we were actually going to remove unreferenced remote ports
3506 	 * we would want to acquire stmf_state.stmf_lock before getting
3507 	 * the irport mutex.
3508 	 *
3509 	 * Instead we're just going to leave it there even if unreferenced.
3510 	 */
3511 	mutex_enter(&irport->irport_mutex);
3512 	irport->irport_refcnt--;
3513 	mutex_exit(&irport->irport_mutex);
3514 }
3515 
3516 /*
3517  * Port provider has to make sure that register/deregister session and
3518  * port are serialized calls.
3519  */
3520 stmf_status_t
3521 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3522 {
3523 	stmf_i_scsi_session_t *iss;
3524 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3525 	    lport->lport_stmf_private;
3526 	uint8_t		lun[8];
3527 
3528 	/*
3529 	 * Port state has to be online to register a scsi session. It is
3530 	 * possible that we started an offline operation and a new SCSI
3531 	 * session started at the same time (in that case also we are going
3532 	 * to fail the registeration). But any other state is simply
3533 	 * a bad port provider implementation.
3534 	 */
3535 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
3536 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
3537 			stmf_trace(lport->lport_alias, "Port is trying to "
3538 			    "register a session while the state is neither "
3539 			    "online nor offlining");
3540 		}
3541 		return (STMF_FAILURE);
3542 	}
3543 	bzero(lun, 8);
3544 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3545 	if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) {
3546 		stmf_trace(lport->lport_alias, "Could not register "
3547 		    "remote port during session registration");
3548 		return (STMF_FAILURE);
3549 	}
3550 
3551 	iss->iss_flags |= ISS_BEING_CREATED;
3552 
3553 	/* sessions use the ilport_lock. No separate lock is required */
3554 	iss->iss_lockp = &ilport->ilport_lock;
3555 
3556 	if (iss->iss_sm != NULL)
3557 		cmn_err(CE_PANIC, "create lun map called with non NULL map");
3558 	iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t),
3559 	    KM_SLEEP);
3560 
3561 	mutex_enter(&stmf_state.stmf_lock);
3562 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3563 	(void) stmf_session_create_lun_map(ilport, iss);
3564 	ilport->ilport_nsessions++;
3565 	iss->iss_next = ilport->ilport_ss_list;
3566 	ilport->ilport_ss_list = iss;
3567 	rw_exit(&ilport->ilport_lock);
3568 	mutex_exit(&stmf_state.stmf_lock);
3569 
3570 	iss->iss_creation_time = ddi_get_time();
3571 	ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1);
3572 	iss->iss_flags &= ~ISS_BEING_CREATED;
3573 	/* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */
3574 	iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED;
3575 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
3576 	    stmf_scsi_session_t *, ss);
3577 	return (STMF_SUCCESS);
3578 }
3579 
3580 void
3581 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3582 {
3583 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3584 	    lport->lport_stmf_private;
3585 	stmf_i_scsi_session_t *iss, **ppss;
3586 	int found = 0;
3587 	stmf_ic_msg_t *ic_session_dereg;
3588 	stmf_status_t ic_ret = STMF_FAILURE;
3589 
3590 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
3591 	    stmf_scsi_session_t *, ss);
3592 
3593 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3594 	if (ss->ss_rport_alias) {
3595 		ss->ss_rport_alias = NULL;
3596 	}
3597 
3598 try_dereg_ss_again:
3599 	mutex_enter(&stmf_state.stmf_lock);
3600 	atomic_and_32(&iss->iss_flags,
3601 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
3602 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
3603 		mutex_exit(&stmf_state.stmf_lock);
3604 		delay(1);
3605 		goto try_dereg_ss_again;
3606 	}
3607 
3608 	/* dereg proxy session if not standby port */
3609 	if (stmf_state.stmf_alua_state == 1 &&
3610 	    ilport->ilport_standby == 0 &&
3611 	    ilport->ilport_alua == 1) {
3612 		ic_session_dereg = ic_session_dereg_msg_alloc(
3613 		    ss, stmf_proxy_msg_id);
3614 		if (ic_session_dereg) {
3615 			ic_ret = ic_tx_msg(ic_session_dereg);
3616 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3617 				stmf_proxy_msg_id++;
3618 			}
3619 		}
3620 	}
3621 
3622 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3623 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
3624 	    ppss = &((*ppss)->iss_next)) {
3625 		if (iss == (*ppss)) {
3626 			*ppss = (*ppss)->iss_next;
3627 			found = 1;
3628 			break;
3629 		}
3630 	}
3631 	if (!found) {
3632 		cmn_err(CE_PANIC, "Deregister session called for non existent"
3633 		    " session");
3634 	}
3635 	ilport->ilport_nsessions--;
3636 
3637 	stmf_irport_deregister(iss->iss_irport);
3638 	(void) stmf_session_destroy_lun_map(ilport, iss);
3639 	rw_exit(&ilport->ilport_lock);
3640 	mutex_exit(&stmf_state.stmf_lock);
3641 }
3642 
3643 stmf_i_scsi_session_t *
3644 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
3645 {
3646 	stmf_i_local_port_t *ilport;
3647 	stmf_i_scsi_session_t *iss;
3648 
3649 	mutex_enter(&stmf_state.stmf_lock);
3650 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3651 	    ilport = ilport->ilport_next) {
3652 		rw_enter(&ilport->ilport_lock, RW_WRITER);
3653 		for (iss = ilport->ilport_ss_list; iss != NULL;
3654 		    iss = iss->iss_next) {
3655 			if (iss->iss_ss->ss_session_id == session_id) {
3656 				if (!stay_locked)
3657 					rw_exit(&ilport->ilport_lock);
3658 				mutex_exit(&stmf_state.stmf_lock);
3659 				return (iss);
3660 			}
3661 		}
3662 		rw_exit(&ilport->ilport_lock);
3663 	}
3664 	mutex_exit(&stmf_state.stmf_lock);
3665 	return (NULL);
3666 }
3667 
3668 #define	MAX_ALIAS		128
3669 
3670 static int
3671 stmf_itl_kstat_compare(const void *itl_kstat_1, const void *itl_kstat_2)
3672 {
3673 	const	stmf_i_itl_kstat_t	*kstat_nm1 = itl_kstat_1;
3674 	const	stmf_i_itl_kstat_t	*kstat_nm2 = itl_kstat_2;
3675 	int	ret;
3676 
3677 	ret = strcmp(kstat_nm1->iitl_kstat_nm, kstat_nm2->iitl_kstat_nm);
3678 	if (ret < 0) {
3679 		return (-1);
3680 	} else if (ret > 0) {
3681 		return (1);
3682 	}
3683 	return (0);
3684 }
3685 
3686 static stmf_i_itl_kstat_t *
3687 stmf_itl_kstat_lookup(char *kstat_nm)
3688 {
3689 	stmf_i_itl_kstat_t	tmp;
3690 	stmf_i_itl_kstat_t	*itl_kstat;
3691 
3692 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3693 	(void) strcpy(tmp.iitl_kstat_nm, kstat_nm);
3694 	itl_kstat = avl_find(&stmf_state.stmf_itl_kstat_list, &tmp, NULL);
3695 	return (itl_kstat);
3696 }
3697 
3698 static void
3699 stmf_delete_itl_kstat_by_lport(char *tgt)
3700 {
3701 	stmf_i_itl_kstat_t	*ks_itl, *next;
3702 
3703 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3704 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
3705 	for (; ks_itl != NULL; ks_itl = next) {
3706 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
3707 		if (strcmp(ks_itl->iitl_kstat_lport, tgt) == 0) {
3708 			stmf_teardown_itl_kstats(ks_itl);
3709 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
3710 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
3711 		}
3712 	}
3713 }
3714 
3715 static void
3716 stmf_delete_itl_kstat_by_guid(char *guid)
3717 {
3718 	stmf_i_itl_kstat_t	*ks_itl, *next;
3719 
3720 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3721 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
3722 	for (; ks_itl != NULL; ks_itl = next) {
3723 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
3724 		if (strcmp(ks_itl->iitl_kstat_guid, guid) == 0) {
3725 			stmf_teardown_itl_kstats(ks_itl);
3726 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
3727 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
3728 		}
3729 	}
3730 }
3731 
3732 static stmf_i_itl_kstat_t *
3733 stmf_itl_kstat_create(stmf_itl_data_t *itl, char *nm,
3734     scsi_devid_desc_t *lport, scsi_devid_desc_t *lun)
3735 {
3736 	stmf_i_itl_kstat_t	*ks_itl;
3737 	int			i, len;
3738 
3739 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3740 	if ((ks_itl = stmf_itl_kstat_lookup(nm)) != NULL)
3741 		return (ks_itl);
3742 
3743 	len = sizeof (stmf_i_itl_kstat_t);
3744 	ks_itl = kmem_zalloc(len, KM_NOSLEEP);
3745 	if (ks_itl == NULL)
3746 		return (NULL);
3747 
3748 	(void) strcpy(ks_itl->iitl_kstat_nm, nm);
3749 	bcopy(lport->ident, ks_itl->iitl_kstat_lport, lport->ident_length);
3750 	ks_itl->iitl_kstat_lport[lport->ident_length] = '\0';
3751 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
3752 		(void) sprintf(&ks_itl->iitl_kstat_guid[i * 2], "%02x",
3753 		    lun->ident[i]);
3754 	}
3755 	ks_itl->iitl_kstat_strbuf = itl->itl_kstat_strbuf;
3756 	ks_itl->iitl_kstat_strbuflen = itl->itl_kstat_strbuflen;
3757 	ks_itl->iitl_kstat_info = itl->itl_kstat_info;
3758 	ks_itl->iitl_kstat_taskq = itl->itl_kstat_taskq;
3759 	ks_itl->iitl_kstat_lu_xfer = itl->itl_kstat_lu_xfer;
3760 	ks_itl->iitl_kstat_lport_xfer = itl->itl_kstat_lport_xfer;
3761 	avl_add(&stmf_state.stmf_itl_kstat_list, ks_itl);
3762 
3763 	return (ks_itl);
3764 }
3765 
3766 stmf_status_t
3767 stmf_setup_itl_kstats(stmf_itl_data_t *itl)
3768 {
3769 	char				ks_itl_id[32];
3770 	char				ks_nm[KSTAT_STRLEN];
3771 	char				ks_itl_nm[KSTAT_STRLEN];
3772 	stmf_kstat_itl_info_t		*ks_itl;
3773 	stmf_scsi_session_t		*ss;
3774 	stmf_i_scsi_session_t		*iss;
3775 	stmf_i_local_port_t		*ilport;
3776 	char				*strbuf;
3777 	int				id, len, i;
3778 	char				*rport_alias;
3779 	char				*lport_alias;
3780 	char				*lu_alias;
3781 	stmf_i_itl_kstat_t		*tmp_kstat;
3782 
3783 	/*
3784 	 * Allocate enough memory in the ITL to hold the relevant
3785 	 * identifiers.
3786 	 * rport and lport identifiers come from the stmf_scsi_session_t.
3787 	 * ident might not be null terminated.
3788 	 */
3789 	ss = itl->itl_session->iss_ss;
3790 	iss = ss->ss_stmf_private;
3791 	ilport = ss->ss_lport->lport_stmf_private;
3792 	(void) snprintf(ks_itl_id, 32, "%d.%d.%d",
3793 	    iss->iss_irport->irport_instance, ilport->ilport_instance,
3794 	    itl->itl_lun);
3795 
3796 	(void) snprintf(ks_itl_nm, KSTAT_STRLEN, "itl_%s", ks_itl_id);
3797 	/*
3798 	 * let's verify this itl_kstat already exist
3799 	 */
3800 	if ((tmp_kstat = stmf_itl_kstat_lookup(ks_itl_nm)) != NULL) {
3801 		itl->itl_kstat_strbuf = tmp_kstat->iitl_kstat_strbuf;
3802 		itl->itl_kstat_strbuflen = tmp_kstat->iitl_kstat_strbuflen;
3803 		itl->itl_kstat_info = tmp_kstat->iitl_kstat_info;
3804 		itl->itl_kstat_taskq = tmp_kstat->iitl_kstat_taskq;
3805 		itl->itl_kstat_lu_xfer = tmp_kstat->iitl_kstat_lu_xfer;
3806 		itl->itl_kstat_lport_xfer = tmp_kstat->iitl_kstat_lport_xfer;
3807 		return (STMF_SUCCESS);
3808 	}
3809 
3810 	/* New itl_kstat */
3811 	rport_alias = (ss->ss_rport_alias == NULL) ?
3812 	    "" : ss->ss_rport_alias;
3813 	lport_alias = (ss->ss_lport->lport_alias == NULL) ?
3814 	    "" : ss->ss_lport->lport_alias;
3815 	lu_alias = (itl->itl_ilu->ilu_lu->lu_alias == NULL) ?
3816 	    "" : itl->itl_ilu->ilu_lu->lu_alias;
3817 
3818 	itl->itl_kstat_strbuflen = (ss->ss_rport_id->ident_length + 1) +
3819 	    (strnlen(rport_alias, MAX_ALIAS) + 1) +
3820 	    (ss->ss_lport->lport_id->ident_length + 1) +
3821 	    (strnlen(lport_alias, MAX_ALIAS) + 1) +
3822 	    (STMF_GUID_INPUT + 1) +
3823 	    (strnlen(lu_alias, MAX_ALIAS) + 1) +
3824 	    MAX_PROTO_STR_LEN;
3825 	itl->itl_kstat_strbuf = kmem_zalloc(itl->itl_kstat_strbuflen,
3826 	    KM_NOSLEEP);
3827 	if (itl->itl_kstat_strbuf == NULL) {
3828 		return (STMF_ALLOC_FAILURE);
3829 	}
3830 
3831 	ks_itl = (stmf_kstat_itl_info_t *)kmem_zalloc(sizeof (*ks_itl),
3832 	    KM_NOSLEEP);
3833 	if (ks_itl == NULL) {
3834 		kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
3835 		return (STMF_ALLOC_FAILURE);
3836 	}
3837 
3838 	if ((itl->itl_kstat_info = kstat_create(STMF_MODULE_NAME,
3839 	    0, ks_itl_nm, "misc", KSTAT_TYPE_NAMED,
3840 	    sizeof (stmf_kstat_itl_info_t) / sizeof (kstat_named_t),
3841 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
3842 		goto itl_kstat_cleanup;
3843 	}
3844 
3845 	itl->itl_kstat_info->ks_data_size += itl->itl_kstat_strbuflen;
3846 	itl->itl_kstat_info->ks_data = ks_itl;
3847 
3848 	kstat_named_init(&ks_itl->i_rport_name, "rport-name",
3849 	    KSTAT_DATA_STRING);
3850 	kstat_named_init(&ks_itl->i_rport_alias, "rport-alias",
3851 	    KSTAT_DATA_STRING);
3852 	kstat_named_init(&ks_itl->i_lport_name, "lport-name",
3853 	    KSTAT_DATA_STRING);
3854 	kstat_named_init(&ks_itl->i_lport_alias, "lport-alias",
3855 	    KSTAT_DATA_STRING);
3856 	kstat_named_init(&ks_itl->i_protocol, "protocol",
3857 	    KSTAT_DATA_STRING);
3858 	kstat_named_init(&ks_itl->i_lu_guid, "lu-guid",
3859 	    KSTAT_DATA_STRING);
3860 	kstat_named_init(&ks_itl->i_lu_alias, "lu-alias",
3861 	    KSTAT_DATA_STRING);
3862 	kstat_named_init(&ks_itl->i_lu_number, "lu-number",
3863 	    KSTAT_DATA_UINT64);
3864 	kstat_named_init(&ks_itl->i_task_waitq_elapsed, "task-waitq-elapsed",
3865 	    KSTAT_DATA_UINT64);
3866 	kstat_named_init(&ks_itl->i_task_read_elapsed, "task-read-elapsed",
3867 	    KSTAT_DATA_UINT64);
3868 	kstat_named_init(&ks_itl->i_task_write_elapsed, "task-write-elapsed",
3869 	    KSTAT_DATA_UINT64);
3870 	kstat_named_init(&ks_itl->i_lu_read_elapsed, "lu-read-elapsed",
3871 	    KSTAT_DATA_UINT64);
3872 	kstat_named_init(&ks_itl->i_lu_write_elapsed, "lu-write-elapsed",
3873 	    KSTAT_DATA_UINT64);
3874 	kstat_named_init(&ks_itl->i_lport_read_elapsed, "lport-read-elapsed",
3875 	    KSTAT_DATA_UINT64);
3876 	kstat_named_init(&ks_itl->i_lport_write_elapsed, "lport-write-elapsed",
3877 	    KSTAT_DATA_UINT64);
3878 
3879 	strbuf = itl->itl_kstat_strbuf;
3880 
3881 	/* Rport */
3882 	len = ss->ss_rport_id->ident_length;
3883 	bcopy(ss->ss_rport_id->ident, strbuf, len);
3884 	strbuf += len;
3885 	*strbuf = '\0';
3886 	kstat_named_setstr(&ks_itl->i_rport_name, strbuf - len);
3887 	strbuf++;
3888 
3889 	len = strnlen(rport_alias, MAX_ALIAS);
3890 	(void) strncpy(strbuf, rport_alias, len + 1);
3891 	kstat_named_setstr(&ks_itl->i_rport_alias, strbuf);
3892 	strbuf += len + 1;
3893 
3894 	/* Lport */
3895 	len = ss->ss_lport->lport_id->ident_length;
3896 	bcopy(ss->ss_lport->lport_id->ident, strbuf, len);
3897 	strbuf += len;
3898 	*strbuf = '\0';
3899 	kstat_named_setstr(&ks_itl->i_lport_name, strbuf - len);
3900 	strbuf++;
3901 
3902 	len = strnlen(lport_alias, MAX_ALIAS);
3903 	(void) strncpy(strbuf, lport_alias, len + 1);
3904 	kstat_named_setstr(&ks_itl->i_lport_alias, strbuf);
3905 	strbuf += len + 1;
3906 
3907 	id = (ss->ss_lport->lport_id->protocol_id > PROTOCOL_ANY) ?
3908 	    PROTOCOL_ANY : ss->ss_lport->lport_id->protocol_id;
3909 	kstat_named_setstr(&ks_itl->i_protocol, protocol_ident[id]);
3910 
3911 	/* LU */
3912 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
3913 		(void) sprintf(&strbuf[i * 2], "%02x",
3914 		    itl->itl_ilu->ilu_lu->lu_id->ident[i]);
3915 	}
3916 	kstat_named_setstr(&ks_itl->i_lu_guid, strbuf);
3917 	strbuf += STMF_GUID_INPUT + 1;
3918 
3919 	len = strnlen(lu_alias, MAX_ALIAS);
3920 	(void) strncpy(strbuf, lu_alias, len + 1);
3921 	kstat_named_setstr(&ks_itl->i_lu_alias, strbuf);
3922 	strbuf += len + 1;
3923 
3924 	ks_itl->i_lu_number.value.ui64 = itl->itl_lun;
3925 
3926 	/* Now create the I/O kstats */
3927 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_tasks_%s",  ks_itl_id);
3928 	if ((itl->itl_kstat_taskq = kstat_create(STMF_MODULE_NAME, 0,
3929 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3930 		goto itl_kstat_cleanup;
3931 	}
3932 
3933 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lu_%s",  ks_itl_id);
3934 	if ((itl->itl_kstat_lu_xfer = kstat_create(STMF_MODULE_NAME, 0,
3935 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3936 		goto itl_kstat_cleanup;
3937 	}
3938 
3939 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lport_%s",  ks_itl_id);
3940 	if ((itl->itl_kstat_lport_xfer = kstat_create(STMF_MODULE_NAME, 0,
3941 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3942 		goto itl_kstat_cleanup;
3943 	}
3944 
3945 	/* Install all the kstats */
3946 	kstat_install(itl->itl_kstat_info);
3947 	kstat_install(itl->itl_kstat_taskq);
3948 	kstat_install(itl->itl_kstat_lu_xfer);
3949 	kstat_install(itl->itl_kstat_lport_xfer);
3950 
3951 	/* Add new itl_kstat to stmf_itl_kstat_list */
3952 	if (stmf_itl_kstat_create(itl, ks_itl_nm, ss->ss_lport->lport_id,
3953 	    itl->itl_ilu->ilu_lu->lu_id) != NULL)
3954 		return (STMF_SUCCESS);
3955 
3956 itl_kstat_cleanup:
3957 	if (itl->itl_kstat_taskq)
3958 		kstat_delete(itl->itl_kstat_taskq);
3959 	if (itl->itl_kstat_lu_xfer)
3960 		kstat_delete(itl->itl_kstat_lu_xfer);
3961 	if (itl->itl_kstat_lport_xfer)
3962 		kstat_delete(itl->itl_kstat_lport_xfer);
3963 	if (itl->itl_kstat_info)
3964 		kstat_delete(itl->itl_kstat_info);
3965 	kmem_free(ks_itl, sizeof (*ks_itl));
3966 	kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
3967 	cmn_err(CE_WARN, "STMF: kstat_create itl failed");
3968 	return (STMF_ALLOC_FAILURE);
3969 }
3970 
3971 static void
3972 stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks)
3973 {
3974 	kstat_delete(ks->iitl_kstat_lport_xfer);
3975 	kstat_delete(ks->iitl_kstat_lu_xfer);
3976 	kstat_delete(ks->iitl_kstat_taskq);
3977 	kmem_free(ks->iitl_kstat_info->ks_data, sizeof (stmf_kstat_itl_info_t));
3978 	kstat_delete(ks->iitl_kstat_info);
3979 	kmem_free(ks->iitl_kstat_strbuf, ks->iitl_kstat_strbuflen);
3980 }
3981 
3982 void
3983 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
3984 {
3985 	stmf_itl_data_t **itlpp;
3986 	stmf_i_lu_t *ilu;
3987 
3988 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
3989 
3990 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3991 	mutex_enter(&ilu->ilu_task_lock);
3992 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
3993 	    itlpp = &(*itlpp)->itl_next) {
3994 		if ((*itlpp) == itl)
3995 			break;
3996 	}
3997 	ASSERT((*itlpp) != NULL);
3998 	*itlpp = itl->itl_next;
3999 	mutex_exit(&ilu->ilu_task_lock);
4000 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
4001 	    (uint32_t)itl->itl_hdlrm_reason);
4002 
4003 	kmem_free(itl, sizeof (*itl));
4004 }
4005 
4006 stmf_status_t
4007 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
4008     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
4009 {
4010 	stmf_itl_data_t *itl;
4011 	stmf_i_scsi_session_t *iss;
4012 	stmf_lun_map_ent_t *lun_map_ent;
4013 	stmf_i_lu_t *ilu;
4014 	uint16_t n;
4015 
4016 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4017 	if (ss == NULL) {
4018 		iss = stmf_session_id_to_issptr(session_id, 1);
4019 		if (iss == NULL)
4020 			return (STMF_NOT_FOUND);
4021 	} else {
4022 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4023 	}
4024 
4025 	/*
4026 	 * Acquire stmf_lock for stmf_itl_kstat_lookup.
4027 	 */
4028 	mutex_enter(&stmf_state.stmf_lock);
4029 	rw_enter(iss->iss_lockp, RW_WRITER);
4030 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4031 	lun_map_ent = (stmf_lun_map_ent_t *)
4032 	    stmf_get_ent_from_map(iss->iss_sm, n);
4033 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
4034 		rw_exit(iss->iss_lockp);
4035 		mutex_exit(&stmf_state.stmf_lock);
4036 		return (STMF_NOT_FOUND);
4037 	}
4038 	if (lun_map_ent->ent_itl_datap != NULL) {
4039 		rw_exit(iss->iss_lockp);
4040 		mutex_exit(&stmf_state.stmf_lock);
4041 		return (STMF_ALREADY);
4042 	}
4043 
4044 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
4045 	if (itl == NULL) {
4046 		rw_exit(iss->iss_lockp);
4047 		mutex_exit(&stmf_state.stmf_lock);
4048 		return (STMF_ALLOC_FAILURE);
4049 	}
4050 
4051 	itl->itl_ilu = ilu;
4052 	itl->itl_session = iss;
4053 	itl->itl_counter = 1;
4054 	itl->itl_lun = n;
4055 	itl->itl_handle = itl_handle;
4056 
4057 	if (stmf_setup_itl_kstats(itl) != STMF_SUCCESS) {
4058 		kmem_free(itl, sizeof (*itl));
4059 		rw_exit(iss->iss_lockp);
4060 		mutex_exit(&stmf_state.stmf_lock);
4061 		return (STMF_ALLOC_FAILURE);
4062 	}
4063 
4064 	mutex_enter(&ilu->ilu_task_lock);
4065 	itl->itl_next = ilu->ilu_itl_list;
4066 	ilu->ilu_itl_list = itl;
4067 	mutex_exit(&ilu->ilu_task_lock);
4068 	lun_map_ent->ent_itl_datap = itl;
4069 	rw_exit(iss->iss_lockp);
4070 	mutex_exit(&stmf_state.stmf_lock);
4071 
4072 	return (STMF_SUCCESS);
4073 }
4074 
4075 void
4076 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
4077 {
4078 	uint8_t old, new;
4079 
4080 	do {
4081 		old = new = itl->itl_flags;
4082 		if (old & STMF_ITL_BEING_TERMINATED)
4083 			return;
4084 		new |= STMF_ITL_BEING_TERMINATED;
4085 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
4086 	itl->itl_hdlrm_reason = hdlrm_reason;
4087 
4088 	ASSERT(itl->itl_counter);
4089 
4090 	if (atomic_add_32_nv(&itl->itl_counter, -1))
4091 		return;
4092 
4093 	drv_usecwait(10);
4094 	if (itl->itl_counter)
4095 		return;
4096 
4097 	stmf_release_itl_handle(lu, itl);
4098 }
4099 
4100 stmf_status_t
4101 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
4102 {
4103 	stmf_i_lu_t *ilu;
4104 	stmf_i_local_port_t *ilport;
4105 	stmf_i_scsi_session_t *iss;
4106 	stmf_lun_map_t *lm;
4107 	stmf_lun_map_ent_t *ent;
4108 	uint32_t nmaps, nu;
4109 	stmf_itl_data_t **itl_list;
4110 	int i;
4111 
4112 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4113 
4114 dereg_itl_start:;
4115 	nmaps = ilu->ilu_ref_cnt;
4116 	if (nmaps == 0)
4117 		return (STMF_NOT_FOUND);
4118 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
4119 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
4120 	mutex_enter(&stmf_state.stmf_lock);
4121 	if (nmaps != ilu->ilu_ref_cnt) {
4122 		/* Something changed, start all over */
4123 		mutex_exit(&stmf_state.stmf_lock);
4124 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
4125 		goto dereg_itl_start;
4126 	}
4127 	nu = 0;
4128 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
4129 	    ilport = ilport->ilport_next) {
4130 		rw_enter(&ilport->ilport_lock, RW_WRITER);
4131 		for (iss = ilport->ilport_ss_list; iss != NULL;
4132 		    iss = iss->iss_next) {
4133 			lm = iss->iss_sm;
4134 			if (!lm)
4135 				continue;
4136 			for (i = 0; i < lm->lm_nentries; i++) {
4137 				if (lm->lm_plus[i] == NULL)
4138 					continue;
4139 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4140 				if ((ent->ent_lu == lu) &&
4141 				    (ent->ent_itl_datap)) {
4142 					itl_list[nu++] = ent->ent_itl_datap;
4143 					ent->ent_itl_datap = NULL;
4144 					if (nu == nmaps) {
4145 						rw_exit(&ilport->ilport_lock);
4146 						goto dai_scan_done;
4147 					}
4148 				}
4149 			} /* lun table for a session */
4150 		} /* sessions */
4151 		rw_exit(&ilport->ilport_lock);
4152 	} /* ports */
4153 
4154 dai_scan_done:
4155 	mutex_exit(&stmf_state.stmf_lock);
4156 
4157 	for (i = 0; i < nu; i++) {
4158 		stmf_do_itl_dereg(lu, itl_list[i],
4159 		    STMF_ITL_REASON_DEREG_REQUEST);
4160 	}
4161 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
4162 
4163 	return (STMF_SUCCESS);
4164 }
4165 
4166 stmf_status_t
4167 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun,
4168     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
4169 {
4170 	stmf_i_scsi_session_t *iss;
4171 	stmf_itl_data_t *itl;
4172 	stmf_lun_map_ent_t *ent;
4173 	stmf_lun_map_t *lm;
4174 	int i;
4175 	uint16_t n;
4176 
4177 	if (ss == NULL) {
4178 		if (session_id == STMF_SESSION_ID_NONE)
4179 			return (STMF_INVALID_ARG);
4180 		iss = stmf_session_id_to_issptr(session_id, 1);
4181 		if (iss == NULL)
4182 			return (STMF_NOT_FOUND);
4183 	} else {
4184 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4185 		rw_enter(iss->iss_lockp, RW_WRITER);
4186 	}
4187 	lm = iss->iss_sm;
4188 	if (lm == NULL) {
4189 		rw_exit(iss->iss_lockp);
4190 		return (STMF_NOT_FOUND);
4191 	}
4192 
4193 	if (lun) {
4194 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4195 		ent = (stmf_lun_map_ent_t *)
4196 		    stmf_get_ent_from_map(iss->iss_sm, n);
4197 	} else {
4198 		if (itl_handle == NULL) {
4199 			rw_exit(iss->iss_lockp);
4200 			return (STMF_INVALID_ARG);
4201 		}
4202 		ent = NULL;
4203 		for (i = 0; i < lm->lm_nentries; i++) {
4204 			if (lm->lm_plus[i] == NULL)
4205 				continue;
4206 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4207 			if (ent->ent_itl_datap &&
4208 			    (ent->ent_itl_datap->itl_handle == itl_handle)) {
4209 				break;
4210 			}
4211 		}
4212 	}
4213 	if ((ent == NULL) || (ent->ent_lu != lu) ||
4214 	    (ent->ent_itl_datap == NULL)) {
4215 		rw_exit(iss->iss_lockp);
4216 		return (STMF_NOT_FOUND);
4217 	}
4218 	itl = ent->ent_itl_datap;
4219 	ent->ent_itl_datap = NULL;
4220 	rw_exit(iss->iss_lockp);
4221 	stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST);
4222 
4223 	return (STMF_SUCCESS);
4224 }
4225 
4226 stmf_status_t
4227 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
4228     uint64_t session_id, void **itl_handle_retp)
4229 {
4230 	stmf_i_scsi_session_t *iss;
4231 	stmf_lun_map_ent_t *ent;
4232 	stmf_lun_map_t *lm;
4233 	stmf_status_t ret;
4234 	int i;
4235 	uint16_t n;
4236 
4237 	if (ss == NULL) {
4238 		iss = stmf_session_id_to_issptr(session_id, 1);
4239 		if (iss == NULL)
4240 			return (STMF_NOT_FOUND);
4241 	} else {
4242 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4243 		rw_enter(iss->iss_lockp, RW_WRITER);
4244 	}
4245 
4246 	ent = NULL;
4247 	if (lun == NULL) {
4248 		lm = iss->iss_sm;
4249 		for (i = 0; i < lm->lm_nentries; i++) {
4250 			if (lm->lm_plus[i] == NULL)
4251 				continue;
4252 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4253 			if (ent->ent_lu == lu)
4254 				break;
4255 		}
4256 	} else {
4257 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4258 		ent = (stmf_lun_map_ent_t *)
4259 		    stmf_get_ent_from_map(iss->iss_sm, n);
4260 		if (lu && (ent->ent_lu != lu))
4261 			ent = NULL;
4262 	}
4263 	if (ent && ent->ent_itl_datap) {
4264 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
4265 		ret = STMF_SUCCESS;
4266 	} else {
4267 		ret = STMF_NOT_FOUND;
4268 	}
4269 
4270 	rw_exit(iss->iss_lockp);
4271 	return (ret);
4272 }
4273 
4274 stmf_data_buf_t *
4275 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
4276     uint32_t flags)
4277 {
4278 	stmf_i_scsi_task_t *itask =
4279 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4280 	stmf_local_port_t *lport = task->task_lport;
4281 	stmf_data_buf_t *dbuf;
4282 	uint8_t ndx;
4283 
4284 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
4285 	if (ndx == 0xff)
4286 		return (NULL);
4287 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
4288 	    task, size, pminsize, flags);
4289 	if (dbuf) {
4290 		task->task_cur_nbufs++;
4291 		itask->itask_allocated_buf_map |= (1 << ndx);
4292 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4293 		dbuf->db_handle = ndx;
4294 		return (dbuf);
4295 	}
4296 
4297 	return (NULL);
4298 }
4299 
4300 stmf_status_t
4301 stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags)
4302 {
4303 	stmf_i_scsi_task_t *itask =
4304 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4305 	stmf_local_port_t *lport = task->task_lport;
4306 	uint8_t ndx;
4307 	stmf_status_t ret;
4308 
4309 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
4310 	ASSERT(lport->lport_ds->ds_setup_dbuf != NULL);
4311 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
4312 
4313 	if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0)
4314 		return (STMF_FAILURE);
4315 	if (lport->lport_ds->ds_setup_dbuf == NULL)
4316 		return (STMF_FAILURE);
4317 
4318 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
4319 	if (ndx == 0xff)
4320 		return (STMF_FAILURE);
4321 	ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags);
4322 	if (ret == STMF_FAILURE)
4323 		return (STMF_FAILURE);
4324 	itask->itask_dbufs[ndx] = dbuf;
4325 	task->task_cur_nbufs++;
4326 	itask->itask_allocated_buf_map |= (1 << ndx);
4327 	dbuf->db_handle = ndx;
4328 
4329 	return (STMF_SUCCESS);
4330 }
4331 
4332 void
4333 stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
4334 {
4335 	stmf_i_scsi_task_t *itask =
4336 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4337 	stmf_local_port_t *lport = task->task_lport;
4338 
4339 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
4340 	ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL);
4341 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
4342 
4343 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
4344 	task->task_cur_nbufs--;
4345 	lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf);
4346 }
4347 
4348 void
4349 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
4350 {
4351 	stmf_i_scsi_task_t *itask =
4352 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4353 	stmf_local_port_t *lport = task->task_lport;
4354 
4355 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
4356 	task->task_cur_nbufs--;
4357 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
4358 }
4359 
4360 stmf_data_buf_t *
4361 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
4362 {
4363 	stmf_i_scsi_task_t *itask;
4364 
4365 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4366 	if (h > 3)
4367 		return (NULL);
4368 	return (itask->itask_dbufs[h]);
4369 }
4370 
4371 /* ARGSUSED */
4372 struct scsi_task *
4373 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
4374 			uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
4375 {
4376 	stmf_lu_t *lu;
4377 	stmf_i_scsi_session_t *iss;
4378 	stmf_i_lu_t *ilu;
4379 	stmf_i_scsi_task_t *itask;
4380 	stmf_i_scsi_task_t **ppitask;
4381 	scsi_task_t *task;
4382 	uint8_t	*l;
4383 	stmf_lun_map_ent_t *lun_map_ent;
4384 	uint16_t cdb_length;
4385 	uint16_t luNbr;
4386 	uint8_t new_task = 0;
4387 
4388 	/*
4389 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
4390 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
4391 	 * depend upon this alignment.
4392 	 */
4393 	if (cdb_length_in >= 16)
4394 		cdb_length = cdb_length_in + 7;
4395 	else
4396 		cdb_length = 16 + 7;
4397 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4398 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4399 	rw_enter(iss->iss_lockp, RW_READER);
4400 	lun_map_ent =
4401 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
4402 	if (!lun_map_ent) {
4403 		lu = dlun0;
4404 	} else {
4405 		lu = lun_map_ent->ent_lu;
4406 	}
4407 	ilu = lu->lu_stmf_private;
4408 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4409 		rw_exit(iss->iss_lockp);
4410 		return (NULL);
4411 	}
4412 	do {
4413 		if (ilu->ilu_free_tasks == NULL) {
4414 			new_task = 1;
4415 			break;
4416 		}
4417 		mutex_enter(&ilu->ilu_task_lock);
4418 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
4419 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
4420 		    ppitask = &((*ppitask)->itask_lu_free_next))
4421 			;
4422 		if (*ppitask) {
4423 			itask = *ppitask;
4424 			*ppitask = (*ppitask)->itask_lu_free_next;
4425 			ilu->ilu_ntasks_free--;
4426 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
4427 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4428 		} else {
4429 			new_task = 1;
4430 		}
4431 		mutex_exit(&ilu->ilu_task_lock);
4432 	/* CONSTCOND */
4433 	} while (0);
4434 
4435 	if (!new_task) {
4436 		/*
4437 		 * Save the task_cdb pointer and zero per cmd fields.
4438 		 * We know the task_cdb_length is large enough by task
4439 		 * selection process above.
4440 		 */
4441 		uint8_t *save_cdb;
4442 		uintptr_t t_start, t_end;
4443 
4444 		task = itask->itask_task;
4445 		save_cdb = task->task_cdb;	/* save */
4446 		t_start = (uintptr_t)&task->task_flags;
4447 		t_end = (uintptr_t)&task->task_extended_cmd;
4448 		bzero((void *)t_start, (size_t)(t_end - t_start));
4449 		task->task_cdb = save_cdb;	/* restore */
4450 		itask->itask_ncmds = 0;
4451 	} else {
4452 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
4453 		    cdb_length, AF_FORCE_NOSLEEP);
4454 		if (task == NULL) {
4455 			rw_exit(iss->iss_lockp);
4456 			return (NULL);
4457 		}
4458 		task->task_lu = lu;
4459 		l = task->task_lun_no;
4460 		l[0] = lun[0];
4461 		l[1] = lun[1];
4462 		l[2] = lun[2];
4463 		l[3] = lun[3];
4464 		l[4] = lun[4];
4465 		l[5] = lun[5];
4466 		l[6] = lun[6];
4467 		l[7] = lun[7];
4468 		task->task_cdb = (uint8_t *)task->task_port_private;
4469 		if ((ulong_t)(task->task_cdb) & 7ul) {
4470 			task->task_cdb = (uint8_t *)(((ulong_t)
4471 			    (task->task_cdb) + 7ul) & ~(7ul));
4472 		}
4473 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4474 		itask->itask_cdb_buf_size = cdb_length;
4475 		mutex_init(&itask->itask_audit_mutex, NULL, MUTEX_DRIVER, NULL);
4476 	}
4477 	task->task_session = ss;
4478 	task->task_lport = lport;
4479 	task->task_cdb_length = cdb_length_in;
4480 	itask->itask_flags = ITASK_IN_TRANSITION;
4481 	itask->itask_waitq_time = 0;
4482 	itask->itask_lu_read_time = itask->itask_lu_write_time = 0;
4483 	itask->itask_lport_read_time = itask->itask_lport_write_time = 0;
4484 	itask->itask_read_xfer = itask->itask_write_xfer = 0;
4485 	itask->itask_audit_index = 0;
4486 
4487 	if (new_task) {
4488 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
4489 			rw_exit(iss->iss_lockp);
4490 			stmf_free(task);
4491 			return (NULL);
4492 		}
4493 		mutex_enter(&ilu->ilu_task_lock);
4494 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4495 			mutex_exit(&ilu->ilu_task_lock);
4496 			rw_exit(iss->iss_lockp);
4497 			stmf_free(task);
4498 			return (NULL);
4499 		}
4500 		itask->itask_lu_next = ilu->ilu_tasks;
4501 		if (ilu->ilu_tasks)
4502 			ilu->ilu_tasks->itask_lu_prev = itask;
4503 		ilu->ilu_tasks = itask;
4504 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
4505 		ilu->ilu_ntasks++;
4506 		mutex_exit(&ilu->ilu_task_lock);
4507 	}
4508 
4509 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
4510 	atomic_add_32(itask->itask_ilu_task_cntr, 1);
4511 	itask->itask_start_time = ddi_get_lbolt();
4512 
4513 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
4514 	    lun_map_ent->ent_itl_datap) != NULL)) {
4515 		atomic_add_32(&itask->itask_itl_datap->itl_counter, 1);
4516 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
4517 	} else {
4518 		itask->itask_itl_datap = NULL;
4519 		task->task_lu_itl_handle = NULL;
4520 	}
4521 
4522 	rw_exit(iss->iss_lockp);
4523 	return (task);
4524 }
4525 
4526 static void
4527 stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss)
4528 {
4529 	stmf_i_scsi_task_t *itask =
4530 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4531 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4532 
4533 	ASSERT(rw_lock_held(iss->iss_lockp));
4534 	itask->itask_flags = ITASK_IN_FREE_LIST;
4535 	itask->itask_proxy_msg_id = 0;
4536 	mutex_enter(&ilu->ilu_task_lock);
4537 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
4538 	ilu->ilu_free_tasks = itask;
4539 	ilu->ilu_ntasks_free++;
4540 	mutex_exit(&ilu->ilu_task_lock);
4541 	atomic_add_32(itask->itask_ilu_task_cntr, -1);
4542 }
4543 
4544 void
4545 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
4546 {
4547 	uint32_t	num_to_release, ndx;
4548 	stmf_i_scsi_task_t *itask;
4549 	stmf_lu_t	*lu = ilu->ilu_lu;
4550 
4551 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
4552 
4553 	/* free half of the minimal free of the free tasks */
4554 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
4555 	if (!num_to_release) {
4556 		return;
4557 	}
4558 	for (ndx = 0; ndx < num_to_release; ndx++) {
4559 		mutex_enter(&ilu->ilu_task_lock);
4560 		itask = ilu->ilu_free_tasks;
4561 		if (itask == NULL) {
4562 			mutex_exit(&ilu->ilu_task_lock);
4563 			break;
4564 		}
4565 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
4566 		ilu->ilu_ntasks_free--;
4567 		mutex_exit(&ilu->ilu_task_lock);
4568 
4569 		lu->lu_task_free(itask->itask_task);
4570 		mutex_enter(&ilu->ilu_task_lock);
4571 		if (itask->itask_lu_next)
4572 			itask->itask_lu_next->itask_lu_prev =
4573 			    itask->itask_lu_prev;
4574 		if (itask->itask_lu_prev)
4575 			itask->itask_lu_prev->itask_lu_next =
4576 			    itask->itask_lu_next;
4577 		else
4578 			ilu->ilu_tasks = itask->itask_lu_next;
4579 
4580 		ilu->ilu_ntasks--;
4581 		mutex_exit(&ilu->ilu_task_lock);
4582 		stmf_free(itask->itask_task);
4583 	}
4584 }
4585 
4586 /*
4587  * Called with stmf_lock held
4588  */
4589 void
4590 stmf_check_freetask()
4591 {
4592 	stmf_i_lu_t *ilu;
4593 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4594 
4595 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
4596 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
4597 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
4598 		if (!ilu->ilu_ntasks_min_free) {
4599 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4600 			continue;
4601 		}
4602 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4603 		mutex_exit(&stmf_state.stmf_lock);
4604 		stmf_task_lu_check_freelist(ilu);
4605 		/*
4606 		 * we do not care about the accuracy of
4607 		 * ilu_ntasks_min_free, so we don't lock here
4608 		 */
4609 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4610 		mutex_enter(&stmf_state.stmf_lock);
4611 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4612 		cv_broadcast(&stmf_state.stmf_cv);
4613 		if (ddi_get_lbolt() >= endtime)
4614 			break;
4615 	}
4616 }
4617 
4618 void
4619 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
4620 {
4621 	clock_t l = ddi_get_lbolt();
4622 	clock_t ps = drv_usectohz(1000000);
4623 	stmf_i_scsi_task_t *itask;
4624 	scsi_task_t *task;
4625 	uint32_t to;
4626 
4627 	mutex_enter(&ilu->ilu_task_lock);
4628 	for (itask = ilu->ilu_tasks; itask != NULL;
4629 	    itask = itask->itask_lu_next) {
4630 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
4631 		    ITASK_BEING_ABORTED)) {
4632 			continue;
4633 		}
4634 		task = itask->itask_task;
4635 		if (task->task_timeout == 0)
4636 			to = stmf_default_task_timeout;
4637 		else
4638 			to = task->task_timeout;
4639 		if ((itask->itask_start_time + (to * ps)) > l)
4640 			continue;
4641 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4642 		    STMF_TIMEOUT, NULL);
4643 	}
4644 	mutex_exit(&ilu->ilu_task_lock);
4645 }
4646 
4647 /*
4648  * Called with stmf_lock held
4649  */
4650 void
4651 stmf_check_ilu_timing()
4652 {
4653 	stmf_i_lu_t *ilu;
4654 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4655 
4656 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
4657 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
4658 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
4659 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
4660 			if (ilu->ilu_task_cntr2 == 0) {
4661 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
4662 				continue;
4663 			}
4664 		} else {
4665 			if (ilu->ilu_task_cntr1 == 0) {
4666 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
4667 				continue;
4668 			}
4669 		}
4670 		/*
4671 		 * If we are here then it means that there is some slowdown
4672 		 * in tasks on this lu. We need to check.
4673 		 */
4674 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4675 		mutex_exit(&stmf_state.stmf_lock);
4676 		stmf_do_ilu_timeouts(ilu);
4677 		mutex_enter(&stmf_state.stmf_lock);
4678 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4679 		cv_broadcast(&stmf_state.stmf_cv);
4680 		if (ddi_get_lbolt() >= endtime)
4681 			break;
4682 	}
4683 }
4684 
4685 /*
4686  * Kills all tasks on a lu except tm_task
4687  */
4688 void
4689 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
4690 {
4691 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4692 	stmf_i_scsi_task_t *itask;
4693 
4694 	mutex_enter(&ilu->ilu_task_lock);
4695 
4696 	for (itask = ilu->ilu_tasks; itask != NULL;
4697 	    itask = itask->itask_lu_next) {
4698 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
4699 			continue;
4700 		if (itask->itask_task == tm_task)
4701 			continue;
4702 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
4703 	}
4704 	mutex_exit(&ilu->ilu_task_lock);
4705 }
4706 
4707 void
4708 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
4709 {
4710 	int i;
4711 	uint8_t map;
4712 
4713 	if ((map = itask->itask_allocated_buf_map) == 0)
4714 		return;
4715 	for (i = 0; i < 4; i++) {
4716 		if (map & 1) {
4717 			stmf_data_buf_t *dbuf;
4718 
4719 			dbuf = itask->itask_dbufs[i];
4720 			if (dbuf->db_xfer_start_timestamp) {
4721 				stmf_lport_xfer_done(itask, dbuf);
4722 			}
4723 			if (dbuf->db_flags & DB_LU_DATA_BUF) {
4724 				/*
4725 				 * LU needs to clean up buffer.
4726 				 * LU is required to free the buffer
4727 				 * in the xfer_done handler.
4728 				 */
4729 				scsi_task_t *task = itask->itask_task;
4730 				stmf_lu_t *lu = task->task_lu;
4731 
4732 				lu->lu_dbuf_free(task, dbuf);
4733 				ASSERT(((itask->itask_allocated_buf_map>>i)
4734 				    & 1) == 0); /* must be gone */
4735 			} else {
4736 				ASSERT(dbuf->db_lu_private == NULL);
4737 				dbuf->db_lu_private = NULL;
4738 				lport->lport_ds->ds_free_data_buf(
4739 				    lport->lport_ds, dbuf);
4740 			}
4741 		}
4742 		map >>= 1;
4743 	}
4744 	itask->itask_allocated_buf_map = 0;
4745 }
4746 
4747 void
4748 stmf_task_free(scsi_task_t *task)
4749 {
4750 	stmf_local_port_t *lport = task->task_lport;
4751 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4752 	    task->task_stmf_private;
4753 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4754 	    task->task_session->ss_stmf_private;
4755 
4756 	stmf_task_audit(itask, TE_TASK_FREE, CMD_OR_IOF_NA, NULL);
4757 
4758 	stmf_free_task_bufs(itask, lport);
4759 	stmf_itl_task_done(itask);
4760 	DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task,
4761 	    hrtime_t,
4762 	    itask->itask_done_timestamp - itask->itask_start_timestamp);
4763 	if (itask->itask_itl_datap) {
4764 		if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter,
4765 		    -1) == 0) {
4766 			stmf_release_itl_handle(task->task_lu,
4767 			    itask->itask_itl_datap);
4768 		}
4769 	}
4770 
4771 	rw_enter(iss->iss_lockp, RW_READER);
4772 	lport->lport_task_free(task);
4773 	if (itask->itask_worker) {
4774 		atomic_add_32(&stmf_cur_ntasks, -1);
4775 		atomic_add_32(&itask->itask_worker->worker_ref_count, -1);
4776 	}
4777 	/*
4778 	 * After calling stmf_task_lu_free, the task pointer can no longer
4779 	 * be trusted.
4780 	 */
4781 	stmf_task_lu_free(task, iss);
4782 	rw_exit(iss->iss_lockp);
4783 }
4784 
4785 void
4786 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
4787 {
4788 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4789 	    task->task_stmf_private;
4790 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4791 	int nv;
4792 	uint32_t old, new;
4793 	uint32_t ct;
4794 	stmf_worker_t *w, *w1;
4795 	uint8_t tm;
4796 
4797 	if (task->task_max_nbufs > 4)
4798 		task->task_max_nbufs = 4;
4799 	task->task_cur_nbufs = 0;
4800 	/* Latest value of currently running tasks */
4801 	ct = atomic_add_32_nv(&stmf_cur_ntasks, 1);
4802 
4803 	/* Select the next worker using round robin */
4804 	nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1);
4805 	if (nv >= stmf_nworkers_accepting_cmds) {
4806 		int s = nv;
4807 		do {
4808 			nv -= stmf_nworkers_accepting_cmds;
4809 		} while (nv >= stmf_nworkers_accepting_cmds);
4810 		if (nv < 0)
4811 			nv = 0;
4812 		/* Its ok if this cas fails */
4813 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
4814 		    s, nv);
4815 	}
4816 	w = &stmf_workers[nv];
4817 
4818 	/*
4819 	 * A worker can be pinned by interrupt. So select the next one
4820 	 * if it has lower load.
4821 	 */
4822 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
4823 		w1 = stmf_workers;
4824 	} else {
4825 		w1 = &stmf_workers[nv + 1];
4826 	}
4827 	if (w1->worker_queue_depth < w->worker_queue_depth)
4828 		w = w1;
4829 
4830 	mutex_enter(&w->worker_lock);
4831 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
4832 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4833 		/*
4834 		 * Maybe we are in the middle of a change. Just go to
4835 		 * the 1st worker.
4836 		 */
4837 		mutex_exit(&w->worker_lock);
4838 		w = stmf_workers;
4839 		mutex_enter(&w->worker_lock);
4840 	}
4841 	itask->itask_worker = w;
4842 	/*
4843 	 * Track max system load inside the worker as we already have the
4844 	 * worker lock (no point implementing another lock). The service
4845 	 * thread will do the comparisons and figure out the max overall
4846 	 * system load.
4847 	 */
4848 	if (w->worker_max_sys_qdepth_pu < ct)
4849 		w->worker_max_sys_qdepth_pu = ct;
4850 
4851 	do {
4852 		old = new = itask->itask_flags;
4853 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
4854 		if (task->task_mgmt_function) {
4855 			tm = task->task_mgmt_function;
4856 			if ((tm == TM_TARGET_RESET) ||
4857 			    (tm == TM_TARGET_COLD_RESET) ||
4858 			    (tm == TM_TARGET_WARM_RESET)) {
4859 				new |= ITASK_DEFAULT_HANDLING;
4860 			}
4861 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4862 			new |= ITASK_DEFAULT_HANDLING;
4863 		}
4864 		new &= ~ITASK_IN_TRANSITION;
4865 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4866 
4867 	stmf_itl_task_start(itask);
4868 
4869 	itask->itask_worker_next = NULL;
4870 	if (w->worker_task_tail) {
4871 		w->worker_task_tail->itask_worker_next = itask;
4872 	} else {
4873 		w->worker_task_head = itask;
4874 	}
4875 	w->worker_task_tail = itask;
4876 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4877 		w->worker_max_qdepth_pu = w->worker_queue_depth;
4878 	}
4879 	/* Measure task waitq time */
4880 	itask->itask_waitq_enter_timestamp = gethrtime();
4881 	atomic_add_32(&w->worker_ref_count, 1);
4882 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
4883 	itask->itask_ncmds = 1;
4884 	stmf_task_audit(itask, TE_TASK_START, CMD_OR_IOF_NA, dbuf);
4885 	if (dbuf) {
4886 		itask->itask_allocated_buf_map = 1;
4887 		itask->itask_dbufs[0] = dbuf;
4888 		dbuf->db_handle = 0;
4889 	} else {
4890 		itask->itask_allocated_buf_map = 0;
4891 		itask->itask_dbufs[0] = NULL;
4892 	}
4893 
4894 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) {
4895 		w->worker_signal_timestamp = gethrtime();
4896 		DTRACE_PROBE2(worker__signal, stmf_worker_t *, w,
4897 		    scsi_task_t *, task);
4898 		cv_signal(&w->worker_cv);
4899 	}
4900 	mutex_exit(&w->worker_lock);
4901 
4902 	/*
4903 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
4904 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
4905 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
4906 	 */
4907 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4908 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
4909 	}
4910 }
4911 
4912 static void
4913 stmf_task_audit(stmf_i_scsi_task_t *itask,
4914     task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf)
4915 {
4916 	stmf_task_audit_rec_t *ar;
4917 
4918 	mutex_enter(&itask->itask_audit_mutex);
4919 	ar = &itask->itask_audit_records[itask->itask_audit_index++];
4920 	itask->itask_audit_index &= (ITASK_TASK_AUDIT_DEPTH - 1);
4921 	ar->ta_event = te;
4922 	ar->ta_cmd_or_iof = cmd_or_iof;
4923 	ar->ta_itask_flags = itask->itask_flags;
4924 	ar->ta_dbuf = dbuf;
4925 	gethrestime(&ar->ta_timestamp);
4926 	mutex_exit(&itask->itask_audit_mutex);
4927 }
4928 
4929 
4930 /*
4931  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
4932  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
4933  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
4934  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
4935  * the LU will make this call only if we call the LU's abort entry point.
4936  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
4937  *
4938  * Same logic applies for the port.
4939  *
4940  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
4941  * and KNOWN_TO_TGT_PORT are reset.
4942  *
4943  * +++++++++++++++++++++++++++++++++++++++++++++++
4944  */
4945 
4946 stmf_status_t
4947 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
4948 {
4949 	stmf_status_t ret = STMF_SUCCESS;
4950 
4951 	stmf_i_scsi_task_t *itask =
4952 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4953 
4954 	stmf_task_audit(itask, TE_XFER_START, ioflags, dbuf);
4955 
4956 	if (ioflags & STMF_IOF_LU_DONE) {
4957 		uint32_t new, old;
4958 		do {
4959 			new = old = itask->itask_flags;
4960 			if (new & ITASK_BEING_ABORTED)
4961 				return (STMF_ABORTED);
4962 			new &= ~ITASK_KNOWN_TO_LU;
4963 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4964 	}
4965 	if (itask->itask_flags & ITASK_BEING_ABORTED)
4966 		return (STMF_ABORTED);
4967 #ifdef	DEBUG
4968 	if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) {
4969 		if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) ==
4970 		    1)
4971 			return (STMF_SUCCESS);
4972 	}
4973 #endif
4974 
4975 	stmf_update_kstat_lu_io(task, dbuf);
4976 	stmf_update_kstat_lport_io(task, dbuf);
4977 	stmf_lport_xfer_start(itask, dbuf);
4978 	if (ioflags & STMF_IOF_STATS_ONLY) {
4979 		stmf_lport_xfer_done(itask, dbuf);
4980 		return (STMF_SUCCESS);
4981 	}
4982 
4983 	dbuf->db_flags |= DB_LPORT_XFER_ACTIVE;
4984 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
4985 
4986 	/*
4987 	 * Port provider may have already called the buffer callback in
4988 	 * which case dbuf->db_xfer_start_timestamp will be 0.
4989 	 */
4990 	if (ret != STMF_SUCCESS) {
4991 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4992 		if (dbuf->db_xfer_start_timestamp != 0)
4993 			stmf_lport_xfer_done(itask, dbuf);
4994 	}
4995 
4996 	return (ret);
4997 }
4998 
4999 void
5000 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
5001 {
5002 	stmf_i_scsi_task_t *itask =
5003 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5004 	stmf_i_local_port_t *ilport;
5005 	stmf_worker_t *w = itask->itask_worker;
5006 	uint32_t new, old;
5007 	uint8_t update_queue_flags, free_it, queue_it;
5008 
5009 	stmf_lport_xfer_done(itask, dbuf);
5010 
5011 	stmf_task_audit(itask, TE_XFER_DONE, iof, dbuf);
5012 
5013 	/* Guard against unexpected completions from the lport */
5014 	if (dbuf->db_flags & DB_LPORT_XFER_ACTIVE) {
5015 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
5016 	} else {
5017 		/*
5018 		 * This should never happen.
5019 		 */
5020 		ilport = task->task_lport->lport_stmf_private;
5021 		ilport->ilport_unexpected_comp++;
5022 		cmn_err(CE_PANIC, "Unexpected xfer completion task %p dbuf %p",
5023 		    (void *)task, (void *)dbuf);
5024 		return;
5025 	}
5026 
5027 	mutex_enter(&w->worker_lock);
5028 	do {
5029 		new = old = itask->itask_flags;
5030 		if (old & ITASK_BEING_ABORTED) {
5031 			mutex_exit(&w->worker_lock);
5032 			return;
5033 		}
5034 		free_it = 0;
5035 		if (iof & STMF_IOF_LPORT_DONE) {
5036 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5037 			task->task_completion_status = dbuf->db_xfer_status;
5038 			free_it = 1;
5039 		}
5040 		/*
5041 		 * If the task is known to LU then queue it. But if
5042 		 * it is already queued (multiple completions) then
5043 		 * just update the buffer information by grabbing the
5044 		 * worker lock. If the task is not known to LU,
5045 		 * completed/aborted, then see if we need to
5046 		 * free this task.
5047 		 */
5048 		if (old & ITASK_KNOWN_TO_LU) {
5049 			free_it = 0;
5050 			update_queue_flags = 1;
5051 			if (old & ITASK_IN_WORKER_QUEUE) {
5052 				queue_it = 0;
5053 			} else {
5054 				queue_it = 1;
5055 				new |= ITASK_IN_WORKER_QUEUE;
5056 			}
5057 		} else {
5058 			update_queue_flags = 0;
5059 			queue_it = 0;
5060 		}
5061 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5062 
5063 	if (update_queue_flags) {
5064 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
5065 
5066 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
5067 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
5068 		if (queue_it) {
5069 			itask->itask_worker_next = NULL;
5070 			if (w->worker_task_tail) {
5071 				w->worker_task_tail->itask_worker_next = itask;
5072 			} else {
5073 				w->worker_task_head = itask;
5074 			}
5075 			w->worker_task_tail = itask;
5076 			/* Measure task waitq time */
5077 			itask->itask_waitq_enter_timestamp = gethrtime();
5078 			if (++(w->worker_queue_depth) >
5079 			    w->worker_max_qdepth_pu) {
5080 				w->worker_max_qdepth_pu = w->worker_queue_depth;
5081 			}
5082 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5083 				cv_signal(&w->worker_cv);
5084 		}
5085 	}
5086 	mutex_exit(&w->worker_lock);
5087 
5088 	if (free_it) {
5089 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
5090 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
5091 		    ITASK_BEING_ABORTED)) == 0) {
5092 			stmf_task_free(task);
5093 		}
5094 	}
5095 }
5096 
5097 stmf_status_t
5098 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
5099 {
5100 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
5101 
5102 	stmf_i_scsi_task_t *itask =
5103 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5104 
5105 	stmf_task_audit(itask, TE_SEND_STATUS, ioflags, NULL);
5106 
5107 	if (ioflags & STMF_IOF_LU_DONE) {
5108 		uint32_t new, old;
5109 		do {
5110 			new = old = itask->itask_flags;
5111 			if (new & ITASK_BEING_ABORTED)
5112 				return (STMF_ABORTED);
5113 			new &= ~ITASK_KNOWN_TO_LU;
5114 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5115 	}
5116 
5117 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
5118 		return (STMF_SUCCESS);
5119 	}
5120 
5121 	if (itask->itask_flags & ITASK_BEING_ABORTED)
5122 		return (STMF_ABORTED);
5123 
5124 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5125 		task->task_status_ctrl = 0;
5126 		task->task_resid = 0;
5127 	} else if (task->task_cmd_xfer_length >
5128 	    task->task_expected_xfer_length) {
5129 		task->task_status_ctrl = TASK_SCTRL_OVER;
5130 		task->task_resid = task->task_cmd_xfer_length -
5131 		    task->task_expected_xfer_length;
5132 	} else if (task->task_nbytes_transferred <
5133 	    task->task_expected_xfer_length) {
5134 		task->task_status_ctrl = TASK_SCTRL_UNDER;
5135 		task->task_resid = task->task_expected_xfer_length -
5136 		    task->task_nbytes_transferred;
5137 	} else {
5138 		task->task_status_ctrl = 0;
5139 		task->task_resid = 0;
5140 	}
5141 	return (task->task_lport->lport_send_status(task, ioflags));
5142 }
5143 
5144 void
5145 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5146 {
5147 	stmf_i_scsi_task_t *itask =
5148 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5149 	stmf_worker_t *w = itask->itask_worker;
5150 	uint32_t new, old;
5151 	uint8_t free_it, queue_it;
5152 
5153 	stmf_task_audit(itask, TE_SEND_STATUS_DONE, iof, NULL);
5154 
5155 	mutex_enter(&w->worker_lock);
5156 	do {
5157 		new = old = itask->itask_flags;
5158 		if (old & ITASK_BEING_ABORTED) {
5159 			mutex_exit(&w->worker_lock);
5160 			return;
5161 		}
5162 		free_it = 0;
5163 		if (iof & STMF_IOF_LPORT_DONE) {
5164 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5165 			free_it = 1;
5166 		}
5167 		/*
5168 		 * If the task is known to LU then queue it. But if
5169 		 * it is already queued (multiple completions) then
5170 		 * just update the buffer information by grabbing the
5171 		 * worker lock. If the task is not known to LU,
5172 		 * completed/aborted, then see if we need to
5173 		 * free this task.
5174 		 */
5175 		if (old & ITASK_KNOWN_TO_LU) {
5176 			free_it = 0;
5177 			queue_it = 1;
5178 			if (old & ITASK_IN_WORKER_QUEUE) {
5179 				cmn_err(CE_PANIC, "status completion received"
5180 				    " when task is already in worker queue "
5181 				    " task = %p", (void *)task);
5182 			}
5183 			new |= ITASK_IN_WORKER_QUEUE;
5184 		} else {
5185 			queue_it = 0;
5186 		}
5187 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5188 	task->task_completion_status = s;
5189 
5190 
5191 	if (queue_it) {
5192 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
5193 		itask->itask_cmd_stack[itask->itask_ncmds++] =
5194 		    ITASK_CMD_STATUS_DONE;
5195 		itask->itask_worker_next = NULL;
5196 		if (w->worker_task_tail) {
5197 			w->worker_task_tail->itask_worker_next = itask;
5198 		} else {
5199 			w->worker_task_head = itask;
5200 		}
5201 		w->worker_task_tail = itask;
5202 		/* Measure task waitq time */
5203 		itask->itask_waitq_enter_timestamp = gethrtime();
5204 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5205 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5206 		}
5207 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5208 			cv_signal(&w->worker_cv);
5209 	}
5210 	mutex_exit(&w->worker_lock);
5211 
5212 	if (free_it) {
5213 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
5214 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
5215 		    ITASK_BEING_ABORTED)) == 0) {
5216 			stmf_task_free(task);
5217 		} else {
5218 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
5219 			    " is not done, itask %p itask_flags %x",
5220 			    (void *)itask, itask->itask_flags);
5221 		}
5222 	}
5223 }
5224 
5225 void
5226 stmf_task_lu_done(scsi_task_t *task)
5227 {
5228 	stmf_i_scsi_task_t *itask =
5229 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5230 	stmf_worker_t *w = itask->itask_worker;
5231 	uint32_t new, old;
5232 
5233 	mutex_enter(&w->worker_lock);
5234 	do {
5235 		new = old = itask->itask_flags;
5236 		if (old & ITASK_BEING_ABORTED) {
5237 			mutex_exit(&w->worker_lock);
5238 			return;
5239 		}
5240 		if (old & ITASK_IN_WORKER_QUEUE) {
5241 			cmn_err(CE_PANIC, "task_lu_done received"
5242 			    " when task is in worker queue "
5243 			    " task = %p", (void *)task);
5244 		}
5245 		new &= ~ITASK_KNOWN_TO_LU;
5246 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5247 
5248 	mutex_exit(&w->worker_lock);
5249 
5250 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
5251 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
5252 	    ITASK_BEING_ABORTED)) == 0) {
5253 		stmf_task_free(task);
5254 	} else {
5255 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
5256 		    " the task is still not done, task = %p", (void *)task);
5257 	}
5258 }
5259 
5260 void
5261 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
5262 {
5263 	stmf_i_scsi_task_t *itask =
5264 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5265 	stmf_worker_t *w;
5266 	uint32_t old, new;
5267 
5268 	stmf_task_audit(itask, TE_TASK_ABORT, CMD_OR_IOF_NA, NULL);
5269 
5270 	do {
5271 		old = new = itask->itask_flags;
5272 		if ((old & ITASK_BEING_ABORTED) ||
5273 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
5274 		    ITASK_KNOWN_TO_LU)) == 0)) {
5275 			return;
5276 		}
5277 		new |= ITASK_BEING_ABORTED;
5278 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5279 	task->task_completion_status = s;
5280 	itask->itask_start_time = ddi_get_lbolt();
5281 
5282 	if (((w = itask->itask_worker) == NULL) ||
5283 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
5284 		return;
5285 	}
5286 
5287 	/* Queue it and get out */
5288 	mutex_enter(&w->worker_lock);
5289 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
5290 		mutex_exit(&w->worker_lock);
5291 		return;
5292 	}
5293 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5294 	itask->itask_worker_next = NULL;
5295 	if (w->worker_task_tail) {
5296 		w->worker_task_tail->itask_worker_next = itask;
5297 	} else {
5298 		w->worker_task_head = itask;
5299 	}
5300 	w->worker_task_tail = itask;
5301 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5302 		w->worker_max_qdepth_pu = w->worker_queue_depth;
5303 	}
5304 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5305 		cv_signal(&w->worker_cv);
5306 	mutex_exit(&w->worker_lock);
5307 }
5308 
5309 void
5310 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
5311 {
5312 	stmf_i_scsi_task_t *itask = NULL;
5313 	uint32_t old, new, f, rf;
5314 
5315 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
5316 	    stmf_status_t, s);
5317 
5318 	switch (abort_cmd) {
5319 	case STMF_QUEUE_ABORT_LU:
5320 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
5321 		return;
5322 	case STMF_QUEUE_TASK_ABORT:
5323 		stmf_queue_task_for_abort(task, s);
5324 		return;
5325 	case STMF_REQUEUE_TASK_ABORT_LPORT:
5326 		rf = ITASK_TGT_PORT_ABORT_CALLED;
5327 		f = ITASK_KNOWN_TO_TGT_PORT;
5328 		break;
5329 	case STMF_REQUEUE_TASK_ABORT_LU:
5330 		rf = ITASK_LU_ABORT_CALLED;
5331 		f = ITASK_KNOWN_TO_LU;
5332 		break;
5333 	default:
5334 		return;
5335 	}
5336 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
5337 	f |= ITASK_BEING_ABORTED | rf;
5338 	do {
5339 		old = new = itask->itask_flags;
5340 		if ((old & f) != f) {
5341 			return;
5342 		}
5343 		new &= ~rf;
5344 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5345 }
5346 
5347 void
5348 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5349 {
5350 	char			 info[STMF_CHANGE_INFO_LEN];
5351 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5352 	unsigned long long	st;
5353 
5354 	stmf_task_audit(itask, TE_TASK_LU_ABORTED, iof, NULL);
5355 
5356 	st = s;	/* gcc fix */
5357 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5358 		(void) snprintf(info, sizeof (info),
5359 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
5360 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
5361 		(void) snprintf(info, sizeof (info),
5362 		    "Task aborted but LU is not finished, task ="
5363 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
5364 	} else {
5365 		/*
5366 		 * LU abort successfully
5367 		 */
5368 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
5369 		return;
5370 	}
5371 
5372 	stmf_abort_task_offline(task, 1, info);
5373 }
5374 
5375 void
5376 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5377 {
5378 	char			info[STMF_CHANGE_INFO_LEN];
5379 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5380 	unsigned long long	st;
5381 	uint32_t		old, new;
5382 
5383 	stmf_task_audit(itask, TE_TASK_LPORT_ABORTED, iof, NULL);
5384 
5385 	st = s;
5386 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5387 		(void) snprintf(info, sizeof (info),
5388 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
5389 		    st);
5390 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
5391 		(void) snprintf(info, sizeof (info),
5392 		    "Task aborted but tgt port is not finished, "
5393 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
5394 	} else {
5395 		/*
5396 		 * LPORT abort successfully
5397 		 */
5398 		do {
5399 			old = new = itask->itask_flags;
5400 			if (!(old & ITASK_KNOWN_TO_TGT_PORT))
5401 				return;
5402 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5403 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5404 		return;
5405 	}
5406 
5407 	stmf_abort_task_offline(task, 0, info);
5408 }
5409 
5410 stmf_status_t
5411 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
5412 {
5413 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5414 	    task->task_stmf_private;
5415 	stmf_worker_t *w = itask->itask_worker;
5416 	int i;
5417 
5418 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
5419 	mutex_enter(&w->worker_lock);
5420 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5421 		mutex_exit(&w->worker_lock);
5422 		return (STMF_BUSY);
5423 	}
5424 	for (i = 0; i < itask->itask_ncmds; i++) {
5425 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
5426 			mutex_exit(&w->worker_lock);
5427 			return (STMF_SUCCESS);
5428 		}
5429 	}
5430 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
5431 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5432 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5433 	} else {
5434 		clock_t t = drv_usectohz(timeout * 1000);
5435 		if (t == 0)
5436 			t = 1;
5437 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5438 	}
5439 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5440 		itask->itask_worker_next = NULL;
5441 		if (w->worker_task_tail) {
5442 			w->worker_task_tail->itask_worker_next = itask;
5443 		} else {
5444 			w->worker_task_head = itask;
5445 		}
5446 		w->worker_task_tail = itask;
5447 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5448 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5449 		}
5450 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5451 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5452 			cv_signal(&w->worker_cv);
5453 	}
5454 	mutex_exit(&w->worker_lock);
5455 	return (STMF_SUCCESS);
5456 }
5457 
5458 stmf_status_t
5459 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
5460 {
5461 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5462 	    task->task_stmf_private;
5463 	stmf_worker_t *w = itask->itask_worker;
5464 	int i;
5465 
5466 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
5467 	mutex_enter(&w->worker_lock);
5468 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5469 		mutex_exit(&w->worker_lock);
5470 		return (STMF_BUSY);
5471 	}
5472 	for (i = 0; i < itask->itask_ncmds; i++) {
5473 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
5474 			mutex_exit(&w->worker_lock);
5475 			return (STMF_SUCCESS);
5476 		}
5477 	}
5478 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
5479 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5480 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5481 	} else {
5482 		clock_t t = drv_usectohz(timeout * 1000);
5483 		if (t == 0)
5484 			t = 1;
5485 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5486 	}
5487 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5488 		itask->itask_worker_next = NULL;
5489 		if (w->worker_task_tail) {
5490 			w->worker_task_tail->itask_worker_next = itask;
5491 		} else {
5492 			w->worker_task_head = itask;
5493 		}
5494 		w->worker_task_tail = itask;
5495 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5496 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5497 		}
5498 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5499 			cv_signal(&w->worker_cv);
5500 	}
5501 	mutex_exit(&w->worker_lock);
5502 	return (STMF_SUCCESS);
5503 }
5504 
5505 void
5506 stmf_do_task_abort(scsi_task_t *task)
5507 {
5508 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5509 	stmf_lu_t		*lu;
5510 	stmf_local_port_t	*lport;
5511 	unsigned long long	 ret;
5512 	uint32_t		 old, new;
5513 	uint8_t			 call_lu_abort, call_port_abort;
5514 	char			 info[STMF_CHANGE_INFO_LEN];
5515 
5516 	lu = task->task_lu;
5517 	lport = task->task_lport;
5518 	do {
5519 		old = new = itask->itask_flags;
5520 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
5521 		    ITASK_KNOWN_TO_LU) {
5522 			new |= ITASK_LU_ABORT_CALLED;
5523 			call_lu_abort = 1;
5524 		} else {
5525 			call_lu_abort = 0;
5526 		}
5527 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5528 
5529 	if (call_lu_abort) {
5530 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
5531 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5532 		} else {
5533 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5534 		}
5535 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5536 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
5537 		} else if (ret == STMF_BUSY) {
5538 			atomic_and_32(&itask->itask_flags,
5539 			    ~ITASK_LU_ABORT_CALLED);
5540 		} else if (ret != STMF_SUCCESS) {
5541 			(void) snprintf(info, sizeof (info),
5542 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
5543 			stmf_abort_task_offline(task, 1, info);
5544 		}
5545 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
5546 		if (ddi_get_lbolt() > (itask->itask_start_time +
5547 		    STMF_SEC2TICK(lu->lu_abort_timeout?
5548 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
5549 			(void) snprintf(info, sizeof (info),
5550 			    "lu abort timed out");
5551 			stmf_abort_task_offline(itask->itask_task, 1, info);
5552 		}
5553 	}
5554 
5555 	do {
5556 		old = new = itask->itask_flags;
5557 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
5558 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
5559 			new |= ITASK_TGT_PORT_ABORT_CALLED;
5560 			call_port_abort = 1;
5561 		} else {
5562 			call_port_abort = 0;
5563 		}
5564 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5565 	if (call_port_abort) {
5566 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
5567 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5568 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
5569 		} else if (ret == STMF_BUSY) {
5570 			atomic_and_32(&itask->itask_flags,
5571 			    ~ITASK_TGT_PORT_ABORT_CALLED);
5572 		} else if (ret != STMF_SUCCESS) {
5573 			(void) snprintf(info, sizeof (info),
5574 			    "Abort failed by tgt port %p ret %llx",
5575 			    (void *)lport, ret);
5576 			stmf_abort_task_offline(task, 0, info);
5577 		}
5578 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
5579 		if (ddi_get_lbolt() > (itask->itask_start_time +
5580 		    STMF_SEC2TICK(lport->lport_abort_timeout?
5581 		    lport->lport_abort_timeout :
5582 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
5583 			(void) snprintf(info, sizeof (info),
5584 			    "lport abort timed out");
5585 			stmf_abort_task_offline(itask->itask_task, 0, info);
5586 		}
5587 	}
5588 }
5589 
5590 stmf_status_t
5591 stmf_ctl(int cmd, void *obj, void *arg)
5592 {
5593 	stmf_status_t			ret;
5594 	stmf_i_lu_t			*ilu;
5595 	stmf_i_local_port_t		*ilport;
5596 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
5597 
5598 	mutex_enter(&stmf_state.stmf_lock);
5599 	ret = STMF_INVALID_ARG;
5600 	if (cmd & STMF_CMD_LU_OP) {
5601 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
5602 		if (ilu == NULL) {
5603 			goto stmf_ctl_lock_exit;
5604 		}
5605 		DTRACE_PROBE3(lu__state__change,
5606 		    stmf_lu_t *, ilu->ilu_lu,
5607 		    int, cmd, stmf_state_change_info_t *, ssci);
5608 	} else if (cmd & STMF_CMD_LPORT_OP) {
5609 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
5610 		if (ilport == NULL) {
5611 			goto stmf_ctl_lock_exit;
5612 		}
5613 		DTRACE_PROBE3(lport__state__change,
5614 		    stmf_local_port_t *, ilport->ilport_lport,
5615 		    int, cmd, stmf_state_change_info_t *, ssci);
5616 	} else {
5617 		goto stmf_ctl_lock_exit;
5618 	}
5619 
5620 	switch (cmd) {
5621 	case STMF_CMD_LU_ONLINE:
5622 		switch (ilu->ilu_state) {
5623 			case STMF_STATE_OFFLINE:
5624 				ret = STMF_SUCCESS;
5625 				break;
5626 			case STMF_STATE_ONLINE:
5627 			case STMF_STATE_ONLINING:
5628 				ret = STMF_ALREADY;
5629 				break;
5630 			case STMF_STATE_OFFLINING:
5631 				ret = STMF_BUSY;
5632 				break;
5633 			default:
5634 				ret = STMF_BADSTATE;
5635 				break;
5636 		}
5637 		if (ret != STMF_SUCCESS)
5638 			goto stmf_ctl_lock_exit;
5639 
5640 		ilu->ilu_state = STMF_STATE_ONLINING;
5641 		mutex_exit(&stmf_state.stmf_lock);
5642 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5643 		break;
5644 
5645 	case STMF_CMD_LU_ONLINE_COMPLETE:
5646 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
5647 			ret = STMF_BADSTATE;
5648 			goto stmf_ctl_lock_exit;
5649 		}
5650 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5651 		    STMF_SUCCESS) {
5652 			ilu->ilu_state = STMF_STATE_ONLINE;
5653 			mutex_exit(&stmf_state.stmf_lock);
5654 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5655 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
5656 			mutex_enter(&stmf_state.stmf_lock);
5657 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5658 		} else {
5659 			/* XXX: should throw a meesage an record more data */
5660 			ilu->ilu_state = STMF_STATE_OFFLINE;
5661 		}
5662 		ret = STMF_SUCCESS;
5663 		goto stmf_ctl_lock_exit;
5664 
5665 	case STMF_CMD_LU_OFFLINE:
5666 		switch (ilu->ilu_state) {
5667 			case STMF_STATE_ONLINE:
5668 				ret = STMF_SUCCESS;
5669 				break;
5670 			case STMF_STATE_OFFLINE:
5671 			case STMF_STATE_OFFLINING:
5672 				ret = STMF_ALREADY;
5673 				break;
5674 			case STMF_STATE_ONLINING:
5675 				ret = STMF_BUSY;
5676 				break;
5677 			default:
5678 				ret = STMF_BADSTATE;
5679 				break;
5680 		}
5681 		if (ret != STMF_SUCCESS)
5682 			goto stmf_ctl_lock_exit;
5683 		ilu->ilu_state = STMF_STATE_OFFLINING;
5684 		mutex_exit(&stmf_state.stmf_lock);
5685 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5686 		break;
5687 
5688 	case STMF_CMD_LU_OFFLINE_COMPLETE:
5689 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
5690 			ret = STMF_BADSTATE;
5691 			goto stmf_ctl_lock_exit;
5692 		}
5693 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5694 		    STMF_SUCCESS) {
5695 			ilu->ilu_state = STMF_STATE_OFFLINE;
5696 			mutex_exit(&stmf_state.stmf_lock);
5697 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5698 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
5699 			mutex_enter(&stmf_state.stmf_lock);
5700 		} else {
5701 			ilu->ilu_state = STMF_STATE_ONLINE;
5702 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5703 		}
5704 		mutex_exit(&stmf_state.stmf_lock);
5705 		break;
5706 
5707 	/*
5708 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
5709 	 * It's related with hardware disable/enable.
5710 	 */
5711 	case STMF_CMD_LPORT_ONLINE:
5712 		switch (ilport->ilport_state) {
5713 			case STMF_STATE_OFFLINE:
5714 				ret = STMF_SUCCESS;
5715 				break;
5716 			case STMF_STATE_ONLINE:
5717 			case STMF_STATE_ONLINING:
5718 				ret = STMF_ALREADY;
5719 				break;
5720 			case STMF_STATE_OFFLINING:
5721 				ret = STMF_BUSY;
5722 				break;
5723 			default:
5724 				ret = STMF_BADSTATE;
5725 				break;
5726 		}
5727 		if (ret != STMF_SUCCESS)
5728 			goto stmf_ctl_lock_exit;
5729 
5730 		/*
5731 		 * Only user request can recover the port from the
5732 		 * FORCED_OFFLINE state
5733 		 */
5734 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
5735 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
5736 				ret = STMF_FAILURE;
5737 				goto stmf_ctl_lock_exit;
5738 			}
5739 		}
5740 
5741 		/*
5742 		 * Avoid too frequent request to online
5743 		 */
5744 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
5745 			ilport->ilport_online_times = 0;
5746 			ilport->ilport_avg_interval = 0;
5747 		}
5748 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
5749 		    (ilport->ilport_online_times >= 4)) {
5750 			ret = STMF_FAILURE;
5751 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
5752 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
5753 			    "online the port");
5754 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
5755 			    "online the port, set FORCED_OFFLINE now");
5756 			goto stmf_ctl_lock_exit;
5757 		}
5758 		if (ilport->ilport_online_times > 0) {
5759 			if (ilport->ilport_online_times == 1) {
5760 				ilport->ilport_avg_interval = ddi_get_lbolt() -
5761 				    ilport->ilport_last_online_clock;
5762 			} else {
5763 				ilport->ilport_avg_interval =
5764 				    (ilport->ilport_avg_interval +
5765 				    ddi_get_lbolt() -
5766 				    ilport->ilport_last_online_clock) >> 1;
5767 			}
5768 		}
5769 		ilport->ilport_last_online_clock = ddi_get_lbolt();
5770 		ilport->ilport_online_times++;
5771 
5772 		/*
5773 		 * Submit online service request
5774 		 */
5775 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
5776 		ilport->ilport_state = STMF_STATE_ONLINING;
5777 		mutex_exit(&stmf_state.stmf_lock);
5778 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5779 		break;
5780 
5781 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
5782 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
5783 			ret = STMF_BADSTATE;
5784 			goto stmf_ctl_lock_exit;
5785 		}
5786 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5787 		    STMF_SUCCESS) {
5788 			ilport->ilport_state = STMF_STATE_ONLINE;
5789 			mutex_exit(&stmf_state.stmf_lock);
5790 			((stmf_local_port_t *)obj)->lport_ctl(
5791 			    (stmf_local_port_t *)obj,
5792 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
5793 			mutex_enter(&stmf_state.stmf_lock);
5794 		} else {
5795 			ilport->ilport_state = STMF_STATE_OFFLINE;
5796 		}
5797 		ret = STMF_SUCCESS;
5798 		goto stmf_ctl_lock_exit;
5799 
5800 	case STMF_CMD_LPORT_OFFLINE:
5801 		switch (ilport->ilport_state) {
5802 			case STMF_STATE_ONLINE:
5803 				ret = STMF_SUCCESS;
5804 				break;
5805 			case STMF_STATE_OFFLINE:
5806 			case STMF_STATE_OFFLINING:
5807 				ret = STMF_ALREADY;
5808 				break;
5809 			case STMF_STATE_ONLINING:
5810 				ret = STMF_BUSY;
5811 				break;
5812 			default:
5813 				ret = STMF_BADSTATE;
5814 				break;
5815 		}
5816 		if (ret != STMF_SUCCESS)
5817 			goto stmf_ctl_lock_exit;
5818 
5819 		ilport->ilport_state = STMF_STATE_OFFLINING;
5820 		mutex_exit(&stmf_state.stmf_lock);
5821 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5822 		break;
5823 
5824 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
5825 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
5826 			ret = STMF_BADSTATE;
5827 			goto stmf_ctl_lock_exit;
5828 		}
5829 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5830 		    STMF_SUCCESS) {
5831 			ilport->ilport_state = STMF_STATE_OFFLINE;
5832 			mutex_exit(&stmf_state.stmf_lock);
5833 			((stmf_local_port_t *)obj)->lport_ctl(
5834 			    (stmf_local_port_t *)obj,
5835 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
5836 			mutex_enter(&stmf_state.stmf_lock);
5837 		} else {
5838 			ilport->ilport_state = STMF_STATE_ONLINE;
5839 		}
5840 		mutex_exit(&stmf_state.stmf_lock);
5841 		break;
5842 
5843 	default:
5844 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
5845 		ret = STMF_INVALID_ARG;
5846 		goto stmf_ctl_lock_exit;
5847 	}
5848 
5849 	return (STMF_SUCCESS);
5850 
5851 stmf_ctl_lock_exit:;
5852 	mutex_exit(&stmf_state.stmf_lock);
5853 	return (ret);
5854 }
5855 
5856 /* ARGSUSED */
5857 stmf_status_t
5858 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5859 						uint32_t *bufsizep)
5860 {
5861 	return (STMF_NOT_SUPPORTED);
5862 }
5863 
5864 /* ARGSUSED */
5865 stmf_status_t
5866 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5867 						uint32_t *bufsizep)
5868 {
5869 	uint32_t cl = SI_GET_CLASS(cmd);
5870 
5871 	if (cl == SI_STMF) {
5872 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
5873 	}
5874 	if (cl == SI_LPORT) {
5875 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
5876 		    arg2, buf, bufsizep));
5877 	} else if (cl == SI_LU) {
5878 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
5879 		    bufsizep));
5880 	}
5881 
5882 	return (STMF_NOT_SUPPORTED);
5883 }
5884 
5885 /*
5886  * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
5887  * stmf to register local ports. The ident should have 20 bytes in buffer
5888  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
5889  */
5890 void
5891 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
5892     uint8_t protocol_id)
5893 {
5894 	char wwn_str[20+1];
5895 
5896 	sdid->protocol_id = protocol_id;
5897 	sdid->piv = 1;
5898 	sdid->code_set = CODE_SET_ASCII;
5899 	sdid->association = ID_IS_TARGET_PORT;
5900 	sdid->ident_length = 20;
5901 	/* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
5902 	(void) snprintf(wwn_str, sizeof (wwn_str),
5903 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
5904 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
5905 	bcopy(wwn_str, (char *)sdid->ident, 20);
5906 }
5907 
5908 
5909 stmf_xfer_data_t *
5910 stmf_prepare_tpgs_data(uint8_t ilu_alua)
5911 {
5912 	stmf_xfer_data_t *xd;
5913 	stmf_i_local_port_t *ilport;
5914 	uint8_t *p;
5915 	uint32_t sz, asz, nports = 0, nports_standby = 0;
5916 
5917 	mutex_enter(&stmf_state.stmf_lock);
5918 	/* check if any ports are standby and create second group */
5919 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5920 	    ilport = ilport->ilport_next) {
5921 		if (ilport->ilport_standby == 1) {
5922 			nports_standby++;
5923 		} else {
5924 			nports++;
5925 		}
5926 	}
5927 
5928 	/* The spec only allows for 255 ports to be reported per group */
5929 	nports = min(nports, 255);
5930 	nports_standby = min(nports_standby, 255);
5931 	sz = (nports * 4) + 12;
5932 	if (nports_standby && ilu_alua) {
5933 		sz += (nports_standby * 4) + 8;
5934 	}
5935 	asz = sz + sizeof (*xd) - 4;
5936 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
5937 	if (xd == NULL) {
5938 		mutex_exit(&stmf_state.stmf_lock);
5939 		return (NULL);
5940 	}
5941 	xd->alloc_size = asz;
5942 	xd->size_left = sz;
5943 
5944 	p = xd->buf;
5945 
5946 	*((uint32_t *)p) = BE_32(sz - 4);
5947 	p += 4;
5948 	p[0] = 0x80;	/* PREF */
5949 	p[1] = 5;	/* AO_SUP, S_SUP */
5950 	if (stmf_state.stmf_alua_node == 1) {
5951 		p[3] = 1;	/* Group 1 */
5952 	} else {
5953 		p[3] = 0;	/* Group 0 */
5954 	}
5955 	p[7] = nports & 0xff;
5956 	p += 8;
5957 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5958 	    ilport = ilport->ilport_next) {
5959 		if (ilport->ilport_standby == 1) {
5960 			continue;
5961 		}
5962 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5963 		p += 4;
5964 	}
5965 	if (nports_standby && ilu_alua) {
5966 		p[0] = 0x02;	/* Non PREF, Standby */
5967 		p[1] = 5;	/* AO_SUP, S_SUP */
5968 		if (stmf_state.stmf_alua_node == 1) {
5969 			p[3] = 0;	/* Group 0 */
5970 		} else {
5971 			p[3] = 1;	/* Group 1 */
5972 		}
5973 		p[7] = nports_standby & 0xff;
5974 		p += 8;
5975 		for (ilport = stmf_state.stmf_ilportlist; ilport;
5976 		    ilport = ilport->ilport_next) {
5977 			if (ilport->ilport_standby == 0) {
5978 				continue;
5979 			}
5980 			((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5981 			p += 4;
5982 		}
5983 	}
5984 
5985 	mutex_exit(&stmf_state.stmf_lock);
5986 
5987 	return (xd);
5988 }
5989 
5990 struct scsi_devid_desc *
5991 stmf_scsilib_get_devid_desc(uint16_t rtpid)
5992 {
5993 	scsi_devid_desc_t *devid = NULL;
5994 	stmf_i_local_port_t *ilport;
5995 
5996 	mutex_enter(&stmf_state.stmf_lock);
5997 
5998 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5999 	    ilport = ilport->ilport_next) {
6000 		if (ilport->ilport_rtpid == rtpid) {
6001 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
6002 			uint32_t id_sz = sizeof (scsi_devid_desc_t) - 1 +
6003 			    id->ident_length;
6004 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
6005 			    KM_NOSLEEP);
6006 			if (devid != NULL) {
6007 				bcopy(id, devid, id_sz);
6008 			}
6009 			break;
6010 		}
6011 	}
6012 
6013 	mutex_exit(&stmf_state.stmf_lock);
6014 	return (devid);
6015 }
6016 
6017 uint16_t
6018 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
6019 {
6020 	stmf_i_local_port_t	*ilport;
6021 	scsi_devid_desc_t	*id;
6022 	uint16_t		rtpid = 0;
6023 
6024 	mutex_enter(&stmf_state.stmf_lock);
6025 	for (ilport = stmf_state.stmf_ilportlist; ilport;
6026 	    ilport = ilport->ilport_next) {
6027 		id = ilport->ilport_lport->lport_id;
6028 		if ((devid->ident_length == id->ident_length) &&
6029 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
6030 			rtpid = ilport->ilport_rtpid;
6031 			break;
6032 		}
6033 	}
6034 	mutex_exit(&stmf_state.stmf_lock);
6035 	return (rtpid);
6036 }
6037 
6038 static uint16_t stmf_lu_id_gen_number = 0;
6039 
6040 stmf_status_t
6041 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
6042 {
6043 	return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id));
6044 }
6045 
6046 stmf_status_t
6047 stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id,
6048     scsi_devid_desc_t *lu_id)
6049 {
6050 	uint8_t *p;
6051 	struct timeval32 timestamp32;
6052 	uint32_t *t = (uint32_t *)&timestamp32;
6053 	struct ether_addr mac;
6054 	uint8_t *e = (uint8_t *)&mac;
6055 	int hid = (int)host_id;
6056 
6057 	if (company_id == COMPANY_ID_NONE)
6058 		company_id = COMPANY_ID_SUN;
6059 
6060 	if (lu_id->ident_length != 0x10)
6061 		return (STMF_INVALID_ARG);
6062 
6063 	p = (uint8_t *)lu_id;
6064 
6065 	atomic_add_16(&stmf_lu_id_gen_number, 1);
6066 
6067 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
6068 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
6069 	p[5] = (company_id >> 12) & 0xff;
6070 	p[6] = (company_id >> 4) & 0xff;
6071 	p[7] = (company_id << 4) & 0xf0;
6072 	if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) {
6073 		hid = BE_32((int)zone_get_hostid(NULL));
6074 	}
6075 	if (hid != 0) {
6076 		e[0] = (hid >> 24) & 0xff;
6077 		e[1] = (hid >> 16) & 0xff;
6078 		e[2] = (hid >> 8) & 0xff;
6079 		e[3] = hid & 0xff;
6080 		e[4] = e[5] = 0;
6081 	}
6082 	bcopy(e, p+8, 6);
6083 	uniqtime32(&timestamp32);
6084 	*t = BE_32(*t);
6085 	bcopy(t, p+14, 4);
6086 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
6087 	p[19] = stmf_lu_id_gen_number & 0xff;
6088 
6089 	return (STMF_SUCCESS);
6090 }
6091 
6092 /*
6093  * saa is sense key, ASC, ASCQ
6094  */
6095 void
6096 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
6097 {
6098 	uint8_t sd[18];
6099 	task->task_scsi_status = st;
6100 	if (st == 2) {
6101 		bzero(sd, 18);
6102 		sd[0] = 0x70;
6103 		sd[2] = (saa >> 16) & 0xf;
6104 		sd[7] = 10;
6105 		sd[12] = (saa >> 8) & 0xff;
6106 		sd[13] = saa & 0xff;
6107 		task->task_sense_data = sd;
6108 		task->task_sense_length = 18;
6109 	} else {
6110 		task->task_sense_data = NULL;
6111 		task->task_sense_length = 0;
6112 	}
6113 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
6114 }
6115 
6116 uint32_t
6117 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
6118     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
6119 {
6120 	uint8_t		*p = NULL;
6121 	uint8_t		small_buf[32];
6122 	uint32_t	sz = 0;
6123 	uint32_t	n = 4;
6124 	uint32_t	m = 0;
6125 	uint32_t	last_bit = 0;
6126 
6127 	if (page_len < 4)
6128 		return (0);
6129 	if (page_len > 65535)
6130 		page_len = 65535;
6131 
6132 	page[0] = byte0;
6133 	page[1] = 0x83;
6134 
6135 	/* CONSTCOND */
6136 	while (1) {
6137 		m += sz;
6138 		if (sz && (page_len > n)) {
6139 			uint32_t copysz;
6140 			copysz = page_len > (n + sz) ? sz : page_len - n;
6141 			bcopy(p, page + n, copysz);
6142 			n += copysz;
6143 		}
6144 		vpd_mask &= ~last_bit;
6145 		if (vpd_mask == 0)
6146 			break;
6147 
6148 		if (vpd_mask & STMF_VPD_LU_ID) {
6149 			last_bit = STMF_VPD_LU_ID;
6150 			sz = task->task_lu->lu_id->ident_length + 4;
6151 			p = (uint8_t *)task->task_lu->lu_id;
6152 			continue;
6153 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
6154 			last_bit = STMF_VPD_TARGET_ID;
6155 			sz = task->task_lport->lport_id->ident_length + 4;
6156 			p = (uint8_t *)task->task_lport->lport_id;
6157 			continue;
6158 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
6159 			stmf_i_local_port_t *ilport;
6160 			last_bit = STMF_VPD_TP_GROUP;
6161 			p = small_buf;
6162 			bzero(p, 8);
6163 			p[0] = 1;
6164 			p[1] = 0x15;
6165 			p[3] = 4;
6166 			ilport = (stmf_i_local_port_t *)
6167 			    task->task_lport->lport_stmf_private;
6168 			/*
6169 			 * If we're in alua mode, group 1 contains all alua
6170 			 * participating ports and all standby ports
6171 			 * > 255. Otherwise, if we're in alua mode, any local
6172 			 * ports (non standby/pppt) are also in group 1 if the
6173 			 * alua node is 1. Otherwise the group is 0.
6174 			 */
6175 			if ((stmf_state.stmf_alua_state &&
6176 			    (ilport->ilport_alua || ilport->ilport_standby) &&
6177 			    ilport->ilport_rtpid > 255) ||
6178 			    (stmf_state.stmf_alua_node == 1 &&
6179 			    ilport->ilport_standby != 1)) {
6180 				p[7] = 1;	/* Group 1 */
6181 			}
6182 			sz = 8;
6183 			continue;
6184 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
6185 			stmf_i_local_port_t *ilport;
6186 
6187 			last_bit = STMF_VPD_RELATIVE_TP_ID;
6188 			p = small_buf;
6189 			bzero(p, 8);
6190 			p[0] = 1;
6191 			p[1] = 0x14;
6192 			p[3] = 4;
6193 			ilport = (stmf_i_local_port_t *)
6194 			    task->task_lport->lport_stmf_private;
6195 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
6196 			p[7] = ilport->ilport_rtpid & 0xff;
6197 			sz = 8;
6198 			continue;
6199 		} else {
6200 			cmn_err(CE_WARN, "Invalid vpd_mask");
6201 			break;
6202 		}
6203 	}
6204 
6205 	page[2] = (m >> 8) & 0xff;
6206 	page[3] = m & 0xff;
6207 
6208 	return (n);
6209 }
6210 
6211 void
6212 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
6213 {
6214 	stmf_i_scsi_task_t *itask =
6215 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
6216 	stmf_i_lu_t *ilu =
6217 	    (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6218 	stmf_xfer_data_t *xd;
6219 	uint32_t sz, minsz;
6220 
6221 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
6222 	task->task_cmd_xfer_length =
6223 	    ((((uint32_t)task->task_cdb[6]) << 24) |
6224 	    (((uint32_t)task->task_cdb[7]) << 16) |
6225 	    (((uint32_t)task->task_cdb[8]) << 8) |
6226 	    ((uint32_t)task->task_cdb[9]));
6227 
6228 	if (task->task_additional_flags &
6229 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6230 		task->task_expected_xfer_length =
6231 		    task->task_cmd_xfer_length;
6232 	}
6233 
6234 	if (task->task_cmd_xfer_length == 0) {
6235 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6236 		return;
6237 	}
6238 	if (task->task_cmd_xfer_length < 4) {
6239 		stmf_scsilib_send_status(task, STATUS_CHECK,
6240 		    STMF_SAA_INVALID_FIELD_IN_CDB);
6241 		return;
6242 	}
6243 
6244 	sz = min(task->task_expected_xfer_length,
6245 	    task->task_cmd_xfer_length);
6246 
6247 	xd = stmf_prepare_tpgs_data(ilu->ilu_alua);
6248 
6249 	if (xd == NULL) {
6250 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6251 		    STMF_ALLOC_FAILURE, NULL);
6252 		return;
6253 	}
6254 
6255 	sz = min(sz, xd->size_left);
6256 	xd->size_left = sz;
6257 	minsz = min(512, sz);
6258 
6259 	if (dbuf == NULL)
6260 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
6261 	if (dbuf == NULL) {
6262 		kmem_free(xd, xd->alloc_size);
6263 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6264 		    STMF_ALLOC_FAILURE, NULL);
6265 		return;
6266 	}
6267 	dbuf->db_lu_private = xd;
6268 	stmf_xd_to_dbuf(dbuf, 1);
6269 
6270 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6271 	(void) stmf_xfer_data(task, dbuf, 0);
6272 
6273 }
6274 
6275 void
6276 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
6277 {
6278 
6279 	switch (task->task_mgmt_function) {
6280 	/*
6281 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
6282 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
6283 	 * in these cases. This needs to be changed to abort only the required
6284 	 * set.
6285 	 */
6286 	case TM_ABORT_TASK:
6287 	case TM_ABORT_TASK_SET:
6288 	case TM_CLEAR_TASK_SET:
6289 	case TM_LUN_RESET:
6290 		stmf_handle_lun_reset(task);
6291 		/* issue the reset to the proxy node as well */
6292 		if (stmf_state.stmf_alua_state == 1) {
6293 			(void) stmf_proxy_scsi_cmd(task, NULL);
6294 		}
6295 		return;
6296 	case TM_TARGET_RESET:
6297 	case TM_TARGET_COLD_RESET:
6298 	case TM_TARGET_WARM_RESET:
6299 		stmf_handle_target_reset(task);
6300 		return;
6301 	default:
6302 		/* We dont support this task mgmt function */
6303 		stmf_scsilib_send_status(task, STATUS_CHECK,
6304 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
6305 		return;
6306 	}
6307 }
6308 
6309 void
6310 stmf_handle_lun_reset(scsi_task_t *task)
6311 {
6312 	stmf_i_scsi_task_t *itask;
6313 	stmf_i_lu_t *ilu;
6314 
6315 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6316 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6317 
6318 	/*
6319 	 * To sync with target reset, grab this lock. The LU is not going
6320 	 * anywhere as there is atleast one task pending (this task).
6321 	 */
6322 	mutex_enter(&stmf_state.stmf_lock);
6323 
6324 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6325 		mutex_exit(&stmf_state.stmf_lock);
6326 		stmf_scsilib_send_status(task, STATUS_CHECK,
6327 		    STMF_SAA_OPERATION_IN_PROGRESS);
6328 		return;
6329 	}
6330 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6331 	mutex_exit(&stmf_state.stmf_lock);
6332 
6333 	/*
6334 	 * Mark this task as the one causing LU reset so that we know who
6335 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
6336 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
6337 	 */
6338 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
6339 
6340 	/* Initiatiate abort on all commands on this LU except this one */
6341 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
6342 
6343 	/* Start polling on this task */
6344 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6345 	    != STMF_SUCCESS) {
6346 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6347 		    NULL);
6348 		return;
6349 	}
6350 }
6351 
6352 void
6353 stmf_handle_target_reset(scsi_task_t *task)
6354 {
6355 	stmf_i_scsi_task_t *itask;
6356 	stmf_i_lu_t *ilu;
6357 	stmf_i_scsi_session_t *iss;
6358 	stmf_lun_map_t *lm;
6359 	stmf_lun_map_ent_t *lm_ent;
6360 	int i, lf;
6361 
6362 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6363 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
6364 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6365 
6366 	/*
6367 	 * To sync with LUN reset, grab this lock. The session is not going
6368 	 * anywhere as there is atleast one task pending (this task).
6369 	 */
6370 	mutex_enter(&stmf_state.stmf_lock);
6371 
6372 	/* Grab the session lock as a writer to prevent any changes in it */
6373 	rw_enter(iss->iss_lockp, RW_WRITER);
6374 
6375 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
6376 		rw_exit(iss->iss_lockp);
6377 		mutex_exit(&stmf_state.stmf_lock);
6378 		stmf_scsilib_send_status(task, STATUS_CHECK,
6379 		    STMF_SAA_OPERATION_IN_PROGRESS);
6380 		return;
6381 	}
6382 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
6383 
6384 	/*
6385 	 * Now go through each LUN in this session and make sure all of them
6386 	 * can be reset.
6387 	 */
6388 	lm = iss->iss_sm;
6389 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
6390 		if (lm->lm_plus[i] == NULL)
6391 			continue;
6392 		lf++;
6393 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6394 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6395 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6396 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6397 			rw_exit(iss->iss_lockp);
6398 			mutex_exit(&stmf_state.stmf_lock);
6399 			stmf_scsilib_send_status(task, STATUS_CHECK,
6400 			    STMF_SAA_OPERATION_IN_PROGRESS);
6401 			return;
6402 		}
6403 	}
6404 	if (lf == 0) {
6405 		/* No luns in this session */
6406 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6407 		rw_exit(iss->iss_lockp);
6408 		mutex_exit(&stmf_state.stmf_lock);
6409 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6410 		return;
6411 	}
6412 
6413 	/* ok, start the damage */
6414 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
6415 	    ITASK_CAUSING_TARGET_RESET;
6416 	for (i = 0; i < lm->lm_nentries; i++) {
6417 		if (lm->lm_plus[i] == NULL)
6418 			continue;
6419 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6420 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6421 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6422 	}
6423 
6424 	for (i = 0; i < lm->lm_nentries; i++) {
6425 		if (lm->lm_plus[i] == NULL)
6426 			continue;
6427 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6428 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
6429 		    lm_ent->ent_lu);
6430 	}
6431 
6432 	rw_exit(iss->iss_lockp);
6433 	mutex_exit(&stmf_state.stmf_lock);
6434 
6435 	/* Start polling on this task */
6436 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6437 	    != STMF_SUCCESS) {
6438 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6439 		    NULL);
6440 		return;
6441 	}
6442 }
6443 
6444 int
6445 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
6446 {
6447 	scsi_task_t *task = itask->itask_task;
6448 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6449 	    task->task_session->ss_stmf_private;
6450 
6451 	rw_enter(iss->iss_lockp, RW_WRITER);
6452 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
6453 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
6454 		rw_exit(iss->iss_lockp);
6455 		return (0);
6456 	}
6457 	atomic_and_32(&iss->iss_flags,
6458 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6459 	rw_exit(iss->iss_lockp);
6460 
6461 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
6462 		return (0);
6463 	}
6464 	stmf_scsilib_send_status(task, STATUS_CHECK,
6465 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
6466 	return (1);
6467 }
6468 
6469 void
6470 stmf_worker_init()
6471 {
6472 	uint32_t i;
6473 
6474 	/* Make local copy of global tunables */
6475 	stmf_i_max_nworkers = stmf_max_nworkers;
6476 	stmf_i_min_nworkers = stmf_min_nworkers;
6477 
6478 	ASSERT(stmf_workers == NULL);
6479 	if (stmf_i_min_nworkers < 4) {
6480 		stmf_i_min_nworkers = 4;
6481 	}
6482 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
6483 		stmf_i_max_nworkers = stmf_i_min_nworkers;
6484 	}
6485 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
6486 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
6487 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6488 		stmf_worker_t *w = &stmf_workers[i];
6489 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
6490 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
6491 	}
6492 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6493 	stmf_workers_state = STMF_WORKERS_ENABLED;
6494 
6495 	/* Workers will be started by stmf_worker_mgmt() */
6496 
6497 	/* Lets wait for atleast one worker to start */
6498 	while (stmf_nworkers_cur == 0)
6499 		delay(drv_usectohz(20 * 1000));
6500 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
6501 }
6502 
6503 stmf_status_t
6504 stmf_worker_fini()
6505 {
6506 	int i;
6507 	clock_t sb;
6508 
6509 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
6510 		return (STMF_SUCCESS);
6511 	ASSERT(stmf_workers);
6512 	stmf_workers_state = STMF_WORKERS_DISABLED;
6513 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6514 	cv_signal(&stmf_state.stmf_cv);
6515 
6516 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
6517 	/* Wait for all the threads to die */
6518 	while (stmf_nworkers_cur != 0) {
6519 		if (ddi_get_lbolt() > sb) {
6520 			stmf_workers_state = STMF_WORKERS_ENABLED;
6521 			return (STMF_BUSY);
6522 		}
6523 		delay(drv_usectohz(100 * 1000));
6524 	}
6525 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6526 		stmf_worker_t *w = &stmf_workers[i];
6527 		mutex_destroy(&w->worker_lock);
6528 		cv_destroy(&w->worker_cv);
6529 	}
6530 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
6531 	stmf_workers = NULL;
6532 
6533 	return (STMF_SUCCESS);
6534 }
6535 
6536 void
6537 stmf_worker_task(void *arg)
6538 {
6539 	stmf_worker_t *w;
6540 	stmf_i_scsi_session_t *iss;
6541 	scsi_task_t *task;
6542 	stmf_i_scsi_task_t *itask;
6543 	stmf_data_buf_t *dbuf;
6544 	stmf_lu_t *lu;
6545 	clock_t wait_timer = 0;
6546 	clock_t wait_ticks, wait_delta = 0;
6547 	uint32_t old, new;
6548 	uint8_t curcmd;
6549 	uint8_t abort_free;
6550 	uint8_t wait_queue;
6551 	uint8_t dec_qdepth;
6552 
6553 	w = (stmf_worker_t *)arg;
6554 	wait_ticks = drv_usectohz(10000);
6555 
6556 	DTRACE_PROBE1(worker__create, stmf_worker_t, w);
6557 	mutex_enter(&w->worker_lock);
6558 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
6559 stmf_worker_loop:;
6560 	if ((w->worker_ref_count == 0) &&
6561 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
6562 		w->worker_flags &= ~(STMF_WORKER_STARTED |
6563 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
6564 		w->worker_tid = NULL;
6565 		mutex_exit(&w->worker_lock);
6566 		DTRACE_PROBE1(worker__destroy, stmf_worker_t, w);
6567 		thread_exit();
6568 	}
6569 	/* CONSTCOND */
6570 	while (1) {
6571 		dec_qdepth = 0;
6572 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
6573 			wait_timer = 0;
6574 			wait_delta = 0;
6575 			if (w->worker_wait_head) {
6576 				ASSERT(w->worker_wait_tail);
6577 				if (w->worker_task_head == NULL)
6578 					w->worker_task_head =
6579 					    w->worker_wait_head;
6580 				else
6581 					w->worker_task_tail->itask_worker_next =
6582 					    w->worker_wait_head;
6583 				w->worker_task_tail = w->worker_wait_tail;
6584 				w->worker_wait_head = w->worker_wait_tail =
6585 				    NULL;
6586 			}
6587 		}
6588 		if ((itask = w->worker_task_head) == NULL) {
6589 			break;
6590 		}
6591 		task = itask->itask_task;
6592 		DTRACE_PROBE2(worker__active, stmf_worker_t, w,
6593 		    scsi_task_t *, task);
6594 		w->worker_task_head = itask->itask_worker_next;
6595 		if (w->worker_task_head == NULL)
6596 			w->worker_task_tail = NULL;
6597 
6598 		wait_queue = 0;
6599 		abort_free = 0;
6600 		if (itask->itask_ncmds > 0) {
6601 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
6602 		} else {
6603 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
6604 		}
6605 		do {
6606 			old = itask->itask_flags;
6607 			if (old & ITASK_BEING_ABORTED) {
6608 				itask->itask_ncmds = 1;
6609 				curcmd = itask->itask_cmd_stack[0] =
6610 				    ITASK_CMD_ABORT;
6611 				goto out_itask_flag_loop;
6612 			} else if ((curcmd & ITASK_CMD_MASK) ==
6613 			    ITASK_CMD_NEW_TASK) {
6614 				/*
6615 				 * set ITASK_KSTAT_IN_RUNQ, this flag
6616 				 * will not reset until task completed
6617 				 */
6618 				new = old | ITASK_KNOWN_TO_LU |
6619 				    ITASK_KSTAT_IN_RUNQ;
6620 			} else {
6621 				goto out_itask_flag_loop;
6622 			}
6623 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
6624 
6625 out_itask_flag_loop:
6626 
6627 		/*
6628 		 * Decide if this task needs to go to a queue and/or if
6629 		 * we can decrement the itask_cmd_stack.
6630 		 */
6631 		if (curcmd == ITASK_CMD_ABORT) {
6632 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
6633 			    ITASK_KNOWN_TO_TGT_PORT)) {
6634 				wait_queue = 1;
6635 			} else {
6636 				abort_free = 1;
6637 			}
6638 		} else if ((curcmd & ITASK_CMD_POLL) &&
6639 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
6640 			wait_queue = 1;
6641 		}
6642 
6643 		if (wait_queue) {
6644 			itask->itask_worker_next = NULL;
6645 			if (w->worker_wait_tail) {
6646 				w->worker_wait_tail->itask_worker_next = itask;
6647 			} else {
6648 				w->worker_wait_head = itask;
6649 			}
6650 			w->worker_wait_tail = itask;
6651 			if (wait_timer == 0) {
6652 				wait_timer = ddi_get_lbolt() + wait_ticks;
6653 				wait_delta = wait_ticks;
6654 			}
6655 		} else if ((--(itask->itask_ncmds)) != 0) {
6656 			itask->itask_worker_next = NULL;
6657 			if (w->worker_task_tail) {
6658 				w->worker_task_tail->itask_worker_next = itask;
6659 			} else {
6660 				w->worker_task_head = itask;
6661 			}
6662 			w->worker_task_tail = itask;
6663 		} else {
6664 			atomic_and_32(&itask->itask_flags,
6665 			    ~ITASK_IN_WORKER_QUEUE);
6666 			/*
6667 			 * This is where the queue depth should go down by
6668 			 * one but we delay that on purpose to account for
6669 			 * the call into the provider. The actual decrement
6670 			 * happens after the worker has done its job.
6671 			 */
6672 			dec_qdepth = 1;
6673 			itask->itask_waitq_time +=
6674 			    gethrtime() - itask->itask_waitq_enter_timestamp;
6675 		}
6676 
6677 		/* We made it here means we are going to call LU */
6678 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
6679 			lu = task->task_lu;
6680 		else
6681 			lu = dlun0;
6682 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
6683 		mutex_exit(&w->worker_lock);
6684 		curcmd &= ITASK_CMD_MASK;
6685 		stmf_task_audit(itask, TE_PROCESS_CMD, curcmd, dbuf);
6686 		switch (curcmd) {
6687 		case ITASK_CMD_NEW_TASK:
6688 			iss = (stmf_i_scsi_session_t *)
6689 			    task->task_session->ss_stmf_private;
6690 			stmf_itl_lu_new_task(itask);
6691 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
6692 				if (stmf_handle_cmd_during_ic(itask))
6693 					break;
6694 			}
6695 #ifdef	DEBUG
6696 			if (stmf_drop_task_counter > 0) {
6697 				if (atomic_add_32_nv(
6698 				    (uint32_t *)&stmf_drop_task_counter,
6699 				    -1) == 1) {
6700 					break;
6701 				}
6702 			}
6703 #endif
6704 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
6705 			lu->lu_new_task(task, dbuf);
6706 			break;
6707 		case ITASK_CMD_DATA_XFER_DONE:
6708 			lu->lu_dbuf_xfer_done(task, dbuf);
6709 			break;
6710 		case ITASK_CMD_STATUS_DONE:
6711 			lu->lu_send_status_done(task);
6712 			break;
6713 		case ITASK_CMD_ABORT:
6714 			if (abort_free) {
6715 				stmf_task_free(task);
6716 			} else {
6717 				stmf_do_task_abort(task);
6718 			}
6719 			break;
6720 		case ITASK_CMD_POLL_LU:
6721 			if (!wait_queue) {
6722 				lu->lu_task_poll(task);
6723 			}
6724 			break;
6725 		case ITASK_CMD_POLL_LPORT:
6726 			if (!wait_queue)
6727 				task->task_lport->lport_task_poll(task);
6728 			break;
6729 		case ITASK_CMD_SEND_STATUS:
6730 		/* case ITASK_CMD_XFER_DATA: */
6731 			break;
6732 		}
6733 		mutex_enter(&w->worker_lock);
6734 		if (dec_qdepth) {
6735 			w->worker_queue_depth--;
6736 		}
6737 	}
6738 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
6739 		if (w->worker_ref_count == 0)
6740 			goto stmf_worker_loop;
6741 		else {
6742 			wait_timer = ddi_get_lbolt() + 1;
6743 			wait_delta = 1;
6744 		}
6745 	}
6746 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
6747 	if (wait_timer) {
6748 		DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w);
6749 		(void) cv_reltimedwait(&w->worker_cv, &w->worker_lock,
6750 		    wait_delta, TR_CLOCK_TICK);
6751 	} else {
6752 		DTRACE_PROBE1(worker__sleep, stmf_worker_t, w);
6753 		cv_wait(&w->worker_cv, &w->worker_lock);
6754 	}
6755 	DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w);
6756 	w->worker_flags |= STMF_WORKER_ACTIVE;
6757 	goto stmf_worker_loop;
6758 }
6759 
6760 void
6761 stmf_worker_mgmt()
6762 {
6763 	int i;
6764 	int workers_needed;
6765 	uint32_t qd;
6766 	clock_t tps, d = 0;
6767 	uint32_t cur_max_ntasks = 0;
6768 	stmf_worker_t *w;
6769 
6770 	/* Check if we are trying to increase the # of threads */
6771 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
6772 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
6773 			stmf_nworkers_cur++;
6774 			stmf_nworkers_accepting_cmds++;
6775 		} else {
6776 			/* Wait for transition to complete */
6777 			return;
6778 		}
6779 	}
6780 	/* Check if we are trying to decrease the # of workers */
6781 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6782 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
6783 			stmf_nworkers_cur--;
6784 			/*
6785 			 * stmf_nworkers_accepting_cmds has already been
6786 			 * updated by the request to reduce the # of workers.
6787 			 */
6788 		} else {
6789 			/* Wait for transition to complete */
6790 			return;
6791 		}
6792 	}
6793 	/* Check if we are being asked to quit */
6794 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
6795 		if (stmf_nworkers_cur) {
6796 			workers_needed = 0;
6797 			goto worker_mgmt_trigger_change;
6798 		}
6799 		return;
6800 	}
6801 	/* Check if we are starting */
6802 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
6803 		workers_needed = stmf_i_min_nworkers;
6804 		goto worker_mgmt_trigger_change;
6805 	}
6806 
6807 	tps = drv_usectohz(1 * 1000 * 1000);
6808 	if ((stmf_wm_last != 0) &&
6809 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
6810 		qd = 0;
6811 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
6812 			qd += stmf_workers[i].worker_max_qdepth_pu;
6813 			stmf_workers[i].worker_max_qdepth_pu = 0;
6814 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
6815 			    cur_max_ntasks) {
6816 				cur_max_ntasks =
6817 				    stmf_workers[i].worker_max_sys_qdepth_pu;
6818 			}
6819 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
6820 		}
6821 	}
6822 	stmf_wm_last = ddi_get_lbolt();
6823 	if (d <= tps) {
6824 		/* still ramping up */
6825 		return;
6826 	}
6827 	/* max qdepth cannot be more than max tasks */
6828 	if (qd > cur_max_ntasks)
6829 		qd = cur_max_ntasks;
6830 
6831 	/* See if we have more workers */
6832 	if (qd < stmf_nworkers_accepting_cmds) {
6833 		/*
6834 		 * Since we dont reduce the worker count right away, monitor
6835 		 * the highest load during the scale_down_delay.
6836 		 */
6837 		if (qd > stmf_worker_scale_down_qd)
6838 			stmf_worker_scale_down_qd = qd;
6839 		if (stmf_worker_scale_down_timer == 0) {
6840 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
6841 			    drv_usectohz(stmf_worker_scale_down_delay *
6842 			    1000 * 1000);
6843 			return;
6844 		}
6845 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
6846 			return;
6847 		}
6848 		/* Its time to reduce the workers */
6849 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
6850 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
6851 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
6852 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
6853 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
6854 			return;
6855 		workers_needed = stmf_worker_scale_down_qd;
6856 		stmf_worker_scale_down_qd = 0;
6857 		goto worker_mgmt_trigger_change;
6858 	}
6859 	stmf_worker_scale_down_qd = 0;
6860 	stmf_worker_scale_down_timer = 0;
6861 	if (qd > stmf_i_max_nworkers)
6862 		qd = stmf_i_max_nworkers;
6863 	if (qd < stmf_i_min_nworkers)
6864 		qd = stmf_i_min_nworkers;
6865 	if (qd == stmf_nworkers_cur)
6866 		return;
6867 	workers_needed = qd;
6868 	goto worker_mgmt_trigger_change;
6869 
6870 	/* NOTREACHED */
6871 	return;
6872 
6873 worker_mgmt_trigger_change:
6874 	ASSERT(workers_needed != stmf_nworkers_cur);
6875 	if (workers_needed > stmf_nworkers_cur) {
6876 		stmf_nworkers_needed = workers_needed;
6877 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
6878 			w = &stmf_workers[i];
6879 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
6880 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
6881 			    minclsyspri);
6882 		}
6883 		return;
6884 	}
6885 	/* At this point we know that we are decreasing the # of workers */
6886 	stmf_nworkers_accepting_cmds = workers_needed;
6887 	stmf_nworkers_needed = workers_needed;
6888 	/* Signal the workers that its time to quit */
6889 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6890 		w = &stmf_workers[i];
6891 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
6892 		mutex_enter(&w->worker_lock);
6893 		w->worker_flags |= STMF_WORKER_TERMINATE;
6894 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
6895 			cv_signal(&w->worker_cv);
6896 		mutex_exit(&w->worker_lock);
6897 	}
6898 }
6899 
6900 /*
6901  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
6902  * If all the data has been filled out, frees the xd and makes
6903  * db_lu_private NULL.
6904  */
6905 void
6906 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off)
6907 {
6908 	stmf_xfer_data_t *xd;
6909 	uint8_t *p;
6910 	int i;
6911 	uint32_t s;
6912 
6913 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6914 	dbuf->db_data_size = 0;
6915 	if (set_rel_off)
6916 		dbuf->db_relative_offset = xd->size_done;
6917 	for (i = 0; i < dbuf->db_sglist_length; i++) {
6918 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
6919 		p = &xd->buf[xd->size_done];
6920 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
6921 		xd->size_left -= s;
6922 		xd->size_done += s;
6923 		dbuf->db_data_size += s;
6924 		if (xd->size_left == 0) {
6925 			kmem_free(xd, xd->alloc_size);
6926 			dbuf->db_lu_private = NULL;
6927 			return;
6928 		}
6929 	}
6930 }
6931 
6932 /* ARGSUSED */
6933 stmf_status_t
6934 stmf_dlun0_task_alloc(scsi_task_t *task)
6935 {
6936 	return (STMF_SUCCESS);
6937 }
6938 
6939 void
6940 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
6941 {
6942 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
6943 	stmf_i_scsi_session_t *iss;
6944 	uint32_t sz, minsz;
6945 	uint8_t *p;
6946 	stmf_xfer_data_t *xd;
6947 	uint8_t inq_page_length = 31;
6948 
6949 	if (task->task_mgmt_function) {
6950 		stmf_scsilib_handle_task_mgmt(task);
6951 		return;
6952 	}
6953 
6954 	switch (cdbp[0]) {
6955 	case SCMD_INQUIRY:
6956 		/*
6957 		 * Basic protocol checks.  In addition, only reply to
6958 		 * standard inquiry.  Otherwise, the LU provider needs
6959 		 * to respond.
6960 		 */
6961 
6962 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
6963 			stmf_scsilib_send_status(task, STATUS_CHECK,
6964 			    STMF_SAA_INVALID_FIELD_IN_CDB);
6965 			return;
6966 		}
6967 
6968 		task->task_cmd_xfer_length =
6969 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
6970 
6971 		if (task->task_additional_flags &
6972 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6973 			task->task_expected_xfer_length =
6974 			    task->task_cmd_xfer_length;
6975 		}
6976 
6977 		sz = min(task->task_expected_xfer_length,
6978 		    min(36, task->task_cmd_xfer_length));
6979 		minsz = 36;
6980 
6981 		if (sz == 0) {
6982 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6983 			return;
6984 		}
6985 
6986 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
6987 			/*
6988 			 * Ignore any preallocated dbuf if the size is less
6989 			 * than 36. It will be freed during the task_free.
6990 			 */
6991 			dbuf = NULL;
6992 		}
6993 		if (dbuf == NULL)
6994 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
6995 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
6996 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6997 			    STMF_ALLOC_FAILURE, NULL);
6998 			return;
6999 		}
7000 		dbuf->db_lu_private = NULL;
7001 
7002 		p = dbuf->db_sglist[0].seg_addr;
7003 
7004 		/*
7005 		 * Standard inquiry handling only.
7006 		 */
7007 
7008 		bzero(p, inq_page_length + 5);
7009 
7010 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
7011 		p[2] = 5;
7012 		p[3] = 0x12;
7013 		p[4] = inq_page_length;
7014 		p[6] = 0x80;
7015 
7016 		(void) strncpy((char *)p+8, "SUN     ", 8);
7017 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
7018 		(void) strncpy((char *)p+32, "1.0 ", 4);
7019 
7020 		dbuf->db_data_size = sz;
7021 		dbuf->db_relative_offset = 0;
7022 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
7023 		(void) stmf_xfer_data(task, dbuf, 0);
7024 
7025 		return;
7026 
7027 	case SCMD_REPORT_LUNS:
7028 		task->task_cmd_xfer_length =
7029 		    ((((uint32_t)task->task_cdb[6]) << 24) |
7030 		    (((uint32_t)task->task_cdb[7]) << 16) |
7031 		    (((uint32_t)task->task_cdb[8]) << 8) |
7032 		    ((uint32_t)task->task_cdb[9]));
7033 
7034 		if (task->task_additional_flags &
7035 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
7036 			task->task_expected_xfer_length =
7037 			    task->task_cmd_xfer_length;
7038 		}
7039 
7040 		sz = min(task->task_expected_xfer_length,
7041 		    task->task_cmd_xfer_length);
7042 
7043 		if (sz < 16) {
7044 			stmf_scsilib_send_status(task, STATUS_CHECK,
7045 			    STMF_SAA_INVALID_FIELD_IN_CDB);
7046 			return;
7047 		}
7048 
7049 		iss = (stmf_i_scsi_session_t *)
7050 		    task->task_session->ss_stmf_private;
7051 		rw_enter(iss->iss_lockp, RW_WRITER);
7052 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
7053 		rw_exit(iss->iss_lockp);
7054 
7055 		if (xd == NULL) {
7056 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7057 			    STMF_ALLOC_FAILURE, NULL);
7058 			return;
7059 		}
7060 
7061 		sz = min(sz, xd->size_left);
7062 		xd->size_left = sz;
7063 		minsz = min(512, sz);
7064 
7065 		if (dbuf == NULL)
7066 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
7067 		if (dbuf == NULL) {
7068 			kmem_free(xd, xd->alloc_size);
7069 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7070 			    STMF_ALLOC_FAILURE, NULL);
7071 			return;
7072 		}
7073 		dbuf->db_lu_private = xd;
7074 		stmf_xd_to_dbuf(dbuf, 1);
7075 
7076 		atomic_and_32(&iss->iss_flags,
7077 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
7078 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
7079 		(void) stmf_xfer_data(task, dbuf, 0);
7080 		return;
7081 	}
7082 
7083 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
7084 }
7085 
7086 void
7087 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
7088 {
7089 	stmf_i_scsi_task_t *itask =
7090 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
7091 
7092 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
7093 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7094 		    dbuf->db_xfer_status, NULL);
7095 		return;
7096 	}
7097 	task->task_nbytes_transferred += dbuf->db_data_size;
7098 	if (dbuf->db_lu_private) {
7099 		/* There is more */
7100 		stmf_xd_to_dbuf(dbuf, 1);
7101 		(void) stmf_xfer_data(task, dbuf, 0);
7102 		return;
7103 	}
7104 
7105 	stmf_free_dbuf(task, dbuf);
7106 	/*
7107 	 * If this is a proxy task, it will need to be completed from the
7108 	 * proxy port provider. This message lets pppt know that the xfer
7109 	 * is complete. When we receive the status from pppt, we will
7110 	 * then relay that status back to the lport.
7111 	 */
7112 	if (itask->itask_flags & ITASK_PROXY_TASK) {
7113 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
7114 		stmf_status_t ic_ret = STMF_FAILURE;
7115 		uint64_t session_msg_id;
7116 		mutex_enter(&stmf_state.stmf_lock);
7117 		session_msg_id = stmf_proxy_msg_id++;
7118 		mutex_exit(&stmf_state.stmf_lock);
7119 		/* send xfer done status to pppt */
7120 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
7121 		    itask->itask_proxy_msg_id,
7122 		    task->task_session->ss_session_id,
7123 		    STMF_SUCCESS, session_msg_id);
7124 		if (ic_xfer_done_msg) {
7125 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
7126 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
7127 				cmn_err(CE_WARN, "unable to xmit session msg");
7128 			}
7129 		}
7130 		/* task will be completed from pppt */
7131 		return;
7132 	}
7133 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7134 }
7135 
7136 /* ARGSUSED */
7137 void
7138 stmf_dlun0_status_done(scsi_task_t *task)
7139 {
7140 }
7141 
7142 /* ARGSUSED */
7143 void
7144 stmf_dlun0_task_free(scsi_task_t *task)
7145 {
7146 }
7147 
7148 /* ARGSUSED */
7149 stmf_status_t
7150 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
7151 {
7152 	scsi_task_t *task = (scsi_task_t *)arg;
7153 	stmf_i_scsi_task_t *itask =
7154 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
7155 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7156 	int i;
7157 	uint8_t map;
7158 
7159 	if ((task->task_mgmt_function) && (itask->itask_flags &
7160 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
7161 		switch (task->task_mgmt_function) {
7162 		case TM_ABORT_TASK:
7163 		case TM_ABORT_TASK_SET:
7164 		case TM_CLEAR_TASK_SET:
7165 		case TM_LUN_RESET:
7166 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7167 			break;
7168 		case TM_TARGET_RESET:
7169 		case TM_TARGET_COLD_RESET:
7170 		case TM_TARGET_WARM_RESET:
7171 			stmf_abort_target_reset(task);
7172 			break;
7173 		}
7174 		return (STMF_ABORT_SUCCESS);
7175 	}
7176 
7177 	/*
7178 	 * OK so its not a task mgmt. Make sure we free any xd sitting
7179 	 * inside any dbuf.
7180 	 */
7181 	if ((map = itask->itask_allocated_buf_map) != 0) {
7182 		for (i = 0; i < 4; i++) {
7183 			if ((map & 1) &&
7184 			    ((itask->itask_dbufs[i])->db_lu_private)) {
7185 				stmf_xfer_data_t *xd;
7186 				stmf_data_buf_t *dbuf;
7187 
7188 				dbuf = itask->itask_dbufs[i];
7189 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
7190 				dbuf->db_lu_private = NULL;
7191 				kmem_free(xd, xd->alloc_size);
7192 			}
7193 			map >>= 1;
7194 		}
7195 	}
7196 	return (STMF_ABORT_SUCCESS);
7197 }
7198 
7199 void
7200 stmf_dlun0_task_poll(struct scsi_task *task)
7201 {
7202 	/* Right now we only do this for handling task management functions */
7203 	ASSERT(task->task_mgmt_function);
7204 
7205 	switch (task->task_mgmt_function) {
7206 	case TM_ABORT_TASK:
7207 	case TM_ABORT_TASK_SET:
7208 	case TM_CLEAR_TASK_SET:
7209 	case TM_LUN_RESET:
7210 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
7211 		return;
7212 	case TM_TARGET_RESET:
7213 	case TM_TARGET_COLD_RESET:
7214 	case TM_TARGET_WARM_RESET:
7215 		stmf_target_reset_poll(task);
7216 		return;
7217 	}
7218 }
7219 
7220 /* ARGSUSED */
7221 void
7222 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
7223 {
7224 	/* This function will never be called */
7225 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
7226 }
7227 
7228 void
7229 stmf_dlun_init()
7230 {
7231 	stmf_i_lu_t *ilu;
7232 
7233 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
7234 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
7235 	dlun0->lu_new_task = stmf_dlun0_new_task;
7236 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
7237 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
7238 	dlun0->lu_task_free = stmf_dlun0_task_free;
7239 	dlun0->lu_abort = stmf_dlun0_abort;
7240 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
7241 	dlun0->lu_ctl = stmf_dlun0_ctl;
7242 
7243 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
7244 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
7245 }
7246 
7247 stmf_status_t
7248 stmf_dlun_fini()
7249 {
7250 	stmf_i_lu_t *ilu;
7251 
7252 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
7253 
7254 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
7255 	if (ilu->ilu_ntasks) {
7256 		stmf_i_scsi_task_t *itask, *nitask;
7257 
7258 		nitask = ilu->ilu_tasks;
7259 		do {
7260 			itask = nitask;
7261 			nitask = itask->itask_lu_next;
7262 			dlun0->lu_task_free(itask->itask_task);
7263 			stmf_free(itask->itask_task);
7264 		} while (nitask != NULL);
7265 
7266 	}
7267 	stmf_free(dlun0);
7268 	return (STMF_SUCCESS);
7269 }
7270 
7271 void
7272 stmf_abort_target_reset(scsi_task_t *task)
7273 {
7274 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7275 	    task->task_session->ss_stmf_private;
7276 	stmf_lun_map_t *lm;
7277 	stmf_lun_map_ent_t *lm_ent;
7278 	stmf_i_lu_t *ilu;
7279 	int i;
7280 
7281 	rw_enter(iss->iss_lockp, RW_READER);
7282 	lm = iss->iss_sm;
7283 	for (i = 0; i < lm->lm_nentries; i++) {
7284 		if (lm->lm_plus[i] == NULL)
7285 			continue;
7286 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7287 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7288 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7289 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7290 		}
7291 	}
7292 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7293 	rw_exit(iss->iss_lockp);
7294 }
7295 
7296 /*
7297  * The return value is only used by function managing target reset.
7298  */
7299 stmf_status_t
7300 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
7301 {
7302 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7303 	int ntasks_pending;
7304 
7305 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
7306 	/*
7307 	 * This function is also used during Target reset. The idea is that
7308 	 * once all the commands are aborted, call the LU's reset entry
7309 	 * point (abort entry point with a reset flag). But if this Task
7310 	 * mgmt is running on this LU then all the tasks cannot be aborted.
7311 	 * one task (this task) will still be running which is OK.
7312 	 */
7313 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
7314 	    (ntasks_pending == 1))) {
7315 		stmf_status_t ret;
7316 
7317 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
7318 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
7319 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
7320 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
7321 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
7322 		} else {
7323 			ret = STMF_SUCCESS;
7324 		}
7325 		if (ret == STMF_SUCCESS) {
7326 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7327 		}
7328 		if (target_reset) {
7329 			return (ret);
7330 		}
7331 		if (ret == STMF_SUCCESS) {
7332 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7333 			return (ret);
7334 		}
7335 		if (ret != STMF_BUSY) {
7336 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
7337 			return (ret);
7338 		}
7339 	}
7340 
7341 	if (target_reset) {
7342 		/* Tell target reset polling code that we are not done */
7343 		return (STMF_BUSY);
7344 	}
7345 
7346 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7347 	    != STMF_SUCCESS) {
7348 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7349 		    STMF_ALLOC_FAILURE, NULL);
7350 		return (STMF_SUCCESS);
7351 	}
7352 
7353 	return (STMF_SUCCESS);
7354 }
7355 
7356 void
7357 stmf_target_reset_poll(struct scsi_task *task)
7358 {
7359 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7360 	    task->task_session->ss_stmf_private;
7361 	stmf_lun_map_t *lm;
7362 	stmf_lun_map_ent_t *lm_ent;
7363 	stmf_i_lu_t *ilu;
7364 	stmf_status_t ret;
7365 	int i;
7366 	int not_done = 0;
7367 
7368 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
7369 
7370 	rw_enter(iss->iss_lockp, RW_READER);
7371 	lm = iss->iss_sm;
7372 	for (i = 0; i < lm->lm_nentries; i++) {
7373 		if (lm->lm_plus[i] == NULL)
7374 			continue;
7375 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7376 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7377 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7378 			rw_exit(iss->iss_lockp);
7379 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
7380 			rw_enter(iss->iss_lockp, RW_READER);
7381 			if (ret == STMF_SUCCESS)
7382 				continue;
7383 			not_done = 1;
7384 			if (ret != STMF_BUSY) {
7385 				rw_exit(iss->iss_lockp);
7386 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7387 				    STMF_ABORTED, NULL);
7388 				return;
7389 			}
7390 		}
7391 	}
7392 	rw_exit(iss->iss_lockp);
7393 
7394 	if (not_done) {
7395 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7396 		    != STMF_SUCCESS) {
7397 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7398 			    STMF_ALLOC_FAILURE, NULL);
7399 			return;
7400 		}
7401 		return;
7402 	}
7403 
7404 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7405 
7406 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7407 }
7408 
7409 stmf_status_t
7410 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
7411 {
7412 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7413 
7414 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7415 		return (STMF_INVALID_ARG);
7416 	}
7417 
7418 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
7419 	return (STMF_SUCCESS);
7420 }
7421 
7422 stmf_status_t
7423 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
7424 {
7425 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7426 
7427 	if (eventid == STMF_EVENT_ALL) {
7428 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
7429 		return (STMF_SUCCESS);
7430 	}
7431 
7432 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7433 		return (STMF_INVALID_ARG);
7434 	}
7435 
7436 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
7437 	return (STMF_SUCCESS);
7438 }
7439 
7440 stmf_status_t
7441 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
7442 {
7443 	stmf_i_local_port_t *ilport =
7444 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7445 
7446 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7447 		return (STMF_INVALID_ARG);
7448 	}
7449 
7450 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
7451 	return (STMF_SUCCESS);
7452 }
7453 
7454 stmf_status_t
7455 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
7456 {
7457 	stmf_i_local_port_t *ilport =
7458 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7459 
7460 	if (eventid == STMF_EVENT_ALL) {
7461 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
7462 		return (STMF_SUCCESS);
7463 	}
7464 
7465 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7466 		return (STMF_INVALID_ARG);
7467 	}
7468 
7469 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
7470 	return (STMF_SUCCESS);
7471 }
7472 
7473 void
7474 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
7475 {
7476 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
7477 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
7478 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
7479 	}
7480 }
7481 
7482 void
7483 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
7484 				uint32_t flags)
7485 {
7486 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
7487 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
7488 		ilport->ilport_lport->lport_event_handler(
7489 		    ilport->ilport_lport, eventid, arg, flags);
7490 	}
7491 }
7492 
7493 /*
7494  * With the possibility of having multiple itl sessions pointing to the
7495  * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize
7496  * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer
7497  * statistics.
7498  */
7499 void
7500 stmf_itl_task_start(stmf_i_scsi_task_t *itask)
7501 {
7502 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7503 	scsi_task_t	*task = itask->itask_task;
7504 	stmf_i_lu_t	*ilu;
7505 
7506 	if (itl == NULL || task->task_lu == dlun0)
7507 		return;
7508 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7509 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7510 	itask->itask_start_timestamp = gethrtime();
7511 	kstat_waitq_enter(KSTAT_IO_PTR(itl->itl_kstat_taskq));
7512 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter);
7513 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7514 
7515 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter);
7516 }
7517 
7518 void
7519 stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask)
7520 {
7521 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7522 	scsi_task_t	*task = itask->itask_task;
7523 	stmf_i_lu_t	*ilu;
7524 
7525 	if (itl == NULL || task->task_lu == dlun0)
7526 		return;
7527 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7528 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7529 	kstat_waitq_to_runq(KSTAT_IO_PTR(itl->itl_kstat_taskq));
7530 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq);
7531 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7532 
7533 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq);
7534 }
7535 
7536 void
7537 stmf_itl_task_done(stmf_i_scsi_task_t *itask)
7538 {
7539 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7540 	scsi_task_t		*task = itask->itask_task;
7541 	kstat_io_t		*kip;
7542 	hrtime_t		elapsed_time;
7543 	stmf_kstat_itl_info_t	*itli;
7544 	stmf_i_lu_t	*ilu;
7545 
7546 	if (itl == NULL || task->task_lu == dlun0)
7547 		return;
7548 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7549 
7550 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7551 	itli = (stmf_kstat_itl_info_t *)KSTAT_NAMED_PTR(itl->itl_kstat_info);
7552 	kip = KSTAT_IO_PTR(itl->itl_kstat_taskq);
7553 
7554 	itli->i_task_waitq_elapsed.value.ui64 += itask->itask_waitq_time;
7555 
7556 	itask->itask_done_timestamp = gethrtime();
7557 	elapsed_time =
7558 	    itask->itask_done_timestamp - itask->itask_start_timestamp;
7559 
7560 	if (task->task_flags & TF_READ_DATA) {
7561 		kip->reads++;
7562 		kip->nread += itask->itask_read_xfer;
7563 		itli->i_task_read_elapsed.value.ui64 += elapsed_time;
7564 		itli->i_lu_read_elapsed.value.ui64 +=
7565 		    itask->itask_lu_read_time;
7566 		itli->i_lport_read_elapsed.value.ui64 +=
7567 		    itask->itask_lport_read_time;
7568 	}
7569 
7570 	if (task->task_flags & TF_WRITE_DATA) {
7571 		kip->writes++;
7572 		kip->nwritten += itask->itask_write_xfer;
7573 		itli->i_task_write_elapsed.value.ui64 += elapsed_time;
7574 		itli->i_lu_write_elapsed.value.ui64 +=
7575 		    itask->itask_lu_write_time;
7576 		itli->i_lport_write_elapsed.value.ui64 +=
7577 		    itask->itask_lport_write_time;
7578 	}
7579 
7580 	if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) {
7581 		kstat_runq_exit(kip);
7582 		stmf_update_kstat_lu_q(task, kstat_runq_exit);
7583 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7584 		stmf_update_kstat_lport_q(task, kstat_runq_exit);
7585 	} else {
7586 		kstat_waitq_exit(kip);
7587 		stmf_update_kstat_lu_q(task, kstat_waitq_exit);
7588 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7589 		stmf_update_kstat_lport_q(task, kstat_waitq_exit);
7590 	}
7591 }
7592 
7593 void
7594 stmf_lu_xfer_start(scsi_task_t *task)
7595 {
7596 	stmf_i_scsi_task_t *itask = task->task_stmf_private;
7597 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7598 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7599 	kstat_io_t		*kip;
7600 
7601 	if (itl == NULL || task->task_lu == dlun0)
7602 		return;
7603 
7604 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
7605 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7606 	kstat_runq_enter(kip);
7607 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7608 }
7609 
7610 void
7611 stmf_lu_xfer_done(scsi_task_t *task, boolean_t read, uint64_t xfer_bytes,
7612     hrtime_t elapsed_time)
7613 {
7614 	stmf_i_scsi_task_t	*itask = task->task_stmf_private;
7615 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7616 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7617 	kstat_io_t		*kip;
7618 
7619 	if (itl == NULL || task->task_lu == dlun0)
7620 		return;
7621 
7622 	if (read) {
7623 		atomic_add_64((uint64_t *)&itask->itask_lu_read_time,
7624 		    elapsed_time);
7625 	} else {
7626 		atomic_add_64((uint64_t *)&itask->itask_lu_write_time,
7627 		    elapsed_time);
7628 	}
7629 
7630 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
7631 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7632 	kstat_runq_exit(kip);
7633 	if (read) {
7634 		kip->reads++;
7635 		kip->nread += xfer_bytes;
7636 	} else {
7637 		kip->writes++;
7638 		kip->nwritten += xfer_bytes;
7639 	}
7640 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7641 }
7642 
7643 static void
7644 stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7645 {
7646 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7647 
7648 	if (itl == NULL)
7649 		return;
7650 
7651 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task,
7652 	    stmf_data_buf_t *, dbuf);
7653 
7654 	dbuf->db_xfer_start_timestamp = gethrtime();
7655 }
7656 
7657 static void
7658 stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7659 {
7660 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7661 	scsi_task_t		*task;
7662 	stmf_i_local_port_t	*ilp;
7663 	kstat_io_t		*kip;
7664 	hrtime_t		elapsed_time;
7665 	uint64_t		xfer_size;
7666 
7667 	if (itl == NULL)
7668 		return;
7669 
7670 	task = (scsi_task_t *)itask->itask_task;
7671 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
7672 	xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ?
7673 	    dbuf->db_data_size : 0;
7674 
7675 	elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp;
7676 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7677 		atomic_add_64((uint64_t *)&itask->itask_lport_read_time,
7678 		    elapsed_time);
7679 		atomic_add_64((uint64_t *)&itask->itask_read_xfer,
7680 		    xfer_size);
7681 	} else {
7682 		atomic_add_64((uint64_t *)&itask->itask_lport_write_time,
7683 		    elapsed_time);
7684 		atomic_add_64((uint64_t *)&itask->itask_write_xfer,
7685 		    xfer_size);
7686 	}
7687 
7688 	DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task,
7689 	    stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time);
7690 
7691 	kip = KSTAT_IO_PTR(itl->itl_kstat_lport_xfer);
7692 	mutex_enter(ilp->ilport_kstat_io->ks_lock);
7693 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7694 		kip->reads++;
7695 		kip->nread += xfer_size;
7696 	} else {
7697 		kip->writes++;
7698 		kip->nwritten += xfer_size;
7699 	}
7700 	mutex_exit(ilp->ilport_kstat_io->ks_lock);
7701 
7702 	dbuf->db_xfer_start_timestamp = 0;
7703 }
7704 
7705 void
7706 stmf_svc_init()
7707 {
7708 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7709 		return;
7710 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
7711 	    TASKQ_DEFAULTPRI, 0);
7712 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
7713 	    stmf_svc, 0, DDI_SLEEP);
7714 }
7715 
7716 stmf_status_t
7717 stmf_svc_fini()
7718 {
7719 	uint32_t i;
7720 
7721 	mutex_enter(&stmf_state.stmf_lock);
7722 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
7723 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
7724 		cv_signal(&stmf_state.stmf_cv);
7725 	}
7726 	mutex_exit(&stmf_state.stmf_lock);
7727 
7728 	/* Wait for 5 seconds */
7729 	for (i = 0; i < 500; i++) {
7730 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7731 			delay(drv_usectohz(10000));
7732 		else
7733 			break;
7734 	}
7735 	if (i == 500)
7736 		return (STMF_BUSY);
7737 
7738 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
7739 
7740 	return (STMF_SUCCESS);
7741 }
7742 
7743 /* ARGSUSED */
7744 void
7745 stmf_svc(void *arg)
7746 {
7747 	stmf_svc_req_t *req, **preq;
7748 	clock_t td;
7749 	clock_t	drain_start, drain_next = 0;
7750 	clock_t	timing_start, timing_next = 0;
7751 	clock_t worker_delay = 0;
7752 	int deq;
7753 	stmf_lu_t *lu;
7754 	stmf_i_lu_t *ilu;
7755 	stmf_local_port_t *lport;
7756 	stmf_i_local_port_t *ilport, *next_ilport;
7757 	stmf_i_scsi_session_t *iss;
7758 
7759 	td = drv_usectohz(20000);
7760 
7761 	mutex_enter(&stmf_state.stmf_lock);
7762 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
7763 
7764 stmf_svc_loop:
7765 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
7766 		stmf_state.stmf_svc_flags &=
7767 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
7768 		mutex_exit(&stmf_state.stmf_lock);
7769 		return;
7770 	}
7771 
7772 	if (stmf_state.stmf_svc_active) {
7773 		int waitq_add = 0;
7774 		req = stmf_state.stmf_svc_active;
7775 		stmf_state.stmf_svc_active = req->svc_next;
7776 
7777 		switch (req->svc_cmd) {
7778 		case STMF_CMD_LPORT_ONLINE:
7779 			/* Fallthrough */
7780 		case STMF_CMD_LPORT_OFFLINE:
7781 			/* Fallthrough */
7782 		case STMF_CMD_LU_ONLINE:
7783 			/* Nothing to do */
7784 			waitq_add = 1;
7785 			break;
7786 
7787 		case STMF_CMD_LU_OFFLINE:
7788 			/* Remove all mappings of this LU */
7789 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
7790 			/* Kill all the pending I/Os for this LU */
7791 			mutex_exit(&stmf_state.stmf_lock);
7792 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
7793 			    STMF_ABORTED);
7794 			mutex_enter(&stmf_state.stmf_lock);
7795 			waitq_add = 1;
7796 			break;
7797 		default:
7798 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
7799 			    req->svc_cmd);
7800 		}
7801 
7802 		if (waitq_add) {
7803 			/* Put it in the wait queue */
7804 			req->svc_next = stmf_state.stmf_svc_waiting;
7805 			stmf_state.stmf_svc_waiting = req;
7806 		}
7807 	}
7808 
7809 	/* The waiting list is not going to be modified by anybody else */
7810 	mutex_exit(&stmf_state.stmf_lock);
7811 
7812 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
7813 		req = *preq;
7814 		deq = 0;
7815 
7816 		switch (req->svc_cmd) {
7817 		case STMF_CMD_LU_ONLINE:
7818 			lu = (stmf_lu_t *)req->svc_obj;
7819 			deq = 1;
7820 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7821 			break;
7822 
7823 		case STMF_CMD_LU_OFFLINE:
7824 			lu = (stmf_lu_t *)req->svc_obj;
7825 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7826 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
7827 				break;
7828 			deq = 1;
7829 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7830 			break;
7831 
7832 		case STMF_CMD_LPORT_OFFLINE:
7833 			/* Fallthrough */
7834 		case STMF_CMD_LPORT_ONLINE:
7835 			lport = (stmf_local_port_t *)req->svc_obj;
7836 			deq = 1;
7837 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
7838 			break;
7839 		}
7840 		if (deq) {
7841 			*preq = req->svc_next;
7842 			kmem_free(req, req->svc_req_alloc_size);
7843 		} else {
7844 			preq = &req->svc_next;
7845 		}
7846 	}
7847 
7848 	mutex_enter(&stmf_state.stmf_lock);
7849 	if (stmf_state.stmf_svc_active == NULL) {
7850 		/* Do timeouts */
7851 		if (stmf_state.stmf_nlus &&
7852 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
7853 			if (!stmf_state.stmf_svc_ilu_timing) {
7854 				/* we are starting a new round */
7855 				stmf_state.stmf_svc_ilu_timing =
7856 				    stmf_state.stmf_ilulist;
7857 				timing_start = ddi_get_lbolt();
7858 			}
7859 			stmf_check_ilu_timing();
7860 			if (!stmf_state.stmf_svc_ilu_timing) {
7861 				/* we finished a complete round */
7862 				timing_next =
7863 				    timing_start + drv_usectohz(5*1000*1000);
7864 			} else {
7865 				/* we still have some ilu items to check */
7866 				timing_next =
7867 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7868 			}
7869 			if (stmf_state.stmf_svc_active)
7870 				goto stmf_svc_loop;
7871 		}
7872 		/* Check if there are free tasks to clear */
7873 		if (stmf_state.stmf_nlus &&
7874 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
7875 			if (!stmf_state.stmf_svc_ilu_draining) {
7876 				/* we are starting a new round */
7877 				stmf_state.stmf_svc_ilu_draining =
7878 				    stmf_state.stmf_ilulist;
7879 				drain_start = ddi_get_lbolt();
7880 			}
7881 			stmf_check_freetask();
7882 			if (!stmf_state.stmf_svc_ilu_draining) {
7883 				/* we finished a complete round */
7884 				drain_next =
7885 				    drain_start + drv_usectohz(10*1000*1000);
7886 			} else {
7887 				/* we still have some ilu items to check */
7888 				drain_next =
7889 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7890 			}
7891 			if (stmf_state.stmf_svc_active)
7892 				goto stmf_svc_loop;
7893 		}
7894 
7895 		/* Check if we need to run worker_mgmt */
7896 		if (ddi_get_lbolt() > worker_delay) {
7897 			stmf_worker_mgmt();
7898 			worker_delay = ddi_get_lbolt() +
7899 			    stmf_worker_mgmt_delay;
7900 		}
7901 
7902 		/* Check if any active session got its 1st LUN */
7903 		if (stmf_state.stmf_process_initial_luns) {
7904 			int stmf_level = 0;
7905 			int port_level;
7906 			for (ilport = stmf_state.stmf_ilportlist; ilport;
7907 			    ilport = next_ilport) {
7908 				int ilport_lock_held;
7909 				next_ilport = ilport->ilport_next;
7910 				if ((ilport->ilport_flags &
7911 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
7912 					continue;
7913 				}
7914 				port_level = 0;
7915 				rw_enter(&ilport->ilport_lock, RW_READER);
7916 				ilport_lock_held = 1;
7917 				for (iss = ilport->ilport_ss_list; iss;
7918 				    iss = iss->iss_next) {
7919 					if ((iss->iss_flags &
7920 					    ISS_GOT_INITIAL_LUNS) == 0) {
7921 						continue;
7922 					}
7923 					port_level++;
7924 					stmf_level++;
7925 					atomic_and_32(&iss->iss_flags,
7926 					    ~ISS_GOT_INITIAL_LUNS);
7927 					atomic_or_32(&iss->iss_flags,
7928 					    ISS_EVENT_ACTIVE);
7929 					rw_exit(&ilport->ilport_lock);
7930 					ilport_lock_held = 0;
7931 					mutex_exit(&stmf_state.stmf_lock);
7932 					stmf_generate_lport_event(ilport,
7933 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
7934 					    iss->iss_ss, 0);
7935 					atomic_and_32(&iss->iss_flags,
7936 					    ~ISS_EVENT_ACTIVE);
7937 					mutex_enter(&stmf_state.stmf_lock);
7938 					/*
7939 					 * scan all the ilports again as the
7940 					 * ilport list might have changed.
7941 					 */
7942 					next_ilport =
7943 					    stmf_state.stmf_ilportlist;
7944 					break;
7945 				}
7946 				if (port_level == 0) {
7947 					atomic_and_32(&ilport->ilport_flags,
7948 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
7949 				}
7950 				/* drop the lock if we are holding it. */
7951 				if (ilport_lock_held == 1)
7952 					rw_exit(&ilport->ilport_lock);
7953 
7954 				/* Max 4 session at a time */
7955 				if (stmf_level >= 4) {
7956 					break;
7957 				}
7958 			}
7959 			if (stmf_level == 0) {
7960 				stmf_state.stmf_process_initial_luns = 0;
7961 			}
7962 		}
7963 
7964 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
7965 		(void) cv_reltimedwait(&stmf_state.stmf_cv,
7966 		    &stmf_state.stmf_lock, td, TR_CLOCK_TICK);
7967 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
7968 	}
7969 	goto stmf_svc_loop;
7970 }
7971 
7972 void
7973 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
7974 {
7975 	stmf_svc_req_t *req;
7976 	int s;
7977 
7978 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
7979 	s = sizeof (stmf_svc_req_t);
7980 	if (info->st_additional_info) {
7981 		s += strlen(info->st_additional_info) + 1;
7982 	}
7983 	req = kmem_zalloc(s, KM_SLEEP);
7984 
7985 	req->svc_cmd = cmd;
7986 	req->svc_obj = obj;
7987 	req->svc_info.st_rflags = info->st_rflags;
7988 	if (info->st_additional_info) {
7989 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
7990 		    sizeof (stmf_svc_req_t)));
7991 		(void) strcpy(req->svc_info.st_additional_info,
7992 		    info->st_additional_info);
7993 	}
7994 	req->svc_req_alloc_size = s;
7995 
7996 	mutex_enter(&stmf_state.stmf_lock);
7997 	req->svc_next = stmf_state.stmf_svc_active;
7998 	stmf_state.stmf_svc_active = req;
7999 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
8000 		cv_signal(&stmf_state.stmf_cv);
8001 	}
8002 	mutex_exit(&stmf_state.stmf_lock);
8003 }
8004 
8005 void
8006 stmf_trace(caddr_t ident, const char *fmt, ...)
8007 {
8008 	va_list args;
8009 	char tbuf[160];
8010 	int len;
8011 
8012 	if (!stmf_trace_on)
8013 		return;
8014 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
8015 	    ddi_get_lbolt());
8016 	va_start(args, fmt);
8017 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
8018 	va_end(args);
8019 
8020 	if (len > 158) {
8021 		len = 158;
8022 	}
8023 	tbuf[len++] = '\n';
8024 	tbuf[len] = 0;
8025 
8026 	mutex_enter(&trace_buf_lock);
8027 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
8028 	trace_buf_curndx += len;
8029 	if (trace_buf_curndx > (trace_buf_size - 320))
8030 		trace_buf_curndx = 0;
8031 	mutex_exit(&trace_buf_lock);
8032 }
8033 
8034 void
8035 stmf_trace_clear()
8036 {
8037 	if (!stmf_trace_on)
8038 		return;
8039 	mutex_enter(&trace_buf_lock);
8040 	trace_buf_curndx = 0;
8041 	if (trace_buf_size > 0)
8042 		stmf_trace_buf[0] = 0;
8043 	mutex_exit(&trace_buf_lock);
8044 }
8045 
8046 static void
8047 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
8048 {
8049 	stmf_state_change_info_t	change_info;
8050 	void				*ctl_private;
8051 	uint32_t			ctl_cmd;
8052 	int				msg = 0;
8053 
8054 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
8055 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
8056 	change_info.st_additional_info = info;
8057 	if (offline_lu) {
8058 		change_info.st_rflags = STMF_RFLAG_RESET |
8059 		    STMF_RFLAG_LU_ABORT;
8060 		ctl_private = task->task_lu;
8061 		if (((stmf_i_lu_t *)
8062 		    task->task_lu->lu_stmf_private)->ilu_state ==
8063 		    STMF_STATE_ONLINE) {
8064 			msg = 1;
8065 		}
8066 		ctl_cmd = STMF_CMD_LU_OFFLINE;
8067 	} else {
8068 		change_info.st_rflags = STMF_RFLAG_RESET |
8069 		    STMF_RFLAG_LPORT_ABORT;
8070 		ctl_private = task->task_lport;
8071 		if (((stmf_i_local_port_t *)
8072 		    task->task_lport->lport_stmf_private)->ilport_state ==
8073 		    STMF_STATE_ONLINE) {
8074 			msg = 1;
8075 		}
8076 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
8077 	}
8078 
8079 	if (msg) {
8080 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
8081 		    offline_lu ? "LU" : "LPORT", info ? info :
8082 		    "<no additional info>");
8083 	}
8084 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
8085 }
8086