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