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