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