xref: /illumos-gate/usr/src/uts/common/io/comstar/stmf/stmf.c (revision bde334a8dbd66dfa70ce4d7fc9dcad6e1ae45fe4)
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] = '\0';
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, STMF_KSTAT_LU_SZ);
3184 		kstat_delete(ilu->ilu_kstat_info);
3185 	}
3186 	if (ilu->ilu_kstat_io) {
3187 		kstat_delete(ilu->ilu_kstat_io);
3188 		mutex_destroy(&ilu->ilu_kstat_lock);
3189 	}
3190 	cv_destroy(&ilu->ilu_offline_pending_cv);
3191 	mutex_exit(&stmf_state.stmf_lock);
3192 	return (STMF_SUCCESS);
3193 }
3194 
3195 void
3196 stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid)
3197 {
3198 	stmf_i_local_port_t *ilport =
3199 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3200 	ilport->ilport_rtpid = rtpid;
3201 	ilport->ilport_standby = 1;
3202 }
3203 
3204 void
3205 stmf_set_port_alua(stmf_local_port_t *lport)
3206 {
3207 	stmf_i_local_port_t *ilport =
3208 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
3209 	ilport->ilport_alua = 1;
3210 }
3211 
3212 stmf_status_t
3213 stmf_register_local_port(stmf_local_port_t *lport)
3214 {
3215 	stmf_i_local_port_t *ilport;
3216 	stmf_state_change_info_t ssci;
3217 	int start_workers = 0;
3218 
3219 	mutex_enter(&stmf_state.stmf_lock);
3220 	if (stmf_state.stmf_inventory_locked) {
3221 		mutex_exit(&stmf_state.stmf_lock);
3222 		return (STMF_BUSY);
3223 	}
3224 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3225 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
3226 
3227 	ilport->ilport_instance =
3228 	    id_alloc_nosleep(stmf_state.stmf_ilport_inst_space);
3229 	if (ilport->ilport_instance == -1) {
3230 		mutex_exit(&stmf_state.stmf_lock);
3231 		return (STMF_FAILURE);
3232 	}
3233 	ilport->ilport_next = stmf_state.stmf_ilportlist;
3234 	ilport->ilport_prev = NULL;
3235 	if (ilport->ilport_next)
3236 		ilport->ilport_next->ilport_prev = ilport;
3237 	stmf_state.stmf_ilportlist = ilport;
3238 	stmf_state.stmf_nlports++;
3239 	if (lport->lport_pp) {
3240 		((stmf_i_port_provider_t *)
3241 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
3242 	}
3243 	ilport->ilport_tg =
3244 	    stmf_lookup_group_for_target(lport->lport_id->ident,
3245 	    lport->lport_id->ident_length);
3246 
3247 	/*
3248 	 * rtpid will/must be set if this is a standby port
3249 	 * only register ports that are not standby (proxy) ports
3250 	 * and ports that are alua participants (ilport_alua == 1)
3251 	 */
3252 	if (ilport->ilport_standby == 0) {
3253 		ilport->ilport_rtpid = atomic_inc_16_nv(&stmf_rtpid_counter);
3254 	}
3255 
3256 	if (stmf_state.stmf_alua_state == 1 &&
3257 	    ilport->ilport_standby == 0 &&
3258 	    ilport->ilport_alua == 1) {
3259 		stmf_ic_msg_t *ic_reg_port;
3260 		stmf_ic_msg_status_t ic_ret;
3261 		stmf_local_port_t *lport;
3262 		lport = ilport->ilport_lport;
3263 		ic_reg_port = ic_reg_port_msg_alloc(
3264 		    lport->lport_id, ilport->ilport_rtpid,
3265 		    0, NULL, stmf_proxy_msg_id);
3266 		if (ic_reg_port) {
3267 			ic_ret = ic_tx_msg(ic_reg_port);
3268 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3269 				ilport->ilport_reg_msgid = stmf_proxy_msg_id++;
3270 			} else {
3271 				cmn_err(CE_WARN, "error on port registration "
3272 				"port - %s", ilport->ilport_kstat_tgt_name);
3273 			}
3274 		}
3275 	}
3276 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
3277 	stmf_create_kstat_lport(ilport);
3278 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
3279 		stmf_workers_state = STMF_WORKERS_ENABLING;
3280 		start_workers = 1;
3281 	}
3282 	mutex_exit(&stmf_state.stmf_lock);
3283 
3284 	if (start_workers)
3285 		stmf_worker_init();
3286 
3287 	/*  the default state of LPORT */
3288 
3289 	if (stmf_state.stmf_default_lport_state == STMF_STATE_OFFLINE) {
3290 		ilport->ilport_prev_state = STMF_STATE_OFFLINE;
3291 	} else {
3292 		ilport->ilport_prev_state = STMF_STATE_ONLINE;
3293 		if (stmf_state.stmf_service_running) {
3294 			ssci.st_rflags = 0;
3295 			ssci.st_additional_info = NULL;
3296 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
3297 		}
3298 	}
3299 
3300 	/* XXX: Generate event */
3301 	return (STMF_SUCCESS);
3302 }
3303 
3304 stmf_status_t
3305 stmf_deregister_local_port(stmf_local_port_t *lport)
3306 {
3307 	stmf_i_local_port_t *ilport;
3308 
3309 	mutex_enter(&stmf_state.stmf_lock);
3310 	if (stmf_state.stmf_inventory_locked) {
3311 		mutex_exit(&stmf_state.stmf_lock);
3312 		return (STMF_BUSY);
3313 	}
3314 
3315 	/* dequeue all object requests from active queue */
3316 	stmf_svc_kill_obj_requests(lport);
3317 
3318 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3319 
3320 	/*
3321 	 * deregister ports that are not standby (proxy)
3322 	 */
3323 	if (stmf_state.stmf_alua_state == 1 &&
3324 	    ilport->ilport_standby == 0 &&
3325 	    ilport->ilport_alua == 1) {
3326 		stmf_ic_msg_t *ic_dereg_port;
3327 		stmf_ic_msg_status_t ic_ret;
3328 		ic_dereg_port = ic_dereg_port_msg_alloc(
3329 		    lport->lport_id, 0, NULL, stmf_proxy_msg_id);
3330 		if (ic_dereg_port) {
3331 			ic_ret = ic_tx_msg(ic_dereg_port);
3332 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3333 				stmf_proxy_msg_id++;
3334 			}
3335 		}
3336 	}
3337 
3338 	if (ilport->ilport_nsessions == 0) {
3339 		if (ilport->ilport_next)
3340 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
3341 		if (ilport->ilport_prev)
3342 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
3343 		else
3344 			stmf_state.stmf_ilportlist = ilport->ilport_next;
3345 		id_free(stmf_state.stmf_ilport_inst_space,
3346 		    ilport->ilport_instance);
3347 		rw_destroy(&ilport->ilport_lock);
3348 		stmf_state.stmf_nlports--;
3349 		if (lport->lport_pp) {
3350 			((stmf_i_port_provider_t *)
3351 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
3352 		}
3353 		ilport->ilport_tg = NULL;
3354 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
3355 	} else {
3356 		mutex_exit(&stmf_state.stmf_lock);
3357 		return (STMF_BUSY);
3358 	}
3359 	if (ilport->ilport_kstat_info) {
3360 		kmem_free(ilport->ilport_kstat_info->ks_data,
3361 		    STMF_KSTAT_TGT_SZ);
3362 		kstat_delete(ilport->ilport_kstat_info);
3363 	}
3364 	if (ilport->ilport_kstat_io) {
3365 		kstat_delete(ilport->ilport_kstat_io);
3366 		mutex_destroy(&ilport->ilport_kstat_lock);
3367 	}
3368 	mutex_exit(&stmf_state.stmf_lock);
3369 	return (STMF_SUCCESS);
3370 }
3371 
3372 /*
3373  * Rport id/instance mappings remain valid until STMF is unloaded
3374  */
3375 static int
3376 stmf_irport_compare(const void *void_irport1, const void *void_irport2)
3377 {
3378 	const	stmf_i_remote_port_t	*irport1 = void_irport1;
3379 	const	stmf_i_remote_port_t	*irport2 = void_irport2;
3380 	int			result;
3381 
3382 	/* Sort by code set then ident */
3383 	if (irport1->irport_id->code_set <
3384 	    irport2->irport_id->code_set) {
3385 		return (-1);
3386 	} else if (irport1->irport_id->code_set >
3387 	    irport2->irport_id->code_set) {
3388 		return (1);
3389 	}
3390 
3391 	/* Next by ident length */
3392 	if (irport1->irport_id->ident_length <
3393 	    irport2->irport_id->ident_length) {
3394 		return (-1);
3395 	} else if (irport1->irport_id->ident_length >
3396 	    irport2->irport_id->ident_length) {
3397 		return (1);
3398 	}
3399 
3400 	/* Code set and ident length both match, now compare idents */
3401 	result = memcmp(irport1->irport_id->ident,
3402 	    irport2->irport_id->ident,
3403 	    irport1->irport_id->ident_length);
3404 
3405 	if (result < 0) {
3406 		return (-1);
3407 	} else if (result > 0) {
3408 		return (1);
3409 	}
3410 
3411 	return (0);
3412 }
3413 
3414 static stmf_i_remote_port_t *
3415 stmf_irport_create(scsi_devid_desc_t *rport_devid)
3416 {
3417 	int			alloc_len;
3418 	stmf_i_remote_port_t	*irport;
3419 
3420 	/*
3421 	 * Lookup will bump the refcnt if there's an existing rport
3422 	 * context for this identifier.
3423 	 */
3424 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3425 
3426 	alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3427 	    rport_devid->ident_length - 1;
3428 	irport = kmem_zalloc(alloc_len, KM_NOSLEEP);
3429 	if (irport == NULL) {
3430 		return (NULL);
3431 	}
3432 
3433 	irport->irport_instance =
3434 	    id_alloc_nosleep(stmf_state.stmf_irport_inst_space);
3435 	if (irport->irport_instance == -1) {
3436 		kmem_free(irport, alloc_len);
3437 		return (NULL);
3438 	}
3439 
3440 	irport->irport_id =
3441 	    (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */
3442 	bcopy(rport_devid, irport->irport_id,
3443 	    sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1);
3444 	irport->irport_refcnt = 1;
3445 	mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL);
3446 
3447 	return (irport);
3448 }
3449 
3450 static void
3451 stmf_irport_destroy(stmf_i_remote_port_t *irport)
3452 {
3453 	id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance);
3454 	mutex_destroy(&irport->irport_mutex);
3455 	kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3456 	    irport->irport_id->ident_length - 1);
3457 }
3458 
3459 static stmf_i_remote_port_t *
3460 stmf_irport_register(scsi_devid_desc_t *rport_devid)
3461 {
3462 	stmf_i_remote_port_t	*irport;
3463 
3464 	mutex_enter(&stmf_state.stmf_lock);
3465 
3466 	/*
3467 	 * Lookup will bump the refcnt if there's an existing rport
3468 	 * context for this identifier.
3469 	 */
3470 	if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) {
3471 		mutex_exit(&stmf_state.stmf_lock);
3472 		return (irport);
3473 	}
3474 
3475 	irport = stmf_irport_create(rport_devid);
3476 	if (irport == NULL) {
3477 		mutex_exit(&stmf_state.stmf_lock);
3478 		return (NULL);
3479 	}
3480 
3481 	avl_add(&stmf_state.stmf_irportlist, irport);
3482 	mutex_exit(&stmf_state.stmf_lock);
3483 
3484 	return (irport);
3485 }
3486 
3487 static stmf_i_remote_port_t *
3488 stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid)
3489 {
3490 	stmf_i_remote_port_t	*irport;
3491 	stmf_i_remote_port_t	tmp_irport;
3492 
3493 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
3494 	tmp_irport.irport_id = rport_devid;
3495 	irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL);
3496 	if (irport != NULL) {
3497 		mutex_enter(&irport->irport_mutex);
3498 		irport->irport_refcnt++;
3499 		mutex_exit(&irport->irport_mutex);
3500 	}
3501 
3502 	return (irport);
3503 }
3504 
3505 static void
3506 stmf_irport_deregister(stmf_i_remote_port_t *irport)
3507 {
3508 	/*
3509 	 * If we were actually going to remove unreferenced remote ports
3510 	 * we would want to acquire stmf_state.stmf_lock before getting
3511 	 * the irport mutex.
3512 	 *
3513 	 * Instead we're just going to leave it there even if unreferenced.
3514 	 */
3515 	mutex_enter(&irport->irport_mutex);
3516 	irport->irport_refcnt--;
3517 	mutex_exit(&irport->irport_mutex);
3518 }
3519 
3520 /*
3521  * Port provider has to make sure that register/deregister session and
3522  * port are serialized calls.
3523  */
3524 stmf_status_t
3525 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3526 {
3527 	stmf_i_scsi_session_t *iss;
3528 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3529 	    lport->lport_stmf_private;
3530 	uint8_t		lun[8];
3531 
3532 	/*
3533 	 * Port state has to be online to register a scsi session. It is
3534 	 * possible that we started an offline operation and a new SCSI
3535 	 * session started at the same time (in that case also we are going
3536 	 * to fail the registeration). But any other state is simply
3537 	 * a bad port provider implementation.
3538 	 */
3539 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
3540 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
3541 			stmf_trace(lport->lport_alias, "Port is trying to "
3542 			    "register a session while the state is neither "
3543 			    "online nor offlining");
3544 		}
3545 		return (STMF_FAILURE);
3546 	}
3547 	bzero(lun, 8);
3548 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3549 	if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) {
3550 		stmf_trace(lport->lport_alias, "Could not register "
3551 		    "remote port during session registration");
3552 		return (STMF_FAILURE);
3553 	}
3554 
3555 	iss->iss_flags |= ISS_BEING_CREATED;
3556 
3557 	if (ss->ss_rport == NULL) {
3558 		iss->iss_flags |= ISS_NULL_TPTID;
3559 		ss->ss_rport = stmf_scsilib_devid_to_remote_port(
3560 		    ss->ss_rport_id);
3561 		if (ss->ss_rport == NULL) {
3562 			iss->iss_flags &= ~(ISS_NULL_TPTID | ISS_BEING_CREATED);
3563 			stmf_trace(lport->lport_alias, "Device id to "
3564 			    "remote port conversion failed");
3565 			return (STMF_FAILURE);
3566 		}
3567 	} else {
3568 		if (!stmf_scsilib_tptid_validate(ss->ss_rport->rport_tptid,
3569 		    ss->ss_rport->rport_tptid_sz, NULL)) {
3570 			iss->iss_flags &= ~ISS_BEING_CREATED;
3571 			stmf_trace(lport->lport_alias, "Remote port "
3572 			    "transport id validation failed");
3573 			return (STMF_FAILURE);
3574 		}
3575 	}
3576 
3577 	/* sessions use the ilport_lock. No separate lock is required */
3578 	iss->iss_lockp = &ilport->ilport_lock;
3579 
3580 	if (iss->iss_sm != NULL)
3581 		cmn_err(CE_PANIC, "create lun map called with non NULL map");
3582 	iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t),
3583 	    KM_SLEEP);
3584 
3585 	mutex_enter(&stmf_state.stmf_lock);
3586 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3587 	(void) stmf_session_create_lun_map(ilport, iss);
3588 	ilport->ilport_nsessions++;
3589 	iss->iss_next = ilport->ilport_ss_list;
3590 	ilport->ilport_ss_list = iss;
3591 	rw_exit(&ilport->ilport_lock);
3592 	mutex_exit(&stmf_state.stmf_lock);
3593 
3594 	iss->iss_creation_time = ddi_get_time();
3595 	ss->ss_session_id = atomic_inc_64_nv(&stmf_session_counter);
3596 	iss->iss_flags &= ~ISS_BEING_CREATED;
3597 	/* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */
3598 	iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED;
3599 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
3600 	    stmf_scsi_session_t *, ss);
3601 	return (STMF_SUCCESS);
3602 }
3603 
3604 void
3605 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3606 {
3607 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3608 	    lport->lport_stmf_private;
3609 	stmf_i_scsi_session_t *iss, **ppss;
3610 	int found = 0;
3611 	stmf_ic_msg_t *ic_session_dereg;
3612 	stmf_status_t ic_ret = STMF_FAILURE;
3613 
3614 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
3615 	    stmf_scsi_session_t *, ss);
3616 
3617 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3618 	if (ss->ss_rport_alias) {
3619 		ss->ss_rport_alias = NULL;
3620 	}
3621 
3622 try_dereg_ss_again:
3623 	mutex_enter(&stmf_state.stmf_lock);
3624 	atomic_and_32(&iss->iss_flags,
3625 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
3626 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
3627 		mutex_exit(&stmf_state.stmf_lock);
3628 		delay(1);
3629 		goto try_dereg_ss_again;
3630 	}
3631 
3632 	/* dereg proxy session if not standby port */
3633 	if (stmf_state.stmf_alua_state == 1 &&
3634 	    ilport->ilport_standby == 0 &&
3635 	    ilport->ilport_alua == 1) {
3636 		ic_session_dereg = ic_session_dereg_msg_alloc(
3637 		    ss, stmf_proxy_msg_id);
3638 		if (ic_session_dereg) {
3639 			ic_ret = ic_tx_msg(ic_session_dereg);
3640 			if (ic_ret == STMF_IC_MSG_SUCCESS) {
3641 				stmf_proxy_msg_id++;
3642 			}
3643 		}
3644 	}
3645 
3646 	rw_enter(&ilport->ilport_lock, RW_WRITER);
3647 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
3648 	    ppss = &((*ppss)->iss_next)) {
3649 		if (iss == (*ppss)) {
3650 			*ppss = (*ppss)->iss_next;
3651 			found = 1;
3652 			break;
3653 		}
3654 	}
3655 	if (!found) {
3656 		cmn_err(CE_PANIC, "Deregister session called for non existent"
3657 		    " session");
3658 	}
3659 	ilport->ilport_nsessions--;
3660 
3661 	stmf_irport_deregister(iss->iss_irport);
3662 	(void) stmf_session_destroy_lun_map(ilport, iss);
3663 	rw_exit(&ilport->ilport_lock);
3664 	mutex_exit(&stmf_state.stmf_lock);
3665 
3666 	if (iss->iss_flags & ISS_NULL_TPTID) {
3667 		stmf_remote_port_free(ss->ss_rport);
3668 	}
3669 }
3670 
3671 stmf_i_scsi_session_t *
3672 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
3673 {
3674 	stmf_i_local_port_t *ilport;
3675 	stmf_i_scsi_session_t *iss;
3676 
3677 	mutex_enter(&stmf_state.stmf_lock);
3678 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3679 	    ilport = ilport->ilport_next) {
3680 		rw_enter(&ilport->ilport_lock, RW_WRITER);
3681 		for (iss = ilport->ilport_ss_list; iss != NULL;
3682 		    iss = iss->iss_next) {
3683 			if (iss->iss_ss->ss_session_id == session_id) {
3684 				if (!stay_locked)
3685 					rw_exit(&ilport->ilport_lock);
3686 				mutex_exit(&stmf_state.stmf_lock);
3687 				return (iss);
3688 			}
3689 		}
3690 		rw_exit(&ilport->ilport_lock);
3691 	}
3692 	mutex_exit(&stmf_state.stmf_lock);
3693 	return (NULL);
3694 }
3695 
3696 void
3697 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
3698 {
3699 	stmf_itl_data_t **itlpp;
3700 	stmf_i_lu_t *ilu;
3701 
3702 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
3703 
3704 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3705 	mutex_enter(&ilu->ilu_task_lock);
3706 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
3707 	    itlpp = &(*itlpp)->itl_next) {
3708 		if ((*itlpp) == itl)
3709 			break;
3710 	}
3711 	ASSERT((*itlpp) != NULL);
3712 	*itlpp = itl->itl_next;
3713 	mutex_exit(&ilu->ilu_task_lock);
3714 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
3715 	    (uint32_t)itl->itl_hdlrm_reason);
3716 
3717 	kmem_free(itl, sizeof (*itl));
3718 }
3719 
3720 stmf_status_t
3721 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
3722     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
3723 {
3724 	stmf_itl_data_t *itl;
3725 	stmf_i_scsi_session_t *iss;
3726 	stmf_lun_map_ent_t *lun_map_ent;
3727 	stmf_i_lu_t *ilu;
3728 	uint16_t n;
3729 
3730 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3731 	if (ss == NULL) {
3732 		iss = stmf_session_id_to_issptr(session_id, 1);
3733 		if (iss == NULL)
3734 			return (STMF_NOT_FOUND);
3735 	} else {
3736 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3737 	}
3738 
3739 	mutex_enter(&stmf_state.stmf_lock);
3740 	rw_enter(iss->iss_lockp, RW_WRITER);
3741 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
3742 	lun_map_ent = (stmf_lun_map_ent_t *)
3743 	    stmf_get_ent_from_map(iss->iss_sm, n);
3744 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
3745 		rw_exit(iss->iss_lockp);
3746 		mutex_exit(&stmf_state.stmf_lock);
3747 		return (STMF_NOT_FOUND);
3748 	}
3749 	if (lun_map_ent->ent_itl_datap != NULL) {
3750 		rw_exit(iss->iss_lockp);
3751 		mutex_exit(&stmf_state.stmf_lock);
3752 		return (STMF_ALREADY);
3753 	}
3754 
3755 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
3756 	if (itl == NULL) {
3757 		rw_exit(iss->iss_lockp);
3758 		mutex_exit(&stmf_state.stmf_lock);
3759 		return (STMF_ALLOC_FAILURE);
3760 	}
3761 
3762 	itl->itl_ilu = ilu;
3763 	itl->itl_session = iss;
3764 	itl->itl_counter = 1;
3765 	itl->itl_lun = n;
3766 	itl->itl_handle = itl_handle;
3767 
3768 	mutex_enter(&ilu->ilu_task_lock);
3769 	itl->itl_next = ilu->ilu_itl_list;
3770 	ilu->ilu_itl_list = itl;
3771 	mutex_exit(&ilu->ilu_task_lock);
3772 	lun_map_ent->ent_itl_datap = itl;
3773 	rw_exit(iss->iss_lockp);
3774 	mutex_exit(&stmf_state.stmf_lock);
3775 
3776 	return (STMF_SUCCESS);
3777 }
3778 
3779 void
3780 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
3781 {
3782 	uint8_t old, new;
3783 
3784 	do {
3785 		old = new = itl->itl_flags;
3786 		if (old & STMF_ITL_BEING_TERMINATED)
3787 			return;
3788 		new |= STMF_ITL_BEING_TERMINATED;
3789 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
3790 	itl->itl_hdlrm_reason = hdlrm_reason;
3791 
3792 	ASSERT(itl->itl_counter);
3793 
3794 	if (atomic_dec_32_nv(&itl->itl_counter))
3795 		return;
3796 
3797 	stmf_release_itl_handle(lu, itl);
3798 }
3799 
3800 stmf_status_t
3801 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
3802 {
3803 	stmf_i_lu_t *ilu;
3804 	stmf_i_local_port_t *ilport;
3805 	stmf_i_scsi_session_t *iss;
3806 	stmf_lun_map_t *lm;
3807 	stmf_lun_map_ent_t *ent;
3808 	uint32_t nmaps, nu;
3809 	stmf_itl_data_t **itl_list;
3810 	int i;
3811 
3812 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3813 
3814 dereg_itl_start:;
3815 	nmaps = ilu->ilu_ref_cnt;
3816 	if (nmaps == 0)
3817 		return (STMF_NOT_FOUND);
3818 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
3819 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
3820 	mutex_enter(&stmf_state.stmf_lock);
3821 	if (nmaps != ilu->ilu_ref_cnt) {
3822 		/* Something changed, start all over */
3823 		mutex_exit(&stmf_state.stmf_lock);
3824 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
3825 		goto dereg_itl_start;
3826 	}
3827 	nu = 0;
3828 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3829 	    ilport = ilport->ilport_next) {
3830 		rw_enter(&ilport->ilport_lock, RW_WRITER);
3831 		for (iss = ilport->ilport_ss_list; iss != NULL;
3832 		    iss = iss->iss_next) {
3833 			lm = iss->iss_sm;
3834 			if (!lm)
3835 				continue;
3836 			for (i = 0; i < lm->lm_nentries; i++) {
3837 				if (lm->lm_plus[i] == NULL)
3838 					continue;
3839 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
3840 				if ((ent->ent_lu == lu) &&
3841 				    (ent->ent_itl_datap)) {
3842 					itl_list[nu++] = ent->ent_itl_datap;
3843 					ent->ent_itl_datap = NULL;
3844 					if (nu == nmaps) {
3845 						rw_exit(&ilport->ilport_lock);
3846 						goto dai_scan_done;
3847 					}
3848 				}
3849 			} /* lun table for a session */
3850 		} /* sessions */
3851 		rw_exit(&ilport->ilport_lock);
3852 	} /* ports */
3853 
3854 dai_scan_done:
3855 	mutex_exit(&stmf_state.stmf_lock);
3856 
3857 	for (i = 0; i < nu; i++) {
3858 		stmf_do_itl_dereg(lu, itl_list[i],
3859 		    STMF_ITL_REASON_DEREG_REQUEST);
3860 	}
3861 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
3862 
3863 	return (STMF_SUCCESS);
3864 }
3865 
3866 stmf_status_t
3867 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
3868     uint64_t session_id, void **itl_handle_retp)
3869 {
3870 	stmf_i_scsi_session_t *iss;
3871 	stmf_lun_map_ent_t *ent;
3872 	stmf_lun_map_t *lm;
3873 	stmf_status_t ret;
3874 	int i;
3875 	uint16_t n;
3876 
3877 	if (ss == NULL) {
3878 		iss = stmf_session_id_to_issptr(session_id, 1);
3879 		if (iss == NULL)
3880 			return (STMF_NOT_FOUND);
3881 	} else {
3882 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3883 		rw_enter(iss->iss_lockp, RW_WRITER);
3884 	}
3885 
3886 	ent = NULL;
3887 	if (lun == NULL) {
3888 		lm = iss->iss_sm;
3889 		for (i = 0; i < lm->lm_nentries; i++) {
3890 			if (lm->lm_plus[i] == NULL)
3891 				continue;
3892 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
3893 			if (ent->ent_lu == lu)
3894 				break;
3895 		}
3896 	} else {
3897 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
3898 		ent = (stmf_lun_map_ent_t *)
3899 		    stmf_get_ent_from_map(iss->iss_sm, n);
3900 		if (lu && (ent->ent_lu != lu))
3901 			ent = NULL;
3902 	}
3903 	if (ent && ent->ent_itl_datap) {
3904 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
3905 		ret = STMF_SUCCESS;
3906 	} else {
3907 		ret = STMF_NOT_FOUND;
3908 	}
3909 
3910 	rw_exit(iss->iss_lockp);
3911 	return (ret);
3912 }
3913 
3914 stmf_data_buf_t *
3915 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
3916     uint32_t flags)
3917 {
3918 	stmf_i_scsi_task_t *itask =
3919 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3920 	stmf_local_port_t *lport = task->task_lport;
3921 	stmf_data_buf_t *dbuf;
3922 	uint8_t ndx;
3923 
3924 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
3925 	if (ndx == 0xff)
3926 		return (NULL);
3927 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
3928 	    task, size, pminsize, flags);
3929 	if (dbuf) {
3930 		task->task_cur_nbufs++;
3931 		itask->itask_allocated_buf_map |= (1 << ndx);
3932 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
3933 		dbuf->db_handle = ndx;
3934 		return (dbuf);
3935 	}
3936 
3937 	return (NULL);
3938 }
3939 
3940 stmf_status_t
3941 stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags)
3942 {
3943 	stmf_i_scsi_task_t *itask =
3944 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3945 	stmf_local_port_t *lport = task->task_lport;
3946 	uint8_t ndx;
3947 	stmf_status_t ret;
3948 
3949 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
3950 	ASSERT(lport->lport_ds->ds_setup_dbuf != NULL);
3951 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
3952 
3953 	if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0)
3954 		return (STMF_FAILURE);
3955 	if (lport->lport_ds->ds_setup_dbuf == NULL)
3956 		return (STMF_FAILURE);
3957 
3958 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
3959 	if (ndx == 0xff)
3960 		return (STMF_FAILURE);
3961 	ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags);
3962 	if (ret == STMF_FAILURE)
3963 		return (STMF_FAILURE);
3964 	itask->itask_dbufs[ndx] = dbuf;
3965 	task->task_cur_nbufs++;
3966 	itask->itask_allocated_buf_map |= (1 << ndx);
3967 	dbuf->db_handle = ndx;
3968 
3969 	return (STMF_SUCCESS);
3970 }
3971 
3972 void
3973 stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
3974 {
3975 	stmf_i_scsi_task_t *itask =
3976 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3977 	stmf_local_port_t *lport = task->task_lport;
3978 
3979 	ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
3980 	ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL);
3981 	ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
3982 
3983 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
3984 	task->task_cur_nbufs--;
3985 	lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf);
3986 }
3987 
3988 void
3989 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
3990 {
3991 	stmf_i_scsi_task_t *itask =
3992 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3993 	stmf_local_port_t *lport = task->task_lport;
3994 
3995 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
3996 	task->task_cur_nbufs--;
3997 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
3998 }
3999 
4000 stmf_data_buf_t *
4001 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
4002 {
4003 	stmf_i_scsi_task_t *itask;
4004 
4005 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4006 	if (h > 3)
4007 		return (NULL);
4008 	return (itask->itask_dbufs[h]);
4009 }
4010 
4011 /* ARGSUSED */
4012 struct scsi_task *
4013 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
4014     uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
4015 {
4016 	stmf_lu_t *lu;
4017 	stmf_i_scsi_session_t *iss;
4018 	stmf_i_lu_t *ilu;
4019 	stmf_i_scsi_task_t *itask;
4020 	stmf_i_scsi_task_t **ppitask;
4021 	scsi_task_t *task;
4022 	uint8_t	*l;
4023 	stmf_lun_map_ent_t *lun_map_ent;
4024 	uint16_t cdb_length;
4025 	uint16_t luNbr;
4026 	uint8_t new_task = 0;
4027 
4028 	/*
4029 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
4030 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
4031 	 * depend upon this alignment.
4032 	 */
4033 	if (cdb_length_in >= 16)
4034 		cdb_length = cdb_length_in + 7;
4035 	else
4036 		cdb_length = 16 + 7;
4037 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4038 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4039 	rw_enter(iss->iss_lockp, RW_READER);
4040 	lun_map_ent =
4041 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
4042 	if (!lun_map_ent) {
4043 		lu = dlun0;
4044 	} else {
4045 		lu = lun_map_ent->ent_lu;
4046 	}
4047 	ilu = lu->lu_stmf_private;
4048 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4049 		rw_exit(iss->iss_lockp);
4050 		return (NULL);
4051 	}
4052 	ASSERT(lu == dlun0 || (ilu->ilu_state != STMF_STATE_OFFLINING &&
4053 	    ilu->ilu_state != STMF_STATE_OFFLINE));
4054 	do {
4055 		if (ilu->ilu_free_tasks == NULL) {
4056 			new_task = 1;
4057 			break;
4058 		}
4059 		mutex_enter(&ilu->ilu_task_lock);
4060 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
4061 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
4062 		    ppitask = &((*ppitask)->itask_lu_free_next))
4063 			;
4064 		if (*ppitask) {
4065 			itask = *ppitask;
4066 			*ppitask = (*ppitask)->itask_lu_free_next;
4067 			ilu->ilu_ntasks_free--;
4068 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
4069 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4070 		} else {
4071 			new_task = 1;
4072 		}
4073 		mutex_exit(&ilu->ilu_task_lock);
4074 	/* CONSTCOND */
4075 	} while (0);
4076 
4077 	if (!new_task) {
4078 		/*
4079 		 * Save the task_cdb pointer and zero per cmd fields.
4080 		 * We know the task_cdb_length is large enough by task
4081 		 * selection process above.
4082 		 */
4083 		uint8_t *save_cdb;
4084 		uintptr_t t_start, t_end;
4085 
4086 		task = itask->itask_task;
4087 		save_cdb = task->task_cdb;	/* save */
4088 		t_start = (uintptr_t)&task->task_flags;
4089 		t_end = (uintptr_t)&task->task_extended_cmd;
4090 		bzero((void *)t_start, (size_t)(t_end - t_start));
4091 		task->task_cdb = save_cdb;	/* restore */
4092 		itask->itask_ncmds = 0;
4093 	} else {
4094 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
4095 		    cdb_length, AF_FORCE_NOSLEEP);
4096 		if (task == NULL) {
4097 			rw_exit(iss->iss_lockp);
4098 			return (NULL);
4099 		}
4100 		task->task_lu = lu;
4101 		l = task->task_lun_no;
4102 		l[0] = lun[0];
4103 		l[1] = lun[1];
4104 		l[2] = lun[2];
4105 		l[3] = lun[3];
4106 		l[4] = lun[4];
4107 		l[5] = lun[5];
4108 		l[6] = lun[6];
4109 		l[7] = lun[7];
4110 		task->task_cdb = (uint8_t *)task->task_port_private;
4111 		if ((ulong_t)(task->task_cdb) & 7ul) {
4112 			task->task_cdb = (uint8_t *)(((ulong_t)
4113 			    (task->task_cdb) + 7ul) & ~(7ul));
4114 		}
4115 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4116 		itask->itask_cdb_buf_size = cdb_length;
4117 		mutex_init(&itask->itask_audit_mutex, NULL, MUTEX_DRIVER, NULL);
4118 	}
4119 	task->task_session = ss;
4120 	task->task_lport = lport;
4121 	task->task_cdb_length = cdb_length_in;
4122 	itask->itask_flags = ITASK_IN_TRANSITION;
4123 	itask->itask_waitq_time = 0;
4124 	itask->itask_lu_read_time = itask->itask_lu_write_time = 0;
4125 	itask->itask_lport_read_time = itask->itask_lport_write_time = 0;
4126 	itask->itask_read_xfer = itask->itask_write_xfer = 0;
4127 	itask->itask_audit_index = 0;
4128 
4129 	if (new_task) {
4130 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
4131 			rw_exit(iss->iss_lockp);
4132 			stmf_free(task);
4133 			return (NULL);
4134 		}
4135 		mutex_enter(&ilu->ilu_task_lock);
4136 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4137 			mutex_exit(&ilu->ilu_task_lock);
4138 			rw_exit(iss->iss_lockp);
4139 			stmf_free(task);
4140 			return (NULL);
4141 		}
4142 		itask->itask_lu_next = ilu->ilu_tasks;
4143 		if (ilu->ilu_tasks)
4144 			ilu->ilu_tasks->itask_lu_prev = itask;
4145 		ilu->ilu_tasks = itask;
4146 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
4147 		ilu->ilu_ntasks++;
4148 		mutex_exit(&ilu->ilu_task_lock);
4149 	}
4150 
4151 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
4152 	atomic_inc_32(itask->itask_ilu_task_cntr);
4153 	itask->itask_start_time = ddi_get_lbolt();
4154 
4155 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
4156 	    lun_map_ent->ent_itl_datap) != NULL)) {
4157 		atomic_inc_32(&itask->itask_itl_datap->itl_counter);
4158 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
4159 	} else {
4160 		itask->itask_itl_datap = NULL;
4161 		task->task_lu_itl_handle = NULL;
4162 	}
4163 
4164 	rw_exit(iss->iss_lockp);
4165 	return (task);
4166 }
4167 
4168 static void
4169 stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss)
4170 {
4171 	stmf_i_scsi_task_t *itask =
4172 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4173 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4174 
4175 	ASSERT(rw_lock_held(iss->iss_lockp));
4176 	itask->itask_flags = ITASK_IN_FREE_LIST;
4177 	itask->itask_proxy_msg_id = 0;
4178 	mutex_enter(&ilu->ilu_task_lock);
4179 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
4180 	ilu->ilu_free_tasks = itask;
4181 	ilu->ilu_ntasks_free++;
4182 	if (ilu->ilu_ntasks == ilu->ilu_ntasks_free)
4183 		cv_signal(&ilu->ilu_offline_pending_cv);
4184 	mutex_exit(&ilu->ilu_task_lock);
4185 	atomic_dec_32(itask->itask_ilu_task_cntr);
4186 }
4187 
4188 void
4189 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
4190 {
4191 	uint32_t	num_to_release, ndx;
4192 	stmf_i_scsi_task_t *itask;
4193 	stmf_lu_t	*lu = ilu->ilu_lu;
4194 
4195 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
4196 
4197 	/* free half of the minimal free of the free tasks */
4198 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
4199 	if (!num_to_release) {
4200 		return;
4201 	}
4202 	for (ndx = 0; ndx < num_to_release; ndx++) {
4203 		mutex_enter(&ilu->ilu_task_lock);
4204 		itask = ilu->ilu_free_tasks;
4205 		if (itask == NULL) {
4206 			mutex_exit(&ilu->ilu_task_lock);
4207 			break;
4208 		}
4209 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
4210 		ilu->ilu_ntasks_free--;
4211 		mutex_exit(&ilu->ilu_task_lock);
4212 
4213 		lu->lu_task_free(itask->itask_task);
4214 		mutex_enter(&ilu->ilu_task_lock);
4215 		if (itask->itask_lu_next)
4216 			itask->itask_lu_next->itask_lu_prev =
4217 			    itask->itask_lu_prev;
4218 		if (itask->itask_lu_prev)
4219 			itask->itask_lu_prev->itask_lu_next =
4220 			    itask->itask_lu_next;
4221 		else
4222 			ilu->ilu_tasks = itask->itask_lu_next;
4223 
4224 		ilu->ilu_ntasks--;
4225 		mutex_exit(&ilu->ilu_task_lock);
4226 		stmf_free(itask->itask_task);
4227 	}
4228 }
4229 
4230 /*
4231  * Called with stmf_lock held
4232  */
4233 void
4234 stmf_check_freetask()
4235 {
4236 	stmf_i_lu_t *ilu;
4237 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4238 
4239 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
4240 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
4241 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
4242 		if (!ilu->ilu_ntasks_min_free) {
4243 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4244 			continue;
4245 		}
4246 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4247 		mutex_exit(&stmf_state.stmf_lock);
4248 		stmf_task_lu_check_freelist(ilu);
4249 		/*
4250 		 * we do not care about the accuracy of
4251 		 * ilu_ntasks_min_free, so we don't lock here
4252 		 */
4253 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4254 		mutex_enter(&stmf_state.stmf_lock);
4255 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4256 		cv_broadcast(&stmf_state.stmf_cv);
4257 		if (ddi_get_lbolt() >= endtime)
4258 			break;
4259 	}
4260 }
4261 
4262 void
4263 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
4264 {
4265 	clock_t l = ddi_get_lbolt();
4266 	clock_t ps = drv_usectohz(1000000);
4267 	stmf_i_scsi_task_t *itask;
4268 	scsi_task_t *task;
4269 	uint32_t to;
4270 
4271 	mutex_enter(&ilu->ilu_task_lock);
4272 	for (itask = ilu->ilu_tasks; itask != NULL;
4273 	    itask = itask->itask_lu_next) {
4274 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
4275 		    ITASK_BEING_ABORTED)) {
4276 			continue;
4277 		}
4278 		task = itask->itask_task;
4279 		if (task->task_timeout == 0)
4280 			to = stmf_default_task_timeout;
4281 		else
4282 			to = task->task_timeout;
4283 		if ((itask->itask_start_time + (to * ps)) > l)
4284 			continue;
4285 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4286 		    STMF_TIMEOUT, NULL);
4287 	}
4288 	mutex_exit(&ilu->ilu_task_lock);
4289 }
4290 
4291 /*
4292  * Called with stmf_lock held
4293  */
4294 void
4295 stmf_check_ilu_timing()
4296 {
4297 	stmf_i_lu_t *ilu;
4298 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
4299 
4300 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
4301 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
4302 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
4303 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
4304 			if (ilu->ilu_task_cntr2 == 0) {
4305 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
4306 				continue;
4307 			}
4308 		} else {
4309 			if (ilu->ilu_task_cntr1 == 0) {
4310 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
4311 				continue;
4312 			}
4313 		}
4314 		/*
4315 		 * If we are here then it means that there is some slowdown
4316 		 * in tasks on this lu. We need to check.
4317 		 */
4318 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4319 		mutex_exit(&stmf_state.stmf_lock);
4320 		stmf_do_ilu_timeouts(ilu);
4321 		mutex_enter(&stmf_state.stmf_lock);
4322 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4323 		cv_broadcast(&stmf_state.stmf_cv);
4324 		if (ddi_get_lbolt() >= endtime)
4325 			break;
4326 	}
4327 }
4328 
4329 /*
4330  * Kills all tasks on a lu except tm_task
4331  */
4332 void
4333 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
4334 {
4335 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4336 	stmf_i_scsi_task_t *itask;
4337 
4338 	mutex_enter(&ilu->ilu_task_lock);
4339 
4340 	for (itask = ilu->ilu_tasks; itask != NULL;
4341 	    itask = itask->itask_lu_next) {
4342 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
4343 			continue;
4344 		if (itask->itask_task == tm_task)
4345 			continue;
4346 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
4347 	}
4348 	mutex_exit(&ilu->ilu_task_lock);
4349 }
4350 
4351 void
4352 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
4353 {
4354 	int i;
4355 	uint8_t map;
4356 
4357 	if ((map = itask->itask_allocated_buf_map) == 0)
4358 		return;
4359 	for (i = 0; i < 4; i++) {
4360 		if (map & 1) {
4361 			stmf_data_buf_t *dbuf;
4362 
4363 			dbuf = itask->itask_dbufs[i];
4364 			if (dbuf->db_xfer_start_timestamp) {
4365 				stmf_lport_xfer_done(itask, dbuf);
4366 			}
4367 			if (dbuf->db_flags & DB_LU_DATA_BUF) {
4368 				/*
4369 				 * LU needs to clean up buffer.
4370 				 * LU is required to free the buffer
4371 				 * in the xfer_done handler.
4372 				 */
4373 				scsi_task_t *task = itask->itask_task;
4374 				stmf_lu_t *lu = task->task_lu;
4375 
4376 				lu->lu_dbuf_free(task, dbuf);
4377 				ASSERT(((itask->itask_allocated_buf_map>>i)
4378 				    & 1) == 0); /* must be gone */
4379 			} else {
4380 				ASSERT(dbuf->db_lu_private == NULL);
4381 				dbuf->db_lu_private = NULL;
4382 				lport->lport_ds->ds_free_data_buf(
4383 				    lport->lport_ds, dbuf);
4384 			}
4385 		}
4386 		map >>= 1;
4387 	}
4388 	itask->itask_allocated_buf_map = 0;
4389 }
4390 
4391 void
4392 stmf_task_free(scsi_task_t *task)
4393 {
4394 	stmf_local_port_t *lport = task->task_lport;
4395 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4396 	    task->task_stmf_private;
4397 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4398 	    task->task_session->ss_stmf_private;
4399 
4400 	stmf_task_audit(itask, TE_TASK_FREE, CMD_OR_IOF_NA, NULL);
4401 
4402 	stmf_free_task_bufs(itask, lport);
4403 	stmf_itl_task_done(itask);
4404 	DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task,
4405 	    hrtime_t,
4406 	    itask->itask_done_timestamp - itask->itask_start_timestamp);
4407 	if (itask->itask_itl_datap) {
4408 		if (atomic_dec_32_nv(&itask->itask_itl_datap->itl_counter) ==
4409 		    0) {
4410 			stmf_release_itl_handle(task->task_lu,
4411 			    itask->itask_itl_datap);
4412 		}
4413 	}
4414 
4415 	rw_enter(iss->iss_lockp, RW_READER);
4416 	lport->lport_task_free(task);
4417 	if (itask->itask_worker) {
4418 		atomic_dec_32(&stmf_cur_ntasks);
4419 		atomic_dec_32(&itask->itask_worker->worker_ref_count);
4420 	}
4421 	/*
4422 	 * After calling stmf_task_lu_free, the task pointer can no longer
4423 	 * be trusted.
4424 	 */
4425 	stmf_task_lu_free(task, iss);
4426 	rw_exit(iss->iss_lockp);
4427 }
4428 
4429 void
4430 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
4431 {
4432 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4433 	    task->task_stmf_private;
4434 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4435 	int nv;
4436 	uint32_t old, new;
4437 	uint32_t ct;
4438 	stmf_worker_t *w, *w1;
4439 	uint8_t tm;
4440 
4441 	if (task->task_max_nbufs > 4)
4442 		task->task_max_nbufs = 4;
4443 	task->task_cur_nbufs = 0;
4444 	/* Latest value of currently running tasks */
4445 	ct = atomic_inc_32_nv(&stmf_cur_ntasks);
4446 
4447 	/* Select the next worker using round robin */
4448 	nv = (int)atomic_inc_32_nv((uint32_t *)&stmf_worker_sel_counter);
4449 	if (nv >= stmf_nworkers_accepting_cmds) {
4450 		int s = nv;
4451 		do {
4452 			nv -= stmf_nworkers_accepting_cmds;
4453 		} while (nv >= stmf_nworkers_accepting_cmds);
4454 		if (nv < 0)
4455 			nv = 0;
4456 		/* Its ok if this cas fails */
4457 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
4458 		    s, nv);
4459 	}
4460 	w = &stmf_workers[nv];
4461 
4462 	/*
4463 	 * A worker can be pinned by interrupt. So select the next one
4464 	 * if it has lower load.
4465 	 */
4466 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
4467 		w1 = stmf_workers;
4468 	} else {
4469 		w1 = &stmf_workers[nv + 1];
4470 	}
4471 	if (w1->worker_queue_depth < w->worker_queue_depth)
4472 		w = w1;
4473 
4474 	mutex_enter(&w->worker_lock);
4475 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
4476 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4477 		/*
4478 		 * Maybe we are in the middle of a change. Just go to
4479 		 * the 1st worker.
4480 		 */
4481 		mutex_exit(&w->worker_lock);
4482 		w = stmf_workers;
4483 		mutex_enter(&w->worker_lock);
4484 	}
4485 	itask->itask_worker = w;
4486 	/*
4487 	 * Track max system load inside the worker as we already have the
4488 	 * worker lock (no point implementing another lock). The service
4489 	 * thread will do the comparisons and figure out the max overall
4490 	 * system load.
4491 	 */
4492 	if (w->worker_max_sys_qdepth_pu < ct)
4493 		w->worker_max_sys_qdepth_pu = ct;
4494 
4495 	do {
4496 		old = new = itask->itask_flags;
4497 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
4498 		if (task->task_mgmt_function) {
4499 			tm = task->task_mgmt_function;
4500 			if ((tm == TM_TARGET_RESET) ||
4501 			    (tm == TM_TARGET_COLD_RESET) ||
4502 			    (tm == TM_TARGET_WARM_RESET)) {
4503 				new |= ITASK_DEFAULT_HANDLING;
4504 			}
4505 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4506 			new |= ITASK_DEFAULT_HANDLING;
4507 		}
4508 		new &= ~ITASK_IN_TRANSITION;
4509 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4510 
4511 	stmf_itl_task_start(itask);
4512 
4513 	itask->itask_worker_next = NULL;
4514 	if (w->worker_task_tail) {
4515 		w->worker_task_tail->itask_worker_next = itask;
4516 	} else {
4517 		w->worker_task_head = itask;
4518 	}
4519 	w->worker_task_tail = itask;
4520 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4521 		w->worker_max_qdepth_pu = w->worker_queue_depth;
4522 	}
4523 	/* Measure task waitq time */
4524 	itask->itask_waitq_enter_timestamp = gethrtime();
4525 	atomic_inc_32(&w->worker_ref_count);
4526 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
4527 	itask->itask_ncmds = 1;
4528 	stmf_task_audit(itask, TE_TASK_START, CMD_OR_IOF_NA, dbuf);
4529 	if (dbuf) {
4530 		itask->itask_allocated_buf_map = 1;
4531 		itask->itask_dbufs[0] = dbuf;
4532 		dbuf->db_handle = 0;
4533 	} else {
4534 		itask->itask_allocated_buf_map = 0;
4535 		itask->itask_dbufs[0] = NULL;
4536 	}
4537 
4538 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) {
4539 		w->worker_signal_timestamp = gethrtime();
4540 		DTRACE_PROBE2(worker__signal, stmf_worker_t *, w,
4541 		    scsi_task_t *, task);
4542 		cv_signal(&w->worker_cv);
4543 	}
4544 	mutex_exit(&w->worker_lock);
4545 
4546 	/*
4547 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
4548 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
4549 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
4550 	 */
4551 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4552 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
4553 	}
4554 }
4555 
4556 static void
4557 stmf_task_audit(stmf_i_scsi_task_t *itask,
4558     task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf)
4559 {
4560 	stmf_task_audit_rec_t *ar;
4561 
4562 	mutex_enter(&itask->itask_audit_mutex);
4563 	ar = &itask->itask_audit_records[itask->itask_audit_index++];
4564 	itask->itask_audit_index &= (ITASK_TASK_AUDIT_DEPTH - 1);
4565 	ar->ta_event = te;
4566 	ar->ta_cmd_or_iof = cmd_or_iof;
4567 	ar->ta_itask_flags = itask->itask_flags;
4568 	ar->ta_dbuf = dbuf;
4569 	gethrestime(&ar->ta_timestamp);
4570 	mutex_exit(&itask->itask_audit_mutex);
4571 }
4572 
4573 
4574 /*
4575  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
4576  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
4577  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
4578  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
4579  * the LU will make this call only if we call the LU's abort entry point.
4580  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
4581  *
4582  * Same logic applies for the port.
4583  *
4584  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
4585  * and KNOWN_TO_TGT_PORT are reset.
4586  *
4587  * +++++++++++++++++++++++++++++++++++++++++++++++
4588  */
4589 
4590 stmf_status_t
4591 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
4592 {
4593 	stmf_status_t ret = STMF_SUCCESS;
4594 
4595 	stmf_i_scsi_task_t *itask =
4596 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4597 
4598 	stmf_task_audit(itask, TE_XFER_START, ioflags, dbuf);
4599 
4600 	if (ioflags & STMF_IOF_LU_DONE) {
4601 		uint32_t new, old;
4602 		do {
4603 			new = old = itask->itask_flags;
4604 			if (new & ITASK_BEING_ABORTED)
4605 				return (STMF_ABORTED);
4606 			new &= ~ITASK_KNOWN_TO_LU;
4607 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4608 	}
4609 	if (itask->itask_flags & ITASK_BEING_ABORTED)
4610 		return (STMF_ABORTED);
4611 #ifdef	DEBUG
4612 	if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) {
4613 		if (atomic_dec_32_nv(&stmf_drop_buf_counter) == 1)
4614 			return (STMF_SUCCESS);
4615 	}
4616 #endif
4617 
4618 	stmf_update_kstat_lu_io(task, dbuf);
4619 	stmf_update_kstat_lport_io(task, dbuf);
4620 	stmf_lport_xfer_start(itask, dbuf);
4621 	if (ioflags & STMF_IOF_STATS_ONLY) {
4622 		stmf_lport_xfer_done(itask, dbuf);
4623 		return (STMF_SUCCESS);
4624 	}
4625 
4626 	dbuf->db_flags |= DB_LPORT_XFER_ACTIVE;
4627 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
4628 
4629 	/*
4630 	 * Port provider may have already called the buffer callback in
4631 	 * which case dbuf->db_xfer_start_timestamp will be 0.
4632 	 */
4633 	if (ret != STMF_SUCCESS) {
4634 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4635 		if (dbuf->db_xfer_start_timestamp != 0)
4636 			stmf_lport_xfer_done(itask, dbuf);
4637 	}
4638 
4639 	return (ret);
4640 }
4641 
4642 void
4643 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
4644 {
4645 	stmf_i_scsi_task_t *itask =
4646 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4647 	stmf_i_local_port_t *ilport;
4648 	stmf_worker_t *w = itask->itask_worker;
4649 	uint32_t new, old;
4650 	uint8_t update_queue_flags, free_it, queue_it;
4651 
4652 	stmf_lport_xfer_done(itask, dbuf);
4653 
4654 	stmf_task_audit(itask, TE_XFER_DONE, iof, dbuf);
4655 
4656 	/* Guard against unexpected completions from the lport */
4657 	if (dbuf->db_flags & DB_LPORT_XFER_ACTIVE) {
4658 		dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4659 	} else {
4660 		/*
4661 		 * This should never happen.
4662 		 */
4663 		ilport = task->task_lport->lport_stmf_private;
4664 		ilport->ilport_unexpected_comp++;
4665 		cmn_err(CE_PANIC, "Unexpected xfer completion task %p dbuf %p",
4666 		    (void *)task, (void *)dbuf);
4667 		return;
4668 	}
4669 
4670 	mutex_enter(&w->worker_lock);
4671 	do {
4672 		new = old = itask->itask_flags;
4673 		if (old & ITASK_BEING_ABORTED) {
4674 			mutex_exit(&w->worker_lock);
4675 			return;
4676 		}
4677 		free_it = 0;
4678 		if (iof & STMF_IOF_LPORT_DONE) {
4679 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
4680 			task->task_completion_status = dbuf->db_xfer_status;
4681 			free_it = 1;
4682 		}
4683 		/*
4684 		 * If the task is known to LU then queue it. But if
4685 		 * it is already queued (multiple completions) then
4686 		 * just update the buffer information by grabbing the
4687 		 * worker lock. If the task is not known to LU,
4688 		 * completed/aborted, then see if we need to
4689 		 * free this task.
4690 		 */
4691 		if (old & ITASK_KNOWN_TO_LU) {
4692 			free_it = 0;
4693 			update_queue_flags = 1;
4694 			if (old & ITASK_IN_WORKER_QUEUE) {
4695 				queue_it = 0;
4696 			} else {
4697 				queue_it = 1;
4698 				new |= ITASK_IN_WORKER_QUEUE;
4699 			}
4700 		} else {
4701 			update_queue_flags = 0;
4702 			queue_it = 0;
4703 		}
4704 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4705 
4706 	if (update_queue_flags) {
4707 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
4708 
4709 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
4710 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
4711 		if (queue_it) {
4712 			itask->itask_worker_next = NULL;
4713 			if (w->worker_task_tail) {
4714 				w->worker_task_tail->itask_worker_next = itask;
4715 			} else {
4716 				w->worker_task_head = itask;
4717 			}
4718 			w->worker_task_tail = itask;
4719 			/* Measure task waitq time */
4720 			itask->itask_waitq_enter_timestamp = gethrtime();
4721 			if (++(w->worker_queue_depth) >
4722 			    w->worker_max_qdepth_pu) {
4723 				w->worker_max_qdepth_pu = w->worker_queue_depth;
4724 			}
4725 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4726 				cv_signal(&w->worker_cv);
4727 		}
4728 	}
4729 	mutex_exit(&w->worker_lock);
4730 
4731 	if (free_it) {
4732 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4733 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4734 		    ITASK_BEING_ABORTED)) == 0) {
4735 			stmf_task_free(task);
4736 		}
4737 	}
4738 }
4739 
4740 stmf_status_t
4741 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
4742 {
4743 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
4744 
4745 	stmf_i_scsi_task_t *itask =
4746 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4747 
4748 	stmf_task_audit(itask, TE_SEND_STATUS, ioflags, NULL);
4749 
4750 	if (ioflags & STMF_IOF_LU_DONE) {
4751 		uint32_t new, old;
4752 		do {
4753 			new = old = itask->itask_flags;
4754 			if (new & ITASK_BEING_ABORTED)
4755 				return (STMF_ABORTED);
4756 			new &= ~ITASK_KNOWN_TO_LU;
4757 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4758 	}
4759 
4760 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
4761 		return (STMF_SUCCESS);
4762 	}
4763 
4764 	if (itask->itask_flags & ITASK_BEING_ABORTED)
4765 		return (STMF_ABORTED);
4766 
4767 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4768 		task->task_status_ctrl = 0;
4769 		task->task_resid = 0;
4770 	} else if (task->task_cmd_xfer_length >
4771 	    task->task_expected_xfer_length) {
4772 		task->task_status_ctrl = TASK_SCTRL_OVER;
4773 		task->task_resid = task->task_cmd_xfer_length -
4774 		    task->task_expected_xfer_length;
4775 	} else if (task->task_nbytes_transferred <
4776 	    task->task_expected_xfer_length) {
4777 		task->task_status_ctrl = TASK_SCTRL_UNDER;
4778 		task->task_resid = task->task_expected_xfer_length -
4779 		    task->task_nbytes_transferred;
4780 	} else {
4781 		task->task_status_ctrl = 0;
4782 		task->task_resid = 0;
4783 	}
4784 	return (task->task_lport->lport_send_status(task, ioflags));
4785 }
4786 
4787 void
4788 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
4789 {
4790 	stmf_i_scsi_task_t *itask =
4791 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4792 	stmf_worker_t *w = itask->itask_worker;
4793 	uint32_t new, old;
4794 	uint8_t free_it, queue_it;
4795 
4796 	stmf_task_audit(itask, TE_SEND_STATUS_DONE, iof, NULL);
4797 
4798 	mutex_enter(&w->worker_lock);
4799 	do {
4800 		new = old = itask->itask_flags;
4801 		if (old & ITASK_BEING_ABORTED) {
4802 			mutex_exit(&w->worker_lock);
4803 			return;
4804 		}
4805 		free_it = 0;
4806 		if (iof & STMF_IOF_LPORT_DONE) {
4807 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
4808 			free_it = 1;
4809 		}
4810 		/*
4811 		 * If the task is known to LU then queue it. But if
4812 		 * it is already queued (multiple completions) then
4813 		 * just update the buffer information by grabbing the
4814 		 * worker lock. If the task is not known to LU,
4815 		 * completed/aborted, then see if we need to
4816 		 * free this task.
4817 		 */
4818 		if (old & ITASK_KNOWN_TO_LU) {
4819 			free_it = 0;
4820 			queue_it = 1;
4821 			if (old & ITASK_IN_WORKER_QUEUE) {
4822 				cmn_err(CE_PANIC, "status completion received"
4823 				    " when task is already in worker queue "
4824 				    " task = %p", (void *)task);
4825 			}
4826 			new |= ITASK_IN_WORKER_QUEUE;
4827 		} else {
4828 			queue_it = 0;
4829 		}
4830 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4831 	task->task_completion_status = s;
4832 
4833 
4834 	if (queue_it) {
4835 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
4836 		itask->itask_cmd_stack[itask->itask_ncmds++] =
4837 		    ITASK_CMD_STATUS_DONE;
4838 		itask->itask_worker_next = NULL;
4839 		if (w->worker_task_tail) {
4840 			w->worker_task_tail->itask_worker_next = itask;
4841 		} else {
4842 			w->worker_task_head = itask;
4843 		}
4844 		w->worker_task_tail = itask;
4845 		/* Measure task waitq time */
4846 		itask->itask_waitq_enter_timestamp = gethrtime();
4847 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4848 			w->worker_max_qdepth_pu = w->worker_queue_depth;
4849 		}
4850 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4851 			cv_signal(&w->worker_cv);
4852 	}
4853 	mutex_exit(&w->worker_lock);
4854 
4855 	if (free_it) {
4856 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4857 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4858 		    ITASK_BEING_ABORTED)) == 0) {
4859 			stmf_task_free(task);
4860 		} else {
4861 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
4862 			    " is not done, itask %p itask_flags %x",
4863 			    (void *)itask, itask->itask_flags);
4864 		}
4865 	}
4866 }
4867 
4868 void
4869 stmf_task_lu_done(scsi_task_t *task)
4870 {
4871 	stmf_i_scsi_task_t *itask =
4872 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4873 	stmf_worker_t *w = itask->itask_worker;
4874 	uint32_t new, old;
4875 
4876 	mutex_enter(&w->worker_lock);
4877 	do {
4878 		new = old = itask->itask_flags;
4879 		if (old & ITASK_BEING_ABORTED) {
4880 			mutex_exit(&w->worker_lock);
4881 			return;
4882 		}
4883 		if (old & ITASK_IN_WORKER_QUEUE) {
4884 			cmn_err(CE_PANIC, "task_lu_done received"
4885 			    " when task is in worker queue "
4886 			    " task = %p", (void *)task);
4887 		}
4888 		new &= ~ITASK_KNOWN_TO_LU;
4889 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4890 
4891 	mutex_exit(&w->worker_lock);
4892 
4893 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4894 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4895 	    ITASK_BEING_ABORTED)) == 0) {
4896 		stmf_task_free(task);
4897 	} else {
4898 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
4899 		    " the task is still not done, task = %p", (void *)task);
4900 	}
4901 }
4902 
4903 void
4904 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
4905 {
4906 	stmf_i_scsi_task_t *itask =
4907 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4908 	stmf_worker_t *w;
4909 	uint32_t old, new;
4910 
4911 	stmf_task_audit(itask, TE_TASK_ABORT, CMD_OR_IOF_NA, NULL);
4912 
4913 	do {
4914 		old = new = itask->itask_flags;
4915 		if ((old & ITASK_BEING_ABORTED) ||
4916 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
4917 		    ITASK_KNOWN_TO_LU)) == 0)) {
4918 			return;
4919 		}
4920 		new |= ITASK_BEING_ABORTED;
4921 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4922 	task->task_completion_status = s;
4923 	itask->itask_start_time = ddi_get_lbolt();
4924 
4925 	if (((w = itask->itask_worker) == NULL) ||
4926 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
4927 		return;
4928 	}
4929 
4930 	/* Queue it and get out */
4931 	mutex_enter(&w->worker_lock);
4932 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
4933 		mutex_exit(&w->worker_lock);
4934 		return;
4935 	}
4936 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
4937 	itask->itask_worker_next = NULL;
4938 	if (w->worker_task_tail) {
4939 		w->worker_task_tail->itask_worker_next = itask;
4940 	} else {
4941 		w->worker_task_head = itask;
4942 	}
4943 	w->worker_task_tail = itask;
4944 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4945 		w->worker_max_qdepth_pu = w->worker_queue_depth;
4946 	}
4947 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4948 		cv_signal(&w->worker_cv);
4949 	mutex_exit(&w->worker_lock);
4950 }
4951 
4952 void
4953 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
4954 {
4955 	stmf_i_scsi_task_t *itask = NULL;
4956 	uint32_t old, new, f, rf;
4957 
4958 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
4959 	    stmf_status_t, s);
4960 
4961 	switch (abort_cmd) {
4962 	case STMF_QUEUE_ABORT_LU:
4963 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
4964 		return;
4965 	case STMF_QUEUE_TASK_ABORT:
4966 		stmf_queue_task_for_abort(task, s);
4967 		return;
4968 	case STMF_REQUEUE_TASK_ABORT_LPORT:
4969 		rf = ITASK_TGT_PORT_ABORT_CALLED;
4970 		f = ITASK_KNOWN_TO_TGT_PORT;
4971 		break;
4972 	case STMF_REQUEUE_TASK_ABORT_LU:
4973 		rf = ITASK_LU_ABORT_CALLED;
4974 		f = ITASK_KNOWN_TO_LU;
4975 		break;
4976 	default:
4977 		return;
4978 	}
4979 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4980 	f |= ITASK_BEING_ABORTED | rf;
4981 	do {
4982 		old = new = itask->itask_flags;
4983 		if ((old & f) != f) {
4984 			return;
4985 		}
4986 		new &= ~rf;
4987 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4988 }
4989 
4990 void
4991 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
4992 {
4993 	char			 info[STMF_CHANGE_INFO_LEN];
4994 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
4995 	unsigned long long	st;
4996 
4997 	stmf_task_audit(itask, TE_TASK_LU_ABORTED, iof, NULL);
4998 
4999 	st = s;	/* gcc fix */
5000 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5001 		(void) snprintf(info, sizeof (info),
5002 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
5003 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
5004 		(void) snprintf(info, sizeof (info),
5005 		    "Task aborted but LU is not finished, task ="
5006 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
5007 	} else {
5008 		/*
5009 		 * LU abort successfully
5010 		 */
5011 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
5012 		return;
5013 	}
5014 
5015 	stmf_abort_task_offline(task, 1, info);
5016 }
5017 
5018 void
5019 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5020 {
5021 	char			info[STMF_CHANGE_INFO_LEN];
5022 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5023 	unsigned long long	st;
5024 	uint32_t		old, new;
5025 
5026 	stmf_task_audit(itask, TE_TASK_LPORT_ABORTED, iof, NULL);
5027 
5028 	st = s;
5029 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5030 		(void) snprintf(info, sizeof (info),
5031 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
5032 		    st);
5033 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
5034 		(void) snprintf(info, sizeof (info),
5035 		    "Task aborted but tgt port is not finished, "
5036 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
5037 	} else {
5038 		/*
5039 		 * LPORT abort successfully
5040 		 */
5041 		do {
5042 			old = new = itask->itask_flags;
5043 			if (!(old & ITASK_KNOWN_TO_TGT_PORT))
5044 				return;
5045 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
5046 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5047 		return;
5048 	}
5049 
5050 	stmf_abort_task_offline(task, 0, info);
5051 }
5052 
5053 stmf_status_t
5054 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
5055 {
5056 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5057 	    task->task_stmf_private;
5058 	stmf_worker_t *w = itask->itask_worker;
5059 	int i;
5060 
5061 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
5062 	mutex_enter(&w->worker_lock);
5063 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5064 		mutex_exit(&w->worker_lock);
5065 		return (STMF_BUSY);
5066 	}
5067 	for (i = 0; i < itask->itask_ncmds; i++) {
5068 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
5069 			mutex_exit(&w->worker_lock);
5070 			return (STMF_SUCCESS);
5071 		}
5072 	}
5073 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
5074 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5075 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5076 	} else {
5077 		clock_t t = drv_usectohz(timeout * 1000);
5078 		if (t == 0)
5079 			t = 1;
5080 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5081 	}
5082 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5083 		itask->itask_worker_next = NULL;
5084 		if (w->worker_task_tail) {
5085 			w->worker_task_tail->itask_worker_next = itask;
5086 		} else {
5087 			w->worker_task_head = itask;
5088 		}
5089 		w->worker_task_tail = itask;
5090 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5091 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5092 		}
5093 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5094 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5095 			cv_signal(&w->worker_cv);
5096 	}
5097 	mutex_exit(&w->worker_lock);
5098 	return (STMF_SUCCESS);
5099 }
5100 
5101 stmf_status_t
5102 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
5103 {
5104 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5105 	    task->task_stmf_private;
5106 	stmf_worker_t *w = itask->itask_worker;
5107 	int i;
5108 
5109 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
5110 	mutex_enter(&w->worker_lock);
5111 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5112 		mutex_exit(&w->worker_lock);
5113 		return (STMF_BUSY);
5114 	}
5115 	for (i = 0; i < itask->itask_ncmds; i++) {
5116 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
5117 			mutex_exit(&w->worker_lock);
5118 			return (STMF_SUCCESS);
5119 		}
5120 	}
5121 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
5122 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5123 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5124 	} else {
5125 		clock_t t = drv_usectohz(timeout * 1000);
5126 		if (t == 0)
5127 			t = 1;
5128 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
5129 	}
5130 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5131 		itask->itask_worker_next = NULL;
5132 		if (w->worker_task_tail) {
5133 			w->worker_task_tail->itask_worker_next = itask;
5134 		} else {
5135 			w->worker_task_head = itask;
5136 		}
5137 		w->worker_task_tail = itask;
5138 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5139 			w->worker_max_qdepth_pu = w->worker_queue_depth;
5140 		}
5141 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5142 			cv_signal(&w->worker_cv);
5143 	}
5144 	mutex_exit(&w->worker_lock);
5145 	return (STMF_SUCCESS);
5146 }
5147 
5148 void
5149 stmf_do_task_abort(scsi_task_t *task)
5150 {
5151 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
5152 	stmf_lu_t		*lu;
5153 	stmf_local_port_t	*lport;
5154 	unsigned long long	 ret;
5155 	uint32_t		 old, new;
5156 	uint8_t			 call_lu_abort, call_port_abort;
5157 	char			 info[STMF_CHANGE_INFO_LEN];
5158 
5159 	lu = task->task_lu;
5160 	lport = task->task_lport;
5161 	do {
5162 		old = new = itask->itask_flags;
5163 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
5164 		    ITASK_KNOWN_TO_LU) {
5165 			new |= ITASK_LU_ABORT_CALLED;
5166 			call_lu_abort = 1;
5167 		} else {
5168 			call_lu_abort = 0;
5169 		}
5170 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5171 
5172 	if (call_lu_abort) {
5173 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
5174 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5175 		} else {
5176 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5177 		}
5178 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5179 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
5180 		} else if (ret == STMF_BUSY) {
5181 			atomic_and_32(&itask->itask_flags,
5182 			    ~ITASK_LU_ABORT_CALLED);
5183 		} else if (ret != STMF_SUCCESS) {
5184 			(void) snprintf(info, sizeof (info),
5185 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
5186 			stmf_abort_task_offline(task, 1, info);
5187 		}
5188 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
5189 		if (ddi_get_lbolt() > (itask->itask_start_time +
5190 		    STMF_SEC2TICK(lu->lu_abort_timeout?
5191 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
5192 			(void) snprintf(info, sizeof (info),
5193 			    "lu abort timed out");
5194 			stmf_abort_task_offline(itask->itask_task, 1, info);
5195 		}
5196 	}
5197 
5198 	do {
5199 		old = new = itask->itask_flags;
5200 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
5201 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
5202 			new |= ITASK_TGT_PORT_ABORT_CALLED;
5203 			call_port_abort = 1;
5204 		} else {
5205 			call_port_abort = 0;
5206 		}
5207 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5208 	if (call_port_abort) {
5209 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
5210 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5211 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
5212 		} else if (ret == STMF_BUSY) {
5213 			atomic_and_32(&itask->itask_flags,
5214 			    ~ITASK_TGT_PORT_ABORT_CALLED);
5215 		} else if (ret != STMF_SUCCESS) {
5216 			(void) snprintf(info, sizeof (info),
5217 			    "Abort failed by tgt port %p ret %llx",
5218 			    (void *)lport, ret);
5219 			stmf_abort_task_offline(task, 0, info);
5220 		}
5221 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
5222 		if (ddi_get_lbolt() > (itask->itask_start_time +
5223 		    STMF_SEC2TICK(lport->lport_abort_timeout?
5224 		    lport->lport_abort_timeout :
5225 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
5226 			(void) snprintf(info, sizeof (info),
5227 			    "lport abort timed out");
5228 			stmf_abort_task_offline(itask->itask_task, 0, info);
5229 		}
5230 	}
5231 }
5232 
5233 stmf_status_t
5234 stmf_ctl(int cmd, void *obj, void *arg)
5235 {
5236 	stmf_status_t			ret;
5237 	stmf_i_lu_t			*ilu;
5238 	stmf_i_local_port_t		*ilport;
5239 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
5240 
5241 	mutex_enter(&stmf_state.stmf_lock);
5242 	ret = STMF_INVALID_ARG;
5243 	if (cmd & STMF_CMD_LU_OP) {
5244 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
5245 		if (ilu == NULL) {
5246 			goto stmf_ctl_lock_exit;
5247 		}
5248 		DTRACE_PROBE3(lu__state__change,
5249 		    stmf_lu_t *, ilu->ilu_lu,
5250 		    int, cmd, stmf_state_change_info_t *, ssci);
5251 	} else if (cmd & STMF_CMD_LPORT_OP) {
5252 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
5253 		if (ilport == NULL) {
5254 			goto stmf_ctl_lock_exit;
5255 		}
5256 		DTRACE_PROBE3(lport__state__change,
5257 		    stmf_local_port_t *, ilport->ilport_lport,
5258 		    int, cmd, stmf_state_change_info_t *, ssci);
5259 	} else {
5260 		goto stmf_ctl_lock_exit;
5261 	}
5262 
5263 	switch (cmd) {
5264 	case STMF_CMD_LU_ONLINE:
5265 		switch (ilu->ilu_state) {
5266 			case STMF_STATE_OFFLINE:
5267 				ret = STMF_SUCCESS;
5268 				break;
5269 			case STMF_STATE_ONLINE:
5270 			case STMF_STATE_ONLINING:
5271 				ret = STMF_ALREADY;
5272 				break;
5273 			case STMF_STATE_OFFLINING:
5274 				ret = STMF_BUSY;
5275 				break;
5276 			default:
5277 				ret = STMF_BADSTATE;
5278 				break;
5279 		}
5280 		if (ret != STMF_SUCCESS)
5281 			goto stmf_ctl_lock_exit;
5282 
5283 		ilu->ilu_state = STMF_STATE_ONLINING;
5284 		mutex_exit(&stmf_state.stmf_lock);
5285 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5286 		break;
5287 
5288 	case STMF_CMD_LU_ONLINE_COMPLETE:
5289 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
5290 			ret = STMF_BADSTATE;
5291 			goto stmf_ctl_lock_exit;
5292 		}
5293 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5294 		    STMF_SUCCESS) {
5295 			ilu->ilu_state = STMF_STATE_ONLINE;
5296 			mutex_exit(&stmf_state.stmf_lock);
5297 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5298 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
5299 			mutex_enter(&stmf_state.stmf_lock);
5300 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5301 		} else {
5302 			/* XXX: should throw a meesage an record more data */
5303 			ilu->ilu_state = STMF_STATE_OFFLINE;
5304 		}
5305 		ret = STMF_SUCCESS;
5306 		goto stmf_ctl_lock_exit;
5307 
5308 	case STMF_CMD_LU_OFFLINE:
5309 		switch (ilu->ilu_state) {
5310 			case STMF_STATE_ONLINE:
5311 				ret = STMF_SUCCESS;
5312 				break;
5313 			case STMF_STATE_OFFLINE:
5314 			case STMF_STATE_OFFLINING:
5315 				ret = STMF_ALREADY;
5316 				break;
5317 			case STMF_STATE_ONLINING:
5318 				ret = STMF_BUSY;
5319 				break;
5320 			default:
5321 				ret = STMF_BADSTATE;
5322 				break;
5323 		}
5324 		if (ret != STMF_SUCCESS)
5325 			goto stmf_ctl_lock_exit;
5326 		ilu->ilu_state = STMF_STATE_OFFLINING;
5327 		mutex_exit(&stmf_state.stmf_lock);
5328 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5329 		break;
5330 
5331 	case STMF_CMD_LU_OFFLINE_COMPLETE:
5332 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
5333 			ret = STMF_BADSTATE;
5334 			goto stmf_ctl_lock_exit;
5335 		}
5336 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5337 		    STMF_SUCCESS) {
5338 			ilu->ilu_state = STMF_STATE_OFFLINE;
5339 			mutex_exit(&stmf_state.stmf_lock);
5340 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5341 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
5342 			mutex_enter(&stmf_state.stmf_lock);
5343 		} else {
5344 			ilu->ilu_state = STMF_STATE_ONLINE;
5345 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5346 		}
5347 		mutex_exit(&stmf_state.stmf_lock);
5348 		break;
5349 
5350 	/*
5351 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
5352 	 * It's related with hardware disable/enable.
5353 	 */
5354 	case STMF_CMD_LPORT_ONLINE:
5355 		switch (ilport->ilport_state) {
5356 			case STMF_STATE_OFFLINE:
5357 				ret = STMF_SUCCESS;
5358 				break;
5359 			case STMF_STATE_ONLINE:
5360 			case STMF_STATE_ONLINING:
5361 				ret = STMF_ALREADY;
5362 				break;
5363 			case STMF_STATE_OFFLINING:
5364 				ret = STMF_BUSY;
5365 				break;
5366 			default:
5367 				ret = STMF_BADSTATE;
5368 				break;
5369 		}
5370 		if (ret != STMF_SUCCESS)
5371 			goto stmf_ctl_lock_exit;
5372 
5373 		/*
5374 		 * Only user request can recover the port from the
5375 		 * FORCED_OFFLINE state
5376 		 */
5377 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
5378 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
5379 				ret = STMF_FAILURE;
5380 				goto stmf_ctl_lock_exit;
5381 			}
5382 		}
5383 
5384 		/*
5385 		 * Avoid too frequent request to online
5386 		 */
5387 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
5388 			ilport->ilport_online_times = 0;
5389 			ilport->ilport_avg_interval = 0;
5390 		}
5391 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
5392 		    (ilport->ilport_online_times >= 4)) {
5393 			ret = STMF_FAILURE;
5394 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
5395 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
5396 			    "online the port");
5397 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
5398 			    "online the port, set FORCED_OFFLINE now");
5399 			goto stmf_ctl_lock_exit;
5400 		}
5401 		if (ilport->ilport_online_times > 0) {
5402 			if (ilport->ilport_online_times == 1) {
5403 				ilport->ilport_avg_interval = ddi_get_lbolt() -
5404 				    ilport->ilport_last_online_clock;
5405 			} else {
5406 				ilport->ilport_avg_interval =
5407 				    (ilport->ilport_avg_interval +
5408 				    ddi_get_lbolt() -
5409 				    ilport->ilport_last_online_clock) >> 1;
5410 			}
5411 		}
5412 		ilport->ilport_last_online_clock = ddi_get_lbolt();
5413 		ilport->ilport_online_times++;
5414 
5415 		/*
5416 		 * Submit online service request
5417 		 */
5418 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
5419 		ilport->ilport_state = STMF_STATE_ONLINING;
5420 		mutex_exit(&stmf_state.stmf_lock);
5421 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5422 		break;
5423 
5424 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
5425 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
5426 			ret = STMF_BADSTATE;
5427 			goto stmf_ctl_lock_exit;
5428 		}
5429 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5430 		    STMF_SUCCESS) {
5431 			ilport->ilport_state = STMF_STATE_ONLINE;
5432 			mutex_exit(&stmf_state.stmf_lock);
5433 			((stmf_local_port_t *)obj)->lport_ctl(
5434 			    (stmf_local_port_t *)obj,
5435 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
5436 			mutex_enter(&stmf_state.stmf_lock);
5437 		} else {
5438 			ilport->ilport_state = STMF_STATE_OFFLINE;
5439 		}
5440 		ret = STMF_SUCCESS;
5441 		goto stmf_ctl_lock_exit;
5442 
5443 	case STMF_CMD_LPORT_OFFLINE:
5444 		switch (ilport->ilport_state) {
5445 			case STMF_STATE_ONLINE:
5446 				ret = STMF_SUCCESS;
5447 				break;
5448 			case STMF_STATE_OFFLINE:
5449 			case STMF_STATE_OFFLINING:
5450 				ret = STMF_ALREADY;
5451 				break;
5452 			case STMF_STATE_ONLINING:
5453 				ret = STMF_BUSY;
5454 				break;
5455 			default:
5456 				ret = STMF_BADSTATE;
5457 				break;
5458 		}
5459 		if (ret != STMF_SUCCESS)
5460 			goto stmf_ctl_lock_exit;
5461 
5462 		ilport->ilport_state = STMF_STATE_OFFLINING;
5463 		mutex_exit(&stmf_state.stmf_lock);
5464 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5465 		break;
5466 
5467 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
5468 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
5469 			ret = STMF_BADSTATE;
5470 			goto stmf_ctl_lock_exit;
5471 		}
5472 		if (((stmf_change_status_t *)arg)->st_completion_status ==
5473 		    STMF_SUCCESS) {
5474 			ilport->ilport_state = STMF_STATE_OFFLINE;
5475 			mutex_exit(&stmf_state.stmf_lock);
5476 			((stmf_local_port_t *)obj)->lport_ctl(
5477 			    (stmf_local_port_t *)obj,
5478 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
5479 			mutex_enter(&stmf_state.stmf_lock);
5480 		} else {
5481 			ilport->ilport_state = STMF_STATE_ONLINE;
5482 		}
5483 		mutex_exit(&stmf_state.stmf_lock);
5484 		break;
5485 
5486 	default:
5487 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
5488 		ret = STMF_INVALID_ARG;
5489 		goto stmf_ctl_lock_exit;
5490 	}
5491 
5492 	return (STMF_SUCCESS);
5493 
5494 stmf_ctl_lock_exit:;
5495 	mutex_exit(&stmf_state.stmf_lock);
5496 	return (ret);
5497 }
5498 
5499 /* ARGSUSED */
5500 stmf_status_t
5501 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5502     uint32_t *bufsizep)
5503 {
5504 	return (STMF_NOT_SUPPORTED);
5505 }
5506 
5507 /* ARGSUSED */
5508 stmf_status_t
5509 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5510     uint32_t *bufsizep)
5511 {
5512 	uint32_t cl = SI_GET_CLASS(cmd);
5513 
5514 	if (cl == SI_STMF) {
5515 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
5516 	}
5517 	if (cl == SI_LPORT) {
5518 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
5519 		    arg2, buf, bufsizep));
5520 	} else if (cl == SI_LU) {
5521 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
5522 		    bufsizep));
5523 	}
5524 
5525 	return (STMF_NOT_SUPPORTED);
5526 }
5527 
5528 /*
5529  * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
5530  * stmf to register local ports. The ident should have 20 bytes in buffer
5531  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
5532  */
5533 void
5534 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
5535     uint8_t protocol_id)
5536 {
5537 	char wwn_str[20+1];
5538 
5539 	sdid->protocol_id = protocol_id;
5540 	sdid->piv = 1;
5541 	sdid->code_set = CODE_SET_ASCII;
5542 	sdid->association = ID_IS_TARGET_PORT;
5543 	sdid->ident_length = 20;
5544 	/* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
5545 	(void) snprintf(wwn_str, sizeof (wwn_str),
5546 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
5547 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
5548 	bcopy(wwn_str, (char *)sdid->ident, 20);
5549 }
5550 
5551 
5552 stmf_xfer_data_t *
5553 stmf_prepare_tpgs_data(uint8_t ilu_alua)
5554 {
5555 	stmf_xfer_data_t *xd;
5556 	stmf_i_local_port_t *ilport;
5557 	uint8_t *p;
5558 	uint32_t sz, asz, nports = 0, nports_standby = 0;
5559 
5560 	mutex_enter(&stmf_state.stmf_lock);
5561 	/* check if any ports are standby and create second group */
5562 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5563 	    ilport = ilport->ilport_next) {
5564 		if (ilport->ilport_standby == 1) {
5565 			nports_standby++;
5566 		} else {
5567 			nports++;
5568 		}
5569 	}
5570 
5571 	/* The spec only allows for 255 ports to be reported per group */
5572 	nports = min(nports, 255);
5573 	nports_standby = min(nports_standby, 255);
5574 	sz = (nports * 4) + 12;
5575 	if (nports_standby && ilu_alua) {
5576 		sz += (nports_standby * 4) + 8;
5577 	}
5578 	asz = sz + sizeof (*xd) - 4;
5579 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
5580 	if (xd == NULL) {
5581 		mutex_exit(&stmf_state.stmf_lock);
5582 		return (NULL);
5583 	}
5584 	xd->alloc_size = asz;
5585 	xd->size_left = sz;
5586 
5587 	p = xd->buf;
5588 
5589 	*((uint32_t *)p) = BE_32(sz - 4);
5590 	p += 4;
5591 	p[0] = 0x80;	/* PREF */
5592 	p[1] = 5;	/* AO_SUP, S_SUP */
5593 	if (stmf_state.stmf_alua_node == 1) {
5594 		p[3] = 1;	/* Group 1 */
5595 	} else {
5596 		p[3] = 0;	/* Group 0 */
5597 	}
5598 	p[7] = nports & 0xff;
5599 	p += 8;
5600 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5601 	    ilport = ilport->ilport_next) {
5602 		if (ilport->ilport_standby == 1) {
5603 			continue;
5604 		}
5605 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5606 		p += 4;
5607 	}
5608 	if (nports_standby && ilu_alua) {
5609 		p[0] = 0x02;	/* Non PREF, Standby */
5610 		p[1] = 5;	/* AO_SUP, S_SUP */
5611 		if (stmf_state.stmf_alua_node == 1) {
5612 			p[3] = 0;	/* Group 0 */
5613 		} else {
5614 			p[3] = 1;	/* Group 1 */
5615 		}
5616 		p[7] = nports_standby & 0xff;
5617 		p += 8;
5618 		for (ilport = stmf_state.stmf_ilportlist; ilport;
5619 		    ilport = ilport->ilport_next) {
5620 			if (ilport->ilport_standby == 0) {
5621 				continue;
5622 			}
5623 			((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5624 			p += 4;
5625 		}
5626 	}
5627 
5628 	mutex_exit(&stmf_state.stmf_lock);
5629 
5630 	return (xd);
5631 }
5632 
5633 struct scsi_devid_desc *
5634 stmf_scsilib_get_devid_desc(uint16_t rtpid)
5635 {
5636 	scsi_devid_desc_t *devid = NULL;
5637 	stmf_i_local_port_t *ilport;
5638 
5639 	mutex_enter(&stmf_state.stmf_lock);
5640 
5641 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5642 	    ilport = ilport->ilport_next) {
5643 		if (ilport->ilport_rtpid == rtpid) {
5644 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
5645 			uint32_t id_sz = sizeof (scsi_devid_desc_t) +
5646 			    id->ident_length;
5647 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
5648 			    KM_NOSLEEP);
5649 			if (devid != NULL) {
5650 				bcopy(id, devid, id_sz);
5651 			}
5652 			break;
5653 		}
5654 	}
5655 
5656 	mutex_exit(&stmf_state.stmf_lock);
5657 	return (devid);
5658 }
5659 
5660 uint16_t
5661 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
5662 {
5663 	stmf_i_local_port_t	*ilport;
5664 	scsi_devid_desc_t	*id;
5665 	uint16_t		rtpid = 0;
5666 
5667 	mutex_enter(&stmf_state.stmf_lock);
5668 	for (ilport = stmf_state.stmf_ilportlist; ilport;
5669 	    ilport = ilport->ilport_next) {
5670 		id = ilport->ilport_lport->lport_id;
5671 		if ((devid->ident_length == id->ident_length) &&
5672 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
5673 			rtpid = ilport->ilport_rtpid;
5674 			break;
5675 		}
5676 	}
5677 	mutex_exit(&stmf_state.stmf_lock);
5678 	return (rtpid);
5679 }
5680 
5681 static uint16_t stmf_lu_id_gen_number = 0;
5682 
5683 stmf_status_t
5684 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
5685 {
5686 	return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id));
5687 }
5688 
5689 stmf_status_t
5690 stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id,
5691     scsi_devid_desc_t *lu_id)
5692 {
5693 	uint8_t *p;
5694 	struct timeval32 timestamp32;
5695 	uint32_t *t = (uint32_t *)&timestamp32;
5696 	struct ether_addr mac;
5697 	uint8_t *e = (uint8_t *)&mac;
5698 	int hid = (int)host_id;
5699 	uint16_t gen_number;
5700 
5701 	if (company_id == COMPANY_ID_NONE)
5702 		company_id = COMPANY_ID_SUN;
5703 
5704 	if (lu_id->ident_length != 0x10)
5705 		return (STMF_INVALID_ARG);
5706 
5707 	p = (uint8_t *)lu_id;
5708 
5709 	gen_number = atomic_inc_16_nv(&stmf_lu_id_gen_number);
5710 
5711 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
5712 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
5713 	p[5] = (company_id >> 12) & 0xff;
5714 	p[6] = (company_id >> 4) & 0xff;
5715 	p[7] = (company_id << 4) & 0xf0;
5716 	if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) {
5717 		hid = BE_32((int)zone_get_hostid(NULL));
5718 	}
5719 	if (hid != 0) {
5720 		e[0] = (hid >> 24) & 0xff;
5721 		e[1] = (hid >> 16) & 0xff;
5722 		e[2] = (hid >> 8) & 0xff;
5723 		e[3] = hid & 0xff;
5724 		e[4] = e[5] = 0;
5725 	}
5726 	bcopy(e, p+8, 6);
5727 	uniqtime32(&timestamp32);
5728 	*t = BE_32(*t);
5729 	bcopy(t, p+14, 4);
5730 	p[18] = (gen_number >> 8) & 0xff;
5731 	p[19] = gen_number & 0xff;
5732 
5733 	return (STMF_SUCCESS);
5734 }
5735 
5736 /*
5737  * saa is sense key, ASC, ASCQ
5738  */
5739 void
5740 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
5741 {
5742 	uint8_t sd[18];
5743 	task->task_scsi_status = st;
5744 	if (st == 2) {
5745 		bzero(sd, 18);
5746 		sd[0] = 0x70;
5747 		sd[2] = (saa >> 16) & 0xf;
5748 		sd[7] = 10;
5749 		sd[12] = (saa >> 8) & 0xff;
5750 		sd[13] = saa & 0xff;
5751 		task->task_sense_data = sd;
5752 		task->task_sense_length = 18;
5753 	} else {
5754 		task->task_sense_data = NULL;
5755 		task->task_sense_length = 0;
5756 	}
5757 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
5758 }
5759 
5760 uint32_t
5761 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
5762     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
5763 {
5764 	uint8_t		*p = NULL;
5765 	uint8_t		small_buf[32];
5766 	uint32_t	sz = 0;
5767 	uint32_t	n = 4;
5768 	uint32_t	m = 0;
5769 	uint32_t	last_bit = 0;
5770 
5771 	if (page_len < 4)
5772 		return (0);
5773 	if (page_len > 65535)
5774 		page_len = 65535;
5775 
5776 	page[0] = byte0;
5777 	page[1] = 0x83;
5778 
5779 	/* CONSTCOND */
5780 	while (1) {
5781 		m += sz;
5782 		if (sz && (page_len > n)) {
5783 			uint32_t copysz;
5784 			copysz = page_len > (n + sz) ? sz : page_len - n;
5785 			bcopy(p, page + n, copysz);
5786 			n += copysz;
5787 		}
5788 		vpd_mask &= ~last_bit;
5789 		if (vpd_mask == 0)
5790 			break;
5791 
5792 		if (vpd_mask & STMF_VPD_LU_ID) {
5793 			last_bit = STMF_VPD_LU_ID;
5794 			sz = task->task_lu->lu_id->ident_length + 4;
5795 			p = (uint8_t *)task->task_lu->lu_id;
5796 			continue;
5797 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
5798 			last_bit = STMF_VPD_TARGET_ID;
5799 			sz = task->task_lport->lport_id->ident_length + 4;
5800 			p = (uint8_t *)task->task_lport->lport_id;
5801 			continue;
5802 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
5803 			stmf_i_local_port_t *ilport;
5804 			last_bit = STMF_VPD_TP_GROUP;
5805 			p = small_buf;
5806 			bzero(p, 8);
5807 			p[0] = 1;
5808 			p[1] = 0x15;
5809 			p[3] = 4;
5810 			ilport = (stmf_i_local_port_t *)
5811 			    task->task_lport->lport_stmf_private;
5812 			/*
5813 			 * If we're in alua mode, group 1 contains all alua
5814 			 * participating ports and all standby ports
5815 			 * > 255. Otherwise, if we're in alua mode, any local
5816 			 * ports (non standby/pppt) are also in group 1 if the
5817 			 * alua node is 1. Otherwise the group is 0.
5818 			 */
5819 			if ((stmf_state.stmf_alua_state &&
5820 			    (ilport->ilport_alua || ilport->ilport_standby) &&
5821 			    ilport->ilport_rtpid > 255) ||
5822 			    (stmf_state.stmf_alua_node == 1 &&
5823 			    ilport->ilport_standby != 1)) {
5824 				p[7] = 1;	/* Group 1 */
5825 			}
5826 			sz = 8;
5827 			continue;
5828 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
5829 			stmf_i_local_port_t *ilport;
5830 
5831 			last_bit = STMF_VPD_RELATIVE_TP_ID;
5832 			p = small_buf;
5833 			bzero(p, 8);
5834 			p[0] = 1;
5835 			p[1] = 0x14;
5836 			p[3] = 4;
5837 			ilport = (stmf_i_local_port_t *)
5838 			    task->task_lport->lport_stmf_private;
5839 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
5840 			p[7] = ilport->ilport_rtpid & 0xff;
5841 			sz = 8;
5842 			continue;
5843 		} else {
5844 			cmn_err(CE_WARN, "Invalid vpd_mask");
5845 			break;
5846 		}
5847 	}
5848 
5849 	page[2] = (m >> 8) & 0xff;
5850 	page[3] = m & 0xff;
5851 
5852 	return (n);
5853 }
5854 
5855 void
5856 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
5857 {
5858 	stmf_i_scsi_task_t *itask =
5859 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5860 	stmf_i_lu_t *ilu =
5861 	    (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5862 	stmf_xfer_data_t *xd;
5863 	uint32_t sz, minsz;
5864 
5865 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
5866 	task->task_cmd_xfer_length =
5867 	    ((((uint32_t)task->task_cdb[6]) << 24) |
5868 	    (((uint32_t)task->task_cdb[7]) << 16) |
5869 	    (((uint32_t)task->task_cdb[8]) << 8) |
5870 	    ((uint32_t)task->task_cdb[9]));
5871 
5872 	if (task->task_additional_flags &
5873 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5874 		task->task_expected_xfer_length =
5875 		    task->task_cmd_xfer_length;
5876 	}
5877 
5878 	if (task->task_cmd_xfer_length == 0) {
5879 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5880 		return;
5881 	}
5882 	if (task->task_cmd_xfer_length < 4) {
5883 		stmf_scsilib_send_status(task, STATUS_CHECK,
5884 		    STMF_SAA_INVALID_FIELD_IN_CDB);
5885 		return;
5886 	}
5887 
5888 	sz = min(task->task_expected_xfer_length,
5889 	    task->task_cmd_xfer_length);
5890 
5891 	xd = stmf_prepare_tpgs_data(ilu->ilu_alua);
5892 
5893 	if (xd == NULL) {
5894 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5895 		    STMF_ALLOC_FAILURE, NULL);
5896 		return;
5897 	}
5898 
5899 	sz = min(sz, xd->size_left);
5900 	xd->size_left = sz;
5901 	minsz = min(512, sz);
5902 
5903 	if (dbuf == NULL)
5904 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
5905 	if (dbuf == NULL) {
5906 		kmem_free(xd, xd->alloc_size);
5907 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5908 		    STMF_ALLOC_FAILURE, NULL);
5909 		return;
5910 	}
5911 	dbuf->db_lu_private = xd;
5912 	stmf_xd_to_dbuf(dbuf, 1);
5913 
5914 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5915 	(void) stmf_xfer_data(task, dbuf, 0);
5916 
5917 }
5918 
5919 void
5920 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
5921 {
5922 
5923 	switch (task->task_mgmt_function) {
5924 	/*
5925 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
5926 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
5927 	 * in these cases. This needs to be changed to abort only the required
5928 	 * set.
5929 	 */
5930 	case TM_ABORT_TASK:
5931 	case TM_ABORT_TASK_SET:
5932 	case TM_CLEAR_TASK_SET:
5933 	case TM_LUN_RESET:
5934 		stmf_handle_lun_reset(task);
5935 		/* issue the reset to the proxy node as well */
5936 		if (stmf_state.stmf_alua_state == 1) {
5937 			(void) stmf_proxy_scsi_cmd(task, NULL);
5938 		}
5939 		return;
5940 	case TM_TARGET_RESET:
5941 	case TM_TARGET_COLD_RESET:
5942 	case TM_TARGET_WARM_RESET:
5943 		stmf_handle_target_reset(task);
5944 		return;
5945 	default:
5946 		/* We dont support this task mgmt function */
5947 		stmf_scsilib_send_status(task, STATUS_CHECK,
5948 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
5949 		return;
5950 	}
5951 }
5952 
5953 void
5954 stmf_handle_lun_reset(scsi_task_t *task)
5955 {
5956 	stmf_i_scsi_task_t *itask;
5957 	stmf_i_lu_t *ilu;
5958 
5959 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
5960 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5961 
5962 	/*
5963 	 * To sync with target reset, grab this lock. The LU is not going
5964 	 * anywhere as there is atleast one task pending (this task).
5965 	 */
5966 	mutex_enter(&stmf_state.stmf_lock);
5967 
5968 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5969 		mutex_exit(&stmf_state.stmf_lock);
5970 		stmf_scsilib_send_status(task, STATUS_CHECK,
5971 		    STMF_SAA_OPERATION_IN_PROGRESS);
5972 		return;
5973 	}
5974 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
5975 	mutex_exit(&stmf_state.stmf_lock);
5976 
5977 	/*
5978 	 * Mark this task as the one causing LU reset so that we know who
5979 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
5980 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
5981 	 */
5982 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
5983 
5984 	/* Initiatiate abort on all commands on this LU except this one */
5985 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
5986 
5987 	/* Start polling on this task */
5988 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5989 	    != STMF_SUCCESS) {
5990 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
5991 		    NULL);
5992 		return;
5993 	}
5994 }
5995 
5996 void
5997 stmf_handle_target_reset(scsi_task_t *task)
5998 {
5999 	stmf_i_scsi_task_t *itask;
6000 	stmf_i_lu_t *ilu;
6001 	stmf_i_scsi_session_t *iss;
6002 	stmf_lun_map_t *lm;
6003 	stmf_lun_map_ent_t *lm_ent;
6004 	int i, lf;
6005 
6006 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6007 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
6008 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6009 
6010 	/*
6011 	 * To sync with LUN reset, grab this lock. The session is not going
6012 	 * anywhere as there is atleast one task pending (this task).
6013 	 */
6014 	mutex_enter(&stmf_state.stmf_lock);
6015 
6016 	/* Grab the session lock as a writer to prevent any changes in it */
6017 	rw_enter(iss->iss_lockp, RW_WRITER);
6018 
6019 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
6020 		rw_exit(iss->iss_lockp);
6021 		mutex_exit(&stmf_state.stmf_lock);
6022 		stmf_scsilib_send_status(task, STATUS_CHECK,
6023 		    STMF_SAA_OPERATION_IN_PROGRESS);
6024 		return;
6025 	}
6026 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
6027 
6028 	/*
6029 	 * Now go through each LUN in this session and make sure all of them
6030 	 * can be reset.
6031 	 */
6032 	lm = iss->iss_sm;
6033 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
6034 		if (lm->lm_plus[i] == NULL)
6035 			continue;
6036 		lf++;
6037 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6038 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6039 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6040 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6041 			rw_exit(iss->iss_lockp);
6042 			mutex_exit(&stmf_state.stmf_lock);
6043 			stmf_scsilib_send_status(task, STATUS_CHECK,
6044 			    STMF_SAA_OPERATION_IN_PROGRESS);
6045 			return;
6046 		}
6047 	}
6048 	if (lf == 0) {
6049 		/* No luns in this session */
6050 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6051 		rw_exit(iss->iss_lockp);
6052 		mutex_exit(&stmf_state.stmf_lock);
6053 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6054 		return;
6055 	}
6056 
6057 	/* ok, start the damage */
6058 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
6059 	    ITASK_CAUSING_TARGET_RESET;
6060 	for (i = 0; i < lm->lm_nentries; i++) {
6061 		if (lm->lm_plus[i] == NULL)
6062 			continue;
6063 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6064 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6065 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6066 	}
6067 
6068 	for (i = 0; i < lm->lm_nentries; i++) {
6069 		if (lm->lm_plus[i] == NULL)
6070 			continue;
6071 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6072 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
6073 		    lm_ent->ent_lu);
6074 	}
6075 
6076 	rw_exit(iss->iss_lockp);
6077 	mutex_exit(&stmf_state.stmf_lock);
6078 
6079 	/* Start polling on this task */
6080 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6081 	    != STMF_SUCCESS) {
6082 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6083 		    NULL);
6084 		return;
6085 	}
6086 }
6087 
6088 int
6089 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
6090 {
6091 	scsi_task_t *task = itask->itask_task;
6092 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6093 	    task->task_session->ss_stmf_private;
6094 
6095 	rw_enter(iss->iss_lockp, RW_WRITER);
6096 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
6097 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
6098 		rw_exit(iss->iss_lockp);
6099 		return (0);
6100 	}
6101 	atomic_and_32(&iss->iss_flags,
6102 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6103 	rw_exit(iss->iss_lockp);
6104 
6105 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
6106 		return (0);
6107 	}
6108 	stmf_scsilib_send_status(task, STATUS_CHECK,
6109 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
6110 	return (1);
6111 }
6112 
6113 void
6114 stmf_worker_init()
6115 {
6116 	uint32_t i;
6117 
6118 	/* Make local copy of global tunables */
6119 	stmf_i_max_nworkers = stmf_max_nworkers;
6120 	stmf_i_min_nworkers = stmf_min_nworkers;
6121 
6122 	ASSERT(stmf_workers == NULL);
6123 	if (stmf_i_min_nworkers < 4) {
6124 		stmf_i_min_nworkers = 4;
6125 	}
6126 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
6127 		stmf_i_max_nworkers = stmf_i_min_nworkers;
6128 	}
6129 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
6130 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
6131 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6132 		stmf_worker_t *w = &stmf_workers[i];
6133 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
6134 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
6135 	}
6136 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6137 	stmf_workers_state = STMF_WORKERS_ENABLED;
6138 
6139 	/* Workers will be started by stmf_worker_mgmt() */
6140 
6141 	/* Lets wait for atleast one worker to start */
6142 	while (stmf_nworkers_cur == 0)
6143 		delay(drv_usectohz(20 * 1000));
6144 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
6145 }
6146 
6147 stmf_status_t
6148 stmf_worker_fini()
6149 {
6150 	int i;
6151 	clock_t sb;
6152 
6153 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
6154 		return (STMF_SUCCESS);
6155 	ASSERT(stmf_workers);
6156 	stmf_workers_state = STMF_WORKERS_DISABLED;
6157 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6158 	cv_signal(&stmf_state.stmf_cv);
6159 
6160 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
6161 	/* Wait for all the threads to die */
6162 	while (stmf_nworkers_cur != 0) {
6163 		if (ddi_get_lbolt() > sb) {
6164 			stmf_workers_state = STMF_WORKERS_ENABLED;
6165 			return (STMF_BUSY);
6166 		}
6167 		delay(drv_usectohz(100 * 1000));
6168 	}
6169 	for (i = 0; i < stmf_i_max_nworkers; i++) {
6170 		stmf_worker_t *w = &stmf_workers[i];
6171 		mutex_destroy(&w->worker_lock);
6172 		cv_destroy(&w->worker_cv);
6173 	}
6174 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
6175 	stmf_workers = NULL;
6176 
6177 	return (STMF_SUCCESS);
6178 }
6179 
6180 void
6181 stmf_worker_task(void *arg)
6182 {
6183 	stmf_worker_t *w;
6184 	stmf_i_scsi_session_t *iss;
6185 	scsi_task_t *task;
6186 	stmf_i_scsi_task_t *itask;
6187 	stmf_data_buf_t *dbuf;
6188 	stmf_lu_t *lu;
6189 	clock_t wait_timer = 0;
6190 	clock_t wait_ticks, wait_delta = 0;
6191 	uint32_t old, new;
6192 	uint8_t curcmd;
6193 	uint8_t abort_free;
6194 	uint8_t wait_queue;
6195 	uint8_t dec_qdepth;
6196 
6197 	w = (stmf_worker_t *)arg;
6198 	wait_ticks = drv_usectohz(10000);
6199 
6200 	DTRACE_PROBE1(worker__create, stmf_worker_t, w);
6201 	mutex_enter(&w->worker_lock);
6202 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
6203 stmf_worker_loop:;
6204 	if ((w->worker_ref_count == 0) &&
6205 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
6206 		w->worker_flags &= ~(STMF_WORKER_STARTED |
6207 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
6208 		w->worker_tid = NULL;
6209 		mutex_exit(&w->worker_lock);
6210 		DTRACE_PROBE1(worker__destroy, stmf_worker_t, w);
6211 		thread_exit();
6212 	}
6213 	/* CONSTCOND */
6214 	while (1) {
6215 		dec_qdepth = 0;
6216 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
6217 			wait_timer = 0;
6218 			wait_delta = 0;
6219 			if (w->worker_wait_head) {
6220 				ASSERT(w->worker_wait_tail);
6221 				if (w->worker_task_head == NULL)
6222 					w->worker_task_head =
6223 					    w->worker_wait_head;
6224 				else
6225 					w->worker_task_tail->itask_worker_next =
6226 					    w->worker_wait_head;
6227 				w->worker_task_tail = w->worker_wait_tail;
6228 				w->worker_wait_head = w->worker_wait_tail =
6229 				    NULL;
6230 			}
6231 		}
6232 		if ((itask = w->worker_task_head) == NULL) {
6233 			break;
6234 		}
6235 		task = itask->itask_task;
6236 		DTRACE_PROBE2(worker__active, stmf_worker_t, w,
6237 		    scsi_task_t *, task);
6238 		w->worker_task_head = itask->itask_worker_next;
6239 		if (w->worker_task_head == NULL)
6240 			w->worker_task_tail = NULL;
6241 
6242 		wait_queue = 0;
6243 		abort_free = 0;
6244 		if (itask->itask_ncmds > 0) {
6245 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
6246 		} else {
6247 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
6248 		}
6249 		do {
6250 			old = itask->itask_flags;
6251 			if (old & ITASK_BEING_ABORTED) {
6252 				itask->itask_ncmds = 1;
6253 				curcmd = itask->itask_cmd_stack[0] =
6254 				    ITASK_CMD_ABORT;
6255 				goto out_itask_flag_loop;
6256 			} else if ((curcmd & ITASK_CMD_MASK) ==
6257 			    ITASK_CMD_NEW_TASK) {
6258 				/*
6259 				 * set ITASK_KSTAT_IN_RUNQ, this flag
6260 				 * will not reset until task completed
6261 				 */
6262 				new = old | ITASK_KNOWN_TO_LU |
6263 				    ITASK_KSTAT_IN_RUNQ;
6264 			} else {
6265 				goto out_itask_flag_loop;
6266 			}
6267 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
6268 
6269 out_itask_flag_loop:
6270 
6271 		/*
6272 		 * Decide if this task needs to go to a queue and/or if
6273 		 * we can decrement the itask_cmd_stack.
6274 		 */
6275 		if (curcmd == ITASK_CMD_ABORT) {
6276 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
6277 			    ITASK_KNOWN_TO_TGT_PORT)) {
6278 				wait_queue = 1;
6279 			} else {
6280 				abort_free = 1;
6281 			}
6282 		} else if ((curcmd & ITASK_CMD_POLL) &&
6283 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
6284 			wait_queue = 1;
6285 		}
6286 
6287 		if (wait_queue) {
6288 			itask->itask_worker_next = NULL;
6289 			if (w->worker_wait_tail) {
6290 				w->worker_wait_tail->itask_worker_next = itask;
6291 			} else {
6292 				w->worker_wait_head = itask;
6293 			}
6294 			w->worker_wait_tail = itask;
6295 			if (wait_timer == 0) {
6296 				wait_timer = ddi_get_lbolt() + wait_ticks;
6297 				wait_delta = wait_ticks;
6298 			}
6299 		} else if ((--(itask->itask_ncmds)) != 0) {
6300 			itask->itask_worker_next = NULL;
6301 			if (w->worker_task_tail) {
6302 				w->worker_task_tail->itask_worker_next = itask;
6303 			} else {
6304 				w->worker_task_head = itask;
6305 			}
6306 			w->worker_task_tail = itask;
6307 		} else {
6308 			atomic_and_32(&itask->itask_flags,
6309 			    ~ITASK_IN_WORKER_QUEUE);
6310 			/*
6311 			 * This is where the queue depth should go down by
6312 			 * one but we delay that on purpose to account for
6313 			 * the call into the provider. The actual decrement
6314 			 * happens after the worker has done its job.
6315 			 */
6316 			dec_qdepth = 1;
6317 			itask->itask_waitq_time +=
6318 			    gethrtime() - itask->itask_waitq_enter_timestamp;
6319 		}
6320 
6321 		/* We made it here means we are going to call LU */
6322 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
6323 			lu = task->task_lu;
6324 		else
6325 			lu = dlun0;
6326 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
6327 		mutex_exit(&w->worker_lock);
6328 		curcmd &= ITASK_CMD_MASK;
6329 		stmf_task_audit(itask, TE_PROCESS_CMD, curcmd, dbuf);
6330 		switch (curcmd) {
6331 		case ITASK_CMD_NEW_TASK:
6332 			iss = (stmf_i_scsi_session_t *)
6333 			    task->task_session->ss_stmf_private;
6334 			stmf_itl_lu_new_task(itask);
6335 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
6336 				if (stmf_handle_cmd_during_ic(itask))
6337 					break;
6338 			}
6339 #ifdef	DEBUG
6340 			if (stmf_drop_task_counter > 0) {
6341 				if (atomic_dec_32_nv(&stmf_drop_task_counter)
6342 				    == 1)
6343 					break;
6344 			}
6345 #endif
6346 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
6347 			lu->lu_new_task(task, dbuf);
6348 			break;
6349 		case ITASK_CMD_DATA_XFER_DONE:
6350 			lu->lu_dbuf_xfer_done(task, dbuf);
6351 			break;
6352 		case ITASK_CMD_STATUS_DONE:
6353 			lu->lu_send_status_done(task);
6354 			break;
6355 		case ITASK_CMD_ABORT:
6356 			if (abort_free) {
6357 				stmf_task_free(task);
6358 			} else {
6359 				stmf_do_task_abort(task);
6360 			}
6361 			break;
6362 		case ITASK_CMD_POLL_LU:
6363 			if (!wait_queue) {
6364 				lu->lu_task_poll(task);
6365 			}
6366 			break;
6367 		case ITASK_CMD_POLL_LPORT:
6368 			if (!wait_queue)
6369 				task->task_lport->lport_task_poll(task);
6370 			break;
6371 		case ITASK_CMD_SEND_STATUS:
6372 		/* case ITASK_CMD_XFER_DATA: */
6373 			break;
6374 		}
6375 		mutex_enter(&w->worker_lock);
6376 		if (dec_qdepth) {
6377 			w->worker_queue_depth--;
6378 		}
6379 	}
6380 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
6381 		if (w->worker_ref_count == 0)
6382 			goto stmf_worker_loop;
6383 		else {
6384 			wait_timer = ddi_get_lbolt() + 1;
6385 			wait_delta = 1;
6386 		}
6387 	}
6388 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
6389 	if (wait_timer) {
6390 		DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w);
6391 		(void) cv_reltimedwait(&w->worker_cv, &w->worker_lock,
6392 		    wait_delta, TR_CLOCK_TICK);
6393 	} else {
6394 		DTRACE_PROBE1(worker__sleep, stmf_worker_t, w);
6395 		cv_wait(&w->worker_cv, &w->worker_lock);
6396 	}
6397 	DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w);
6398 	w->worker_flags |= STMF_WORKER_ACTIVE;
6399 	goto stmf_worker_loop;
6400 }
6401 
6402 void
6403 stmf_worker_mgmt()
6404 {
6405 	int i;
6406 	int workers_needed;
6407 	uint32_t qd;
6408 	clock_t tps, d = 0;
6409 	uint32_t cur_max_ntasks = 0;
6410 	stmf_worker_t *w;
6411 
6412 	/* Check if we are trying to increase the # of threads */
6413 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
6414 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
6415 			stmf_nworkers_cur++;
6416 			stmf_nworkers_accepting_cmds++;
6417 		} else {
6418 			/* Wait for transition to complete */
6419 			return;
6420 		}
6421 	}
6422 	/* Check if we are trying to decrease the # of workers */
6423 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6424 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
6425 			stmf_nworkers_cur--;
6426 			/*
6427 			 * stmf_nworkers_accepting_cmds has already been
6428 			 * updated by the request to reduce the # of workers.
6429 			 */
6430 		} else {
6431 			/* Wait for transition to complete */
6432 			return;
6433 		}
6434 	}
6435 	/* Check if we are being asked to quit */
6436 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
6437 		if (stmf_nworkers_cur) {
6438 			workers_needed = 0;
6439 			goto worker_mgmt_trigger_change;
6440 		}
6441 		return;
6442 	}
6443 	/* Check if we are starting */
6444 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
6445 		workers_needed = stmf_i_min_nworkers;
6446 		goto worker_mgmt_trigger_change;
6447 	}
6448 
6449 	tps = drv_usectohz(1 * 1000 * 1000);
6450 	if ((stmf_wm_last != 0) &&
6451 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
6452 		qd = 0;
6453 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
6454 			qd += stmf_workers[i].worker_max_qdepth_pu;
6455 			stmf_workers[i].worker_max_qdepth_pu = 0;
6456 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
6457 			    cur_max_ntasks) {
6458 				cur_max_ntasks =
6459 				    stmf_workers[i].worker_max_sys_qdepth_pu;
6460 			}
6461 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
6462 		}
6463 	}
6464 	stmf_wm_last = ddi_get_lbolt();
6465 	if (d <= tps) {
6466 		/* still ramping up */
6467 		return;
6468 	}
6469 	/* max qdepth cannot be more than max tasks */
6470 	if (qd > cur_max_ntasks)
6471 		qd = cur_max_ntasks;
6472 
6473 	/* See if we have more workers */
6474 	if (qd < stmf_nworkers_accepting_cmds) {
6475 		/*
6476 		 * Since we dont reduce the worker count right away, monitor
6477 		 * the highest load during the scale_down_delay.
6478 		 */
6479 		if (qd > stmf_worker_scale_down_qd)
6480 			stmf_worker_scale_down_qd = qd;
6481 		if (stmf_worker_scale_down_timer == 0) {
6482 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
6483 			    drv_usectohz(stmf_worker_scale_down_delay *
6484 			    1000 * 1000);
6485 			return;
6486 		}
6487 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
6488 			return;
6489 		}
6490 		/* Its time to reduce the workers */
6491 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
6492 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
6493 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
6494 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
6495 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
6496 			return;
6497 		workers_needed = stmf_worker_scale_down_qd;
6498 		stmf_worker_scale_down_qd = 0;
6499 		goto worker_mgmt_trigger_change;
6500 	}
6501 	stmf_worker_scale_down_qd = 0;
6502 	stmf_worker_scale_down_timer = 0;
6503 	if (qd > stmf_i_max_nworkers)
6504 		qd = stmf_i_max_nworkers;
6505 	if (qd < stmf_i_min_nworkers)
6506 		qd = stmf_i_min_nworkers;
6507 	if (qd == stmf_nworkers_cur)
6508 		return;
6509 	workers_needed = qd;
6510 	goto worker_mgmt_trigger_change;
6511 
6512 	/* NOTREACHED */
6513 	return;
6514 
6515 worker_mgmt_trigger_change:
6516 	ASSERT(workers_needed != stmf_nworkers_cur);
6517 	if (workers_needed > stmf_nworkers_cur) {
6518 		stmf_nworkers_needed = workers_needed;
6519 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
6520 			w = &stmf_workers[i];
6521 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
6522 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
6523 			    minclsyspri);
6524 		}
6525 		return;
6526 	}
6527 	/* At this point we know that we are decreasing the # of workers */
6528 	stmf_nworkers_accepting_cmds = workers_needed;
6529 	stmf_nworkers_needed = workers_needed;
6530 	/* Signal the workers that its time to quit */
6531 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6532 		w = &stmf_workers[i];
6533 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
6534 		mutex_enter(&w->worker_lock);
6535 		w->worker_flags |= STMF_WORKER_TERMINATE;
6536 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
6537 			cv_signal(&w->worker_cv);
6538 		mutex_exit(&w->worker_lock);
6539 	}
6540 }
6541 
6542 /*
6543  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
6544  * If all the data has been filled out, frees the xd and makes
6545  * db_lu_private NULL.
6546  */
6547 void
6548 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off)
6549 {
6550 	stmf_xfer_data_t *xd;
6551 	uint8_t *p;
6552 	int i;
6553 	uint32_t s;
6554 
6555 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6556 	dbuf->db_data_size = 0;
6557 	if (set_rel_off)
6558 		dbuf->db_relative_offset = xd->size_done;
6559 	for (i = 0; i < dbuf->db_sglist_length; i++) {
6560 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
6561 		p = &xd->buf[xd->size_done];
6562 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
6563 		xd->size_left -= s;
6564 		xd->size_done += s;
6565 		dbuf->db_data_size += s;
6566 		if (xd->size_left == 0) {
6567 			kmem_free(xd, xd->alloc_size);
6568 			dbuf->db_lu_private = NULL;
6569 			return;
6570 		}
6571 	}
6572 }
6573 
6574 /* ARGSUSED */
6575 stmf_status_t
6576 stmf_dlun0_task_alloc(scsi_task_t *task)
6577 {
6578 	return (STMF_SUCCESS);
6579 }
6580 
6581 void
6582 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
6583 {
6584 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
6585 	stmf_i_scsi_session_t *iss;
6586 	uint32_t sz, minsz;
6587 	uint8_t *p;
6588 	stmf_xfer_data_t *xd;
6589 	uint8_t inq_page_length = 31;
6590 
6591 	if (task->task_mgmt_function) {
6592 		stmf_scsilib_handle_task_mgmt(task);
6593 		return;
6594 	}
6595 
6596 	switch (cdbp[0]) {
6597 	case SCMD_INQUIRY:
6598 		/*
6599 		 * Basic protocol checks.  In addition, only reply to
6600 		 * standard inquiry.  Otherwise, the LU provider needs
6601 		 * to respond.
6602 		 */
6603 
6604 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
6605 			stmf_scsilib_send_status(task, STATUS_CHECK,
6606 			    STMF_SAA_INVALID_FIELD_IN_CDB);
6607 			return;
6608 		}
6609 
6610 		task->task_cmd_xfer_length =
6611 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
6612 
6613 		if (task->task_additional_flags &
6614 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6615 			task->task_expected_xfer_length =
6616 			    task->task_cmd_xfer_length;
6617 		}
6618 
6619 		sz = min(task->task_expected_xfer_length,
6620 		    min(36, task->task_cmd_xfer_length));
6621 		minsz = 36;
6622 
6623 		if (sz == 0) {
6624 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6625 			return;
6626 		}
6627 
6628 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
6629 			/*
6630 			 * Ignore any preallocated dbuf if the size is less
6631 			 * than 36. It will be freed during the task_free.
6632 			 */
6633 			dbuf = NULL;
6634 		}
6635 		if (dbuf == NULL)
6636 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
6637 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
6638 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6639 			    STMF_ALLOC_FAILURE, NULL);
6640 			return;
6641 		}
6642 		dbuf->db_lu_private = NULL;
6643 
6644 		p = dbuf->db_sglist[0].seg_addr;
6645 
6646 		/*
6647 		 * Standard inquiry handling only.
6648 		 */
6649 
6650 		bzero(p, inq_page_length + 5);
6651 
6652 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
6653 		p[2] = 5;
6654 		p[3] = 0x12;
6655 		p[4] = inq_page_length;
6656 		p[6] = 0x80;
6657 
6658 		(void) strncpy((char *)p+8, "SUN     ", 8);
6659 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
6660 		(void) strncpy((char *)p+32, "1.0 ", 4);
6661 
6662 		dbuf->db_data_size = sz;
6663 		dbuf->db_relative_offset = 0;
6664 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6665 		(void) stmf_xfer_data(task, dbuf, 0);
6666 
6667 		return;
6668 
6669 	case SCMD_REPORT_LUNS:
6670 		task->task_cmd_xfer_length =
6671 		    ((((uint32_t)task->task_cdb[6]) << 24) |
6672 		    (((uint32_t)task->task_cdb[7]) << 16) |
6673 		    (((uint32_t)task->task_cdb[8]) << 8) |
6674 		    ((uint32_t)task->task_cdb[9]));
6675 
6676 		if (task->task_additional_flags &
6677 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6678 			task->task_expected_xfer_length =
6679 			    task->task_cmd_xfer_length;
6680 		}
6681 
6682 		sz = min(task->task_expected_xfer_length,
6683 		    task->task_cmd_xfer_length);
6684 
6685 		if (sz < 16) {
6686 			stmf_scsilib_send_status(task, STATUS_CHECK,
6687 			    STMF_SAA_INVALID_FIELD_IN_CDB);
6688 			return;
6689 		}
6690 
6691 		iss = (stmf_i_scsi_session_t *)
6692 		    task->task_session->ss_stmf_private;
6693 		rw_enter(iss->iss_lockp, RW_WRITER);
6694 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
6695 		rw_exit(iss->iss_lockp);
6696 
6697 		if (xd == NULL) {
6698 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6699 			    STMF_ALLOC_FAILURE, NULL);
6700 			return;
6701 		}
6702 
6703 		sz = min(sz, xd->size_left);
6704 		xd->size_left = sz;
6705 		minsz = min(512, sz);
6706 
6707 		if (dbuf == NULL)
6708 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
6709 		if (dbuf == NULL) {
6710 			kmem_free(xd, xd->alloc_size);
6711 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6712 			    STMF_ALLOC_FAILURE, NULL);
6713 			return;
6714 		}
6715 		dbuf->db_lu_private = xd;
6716 		stmf_xd_to_dbuf(dbuf, 1);
6717 
6718 		atomic_and_32(&iss->iss_flags,
6719 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6720 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6721 		(void) stmf_xfer_data(task, dbuf, 0);
6722 		return;
6723 	}
6724 
6725 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
6726 }
6727 
6728 void
6729 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
6730 {
6731 	stmf_i_scsi_task_t *itask =
6732 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
6733 
6734 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
6735 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6736 		    dbuf->db_xfer_status, NULL);
6737 		return;
6738 	}
6739 	task->task_nbytes_transferred += dbuf->db_data_size;
6740 	if (dbuf->db_lu_private) {
6741 		/* There is more */
6742 		stmf_xd_to_dbuf(dbuf, 1);
6743 		(void) stmf_xfer_data(task, dbuf, 0);
6744 		return;
6745 	}
6746 
6747 	stmf_free_dbuf(task, dbuf);
6748 	/*
6749 	 * If this is a proxy task, it will need to be completed from the
6750 	 * proxy port provider. This message lets pppt know that the xfer
6751 	 * is complete. When we receive the status from pppt, we will
6752 	 * then relay that status back to the lport.
6753 	 */
6754 	if (itask->itask_flags & ITASK_PROXY_TASK) {
6755 		stmf_ic_msg_t *ic_xfer_done_msg = NULL;
6756 		stmf_status_t ic_ret = STMF_FAILURE;
6757 		uint64_t session_msg_id;
6758 		mutex_enter(&stmf_state.stmf_lock);
6759 		session_msg_id = stmf_proxy_msg_id++;
6760 		mutex_exit(&stmf_state.stmf_lock);
6761 		/* send xfer done status to pppt */
6762 		ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
6763 		    itask->itask_proxy_msg_id,
6764 		    task->task_session->ss_session_id,
6765 		    STMF_SUCCESS, session_msg_id);
6766 		if (ic_xfer_done_msg) {
6767 			ic_ret = ic_tx_msg(ic_xfer_done_msg);
6768 			if (ic_ret != STMF_IC_MSG_SUCCESS) {
6769 				cmn_err(CE_WARN, "unable to xmit session msg");
6770 			}
6771 		}
6772 		/* task will be completed from pppt */
6773 		return;
6774 	}
6775 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6776 }
6777 
6778 /* ARGSUSED */
6779 void
6780 stmf_dlun0_status_done(scsi_task_t *task)
6781 {
6782 }
6783 
6784 /* ARGSUSED */
6785 void
6786 stmf_dlun0_task_free(scsi_task_t *task)
6787 {
6788 }
6789 
6790 /* ARGSUSED */
6791 stmf_status_t
6792 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
6793 {
6794 	scsi_task_t *task = (scsi_task_t *)arg;
6795 	stmf_i_scsi_task_t *itask =
6796 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
6797 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6798 	int i;
6799 	uint8_t map;
6800 
6801 	if ((task->task_mgmt_function) && (itask->itask_flags &
6802 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
6803 		switch (task->task_mgmt_function) {
6804 		case TM_ABORT_TASK:
6805 		case TM_ABORT_TASK_SET:
6806 		case TM_CLEAR_TASK_SET:
6807 		case TM_LUN_RESET:
6808 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6809 			break;
6810 		case TM_TARGET_RESET:
6811 		case TM_TARGET_COLD_RESET:
6812 		case TM_TARGET_WARM_RESET:
6813 			stmf_abort_target_reset(task);
6814 			break;
6815 		}
6816 		return (STMF_ABORT_SUCCESS);
6817 	}
6818 
6819 	/*
6820 	 * OK so its not a task mgmt. Make sure we free any xd sitting
6821 	 * inside any dbuf.
6822 	 */
6823 	if ((map = itask->itask_allocated_buf_map) != 0) {
6824 		for (i = 0; i < 4; i++) {
6825 			if ((map & 1) &&
6826 			    ((itask->itask_dbufs[i])->db_lu_private)) {
6827 				stmf_xfer_data_t *xd;
6828 				stmf_data_buf_t *dbuf;
6829 
6830 				dbuf = itask->itask_dbufs[i];
6831 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6832 				dbuf->db_lu_private = NULL;
6833 				kmem_free(xd, xd->alloc_size);
6834 			}
6835 			map >>= 1;
6836 		}
6837 	}
6838 	return (STMF_ABORT_SUCCESS);
6839 }
6840 
6841 void
6842 stmf_dlun0_task_poll(struct scsi_task *task)
6843 {
6844 	/* Right now we only do this for handling task management functions */
6845 	ASSERT(task->task_mgmt_function);
6846 
6847 	switch (task->task_mgmt_function) {
6848 	case TM_ABORT_TASK:
6849 	case TM_ABORT_TASK_SET:
6850 	case TM_CLEAR_TASK_SET:
6851 	case TM_LUN_RESET:
6852 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
6853 		return;
6854 	case TM_TARGET_RESET:
6855 	case TM_TARGET_COLD_RESET:
6856 	case TM_TARGET_WARM_RESET:
6857 		stmf_target_reset_poll(task);
6858 		return;
6859 	}
6860 }
6861 
6862 /* ARGSUSED */
6863 void
6864 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
6865 {
6866 	/* This function will never be called */
6867 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
6868 }
6869 
6870 void
6871 stmf_dlun_init()
6872 {
6873 	stmf_i_lu_t *ilu;
6874 
6875 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
6876 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
6877 	dlun0->lu_new_task = stmf_dlun0_new_task;
6878 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
6879 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
6880 	dlun0->lu_task_free = stmf_dlun0_task_free;
6881 	dlun0->lu_abort = stmf_dlun0_abort;
6882 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
6883 	dlun0->lu_ctl = stmf_dlun0_ctl;
6884 
6885 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
6886 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
6887 }
6888 
6889 stmf_status_t
6890 stmf_dlun_fini()
6891 {
6892 	stmf_i_lu_t *ilu;
6893 
6894 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
6895 
6896 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
6897 	if (ilu->ilu_ntasks) {
6898 		stmf_i_scsi_task_t *itask, *nitask;
6899 
6900 		nitask = ilu->ilu_tasks;
6901 		do {
6902 			itask = nitask;
6903 			nitask = itask->itask_lu_next;
6904 			dlun0->lu_task_free(itask->itask_task);
6905 			stmf_free(itask->itask_task);
6906 		} while (nitask != NULL);
6907 
6908 	}
6909 	stmf_free(dlun0);
6910 	return (STMF_SUCCESS);
6911 }
6912 
6913 void
6914 stmf_abort_target_reset(scsi_task_t *task)
6915 {
6916 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6917 	    task->task_session->ss_stmf_private;
6918 	stmf_lun_map_t *lm;
6919 	stmf_lun_map_ent_t *lm_ent;
6920 	stmf_i_lu_t *ilu;
6921 	int i;
6922 
6923 	rw_enter(iss->iss_lockp, RW_READER);
6924 	lm = iss->iss_sm;
6925 	for (i = 0; i < lm->lm_nentries; i++) {
6926 		if (lm->lm_plus[i] == NULL)
6927 			continue;
6928 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6929 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
6930 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6931 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6932 		}
6933 	}
6934 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6935 	rw_exit(iss->iss_lockp);
6936 }
6937 
6938 /*
6939  * The return value is only used by function managing target reset.
6940  */
6941 stmf_status_t
6942 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
6943 {
6944 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
6945 	int ntasks_pending;
6946 
6947 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
6948 	/*
6949 	 * This function is also used during Target reset. The idea is that
6950 	 * once all the commands are aborted, call the LU's reset entry
6951 	 * point (abort entry point with a reset flag). But if this Task
6952 	 * mgmt is running on this LU then all the tasks cannot be aborted.
6953 	 * one task (this task) will still be running which is OK.
6954 	 */
6955 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
6956 	    (ntasks_pending == 1))) {
6957 		stmf_status_t ret;
6958 
6959 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
6960 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
6961 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
6962 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
6963 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
6964 		} else {
6965 			ret = STMF_SUCCESS;
6966 		}
6967 		if (ret == STMF_SUCCESS) {
6968 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6969 		}
6970 		if (target_reset) {
6971 			return (ret);
6972 		}
6973 		if (ret == STMF_SUCCESS) {
6974 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6975 			return (ret);
6976 		}
6977 		if (ret != STMF_BUSY) {
6978 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
6979 			return (ret);
6980 		}
6981 	}
6982 
6983 	if (target_reset) {
6984 		/* Tell target reset polling code that we are not done */
6985 		return (STMF_BUSY);
6986 	}
6987 
6988 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6989 	    != STMF_SUCCESS) {
6990 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6991 		    STMF_ALLOC_FAILURE, NULL);
6992 		return (STMF_SUCCESS);
6993 	}
6994 
6995 	return (STMF_SUCCESS);
6996 }
6997 
6998 void
6999 stmf_target_reset_poll(struct scsi_task *task)
7000 {
7001 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7002 	    task->task_session->ss_stmf_private;
7003 	stmf_lun_map_t *lm;
7004 	stmf_lun_map_ent_t *lm_ent;
7005 	stmf_i_lu_t *ilu;
7006 	stmf_status_t ret;
7007 	int i;
7008 	int not_done = 0;
7009 
7010 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
7011 
7012 	rw_enter(iss->iss_lockp, RW_READER);
7013 	lm = iss->iss_sm;
7014 	for (i = 0; i < lm->lm_nentries; i++) {
7015 		if (lm->lm_plus[i] == NULL)
7016 			continue;
7017 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7018 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7019 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7020 			rw_exit(iss->iss_lockp);
7021 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
7022 			rw_enter(iss->iss_lockp, RW_READER);
7023 			if (ret == STMF_SUCCESS)
7024 				continue;
7025 			not_done = 1;
7026 			if (ret != STMF_BUSY) {
7027 				rw_exit(iss->iss_lockp);
7028 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7029 				    STMF_ABORTED, NULL);
7030 				return;
7031 			}
7032 		}
7033 	}
7034 	rw_exit(iss->iss_lockp);
7035 
7036 	if (not_done) {
7037 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7038 		    != STMF_SUCCESS) {
7039 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7040 			    STMF_ALLOC_FAILURE, NULL);
7041 			return;
7042 		}
7043 		return;
7044 	}
7045 
7046 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7047 
7048 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7049 }
7050 
7051 stmf_status_t
7052 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
7053 {
7054 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7055 
7056 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7057 		return (STMF_INVALID_ARG);
7058 	}
7059 
7060 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
7061 	return (STMF_SUCCESS);
7062 }
7063 
7064 stmf_status_t
7065 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
7066 {
7067 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7068 
7069 	if (eventid == STMF_EVENT_ALL) {
7070 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
7071 		return (STMF_SUCCESS);
7072 	}
7073 
7074 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7075 		return (STMF_INVALID_ARG);
7076 	}
7077 
7078 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
7079 	return (STMF_SUCCESS);
7080 }
7081 
7082 stmf_status_t
7083 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
7084 {
7085 	stmf_i_local_port_t *ilport =
7086 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7087 
7088 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7089 		return (STMF_INVALID_ARG);
7090 	}
7091 
7092 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
7093 	return (STMF_SUCCESS);
7094 }
7095 
7096 stmf_status_t
7097 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
7098 {
7099 	stmf_i_local_port_t *ilport =
7100 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
7101 
7102 	if (eventid == STMF_EVENT_ALL) {
7103 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
7104 		return (STMF_SUCCESS);
7105 	}
7106 
7107 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7108 		return (STMF_INVALID_ARG);
7109 	}
7110 
7111 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
7112 	return (STMF_SUCCESS);
7113 }
7114 
7115 void
7116 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
7117 {
7118 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
7119 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
7120 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
7121 	}
7122 }
7123 
7124 void
7125 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
7126     uint32_t flags)
7127 {
7128 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
7129 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
7130 		ilport->ilport_lport->lport_event_handler(
7131 		    ilport->ilport_lport, eventid, arg, flags);
7132 	}
7133 }
7134 
7135 /*
7136  * With the possibility of having multiple itl sessions pointing to the
7137  * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize
7138  * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer
7139  * statistics.
7140  */
7141 void
7142 stmf_itl_task_start(stmf_i_scsi_task_t *itask)
7143 {
7144 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7145 	scsi_task_t	*task = itask->itask_task;
7146 	stmf_i_lu_t	*ilu;
7147 
7148 	if (itl == NULL || task->task_lu == dlun0)
7149 		return;
7150 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7151 	itask->itask_start_timestamp = gethrtime();
7152 	if (ilu->ilu_kstat_io != NULL) {
7153 		mutex_enter(ilu->ilu_kstat_io->ks_lock);
7154 		stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter);
7155 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7156 	}
7157 
7158 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter);
7159 }
7160 
7161 void
7162 stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask)
7163 {
7164 	stmf_itl_data_t	*itl = itask->itask_itl_datap;
7165 	scsi_task_t	*task = itask->itask_task;
7166 	stmf_i_lu_t	*ilu;
7167 
7168 	if (itl == NULL || task->task_lu == dlun0)
7169 		return;
7170 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7171 	if (ilu->ilu_kstat_io != NULL) {
7172 		mutex_enter(ilu->ilu_kstat_io->ks_lock);
7173 		stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq);
7174 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7175 	}
7176 
7177 	stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq);
7178 }
7179 
7180 void
7181 stmf_itl_task_done(stmf_i_scsi_task_t *itask)
7182 {
7183 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7184 	scsi_task_t		*task = itask->itask_task;
7185 	stmf_i_lu_t	*ilu;
7186 
7187 	itask->itask_done_timestamp = gethrtime();
7188 
7189 	if (itl == NULL || task->task_lu == dlun0)
7190 		return;
7191 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7192 
7193 	if (ilu->ilu_kstat_io == NULL)
7194 		return;
7195 
7196 	mutex_enter(ilu->ilu_kstat_io->ks_lock);
7197 
7198 	if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) {
7199 		stmf_update_kstat_lu_q(task, kstat_runq_exit);
7200 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7201 		stmf_update_kstat_lport_q(task, kstat_runq_exit);
7202 	} else {
7203 		stmf_update_kstat_lu_q(task, kstat_waitq_exit);
7204 		mutex_exit(ilu->ilu_kstat_io->ks_lock);
7205 		stmf_update_kstat_lport_q(task, kstat_waitq_exit);
7206 	}
7207 }
7208 
7209 static void
7210 stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7211 {
7212 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7213 
7214 	if (itl == NULL)
7215 		return;
7216 
7217 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task,
7218 	    stmf_data_buf_t *, dbuf);
7219 
7220 	dbuf->db_xfer_start_timestamp = gethrtime();
7221 }
7222 
7223 static void
7224 stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7225 {
7226 	stmf_itl_data_t		*itl = itask->itask_itl_datap;
7227 	hrtime_t		elapsed_time;
7228 	uint64_t		xfer_size;
7229 
7230 	if (itl == NULL)
7231 		return;
7232 
7233 	xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ?
7234 	    dbuf->db_data_size : 0;
7235 
7236 	elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp;
7237 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7238 		atomic_add_64((uint64_t *)&itask->itask_lport_read_time,
7239 		    elapsed_time);
7240 		atomic_add_64((uint64_t *)&itask->itask_read_xfer,
7241 		    xfer_size);
7242 	} else {
7243 		atomic_add_64((uint64_t *)&itask->itask_lport_write_time,
7244 		    elapsed_time);
7245 		atomic_add_64((uint64_t *)&itask->itask_write_xfer,
7246 		    xfer_size);
7247 	}
7248 
7249 	DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task,
7250 	    stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time);
7251 
7252 	dbuf->db_xfer_start_timestamp = 0;
7253 }
7254 
7255 void
7256 stmf_svc_init()
7257 {
7258 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7259 		return;
7260 	stmf_state.stmf_svc_tailp = &stmf_state.stmf_svc_active;
7261 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
7262 	    TASKQ_DEFAULTPRI, 0);
7263 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
7264 	    stmf_svc, 0, DDI_SLEEP);
7265 }
7266 
7267 stmf_status_t
7268 stmf_svc_fini()
7269 {
7270 	uint32_t i;
7271 
7272 	mutex_enter(&stmf_state.stmf_lock);
7273 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
7274 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
7275 		cv_signal(&stmf_state.stmf_cv);
7276 	}
7277 	mutex_exit(&stmf_state.stmf_lock);
7278 
7279 	/* Wait for 5 seconds */
7280 	for (i = 0; i < 500; i++) {
7281 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7282 			delay(drv_usectohz(10000));
7283 		else
7284 			break;
7285 	}
7286 	if (i == 500)
7287 		return (STMF_BUSY);
7288 
7289 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
7290 
7291 	return (STMF_SUCCESS);
7292 }
7293 
7294 struct stmf_svc_clocks {
7295 	clock_t drain_start, drain_next;
7296 	clock_t timing_start, timing_next;
7297 	clock_t worker_delay;
7298 };
7299 
7300 /* ARGSUSED */
7301 void
7302 stmf_svc(void *arg)
7303 {
7304 	stmf_svc_req_t *req;
7305 	stmf_lu_t *lu;
7306 	stmf_i_lu_t *ilu;
7307 	stmf_local_port_t *lport;
7308 	struct stmf_svc_clocks clks = { 0 };
7309 
7310 	mutex_enter(&stmf_state.stmf_lock);
7311 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
7312 
7313 	while (!(stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE)) {
7314 		if (stmf_state.stmf_svc_active == NULL) {
7315 			stmf_svc_timeout(&clks);
7316 			continue;
7317 		}
7318 
7319 		/*
7320 		 * Pop the front request from the active list.  After this,
7321 		 * the request will no longer be referenced by global state,
7322 		 * so it should be safe to access it without holding the
7323 		 * stmf state lock.
7324 		 */
7325 		req = stmf_state.stmf_svc_active;
7326 		stmf_state.stmf_svc_active = req->svc_next;
7327 
7328 		if (stmf_state.stmf_svc_active == NULL)
7329 			stmf_state.stmf_svc_tailp = &stmf_state.stmf_svc_active;
7330 
7331 		switch (req->svc_cmd) {
7332 		case STMF_CMD_LPORT_ONLINE:
7333 			/* Fallthrough */
7334 		case STMF_CMD_LPORT_OFFLINE:
7335 			mutex_exit(&stmf_state.stmf_lock);
7336 			lport = (stmf_local_port_t *)req->svc_obj;
7337 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
7338 			break;
7339 		case STMF_CMD_LU_ONLINE:
7340 			mutex_exit(&stmf_state.stmf_lock);
7341 			lu = (stmf_lu_t *)req->svc_obj;
7342 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7343 			break;
7344 		case STMF_CMD_LU_OFFLINE:
7345 			/* Remove all mappings of this LU */
7346 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
7347 			/* Kill all the pending I/Os for this LU */
7348 			mutex_exit(&stmf_state.stmf_lock);
7349 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
7350 			    STMF_ABORTED);
7351 			lu = (stmf_lu_t *)req->svc_obj;
7352 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7353 			stmf_wait_ilu_tasks_finish(ilu);
7354 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7355 			break;
7356 		default:
7357 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
7358 			    req->svc_cmd);
7359 		}
7360 
7361 		kmem_free(req, req->svc_req_alloc_size);
7362 		mutex_enter(&stmf_state.stmf_lock);
7363 	}
7364 
7365 	stmf_state.stmf_svc_flags &= ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
7366 	mutex_exit(&stmf_state.stmf_lock);
7367 }
7368 
7369 static void
7370 stmf_svc_timeout(struct stmf_svc_clocks *clks)
7371 {
7372 	clock_t td;
7373 	stmf_i_local_port_t *ilport, *next_ilport;
7374 	stmf_i_scsi_session_t *iss;
7375 
7376 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
7377 
7378 	td = drv_usectohz(20000);
7379 
7380 	/* Do timeouts */
7381 	if (stmf_state.stmf_nlus &&
7382 	    ((!clks->timing_next) || (ddi_get_lbolt() >= clks->timing_next))) {
7383 		if (!stmf_state.stmf_svc_ilu_timing) {
7384 			/* we are starting a new round */
7385 			stmf_state.stmf_svc_ilu_timing =
7386 			    stmf_state.stmf_ilulist;
7387 			clks->timing_start = ddi_get_lbolt();
7388 		}
7389 
7390 		stmf_check_ilu_timing();
7391 		if (!stmf_state.stmf_svc_ilu_timing) {
7392 			/* we finished a complete round */
7393 			clks->timing_next =
7394 			    clks->timing_start + drv_usectohz(5*1000*1000);
7395 		} else {
7396 			/* we still have some ilu items to check */
7397 			clks->timing_next =
7398 			    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7399 		}
7400 
7401 		if (stmf_state.stmf_svc_active)
7402 			return;
7403 	}
7404 
7405 	/* Check if there are free tasks to clear */
7406 	if (stmf_state.stmf_nlus &&
7407 	    ((!clks->drain_next) || (ddi_get_lbolt() >= clks->drain_next))) {
7408 		if (!stmf_state.stmf_svc_ilu_draining) {
7409 			/* we are starting a new round */
7410 			stmf_state.stmf_svc_ilu_draining =
7411 			    stmf_state.stmf_ilulist;
7412 			clks->drain_start = ddi_get_lbolt();
7413 		}
7414 
7415 		stmf_check_freetask();
7416 		if (!stmf_state.stmf_svc_ilu_draining) {
7417 			/* we finished a complete round */
7418 			clks->drain_next =
7419 			    clks->drain_start + drv_usectohz(10*1000*1000);
7420 		} else {
7421 			/* we still have some ilu items to check */
7422 			clks->drain_next =
7423 			    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7424 		}
7425 
7426 		if (stmf_state.stmf_svc_active)
7427 			return;
7428 	}
7429 
7430 	/* Check if we need to run worker_mgmt */
7431 	if (ddi_get_lbolt() > clks->worker_delay) {
7432 		stmf_worker_mgmt();
7433 		clks->worker_delay = ddi_get_lbolt() +
7434 		    stmf_worker_mgmt_delay;
7435 	}
7436 
7437 	/* Check if any active session got its 1st LUN */
7438 	if (stmf_state.stmf_process_initial_luns) {
7439 		int stmf_level = 0;
7440 		int port_level;
7441 
7442 		for (ilport = stmf_state.stmf_ilportlist; ilport;
7443 		    ilport = next_ilport) {
7444 			int ilport_lock_held;
7445 			next_ilport = ilport->ilport_next;
7446 
7447 			if ((ilport->ilport_flags &
7448 			    ILPORT_SS_GOT_INITIAL_LUNS) == 0)
7449 				continue;
7450 
7451 			port_level = 0;
7452 			rw_enter(&ilport->ilport_lock, RW_READER);
7453 			ilport_lock_held = 1;
7454 
7455 			for (iss = ilport->ilport_ss_list; iss;
7456 			    iss = iss->iss_next) {
7457 				if ((iss->iss_flags &
7458 				    ISS_GOT_INITIAL_LUNS) == 0)
7459 					continue;
7460 
7461 				port_level++;
7462 				stmf_level++;
7463 				atomic_and_32(&iss->iss_flags,
7464 				    ~ISS_GOT_INITIAL_LUNS);
7465 				atomic_or_32(&iss->iss_flags,
7466 				    ISS_EVENT_ACTIVE);
7467 				rw_exit(&ilport->ilport_lock);
7468 				ilport_lock_held = 0;
7469 				mutex_exit(&stmf_state.stmf_lock);
7470 				stmf_generate_lport_event(ilport,
7471 				    LPORT_EVENT_INITIAL_LUN_MAPPED,
7472 				    iss->iss_ss, 0);
7473 				atomic_and_32(&iss->iss_flags,
7474 				    ~ISS_EVENT_ACTIVE);
7475 				mutex_enter(&stmf_state.stmf_lock);
7476 				/*
7477 				 * scan all the ilports again as the
7478 				 * ilport list might have changed.
7479 				 */
7480 				next_ilport = stmf_state.stmf_ilportlist;
7481 				break;
7482 			}
7483 
7484 			if (port_level == 0)
7485 				atomic_and_32(&ilport->ilport_flags,
7486 				    ~ILPORT_SS_GOT_INITIAL_LUNS);
7487 			/* drop the lock if we are holding it. */
7488 			if (ilport_lock_held == 1)
7489 				rw_exit(&ilport->ilport_lock);
7490 
7491 			/* Max 4 session at a time */
7492 			if (stmf_level >= 4)
7493 				break;
7494 		}
7495 
7496 		if (stmf_level == 0)
7497 			stmf_state.stmf_process_initial_luns = 0;
7498 	}
7499 
7500 	stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
7501 	(void) cv_reltimedwait(&stmf_state.stmf_cv,
7502 	    &stmf_state.stmf_lock, td, TR_CLOCK_TICK);
7503 	stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
7504 }
7505 
7506 /*
7507  * Waits for ongoing I/O tasks to finish on an LU in preparation for
7508  * the LU's offlining. The LU should already be in an Offlining state
7509  * (otherwise I/O to the LU might never end). There is an additional
7510  * enforcement of this via a deadman timer check.
7511  */
7512 static void
7513 stmf_wait_ilu_tasks_finish(stmf_i_lu_t *ilu)
7514 {
7515 	clock_t start, now, deadline;
7516 
7517 	start = now = ddi_get_lbolt();
7518 	deadline = start + drv_usectohz(stmf_io_deadman * 1000000llu);
7519 	mutex_enter(&ilu->ilu_task_lock);
7520 	while (ilu->ilu_ntasks != ilu->ilu_ntasks_free) {
7521 		(void) cv_timedwait(&ilu->ilu_offline_pending_cv,
7522 		    &ilu->ilu_task_lock, deadline);
7523 		now = ddi_get_lbolt();
7524 		if (now > deadline) {
7525 			if (stmf_io_deadman_enabled) {
7526 				cmn_err(CE_PANIC, "stmf_svc: I/O deadman hit "
7527 				    "on STMF_CMD_LU_OFFLINE after %d seconds",
7528 				    stmf_io_deadman);
7529 			} else {
7530 				/* keep on spinning */
7531 				deadline = now + drv_usectohz(stmf_io_deadman *
7532 				    1000000llu);
7533 			}
7534 		}
7535 	}
7536 	mutex_exit(&ilu->ilu_task_lock);
7537 	DTRACE_PROBE1(deadman__timeout__wait, clock_t, now - start);
7538 }
7539 
7540 void
7541 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
7542 {
7543 	stmf_svc_req_t *req;
7544 	int s;
7545 
7546 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
7547 	s = sizeof (stmf_svc_req_t);
7548 	if (info->st_additional_info) {
7549 		s += strlen(info->st_additional_info) + 1;
7550 	}
7551 	req = kmem_zalloc(s, KM_SLEEP);
7552 
7553 	req->svc_cmd = cmd;
7554 	req->svc_obj = obj;
7555 	req->svc_info.st_rflags = info->st_rflags;
7556 	if (info->st_additional_info) {
7557 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
7558 		    sizeof (stmf_svc_req_t)));
7559 		(void) strcpy(req->svc_info.st_additional_info,
7560 		    info->st_additional_info);
7561 	}
7562 	req->svc_req_alloc_size = s;
7563 	req->svc_next = NULL;
7564 
7565 	mutex_enter(&stmf_state.stmf_lock);
7566 	*stmf_state.stmf_svc_tailp = req;
7567 	stmf_state.stmf_svc_tailp = &req->svc_next;
7568 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
7569 		cv_signal(&stmf_state.stmf_cv);
7570 	}
7571 	mutex_exit(&stmf_state.stmf_lock);
7572 }
7573 
7574 static void
7575 stmf_svc_kill_obj_requests(void *obj)
7576 {
7577 	stmf_svc_req_t *prev_req = NULL;
7578 	stmf_svc_req_t *next_req;
7579 	stmf_svc_req_t *req;
7580 
7581 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
7582 
7583 	for (req = stmf_state.stmf_svc_active; req != NULL; req = next_req) {
7584 		next_req = req->svc_next;
7585 
7586 		if (req->svc_obj == obj) {
7587 			if (prev_req != NULL)
7588 				prev_req->svc_next = next_req;
7589 			else
7590 				stmf_state.stmf_svc_active = next_req;
7591 
7592 			if (next_req == NULL)
7593 				stmf_state.stmf_svc_tailp = (prev_req != NULL) ?
7594 				    &prev_req->svc_next :
7595 				    &stmf_state.stmf_svc_active;
7596 
7597 			kmem_free(req, req->svc_req_alloc_size);
7598 		} else {
7599 			prev_req = req;
7600 		}
7601 	}
7602 }
7603 
7604 void
7605 stmf_trace(caddr_t ident, const char *fmt, ...)
7606 {
7607 	va_list args;
7608 	char tbuf[160];
7609 	int len;
7610 
7611 	if (!stmf_trace_on)
7612 		return;
7613 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
7614 	    ddi_get_lbolt());
7615 	va_start(args, fmt);
7616 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
7617 	va_end(args);
7618 
7619 	if (len > 158) {
7620 		len = 158;
7621 	}
7622 	tbuf[len++] = '\n';
7623 	tbuf[len] = '\0';
7624 
7625 	mutex_enter(&trace_buf_lock);
7626 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
7627 	trace_buf_curndx += len;
7628 	if (trace_buf_curndx > (trace_buf_size - 320))
7629 		trace_buf_curndx = 0;
7630 	mutex_exit(&trace_buf_lock);
7631 }
7632 
7633 void
7634 stmf_trace_clear()
7635 {
7636 	if (!stmf_trace_on)
7637 		return;
7638 	mutex_enter(&trace_buf_lock);
7639 	trace_buf_curndx = 0;
7640 	if (trace_buf_size > 0)
7641 		stmf_trace_buf[0] = '\0';
7642 	mutex_exit(&trace_buf_lock);
7643 }
7644 
7645 static void
7646 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
7647 {
7648 	stmf_state_change_info_t	change_info;
7649 	void				*ctl_private;
7650 	uint32_t			ctl_cmd;
7651 	int				msg = 0;
7652 
7653 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
7654 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
7655 	change_info.st_additional_info = info;
7656 	if (offline_lu) {
7657 		change_info.st_rflags = STMF_RFLAG_RESET |
7658 		    STMF_RFLAG_LU_ABORT;
7659 		ctl_private = task->task_lu;
7660 		if (((stmf_i_lu_t *)
7661 		    task->task_lu->lu_stmf_private)->ilu_state ==
7662 		    STMF_STATE_ONLINE) {
7663 			msg = 1;
7664 		}
7665 		ctl_cmd = STMF_CMD_LU_OFFLINE;
7666 	} else {
7667 		change_info.st_rflags = STMF_RFLAG_RESET |
7668 		    STMF_RFLAG_LPORT_ABORT;
7669 		ctl_private = task->task_lport;
7670 		if (((stmf_i_local_port_t *)
7671 		    task->task_lport->lport_stmf_private)->ilport_state ==
7672 		    STMF_STATE_ONLINE) {
7673 			msg = 1;
7674 		}
7675 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
7676 	}
7677 
7678 	if (msg) {
7679 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
7680 		    offline_lu ? "LU" : "LPORT", info ? info :
7681 		    "<no additional info>");
7682 	}
7683 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
7684 }
7685 
7686 static char
7687 stmf_ctoi(char c)
7688 {
7689 	if ((c >= '0') && (c <= '9'))
7690 		c -= '0';
7691 	else if ((c >= 'A') && (c <= 'F'))
7692 		c = c - 'A' + 10;
7693 	else if ((c >= 'a') && (c <= 'f'))
7694 		c = c - 'a' + 10;
7695 	else
7696 		c = -1;
7697 	return (c);
7698 }
7699 
7700 /* Convert from Hex value in ASCII format to the equivalent bytes */
7701 static boolean_t
7702 stmf_base16_str_to_binary(char *c, int dplen, uint8_t *dp)
7703 {
7704 	int		ii;
7705 
7706 	for (ii = 0; ii < dplen; ii++) {
7707 		char nibble1, nibble2;
7708 		char enc_char = *c++;
7709 		nibble1 = stmf_ctoi(enc_char);
7710 
7711 		enc_char = *c++;
7712 		nibble2 = stmf_ctoi(enc_char);
7713 		if (nibble1 == -1 || nibble2 == -1)
7714 			return (B_FALSE);
7715 
7716 		dp[ii] = (nibble1 << 4) | nibble2;
7717 	}
7718 	return (B_TRUE);
7719 }
7720 
7721 boolean_t
7722 stmf_scsilib_tptid_validate(scsi_transport_id_t *tptid, uint32_t total_sz,
7723     uint16_t *tptid_sz)
7724 {
7725 	uint16_t tpd_len = SCSI_TPTID_SIZE;
7726 
7727 	if (tptid_sz)
7728 		*tptid_sz = 0;
7729 	if (total_sz < sizeof (scsi_transport_id_t))
7730 		return (B_FALSE);
7731 
7732 	switch (tptid->protocol_id) {
7733 
7734 	case PROTOCOL_FIBRE_CHANNEL:
7735 		/* FC Transport ID validation checks. SPC3 rev23, Table 284 */
7736 		if (total_sz < tpd_len || tptid->format_code != 0)
7737 			return (B_FALSE);
7738 		break;
7739 
7740 	case PROTOCOL_iSCSI:
7741 		{
7742 		iscsi_transport_id_t	*iscsiid;
7743 		uint16_t		adn_len, name_len;
7744 
7745 		/* Check for valid format code, SPC3 rev 23 Table 288 */
7746 		if ((total_sz < tpd_len) ||
7747 		    (tptid->format_code != 0 && tptid->format_code != 1))
7748 			return (B_FALSE);
7749 
7750 		iscsiid = (iscsi_transport_id_t *)tptid;
7751 		adn_len = READ_SCSI16(iscsiid->add_len, uint16_t);
7752 		tpd_len = sizeof (iscsi_transport_id_t) + adn_len - 1;
7753 
7754 		/*
7755 		 * iSCSI Transport ID validation checks.
7756 		 * As per SPC3 rev 23 Section 7.5.4.6 and Table 289 & Table 290
7757 		 */
7758 		if (adn_len < 20 || (adn_len % 4 != 0))
7759 			return (B_FALSE);
7760 
7761 		name_len = strnlen(iscsiid->iscsi_name, adn_len);
7762 		if (name_len == 0 || name_len >= adn_len)
7763 			return (B_FALSE);
7764 
7765 		/* If the format_code is 1 check for ISID seperator */
7766 		if ((tptid->format_code == 1) && (strstr(iscsiid->iscsi_name,
7767 		    SCSI_TPTID_ISCSI_ISID_SEPERATOR) == NULL))
7768 			return (B_FALSE);
7769 
7770 		}
7771 		break;
7772 
7773 	case PROTOCOL_SRP:
7774 		/* SRP Transport ID validation checks. SPC3 rev23, Table 287 */
7775 		if (total_sz < tpd_len || tptid->format_code != 0)
7776 			return (B_FALSE);
7777 		break;
7778 
7779 	case PROTOCOL_PARALLEL_SCSI:
7780 	case PROTOCOL_SSA:
7781 	case PROTOCOL_IEEE_1394:
7782 	case PROTOCOL_SAS:
7783 	case PROTOCOL_ADT:
7784 	case PROTOCOL_ATAPI:
7785 	default:
7786 		{
7787 		stmf_dflt_scsi_tptid_t *dflttpd;
7788 
7789 		tpd_len = sizeof (stmf_dflt_scsi_tptid_t);
7790 		if (total_sz < tpd_len)
7791 			return (B_FALSE);
7792 		dflttpd = (stmf_dflt_scsi_tptid_t *)tptid;
7793 		tpd_len = tpd_len + SCSI_READ16(&dflttpd->ident_len) - 1;
7794 		if (total_sz < tpd_len)
7795 			return (B_FALSE);
7796 		}
7797 		break;
7798 	}
7799 	if (tptid_sz)
7800 		*tptid_sz = tpd_len;
7801 	return (B_TRUE);
7802 }
7803 
7804 boolean_t
7805 stmf_scsilib_tptid_compare(scsi_transport_id_t *tpd1, scsi_transport_id_t *tpd2)
7806 {
7807 	if ((tpd1->protocol_id != tpd2->protocol_id) ||
7808 	    (tpd1->format_code != tpd2->format_code))
7809 		return (B_FALSE);
7810 
7811 	switch (tpd1->protocol_id) {
7812 
7813 	case PROTOCOL_iSCSI:
7814 		{
7815 		iscsi_transport_id_t *iscsitpd1, *iscsitpd2;
7816 		uint16_t len;
7817 
7818 		iscsitpd1 = (iscsi_transport_id_t *)tpd1;
7819 		iscsitpd2 = (iscsi_transport_id_t *)tpd2;
7820 		len = SCSI_READ16(&iscsitpd1->add_len);
7821 		if ((memcmp(iscsitpd1->add_len, iscsitpd2->add_len, 2) != 0) ||
7822 		    (memcmp(iscsitpd1->iscsi_name, iscsitpd2->iscsi_name, len)
7823 		    != 0))
7824 			return (B_FALSE);
7825 		}
7826 		break;
7827 
7828 	case PROTOCOL_SRP:
7829 		{
7830 		scsi_srp_transport_id_t *srptpd1, *srptpd2;
7831 
7832 		srptpd1 = (scsi_srp_transport_id_t *)tpd1;
7833 		srptpd2 = (scsi_srp_transport_id_t *)tpd2;
7834 		if (memcmp(srptpd1->srp_name, srptpd2->srp_name,
7835 		    sizeof (srptpd1->srp_name)) != 0)
7836 			return (B_FALSE);
7837 		}
7838 		break;
7839 
7840 	case PROTOCOL_FIBRE_CHANNEL:
7841 		{
7842 		scsi_fc_transport_id_t *fctpd1, *fctpd2;
7843 
7844 		fctpd1 = (scsi_fc_transport_id_t *)tpd1;
7845 		fctpd2 = (scsi_fc_transport_id_t *)tpd2;
7846 		if (memcmp(fctpd1->port_name, fctpd2->port_name,
7847 		    sizeof (fctpd1->port_name)) != 0)
7848 			return (B_FALSE);
7849 		}
7850 		break;
7851 
7852 	case PROTOCOL_PARALLEL_SCSI:
7853 	case PROTOCOL_SSA:
7854 	case PROTOCOL_IEEE_1394:
7855 	case PROTOCOL_SAS:
7856 	case PROTOCOL_ADT:
7857 	case PROTOCOL_ATAPI:
7858 	default:
7859 		{
7860 		stmf_dflt_scsi_tptid_t *dflt1, *dflt2;
7861 		uint16_t len;
7862 
7863 		dflt1 = (stmf_dflt_scsi_tptid_t *)tpd1;
7864 		dflt2 = (stmf_dflt_scsi_tptid_t *)tpd2;
7865 		len = SCSI_READ16(&dflt1->ident_len);
7866 		if ((memcmp(dflt1->ident_len, dflt2->ident_len, 2) != 0) ||
7867 		    (memcmp(dflt1->ident, dflt2->ident, len) != 0))
7868 			return (B_FALSE);
7869 		}
7870 		break;
7871 	}
7872 	return (B_TRUE);
7873 }
7874 
7875 /*
7876  * Changes devid_desc to corresponding TransportID format
7877  * Returns :- pointer to stmf_remote_port_t
7878  * Note    :- Allocates continous memory for stmf_remote_port_t and TransportID,
7879  *            This memory need to be freed when this remote_port is no longer
7880  *            used.
7881  */
7882 stmf_remote_port_t *
7883 stmf_scsilib_devid_to_remote_port(scsi_devid_desc_t *devid)
7884 {
7885 	struct scsi_fc_transport_id	*fc_tpd;
7886 	struct iscsi_transport_id	*iscsi_tpd;
7887 	struct scsi_srp_transport_id	*srp_tpd;
7888 	struct stmf_dflt_scsi_tptid	*dflt_tpd;
7889 	uint16_t ident_len,  sz = 0;
7890 	stmf_remote_port_t *rpt = NULL;
7891 
7892 	ident_len = devid->ident_length;
7893 	ASSERT(ident_len);
7894 	switch (devid->protocol_id) {
7895 	case PROTOCOL_FIBRE_CHANNEL:
7896 		sz = sizeof (scsi_fc_transport_id_t);
7897 		rpt = stmf_remote_port_alloc(sz);
7898 		rpt->rport_tptid->format_code = 0;
7899 		rpt->rport_tptid->protocol_id = devid->protocol_id;
7900 		fc_tpd = (scsi_fc_transport_id_t *)rpt->rport_tptid;
7901 		/*
7902 		 * convert from "wwn.xxxxxxxxxxxxxxxx" to 8-byte binary
7903 		 * skip first 4 byte for "wwn."
7904 		 */
7905 		ASSERT(strncmp("wwn.", (char *)devid->ident, 4) == 0);
7906 		if ((ident_len < SCSI_TPTID_FC_PORT_NAME_SIZE * 2 + 4) ||
7907 		    !stmf_base16_str_to_binary((char *)devid->ident + 4,
7908 		    SCSI_TPTID_FC_PORT_NAME_SIZE, fc_tpd->port_name))
7909 			goto devid_to_remote_port_fail;
7910 		break;
7911 
7912 	case PROTOCOL_iSCSI:
7913 		sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (iscsi_transport_id_t) +
7914 		    ident_len - 1);
7915 		rpt = stmf_remote_port_alloc(sz);
7916 		rpt->rport_tptid->format_code = 0;
7917 		rpt->rport_tptid->protocol_id = devid->protocol_id;
7918 		iscsi_tpd = (iscsi_transport_id_t *)rpt->rport_tptid;
7919 		SCSI_WRITE16(iscsi_tpd->add_len, ident_len);
7920 		(void) memcpy(iscsi_tpd->iscsi_name, devid->ident, ident_len);
7921 		break;
7922 
7923 	case PROTOCOL_SRP:
7924 		sz = sizeof (scsi_srp_transport_id_t);
7925 		rpt = stmf_remote_port_alloc(sz);
7926 		rpt->rport_tptid->format_code = 0;
7927 		rpt->rport_tptid->protocol_id = devid->protocol_id;
7928 		srp_tpd = (scsi_srp_transport_id_t *)rpt->rport_tptid;
7929 		/*
7930 		 * convert from "eui.xxxxxxxxxxxxxxx" to 8-byte binary
7931 		 * skip first 4 byte for "eui."
7932 		 * Assume 8-byte initiator-extension part of srp_name is NOT
7933 		 * stored in devid and hence will be set as zero
7934 		 */
7935 		ASSERT(strncmp("eui.", (char *)devid->ident, 4) == 0);
7936 		if ((ident_len < (SCSI_TPTID_SRP_PORT_NAME_LEN - 8) * 2 + 4) ||
7937 		    !stmf_base16_str_to_binary((char *)devid->ident+4,
7938 		    SCSI_TPTID_SRP_PORT_NAME_LEN, srp_tpd->srp_name))
7939 			goto devid_to_remote_port_fail;
7940 		break;
7941 
7942 	case PROTOCOL_PARALLEL_SCSI:
7943 	case PROTOCOL_SSA:
7944 	case PROTOCOL_IEEE_1394:
7945 	case PROTOCOL_SAS:
7946 	case PROTOCOL_ADT:
7947 	case PROTOCOL_ATAPI:
7948 	default :
7949 		ident_len = devid->ident_length;
7950 		sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (stmf_dflt_scsi_tptid_t) +
7951 		    ident_len - 1);
7952 		rpt = stmf_remote_port_alloc(sz);
7953 		rpt->rport_tptid->format_code = 0;
7954 		rpt->rport_tptid->protocol_id = devid->protocol_id;
7955 		dflt_tpd = (stmf_dflt_scsi_tptid_t *)rpt->rport_tptid;
7956 		SCSI_WRITE16(dflt_tpd->ident_len, ident_len);
7957 		(void) memcpy(dflt_tpd->ident, devid->ident, ident_len);
7958 		break;
7959 	}
7960 	return (rpt);
7961 
7962 devid_to_remote_port_fail:
7963 	stmf_remote_port_free(rpt);
7964 	return (NULL);
7965 
7966 }
7967 
7968 stmf_remote_port_t *
7969 stmf_remote_port_alloc(uint16_t tptid_sz)
7970 {
7971 	stmf_remote_port_t *rpt;
7972 	rpt = (stmf_remote_port_t *)kmem_zalloc(
7973 	    sizeof (stmf_remote_port_t) + tptid_sz, KM_SLEEP);
7974 	rpt->rport_tptid_sz = tptid_sz;
7975 	rpt->rport_tptid = (scsi_transport_id_t *)(rpt + 1);
7976 	return (rpt);
7977 }
7978 
7979 void
7980 stmf_remote_port_free(stmf_remote_port_t *rpt)
7981 {
7982 	/*
7983 	 * Note: stmf_scsilib_devid_to_remote_port() function allocates
7984 	 *	remote port structures for all transports in the same way, So
7985 	 *	it is safe to deallocate it in a protocol independent manner.
7986 	 *	If any of the allocation method changes, corresponding changes
7987 	 *	need to be made here too.
7988 	 */
7989 	kmem_free(rpt, sizeof (stmf_remote_port_t) + rpt->rport_tptid_sz);
7990 }
7991