xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 061b8865d125bb564cfc807b8348b684d2485ebc)
1 /*-
2  * Copyright (c) 1997-2009 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <dev/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
35 #include <sys/conf.h>
36 #include <sys/module.h>
37 #include <sys/ioccom.h>
38 #include <dev/isp/isp_ioctl.h>
39 #include <sys/devicestat.h>
40 #include <cam/cam_periph.h>
41 #include <cam/cam_xpt_periph.h>
42 
43 #if	__FreeBSD_version < 800002
44 #define	THREAD_CREATE	kthread_create
45 #else
46 #define	THREAD_CREATE	kproc_create
47 #endif
48 
49 MODULE_VERSION(isp, 1);
50 MODULE_DEPEND(isp, cam, 1, 1, 1);
51 int isp_announced = 0;
52 int isp_fabric_hysteresis = 5;
53 int isp_loop_down_limit = 60;	/* default loop down limit */
54 int isp_change_is_bad = 0;	/* "changed" devices are bad */
55 int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
56 int isp_gone_device_time = 30;	/* grace time before reporting device lost */
57 int isp_autoconfig = 1;		/* automatically attach/detach devices */
58 static const char *roles[4] = {
59     "(none)", "Target", "Initiator", "Target/Initiator"
60 };
61 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
62 static const char rqo[] = "%s: Request Queue Overflow\n";
63 
64 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
65 static d_ioctl_t ispioctl;
66 static void isp_intr_enable(void *);
67 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
68 static void isp_poll(struct cam_sim *);
69 static timeout_t isp_watchdog;
70 static timeout_t isp_gdt;
71 static task_fn_t isp_gdt_task;
72 static timeout_t isp_ldt;
73 static task_fn_t isp_ldt_task;
74 static void isp_kthread(void *);
75 static void isp_action(struct cam_sim *, union ccb *);
76 #ifdef	ISP_INTERNAL_TARGET
77 static void isp_target_thread_pi(void *);
78 static void isp_target_thread_fc(void *);
79 #endif
80 static void isp_timer(void *);
81 
82 static struct cdevsw isp_cdevsw = {
83 	.d_version =	D_VERSION,
84 	.d_ioctl =	ispioctl,
85 	.d_name =	"isp",
86 };
87 
88 static int
89 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
90 {
91 	struct ccb_setasync csa;
92 	struct cam_sim *sim;
93 	struct cam_path *path;
94 
95 	/*
96 	 * Construct our SIM entry.
97 	 */
98 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
99 
100 	if (sim == NULL) {
101 		return (ENOMEM);
102 	}
103 
104 	ISP_LOCK(isp);
105 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
106 		ISP_UNLOCK(isp);
107 		cam_sim_free(sim, FALSE);
108 		return (EIO);
109 	}
110 	ISP_UNLOCK(isp);
111 	if (xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
112 		ISP_LOCK(isp);
113 		xpt_bus_deregister(cam_sim_path(sim));
114 		ISP_UNLOCK(isp);
115 		cam_sim_free(sim, FALSE);
116 		return (ENXIO);
117 	}
118 	xpt_setup_ccb(&csa.ccb_h, path, 5);
119 	csa.ccb_h.func_code = XPT_SASYNC_CB;
120 	csa.event_enable = AC_LOST_DEVICE;
121 	csa.callback = isp_cam_async;
122 	csa.callback_arg = sim;
123 	xpt_action((union ccb *)&csa);
124 
125 	if (IS_SCSI(isp)) {
126 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
127 		spi->sim = sim;
128 		spi->path = path;
129 #ifdef	ISP_INTERNAL_TARGET
130 		ISP_SET_PC(isp, chan, proc_active, 1);
131 		if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
132 			ISP_SET_PC(isp, chan, proc_active, 0);
133 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
134 		}
135 #endif
136 	} else {
137 		fcparam *fcp = FCPARAM(isp, chan);
138 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
139 
140 		ISP_LOCK(isp);
141 		fc->sim = sim;
142 		fc->path = path;
143 		fc->isp = isp;
144 		fc->ready = 1;
145 
146 		callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
147 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
148 		TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
149 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
150 
151 		/*
152 		 * We start by being "loop down" if we have an initiator role
153 		 */
154 		if (fcp->role & ISP_ROLE_INITIATOR) {
155 			isp_freeze_loopdown(isp, chan, "isp_attach");
156 			callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
157 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
158 		}
159 		ISP_UNLOCK(isp);
160 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
161 			xpt_free_path(fc->path);
162 			ISP_LOCK(isp);
163 			if (callout_active(&fc->ldt)) {
164 				callout_stop(&fc->ldt);
165 			}
166 			xpt_bus_deregister(cam_sim_path(fc->sim));
167 			ISP_UNLOCK(isp);
168 			cam_sim_free(fc->sim, FALSE);
169 			return (ENOMEM);
170 		}
171 #ifdef	ISP_INTERNAL_TARGET
172 		ISP_SET_PC(isp, chan, proc_active, 1);
173 		if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
174 			ISP_SET_PC(isp, chan, proc_active, 0);
175 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
176 		}
177 #endif
178 		if (chan == 0) {
179 			struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
180 			struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
181 			SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwnn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwnn, "World Wide Node Name");
182 			SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, "World Wide Port Name");
183 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "loop_down_limit", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->loop_down_limit, 0, "Loop Down Limit");
184 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "gone_device_time", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->gone_device_time, 0, "Gone Device Time");
185 		}
186 	}
187 	return (0);
188 }
189 
190 int
191 isp_attach(ispsoftc_t *isp)
192 {
193 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
194 	int du = device_get_unit(isp->isp_dev);
195 	int chan;
196 
197 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
198 	isp->isp_osinfo.ehook.ich_arg = isp;
199 	/*
200 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
201 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
202 	 * which would be unwise to then set again later.
203 	 */
204 	isp->isp_osinfo.ehook_active = 1;
205 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
206 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
207 		return (-EIO);
208 	}
209 
210 	/*
211 	 * Create the device queue for our SIM(s).
212 	 */
213 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
214 	if (isp->isp_osinfo.devq == NULL) {
215 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
216 		return (EIO);
217 	}
218 
219 	for (chan = 0; chan < isp->isp_nchan; chan++) {
220 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
221 			goto unwind;
222 		}
223 	}
224 
225 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
226 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
227 	isp->isp_osinfo.timer_active = 1;
228 
229 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
230 	if (isp->isp_osinfo.cdev) {
231 		isp->isp_osinfo.cdev->si_drv1 = isp;
232 	}
233 	return (0);
234 
235 unwind:
236 	while (--chan >= 0) {
237 		struct cam_sim *sim;
238 		struct cam_path *path;
239 		if (IS_FC(isp)) {
240 			sim = ISP_FC_PC(isp, chan)->sim;
241 			path = ISP_FC_PC(isp, chan)->path;
242 		} else {
243 			sim = ISP_SPI_PC(isp, chan)->sim;
244 			path = ISP_SPI_PC(isp, chan)->path;
245 		}
246 		xpt_free_path(path);
247 		ISP_LOCK(isp);
248 		xpt_bus_deregister(cam_sim_path(sim));
249 		ISP_UNLOCK(isp);
250 		cam_sim_free(sim, FALSE);
251 	}
252 	if (isp->isp_osinfo.ehook_active) {
253 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
254 		isp->isp_osinfo.ehook_active = 0;
255 	}
256 	if (isp->isp_osinfo.cdev) {
257 		destroy_dev(isp->isp_osinfo.cdev);
258 		isp->isp_osinfo.cdev = NULL;
259 	}
260 	cam_simq_free(isp->isp_osinfo.devq);
261 	isp->isp_osinfo.devq = NULL;
262 	return (-1);
263 }
264 
265 int
266 isp_detach(ispsoftc_t *isp)
267 {
268 	struct cam_sim *sim;
269 	struct cam_path *path;
270 	struct ccb_setasync csa;
271 	int chan;
272 
273 	ISP_LOCK(isp);
274 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
275 		if (IS_FC(isp)) {
276 			sim = ISP_FC_PC(isp, chan)->sim;
277 			path = ISP_FC_PC(isp, chan)->path;
278 		} else {
279 			sim = ISP_SPI_PC(isp, chan)->sim;
280 			path = ISP_SPI_PC(isp, chan)->path;
281 		}
282 		if (sim->refcount > 2) {
283 			ISP_UNLOCK(isp);
284 			return (EBUSY);
285 		}
286 	}
287 	if (isp->isp_osinfo.timer_active) {
288 		callout_stop(&isp->isp_osinfo.tmo);
289 		isp->isp_osinfo.timer_active = 0;
290 	}
291 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
292 		if (IS_FC(isp)) {
293 			sim = ISP_FC_PC(isp, chan)->sim;
294 			path = ISP_FC_PC(isp, chan)->path;
295 		} else {
296 			sim = ISP_SPI_PC(isp, chan)->sim;
297 			path = ISP_SPI_PC(isp, chan)->path;
298 		}
299 		xpt_setup_ccb(&csa.ccb_h, path, 5);
300 		csa.ccb_h.func_code = XPT_SASYNC_CB;
301 		csa.event_enable = 0;
302 		csa.callback = isp_cam_async;
303 		csa.callback_arg = sim;
304 		xpt_action((union ccb *)&csa);
305 		xpt_free_path(path);
306 		xpt_bus_deregister(cam_sim_path(sim));
307 		cam_sim_free(sim, FALSE);
308 	}
309 	ISP_UNLOCK(isp);
310 	if (isp->isp_osinfo.cdev) {
311 		destroy_dev(isp->isp_osinfo.cdev);
312 		isp->isp_osinfo.cdev = NULL;
313 	}
314 	if (isp->isp_osinfo.ehook_active) {
315 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
316 		isp->isp_osinfo.ehook_active = 0;
317 	}
318 	if (isp->isp_osinfo.devq != NULL) {
319 		cam_simq_free(isp->isp_osinfo.devq);
320 		isp->isp_osinfo.devq = NULL;
321 	}
322 	return (0);
323 }
324 
325 static void
326 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
327 {
328 	if (IS_FC(isp)) {
329 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
330 		if (fc->simqfrozen == 0) {
331 			isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
332 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
333 			xpt_freeze_simq(fc->sim, 1);
334 		} else {
335 			isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
336 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
337 		}
338 	}
339 }
340 
341 static void
342 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
343 {
344 	if (IS_FC(isp)) {
345 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
346 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
347 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
348 		if (wasfrozen && fc->simqfrozen == 0) {
349 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
350 			xpt_release_simq(fc->sim, 1);
351 		}
352 	}
353 }
354 
355 
356 static int
357 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
358 {
359 	ispsoftc_t *isp;
360 	int nr, chan, retval = ENOTTY;
361 
362 	isp = dev->si_drv1;
363 
364 	switch (c) {
365 	case ISP_SDBLEV:
366 	{
367 		int olddblev = isp->isp_dblev;
368 		isp->isp_dblev = *(int *)addr;
369 		*(int *)addr = olddblev;
370 		retval = 0;
371 		break;
372 	}
373 	case ISP_GETROLE:
374 		chan = *(int *)addr;
375 		if (chan < 0 || chan >= isp->isp_nchan) {
376 			retval = -ENXIO;
377 			break;
378 		}
379 		if (IS_FC(isp)) {
380 			*(int *)addr = FCPARAM(isp, chan)->role;
381 		} else {
382 			*(int *)addr = SDPARAM(isp, chan)->role;
383 		}
384 		retval = 0;
385 		break;
386 	case ISP_SETROLE:
387 		nr = *(int *)addr;
388 		chan = nr >> 8;
389 		if (chan < 0 || chan >= isp->isp_nchan) {
390 			retval = -ENXIO;
391 			break;
392 		}
393 		nr &= 0xff;
394 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
395 			retval = EINVAL;
396 			break;
397 		}
398 		if (IS_FC(isp)) {
399 			/*
400 			 * We don't really support dual role at present on FC cards.
401 			 *
402 			 * We should, but a bunch of things are currently broken,
403 			 * so don't allow it.
404 			 */
405 			if (nr == ISP_ROLE_BOTH) {
406 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
407 				retval = EINVAL;
408 				break;
409 			}
410 			*(int *)addr = FCPARAM(isp, chan)->role;
411 #ifdef	ISP_INTERNAL_TARGET
412 			ISP_LOCK(isp);
413 			retval = isp_fc_change_role(isp, chan, nr);
414 			ISP_UNLOCK(isp);
415 #else
416 			FCPARAM(isp, chan)->role = nr;
417 #endif
418 		} else {
419 			*(int *)addr = SDPARAM(isp, chan)->role;
420 			SDPARAM(isp, chan)->role = nr;
421 		}
422 		retval = 0;
423 		break;
424 
425 	case ISP_RESETHBA:
426 		ISP_LOCK(isp);
427 #ifdef	ISP_TARGET_MODE
428 		isp_del_all_wwn_entries(isp, ISP_NOCHAN);
429 #endif
430 		isp_reinit(isp, 0);
431 		ISP_UNLOCK(isp);
432 		retval = 0;
433 		break;
434 
435 	case ISP_RESCAN:
436 		if (IS_FC(isp)) {
437 			chan = *(int *)addr;
438 			if (chan < 0 || chan >= isp->isp_nchan) {
439 				retval = -ENXIO;
440 				break;
441 			}
442 			ISP_LOCK(isp);
443 			if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
444 				retval = EIO;
445 			} else {
446 				retval = 0;
447 			}
448 			ISP_UNLOCK(isp);
449 		}
450 		break;
451 
452 	case ISP_FC_LIP:
453 		if (IS_FC(isp)) {
454 			chan = *(int *)addr;
455 			if (chan < 0 || chan >= isp->isp_nchan) {
456 				retval = -ENXIO;
457 				break;
458 			}
459 			ISP_LOCK(isp);
460 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
461 				retval = EIO;
462 			} else {
463 				retval = 0;
464 			}
465 			ISP_UNLOCK(isp);
466 		}
467 		break;
468 	case ISP_FC_GETDINFO:
469 	{
470 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
471 		fcportdb_t *lp;
472 
473 		if (IS_SCSI(isp)) {
474 			break;
475 		}
476 		if (ifc->loopid >= MAX_FC_TARG) {
477 			retval = EINVAL;
478 			break;
479 		}
480 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
481 		if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
482 			ifc->role = lp->roles;
483 			ifc->loopid = lp->handle;
484 			ifc->portid = lp->portid;
485 			ifc->node_wwn = lp->node_wwn;
486 			ifc->port_wwn = lp->port_wwn;
487 			retval = 0;
488 		} else {
489 			retval = ENODEV;
490 		}
491 		break;
492 	}
493 	case ISP_GET_STATS:
494 	{
495 		isp_stats_t *sp = (isp_stats_t *) addr;
496 
497 		ISP_MEMZERO(sp, sizeof (*sp));
498 		sp->isp_stat_version = ISP_STATS_VERSION;
499 		sp->isp_type = isp->isp_type;
500 		sp->isp_revision = isp->isp_revision;
501 		ISP_LOCK(isp);
502 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
503 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
504 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
505 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
506 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
507 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
508 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
509 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
510 		ISP_UNLOCK(isp);
511 		retval = 0;
512 		break;
513 	}
514 	case ISP_CLR_STATS:
515 		ISP_LOCK(isp);
516 		isp->isp_intcnt = 0;
517 		isp->isp_intbogus = 0;
518 		isp->isp_intmboxc = 0;
519 		isp->isp_intoasync = 0;
520 		isp->isp_rsltccmplt = 0;
521 		isp->isp_fphccmplt = 0;
522 		isp->isp_rscchiwater = 0;
523 		isp->isp_fpcchiwater = 0;
524 		ISP_UNLOCK(isp);
525 		retval = 0;
526 		break;
527 	case ISP_FC_GETHINFO:
528 	{
529 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
530 		int chan = hba->fc_channel;
531 
532 		if (chan < 0 || chan >= isp->isp_nchan) {
533 			retval = ENXIO;
534 			break;
535 		}
536 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
537 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
538 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
539 		hba->fc_nchannels = isp->isp_nchan;
540 		if (IS_FC(isp)) {
541 			hba->fc_nports = MAX_FC_TARG;
542 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
543 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
544 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
545 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
546 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
547 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
548 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
549 		} else {
550 			hba->fc_nports = MAX_TARGETS;
551 			hba->fc_speed = 0;
552 			hba->fc_topology = 0;
553 			hba->nvram_node_wwn = 0ull;
554 			hba->nvram_port_wwn = 0ull;
555 			hba->active_node_wwn = 0ull;
556 			hba->active_port_wwn = 0ull;
557 		}
558 		retval = 0;
559 		break;
560 	}
561 	case ISP_TSK_MGMT:
562 	{
563 		int needmarker;
564 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
565 		uint16_t loopid;
566 		mbreg_t mbs;
567 
568 		if (IS_SCSI(isp)) {
569 			break;
570 		}
571 
572 		chan = fct->chan;
573 		if (chan < 0 || chan >= isp->isp_nchan) {
574 			retval = -ENXIO;
575 			break;
576 		}
577 
578 		needmarker = retval = 0;
579 		loopid = fct->loopid;
580 		ISP_LOCK(isp);
581 		if (IS_24XX(isp)) {
582 			uint8_t local[QENTRY_LEN];
583 			isp24xx_tmf_t *tmf;
584 			isp24xx_statusreq_t *sp;
585 			fcparam *fcp = FCPARAM(isp, chan);
586 			fcportdb_t *lp;
587 			int i;
588 
589 			for (i = 0; i < MAX_FC_TARG; i++) {
590 				lp = &fcp->portdb[i];
591 				if (lp->handle == loopid) {
592 					break;
593 				}
594 			}
595 			if (i == MAX_FC_TARG) {
596 				retval = ENXIO;
597 				ISP_UNLOCK(isp);
598 				break;
599 			}
600 			/* XXX VALIDATE LP XXX */
601 			tmf = (isp24xx_tmf_t *) local;
602 			ISP_MEMZERO(tmf, QENTRY_LEN);
603 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
604 			tmf->tmf_header.rqs_entry_count = 1;
605 			tmf->tmf_nphdl = lp->handle;
606 			tmf->tmf_delay = 2;
607 			tmf->tmf_timeout = 2;
608 			tmf->tmf_tidlo = lp->portid;
609 			tmf->tmf_tidhi = lp->portid >> 16;
610 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
611 			tmf->tmf_lun[1] = fct->lun & 0xff;
612 			if (fct->lun >= 256) {
613 				tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
614 			}
615 			switch (fct->action) {
616 			case IPT_CLEAR_ACA:
617 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
618 				break;
619 			case IPT_TARGET_RESET:
620 				tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
621 				needmarker = 1;
622 				break;
623 			case IPT_LUN_RESET:
624 				tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
625 				needmarker = 1;
626 				break;
627 			case IPT_CLEAR_TASK_SET:
628 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
629 				needmarker = 1;
630 				break;
631 			case IPT_ABORT_TASK_SET:
632 				tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
633 				needmarker = 1;
634 				break;
635 			default:
636 				retval = EINVAL;
637 				break;
638 			}
639 			if (retval) {
640 				ISP_UNLOCK(isp);
641 				break;
642 			}
643 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
644 			mbs.param[1] = QENTRY_LEN;
645 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
646 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
647 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
648 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
649 
650 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
651 				ISP_UNLOCK(isp);
652 				retval = ENOMEM;
653 				break;
654 			}
655 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
656 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
657 			sp = (isp24xx_statusreq_t *) local;
658 			sp->req_completion_status = 1;
659 			retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
660 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
661 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
662 			FC_SCRATCH_RELEASE(isp, chan);
663 			if (retval || sp->req_completion_status != 0) {
664 				FC_SCRATCH_RELEASE(isp, chan);
665 				retval = EIO;
666 			}
667 			if (retval == 0) {
668 				if (needmarker) {
669 					fcp->sendmarker = 1;
670 				}
671 			}
672 		} else {
673 			MBSINIT(&mbs, 0, MBLOGALL, 0);
674 			if (ISP_CAP_2KLOGIN(isp) == 0) {
675 				loopid <<= 8;
676 			}
677 			switch (fct->action) {
678 			case IPT_CLEAR_ACA:
679 				mbs.param[0] = MBOX_CLEAR_ACA;
680 				mbs.param[1] = loopid;
681 				mbs.param[2] = fct->lun;
682 				break;
683 			case IPT_TARGET_RESET:
684 				mbs.param[0] = MBOX_TARGET_RESET;
685 				mbs.param[1] = loopid;
686 				needmarker = 1;
687 				break;
688 			case IPT_LUN_RESET:
689 				mbs.param[0] = MBOX_LUN_RESET;
690 				mbs.param[1] = loopid;
691 				mbs.param[2] = fct->lun;
692 				needmarker = 1;
693 				break;
694 			case IPT_CLEAR_TASK_SET:
695 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
696 				mbs.param[1] = loopid;
697 				mbs.param[2] = fct->lun;
698 				needmarker = 1;
699 				break;
700 			case IPT_ABORT_TASK_SET:
701 				mbs.param[0] = MBOX_ABORT_TASK_SET;
702 				mbs.param[1] = loopid;
703 				mbs.param[2] = fct->lun;
704 				needmarker = 1;
705 				break;
706 			default:
707 				retval = EINVAL;
708 				break;
709 			}
710 			if (retval == 0) {
711 				if (needmarker) {
712 					FCPARAM(isp, chan)->sendmarker = 1;
713 				}
714 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
715 				if (retval) {
716 					retval = EIO;
717 				}
718 			}
719 		}
720 		ISP_UNLOCK(isp);
721 		break;
722 	}
723 	default:
724 		break;
725 	}
726 	return (retval);
727 }
728 
729 static void
730 isp_intr_enable(void *arg)
731 {
732 	int chan;
733 	ispsoftc_t *isp = arg;
734 	ISP_LOCK(isp);
735 	for (chan = 0; chan < isp->isp_nchan; chan++) {
736 		if (IS_FC(isp)) {
737 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
738 				ISP_ENABLE_INTS(isp);
739 				break;
740 			}
741 		} else {
742 			if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
743 				ISP_ENABLE_INTS(isp);
744 				break;
745 			}
746 		}
747 	}
748 	isp->isp_osinfo.ehook_active = 0;
749 	ISP_UNLOCK(isp);
750 	/* Release our hook so that the boot can continue. */
751 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
752 }
753 
754 /*
755  * Local Inlines
756  */
757 
758 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
759 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
760 
761 static ISP_INLINE int
762 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
763 {
764 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
765 	if (ISP_PCMD(ccb) == NULL) {
766 		return (-1);
767 	}
768 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
769 	return (0);
770 }
771 
772 static ISP_INLINE void
773 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
774 {
775 	((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
776 	isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
777 	ISP_PCMD(ccb) = NULL;
778 }
779 /*
780  * Put the target mode functions here, because some are inlines
781  */
782 
783 #ifdef	ISP_TARGET_MODE
784 static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
785 static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
786 static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
787 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
788 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
789 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
790 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
791 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
792 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
793 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
794 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
795 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
796 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
797 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
798 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
799 static void isp_enable_lun(ispsoftc_t *, union ccb *);
800 static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
801 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
802 static void isp_disable_lun(ispsoftc_t *, union ccb *);
803 static int isp_enable_target_mode(ispsoftc_t *, int);
804 static int isp_disable_target_mode(ispsoftc_t *, int);
805 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
806 static timeout_t isp_refire_putback_atio;
807 static void isp_complete_ctio(union ccb *);
808 static void isp_target_putback_atio(union ccb *);
809 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
810 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
811 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
812 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
813 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
814 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
815 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
816 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
817 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
818 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
819 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
820 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
821 
822 static ISP_INLINE void
823 isp_tmlock(ispsoftc_t *isp, const char *msg)
824 {
825 	while (isp->isp_osinfo.tmbusy) {
826 		isp->isp_osinfo.tmwanted = 1;
827 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
828 	}
829 	isp->isp_osinfo.tmbusy = 1;
830 }
831 
832 static ISP_INLINE void
833 isp_tmunlk(ispsoftc_t *isp)
834 {
835 	isp->isp_osinfo.tmbusy = 0;
836 	if (isp->isp_osinfo.tmwanted) {
837 		isp->isp_osinfo.tmwanted = 0;
838 		wakeup(isp);
839 	}
840 }
841 
842 static ISP_INLINE int
843 is_any_lun_enabled(ispsoftc_t *isp, int bus)
844 {
845 	struct tslist *lhp;
846 	int i;
847 
848 	for (i = 0; i < LUN_HASH_SIZE; i++) {
849 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
850 		if (SLIST_FIRST(lhp))
851 			return (1);
852 	}
853 	return (0);
854 }
855 
856 static ISP_INLINE int
857 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
858 {
859 	tstate_t *tptr;
860 	struct tslist *lhp;
861 
862 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
863 	SLIST_FOREACH(tptr, lhp, next) {
864 		if (xpt_path_lun_id(tptr->owner) == lun) {
865 			return (1);
866 		}
867 	}
868 	return (0);
869 }
870 
871 static void
872 dump_tstates(ispsoftc_t *isp, int bus)
873 {
874 	int i, j;
875 	struct tslist *lhp;
876 	tstate_t *tptr = NULL;
877 
878 	if (bus >= isp->isp_nchan) {
879 		return;
880 	}
881 	for (i = 0; i < LUN_HASH_SIZE; i++) {
882 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
883 		j = 0;
884 		SLIST_FOREACH(tptr, lhp, next) {
885 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
886 			j++;
887 		}
888 	}
889 }
890 
891 static ISP_INLINE tstate_t *
892 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
893 {
894 	tstate_t *tptr = NULL;
895 	struct tslist *lhp;
896 	int i;
897 
898 	if (bus < isp->isp_nchan) {
899 		for (i = 0; i < LUN_HASH_SIZE; i++) {
900 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
901 			SLIST_FOREACH(tptr, lhp, next) {
902 				if (xpt_path_lun_id(tptr->owner) == lun) {
903 					tptr->hold++;
904 					return (tptr);
905 				}
906 			}
907 		}
908 	}
909 	return (NULL);
910 }
911 
912 static ISP_INLINE tstate_t *
913 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
914 {
915 	tstate_t *tptr = NULL;
916 	atio_private_data_t *atp;
917 	struct tslist *lhp;
918 	int i;
919 
920 	if (bus < isp->isp_nchan && tagval != 0) {
921 		for (i = 0; i < LUN_HASH_SIZE; i++) {
922 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
923 			SLIST_FOREACH(tptr, lhp, next) {
924 				atp = isp_get_atpd(isp, tptr, tagval);
925 				if (atp && atp->tag == tagval) {
926 					tptr->hold++;
927 					return (tptr);
928 				}
929 			}
930 		}
931 	}
932 	return (NULL);
933 }
934 
935 static ISP_INLINE inot_private_data_t *
936 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
937 {
938 	inot_private_data_t *ntp;
939 	tstate_t *tptr;
940 	struct tslist *lhp;
941 	int bus, i;
942 
943 	for (bus = 0; bus < isp->isp_nchan; bus++) {
944 		for (i = 0; i < LUN_HASH_SIZE; i++) {
945 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
946 			SLIST_FOREACH(tptr, lhp, next) {
947 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
948 				if (ntp) {
949 					*rslt = tptr;
950 					tptr->hold++;
951 					return (ntp);
952 				}
953 			}
954 		}
955 	}
956 	return (NULL);
957 }
958 
959 static ISP_INLINE void
960 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
961 {
962 	KASSERT((tptr->hold), ("tptr not held"));
963 	tptr->hold--;
964 }
965 
966 static void
967 isp_tmcmd_restart(ispsoftc_t *isp)
968 {
969 	inot_private_data_t *ntp;
970 	tstate_t *tptr;
971 	struct tslist *lhp;
972 	int bus, i;
973 
974 	for (bus = 0; bus < isp->isp_nchan; bus++) {
975 		for (i = 0; i < LUN_HASH_SIZE; i++) {
976 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
977 			SLIST_FOREACH(tptr, lhp, next) {
978 				inot_private_data_t *restart_queue = tptr->restart_queue;
979 				tptr->restart_queue = NULL;
980 				while (restart_queue) {
981 					ntp = restart_queue;
982 					restart_queue = ntp->rd.nt.nt_hba;
983 					if (IS_24XX(isp)) {
984 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
985 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
986 					} else {
987 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
988 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
989 					}
990 					isp_put_ntpd(isp, tptr, ntp);
991 					if (tptr->restart_queue && restart_queue != NULL) {
992 						ntp = tptr->restart_queue;
993 						tptr->restart_queue = restart_queue;
994 						while (restart_queue->rd.nt.nt_hba) {
995 							restart_queue = restart_queue->rd.nt.nt_hba;
996 						}
997 						restart_queue->rd.nt.nt_hba = ntp;
998 						break;
999 					}
1000 				}
1001 			}
1002 		}
1003 	}
1004 }
1005 
1006 static ISP_INLINE atio_private_data_t *
1007 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1008 {
1009 	atio_private_data_t *atp;
1010 
1011 	if (tag == 0) {
1012 		atp = tptr->atfree;
1013 		if (atp) {
1014 			tptr->atfree = atp->next;
1015 		}
1016 		return (atp);
1017 	}
1018 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1019 		if (atp->tag == tag) {
1020 			return (atp);
1021 		}
1022 	}
1023 	return (NULL);
1024 }
1025 
1026 static ISP_INLINE void
1027 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1028 {
1029 	atp->tag = 0;
1030 	atp->dead = 0;
1031 	atp->next = tptr->atfree;
1032 	tptr->atfree = atp;
1033 }
1034 
1035 static void
1036 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1037 {
1038 	atio_private_data_t *atp;
1039 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1040 
1041 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1042 		if (atp->tag == 0) {
1043 			continue;
1044 		}
1045 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u last_xfr %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1046                     atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->last_xframt, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1047 	}
1048 }
1049 
1050 
1051 static ISP_INLINE inot_private_data_t *
1052 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1053 {
1054 	inot_private_data_t *ntp;
1055 	ntp = tptr->ntfree;
1056 	if (ntp) {
1057 		tptr->ntfree = ntp->next;
1058 	}
1059 	return (ntp);
1060 }
1061 
1062 static ISP_INLINE inot_private_data_t *
1063 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1064 {
1065 	inot_private_data_t *ntp;
1066 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1067 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1068 			return (ntp);
1069 		}
1070 	}
1071 	return (NULL);
1072 }
1073 
1074 static ISP_INLINE void
1075 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1076 {
1077 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
1078 	ntp->next = tptr->ntfree;
1079 	tptr->ntfree = ntp;
1080 }
1081 
1082 static cam_status
1083 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1084 {
1085 	cam_status status;
1086 	lun_id_t lun;
1087 	struct tslist *lhp;
1088 	tstate_t *tptr;
1089 	int i;
1090 
1091 	lun = xpt_path_lun_id(path);
1092 	if (lun != CAM_LUN_WILDCARD) {
1093 		if (lun >= ISP_MAX_LUNS(isp)) {
1094 			return (CAM_LUN_INVALID);
1095 		}
1096 	}
1097 	if (is_lun_enabled(isp, bus, lun)) {
1098 		return (CAM_LUN_ALRDY_ENA);
1099 	}
1100 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1101 	if (tptr == NULL) {
1102 		return (CAM_RESRC_UNAVAIL);
1103 	}
1104 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1105 	if (status != CAM_REQ_CMP) {
1106 		free(tptr, M_DEVBUF);
1107 		return (status);
1108 	}
1109 	SLIST_INIT(&tptr->atios);
1110 	SLIST_INIT(&tptr->inots);
1111 	for (i = 0; i < ATPDPSIZE-1; i++) {
1112 		tptr->atpool[i].next = &tptr->atpool[i+1];
1113 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
1114 	}
1115 	tptr->atfree = tptr->atpool;
1116 	tptr->ntfree = tptr->ntpool;
1117 	tptr->hold = 1;
1118 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1119 	SLIST_INSERT_HEAD(lhp, tptr, next);
1120 	*rslt = tptr;
1121 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1122 	return (CAM_REQ_CMP);
1123 }
1124 
1125 static ISP_INLINE void
1126 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1127 {
1128 	struct tslist *lhp;
1129 
1130 	KASSERT((tptr->hold != 0), ("tptr is not held"));
1131 	KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1132 	ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1133 	SLIST_REMOVE(lhp, tptr, tstate, next);
1134 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1135 	xpt_free_path(tptr->owner);
1136 	free(tptr, M_DEVBUF);
1137 }
1138 
1139 /*
1140  * Enable a lun.
1141  */
1142 static void
1143 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1144 {
1145 	tstate_t *tptr = NULL;
1146 	int bus, tm_enabled, target_role;
1147 	target_id_t target;
1148 	lun_id_t lun;
1149 
1150 
1151 	/*
1152 	 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1153 	 */
1154 	bus = XS_CHANNEL(ccb);
1155 	target = ccb->ccb_h.target_id;
1156 	lun = ccb->ccb_h.target_lun;
1157 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "enabling lun %u\n", lun);
1158 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1159 		ccb->ccb_h.status = CAM_TID_INVALID;
1160 		xpt_done(ccb);
1161 		return;
1162 	}
1163 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1164 		ccb->ccb_h.status = CAM_LUN_INVALID;
1165 		xpt_done(ccb);
1166 		return;
1167 	}
1168 
1169 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1170 		ccb->ccb_h.status = CAM_LUN_INVALID;
1171 		xpt_done(ccb);
1172 		return;
1173 	}
1174 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1175 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1176 	}
1177 
1178 	/*
1179 	 * Wait until we're not busy with the lun enables subsystem
1180 	 */
1181 	isp_tmlock(isp, "isp_enable_lun");
1182 
1183 	/*
1184 	 * This is as a good a place as any to check f/w capabilities.
1185 	 */
1186 
1187 	if (IS_FC(isp)) {
1188 		if (ISP_CAP_TMODE(isp) == 0) {
1189 			xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1190 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1191 			goto done;
1192 		}
1193 		/*
1194 		 * We *could* handle non-SCCLUN f/w, but we'd have to
1195 		 * dork with our already fragile enable/disable code.
1196 		 */
1197 		if (ISP_CAP_SCCFW(isp) == 0) {
1198 			xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1199 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1200 			goto done;
1201 		}
1202 
1203 		target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1204 
1205 	} else {
1206 		target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1207 	}
1208 
1209 	/*
1210 	 * Create the state pointer.
1211 	 * It should not already exist.
1212 	 */
1213 	tptr = get_lun_statep(isp, bus, lun);
1214 	if (tptr) {
1215 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1216 		goto done;
1217 	}
1218 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1219 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
1220 		goto done;
1221 	}
1222 
1223 	/*
1224 	 * We have a tricky maneuver to perform here.
1225 	 *
1226 	 * If target mode isn't already enabled here,
1227 	 * *and* our current role includes target mode,
1228 	 * we enable target mode here.
1229 	 *
1230 	 */
1231 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1232 	if (tm_enabled == 0 && target_role != 0) {
1233 		if (isp_enable_target_mode(isp, bus)) {
1234 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1235 			destroy_lun_state(isp, tptr);
1236 			tptr = NULL;
1237 			goto done;
1238 		}
1239 		tm_enabled = 1;
1240 	}
1241 
1242 	/*
1243 	 * Now check to see whether this bus is in target mode already.
1244 	 *
1245 	 * If not, a later role change into target mode will finish the job.
1246 	 */
1247 	if (tm_enabled == 0) {
1248 		ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1249 		ccb->ccb_h.status = CAM_REQ_CMP;
1250 		xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1251 		goto done;
1252 	}
1253 
1254 	/*
1255 	 * Enable the lun.
1256 	 */
1257 	ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1258 
1259 done:
1260 	if (ccb->ccb_h.status != CAM_REQ_CMP)  {
1261 		if (tptr) {
1262 			destroy_lun_state(isp, tptr);
1263 			tptr = NULL;
1264 		}
1265 	} else {
1266 		tptr->enabled = 1;
1267 	}
1268 	if (tptr) {
1269 		rls_lun_statep(isp, tptr);
1270 	}
1271 
1272 	/*
1273 	 * And we're outta here....
1274 	 */
1275 	isp_tmunlk(isp);
1276 	xpt_done(ccb);
1277 }
1278 
1279 static cam_status
1280 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1281 {
1282 	tstate_t *tptr = NULL;
1283 	struct tslist *lhp;
1284 	int i, n;
1285 
1286 	ISP_GET_PC(isp, bus, tm_enabled, i);
1287 	if (i == 1) {
1288 		return (CAM_REQ_CMP);
1289 	}
1290 	ISP_GET_PC(isp, bus, tm_enable_defer, i);
1291 	if (i == 0) {
1292 		return (CAM_REQ_CMP);
1293 	}
1294 	/*
1295 	 * If this succeeds, it will set tm_enable
1296 	 */
1297 	if (isp_enable_target_mode(isp, bus)) {
1298 		return (CAM_REQ_CMP_ERR);
1299 	}
1300 	isp_tmlock(isp, "isp_enable_deferred_luns");
1301 	for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1302 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1303 		SLIST_FOREACH(tptr, lhp, next) {
1304 			tptr->hold++;
1305 			if (tptr->enabled == 0) {
1306 				if (isp_enable_deferred(isp, bus, xpt_path_lun_id(tptr->owner)) == 0) {
1307 					tptr->enabled = 1;
1308 					n++;
1309 				}
1310 			} else {
1311 				n++;
1312 			}
1313 			tptr->hold--;
1314 		}
1315 	}
1316 	isp_tmunlk(isp);
1317 	if (n == 0) {
1318 		return (CAM_REQ_CMP_ERR);
1319 	}
1320 	ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1321 	return (CAM_REQ_CMP);
1322 }
1323 
1324 static cam_status
1325 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1326 {
1327 	cam_status status;
1328 	int luns_already_enabled = ISP_FC_PC(isp, bus)->tm_luns_enabled;
1329 
1330 	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1331 	if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
1332 		status = CAM_REQ_CMP;
1333 	} else {
1334 		int cmd_cnt, not_cnt;
1335 
1336 		if (IS_23XX(isp)) {
1337 			cmd_cnt = DFLT_CMND_CNT;
1338 			not_cnt = DFLT_INOT_CNT;
1339 		} else {
1340 			cmd_cnt = 64;
1341 			not_cnt = 8;
1342 		}
1343 		status = CAM_REQ_INPROG;
1344 		isp->isp_osinfo.rptr = &status;
1345 		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1346 			status = CAM_RESRC_UNAVAIL;
1347 		} else {
1348 			mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1349 		}
1350 		isp->isp_osinfo.rptr = NULL;
1351 	}
1352 	if (status == CAM_REQ_CMP) {
1353 		ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1354 		isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1355 	}
1356 	return (status);
1357 }
1358 
1359 static void
1360 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1361 {
1362 	tstate_t *tptr = NULL;
1363 	int bus;
1364 	cam_status status;
1365 	target_id_t target;
1366 	lun_id_t lun;
1367 
1368 	bus = XS_CHANNEL(ccb);
1369 	target = ccb->ccb_h.target_id;
1370 	lun = ccb->ccb_h.target_lun;
1371 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "disabling lun %u\n", lun);
1372 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1373 		ccb->ccb_h.status = CAM_TID_INVALID;
1374 		xpt_done(ccb);
1375 		return;
1376 	}
1377 
1378 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1379 		ccb->ccb_h.status = CAM_LUN_INVALID;
1380 		xpt_done(ccb);
1381 		return;
1382 	}
1383 
1384 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1385 		ccb->ccb_h.status = CAM_LUN_INVALID;
1386 		xpt_done(ccb);
1387 		return;
1388 	}
1389 
1390 	/*
1391 	 * See if we're busy disabling a lun now.
1392 	 */
1393 	isp_tmlock(isp, "isp_disable_lun");
1394 	status = CAM_REQ_INPROG;
1395 
1396 	/*
1397 	 * Find the state pointer.
1398 	 */
1399 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1400 		status = CAM_PATH_INVALID;
1401 		goto done;
1402 	}
1403 
1404 	/*
1405 	 * If we're a 24XX card, we're done.
1406 	 */
1407 	if (IS_24XX(isp)) {
1408 		status = CAM_REQ_CMP;
1409 		goto done;
1410 	}
1411 
1412 	/*
1413 	 * For SCC FW, we only deal with lun zero.
1414 	 */
1415 	if (IS_FC(isp)) {
1416 		lun = 0;
1417 	}
1418 	isp->isp_osinfo.rptr = &status;
1419 	if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1420 		status = CAM_RESRC_UNAVAIL;
1421 	} else {
1422 		mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1423 	}
1424 	isp->isp_osinfo.rptr = NULL;
1425 done:
1426 	if (status == CAM_REQ_CMP) {
1427 		tptr->enabled = 0;
1428 		/*
1429 		 * If we have no more luns enabled for this bus, delete all tracked wwns for it (if we are FC)
1430 		 * and disable target mode.
1431 		 */
1432 		if (is_any_lun_enabled(isp, bus) == 0) {
1433 			isp_del_all_wwn_entries(isp, bus);
1434 			if (isp_disable_target_mode(isp, bus)) {
1435 				status = CAM_REQ_CMP_ERR;
1436 			}
1437 		}
1438 	}
1439 	ccb->ccb_h.status = status;
1440 	if (status == CAM_REQ_CMP) {
1441 		xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1442 		destroy_lun_state(isp, tptr);
1443 	} else {
1444 		if (tptr)
1445 			rls_lun_statep(isp, tptr);
1446 	}
1447 	isp_tmunlk(isp);
1448 	xpt_done(ccb);
1449 }
1450 
1451 static int
1452 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1453 {
1454 	int tm_enabled;
1455 
1456 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1457 	if (tm_enabled != 0) {
1458 		return (0);
1459 	}
1460 	if (IS_SCSI(isp)) {
1461 		mbreg_t mbs;
1462 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1463 		mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1464 		mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1465 		mbs.param[2] = bus << 7;
1466 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1467 			isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
1468 			return (EIO);
1469 		}
1470 	}
1471 	ISP_SET_PC(isp, bus, tm_enabled, 1);
1472 	isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
1473 	return (0);
1474 }
1475 
1476 static int
1477 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1478 {
1479 	int tm_enabled;
1480 
1481 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1482 	if (tm_enabled == 0) {
1483 		return (0);
1484 	}
1485 	if (IS_SCSI(isp)) {
1486 		mbreg_t mbs;
1487 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1488 		mbs.param[2] = bus << 7;
1489 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1490 			isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
1491 			return (EIO);
1492 		}
1493 	}
1494 	ISP_SET_PC(isp, bus, tm_enabled, 0);
1495 	isp_prt(isp, ISP_LOGINFO, "Target Role disabled onon  Bus %d", bus);
1496 	return (0);
1497 }
1498 
1499 static void
1500 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1501 {
1502 	uint32_t *rptr;
1503 
1504 	rptr = isp->isp_osinfo.rptr;
1505 	if (lep->le_status != LUN_OK) {
1506 		isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1507 		if (rptr) {
1508 			*rptr = CAM_REQ_CMP_ERR;
1509 			wakeup_one(rptr);
1510 		}
1511 	} else {
1512 		if (rptr) {
1513 			*rptr = CAM_REQ_CMP;
1514 			wakeup_one(rptr);
1515 		}
1516 	}
1517 }
1518 
1519 static void
1520 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1521 {
1522 	void *qe;
1523 	tstate_t *tptr;
1524 	atio_private_data_t *atp;
1525 	struct ccb_scsiio *cso = &ccb->csio;
1526 	uint32_t dmaresult, handle;
1527 	uint8_t local[QENTRY_LEN];
1528 
1529 	/*
1530 	 * Do some sanity checks.
1531 	 */
1532 	if (cso->dxfer_len == 0) {
1533 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1534 			xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1535 			ccb->ccb_h.status = CAM_REQ_INVALID;
1536 			xpt_done(ccb);
1537 			return;
1538 		}
1539 	}
1540 
1541 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1542 	if (tptr == NULL) {
1543 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1544 		if (tptr == NULL) {
1545 			xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1546 			dump_tstates(isp, XS_CHANNEL(ccb));
1547 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1548 			xpt_done(ccb);
1549 			return;
1550 		}
1551 	}
1552 
1553 	atp = isp_get_atpd(isp, tptr, cso->tag_id);
1554 	if (atp == NULL) {
1555 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1556 		isp_dump_atpd(isp, tptr);
1557 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1558 		xpt_done(ccb);
1559 		return;
1560 	}
1561 	if (atp->dead) {
1562 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1563 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1564 		xpt_done(ccb);
1565 		return;
1566 	}
1567 
1568 	/*
1569 	 * Check to make sure we're still in target mode.
1570 	 */
1571 	if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1572 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
1573 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1574 		xpt_done(ccb);
1575 		return;
1576 	}
1577 
1578 	/*
1579 	 * Get some resources
1580 	 */
1581 	if (isp_get_pcmd(isp, ccb)) {
1582 		rls_lun_statep(isp, tptr);
1583 		xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1584 		cam_freeze_devq(ccb->ccb_h.path);
1585 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1586 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1587 		xpt_done(ccb);
1588 		return;
1589 	}
1590 	qe = isp_getrqentry(isp);
1591 	if (qe == NULL) {
1592 		xpt_print(ccb->ccb_h.path, rqo, __func__);
1593 		cam_freeze_devq(ccb->ccb_h.path);
1594 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1595 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1596 		goto out;
1597 	}
1598 	memset(local, 0, QENTRY_LEN);
1599 
1600 	/*
1601 	 * We're either moving data or completing a command here.
1602 	 */
1603 	if (IS_24XX(isp)) {
1604 		ct7_entry_t *cto = (ct7_entry_t *) local;
1605 
1606 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1607 		cto->ct_header.rqs_entry_count = 1;
1608 		cto->ct_header.rqs_seqno = 1;
1609 		cto->ct_nphdl = atp->nphdl;
1610 		cto->ct_rxid = atp->tag;
1611 		cto->ct_iid_lo = atp->portid;
1612 		cto->ct_iid_hi = atp->portid >> 16;
1613 		cto->ct_oxid = atp->oxid;
1614 		cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1615 		cto->ct_scsi_status = cso->scsi_status;
1616 		cto->ct_timeout = 120;
1617 		cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1618 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1619 			cto->ct_flags |= CT7_SENDSTATUS;
1620 		}
1621 		if (cso->dxfer_len == 0) {
1622 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1623 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1624 				int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1625 				cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1626 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1627 				cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1628 			}
1629 		} else {
1630 			cto->ct_flags |= CT7_FLAG_MODE0;
1631 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1632 				cto->ct_flags |= CT7_DATA_IN;
1633 			} else {
1634 				cto->ct_flags |= CT7_DATA_OUT;
1635 			}
1636 			cto->rsp.m0.reloff = atp->bytes_xfered;
1637 			/*
1638 			 * Don't overrun the limits placed on us
1639 			 */
1640 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1641 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1642 			}
1643 			atp->last_xframt = cso->dxfer_len;
1644 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1645 		}
1646 		if (cto->ct_flags & CT7_SENDSTATUS) {
1647 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1648 			cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1649 			if (cto->ct_resid < 0) {
1650 				cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1651 			} else if (cto->ct_resid > 0) {
1652 				cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1653 			}
1654 			atp->state = ATPD_STATE_LAST_CTIO;
1655 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1656 			    atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1657 		} else {
1658 			cto->ct_resid = 0;
1659 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1660 			    cso->dxfer_len, atp->bytes_xfered);
1661 			atp->state = ATPD_STATE_CTIO;
1662 		}
1663 	} else if (IS_FC(isp)) {
1664 		ct2_entry_t *cto = (ct2_entry_t *) local;
1665 
1666 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1667 		cto->ct_header.rqs_entry_count = 1;
1668 		cto->ct_header.rqs_seqno = 1;
1669 		if (ISP_CAP_2KLOGIN(isp) == 0) {
1670 			((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1671 		} else {
1672 			cto->ct_iid = cso->init_id;
1673 			if (ISP_CAP_SCCFW(isp) == 0) {
1674 				cto->ct_lun = ccb->ccb_h.target_lun;
1675 			}
1676 		}
1677 
1678 
1679 		cto->ct_rxid = cso->tag_id;
1680 		if (cso->dxfer_len == 0) {
1681 			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1682 			cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1683 			cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1684 			if (cto->ct_resid < 0) {
1685 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1686 			} else if (cto->ct_resid > 0) {
1687 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1688 			}
1689 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1690 				int m = min(cso->sense_len, MAXRESPLEN);
1691 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1692 				cto->rsp.m1.ct_senselen = m;
1693 				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1694 			} else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1695 				/*
1696 				 * XXX: DEBUG
1697 				 */
1698 				xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1699 			}
1700 		} else {
1701 			cto->ct_flags |= CT2_FLAG_MODE0;
1702 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1703 				cto->ct_flags |= CT2_DATA_IN;
1704 			} else {
1705 				cto->ct_flags |= CT2_DATA_OUT;
1706 			}
1707 			cto->ct_reloff = atp->bytes_xfered;
1708 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1709 			/*
1710 			 * Don't overrun the limits placed on us
1711 			 */
1712 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1713 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1714 			}
1715 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1716 				cto->ct_flags |= CT2_SENDSTATUS;
1717 				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1718 				cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1719 				if (cto->ct_resid < 0) {
1720 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1721 				} else if (cto->ct_resid > 0) {
1722 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1723 				}
1724 			} else {
1725 				atp->last_xframt = cso->dxfer_len;
1726 			}
1727 			/*
1728 			 * If we're sending data and status back together,
1729 			 * we can't also send back sense data as well.
1730 			 */
1731 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1732 		}
1733 
1734 		if (cto->ct_flags & CT2_SENDSTATUS) {
1735 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1736 			cto->ct_flags |= CT2_CCINCR;
1737 			atp->state = ATPD_STATE_LAST_CTIO;
1738 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1739 			    atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1740 		} else {
1741 			cto->ct_resid = 0;
1742 			atp->state = ATPD_STATE_CTIO;
1743 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1744 			    cso->dxfer_len, atp->bytes_xfered);
1745 		}
1746 		cto->ct_timeout = 10;
1747 	} else {
1748 		ct_entry_t *cto = (ct_entry_t *) local;
1749 
1750 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1751 		cto->ct_header.rqs_entry_count = 1;
1752 		cto->ct_header.rqs_seqno = 1;
1753 		cto->ct_iid = cso->init_id;
1754 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1755 		cto->ct_tgt = ccb->ccb_h.target_id;
1756 		cto->ct_lun = ccb->ccb_h.target_lun;
1757 		cto->ct_fwhandle = cso->tag_id >> 16;
1758 		if (AT_HAS_TAG(cso->tag_id)) {
1759 			cto->ct_tag_val = cso->tag_id;
1760 			cto->ct_flags |= CT_TQAE;
1761 		}
1762 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1763 			cto->ct_flags |= CT_NODISC;
1764 		}
1765 		if (cso->dxfer_len == 0) {
1766 			cto->ct_flags |= CT_NO_DATA;
1767 		} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1768 			cto->ct_flags |= CT_DATA_IN;
1769 		} else {
1770 			cto->ct_flags |= CT_DATA_OUT;
1771 		}
1772 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1773 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1774 			cto->ct_scsi_status = cso->scsi_status;
1775 			cto->ct_resid = cso->resid;
1776 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1777 			    cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1778 		}
1779 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1780 		cto->ct_timeout = 10;
1781 	}
1782 
1783 	if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1784 		xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1785 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1786 		goto out;
1787 	}
1788 
1789 
1790 	/*
1791 	 * Call the dma setup routines for this entry (and any subsequent
1792 	 * CTIOs) if there's data to move, and then tell the f/w it's got
1793 	 * new things to play with. As with isp_start's usage of DMA setup,
1794 	 * any swizzling is done in the machine dependent layer. Because
1795 	 * of this, we put the request onto the queue area first in native
1796 	 * format.
1797 	 */
1798 
1799 	if (IS_24XX(isp)) {
1800 		ct7_entry_t *cto = (ct7_entry_t *) local;
1801 		cto->ct_syshandle = handle;
1802 	} else if (IS_FC(isp)) {
1803 		ct2_entry_t *cto = (ct2_entry_t *) local;
1804 		cto->ct_syshandle = handle;
1805 	} else {
1806 		ct_entry_t *cto = (ct_entry_t *) local;
1807 		cto->ct_syshandle = handle;
1808 	}
1809 
1810 	dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1811 	if (dmaresult == CMD_QUEUED) {
1812 		isp->isp_nactive++;
1813 		ccb->ccb_h.status |= CAM_SIM_QUEUED;
1814 		rls_lun_statep(isp, tptr);
1815 		return;
1816 	}
1817 	if (dmaresult == CMD_EAGAIN) {
1818 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1819 	} else {
1820 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1821 	}
1822 	isp_destroy_tgt_handle(isp, handle);
1823 out:
1824 	rls_lun_statep(isp, tptr);
1825 	isp_free_pcmd(isp, ccb);
1826 	xpt_done(ccb);
1827 }
1828 
1829 static void
1830 isp_refire_putback_atio(void *arg)
1831 {
1832 	union ccb *ccb = arg;
1833 	ispsoftc_t *isp = XS_ISP(ccb);
1834 	ISP_LOCK(isp);
1835 	isp_target_putback_atio(ccb);
1836 	ISP_UNLOCK(isp);
1837 }
1838 
1839 static void
1840 isp_target_putback_atio(union ccb *ccb)
1841 {
1842 	ispsoftc_t *isp;
1843 	struct ccb_scsiio *cso;
1844 	void *qe;
1845 
1846 	isp = XS_ISP(ccb);
1847 
1848 	qe = isp_getrqentry(isp);
1849 	if (qe == NULL) {
1850 		xpt_print(ccb->ccb_h.path, rqo, __func__);
1851 		(void) timeout(isp_refire_putback_atio, ccb, 10);
1852 		return;
1853 	}
1854 	memset(qe, 0, QENTRY_LEN);
1855 	cso = &ccb->csio;
1856 	if (IS_FC(isp)) {
1857 		at2_entry_t local, *at = &local;
1858 		ISP_MEMZERO(at, sizeof (at2_entry_t));
1859 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1860 		at->at_header.rqs_entry_count = 1;
1861 		if (ISP_CAP_SCCFW(isp)) {
1862 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1863 		} else {
1864 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1865 		}
1866 		at->at_status = CT_OK;
1867 		at->at_rxid = cso->tag_id;
1868 		at->at_iid = cso->ccb_h.target_id;
1869 		isp_put_atio2(isp, at, qe);
1870 	} else {
1871 		at_entry_t local, *at = &local;
1872 		ISP_MEMZERO(at, sizeof (at_entry_t));
1873 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1874 		at->at_header.rqs_entry_count = 1;
1875 		at->at_iid = cso->init_id;
1876 		at->at_iid |= XS_CHANNEL(ccb) << 7;
1877 		at->at_tgt = cso->ccb_h.target_id;
1878 		at->at_lun = cso->ccb_h.target_lun;
1879 		at->at_status = CT_OK;
1880 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
1881 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
1882 		isp_put_atio(isp, at, qe);
1883 	}
1884 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1885 	ISP_SYNC_REQUEST(isp);
1886 	isp_complete_ctio(ccb);
1887 }
1888 
1889 static void
1890 isp_complete_ctio(union ccb *ccb)
1891 {
1892 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1893 		ccb->ccb_h.status |= CAM_REQ_CMP;
1894 	}
1895 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1896 	isp_free_pcmd(XS_ISP(ccb), ccb);
1897 	xpt_done(ccb);
1898 }
1899 
1900 /*
1901  * Handle ATIO stuff that the generic code can't.
1902  * This means handling CDBs.
1903  */
1904 
1905 static void
1906 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1907 {
1908 	tstate_t *tptr;
1909 	int status, bus;
1910 	struct ccb_accept_tio *atiop;
1911 	atio_private_data_t *atp;
1912 
1913 	/*
1914 	 * The firmware status (except for the QLTM_SVALID bit)
1915 	 * indicates why this ATIO was sent to us.
1916 	 *
1917 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1918 	 *
1919 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1920 	 * we're still connected on the SCSI bus.
1921 	 */
1922 	status = aep->at_status;
1923 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1924 		/*
1925 		 * Bus Phase Sequence error. We should have sense data
1926 		 * suggested by the f/w. I'm not sure quite yet what
1927 		 * to do about this for CAM.
1928 		 */
1929 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1930 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1931 		return;
1932 	}
1933 	if ((status & ~QLTM_SVALID) != AT_CDB) {
1934 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1935 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1936 		return;
1937 	}
1938 
1939 	bus = GET_BUS_VAL(aep->at_iid);
1940 	tptr = get_lun_statep(isp, bus, aep->at_lun);
1941 	if (tptr == NULL) {
1942 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1943 		if (tptr == NULL) {
1944 			/*
1945 			 * Because we can't autofeed sense data back with
1946 			 * a command for parallel SCSI, we can't give back
1947 			 * a CHECK CONDITION. We'll give back a BUSY status
1948 			 * instead. This works out okay because the only
1949 			 * time we should, in fact, get this, is in the
1950 			 * case that somebody configured us without the
1951 			 * blackhole driver, so they get what they deserve.
1952 			 */
1953 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1954 			return;
1955 		}
1956 	}
1957 
1958 	atp = isp_get_atpd(isp, tptr, 0);
1959 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1960 	if (atiop == NULL || atp == NULL) {
1961 		/*
1962 		 * Because we can't autofeed sense data back with
1963 		 * a command for parallel SCSI, we can't give back
1964 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1965 		 * instead. This works out okay because the only time we
1966 		 * should, in fact, get this, is in the case that we've
1967 		 * run out of ATIOS.
1968 		 */
1969 		xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1970 		    ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1971 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1972 		if (atp) {
1973 			isp_put_atpd(isp, tptr, atp);
1974 		}
1975 		rls_lun_statep(isp, tptr);
1976 		return;
1977 	}
1978 	atp->tag = aep->at_tag_val;
1979 	if (atp->tag == 0) {
1980 		atp->tag = ~0;
1981 	}
1982 	atp->state = ATPD_STATE_ATIO;
1983 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1984 	tptr->atio_count--;
1985 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1986 	atiop->ccb_h.target_id = aep->at_tgt;
1987 	atiop->ccb_h.target_lun = aep->at_lun;
1988 	if (aep->at_flags & AT_NODISC) {
1989 		atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1990 	} else {
1991 		atiop->ccb_h.flags = 0;
1992 	}
1993 
1994 	if (status & QLTM_SVALID) {
1995 		size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1996 		atiop->sense_len = amt;
1997 		ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1998 	} else {
1999 		atiop->sense_len = 0;
2000 	}
2001 
2002 	atiop->init_id = GET_IID_VAL(aep->at_iid);
2003 	atiop->cdb_len = aep->at_cdblen;
2004 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2005 	atiop->ccb_h.status = CAM_CDB_RECVD;
2006 	/*
2007 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
2008 	 * and the handle (which we have to preserve).
2009 	 */
2010 	atiop->tag_id = atp->tag;
2011 	if (aep->at_flags & AT_TQAE) {
2012 		atiop->tag_action = aep->at_tag_type;
2013 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2014 	}
2015 	atp->orig_datalen = 0;
2016 	atp->bytes_xfered = 0;
2017 	atp->last_xframt = 0;
2018 	atp->lun = aep->at_lun;
2019 	atp->nphdl = aep->at_iid;
2020 	atp->portid = PORT_NONE;
2021 	atp->oxid = 0;
2022 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2023 	atp->tattr = aep->at_tag_type;
2024 	atp->state = ATPD_STATE_CAM;
2025 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
2026 	rls_lun_statep(isp, tptr);
2027 }
2028 
2029 static void
2030 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2031 {
2032 	lun_id_t lun;
2033 	fcportdb_t *lp;
2034 	tstate_t *tptr;
2035 	struct ccb_accept_tio *atiop;
2036 	uint16_t nphdl;
2037 	atio_private_data_t *atp;
2038 	inot_private_data_t *ntp;
2039 
2040 	/*
2041 	 * The firmware status (except for the QLTM_SVALID bit)
2042 	 * indicates why this ATIO was sent to us.
2043 	 *
2044 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2045 	 */
2046 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
2047 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2048 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2049 		return;
2050 	}
2051 
2052 	if (ISP_CAP_SCCFW(isp)) {
2053 		lun = aep->at_scclun;
2054 	} else {
2055 		lun = aep->at_lun;
2056 	}
2057 	if (ISP_CAP_2KLOGIN(isp)) {
2058 		nphdl = ((at2e_entry_t *)aep)->at_iid;
2059 	} else {
2060 		nphdl = aep->at_iid;
2061 	}
2062 	tptr = get_lun_statep(isp, 0, lun);
2063 	if (tptr == NULL) {
2064 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2065 		if (tptr == NULL) {
2066 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
2067 			isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2068 			return;
2069 		}
2070 	}
2071 
2072 	/*
2073 	 * Start any commands pending resources first.
2074 	 */
2075 	if (tptr->restart_queue) {
2076 		inot_private_data_t *restart_queue = tptr->restart_queue;
2077 		tptr->restart_queue = NULL;
2078 		while (restart_queue) {
2079 			ntp = restart_queue;
2080 			restart_queue = ntp->rd.nt.nt_hba;
2081 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2082 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2083 			isp_put_ntpd(isp, tptr, ntp);
2084 			/*
2085 			 * If a recursion caused the restart queue to start to fill again,
2086 			 * stop and splice the new list on top of the old list and restore
2087 			 * it and go to noresrc.
2088 			 */
2089 			if (tptr->restart_queue) {
2090 				ntp = tptr->restart_queue;
2091 				tptr->restart_queue = restart_queue;
2092 				while (restart_queue->rd.nt.nt_hba) {
2093 					restart_queue = restart_queue->rd.nt.nt_hba;
2094 				}
2095 				restart_queue->rd.nt.nt_hba = ntp;
2096 				goto noresrc;
2097 			}
2098 		}
2099 	}
2100 
2101 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2102 	if (atiop == NULL) {
2103 		goto noresrc;
2104 	}
2105 
2106 	atp = isp_get_atpd(isp, tptr, 0);
2107 	if (atp == NULL) {
2108 		goto noresrc;
2109 	}
2110 
2111 	atp->tag = aep->at_rxid;
2112 	atp->state = ATPD_STATE_ATIO;
2113 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2114 	tptr->atio_count--;
2115 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2116 	atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2117 	atiop->ccb_h.target_lun = lun;
2118 
2119 	/*
2120 	 * We don't get 'suggested' sense data as we do with SCSI cards.
2121 	 */
2122 	atiop->sense_len = 0;
2123 	if (ISP_CAP_2KLOGIN(isp)) {
2124 		/*
2125 		 * NB: We could not possibly have 2K logins if we
2126 		 * NB: also did not have SCC FW.
2127 		 */
2128 		atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2129 	} else {
2130 		atiop->init_id = aep->at_iid;
2131 	}
2132 
2133 	/*
2134 	 * If we're not in the port database, add ourselves.
2135 	 */
2136 	if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2137     		uint64_t iid =
2138 			(((uint64_t) aep->at_wwpn[0]) << 48) |
2139 			(((uint64_t) aep->at_wwpn[1]) << 32) |
2140 			(((uint64_t) aep->at_wwpn[2]) << 16) |
2141 			(((uint64_t) aep->at_wwpn[3]) <<  0);
2142 		/*
2143 		 * However, make sure we delete ourselves if otherwise
2144 		 * we were there but at a different loop id.
2145 		 */
2146 		if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2147 			isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2148 		}
2149 		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2150 	}
2151 	atiop->cdb_len = ATIO2_CDBLEN;
2152 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2153 	atiop->ccb_h.status = CAM_CDB_RECVD;
2154 	atiop->tag_id = atp->tag;
2155 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2156 	case ATIO2_TC_ATTR_SIMPLEQ:
2157 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2158 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2159 		break;
2160 	case ATIO2_TC_ATTR_HEADOFQ:
2161 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2162 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2163 		break;
2164 	case ATIO2_TC_ATTR_ORDERED:
2165 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2166 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2167 		break;
2168 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
2169 	case ATIO2_TC_ATTR_UNTAGGED:
2170 	default:
2171 		atiop->tag_action = 0;
2172 		break;
2173 	}
2174 
2175 	atp->orig_datalen = aep->at_datalen;
2176 	atp->bytes_xfered = 0;
2177 	atp->last_xframt = 0;
2178 	atp->lun = lun;
2179 	atp->nphdl = atiop->init_id;
2180 	atp->sid = PORT_ANY;
2181 	atp->oxid = aep->at_oxid;
2182 	atp->cdb0 = aep->at_cdb[0];
2183 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2184 	atp->state = ATPD_STATE_CAM;
2185 	xpt_done((union ccb *)atiop);
2186 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2187 	rls_lun_statep(isp, tptr);
2188 	return;
2189 noresrc:
2190 	ntp = isp_get_ntpd(isp, tptr);
2191 	if (ntp == NULL) {
2192 		rls_lun_statep(isp, tptr);
2193 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2194 		return;
2195 	}
2196 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2197 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2198 	tptr->restart_queue = ntp;
2199 	rls_lun_statep(isp, tptr);
2200 }
2201 
2202 static void
2203 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2204 {
2205 	int cdbxlen;
2206 	uint16_t lun, chan, nphdl = NIL_HANDLE;
2207 	uint32_t did, sid;
2208 	uint64_t wwn = INI_NONE;
2209 	fcportdb_t *lp;
2210 	tstate_t *tptr;
2211 	struct ccb_accept_tio *atiop;
2212 	atio_private_data_t *atp = NULL;
2213 	inot_private_data_t *ntp;
2214 
2215 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2216 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2217 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2218 
2219 	/*
2220 	 * Find the N-port handle, and Virtual Port Index for this command.
2221 	 *
2222 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2223 	 * We also, as a matter of course, need to know the WWN of the initiator too.
2224 	 */
2225 	if (ISP_CAP_MULTI_ID(isp)) {
2226 		/*
2227 		 * Find the right channel based upon D_ID
2228 		 */
2229 		isp_find_chan_by_did(isp, did, &chan);
2230 
2231 		if (chan == ISP_NOCHAN) {
2232 			NANOTIME_T now;
2233 
2234 			/*
2235 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2236 			 * It's a bit tricky here as we need to stash this command *somewhere*.
2237 			 */
2238 			GET_NANOTIME(&now);
2239 			if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2240 				isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2241 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2242 				return;
2243 			}
2244 			tptr = get_lun_statep(isp, 0, 0);
2245 			if (tptr == NULL) {
2246 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2247 				if (tptr == NULL) {
2248 					isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2249 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2250 					return;
2251 				}
2252 			}
2253 			isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2254 			goto noresrc;
2255 		}
2256 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2257 	} else {
2258 		chan = 0;
2259 	}
2260 
2261 	/*
2262 	 * Find the PDB entry for this initiator
2263 	 */
2264 	if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2265 		/*
2266 		 * If we're not in the port database terminate the exchange.
2267 		 */
2268 		isp_prt(isp, ISP_LOGTINFO, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x wasn't in PDB already",
2269 		    __func__, aep->at_rxid, did, chan, sid);
2270 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2271 		return;
2272 	}
2273 	nphdl = lp->handle;
2274 	wwn = lp->port_wwn;
2275 
2276 	/*
2277 	 * Get the tstate pointer
2278 	 */
2279 	tptr = get_lun_statep(isp, chan, lun);
2280 	if (tptr == NULL) {
2281 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2282 		if (tptr == NULL) {
2283 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2284 			isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2285 			return;
2286 		}
2287 	}
2288 
2289 	/*
2290 	 * Start any commands pending resources first.
2291 	 */
2292 	if (tptr->restart_queue) {
2293 		inot_private_data_t *restart_queue = tptr->restart_queue;
2294 		tptr->restart_queue = NULL;
2295 		while (restart_queue) {
2296 			ntp = restart_queue;
2297 			restart_queue = ntp->rd.nt.nt_hba;
2298 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2299 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2300 			isp_put_ntpd(isp, tptr, ntp);
2301 			/*
2302 			 * If a recursion caused the restart queue to start to fill again,
2303 			 * stop and splice the new list on top of the old list and restore
2304 			 * it and go to noresrc.
2305 			 */
2306 			if (tptr->restart_queue) {
2307 				if (restart_queue) {
2308 					ntp = tptr->restart_queue;
2309 					tptr->restart_queue = restart_queue;
2310 					while (restart_queue->rd.nt.nt_hba) {
2311 						restart_queue = restart_queue->rd.nt.nt_hba;
2312 					}
2313 					restart_queue->rd.nt.nt_hba = ntp;
2314 				}
2315 				goto noresrc;
2316 			}
2317 		}
2318 	}
2319 
2320 	/*
2321 	 * If the f/w is out of resources, just send a BUSY status back.
2322 	 */
2323 	if (aep->at_rxid == AT7_NORESRC_RXID) {
2324 		rls_lun_statep(isp, tptr);
2325 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2326 		return;
2327 	}
2328 
2329 	/*
2330 	 * If we're out of resources, just send a BUSY status back.
2331 	 */
2332 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2333 	if (atiop == NULL) {
2334 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2335 		goto noresrc;
2336 	}
2337 
2338 	atp = isp_get_atpd(isp, tptr, 0);
2339 	if (atp == NULL) {
2340 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2341 		goto noresrc;
2342 	}
2343 	if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2344 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x)\n",
2345 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2346 		/*
2347 		 * It's not a "no resource" condition- but we can treat it like one
2348 		 */
2349 		goto noresrc;
2350 	}
2351 
2352 	atp->tag = aep->at_rxid;
2353 	atp->state = ATPD_STATE_ATIO;
2354 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2355 	tptr->atio_count--;
2356 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2357 	atiop->init_id = nphdl;
2358 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2359 	atiop->ccb_h.target_lun = lun;
2360 	atiop->sense_len = 0;
2361 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2362 	if (cdbxlen) {
2363 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2364 	}
2365 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2366 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2367 	atiop->cdb_len = cdbxlen;
2368 	atiop->ccb_h.status = CAM_CDB_RECVD;
2369 	atiop->tag_id = atp->tag;
2370 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2371 	case FCP_CMND_TASK_ATTR_SIMPLE:
2372 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2373 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2374 		break;
2375 	case FCP_CMND_TASK_ATTR_HEAD:
2376 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2377 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2378 		break;
2379 	case FCP_CMND_TASK_ATTR_ORDERED:
2380 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2381 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2382 		break;
2383 	default:
2384 		/* FALLTHROUGH */
2385 	case FCP_CMND_TASK_ATTR_ACA:
2386 	case FCP_CMND_TASK_ATTR_UNTAGGED:
2387 		atiop->tag_action = 0;
2388 		break;
2389 	}
2390 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2391 	atp->bytes_xfered = 0;
2392 	atp->last_xframt = 0;
2393 	atp->lun = lun;
2394 	atp->nphdl = nphdl;
2395 	atp->portid = sid;
2396 	atp->oxid = aep->at_hdr.ox_id;
2397 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2398 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2399 	atp->state = ATPD_STATE_CAM;
2400 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2401 	xpt_done((union ccb *)atiop);
2402 	rls_lun_statep(isp, tptr);
2403 	return;
2404 noresrc:
2405 	if (atp) {
2406 		isp_put_atpd(isp, tptr, atp);
2407 	}
2408 	ntp = isp_get_ntpd(isp, tptr);
2409 	if (ntp == NULL) {
2410 		rls_lun_statep(isp, tptr);
2411 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2412 		return;
2413 	}
2414 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2415 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2416 	tptr->restart_queue = ntp;
2417 	rls_lun_statep(isp, tptr);
2418 }
2419 
2420 static void
2421 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2422 {
2423 	union ccb *ccb;
2424 	int sentstatus, ok, notify_cam, resid = 0;
2425 	tstate_t *tptr = NULL;
2426 	atio_private_data_t *atp = NULL;
2427 	int bus;
2428 	uint32_t tval, handle;
2429 
2430 	/*
2431 	 * CTIO handles are 16 bits.
2432 	 * CTIO2 and CTIO7 are 32 bits.
2433 	 */
2434 
2435 	if (IS_SCSI(isp)) {
2436 		handle = ((ct_entry_t *)arg)->ct_syshandle;
2437 	} else {
2438 		handle = ((ct2_entry_t *)arg)->ct_syshandle;
2439 	}
2440 	ccb = isp_find_xs_tgt(isp, handle);
2441 	if (ccb == NULL) {
2442 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2443 		return;
2444 	}
2445 	isp_destroy_tgt_handle(isp, handle);
2446 	bus = XS_CHANNEL(ccb);
2447 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2448 	if (tptr == NULL) {
2449 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2450 	}
2451 	KASSERT((tptr != NULL), ("cannot get state pointer"));
2452 	if (isp->isp_nactive) {
2453 		isp->isp_nactive++;
2454 	}
2455 	if (IS_24XX(isp)) {
2456 		ct7_entry_t *ct = arg;
2457 
2458 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2459 		if (atp == NULL) {
2460 			rls_lun_statep(isp, tptr);
2461 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2462 			return;
2463 		}
2464 
2465 		sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2466 		ok = (ct->ct_nphdl == CT7_OK);
2467 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2468 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2469 		}
2470 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2471 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2472 			resid = ct->ct_resid;
2473 			atp->bytes_xfered += (atp->last_xframt - resid);
2474 			atp->last_xframt = 0;
2475 		}
2476 		if (ct->ct_nphdl == CT_HBA_RESET) {
2477 			ok = 0;
2478 			notify_cam = 1;
2479 			sentstatus = 1;
2480 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2481 		} else if (!ok) {
2482 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2483 		}
2484 		tval = atp->tag;
2485 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2486 		    ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2487 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2488 	} else if (IS_FC(isp)) {
2489 		ct2_entry_t *ct = arg;
2490 
2491 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2492 		if (atp == NULL) {
2493 			rls_lun_statep(isp, tptr);
2494 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2495 			return;
2496 		}
2497 		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2498 		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2499 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2500 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2501 		}
2502 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2503 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2504 			resid = ct->ct_resid;
2505 			atp->bytes_xfered += (atp->last_xframt - resid);
2506 			atp->last_xframt = 0;
2507 		}
2508 		if (ct->ct_status == CT_HBA_RESET) {
2509 			ok = 0;
2510 			notify_cam = 1;
2511 			sentstatus = 1;
2512 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2513 		} else if (!ok) {
2514 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2515 		}
2516 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2517 		    ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2518 		tval = atp->tag;
2519 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2520 	} else {
2521 		ct_entry_t *ct = arg;
2522 		sentstatus = ct->ct_flags & CT_SENDSTATUS;
2523 		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
2524 		/*
2525 		 * We *ought* to be able to get back to the original ATIO
2526 		 * here, but for some reason this gets lost. It's just as
2527 		 * well because it's squirrelled away as part of periph
2528 		 * private data.
2529 		 *
2530 		 * We can live without it as long as we continue to use
2531 		 * the auto-replenish feature for CTIOs.
2532 		 */
2533 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2534 		if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2535 			ok = 0;
2536 			notify_cam = 1;
2537 			sentstatus = 1;
2538 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2539 		} else if (!ok) {
2540 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2541 		} else if (ct->ct_status & QLTM_SVALID) {
2542 			char *sp = (char *)ct;
2543 			sp += CTIO_SENSE_OFFSET;
2544 			ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2545 			ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2546 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2547 		}
2548 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2549 			resid = ct->ct_resid;
2550 		}
2551 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2552 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2553 		tval = ct->ct_fwhandle;
2554 	}
2555 	ccb->csio.resid += resid;
2556 
2557 	/*
2558 	 * We're here either because intermediate data transfers are done
2559 	 * and/or the final status CTIO (which may have joined with a
2560 	 * Data Transfer) is done.
2561 	 *
2562 	 * In any case, for this platform, the upper layers figure out
2563 	 * what to do next, so all we do here is collect status and
2564 	 * pass information along. Any DMA handles have already been
2565 	 * freed.
2566 	 */
2567 	if (notify_cam == 0) {
2568 		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
2569 		return;
2570 	}
2571 	if (tptr) {
2572 		rls_lun_statep(isp, tptr);
2573 	}
2574 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? "  FINAL " : "MIDTERM ", tval);
2575 
2576 	if (!ok && !IS_24XX(isp)) {
2577 		isp_target_putback_atio(ccb);
2578 	} else {
2579 		isp_complete_ctio(ccb);
2580 	}
2581 }
2582 
2583 static void
2584 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2585 {
2586 	(void) isp_notify_ack(isp, inot);
2587 }
2588 
2589 static void
2590 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2591 {
2592 	int needack = 1;
2593 	switch (inp->in_status) {
2594 	case IN_PORT_LOGOUT:
2595 		/*
2596 		 * XXX: Need to delete this initiator's WWN from the database
2597 		 * XXX: Need to send this LOGOUT upstream
2598 		 */
2599 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2600 		break;
2601 	case IN_PORT_CHANGED:
2602 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2603 		break;
2604 	case IN_GLOBAL_LOGO:
2605 		isp_del_all_wwn_entries(isp, 0);
2606 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2607 		break;
2608 	case IN_ABORT_TASK:
2609 	{
2610 		tstate_t *tptr;
2611 		uint16_t lun;
2612 		uint32_t loopid;
2613 		uint64_t wwn;
2614 		atio_private_data_t *atp;
2615 		fcportdb_t *lp;
2616 		struct ccb_immediate_notify *inot = NULL;
2617 
2618 		if (ISP_CAP_SCCFW(isp)) {
2619 			lun = inp->in_scclun;
2620 		} else {
2621 			lun = inp->in_lun;
2622 		}
2623 		if (ISP_CAP_2KLOGIN(isp)) {
2624 			loopid = ((in_fcentry_e_t *)inp)->in_iid;
2625 		} else {
2626 			loopid = inp->in_iid;
2627 		}
2628 		if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2629 			wwn = lp->port_wwn;
2630 		} else {
2631 			wwn = INI_ANY;
2632 		}
2633 		tptr = get_lun_statep(isp, 0, lun);
2634 		if (tptr == NULL) {
2635 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2636 			if (tptr == NULL) {
2637 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2638 				return;
2639 			}
2640 		}
2641 		atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2642 
2643 		if (atp) {
2644 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2645 			isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2646 			if (inot) {
2647 				tptr->inot_count--;
2648 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2649 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2650 			} else {
2651 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2652 			}
2653 		} else {
2654 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2655 		}
2656 		if (inot) {
2657 			isp_notify_t tmp, *nt = &tmp;
2658 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
2659     			nt->nt_hba = isp;
2660 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2661 			nt->nt_wwn = wwn;
2662 			nt->nt_nphdl = loopid;
2663 			nt->nt_sid = PORT_ANY;
2664 			nt->nt_did = PORT_ANY;
2665     			nt->nt_lun = lun;
2666             		nt->nt_need_ack = 1;
2667     			nt->nt_channel = 0;
2668     			nt->nt_ncode = NT_ABORT_TASK;
2669     			nt->nt_lreserved = inot;
2670 			isp_handle_platform_target_tmf(isp, nt);
2671 			needack = 0;
2672 		}
2673 		rls_lun_statep(isp, tptr);
2674 		break;
2675 	}
2676 	default:
2677 		break;
2678 	}
2679 	if (needack) {
2680 		(void) isp_notify_ack(isp, inp);
2681 	}
2682 }
2683 
2684 static void
2685 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2686 {
2687 	uint16_t nphdl;
2688 	uint32_t portid;
2689 	fcportdb_t *lp;
2690 	uint8_t *ptr = NULL;
2691 	uint64_t wwn;
2692 
2693 	nphdl = inot->in_nphdl;
2694 	if (nphdl != NIL_HANDLE) {
2695 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2696 	} else {
2697 		portid = PORT_ANY;
2698 	}
2699 
2700 	switch (inot->in_status) {
2701 	case IN24XX_ELS_RCVD:
2702 	{
2703 		char buf[16], *msg;
2704 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2705 
2706 		/*
2707 		 * Note that we're just getting notification that an ELS was received
2708 		 * (possibly with some associated information sent upstream). This is
2709 		 * *not* the same as being given the ELS frame to accept or reject.
2710 		 */
2711 		switch (inot->in_status_subcode) {
2712 		case LOGO:
2713 			msg = "LOGO";
2714 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2715 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2716 				wwn =	(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF])   << 56) |
2717 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2718 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2719 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2720 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2721 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2722 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) <<  8) |
2723 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2724 			} else {
2725 				wwn = INI_ANY;
2726 			}
2727 			isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2728 			break;
2729 		case PRLO:
2730 			msg = "PRLO";
2731 			break;
2732 		case PLOGI:
2733 		case PRLI:
2734 			/*
2735 			 * Treat PRLI the same as PLOGI and make a database entry for it.
2736 			 */
2737 			if (inot->in_status_subcode == PLOGI)
2738 				msg = "PLOGI";
2739 			else
2740 				msg = "PRLI";
2741 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2742 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2743 				wwn =	(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
2744 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2745 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2746 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2747 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2748 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2749 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) <<  8) |
2750 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2751 			} else {
2752 				wwn = INI_NONE;
2753 			}
2754 			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2755 			break;
2756 		case PDISC:
2757 			msg = "PDISC";
2758 			break;
2759 		case ADISC:
2760 			msg = "ADISC";
2761 			break;
2762 		default:
2763 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2764 			msg = buf;
2765 			break;
2766 		}
2767 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2768 			isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
2769 			break;
2770 		}
2771 		isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
2772 		    inot->in_rxid, inot->in_oxid);
2773 		(void) isp_notify_ack(isp, inot);
2774 		break;
2775 	}
2776 
2777 	case IN24XX_PORT_LOGOUT:
2778 		ptr = "PORT LOGOUT";
2779 		if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2780 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2781 		}
2782 		/* FALLTHROUGH */
2783 	case IN24XX_PORT_CHANGED:
2784 		if (ptr == NULL) {
2785 			ptr = "PORT CHANGED";
2786 		}
2787 		/* FALLTHROUGH */
2788 	case IN24XX_LIP_RESET:
2789 		if (ptr == NULL) {
2790 			ptr = "LIP RESET";
2791 		}
2792 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr, inot->in_status_subcode, nphdl);
2793 
2794 		/*
2795 		 * All subcodes here are irrelevant. What is relevant
2796 		 * is that we need to terminate all active commands from
2797 		 * this initiator (known by N-port handle).
2798 		 */
2799 		/* XXX IMPLEMENT XXX */
2800 		(void) isp_notify_ack(isp, inot);
2801 		break;
2802 
2803 	case IN24XX_LINK_RESET:
2804 	case IN24XX_LINK_FAILED:
2805 	case IN24XX_SRR_RCVD:
2806 	default:
2807 		(void) isp_notify_ack(isp, inot);
2808 		break;
2809 	}
2810 }
2811 
2812 static int
2813 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2814 {
2815 
2816 	if (isp->isp_state != ISP_RUNSTATE) {
2817 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2818 		return (0);
2819 	}
2820 
2821 	/*
2822 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2823 	 */
2824 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2825 		ct7_entry_t local, *cto = &local;
2826 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2827 		fcportdb_t *lp;
2828 		uint32_t sid;
2829 		uint16_t nphdl;
2830 
2831 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2832 		if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2833 			nphdl = lp->handle;
2834 		} else {
2835 			nphdl = NIL_HANDLE;
2836 		}
2837 		ISP_MEMZERO(&local, sizeof (local));
2838 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2839 		cto->ct_header.rqs_entry_count = 1;
2840 		cto->ct_nphdl = nphdl;
2841 		cto->ct_rxid = aep->at_rxid;
2842 		cto->ct_vpidx = mp->nt_channel;
2843 		cto->ct_iid_lo = sid;
2844 		cto->ct_iid_hi = sid >> 16;
2845 		cto->ct_oxid = aep->at_hdr.ox_id;
2846 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2847 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2848 		return (isp_target_put_entry(isp, &local));
2849 	}
2850 
2851 	/*
2852 	 * This case is for a responding to an ABTS frame
2853 	 */
2854 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2855 
2856 		/*
2857 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2858 		 */
2859 		if (mp->nt_need_ack) {
2860 			uint8_t storage[QENTRY_LEN];
2861 			ct7_entry_t *cto = (ct7_entry_t *) storage;
2862 			abts_t *abts = (abts_t *)mp->nt_lreserved;
2863 
2864 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2865 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2866 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2867 			cto->ct_header.rqs_entry_count = 1;
2868 			cto->ct_nphdl = mp->nt_nphdl;
2869 			cto->ct_rxid = abts->abts_rxid_task;
2870 			cto->ct_iid_lo = mp->nt_sid;
2871 			cto->ct_iid_hi = mp->nt_sid >> 16;
2872 			cto->ct_oxid = abts->abts_ox_id;
2873 			cto->ct_vpidx = mp->nt_channel;
2874 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2875 			if (isp_target_put_entry(isp, cto)) {
2876 				return (ENOMEM);
2877 			}
2878 			mp->nt_need_ack = 0;
2879 		}
2880 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2881 			return (ENOMEM);
2882 		} else {
2883 			return (0);
2884 		}
2885 	}
2886 
2887 	/*
2888 	 * Handle logout cases here
2889 	 */
2890 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2891 		isp_del_all_wwn_entries(isp, mp->nt_channel);
2892 	}
2893 
2894 	if (mp->nt_ncode == NT_LOGOUT) {
2895 		if (!IS_2100(isp) && IS_FC(isp)) {
2896 			isp_del_wwn_entries(isp, mp);
2897 		}
2898 	}
2899 
2900 	/*
2901 	 * General purpose acknowledgement
2902 	 */
2903 	if (mp->nt_need_ack) {
2904 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2905 		return (isp_notify_ack(isp, mp->nt_lreserved));
2906 	}
2907 	return (0);
2908 }
2909 
2910 /*
2911  * Handle task management functions.
2912  *
2913  * We show up here with a notify structure filled out.
2914  *
2915  * The nt_lreserved tag points to the original queue entry
2916  */
2917 static void
2918 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2919 {
2920 	tstate_t *tptr;
2921 	fcportdb_t *lp;
2922 	struct ccb_immediate_notify *inot;
2923 	inot_private_data_t *ntp = NULL;
2924 	lun_id_t lun;
2925 
2926 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2927 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2928 	/*
2929 	 * NB: This assignment is necessary because of tricky type conversion.
2930 	 * XXX: This is tricky and I need to check this. If the lun isn't known
2931 	 * XXX: for the task management function, it does not of necessity follow
2932 	 * XXX: that it should go up stream to the wildcard listener.
2933 	 */
2934 	if (notify->nt_lun == LUN_ANY) {
2935 		lun = CAM_LUN_WILDCARD;
2936 	} else {
2937 		lun = notify->nt_lun;
2938 	}
2939 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
2940 	if (tptr == NULL) {
2941 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2942 		if (tptr == NULL) {
2943 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2944 			goto bad;
2945 		}
2946 	}
2947 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2948 	if (inot == NULL) {
2949 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2950 		goto bad;
2951 	}
2952 
2953 	if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2954 		inot->initiator_id = CAM_TARGET_WILDCARD;
2955 	} else {
2956 		inot->initiator_id = lp->handle;
2957 	}
2958 	inot->seq_id = notify->nt_tagval;
2959 	inot->tag_id = notify->nt_tagval >> 32;
2960 
2961 	switch (notify->nt_ncode) {
2962 	case NT_ABORT_TASK:
2963 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2964 		inot->arg = MSG_ABORT_TASK;
2965 		break;
2966 	case NT_ABORT_TASK_SET:
2967 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2968 		inot->arg = MSG_ABORT_TASK_SET;
2969 		break;
2970 	case NT_CLEAR_ACA:
2971 		inot->arg = MSG_CLEAR_ACA;
2972 		break;
2973 	case NT_CLEAR_TASK_SET:
2974 		inot->arg = MSG_CLEAR_TASK_SET;
2975 		break;
2976 	case NT_LUN_RESET:
2977 		inot->arg = MSG_LOGICAL_UNIT_RESET;
2978 		break;
2979 	case NT_TARGET_RESET:
2980 		inot->arg = MSG_TARGET_RESET;
2981 		break;
2982 	default:
2983 		isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
2984 		goto bad;
2985 	}
2986 
2987 	ntp = isp_get_ntpd(isp, tptr);
2988 	if (ntp == NULL) {
2989 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2990 		goto bad;
2991 	}
2992 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2993 	if (notify->nt_lreserved) {
2994 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2995 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2996 	}
2997 	ntp->rd.seq_id = notify->nt_tagval;
2998 	ntp->rd.tag_id = notify->nt_tagval >> 32;
2999 
3000 	tptr->inot_count--;
3001 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3002 	rls_lun_statep(isp, tptr);
3003 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3004 	inot->ccb_h.status = CAM_MESSAGE_RECV;
3005 	xpt_done((union ccb *)inot);
3006 	return;
3007 bad:
3008 	if (tptr) {
3009 		rls_lun_statep(isp, tptr);
3010 	}
3011 	if (notify->nt_need_ack && notify->nt_lreserved) {
3012 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3013 			(void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
3014 		} else {
3015 			(void) isp_notify_ack(isp, notify->nt_lreserved);
3016 		}
3017 	}
3018 }
3019 
3020 /*
3021  * Find the associated private data and mark it as dead so
3022  * we don't try to work on it any further.
3023  */
3024 static void
3025 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3026 {
3027 	tstate_t *tptr;
3028 	atio_private_data_t *atp;
3029 	union ccb *accb = ccb->cab.abort_ccb;
3030 
3031 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3032 	if (tptr == NULL) {
3033 		tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3034 		if (tptr == NULL) {
3035 			ccb->ccb_h.status = CAM_REQ_INVALID;
3036 			return;
3037 		}
3038 	}
3039 
3040 	atp = isp_get_atpd(isp, tptr, accb->atio.tag_id);
3041 	if (atp == NULL) {
3042 		ccb->ccb_h.status = CAM_REQ_INVALID;
3043 	} else {
3044 		atp->dead = 1;
3045 		ccb->ccb_h.status = CAM_REQ_CMP;
3046 	}
3047 	rls_lun_statep(isp, tptr);
3048 }
3049 
3050 static void
3051 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3052 {
3053 	atio_private_data_t *atp;
3054 	inot_private_data_t *restart_queue = tptr->restart_queue;
3055 
3056 	/*
3057 	 * First, clean any commands pending restart
3058 	 */
3059 	tptr->restart_queue = NULL;
3060 	while (restart_queue) {
3061 		uint32_t this_tag_id;
3062 		inot_private_data_t *ntp = restart_queue;
3063 
3064 		restart_queue = ntp->rd.nt.nt_hba;
3065 
3066 		if (IS_24XX(isp)) {
3067 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3068 		} else {
3069 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3070 		}
3071 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3072 			isp_put_ntpd(isp, tptr, ntp);
3073 		} else {
3074 			ntp->rd.nt.nt_hba = tptr->restart_queue;
3075 			tptr->restart_queue = ntp;
3076 		}
3077 	}
3078 
3079 	/*
3080 	 * Now mark other ones dead as well.
3081 	 */
3082 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3083 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3084 			atp->dead = 1;
3085 		}
3086 	}
3087 }
3088 
3089 
3090 #ifdef	ISP_INTERNAL_TARGET
3091 // #define	ISP_FORCE_TIMEOUT		1
3092 // #define	ISP_TEST_WWNS			1
3093 // #define	ISP_TEST_SEPARATE_STATUS	1
3094 
3095 #define	ccb_data_offset		ppriv_field0
3096 #define	ccb_atio		ppriv_ptr1
3097 #define	ccb_inot		ppriv_ptr1
3098 
3099 #define	MAX_ISP_TARG_TRANSFER	(2 << 20)
3100 #define	NISP_TARG_CMDS		1024
3101 #define	NISP_TARG_NOTIFIES	1024
3102 #define	DISK_SHIFT		9
3103 #define	JUNK_SIZE		256
3104 
3105 #ifndef	VERIFY_10
3106 #define	VERIFY_10	0x2f
3107 #endif
3108 
3109 TAILQ_HEAD(ccb_queue, ccb_hdr);
3110 extern u_int vm_kmem_size;
3111 static int ca;
3112 static uint32_t disk_size;
3113 static uint8_t *disk_data = NULL;
3114 static uint8_t *junk_data;
3115 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3116 struct isptarg_softc {
3117 	/* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3118 	struct ccb_queue	work_queue;
3119 	struct ccb_queue	rework_queue;
3120 	struct ccb_queue	running_queue;
3121 	struct ccb_queue	inot_queue;
3122 	struct cam_periph       *periph;
3123 	struct cam_path	 	*path;
3124 	ispsoftc_t		*isp;
3125 };
3126 static periph_ctor_t	isptargctor;
3127 static periph_dtor_t	isptargdtor;
3128 static periph_start_t	isptargstart;
3129 static periph_init_t	isptarginit;
3130 static void		isptarg_done(struct cam_periph *, union ccb *);
3131 static void		isptargasync(void *, u_int32_t, struct cam_path *, void *);
3132 
3133 
3134 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3135 
3136 static struct periph_driver isptargdriver =
3137 {
3138 	isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3139 };
3140 
3141 static void
3142 isptarginit(void)
3143 {
3144 }
3145 
3146 static void
3147 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3148 {
3149 	struct ccb_notify_acknowledge *ack = &iccb->cna2;
3150 
3151 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3152 	    inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3153 	ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3154 	ack->ccb_h.flags = 0;
3155 	ack->ccb_h.retry_count = 0;
3156 	ack->ccb_h.cbfcnp = isptarg_done;
3157 	ack->ccb_h.timeout = 0;
3158 	ack->ccb_h.ccb_inot = inot;
3159 	ack->tag_id = inot->tag_id;
3160 	ack->seq_id = inot->seq_id;
3161 	ack->initiator_id = inot->initiator_id;
3162 	xpt_action(iccb);
3163 }
3164 
3165 static void
3166 isptargstart(struct cam_periph *periph, union ccb *iccb)
3167 {
3168 	const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3169 		0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3170 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3171 		'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3172 		'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3173 		'0', '0', '0', '1'
3174 	};
3175 	const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3176 		0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3177 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3178 		'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3179 		'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3180 		'0', '0', '0', '1'
3181 	};
3182 	int i, more = 0, last;
3183 	struct isptarg_softc *softc = periph->softc;
3184 	struct ccb_scsiio *csio;
3185 	lun_id_t return_lun;
3186 	struct ccb_accept_tio *atio;
3187 	uint8_t *cdb, *ptr, status;
3188 	uint8_t *data_ptr;
3189 	uint32_t data_len, flags;
3190 	struct ccb_hdr *ccbh;
3191 
3192 	mtx_assert(periph->sim->mtx, MA_OWNED);
3193 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3194 	    TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3195 	/*
3196 	 * Check for immediate notifies first
3197 	 */
3198 	ccbh = TAILQ_FIRST(&softc->inot_queue);
3199 	if (ccbh) {
3200 		TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3201 		if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3202 			xpt_schedule(periph, 1);
3203 		}
3204 		isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3205 		return;
3206 	}
3207 
3208 	/*
3209 	 * Check the rework (continuation) work queue first.
3210 	 */
3211 	ccbh = TAILQ_FIRST(&softc->rework_queue);
3212 	if (ccbh) {
3213 		atio = (struct ccb_accept_tio *)ccbh;
3214 		TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3215 		more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3216 	} else {
3217 		ccbh = TAILQ_FIRST(&softc->work_queue);
3218 		if (ccbh == NULL) {
3219 			ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3220 			xpt_release_ccb(iccb);
3221 			return;
3222 		}
3223 		atio = (struct ccb_accept_tio *)ccbh;
3224 		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3225 		more = TAILQ_FIRST(&softc->work_queue) != NULL;
3226 		atio->ccb_h.ccb_data_offset = 0;
3227 	}
3228 
3229 	if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3230 		panic("BAD ATIO");
3231 	}
3232 
3233 	data_ptr = NULL;
3234 	data_len = 0;
3235 	csio = &iccb->csio;
3236 	status = SCSI_STATUS_OK;
3237 	flags = CAM_SEND_STATUS;
3238 	memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3239 	cdb = atio->cdb_io.cdb_bytes;
3240 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
3241 	    cdb[0], atio->ccb_h.ccb_data_offset);
3242 
3243 	return_lun = XS_LUN(atio);
3244 	if (return_lun != 0) {
3245 		xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3246 		if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3247 			status = SCSI_STATUS_CHECK_COND;
3248 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3249 			SDFIXED(atio->sense_data)->add_sense_code = 0x25;
3250 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x0;
3251 			atio->sense_len = SSD_MIN_SIZE;
3252 		}
3253 		return_lun = CAM_LUN_WILDCARD;
3254 	}
3255 
3256 	switch (cdb[0]) {
3257 	case REQUEST_SENSE:
3258 		flags |= CAM_DIR_IN;
3259 		data_len = sizeof (atio->sense_data);
3260 		junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3261 		memset(junk_data+1, 0, data_len-1);
3262 		if (data_len > cdb[4]) {
3263 			data_len = cdb[4];
3264 		}
3265 		if (data_len) {
3266 			data_ptr = junk_data;
3267 		}
3268 		break;
3269 	case READ_6:
3270 	case READ_10:
3271 	case READ_12:
3272 	case READ_16:
3273 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3274 			status = SCSI_STATUS_CHECK_COND;
3275 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3276 			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
3277 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
3278 			atio->sense_len = SSD_MIN_SIZE;
3279 		} else {
3280 #ifdef	ISP_FORCE_TIMEOUT
3281 			{
3282 				static int foo;
3283 				if (foo++ == 500) {
3284 					if (more) {
3285 						xpt_schedule(periph, 1);
3286 					}
3287 					foo = 0;
3288 					return;
3289 				}
3290 			}
3291 #endif
3292 #ifdef	ISP_TEST_SEPARATE_STATUS
3293 			if (last && data_len) {
3294 				last = 0;
3295 			}
3296 #endif
3297 			if (last == 0) {
3298 				flags &= ~CAM_SEND_STATUS;
3299 			}
3300 			if (data_len) {
3301 				atio->ccb_h.ccb_data_offset += data_len;
3302 				flags |= CAM_DIR_IN;
3303 			} else {
3304 				flags |= CAM_DIR_NONE;
3305 			}
3306 		}
3307 		break;
3308 	case WRITE_6:
3309 	case WRITE_10:
3310 	case WRITE_12:
3311 	case WRITE_16:
3312 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3313 			status = SCSI_STATUS_CHECK_COND;
3314 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3315 			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
3316 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
3317 			atio->sense_len = SSD_MIN_SIZE;
3318 		} else {
3319 #ifdef	ISP_FORCE_TIMEOUT
3320 			{
3321 				static int foo;
3322 				if (foo++ == 500) {
3323 					if (more) {
3324 						xpt_schedule(periph, 1);
3325 					}
3326 					foo = 0;
3327 					return;
3328 				}
3329 			}
3330 #endif
3331 #ifdef	ISP_TEST_SEPARATE_STATUS
3332 			if (last && data_len) {
3333 				last = 0;
3334 			}
3335 #endif
3336 			if (last == 0) {
3337 				flags &= ~CAM_SEND_STATUS;
3338 			}
3339 			if (data_len) {
3340 				atio->ccb_h.ccb_data_offset += data_len;
3341 				flags |= CAM_DIR_OUT;
3342 			} else {
3343 				flags |= CAM_DIR_NONE;
3344 			}
3345 		}
3346 		break;
3347 	case INQUIRY:
3348 		flags |= CAM_DIR_IN;
3349 		if (cdb[1] || cdb[2] || cdb[3]) {
3350 			status = SCSI_STATUS_CHECK_COND;
3351 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3352 			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
3353 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x20;
3354 			atio->sense_len = SSD_MIN_SIZE;
3355 			break;
3356 		}
3357 		data_len = sizeof (iqd);
3358 		if (data_len > cdb[4]) {
3359 			data_len = cdb[4];
3360 		}
3361 		if (data_len) {
3362 			if (XS_LUN(iccb) != 0) {
3363 				memcpy(junk_data, niliqd, sizeof (iqd));
3364 			} else {
3365 				memcpy(junk_data, iqd, sizeof (iqd));
3366 			}
3367 			data_ptr = junk_data;
3368 		}
3369 		break;
3370 	case TEST_UNIT_READY:
3371 		flags |= CAM_DIR_NONE;
3372 		if (ca) {
3373 			ca = 0;
3374 			status = SCSI_STATUS_CHECK_COND;
3375 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3376 			SDFIXED(atio->sense_data)->add_sense_code = 0x28;
3377 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x0;
3378 			atio->sense_len = SSD_MIN_SIZE;
3379 		}
3380 		break;
3381 	case SYNCHRONIZE_CACHE:
3382 	case START_STOP:
3383 	case RESERVE:
3384 	case RELEASE:
3385 	case VERIFY_10:
3386 		flags |= CAM_DIR_NONE;
3387 		break;
3388 
3389 	case READ_CAPACITY:
3390 		flags |= CAM_DIR_IN;
3391 		if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3392 			status = SCSI_STATUS_CHECK_COND;
3393 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3394 			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
3395 			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
3396 			atio->sense_len = SSD_MIN_SIZE;
3397 			break;
3398 		}
3399 		if (cdb[8] & 0x1) { /* PMI */
3400 			junk_data[0] = 0xff;
3401 			junk_data[1] = 0xff;
3402 			junk_data[2] = 0xff;
3403 			junk_data[3] = 0xff;
3404 		} else {
3405 			uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3406 			if (last_blk < 0xffffffffULL) {
3407 			    junk_data[0] = (last_blk >> 24) & 0xff;
3408 			    junk_data[1] = (last_blk >> 16) & 0xff;
3409 			    junk_data[2] = (last_blk >>  8) & 0xff;
3410 			    junk_data[3] = (last_blk) & 0xff;
3411 			} else {
3412 			    junk_data[0] = 0xff;
3413 			    junk_data[1] = 0xff;
3414 			    junk_data[2] = 0xff;
3415 			    junk_data[3] = 0xff;
3416 			}
3417 		}
3418 		junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3419 		junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3420 		junk_data[6] = ((1 << DISK_SHIFT) >>  8) & 0xff;
3421 		junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3422 		data_ptr = junk_data;
3423 		data_len = 8;
3424 		break;
3425 	case REPORT_LUNS:
3426 		flags |= CAM_DIR_IN;
3427 		memset(junk_data, 0, JUNK_SIZE);
3428 		junk_data[0] = (1 << 3) >> 24;
3429 		junk_data[1] = (1 << 3) >> 16;
3430 		junk_data[2] = (1 << 3) >> 8;
3431 		junk_data[3] = (1 << 3);
3432 		ptr = NULL;
3433 		for (i = 0; i < 1; i++) {
3434 			ptr = &junk_data[8 + (1 << 3)];
3435 			if (i >= 256) {
3436 				ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3437 			}
3438 			ptr[1] = i;
3439 		}
3440 		data_ptr = junk_data;
3441 		data_len = (ptr + 8) - junk_data;
3442 		break;
3443 
3444 	default:
3445 		flags |= CAM_DIR_NONE;
3446 		status = SCSI_STATUS_CHECK_COND;
3447 		SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3448 		SDFIXED(atio->sense_data)->add_sense_code = 0x5;
3449 		SDFIXED(atio->sense_data)->add_sense_code_qual = 0x20;
3450 		atio->sense_len = SSD_MIN_SIZE;
3451 		break;
3452 	}
3453 
3454 	/*
3455 	 * If we are done with the transaction, tell the
3456 	 * controller to send status and perform a CMD_CMPLT.
3457 	 * If we have associated sense data, see if we can
3458 	 * send that too.
3459 	 */
3460 	if (status == SCSI_STATUS_CHECK_COND) {
3461 		flags |= CAM_SEND_SENSE;
3462 		csio->sense_len = atio->sense_len;
3463 		csio->sense_data = atio->sense_data;
3464 		flags &= ~CAM_DIR_MASK;
3465 		data_len = 0;
3466 		data_ptr = NULL;
3467 	}
3468 	cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3469 	iccb->ccb_h.target_id = atio->ccb_h.target_id;
3470 	iccb->ccb_h.target_lun = return_lun;
3471 	iccb->ccb_h.ccb_atio = atio;
3472 	xpt_action(iccb);
3473 
3474 	if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3475 		cam_release_devq(periph->path, 0, 0, 0, 0);
3476 		atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3477 	}
3478 	if (more) {
3479 		xpt_schedule(periph, 1);
3480 	}
3481 }
3482 
3483 static cam_status
3484 isptargctor(struct cam_periph *periph, void *arg)
3485 {
3486 	struct isptarg_softc *softc;
3487 
3488 	softc = (struct isptarg_softc *)arg;
3489 	periph->softc = softc;
3490 	softc->periph = periph;
3491 	softc->path = periph->path;
3492 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3493 	return (CAM_REQ_CMP);
3494 }
3495 
3496 static void
3497 isptargdtor(struct cam_periph *periph)
3498 {
3499 	struct isptarg_softc *softc;
3500 	softc = (struct isptarg_softc *)periph->softc;
3501 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3502 	softc->periph = NULL;
3503 	softc->path = NULL;
3504 	periph->softc = NULL;
3505 }
3506 
3507 static void
3508 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3509 {
3510 	struct isptarg_softc *softc;
3511 	ispsoftc_t *isp;
3512 	struct ccb_accept_tio *atio;
3513 	struct ccb_immediate_notify *inot;
3514 	cam_status status;
3515 
3516 	softc = (struct isptarg_softc *)periph->softc;
3517 	isp = softc->isp;
3518 	status = ccb->ccb_h.status & CAM_STATUS_MASK;
3519 
3520 	switch (ccb->ccb_h.func_code) {
3521 	case XPT_ACCEPT_TARGET_IO:
3522 		atio = (struct ccb_accept_tio *) ccb;
3523 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3524 		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3525 		xpt_schedule(periph, 1);
3526 		break;
3527 	case XPT_IMMEDIATE_NOTIFY:
3528 		inot = (struct ccb_immediate_notify *) ccb;
3529 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
3530 		TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3531 		xpt_schedule(periph, 1);
3532 		break;
3533 	case XPT_CONT_TARGET_IO:
3534 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3535 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3536 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3537 		}
3538 		atio = ccb->ccb_h.ccb_atio;
3539 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3540 			cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3541 			xpt_action((union ccb *)atio);
3542 		} else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3543 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3544 			TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3545 			xpt_schedule(periph, 1);
3546 		} else {
3547 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3548 			xpt_action((union ccb *)atio);
3549 		}
3550 		xpt_release_ccb(ccb);
3551 		break;
3552 	case XPT_NOTIFY_ACKNOWLEDGE:
3553 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3554 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3555 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3556 		}
3557 		inot = ccb->ccb_h.ccb_inot;
3558 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
3559 		xpt_release_ccb(ccb);
3560 		xpt_action((union ccb *)inot);
3561 		break;
3562 	default:
3563 		xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3564 		break;
3565 	}
3566 }
3567 
3568 static void
3569 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3570 {
3571 	struct ac_contract *acp = arg;
3572 	struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3573 
3574 	if (code != AC_CONTRACT) {
3575 		return;
3576 	}
3577 	xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3578 }
3579 
3580 static void
3581 isp_target_thread(ispsoftc_t *isp, int chan)
3582 {
3583 	union ccb *ccb = NULL;
3584 	int i;
3585 	void *wchan;
3586 	cam_status status;
3587 	struct isptarg_softc *softc = NULL;
3588 	struct cam_periph *periph = NULL, *wperiph = NULL;
3589 	struct cam_path *path, *wpath;
3590 	struct cam_sim *sim;
3591 
3592 	if (disk_data == NULL) {
3593 		disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3594 		if (disk_size < (50 << 20)) {
3595 			disk_size = 50 << 20;
3596 		}
3597 		disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3598 		if (disk_data == NULL) {
3599 			isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
3600 			goto out;
3601 		}
3602 		isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3603 	}
3604 	junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3605 	if (junk_data == NULL) {
3606 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
3607 		goto out;
3608 	}
3609 
3610 
3611 	softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3612 	if (softc == NULL) {
3613 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
3614 		goto out;
3615 	}
3616 	TAILQ_INIT(&softc->work_queue);
3617 	TAILQ_INIT(&softc->rework_queue);
3618 	TAILQ_INIT(&softc->running_queue);
3619 	TAILQ_INIT(&softc->inot_queue);
3620 	softc->isp = isp;
3621 
3622 	periphdriver_register(&isptargdriver);
3623 	ISP_GET_PC(isp, chan, sim, sim);
3624 	ISP_GET_PC(isp, chan, path,  path);
3625 	status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3626 	if (status != CAM_REQ_CMP) {
3627 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3628 		return;
3629 	}
3630 	status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3631 	if (status != CAM_REQ_CMP) {
3632 		xpt_free_path(wpath);
3633 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3634 		return;
3635 	}
3636 
3637 	ccb = xpt_alloc_ccb();
3638 
3639 	ISP_LOCK(isp);
3640 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3641 	if (status != CAM_REQ_CMP) {
3642 		ISP_UNLOCK(isp);
3643 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3644 		goto out;
3645 	}
3646 	wperiph = cam_periph_find(wpath, "isptarg");
3647 	if (wperiph == NULL) {
3648 		ISP_UNLOCK(isp);
3649 		isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3650 		goto out;
3651 	}
3652 
3653 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3654 	if (status != CAM_REQ_CMP) {
3655 		ISP_UNLOCK(isp);
3656 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3657 		goto out;
3658 	}
3659 
3660 	periph = cam_periph_find(path, "isptarg");
3661 	if (periph == NULL) {
3662 		ISP_UNLOCK(isp);
3663 		isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3664 		goto out;
3665 	}
3666 
3667 	status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3668 	if (status != CAM_REQ_CMP) {
3669 		ISP_UNLOCK(isp);
3670 		isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3671 		goto out;
3672 	}
3673 
3674 	ISP_UNLOCK(isp);
3675 
3676 	ccb = xpt_alloc_ccb();
3677 
3678 	/*
3679 	 * Make sure role is none.
3680 	 */
3681 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3682 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3683 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3684 #ifdef	ISP_TEST_WWNS
3685 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3686 	ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3687 	ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3688 #else
3689 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3690 #endif
3691 
3692 	ISP_LOCK(isp);
3693 	xpt_action(ccb);
3694 	ISP_UNLOCK(isp);
3695 
3696 	/*
3697 	 * Now enable luns
3698 	 */
3699 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3700 	ccb->ccb_h.func_code = XPT_EN_LUN;
3701 	ccb->cel.enable = 1;
3702 	ISP_LOCK(isp);
3703 	xpt_action(ccb);
3704 	ISP_UNLOCK(isp);
3705 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
3706 		xpt_free_ccb(ccb);
3707 		xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3708 		goto out;
3709 	}
3710 
3711 	xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3712 	ccb->ccb_h.func_code = XPT_EN_LUN;
3713 	ccb->cel.enable = 1;
3714 	ISP_LOCK(isp);
3715 	xpt_action(ccb);
3716 	ISP_UNLOCK(isp);
3717 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
3718 		xpt_free_ccb(ccb);
3719 		xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3720 		goto out;
3721 	}
3722 	xpt_free_ccb(ccb);
3723 
3724 	/*
3725 	 * Add resources
3726 	 */
3727 	ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3728 	for (i = 0; i < 4; i++) {
3729 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3730 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3731 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3732 		ccb->ccb_h.cbfcnp = isptarg_done;
3733 		ISP_LOCK(isp);
3734 		xpt_action(ccb);
3735 		ISP_UNLOCK(isp);
3736 	}
3737 	for (i = 0; i < NISP_TARG_CMDS; i++) {
3738 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3739 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3740 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3741 		ccb->ccb_h.cbfcnp = isptarg_done;
3742 		ISP_LOCK(isp);
3743 		xpt_action(ccb);
3744 		ISP_UNLOCK(isp);
3745 	}
3746 	for (i = 0; i < 4; i++) {
3747 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3748 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3749 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3750 		ccb->ccb_h.cbfcnp = isptarg_done;
3751 		ISP_LOCK(isp);
3752 		xpt_action(ccb);
3753 		ISP_UNLOCK(isp);
3754 	}
3755 	for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3756 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3757 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3758 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3759 		ccb->ccb_h.cbfcnp = isptarg_done;
3760 		ISP_LOCK(isp);
3761 		xpt_action(ccb);
3762 		ISP_UNLOCK(isp);
3763 	}
3764 
3765 	/*
3766 	 * Now turn it all back on
3767 	 */
3768 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3769 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3770 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3771 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3772 	ISP_LOCK(isp);
3773 	xpt_action(ccb);
3774 	ISP_UNLOCK(isp);
3775 
3776 	/*
3777 	 * Okay, while things are still active, sleep...
3778 	 */
3779 	ISP_LOCK(isp);
3780 	for (;;) {
3781 		ISP_GET_PC(isp, chan, proc_active, i);
3782 		if (i == 0) {
3783 			break;
3784 		}
3785 		msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3786 	}
3787 	ISP_UNLOCK(isp);
3788 
3789 out:
3790 	if (wperiph) {
3791 		cam_periph_invalidate(wperiph);
3792 	}
3793 	if (periph) {
3794 		cam_periph_invalidate(periph);
3795 	}
3796 	if (junk_data) {
3797 		free(junk_data, M_ISPTARG);
3798 	}
3799 	if (disk_data) {
3800 		free(disk_data, M_ISPTARG);
3801 	}
3802 	if (softc) {
3803 		free(softc, M_ISPTARG);
3804 	}
3805 	xpt_free_path(path);
3806 	xpt_free_path(wpath);
3807 }
3808 
3809 static void
3810 isp_target_thread_pi(void *arg)
3811 {
3812 	struct isp_spi *pi = arg;
3813 	isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3814 }
3815 
3816 static void
3817 isp_target_thread_fc(void *arg)
3818 {
3819 	struct isp_fc *fc = arg;
3820 	isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3821 }
3822 
3823 static int
3824 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3825 {
3826 	uint32_t cnt, curcnt;
3827 	uint64_t lba;
3828 
3829 	switch (cdb[0]) {
3830 	case WRITE_16:
3831 	case READ_16:
3832 		cnt =	(((uint32_t)cdb[10]) <<  24) |
3833 			(((uint32_t)cdb[11]) <<  16) |
3834 			(((uint32_t)cdb[12]) <<   8) |
3835 			((uint32_t)cdb[13]);
3836 
3837 		lba =	(((uint64_t)cdb[2]) << 56) |
3838 			(((uint64_t)cdb[3]) << 48) |
3839 			(((uint64_t)cdb[4]) << 40) |
3840 			(((uint64_t)cdb[5]) << 32) |
3841 			(((uint64_t)cdb[6]) << 24) |
3842 			(((uint64_t)cdb[7]) << 16) |
3843 			(((uint64_t)cdb[8]) <<  8) |
3844 			((uint64_t)cdb[9]);
3845 		break;
3846 	case WRITE_12:
3847 	case READ_12:
3848 		cnt =	(((uint32_t)cdb[6]) <<  16) |
3849 			(((uint32_t)cdb[7]) <<   8) |
3850 			((u_int32_t)cdb[8]);
3851 
3852 		lba =	(((uint32_t)cdb[2]) << 24) |
3853 			(((uint32_t)cdb[3]) << 16) |
3854 			(((uint32_t)cdb[4]) <<  8) |
3855 			((uint32_t)cdb[5]);
3856 		break;
3857 	case WRITE_10:
3858 	case READ_10:
3859 		cnt =	(((uint32_t)cdb[7]) <<  8) |
3860 			((u_int32_t)cdb[8]);
3861 
3862 		lba =	(((uint32_t)cdb[2]) << 24) |
3863 			(((uint32_t)cdb[3]) << 16) |
3864 			(((uint32_t)cdb[4]) <<  8) |
3865 			((uint32_t)cdb[5]);
3866 		break;
3867 	case WRITE_6:
3868 	case READ_6:
3869 		cnt = cdb[4];
3870 		if (cnt == 0) {
3871 			cnt = 256;
3872 		}
3873 		lba =	(((uint32_t)cdb[1] & 0x1f) << 16) |
3874 			(((uint32_t)cdb[2]) << 8) |
3875 			((uint32_t)cdb[3]);
3876 		break;
3877 	default:
3878 		return (-1);
3879 	}
3880 
3881 	cnt <<= DISK_SHIFT;
3882 	lba <<= DISK_SHIFT;
3883 
3884 	if (offset == cnt) {
3885 		*lp = 1;
3886 		return (0);
3887 	}
3888 
3889 	if (lba + cnt > dl) {
3890 		return (-1);
3891 	}
3892 
3893 
3894 	curcnt = MAX_ISP_TARG_TRANSFER;
3895 	if (offset + curcnt >= cnt) {
3896 		curcnt = cnt - offset;
3897 		*lp = 1;
3898 	} else {
3899 		*lp = 0;
3900 	}
3901 	*tl = curcnt;
3902 	*kp = &dp[lba + offset];
3903 	return (0);
3904 }
3905 
3906 #endif
3907 #endif
3908 
3909 static void
3910 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3911 {
3912 	struct cam_sim *sim;
3913 	int bus, tgt;
3914 	ispsoftc_t *isp;
3915 
3916 	sim = (struct cam_sim *)cbarg;
3917 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3918 	bus = cam_sim_bus(sim);
3919 	tgt = xpt_path_target_id(path);
3920 
3921 	switch (code) {
3922 	case AC_LOST_DEVICE:
3923 		if (IS_SCSI(isp)) {
3924 			uint16_t oflags, nflags;
3925 			sdparam *sdp = SDPARAM(isp, bus);
3926 
3927 			if (tgt >= 0) {
3928 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
3929 #ifndef	ISP_TARGET_MODE
3930 				nflags &= DPARM_SAFE_DFLT;
3931 				if (isp->isp_loaded_fw) {
3932 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3933 				}
3934 #else
3935 				nflags = DPARM_DEFAULT;
3936 #endif
3937 				oflags = sdp->isp_devparam[tgt].goal_flags;
3938 				sdp->isp_devparam[tgt].goal_flags = nflags;
3939 				sdp->isp_devparam[tgt].dev_update = 1;
3940 				sdp->update = 1;
3941 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3942 				sdp->isp_devparam[tgt].goal_flags = oflags;
3943 			}
3944 		}
3945 		break;
3946 	default:
3947 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3948 		break;
3949 	}
3950 }
3951 
3952 static void
3953 isp_poll(struct cam_sim *sim)
3954 {
3955 	ispsoftc_t *isp = cam_sim_softc(sim);
3956 	uint32_t isr;
3957 	uint16_t sema, mbox;
3958 
3959 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3960 		isp_intr(isp, isr, sema, mbox);
3961 	}
3962 }
3963 
3964 
3965 static void
3966 isp_watchdog(void *arg)
3967 {
3968 	struct ccb_scsiio *xs = arg;
3969 	ispsoftc_t *isp;
3970 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
3971 
3972 	isp = XS_ISP(xs);
3973 
3974 	handle = isp_find_handle(isp, xs);
3975 
3976 	if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3977 		isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3978 		callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3979 		XS_CMD_S_WPEND(xs);
3980 		return;
3981 	}
3982 	XS_C_TACTIVE(xs);
3983 
3984 	/*
3985 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3986 	 */
3987 	if (handle != ISP_HANDLE_FREE) {
3988 		uint32_t isr;
3989 		uint16_t sema, mbox;
3990 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3991 			isp_intr(isp, isr, sema, mbox);
3992 		}
3993 		ohandle = handle;
3994 		handle = isp_find_handle(isp, xs);
3995 	}
3996 	if (handle != ISP_HANDLE_FREE) {
3997 		/*
3998 		 * Try and make sure the command is really dead before
3999 		 * we release the handle (and DMA resources) for reuse.
4000 		 *
4001 		 * If we are successful in aborting the command then
4002 		 * we're done here because we'll get the command returned
4003 		 * back separately.
4004 		 */
4005 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
4006 			return;
4007 		}
4008 
4009 		/*
4010 		 * Note that after calling the above, the command may in
4011 		 * fact have been completed.
4012 		 */
4013 		xs = isp_find_xs(isp, handle);
4014 
4015 		/*
4016 		 * If the command no longer exists, then we won't
4017 		 * be able to find the xs again with this handle.
4018 		 */
4019 		if (xs == NULL) {
4020 			return;
4021 		}
4022 
4023 		/*
4024 		 * After this point, the command is really dead.
4025 		 */
4026 		if (XS_XFRLEN(xs)) {
4027 			ISP_DMAFREE(isp, xs, handle);
4028 		}
4029 		isp_destroy_handle(isp, handle);
4030 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
4031 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
4032 		isp_prt_endcmd(isp, xs);
4033 		isp_done(xs);
4034 	} else {
4035 		if (ohandle != ISP_HANDLE_FREE) {
4036 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
4037 		} else {
4038 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
4039 		}
4040 	}
4041 }
4042 
4043 static void
4044 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
4045 {
4046 	union ccb *ccb;
4047 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4048 
4049 	if (isp_autoconfig == 0) {
4050 		return;
4051 	}
4052 
4053 	/*
4054 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
4055 	 */
4056 	ccb = xpt_alloc_ccb_nowait();
4057 	if (ccb == NULL) {
4058 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
4059 		return;
4060 	}
4061 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4062 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
4063 		xpt_free_ccb(ccb);
4064 		return;
4065 	}
4066 	xpt_rescan(ccb);
4067 }
4068 
4069 static void
4070 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
4071 {
4072 	struct cam_path *tp;
4073 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4074 
4075 	if (isp_autoconfig == 0) {
4076 		return;
4077 	}
4078 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4079 		xpt_async(AC_LOST_DEVICE, tp, NULL);
4080 		xpt_free_path(tp);
4081 	}
4082 }
4083 
4084 /*
4085  * Gone Device Timer Function- when we have decided that a device has gone
4086  * away, we wait a specific period of time prior to telling the OS it has
4087  * gone away.
4088  *
4089  * This timer function fires once a second and then scans the port database
4090  * for devices that are marked dead but still have a virtual target assigned.
4091  * We decrement a counter for that port database entry, and when it hits zero,
4092  * we tell the OS the device has gone away.
4093  */
4094 static void
4095 isp_gdt(void *arg)
4096 {
4097 	struct isp_fc *fc = arg;
4098 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4099 }
4100 
4101 static void
4102 isp_gdt_task(void *arg, int pending)
4103 {
4104 	struct isp_fc *fc = arg;
4105 	ispsoftc_t *isp = fc->isp;
4106 	int chan = fc - isp->isp_osinfo.pc.fc;
4107 	fcportdb_t *lp;
4108 	int dbidx, tgt, more_to_do = 0;
4109 
4110 	ISP_LOCK(isp);
4111 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4112 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4113 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
4114 
4115 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4116 			continue;
4117 		}
4118 		if (lp->dev_map_idx == 0 || lp->target_mode) {
4119 			continue;
4120 		}
4121 		if (lp->gone_timer != 0) {
4122 			isp_prt(isp, ISP_LOGSANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
4123 			lp->gone_timer -= 1;
4124 			more_to_do++;
4125 			continue;
4126 		}
4127 		tgt = lp->dev_map_idx - 1;
4128 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4129 		lp->dev_map_idx = 0;
4130 		lp->state = FC_PORTDB_STATE_NIL;
4131 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4132 		isp_make_gone(isp, chan, tgt);
4133 	}
4134 	if (fc->ready) {
4135 		if (more_to_do) {
4136 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
4137 		} else {
4138 			callout_deactivate(&fc->gdt);
4139 			isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4140 		}
4141 	}
4142 	ISP_UNLOCK(isp);
4143 }
4144 
4145 /*
4146  * Loop Down Timer Function- when loop goes down, a timer is started and
4147  * and after it expires we come here and take all probational devices that
4148  * the OS knows about and the tell the OS that they've gone away.
4149  *
4150  * We don't clear the devices out of our port database because, when loop
4151  * come back up, we have to do some actual cleanup with the chip at that
4152  * point (implicit PLOGO, e.g., to get the chip's port database state right).
4153  */
4154 static void
4155 isp_ldt(void *arg)
4156 {
4157 	struct isp_fc *fc = arg;
4158 	taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4159 }
4160 
4161 static void
4162 isp_ldt_task(void *arg, int pending)
4163 {
4164 	struct isp_fc *fc = arg;
4165 	ispsoftc_t *isp = fc->isp;
4166 	int chan = fc - isp->isp_osinfo.pc.fc;
4167 	fcportdb_t *lp;
4168 	int dbidx, tgt, i;
4169 
4170 	ISP_LOCK(isp);
4171 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4172 	callout_deactivate(&fc->ldt);
4173 
4174 	/*
4175 	 * Notify to the OS all targets who we now consider have departed.
4176 	 */
4177 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4178 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
4179 
4180 		if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4181 			continue;
4182 		}
4183 		if (lp->dev_map_idx == 0 || lp->target_mode) {
4184 			continue;
4185 		}
4186 
4187 		/*
4188 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4189 		 */
4190 
4191 
4192 		for (i = 0; i < isp->isp_maxcmds; i++) {
4193 			struct ccb_scsiio *xs;
4194 
4195 			if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4196 				continue;
4197 			}
4198 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4199 				continue;
4200                         }
4201 			if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4202 				continue;
4203 			}
4204 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4205 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4206 		}
4207 
4208 		/*
4209 		 * Mark that we've announced that this device is gone....
4210 		 */
4211 		lp->reserved = 1;
4212 
4213 		/*
4214 		 * but *don't* change the state of the entry. Just clear
4215 		 * any target id stuff and announce to CAM that the
4216 		 * device is gone. This way any necessary PLOGO stuff
4217 		 * will happen when loop comes back up.
4218 		 */
4219 
4220 		tgt = lp->dev_map_idx - 1;
4221 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4222 		lp->dev_map_idx = 0;
4223 		lp->state = FC_PORTDB_STATE_NIL;
4224 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4225 		isp_make_gone(isp, chan, tgt);
4226 	}
4227 
4228 	if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4229 		isp_unfreeze_loopdown(isp, chan);
4230 	}
4231 	/*
4232 	 * The loop down timer has expired. Wake up the kthread
4233 	 * to notice that fact (or make it false).
4234 	 */
4235 	fc->loop_dead = 1;
4236 	fc->loop_down_time = fc->loop_down_limit+1;
4237 	wakeup(fc);
4238 	ISP_UNLOCK(isp);
4239 }
4240 
4241 static void
4242 isp_kthread(void *arg)
4243 {
4244 	struct isp_fc *fc = arg;
4245 	ispsoftc_t *isp = fc->isp;
4246 	int chan = fc - isp->isp_osinfo.pc.fc;
4247 	int slp = 0;
4248 
4249 	mtx_lock(&isp->isp_osinfo.lock);
4250 
4251 	for (;;) {
4252 		int lb, lim;
4253 
4254 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4255 		lb = isp_fc_runstate(isp, chan, 250000);
4256 
4257 		/*
4258 		 * Our action is different based upon whether we're supporting
4259 		 * Initiator mode or not. If we are, we might freeze the simq
4260 		 * when loop is down and set all sorts of different delays to
4261 		 * check again.
4262 		 *
4263 		 * If not, we simply just wait for loop to come up.
4264 		 */
4265 		if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4266 			/*
4267 			 * Increment loop down time by the last sleep interval
4268 			 */
4269 			fc->loop_down_time += slp;
4270 
4271 			if (lb < 0) {
4272 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4273 			} else {
4274 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4275 			}
4276 
4277 			/*
4278 			 * If we've never seen loop up and we've waited longer
4279 			 * than quickboot time, or we've seen loop up but we've
4280 			 * waited longer than loop_down_limit, give up and go
4281 			 * to sleep until loop comes up.
4282 			 */
4283 			if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4284 				lim = isp_quickboot_time;
4285 			} else {
4286 				lim = fc->loop_down_limit;
4287 			}
4288 			if (fc->loop_down_time >= lim) {
4289 				isp_freeze_loopdown(isp, chan, "loop limit hit");
4290 				slp = 0;
4291 			} else if (fc->loop_down_time < 10) {
4292 				slp = 1;
4293 			} else if (fc->loop_down_time < 30) {
4294 				slp = 5;
4295 			} else if (fc->loop_down_time < 60) {
4296 				slp = 10;
4297 			} else if (fc->loop_down_time < 120) {
4298 				slp = 20;
4299 			} else {
4300 				slp = 30;
4301 			}
4302 
4303 		} else if (lb) {
4304 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4305 			fc->loop_down_time += slp;
4306 			slp = 60;
4307 		} else {
4308 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4309 			fc->loop_down_time = 0;
4310 			slp = 0;
4311 		}
4312 
4313 
4314 		/*
4315 		 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4316 		 * now so that CAM can start sending us commands.
4317 		 *
4318 		 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4319 		 * or kill the commands, as appropriate.
4320 		 */
4321 
4322 		if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4323 			isp_unfreeze_loopdown(isp, chan);
4324 		}
4325 
4326 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4327 
4328 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4329 
4330 		/*
4331 		 * If slp is zero, we're waking up for the first time after
4332 		 * things have been okay. In this case, we set a deferral state
4333 		 * for all commands and delay hysteresis seconds before starting
4334 		 * the FC state evaluation. This gives the loop/fabric a chance
4335 		 * to settle.
4336 		 */
4337 		if (slp == 0 && fc->hysteresis) {
4338 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4339 			mtx_unlock(&isp->isp_osinfo.lock);
4340 			pause("ispt", fc->hysteresis * hz);
4341 			mtx_lock(&isp->isp_osinfo.lock);
4342 		}
4343 	}
4344 	mtx_unlock(&isp->isp_osinfo.lock);
4345 }
4346 
4347 static void
4348 isp_action(struct cam_sim *sim, union ccb *ccb)
4349 {
4350 	int bus, tgt, ts, error, lim;
4351 	ispsoftc_t *isp;
4352 	struct ccb_trans_settings *cts;
4353 
4354 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4355 
4356 	isp = (ispsoftc_t *)cam_sim_softc(sim);
4357 	mtx_assert(&isp->isp_lock, MA_OWNED);
4358 
4359 	if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4360 		isp_init(isp);
4361 		if (isp->isp_state != ISP_INITSTATE) {
4362 			/*
4363 			 * Lie. Say it was a selection timeout.
4364 			 */
4365 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4366 			xpt_freeze_devq(ccb->ccb_h.path, 1);
4367 			xpt_done(ccb);
4368 			return;
4369 		}
4370 		isp->isp_state = ISP_RUNSTATE;
4371 	}
4372 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4373 	ISP_PCMD(ccb) = NULL;
4374 
4375 	switch (ccb->ccb_h.func_code) {
4376 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
4377 		bus = XS_CHANNEL(ccb);
4378 		/*
4379 		 * Do a couple of preliminary checks...
4380 		 */
4381 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4382 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4383 				ccb->ccb_h.status = CAM_REQ_INVALID;
4384 				xpt_done(ccb);
4385 				break;
4386 			}
4387 		}
4388 #ifdef	DIAGNOSTIC
4389 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4390 			xpt_print(ccb->ccb_h.path, "invalid target\n");
4391 			ccb->ccb_h.status = CAM_PATH_INVALID;
4392 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4393 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
4394 			ccb->ccb_h.status = CAM_PATH_INVALID;
4395 		}
4396 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4397 			xpt_done(ccb);
4398 			break;
4399 		}
4400 #endif
4401 		ccb->csio.scsi_status = SCSI_STATUS_OK;
4402 		if (isp_get_pcmd(isp, ccb)) {
4403 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4404 			cam_freeze_devq(ccb->ccb_h.path);
4405 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4406 			xpt_done(ccb);
4407 			break;
4408 		}
4409 		error = isp_start((XS_T *) ccb);
4410 		switch (error) {
4411 		case CMD_QUEUED:
4412 			XS_CMD_S_CLEAR(ccb);
4413 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
4414 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4415 				break;
4416 			}
4417 			ts = ccb->ccb_h.timeout;
4418 			if (ts == CAM_TIME_DEFAULT) {
4419 				ts = 60*1000;
4420 			}
4421 			ts = isp_mstohz(ts);
4422 			XS_S_TACTIVE(ccb);
4423 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4424 			break;
4425 		case CMD_RQLATER:
4426 			/*
4427 			 * We get this result for FC devices if the loop state isn't ready yet
4428 			 * or if the device in question has gone zombie on us.
4429 			 *
4430 			 * If we've never seen Loop UP at all, we requeue this request and wait
4431 			 * for the initial loop up delay to expire.
4432 			 */
4433 			lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4434 			if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4435 				if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4436 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4437 				} else {
4438 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4439 				}
4440 				ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4441 				xpt_freeze_devq(ccb->ccb_h.path, 1);
4442 				isp_free_pcmd(isp, ccb);
4443 				xpt_done(ccb);
4444 				break;
4445 			}
4446 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4447 			cam_freeze_devq(ccb->ccb_h.path);
4448 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4449 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4450 			isp_free_pcmd(isp, ccb);
4451 			xpt_done(ccb);
4452 			break;
4453 		case CMD_EAGAIN:
4454 			isp_free_pcmd(isp, ccb);
4455 			cam_freeze_devq(ccb->ccb_h.path);
4456 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4457 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4458 			xpt_done(ccb);
4459 			break;
4460 		case CMD_COMPLETE:
4461 			isp_done((struct ccb_scsiio *) ccb);
4462 			break;
4463 		default:
4464 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4465 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4466 			isp_free_pcmd(isp, ccb);
4467 			xpt_done(ccb);
4468 		}
4469 		break;
4470 
4471 #ifdef	ISP_TARGET_MODE
4472 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
4473 		if (ccb->cel.enable) {
4474 			isp_enable_lun(isp, ccb);
4475 		} else {
4476 			isp_disable_lun(isp, ccb);
4477 		}
4478 		break;
4479 	case XPT_IMMED_NOTIFY:
4480 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
4481 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
4482 	{
4483 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4484 		if (tptr == NULL) {
4485 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4486 		}
4487 		if (tptr == NULL) {
4488 			const char *str;
4489 			uint32_t tag;
4490 
4491 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4492 				str = "XPT_IMMEDIATE_NOTIFY";
4493 				tag = ccb->cin1.seq_id;
4494 			} else {
4495 				tag = ccb->atio.tag_id;
4496 				str = "XPT_ACCEPT_TARGET_IO";
4497 			}
4498 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4499 			dump_tstates(isp, XS_CHANNEL(ccb));
4500 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4501 			break;
4502 		}
4503 		ccb->ccb_h.sim_priv.entries[0].field = 0;
4504 		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4505 		ccb->ccb_h.flags = 0;
4506 
4507 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4508 			if (ccb->atio.tag_id) {
4509 				atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4510 				if (atp) {
4511 					isp_put_atpd(isp, tptr, atp);
4512 				}
4513 			}
4514 			tptr->atio_count++;
4515 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4516 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4517 			    ccb->atio.tag_id, tptr->atio_count);
4518 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4519 			if (ccb->cin1.tag_id) {
4520 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4521 				if (ntp) {
4522 					isp_put_ntpd(isp, tptr, ntp);
4523 				}
4524 			}
4525 			tptr->inot_count++;
4526 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4527 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4528 			    ccb->cin1.seq_id, tptr->inot_count);
4529 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4530 			tptr->inot_count++;
4531 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4532 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4533 			    ccb->cin1.seq_id, tptr->inot_count);
4534 		}
4535 		rls_lun_statep(isp, tptr);
4536 		ccb->ccb_h.status = CAM_REQ_INPROG;
4537 		break;
4538 	}
4539 	case XPT_NOTIFY_ACK:
4540 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4541 		break;
4542 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
4543 	{
4544 		tstate_t *tptr;
4545 		inot_private_data_t *ntp;
4546 
4547 		/*
4548 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4549 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4550 		 */
4551 		/*
4552 		 * All the relevant path information is in the associated immediate notify
4553 		 */
4554 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] NOTIFY ACKNOWLEDGE for 0x%x seen\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
4555 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4556 		if (ntp == NULL) {
4557 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] XPT_NOTIFY_ACKNOWLEDGE of 0x%x cannot find ntp private data\n", __func__,
4558 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
4559 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4560 			xpt_done(ccb);
4561 			break;
4562 		}
4563 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4564 			rls_lun_statep(isp, tptr);
4565 			cam_freeze_devq(ccb->ccb_h.path);
4566 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4567 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4568 			break;
4569 		}
4570 		isp_put_ntpd(isp, tptr, ntp);
4571 		rls_lun_statep(isp, tptr);
4572 		ccb->ccb_h.status = CAM_REQ_CMP;
4573 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] calling xpt_done for tag 0x%x\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
4574 		xpt_done(ccb);
4575 		break;
4576 	}
4577 	case XPT_CONT_TARGET_IO:
4578 		isp_target_start_ctio(isp, ccb);
4579 		break;
4580 #endif
4581 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
4582 
4583 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4584 		tgt = ccb->ccb_h.target_id;
4585 		tgt |= (bus << 16);
4586 
4587 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4588 		if (error) {
4589 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4590 		} else {
4591 			ccb->ccb_h.status = CAM_REQ_CMP;
4592 		}
4593 		xpt_done(ccb);
4594 		break;
4595 	case XPT_ABORT:			/* Abort the specified CCB */
4596 	{
4597 		union ccb *accb = ccb->cab.abort_ccb;
4598 		switch (accb->ccb_h.func_code) {
4599 #ifdef	ISP_TARGET_MODE
4600 		case XPT_ACCEPT_TARGET_IO:
4601 			isp_target_mark_aborted(isp, ccb);
4602 			break;
4603 #endif
4604 		case XPT_SCSI_IO:
4605 			error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4606 			if (error) {
4607 				ccb->ccb_h.status = CAM_UA_ABORT;
4608 			} else {
4609 				ccb->ccb_h.status = CAM_REQ_CMP;
4610 			}
4611 			break;
4612 		default:
4613 			ccb->ccb_h.status = CAM_REQ_INVALID;
4614 			break;
4615 		}
4616 		/*
4617 		 * This is not a queued CCB, so the caller expects it to be
4618 		 * complete when control is returned.
4619 		 */
4620 		break;
4621 	}
4622 #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
4623 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
4624 		cts = &ccb->cts;
4625 		if (!IS_CURRENT_SETTINGS(cts)) {
4626 			ccb->ccb_h.status = CAM_REQ_INVALID;
4627 			xpt_done(ccb);
4628 			break;
4629 		}
4630 		tgt = cts->ccb_h.target_id;
4631 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4632 		if (IS_SCSI(isp)) {
4633 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4634 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4635 			sdparam *sdp = SDPARAM(isp, bus);
4636 			uint16_t *dptr;
4637 
4638 			if (spi->valid == 0 && scsi->valid == 0) {
4639 				ccb->ccb_h.status = CAM_REQ_CMP;
4640 				xpt_done(ccb);
4641 				break;
4642 			}
4643 
4644 			/*
4645 			 * We always update (internally) from goal_flags
4646 			 * so any request to change settings just gets
4647 			 * vectored to that location.
4648 			 */
4649 			dptr = &sdp->isp_devparam[tgt].goal_flags;
4650 
4651 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4652 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4653 					*dptr |= DPARM_DISC;
4654 				else
4655 					*dptr &= ~DPARM_DISC;
4656 			}
4657 
4658 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4659 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4660 					*dptr |= DPARM_TQING;
4661 				else
4662 					*dptr &= ~DPARM_TQING;
4663 			}
4664 
4665 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4666 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4667 					*dptr |= DPARM_WIDE;
4668 				else
4669 					*dptr &= ~DPARM_WIDE;
4670 			}
4671 
4672 			/*
4673 			 * XXX: FIX ME
4674 			 */
4675 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4676 				*dptr |= DPARM_SYNC;
4677 				/*
4678 				 * XXX: CHECK FOR LEGALITY
4679 				 */
4680 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4681 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4682 			} else {
4683 				*dptr &= ~DPARM_SYNC;
4684 			}
4685 			isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
4686 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4687 			sdp->isp_devparam[tgt].dev_update = 1;
4688 			sdp->update = 1;
4689 		}
4690 		ccb->ccb_h.status = CAM_REQ_CMP;
4691 		xpt_done(ccb);
4692 		break;
4693 	case XPT_GET_TRAN_SETTINGS:
4694 		cts = &ccb->cts;
4695 		tgt = cts->ccb_h.target_id;
4696 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4697 		if (IS_FC(isp)) {
4698 			fcparam *fcp = FCPARAM(isp, bus);
4699 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4700 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4701 			unsigned int hdlidx;
4702 
4703 			cts->protocol = PROTO_SCSI;
4704 			cts->protocol_version = SCSI_REV_2;
4705 			cts->transport = XPORT_FC;
4706 			cts->transport_version = 0;
4707 
4708 			scsi->valid = CTS_SCSI_VALID_TQ;
4709 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4710 			fc->valid = CTS_FC_VALID_SPEED;
4711 			fc->bitrate = 100000;
4712 			fc->bitrate *= fcp->isp_gbspeed;
4713 			hdlidx = fcp->isp_dev_map[tgt] - 1;
4714 			if (hdlidx < MAX_FC_TARG) {
4715 				fcportdb_t *lp = &fcp->portdb[hdlidx];
4716 				fc->wwnn = lp->node_wwn;
4717 				fc->wwpn = lp->port_wwn;
4718 				fc->port = lp->portid;
4719 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4720 			}
4721 		} else {
4722 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4723 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4724 			sdparam *sdp = SDPARAM(isp, bus);
4725 			uint16_t dval, pval, oval;
4726 
4727 			if (IS_CURRENT_SETTINGS(cts)) {
4728 				sdp->isp_devparam[tgt].dev_refresh = 1;
4729 				sdp->update = 1;
4730 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4731 				dval = sdp->isp_devparam[tgt].actv_flags;
4732 				oval = sdp->isp_devparam[tgt].actv_offset;
4733 				pval = sdp->isp_devparam[tgt].actv_period;
4734 			} else {
4735 				dval = sdp->isp_devparam[tgt].nvrm_flags;
4736 				oval = sdp->isp_devparam[tgt].nvrm_offset;
4737 				pval = sdp->isp_devparam[tgt].nvrm_period;
4738 			}
4739 
4740 			cts->protocol = PROTO_SCSI;
4741 			cts->protocol_version = SCSI_REV_2;
4742 			cts->transport = XPORT_SPI;
4743 			cts->transport_version = 2;
4744 
4745 			spi->valid = 0;
4746 			scsi->valid = 0;
4747 			spi->flags = 0;
4748 			scsi->flags = 0;
4749 			if (dval & DPARM_DISC) {
4750 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4751 			}
4752 			if ((dval & DPARM_SYNC) && oval && pval) {
4753 				spi->sync_offset = oval;
4754 				spi->sync_period = pval;
4755 			} else {
4756 				spi->sync_offset = 0;
4757 				spi->sync_period = 0;
4758 			}
4759 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4760 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4761 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4762 			if (dval & DPARM_WIDE) {
4763 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4764 			} else {
4765 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4766 			}
4767 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4768 				scsi->valid = CTS_SCSI_VALID_TQ;
4769 				if (dval & DPARM_TQING) {
4770 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4771 				}
4772 				spi->valid |= CTS_SPI_VALID_DISC;
4773 			}
4774 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4775 			    bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4776 		}
4777 		ccb->ccb_h.status = CAM_REQ_CMP;
4778 		xpt_done(ccb);
4779 		break;
4780 
4781 	case XPT_CALC_GEOMETRY:
4782 		cam_calc_geometry(&ccb->ccg, 1);
4783 		xpt_done(ccb);
4784 		break;
4785 
4786 	case XPT_RESET_BUS:		/* Reset the specified bus */
4787 		bus = cam_sim_bus(sim);
4788 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4789 		if (error) {
4790 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4791 			xpt_done(ccb);
4792 			break;
4793 		}
4794 		if (bootverbose) {
4795 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4796 		}
4797 		if (IS_FC(isp)) {
4798 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4799 		} else {
4800 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4801 		}
4802 		ccb->ccb_h.status = CAM_REQ_CMP;
4803 		xpt_done(ccb);
4804 		break;
4805 
4806 	case XPT_TERM_IO:		/* Terminate the I/O process */
4807 		ccb->ccb_h.status = CAM_REQ_INVALID;
4808 		xpt_done(ccb);
4809 		break;
4810 
4811 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
4812 	{
4813 		struct ccb_sim_knob *kp = &ccb->knob;
4814 		fcparam *fcp;
4815 
4816 		if (!IS_FC(isp)) {
4817 			ccb->ccb_h.status = CAM_REQ_INVALID;
4818 			xpt_done(ccb);
4819 			break;
4820 		}
4821 
4822 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4823 		fcp = FCPARAM(isp, bus);
4824 
4825 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4826 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4827 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4828 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4829 		}
4830 		ccb->ccb_h.status = CAM_REQ_CMP;
4831 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4832 			int rchange = 0;
4833 			int newrole = 0;
4834 
4835 			switch (kp->xport_specific.fc.role) {
4836 			case KNOB_ROLE_NONE:
4837 				if (fcp->role != ISP_ROLE_NONE) {
4838 					rchange = 1;
4839 					newrole = ISP_ROLE_NONE;
4840 				}
4841 				break;
4842 			case KNOB_ROLE_TARGET:
4843 				if (fcp->role != ISP_ROLE_TARGET) {
4844 					rchange = 1;
4845 					newrole = ISP_ROLE_TARGET;
4846 				}
4847 				break;
4848 			case KNOB_ROLE_INITIATOR:
4849 				if (fcp->role != ISP_ROLE_INITIATOR) {
4850 					rchange = 1;
4851 					newrole = ISP_ROLE_INITIATOR;
4852 				}
4853 				break;
4854 			case KNOB_ROLE_BOTH:
4855 #if 0
4856 				if (fcp->role != ISP_ROLE_BOTH) {
4857 					rchange = 1;
4858 					newrole = ISP_ROLE_BOTH;
4859 				}
4860 #else
4861 				/*
4862 				 * We don't really support dual role at present on FC cards.
4863 				 *
4864 				 * We should, but a bunch of things are currently broken,
4865 				 * so don't allow it.
4866 				 */
4867 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4868 				ccb->ccb_h.status = CAM_REQ_INVALID;
4869 #endif
4870 				break;
4871 			}
4872 			if (rchange) {
4873 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
4874 				if (isp_fc_change_role(isp, bus, newrole) != 0) {
4875 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4876 #ifdef	ISP_TARGET_MODE
4877 				} else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4878 					ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
4879 #endif
4880 				}
4881 			}
4882 		}
4883 		xpt_done(ccb);
4884 		break;
4885 	}
4886 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
4887 	{
4888 		struct ccb_sim_knob *kp = &ccb->knob;
4889 
4890 		if (IS_FC(isp)) {
4891 			fcparam *fcp;
4892 
4893 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4894 			fcp = FCPARAM(isp, bus);
4895 
4896 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4897 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4898 			switch (fcp->role) {
4899 			case ISP_ROLE_NONE:
4900 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4901 				break;
4902 			case ISP_ROLE_TARGET:
4903 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4904 				break;
4905 			case ISP_ROLE_INITIATOR:
4906 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4907 				break;
4908 			case ISP_ROLE_BOTH:
4909 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4910 				break;
4911 			}
4912 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4913 			ccb->ccb_h.status = CAM_REQ_CMP;
4914 		} else {
4915 			ccb->ccb_h.status = CAM_REQ_INVALID;
4916 		}
4917 		xpt_done(ccb);
4918 		break;
4919 	}
4920 	case XPT_PATH_INQ:		/* Path routing inquiry */
4921 	{
4922 		struct ccb_pathinq *cpi = &ccb->cpi;
4923 
4924 		cpi->version_num = 1;
4925 #ifdef	ISP_TARGET_MODE
4926 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4927 #else
4928 		cpi->target_sprt = 0;
4929 #endif
4930 		cpi->hba_eng_cnt = 0;
4931 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4932 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4933 		cpi->bus_id = cam_sim_bus(sim);
4934 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4935 		if (IS_FC(isp)) {
4936 			fcparam *fcp = FCPARAM(isp, bus);
4937 
4938 			cpi->hba_misc = PIM_NOBUSRESET;
4939 
4940 			/*
4941 			 * Because our loop ID can shift from time to time,
4942 			 * make our initiator ID out of range of our bus.
4943 			 */
4944 			cpi->initiator_id = cpi->max_target + 1;
4945 
4946 			/*
4947 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
4948 			 */
4949 			if (IS_25XX(isp)) {
4950 				cpi->base_transfer_speed = 8000000;
4951 			} else if (IS_24XX(isp)) {
4952 				cpi->base_transfer_speed = 4000000;
4953 			} else if (IS_23XX(isp)) {
4954 				cpi->base_transfer_speed = 2000000;
4955 			} else {
4956 				cpi->base_transfer_speed = 1000000;
4957 			}
4958 			cpi->hba_inquiry = PI_TAG_ABLE;
4959 			cpi->transport = XPORT_FC;
4960 			cpi->transport_version = 0;
4961 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4962 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4963 			cpi->xport_specific.fc.port = fcp->isp_portid;
4964 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4965 		} else {
4966 			sdparam *sdp = SDPARAM(isp, bus);
4967 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4968 			cpi->hba_misc = 0;
4969 			cpi->initiator_id = sdp->isp_initiator_id;
4970 			cpi->base_transfer_speed = 3300;
4971 			cpi->transport = XPORT_SPI;
4972 			cpi->transport_version = 2;
4973 		}
4974 		cpi->protocol = PROTO_SCSI;
4975 		cpi->protocol_version = SCSI_REV_2;
4976 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4977 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4978 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4979 		cpi->unit_number = cam_sim_unit(sim);
4980 		cpi->ccb_h.status = CAM_REQ_CMP;
4981 		xpt_done(ccb);
4982 		break;
4983 	}
4984 	default:
4985 		ccb->ccb_h.status = CAM_REQ_INVALID;
4986 		xpt_done(ccb);
4987 		break;
4988 	}
4989 }
4990 
4991 #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4992 
4993 void
4994 isp_done(XS_T *sccb)
4995 {
4996 	ispsoftc_t *isp = XS_ISP(sccb);
4997 	uint32_t status;
4998 
4999 	if (XS_NOERR(sccb))
5000 		XS_SETERR(sccb, CAM_REQ_CMP);
5001 
5002 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
5003 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
5004 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
5005 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
5006 		} else {
5007 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
5008 		}
5009 	}
5010 
5011 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5012 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
5013 	if (status != CAM_REQ_CMP) {
5014 		if (status != CAM_SEL_TIMEOUT)
5015 			isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
5016 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
5017 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
5018 			xpt_freeze_devq(sccb->ccb_h.path, 1);
5019 		}
5020 	}
5021 
5022 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5023 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
5024 	}
5025 
5026 	XS_CMD_S_DONE(sccb);
5027 	if (XS_TACTIVE_P(sccb))
5028 		callout_stop(&PISP_PCMD(sccb)->wdog);
5029 	XS_CMD_S_CLEAR(sccb);
5030 	isp_free_pcmd(isp, (union ccb *) sccb);
5031 	xpt_done((union ccb *) sccb);
5032 }
5033 
5034 void
5035 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
5036 {
5037 	int bus;
5038 	static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
5039 	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
5040 	char *msg = NULL;
5041 	target_id_t tgt;
5042 	fcportdb_t *lp;
5043 	struct isp_fc *fc;
5044 	struct cam_path *tmppath;
5045 	va_list ap;
5046 
5047 	switch (cmd) {
5048 	case ISPASYNC_NEW_TGT_PARAMS:
5049 	{
5050 		struct ccb_trans_settings_scsi *scsi;
5051 		struct ccb_trans_settings_spi *spi;
5052 		int flags, tgt;
5053 		sdparam *sdp;
5054 		struct ccb_trans_settings cts;
5055 
5056 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
5057 
5058 		va_start(ap, cmd);
5059 		bus = va_arg(ap, int);
5060 		tgt = va_arg(ap, int);
5061 		va_end(ap);
5062 		sdp = SDPARAM(isp, bus);
5063 
5064 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5065 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5066 			break;
5067 		}
5068 		flags = sdp->isp_devparam[tgt].actv_flags;
5069 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
5070 		cts.protocol = PROTO_SCSI;
5071 		cts.transport = XPORT_SPI;
5072 
5073 		scsi = &cts.proto_specific.scsi;
5074 		spi = &cts.xport_specific.spi;
5075 
5076 		if (flags & DPARM_TQING) {
5077 			scsi->valid |= CTS_SCSI_VALID_TQ;
5078 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5079 		}
5080 
5081 		if (flags & DPARM_DISC) {
5082 			spi->valid |= CTS_SPI_VALID_DISC;
5083 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5084 		}
5085 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5086 		if (flags & DPARM_WIDE) {
5087 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5088 		} else {
5089 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5090 		}
5091 		if (flags & DPARM_SYNC) {
5092 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5093 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5094 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5095 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5096 		}
5097 		isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags);
5098 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5099 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5100 		xpt_free_path(tmppath);
5101 		break;
5102 	}
5103 	case ISPASYNC_BUS_RESET:
5104 	{
5105 		va_start(ap, cmd);
5106 		bus = va_arg(ap, int);
5107 		va_end(ap);
5108 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5109 		if (IS_FC(isp)) {
5110 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5111 		} else {
5112 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5113 		}
5114 		break;
5115 	}
5116 	case ISPASYNC_LIP:
5117 		if (msg == NULL) {
5118 			msg = "LIP Received";
5119 		}
5120 		/* FALLTHROUGH */
5121 	case ISPASYNC_LOOP_RESET:
5122 		if (msg == NULL) {
5123 			msg = "LOOP Reset";
5124 		}
5125 		/* FALLTHROUGH */
5126 	case ISPASYNC_LOOP_DOWN:
5127 	{
5128 		if (msg == NULL) {
5129 			msg = "LOOP Down";
5130 		}
5131 		va_start(ap, cmd);
5132 		bus = va_arg(ap, int);
5133 		va_end(ap);
5134 
5135 		FCPARAM(isp, bus)->link_active = 0;
5136 
5137 		fc = ISP_FC_PC(isp, bus);
5138 		if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5139 			/*
5140 			 * We don't do any simq freezing if we are only in target mode
5141 			 */
5142 			if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5143 				if (fc->path) {
5144 					isp_freeze_loopdown(isp, bus, msg);
5145 				}
5146 				if (!callout_active(&fc->ldt)) {
5147 					callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5148 					isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5149 				}
5150 			}
5151 		}
5152 		isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5153 		break;
5154 	}
5155 	case ISPASYNC_LOOP_UP:
5156 		va_start(ap, cmd);
5157 		bus = va_arg(ap, int);
5158 		va_end(ap);
5159 		fc = ISP_FC_PC(isp, bus);
5160 		/*
5161 		 * Now we just note that Loop has come up. We don't
5162 		 * actually do anything because we're waiting for a
5163 		 * Change Notify before activating the FC cleanup
5164 		 * thread to look at the state of the loop again.
5165 		 */
5166 		FCPARAM(isp, bus)->link_active = 1;
5167 		fc->loop_dead = 0;
5168 		fc->loop_down_time = 0;
5169 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5170 		break;
5171 	case ISPASYNC_DEV_ARRIVED:
5172 		va_start(ap, cmd);
5173 		bus = va_arg(ap, int);
5174 		lp = va_arg(ap, fcportdb_t *);
5175 		va_end(ap);
5176 		fc = ISP_FC_PC(isp, bus);
5177 		lp->reserved = 0;
5178 		lp->gone_timer = 0;
5179 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5180 			int dbidx = lp - FCPARAM(isp, bus)->portdb;
5181 			int i;
5182 
5183 			for (i = 0; i < MAX_FC_TARG; i++) {
5184 				if (i >= FL_ID && i <= SNS_ID) {
5185 					continue;
5186 				}
5187 				if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5188 					break;
5189 				}
5190 			}
5191 			if (i < MAX_FC_TARG) {
5192 				FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5193 				lp->dev_map_idx = i + 1;
5194 			} else {
5195 				isp_prt(isp, ISP_LOGWARN, "out of target ids");
5196 				isp_dump_portdb(isp, bus);
5197 			}
5198 		}
5199 		if (lp->dev_map_idx) {
5200 			tgt = lp->dev_map_idx - 1;
5201 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5202 			isp_make_here(isp, bus, tgt);
5203 		} else {
5204 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5205 		}
5206 		break;
5207 	case ISPASYNC_DEV_CHANGED:
5208 		va_start(ap, cmd);
5209 		bus = va_arg(ap, int);
5210 		lp = va_arg(ap, fcportdb_t *);
5211 		va_end(ap);
5212 		fc = ISP_FC_PC(isp, bus);
5213 		lp->reserved = 0;
5214 		lp->gone_timer = 0;
5215 		if (isp_change_is_bad) {
5216 			lp->state = FC_PORTDB_STATE_NIL;
5217 			if (lp->dev_map_idx) {
5218 				tgt = lp->dev_map_idx - 1;
5219 				FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5220 				lp->dev_map_idx = 0;
5221 				isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5222 				isp_make_gone(isp, bus, tgt);
5223 			} else {
5224 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5225 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5226 			}
5227 		} else {
5228 			lp->portid = lp->new_portid;
5229 			lp->roles = lp->new_roles;
5230 			if (lp->dev_map_idx) {
5231 				int t = lp->dev_map_idx - 1;
5232 				FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5233 				tgt = lp->dev_map_idx - 1;
5234 				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5235 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5236 			} else {
5237 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5238 			}
5239 		}
5240 		break;
5241 	case ISPASYNC_DEV_STAYED:
5242 		va_start(ap, cmd);
5243 		bus = va_arg(ap, int);
5244 		lp = va_arg(ap, fcportdb_t *);
5245 		va_end(ap);
5246 		if (lp->dev_map_idx) {
5247 			tgt = lp->dev_map_idx - 1;
5248 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5249 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5250 		} else {
5251 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5252 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5253 		}
5254 		break;
5255 	case ISPASYNC_DEV_GONE:
5256 		va_start(ap, cmd);
5257 		bus = va_arg(ap, int);
5258 		lp = va_arg(ap, fcportdb_t *);
5259 		va_end(ap);
5260 		fc = ISP_FC_PC(isp, bus);
5261 		/*
5262 		 * If this has a virtual target and we haven't marked it
5263 		 * that we're going to have isp_gdt tell the OS it's gone,
5264 		 * set the isp_gdt timer running on it.
5265 		 *
5266 		 * If it isn't marked that isp_gdt is going to get rid of it,
5267 		 * announce that it's gone.
5268 		 *
5269 		 */
5270 		if (lp->dev_map_idx && lp->reserved == 0) {
5271 			lp->reserved = 1;
5272 			lp->state = FC_PORTDB_STATE_ZOMBIE;
5273 			lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5274 			if (fc->ready && !callout_active(&fc->gdt)) {
5275 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
5276 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
5277 			}
5278 			tgt = lp->dev_map_idx - 1;
5279 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5280 		} else if (lp->reserved == 0) {
5281 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5282 		}
5283 		break;
5284 	case ISPASYNC_CHANGE_NOTIFY:
5285 	{
5286 		char *msg;
5287 		int evt, nphdl, nlstate, reason;
5288 
5289 		va_start(ap, cmd);
5290 		bus = va_arg(ap, int);
5291 		evt = va_arg(ap, int);
5292 		if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5293 			nphdl = va_arg(ap, int);
5294 			nlstate = va_arg(ap, int);
5295 			reason = va_arg(ap, int);
5296 		} else {
5297 			nphdl = NIL_HANDLE;
5298 			nlstate = reason = 0;
5299 		}
5300 		va_end(ap);
5301 		fc = ISP_FC_PC(isp, bus);
5302 
5303 		if (evt == ISPASYNC_CHANGE_PDB) {
5304 			msg = "Chan %d Port Database Changed";
5305 		} else if (evt == ISPASYNC_CHANGE_SNS) {
5306 			msg = "Chan %d Name Server Database Changed";
5307 		} else {
5308 			msg = "Chan %d Other Change Notify";
5309 		}
5310 
5311 		/*
5312 		 * If the loop down timer is running, cancel it.
5313 		 */
5314 		if (fc->ready && callout_active(&fc->ldt)) {
5315 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5316 			callout_stop(&fc->ldt);
5317 		}
5318 		isp_prt(isp, ISP_LOGINFO, msg, bus);
5319 		if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5320 			isp_freeze_loopdown(isp, bus, msg);
5321 		}
5322 		wakeup(fc);
5323 		break;
5324 	}
5325 #ifdef	ISP_TARGET_MODE
5326 	case ISPASYNC_TARGET_NOTIFY:
5327 	{
5328 		isp_notify_t *notify;
5329 		va_start(ap, cmd);
5330 		notify = va_arg(ap, isp_notify_t *);
5331 		va_end(ap);
5332 		switch (notify->nt_ncode) {
5333 		case NT_ABORT_TASK:
5334 		case NT_ABORT_TASK_SET:
5335 		case NT_CLEAR_ACA:
5336 		case NT_CLEAR_TASK_SET:
5337 		case NT_LUN_RESET:
5338 		case NT_TARGET_RESET:
5339 			/*
5340 			 * These are task management functions.
5341 			 */
5342 			isp_handle_platform_target_tmf(isp, notify);
5343 			break;
5344 		case NT_BUS_RESET:
5345 		case NT_LIP_RESET:
5346 		case NT_LINK_UP:
5347 		case NT_LINK_DOWN:
5348 			/*
5349 			 * No action need be taken here.
5350 			 */
5351 			break;
5352 		case NT_HBA_RESET:
5353 			isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5354 			break;
5355 		case NT_LOGOUT:
5356 			/*
5357 			 * This is device arrival/departure notification
5358 			 */
5359 			isp_handle_platform_target_notify_ack(isp, notify);
5360 			break;
5361 		case NT_ARRIVED:
5362 		{
5363 			struct ac_contract ac;
5364 			struct ac_device_changed *fc;
5365 
5366 			ac.contract_number = AC_CONTRACT_DEV_CHG;
5367 			fc = (struct ac_device_changed *) ac.contract_data;
5368 			fc->wwpn = notify->nt_wwn;
5369 			fc->port = notify->nt_sid;
5370 			fc->target = notify->nt_nphdl;
5371 			fc->arrived = 1;
5372 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5373 			break;
5374 		}
5375 		case NT_DEPARTED:
5376 		{
5377 			struct ac_contract ac;
5378 			struct ac_device_changed *fc;
5379 
5380 			ac.contract_number = AC_CONTRACT_DEV_CHG;
5381 			fc = (struct ac_device_changed *) ac.contract_data;
5382 			fc->wwpn = notify->nt_wwn;
5383 			fc->port = notify->nt_sid;
5384 			fc->target = notify->nt_nphdl;
5385 			fc->arrived = 0;
5386 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5387 			break;
5388 		}
5389 		default:
5390 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5391 			isp_handle_platform_target_notify_ack(isp, notify);
5392 			break;
5393 		}
5394 		break;
5395 	}
5396 	case ISPASYNC_TARGET_ACTION:
5397 	{
5398 		isphdr_t *hp;
5399 
5400 		va_start(ap, cmd);
5401 		hp = va_arg(ap, isphdr_t *);
5402 		va_end(ap);
5403 		switch (hp->rqs_entry_type) {
5404 		default:
5405 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5406 			break;
5407 		case RQSTYPE_NOTIFY:
5408 			if (IS_SCSI(isp)) {
5409 				isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5410 			} else if (IS_24XX(isp)) {
5411 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5412 			} else {
5413 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5414 			}
5415 			break;
5416 		case RQSTYPE_ATIO:
5417 			if (IS_24XX(isp)) {
5418 				isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5419 			} else {
5420 				isp_handle_platform_atio(isp, (at_entry_t *) hp);
5421 			}
5422 			break;
5423 		case RQSTYPE_ATIO2:
5424 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5425 			break;
5426 		case RQSTYPE_CTIO7:
5427 		case RQSTYPE_CTIO3:
5428 		case RQSTYPE_CTIO2:
5429 		case RQSTYPE_CTIO:
5430 			isp_handle_platform_ctio(isp, hp);
5431 			break;
5432 		case RQSTYPE_ABTS_RCVD:
5433 		{
5434 			abts_t *abts = (abts_t *)hp;
5435 			isp_notify_t notify, *nt = &notify;
5436 			tstate_t *tptr;
5437 			fcportdb_t *lp;
5438 			uint16_t chan;
5439 			uint32_t sid, did;
5440 
5441 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5442 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5443 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
5444 
5445 			nt->nt_hba = isp;
5446 			nt->nt_did = did;
5447 			nt->nt_nphdl = abts->abts_nphdl;
5448 			nt->nt_sid = sid;
5449 			isp_find_chan_by_did(isp, did, &chan);
5450 			if (chan == ISP_NOCHAN) {
5451 				nt->nt_tgt = TGT_ANY;
5452 			} else {
5453 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5454 				if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5455 					nt->nt_wwn = lp->port_wwn;
5456 				} else {
5457 					nt->nt_wwn = INI_ANY;
5458 				}
5459 			}
5460 			/*
5461 			 * Try hard to find the lun for this command.
5462 			 */
5463 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5464 			if (tptr) {
5465 				nt->nt_lun = xpt_path_lun_id(tptr->owner);
5466 				rls_lun_statep(isp, tptr);
5467 			} else {
5468 				nt->nt_lun = LUN_ANY;
5469 			}
5470 			nt->nt_need_ack = 1;
5471 			nt->nt_tagval = abts->abts_rxid_task;
5472 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5473 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5474 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
5475 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5476 			} else {
5477 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
5478 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5479 			}
5480 			nt->nt_channel = chan;
5481 			nt->nt_ncode = NT_ABORT_TASK;
5482 			nt->nt_lreserved = hp;
5483 			isp_handle_platform_target_tmf(isp, nt);
5484 			break;
5485 		}
5486 		case RQSTYPE_ENABLE_LUN:
5487 		case RQSTYPE_MODIFY_LUN:
5488 			isp_ledone(isp, (lun_entry_t *) hp);
5489 			break;
5490 		}
5491 		break;
5492 	}
5493 #endif
5494 	case ISPASYNC_FW_CRASH:
5495 	{
5496 		uint16_t mbox1, mbox6;
5497 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
5498 		if (IS_DUALBUS(isp)) {
5499 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
5500 		} else {
5501 			mbox6 = 0;
5502 		}
5503 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5504 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5505 		isp->isp_osinfo.mbox_sleep_ok = 0;
5506 		isp_reinit(isp, 1);
5507 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
5508 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5509 		break;
5510 	}
5511 	default:
5512 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5513 		break;
5514 	}
5515 }
5516 
5517 
5518 /*
5519  * Locks are held before coming here.
5520  */
5521 void
5522 isp_uninit(ispsoftc_t *isp)
5523 {
5524 	if (IS_24XX(isp)) {
5525 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5526 	} else {
5527 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5528 	}
5529 	ISP_DISABLE_INTS(isp);
5530 }
5531 
5532 /*
5533  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5534  * up from our platform default (defww{p|n}n) and morph them based upon
5535  * channel.
5536  *
5537  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5538  * based upon channel.
5539  */
5540 
5541 uint64_t
5542 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5543 {
5544 	uint64_t seed;
5545 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
5546 
5547 	/*
5548 	 * If we're asking for a active WWN, the default overrides get
5549 	 * returned, otherwise the NVRAM value is picked.
5550 	 *
5551 	 * If we're asking for a default WWN, we just pick the default override.
5552 	 */
5553 	if (isactive) {
5554 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5555 		if (seed) {
5556 			return (seed);
5557 		}
5558 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5559 		if (seed) {
5560 			return (seed);
5561 		}
5562 		return (0x400000007F000009ull);
5563 	} else {
5564 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5565 	}
5566 
5567 
5568 	/*
5569 	 * For channel zero just return what we have. For either ACTIVE or
5570 	 * DEFAULT cases, we depend on default override of NVRAM values for
5571 	 * channel zero.
5572 	 */
5573 	if (chan == 0) {
5574 		return (seed);
5575 	}
5576 
5577 	/*
5578 	 * For other channels, we are doing one of three things:
5579 	 *
5580 	 * 1. If what we have now is non-zero, return it. Otherwise we morph
5581 	 * values from channel 0. 2. If we're here for a WWPN we synthesize
5582 	 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5583 	 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5584 	 */
5585 
5586 	if (seed) {
5587 		return (seed);
5588 	}
5589 	if (isactive) {
5590 		seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5591 	} else {
5592 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5593 	}
5594 
5595 	if (((seed >> 60) & 0xf) == 2) {
5596 		/*
5597 		 * The type 2 NAA fields for QLogic cards appear be laid out
5598 		 * thusly:
5599 		 *
5600 		 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5601 		 * port (1) or node (0) WWN distinguishor bit 48
5602 		 * physical port on dual-port chips (23XX/24XX)
5603 		 *
5604 		 * This is somewhat nutty, particularly since bit 48 is
5605 		 * irrelevant as they assign separate serial numbers to
5606 		 * different physical ports anyway.
5607 		 *
5608 		 * We'll stick our channel number plus one first into bits
5609 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
5610 		 * of channel which is comfortably more than our maximum
5611 		 * (126) now.
5612 		 */
5613 		seed &= ~0x0FF0000000000000ULL;
5614 		if (iswwnn == 0) {
5615 			seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5616 			seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5617 		}
5618 	} else {
5619 		seed = 0;
5620 	}
5621 	return (seed);
5622 }
5623 
5624 void
5625 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5626 {
5627 	int loc;
5628 	char lbuf[128];
5629 	va_list ap;
5630 
5631 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5632 		return;
5633 	}
5634 	sprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5635 	loc = strlen(lbuf);
5636 	va_start(ap, fmt);
5637 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
5638 	va_end(ap);
5639 	printf("%s\n", lbuf);
5640 }
5641 
5642 void
5643 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5644 {
5645 	va_list ap;
5646 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5647 		return;
5648 	}
5649 	xpt_print_path(xs->ccb_h.path);
5650 	va_start(ap, fmt);
5651 	vprintf(fmt, ap);
5652 	va_end(ap);
5653 	printf("\n");
5654 }
5655 
5656 uint64_t
5657 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5658 {
5659 	uint64_t elapsed;
5660 	struct timespec x = *b;
5661 	timespecsub(&x, a);
5662 	elapsed = GET_NANOSEC(&x);
5663 	if (elapsed == 0)
5664 		elapsed++;
5665 	return (elapsed);
5666 }
5667 
5668 int
5669 isp_mbox_acquire(ispsoftc_t *isp)
5670 {
5671 	if (isp->isp_osinfo.mboxbsy) {
5672 		return (1);
5673 	} else {
5674 		isp->isp_osinfo.mboxcmd_done = 0;
5675 		isp->isp_osinfo.mboxbsy = 1;
5676 		return (0);
5677 	}
5678 }
5679 
5680 void
5681 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5682 {
5683 	unsigned int usecs = mbp->timeout;
5684 	unsigned int max, olim, ilim;
5685 
5686 	if (usecs == 0) {
5687 		usecs = MBCMD_DEFAULT_TIMEOUT;
5688 	}
5689 	max = isp->isp_mbxwrk0 + 1;
5690 
5691 	if (isp->isp_osinfo.mbox_sleep_ok) {
5692 		unsigned int ms = (usecs + 999) / 1000;
5693 
5694 		isp->isp_osinfo.mbox_sleep_ok = 0;
5695 		isp->isp_osinfo.mbox_sleeping = 1;
5696 		for (olim = 0; olim < max; olim++) {
5697 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5698 			if (isp->isp_osinfo.mboxcmd_done) {
5699 				break;
5700 			}
5701 		}
5702 		isp->isp_osinfo.mbox_sleep_ok = 1;
5703 		isp->isp_osinfo.mbox_sleeping = 0;
5704 	} else {
5705 		for (olim = 0; olim < max; olim++) {
5706 			for (ilim = 0; ilim < usecs; ilim += 100) {
5707 				uint32_t isr;
5708 				uint16_t sema, mbox;
5709 				if (isp->isp_osinfo.mboxcmd_done) {
5710 					break;
5711 				}
5712 				if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5713 					isp_intr(isp, isr, sema, mbox);
5714 					if (isp->isp_osinfo.mboxcmd_done) {
5715 						break;
5716 					}
5717 				}
5718 				ISP_DELAY(100);
5719 			}
5720 			if (isp->isp_osinfo.mboxcmd_done) {
5721 				break;
5722 			}
5723 		}
5724 	}
5725 	if (isp->isp_osinfo.mboxcmd_done == 0) {
5726 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5727 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5728 		mbp->param[0] = MBOX_TIMEOUT;
5729 		isp->isp_osinfo.mboxcmd_done = 1;
5730 	}
5731 }
5732 
5733 void
5734 isp_mbox_notify_done(ispsoftc_t *isp)
5735 {
5736 	if (isp->isp_osinfo.mbox_sleeping) {
5737 		wakeup(&isp->isp_mbxworkp);
5738 	}
5739 	isp->isp_osinfo.mboxcmd_done = 1;
5740 }
5741 
5742 void
5743 isp_mbox_release(ispsoftc_t *isp)
5744 {
5745 	isp->isp_osinfo.mboxbsy = 0;
5746 }
5747 
5748 int
5749 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5750 {
5751 	int ret = 0;
5752 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5753 		ret = -1;
5754 	} else {
5755 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5756 	}
5757 	return (ret);
5758 }
5759 
5760 int
5761 isp_mstohz(int ms)
5762 {
5763 	int hz;
5764 	struct timeval t;
5765 	t.tv_sec = ms / 1000;
5766 	t.tv_usec = (ms % 1000) * 1000;
5767 	hz = tvtohz(&t);
5768 	if (hz < 0) {
5769 		hz = 0x7fffffff;
5770 	}
5771 	if (hz == 0) {
5772 		hz = 1;
5773 	}
5774 	return (hz);
5775 }
5776 
5777 void
5778 isp_platform_intr(void *arg)
5779 {
5780 	ispsoftc_t *isp = arg;
5781 	uint32_t isr;
5782 	uint16_t sema, mbox;
5783 
5784 	ISP_LOCK(isp);
5785 	isp->isp_intcnt++;
5786 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5787 		isp->isp_intbogus++;
5788 	} else {
5789 		isp_intr(isp, isr, sema, mbox);
5790 	}
5791 	ISP_UNLOCK(isp);
5792 }
5793 
5794 void
5795 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5796 {
5797 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5798 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5799 	} else {
5800 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5801 	}
5802 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5803 }
5804 
5805 void
5806 isp_timer(void *arg)
5807 {
5808 	ispsoftc_t *isp = arg;
5809 #ifdef	ISP_TARGET_MODE
5810 	isp_tmcmd_restart(isp);
5811 #endif
5812 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
5813 }
5814