xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 950c5aca4ac28e845fc8f5077f21451dbe9fabb4)
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_loop_down_limit = 60;	/* default loop down limit */
54 int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
55 int isp_gone_device_time = 30;	/* grace time before reporting device lost */
56 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
57 
58 static void isp_freeze_loopdown(ispsoftc_t *, int);
59 static void isp_loop_changed(ispsoftc_t *isp, int chan);
60 static d_ioctl_t ispioctl;
61 static void isp_intr_enable(void *);
62 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
63 static void isp_poll(struct cam_sim *);
64 static timeout_t isp_watchdog;
65 static timeout_t isp_gdt;
66 static task_fn_t isp_gdt_task;
67 static void isp_kthread(void *);
68 static void isp_action(struct cam_sim *, union ccb *);
69 static int isp_timer_count;
70 static void isp_timer(void *);
71 
72 static struct cdevsw isp_cdevsw = {
73 	.d_version =	D_VERSION,
74 	.d_ioctl =	ispioctl,
75 	.d_name =	"isp",
76 };
77 
78 static int
79 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
80 {
81 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
82 	int chan = arg2;
83 	int error, old, value;
84 
85 	value = FCPARAM(isp, chan)->role;
86 
87 	error = sysctl_handle_int(oidp, &value, 0, req);
88 	if ((error != 0) || (req->newptr == NULL))
89 		return (error);
90 
91 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
92 		return (EINVAL);
93 
94 	ISP_LOCK(isp);
95 	old = FCPARAM(isp, chan)->role;
96 
97 	/* We don't allow target mode switch from here. */
98 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
99 
100 	/* If nothing has changed -- we are done. */
101 	if (value == old) {
102 		ISP_UNLOCK(isp);
103 		return (0);
104 	}
105 
106 	/* Actually change the role. */
107 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
108 	ISP_UNLOCK(isp);
109 	return (error);
110 }
111 
112 static int
113 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
114 {
115 	struct ccb_setasync csa;
116 	struct cam_sim *sim;
117 	struct cam_path *path;
118 
119 	/*
120 	 * Construct our SIM entry.
121 	 */
122 	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);
123 
124 	if (sim == NULL) {
125 		return (ENOMEM);
126 	}
127 
128 	ISP_LOCK(isp);
129 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
130 		ISP_UNLOCK(isp);
131 		cam_sim_free(sim, FALSE);
132 		return (EIO);
133 	}
134 	ISP_UNLOCK(isp);
135 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
136 		ISP_LOCK(isp);
137 		xpt_bus_deregister(cam_sim_path(sim));
138 		ISP_UNLOCK(isp);
139 		cam_sim_free(sim, FALSE);
140 		return (ENXIO);
141 	}
142 	xpt_setup_ccb(&csa.ccb_h, path, 5);
143 	csa.ccb_h.func_code = XPT_SASYNC_CB;
144 	csa.event_enable = AC_LOST_DEVICE;
145 	csa.callback = isp_cam_async;
146 	csa.callback_arg = sim;
147 
148 	ISP_LOCK(isp);
149 	xpt_action((union ccb *)&csa);
150 	ISP_UNLOCK(isp);
151 
152 	if (IS_SCSI(isp)) {
153 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
154 		spi->sim = sim;
155 		spi->path = path;
156 #ifdef	ISP_TARGET_MODE
157 		TAILQ_INIT(&spi->waitq);
158 #endif
159 	} else {
160 		fcparam *fcp = FCPARAM(isp, chan);
161 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
162 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
163 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
164 		char name[16];
165 
166 		ISP_LOCK(isp);
167 		fc->sim = sim;
168 		fc->path = path;
169 		fc->isp = isp;
170 		fc->ready = 1;
171 
172 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
173 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
174 #ifdef	ISP_TARGET_MODE
175 		TAILQ_INIT(&fc->waitq);
176 #endif
177 		isp_loop_changed(isp, chan);
178 		ISP_UNLOCK(isp);
179 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
180 			xpt_free_path(fc->path);
181 			ISP_LOCK(isp);
182 			xpt_bus_deregister(cam_sim_path(fc->sim));
183 			ISP_UNLOCK(isp);
184 			cam_sim_free(fc->sim, FALSE);
185 			return (ENOMEM);
186 		}
187 		fc->num_threads += 1;
188 		if (chan > 0) {
189 			snprintf(name, sizeof(name), "chan%d", chan);
190 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
191 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
192 		}
193 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
194 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
195 		    "World Wide Node Name");
196 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
197 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
198 		    "World Wide Port Name");
199 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
200 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
201 		    "Loop Down Limit");
202 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
203 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
204 		    "Gone Device Time");
205 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
206 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
207 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
208 		    "Cause a Lost Frame on a Read");
209 #endif
210 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
211 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
212 		    isp_role_sysctl, "I", "Current role");
213 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
214 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
215 		    "Connection speed in gigabits");
216 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
217 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
218 		    "Link state");
219 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
220 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
221 		    "Firmware state");
222 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
223 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
224 		    "Loop state");
225 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
226 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
227 		    "Connection topology");
228 	}
229 	return (0);
230 }
231 
232 static void
233 isp_detach_chan(ispsoftc_t *isp, int chan)
234 {
235 	struct cam_sim *sim;
236 	struct cam_path *path;
237 	struct ccb_setasync csa;
238 	int *num_threads;
239 
240 	ISP_GET_PC(isp, chan, sim, sim);
241 	ISP_GET_PC(isp, chan, path, path);
242 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
243 
244 	xpt_setup_ccb(&csa.ccb_h, path, 5);
245 	csa.ccb_h.func_code = XPT_SASYNC_CB;
246 	csa.event_enable = 0;
247 	csa.callback = isp_cam_async;
248 	csa.callback_arg = sim;
249 	xpt_action((union ccb *)&csa);
250 	xpt_free_path(path);
251 	xpt_bus_deregister(cam_sim_path(sim));
252 	cam_sim_free(sim, FALSE);
253 
254 	/* Wait for the channel's spawned threads to exit. */
255 	wakeup(isp->isp_osinfo.pc.ptr);
256 	while (*num_threads != 0)
257 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
258 }
259 
260 int
261 isp_attach(ispsoftc_t *isp)
262 {
263 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
264 	int du = device_get_unit(isp->isp_dev);
265 	int chan;
266 
267 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
268 	isp->isp_osinfo.ehook.ich_arg = isp;
269 	/*
270 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
271 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
272 	 * which would be unwise to then set again later.
273 	 */
274 	isp->isp_osinfo.ehook_active = 1;
275 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
276 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
277 		return (-EIO);
278 	}
279 
280 	/*
281 	 * Create the device queue for our SIM(s).
282 	 */
283 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
284 	if (isp->isp_osinfo.devq == NULL) {
285 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
286 		return (EIO);
287 	}
288 
289 	for (chan = 0; chan < isp->isp_nchan; chan++) {
290 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
291 			goto unwind;
292 		}
293 	}
294 
295 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
296 	isp_timer_count = hz >> 2;
297 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
298 	isp->isp_osinfo.timer_active = 1;
299 
300 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
301 	if (isp->isp_osinfo.cdev) {
302 		isp->isp_osinfo.cdev->si_drv1 = isp;
303 	}
304 	return (0);
305 
306 unwind:
307 	while (--chan >= 0) {
308 		struct cam_sim *sim;
309 		struct cam_path *path;
310 
311 		ISP_GET_PC(isp, chan, sim, sim);
312 		ISP_GET_PC(isp, chan, path, path);
313 		xpt_free_path(path);
314 		ISP_LOCK(isp);
315 		xpt_bus_deregister(cam_sim_path(sim));
316 		ISP_UNLOCK(isp);
317 		cam_sim_free(sim, FALSE);
318 	}
319 	if (isp->isp_osinfo.ehook_active) {
320 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
321 		isp->isp_osinfo.ehook_active = 0;
322 	}
323 	if (isp->isp_osinfo.cdev) {
324 		destroy_dev(isp->isp_osinfo.cdev);
325 		isp->isp_osinfo.cdev = NULL;
326 	}
327 	cam_simq_free(isp->isp_osinfo.devq);
328 	isp->isp_osinfo.devq = NULL;
329 	return (-1);
330 }
331 
332 int
333 isp_detach(ispsoftc_t *isp)
334 {
335 	struct cam_sim *sim;
336 	int chan;
337 
338 	ISP_LOCK(isp);
339 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
340 		ISP_GET_PC(isp, chan, sim, sim);
341 		if (sim->refcount > 2) {
342 			ISP_UNLOCK(isp);
343 			return (EBUSY);
344 		}
345 	}
346 	/* Tell spawned threads that we're exiting. */
347 	isp->isp_osinfo.is_exiting = 1;
348 	if (isp->isp_osinfo.timer_active) {
349 		callout_stop(&isp->isp_osinfo.tmo);
350 		isp->isp_osinfo.timer_active = 0;
351 	}
352 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
353 		isp_detach_chan(isp, chan);
354 	ISP_UNLOCK(isp);
355 
356 	if (isp->isp_osinfo.cdev) {
357 		destroy_dev(isp->isp_osinfo.cdev);
358 		isp->isp_osinfo.cdev = NULL;
359 	}
360 	if (isp->isp_osinfo.ehook_active) {
361 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
362 		isp->isp_osinfo.ehook_active = 0;
363 	}
364 	if (isp->isp_osinfo.devq != NULL) {
365 		cam_simq_free(isp->isp_osinfo.devq);
366 		isp->isp_osinfo.devq = NULL;
367 	}
368 	return (0);
369 }
370 
371 static void
372 isp_freeze_loopdown(ispsoftc_t *isp, int chan)
373 {
374 	if (IS_FC(isp)) {
375 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
376 		if (fc->simqfrozen == 0) {
377 			isp_prt(isp, ISP_LOGDEBUG0,
378 			    "Chan %d Freeze simq (loopdown)", chan);
379 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
380 #if __FreeBSD_version >= 1000039
381 			xpt_hold_boot();
382 #endif
383 			xpt_freeze_simq(fc->sim, 1);
384 		} else {
385 			isp_prt(isp, ISP_LOGDEBUG0,
386 			    "Chan %d Mark simq frozen (loopdown)", chan);
387 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
388 		}
389 	}
390 }
391 
392 static void
393 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
394 {
395 	if (IS_FC(isp)) {
396 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
397 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
398 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
399 		if (wasfrozen && fc->simqfrozen == 0) {
400 			isp_prt(isp, ISP_LOGDEBUG0,
401 			    "Chan %d Release simq", chan);
402 			xpt_release_simq(fc->sim, 1);
403 #if __FreeBSD_version >= 1000039
404 			xpt_release_boot();
405 #endif
406 		}
407 	}
408 }
409 
410 
411 static int
412 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
413 {
414 	ispsoftc_t *isp;
415 	int nr, chan, retval = ENOTTY;
416 
417 	isp = dev->si_drv1;
418 
419 	switch (c) {
420 	case ISP_SDBLEV:
421 	{
422 		int olddblev = isp->isp_dblev;
423 		isp->isp_dblev = *(int *)addr;
424 		*(int *)addr = olddblev;
425 		retval = 0;
426 		break;
427 	}
428 	case ISP_GETROLE:
429 		chan = *(int *)addr;
430 		if (chan < 0 || chan >= isp->isp_nchan) {
431 			retval = -ENXIO;
432 			break;
433 		}
434 		if (IS_FC(isp)) {
435 			*(int *)addr = FCPARAM(isp, chan)->role;
436 		} else {
437 			*(int *)addr = ISP_ROLE_INITIATOR;
438 		}
439 		retval = 0;
440 		break;
441 	case ISP_SETROLE:
442 		if (IS_SCSI(isp))
443 			break;
444 		nr = *(int *)addr;
445 		chan = nr >> 8;
446 		if (chan < 0 || chan >= isp->isp_nchan) {
447 			retval = -ENXIO;
448 			break;
449 		}
450 		nr &= 0xff;
451 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
452 			retval = EINVAL;
453 			break;
454 		}
455 		ISP_LOCK(isp);
456 		*(int *)addr = FCPARAM(isp, chan)->role;
457 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
458 		ISP_UNLOCK(isp);
459 		retval = 0;
460 		break;
461 
462 	case ISP_RESETHBA:
463 		ISP_LOCK(isp);
464 		isp_reinit(isp, 0);
465 		ISP_UNLOCK(isp);
466 		retval = 0;
467 		break;
468 
469 	case ISP_RESCAN:
470 		if (IS_FC(isp)) {
471 			chan = *(int *)addr;
472 			if (chan < 0 || chan >= isp->isp_nchan) {
473 				retval = -ENXIO;
474 				break;
475 			}
476 			ISP_LOCK(isp);
477 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
478 				retval = EIO;
479 			} else {
480 				retval = 0;
481 			}
482 			ISP_UNLOCK(isp);
483 		}
484 		break;
485 
486 	case ISP_FC_LIP:
487 		if (IS_FC(isp)) {
488 			chan = *(int *)addr;
489 			if (chan < 0 || chan >= isp->isp_nchan) {
490 				retval = -ENXIO;
491 				break;
492 			}
493 			ISP_LOCK(isp);
494 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
495 				retval = EIO;
496 			} else {
497 				retval = 0;
498 			}
499 			ISP_UNLOCK(isp);
500 		}
501 		break;
502 	case ISP_FC_GETDINFO:
503 	{
504 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
505 		fcportdb_t *lp;
506 
507 		if (IS_SCSI(isp)) {
508 			break;
509 		}
510 		if (ifc->loopid >= MAX_FC_TARG) {
511 			retval = EINVAL;
512 			break;
513 		}
514 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
515 		if (lp->state != FC_PORTDB_STATE_NIL) {
516 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
517 			ifc->loopid = lp->handle;
518 			ifc->portid = lp->portid;
519 			ifc->node_wwn = lp->node_wwn;
520 			ifc->port_wwn = lp->port_wwn;
521 			retval = 0;
522 		} else {
523 			retval = ENODEV;
524 		}
525 		break;
526 	}
527 	case ISP_GET_STATS:
528 	{
529 		isp_stats_t *sp = (isp_stats_t *) addr;
530 
531 		ISP_MEMZERO(sp, sizeof (*sp));
532 		sp->isp_stat_version = ISP_STATS_VERSION;
533 		sp->isp_type = isp->isp_type;
534 		sp->isp_revision = isp->isp_revision;
535 		ISP_LOCK(isp);
536 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
537 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
538 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
539 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
540 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
541 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
542 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
543 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
544 		ISP_UNLOCK(isp);
545 		retval = 0;
546 		break;
547 	}
548 	case ISP_CLR_STATS:
549 		ISP_LOCK(isp);
550 		isp->isp_intcnt = 0;
551 		isp->isp_intbogus = 0;
552 		isp->isp_intmboxc = 0;
553 		isp->isp_intoasync = 0;
554 		isp->isp_rsltccmplt = 0;
555 		isp->isp_fphccmplt = 0;
556 		isp->isp_rscchiwater = 0;
557 		isp->isp_fpcchiwater = 0;
558 		ISP_UNLOCK(isp);
559 		retval = 0;
560 		break;
561 	case ISP_FC_GETHINFO:
562 	{
563 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
564 		int chan = hba->fc_channel;
565 
566 		if (chan < 0 || chan >= isp->isp_nchan) {
567 			retval = ENXIO;
568 			break;
569 		}
570 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
571 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
572 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
573 		hba->fc_nchannels = isp->isp_nchan;
574 		if (IS_FC(isp)) {
575 			hba->fc_nports = MAX_FC_TARG;
576 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
577 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
578 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
579 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
580 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
581 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
582 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
583 		} else {
584 			hba->fc_nports = MAX_TARGETS;
585 			hba->fc_speed = 0;
586 			hba->fc_topology = 0;
587 			hba->nvram_node_wwn = 0ull;
588 			hba->nvram_port_wwn = 0ull;
589 			hba->active_node_wwn = 0ull;
590 			hba->active_port_wwn = 0ull;
591 		}
592 		retval = 0;
593 		break;
594 	}
595 	case ISP_TSK_MGMT:
596 	{
597 		int needmarker;
598 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
599 		uint16_t nphdl;
600 		mbreg_t mbs;
601 
602 		if (IS_SCSI(isp)) {
603 			break;
604 		}
605 
606 		chan = fct->chan;
607 		if (chan < 0 || chan >= isp->isp_nchan) {
608 			retval = -ENXIO;
609 			break;
610 		}
611 
612 		needmarker = retval = 0;
613 		nphdl = fct->loopid;
614 		ISP_LOCK(isp);
615 		if (IS_24XX(isp)) {
616 			void *reqp;
617 			uint8_t resp[QENTRY_LEN];
618 			isp24xx_tmf_t tmf;
619 			isp24xx_statusreq_t sp;
620 			fcparam *fcp = FCPARAM(isp, chan);
621 			fcportdb_t *lp;
622 			int i;
623 
624 			for (i = 0; i < MAX_FC_TARG; i++) {
625 				lp = &fcp->portdb[i];
626 				if (lp->handle == nphdl) {
627 					break;
628 				}
629 			}
630 			if (i == MAX_FC_TARG) {
631 				retval = ENXIO;
632 				ISP_UNLOCK(isp);
633 				break;
634 			}
635 			ISP_MEMZERO(&tmf, sizeof(tmf));
636 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
637 			tmf.tmf_header.rqs_entry_count = 1;
638 			tmf.tmf_nphdl = lp->handle;
639 			tmf.tmf_delay = 2;
640 			tmf.tmf_timeout = 4;
641 			tmf.tmf_tidlo = lp->portid;
642 			tmf.tmf_tidhi = lp->portid >> 16;
643 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
644 			tmf.tmf_lun[1] = fct->lun & 0xff;
645 			if (fct->lun >= 256) {
646 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
647 			}
648 			switch (fct->action) {
649 			case IPT_CLEAR_ACA:
650 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
651 				break;
652 			case IPT_TARGET_RESET:
653 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
654 				needmarker = 1;
655 				break;
656 			case IPT_LUN_RESET:
657 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
658 				needmarker = 1;
659 				break;
660 			case IPT_CLEAR_TASK_SET:
661 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
662 				needmarker = 1;
663 				break;
664 			case IPT_ABORT_TASK_SET:
665 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
666 				needmarker = 1;
667 				break;
668 			default:
669 				retval = EINVAL;
670 				break;
671 			}
672 			if (retval) {
673 				ISP_UNLOCK(isp);
674 				break;
675 			}
676 
677 			/* Prepare space for response in memory */
678 			memset(resp, 0xff, sizeof(resp));
679 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
680 			    ISP_HANDLE_CTRL);
681 			if (tmf.tmf_handle == 0) {
682 				isp_prt(isp, ISP_LOGERR,
683 				    "%s: TMF of Chan %d out of handles",
684 				    __func__, chan);
685 				ISP_UNLOCK(isp);
686 				retval = ENOMEM;
687 				break;
688 			}
689 
690 			/* Send request and wait for response. */
691 			reqp = isp_getrqentry(isp);
692 			if (reqp == NULL) {
693 				isp_prt(isp, ISP_LOGERR,
694 				    "%s: TMF of Chan %d out of rqent",
695 				    __func__, chan);
696 				isp_destroy_handle(isp, tmf.tmf_handle);
697 				ISP_UNLOCK(isp);
698 				retval = EIO;
699 				break;
700 			}
701 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
702 			if (isp->isp_dblev & ISP_LOGDEBUG1)
703 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
704 			ISP_SYNC_REQUEST(isp);
705 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
706 				isp_prt(isp, ISP_LOGERR,
707 				    "%s: TMF of Chan %d timed out",
708 				    __func__, chan);
709 				isp_destroy_handle(isp, tmf.tmf_handle);
710 				ISP_UNLOCK(isp);
711 				retval = EIO;
712 				break;
713 			}
714 			if (isp->isp_dblev & ISP_LOGDEBUG1)
715 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
716 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
717 
718 			if (sp.req_completion_status != 0)
719 				retval = EIO;
720 			else if (needmarker)
721 				fcp->sendmarker = 1;
722 		} else {
723 			MBSINIT(&mbs, 0, MBLOGALL, 0);
724 			if (ISP_CAP_2KLOGIN(isp) == 0) {
725 				nphdl <<= 8;
726 			}
727 			switch (fct->action) {
728 			case IPT_CLEAR_ACA:
729 				mbs.param[0] = MBOX_CLEAR_ACA;
730 				mbs.param[1] = nphdl;
731 				mbs.param[2] = fct->lun;
732 				break;
733 			case IPT_TARGET_RESET:
734 				mbs.param[0] = MBOX_TARGET_RESET;
735 				mbs.param[1] = nphdl;
736 				needmarker = 1;
737 				break;
738 			case IPT_LUN_RESET:
739 				mbs.param[0] = MBOX_LUN_RESET;
740 				mbs.param[1] = nphdl;
741 				mbs.param[2] = fct->lun;
742 				needmarker = 1;
743 				break;
744 			case IPT_CLEAR_TASK_SET:
745 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
746 				mbs.param[1] = nphdl;
747 				mbs.param[2] = fct->lun;
748 				needmarker = 1;
749 				break;
750 			case IPT_ABORT_TASK_SET:
751 				mbs.param[0] = MBOX_ABORT_TASK_SET;
752 				mbs.param[1] = nphdl;
753 				mbs.param[2] = fct->lun;
754 				needmarker = 1;
755 				break;
756 			default:
757 				retval = EINVAL;
758 				break;
759 			}
760 			if (retval == 0) {
761 				if (needmarker) {
762 					FCPARAM(isp, chan)->sendmarker = 1;
763 				}
764 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
765 				if (retval) {
766 					retval = EIO;
767 				}
768 			}
769 		}
770 		ISP_UNLOCK(isp);
771 		break;
772 	}
773 	default:
774 		break;
775 	}
776 	return (retval);
777 }
778 
779 static void
780 isp_intr_enable(void *arg)
781 {
782 	int chan;
783 	ispsoftc_t *isp = arg;
784 	ISP_LOCK(isp);
785 	if (IS_FC(isp)) {
786 		for (chan = 0; chan < isp->isp_nchan; chan++) {
787 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
788 				ISP_ENABLE_INTS(isp);
789 				break;
790 			}
791 		}
792 	} else {
793 		ISP_ENABLE_INTS(isp);
794 	}
795 	isp->isp_osinfo.ehook_active = 0;
796 	ISP_UNLOCK(isp);
797 	/* Release our hook so that the boot can continue. */
798 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
799 }
800 
801 /*
802  * Local Inlines
803  */
804 
805 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
806 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
807 
808 static ISP_INLINE int
809 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
810 {
811 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
812 	if (ISP_PCMD(ccb) == NULL) {
813 		return (-1);
814 	}
815 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
816 	return (0);
817 }
818 
819 static ISP_INLINE void
820 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
821 {
822 	if (ISP_PCMD(ccb)) {
823 #ifdef	ISP_TARGET_MODE
824 		PISP_PCMD(ccb)->datalen = 0;
825 		PISP_PCMD(ccb)->totslen = 0;
826 		PISP_PCMD(ccb)->cumslen = 0;
827 		PISP_PCMD(ccb)->crn = 0;
828 #endif
829 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
830 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
831 		ISP_PCMD(ccb) = NULL;
832 	}
833 }
834 
835 /*
836  * Put the target mode functions here, because some are inlines
837  */
838 #ifdef	ISP_TARGET_MODE
839 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
840 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
841 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
842 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
843 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
844 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
845 static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
846 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
847 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
848 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
849 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
850 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
851 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
852 static void isp_enable_lun(ispsoftc_t *, union ccb *);
853 static void isp_disable_lun(ispsoftc_t *, union ccb *);
854 static timeout_t isp_refire_putback_atio;
855 static timeout_t isp_refire_notify_ack;
856 static void isp_complete_ctio(union ccb *);
857 static void isp_target_putback_atio(union ccb *);
858 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
859 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
860 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
861 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
862 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
863 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
864 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
865 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
866 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
867 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
868 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
869 
870 static ISP_INLINE int
871 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
872 {
873 	tstate_t *tptr;
874 	struct tslist *lhp;
875 
876 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
877 	SLIST_FOREACH(tptr, lhp, next) {
878 		if (tptr->ts_lun == lun) {
879 			return (1);
880 		}
881 	}
882 	return (0);
883 }
884 
885 static void
886 dump_tstates(ispsoftc_t *isp, int bus)
887 {
888 	int i, j;
889 	struct tslist *lhp;
890 	tstate_t *tptr = NULL;
891 
892 	if (bus >= isp->isp_nchan) {
893 		return;
894 	}
895 	for (i = 0; i < LUN_HASH_SIZE; i++) {
896 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
897 		j = 0;
898 		SLIST_FOREACH(tptr, lhp, next) {
899 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
900 			j++;
901 		}
902 	}
903 }
904 
905 static ISP_INLINE tstate_t *
906 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
907 {
908 	tstate_t *tptr = NULL;
909 	struct tslist *lhp;
910 
911 	if (bus < isp->isp_nchan) {
912 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
913 		SLIST_FOREACH(tptr, lhp, next) {
914 			if (tptr->ts_lun == lun) {
915 				tptr->hold++;
916 				return (tptr);
917 			}
918 		}
919 	}
920 	return (NULL);
921 }
922 
923 static ISP_INLINE tstate_t *
924 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
925 {
926 	tstate_t *tptr = NULL;
927 	atio_private_data_t *atp;
928 	struct tslist *lhp;
929 	int i;
930 
931 	if (bus < isp->isp_nchan && tagval != 0) {
932 		for (i = 0; i < LUN_HASH_SIZE; i++) {
933 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
934 			SLIST_FOREACH(tptr, lhp, next) {
935 				atp = isp_find_atpd(isp, tptr, tagval);
936 				if (atp) {
937 					tptr->hold++;
938 					return (tptr);
939 				}
940 			}
941 		}
942 	}
943 	return (NULL);
944 }
945 
946 static ISP_INLINE inot_private_data_t *
947 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
948 {
949 	inot_private_data_t *ntp;
950 	tstate_t *tptr;
951 	struct tslist *lhp;
952 	int bus, i;
953 
954 	for (bus = 0; bus < isp->isp_nchan; bus++) {
955 		for (i = 0; i < LUN_HASH_SIZE; i++) {
956 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
957 			SLIST_FOREACH(tptr, lhp, next) {
958 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
959 				if (ntp) {
960 					*rslt = tptr;
961 					tptr->hold++;
962 					return (ntp);
963 				}
964 			}
965 		}
966 	}
967 	return (NULL);
968 }
969 
970 static ISP_INLINE void
971 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
972 {
973 	KASSERT((tptr->hold), ("tptr not held"));
974 	tptr->hold--;
975 }
976 
977 static void
978 isp_tmcmd_restart(ispsoftc_t *isp)
979 {
980 	inot_private_data_t *ntp;
981 	inot_private_data_t *restart_queue;
982 	tstate_t *tptr;
983 	union ccb *ccb;
984 	struct tslist *lhp;
985 	struct isp_ccbq *waitq;
986 	int bus, i;
987 
988 	for (bus = 0; bus < isp->isp_nchan; bus++) {
989 		for (i = 0; i < LUN_HASH_SIZE; i++) {
990 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
991 			SLIST_FOREACH(tptr, lhp, next) {
992 				if ((restart_queue = tptr->restart_queue) != NULL)
993 					tptr->restart_queue = NULL;
994 				while (restart_queue) {
995 					ntp = restart_queue;
996 					restart_queue = ntp->rd.nt.nt_hba;
997 					if (IS_24XX(isp)) {
998 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
999 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
1000 					} else {
1001 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1002 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1003 					}
1004 					isp_put_ntpd(isp, tptr, ntp);
1005 					if (tptr->restart_queue && restart_queue != NULL) {
1006 						ntp = tptr->restart_queue;
1007 						tptr->restart_queue = restart_queue;
1008 						while (restart_queue->rd.nt.nt_hba) {
1009 							restart_queue = restart_queue->rd.nt.nt_hba;
1010 						}
1011 						restart_queue->rd.nt.nt_hba = ntp;
1012 						break;
1013 					}
1014 				}
1015 			}
1016 		}
1017 
1018 		/*
1019 		 * We only need to do this once per channel.
1020 		 */
1021 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
1022 		ccb = (union ccb *)TAILQ_FIRST(waitq);
1023 		if (ccb != NULL) {
1024 			TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
1025 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
1026 		}
1027 	}
1028 }
1029 
1030 static ISP_INLINE atio_private_data_t *
1031 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1032 {
1033 	atio_private_data_t *atp;
1034 
1035 	atp = LIST_FIRST(&tptr->atfree);
1036 	if (atp) {
1037 		LIST_REMOVE(atp, next);
1038 		atp->tag = tag;
1039 		LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
1040 	}
1041 	return (atp);
1042 }
1043 
1044 static ISP_INLINE atio_private_data_t *
1045 isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1046 {
1047 	atio_private_data_t *atp;
1048 
1049 	LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1050 		if (atp->tag == tag)
1051 			return (atp);
1052 	}
1053 	return (NULL);
1054 }
1055 
1056 static ISP_INLINE void
1057 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1058 {
1059 	if (atp->ests) {
1060 		isp_put_ecmd(isp, atp->ests);
1061 	}
1062 	LIST_REMOVE(atp, next);
1063 	memset(atp, 0, sizeof (*atp));
1064 	LIST_INSERT_HEAD(&tptr->atfree, atp, next);
1065 }
1066 
1067 static void
1068 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1069 {
1070 	atio_private_data_t *atp;
1071 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1072 
1073 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1074 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %x nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1075 		    atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1076 	}
1077 }
1078 
1079 
1080 static ISP_INLINE inot_private_data_t *
1081 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1082 {
1083 	inot_private_data_t *ntp;
1084 	ntp = tptr->ntfree;
1085 	if (ntp) {
1086 		tptr->ntfree = ntp->next;
1087 	}
1088 	return (ntp);
1089 }
1090 
1091 static ISP_INLINE inot_private_data_t *
1092 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1093 {
1094 	inot_private_data_t *ntp;
1095 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1096 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1097 			return (ntp);
1098 		}
1099 	}
1100 	return (NULL);
1101 }
1102 
1103 static ISP_INLINE void
1104 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1105 {
1106 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
1107 	ntp->next = tptr->ntfree;
1108 	tptr->ntfree = ntp;
1109 }
1110 
1111 static cam_status
1112 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1113 {
1114 	cam_status status;
1115 	lun_id_t lun;
1116 	struct tslist *lhp;
1117 	tstate_t *tptr;
1118 	int i;
1119 
1120 	lun = xpt_path_lun_id(path);
1121 	if (lun != CAM_LUN_WILDCARD) {
1122 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1123 			return (CAM_LUN_INVALID);
1124 		}
1125 	}
1126 	if (is_lun_enabled(isp, bus, lun)) {
1127 		return (CAM_LUN_ALRDY_ENA);
1128 	}
1129 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1130 	if (tptr == NULL) {
1131 		return (CAM_RESRC_UNAVAIL);
1132 	}
1133 	tptr->ts_lun = lun;
1134 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1135 	if (status != CAM_REQ_CMP) {
1136 		free(tptr, M_DEVBUF);
1137 		return (status);
1138 	}
1139 	SLIST_INIT(&tptr->atios);
1140 	SLIST_INIT(&tptr->inots);
1141 	LIST_INIT(&tptr->atfree);
1142 	for (i = ATPDPSIZE-1; i >= 0; i--)
1143 		LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1144 	for (i = 0; i < ATPDPHASHSIZE; i++)
1145 		LIST_INIT(&tptr->atused[i]);
1146 	for (i = 0; i < ATPDPSIZE-1; i++)
1147 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
1148 	tptr->ntfree = tptr->ntpool;
1149 	tptr->hold = 1;
1150 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
1151 	SLIST_INSERT_HEAD(lhp, tptr, next);
1152 	*rslt = tptr;
1153 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1154 	return (CAM_REQ_CMP);
1155 }
1156 
1157 static ISP_INLINE void
1158 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1159 {
1160 	union ccb *ccb;
1161 	struct tslist *lhp;
1162 
1163 	KASSERT((tptr->hold != 0), ("tptr is not held"));
1164 	KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1165 	do {
1166 		ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1167 		if (ccb) {
1168 			SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1169 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1170 			xpt_done(ccb);
1171 		}
1172 	} while (ccb);
1173 	do {
1174 		ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1175 		if (ccb) {
1176 			SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1177 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1178 			xpt_done(ccb);
1179 		}
1180 	} while (ccb);
1181 	ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1182 	SLIST_REMOVE(lhp, tptr, tstate, next);
1183 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1184 	xpt_free_path(tptr->owner);
1185 	free(tptr, M_DEVBUF);
1186 }
1187 
1188 static void
1189 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1190 {
1191 	tstate_t *tptr;
1192 	int bus;
1193 	target_id_t target;
1194 	lun_id_t lun;
1195 
1196 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
1197 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
1198 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1199 		xpt_done(ccb);
1200 		return;
1201 	}
1202 
1203 	/*
1204 	 * We only support either target and lun both wildcard
1205 	 * or target and lun both non-wildcard.
1206 	 */
1207 	bus = XS_CHANNEL(ccb);
1208 	target = ccb->ccb_h.target_id;
1209 	lun = ccb->ccb_h.target_lun;
1210 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1211 	    "enabling lun %jx\n", (uintmax_t)lun);
1212 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1213 		ccb->ccb_h.status = CAM_LUN_INVALID;
1214 		xpt_done(ccb);
1215 		return;
1216 	}
1217 
1218 	/* Create the state pointer. It should not already exist. */
1219 	tptr = get_lun_statep(isp, bus, lun);
1220 	if (tptr) {
1221 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1222 		xpt_done(ccb);
1223 		return;
1224 	}
1225 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1226 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
1227 		xpt_done(ccb);
1228 		return;
1229 	}
1230 
1231 	rls_lun_statep(isp, tptr);
1232 	ccb->ccb_h.status = CAM_REQ_CMP;
1233 	xpt_done(ccb);
1234 }
1235 
1236 static void
1237 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1238 {
1239 	tstate_t *tptr = NULL;
1240 	int bus;
1241 	target_id_t target;
1242 	lun_id_t lun;
1243 
1244 	bus = XS_CHANNEL(ccb);
1245 	target = ccb->ccb_h.target_id;
1246 	lun = ccb->ccb_h.target_lun;
1247 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1248 	    "disabling lun %jx\n", (uintmax_t)lun);
1249 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1250 		ccb->ccb_h.status = CAM_LUN_INVALID;
1251 		xpt_done(ccb);
1252 		return;
1253 	}
1254 
1255 	/* Find the state pointer. */
1256 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1257 		ccb->ccb_h.status = CAM_PATH_INVALID;
1258 		xpt_done(ccb);
1259 		return;
1260 	}
1261 
1262 	destroy_lun_state(isp, tptr);
1263 	ccb->ccb_h.status = CAM_REQ_CMP;
1264 	xpt_done(ccb);
1265 }
1266 
1267 static void
1268 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1269 {
1270 	int fctape, sendstatus, resid;
1271 	tstate_t *tptr;
1272 	fcparam *fcp;
1273 	atio_private_data_t *atp;
1274 	struct ccb_scsiio *cso;
1275 	struct isp_ccbq *waitq;
1276 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1277 	uint8_t local[QENTRY_LEN];
1278 
1279 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1280 	if (tptr == NULL) {
1281 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1282 		if (tptr == NULL) {
1283 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
1284 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1285 			xpt_done(ccb);
1286 			return;
1287 		}
1288 	}
1289 	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,
1290 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1291 
1292 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
1293 	switch (how) {
1294 	case FROM_CAM:
1295 		/*
1296 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
1297 		 */
1298 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
1299 		break;
1300 	case FROM_TIMER:
1301 	case FROM_SRR:
1302 	case FROM_CTIO_DONE:
1303 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1304 		break;
1305 	}
1306 
1307 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1308 		TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
1309 
1310 		cso = &ccb->csio;
1311 		xfrlen = cso->dxfer_len;
1312 		if (xfrlen == 0) {
1313 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1314 				ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1315 				ccb->ccb_h.status = CAM_REQ_INVALID;
1316 				xpt_done(ccb);
1317 				continue;
1318 			}
1319 		}
1320 
1321 		atp = isp_find_atpd(isp, tptr, cso->tag_id);
1322 		if (atp == NULL) {
1323 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1324 			isp_dump_atpd(isp, tptr);
1325 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1326 			xpt_done(ccb);
1327 			continue;
1328 		}
1329 
1330 		/*
1331 		 * Is this command a dead duck?
1332 		 */
1333 		if (atp->dead) {
1334 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1335 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1336 			xpt_done(ccb);
1337 			continue;
1338 		}
1339 
1340 		/*
1341 		 * Check to make sure we're still in target mode.
1342 		 */
1343 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1344 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1345 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1346 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1347 			xpt_done(ccb);
1348 			continue;
1349 		}
1350 
1351 		/*
1352 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1353 		 * could be split into two CTIOs to split data and status).
1354 		 */
1355 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1356 			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);
1357 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1358 			break;
1359 		}
1360 
1361 		/*
1362 		 * Does the initiator expect FC-Tape style responses?
1363 		 */
1364 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1365 			fctape = 1;
1366 		} else {
1367 			fctape = 0;
1368 		}
1369 
1370 		/*
1371 		 * If we already did the data xfer portion of a CTIO that sends data
1372 		 * and status, don't do it again and do the status portion now.
1373 		 */
1374 		if (atp->sendst) {
1375 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1376 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1377 			xfrlen = 0;	/* we already did the data transfer */
1378 			atp->sendst = 0;
1379 		}
1380 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1381 			sendstatus = 1;
1382 		} else {
1383 			sendstatus = 0;
1384 		}
1385 
1386 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1387 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1388 			/*
1389 			 * Sense length is not the entire sense data structure size. Periph
1390 			 * drivers don't seem to be setting sense_len to reflect the actual
1391 			 * size. We'll peek inside to get the right amount.
1392 			 */
1393 			sense_length = cso->sense_len;
1394 
1395 			/*
1396 			 * This 'cannot' happen
1397 			 */
1398 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1399 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1400 			}
1401 		} else {
1402 			sense_length = 0;
1403 		}
1404 
1405 		memset(local, 0, QENTRY_LEN);
1406 
1407 		/*
1408 		 * Check for overflow
1409 		 */
1410 		tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
1411 		if (tmp > atp->orig_datalen) {
1412 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
1413 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1414 			xpt_done(ccb);
1415 			continue;
1416 		}
1417 
1418 		if (IS_24XX(isp)) {
1419 			ct7_entry_t *cto = (ct7_entry_t *) local;
1420 
1421 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1422 			cto->ct_header.rqs_entry_count = 1;
1423 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1424 			ATPD_SET_SEQNO(cto, atp);
1425 			cto->ct_nphdl = atp->nphdl;
1426 			cto->ct_rxid = atp->tag;
1427 			cto->ct_iid_lo = atp->portid;
1428 			cto->ct_iid_hi = atp->portid >> 16;
1429 			cto->ct_oxid = atp->oxid;
1430 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1431 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1432 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1433 
1434 			/*
1435 			 * Mode 1, status, no data. Only possible when we are sending status, have
1436 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1437 			 *
1438 			 * Mode 2, status, no data. We have to use this in the case that
1439 			 * the sense data won't fit into a ct7_entry_t.
1440 			 *
1441 			 */
1442 			if (sendstatus && xfrlen == 0) {
1443 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1444 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1445 				if (sense_length <= MAXRESPLEN_24XX) {
1446 					if (resid < 0) {
1447 						cto->ct_resid = -resid;
1448 					} else if (resid > 0) {
1449 						cto->ct_resid = resid;
1450 					}
1451 					cto->ct_flags |= CT7_FLAG_MODE1;
1452 					cto->ct_scsi_status = cso->scsi_status;
1453 					if (resid < 0) {
1454 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1455 					} else if (resid > 0) {
1456 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1457 					}
1458 					if (fctape) {
1459 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1460 					}
1461 					if (sense_length) {
1462 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1463 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1464 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1465 					}
1466 				} else {
1467 					bus_addr_t addr;
1468 					char buf[XCMD_SIZE];
1469 					fcp_rsp_iu_t *rp;
1470 
1471 					if (atp->ests == NULL) {
1472 						atp->ests = isp_get_ecmd(isp);
1473 						if (atp->ests == NULL) {
1474 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1475 							break;
1476 						}
1477 					}
1478 					memset(buf, 0, sizeof (buf));
1479 					rp = (fcp_rsp_iu_t *)buf;
1480 					if (fctape) {
1481 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1482 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1483 					}
1484 					cto->ct_flags |= CT7_FLAG_MODE2;
1485 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1486 					if (resid < 0) {
1487 						rp->fcp_rsp_resid = -resid;
1488 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1489 					} else if (resid > 0) {
1490 						rp->fcp_rsp_resid = resid;
1491 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1492 					}
1493 					if (sense_length) {
1494 	        				rp->fcp_rsp_snslen = sense_length;
1495 						cto->ct_senselen = sense_length;
1496 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1497 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1498 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1499 					} else {
1500 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1501 					}
1502 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1503 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1504 					}
1505 					addr = isp->isp_osinfo.ecmd_dma;
1506 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1507 					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,
1508 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1509 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1510 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1511 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1512 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1513 				}
1514 				if (sense_length) {
1515 					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__,
1516 					    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,
1517 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1518 				} else {
1519 					isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1520 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1521 				}
1522 				atp->state = ATPD_STATE_LAST_CTIO;
1523 			}
1524 
1525 			/*
1526 			 * Mode 0 data transfers, *possibly* with status.
1527 			 */
1528 			if (xfrlen != 0) {
1529 				cto->ct_flags |= CT7_FLAG_MODE0;
1530 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1531 					cto->ct_flags |= CT7_DATA_IN;
1532 				} else {
1533 					cto->ct_flags |= CT7_DATA_OUT;
1534 				}
1535 
1536 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1537 				cto->rsp.m0.ct_xfrlen = xfrlen;
1538 
1539 #ifdef	DEBUG
1540 				if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1541 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1542 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1543 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1544 				}
1545 #endif
1546 				if (sendstatus) {
1547 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1548 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1549 						cto->ct_flags |= CT7_SENDSTATUS;
1550 						atp->state = ATPD_STATE_LAST_CTIO;
1551 						if (fctape) {
1552 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1553 						}
1554 					} else {
1555 						atp->sendst = 1;	/* send status later */
1556 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1557 						atp->state = ATPD_STATE_CTIO;
1558 					}
1559 				} else {
1560 					atp->state = ATPD_STATE_CTIO;
1561 				}
1562 				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__,
1563 				    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1564 			}
1565 		} else {
1566 			ct2_entry_t *cto = (ct2_entry_t *) local;
1567 
1568 			if (isp->isp_osinfo.sixtyfourbit)
1569 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1570 			else
1571 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1572 			cto->ct_header.rqs_entry_count = 1;
1573 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1574 			ATPD_SET_SEQNO(cto, atp);
1575 			if (ISP_CAP_2KLOGIN(isp)) {
1576 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1577 			} else {
1578 				cto->ct_iid = atp->nphdl;
1579 				if (ISP_CAP_SCCFW(isp) == 0) {
1580 					cto->ct_lun = ccb->ccb_h.target_lun;
1581 				}
1582 			}
1583 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1584 			cto->ct_rxid = cso->tag_id;
1585 
1586 			/*
1587 			 * Mode 1, status, no data. Only possible when we are sending status, have
1588 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1589 			 *
1590 			 * Mode 2, status, no data. We have to use this in the case the response
1591 			 * length won't fit into a ct2_entry_t.
1592 			 *
1593 			 * We'll fill out this structure with information as if this were a
1594 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1595 			 * needed based upon this.
1596 			 */
1597 			if (sendstatus && xfrlen == 0) {
1598 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1599 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1600 				if (sense_length <= MAXRESPLEN) {
1601 					if (resid < 0) {
1602 						cto->ct_resid = -resid;
1603 					} else if (resid > 0) {
1604 						cto->ct_resid = resid;
1605 					}
1606 					cto->ct_flags |= CT2_FLAG_MODE1;
1607 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1608 					if (resid < 0) {
1609 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1610 					} else if (resid > 0) {
1611 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1612 					}
1613 					if (fctape) {
1614 						cto->ct_flags |= CT2_CONFIRM;
1615 					}
1616 					if (sense_length) {
1617 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1618 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1619 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1620 					}
1621 				} else {
1622 					bus_addr_t addr;
1623 					char buf[XCMD_SIZE];
1624 					fcp_rsp_iu_t *rp;
1625 
1626 					if (atp->ests == NULL) {
1627 						atp->ests = isp_get_ecmd(isp);
1628 						if (atp->ests == NULL) {
1629 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1630 							break;
1631 						}
1632 					}
1633 					memset(buf, 0, sizeof (buf));
1634 					rp = (fcp_rsp_iu_t *)buf;
1635 					if (fctape) {
1636 						cto->ct_flags |= CT2_CONFIRM;
1637 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1638 					}
1639 					cto->ct_flags |= CT2_FLAG_MODE2;
1640 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1641 					if (resid < 0) {
1642 						rp->fcp_rsp_resid = -resid;
1643 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1644 					} else if (resid > 0) {
1645 						rp->fcp_rsp_resid = resid;
1646 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1647 					}
1648 					if (sense_length) {
1649 	        				rp->fcp_rsp_snslen = sense_length;
1650 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1651 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1652 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1653 					} else {
1654 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1655 					}
1656 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1657 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1658 					}
1659 					addr = isp->isp_osinfo.ecmd_dma;
1660 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1661 					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,
1662 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1663 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1664 					if (isp->isp_osinfo.sixtyfourbit) {
1665 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1666 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1667 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1668 					} else {
1669 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1670 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1671 					}
1672 				}
1673 				if (sense_length) {
1674 					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__,
1675 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
1676 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1677 				} else {
1678 					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,
1679 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1680 				}
1681 				atp->state = ATPD_STATE_LAST_CTIO;
1682 			}
1683 
1684 			if (xfrlen != 0) {
1685 				cto->ct_flags |= CT2_FLAG_MODE0;
1686 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1687 					cto->ct_flags |= CT2_DATA_IN;
1688 				} else {
1689 					cto->ct_flags |= CT2_DATA_OUT;
1690 				}
1691 
1692 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1693 				cto->rsp.m0.ct_xfrlen = xfrlen;
1694 
1695 				if (sendstatus) {
1696 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1697 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1698 						cto->ct_flags |= CT2_SENDSTATUS;
1699 						atp->state = ATPD_STATE_LAST_CTIO;
1700 						if (fctape) {
1701 							cto->ct_flags |= CT2_CONFIRM;
1702 						}
1703 					} else {
1704 						atp->sendst = 1;	/* send status later */
1705 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1706 						atp->state = ATPD_STATE_CTIO;
1707 					}
1708 				} else {
1709 					atp->state = ATPD_STATE_CTIO;
1710 				}
1711 			}
1712 			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,
1713 			    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);
1714 		}
1715 
1716 		if (isp_get_pcmd(isp, ccb)) {
1717 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1718 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1719 			break;
1720 		}
1721 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1722 		if (handle == 0) {
1723 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1724 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1725 			isp_free_pcmd(isp, ccb);
1726 			break;
1727 		}
1728 		atp->bytes_in_transit += xfrlen;
1729 		PISP_PCMD(ccb)->datalen = xfrlen;
1730 
1731 
1732 		/*
1733 		 * Call the dma setup routines for this entry (and any subsequent
1734 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1735 		 * new things to play with. As with isp_start's usage of DMA setup,
1736 		 * any swizzling is done in the machine dependent layer. Because
1737 		 * of this, we put the request onto the queue area first in native
1738 		 * format.
1739 		 */
1740 
1741 		if (IS_24XX(isp)) {
1742 			ct7_entry_t *cto = (ct7_entry_t *) local;
1743 			cto->ct_syshandle = handle;
1744 		} else {
1745 			ct2_entry_t *cto = (ct2_entry_t *) local;
1746 			cto->ct_syshandle = handle;
1747 		}
1748 
1749 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1750 		if (dmaresult != CMD_QUEUED) {
1751 			isp_destroy_handle(isp, handle);
1752 			isp_free_pcmd(isp, ccb);
1753 			if (dmaresult == CMD_EAGAIN) {
1754 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1755 				break;
1756 			}
1757 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1758 			xpt_done(ccb);
1759 			continue;
1760 		}
1761 		isp->isp_nactive++;
1762 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1763 		if (xfrlen) {
1764 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1765 		} else {
1766 			ccb->ccb_h.spriv_field0 = ~0;
1767 		}
1768 		atp->ctcnt++;
1769 		atp->seqno++;
1770 	}
1771 	rls_lun_statep(isp, tptr);
1772 }
1773 
1774 static void
1775 isp_refire_putback_atio(void *arg)
1776 {
1777 	union ccb *ccb = arg;
1778 
1779 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
1780 	isp_target_putback_atio(ccb);
1781 }
1782 
1783 static void
1784 isp_refire_notify_ack(void *arg)
1785 {
1786 	isp_tna_t *tp  = arg;
1787 	ispsoftc_t *isp = tp->isp;
1788 
1789 	ISP_ASSERT_LOCKED(isp);
1790 	if (isp_notify_ack(isp, tp->not)) {
1791 		callout_schedule(&tp->timer, 5);
1792 	} else {
1793 		free(tp, M_DEVBUF);
1794 	}
1795 }
1796 
1797 
1798 static void
1799 isp_target_putback_atio(union ccb *ccb)
1800 {
1801 	ispsoftc_t *isp;
1802 	struct ccb_scsiio *cso;
1803 	void *qe;
1804 	at2_entry_t local, *at = &local;
1805 
1806 	isp = XS_ISP(ccb);
1807 
1808 	qe = isp_getrqentry(isp);
1809 	if (qe == NULL) {
1810 		xpt_print(ccb->ccb_h.path,
1811 		    "%s: Request Queue Overflow\n", __func__);
1812 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1813 		    isp_refire_putback_atio, ccb);
1814 		return;
1815 	}
1816 	memset(qe, 0, QENTRY_LEN);
1817 	cso = &ccb->csio;
1818 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1819 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1820 	at->at_header.rqs_entry_count = 1;
1821 	if (ISP_CAP_SCCFW(isp)) {
1822 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1823 #if __FreeBSD_version < 1000700
1824 		if (at->at_scclun >= 256)
1825 			at->at_scclun |= 0x4000;
1826 #endif
1827 	} else {
1828 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1829 	}
1830 	at->at_status = CT_OK;
1831 	at->at_rxid = cso->tag_id;
1832 	at->at_iid = cso->ccb_h.target_id;
1833 	isp_put_atio2(isp, at, qe);
1834 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1835 	ISP_SYNC_REQUEST(isp);
1836 	isp_complete_ctio(ccb);
1837 }
1838 
1839 static void
1840 isp_complete_ctio(union ccb *ccb)
1841 {
1842 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1843 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1844 		xpt_done(ccb);
1845 	}
1846 }
1847 
1848 static void
1849 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1850 {
1851 	fcparam *fcp;
1852 	lun_id_t lun;
1853 	fcportdb_t *lp;
1854 	tstate_t *tptr;
1855 	struct ccb_accept_tio *atiop;
1856 	uint16_t nphdl;
1857 	atio_private_data_t *atp;
1858 	inot_private_data_t *ntp;
1859 
1860 	/*
1861 	 * The firmware status (except for the QLTM_SVALID bit)
1862 	 * indicates why this ATIO was sent to us.
1863 	 *
1864 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1865 	 */
1866 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1867 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1868 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1869 		return;
1870 	}
1871 
1872 	fcp = FCPARAM(isp, 0);
1873 	if (ISP_CAP_SCCFW(isp)) {
1874 		lun = aep->at_scclun;
1875 #if __FreeBSD_version < 1000700
1876 		lun &= 0x3fff;
1877 #endif
1878 	} else {
1879 		lun = aep->at_lun;
1880 	}
1881 	if (ISP_CAP_2KLOGIN(isp)) {
1882 		nphdl = ((at2e_entry_t *)aep)->at_iid;
1883 	} else {
1884 		nphdl = aep->at_iid;
1885 	}
1886 	tptr = get_lun_statep(isp, 0, lun);
1887 	if (tptr == NULL) {
1888 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1889 		if (tptr == NULL) {
1890 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1891 			if (lun == 0) {
1892 				isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1893 			} else {
1894 				isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1895 			}
1896 			return;
1897 		}
1898 	}
1899 
1900 	/*
1901 	 * Start any commands pending resources first.
1902 	 */
1903 	if (tptr->restart_queue) {
1904 		inot_private_data_t *restart_queue = tptr->restart_queue;
1905 		tptr->restart_queue = NULL;
1906 		while (restart_queue) {
1907 			ntp = restart_queue;
1908 			restart_queue = ntp->rd.nt.nt_hba;
1909 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1910 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1911 			isp_put_ntpd(isp, tptr, ntp);
1912 			/*
1913 			 * If a recursion caused the restart queue to start to fill again,
1914 			 * stop and splice the new list on top of the old list and restore
1915 			 * it and go to noresrc.
1916 			 */
1917 			if (tptr->restart_queue) {
1918 				ntp = tptr->restart_queue;
1919 				tptr->restart_queue = restart_queue;
1920 				while (restart_queue->rd.nt.nt_hba) {
1921 					restart_queue = restart_queue->rd.nt.nt_hba;
1922 				}
1923 				restart_queue->rd.nt.nt_hba = ntp;
1924 				goto noresrc;
1925 			}
1926 		}
1927 	}
1928 
1929 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1930 	if (atiop == NULL) {
1931 		goto noresrc;
1932 	}
1933 
1934 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
1935 	if (atp == NULL) {
1936 		goto noresrc;
1937 	}
1938 
1939 	atp->state = ATPD_STATE_ATIO;
1940 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1941 	tptr->atio_count--;
1942 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
1943 	atiop->ccb_h.target_id = fcp->isp_loopid;
1944 	atiop->ccb_h.target_lun = lun;
1945 
1946 	/*
1947 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1948 	 */
1949 	atiop->sense_len = 0;
1950 
1951 	/*
1952 	 * If we're not in the port database, add ourselves.
1953 	 */
1954 	if (IS_2100(isp))
1955 		atiop->init_id = nphdl;
1956 	else {
1957 		if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1958 		     lp->state == FC_PORTDB_STATE_ZOMBIE)) {
1959 			uint64_t wwpn =
1960 				(((uint64_t) aep->at_wwpn[0]) << 48) |
1961 				(((uint64_t) aep->at_wwpn[1]) << 32) |
1962 				(((uint64_t) aep->at_wwpn[2]) << 16) |
1963 				(((uint64_t) aep->at_wwpn[3]) <<  0);
1964 			isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
1965 			    nphdl, PORT_ANY, 0);
1966 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
1967 				fcp->isp_loopstate = LOOP_LTEST_DONE;
1968 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
1969 			    ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1970 			isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1971 		}
1972 		atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1973 	}
1974 	atiop->cdb_len = ATIO2_CDBLEN;
1975 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1976 	atiop->ccb_h.status = CAM_CDB_RECVD;
1977 	atiop->tag_id = atp->tag;
1978 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1979 	case ATIO2_TC_ATTR_SIMPLEQ:
1980 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1981 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1982 		break;
1983 	case ATIO2_TC_ATTR_HEADOFQ:
1984 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1985 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1986 		break;
1987 	case ATIO2_TC_ATTR_ORDERED:
1988 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1989 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1990 		break;
1991 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1992 	case ATIO2_TC_ATTR_UNTAGGED:
1993 	default:
1994 		atiop->tag_action = 0;
1995 		break;
1996 	}
1997 
1998 	atp->orig_datalen = aep->at_datalen;
1999 	atp->bytes_xfered = 0;
2000 	atp->lun = lun;
2001 	atp->nphdl = nphdl;
2002 	atp->sid = PORT_ANY;
2003 	atp->oxid = aep->at_oxid;
2004 	atp->cdb0 = aep->at_cdb[0];
2005 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2006 	atp->state = ATPD_STATE_CAM;
2007 	xpt_done((union ccb *)atiop);
2008 	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);
2009 	rls_lun_statep(isp, tptr);
2010 	return;
2011 noresrc:
2012 	ntp = isp_get_ntpd(isp, tptr);
2013 	if (ntp == NULL) {
2014 		rls_lun_statep(isp, tptr);
2015 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2016 		return;
2017 	}
2018 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2019 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2020 	tptr->restart_queue = ntp;
2021 	rls_lun_statep(isp, tptr);
2022 }
2023 
2024 static void
2025 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2026 {
2027 	int cdbxlen;
2028 	lun_id_t lun;
2029 	uint16_t chan, nphdl = NIL_HANDLE;
2030 	uint32_t did, sid;
2031 	fcportdb_t *lp;
2032 	tstate_t *tptr;
2033 	struct ccb_accept_tio *atiop;
2034 	atio_private_data_t *atp = NULL;
2035 	atio_private_data_t *oatp;
2036 	inot_private_data_t *ntp;
2037 
2038 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2039 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2040 #if __FreeBSD_version >= 1000700
2041 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
2042 #else
2043 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] & 0x3f << 8) |
2044 	    aep->at_cmnd.fcp_cmnd_lun[1];
2045 #endif
2046 
2047 	/*
2048 	 * Find the N-port handle, and Virtual Port Index for this command.
2049 	 *
2050 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2051 	 * We also, as a matter of course, need to know the WWN of the initiator too.
2052 	 */
2053 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
2054 		/*
2055 		 * Find the right channel based upon D_ID
2056 		 */
2057 		isp_find_chan_by_did(isp, did, &chan);
2058 
2059 		if (chan == ISP_NOCHAN) {
2060 			NANOTIME_T now;
2061 
2062 			/*
2063 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2064 			 * It's a bit tricky here as we need to stash this command *somewhere*.
2065 			 */
2066 			GET_NANOTIME(&now);
2067 			if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
2068 				isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2069 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2070 				return;
2071 			}
2072 			tptr = get_lun_statep(isp, 0, 0);
2073 			if (tptr == NULL) {
2074 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2075 				if (tptr == NULL) {
2076 					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);
2077 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2078 					return;
2079 				}
2080 			}
2081 			isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2082 			goto noresrc;
2083 		}
2084 		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);
2085 	} else {
2086 		chan = 0;
2087 	}
2088 
2089 	/*
2090 	 * Find the PDB entry for this initiator
2091 	 */
2092 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
2093 		/*
2094 		 * If we're not in the port database terminate the exchange.
2095 		 */
2096 		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",
2097 		    __func__, aep->at_rxid, did, chan, sid);
2098 		isp_dump_portdb(isp, chan);
2099 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2100 		return;
2101 	}
2102 	nphdl = lp->handle;
2103 
2104 	/*
2105 	 * Get the tstate pointer
2106 	 */
2107 	tptr = get_lun_statep(isp, chan, lun);
2108 	if (tptr == NULL) {
2109 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2110 		if (tptr == NULL) {
2111 			isp_prt(isp, ISP_LOGWARN,
2112 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
2113 			    __func__, aep->at_rxid, (uintmax_t)lun);
2114 			if (lun == 0) {
2115 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2116 			} else {
2117 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2118 			}
2119 			return;
2120 		}
2121 	}
2122 
2123 	/*
2124 	 * Start any commands pending resources first.
2125 	 */
2126 	if (tptr->restart_queue) {
2127 		inot_private_data_t *restart_queue = tptr->restart_queue;
2128 		tptr->restart_queue = NULL;
2129 		while (restart_queue) {
2130 			ntp = restart_queue;
2131 			restart_queue = ntp->rd.nt.nt_hba;
2132 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2133 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2134 			isp_put_ntpd(isp, tptr, ntp);
2135 			/*
2136 			 * If a recursion caused the restart queue to start to fill again,
2137 			 * stop and splice the new list on top of the old list and restore
2138 			 * it and go to noresrc.
2139 			 */
2140 			if (tptr->restart_queue) {
2141 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
2142 				if (restart_queue) {
2143 					ntp = tptr->restart_queue;
2144 					tptr->restart_queue = restart_queue;
2145 					while (restart_queue->rd.nt.nt_hba) {
2146 						restart_queue = restart_queue->rd.nt.nt_hba;
2147 					}
2148 					restart_queue->rd.nt.nt_hba = ntp;
2149 				}
2150 				goto noresrc;
2151 			}
2152 		}
2153 	}
2154 
2155 	/*
2156 	 * If the f/w is out of resources, just send a BUSY status back.
2157 	 */
2158 	if (aep->at_rxid == AT7_NORESRC_RXID) {
2159 		rls_lun_statep(isp, tptr);
2160 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2161 		return;
2162 	}
2163 
2164 	/*
2165 	 * If we're out of resources, just send a BUSY status back.
2166 	 */
2167 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2168 	if (atiop == NULL) {
2169 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2170 		goto noresrc;
2171 	}
2172 
2173 	oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
2174 	if (oatp) {
2175 		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",
2176 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2177 		/*
2178 		 * It's not a "no resource" condition- but we can treat it like one
2179 		 */
2180 		goto noresrc;
2181 	}
2182 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2183 	if (atp == NULL) {
2184 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2185 		goto noresrc;
2186 	}
2187 	atp->word3 = lp->prli_word3;
2188 	atp->state = ATPD_STATE_ATIO;
2189 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2190 	tptr->atio_count--;
2191 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2192 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
2193 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2194 	atiop->ccb_h.target_lun = lun;
2195 	atiop->sense_len = 0;
2196 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2197 	if (cdbxlen) {
2198 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2199 	}
2200 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2201 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2202 	atiop->cdb_len = cdbxlen;
2203 	atiop->ccb_h.status = CAM_CDB_RECVD;
2204 	atiop->tag_id = atp->tag;
2205 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2206 	case FCP_CMND_TASK_ATTR_SIMPLE:
2207 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2208 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2209 		break;
2210 	case FCP_CMND_TASK_ATTR_HEAD:
2211 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2212 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2213 		break;
2214 	case FCP_CMND_TASK_ATTR_ORDERED:
2215 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2216 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2217 		break;
2218 	default:
2219 		/* FALLTHROUGH */
2220 	case FCP_CMND_TASK_ATTR_ACA:
2221 	case FCP_CMND_TASK_ATTR_UNTAGGED:
2222 		atiop->tag_action = 0;
2223 		break;
2224 	}
2225 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2226 	atp->bytes_xfered = 0;
2227 	atp->lun = lun;
2228 	atp->nphdl = nphdl;
2229 	atp->portid = sid;
2230 	atp->oxid = aep->at_hdr.ox_id;
2231 	atp->rxid = aep->at_hdr.rx_id;
2232 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2233 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2234 	atp->state = ATPD_STATE_CAM;
2235 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
2236 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
2237 	xpt_done((union ccb *)atiop);
2238 	rls_lun_statep(isp, tptr);
2239 	return;
2240 noresrc:
2241 	if (atp) {
2242 		isp_put_atpd(isp, tptr, atp);
2243 	}
2244 	ntp = isp_get_ntpd(isp, tptr);
2245 	if (ntp == NULL) {
2246 		rls_lun_statep(isp, tptr);
2247 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2248 		return;
2249 	}
2250 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2251 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2252 	tptr->restart_queue = ntp;
2253 	rls_lun_statep(isp, tptr);
2254 }
2255 
2256 
2257 /*
2258  * Handle starting an SRR (sequence retransmit request)
2259  * We get here when we've gotten the immediate notify
2260  * and the return of all outstanding CTIOs for this
2261  * transaction.
2262  */
2263 static void
2264 isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2265 {
2266 	in_fcentry_24xx_t *inot;
2267 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2268 	union ccb *ccb;
2269 
2270 	inot = (in_fcentry_24xx_t *)atp->srr;
2271 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2272 	ccb = atp->srr_ccb;
2273 	atp->srr_ccb = NULL;
2274 	atp->nsrr++;
2275 	if (ccb == NULL) {
2276 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2277 		goto fail;
2278 	}
2279 
2280 	ccb_off = ccb->ccb_h.spriv_field0;
2281 	ccb_len = ccb->csio.dxfer_len;
2282         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2283 
2284 	switch (inot->in_srr_iu) {
2285 	case R_CTL_INFO_SOLICITED_DATA:
2286 		/*
2287 		 * We have to restart a FCP_DATA data out transaction
2288 		 */
2289 		atp->sendst = 0;
2290 		atp->bytes_xfered = srr_off;
2291 		if (ccb_len == 0) {
2292 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2293 			goto mdp;
2294 		}
2295  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
2296 			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);
2297 			goto mdp;
2298 		}
2299 		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);
2300 		break;
2301 	case R_CTL_INFO_COMMAND_STATUS:
2302 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2303 		atp->sendst = 1;
2304 		/*
2305 		 * We have to restart a FCP_RSP IU transaction
2306 		 */
2307 		break;
2308 	case R_CTL_INFO_DATA_DESCRIPTOR:
2309 		/*
2310 		 * We have to restart an FCP DATA in transaction
2311 		 */
2312 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2313 		goto fail;
2314 
2315 	default:
2316 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2317 		goto fail;
2318 	}
2319 
2320 	/*
2321 	 * We can't do anything until this is acked, so we might as well start it now.
2322 	 * We aren't going to do the usual asynchronous ack issue because we need
2323 	 * to make sure this gets on the wire first.
2324 	 */
2325 	if (isp_notify_ack(isp, inot)) {
2326 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2327 		goto fail;
2328 	}
2329 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2330 	return;
2331 fail:
2332 	inot->in_reserved = 1;
2333 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2334 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2335 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2336 	isp_complete_ctio(ccb);
2337 	return;
2338 mdp:
2339 	if (isp_notify_ack(isp, inot)) {
2340 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2341 		goto fail;
2342 	}
2343 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2344 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2345 	/*
2346 	 * This is not a strict interpretation of MDP, but it's close
2347 	 */
2348 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2349 	ccb->csio.msg_len = 7;
2350 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2351 	ccb->csio.msg_ptr[1] = 5;
2352 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2353 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2354 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2355 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2356 	ccb->csio.msg_ptr[6] = srr_off;
2357 	isp_complete_ctio(ccb);
2358 }
2359 
2360 
2361 static void
2362 isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2363 {
2364 	tstate_t *tptr;
2365 	in_fcentry_24xx_t *inot = inot_raw;
2366 	atio_private_data_t *atp;
2367 	uint32_t tag = inot->in_rxid;
2368 	uint32_t bus = inot->in_vpidx;
2369 
2370 	if (!IS_24XX(isp)) {
2371 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2372 		return;
2373 	}
2374 
2375 	tptr = get_lun_statep_from_tag(isp, bus, tag);
2376 	if (tptr == NULL) {
2377 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2378 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2379 		return;
2380 	}
2381 	atp = isp_find_atpd(isp, tptr, tag);
2382 	if (atp == NULL) {
2383 		rls_lun_statep(isp, tptr);
2384 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2385 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2386 		return;
2387 	}
2388 	atp->srr_notify_rcvd = 1;
2389 	memcpy(atp->srr, inot, sizeof (atp->srr));
2390 	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,
2391 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2392 	if (atp->srr_ccb)
2393 		isp_handle_srr_start(isp, tptr, atp);
2394 	rls_lun_statep(isp, tptr);
2395 }
2396 
2397 static void
2398 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2399 {
2400 	union ccb *ccb;
2401 	int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
2402 	tstate_t *tptr = NULL;
2403 	atio_private_data_t *atp = NULL;
2404 	int bus;
2405 	uint32_t handle, moved_data = 0, data_requested;
2406 
2407 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2408 	ccb = isp_find_xs(isp, handle);
2409 	if (ccb == NULL) {
2410 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2411 		return;
2412 	}
2413 	isp_destroy_handle(isp, handle);
2414 	data_requested = PISP_PCMD(ccb)->datalen;
2415 	isp_free_pcmd(isp, ccb);
2416 	if (isp->isp_nactive) {
2417 		isp->isp_nactive--;
2418 	}
2419 
2420 	bus = XS_CHANNEL(ccb);
2421 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2422 	if (tptr == NULL) {
2423 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2424 	}
2425 	if (tptr == NULL) {
2426 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2427 		return;
2428 	}
2429 
2430 	if (IS_24XX(isp)) {
2431 		atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2432 	} else {
2433 		atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2434 	}
2435 	if (atp == NULL) {
2436 		/*
2437 		 * XXX: isp_clear_commands() generates fake CTIO with zero
2438 		 * ct_rxid value, filling only ct_syshandle.  Workaround
2439 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2440 		 */
2441 		atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2442 	}
2443 	if (atp == NULL) {
2444 		rls_lun_statep(isp, tptr);
2445 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2446 		return;
2447 	}
2448 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2449 	atp->bytes_in_transit -= data_requested;
2450 	atp->ctcnt -= 1;
2451 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2452 
2453 	if (IS_24XX(isp)) {
2454 		ct7_entry_t *ct = arg;
2455 
2456 		if (ct->ct_nphdl == CT7_SRR) {
2457 			atp->srr_ccb = ccb;
2458 			if (atp->srr_notify_rcvd)
2459 				isp_handle_srr_start(isp, tptr, atp);
2460 			rls_lun_statep(isp, tptr);
2461 			return;
2462 		}
2463 		if (ct->ct_nphdl == CT_HBA_RESET) {
2464 			failure = CAM_UNREC_HBA_ERROR;
2465 		} else {
2466 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2467 			ok = (ct->ct_nphdl == CT7_OK);
2468 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2469 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2470 				resid = ct->ct_resid;
2471 				moved_data = data_requested - resid;
2472 			}
2473 		}
2474 		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),
2475 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2476 	} else {
2477 		ct2_entry_t *ct = arg;
2478 		if (ct->ct_status == CT_SRR) {
2479 			atp->srr_ccb = ccb;
2480 			if (atp->srr_notify_rcvd)
2481 				isp_handle_srr_start(isp, tptr, atp);
2482 			rls_lun_statep(isp, tptr);
2483 			isp_target_putback_atio(ccb);
2484 			return;
2485 		}
2486 		if (ct->ct_status == CT_HBA_RESET) {
2487 			failure = CAM_UNREC_HBA_ERROR;
2488 		} else {
2489 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2490 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2491 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2492 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2493 				resid = ct->ct_resid;
2494 				moved_data = data_requested - resid;
2495 			}
2496 		}
2497 		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),
2498 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2499 	}
2500 	if (ok) {
2501 		if (moved_data) {
2502 			atp->bytes_xfered += moved_data;
2503 			ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
2504 		}
2505 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2506 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2507 		}
2508 		ccb->ccb_h.status |= CAM_REQ_CMP;
2509 	} else {
2510 		notify_cam = 1;
2511 		if (failure == CAM_UNREC_HBA_ERROR)
2512 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2513 		else
2514 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2515 	}
2516 	atp->state = ATPD_STATE_PDON;
2517 	rls_lun_statep(isp, tptr);
2518 
2519 	/*
2520 	 * We never *not* notify CAM when there has been any error (ok == 0),
2521 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2522 	 */
2523 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
2524 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2525 	if (notify_cam == 0) {
2526 		if (atp->sendst) {
2527 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2528 		}
2529 		return;
2530 	}
2531 
2532 	/*
2533 	 * We're telling CAM we're done with this CTIO transaction.
2534 	 *
2535 	 * 24XX cards never need an ATIO put back.
2536 	 *
2537 	 * Other cards need one put back only on error.
2538 	 * In the latter case, a timeout will re-fire
2539 	 * and try again in case we didn't have
2540 	 * queue resources to do so at first. In any case,
2541 	 * once the putback is done we do the completion
2542 	 * call.
2543 	 */
2544 	if (ok || IS_24XX(isp)) {
2545 		isp_complete_ctio(ccb);
2546 	} else {
2547 		isp_target_putback_atio(ccb);
2548 	}
2549 }
2550 
2551 static void
2552 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2553 {
2554 	int needack = 1;
2555 	switch (inp->in_status) {
2556 	case IN_PORT_LOGOUT:
2557 		/*
2558 		 * XXX: Need to delete this initiator's WWN from the database
2559 		 * XXX: Need to send this LOGOUT upstream
2560 		 */
2561 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2562 		break;
2563 	case IN_PORT_CHANGED:
2564 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2565 		break;
2566 	case IN_GLOBAL_LOGO:
2567 		isp_del_all_wwn_entries(isp, 0);
2568 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2569 		break;
2570 	case IN_ABORT_TASK:
2571 	{
2572 		tstate_t *tptr;
2573 		uint16_t nphdl, lun;
2574 		uint32_t sid;
2575 		uint64_t wwn;
2576 		atio_private_data_t *atp;
2577 		fcportdb_t *lp;
2578 		struct ccb_immediate_notify *inot = NULL;
2579 
2580 		if (ISP_CAP_SCCFW(isp)) {
2581 			lun = inp->in_scclun;
2582 #if __FreeBSD_version < 1000700
2583 			lun &= 0x3fff;
2584 #endif
2585 		} else {
2586 			lun = inp->in_lun;
2587 		}
2588 		if (ISP_CAP_2KLOGIN(isp)) {
2589 			nphdl = ((in_fcentry_e_t *)inp)->in_iid;
2590 		} else {
2591 			nphdl = inp->in_iid;
2592 		}
2593 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
2594 			wwn = lp->port_wwn;
2595 			sid = lp->portid;
2596 		} else {
2597 			wwn = INI_ANY;
2598 			sid = PORT_ANY;
2599 		}
2600 		tptr = get_lun_statep(isp, 0, lun);
2601 		if (tptr == NULL) {
2602 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2603 			if (tptr == NULL) {
2604 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %x, but no tstate", lun);
2605 				return;
2606 			}
2607 		}
2608 		atp = isp_find_atpd(isp, tptr, inp->in_seqid);
2609 
2610 		if (atp) {
2611 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2612 			isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2613 			if (inot) {
2614 				tptr->inot_count--;
2615 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2616 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2617 			} else {
2618 				ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
2619 			}
2620 		} else {
2621 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2622 		}
2623 		if (inot) {
2624 			isp_notify_t tmp, *nt = &tmp;
2625 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
2626     			nt->nt_hba = isp;
2627 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2628 			nt->nt_wwn = wwn;
2629 			nt->nt_nphdl = nphdl;
2630 			nt->nt_sid = sid;
2631 			nt->nt_did = PORT_ANY;
2632     			nt->nt_lun = lun;
2633             		nt->nt_need_ack = 1;
2634     			nt->nt_channel = 0;
2635     			nt->nt_ncode = NT_ABORT_TASK;
2636     			nt->nt_lreserved = inot;
2637 			isp_handle_platform_target_tmf(isp, nt);
2638 			needack = 0;
2639 		}
2640 		rls_lun_statep(isp, tptr);
2641 		break;
2642 	}
2643 	default:
2644 		break;
2645 	}
2646 	if (needack) {
2647 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
2648 	}
2649 }
2650 
2651 static void
2652 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2653 {
2654 	uint16_t nphdl;
2655 	uint16_t prli_options = 0;
2656 	uint32_t portid;
2657 	fcportdb_t *lp;
2658 	char *msg = NULL;
2659 	uint8_t *ptr = (uint8_t *)inot;
2660 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
2661 
2662 	nphdl = inot->in_nphdl;
2663 	if (nphdl != NIL_HANDLE) {
2664 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2665 	} else {
2666 		portid = PORT_ANY;
2667 	}
2668 
2669 	switch (inot->in_status) {
2670 	case IN24XX_ELS_RCVD:
2671 	{
2672 		char buf[16];
2673 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2674 
2675 		/*
2676 		 * Note that we're just getting notification that an ELS was received
2677 		 * (possibly with some associated information sent upstream). This is
2678 		 * *not* the same as being given the ELS frame to accept or reject.
2679 		 */
2680 		switch (inot->in_status_subcode) {
2681 		case LOGO:
2682 			msg = "LOGO";
2683 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
2684 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
2685 			break;
2686 		case PRLO:
2687 			msg = "PRLO";
2688 			break;
2689 		case PLOGI:
2690 			msg = "PLOGI";
2691 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
2692 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
2693 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
2694 			    nphdl, portid, prli_options);
2695 			break;
2696 		case PRLI:
2697 			msg = "PRLI";
2698 			prli_options = inot->in_prli_options;
2699 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
2700 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
2701 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
2702 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
2703 			    nphdl, portid, prli_options);
2704 			break;
2705 		case PDISC:
2706 			msg = "PDISC";
2707 			break;
2708 		case ADISC:
2709 			msg = "ADISC";
2710 			break;
2711 		default:
2712 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2713 			msg = buf;
2714 			break;
2715 		}
2716 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2717 			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);
2718 			break;
2719 		}
2720 		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,
2721 		    inot->in_rxid, inot->in_oxid);
2722 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2723 		break;
2724 	}
2725 
2726 	case IN24XX_PORT_LOGOUT:
2727 		msg = "PORT LOGOUT";
2728 		if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2729 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2730 		}
2731 		/* FALLTHROUGH */
2732 	case IN24XX_PORT_CHANGED:
2733 		if (msg == NULL)
2734 			msg = "PORT CHANGED";
2735 		/* FALLTHROUGH */
2736 	case IN24XX_LIP_RESET:
2737 		if (msg == NULL)
2738 			msg = "LIP RESET";
2739 		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), msg, inot->in_status_subcode, nphdl);
2740 
2741 		/*
2742 		 * All subcodes here are irrelevant. What is relevant
2743 		 * is that we need to terminate all active commands from
2744 		 * this initiator (known by N-port handle).
2745 		 */
2746 		/* XXX IMPLEMENT XXX */
2747 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2748 		break;
2749 
2750 	case IN24XX_SRR_RCVD:
2751 #ifdef	ISP_TARGET_MODE
2752 		isp_handle_srr_notify(isp, inot);
2753 		break;
2754 #else
2755 		if (msg == NULL)
2756 			msg = "SRR RCVD";
2757 		/* FALLTHROUGH */
2758 #endif
2759 	case IN24XX_LINK_RESET:
2760 		if (msg == NULL)
2761 			msg = "LINK RESET";
2762 	case IN24XX_LINK_FAILED:
2763 		if (msg == NULL)
2764 			msg = "LINK FAILED";
2765 	default:
2766 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2767 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2768 		break;
2769 	}
2770 }
2771 
2772 static int
2773 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
2774 {
2775 
2776 	if (isp->isp_state != ISP_RUNSTATE) {
2777 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2778 		return (0);
2779 	}
2780 
2781 	/*
2782 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2783 	 */
2784 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2785 		ct7_entry_t local, *cto = &local;
2786 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2787 		fcportdb_t *lp;
2788 		uint32_t sid;
2789 		uint16_t nphdl;
2790 
2791 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2792 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
2793 			nphdl = lp->handle;
2794 		} else {
2795 			nphdl = NIL_HANDLE;
2796 		}
2797 		ISP_MEMZERO(&local, sizeof (local));
2798 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2799 		cto->ct_header.rqs_entry_count = 1;
2800 		cto->ct_nphdl = nphdl;
2801 		cto->ct_rxid = aep->at_rxid;
2802 		cto->ct_vpidx = mp->nt_channel;
2803 		cto->ct_iid_lo = sid;
2804 		cto->ct_iid_hi = sid >> 16;
2805 		cto->ct_oxid = aep->at_hdr.ox_id;
2806 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2807 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2808 		if (rsp != 0) {
2809 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
2810 			cto->rsp.m1.ct_resplen = 4;
2811 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
2812 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
2813 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
2814 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
2815 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
2816 		}
2817 		return (isp_target_put_entry(isp, &local));
2818 	}
2819 
2820 	/*
2821 	 * This case is for a responding to an ABTS frame
2822 	 */
2823 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2824 
2825 		/*
2826 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2827 		 */
2828 		if (mp->nt_need_ack) {
2829 			uint8_t storage[QENTRY_LEN];
2830 			ct7_entry_t *cto = (ct7_entry_t *) storage;
2831 			abts_t *abts = (abts_t *)mp->nt_lreserved;
2832 
2833 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2834 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2835 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2836 			cto->ct_header.rqs_entry_count = 1;
2837 			cto->ct_nphdl = mp->nt_nphdl;
2838 			cto->ct_rxid = abts->abts_rxid_task;
2839 			cto->ct_iid_lo = mp->nt_sid;
2840 			cto->ct_iid_hi = mp->nt_sid >> 16;
2841 			cto->ct_oxid = abts->abts_ox_id;
2842 			cto->ct_vpidx = mp->nt_channel;
2843 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2844 			if (isp_target_put_entry(isp, cto)) {
2845 				return (ENOMEM);
2846 			}
2847 			mp->nt_need_ack = 0;
2848 		}
2849 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2850 			return (ENOMEM);
2851 		} else {
2852 			return (0);
2853 		}
2854 	}
2855 
2856 	/*
2857 	 * Handle logout cases here
2858 	 */
2859 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2860 		isp_del_all_wwn_entries(isp, mp->nt_channel);
2861 	}
2862 
2863 	if (mp->nt_ncode == NT_LOGOUT) {
2864 		if (!IS_2100(isp) && IS_FC(isp)) {
2865 			isp_del_wwn_entries(isp, mp);
2866 		}
2867 	}
2868 
2869 	/*
2870 	 * General purpose acknowledgement
2871 	 */
2872 	if (mp->nt_need_ack) {
2873 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2874 		/*
2875 		 * Don't need to use the guaranteed send because the caller can retry
2876 		 */
2877 		return (isp_notify_ack(isp, mp->nt_lreserved));
2878 	}
2879 	return (0);
2880 }
2881 
2882 /*
2883  * Handle task management functions.
2884  *
2885  * We show up here with a notify structure filled out.
2886  *
2887  * The nt_lreserved tag points to the original queue entry
2888  */
2889 static void
2890 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2891 {
2892 	tstate_t *tptr;
2893 	fcportdb_t *lp;
2894 	struct ccb_immediate_notify *inot;
2895 	inot_private_data_t *ntp = NULL;
2896 	lun_id_t lun;
2897 
2898 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2899 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2900 	/*
2901 	 * NB: This assignment is necessary because of tricky type conversion.
2902 	 * XXX: This is tricky and I need to check this. If the lun isn't known
2903 	 * XXX: for the task management function, it does not of necessity follow
2904 	 * XXX: that it should go up stream to the wildcard listener.
2905 	 */
2906 	if (notify->nt_lun == LUN_ANY) {
2907 		lun = CAM_LUN_WILDCARD;
2908 	} else {
2909 		lun = notify->nt_lun;
2910 	}
2911 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
2912 	if (tptr == NULL) {
2913 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2914 		if (tptr == NULL) {
2915 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2916 			goto bad;
2917 		}
2918 	}
2919 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2920 	if (inot == NULL) {
2921 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2922 		goto bad;
2923 	}
2924 
2925 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2926 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
2927 		inot->initiator_id = CAM_TARGET_WILDCARD;
2928 	} else {
2929 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
2930 	}
2931 	inot->seq_id = notify->nt_tagval;
2932 	inot->tag_id = notify->nt_tagval >> 32;
2933 
2934 	switch (notify->nt_ncode) {
2935 	case NT_ABORT_TASK:
2936 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2937 		inot->arg = MSG_ABORT_TASK;
2938 		break;
2939 	case NT_ABORT_TASK_SET:
2940 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2941 		inot->arg = MSG_ABORT_TASK_SET;
2942 		break;
2943 	case NT_CLEAR_ACA:
2944 		inot->arg = MSG_CLEAR_ACA;
2945 		break;
2946 	case NT_CLEAR_TASK_SET:
2947 		inot->arg = MSG_CLEAR_TASK_SET;
2948 		break;
2949 	case NT_LUN_RESET:
2950 		inot->arg = MSG_LOGICAL_UNIT_RESET;
2951 		break;
2952 	case NT_TARGET_RESET:
2953 		inot->arg = MSG_TARGET_RESET;
2954 		break;
2955 	case NT_QUERY_TASK_SET:
2956 		inot->arg = MSG_QUERY_TASK_SET;
2957 		break;
2958 	case NT_QUERY_ASYNC_EVENT:
2959 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2960 		break;
2961 	default:
2962 		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);
2963 		goto bad;
2964 	}
2965 
2966 	ntp = isp_get_ntpd(isp, tptr);
2967 	if (ntp == NULL) {
2968 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2969 		goto bad;
2970 	}
2971 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2972 	if (notify->nt_lreserved) {
2973 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2974 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2975 	}
2976 	ntp->rd.seq_id = notify->nt_tagval;
2977 	ntp->rd.tag_id = notify->nt_tagval >> 32;
2978 
2979 	tptr->inot_count--;
2980 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2981 	rls_lun_statep(isp, tptr);
2982 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2983 	inot->ccb_h.status = CAM_MESSAGE_RECV;
2984 	xpt_done((union ccb *)inot);
2985 	return;
2986 bad:
2987 	if (tptr) {
2988 		rls_lun_statep(isp, tptr);
2989 	}
2990 	if (notify->nt_need_ack && notify->nt_lreserved) {
2991 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2992 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2993 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2994 			}
2995 		} else {
2996 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
2997 		}
2998 	}
2999 }
3000 
3001 /*
3002  * Find the associated private data and mark it as dead so
3003  * we don't try to work on it any further.
3004  */
3005 static void
3006 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3007 {
3008 	tstate_t *tptr;
3009 	atio_private_data_t *atp;
3010 	union ccb *accb = ccb->cab.abort_ccb;
3011 
3012 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3013 	if (tptr == NULL) {
3014 		tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3015 		if (tptr == NULL) {
3016 			ccb->ccb_h.status = CAM_REQ_INVALID;
3017 			return;
3018 		}
3019 	}
3020 
3021 	atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
3022 	if (atp == NULL) {
3023 		ccb->ccb_h.status = CAM_REQ_INVALID;
3024 	} else {
3025 		atp->dead = 1;
3026 		ccb->ccb_h.status = CAM_REQ_CMP;
3027 	}
3028 	rls_lun_statep(isp, tptr);
3029 }
3030 
3031 static void
3032 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3033 {
3034 	atio_private_data_t *atp;
3035 	inot_private_data_t *restart_queue = tptr->restart_queue;
3036 
3037 	/*
3038 	 * First, clean any commands pending restart
3039 	 */
3040 	tptr->restart_queue = NULL;
3041 	while (restart_queue) {
3042 		uint32_t this_tag_id;
3043 		inot_private_data_t *ntp = restart_queue;
3044 
3045 		restart_queue = ntp->rd.nt.nt_hba;
3046 
3047 		if (IS_24XX(isp)) {
3048 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3049 		} else {
3050 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3051 		}
3052 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3053 			isp_put_ntpd(isp, tptr, ntp);
3054 		} else {
3055 			ntp->rd.nt.nt_hba = tptr->restart_queue;
3056 			tptr->restart_queue = ntp;
3057 		}
3058 	}
3059 
3060 	/*
3061 	 * Now mark other ones dead as well.
3062 	 */
3063 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3064 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3065 			atp->dead = 1;
3066 		}
3067 	}
3068 }
3069 #endif
3070 
3071 static void
3072 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3073 {
3074 	struct cam_sim *sim;
3075 	int bus, tgt;
3076 	ispsoftc_t *isp;
3077 
3078 	sim = (struct cam_sim *)cbarg;
3079 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3080 	bus = cam_sim_bus(sim);
3081 	tgt = xpt_path_target_id(path);
3082 
3083 	switch (code) {
3084 	case AC_LOST_DEVICE:
3085 		if (IS_SCSI(isp)) {
3086 			uint16_t oflags, nflags;
3087 			sdparam *sdp = SDPARAM(isp, bus);
3088 
3089 			if (tgt >= 0) {
3090 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
3091 				nflags &= DPARM_SAFE_DFLT;
3092 				if (isp->isp_loaded_fw) {
3093 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3094 				}
3095 				oflags = sdp->isp_devparam[tgt].goal_flags;
3096 				sdp->isp_devparam[tgt].goal_flags = nflags;
3097 				sdp->isp_devparam[tgt].dev_update = 1;
3098 				sdp->update = 1;
3099 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3100 				sdp->isp_devparam[tgt].goal_flags = oflags;
3101 			}
3102 		}
3103 		break;
3104 	default:
3105 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3106 		break;
3107 	}
3108 }
3109 
3110 static void
3111 isp_poll(struct cam_sim *sim)
3112 {
3113 	ispsoftc_t *isp = cam_sim_softc(sim);
3114 	uint16_t isr, sema, info;
3115 
3116 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
3117 		isp_intr(isp, isr, sema, info);
3118 }
3119 
3120 
3121 static void
3122 isp_watchdog(void *arg)
3123 {
3124 	struct ccb_scsiio *xs = arg;
3125 	ispsoftc_t *isp;
3126 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
3127 
3128 	isp = XS_ISP(xs);
3129 
3130 	handle = isp_find_handle(isp, xs);
3131 
3132 	/*
3133 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3134 	 */
3135 	if (handle != ISP_HANDLE_FREE) {
3136 		uint16_t isr, sema, info;
3137 		if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
3138 			isp_intr(isp, isr, sema, info);
3139 		ohandle = handle;
3140 		handle = isp_find_handle(isp, xs);
3141 	}
3142 	if (handle != ISP_HANDLE_FREE) {
3143 		/*
3144 		 * Try and make sure the command is really dead before
3145 		 * we release the handle (and DMA resources) for reuse.
3146 		 *
3147 		 * If we are successful in aborting the command then
3148 		 * we're done here because we'll get the command returned
3149 		 * back separately.
3150 		 */
3151 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3152 			return;
3153 		}
3154 
3155 		/*
3156 		 * Note that after calling the above, the command may in
3157 		 * fact have been completed.
3158 		 */
3159 		xs = isp_find_xs(isp, handle);
3160 
3161 		/*
3162 		 * If the command no longer exists, then we won't
3163 		 * be able to find the xs again with this handle.
3164 		 */
3165 		if (xs == NULL) {
3166 			return;
3167 		}
3168 
3169 		/*
3170 		 * After this point, the command is really dead.
3171 		 */
3172 		if (XS_XFRLEN(xs)) {
3173 			ISP_DMAFREE(isp, xs, handle);
3174 		}
3175 		isp_destroy_handle(isp, handle);
3176 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3177 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
3178 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
3179 		isp_prt_endcmd(isp, xs);
3180 		isp_done(xs);
3181 	} else {
3182 		if (ohandle != ISP_HANDLE_FREE) {
3183 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3184 		} else {
3185 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3186 		}
3187 	}
3188 }
3189 
3190 static void
3191 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3192 {
3193 	union ccb *ccb;
3194 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3195 
3196 	/*
3197 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3198 	 */
3199 	ccb = xpt_alloc_ccb_nowait();
3200 	if (ccb == NULL) {
3201 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3202 		return;
3203 	}
3204 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
3205 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3206 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3207 		xpt_free_ccb(ccb);
3208 		return;
3209 	}
3210 	xpt_rescan(ccb);
3211 }
3212 
3213 static void
3214 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3215 {
3216 	struct cam_path *tp;
3217 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3218 
3219 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3220 		xpt_async(AC_LOST_DEVICE, tp, NULL);
3221 		xpt_free_path(tp);
3222 	}
3223 }
3224 
3225 /*
3226  * Gone Device Timer Function- when we have decided that a device has gone
3227  * away, we wait a specific period of time prior to telling the OS it has
3228  * gone away.
3229  *
3230  * This timer function fires once a second and then scans the port database
3231  * for devices that are marked dead but still have a virtual target assigned.
3232  * We decrement a counter for that port database entry, and when it hits zero,
3233  * we tell the OS the device has gone away.
3234  */
3235 static void
3236 isp_gdt(void *arg)
3237 {
3238 	struct isp_fc *fc = arg;
3239 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3240 }
3241 
3242 static void
3243 isp_gdt_task(void *arg, int pending)
3244 {
3245 	struct isp_fc *fc = arg;
3246 	ispsoftc_t *isp = fc->isp;
3247 	int chan = fc - isp->isp_osinfo.pc.fc;
3248 	fcportdb_t *lp;
3249 	struct ac_contract ac;
3250 	struct ac_device_changed *adc;
3251 	int dbidx, more_to_do = 0;
3252 
3253 	ISP_LOCK(isp);
3254 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3255 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3256 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3257 
3258 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3259 			continue;
3260 		}
3261 		if (lp->gone_timer != 0) {
3262 			lp->gone_timer -= 1;
3263 			more_to_do++;
3264 			continue;
3265 		}
3266 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3267 		if (lp->is_target) {
3268 			lp->is_target = 0;
3269 			isp_make_gone(isp, lp, chan, dbidx);
3270 		}
3271 		if (lp->is_initiator) {
3272 			lp->is_initiator = 0;
3273 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3274 			adc = (struct ac_device_changed *) ac.contract_data;
3275 			adc->wwpn = lp->port_wwn;
3276 			adc->port = lp->portid;
3277 			adc->target = dbidx;
3278 			adc->arrived = 0;
3279 			xpt_async(AC_CONTRACT, fc->path, &ac);
3280 		}
3281 		lp->state = FC_PORTDB_STATE_NIL;
3282 	}
3283 	if (fc->ready) {
3284 		if (more_to_do) {
3285 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
3286 		} else {
3287 			callout_deactivate(&fc->gdt);
3288 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3289 		}
3290 	}
3291 	ISP_UNLOCK(isp);
3292 }
3293 
3294 /*
3295  * When loop goes down we remember the time and freeze CAM command queue.
3296  * During some time period we are trying to reprobe the loop.  But if we
3297  * fail, we tell the OS that devices have gone away and drop the freeze.
3298  *
3299  * We don't clear the devices out of our port database because, when loop
3300  * come back up, we have to do some actual cleanup with the chip at that
3301  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3302  */
3303 static void
3304 isp_loop_changed(ispsoftc_t *isp, int chan)
3305 {
3306 	fcparam *fcp = FCPARAM(isp, chan);
3307 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3308 
3309 	if (fc->loop_down_time)
3310 		return;
3311 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3312 	if (fcp->role & ISP_ROLE_INITIATOR)
3313 		isp_freeze_loopdown(isp, chan);
3314 	fc->loop_dead = 0;
3315 	fc->loop_down_time = time_uptime;
3316 	wakeup(fc);
3317 }
3318 
3319 static void
3320 isp_loop_up(ispsoftc_t *isp, int chan)
3321 {
3322 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3323 
3324 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3325 	fc->loop_seen_once = 1;
3326 	fc->loop_dead = 0;
3327 	fc->loop_down_time = 0;
3328 	isp_unfreeze_loopdown(isp, chan);
3329 }
3330 
3331 static void
3332 isp_loop_dead(ispsoftc_t *isp, int chan)
3333 {
3334 	fcparam *fcp = FCPARAM(isp, chan);
3335 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3336 	fcportdb_t *lp;
3337 	struct ac_contract ac;
3338 	struct ac_device_changed *adc;
3339 	int dbidx, i;
3340 
3341 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
3342 
3343 	/*
3344 	 * Notify to the OS all targets who we now consider have departed.
3345 	 */
3346 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3347 		lp = &fcp->portdb[dbidx];
3348 
3349 		if (lp->state == FC_PORTDB_STATE_NIL)
3350 			continue;
3351 
3352 		/*
3353 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3354 		 */
3355 		for (i = 0; i < isp->isp_maxcmds; i++) {
3356 			struct ccb_scsiio *xs;
3357 
3358 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3359 				continue;
3360 			}
3361 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3362 				continue;
3363                         }
3364 			if (dbidx != XS_TGT(xs)) {
3365 				continue;
3366 			}
3367 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
3368 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
3369 			    (uintmax_t)XS_LUN(xs));
3370 		}
3371 
3372 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3373 		if (lp->is_target) {
3374 			lp->is_target = 0;
3375 			isp_make_gone(isp, lp, chan, dbidx);
3376 		}
3377 		if (lp->is_initiator) {
3378 			lp->is_initiator = 0;
3379 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3380 			adc = (struct ac_device_changed *) ac.contract_data;
3381 			adc->wwpn = lp->port_wwn;
3382 			adc->port = lp->portid;
3383 			adc->target = dbidx;
3384 			adc->arrived = 0;
3385 			xpt_async(AC_CONTRACT, fc->path, &ac);
3386 		}
3387 	}
3388 
3389 	isp_unfreeze_loopdown(isp, chan);
3390 	fc->loop_dead = 1;
3391 	fc->loop_down_time = 0;
3392 }
3393 
3394 static void
3395 isp_kthread(void *arg)
3396 {
3397 	struct isp_fc *fc = arg;
3398 	ispsoftc_t *isp = fc->isp;
3399 	int chan = fc - isp->isp_osinfo.pc.fc;
3400 	int slp = 0, d;
3401 	int lb, lim;
3402 
3403 	mtx_lock(&isp->isp_osinfo.lock);
3404 
3405 	while (isp->isp_osinfo.is_exiting == 0) {
3406 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3407 		    "Chan %d Checking FC state", chan);
3408 		lb = isp_fc_runstate(isp, chan, 250000);
3409 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3410 		    "Chan %d FC got to %s state", chan,
3411 		    isp_fc_loop_statename(lb));
3412 
3413 		/*
3414 		 * Our action is different based upon whether we're supporting
3415 		 * Initiator mode or not. If we are, we might freeze the simq
3416 		 * when loop is down and set all sorts of different delays to
3417 		 * check again.
3418 		 *
3419 		 * If not, we simply just wait for loop to come up.
3420 		 */
3421 		if (lb == LOOP_READY || lb < 0) {
3422 			slp = 0;
3423 		} else {
3424 			/*
3425 			 * If we've never seen loop up and we've waited longer
3426 			 * than quickboot time, or we've seen loop up but we've
3427 			 * waited longer than loop_down_limit, give up and go
3428 			 * to sleep until loop comes up.
3429 			 */
3430 			if (fc->loop_seen_once == 0)
3431 				lim = isp_quickboot_time;
3432 			else
3433 				lim = fc->loop_down_limit;
3434 			d = time_uptime - fc->loop_down_time;
3435 			if (d >= lim)
3436 				slp = 0;
3437 			else if (d < 10)
3438 				slp = 1;
3439 			else if (d < 30)
3440 				slp = 5;
3441 			else if (d < 60)
3442 				slp = 10;
3443 			else if (d < 120)
3444 				slp = 20;
3445 			else
3446 				slp = 30;
3447 		}
3448 
3449 		if (slp == 0) {
3450 			if (lb == LOOP_READY)
3451 				isp_loop_up(isp, chan);
3452 			else
3453 				isp_loop_dead(isp, chan);
3454 		}
3455 
3456 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3457 		    "Chan %d sleep for %d seconds", chan, slp);
3458 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
3459 	}
3460 	fc->num_threads -= 1;
3461 	mtx_unlock(&isp->isp_osinfo.lock);
3462 	kthread_exit();
3463 }
3464 
3465 static void
3466 isp_action(struct cam_sim *sim, union ccb *ccb)
3467 {
3468 	int bus, tgt, ts, error;
3469 	ispsoftc_t *isp;
3470 	struct ccb_trans_settings *cts;
3471 
3472 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3473 
3474 	isp = (ispsoftc_t *)cam_sim_softc(sim);
3475 	mtx_assert(&isp->isp_lock, MA_OWNED);
3476 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
3477 	ISP_PCMD(ccb) = NULL;
3478 
3479 	switch (ccb->ccb_h.func_code) {
3480 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
3481 		bus = XS_CHANNEL(ccb);
3482 		/*
3483 		 * Do a couple of preliminary checks...
3484 		 */
3485 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3486 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3487 				ccb->ccb_h.status = CAM_REQ_INVALID;
3488 				isp_done((struct ccb_scsiio *) ccb);
3489 				break;
3490 			}
3491 		}
3492 		ccb->csio.req_map = NULL;
3493 #ifdef	DIAGNOSTIC
3494 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3495 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3496 			ccb->ccb_h.status = CAM_PATH_INVALID;
3497 		} else if (ISP_MAX_LUNS(isp) > 0 &&
3498 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3499 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3500 			ccb->ccb_h.status = CAM_PATH_INVALID;
3501 		}
3502 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3503 			xpt_done(ccb);
3504 			break;
3505 		}
3506 #endif
3507 		ccb->csio.scsi_status = SCSI_STATUS_OK;
3508 		if (isp_get_pcmd(isp, ccb)) {
3509 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
3510 			cam_freeze_devq(ccb->ccb_h.path);
3511 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
3512 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3513 			xpt_done(ccb);
3514 			break;
3515 		}
3516 		error = isp_start((XS_T *) ccb);
3517 		switch (error) {
3518 		case CMD_QUEUED:
3519 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
3520 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
3521 				break;
3522 			}
3523 			ts = ccb->ccb_h.timeout;
3524 			if (ts == CAM_TIME_DEFAULT) {
3525 				ts = 60*1000;
3526 			}
3527 			ts = isp_mstohz(ts);
3528 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3529 			break;
3530 		case CMD_RQLATER:
3531 			/*
3532 			 * We get this result if the loop isn't ready
3533 			 * or if the device in question has gone zombie.
3534 			 */
3535 			if (ISP_FC_PC(isp, bus)->loop_dead) {
3536 				isp_prt(isp, ISP_LOGDEBUG0,
3537 				    "%d.%jx loop is dead",
3538 				    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3539 				ccb->ccb_h.status = CAM_SEL_TIMEOUT;
3540 				isp_done((struct ccb_scsiio *) ccb);
3541 				break;
3542 			}
3543 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
3544 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3545 			cam_freeze_devq(ccb->ccb_h.path);
3546 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3547 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3548 			isp_free_pcmd(isp, ccb);
3549 			xpt_done(ccb);
3550 			break;
3551 		case CMD_EAGAIN:
3552 			isp_free_pcmd(isp, ccb);
3553 			cam_freeze_devq(ccb->ccb_h.path);
3554 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3555 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3556 			xpt_done(ccb);
3557 			break;
3558 		case CMD_COMPLETE:
3559 			isp_done((struct ccb_scsiio *) ccb);
3560 			break;
3561 		default:
3562 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3563 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3564 			isp_free_pcmd(isp, ccb);
3565 			xpt_done(ccb);
3566 		}
3567 		break;
3568 
3569 #ifdef	ISP_TARGET_MODE
3570 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
3571 		if (ccb->cel.enable) {
3572 			isp_enable_lun(isp, ccb);
3573 		} else {
3574 			isp_disable_lun(isp, ccb);
3575 		}
3576 		break;
3577 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3578 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3579 	{
3580 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3581 		if (tptr == NULL) {
3582 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
3583 		}
3584 		if (tptr == NULL) {
3585 			const char *str;
3586 			uint32_t tag;
3587 
3588 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3589 				str = "XPT_IMMEDIATE_NOTIFY";
3590 				tag = ccb->cin1.seq_id;
3591 			} else {
3592 				tag = ccb->atio.tag_id;
3593 				str = "XPT_ACCEPT_TARGET_IO";
3594 			}
3595 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
3596 			dump_tstates(isp, XS_CHANNEL(ccb));
3597 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3598 			break;
3599 		}
3600 		ccb->ccb_h.spriv_field0 = 0;
3601 		ccb->ccb_h.spriv_ptr1 = isp;
3602 
3603 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3604 			if (ccb->atio.tag_id) {
3605 				atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
3606 				if (atp) {
3607 					isp_put_atpd(isp, tptr, atp);
3608 				}
3609 			}
3610 			tptr->atio_count++;
3611 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3612 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
3613 			    ccb->atio.tag_id, tptr->atio_count);
3614 			ccb->atio.tag_id = 0;
3615 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3616 			if (ccb->cin1.tag_id) {
3617 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
3618 				if (ntp) {
3619 					isp_put_ntpd(isp, tptr, ntp);
3620 				}
3621 			}
3622 			tptr->inot_count++;
3623 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3624 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
3625 			    ccb->cin1.seq_id, tptr->inot_count);
3626 			ccb->cin1.seq_id = 0;
3627 		}
3628 		rls_lun_statep(isp, tptr);
3629 		ccb->ccb_h.status = CAM_REQ_INPROG;
3630 		break;
3631 	}
3632 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3633 	{
3634 		tstate_t *tptr;
3635 		inot_private_data_t *ntp;
3636 
3637 		/*
3638 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
3639 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
3640 		 */
3641 		/*
3642 		 * All the relevant path information is in the associated immediate notify
3643 		 */
3644 		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);
3645 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
3646 		if (ntp == NULL) {
3647 			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__,
3648 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
3649 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3650 			xpt_done(ccb);
3651 			break;
3652 		}
3653 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt,
3654 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
3655 			rls_lun_statep(isp, tptr);
3656 			cam_freeze_devq(ccb->ccb_h.path);
3657 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3658 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3659 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
3660 			break;
3661 		}
3662 		isp_put_ntpd(isp, tptr, ntp);
3663 		rls_lun_statep(isp, tptr);
3664 		ccb->ccb_h.status = CAM_REQ_CMP;
3665 		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);
3666 		xpt_done(ccb);
3667 		break;
3668 	}
3669 	case XPT_CONT_TARGET_IO:
3670 		isp_target_start_ctio(isp, ccb, FROM_CAM);
3671 		break;
3672 #endif
3673 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3674 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3675 		tgt = ccb->ccb_h.target_id;
3676 		tgt |= (bus << 16);
3677 
3678 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3679 		if (error) {
3680 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3681 		} else {
3682 			/*
3683 			 * If we have a FC device, reset the Command
3684 			 * Reference Number, because the target will expect
3685 			 * that we re-start the CRN at 1 after a reset.
3686 			 */
3687 			if (IS_FC(isp))
3688 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3689 
3690 			ccb->ccb_h.status = CAM_REQ_CMP;
3691 		}
3692 		xpt_done(ccb);
3693 		break;
3694 	case XPT_ABORT:			/* Abort the specified CCB */
3695 	{
3696 		union ccb *accb = ccb->cab.abort_ccb;
3697 		switch (accb->ccb_h.func_code) {
3698 #ifdef	ISP_TARGET_MODE
3699 		case XPT_ACCEPT_TARGET_IO:
3700 			isp_target_mark_aborted(isp, ccb);
3701 			break;
3702 #endif
3703 		case XPT_SCSI_IO:
3704 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3705 			if (error) {
3706 				ccb->ccb_h.status = CAM_UA_ABORT;
3707 			} else {
3708 				ccb->ccb_h.status = CAM_REQ_CMP;
3709 			}
3710 			break;
3711 		default:
3712 			ccb->ccb_h.status = CAM_REQ_INVALID;
3713 			break;
3714 		}
3715 		/*
3716 		 * This is not a queued CCB, so the caller expects it to be
3717 		 * complete when control is returned.
3718 		 */
3719 		break;
3720 	}
3721 #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3722 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3723 		cts = &ccb->cts;
3724 		if (!IS_CURRENT_SETTINGS(cts)) {
3725 			ccb->ccb_h.status = CAM_REQ_INVALID;
3726 			xpt_done(ccb);
3727 			break;
3728 		}
3729 		tgt = cts->ccb_h.target_id;
3730 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3731 		if (IS_SCSI(isp)) {
3732 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3733 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3734 			sdparam *sdp = SDPARAM(isp, bus);
3735 			uint16_t *dptr;
3736 
3737 			if (spi->valid == 0 && scsi->valid == 0) {
3738 				ccb->ccb_h.status = CAM_REQ_CMP;
3739 				xpt_done(ccb);
3740 				break;
3741 			}
3742 
3743 			/*
3744 			 * We always update (internally) from goal_flags
3745 			 * so any request to change settings just gets
3746 			 * vectored to that location.
3747 			 */
3748 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3749 
3750 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3751 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3752 					*dptr |= DPARM_DISC;
3753 				else
3754 					*dptr &= ~DPARM_DISC;
3755 			}
3756 
3757 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3758 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3759 					*dptr |= DPARM_TQING;
3760 				else
3761 					*dptr &= ~DPARM_TQING;
3762 			}
3763 
3764 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3765 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3766 					*dptr |= DPARM_WIDE;
3767 				else
3768 					*dptr &= ~DPARM_WIDE;
3769 			}
3770 
3771 			/*
3772 			 * XXX: FIX ME
3773 			 */
3774 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3775 				*dptr |= DPARM_SYNC;
3776 				/*
3777 				 * XXX: CHECK FOR LEGALITY
3778 				 */
3779 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
3780 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3781 			} else {
3782 				*dptr &= ~DPARM_SYNC;
3783 			}
3784 			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,
3785 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3786 			sdp->isp_devparam[tgt].dev_update = 1;
3787 			sdp->update = 1;
3788 		}
3789 		ccb->ccb_h.status = CAM_REQ_CMP;
3790 		xpt_done(ccb);
3791 		break;
3792 	case XPT_GET_TRAN_SETTINGS:
3793 		cts = &ccb->cts;
3794 		tgt = cts->ccb_h.target_id;
3795 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3796 		if (IS_FC(isp)) {
3797 			fcparam *fcp = FCPARAM(isp, bus);
3798 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3799 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3800 
3801 			cts->protocol = PROTO_SCSI;
3802 			cts->protocol_version = SCSI_REV_2;
3803 			cts->transport = XPORT_FC;
3804 			cts->transport_version = 0;
3805 
3806 			scsi->valid = CTS_SCSI_VALID_TQ;
3807 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3808 			fc->valid = CTS_FC_VALID_SPEED;
3809 			fc->bitrate = 100000;
3810 			fc->bitrate *= fcp->isp_gbspeed;
3811 			if (tgt < MAX_FC_TARG) {
3812 				fcportdb_t *lp = &fcp->portdb[tgt];
3813 				fc->wwnn = lp->node_wwn;
3814 				fc->wwpn = lp->port_wwn;
3815 				fc->port = lp->portid;
3816 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3817 			}
3818 		} else {
3819 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3820 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3821 			sdparam *sdp = SDPARAM(isp, bus);
3822 			uint16_t dval, pval, oval;
3823 
3824 			if (IS_CURRENT_SETTINGS(cts)) {
3825 				sdp->isp_devparam[tgt].dev_refresh = 1;
3826 				sdp->update = 1;
3827 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3828 				dval = sdp->isp_devparam[tgt].actv_flags;
3829 				oval = sdp->isp_devparam[tgt].actv_offset;
3830 				pval = sdp->isp_devparam[tgt].actv_period;
3831 			} else {
3832 				dval = sdp->isp_devparam[tgt].nvrm_flags;
3833 				oval = sdp->isp_devparam[tgt].nvrm_offset;
3834 				pval = sdp->isp_devparam[tgt].nvrm_period;
3835 			}
3836 
3837 			cts->protocol = PROTO_SCSI;
3838 			cts->protocol_version = SCSI_REV_2;
3839 			cts->transport = XPORT_SPI;
3840 			cts->transport_version = 2;
3841 
3842 			spi->valid = 0;
3843 			scsi->valid = 0;
3844 			spi->flags = 0;
3845 			scsi->flags = 0;
3846 			if (dval & DPARM_DISC) {
3847 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3848 			}
3849 			if ((dval & DPARM_SYNC) && oval && pval) {
3850 				spi->sync_offset = oval;
3851 				spi->sync_period = pval;
3852 			} else {
3853 				spi->sync_offset = 0;
3854 				spi->sync_period = 0;
3855 			}
3856 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3857 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3858 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3859 			if (dval & DPARM_WIDE) {
3860 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3861 			} else {
3862 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3863 			}
3864 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3865 				scsi->valid = CTS_SCSI_VALID_TQ;
3866 				if (dval & DPARM_TQING) {
3867 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3868 				}
3869 				spi->valid |= CTS_SPI_VALID_DISC;
3870 			}
3871 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3872 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3873 		}
3874 		ccb->ccb_h.status = CAM_REQ_CMP;
3875 		xpt_done(ccb);
3876 		break;
3877 
3878 	case XPT_CALC_GEOMETRY:
3879 		cam_calc_geometry(&ccb->ccg, 1);
3880 		xpt_done(ccb);
3881 		break;
3882 
3883 	case XPT_RESET_BUS:		/* Reset the specified bus */
3884 		bus = cam_sim_bus(sim);
3885 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
3886 		if (error) {
3887 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3888 			xpt_done(ccb);
3889 			break;
3890 		}
3891 		if (bootverbose) {
3892 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
3893 		}
3894 		if (IS_FC(isp)) {
3895 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
3896 		} else {
3897 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
3898 		}
3899 		ccb->ccb_h.status = CAM_REQ_CMP;
3900 		xpt_done(ccb);
3901 		break;
3902 
3903 	case XPT_TERM_IO:		/* Terminate the I/O process */
3904 		ccb->ccb_h.status = CAM_REQ_INVALID;
3905 		xpt_done(ccb);
3906 		break;
3907 
3908 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
3909 	{
3910 		struct ccb_sim_knob *kp = &ccb->knob;
3911 		fcparam *fcp;
3912 
3913 		if (!IS_FC(isp)) {
3914 			ccb->ccb_h.status = CAM_REQ_INVALID;
3915 			xpt_done(ccb);
3916 			break;
3917 		}
3918 
3919 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3920 		fcp = FCPARAM(isp, bus);
3921 
3922 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
3923 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
3924 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
3925 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
3926 		}
3927 		ccb->ccb_h.status = CAM_REQ_CMP;
3928 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
3929 			int rchange = 0;
3930 			int newrole = 0;
3931 
3932 			switch (kp->xport_specific.fc.role) {
3933 			case KNOB_ROLE_NONE:
3934 				if (fcp->role != ISP_ROLE_NONE) {
3935 					rchange = 1;
3936 					newrole = ISP_ROLE_NONE;
3937 				}
3938 				break;
3939 			case KNOB_ROLE_TARGET:
3940 				if (fcp->role != ISP_ROLE_TARGET) {
3941 					rchange = 1;
3942 					newrole = ISP_ROLE_TARGET;
3943 				}
3944 				break;
3945 			case KNOB_ROLE_INITIATOR:
3946 				if (fcp->role != ISP_ROLE_INITIATOR) {
3947 					rchange = 1;
3948 					newrole = ISP_ROLE_INITIATOR;
3949 				}
3950 				break;
3951 			case KNOB_ROLE_BOTH:
3952 				if (fcp->role != ISP_ROLE_BOTH) {
3953 					rchange = 1;
3954 					newrole = ISP_ROLE_BOTH;
3955 				}
3956 				break;
3957 			}
3958 			if (rchange) {
3959 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
3960 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
3961 				    bus, newrole) != 0) {
3962 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3963 					xpt_done(ccb);
3964 					break;
3965 				}
3966 			}
3967 		}
3968 		xpt_done(ccb);
3969 		break;
3970 	}
3971 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3972 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
3973 	{
3974 		struct ccb_sim_knob *kp = &ccb->knob;
3975 
3976 		if (IS_FC(isp)) {
3977 			fcparam *fcp;
3978 
3979 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3980 			fcp = FCPARAM(isp, bus);
3981 
3982 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
3983 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
3984 			switch (fcp->role) {
3985 			case ISP_ROLE_NONE:
3986 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3987 				break;
3988 			case ISP_ROLE_TARGET:
3989 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3990 				break;
3991 			case ISP_ROLE_INITIATOR:
3992 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3993 				break;
3994 			case ISP_ROLE_BOTH:
3995 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3996 				break;
3997 			}
3998 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3999 			ccb->ccb_h.status = CAM_REQ_CMP;
4000 		} else {
4001 			ccb->ccb_h.status = CAM_REQ_INVALID;
4002 		}
4003 		xpt_done(ccb);
4004 		break;
4005 	}
4006 	case XPT_PATH_INQ:		/* Path routing inquiry */
4007 	{
4008 		struct ccb_pathinq *cpi = &ccb->cpi;
4009 
4010 		cpi->version_num = 1;
4011 #ifdef	ISP_TARGET_MODE
4012 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
4013 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4014 		else
4015 #endif
4016 			cpi->target_sprt = 0;
4017 		cpi->hba_eng_cnt = 0;
4018 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4019 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
4020 		    255 : ISP_MAX_LUNS(isp) - 1;
4021 		cpi->bus_id = cam_sim_bus(sim);
4022 		if (isp->isp_osinfo.sixtyfourbit)
4023 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
4024 		else
4025 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
4026 
4027 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4028 		if (IS_FC(isp)) {
4029 			fcparam *fcp = FCPARAM(isp, bus);
4030 
4031 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
4032 #if __FreeBSD_version >= 1000700
4033 			cpi->hba_misc |= PIM_EXTLUNS;
4034 #endif
4035 #if __FreeBSD_version >= 1000039
4036 			cpi->hba_misc |= PIM_NOSCAN;
4037 #endif
4038 
4039 			/*
4040 			 * Because our loop ID can shift from time to time,
4041 			 * make our initiator ID out of range of our bus.
4042 			 */
4043 			cpi->initiator_id = cpi->max_target + 1;
4044 
4045 			/*
4046 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
4047 			 */
4048 			if (IS_25XX(isp)) {
4049 				cpi->base_transfer_speed = 8000000;
4050 			} else if (IS_24XX(isp)) {
4051 				cpi->base_transfer_speed = 4000000;
4052 			} else if (IS_23XX(isp)) {
4053 				cpi->base_transfer_speed = 2000000;
4054 			} else {
4055 				cpi->base_transfer_speed = 1000000;
4056 			}
4057 			cpi->hba_inquiry = PI_TAG_ABLE;
4058 			cpi->transport = XPORT_FC;
4059 			cpi->transport_version = 0;
4060 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4061 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4062 			cpi->xport_specific.fc.port = fcp->isp_portid;
4063 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4064 		} else {
4065 			sdparam *sdp = SDPARAM(isp, bus);
4066 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4067 			cpi->hba_misc = PIM_UNMAPPED;
4068 			cpi->initiator_id = sdp->isp_initiator_id;
4069 			cpi->base_transfer_speed = 3300;
4070 			cpi->transport = XPORT_SPI;
4071 			cpi->transport_version = 2;
4072 		}
4073 		cpi->protocol = PROTO_SCSI;
4074 		cpi->protocol_version = SCSI_REV_2;
4075 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4076 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4077 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4078 		cpi->unit_number = cam_sim_unit(sim);
4079 		cpi->ccb_h.status = CAM_REQ_CMP;
4080 		xpt_done(ccb);
4081 		break;
4082 	}
4083 	default:
4084 		ccb->ccb_h.status = CAM_REQ_INVALID;
4085 		xpt_done(ccb);
4086 		break;
4087 	}
4088 }
4089 
4090 #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4091 
4092 void
4093 isp_done(XS_T *sccb)
4094 {
4095 	ispsoftc_t *isp = XS_ISP(sccb);
4096 	uint32_t status;
4097 
4098 	if (XS_NOERR(sccb))
4099 		XS_SETERR(sccb, CAM_REQ_CMP);
4100 
4101 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4102 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4103 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4104 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4105 		} else {
4106 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4107 		}
4108 	}
4109 
4110 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4111 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
4112 	if (status != CAM_REQ_CMP) {
4113 		if (status != CAM_SEL_TIMEOUT)
4114 			isp_prt(isp, ISP_LOGDEBUG0,
4115 			    "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
4116 			    XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
4117 			    sccb->ccb_h.status, sccb->scsi_status);
4118 		else if ((IS_FC(isp))
4119 		      && (XS_TGT(sccb) < MAX_FC_TARG)) {
4120 			fcparam *fcp;
4121 
4122 			fcp = FCPARAM(isp, XS_CHANNEL(sccb));
4123 			fcp->portdb[XS_TGT(sccb)].is_target = 0;
4124 		}
4125 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4126 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
4127 			xpt_freeze_devq(sccb->ccb_h.path, 1);
4128 		}
4129 	}
4130 
4131 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4132 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4133 	}
4134 
4135 	if (ISP_PCMD(sccb)) {
4136 		if (callout_active(&PISP_PCMD(sccb)->wdog))
4137 			callout_stop(&PISP_PCMD(sccb)->wdog);
4138 		isp_free_pcmd(isp, (union ccb *) sccb);
4139 	}
4140 	xpt_done((union ccb *) sccb);
4141 }
4142 
4143 void
4144 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4145 {
4146 	int bus;
4147 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
4148 	char buf[64];
4149 	char *msg = NULL;
4150 	target_id_t tgt;
4151 	fcportdb_t *lp;
4152 	struct isp_fc *fc;
4153 	struct cam_path *tmppath;
4154 	struct ac_contract ac;
4155 	struct ac_device_changed *adc;
4156 	va_list ap;
4157 
4158 	switch (cmd) {
4159 	case ISPASYNC_NEW_TGT_PARAMS:
4160 	{
4161 		struct ccb_trans_settings_scsi *scsi;
4162 		struct ccb_trans_settings_spi *spi;
4163 		int flags, tgt;
4164 		sdparam *sdp;
4165 		struct ccb_trans_settings cts;
4166 
4167 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
4168 
4169 		va_start(ap, cmd);
4170 		bus = va_arg(ap, int);
4171 		tgt = va_arg(ap, int);
4172 		va_end(ap);
4173 		sdp = SDPARAM(isp, bus);
4174 
4175 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4176 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4177 			break;
4178 		}
4179 		flags = sdp->isp_devparam[tgt].actv_flags;
4180 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4181 		cts.protocol = PROTO_SCSI;
4182 		cts.transport = XPORT_SPI;
4183 
4184 		scsi = &cts.proto_specific.scsi;
4185 		spi = &cts.xport_specific.spi;
4186 
4187 		if (flags & DPARM_TQING) {
4188 			scsi->valid |= CTS_SCSI_VALID_TQ;
4189 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4190 		}
4191 
4192 		if (flags & DPARM_DISC) {
4193 			spi->valid |= CTS_SPI_VALID_DISC;
4194 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4195 		}
4196 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4197 		if (flags & DPARM_WIDE) {
4198 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4199 		} else {
4200 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4201 		}
4202 		if (flags & DPARM_SYNC) {
4203 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4204 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4205 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
4206 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4207 		}
4208 		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);
4209 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4210 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4211 		xpt_free_path(tmppath);
4212 		break;
4213 	}
4214 	case ISPASYNC_BUS_RESET:
4215 	{
4216 		va_start(ap, cmd);
4217 		bus = va_arg(ap, int);
4218 		va_end(ap);
4219 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
4220 		if (IS_FC(isp)) {
4221 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
4222 		} else {
4223 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
4224 		}
4225 		break;
4226 	}
4227 	case ISPASYNC_LIP:
4228 		if (msg == NULL)
4229 			msg = "LIP Received";
4230 		/* FALLTHROUGH */
4231 	case ISPASYNC_LOOP_RESET:
4232 		if (msg == NULL)
4233 			msg = "LOOP Reset";
4234 		/* FALLTHROUGH */
4235 	case ISPASYNC_LOOP_DOWN:
4236 		if (msg == NULL)
4237 			msg = "LOOP Down";
4238 		va_start(ap, cmd);
4239 		bus = va_arg(ap, int);
4240 		va_end(ap);
4241 		isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
4242 		isp_loop_changed(isp, bus);
4243 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
4244 		break;
4245 	case ISPASYNC_LOOP_UP:
4246 		va_start(ap, cmd);
4247 		bus = va_arg(ap, int);
4248 		va_end(ap);
4249 		isp_loop_changed(isp, bus);
4250 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
4251 		break;
4252 	case ISPASYNC_DEV_ARRIVED:
4253 		va_start(ap, cmd);
4254 		bus = va_arg(ap, int);
4255 		lp = va_arg(ap, fcportdb_t *);
4256 		va_end(ap);
4257 		fc = ISP_FC_PC(isp, bus);
4258 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4259 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4260 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4261 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4262 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4263 			lp->is_target = 1;
4264 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4265 			isp_make_here(isp, lp, bus, tgt);
4266 		}
4267 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4268 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4269 			lp->is_initiator = 1;
4270 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4271 			adc = (struct ac_device_changed *) ac.contract_data;
4272 			adc->wwpn = lp->port_wwn;
4273 			adc->port = lp->portid;
4274 			adc->target = tgt;
4275 			adc->arrived = 1;
4276 			xpt_async(AC_CONTRACT, fc->path, &ac);
4277 		}
4278 		break;
4279 	case ISPASYNC_DEV_CHANGED:
4280 		va_start(ap, cmd);
4281 		bus = va_arg(ap, int);
4282 		lp = va_arg(ap, fcportdb_t *);
4283 		va_end(ap);
4284 		fc = ISP_FC_PC(isp, bus);
4285 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4286 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4287 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4288 changed:
4289 		if (lp->is_target !=
4290 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4291 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4292 			lp->is_target = !lp->is_target;
4293 			if (lp->is_target) {
4294 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4295 				isp_make_here(isp, lp, bus, tgt);
4296 			} else {
4297 				isp_make_gone(isp, lp, bus, tgt);
4298 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4299 			}
4300 		}
4301 		if (lp->is_initiator !=
4302 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4303 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4304 			lp->is_initiator = !lp->is_initiator;
4305 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4306 			adc = (struct ac_device_changed *) ac.contract_data;
4307 			adc->wwpn = lp->port_wwn;
4308 			adc->port = lp->portid;
4309 			adc->target = tgt;
4310 			adc->arrived = lp->is_initiator;
4311 			xpt_async(AC_CONTRACT, fc->path, &ac);
4312 		}
4313 		break;
4314 	case ISPASYNC_DEV_STAYED:
4315 		va_start(ap, cmd);
4316 		bus = va_arg(ap, int);
4317 		lp = va_arg(ap, fcportdb_t *);
4318 		va_end(ap);
4319 		fc = ISP_FC_PC(isp, bus);
4320 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4321 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4322 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4323 		goto changed;
4324 	case ISPASYNC_DEV_GONE:
4325 		va_start(ap, cmd);
4326 		bus = va_arg(ap, int);
4327 		lp = va_arg(ap, fcportdb_t *);
4328 		va_end(ap);
4329 		fc = ISP_FC_PC(isp, bus);
4330 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4331 		/*
4332 		 * If this has a virtual target or initiator set the isp_gdt
4333 		 * timer running on it to delay its departure.
4334 		 */
4335 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4336 		if (lp->is_target || lp->is_initiator) {
4337 			lp->state = FC_PORTDB_STATE_ZOMBIE;
4338 			lp->gone_timer = fc->gone_device_time;
4339 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4340 			if (fc->ready && !callout_active(&fc->gdt)) {
4341 				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);
4342 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
4343 			}
4344 			break;
4345 		}
4346 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
4347 		break;
4348 	case ISPASYNC_CHANGE_NOTIFY:
4349 	{
4350 		char *msg;
4351 		int evt, nphdl, nlstate, portid, reason;
4352 
4353 		va_start(ap, cmd);
4354 		bus = va_arg(ap, int);
4355 		evt = va_arg(ap, int);
4356 		if (evt == ISPASYNC_CHANGE_PDB) {
4357 			nphdl = va_arg(ap, int);
4358 			nlstate = va_arg(ap, int);
4359 			reason = va_arg(ap, int);
4360 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4361 			portid = va_arg(ap, int);
4362 		} else {
4363 			nphdl = NIL_HANDLE;
4364 			nlstate = reason = 0;
4365 		}
4366 		va_end(ap);
4367 		fc = ISP_FC_PC(isp, bus);
4368 
4369 		if (evt == ISPASYNC_CHANGE_PDB) {
4370 			msg = "Port Database Changed";
4371 			isp_prt(isp, ISP_LOGINFO,
4372 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4373 			    bus, msg, nphdl, nlstate, reason);
4374 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4375 			msg = "Name Server Database Changed";
4376 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4377 			    bus, msg, portid);
4378 		} else {
4379 			msg = "Other Change Notify";
4380 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
4381 		}
4382 		isp_loop_changed(isp, bus);
4383 		break;
4384 	}
4385 #ifdef	ISP_TARGET_MODE
4386 	case ISPASYNC_TARGET_NOTIFY:
4387 	{
4388 		isp_notify_t *notify;
4389 		va_start(ap, cmd);
4390 		notify = va_arg(ap, isp_notify_t *);
4391 		va_end(ap);
4392 		switch (notify->nt_ncode) {
4393 		case NT_ABORT_TASK:
4394 		case NT_ABORT_TASK_SET:
4395 		case NT_CLEAR_ACA:
4396 		case NT_CLEAR_TASK_SET:
4397 		case NT_LUN_RESET:
4398 		case NT_TARGET_RESET:
4399 		case NT_QUERY_TASK_SET:
4400 		case NT_QUERY_ASYNC_EVENT:
4401 			/*
4402 			 * These are task management functions.
4403 			 */
4404 			isp_handle_platform_target_tmf(isp, notify);
4405 			break;
4406 		case NT_BUS_RESET:
4407 		case NT_LIP_RESET:
4408 		case NT_LINK_UP:
4409 		case NT_LINK_DOWN:
4410 		case NT_HBA_RESET:
4411 			/*
4412 			 * No action need be taken here.
4413 			 */
4414 			break;
4415 		case NT_GLOBAL_LOGOUT:
4416 		case NT_LOGOUT:
4417 			/*
4418 			 * This is device arrival/departure notification
4419 			 */
4420 			isp_handle_platform_target_notify_ack(isp, notify, 0);
4421 			break;
4422 		default:
4423 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
4424 			isp_handle_platform_target_notify_ack(isp, notify, 0);
4425 			break;
4426 		}
4427 		break;
4428 	}
4429 	case ISPASYNC_TARGET_NOTIFY_ACK:
4430 	{
4431 		void *inot;
4432 		va_start(ap, cmd);
4433 		inot = va_arg(ap, void *);
4434 		va_end(ap);
4435 		if (isp_notify_ack(isp, inot)) {
4436 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4437 			if (tp) {
4438 				tp->isp = isp;
4439 				if (inot) {
4440 					memcpy(tp->data, inot, sizeof (tp->data));
4441 					tp->not = tp->data;
4442 				} else {
4443 					tp->not = NULL;
4444 				}
4445 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
4446 				callout_reset(&tp->timer, 5,
4447 				    isp_refire_notify_ack, tp);
4448 			} else {
4449 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4450 			}
4451 		}
4452 		break;
4453 	}
4454 	case ISPASYNC_TARGET_ACTION:
4455 	{
4456 		isphdr_t *hp;
4457 
4458 		va_start(ap, cmd);
4459 		hp = va_arg(ap, isphdr_t *);
4460 		va_end(ap);
4461 		switch (hp->rqs_entry_type) {
4462 		default:
4463 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4464 			break;
4465 		case RQSTYPE_NOTIFY:
4466 			if (IS_24XX(isp)) {
4467 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4468 			} else {
4469 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4470 			}
4471 			break;
4472 		case RQSTYPE_ATIO:
4473 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4474 			break;
4475 		case RQSTYPE_ATIO2:
4476 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4477 			break;
4478 		case RQSTYPE_CTIO7:
4479 		case RQSTYPE_CTIO3:
4480 		case RQSTYPE_CTIO2:
4481 		case RQSTYPE_CTIO:
4482 			isp_handle_platform_ctio(isp, hp);
4483 			break;
4484 		case RQSTYPE_ABTS_RCVD:
4485 		{
4486 			abts_t *abts = (abts_t *)hp;
4487 			isp_notify_t notify, *nt = &notify;
4488 			tstate_t *tptr;
4489 			fcportdb_t *lp;
4490 			uint16_t chan;
4491 			uint32_t sid, did;
4492 
4493 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
4494 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
4495 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
4496 
4497 			nt->nt_hba = isp;
4498 			nt->nt_did = did;
4499 			nt->nt_nphdl = abts->abts_nphdl;
4500 			nt->nt_sid = sid;
4501 			isp_find_chan_by_did(isp, did, &chan);
4502 			if (chan == ISP_NOCHAN) {
4503 				nt->nt_tgt = TGT_ANY;
4504 			} else {
4505 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4506 				if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
4507 					nt->nt_wwn = lp->port_wwn;
4508 				} else {
4509 					nt->nt_wwn = INI_ANY;
4510 				}
4511 			}
4512 			/*
4513 			 * Try hard to find the lun for this command.
4514 			 */
4515 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
4516 			if (tptr) {
4517 				nt->nt_lun = tptr->ts_lun;
4518 				rls_lun_statep(isp, tptr);
4519 			} else {
4520 				nt->nt_lun = LUN_ANY;
4521 			}
4522 			nt->nt_need_ack = 1;
4523 			nt->nt_tagval = abts->abts_rxid_task;
4524 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
4525 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
4526 				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)",
4527 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
4528 			} else {
4529 				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)",
4530 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
4531 			}
4532 			nt->nt_channel = chan;
4533 			nt->nt_ncode = NT_ABORT_TASK;
4534 			nt->nt_lreserved = hp;
4535 			isp_handle_platform_target_tmf(isp, nt);
4536 			break;
4537 		}
4538 		}
4539 		break;
4540 	}
4541 #endif
4542 	case ISPASYNC_FW_CRASH:
4543 	{
4544 		uint16_t mbox1, mbox6;
4545 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
4546 		if (IS_DUALBUS(isp)) {
4547 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
4548 		} else {
4549 			mbox6 = 0;
4550 		}
4551 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
4552 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
4553 		isp->isp_osinfo.mbox_sleep_ok = 0;
4554 		isp_reinit(isp, 1);
4555 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
4556 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4557 		break;
4558 	}
4559 	default:
4560 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4561 		break;
4562 	}
4563 }
4564 
4565 
4566 /*
4567  * Locks are held before coming here.
4568  */
4569 void
4570 isp_uninit(ispsoftc_t *isp)
4571 {
4572 	if (IS_24XX(isp)) {
4573 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
4574 	} else {
4575 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
4576 	}
4577 	ISP_DISABLE_INTS(isp);
4578 }
4579 
4580 uint64_t
4581 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
4582 {
4583 	uint64_t seed;
4584 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4585 
4586 	/* First try to use explicitly configured WWNs. */
4587 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4588 	if (seed)
4589 		return (seed);
4590 
4591 	/* Otherwise try to use WWNs from NVRAM. */
4592 	if (isactive) {
4593 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4594 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4595 		if (seed)
4596 			return (seed);
4597 	}
4598 
4599 	/* If still no WWNs, try to steal them from the first channel. */
4600 	if (chan > 0) {
4601 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4602 		    ISP_FC_PC(isp, 0)->def_wwpn;
4603 		if (seed == 0) {
4604 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4605 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
4606 		}
4607 	}
4608 
4609 	/* If still nothing -- improvise. */
4610 	if (seed == 0) {
4611 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4612 		if (!iswwnn)
4613 			seed ^= 0x0100000000000000ULL;
4614 	}
4615 
4616 	/* For additional channels we have to improvise even more. */
4617 	if (!iswwnn && chan > 0) {
4618 		/*
4619 		 * We'll stick our channel number plus one first into bits
4620 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4621 		 * of channel which is enough for our maximum of 255 channels.
4622 		 */
4623 		seed ^= 0x0100000000000000ULL;
4624 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4625 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
4626 	}
4627 	return (seed);
4628 }
4629 
4630 void
4631 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4632 {
4633 	int loc;
4634 	char lbuf[200];
4635 	va_list ap;
4636 
4637 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4638 		return;
4639 	}
4640 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4641 	loc = strlen(lbuf);
4642 	va_start(ap, fmt);
4643 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4644 	va_end(ap);
4645 	printf("%s\n", lbuf);
4646 }
4647 
4648 void
4649 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4650 {
4651 	va_list ap;
4652 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4653 		return;
4654 	}
4655 	xpt_print_path(xs->ccb_h.path);
4656 	va_start(ap, fmt);
4657 	vprintf(fmt, ap);
4658 	va_end(ap);
4659 	printf("\n");
4660 }
4661 
4662 uint64_t
4663 isp_nanotime_sub(struct timespec *b, struct timespec *a)
4664 {
4665 	uint64_t elapsed;
4666 	struct timespec x = *b;
4667 	timespecsub(&x, a);
4668 	elapsed = GET_NANOSEC(&x);
4669 	if (elapsed == 0)
4670 		elapsed++;
4671 	return (elapsed);
4672 }
4673 
4674 int
4675 isp_mbox_acquire(ispsoftc_t *isp)
4676 {
4677 	if (isp->isp_osinfo.mboxbsy) {
4678 		return (1);
4679 	} else {
4680 		isp->isp_osinfo.mboxcmd_done = 0;
4681 		isp->isp_osinfo.mboxbsy = 1;
4682 		return (0);
4683 	}
4684 }
4685 
4686 void
4687 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4688 {
4689 	unsigned int usecs = mbp->timeout;
4690 	unsigned int max, olim, ilim;
4691 
4692 	if (usecs == 0) {
4693 		usecs = MBCMD_DEFAULT_TIMEOUT;
4694 	}
4695 	max = isp->isp_mbxwrk0 + 1;
4696 
4697 	if (isp->isp_osinfo.mbox_sleep_ok) {
4698 		unsigned int ms = (usecs + 999) / 1000;
4699 
4700 		isp->isp_osinfo.mbox_sleep_ok = 0;
4701 		isp->isp_osinfo.mbox_sleeping = 1;
4702 		for (olim = 0; olim < max; olim++) {
4703 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
4704 			if (isp->isp_osinfo.mboxcmd_done) {
4705 				break;
4706 			}
4707 		}
4708 		isp->isp_osinfo.mbox_sleep_ok = 1;
4709 		isp->isp_osinfo.mbox_sleeping = 0;
4710 	} else {
4711 		for (olim = 0; olim < max; olim++) {
4712 			for (ilim = 0; ilim < usecs; ilim += 100) {
4713 				uint16_t isr, sema, info;
4714 				if (isp->isp_osinfo.mboxcmd_done) {
4715 					break;
4716 				}
4717 				if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
4718 					isp_intr(isp, isr, sema, info);
4719 					if (isp->isp_osinfo.mboxcmd_done) {
4720 						break;
4721 					}
4722 				}
4723 				ISP_DELAY(100);
4724 			}
4725 			if (isp->isp_osinfo.mboxcmd_done) {
4726 				break;
4727 			}
4728 		}
4729 	}
4730 	if (isp->isp_osinfo.mboxcmd_done == 0) {
4731 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
4732 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
4733 		mbp->param[0] = MBOX_TIMEOUT;
4734 		isp->isp_osinfo.mboxcmd_done = 1;
4735 	}
4736 }
4737 
4738 void
4739 isp_mbox_notify_done(ispsoftc_t *isp)
4740 {
4741 	if (isp->isp_osinfo.mbox_sleeping) {
4742 		wakeup(&isp->isp_mbxworkp);
4743 	}
4744 	isp->isp_osinfo.mboxcmd_done = 1;
4745 }
4746 
4747 void
4748 isp_mbox_release(ispsoftc_t *isp)
4749 {
4750 	isp->isp_osinfo.mboxbsy = 0;
4751 }
4752 
4753 int
4754 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
4755 {
4756 	int ret = 0;
4757 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
4758 		ret = -1;
4759 	} else {
4760 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
4761 	}
4762 	return (ret);
4763 }
4764 
4765 int
4766 isp_mstohz(int ms)
4767 {
4768 	int hz;
4769 	struct timeval t;
4770 	t.tv_sec = ms / 1000;
4771 	t.tv_usec = (ms % 1000) * 1000;
4772 	hz = tvtohz(&t);
4773 	if (hz < 0) {
4774 		hz = 0x7fffffff;
4775 	}
4776 	if (hz == 0) {
4777 		hz = 1;
4778 	}
4779 	return (hz);
4780 }
4781 
4782 void
4783 isp_platform_intr(void *arg)
4784 {
4785 	ispsoftc_t *isp = arg;
4786 	uint16_t isr, sema, info;
4787 
4788 	ISP_LOCK(isp);
4789 	isp->isp_intcnt++;
4790 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
4791 		isp_intr(isp, isr, sema, info);
4792 	else
4793 		isp->isp_intbogus++;
4794 	ISP_UNLOCK(isp);
4795 }
4796 
4797 void
4798 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
4799 {
4800 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4801 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
4802 	} else {
4803 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
4804 	}
4805 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
4806 }
4807 
4808 /*
4809  * Reset the command reference number for all LUNs on a specific target
4810  * (needed when a target arrives again) or for all targets on a port
4811  * (needed for events like a LIP).
4812  */
4813 void
4814 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4815 {
4816 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4817 	struct isp_nexus *nxp;
4818 	int i;
4819 
4820 	if (tgt_set == 0)
4821 		isp_prt(isp, ISP_LOGDEBUG0,
4822 		    "Chan %d resetting CRN on all targets", chan);
4823 	else
4824 		isp_prt(isp, ISP_LOGDEBUG0,
4825 		    "Chan %d resetting CRN on target %u", chan, tgt);
4826 
4827 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
4828 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
4829 			if (tgt_set == 0 || tgt == nxp->tgt)
4830 				nxp->crnseed = 0;
4831 		}
4832 	}
4833 }
4834 
4835 int
4836 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4837 {
4838 	lun_id_t lun;
4839 	uint32_t chan, tgt;
4840 	struct isp_fc *fc;
4841 	struct isp_nexus *nxp;
4842 	int idx;
4843 
4844 	if (IS_2100(isp))
4845 		return (0);
4846 
4847 	chan = XS_CHANNEL(cmd);
4848 	tgt = XS_TGT(cmd);
4849 	lun = XS_LUN(cmd);
4850 	fc = &isp->isp_osinfo.pc.fc[chan];
4851 	idx = NEXUS_HASH(tgt, lun);
4852 	nxp = fc->nexus_hash[idx];
4853 
4854 	while (nxp) {
4855 		if (nxp->tgt == tgt && nxp->lun == lun)
4856 			break;
4857 		nxp = nxp->next;
4858 	}
4859 	if (nxp == NULL) {
4860 		nxp = fc->nexus_free_list;
4861 		if (nxp == NULL) {
4862 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4863 			if (nxp == NULL) {
4864 				return (-1);
4865 			}
4866 		} else {
4867 			fc->nexus_free_list = nxp->next;
4868 		}
4869 		nxp->tgt = tgt;
4870 		nxp->lun = lun;
4871 		nxp->next = fc->nexus_hash[idx];
4872 		fc->nexus_hash[idx] = nxp;
4873 	}
4874 	if (nxp->crnseed == 0)
4875 		nxp->crnseed = 1;
4876 	PISP_PCMD(cmd)->crn = nxp->crnseed;
4877 	*crnp = nxp->crnseed++;
4878 	return (0);
4879 }
4880 
4881 /*
4882  * We enter with the lock held
4883  */
4884 void
4885 isp_timer(void *arg)
4886 {
4887 	ispsoftc_t *isp = arg;
4888 #ifdef	ISP_TARGET_MODE
4889 	isp_tmcmd_restart(isp);
4890 #endif
4891 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
4892 }
4893 
4894 isp_ecmd_t *
4895 isp_get_ecmd(ispsoftc_t *isp)
4896 {
4897 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4898 	if (ecmd) {
4899 		isp->isp_osinfo.ecmd_free = ecmd->next;
4900 	}
4901 	return (ecmd);
4902 }
4903 
4904 void
4905 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4906 {
4907 	ecmd->next = isp->isp_osinfo.ecmd_free;
4908 	isp->isp_osinfo.ecmd_free = ecmd;
4909 }
4910