xref: /illumos-gate/usr/src/uts/common/io/comstar/stmf/stmf.c (revision bdb9230ac765cb7af3fc1f4119caf2c5720dceb3)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/conf.h>
27 #include <sys/file.h>
28 #include <sys/ddi.h>
29 #include <sys/sunddi.h>
30 #include <sys/modctl.h>
31 #include <sys/scsi/scsi.h>
32 #include <sys/scsi/impl/scsi_reset_notify.h>
33 #include <sys/disp.h>
34 #include <sys/byteorder.h>
35 #include <sys/atomic.h>
36 #include <sys/ethernet.h>
37 #include <sys/sdt.h>
38 #include <sys/nvpair.h>
39 #include <sys/zone.h>
40 
41 #include <stmf.h>
42 #include <lpif.h>
43 #include <portif.h>
44 #include <stmf_ioctl.h>
45 #include <stmf_impl.h>
46 #include <lun_map.h>
47 #include <stmf_state.h>
48 
49 static uint64_t stmf_session_counter = 0;
50 static uint16_t stmf_rtpid_counter = 0;
51 
52 static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
53 static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
54 static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
55 	void **result);
56 static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp);
57 static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp);
58 static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
59 	cred_t *credp, int *rval);
60 static int stmf_get_stmf_state(stmf_state_desc_t *std);
61 static int stmf_set_stmf_state(stmf_state_desc_t *std);
62 static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu,
63     char *info);
64 void stmf_svc_init();
65 stmf_status_t stmf_svc_fini();
66 void stmf_svc(void *arg);
67 void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info);
68 void stmf_check_freetask();
69 void stmf_abort_target_reset(scsi_task_t *task);
70 stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task,
71 							int target_reset);
72 void stmf_target_reset_poll(struct scsi_task *task);
73 void stmf_handle_lun_reset(scsi_task_t *task);
74 void stmf_handle_target_reset(scsi_task_t *task);
75 void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf);
76 int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
77     uint32_t *err_ret);
78 int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi);
79 int stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
80     uint32_t *err_ret);
81 void stmf_delete_ppd(stmf_pp_data_t *ppd);
82 void stmf_delete_all_ppds();
83 void stmf_trace_clear();
84 void stmf_worker_init();
85 stmf_status_t stmf_worker_fini();
86 void stmf_worker_mgmt();
87 void stmf_worker_task(void *arg);
88 
89 static void stmf_update_kstat_lu_q(scsi_task_t *, void());
90 static void stmf_update_kstat_lport_q(scsi_task_t *, void());
91 static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *);
92 static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *);
93 
94 extern struct mod_ops mod_driverops;
95 
96 /* =====[ Tunables ]===== */
97 /* Internal tracing */
98 volatile int	stmf_trace_on = 1;
99 volatile int	stmf_trace_buf_size = (1 * 1024 * 1024);
100 /*
101  * The reason default task timeout is 75 is because we want the
102  * host to timeout 1st and mostly host timeout is 60 seconds.
103  */
104 volatile int	stmf_default_task_timeout = 75;
105 /*
106  * Setting this to one means, you are responsible for config load and keeping
107  * things in sync with persistent database.
108  */
109 volatile int	stmf_allow_modunload = 0;
110 
111 volatile int stmf_max_nworkers = 256;
112 volatile int stmf_min_nworkers = 4;
113 volatile int stmf_worker_scale_down_delay = 20;
114 
115 /* === [ Debugging and fault injection ] === */
116 #ifdef	DEBUG
117 volatile int stmf_drop_task_counter = 0;
118 volatile int stmf_drop_buf_counter = 0;
119 
120 #endif
121 
122 stmf_state_t		stmf_state;
123 static stmf_lu_t	*dlun0;
124 
125 static uint8_t stmf_first_zero[] =
126 	{ 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff };
127 static uint8_t stmf_first_one[] =
128 	{ 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 };
129 
130 static kmutex_t	trace_buf_lock;
131 static int	trace_buf_size;
132 static int	trace_buf_curndx;
133 caddr_t	stmf_trace_buf;
134 
135 static enum {
136 	STMF_WORKERS_DISABLED = 0,
137 	STMF_WORKERS_ENABLING,
138 	STMF_WORKERS_ENABLED
139 } stmf_workers_state = STMF_WORKERS_DISABLED;
140 static int stmf_i_max_nworkers;
141 static int stmf_i_min_nworkers;
142 static int stmf_nworkers_cur;		/* # of workers currently running */
143 static int stmf_nworkers_needed;	/* # of workers need to be running */
144 static int stmf_worker_sel_counter = 0;
145 static uint32_t stmf_cur_ntasks = 0;
146 static clock_t stmf_wm_last = 0;
147 /*
148  * This is equal to stmf_nworkers_cur while we are increasing # workers and
149  * stmf_nworkers_needed while we are decreasing the worker count.
150  */
151 static int stmf_nworkers_accepting_cmds;
152 static stmf_worker_t *stmf_workers = NULL;
153 static clock_t stmf_worker_mgmt_delay = 2;
154 static clock_t stmf_worker_scale_down_timer = 0;
155 static int stmf_worker_scale_down_qd = 0;
156 
157 static struct cb_ops stmf_cb_ops = {
158 	stmf_open,			/* open */
159 	stmf_close,			/* close */
160 	nodev,				/* strategy */
161 	nodev,				/* print */
162 	nodev,				/* dump */
163 	nodev,				/* read */
164 	nodev,				/* write */
165 	stmf_ioctl,			/* ioctl */
166 	nodev,				/* devmap */
167 	nodev,				/* mmap */
168 	nodev,				/* segmap */
169 	nochpoll,			/* chpoll */
170 	ddi_prop_op,			/* cb_prop_op */
171 	0,				/* streamtab */
172 	D_NEW | D_MP,			/* cb_flag */
173 	CB_REV,				/* rev */
174 	nodev,				/* aread */
175 	nodev				/* awrite */
176 };
177 
178 static struct dev_ops stmf_ops = {
179 	DEVO_REV,
180 	0,
181 	stmf_getinfo,
182 	nulldev,		/* identify */
183 	nulldev,		/* probe */
184 	stmf_attach,
185 	stmf_detach,
186 	nodev,			/* reset */
187 	&stmf_cb_ops,
188 	NULL,			/* bus_ops */
189 	NULL			/* power */
190 };
191 
192 #define	STMF_NAME		"COMSTAR STMF"
193 #define	STMF_MODULE_NAME	"stmf"
194 
195 static struct modldrv modldrv = {
196 	&mod_driverops,
197 	STMF_NAME,
198 	&stmf_ops
199 };
200 
201 static struct modlinkage modlinkage = {
202 	MODREV_1,
203 	&modldrv,
204 	NULL
205 };
206 
207 int
208 _init(void)
209 {
210 	int ret;
211 
212 	ret = mod_install(&modlinkage);
213 	if (ret)
214 		return (ret);
215 	stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP);
216 	trace_buf_size = stmf_trace_buf_size;
217 	trace_buf_curndx = 0;
218 	mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0);
219 	bzero(&stmf_state, sizeof (stmf_state_t));
220 	/* STMF service is off by default */
221 	stmf_state.stmf_service_running = 0;
222 	mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL);
223 	cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL);
224 	stmf_session_counter = (uint64_t)ddi_get_lbolt();
225 	stmf_view_init();
226 	stmf_svc_init();
227 	stmf_dlun_init();
228 	return (ret);
229 }
230 
231 int
232 _fini(void)
233 {
234 	int ret;
235 
236 	if (stmf_state.stmf_service_running)
237 		return (EBUSY);
238 	if ((!stmf_allow_modunload) &&
239 	    (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) {
240 		return (EBUSY);
241 	}
242 	if (stmf_state.stmf_nlps || stmf_state.stmf_npps) {
243 		return (EBUSY);
244 	}
245 	if (stmf_dlun_fini() != STMF_SUCCESS)
246 		return (EBUSY);
247 	if (stmf_worker_fini() != STMF_SUCCESS) {
248 		stmf_dlun_init();
249 		return (EBUSY);
250 	}
251 	if (stmf_svc_fini() != STMF_SUCCESS) {
252 		stmf_dlun_init();
253 		stmf_worker_init();
254 		return (EBUSY);
255 	}
256 
257 	ret = mod_remove(&modlinkage);
258 	if (ret) {
259 		stmf_svc_init();
260 		stmf_dlun_init();
261 		stmf_worker_init();
262 		return (ret);
263 	}
264 
265 	stmf_view_clear_config();
266 	kmem_free(stmf_trace_buf, stmf_trace_buf_size);
267 	mutex_destroy(&trace_buf_lock);
268 	mutex_destroy(&stmf_state.stmf_lock);
269 	cv_destroy(&stmf_state.stmf_cv);
270 	return (ret);
271 }
272 
273 int
274 _info(struct modinfo *modinfop)
275 {
276 	return (mod_info(&modlinkage, modinfop));
277 }
278 
279 /* ARGSUSED */
280 static int
281 stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
282 {
283 	switch (cmd) {
284 	case DDI_INFO_DEVT2DEVINFO:
285 		*result = stmf_state.stmf_dip;
286 		break;
287 	case DDI_INFO_DEVT2INSTANCE:
288 		*result =
289 		    (void *)(uintptr_t)ddi_get_instance(stmf_state.stmf_dip);
290 		break;
291 	default:
292 		return (DDI_FAILURE);
293 	}
294 
295 	return (DDI_SUCCESS);
296 }
297 
298 static int
299 stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
300 {
301 	switch (cmd) {
302 	case DDI_ATTACH:
303 		stmf_state.stmf_dip = dip;
304 
305 		if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0,
306 		    DDI_NT_STMF, 0) != DDI_SUCCESS) {
307 			break;
308 		}
309 		ddi_report_dev(dip);
310 		return (DDI_SUCCESS);
311 	}
312 
313 	return (DDI_FAILURE);
314 }
315 
316 static int
317 stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
318 {
319 	switch (cmd) {
320 	case DDI_DETACH:
321 		ddi_remove_minor_node(dip, 0);
322 		return (DDI_SUCCESS);
323 	}
324 
325 	return (DDI_FAILURE);
326 }
327 
328 /* ARGSUSED */
329 static int
330 stmf_open(dev_t *devp, int flag, int otype, cred_t *credp)
331 {
332 	mutex_enter(&stmf_state.stmf_lock);
333 	if (stmf_state.stmf_exclusive_open) {
334 		mutex_exit(&stmf_state.stmf_lock);
335 		return (EBUSY);
336 	}
337 	if (flag & FEXCL) {
338 		if (stmf_state.stmf_opened) {
339 			mutex_exit(&stmf_state.stmf_lock);
340 			return (EBUSY);
341 		}
342 		stmf_state.stmf_exclusive_open = 1;
343 	}
344 	stmf_state.stmf_opened = 1;
345 	mutex_exit(&stmf_state.stmf_lock);
346 	return (0);
347 }
348 
349 /* ARGSUSED */
350 static int
351 stmf_close(dev_t dev, int flag, int otype, cred_t *credp)
352 {
353 	mutex_enter(&stmf_state.stmf_lock);
354 	stmf_state.stmf_opened = 0;
355 	if (stmf_state.stmf_exclusive_open &&
356 	    (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) {
357 		stmf_state.stmf_config_state = STMF_CONFIG_NONE;
358 		stmf_delete_all_ppds();
359 		stmf_view_clear_config();
360 		stmf_view_init();
361 	}
362 	stmf_state.stmf_exclusive_open = 0;
363 	mutex_exit(&stmf_state.stmf_lock);
364 	return (0);
365 }
366 
367 int
368 stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd,
369 						void **ibuf, void **obuf)
370 {
371 	int ret;
372 
373 	*ibuf = NULL;
374 	*obuf = NULL;
375 	*iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP);
376 
377 	ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode);
378 	if (ret)
379 		return (EFAULT);
380 	if ((*iocd)->stmf_version != STMF_VERSION_1) {
381 		ret = EINVAL;
382 		goto copyin_iocdata_done;
383 	}
384 	if ((*iocd)->stmf_ibuf_size) {
385 		*ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP);
386 		ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf),
387 		    *ibuf, (*iocd)->stmf_ibuf_size, mode);
388 	}
389 	if ((*iocd)->stmf_obuf_size)
390 		*obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP);
391 
392 	if (ret == 0)
393 		return (0);
394 	ret = EFAULT;
395 copyin_iocdata_done:;
396 	if (*obuf) {
397 		kmem_free(*obuf, (*iocd)->stmf_obuf_size);
398 		*obuf = NULL;
399 	}
400 	if (*ibuf) {
401 		kmem_free(*ibuf, (*iocd)->stmf_ibuf_size);
402 		*ibuf = NULL;
403 	}
404 	kmem_free(*iocd, sizeof (stmf_iocdata_t));
405 	return (ret);
406 }
407 
408 int
409 stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf)
410 {
411 	int ret;
412 
413 	if (iocd->stmf_obuf_size) {
414 		ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf,
415 		    iocd->stmf_obuf_size, mode);
416 		if (ret)
417 			return (EFAULT);
418 	}
419 	ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode);
420 	if (ret)
421 		return (EFAULT);
422 	return (0);
423 }
424 
425 /* ARGSUSED */
426 static int
427 stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
428 	cred_t *credp, int *rval)
429 {
430 	stmf_iocdata_t *iocd;
431 	void *ibuf = NULL, *obuf = NULL;
432 	slist_lu_t *luid_list;
433 	slist_target_port_t *lportid_list;
434 	stmf_i_lu_t *ilu;
435 	stmf_i_local_port_t *ilport;
436 	stmf_i_scsi_session_t *iss;
437 	slist_scsi_session_t *iss_list;
438 	sioc_lu_props_t *lup;
439 	sioc_target_port_props_t *lportp;
440 	stmf_ppioctl_data_t *ppi, *ppi_out = NULL;
441 	uint64_t *ppi_token = NULL;
442 	uint8_t *p_id, *id;
443 	stmf_state_desc_t *std;
444 	stmf_status_t ctl_ret;
445 	stmf_state_change_info_t ssi;
446 	int ret = 0;
447 	uint32_t n;
448 	int i;
449 	stmf_group_op_data_t *grp_entry;
450 	stmf_group_name_t *grpname;
451 	stmf_view_op_entry_t *ve;
452 	stmf_id_type_t idtype;
453 	stmf_id_data_t *id_entry;
454 	stmf_id_list_t	*id_list;
455 	stmf_view_entry_t *view_entry;
456 	uint32_t	veid;
457 
458 	if ((cmd & 0xff000000) != STMF_IOCTL) {
459 		return (ENOTTY);
460 	}
461 
462 	if (drv_priv(credp) != 0) {
463 		return (EPERM);
464 	}
465 
466 	ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf);
467 	if (ret)
468 		return (ret);
469 	iocd->stmf_error = 0;
470 
471 	switch (cmd) {
472 	case STMF_IOCTL_LU_LIST:
473 		/* retrieves both registered/unregistered */
474 		mutex_enter(&stmf_state.stmf_lock);
475 		id_list = &stmf_state.stmf_luid_list;
476 		n = min(id_list->id_count,
477 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
478 		iocd->stmf_obuf_max_nentries = id_list->id_count;
479 		luid_list = (slist_lu_t *)obuf;
480 		id_entry = id_list->idl_head;
481 		for (i = 0; i < n; i++) {
482 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
483 			id_entry = id_entry->id_next;
484 		}
485 
486 		n = iocd->stmf_obuf_size/sizeof (slist_lu_t);
487 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
488 			id = (uint8_t *)ilu->ilu_lu->lu_id;
489 			if (stmf_lookup_id(id_list, 16, id + 4) == NULL) {
490 				iocd->stmf_obuf_max_nentries++;
491 				if (i < n) {
492 					bcopy(id + 4, luid_list[i].lu_guid,
493 					    sizeof (slist_lu_t));
494 					i++;
495 				}
496 			}
497 		}
498 		iocd->stmf_obuf_nentries = i;
499 		mutex_exit(&stmf_state.stmf_lock);
500 		break;
501 
502 	case STMF_IOCTL_REG_LU_LIST:
503 		mutex_enter(&stmf_state.stmf_lock);
504 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus;
505 		n = min(stmf_state.stmf_nlus,
506 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
507 		iocd->stmf_obuf_nentries = n;
508 		ilu = stmf_state.stmf_ilulist;
509 		luid_list = (slist_lu_t *)obuf;
510 		for (i = 0; i < n; i++) {
511 			uint8_t *id;
512 			id = (uint8_t *)ilu->ilu_lu->lu_id;
513 			bcopy(id + 4, luid_list[i].lu_guid, 16);
514 			ilu = ilu->ilu_next;
515 		}
516 		mutex_exit(&stmf_state.stmf_lock);
517 		break;
518 
519 	case STMF_IOCTL_VE_LU_LIST:
520 		mutex_enter(&stmf_state.stmf_lock);
521 		id_list = &stmf_state.stmf_luid_list;
522 		n = min(id_list->id_count,
523 		    (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
524 		iocd->stmf_obuf_max_nentries = id_list->id_count;
525 		iocd->stmf_obuf_nentries = n;
526 		luid_list = (slist_lu_t *)obuf;
527 		id_entry = id_list->idl_head;
528 		for (i = 0; i < n; i++) {
529 			bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
530 			id_entry = id_entry->id_next;
531 		}
532 		mutex_exit(&stmf_state.stmf_lock);
533 		break;
534 
535 	case STMF_IOCTL_TARGET_PORT_LIST:
536 		mutex_enter(&stmf_state.stmf_lock);
537 		iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports;
538 		n = min(stmf_state.stmf_nlports,
539 		    (iocd->stmf_obuf_size)/sizeof (slist_target_port_t));
540 		iocd->stmf_obuf_nentries = n;
541 		ilport = stmf_state.stmf_ilportlist;
542 		lportid_list = (slist_target_port_t *)obuf;
543 		for (i = 0; i < n; i++) {
544 			uint8_t *id;
545 			id = (uint8_t *)ilport->ilport_lport->lport_id;
546 			bcopy(id, lportid_list[i].target, id[3] + 4);
547 			ilport = ilport->ilport_next;
548 		}
549 		mutex_exit(&stmf_state.stmf_lock);
550 		break;
551 
552 	case STMF_IOCTL_SESSION_LIST:
553 		p_id = (uint8_t *)ibuf;
554 		if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) ||
555 		    (iocd->stmf_ibuf_size < (p_id[3] + 4))) {
556 			ret = EINVAL;
557 			break;
558 		}
559 		mutex_enter(&stmf_state.stmf_lock);
560 		for (ilport = stmf_state.stmf_ilportlist; ilport; ilport =
561 		    ilport->ilport_next) {
562 			uint8_t *id;
563 			id = (uint8_t *)ilport->ilport_lport->lport_id;
564 			if ((p_id[3] == id[3]) &&
565 			    (bcmp(p_id + 4, id + 4, id[3]) == 0)) {
566 				break;
567 			}
568 		}
569 		if (ilport == NULL) {
570 			mutex_exit(&stmf_state.stmf_lock);
571 			ret = ENOENT;
572 			break;
573 		}
574 		iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions;
575 		n = min(ilport->ilport_nsessions,
576 		    (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t));
577 		iocd->stmf_obuf_nentries = n;
578 		iss = ilport->ilport_ss_list;
579 		iss_list = (slist_scsi_session_t *)obuf;
580 		for (i = 0; i < n; i++) {
581 			uint8_t *id;
582 			id = (uint8_t *)iss->iss_ss->ss_rport_id;
583 			bcopy(id, iss_list[i].initiator, id[3] + 4);
584 			iss_list[i].creation_time = (uint32_t)
585 			    iss->iss_creation_time;
586 			if (iss->iss_ss->ss_rport_alias) {
587 				(void) strncpy(iss_list[i].alias,
588 				    iss->iss_ss->ss_rport_alias, 255);
589 				iss_list[i].alias[255] = 0;
590 			} else {
591 				iss_list[i].alias[0] = 0;
592 			}
593 			iss = iss->iss_next;
594 		}
595 		mutex_exit(&stmf_state.stmf_lock);
596 		break;
597 
598 	case STMF_IOCTL_GET_LU_PROPERTIES:
599 		p_id = (uint8_t *)ibuf;
600 		if ((iocd->stmf_ibuf_size < 16) ||
601 		    (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) ||
602 		    (p_id[0] == 0)) {
603 			ret = EINVAL;
604 			break;
605 		}
606 		mutex_enter(&stmf_state.stmf_lock);
607 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
608 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
609 				break;
610 		}
611 		if (ilu == NULL) {
612 			mutex_exit(&stmf_state.stmf_lock);
613 			ret = ENOENT;
614 			break;
615 		}
616 		lup = (sioc_lu_props_t *)obuf;
617 		bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16);
618 		lup->lu_state = ilu->ilu_state & 0x0f;
619 		lup->lu_present = 1; /* XXX */
620 		(void) strncpy(lup->lu_provider_name,
621 		    ilu->ilu_lu->lu_lp->lp_name, 255);
622 		lup->lu_provider_name[254] = 0;
623 		if (ilu->ilu_lu->lu_alias) {
624 			(void) strncpy(lup->lu_alias,
625 			    ilu->ilu_lu->lu_alias, 255);
626 			lup->lu_alias[255] = 0;
627 		} else {
628 			lup->lu_alias[0] = 0;
629 		}
630 		mutex_exit(&stmf_state.stmf_lock);
631 		break;
632 
633 	case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES:
634 		p_id = (uint8_t *)ibuf;
635 		if ((p_id == NULL) ||
636 		    (iocd->stmf_ibuf_size < (p_id[3] + 4)) ||
637 		    (iocd->stmf_obuf_size <
638 		    sizeof (sioc_target_port_props_t))) {
639 			ret = EINVAL;
640 			break;
641 		}
642 		mutex_enter(&stmf_state.stmf_lock);
643 		for (ilport = stmf_state.stmf_ilportlist; ilport;
644 		    ilport = ilport->ilport_next) {
645 			uint8_t *id;
646 			id = (uint8_t *)ilport->ilport_lport->lport_id;
647 			if ((p_id[3] == id[3]) &&
648 			    (bcmp(p_id+4, id+4, id[3]) == 0))
649 				break;
650 		}
651 		if (ilport == NULL) {
652 			mutex_exit(&stmf_state.stmf_lock);
653 			ret = ENOENT;
654 			break;
655 		}
656 		lportp = (sioc_target_port_props_t *)obuf;
657 		bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id,
658 		    ilport->ilport_lport->lport_id->ident_length + 4);
659 		lportp->tgt_state = ilport->ilport_state & 0x0f;
660 		lportp->tgt_present = 1; /* XXX */
661 		(void) strncpy(lportp->tgt_provider_name,
662 		    ilport->ilport_lport->lport_pp->pp_name, 255);
663 		lportp->tgt_provider_name[254] = 0;
664 		if (ilport->ilport_lport->lport_alias) {
665 			(void) strncpy(lportp->tgt_alias,
666 			    ilport->ilport_lport->lport_alias, 255);
667 			lportp->tgt_alias[255] = 0;
668 		} else {
669 			lportp->tgt_alias[0] = 0;
670 		}
671 		mutex_exit(&stmf_state.stmf_lock);
672 		break;
673 
674 	case STMF_IOCTL_SET_STMF_STATE:
675 		if ((ibuf == NULL) ||
676 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
677 			ret = EINVAL;
678 			break;
679 		}
680 		ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf);
681 		break;
682 
683 	case STMF_IOCTL_GET_STMF_STATE:
684 		if ((obuf == NULL) ||
685 		    (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) {
686 			ret = EINVAL;
687 			break;
688 		}
689 		ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf);
690 		break;
691 
692 	case STMF_IOCTL_SET_LU_STATE:
693 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
694 		ssi.st_additional_info = NULL;
695 		std = (stmf_state_desc_t *)ibuf;
696 		if ((ibuf == NULL) ||
697 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
698 			ret = EINVAL;
699 			break;
700 		}
701 		p_id = std->ident;
702 		mutex_enter(&stmf_state.stmf_lock);
703 		if (stmf_state.stmf_inventory_locked) {
704 			mutex_exit(&stmf_state.stmf_lock);
705 			ret = EBUSY;
706 			break;
707 		}
708 		for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
709 			if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
710 				break;
711 		}
712 		if (ilu == NULL) {
713 			mutex_exit(&stmf_state.stmf_lock);
714 			ret = ENOENT;
715 			break;
716 		}
717 		stmf_state.stmf_inventory_locked = 1;
718 		mutex_exit(&stmf_state.stmf_lock);
719 		cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE :
720 		    STMF_CMD_LU_OFFLINE;
721 		ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi);
722 		if (ctl_ret == STMF_ALREADY)
723 			ret = 0;
724 		else if (ctl_ret != STMF_SUCCESS)
725 			ret = EIO;
726 		mutex_enter(&stmf_state.stmf_lock);
727 		stmf_state.stmf_inventory_locked = 0;
728 		mutex_exit(&stmf_state.stmf_lock);
729 		break;
730 
731 	case STMF_IOCTL_SET_TARGET_PORT_STATE:
732 		ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
733 		ssi.st_additional_info = NULL;
734 		std = (stmf_state_desc_t *)ibuf;
735 		if ((ibuf == NULL) ||
736 		    (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
737 			ret = EINVAL;
738 			break;
739 		}
740 		p_id = std->ident;
741 		mutex_enter(&stmf_state.stmf_lock);
742 		if (stmf_state.stmf_inventory_locked) {
743 			mutex_exit(&stmf_state.stmf_lock);
744 			ret = EBUSY;
745 			break;
746 		}
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 ((id[3] == p_id[3]) &&
752 			    (bcmp(id+4, p_id+4, id[3]) == 0)) {
753 				break;
754 			}
755 		}
756 		if (ilport == NULL) {
757 			mutex_exit(&stmf_state.stmf_lock);
758 			ret = ENOENT;
759 			break;
760 		}
761 		stmf_state.stmf_inventory_locked = 1;
762 		mutex_exit(&stmf_state.stmf_lock);
763 		cmd = (std->state == STMF_STATE_ONLINE) ?
764 		    STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE;
765 		ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi);
766 		if (ctl_ret == STMF_ALREADY)
767 			ret = 0;
768 		else if (ctl_ret != STMF_SUCCESS)
769 			ret = EIO;
770 		mutex_enter(&stmf_state.stmf_lock);
771 		stmf_state.stmf_inventory_locked = 0;
772 		mutex_exit(&stmf_state.stmf_lock);
773 		break;
774 
775 	case STMF_IOCTL_ADD_HG_ENTRY:
776 		idtype = STMF_ID_TYPE_HOST;
777 		/* FALLTHROUGH */
778 	case STMF_IOCTL_ADD_TG_ENTRY:
779 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
780 			ret = EACCES;
781 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
782 			break;
783 		}
784 		if (cmd == STMF_IOCTL_ADD_TG_ENTRY) {
785 			idtype = STMF_ID_TYPE_TARGET;
786 		}
787 		grp_entry = (stmf_group_op_data_t *)ibuf;
788 		if ((ibuf == NULL) ||
789 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
790 			ret = EINVAL;
791 			break;
792 		}
793 		if (grp_entry->group.name[0] == '*') {
794 			ret = EINVAL;
795 			break; /* not allowed */
796 		}
797 		mutex_enter(&stmf_state.stmf_lock);
798 		if (idtype == STMF_ID_TYPE_TARGET &&
799 		    stmf_state.stmf_service_running) {
800 			mutex_exit(&stmf_state.stmf_lock);
801 			iocd->stmf_error =
802 			    STMF_IOCERR_TG_UPDATE_NEED_SVC_OFFLINE;
803 			ret = EBUSY;
804 			break; /* not allowed */
805 		}
806 		ret = stmf_add_group_member(grp_entry->group.name,
807 		    grp_entry->group.name_size,
808 		    grp_entry->ident + 4,
809 		    grp_entry->ident[3],
810 		    idtype,
811 		    &iocd->stmf_error);
812 		mutex_exit(&stmf_state.stmf_lock);
813 		break;
814 	case STMF_IOCTL_REMOVE_HG_ENTRY:
815 		idtype = STMF_ID_TYPE_HOST;
816 		/* FALLTHROUGH */
817 	case STMF_IOCTL_REMOVE_TG_ENTRY:
818 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
819 			ret = EACCES;
820 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
821 			break;
822 		}
823 		if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) {
824 			idtype = STMF_ID_TYPE_TARGET;
825 		}
826 		grp_entry = (stmf_group_op_data_t *)ibuf;
827 		if ((ibuf == NULL) ||
828 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
829 			ret = EINVAL;
830 			break;
831 		}
832 		if (grp_entry->group.name[0] == '*') {
833 			ret = EINVAL;
834 			break; /* not allowed */
835 		}
836 		mutex_enter(&stmf_state.stmf_lock);
837 		if (idtype == STMF_ID_TYPE_TARGET &&
838 		    stmf_state.stmf_service_running) {
839 			mutex_exit(&stmf_state.stmf_lock);
840 			iocd->stmf_error =
841 			    STMF_IOCERR_TG_UPDATE_NEED_SVC_OFFLINE;
842 			ret = EBUSY;
843 			break; /* not allowed */
844 		}
845 		ret = stmf_remove_group_member(grp_entry->group.name,
846 		    grp_entry->group.name_size,
847 		    grp_entry->ident + 4,
848 		    grp_entry->ident[3],
849 		    idtype,
850 		    &iocd->stmf_error);
851 		mutex_exit(&stmf_state.stmf_lock);
852 		break;
853 	case STMF_IOCTL_CREATE_HOST_GROUP:
854 		idtype = STMF_ID_TYPE_HOST_GROUP;
855 		/* FALLTHROUGH */
856 	case STMF_IOCTL_CREATE_TARGET_GROUP:
857 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
858 			ret = EACCES;
859 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
860 			break;
861 		}
862 		grpname = (stmf_group_name_t *)ibuf;
863 
864 		if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP)
865 			idtype = STMF_ID_TYPE_TARGET_GROUP;
866 		if ((ibuf == NULL) ||
867 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
868 			ret = EINVAL;
869 			break;
870 		}
871 		if (grpname->name[0] == '*') {
872 			ret = EINVAL;
873 			break; /* not allowed */
874 		}
875 		mutex_enter(&stmf_state.stmf_lock);
876 		ret = stmf_add_group(grpname->name,
877 		    grpname->name_size, idtype, &iocd->stmf_error);
878 		mutex_exit(&stmf_state.stmf_lock);
879 		break;
880 	case STMF_IOCTL_REMOVE_HOST_GROUP:
881 		idtype = STMF_ID_TYPE_HOST_GROUP;
882 		/* FALLTHROUGH */
883 	case STMF_IOCTL_REMOVE_TARGET_GROUP:
884 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
885 			ret = EACCES;
886 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
887 			break;
888 		}
889 		grpname = (stmf_group_name_t *)ibuf;
890 		if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP)
891 			idtype = STMF_ID_TYPE_TARGET_GROUP;
892 		if ((ibuf == NULL) ||
893 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
894 			ret = EINVAL;
895 			break;
896 		}
897 		if (grpname->name[0] == '*') {
898 			ret = EINVAL;
899 			break; /* not allowed */
900 		}
901 		mutex_enter(&stmf_state.stmf_lock);
902 		ret = stmf_remove_group(grpname->name,
903 		    grpname->name_size, idtype, &iocd->stmf_error);
904 		mutex_exit(&stmf_state.stmf_lock);
905 		break;
906 	case STMF_IOCTL_ADD_VIEW_ENTRY:
907 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
908 			ret = EACCES;
909 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
910 			break;
911 		}
912 		ve = (stmf_view_op_entry_t *)ibuf;
913 		if ((ibuf == NULL) ||
914 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
915 			ret = EINVAL;
916 			break;
917 		}
918 		if (!ve->ve_lu_number_valid)
919 			ve->ve_lu_nbr[2] = 0xFF;
920 		if (ve->ve_all_hosts) {
921 			ve->ve_host_group.name[0] = '*';
922 			ve->ve_host_group.name_size = 1;
923 		}
924 		if (ve->ve_all_targets) {
925 			ve->ve_target_group.name[0] = '*';
926 			ve->ve_target_group.name_size = 1;
927 		}
928 		if (ve->ve_ndx_valid)
929 			veid = ve->ve_ndx;
930 		else
931 			veid = 0xffffffff;
932 		mutex_enter(&stmf_state.stmf_lock);
933 		ret = stmf_add_ve(ve->ve_host_group.name,
934 		    ve->ve_host_group.name_size,
935 		    ve->ve_target_group.name,
936 		    ve->ve_target_group.name_size,
937 		    ve->ve_guid,
938 		    &veid,
939 		    ve->ve_lu_nbr,
940 		    &iocd->stmf_error);
941 		mutex_exit(&stmf_state.stmf_lock);
942 		if (ret == 0 &&
943 		    (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) &&
944 		    iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) {
945 			stmf_view_op_entry_t *ve_ret =
946 			    (stmf_view_op_entry_t *)obuf;
947 			iocd->stmf_obuf_nentries = 1;
948 			iocd->stmf_obuf_max_nentries = 1;
949 			if (!ve->ve_ndx_valid) {
950 				ve_ret->ve_ndx = veid;
951 				ve_ret->ve_ndx_valid = 1;
952 			}
953 			if (!ve->ve_lu_number_valid) {
954 				ve_ret->ve_lu_number_valid = 1;
955 				bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8);
956 			}
957 		}
958 		break;
959 	case STMF_IOCTL_REMOVE_VIEW_ENTRY:
960 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
961 			ret = EACCES;
962 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
963 			break;
964 		}
965 		ve = (stmf_view_op_entry_t *)ibuf;
966 		if ((ibuf == NULL) ||
967 		    (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
968 			ret = EINVAL;
969 			break;
970 		}
971 		if (!ve->ve_ndx_valid) {
972 			ret = EINVAL;
973 			break;
974 		}
975 		mutex_enter(&stmf_state.stmf_lock);
976 		ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx,
977 		    &iocd->stmf_error);
978 		mutex_exit(&stmf_state.stmf_lock);
979 		break;
980 	case STMF_IOCTL_GET_HG_LIST:
981 		id_list = &stmf_state.stmf_hg_list;
982 		/* FALLTHROUGH */
983 	case STMF_IOCTL_GET_TG_LIST:
984 		if (cmd == STMF_IOCTL_GET_TG_LIST)
985 			id_list = &stmf_state.stmf_tg_list;
986 		mutex_enter(&stmf_state.stmf_lock);
987 		iocd->stmf_obuf_max_nentries = id_list->id_count;
988 		n = min(id_list->id_count,
989 		    (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t));
990 		iocd->stmf_obuf_nentries = n;
991 		id_entry = id_list->idl_head;
992 		grpname = (stmf_group_name_t *)obuf;
993 		for (i = 0; i < n; i++) {
994 			if (id_entry->id_data[0] == '*') {
995 				if (iocd->stmf_obuf_nentries > 0) {
996 					iocd->stmf_obuf_nentries--;
997 				}
998 				id_entry = id_entry->id_next;
999 				continue;
1000 			}
1001 			grpname->name_size = id_entry->id_data_size;
1002 			bcopy(id_entry->id_data, grpname->name,
1003 			    id_entry->id_data_size);
1004 			grpname++;
1005 			id_entry = id_entry->id_next;
1006 		}
1007 		mutex_exit(&stmf_state.stmf_lock);
1008 		break;
1009 	case STMF_IOCTL_GET_HG_ENTRIES:
1010 		id_list = &stmf_state.stmf_hg_list;
1011 		/* FALLTHROUGH */
1012 	case STMF_IOCTL_GET_TG_ENTRIES:
1013 		grpname = (stmf_group_name_t *)ibuf;
1014 		if ((ibuf == NULL) ||
1015 		    (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1016 			ret = EINVAL;
1017 			break;
1018 		}
1019 		if (cmd == STMF_IOCTL_GET_TG_ENTRIES) {
1020 			id_list = &stmf_state.stmf_tg_list;
1021 		}
1022 		mutex_enter(&stmf_state.stmf_lock);
1023 		id_entry = stmf_lookup_id(id_list, grpname->name_size,
1024 		    grpname->name);
1025 		if (!id_entry)
1026 			ret = ENODEV;
1027 		else {
1028 			stmf_ge_ident_t *grp_entry;
1029 			id_list = (stmf_id_list_t *)id_entry->id_impl_specific;
1030 			iocd->stmf_obuf_max_nentries = id_list->id_count;
1031 			n = min(id_list->id_count,
1032 			    iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t));
1033 			iocd->stmf_obuf_nentries = n;
1034 			id_entry = id_list->idl_head;
1035 			grp_entry = (stmf_ge_ident_t *)obuf;
1036 			for (i = 0; i < n; i++) {
1037 				bcopy(id_entry->id_data, grp_entry->ident,
1038 				    id_entry->id_data_size);
1039 				grp_entry->ident_size = id_entry->id_data_size;
1040 				id_entry = id_entry->id_next;
1041 				grp_entry++;
1042 			}
1043 		}
1044 		mutex_exit(&stmf_state.stmf_lock);
1045 		break;
1046 
1047 	case STMF_IOCTL_GET_VE_LIST:
1048 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1049 		mutex_enter(&stmf_state.stmf_lock);
1050 		ve = (stmf_view_op_entry_t *)obuf;
1051 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
1052 		    id_entry; id_entry = id_entry->id_next) {
1053 			for (view_entry = (stmf_view_entry_t *)
1054 			    id_entry->id_impl_specific; view_entry;
1055 			    view_entry = view_entry->ve_next) {
1056 				iocd->stmf_obuf_max_nentries++;
1057 				if (iocd->stmf_obuf_nentries >= n)
1058 					continue;
1059 				ve->ve_ndx_valid = 1;
1060 				ve->ve_ndx = view_entry->ve_id;
1061 				ve->ve_lu_number_valid = 1;
1062 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1063 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1064 				    view_entry->ve_luid->id_data_size);
1065 				if (view_entry->ve_hg->id_data[0] == '*') {
1066 					ve->ve_all_hosts = 1;
1067 				} else {
1068 					bcopy(view_entry->ve_hg->id_data,
1069 					    ve->ve_host_group.name,
1070 					    view_entry->ve_hg->id_data_size);
1071 					ve->ve_host_group.name_size =
1072 					    view_entry->ve_hg->id_data_size;
1073 				}
1074 
1075 				if (view_entry->ve_tg->id_data[0] == '*') {
1076 					ve->ve_all_targets = 1;
1077 				} else {
1078 					bcopy(view_entry->ve_tg->id_data,
1079 					    ve->ve_target_group.name,
1080 					    view_entry->ve_tg->id_data_size);
1081 					ve->ve_target_group.name_size =
1082 					    view_entry->ve_tg->id_data_size;
1083 				}
1084 				ve++;
1085 				iocd->stmf_obuf_nentries++;
1086 			}
1087 		}
1088 		mutex_exit(&stmf_state.stmf_lock);
1089 		break;
1090 
1091 	case STMF_IOCTL_LU_VE_LIST:
1092 		p_id = (uint8_t *)ibuf;
1093 		if ((iocd->stmf_ibuf_size != 16) ||
1094 		    (iocd->stmf_obuf_size < sizeof (stmf_view_op_entry_t))) {
1095 			ret = EINVAL;
1096 			break;
1097 		}
1098 
1099 		n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1100 		mutex_enter(&stmf_state.stmf_lock);
1101 		ve = (stmf_view_op_entry_t *)obuf;
1102 		for (id_entry = stmf_state.stmf_luid_list.idl_head;
1103 		    id_entry; id_entry = id_entry->id_next) {
1104 			if (bcmp(id_entry->id_data, p_id, 16) != 0)
1105 				continue;
1106 			for (view_entry = (stmf_view_entry_t *)
1107 			    id_entry->id_impl_specific; view_entry;
1108 			    view_entry = view_entry->ve_next) {
1109 				iocd->stmf_obuf_max_nentries++;
1110 				if (iocd->stmf_obuf_nentries >= n)
1111 					continue;
1112 				ve->ve_ndx_valid = 1;
1113 				ve->ve_ndx = view_entry->ve_id;
1114 				ve->ve_lu_number_valid = 1;
1115 				bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1116 				bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1117 				    view_entry->ve_luid->id_data_size);
1118 				if (view_entry->ve_hg->id_data[0] == '*') {
1119 					ve->ve_all_hosts = 1;
1120 				} else {
1121 					bcopy(view_entry->ve_hg->id_data,
1122 					    ve->ve_host_group.name,
1123 					    view_entry->ve_hg->id_data_size);
1124 					ve->ve_host_group.name_size =
1125 					    view_entry->ve_hg->id_data_size;
1126 				}
1127 
1128 				if (view_entry->ve_tg->id_data[0] == '*') {
1129 					ve->ve_all_targets = 1;
1130 				} else {
1131 					bcopy(view_entry->ve_tg->id_data,
1132 					    ve->ve_target_group.name,
1133 					    view_entry->ve_tg->id_data_size);
1134 					ve->ve_target_group.name_size =
1135 					    view_entry->ve_tg->id_data_size;
1136 				}
1137 				ve++;
1138 				iocd->stmf_obuf_nentries++;
1139 			}
1140 			break;
1141 		}
1142 		mutex_exit(&stmf_state.stmf_lock);
1143 		break;
1144 
1145 	case STMF_IOCTL_LOAD_PP_DATA:
1146 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1147 			ret = EACCES;
1148 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1149 			break;
1150 		}
1151 		ppi = (stmf_ppioctl_data_t *)ibuf;
1152 		if ((ppi == NULL) ||
1153 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1154 			ret = EINVAL;
1155 			break;
1156 		}
1157 		/* returned token */
1158 		ppi_token = (uint64_t *)obuf;
1159 		if ((ppi_token == NULL) ||
1160 		    (iocd->stmf_obuf_size < sizeof (uint64_t))) {
1161 			ret = EINVAL;
1162 			break;
1163 		}
1164 		ret = stmf_load_ppd_ioctl(ppi, ppi_token, &iocd->stmf_error);
1165 		break;
1166 
1167 	case STMF_IOCTL_GET_PP_DATA:
1168 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1169 			ret = EACCES;
1170 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1171 			break;
1172 		}
1173 		ppi = (stmf_ppioctl_data_t *)ibuf;
1174 		if (ppi == NULL ||
1175 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1176 			ret = EINVAL;
1177 			break;
1178 		}
1179 		ppi_out = (stmf_ppioctl_data_t *)obuf;
1180 		if ((ppi_out == NULL) ||
1181 		    (iocd->stmf_obuf_size < sizeof (stmf_ppioctl_data_t))) {
1182 			ret = EINVAL;
1183 			break;
1184 		}
1185 		ret = stmf_get_ppd_ioctl(ppi, ppi_out, &iocd->stmf_error);
1186 		break;
1187 
1188 	case STMF_IOCTL_CLEAR_PP_DATA:
1189 		if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1190 			ret = EACCES;
1191 			iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1192 			break;
1193 		}
1194 		ppi = (stmf_ppioctl_data_t *)ibuf;
1195 		if ((ppi == NULL) ||
1196 		    (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1197 			ret = EINVAL;
1198 			break;
1199 		}
1200 		ret = stmf_delete_ppd_ioctl(ppi);
1201 		break;
1202 
1203 	case STMF_IOCTL_CLEAR_TRACE:
1204 		stmf_trace_clear();
1205 		break;
1206 
1207 	case STMF_IOCTL_ADD_TRACE:
1208 		if (iocd->stmf_ibuf_size && ibuf) {
1209 			((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0;
1210 			stmf_trace("\nstradm", "%s\n", ibuf);
1211 		}
1212 		break;
1213 
1214 	case STMF_IOCTL_GET_TRACE_POSITION:
1215 		if (obuf && (iocd->stmf_obuf_size > 3)) {
1216 			mutex_enter(&trace_buf_lock);
1217 			*((int *)obuf) = trace_buf_curndx;
1218 			mutex_exit(&trace_buf_lock);
1219 		} else {
1220 			ret = EINVAL;
1221 		}
1222 		break;
1223 
1224 	case STMF_IOCTL_GET_TRACE:
1225 		if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) {
1226 			ret = EINVAL;
1227 			break;
1228 		}
1229 		i = *((int *)ibuf);
1230 		if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) >
1231 		    trace_buf_size)) {
1232 			ret = EINVAL;
1233 			break;
1234 		}
1235 		mutex_enter(&trace_buf_lock);
1236 		bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size);
1237 		mutex_exit(&trace_buf_lock);
1238 		break;
1239 
1240 	default:
1241 		ret = ENOTTY;
1242 	}
1243 
1244 	if (ret == 0) {
1245 		ret = stmf_copyout_iocdata(data, mode, iocd, obuf);
1246 	} else if (iocd->stmf_error) {
1247 		(void) stmf_copyout_iocdata(data, mode, iocd, obuf);
1248 	}
1249 	if (obuf) {
1250 		kmem_free(obuf, iocd->stmf_obuf_size);
1251 		obuf = NULL;
1252 	}
1253 	if (ibuf) {
1254 		kmem_free(ibuf, iocd->stmf_ibuf_size);
1255 		ibuf = NULL;
1256 	}
1257 	kmem_free(iocd, sizeof (stmf_iocdata_t));
1258 	return (ret);
1259 }
1260 
1261 static int
1262 stmf_get_service_state()
1263 {
1264 	stmf_i_local_port_t *ilport;
1265 	stmf_i_lu_t *ilu;
1266 	int online = 0;
1267 	int offline = 0;
1268 	int onlining = 0;
1269 	int offlining = 0;
1270 
1271 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1272 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1273 	    ilport = ilport->ilport_next) {
1274 		if (ilport->ilport_state == STMF_STATE_OFFLINE)
1275 			offline++;
1276 		else if (ilport->ilport_state == STMF_STATE_ONLINE)
1277 			online++;
1278 		else if (ilport->ilport_state == STMF_STATE_ONLINING)
1279 			onlining++;
1280 		else if (ilport->ilport_state == STMF_STATE_OFFLINING)
1281 			offlining++;
1282 	}
1283 
1284 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1285 	    ilu = ilu->ilu_next) {
1286 		if (ilu->ilu_state == STMF_STATE_OFFLINE)
1287 			offline++;
1288 		else if (ilu->ilu_state == STMF_STATE_ONLINE)
1289 			online++;
1290 		else if (ilu->ilu_state == STMF_STATE_ONLINING)
1291 			onlining++;
1292 		else if (ilu->ilu_state == STMF_STATE_OFFLINING)
1293 			offlining++;
1294 	}
1295 
1296 	if (stmf_state.stmf_service_running) {
1297 		if (onlining)
1298 			return (STMF_STATE_ONLINING);
1299 		else
1300 			return (STMF_STATE_ONLINE);
1301 	}
1302 
1303 	if (offlining) {
1304 		return (STMF_STATE_OFFLINING);
1305 	}
1306 
1307 	return (STMF_STATE_OFFLINE);
1308 }
1309 
1310 static int
1311 stmf_set_stmf_state(stmf_state_desc_t *std)
1312 {
1313 	stmf_i_local_port_t *ilport;
1314 	stmf_i_lu_t *ilu;
1315 	stmf_state_change_info_t ssi;
1316 	int svc_state;
1317 
1318 	ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
1319 	ssi.st_additional_info = NULL;
1320 
1321 	mutex_enter(&stmf_state.stmf_lock);
1322 	if (!stmf_state.stmf_exclusive_open) {
1323 		mutex_exit(&stmf_state.stmf_lock);
1324 		return (EACCES);
1325 	}
1326 
1327 	if (stmf_state.stmf_inventory_locked) {
1328 		mutex_exit(&stmf_state.stmf_lock);
1329 		return (EBUSY);
1330 	}
1331 
1332 	if ((std->state != STMF_STATE_ONLINE) &&
1333 	    (std->state != STMF_STATE_OFFLINE)) {
1334 		mutex_exit(&stmf_state.stmf_lock);
1335 		return (EINVAL);
1336 	}
1337 
1338 	svc_state = stmf_get_service_state();
1339 	if ((svc_state == STMF_STATE_OFFLINING) ||
1340 	    (svc_state == STMF_STATE_ONLINING)) {
1341 		mutex_exit(&stmf_state.stmf_lock);
1342 		return (EBUSY);
1343 	}
1344 
1345 	if (svc_state == STMF_STATE_OFFLINE) {
1346 		if (std->config_state == STMF_CONFIG_INIT) {
1347 			if (std->state != STMF_STATE_OFFLINE) {
1348 				mutex_exit(&stmf_state.stmf_lock);
1349 				return (EINVAL);
1350 			}
1351 			stmf_state.stmf_config_state = STMF_CONFIG_INIT;
1352 			stmf_delete_all_ppds();
1353 			stmf_view_clear_config();
1354 			stmf_view_init();
1355 			mutex_exit(&stmf_state.stmf_lock);
1356 			return (0);
1357 		}
1358 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) {
1359 			if (std->config_state != STMF_CONFIG_INIT_DONE) {
1360 				mutex_exit(&stmf_state.stmf_lock);
1361 				return (EINVAL);
1362 			}
1363 			stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE;
1364 		}
1365 		if (std->state == STMF_STATE_OFFLINE) {
1366 			mutex_exit(&stmf_state.stmf_lock);
1367 			return (0);
1368 		}
1369 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) {
1370 			mutex_exit(&stmf_state.stmf_lock);
1371 			return (EINVAL);
1372 		}
1373 		stmf_state.stmf_inventory_locked = 1;
1374 		stmf_state.stmf_service_running = 1;
1375 		mutex_exit(&stmf_state.stmf_lock);
1376 
1377 		for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1378 		    ilport = ilport->ilport_next) {
1379 			if (ilport->ilport_prev_state != STMF_STATE_ONLINE)
1380 				continue;
1381 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE,
1382 			    ilport->ilport_lport, &ssi);
1383 		}
1384 
1385 		for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1386 		    ilu = ilu->ilu_next) {
1387 			if (ilu->ilu_prev_state != STMF_STATE_ONLINE)
1388 				continue;
1389 			(void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi);
1390 		}
1391 		mutex_enter(&stmf_state.stmf_lock);
1392 		stmf_state.stmf_inventory_locked = 0;
1393 		mutex_exit(&stmf_state.stmf_lock);
1394 		return (0);
1395 	}
1396 
1397 	/* svc_state is STMF_STATE_ONLINE here */
1398 	if ((std->state != STMF_STATE_OFFLINE) ||
1399 	    (std->config_state == STMF_CONFIG_INIT)) {
1400 		mutex_exit(&stmf_state.stmf_lock);
1401 		return (EACCES);
1402 	}
1403 
1404 	stmf_state.stmf_inventory_locked = 1;
1405 	stmf_state.stmf_service_running = 0;
1406 	stmf_delete_all_ppds();
1407 	mutex_exit(&stmf_state.stmf_lock);
1408 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1409 	    ilport = ilport->ilport_next) {
1410 		if (ilport->ilport_state != STMF_STATE_ONLINE)
1411 			continue;
1412 		(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE,
1413 		    ilport->ilport_lport, &ssi);
1414 	}
1415 
1416 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1417 	    ilu = ilu->ilu_next) {
1418 		if (ilu->ilu_state != STMF_STATE_ONLINE)
1419 			continue;
1420 		(void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi);
1421 	}
1422 	mutex_enter(&stmf_state.stmf_lock);
1423 	stmf_state.stmf_inventory_locked = 0;
1424 	mutex_exit(&stmf_state.stmf_lock);
1425 	return (0);
1426 }
1427 
1428 static int
1429 stmf_get_stmf_state(stmf_state_desc_t *std)
1430 {
1431 	mutex_enter(&stmf_state.stmf_lock);
1432 	std->state = stmf_get_service_state();
1433 	std->config_state = stmf_state.stmf_config_state;
1434 	mutex_exit(&stmf_state.stmf_lock);
1435 
1436 	return (0);
1437 }
1438 
1439 typedef struct {
1440 	void	*bp;	/* back pointer from internal struct to main struct */
1441 	int	alloc_size;
1442 } __istmf_t;
1443 
1444 typedef struct {
1445 	__istmf_t	*fp;	/* Framework private */
1446 	void		*cp;	/* Caller private */
1447 	void		*ss;	/* struct specific */
1448 } __stmf_t;
1449 
1450 static struct {
1451 	int shared;
1452 	int fw_private;
1453 } stmf_sizes[] = { { 0, 0 },
1454 	{ GET_STRUCT_SIZE(stmf_lu_provider_t),
1455 		GET_STRUCT_SIZE(stmf_i_lu_provider_t) },
1456 	{ GET_STRUCT_SIZE(stmf_port_provider_t),
1457 		GET_STRUCT_SIZE(stmf_i_port_provider_t) },
1458 	{ GET_STRUCT_SIZE(stmf_local_port_t),
1459 		GET_STRUCT_SIZE(stmf_i_local_port_t) },
1460 	{ GET_STRUCT_SIZE(stmf_lu_t),
1461 		GET_STRUCT_SIZE(stmf_i_lu_t) },
1462 	{ GET_STRUCT_SIZE(stmf_scsi_session_t),
1463 		GET_STRUCT_SIZE(stmf_i_scsi_session_t) },
1464 	{ GET_STRUCT_SIZE(scsi_task_t),
1465 		GET_STRUCT_SIZE(stmf_i_scsi_task_t) },
1466 	{ GET_STRUCT_SIZE(stmf_data_buf_t),
1467 		GET_STRUCT_SIZE(__istmf_t) },
1468 	{ GET_STRUCT_SIZE(stmf_dbuf_store_t),
1469 		GET_STRUCT_SIZE(__istmf_t) }
1470 
1471 };
1472 
1473 void *
1474 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags)
1475 {
1476 	int stmf_size;
1477 	int kmem_flag;
1478 	__stmf_t *sh;
1479 
1480 	if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS))
1481 		return (NULL);
1482 
1483 	if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
1484 		kmem_flag = KM_NOSLEEP;
1485 	} else {
1486 		kmem_flag = KM_SLEEP;
1487 	}
1488 
1489 	additional_size = (additional_size + 7) & (~7);
1490 	stmf_size = stmf_sizes[struct_id].shared +
1491 	    stmf_sizes[struct_id].fw_private + additional_size;
1492 
1493 	sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag);
1494 
1495 	if (sh == NULL)
1496 		return (NULL);
1497 
1498 	/*
1499 	 * In principle, the implementation inside stmf_alloc should not
1500 	 * be changed anyway. But the original order of framework private
1501 	 * data and caller private data does not support sglist in the caller
1502 	 * private data.
1503 	 * To work around this, the memory segments of framework private
1504 	 * data and caller private data are re-ordered here.
1505 	 * A better solution is to provide a specific interface to allocate
1506 	 * the sglist, then we will not need this workaround any more.
1507 	 * But before the new interface is available, the memory segment
1508 	 * ordering should be kept as is.
1509 	 */
1510 	sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
1511 	sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh,
1512 	    stmf_sizes[struct_id].shared + additional_size);
1513 
1514 	sh->fp->bp = sh;
1515 	/* Just store the total size instead of storing additional size */
1516 	sh->fp->alloc_size = stmf_size;
1517 
1518 	return (sh);
1519 }
1520 
1521 void
1522 stmf_free(void *ptr)
1523 {
1524 	__stmf_t *sh = (__stmf_t *)ptr;
1525 
1526 	/*
1527 	 * So far we dont need any struct specific processing. If such
1528 	 * a need ever arises, then store the struct id in the framework
1529 	 * private section and get it here as sh->fp->struct_id.
1530 	 */
1531 	kmem_free(ptr, sh->fp->alloc_size);
1532 }
1533 
1534 /*
1535  * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
1536  * framework and returns a pointer to framework private data for the lu.
1537  * Returns NULL if the lu was not found.
1538  */
1539 stmf_i_lu_t *
1540 stmf_lookup_lu(stmf_lu_t *lu)
1541 {
1542 	stmf_i_lu_t *ilu;
1543 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1544 
1545 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
1546 		if (ilu->ilu_lu == lu)
1547 			return (ilu);
1548 	}
1549 	return (NULL);
1550 }
1551 
1552 /*
1553  * Given a pointer to stmf_local_port_t, verifies if this lport is registered
1554  * with the framework and returns a pointer to framework private data for
1555  * the lport.
1556  * Returns NULL if the lport was not found.
1557  */
1558 stmf_i_local_port_t *
1559 stmf_lookup_lport(stmf_local_port_t *lport)
1560 {
1561 	stmf_i_local_port_t *ilport;
1562 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1563 
1564 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1565 	    ilport = ilport->ilport_next) {
1566 		if (ilport->ilport_lport == lport)
1567 			return (ilport);
1568 	}
1569 	return (NULL);
1570 }
1571 
1572 stmf_status_t
1573 stmf_register_lu_provider(stmf_lu_provider_t *lp)
1574 {
1575 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
1576 	stmf_pp_data_t *ppd;
1577 	uint32_t cb_flags;
1578 
1579 	if (lp->lp_lpif_rev != LPIF_REV_1)
1580 		return (STMF_FAILURE);
1581 
1582 	mutex_enter(&stmf_state.stmf_lock);
1583 	ilp->ilp_next = stmf_state.stmf_ilplist;
1584 	stmf_state.stmf_ilplist = ilp;
1585 	stmf_state.stmf_nlps++;
1586 
1587 	/* See if we need to do a callback */
1588 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1589 		if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
1590 			break;
1591 		}
1592 	}
1593 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
1594 		goto rlp_bail_out;
1595 	}
1596 	ilp->ilp_ppd = ppd;
1597 	ppd->ppd_provider = ilp;
1598 	if (lp->lp_cb == NULL)
1599 		goto rlp_bail_out;
1600 	ilp->ilp_cb_in_progress = 1;
1601 	cb_flags = STMF_PCB_PREG_COMPLETE;
1602 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1603 		cb_flags |= STMF_PCB_STMF_ONLINING;
1604 	mutex_exit(&stmf_state.stmf_lock);
1605 	lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1606 	mutex_enter(&stmf_state.stmf_lock);
1607 	ilp->ilp_cb_in_progress = 0;
1608 
1609 rlp_bail_out:
1610 	mutex_exit(&stmf_state.stmf_lock);
1611 
1612 	return (STMF_SUCCESS);
1613 }
1614 
1615 stmf_status_t
1616 stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
1617 {
1618 	stmf_i_lu_provider_t	**ppilp;
1619 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
1620 
1621 	mutex_enter(&stmf_state.stmf_lock);
1622 	if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
1623 		mutex_exit(&stmf_state.stmf_lock);
1624 		return (STMF_BUSY);
1625 	}
1626 	for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
1627 	    ppilp = &((*ppilp)->ilp_next)) {
1628 		if (*ppilp == ilp) {
1629 			*ppilp = ilp->ilp_next;
1630 			stmf_state.stmf_nlps--;
1631 			if (ilp->ilp_ppd) {
1632 				ilp->ilp_ppd->ppd_provider = NULL;
1633 				ilp->ilp_ppd = NULL;
1634 			}
1635 			mutex_exit(&stmf_state.stmf_lock);
1636 			return (STMF_SUCCESS);
1637 		}
1638 	}
1639 	mutex_exit(&stmf_state.stmf_lock);
1640 	return (STMF_NOT_FOUND);
1641 }
1642 
1643 stmf_status_t
1644 stmf_register_port_provider(stmf_port_provider_t *pp)
1645 {
1646 	stmf_i_port_provider_t *ipp =
1647 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
1648 	stmf_pp_data_t *ppd;
1649 	uint32_t cb_flags;
1650 
1651 	if (pp->pp_portif_rev != PORTIF_REV_1)
1652 		return (STMF_FAILURE);
1653 
1654 	mutex_enter(&stmf_state.stmf_lock);
1655 	ipp->ipp_next = stmf_state.stmf_ipplist;
1656 	stmf_state.stmf_ipplist = ipp;
1657 	stmf_state.stmf_npps++;
1658 	/* See if we need to do a callback */
1659 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1660 		if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
1661 			break;
1662 		}
1663 	}
1664 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
1665 		goto rpp_bail_out;
1666 	}
1667 	ipp->ipp_ppd = ppd;
1668 	ppd->ppd_provider = ipp;
1669 	if (pp->pp_cb == NULL)
1670 		goto rpp_bail_out;
1671 	ipp->ipp_cb_in_progress = 1;
1672 	cb_flags = STMF_PCB_PREG_COMPLETE;
1673 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1674 		cb_flags |= STMF_PCB_STMF_ONLINING;
1675 	mutex_exit(&stmf_state.stmf_lock);
1676 	pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1677 	mutex_enter(&stmf_state.stmf_lock);
1678 	ipp->ipp_cb_in_progress = 0;
1679 
1680 rpp_bail_out:
1681 	mutex_exit(&stmf_state.stmf_lock);
1682 
1683 	return (STMF_SUCCESS);
1684 }
1685 
1686 stmf_status_t
1687 stmf_deregister_port_provider(stmf_port_provider_t *pp)
1688 {
1689 	stmf_i_port_provider_t *ipp =
1690 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
1691 	stmf_i_port_provider_t **ppipp;
1692 
1693 	mutex_enter(&stmf_state.stmf_lock);
1694 	if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
1695 		mutex_exit(&stmf_state.stmf_lock);
1696 		return (STMF_BUSY);
1697 	}
1698 	for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
1699 	    ppipp = &((*ppipp)->ipp_next)) {
1700 		if (*ppipp == ipp) {
1701 			*ppipp = ipp->ipp_next;
1702 			stmf_state.stmf_npps--;
1703 			if (ipp->ipp_ppd) {
1704 				ipp->ipp_ppd->ppd_provider = NULL;
1705 				ipp->ipp_ppd = NULL;
1706 			}
1707 			mutex_exit(&stmf_state.stmf_lock);
1708 			return (STMF_SUCCESS);
1709 		}
1710 	}
1711 	mutex_exit(&stmf_state.stmf_lock);
1712 	return (STMF_NOT_FOUND);
1713 }
1714 
1715 int
1716 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
1717     uint32_t *err_ret)
1718 {
1719 	stmf_i_port_provider_t		*ipp;
1720 	stmf_i_lu_provider_t		*ilp;
1721 	stmf_pp_data_t			*ppd;
1722 	nvlist_t			*nv;
1723 	int				s;
1724 	int				ret;
1725 
1726 	*err_ret = 0;
1727 
1728 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
1729 		return (EINVAL);
1730 	}
1731 
1732 	mutex_enter(&stmf_state.stmf_lock);
1733 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1734 		if (ppi->ppi_lu_provider) {
1735 			if (!ppd->ppd_lu_provider)
1736 				continue;
1737 		} else if (ppi->ppi_port_provider) {
1738 			if (!ppd->ppd_port_provider)
1739 				continue;
1740 		}
1741 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
1742 			break;
1743 	}
1744 
1745 	if (ppd == NULL) {
1746 		/* New provider */
1747 		s = strlen(ppi->ppi_name);
1748 		if (s > 254) {
1749 			mutex_exit(&stmf_state.stmf_lock);
1750 			return (EINVAL);
1751 		}
1752 		s += sizeof (stmf_pp_data_t) - 7;
1753 
1754 		ppd = kmem_zalloc(s, KM_NOSLEEP);
1755 		if (ppd == NULL) {
1756 			mutex_exit(&stmf_state.stmf_lock);
1757 			return (ENOMEM);
1758 		}
1759 		ppd->ppd_alloc_size = s;
1760 		(void) strcpy(ppd->ppd_name, ppi->ppi_name);
1761 
1762 		/* See if this provider already exists */
1763 		if (ppi->ppi_lu_provider) {
1764 			ppd->ppd_lu_provider = 1;
1765 			for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
1766 			    ilp = ilp->ilp_next) {
1767 				if (strcmp(ppi->ppi_name,
1768 				    ilp->ilp_lp->lp_name) == 0) {
1769 					ppd->ppd_provider = ilp;
1770 					ilp->ilp_ppd = ppd;
1771 					break;
1772 				}
1773 			}
1774 		} else {
1775 			ppd->ppd_port_provider = 1;
1776 			for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
1777 			    ipp = ipp->ipp_next) {
1778 				if (strcmp(ppi->ppi_name,
1779 				    ipp->ipp_pp->pp_name) == 0) {
1780 					ppd->ppd_provider = ipp;
1781 					ipp->ipp_ppd = ppd;
1782 					break;
1783 				}
1784 			}
1785 		}
1786 
1787 		/* Link this ppd in */
1788 		ppd->ppd_next = stmf_state.stmf_ppdlist;
1789 		stmf_state.stmf_ppdlist = ppd;
1790 	}
1791 
1792 	/*
1793 	 * User is requesting that the token be checked.
1794 	 * If there was another set after the user's get
1795 	 * it's an error
1796 	 */
1797 	if (ppi->ppi_token_valid) {
1798 		if (ppi->ppi_token != ppd->ppd_token) {
1799 			*err_ret = STMF_IOCERR_PPD_UPDATED;
1800 			mutex_exit(&stmf_state.stmf_lock);
1801 			return (EINVAL);
1802 		}
1803 	}
1804 
1805 	if ((ret = nvlist_unpack((char *)ppi->ppi_data,
1806 	    (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
1807 		mutex_exit(&stmf_state.stmf_lock);
1808 		return (ret);
1809 	}
1810 
1811 	/* Free any existing lists and add this one to the ppd */
1812 	if (ppd->ppd_nv)
1813 		nvlist_free(ppd->ppd_nv);
1814 	ppd->ppd_nv = nv;
1815 
1816 	/* set the token for writes */
1817 	ppd->ppd_token++;
1818 	/* return token to caller */
1819 	if (ppi_token) {
1820 		*ppi_token = ppd->ppd_token;
1821 	}
1822 
1823 	/* If there is a provider registered, do the notifications */
1824 	if (ppd->ppd_provider) {
1825 		uint32_t cb_flags = 0;
1826 
1827 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1828 			cb_flags |= STMF_PCB_STMF_ONLINING;
1829 		if (ppi->ppi_lu_provider) {
1830 			ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
1831 			if (ilp->ilp_lp->lp_cb == NULL)
1832 				goto bail_out;
1833 			ilp->ilp_cb_in_progress = 1;
1834 			mutex_exit(&stmf_state.stmf_lock);
1835 			ilp->ilp_lp->lp_cb(ilp->ilp_lp,
1836 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1837 			mutex_enter(&stmf_state.stmf_lock);
1838 			ilp->ilp_cb_in_progress = 0;
1839 		} else {
1840 			ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
1841 			if (ipp->ipp_pp->pp_cb == NULL)
1842 				goto bail_out;
1843 			ipp->ipp_cb_in_progress = 1;
1844 			mutex_exit(&stmf_state.stmf_lock);
1845 			ipp->ipp_pp->pp_cb(ipp->ipp_pp,
1846 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1847 			mutex_enter(&stmf_state.stmf_lock);
1848 			ipp->ipp_cb_in_progress = 0;
1849 		}
1850 	}
1851 
1852 bail_out:
1853 	mutex_exit(&stmf_state.stmf_lock);
1854 
1855 	return (0);
1856 }
1857 
1858 void
1859 stmf_delete_ppd(stmf_pp_data_t *ppd)
1860 {
1861 	stmf_pp_data_t **pppd;
1862 
1863 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1864 	if (ppd->ppd_provider) {
1865 		if (ppd->ppd_lu_provider) {
1866 			((stmf_i_lu_provider_t *)
1867 			    ppd->ppd_provider)->ilp_ppd = NULL;
1868 		} else {
1869 			((stmf_i_port_provider_t *)
1870 			    ppd->ppd_provider)->ipp_ppd = NULL;
1871 		}
1872 		ppd->ppd_provider = NULL;
1873 	}
1874 
1875 	for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
1876 	    pppd = &((*pppd)->ppd_next)) {
1877 		if (*pppd == ppd)
1878 			break;
1879 	}
1880 
1881 	if (*pppd == NULL)
1882 		return;
1883 
1884 	*pppd = ppd->ppd_next;
1885 	if (ppd->ppd_nv)
1886 		nvlist_free(ppd->ppd_nv);
1887 
1888 	kmem_free(ppd, ppd->ppd_alloc_size);
1889 }
1890 
1891 int
1892 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
1893 {
1894 	stmf_pp_data_t *ppd;
1895 	int ret = ENOENT;
1896 
1897 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
1898 		return (EINVAL);
1899 	}
1900 
1901 	mutex_enter(&stmf_state.stmf_lock);
1902 
1903 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1904 		if (ppi->ppi_lu_provider) {
1905 			if (!ppd->ppd_lu_provider)
1906 				continue;
1907 		} else if (ppi->ppi_port_provider) {
1908 			if (!ppd->ppd_port_provider)
1909 				continue;
1910 		}
1911 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
1912 			break;
1913 	}
1914 
1915 	if (ppd) {
1916 		ret = 0;
1917 		stmf_delete_ppd(ppd);
1918 	}
1919 	mutex_exit(&stmf_state.stmf_lock);
1920 
1921 	return (ret);
1922 }
1923 
1924 int
1925 stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
1926     uint32_t *err_ret)
1927 {
1928 	stmf_pp_data_t *ppd;
1929 	size_t req_size;
1930 	int ret = ENOENT;
1931 	char *bufp = (char *)ppi_out->ppi_data;
1932 
1933 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
1934 		return (EINVAL);
1935 	}
1936 
1937 	mutex_enter(&stmf_state.stmf_lock);
1938 
1939 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1940 		if (ppi->ppi_lu_provider) {
1941 			if (!ppd->ppd_lu_provider)
1942 				continue;
1943 		} else if (ppi->ppi_port_provider) {
1944 			if (!ppd->ppd_port_provider)
1945 				continue;
1946 		}
1947 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
1948 			break;
1949 	}
1950 
1951 	if (ppd && ppd->ppd_nv) {
1952 		ppi_out->ppi_token = ppd->ppd_token;
1953 		if ((ret = nvlist_size(ppd->ppd_nv, &req_size,
1954 		    NV_ENCODE_XDR)) != 0) {
1955 			goto done;
1956 		}
1957 		ppi_out->ppi_data_size = req_size;
1958 		if (req_size > ppi->ppi_data_size) {
1959 			*err_ret = STMF_IOCERR_INSUFFICIENT_BUF;
1960 			ret = EINVAL;
1961 			goto done;
1962 		}
1963 
1964 		if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size,
1965 		    NV_ENCODE_XDR, 0)) != 0) {
1966 			goto done;
1967 		}
1968 		ret = 0;
1969 	}
1970 
1971 done:
1972 	mutex_exit(&stmf_state.stmf_lock);
1973 
1974 	return (ret);
1975 }
1976 
1977 void
1978 stmf_delete_all_ppds()
1979 {
1980 	stmf_pp_data_t *ppd, *nppd;
1981 
1982 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1983 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
1984 		nppd = ppd->ppd_next;
1985 		stmf_delete_ppd(ppd);
1986 	}
1987 }
1988 
1989 /*
1990  * 16 is the max string length of a protocol_ident, increase
1991  * the size if needed.
1992  */
1993 #define	STMF_KSTAT_LU_SZ	(STMF_GUID_INPUT + 1 + 256)
1994 #define	STMF_KSTAT_TGT_SZ	(256 * 2 + 16)
1995 
1996 typedef struct stmf_kstat_lu_info {
1997 	kstat_named_t		i_lun_guid;
1998 	kstat_named_t		i_lun_alias;
1999 } stmf_kstat_lu_info_t;
2000 
2001 typedef struct stmf_kstat_tgt_info {
2002 	kstat_named_t		i_tgt_name;
2003 	kstat_named_t		i_tgt_alias;
2004 	kstat_named_t		i_protocol;
2005 } stmf_kstat_tgt_info_t;
2006 
2007 /*
2008  * This array matches the Protocol Identifier in stmf_ioctl.h
2009  */
2010 char *protocol_ident[PROTOCOL_ANY] = {
2011 	"Fibre Channel",
2012 	"Parallel SCSI",
2013 	"SSA",
2014 	"IEEE_1394",
2015 	"SRP",
2016 	"iSCSI",
2017 	"SAS",
2018 	"ADT",
2019 	"ATAPI",
2020 	"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"
2021 };
2022 
2023 /*
2024  * Update the lun wait/run queue count
2025  */
2026 static void
2027 stmf_update_kstat_lu_q(scsi_task_t *task, void func())
2028 {
2029 	stmf_i_lu_t		*ilu;
2030 	kstat_io_t		*kip;
2031 
2032 	if (task->task_lu == dlun0)
2033 		return;
2034 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2035 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2036 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2037 		if (kip != NULL) {
2038 			mutex_enter(ilu->ilu_kstat_io->ks_lock);
2039 			func(kip);
2040 			mutex_exit(ilu->ilu_kstat_io->ks_lock);
2041 		}
2042 	}
2043 }
2044 
2045 /*
2046  * Update the target(lport) wait/run queue count
2047  */
2048 static void
2049 stmf_update_kstat_lport_q(scsi_task_t *task, void func())
2050 {
2051 	stmf_i_local_port_t	*ilp;
2052 	kstat_io_t		*kip;
2053 
2054 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2055 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2056 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2057 		if (kip != NULL) {
2058 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2059 			func(kip);
2060 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2061 		}
2062 	}
2063 }
2064 
2065 static void
2066 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2067 {
2068 	stmf_i_local_port_t	*ilp;
2069 	kstat_io_t		*kip;
2070 
2071 	ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2072 	if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2073 		kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2074 		if (kip != NULL) {
2075 			mutex_enter(ilp->ilport_kstat_io->ks_lock);
2076 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2077 			mutex_exit(ilp->ilport_kstat_io->ks_lock);
2078 		}
2079 	}
2080 }
2081 
2082 static void
2083 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2084 {
2085 	stmf_i_lu_t		*ilu;
2086 	kstat_io_t		*kip;
2087 
2088 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2089 	if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2090 		kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2091 		if (kip != NULL) {
2092 			mutex_enter(ilu->ilu_kstat_io->ks_lock);
2093 			STMF_UPDATE_KSTAT_IO(kip, dbuf);
2094 			mutex_exit(ilu->ilu_kstat_io->ks_lock);
2095 		}
2096 	}
2097 }
2098 
2099 static void
2100 stmf_create_kstat_lu(stmf_i_lu_t *ilu)
2101 {
2102 	char				ks_nm[KSTAT_STRLEN];
2103 	stmf_kstat_lu_info_t		*ks_lu;
2104 
2105 	/* create kstat lun info */
2106 	ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ,
2107 	    KM_NOSLEEP);
2108 	if (ks_lu == NULL) {
2109 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2110 		return;
2111 	}
2112 
2113 	bzero(ks_nm, sizeof (ks_nm));
2114 	(void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu);
2115 	if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0,
2116 	    ks_nm, "misc", KSTAT_TYPE_NAMED,
2117 	    sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t),
2118 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2119 		kmem_free(ks_lu, STMF_KSTAT_LU_SZ);
2120 		cmn_err(CE_WARN, "STMF: kstat_create lu failed");
2121 		return;
2122 	}
2123 
2124 	ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ;
2125 	ilu->ilu_kstat_info->ks_data = ks_lu;
2126 
2127 	kstat_named_init(&ks_lu->i_lun_guid, "lun-guid",
2128 	    KSTAT_DATA_STRING);
2129 	kstat_named_init(&ks_lu->i_lun_alias, "lun-alias",
2130 	    KSTAT_DATA_STRING);
2131 
2132 	/* convert guid to hex string */
2133 	int		i;
2134 	uint8_t		*p = ilu->ilu_lu->lu_id->ident;
2135 	bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid));
2136 	for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
2137 		(void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]);
2138 	}
2139 	kstat_named_setstr(&ks_lu->i_lun_guid,
2140 	    (const char *)ilu->ilu_ascii_hex_guid);
2141 	kstat_named_setstr(&ks_lu->i_lun_alias,
2142 	    (const char *)ilu->ilu_lu->lu_alias);
2143 	kstat_install(ilu->ilu_kstat_info);
2144 
2145 	/* create kstat lun io */
2146 	bzero(ks_nm, sizeof (ks_nm));
2147 	(void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu);
2148 	if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2149 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2150 		cmn_err(CE_WARN, "STMF: kstat_create lu_io failed");
2151 		return;
2152 	}
2153 	mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0);
2154 	ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock;
2155 	kstat_install(ilu->ilu_kstat_io);
2156 }
2157 
2158 static void
2159 stmf_create_kstat_lport(stmf_i_local_port_t *ilport)
2160 {
2161 	char				ks_nm[KSTAT_STRLEN];
2162 	stmf_kstat_tgt_info_t		*ks_tgt;
2163 	int				id, len;
2164 
2165 	/* create kstat lport info */
2166 	ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ,
2167 	    KM_NOSLEEP);
2168 	if (ks_tgt == NULL) {
2169 		cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2170 		return;
2171 	}
2172 
2173 	bzero(ks_nm, sizeof (ks_nm));
2174 	(void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport);
2175 	if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME,
2176 	    0, ks_nm, "misc", KSTAT_TYPE_NAMED,
2177 	    sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t),
2178 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
2179 		kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ);
2180 		cmn_err(CE_WARN, "STMF: kstat_create target failed");
2181 		return;
2182 	}
2183 
2184 	ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ;
2185 	ilport->ilport_kstat_info->ks_data = ks_tgt;
2186 
2187 	kstat_named_init(&ks_tgt->i_tgt_name, "target-name",
2188 	    KSTAT_DATA_STRING);
2189 	kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias",
2190 	    KSTAT_DATA_STRING);
2191 	kstat_named_init(&ks_tgt->i_protocol, "protocol",
2192 	    KSTAT_DATA_STRING);
2193 
2194 	/* ident might not be null terminated */
2195 	len = ilport->ilport_lport->lport_id->ident_length;
2196 	bcopy(ilport->ilport_lport->lport_id->ident,
2197 	    ilport->ilport_kstat_tgt_name, len);
2198 	ilport->ilport_kstat_tgt_name[len + 1] = NULL;
2199 	kstat_named_setstr(&ks_tgt->i_tgt_name,
2200 	    (const char *)ilport->ilport_kstat_tgt_name);
2201 	kstat_named_setstr(&ks_tgt->i_tgt_alias,
2202 	    (const char *)ilport->ilport_lport->lport_alias);
2203 	/* protocol */
2204 	if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) {
2205 		cmn_err(CE_WARN, "STMF: protocol_id out of bound");
2206 		id = PROTOCOL_ANY;
2207 	}
2208 	kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]);
2209 	kstat_install(ilport->ilport_kstat_info);
2210 
2211 	/* create kstat lport io */
2212 	bzero(ks_nm, sizeof (ks_nm));
2213 	(void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport);
2214 	if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2215 	    ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2216 		cmn_err(CE_WARN, "STMF: kstat_create target_io failed");
2217 		return;
2218 	}
2219 	mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0);
2220 	ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock;
2221 	kstat_install(ilport->ilport_kstat_io);
2222 }
2223 
2224 stmf_status_t
2225 stmf_register_lu(stmf_lu_t *lu)
2226 {
2227 	stmf_i_lu_t *ilu;
2228 	uint8_t *p1, *p2;
2229 	stmf_state_change_info_t ssci;
2230 	stmf_id_data_t *luid;
2231 
2232 	if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
2233 	    (lu->lu_id->ident_length != 16) ||
2234 	    ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
2235 		return (STMF_INVALID_ARG);
2236 	}
2237 	p1 = &lu->lu_id->ident[0];
2238 	mutex_enter(&stmf_state.stmf_lock);
2239 	if (stmf_state.stmf_inventory_locked) {
2240 		mutex_exit(&stmf_state.stmf_lock);
2241 		return (STMF_BUSY);
2242 	}
2243 
2244 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2245 		p2 = &ilu->ilu_lu->lu_id->ident[0];
2246 		if (bcmp(p1, p2, 16) == 0) {
2247 			mutex_exit(&stmf_state.stmf_lock);
2248 			return (STMF_ALREADY);
2249 		}
2250 	}
2251 
2252 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2253 	luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
2254 	    lu->lu_id->ident_length, lu->lu_id->ident);
2255 	if (luid) {
2256 		luid->id_pt_to_object = (void *)ilu;
2257 		ilu->ilu_luid = luid;
2258 	}
2259 	ilu->ilu_alias = NULL;
2260 
2261 	ilu->ilu_next = stmf_state.stmf_ilulist;
2262 	ilu->ilu_prev = NULL;
2263 	if (ilu->ilu_next)
2264 		ilu->ilu_next->ilu_prev = ilu;
2265 	stmf_state.stmf_ilulist = ilu;
2266 	stmf_state.stmf_nlus++;
2267 	if (lu->lu_lp) {
2268 		((stmf_i_lu_provider_t *)
2269 		    (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
2270 	}
2271 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
2272 	STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
2273 	stmf_create_kstat_lu(ilu);
2274 	mutex_exit(&stmf_state.stmf_lock);
2275 
2276 	/* XXX we should probably check if this lu can be brought online */
2277 	ilu->ilu_prev_state = STMF_STATE_ONLINE;
2278 	if (stmf_state.stmf_service_running) {
2279 		ssci.st_rflags = 0;
2280 		ssci.st_additional_info = NULL;
2281 		(void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
2282 	}
2283 
2284 	/* XXX: Generate event */
2285 	return (STMF_SUCCESS);
2286 }
2287 
2288 stmf_status_t
2289 stmf_deregister_lu(stmf_lu_t *lu)
2290 {
2291 	stmf_i_lu_t *ilu;
2292 
2293 	mutex_enter(&stmf_state.stmf_lock);
2294 	if (stmf_state.stmf_inventory_locked) {
2295 		mutex_exit(&stmf_state.stmf_lock);
2296 		return (STMF_BUSY);
2297 	}
2298 	ilu = stmf_lookup_lu(lu);
2299 	if (ilu == NULL) {
2300 		mutex_exit(&stmf_state.stmf_lock);
2301 		return (STMF_INVALID_ARG);
2302 	}
2303 	if (ilu->ilu_state == STMF_STATE_OFFLINE) {
2304 		ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
2305 		while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
2306 			cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
2307 		}
2308 		if (ilu->ilu_ntasks) {
2309 			stmf_i_scsi_task_t *itask, *nitask;
2310 
2311 			nitask = ilu->ilu_tasks;
2312 			do {
2313 				itask = nitask;
2314 				nitask = itask->itask_lu_next;
2315 				lu->lu_task_free(itask->itask_task);
2316 				stmf_free(itask->itask_task);
2317 			} while (nitask != NULL);
2318 
2319 			ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
2320 			ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
2321 		}
2322 
2323 		if (ilu->ilu_next)
2324 			ilu->ilu_next->ilu_prev = ilu->ilu_prev;
2325 		if (ilu->ilu_prev)
2326 			ilu->ilu_prev->ilu_next = ilu->ilu_next;
2327 		else
2328 			stmf_state.stmf_ilulist = ilu->ilu_next;
2329 		stmf_state.stmf_nlus--;
2330 
2331 		if (ilu == stmf_state.stmf_svc_ilu_draining) {
2332 			stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
2333 		}
2334 		if (ilu == stmf_state.stmf_svc_ilu_timing) {
2335 			stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
2336 		}
2337 		if (lu->lu_lp) {
2338 			((stmf_i_lu_provider_t *)
2339 			    (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
2340 		}
2341 		if (ilu->ilu_luid) {
2342 			((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
2343 			    NULL;
2344 			ilu->ilu_luid = NULL;
2345 		}
2346 		STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
2347 	} else {
2348 		mutex_exit(&stmf_state.stmf_lock);
2349 		return (STMF_BUSY);
2350 	}
2351 	if (ilu->ilu_kstat_info) {
2352 		kstat_delete(ilu->ilu_kstat_info);
2353 	}
2354 	if (ilu->ilu_kstat_io) {
2355 		kstat_delete(ilu->ilu_kstat_io);
2356 		mutex_destroy(&ilu->ilu_kstat_lock);
2357 	}
2358 	mutex_exit(&stmf_state.stmf_lock);
2359 	return (STMF_SUCCESS);
2360 }
2361 
2362 stmf_status_t
2363 stmf_register_local_port(stmf_local_port_t *lport)
2364 {
2365 	stmf_i_local_port_t *ilport;
2366 	stmf_state_change_info_t ssci;
2367 	int start_workers = 0;
2368 
2369 	mutex_enter(&stmf_state.stmf_lock);
2370 	if (stmf_state.stmf_inventory_locked) {
2371 		mutex_exit(&stmf_state.stmf_lock);
2372 		return (STMF_BUSY);
2373 	}
2374 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
2375 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
2376 
2377 	ilport->ilport_next = stmf_state.stmf_ilportlist;
2378 	ilport->ilport_prev = NULL;
2379 	if (ilport->ilport_next)
2380 		ilport->ilport_next->ilport_prev = ilport;
2381 	stmf_state.stmf_ilportlist = ilport;
2382 	stmf_state.stmf_nlports++;
2383 	if (lport->lport_pp) {
2384 		((stmf_i_port_provider_t *)
2385 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
2386 	}
2387 	ilport->ilport_tg =
2388 	    stmf_lookup_group_for_target(lport->lport_id->ident,
2389 	    lport->lport_id->ident_length);
2390 	ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1);
2391 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
2392 	stmf_create_kstat_lport(ilport);
2393 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
2394 		stmf_workers_state = STMF_WORKERS_ENABLING;
2395 		start_workers = 1;
2396 	}
2397 	mutex_exit(&stmf_state.stmf_lock);
2398 
2399 	if (start_workers)
2400 		stmf_worker_init();
2401 
2402 	/* XXX we should probably check if this lport can be brought online */
2403 	ilport->ilport_prev_state = STMF_STATE_ONLINE;
2404 	if (stmf_state.stmf_service_running) {
2405 		ssci.st_rflags = 0;
2406 		ssci.st_additional_info = NULL;
2407 		(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
2408 	}
2409 
2410 	/* XXX: Generate event */
2411 	return (STMF_SUCCESS);
2412 }
2413 
2414 stmf_status_t
2415 stmf_deregister_local_port(stmf_local_port_t *lport)
2416 {
2417 	stmf_i_local_port_t *ilport;
2418 
2419 	mutex_enter(&stmf_state.stmf_lock);
2420 	if (stmf_state.stmf_inventory_locked) {
2421 		mutex_exit(&stmf_state.stmf_lock);
2422 		return (STMF_BUSY);
2423 	}
2424 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
2425 	if (ilport->ilport_nsessions == 0) {
2426 		if (ilport->ilport_next)
2427 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
2428 		if (ilport->ilport_prev)
2429 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
2430 		else
2431 			stmf_state.stmf_ilportlist = ilport->ilport_next;
2432 		rw_destroy(&ilport->ilport_lock);
2433 		stmf_state.stmf_nlports--;
2434 		if (lport->lport_pp) {
2435 			((stmf_i_port_provider_t *)
2436 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
2437 		}
2438 		ilport->ilport_tg = NULL;
2439 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
2440 	} else {
2441 		mutex_exit(&stmf_state.stmf_lock);
2442 		return (STMF_BUSY);
2443 	}
2444 	if (ilport->ilport_kstat_info) {
2445 		kstat_delete(ilport->ilport_kstat_info);
2446 	}
2447 	if (ilport->ilport_kstat_io) {
2448 		kstat_delete(ilport->ilport_kstat_io);
2449 		mutex_destroy(&ilport->ilport_kstat_lock);
2450 	}
2451 	mutex_exit(&stmf_state.stmf_lock);
2452 	return (STMF_SUCCESS);
2453 }
2454 
2455 /*
2456  * Port provider has to make sure that register/deregister session and
2457  * port are serialized calls.
2458  */
2459 stmf_status_t
2460 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
2461 {
2462 	stmf_i_scsi_session_t *iss;
2463 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
2464 	    lport->lport_stmf_private;
2465 	uint8_t		lun[8];
2466 
2467 	/*
2468 	 * Port state has to be online to register a scsi session. It is
2469 	 * possible that we started an offline operation and a new SCSI
2470 	 * session started at the same time (in that case also we are going
2471 	 * to fail the registeration). But any other state is simply
2472 	 * a bad port provider implementation.
2473 	 */
2474 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
2475 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
2476 			stmf_trace(lport->lport_alias, "Port is trying to "
2477 			    "register a session while the state is neither "
2478 			    "online nor offlining");
2479 		}
2480 		return (STMF_FAILURE);
2481 	}
2482 	bzero(lun, 8);
2483 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2484 	iss->iss_flags |= ISS_BEING_CREATED;
2485 
2486 	/* sessions use the ilport_lock. No separate lock is required */
2487 	iss->iss_lockp = &ilport->ilport_lock;
2488 	(void) stmf_session_create_lun_map(ilport, iss);
2489 
2490 	rw_enter(&ilport->ilport_lock, RW_WRITER);
2491 	ilport->ilport_nsessions++;
2492 	iss->iss_next = ilport->ilport_ss_list;
2493 	ilport->ilport_ss_list = iss;
2494 	rw_exit(&ilport->ilport_lock);
2495 
2496 	iss->iss_creation_time = ddi_get_time();
2497 	ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1);
2498 	iss->iss_flags &= ~ISS_BEING_CREATED;
2499 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
2500 	    stmf_scsi_session_t *, ss);
2501 	return (STMF_SUCCESS);
2502 }
2503 
2504 void
2505 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
2506 {
2507 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
2508 	    lport->lport_stmf_private;
2509 	stmf_i_scsi_session_t *iss, **ppss;
2510 	int found = 0;
2511 
2512 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
2513 	    stmf_scsi_session_t *, ss);
2514 
2515 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2516 	if (ss->ss_rport_alias) {
2517 		ss->ss_rport_alias = NULL;
2518 	}
2519 
2520 try_dereg_ss_again:
2521 	mutex_enter(&stmf_state.stmf_lock);
2522 	atomic_and_32(&iss->iss_flags,
2523 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
2524 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
2525 		mutex_exit(&stmf_state.stmf_lock);
2526 		delay(1);
2527 		goto try_dereg_ss_again;
2528 	}
2529 	mutex_exit(&stmf_state.stmf_lock);
2530 	rw_enter(&ilport->ilport_lock, RW_WRITER);
2531 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
2532 	    ppss = &((*ppss)->iss_next)) {
2533 		if (iss == (*ppss)) {
2534 			*ppss = (*ppss)->iss_next;
2535 			found = 1;
2536 			break;
2537 		}
2538 	}
2539 	if (!found) {
2540 		cmn_err(CE_PANIC, "Deregister session called for non existent"
2541 		    " session");
2542 	}
2543 	ilport->ilport_nsessions--;
2544 	rw_exit(&ilport->ilport_lock);
2545 
2546 	(void) stmf_session_destroy_lun_map(ilport, iss);
2547 }
2548 
2549 stmf_i_scsi_session_t *
2550 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
2551 {
2552 	stmf_i_local_port_t *ilport;
2553 	stmf_i_scsi_session_t *iss;
2554 
2555 	mutex_enter(&stmf_state.stmf_lock);
2556 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2557 	    ilport = ilport->ilport_next) {
2558 		rw_enter(&ilport->ilport_lock, RW_WRITER);
2559 		for (iss = ilport->ilport_ss_list; iss != NULL;
2560 		    iss = iss->iss_next) {
2561 			if (iss->iss_ss->ss_session_id == session_id) {
2562 				if (!stay_locked)
2563 					rw_exit(&ilport->ilport_lock);
2564 				mutex_exit(&stmf_state.stmf_lock);
2565 				return (iss);
2566 			}
2567 		}
2568 		rw_exit(&ilport->ilport_lock);
2569 	}
2570 	mutex_exit(&stmf_state.stmf_lock);
2571 	return (NULL);
2572 }
2573 
2574 void
2575 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
2576 {
2577 	stmf_itl_data_t **itlpp;
2578 	stmf_i_lu_t *ilu;
2579 
2580 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
2581 
2582 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2583 	mutex_enter(&ilu->ilu_task_lock);
2584 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
2585 	    itlpp = &(*itlpp)->itl_next) {
2586 		if ((*itlpp) == itl)
2587 			break;
2588 	}
2589 	ASSERT((*itlpp) != NULL);
2590 	*itlpp = itl->itl_next;
2591 	mutex_exit(&ilu->ilu_task_lock);
2592 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
2593 	    (uint32_t)itl->itl_hdlrm_reason);
2594 	kmem_free(itl, sizeof (*itl));
2595 }
2596 
2597 stmf_status_t
2598 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
2599     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
2600 {
2601 	stmf_itl_data_t *itl;
2602 	stmf_i_scsi_session_t *iss;
2603 	stmf_lun_map_ent_t *lun_map_ent;
2604 	stmf_i_lu_t *ilu;
2605 	uint16_t n;
2606 
2607 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2608 	if (ss == NULL) {
2609 		iss = stmf_session_id_to_issptr(session_id, 1);
2610 		if (iss == NULL)
2611 			return (STMF_NOT_FOUND);
2612 	} else {
2613 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2614 		rw_enter(iss->iss_lockp, RW_WRITER);
2615 	}
2616 
2617 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2618 	lun_map_ent = (stmf_lun_map_ent_t *)
2619 	    stmf_get_ent_from_map(iss->iss_sm, n);
2620 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
2621 		rw_exit(iss->iss_lockp);
2622 		return (STMF_NOT_FOUND);
2623 	}
2624 	if (lun_map_ent->ent_itl_datap != NULL) {
2625 		rw_exit(iss->iss_lockp);
2626 		return (STMF_ALREADY);
2627 	}
2628 
2629 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
2630 	if (itl == NULL) {
2631 		rw_exit(iss->iss_lockp);
2632 		return (STMF_ALLOC_FAILURE);
2633 	}
2634 
2635 	itl->itl_counter = 1;
2636 	itl->itl_lun = n;
2637 	itl->itl_handle = itl_handle;
2638 	itl->itl_session = iss;
2639 	mutex_enter(&ilu->ilu_task_lock);
2640 	itl->itl_next = ilu->ilu_itl_list;
2641 	ilu->ilu_itl_list = itl;
2642 	mutex_exit(&ilu->ilu_task_lock);
2643 	lun_map_ent->ent_itl_datap = itl;
2644 	rw_exit(iss->iss_lockp);
2645 
2646 	return (STMF_SUCCESS);
2647 }
2648 
2649 void
2650 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
2651 {
2652 	uint8_t old, new;
2653 
2654 	do {
2655 		old = new = itl->itl_flags;
2656 		if (old & STMF_ITL_BEING_TERMINATED)
2657 			return;
2658 		new |= STMF_ITL_BEING_TERMINATED;
2659 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
2660 	itl->itl_hdlrm_reason = hdlrm_reason;
2661 
2662 	ASSERT(itl->itl_counter);
2663 
2664 	if (atomic_add_32_nv(&itl->itl_counter, -1))
2665 		return;
2666 
2667 	drv_usecwait(10);
2668 	if (itl->itl_counter)
2669 		return;
2670 
2671 	stmf_release_itl_handle(lu, itl);
2672 }
2673 
2674 stmf_status_t
2675 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
2676 {
2677 	stmf_i_lu_t *ilu;
2678 	stmf_i_local_port_t *ilport;
2679 	stmf_i_scsi_session_t *iss;
2680 	stmf_lun_map_t *lm;
2681 	stmf_lun_map_ent_t *ent;
2682 	uint32_t nmaps, nu;
2683 	stmf_itl_data_t **itl_list;
2684 	int i;
2685 
2686 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2687 
2688 dereg_itl_start:;
2689 	nmaps = ilu->ilu_ref_cnt;
2690 	if (nmaps == 0)
2691 		return (STMF_NOT_FOUND);
2692 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
2693 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
2694 	mutex_enter(&stmf_state.stmf_lock);
2695 	if (nmaps != ilu->ilu_ref_cnt) {
2696 		/* Something changed, start all over */
2697 		mutex_exit(&stmf_state.stmf_lock);
2698 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
2699 		goto dereg_itl_start;
2700 	}
2701 	nu = 0;
2702 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2703 	    ilport = ilport->ilport_next) {
2704 		rw_enter(&ilport->ilport_lock, RW_WRITER);
2705 		for (iss = ilport->ilport_ss_list; iss != NULL;
2706 		    iss = iss->iss_next) {
2707 			lm = iss->iss_sm;
2708 			if (!lm)
2709 				continue;
2710 			for (i = 0; i < lm->lm_nentries; i++) {
2711 				if (lm->lm_plus[i] == NULL)
2712 					continue;
2713 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2714 				if ((ent->ent_lu == lu) &&
2715 				    (ent->ent_itl_datap)) {
2716 					itl_list[nu++] = ent->ent_itl_datap;
2717 					ent->ent_itl_datap = NULL;
2718 					if (nu == nmaps) {
2719 						rw_exit(&ilport->ilport_lock);
2720 						goto dai_scan_done;
2721 					}
2722 				}
2723 			} /* lun table for a session */
2724 		} /* sessions */
2725 		rw_exit(&ilport->ilport_lock);
2726 	} /* ports */
2727 
2728 dai_scan_done:
2729 	mutex_exit(&stmf_state.stmf_lock);
2730 
2731 	for (i = 0; i < nu; i++) {
2732 		stmf_do_itl_dereg(lu, itl_list[i],
2733 		    STMF_ITL_REASON_DEREG_REQUEST);
2734 	}
2735 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
2736 
2737 	return (STMF_SUCCESS);
2738 }
2739 
2740 stmf_status_t
2741 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun,
2742     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
2743 {
2744 	stmf_i_scsi_session_t *iss;
2745 	stmf_itl_data_t *itl;
2746 	stmf_lun_map_ent_t *ent;
2747 	stmf_lun_map_t *lm;
2748 	int i;
2749 	uint16_t n;
2750 
2751 	if (ss == NULL) {
2752 		if (session_id == STMF_SESSION_ID_NONE)
2753 			return (STMF_INVALID_ARG);
2754 		iss = stmf_session_id_to_issptr(session_id, 1);
2755 		if (iss == NULL)
2756 			return (STMF_NOT_FOUND);
2757 	} else {
2758 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2759 		rw_enter(iss->iss_lockp, RW_WRITER);
2760 	}
2761 	lm = iss->iss_sm;
2762 	if (lm == NULL) {
2763 		rw_exit(iss->iss_lockp);
2764 		return (STMF_NOT_FOUND);
2765 	}
2766 
2767 	if (lun) {
2768 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2769 		ent = (stmf_lun_map_ent_t *)
2770 		    stmf_get_ent_from_map(iss->iss_sm, n);
2771 	} else {
2772 		if (itl_handle == NULL) {
2773 			rw_exit(iss->iss_lockp);
2774 			return (STMF_INVALID_ARG);
2775 		}
2776 		ent = NULL;
2777 		for (i = 0; i < lm->lm_nentries; i++) {
2778 			if (lm->lm_plus[i] == NULL)
2779 				continue;
2780 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2781 			if (ent->ent_itl_datap &&
2782 			    (ent->ent_itl_datap->itl_handle == itl_handle)) {
2783 				break;
2784 			}
2785 		}
2786 	}
2787 	if ((ent == NULL) || (ent->ent_lu != lu) ||
2788 	    (ent->ent_itl_datap == NULL)) {
2789 		rw_exit(iss->iss_lockp);
2790 		return (STMF_NOT_FOUND);
2791 	}
2792 	itl = ent->ent_itl_datap;
2793 	ent->ent_itl_datap = NULL;
2794 	rw_exit(iss->iss_lockp);
2795 	stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST);
2796 
2797 	return (STMF_SUCCESS);
2798 }
2799 
2800 stmf_status_t
2801 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
2802     uint64_t session_id, void **itl_handle_retp)
2803 {
2804 	stmf_i_scsi_session_t *iss;
2805 	stmf_lun_map_ent_t *ent;
2806 	stmf_lun_map_t *lm;
2807 	stmf_status_t ret;
2808 	int i;
2809 	uint16_t n;
2810 
2811 	if (ss == NULL) {
2812 		iss = stmf_session_id_to_issptr(session_id, 1);
2813 		if (iss == NULL)
2814 			return (STMF_NOT_FOUND);
2815 	} else {
2816 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2817 		rw_enter(iss->iss_lockp, RW_WRITER);
2818 	}
2819 
2820 	ent = NULL;
2821 	if (lun == NULL) {
2822 		lm = iss->iss_sm;
2823 		for (i = 0; i < lm->lm_nentries; i++) {
2824 			if (lm->lm_plus[i] == NULL)
2825 				continue;
2826 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2827 			if (ent->ent_lu == lu)
2828 				break;
2829 		}
2830 	} else {
2831 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2832 		ent = (stmf_lun_map_ent_t *)
2833 		    stmf_get_ent_from_map(iss->iss_sm, n);
2834 		if (lu && (ent->ent_lu != lu))
2835 			ent = NULL;
2836 	}
2837 	if (ent && ent->ent_itl_datap) {
2838 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
2839 		ret = STMF_SUCCESS;
2840 	} else {
2841 		ret = STMF_NOT_FOUND;
2842 	}
2843 
2844 	rw_exit(iss->iss_lockp);
2845 	return (ret);
2846 }
2847 
2848 stmf_data_buf_t *
2849 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
2850     uint32_t flags)
2851 {
2852 	stmf_i_scsi_task_t *itask =
2853 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2854 	stmf_local_port_t *lport = task->task_lport;
2855 	stmf_data_buf_t *dbuf;
2856 	uint8_t ndx;
2857 
2858 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
2859 	if (ndx == 0xff)
2860 		return (NULL);
2861 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
2862 	    task, size, pminsize, flags);
2863 	if (dbuf) {
2864 		task->task_cur_nbufs++;
2865 		itask->itask_allocated_buf_map |= (1 << ndx);
2866 		dbuf->db_handle = ndx;
2867 		return (dbuf);
2868 	}
2869 
2870 	return (NULL);
2871 }
2872 
2873 void
2874 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
2875 {
2876 	stmf_i_scsi_task_t *itask =
2877 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2878 	stmf_local_port_t *lport = task->task_lport;
2879 
2880 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
2881 	task->task_cur_nbufs--;
2882 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
2883 }
2884 
2885 stmf_data_buf_t *
2886 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
2887 {
2888 	stmf_i_scsi_task_t *itask;
2889 
2890 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
2891 	if (h > 3)
2892 		return (NULL);
2893 	return (itask->itask_dbufs[h]);
2894 }
2895 
2896 /* ARGSUSED */
2897 struct scsi_task *
2898 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
2899 			uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
2900 {
2901 	stmf_lu_t *lu;
2902 	stmf_i_scsi_session_t *iss;
2903 	stmf_i_lu_t *ilu;
2904 	stmf_i_scsi_task_t *itask;
2905 	stmf_i_scsi_task_t **ppitask;
2906 	scsi_task_t *task;
2907 	uint64_t *p;
2908 	uint8_t	*l;
2909 	stmf_lun_map_ent_t *lun_map_ent;
2910 	uint16_t cdb_length;
2911 	uint16_t luNbr;
2912 	uint8_t new_task = 0;
2913 
2914 	/*
2915 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
2916 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
2917 	 * depend upon this alignment.
2918 	 */
2919 	if (cdb_length_in >= 16)
2920 		cdb_length = cdb_length_in + 7;
2921 	else
2922 		cdb_length = 16 + 7;
2923 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2924 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2925 	rw_enter(iss->iss_lockp, RW_READER);
2926 	lun_map_ent =
2927 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
2928 	if (!lun_map_ent) {
2929 		lu = dlun0;
2930 	} else {
2931 		lu = lun_map_ent->ent_lu;
2932 	}
2933 	ilu = lu->lu_stmf_private;
2934 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
2935 		rw_exit(iss->iss_lockp);
2936 		return (NULL);
2937 	}
2938 	do {
2939 		if (ilu->ilu_free_tasks == NULL) {
2940 			new_task = 1;
2941 			break;
2942 		}
2943 		mutex_enter(&ilu->ilu_task_lock);
2944 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
2945 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
2946 		    ppitask = &((*ppitask)->itask_lu_free_next))
2947 			;
2948 		if (*ppitask) {
2949 			itask = *ppitask;
2950 			*ppitask = (*ppitask)->itask_lu_free_next;
2951 			ilu->ilu_ntasks_free--;
2952 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
2953 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
2954 		} else {
2955 			new_task = 1;
2956 		}
2957 		mutex_exit(&ilu->ilu_task_lock);
2958 	/* CONSTCOND */
2959 	} while (0);
2960 
2961 	if (!new_task) {
2962 		task = itask->itask_task;
2963 		task->task_timeout = 0;
2964 		p = (uint64_t *)&task->task_flags;
2965 		*p++ = 0; *p++ = 0; p++; p++; *p++ = 0; *p++ = 0; *p = 0;
2966 		itask->itask_ncmds = 0;
2967 	} else {
2968 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
2969 		    cdb_length, AF_FORCE_NOSLEEP);
2970 		if (task == NULL) {
2971 			rw_exit(iss->iss_lockp);
2972 			return (NULL);
2973 		}
2974 		task->task_lu = lu;
2975 		l = task->task_lun_no;
2976 		l[0] = lun[0];
2977 		l[1] = lun[1];
2978 		l[2] = lun[2];
2979 		l[3] = lun[3];
2980 		l[4] = lun[4];
2981 		l[5] = lun[5];
2982 		l[6] = lun[6];
2983 		l[7] = lun[7];
2984 		task->task_cdb = (uint8_t *)task->task_port_private;
2985 		if ((ulong_t)(task->task_cdb) & 7ul) {
2986 			task->task_cdb = (uint8_t *)(((ulong_t)
2987 			    (task->task_cdb) + 7ul) & ~(7ul));
2988 		}
2989 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
2990 		itask->itask_cdb_buf_size = cdb_length;
2991 	}
2992 	task->task_session = ss;
2993 	task->task_lport = lport;
2994 	task->task_cdb_length = cdb_length_in;
2995 	itask->itask_flags = ITASK_IN_TRANSITION;
2996 
2997 	if (new_task) {
2998 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
2999 			rw_exit(iss->iss_lockp);
3000 			stmf_free(task);
3001 			return (NULL);
3002 		}
3003 		mutex_enter(&ilu->ilu_task_lock);
3004 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
3005 			mutex_exit(&ilu->ilu_task_lock);
3006 			rw_exit(iss->iss_lockp);
3007 			stmf_free(task);
3008 			return (NULL);
3009 		}
3010 		itask->itask_lu_next = ilu->ilu_tasks;
3011 		if (ilu->ilu_tasks)
3012 			ilu->ilu_tasks->itask_lu_prev = itask;
3013 		ilu->ilu_tasks = itask;
3014 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
3015 		ilu->ilu_ntasks++;
3016 		mutex_exit(&ilu->ilu_task_lock);
3017 	}
3018 
3019 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
3020 	atomic_add_32(itask->itask_ilu_task_cntr, 1);
3021 	itask->itask_start_time = ddi_get_lbolt();
3022 
3023 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
3024 	    lun_map_ent->ent_itl_datap) != NULL)) {
3025 		atomic_add_32(&itask->itask_itl_datap->itl_counter, 1);
3026 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
3027 	} else {
3028 		itask->itask_itl_datap = NULL;
3029 		task->task_lu_itl_handle = NULL;
3030 	}
3031 
3032 	rw_exit(iss->iss_lockp);
3033 	return (task);
3034 }
3035 
3036 void
3037 stmf_task_lu_free(scsi_task_t *task)
3038 {
3039 	stmf_i_scsi_task_t *itask =
3040 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3041 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
3042 	    task->task_session->ss_stmf_private;
3043 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
3044 
3045 	rw_enter(iss->iss_lockp, RW_READER);
3046 	itask->itask_flags = ITASK_IN_FREE_LIST;
3047 	mutex_enter(&ilu->ilu_task_lock);
3048 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
3049 	ilu->ilu_free_tasks = itask;
3050 	ilu->ilu_ntasks_free++;
3051 	mutex_exit(&ilu->ilu_task_lock);
3052 	atomic_add_32(itask->itask_ilu_task_cntr, -1);
3053 	rw_exit(iss->iss_lockp);
3054 }
3055 
3056 void
3057 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
3058 {
3059 	uint32_t	num_to_release, ndx;
3060 	stmf_i_scsi_task_t *itask;
3061 	stmf_lu_t	*lu = ilu->ilu_lu;
3062 
3063 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
3064 
3065 	/* free half of the minimal free of the free tasks */
3066 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
3067 	if (!num_to_release) {
3068 		return;
3069 	}
3070 	for (ndx = 0; ndx < num_to_release; ndx++) {
3071 		mutex_enter(&ilu->ilu_task_lock);
3072 		itask = ilu->ilu_free_tasks;
3073 		if (itask == NULL) {
3074 			mutex_exit(&ilu->ilu_task_lock);
3075 			break;
3076 		}
3077 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
3078 		ilu->ilu_ntasks_free--;
3079 		mutex_exit(&ilu->ilu_task_lock);
3080 
3081 		lu->lu_task_free(itask->itask_task);
3082 		mutex_enter(&ilu->ilu_task_lock);
3083 		if (itask->itask_lu_next)
3084 			itask->itask_lu_next->itask_lu_prev =
3085 			    itask->itask_lu_prev;
3086 		if (itask->itask_lu_prev)
3087 			itask->itask_lu_prev->itask_lu_next =
3088 			    itask->itask_lu_next;
3089 		else
3090 			ilu->ilu_tasks = itask->itask_lu_next;
3091 
3092 		ilu->ilu_ntasks--;
3093 		mutex_exit(&ilu->ilu_task_lock);
3094 		stmf_free(itask->itask_task);
3095 	}
3096 }
3097 
3098 /*
3099  * Called with stmf_lock held
3100  */
3101 void
3102 stmf_check_freetask()
3103 {
3104 	stmf_i_lu_t *ilu;
3105 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
3106 
3107 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
3108 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
3109 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
3110 		if (!ilu->ilu_ntasks_min_free) {
3111 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
3112 			continue;
3113 		}
3114 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
3115 		mutex_exit(&stmf_state.stmf_lock);
3116 		stmf_task_lu_check_freelist(ilu);
3117 		/*
3118 		 * we do not care about the accuracy of
3119 		 * ilu_ntasks_min_free, so we don't lock here
3120 		 */
3121 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
3122 		mutex_enter(&stmf_state.stmf_lock);
3123 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
3124 		cv_broadcast(&stmf_state.stmf_cv);
3125 		if (ddi_get_lbolt() >= endtime)
3126 			break;
3127 	}
3128 }
3129 
3130 void
3131 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
3132 {
3133 	clock_t l = ddi_get_lbolt();
3134 	clock_t ps = drv_usectohz(1000000);
3135 	stmf_i_scsi_task_t *itask;
3136 	scsi_task_t *task;
3137 	uint32_t to;
3138 
3139 	mutex_enter(&ilu->ilu_task_lock);
3140 	for (itask = ilu->ilu_tasks; itask != NULL;
3141 	    itask = itask->itask_lu_next) {
3142 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
3143 		    ITASK_BEING_ABORTED)) {
3144 			continue;
3145 		}
3146 		task = itask->itask_task;
3147 		if (task->task_timeout == 0)
3148 			to = stmf_default_task_timeout;
3149 		else
3150 			to = task->task_timeout;
3151 		if ((itask->itask_start_time + (to * ps)) > l)
3152 			continue;
3153 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
3154 		    STMF_TIMEOUT, NULL);
3155 	}
3156 	mutex_exit(&ilu->ilu_task_lock);
3157 }
3158 
3159 /*
3160  * Called with stmf_lock held
3161  */
3162 void
3163 stmf_check_ilu_timing()
3164 {
3165 	stmf_i_lu_t *ilu;
3166 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
3167 
3168 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
3169 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
3170 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
3171 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
3172 			if (ilu->ilu_task_cntr2 == 0) {
3173 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
3174 				continue;
3175 			}
3176 		} else {
3177 			if (ilu->ilu_task_cntr1 == 0) {
3178 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
3179 				continue;
3180 			}
3181 		}
3182 		/*
3183 		 * If we are here then it means that there is some slowdown
3184 		 * in tasks on this lu. We need to check.
3185 		 */
3186 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
3187 		mutex_exit(&stmf_state.stmf_lock);
3188 		stmf_do_ilu_timeouts(ilu);
3189 		mutex_enter(&stmf_state.stmf_lock);
3190 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
3191 		cv_broadcast(&stmf_state.stmf_cv);
3192 		if (ddi_get_lbolt() >= endtime)
3193 			break;
3194 	}
3195 }
3196 
3197 /*
3198  * Kills all tasks on a lu except tm_task
3199  */
3200 void
3201 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
3202 {
3203 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3204 	stmf_i_scsi_task_t *itask;
3205 
3206 	mutex_enter(&ilu->ilu_task_lock);
3207 
3208 	for (itask = ilu->ilu_tasks; itask != NULL;
3209 	    itask = itask->itask_lu_next) {
3210 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
3211 			continue;
3212 		if (itask->itask_task == tm_task)
3213 			continue;
3214 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
3215 	}
3216 	mutex_exit(&ilu->ilu_task_lock);
3217 }
3218 
3219 void
3220 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
3221 {
3222 	int i;
3223 	uint8_t map;
3224 
3225 	if ((map = itask->itask_allocated_buf_map) != 0) {
3226 		for (i = 0; i < 4; i++) {
3227 			if (map & 1) {
3228 				stmf_data_buf_t *dbuf;
3229 
3230 				dbuf = itask->itask_dbufs[i];
3231 				if (dbuf->db_lu_private) {
3232 					dbuf->db_lu_private = NULL;
3233 				}
3234 				lport->lport_ds->ds_free_data_buf(
3235 				    lport->lport_ds, dbuf);
3236 			}
3237 			map >>= 1;
3238 		}
3239 		itask->itask_allocated_buf_map = 0;
3240 	}
3241 }
3242 
3243 void
3244 stmf_task_free(scsi_task_t *task)
3245 {
3246 	stmf_local_port_t *lport = task->task_lport;
3247 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3248 	    task->task_stmf_private;
3249 
3250 	DTRACE_PROBE1(stmf__task__end, scsi_task_t *, task);
3251 	stmf_free_task_bufs(itask, lport);
3252 	if (itask->itask_itl_datap) {
3253 		if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter,
3254 		    -1) == 0) {
3255 			stmf_release_itl_handle(task->task_lu,
3256 			    itask->itask_itl_datap);
3257 		}
3258 	}
3259 	lport->lport_task_free(task);
3260 	if (itask->itask_worker) {
3261 		atomic_add_32(&stmf_cur_ntasks, -1);
3262 		atomic_add_32(&itask->itask_worker->worker_ref_count, -1);
3263 	}
3264 	/*
3265 	 * After calling stmf_task_lu_free, the task pointer can no longer
3266 	 * be trusted.
3267 	 */
3268 	stmf_task_lu_free(task);
3269 }
3270 
3271 void
3272 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
3273 {
3274 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3275 	    task->task_stmf_private;
3276 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
3277 	int nv;
3278 	uint32_t old, new;
3279 	uint32_t ct;
3280 	stmf_worker_t *w, *w1;
3281 	uint8_t tm;
3282 
3283 	if (task->task_max_nbufs > 4)
3284 		task->task_max_nbufs = 4;
3285 	task->task_cur_nbufs = 0;
3286 	/* Latest value of currently running tasks */
3287 	ct = atomic_add_32_nv(&stmf_cur_ntasks, 1);
3288 
3289 	/* Select the next worker using round robin */
3290 	nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1);
3291 	if (nv >= stmf_nworkers_accepting_cmds) {
3292 		int s = nv;
3293 		do {
3294 			nv -= stmf_nworkers_accepting_cmds;
3295 		} while (nv >= stmf_nworkers_accepting_cmds);
3296 		if (nv < 0)
3297 			nv = 0;
3298 		/* Its ok if this cas fails */
3299 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
3300 		    s, nv);
3301 	}
3302 	w = &stmf_workers[nv];
3303 
3304 	/*
3305 	 * A worker can be pinned by interrupt. So select the next one
3306 	 * if it has lower load.
3307 	 */
3308 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
3309 		w1 = stmf_workers;
3310 	} else {
3311 		w1 = &stmf_workers[nv + 1];
3312 	}
3313 	if (w1->worker_queue_depth < w->worker_queue_depth)
3314 		w = w1;
3315 
3316 	mutex_enter(&w->worker_lock);
3317 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
3318 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
3319 		/*
3320 		 * Maybe we are in the middle of a change. Just go to
3321 		 * the 1st worker.
3322 		 */
3323 		mutex_exit(&w->worker_lock);
3324 		w = stmf_workers;
3325 		mutex_enter(&w->worker_lock);
3326 	}
3327 	itask->itask_worker = w;
3328 	/*
3329 	 * Track max system load inside the worker as we already have the
3330 	 * worker lock (no point implementing another lock). The service
3331 	 * thread will do the comparisons and figure out the max overall
3332 	 * system load.
3333 	 */
3334 	if (w->worker_max_sys_qdepth_pu < ct)
3335 		w->worker_max_sys_qdepth_pu = ct;
3336 
3337 	do {
3338 		old = new = itask->itask_flags;
3339 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
3340 		if (task->task_mgmt_function) {
3341 			tm = task->task_mgmt_function;
3342 			if ((tm == TM_TARGET_RESET) ||
3343 			    (tm == TM_TARGET_COLD_RESET) ||
3344 			    (tm == TM_TARGET_WARM_RESET)) {
3345 				new |= ITASK_DEFAULT_HANDLING;
3346 			}
3347 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
3348 			new |= ITASK_DEFAULT_HANDLING;
3349 		}
3350 		new &= ~ITASK_IN_TRANSITION;
3351 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3352 	itask->itask_worker_next = NULL;
3353 	if (w->worker_task_tail) {
3354 		w->worker_task_tail->itask_worker_next = itask;
3355 	} else {
3356 		w->worker_task_head = itask;
3357 	}
3358 	w->worker_task_tail = itask;
3359 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3360 		w->worker_max_qdepth_pu = w->worker_queue_depth;
3361 	}
3362 	atomic_add_32(&w->worker_ref_count, 1);
3363 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
3364 	itask->itask_ncmds = 1;
3365 	if (dbuf) {
3366 		itask->itask_allocated_buf_map = 1;
3367 		itask->itask_dbufs[0] = dbuf;
3368 		dbuf->db_handle = 0;
3369 	} else {
3370 		itask->itask_allocated_buf_map = 0;
3371 		itask->itask_dbufs[0] = NULL;
3372 	}
3373 
3374 	stmf_update_kstat_lu_q(task, kstat_waitq_enter);
3375 	stmf_update_kstat_lport_q(task, kstat_waitq_enter);
3376 
3377 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3378 		cv_signal(&w->worker_cv);
3379 	mutex_exit(&w->worker_lock);
3380 
3381 	/*
3382 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
3383 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
3384 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
3385 	 */
3386 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
3387 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
3388 	}
3389 }
3390 
3391 /*
3392  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
3393  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
3394  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
3395  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
3396  * the LU will make this call only if we call the LU's abort entry point.
3397  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
3398  *
3399  * Same logic applies for the port.
3400  *
3401  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
3402  * and KNOWN_TO_TGT_PORT are reset.
3403  *
3404  * +++++++++++++++++++++++++++++++++++++++++++++++
3405  */
3406 
3407 stmf_status_t
3408 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
3409 {
3410 	stmf_status_t ret;
3411 
3412 	stmf_i_scsi_task_t *itask =
3413 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3414 
3415 	if (ioflags & STMF_IOF_LU_DONE) {
3416 		uint32_t new, old;
3417 		do {
3418 			new = old = itask->itask_flags;
3419 			if (new & ITASK_BEING_ABORTED)
3420 				return (STMF_ABORTED);
3421 			new &= ~ITASK_KNOWN_TO_LU;
3422 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3423 	}
3424 	if (itask->itask_flags & ITASK_BEING_ABORTED)
3425 		return (STMF_ABORTED);
3426 #ifdef	DEBUG
3427 	if (stmf_drop_buf_counter > 0) {
3428 		if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) ==
3429 		    1)
3430 			return (STMF_SUCCESS);
3431 	}
3432 #endif
3433 
3434 	stmf_update_kstat_lu_io(task, dbuf);
3435 	stmf_update_kstat_lport_io(task, dbuf);
3436 
3437 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, task,
3438 	    stmf_data_buf_t *, dbuf);
3439 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
3440 	DTRACE_PROBE2(scsi__xfer__end, scsi_task_t *, task,
3441 	    stmf_data_buf_t *, dbuf);
3442 	return (ret);
3443 }
3444 
3445 void
3446 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
3447 {
3448 	stmf_i_scsi_task_t *itask =
3449 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3450 	stmf_worker_t *w = itask->itask_worker;
3451 	uint32_t new, old;
3452 	uint8_t update_queue_flags, free_it, queue_it;
3453 
3454 	mutex_enter(&w->worker_lock);
3455 	do {
3456 		new = old = itask->itask_flags;
3457 		if (old & ITASK_BEING_ABORTED) {
3458 			mutex_exit(&w->worker_lock);
3459 			return;
3460 		}
3461 		free_it = 0;
3462 		if (iof & STMF_IOF_LPORT_DONE) {
3463 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
3464 			task->task_completion_status = dbuf->db_xfer_status;
3465 			free_it = 1;
3466 		}
3467 		/*
3468 		 * If the task is known to LU then queue it. But if
3469 		 * it is already queued (multiple completions) then
3470 		 * just update the buffer information by grabbing the
3471 		 * worker lock. If the task is not known to LU,
3472 		 * completed/aborted, then see if we need to
3473 		 * free this task.
3474 		 */
3475 		if (old & ITASK_KNOWN_TO_LU) {
3476 			free_it = 0;
3477 			update_queue_flags = 1;
3478 			if (old & ITASK_IN_WORKER_QUEUE) {
3479 				queue_it = 0;
3480 			} else {
3481 				queue_it = 1;
3482 				new |= ITASK_IN_WORKER_QUEUE;
3483 			}
3484 		} else {
3485 			update_queue_flags = 0;
3486 			queue_it = 0;
3487 		}
3488 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3489 
3490 	if (update_queue_flags) {
3491 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
3492 
3493 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
3494 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
3495 		if (queue_it) {
3496 			itask->itask_worker_next = NULL;
3497 			if (w->worker_task_tail) {
3498 				w->worker_task_tail->itask_worker_next = itask;
3499 			} else {
3500 				w->worker_task_head = itask;
3501 			}
3502 			w->worker_task_tail = itask;
3503 			if (++(w->worker_queue_depth) >
3504 			    w->worker_max_qdepth_pu) {
3505 				w->worker_max_qdepth_pu = w->worker_queue_depth;
3506 			}
3507 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3508 				cv_signal(&w->worker_cv);
3509 		}
3510 	}
3511 	mutex_exit(&w->worker_lock);
3512 
3513 	if (free_it) {
3514 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3515 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3516 		    ITASK_BEING_ABORTED)) == 0) {
3517 			stmf_update_kstat_lu_q(task, kstat_runq_exit);
3518 			stmf_update_kstat_lport_q(task, kstat_runq_exit);
3519 			stmf_task_free(task);
3520 		}
3521 	}
3522 }
3523 
3524 stmf_status_t
3525 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
3526 {
3527 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
3528 
3529 	stmf_i_scsi_task_t *itask =
3530 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3531 	if (ioflags & STMF_IOF_LU_DONE) {
3532 		uint32_t new, old;
3533 		do {
3534 			new = old = itask->itask_flags;
3535 			if (new & ITASK_BEING_ABORTED)
3536 				return (STMF_ABORTED);
3537 			new &= ~ITASK_KNOWN_TO_LU;
3538 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3539 	}
3540 
3541 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
3542 		return (STMF_SUCCESS);
3543 	}
3544 
3545 	if (itask->itask_flags & ITASK_BEING_ABORTED)
3546 		return (STMF_ABORTED);
3547 
3548 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
3549 		task->task_status_ctrl = 0;
3550 		task->task_resid = 0;
3551 	} else if (task->task_cmd_xfer_length >
3552 	    task->task_expected_xfer_length) {
3553 		task->task_status_ctrl = TASK_SCTRL_OVER;
3554 		task->task_resid = task->task_cmd_xfer_length -
3555 		    task->task_expected_xfer_length;
3556 	} else if (task->task_nbytes_transferred <
3557 	    task->task_expected_xfer_length) {
3558 		task->task_status_ctrl = TASK_SCTRL_UNDER;
3559 		task->task_resid = task->task_expected_xfer_length -
3560 		    task->task_nbytes_transferred;
3561 	} else {
3562 		task->task_status_ctrl = 0;
3563 		task->task_resid = 0;
3564 	}
3565 	return (task->task_lport->lport_send_status(task, ioflags));
3566 }
3567 
3568 void
3569 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3570 {
3571 	stmf_i_scsi_task_t *itask =
3572 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3573 	stmf_worker_t *w = itask->itask_worker;
3574 	uint32_t new, old;
3575 	uint8_t free_it, queue_it;
3576 
3577 	mutex_enter(&w->worker_lock);
3578 	do {
3579 		new = old = itask->itask_flags;
3580 		if (old & ITASK_BEING_ABORTED) {
3581 			mutex_exit(&w->worker_lock);
3582 			return;
3583 		}
3584 		free_it = 0;
3585 		if (iof & STMF_IOF_LPORT_DONE) {
3586 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
3587 			free_it = 1;
3588 		}
3589 		/*
3590 		 * If the task is known to LU then queue it. But if
3591 		 * it is already queued (multiple completions) then
3592 		 * just update the buffer information by grabbing the
3593 		 * worker lock. If the task is not known to LU,
3594 		 * completed/aborted, then see if we need to
3595 		 * free this task.
3596 		 */
3597 		if (old & ITASK_KNOWN_TO_LU) {
3598 			free_it = 0;
3599 			queue_it = 1;
3600 			if (old & ITASK_IN_WORKER_QUEUE) {
3601 				cmn_err(CE_PANIC, "status completion received"
3602 				    " when task is already in worker queue "
3603 				    " task = %p", (void *)task);
3604 			}
3605 			new |= ITASK_IN_WORKER_QUEUE;
3606 		} else {
3607 			queue_it = 0;
3608 		}
3609 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3610 	task->task_completion_status = s;
3611 
3612 	stmf_update_kstat_lu_q(task, kstat_runq_exit);
3613 	stmf_update_kstat_lport_q(task, kstat_runq_exit);
3614 
3615 	if (queue_it) {
3616 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
3617 		itask->itask_cmd_stack[itask->itask_ncmds++] =
3618 		    ITASK_CMD_STATUS_DONE;
3619 		itask->itask_worker_next = NULL;
3620 		if (w->worker_task_tail) {
3621 			w->worker_task_tail->itask_worker_next = itask;
3622 		} else {
3623 			w->worker_task_head = itask;
3624 		}
3625 		w->worker_task_tail = itask;
3626 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3627 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3628 		}
3629 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3630 			cv_signal(&w->worker_cv);
3631 	}
3632 	mutex_exit(&w->worker_lock);
3633 
3634 	if (free_it) {
3635 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3636 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3637 		    ITASK_BEING_ABORTED)) == 0) {
3638 			stmf_task_free(task);
3639 		} else {
3640 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
3641 			    " is not done, itask %p", (void *)itask);
3642 		}
3643 	}
3644 }
3645 
3646 void
3647 stmf_task_lu_done(scsi_task_t *task)
3648 {
3649 	stmf_i_scsi_task_t *itask =
3650 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3651 	stmf_worker_t *w = itask->itask_worker;
3652 	uint32_t new, old;
3653 
3654 	mutex_enter(&w->worker_lock);
3655 	do {
3656 		new = old = itask->itask_flags;
3657 		if (old & ITASK_BEING_ABORTED) {
3658 			mutex_exit(&w->worker_lock);
3659 			return;
3660 		}
3661 		if (old & ITASK_IN_WORKER_QUEUE) {
3662 			cmn_err(CE_PANIC, "task_lu_done received"
3663 			    " when task is in worker queue "
3664 			    " task = %p", (void *)task);
3665 		}
3666 		new &= ~ITASK_KNOWN_TO_LU;
3667 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3668 
3669 	mutex_exit(&w->worker_lock);
3670 
3671 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3672 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3673 	    ITASK_BEING_ABORTED)) == 0) {
3674 		stmf_task_free(task);
3675 	} else {
3676 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
3677 		    " the task is still not done, task = %p", (void *)task);
3678 	}
3679 }
3680 
3681 void
3682 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
3683 {
3684 	stmf_i_scsi_task_t *itask =
3685 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3686 	stmf_worker_t *w;
3687 	uint32_t old, new;
3688 
3689 	do {
3690 		old = new = itask->itask_flags;
3691 		if ((old & ITASK_BEING_ABORTED) ||
3692 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
3693 		    ITASK_KNOWN_TO_LU)) == 0)) {
3694 			return;
3695 		}
3696 		new |= ITASK_BEING_ABORTED;
3697 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3698 	task->task_completion_status = s;
3699 	itask->itask_start_time = ddi_get_lbolt();
3700 
3701 	if (((w = itask->itask_worker) == NULL) ||
3702 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
3703 		return;
3704 	}
3705 
3706 	/* Queue it and get out */
3707 	mutex_enter(&w->worker_lock);
3708 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
3709 		mutex_exit(&w->worker_lock);
3710 		return;
3711 	}
3712 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
3713 	itask->itask_worker_next = NULL;
3714 	if (w->worker_task_tail) {
3715 		w->worker_task_tail->itask_worker_next = itask;
3716 	} else {
3717 		w->worker_task_head = itask;
3718 	}
3719 	w->worker_task_tail = itask;
3720 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3721 		w->worker_max_qdepth_pu = w->worker_queue_depth;
3722 	}
3723 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3724 		cv_signal(&w->worker_cv);
3725 	mutex_exit(&w->worker_lock);
3726 }
3727 
3728 void
3729 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
3730 {
3731 	stmf_i_scsi_task_t *itask = NULL;
3732 	uint32_t old, new, f, rf;
3733 
3734 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
3735 	    stmf_status_t, s);
3736 
3737 	switch (abort_cmd) {
3738 	case STMF_QUEUE_ABORT_LU:
3739 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
3740 		return;
3741 	case STMF_QUEUE_TASK_ABORT:
3742 		stmf_queue_task_for_abort(task, s);
3743 		return;
3744 	case STMF_REQUEUE_TASK_ABORT_LPORT:
3745 		rf = ITASK_TGT_PORT_ABORT_CALLED;
3746 		f = ITASK_KNOWN_TO_TGT_PORT;
3747 		break;
3748 	case STMF_REQUEUE_TASK_ABORT_LU:
3749 		rf = ITASK_LU_ABORT_CALLED;
3750 		f = ITASK_KNOWN_TO_LU;
3751 		break;
3752 	default:
3753 		return;
3754 	}
3755 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
3756 	f |= ITASK_BEING_ABORTED | rf;
3757 	do {
3758 		old = new = itask->itask_flags;
3759 		if ((old & f) != f) {
3760 			return;
3761 		}
3762 		new &= ~rf;
3763 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3764 }
3765 
3766 void
3767 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3768 {
3769 	char			 info[STMF_CHANGE_INFO_LEN];
3770 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3771 	unsigned long long	st;
3772 
3773 	st = s;	/* gcc fix */
3774 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
3775 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3776 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
3777 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
3778 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3779 		    "Task aborted but LU is not finished, task ="
3780 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
3781 	} else {
3782 		/*
3783 		 * LU abort successfully
3784 		 */
3785 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
3786 		return;
3787 	}
3788 
3789 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
3790 	stmf_abort_task_offline(task, 1, info);
3791 }
3792 
3793 void
3794 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3795 {
3796 	char			 info[STMF_CHANGE_INFO_LEN];
3797 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3798 	unsigned long long	st;
3799 
3800 	st = s;
3801 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
3802 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3803 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
3804 		    st);
3805 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
3806 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3807 		    "Task aborted but tgt port is not finished, "
3808 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
3809 	} else {
3810 		/*
3811 		 * LU abort successfully
3812 		 */
3813 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_TGT_PORT);
3814 		return;
3815 	}
3816 
3817 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
3818 	stmf_abort_task_offline(task, 0, info);
3819 }
3820 
3821 stmf_status_t
3822 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
3823 {
3824 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3825 	    task->task_stmf_private;
3826 	stmf_worker_t *w = itask->itask_worker;
3827 	int i;
3828 
3829 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
3830 	mutex_enter(&w->worker_lock);
3831 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
3832 		mutex_exit(&w->worker_lock);
3833 		return (STMF_BUSY);
3834 	}
3835 	for (i = 0; i < itask->itask_ncmds; i++) {
3836 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
3837 			mutex_exit(&w->worker_lock);
3838 			return (STMF_SUCCESS);
3839 		}
3840 	}
3841 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
3842 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
3843 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
3844 	} else {
3845 		clock_t t = drv_usectohz(timeout * 1000);
3846 		if (t == 0)
3847 			t = 1;
3848 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
3849 	}
3850 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
3851 		itask->itask_worker_next = NULL;
3852 		if (w->worker_task_tail) {
3853 			w->worker_task_tail->itask_worker_next = itask;
3854 		} else {
3855 			w->worker_task_head = itask;
3856 		}
3857 		w->worker_task_tail = itask;
3858 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3859 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3860 		}
3861 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
3862 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3863 			cv_signal(&w->worker_cv);
3864 	}
3865 	mutex_exit(&w->worker_lock);
3866 	return (STMF_SUCCESS);
3867 }
3868 
3869 stmf_status_t
3870 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
3871 {
3872 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3873 	    task->task_stmf_private;
3874 	stmf_worker_t *w = itask->itask_worker;
3875 	int i;
3876 
3877 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
3878 	mutex_enter(&w->worker_lock);
3879 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
3880 		mutex_exit(&w->worker_lock);
3881 		return (STMF_BUSY);
3882 	}
3883 	for (i = 0; i < itask->itask_ncmds; i++) {
3884 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
3885 			mutex_exit(&w->worker_lock);
3886 			return (STMF_SUCCESS);
3887 		}
3888 	}
3889 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
3890 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
3891 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
3892 	} else {
3893 		clock_t t = drv_usectohz(timeout * 1000);
3894 		if (t == 0)
3895 			t = 1;
3896 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
3897 	}
3898 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
3899 		itask->itask_worker_next = NULL;
3900 		if (w->worker_task_tail) {
3901 			w->worker_task_tail->itask_worker_next = itask;
3902 		} else {
3903 			w->worker_task_head = itask;
3904 		}
3905 		w->worker_task_tail = itask;
3906 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3907 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3908 		}
3909 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3910 			cv_signal(&w->worker_cv);
3911 	}
3912 	mutex_exit(&w->worker_lock);
3913 	return (STMF_SUCCESS);
3914 }
3915 
3916 void
3917 stmf_do_task_abort(scsi_task_t *task)
3918 {
3919 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3920 	stmf_lu_t		*lu;
3921 	stmf_local_port_t	*lport;
3922 	unsigned long long	 ret;
3923 	uint32_t		 old, new;
3924 	uint8_t			 call_lu_abort, call_port_abort;
3925 	char			 info[STMF_CHANGE_INFO_LEN];
3926 
3927 	lu = task->task_lu;
3928 	lport = task->task_lport;
3929 	do {
3930 		old = new = itask->itask_flags;
3931 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
3932 		    ITASK_KNOWN_TO_LU) {
3933 			new |= ITASK_LU_ABORT_CALLED;
3934 			call_lu_abort = 1;
3935 		} else {
3936 			call_lu_abort = 0;
3937 		}
3938 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3939 
3940 	if (call_lu_abort) {
3941 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
3942 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
3943 		} else {
3944 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
3945 		}
3946 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
3947 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
3948 		} else if (ret == STMF_BUSY) {
3949 			atomic_and_32(&itask->itask_flags,
3950 			    ~ITASK_LU_ABORT_CALLED);
3951 		} else if (ret != STMF_SUCCESS) {
3952 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3953 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
3954 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3955 			stmf_abort_task_offline(task, 1, info);
3956 		}
3957 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
3958 		if (ddi_get_lbolt() > (itask->itask_start_time +
3959 		    STMF_SEC2TICK(lu->lu_abort_timeout?
3960 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
3961 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3962 			    "lu abort timed out");
3963 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3964 			stmf_abort_task_offline(itask->itask_task, 1, info);
3965 		}
3966 	}
3967 
3968 	do {
3969 		old = new = itask->itask_flags;
3970 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
3971 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
3972 			new |= ITASK_TGT_PORT_ABORT_CALLED;
3973 			call_port_abort = 1;
3974 		} else {
3975 			call_port_abort = 0;
3976 		}
3977 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3978 	if (call_port_abort) {
3979 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
3980 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
3981 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
3982 		} else if (ret == STMF_BUSY) {
3983 			atomic_and_32(&itask->itask_flags,
3984 			    ~ITASK_TGT_PORT_ABORT_CALLED);
3985 		} else if (ret != STMF_SUCCESS) {
3986 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3987 			    "Abort failed by tgt port %p ret %llx",
3988 			    (void *)lport, ret);
3989 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3990 			stmf_abort_task_offline(task, 0, info);
3991 		}
3992 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
3993 		if (ddi_get_lbolt() > (itask->itask_start_time +
3994 		    STMF_SEC2TICK(lport->lport_abort_timeout?
3995 		    lport->lport_abort_timeout :
3996 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
3997 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3998 			    "lport abort timed out");
3999 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
4000 			stmf_abort_task_offline(itask->itask_task, 0, info);
4001 		}
4002 	}
4003 }
4004 
4005 stmf_status_t
4006 stmf_ctl(int cmd, void *obj, void *arg)
4007 {
4008 	stmf_status_t			ret;
4009 	stmf_i_lu_t			*ilu;
4010 	stmf_i_local_port_t		*ilport;
4011 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
4012 
4013 	mutex_enter(&stmf_state.stmf_lock);
4014 	ret = STMF_INVALID_ARG;
4015 	if (cmd & STMF_CMD_LU_OP) {
4016 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
4017 		if (ilu == NULL) {
4018 			goto stmf_ctl_lock_exit;
4019 		}
4020 		DTRACE_PROBE3(lu__state__change,
4021 		    stmf_lu_t *, ilu->ilu_lu,
4022 		    int, cmd, stmf_state_change_info_t *, ssci);
4023 	} else if (cmd & STMF_CMD_LPORT_OP) {
4024 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
4025 		if (ilport == NULL) {
4026 			goto stmf_ctl_lock_exit;
4027 		}
4028 		DTRACE_PROBE3(lport__state__change,
4029 		    stmf_local_port_t *, ilport->ilport_lport,
4030 		    int, cmd, stmf_state_change_info_t *, ssci);
4031 	} else {
4032 		goto stmf_ctl_lock_exit;
4033 	}
4034 
4035 	switch (cmd) {
4036 	case STMF_CMD_LU_ONLINE:
4037 		if (ilu->ilu_state == STMF_STATE_ONLINE) {
4038 			ret = STMF_ALREADY;
4039 			goto stmf_ctl_lock_exit;
4040 		}
4041 		if (ilu->ilu_state != STMF_STATE_OFFLINE) {
4042 			ret = STMF_INVALID_ARG;
4043 			goto stmf_ctl_lock_exit;
4044 		}
4045 		ilu->ilu_state = STMF_STATE_ONLINING;
4046 		mutex_exit(&stmf_state.stmf_lock);
4047 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
4048 		break;
4049 
4050 	case STMF_CMD_LU_ONLINE_COMPLETE:
4051 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
4052 			ret = STMF_INVALID_ARG;
4053 			goto stmf_ctl_lock_exit;
4054 		}
4055 		if (((stmf_change_status_t *)arg)->st_completion_status ==
4056 		    STMF_SUCCESS) {
4057 			ilu->ilu_state = STMF_STATE_ONLINE;
4058 			mutex_exit(&stmf_state.stmf_lock);
4059 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
4060 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
4061 			mutex_enter(&stmf_state.stmf_lock);
4062 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
4063 		} else {
4064 			/* XXX: should throw a meesage an record more data */
4065 			ilu->ilu_state = STMF_STATE_OFFLINE;
4066 		}
4067 		ret = STMF_SUCCESS;
4068 		goto stmf_ctl_lock_exit;
4069 
4070 	case STMF_CMD_LU_OFFLINE:
4071 		if (ilu->ilu_state == STMF_STATE_OFFLINE) {
4072 			ret = STMF_ALREADY;
4073 			goto stmf_ctl_lock_exit;
4074 		}
4075 		if (ilu->ilu_state != STMF_STATE_ONLINE) {
4076 			ret = STMF_INVALID_ARG;
4077 			goto stmf_ctl_lock_exit;
4078 		}
4079 		ilu->ilu_state = STMF_STATE_OFFLINING;
4080 		mutex_exit(&stmf_state.stmf_lock);
4081 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
4082 		break;
4083 
4084 	case STMF_CMD_LU_OFFLINE_COMPLETE:
4085 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
4086 			ret = STMF_INVALID_ARG;
4087 			goto stmf_ctl_lock_exit;
4088 		}
4089 		if (((stmf_change_status_t *)arg)->st_completion_status ==
4090 		    STMF_SUCCESS) {
4091 			ilu->ilu_state = STMF_STATE_OFFLINE;
4092 			mutex_exit(&stmf_state.stmf_lock);
4093 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
4094 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
4095 			mutex_enter(&stmf_state.stmf_lock);
4096 		} else {
4097 			ilu->ilu_state = STMF_STATE_ONLINE;
4098 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
4099 		}
4100 		mutex_exit(&stmf_state.stmf_lock);
4101 		break;
4102 
4103 	/*
4104 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
4105 	 * It's related with hardware disable/enable.
4106 	 */
4107 	case STMF_CMD_LPORT_ONLINE:
4108 		if (ilport->ilport_state == STMF_STATE_ONLINE) {
4109 			ret = STMF_ALREADY;
4110 			goto stmf_ctl_lock_exit;
4111 		}
4112 		if (ilport->ilport_state != STMF_STATE_OFFLINE) {
4113 			ret = STMF_INVALID_ARG;
4114 			goto stmf_ctl_lock_exit;
4115 		}
4116 
4117 		/*
4118 		 * Only user request can recover the port from the
4119 		 * FORCED_OFFLINE state
4120 		 */
4121 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
4122 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
4123 				ret = STMF_FAILURE;
4124 				goto stmf_ctl_lock_exit;
4125 			}
4126 		}
4127 
4128 		/*
4129 		 * Avoid too frequent request to online
4130 		 */
4131 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
4132 			ilport->ilport_online_times = 0;
4133 			ilport->ilport_avg_interval = 0;
4134 		}
4135 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
4136 		    (ilport->ilport_online_times >= 4)) {
4137 			ret = STMF_FAILURE;
4138 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
4139 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
4140 			    "online the port");
4141 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
4142 			    "online the port, set FORCED_OFFLINE now");
4143 			goto stmf_ctl_lock_exit;
4144 		}
4145 		if (ilport->ilport_online_times > 0) {
4146 			if (ilport->ilport_online_times == 1) {
4147 				ilport->ilport_avg_interval = ddi_get_lbolt() -
4148 				    ilport->ilport_last_online_clock;
4149 			} else {
4150 				ilport->ilport_avg_interval =
4151 				    (ilport->ilport_avg_interval +
4152 				    ddi_get_lbolt() -
4153 				    ilport->ilport_last_online_clock) >> 1;
4154 			}
4155 		}
4156 		ilport->ilport_last_online_clock = ddi_get_lbolt();
4157 		ilport->ilport_online_times++;
4158 
4159 		/*
4160 		 * Submit online service request
4161 		 */
4162 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
4163 		ilport->ilport_state = STMF_STATE_ONLINING;
4164 		mutex_exit(&stmf_state.stmf_lock);
4165 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
4166 		break;
4167 
4168 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
4169 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
4170 			ret = STMF_INVALID_ARG;
4171 			goto stmf_ctl_lock_exit;
4172 		}
4173 		if (((stmf_change_status_t *)arg)->st_completion_status ==
4174 		    STMF_SUCCESS) {
4175 			ilport->ilport_state = STMF_STATE_ONLINE;
4176 			mutex_exit(&stmf_state.stmf_lock);
4177 			((stmf_local_port_t *)obj)->lport_ctl(
4178 			    (stmf_local_port_t *)obj,
4179 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
4180 			mutex_enter(&stmf_state.stmf_lock);
4181 		} else {
4182 			ilport->ilport_state = STMF_STATE_OFFLINE;
4183 		}
4184 		ret = STMF_SUCCESS;
4185 		goto stmf_ctl_lock_exit;
4186 
4187 	case STMF_CMD_LPORT_OFFLINE:
4188 		if (ilport->ilport_state == STMF_STATE_OFFLINE) {
4189 			ret = STMF_ALREADY;
4190 			goto stmf_ctl_lock_exit;
4191 		}
4192 		if (ilport->ilport_state != STMF_STATE_ONLINE) {
4193 			ret = STMF_INVALID_ARG;
4194 			goto stmf_ctl_lock_exit;
4195 		}
4196 		ilport->ilport_state = STMF_STATE_OFFLINING;
4197 		mutex_exit(&stmf_state.stmf_lock);
4198 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
4199 		break;
4200 
4201 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
4202 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
4203 			ret = STMF_INVALID_ARG;
4204 			goto stmf_ctl_lock_exit;
4205 		}
4206 		if (((stmf_change_status_t *)arg)->st_completion_status ==
4207 		    STMF_SUCCESS) {
4208 			ilport->ilport_state = STMF_STATE_OFFLINE;
4209 			mutex_exit(&stmf_state.stmf_lock);
4210 			((stmf_local_port_t *)obj)->lport_ctl(
4211 			    (stmf_local_port_t *)obj,
4212 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
4213 			mutex_enter(&stmf_state.stmf_lock);
4214 		} else {
4215 			ilport->ilport_state = STMF_STATE_ONLINE;
4216 		}
4217 		mutex_exit(&stmf_state.stmf_lock);
4218 		break;
4219 
4220 	default:
4221 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
4222 		ret = STMF_INVALID_ARG;
4223 		goto stmf_ctl_lock_exit;
4224 	}
4225 
4226 	return (STMF_SUCCESS);
4227 
4228 stmf_ctl_lock_exit:;
4229 	mutex_exit(&stmf_state.stmf_lock);
4230 	return (ret);
4231 }
4232 
4233 /* ARGSUSED */
4234 stmf_status_t
4235 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
4236 						uint32_t *bufsizep)
4237 {
4238 	return (STMF_NOT_SUPPORTED);
4239 }
4240 
4241 /* ARGSUSED */
4242 stmf_status_t
4243 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
4244 						uint32_t *bufsizep)
4245 {
4246 	uint32_t cl = SI_GET_CLASS(cmd);
4247 
4248 	if (cl == SI_STMF) {
4249 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
4250 	}
4251 	if (cl == SI_LPORT) {
4252 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
4253 		    arg2, buf, bufsizep));
4254 	} else if (cl == SI_LU) {
4255 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
4256 		    bufsizep));
4257 	}
4258 
4259 	return (STMF_NOT_SUPPORTED);
4260 }
4261 
4262 /*
4263  * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
4264  * stmf to register local ports. The ident should have 20 bytes in buffer
4265  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
4266  */
4267 void
4268 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
4269     uint8_t protocol_id)
4270 {
4271 	char wwn_str[20+1];
4272 
4273 	sdid->protocol_id = protocol_id;
4274 	sdid->piv = 1;
4275 	sdid->code_set = CODE_SET_ASCII;
4276 	sdid->association = ID_IS_TARGET_PORT;
4277 	sdid->ident_length = 20;
4278 	/* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
4279 	(void) snprintf(wwn_str, sizeof (wwn_str),
4280 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
4281 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
4282 	bcopy(wwn_str, (char *)sdid->ident, 20);
4283 }
4284 
4285 
4286 stmf_xfer_data_t *
4287 stmf_prepare_tpgs_data()
4288 {
4289 	stmf_xfer_data_t *xd;
4290 	stmf_i_local_port_t *ilport;
4291 	uint8_t *p;
4292 	uint32_t sz, asz, nports;
4293 
4294 	mutex_enter(&stmf_state.stmf_lock);
4295 	/* The spec only allows for 255 ports to be reported */
4296 	nports = min(stmf_state.stmf_nlports, 255);
4297 	sz = (nports * 4) + 12;
4298 	asz = sz + sizeof (*xd) - 4;
4299 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
4300 	if (xd == NULL) {
4301 		mutex_exit(&stmf_state.stmf_lock);
4302 		return (NULL);
4303 	}
4304 	xd->alloc_size = asz;
4305 	xd->size_left = sz;
4306 
4307 	p = xd->buf;
4308 
4309 	*((uint32_t *)p) = BE_32(sz - 4);
4310 	p += 4;
4311 	p[0] = 0x80;	/* PREF */
4312 	p[1] = 1;	/* AO_SUP */
4313 	p[7] = nports & 0xff;
4314 	p += 8;
4315 	for (ilport = stmf_state.stmf_ilportlist; ilport && nports;
4316 	    nports++, ilport = ilport->ilport_next, p += 4) {
4317 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
4318 	}
4319 	mutex_exit(&stmf_state.stmf_lock);
4320 
4321 	return (xd);
4322 }
4323 
4324 struct scsi_devid_desc *
4325 stmf_scsilib_get_devid_desc(uint16_t rtpid)
4326 {
4327 	scsi_devid_desc_t *devid = NULL;
4328 	stmf_i_local_port_t *ilport;
4329 
4330 	mutex_enter(&stmf_state.stmf_lock);
4331 
4332 	for (ilport = stmf_state.stmf_ilportlist; ilport;
4333 	    ilport = ilport->ilport_next) {
4334 		if (ilport->ilport_rtpid == rtpid) {
4335 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
4336 			uint32_t id_sz = sizeof (scsi_devid_desc_t) - 1 +
4337 			    id->ident_length;
4338 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
4339 			    KM_NOSLEEP);
4340 			if (devid != NULL) {
4341 				bcopy(id, devid, id_sz);
4342 			}
4343 			break;
4344 		}
4345 	}
4346 
4347 	mutex_exit(&stmf_state.stmf_lock);
4348 	return (devid);
4349 }
4350 
4351 uint16_t
4352 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
4353 {
4354 	stmf_i_local_port_t	*ilport;
4355 	scsi_devid_desc_t	*id;
4356 	uint16_t		rtpid = 0;
4357 
4358 	mutex_enter(&stmf_state.stmf_lock);
4359 	for (ilport = stmf_state.stmf_ilportlist; ilport;
4360 	    ilport = ilport->ilport_next) {
4361 		id = ilport->ilport_lport->lport_id;
4362 		if ((devid->ident_length == id->ident_length) &&
4363 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
4364 			rtpid = ilport->ilport_rtpid;
4365 			break;
4366 		}
4367 	}
4368 	mutex_exit(&stmf_state.stmf_lock);
4369 	return (rtpid);
4370 }
4371 
4372 static uint16_t stmf_lu_id_gen_number = 0;
4373 
4374 stmf_status_t
4375 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
4376 {
4377 	uint8_t *p;
4378 	struct timeval32 timestamp32;
4379 	uint32_t *t = (uint32_t *)&timestamp32;
4380 	struct ether_addr mac;
4381 	uint8_t *e = (uint8_t *)&mac;
4382 
4383 	if (company_id == COMPANY_ID_NONE)
4384 		company_id = COMPANY_ID_SUN;
4385 
4386 	if (lu_id->ident_length != 0x10)
4387 		return (STMF_INVALID_ARG);
4388 
4389 	p = (uint8_t *)lu_id;
4390 
4391 	atomic_add_16(&stmf_lu_id_gen_number, 1);
4392 
4393 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
4394 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
4395 	p[5] = (company_id >> 12) & 0xff;
4396 	p[6] = (company_id >> 4) & 0xff;
4397 	p[7] = (company_id << 4) & 0xf0;
4398 	if (!localetheraddr((struct ether_addr *)NULL, &mac)) {
4399 		int hid = BE_32((int)zone_get_hostid(NULL));
4400 		e[0] = (hid >> 24) & 0xff;
4401 		e[1] = (hid >> 16) & 0xff;
4402 		e[2] = (hid >> 8) & 0xff;
4403 		e[3] = hid & 0xff;
4404 		e[4] = e[5] = 0;
4405 	}
4406 	bcopy(e, p+8, 6);
4407 	uniqtime32(&timestamp32);
4408 	*t = BE_32(*t);
4409 	bcopy(t, p+14, 4);
4410 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
4411 	p[19] = stmf_lu_id_gen_number & 0xff;
4412 
4413 	return (STMF_SUCCESS);
4414 }
4415 
4416 /*
4417  * saa is sense key, ASC, ASCQ
4418  */
4419 void
4420 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
4421 {
4422 	uint8_t sd[18];
4423 	task->task_scsi_status = st;
4424 	if (st == 2) {
4425 		bzero(sd, 18);
4426 		sd[0] = 0x70;
4427 		sd[2] = (saa >> 16) & 0xf;
4428 		sd[7] = 10;
4429 		sd[12] = (saa >> 8) & 0xff;
4430 		sd[13] = saa & 0xff;
4431 		task->task_sense_data = sd;
4432 		task->task_sense_length = 18;
4433 	} else {
4434 		task->task_sense_data = NULL;
4435 		task->task_sense_length = 0;
4436 	}
4437 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
4438 }
4439 
4440 uint32_t
4441 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
4442     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
4443 {
4444 	uint8_t		*p = NULL;
4445 	uint8_t		small_buf[32];
4446 	uint32_t	sz = 0;
4447 	uint32_t	n = 4;
4448 	uint32_t	m = 0;
4449 	uint32_t	last_bit = 0;
4450 
4451 	if (page_len < 4)
4452 		return (0);
4453 	if (page_len > 65535)
4454 		page_len = 65535;
4455 
4456 	page[0] = byte0;
4457 	page[1] = 0x83;
4458 
4459 	/* CONSTCOND */
4460 	while (1) {
4461 		m += sz;
4462 		if (sz && (page_len > n)) {
4463 			uint32_t copysz;
4464 			copysz = page_len > (n + sz) ? sz : page_len - n;
4465 			bcopy(p, page + n, copysz);
4466 			n += copysz;
4467 		}
4468 		vpd_mask &= ~last_bit;
4469 		if (vpd_mask == 0)
4470 			break;
4471 
4472 		if (vpd_mask & STMF_VPD_LU_ID) {
4473 			last_bit = STMF_VPD_LU_ID;
4474 			sz = task->task_lu->lu_id->ident_length + 4;
4475 			p = (uint8_t *)task->task_lu->lu_id;
4476 			continue;
4477 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
4478 			last_bit = STMF_VPD_TARGET_ID;
4479 			sz = task->task_lport->lport_id->ident_length + 4;
4480 			p = (uint8_t *)task->task_lport->lport_id;
4481 			continue;
4482 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
4483 			last_bit = STMF_VPD_TP_GROUP;
4484 			p = small_buf;
4485 			bzero(p, 8);
4486 			p[0] = 1;
4487 			p[1] = 0x15;
4488 			p[3] = 4;
4489 			/* Group ID is always 0 */
4490 			sz = 8;
4491 			continue;
4492 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
4493 			stmf_i_local_port_t *ilport;
4494 
4495 			last_bit = STMF_VPD_RELATIVE_TP_ID;
4496 			p = small_buf;
4497 			bzero(p, 8);
4498 			p[0] = 1;
4499 			p[1] = 0x14;
4500 			p[3] = 4;
4501 			ilport = (stmf_i_local_port_t *)
4502 			    task->task_lport->lport_stmf_private;
4503 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
4504 			p[7] = ilport->ilport_rtpid & 0xff;
4505 			sz = 8;
4506 			continue;
4507 		} else {
4508 			cmn_err(CE_WARN, "Invalid vpd_mask");
4509 			break;
4510 		}
4511 	}
4512 
4513 	page[2] = (m >> 8) & 0xff;
4514 	page[3] = m & 0xff;
4515 
4516 	return (n);
4517 }
4518 
4519 void
4520 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
4521 {
4522 	stmf_i_scsi_task_t *itask =
4523 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4524 	stmf_xfer_data_t *xd;
4525 	uint32_t sz, minsz;
4526 
4527 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
4528 	task->task_cmd_xfer_length =
4529 	    ((((uint32_t)task->task_cdb[6]) << 24) |
4530 	    (((uint32_t)task->task_cdb[7]) << 16) |
4531 	    (((uint32_t)task->task_cdb[8]) << 8) |
4532 	    ((uint32_t)task->task_cdb[9]));
4533 
4534 	if (task->task_additional_flags &
4535 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4536 		task->task_expected_xfer_length =
4537 		    task->task_cmd_xfer_length;
4538 	}
4539 
4540 	if (task->task_cmd_xfer_length < 16) {
4541 		stmf_scsilib_send_status(task, STATUS_CHECK,
4542 		    STMF_SAA_INVALID_FIELD_IN_CDB);
4543 		return;
4544 	}
4545 
4546 	sz = min(task->task_expected_xfer_length,
4547 	    task->task_cmd_xfer_length);
4548 
4549 	xd = stmf_prepare_tpgs_data();
4550 
4551 	if (xd == NULL) {
4552 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4553 		    STMF_ALLOC_FAILURE, NULL);
4554 		return;
4555 	}
4556 
4557 	sz = min(sz, xd->size_left);
4558 	xd->size_left = sz;
4559 	minsz = min(512, sz);
4560 
4561 	if (dbuf == NULL)
4562 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
4563 	if (dbuf == NULL) {
4564 		kmem_free(xd, xd->alloc_size);
4565 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4566 		    STMF_ALLOC_FAILURE, NULL);
4567 		return;
4568 	}
4569 	dbuf->db_lu_private = xd;
4570 	stmf_xd_to_dbuf(dbuf);
4571 
4572 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
4573 	(void) stmf_xfer_data(task, dbuf, 0);
4574 
4575 }
4576 
4577 void
4578 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
4579 {
4580 	switch (task->task_mgmt_function) {
4581 	/*
4582 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
4583 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
4584 	 * in these cases. This needs to be changed to abort only the required
4585 	 * set.
4586 	 */
4587 	case TM_ABORT_TASK:
4588 	case TM_ABORT_TASK_SET:
4589 	case TM_CLEAR_TASK_SET:
4590 	case TM_LUN_RESET:
4591 		stmf_handle_lun_reset(task);
4592 		return;
4593 	case TM_TARGET_RESET:
4594 	case TM_TARGET_COLD_RESET:
4595 	case TM_TARGET_WARM_RESET:
4596 		stmf_handle_target_reset(task);
4597 		return;
4598 	default:
4599 		/* We dont support this task mgmt function */
4600 		stmf_scsilib_send_status(task, STATUS_CHECK,
4601 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
4602 		return;
4603 	}
4604 }
4605 
4606 void
4607 stmf_handle_lun_reset(scsi_task_t *task)
4608 {
4609 	stmf_i_scsi_task_t *itask;
4610 	stmf_i_lu_t *ilu;
4611 
4612 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4613 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4614 
4615 	/*
4616 	 * To sync with target reset, grab this lock. The LU is not going
4617 	 * anywhere as there is atleast one task pending (this task).
4618 	 */
4619 	mutex_enter(&stmf_state.stmf_lock);
4620 
4621 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4622 		mutex_exit(&stmf_state.stmf_lock);
4623 		stmf_scsilib_send_status(task, STATUS_CHECK,
4624 		    STMF_SAA_OPERATION_IN_PROGRESS);
4625 		return;
4626 	}
4627 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4628 	mutex_exit(&stmf_state.stmf_lock);
4629 
4630 	/*
4631 	 * Mark this task as the one causing LU reset so that we know who
4632 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
4633 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
4634 	 */
4635 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
4636 
4637 	/* Initiatiate abort on all commands on this LU except this one */
4638 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
4639 
4640 	/* Start polling on this task */
4641 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4642 	    != STMF_SUCCESS) {
4643 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4644 		    NULL);
4645 		return;
4646 	}
4647 }
4648 
4649 void
4650 stmf_handle_target_reset(scsi_task_t *task)
4651 {
4652 	stmf_i_scsi_task_t *itask;
4653 	stmf_i_lu_t *ilu;
4654 	stmf_i_scsi_session_t *iss;
4655 	stmf_lun_map_t *lm;
4656 	stmf_lun_map_ent_t *lm_ent;
4657 	int i, lf;
4658 
4659 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4660 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
4661 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4662 
4663 	/*
4664 	 * To sync with LUN reset, grab this lock. The session is not going
4665 	 * anywhere as there is atleast one task pending (this task).
4666 	 */
4667 	mutex_enter(&stmf_state.stmf_lock);
4668 
4669 	/* Grab the session lock as a writer to prevent any changes in it */
4670 	rw_enter(iss->iss_lockp, RW_WRITER);
4671 
4672 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
4673 		rw_exit(iss->iss_lockp);
4674 		mutex_exit(&stmf_state.stmf_lock);
4675 		stmf_scsilib_send_status(task, STATUS_CHECK,
4676 		    STMF_SAA_OPERATION_IN_PROGRESS);
4677 		return;
4678 	}
4679 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
4680 
4681 	/*
4682 	 * Now go through each LUN in this session and make sure all of them
4683 	 * can be reset.
4684 	 */
4685 	lm = iss->iss_sm;
4686 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
4687 		if (lm->lm_plus[i] == NULL)
4688 			continue;
4689 		lf++;
4690 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4691 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4692 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4693 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4694 			rw_exit(iss->iss_lockp);
4695 			mutex_exit(&stmf_state.stmf_lock);
4696 			stmf_scsilib_send_status(task, STATUS_CHECK,
4697 			    STMF_SAA_OPERATION_IN_PROGRESS);
4698 			return;
4699 		}
4700 	}
4701 	if (lf == 0) {
4702 		/* No luns in this session */
4703 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4704 		rw_exit(iss->iss_lockp);
4705 		mutex_exit(&stmf_state.stmf_lock);
4706 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
4707 		return;
4708 	}
4709 
4710 	/* ok, start the damage */
4711 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
4712 	    ITASK_CAUSING_TARGET_RESET;
4713 	for (i = 0; i < lm->lm_nentries; i++) {
4714 		if (lm->lm_plus[i] == NULL)
4715 			continue;
4716 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4717 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4718 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4719 	}
4720 	rw_exit(iss->iss_lockp);
4721 	mutex_exit(&stmf_state.stmf_lock);
4722 
4723 	for (i = 0; i < lm->lm_nentries; i++) {
4724 		if (lm->lm_plus[i] == NULL)
4725 			continue;
4726 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4727 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
4728 		    lm_ent->ent_lu);
4729 	}
4730 
4731 	/* Start polling on this task */
4732 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4733 	    != STMF_SUCCESS) {
4734 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4735 		    NULL);
4736 		return;
4737 	}
4738 }
4739 
4740 int
4741 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
4742 {
4743 	scsi_task_t *task = itask->itask_task;
4744 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4745 	    task->task_session->ss_stmf_private;
4746 
4747 	rw_enter(iss->iss_lockp, RW_WRITER);
4748 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
4749 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
4750 		rw_exit(iss->iss_lockp);
4751 		return (0);
4752 	}
4753 	atomic_and_32(&iss->iss_flags,
4754 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
4755 	rw_exit(iss->iss_lockp);
4756 
4757 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4758 		return (0);
4759 	}
4760 	stmf_scsilib_send_status(task, STATUS_CHECK,
4761 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
4762 	return (1);
4763 }
4764 
4765 void
4766 stmf_worker_init()
4767 {
4768 	uint32_t i;
4769 
4770 	/* Make local copy of global tunables */
4771 	stmf_i_max_nworkers = stmf_max_nworkers;
4772 	stmf_i_min_nworkers = stmf_min_nworkers;
4773 
4774 	ASSERT(stmf_workers == NULL);
4775 	if (stmf_i_min_nworkers < 4) {
4776 		stmf_i_min_nworkers = 4;
4777 	}
4778 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
4779 		stmf_i_max_nworkers = stmf_i_min_nworkers;
4780 	}
4781 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
4782 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
4783 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4784 		stmf_worker_t *w = &stmf_workers[i];
4785 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
4786 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
4787 	}
4788 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4789 	stmf_workers_state = STMF_WORKERS_ENABLED;
4790 
4791 	/* Workers will be started by stmf_worker_mgmt() */
4792 
4793 	/* Lets wait for atleast one worker to start */
4794 	while (stmf_nworkers_cur == 0)
4795 		delay(drv_usectohz(20 * 1000));
4796 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
4797 }
4798 
4799 stmf_status_t
4800 stmf_worker_fini()
4801 {
4802 	int i;
4803 	clock_t sb;
4804 
4805 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
4806 		return (STMF_SUCCESS);
4807 	ASSERT(stmf_workers);
4808 	stmf_workers_state = STMF_WORKERS_DISABLED;
4809 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4810 	cv_signal(&stmf_state.stmf_cv);
4811 
4812 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
4813 	/* Wait for all the threads to die */
4814 	while (stmf_nworkers_cur != 0) {
4815 		if (ddi_get_lbolt() > sb) {
4816 			stmf_workers_state = STMF_WORKERS_ENABLED;
4817 			return (STMF_BUSY);
4818 		}
4819 		delay(drv_usectohz(100 * 1000));
4820 	}
4821 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4822 		stmf_worker_t *w = &stmf_workers[i];
4823 		mutex_destroy(&w->worker_lock);
4824 		cv_destroy(&w->worker_cv);
4825 	}
4826 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
4827 	stmf_workers = NULL;
4828 
4829 	return (STMF_SUCCESS);
4830 }
4831 
4832 void
4833 stmf_worker_task(void *arg)
4834 {
4835 	stmf_worker_t *w;
4836 	stmf_i_scsi_session_t *iss;
4837 	scsi_task_t *task;
4838 	stmf_i_scsi_task_t *itask;
4839 	stmf_data_buf_t *dbuf;
4840 	stmf_lu_t *lu;
4841 	clock_t wait_timer = 0;
4842 	clock_t wait_ticks;
4843 	uint32_t old, new;
4844 	uint8_t curcmd;
4845 	uint8_t abort_free;
4846 	uint8_t wait_queue;
4847 	uint8_t dec_qdepth;
4848 
4849 	w = (stmf_worker_t *)arg;
4850 	wait_ticks = drv_usectohz(10000);
4851 
4852 	mutex_enter(&w->worker_lock);
4853 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
4854 stmf_worker_loop:;
4855 	if ((w->worker_ref_count == 0) &&
4856 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4857 		w->worker_flags &= ~(STMF_WORKER_STARTED |
4858 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
4859 		w->worker_tid = NULL;
4860 		mutex_exit(&w->worker_lock);
4861 		thread_exit();
4862 	}
4863 	/* CONSTCOND */
4864 	while (1) {
4865 		dec_qdepth = 0;
4866 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
4867 			wait_timer = 0;
4868 			if (w->worker_wait_head) {
4869 				ASSERT(w->worker_wait_tail);
4870 				if (w->worker_task_head == NULL)
4871 					w->worker_task_head =
4872 					    w->worker_wait_head;
4873 				else
4874 					w->worker_task_tail->itask_worker_next =
4875 					    w->worker_wait_head;
4876 				w->worker_task_tail = w->worker_wait_tail;
4877 				w->worker_wait_head = w->worker_wait_tail =
4878 				    NULL;
4879 			}
4880 		}
4881 		if ((itask = w->worker_task_head) == NULL) {
4882 			break;
4883 		}
4884 		task = itask->itask_task;
4885 		w->worker_task_head = itask->itask_worker_next;
4886 		if (w->worker_task_head == NULL)
4887 			w->worker_task_tail = NULL;
4888 
4889 		wait_queue = 0;
4890 		abort_free = 0;
4891 		if (itask->itask_ncmds > 0) {
4892 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
4893 		} else {
4894 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
4895 		}
4896 		do {
4897 			old = itask->itask_flags;
4898 			if (old & ITASK_BEING_ABORTED) {
4899 				itask->itask_ncmds = 1;
4900 				curcmd = itask->itask_cmd_stack[0] =
4901 				    ITASK_CMD_ABORT;
4902 				goto out_itask_flag_loop;
4903 			} else if ((curcmd & ITASK_CMD_MASK) ==
4904 			    ITASK_CMD_NEW_TASK) {
4905 				new = old | ITASK_KNOWN_TO_LU;
4906 			} else {
4907 				goto out_itask_flag_loop;
4908 			}
4909 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4910 
4911 out_itask_flag_loop:
4912 
4913 		/*
4914 		 * Decide if this task needs to go to a queue and/or if
4915 		 * we can decrement the itask_cmd_stack.
4916 		 */
4917 		if (curcmd == ITASK_CMD_ABORT) {
4918 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
4919 			    ITASK_KNOWN_TO_TGT_PORT)) {
4920 				wait_queue = 1;
4921 			} else {
4922 				abort_free = 1;
4923 			}
4924 		} else if ((curcmd & ITASK_CMD_POLL) &&
4925 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
4926 			wait_queue = 1;
4927 		}
4928 
4929 		if (wait_queue) {
4930 			itask->itask_worker_next = NULL;
4931 			if (w->worker_wait_tail) {
4932 				w->worker_wait_tail->itask_worker_next = itask;
4933 			} else {
4934 				w->worker_wait_head = itask;
4935 			}
4936 			w->worker_wait_tail = itask;
4937 			if (wait_timer == 0) {
4938 				wait_timer = ddi_get_lbolt() + wait_ticks;
4939 			}
4940 		} else if ((--(itask->itask_ncmds)) != 0) {
4941 			itask->itask_worker_next = NULL;
4942 			if (w->worker_task_tail) {
4943 				w->worker_task_tail->itask_worker_next = itask;
4944 			} else {
4945 				w->worker_task_head = itask;
4946 			}
4947 			w->worker_task_tail = itask;
4948 		} else {
4949 			atomic_and_32(&itask->itask_flags,
4950 			    ~ITASK_IN_WORKER_QUEUE);
4951 			/*
4952 			 * This is where the queue depth should go down by
4953 			 * one but we delay that on purpose to account for
4954 			 * the call into the provider. The actual decrement
4955 			 * happens after the worker has done its job.
4956 			 */
4957 			dec_qdepth = 1;
4958 		}
4959 
4960 		/* We made it here means we are going to call LU */
4961 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
4962 			lu = task->task_lu;
4963 		else
4964 			lu = dlun0;
4965 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
4966 		mutex_exit(&w->worker_lock);
4967 		curcmd &= ITASK_CMD_MASK;
4968 		switch (curcmd) {
4969 		case ITASK_CMD_NEW_TASK:
4970 			iss = (stmf_i_scsi_session_t *)
4971 			    task->task_session->ss_stmf_private;
4972 			stmf_update_kstat_lu_q(task, kstat_waitq_to_runq);
4973 			stmf_update_kstat_lport_q(task, kstat_waitq_to_runq);
4974 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
4975 				if (stmf_handle_cmd_during_ic(itask))
4976 					break;
4977 			}
4978 #ifdef	DEBUG
4979 			if (stmf_drop_task_counter > 0) {
4980 				if (atomic_add_32_nv(
4981 				    (uint32_t *)&stmf_drop_task_counter,
4982 				    -1) == 1) {
4983 					break;
4984 				}
4985 			}
4986 #endif
4987 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
4988 			lu->lu_new_task(task, dbuf);
4989 			break;
4990 		case ITASK_CMD_DATA_XFER_DONE:
4991 			lu->lu_dbuf_xfer_done(task, dbuf);
4992 			break;
4993 		case ITASK_CMD_STATUS_DONE:
4994 			lu->lu_send_status_done(task);
4995 			break;
4996 		case ITASK_CMD_ABORT:
4997 			if (abort_free) {
4998 				stmf_task_free(task);
4999 			} else {
5000 				stmf_do_task_abort(task);
5001 			}
5002 			break;
5003 		case ITASK_CMD_POLL_LU:
5004 			if (!wait_queue) {
5005 				lu->lu_task_poll(task);
5006 			}
5007 			break;
5008 		case ITASK_CMD_POLL_LPORT:
5009 			if (!wait_queue)
5010 				task->task_lport->lport_task_poll(task);
5011 			break;
5012 		case ITASK_CMD_SEND_STATUS:
5013 		/* case ITASK_CMD_XFER_DATA: */
5014 			break;
5015 		}
5016 		mutex_enter(&w->worker_lock);
5017 		if (dec_qdepth) {
5018 			w->worker_queue_depth--;
5019 		}
5020 	}
5021 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
5022 		if (w->worker_ref_count == 0)
5023 			goto stmf_worker_loop;
5024 		else
5025 			wait_timer = ddi_get_lbolt() + 1;
5026 	}
5027 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
5028 	if (wait_timer) {
5029 		(void) cv_timedwait(&w->worker_cv, &w->worker_lock, wait_timer);
5030 	} else {
5031 		cv_wait(&w->worker_cv, &w->worker_lock);
5032 	}
5033 	w->worker_flags |= STMF_WORKER_ACTIVE;
5034 	goto stmf_worker_loop;
5035 }
5036 
5037 void
5038 stmf_worker_mgmt()
5039 {
5040 	int i;
5041 	int workers_needed;
5042 	uint32_t qd;
5043 	clock_t tps, d = 0;
5044 	uint32_t cur_max_ntasks = 0;
5045 	stmf_worker_t *w;
5046 
5047 	/* Check if we are trying to increase the # of threads */
5048 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
5049 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
5050 			stmf_nworkers_cur++;
5051 			stmf_nworkers_accepting_cmds++;
5052 		} else {
5053 			/* Wait for transition to complete */
5054 			return;
5055 		}
5056 	}
5057 	/* Check if we are trying to decrease the # of workers */
5058 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
5059 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
5060 			stmf_nworkers_cur--;
5061 			/*
5062 			 * stmf_nworkers_accepting_cmds has already been
5063 			 * updated by the request to reduce the # of workers.
5064 			 */
5065 		} else {
5066 			/* Wait for transition to complete */
5067 			return;
5068 		}
5069 	}
5070 	/* Check if we are being asked to quit */
5071 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
5072 		if (stmf_nworkers_cur) {
5073 			workers_needed = 0;
5074 			goto worker_mgmt_trigger_change;
5075 		}
5076 		return;
5077 	}
5078 	/* Check if we are starting */
5079 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
5080 		workers_needed = stmf_i_min_nworkers;
5081 		goto worker_mgmt_trigger_change;
5082 	}
5083 
5084 	tps = drv_usectohz(1 * 1000 * 1000);
5085 	if ((stmf_wm_last != 0) &&
5086 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
5087 		qd = 0;
5088 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
5089 			qd += stmf_workers[i].worker_max_qdepth_pu;
5090 			stmf_workers[i].worker_max_qdepth_pu = 0;
5091 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
5092 			    cur_max_ntasks) {
5093 				cur_max_ntasks =
5094 				    stmf_workers[i].worker_max_sys_qdepth_pu;
5095 			}
5096 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
5097 		}
5098 	}
5099 	stmf_wm_last = ddi_get_lbolt();
5100 	if (d <= tps) {
5101 		/* still ramping up */
5102 		return;
5103 	}
5104 	/* max qdepth cannot be more than max tasks */
5105 	if (qd > cur_max_ntasks)
5106 		qd = cur_max_ntasks;
5107 
5108 	/* See if we have more workers */
5109 	if (qd < stmf_nworkers_accepting_cmds) {
5110 		/*
5111 		 * Since we dont reduce the worker count right away, monitor
5112 		 * the highest load during the scale_down_delay.
5113 		 */
5114 		if (qd > stmf_worker_scale_down_qd)
5115 			stmf_worker_scale_down_qd = qd;
5116 		if (stmf_worker_scale_down_timer == 0) {
5117 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
5118 			    drv_usectohz(stmf_worker_scale_down_delay *
5119 			    1000 * 1000);
5120 			return;
5121 		}
5122 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
5123 			return;
5124 		}
5125 		/* Its time to reduce the workers */
5126 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
5127 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
5128 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
5129 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
5130 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
5131 			return;
5132 		workers_needed = stmf_worker_scale_down_qd;
5133 		stmf_worker_scale_down_qd = 0;
5134 		goto worker_mgmt_trigger_change;
5135 	}
5136 	stmf_worker_scale_down_qd = 0;
5137 	stmf_worker_scale_down_timer = 0;
5138 	if (qd > stmf_i_max_nworkers)
5139 		qd = stmf_i_max_nworkers;
5140 	if (qd < stmf_i_min_nworkers)
5141 		qd = stmf_i_min_nworkers;
5142 	if (qd == stmf_nworkers_cur)
5143 		return;
5144 	workers_needed = qd;
5145 	goto worker_mgmt_trigger_change;
5146 
5147 	/* NOTREACHED */
5148 	return;
5149 
5150 worker_mgmt_trigger_change:
5151 	ASSERT(workers_needed != stmf_nworkers_cur);
5152 	if (workers_needed > stmf_nworkers_cur) {
5153 		stmf_nworkers_needed = workers_needed;
5154 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
5155 			w = &stmf_workers[i];
5156 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
5157 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
5158 			    minclsyspri);
5159 		}
5160 		return;
5161 	}
5162 	/* At this point we know that we are decreasing the # of workers */
5163 	stmf_nworkers_accepting_cmds = workers_needed;
5164 	stmf_nworkers_needed = workers_needed;
5165 	/* Signal the workers that its time to quit */
5166 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
5167 		w = &stmf_workers[i];
5168 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
5169 		mutex_enter(&w->worker_lock);
5170 		w->worker_flags |= STMF_WORKER_TERMINATE;
5171 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5172 			cv_signal(&w->worker_cv);
5173 		mutex_exit(&w->worker_lock);
5174 	}
5175 }
5176 
5177 /*
5178  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
5179  * If all the data has been filled out, frees the xd and makes
5180  * db_lu_private NULL.
5181  */
5182 void
5183 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf)
5184 {
5185 	stmf_xfer_data_t *xd;
5186 	uint8_t *p;
5187 	int i;
5188 	uint32_t s;
5189 
5190 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
5191 	dbuf->db_data_size = 0;
5192 	dbuf->db_relative_offset = xd->size_done;
5193 	for (i = 0; i < dbuf->db_sglist_length; i++) {
5194 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
5195 		p = &xd->buf[xd->size_done];
5196 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
5197 		xd->size_left -= s;
5198 		xd->size_done += s;
5199 		dbuf->db_data_size += s;
5200 		if (xd->size_left == 0) {
5201 			kmem_free(xd, xd->alloc_size);
5202 			dbuf->db_lu_private = NULL;
5203 			return;
5204 		}
5205 	}
5206 }
5207 
5208 /* ARGSUSED */
5209 stmf_status_t
5210 stmf_dlun0_task_alloc(scsi_task_t *task)
5211 {
5212 	return (STMF_SUCCESS);
5213 }
5214 
5215 void
5216 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
5217 {
5218 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
5219 	stmf_i_scsi_session_t *iss;
5220 	uint32_t sz, minsz;
5221 	uint8_t *p;
5222 	stmf_xfer_data_t *xd;
5223 	uint8_t inq_page_length = 31;
5224 
5225 	if (task->task_mgmt_function) {
5226 		stmf_scsilib_handle_task_mgmt(task);
5227 		return;
5228 	}
5229 
5230 	switch (cdbp[0]) {
5231 	case SCMD_INQUIRY:
5232 		/*
5233 		 * Basic protocol checks.  In addition, only reply to
5234 		 * standard inquiry.  Otherwise, the LU provider needs
5235 		 * to respond.
5236 		 */
5237 
5238 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
5239 			stmf_scsilib_send_status(task, STATUS_CHECK,
5240 			    STMF_SAA_INVALID_FIELD_IN_CDB);
5241 			return;
5242 		}
5243 
5244 		task->task_cmd_xfer_length =
5245 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
5246 
5247 		if (task->task_additional_flags &
5248 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5249 			task->task_expected_xfer_length =
5250 			    task->task_cmd_xfer_length;
5251 		}
5252 
5253 		sz = min(task->task_expected_xfer_length,
5254 		    min(36, task->task_cmd_xfer_length));
5255 		minsz = 36;
5256 
5257 		if (sz == 0) {
5258 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5259 			return;
5260 		}
5261 
5262 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
5263 			/*
5264 			 * Ignore any preallocated dbuf if the size is less
5265 			 * than 36. It will be freed during the task_free.
5266 			 */
5267 			dbuf = NULL;
5268 		}
5269 		if (dbuf == NULL)
5270 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
5271 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
5272 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5273 			    STMF_ALLOC_FAILURE, NULL);
5274 			return;
5275 		}
5276 		dbuf->db_lu_private = NULL;
5277 
5278 		p = dbuf->db_sglist[0].seg_addr;
5279 
5280 		/*
5281 		 * Standard inquiry handling only.
5282 		 */
5283 
5284 		bzero(p, inq_page_length + 5);
5285 
5286 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
5287 		p[2] = 5;
5288 		p[3] = 0x12;
5289 		p[4] = inq_page_length;
5290 		p[6] = 0x80;
5291 
5292 		(void) strncpy((char *)p+8, "SUN     ", 8);
5293 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
5294 		(void) strncpy((char *)p+32, "1.0 ", 4);
5295 
5296 		dbuf->db_data_size = sz;
5297 		dbuf->db_relative_offset = 0;
5298 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5299 		(void) stmf_xfer_data(task, dbuf, 0);
5300 
5301 		return;
5302 
5303 	case SCMD_REPORT_LUNS:
5304 		task->task_cmd_xfer_length =
5305 		    ((((uint32_t)task->task_cdb[6]) << 24) |
5306 		    (((uint32_t)task->task_cdb[7]) << 16) |
5307 		    (((uint32_t)task->task_cdb[8]) << 8) |
5308 		    ((uint32_t)task->task_cdb[9]));
5309 
5310 		if (task->task_additional_flags &
5311 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5312 			task->task_expected_xfer_length =
5313 			    task->task_cmd_xfer_length;
5314 		}
5315 
5316 		sz = min(task->task_expected_xfer_length,
5317 		    task->task_cmd_xfer_length);
5318 
5319 		if (sz < 16) {
5320 			stmf_scsilib_send_status(task, STATUS_CHECK,
5321 			    STMF_SAA_INVALID_FIELD_IN_CDB);
5322 			return;
5323 		}
5324 
5325 		iss = (stmf_i_scsi_session_t *)
5326 		    task->task_session->ss_stmf_private;
5327 		rw_enter(iss->iss_lockp, RW_WRITER);
5328 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
5329 		rw_exit(iss->iss_lockp);
5330 
5331 		if (xd == NULL) {
5332 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5333 			    STMF_ALLOC_FAILURE, NULL);
5334 			return;
5335 		}
5336 
5337 		sz = min(sz, xd->size_left);
5338 		xd->size_left = sz;
5339 		minsz = min(512, sz);
5340 
5341 		if (dbuf == NULL)
5342 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
5343 		if (dbuf == NULL) {
5344 			kmem_free(xd, xd->alloc_size);
5345 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5346 			    STMF_ALLOC_FAILURE, NULL);
5347 			return;
5348 		}
5349 		dbuf->db_lu_private = xd;
5350 		stmf_xd_to_dbuf(dbuf);
5351 
5352 		atomic_and_32(&iss->iss_flags,
5353 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
5354 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5355 		(void) stmf_xfer_data(task, dbuf, 0);
5356 		return;
5357 	}
5358 
5359 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
5360 }
5361 
5362 void
5363 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
5364 {
5365 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
5366 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5367 		    dbuf->db_xfer_status, NULL);
5368 		return;
5369 	}
5370 	task->task_nbytes_transferred = dbuf->db_data_size;
5371 	if (dbuf->db_lu_private) {
5372 		/* There is more */
5373 		stmf_xd_to_dbuf(dbuf);
5374 		(void) stmf_xfer_data(task, dbuf, 0);
5375 		return;
5376 	}
5377 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5378 }
5379 
5380 /* ARGSUSED */
5381 void
5382 stmf_dlun0_status_done(scsi_task_t *task)
5383 {
5384 }
5385 
5386 /* ARGSUSED */
5387 void
5388 stmf_dlun0_task_free(scsi_task_t *task)
5389 {
5390 }
5391 
5392 /* ARGSUSED */
5393 stmf_status_t
5394 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
5395 {
5396 	scsi_task_t *task = (scsi_task_t *)arg;
5397 	stmf_i_scsi_task_t *itask =
5398 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5399 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5400 	int i;
5401 	uint8_t map;
5402 
5403 	ASSERT(abort_cmd == STMF_LU_ABORT_TASK);
5404 	if ((task->task_mgmt_function) && (itask->itask_flags &
5405 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
5406 		switch (task->task_mgmt_function) {
5407 		case TM_ABORT_TASK:
5408 		case TM_ABORT_TASK_SET:
5409 		case TM_CLEAR_TASK_SET:
5410 		case TM_LUN_RESET:
5411 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5412 			break;
5413 		case TM_TARGET_RESET:
5414 		case TM_TARGET_COLD_RESET:
5415 		case TM_TARGET_WARM_RESET:
5416 			stmf_abort_target_reset(task);
5417 			break;
5418 		}
5419 		return (STMF_ABORT_SUCCESS);
5420 	}
5421 
5422 	/*
5423 	 * OK so its not a task mgmt. Make sure we free any xd sitting
5424 	 * inside any dbuf.
5425 	 */
5426 	if ((map = itask->itask_allocated_buf_map) != 0) {
5427 		for (i = 0; i < 4; i++) {
5428 			if ((map & 1) &&
5429 			    ((itask->itask_dbufs[i])->db_lu_private)) {
5430 				stmf_xfer_data_t *xd;
5431 				stmf_data_buf_t *dbuf;
5432 
5433 				dbuf = itask->itask_dbufs[i];
5434 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
5435 				dbuf->db_lu_private = NULL;
5436 				kmem_free(xd, xd->alloc_size);
5437 			}
5438 			map >>= 1;
5439 		}
5440 	}
5441 	return (STMF_ABORT_SUCCESS);
5442 }
5443 
5444 void
5445 stmf_dlun0_task_poll(struct scsi_task *task)
5446 {
5447 	/* Right now we only do this for handling task management functions */
5448 	ASSERT(task->task_mgmt_function);
5449 
5450 	switch (task->task_mgmt_function) {
5451 	case TM_ABORT_TASK:
5452 	case TM_ABORT_TASK_SET:
5453 	case TM_CLEAR_TASK_SET:
5454 	case TM_LUN_RESET:
5455 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
5456 		return;
5457 	case TM_TARGET_RESET:
5458 	case TM_TARGET_COLD_RESET:
5459 	case TM_TARGET_WARM_RESET:
5460 		stmf_target_reset_poll(task);
5461 		return;
5462 	}
5463 }
5464 
5465 /* ARGSUSED */
5466 void
5467 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
5468 {
5469 	/* This function will never be called */
5470 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
5471 }
5472 
5473 void
5474 stmf_dlun_init()
5475 {
5476 	stmf_i_lu_t *ilu;
5477 
5478 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
5479 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
5480 	dlun0->lu_new_task = stmf_dlun0_new_task;
5481 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
5482 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
5483 	dlun0->lu_task_free = stmf_dlun0_task_free;
5484 	dlun0->lu_abort = stmf_dlun0_abort;
5485 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
5486 	dlun0->lu_ctl = stmf_dlun0_ctl;
5487 
5488 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
5489 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
5490 }
5491 
5492 stmf_status_t
5493 stmf_dlun_fini()
5494 {
5495 	stmf_i_lu_t *ilu;
5496 
5497 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
5498 
5499 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
5500 	if (ilu->ilu_ntasks) {
5501 		stmf_i_scsi_task_t *itask, *nitask;
5502 
5503 		nitask = ilu->ilu_tasks;
5504 		do {
5505 			itask = nitask;
5506 			nitask = itask->itask_lu_next;
5507 			dlun0->lu_task_free(itask->itask_task);
5508 			stmf_free(itask->itask_task);
5509 		} while (nitask != NULL);
5510 
5511 	}
5512 	stmf_free(dlun0);
5513 	return (STMF_SUCCESS);
5514 }
5515 
5516 void
5517 stmf_abort_target_reset(scsi_task_t *task)
5518 {
5519 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
5520 	    task->task_session->ss_stmf_private;
5521 	stmf_lun_map_t *lm;
5522 	stmf_lun_map_ent_t *lm_ent;
5523 	stmf_i_lu_t *ilu;
5524 	int i;
5525 
5526 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
5527 
5528 	rw_enter(iss->iss_lockp, RW_READER);
5529 	lm = iss->iss_sm;
5530 	for (i = 0; i < lm->lm_nentries; i++) {
5531 		if (lm->lm_plus[i] == NULL)
5532 			continue;
5533 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
5534 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
5535 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5536 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5537 		}
5538 	}
5539 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
5540 	rw_exit(iss->iss_lockp);
5541 }
5542 
5543 /*
5544  * The return value is only used by function managing target reset.
5545  */
5546 stmf_status_t
5547 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
5548 {
5549 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5550 	int ntasks_pending;
5551 
5552 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
5553 	/*
5554 	 * This function is also used during Target reset. The idea is that
5555 	 * once all the commands are aborted, call the LU's reset entry
5556 	 * point (abort entry point with a reset flag). But if this Task
5557 	 * mgmt is running on this LU then all the tasks cannot be aborted.
5558 	 * one task (this task) will still be running which is OK.
5559 	 */
5560 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
5561 	    (ntasks_pending == 1))) {
5562 		stmf_status_t ret;
5563 
5564 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
5565 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
5566 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
5567 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
5568 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
5569 		} else {
5570 			ret = STMF_SUCCESS;
5571 		}
5572 		if (ret == STMF_SUCCESS) {
5573 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5574 		}
5575 		if (target_reset) {
5576 			return (ret);
5577 		}
5578 		if (ret == STMF_SUCCESS) {
5579 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5580 			return (ret);
5581 		}
5582 		if (ret != STMF_BUSY) {
5583 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
5584 			return (ret);
5585 		}
5586 	}
5587 
5588 	if (target_reset) {
5589 		/* Tell target reset polling code that we are not done */
5590 		return (STMF_BUSY);
5591 	}
5592 
5593 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5594 	    != STMF_SUCCESS) {
5595 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5596 		    STMF_ALLOC_FAILURE, NULL);
5597 		return (STMF_SUCCESS);
5598 	}
5599 
5600 	return (STMF_SUCCESS);
5601 }
5602 
5603 void
5604 stmf_target_reset_poll(struct scsi_task *task)
5605 {
5606 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
5607 	    task->task_session->ss_stmf_private;
5608 	stmf_lun_map_t *lm;
5609 	stmf_lun_map_ent_t *lm_ent;
5610 	stmf_i_lu_t *ilu;
5611 	stmf_status_t ret;
5612 	int i;
5613 	int not_done = 0;
5614 
5615 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
5616 
5617 	rw_enter(iss->iss_lockp, RW_READER);
5618 	lm = iss->iss_sm;
5619 	for (i = 0; i < lm->lm_nentries; i++) {
5620 		if (lm->lm_plus[i] == NULL)
5621 			continue;
5622 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
5623 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
5624 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5625 			rw_exit(iss->iss_lockp);
5626 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
5627 			rw_enter(iss->iss_lockp, RW_READER);
5628 			if (ret == STMF_SUCCESS)
5629 				continue;
5630 			not_done = 1;
5631 			if (ret != STMF_BUSY) {
5632 				rw_exit(iss->iss_lockp);
5633 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5634 				    STMF_ABORTED, NULL);
5635 				return;
5636 			}
5637 		}
5638 	}
5639 	rw_exit(iss->iss_lockp);
5640 
5641 	if (not_done) {
5642 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5643 		    != STMF_SUCCESS) {
5644 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5645 			    STMF_ALLOC_FAILURE, NULL);
5646 			return;
5647 		}
5648 		return;
5649 	}
5650 
5651 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
5652 
5653 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5654 }
5655 
5656 stmf_status_t
5657 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
5658 {
5659 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5660 
5661 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5662 		return (STMF_INVALID_ARG);
5663 	}
5664 
5665 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
5666 	return (STMF_SUCCESS);
5667 }
5668 
5669 stmf_status_t
5670 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
5671 {
5672 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5673 
5674 	if (eventid == STMF_EVENT_ALL) {
5675 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
5676 		return (STMF_SUCCESS);
5677 	}
5678 
5679 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5680 		return (STMF_INVALID_ARG);
5681 	}
5682 
5683 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
5684 	return (STMF_SUCCESS);
5685 }
5686 
5687 stmf_status_t
5688 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
5689 {
5690 	stmf_i_local_port_t *ilport =
5691 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5692 
5693 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5694 		return (STMF_INVALID_ARG);
5695 	}
5696 
5697 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
5698 	return (STMF_SUCCESS);
5699 }
5700 
5701 stmf_status_t
5702 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
5703 {
5704 	stmf_i_local_port_t *ilport =
5705 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5706 
5707 	if (eventid == STMF_EVENT_ALL) {
5708 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
5709 		return (STMF_SUCCESS);
5710 	}
5711 
5712 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5713 		return (STMF_INVALID_ARG);
5714 	}
5715 
5716 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
5717 	return (STMF_SUCCESS);
5718 }
5719 
5720 void
5721 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
5722 {
5723 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
5724 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
5725 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
5726 	}
5727 }
5728 
5729 void
5730 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
5731 				uint32_t flags)
5732 {
5733 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
5734 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
5735 		ilport->ilport_lport->lport_event_handler(
5736 		    ilport->ilport_lport, eventid, arg, flags);
5737 	}
5738 }
5739 
5740 void
5741 stmf_svc_init()
5742 {
5743 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5744 		return;
5745 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
5746 	    TASKQ_DEFAULTPRI, 0);
5747 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
5748 	    stmf_svc, 0, DDI_SLEEP);
5749 }
5750 
5751 stmf_status_t
5752 stmf_svc_fini()
5753 {
5754 	uint32_t i;
5755 
5756 	mutex_enter(&stmf_state.stmf_lock);
5757 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
5758 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
5759 		cv_signal(&stmf_state.stmf_cv);
5760 	}
5761 	mutex_exit(&stmf_state.stmf_lock);
5762 
5763 	/* Wait for 5 seconds */
5764 	for (i = 0; i < 500; i++) {
5765 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5766 			delay(drv_usectohz(10000));
5767 		else
5768 			break;
5769 	}
5770 	if (i == 500)
5771 		return (STMF_BUSY);
5772 
5773 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
5774 
5775 	return (STMF_SUCCESS);
5776 }
5777 
5778 /* ARGSUSED */
5779 void
5780 stmf_svc(void *arg)
5781 {
5782 	stmf_svc_req_t *req, **preq;
5783 	clock_t td;
5784 	clock_t	drain_start, drain_next = 0;
5785 	clock_t	timing_start, timing_next = 0;
5786 	clock_t worker_delay = 0;
5787 	int deq;
5788 	stmf_lu_t *lu;
5789 	stmf_i_lu_t *ilu;
5790 	stmf_local_port_t *lport;
5791 	stmf_i_local_port_t *ilport, *next_ilport;
5792 	stmf_i_scsi_session_t *iss;
5793 
5794 	td = drv_usectohz(20000);
5795 
5796 	mutex_enter(&stmf_state.stmf_lock);
5797 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
5798 
5799 stmf_svc_loop:
5800 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
5801 		stmf_state.stmf_svc_flags &=
5802 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
5803 		mutex_exit(&stmf_state.stmf_lock);
5804 		return;
5805 	}
5806 
5807 	if (stmf_state.stmf_svc_active) {
5808 		int waitq_add = 0;
5809 		req = stmf_state.stmf_svc_active;
5810 		stmf_state.stmf_svc_active = req->svc_next;
5811 
5812 		switch (req->svc_cmd) {
5813 		case STMF_CMD_LPORT_ONLINE:
5814 			/* Fallthrough */
5815 		case STMF_CMD_LPORT_OFFLINE:
5816 			/* Fallthrough */
5817 		case STMF_CMD_LU_ONLINE:
5818 			/* Nothing to do */
5819 			waitq_add = 1;
5820 			break;
5821 
5822 		case STMF_CMD_LU_OFFLINE:
5823 			/* Remove all mappings of this LU */
5824 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
5825 			/* Kill all the pending I/Os for this LU */
5826 			mutex_exit(&stmf_state.stmf_lock);
5827 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
5828 			    STMF_ABORTED);
5829 			mutex_enter(&stmf_state.stmf_lock);
5830 			waitq_add = 1;
5831 			break;
5832 		default:
5833 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
5834 			    req->svc_cmd);
5835 		}
5836 
5837 		if (waitq_add) {
5838 			/* Put it in the wait queue */
5839 			req->svc_next = stmf_state.stmf_svc_waiting;
5840 			stmf_state.stmf_svc_waiting = req;
5841 		}
5842 	}
5843 
5844 	/* The waiting list is not going to be modified by anybody else */
5845 	mutex_exit(&stmf_state.stmf_lock);
5846 
5847 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
5848 		req = *preq;
5849 		deq = 0;
5850 		switch (req->svc_cmd) {
5851 		case STMF_CMD_LU_ONLINE:
5852 			lu = (stmf_lu_t *)req->svc_obj;
5853 			deq = 1;
5854 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5855 			break;
5856 
5857 		case STMF_CMD_LU_OFFLINE:
5858 			lu = (stmf_lu_t *)req->svc_obj;
5859 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5860 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
5861 				break;
5862 			deq = 1;
5863 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5864 			break;
5865 
5866 		case STMF_CMD_LPORT_OFFLINE:
5867 			/* Fallthrough */
5868 		case STMF_CMD_LPORT_ONLINE:
5869 			lport = (stmf_local_port_t *)req->svc_obj;
5870 			deq = 1;
5871 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
5872 			break;
5873 		}
5874 		if (deq) {
5875 			*preq = req->svc_next;
5876 			kmem_free(req, req->svc_req_alloc_size);
5877 		} else {
5878 			preq = &req->svc_next;
5879 		}
5880 	}
5881 
5882 	mutex_enter(&stmf_state.stmf_lock);
5883 	if (stmf_state.stmf_svc_active == NULL) {
5884 		/* Do timeouts */
5885 		if (stmf_state.stmf_nlus &&
5886 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
5887 			if (!stmf_state.stmf_svc_ilu_timing) {
5888 				/* we are starting a new round */
5889 				stmf_state.stmf_svc_ilu_timing =
5890 				    stmf_state.stmf_ilulist;
5891 				timing_start = ddi_get_lbolt();
5892 			}
5893 			stmf_check_ilu_timing();
5894 			if (!stmf_state.stmf_svc_ilu_timing) {
5895 				/* we finished a complete round */
5896 				timing_next =
5897 				    timing_start + drv_usectohz(5*1000*1000);
5898 			} else {
5899 				/* we still have some ilu items to check */
5900 				timing_next =
5901 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5902 			}
5903 			if (stmf_state.stmf_svc_active)
5904 				goto stmf_svc_loop;
5905 		}
5906 		/* Check if there are free tasks to clear */
5907 		if (stmf_state.stmf_nlus &&
5908 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
5909 			if (!stmf_state.stmf_svc_ilu_draining) {
5910 				/* we are starting a new round */
5911 				stmf_state.stmf_svc_ilu_draining =
5912 				    stmf_state.stmf_ilulist;
5913 				drain_start = ddi_get_lbolt();
5914 			}
5915 			stmf_check_freetask();
5916 			if (!stmf_state.stmf_svc_ilu_draining) {
5917 				/* we finished a complete round */
5918 				drain_next =
5919 				    drain_start + drv_usectohz(10*1000*1000);
5920 			} else {
5921 				/* we still have some ilu items to check */
5922 				drain_next =
5923 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5924 			}
5925 			if (stmf_state.stmf_svc_active)
5926 				goto stmf_svc_loop;
5927 		}
5928 
5929 		/* Check if we need to run worker_mgmt */
5930 		if (ddi_get_lbolt() > worker_delay) {
5931 			stmf_worker_mgmt();
5932 			worker_delay = ddi_get_lbolt() +
5933 			    stmf_worker_mgmt_delay;
5934 		}
5935 
5936 		/* Check if any active session got its 1st LUN */
5937 		if (stmf_state.stmf_process_initial_luns) {
5938 			int stmf_level = 0;
5939 			int port_level;
5940 			for (ilport = stmf_state.stmf_ilportlist; ilport;
5941 			    ilport = next_ilport) {
5942 				next_ilport = ilport->ilport_next;
5943 				if ((ilport->ilport_flags &
5944 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
5945 					continue;
5946 				}
5947 				port_level = 0;
5948 				rw_enter(&ilport->ilport_lock, RW_READER);
5949 				for (iss = ilport->ilport_ss_list; iss;
5950 				    iss = iss->iss_next) {
5951 					if ((iss->iss_flags &
5952 					    ISS_GOT_INITIAL_LUNS) == 0) {
5953 						continue;
5954 					}
5955 					port_level++;
5956 					stmf_level++;
5957 					atomic_and_32(&iss->iss_flags,
5958 					    ~ISS_GOT_INITIAL_LUNS);
5959 					atomic_or_32(&iss->iss_flags,
5960 					    ISS_EVENT_ACTIVE);
5961 					rw_exit(&ilport->ilport_lock);
5962 					mutex_exit(&stmf_state.stmf_lock);
5963 					stmf_generate_lport_event(ilport,
5964 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
5965 					    iss->iss_ss, 0);
5966 					atomic_and_32(&iss->iss_flags,
5967 					    ~ISS_EVENT_ACTIVE);
5968 					mutex_enter(&stmf_state.stmf_lock);
5969 					/*
5970 					 * scan all the ilports again as the
5971 					 * ilport list might have changed.
5972 					 */
5973 					next_ilport =
5974 					    stmf_state.stmf_ilportlist;
5975 					break;
5976 				}
5977 				if (port_level == 0) {
5978 					atomic_and_32(&ilport->ilport_flags,
5979 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
5980 				}
5981 				/* drop the lock if we are holding it. */
5982 				if (rw_lock_held(&ilport->ilport_lock))
5983 					rw_exit(&ilport->ilport_lock);
5984 
5985 				/* Max 4 session at a time */
5986 				if (stmf_level >= 4) {
5987 					break;
5988 				}
5989 			}
5990 			if (stmf_level == 0) {
5991 				stmf_state.stmf_process_initial_luns = 0;
5992 			}
5993 		}
5994 
5995 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
5996 		(void) cv_timedwait(&stmf_state.stmf_cv, &stmf_state.stmf_lock,
5997 		    ddi_get_lbolt() + td);
5998 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
5999 	}
6000 	goto stmf_svc_loop;
6001 }
6002 
6003 void
6004 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
6005 {
6006 	stmf_svc_req_t *req;
6007 	int s;
6008 
6009 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
6010 	s = sizeof (stmf_svc_req_t);
6011 	if (info->st_additional_info) {
6012 		s += strlen(info->st_additional_info) + 1;
6013 	}
6014 	req = kmem_zalloc(s, KM_SLEEP);
6015 
6016 	req->svc_cmd = cmd;
6017 	req->svc_obj = obj;
6018 	req->svc_info.st_rflags = info->st_rflags;
6019 	if (info->st_additional_info) {
6020 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
6021 		    sizeof (stmf_svc_req_t)));
6022 		(void) strcpy(req->svc_info.st_additional_info,
6023 		    info->st_additional_info);
6024 	}
6025 	req->svc_req_alloc_size = s;
6026 
6027 	mutex_enter(&stmf_state.stmf_lock);
6028 	req->svc_next = stmf_state.stmf_svc_active;
6029 	stmf_state.stmf_svc_active = req;
6030 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
6031 		cv_signal(&stmf_state.stmf_cv);
6032 	}
6033 	mutex_exit(&stmf_state.stmf_lock);
6034 }
6035 
6036 void
6037 stmf_trace(caddr_t ident, const char *fmt, ...)
6038 {
6039 	va_list args;
6040 	char tbuf[160];
6041 	int len;
6042 
6043 	if (!stmf_trace_on)
6044 		return;
6045 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
6046 	    ddi_get_lbolt());
6047 	va_start(args, fmt);
6048 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
6049 	va_end(args);
6050 
6051 	if (len > 158) {
6052 		len = 158;
6053 	}
6054 	tbuf[len++] = '\n';
6055 	tbuf[len] = 0;
6056 
6057 	mutex_enter(&trace_buf_lock);
6058 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
6059 	trace_buf_curndx += len;
6060 	if (trace_buf_curndx > (trace_buf_size - 320))
6061 		trace_buf_curndx = 0;
6062 	mutex_exit(&trace_buf_lock);
6063 }
6064 
6065 void
6066 stmf_trace_clear()
6067 {
6068 	if (!stmf_trace_on)
6069 		return;
6070 	mutex_enter(&trace_buf_lock);
6071 	trace_buf_curndx = 0;
6072 	if (trace_buf_size > 0)
6073 		stmf_trace_buf[0] = 0;
6074 	mutex_exit(&trace_buf_lock);
6075 }
6076 
6077 static void
6078 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
6079 {
6080 	stmf_state_change_info_t	 change_info;
6081 	void				*ctl_private;
6082 	uint32_t			 ctl_cmd;
6083 	int				msg = 0;
6084 
6085 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
6086 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
6087 	change_info.st_additional_info = info;
6088 	if (offline_lu) {
6089 		change_info.st_rflags = STMF_RFLAG_RESET |
6090 		    STMF_RFLAG_LU_ABORT;
6091 		ctl_private = task->task_lu;
6092 		if (((stmf_i_lu_t *)
6093 		    task->task_lu->lu_stmf_private)->ilu_state ==
6094 		    STMF_STATE_ONLINE) {
6095 			msg = 1;
6096 		}
6097 		ctl_cmd = STMF_CMD_LU_OFFLINE;
6098 	} else {
6099 		change_info.st_rflags = STMF_RFLAG_RESET |
6100 		    STMF_RFLAG_LPORT_ABORT;
6101 		ctl_private = task->task_lport;
6102 		if (((stmf_i_local_port_t *)
6103 		    task->task_lport->lport_stmf_private)->ilport_state ==
6104 		    STMF_STATE_ONLINE) {
6105 			msg = 1;
6106 		}
6107 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
6108 	}
6109 
6110 	if (msg) {
6111 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
6112 		    offline_lu ? "LU" : "LPORT", info ? info :
6113 		    "<no additional info>");
6114 	}
6115 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
6116 }
6117