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