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