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