xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 09ddc7ad7ddf03218434d5a84d2e808da6fdcfae)
1098ca2bdSWarner Losh /*-
22df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
3e5265237SMatt Jacob  * All rights reserved.
46054c3f6SMatt Jacob  *
56054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
66054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
76054c3f6SMatt Jacob  * are met:
86054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
96054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
106054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
11aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
12aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
136054c3f6SMatt Jacob  *
146054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
186054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246054c3f6SMatt Jacob  * SUCH DAMAGE.
256054c3f6SMatt Jacob  */
26aad970f1SDavid E. O'Brien 
27799881e0SMatt Jacob /*
28799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29799881e0SMatt Jacob  */
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
325b14cb41SWill Andrews 
336054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
345d571944SMatt Jacob #include <sys/unistd.h>
355d571944SMatt Jacob #include <sys/kthread.h>
365d571944SMatt Jacob #include <sys/conf.h>
374eb49427SMatt Jacob #include <sys/module.h>
385d571944SMatt Jacob #include <sys/ioccom.h>
395d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4070273f90SMatt Jacob #include <sys/devicestat.h>
41f7c631bcSMatt Jacob #include <cam/cam_periph.h>
426c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
436054c3f6SMatt Jacob 
442df76c16SMatt Jacob #if	__FreeBSD_version < 800002
452df76c16SMatt Jacob #define	THREAD_CREATE	kthread_create
462df76c16SMatt Jacob #else
472df76c16SMatt Jacob #define	THREAD_CREATE	kproc_create
48805e1f82SMatt Jacob #endif
49805e1f82SMatt Jacob 
504eb49427SMatt Jacob MODULE_VERSION(isp, 1);
5156c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
5273030e03SMatt Jacob int isp_announced = 0;
532df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
542df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
55f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
56e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5773030e03SMatt Jacob 
58e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
59e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
605d571944SMatt Jacob static d_ioctl_t ispioctl;
61f6e75de2SMatt Jacob static void isp_intr_enable(void *);
621dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
630470d791SMatt Jacob static void isp_poll(struct cam_sim *);
64b85389e1SMatt Jacob static timeout_t isp_watchdog;
65de461933SMatt Jacob static timeout_t isp_gdt;
66de461933SMatt Jacob static task_fn_t isp_gdt_task;
675d571944SMatt Jacob static void isp_kthread(void *);
68d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6994dff771SMatt Jacob static int isp_timer_count;
702df76c16SMatt Jacob static void isp_timer(void *);
71cc8df88bSMatt Jacob 
725d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
73dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
747ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
757ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
765d571944SMatt Jacob };
775d571944SMatt Jacob 
782df76c16SMatt Jacob static int
7907f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
8007f56f1cSAlexander Motin {
8107f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
8207f56f1cSAlexander Motin 	int chan = arg2;
8307f56f1cSAlexander Motin 	int error, old, value;
8407f56f1cSAlexander Motin 
8507f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8607f56f1cSAlexander Motin 
8707f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8807f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8907f56f1cSAlexander Motin 		return (error);
9007f56f1cSAlexander Motin 
9107f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
9207f56f1cSAlexander Motin 		return (EINVAL);
9307f56f1cSAlexander Motin 
9407f56f1cSAlexander Motin 	ISP_LOCK(isp);
9507f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9607f56f1cSAlexander Motin 
97391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
98391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
99391f03daSAlexander Motin 
10007f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
10107f56f1cSAlexander Motin 	if (value == old) {
10207f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
10307f56f1cSAlexander Motin 		return (0);
10407f56f1cSAlexander Motin 	}
10507f56f1cSAlexander Motin 
10607f56f1cSAlexander Motin 	/* Actually change the role. */
1076bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10807f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10907f56f1cSAlexander Motin 	return (error);
11007f56f1cSAlexander Motin }
11107f56f1cSAlexander Motin 
11207f56f1cSAlexander Motin static int
1132df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
114478f8a96SJustin T. Gibbs {
115478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
116ea6f23cdSMatt Jacob 	struct cam_sim *sim;
117ea6f23cdSMatt Jacob 	struct cam_path *path;
118478f8a96SJustin T. Gibbs 
119478f8a96SJustin T. Gibbs 	/*
1202df76c16SMatt Jacob 	 * Construct our SIM entry.
121ea6f23cdSMatt Jacob 	 */
1222df76c16SMatt Jacob 	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);
123ea6f23cdSMatt Jacob 
1242df76c16SMatt Jacob 	if (sim == NULL) {
1252df76c16SMatt Jacob 		return (ENOMEM);
1262df76c16SMatt Jacob 	}
1272df76c16SMatt Jacob 
1282df76c16SMatt Jacob 	ISP_LOCK(isp);
1292df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1302df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1312df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1322df76c16SMatt Jacob 		return (EIO);
1332df76c16SMatt Jacob 	}
1342df76c16SMatt Jacob 	ISP_UNLOCK(isp);
135227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1362df76c16SMatt Jacob 		ISP_LOCK(isp);
1372df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1382df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1392df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1402df76c16SMatt Jacob 		return (ENXIO);
1412df76c16SMatt Jacob 	}
1422df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1432df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1442df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1452df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1462df76c16SMatt Jacob 	csa.callback_arg = sim;
147387d8239SMatt Jacob 
148387d8239SMatt Jacob 	ISP_LOCK(isp);
1492df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
150387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1512df76c16SMatt Jacob 
1522df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1532df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1542df76c16SMatt Jacob 		spi->sim = sim;
1552df76c16SMatt Jacob 		spi->path = path;
156*09ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1572df76c16SMatt Jacob 	} else {
158a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1592df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
16007f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
16107f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
16207f56f1cSAlexander Motin 		char name[16];
1632df76c16SMatt Jacob 
164a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1652df76c16SMatt Jacob 		fc->sim = sim;
1662df76c16SMatt Jacob 		fc->path = path;
1672df76c16SMatt Jacob 		fc->isp = isp;
168a01f5aebSMatt Jacob 		fc->ready = 1;
169de461933SMatt Jacob 
1702df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
171de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
172*09ddc7adSAlexander Motin 		TAILQ_INIT(&fc->waitq);
173e561aa79SAlexander Motin 		isp_loop_changed(isp, chan);
1742df76c16SMatt Jacob 		ISP_UNLOCK(isp);
175a01f5aebSMatt Jacob 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
176a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
177a01f5aebSMatt Jacob 			ISP_LOCK(isp);
178a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
179a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
180a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
181a01f5aebSMatt Jacob 			return (ENOMEM);
182a01f5aebSMatt Jacob 		}
183e596ff7aSAlexander Motin 		fc->num_threads += 1;
18407f56f1cSAlexander Motin 		if (chan > 0) {
18507f56f1cSAlexander Motin 			snprintf(name, sizeof(name), "chan%d", chan);
18607f56f1cSAlexander Motin 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
18707f56f1cSAlexander Motin 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
188a0ec8e99SMatt Jacob 		}
189e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
190e596ff7aSAlexander Motin 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
191e596ff7aSAlexander Motin 		    "World Wide Node Name");
192e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
193e596ff7aSAlexander Motin 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
194e596ff7aSAlexander Motin 		    "World Wide Port Name");
195e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
196e596ff7aSAlexander Motin 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
197e596ff7aSAlexander Motin 		    "Loop Down Limit");
198e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
199e596ff7aSAlexander Motin 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
200e596ff7aSAlexander Motin 		    "Gone Device Time");
20107f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
202e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
203e596ff7aSAlexander Motin 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
204e596ff7aSAlexander Motin 		    "Cause a Lost Frame on a Read");
20507f56f1cSAlexander Motin #endif
20607f56f1cSAlexander Motin 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
20707f56f1cSAlexander Motin 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
20807f56f1cSAlexander Motin 		    isp_role_sysctl, "I", "Current role");
209e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210e596ff7aSAlexander Motin 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
211e596ff7aSAlexander Motin 		    "Connection speed in gigabits");
212e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213e596ff7aSAlexander Motin 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
214e596ff7aSAlexander Motin 		    "Link state");
215e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
216e596ff7aSAlexander Motin 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
217e596ff7aSAlexander Motin 		    "Firmware state");
218e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
219e596ff7aSAlexander Motin 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
220e596ff7aSAlexander Motin 		    "Loop state");
221e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
222e596ff7aSAlexander Motin 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
223e596ff7aSAlexander Motin 		    "Connection topology");
2242df76c16SMatt Jacob 	}
2252df76c16SMatt Jacob 	return (0);
2262df76c16SMatt Jacob }
2272df76c16SMatt Jacob 
2281d0a1de2SWill Andrews static void
2291d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2301d0a1de2SWill Andrews {
2311d0a1de2SWill Andrews 	struct cam_sim *sim;
2321d0a1de2SWill Andrews 	struct cam_path *path;
2331d0a1de2SWill Andrews 	struct ccb_setasync csa;
2341d0a1de2SWill Andrews 	int *num_threads;
2351d0a1de2SWill Andrews 
2361d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2371d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2381d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2391d0a1de2SWill Andrews 
2401d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2411d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2421d0a1de2SWill Andrews 	csa.event_enable = 0;
2431d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2441d0a1de2SWill Andrews 	csa.callback_arg = sim;
2451d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2461d0a1de2SWill Andrews 	xpt_free_path(path);
2471d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2481d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2491d0a1de2SWill Andrews 
2501d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2511d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2521d0a1de2SWill Andrews 	while (*num_threads != 0)
2531d0a1de2SWill Andrews 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
2541d0a1de2SWill Andrews }
2551d0a1de2SWill Andrews 
2562df76c16SMatt Jacob int
2572df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2582df76c16SMatt Jacob {
2592df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2602df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2612df76c16SMatt Jacob 	int chan;
2622df76c16SMatt Jacob 
2632df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
2642df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_arg = isp;
265e95725cbSMatt Jacob 	/*
266e95725cbSMatt Jacob 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
267e95725cbSMatt Jacob 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
268e95725cbSMatt Jacob 	 * which would be unwise to then set again later.
269e95725cbSMatt Jacob 	 */
270e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 1;
2712df76c16SMatt Jacob 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
2722df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
2732df76c16SMatt Jacob 		return (-EIO);
2742df76c16SMatt Jacob 	}
275ea6f23cdSMatt Jacob 
276ea6f23cdSMatt Jacob 	/*
277ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
278478f8a96SJustin T. Gibbs 	 */
2792df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2802df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2815d571944SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2822df76c16SMatt Jacob 		return (EIO);
283478f8a96SJustin T. Gibbs 	}
284478f8a96SJustin T. Gibbs 
2852df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2862df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2872df76c16SMatt Jacob 			goto unwind;
288ea6f23cdSMatt Jacob 		}
289ea6f23cdSMatt Jacob 	}
290ea6f23cdSMatt Jacob 
2912df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
29294dff771SMatt Jacob 	isp_timer_count = hz >> 2;
29394dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
2942df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
295ea6f23cdSMatt Jacob 
2962df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
2972df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2982df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
299ea6f23cdSMatt Jacob 	}
3002df76c16SMatt Jacob 	return (0);
3015d571944SMatt Jacob 
3022df76c16SMatt Jacob unwind:
3032df76c16SMatt Jacob 	while (--chan >= 0) {
3042df76c16SMatt Jacob 		struct cam_sim *sim;
3052df76c16SMatt Jacob 		struct cam_path *path;
3061d0a1de2SWill Andrews 
3071d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3081d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3092df76c16SMatt Jacob 		xpt_free_path(path);
3100a70657fSMatt Jacob 		ISP_LOCK(isp);
3110a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3122df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3132df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3142df76c16SMatt Jacob 	}
3152df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3160a70657fSMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3172df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3180a70657fSMatt Jacob 	}
3192df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3202df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3212df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3222df76c16SMatt Jacob 	}
3232df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3242df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3252df76c16SMatt Jacob 	return (-1);
3262df76c16SMatt Jacob }
3272df76c16SMatt Jacob 
328e95725cbSMatt Jacob int
3292df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3302df76c16SMatt Jacob {
331e95725cbSMatt Jacob 	struct cam_sim *sim;
3322df76c16SMatt Jacob 	int chan;
3332df76c16SMatt Jacob 
3340a70657fSMatt Jacob 	ISP_LOCK(isp);
3352df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
3361d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
337e95725cbSMatt Jacob 		if (sim->refcount > 2) {
3382df76c16SMatt Jacob 			ISP_UNLOCK(isp);
339e95725cbSMatt Jacob 			return (EBUSY);
340e95725cbSMatt Jacob 		}
341e95725cbSMatt Jacob 	}
3421d0a1de2SWill Andrews 	/* Tell spawned threads that we're exiting. */
3431d0a1de2SWill Andrews 	isp->isp_osinfo.is_exiting = 1;
344e95725cbSMatt Jacob 	if (isp->isp_osinfo.timer_active) {
345e95725cbSMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
346e95725cbSMatt Jacob 		isp->isp_osinfo.timer_active = 0;
347e95725cbSMatt Jacob 	}
3481d0a1de2SWill Andrews 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
3491d0a1de2SWill Andrews 		isp_detach_chan(isp, chan);
350387d8239SMatt Jacob 	ISP_UNLOCK(isp);
3511d0a1de2SWill Andrews 
3522df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3532df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3542df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3552df76c16SMatt Jacob 	}
3562df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3572df76c16SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3582df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3592df76c16SMatt Jacob 	}
360a035b0afSMatt Jacob 	if (isp->isp_osinfo.devq != NULL) {
3612df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
3622df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
3630a70657fSMatt Jacob 	}
364e95725cbSMatt Jacob 	return (0);
3655d571944SMatt Jacob }
3665d571944SMatt Jacob 
367f7c631bcSMatt Jacob static void
368e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
369fdeb9f2fSMatt Jacob {
3702df76c16SMatt Jacob 	if (IS_FC(isp)) {
3712df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
3722df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
373b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
374e561aa79SAlexander Motin 			    "Chan %d Freeze simq (loopdown)", chan);
3752df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
376c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
377c07b9e07SAlexander Motin 			xpt_hold_boot();
378c07b9e07SAlexander Motin #endif
3792df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
380fdeb9f2fSMatt Jacob 		} else {
381b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
382e561aa79SAlexander Motin 			    "Chan %d Mark simq frozen (loopdown)", chan);
3832df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3842df76c16SMatt Jacob 		}
385fdeb9f2fSMatt Jacob 	}
386fdeb9f2fSMatt Jacob }
387fdeb9f2fSMatt Jacob 
388e95725cbSMatt Jacob static void
389e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
390e95725cbSMatt Jacob {
391e95725cbSMatt Jacob 	if (IS_FC(isp)) {
392e95725cbSMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
393e95725cbSMatt Jacob 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
394e95725cbSMatt Jacob 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
395e95725cbSMatt Jacob 		if (wasfrozen && fc->simqfrozen == 0) {
396e561aa79SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
397e561aa79SAlexander Motin 			    "Chan %d Release simq", chan);
398e95725cbSMatt Jacob 			xpt_release_simq(fc->sim, 1);
399c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
400c07b9e07SAlexander Motin 			xpt_release_boot();
401c07b9e07SAlexander Motin #endif
402e95725cbSMatt Jacob 		}
403e95725cbSMatt Jacob 	}
404e95725cbSMatt Jacob }
405e95725cbSMatt Jacob 
4069cd7268eSMatt Jacob 
4075d571944SMatt Jacob static int
4082df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
4095d571944SMatt Jacob {
4109cd7268eSMatt Jacob 	ispsoftc_t *isp;
4112df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
4125d571944SMatt Jacob 
4132df76c16SMatt Jacob 	isp = dev->si_drv1;
4145d571944SMatt Jacob 
415c1504bc0SMatt Jacob 	switch (c) {
4165d571944SMatt Jacob 	case ISP_SDBLEV:
4175d571944SMatt Jacob 	{
4185d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
4195d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
4205d571944SMatt Jacob 		*(int *)addr = olddblev;
4215d571944SMatt Jacob 		retval = 0;
4225d571944SMatt Jacob 		break;
4235d571944SMatt Jacob 	}
424746e9c85SMatt Jacob 	case ISP_GETROLE:
4252df76c16SMatt Jacob 		chan = *(int *)addr;
4262df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4272df76c16SMatt Jacob 			retval = -ENXIO;
4282df76c16SMatt Jacob 			break;
4292df76c16SMatt Jacob 		}
4302df76c16SMatt Jacob 		if (IS_FC(isp)) {
4312df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4322df76c16SMatt Jacob 		} else {
4333e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4342df76c16SMatt Jacob 		}
435746e9c85SMatt Jacob 		retval = 0;
436746e9c85SMatt Jacob 		break;
437746e9c85SMatt Jacob 	case ISP_SETROLE:
4383e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4393e6deb33SAlexander Motin 			break;
440746e9c85SMatt Jacob 		nr = *(int *)addr;
4412df76c16SMatt Jacob 		chan = nr >> 8;
4422df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4432df76c16SMatt Jacob 			retval = -ENXIO;
4442df76c16SMatt Jacob 			break;
4452df76c16SMatt Jacob 		}
4462df76c16SMatt Jacob 		nr &= 0xff;
447746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
448746e9c85SMatt Jacob 			retval = EINVAL;
449746e9c85SMatt Jacob 			break;
450746e9c85SMatt Jacob 		}
4512df76c16SMatt Jacob 		ISP_LOCK(isp);
4526bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4536bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4546bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4552df76c16SMatt Jacob 		retval = 0;
4562df76c16SMatt Jacob 		break;
4572df76c16SMatt Jacob 
4585d571944SMatt Jacob 	case ISP_RESETHBA:
459511ced9bSMatt Jacob 		ISP_LOCK(isp);
4602df76c16SMatt Jacob 		isp_reinit(isp, 0);
461511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4625d571944SMatt Jacob 		retval = 0;
4635d571944SMatt Jacob 		break;
4642df76c16SMatt Jacob 
465f553351eSMatt Jacob 	case ISP_RESCAN:
4665d571944SMatt Jacob 		if (IS_FC(isp)) {
4672df76c16SMatt Jacob 			chan = *(int *)addr;
4682df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4692df76c16SMatt Jacob 				retval = -ENXIO;
4702df76c16SMatt Jacob 				break;
4712df76c16SMatt Jacob 			}
472511ced9bSMatt Jacob 			ISP_LOCK(isp);
473e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4745d571944SMatt Jacob 				retval = EIO;
4755d571944SMatt Jacob 			} else {
4765d571944SMatt Jacob 				retval = 0;
4775d571944SMatt Jacob 			}
478511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4795d571944SMatt Jacob 		}
4805d571944SMatt Jacob 		break;
4812df76c16SMatt Jacob 
4825d571944SMatt Jacob 	case ISP_FC_LIP:
4835d571944SMatt Jacob 		if (IS_FC(isp)) {
4842df76c16SMatt Jacob 			chan = *(int *)addr;
4852df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4862df76c16SMatt Jacob 				retval = -ENXIO;
4872df76c16SMatt Jacob 				break;
4882df76c16SMatt Jacob 			}
489511ced9bSMatt Jacob 			ISP_LOCK(isp);
4902df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4915d571944SMatt Jacob 				retval = EIO;
4925d571944SMatt Jacob 			} else {
4935d571944SMatt Jacob 				retval = 0;
4945d571944SMatt Jacob 			}
495511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4965d571944SMatt Jacob 		}
4975d571944SMatt Jacob 		break;
4985d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4995d571944SMatt Jacob 	{
5005d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
50110365e5aSMatt Jacob 		fcportdb_t *lp;
5025d571944SMatt Jacob 
50302e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
50402e2b2d9SMatt Jacob 			break;
50502e2b2d9SMatt Jacob 		}
5062e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
5075d571944SMatt Jacob 			retval = EINVAL;
5085d571944SMatt Jacob 			break;
5095d571944SMatt Jacob 		}
5102df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
511e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
512387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
51310365e5aSMatt Jacob 			ifc->loopid = lp->handle;
5145d571944SMatt Jacob 			ifc->portid = lp->portid;
5155d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
5165d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
5175d571944SMatt Jacob 			retval = 0;
5185d571944SMatt Jacob 		} else {
5195d571944SMatt Jacob 			retval = ENODEV;
5205d571944SMatt Jacob 		}
5215d571944SMatt Jacob 		break;
5225d571944SMatt Jacob 	}
5232903b272SMatt Jacob 	case ISP_GET_STATS:
5242903b272SMatt Jacob 	{
5252903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
5262903b272SMatt Jacob 
5272df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
5282903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
5292903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
5302903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
531511ced9bSMatt Jacob 		ISP_LOCK(isp);
5322903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
5332903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
5342903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
5352903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
5362903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
5372903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
5382903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
5392903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
540511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5412903b272SMatt Jacob 		retval = 0;
5422903b272SMatt Jacob 		break;
5432903b272SMatt Jacob 	}
5442903b272SMatt Jacob 	case ISP_CLR_STATS:
545511ced9bSMatt Jacob 		ISP_LOCK(isp);
5462903b272SMatt Jacob 		isp->isp_intcnt = 0;
5472903b272SMatt Jacob 		isp->isp_intbogus = 0;
5482903b272SMatt Jacob 		isp->isp_intmboxc = 0;
5492903b272SMatt Jacob 		isp->isp_intoasync = 0;
5502903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
5512903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
5522903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
5532903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
554511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5552903b272SMatt Jacob 		retval = 0;
5562903b272SMatt Jacob 		break;
557570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
558570c7a3fSMatt Jacob 	{
559570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5602df76c16SMatt Jacob 		int chan = hba->fc_channel;
56102e2b2d9SMatt Jacob 
5622df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5632df76c16SMatt Jacob 			retval = ENXIO;
5642df76c16SMatt Jacob 			break;
5652df76c16SMatt Jacob 		}
566a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
567a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
568a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5692df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
57002e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5712df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5722df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5732df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5742df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5752df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5762df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5772df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5782df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5792df76c16SMatt Jacob 		} else {
5802df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5812df76c16SMatt Jacob 			hba->fc_speed = 0;
5822df76c16SMatt Jacob 			hba->fc_topology = 0;
5832df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5842df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5852df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5862df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
58702e2b2d9SMatt Jacob 		}
588570c7a3fSMatt Jacob 		retval = 0;
589570c7a3fSMatt Jacob 		break;
590570c7a3fSMatt Jacob 	}
5918e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5928e62a8acSMatt Jacob 	{
5938e62a8acSMatt Jacob 		int needmarker;
5948e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
595c5fd36edSAlexander Motin 		uint16_t nphdl;
5968e62a8acSMatt Jacob 		mbreg_t mbs;
5978e62a8acSMatt Jacob 
5988e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5998e62a8acSMatt Jacob 			break;
6008e62a8acSMatt Jacob 		}
6018e62a8acSMatt Jacob 
6022df76c16SMatt Jacob 		chan = fct->chan;
6032df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
6042df76c16SMatt Jacob 			retval = -ENXIO;
6052df76c16SMatt Jacob 			break;
6062df76c16SMatt Jacob 		}
6072df76c16SMatt Jacob 
6088e62a8acSMatt Jacob 		needmarker = retval = 0;
609c5fd36edSAlexander Motin 		nphdl = fct->loopid;
6102df76c16SMatt Jacob 		ISP_LOCK(isp);
6112df76c16SMatt Jacob 		if (IS_24XX(isp)) {
61253791a95SAlexander Motin 			void *reqp;
61353791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
61453791a95SAlexander Motin 			isp24xx_tmf_t tmf;
61553791a95SAlexander Motin 			isp24xx_statusreq_t sp;
6162df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
6172df76c16SMatt Jacob 			fcportdb_t *lp;
6182df76c16SMatt Jacob 			int i;
6192df76c16SMatt Jacob 
6202df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
6212df76c16SMatt Jacob 				lp = &fcp->portdb[i];
622c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
6232df76c16SMatt Jacob 					break;
6242df76c16SMatt Jacob 				}
6252df76c16SMatt Jacob 			}
6262df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
6272df76c16SMatt Jacob 				retval = ENXIO;
6282df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6292df76c16SMatt Jacob 				break;
6302df76c16SMatt Jacob 			}
63153791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
63253791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
63353791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
63453791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
63553791a95SAlexander Motin 			tmf.tmf_delay = 2;
63653791a95SAlexander Motin 			tmf.tmf_timeout = 4;
63753791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
63853791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
63953791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
64053791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
6412df76c16SMatt Jacob 			if (fct->lun >= 256) {
64253791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
6432df76c16SMatt Jacob 			}
6442df76c16SMatt Jacob 			switch (fct->action) {
6452df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
64653791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
6472df76c16SMatt Jacob 				break;
6482df76c16SMatt Jacob 			case IPT_TARGET_RESET:
64953791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
6502df76c16SMatt Jacob 				needmarker = 1;
6512df76c16SMatt Jacob 				break;
6522df76c16SMatt Jacob 			case IPT_LUN_RESET:
65353791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
6542df76c16SMatt Jacob 				needmarker = 1;
6552df76c16SMatt Jacob 				break;
6562df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
65753791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6582df76c16SMatt Jacob 				needmarker = 1;
6592df76c16SMatt Jacob 				break;
6602df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
66153791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6622df76c16SMatt Jacob 				needmarker = 1;
6632df76c16SMatt Jacob 				break;
6642df76c16SMatt Jacob 			default:
6652df76c16SMatt Jacob 				retval = EINVAL;
6662df76c16SMatt Jacob 				break;
6672df76c16SMatt Jacob 			}
6682df76c16SMatt Jacob 			if (retval) {
6692df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6702df76c16SMatt Jacob 				break;
6712df76c16SMatt Jacob 			}
6722df76c16SMatt Jacob 
67353791a95SAlexander Motin 			/* Prepare space for response in memory */
67453791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
67553791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
67653791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
67753791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
67853791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
67953791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
68053791a95SAlexander Motin 				    __func__, chan);
6812df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6822df76c16SMatt Jacob 				retval = ENOMEM;
6832df76c16SMatt Jacob 				break;
6842df76c16SMatt Jacob 			}
68553791a95SAlexander Motin 
68653791a95SAlexander Motin 			/* Send request and wait for response. */
68753791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
68853791a95SAlexander Motin 			if (reqp == NULL) {
68953791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
69053791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
69153791a95SAlexander Motin 				    __func__, chan);
69253791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
69353791a95SAlexander Motin 				ISP_UNLOCK(isp);
6942df76c16SMatt Jacob 				retval = EIO;
69553791a95SAlexander Motin 				break;
6962df76c16SMatt Jacob 			}
69753791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
69853791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
69953791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
70053791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
70153791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
70253791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
70353791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
70453791a95SAlexander Motin 				    __func__, chan);
70553791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
70653791a95SAlexander Motin 				ISP_UNLOCK(isp);
70753791a95SAlexander Motin 				retval = EIO;
70853791a95SAlexander Motin 				break;
70953791a95SAlexander Motin 			}
71053791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
71153791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
71253791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
71353791a95SAlexander Motin 
71453791a95SAlexander Motin 			if (sp.req_completion_status != 0)
71553791a95SAlexander Motin 				retval = EIO;
71653791a95SAlexander Motin 			else if (needmarker)
7172df76c16SMatt Jacob 				fcp->sendmarker = 1;
7182df76c16SMatt Jacob 		} else {
7192df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
7202df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
721c5fd36edSAlexander Motin 				nphdl <<= 8;
722e5265237SMatt Jacob 			}
7238e62a8acSMatt Jacob 			switch (fct->action) {
724f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
7258e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
726c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7278e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7288e62a8acSMatt Jacob 				break;
729f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
7308e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
731c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7328e62a8acSMatt Jacob 				needmarker = 1;
7338e62a8acSMatt Jacob 				break;
734f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
7358e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
736c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7378e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7388e62a8acSMatt Jacob 				needmarker = 1;
7398e62a8acSMatt Jacob 				break;
740f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
7418e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
742c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7438e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7448e62a8acSMatt Jacob 				needmarker = 1;
7458e62a8acSMatt Jacob 				break;
746f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
7478e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
748c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7498e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7508e62a8acSMatt Jacob 				needmarker = 1;
7518e62a8acSMatt Jacob 				break;
7528e62a8acSMatt Jacob 			default:
7538e62a8acSMatt Jacob 				retval = EINVAL;
7548e62a8acSMatt Jacob 				break;
7558e62a8acSMatt Jacob 			}
7568e62a8acSMatt Jacob 			if (retval == 0) {
7578e62a8acSMatt Jacob 				if (needmarker) {
7582df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7598e62a8acSMatt Jacob 				}
7608e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7612df76c16SMatt Jacob 				if (retval) {
7628e62a8acSMatt Jacob 					retval = EIO;
7638e62a8acSMatt Jacob 				}
7642df76c16SMatt Jacob 			}
7652df76c16SMatt Jacob 		}
7662df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7678e62a8acSMatt Jacob 		break;
7688e62a8acSMatt Jacob 	}
7695d571944SMatt Jacob 	default:
7705d571944SMatt Jacob 		break;
7715d571944SMatt Jacob 	}
7725d571944SMatt Jacob 	return (retval);
7730470d791SMatt Jacob }
774478f8a96SJustin T. Gibbs 
775f6e75de2SMatt Jacob static void
776f6e75de2SMatt Jacob isp_intr_enable(void *arg)
777f6e75de2SMatt Jacob {
7782df76c16SMatt Jacob 	int chan;
7799cd7268eSMatt Jacob 	ispsoftc_t *isp = arg;
7800a70657fSMatt Jacob 	ISP_LOCK(isp);
7812df76c16SMatt Jacob 	if (IS_FC(isp)) {
7823e6deb33SAlexander Motin 		for (chan = 0; chan < isp->isp_nchan; chan++) {
7832df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
78410365e5aSMatt Jacob 				ISP_ENABLE_INTS(isp);
7852df76c16SMatt Jacob 				break;
7862df76c16SMatt Jacob 			}
7873e6deb33SAlexander Motin 		}
7882df76c16SMatt Jacob 	} else {
7892df76c16SMatt Jacob 		ISP_ENABLE_INTS(isp);
790d6e5500fSMatt Jacob 	}
791e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 0;
7920a70657fSMatt Jacob 	ISP_UNLOCK(isp);
793f6e75de2SMatt Jacob 	/* Release our hook so that the boot can continue. */
794f6e75de2SMatt Jacob 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
795f6e75de2SMatt Jacob }
796d81ba9d5SMatt Jacob 
797d81ba9d5SMatt Jacob /*
7982df76c16SMatt Jacob  * Local Inlines
7992df76c16SMatt Jacob  */
8002df76c16SMatt Jacob 
8012df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
8022df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
8032df76c16SMatt Jacob 
8042df76c16SMatt Jacob static ISP_INLINE int
8052df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
8062df76c16SMatt Jacob {
8072df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
8082df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
8092df76c16SMatt Jacob 		return (-1);
8102df76c16SMatt Jacob 	}
8112df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
8122df76c16SMatt Jacob 	return (0);
8132df76c16SMatt Jacob }
8142df76c16SMatt Jacob 
8152df76c16SMatt Jacob static ISP_INLINE void
8162df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
8172df76c16SMatt Jacob {
818387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
819405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
820405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
821405b7a29SMatt Jacob 		PISP_PCMD(ccb)->totslen = 0;
822405b7a29SMatt Jacob 		PISP_PCMD(ccb)->cumslen = 0;
823405b7a29SMatt Jacob 		PISP_PCMD(ccb)->crn = 0;
824405b7a29SMatt Jacob #endif
825405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
8262df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
8272df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
8282df76c16SMatt Jacob 	}
829387d8239SMatt Jacob }
830387d8239SMatt Jacob 
8312df76c16SMatt Jacob /*
832d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
833d81ba9d5SMatt Jacob  */
834d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
8352df76c16SMatt Jacob static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
8362df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
8372df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
8382df76c16SMatt Jacob static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
8392df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
8402df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
841523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
8422df76c16SMatt Jacob static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
8432df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
8442df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
8452df76c16SMatt Jacob static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
8462df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
8479cd7268eSMatt Jacob static void destroy_lun_state(ispsoftc_t *, tstate_t *);
8482df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
8492df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
850f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
851387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
852a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
853a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
85494dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
855387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
8562df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
8572df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
8582df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
8592df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
8602df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
86196b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
8622df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
8632df76c16SMatt Jacob static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
8642df76c16SMatt Jacob static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
865d81ba9d5SMatt Jacob 
8662df76c16SMatt Jacob static ISP_INLINE int
8679cd7268eSMatt Jacob is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
868d81ba9d5SMatt Jacob {
869d81ba9d5SMatt Jacob 	tstate_t *tptr;
8702df76c16SMatt Jacob 	struct tslist *lhp;
8712df76c16SMatt Jacob 
8722df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
8732df76c16SMatt Jacob 	SLIST_FOREACH(tptr, lhp, next) {
874315a4d6fSAlexander Motin 		if (tptr->ts_lun == lun) {
875d81ba9d5SMatt Jacob 			return (1);
876d81ba9d5SMatt Jacob 		}
8772df76c16SMatt Jacob 	}
878d81ba9d5SMatt Jacob 	return (0);
879d81ba9d5SMatt Jacob }
880d81ba9d5SMatt Jacob 
8812df76c16SMatt Jacob static void
8822df76c16SMatt Jacob dump_tstates(ispsoftc_t *isp, int bus)
883d81ba9d5SMatt Jacob {
8842df76c16SMatt Jacob 	int i, j;
8852df76c16SMatt Jacob 	struct tslist *lhp;
8862df76c16SMatt Jacob 	tstate_t *tptr = NULL;
8872df76c16SMatt Jacob 
8882df76c16SMatt Jacob 	if (bus >= isp->isp_nchan) {
8892df76c16SMatt Jacob 		return;
890a1bc34c6SMatt Jacob 	}
8912df76c16SMatt Jacob 	for (i = 0; i < LUN_HASH_SIZE; i++) {
8922df76c16SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8932df76c16SMatt Jacob 		j = 0;
8942df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
8952df76c16SMatt Jacob 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
8962df76c16SMatt Jacob 			j++;
897d81ba9d5SMatt Jacob 		}
898d81ba9d5SMatt Jacob 	}
899d81ba9d5SMatt Jacob }
900d81ba9d5SMatt Jacob 
9012df76c16SMatt Jacob static ISP_INLINE tstate_t *
9029cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
903d81ba9d5SMatt Jacob {
90464edff94SMatt Jacob 	tstate_t *tptr = NULL;
9052df76c16SMatt Jacob 	struct tslist *lhp;
906d81ba9d5SMatt Jacob 
9072df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
908315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
9092df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
910315a4d6fSAlexander Motin 			if (tptr->ts_lun == lun) {
911d81ba9d5SMatt Jacob 				tptr->hold++;
912d81ba9d5SMatt Jacob 				return (tptr);
913d81ba9d5SMatt Jacob 			}
91464edff94SMatt Jacob 		}
915126ec864SMatt Jacob 	}
9162df76c16SMatt Jacob 	return (NULL);
9172df76c16SMatt Jacob }
918d81ba9d5SMatt Jacob 
9192df76c16SMatt Jacob static ISP_INLINE tstate_t *
9202df76c16SMatt Jacob get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
9212df76c16SMatt Jacob {
9222df76c16SMatt Jacob 	tstate_t *tptr = NULL;
9232df76c16SMatt Jacob 	atio_private_data_t *atp;
9242df76c16SMatt Jacob 	struct tslist *lhp;
9252df76c16SMatt Jacob 	int i;
9262df76c16SMatt Jacob 
9272df76c16SMatt Jacob 	if (bus < isp->isp_nchan && tagval != 0) {
9282df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9292df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9302df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
931523ea374SAlexander Motin 				atp = isp_find_atpd(isp, tptr, tagval);
932523ea374SAlexander Motin 				if (atp) {
933d81ba9d5SMatt Jacob 					tptr->hold++;
934d81ba9d5SMatt Jacob 					return (tptr);
935d81ba9d5SMatt Jacob 				}
9362df76c16SMatt Jacob 			}
9372df76c16SMatt Jacob 		}
9382df76c16SMatt Jacob 	}
9392df76c16SMatt Jacob 	return (NULL);
940d81ba9d5SMatt Jacob }
941d81ba9d5SMatt Jacob 
9422df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
9432df76c16SMatt Jacob get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
9442df76c16SMatt Jacob {
9452df76c16SMatt Jacob 	inot_private_data_t *ntp;
9462df76c16SMatt Jacob 	tstate_t *tptr;
9472df76c16SMatt Jacob 	struct tslist *lhp;
9482df76c16SMatt Jacob 	int bus, i;
9492df76c16SMatt Jacob 
9502df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9512df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9522df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9532df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
9542df76c16SMatt Jacob 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
9552df76c16SMatt Jacob 				if (ntp) {
9562df76c16SMatt Jacob 					*rslt = tptr;
9572df76c16SMatt Jacob 					tptr->hold++;
9582df76c16SMatt Jacob 					return (ntp);
9592df76c16SMatt Jacob 				}
9602df76c16SMatt Jacob 			}
9612df76c16SMatt Jacob 		}
9622df76c16SMatt Jacob 	}
9632df76c16SMatt Jacob 	return (NULL);
9642df76c16SMatt Jacob }
965e2873b76SMatt Jacob 
9662df76c16SMatt Jacob static ISP_INLINE void
9679cd7268eSMatt Jacob rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
968d81ba9d5SMatt Jacob {
9692df76c16SMatt Jacob 	KASSERT((tptr->hold), ("tptr not held"));
970d81ba9d5SMatt Jacob 	tptr->hold--;
971d81ba9d5SMatt Jacob }
972d81ba9d5SMatt Jacob 
9732df76c16SMatt Jacob static void
9742df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
9752df76c16SMatt Jacob {
9762df76c16SMatt Jacob 	inot_private_data_t *ntp;
97794dff771SMatt Jacob 	inot_private_data_t *restart_queue;
9782df76c16SMatt Jacob 	tstate_t *tptr;
97994dff771SMatt Jacob 	union ccb *ccb;
9802df76c16SMatt Jacob 	struct tslist *lhp;
981*09ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
9822df76c16SMatt Jacob 	int bus, i;
9832df76c16SMatt Jacob 
9842df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9852df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9862df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9872df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
98894dff771SMatt Jacob 				if ((restart_queue = tptr->restart_queue) != NULL)
9892df76c16SMatt Jacob 					tptr->restart_queue = NULL;
9902df76c16SMatt Jacob 				while (restart_queue) {
9912df76c16SMatt Jacob 					ntp = restart_queue;
9922df76c16SMatt Jacob 					restart_queue = ntp->rd.nt.nt_hba;
9932df76c16SMatt Jacob 					if (IS_24XX(isp)) {
9942df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
9952df76c16SMatt Jacob 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
9962df76c16SMatt Jacob 					} else {
9972df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
9982df76c16SMatt Jacob 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
9992df76c16SMatt Jacob 					}
10002df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
10012df76c16SMatt Jacob 					if (tptr->restart_queue && restart_queue != NULL) {
10022df76c16SMatt Jacob 						ntp = tptr->restart_queue;
10032df76c16SMatt Jacob 						tptr->restart_queue = restart_queue;
10042df76c16SMatt Jacob 						while (restart_queue->rd.nt.nt_hba) {
10052df76c16SMatt Jacob 							restart_queue = restart_queue->rd.nt.nt_hba;
10062df76c16SMatt Jacob 						}
10072df76c16SMatt Jacob 						restart_queue->rd.nt.nt_hba = ntp;
10082df76c16SMatt Jacob 						break;
10092df76c16SMatt Jacob 					}
10102df76c16SMatt Jacob 				}
1011*09ddc7adSAlexander Motin 			}
1012*09ddc7adSAlexander Motin 		}
1013*09ddc7adSAlexander Motin 
101494dff771SMatt Jacob 		/*
1015*09ddc7adSAlexander Motin 		 * We only need to do this once per channel.
101694dff771SMatt Jacob 		 */
1017*09ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
1018*09ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
1019*09ddc7adSAlexander Motin 		if (ccb != NULL) {
1020*09ddc7adSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
102194dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
102294dff771SMatt Jacob 		}
10232df76c16SMatt Jacob 	}
10242df76c16SMatt Jacob }
10252df76c16SMatt Jacob 
10262df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *
10272df76c16SMatt Jacob isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
102853036e92SMatt Jacob {
102953036e92SMatt Jacob 	atio_private_data_t *atp;
10302df76c16SMatt Jacob 
1031523ea374SAlexander Motin 	atp = LIST_FIRST(&tptr->atfree);
10322df76c16SMatt Jacob 	if (atp) {
1033523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
1034523ea374SAlexander Motin 		atp->tag = tag;
1035523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
10362df76c16SMatt Jacob 	}
103753036e92SMatt Jacob 	return (atp);
103853036e92SMatt Jacob }
1039523ea374SAlexander Motin 
1040523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *
1041523ea374SAlexander Motin isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1042523ea374SAlexander Motin {
1043523ea374SAlexander Motin 	atio_private_data_t *atp;
1044523ea374SAlexander Motin 
1045523ea374SAlexander Motin 	LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1046523ea374SAlexander Motin 		if (atp->tag == tag)
10472df76c16SMatt Jacob 			return (atp);
10482df76c16SMatt Jacob 	}
104953036e92SMatt Jacob 	return (NULL);
105053036e92SMatt Jacob }
105153036e92SMatt Jacob 
10522df76c16SMatt Jacob static ISP_INLINE void
10532df76c16SMatt Jacob isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
10542df76c16SMatt Jacob {
1055387d8239SMatt Jacob 	if (atp->ests) {
1056387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
1057387d8239SMatt Jacob 	}
1058523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
1059387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
1060523ea374SAlexander Motin 	LIST_INSERT_HEAD(&tptr->atfree, atp, next);
10612df76c16SMatt Jacob }
10622df76c16SMatt Jacob 
10632df76c16SMatt Jacob static void
10642df76c16SMatt Jacob isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
10652df76c16SMatt Jacob {
10662df76c16SMatt Jacob 	atio_private_data_t *atp;
10672df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
10682df76c16SMatt Jacob 
10692df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1070c5fd36edSAlexander Motin 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %x nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
107194dff771SMatt Jacob 		    atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
10722df76c16SMatt Jacob 	}
10732df76c16SMatt Jacob }
10742df76c16SMatt Jacob 
10752df76c16SMatt Jacob 
10762df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
10772df76c16SMatt Jacob isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
10782df76c16SMatt Jacob {
10792df76c16SMatt Jacob 	inot_private_data_t *ntp;
10802df76c16SMatt Jacob 	ntp = tptr->ntfree;
10812df76c16SMatt Jacob 	if (ntp) {
10822df76c16SMatt Jacob 		tptr->ntfree = ntp->next;
10832df76c16SMatt Jacob 	}
10842df76c16SMatt Jacob 	return (ntp);
10852df76c16SMatt Jacob }
10862df76c16SMatt Jacob 
10872df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
10882df76c16SMatt Jacob isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
10892df76c16SMatt Jacob {
10902df76c16SMatt Jacob 	inot_private_data_t *ntp;
10912df76c16SMatt Jacob 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
10922df76c16SMatt Jacob 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
10932df76c16SMatt Jacob 			return (ntp);
10942df76c16SMatt Jacob 		}
10952df76c16SMatt Jacob 	}
10962df76c16SMatt Jacob 	return (NULL);
10972df76c16SMatt Jacob }
10982df76c16SMatt Jacob 
10992df76c16SMatt Jacob static ISP_INLINE void
11002df76c16SMatt Jacob isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
11012df76c16SMatt Jacob {
11022df76c16SMatt Jacob 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
11032df76c16SMatt Jacob 	ntp->next = tptr->ntfree;
11042df76c16SMatt Jacob 	tptr->ntfree = ntp;
11052df76c16SMatt Jacob }
11062df76c16SMatt Jacob 
1107d81ba9d5SMatt Jacob static cam_status
11082df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1109d81ba9d5SMatt Jacob {
1110d81ba9d5SMatt Jacob 	cam_status status;
1111d81ba9d5SMatt Jacob 	lun_id_t lun;
11122df76c16SMatt Jacob 	struct tslist *lhp;
11132df76c16SMatt Jacob 	tstate_t *tptr;
11142df76c16SMatt Jacob 	int i;
1115d81ba9d5SMatt Jacob 
1116d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
11172df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
11186af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1119d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
1120d81ba9d5SMatt Jacob 		}
11212df76c16SMatt Jacob 	}
1122a1bc34c6SMatt Jacob 	if (is_lun_enabled(isp, bus, lun)) {
1123d81ba9d5SMatt Jacob 		return (CAM_LUN_ALRDY_ENA);
1124d81ba9d5SMatt Jacob 	}
1125e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
11262df76c16SMatt Jacob 	if (tptr == NULL) {
1127d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
1128d81ba9d5SMatt Jacob 	}
1129315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
11302df76c16SMatt Jacob 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1131d81ba9d5SMatt Jacob 	if (status != CAM_REQ_CMP) {
11322df76c16SMatt Jacob 		free(tptr, M_DEVBUF);
1133d81ba9d5SMatt Jacob 		return (status);
1134d81ba9d5SMatt Jacob 	}
11352df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
11362df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
1137523ea374SAlexander Motin 	LIST_INIT(&tptr->atfree);
1138523ea374SAlexander Motin 	for (i = ATPDPSIZE-1; i >= 0; i--)
1139523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1140523ea374SAlexander Motin 	for (i = 0; i < ATPDPHASHSIZE; i++)
1141523ea374SAlexander Motin 		LIST_INIT(&tptr->atused[i]);
1142523ea374SAlexander Motin 	for (i = 0; i < ATPDPSIZE-1; i++)
11432df76c16SMatt Jacob 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
11442df76c16SMatt Jacob 	tptr->ntfree = tptr->ntpool;
11452df76c16SMatt Jacob 	tptr->hold = 1;
1146315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
11472df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
11482df76c16SMatt Jacob 	*rslt = tptr;
11492df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1150d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1151d81ba9d5SMatt Jacob }
1152d81ba9d5SMatt Jacob 
11532df76c16SMatt Jacob static ISP_INLINE void
11549cd7268eSMatt Jacob destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1155d81ba9d5SMatt Jacob {
1156344aebe2SMatt Jacob 	union ccb *ccb;
11572df76c16SMatt Jacob 	struct tslist *lhp;
1158e2873b76SMatt Jacob 
115945210a25SAlexander Motin 	KASSERT((tptr->hold != 0), ("tptr is not held"));
116045210a25SAlexander Motin 	KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1161344aebe2SMatt Jacob 	do {
1162344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1163344aebe2SMatt Jacob 		if (ccb) {
1164344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1165344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1166344aebe2SMatt Jacob 			xpt_done(ccb);
1167344aebe2SMatt Jacob 		}
1168344aebe2SMatt Jacob 	} while (ccb);
1169344aebe2SMatt Jacob 	do {
1170344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1171344aebe2SMatt Jacob 		if (ccb) {
1172344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1173344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1174344aebe2SMatt Jacob 			xpt_done(ccb);
1175344aebe2SMatt Jacob 		}
1176344aebe2SMatt Jacob 	} while (ccb);
1177315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
11782df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1179e2873b76SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
11802df76c16SMatt Jacob 	xpt_free_path(tptr->owner);
1181d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1182d81ba9d5SMatt Jacob }
1183d81ba9d5SMatt Jacob 
11842df76c16SMatt Jacob static void
11852df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1186d81ba9d5SMatt Jacob {
11873e6deb33SAlexander Motin 	tstate_t *tptr;
11883e6deb33SAlexander Motin 	int bus;
11892df76c16SMatt Jacob 	target_id_t target;
1190d81ba9d5SMatt Jacob 	lun_id_t lun;
1191d81ba9d5SMatt Jacob 
11923e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
11933e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
11943e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
11953e6deb33SAlexander Motin 		xpt_done(ccb);
11963e6deb33SAlexander Motin 		return;
11973e6deb33SAlexander Motin 	}
1198e2873b76SMatt Jacob 
1199746e9c85SMatt Jacob 	/*
12003e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
12013e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1202746e9c85SMatt Jacob 	 */
12032df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12042df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12052df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
12066af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
12076af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
12083e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
12092df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12102df76c16SMatt Jacob 		xpt_done(ccb);
12112df76c16SMatt Jacob 		return;
12122df76c16SMatt Jacob 	}
12132df76c16SMatt Jacob 
12143e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1215a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
12162df76c16SMatt Jacob 	if (tptr) {
12172df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
12183e6deb33SAlexander Motin 		xpt_done(ccb);
12193e6deb33SAlexander Motin 		return;
1220d81ba9d5SMatt Jacob 	}
12212df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
12222df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
12232df76c16SMatt Jacob 		xpt_done(ccb);
12243e6deb33SAlexander Motin 		return;
12252df76c16SMatt Jacob 	}
12262df76c16SMatt Jacob 
12273e6deb33SAlexander Motin 	rls_lun_statep(isp, tptr);
12283e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
12293e6deb33SAlexander Motin 	xpt_done(ccb);
12302df76c16SMatt Jacob }
12312df76c16SMatt Jacob 
12322df76c16SMatt Jacob static void
12332df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
12342df76c16SMatt Jacob {
12352df76c16SMatt Jacob 	tstate_t *tptr = NULL;
12362df76c16SMatt Jacob 	int bus;
12372df76c16SMatt Jacob 	target_id_t target;
12382df76c16SMatt Jacob 	lun_id_t lun;
12392df76c16SMatt Jacob 
12402df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12412df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12422df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
12436af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
12446af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
12453e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
12462df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12472df76c16SMatt Jacob 		xpt_done(ccb);
12482df76c16SMatt Jacob 		return;
12492df76c16SMatt Jacob 	}
12502df76c16SMatt Jacob 
12513e6deb33SAlexander Motin 	/* Find the state pointer. */
12522df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
12533e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
12542df76c16SMatt Jacob 		xpt_done(ccb);
12553e6deb33SAlexander Motin 		return;
12562df76c16SMatt Jacob 	}
12572df76c16SMatt Jacob 
12583e6deb33SAlexander Motin 	destroy_lun_state(isp, tptr);
12593e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
12603e6deb33SAlexander Motin 	xpt_done(ccb);
1261d81ba9d5SMatt Jacob }
1262d81ba9d5SMatt Jacob 
12639cd7268eSMatt Jacob static void
1264387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1265d81ba9d5SMatt Jacob {
126694dff771SMatt Jacob 	int fctape, sendstatus, resid;
12672df76c16SMatt Jacob 	tstate_t *tptr;
1268387d8239SMatt Jacob 	fcparam *fcp;
12692df76c16SMatt Jacob 	atio_private_data_t *atp;
127094dff771SMatt Jacob 	struct ccb_scsiio *cso;
1271*09ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
127294dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
12731dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1274d81ba9d5SMatt Jacob 
127594dff771SMatt Jacob 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
127694dff771SMatt Jacob 	if (tptr == NULL) {
127794dff771SMatt Jacob 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
127894dff771SMatt Jacob 		if (tptr == NULL) {
127994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
128094dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
128194dff771SMatt Jacob 			xpt_done(ccb);
128294dff771SMatt Jacob 			return;
128394dff771SMatt Jacob 		}
128494dff771SMatt Jacob 	}
128594dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
128694dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
128794dff771SMatt Jacob 
1288*09ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
128994dff771SMatt Jacob 	switch (how) {
129094dff771SMatt Jacob 	case FROM_CAM:
12912df76c16SMatt Jacob 		/*
129294dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
12932df76c16SMatt Jacob 		 */
1294*09ddc7adSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
129594dff771SMatt Jacob 		break;
1296*09ddc7adSAlexander Motin 	case FROM_TIMER:
129794dff771SMatt Jacob 	case FROM_SRR:
129894dff771SMatt Jacob 	case FROM_CTIO_DONE:
1299*09ddc7adSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
130094dff771SMatt Jacob 		break;
130194dff771SMatt Jacob 	}
130294dff771SMatt Jacob 
1303*09ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1304*09ddc7adSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
130594dff771SMatt Jacob 
130694dff771SMatt Jacob 		cso = &ccb->csio;
1307387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1308387d8239SMatt Jacob 		if (xfrlen == 0) {
13092df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1310387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
13112df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
13122df76c16SMatt Jacob 				xpt_done(ccb);
131394dff771SMatt Jacob 				continue;
13142df76c16SMatt Jacob 			}
13152df76c16SMatt Jacob 		}
13162df76c16SMatt Jacob 
1317523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, cso->tag_id);
13182df76c16SMatt Jacob 		if (atp == NULL) {
131994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
13202df76c16SMatt Jacob 			isp_dump_atpd(isp, tptr);
13212df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
13222df76c16SMatt Jacob 			xpt_done(ccb);
132394dff771SMatt Jacob 			continue;
13242df76c16SMatt Jacob 		}
1325387d8239SMatt Jacob 
1326387d8239SMatt Jacob 		/*
1327387d8239SMatt Jacob 		 * Is this command a dead duck?
1328387d8239SMatt Jacob 		 */
13292df76c16SMatt Jacob 		if (atp->dead) {
133094dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
13312df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
13322df76c16SMatt Jacob 			xpt_done(ccb);
133394dff771SMatt Jacob 			continue;
13342df76c16SMatt Jacob 		}
13352df76c16SMatt Jacob 
13362df76c16SMatt Jacob 		/*
13372df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
13382df76c16SMatt Jacob 		 */
1339387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1340387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
134194dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
13422df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
13432df76c16SMatt Jacob 			xpt_done(ccb);
134494dff771SMatt Jacob 			continue;
13452df76c16SMatt Jacob 		}
13462df76c16SMatt Jacob 
13472df76c16SMatt Jacob 		/*
134894dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
134994dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1350387d8239SMatt Jacob 		 */
135194dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
135294dff771SMatt Jacob 			isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1353*09ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
135494dff771SMatt Jacob 			break;
1355387d8239SMatt Jacob 		}
1356387d8239SMatt Jacob 
1357d81ba9d5SMatt Jacob 		/*
1358387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1359d81ba9d5SMatt Jacob 		 */
1360387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1361387d8239SMatt Jacob 			fctape = 1;
1362387d8239SMatt Jacob 		} else {
1363387d8239SMatt Jacob 			fctape = 0;
1364387d8239SMatt Jacob 		}
1365387d8239SMatt Jacob 
1366387d8239SMatt Jacob 		/*
1367387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1368387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1369387d8239SMatt Jacob 		 */
1370387d8239SMatt Jacob 		if (atp->sendst) {
1371daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
137294dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1373387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1374387d8239SMatt Jacob 			atp->sendst = 0;
1375387d8239SMatt Jacob 		}
1376387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1377387d8239SMatt Jacob 			sendstatus = 1;
1378387d8239SMatt Jacob 		} else {
1379387d8239SMatt Jacob 			sendstatus = 0;
1380387d8239SMatt Jacob 		}
1381387d8239SMatt Jacob 
1382387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
138394dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1384387d8239SMatt Jacob 			/*
1385387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1386387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1387387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1388387d8239SMatt Jacob 			 */
1389387d8239SMatt Jacob 			sense_length = cso->sense_len;
1390387d8239SMatt Jacob 
1391387d8239SMatt Jacob 			/*
1392387d8239SMatt Jacob 			 * This 'cannot' happen
1393387d8239SMatt Jacob 			 */
1394387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1395387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1396387d8239SMatt Jacob 			}
1397387d8239SMatt Jacob 		} else {
1398387d8239SMatt Jacob 			sense_length = 0;
1399387d8239SMatt Jacob 		}
1400387d8239SMatt Jacob 
140194dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
140294dff771SMatt Jacob 
140394dff771SMatt Jacob 		/*
140494dff771SMatt Jacob 		 * Check for overflow
140594dff771SMatt Jacob 		 */
140694dff771SMatt Jacob 		tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
140794dff771SMatt Jacob 		if (tmp > atp->orig_datalen) {
140894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
140994dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
141094dff771SMatt Jacob 			xpt_done(ccb);
141194dff771SMatt Jacob 			continue;
141294dff771SMatt Jacob 		}
1413387d8239SMatt Jacob 
14142df76c16SMatt Jacob 		if (IS_24XX(isp)) {
14152df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1416d81ba9d5SMatt Jacob 
14172df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
14182df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
141994dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
142094dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
14212df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
14222df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
14232df76c16SMatt Jacob 			cto->ct_iid_lo = atp->portid;
14242df76c16SMatt Jacob 			cto->ct_iid_hi = atp->portid >> 16;
14252df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
14262df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1427a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
14282df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1429387d8239SMatt Jacob 
1430387d8239SMatt Jacob 			/*
1431387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
14326f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1433387d8239SMatt Jacob 			 *
14348b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
14358b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1436387d8239SMatt Jacob 			 *
1437387d8239SMatt Jacob 			 */
1438387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1439387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
144094dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1441387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1442387d8239SMatt Jacob 					if (resid < 0) {
1443387d8239SMatt Jacob 						cto->ct_resid = -resid;
1444387d8239SMatt Jacob 					} else if (resid > 0) {
1445387d8239SMatt Jacob 						cto->ct_resid = resid;
14462df76c16SMatt Jacob 					}
1447387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1448387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1449387d8239SMatt Jacob 					if (resid < 0) {
1450387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1451387d8239SMatt Jacob 					} else if (resid > 0) {
1452387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1453387d8239SMatt Jacob 					}
1454387d8239SMatt Jacob 					if (fctape) {
1455387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1456387d8239SMatt Jacob 					}
1457387d8239SMatt Jacob 					if (sense_length) {
14582df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1459387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1460387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
14612df76c16SMatt Jacob 					}
14622df76c16SMatt Jacob 				} else {
1463387d8239SMatt Jacob 					bus_addr_t addr;
1464387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1465387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1466387d8239SMatt Jacob 
1467387d8239SMatt Jacob 					if (atp->ests == NULL) {
1468387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1469387d8239SMatt Jacob 						if (atp->ests == NULL) {
1470*09ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
147194dff771SMatt Jacob 							break;
1472387d8239SMatt Jacob 						}
1473387d8239SMatt Jacob 					}
1474387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1475387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1476387d8239SMatt Jacob 					if (fctape) {
1477387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1478387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1479387d8239SMatt Jacob 					}
1480387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1481387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1482387d8239SMatt Jacob 					if (resid < 0) {
1483387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1484387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1485387d8239SMatt Jacob 					} else if (resid > 0) {
1486387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1487387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1488387d8239SMatt Jacob 					}
1489387d8239SMatt Jacob 					if (sense_length) {
1490387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1491387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1492387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1493387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1494387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1495387d8239SMatt Jacob 					} else {
1496387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1497387d8239SMatt Jacob 					}
1498387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1499387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1500387d8239SMatt Jacob 					}
1501387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1502387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
150394dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1504387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1505387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1506387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1507387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1508387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1509387d8239SMatt Jacob 				}
1510387d8239SMatt Jacob 				if (sense_length) {
151194dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
151294dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
151394dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1514387d8239SMatt Jacob 				} else {
151594dff771SMatt Jacob 					isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
151694dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1517387d8239SMatt Jacob 				}
1518387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1519387d8239SMatt Jacob 			}
1520387d8239SMatt Jacob 
1521387d8239SMatt Jacob 			/*
1522387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1523387d8239SMatt Jacob 			 */
1524387d8239SMatt Jacob 			if (xfrlen != 0) {
15252df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
15262df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
15272df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
15282df76c16SMatt Jacob 				} else {
15292df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
15302df76c16SMatt Jacob 				}
1531387d8239SMatt Jacob 
153294dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1533387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1534387d8239SMatt Jacob 
1535387d8239SMatt Jacob #ifdef	DEBUG
1536387d8239SMatt Jacob 				if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1537387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1538387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1539387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1540387d8239SMatt Jacob 				}
1541387d8239SMatt Jacob #endif
1542387d8239SMatt Jacob 				if (sendstatus) {
154394dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
154494dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1545387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1546387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
154794dff771SMatt Jacob 						if (fctape) {
154894dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
154994dff771SMatt Jacob 						}
1550387d8239SMatt Jacob 					} else {
1551387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
155294dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
15532df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
15542df76c16SMatt Jacob 					}
1555387d8239SMatt Jacob 				} else {
1556387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1557387d8239SMatt Jacob 				}
155894dff771SMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
155994dff771SMatt Jacob 				    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1560387d8239SMatt Jacob 			}
15613e6deb33SAlexander Motin 		} else {
15624fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
156300a8e174SMatt Jacob 
1564387d8239SMatt Jacob 			if (isp->isp_osinfo.sixtyfourbit)
1565387d8239SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1566387d8239SMatt Jacob 			else
1567d81ba9d5SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1568d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
156994dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
157094dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1571d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1572d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
157329f76675SMatt Jacob 			} else {
1574d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
15752df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1576d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
15772ad50ca5SMatt Jacob 				}
157829f76675SMatt Jacob 			}
1579a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
158000a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1581387d8239SMatt Jacob 
1582387d8239SMatt Jacob 			/*
1583387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1584387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1585387d8239SMatt Jacob 			 *
158696240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1587387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1588387d8239SMatt Jacob 			 *
1589387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1590387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1591387d8239SMatt Jacob 			 * needed based upon this.
1592387d8239SMatt Jacob 			 */
1593387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1594387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
159594dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1596387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1597387d8239SMatt Jacob 					if (resid < 0) {
1598387d8239SMatt Jacob 						cto->ct_resid = -resid;
1599387d8239SMatt Jacob 					} else if (resid > 0) {
1600387d8239SMatt Jacob 						cto->ct_resid = resid;
1601387d8239SMatt Jacob 					}
1602387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1603f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1604387d8239SMatt Jacob 					if (resid < 0) {
16052df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1606387d8239SMatt Jacob 					} else if (resid > 0) {
16072df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1608f48ce188SMatt Jacob 					}
1609387d8239SMatt Jacob 					if (fctape) {
1610387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1611387d8239SMatt Jacob 					}
1612387d8239SMatt Jacob 					if (sense_length) {
161300a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1614387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1615387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
161600a8e174SMatt Jacob 					}
161700a8e174SMatt Jacob 				} else {
1618387d8239SMatt Jacob 					bus_addr_t addr;
1619387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1620387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1621387d8239SMatt Jacob 
1622387d8239SMatt Jacob 					if (atp->ests == NULL) {
1623387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1624387d8239SMatt Jacob 						if (atp->ests == NULL) {
1625*09ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
162694dff771SMatt Jacob 							break;
1627387d8239SMatt Jacob 						}
1628387d8239SMatt Jacob 					}
1629387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1630387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1631387d8239SMatt Jacob 					if (fctape) {
1632387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1633387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1634387d8239SMatt Jacob 					}
1635387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1636387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1637387d8239SMatt Jacob 					if (resid < 0) {
1638387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1639387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1640387d8239SMatt Jacob 					} else if (resid > 0) {
1641387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1642387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1643387d8239SMatt Jacob 					}
1644387d8239SMatt Jacob 					if (sense_length) {
1645387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1646387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1647387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1648387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1649387d8239SMatt Jacob 					} else {
1650387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1651387d8239SMatt Jacob 					}
1652387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1653387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1654387d8239SMatt Jacob 					}
1655387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1656387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
165794dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1658387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1659387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1660387d8239SMatt Jacob 					if (isp->isp_osinfo.sixtyfourbit) {
1661387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1662387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1663387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1664387d8239SMatt Jacob 					} else {
1665387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1666387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1667387d8239SMatt Jacob 					}
1668387d8239SMatt Jacob 				}
1669387d8239SMatt Jacob 				if (sense_length) {
167094dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
167194dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
167294dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1673387d8239SMatt Jacob 				} else {
167494dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
167594dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1676387d8239SMatt Jacob 				}
1677387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1678387d8239SMatt Jacob 			}
1679387d8239SMatt Jacob 
1680387d8239SMatt Jacob 			if (xfrlen != 0) {
168100a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
168200a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
168300a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
168400a8e174SMatt Jacob 				} else {
168500a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1686d81ba9d5SMatt Jacob 				}
168753036e92SMatt Jacob 
168894dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1689387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1690387d8239SMatt Jacob 
1691387d8239SMatt Jacob 				if (sendstatus) {
169294dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
169394dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1694387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1695387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
169694dff771SMatt Jacob 						if (fctape) {
169794dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
169894dff771SMatt Jacob 						}
1699387d8239SMatt Jacob 					} else {
1700387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
170194dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1702387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1703387d8239SMatt Jacob 					}
1704387d8239SMatt Jacob 				} else {
1705387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1706387d8239SMatt Jacob 				}
1707387d8239SMatt Jacob 			}
170894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
170994dff771SMatt Jacob 			    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1710d81ba9d5SMatt Jacob 		}
1711d81ba9d5SMatt Jacob 
171294dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
171394dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1714*09ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
171594dff771SMatt Jacob 			break;
171694dff771SMatt Jacob 		}
1717970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1718970ceb2fSAlexander Motin 		if (handle == 0) {
1719387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1720*09ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
172194dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
172294dff771SMatt Jacob 			break;
1723d81ba9d5SMatt Jacob 		}
172494dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
172594dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
172694dff771SMatt Jacob 
1727d81ba9d5SMatt Jacob 
1728d81ba9d5SMatt Jacob 		/*
1729d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1730d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1731b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1732d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1733d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1734d81ba9d5SMatt Jacob 		 * format.
1735d81ba9d5SMatt Jacob 		 */
1736d81ba9d5SMatt Jacob 
17372df76c16SMatt Jacob 		if (IS_24XX(isp)) {
17382df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
17392df76c16SMatt Jacob 			cto->ct_syshandle = handle;
174010365e5aSMatt Jacob 		} else {
17413e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
174210365e5aSMatt Jacob 			cto->ct_syshandle = handle;
174310365e5aSMatt Jacob 		}
1744a1bc34c6SMatt Jacob 
17452df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
174694dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1747970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
174894dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
174994dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
1750*09ddc7adSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
175194dff771SMatt Jacob 				break;
175294dff771SMatt Jacob 			}
175394dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
175494dff771SMatt Jacob 			xpt_done(ccb);
175594dff771SMatt Jacob 			continue;
175694dff771SMatt Jacob 		}
17572df76c16SMatt Jacob 		isp->isp_nactive++;
175894dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1759387d8239SMatt Jacob 		if (xfrlen) {
1760387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1761387d8239SMatt Jacob 		} else {
1762387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1763387d8239SMatt Jacob 		}
1764387d8239SMatt Jacob 		atp->ctcnt++;
176594dff771SMatt Jacob 		atp->seqno++;
17662df76c16SMatt Jacob 	}
17672df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
1768d81ba9d5SMatt Jacob }
1769d81ba9d5SMatt Jacob 
1770a1bc34c6SMatt Jacob static void
1771a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1772f48ce188SMatt Jacob {
17732df76c16SMatt Jacob 	union ccb *ccb = arg;
17742a0db815SJohn Baldwin 
1775b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
17762df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1777a1bc34c6SMatt Jacob }
1778a1bc34c6SMatt Jacob 
1779a1bc34c6SMatt Jacob static void
1780387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1781387d8239SMatt Jacob {
1782387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1783387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
17842a0db815SJohn Baldwin 
17852a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1786387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
17872a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1788387d8239SMatt Jacob 	} else {
1789387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1790387d8239SMatt Jacob 	}
1791387d8239SMatt Jacob }
1792387d8239SMatt Jacob 
1793387d8239SMatt Jacob 
1794387d8239SMatt Jacob static void
1795a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1796a1bc34c6SMatt Jacob {
17979cd7268eSMatt Jacob 	ispsoftc_t *isp;
1798a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
1799a1bc34c6SMatt Jacob 	void *qe;
18003e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1801a1bc34c6SMatt Jacob 
1802a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
1803f48ce188SMatt Jacob 
18042df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
18052df76c16SMatt Jacob 	if (qe == NULL) {
1806c3167cabSMatt Jacob 		xpt_print(ccb->ccb_h.path,
1807c3167cabSMatt Jacob 		    "%s: Request Queue Overflow\n", __func__);
18082a0db815SJohn Baldwin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
18092a0db815SJohn Baldwin 		    isp_refire_putback_atio, ccb);
1810a1bc34c6SMatt Jacob 		return;
1811f48ce188SMatt Jacob 	}
181229f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
1813a1bc34c6SMatt Jacob 	cso = &ccb->csio;
18142df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1815f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1816f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
18172df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1818a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
18196af11b82SAlexander Motin #if __FreeBSD_version < 1000700
18206af11b82SAlexander Motin 		if (at->at_scclun >= 256)
18216af11b82SAlexander Motin 			at->at_scclun |= 0x4000;
18226af11b82SAlexander Motin #endif
1823f48ce188SMatt Jacob 	} else {
1824a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1825f48ce188SMatt Jacob 	}
1826f48ce188SMatt Jacob 	at->at_status = CT_OK;
1827a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
1828570c7a3fSMatt Jacob 	at->at_iid = cso->ccb_h.target_id;
18294fd13c1bSMatt Jacob 	isp_put_atio2(isp, at, qe);
18302df76c16SMatt Jacob 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
18312df76c16SMatt Jacob 	ISP_SYNC_REQUEST(isp);
1832a1bc34c6SMatt Jacob 	isp_complete_ctio(ccb);
1833f48ce188SMatt Jacob }
1834f48ce188SMatt Jacob 
1835f48ce188SMatt Jacob static void
1836a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1837f48ce188SMatt Jacob {
1838387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1839a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1840a1bc34c6SMatt Jacob 		xpt_done(ccb);
1841f48ce188SMatt Jacob 	}
1842387d8239SMatt Jacob }
1843f48ce188SMatt Jacob 
18442df76c16SMatt Jacob static void
18459cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1846d81ba9d5SMatt Jacob {
184745b9ad7aSAlexander Motin 	fcparam *fcp;
184892a1e549SMatt Jacob 	lun_id_t lun;
18492df76c16SMatt Jacob 	fcportdb_t *lp;
1850d81ba9d5SMatt Jacob 	tstate_t *tptr;
1851d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
18522df76c16SMatt Jacob 	uint16_t nphdl;
1853a035b0afSMatt Jacob 	atio_private_data_t *atp;
18542df76c16SMatt Jacob 	inot_private_data_t *ntp;
1855d81ba9d5SMatt Jacob 
1856d81ba9d5SMatt Jacob 	/*
1857d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1858d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1859d81ba9d5SMatt Jacob 	 *
1860b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1861d81ba9d5SMatt Jacob 	 */
1862d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
18632df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1864d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
18652df76c16SMatt Jacob 		return;
1866d81ba9d5SMatt Jacob 	}
1867d81ba9d5SMatt Jacob 
186845b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
18692df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
187092a1e549SMatt Jacob 		lun = aep->at_scclun;
18716af11b82SAlexander Motin #if __FreeBSD_version < 1000700
18726af11b82SAlexander Motin 		lun &= 0x3fff;
18736af11b82SAlexander Motin #endif
18742ad50ca5SMatt Jacob 	} else {
187592a1e549SMatt Jacob 		lun = aep->at_lun;
18762ad50ca5SMatt Jacob 	}
18772df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
18782df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
18792df76c16SMatt Jacob 	} else {
18802df76c16SMatt Jacob 		nphdl = aep->at_iid;
18812df76c16SMatt Jacob 	}
1882a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1883d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1884a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1885746e9c85SMatt Jacob 		if (tptr == NULL) {
1886123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1887387d8239SMatt Jacob 			if (lun == 0) {
1888387d8239SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1889387d8239SMatt Jacob 			} else {
18902df76c16SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1891387d8239SMatt Jacob 			}
18922df76c16SMatt Jacob 			return;
1893746e9c85SMatt Jacob 		}
1894d81ba9d5SMatt Jacob 	}
1895d81ba9d5SMatt Jacob 
1896d81ba9d5SMatt Jacob 	/*
18972df76c16SMatt Jacob 	 * Start any commands pending resources first.
1898d81ba9d5SMatt Jacob 	 */
18992df76c16SMatt Jacob 	if (tptr->restart_queue) {
19002df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
19012df76c16SMatt Jacob 		tptr->restart_queue = NULL;
19022df76c16SMatt Jacob 		while (restart_queue) {
19032df76c16SMatt Jacob 			ntp = restart_queue;
19042df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
19052df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
19062df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
19072df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
19082df76c16SMatt Jacob 			/*
19092df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
19102df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
19112df76c16SMatt Jacob 			 * it and go to noresrc.
19122df76c16SMatt Jacob 			 */
19132df76c16SMatt Jacob 			if (tptr->restart_queue) {
19142df76c16SMatt Jacob 				ntp = tptr->restart_queue;
19152df76c16SMatt Jacob 				tptr->restart_queue = restart_queue;
19162df76c16SMatt Jacob 				while (restart_queue->rd.nt.nt_hba) {
19172df76c16SMatt Jacob 					restart_queue = restart_queue->rd.nt.nt_hba;
1918d81ba9d5SMatt Jacob 				}
19192df76c16SMatt Jacob 				restart_queue->rd.nt.nt_hba = ntp;
19202df76c16SMatt Jacob 				goto noresrc;
19212df76c16SMatt Jacob 			}
19222df76c16SMatt Jacob 		}
19232df76c16SMatt Jacob 	}
19242df76c16SMatt Jacob 
19252df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
19262df76c16SMatt Jacob 	if (atiop == NULL) {
19272df76c16SMatt Jacob 		goto noresrc;
19282df76c16SMatt Jacob 	}
19292df76c16SMatt Jacob 
1930523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
19312df76c16SMatt Jacob 	if (atp == NULL) {
19322df76c16SMatt Jacob 		goto noresrc;
19332df76c16SMatt Jacob 	}
19342df76c16SMatt Jacob 
1935570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1936d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1937570c7a3fSMatt Jacob 	tptr->atio_count--;
1938387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
193945b9ad7aSAlexander Motin 	atiop->ccb_h.target_id = fcp->isp_loopid;
194092a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
19412df76c16SMatt Jacob 
1942b0a3ba7eSMatt Jacob 	/*
1943b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1944b0a3ba7eSMatt Jacob 	 */
1945f48ce188SMatt Jacob 	atiop->sense_len = 0;
19462df76c16SMatt Jacob 
19472df76c16SMatt Jacob 	/*
19482df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
19492df76c16SMatt Jacob 	 */
1950d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1951d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1952d4f3ad3aSAlexander Motin 	else {
1953d4f3ad3aSAlexander Motin 		if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1954e68eef14SAlexander Motin 		     lp->state == FC_PORTDB_STATE_ZOMBIE)) {
195537a7daacSAlexander Motin 			uint64_t wwpn =
19562df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[0]) << 48) |
19572df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[1]) << 32) |
19582df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[2]) << 16) |
19592df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[3]) <<  0);
196037a7daacSAlexander Motin 			isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
196137a7daacSAlexander Motin 			    nphdl, PORT_ANY, 0);
196245b9ad7aSAlexander Motin 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
196345b9ad7aSAlexander Motin 				fcp->isp_loopstate = LOOP_LTEST_DONE;
196445b9ad7aSAlexander Motin 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
196545b9ad7aSAlexander Motin 			    ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1966d4f3ad3aSAlexander Motin 			isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1967d4f3ad3aSAlexander Motin 		}
1968d4f3ad3aSAlexander Motin 		atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
19692df76c16SMatt Jacob 	}
1970d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
19712df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1972d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
19732df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1974d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1975d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
19765e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1977d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1978d81ba9d5SMatt Jacob 		break;
1979d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
19805e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1981d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1982d81ba9d5SMatt Jacob 		break;
1983d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
19845e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1985d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1986d81ba9d5SMatt Jacob 		break;
1987d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1988d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1989d81ba9d5SMatt Jacob 	default:
1990d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1991d81ba9d5SMatt Jacob 		break;
1992d81ba9d5SMatt Jacob 	}
1993f48ce188SMatt Jacob 
199453036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
199553036e92SMatt Jacob 	atp->bytes_xfered = 0;
19962df76c16SMatt Jacob 	atp->lun = lun;
1997d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
19982df76c16SMatt Jacob 	atp->sid = PORT_ANY;
19992df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
20002df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
20012df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2002570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
2003d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
2004123055f0SNathan Whitehorn 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %jx datalen %u", aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
2005d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
20062df76c16SMatt Jacob 	return;
20072df76c16SMatt Jacob noresrc:
20082df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
20092df76c16SMatt Jacob 	if (ntp == NULL) {
20102df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
2011873b0260SAlexander Motin 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
20122df76c16SMatt Jacob 		return;
20132df76c16SMatt Jacob 	}
20142df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
20152df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
20162df76c16SMatt Jacob 	tptr->restart_queue = ntp;
20172df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2018d81ba9d5SMatt Jacob }
2019d81ba9d5SMatt Jacob 
20202df76c16SMatt Jacob static void
20212df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
20222df76c16SMatt Jacob {
20232df76c16SMatt Jacob 	int cdbxlen;
20246af11b82SAlexander Motin 	lun_id_t lun;
20256af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
20262df76c16SMatt Jacob 	uint32_t did, sid;
20272df76c16SMatt Jacob 	fcportdb_t *lp;
20282df76c16SMatt Jacob 	tstate_t *tptr;
20292df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
20302df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
20319e7d423dSMatt Jacob 	atio_private_data_t *oatp;
20322df76c16SMatt Jacob 	inot_private_data_t *ntp;
20332df76c16SMatt Jacob 
20342df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
20352df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
20366af11b82SAlexander Motin #if __FreeBSD_version >= 1000700
20376af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
20386af11b82SAlexander Motin #else
20396af11b82SAlexander Motin 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] & 0x3f << 8) |
20406af11b82SAlexander Motin 	    aep->at_cmnd.fcp_cmnd_lun[1];
20416af11b82SAlexander Motin #endif
20422df76c16SMatt Jacob 
20432df76c16SMatt Jacob 	/*
20442df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
20452df76c16SMatt Jacob 	 *
20462df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
20472df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
20482df76c16SMatt Jacob 	 */
20497dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
20502df76c16SMatt Jacob 		/*
20512df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
20522df76c16SMatt Jacob 		 */
20532df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
20542df76c16SMatt Jacob 
20552df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
20562df76c16SMatt Jacob 			NANOTIME_T now;
20572df76c16SMatt Jacob 
20582df76c16SMatt Jacob 			/*
20592df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
20602df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
20612df76c16SMatt Jacob 			 */
20622df76c16SMatt Jacob 			GET_NANOTIME(&now);
2063514a71ebSAlexander Motin 			if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
20642df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
20652df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
20662df76c16SMatt Jacob 				return;
20672df76c16SMatt Jacob 			}
20682df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
20692df76c16SMatt Jacob 			if (tptr == NULL) {
20702df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
20712df76c16SMatt Jacob 				if (tptr == NULL) {
20722df76c16SMatt Jacob 					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);
20732df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
20742df76c16SMatt Jacob 					return;
20752df76c16SMatt Jacob 				}
20762df76c16SMatt Jacob 			}
20772df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
20782df76c16SMatt Jacob 			goto noresrc;
20792df76c16SMatt Jacob 		}
20802df76c16SMatt Jacob 		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);
20812df76c16SMatt Jacob 	} else {
20822df76c16SMatt Jacob 		chan = 0;
20832df76c16SMatt Jacob 	}
20842df76c16SMatt Jacob 
20852df76c16SMatt Jacob 	/*
20862df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
20872df76c16SMatt Jacob 	 */
2088eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
20892df76c16SMatt Jacob 		/*
20902df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
20912df76c16SMatt Jacob 		 */
20922df76c16SMatt Jacob 		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",
20932df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
2094e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
20952df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
20962df76c16SMatt Jacob 		return;
20972df76c16SMatt Jacob 	}
20982df76c16SMatt Jacob 	nphdl = lp->handle;
20992df76c16SMatt Jacob 
21002df76c16SMatt Jacob 	/*
21012df76c16SMatt Jacob 	 * Get the tstate pointer
21022df76c16SMatt Jacob 	 */
21032df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
21042df76c16SMatt Jacob 	if (tptr == NULL) {
21052df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
21062df76c16SMatt Jacob 		if (tptr == NULL) {
21076af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
21086af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
21096af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
2110387d8239SMatt Jacob 			if (lun == 0) {
2111352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2112387d8239SMatt Jacob 			} else {
21132df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2114387d8239SMatt Jacob 			}
21152df76c16SMatt Jacob 			return;
21162df76c16SMatt Jacob 		}
21172df76c16SMatt Jacob 	}
21182df76c16SMatt Jacob 
21192df76c16SMatt Jacob 	/*
21202df76c16SMatt Jacob 	 * Start any commands pending resources first.
21212df76c16SMatt Jacob 	 */
21222df76c16SMatt Jacob 	if (tptr->restart_queue) {
21232df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
21242df76c16SMatt Jacob 		tptr->restart_queue = NULL;
21252df76c16SMatt Jacob 		while (restart_queue) {
21262df76c16SMatt Jacob 			ntp = restart_queue;
21272df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
21282df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
21292df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
21302df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
21312df76c16SMatt Jacob 			/*
21322df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
21332df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
21342df76c16SMatt Jacob 			 * it and go to noresrc.
21352df76c16SMatt Jacob 			 */
21362df76c16SMatt Jacob 			if (tptr->restart_queue) {
21379e7d423dSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
21382df76c16SMatt Jacob 				if (restart_queue) {
21392df76c16SMatt Jacob 					ntp = tptr->restart_queue;
21402df76c16SMatt Jacob 					tptr->restart_queue = restart_queue;
21412df76c16SMatt Jacob 					while (restart_queue->rd.nt.nt_hba) {
21422df76c16SMatt Jacob 						restart_queue = restart_queue->rd.nt.nt_hba;
21432df76c16SMatt Jacob 					}
21442df76c16SMatt Jacob 					restart_queue->rd.nt.nt_hba = ntp;
21452df76c16SMatt Jacob 				}
21462df76c16SMatt Jacob 				goto noresrc;
21472df76c16SMatt Jacob 			}
21482df76c16SMatt Jacob 		}
21492df76c16SMatt Jacob 	}
21502df76c16SMatt Jacob 
21512df76c16SMatt Jacob 	/*
21522df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
21532df76c16SMatt Jacob 	 */
21542df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
21552df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
21562df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
21572df76c16SMatt Jacob 		return;
21582df76c16SMatt Jacob 	}
21592df76c16SMatt Jacob 
21602df76c16SMatt Jacob 	/*
21612df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
21622df76c16SMatt Jacob 	 */
21632df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
21642df76c16SMatt Jacob 	if (atiop == NULL) {
21652df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
21662df76c16SMatt Jacob 		goto noresrc;
21672df76c16SMatt Jacob 	}
21682df76c16SMatt Jacob 
2169523ea374SAlexander Motin 	oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
21709e7d423dSMatt Jacob 	if (oatp) {
2171387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
21729e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
21732df76c16SMatt Jacob 		/*
21742df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
21752df76c16SMatt Jacob 		 */
21762df76c16SMatt Jacob 		goto noresrc;
21772df76c16SMatt Jacob 	}
2178523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2179523ea374SAlexander Motin 	if (atp == NULL) {
2180523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2181523ea374SAlexander Motin 		goto noresrc;
2182523ea374SAlexander Motin 	}
2183387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
21842df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
21852df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
21862df76c16SMatt Jacob 	tptr->atio_count--;
2187387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2188d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
21892df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
21902df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
21912df76c16SMatt Jacob 	atiop->sense_len = 0;
21922df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
21932df76c16SMatt Jacob 	if (cdbxlen) {
21942df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
21952df76c16SMatt Jacob 	}
21962df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
21972df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
21982df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
21992df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
22002df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
22012df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
22022df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
22035e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22042df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
22052df76c16SMatt Jacob 		break;
22062df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
22075e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22082df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
22092df76c16SMatt Jacob 		break;
22102df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
22115e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22122df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
22132df76c16SMatt Jacob 		break;
22142df76c16SMatt Jacob 	default:
22152df76c16SMatt Jacob 		/* FALLTHROUGH */
22162df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
22172df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
22182df76c16SMatt Jacob 		atiop->tag_action = 0;
22192df76c16SMatt Jacob 		break;
22202df76c16SMatt Jacob 	}
22212df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
22222df76c16SMatt Jacob 	atp->bytes_xfered = 0;
22232df76c16SMatt Jacob 	atp->lun = lun;
22242df76c16SMatt Jacob 	atp->nphdl = nphdl;
22252df76c16SMatt Jacob 	atp->portid = sid;
22262df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
22279e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
22282df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
22292df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
22302df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
22316af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
22326af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
22332df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
22342df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22352df76c16SMatt Jacob 	return;
22362df76c16SMatt Jacob noresrc:
22372df76c16SMatt Jacob 	if (atp) {
22382df76c16SMatt Jacob 		isp_put_atpd(isp, tptr, atp);
22392df76c16SMatt Jacob 	}
22402df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
22412df76c16SMatt Jacob 	if (ntp == NULL) {
22422df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
22432df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
22442df76c16SMatt Jacob 		return;
22452df76c16SMatt Jacob 	}
22462df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
22472df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
22482df76c16SMatt Jacob 	tptr->restart_queue = ntp;
22492df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22502df76c16SMatt Jacob }
22512df76c16SMatt Jacob 
2252387d8239SMatt Jacob 
2253387d8239SMatt Jacob /*
2254387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
2255387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
2256387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
2257387d8239SMatt Jacob  * transaction.
2258387d8239SMatt Jacob  */
2259387d8239SMatt Jacob static void
2260387d8239SMatt Jacob isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2261387d8239SMatt Jacob {
2262387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
2263387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2264387d8239SMatt Jacob 	union ccb *ccb;
2265387d8239SMatt Jacob 
2266387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
2267387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2268387d8239SMatt Jacob 	ccb = atp->srr_ccb;
2269387d8239SMatt Jacob 	atp->srr_ccb = NULL;
2270387d8239SMatt Jacob 	atp->nsrr++;
2271387d8239SMatt Jacob 	if (ccb == NULL) {
227294dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2273387d8239SMatt Jacob 		goto fail;
2274387d8239SMatt Jacob 	}
2275387d8239SMatt Jacob 
2276387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
2277387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
2278387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2279387d8239SMatt Jacob 
2280387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
2281387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
2282387d8239SMatt Jacob 		/*
2283387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
2284387d8239SMatt Jacob 		 */
2285387d8239SMatt Jacob 		atp->sendst = 0;
2286387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
2287387d8239SMatt Jacob 		if (ccb_len == 0) {
228894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2289387d8239SMatt Jacob 			goto mdp;
2290387d8239SMatt Jacob 		}
2291387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
229294dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2293387d8239SMatt Jacob 			goto mdp;
2294387d8239SMatt Jacob 		}
229594dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2296387d8239SMatt Jacob 		break;
2297387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
229894dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2299387d8239SMatt Jacob 		atp->sendst = 1;
2300387d8239SMatt Jacob 		/*
2301387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
2302387d8239SMatt Jacob 		 */
2303387d8239SMatt Jacob 		break;
2304387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
2305387d8239SMatt Jacob 		/*
2306387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2307387d8239SMatt Jacob 		 */
2308387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2309387d8239SMatt Jacob 		goto fail;
2310387d8239SMatt Jacob 
2311387d8239SMatt Jacob 	default:
2312387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2313387d8239SMatt Jacob 		goto fail;
2314387d8239SMatt Jacob 	}
2315387d8239SMatt Jacob 
2316387d8239SMatt Jacob 	/*
2317387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2318387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2319387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2320387d8239SMatt Jacob 	 */
2321387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2322387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2323387d8239SMatt Jacob 		goto fail;
2324387d8239SMatt Jacob 	}
2325387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2326387d8239SMatt Jacob 	return;
2327387d8239SMatt Jacob fail:
2328387d8239SMatt Jacob 	inot->in_reserved = 1;
2329387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2330387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2331387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2332387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2333387d8239SMatt Jacob 	return;
2334387d8239SMatt Jacob mdp:
2335387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2336387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2337387d8239SMatt Jacob 		goto fail;
2338387d8239SMatt Jacob 	}
2339387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2340387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2341387d8239SMatt Jacob 	/*
2342387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2343387d8239SMatt Jacob 	 */
2344387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2345387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2346387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2347387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2348387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2349387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2350387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2351387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2352387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2353387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2354387d8239SMatt Jacob }
2355387d8239SMatt Jacob 
2356387d8239SMatt Jacob 
2357387d8239SMatt Jacob static void
2358387d8239SMatt Jacob isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2359387d8239SMatt Jacob {
2360387d8239SMatt Jacob 	tstate_t *tptr;
2361387d8239SMatt Jacob 	in_fcentry_24xx_t *inot = inot_raw;
2362387d8239SMatt Jacob 	atio_private_data_t *atp;
2363387d8239SMatt Jacob 	uint32_t tag = inot->in_rxid;
2364387d8239SMatt Jacob 	uint32_t bus = inot->in_vpidx;
2365387d8239SMatt Jacob 
2366387d8239SMatt Jacob 	if (!IS_24XX(isp)) {
2367387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2368387d8239SMatt Jacob 		return;
2369387d8239SMatt Jacob 	}
2370387d8239SMatt Jacob 
2371387d8239SMatt Jacob 	tptr = get_lun_statep_from_tag(isp, bus, tag);
2372387d8239SMatt Jacob 	if (tptr == NULL) {
2373387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2374387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2375387d8239SMatt Jacob 		return;
2376387d8239SMatt Jacob 	}
2377523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, tag);
2378387d8239SMatt Jacob 	if (atp == NULL) {
2379387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2380387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2381387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2382387d8239SMatt Jacob 		return;
2383387d8239SMatt Jacob 	}
2384387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2385387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
238694dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2387387d8239SMatt Jacob 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2388387d8239SMatt Jacob 	if (atp->srr_ccb)
2389387d8239SMatt Jacob 		isp_handle_srr_start(isp, tptr, atp);
2390387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
2391387d8239SMatt Jacob }
2392387d8239SMatt Jacob 
23932df76c16SMatt Jacob static void
23949cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2395d81ba9d5SMatt Jacob {
2396d81ba9d5SMatt Jacob 	union ccb *ccb;
239794dff771SMatt Jacob 	int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
23982df76c16SMatt Jacob 	tstate_t *tptr = NULL;
23992df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
24002df76c16SMatt Jacob 	int bus;
240194dff771SMatt Jacob 	uint32_t handle, moved_data = 0, data_requested;
2402d81ba9d5SMatt Jacob 
24032df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2404970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
24052df76c16SMatt Jacob 	if (ccb == NULL) {
24062df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
24072df76c16SMatt Jacob 		return;
24082df76c16SMatt Jacob 	}
2409970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
241094dff771SMatt Jacob 	data_requested = PISP_PCMD(ccb)->datalen;
2411387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2412387d8239SMatt Jacob 	if (isp->isp_nactive) {
2413387d8239SMatt Jacob 		isp->isp_nactive--;
2414387d8239SMatt Jacob 	}
2415387d8239SMatt Jacob 
24162df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
24172df76c16SMatt Jacob 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
24182df76c16SMatt Jacob 	if (tptr == NULL) {
24192df76c16SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
24202df76c16SMatt Jacob 	}
2421387d8239SMatt Jacob 	if (tptr == NULL) {
2422387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2423387d8239SMatt Jacob 		return;
24242df76c16SMatt Jacob 	}
2425387d8239SMatt Jacob 
2426387d8239SMatt Jacob 	if (IS_24XX(isp)) {
2427523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2428387d8239SMatt Jacob 	} else {
24293e6deb33SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2430387d8239SMatt Jacob 	}
2431387d8239SMatt Jacob 	if (atp == NULL) {
2432af207637SAlexander Motin 		/*
24338656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
24348656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
24358656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2436af207637SAlexander Motin 		 */
2437af207637SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2438af207637SAlexander Motin 	}
2439af207637SAlexander Motin 	if (atp == NULL) {
2440387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2441387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2442387d8239SMatt Jacob 		return;
2443387d8239SMatt Jacob 	}
2444387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
244594dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2446387d8239SMatt Jacob 	atp->ctcnt -= 1;
2447387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2448387d8239SMatt Jacob 
24492df76c16SMatt Jacob 	if (IS_24XX(isp)) {
24502df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2451d81ba9d5SMatt Jacob 
2452387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2453387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2454387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2455387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
24562df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
24572df76c16SMatt Jacob 			return;
24582df76c16SMatt Jacob 		}
2459387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
2460387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2461387d8239SMatt Jacob 		} else {
24622df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
24632df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
246494dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
24652df76c16SMatt Jacob 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
24662df76c16SMatt Jacob 				resid = ct->ct_resid;
246794dff771SMatt Jacob 				moved_data = data_requested - resid;
24682df76c16SMatt Jacob 			}
24692df76c16SMatt Jacob 		}
247094dff771SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
247194dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
24723e6deb33SAlexander Motin 	} else {
24732df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2474387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2475387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2476387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2477387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
24782df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
2479387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
24802df76c16SMatt Jacob 			return;
2481570c7a3fSMatt Jacob 		}
2482387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
2483387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2484387d8239SMatt Jacob 		} else {
2485d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2486d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
248794dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
24885d571944SMatt Jacob 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2489a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
249094dff771SMatt Jacob 				moved_data = data_requested - resid;
2491570c7a3fSMatt Jacob 			}
249253036e92SMatt Jacob 		}
249394dff771SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
249494dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
24955d571944SMatt Jacob 	}
2496387d8239SMatt Jacob 	if (ok) {
2497387d8239SMatt Jacob 		if (moved_data) {
249894dff771SMatt Jacob 			atp->bytes_xfered += moved_data;
249994dff771SMatt Jacob 			ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
2500387d8239SMatt Jacob 		}
2501387d8239SMatt Jacob 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2502387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2503387d8239SMatt Jacob 		}
2504387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2505387d8239SMatt Jacob 	} else {
2506387d8239SMatt Jacob 		notify_cam = 1;
2507387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2508387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2509387d8239SMatt Jacob 		else
2510387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2511387d8239SMatt Jacob 	}
2512387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2513387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
2514a1bc34c6SMatt Jacob 
2515a1bc34c6SMatt Jacob 	/*
2516387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2517387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2518d81ba9d5SMatt Jacob 	 */
251994dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
252094dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2521f48ce188SMatt Jacob 	if (notify_cam == 0) {
2522387d8239SMatt Jacob 		if (atp->sendst) {
2523387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2524387d8239SMatt Jacob 		}
25252df76c16SMatt Jacob 		return;
2526f48ce188SMatt Jacob 	}
2527d81ba9d5SMatt Jacob 
2528387d8239SMatt Jacob 	/*
2529387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2530387d8239SMatt Jacob 	 *
2531387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2532387d8239SMatt Jacob 	 *
2533387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2534387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2535387d8239SMatt Jacob 	 * and try again in case we didn't have
2536387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2537387d8239SMatt Jacob 	 * once the putback is done we do the completion
2538387d8239SMatt Jacob 	 * call.
2539387d8239SMatt Jacob 	 */
2540387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2541a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2542387d8239SMatt Jacob 	} else {
2543387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2544d81ba9d5SMatt Jacob 	}
2545d81ba9d5SMatt Jacob }
2546570c7a3fSMatt Jacob 
25472df76c16SMatt Jacob static void
25489cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2549570c7a3fSMatt Jacob {
25502df76c16SMatt Jacob 	int needack = 1;
2551570c7a3fSMatt Jacob 	switch (inp->in_status) {
2552570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
25532df76c16SMatt Jacob 		/*
25542df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
25552df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
25562df76c16SMatt Jacob 		 */
25572df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2558570c7a3fSMatt Jacob 		break;
2559570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
25602df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2561570c7a3fSMatt Jacob 		break;
2562570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
25632df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
2564570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2565570c7a3fSMatt Jacob 		break;
2566570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
2567570c7a3fSMatt Jacob 	{
25682df76c16SMatt Jacob 		tstate_t *tptr;
2569c5fd36edSAlexander Motin 		uint16_t nphdl, lun;
2570c5fd36edSAlexander Motin 		uint32_t sid;
25712df76c16SMatt Jacob 		uint64_t wwn;
25722df76c16SMatt Jacob 		atio_private_data_t *atp;
25732df76c16SMatt Jacob 		fcportdb_t *lp;
25742df76c16SMatt Jacob 		struct ccb_immediate_notify *inot = NULL;
25752df76c16SMatt Jacob 
25762df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
25772df76c16SMatt Jacob 			lun = inp->in_scclun;
25786af11b82SAlexander Motin #if __FreeBSD_version < 1000700
25796af11b82SAlexander Motin 			lun &= 0x3fff;
25806af11b82SAlexander Motin #endif
25812df76c16SMatt Jacob 		} else {
25822df76c16SMatt Jacob 			lun = inp->in_lun;
25832df76c16SMatt Jacob 		}
25842df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
2585c5fd36edSAlexander Motin 			nphdl = ((in_fcentry_e_t *)inp)->in_iid;
25862df76c16SMatt Jacob 		} else {
2587c5fd36edSAlexander Motin 			nphdl = inp->in_iid;
25882df76c16SMatt Jacob 		}
2589c5fd36edSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
25902df76c16SMatt Jacob 			wwn = lp->port_wwn;
2591d4f3ad3aSAlexander Motin 			sid = lp->portid;
25922df76c16SMatt Jacob 		} else {
25932df76c16SMatt Jacob 			wwn = INI_ANY;
2594d4f3ad3aSAlexander Motin 			sid = PORT_ANY;
25952df76c16SMatt Jacob 		}
25962df76c16SMatt Jacob 		tptr = get_lun_statep(isp, 0, lun);
25972df76c16SMatt Jacob 		if (tptr == NULL) {
25982df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
25992df76c16SMatt Jacob 			if (tptr == NULL) {
2600c5fd36edSAlexander Motin 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %x, but no tstate", lun);
26012df76c16SMatt Jacob 				return;
26022df76c16SMatt Jacob 			}
26032df76c16SMatt Jacob 		}
2604523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, inp->in_seqid);
2605570c7a3fSMatt Jacob 
2606570c7a3fSMatt Jacob 		if (atp) {
26072df76c16SMatt Jacob 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
26082df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2609570c7a3fSMatt Jacob 			if (inot) {
2610746e9c85SMatt Jacob 				tptr->inot_count--;
26112df76c16SMatt Jacob 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2612387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2613570c7a3fSMatt Jacob 			} else {
2614387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
26152df76c16SMatt Jacob 			}
26162df76c16SMatt Jacob 		} else {
26172df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2618570c7a3fSMatt Jacob 		}
2619570c7a3fSMatt Jacob 		if (inot) {
26202df76c16SMatt Jacob 			isp_notify_t tmp, *nt = &tmp;
26212df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
26222df76c16SMatt Jacob     			nt->nt_hba = isp;
26232df76c16SMatt Jacob 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
26242df76c16SMatt Jacob 			nt->nt_wwn = wwn;
2625c5fd36edSAlexander Motin 			nt->nt_nphdl = nphdl;
2626d4f3ad3aSAlexander Motin 			nt->nt_sid = sid;
26272df76c16SMatt Jacob 			nt->nt_did = PORT_ANY;
26282df76c16SMatt Jacob     			nt->nt_lun = lun;
26292df76c16SMatt Jacob             		nt->nt_need_ack = 1;
26302df76c16SMatt Jacob     			nt->nt_channel = 0;
26312df76c16SMatt Jacob     			nt->nt_ncode = NT_ABORT_TASK;
26322df76c16SMatt Jacob     			nt->nt_lreserved = inot;
26332df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
26342df76c16SMatt Jacob 			needack = 0;
2635570c7a3fSMatt Jacob 		}
26362df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
2637570c7a3fSMatt Jacob 		break;
2638570c7a3fSMatt Jacob 	}
2639570c7a3fSMatt Jacob 	default:
2640570c7a3fSMatt Jacob 		break;
2641570c7a3fSMatt Jacob 	}
26422df76c16SMatt Jacob 	if (needack) {
2643387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
26442df76c16SMatt Jacob 	}
26452df76c16SMatt Jacob }
26462df76c16SMatt Jacob 
26472df76c16SMatt Jacob static void
26482df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
26492df76c16SMatt Jacob {
26502df76c16SMatt Jacob 	uint16_t nphdl;
2651387d8239SMatt Jacob 	uint16_t prli_options = 0;
26522df76c16SMatt Jacob 	uint32_t portid;
26532df76c16SMatt Jacob 	fcportdb_t *lp;
265437a7daacSAlexander Motin 	char *msg = NULL;
265537a7daacSAlexander Motin 	uint8_t *ptr = (uint8_t *)inot;
265637a7daacSAlexander Motin 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
26572df76c16SMatt Jacob 
26582df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
26592df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
26602df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
26612df76c16SMatt Jacob 	} else {
26622df76c16SMatt Jacob 		portid = PORT_ANY;
26632df76c16SMatt Jacob 	}
26642df76c16SMatt Jacob 
26652df76c16SMatt Jacob 	switch (inot->in_status) {
26662df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
26672df76c16SMatt Jacob 	{
266837a7daacSAlexander Motin 		char buf[16];
26692df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
26702df76c16SMatt Jacob 
26712df76c16SMatt Jacob 		/*
26722df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
2673b1ce21c6SRebecca Cran 		 * (possibly with some associated information sent upstream). This is
26742df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
26752df76c16SMatt Jacob 		 */
26762df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
26772df76c16SMatt Jacob 		case LOGO:
26782df76c16SMatt Jacob 			msg = "LOGO";
267937a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
268037a7daacSAlexander Motin 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
26812df76c16SMatt Jacob 			break;
26822df76c16SMatt Jacob 		case PRLO:
26832df76c16SMatt Jacob 			msg = "PRLO";
26842df76c16SMatt Jacob 			break;
26852df76c16SMatt Jacob 		case PLOGI:
26862df76c16SMatt Jacob 			msg = "PLOGI";
268737a7daacSAlexander Motin 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
268837a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
268937a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
269037a7daacSAlexander Motin 			    nphdl, portid, prli_options);
269137a7daacSAlexander Motin 			break;
269237a7daacSAlexander Motin 		case PRLI:
2693dad28623SMatt Jacob 			msg = "PRLI";
269437a7daacSAlexander Motin 			prli_options = inot->in_prli_options;
269537a7daacSAlexander Motin 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
269637a7daacSAlexander Motin 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
269737a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
269837a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
269937a7daacSAlexander Motin 			    nphdl, portid, prli_options);
27002df76c16SMatt Jacob 			break;
27012df76c16SMatt Jacob 		case PDISC:
27022df76c16SMatt Jacob 			msg = "PDISC";
27032df76c16SMatt Jacob 			break;
27042df76c16SMatt Jacob 		case ADISC:
27052df76c16SMatt Jacob 			msg = "ADISC";
27062df76c16SMatt Jacob 			break;
27072df76c16SMatt Jacob 		default:
27082df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
27092df76c16SMatt Jacob 			msg = buf;
27102df76c16SMatt Jacob 			break;
27112df76c16SMatt Jacob 		}
27122df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
27132df76c16SMatt Jacob 			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);
27142df76c16SMatt Jacob 			break;
27152df76c16SMatt Jacob 		}
27162df76c16SMatt Jacob 		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,
27172df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
2718387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
27192df76c16SMatt Jacob 		break;
27202df76c16SMatt Jacob 	}
27212df76c16SMatt Jacob 
27222df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
272337a7daacSAlexander Motin 		msg = "PORT LOGOUT";
2724e68eef14SAlexander Motin 		if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
27252df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
27262df76c16SMatt Jacob 		}
27272df76c16SMatt Jacob 		/* FALLTHROUGH */
27282df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
272937a7daacSAlexander Motin 		if (msg == NULL)
273037a7daacSAlexander Motin 			msg = "PORT CHANGED";
27312df76c16SMatt Jacob 		/* FALLTHROUGH */
27322df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
273337a7daacSAlexander Motin 		if (msg == NULL)
273437a7daacSAlexander Motin 			msg = "LIP RESET";
273537a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), msg, inot->in_status_subcode, nphdl);
27362df76c16SMatt Jacob 
27372df76c16SMatt Jacob 		/*
27382df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
27392df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
27402df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
27412df76c16SMatt Jacob 		 */
27422df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
2743387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
27442df76c16SMatt Jacob 		break;
27452df76c16SMatt Jacob 
27462df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
2747387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
2748387d8239SMatt Jacob 		isp_handle_srr_notify(isp, inot);
2749387d8239SMatt Jacob 		break;
2750387d8239SMatt Jacob #else
275137a7daacSAlexander Motin 		if (msg == NULL)
275237a7daacSAlexander Motin 			msg = "SRR RCVD";
2753387d8239SMatt Jacob 		/* FALLTHROUGH */
2754387d8239SMatt Jacob #endif
2755387d8239SMatt Jacob 	case IN24XX_LINK_RESET:
275637a7daacSAlexander Motin 		if (msg == NULL)
275737a7daacSAlexander Motin 			msg = "LINK RESET";
2758387d8239SMatt Jacob 	case IN24XX_LINK_FAILED:
275937a7daacSAlexander Motin 		if (msg == NULL)
276037a7daacSAlexander Motin 			msg = "LINK FAILED";
27612df76c16SMatt Jacob 	default:
276237a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2763387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
27642df76c16SMatt Jacob 		break;
27652df76c16SMatt Jacob 	}
27662df76c16SMatt Jacob }
27672df76c16SMatt Jacob 
27682df76c16SMatt Jacob static int
276996b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
27702df76c16SMatt Jacob {
27712df76c16SMatt Jacob 
27722df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
27732df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2774570c7a3fSMatt Jacob 		return (0);
2775570c7a3fSMatt Jacob 	}
27762df76c16SMatt Jacob 
27772df76c16SMatt Jacob 	/*
27782df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
27792df76c16SMatt Jacob 	 */
27802df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
27812df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
27822df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
27832df76c16SMatt Jacob 		fcportdb_t *lp;
27842df76c16SMatt Jacob 		uint32_t sid;
27852df76c16SMatt Jacob 		uint16_t nphdl;
27862df76c16SMatt Jacob 
27872df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2788eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
27892df76c16SMatt Jacob 			nphdl = lp->handle;
27902df76c16SMatt Jacob 		} else {
27912df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
27922df76c16SMatt Jacob 		}
27932df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
27942df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
27952df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
27962df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
27972df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
27982df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
27992df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
28002df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
28012df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
28022df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
28032df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
280496b5475bSAlexander Motin 		if (rsp != 0) {
280596b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
280696b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
280796b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
280896b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
280996b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
281096b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
281196b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
281296b5475bSAlexander Motin 		}
28132df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
28142df76c16SMatt Jacob 	}
28152df76c16SMatt Jacob 
28162df76c16SMatt Jacob 	/*
28172df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
28182df76c16SMatt Jacob 	 */
28192df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
28202df76c16SMatt Jacob 
28212df76c16SMatt Jacob 		/*
28222df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
28232df76c16SMatt Jacob 		 */
28242df76c16SMatt Jacob 		if (mp->nt_need_ack) {
28252df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
28262df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
28272df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
28282df76c16SMatt Jacob 
28292df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
28302df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
28312df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
28322df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
28332df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
28342df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
28352df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
28362df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
28372df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
28382df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
28392df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
28402df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
28412df76c16SMatt Jacob 				return (ENOMEM);
28422df76c16SMatt Jacob 			}
28432df76c16SMatt Jacob 			mp->nt_need_ack = 0;
28442df76c16SMatt Jacob 		}
28452df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
28462df76c16SMatt Jacob 			return (ENOMEM);
28472df76c16SMatt Jacob 		} else {
28482df76c16SMatt Jacob 			return (0);
28492df76c16SMatt Jacob 		}
28502df76c16SMatt Jacob 	}
28512df76c16SMatt Jacob 
28522df76c16SMatt Jacob 	/*
28532df76c16SMatt Jacob 	 * Handle logout cases here
28542df76c16SMatt Jacob 	 */
28552df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
28562df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
28572df76c16SMatt Jacob 	}
28582df76c16SMatt Jacob 
28592df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
28602df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
28612df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
28622df76c16SMatt Jacob 		}
28632df76c16SMatt Jacob 	}
28642df76c16SMatt Jacob 
28652df76c16SMatt Jacob 	/*
28662df76c16SMatt Jacob 	 * General purpose acknowledgement
28672df76c16SMatt Jacob 	 */
28682df76c16SMatt Jacob 	if (mp->nt_need_ack) {
28692df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2870387d8239SMatt Jacob 		/*
2871387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2872387d8239SMatt Jacob 		 */
28732df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
28742df76c16SMatt Jacob 	}
28752df76c16SMatt Jacob 	return (0);
28762df76c16SMatt Jacob }
28772df76c16SMatt Jacob 
28782df76c16SMatt Jacob /*
2879b1ce21c6SRebecca Cran  * Handle task management functions.
28802df76c16SMatt Jacob  *
28812df76c16SMatt Jacob  * We show up here with a notify structure filled out.
28822df76c16SMatt Jacob  *
28832df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
28842df76c16SMatt Jacob  */
28852df76c16SMatt Jacob static void
28862df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
28872df76c16SMatt Jacob {
28882df76c16SMatt Jacob 	tstate_t *tptr;
28892df76c16SMatt Jacob 	fcportdb_t *lp;
28902df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
28912df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
28922df76c16SMatt Jacob 	lun_id_t lun;
28932df76c16SMatt Jacob 
28942df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
28952df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
28962df76c16SMatt Jacob 	/*
28972df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
28982df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
28992df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
29002df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
29012df76c16SMatt Jacob 	 */
29022df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
29032df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
29042df76c16SMatt Jacob 	} else {
29052df76c16SMatt Jacob 		lun = notify->nt_lun;
29062df76c16SMatt Jacob 	}
29072df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
29082df76c16SMatt Jacob 	if (tptr == NULL) {
29092df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
29102df76c16SMatt Jacob 		if (tptr == NULL) {
2911123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
29122df76c16SMatt Jacob 			goto bad;
29132df76c16SMatt Jacob 		}
29142df76c16SMatt Jacob 	}
29152df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
29162df76c16SMatt Jacob 	if (inot == NULL) {
2917123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
29182df76c16SMatt Jacob 		goto bad;
29192df76c16SMatt Jacob 	}
29202df76c16SMatt Jacob 
2921eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2922d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
29232df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
29242df76c16SMatt Jacob 	} else {
2925d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
29262df76c16SMatt Jacob 	}
29272df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
29282df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
29292df76c16SMatt Jacob 
29302df76c16SMatt Jacob 	switch (notify->nt_ncode) {
29312df76c16SMatt Jacob 	case NT_ABORT_TASK:
29322df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
29332df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
29342df76c16SMatt Jacob 		break;
29352df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
29362df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
29372df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
29382df76c16SMatt Jacob 		break;
29392df76c16SMatt Jacob 	case NT_CLEAR_ACA:
29402df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
29412df76c16SMatt Jacob 		break;
29422df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
29432df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
29442df76c16SMatt Jacob 		break;
29452df76c16SMatt Jacob 	case NT_LUN_RESET:
29462df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
29472df76c16SMatt Jacob 		break;
29482df76c16SMatt Jacob 	case NT_TARGET_RESET:
29492df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
29502df76c16SMatt Jacob 		break;
2951c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2952c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2953c98d2b1fSAlexander Motin 		break;
2954c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2955c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2956c98d2b1fSAlexander Motin 		break;
29572df76c16SMatt Jacob 	default:
2958123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun %#jx", __func__, notify->nt_ncode, notify->nt_channel, (uintmax_t)lun);
29592df76c16SMatt Jacob 		goto bad;
29602df76c16SMatt Jacob 	}
29612df76c16SMatt Jacob 
29622df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
29632df76c16SMatt Jacob 	if (ntp == NULL) {
29642df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
29652df76c16SMatt Jacob 		goto bad;
29662df76c16SMatt Jacob 	}
29672df76c16SMatt Jacob 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
29682df76c16SMatt Jacob 	if (notify->nt_lreserved) {
29692df76c16SMatt Jacob 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
29702df76c16SMatt Jacob 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
29712df76c16SMatt Jacob 	}
29722df76c16SMatt Jacob 	ntp->rd.seq_id = notify->nt_tagval;
29732df76c16SMatt Jacob 	ntp->rd.tag_id = notify->nt_tagval >> 32;
29742df76c16SMatt Jacob 
29752df76c16SMatt Jacob 	tptr->inot_count--;
29762df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
29772df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2978387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
29792df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
29802df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
29812df76c16SMatt Jacob 	return;
29822df76c16SMatt Jacob bad:
29832df76c16SMatt Jacob 	if (tptr) {
29842df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
29852df76c16SMatt Jacob 	}
29862df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
29872df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2988387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2989387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2990387d8239SMatt Jacob 			}
29912df76c16SMatt Jacob 		} else {
2992387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
29932df76c16SMatt Jacob 		}
29942df76c16SMatt Jacob 	}
29952df76c16SMatt Jacob }
29962df76c16SMatt Jacob 
29972df76c16SMatt Jacob /*
2998b1ce21c6SRebecca Cran  * Find the associated private data and mark it as dead so
29992df76c16SMatt Jacob  * we don't try to work on it any further.
30002df76c16SMatt Jacob  */
30012df76c16SMatt Jacob static void
30022df76c16SMatt Jacob isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
30032df76c16SMatt Jacob {
30042df76c16SMatt Jacob 	tstate_t *tptr;
30052df76c16SMatt Jacob 	atio_private_data_t *atp;
300645210a25SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
30072df76c16SMatt Jacob 
300845210a25SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
30092df76c16SMatt Jacob 	if (tptr == NULL) {
301045210a25SAlexander Motin 		tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
30112df76c16SMatt Jacob 		if (tptr == NULL) {
30122df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
30132df76c16SMatt Jacob 			return;
30142df76c16SMatt Jacob 		}
30152df76c16SMatt Jacob 	}
30162df76c16SMatt Jacob 
3017523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
30182df76c16SMatt Jacob 	if (atp == NULL) {
30192df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INVALID;
302045210a25SAlexander Motin 	} else {
30212df76c16SMatt Jacob 		atp->dead = 1;
30222df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
30232df76c16SMatt Jacob 	}
302445210a25SAlexander Motin 	rls_lun_statep(isp, tptr);
302545210a25SAlexander Motin }
30262df76c16SMatt Jacob 
30272df76c16SMatt Jacob static void
30282df76c16SMatt Jacob isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
30292df76c16SMatt Jacob {
30302df76c16SMatt Jacob 	atio_private_data_t *atp;
30312df76c16SMatt Jacob 	inot_private_data_t *restart_queue = tptr->restart_queue;
30322df76c16SMatt Jacob 
30332df76c16SMatt Jacob 	/*
30342df76c16SMatt Jacob 	 * First, clean any commands pending restart
30352df76c16SMatt Jacob 	 */
30362df76c16SMatt Jacob 	tptr->restart_queue = NULL;
30372df76c16SMatt Jacob 	while (restart_queue) {
30382df76c16SMatt Jacob 		uint32_t this_tag_id;
30392df76c16SMatt Jacob 		inot_private_data_t *ntp = restart_queue;
30402df76c16SMatt Jacob 
30412df76c16SMatt Jacob 		restart_queue = ntp->rd.nt.nt_hba;
30422df76c16SMatt Jacob 
30432df76c16SMatt Jacob 		if (IS_24XX(isp)) {
30442df76c16SMatt Jacob 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
30452df76c16SMatt Jacob 		} else {
30462df76c16SMatt Jacob 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
30472df76c16SMatt Jacob 		}
30482df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
30492df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
30502df76c16SMatt Jacob 		} else {
30512df76c16SMatt Jacob 			ntp->rd.nt.nt_hba = tptr->restart_queue;
30522df76c16SMatt Jacob 			tptr->restart_queue = ntp;
30532df76c16SMatt Jacob 		}
30542df76c16SMatt Jacob 	}
30552df76c16SMatt Jacob 
30562df76c16SMatt Jacob 	/*
30572df76c16SMatt Jacob 	 * Now mark other ones dead as well.
30582df76c16SMatt Jacob 	 */
30592df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
30602df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
30612df76c16SMatt Jacob 			atp->dead = 1;
30622df76c16SMatt Jacob 		}
30632df76c16SMatt Jacob 	}
30642df76c16SMatt Jacob }
3065d81ba9d5SMatt Jacob #endif
3066d81ba9d5SMatt Jacob 
3067478f8a96SJustin T. Gibbs static void
30681dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3069478f8a96SJustin T. Gibbs {
3070478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
3071e95725cbSMatt Jacob 	int bus, tgt;
30729cd7268eSMatt Jacob 	ispsoftc_t *isp;
3073478f8a96SJustin T. Gibbs 
3074478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
30759cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3076e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
3077e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
3078e95725cbSMatt Jacob 
3079478f8a96SJustin T. Gibbs 	switch (code) {
3080478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
3081ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
30821dae40ebSMatt Jacob 			uint16_t oflags, nflags;
30832df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
3084478f8a96SJustin T. Gibbs 
308541ed683eSMatt Jacob 			if (tgt >= 0) {
30869ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
30879ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
3088a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
3089478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3090478f8a96SJustin T. Gibbs 				}
30919ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
30929ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
3093478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
30942df76c16SMatt Jacob 				sdp->update = 1;
30952df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
30969ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
3097478f8a96SJustin T. Gibbs 			}
309841ed683eSMatt Jacob 		}
3099478f8a96SJustin T. Gibbs 		break;
3100478f8a96SJustin T. Gibbs 	default:
31013c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3102478f8a96SJustin T. Gibbs 		break;
3103478f8a96SJustin T. Gibbs 	}
3104478f8a96SJustin T. Gibbs }
3105478f8a96SJustin T. Gibbs 
3106478f8a96SJustin T. Gibbs static void
3107c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
3108478f8a96SJustin T. Gibbs {
31099cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
31106ce548a1SAlexander Motin 	uint16_t isr, sema, info;
3111126ec864SMatt Jacob 
31126ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
31136ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
3114478f8a96SJustin T. Gibbs }
3115478f8a96SJustin T. Gibbs 
3116ab6c4b31SMatt Jacob 
31172df76c16SMatt Jacob static void
31182df76c16SMatt Jacob isp_watchdog(void *arg)
3119cc8df88bSMatt Jacob {
31202df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
31212df76c16SMatt Jacob 	ispsoftc_t *isp;
3122e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
3123b85389e1SMatt Jacob 
31242df76c16SMatt Jacob 	isp = XS_ISP(xs);
31252df76c16SMatt Jacob 
3126cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
3127e95725cbSMatt Jacob 
3128e95725cbSMatt Jacob 	/*
3129e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3130e95725cbSMatt Jacob 	 */
3131e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
31326ce548a1SAlexander Motin 		uint16_t isr, sema, info;
31336ce548a1SAlexander Motin 		if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
31346ce548a1SAlexander Motin 			isp_intr(isp, isr, sema, info);
3135e95725cbSMatt Jacob 		ohandle = handle;
3136e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
3137e95725cbSMatt Jacob 	}
3138c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
31391fcf5debSMatt Jacob 		/*
3140c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
3141c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
3142c8b8a2c4SMatt Jacob 		 *
3143c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
3144c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
3145c8b8a2c4SMatt Jacob 		 * back separately.
31461fcf5debSMatt Jacob 		 */
3147c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3148c8b8a2c4SMatt Jacob 			return;
3149c8b8a2c4SMatt Jacob 		}
31501fcf5debSMatt Jacob 
31511fcf5debSMatt Jacob 		/*
3152c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
3153c8b8a2c4SMatt Jacob 		 * fact have been completed.
3154c8b8a2c4SMatt Jacob 		 */
3155c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
3156c8b8a2c4SMatt Jacob 
3157c8b8a2c4SMatt Jacob 		/*
3158c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
3159c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
3160c8b8a2c4SMatt Jacob 		 */
3161c8b8a2c4SMatt Jacob 		if (xs == NULL) {
3162c8b8a2c4SMatt Jacob 			return;
3163c8b8a2c4SMatt Jacob 		}
3164c8b8a2c4SMatt Jacob 
3165c8b8a2c4SMatt Jacob 		/*
3166c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
31671fcf5debSMatt Jacob 		 */
3168f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
3169f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
3170f6e75de2SMatt Jacob 		}
3171cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
3172c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3173387d8239SMatt Jacob 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
3174387d8239SMatt Jacob 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
3175e95725cbSMatt Jacob 		isp_prt_endcmd(isp, xs);
3176cc8df88bSMatt Jacob 		isp_done(xs);
3177e95725cbSMatt Jacob 	} else {
3178e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
3179e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3180e95725cbSMatt Jacob 		} else {
3181e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3182e95725cbSMatt Jacob 		}
3183b85389e1SMatt Jacob 	}
3184f7c631bcSMatt Jacob }
3185f7c631bcSMatt Jacob 
3186f7c631bcSMatt Jacob static void
31875704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3188f7c631bcSMatt Jacob {
3189ffcf6651SMatt Jacob 	union ccb *ccb;
31902df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
31912df76c16SMatt Jacob 
3192ffcf6651SMatt Jacob 	/*
31930e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3194ffcf6651SMatt Jacob 	 */
31958008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
3196ffcf6651SMatt Jacob 	if (ccb == NULL) {
31972df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3198ffcf6651SMatt Jacob 		return;
3199ffcf6651SMatt Jacob 	}
3200e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
3201e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3202ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3203ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
3204ffcf6651SMatt Jacob 		return;
3205ffcf6651SMatt Jacob 	}
3206ffcf6651SMatt Jacob 	xpt_rescan(ccb);
3207ffcf6651SMatt Jacob }
3208ffcf6651SMatt Jacob 
3209ffcf6651SMatt Jacob static void
32105704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3211ffcf6651SMatt Jacob {
3212ffcf6651SMatt Jacob 	struct cam_path *tp;
32132df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
32142df76c16SMatt Jacob 
32152df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3216ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
3217ffcf6651SMatt Jacob 		xpt_free_path(tp);
3218f7c631bcSMatt Jacob 	}
3219f7c631bcSMatt Jacob }
3220f7c631bcSMatt Jacob 
3221f7c631bcSMatt Jacob /*
3222f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
3223f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
3224f7c631bcSMatt Jacob  * gone away.
3225f7c631bcSMatt Jacob  *
3226f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
3227f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
3228f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
3229f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
3230f7c631bcSMatt Jacob  */
3231f7c631bcSMatt Jacob static void
3232f7c631bcSMatt Jacob isp_gdt(void *arg)
3233f7c631bcSMatt Jacob {
32342df76c16SMatt Jacob 	struct isp_fc *fc = arg;
3235de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3236de461933SMatt Jacob }
3237de461933SMatt Jacob 
3238de461933SMatt Jacob static void
3239de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
3240de461933SMatt Jacob {
3241de461933SMatt Jacob 	struct isp_fc *fc = arg;
32422df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
32432df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3244f7c631bcSMatt Jacob 	fcportdb_t *lp;
3245e68eef14SAlexander Motin 	struct ac_contract ac;
3246e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3247766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
3248f7c631bcSMatt Jacob 
3249de461933SMatt Jacob 	ISP_LOCK(isp);
3250de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3251f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
32522df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3253f7c631bcSMatt Jacob 
3254f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3255f7c631bcSMatt Jacob 			continue;
3256f7c631bcSMatt Jacob 		}
3257427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
3258427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
3259f7c631bcSMatt Jacob 			more_to_do++;
3260f7c631bcSMatt Jacob 			continue;
3261f7c631bcSMatt Jacob 		}
3262e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3263e68eef14SAlexander Motin 		if (lp->is_target) {
3264e68eef14SAlexander Motin 			lp->is_target = 0;
3265766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3266f7c631bcSMatt Jacob 		}
3267e68eef14SAlexander Motin 		if (lp->is_initiator) {
3268e68eef14SAlexander Motin 			lp->is_initiator = 0;
3269e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3270e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3271e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3272e68eef14SAlexander Motin 			adc->port = lp->portid;
3273d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3274e68eef14SAlexander Motin 			adc->arrived = 0;
3275e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3276e68eef14SAlexander Motin 		}
3277e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
3278e68eef14SAlexander Motin 	}
3279a01f5aebSMatt Jacob 	if (fc->ready) {
3280f7c631bcSMatt Jacob 		if (more_to_do) {
32812df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
3282f7c631bcSMatt Jacob 		} else {
3283de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
3284387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3285a01f5aebSMatt Jacob 		}
3286f7c631bcSMatt Jacob 	}
3287de461933SMatt Jacob 	ISP_UNLOCK(isp);
3288f7c631bcSMatt Jacob }
3289f7c631bcSMatt Jacob 
3290f7c631bcSMatt Jacob /*
3291e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
3292e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
3293e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
3294f7c631bcSMatt Jacob  *
3295f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
3296f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
3297f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3298f7c631bcSMatt Jacob  */
3299f7c631bcSMatt Jacob static void
3300e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
3301f7c631bcSMatt Jacob {
3302e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3303e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3304e561aa79SAlexander Motin 
3305e561aa79SAlexander Motin 	if (fc->loop_down_time)
3306e561aa79SAlexander Motin 		return;
3307e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3308e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
3309e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
3310e561aa79SAlexander Motin 	fc->loop_dead = 0;
3311e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
3312e561aa79SAlexander Motin 	wakeup(fc);
3313de461933SMatt Jacob }
3314de461933SMatt Jacob 
3315de461933SMatt Jacob static void
3316e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
3317de461933SMatt Jacob {
3318e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3319e561aa79SAlexander Motin 
3320e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3321e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
3322e561aa79SAlexander Motin 	fc->loop_dead = 0;
3323e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3324e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
3325e561aa79SAlexander Motin }
3326e561aa79SAlexander Motin 
3327e561aa79SAlexander Motin static void
3328e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
3329e561aa79SAlexander Motin {
3330e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3331e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3332f7c631bcSMatt Jacob 	fcportdb_t *lp;
3333e68eef14SAlexander Motin 	struct ac_contract ac;
3334e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3335766a65a5SAlexander Motin 	int dbidx, i;
3336f7c631bcSMatt Jacob 
3337e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
33380a70657fSMatt Jacob 
3339f7c631bcSMatt Jacob 	/*
3340f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
3341f7c631bcSMatt Jacob 	 */
3342f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3343e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
3344f7c631bcSMatt Jacob 
3345e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
3346f7c631bcSMatt Jacob 			continue;
3347f7c631bcSMatt Jacob 
3348f7c631bcSMatt Jacob 		/*
3349f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3350f7c631bcSMatt Jacob 		 */
3351e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
3352e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
3353e95725cbSMatt Jacob 
3354970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3355e95725cbSMatt Jacob 				continue;
3356e95725cbSMatt Jacob 			}
3357e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3358e95725cbSMatt Jacob 				continue;
3359e95725cbSMatt Jacob                         }
3360766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
3361e95725cbSMatt Jacob 				continue;
3362e95725cbSMatt Jacob 			}
33636af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
33646af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
33656af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
3366e95725cbSMatt Jacob 		}
3367e95725cbSMatt Jacob 
3368e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3369e68eef14SAlexander Motin 		if (lp->is_target) {
3370e68eef14SAlexander Motin 			lp->is_target = 0;
3371766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3372f7c631bcSMatt Jacob 		}
3373e68eef14SAlexander Motin 		if (lp->is_initiator) {
3374e68eef14SAlexander Motin 			lp->is_initiator = 0;
3375e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3376e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3377e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3378e68eef14SAlexander Motin 			adc->port = lp->portid;
3379d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3380e68eef14SAlexander Motin 			adc->arrived = 0;
3381e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3382e95725cbSMatt Jacob 		}
3383e68eef14SAlexander Motin 	}
3384e68eef14SAlexander Motin 
3385e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
33862df76c16SMatt Jacob 	fc->loop_dead = 1;
3387e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3388cc8df88bSMatt Jacob }
3389cc8df88bSMatt Jacob 
33905d571944SMatt Jacob static void
33915d571944SMatt Jacob isp_kthread(void *arg)
33925d571944SMatt Jacob {
33932df76c16SMatt Jacob 	struct isp_fc *fc = arg;
33942df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
33952df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3396e561aa79SAlexander Motin 	int slp = 0, d;
3397e561aa79SAlexander Motin 	int lb, lim;
3398a01f5aebSMatt Jacob 
33990a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
34002df76c16SMatt Jacob 
34011d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
3402e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3403e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
34042df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
3405e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3406e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
3407e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
34082df76c16SMatt Jacob 
34092df76c16SMatt Jacob 		/*
34102df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
34112df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
34122df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
34132df76c16SMatt Jacob 		 * check again.
34142df76c16SMatt Jacob 		 *
34152df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
34162df76c16SMatt Jacob 		 */
3417e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
3418e561aa79SAlexander Motin 			slp = 0;
341910365e5aSMatt Jacob 		} else {
342010365e5aSMatt Jacob 			/*
342110365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
3422f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
3423f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
3424f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
342510365e5aSMatt Jacob 			 */
3426e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
3427f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
3428e561aa79SAlexander Motin 			else
34292df76c16SMatt Jacob 				lim = fc->loop_down_limit;
3430e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
3431e561aa79SAlexander Motin 			if (d >= lim)
3432f7c631bcSMatt Jacob 				slp = 0;
3433e561aa79SAlexander Motin 			else if (d < 10)
343410365e5aSMatt Jacob 				slp = 1;
3435e561aa79SAlexander Motin 			else if (d < 30)
3436f7c631bcSMatt Jacob 				slp = 5;
3437e561aa79SAlexander Motin 			else if (d < 60)
3438f7c631bcSMatt Jacob 				slp = 10;
3439e561aa79SAlexander Motin 			else if (d < 120)
3440f7c631bcSMatt Jacob 				slp = 20;
3441e561aa79SAlexander Motin 			else
3442f7c631bcSMatt Jacob 				slp = 30;
3443f44257c2SMatt Jacob 		}
344410365e5aSMatt Jacob 
3445e561aa79SAlexander Motin 		if (slp == 0) {
3446e561aa79SAlexander Motin 			if (lb == LOOP_READY)
3447e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
3448387d8239SMatt Jacob 			else
3449e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
34505d571944SMatt Jacob 		}
3451fdeb9f2fSMatt Jacob 
3452e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3453e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
34542df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
34555d571944SMatt Jacob 	}
34561d0a1de2SWill Andrews 	fc->num_threads -= 1;
34570a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
34581d0a1de2SWill Andrews 	kthread_exit();
34595d571944SMatt Jacob }
34605d571944SMatt Jacob 
3461cc8df88bSMatt Jacob static void
3462c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
3463478f8a96SJustin T. Gibbs {
3464e561aa79SAlexander Motin 	int bus, tgt, ts, error;
34659cd7268eSMatt Jacob 	ispsoftc_t *isp;
34664663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
3467478f8a96SJustin T. Gibbs 
3468478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3469478f8a96SJustin T. Gibbs 
34709cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
34712df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
34725cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
34735cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
34742df76c16SMatt Jacob 
3475478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
3476478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
34772df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
3478478f8a96SJustin T. Gibbs 		/*
3479478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
3480478f8a96SJustin T. Gibbs 		 */
3481478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3482478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3483478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
34845cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3485478f8a96SJustin T. Gibbs 				break;
3486478f8a96SJustin T. Gibbs 			}
3487478f8a96SJustin T. Gibbs 		}
3488387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
34890470d791SMatt Jacob #ifdef	DIAGNOSTIC
34906af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3491dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3492478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
34936af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
34946af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3495dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3496478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3497478f8a96SJustin T. Gibbs 		}
3498478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3499478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3500478f8a96SJustin T. Gibbs 			break;
3501478f8a96SJustin T. Gibbs 		}
35020470d791SMatt Jacob #endif
35030a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
35040a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
35050a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
35060a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
35072df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
35085cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
35090a70657fSMatt Jacob 			xpt_done(ccb);
35100a70657fSMatt Jacob 			break;
35110a70657fSMatt Jacob 		}
3512b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
35130470d791SMatt Jacob 		switch (error) {
3514478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3515478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
35160a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
35170a70657fSMatt Jacob 				break;
3518d69a5f7dSMatt Jacob 			}
35190a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
35200a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
35210a70657fSMatt Jacob 				ts = 60*1000;
3522cc8df88bSMatt Jacob 			}
35230a70657fSMatt Jacob 			ts = isp_mstohz(ts);
35242df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3525478f8a96SJustin T. Gibbs 			break;
35260470d791SMatt Jacob 		case CMD_RQLATER:
3527f44257c2SMatt Jacob 			/*
3528e561aa79SAlexander Motin 			 * We get this result if the loop isn't ready
3529e561aa79SAlexander Motin 			 * or if the device in question has gone zombie.
353010365e5aSMatt Jacob 			 */
3531e561aa79SAlexander Motin 			if (ISP_FC_PC(isp, bus)->loop_dead) {
35326af11b82SAlexander Motin 				isp_prt(isp, ISP_LOGDEBUG0,
3533e561aa79SAlexander Motin 				    "%d.%jx loop is dead",
3534e561aa79SAlexander Motin 				    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
35355cbe3e8eSAlexander Motin 				ccb->ccb_h.status = CAM_SEL_TIMEOUT;
35365cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3537f44257c2SMatt Jacob 				break;
3538f44257c2SMatt Jacob 			}
35396af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
35406af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3541f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
35422df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3543387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
35440a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3545478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3546478f8a96SJustin T. Gibbs 			break;
35470470d791SMatt Jacob 		case CMD_EAGAIN:
35480a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
35492df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
35502df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3551387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3552478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3553478f8a96SJustin T. Gibbs 			break;
35540470d791SMatt Jacob 		case CMD_COMPLETE:
35550470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
35560470d791SMatt Jacob 			break;
35570470d791SMatt Jacob 		default:
35582df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3559387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
35600a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
35610470d791SMatt Jacob 			xpt_done(ccb);
3562478f8a96SJustin T. Gibbs 		}
3563478f8a96SJustin T. Gibbs 		break;
3564478f8a96SJustin T. Gibbs 
3565d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
35662df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
35672df76c16SMatt Jacob 		if (ccb->cel.enable) {
35682df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
35692df76c16SMatt Jacob 		} else {
35702df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
357167ff51f1SMatt Jacob 		}
357267ff51f1SMatt Jacob 		break;
3573ae5db118SMatt Jacob 	case XPT_IMMED_NOTIFY:
35742df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3575d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3576d81ba9d5SMatt Jacob 	{
35772df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3578d81ba9d5SMatt Jacob 		if (tptr == NULL) {
35792df76c16SMatt Jacob 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
35802df76c16SMatt Jacob 		}
35812df76c16SMatt Jacob 		if (tptr == NULL) {
35822df76c16SMatt Jacob 			const char *str;
35832df76c16SMatt Jacob 			uint32_t tag;
35842df76c16SMatt Jacob 
35852df76c16SMatt Jacob 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
35862df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
35872df76c16SMatt Jacob 				tag = ccb->cin1.seq_id;
35882df76c16SMatt Jacob 			} else {
35892df76c16SMatt Jacob 				tag = ccb->atio.tag_id;
35902df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
35912df76c16SMatt Jacob 			}
35922df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
35932df76c16SMatt Jacob 			dump_tstates(isp, XS_CHANNEL(ccb));
35942df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3595d81ba9d5SMatt Jacob 			break;
3596d81ba9d5SMatt Jacob 		}
3597387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3598387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3599570c7a3fSMatt Jacob 
3600d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
36012df76c16SMatt Jacob 			if (ccb->atio.tag_id) {
3602523ea374SAlexander Motin 				atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
36032df76c16SMatt Jacob 				if (atp) {
36042df76c16SMatt Jacob 					isp_put_atpd(isp, tptr, atp);
36052df76c16SMatt Jacob 				}
36062df76c16SMatt Jacob 			}
3607570c7a3fSMatt Jacob 			tptr->atio_count++;
36082df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3609387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
3610e2873b76SMatt Jacob 			    ccb->atio.tag_id, tptr->atio_count);
361194dff771SMatt Jacob 			ccb->atio.tag_id = 0;
36122df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
36132df76c16SMatt Jacob 			if (ccb->cin1.tag_id) {
36142df76c16SMatt Jacob 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
36152df76c16SMatt Jacob 				if (ntp) {
36162df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
36172df76c16SMatt Jacob 				}
36182df76c16SMatt Jacob 			}
3619746e9c85SMatt Jacob 			tptr->inot_count++;
36202df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3621387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
3622e2873b76SMatt Jacob 			    ccb->cin1.seq_id, tptr->inot_count);
362394dff771SMatt Jacob 			ccb->cin1.seq_id = 0;
3624ae5db118SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
3625ae5db118SMatt Jacob 			tptr->inot_count++;
3626ae5db118SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3627387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
3628e2873b76SMatt Jacob 			    ccb->cin1.seq_id, tptr->inot_count);
362994dff771SMatt Jacob 			ccb->cin1.seq_id = 0;
3630d81ba9d5SMatt Jacob 		}
3631d81ba9d5SMatt Jacob 		rls_lun_statep(isp, tptr);
3632d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3633d81ba9d5SMatt Jacob 		break;
3634d81ba9d5SMatt Jacob 	}
3635ae5db118SMatt Jacob 	case XPT_NOTIFY_ACK:
3636ae5db118SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3637ae5db118SMatt Jacob 		break;
36382df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3639d81ba9d5SMatt Jacob 	{
36402df76c16SMatt Jacob 		tstate_t *tptr;
36412df76c16SMatt Jacob 		inot_private_data_t *ntp;
36422df76c16SMatt Jacob 
36432df76c16SMatt Jacob 		/*
36442df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
36452df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
36462df76c16SMatt Jacob 		 */
36472df76c16SMatt Jacob 		/*
36482df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
36492df76c16SMatt Jacob 		 */
36502df76c16SMatt Jacob 		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);
36512df76c16SMatt Jacob 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
36522df76c16SMatt Jacob 		if (ntp == NULL) {
36532df76c16SMatt Jacob 			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__,
36542df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
36552df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
36562df76c16SMatt Jacob 			xpt_done(ccb);
3657d81ba9d5SMatt Jacob 			break;
3658d81ba9d5SMatt Jacob 		}
365996b5475bSAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt,
366096b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
36612df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
36622df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
36632df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3664387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3665387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
36662df76c16SMatt Jacob 			break;
36672df76c16SMatt Jacob 		}
36682df76c16SMatt Jacob 		isp_put_ntpd(isp, tptr, ntp);
36692df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
36702df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
36712df76c16SMatt Jacob 		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);
36722df76c16SMatt Jacob 		xpt_done(ccb);
36732df76c16SMatt Jacob 		break;
36742df76c16SMatt Jacob 	}
36752df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3676387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
36772df76c16SMatt Jacob 		break;
3678d81ba9d5SMatt Jacob #endif
3679478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3680d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3681d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3682d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3683d81ba9d5SMatt Jacob 
36842df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3685478f8a96SJustin T. Gibbs 		if (error) {
3686478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3687478f8a96SJustin T. Gibbs 		} else {
3688b44e442eSWill Andrews 			/*
3689b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3690b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3691b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3692b44e442eSWill Andrews 			 */
369321daf914SAlexander Motin 			if (IS_FC(isp))
369421daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3695b44e442eSWill Andrews 
3696478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3697478f8a96SJustin T. Gibbs 		}
3698478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3699478f8a96SJustin T. Gibbs 		break;
3700478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3701d81ba9d5SMatt Jacob 	{
3702d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3703d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3704d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3705d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
370645210a25SAlexander Motin 			isp_target_mark_aborted(isp, ccb);
3707d81ba9d5SMatt Jacob 			break;
3708d81ba9d5SMatt Jacob #endif
3709d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3710950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3711478f8a96SJustin T. Gibbs 			if (error) {
3712d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3713478f8a96SJustin T. Gibbs 			} else {
3714478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3715478f8a96SJustin T. Gibbs 			}
3716d81ba9d5SMatt Jacob 			break;
3717d81ba9d5SMatt Jacob 		default:
3718d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3719d81ba9d5SMatt Jacob 			break;
3720d81ba9d5SMatt Jacob 		}
37211c0a1eb2SMatt Jacob 		/*
37221c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
37231c0a1eb2SMatt Jacob 		 * complete when control is returned.
37241c0a1eb2SMatt Jacob 		 */
3725478f8a96SJustin T. Gibbs 		break;
3726d81ba9d5SMatt Jacob 	}
3727ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3728478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3729478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
37309ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
37319ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
37329ce9bdafSMatt Jacob 			xpt_done(ccb);
37339ce9bdafSMatt Jacob 			break;
37349ce9bdafSMatt Jacob 		}
3735478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3736805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
37372df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
37382df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
37392df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
37402df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
37411dae40ebSMatt Jacob 			uint16_t *dptr;
3742ab163f5fSMatt Jacob 
3743b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3744b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3745b61386a4SMatt Jacob 				xpt_done(ccb);
3746b61386a4SMatt Jacob 				break;
3747b61386a4SMatt Jacob 			}
3748b61386a4SMatt Jacob 
3749ab163f5fSMatt Jacob 			/*
37509ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3751ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3752ab163f5fSMatt Jacob 			 * vectored to that location.
3753ab163f5fSMatt Jacob 			 */
37549ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3755ab163f5fSMatt Jacob 
3756ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3757ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3758ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3759ab163f5fSMatt Jacob 				else
3760ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3761ab163f5fSMatt Jacob 			}
3762ab163f5fSMatt Jacob 
3763ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3764ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3765ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3766ab163f5fSMatt Jacob 				else
3767ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3768ab163f5fSMatt Jacob 			}
3769ab163f5fSMatt Jacob 
3770ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3771ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3772ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3773ab163f5fSMatt Jacob 				else
3774ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3775ab163f5fSMatt Jacob 			}
3776ab163f5fSMatt Jacob 
3777ab163f5fSMatt Jacob 			/*
3778ab163f5fSMatt Jacob 			 * XXX: FIX ME
3779ab163f5fSMatt Jacob 			 */
37802df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3781ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
37829ce9bdafSMatt Jacob 				/*
37839ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
37849ce9bdafSMatt Jacob 				 */
37852df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
37862df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3787ab163f5fSMatt Jacob 			} else {
3788ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3789ab163f5fSMatt Jacob 			}
3790123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%jx) to flags %x off %x per %x", bus, tgt, (uintmax_t)cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
37912df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3792478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
37932df76c16SMatt Jacob 			sdp->update = 1;
3794478f8a96SJustin T. Gibbs 		}
3795478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3796478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3797478f8a96SJustin T. Gibbs 		break;
3798478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3799478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3800478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
38012df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3802ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
38032df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
38042df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
38052df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3806478f8a96SJustin T. Gibbs 
3807ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3808ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3809ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3810ab163f5fSMatt Jacob 			cts->transport_version = 0;
3811ab163f5fSMatt Jacob 
38129b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
38139b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3814ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3815ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3816fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3817766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3818766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3819ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3820ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3821ab163f5fSMatt Jacob 				fc->port = lp->portid;
38222df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3823ab163f5fSMatt Jacob 			}
3824ab163f5fSMatt Jacob 		} else {
38252df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
38262df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
38272df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
38281dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3829ab163f5fSMatt Jacob 
3830ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
383183ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
38322df76c16SMatt Jacob 				sdp->update = 1;
38332df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
38349ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
38359ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
38369ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
38374394c92fSMatt Jacob 			} else {
38389ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
38399ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
38409ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
38414394c92fSMatt Jacob 			}
3842478f8a96SJustin T. Gibbs 
3843ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3844ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3845ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3846ab163f5fSMatt Jacob 			cts->transport_version = 2;
3847ab163f5fSMatt Jacob 
3848b61386a4SMatt Jacob 			spi->valid = 0;
3849b61386a4SMatt Jacob 			scsi->valid = 0;
3850b61386a4SMatt Jacob 			spi->flags = 0;
3851b61386a4SMatt Jacob 			scsi->flags = 0;
3852ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3853ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3854ab163f5fSMatt Jacob 			}
38559ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3856ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3857ab163f5fSMatt Jacob 				spi->sync_period = pval;
3858b61386a4SMatt Jacob 			} else {
3859b61386a4SMatt Jacob 				spi->sync_offset = 0;
3860b61386a4SMatt Jacob 				spi->sync_period = 0;
3861b61386a4SMatt Jacob 			}
3862ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3863ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3864ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3865ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3866ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3867ab163f5fSMatt Jacob 			} else {
3868ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3869ab163f5fSMatt Jacob 			}
3870ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3871ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3872b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3873b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3874b61386a4SMatt Jacob 				}
3875ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3876ab163f5fSMatt Jacob 			}
3877123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3878123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3879478f8a96SJustin T. Gibbs 		}
3880478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3881478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3882478f8a96SJustin T. Gibbs 		break;
3883478f8a96SJustin T. Gibbs 
3884478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
38852df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
38862df76c16SMatt Jacob 		xpt_done(ccb);
38872df76c16SMatt Jacob 		break;
3888478f8a96SJustin T. Gibbs 
3889478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
3890ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
38912df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
38922df76c16SMatt Jacob 		if (error) {
3893478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
38942df76c16SMatt Jacob 			xpt_done(ccb);
38952df76c16SMatt Jacob 			break;
38962df76c16SMatt Jacob 		}
38970a70657fSMatt Jacob 		if (bootverbose) {
38982df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
38990a70657fSMatt Jacob 		}
39002df76c16SMatt Jacob 		if (IS_FC(isp)) {
39012df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
39022df76c16SMatt Jacob 		} else {
39032df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
39042df76c16SMatt Jacob 		}
3905478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3906478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3907478f8a96SJustin T. Gibbs 		break;
3908478f8a96SJustin T. Gibbs 
3909478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3910478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3911478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3912478f8a96SJustin T. Gibbs 		break;
3913478f8a96SJustin T. Gibbs 
39142df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
39152df76c16SMatt Jacob 	{
39162df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
39172df76c16SMatt Jacob 		fcparam *fcp;
39182df76c16SMatt Jacob 
39192df76c16SMatt Jacob 		if (!IS_FC(isp)) {
39202df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
39212df76c16SMatt Jacob 			xpt_done(ccb);
39222df76c16SMatt Jacob 			break;
39232df76c16SMatt Jacob 		}
39242df76c16SMatt Jacob 
39252df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
39262df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
39272df76c16SMatt Jacob 
39282df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
39292df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
39302df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
39312df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
39322df76c16SMatt Jacob 		}
39332df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
39342df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
39352df76c16SMatt Jacob 			int rchange = 0;
39362df76c16SMatt Jacob 			int newrole = 0;
39372df76c16SMatt Jacob 
39382df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
39392df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
39402df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
39412df76c16SMatt Jacob 					rchange = 1;
39422df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
39432df76c16SMatt Jacob 				}
39442df76c16SMatt Jacob 				break;
39452df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
39462df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
39472df76c16SMatt Jacob 					rchange = 1;
39482df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
39492df76c16SMatt Jacob 				}
39502df76c16SMatt Jacob 				break;
39512df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
39522df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
39532df76c16SMatt Jacob 					rchange = 1;
39542df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
39552df76c16SMatt Jacob 				}
39562df76c16SMatt Jacob 				break;
39572df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
39582df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
39592df76c16SMatt Jacob 					rchange = 1;
39602df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
39612df76c16SMatt Jacob 				}
39622df76c16SMatt Jacob 				break;
39632df76c16SMatt Jacob 			}
39642df76c16SMatt Jacob 			if (rchange) {
3965e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
39666bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
39676bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
39682df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3969387d8239SMatt Jacob 					xpt_done(ccb);
3970387d8239SMatt Jacob 					break;
39712df76c16SMatt Jacob 				}
39722df76c16SMatt Jacob 			}
39732df76c16SMatt Jacob 		}
39742df76c16SMatt Jacob 		xpt_done(ccb);
39752df76c16SMatt Jacob 		break;
39762df76c16SMatt Jacob 	}
3977a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3978e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
39792df76c16SMatt Jacob 	{
39802df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
39812df76c16SMatt Jacob 
39822df76c16SMatt Jacob 		if (IS_FC(isp)) {
39832df76c16SMatt Jacob 			fcparam *fcp;
39842df76c16SMatt Jacob 
39852df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
39862df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
39872df76c16SMatt Jacob 
39882df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
39892df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
39902df76c16SMatt Jacob 			switch (fcp->role) {
39912df76c16SMatt Jacob 			case ISP_ROLE_NONE:
39922df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
39932df76c16SMatt Jacob 				break;
39942df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
39952df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
39962df76c16SMatt Jacob 				break;
39972df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
39982df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
39992df76c16SMatt Jacob 				break;
40002df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
40012df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
40022df76c16SMatt Jacob 				break;
40032df76c16SMatt Jacob 			}
40042df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
40052df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
40062df76c16SMatt Jacob 		} else {
40072df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
40082df76c16SMatt Jacob 		}
40092df76c16SMatt Jacob 		xpt_done(ccb);
40102df76c16SMatt Jacob 		break;
40112df76c16SMatt Jacob 	}
4012478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
4013478f8a96SJustin T. Gibbs 	{
4014478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
4015478f8a96SJustin T. Gibbs 
4016478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
4017d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
40183e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
4019a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
40203e6deb33SAlexander Motin 		else
4021d81ba9d5SMatt Jacob #endif
40223e6deb33SAlexander Motin 			cpi->target_sprt = 0;
4023478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
40240470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
40256af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
40266af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
40270470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
40287bf825d1SKenneth D. Merry 		if (isp->isp_osinfo.sixtyfourbit)
40297bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
40307bf825d1SKenneth D. Merry 		else
40317bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
40327bf825d1SKenneth D. Merry 
40332df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
40344394c92fSMatt Jacob 		if (IS_FC(isp)) {
40352df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
40362df76c16SMatt Jacob 
4037ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
40386af11b82SAlexander Motin #if __FreeBSD_version >= 1000700
40396af11b82SAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS;
40406af11b82SAlexander Motin #endif
4041c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
4042c07b9e07SAlexander Motin 			cpi->hba_misc |= PIM_NOSCAN;
4043c07b9e07SAlexander Motin #endif
40442df76c16SMatt Jacob 
40450470d791SMatt Jacob 			/*
40460470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
40470470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
40480470d791SMatt Jacob 			 */
40490470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
40500470d791SMatt Jacob 
40519deea857SKenneth D. Merry 			/*
40522df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
40539deea857SKenneth D. Merry 			 */
4054e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
4055e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
4056e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
40572df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
40582df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
40592df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
40602df76c16SMatt Jacob 			} else {
40612df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
40622df76c16SMatt Jacob 			}
40630470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
4064ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
4065805e1f82SMatt Jacob 			cpi->transport_version = 0;
40662df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
40672df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
40682df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
40692df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4070478f8a96SJustin T. Gibbs 		} else {
40712df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
40720470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4073ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
4074ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
40759deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
4076ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
4077805e1f82SMatt Jacob 			cpi->transport_version = 2;
4078478f8a96SJustin T. Gibbs 		}
4079ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
4080ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
40814195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
40824195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
40834195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4084478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
4085478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
4086478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4087478f8a96SJustin T. Gibbs 		break;
4088478f8a96SJustin T. Gibbs 	}
4089478f8a96SJustin T. Gibbs 	default:
4090478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
4091478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4092478f8a96SJustin T. Gibbs 		break;
4093478f8a96SJustin T. Gibbs 	}
4094478f8a96SJustin T. Gibbs }
4095d3a9eb2eSMatt Jacob 
4096d3a9eb2eSMatt Jacob #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4097f7c631bcSMatt Jacob 
4098d3a9eb2eSMatt Jacob void
40992df76c16SMatt Jacob isp_done(XS_T *sccb)
4100d3a9eb2eSMatt Jacob {
41019cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
4102e95725cbSMatt Jacob 	uint32_t status;
4103d3a9eb2eSMatt Jacob 
4104d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
4105d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
4106b85389e1SMatt Jacob 
41072df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4108d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
41092df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
411092a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
411192a1e549SMatt Jacob 		} else {
4112d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4113d3a9eb2eSMatt Jacob 		}
411492a1e549SMatt Jacob 	}
4115b85389e1SMatt Jacob 
41160470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4117e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
4118e95725cbSMatt Jacob 	if (status != CAM_REQ_CMP) {
4119e95725cbSMatt Jacob 		if (status != CAM_SEL_TIMEOUT)
41206af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
41216af11b82SAlexander Motin 			    "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
41226af11b82SAlexander Motin 			    XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
41236af11b82SAlexander Motin 			    sccb->ccb_h.status, sccb->scsi_status);
41245704e6f0SKenneth D. Merry 		else if ((IS_FC(isp))
41255704e6f0SKenneth D. Merry 		      && (XS_TGT(sccb) < MAX_FC_TARG)) {
41265704e6f0SKenneth D. Merry 			fcparam *fcp;
41275704e6f0SKenneth D. Merry 
41285704e6f0SKenneth D. Merry 			fcp = FCPARAM(isp, XS_CHANNEL(sccb));
4129e68eef14SAlexander Motin 			fcp->portdb[XS_TGT(sccb)].is_target = 0;
41305704e6f0SKenneth D. Merry 		}
4131d3a9eb2eSMatt Jacob 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4132d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
41330470d791SMatt Jacob 			xpt_freeze_devq(sccb->ccb_h.path, 1);
4134d3a9eb2eSMatt Jacob 		}
4135d3a9eb2eSMatt Jacob 	}
4136b85389e1SMatt Jacob 
41372df76c16SMatt Jacob 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
41382df76c16SMatt Jacob 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4139d3a9eb2eSMatt Jacob 	}
4140b85389e1SMatt Jacob 
41415cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
4142387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
41430ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
41440a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
41455cbe3e8eSAlexander Motin 	}
4146d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
4147d3a9eb2eSMatt Jacob }
4148d3a9eb2eSMatt Jacob 
41492df76c16SMatt Jacob void
41502df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4151cbf57b47SMatt Jacob {
4152e68eef14SAlexander Motin 	int bus;
4153e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
4154387d8239SMatt Jacob 	char buf[64];
4155f7c631bcSMatt Jacob 	char *msg = NULL;
415610365e5aSMatt Jacob 	target_id_t tgt;
415710365e5aSMatt Jacob 	fcportdb_t *lp;
4158a01f5aebSMatt Jacob 	struct isp_fc *fc;
415910365e5aSMatt Jacob 	struct cam_path *tmppath;
4160e68eef14SAlexander Motin 	struct ac_contract ac;
4161e68eef14SAlexander Motin 	struct ac_device_changed *adc;
41622df76c16SMatt Jacob 	va_list ap;
416310365e5aSMatt Jacob 
4164cbf57b47SMatt Jacob 	switch (cmd) {
4165cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
41660470d791SMatt Jacob 	{
4167ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
4168ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
4169cbf57b47SMatt Jacob 		int flags, tgt;
41702df76c16SMatt Jacob 		sdparam *sdp;
4171ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
4172cbf57b47SMatt Jacob 
417329f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
4174ab163f5fSMatt Jacob 
41752df76c16SMatt Jacob 		va_start(ap, cmd);
41762df76c16SMatt Jacob 		bus = va_arg(ap, int);
41772df76c16SMatt Jacob 		tgt = va_arg(ap, int);
41782df76c16SMatt Jacob 		va_end(ap);
41792df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
41802df76c16SMatt Jacob 
41812df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
41822df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4183cbf57b47SMatt Jacob 			break;
4184cbf57b47SMatt Jacob 		}
41859ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
4186ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4187ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
4188ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
4189ab163f5fSMatt Jacob 
4190ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
4191ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
4192ab163f5fSMatt Jacob 
4193ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
4194ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
4195ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4196ab163f5fSMatt Jacob 		}
4197ab163f5fSMatt Jacob 
4198cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
4199ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
4200ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4201ab163f5fSMatt Jacob 		}
4202ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4203ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
4204ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4205ab163f5fSMatt Jacob 		} else {
4206ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4207ab163f5fSMatt Jacob 		}
4208ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
4209ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4210ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
42119ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
42129ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4213ab163f5fSMatt Jacob 		}
42142df76c16SMatt Jacob 		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);
4215ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4216ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4217cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
4218cbf57b47SMatt Jacob 		break;
42190470d791SMatt Jacob 	}
422057c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
42212df76c16SMatt Jacob 	{
42222df76c16SMatt Jacob 		va_start(ap, cmd);
42232df76c16SMatt Jacob 		bus = va_arg(ap, int);
42242df76c16SMatt Jacob 		va_end(ap);
42252df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
42262df76c16SMatt Jacob 		if (IS_FC(isp)) {
42272df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
42282df76c16SMatt Jacob 		} else {
42292df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
423057c801f5SMatt Jacob 		}
423157c801f5SMatt Jacob 		break;
42322df76c16SMatt Jacob 	}
42335d571944SMatt Jacob 	case ISPASYNC_LIP:
423437a7daacSAlexander Motin 		if (msg == NULL)
4235f7c631bcSMatt Jacob 			msg = "LIP Received";
4236f7c631bcSMatt Jacob 		/* FALLTHROUGH */
42375d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
423837a7daacSAlexander Motin 		if (msg == NULL)
4239f7c631bcSMatt Jacob 			msg = "LOOP Reset";
4240f7c631bcSMatt Jacob 		/* FALLTHROUGH */
424157c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
424237a7daacSAlexander Motin 		if (msg == NULL)
4243f7c631bcSMatt Jacob 			msg = "LOOP Down";
42442df76c16SMatt Jacob 		va_start(ap, cmd);
42452df76c16SMatt Jacob 		bus = va_arg(ap, int);
42462df76c16SMatt Jacob 		va_end(ap);
424721daf914SAlexander Motin 		isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
4248e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
4249e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
425057c801f5SMatt Jacob 		break;
425157c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
42522df76c16SMatt Jacob 		va_start(ap, cmd);
42532df76c16SMatt Jacob 		bus = va_arg(ap, int);
42542df76c16SMatt Jacob 		va_end(ap);
4255e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
42562df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
425757c801f5SMatt Jacob 		break;
425810365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
42592df76c16SMatt Jacob 		va_start(ap, cmd);
42602df76c16SMatt Jacob 		bus = va_arg(ap, int);
42612df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
42622df76c16SMatt Jacob 		va_end(ap);
4263a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4264e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4265387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4266e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4267e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4268e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4269e68eef14SAlexander Motin 			lp->is_target = 1;
427021daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
42715704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
4272e68eef14SAlexander Motin 		}
4273e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4274e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4275e68eef14SAlexander Motin 			lp->is_initiator = 1;
4276e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4277e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4278e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4279e68eef14SAlexander Motin 			adc->port = lp->portid;
4280d4f3ad3aSAlexander Motin 			adc->target = tgt;
4281e68eef14SAlexander Motin 			adc->arrived = 1;
4282e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
428310365e5aSMatt Jacob 		}
428410365e5aSMatt Jacob 		break;
428510365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
42862df76c16SMatt Jacob 		va_start(ap, cmd);
42872df76c16SMatt Jacob 		bus = va_arg(ap, int);
42882df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
42892df76c16SMatt Jacob 		va_end(ap);
4290a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4291e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4292e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4293e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4294e68eef14SAlexander Motin changed:
4295e68eef14SAlexander Motin 		if (lp->is_target !=
4296e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4297e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4298e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
4299e68eef14SAlexander Motin 			if (lp->is_target) {
430021daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4301e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
430210365e5aSMatt Jacob 			} else {
4303e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
430421daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
430510365e5aSMatt Jacob 			}
4306f7c631bcSMatt Jacob 		}
4307e68eef14SAlexander Motin 		if (lp->is_initiator !=
4308e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4309e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4310e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
4311e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4312e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4313e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4314e68eef14SAlexander Motin 			adc->port = lp->portid;
4315d4f3ad3aSAlexander Motin 			adc->target = tgt;
4316e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
4317e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
4318e68eef14SAlexander Motin 		}
431910365e5aSMatt Jacob 		break;
432010365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
43212df76c16SMatt Jacob 		va_start(ap, cmd);
43222df76c16SMatt Jacob 		bus = va_arg(ap, int);
43232df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
43242df76c16SMatt Jacob 		va_end(ap);
4325fb4a4356SKenneth D. Merry 		fc = ISP_FC_PC(isp, bus);
4326e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4327e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4328e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4329e68eef14SAlexander Motin 		goto changed;
433010365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
43312df76c16SMatt Jacob 		va_start(ap, cmd);
43322df76c16SMatt Jacob 		bus = va_arg(ap, int);
43332df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
43342df76c16SMatt Jacob 		va_end(ap);
4335a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4336e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4337f7c631bcSMatt Jacob 		/*
4338e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
4339e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
4340f7c631bcSMatt Jacob 		 */
4341387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4342e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
4343f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
4344e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
4345e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4346a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
4347387d8239SMatt Jacob 				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
4348a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
4349f7c631bcSMatt Jacob 			}
4350e68eef14SAlexander Motin 			break;
43514b9d588eSMatt Jacob 		}
4352e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
435310365e5aSMatt Jacob 		break;
435410365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
435510365e5aSMatt Jacob 	{
435610365e5aSMatt Jacob 		char *msg;
4357eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
43582df76c16SMatt Jacob 
43592df76c16SMatt Jacob 		va_start(ap, cmd);
43602df76c16SMatt Jacob 		bus = va_arg(ap, int);
43612df76c16SMatt Jacob 		evt = va_arg(ap, int);
4362eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
43632df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
43642df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
43652df76c16SMatt Jacob 			reason = va_arg(ap, int);
4366eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4367eea52482SAlexander Motin 			portid = va_arg(ap, int);
436810365e5aSMatt Jacob 		} else {
43692df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
43702df76c16SMatt Jacob 			nlstate = reason = 0;
437110365e5aSMatt Jacob 		}
43722df76c16SMatt Jacob 		va_end(ap);
4373a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
43742df76c16SMatt Jacob 
43752df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
4376b5d5037bSAlexander Motin 			msg = "Port Database Changed";
4377eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
4378eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4379eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
43802df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4381b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
4382eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4383eea52482SAlexander Motin 			    bus, msg, portid);
43842df76c16SMatt Jacob 		} else {
4385b5d5037bSAlexander Motin 			msg = "Other Change Notify";
4386eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
43872df76c16SMatt Jacob 		}
4388e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
438957c801f5SMatt Jacob 		break;
439002ab3379SMatt Jacob 	}
4391d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4392e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
4393d81ba9d5SMatt Jacob 	{
43942df76c16SMatt Jacob 		isp_notify_t *notify;
43952df76c16SMatt Jacob 		va_start(ap, cmd);
43962df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
43972df76c16SMatt Jacob 		va_end(ap);
43982df76c16SMatt Jacob 		switch (notify->nt_ncode) {
43992df76c16SMatt Jacob 		case NT_ABORT_TASK:
44002df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
44012df76c16SMatt Jacob 		case NT_CLEAR_ACA:
44022df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
44032df76c16SMatt Jacob 		case NT_LUN_RESET:
44042df76c16SMatt Jacob 		case NT_TARGET_RESET:
4405c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
4406c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
44072df76c16SMatt Jacob 			/*
44082df76c16SMatt Jacob 			 * These are task management functions.
44092df76c16SMatt Jacob 			 */
44102df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
44112df76c16SMatt Jacob 			break;
44122df76c16SMatt Jacob 		case NT_BUS_RESET:
44132df76c16SMatt Jacob 		case NT_LIP_RESET:
44142df76c16SMatt Jacob 		case NT_LINK_UP:
44152df76c16SMatt Jacob 		case NT_LINK_DOWN:
4416e68eef14SAlexander Motin 		case NT_HBA_RESET:
44172df76c16SMatt Jacob 			/*
44182df76c16SMatt Jacob 			 * No action need be taken here.
44192df76c16SMatt Jacob 			 */
44202df76c16SMatt Jacob 			break;
4421387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
44222df76c16SMatt Jacob 		case NT_LOGOUT:
44232df76c16SMatt Jacob 			/*
44242df76c16SMatt Jacob 			 * This is device arrival/departure notification
44252df76c16SMatt Jacob 			 */
442696b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
44272df76c16SMatt Jacob 			break;
44282df76c16SMatt Jacob 		default:
44292df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
443096b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
44312df76c16SMatt Jacob 			break;
44322df76c16SMatt Jacob 		}
4433d81ba9d5SMatt Jacob 		break;
4434d81ba9d5SMatt Jacob 	}
4435387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
4436387d8239SMatt Jacob 	{
4437387d8239SMatt Jacob 		void *inot;
4438387d8239SMatt Jacob 		va_start(ap, cmd);
4439387d8239SMatt Jacob 		inot = va_arg(ap, void *);
4440387d8239SMatt Jacob 		va_end(ap);
4441387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
4442387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4443387d8239SMatt Jacob 			if (tp) {
4444387d8239SMatt Jacob 				tp->isp = isp;
4445387d8239SMatt Jacob 				if (inot) {
4446387d8239SMatt Jacob 					memcpy(tp->data, inot, sizeof (tp->data));
4447387d8239SMatt Jacob 					tp->not = tp->data;
4448387d8239SMatt Jacob 				} else {
4449387d8239SMatt Jacob 					tp->not = NULL;
4450387d8239SMatt Jacob 				}
44512a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
44522a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
44532a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
4454387d8239SMatt Jacob 			} else {
4455387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4456387d8239SMatt Jacob 			}
4457387d8239SMatt Jacob 		}
4458387d8239SMatt Jacob 		break;
4459387d8239SMatt Jacob 	}
4460d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
44612df76c16SMatt Jacob 	{
44622df76c16SMatt Jacob 		isphdr_t *hp;
44632df76c16SMatt Jacob 
44642df76c16SMatt Jacob 		va_start(ap, cmd);
44652df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
44662df76c16SMatt Jacob 		va_end(ap);
44672df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
4468cbf57b47SMatt Jacob 		default:
44692df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4470d81ba9d5SMatt Jacob 			break;
4471570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
44723e6deb33SAlexander Motin 			if (IS_24XX(isp)) {
44732df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4474570c7a3fSMatt Jacob 			} else {
44752df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4476570c7a3fSMatt Jacob 			}
4477570c7a3fSMatt Jacob 			break;
4478d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
44792df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4480d81ba9d5SMatt Jacob 			break;
4481d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
44822df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4483d81ba9d5SMatt Jacob 			break;
44842df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
4485d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
4486d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
4487d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
44882df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
4489d81ba9d5SMatt Jacob 			break;
44902df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
44912df76c16SMatt Jacob 		{
44922df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
44932df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
44942df76c16SMatt Jacob 			tstate_t *tptr;
44952df76c16SMatt Jacob 			fcportdb_t *lp;
44962df76c16SMatt Jacob 			uint16_t chan;
44972df76c16SMatt Jacob 			uint32_t sid, did;
44982df76c16SMatt Jacob 
44992df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
45002df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
45012df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
45022df76c16SMatt Jacob 
45032df76c16SMatt Jacob 			nt->nt_hba = isp;
45042df76c16SMatt Jacob 			nt->nt_did = did;
45052df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
45062df76c16SMatt Jacob 			nt->nt_sid = sid;
45072df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
45082df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
45092df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
45102df76c16SMatt Jacob 			} else {
45112df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4512e68eef14SAlexander Motin 				if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
45132df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
45142df76c16SMatt Jacob 				} else {
45152df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
45162df76c16SMatt Jacob 				}
45172df76c16SMatt Jacob 			}
45182df76c16SMatt Jacob 			/*
45192df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
45202df76c16SMatt Jacob 			 */
45212df76c16SMatt Jacob 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
45222df76c16SMatt Jacob 			if (tptr) {
4523315a4d6fSAlexander Motin 				nt->nt_lun = tptr->ts_lun;
45242df76c16SMatt Jacob 				rls_lun_statep(isp, tptr);
45252df76c16SMatt Jacob 			} else {
45262df76c16SMatt Jacob 				nt->nt_lun = LUN_ANY;
45272df76c16SMatt Jacob 			}
45282df76c16SMatt Jacob 			nt->nt_need_ack = 1;
45292df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
45302df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
45312df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
45322df76c16SMatt Jacob 				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)",
45332df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
45342df76c16SMatt Jacob 			} else {
45352df76c16SMatt Jacob 				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)",
45362df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
45372df76c16SMatt Jacob 			}
45382df76c16SMatt Jacob 			nt->nt_channel = chan;
45392df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
45402df76c16SMatt Jacob 			nt->nt_lreserved = hp;
45412df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
45422df76c16SMatt Jacob 			break;
45432df76c16SMatt Jacob 		}
4544d81ba9d5SMatt Jacob 		}
4545d81ba9d5SMatt Jacob 		break;
45462df76c16SMatt Jacob 	}
4547d81ba9d5SMatt Jacob #endif
4548ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
4549ab163f5fSMatt Jacob 	{
45501dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
4551ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
4552ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
4553ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
4554ab163f5fSMatt Jacob 		} else {
4555ab163f5fSMatt Jacob 			mbox6 = 0;
4556ab163f5fSMatt Jacob 		}
45572df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
45580a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
45590a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
45602df76c16SMatt Jacob 		isp_reinit(isp, 1);
45610a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
45620a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4563ab163f5fSMatt Jacob 		break;
4564ab163f5fSMatt Jacob 	}
4565d81ba9d5SMatt Jacob 	default:
4566b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4567cbf57b47SMatt Jacob 		break;
4568cbf57b47SMatt Jacob 	}
4569cbf57b47SMatt Jacob }
4570cbf57b47SMatt Jacob 
457192718a7fSMatt Jacob 
457292718a7fSMatt Jacob /*
457392718a7fSMatt Jacob  * Locks are held before coming here.
457492718a7fSMatt Jacob  */
457592718a7fSMatt Jacob void
45769cd7268eSMatt Jacob isp_uninit(ispsoftc_t *isp)
457792718a7fSMatt Jacob {
457810365e5aSMatt Jacob 	if (IS_24XX(isp)) {
457910365e5aSMatt Jacob 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
458010365e5aSMatt Jacob 	} else {
4581ea6f23cdSMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
458210365e5aSMatt Jacob 	}
458310365e5aSMatt Jacob 	ISP_DISABLE_INTS(isp);
458492718a7fSMatt Jacob }
4585b09b0095SMatt Jacob 
45862df76c16SMatt Jacob uint64_t
45872df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
45882df76c16SMatt Jacob {
45892df76c16SMatt Jacob 	uint64_t seed;
45902df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
45912df76c16SMatt Jacob 
4592a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4593a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4594a193dc4bSAlexander Motin 	if (seed)
4595a193dc4bSAlexander Motin 		return (seed);
4596a193dc4bSAlexander Motin 
4597a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
45982df76c16SMatt Jacob 	if (isactive) {
4599a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4600a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4601a193dc4bSAlexander Motin 		if (seed)
46022df76c16SMatt Jacob 			return (seed);
46032df76c16SMatt Jacob 	}
4604a193dc4bSAlexander Motin 
4605a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4606a193dc4bSAlexander Motin 	if (chan > 0) {
4607a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4608a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4609a193dc4bSAlexander Motin 		if (seed == 0) {
4610a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4611a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
46125cc3786cSMatt Jacob 		}
46132df76c16SMatt Jacob 	}
46142df76c16SMatt Jacob 
4615a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4616a193dc4bSAlexander Motin 	if (seed == 0) {
4617a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4618a193dc4bSAlexander Motin 		if (!iswwnn)
4619a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4620a193dc4bSAlexander Motin 	}
46212df76c16SMatt Jacob 
4622a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4623a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
46242df76c16SMatt Jacob 		/*
46252df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
46262df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4627a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
46282df76c16SMatt Jacob 		 */
4629a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4630a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4631a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
46322df76c16SMatt Jacob 	}
46332df76c16SMatt Jacob 	return (seed);
46342df76c16SMatt Jacob }
46352df76c16SMatt Jacob 
4636b09b0095SMatt Jacob void
46379cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4638b09b0095SMatt Jacob {
4639de461933SMatt Jacob 	int loc;
4640387d8239SMatt Jacob 	char lbuf[200];
4641b09b0095SMatt Jacob 	va_list ap;
4642de461933SMatt Jacob 
4643b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4644b09b0095SMatt Jacob 		return;
4645b09b0095SMatt Jacob 	}
4646387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4647de461933SMatt Jacob 	loc = strlen(lbuf);
4648b09b0095SMatt Jacob 	va_start(ap, fmt);
4649de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4650b09b0095SMatt Jacob 	va_end(ap);
4651de461933SMatt Jacob 	printf("%s\n", lbuf);
4652b09b0095SMatt Jacob }
4653f7c631bcSMatt Jacob 
4654670508b1SMatt Jacob void
4655670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4656670508b1SMatt Jacob {
4657670508b1SMatt Jacob 	va_list ap;
4658670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4659670508b1SMatt Jacob 		return;
4660670508b1SMatt Jacob 	}
4661670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4662670508b1SMatt Jacob 	va_start(ap, fmt);
4663670508b1SMatt Jacob 	vprintf(fmt, ap);
4664670508b1SMatt Jacob 	va_end(ap);
4665670508b1SMatt Jacob 	printf("\n");
4666670508b1SMatt Jacob }
4667670508b1SMatt Jacob 
4668f7c631bcSMatt Jacob uint64_t
4669f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4670f7c631bcSMatt Jacob {
4671f7c631bcSMatt Jacob 	uint64_t elapsed;
4672f7c631bcSMatt Jacob 	struct timespec x = *b;
4673f7c631bcSMatt Jacob 	timespecsub(&x, a);
4674f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4675f7c631bcSMatt Jacob 	if (elapsed == 0)
4676f7c631bcSMatt Jacob 		elapsed++;
4677f7c631bcSMatt Jacob 	return (elapsed);
4678f7c631bcSMatt Jacob }
4679f7c631bcSMatt Jacob 
4680f7c631bcSMatt Jacob int
4681f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4682f7c631bcSMatt Jacob {
4683f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4684f7c631bcSMatt Jacob 		return (1);
4685f7c631bcSMatt Jacob 	} else {
4686f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4687f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4688f7c631bcSMatt Jacob 		return (0);
4689f7c631bcSMatt Jacob 	}
4690f7c631bcSMatt Jacob }
4691f7c631bcSMatt Jacob 
4692f7c631bcSMatt Jacob void
4693f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4694f7c631bcSMatt Jacob {
4695a4f3a2beSMatt Jacob 	unsigned int usecs = mbp->timeout;
4696a4f3a2beSMatt Jacob 	unsigned int max, olim, ilim;
4697f7c631bcSMatt Jacob 
4698f7c631bcSMatt Jacob 	if (usecs == 0) {
4699f7c631bcSMatt Jacob 		usecs = MBCMD_DEFAULT_TIMEOUT;
4700f7c631bcSMatt Jacob 	}
4701a4f3a2beSMatt Jacob 	max = isp->isp_mbxwrk0 + 1;
4702a4f3a2beSMatt Jacob 
4703f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4704a4f3a2beSMatt Jacob 		unsigned int ms = (usecs + 999) / 1000;
4705a4f3a2beSMatt Jacob 
4706f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4707f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
4708a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
47092df76c16SMatt Jacob 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
4710a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4711a4f3a2beSMatt Jacob 				break;
4712a4f3a2beSMatt Jacob 			}
4713a4f3a2beSMatt Jacob 		}
4714f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4715f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4716f7c631bcSMatt Jacob 	} else {
4717a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
4718a4f3a2beSMatt Jacob 			for (ilim = 0; ilim < usecs; ilim += 100) {
47196ce548a1SAlexander Motin 				uint16_t isr, sema, info;
4720f7c631bcSMatt Jacob 				if (isp->isp_osinfo.mboxcmd_done) {
4721f7c631bcSMatt Jacob 					break;
4722f7c631bcSMatt Jacob 				}
47236ce548a1SAlexander Motin 				if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
47246ce548a1SAlexander Motin 					isp_intr(isp, isr, sema, info);
4725f7c631bcSMatt Jacob 					if (isp->isp_osinfo.mboxcmd_done) {
4726f7c631bcSMatt Jacob 						break;
4727f7c631bcSMatt Jacob 					}
4728f7c631bcSMatt Jacob 				}
47292df76c16SMatt Jacob 				ISP_DELAY(100);
4730f7c631bcSMatt Jacob 			}
4731a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4732a4f3a2beSMatt Jacob 				break;
4733a4f3a2beSMatt Jacob 			}
4734a4f3a2beSMatt Jacob 		}
4735f7c631bcSMatt Jacob 	}
4736f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
47372df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
47382df76c16SMatt Jacob 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
4739f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4740f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4741f7c631bcSMatt Jacob 	}
4742f7c631bcSMatt Jacob }
4743f7c631bcSMatt Jacob 
4744f7c631bcSMatt Jacob void
4745f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4746f7c631bcSMatt Jacob {
4747f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleeping) {
4748f7c631bcSMatt Jacob 		wakeup(&isp->isp_mbxworkp);
4749f7c631bcSMatt Jacob 	}
4750f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
4751f7c631bcSMatt Jacob }
4752f7c631bcSMatt Jacob 
4753f7c631bcSMatt Jacob void
4754f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4755f7c631bcSMatt Jacob {
4756f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4757f7c631bcSMatt Jacob }
4758f7c631bcSMatt Jacob 
4759f7c631bcSMatt Jacob int
47602df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
47612df76c16SMatt Jacob {
47622df76c16SMatt Jacob 	int ret = 0;
47632df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
47642df76c16SMatt Jacob 		ret = -1;
47652df76c16SMatt Jacob 	} else {
47662df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
47672df76c16SMatt Jacob 	}
47682df76c16SMatt Jacob 	return (ret);
47692df76c16SMatt Jacob }
47702df76c16SMatt Jacob 
47712df76c16SMatt Jacob int
4772f7c631bcSMatt Jacob isp_mstohz(int ms)
4773f7c631bcSMatt Jacob {
4774a4f3a2beSMatt Jacob 	int hz;
4775f7c631bcSMatt Jacob 	struct timeval t;
4776f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
4777f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
4778a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
4779a4f3a2beSMatt Jacob 	if (hz < 0) {
4780a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
4781f7c631bcSMatt Jacob 	}
4782a4f3a2beSMatt Jacob 	if (hz == 0) {
4783a4f3a2beSMatt Jacob 		hz = 1;
4784a4f3a2beSMatt Jacob 	}
4785a4f3a2beSMatt Jacob 	return (hz);
4786f7c631bcSMatt Jacob }
47870a70657fSMatt Jacob 
47880a70657fSMatt Jacob void
47890a70657fSMatt Jacob isp_platform_intr(void *arg)
47900a70657fSMatt Jacob {
47910a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
47926ce548a1SAlexander Motin 	uint16_t isr, sema, info;
47930a70657fSMatt Jacob 
47940a70657fSMatt Jacob 	ISP_LOCK(isp);
47950a70657fSMatt Jacob 	isp->isp_intcnt++;
47966ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
47976ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
47986ce548a1SAlexander Motin 	else
47990a70657fSMatt Jacob 		isp->isp_intbogus++;
48000a70657fSMatt Jacob 	ISP_UNLOCK(isp);
48010a70657fSMatt Jacob }
48020a70657fSMatt Jacob 
48030a70657fSMatt Jacob void
48040a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
48050a70657fSMatt Jacob {
48060a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
48072df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
48080a70657fSMatt Jacob 	} else {
48092df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
48100a70657fSMatt Jacob 	}
48110a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
48120a70657fSMatt Jacob }
48132df76c16SMatt Jacob 
4814fb4a4356SKenneth D. Merry /*
4815fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4816fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4817fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4818fb4a4356SKenneth D. Merry  */
4819fb4a4356SKenneth D. Merry void
482021daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4821fb4a4356SKenneth D. Merry {
482221daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4823fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
482421daf914SAlexander Motin 	int i;
4825fb4a4356SKenneth D. Merry 
4826fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
482721daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
482821daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4829fb4a4356SKenneth D. Merry 	else
483021daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
483121daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4832fb4a4356SKenneth D. Merry 
4833fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
483421daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
483521daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4836fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4837fb4a4356SKenneth D. Merry 		}
4838fb4a4356SKenneth D. Merry 	}
4839fb4a4356SKenneth D. Merry }
4840fb4a4356SKenneth D. Merry 
4841387d8239SMatt Jacob int
4842387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4843387d8239SMatt Jacob {
48446af11b82SAlexander Motin 	lun_id_t lun;
48456af11b82SAlexander Motin 	uint32_t chan, tgt;
4846405b7a29SMatt Jacob 	struct isp_fc *fc;
4847405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4848405b7a29SMatt Jacob 	int idx;
4849405b7a29SMatt Jacob 
4850a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4851405b7a29SMatt Jacob 		return (0);
4852405b7a29SMatt Jacob 
4853405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4854405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4855405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4856405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4857405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4858405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4859387d8239SMatt Jacob 
4860387d8239SMatt Jacob 	while (nxp) {
4861387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4862387d8239SMatt Jacob 			break;
4863387d8239SMatt Jacob 		nxp = nxp->next;
4864387d8239SMatt Jacob 	}
4865387d8239SMatt Jacob 	if (nxp == NULL) {
4866387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4867387d8239SMatt Jacob 		if (nxp == NULL) {
4868387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4869387d8239SMatt Jacob 			if (nxp == NULL) {
4870387d8239SMatt Jacob 				return (-1);
4871387d8239SMatt Jacob 			}
4872387d8239SMatt Jacob 		} else {
4873387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4874387d8239SMatt Jacob 		}
4875387d8239SMatt Jacob 		nxp->tgt = tgt;
4876387d8239SMatt Jacob 		nxp->lun = lun;
4877387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4878387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4879387d8239SMatt Jacob 	}
4880387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4881387d8239SMatt Jacob 		nxp->crnseed = 1;
4882387d8239SMatt Jacob 	PISP_PCMD(cmd)->crn = nxp->crnseed;
4883387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4884387d8239SMatt Jacob 	return (0);
4885387d8239SMatt Jacob }
4886387d8239SMatt Jacob 
488794dff771SMatt Jacob /*
488894dff771SMatt Jacob  * We enter with the lock held
488994dff771SMatt Jacob  */
48902df76c16SMatt Jacob void
48912df76c16SMatt Jacob isp_timer(void *arg)
48922df76c16SMatt Jacob {
48932df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
48942df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
48952df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
48962df76c16SMatt Jacob #endif
489794dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
48982df76c16SMatt Jacob }
4899387d8239SMatt Jacob 
4900387d8239SMatt Jacob isp_ecmd_t *
4901387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4902387d8239SMatt Jacob {
4903387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4904387d8239SMatt Jacob 	if (ecmd) {
4905387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4906387d8239SMatt Jacob 	}
4907387d8239SMatt Jacob 	return (ecmd);
4908387d8239SMatt Jacob }
4909387d8239SMatt Jacob 
4910387d8239SMatt Jacob void
4911387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4912387d8239SMatt Jacob {
4913387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4914387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4915387d8239SMatt Jacob }
4916