xref: /titanic_41/usr/src/uts/common/io/comstar/stmf/stmf.c (revision 8475e04352e630e4bd0f59a283286ee2475a14ce)
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 	sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
1345 	sh->cp = GET_BYTE_OFFSET(sh->fp, stmf_sizes[struct_id].fw_private);
1346 
1347 	sh->fp->bp = sh;
1348 	/* Just store the total size instead of storing additional size */
1349 	sh->fp->alloc_size = stmf_size;
1350 
1351 	return (sh);
1352 }
1353 
1354 void
1355 stmf_free(void *ptr)
1356 {
1357 	__stmf_t *sh = (__stmf_t *)ptr;
1358 
1359 	/*
1360 	 * So far we dont need any struct specific processing. If such
1361 	 * a need ever arises, then store the struct id in the framework
1362 	 * private section and get it here as sh->fp->struct_id.
1363 	 */
1364 	kmem_free(ptr, sh->fp->alloc_size);
1365 }
1366 
1367 /*
1368  * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
1369  * framework and returns a pointer to framework private data for the lu.
1370  * Returns NULL if the lu was not found.
1371  */
1372 stmf_i_lu_t *
1373 stmf_lookup_lu(stmf_lu_t *lu)
1374 {
1375 	stmf_i_lu_t *ilu;
1376 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1377 
1378 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
1379 		if (ilu->ilu_lu == lu)
1380 			return (ilu);
1381 	}
1382 	return (NULL);
1383 }
1384 
1385 /*
1386  * Given a pointer to stmf_local_port_t, verifies if this lport is registered
1387  * with the framework and returns a pointer to framework private data for
1388  * the lport.
1389  * Returns NULL if the lport was not found.
1390  */
1391 stmf_i_local_port_t *
1392 stmf_lookup_lport(stmf_local_port_t *lport)
1393 {
1394 	stmf_i_local_port_t *ilport;
1395 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1396 
1397 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1398 	    ilport = ilport->ilport_next) {
1399 		if (ilport->ilport_lport == lport)
1400 			return (ilport);
1401 	}
1402 	return (NULL);
1403 }
1404 
1405 stmf_status_t
1406 stmf_register_lu_provider(stmf_lu_provider_t *lp)
1407 {
1408 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
1409 	stmf_pp_data_t *ppd;
1410 	uint32_t cb_flags;
1411 
1412 	if (lp->lp_lpif_rev != LPIF_REV_1)
1413 		return (STMF_FAILURE);
1414 
1415 	mutex_enter(&stmf_state.stmf_lock);
1416 	ilp->ilp_next = stmf_state.stmf_ilplist;
1417 	stmf_state.stmf_ilplist = ilp;
1418 	stmf_state.stmf_nlps++;
1419 
1420 	/* See if we need to do a callback */
1421 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1422 		if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
1423 			break;
1424 		}
1425 	}
1426 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
1427 		goto rlp_bail_out;
1428 	}
1429 	ilp->ilp_ppd = ppd;
1430 	ppd->ppd_provider = ilp;
1431 	if (lp->lp_cb == NULL)
1432 		goto rlp_bail_out;
1433 	ilp->ilp_cb_in_progress = 1;
1434 	cb_flags = STMF_PCB_PREG_COMPLETE;
1435 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1436 		cb_flags |= STMF_PCB_STMF_ONLINING;
1437 	mutex_exit(&stmf_state.stmf_lock);
1438 	lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1439 	mutex_enter(&stmf_state.stmf_lock);
1440 	ilp->ilp_cb_in_progress = 0;
1441 
1442 rlp_bail_out:
1443 	mutex_exit(&stmf_state.stmf_lock);
1444 
1445 	return (STMF_SUCCESS);
1446 }
1447 
1448 stmf_status_t
1449 stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
1450 {
1451 	stmf_i_lu_provider_t	**ppilp;
1452 	stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
1453 
1454 	mutex_enter(&stmf_state.stmf_lock);
1455 	if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
1456 		mutex_exit(&stmf_state.stmf_lock);
1457 		return (STMF_BUSY);
1458 	}
1459 	for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
1460 	    ppilp = &((*ppilp)->ilp_next)) {
1461 		if (*ppilp == ilp) {
1462 			*ppilp = ilp->ilp_next;
1463 			stmf_state.stmf_nlps--;
1464 			if (ilp->ilp_ppd) {
1465 				ilp->ilp_ppd->ppd_provider = NULL;
1466 				ilp->ilp_ppd = NULL;
1467 			}
1468 			mutex_exit(&stmf_state.stmf_lock);
1469 			return (STMF_SUCCESS);
1470 		}
1471 	}
1472 	mutex_exit(&stmf_state.stmf_lock);
1473 	return (STMF_NOT_FOUND);
1474 }
1475 
1476 stmf_status_t
1477 stmf_register_port_provider(stmf_port_provider_t *pp)
1478 {
1479 	stmf_i_port_provider_t *ipp =
1480 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
1481 	stmf_pp_data_t *ppd;
1482 	uint32_t cb_flags;
1483 
1484 	if (pp->pp_portif_rev != PORTIF_REV_1)
1485 		return (STMF_FAILURE);
1486 
1487 	mutex_enter(&stmf_state.stmf_lock);
1488 	ipp->ipp_next = stmf_state.stmf_ipplist;
1489 	stmf_state.stmf_ipplist = ipp;
1490 	stmf_state.stmf_npps++;
1491 	/* See if we need to do a callback */
1492 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1493 		if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
1494 			break;
1495 		}
1496 	}
1497 	if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
1498 		goto rpp_bail_out;
1499 	}
1500 	ipp->ipp_ppd = ppd;
1501 	ppd->ppd_provider = ipp;
1502 	if (pp->pp_cb == NULL)
1503 		goto rpp_bail_out;
1504 	ipp->ipp_cb_in_progress = 1;
1505 	cb_flags = STMF_PCB_PREG_COMPLETE;
1506 	if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1507 		cb_flags |= STMF_PCB_STMF_ONLINING;
1508 	mutex_exit(&stmf_state.stmf_lock);
1509 	pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1510 	mutex_enter(&stmf_state.stmf_lock);
1511 	ipp->ipp_cb_in_progress = 0;
1512 
1513 rpp_bail_out:
1514 	mutex_exit(&stmf_state.stmf_lock);
1515 
1516 	return (STMF_SUCCESS);
1517 }
1518 
1519 stmf_status_t
1520 stmf_deregister_port_provider(stmf_port_provider_t *pp)
1521 {
1522 	stmf_i_port_provider_t *ipp =
1523 	    (stmf_i_port_provider_t *)pp->pp_stmf_private;
1524 	stmf_i_port_provider_t **ppipp;
1525 
1526 	mutex_enter(&stmf_state.stmf_lock);
1527 	if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
1528 		mutex_exit(&stmf_state.stmf_lock);
1529 		return (STMF_BUSY);
1530 	}
1531 	for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
1532 	    ppipp = &((*ppipp)->ipp_next)) {
1533 		if (*ppipp == ipp) {
1534 			*ppipp = ipp->ipp_next;
1535 			stmf_state.stmf_npps--;
1536 			if (ipp->ipp_ppd) {
1537 				ipp->ipp_ppd->ppd_provider = NULL;
1538 				ipp->ipp_ppd = NULL;
1539 			}
1540 			mutex_exit(&stmf_state.stmf_lock);
1541 			return (STMF_SUCCESS);
1542 		}
1543 	}
1544 	mutex_exit(&stmf_state.stmf_lock);
1545 	return (STMF_NOT_FOUND);
1546 }
1547 
1548 int
1549 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi)
1550 {
1551 	stmf_i_port_provider_t		*ipp;
1552 	stmf_i_lu_provider_t		*ilp;
1553 	stmf_pp_data_t			*ppd;
1554 	nvlist_t			*nv;
1555 	int				s;
1556 	int				ret;
1557 
1558 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
1559 		return (EINVAL);
1560 	}
1561 
1562 	mutex_enter(&stmf_state.stmf_lock);
1563 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1564 		if (ppi->ppi_lu_provider) {
1565 			if (!ppd->ppd_lu_provider)
1566 				continue;
1567 		} else if (ppi->ppi_port_provider) {
1568 			if (!ppd->ppd_port_provider)
1569 				continue;
1570 		}
1571 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
1572 			break;
1573 	}
1574 
1575 	if (ppd == NULL) {
1576 		/* New provider */
1577 		s = strlen(ppi->ppi_name);
1578 		if (s > 254) {
1579 			mutex_exit(&stmf_state.stmf_lock);
1580 			return (EINVAL);
1581 		}
1582 		s += sizeof (stmf_pp_data_t) - 7;
1583 
1584 		ppd = kmem_zalloc(s, KM_NOSLEEP);
1585 		if (ppd == NULL) {
1586 			mutex_exit(&stmf_state.stmf_lock);
1587 			return (ENOMEM);
1588 		}
1589 		ppd->ppd_alloc_size = s;
1590 		(void) strcpy(ppd->ppd_name, ppi->ppi_name);
1591 
1592 		/* See if this provider already exists */
1593 		if (ppi->ppi_lu_provider) {
1594 			ppd->ppd_lu_provider = 1;
1595 			for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
1596 			    ilp = ilp->ilp_next) {
1597 				if (strcmp(ppi->ppi_name,
1598 				    ilp->ilp_lp->lp_name) == 0) {
1599 					ppd->ppd_provider = ilp;
1600 					ilp->ilp_ppd = ppd;
1601 					break;
1602 				}
1603 			}
1604 		} else {
1605 			ppd->ppd_port_provider = 1;
1606 			for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
1607 			    ipp = ipp->ipp_next) {
1608 				if (strcmp(ppi->ppi_name,
1609 				    ipp->ipp_pp->pp_name) == 0) {
1610 					ppd->ppd_provider = ipp;
1611 					ipp->ipp_ppd = ppd;
1612 					break;
1613 				}
1614 			}
1615 		}
1616 
1617 		/* Link this ppd in */
1618 		ppd->ppd_next = stmf_state.stmf_ppdlist;
1619 		stmf_state.stmf_ppdlist = ppd;
1620 	}
1621 
1622 	if ((ret = nvlist_unpack((char *)ppi->ppi_data,
1623 	    (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
1624 		mutex_exit(&stmf_state.stmf_lock);
1625 		return (ret);
1626 	}
1627 
1628 	/* Free any existing lists and add this one to the ppd */
1629 	if (ppd->ppd_nv)
1630 		nvlist_free(ppd->ppd_nv);
1631 	ppd->ppd_nv = nv;
1632 
1633 	/* If there is a provider registered, do the notifications */
1634 	if (ppd->ppd_provider) {
1635 		uint32_t cb_flags = 0;
1636 
1637 		if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
1638 			cb_flags |= STMF_PCB_STMF_ONLINING;
1639 		if (ppi->ppi_lu_provider) {
1640 			ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
1641 			if (ilp->ilp_lp->lp_cb == NULL)
1642 				goto bail_out;
1643 			ilp->ilp_cb_in_progress = 1;
1644 			mutex_exit(&stmf_state.stmf_lock);
1645 			ilp->ilp_lp->lp_cb(ilp->ilp_lp,
1646 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1647 			mutex_enter(&stmf_state.stmf_lock);
1648 			ilp->ilp_cb_in_progress = 0;
1649 		} else {
1650 			ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
1651 			if (ipp->ipp_pp->pp_cb == NULL)
1652 				goto bail_out;
1653 			ipp->ipp_cb_in_progress = 1;
1654 			mutex_exit(&stmf_state.stmf_lock);
1655 			ipp->ipp_pp->pp_cb(ipp->ipp_pp,
1656 			    STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
1657 			mutex_enter(&stmf_state.stmf_lock);
1658 			ipp->ipp_cb_in_progress = 0;
1659 		}
1660 	}
1661 
1662 bail_out:
1663 	mutex_exit(&stmf_state.stmf_lock);
1664 
1665 	return (0);
1666 }
1667 
1668 void
1669 stmf_delete_ppd(stmf_pp_data_t *ppd)
1670 {
1671 	stmf_pp_data_t **pppd;
1672 
1673 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1674 	if (ppd->ppd_provider) {
1675 		if (ppd->ppd_lu_provider) {
1676 			((stmf_i_lu_provider_t *)
1677 			    ppd->ppd_provider)->ilp_ppd = NULL;
1678 		} else {
1679 			((stmf_i_port_provider_t *)
1680 			    ppd->ppd_provider)->ipp_ppd = NULL;
1681 		}
1682 		ppd->ppd_provider = NULL;
1683 	}
1684 
1685 	for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
1686 	    pppd = &((*pppd)->ppd_next)) {
1687 		if (*pppd == ppd)
1688 			break;
1689 	}
1690 
1691 	if (*pppd == NULL)
1692 		return;
1693 
1694 	*pppd = ppd->ppd_next;
1695 	if (ppd->ppd_nv)
1696 		nvlist_free(ppd->ppd_nv);
1697 
1698 	kmem_free(ppd, ppd->ppd_alloc_size);
1699 }
1700 
1701 int
1702 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
1703 {
1704 	stmf_pp_data_t *ppd;
1705 	int ret = ENOENT;
1706 
1707 	if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
1708 		return (EINVAL);
1709 	}
1710 
1711 	mutex_enter(&stmf_state.stmf_lock);
1712 
1713 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
1714 		if (ppi->ppi_lu_provider) {
1715 			if (!ppd->ppd_lu_provider)
1716 				continue;
1717 		} else if (ppi->ppi_port_provider) {
1718 			if (!ppd->ppd_port_provider)
1719 				continue;
1720 		}
1721 		if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
1722 			break;
1723 	}
1724 
1725 	if (ppd) {
1726 		ret = 0;
1727 		stmf_delete_ppd(ppd);
1728 	}
1729 	mutex_exit(&stmf_state.stmf_lock);
1730 
1731 	return (ret);
1732 }
1733 
1734 void
1735 stmf_delete_all_ppds()
1736 {
1737 	stmf_pp_data_t *ppd, *nppd;
1738 
1739 	ASSERT(mutex_owned(&stmf_state.stmf_lock));
1740 	for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
1741 		nppd = ppd->ppd_next;
1742 		stmf_delete_ppd(ppd);
1743 	}
1744 }
1745 
1746 stmf_status_t
1747 stmf_register_lu(stmf_lu_t *lu)
1748 {
1749 	stmf_i_lu_t *ilu;
1750 	uint8_t *p1, *p2;
1751 	stmf_state_change_info_t ssci;
1752 	stmf_id_data_t *luid;
1753 
1754 	if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
1755 	    (lu->lu_id->ident_length != 16) ||
1756 	    ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
1757 		return (STMF_INVALID_ARG);
1758 	}
1759 	p1 = &lu->lu_id->ident[0];
1760 	mutex_enter(&stmf_state.stmf_lock);
1761 	if (stmf_state.stmf_inventory_locked) {
1762 		mutex_exit(&stmf_state.stmf_lock);
1763 		return (STMF_BUSY);
1764 	}
1765 
1766 	for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
1767 		p2 = &ilu->ilu_lu->lu_id->ident[0];
1768 		if (bcmp(p1, p2, 16) == 0) {
1769 			mutex_exit(&stmf_state.stmf_lock);
1770 			return (STMF_ALREADY);
1771 		}
1772 	}
1773 
1774 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
1775 	luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
1776 	    lu->lu_id->ident_length, lu->lu_id->ident);
1777 	if (luid) {
1778 		luid->id_pt_to_object = (void *)ilu;
1779 		ilu->ilu_luid = luid;
1780 	}
1781 	ilu->ilu_alias = NULL;
1782 
1783 	ilu->ilu_next = stmf_state.stmf_ilulist;
1784 	ilu->ilu_prev = NULL;
1785 	if (ilu->ilu_next)
1786 		ilu->ilu_next->ilu_prev = ilu;
1787 	stmf_state.stmf_ilulist = ilu;
1788 	stmf_state.stmf_nlus++;
1789 	if (lu->lu_lp) {
1790 		((stmf_i_lu_provider_t *)
1791 		    (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
1792 	}
1793 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
1794 	STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
1795 	mutex_exit(&stmf_state.stmf_lock);
1796 
1797 	/* XXX we should probably check if this lu can be brought online */
1798 	ilu->ilu_prev_state = STMF_STATE_ONLINE;
1799 	if (stmf_state.stmf_service_running) {
1800 		ssci.st_rflags = 0;
1801 		ssci.st_additional_info = NULL;
1802 		(void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
1803 	}
1804 
1805 	/* XXX: Generate event */
1806 	return (STMF_SUCCESS);
1807 }
1808 
1809 stmf_status_t
1810 stmf_deregister_lu(stmf_lu_t *lu)
1811 {
1812 	stmf_i_lu_t *ilu;
1813 
1814 	mutex_enter(&stmf_state.stmf_lock);
1815 	if (stmf_state.stmf_inventory_locked) {
1816 		mutex_exit(&stmf_state.stmf_lock);
1817 		return (STMF_BUSY);
1818 	}
1819 	ilu = stmf_lookup_lu(lu);
1820 	if (ilu == NULL) {
1821 		mutex_exit(&stmf_state.stmf_lock);
1822 		return (STMF_INVALID_ARG);
1823 	}
1824 	if (ilu->ilu_state == STMF_STATE_OFFLINE) {
1825 		ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
1826 		while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
1827 			cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
1828 		}
1829 		if (ilu->ilu_ntasks) {
1830 			stmf_i_scsi_task_t *itask, *nitask;
1831 
1832 			nitask = ilu->ilu_tasks;
1833 			do {
1834 				itask = nitask;
1835 				nitask = itask->itask_lu_next;
1836 				lu->lu_task_free(itask->itask_task);
1837 				stmf_free(itask->itask_task);
1838 			} while (nitask != NULL);
1839 
1840 			ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
1841 			ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
1842 		}
1843 
1844 		if (ilu->ilu_next)
1845 			ilu->ilu_next->ilu_prev = ilu->ilu_prev;
1846 		if (ilu->ilu_prev)
1847 			ilu->ilu_prev->ilu_next = ilu->ilu_next;
1848 		else
1849 			stmf_state.stmf_ilulist = ilu->ilu_next;
1850 		stmf_state.stmf_nlus--;
1851 
1852 		if (ilu == stmf_state.stmf_svc_ilu_draining) {
1853 			stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
1854 		}
1855 		if (ilu == stmf_state.stmf_svc_ilu_timing) {
1856 			stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
1857 		}
1858 		if (lu->lu_lp) {
1859 			((stmf_i_lu_provider_t *)
1860 			    (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
1861 		}
1862 		if (ilu->ilu_luid) {
1863 			((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
1864 			    NULL;
1865 			ilu->ilu_luid = NULL;
1866 		}
1867 		STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
1868 	} else {
1869 		mutex_exit(&stmf_state.stmf_lock);
1870 		return (STMF_BUSY);
1871 	}
1872 	mutex_exit(&stmf_state.stmf_lock);
1873 	return (STMF_SUCCESS);
1874 }
1875 
1876 stmf_status_t
1877 stmf_register_local_port(stmf_local_port_t *lport)
1878 {
1879 	stmf_i_local_port_t *ilport;
1880 	stmf_state_change_info_t ssci;
1881 	int start_workers = 0;
1882 
1883 	mutex_enter(&stmf_state.stmf_lock);
1884 	if (stmf_state.stmf_inventory_locked) {
1885 		mutex_exit(&stmf_state.stmf_lock);
1886 		return (STMF_BUSY);
1887 	}
1888 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
1889 	rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
1890 
1891 	ilport->ilport_next = stmf_state.stmf_ilportlist;
1892 	ilport->ilport_prev = NULL;
1893 	if (ilport->ilport_next)
1894 		ilport->ilport_next->ilport_prev = ilport;
1895 	stmf_state.stmf_ilportlist = ilport;
1896 	stmf_state.stmf_nlports++;
1897 	if (lport->lport_pp) {
1898 		((stmf_i_port_provider_t *)
1899 		    (lport->lport_pp->pp_stmf_private))->ipp_npps++;
1900 	}
1901 	ilport->ilport_tg =
1902 	    stmf_lookup_group_for_target(lport->lport_id->ident,
1903 	    lport->lport_id->ident_length);
1904 	ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1);
1905 	STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
1906 	if (stmf_workers_state == STMF_WORKERS_DISABLED) {
1907 		stmf_workers_state = STMF_WORKERS_ENABLING;
1908 		start_workers = 1;
1909 	}
1910 	mutex_exit(&stmf_state.stmf_lock);
1911 
1912 	if (start_workers)
1913 		stmf_worker_init();
1914 
1915 	/* XXX we should probably check if this lport can be brought online */
1916 	ilport->ilport_prev_state = STMF_STATE_ONLINE;
1917 	if (stmf_state.stmf_service_running) {
1918 		ssci.st_rflags = 0;
1919 		ssci.st_additional_info = NULL;
1920 		(void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
1921 	}
1922 
1923 	/* XXX: Generate event */
1924 	return (STMF_SUCCESS);
1925 }
1926 
1927 stmf_status_t
1928 stmf_deregister_local_port(stmf_local_port_t *lport)
1929 {
1930 	stmf_i_local_port_t *ilport;
1931 
1932 	mutex_enter(&stmf_state.stmf_lock);
1933 	if (stmf_state.stmf_inventory_locked) {
1934 		mutex_exit(&stmf_state.stmf_lock);
1935 		return (STMF_BUSY);
1936 	}
1937 	ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
1938 	if (ilport->ilport_nsessions == 0) {
1939 		if (ilport->ilport_next)
1940 			ilport->ilport_next->ilport_prev = ilport->ilport_prev;
1941 		if (ilport->ilport_prev)
1942 			ilport->ilport_prev->ilport_next = ilport->ilport_next;
1943 		else
1944 			stmf_state.stmf_ilportlist = ilport->ilport_next;
1945 		rw_destroy(&ilport->ilport_lock);
1946 		stmf_state.stmf_nlports--;
1947 		if (lport->lport_pp) {
1948 			((stmf_i_port_provider_t *)
1949 			    (lport->lport_pp->pp_stmf_private))->ipp_npps--;
1950 		}
1951 		ilport->ilport_tg = NULL;
1952 		STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
1953 	} else {
1954 		mutex_exit(&stmf_state.stmf_lock);
1955 		return (STMF_BUSY);
1956 	}
1957 	mutex_exit(&stmf_state.stmf_lock);
1958 	return (STMF_SUCCESS);
1959 }
1960 
1961 /*
1962  * Port provider has to make sure that register/deregister session and
1963  * port are serialized calls.
1964  */
1965 stmf_status_t
1966 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
1967 {
1968 	stmf_i_scsi_session_t *iss;
1969 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
1970 	    lport->lport_stmf_private;
1971 	uint8_t		lun[8];
1972 
1973 	/*
1974 	 * Port state has to be online to register a scsi session. It is
1975 	 * possible that we started an offline operation and a new SCSI
1976 	 * session started at the same time (in that case also we are going
1977 	 * to fail the registeration). But any other state is simply
1978 	 * a bad port provider implementation.
1979 	 */
1980 	if (ilport->ilport_state != STMF_STATE_ONLINE) {
1981 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
1982 			stmf_trace(lport->lport_alias, "Port is trying to "
1983 			    "register a session while the state is neither "
1984 			    "online nor offlining");
1985 		}
1986 		return (STMF_FAILURE);
1987 	}
1988 	bzero(lun, 8);
1989 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
1990 	iss->iss_flags |= ISS_BEING_CREATED;
1991 
1992 	/* sessions use the ilport_lock. No separate lock is required */
1993 	iss->iss_lockp = &ilport->ilport_lock;
1994 	(void) stmf_session_create_lun_map(ilport, iss);
1995 
1996 	rw_enter(&ilport->ilport_lock, RW_WRITER);
1997 	ilport->ilport_nsessions++;
1998 	iss->iss_next = ilport->ilport_ss_list;
1999 	ilport->ilport_ss_list = iss;
2000 	rw_exit(&ilport->ilport_lock);
2001 
2002 	iss->iss_creation_time = ddi_get_time();
2003 	ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1);
2004 	iss->iss_flags &= ~ISS_BEING_CREATED;
2005 	DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
2006 	    stmf_scsi_session_t *, ss);
2007 	return (STMF_SUCCESS);
2008 }
2009 
2010 void
2011 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
2012 {
2013 	stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
2014 	    lport->lport_stmf_private;
2015 	stmf_i_scsi_session_t *iss, **ppss;
2016 	int found = 0;
2017 
2018 	DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
2019 	    stmf_scsi_session_t *, ss);
2020 
2021 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2022 	if (ss->ss_rport_alias) {
2023 		ss->ss_rport_alias = NULL;
2024 	}
2025 
2026 try_dereg_ss_again:
2027 	mutex_enter(&stmf_state.stmf_lock);
2028 	atomic_and_32(&iss->iss_flags,
2029 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
2030 	if (iss->iss_flags & ISS_EVENT_ACTIVE) {
2031 		mutex_exit(&stmf_state.stmf_lock);
2032 		delay(1);
2033 		goto try_dereg_ss_again;
2034 	}
2035 	mutex_exit(&stmf_state.stmf_lock);
2036 	rw_enter(&ilport->ilport_lock, RW_WRITER);
2037 	for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
2038 	    ppss = &((*ppss)->iss_next)) {
2039 		if (iss == (*ppss)) {
2040 			*ppss = (*ppss)->iss_next;
2041 			found = 1;
2042 			break;
2043 		}
2044 	}
2045 	if (!found) {
2046 		cmn_err(CE_PANIC, "Deregister session called for non existent"
2047 		    " session");
2048 	}
2049 	ilport->ilport_nsessions--;
2050 	rw_exit(&ilport->ilport_lock);
2051 
2052 	(void) stmf_session_destroy_lun_map(ilport, iss);
2053 }
2054 
2055 stmf_i_scsi_session_t *
2056 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
2057 {
2058 	stmf_i_local_port_t *ilport;
2059 	stmf_i_scsi_session_t *iss;
2060 
2061 	mutex_enter(&stmf_state.stmf_lock);
2062 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2063 	    ilport = ilport->ilport_next) {
2064 		rw_enter(&ilport->ilport_lock, RW_WRITER);
2065 		for (iss = ilport->ilport_ss_list; iss != NULL;
2066 		    iss = iss->iss_next) {
2067 			if (iss->iss_ss->ss_session_id == session_id) {
2068 				if (!stay_locked)
2069 					rw_exit(&ilport->ilport_lock);
2070 				mutex_exit(&stmf_state.stmf_lock);
2071 				return (iss);
2072 			}
2073 		}
2074 		rw_exit(&ilport->ilport_lock);
2075 	}
2076 	mutex_exit(&stmf_state.stmf_lock);
2077 	return (NULL);
2078 }
2079 
2080 void
2081 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
2082 {
2083 	stmf_itl_data_t **itlpp;
2084 	stmf_i_lu_t *ilu;
2085 
2086 	ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
2087 
2088 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2089 	mutex_enter(&ilu->ilu_task_lock);
2090 	for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
2091 	    itlpp = &(*itlpp)->itl_next) {
2092 		if ((*itlpp) == itl)
2093 			break;
2094 	}
2095 	ASSERT((*itlpp) != NULL);
2096 	*itlpp = itl->itl_next;
2097 	mutex_exit(&ilu->ilu_task_lock);
2098 	lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
2099 	    (uint32_t)itl->itl_hdlrm_reason);
2100 	kmem_free(itl, sizeof (*itl));
2101 }
2102 
2103 stmf_status_t
2104 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
2105     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
2106 {
2107 	stmf_itl_data_t *itl;
2108 	stmf_i_scsi_session_t *iss;
2109 	stmf_lun_map_ent_t *lun_map_ent;
2110 	stmf_i_lu_t *ilu;
2111 	uint16_t n;
2112 
2113 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2114 	if (ss == NULL) {
2115 		iss = stmf_session_id_to_issptr(session_id, 1);
2116 		if (iss == NULL)
2117 			return (STMF_NOT_FOUND);
2118 	} else {
2119 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2120 		rw_enter(iss->iss_lockp, RW_WRITER);
2121 	}
2122 
2123 	n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2124 	lun_map_ent = (stmf_lun_map_ent_t *)
2125 	    stmf_get_ent_from_map(iss->iss_sm, n);
2126 	if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
2127 		rw_exit(iss->iss_lockp);
2128 		return (STMF_NOT_FOUND);
2129 	}
2130 	if (lun_map_ent->ent_itl_datap != NULL) {
2131 		rw_exit(iss->iss_lockp);
2132 		return (STMF_ALREADY);
2133 	}
2134 
2135 	itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
2136 	if (itl == NULL) {
2137 		rw_exit(iss->iss_lockp);
2138 		return (STMF_ALLOC_FAILURE);
2139 	}
2140 
2141 	itl->itl_counter = 1;
2142 	itl->itl_lun = n;
2143 	itl->itl_handle = itl_handle;
2144 	itl->itl_session = iss;
2145 	mutex_enter(&ilu->ilu_task_lock);
2146 	itl->itl_next = ilu->ilu_itl_list;
2147 	ilu->ilu_itl_list = itl;
2148 	mutex_exit(&ilu->ilu_task_lock);
2149 	lun_map_ent->ent_itl_datap = itl;
2150 	rw_exit(iss->iss_lockp);
2151 
2152 	return (STMF_SUCCESS);
2153 }
2154 
2155 void
2156 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
2157 {
2158 	uint8_t old, new;
2159 
2160 	do {
2161 		old = new = itl->itl_flags;
2162 		if (old & STMF_ITL_BEING_TERMINATED)
2163 			return;
2164 		new |= STMF_ITL_BEING_TERMINATED;
2165 	} while (atomic_cas_8(&itl->itl_flags, old, new) != old);
2166 	itl->itl_hdlrm_reason = hdlrm_reason;
2167 
2168 	ASSERT(itl->itl_counter);
2169 
2170 	if (atomic_add_32_nv(&itl->itl_counter, -1))
2171 		return;
2172 
2173 	drv_usecwait(10);
2174 	if (itl->itl_counter)
2175 		return;
2176 
2177 	stmf_release_itl_handle(lu, itl);
2178 }
2179 
2180 stmf_status_t
2181 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
2182 {
2183 	stmf_i_lu_t *ilu;
2184 	stmf_i_local_port_t *ilport;
2185 	stmf_i_scsi_session_t *iss;
2186 	stmf_lun_map_t *lm;
2187 	stmf_lun_map_ent_t *ent;
2188 	uint32_t nmaps, nu;
2189 	stmf_itl_data_t **itl_list;
2190 	int i;
2191 
2192 	ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2193 
2194 dereg_itl_start:;
2195 	nmaps = ilu->ilu_ref_cnt;
2196 	if (nmaps == 0)
2197 		return (STMF_NOT_FOUND);
2198 	itl_list = (stmf_itl_data_t **)kmem_zalloc(
2199 	    nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
2200 	mutex_enter(&stmf_state.stmf_lock);
2201 	if (nmaps != ilu->ilu_ref_cnt) {
2202 		/* Something changed, start all over */
2203 		mutex_exit(&stmf_state.stmf_lock);
2204 		kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
2205 		goto dereg_itl_start;
2206 	}
2207 	nu = 0;
2208 	for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2209 	    ilport = ilport->ilport_next) {
2210 		rw_enter(&ilport->ilport_lock, RW_WRITER);
2211 		for (iss = ilport->ilport_ss_list; iss != NULL;
2212 		    iss = iss->iss_next) {
2213 			lm = iss->iss_sm;
2214 			if (!lm)
2215 				continue;
2216 			for (i = 0; i < lm->lm_nentries; i++) {
2217 				if (lm->lm_plus[i] == NULL)
2218 					continue;
2219 				ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2220 				if ((ent->ent_lu == lu) &&
2221 				    (ent->ent_itl_datap)) {
2222 					itl_list[nu++] = ent->ent_itl_datap;
2223 					ent->ent_itl_datap = NULL;
2224 					if (nu == nmaps) {
2225 						rw_exit(&ilport->ilport_lock);
2226 						goto dai_scan_done;
2227 					}
2228 				}
2229 			} /* lun table for a session */
2230 		} /* sessions */
2231 		rw_exit(&ilport->ilport_lock);
2232 	} /* ports */
2233 
2234 dai_scan_done:
2235 	mutex_exit(&stmf_state.stmf_lock);
2236 
2237 	for (i = 0; i < nu; i++) {
2238 		stmf_do_itl_dereg(lu, itl_list[i],
2239 		    STMF_ITL_REASON_DEREG_REQUEST);
2240 	}
2241 	kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
2242 
2243 	return (STMF_SUCCESS);
2244 }
2245 
2246 stmf_status_t
2247 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun,
2248     stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
2249 {
2250 	stmf_i_scsi_session_t *iss;
2251 	stmf_itl_data_t *itl;
2252 	stmf_lun_map_ent_t *ent;
2253 	stmf_lun_map_t *lm;
2254 	int i;
2255 	uint16_t n;
2256 
2257 	if (ss == NULL) {
2258 		if (session_id == STMF_SESSION_ID_NONE)
2259 			return (STMF_INVALID_ARG);
2260 		iss = stmf_session_id_to_issptr(session_id, 1);
2261 		if (iss == NULL)
2262 			return (STMF_NOT_FOUND);
2263 	} else {
2264 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2265 		rw_enter(iss->iss_lockp, RW_WRITER);
2266 	}
2267 	lm = iss->iss_sm;
2268 	if (lm == NULL) {
2269 		rw_exit(iss->iss_lockp);
2270 		return (STMF_NOT_FOUND);
2271 	}
2272 
2273 	if (lun) {
2274 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2275 		ent = (stmf_lun_map_ent_t *)
2276 		    stmf_get_ent_from_map(iss->iss_sm, n);
2277 	} else {
2278 		if (itl_handle == NULL) {
2279 			rw_exit(iss->iss_lockp);
2280 			return (STMF_INVALID_ARG);
2281 		}
2282 		ent = NULL;
2283 		for (i = 0; i < lm->lm_nentries; i++) {
2284 			if (lm->lm_plus[i] == NULL)
2285 				continue;
2286 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2287 			if (ent->ent_itl_datap &&
2288 			    (ent->ent_itl_datap->itl_handle == itl_handle)) {
2289 				break;
2290 			}
2291 		}
2292 	}
2293 	if ((ent == NULL) || (ent->ent_lu != lu) ||
2294 	    (ent->ent_itl_datap == NULL)) {
2295 		rw_exit(iss->iss_lockp);
2296 		return (STMF_NOT_FOUND);
2297 	}
2298 	itl = ent->ent_itl_datap;
2299 	ent->ent_itl_datap = NULL;
2300 	rw_exit(iss->iss_lockp);
2301 	stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST);
2302 
2303 	return (STMF_SUCCESS);
2304 }
2305 
2306 stmf_status_t
2307 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
2308     uint64_t session_id, void **itl_handle_retp)
2309 {
2310 	stmf_i_scsi_session_t *iss;
2311 	stmf_lun_map_ent_t *ent;
2312 	stmf_lun_map_t *lm;
2313 	stmf_status_t ret;
2314 	int i;
2315 	uint16_t n;
2316 
2317 	if (ss == NULL) {
2318 		iss = stmf_session_id_to_issptr(session_id, 1);
2319 		if (iss == NULL)
2320 			return (STMF_NOT_FOUND);
2321 	} else {
2322 		iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2323 		rw_enter(iss->iss_lockp, RW_WRITER);
2324 	}
2325 
2326 	ent = NULL;
2327 	if (lun == NULL) {
2328 		lm = iss->iss_sm;
2329 		for (i = 0; i < lm->lm_nentries; i++) {
2330 			if (lm->lm_plus[i] == NULL)
2331 				continue;
2332 			ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
2333 			if (ent->ent_lu == lu)
2334 				break;
2335 		}
2336 	} else {
2337 		n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2338 		ent = (stmf_lun_map_ent_t *)
2339 		    stmf_get_ent_from_map(iss->iss_sm, n);
2340 		if (lu && (ent->ent_lu != lu))
2341 			ent = NULL;
2342 	}
2343 	if (ent && ent->ent_itl_datap) {
2344 		*itl_handle_retp = ent->ent_itl_datap->itl_handle;
2345 		ret = STMF_SUCCESS;
2346 	} else {
2347 		ret = STMF_NOT_FOUND;
2348 	}
2349 
2350 	rw_exit(iss->iss_lockp);
2351 	return (ret);
2352 }
2353 
2354 stmf_data_buf_t *
2355 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
2356     uint32_t flags)
2357 {
2358 	stmf_i_scsi_task_t *itask =
2359 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2360 	stmf_local_port_t *lport = task->task_lport;
2361 	stmf_data_buf_t *dbuf;
2362 	uint8_t ndx;
2363 
2364 	ndx = stmf_first_zero[itask->itask_allocated_buf_map];
2365 	if (ndx == 0xff)
2366 		return (NULL);
2367 	dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
2368 	    task, size, pminsize, flags);
2369 	if (dbuf) {
2370 		task->task_cur_nbufs++;
2371 		itask->itask_allocated_buf_map |= (1 << ndx);
2372 		dbuf->db_handle = ndx;
2373 		return (dbuf);
2374 	}
2375 
2376 	return (NULL);
2377 }
2378 
2379 void
2380 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
2381 {
2382 	stmf_i_scsi_task_t *itask =
2383 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2384 	stmf_local_port_t *lport = task->task_lport;
2385 
2386 	itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
2387 	task->task_cur_nbufs--;
2388 	lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
2389 }
2390 
2391 stmf_data_buf_t *
2392 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
2393 {
2394 	stmf_i_scsi_task_t *itask;
2395 
2396 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
2397 	if (h > 3)
2398 		return (NULL);
2399 	return (itask->itask_dbufs[h]);
2400 }
2401 
2402 /* ARGSUSED */
2403 struct scsi_task *
2404 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
2405 			uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
2406 {
2407 	stmf_lu_t *lu;
2408 	stmf_i_scsi_session_t *iss;
2409 	stmf_i_lu_t *ilu;
2410 	stmf_i_scsi_task_t *itask;
2411 	stmf_i_scsi_task_t **ppitask;
2412 	scsi_task_t *task;
2413 	uint64_t *p;
2414 	uint8_t	*l;
2415 	stmf_lun_map_ent_t *lun_map_ent;
2416 	uint16_t cdb_length;
2417 	uint16_t luNbr;
2418 	uint8_t new_task = 0;
2419 
2420 	/*
2421 	 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
2422 	 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
2423 	 * depend upon this alignment.
2424 	 */
2425 	if (cdb_length_in >= 16)
2426 		cdb_length = cdb_length_in + 7;
2427 	else
2428 		cdb_length = 16 + 7;
2429 	iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
2430 	luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
2431 	rw_enter(iss->iss_lockp, RW_READER);
2432 	lun_map_ent =
2433 	    (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
2434 	if (!lun_map_ent) {
2435 		lu = dlun0;
2436 	} else {
2437 		lu = lun_map_ent->ent_lu;
2438 	}
2439 	ilu = lu->lu_stmf_private;
2440 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
2441 		rw_exit(iss->iss_lockp);
2442 		return (NULL);
2443 	}
2444 	do {
2445 		if (ilu->ilu_free_tasks == NULL) {
2446 			new_task = 1;
2447 			break;
2448 		}
2449 		mutex_enter(&ilu->ilu_task_lock);
2450 		for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
2451 		    ((*ppitask)->itask_cdb_buf_size < cdb_length);
2452 		    ppitask = &((*ppitask)->itask_lu_free_next))
2453 			;
2454 		if (*ppitask) {
2455 			itask = *ppitask;
2456 			*ppitask = (*ppitask)->itask_lu_free_next;
2457 			ilu->ilu_ntasks_free--;
2458 			if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
2459 				ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
2460 		} else {
2461 			new_task = 1;
2462 		}
2463 		mutex_exit(&ilu->ilu_task_lock);
2464 	/* CONSTCOND */
2465 	} while (0);
2466 
2467 	if (!new_task) {
2468 		task = itask->itask_task;
2469 		task->task_timeout = 0;
2470 		p = (uint64_t *)&task->task_flags;
2471 		*p++ = 0; *p++ = 0; p++; p++; *p++ = 0; *p++ = 0; *p = 0;
2472 		itask->itask_ncmds = 0;
2473 	} else {
2474 		task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
2475 		    cdb_length, AF_FORCE_NOSLEEP);
2476 		if (task == NULL) {
2477 			rw_exit(iss->iss_lockp);
2478 			return (NULL);
2479 		}
2480 		task->task_lu = lu;
2481 		l = task->task_lun_no;
2482 		l[0] = lun[0];
2483 		l[1] = lun[1];
2484 		l[2] = lun[2];
2485 		l[3] = lun[3];
2486 		l[4] = lun[4];
2487 		l[5] = lun[5];
2488 		l[6] = lun[6];
2489 		l[7] = lun[7];
2490 		task->task_cdb = (uint8_t *)task->task_port_private;
2491 		if ((ulong_t)(task->task_cdb) & 7ul) {
2492 			task->task_cdb = (uint8_t *)(((ulong_t)
2493 			    (task->task_cdb) + 7ul) & ~(7ul));
2494 		}
2495 		itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
2496 		itask->itask_cdb_buf_size = cdb_length;
2497 	}
2498 	task->task_session = ss;
2499 	task->task_lport = lport;
2500 	task->task_cdb_length = cdb_length_in;
2501 	itask->itask_flags = ITASK_IN_TRANSITION;
2502 
2503 	if (new_task) {
2504 		if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
2505 			rw_exit(iss->iss_lockp);
2506 			stmf_free(task);
2507 			return (NULL);
2508 		}
2509 		mutex_enter(&ilu->ilu_task_lock);
2510 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
2511 			mutex_exit(&ilu->ilu_task_lock);
2512 			rw_exit(iss->iss_lockp);
2513 			stmf_free(task);
2514 			return (NULL);
2515 		}
2516 		itask->itask_lu_next = ilu->ilu_tasks;
2517 		if (ilu->ilu_tasks)
2518 			ilu->ilu_tasks->itask_lu_prev = itask;
2519 		ilu->ilu_tasks = itask;
2520 		/* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
2521 		ilu->ilu_ntasks++;
2522 		mutex_exit(&ilu->ilu_task_lock);
2523 	}
2524 
2525 	itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
2526 	atomic_add_32(itask->itask_ilu_task_cntr, 1);
2527 	itask->itask_start_time = ddi_get_lbolt();
2528 
2529 	if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
2530 	    lun_map_ent->ent_itl_datap) != NULL)) {
2531 		atomic_add_32(&itask->itask_itl_datap->itl_counter, 1);
2532 		task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
2533 	} else {
2534 		itask->itask_itl_datap = NULL;
2535 		task->task_lu_itl_handle = NULL;
2536 	}
2537 
2538 	rw_exit(iss->iss_lockp);
2539 	return (task);
2540 }
2541 
2542 void
2543 stmf_task_lu_free(scsi_task_t *task)
2544 {
2545 	stmf_i_scsi_task_t *itask =
2546 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2547 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
2548 	    task->task_session->ss_stmf_private;
2549 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2550 
2551 	rw_enter(iss->iss_lockp, RW_READER);
2552 	itask->itask_flags = ITASK_IN_FREE_LIST;
2553 	mutex_enter(&ilu->ilu_task_lock);
2554 	itask->itask_lu_free_next = ilu->ilu_free_tasks;
2555 	ilu->ilu_free_tasks = itask;
2556 	ilu->ilu_ntasks_free++;
2557 	mutex_exit(&ilu->ilu_task_lock);
2558 	atomic_add_32(itask->itask_ilu_task_cntr, -1);
2559 	rw_exit(iss->iss_lockp);
2560 }
2561 
2562 void
2563 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
2564 {
2565 	uint32_t	num_to_release, ndx;
2566 	stmf_i_scsi_task_t *itask;
2567 	stmf_lu_t	*lu = ilu->ilu_lu;
2568 
2569 	ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
2570 
2571 	/* free half of the minimal free of the free tasks */
2572 	num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
2573 	if (!num_to_release) {
2574 		return;
2575 	}
2576 	for (ndx = 0; ndx < num_to_release; ndx++) {
2577 		mutex_enter(&ilu->ilu_task_lock);
2578 		itask = ilu->ilu_free_tasks;
2579 		if (itask == NULL) {
2580 			mutex_exit(&ilu->ilu_task_lock);
2581 			break;
2582 		}
2583 		ilu->ilu_free_tasks = itask->itask_lu_free_next;
2584 		ilu->ilu_ntasks_free--;
2585 		mutex_exit(&ilu->ilu_task_lock);
2586 
2587 		lu->lu_task_free(itask->itask_task);
2588 		mutex_enter(&ilu->ilu_task_lock);
2589 		if (itask->itask_lu_next)
2590 			itask->itask_lu_next->itask_lu_prev =
2591 			    itask->itask_lu_prev;
2592 		if (itask->itask_lu_prev)
2593 			itask->itask_lu_prev->itask_lu_next =
2594 			    itask->itask_lu_next;
2595 		else
2596 			ilu->ilu_tasks = itask->itask_lu_next;
2597 
2598 		ilu->ilu_ntasks--;
2599 		mutex_exit(&ilu->ilu_task_lock);
2600 		stmf_free(itask->itask_task);
2601 	}
2602 }
2603 
2604 /*
2605  * Called with stmf_lock held
2606  */
2607 void
2608 stmf_check_freetask()
2609 {
2610 	stmf_i_lu_t *ilu;
2611 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
2612 
2613 	/* stmf_svc_ilu_draining may get changed after stmf_lock is released */
2614 	while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
2615 		stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
2616 		if (!ilu->ilu_ntasks_min_free) {
2617 			ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
2618 			continue;
2619 		}
2620 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
2621 		mutex_exit(&stmf_state.stmf_lock);
2622 		stmf_task_lu_check_freelist(ilu);
2623 		/*
2624 		 * we do not care about the accuracy of
2625 		 * ilu_ntasks_min_free, so we don't lock here
2626 		 */
2627 		ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
2628 		mutex_enter(&stmf_state.stmf_lock);
2629 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
2630 		cv_broadcast(&stmf_state.stmf_cv);
2631 		if (ddi_get_lbolt() >= endtime)
2632 			break;
2633 	}
2634 }
2635 
2636 void
2637 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
2638 {
2639 	clock_t l = ddi_get_lbolt();
2640 	clock_t ps = drv_usectohz(1000000);
2641 	stmf_i_scsi_task_t *itask;
2642 	scsi_task_t *task;
2643 	uint32_t to;
2644 
2645 	mutex_enter(&ilu->ilu_task_lock);
2646 	for (itask = ilu->ilu_tasks; itask != NULL;
2647 	    itask = itask->itask_lu_next) {
2648 		if (itask->itask_flags & (ITASK_IN_FREE_LIST |
2649 		    ITASK_BEING_ABORTED)) {
2650 			continue;
2651 		}
2652 		task = itask->itask_task;
2653 		if (task->task_timeout == 0)
2654 			to = stmf_default_task_timeout;
2655 		else
2656 			to = task->task_timeout;
2657 		if ((itask->itask_start_time + (to * ps)) > l)
2658 			continue;
2659 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
2660 		    STMF_TIMEOUT, NULL);
2661 	}
2662 	mutex_exit(&ilu->ilu_task_lock);
2663 }
2664 
2665 /*
2666  * Called with stmf_lock held
2667  */
2668 void
2669 stmf_check_ilu_timing()
2670 {
2671 	stmf_i_lu_t *ilu;
2672 	clock_t	endtime = ddi_get_lbolt() + drv_usectohz(10000);
2673 
2674 	/* stmf_svc_ilu_timing may get changed after stmf_lock is released */
2675 	while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
2676 		stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
2677 		if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
2678 			if (ilu->ilu_task_cntr2 == 0) {
2679 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
2680 				continue;
2681 			}
2682 		} else {
2683 			if (ilu->ilu_task_cntr1 == 0) {
2684 				ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
2685 				continue;
2686 			}
2687 		}
2688 		/*
2689 		 * If we are here then it means that there is some slowdown
2690 		 * in tasks on this lu. We need to check.
2691 		 */
2692 		ilu->ilu_flags |= ILU_STALL_DEREGISTER;
2693 		mutex_exit(&stmf_state.stmf_lock);
2694 		stmf_do_ilu_timeouts(ilu);
2695 		mutex_enter(&stmf_state.stmf_lock);
2696 		ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
2697 		cv_broadcast(&stmf_state.stmf_cv);
2698 		if (ddi_get_lbolt() >= endtime)
2699 			break;
2700 	}
2701 }
2702 
2703 /*
2704  * Kills all tasks on a lu except tm_task
2705  */
2706 void
2707 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
2708 {
2709 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2710 	stmf_i_scsi_task_t *itask;
2711 
2712 	mutex_enter(&ilu->ilu_task_lock);
2713 
2714 	for (itask = ilu->ilu_tasks; itask != NULL;
2715 	    itask = itask->itask_lu_next) {
2716 		if (itask->itask_flags & ITASK_IN_FREE_LIST)
2717 			continue;
2718 		if (itask->itask_task == tm_task)
2719 			continue;
2720 		stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
2721 	}
2722 	mutex_exit(&ilu->ilu_task_lock);
2723 }
2724 
2725 void
2726 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
2727 {
2728 	int i;
2729 	uint8_t map;
2730 
2731 	if ((map = itask->itask_allocated_buf_map) != 0) {
2732 		for (i = 0; i < 4; i++) {
2733 			if (map & 1) {
2734 				stmf_data_buf_t *dbuf;
2735 
2736 				dbuf = itask->itask_dbufs[i];
2737 				if (dbuf->db_lu_private) {
2738 					dbuf->db_lu_private = NULL;
2739 				}
2740 				lport->lport_ds->ds_free_data_buf(
2741 				    lport->lport_ds, dbuf);
2742 			}
2743 			map >>= 1;
2744 		}
2745 		itask->itask_allocated_buf_map = 0;
2746 	}
2747 }
2748 
2749 void
2750 stmf_task_free(scsi_task_t *task)
2751 {
2752 	stmf_local_port_t *lport = task->task_lport;
2753 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
2754 	    task->task_stmf_private;
2755 
2756 	DTRACE_PROBE1(stmf__task__end, scsi_task_t *, task);
2757 	stmf_free_task_bufs(itask, lport);
2758 	if (itask->itask_itl_datap) {
2759 		if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter,
2760 		    -1) == 0) {
2761 			stmf_release_itl_handle(task->task_lu,
2762 			    itask->itask_itl_datap);
2763 		}
2764 	}
2765 	lport->lport_task_free(task);
2766 	if (itask->itask_worker) {
2767 		atomic_add_32(&stmf_cur_ntasks, -1);
2768 		atomic_add_32(&itask->itask_worker->worker_ref_count, -1);
2769 	}
2770 	/*
2771 	 * After calling stmf_task_lu_free, the task pointer can no longer
2772 	 * be trusted.
2773 	 */
2774 	stmf_task_lu_free(task);
2775 }
2776 
2777 void
2778 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
2779 {
2780 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
2781 	    task->task_stmf_private;
2782 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2783 	int nv;
2784 	uint32_t old, new;
2785 	uint32_t ct;
2786 	stmf_worker_t *w, *w1;
2787 	uint8_t tm;
2788 
2789 	if (task->task_max_nbufs > 4)
2790 		task->task_max_nbufs = 4;
2791 	task->task_cur_nbufs = 0;
2792 	/* Latest value of currently running tasks */
2793 	ct = atomic_add_32_nv(&stmf_cur_ntasks, 1);
2794 
2795 	/* Select the next worker using round robin */
2796 	nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1);
2797 	if (nv >= stmf_nworkers_accepting_cmds) {
2798 		int s = nv;
2799 		do {
2800 			nv -= stmf_nworkers_accepting_cmds;
2801 		} while (nv >= stmf_nworkers_accepting_cmds);
2802 		if (nv < 0)
2803 			nv = 0;
2804 		/* Its ok if this cas fails */
2805 		(void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
2806 		    s, nv);
2807 	}
2808 	w = &stmf_workers[nv];
2809 
2810 	/*
2811 	 * A worker can be pinned by interrupt. So select the next one
2812 	 * if it has lower load.
2813 	 */
2814 	if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
2815 		w1 = stmf_workers;
2816 	} else {
2817 		w1 = &stmf_workers[nv + 1];
2818 	}
2819 	if (w1->worker_queue_depth < w->worker_queue_depth)
2820 		w = w1;
2821 	mutex_enter(&w->worker_lock);
2822 	if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
2823 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
2824 		/*
2825 		 * Maybe we are in the middle of a change. Just go to
2826 		 * the 1st worker.
2827 		 */
2828 		mutex_exit(&w->worker_lock);
2829 		w = stmf_workers;
2830 		mutex_enter(&w->worker_lock);
2831 	}
2832 	itask->itask_worker = w;
2833 	/*
2834 	 * Track max system load inside the worker as we already have the
2835 	 * worker lock (no point implementing another lock). The service
2836 	 * thread will do the comparisons and figure out the max overall
2837 	 * system load.
2838 	 */
2839 	if (w->worker_max_sys_qdepth_pu < ct)
2840 		w->worker_max_sys_qdepth_pu = ct;
2841 
2842 	do {
2843 		old = new = itask->itask_flags;
2844 		new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
2845 		if (task->task_mgmt_function) {
2846 			tm = task->task_mgmt_function;
2847 			if ((tm == TM_TARGET_RESET) ||
2848 			    (tm == TM_TARGET_COLD_RESET) ||
2849 			    (tm == TM_TARGET_WARM_RESET)) {
2850 				new |= ITASK_DEFAULT_HANDLING;
2851 			}
2852 		} else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
2853 			new |= ITASK_DEFAULT_HANDLING;
2854 		}
2855 		new &= ~ITASK_IN_TRANSITION;
2856 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
2857 	itask->itask_worker_next = NULL;
2858 	if (w->worker_task_tail) {
2859 		w->worker_task_tail->itask_worker_next = itask;
2860 	} else {
2861 		w->worker_task_head = itask;
2862 	}
2863 	w->worker_task_tail = itask;
2864 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
2865 		w->worker_max_qdepth_pu = w->worker_queue_depth;
2866 	}
2867 	atomic_add_32(&w->worker_ref_count, 1);
2868 	itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
2869 	itask->itask_ncmds = 1;
2870 	if (dbuf) {
2871 		itask->itask_allocated_buf_map = 1;
2872 		itask->itask_dbufs[0] = dbuf;
2873 		dbuf->db_handle = 0;
2874 	} else {
2875 		itask->itask_allocated_buf_map = 0;
2876 		itask->itask_dbufs[0] = NULL;
2877 	}
2878 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
2879 		cv_signal(&w->worker_cv);
2880 	mutex_exit(&w->worker_lock);
2881 
2882 	/*
2883 	 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
2884 	 * was set between checking of ILU_RESET_ACTIVE and clearing of the
2885 	 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
2886 	 */
2887 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
2888 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
2889 	}
2890 }
2891 
2892 /*
2893  * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
2894  * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
2895  * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
2896  * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
2897  * the LU will make this call only if we call the LU's abort entry point.
2898  * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
2899  *
2900  * Same logic applies for the port.
2901  *
2902  * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
2903  * and KNOWN_TO_TGT_PORT are reset.
2904  *
2905  * +++++++++++++++++++++++++++++++++++++++++++++++
2906  */
2907 
2908 stmf_status_t
2909 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
2910 {
2911 	stmf_status_t ret;
2912 
2913 	stmf_i_scsi_task_t *itask =
2914 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2915 
2916 	if (ioflags & STMF_IOF_LU_DONE) {
2917 		uint32_t new, old;
2918 		do {
2919 			new = old = itask->itask_flags;
2920 			if (new & ITASK_BEING_ABORTED)
2921 				return (STMF_ABORTED);
2922 			new &= ~ITASK_KNOWN_TO_LU;
2923 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
2924 	}
2925 	if (itask->itask_flags & ITASK_BEING_ABORTED)
2926 		return (STMF_ABORTED);
2927 #ifdef	DEBUG
2928 	if (stmf_drop_buf_counter > 0) {
2929 		if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) ==
2930 		    1)
2931 			return (STMF_SUCCESS);
2932 	}
2933 #endif
2934 	DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, task,
2935 	    stmf_data_buf_t *, dbuf);
2936 	ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
2937 	DTRACE_PROBE2(scsi__xfer__end, scsi_task_t *, task,
2938 	    stmf_data_buf_t *, dbuf);
2939 	return (ret);
2940 }
2941 
2942 void
2943 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
2944 {
2945 	stmf_i_scsi_task_t *itask =
2946 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
2947 	stmf_worker_t *w = itask->itask_worker;
2948 	uint32_t new, old;
2949 	uint8_t update_queue_flags, free_it, queue_it;
2950 
2951 	mutex_enter(&w->worker_lock);
2952 	do {
2953 		new = old = itask->itask_flags;
2954 		if (old & ITASK_BEING_ABORTED) {
2955 			mutex_exit(&w->worker_lock);
2956 			return;
2957 		}
2958 		free_it = 0;
2959 		if (iof & STMF_IOF_LPORT_DONE) {
2960 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
2961 			task->task_completion_status = dbuf->db_xfer_status;
2962 			free_it = 1;
2963 		}
2964 		/*
2965 		 * If the task is known to LU then queue it. But if
2966 		 * it is already queued (multiple completions) then
2967 		 * just update the buffer information by grabbing the
2968 		 * worker lock. If the task is not known to LU,
2969 		 * completed/aborted, then see if we need to
2970 		 * free this task.
2971 		 */
2972 		if (old & ITASK_KNOWN_TO_LU) {
2973 			free_it = 0;
2974 			update_queue_flags = 1;
2975 			if (old & ITASK_IN_WORKER_QUEUE) {
2976 				queue_it = 0;
2977 			} else {
2978 				queue_it = 1;
2979 				new |= ITASK_IN_WORKER_QUEUE;
2980 			}
2981 		} else {
2982 			update_queue_flags = 0;
2983 			queue_it = 0;
2984 		}
2985 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
2986 
2987 	if (update_queue_flags) {
2988 		uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
2989 
2990 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
2991 		itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
2992 		if (queue_it) {
2993 			itask->itask_worker_next = NULL;
2994 			if (w->worker_task_tail) {
2995 				w->worker_task_tail->itask_worker_next = itask;
2996 			} else {
2997 				w->worker_task_head = itask;
2998 			}
2999 			w->worker_task_tail = itask;
3000 			if (++(w->worker_queue_depth) >
3001 			    w->worker_max_qdepth_pu) {
3002 				w->worker_max_qdepth_pu = w->worker_queue_depth;
3003 			}
3004 			if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3005 				cv_signal(&w->worker_cv);
3006 		}
3007 	}
3008 	mutex_exit(&w->worker_lock);
3009 
3010 	if (free_it) {
3011 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3012 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3013 		    ITASK_BEING_ABORTED)) == 0) {
3014 			stmf_task_free(task);
3015 		}
3016 	}
3017 }
3018 
3019 stmf_status_t
3020 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
3021 {
3022 	DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
3023 
3024 	stmf_i_scsi_task_t *itask =
3025 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3026 	if (ioflags & STMF_IOF_LU_DONE) {
3027 		uint32_t new, old;
3028 		do {
3029 			new = old = itask->itask_flags;
3030 			if (new & ITASK_BEING_ABORTED)
3031 				return (STMF_ABORTED);
3032 			new &= ~ITASK_KNOWN_TO_LU;
3033 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3034 	}
3035 
3036 	if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
3037 		return (STMF_SUCCESS);
3038 	}
3039 
3040 	if (itask->itask_flags & ITASK_BEING_ABORTED)
3041 		return (STMF_ABORTED);
3042 
3043 	if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
3044 		task->task_status_ctrl = 0;
3045 		task->task_resid = 0;
3046 	} else if (task->task_cmd_xfer_length >
3047 	    task->task_expected_xfer_length) {
3048 		task->task_status_ctrl = TASK_SCTRL_OVER;
3049 		task->task_resid = task->task_cmd_xfer_length -
3050 		    task->task_expected_xfer_length;
3051 	} else if (task->task_nbytes_transferred <
3052 	    task->task_expected_xfer_length) {
3053 		task->task_status_ctrl = TASK_SCTRL_UNDER;
3054 		task->task_resid = task->task_expected_xfer_length -
3055 		    task->task_nbytes_transferred;
3056 	} else {
3057 		task->task_status_ctrl = 0;
3058 		task->task_resid = 0;
3059 	}
3060 	return (task->task_lport->lport_send_status(task, ioflags));
3061 }
3062 
3063 void
3064 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3065 {
3066 	stmf_i_scsi_task_t *itask =
3067 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3068 	stmf_worker_t *w = itask->itask_worker;
3069 	uint32_t new, old;
3070 	uint8_t free_it, queue_it;
3071 
3072 	mutex_enter(&w->worker_lock);
3073 	do {
3074 		new = old = itask->itask_flags;
3075 		if (old & ITASK_BEING_ABORTED) {
3076 			mutex_exit(&w->worker_lock);
3077 			return;
3078 		}
3079 		free_it = 0;
3080 		if (iof & STMF_IOF_LPORT_DONE) {
3081 			new &= ~ITASK_KNOWN_TO_TGT_PORT;
3082 			free_it = 1;
3083 		}
3084 		/*
3085 		 * If the task is known to LU then queue it. But if
3086 		 * it is already queued (multiple completions) then
3087 		 * just update the buffer information by grabbing the
3088 		 * worker lock. If the task is not known to LU,
3089 		 * completed/aborted, then see if we need to
3090 		 * free this task.
3091 		 */
3092 		if (old & ITASK_KNOWN_TO_LU) {
3093 			free_it = 0;
3094 			queue_it = 1;
3095 			if (old & ITASK_IN_WORKER_QUEUE) {
3096 				cmn_err(CE_PANIC, "status completion received"
3097 				    " when task is already in worker queue "
3098 				    " task = %p", (void *)task);
3099 			}
3100 			new |= ITASK_IN_WORKER_QUEUE;
3101 		} else {
3102 			queue_it = 0;
3103 		}
3104 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3105 	task->task_completion_status = s;
3106 
3107 	if (queue_it) {
3108 		ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
3109 		itask->itask_cmd_stack[itask->itask_ncmds++] =
3110 		    ITASK_CMD_STATUS_DONE;
3111 		itask->itask_worker_next = NULL;
3112 		if (w->worker_task_tail) {
3113 			w->worker_task_tail->itask_worker_next = itask;
3114 		} else {
3115 			w->worker_task_head = itask;
3116 		}
3117 		w->worker_task_tail = itask;
3118 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3119 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3120 		}
3121 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3122 			cv_signal(&w->worker_cv);
3123 	}
3124 	mutex_exit(&w->worker_lock);
3125 
3126 	if (free_it) {
3127 		if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3128 		    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3129 		    ITASK_BEING_ABORTED)) == 0) {
3130 			stmf_task_free(task);
3131 		} else {
3132 			cmn_err(CE_PANIC, "LU is done with the task but LPORT "
3133 			    " is not done, itask %p", (void *)itask);
3134 		}
3135 	}
3136 }
3137 
3138 void
3139 stmf_task_lu_done(scsi_task_t *task)
3140 {
3141 	stmf_i_scsi_task_t *itask =
3142 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3143 	stmf_worker_t *w = itask->itask_worker;
3144 	uint32_t new, old;
3145 
3146 	mutex_enter(&w->worker_lock);
3147 	do {
3148 		new = old = itask->itask_flags;
3149 		if (old & ITASK_BEING_ABORTED) {
3150 			mutex_exit(&w->worker_lock);
3151 			return;
3152 		}
3153 		if (old & ITASK_IN_WORKER_QUEUE) {
3154 			cmn_err(CE_PANIC, "task_lu_done received"
3155 			    " when task is in worker queue "
3156 			    " task = %p", (void *)task);
3157 		}
3158 		new &= ~ITASK_KNOWN_TO_LU;
3159 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3160 
3161 	mutex_exit(&w->worker_lock);
3162 
3163 	if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
3164 	    ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
3165 	    ITASK_BEING_ABORTED)) == 0) {
3166 		stmf_task_free(task);
3167 	} else {
3168 		cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
3169 		    " the task is still not done, task = %p", (void *)task);
3170 	}
3171 }
3172 
3173 void
3174 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
3175 {
3176 	stmf_i_scsi_task_t *itask =
3177 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3178 	stmf_worker_t *w;
3179 	uint32_t old, new;
3180 
3181 	do {
3182 		old = new = itask->itask_flags;
3183 		if ((old & ITASK_BEING_ABORTED) ||
3184 		    ((old & (ITASK_KNOWN_TO_TGT_PORT |
3185 		    ITASK_KNOWN_TO_LU)) == 0)) {
3186 			return;
3187 		}
3188 		new |= ITASK_BEING_ABORTED;
3189 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3190 	task->task_completion_status = s;
3191 	itask->itask_start_time = ddi_get_lbolt();
3192 
3193 	if (((w = itask->itask_worker) == NULL) ||
3194 	    (itask->itask_flags & ITASK_IN_TRANSITION)) {
3195 		return;
3196 	}
3197 
3198 	/* Queue it and get out */
3199 	mutex_enter(&w->worker_lock);
3200 	if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
3201 		mutex_exit(&w->worker_lock);
3202 		return;
3203 	}
3204 	atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
3205 	itask->itask_worker_next = NULL;
3206 	if (w->worker_task_tail) {
3207 		w->worker_task_tail->itask_worker_next = itask;
3208 	} else {
3209 		w->worker_task_head = itask;
3210 	}
3211 	w->worker_task_tail = itask;
3212 	if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3213 		w->worker_max_qdepth_pu = w->worker_queue_depth;
3214 	}
3215 	if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3216 		cv_signal(&w->worker_cv);
3217 	mutex_exit(&w->worker_lock);
3218 }
3219 
3220 void
3221 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
3222 {
3223 	stmf_i_scsi_task_t *itask = NULL;
3224 	uint32_t old, new, f, rf;
3225 
3226 	DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
3227 	    stmf_status_t, s);
3228 
3229 	switch (abort_cmd) {
3230 	case STMF_QUEUE_ABORT_LU:
3231 		stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
3232 		return;
3233 	case STMF_QUEUE_TASK_ABORT:
3234 		stmf_queue_task_for_abort(task, s);
3235 		return;
3236 	case STMF_REQUEUE_TASK_ABORT_LPORT:
3237 		rf = ITASK_TGT_PORT_ABORT_CALLED;
3238 		f = ITASK_KNOWN_TO_TGT_PORT;
3239 		break;
3240 	case STMF_REQUEUE_TASK_ABORT_LU:
3241 		rf = ITASK_LU_ABORT_CALLED;
3242 		f = ITASK_KNOWN_TO_LU;
3243 		break;
3244 	default:
3245 		return;
3246 	}
3247 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
3248 	f |= ITASK_BEING_ABORTED | rf;
3249 	do {
3250 		old = new = itask->itask_flags;
3251 		if ((old & f) != f) {
3252 			return;
3253 		}
3254 		new &= ~rf;
3255 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3256 }
3257 
3258 void
3259 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3260 {
3261 	char			 info[STMF_CHANGE_INFO_LEN];
3262 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3263 	unsigned long long	st;
3264 
3265 	st = s;	/* gcc fix */
3266 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
3267 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3268 		    "task %p, lu failed to abort ret=%llx", (void *)task, st);
3269 	} else if ((iof & STMF_IOF_LU_DONE) == 0) {
3270 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3271 		    "Task aborted but LU is not finished, task ="
3272 		    "%p, s=%llx, iof=%x", (void *)task, st, iof);
3273 	} else {
3274 		/*
3275 		 * LU abort successfully
3276 		 */
3277 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
3278 		return;
3279 	}
3280 
3281 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
3282 	stmf_abort_task_offline(task, 1, info);
3283 }
3284 
3285 void
3286 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
3287 {
3288 	char			 info[STMF_CHANGE_INFO_LEN];
3289 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3290 	unsigned long long	st;
3291 
3292 	st = s;
3293 	if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
3294 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3295 		    "task %p, tgt port failed to abort ret=%llx", (void *)task,
3296 		    st);
3297 	} else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
3298 		(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3299 		    "Task aborted but tgt port is not finished, "
3300 		    "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
3301 	} else {
3302 		/*
3303 		 * LU abort successfully
3304 		 */
3305 		atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_TGT_PORT);
3306 		return;
3307 	}
3308 
3309 	info[STMF_CHANGE_INFO_LEN - 1] = 0;
3310 	stmf_abort_task_offline(task, 0, info);
3311 }
3312 
3313 stmf_status_t
3314 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
3315 {
3316 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3317 	    task->task_stmf_private;
3318 	stmf_worker_t *w = itask->itask_worker;
3319 	int i;
3320 
3321 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
3322 	mutex_enter(&w->worker_lock);
3323 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
3324 		mutex_exit(&w->worker_lock);
3325 		return (STMF_BUSY);
3326 	}
3327 	for (i = 0; i < itask->itask_ncmds; i++) {
3328 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
3329 			mutex_exit(&w->worker_lock);
3330 			return (STMF_SUCCESS);
3331 		}
3332 	}
3333 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
3334 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
3335 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
3336 	} else {
3337 		clock_t t = drv_usectohz(timeout * 1000);
3338 		if (t == 0)
3339 			t = 1;
3340 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
3341 	}
3342 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
3343 		itask->itask_worker_next = NULL;
3344 		if (w->worker_task_tail) {
3345 			w->worker_task_tail->itask_worker_next = itask;
3346 		} else {
3347 			w->worker_task_head = itask;
3348 		}
3349 		w->worker_task_tail = itask;
3350 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3351 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3352 		}
3353 		atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
3354 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3355 			cv_signal(&w->worker_cv);
3356 	}
3357 	mutex_exit(&w->worker_lock);
3358 	return (STMF_SUCCESS);
3359 }
3360 
3361 stmf_status_t
3362 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
3363 {
3364 	stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
3365 	    task->task_stmf_private;
3366 	stmf_worker_t *w = itask->itask_worker;
3367 	int i;
3368 
3369 	ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
3370 	mutex_enter(&w->worker_lock);
3371 	if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
3372 		mutex_exit(&w->worker_lock);
3373 		return (STMF_BUSY);
3374 	}
3375 	for (i = 0; i < itask->itask_ncmds; i++) {
3376 		if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
3377 			mutex_exit(&w->worker_lock);
3378 			return (STMF_SUCCESS);
3379 		}
3380 	}
3381 	itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
3382 	if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
3383 		itask->itask_poll_timeout = ddi_get_lbolt() + 1;
3384 	} else {
3385 		clock_t t = drv_usectohz(timeout * 1000);
3386 		if (t == 0)
3387 			t = 1;
3388 		itask->itask_poll_timeout = ddi_get_lbolt() + t;
3389 	}
3390 	if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
3391 		itask->itask_worker_next = NULL;
3392 		if (w->worker_task_tail) {
3393 			w->worker_task_tail->itask_worker_next = itask;
3394 		} else {
3395 			w->worker_task_head = itask;
3396 		}
3397 		w->worker_task_tail = itask;
3398 		if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
3399 			w->worker_max_qdepth_pu = w->worker_queue_depth;
3400 		}
3401 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
3402 			cv_signal(&w->worker_cv);
3403 	}
3404 	mutex_exit(&w->worker_lock);
3405 	return (STMF_SUCCESS);
3406 }
3407 
3408 void
3409 stmf_do_task_abort(scsi_task_t *task)
3410 {
3411 	stmf_i_scsi_task_t	*itask = TASK_TO_ITASK(task);
3412 	stmf_lu_t		*lu;
3413 	stmf_local_port_t	*lport;
3414 	unsigned long long	 ret;
3415 	uint32_t		 old, new;
3416 	uint8_t			 call_lu_abort, call_port_abort;
3417 	char			 info[STMF_CHANGE_INFO_LEN];
3418 
3419 	lu = task->task_lu;
3420 	lport = task->task_lport;
3421 	do {
3422 		old = new = itask->itask_flags;
3423 		if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
3424 		    ITASK_KNOWN_TO_LU) {
3425 			new |= ITASK_LU_ABORT_CALLED;
3426 			call_lu_abort = 1;
3427 		} else {
3428 			call_lu_abort = 0;
3429 		}
3430 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3431 
3432 	if (call_lu_abort) {
3433 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
3434 			ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
3435 		} else {
3436 			ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
3437 		}
3438 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
3439 			stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
3440 		} else if (ret == STMF_BUSY) {
3441 			atomic_and_32(&itask->itask_flags,
3442 			    ~ITASK_LU_ABORT_CALLED);
3443 		} else if (ret != STMF_SUCCESS) {
3444 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3445 			    "Abort failed by LU %p, ret %llx", (void *)lu, ret);
3446 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3447 			stmf_abort_task_offline(task, 1, info);
3448 		}
3449 	} else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
3450 		if (ddi_get_lbolt() > (itask->itask_start_time +
3451 		    STMF_SEC2TICK(lu->lu_abort_timeout?
3452 		    lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
3453 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3454 			    "lu abort timed out");
3455 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3456 			stmf_abort_task_offline(itask->itask_task, 1, info);
3457 		}
3458 	}
3459 
3460 	do {
3461 		old = new = itask->itask_flags;
3462 		if ((old & (ITASK_KNOWN_TO_TGT_PORT |
3463 		    ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
3464 			new |= ITASK_TGT_PORT_ABORT_CALLED;
3465 			call_port_abort = 1;
3466 		} else {
3467 			call_port_abort = 0;
3468 		}
3469 	} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
3470 	if (call_port_abort) {
3471 		ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
3472 		if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
3473 			stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
3474 		} else if (ret == STMF_BUSY) {
3475 			atomic_and_32(&itask->itask_flags,
3476 			    ~ITASK_TGT_PORT_ABORT_CALLED);
3477 		} else if (ret != STMF_SUCCESS) {
3478 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3479 			    "Abort failed by tgt port %p ret %llx",
3480 			    (void *)lport, ret);
3481 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3482 			stmf_abort_task_offline(task, 0, info);
3483 		}
3484 	} else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
3485 		if (ddi_get_lbolt() > (itask->itask_start_time +
3486 		    STMF_SEC2TICK(lport->lport_abort_timeout?
3487 		    lport->lport_abort_timeout :
3488 		    ITASK_DEFAULT_ABORT_TIMEOUT))) {
3489 			(void) snprintf(info, STMF_CHANGE_INFO_LEN,
3490 			    "lport abort timed out");
3491 			info[STMF_CHANGE_INFO_LEN - 1] = 0;
3492 			stmf_abort_task_offline(itask->itask_task, 0, info);
3493 		}
3494 	}
3495 }
3496 
3497 stmf_status_t
3498 stmf_ctl(int cmd, void *obj, void *arg)
3499 {
3500 	stmf_status_t			ret;
3501 	stmf_i_lu_t			*ilu;
3502 	stmf_i_local_port_t		*ilport;
3503 	stmf_state_change_info_t	*ssci = (stmf_state_change_info_t *)arg;
3504 
3505 	mutex_enter(&stmf_state.stmf_lock);
3506 	ret = STMF_INVALID_ARG;
3507 	if (cmd & STMF_CMD_LU_OP) {
3508 		ilu = stmf_lookup_lu((stmf_lu_t *)obj);
3509 		if (ilu == NULL) {
3510 			goto stmf_ctl_lock_exit;
3511 		}
3512 		DTRACE_PROBE3(lu__state__change,
3513 		    stmf_lu_t *, ilu->ilu_lu,
3514 		    int, cmd, stmf_state_change_info_t *, ssci);
3515 	} else if (cmd & STMF_CMD_LPORT_OP) {
3516 		ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
3517 		if (ilport == NULL) {
3518 			goto stmf_ctl_lock_exit;
3519 		}
3520 		DTRACE_PROBE3(lport__state__change,
3521 		    stmf_local_port_t *, ilport->ilport_lport,
3522 		    int, cmd, stmf_state_change_info_t *, ssci);
3523 	} else {
3524 		goto stmf_ctl_lock_exit;
3525 	}
3526 
3527 	switch (cmd) {
3528 	case STMF_CMD_LU_ONLINE:
3529 		if (ilu->ilu_state == STMF_STATE_ONLINE) {
3530 			ret = STMF_ALREADY;
3531 			goto stmf_ctl_lock_exit;
3532 		}
3533 		if (ilu->ilu_state != STMF_STATE_OFFLINE) {
3534 			ret = STMF_INVALID_ARG;
3535 			goto stmf_ctl_lock_exit;
3536 		}
3537 		ilu->ilu_state = STMF_STATE_ONLINING;
3538 		mutex_exit(&stmf_state.stmf_lock);
3539 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
3540 		break;
3541 
3542 	case STMF_CMD_LU_ONLINE_COMPLETE:
3543 		if (ilu->ilu_state != STMF_STATE_ONLINING) {
3544 			ret = STMF_INVALID_ARG;
3545 			goto stmf_ctl_lock_exit;
3546 		}
3547 		if (((stmf_change_status_t *)arg)->st_completion_status ==
3548 		    STMF_SUCCESS) {
3549 			ilu->ilu_state = STMF_STATE_ONLINE;
3550 			mutex_exit(&stmf_state.stmf_lock);
3551 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
3552 			    STMF_ACK_LU_ONLINE_COMPLETE, arg);
3553 			mutex_enter(&stmf_state.stmf_lock);
3554 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
3555 		} else {
3556 			/* XXX: should throw a meesage an record more data */
3557 			ilu->ilu_state = STMF_STATE_OFFLINE;
3558 		}
3559 		ret = STMF_SUCCESS;
3560 		goto stmf_ctl_lock_exit;
3561 
3562 	case STMF_CMD_LU_OFFLINE:
3563 		if (ilu->ilu_state == STMF_STATE_OFFLINE) {
3564 			ret = STMF_ALREADY;
3565 			goto stmf_ctl_lock_exit;
3566 		}
3567 		if (ilu->ilu_state != STMF_STATE_ONLINE) {
3568 			ret = STMF_INVALID_ARG;
3569 			goto stmf_ctl_lock_exit;
3570 		}
3571 		ilu->ilu_state = STMF_STATE_OFFLINING;
3572 		mutex_exit(&stmf_state.stmf_lock);
3573 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
3574 		break;
3575 
3576 	case STMF_CMD_LU_OFFLINE_COMPLETE:
3577 		if (ilu->ilu_state != STMF_STATE_OFFLINING) {
3578 			ret = STMF_INVALID_ARG;
3579 			goto stmf_ctl_lock_exit;
3580 		}
3581 		if (((stmf_change_status_t *)arg)->st_completion_status ==
3582 		    STMF_SUCCESS) {
3583 			ilu->ilu_state = STMF_STATE_OFFLINE;
3584 			mutex_exit(&stmf_state.stmf_lock);
3585 			((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
3586 			    STMF_ACK_LU_OFFLINE_COMPLETE, arg);
3587 			mutex_enter(&stmf_state.stmf_lock);
3588 		} else {
3589 			ilu->ilu_state = STMF_STATE_ONLINE;
3590 			stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
3591 		}
3592 		mutex_exit(&stmf_state.stmf_lock);
3593 		break;
3594 
3595 	/*
3596 	 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
3597 	 * It's related with hardware disable/enable.
3598 	 */
3599 	case STMF_CMD_LPORT_ONLINE:
3600 		if (ilport->ilport_state == STMF_STATE_ONLINE) {
3601 			ret = STMF_ALREADY;
3602 			goto stmf_ctl_lock_exit;
3603 		}
3604 		if (ilport->ilport_state != STMF_STATE_OFFLINE) {
3605 			ret = STMF_INVALID_ARG;
3606 			goto stmf_ctl_lock_exit;
3607 		}
3608 
3609 		/*
3610 		 * Only user request can recover the port from the
3611 		 * FORCED_OFFLINE state
3612 		 */
3613 		if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
3614 			if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
3615 				ret = STMF_FAILURE;
3616 				goto stmf_ctl_lock_exit;
3617 			}
3618 		}
3619 
3620 		/*
3621 		 * Avoid too frequent request to online
3622 		 */
3623 		if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
3624 			ilport->ilport_online_times = 0;
3625 			ilport->ilport_avg_interval = 0;
3626 		}
3627 		if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
3628 		    (ilport->ilport_online_times >= 4)) {
3629 			ret = STMF_FAILURE;
3630 			ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
3631 			stmf_trace(NULL, "stmf_ctl: too frequent request to "
3632 			    "online the port");
3633 			cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
3634 			    "online the port, set FORCED_OFFLINE now");
3635 			goto stmf_ctl_lock_exit;
3636 		}
3637 		if (ilport->ilport_online_times > 0) {
3638 			if (ilport->ilport_online_times == 1) {
3639 				ilport->ilport_avg_interval = ddi_get_lbolt() -
3640 				    ilport->ilport_last_online_clock;
3641 			} else {
3642 				ilport->ilport_avg_interval =
3643 				    (ilport->ilport_avg_interval +
3644 				    ddi_get_lbolt() -
3645 				    ilport->ilport_last_online_clock) >> 1;
3646 			}
3647 		}
3648 		ilport->ilport_last_online_clock = ddi_get_lbolt();
3649 		ilport->ilport_online_times++;
3650 
3651 		/*
3652 		 * Submit online service request
3653 		 */
3654 		ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
3655 		ilport->ilport_state = STMF_STATE_ONLINING;
3656 		mutex_exit(&stmf_state.stmf_lock);
3657 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
3658 		break;
3659 
3660 	case STMF_CMD_LPORT_ONLINE_COMPLETE:
3661 		if (ilport->ilport_state != STMF_STATE_ONLINING) {
3662 			ret = STMF_INVALID_ARG;
3663 			goto stmf_ctl_lock_exit;
3664 		}
3665 		if (((stmf_change_status_t *)arg)->st_completion_status ==
3666 		    STMF_SUCCESS) {
3667 			ilport->ilport_state = STMF_STATE_ONLINE;
3668 			mutex_exit(&stmf_state.stmf_lock);
3669 			((stmf_local_port_t *)obj)->lport_ctl(
3670 			    (stmf_local_port_t *)obj,
3671 			    STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
3672 			mutex_enter(&stmf_state.stmf_lock);
3673 		} else {
3674 			ilport->ilport_state = STMF_STATE_OFFLINE;
3675 		}
3676 		ret = STMF_SUCCESS;
3677 		goto stmf_ctl_lock_exit;
3678 
3679 	case STMF_CMD_LPORT_OFFLINE:
3680 		if (ilport->ilport_state == STMF_STATE_OFFLINE) {
3681 			ret = STMF_ALREADY;
3682 			goto stmf_ctl_lock_exit;
3683 		}
3684 		if (ilport->ilport_state != STMF_STATE_ONLINE) {
3685 			ret = STMF_INVALID_ARG;
3686 			goto stmf_ctl_lock_exit;
3687 		}
3688 		ilport->ilport_state = STMF_STATE_OFFLINING;
3689 		mutex_exit(&stmf_state.stmf_lock);
3690 		stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
3691 		break;
3692 
3693 	case STMF_CMD_LPORT_OFFLINE_COMPLETE:
3694 		if (ilport->ilport_state != STMF_STATE_OFFLINING) {
3695 			ret = STMF_INVALID_ARG;
3696 			goto stmf_ctl_lock_exit;
3697 		}
3698 		if (((stmf_change_status_t *)arg)->st_completion_status ==
3699 		    STMF_SUCCESS) {
3700 			ilport->ilport_state = STMF_STATE_OFFLINE;
3701 			mutex_exit(&stmf_state.stmf_lock);
3702 			((stmf_local_port_t *)obj)->lport_ctl(
3703 			    (stmf_local_port_t *)obj,
3704 			    STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
3705 			mutex_enter(&stmf_state.stmf_lock);
3706 		} else {
3707 			ilport->ilport_state = STMF_STATE_ONLINE;
3708 		}
3709 		mutex_exit(&stmf_state.stmf_lock);
3710 		break;
3711 
3712 	default:
3713 		cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
3714 		ret = STMF_INVALID_ARG;
3715 		goto stmf_ctl_lock_exit;
3716 	}
3717 
3718 	return (STMF_SUCCESS);
3719 
3720 stmf_ctl_lock_exit:;
3721 	mutex_exit(&stmf_state.stmf_lock);
3722 	return (ret);
3723 }
3724 
3725 /* ARGSUSED */
3726 stmf_status_t
3727 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
3728 						uint32_t *bufsizep)
3729 {
3730 	return (STMF_NOT_SUPPORTED);
3731 }
3732 
3733 /* ARGSUSED */
3734 stmf_status_t
3735 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
3736 						uint32_t *bufsizep)
3737 {
3738 	uint32_t cl = SI_GET_CLASS(cmd);
3739 
3740 	if (cl == SI_STMF) {
3741 		return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
3742 	}
3743 	if (cl == SI_LPORT) {
3744 		return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
3745 		    arg2, buf, bufsizep));
3746 	} else if (cl == SI_LU) {
3747 		return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
3748 		    bufsizep));
3749 	}
3750 
3751 	return (STMF_NOT_SUPPORTED);
3752 }
3753 
3754 /*
3755  * Used by port providers. pwwn is 8 byte wwn, sdid is th devid used by
3756  * stmf to register local ports. The ident should have 20 bytes in buffer
3757  * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
3758  */
3759 void
3760 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
3761     uint8_t protocol_id)
3762 {
3763 	sdid->protocol_id = protocol_id;
3764 	sdid->piv = 1;
3765 	sdid->code_set = CODE_SET_ASCII;
3766 	sdid->association = ID_IS_TARGET_PORT;
3767 	sdid->ident_length = 20;
3768 	(void) sprintf((char *)sdid->ident,
3769 	    "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
3770 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
3771 }
3772 
3773 stmf_xfer_data_t *
3774 stmf_prepare_tpgs_data()
3775 {
3776 	stmf_xfer_data_t *xd;
3777 	stmf_i_local_port_t *ilport;
3778 	uint8_t *p;
3779 	uint32_t sz, asz, nports;
3780 
3781 	mutex_enter(&stmf_state.stmf_lock);
3782 	/* The spec only allows for 255 ports to be reported */
3783 	nports = min(stmf_state.stmf_nlports, 255);
3784 	sz = (nports * 4) + 12;
3785 	asz = sz + sizeof (*xd) - 4;
3786 	xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
3787 	if (xd == NULL) {
3788 		mutex_exit(&stmf_state.stmf_lock);
3789 		return (NULL);
3790 	}
3791 	xd->alloc_size = asz;
3792 	xd->size_left = sz;
3793 
3794 	p = xd->buf;
3795 
3796 	*((uint32_t *)p) = BE_32(sz - 4);
3797 	p += 4;
3798 	p[0] = 0x80;	/* PREF */
3799 	p[1] = 1;	/* AO_SUP */
3800 	p[7] = nports & 0xff;
3801 	p += 8;
3802 	for (ilport = stmf_state.stmf_ilportlist; ilport && nports;
3803 	    nports++, ilport = ilport->ilport_next, p += 4) {
3804 		((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
3805 	}
3806 	mutex_exit(&stmf_state.stmf_lock);
3807 
3808 	return (xd);
3809 }
3810 
3811 
3812 static uint16_t stmf_lu_id_gen_number = 0;
3813 
3814 stmf_status_t
3815 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
3816 {
3817 	uint8_t *p;
3818 	struct timeval32 timestamp32;
3819 	uint32_t *t = (uint32_t *)&timestamp32;
3820 	struct ether_addr mac;
3821 	uint8_t *e = (uint8_t *)&mac;
3822 
3823 	if (company_id == COMPANY_ID_NONE)
3824 		company_id = COMPANY_ID_SUN;
3825 
3826 	if (lu_id->ident_length != 0x10)
3827 		return (STMF_INVALID_ARG);
3828 
3829 	p = (uint8_t *)lu_id;
3830 
3831 	atomic_add_16(&stmf_lu_id_gen_number, 1);
3832 
3833 	p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
3834 	p[4] = ((company_id >> 20) & 0xf) | 0x60;
3835 	p[5] = (company_id >> 12) & 0xff;
3836 	p[6] = (company_id >> 4) & 0xff;
3837 	p[7] = (company_id << 4) & 0xf0;
3838 	if (!localetheraddr((struct ether_addr *)NULL, &mac)) {
3839 		int hid = BE_32((int)zone_get_hostid(NULL));
3840 
3841 		e[0] = (hid >> 24) & 0xff;
3842 		e[1] = (hid >> 16) & 0xff;
3843 		e[2] = (hid >> 8) & 0xff;
3844 		e[3] = hid & 0xff;
3845 		e[4] = e[5] = 0;
3846 	}
3847 	bcopy(e, p+8, 6);
3848 	uniqtime32(&timestamp32);
3849 	*t = BE_32(*t);
3850 	bcopy(t, p+14, 4);
3851 	p[18] = (stmf_lu_id_gen_number >> 8) & 0xff;
3852 	p[19] = stmf_lu_id_gen_number & 0xff;
3853 
3854 	return (STMF_SUCCESS);
3855 }
3856 
3857 /*
3858  * saa is sense key, ASC, ASCQ
3859  */
3860 void
3861 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
3862 {
3863 	uint8_t sd[18];
3864 	task->task_scsi_status = st;
3865 	if (st == 2) {
3866 		bzero(sd, 18);
3867 		sd[0] = 0x70;
3868 		sd[2] = (saa >> 16) & 0xf;
3869 		sd[7] = 10;
3870 		sd[12] = (saa >> 8) & 0xff;
3871 		sd[13] = saa & 0xff;
3872 		task->task_sense_data = sd;
3873 		task->task_sense_length = 18;
3874 	} else {
3875 		task->task_sense_data = NULL;
3876 		task->task_sense_length = 0;
3877 	}
3878 	(void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
3879 }
3880 
3881 uint32_t
3882 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
3883     uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
3884 {
3885 	uint8_t		*p = NULL;
3886 	uint8_t		small_buf[32];
3887 	uint32_t	sz = 0;
3888 	uint32_t	n = 4;
3889 	uint32_t	m = 0;
3890 	uint32_t	last_bit = 0;
3891 
3892 	if (page_len < 4)
3893 		return (0);
3894 	if (page_len > 65535)
3895 		page_len = 65535;
3896 
3897 	page[0] = byte0;
3898 	page[1] = 0x83;
3899 
3900 	/* CONSTCOND */
3901 	while (1) {
3902 		m += sz;
3903 		if (sz && (page_len > n)) {
3904 			uint32_t copysz;
3905 			copysz = page_len > (n + sz) ? sz : page_len - n;
3906 			bcopy(p, page + n, copysz);
3907 			n += copysz;
3908 		}
3909 		vpd_mask &= ~last_bit;
3910 		if (vpd_mask == 0)
3911 			break;
3912 
3913 		if (vpd_mask & STMF_VPD_LU_ID) {
3914 			last_bit = STMF_VPD_LU_ID;
3915 			sz = task->task_lu->lu_id->ident_length + 4;
3916 			p = (uint8_t *)task->task_lu->lu_id;
3917 			continue;
3918 		} else if (vpd_mask & STMF_VPD_TARGET_ID) {
3919 			last_bit = STMF_VPD_TARGET_ID;
3920 			sz = task->task_lport->lport_id->ident_length + 4;
3921 			p = (uint8_t *)task->task_lport->lport_id;
3922 			continue;
3923 		} else if (vpd_mask & STMF_VPD_TP_GROUP) {
3924 			last_bit = STMF_VPD_TP_GROUP;
3925 			p = small_buf;
3926 			bzero(p, 8);
3927 			p[0] = 1;
3928 			p[1] = 0x15;
3929 			p[3] = 4;
3930 			/* Group ID is always 0 */
3931 			sz = 8;
3932 			continue;
3933 		} else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
3934 			stmf_i_local_port_t *ilport;
3935 
3936 			last_bit = STMF_VPD_RELATIVE_TP_ID;
3937 			p = small_buf;
3938 			bzero(p, 8);
3939 			p[0] = 1;
3940 			p[1] = 0x14;
3941 			p[3] = 4;
3942 			ilport = (stmf_i_local_port_t *)
3943 			    task->task_lport->lport_stmf_private;
3944 			p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
3945 			p[7] = ilport->ilport_rtpid & 0xff;
3946 			sz = 8;
3947 			continue;
3948 		} else {
3949 			cmn_err(CE_WARN, "Invalid vpd_mask");
3950 			break;
3951 		}
3952 	}
3953 
3954 	page[2] = (m >> 8) & 0xff;
3955 	page[3] = m & 0xff;
3956 
3957 	return (n);
3958 }
3959 
3960 void
3961 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
3962 {
3963 	stmf_i_scsi_task_t *itask =
3964 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
3965 	stmf_xfer_data_t *xd;
3966 	uint32_t sz, minsz;
3967 
3968 	itask->itask_flags |= ITASK_DEFAULT_HANDLING;
3969 	task->task_cmd_xfer_length =
3970 	    ((((uint32_t)task->task_cdb[6]) << 24) |
3971 	    (((uint32_t)task->task_cdb[7]) << 16) |
3972 	    (((uint32_t)task->task_cdb[8]) << 8) |
3973 	    ((uint32_t)task->task_cdb[9]));
3974 
3975 	if (task->task_additional_flags &
3976 	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
3977 		task->task_expected_xfer_length =
3978 		    task->task_cmd_xfer_length;
3979 	}
3980 
3981 	if (task->task_cmd_xfer_length < 16) {
3982 		stmf_scsilib_send_status(task, STATUS_CHECK,
3983 		    STMF_SAA_INVALID_FIELD_IN_CDB);
3984 		return;
3985 	}
3986 
3987 	sz = min(task->task_expected_xfer_length,
3988 	    task->task_cmd_xfer_length);
3989 
3990 	xd = stmf_prepare_tpgs_data();
3991 
3992 	if (xd == NULL) {
3993 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
3994 		    STMF_ALLOC_FAILURE, NULL);
3995 		return;
3996 	}
3997 
3998 	sz = min(sz, xd->size_left);
3999 	xd->size_left = sz;
4000 	minsz = min(512, sz);
4001 
4002 	if (dbuf == NULL)
4003 		dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
4004 	if (dbuf == NULL) {
4005 		kmem_free(xd, xd->alloc_size);
4006 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4007 		    STMF_ALLOC_FAILURE, NULL);
4008 		return;
4009 	}
4010 	dbuf->db_lu_private = xd;
4011 	stmf_xd_to_dbuf(dbuf);
4012 
4013 	dbuf->db_flags = DB_DIRECTION_TO_RPORT;
4014 	(void) stmf_xfer_data(task, dbuf, 0);
4015 
4016 }
4017 
4018 void
4019 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
4020 {
4021 	switch (task->task_mgmt_function) {
4022 	/*
4023 	 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
4024 	 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
4025 	 * in these cases. This needs to be changed to abort only the required
4026 	 * set.
4027 	 */
4028 	case TM_ABORT_TASK:
4029 	case TM_ABORT_TASK_SET:
4030 	case TM_CLEAR_TASK_SET:
4031 	case TM_LUN_RESET:
4032 		stmf_handle_lun_reset(task);
4033 		return;
4034 	case TM_TARGET_RESET:
4035 	case TM_TARGET_COLD_RESET:
4036 	case TM_TARGET_WARM_RESET:
4037 		stmf_handle_target_reset(task);
4038 		return;
4039 	default:
4040 		/* We dont support this task mgmt function */
4041 		stmf_scsilib_send_status(task, STATUS_CHECK,
4042 		    STMF_SAA_INVALID_FIELD_IN_CMD_IU);
4043 		return;
4044 	}
4045 }
4046 
4047 void
4048 stmf_handle_lun_reset(scsi_task_t *task)
4049 {
4050 	stmf_i_scsi_task_t *itask;
4051 	stmf_i_lu_t *ilu;
4052 
4053 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4054 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4055 
4056 	/*
4057 	 * To sync with target reset, grab this lock. The LU is not going
4058 	 * anywhere as there is atleast one task pending (this task).
4059 	 */
4060 	mutex_enter(&stmf_state.stmf_lock);
4061 
4062 	if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4063 		mutex_exit(&stmf_state.stmf_lock);
4064 		stmf_scsilib_send_status(task, STATUS_CHECK,
4065 		    STMF_SAA_OPERATION_IN_PROGRESS);
4066 		return;
4067 	}
4068 	atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4069 	mutex_exit(&stmf_state.stmf_lock);
4070 
4071 	/*
4072 	 * Mark this task as the one causing LU reset so that we know who
4073 	 * was responsible for setting the ILU_RESET_ACTIVE. In case this
4074 	 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
4075 	 */
4076 	itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
4077 
4078 	/* Initiatiate abort on all commands on this LU except this one */
4079 	stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
4080 
4081 	/* Start polling on this task */
4082 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4083 	    != STMF_SUCCESS) {
4084 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4085 		    NULL);
4086 		return;
4087 	}
4088 }
4089 
4090 void
4091 stmf_handle_target_reset(scsi_task_t *task)
4092 {
4093 	stmf_i_scsi_task_t *itask;
4094 	stmf_i_lu_t *ilu;
4095 	stmf_i_scsi_session_t *iss;
4096 	stmf_lun_map_t *lm;
4097 	stmf_lun_map_ent_t *lm_ent;
4098 	int i, lf;
4099 
4100 	itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4101 	iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
4102 	ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4103 
4104 	/*
4105 	 * To sync with LUN reset, grab this lock. The session is not going
4106 	 * anywhere as there is atleast one task pending (this task).
4107 	 */
4108 	mutex_enter(&stmf_state.stmf_lock);
4109 
4110 	/* Grab the session lock as a writer to prevent any changes in it */
4111 	rw_enter(iss->iss_lockp, RW_WRITER);
4112 
4113 	if (iss->iss_flags & ISS_RESET_ACTIVE) {
4114 		rw_exit(iss->iss_lockp);
4115 		mutex_exit(&stmf_state.stmf_lock);
4116 		stmf_scsilib_send_status(task, STATUS_CHECK,
4117 		    STMF_SAA_OPERATION_IN_PROGRESS);
4118 		return;
4119 	}
4120 	atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
4121 
4122 	/*
4123 	 * Now go through each LUN in this session and make sure all of them
4124 	 * can be reset.
4125 	 */
4126 	lm = iss->iss_sm;
4127 	for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
4128 		if (lm->lm_plus[i] == NULL)
4129 			continue;
4130 		lf++;
4131 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4132 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4133 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4134 			atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4135 			rw_exit(iss->iss_lockp);
4136 			mutex_exit(&stmf_state.stmf_lock);
4137 			stmf_scsilib_send_status(task, STATUS_CHECK,
4138 			    STMF_SAA_OPERATION_IN_PROGRESS);
4139 			return;
4140 		}
4141 	}
4142 	if (lf == 0) {
4143 		/* No luns in this session */
4144 		atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4145 		rw_exit(iss->iss_lockp);
4146 		mutex_exit(&stmf_state.stmf_lock);
4147 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
4148 		return;
4149 	}
4150 
4151 	/* ok, start the damage */
4152 	itask->itask_flags |= ITASK_DEFAULT_HANDLING |
4153 	    ITASK_CAUSING_TARGET_RESET;
4154 	for (i = 0; i < lm->lm_nentries; i++) {
4155 		if (lm->lm_plus[i] == NULL)
4156 			continue;
4157 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4158 		ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
4159 		atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
4160 	}
4161 	rw_exit(iss->iss_lockp);
4162 	mutex_exit(&stmf_state.stmf_lock);
4163 
4164 	for (i = 0; i < lm->lm_nentries; i++) {
4165 		if (lm->lm_plus[i] == NULL)
4166 			continue;
4167 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4168 		stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
4169 		    lm_ent->ent_lu);
4170 	}
4171 
4172 	/* Start polling on this task */
4173 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
4174 	    != STMF_SUCCESS) {
4175 		stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
4176 		    NULL);
4177 		return;
4178 	}
4179 }
4180 
4181 int
4182 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
4183 {
4184 	scsi_task_t *task = itask->itask_task;
4185 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4186 	    task->task_session->ss_stmf_private;
4187 
4188 	rw_enter(iss->iss_lockp, RW_WRITER);
4189 	if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
4190 	    (task->task_cdb[0] == SCMD_INQUIRY)) {
4191 		rw_exit(iss->iss_lockp);
4192 		return (0);
4193 	}
4194 	atomic_and_32(&iss->iss_flags,
4195 	    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
4196 	rw_exit(iss->iss_lockp);
4197 
4198 	if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4199 		return (0);
4200 	}
4201 	stmf_scsilib_send_status(task, STATUS_CHECK,
4202 	    STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
4203 	return (1);
4204 }
4205 
4206 void
4207 stmf_worker_init()
4208 {
4209 	uint32_t i;
4210 
4211 	/* Make local copy of global tunables */
4212 	stmf_i_max_nworkers = stmf_max_nworkers;
4213 	stmf_i_min_nworkers = stmf_min_nworkers;
4214 
4215 	ASSERT(stmf_workers == NULL);
4216 	if (stmf_i_min_nworkers < 4) {
4217 		stmf_i_min_nworkers = 4;
4218 	}
4219 	if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
4220 		stmf_i_max_nworkers = stmf_i_min_nworkers;
4221 	}
4222 	stmf_workers = (stmf_worker_t *)kmem_zalloc(
4223 	    sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
4224 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4225 		stmf_worker_t *w = &stmf_workers[i];
4226 		mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
4227 		cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
4228 	}
4229 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4230 	stmf_workers_state = STMF_WORKERS_ENABLED;
4231 
4232 	/* Workers will be started by stmf_worker_mgmt() */
4233 
4234 	/* Lets wait for atleast one worker to start */
4235 	while (stmf_nworkers_cur == 0)
4236 		delay(drv_usectohz(20 * 1000));
4237 	stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
4238 }
4239 
4240 stmf_status_t
4241 stmf_worker_fini()
4242 {
4243 	int i;
4244 	clock_t sb;
4245 
4246 	if (stmf_workers_state == STMF_WORKERS_DISABLED)
4247 		return (STMF_SUCCESS);
4248 	ASSERT(stmf_workers);
4249 	stmf_workers_state = STMF_WORKERS_DISABLED;
4250 	stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
4251 	cv_signal(&stmf_state.stmf_cv);
4252 
4253 	sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
4254 	/* Wait for all the threads to die */
4255 	while (stmf_nworkers_cur != 0) {
4256 		if (ddi_get_lbolt() > sb) {
4257 			stmf_workers_state = STMF_WORKERS_ENABLED;
4258 			return (STMF_BUSY);
4259 		}
4260 		delay(drv_usectohz(100 * 1000));
4261 	}
4262 	for (i = 0; i < stmf_i_max_nworkers; i++) {
4263 		stmf_worker_t *w = &stmf_workers[i];
4264 		mutex_destroy(&w->worker_lock);
4265 		cv_destroy(&w->worker_cv);
4266 	}
4267 	kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
4268 	stmf_workers = NULL;
4269 
4270 	return (STMF_SUCCESS);
4271 }
4272 
4273 void
4274 stmf_worker_task(void *arg)
4275 {
4276 	stmf_worker_t *w;
4277 	stmf_i_scsi_session_t *iss;
4278 	scsi_task_t *task;
4279 	stmf_i_scsi_task_t *itask;
4280 	stmf_data_buf_t *dbuf;
4281 	stmf_lu_t *lu;
4282 	clock_t wait_timer = 0;
4283 	clock_t wait_ticks;
4284 	uint32_t old, new;
4285 	uint8_t curcmd;
4286 	uint8_t abort_free;
4287 	uint8_t wait_queue;
4288 	uint8_t dec_qdepth;
4289 
4290 	w = (stmf_worker_t *)arg;
4291 	wait_ticks = drv_usectohz(10000);
4292 
4293 	mutex_enter(&w->worker_lock);
4294 	w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
4295 stmf_worker_loop:;
4296 	if ((w->worker_ref_count == 0) &&
4297 	    (w->worker_flags & STMF_WORKER_TERMINATE)) {
4298 		w->worker_flags &= ~(STMF_WORKER_STARTED |
4299 		    STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
4300 		w->worker_tid = NULL;
4301 		mutex_exit(&w->worker_lock);
4302 		thread_exit();
4303 	}
4304 	/* CONSTCOND */
4305 	while (1) {
4306 		dec_qdepth = 0;
4307 		if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
4308 			wait_timer = 0;
4309 			if (w->worker_wait_head) {
4310 				ASSERT(w->worker_wait_tail);
4311 				if (w->worker_task_head == NULL)
4312 					w->worker_task_head =
4313 					    w->worker_wait_head;
4314 				else
4315 					w->worker_task_tail->itask_worker_next =
4316 					    w->worker_wait_head;
4317 				w->worker_task_tail = w->worker_wait_tail;
4318 				w->worker_wait_head = w->worker_wait_tail =
4319 				    NULL;
4320 			}
4321 		}
4322 		if ((itask = w->worker_task_head) == NULL) {
4323 			break;
4324 		}
4325 		task = itask->itask_task;
4326 		w->worker_task_head = itask->itask_worker_next;
4327 		if (w->worker_task_head == NULL)
4328 			w->worker_task_tail = NULL;
4329 
4330 		wait_queue = 0;
4331 		abort_free = 0;
4332 		if (itask->itask_ncmds > 0) {
4333 			curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
4334 		} else {
4335 			ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
4336 		}
4337 		do {
4338 			old = itask->itask_flags;
4339 			if (old & ITASK_BEING_ABORTED) {
4340 				itask->itask_ncmds = 1;
4341 				curcmd = itask->itask_cmd_stack[0] =
4342 				    ITASK_CMD_ABORT;
4343 				goto out_itask_flag_loop;
4344 			} else if ((curcmd & ITASK_CMD_MASK) ==
4345 			    ITASK_CMD_NEW_TASK) {
4346 				new = old | ITASK_KNOWN_TO_LU;
4347 			} else {
4348 				goto out_itask_flag_loop;
4349 			}
4350 		} while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4351 
4352 out_itask_flag_loop:
4353 
4354 		/*
4355 		 * Decide if this task needs to go to a queue and/or if
4356 		 * we can decrement the itask_cmd_stack.
4357 		 */
4358 		if (curcmd == ITASK_CMD_ABORT) {
4359 			if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
4360 			    ITASK_KNOWN_TO_TGT_PORT)) {
4361 				wait_queue = 1;
4362 			} else {
4363 				abort_free = 1;
4364 			}
4365 		} else if ((curcmd & ITASK_CMD_POLL) &&
4366 		    (itask->itask_poll_timeout > ddi_get_lbolt())) {
4367 			wait_queue = 1;
4368 		}
4369 
4370 		if (wait_queue) {
4371 			itask->itask_worker_next = NULL;
4372 			if (w->worker_wait_tail) {
4373 				w->worker_wait_tail->itask_worker_next = itask;
4374 			} else {
4375 				w->worker_wait_head = itask;
4376 			}
4377 			w->worker_wait_tail = itask;
4378 			if (wait_timer == 0) {
4379 				wait_timer = ddi_get_lbolt() + wait_ticks;
4380 			}
4381 		} else if ((--(itask->itask_ncmds)) != 0) {
4382 			itask->itask_worker_next = NULL;
4383 			if (w->worker_task_tail) {
4384 				w->worker_task_tail->itask_worker_next = itask;
4385 			} else {
4386 				w->worker_task_head = itask;
4387 			}
4388 			w->worker_task_tail = itask;
4389 		} else {
4390 			atomic_and_32(&itask->itask_flags,
4391 			    ~ITASK_IN_WORKER_QUEUE);
4392 			/*
4393 			 * This is where the queue depth should go down by
4394 			 * one but we delay that on purpose to account for
4395 			 * the call into the provider. The actual decrement
4396 			 * happens after the worker has done its job.
4397 			 */
4398 			dec_qdepth = 1;
4399 		}
4400 
4401 		/* We made it here means we are going to call LU */
4402 		if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
4403 			lu = task->task_lu;
4404 		else
4405 			lu = dlun0;
4406 		dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
4407 		mutex_exit(&w->worker_lock);
4408 		curcmd &= ITASK_CMD_MASK;
4409 		switch (curcmd) {
4410 		case ITASK_CMD_NEW_TASK:
4411 			iss = (stmf_i_scsi_session_t *)
4412 			    task->task_session->ss_stmf_private;
4413 			if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
4414 				if (stmf_handle_cmd_during_ic(itask))
4415 					break;
4416 			}
4417 #ifdef	DEBUG
4418 			if (stmf_drop_task_counter > 0) {
4419 				if (atomic_add_32_nv(
4420 				    (uint32_t *)&stmf_drop_task_counter,
4421 				    -1) == 1) {
4422 					break;
4423 				}
4424 			}
4425 #endif
4426 			DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
4427 			lu->lu_new_task(task, dbuf);
4428 			break;
4429 		case ITASK_CMD_DATA_XFER_DONE:
4430 			lu->lu_dbuf_xfer_done(task, dbuf);
4431 			break;
4432 		case ITASK_CMD_STATUS_DONE:
4433 			lu->lu_send_status_done(task);
4434 			break;
4435 		case ITASK_CMD_ABORT:
4436 			if (abort_free) {
4437 				stmf_task_free(task);
4438 			} else {
4439 				stmf_do_task_abort(task);
4440 			}
4441 			break;
4442 		case ITASK_CMD_POLL_LU:
4443 			if (!wait_queue) {
4444 				lu->lu_task_poll(task);
4445 			}
4446 			break;
4447 		case ITASK_CMD_POLL_LPORT:
4448 			if (!wait_queue)
4449 				task->task_lport->lport_task_poll(task);
4450 			break;
4451 		case ITASK_CMD_SEND_STATUS:
4452 		/* case ITASK_CMD_XFER_DATA: */
4453 			break;
4454 		}
4455 		mutex_enter(&w->worker_lock);
4456 		if (dec_qdepth) {
4457 			w->worker_queue_depth--;
4458 		}
4459 	}
4460 	if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
4461 		if (w->worker_ref_count == 0)
4462 			goto stmf_worker_loop;
4463 		else
4464 			wait_timer = ddi_get_lbolt() + 1;
4465 	}
4466 	w->worker_flags &= ~STMF_WORKER_ACTIVE;
4467 	if (wait_timer) {
4468 		(void) cv_timedwait(&w->worker_cv, &w->worker_lock, wait_timer);
4469 	} else {
4470 		cv_wait(&w->worker_cv, &w->worker_lock);
4471 	}
4472 	w->worker_flags |= STMF_WORKER_ACTIVE;
4473 	goto stmf_worker_loop;
4474 }
4475 
4476 void
4477 stmf_worker_mgmt()
4478 {
4479 	int i;
4480 	int workers_needed;
4481 	uint32_t qd;
4482 	clock_t tps, d = 0;
4483 	uint32_t cur_max_ntasks = 0;
4484 	stmf_worker_t *w;
4485 
4486 	/* Check if we are trying to increase the # of threads */
4487 	for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
4488 		if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
4489 			stmf_nworkers_cur++;
4490 			stmf_nworkers_accepting_cmds++;
4491 		} else {
4492 			/* Wait for transition to complete */
4493 			return;
4494 		}
4495 	}
4496 	/* Check if we are trying to decrease the # of workers */
4497 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
4498 		if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
4499 			stmf_nworkers_cur--;
4500 			/*
4501 			 * stmf_nworkers_accepting_cmds has already been
4502 			 * updated by the request to reduce the # of workers.
4503 			 */
4504 		} else {
4505 			/* Wait for transition to complete */
4506 			return;
4507 		}
4508 	}
4509 	/* Check if we are being asked to quit */
4510 	if (stmf_workers_state != STMF_WORKERS_ENABLED) {
4511 		if (stmf_nworkers_cur) {
4512 			workers_needed = 0;
4513 			goto worker_mgmt_trigger_change;
4514 		}
4515 		return;
4516 	}
4517 	/* Check if we are starting */
4518 	if (stmf_nworkers_cur < stmf_i_min_nworkers) {
4519 		workers_needed = stmf_i_min_nworkers;
4520 		goto worker_mgmt_trigger_change;
4521 	}
4522 
4523 	tps = drv_usectohz(1 * 1000 * 1000);
4524 	if ((stmf_wm_last != 0) &&
4525 	    ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
4526 		qd = 0;
4527 		for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
4528 			qd += stmf_workers[i].worker_max_qdepth_pu;
4529 			stmf_workers[i].worker_max_qdepth_pu = 0;
4530 			if (stmf_workers[i].worker_max_sys_qdepth_pu >
4531 			    cur_max_ntasks) {
4532 				cur_max_ntasks =
4533 				    stmf_workers[i].worker_max_sys_qdepth_pu;
4534 			}
4535 			stmf_workers[i].worker_max_sys_qdepth_pu = 0;
4536 		}
4537 	}
4538 	stmf_wm_last = ddi_get_lbolt();
4539 	if (d <= tps) {
4540 		/* still ramping up */
4541 		return;
4542 	}
4543 	/* max qdepth cannot be more than max tasks */
4544 	if (qd > cur_max_ntasks)
4545 		qd = cur_max_ntasks;
4546 
4547 	/* See if we have more workers */
4548 	if (qd < stmf_nworkers_accepting_cmds) {
4549 		/*
4550 		 * Since we dont reduce the worker count right away, monitor
4551 		 * the highest load during the scale_down_delay.
4552 		 */
4553 		if (qd > stmf_worker_scale_down_qd)
4554 			stmf_worker_scale_down_qd = qd;
4555 		if (stmf_worker_scale_down_timer == 0) {
4556 			stmf_worker_scale_down_timer = ddi_get_lbolt() +
4557 			    drv_usectohz(stmf_worker_scale_down_delay *
4558 			    1000 * 1000);
4559 			return;
4560 		}
4561 		if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
4562 			return;
4563 		}
4564 		/* Its time to reduce the workers */
4565 		if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
4566 			stmf_worker_scale_down_qd = stmf_i_min_nworkers;
4567 		if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
4568 			stmf_worker_scale_down_qd = stmf_i_max_nworkers;
4569 		if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
4570 			return;
4571 		workers_needed = stmf_worker_scale_down_qd;
4572 		stmf_worker_scale_down_qd = 0;
4573 		goto worker_mgmt_trigger_change;
4574 	}
4575 	stmf_worker_scale_down_qd = 0;
4576 	stmf_worker_scale_down_timer = 0;
4577 	if (qd > stmf_i_max_nworkers)
4578 		qd = stmf_i_max_nworkers;
4579 	if (qd < stmf_i_min_nworkers)
4580 		qd = stmf_i_min_nworkers;
4581 	if (qd == stmf_nworkers_cur)
4582 		return;
4583 	workers_needed = qd;
4584 	goto worker_mgmt_trigger_change;
4585 
4586 	/* NOTREACHED */
4587 	return;
4588 
4589 worker_mgmt_trigger_change:
4590 	ASSERT(workers_needed != stmf_nworkers_cur);
4591 	if (workers_needed > stmf_nworkers_cur) {
4592 		stmf_nworkers_needed = workers_needed;
4593 		for (i = stmf_nworkers_cur; i < workers_needed; i++) {
4594 			w = &stmf_workers[i];
4595 			w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
4596 			    (void *)&stmf_workers[i], 0, &p0, TS_RUN,
4597 			    minclsyspri);
4598 		}
4599 		return;
4600 	}
4601 	/* At this point we know that we are decreasing the # of workers */
4602 	stmf_nworkers_accepting_cmds = workers_needed;
4603 	stmf_nworkers_needed = workers_needed;
4604 	/* Signal the workers that its time to quit */
4605 	for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
4606 		w = &stmf_workers[i];
4607 		ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
4608 		mutex_enter(&w->worker_lock);
4609 		w->worker_flags |= STMF_WORKER_TERMINATE;
4610 		if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4611 			cv_signal(&w->worker_cv);
4612 		mutex_exit(&w->worker_lock);
4613 	}
4614 }
4615 
4616 /*
4617  * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
4618  * If all the data has been filled out, frees the xd and makes
4619  * db_lu_private NULL.
4620  */
4621 void
4622 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf)
4623 {
4624 	stmf_xfer_data_t *xd;
4625 	uint8_t *p;
4626 	int i;
4627 	uint32_t s;
4628 
4629 	xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
4630 	dbuf->db_data_size = 0;
4631 	dbuf->db_relative_offset = xd->size_done;
4632 	for (i = 0; i < dbuf->db_sglist_length; i++) {
4633 		s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
4634 		p = &xd->buf[xd->size_done];
4635 		bcopy(p, dbuf->db_sglist[i].seg_addr, s);
4636 		xd->size_left -= s;
4637 		xd->size_done += s;
4638 		dbuf->db_data_size += s;
4639 		if (xd->size_left == 0) {
4640 			kmem_free(xd, xd->alloc_size);
4641 			dbuf->db_lu_private = NULL;
4642 			return;
4643 		}
4644 	}
4645 }
4646 
4647 /* ARGSUSED */
4648 stmf_status_t
4649 stmf_dlun0_task_alloc(scsi_task_t *task)
4650 {
4651 	return (STMF_SUCCESS);
4652 }
4653 
4654 void
4655 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
4656 {
4657 	uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
4658 	stmf_i_scsi_session_t *iss;
4659 	uint32_t sz, minsz;
4660 	uint8_t *p;
4661 	stmf_xfer_data_t *xd;
4662 	uint8_t inq_page_length = 31;
4663 
4664 	if (task->task_mgmt_function) {
4665 		stmf_scsilib_handle_task_mgmt(task);
4666 		return;
4667 	}
4668 
4669 	switch (cdbp[0]) {
4670 	case SCMD_INQUIRY:
4671 		/*
4672 		 * Basic protocol checks.  In addition, only reply to
4673 		 * standard inquiry.  Otherwise, the LU provider needs
4674 		 * to respond.
4675 		 */
4676 
4677 		if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
4678 			stmf_scsilib_send_status(task, STATUS_CHECK,
4679 			    STMF_SAA_INVALID_FIELD_IN_CDB);
4680 			return;
4681 		}
4682 
4683 		task->task_cmd_xfer_length =
4684 		    (((uint32_t)cdbp[3]) << 8) | cdbp[4];
4685 
4686 		if (task->task_additional_flags &
4687 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4688 			task->task_expected_xfer_length =
4689 			    task->task_cmd_xfer_length;
4690 		}
4691 
4692 		sz = min(task->task_expected_xfer_length,
4693 		    min(36, task->task_cmd_xfer_length));
4694 		minsz = 36;
4695 
4696 		if (sz == 0) {
4697 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
4698 			return;
4699 		}
4700 
4701 		if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
4702 			/*
4703 			 * Ignore any preallocated dbuf if the size is less
4704 			 * than 36. It will be freed during the task_free.
4705 			 */
4706 			dbuf = NULL;
4707 		}
4708 		if (dbuf == NULL)
4709 			dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
4710 		if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
4711 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4712 			    STMF_ALLOC_FAILURE, NULL);
4713 			return;
4714 		}
4715 		dbuf->db_lu_private = NULL;
4716 
4717 		p = dbuf->db_sglist[0].seg_addr;
4718 
4719 		/*
4720 		 * Standard inquiry handling only.
4721 		 */
4722 
4723 		bzero(p, inq_page_length + 5);
4724 
4725 		p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
4726 		p[2] = 5;
4727 		p[3] = 0x12;
4728 		p[4] = inq_page_length;
4729 		p[6] = 0x80;
4730 
4731 		(void) strncpy((char *)p+8, "SUN     ", 8);
4732 		(void) strncpy((char *)p+16, "COMSTAR	       ", 16);
4733 		(void) strncpy((char *)p+32, "1.0 ", 4);
4734 
4735 		dbuf->db_data_size = sz;
4736 		dbuf->db_relative_offset = 0;
4737 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
4738 		(void) stmf_xfer_data(task, dbuf, 0);
4739 
4740 		return;
4741 
4742 	case SCMD_REPORT_LUNS:
4743 		task->task_cmd_xfer_length =
4744 		    ((((uint32_t)task->task_cdb[6]) << 24) |
4745 		    (((uint32_t)task->task_cdb[7]) << 16) |
4746 		    (((uint32_t)task->task_cdb[8]) << 8) |
4747 		    ((uint32_t)task->task_cdb[9]));
4748 
4749 		if (task->task_additional_flags &
4750 		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4751 			task->task_expected_xfer_length =
4752 			    task->task_cmd_xfer_length;
4753 		}
4754 
4755 		sz = min(task->task_expected_xfer_length,
4756 		    task->task_cmd_xfer_length);
4757 
4758 		if (sz < 16) {
4759 			stmf_scsilib_send_status(task, STATUS_CHECK,
4760 			    STMF_SAA_INVALID_FIELD_IN_CDB);
4761 			return;
4762 		}
4763 
4764 		iss = (stmf_i_scsi_session_t *)
4765 		    task->task_session->ss_stmf_private;
4766 		rw_enter(iss->iss_lockp, RW_WRITER);
4767 		xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
4768 		rw_exit(iss->iss_lockp);
4769 
4770 		if (xd == NULL) {
4771 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4772 			    STMF_ALLOC_FAILURE, NULL);
4773 			return;
4774 		}
4775 
4776 		sz = min(sz, xd->size_left);
4777 		xd->size_left = sz;
4778 		minsz = min(512, sz);
4779 
4780 		if (dbuf == NULL)
4781 			dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
4782 		if (dbuf == NULL) {
4783 			kmem_free(xd, xd->alloc_size);
4784 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4785 			    STMF_ALLOC_FAILURE, NULL);
4786 			return;
4787 		}
4788 		dbuf->db_lu_private = xd;
4789 		stmf_xd_to_dbuf(dbuf);
4790 
4791 		atomic_and_32(&iss->iss_flags,
4792 		    ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
4793 		dbuf->db_flags = DB_DIRECTION_TO_RPORT;
4794 		(void) stmf_xfer_data(task, dbuf, 0);
4795 		return;
4796 	}
4797 
4798 	stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
4799 }
4800 
4801 void
4802 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
4803 {
4804 	if (dbuf->db_xfer_status != STMF_SUCCESS) {
4805 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4806 		    dbuf->db_xfer_status, NULL);
4807 		return;
4808 	}
4809 	task->task_nbytes_transferred = dbuf->db_data_size;
4810 	if (dbuf->db_lu_private) {
4811 		/* There is more */
4812 		stmf_xd_to_dbuf(dbuf);
4813 		(void) stmf_xfer_data(task, dbuf, 0);
4814 		return;
4815 	}
4816 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
4817 }
4818 
4819 /* ARGSUSED */
4820 void
4821 stmf_dlun0_status_done(scsi_task_t *task)
4822 {
4823 }
4824 
4825 /* ARGSUSED */
4826 void
4827 stmf_dlun0_task_free(scsi_task_t *task)
4828 {
4829 }
4830 
4831 /* ARGSUSED */
4832 stmf_status_t
4833 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
4834 {
4835 	scsi_task_t *task = (scsi_task_t *)arg;
4836 	stmf_i_scsi_task_t *itask =
4837 	    (stmf_i_scsi_task_t *)task->task_stmf_private;
4838 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4839 	int i;
4840 	uint8_t map;
4841 
4842 	ASSERT(abort_cmd == STMF_LU_ABORT_TASK);
4843 	if ((task->task_mgmt_function) && (itask->itask_flags &
4844 	    (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
4845 		switch (task->task_mgmt_function) {
4846 		case TM_ABORT_TASK:
4847 		case TM_ABORT_TASK_SET:
4848 		case TM_CLEAR_TASK_SET:
4849 		case TM_LUN_RESET:
4850 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
4851 			break;
4852 		case TM_TARGET_RESET:
4853 		case TM_TARGET_COLD_RESET:
4854 		case TM_TARGET_WARM_RESET:
4855 			stmf_abort_target_reset(task);
4856 			break;
4857 		}
4858 		return (STMF_ABORT_SUCCESS);
4859 	}
4860 
4861 	/*
4862 	 * OK so its not a task mgmt. Make sure we free any xd sitting
4863 	 * inside any dbuf.
4864 	 */
4865 	if ((map = itask->itask_allocated_buf_map) != 0) {
4866 		for (i = 0; i < 4; i++) {
4867 			if ((map & 1) &&
4868 			    ((itask->itask_dbufs[i])->db_lu_private)) {
4869 				stmf_xfer_data_t *xd;
4870 				stmf_data_buf_t *dbuf;
4871 
4872 				dbuf = itask->itask_dbufs[i];
4873 				xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
4874 				dbuf->db_lu_private = NULL;
4875 				kmem_free(xd, xd->alloc_size);
4876 			}
4877 			map >>= 1;
4878 		}
4879 	}
4880 	return (STMF_ABORT_SUCCESS);
4881 }
4882 
4883 void
4884 stmf_dlun0_task_poll(struct scsi_task *task)
4885 {
4886 	/* Right now we only do this for handling task management functions */
4887 	ASSERT(task->task_mgmt_function);
4888 
4889 	switch (task->task_mgmt_function) {
4890 	case TM_ABORT_TASK:
4891 	case TM_ABORT_TASK_SET:
4892 	case TM_CLEAR_TASK_SET:
4893 	case TM_LUN_RESET:
4894 		(void) stmf_lun_reset_poll(task->task_lu, task, 0);
4895 		return;
4896 	case TM_TARGET_RESET:
4897 	case TM_TARGET_COLD_RESET:
4898 	case TM_TARGET_WARM_RESET:
4899 		stmf_target_reset_poll(task);
4900 		return;
4901 	}
4902 }
4903 
4904 /* ARGSUSED */
4905 void
4906 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
4907 {
4908 	/* This function will never be called */
4909 	cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
4910 }
4911 
4912 void
4913 stmf_dlun_init()
4914 {
4915 	stmf_i_lu_t *ilu;
4916 
4917 	dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
4918 	dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
4919 	dlun0->lu_new_task = stmf_dlun0_new_task;
4920 	dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
4921 	dlun0->lu_send_status_done = stmf_dlun0_status_done;
4922 	dlun0->lu_task_free = stmf_dlun0_task_free;
4923 	dlun0->lu_abort = stmf_dlun0_abort;
4924 	dlun0->lu_task_poll = stmf_dlun0_task_poll;
4925 	dlun0->lu_ctl = stmf_dlun0_ctl;
4926 
4927 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
4928 	ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
4929 }
4930 
4931 stmf_status_t
4932 stmf_dlun_fini()
4933 {
4934 	stmf_i_lu_t *ilu;
4935 
4936 	ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
4937 
4938 	ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
4939 	if (ilu->ilu_ntasks) {
4940 		stmf_i_scsi_task_t *itask, *nitask;
4941 
4942 		nitask = ilu->ilu_tasks;
4943 		do {
4944 			itask = nitask;
4945 			nitask = itask->itask_lu_next;
4946 			dlun0->lu_task_free(itask->itask_task);
4947 			stmf_free(itask->itask_task);
4948 		} while (nitask != NULL);
4949 
4950 	}
4951 	stmf_free(dlun0);
4952 	return (STMF_SUCCESS);
4953 }
4954 
4955 void
4956 stmf_abort_target_reset(scsi_task_t *task)
4957 {
4958 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4959 	    task->task_session->ss_stmf_private;
4960 	stmf_lun_map_t *lm;
4961 	stmf_lun_map_ent_t *lm_ent;
4962 	stmf_i_lu_t *ilu;
4963 	int i;
4964 
4965 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
4966 
4967 	rw_enter(iss->iss_lockp, RW_READER);
4968 	lm = iss->iss_sm;
4969 	for (i = 0; i < lm->lm_nentries; i++) {
4970 		if (lm->lm_plus[i] == NULL)
4971 			continue;
4972 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
4973 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
4974 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4975 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
4976 		}
4977 	}
4978 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
4979 	rw_exit(iss->iss_lockp);
4980 }
4981 
4982 /*
4983  * The return value is only used by function managing target reset.
4984  */
4985 stmf_status_t
4986 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
4987 {
4988 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4989 	int ntasks_pending;
4990 
4991 	ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
4992 	/*
4993 	 * This function is also used during Target reset. The idea is that
4994 	 * once all the commands are aborted, call the LU's reset entry
4995 	 * point (abort entry point with a reset flag). But if this Task
4996 	 * mgmt is running on this LU then all the tasks cannot be aborted.
4997 	 * one task (this task) will still be running which is OK.
4998 	 */
4999 	if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
5000 	    (ntasks_pending == 1))) {
5001 		stmf_status_t ret;
5002 
5003 		if ((task->task_mgmt_function == TM_LUN_RESET) ||
5004 		    (task->task_mgmt_function == TM_TARGET_RESET) ||
5005 		    (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
5006 		    (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
5007 			ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
5008 		} else {
5009 			ret = STMF_SUCCESS;
5010 		}
5011 		if (ret == STMF_SUCCESS) {
5012 			atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
5013 		}
5014 		if (target_reset) {
5015 			return (ret);
5016 		}
5017 		if (ret == STMF_SUCCESS) {
5018 			stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5019 			return (ret);
5020 		}
5021 		if (ret != STMF_BUSY) {
5022 			stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
5023 			return (ret);
5024 		}
5025 	}
5026 
5027 	if (target_reset) {
5028 		/* Tell target reset polling code that we are not done */
5029 		return (STMF_BUSY);
5030 	}
5031 
5032 	if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5033 	    != STMF_SUCCESS) {
5034 		stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5035 		    STMF_ALLOC_FAILURE, NULL);
5036 		return (STMF_SUCCESS);
5037 	}
5038 
5039 	return (STMF_SUCCESS);
5040 }
5041 
5042 void
5043 stmf_target_reset_poll(struct scsi_task *task)
5044 {
5045 	stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
5046 	    task->task_session->ss_stmf_private;
5047 	stmf_lun_map_t *lm;
5048 	stmf_lun_map_ent_t *lm_ent;
5049 	stmf_i_lu_t *ilu;
5050 	stmf_status_t ret;
5051 	int i;
5052 	int not_done = 0;
5053 
5054 	ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
5055 
5056 	rw_enter(iss->iss_lockp, RW_READER);
5057 	lm = iss->iss_sm;
5058 	for (i = 0; i < lm->lm_nentries; i++) {
5059 		if (lm->lm_plus[i] == NULL)
5060 			continue;
5061 		lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
5062 		ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
5063 		if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5064 			rw_exit(iss->iss_lockp);
5065 			ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
5066 			rw_enter(iss->iss_lockp, RW_READER);
5067 			if (ret == STMF_SUCCESS)
5068 				continue;
5069 			not_done = 1;
5070 			if (ret != STMF_BUSY) {
5071 				rw_exit(iss->iss_lockp);
5072 				stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5073 				    STMF_ABORTED, NULL);
5074 				return;
5075 			}
5076 		}
5077 	}
5078 	rw_exit(iss->iss_lockp);
5079 
5080 	if (not_done) {
5081 		if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5082 		    != STMF_SUCCESS) {
5083 			stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5084 			    STMF_ALLOC_FAILURE, NULL);
5085 			return;
5086 		}
5087 		return;
5088 	}
5089 
5090 	atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
5091 
5092 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5093 }
5094 
5095 stmf_status_t
5096 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
5097 {
5098 	stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5099 
5100 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5101 		return (STMF_INVALID_ARG);
5102 	}
5103 
5104 	STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
5105 	return (STMF_SUCCESS);
5106 }
5107 
5108 stmf_status_t
5109 stmf_lu_remove_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 == STMF_EVENT_ALL) {
5114 		STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
5115 		return (STMF_SUCCESS);
5116 	}
5117 
5118 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5119 		return (STMF_INVALID_ARG);
5120 	}
5121 
5122 	STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
5123 	return (STMF_SUCCESS);
5124 }
5125 
5126 stmf_status_t
5127 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
5128 {
5129 	stmf_i_local_port_t *ilport =
5130 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5131 
5132 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5133 		return (STMF_INVALID_ARG);
5134 	}
5135 
5136 	STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
5137 	return (STMF_SUCCESS);
5138 }
5139 
5140 stmf_status_t
5141 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
5142 {
5143 	stmf_i_local_port_t *ilport =
5144 	    (stmf_i_local_port_t *)lport->lport_stmf_private;
5145 
5146 	if (eventid == STMF_EVENT_ALL) {
5147 		STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
5148 		return (STMF_SUCCESS);
5149 	}
5150 
5151 	if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
5152 		return (STMF_INVALID_ARG);
5153 	}
5154 
5155 	STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
5156 	return (STMF_SUCCESS);
5157 }
5158 
5159 void
5160 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
5161 {
5162 	if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
5163 	    (ilu->ilu_lu->lu_event_handler != NULL)) {
5164 		ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
5165 	}
5166 }
5167 
5168 void
5169 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
5170 				uint32_t flags)
5171 {
5172 	if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
5173 	    (ilport->ilport_lport->lport_event_handler != NULL)) {
5174 		ilport->ilport_lport->lport_event_handler(
5175 		    ilport->ilport_lport, eventid, arg, flags);
5176 	}
5177 }
5178 
5179 void
5180 stmf_svc_init()
5181 {
5182 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5183 		return;
5184 	stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
5185 	    TASKQ_DEFAULTPRI, 0);
5186 	(void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
5187 	    stmf_svc, 0, DDI_SLEEP);
5188 }
5189 
5190 stmf_status_t
5191 stmf_svc_fini()
5192 {
5193 	uint32_t i;
5194 
5195 	mutex_enter(&stmf_state.stmf_lock);
5196 	if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
5197 		stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
5198 		cv_signal(&stmf_state.stmf_cv);
5199 	}
5200 	mutex_exit(&stmf_state.stmf_lock);
5201 
5202 	/* Wait for 5 seconds */
5203 	for (i = 0; i < 500; i++) {
5204 		if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
5205 			delay(drv_usectohz(10000));
5206 		else
5207 			break;
5208 	}
5209 	if (i == 500)
5210 		return (STMF_BUSY);
5211 
5212 	ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
5213 
5214 	return (STMF_SUCCESS);
5215 }
5216 
5217 /* ARGSUSED */
5218 void
5219 stmf_svc(void *arg)
5220 {
5221 	stmf_svc_req_t *req, **preq;
5222 	clock_t td;
5223 	clock_t	drain_start, drain_next = 0;
5224 	clock_t	timing_start, timing_next = 0;
5225 	clock_t worker_delay = 0;
5226 	int deq;
5227 	stmf_lu_t *lu;
5228 	stmf_i_lu_t *ilu;
5229 	stmf_local_port_t *lport;
5230 	stmf_i_local_port_t *ilport, *next_ilport;
5231 	stmf_i_scsi_session_t *iss;
5232 
5233 	td = drv_usectohz(20000);
5234 
5235 	mutex_enter(&stmf_state.stmf_lock);
5236 	stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
5237 
5238 stmf_svc_loop:
5239 	if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) {
5240 		stmf_state.stmf_svc_flags &=
5241 		    ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
5242 		mutex_exit(&stmf_state.stmf_lock);
5243 		return;
5244 	}
5245 
5246 	if (stmf_state.stmf_svc_active) {
5247 		int waitq_add = 0;
5248 		req = stmf_state.stmf_svc_active;
5249 		stmf_state.stmf_svc_active = req->svc_next;
5250 
5251 		switch (req->svc_cmd) {
5252 		case STMF_CMD_LPORT_ONLINE:
5253 			/* Fallthrough */
5254 		case STMF_CMD_LPORT_OFFLINE:
5255 			/* Fallthrough */
5256 		case STMF_CMD_LU_ONLINE:
5257 			/* Nothing to do */
5258 			waitq_add = 1;
5259 			break;
5260 
5261 		case STMF_CMD_LU_OFFLINE:
5262 			/* Remove all mappings of this LU */
5263 			stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
5264 			/* Kill all the pending I/Os for this LU */
5265 			mutex_exit(&stmf_state.stmf_lock);
5266 			stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
5267 			    STMF_ABORTED);
5268 			mutex_enter(&stmf_state.stmf_lock);
5269 			waitq_add = 1;
5270 			break;
5271 		default:
5272 			cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
5273 			    req->svc_cmd);
5274 		}
5275 
5276 		if (waitq_add) {
5277 			/* Put it in the wait queue */
5278 			req->svc_next = stmf_state.stmf_svc_waiting;
5279 			stmf_state.stmf_svc_waiting = req;
5280 		}
5281 	}
5282 
5283 	/* The waiting list is not going to be modified by anybody else */
5284 	mutex_exit(&stmf_state.stmf_lock);
5285 
5286 	for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) {
5287 		req = *preq;
5288 		deq = 0;
5289 		switch (req->svc_cmd) {
5290 		case STMF_CMD_LU_ONLINE:
5291 			lu = (stmf_lu_t *)req->svc_obj;
5292 			deq = 1;
5293 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5294 			break;
5295 
5296 		case STMF_CMD_LU_OFFLINE:
5297 			lu = (stmf_lu_t *)req->svc_obj;
5298 			ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
5299 			if (ilu->ilu_ntasks != ilu->ilu_ntasks_free)
5300 				break;
5301 			deq = 1;
5302 			lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
5303 			break;
5304 
5305 		case STMF_CMD_LPORT_OFFLINE:
5306 			/* Fallthrough */
5307 		case STMF_CMD_LPORT_ONLINE:
5308 			lport = (stmf_local_port_t *)req->svc_obj;
5309 			deq = 1;
5310 			lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
5311 			break;
5312 		}
5313 		if (deq) {
5314 			*preq = req->svc_next;
5315 			kmem_free(req, req->svc_req_alloc_size);
5316 		} else {
5317 			preq = &req->svc_next;
5318 		}
5319 	}
5320 
5321 	mutex_enter(&stmf_state.stmf_lock);
5322 	if (stmf_state.stmf_svc_active == NULL) {
5323 		/* Do timeouts */
5324 		if (stmf_state.stmf_nlus &&
5325 		    ((!timing_next) || (ddi_get_lbolt() >= timing_next))) {
5326 			if (!stmf_state.stmf_svc_ilu_timing) {
5327 				/* we are starting a new round */
5328 				stmf_state.stmf_svc_ilu_timing =
5329 				    stmf_state.stmf_ilulist;
5330 				timing_start = ddi_get_lbolt();
5331 			}
5332 			stmf_check_ilu_timing();
5333 			if (!stmf_state.stmf_svc_ilu_timing) {
5334 				/* we finished a complete round */
5335 				timing_next =
5336 				    timing_start + drv_usectohz(5*1000*1000);
5337 			} else {
5338 				/* we still have some ilu items to check */
5339 				timing_next =
5340 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5341 			}
5342 			if (stmf_state.stmf_svc_active)
5343 				goto stmf_svc_loop;
5344 		}
5345 		/* Check if there are free tasks to clear */
5346 		if (stmf_state.stmf_nlus &&
5347 		    ((!drain_next) || (ddi_get_lbolt() >= drain_next))) {
5348 			if (!stmf_state.stmf_svc_ilu_draining) {
5349 				/* we are starting a new round */
5350 				stmf_state.stmf_svc_ilu_draining =
5351 				    stmf_state.stmf_ilulist;
5352 				drain_start = ddi_get_lbolt();
5353 			}
5354 			stmf_check_freetask();
5355 			if (!stmf_state.stmf_svc_ilu_draining) {
5356 				/* we finished a complete round */
5357 				drain_next =
5358 				    drain_start + drv_usectohz(10*1000*1000);
5359 			} else {
5360 				/* we still have some ilu items to check */
5361 				drain_next =
5362 				    ddi_get_lbolt() + drv_usectohz(1*1000*1000);
5363 			}
5364 			if (stmf_state.stmf_svc_active)
5365 				goto stmf_svc_loop;
5366 		}
5367 
5368 		/* Check if we need to run worker_mgmt */
5369 		if (ddi_get_lbolt() > worker_delay) {
5370 			stmf_worker_mgmt();
5371 			worker_delay = ddi_get_lbolt() +
5372 			    stmf_worker_mgmt_delay;
5373 		}
5374 
5375 		/* Check if any active session got its 1st LUN */
5376 		if (stmf_state.stmf_process_initial_luns) {
5377 			int stmf_level = 0;
5378 			int port_level;
5379 			for (ilport = stmf_state.stmf_ilportlist; ilport;
5380 			    ilport = next_ilport) {
5381 				next_ilport = ilport->ilport_next;
5382 				if ((ilport->ilport_flags &
5383 				    ILPORT_SS_GOT_INITIAL_LUNS) == 0) {
5384 					continue;
5385 				}
5386 				port_level = 0;
5387 				rw_enter(&ilport->ilport_lock, RW_READER);
5388 				for (iss = ilport->ilport_ss_list; iss;
5389 				    iss = iss->iss_next) {
5390 					if ((iss->iss_flags &
5391 					    ISS_GOT_INITIAL_LUNS) == 0) {
5392 						continue;
5393 					}
5394 					port_level++;
5395 					stmf_level++;
5396 					atomic_and_32(&iss->iss_flags,
5397 					    ~ISS_GOT_INITIAL_LUNS);
5398 					atomic_or_32(&iss->iss_flags,
5399 					    ISS_EVENT_ACTIVE);
5400 					rw_exit(&ilport->ilport_lock);
5401 					mutex_exit(&stmf_state.stmf_lock);
5402 					stmf_generate_lport_event(ilport,
5403 					    LPORT_EVENT_INITIAL_LUN_MAPPED,
5404 					    iss->iss_ss, 0);
5405 					atomic_and_32(&iss->iss_flags,
5406 					    ~ISS_EVENT_ACTIVE);
5407 					mutex_enter(&stmf_state.stmf_lock);
5408 					/*
5409 					 * scan all the ilports again as the
5410 					 * ilport list might have changed.
5411 					 */
5412 					next_ilport =
5413 					    stmf_state.stmf_ilportlist;
5414 					break;
5415 				}
5416 				if (port_level == 0) {
5417 					atomic_and_32(&ilport->ilport_flags,
5418 					    ~ILPORT_SS_GOT_INITIAL_LUNS);
5419 				}
5420 				/* drop the lock if we are holding it. */
5421 				if (rw_lock_held(&ilport->ilport_lock))
5422 					rw_exit(&ilport->ilport_lock);
5423 
5424 				/* Max 4 session at a time */
5425 				if (stmf_level >= 4) {
5426 					break;
5427 				}
5428 			}
5429 			if (stmf_level == 0) {
5430 				stmf_state.stmf_process_initial_luns = 0;
5431 			}
5432 		}
5433 
5434 		stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
5435 		(void) cv_timedwait(&stmf_state.stmf_cv, &stmf_state.stmf_lock,
5436 		    ddi_get_lbolt() + td);
5437 		stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
5438 	}
5439 	goto stmf_svc_loop;
5440 }
5441 
5442 void
5443 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
5444 {
5445 	stmf_svc_req_t *req;
5446 	int s;
5447 
5448 	ASSERT(!mutex_owned(&stmf_state.stmf_lock));
5449 	s = sizeof (stmf_svc_req_t);
5450 	if (info->st_additional_info) {
5451 		s += strlen(info->st_additional_info) + 1;
5452 	}
5453 	req = kmem_zalloc(s, KM_SLEEP);
5454 
5455 	req->svc_cmd = cmd;
5456 	req->svc_obj = obj;
5457 	req->svc_info.st_rflags = info->st_rflags;
5458 	if (info->st_additional_info) {
5459 		req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
5460 		    sizeof (stmf_svc_req_t)));
5461 		(void) strcpy(req->svc_info.st_additional_info,
5462 		    info->st_additional_info);
5463 	}
5464 	req->svc_req_alloc_size = s;
5465 
5466 	mutex_enter(&stmf_state.stmf_lock);
5467 	req->svc_next = stmf_state.stmf_svc_active;
5468 	stmf_state.stmf_svc_active = req;
5469 	if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
5470 		cv_signal(&stmf_state.stmf_cv);
5471 	}
5472 	mutex_exit(&stmf_state.stmf_lock);
5473 }
5474 
5475 void
5476 stmf_trace(caddr_t ident, const char *fmt, ...)
5477 {
5478 	va_list args;
5479 	char tbuf[160];
5480 	int len;
5481 
5482 	if (!stmf_trace_on)
5483 		return;
5484 	len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
5485 	    ddi_get_lbolt());
5486 	va_start(args, fmt);
5487 	len += vsnprintf(tbuf + len, 158 - len, fmt, args);
5488 	va_end(args);
5489 
5490 	if (len > 158) {
5491 		len = 158;
5492 	}
5493 	tbuf[len++] = '\n';
5494 	tbuf[len] = 0;
5495 
5496 	mutex_enter(&trace_buf_lock);
5497 	bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
5498 	trace_buf_curndx += len;
5499 	if (trace_buf_curndx > (trace_buf_size - 320))
5500 		trace_buf_curndx = 0;
5501 	mutex_exit(&trace_buf_lock);
5502 }
5503 
5504 void
5505 stmf_trace_clear()
5506 {
5507 	if (!stmf_trace_on)
5508 		return;
5509 	mutex_enter(&trace_buf_lock);
5510 	trace_buf_curndx = 0;
5511 	if (trace_buf_size > 0)
5512 		stmf_trace_buf[0] = 0;
5513 	mutex_exit(&trace_buf_lock);
5514 }
5515 
5516 static void
5517 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
5518 {
5519 	stmf_state_change_info_t	 change_info;
5520 	void				*ctl_private;
5521 	uint32_t			 ctl_cmd;
5522 	int				msg = 0;
5523 
5524 	stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
5525 	    offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
5526 	change_info.st_additional_info = info;
5527 	if (offline_lu) {
5528 		change_info.st_rflags = STMF_RFLAG_RESET |
5529 		    STMF_RFLAG_LU_ABORT;
5530 		ctl_private = task->task_lu;
5531 		if (((stmf_i_lu_t *)
5532 		    task->task_lu->lu_stmf_private)->ilu_state ==
5533 		    STMF_STATE_ONLINE) {
5534 			msg = 1;
5535 		}
5536 		ctl_cmd = STMF_CMD_LU_OFFLINE;
5537 	} else {
5538 		change_info.st_rflags = STMF_RFLAG_RESET |
5539 		    STMF_RFLAG_LPORT_ABORT;
5540 		ctl_private = task->task_lport;
5541 		if (((stmf_i_local_port_t *)
5542 		    task->task_lport->lport_stmf_private)->ilport_state ==
5543 		    STMF_STATE_ONLINE) {
5544 			msg = 1;
5545 		}
5546 		ctl_cmd = STMF_CMD_LPORT_OFFLINE;
5547 	}
5548 
5549 	if (msg) {
5550 		stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
5551 		    offline_lu ? "LU" : "LPORT", info ? info :
5552 		    "<no additional info>");
5553 	}
5554 	(void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
5555 }
5556