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