xref: /titanic_50/usr/src/uts/common/io/comstar/stmf/stmf.c (revision 9b902bdefd2b455e3a0a235f279879c5a30eb1d5)
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);
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 	stmf_xd_to_dbuf(dbuf);
1826 
1827 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
1828 	(void) stmf_xfer_data(task, dbuf, 0);
1829 	return (STMF_SUCCESS);
1830 }
1831 
1832 stmf_status_t
1833 stmf_proxy_scsi_cmd(scsi_task_t *task, stmf_data_buf_t *dbuf)
1834 {
1835 	stmf_i_scsi_task_t *itask =
1836 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
1837 	stmf_i_local_port_t *ilport =
1838 	    (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
1839 	stmf_ic_msg_t *ic_cmd_msg;
1840 	stmf_ic_msg_status_t ic_ret;
1841 	stmf_status_t ret = STMF_FAILURE;
1842 
1843 	if (stmf_state.stmf_alua_state != 1) {
1844 		cmn_err(CE_WARN, "stmf alua state is disabled");
1845 		return (STMF_FAILURE);
1846 	}
1847 
1848 	if (ilport->ilport_proxy_registered == 0) {
1849 		return (STMF_FAILURE);
1850 	}
1851 
1852 	mutex_enter(&stmf_state.stmf_lock);
1853 	itask->itask_proxy_msg_id = stmf_proxy_msg_id++;
1854 	mutex_exit(&stmf_state.stmf_lock);
1855 	itask->itask_proxy_dbuf = dbuf;
1856 
1857 	/*
1858 	 * stmf will now take over the task handling for this task
1859 	 * but it still needs to be treated differently from other
1860 	 * default handled tasks, hence the ITASK_PROXY_TASK.
1861 	 * If this is a task management function, we're really just
1862 	 * duping the command to the peer. Set the TM bit so that
1863 	 * we can recognize this on return since we won't be completing
1864 	 * the proxied task in that case.
1865 	 */
1866 	if (task->task_mgmt_function) {
1867 		itask->itask_proxy_msg_id |= MSG_ID_TM_BIT;
1868 	} else {
1869 		itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_PROXY_TASK;
1870 	}
1871 	if (dbuf) {
1872 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1873 		    task, dbuf->db_data_size, dbuf->db_sglist[0].seg_addr,
1874 		    itask->itask_proxy_msg_id);
1875 	} else {
1876 		ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1877 		    task, 0, NULL, itask->itask_proxy_msg_id);
1878 	}
1879 	if (ic_cmd_msg) {
1880 		ic_ret = ic_tx_msg(ic_cmd_msg);
1881 		if (ic_ret == STMF_IC_MSG_SUCCESS) {
1882 			ret = STMF_SUCCESS;
1883 		}
1884 	}
1885 	return (ret);
1886 }
1887 
1888 
1889 stmf_status_t
1890 pppt_modload()
1891 {
1892 	int error;
1893 
1894 	if (pppt_mod == NULL && ((pppt_mod =
1895 	    ddi_modopen("drv/pppt", KRTLD_MODE_FIRST, &error)) == NULL)) {
1896 		cmn_err(CE_WARN, "Unable to load pppt");
1897 		return (STMF_FAILURE);
1898 	}
1899 
1900 	if (ic_reg_port_msg_alloc == NULL && ((ic_reg_port_msg_alloc =
1901 	    (stmf_ic_reg_port_msg_alloc_func_t)
1902 	    ddi_modsym(pppt_mod, "stmf_ic_reg_port_msg_alloc",
1903 	    &error)) == NULL)) {
1904 		cmn_err(CE_WARN,
1905 		    "Unable to find symbol - stmf_ic_reg_port_msg_alloc");
1906 		return (STMF_FAILURE);
1907 	}
1908 
1909 
1910 	if (ic_dereg_port_msg_alloc == NULL && ((ic_dereg_port_msg_alloc =
1911 	    (stmf_ic_dereg_port_msg_alloc_func_t)
1912 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_port_msg_alloc",
1913 	    &error)) == NULL)) {
1914 		cmn_err(CE_WARN,
1915 		    "Unable to find symbol - stmf_ic_dereg_port_msg_alloc");
1916 		return (STMF_FAILURE);
1917 	}
1918 
1919 	if (ic_reg_lun_msg_alloc == NULL && ((ic_reg_lun_msg_alloc =
1920 	    (stmf_ic_reg_lun_msg_alloc_func_t)
1921 	    ddi_modsym(pppt_mod, "stmf_ic_reg_lun_msg_alloc",
1922 	    &error)) == NULL)) {
1923 		cmn_err(CE_WARN,
1924 		    "Unable to find symbol - stmf_ic_reg_lun_msg_alloc");
1925 		return (STMF_FAILURE);
1926 	}
1927 
1928 	if (ic_lun_active_msg_alloc == NULL && ((ic_lun_active_msg_alloc =
1929 	    (stmf_ic_lun_active_msg_alloc_func_t)
1930 	    ddi_modsym(pppt_mod, "stmf_ic_lun_active_msg_alloc",
1931 	    &error)) == NULL)) {
1932 		cmn_err(CE_WARN,
1933 		    "Unable to find symbol - stmf_ic_lun_active_msg_alloc");
1934 		return (STMF_FAILURE);
1935 	}
1936 
1937 	if (ic_dereg_lun_msg_alloc == NULL && ((ic_dereg_lun_msg_alloc =
1938 	    (stmf_ic_dereg_lun_msg_alloc_func_t)
1939 	    ddi_modsym(pppt_mod, "stmf_ic_dereg_lun_msg_alloc",
1940 	    &error)) == NULL)) {
1941 		cmn_err(CE_WARN,
1942 		    "Unable to find symbol - stmf_ic_dereg_lun_msg_alloc");
1943 		return (STMF_FAILURE);
1944 	}
1945 
1946 	if (ic_scsi_cmd_msg_alloc == NULL && ((ic_scsi_cmd_msg_alloc =
1947 	    (stmf_ic_scsi_cmd_msg_alloc_func_t)
1948 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_cmd_msg_alloc",
1949 	    &error)) == NULL)) {
1950 		cmn_err(CE_WARN,
1951 		    "Unable to find symbol - stmf_ic_scsi_cmd_msg_alloc");
1952 		return (STMF_FAILURE);
1953 	}
1954 
1955 	if (ic_scsi_data_xfer_done_msg_alloc == NULL &&
1956 	    ((ic_scsi_data_xfer_done_msg_alloc =
1957 	    (stmf_ic_scsi_data_xfer_done_msg_alloc_func_t)
1958 	    ddi_modsym(pppt_mod, "stmf_ic_scsi_data_xfer_done_msg_alloc",
1959 	    &error)) == NULL)) {
1960 		cmn_err(CE_WARN,
1961 		    "Unable to find symbol -"
1962 		    "stmf_ic_scsi_data_xfer_done_msg_alloc");
1963 		return (STMF_FAILURE);
1964 	}
1965 
1966 	if (ic_session_reg_msg_alloc == NULL &&
1967 	    ((ic_session_reg_msg_alloc =
1968 	    (stmf_ic_session_create_msg_alloc_func_t)
1969 	    ddi_modsym(pppt_mod, "stmf_ic_session_create_msg_alloc",
1970 	    &error)) == NULL)) {
1971 		cmn_err(CE_WARN,
1972 		    "Unable to find symbol -"
1973 		    "stmf_ic_session_create_msg_alloc");
1974 		return (STMF_FAILURE);
1975 	}
1976 
1977 	if (ic_session_dereg_msg_alloc == NULL &&
1978 	    ((ic_session_dereg_msg_alloc =
1979 	    (stmf_ic_session_destroy_msg_alloc_func_t)
1980 	    ddi_modsym(pppt_mod, "stmf_ic_session_destroy_msg_alloc",
1981 	    &error)) == NULL)) {
1982 		cmn_err(CE_WARN,
1983 		    "Unable to find symbol -"
1984 		    "stmf_ic_session_destroy_msg_alloc");
1985 		return (STMF_FAILURE);
1986 	}
1987 
1988 	if (ic_tx_msg == NULL && ((ic_tx_msg =
1989 	    (stmf_ic_tx_msg_func_t)ddi_modsym(pppt_mod, "stmf_ic_tx_msg",
1990 	    &error)) == NULL)) {
1991 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_tx_msg");
1992 		return (STMF_FAILURE);
1993 	}
1994 
1995 	if (ic_msg_free == NULL && ((ic_msg_free =
1996 	    (stmf_ic_msg_free_func_t)ddi_modsym(pppt_mod, "stmf_ic_msg_free",
1997 	    &error)) == NULL)) {
1998 		cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_msg_free");
1999 		return (STMF_FAILURE);
2000 	}
2001 	return (STMF_SUCCESS);
2002 }
2003 
2004 static void
2005 stmf_get_alua_state(stmf_alua_state_desc_t *alua_state)
2006 {
2007 	mutex_enter(&stmf_state.stmf_lock);
2008 	alua_state->alua_node = stmf_state.stmf_alua_node;
2009 	alua_state->alua_state = stmf_state.stmf_alua_state;
2010 	mutex_exit(&stmf_state.stmf_lock);
2011 }
2012 
2013 
2014 static int
2015 stmf_set_alua_state(stmf_alua_state_desc_t *alua_state)
2016 {
2017 	stmf_i_local_port_t *ilport;
2018 	stmf_i_lu_t *ilu;
2019 	stmf_lu_t *lu;
2020 	stmf_ic_msg_status_t ic_ret;
2021 	stmf_ic_msg_t *ic_reg_lun, *ic_reg_port;
2022 	stmf_local_port_t *lport;
2023 	int ret = 0;
2024 
2025 	if (alua_state->alua_state > 1 || alua_state->alua_node > 1) {
2026 		return (EINVAL);
2027 	}
2028 
2029 	mutex_enter(&stmf_state.stmf_lock);
2030 	if (alua_state->alua_state == 1) {
2031 		if (pppt_modload() == STMF_FAILURE) {
2032 			ret = EIO;
2033 			goto err;
2034 		}
2035 		if (alua_state->alua_node != 0) {
2036 			/* reset existing rtpids to new base */
2037 			stmf_rtpid_counter = 255;
2038 		}
2039 		stmf_state.stmf_alua_node = alua_state->alua_node;
2040 		stmf_state.stmf_alua_state = 1;
2041 		/* register existing local ports with ppp */
2042 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2043 		    ilport = ilport->ilport_next) {
2044 			/* skip standby ports and non-alua participants */
2045 			if (ilport->ilport_standby == 1 ||
2046 			    ilport->ilport_alua == 0) {
2047 				continue;
2048 			}
2049 			if (alua_state->alua_node != 0) {
2050 				ilport->ilport_rtpid =
2051 				    atomic_add_16_nv(&stmf_rtpid_counter, 1);
2052 			}
2053 			lport = ilport->ilport_lport;
2054 			ic_reg_port = ic_reg_port_msg_alloc(
2055 			    lport->lport_id, ilport->ilport_rtpid,
2056 			    0, NULL, stmf_proxy_msg_id);
2057 			if (ic_reg_port) {
2058 				ic_ret = ic_tx_msg(ic_reg_port);
2059 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
2060 					ilport->ilport_reg_msgid =
2061 					    stmf_proxy_msg_id++;
2062 				} else {
2063 					cmn_err(CE_WARN,
2064 					    "error on port registration "
2065 					    "port - %s",
2066 					    ilport->ilport_kstat_tgt_name);
2067 				}
2068 			}
2069 		}
2070 		/* register existing logical units */
2071 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
2072 		    ilu = ilu->ilu_next) {
2073 			if (ilu->ilu_access != STMF_LU_ACTIVE) {
2074 				continue;
2075 			}
2076 			/* register with proxy module */
2077 			lu = ilu->ilu_lu;
2078 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2079 			    lu->lu_lp->lp_alua_support) {
2080 				ilu->ilu_alua = 1;
2081 				/* allocate the register message */
2082 				ic_reg_lun = ic_reg_lun_msg_alloc(
2083 				    lu->lu_id->ident, lu->lu_lp->lp_name,
2084 				    lu->lu_proxy_reg_arg_len,
2085 				    (uint8_t *)lu->lu_proxy_reg_arg,
2086 				    stmf_proxy_msg_id);
2087 				/* send the message */
2088 				if (ic_reg_lun) {
2089 					ic_ret = ic_tx_msg(ic_reg_lun);
2090 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
2091 						stmf_proxy_msg_id++;
2092 					}
2093 				}
2094 			}
2095 		}
2096 	} else {
2097 		stmf_state.stmf_alua_state = 0;
2098 	}
2099 
2100 err:
2101 	mutex_exit(&stmf_state.stmf_lock);
2102 	return (ret);
2103 }
2104 
2105 
2106 typedef struct {
2107 	void	*bp;	/* back pointer from internal struct to main struct */
2108 	int	alloc_size;
2109 } __istmf_t;
2110 
2111 typedef struct {
2112 	__istmf_t	*fp;	/* Framework private */
2113 	void		*cp;	/* Caller private */
2114 	void		*ss;	/* struct specific */
2115 } __stmf_t;
2116 
2117 static struct {
2118 	int shared;
2119 	int fw_private;
2120 } stmf_sizes[] = { { 0, 0 },
2121 	{ GET_STRUCT_SIZE(stmf_lu_provider_t),
2122 		GET_STRUCT_SIZE(stmf_i_lu_provider_t) },
2123 	{ GET_STRUCT_SIZE(stmf_port_provider_t),
2124 		GET_STRUCT_SIZE(stmf_i_port_provider_t) },
2125 	{ GET_STRUCT_SIZE(stmf_local_port_t),
2126 		GET_STRUCT_SIZE(stmf_i_local_port_t) },
2127 	{ GET_STRUCT_SIZE(stmf_lu_t),
2128 		GET_STRUCT_SIZE(stmf_i_lu_t) },
2129 	{ GET_STRUCT_SIZE(stmf_scsi_session_t),
2130 		GET_STRUCT_SIZE(stmf_i_scsi_session_t) },
2131 	{ GET_STRUCT_SIZE(scsi_task_t),
2132 		GET_STRUCT_SIZE(stmf_i_scsi_task_t) },
2133 	{ GET_STRUCT_SIZE(stmf_data_buf_t),
2134 		GET_STRUCT_SIZE(__istmf_t) },
2135 	{ GET_STRUCT_SIZE(stmf_dbuf_store_t),
2136 		GET_STRUCT_SIZE(__istmf_t) }
2137 
2138 };
2139 
2140 void *
2141 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags)
2142 {
2143 	int stmf_size;
2144 	int kmem_flag;
2145 	__stmf_t *sh;
2146 
2147 	if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS))
2148 		return (NULL);
2149 
2150 	if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
2151 		kmem_flag = KM_NOSLEEP;
2152 	} else {
2153 		kmem_flag = KM_SLEEP;
2154 	}
2155 
2156 	additional_size = (additional_size + 7) & (~7);
2157 	stmf_size = stmf_sizes[struct_id].shared +
2158 	    stmf_sizes[struct_id].fw_private + additional_size;
2159 
2160 	if (flags & AF_DONTZERO)
2161 		sh = (__stmf_t *)kmem_alloc(stmf_size, kmem_flag);
2162 	else
2163 		sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag);
2164 
2165 	if (sh == NULL)
2166 		return (NULL);
2167 
2168 	/*
2169 	 * In principle, the implementation inside stmf_alloc should not
2170 	 * be changed anyway. But the original order of framework private
2171 	 * data and caller private data does not support sglist in the caller
2172 	 * private data.
2173 	 * To work around this, the memory segments of framework private
2174 	 * data and caller private data are re-ordered here.
2175 	 * A better solution is to provide a specific interface to allocate
2176 	 * the sglist, then we will not need this workaround any more.
2177 	 * But before the new interface is available, the memory segment
2178 	 * ordering should be kept as is.
2179 	 */
2180 	sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
2181 	sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh,
2182 	    stmf_sizes[struct_id].shared + additional_size);
2183 
2184 	sh->fp->bp = sh;
2185 	/* Just store the total size instead of storing additional size */
2186 	sh->fp->alloc_size = stmf_size;
2187 
2188 	return (sh);
2189 }
2190 
2191 void
2192 stmf_free(void *ptr)
2193 {
2194 	__stmf_t *sh = (__stmf_t *)ptr;
2195 
2196 	/*
2197 	 * So far we dont need any struct specific processing. If such
2198 	 * a need ever arises, then store the struct id in the framework
2199 	 * private section and get it here as sh->fp->struct_id.
2200 	 */
2201 	kmem_free(ptr, sh->fp->alloc_size);
2202 }
2203 
2204 /*
2205  * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
2206  * framework and returns a pointer to framework private data for the lu.
2207  * Returns NULL if the lu was not found.
2208  */
2209 stmf_i_lu_t *
2210 stmf_lookup_lu(stmf_lu_t *lu)
2211 {
2212 	stmf_i_lu_t *ilu;
2213 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2214 
2215 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2216 		if (ilu->ilu_lu == lu)
2217 			return (ilu);
2218 	}
2219 	return (NULL);
2220 }
2221 
2222 /*
2223  * Given a pointer to stmf_local_port_t, verifies if this lport is registered
2224  * with the framework and returns a pointer to framework private data for
2225  * the lport.
2226  * Returns NULL if the lport was not found.
2227  */
2228 stmf_i_local_port_t *
2229 stmf_lookup_lport(stmf_local_port_t *lport)
2230 {
2231 	stmf_i_local_port_t *ilport;
2232 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2233 
2234 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2235 	    ilport = ilport->ilport_next) {
2236 		if (ilport->ilport_lport == lport)
2237 			return (ilport);
2238 	}
2239 	return (NULL);
2240 }
2241 
2242 stmf_status_t
2243 stmf_register_lu_provider(stmf_lu_provider_t *lp)
2244 {
2245 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2246 	stmf_pp_data_t *ppd;
2247 	uint32_t cb_flags;
2248 
2249 	if (lp->lp_lpif_rev != LPIF_REV_1 && lp->lp_lpif_rev != LPIF_REV_2)
2250 		return (STMF_FAILURE);
2251 
2252 	mutex_enter(&stmf_state.stmf_lock);
2253 	ilp->ilp_next = stmf_state.stmf_ilplist;
2254 	stmf_state.stmf_ilplist = ilp;
2255 	stmf_state.stmf_nlps++;
2256 
2257 	/* See if we need to do a callback */
2258 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2259 		if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
2260 			break;
2261 		}
2262 	}
2263 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2264 		goto rlp_bail_out;
2265 	}
2266 	ilp->ilp_ppd = ppd;
2267 	ppd->ppd_provider = ilp;
2268 	if (lp->lp_cb == NULL)
2269 		goto rlp_bail_out;
2270 	ilp->ilp_cb_in_progress = 1;
2271 	cb_flags = STMF_PCB_PREG_COMPLETE;
2272 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2273 		cb_flags |= STMF_PCB_STMF_ONLINING;
2274 	mutex_exit(&stmf_state.stmf_lock);
2275 	lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2276 	mutex_enter(&stmf_state.stmf_lock);
2277 	ilp->ilp_cb_in_progress = 0;
2278 
2279 rlp_bail_out:
2280 	mutex_exit(&stmf_state.stmf_lock);
2281 
2282 	return (STMF_SUCCESS);
2283 }
2284 
2285 stmf_status_t
2286 stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
2287 {
2288 	stmf_i_lu_provider_t	**ppilp;
2289 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2290 
2291 	mutex_enter(&stmf_state.stmf_lock);
2292 	if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
2293 		mutex_exit(&stmf_state.stmf_lock);
2294 		return (STMF_BUSY);
2295 	}
2296 	for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
2297 	    ppilp = &((*ppilp)->ilp_next)) {
2298 		if (*ppilp == ilp) {
2299 			*ppilp = ilp->ilp_next;
2300 			stmf_state.stmf_nlps--;
2301 			if (ilp->ilp_ppd) {
2302 				ilp->ilp_ppd->ppd_provider = NULL;
2303 				ilp->ilp_ppd = NULL;
2304 			}
2305 			mutex_exit(&stmf_state.stmf_lock);
2306 			return (STMF_SUCCESS);
2307 		}
2308 	}
2309 	mutex_exit(&stmf_state.stmf_lock);
2310 	return (STMF_NOT_FOUND);
2311 }
2312 
2313 stmf_status_t
2314 stmf_register_port_provider(stmf_port_provider_t *pp)
2315 {
2316 	stmf_i_port_provider_t *ipp =
2317 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
2318 	stmf_pp_data_t *ppd;
2319 	uint32_t cb_flags;
2320 
2321 	if (pp->pp_portif_rev != PORTIF_REV_1)
2322 		return (STMF_FAILURE);
2323 
2324 	mutex_enter(&stmf_state.stmf_lock);
2325 	ipp->ipp_next = stmf_state.stmf_ipplist;
2326 	stmf_state.stmf_ipplist = ipp;
2327 	stmf_state.stmf_npps++;
2328 	/* See if we need to do a callback */
2329 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2330 		if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
2331 			break;
2332 		}
2333 	}
2334 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2335 		goto rpp_bail_out;
2336 	}
2337 	ipp->ipp_ppd = ppd;
2338 	ppd->ppd_provider = ipp;
2339 	if (pp->pp_cb == NULL)
2340 		goto rpp_bail_out;
2341 	ipp->ipp_cb_in_progress = 1;
2342 	cb_flags = STMF_PCB_PREG_COMPLETE;
2343 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2344 		cb_flags |= STMF_PCB_STMF_ONLINING;
2345 	mutex_exit(&stmf_state.stmf_lock);
2346 	pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2347 	mutex_enter(&stmf_state.stmf_lock);
2348 	ipp->ipp_cb_in_progress = 0;
2349 
2350 rpp_bail_out:
2351 	mutex_exit(&stmf_state.stmf_lock);
2352 
2353 	return (STMF_SUCCESS);
2354 }
2355 
2356 stmf_status_t
2357 stmf_deregister_port_provider(stmf_port_provider_t *pp)
2358 {
2359 	stmf_i_port_provider_t *ipp =
2360 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
2361 	stmf_i_port_provider_t **ppipp;
2362 
2363 	mutex_enter(&stmf_state.stmf_lock);
2364 	if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
2365 		mutex_exit(&stmf_state.stmf_lock);
2366 		return (STMF_BUSY);
2367 	}
2368 	for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
2369 	    ppipp = &((*ppipp)->ipp_next)) {
2370 		if (*ppipp == ipp) {
2371 			*ppipp = ipp->ipp_next;
2372 			stmf_state.stmf_npps--;
2373 			if (ipp->ipp_ppd) {
2374 				ipp->ipp_ppd->ppd_provider = NULL;
2375 				ipp->ipp_ppd = NULL;
2376 			}
2377 			mutex_exit(&stmf_state.stmf_lock);
2378 			return (STMF_SUCCESS);
2379 		}
2380 	}
2381 	mutex_exit(&stmf_state.stmf_lock);
2382 	return (STMF_NOT_FOUND);
2383 }
2384 
2385 int
2386 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
2387     uint32_t *err_ret)
2388 {
2389 	stmf_i_port_provider_t		*ipp;
2390 	stmf_i_lu_provider_t		*ilp;
2391 	stmf_pp_data_t			*ppd;
2392 	nvlist_t			*nv;
2393 	int				s;
2394 	int				ret;
2395 
2396 	*err_ret = 0;
2397 
2398 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2399 		return (EINVAL);
2400 	}
2401 
2402 	mutex_enter(&stmf_state.stmf_lock);
2403 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2404 		if (ppi->ppi_lu_provider) {
2405 			if (!ppd->ppd_lu_provider)
2406 				continue;
2407 		} else if (ppi->ppi_port_provider) {
2408 			if (!ppd->ppd_port_provider)
2409 				continue;
2410 		}
2411 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2412 			break;
2413 	}
2414 
2415 	if (ppd == NULL) {
2416 		/* New provider */
2417 		s = strlen(ppi->ppi_name);
2418 		if (s > 254) {
2419 			mutex_exit(&stmf_state.stmf_lock);
2420 			return (EINVAL);
2421 		}
2422 		s += sizeof (stmf_pp_data_t) - 7;
2423 
2424 		ppd = kmem_zalloc(s, KM_NOSLEEP);
2425 		if (ppd == NULL) {
2426 			mutex_exit(&stmf_state.stmf_lock);
2427 			return (ENOMEM);
2428 		}
2429 		ppd->ppd_alloc_size = s;
2430 		(void) strcpy(ppd->ppd_name, ppi->ppi_name);
2431 
2432 		/* See if this provider already exists */
2433 		if (ppi->ppi_lu_provider) {
2434 			ppd->ppd_lu_provider = 1;
2435 			for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
2436 			    ilp = ilp->ilp_next) {
2437 				if (strcmp(ppi->ppi_name,
2438 				    ilp->ilp_lp->lp_name) == 0) {
2439 					ppd->ppd_provider = ilp;
2440 					ilp->ilp_ppd = ppd;
2441 					break;
2442 				}
2443 			}
2444 		} else {
2445 			ppd->ppd_port_provider = 1;
2446 			for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
2447 			    ipp = ipp->ipp_next) {
2448 				if (strcmp(ppi->ppi_name,
2449 				    ipp->ipp_pp->pp_name) == 0) {
2450 					ppd->ppd_provider = ipp;
2451 					ipp->ipp_ppd = ppd;
2452 					break;
2453 				}
2454 			}
2455 		}
2456 
2457 		/* Link this ppd in */
2458 		ppd->ppd_next = stmf_state.stmf_ppdlist;
2459 		stmf_state.stmf_ppdlist = ppd;
2460 	}
2461 
2462 	/*
2463 	 * User is requesting that the token be checked.
2464 	 * If there was another set after the user's get
2465 	 * it's an error
2466 	 */
2467 	if (ppi->ppi_token_valid) {
2468 		if (ppi->ppi_token != ppd->ppd_token) {
2469 			*err_ret = STMF_IOCERR_PPD_UPDATED;
2470 			mutex_exit(&stmf_state.stmf_lock);
2471 			return (EINVAL);
2472 		}
2473 	}
2474 
2475 	if ((ret = nvlist_unpack((char *)ppi->ppi_data,
2476 	    (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
2477 		mutex_exit(&stmf_state.stmf_lock);
2478 		return (ret);
2479 	}
2480 
2481 	/* Free any existing lists and add this one to the ppd */
2482 	if (ppd->ppd_nv)
2483 		nvlist_free(ppd->ppd_nv);
2484 	ppd->ppd_nv = nv;
2485 
2486 	/* set the token for writes */
2487 	ppd->ppd_token++;
2488 	/* return token to caller */
2489 	if (ppi_token) {
2490 		*ppi_token = ppd->ppd_token;
2491 	}
2492 
2493 	/* If there is a provider registered, do the notifications */
2494 	if (ppd->ppd_provider) {
2495 		uint32_t cb_flags = 0;
2496 
2497 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2498 			cb_flags |= STMF_PCB_STMF_ONLINING;
2499 		if (ppi->ppi_lu_provider) {
2500 			ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
2501 			if (ilp->ilp_lp->lp_cb == NULL)
2502 				goto bail_out;
2503 			ilp->ilp_cb_in_progress = 1;
2504 			mutex_exit(&stmf_state.stmf_lock);
2505 			ilp->ilp_lp->lp_cb(ilp->ilp_lp,
2506 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2507 			mutex_enter(&stmf_state.stmf_lock);
2508 			ilp->ilp_cb_in_progress = 0;
2509 		} else {
2510 			ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
2511 			if (ipp->ipp_pp->pp_cb == NULL)
2512 				goto bail_out;
2513 			ipp->ipp_cb_in_progress = 1;
2514 			mutex_exit(&stmf_state.stmf_lock);
2515 			ipp->ipp_pp->pp_cb(ipp->ipp_pp,
2516 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2517 			mutex_enter(&stmf_state.stmf_lock);
2518 			ipp->ipp_cb_in_progress = 0;
2519 		}
2520 	}
2521 
2522 bail_out:
2523 	mutex_exit(&stmf_state.stmf_lock);
2524 
2525 	return (0);
2526 }
2527 
2528 void
2529 stmf_delete_ppd(stmf_pp_data_t *ppd)
2530 {
2531 	stmf_pp_data_t **pppd;
2532 
2533 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2534 	if (ppd->ppd_provider) {
2535 		if (ppd->ppd_lu_provider) {
2536 			((stmf_i_lu_provider_t *)
2537 			    ppd->ppd_provider)->ilp_ppd = NULL;
2538 		} else {
2539 			((stmf_i_port_provider_t *)
2540 			    ppd->ppd_provider)->ipp_ppd = NULL;
2541 		}
2542 		ppd->ppd_provider = NULL;
2543 	}
2544 
2545 	for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
2546 	    pppd = &((*pppd)->ppd_next)) {
2547 		if (*pppd == ppd)
2548 			break;
2549 	}
2550 
2551 	if (*pppd == NULL)
2552 		return;
2553 
2554 	*pppd = ppd->ppd_next;
2555 	if (ppd->ppd_nv)
2556 		nvlist_free(ppd->ppd_nv);
2557 
2558 	kmem_free(ppd, ppd->ppd_alloc_size);
2559 }
2560 
2561 int
2562 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
2563 {
2564 	stmf_pp_data_t *ppd;
2565 	int ret = ENOENT;
2566 
2567 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2568 		return (EINVAL);
2569 	}
2570 
2571 	mutex_enter(&stmf_state.stmf_lock);
2572 
2573 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2574 		if (ppi->ppi_lu_provider) {
2575 			if (!ppd->ppd_lu_provider)
2576 				continue;
2577 		} else if (ppi->ppi_port_provider) {
2578 			if (!ppd->ppd_port_provider)
2579 				continue;
2580 		}
2581 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2582 			break;
2583 	}
2584 
2585 	if (ppd) {
2586 		ret = 0;
2587 		stmf_delete_ppd(ppd);
2588 	}
2589 	mutex_exit(&stmf_state.stmf_lock);
2590 
2591 	return (ret);
2592 }
2593 
2594 int
2595 stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
2596     uint32_t *err_ret)
2597 {
2598 	stmf_pp_data_t *ppd;
2599 	size_t req_size;
2600 	int ret = ENOENT;
2601 	char *bufp = (char *)ppi_out->ppi_data;
2602 
2603 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2604 		return (EINVAL);
2605 	}
2606 
2607 	mutex_enter(&stmf_state.stmf_lock);
2608 
2609 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2610 		if (ppi->ppi_lu_provider) {
2611 			if (!ppd->ppd_lu_provider)
2612 				continue;
2613 		} else if (ppi->ppi_port_provider) {
2614 			if (!ppd->ppd_port_provider)
2615 				continue;
2616 		}
2617 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2618 			break;
2619 	}
2620 
2621 	if (ppd && ppd->ppd_nv) {
2622 		ppi_out->ppi_token = ppd->ppd_token;
2623 		if ((ret = nvlist_size(ppd->ppd_nv, &req_size,
2624 		    NV_ENCODE_XDR)) != 0) {
2625 			goto done;
2626 		}
2627 		ppi_out->ppi_data_size = req_size;
2628 		if (req_size > ppi->ppi_data_size) {
2629 			*err_ret = STMF_IOCERR_INSUFFICIENT_BUF;
2630 			ret = EINVAL;
2631 			goto done;
2632 		}
2633 
2634 		if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size,
2635 		    NV_ENCODE_XDR, 0)) != 0) {
2636 			goto done;
2637 		}
2638 		ret = 0;
2639 	}
2640 
2641 done:
2642 	mutex_exit(&stmf_state.stmf_lock);
2643 
2644 	return (ret);
2645 }
2646 
2647 void
2648 stmf_delete_all_ppds()
2649 {
2650 	stmf_pp_data_t *ppd, *nppd;
2651 
2652 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
2653 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
2654 		nppd = ppd->ppd_next;
2655 		stmf_delete_ppd(ppd);
2656 	}
2657 }
2658 
2659 /*
2660  * 16 is the max string length of a protocol_ident, increase
2661  * the size if needed.
2662  */
2663 #define	STMF_KSTAT_LU_SZ	(STMF_GUID_INPUT + 1 + 256)
2664 #define	STMF_KSTAT_TGT_SZ	(256 * 2 + 16)
2665 
2666 /*
2667  * This array matches the Protocol Identifier in stmf_ioctl.h
2668  */
2669 #define	MAX_PROTO_STR_LEN	32
2670 
2671 char *protocol_ident[PROTOCOL_ANY] = {
2672 	"Fibre Channel",
2673 	"Parallel SCSI",
2674 	"SSA",
2675 	"IEEE_1394",
2676 	"SRP",
2677 	"iSCSI",
2678 	"SAS",
2679 	"ADT",
2680 	"ATAPI",
2681 	"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"
2682 };
2683 
2684 /*
2685  * Update the lun wait/run queue count
2686  */
2687 static void
2688 stmf_update_kstat_lu_q(scsi_task_t *task, void func())
2689 {
2690 	stmf_i_lu_t		*ilu;
2691 	kstat_io_t		*kip;
2692 
2693 	if (task->task_lu == dlun0)
2694 		return;
2695 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2696 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2697 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2698 		if (kip != NULL) {
2699 			func(kip);
2700 		}
2701 	}
2702 }
2703 
2704 /*
2705  * Update the target(lport) wait/run queue count
2706  */
2707 static void
2708 stmf_update_kstat_lport_q(scsi_task_t *task, void func())
2709 {
2710 	stmf_i_local_port_t	*ilp;
2711 	kstat_io_t		*kip;
2712 
2713 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2714 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2715 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2716 		if (kip != NULL) {
2717 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2718 			func(kip);
2719 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2720 		}
2721 	}
2722 }
2723 
2724 static void
2725 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2726 {
2727 	stmf_i_local_port_t	*ilp;
2728 	kstat_io_t		*kip;
2729 
2730 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2731 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2732 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2733 		if (kip != NULL) {
2734 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2735 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2736 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2737 		}
2738 	}
2739 }
2740 
2741 static void
2742 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2743 {
2744 	stmf_i_lu_t		*ilu;
2745 	kstat_io_t		*kip;
2746 
2747 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2748 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2749 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2750 		if (kip != NULL) {
2751 			mutex_enter(ilu->ilu_kstat_io->ks_lock);
2752 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2753 			mutex_exit(ilu->ilu_kstat_io->ks_lock);
2754 		}
2755 	}
2756 }
2757 
2758 static void
2759 stmf_create_kstat_lu(stmf_i_lu_t *ilu)
2760 {
2761 	char				ks_nm[KSTAT_STRLEN];
2762 	stmf_kstat_lu_info_t		*ks_lu;
2763 
2764 	/* create kstat lun info */
2765 	ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ,
2766 	    KM_NOSLEEP);
2767 	if (ks_lu == NULL) {
2768 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2769 		return;
2770 	}
2771 
2772 	bzero(ks_nm, sizeof (ks_nm));
2773 	(void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu);
2774 	if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0,
2775 	    ks_nm, "misc", KSTAT_TYPE_NAMED,
2776 	    sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t),
2777 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2778 		kmem_free(ks_lu, STMF_KSTAT_LU_SZ);
2779 		cmn_err(CE_WARN, "STMF: kstat_create lu failed");
2780 		return;
2781 	}
2782 
2783 	ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ;
2784 	ilu->ilu_kstat_info->ks_data = ks_lu;
2785 
2786 	kstat_named_init(&ks_lu->i_lun_guid, "lun-guid",
2787 	    KSTAT_DATA_STRING);
2788 	kstat_named_init(&ks_lu->i_lun_alias, "lun-alias",
2789 	    KSTAT_DATA_STRING);
2790 
2791 	/* convert guid to hex string */
2792 	int		i;
2793 	uint8_t		*p = ilu->ilu_lu->lu_id->ident;
2794 	bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid));
2795 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
2796 		(void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]);
2797 	}
2798 	kstat_named_setstr(&ks_lu->i_lun_guid,
2799 	    (const char *)ilu->ilu_ascii_hex_guid);
2800 	kstat_named_setstr(&ks_lu->i_lun_alias,
2801 	    (const char *)ilu->ilu_lu->lu_alias);
2802 	kstat_install(ilu->ilu_kstat_info);
2803 
2804 	/* create kstat lun io */
2805 	bzero(ks_nm, sizeof (ks_nm));
2806 	(void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu);
2807 	if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2808 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2809 		cmn_err(CE_WARN, "STMF: kstat_create lu_io failed");
2810 		return;
2811 	}
2812 	mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0);
2813 	ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock;
2814 	kstat_install(ilu->ilu_kstat_io);
2815 }
2816 
2817 static void
2818 stmf_create_kstat_lport(stmf_i_local_port_t *ilport)
2819 {
2820 	char				ks_nm[KSTAT_STRLEN];
2821 	stmf_kstat_tgt_info_t		*ks_tgt;
2822 	int				id, len;
2823 
2824 	/* create kstat lport info */
2825 	ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ,
2826 	    KM_NOSLEEP);
2827 	if (ks_tgt == NULL) {
2828 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2829 		return;
2830 	}
2831 
2832 	bzero(ks_nm, sizeof (ks_nm));
2833 	(void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport);
2834 	if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME,
2835 	    0, ks_nm, "misc", KSTAT_TYPE_NAMED,
2836 	    sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t),
2837 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2838 		kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ);
2839 		cmn_err(CE_WARN, "STMF: kstat_create target failed");
2840 		return;
2841 	}
2842 
2843 	ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ;
2844 	ilport->ilport_kstat_info->ks_data = ks_tgt;
2845 
2846 	kstat_named_init(&ks_tgt->i_tgt_name, "target-name",
2847 	    KSTAT_DATA_STRING);
2848 	kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias",
2849 	    KSTAT_DATA_STRING);
2850 	kstat_named_init(&ks_tgt->i_protocol, "protocol",
2851 	    KSTAT_DATA_STRING);
2852 
2853 	/* ident might not be null terminated */
2854 	len = ilport->ilport_lport->lport_id->ident_length;
2855 	bcopy(ilport->ilport_lport->lport_id->ident,
2856 	    ilport->ilport_kstat_tgt_name, len);
2857 	ilport->ilport_kstat_tgt_name[len + 1] = NULL;
2858 	kstat_named_setstr(&ks_tgt->i_tgt_name,
2859 	    (const char *)ilport->ilport_kstat_tgt_name);
2860 	kstat_named_setstr(&ks_tgt->i_tgt_alias,
2861 	    (const char *)ilport->ilport_lport->lport_alias);
2862 	/* protocol */
2863 	if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) {
2864 		cmn_err(CE_WARN, "STMF: protocol_id out of bound");
2865 		id = PROTOCOL_ANY;
2866 	}
2867 	kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]);
2868 	kstat_install(ilport->ilport_kstat_info);
2869 
2870 	/* create kstat lport io */
2871 	bzero(ks_nm, sizeof (ks_nm));
2872 	(void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport);
2873 	if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2874 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2875 		cmn_err(CE_WARN, "STMF: kstat_create target_io failed");
2876 		return;
2877 	}
2878 	mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0);
2879 	ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock;
2880 	kstat_install(ilport->ilport_kstat_io);
2881 }
2882 
2883 /*
2884  * set the asymmetric access state for a logical unit
2885  * caller is responsible for establishing SCSI unit attention on
2886  * state change
2887  */
2888 stmf_status_t
2889 stmf_set_lu_access(stmf_lu_t *lu, uint8_t access_state)
2890 {
2891 	stmf_i_lu_t *ilu;
2892 	uint8_t *p1, *p2;
2893 
2894 	if ((access_state != STMF_LU_STANDBY) &&
2895 	    (access_state != STMF_LU_ACTIVE)) {
2896 		return (STMF_INVALID_ARG);
2897 	}
2898 
2899 	p1 = &lu->lu_id->ident[0];
2900 	mutex_enter(&stmf_state.stmf_lock);
2901 	if (stmf_state.stmf_inventory_locked) {
2902 		mutex_exit(&stmf_state.stmf_lock);
2903 		return (STMF_BUSY);
2904 	}
2905 
2906 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2907 		p2 = &ilu->ilu_lu->lu_id->ident[0];
2908 		if (bcmp(p1, p2, 16) == 0) {
2909 			break;
2910 		}
2911 	}
2912 
2913 	if (!ilu) {
2914 		ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2915 	} else {
2916 		/*
2917 		 * We're changing access state on an existing logical unit
2918 		 * Send the proxy registration message for this logical unit
2919 		 * if we're in alua mode.
2920 		 * If the requested state is STMF_LU_ACTIVE, we want to register
2921 		 * this logical unit.
2922 		 * If the requested state is STMF_LU_STANDBY, we're going to
2923 		 * abort all tasks for this logical unit.
2924 		 */
2925 		if (stmf_state.stmf_alua_state == 1 &&
2926 		    access_state == STMF_LU_ACTIVE) {
2927 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
2928 			stmf_ic_msg_t *ic_reg_lun;
2929 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2930 			    lu->lu_lp->lp_alua_support) {
2931 				ilu->ilu_alua = 1;
2932 				/* allocate the register message */
2933 				ic_reg_lun = ic_lun_active_msg_alloc(p1,
2934 				    lu->lu_lp->lp_name,
2935 				    lu->lu_proxy_reg_arg_len,
2936 				    (uint8_t *)lu->lu_proxy_reg_arg,
2937 				    stmf_proxy_msg_id);
2938 				/* send the message */
2939 				if (ic_reg_lun) {
2940 					ic_ret = ic_tx_msg(ic_reg_lun);
2941 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
2942 						stmf_proxy_msg_id++;
2943 					}
2944 				}
2945 			}
2946 		} else if (stmf_state.stmf_alua_state == 1 &&
2947 		    access_state == STMF_LU_STANDBY) {
2948 			/* abort all tasks for this lu */
2949 			stmf_task_lu_killall(lu, NULL, STMF_ABORTED);
2950 		}
2951 	}
2952 
2953 	ilu->ilu_access = access_state;
2954 
2955 	mutex_exit(&stmf_state.stmf_lock);
2956 	return (STMF_SUCCESS);
2957 }
2958 
2959 
2960 stmf_status_t
2961 stmf_register_lu(stmf_lu_t *lu)
2962 {
2963 	stmf_i_lu_t *ilu;
2964 	uint8_t *p1, *p2;
2965 	stmf_state_change_info_t ssci;
2966 	stmf_id_data_t *luid;
2967 
2968 	if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
2969 	    (lu->lu_id->ident_length != 16) ||
2970 	    ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
2971 		return (STMF_INVALID_ARG);
2972 	}
2973 	p1 = &lu->lu_id->ident[0];
2974 	mutex_enter(&stmf_state.stmf_lock);
2975 	if (stmf_state.stmf_inventory_locked) {
2976 		mutex_exit(&stmf_state.stmf_lock);
2977 		return (STMF_BUSY);
2978 	}
2979 
2980 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2981 		p2 = &ilu->ilu_lu->lu_id->ident[0];
2982 		if (bcmp(p1, p2, 16) == 0) {
2983 			mutex_exit(&stmf_state.stmf_lock);
2984 			return (STMF_ALREADY);
2985 		}
2986 	}
2987 
2988 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2989 	luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
2990 	    lu->lu_id->ident_length, lu->lu_id->ident);
2991 	if (luid) {
2992 		luid->id_pt_to_object = (void *)ilu;
2993 		ilu->ilu_luid = luid;
2994 	}
2995 	ilu->ilu_alias = NULL;
2996 
2997 	ilu->ilu_next = stmf_state.stmf_ilulist;
2998 	ilu->ilu_prev = NULL;
2999 	if (ilu->ilu_next)
3000 		ilu->ilu_next->ilu_prev = ilu;
3001 	stmf_state.stmf_ilulist = ilu;
3002 	stmf_state.stmf_nlus++;
3003 	if (lu->lu_lp) {
3004 		((stmf_i_lu_provider_t *)
3005 		    (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
3006 	}
3007 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
3008 	STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
3009 	stmf_create_kstat_lu(ilu);
3010 	/*
3011 	 * register with proxy module if available and logical unit
3012 	 * is in active state
3013 	 */
3014 	if (stmf_state.stmf_alua_state == 1 &&
3015 	    ilu->ilu_access == STMF_LU_ACTIVE) {
3016 		stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3017 		stmf_ic_msg_t *ic_reg_lun;
3018 		if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3019 		    lu->lu_lp->lp_alua_support) {
3020 			ilu->ilu_alua = 1;
3021 			/* allocate the register message */
3022 			ic_reg_lun = ic_reg_lun_msg_alloc(p1,
3023 			    lu->lu_lp->lp_name, lu->lu_proxy_reg_arg_len,
3024 			    (uint8_t *)lu->lu_proxy_reg_arg, stmf_proxy_msg_id);
3025 			/* send the message */
3026 			if (ic_reg_lun) {
3027 				ic_ret = ic_tx_msg(ic_reg_lun);
3028 				if (ic_ret == STMF_IC_MSG_SUCCESS) {
3029 					stmf_proxy_msg_id++;
3030 				}
3031 			}
3032 		}
3033 	}
3034 	mutex_exit(&stmf_state.stmf_lock);
3035 
3036 	/* XXX we should probably check if this lu can be brought online */
3037 	ilu->ilu_prev_state = STMF_STATE_ONLINE;
3038 	if (stmf_state.stmf_service_running) {
3039 		ssci.st_rflags = 0;
3040 		ssci.st_additional_info = NULL;
3041 		(void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
3042 	}
3043 
3044 	/* XXX: Generate event */
3045 	return (STMF_SUCCESS);
3046 }
3047 
3048 stmf_status_t
3049 stmf_deregister_lu(stmf_lu_t *lu)
3050 {
3051 	stmf_i_lu_t *ilu;
3052 
3053 	mutex_enter(&stmf_state.stmf_lock);
3054 	if (stmf_state.stmf_inventory_locked) {
3055 		mutex_exit(&stmf_state.stmf_lock);
3056 		return (STMF_BUSY);
3057 	}
3058 	ilu = stmf_lookup_lu(lu);
3059 	if (ilu == NULL) {
3060 		mutex_exit(&stmf_state.stmf_lock);
3061 		return (STMF_INVALID_ARG);
3062 	}
3063 	if (ilu->ilu_state == STMF_STATE_OFFLINE) {
3064 		ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
3065 		while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
3066 			cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
3067 		}
3068 		if (ilu->ilu_ntasks) {
3069 			stmf_i_scsi_task_t *itask, *nitask;
3070 
3071 			nitask = ilu->ilu_tasks;
3072 			do {
3073 				itask = nitask;
3074 				nitask = itask->itask_lu_next;
3075 				lu->lu_task_free(itask->itask_task);
3076 				stmf_free(itask->itask_task);
3077 			} while (nitask != NULL);
3078 
3079 			ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
3080 			ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
3081 		}
3082 		/* de-register with proxy if available */
3083 		if (ilu->ilu_access == STMF_LU_ACTIVE &&
3084 		    stmf_state.stmf_alua_state == 1) {
3085 			/* de-register with proxy module */
3086 			stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3087 			stmf_ic_msg_t *ic_dereg_lun;
3088 			if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3089 			    lu->lu_lp->lp_alua_support) {
3090 				ilu->ilu_alua = 1;
3091 				/* allocate the de-register message */
3092 				ic_dereg_lun = ic_dereg_lun_msg_alloc(
3093 				    lu->lu_id->ident, lu->lu_lp->lp_name, 0,
3094 				    NULL, stmf_proxy_msg_id);
3095 				/* send the message */
3096 				if (ic_dereg_lun) {
3097 					ic_ret = ic_tx_msg(ic_dereg_lun);
3098 					if (ic_ret == STMF_IC_MSG_SUCCESS) {
3099 						stmf_proxy_msg_id++;
3100 					}
3101 				}
3102 			}
3103 		}
3104 
3105 		if (ilu->ilu_next)
3106 			ilu->ilu_next->ilu_prev = ilu->ilu_prev;
3107 		if (ilu->ilu_prev)
3108 			ilu->ilu_prev->ilu_next = ilu->ilu_next;
3109 		else
3110 			stmf_state.stmf_ilulist = ilu->ilu_next;
3111 		stmf_state.stmf_nlus--;
3112 
3113 		if (ilu == stmf_state.stmf_svc_ilu_draining) {
3114 			stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
3115 		}
3116 		if (ilu == stmf_state.stmf_svc_ilu_timing) {
3117 			stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
3118 		}
3119 		if (lu->lu_lp) {
3120 			((stmf_i_lu_provider_t *)
3121 			    (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
3122 		}
3123 		if (ilu->ilu_luid) {
3124 			((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
3125 			    NULL;
3126 			ilu->ilu_luid = NULL;
3127 		}
3128 		STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
3129 	} else {
3130 		mutex_exit(&stmf_state.stmf_lock);
3131 		return (STMF_BUSY);
3132 	}
3133 	if (ilu->ilu_kstat_info) {
3134 		kmem_free(ilu->ilu_kstat_info->ks_data,
3135 		    ilu->ilu_kstat_info->ks_data_size);
3136 		kstat_delete(ilu->ilu_kstat_info);
3137 	}
3138 	if (ilu->ilu_kstat_io) {
3139 		kstat_delete(ilu->ilu_kstat_io);
3140 		mutex_destroy(&ilu->ilu_kstat_lock);
3141 	}
3142 	stmf_delete_itl_kstat_by_guid(ilu->ilu_ascii_hex_guid);
3143 	mutex_exit(&stmf_state.stmf_lock);
3144 	return (STMF_SUCCESS);
3145 }
3146 
3147 void
3148 stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid)
3149 {
3150 	stmf_i_local_port_t *ilport =
3151 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3152 	ilport->ilport_rtpid = rtpid;
3153 	ilport->ilport_standby = 1;
3154 }
3155 
3156 void
3157 stmf_set_port_alua(stmf_local_port_t *lport)
3158 {
3159 	stmf_i_local_port_t *ilport =
3160 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3161 	ilport->ilport_alua = 1;
3162 }
3163 
3164 stmf_status_t
3165 stmf_register_local_port(stmf_local_port_t *lport)
3166 {
3167 	stmf_i_local_port_t *ilport;
3168 	stmf_state_change_info_t ssci;
3169 	int start_workers = 0;
3170 
3171 	mutex_enter(&stmf_state.stmf_lock);
3172 	if (stmf_state.stmf_inventory_locked) {
3173 		mutex_exit(&stmf_state.stmf_lock);
3174 		return (STMF_BUSY);
3175 	}
3176 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3177 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
3178 
3179 	ilport->ilport_instance =
3180 	    id_alloc_nosleep(stmf_state.stmf_ilport_inst_space);
3181 	if (ilport->ilport_instance == -1) {
3182 		mutex_exit(&stmf_state.stmf_lock);
3183 		return (STMF_FAILURE);
3184 	}
3185 	ilport->ilport_next = stmf_state.stmf_ilportlist;
3186 	ilport->ilport_prev = NULL;
3187 	if (ilport->ilport_next)
3188 		ilport->ilport_next->ilport_prev = ilport;
3189 	stmf_state.stmf_ilportlist = ilport;
3190 	stmf_state.stmf_nlports++;
3191 	if (lport->lport_pp) {
3192 		((stmf_i_port_provider_t *)
3193 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
3194 	}
3195 	ilport->ilport_tg =
3196 	    stmf_lookup_group_for_target(lport->lport_id->ident,
3197 	    lport->lport_id->ident_length);
3198 
3199 	/*
3200 	 * rtpid will/must be set if this is a standby port
3201 	 * only register ports that are not standby (proxy) ports
3202 	 * and ports that are alua participants (ilport_alua == 1)
3203 	 */
3204 	if (ilport->ilport_standby == 0) {
3205 		ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1);
3206 	}
3207 
3208 	if (stmf_state.stmf_alua_state == 1 &&
3209 	    ilport->ilport_standby == 0 &&
3210 	    ilport->ilport_alua == 1) {
3211 		stmf_ic_msg_t *ic_reg_port;
3212 		stmf_ic_msg_status_t ic_ret;
3213 		stmf_local_port_t *lport;
3214 		lport = ilport->ilport_lport;
3215 		ic_reg_port = ic_reg_port_msg_alloc(
3216 		    lport->lport_id, ilport->ilport_rtpid,
3217 		    0, NULL, stmf_proxy_msg_id);
3218 		if (ic_reg_port) {
3219 			ic_ret = ic_tx_msg(ic_reg_port);
3220 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3221 				ilport->ilport_reg_msgid = stmf_proxy_msg_id++;
3222 			} else {
3223 				cmn_err(CE_WARN, "error on port registration "
3224 				"port - %s", ilport->ilport_kstat_tgt_name);
3225 			}
3226 		}
3227 	}
3228 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
3229 	stmf_create_kstat_lport(ilport);
3230 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
3231 		stmf_workers_state = STMF_WORKERS_ENABLING;
3232 		start_workers = 1;
3233 	}
3234 	mutex_exit(&stmf_state.stmf_lock);
3235 
3236 	if (start_workers)
3237 		stmf_worker_init();
3238 
3239 	/* XXX we should probably check if this lport can be brought online */
3240 	ilport->ilport_prev_state = STMF_STATE_ONLINE;
3241 	if (stmf_state.stmf_service_running) {
3242 		ssci.st_rflags = 0;
3243 		ssci.st_additional_info = NULL;
3244 		(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
3245 	}
3246 
3247 	/* XXX: Generate event */
3248 	return (STMF_SUCCESS);
3249 }
3250 
3251 stmf_status_t
3252 stmf_deregister_local_port(stmf_local_port_t *lport)
3253 {
3254 	stmf_i_local_port_t *ilport;
3255 
3256 	mutex_enter(&stmf_state.stmf_lock);
3257 	if (stmf_state.stmf_inventory_locked) {
3258 		mutex_exit(&stmf_state.stmf_lock);
3259 		return (STMF_BUSY);
3260 	}
3261 
3262 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3263 
3264 	/*
3265 	 * deregister ports that are not standby (proxy)
3266 	 */
3267 	if (stmf_state.stmf_alua_state == 1 &&
3268 	    ilport->ilport_standby == 0 &&
3269 	    ilport->ilport_alua == 1) {
3270 		stmf_ic_msg_t *ic_dereg_port;
3271 		stmf_ic_msg_status_t ic_ret;
3272 		ic_dereg_port = ic_dereg_port_msg_alloc(
3273 		    lport->lport_id, 0, NULL, stmf_proxy_msg_id);
3274 		if (ic_dereg_port) {
3275 			ic_ret = ic_tx_msg(ic_dereg_port);
3276 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3277 				stmf_proxy_msg_id++;
3278 			}
3279 		}
3280 	}
3281 
3282 	if (ilport->ilport_nsessions == 0) {
3283 		if (ilport->ilport_next)
3284 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
3285 		if (ilport->ilport_prev)
3286 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
3287 		else
3288 			stmf_state.stmf_ilportlist = ilport->ilport_next;
3289 		id_free(stmf_state.stmf_ilport_inst_space,
3290 		    ilport->ilport_instance);
3291 		rw_destroy(&ilport->ilport_lock);
3292 		stmf_state.stmf_nlports--;
3293 		if (lport->lport_pp) {
3294 			((stmf_i_port_provider_t *)
3295 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
3296 		}
3297 		ilport->ilport_tg = NULL;
3298 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
3299 	} else {
3300 		mutex_exit(&stmf_state.stmf_lock);
3301 		return (STMF_BUSY);
3302 	}
3303 	if (ilport->ilport_kstat_info) {
3304 		kmem_free(ilport->ilport_kstat_info->ks_data,
3305 		    ilport->ilport_kstat_info->ks_data_size);
3306 		kstat_delete(ilport->ilport_kstat_info);
3307 	}
3308 	if (ilport->ilport_kstat_io) {
3309 		kstat_delete(ilport->ilport_kstat_io);
3310 		mutex_destroy(&ilport->ilport_kstat_lock);
3311 	}
3312 	stmf_delete_itl_kstat_by_lport(ilport->ilport_kstat_tgt_name);
3313 	mutex_exit(&stmf_state.stmf_lock);
3314 	return (STMF_SUCCESS);
3315 }
3316 
3317 /*
3318  * Rport id/instance mappings remain valid until STMF is unloaded
3319  */
3320 static int
3321 stmf_irport_compare(const void *void_irport1, const void *void_irport2)
3322 {
3323 	const	stmf_i_remote_port_t	*irport1 = void_irport1;
3324 	const	stmf_i_remote_port_t	*irport2 = void_irport2;
3325 	int			result;
3326 
3327 	/* Sort by code set then ident */
3328 	if (irport1->irport_id->code_set <
3329 	    irport2->irport_id->code_set) {
3330 		return (-1);
3331 	} else if (irport1->irport_id->code_set >
3332 	    irport2->irport_id->code_set) {
3333 		return (1);
3334 	}
3335 
3336 	/* Next by ident length */
3337 	if (irport1->irport_id->ident_length <
3338 	    irport2->irport_id->ident_length) {
3339 		return (-1);
3340 	} else if (irport1->irport_id->ident_length >
3341 	    irport2->irport_id->ident_length) {
3342 		return (1);
3343 	}
3344 
3345 	/* Code set and ident length both match, now compare idents */
3346 	result = memcmp(irport1->irport_id->ident,
3347 	    irport2->irport_id->ident,
3348 	    irport1->irport_id->ident_length);
3349 
3350 	if (result < 0) {
3351 		return (-1);
3352 	} else if (result > 0) {
3353 		return (1);
3354 	}
3355 
3356 	return (0);
3357 }
3358 
3359 static stmf_i_remote_port_t *
3360 stmf_irport_create(scsi_devid_desc_t *rport_devid)
3361 {
3362 	int			alloc_len;
3363 	stmf_i_remote_port_t	*irport;
3364 
3365 	/*
3366 	 * Lookup will bump the refcnt if there's an existing rport
3367 	 * context for this identifier.
3368 	 */
3369 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3370 
3371 	alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3372 	    rport_devid->ident_length - 1;
3373 	irport = kmem_zalloc(alloc_len, KM_NOSLEEP);
3374 	if (irport == NULL) {
3375 		return (NULL);
3376 	}
3377 
3378 	irport->irport_instance =
3379 	    id_alloc_nosleep(stmf_state.stmf_irport_inst_space);
3380 	if (irport->irport_instance == -1) {
3381 		kmem_free(irport, alloc_len);
3382 		return (NULL);
3383 	}
3384 
3385 	irport->irport_id =
3386 	    (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */
3387 	bcopy(rport_devid, irport->irport_id,
3388 	    sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1);
3389 	irport->irport_refcnt = 1;
3390 	mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL);
3391 
3392 	return (irport);
3393 }
3394 
3395 static void
3396 stmf_irport_destroy(stmf_i_remote_port_t *irport)
3397 {
3398 	id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance);
3399 	mutex_destroy(&irport->irport_mutex);
3400 	kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3401 	    irport->irport_id->ident_length - 1);
3402 }
3403 
3404 static stmf_i_remote_port_t *
3405 stmf_irport_register(scsi_devid_desc_t *rport_devid)
3406 {
3407 	stmf_i_remote_port_t	*irport;
3408 
3409 	mutex_enter(&stmf_state.stmf_lock);
3410 
3411 	/*
3412 	 * Lookup will bump the refcnt if there's an existing rport
3413 	 * context for this identifier.
3414 	 */
3415 	if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) {
3416 		mutex_exit(&stmf_state.stmf_lock);
3417 		return (irport);
3418 	}
3419 
3420 	irport = stmf_irport_create(rport_devid);
3421 	if (irport == NULL) {
3422 		mutex_exit(&stmf_state.stmf_lock);
3423 		return (NULL);
3424 	}
3425 
3426 	avl_add(&stmf_state.stmf_irportlist, irport);
3427 	mutex_exit(&stmf_state.stmf_lock);
3428 
3429 	return (irport);
3430 }
3431 
3432 static stmf_i_remote_port_t *
3433 stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid)
3434 {
3435 	stmf_i_remote_port_t	*irport;
3436 	stmf_i_remote_port_t	tmp_irport;
3437 
3438 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3439 	tmp_irport.irport_id = rport_devid;
3440 	irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL);
3441 	if (irport != NULL) {
3442 		mutex_enter(&irport->irport_mutex);
3443 		irport->irport_refcnt++;
3444 		mutex_exit(&irport->irport_mutex);
3445 	}
3446 
3447 	return (irport);
3448 }
3449 
3450 static void
3451 stmf_irport_deregister(stmf_i_remote_port_t *irport)
3452 {
3453 	/*
3454 	 * If we were actually going to remove unreferenced remote ports
3455 	 * we would want to acquire stmf_state.stmf_lock before getting
3456 	 * the irport mutex.
3457 	 *
3458 	 * Instead we're just going to leave it there even if unreferenced.
3459 	 */
3460 	mutex_enter(&irport->irport_mutex);
3461 	irport->irport_refcnt--;
3462 	mutex_exit(&irport->irport_mutex);
3463 }
3464 
3465 /*
3466  * Port provider has to make sure that register/deregister session and
3467  * port are serialized calls.
3468  */
3469 stmf_status_t
3470 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3471 {
3472 	stmf_i_scsi_session_t *iss;
3473 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3474 	    lport->lport_stmf_private;
3475 	uint8_t		lun[8];
3476 
3477 	/*
3478 	 * Port state has to be online to register a scsi session. It is
3479 	 * possible that we started an offline operation and a new SCSI
3480 	 * session started at the same time (in that case also we are going
3481 	 * to fail the registeration). But any other state is simply
3482 	 * a bad port provider implementation.
3483 	 */
3484 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
3485 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
3486 			stmf_trace(lport->lport_alias, "Port is trying to "
3487 			    "register a session while the state is neither "
3488 			    "online nor offlining");
3489 		}
3490 		return (STMF_FAILURE);
3491 	}
3492 	bzero(lun, 8);
3493 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3494 	if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) {
3495 		stmf_trace(lport->lport_alias, "Could not register "
3496 		    "remote port during session registration");
3497 		return (STMF_FAILURE);
3498 	}
3499 
3500 	iss->iss_flags |= ISS_BEING_CREATED;
3501 
3502 	/* sessions use the ilport_lock. No separate lock is required */
3503 	iss->iss_lockp = &ilport->ilport_lock;
3504 
3505 	if (iss->iss_sm != NULL)
3506 		cmn_err(CE_PANIC, "create lun map called with non NULL map");
3507 	iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t),
3508 	    KM_SLEEP);
3509 
3510 	mutex_enter(&stmf_state.stmf_lock);
3511 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3512 	(void) stmf_session_create_lun_map(ilport, iss);
3513 	ilport->ilport_nsessions++;
3514 	iss->iss_next = ilport->ilport_ss_list;
3515 	ilport->ilport_ss_list = iss;
3516 	rw_exit(&ilport->ilport_lock);
3517 	mutex_exit(&stmf_state.stmf_lock);
3518 
3519 	iss->iss_creation_time = ddi_get_time();
3520 	ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1);
3521 	iss->iss_flags &= ~ISS_BEING_CREATED;
3522 	/* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */
3523 	iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED;
3524 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
3525 	    stmf_scsi_session_t *, ss);
3526 	return (STMF_SUCCESS);
3527 }
3528 
3529 void
3530 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3531 {
3532 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3533 	    lport->lport_stmf_private;
3534 	stmf_i_scsi_session_t *iss, **ppss;
3535 	int found = 0;
3536 	stmf_ic_msg_t *ic_session_dereg;
3537 	stmf_status_t ic_ret = STMF_FAILURE;
3538 
3539 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
3540 	    stmf_scsi_session_t *, ss);
3541 
3542 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3543 	if (ss->ss_rport_alias) {
3544 		ss->ss_rport_alias = NULL;
3545 	}
3546 
3547 try_dereg_ss_again:
3548 	mutex_enter(&stmf_state.stmf_lock);
3549 	atomic_and_32(&iss->iss_flags,
3550 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
3551 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
3552 		mutex_exit(&stmf_state.stmf_lock);
3553 		delay(1);
3554 		goto try_dereg_ss_again;
3555 	}
3556 
3557 	/* dereg proxy session if not standby port */
3558 	if (stmf_state.stmf_alua_state == 1 &&
3559 	    ilport->ilport_standby == 0 &&
3560 	    ilport->ilport_alua == 1) {
3561 		ic_session_dereg = ic_session_dereg_msg_alloc(
3562 		    ss, stmf_proxy_msg_id);
3563 		if (ic_session_dereg) {
3564 			ic_ret = ic_tx_msg(ic_session_dereg);
3565 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3566 				stmf_proxy_msg_id++;
3567 			}
3568 		}
3569 	}
3570 
3571 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3572 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
3573 	    ppss = &((*ppss)->iss_next)) {
3574 		if (iss == (*ppss)) {
3575 			*ppss = (*ppss)->iss_next;
3576 			found = 1;
3577 			break;
3578 		}
3579 	}
3580 	if (!found) {
3581 		cmn_err(CE_PANIC, "Deregister session called for non existent"
3582 		    " session");
3583 	}
3584 	ilport->ilport_nsessions--;
3585 
3586 	stmf_irport_deregister(iss->iss_irport);
3587 	(void) stmf_session_destroy_lun_map(ilport, iss);
3588 	rw_exit(&ilport->ilport_lock);
3589 	mutex_exit(&stmf_state.stmf_lock);
3590 }
3591 
3592 stmf_i_scsi_session_t *
3593 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
3594 {
3595 	stmf_i_local_port_t *ilport;
3596 	stmf_i_scsi_session_t *iss;
3597 
3598 	mutex_enter(&stmf_state.stmf_lock);
3599 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3600 	    ilport = ilport->ilport_next) {
3601 		rw_enter(&ilport->ilport_lock, RW_WRITER);
3602 		for (iss = ilport->ilport_ss_list; iss != NULL;
3603 		    iss = iss->iss_next) {
3604 			if (iss->iss_ss->ss_session_id == session_id) {
3605 				if (!stay_locked)
3606 					rw_exit(&ilport->ilport_lock);
3607 				mutex_exit(&stmf_state.stmf_lock);
3608 				return (iss);
3609 			}
3610 		}
3611 		rw_exit(&ilport->ilport_lock);
3612 	}
3613 	mutex_exit(&stmf_state.stmf_lock);
3614 	return (NULL);
3615 }
3616 
3617 #define	MAX_ALIAS		128
3618 
3619 static int
3620 stmf_itl_kstat_compare(const void *itl_kstat_1, const void *itl_kstat_2)
3621 {
3622 	const	stmf_i_itl_kstat_t	*kstat_nm1 = itl_kstat_1;
3623 	const	stmf_i_itl_kstat_t	*kstat_nm2 = itl_kstat_2;
3624 	int	ret;
3625 
3626 	ret = strcmp(kstat_nm1->iitl_kstat_nm, kstat_nm2->iitl_kstat_nm);
3627 	if (ret < 0) {
3628 		return (-1);
3629 	} else if (ret > 0) {
3630 		return (1);
3631 	}
3632 	return (0);
3633 }
3634 
3635 static stmf_i_itl_kstat_t *
3636 stmf_itl_kstat_lookup(char *kstat_nm)
3637 {
3638 	stmf_i_itl_kstat_t	tmp;
3639 	stmf_i_itl_kstat_t	*itl_kstat;
3640 
3641 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3642 	(void) strcpy(tmp.iitl_kstat_nm, kstat_nm);
3643 	itl_kstat = avl_find(&stmf_state.stmf_itl_kstat_list, &tmp, NULL);
3644 	return (itl_kstat);
3645 }
3646 
3647 static void
3648 stmf_delete_itl_kstat_by_lport(char *tgt)
3649 {
3650 	stmf_i_itl_kstat_t	*ks_itl, *next;
3651 
3652 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3653 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
3654 	for (; ks_itl != NULL; ks_itl = next) {
3655 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
3656 		if (strcmp(ks_itl->iitl_kstat_lport, tgt) == 0) {
3657 			stmf_teardown_itl_kstats(ks_itl);
3658 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
3659 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
3660 		}
3661 	}
3662 }
3663 
3664 static void
3665 stmf_delete_itl_kstat_by_guid(char *guid)
3666 {
3667 	stmf_i_itl_kstat_t	*ks_itl, *next;
3668 
3669 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3670 	ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list);
3671 	for (; ks_itl != NULL; ks_itl = next) {
3672 		next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl);
3673 		if (strcmp(ks_itl->iitl_kstat_guid, guid) == 0) {
3674 			stmf_teardown_itl_kstats(ks_itl);
3675 			avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl);
3676 			kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t));
3677 		}
3678 	}
3679 }
3680 
3681 static stmf_i_itl_kstat_t *
3682 stmf_itl_kstat_create(stmf_itl_data_t *itl, char *nm,
3683     scsi_devid_desc_t *lport, scsi_devid_desc_t *lun)
3684 {
3685 	stmf_i_itl_kstat_t	*ks_itl;
3686 	int			i, len;
3687 
3688 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3689 	if ((ks_itl = stmf_itl_kstat_lookup(nm)) != NULL)
3690 		return (ks_itl);
3691 
3692 	len = sizeof (stmf_i_itl_kstat_t);
3693 	ks_itl = kmem_zalloc(len, KM_NOSLEEP);
3694 	if (ks_itl == NULL)
3695 		return (NULL);
3696 
3697 	(void) strcpy(ks_itl->iitl_kstat_nm, nm);
3698 	bcopy(lport->ident, ks_itl->iitl_kstat_lport, lport->ident_length);
3699 	ks_itl->iitl_kstat_lport[lport->ident_length] = '\0';
3700 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
3701 		(void) sprintf(&ks_itl->iitl_kstat_guid[i * 2], "%02x",
3702 		    lun->ident[i]);
3703 	}
3704 	ks_itl->iitl_kstat_strbuf = itl->itl_kstat_strbuf;
3705 	ks_itl->iitl_kstat_strbuflen = itl->itl_kstat_strbuflen;
3706 	ks_itl->iitl_kstat_info = itl->itl_kstat_info;
3707 	ks_itl->iitl_kstat_taskq = itl->itl_kstat_taskq;
3708 	ks_itl->iitl_kstat_lu_xfer = itl->itl_kstat_lu_xfer;
3709 	ks_itl->iitl_kstat_lport_xfer = itl->itl_kstat_lport_xfer;
3710 	avl_add(&stmf_state.stmf_itl_kstat_list, ks_itl);
3711 
3712 	return (ks_itl);
3713 }
3714 
3715 stmf_status_t
3716 stmf_setup_itl_kstats(stmf_itl_data_t *itl)
3717 {
3718 	char				ks_itl_id[32];
3719 	char				ks_nm[KSTAT_STRLEN];
3720 	char				ks_itl_nm[KSTAT_STRLEN];
3721 	stmf_kstat_itl_info_t		*ks_itl;
3722 	stmf_scsi_session_t		*ss;
3723 	stmf_i_scsi_session_t		*iss;
3724 	stmf_i_local_port_t		*ilport;
3725 	char				*strbuf;
3726 	int				id, len, i;
3727 	char				*rport_alias;
3728 	char				*lport_alias;
3729 	char				*lu_alias;
3730 	stmf_i_itl_kstat_t		*tmp_kstat;
3731 
3732 	/*
3733 	 * Allocate enough memory in the ITL to hold the relevant
3734 	 * identifiers.
3735 	 * rport and lport identifiers come from the stmf_scsi_session_t.
3736 	 * ident might not be null terminated.
3737 	 */
3738 	ss = itl->itl_session->iss_ss;
3739 	iss = ss->ss_stmf_private;
3740 	ilport = ss->ss_lport->lport_stmf_private;
3741 	(void) snprintf(ks_itl_id, 32, "%d.%d.%d",
3742 	    iss->iss_irport->irport_instance, ilport->ilport_instance,
3743 	    itl->itl_lun);
3744 
3745 	(void) snprintf(ks_itl_nm, KSTAT_STRLEN, "itl_%s", ks_itl_id);
3746 	/*
3747 	 * let's verify this itl_kstat already exist
3748 	 */
3749 	if ((tmp_kstat = stmf_itl_kstat_lookup(ks_itl_nm)) != NULL) {
3750 		itl->itl_kstat_strbuf = tmp_kstat->iitl_kstat_strbuf;
3751 		itl->itl_kstat_strbuflen = tmp_kstat->iitl_kstat_strbuflen;
3752 		itl->itl_kstat_info = tmp_kstat->iitl_kstat_info;
3753 		itl->itl_kstat_taskq = tmp_kstat->iitl_kstat_taskq;
3754 		itl->itl_kstat_lu_xfer = tmp_kstat->iitl_kstat_lu_xfer;
3755 		itl->itl_kstat_lport_xfer = tmp_kstat->iitl_kstat_lport_xfer;
3756 		return (STMF_SUCCESS);
3757 	}
3758 
3759 	/* New itl_kstat */
3760 	rport_alias = (ss->ss_rport_alias == NULL) ?
3761 	    "" : ss->ss_rport_alias;
3762 	lport_alias = (ss->ss_lport->lport_alias == NULL) ?
3763 	    "" : ss->ss_lport->lport_alias;
3764 	lu_alias = (itl->itl_ilu->ilu_lu->lu_alias == NULL) ?
3765 	    "" : itl->itl_ilu->ilu_lu->lu_alias;
3766 
3767 	itl->itl_kstat_strbuflen = (ss->ss_rport_id->ident_length + 1) +
3768 	    (strnlen(rport_alias, MAX_ALIAS) + 1) +
3769 	    (ss->ss_lport->lport_id->ident_length + 1) +
3770 	    (strnlen(lport_alias, MAX_ALIAS) + 1) +
3771 	    (STMF_GUID_INPUT + 1) +
3772 	    (strnlen(lu_alias, MAX_ALIAS) + 1) +
3773 	    MAX_PROTO_STR_LEN;
3774 	itl->itl_kstat_strbuf = kmem_zalloc(itl->itl_kstat_strbuflen,
3775 	    KM_NOSLEEP);
3776 	if (itl->itl_kstat_strbuf == NULL) {
3777 		return (STMF_ALLOC_FAILURE);
3778 	}
3779 
3780 	ks_itl = (stmf_kstat_itl_info_t *)kmem_zalloc(sizeof (*ks_itl),
3781 	    KM_NOSLEEP);
3782 	if (ks_itl == NULL) {
3783 		kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
3784 		return (STMF_ALLOC_FAILURE);
3785 	}
3786 
3787 	if ((itl->itl_kstat_info = kstat_create(STMF_MODULE_NAME,
3788 	    0, ks_itl_nm, "misc", KSTAT_TYPE_NAMED,
3789 	    sizeof (stmf_kstat_itl_info_t) / sizeof (kstat_named_t),
3790 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
3791 		goto itl_kstat_cleanup;
3792 	}
3793 
3794 	itl->itl_kstat_info->ks_data_size += itl->itl_kstat_strbuflen;
3795 	itl->itl_kstat_info->ks_data = ks_itl;
3796 
3797 	kstat_named_init(&ks_itl->i_rport_name, "rport-name",
3798 	    KSTAT_DATA_STRING);
3799 	kstat_named_init(&ks_itl->i_rport_alias, "rport-alias",
3800 	    KSTAT_DATA_STRING);
3801 	kstat_named_init(&ks_itl->i_lport_name, "lport-name",
3802 	    KSTAT_DATA_STRING);
3803 	kstat_named_init(&ks_itl->i_lport_alias, "lport-alias",
3804 	    KSTAT_DATA_STRING);
3805 	kstat_named_init(&ks_itl->i_protocol, "protocol",
3806 	    KSTAT_DATA_STRING);
3807 	kstat_named_init(&ks_itl->i_lu_guid, "lu-guid",
3808 	    KSTAT_DATA_STRING);
3809 	kstat_named_init(&ks_itl->i_lu_alias, "lu-alias",
3810 	    KSTAT_DATA_STRING);
3811 	kstat_named_init(&ks_itl->i_lu_number, "lu-number",
3812 	    KSTAT_DATA_UINT64);
3813 	kstat_named_init(&ks_itl->i_task_waitq_elapsed, "task-waitq-elapsed",
3814 	    KSTAT_DATA_UINT64);
3815 	kstat_named_init(&ks_itl->i_task_read_elapsed, "task-read-elapsed",
3816 	    KSTAT_DATA_UINT64);
3817 	kstat_named_init(&ks_itl->i_task_write_elapsed, "task-write-elapsed",
3818 	    KSTAT_DATA_UINT64);
3819 	kstat_named_init(&ks_itl->i_lu_read_elapsed, "lu-read-elapsed",
3820 	    KSTAT_DATA_UINT64);
3821 	kstat_named_init(&ks_itl->i_lu_write_elapsed, "lu-write-elapsed",
3822 	    KSTAT_DATA_UINT64);
3823 	kstat_named_init(&ks_itl->i_lport_read_elapsed, "lport-read-elapsed",
3824 	    KSTAT_DATA_UINT64);
3825 	kstat_named_init(&ks_itl->i_lport_write_elapsed, "lport-write-elapsed",
3826 	    KSTAT_DATA_UINT64);
3827 
3828 	strbuf = itl->itl_kstat_strbuf;
3829 
3830 	/* Rport */
3831 	len = ss->ss_rport_id->ident_length;
3832 	bcopy(ss->ss_rport_id->ident, strbuf, len);
3833 	strbuf += len;
3834 	*strbuf = '\0';
3835 	kstat_named_setstr(&ks_itl->i_rport_name, strbuf - len);
3836 	strbuf++;
3837 
3838 	len = strnlen(rport_alias, MAX_ALIAS);
3839 	(void) strncpy(strbuf, rport_alias, len + 1);
3840 	kstat_named_setstr(&ks_itl->i_rport_alias, strbuf);
3841 	strbuf += len + 1;
3842 
3843 	/* Lport */
3844 	len = ss->ss_lport->lport_id->ident_length;
3845 	bcopy(ss->ss_lport->lport_id->ident, strbuf, len);
3846 	strbuf += len;
3847 	*strbuf = '\0';
3848 	kstat_named_setstr(&ks_itl->i_lport_name, strbuf - len);
3849 	strbuf++;
3850 
3851 	len = strnlen(lport_alias, MAX_ALIAS);
3852 	(void) strncpy(strbuf, lport_alias, len + 1);
3853 	kstat_named_setstr(&ks_itl->i_lport_alias, strbuf);
3854 	strbuf += len + 1;
3855 
3856 	id = (ss->ss_lport->lport_id->protocol_id > PROTOCOL_ANY) ?
3857 	    PROTOCOL_ANY : ss->ss_lport->lport_id->protocol_id;
3858 	kstat_named_setstr(&ks_itl->i_protocol, protocol_ident[id]);
3859 
3860 	/* LU */
3861 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
3862 		(void) sprintf(&strbuf[i * 2], "%02x",
3863 		    itl->itl_ilu->ilu_lu->lu_id->ident[i]);
3864 	}
3865 	kstat_named_setstr(&ks_itl->i_lu_guid, strbuf);
3866 	strbuf += STMF_GUID_INPUT + 1;
3867 
3868 	len = strnlen(lu_alias, MAX_ALIAS);
3869 	(void) strncpy(strbuf, lu_alias, len + 1);
3870 	kstat_named_setstr(&ks_itl->i_lu_alias, strbuf);
3871 	strbuf += len + 1;
3872 
3873 	ks_itl->i_lu_number.value.ui64 = itl->itl_lun;
3874 
3875 	/* Now create the I/O kstats */
3876 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_tasks_%s",  ks_itl_id);
3877 	if ((itl->itl_kstat_taskq = kstat_create(STMF_MODULE_NAME, 0,
3878 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3879 		goto itl_kstat_cleanup;
3880 	}
3881 
3882 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lu_%s",  ks_itl_id);
3883 	if ((itl->itl_kstat_lu_xfer = kstat_create(STMF_MODULE_NAME, 0,
3884 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3885 		goto itl_kstat_cleanup;
3886 	}
3887 
3888 	(void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lport_%s",  ks_itl_id);
3889 	if ((itl->itl_kstat_lport_xfer = kstat_create(STMF_MODULE_NAME, 0,
3890 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
3891 		goto itl_kstat_cleanup;
3892 	}
3893 
3894 	/* Install all the kstats */
3895 	kstat_install(itl->itl_kstat_info);
3896 	kstat_install(itl->itl_kstat_taskq);
3897 	kstat_install(itl->itl_kstat_lu_xfer);
3898 	kstat_install(itl->itl_kstat_lport_xfer);
3899 
3900 	/* Add new itl_kstat to stmf_itl_kstat_list */
3901 	if (stmf_itl_kstat_create(itl, ks_itl_nm, ss->ss_lport->lport_id,
3902 	    itl->itl_ilu->ilu_lu->lu_id) != NULL)
3903 		return (STMF_SUCCESS);
3904 
3905 itl_kstat_cleanup:
3906 	if (itl->itl_kstat_taskq)
3907 		kstat_delete(itl->itl_kstat_taskq);
3908 	if (itl->itl_kstat_lu_xfer)
3909 		kstat_delete(itl->itl_kstat_lu_xfer);
3910 	if (itl->itl_kstat_lport_xfer)
3911 		kstat_delete(itl->itl_kstat_lport_xfer);
3912 	if (itl->itl_kstat_info)
3913 		kstat_delete(itl->itl_kstat_info);
3914 	kmem_free(ks_itl, sizeof (*ks_itl));
3915 	kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen);
3916 	cmn_err(CE_WARN, "STMF: kstat_create itl failed");
3917 	return (STMF_ALLOC_FAILURE);
3918 }
3919 
3920 static void
3921 stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks)
3922 {
3923 	kstat_delete(ks->iitl_kstat_lport_xfer);
3924 	kstat_delete(ks->iitl_kstat_lu_xfer);
3925 	kstat_delete(ks->iitl_kstat_taskq);
3926 	kmem_free(ks->iitl_kstat_info->ks_data, sizeof (stmf_kstat_itl_info_t));
3927 	kstat_delete(ks->iitl_kstat_info);
3928 	kmem_free(ks->iitl_kstat_strbuf, ks->iitl_kstat_strbuflen);
3929 }
3930 
3931 void
3932 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
3933 {
3934 	stmf_itl_data_t **itlpp;
3935 	stmf_i_lu_t *ilu;
3936 
3937 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
3938 
3939 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3940 	mutex_enter(&ilu->ilu_task_lock);
3941 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
3942 	    itlpp = &(*itlpp)->itl_next) {
3943 		if ((*itlpp) == itl)
3944 			break;
3945 	}
3946 	ASSERT((*itlpp) != NULL);
3947 	*itlpp = itl->itl_next;
3948 	mutex_exit(&ilu->ilu_task_lock);
3949 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
3950 	    (uint32_t)itl->itl_hdlrm_reason);
3951 
3952 	kmem_free(itl, sizeof (*itl));
3953 }
3954 
3955 stmf_status_t
3956 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
3957     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
3958 {
3959 	stmf_itl_data_t *itl;
3960 	stmf_i_scsi_session_t *iss;
3961 	stmf_lun_map_ent_t *lun_map_ent;
3962 	stmf_i_lu_t *ilu;
3963 	uint16_t n;
3964 
3965 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3966 	if (ss == NULL) {
3967 		iss = stmf_session_id_to_issptr(session_id, 1);
3968 		if (iss == NULL)
3969 			return (STMF_NOT_FOUND);
3970 	} else {
3971 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3972 	}
3973 
3974 	/*
3975 	 * Acquire stmf_lock for stmf_itl_kstat_lookup.
3976 	 */
3977 	mutex_enter(&stmf_state.stmf_lock);
3978 	rw_enter(iss->iss_lockp, RW_WRITER);
3979 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
3980 	lun_map_ent = (stmf_lun_map_ent_t *)
3981 	    stmf_get_ent_from_map(iss->iss_sm, n);
3982 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
3983 		rw_exit(iss->iss_lockp);
3984 		mutex_exit(&stmf_state.stmf_lock);
3985 		return (STMF_NOT_FOUND);
3986 	}
3987 	if (lun_map_ent->ent_itl_datap != NULL) {
3988 		rw_exit(iss->iss_lockp);
3989 		mutex_exit(&stmf_state.stmf_lock);
3990 		return (STMF_ALREADY);
3991 	}
3992 
3993 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
3994 	if (itl == NULL) {
3995 		rw_exit(iss->iss_lockp);
3996 		mutex_exit(&stmf_state.stmf_lock);
3997 		return (STMF_ALLOC_FAILURE);
3998 	}
3999 
4000 	itl->itl_ilu = ilu;
4001 	itl->itl_session = iss;
4002 	itl->itl_counter = 1;
4003 	itl->itl_lun = n;
4004 	itl->itl_handle = itl_handle;
4005 
4006 	if (stmf_setup_itl_kstats(itl) != STMF_SUCCESS) {
4007 		kmem_free(itl, sizeof (*itl));
4008 		rw_exit(iss->iss_lockp);
4009 		mutex_exit(&stmf_state.stmf_lock);
4010 		return (STMF_ALLOC_FAILURE);
4011 	}
4012 
4013 	mutex_enter(&ilu->ilu_task_lock);
4014 	itl->itl_next = ilu->ilu_itl_list;
4015 	ilu->ilu_itl_list = itl;
4016 	mutex_exit(&ilu->ilu_task_lock);
4017 	lun_map_ent->ent_itl_datap = itl;
4018 	rw_exit(iss->iss_lockp);
4019 	mutex_exit(&stmf_state.stmf_lock);
4020 
4021 	return (STMF_SUCCESS);
4022 }
4023 
4024 void
4025 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
4026 {
4027 	uint8_t old, new;
4028 
4029 	do {
4030 		old = new = itl->itl_flags;
4031 		if (old & STMF_ITL_BEING_TERMINATED)
4032 			return;
4033 		new |= STMF_ITL_BEING_TERMINATED;
4034 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
4035 	itl->itl_hdlrm_reason = hdlrm_reason;
4036 
4037 	ASSERT(itl->itl_counter);
4038 
4039 	if (atomic_add_32_nv(&itl->itl_counter, -1))
4040 		return;
4041 
4042 	drv_usecwait(10);
4043 	if (itl->itl_counter)
4044 		return;
4045 
4046 	stmf_release_itl_handle(lu, itl);
4047 }
4048 
4049 stmf_status_t
4050 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
4051 {
4052 	stmf_i_lu_t *ilu;
4053 	stmf_i_local_port_t *ilport;
4054 	stmf_i_scsi_session_t *iss;
4055 	stmf_lun_map_t *lm;
4056 	stmf_lun_map_ent_t *ent;
4057 	uint32_t nmaps, nu;
4058 	stmf_itl_data_t **itl_list;
4059 	int i;
4060 
4061 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4062 
4063 dereg_itl_start:;
4064 	nmaps = ilu->ilu_ref_cnt;
4065 	if (nmaps == 0)
4066 		return (STMF_NOT_FOUND);
4067 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
4068 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
4069 	mutex_enter(&stmf_state.stmf_lock);
4070 	if (nmaps != ilu->ilu_ref_cnt) {
4071 		/* Something changed, start all over */
4072 		mutex_exit(&stmf_state.stmf_lock);
4073 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
4074 		goto dereg_itl_start;
4075 	}
4076 	nu = 0;
4077 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
4078 	    ilport = ilport->ilport_next) {
4079 		rw_enter(&ilport->ilport_lock, RW_WRITER);
4080 		for (iss = ilport->ilport_ss_list; iss != NULL;
4081 		    iss = iss->iss_next) {
4082 			lm = iss->iss_sm;
4083 			if (!lm)
4084 				continue;
4085 			for (i = 0; i < lm->lm_nentries; i++) {
4086 				if (lm->lm_plus[i] == NULL)
4087 					continue;
4088 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4089 				if ((ent->ent_lu == lu) &&
4090 				    (ent->ent_itl_datap)) {
4091 					itl_list[nu++] = ent->ent_itl_datap;
4092 					ent->ent_itl_datap = NULL;
4093 					if (nu == nmaps) {
4094 						rw_exit(&ilport->ilport_lock);
4095 						goto dai_scan_done;
4096 					}
4097 				}
4098 			} /* lun table for a session */
4099 		} /* sessions */
4100 		rw_exit(&ilport->ilport_lock);
4101 	} /* ports */
4102 
4103 dai_scan_done:
4104 	mutex_exit(&stmf_state.stmf_lock);
4105 
4106 	for (i = 0; i < nu; i++) {
4107 		stmf_do_itl_dereg(lu, itl_list[i],
4108 		    STMF_ITL_REASON_DEREG_REQUEST);
4109 	}
4110 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
4111 
4112 	return (STMF_SUCCESS);
4113 }
4114 
4115 stmf_status_t
4116 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun,
4117     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
4118 {
4119 	stmf_i_scsi_session_t *iss;
4120 	stmf_itl_data_t *itl;
4121 	stmf_lun_map_ent_t *ent;
4122 	stmf_lun_map_t *lm;
4123 	int i;
4124 	uint16_t n;
4125 
4126 	if (ss == NULL) {
4127 		if (session_id == STMF_SESSION_ID_NONE)
4128 			return (STMF_INVALID_ARG);
4129 		iss = stmf_session_id_to_issptr(session_id, 1);
4130 		if (iss == NULL)
4131 			return (STMF_NOT_FOUND);
4132 	} else {
4133 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4134 		rw_enter(iss->iss_lockp, RW_WRITER);
4135 	}
4136 	lm = iss->iss_sm;
4137 	if (lm == NULL) {
4138 		rw_exit(iss->iss_lockp);
4139 		return (STMF_NOT_FOUND);
4140 	}
4141 
4142 	if (lun) {
4143 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4144 		ent = (stmf_lun_map_ent_t *)
4145 		    stmf_get_ent_from_map(iss->iss_sm, n);
4146 	} else {
4147 		if (itl_handle == NULL) {
4148 			rw_exit(iss->iss_lockp);
4149 			return (STMF_INVALID_ARG);
4150 		}
4151 		ent = NULL;
4152 		for (i = 0; i < lm->lm_nentries; i++) {
4153 			if (lm->lm_plus[i] == NULL)
4154 				continue;
4155 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4156 			if (ent->ent_itl_datap &&
4157 			    (ent->ent_itl_datap->itl_handle == itl_handle)) {
4158 				break;
4159 			}
4160 		}
4161 	}
4162 	if ((ent == NULL) || (ent->ent_lu != lu) ||
4163 	    (ent->ent_itl_datap == NULL)) {
4164 		rw_exit(iss->iss_lockp);
4165 		return (STMF_NOT_FOUND);
4166 	}
4167 	itl = ent->ent_itl_datap;
4168 	ent->ent_itl_datap = NULL;
4169 	rw_exit(iss->iss_lockp);
4170 	stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST);
4171 
4172 	return (STMF_SUCCESS);
4173 }
4174 
4175 stmf_status_t
4176 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
4177     uint64_t session_id, void **itl_handle_retp)
4178 {
4179 	stmf_i_scsi_session_t *iss;
4180 	stmf_lun_map_ent_t *ent;
4181 	stmf_lun_map_t *lm;
4182 	stmf_status_t ret;
4183 	int i;
4184 	uint16_t n;
4185 
4186 	if (ss == NULL) {
4187 		iss = stmf_session_id_to_issptr(session_id, 1);
4188 		if (iss == NULL)
4189 			return (STMF_NOT_FOUND);
4190 	} else {
4191 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4192 		rw_enter(iss->iss_lockp, RW_WRITER);
4193 	}
4194 
4195 	ent = NULL;
4196 	if (lun == NULL) {
4197 		lm = iss->iss_sm;
4198 		for (i = 0; i < lm->lm_nentries; i++) {
4199 			if (lm->lm_plus[i] == NULL)
4200 				continue;
4201 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4202 			if (ent->ent_lu == lu)
4203 				break;
4204 		}
4205 	} else {
4206 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4207 		ent = (stmf_lun_map_ent_t *)
4208 		    stmf_get_ent_from_map(iss->iss_sm, n);
4209 		if (lu && (ent->ent_lu != lu))
4210 			ent = NULL;
4211 	}
4212 	if (ent && ent->ent_itl_datap) {
4213 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
4214 		ret = STMF_SUCCESS;
4215 	} else {
4216 		ret = STMF_NOT_FOUND;
4217 	}
4218 
4219 	rw_exit(iss->iss_lockp);
4220 	return (ret);
4221 }
4222 
4223 stmf_data_buf_t *
4224 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
4225     uint32_t flags)
4226 {
4227 	stmf_i_scsi_task_t *itask =
4228 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4229 	stmf_local_port_t *lport = task->task_lport;
4230 	stmf_data_buf_t *dbuf;
4231 	uint8_t ndx;
4232 
4233 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
4234 	if (ndx == 0xff)
4235 		return (NULL);
4236 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
4237 	    task, size, pminsize, flags);
4238 	if (dbuf) {
4239 		task->task_cur_nbufs++;
4240 		itask->itask_allocated_buf_map |= (1 << ndx);
4241 		dbuf->db_handle = ndx;
4242 		return (dbuf);
4243 	}
4244 
4245 	return (NULL);
4246 }
4247 
4248 stmf_status_t
4249 stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags)
4250 {
4251 	stmf_i_scsi_task_t *itask =
4252 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4253 	stmf_local_port_t *lport = task->task_lport;
4254 	uint8_t ndx;
4255 	stmf_status_t ret;
4256 
4257 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
4258 	ASSERT(lport->lport_ds->ds_setup_dbuf != NULL);
4259 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
4260 
4261 	if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0)
4262 		return (STMF_FAILURE);
4263 	if (lport->lport_ds->ds_setup_dbuf == NULL)
4264 		return (STMF_FAILURE);
4265 
4266 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
4267 	if (ndx == 0xff)
4268 		return (STMF_FAILURE);
4269 	ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags);
4270 	if (ret == STMF_FAILURE)
4271 		return (STMF_FAILURE);
4272 	itask->itask_dbufs[ndx] = dbuf;
4273 	task->task_cur_nbufs++;
4274 	itask->itask_allocated_buf_map |= (1 << ndx);
4275 	dbuf->db_handle = ndx;
4276 
4277 	return (STMF_SUCCESS);
4278 }
4279 
4280 void
4281 stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
4282 {
4283 	stmf_i_scsi_task_t *itask =
4284 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4285 	stmf_local_port_t *lport = task->task_lport;
4286 
4287 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
4288 	ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL);
4289 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
4290 
4291 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
4292 	task->task_cur_nbufs--;
4293 	lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf);
4294 }
4295 
4296 void
4297 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
4298 {
4299 	stmf_i_scsi_task_t *itask =
4300 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4301 	stmf_local_port_t *lport = task->task_lport;
4302 
4303 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
4304 	task->task_cur_nbufs--;
4305 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
4306 }
4307 
4308 stmf_data_buf_t *
4309 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
4310 {
4311 	stmf_i_scsi_task_t *itask;
4312 
4313 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4314 	if (h > 3)
4315 		return (NULL);
4316 	return (itask->itask_dbufs[h]);
4317 }
4318 
4319 /* ARGSUSED */
4320 struct scsi_task *
4321 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
4322 			uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
4323 {
4324 	stmf_lu_t *lu;
4325 	stmf_i_scsi_session_t *iss;
4326 	stmf_i_lu_t *ilu;
4327 	stmf_i_scsi_task_t *itask;
4328 	stmf_i_scsi_task_t **ppitask;
4329 	scsi_task_t *task;
4330 	uint8_t	*l;
4331 	stmf_lun_map_ent_t *lun_map_ent;
4332 	uint16_t cdb_length;
4333 	uint16_t luNbr;
4334 	uint8_t new_task = 0;
4335 
4336 	/*
4337 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
4338 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
4339 	 * depend upon this alignment.
4340 	 */
4341 	if (cdb_length_in >= 16)
4342 		cdb_length = cdb_length_in + 7;
4343 	else
4344 		cdb_length = 16 + 7;
4345 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4346 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4347 	rw_enter(iss->iss_lockp, RW_READER);
4348 	lun_map_ent =
4349 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
4350 	if (!lun_map_ent) {
4351 		lu = dlun0;
4352 	} else {
4353 		lu = lun_map_ent->ent_lu;
4354 	}
4355 	ilu = lu->lu_stmf_private;
4356 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4357 		rw_exit(iss->iss_lockp);
4358 		return (NULL);
4359 	}
4360 	do {
4361 		if (ilu->ilu_free_tasks == NULL) {
4362 			new_task = 1;
4363 			break;
4364 		}
4365 		mutex_enter(&ilu->ilu_task_lock);
4366 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
4367 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
4368 		    ppitask = &((*ppitask)->itask_lu_free_next))
4369 			;
4370 		if (*ppitask) {
4371 			itask = *ppitask;
4372 			*ppitask = (*ppitask)->itask_lu_free_next;
4373 			ilu->ilu_ntasks_free--;
4374 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
4375 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4376 		} else {
4377 			new_task = 1;
4378 		}
4379 		mutex_exit(&ilu->ilu_task_lock);
4380 	/* CONSTCOND */
4381 	} while (0);
4382 
4383 	if (!new_task) {
4384 		/*
4385 		 * Save the task_cdb pointer and zero per cmd fields.
4386 		 * We know the task_cdb_length is large enough by task
4387 		 * selection process above.
4388 		 */
4389 		uint8_t *save_cdb;
4390 		uintptr_t t_start, t_end;
4391 
4392 		task = itask->itask_task;
4393 		save_cdb = task->task_cdb;	/* save */
4394 		t_start = (uintptr_t)&task->task_flags;
4395 		t_end = (uintptr_t)&task->task_extended_cmd;
4396 		bzero((void *)t_start, (size_t)(t_end - t_start));
4397 		task->task_cdb = save_cdb;	/* restore */
4398 		itask->itask_ncmds = 0;
4399 	} else {
4400 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
4401 		    cdb_length, AF_FORCE_NOSLEEP);
4402 		if (task == NULL) {
4403 			rw_exit(iss->iss_lockp);
4404 			return (NULL);
4405 		}
4406 		task->task_lu = lu;
4407 		l = task->task_lun_no;
4408 		l[0] = lun[0];
4409 		l[1] = lun[1];
4410 		l[2] = lun[2];
4411 		l[3] = lun[3];
4412 		l[4] = lun[4];
4413 		l[5] = lun[5];
4414 		l[6] = lun[6];
4415 		l[7] = lun[7];
4416 		task->task_cdb = (uint8_t *)task->task_port_private;
4417 		if ((ulong_t)(task->task_cdb) & 7ul) {
4418 			task->task_cdb = (uint8_t *)(((ulong_t)
4419 			    (task->task_cdb) + 7ul) & ~(7ul));
4420 		}
4421 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4422 		itask->itask_cdb_buf_size = cdb_length;
4423 	}
4424 	task->task_session = ss;
4425 	task->task_lport = lport;
4426 	task->task_cdb_length = cdb_length_in;
4427 	itask->itask_flags = ITASK_IN_TRANSITION;
4428 	itask->itask_waitq_time = 0;
4429 	itask->itask_lu_read_time = itask->itask_lu_write_time = 0;
4430 	itask->itask_lport_read_time = itask->itask_lport_write_time = 0;
4431 	itask->itask_read_xfer = itask->itask_write_xfer = 0;
4432 
4433 	if (new_task) {
4434 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
4435 			rw_exit(iss->iss_lockp);
4436 			stmf_free(task);
4437 			return (NULL);
4438 		}
4439 		mutex_enter(&ilu->ilu_task_lock);
4440 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4441 			mutex_exit(&ilu->ilu_task_lock);
4442 			rw_exit(iss->iss_lockp);
4443 			stmf_free(task);
4444 			return (NULL);
4445 		}
4446 		itask->itask_lu_next = ilu->ilu_tasks;
4447 		if (ilu->ilu_tasks)
4448 			ilu->ilu_tasks->itask_lu_prev = itask;
4449 		ilu->ilu_tasks = itask;
4450 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
4451 		ilu->ilu_ntasks++;
4452 		mutex_exit(&ilu->ilu_task_lock);
4453 	}
4454 
4455 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
4456 	atomic_add_32(itask->itask_ilu_task_cntr, 1);
4457 	itask->itask_start_time = ddi_get_lbolt();
4458 
4459 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
4460 	    lun_map_ent->ent_itl_datap) != NULL)) {
4461 		atomic_add_32(&itask->itask_itl_datap->itl_counter, 1);
4462 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
4463 	} else {
4464 		itask->itask_itl_datap = NULL;
4465 		task->task_lu_itl_handle = NULL;
4466 	}
4467 
4468 	rw_exit(iss->iss_lockp);
4469 	return (task);
4470 }
4471 
4472 static void
4473 stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss)
4474 {
4475 	stmf_i_scsi_task_t *itask =
4476 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4477 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4478 
4479 	ASSERT(rw_lock_held(iss->iss_lockp));
4480 	itask->itask_flags = ITASK_IN_FREE_LIST;
4481 	itask->itask_proxy_msg_id = 0;
4482 	mutex_enter(&ilu->ilu_task_lock);
4483 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
4484 	ilu->ilu_free_tasks = itask;
4485 	ilu->ilu_ntasks_free++;
4486 	mutex_exit(&ilu->ilu_task_lock);
4487 	atomic_add_32(itask->itask_ilu_task_cntr, -1);
4488 }
4489 
4490 void
4491 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
4492 {
4493 	uint32_t	num_to_release, ndx;
4494 	stmf_i_scsi_task_t *itask;
4495 	stmf_lu_t	*lu = ilu->ilu_lu;
4496 
4497 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
4498 
4499 	/* free half of the minimal free of the free tasks */
4500 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
4501 	if (!num_to_release) {
4502 		return;
4503 	}
4504 	for (ndx = 0; ndx < num_to_release; ndx++) {
4505 		mutex_enter(&ilu->ilu_task_lock);
4506 		itask = ilu->ilu_free_tasks;
4507 		if (itask == NULL) {
4508 			mutex_exit(&ilu->ilu_task_lock);
4509 			break;
4510 		}
4511 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
4512 		ilu->ilu_ntasks_free--;
4513 		mutex_exit(&ilu->ilu_task_lock);
4514 
4515 		lu->lu_task_free(itask->itask_task);
4516 		mutex_enter(&ilu->ilu_task_lock);
4517 		if (itask->itask_lu_next)
4518 			itask->itask_lu_next->itask_lu_prev =
4519 			    itask->itask_lu_prev;
4520 		if (itask->itask_lu_prev)
4521 			itask->itask_lu_prev->itask_lu_next =
4522 			    itask->itask_lu_next;
4523 		else
4524 			ilu->ilu_tasks = itask->itask_lu_next;
4525 
4526 		ilu->ilu_ntasks--;
4527 		mutex_exit(&ilu->ilu_task_lock);
4528 		stmf_free(itask->itask_task);
4529 	}
4530 }
4531 
4532 /*
4533  * Called with stmf_lock held
4534  */
4535 void
4536 stmf_check_freetask()
4537 {
4538 	stmf_i_lu_t *ilu;
4539 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4540 
4541 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
4542 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
4543 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
4544 		if (!ilu->ilu_ntasks_min_free) {
4545 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4546 			continue;
4547 		}
4548 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4549 		mutex_exit(&stmf_state.stmf_lock);
4550 		stmf_task_lu_check_freelist(ilu);
4551 		/*
4552 		 * we do not care about the accuracy of
4553 		 * ilu_ntasks_min_free, so we don't lock here
4554 		 */
4555 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4556 		mutex_enter(&stmf_state.stmf_lock);
4557 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4558 		cv_broadcast(&stmf_state.stmf_cv);
4559 		if (ddi_get_lbolt() >= endtime)
4560 			break;
4561 	}
4562 }
4563 
4564 void
4565 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
4566 {
4567 	clock_t l = ddi_get_lbolt();
4568 	clock_t ps = drv_usectohz(1000000);
4569 	stmf_i_scsi_task_t *itask;
4570 	scsi_task_t *task;
4571 	uint32_t to;
4572 
4573 	mutex_enter(&ilu->ilu_task_lock);
4574 	for (itask = ilu->ilu_tasks; itask != NULL;
4575 	    itask = itask->itask_lu_next) {
4576 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
4577 		    ITASK_BEING_ABORTED)) {
4578 			continue;
4579 		}
4580 		task = itask->itask_task;
4581 		if (task->task_timeout == 0)
4582 			to = stmf_default_task_timeout;
4583 		else
4584 			to = task->task_timeout;
4585 		if ((itask->itask_start_time + (to * ps)) > l)
4586 			continue;
4587 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4588 		    STMF_TIMEOUT, NULL);
4589 	}
4590 	mutex_exit(&ilu->ilu_task_lock);
4591 }
4592 
4593 /*
4594  * Called with stmf_lock held
4595  */
4596 void
4597 stmf_check_ilu_timing()
4598 {
4599 	stmf_i_lu_t *ilu;
4600 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4601 
4602 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
4603 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
4604 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
4605 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
4606 			if (ilu->ilu_task_cntr2 == 0) {
4607 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
4608 				continue;
4609 			}
4610 		} else {
4611 			if (ilu->ilu_task_cntr1 == 0) {
4612 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
4613 				continue;
4614 			}
4615 		}
4616 		/*
4617 		 * If we are here then it means that there is some slowdown
4618 		 * in tasks on this lu. We need to check.
4619 		 */
4620 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4621 		mutex_exit(&stmf_state.stmf_lock);
4622 		stmf_do_ilu_timeouts(ilu);
4623 		mutex_enter(&stmf_state.stmf_lock);
4624 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4625 		cv_broadcast(&stmf_state.stmf_cv);
4626 		if (ddi_get_lbolt() >= endtime)
4627 			break;
4628 	}
4629 }
4630 
4631 /*
4632  * Kills all tasks on a lu except tm_task
4633  */
4634 void
4635 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
4636 {
4637 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4638 	stmf_i_scsi_task_t *itask;
4639 
4640 	mutex_enter(&ilu->ilu_task_lock);
4641 
4642 	for (itask = ilu->ilu_tasks; itask != NULL;
4643 	    itask = itask->itask_lu_next) {
4644 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
4645 			continue;
4646 		if (itask->itask_task == tm_task)
4647 			continue;
4648 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
4649 	}
4650 	mutex_exit(&ilu->ilu_task_lock);
4651 }
4652 
4653 void
4654 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
4655 {
4656 	int i;
4657 	uint8_t map;
4658 
4659 	if ((map = itask->itask_allocated_buf_map) == 0)
4660 		return;
4661 	for (i = 0; i < 4; i++) {
4662 		if (map & 1) {
4663 			stmf_data_buf_t *dbuf;
4664 
4665 			dbuf = itask->itask_dbufs[i];
4666 			if (dbuf->db_xfer_start_timestamp) {
4667 				stmf_lport_xfer_done(itask, dbuf);
4668 			}
4669 			if (dbuf->db_flags & DB_LU_DATA_BUF) {
4670 				/*
4671 				 * LU needs to clean up buffer.
4672 				 * LU is required to free the buffer
4673 				 * in the xfer_done handler.
4674 				 */
4675 				scsi_task_t *task = itask->itask_task;
4676 				stmf_lu_t *lu = task->task_lu;
4677 
4678 				lu->lu_dbuf_free(task, dbuf);
4679 				ASSERT(((itask->itask_allocated_buf_map>>i)
4680 				    & 1) == 0); /* must be gone */
4681 			} else {
4682 				ASSERT(dbuf->db_lu_private == NULL);
4683 				dbuf->db_lu_private = NULL;
4684 				lport->lport_ds->ds_free_data_buf(
4685 				    lport->lport_ds, dbuf);
4686 			}
4687 		}
4688 		map >>= 1;
4689 	}
4690 	itask->itask_allocated_buf_map = 0;
4691 }
4692 
4693 void
4694 stmf_task_free(scsi_task_t *task)
4695 {
4696 	stmf_local_port_t *lport = task->task_lport;
4697 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4698 	    task->task_stmf_private;
4699 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4700 	    task->task_session->ss_stmf_private;
4701 
4702 	stmf_free_task_bufs(itask, lport);
4703 	stmf_itl_task_done(itask);
4704 	DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task,
4705 	    hrtime_t,
4706 	    itask->itask_done_timestamp - itask->itask_start_timestamp);
4707 	if (itask->itask_itl_datap) {
4708 		if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter,
4709 		    -1) == 0) {
4710 			stmf_release_itl_handle(task->task_lu,
4711 			    itask->itask_itl_datap);
4712 		}
4713 	}
4714 
4715 	rw_enter(iss->iss_lockp, RW_READER);
4716 	lport->lport_task_free(task);
4717 	if (itask->itask_worker) {
4718 		atomic_add_32(&stmf_cur_ntasks, -1);
4719 		atomic_add_32(&itask->itask_worker->worker_ref_count, -1);
4720 	}
4721 	/*
4722 	 * After calling stmf_task_lu_free, the task pointer can no longer
4723 	 * be trusted.
4724 	 */
4725 	stmf_task_lu_free(task, iss);
4726 	rw_exit(iss->iss_lockp);
4727 }
4728 
4729 void
4730 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
4731 {
4732 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4733 	    task->task_stmf_private;
4734 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4735 	int nv;
4736 	uint32_t old, new;
4737 	uint32_t ct;
4738 	stmf_worker_t *w, *w1;
4739 	uint8_t tm;
4740 
4741 	if (task->task_max_nbufs > 4)
4742 		task->task_max_nbufs = 4;
4743 	task->task_cur_nbufs = 0;
4744 	/* Latest value of currently running tasks */
4745 	ct = atomic_add_32_nv(&stmf_cur_ntasks, 1);
4746 
4747 	/* Select the next worker using round robin */
4748 	nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1);
4749 	if (nv >= stmf_nworkers_accepting_cmds) {
4750 		int s = nv;
4751 		do {
4752 			nv -= stmf_nworkers_accepting_cmds;
4753 		} while (nv >= stmf_nworkers_accepting_cmds);
4754 		if (nv < 0)
4755 			nv = 0;
4756 		/* Its ok if this cas fails */
4757 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
4758 		    s, nv);
4759 	}
4760 	w = &stmf_workers[nv];
4761 
4762 	/*
4763 	 * A worker can be pinned by interrupt. So select the next one
4764 	 * if it has lower load.
4765 	 */
4766 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
4767 		w1 = stmf_workers;
4768 	} else {
4769 		w1 = &stmf_workers[nv + 1];
4770 	}
4771 	if (w1->worker_queue_depth < w->worker_queue_depth)
4772 		w = w1;
4773 
4774 	mutex_enter(&w->worker_lock);
4775 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
4776 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4777 		/*
4778 		 * Maybe we are in the middle of a change. Just go to
4779 		 * the 1st worker.
4780 		 */
4781 		mutex_exit(&w->worker_lock);
4782 		w = stmf_workers;
4783 		mutex_enter(&w->worker_lock);
4784 	}
4785 	itask->itask_worker = w;
4786 	/*
4787 	 * Track max system load inside the worker as we already have the
4788 	 * worker lock (no point implementing another lock). The service
4789 	 * thread will do the comparisons and figure out the max overall
4790 	 * system load.
4791 	 */
4792 	if (w->worker_max_sys_qdepth_pu < ct)
4793 		w->worker_max_sys_qdepth_pu = ct;
4794 
4795 	do {
4796 		old = new = itask->itask_flags;
4797 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
4798 		if (task->task_mgmt_function) {
4799 			tm = task->task_mgmt_function;
4800 			if ((tm == TM_TARGET_RESET) ||
4801 			    (tm == TM_TARGET_COLD_RESET) ||
4802 			    (tm == TM_TARGET_WARM_RESET)) {
4803 				new |= ITASK_DEFAULT_HANDLING;
4804 			}
4805 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4806 			new |= ITASK_DEFAULT_HANDLING;
4807 		}
4808 		new &= ~ITASK_IN_TRANSITION;
4809 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4810 
4811 	stmf_itl_task_start(itask);
4812 
4813 	itask->itask_worker_next = NULL;
4814 	if (w->worker_task_tail) {
4815 		w->worker_task_tail->itask_worker_next = itask;
4816 	} else {
4817 		w->worker_task_head = itask;
4818 	}
4819 	w->worker_task_tail = itask;
4820 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4821 		w->worker_max_qdepth_pu = w->worker_queue_depth;
4822 	}
4823 	/* Measure task waitq time */
4824 	itask->itask_waitq_enter_timestamp = gethrtime();
4825 	atomic_add_32(&w->worker_ref_count, 1);
4826 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
4827 	itask->itask_ncmds = 1;
4828 	if (dbuf) {
4829 		itask->itask_allocated_buf_map = 1;
4830 		itask->itask_dbufs[0] = dbuf;
4831 		dbuf->db_handle = 0;
4832 	} else {
4833 		itask->itask_allocated_buf_map = 0;
4834 		itask->itask_dbufs[0] = NULL;
4835 	}
4836 
4837 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) {
4838 		w->worker_signal_timestamp = gethrtime();
4839 		DTRACE_PROBE2(worker__signal, stmf_worker_t *, w,
4840 		    scsi_task_t *, task);
4841 		cv_signal(&w->worker_cv);
4842 	}
4843 	mutex_exit(&w->worker_lock);
4844 
4845 	/*
4846 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
4847 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
4848 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
4849 	 */
4850 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4851 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
4852 	}
4853 }
4854 
4855 /*
4856  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
4857  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
4858  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
4859  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
4860  * the LU will make this call only if we call the LU's abort entry point.
4861  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
4862  *
4863  * Same logic applies for the port.
4864  *
4865  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
4866  * and KNOWN_TO_TGT_PORT are reset.
4867  *
4868  * +++++++++++++++++++++++++++++++++++++++++++++++
4869  */
4870 
4871 stmf_status_t
4872 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
4873 {
4874 	stmf_status_t ret;
4875 
4876 	stmf_i_scsi_task_t *itask =
4877 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4878 
4879 	if (ioflags & STMF_IOF_LU_DONE) {
4880 		uint32_t new, old;
4881 		do {
4882 			new = old = itask->itask_flags;
4883 			if (new & ITASK_BEING_ABORTED)
4884 				return (STMF_ABORTED);
4885 			new &= ~ITASK_KNOWN_TO_LU;
4886 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4887 	}
4888 	if (itask->itask_flags & ITASK_BEING_ABORTED)
4889 		return (STMF_ABORTED);
4890 #ifdef	DEBUG
4891 	if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) {
4892 		if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) ==
4893 		    1)
4894 			return (STMF_SUCCESS);
4895 	}
4896 #endif
4897 
4898 	stmf_update_kstat_lu_io(task, dbuf);
4899 	stmf_update_kstat_lport_io(task, dbuf);
4900 	stmf_lport_xfer_start(itask, dbuf);
4901 	if (ioflags & STMF_IOF_STATS_ONLY) {
4902 		stmf_lport_xfer_done(itask, dbuf);
4903 		return (STMF_SUCCESS);
4904 	}
4905 
4906 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
4907 
4908 	/*
4909 	 * Port provider may have already called the buffer callback in
4910 	 * which case dbuf->db_xfer_start_timestamp will be 0.
4911 	 */
4912 	if ((ret != STMF_SUCCESS) && (dbuf->db_xfer_start_timestamp != 0)) {
4913 		stmf_lport_xfer_done(itask, dbuf);
4914 	}
4915 
4916 	return (ret);
4917 }
4918 
4919 void
4920 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
4921 {
4922 	stmf_i_scsi_task_t *itask =
4923 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4924 	stmf_worker_t *w = itask->itask_worker;
4925 	uint32_t new, old;
4926 	uint8_t update_queue_flags, free_it, queue_it;
4927 
4928 	stmf_lport_xfer_done(itask, dbuf);
4929 
4930 	mutex_enter(&w->worker_lock);
4931 	do {
4932 		new = old = itask->itask_flags;
4933 		if (old & ITASK_BEING_ABORTED) {
4934 			mutex_exit(&w->worker_lock);
4935 			return;
4936 		}
4937 		free_it = 0;
4938 		if (iof & STMF_IOF_LPORT_DONE) {
4939 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
4940 			task->task_completion_status = dbuf->db_xfer_status;
4941 			free_it = 1;
4942 		}
4943 		/*
4944 		 * If the task is known to LU then queue it. But if
4945 		 * it is already queued (multiple completions) then
4946 		 * just update the buffer information by grabbing the
4947 		 * worker lock. If the task is not known to LU,
4948 		 * completed/aborted, then see if we need to
4949 		 * free this task.
4950 		 */
4951 		if (old & ITASK_KNOWN_TO_LU) {
4952 			free_it = 0;
4953 			update_queue_flags = 1;
4954 			if (old & ITASK_IN_WORKER_QUEUE) {
4955 				queue_it = 0;
4956 			} else {
4957 				queue_it = 1;
4958 				new |= ITASK_IN_WORKER_QUEUE;
4959 			}
4960 		} else {
4961 			update_queue_flags = 0;
4962 			queue_it = 0;
4963 		}
4964 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4965 
4966 	if (update_queue_flags) {
4967 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
4968 
4969 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
4970 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
4971 		if (queue_it) {
4972 			itask->itask_worker_next = NULL;
4973 			if (w->worker_task_tail) {
4974 				w->worker_task_tail->itask_worker_next = itask;
4975 			} else {
4976 				w->worker_task_head = itask;
4977 			}
4978 			w->worker_task_tail = itask;
4979 			/* Measure task waitq time */
4980 			itask->itask_waitq_enter_timestamp = gethrtime();
4981 			if (++(w->worker_queue_depth) >
4982 			    w->worker_max_qdepth_pu) {
4983 				w->worker_max_qdepth_pu = w->worker_queue_depth;
4984 			}
4985 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4986 				cv_signal(&w->worker_cv);
4987 		}
4988 	}
4989 	mutex_exit(&w->worker_lock);
4990 
4991 	if (free_it) {
4992 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4993 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4994 		    ITASK_BEING_ABORTED)) == 0) {
4995 			stmf_task_free(task);
4996 		}
4997 	}
4998 }
4999 
5000 stmf_status_t
5001 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
5002 {
5003 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
5004 
5005 	stmf_i_scsi_task_t *itask =
5006 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5007 	if (ioflags & STMF_IOF_LU_DONE) {
5008 		uint32_t new, old;
5009 		do {
5010 			new = old = itask->itask_flags;
5011 			if (new & ITASK_BEING_ABORTED)
5012 				return (STMF_ABORTED);
5013 			new &= ~ITASK_KNOWN_TO_LU;
5014 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5015 	}
5016 
5017 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
5018 		return (STMF_SUCCESS);
5019 	}
5020 
5021 	if (itask->itask_flags & ITASK_BEING_ABORTED)
5022 		return (STMF_ABORTED);
5023 
5024 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5025 		task->task_status_ctrl = 0;
5026 		task->task_resid = 0;
5027 	} else if (task->task_cmd_xfer_length >
5028 	    task->task_expected_xfer_length) {
5029 		task->task_status_ctrl = TASK_SCTRL_OVER;
5030 		task->task_resid = task->task_cmd_xfer_length -
5031 		    task->task_expected_xfer_length;
5032 	} else if (task->task_nbytes_transferred <
5033 	    task->task_expected_xfer_length) {
5034 		task->task_status_ctrl = TASK_SCTRL_UNDER;
5035 		task->task_resid = task->task_expected_xfer_length -
5036 		    task->task_nbytes_transferred;
5037 	} else {
5038 		task->task_status_ctrl = 0;
5039 		task->task_resid = 0;
5040 	}
5041 	return (task->task_lport->lport_send_status(task, ioflags));
5042 }
5043 
5044 void
5045 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5046 {
5047 	stmf_i_scsi_task_t *itask =
5048 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5049 	stmf_worker_t *w = itask->itask_worker;
5050 	uint32_t new, old;
5051 	uint8_t free_it, queue_it;
5052 
5053 	mutex_enter(&w->worker_lock);
5054 	do {
5055 		new = old = itask->itask_flags;
5056 		if (old & ITASK_BEING_ABORTED) {
5057 			mutex_exit(&w->worker_lock);
5058 			return;
5059 		}
5060 		free_it = 0;
5061 		if (iof & STMF_IOF_LPORT_DONE) {
5062 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5063 			free_it = 1;
5064 		}
5065 		/*
5066 		 * If the task is known to LU then queue it. But if
5067 		 * it is already queued (multiple completions) then
5068 		 * just update the buffer information by grabbing the
5069 		 * worker lock. If the task is not known to LU,
5070 		 * completed/aborted, then see if we need to
5071 		 * free this task.
5072 		 */
5073 		if (old & ITASK_KNOWN_TO_LU) {
5074 			free_it = 0;
5075 			queue_it = 1;
5076 			if (old & ITASK_IN_WORKER_QUEUE) {
5077 				cmn_err(CE_PANIC, "status completion received"
5078 				    " when task is already in worker queue "
5079 				    " task = %p", (void *)task);
5080 			}
5081 			new |= ITASK_IN_WORKER_QUEUE;
5082 		} else {
5083 			queue_it = 0;
5084 		}
5085 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5086 	task->task_completion_status = s;
5087 
5088 
5089 	if (queue_it) {
5090 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
5091 		itask->itask_cmd_stack[itask->itask_ncmds++] =
5092 		    ITASK_CMD_STATUS_DONE;
5093 		itask->itask_worker_next = NULL;
5094 		if (w->worker_task_tail) {
5095 			w->worker_task_tail->itask_worker_next = itask;
5096 		} else {
5097 			w->worker_task_head = itask;
5098 		}
5099 		w->worker_task_tail = itask;
5100 		/* Measure task waitq time */
5101 		itask->itask_waitq_enter_timestamp = gethrtime();
5102 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5103 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5104 		}
5105 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5106 			cv_signal(&w->worker_cv);
5107 	}
5108 	mutex_exit(&w->worker_lock);
5109 
5110 	if (free_it) {
5111 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
5112 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
5113 		    ITASK_BEING_ABORTED)) == 0) {
5114 			stmf_task_free(task);
5115 		} else {
5116 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
5117 			    " is not done, itask %p itask_flags %x",
5118 			    (void *)itask, itask->itask_flags);
5119 		}
5120 	}
5121 }
5122 
5123 void
5124 stmf_task_lu_done(scsi_task_t *task)
5125 {
5126 	stmf_i_scsi_task_t *itask =
5127 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5128 	stmf_worker_t *w = itask->itask_worker;
5129 	uint32_t new, old;
5130 
5131 	mutex_enter(&w->worker_lock);
5132 	do {
5133 		new = old = itask->itask_flags;
5134 		if (old & ITASK_BEING_ABORTED) {
5135 			mutex_exit(&w->worker_lock);
5136 			return;
5137 		}
5138 		if (old & ITASK_IN_WORKER_QUEUE) {
5139 			cmn_err(CE_PANIC, "task_lu_done received"
5140 			    " when task is in worker queue "
5141 			    " task = %p", (void *)task);
5142 		}
5143 		new &= ~ITASK_KNOWN_TO_LU;
5144 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5145 
5146 	mutex_exit(&w->worker_lock);
5147 
5148 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
5149 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
5150 	    ITASK_BEING_ABORTED)) == 0) {
5151 		stmf_task_free(task);
5152 	} else {
5153 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
5154 		    " the task is still not done, task = %p", (void *)task);
5155 	}
5156 }
5157 
5158 void
5159 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
5160 {
5161 	stmf_i_scsi_task_t *itask =
5162 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5163 	stmf_worker_t *w;
5164 	uint32_t old, new;
5165 
5166 	do {
5167 		old = new = itask->itask_flags;
5168 		if ((old & ITASK_BEING_ABORTED) ||
5169 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
5170 		    ITASK_KNOWN_TO_LU)) == 0)) {
5171 			return;
5172 		}
5173 		new |= ITASK_BEING_ABORTED;
5174 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5175 	task->task_completion_status = s;
5176 	itask->itask_start_time = ddi_get_lbolt();
5177 
5178 	if (((w = itask->itask_worker) == NULL) ||
5179 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
5180 		return;
5181 	}
5182 
5183 	/* Queue it and get out */
5184 	mutex_enter(&w->worker_lock);
5185 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
5186 		mutex_exit(&w->worker_lock);
5187 		return;
5188 	}
5189 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5190 	itask->itask_worker_next = NULL;
5191 	if (w->worker_task_tail) {
5192 		w->worker_task_tail->itask_worker_next = itask;
5193 	} else {
5194 		w->worker_task_head = itask;
5195 	}
5196 	w->worker_task_tail = itask;
5197 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5198 		w->worker_max_qdepth_pu = w->worker_queue_depth;
5199 	}
5200 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5201 		cv_signal(&w->worker_cv);
5202 	mutex_exit(&w->worker_lock);
5203 }
5204 
5205 void
5206 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
5207 {
5208 	stmf_i_scsi_task_t *itask = NULL;
5209 	uint32_t old, new, f, rf;
5210 
5211 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
5212 	    stmf_status_t, s);
5213 
5214 	switch (abort_cmd) {
5215 	case STMF_QUEUE_ABORT_LU:
5216 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
5217 		return;
5218 	case STMF_QUEUE_TASK_ABORT:
5219 		stmf_queue_task_for_abort(task, s);
5220 		return;
5221 	case STMF_REQUEUE_TASK_ABORT_LPORT:
5222 		rf = ITASK_TGT_PORT_ABORT_CALLED;
5223 		f = ITASK_KNOWN_TO_TGT_PORT;
5224 		break;
5225 	case STMF_REQUEUE_TASK_ABORT_LU:
5226 		rf = ITASK_LU_ABORT_CALLED;
5227 		f = ITASK_KNOWN_TO_LU;
5228 		break;
5229 	default:
5230 		return;
5231 	}
5232 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
5233 	f |= ITASK_BEING_ABORTED | rf;
5234 	do {
5235 		old = new = itask->itask_flags;
5236 		if ((old & f) != f) {
5237 			return;
5238 		}
5239 		new &= ~rf;
5240 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5241 }
5242 
5243 void
5244 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5245 {
5246 	char			 info[STMF_CHANGE_INFO_LEN];
5247 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5248 	unsigned long long	st;
5249 
5250 	st = s;	/* gcc fix */
5251 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5252 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5253 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
5254 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
5255 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5256 		    "Task aborted but LU is not finished, task ="
5257 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
5258 	} else {
5259 		/*
5260 		 * LU abort successfully
5261 		 */
5262 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
5263 		return;
5264 	}
5265 
5266 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
5267 	stmf_abort_task_offline(task, 1, info);
5268 }
5269 
5270 void
5271 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5272 {
5273 	char			info[STMF_CHANGE_INFO_LEN];
5274 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5275 	unsigned long long	st;
5276 	uint32_t		old, new;
5277 
5278 	st = s;
5279 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5280 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5281 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
5282 		    st);
5283 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
5284 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5285 		    "Task aborted but tgt port is not finished, "
5286 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
5287 	} else {
5288 		/*
5289 		 * LPORT abort successfully
5290 		 */
5291 		do {
5292 			old = new = itask->itask_flags;
5293 			if (!(old & ITASK_KNOWN_TO_TGT_PORT))
5294 				return;
5295 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5296 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5297 		return;
5298 	}
5299 
5300 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
5301 	stmf_abort_task_offline(task, 0, info);
5302 }
5303 
5304 stmf_status_t
5305 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
5306 {
5307 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5308 	    task->task_stmf_private;
5309 	stmf_worker_t *w = itask->itask_worker;
5310 	int i;
5311 
5312 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
5313 	mutex_enter(&w->worker_lock);
5314 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5315 		mutex_exit(&w->worker_lock);
5316 		return (STMF_BUSY);
5317 	}
5318 	for (i = 0; i < itask->itask_ncmds; i++) {
5319 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
5320 			mutex_exit(&w->worker_lock);
5321 			return (STMF_SUCCESS);
5322 		}
5323 	}
5324 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
5325 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5326 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5327 	} else {
5328 		clock_t t = drv_usectohz(timeout * 1000);
5329 		if (t == 0)
5330 			t = 1;
5331 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5332 	}
5333 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5334 		itask->itask_worker_next = NULL;
5335 		if (w->worker_task_tail) {
5336 			w->worker_task_tail->itask_worker_next = itask;
5337 		} else {
5338 			w->worker_task_head = itask;
5339 		}
5340 		w->worker_task_tail = itask;
5341 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5342 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5343 		}
5344 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5345 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5346 			cv_signal(&w->worker_cv);
5347 	}
5348 	mutex_exit(&w->worker_lock);
5349 	return (STMF_SUCCESS);
5350 }
5351 
5352 stmf_status_t
5353 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
5354 {
5355 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5356 	    task->task_stmf_private;
5357 	stmf_worker_t *w = itask->itask_worker;
5358 	int i;
5359 
5360 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
5361 	mutex_enter(&w->worker_lock);
5362 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5363 		mutex_exit(&w->worker_lock);
5364 		return (STMF_BUSY);
5365 	}
5366 	for (i = 0; i < itask->itask_ncmds; i++) {
5367 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
5368 			mutex_exit(&w->worker_lock);
5369 			return (STMF_SUCCESS);
5370 		}
5371 	}
5372 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
5373 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5374 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5375 	} else {
5376 		clock_t t = drv_usectohz(timeout * 1000);
5377 		if (t == 0)
5378 			t = 1;
5379 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5380 	}
5381 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5382 		itask->itask_worker_next = NULL;
5383 		if (w->worker_task_tail) {
5384 			w->worker_task_tail->itask_worker_next = itask;
5385 		} else {
5386 			w->worker_task_head = itask;
5387 		}
5388 		w->worker_task_tail = itask;
5389 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5390 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5391 		}
5392 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5393 			cv_signal(&w->worker_cv);
5394 	}
5395 	mutex_exit(&w->worker_lock);
5396 	return (STMF_SUCCESS);
5397 }
5398 
5399 void
5400 stmf_do_task_abort(scsi_task_t *task)
5401 {
5402 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5403 	stmf_lu_t		*lu;
5404 	stmf_local_port_t	*lport;
5405 	unsigned long long	 ret;
5406 	uint32_t		 old, new;
5407 	uint8_t			 call_lu_abort, call_port_abort;
5408 	char			 info[STMF_CHANGE_INFO_LEN];
5409 
5410 	lu = task->task_lu;
5411 	lport = task->task_lport;
5412 	do {
5413 		old = new = itask->itask_flags;
5414 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
5415 		    ITASK_KNOWN_TO_LU) {
5416 			new |= ITASK_LU_ABORT_CALLED;
5417 			call_lu_abort = 1;
5418 		} else {
5419 			call_lu_abort = 0;
5420 		}
5421 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5422 
5423 	if (call_lu_abort) {
5424 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
5425 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5426 		} else {
5427 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5428 		}
5429 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5430 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
5431 		} else if (ret == STMF_BUSY) {
5432 			atomic_and_32(&itask->itask_flags,
5433 			    ~ITASK_LU_ABORT_CALLED);
5434 		} else if (ret != STMF_SUCCESS) {
5435 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5436 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
5437 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
5438 			stmf_abort_task_offline(task, 1, info);
5439 		}
5440 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
5441 		if (ddi_get_lbolt() > (itask->itask_start_time +
5442 		    STMF_SEC2TICK(lu->lu_abort_timeout?
5443 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
5444 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5445 			    "lu abort timed out");
5446 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
5447 			stmf_abort_task_offline(itask->itask_task, 1, info);
5448 		}
5449 	}
5450 
5451 	do {
5452 		old = new = itask->itask_flags;
5453 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
5454 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
5455 			new |= ITASK_TGT_PORT_ABORT_CALLED;
5456 			call_port_abort = 1;
5457 		} else {
5458 			call_port_abort = 0;
5459 		}
5460 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5461 	if (call_port_abort) {
5462 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
5463 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5464 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
5465 		} else if (ret == STMF_BUSY) {
5466 			atomic_and_32(&itask->itask_flags,
5467 			    ~ITASK_TGT_PORT_ABORT_CALLED);
5468 		} else if (ret != STMF_SUCCESS) {
5469 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5470 			    "Abort failed by tgt port %p ret %llx",
5471 			    (void *)lport, ret);
5472 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
5473 			stmf_abort_task_offline(task, 0, info);
5474 		}
5475 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
5476 		if (ddi_get_lbolt() > (itask->itask_start_time +
5477 		    STMF_SEC2TICK(lport->lport_abort_timeout?
5478 		    lport->lport_abort_timeout :
5479 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
5480 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
5481 			    "lport abort timed out");
5482 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
5483 			stmf_abort_task_offline(itask->itask_task, 0, info);
5484 		}
5485 	}
5486 }
5487 
5488 stmf_status_t
5489 stmf_ctl(int cmd, void *obj, void *arg)
5490 {
5491 	stmf_status_t			ret;
5492 	stmf_i_lu_t			*ilu;
5493 	stmf_i_local_port_t		*ilport;
5494 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
5495 
5496 	mutex_enter(&stmf_state.stmf_lock);
5497 	ret = STMF_INVALID_ARG;
5498 	if (cmd & STMF_CMD_LU_OP) {
5499 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
5500 		if (ilu == NULL) {
5501 			goto stmf_ctl_lock_exit;
5502 		}
5503 		DTRACE_PROBE3(lu__state__change,
5504 		    stmf_lu_t *, ilu->ilu_lu,
5505 		    int, cmd, stmf_state_change_info_t *, ssci);
5506 	} else if (cmd & STMF_CMD_LPORT_OP) {
5507 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
5508 		if (ilport == NULL) {
5509 			goto stmf_ctl_lock_exit;
5510 		}
5511 		DTRACE_PROBE3(lport__state__change,
5512 		    stmf_local_port_t *, ilport->ilport_lport,
5513 		    int, cmd, stmf_state_change_info_t *, ssci);
5514 	} else {
5515 		goto stmf_ctl_lock_exit;
5516 	}
5517 
5518 	switch (cmd) {
5519 	case STMF_CMD_LU_ONLINE:
5520 		switch (ilu->ilu_state) {
5521 			case STMF_STATE_OFFLINE:
5522 				ret = STMF_SUCCESS;
5523 				break;
5524 			case STMF_STATE_ONLINE:
5525 			case STMF_STATE_ONLINING:
5526 				ret = STMF_ALREADY;
5527 				break;
5528 			case STMF_STATE_OFFLINING:
5529 				ret = STMF_BUSY;
5530 				break;
5531 			default:
5532 				ret = STMF_BADSTATE;
5533 				break;
5534 		}
5535 		if (ret != STMF_SUCCESS)
5536 			goto stmf_ctl_lock_exit;
5537 
5538 		ilu->ilu_state = STMF_STATE_ONLINING;
5539 		mutex_exit(&stmf_state.stmf_lock);
5540 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5541 		break;
5542 
5543 	case STMF_CMD_LU_ONLINE_COMPLETE:
5544 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
5545 			ret = STMF_BADSTATE;
5546 			goto stmf_ctl_lock_exit;
5547 		}
5548 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5549 		    STMF_SUCCESS) {
5550 			ilu->ilu_state = STMF_STATE_ONLINE;
5551 			mutex_exit(&stmf_state.stmf_lock);
5552 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5553 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
5554 			mutex_enter(&stmf_state.stmf_lock);
5555 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5556 		} else {
5557 			/* XXX: should throw a meesage an record more data */
5558 			ilu->ilu_state = STMF_STATE_OFFLINE;
5559 		}
5560 		ret = STMF_SUCCESS;
5561 		goto stmf_ctl_lock_exit;
5562 
5563 	case STMF_CMD_LU_OFFLINE:
5564 		switch (ilu->ilu_state) {
5565 			case STMF_STATE_ONLINE:
5566 				ret = STMF_SUCCESS;
5567 				break;
5568 			case STMF_STATE_OFFLINE:
5569 			case STMF_STATE_OFFLINING:
5570 				ret = STMF_ALREADY;
5571 				break;
5572 			case STMF_STATE_ONLINING:
5573 				ret = STMF_BUSY;
5574 				break;
5575 			default:
5576 				ret = STMF_BADSTATE;
5577 				break;
5578 		}
5579 		if (ret != STMF_SUCCESS)
5580 			goto stmf_ctl_lock_exit;
5581 		ilu->ilu_state = STMF_STATE_OFFLINING;
5582 		mutex_exit(&stmf_state.stmf_lock);
5583 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5584 		break;
5585 
5586 	case STMF_CMD_LU_OFFLINE_COMPLETE:
5587 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
5588 			ret = STMF_BADSTATE;
5589 			goto stmf_ctl_lock_exit;
5590 		}
5591 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5592 		    STMF_SUCCESS) {
5593 			ilu->ilu_state = STMF_STATE_OFFLINE;
5594 			mutex_exit(&stmf_state.stmf_lock);
5595 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5596 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
5597 			mutex_enter(&stmf_state.stmf_lock);
5598 		} else {
5599 			ilu->ilu_state = STMF_STATE_ONLINE;
5600 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5601 		}
5602 		mutex_exit(&stmf_state.stmf_lock);
5603 		break;
5604 
5605 	/*
5606 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
5607 	 * It's related with hardware disable/enable.
5608 	 */
5609 	case STMF_CMD_LPORT_ONLINE:
5610 		switch (ilport->ilport_state) {
5611 			case STMF_STATE_OFFLINE:
5612 				ret = STMF_SUCCESS;
5613 				break;
5614 			case STMF_STATE_ONLINE:
5615 			case STMF_STATE_ONLINING:
5616 				ret = STMF_ALREADY;
5617 				break;
5618 			case STMF_STATE_OFFLINING:
5619 				ret = STMF_BUSY;
5620 				break;
5621 			default:
5622 				ret = STMF_BADSTATE;
5623 				break;
5624 		}
5625 		if (ret != STMF_SUCCESS)
5626 			goto stmf_ctl_lock_exit;
5627 
5628 		/*
5629 		 * Only user request can recover the port from the
5630 		 * FORCED_OFFLINE state
5631 		 */
5632 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
5633 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
5634 				ret = STMF_FAILURE;
5635 				goto stmf_ctl_lock_exit;
5636 			}
5637 		}
5638 
5639 		/*
5640 		 * Avoid too frequent request to online
5641 		 */
5642 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
5643 			ilport->ilport_online_times = 0;
5644 			ilport->ilport_avg_interval = 0;
5645 		}
5646 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
5647 		    (ilport->ilport_online_times >= 4)) {
5648 			ret = STMF_FAILURE;
5649 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
5650 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
5651 			    "online the port");
5652 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
5653 			    "online the port, set FORCED_OFFLINE now");
5654 			goto stmf_ctl_lock_exit;
5655 		}
5656 		if (ilport->ilport_online_times > 0) {
5657 			if (ilport->ilport_online_times == 1) {
5658 				ilport->ilport_avg_interval = ddi_get_lbolt() -
5659 				    ilport->ilport_last_online_clock;
5660 			} else {
5661 				ilport->ilport_avg_interval =
5662 				    (ilport->ilport_avg_interval +
5663 				    ddi_get_lbolt() -
5664 				    ilport->ilport_last_online_clock) >> 1;
5665 			}
5666 		}
5667 		ilport->ilport_last_online_clock = ddi_get_lbolt();
5668 		ilport->ilport_online_times++;
5669 
5670 		/*
5671 		 * Submit online service request
5672 		 */
5673 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
5674 		ilport->ilport_state = STMF_STATE_ONLINING;
5675 		mutex_exit(&stmf_state.stmf_lock);
5676 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5677 		break;
5678 
5679 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
5680 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
5681 			ret = STMF_BADSTATE;
5682 			goto stmf_ctl_lock_exit;
5683 		}
5684 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5685 		    STMF_SUCCESS) {
5686 			ilport->ilport_state = STMF_STATE_ONLINE;
5687 			mutex_exit(&stmf_state.stmf_lock);
5688 			((stmf_local_port_t *)obj)->lport_ctl(
5689 			    (stmf_local_port_t *)obj,
5690 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
5691 			mutex_enter(&stmf_state.stmf_lock);
5692 		} else {
5693 			ilport->ilport_state = STMF_STATE_OFFLINE;
5694 		}
5695 		ret = STMF_SUCCESS;
5696 		goto stmf_ctl_lock_exit;
5697 
5698 	case STMF_CMD_LPORT_OFFLINE:
5699 		switch (ilport->ilport_state) {
5700 			case STMF_STATE_ONLINE:
5701 				ret = STMF_SUCCESS;
5702 				break;
5703 			case STMF_STATE_OFFLINE:
5704 			case STMF_STATE_OFFLINING:
5705 				ret = STMF_ALREADY;
5706 				break;
5707 			case STMF_STATE_ONLINING:
5708 				ret = STMF_BUSY;
5709 				break;
5710 			default:
5711 				ret = STMF_BADSTATE;
5712 				break;
5713 		}
5714 		if (ret != STMF_SUCCESS)
5715 			goto stmf_ctl_lock_exit;
5716 
5717 		ilport->ilport_state = STMF_STATE_OFFLINING;
5718 		mutex_exit(&stmf_state.stmf_lock);
5719 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5720 		break;
5721 
5722 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
5723 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
5724 			ret = STMF_BADSTATE;
5725 			goto stmf_ctl_lock_exit;
5726 		}
5727 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5728 		    STMF_SUCCESS) {
5729 			ilport->ilport_state = STMF_STATE_OFFLINE;
5730 			mutex_exit(&stmf_state.stmf_lock);
5731 			((stmf_local_port_t *)obj)->lport_ctl(
5732 			    (stmf_local_port_t *)obj,
5733 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
5734 			mutex_enter(&stmf_state.stmf_lock);
5735 		} else {
5736 			ilport->ilport_state = STMF_STATE_ONLINE;
5737 		}
5738 		mutex_exit(&stmf_state.stmf_lock);
5739 		break;
5740 
5741 	default:
5742 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
5743 		ret = STMF_INVALID_ARG;
5744 		goto stmf_ctl_lock_exit;
5745 	}
5746 
5747 	return (STMF_SUCCESS);
5748 
5749 stmf_ctl_lock_exit:;
5750 	mutex_exit(&stmf_state.stmf_lock);
5751 	return (ret);
5752 }
5753 
5754 /* ARGSUSED */
5755 stmf_status_t
5756 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5757 						uint32_t *bufsizep)
5758 {
5759 	return (STMF_NOT_SUPPORTED);
5760 }
5761 
5762 /* ARGSUSED */
5763 stmf_status_t
5764 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5765 						uint32_t *bufsizep)
5766 {
5767 	uint32_t cl = SI_GET_CLASS(cmd);
5768 
5769 	if (cl == SI_STMF) {
5770 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
5771 	}
5772 	if (cl == SI_LPORT) {
5773 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
5774 		    arg2, buf, bufsizep));
5775 	} else if (cl == SI_LU) {
5776 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
5777 		    bufsizep));
5778 	}
5779 
5780 	return (STMF_NOT_SUPPORTED);
5781 }
5782 
5783 /*
5784  * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
5785  * stmf to register local ports. The ident should have 20 bytes in buffer
5786  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
5787  */
5788 void
5789 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
5790     uint8_t protocol_id)
5791 {
5792 	char wwn_str[20+1];
5793 
5794 	sdid->protocol_id = protocol_id;
5795 	sdid->piv = 1;
5796 	sdid->code_set = CODE_SET_ASCII;
5797 	sdid->association = ID_IS_TARGET_PORT;
5798 	sdid->ident_length = 20;
5799 	/* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
5800 	(void) snprintf(wwn_str, sizeof (wwn_str),
5801 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
5802 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
5803 	bcopy(wwn_str, (char *)sdid->ident, 20);
5804 }
5805 
5806 
5807 stmf_xfer_data_t *
5808 stmf_prepare_tpgs_data(uint8_t ilu_alua)
5809 {
5810 	stmf_xfer_data_t *xd;
5811 	stmf_i_local_port_t *ilport;
5812 	uint8_t *p;
5813 	uint32_t sz, asz, nports = 0, nports_standby = 0;
5814 
5815 	mutex_enter(&stmf_state.stmf_lock);
5816 	/* check if any ports are standby and create second group */
5817 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5818 	    ilport = ilport->ilport_next) {
5819 		if (ilport->ilport_standby == 1) {
5820 			nports_standby++;
5821 		} else {
5822 			nports++;
5823 		}
5824 	}
5825 
5826 	/* The spec only allows for 255 ports to be reported per group */
5827 	nports = min(nports, 255);
5828 	nports_standby = min(nports_standby, 255);
5829 	sz = (nports * 4) + 12;
5830 	if (nports_standby && ilu_alua) {
5831 		sz += (nports_standby * 4) + 8;
5832 	}
5833 	asz = sz + sizeof (*xd) - 4;
5834 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
5835 	if (xd == NULL) {
5836 		mutex_exit(&stmf_state.stmf_lock);
5837 		return (NULL);
5838 	}
5839 	xd->alloc_size = asz;
5840 	xd->size_left = sz;
5841 
5842 	p = xd->buf;
5843 
5844 	*((uint32_t *)p) = BE_32(sz - 4);
5845 	p += 4;
5846 	p[0] = 0x80;	/* PREF */
5847 	p[1] = 5;	/* AO_SUP, S_SUP */
5848 	if (stmf_state.stmf_alua_node == 1) {
5849 		p[3] = 1;	/* Group 1 */
5850 	} else {
5851 		p[3] = 0;	/* Group 0 */
5852 	}
5853 	p[7] = nports & 0xff;
5854 	p += 8;
5855 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5856 	    ilport = ilport->ilport_next) {
5857 		if (ilport->ilport_standby == 1) {
5858 			continue;
5859 		}
5860 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5861 		p += 4;
5862 	}
5863 	if (nports_standby && ilu_alua) {
5864 		p[0] = 0x02;	/* Non PREF, Standby */
5865 		p[1] = 5;	/* AO_SUP, S_SUP */
5866 		if (stmf_state.stmf_alua_node == 1) {
5867 			p[3] = 0;	/* Group 0 */
5868 		} else {
5869 			p[3] = 1;	/* Group 1 */
5870 		}
5871 		p[7] = nports_standby & 0xff;
5872 		p += 8;
5873 		for (ilport = stmf_state.stmf_ilportlist; ilport;
5874 		    ilport = ilport->ilport_next) {
5875 			if (ilport->ilport_standby == 0) {
5876 				continue;
5877 			}
5878 			((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5879 			p += 4;
5880 		}
5881 	}
5882 
5883 	mutex_exit(&stmf_state.stmf_lock);
5884 
5885 	return (xd);
5886 }
5887 
5888 struct scsi_devid_desc *
5889 stmf_scsilib_get_devid_desc(uint16_t rtpid)
5890 {
5891 	scsi_devid_desc_t *devid = NULL;
5892 	stmf_i_local_port_t *ilport;
5893 
5894 	mutex_enter(&stmf_state.stmf_lock);
5895 
5896 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5897 	    ilport = ilport->ilport_next) {
5898 		if (ilport->ilport_rtpid == rtpid) {
5899 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
5900 			uint32_t id_sz = sizeof (scsi_devid_desc_t) - 1 +
5901 			    id->ident_length;
5902 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
5903 			    KM_NOSLEEP);
5904 			if (devid != NULL) {
5905 				bcopy(id, devid, id_sz);
5906 			}
5907 			break;
5908 		}
5909 	}
5910 
5911 	mutex_exit(&stmf_state.stmf_lock);
5912 	return (devid);
5913 }
5914 
5915 uint16_t
5916 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
5917 {
5918 	stmf_i_local_port_t	*ilport;
5919 	scsi_devid_desc_t	*id;
5920 	uint16_t		rtpid = 0;
5921 
5922 	mutex_enter(&stmf_state.stmf_lock);
5923 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5924 	    ilport = ilport->ilport_next) {
5925 		id = ilport->ilport_lport->lport_id;
5926 		if ((devid->ident_length == id->ident_length) &&
5927 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
5928 			rtpid = ilport->ilport_rtpid;
5929 			break;
5930 		}
5931 	}
5932 	mutex_exit(&stmf_state.stmf_lock);
5933 	return (rtpid);
5934 }
5935 
5936 static uint16_t stmf_lu_id_gen_number = 0;
5937 
5938 stmf_status_t
5939 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
5940 {
5941 	return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id));
5942 }
5943 
5944 stmf_status_t
5945 stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id,
5946     scsi_devid_desc_t *lu_id)
5947 {
5948 	uint8_t *p;
5949 	struct timeval32 timestamp32;
5950 	uint32_t *t = (uint32_t *)&timestamp32;
5951 	struct ether_addr mac;
5952 	uint8_t *e = (uint8_t *)&mac;
5953 	int hid = (int)host_id;
5954 
5955 	if (company_id == COMPANY_ID_NONE)
5956 		company_id = COMPANY_ID_SUN;
5957 
5958 	if (lu_id->ident_length != 0x10)
5959 		return (STMF_INVALID_ARG);
5960 
5961 	p = (uint8_t *)lu_id;
5962 
5963 	atomic_add_16(&stmf_lu_id_gen_number, 1);
5964 
5965 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
5966 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
5967 	p[5] = (company_id >> 12) & 0xff;
5968 	p[6] = (company_id >> 4) & 0xff;
5969 	p[7] = (company_id << 4) & 0xf0;
5970 	if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) {
5971 		hid = BE_32((int)zone_get_hostid(NULL));
5972 	}
5973 	if (hid != 0) {
5974 		e[0] = (hid >> 24) & 0xff;
5975 		e[1] = (hid >> 16) & 0xff;
5976 		e[2] = (hid >> 8) & 0xff;
5977 		e[3] = hid & 0xff;
5978 		e[4] = e[5] = 0;
5979 	}
5980 	bcopy(e, p+8, 6);
5981 	uniqtime32(&timestamp32);
5982 	*t = BE_32(*t);
5983 	bcopy(t, p+14, 4);
5984 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
5985 	p[19] = stmf_lu_id_gen_number & 0xff;
5986 
5987 	return (STMF_SUCCESS);
5988 }
5989 
5990 /*
5991  * saa is sense key, ASC, ASCQ
5992  */
5993 void
5994 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
5995 {
5996 	uint8_t sd[18];
5997 	task->task_scsi_status = st;
5998 	if (st == 2) {
5999 		bzero(sd, 18);
6000 		sd[0] = 0x70;
6001 		sd[2] = (saa >> 16) & 0xf;
6002 		sd[7] = 10;
6003 		sd[12] = (saa >> 8) & 0xff;
6004 		sd[13] = saa & 0xff;
6005 		task->task_sense_data = sd;
6006 		task->task_sense_length = 18;
6007 	} else {
6008 		task->task_sense_data = NULL;
6009 		task->task_sense_length = 0;
6010 	}
6011 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
6012 }
6013 
6014 uint32_t
6015 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
6016     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
6017 {
6018 	uint8_t		*p = NULL;
6019 	uint8_t		small_buf[32];
6020 	uint32_t	sz = 0;
6021 	uint32_t	n = 4;
6022 	uint32_t	m = 0;
6023 	uint32_t	last_bit = 0;
6024 
6025 	if (page_len < 4)
6026 		return (0);
6027 	if (page_len > 65535)
6028 		page_len = 65535;
6029 
6030 	page[0] = byte0;
6031 	page[1] = 0x83;
6032 
6033 	/* CONSTCOND */
6034 	while (1) {
6035 		m += sz;
6036 		if (sz && (page_len > n)) {
6037 			uint32_t copysz;
6038 			copysz = page_len > (n + sz) ? sz : page_len - n;
6039 			bcopy(p, page + n, copysz);
6040 			n += copysz;
6041 		}
6042 		vpd_mask &= ~last_bit;
6043 		if (vpd_mask == 0)
6044 			break;
6045 
6046 		if (vpd_mask & STMF_VPD_LU_ID) {
6047 			last_bit = STMF_VPD_LU_ID;
6048 			sz = task->task_lu->lu_id->ident_length + 4;
6049 			p = (uint8_t *)task->task_lu->lu_id;
6050 			continue;
6051 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
6052 			last_bit = STMF_VPD_TARGET_ID;
6053 			sz = task->task_lport->lport_id->ident_length + 4;
6054 			p = (uint8_t *)task->task_lport->lport_id;
6055 			continue;
6056 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
6057 			stmf_i_local_port_t *ilport;
6058 			last_bit = STMF_VPD_TP_GROUP;
6059 			p = small_buf;
6060 			bzero(p, 8);
6061 			p[0] = 1;
6062 			p[1] = 0x15;
6063 			p[3] = 4;
6064 			ilport = (stmf_i_local_port_t *)
6065 			    task->task_lport->lport_stmf_private;
6066 			/*
6067 			 * If we're in alua mode, group 1 contains all alua
6068 			 * participating ports and all standby ports
6069 			 * > 255. Otherwise, if we're in alua mode, any local
6070 			 * ports (non standby/pppt) are also in group 1 if the
6071 			 * alua node is 1. Otherwise the group is 0.
6072 			 */
6073 			if ((stmf_state.stmf_alua_state &&
6074 			    (ilport->ilport_alua || ilport->ilport_standby) &&
6075 			    ilport->ilport_rtpid > 255) ||
6076 			    (stmf_state.stmf_alua_node == 1 &&
6077 			    ilport->ilport_standby != 1)) {
6078 				p[7] = 1;	/* Group 1 */
6079 			}
6080 			sz = 8;
6081 			continue;
6082 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
6083 			stmf_i_local_port_t *ilport;
6084 
6085 			last_bit = STMF_VPD_RELATIVE_TP_ID;
6086 			p = small_buf;
6087 			bzero(p, 8);
6088 			p[0] = 1;
6089 			p[1] = 0x14;
6090 			p[3] = 4;
6091 			ilport = (stmf_i_local_port_t *)
6092 			    task->task_lport->lport_stmf_private;
6093 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
6094 			p[7] = ilport->ilport_rtpid & 0xff;
6095 			sz = 8;
6096 			continue;
6097 		} else {
6098 			cmn_err(CE_WARN, "Invalid vpd_mask");
6099 			break;
6100 		}
6101 	}
6102 
6103 	page[2] = (m >> 8) & 0xff;
6104 	page[3] = m & 0xff;
6105 
6106 	return (n);
6107 }
6108 
6109 void
6110 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
6111 {
6112 	stmf_i_scsi_task_t *itask =
6113 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
6114 	stmf_i_lu_t *ilu =
6115 	    (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6116 	stmf_xfer_data_t *xd;
6117 	uint32_t sz, minsz;
6118 
6119 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
6120 	task->task_cmd_xfer_length =
6121 	    ((((uint32_t)task->task_cdb[6]) << 24) |
6122 	    (((uint32_t)task->task_cdb[7]) << 16) |
6123 	    (((uint32_t)task->task_cdb[8]) << 8) |
6124 	    ((uint32_t)task->task_cdb[9]));
6125 
6126 	if (task->task_additional_flags &
6127 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6128 		task->task_expected_xfer_length =
6129 		    task->task_cmd_xfer_length;
6130 	}
6131 
6132 	if (task->task_cmd_xfer_length == 0) {
6133 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6134 		return;
6135 	}
6136 	if (task->task_cmd_xfer_length < 4) {
6137 		stmf_scsilib_send_status(task, STATUS_CHECK,
6138 		    STMF_SAA_INVALID_FIELD_IN_CDB);
6139 		return;
6140 	}
6141 
6142 	sz = min(task->task_expected_xfer_length,
6143 	    task->task_cmd_xfer_length);
6144 
6145 	xd = stmf_prepare_tpgs_data(ilu->ilu_alua);
6146 
6147 	if (xd == NULL) {
6148 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6149 		    STMF_ALLOC_FAILURE, NULL);
6150 		return;
6151 	}
6152 
6153 	sz = min(sz, xd->size_left);
6154 	xd->size_left = sz;
6155 	minsz = min(512, sz);
6156 
6157 	if (dbuf == NULL)
6158 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
6159 	if (dbuf == NULL) {
6160 		kmem_free(xd, xd->alloc_size);
6161 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6162 		    STMF_ALLOC_FAILURE, NULL);
6163 		return;
6164 	}
6165 	dbuf->db_lu_private = xd;
6166 	stmf_xd_to_dbuf(dbuf);
6167 
6168 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6169 	(void) stmf_xfer_data(task, dbuf, 0);
6170 
6171 }
6172 
6173 void
6174 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
6175 {
6176 
6177 	switch (task->task_mgmt_function) {
6178 	/*
6179 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
6180 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
6181 	 * in these cases. This needs to be changed to abort only the required
6182 	 * set.
6183 	 */
6184 	case TM_ABORT_TASK:
6185 	case TM_ABORT_TASK_SET:
6186 	case TM_CLEAR_TASK_SET:
6187 	case TM_LUN_RESET:
6188 		stmf_handle_lun_reset(task);
6189 		/* issue the reset to the proxy node as well */
6190 		if (stmf_state.stmf_alua_state == 1) {
6191 			(void) stmf_proxy_scsi_cmd(task, NULL);
6192 		}
6193 		return;
6194 	case TM_TARGET_RESET:
6195 	case TM_TARGET_COLD_RESET:
6196 	case TM_TARGET_WARM_RESET:
6197 		stmf_handle_target_reset(task);
6198 		return;
6199 	default:
6200 		/* We dont support this task mgmt function */
6201 		stmf_scsilib_send_status(task, STATUS_CHECK,
6202 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
6203 		return;
6204 	}
6205 }
6206 
6207 void
6208 stmf_handle_lun_reset(scsi_task_t *task)
6209 {
6210 	stmf_i_scsi_task_t *itask;
6211 	stmf_i_lu_t *ilu;
6212 
6213 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6214 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6215 
6216 	/*
6217 	 * To sync with target reset, grab this lock. The LU is not going
6218 	 * anywhere as there is atleast one task pending (this task).
6219 	 */
6220 	mutex_enter(&stmf_state.stmf_lock);
6221 
6222 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6223 		mutex_exit(&stmf_state.stmf_lock);
6224 		stmf_scsilib_send_status(task, STATUS_CHECK,
6225 		    STMF_SAA_OPERATION_IN_PROGRESS);
6226 		return;
6227 	}
6228 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6229 	mutex_exit(&stmf_state.stmf_lock);
6230 
6231 	/*
6232 	 * Mark this task as the one causing LU reset so that we know who
6233 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
6234 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
6235 	 */
6236 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
6237 
6238 	/* Initiatiate abort on all commands on this LU except this one */
6239 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
6240 
6241 	/* Start polling on this task */
6242 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6243 	    != STMF_SUCCESS) {
6244 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6245 		    NULL);
6246 		return;
6247 	}
6248 }
6249 
6250 void
6251 stmf_handle_target_reset(scsi_task_t *task)
6252 {
6253 	stmf_i_scsi_task_t *itask;
6254 	stmf_i_lu_t *ilu;
6255 	stmf_i_scsi_session_t *iss;
6256 	stmf_lun_map_t *lm;
6257 	stmf_lun_map_ent_t *lm_ent;
6258 	int i, lf;
6259 
6260 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6261 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
6262 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6263 
6264 	/*
6265 	 * To sync with LUN reset, grab this lock. The session is not going
6266 	 * anywhere as there is atleast one task pending (this task).
6267 	 */
6268 	mutex_enter(&stmf_state.stmf_lock);
6269 
6270 	/* Grab the session lock as a writer to prevent any changes in it */
6271 	rw_enter(iss->iss_lockp, RW_WRITER);
6272 
6273 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
6274 		rw_exit(iss->iss_lockp);
6275 		mutex_exit(&stmf_state.stmf_lock);
6276 		stmf_scsilib_send_status(task, STATUS_CHECK,
6277 		    STMF_SAA_OPERATION_IN_PROGRESS);
6278 		return;
6279 	}
6280 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
6281 
6282 	/*
6283 	 * Now go through each LUN in this session and make sure all of them
6284 	 * can be reset.
6285 	 */
6286 	lm = iss->iss_sm;
6287 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
6288 		if (lm->lm_plus[i] == NULL)
6289 			continue;
6290 		lf++;
6291 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6292 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6293 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6294 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6295 			rw_exit(iss->iss_lockp);
6296 			mutex_exit(&stmf_state.stmf_lock);
6297 			stmf_scsilib_send_status(task, STATUS_CHECK,
6298 			    STMF_SAA_OPERATION_IN_PROGRESS);
6299 			return;
6300 		}
6301 	}
6302 	if (lf == 0) {
6303 		/* No luns in this session */
6304 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6305 		rw_exit(iss->iss_lockp);
6306 		mutex_exit(&stmf_state.stmf_lock);
6307 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6308 		return;
6309 	}
6310 
6311 	/* ok, start the damage */
6312 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
6313 	    ITASK_CAUSING_TARGET_RESET;
6314 	for (i = 0; i < lm->lm_nentries; i++) {
6315 		if (lm->lm_plus[i] == NULL)
6316 			continue;
6317 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6318 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6319 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6320 	}
6321 
6322 	for (i = 0; i < lm->lm_nentries; i++) {
6323 		if (lm->lm_plus[i] == NULL)
6324 			continue;
6325 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6326 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
6327 		    lm_ent->ent_lu);
6328 	}
6329 
6330 	rw_exit(iss->iss_lockp);
6331 	mutex_exit(&stmf_state.stmf_lock);
6332 
6333 	/* Start polling on this task */
6334 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6335 	    != STMF_SUCCESS) {
6336 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6337 		    NULL);
6338 		return;
6339 	}
6340 }
6341 
6342 int
6343 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
6344 {
6345 	scsi_task_t *task = itask->itask_task;
6346 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6347 	    task->task_session->ss_stmf_private;
6348 
6349 	rw_enter(iss->iss_lockp, RW_WRITER);
6350 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
6351 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
6352 		rw_exit(iss->iss_lockp);
6353 		return (0);
6354 	}
6355 	atomic_and_32(&iss->iss_flags,
6356 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6357 	rw_exit(iss->iss_lockp);
6358 
6359 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
6360 		return (0);
6361 	}
6362 	stmf_scsilib_send_status(task, STATUS_CHECK,
6363 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
6364 	return (1);
6365 }
6366 
6367 void
6368 stmf_worker_init()
6369 {
6370 	uint32_t i;
6371 
6372 	/* Make local copy of global tunables */
6373 	stmf_i_max_nworkers = stmf_max_nworkers;
6374 	stmf_i_min_nworkers = stmf_min_nworkers;
6375 
6376 	ASSERT(stmf_workers == NULL);
6377 	if (stmf_i_min_nworkers < 4) {
6378 		stmf_i_min_nworkers = 4;
6379 	}
6380 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
6381 		stmf_i_max_nworkers = stmf_i_min_nworkers;
6382 	}
6383 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
6384 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
6385 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6386 		stmf_worker_t *w = &stmf_workers[i];
6387 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
6388 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
6389 	}
6390 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6391 	stmf_workers_state = STMF_WORKERS_ENABLED;
6392 
6393 	/* Workers will be started by stmf_worker_mgmt() */
6394 
6395 	/* Lets wait for atleast one worker to start */
6396 	while (stmf_nworkers_cur == 0)
6397 		delay(drv_usectohz(20 * 1000));
6398 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
6399 }
6400 
6401 stmf_status_t
6402 stmf_worker_fini()
6403 {
6404 	int i;
6405 	clock_t sb;
6406 
6407 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
6408 		return (STMF_SUCCESS);
6409 	ASSERT(stmf_workers);
6410 	stmf_workers_state = STMF_WORKERS_DISABLED;
6411 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6412 	cv_signal(&stmf_state.stmf_cv);
6413 
6414 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
6415 	/* Wait for all the threads to die */
6416 	while (stmf_nworkers_cur != 0) {
6417 		if (ddi_get_lbolt() > sb) {
6418 			stmf_workers_state = STMF_WORKERS_ENABLED;
6419 			return (STMF_BUSY);
6420 		}
6421 		delay(drv_usectohz(100 * 1000));
6422 	}
6423 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6424 		stmf_worker_t *w = &stmf_workers[i];
6425 		mutex_destroy(&w->worker_lock);
6426 		cv_destroy(&w->worker_cv);
6427 	}
6428 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
6429 	stmf_workers = NULL;
6430 
6431 	return (STMF_SUCCESS);
6432 }
6433 
6434 void
6435 stmf_worker_task(void *arg)
6436 {
6437 	stmf_worker_t *w;
6438 	stmf_i_scsi_session_t *iss;
6439 	scsi_task_t *task;
6440 	stmf_i_scsi_task_t *itask;
6441 	stmf_data_buf_t *dbuf;
6442 	stmf_lu_t *lu;
6443 	clock_t wait_timer = 0;
6444 	clock_t wait_ticks, wait_delta = 0;
6445 	uint32_t old, new;
6446 	uint8_t curcmd;
6447 	uint8_t abort_free;
6448 	uint8_t wait_queue;
6449 	uint8_t dec_qdepth;
6450 
6451 	w = (stmf_worker_t *)arg;
6452 	wait_ticks = drv_usectohz(10000);
6453 
6454 	DTRACE_PROBE1(worker__create, stmf_worker_t, w);
6455 	mutex_enter(&w->worker_lock);
6456 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
6457 stmf_worker_loop:;
6458 	if ((w->worker_ref_count == 0) &&
6459 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
6460 		w->worker_flags &= ~(STMF_WORKER_STARTED |
6461 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
6462 		w->worker_tid = NULL;
6463 		mutex_exit(&w->worker_lock);
6464 		DTRACE_PROBE1(worker__destroy, stmf_worker_t, w);
6465 		thread_exit();
6466 	}
6467 	/* CONSTCOND */
6468 	while (1) {
6469 		dec_qdepth = 0;
6470 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
6471 			wait_timer = 0;
6472 			wait_delta = 0;
6473 			if (w->worker_wait_head) {
6474 				ASSERT(w->worker_wait_tail);
6475 				if (w->worker_task_head == NULL)
6476 					w->worker_task_head =
6477 					    w->worker_wait_head;
6478 				else
6479 					w->worker_task_tail->itask_worker_next =
6480 					    w->worker_wait_head;
6481 				w->worker_task_tail = w->worker_wait_tail;
6482 				w->worker_wait_head = w->worker_wait_tail =
6483 				    NULL;
6484 			}
6485 		}
6486 		if ((itask = w->worker_task_head) == NULL) {
6487 			break;
6488 		}
6489 		task = itask->itask_task;
6490 		DTRACE_PROBE2(worker__active, stmf_worker_t, w,
6491 		    scsi_task_t *, task);
6492 		w->worker_task_head = itask->itask_worker_next;
6493 		if (w->worker_task_head == NULL)
6494 			w->worker_task_tail = NULL;
6495 
6496 		wait_queue = 0;
6497 		abort_free = 0;
6498 		if (itask->itask_ncmds > 0) {
6499 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
6500 		} else {
6501 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
6502 		}
6503 		do {
6504 			old = itask->itask_flags;
6505 			if (old & ITASK_BEING_ABORTED) {
6506 				itask->itask_ncmds = 1;
6507 				curcmd = itask->itask_cmd_stack[0] =
6508 				    ITASK_CMD_ABORT;
6509 				goto out_itask_flag_loop;
6510 			} else if ((curcmd & ITASK_CMD_MASK) ==
6511 			    ITASK_CMD_NEW_TASK) {
6512 				/*
6513 				 * set ITASK_KSTAT_IN_RUNQ, this flag
6514 				 * will not reset until task completed
6515 				 */
6516 				new = old | ITASK_KNOWN_TO_LU |
6517 				    ITASK_KSTAT_IN_RUNQ;
6518 			} else {
6519 				goto out_itask_flag_loop;
6520 			}
6521 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
6522 
6523 out_itask_flag_loop:
6524 
6525 		/*
6526 		 * Decide if this task needs to go to a queue and/or if
6527 		 * we can decrement the itask_cmd_stack.
6528 		 */
6529 		if (curcmd == ITASK_CMD_ABORT) {
6530 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
6531 			    ITASK_KNOWN_TO_TGT_PORT)) {
6532 				wait_queue = 1;
6533 			} else {
6534 				abort_free = 1;
6535 			}
6536 		} else if ((curcmd & ITASK_CMD_POLL) &&
6537 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
6538 			wait_queue = 1;
6539 		}
6540 
6541 		if (wait_queue) {
6542 			itask->itask_worker_next = NULL;
6543 			if (w->worker_wait_tail) {
6544 				w->worker_wait_tail->itask_worker_next = itask;
6545 			} else {
6546 				w->worker_wait_head = itask;
6547 			}
6548 			w->worker_wait_tail = itask;
6549 			if (wait_timer == 0) {
6550 				wait_timer = ddi_get_lbolt() + wait_ticks;
6551 				wait_delta = wait_ticks;
6552 			}
6553 		} else if ((--(itask->itask_ncmds)) != 0) {
6554 			itask->itask_worker_next = NULL;
6555 			if (w->worker_task_tail) {
6556 				w->worker_task_tail->itask_worker_next = itask;
6557 			} else {
6558 				w->worker_task_head = itask;
6559 			}
6560 			w->worker_task_tail = itask;
6561 		} else {
6562 			atomic_and_32(&itask->itask_flags,
6563 			    ~ITASK_IN_WORKER_QUEUE);
6564 			/*
6565 			 * This is where the queue depth should go down by
6566 			 * one but we delay that on purpose to account for
6567 			 * the call into the provider. The actual decrement
6568 			 * happens after the worker has done its job.
6569 			 */
6570 			dec_qdepth = 1;
6571 			itask->itask_waitq_time +=
6572 			    gethrtime() - itask->itask_waitq_enter_timestamp;
6573 		}
6574 
6575 		/* We made it here means we are going to call LU */
6576 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
6577 			lu = task->task_lu;
6578 		else
6579 			lu = dlun0;
6580 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
6581 		mutex_exit(&w->worker_lock);
6582 		curcmd &= ITASK_CMD_MASK;
6583 		switch (curcmd) {
6584 		case ITASK_CMD_NEW_TASK:
6585 			iss = (stmf_i_scsi_session_t *)
6586 			    task->task_session->ss_stmf_private;
6587 			stmf_itl_lu_new_task(itask);
6588 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
6589 				if (stmf_handle_cmd_during_ic(itask))
6590 					break;
6591 			}
6592 #ifdef	DEBUG
6593 			if (stmf_drop_task_counter > 0) {
6594 				if (atomic_add_32_nv(
6595 				    (uint32_t *)&stmf_drop_task_counter,
6596 				    -1) == 1) {
6597 					break;
6598 				}
6599 			}
6600 #endif
6601 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
6602 			lu->lu_new_task(task, dbuf);
6603 			break;
6604 		case ITASK_CMD_DATA_XFER_DONE:
6605 			lu->lu_dbuf_xfer_done(task, dbuf);
6606 			break;
6607 		case ITASK_CMD_STATUS_DONE:
6608 			lu->lu_send_status_done(task);
6609 			break;
6610 		case ITASK_CMD_ABORT:
6611 			if (abort_free) {
6612 				stmf_task_free(task);
6613 			} else {
6614 				stmf_do_task_abort(task);
6615 			}
6616 			break;
6617 		case ITASK_CMD_POLL_LU:
6618 			if (!wait_queue) {
6619 				lu->lu_task_poll(task);
6620 			}
6621 			break;
6622 		case ITASK_CMD_POLL_LPORT:
6623 			if (!wait_queue)
6624 				task->task_lport->lport_task_poll(task);
6625 			break;
6626 		case ITASK_CMD_SEND_STATUS:
6627 		/* case ITASK_CMD_XFER_DATA: */
6628 			break;
6629 		}
6630 		mutex_enter(&w->worker_lock);
6631 		if (dec_qdepth) {
6632 			w->worker_queue_depth--;
6633 		}
6634 	}
6635 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
6636 		if (w->worker_ref_count == 0)
6637 			goto stmf_worker_loop;
6638 		else {
6639 			wait_timer = ddi_get_lbolt() + 1;
6640 			wait_delta = 1;
6641 		}
6642 	}
6643 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
6644 	if (wait_timer) {
6645 		DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w);
6646 		(void) cv_reltimedwait(&w->worker_cv, &w->worker_lock,
6647 		    wait_delta, TR_CLOCK_TICK);
6648 	} else {
6649 		DTRACE_PROBE1(worker__sleep, stmf_worker_t, w);
6650 		cv_wait(&w->worker_cv, &w->worker_lock);
6651 	}
6652 	DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w);
6653 	w->worker_flags |= STMF_WORKER_ACTIVE;
6654 	goto stmf_worker_loop;
6655 }
6656 
6657 void
6658 stmf_worker_mgmt()
6659 {
6660 	int i;
6661 	int workers_needed;
6662 	uint32_t qd;
6663 	clock_t tps, d = 0;
6664 	uint32_t cur_max_ntasks = 0;
6665 	stmf_worker_t *w;
6666 
6667 	/* Check if we are trying to increase the # of threads */
6668 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
6669 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
6670 			stmf_nworkers_cur++;
6671 			stmf_nworkers_accepting_cmds++;
6672 		} else {
6673 			/* Wait for transition to complete */
6674 			return;
6675 		}
6676 	}
6677 	/* Check if we are trying to decrease the # of workers */
6678 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6679 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
6680 			stmf_nworkers_cur--;
6681 			/*
6682 			 * stmf_nworkers_accepting_cmds has already been
6683 			 * updated by the request to reduce the # of workers.
6684 			 */
6685 		} else {
6686 			/* Wait for transition to complete */
6687 			return;
6688 		}
6689 	}
6690 	/* Check if we are being asked to quit */
6691 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
6692 		if (stmf_nworkers_cur) {
6693 			workers_needed = 0;
6694 			goto worker_mgmt_trigger_change;
6695 		}
6696 		return;
6697 	}
6698 	/* Check if we are starting */
6699 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
6700 		workers_needed = stmf_i_min_nworkers;
6701 		goto worker_mgmt_trigger_change;
6702 	}
6703 
6704 	tps = drv_usectohz(1 * 1000 * 1000);
6705 	if ((stmf_wm_last != 0) &&
6706 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
6707 		qd = 0;
6708 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
6709 			qd += stmf_workers[i].worker_max_qdepth_pu;
6710 			stmf_workers[i].worker_max_qdepth_pu = 0;
6711 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
6712 			    cur_max_ntasks) {
6713 				cur_max_ntasks =
6714 				    stmf_workers[i].worker_max_sys_qdepth_pu;
6715 			}
6716 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
6717 		}
6718 	}
6719 	stmf_wm_last = ddi_get_lbolt();
6720 	if (d <= tps) {
6721 		/* still ramping up */
6722 		return;
6723 	}
6724 	/* max qdepth cannot be more than max tasks */
6725 	if (qd > cur_max_ntasks)
6726 		qd = cur_max_ntasks;
6727 
6728 	/* See if we have more workers */
6729 	if (qd < stmf_nworkers_accepting_cmds) {
6730 		/*
6731 		 * Since we dont reduce the worker count right away, monitor
6732 		 * the highest load during the scale_down_delay.
6733 		 */
6734 		if (qd > stmf_worker_scale_down_qd)
6735 			stmf_worker_scale_down_qd = qd;
6736 		if (stmf_worker_scale_down_timer == 0) {
6737 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
6738 			    drv_usectohz(stmf_worker_scale_down_delay *
6739 			    1000 * 1000);
6740 			return;
6741 		}
6742 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
6743 			return;
6744 		}
6745 		/* Its time to reduce the workers */
6746 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
6747 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
6748 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
6749 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
6750 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
6751 			return;
6752 		workers_needed = stmf_worker_scale_down_qd;
6753 		stmf_worker_scale_down_qd = 0;
6754 		goto worker_mgmt_trigger_change;
6755 	}
6756 	stmf_worker_scale_down_qd = 0;
6757 	stmf_worker_scale_down_timer = 0;
6758 	if (qd > stmf_i_max_nworkers)
6759 		qd = stmf_i_max_nworkers;
6760 	if (qd < stmf_i_min_nworkers)
6761 		qd = stmf_i_min_nworkers;
6762 	if (qd == stmf_nworkers_cur)
6763 		return;
6764 	workers_needed = qd;
6765 	goto worker_mgmt_trigger_change;
6766 
6767 	/* NOTREACHED */
6768 	return;
6769 
6770 worker_mgmt_trigger_change:
6771 	ASSERT(workers_needed != stmf_nworkers_cur);
6772 	if (workers_needed > stmf_nworkers_cur) {
6773 		stmf_nworkers_needed = workers_needed;
6774 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
6775 			w = &stmf_workers[i];
6776 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
6777 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
6778 			    minclsyspri);
6779 		}
6780 		return;
6781 	}
6782 	/* At this point we know that we are decreasing the # of workers */
6783 	stmf_nworkers_accepting_cmds = workers_needed;
6784 	stmf_nworkers_needed = workers_needed;
6785 	/* Signal the workers that its time to quit */
6786 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6787 		w = &stmf_workers[i];
6788 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
6789 		mutex_enter(&w->worker_lock);
6790 		w->worker_flags |= STMF_WORKER_TERMINATE;
6791 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
6792 			cv_signal(&w->worker_cv);
6793 		mutex_exit(&w->worker_lock);
6794 	}
6795 }
6796 
6797 /*
6798  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
6799  * If all the data has been filled out, frees the xd and makes
6800  * db_lu_private NULL.
6801  */
6802 void
6803 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf)
6804 {
6805 	stmf_xfer_data_t *xd;
6806 	uint8_t *p;
6807 	int i;
6808 	uint32_t s;
6809 
6810 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6811 	dbuf->db_data_size = 0;
6812 	dbuf->db_relative_offset = xd->size_done;
6813 	for (i = 0; i < dbuf->db_sglist_length; i++) {
6814 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
6815 		p = &xd->buf[xd->size_done];
6816 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
6817 		xd->size_left -= s;
6818 		xd->size_done += s;
6819 		dbuf->db_data_size += s;
6820 		if (xd->size_left == 0) {
6821 			kmem_free(xd, xd->alloc_size);
6822 			dbuf->db_lu_private = NULL;
6823 			return;
6824 		}
6825 	}
6826 }
6827 
6828 /* ARGSUSED */
6829 stmf_status_t
6830 stmf_dlun0_task_alloc(scsi_task_t *task)
6831 {
6832 	return (STMF_SUCCESS);
6833 }
6834 
6835 void
6836 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
6837 {
6838 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
6839 	stmf_i_scsi_session_t *iss;
6840 	uint32_t sz, minsz;
6841 	uint8_t *p;
6842 	stmf_xfer_data_t *xd;
6843 	uint8_t inq_page_length = 31;
6844 
6845 	if (task->task_mgmt_function) {
6846 		stmf_scsilib_handle_task_mgmt(task);
6847 		return;
6848 	}
6849 
6850 	switch (cdbp[0]) {
6851 	case SCMD_INQUIRY:
6852 		/*
6853 		 * Basic protocol checks.  In addition, only reply to
6854 		 * standard inquiry.  Otherwise, the LU provider needs
6855 		 * to respond.
6856 		 */
6857 
6858 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
6859 			stmf_scsilib_send_status(task, STATUS_CHECK,
6860 			    STMF_SAA_INVALID_FIELD_IN_CDB);
6861 			return;
6862 		}
6863 
6864 		task->task_cmd_xfer_length =
6865 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
6866 
6867 		if (task->task_additional_flags &
6868 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6869 			task->task_expected_xfer_length =
6870 			    task->task_cmd_xfer_length;
6871 		}
6872 
6873 		sz = min(task->task_expected_xfer_length,
6874 		    min(36, task->task_cmd_xfer_length));
6875 		minsz = 36;
6876 
6877 		if (sz == 0) {
6878 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6879 			return;
6880 		}
6881 
6882 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
6883 			/*
6884 			 * Ignore any preallocated dbuf if the size is less
6885 			 * than 36. It will be freed during the task_free.
6886 			 */
6887 			dbuf = NULL;
6888 		}
6889 		if (dbuf == NULL)
6890 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
6891 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
6892 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6893 			    STMF_ALLOC_FAILURE, NULL);
6894 			return;
6895 		}
6896 		dbuf->db_lu_private = NULL;
6897 
6898 		p = dbuf->db_sglist[0].seg_addr;
6899 
6900 		/*
6901 		 * Standard inquiry handling only.
6902 		 */
6903 
6904 		bzero(p, inq_page_length + 5);
6905 
6906 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
6907 		p[2] = 5;
6908 		p[3] = 0x12;
6909 		p[4] = inq_page_length;
6910 		p[6] = 0x80;
6911 
6912 		(void) strncpy((char *)p+8, "SUN     ", 8);
6913 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
6914 		(void) strncpy((char *)p+32, "1.0 ", 4);
6915 
6916 		dbuf->db_data_size = sz;
6917 		dbuf->db_relative_offset = 0;
6918 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6919 		(void) stmf_xfer_data(task, dbuf, 0);
6920 
6921 		return;
6922 
6923 	case SCMD_REPORT_LUNS:
6924 		task->task_cmd_xfer_length =
6925 		    ((((uint32_t)task->task_cdb[6]) << 24) |
6926 		    (((uint32_t)task->task_cdb[7]) << 16) |
6927 		    (((uint32_t)task->task_cdb[8]) << 8) |
6928 		    ((uint32_t)task->task_cdb[9]));
6929 
6930 		if (task->task_additional_flags &
6931 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6932 			task->task_expected_xfer_length =
6933 			    task->task_cmd_xfer_length;
6934 		}
6935 
6936 		sz = min(task->task_expected_xfer_length,
6937 		    task->task_cmd_xfer_length);
6938 
6939 		if (sz < 16) {
6940 			stmf_scsilib_send_status(task, STATUS_CHECK,
6941 			    STMF_SAA_INVALID_FIELD_IN_CDB);
6942 			return;
6943 		}
6944 
6945 		iss = (stmf_i_scsi_session_t *)
6946 		    task->task_session->ss_stmf_private;
6947 		rw_enter(iss->iss_lockp, RW_WRITER);
6948 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
6949 		rw_exit(iss->iss_lockp);
6950 
6951 		if (xd == NULL) {
6952 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6953 			    STMF_ALLOC_FAILURE, NULL);
6954 			return;
6955 		}
6956 
6957 		sz = min(sz, xd->size_left);
6958 		xd->size_left = sz;
6959 		minsz = min(512, sz);
6960 
6961 		if (dbuf == NULL)
6962 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
6963 		if (dbuf == NULL) {
6964 			kmem_free(xd, xd->alloc_size);
6965 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6966 			    STMF_ALLOC_FAILURE, NULL);
6967 			return;
6968 		}
6969 		dbuf->db_lu_private = xd;
6970 		stmf_xd_to_dbuf(dbuf);
6971 
6972 		atomic_and_32(&iss->iss_flags,
6973 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6974 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6975 		(void) stmf_xfer_data(task, dbuf, 0);
6976 		return;
6977 	}
6978 
6979 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
6980 }
6981 
6982 void
6983 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
6984 {
6985 	stmf_i_scsi_task_t *itask =
6986 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
6987 
6988 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
6989 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6990 		    dbuf->db_xfer_status, NULL);
6991 		return;
6992 	}
6993 	task->task_nbytes_transferred = dbuf->db_data_size;
6994 	if (dbuf->db_lu_private) {
6995 		/* There is more */
6996 		stmf_xd_to_dbuf(dbuf);
6997 		(void) stmf_xfer_data(task, dbuf, 0);
6998 		return;
6999 	}
7000 	/*
7001 	 * If this is a proxy task, it will need to be completed from the
7002 	 * proxy port provider. This message lets pppt know that the xfer
7003 	 * is complete. When we receive the status from pppt, we will
7004 	 * then relay that status back to the lport.
7005 	 */
7006 	if (itask->itask_flags & ITASK_PROXY_TASK) {
7007 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
7008 		stmf_status_t ic_ret = STMF_FAILURE;
7009 		uint64_t session_msg_id;
7010 		mutex_enter(&stmf_state.stmf_lock);
7011 		session_msg_id = stmf_proxy_msg_id++;
7012 		mutex_exit(&stmf_state.stmf_lock);
7013 		/* send xfer done status to pppt */
7014 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
7015 		    itask->itask_proxy_msg_id,
7016 		    task->task_session->ss_session_id,
7017 		    STMF_SUCCESS, session_msg_id);
7018 		if (ic_xfer_done_msg) {
7019 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
7020 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
7021 				cmn_err(CE_WARN, "unable to xmit session msg");
7022 			}
7023 		}
7024 		/* task will be completed from pppt */
7025 		return;
7026 	}
7027 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7028 }
7029 
7030 /* ARGSUSED */
7031 void
7032 stmf_dlun0_status_done(scsi_task_t *task)
7033 {
7034 }
7035 
7036 /* ARGSUSED */
7037 void
7038 stmf_dlun0_task_free(scsi_task_t *task)
7039 {
7040 }
7041 
7042 /* ARGSUSED */
7043 stmf_status_t
7044 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
7045 {
7046 	scsi_task_t *task = (scsi_task_t *)arg;
7047 	stmf_i_scsi_task_t *itask =
7048 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
7049 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7050 	int i;
7051 	uint8_t map;
7052 
7053 	if ((task->task_mgmt_function) && (itask->itask_flags &
7054 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
7055 		switch (task->task_mgmt_function) {
7056 		case TM_ABORT_TASK:
7057 		case TM_ABORT_TASK_SET:
7058 		case TM_CLEAR_TASK_SET:
7059 		case TM_LUN_RESET:
7060 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7061 			break;
7062 		case TM_TARGET_RESET:
7063 		case TM_TARGET_COLD_RESET:
7064 		case TM_TARGET_WARM_RESET:
7065 			stmf_abort_target_reset(task);
7066 			break;
7067 		}
7068 		return (STMF_ABORT_SUCCESS);
7069 	}
7070 
7071 	/*
7072 	 * OK so its not a task mgmt. Make sure we free any xd sitting
7073 	 * inside any dbuf.
7074 	 */
7075 	if ((map = itask->itask_allocated_buf_map) != 0) {
7076 		for (i = 0; i < 4; i++) {
7077 			if ((map & 1) &&
7078 			    ((itask->itask_dbufs[i])->db_lu_private)) {
7079 				stmf_xfer_data_t *xd;
7080 				stmf_data_buf_t *dbuf;
7081 
7082 				dbuf = itask->itask_dbufs[i];
7083 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
7084 				dbuf->db_lu_private = NULL;
7085 				kmem_free(xd, xd->alloc_size);
7086 			}
7087 			map >>= 1;
7088 		}
7089 	}
7090 	return (STMF_ABORT_SUCCESS);
7091 }
7092 
7093 void
7094 stmf_dlun0_task_poll(struct scsi_task *task)
7095 {
7096 	/* Right now we only do this for handling task management functions */
7097 	ASSERT(task->task_mgmt_function);
7098 
7099 	switch (task->task_mgmt_function) {
7100 	case TM_ABORT_TASK:
7101 	case TM_ABORT_TASK_SET:
7102 	case TM_CLEAR_TASK_SET:
7103 	case TM_LUN_RESET:
7104 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
7105 		return;
7106 	case TM_TARGET_RESET:
7107 	case TM_TARGET_COLD_RESET:
7108 	case TM_TARGET_WARM_RESET:
7109 		stmf_target_reset_poll(task);
7110 		return;
7111 	}
7112 }
7113 
7114 /* ARGSUSED */
7115 void
7116 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
7117 {
7118 	/* This function will never be called */
7119 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
7120 }
7121 
7122 void
7123 stmf_dlun_init()
7124 {
7125 	stmf_i_lu_t *ilu;
7126 
7127 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
7128 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
7129 	dlun0->lu_new_task = stmf_dlun0_new_task;
7130 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
7131 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
7132 	dlun0->lu_task_free = stmf_dlun0_task_free;
7133 	dlun0->lu_abort = stmf_dlun0_abort;
7134 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
7135 	dlun0->lu_ctl = stmf_dlun0_ctl;
7136 
7137 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
7138 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
7139 }
7140 
7141 stmf_status_t
7142 stmf_dlun_fini()
7143 {
7144 	stmf_i_lu_t *ilu;
7145 
7146 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
7147 
7148 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
7149 	if (ilu->ilu_ntasks) {
7150 		stmf_i_scsi_task_t *itask, *nitask;
7151 
7152 		nitask = ilu->ilu_tasks;
7153 		do {
7154 			itask = nitask;
7155 			nitask = itask->itask_lu_next;
7156 			dlun0->lu_task_free(itask->itask_task);
7157 			stmf_free(itask->itask_task);
7158 		} while (nitask != NULL);
7159 
7160 	}
7161 	stmf_free(dlun0);
7162 	return (STMF_SUCCESS);
7163 }
7164 
7165 void
7166 stmf_abort_target_reset(scsi_task_t *task)
7167 {
7168 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7169 	    task->task_session->ss_stmf_private;
7170 	stmf_lun_map_t *lm;
7171 	stmf_lun_map_ent_t *lm_ent;
7172 	stmf_i_lu_t *ilu;
7173 	int i;
7174 
7175 	rw_enter(iss->iss_lockp, RW_READER);
7176 	lm = iss->iss_sm;
7177 	for (i = 0; i < lm->lm_nentries; i++) {
7178 		if (lm->lm_plus[i] == NULL)
7179 			continue;
7180 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7181 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7182 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7183 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7184 		}
7185 	}
7186 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7187 	rw_exit(iss->iss_lockp);
7188 }
7189 
7190 /*
7191  * The return value is only used by function managing target reset.
7192  */
7193 stmf_status_t
7194 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
7195 {
7196 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7197 	int ntasks_pending;
7198 
7199 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
7200 	/*
7201 	 * This function is also used during Target reset. The idea is that
7202 	 * once all the commands are aborted, call the LU's reset entry
7203 	 * point (abort entry point with a reset flag). But if this Task
7204 	 * mgmt is running on this LU then all the tasks cannot be aborted.
7205 	 * one task (this task) will still be running which is OK.
7206 	 */
7207 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
7208 	    (ntasks_pending == 1))) {
7209 		stmf_status_t ret;
7210 
7211 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
7212 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
7213 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
7214 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
7215 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
7216 		} else {
7217 			ret = STMF_SUCCESS;
7218 		}
7219 		if (ret == STMF_SUCCESS) {
7220 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
7221 		}
7222 		if (target_reset) {
7223 			return (ret);
7224 		}
7225 		if (ret == STMF_SUCCESS) {
7226 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7227 			return (ret);
7228 		}
7229 		if (ret != STMF_BUSY) {
7230 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
7231 			return (ret);
7232 		}
7233 	}
7234 
7235 	if (target_reset) {
7236 		/* Tell target reset polling code that we are not done */
7237 		return (STMF_BUSY);
7238 	}
7239 
7240 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7241 	    != STMF_SUCCESS) {
7242 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7243 		    STMF_ALLOC_FAILURE, NULL);
7244 		return (STMF_SUCCESS);
7245 	}
7246 
7247 	return (STMF_SUCCESS);
7248 }
7249 
7250 void
7251 stmf_target_reset_poll(struct scsi_task *task)
7252 {
7253 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7254 	    task->task_session->ss_stmf_private;
7255 	stmf_lun_map_t *lm;
7256 	stmf_lun_map_ent_t *lm_ent;
7257 	stmf_i_lu_t *ilu;
7258 	stmf_status_t ret;
7259 	int i;
7260 	int not_done = 0;
7261 
7262 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
7263 
7264 	rw_enter(iss->iss_lockp, RW_READER);
7265 	lm = iss->iss_sm;
7266 	for (i = 0; i < lm->lm_nentries; i++) {
7267 		if (lm->lm_plus[i] == NULL)
7268 			continue;
7269 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7270 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7271 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7272 			rw_exit(iss->iss_lockp);
7273 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
7274 			rw_enter(iss->iss_lockp, RW_READER);
7275 			if (ret == STMF_SUCCESS)
7276 				continue;
7277 			not_done = 1;
7278 			if (ret != STMF_BUSY) {
7279 				rw_exit(iss->iss_lockp);
7280 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7281 				    STMF_ABORTED, NULL);
7282 				return;
7283 			}
7284 		}
7285 	}
7286 	rw_exit(iss->iss_lockp);
7287 
7288 	if (not_done) {
7289 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7290 		    != STMF_SUCCESS) {
7291 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7292 			    STMF_ALLOC_FAILURE, NULL);
7293 			return;
7294 		}
7295 		return;
7296 	}
7297 
7298 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7299 
7300 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7301 }
7302 
7303 stmf_status_t
7304 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
7305 {
7306 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7307 
7308 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7309 		return (STMF_INVALID_ARG);
7310 	}
7311 
7312 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
7313 	return (STMF_SUCCESS);
7314 }
7315 
7316 stmf_status_t
7317 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
7318 {
7319 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7320 
7321 	if (eventid == STMF_EVENT_ALL) {
7322 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
7323 		return (STMF_SUCCESS);
7324 	}
7325 
7326 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7327 		return (STMF_INVALID_ARG);
7328 	}
7329 
7330 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
7331 	return (STMF_SUCCESS);
7332 }
7333 
7334 stmf_status_t
7335 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
7336 {
7337 	stmf_i_local_port_t *ilport =
7338 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7339 
7340 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7341 		return (STMF_INVALID_ARG);
7342 	}
7343 
7344 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
7345 	return (STMF_SUCCESS);
7346 }
7347 
7348 stmf_status_t
7349 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
7350 {
7351 	stmf_i_local_port_t *ilport =
7352 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7353 
7354 	if (eventid == STMF_EVENT_ALL) {
7355 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
7356 		return (STMF_SUCCESS);
7357 	}
7358 
7359 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7360 		return (STMF_INVALID_ARG);
7361 	}
7362 
7363 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
7364 	return (STMF_SUCCESS);
7365 }
7366 
7367 void
7368 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
7369 {
7370 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
7371 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
7372 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
7373 	}
7374 }
7375 
7376 void
7377 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
7378 				uint32_t flags)
7379 {
7380 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
7381 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
7382 		ilport->ilport_lport->lport_event_handler(
7383 		    ilport->ilport_lport, eventid, arg, flags);
7384 	}
7385 }
7386 
7387 /*
7388  * With the possibility of having multiple itl sessions pointing to the
7389  * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize
7390  * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer
7391  * statistics.
7392  */
7393 void
7394 stmf_itl_task_start(stmf_i_scsi_task_t *itask)
7395 {
7396 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7397 	scsi_task_t	*task = itask->itask_task;
7398 	stmf_i_lu_t	*ilu;
7399 
7400 	if (itl == NULL || task->task_lu == dlun0)
7401 		return;
7402 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7403 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7404 	itask->itask_start_timestamp = gethrtime();
7405 	kstat_waitq_enter(KSTAT_IO_PTR(itl->itl_kstat_taskq));
7406 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter);
7407 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7408 
7409 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter);
7410 }
7411 
7412 void
7413 stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask)
7414 {
7415 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7416 	scsi_task_t	*task = itask->itask_task;
7417 	stmf_i_lu_t	*ilu;
7418 
7419 	if (itl == NULL || task->task_lu == dlun0)
7420 		return;
7421 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7422 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7423 	kstat_waitq_to_runq(KSTAT_IO_PTR(itl->itl_kstat_taskq));
7424 	stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq);
7425 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7426 
7427 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq);
7428 }
7429 
7430 void
7431 stmf_itl_task_done(stmf_i_scsi_task_t *itask)
7432 {
7433 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7434 	scsi_task_t		*task = itask->itask_task;
7435 	kstat_io_t		*kip;
7436 	hrtime_t		elapsed_time;
7437 	stmf_kstat_itl_info_t	*itli;
7438 	stmf_i_lu_t	*ilu;
7439 
7440 	if (itl == NULL || task->task_lu == dlun0)
7441 		return;
7442 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7443 
7444 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7445 	itli = (stmf_kstat_itl_info_t *)KSTAT_NAMED_PTR(itl->itl_kstat_info);
7446 	kip = KSTAT_IO_PTR(itl->itl_kstat_taskq);
7447 
7448 	itli->i_task_waitq_elapsed.value.ui64 += itask->itask_waitq_time;
7449 
7450 	itask->itask_done_timestamp = gethrtime();
7451 	elapsed_time =
7452 	    itask->itask_done_timestamp - itask->itask_start_timestamp;
7453 
7454 	if (task->task_flags & TF_READ_DATA) {
7455 		kip->reads++;
7456 		kip->nread += itask->itask_read_xfer;
7457 		itli->i_task_read_elapsed.value.ui64 += elapsed_time;
7458 		itli->i_lu_read_elapsed.value.ui64 +=
7459 		    itask->itask_lu_read_time;
7460 		itli->i_lport_read_elapsed.value.ui64 +=
7461 		    itask->itask_lport_read_time;
7462 	}
7463 
7464 	if (task->task_flags & TF_WRITE_DATA) {
7465 		kip->writes++;
7466 		kip->nwritten += itask->itask_write_xfer;
7467 		itli->i_task_write_elapsed.value.ui64 += elapsed_time;
7468 		itli->i_lu_write_elapsed.value.ui64 +=
7469 		    itask->itask_lu_write_time;
7470 		itli->i_lport_write_elapsed.value.ui64 +=
7471 		    itask->itask_lport_write_time;
7472 	}
7473 
7474 	if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) {
7475 		kstat_runq_exit(kip);
7476 		stmf_update_kstat_lu_q(task, kstat_runq_exit);
7477 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7478 		stmf_update_kstat_lport_q(task, kstat_runq_exit);
7479 	} else {
7480 		kstat_waitq_exit(kip);
7481 		stmf_update_kstat_lu_q(task, kstat_waitq_exit);
7482 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7483 		stmf_update_kstat_lport_q(task, kstat_waitq_exit);
7484 	}
7485 }
7486 
7487 void
7488 stmf_lu_xfer_start(scsi_task_t *task)
7489 {
7490 	stmf_i_scsi_task_t *itask = task->task_stmf_private;
7491 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7492 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7493 	kstat_io_t		*kip;
7494 
7495 	if (itl == NULL || task->task_lu == dlun0)
7496 		return;
7497 
7498 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
7499 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7500 	kstat_runq_enter(kip);
7501 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7502 }
7503 
7504 void
7505 stmf_lu_xfer_done(scsi_task_t *task, boolean_t read, uint64_t xfer_bytes,
7506     hrtime_t elapsed_time)
7507 {
7508 	stmf_i_scsi_task_t	*itask = task->task_stmf_private;
7509 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7510 	stmf_i_lu_t	*ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7511 	kstat_io_t		*kip;
7512 
7513 	if (itl == NULL || task->task_lu == dlun0)
7514 		return;
7515 
7516 	if (read) {
7517 		atomic_add_64((uint64_t *)&itask->itask_lu_read_time,
7518 		    elapsed_time);
7519 	} else {
7520 		atomic_add_64((uint64_t *)&itask->itask_lu_write_time,
7521 		    elapsed_time);
7522 	}
7523 
7524 	kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer);
7525 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7526 	kstat_runq_exit(kip);
7527 	if (read) {
7528 		kip->reads++;
7529 		kip->nread += xfer_bytes;
7530 	} else {
7531 		kip->writes++;
7532 		kip->nwritten += xfer_bytes;
7533 	}
7534 	mutex_exit(ilu->ilu_kstat_io->ks_lock);
7535 }
7536 
7537 static void
7538 stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7539 {
7540 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7541 
7542 	if (itl == NULL)
7543 		return;
7544 
7545 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task,
7546 	    stmf_data_buf_t *, dbuf);
7547 
7548 	dbuf->db_xfer_start_timestamp = gethrtime();
7549 }
7550 
7551 static void
7552 stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7553 {
7554 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7555 	scsi_task_t		*task;
7556 	stmf_i_local_port_t	*ilp;
7557 	kstat_io_t		*kip;
7558 	hrtime_t		elapsed_time;
7559 	uint64_t		xfer_size;
7560 
7561 	if (itl == NULL)
7562 		return;
7563 
7564 	task = (scsi_task_t *)itask->itask_task;
7565 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
7566 	xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ?
7567 	    dbuf->db_data_size : 0;
7568 
7569 	elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp;
7570 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7571 		atomic_add_64((uint64_t *)&itask->itask_lport_read_time,
7572 		    elapsed_time);
7573 		atomic_add_64((uint64_t *)&itask->itask_read_xfer,
7574 		    xfer_size);
7575 	} else {
7576 		atomic_add_64((uint64_t *)&itask->itask_lport_write_time,
7577 		    elapsed_time);
7578 		atomic_add_64((uint64_t *)&itask->itask_write_xfer,
7579 		    xfer_size);
7580 	}
7581 
7582 	DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task,
7583 	    stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time);
7584 
7585 	kip = KSTAT_IO_PTR(itl->itl_kstat_lport_xfer);
7586 	mutex_enter(ilp->ilport_kstat_io->ks_lock);
7587 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7588 		kip->reads++;
7589 		kip->nread += xfer_size;
7590 	} else {
7591 		kip->writes++;
7592 		kip->nwritten += xfer_size;
7593 	}
7594 	mutex_exit(ilp->ilport_kstat_io->ks_lock);
7595 
7596 	dbuf->db_xfer_start_timestamp = 0;
7597 }
7598 
7599 void
7600 stmf_svc_init()
7601 {
7602 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7603 		return;
7604 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
7605 	    TASKQ_DEFAULTPRI, 0);
7606 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
7607 	    stmf_svc, 0, DDI_SLEEP);
7608 }
7609 
7610 stmf_status_t
7611 stmf_svc_fini()
7612 {
7613 	uint32_t i;
7614 
7615 	mutex_enter(&stmf_state.stmf_lock);
7616 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
7617 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
7618 		cv_signal(&stmf_state.stmf_cv);
7619 	}
7620 	mutex_exit(&stmf_state.stmf_lock);
7621 
7622 	/* Wait for 5 seconds */
7623 	for (i = 0; i < 500; i++) {
7624 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7625 			delay(drv_usectohz(10000));
7626 		else
7627 			break;
7628 	}
7629 	if (i == 500)
7630 		return (STMF_BUSY);
7631 
7632 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
7633 
7634 	return (STMF_SUCCESS);
7635 }
7636 
7637 /* ARGSUSED */
7638 void
7639 stmf_svc(void *arg)
7640 {
7641 	stmf_svc_req_t *req, **preq;
7642 	clock_t td;
7643 	clock_t	drain_start, drain_next = 0;
7644 	clock_t	timing_start, timing_next = 0;
7645 	clock_t worker_delay = 0;
7646 	int deq;
7647 	stmf_lu_t *lu;
7648 	stmf_i_lu_t *ilu;
7649 	stmf_local_port_t *lport;
7650 	stmf_i_local_port_t *ilport, *next_ilport;
7651 	stmf_i_scsi_session_t *iss;
7652 
7653 	td = drv_usectohz(20000);
7654 
7655 	mutex_enter(&stmf_state.stmf_lock);
7656 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
7657 
7658 stmf_svc_loop:
7659 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
7660 		stmf_state.stmf_svc_flags &=
7661 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
7662 		mutex_exit(&stmf_state.stmf_lock);
7663 		return;
7664 	}
7665 
7666 	if (stmf_state.stmf_svc_active) {
7667 		int waitq_add = 0;
7668 		req = stmf_state.stmf_svc_active;
7669 		stmf_state.stmf_svc_active = req->svc_next;
7670 
7671 		switch (req->svc_cmd) {
7672 		case STMF_CMD_LPORT_ONLINE:
7673 			/* Fallthrough */
7674 		case STMF_CMD_LPORT_OFFLINE:
7675 			/* Fallthrough */
7676 		case STMF_CMD_LU_ONLINE:
7677 			/* Nothing to do */
7678 			waitq_add = 1;
7679 			break;
7680 
7681 		case STMF_CMD_LU_OFFLINE:
7682 			/* Remove all mappings of this LU */
7683 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
7684 			/* Kill all the pending I/Os for this LU */
7685 			mutex_exit(&stmf_state.stmf_lock);
7686 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
7687 			    STMF_ABORTED);
7688 			mutex_enter(&stmf_state.stmf_lock);
7689 			waitq_add = 1;
7690 			break;
7691 		default:
7692 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
7693 			    req->svc_cmd);
7694 		}
7695 
7696 		if (waitq_add) {
7697 			/* Put it in the wait queue */
7698 			req->svc_next = stmf_state.stmf_svc_waiting;
7699 			stmf_state.stmf_svc_waiting = req;
7700 		}
7701 	}
7702 
7703 	/* The waiting list is not going to be modified by anybody else */
7704 	mutex_exit(&stmf_state.stmf_lock);
7705 
7706 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
7707 		req = *preq;
7708 		deq = 0;
7709 
7710 		switch (req->svc_cmd) {
7711 		case STMF_CMD_LU_ONLINE:
7712 			lu = (stmf_lu_t *)req->svc_obj;
7713 			deq = 1;
7714 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7715 			break;
7716 
7717 		case STMF_CMD_LU_OFFLINE:
7718 			lu = (stmf_lu_t *)req->svc_obj;
7719 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7720 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
7721 				break;
7722 			deq = 1;
7723 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7724 			break;
7725 
7726 		case STMF_CMD_LPORT_OFFLINE:
7727 			/* Fallthrough */
7728 		case STMF_CMD_LPORT_ONLINE:
7729 			lport = (stmf_local_port_t *)req->svc_obj;
7730 			deq = 1;
7731 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
7732 			break;
7733 		}
7734 		if (deq) {
7735 			*preq = req->svc_next;
7736 			kmem_free(req, req->svc_req_alloc_size);
7737 		} else {
7738 			preq = &req->svc_next;
7739 		}
7740 	}
7741 
7742 	mutex_enter(&stmf_state.stmf_lock);
7743 	if (stmf_state.stmf_svc_active == NULL) {
7744 		/* Do timeouts */
7745 		if (stmf_state.stmf_nlus &&
7746 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
7747 			if (!stmf_state.stmf_svc_ilu_timing) {
7748 				/* we are starting a new round */
7749 				stmf_state.stmf_svc_ilu_timing =
7750 				    stmf_state.stmf_ilulist;
7751 				timing_start = ddi_get_lbolt();
7752 			}
7753 			stmf_check_ilu_timing();
7754 			if (!stmf_state.stmf_svc_ilu_timing) {
7755 				/* we finished a complete round */
7756 				timing_next =
7757 				    timing_start + drv_usectohz(5*1000*1000);
7758 			} else {
7759 				/* we still have some ilu items to check */
7760 				timing_next =
7761 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7762 			}
7763 			if (stmf_state.stmf_svc_active)
7764 				goto stmf_svc_loop;
7765 		}
7766 		/* Check if there are free tasks to clear */
7767 		if (stmf_state.stmf_nlus &&
7768 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
7769 			if (!stmf_state.stmf_svc_ilu_draining) {
7770 				/* we are starting a new round */
7771 				stmf_state.stmf_svc_ilu_draining =
7772 				    stmf_state.stmf_ilulist;
7773 				drain_start = ddi_get_lbolt();
7774 			}
7775 			stmf_check_freetask();
7776 			if (!stmf_state.stmf_svc_ilu_draining) {
7777 				/* we finished a complete round */
7778 				drain_next =
7779 				    drain_start + drv_usectohz(10*1000*1000);
7780 			} else {
7781 				/* we still have some ilu items to check */
7782 				drain_next =
7783 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7784 			}
7785 			if (stmf_state.stmf_svc_active)
7786 				goto stmf_svc_loop;
7787 		}
7788 
7789 		/* Check if we need to run worker_mgmt */
7790 		if (ddi_get_lbolt() > worker_delay) {
7791 			stmf_worker_mgmt();
7792 			worker_delay = ddi_get_lbolt() +
7793 			    stmf_worker_mgmt_delay;
7794 		}
7795 
7796 		/* Check if any active session got its 1st LUN */
7797 		if (stmf_state.stmf_process_initial_luns) {
7798 			int stmf_level = 0;
7799 			int port_level;
7800 			for (ilport = stmf_state.stmf_ilportlist; ilport;
7801 			    ilport = next_ilport) {
7802 				int ilport_lock_held;
7803 				next_ilport = ilport->ilport_next;
7804 				if ((ilport->ilport_flags &
7805 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
7806 					continue;
7807 				}
7808 				port_level = 0;
7809 				rw_enter(&ilport->ilport_lock, RW_READER);
7810 				ilport_lock_held = 1;
7811 				for (iss = ilport->ilport_ss_list; iss;
7812 				    iss = iss->iss_next) {
7813 					if ((iss->iss_flags &
7814 					    ISS_GOT_INITIAL_LUNS) == 0) {
7815 						continue;
7816 					}
7817 					port_level++;
7818 					stmf_level++;
7819 					atomic_and_32(&iss->iss_flags,
7820 					    ~ISS_GOT_INITIAL_LUNS);
7821 					atomic_or_32(&iss->iss_flags,
7822 					    ISS_EVENT_ACTIVE);
7823 					rw_exit(&ilport->ilport_lock);
7824 					ilport_lock_held = 0;
7825 					mutex_exit(&stmf_state.stmf_lock);
7826 					stmf_generate_lport_event(ilport,
7827 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
7828 					    iss->iss_ss, 0);
7829 					atomic_and_32(&iss->iss_flags,
7830 					    ~ISS_EVENT_ACTIVE);
7831 					mutex_enter(&stmf_state.stmf_lock);
7832 					/*
7833 					 * scan all the ilports again as the
7834 					 * ilport list might have changed.
7835 					 */
7836 					next_ilport =
7837 					    stmf_state.stmf_ilportlist;
7838 					break;
7839 				}
7840 				if (port_level == 0) {
7841 					atomic_and_32(&ilport->ilport_flags,
7842 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
7843 				}
7844 				/* drop the lock if we are holding it. */
7845 				if (ilport_lock_held == 1)
7846 					rw_exit(&ilport->ilport_lock);
7847 
7848 				/* Max 4 session at a time */
7849 				if (stmf_level >= 4) {
7850 					break;
7851 				}
7852 			}
7853 			if (stmf_level == 0) {
7854 				stmf_state.stmf_process_initial_luns = 0;
7855 			}
7856 		}
7857 
7858 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
7859 		(void) cv_reltimedwait(&stmf_state.stmf_cv,
7860 		    &stmf_state.stmf_lock, td, TR_CLOCK_TICK);
7861 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
7862 	}
7863 	goto stmf_svc_loop;
7864 }
7865 
7866 void
7867 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
7868 {
7869 	stmf_svc_req_t *req;
7870 	int s;
7871 
7872 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
7873 	s = sizeof (stmf_svc_req_t);
7874 	if (info->st_additional_info) {
7875 		s += strlen(info->st_additional_info) + 1;
7876 	}
7877 	req = kmem_zalloc(s, KM_SLEEP);
7878 
7879 	req->svc_cmd = cmd;
7880 	req->svc_obj = obj;
7881 	req->svc_info.st_rflags = info->st_rflags;
7882 	if (info->st_additional_info) {
7883 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
7884 		    sizeof (stmf_svc_req_t)));
7885 		(void) strcpy(req->svc_info.st_additional_info,
7886 		    info->st_additional_info);
7887 	}
7888 	req->svc_req_alloc_size = s;
7889 
7890 	mutex_enter(&stmf_state.stmf_lock);
7891 	req->svc_next = stmf_state.stmf_svc_active;
7892 	stmf_state.stmf_svc_active = req;
7893 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
7894 		cv_signal(&stmf_state.stmf_cv);
7895 	}
7896 	mutex_exit(&stmf_state.stmf_lock);
7897 }
7898 
7899 void
7900 stmf_trace(caddr_t ident, const char *fmt, ...)
7901 {
7902 	va_list args;
7903 	char tbuf[160];
7904 	int len;
7905 
7906 	if (!stmf_trace_on)
7907 		return;
7908 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
7909 	    ddi_get_lbolt());
7910 	va_start(args, fmt);
7911 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
7912 	va_end(args);
7913 
7914 	if (len > 158) {
7915 		len = 158;
7916 	}
7917 	tbuf[len++] = '\n';
7918 	tbuf[len] = 0;
7919 
7920 	mutex_enter(&trace_buf_lock);
7921 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
7922 	trace_buf_curndx += len;
7923 	if (trace_buf_curndx > (trace_buf_size - 320))
7924 		trace_buf_curndx = 0;
7925 	mutex_exit(&trace_buf_lock);
7926 }
7927 
7928 void
7929 stmf_trace_clear()
7930 {
7931 	if (!stmf_trace_on)
7932 		return;
7933 	mutex_enter(&trace_buf_lock);
7934 	trace_buf_curndx = 0;
7935 	if (trace_buf_size > 0)
7936 		stmf_trace_buf[0] = 0;
7937 	mutex_exit(&trace_buf_lock);
7938 }
7939 
7940 static void
7941 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
7942 {
7943 	stmf_state_change_info_t	change_info;
7944 	void				*ctl_private;
7945 	uint32_t			ctl_cmd;
7946 	int				msg = 0;
7947 
7948 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
7949 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
7950 	change_info.st_additional_info = info;
7951 	if (offline_lu) {
7952 		change_info.st_rflags = STMF_RFLAG_RESET |
7953 		    STMF_RFLAG_LU_ABORT;
7954 		ctl_private = task->task_lu;
7955 		if (((stmf_i_lu_t *)
7956 		    task->task_lu->lu_stmf_private)->ilu_state ==
7957 		    STMF_STATE_ONLINE) {
7958 			msg = 1;
7959 		}
7960 		ctl_cmd = STMF_CMD_LU_OFFLINE;
7961 	} else {
7962 		change_info.st_rflags = STMF_RFLAG_RESET |
7963 		    STMF_RFLAG_LPORT_ABORT;
7964 		ctl_private = task->task_lport;
7965 		if (((stmf_i_local_port_t *)
7966 		    task->task_lport->lport_stmf_private)->ilport_state ==
7967 		    STMF_STATE_ONLINE) {
7968 			msg = 1;
7969 		}
7970 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
7971 	}
7972 
7973 	if (msg) {
7974 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
7975 		    offline_lu ? "LU" : "LPORT", info ? info :
7976 		    "<no additional info>");
7977 	}
7978 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
7979 }
7980