xref: /titanic_41/usr/src/uts/common/io/comstar/stmf/stmf.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
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 th 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 	sdid->protocol_id = protocol_id;
4272 	sdid->piv = 1;
4273 	sdid->code_set = CODE_SET_ASCII;
4274 	sdid->association = ID_IS_TARGET_PORT;
4275 	sdid->ident_length = 20;
4276 	(void) sprintf((char *)sdid->ident,
4277 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
4278 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
4279 }
4280 
4281 stmf_xfer_data_t *
4282 stmf_prepare_tpgs_data()
4283 {
4284 	stmf_xfer_data_t *xd;
4285 	stmf_i_local_port_t *ilport;
4286 	uint8_t *p;
4287 	uint32_t sz, asz, nports;
4288 
4289 	mutex_enter(&stmf_state.stmf_lock);
4290 	/* The spec only allows for 255 ports to be reported */
4291 	nports = min(stmf_state.stmf_nlports, 255);
4292 	sz = (nports * 4) + 12;
4293 	asz = sz + sizeof (*xd) - 4;
4294 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
4295 	if (xd == NULL) {
4296 		mutex_exit(&stmf_state.stmf_lock);
4297 		return (NULL);
4298 	}
4299 	xd->alloc_size = asz;
4300 	xd->size_left = sz;
4301 
4302 	p = xd->buf;
4303 
4304 	*((uint32_t *)p) = BE_32(sz - 4);
4305 	p += 4;
4306 	p[0] = 0x80;	/* PREF */
4307 	p[1] = 1;	/* AO_SUP */
4308 	p[7] = nports & 0xff;
4309 	p += 8;
4310 	for (ilport = stmf_state.stmf_ilportlist; ilport && nports;
4311 	    nports++, ilport = ilport->ilport_next, p += 4) {
4312 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
4313 	}
4314 	mutex_exit(&stmf_state.stmf_lock);
4315 
4316 	return (xd);
4317 }
4318 
4319 struct scsi_devid_desc *
4320 stmf_scsilib_get_devid_desc(uint16_t rtpid)
4321 {
4322 	scsi_devid_desc_t *devid = NULL;
4323 	stmf_i_local_port_t *ilport;
4324 
4325 	mutex_enter(&stmf_state.stmf_lock);
4326 
4327 	for (ilport = stmf_state.stmf_ilportlist; ilport;
4328 	    ilport = ilport->ilport_next) {
4329 		if (ilport->ilport_rtpid == rtpid) {
4330 			scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
4331 			uint32_t id_sz = sizeof (scsi_devid_desc_t) - 1 +
4332 			    id->ident_length;
4333 			devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
4334 			    KM_NOSLEEP);
4335 			if (devid != NULL) {
4336 				bcopy(id, devid, id_sz);
4337 			}
4338 			break;
4339 		}
4340 	}
4341 
4342 	mutex_exit(&stmf_state.stmf_lock);
4343 	return (devid);
4344 }
4345 
4346 uint16_t
4347 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
4348 {
4349 	stmf_i_local_port_t	*ilport;
4350 	scsi_devid_desc_t	*id;
4351 	uint16_t		rtpid = 0;
4352 
4353 	mutex_enter(&stmf_state.stmf_lock);
4354 	for (ilport = stmf_state.stmf_ilportlist; ilport;
4355 	    ilport = ilport->ilport_next) {
4356 		id = ilport->ilport_lport->lport_id;
4357 		if ((devid->ident_length == id->ident_length) &&
4358 		    (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
4359 			rtpid = ilport->ilport_rtpid;
4360 			break;
4361 		}
4362 	}
4363 	mutex_exit(&stmf_state.stmf_lock);
4364 	return (rtpid);
4365 }
4366 
4367 static uint16_t stmf_lu_id_gen_number = 0;
4368 
4369 stmf_status_t
4370 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
4371 {
4372 	uint8_t *p;
4373 	struct timeval32 timestamp32;
4374 	uint32_t *t = (uint32_t *)&timestamp32;
4375 	struct ether_addr mac;
4376 	uint8_t *e = (uint8_t *)&mac;
4377 
4378 	if (company_id == COMPANY_ID_NONE)
4379 		company_id = COMPANY_ID_SUN;
4380 
4381 	if (lu_id->ident_length != 0x10)
4382 		return (STMF_INVALID_ARG);
4383 
4384 	p = (uint8_t *)lu_id;
4385 
4386 	atomic_add_16(&stmf_lu_id_gen_number, 1);
4387 
4388 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
4389 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
4390 	p[5] = (company_id >> 12) & 0xff;
4391 	p[6] = (company_id >> 4) & 0xff;
4392 	p[7] = (company_id << 4) & 0xf0;
4393 	if (!localetheraddr((struct ether_addr *)NULL, &mac)) {
4394 		int hid = BE_32((int)zone_get_hostid(NULL));
4395 		e[0] = (hid >> 24) & 0xff;
4396 		e[1] = (hid >> 16) & 0xff;
4397 		e[2] = (hid >> 8) & 0xff;
4398 		e[3] = hid & 0xff;
4399 		e[4] = e[5] = 0;
4400 	}
4401 	bcopy(e, p+8, 6);
4402 	uniqtime32(&timestamp32);
4403 	*t = BE_32(*t);
4404 	bcopy(t, p+14, 4);
4405 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
4406 	p[19] = stmf_lu_id_gen_number & 0xff;
4407 
4408 	return (STMF_SUCCESS);
4409 }
4410 
4411 /*
4412  * saa is sense key, ASC, ASCQ
4413  */
4414 void
4415 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
4416 {
4417 	uint8_t sd[18];
4418 	task->task_scsi_status = st;
4419 	if (st == 2) {
4420 		bzero(sd, 18);
4421 		sd[0] = 0x70;
4422 		sd[2] = (saa >> 16) & 0xf;
4423 		sd[7] = 10;
4424 		sd[12] = (saa >> 8) & 0xff;
4425 		sd[13] = saa & 0xff;
4426 		task->task_sense_data = sd;
4427 		task->task_sense_length = 18;
4428 	} else {
4429 		task->task_sense_data = NULL;
4430 		task->task_sense_length = 0;
4431 	}
4432 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
4433 }
4434 
4435 uint32_t
4436 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
4437     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
4438 {
4439 	uint8_t		*p = NULL;
4440 	uint8_t		small_buf[32];
4441 	uint32_t	sz = 0;
4442 	uint32_t	n = 4;
4443 	uint32_t	m = 0;
4444 	uint32_t	last_bit = 0;
4445 
4446 	if (page_len < 4)
4447 		return (0);
4448 	if (page_len > 65535)
4449 		page_len = 65535;
4450 
4451 	page[0] = byte0;
4452 	page[1] = 0x83;
4453 
4454 	/* CONSTCOND */
4455 	while (1) {
4456 		m += sz;
4457 		if (sz && (page_len > n)) {
4458 			uint32_t copysz;
4459 			copysz = page_len > (n + sz) ? sz : page_len - n;
4460 			bcopy(p, page + n, copysz);
4461 			n += copysz;
4462 		}
4463 		vpd_mask &= ~last_bit;
4464 		if (vpd_mask == 0)
4465 			break;
4466 
4467 		if (vpd_mask & STMF_VPD_LU_ID) {
4468 			last_bit = STMF_VPD_LU_ID;
4469 			sz = task->task_lu->lu_id->ident_length + 4;
4470 			p = (uint8_t *)task->task_lu->lu_id;
4471 			continue;
4472 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
4473 			last_bit = STMF_VPD_TARGET_ID;
4474 			sz = task->task_lport->lport_id->ident_length + 4;
4475 			p = (uint8_t *)task->task_lport->lport_id;
4476 			continue;
4477 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
4478 			last_bit = STMF_VPD_TP_GROUP;
4479 			p = small_buf;
4480 			bzero(p, 8);
4481 			p[0] = 1;
4482 			p[1] = 0x15;
4483 			p[3] = 4;
4484 			/* Group ID is always 0 */
4485 			sz = 8;
4486 			continue;
4487 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
4488 			stmf_i_local_port_t *ilport;
4489 
4490 			last_bit = STMF_VPD_RELATIVE_TP_ID;
4491 			p = small_buf;
4492 			bzero(p, 8);
4493 			p[0] = 1;
4494 			p[1] = 0x14;
4495 			p[3] = 4;
4496 			ilport = (stmf_i_local_port_t *)
4497 			    task->task_lport->lport_stmf_private;
4498 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
4499 			p[7] = ilport->ilport_rtpid & 0xff;
4500 			sz = 8;
4501 			continue;
4502 		} else {
4503 			cmn_err(CE_WARN, "Invalid vpd_mask");
4504 			break;
4505 		}
4506 	}
4507 
4508 	page[2] = (m >> 8) & 0xff;
4509 	page[3] = m & 0xff;
4510 
4511 	return (n);
4512 }
4513 
4514 void
4515 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
4516 {
4517 	stmf_i_scsi_task_t *itask =
4518 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4519 	stmf_xfer_data_t *xd;
4520 	uint32_t sz, minsz;
4521 
4522 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
4523 	task->task_cmd_xfer_length =
4524 	    ((((uint32_t)task->task_cdb[6]) << 24) |
4525 	    (((uint32_t)task->task_cdb[7]) << 16) |
4526 	    (((uint32_t)task->task_cdb[8]) << 8) |
4527 	    ((uint32_t)task->task_cdb[9]));
4528 
4529 	if (task->task_additional_flags &
4530 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4531 		task->task_expected_xfer_length =
4532 		    task->task_cmd_xfer_length;
4533 	}
4534 
4535 	if (task->task_cmd_xfer_length < 16) {
4536 		stmf_scsilib_send_status(task, STATUS_CHECK,
4537 		    STMF_SAA_INVALID_FIELD_IN_CDB);
4538 		return;
4539 	}
4540 
4541 	sz = min(task->task_expected_xfer_length,
4542 	    task->task_cmd_xfer_length);
4543 
4544 	xd = stmf_prepare_tpgs_data();
4545 
4546 	if (xd == NULL) {
4547 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4548 		    STMF_ALLOC_FAILURE, NULL);
4549 		return;
4550 	}
4551 
4552 	sz = min(sz, xd->size_left);
4553 	xd->size_left = sz;
4554 	minsz = min(512, sz);
4555 
4556 	if (dbuf == NULL)
4557 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
4558 	if (dbuf == NULL) {
4559 		kmem_free(xd, xd->alloc_size);
4560 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4561 		    STMF_ALLOC_FAILURE, NULL);
4562 		return;
4563 	}
4564 	dbuf->db_lu_private = xd;
4565 	stmf_xd_to_dbuf(dbuf);
4566 
4567 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
4568 	(void) stmf_xfer_data(task, dbuf, 0);
4569 
4570 }
4571 
4572 void
4573 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
4574 {
4575 	switch (task->task_mgmt_function) {
4576 	/*
4577 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
4578 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
4579 	 * in these cases. This needs to be changed to abort only the required
4580 	 * set.
4581 	 */
4582 	case TM_ABORT_TASK:
4583 	case TM_ABORT_TASK_SET:
4584 	case TM_CLEAR_TASK_SET:
4585 	case TM_LUN_RESET:
4586 		stmf_handle_lun_reset(task);
4587 		return;
4588 	case TM_TARGET_RESET:
4589 	case TM_TARGET_COLD_RESET:
4590 	case TM_TARGET_WARM_RESET:
4591 		stmf_handle_target_reset(task);
4592 		return;
4593 	default:
4594 		/* We dont support this task mgmt function */
4595 		stmf_scsilib_send_status(task, STATUS_CHECK,
4596 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
4597 		return;
4598 	}
4599 }
4600 
4601 void
4602 stmf_handle_lun_reset(scsi_task_t *task)
4603 {
4604 	stmf_i_scsi_task_t *itask;
4605 	stmf_i_lu_t *ilu;
4606 
4607 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4608 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4609 
4610 	/*
4611 	 * To sync with target reset, grab this lock. The LU is not going
4612 	 * anywhere as there is atleast one task pending (this task).
4613 	 */
4614 	mutex_enter(&stmf_state.stmf_lock);
4615 
4616 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4617 		mutex_exit(&stmf_state.stmf_lock);
4618 		stmf_scsilib_send_status(task, STATUS_CHECK,
4619 		    STMF_SAA_OPERATION_IN_PROGRESS);
4620 		return;
4621 	}
4622 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4623 	mutex_exit(&stmf_state.stmf_lock);
4624 
4625 	/*
4626 	 * Mark this task as the one causing LU reset so that we know who
4627 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
4628 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
4629 	 */
4630 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
4631 
4632 	/* Initiatiate abort on all commands on this LU except this one */
4633 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
4634 
4635 	/* Start polling on this task */
4636 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4637 	    != STMF_SUCCESS) {
4638 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4639 		    NULL);
4640 		return;
4641 	}
4642 }
4643 
4644 void
4645 stmf_handle_target_reset(scsi_task_t *task)
4646 {
4647 	stmf_i_scsi_task_t *itask;
4648 	stmf_i_lu_t *ilu;
4649 	stmf_i_scsi_session_t *iss;
4650 	stmf_lun_map_t *lm;
4651 	stmf_lun_map_ent_t *lm_ent;
4652 	int i, lf;
4653 
4654 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4655 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
4656 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4657 
4658 	/*
4659 	 * To sync with LUN reset, grab this lock. The session is not going
4660 	 * anywhere as there is atleast one task pending (this task).
4661 	 */
4662 	mutex_enter(&stmf_state.stmf_lock);
4663 
4664 	/* Grab the session lock as a writer to prevent any changes in it */
4665 	rw_enter(iss->iss_lockp, RW_WRITER);
4666 
4667 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
4668 		rw_exit(iss->iss_lockp);
4669 		mutex_exit(&stmf_state.stmf_lock);
4670 		stmf_scsilib_send_status(task, STATUS_CHECK,
4671 		    STMF_SAA_OPERATION_IN_PROGRESS);
4672 		return;
4673 	}
4674 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
4675 
4676 	/*
4677 	 * Now go through each LUN in this session and make sure all of them
4678 	 * can be reset.
4679 	 */
4680 	lm = iss->iss_sm;
4681 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
4682 		if (lm->lm_plus[i] == NULL)
4683 			continue;
4684 		lf++;
4685 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4686 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4687 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4688 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4689 			rw_exit(iss->iss_lockp);
4690 			mutex_exit(&stmf_state.stmf_lock);
4691 			stmf_scsilib_send_status(task, STATUS_CHECK,
4692 			    STMF_SAA_OPERATION_IN_PROGRESS);
4693 			return;
4694 		}
4695 	}
4696 	if (lf == 0) {
4697 		/* No luns in this session */
4698 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4699 		rw_exit(iss->iss_lockp);
4700 		mutex_exit(&stmf_state.stmf_lock);
4701 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
4702 		return;
4703 	}
4704 
4705 	/* ok, start the damage */
4706 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
4707 	    ITASK_CAUSING_TARGET_RESET;
4708 	for (i = 0; i < lm->lm_nentries; i++) {
4709 		if (lm->lm_plus[i] == NULL)
4710 			continue;
4711 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4712 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4713 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4714 	}
4715 	rw_exit(iss->iss_lockp);
4716 	mutex_exit(&stmf_state.stmf_lock);
4717 
4718 	for (i = 0; i < lm->lm_nentries; i++) {
4719 		if (lm->lm_plus[i] == NULL)
4720 			continue;
4721 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4722 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
4723 		    lm_ent->ent_lu);
4724 	}
4725 
4726 	/* Start polling on this task */
4727 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4728 	    != STMF_SUCCESS) {
4729 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4730 		    NULL);
4731 		return;
4732 	}
4733 }
4734 
4735 int
4736 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
4737 {
4738 	scsi_task_t *task = itask->itask_task;
4739 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4740 	    task->task_session->ss_stmf_private;
4741 
4742 	rw_enter(iss->iss_lockp, RW_WRITER);
4743 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
4744 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
4745 		rw_exit(iss->iss_lockp);
4746 		return (0);
4747 	}
4748 	atomic_and_32(&iss->iss_flags,
4749 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
4750 	rw_exit(iss->iss_lockp);
4751 
4752 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4753 		return (0);
4754 	}
4755 	stmf_scsilib_send_status(task, STATUS_CHECK,
4756 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
4757 	return (1);
4758 }
4759 
4760 void
4761 stmf_worker_init()
4762 {
4763 	uint32_t i;
4764 
4765 	/* Make local copy of global tunables */
4766 	stmf_i_max_nworkers = stmf_max_nworkers;
4767 	stmf_i_min_nworkers = stmf_min_nworkers;
4768 
4769 	ASSERT(stmf_workers == NULL);
4770 	if (stmf_i_min_nworkers < 4) {
4771 		stmf_i_min_nworkers = 4;
4772 	}
4773 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
4774 		stmf_i_max_nworkers = stmf_i_min_nworkers;
4775 	}
4776 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
4777 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
4778 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4779 		stmf_worker_t *w = &stmf_workers[i];
4780 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
4781 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
4782 	}
4783 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4784 	stmf_workers_state = STMF_WORKERS_ENABLED;
4785 
4786 	/* Workers will be started by stmf_worker_mgmt() */
4787 
4788 	/* Lets wait for atleast one worker to start */
4789 	while (stmf_nworkers_cur == 0)
4790 		delay(drv_usectohz(20 * 1000));
4791 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
4792 }
4793 
4794 stmf_status_t
4795 stmf_worker_fini()
4796 {
4797 	int i;
4798 	clock_t sb;
4799 
4800 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
4801 		return (STMF_SUCCESS);
4802 	ASSERT(stmf_workers);
4803 	stmf_workers_state = STMF_WORKERS_DISABLED;
4804 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4805 	cv_signal(&stmf_state.stmf_cv);
4806 
4807 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
4808 	/* Wait for all the threads to die */
4809 	while (stmf_nworkers_cur != 0) {
4810 		if (ddi_get_lbolt() > sb) {
4811 			stmf_workers_state = STMF_WORKERS_ENABLED;
4812 			return (STMF_BUSY);
4813 		}
4814 		delay(drv_usectohz(100 * 1000));
4815 	}
4816 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4817 		stmf_worker_t *w = &stmf_workers[i];
4818 		mutex_destroy(&w->worker_lock);
4819 		cv_destroy(&w->worker_cv);
4820 	}
4821 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
4822 	stmf_workers = NULL;
4823 
4824 	return (STMF_SUCCESS);
4825 }
4826 
4827 void
4828 stmf_worker_task(void *arg)
4829 {
4830 	stmf_worker_t *w;
4831 	stmf_i_scsi_session_t *iss;
4832 	scsi_task_t *task;
4833 	stmf_i_scsi_task_t *itask;
4834 	stmf_data_buf_t *dbuf;
4835 	stmf_lu_t *lu;
4836 	clock_t wait_timer = 0;
4837 	clock_t wait_ticks;
4838 	uint32_t old, new;
4839 	uint8_t curcmd;
4840 	uint8_t abort_free;
4841 	uint8_t wait_queue;
4842 	uint8_t dec_qdepth;
4843 
4844 	w = (stmf_worker_t *)arg;
4845 	wait_ticks = drv_usectohz(10000);
4846 
4847 	mutex_enter(&w->worker_lock);
4848 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
4849 stmf_worker_loop:;
4850 	if ((w->worker_ref_count == 0) &&
4851 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4852 		w->worker_flags &= ~(STMF_WORKER_STARTED |
4853 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
4854 		w->worker_tid = NULL;
4855 		mutex_exit(&w->worker_lock);
4856 		thread_exit();
4857 	}
4858 	/* CONSTCOND */
4859 	while (1) {
4860 		dec_qdepth = 0;
4861 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
4862 			wait_timer = 0;
4863 			if (w->worker_wait_head) {
4864 				ASSERT(w->worker_wait_tail);
4865 				if (w->worker_task_head == NULL)
4866 					w->worker_task_head =
4867 					    w->worker_wait_head;
4868 				else
4869 					w->worker_task_tail->itask_worker_next =
4870 					    w->worker_wait_head;
4871 				w->worker_task_tail = w->worker_wait_tail;
4872 				w->worker_wait_head = w->worker_wait_tail =
4873 				    NULL;
4874 			}
4875 		}
4876 		if ((itask = w->worker_task_head) == NULL) {
4877 			break;
4878 		}
4879 		task = itask->itask_task;
4880 		w->worker_task_head = itask->itask_worker_next;
4881 		if (w->worker_task_head == NULL)
4882 			w->worker_task_tail = NULL;
4883 
4884 		wait_queue = 0;
4885 		abort_free = 0;
4886 		if (itask->itask_ncmds > 0) {
4887 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
4888 		} else {
4889 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
4890 		}
4891 		do {
4892 			old = itask->itask_flags;
4893 			if (old & ITASK_BEING_ABORTED) {
4894 				itask->itask_ncmds = 1;
4895 				curcmd = itask->itask_cmd_stack[0] =
4896 				    ITASK_CMD_ABORT;
4897 				goto out_itask_flag_loop;
4898 			} else if ((curcmd & ITASK_CMD_MASK) ==
4899 			    ITASK_CMD_NEW_TASK) {
4900 				new = old | ITASK_KNOWN_TO_LU;
4901 			} else {
4902 				goto out_itask_flag_loop;
4903 			}
4904 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4905 
4906 out_itask_flag_loop:
4907 
4908 		/*
4909 		 * Decide if this task needs to go to a queue and/or if
4910 		 * we can decrement the itask_cmd_stack.
4911 		 */
4912 		if (curcmd == ITASK_CMD_ABORT) {
4913 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
4914 			    ITASK_KNOWN_TO_TGT_PORT)) {
4915 				wait_queue = 1;
4916 			} else {
4917 				abort_free = 1;
4918 			}
4919 		} else if ((curcmd & ITASK_CMD_POLL) &&
4920 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
4921 			wait_queue = 1;
4922 		}
4923 
4924 		if (wait_queue) {
4925 			itask->itask_worker_next = NULL;
4926 			if (w->worker_wait_tail) {
4927 				w->worker_wait_tail->itask_worker_next = itask;
4928 			} else {
4929 				w->worker_wait_head = itask;
4930 			}
4931 			w->worker_wait_tail = itask;
4932 			if (wait_timer == 0) {
4933 				wait_timer = ddi_get_lbolt() + wait_ticks;
4934 			}
4935 		} else if ((--(itask->itask_ncmds)) != 0) {
4936 			itask->itask_worker_next = NULL;
4937 			if (w->worker_task_tail) {
4938 				w->worker_task_tail->itask_worker_next = itask;
4939 			} else {
4940 				w->worker_task_head = itask;
4941 			}
4942 			w->worker_task_tail = itask;
4943 		} else {
4944 			atomic_and_32(&itask->itask_flags,
4945 			    ~ITASK_IN_WORKER_QUEUE);
4946 			/*
4947 			 * This is where the queue depth should go down by
4948 			 * one but we delay that on purpose to account for
4949 			 * the call into the provider. The actual decrement
4950 			 * happens after the worker has done its job.
4951 			 */
4952 			dec_qdepth = 1;
4953 		}
4954 
4955 		/* We made it here means we are going to call LU */
4956 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
4957 			lu = task->task_lu;
4958 		else
4959 			lu = dlun0;
4960 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
4961 		mutex_exit(&w->worker_lock);
4962 		curcmd &= ITASK_CMD_MASK;
4963 		switch (curcmd) {
4964 		case ITASK_CMD_NEW_TASK:
4965 			iss = (stmf_i_scsi_session_t *)
4966 			    task->task_session->ss_stmf_private;
4967 			stmf_update_kstat_lu_q(task, kstat_waitq_to_runq);
4968 			stmf_update_kstat_lport_q(task, kstat_waitq_to_runq);
4969 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
4970 				if (stmf_handle_cmd_during_ic(itask))
4971 					break;
4972 			}
4973 #ifdef	DEBUG
4974 			if (stmf_drop_task_counter > 0) {
4975 				if (atomic_add_32_nv(
4976 				    (uint32_t *)&stmf_drop_task_counter,
4977 				    -1) == 1) {
4978 					break;
4979 				}
4980 			}
4981 #endif
4982 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
4983 			lu->lu_new_task(task, dbuf);
4984 			break;
4985 		case ITASK_CMD_DATA_XFER_DONE:
4986 			lu->lu_dbuf_xfer_done(task, dbuf);
4987 			break;
4988 		case ITASK_CMD_STATUS_DONE:
4989 			lu->lu_send_status_done(task);
4990 			break;
4991 		case ITASK_CMD_ABORT:
4992 			if (abort_free) {
4993 				stmf_task_free(task);
4994 			} else {
4995 				stmf_do_task_abort(task);
4996 			}
4997 			break;
4998 		case ITASK_CMD_POLL_LU:
4999 			if (!wait_queue) {
5000 				lu->lu_task_poll(task);
5001 			}
5002 			break;
5003 		case ITASK_CMD_POLL_LPORT:
5004 			if (!wait_queue)
5005 				task->task_lport->lport_task_poll(task);
5006 			break;
5007 		case ITASK_CMD_SEND_STATUS:
5008 		/* case ITASK_CMD_XFER_DATA: */
5009 			break;
5010 		}
5011 		mutex_enter(&w->worker_lock);
5012 		if (dec_qdepth) {
5013 			w->worker_queue_depth--;
5014 		}
5015 	}
5016 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
5017 		if (w->worker_ref_count == 0)
5018 			goto stmf_worker_loop;
5019 		else
5020 			wait_timer = ddi_get_lbolt() + 1;
5021 	}
5022 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
5023 	if (wait_timer) {
5024 		(void) cv_timedwait(&w->worker_cv, &w->worker_lock, wait_timer);
5025 	} else {
5026 		cv_wait(&w->worker_cv, &w->worker_lock);
5027 	}
5028 	w->worker_flags |= STMF_WORKER_ACTIVE;
5029 	goto stmf_worker_loop;
5030 }
5031 
5032 void
5033 stmf_worker_mgmt()
5034 {
5035 	int i;
5036 	int workers_needed;
5037 	uint32_t qd;
5038 	clock_t tps, d = 0;
5039 	uint32_t cur_max_ntasks = 0;
5040 	stmf_worker_t *w;
5041 
5042 	/* Check if we are trying to increase the # of threads */
5043 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
5044 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
5045 			stmf_nworkers_cur++;
5046 			stmf_nworkers_accepting_cmds++;
5047 		} else {
5048 			/* Wait for transition to complete */
5049 			return;
5050 		}
5051 	}
5052 	/* Check if we are trying to decrease the # of workers */
5053 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
5054 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
5055 			stmf_nworkers_cur--;
5056 			/*
5057 			 * stmf_nworkers_accepting_cmds has already been
5058 			 * updated by the request to reduce the # of workers.
5059 			 */
5060 		} else {
5061 			/* Wait for transition to complete */
5062 			return;
5063 		}
5064 	}
5065 	/* Check if we are being asked to quit */
5066 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
5067 		if (stmf_nworkers_cur) {
5068 			workers_needed = 0;
5069 			goto worker_mgmt_trigger_change;
5070 		}
5071 		return;
5072 	}
5073 	/* Check if we are starting */
5074 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
5075 		workers_needed = stmf_i_min_nworkers;
5076 		goto worker_mgmt_trigger_change;
5077 	}
5078 
5079 	tps = drv_usectohz(1 * 1000 * 1000);
5080 	if ((stmf_wm_last != 0) &&
5081 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
5082 		qd = 0;
5083 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
5084 			qd += stmf_workers[i].worker_max_qdepth_pu;
5085 			stmf_workers[i].worker_max_qdepth_pu = 0;
5086 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
5087 			    cur_max_ntasks) {
5088 				cur_max_ntasks =
5089 				    stmf_workers[i].worker_max_sys_qdepth_pu;
5090 			}
5091 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
5092 		}
5093 	}
5094 	stmf_wm_last = ddi_get_lbolt();
5095 	if (d <= tps) {
5096 		/* still ramping up */
5097 		return;
5098 	}
5099 	/* max qdepth cannot be more than max tasks */
5100 	if (qd > cur_max_ntasks)
5101 		qd = cur_max_ntasks;
5102 
5103 	/* See if we have more workers */
5104 	if (qd < stmf_nworkers_accepting_cmds) {
5105 		/*
5106 		 * Since we dont reduce the worker count right away, monitor
5107 		 * the highest load during the scale_down_delay.
5108 		 */
5109 		if (qd > stmf_worker_scale_down_qd)
5110 			stmf_worker_scale_down_qd = qd;
5111 		if (stmf_worker_scale_down_timer == 0) {
5112 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
5113 			    drv_usectohz(stmf_worker_scale_down_delay *
5114 			    1000 * 1000);
5115 			return;
5116 		}
5117 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
5118 			return;
5119 		}
5120 		/* Its time to reduce the workers */
5121 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
5122 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
5123 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
5124 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
5125 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
5126 			return;
5127 		workers_needed = stmf_worker_scale_down_qd;
5128 		stmf_worker_scale_down_qd = 0;
5129 		goto worker_mgmt_trigger_change;
5130 	}
5131 	stmf_worker_scale_down_qd = 0;
5132 	stmf_worker_scale_down_timer = 0;
5133 	if (qd > stmf_i_max_nworkers)
5134 		qd = stmf_i_max_nworkers;
5135 	if (qd < stmf_i_min_nworkers)
5136 		qd = stmf_i_min_nworkers;
5137 	if (qd == stmf_nworkers_cur)
5138 		return;
5139 	workers_needed = qd;
5140 	goto worker_mgmt_trigger_change;
5141 
5142 	/* NOTREACHED */
5143 	return;
5144 
5145 worker_mgmt_trigger_change:
5146 	ASSERT(workers_needed != stmf_nworkers_cur);
5147 	if (workers_needed > stmf_nworkers_cur) {
5148 		stmf_nworkers_needed = workers_needed;
5149 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
5150 			w = &stmf_workers[i];
5151 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
5152 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
5153 			    minclsyspri);
5154 		}
5155 		return;
5156 	}
5157 	/* At this point we know that we are decreasing the # of workers */
5158 	stmf_nworkers_accepting_cmds = workers_needed;
5159 	stmf_nworkers_needed = workers_needed;
5160 	/* Signal the workers that its time to quit */
5161 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
5162 		w = &stmf_workers[i];
5163 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
5164 		mutex_enter(&w->worker_lock);
5165 		w->worker_flags |= STMF_WORKER_TERMINATE;
5166 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5167 			cv_signal(&w->worker_cv);
5168 		mutex_exit(&w->worker_lock);
5169 	}
5170 }
5171 
5172 /*
5173  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
5174  * If all the data has been filled out, frees the xd and makes
5175  * db_lu_private NULL.
5176  */
5177 void
5178 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf)
5179 {
5180 	stmf_xfer_data_t *xd;
5181 	uint8_t *p;
5182 	int i;
5183 	uint32_t s;
5184 
5185 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
5186 	dbuf->db_data_size = 0;
5187 	dbuf->db_relative_offset = xd->size_done;
5188 	for (i = 0; i < dbuf->db_sglist_length; i++) {
5189 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
5190 		p = &xd->buf[xd->size_done];
5191 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
5192 		xd->size_left -= s;
5193 		xd->size_done += s;
5194 		dbuf->db_data_size += s;
5195 		if (xd->size_left == 0) {
5196 			kmem_free(xd, xd->alloc_size);
5197 			dbuf->db_lu_private = NULL;
5198 			return;
5199 		}
5200 	}
5201 }
5202 
5203 /* ARGSUSED */
5204 stmf_status_t
5205 stmf_dlun0_task_alloc(scsi_task_t *task)
5206 {
5207 	return (STMF_SUCCESS);
5208 }
5209 
5210 void
5211 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
5212 {
5213 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
5214 	stmf_i_scsi_session_t *iss;
5215 	uint32_t sz, minsz;
5216 	uint8_t *p;
5217 	stmf_xfer_data_t *xd;
5218 	uint8_t inq_page_length = 31;
5219 
5220 	if (task->task_mgmt_function) {
5221 		stmf_scsilib_handle_task_mgmt(task);
5222 		return;
5223 	}
5224 
5225 	switch (cdbp[0]) {
5226 	case SCMD_INQUIRY:
5227 		/*
5228 		 * Basic protocol checks.  In addition, only reply to
5229 		 * standard inquiry.  Otherwise, the LU provider needs
5230 		 * to respond.
5231 		 */
5232 
5233 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
5234 			stmf_scsilib_send_status(task, STATUS_CHECK,
5235 			    STMF_SAA_INVALID_FIELD_IN_CDB);
5236 			return;
5237 		}
5238 
5239 		task->task_cmd_xfer_length =
5240 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
5241 
5242 		if (task->task_additional_flags &
5243 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5244 			task->task_expected_xfer_length =
5245 			    task->task_cmd_xfer_length;
5246 		}
5247 
5248 		sz = min(task->task_expected_xfer_length,
5249 		    min(36, task->task_cmd_xfer_length));
5250 		minsz = 36;
5251 
5252 		if (sz == 0) {
5253 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5254 			return;
5255 		}
5256 
5257 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
5258 			/*
5259 			 * Ignore any preallocated dbuf if the size is less
5260 			 * than 36. It will be freed during the task_free.
5261 			 */
5262 			dbuf = NULL;
5263 		}
5264 		if (dbuf == NULL)
5265 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
5266 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
5267 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5268 			    STMF_ALLOC_FAILURE, NULL);
5269 			return;
5270 		}
5271 		dbuf->db_lu_private = NULL;
5272 
5273 		p = dbuf->db_sglist[0].seg_addr;
5274 
5275 		/*
5276 		 * Standard inquiry handling only.
5277 		 */
5278 
5279 		bzero(p, inq_page_length + 5);
5280 
5281 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
5282 		p[2] = 5;
5283 		p[3] = 0x12;
5284 		p[4] = inq_page_length;
5285 		p[6] = 0x80;
5286 
5287 		(void) strncpy((char *)p+8, "SUN     ", 8);
5288 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
5289 		(void) strncpy((char *)p+32, "1.0 ", 4);
5290 
5291 		dbuf->db_data_size = sz;
5292 		dbuf->db_relative_offset = 0;
5293 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5294 		(void) stmf_xfer_data(task, dbuf, 0);
5295 
5296 		return;
5297 
5298 	case SCMD_REPORT_LUNS:
5299 		task->task_cmd_xfer_length =
5300 		    ((((uint32_t)task->task_cdb[6]) << 24) |
5301 		    (((uint32_t)task->task_cdb[7]) << 16) |
5302 		    (((uint32_t)task->task_cdb[8]) << 8) |
5303 		    ((uint32_t)task->task_cdb[9]));
5304 
5305 		if (task->task_additional_flags &
5306 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5307 			task->task_expected_xfer_length =
5308 			    task->task_cmd_xfer_length;
5309 		}
5310 
5311 		sz = min(task->task_expected_xfer_length,
5312 		    task->task_cmd_xfer_length);
5313 
5314 		if (sz < 16) {
5315 			stmf_scsilib_send_status(task, STATUS_CHECK,
5316 			    STMF_SAA_INVALID_FIELD_IN_CDB);
5317 			return;
5318 		}
5319 
5320 		iss = (stmf_i_scsi_session_t *)
5321 		    task->task_session->ss_stmf_private;
5322 		rw_enter(iss->iss_lockp, RW_WRITER);
5323 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
5324 		rw_exit(iss->iss_lockp);
5325 
5326 		if (xd == NULL) {
5327 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5328 			    STMF_ALLOC_FAILURE, NULL);
5329 			return;
5330 		}
5331 
5332 		sz = min(sz, xd->size_left);
5333 		xd->size_left = sz;
5334 		minsz = min(512, sz);
5335 
5336 		if (dbuf == NULL)
5337 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
5338 		if (dbuf == NULL) {
5339 			kmem_free(xd, xd->alloc_size);
5340 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5341 			    STMF_ALLOC_FAILURE, NULL);
5342 			return;
5343 		}
5344 		dbuf->db_lu_private = xd;
5345 		stmf_xd_to_dbuf(dbuf);
5346 
5347 		atomic_and_32(&iss->iss_flags,
5348 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
5349 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5350 		(void) stmf_xfer_data(task, dbuf, 0);
5351 		return;
5352 	}
5353 
5354 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
5355 }
5356 
5357 void
5358 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
5359 {
5360 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
5361 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5362 		    dbuf->db_xfer_status, NULL);
5363 		return;
5364 	}
5365 	task->task_nbytes_transferred = dbuf->db_data_size;
5366 	if (dbuf->db_lu_private) {
5367 		/* There is more */
5368 		stmf_xd_to_dbuf(dbuf);
5369 		(void) stmf_xfer_data(task, dbuf, 0);
5370 		return;
5371 	}
5372 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5373 }
5374 
5375 /* ARGSUSED */
5376 void
5377 stmf_dlun0_status_done(scsi_task_t *task)
5378 {
5379 }
5380 
5381 /* ARGSUSED */
5382 void
5383 stmf_dlun0_task_free(scsi_task_t *task)
5384 {
5385 }
5386 
5387 /* ARGSUSED */
5388 stmf_status_t
5389 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
5390 {
5391 	scsi_task_t *task = (scsi_task_t *)arg;
5392 	stmf_i_scsi_task_t *itask =
5393 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
5394 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5395 	int i;
5396 	uint8_t map;
5397 
5398 	ASSERT(abort_cmd == STMF_LU_ABORT_TASK);
5399 	if ((task->task_mgmt_function) && (itask->itask_flags &
5400 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
5401 		switch (task->task_mgmt_function) {
5402 		case TM_ABORT_TASK:
5403 		case TM_ABORT_TASK_SET:
5404 		case TM_CLEAR_TASK_SET:
5405 		case TM_LUN_RESET:
5406 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5407 			break;
5408 		case TM_TARGET_RESET:
5409 		case TM_TARGET_COLD_RESET:
5410 		case TM_TARGET_WARM_RESET:
5411 			stmf_abort_target_reset(task);
5412 			break;
5413 		}
5414 		return (STMF_ABORT_SUCCESS);
5415 	}
5416 
5417 	/*
5418 	 * OK so its not a task mgmt. Make sure we free any xd sitting
5419 	 * inside any dbuf.
5420 	 */
5421 	if ((map = itask->itask_allocated_buf_map) != 0) {
5422 		for (i = 0; i < 4; i++) {
5423 			if ((map & 1) &&
5424 			    ((itask->itask_dbufs[i])->db_lu_private)) {
5425 				stmf_xfer_data_t *xd;
5426 				stmf_data_buf_t *dbuf;
5427 
5428 				dbuf = itask->itask_dbufs[i];
5429 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
5430 				dbuf->db_lu_private = NULL;
5431 				kmem_free(xd, xd->alloc_size);
5432 			}
5433 			map >>= 1;
5434 		}
5435 	}
5436 	return (STMF_ABORT_SUCCESS);
5437 }
5438 
5439 void
5440 stmf_dlun0_task_poll(struct scsi_task *task)
5441 {
5442 	/* Right now we only do this for handling task management functions */
5443 	ASSERT(task->task_mgmt_function);
5444 
5445 	switch (task->task_mgmt_function) {
5446 	case TM_ABORT_TASK:
5447 	case TM_ABORT_TASK_SET:
5448 	case TM_CLEAR_TASK_SET:
5449 	case TM_LUN_RESET:
5450 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
5451 		return;
5452 	case TM_TARGET_RESET:
5453 	case TM_TARGET_COLD_RESET:
5454 	case TM_TARGET_WARM_RESET:
5455 		stmf_target_reset_poll(task);
5456 		return;
5457 	}
5458 }
5459 
5460 /* ARGSUSED */
5461 void
5462 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
5463 {
5464 	/* This function will never be called */
5465 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
5466 }
5467 
5468 void
5469 stmf_dlun_init()
5470 {
5471 	stmf_i_lu_t *ilu;
5472 
5473 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
5474 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
5475 	dlun0->lu_new_task = stmf_dlun0_new_task;
5476 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
5477 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
5478 	dlun0->lu_task_free = stmf_dlun0_task_free;
5479 	dlun0->lu_abort = stmf_dlun0_abort;
5480 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
5481 	dlun0->lu_ctl = stmf_dlun0_ctl;
5482 
5483 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
5484 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
5485 }
5486 
5487 stmf_status_t
5488 stmf_dlun_fini()
5489 {
5490 	stmf_i_lu_t *ilu;
5491 
5492 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
5493 
5494 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
5495 	if (ilu->ilu_ntasks) {
5496 		stmf_i_scsi_task_t *itask, *nitask;
5497 
5498 		nitask = ilu->ilu_tasks;
5499 		do {
5500 			itask = nitask;
5501 			nitask = itask->itask_lu_next;
5502 			dlun0->lu_task_free(itask->itask_task);
5503 			stmf_free(itask->itask_task);
5504 		} while (nitask != NULL);
5505 
5506 	}
5507 	stmf_free(dlun0);
5508 	return (STMF_SUCCESS);
5509 }
5510 
5511 void
5512 stmf_abort_target_reset(scsi_task_t *task)
5513 {
5514 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
5515 	    task->task_session->ss_stmf_private;
5516 	stmf_lun_map_t *lm;
5517 	stmf_lun_map_ent_t *lm_ent;
5518 	stmf_i_lu_t *ilu;
5519 	int i;
5520 
5521 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
5522 
5523 	rw_enter(iss->iss_lockp, RW_READER);
5524 	lm = iss->iss_sm;
5525 	for (i = 0; i < lm->lm_nentries; i++) {
5526 		if (lm->lm_plus[i] == NULL)
5527 			continue;
5528 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
5529 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
5530 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5531 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5532 		}
5533 	}
5534 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
5535 	rw_exit(iss->iss_lockp);
5536 }
5537 
5538 /*
5539  * The return value is only used by function managing target reset.
5540  */
5541 stmf_status_t
5542 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
5543 {
5544 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5545 	int ntasks_pending;
5546 
5547 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
5548 	/*
5549 	 * This function is also used during Target reset. The idea is that
5550 	 * once all the commands are aborted, call the LU's reset entry
5551 	 * point (abort entry point with a reset flag). But if this Task
5552 	 * mgmt is running on this LU then all the tasks cannot be aborted.
5553 	 * one task (this task) will still be running which is OK.
5554 	 */
5555 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
5556 	    (ntasks_pending == 1))) {
5557 		stmf_status_t ret;
5558 
5559 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
5560 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
5561 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
5562 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
5563 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
5564 		} else {
5565 			ret = STMF_SUCCESS;
5566 		}
5567 		if (ret == STMF_SUCCESS) {
5568 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5569 		}
5570 		if (target_reset) {
5571 			return (ret);
5572 		}
5573 		if (ret == STMF_SUCCESS) {
5574 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5575 			return (ret);
5576 		}
5577 		if (ret != STMF_BUSY) {
5578 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
5579 			return (ret);
5580 		}
5581 	}
5582 
5583 	if (target_reset) {
5584 		/* Tell target reset polling code that we are not done */
5585 		return (STMF_BUSY);
5586 	}
5587 
5588 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5589 	    != STMF_SUCCESS) {
5590 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5591 		    STMF_ALLOC_FAILURE, NULL);
5592 		return (STMF_SUCCESS);
5593 	}
5594 
5595 	return (STMF_SUCCESS);
5596 }
5597 
5598 void
5599 stmf_target_reset_poll(struct scsi_task *task)
5600 {
5601 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
5602 	    task->task_session->ss_stmf_private;
5603 	stmf_lun_map_t *lm;
5604 	stmf_lun_map_ent_t *lm_ent;
5605 	stmf_i_lu_t *ilu;
5606 	stmf_status_t ret;
5607 	int i;
5608 	int not_done = 0;
5609 
5610 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
5611 
5612 	rw_enter(iss->iss_lockp, RW_READER);
5613 	lm = iss->iss_sm;
5614 	for (i = 0; i < lm->lm_nentries; i++) {
5615 		if (lm->lm_plus[i] == NULL)
5616 			continue;
5617 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
5618 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
5619 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5620 			rw_exit(iss->iss_lockp);
5621 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
5622 			rw_enter(iss->iss_lockp, RW_READER);
5623 			if (ret == STMF_SUCCESS)
5624 				continue;
5625 			not_done = 1;
5626 			if (ret != STMF_BUSY) {
5627 				rw_exit(iss->iss_lockp);
5628 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5629 				    STMF_ABORTED, NULL);
5630 				return;
5631 			}
5632 		}
5633 	}
5634 	rw_exit(iss->iss_lockp);
5635 
5636 	if (not_done) {
5637 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5638 		    != STMF_SUCCESS) {
5639 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5640 			    STMF_ALLOC_FAILURE, NULL);
5641 			return;
5642 		}
5643 		return;
5644 	}
5645 
5646 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
5647 
5648 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5649 }
5650 
5651 stmf_status_t
5652 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
5653 {
5654 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5655 
5656 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5657 		return (STMF_INVALID_ARG);
5658 	}
5659 
5660 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
5661 	return (STMF_SUCCESS);
5662 }
5663 
5664 stmf_status_t
5665 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
5666 {
5667 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5668 
5669 	if (eventid == STMF_EVENT_ALL) {
5670 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
5671 		return (STMF_SUCCESS);
5672 	}
5673 
5674 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5675 		return (STMF_INVALID_ARG);
5676 	}
5677 
5678 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
5679 	return (STMF_SUCCESS);
5680 }
5681 
5682 stmf_status_t
5683 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
5684 {
5685 	stmf_i_local_port_t *ilport =
5686 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5687 
5688 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5689 		return (STMF_INVALID_ARG);
5690 	}
5691 
5692 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
5693 	return (STMF_SUCCESS);
5694 }
5695 
5696 stmf_status_t
5697 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
5698 {
5699 	stmf_i_local_port_t *ilport =
5700 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5701 
5702 	if (eventid == STMF_EVENT_ALL) {
5703 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
5704 		return (STMF_SUCCESS);
5705 	}
5706 
5707 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5708 		return (STMF_INVALID_ARG);
5709 	}
5710 
5711 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
5712 	return (STMF_SUCCESS);
5713 }
5714 
5715 void
5716 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
5717 {
5718 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
5719 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
5720 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
5721 	}
5722 }
5723 
5724 void
5725 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
5726 				uint32_t flags)
5727 {
5728 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
5729 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
5730 		ilport->ilport_lport->lport_event_handler(
5731 		    ilport->ilport_lport, eventid, arg, flags);
5732 	}
5733 }
5734 
5735 void
5736 stmf_svc_init()
5737 {
5738 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5739 		return;
5740 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
5741 	    TASKQ_DEFAULTPRI, 0);
5742 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
5743 	    stmf_svc, 0, DDI_SLEEP);
5744 }
5745 
5746 stmf_status_t
5747 stmf_svc_fini()
5748 {
5749 	uint32_t i;
5750 
5751 	mutex_enter(&stmf_state.stmf_lock);
5752 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
5753 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
5754 		cv_signal(&stmf_state.stmf_cv);
5755 	}
5756 	mutex_exit(&stmf_state.stmf_lock);
5757 
5758 	/* Wait for 5 seconds */
5759 	for (i = 0; i < 500; i++) {
5760 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5761 			delay(drv_usectohz(10000));
5762 		else
5763 			break;
5764 	}
5765 	if (i == 500)
5766 		return (STMF_BUSY);
5767 
5768 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
5769 
5770 	return (STMF_SUCCESS);
5771 }
5772 
5773 /* ARGSUSED */
5774 void
5775 stmf_svc(void *arg)
5776 {
5777 	stmf_svc_req_t *req, **preq;
5778 	clock_t td;
5779 	clock_t	drain_start, drain_next = 0;
5780 	clock_t	timing_start, timing_next = 0;
5781 	clock_t worker_delay = 0;
5782 	int deq;
5783 	stmf_lu_t *lu;
5784 	stmf_i_lu_t *ilu;
5785 	stmf_local_port_t *lport;
5786 	stmf_i_local_port_t *ilport, *next_ilport;
5787 	stmf_i_scsi_session_t *iss;
5788 
5789 	td = drv_usectohz(20000);
5790 
5791 	mutex_enter(&stmf_state.stmf_lock);
5792 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
5793 
5794 stmf_svc_loop:
5795 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
5796 		stmf_state.stmf_svc_flags &=
5797 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
5798 		mutex_exit(&stmf_state.stmf_lock);
5799 		return;
5800 	}
5801 
5802 	if (stmf_state.stmf_svc_active) {
5803 		int waitq_add = 0;
5804 		req = stmf_state.stmf_svc_active;
5805 		stmf_state.stmf_svc_active = req->svc_next;
5806 
5807 		switch (req->svc_cmd) {
5808 		case STMF_CMD_LPORT_ONLINE:
5809 			/* Fallthrough */
5810 		case STMF_CMD_LPORT_OFFLINE:
5811 			/* Fallthrough */
5812 		case STMF_CMD_LU_ONLINE:
5813 			/* Nothing to do */
5814 			waitq_add = 1;
5815 			break;
5816 
5817 		case STMF_CMD_LU_OFFLINE:
5818 			/* Remove all mappings of this LU */
5819 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
5820 			/* Kill all the pending I/Os for this LU */
5821 			mutex_exit(&stmf_state.stmf_lock);
5822 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
5823 			    STMF_ABORTED);
5824 			mutex_enter(&stmf_state.stmf_lock);
5825 			waitq_add = 1;
5826 			break;
5827 		default:
5828 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
5829 			    req->svc_cmd);
5830 		}
5831 
5832 		if (waitq_add) {
5833 			/* Put it in the wait queue */
5834 			req->svc_next = stmf_state.stmf_svc_waiting;
5835 			stmf_state.stmf_svc_waiting = req;
5836 		}
5837 	}
5838 
5839 	/* The waiting list is not going to be modified by anybody else */
5840 	mutex_exit(&stmf_state.stmf_lock);
5841 
5842 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
5843 		req = *preq;
5844 		deq = 0;
5845 		switch (req->svc_cmd) {
5846 		case STMF_CMD_LU_ONLINE:
5847 			lu = (stmf_lu_t *)req->svc_obj;
5848 			deq = 1;
5849 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5850 			break;
5851 
5852 		case STMF_CMD_LU_OFFLINE:
5853 			lu = (stmf_lu_t *)req->svc_obj;
5854 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5855 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
5856 				break;
5857 			deq = 1;
5858 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5859 			break;
5860 
5861 		case STMF_CMD_LPORT_OFFLINE:
5862 			/* Fallthrough */
5863 		case STMF_CMD_LPORT_ONLINE:
5864 			lport = (stmf_local_port_t *)req->svc_obj;
5865 			deq = 1;
5866 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
5867 			break;
5868 		}
5869 		if (deq) {
5870 			*preq = req->svc_next;
5871 			kmem_free(req, req->svc_req_alloc_size);
5872 		} else {
5873 			preq = &req->svc_next;
5874 		}
5875 	}
5876 
5877 	mutex_enter(&stmf_state.stmf_lock);
5878 	if (stmf_state.stmf_svc_active == NULL) {
5879 		/* Do timeouts */
5880 		if (stmf_state.stmf_nlus &&
5881 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
5882 			if (!stmf_state.stmf_svc_ilu_timing) {
5883 				/* we are starting a new round */
5884 				stmf_state.stmf_svc_ilu_timing =
5885 				    stmf_state.stmf_ilulist;
5886 				timing_start = ddi_get_lbolt();
5887 			}
5888 			stmf_check_ilu_timing();
5889 			if (!stmf_state.stmf_svc_ilu_timing) {
5890 				/* we finished a complete round */
5891 				timing_next =
5892 				    timing_start + drv_usectohz(5*1000*1000);
5893 			} else {
5894 				/* we still have some ilu items to check */
5895 				timing_next =
5896 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5897 			}
5898 			if (stmf_state.stmf_svc_active)
5899 				goto stmf_svc_loop;
5900 		}
5901 		/* Check if there are free tasks to clear */
5902 		if (stmf_state.stmf_nlus &&
5903 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
5904 			if (!stmf_state.stmf_svc_ilu_draining) {
5905 				/* we are starting a new round */
5906 				stmf_state.stmf_svc_ilu_draining =
5907 				    stmf_state.stmf_ilulist;
5908 				drain_start = ddi_get_lbolt();
5909 			}
5910 			stmf_check_freetask();
5911 			if (!stmf_state.stmf_svc_ilu_draining) {
5912 				/* we finished a complete round */
5913 				drain_next =
5914 				    drain_start + drv_usectohz(10*1000*1000);
5915 			} else {
5916 				/* we still have some ilu items to check */
5917 				drain_next =
5918 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5919 			}
5920 			if (stmf_state.stmf_svc_active)
5921 				goto stmf_svc_loop;
5922 		}
5923 
5924 		/* Check if we need to run worker_mgmt */
5925 		if (ddi_get_lbolt() > worker_delay) {
5926 			stmf_worker_mgmt();
5927 			worker_delay = ddi_get_lbolt() +
5928 			    stmf_worker_mgmt_delay;
5929 		}
5930 
5931 		/* Check if any active session got its 1st LUN */
5932 		if (stmf_state.stmf_process_initial_luns) {
5933 			int stmf_level = 0;
5934 			int port_level;
5935 			for (ilport = stmf_state.stmf_ilportlist; ilport;
5936 			    ilport = next_ilport) {
5937 				next_ilport = ilport->ilport_next;
5938 				if ((ilport->ilport_flags &
5939 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
5940 					continue;
5941 				}
5942 				port_level = 0;
5943 				rw_enter(&ilport->ilport_lock, RW_READER);
5944 				for (iss = ilport->ilport_ss_list; iss;
5945 				    iss = iss->iss_next) {
5946 					if ((iss->iss_flags &
5947 					    ISS_GOT_INITIAL_LUNS) == 0) {
5948 						continue;
5949 					}
5950 					port_level++;
5951 					stmf_level++;
5952 					atomic_and_32(&iss->iss_flags,
5953 					    ~ISS_GOT_INITIAL_LUNS);
5954 					atomic_or_32(&iss->iss_flags,
5955 					    ISS_EVENT_ACTIVE);
5956 					rw_exit(&ilport->ilport_lock);
5957 					mutex_exit(&stmf_state.stmf_lock);
5958 					stmf_generate_lport_event(ilport,
5959 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
5960 					    iss->iss_ss, 0);
5961 					atomic_and_32(&iss->iss_flags,
5962 					    ~ISS_EVENT_ACTIVE);
5963 					mutex_enter(&stmf_state.stmf_lock);
5964 					/*
5965 					 * scan all the ilports again as the
5966 					 * ilport list might have changed.
5967 					 */
5968 					next_ilport =
5969 					    stmf_state.stmf_ilportlist;
5970 					break;
5971 				}
5972 				if (port_level == 0) {
5973 					atomic_and_32(&ilport->ilport_flags,
5974 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
5975 				}
5976 				/* drop the lock if we are holding it. */
5977 				if (rw_lock_held(&ilport->ilport_lock))
5978 					rw_exit(&ilport->ilport_lock);
5979 
5980 				/* Max 4 session at a time */
5981 				if (stmf_level >= 4) {
5982 					break;
5983 				}
5984 			}
5985 			if (stmf_level == 0) {
5986 				stmf_state.stmf_process_initial_luns = 0;
5987 			}
5988 		}
5989 
5990 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
5991 		(void) cv_timedwait(&stmf_state.stmf_cv, &stmf_state.stmf_lock,
5992 		    ddi_get_lbolt() + td);
5993 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
5994 	}
5995 	goto stmf_svc_loop;
5996 }
5997 
5998 void
5999 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
6000 {
6001 	stmf_svc_req_t *req;
6002 	int s;
6003 
6004 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
6005 	s = sizeof (stmf_svc_req_t);
6006 	if (info->st_additional_info) {
6007 		s += strlen(info->st_additional_info) + 1;
6008 	}
6009 	req = kmem_zalloc(s, KM_SLEEP);
6010 
6011 	req->svc_cmd = cmd;
6012 	req->svc_obj = obj;
6013 	req->svc_info.st_rflags = info->st_rflags;
6014 	if (info->st_additional_info) {
6015 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
6016 		    sizeof (stmf_svc_req_t)));
6017 		(void) strcpy(req->svc_info.st_additional_info,
6018 		    info->st_additional_info);
6019 	}
6020 	req->svc_req_alloc_size = s;
6021 
6022 	mutex_enter(&stmf_state.stmf_lock);
6023 	req->svc_next = stmf_state.stmf_svc_active;
6024 	stmf_state.stmf_svc_active = req;
6025 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
6026 		cv_signal(&stmf_state.stmf_cv);
6027 	}
6028 	mutex_exit(&stmf_state.stmf_lock);
6029 }
6030 
6031 void
6032 stmf_trace(caddr_t ident, const char *fmt, ...)
6033 {
6034 	va_list args;
6035 	char tbuf[160];
6036 	int len;
6037 
6038 	if (!stmf_trace_on)
6039 		return;
6040 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
6041 	    ddi_get_lbolt());
6042 	va_start(args, fmt);
6043 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
6044 	va_end(args);
6045 
6046 	if (len > 158) {
6047 		len = 158;
6048 	}
6049 	tbuf[len++] = '\n';
6050 	tbuf[len] = 0;
6051 
6052 	mutex_enter(&trace_buf_lock);
6053 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
6054 	trace_buf_curndx += len;
6055 	if (trace_buf_curndx > (trace_buf_size - 320))
6056 		trace_buf_curndx = 0;
6057 	mutex_exit(&trace_buf_lock);
6058 }
6059 
6060 void
6061 stmf_trace_clear()
6062 {
6063 	if (!stmf_trace_on)
6064 		return;
6065 	mutex_enter(&trace_buf_lock);
6066 	trace_buf_curndx = 0;
6067 	if (trace_buf_size > 0)
6068 		stmf_trace_buf[0] = 0;
6069 	mutex_exit(&trace_buf_lock);
6070 }
6071 
6072 static void
6073 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
6074 {
6075 	stmf_state_change_info_t	 change_info;
6076 	void				*ctl_private;
6077 	uint32_t			 ctl_cmd;
6078 	int				msg = 0;
6079 
6080 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
6081 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
6082 	change_info.st_additional_info = info;
6083 	if (offline_lu) {
6084 		change_info.st_rflags = STMF_RFLAG_RESET |
6085 		    STMF_RFLAG_LU_ABORT;
6086 		ctl_private = task->task_lu;
6087 		if (((stmf_i_lu_t *)
6088 		    task->task_lu->lu_stmf_private)->ilu_state ==
6089 		    STMF_STATE_ONLINE) {
6090 			msg = 1;
6091 		}
6092 		ctl_cmd = STMF_CMD_LU_OFFLINE;
6093 	} else {
6094 		change_info.st_rflags = STMF_RFLAG_RESET |
6095 		    STMF_RFLAG_LPORT_ABORT;
6096 		ctl_private = task->task_lport;
6097 		if (((stmf_i_local_port_t *)
6098 		    task->task_lport->lport_stmf_private)->ilport_state ==
6099 		    STMF_STATE_ONLINE) {
6100 			msg = 1;
6101 		}
6102 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
6103 	}
6104 
6105 	if (msg) {
6106 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
6107 		    offline_lu ? "LU" : "LPORT", info ? info :
6108 		    "<no additional info>");
6109 	}
6110 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
6111 }
6112