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