xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 26719198a7f19017b14bd1653190b58f7bcbfaa9)
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 
444eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4556c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4673030e03SMatt Jacob int isp_announced = 0;
472df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
482df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
49f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
50e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5173030e03SMatt Jacob 
52e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
53e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
545d571944SMatt Jacob static d_ioctl_t ispioctl;
55f6e75de2SMatt Jacob static void isp_intr_enable(void *);
561dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
570470d791SMatt Jacob static void isp_poll(struct cam_sim *);
58b85389e1SMatt Jacob static timeout_t isp_watchdog;
59de461933SMatt Jacob static timeout_t isp_gdt;
60de461933SMatt Jacob static task_fn_t isp_gdt_task;
615d571944SMatt Jacob static void isp_kthread(void *);
62d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6394dff771SMatt Jacob static int isp_timer_count;
642df76c16SMatt Jacob static void isp_timer(void *);
65cc8df88bSMatt Jacob 
665d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
67dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
687ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
697ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
705d571944SMatt Jacob };
715d571944SMatt Jacob 
722df76c16SMatt Jacob static int
7307f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7407f56f1cSAlexander Motin {
7507f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7607f56f1cSAlexander Motin 	int chan = arg2;
7707f56f1cSAlexander Motin 	int error, old, value;
7807f56f1cSAlexander Motin 
7907f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8007f56f1cSAlexander Motin 
8107f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8207f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8307f56f1cSAlexander Motin 		return (error);
8407f56f1cSAlexander Motin 
8507f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8607f56f1cSAlexander Motin 		return (EINVAL);
8707f56f1cSAlexander Motin 
8807f56f1cSAlexander Motin 	ISP_LOCK(isp);
8907f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9007f56f1cSAlexander Motin 
91391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
92391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
93391f03daSAlexander Motin 
9407f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9507f56f1cSAlexander Motin 	if (value == old) {
9607f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9707f56f1cSAlexander Motin 		return (0);
9807f56f1cSAlexander Motin 	}
9907f56f1cSAlexander Motin 
10007f56f1cSAlexander Motin 	/* Actually change the role. */
1016bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10207f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10307f56f1cSAlexander Motin 	return (error);
10407f56f1cSAlexander Motin }
10507f56f1cSAlexander Motin 
10607f56f1cSAlexander Motin static int
1072df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
108478f8a96SJustin T. Gibbs {
109478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
110ea6f23cdSMatt Jacob 	struct cam_sim *sim;
111ea6f23cdSMatt Jacob 	struct cam_path *path;
112478f8a96SJustin T. Gibbs 
113478f8a96SJustin T. Gibbs 	/*
1142df76c16SMatt Jacob 	 * Construct our SIM entry.
115ea6f23cdSMatt Jacob 	 */
1162df76c16SMatt 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);
117ea6f23cdSMatt Jacob 
1182df76c16SMatt Jacob 	if (sim == NULL) {
1192df76c16SMatt Jacob 		return (ENOMEM);
1202df76c16SMatt Jacob 	}
1212df76c16SMatt Jacob 
1222df76c16SMatt Jacob 	ISP_LOCK(isp);
1232df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1242df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1252df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1262df76c16SMatt Jacob 		return (EIO);
1272df76c16SMatt Jacob 	}
1282df76c16SMatt Jacob 	ISP_UNLOCK(isp);
129227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1302df76c16SMatt Jacob 		ISP_LOCK(isp);
1312df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1322df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1332df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1342df76c16SMatt Jacob 		return (ENXIO);
1352df76c16SMatt Jacob 	}
1362df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1372df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1382df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1392df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1402df76c16SMatt Jacob 	csa.callback_arg = sim;
141387d8239SMatt Jacob 
142387d8239SMatt Jacob 	ISP_LOCK(isp);
1432df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
144387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1452df76c16SMatt Jacob 
1462df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1472df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1482df76c16SMatt Jacob 		spi->sim = sim;
1492df76c16SMatt Jacob 		spi->path = path;
1500d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
15109ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1520d965629SAlexander Motin #endif
1532df76c16SMatt Jacob 	} else {
154a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1552df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
15607f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
15707f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
15807f56f1cSAlexander Motin 		char name[16];
1592df76c16SMatt Jacob 
160a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1612df76c16SMatt Jacob 		fc->sim = sim;
1622df76c16SMatt Jacob 		fc->path = path;
1632df76c16SMatt Jacob 		fc->isp = isp;
164a01f5aebSMatt Jacob 		fc->ready = 1;
165de461933SMatt Jacob 
1662df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
167de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
1680d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
16909ddc7adSAlexander Motin 		TAILQ_INIT(&fc->waitq);
1700d965629SAlexander Motin #endif
171e561aa79SAlexander Motin 		isp_loop_changed(isp, chan);
1722df76c16SMatt Jacob 		ISP_UNLOCK(isp);
173069f5ef9SAlexander Motin 		if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
174069f5ef9SAlexander Motin 		    "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
175a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
176a01f5aebSMatt Jacob 			ISP_LOCK(isp);
177a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
178a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
179a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
180a01f5aebSMatt Jacob 			return (ENOMEM);
181a01f5aebSMatt Jacob 		}
182e596ff7aSAlexander Motin 		fc->num_threads += 1;
18307f56f1cSAlexander Motin 		if (chan > 0) {
18407f56f1cSAlexander Motin 			snprintf(name, sizeof(name), "chan%d", chan);
18507f56f1cSAlexander Motin 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
18607f56f1cSAlexander Motin 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
187a0ec8e99SMatt Jacob 		}
188e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
189e596ff7aSAlexander Motin 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
190e596ff7aSAlexander Motin 		    "World Wide Node Name");
191e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
192e596ff7aSAlexander Motin 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
193e596ff7aSAlexander Motin 		    "World Wide Port Name");
194e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
195e596ff7aSAlexander Motin 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
196e596ff7aSAlexander Motin 		    "Loop Down Limit");
197e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
198e596ff7aSAlexander Motin 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
199e596ff7aSAlexander Motin 		    "Gone Device Time");
20007f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
201e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
202e596ff7aSAlexander Motin 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
203e596ff7aSAlexander Motin 		    "Cause a Lost Frame on a Read");
20407f56f1cSAlexander Motin #endif
20507f56f1cSAlexander Motin 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
20607f56f1cSAlexander Motin 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
20707f56f1cSAlexander Motin 		    isp_role_sysctl, "I", "Current role");
208e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
209e596ff7aSAlexander Motin 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
210e596ff7aSAlexander Motin 		    "Connection speed in gigabits");
211e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
212e596ff7aSAlexander Motin 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
213e596ff7aSAlexander Motin 		    "Link state");
214e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
215e596ff7aSAlexander Motin 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
216e596ff7aSAlexander Motin 		    "Firmware state");
217e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
218e596ff7aSAlexander Motin 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
219e596ff7aSAlexander Motin 		    "Loop state");
220e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
221e596ff7aSAlexander Motin 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
222e596ff7aSAlexander Motin 		    "Connection topology");
2232df76c16SMatt Jacob 	}
2242df76c16SMatt Jacob 	return (0);
2252df76c16SMatt Jacob }
2262df76c16SMatt Jacob 
2271d0a1de2SWill Andrews static void
2281d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2291d0a1de2SWill Andrews {
2301d0a1de2SWill Andrews 	struct cam_sim *sim;
2311d0a1de2SWill Andrews 	struct cam_path *path;
2321d0a1de2SWill Andrews 	struct ccb_setasync csa;
2331d0a1de2SWill Andrews 	int *num_threads;
2341d0a1de2SWill Andrews 
2351d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2361d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2371d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2381d0a1de2SWill Andrews 
2391d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2401d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2411d0a1de2SWill Andrews 	csa.event_enable = 0;
2421d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2431d0a1de2SWill Andrews 	csa.callback_arg = sim;
2441d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2451d0a1de2SWill Andrews 	xpt_free_path(path);
2461d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2471d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2481d0a1de2SWill Andrews 
2491d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2501d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2511d0a1de2SWill Andrews 	while (*num_threads != 0)
2521d0a1de2SWill Andrews 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
2531d0a1de2SWill Andrews }
2541d0a1de2SWill Andrews 
2552df76c16SMatt Jacob int
2562df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2572df76c16SMatt Jacob {
2582df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2592df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2602df76c16SMatt Jacob 	int chan;
2612df76c16SMatt Jacob 
2622df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
2632df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_arg = isp;
264e95725cbSMatt Jacob 	/*
265e95725cbSMatt Jacob 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
266e95725cbSMatt Jacob 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
267e95725cbSMatt Jacob 	 * which would be unwise to then set again later.
268e95725cbSMatt Jacob 	 */
269e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 1;
2702df76c16SMatt Jacob 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
2712df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
2722df76c16SMatt Jacob 		return (-EIO);
2732df76c16SMatt Jacob 	}
274ea6f23cdSMatt Jacob 
275ea6f23cdSMatt Jacob 	/*
276ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
277478f8a96SJustin T. Gibbs 	 */
2782df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2792df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2805d571944SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2812df76c16SMatt Jacob 		return (EIO);
282478f8a96SJustin T. Gibbs 	}
283478f8a96SJustin T. Gibbs 
2842df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2852df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2862df76c16SMatt Jacob 			goto unwind;
287ea6f23cdSMatt Jacob 		}
288ea6f23cdSMatt Jacob 	}
289ea6f23cdSMatt Jacob 
2902df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
29194dff771SMatt Jacob 	isp_timer_count = hz >> 2;
29294dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
2932df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
294ea6f23cdSMatt Jacob 
2952df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
2962df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2972df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
298ea6f23cdSMatt Jacob 	}
2992df76c16SMatt Jacob 	return (0);
3005d571944SMatt Jacob 
3012df76c16SMatt Jacob unwind:
3022df76c16SMatt Jacob 	while (--chan >= 0) {
3032df76c16SMatt Jacob 		struct cam_sim *sim;
3042df76c16SMatt Jacob 		struct cam_path *path;
3051d0a1de2SWill Andrews 
3061d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3071d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3082df76c16SMatt Jacob 		xpt_free_path(path);
3090a70657fSMatt Jacob 		ISP_LOCK(isp);
3100a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3112df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3122df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3132df76c16SMatt Jacob 	}
3142df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3150a70657fSMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3162df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3170a70657fSMatt Jacob 	}
3182df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3192df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3202df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3212df76c16SMatt Jacob 	}
3222df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3232df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3242df76c16SMatt Jacob 	return (-1);
3252df76c16SMatt Jacob }
3262df76c16SMatt Jacob 
327e95725cbSMatt Jacob int
3282df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3292df76c16SMatt Jacob {
330e95725cbSMatt Jacob 	struct cam_sim *sim;
3312df76c16SMatt Jacob 	int chan;
3322df76c16SMatt Jacob 
3330a70657fSMatt Jacob 	ISP_LOCK(isp);
3342df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
3351d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
336e95725cbSMatt Jacob 		if (sim->refcount > 2) {
3372df76c16SMatt Jacob 			ISP_UNLOCK(isp);
338e95725cbSMatt Jacob 			return (EBUSY);
339e95725cbSMatt Jacob 		}
340e95725cbSMatt Jacob 	}
3411d0a1de2SWill Andrews 	/* Tell spawned threads that we're exiting. */
3421d0a1de2SWill Andrews 	isp->isp_osinfo.is_exiting = 1;
343e95725cbSMatt Jacob 	if (isp->isp_osinfo.timer_active) {
344e95725cbSMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
345e95725cbSMatt Jacob 		isp->isp_osinfo.timer_active = 0;
346e95725cbSMatt Jacob 	}
3471d0a1de2SWill Andrews 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
3481d0a1de2SWill Andrews 		isp_detach_chan(isp, chan);
349387d8239SMatt Jacob 	ISP_UNLOCK(isp);
3501d0a1de2SWill Andrews 
3512df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3522df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3532df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3542df76c16SMatt Jacob 	}
3552df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3562df76c16SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3572df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3582df76c16SMatt Jacob 	}
359a035b0afSMatt Jacob 	if (isp->isp_osinfo.devq != NULL) {
3602df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
3612df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
3620a70657fSMatt Jacob 	}
363e95725cbSMatt Jacob 	return (0);
3645d571944SMatt Jacob }
3655d571944SMatt Jacob 
366f7c631bcSMatt Jacob static void
367e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
368fdeb9f2fSMatt Jacob {
3692df76c16SMatt Jacob 	if (IS_FC(isp)) {
3702df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
3712df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
372b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
373e561aa79SAlexander Motin 			    "Chan %d Freeze simq (loopdown)", chan);
3742df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
375c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
376c07b9e07SAlexander Motin 			xpt_hold_boot();
377c07b9e07SAlexander Motin #endif
3782df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
379fdeb9f2fSMatt Jacob 		} else {
380b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
381e561aa79SAlexander Motin 			    "Chan %d Mark simq frozen (loopdown)", chan);
3822df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3832df76c16SMatt Jacob 		}
384fdeb9f2fSMatt Jacob 	}
385fdeb9f2fSMatt Jacob }
386fdeb9f2fSMatt Jacob 
387e95725cbSMatt Jacob static void
388e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
389e95725cbSMatt Jacob {
390e95725cbSMatt Jacob 	if (IS_FC(isp)) {
391e95725cbSMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
392e95725cbSMatt Jacob 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
393e95725cbSMatt Jacob 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
394e95725cbSMatt Jacob 		if (wasfrozen && fc->simqfrozen == 0) {
395e561aa79SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
396e561aa79SAlexander Motin 			    "Chan %d Release simq", chan);
397e95725cbSMatt Jacob 			xpt_release_simq(fc->sim, 1);
398c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
399c07b9e07SAlexander Motin 			xpt_release_boot();
400c07b9e07SAlexander Motin #endif
401e95725cbSMatt Jacob 		}
402e95725cbSMatt Jacob 	}
403e95725cbSMatt Jacob }
404e95725cbSMatt Jacob 
4059cd7268eSMatt Jacob 
4065d571944SMatt Jacob static int
4072df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
4085d571944SMatt Jacob {
4099cd7268eSMatt Jacob 	ispsoftc_t *isp;
4102df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
4115d571944SMatt Jacob 
4122df76c16SMatt Jacob 	isp = dev->si_drv1;
4135d571944SMatt Jacob 
414c1504bc0SMatt Jacob 	switch (c) {
4155d571944SMatt Jacob 	case ISP_SDBLEV:
4165d571944SMatt Jacob 	{
4175d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
4185d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
4195d571944SMatt Jacob 		*(int *)addr = olddblev;
4205d571944SMatt Jacob 		retval = 0;
4215d571944SMatt Jacob 		break;
4225d571944SMatt Jacob 	}
423746e9c85SMatt Jacob 	case ISP_GETROLE:
4242df76c16SMatt Jacob 		chan = *(int *)addr;
4252df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4262df76c16SMatt Jacob 			retval = -ENXIO;
4272df76c16SMatt Jacob 			break;
4282df76c16SMatt Jacob 		}
4292df76c16SMatt Jacob 		if (IS_FC(isp)) {
4302df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4312df76c16SMatt Jacob 		} else {
4323e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4332df76c16SMatt Jacob 		}
434746e9c85SMatt Jacob 		retval = 0;
435746e9c85SMatt Jacob 		break;
436746e9c85SMatt Jacob 	case ISP_SETROLE:
4373e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4383e6deb33SAlexander Motin 			break;
439746e9c85SMatt Jacob 		nr = *(int *)addr;
4402df76c16SMatt Jacob 		chan = nr >> 8;
4412df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4422df76c16SMatt Jacob 			retval = -ENXIO;
4432df76c16SMatt Jacob 			break;
4442df76c16SMatt Jacob 		}
4452df76c16SMatt Jacob 		nr &= 0xff;
446746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
447746e9c85SMatt Jacob 			retval = EINVAL;
448746e9c85SMatt Jacob 			break;
449746e9c85SMatt Jacob 		}
4502df76c16SMatt Jacob 		ISP_LOCK(isp);
4516bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4526bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4536bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4542df76c16SMatt Jacob 		retval = 0;
4552df76c16SMatt Jacob 		break;
4562df76c16SMatt Jacob 
4575d571944SMatt Jacob 	case ISP_RESETHBA:
458511ced9bSMatt Jacob 		ISP_LOCK(isp);
4592df76c16SMatt Jacob 		isp_reinit(isp, 0);
460511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4615d571944SMatt Jacob 		retval = 0;
4625d571944SMatt Jacob 		break;
4632df76c16SMatt Jacob 
464f553351eSMatt Jacob 	case ISP_RESCAN:
4655d571944SMatt Jacob 		if (IS_FC(isp)) {
4662df76c16SMatt Jacob 			chan = *(int *)addr;
4672df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4682df76c16SMatt Jacob 				retval = -ENXIO;
4692df76c16SMatt Jacob 				break;
4702df76c16SMatt Jacob 			}
471511ced9bSMatt Jacob 			ISP_LOCK(isp);
472e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4735d571944SMatt Jacob 				retval = EIO;
4745d571944SMatt Jacob 			} else {
4755d571944SMatt Jacob 				retval = 0;
4765d571944SMatt Jacob 			}
477511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4785d571944SMatt Jacob 		}
4795d571944SMatt Jacob 		break;
4802df76c16SMatt Jacob 
4815d571944SMatt Jacob 	case ISP_FC_LIP:
4825d571944SMatt Jacob 		if (IS_FC(isp)) {
4832df76c16SMatt Jacob 			chan = *(int *)addr;
4842df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4852df76c16SMatt Jacob 				retval = -ENXIO;
4862df76c16SMatt Jacob 				break;
4872df76c16SMatt Jacob 			}
488511ced9bSMatt Jacob 			ISP_LOCK(isp);
4892df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4905d571944SMatt Jacob 				retval = EIO;
4915d571944SMatt Jacob 			} else {
4925d571944SMatt Jacob 				retval = 0;
4935d571944SMatt Jacob 			}
494511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4955d571944SMatt Jacob 		}
4965d571944SMatt Jacob 		break;
4975d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4985d571944SMatt Jacob 	{
4995d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
50010365e5aSMatt Jacob 		fcportdb_t *lp;
5015d571944SMatt Jacob 
50202e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
50302e2b2d9SMatt Jacob 			break;
50402e2b2d9SMatt Jacob 		}
5052e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
5065d571944SMatt Jacob 			retval = EINVAL;
5075d571944SMatt Jacob 			break;
5085d571944SMatt Jacob 		}
5092df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
510e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
511387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
51210365e5aSMatt Jacob 			ifc->loopid = lp->handle;
5135d571944SMatt Jacob 			ifc->portid = lp->portid;
5145d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
5155d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
5165d571944SMatt Jacob 			retval = 0;
5175d571944SMatt Jacob 		} else {
5185d571944SMatt Jacob 			retval = ENODEV;
5195d571944SMatt Jacob 		}
5205d571944SMatt Jacob 		break;
5215d571944SMatt Jacob 	}
5222903b272SMatt Jacob 	case ISP_GET_STATS:
5232903b272SMatt Jacob 	{
5242903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
5252903b272SMatt Jacob 
5262df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
5272903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
5282903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
5292903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
530511ced9bSMatt Jacob 		ISP_LOCK(isp);
5312903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
5322903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
5332903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
5342903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
5352903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
5362903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
5372903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
5382903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
539511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5402903b272SMatt Jacob 		retval = 0;
5412903b272SMatt Jacob 		break;
5422903b272SMatt Jacob 	}
5432903b272SMatt Jacob 	case ISP_CLR_STATS:
544511ced9bSMatt Jacob 		ISP_LOCK(isp);
5452903b272SMatt Jacob 		isp->isp_intcnt = 0;
5462903b272SMatt Jacob 		isp->isp_intbogus = 0;
5472903b272SMatt Jacob 		isp->isp_intmboxc = 0;
5482903b272SMatt Jacob 		isp->isp_intoasync = 0;
5492903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
5502903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
5512903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
5522903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
553511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5542903b272SMatt Jacob 		retval = 0;
5552903b272SMatt Jacob 		break;
556570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
557570c7a3fSMatt Jacob 	{
558570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5592df76c16SMatt Jacob 		int chan = hba->fc_channel;
56002e2b2d9SMatt Jacob 
5612df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5622df76c16SMatt Jacob 			retval = ENXIO;
5632df76c16SMatt Jacob 			break;
5642df76c16SMatt Jacob 		}
565a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
566a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
567a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5682df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
56902e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5702df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5712df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5722df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5732df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5742df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5752df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5762df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5772df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5782df76c16SMatt Jacob 		} else {
5792df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5802df76c16SMatt Jacob 			hba->fc_speed = 0;
5812df76c16SMatt Jacob 			hba->fc_topology = 0;
5822df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5832df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5842df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5852df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
58602e2b2d9SMatt Jacob 		}
587570c7a3fSMatt Jacob 		retval = 0;
588570c7a3fSMatt Jacob 		break;
589570c7a3fSMatt Jacob 	}
5908e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5918e62a8acSMatt Jacob 	{
5928e62a8acSMatt Jacob 		int needmarker;
5938e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
594c5fd36edSAlexander Motin 		uint16_t nphdl;
5958e62a8acSMatt Jacob 		mbreg_t mbs;
5968e62a8acSMatt Jacob 
5978e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5988e62a8acSMatt Jacob 			break;
5998e62a8acSMatt Jacob 		}
6008e62a8acSMatt Jacob 
6012df76c16SMatt Jacob 		chan = fct->chan;
6022df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
6032df76c16SMatt Jacob 			retval = -ENXIO;
6042df76c16SMatt Jacob 			break;
6052df76c16SMatt Jacob 		}
6062df76c16SMatt Jacob 
6078e62a8acSMatt Jacob 		needmarker = retval = 0;
608c5fd36edSAlexander Motin 		nphdl = fct->loopid;
6092df76c16SMatt Jacob 		ISP_LOCK(isp);
6102df76c16SMatt Jacob 		if (IS_24XX(isp)) {
61153791a95SAlexander Motin 			void *reqp;
61253791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
61353791a95SAlexander Motin 			isp24xx_tmf_t tmf;
61453791a95SAlexander Motin 			isp24xx_statusreq_t sp;
6152df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
6162df76c16SMatt Jacob 			fcportdb_t *lp;
6172df76c16SMatt Jacob 			int i;
6182df76c16SMatt Jacob 
6192df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
6202df76c16SMatt Jacob 				lp = &fcp->portdb[i];
621c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
6222df76c16SMatt Jacob 					break;
6232df76c16SMatt Jacob 				}
6242df76c16SMatt Jacob 			}
6252df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
6262df76c16SMatt Jacob 				retval = ENXIO;
6272df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6282df76c16SMatt Jacob 				break;
6292df76c16SMatt Jacob 			}
63053791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
63153791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
63253791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
63353791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
63453791a95SAlexander Motin 			tmf.tmf_delay = 2;
63553791a95SAlexander Motin 			tmf.tmf_timeout = 4;
63653791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
63753791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
63853791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
63953791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
6402df76c16SMatt Jacob 			if (fct->lun >= 256) {
64153791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
6422df76c16SMatt Jacob 			}
6432df76c16SMatt Jacob 			switch (fct->action) {
6442df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
64553791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
6462df76c16SMatt Jacob 				break;
6472df76c16SMatt Jacob 			case IPT_TARGET_RESET:
64853791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
6492df76c16SMatt Jacob 				needmarker = 1;
6502df76c16SMatt Jacob 				break;
6512df76c16SMatt Jacob 			case IPT_LUN_RESET:
65253791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
6532df76c16SMatt Jacob 				needmarker = 1;
6542df76c16SMatt Jacob 				break;
6552df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
65653791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6572df76c16SMatt Jacob 				needmarker = 1;
6582df76c16SMatt Jacob 				break;
6592df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
66053791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6612df76c16SMatt Jacob 				needmarker = 1;
6622df76c16SMatt Jacob 				break;
6632df76c16SMatt Jacob 			default:
6642df76c16SMatt Jacob 				retval = EINVAL;
6652df76c16SMatt Jacob 				break;
6662df76c16SMatt Jacob 			}
6672df76c16SMatt Jacob 			if (retval) {
6682df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6692df76c16SMatt Jacob 				break;
6702df76c16SMatt Jacob 			}
6712df76c16SMatt Jacob 
67253791a95SAlexander Motin 			/* Prepare space for response in memory */
67353791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
67453791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
67553791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
67653791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
67753791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
67853791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
67953791a95SAlexander Motin 				    __func__, chan);
6802df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6812df76c16SMatt Jacob 				retval = ENOMEM;
6822df76c16SMatt Jacob 				break;
6832df76c16SMatt Jacob 			}
68453791a95SAlexander Motin 
68553791a95SAlexander Motin 			/* Send request and wait for response. */
68653791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
68753791a95SAlexander Motin 			if (reqp == NULL) {
68853791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
68953791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
69053791a95SAlexander Motin 				    __func__, chan);
69153791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
69253791a95SAlexander Motin 				ISP_UNLOCK(isp);
6932df76c16SMatt Jacob 				retval = EIO;
69453791a95SAlexander Motin 				break;
6952df76c16SMatt Jacob 			}
69653791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
69753791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
69853791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
69953791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
70053791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
70153791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
70253791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
70353791a95SAlexander Motin 				    __func__, chan);
70453791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
70553791a95SAlexander Motin 				ISP_UNLOCK(isp);
70653791a95SAlexander Motin 				retval = EIO;
70753791a95SAlexander Motin 				break;
70853791a95SAlexander Motin 			}
70953791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
71053791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
71153791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
71253791a95SAlexander Motin 
71353791a95SAlexander Motin 			if (sp.req_completion_status != 0)
71453791a95SAlexander Motin 				retval = EIO;
71553791a95SAlexander Motin 			else if (needmarker)
7162df76c16SMatt Jacob 				fcp->sendmarker = 1;
7172df76c16SMatt Jacob 		} else {
7182df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
7192df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
720c5fd36edSAlexander Motin 				nphdl <<= 8;
721e5265237SMatt Jacob 			}
7228e62a8acSMatt Jacob 			switch (fct->action) {
723f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
7248e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
725c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7268e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7278e62a8acSMatt Jacob 				break;
728f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
7298e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
730c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7318e62a8acSMatt Jacob 				needmarker = 1;
7328e62a8acSMatt Jacob 				break;
733f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
7348e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
735c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7368e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7378e62a8acSMatt Jacob 				needmarker = 1;
7388e62a8acSMatt Jacob 				break;
739f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
7408e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
741c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7428e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7438e62a8acSMatt Jacob 				needmarker = 1;
7448e62a8acSMatt Jacob 				break;
745f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
7468e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
747c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7488e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7498e62a8acSMatt Jacob 				needmarker = 1;
7508e62a8acSMatt Jacob 				break;
7518e62a8acSMatt Jacob 			default:
7528e62a8acSMatt Jacob 				retval = EINVAL;
7538e62a8acSMatt Jacob 				break;
7548e62a8acSMatt Jacob 			}
7558e62a8acSMatt Jacob 			if (retval == 0) {
7568e62a8acSMatt Jacob 				if (needmarker) {
7572df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7588e62a8acSMatt Jacob 				}
7598e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7602df76c16SMatt Jacob 				if (retval) {
7618e62a8acSMatt Jacob 					retval = EIO;
7628e62a8acSMatt Jacob 				}
7632df76c16SMatt Jacob 			}
7642df76c16SMatt Jacob 		}
7652df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7668e62a8acSMatt Jacob 		break;
7678e62a8acSMatt Jacob 	}
7685d571944SMatt Jacob 	default:
7695d571944SMatt Jacob 		break;
7705d571944SMatt Jacob 	}
7715d571944SMatt Jacob 	return (retval);
7720470d791SMatt Jacob }
773478f8a96SJustin T. Gibbs 
774f6e75de2SMatt Jacob static void
775f6e75de2SMatt Jacob isp_intr_enable(void *arg)
776f6e75de2SMatt Jacob {
7772df76c16SMatt Jacob 	int chan;
7789cd7268eSMatt Jacob 	ispsoftc_t *isp = arg;
7790a70657fSMatt Jacob 	ISP_LOCK(isp);
7802df76c16SMatt Jacob 	if (IS_FC(isp)) {
7813e6deb33SAlexander Motin 		for (chan = 0; chan < isp->isp_nchan; chan++) {
7822df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
78310365e5aSMatt Jacob 				ISP_ENABLE_INTS(isp);
7842df76c16SMatt Jacob 				break;
7852df76c16SMatt Jacob 			}
7863e6deb33SAlexander Motin 		}
7872df76c16SMatt Jacob 	} else {
7882df76c16SMatt Jacob 		ISP_ENABLE_INTS(isp);
789d6e5500fSMatt Jacob 	}
790e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 0;
7910a70657fSMatt Jacob 	ISP_UNLOCK(isp);
792f6e75de2SMatt Jacob 	/* Release our hook so that the boot can continue. */
793f6e75de2SMatt Jacob 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
794f6e75de2SMatt Jacob }
795d81ba9d5SMatt Jacob 
796d81ba9d5SMatt Jacob /*
7972df76c16SMatt Jacob  * Local Inlines
7982df76c16SMatt Jacob  */
7992df76c16SMatt Jacob 
8002df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
8012df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
8022df76c16SMatt Jacob 
8032df76c16SMatt Jacob static ISP_INLINE int
8042df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
8052df76c16SMatt Jacob {
8062df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
8072df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
8082df76c16SMatt Jacob 		return (-1);
8092df76c16SMatt Jacob 	}
8102df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
8112df76c16SMatt Jacob 	return (0);
8122df76c16SMatt Jacob }
8132df76c16SMatt Jacob 
8142df76c16SMatt Jacob static ISP_INLINE void
8152df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
8162df76c16SMatt Jacob {
817387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
818405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
819405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
820405b7a29SMatt Jacob 		PISP_PCMD(ccb)->totslen = 0;
821405b7a29SMatt Jacob 		PISP_PCMD(ccb)->cumslen = 0;
822405b7a29SMatt Jacob 		PISP_PCMD(ccb)->crn = 0;
823405b7a29SMatt Jacob #endif
824405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
8252df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
8262df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
8272df76c16SMatt Jacob 	}
828387d8239SMatt Jacob }
829387d8239SMatt Jacob 
8302df76c16SMatt Jacob /*
831d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
832d81ba9d5SMatt Jacob  */
833d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
8342df76c16SMatt Jacob static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
8352df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
8362df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
8372df76c16SMatt Jacob static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
8382df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
8392df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
840523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
8412df76c16SMatt Jacob static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
8422df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
8432df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
8442df76c16SMatt Jacob static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
8452df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
8469cd7268eSMatt Jacob static void destroy_lun_state(ispsoftc_t *, tstate_t *);
8472df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
8482df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
849f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
850387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
851a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
852a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
85394dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
854387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
8552df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
8562df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
8572df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
8582df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
8592df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
86096b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
8612df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
8622df76c16SMatt Jacob static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
8632df76c16SMatt Jacob static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
864d81ba9d5SMatt Jacob 
8652df76c16SMatt Jacob static ISP_INLINE int
8669cd7268eSMatt Jacob is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
867d81ba9d5SMatt Jacob {
868d81ba9d5SMatt Jacob 	tstate_t *tptr;
8692df76c16SMatt Jacob 	struct tslist *lhp;
8702df76c16SMatt Jacob 
8712df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
8722df76c16SMatt Jacob 	SLIST_FOREACH(tptr, lhp, next) {
873315a4d6fSAlexander Motin 		if (tptr->ts_lun == lun) {
874d81ba9d5SMatt Jacob 			return (1);
875d81ba9d5SMatt Jacob 		}
8762df76c16SMatt Jacob 	}
877d81ba9d5SMatt Jacob 	return (0);
878d81ba9d5SMatt Jacob }
879d81ba9d5SMatt Jacob 
8802df76c16SMatt Jacob static void
8812df76c16SMatt Jacob dump_tstates(ispsoftc_t *isp, int bus)
882d81ba9d5SMatt Jacob {
8832df76c16SMatt Jacob 	int i, j;
8842df76c16SMatt Jacob 	struct tslist *lhp;
8852df76c16SMatt Jacob 	tstate_t *tptr = NULL;
8862df76c16SMatt Jacob 
8872df76c16SMatt Jacob 	if (bus >= isp->isp_nchan) {
8882df76c16SMatt Jacob 		return;
889a1bc34c6SMatt Jacob 	}
8902df76c16SMatt Jacob 	for (i = 0; i < LUN_HASH_SIZE; i++) {
8912df76c16SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8922df76c16SMatt Jacob 		j = 0;
8932df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
8942df76c16SMatt Jacob 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
8952df76c16SMatt Jacob 			j++;
896d81ba9d5SMatt Jacob 		}
897d81ba9d5SMatt Jacob 	}
898d81ba9d5SMatt Jacob }
899d81ba9d5SMatt Jacob 
9002df76c16SMatt Jacob static ISP_INLINE tstate_t *
9019cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
902d81ba9d5SMatt Jacob {
90364edff94SMatt Jacob 	tstate_t *tptr = NULL;
9042df76c16SMatt Jacob 	struct tslist *lhp;
905d81ba9d5SMatt Jacob 
9062df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
907315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
9082df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
909315a4d6fSAlexander Motin 			if (tptr->ts_lun == lun) {
910d81ba9d5SMatt Jacob 				tptr->hold++;
911d81ba9d5SMatt Jacob 				return (tptr);
912d81ba9d5SMatt Jacob 			}
91364edff94SMatt Jacob 		}
914126ec864SMatt Jacob 	}
9152df76c16SMatt Jacob 	return (NULL);
9162df76c16SMatt Jacob }
917d81ba9d5SMatt Jacob 
9182df76c16SMatt Jacob static ISP_INLINE tstate_t *
9192df76c16SMatt Jacob get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
9202df76c16SMatt Jacob {
9212df76c16SMatt Jacob 	tstate_t *tptr = NULL;
9222df76c16SMatt Jacob 	atio_private_data_t *atp;
9232df76c16SMatt Jacob 	struct tslist *lhp;
9242df76c16SMatt Jacob 	int i;
9252df76c16SMatt Jacob 
9262df76c16SMatt Jacob 	if (bus < isp->isp_nchan && tagval != 0) {
9272df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9282df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9292df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
930523ea374SAlexander Motin 				atp = isp_find_atpd(isp, tptr, tagval);
931523ea374SAlexander Motin 				if (atp) {
932d81ba9d5SMatt Jacob 					tptr->hold++;
933d81ba9d5SMatt Jacob 					return (tptr);
934d81ba9d5SMatt Jacob 				}
9352df76c16SMatt Jacob 			}
9362df76c16SMatt Jacob 		}
9372df76c16SMatt Jacob 	}
9382df76c16SMatt Jacob 	return (NULL);
939d81ba9d5SMatt Jacob }
940d81ba9d5SMatt Jacob 
9412df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
9422df76c16SMatt Jacob get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
9432df76c16SMatt Jacob {
9442df76c16SMatt Jacob 	inot_private_data_t *ntp;
9452df76c16SMatt Jacob 	tstate_t *tptr;
9462df76c16SMatt Jacob 	struct tslist *lhp;
9472df76c16SMatt Jacob 	int bus, i;
9482df76c16SMatt Jacob 
9492df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9502df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9512df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9522df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
9532df76c16SMatt Jacob 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
9542df76c16SMatt Jacob 				if (ntp) {
9552df76c16SMatt Jacob 					*rslt = tptr;
9562df76c16SMatt Jacob 					tptr->hold++;
9572df76c16SMatt Jacob 					return (ntp);
9582df76c16SMatt Jacob 				}
9592df76c16SMatt Jacob 			}
9602df76c16SMatt Jacob 		}
9612df76c16SMatt Jacob 	}
9622df76c16SMatt Jacob 	return (NULL);
9632df76c16SMatt Jacob }
964e2873b76SMatt Jacob 
9652df76c16SMatt Jacob static ISP_INLINE void
9669cd7268eSMatt Jacob rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
967d81ba9d5SMatt Jacob {
9682df76c16SMatt Jacob 	KASSERT((tptr->hold), ("tptr not held"));
969d81ba9d5SMatt Jacob 	tptr->hold--;
970d81ba9d5SMatt Jacob }
971d81ba9d5SMatt Jacob 
9722df76c16SMatt Jacob static void
9732df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
9742df76c16SMatt Jacob {
9752df76c16SMatt Jacob 	inot_private_data_t *ntp;
97694dff771SMatt Jacob 	inot_private_data_t *restart_queue;
9772df76c16SMatt Jacob 	tstate_t *tptr;
97894dff771SMatt Jacob 	union ccb *ccb;
9792df76c16SMatt Jacob 	struct tslist *lhp;
98009ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
9812df76c16SMatt Jacob 	int bus, i;
9822df76c16SMatt Jacob 
9832df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9842df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9852df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9862df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
98794dff771SMatt Jacob 				if ((restart_queue = tptr->restart_queue) != NULL)
9882df76c16SMatt Jacob 					tptr->restart_queue = NULL;
9892df76c16SMatt Jacob 				while (restart_queue) {
9902df76c16SMatt Jacob 					ntp = restart_queue;
9912df76c16SMatt Jacob 					restart_queue = ntp->rd.nt.nt_hba;
9922df76c16SMatt Jacob 					if (IS_24XX(isp)) {
9932df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
9942df76c16SMatt Jacob 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
9952df76c16SMatt Jacob 					} else {
9962df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
9972df76c16SMatt Jacob 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
9982df76c16SMatt Jacob 					}
9992df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
10002df76c16SMatt Jacob 					if (tptr->restart_queue && restart_queue != NULL) {
10012df76c16SMatt Jacob 						ntp = tptr->restart_queue;
10022df76c16SMatt Jacob 						tptr->restart_queue = restart_queue;
10032df76c16SMatt Jacob 						while (restart_queue->rd.nt.nt_hba) {
10042df76c16SMatt Jacob 							restart_queue = restart_queue->rd.nt.nt_hba;
10052df76c16SMatt Jacob 						}
10062df76c16SMatt Jacob 						restart_queue->rd.nt.nt_hba = ntp;
10072df76c16SMatt Jacob 						break;
10082df76c16SMatt Jacob 					}
10092df76c16SMatt Jacob 				}
101009ddc7adSAlexander Motin 			}
101109ddc7adSAlexander Motin 		}
101209ddc7adSAlexander Motin 
101394dff771SMatt Jacob 		/*
101409ddc7adSAlexander Motin 		 * We only need to do this once per channel.
101594dff771SMatt Jacob 		 */
101609ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
101709ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
101809ddc7adSAlexander Motin 		if (ccb != NULL) {
101909ddc7adSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
102094dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
102194dff771SMatt Jacob 		}
10222df76c16SMatt Jacob 	}
10232df76c16SMatt Jacob }
10242df76c16SMatt Jacob 
10252df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *
10262df76c16SMatt Jacob isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
102753036e92SMatt Jacob {
102853036e92SMatt Jacob 	atio_private_data_t *atp;
10292df76c16SMatt Jacob 
1030523ea374SAlexander Motin 	atp = LIST_FIRST(&tptr->atfree);
10312df76c16SMatt Jacob 	if (atp) {
1032523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
1033523ea374SAlexander Motin 		atp->tag = tag;
1034523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
10352df76c16SMatt Jacob 	}
103653036e92SMatt Jacob 	return (atp);
103753036e92SMatt Jacob }
1038523ea374SAlexander Motin 
1039523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *
1040523ea374SAlexander Motin isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1041523ea374SAlexander Motin {
1042523ea374SAlexander Motin 	atio_private_data_t *atp;
1043523ea374SAlexander Motin 
1044523ea374SAlexander Motin 	LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1045523ea374SAlexander Motin 		if (atp->tag == tag)
10462df76c16SMatt Jacob 			return (atp);
10472df76c16SMatt Jacob 	}
104853036e92SMatt Jacob 	return (NULL);
104953036e92SMatt Jacob }
105053036e92SMatt Jacob 
10512df76c16SMatt Jacob static ISP_INLINE void
10522df76c16SMatt Jacob isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
10532df76c16SMatt Jacob {
1054387d8239SMatt Jacob 	if (atp->ests) {
1055387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
1056387d8239SMatt Jacob 	}
1057523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
1058387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
1059523ea374SAlexander Motin 	LIST_INSERT_HEAD(&tptr->atfree, atp, next);
10602df76c16SMatt Jacob }
10612df76c16SMatt Jacob 
10622df76c16SMatt Jacob static void
10632df76c16SMatt Jacob isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
10642df76c16SMatt Jacob {
10652df76c16SMatt Jacob 	atio_private_data_t *atp;
10662df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
10672df76c16SMatt Jacob 
10682df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1069c5fd36edSAlexander 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",
107094dff771SMatt Jacob 		    atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
10712df76c16SMatt Jacob 	}
10722df76c16SMatt Jacob }
10732df76c16SMatt Jacob 
10742df76c16SMatt Jacob 
10752df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
10762df76c16SMatt Jacob isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
10772df76c16SMatt Jacob {
10782df76c16SMatt Jacob 	inot_private_data_t *ntp;
10792df76c16SMatt Jacob 	ntp = tptr->ntfree;
10802df76c16SMatt Jacob 	if (ntp) {
10812df76c16SMatt Jacob 		tptr->ntfree = ntp->next;
10822df76c16SMatt Jacob 	}
10832df76c16SMatt Jacob 	return (ntp);
10842df76c16SMatt Jacob }
10852df76c16SMatt Jacob 
10862df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
10872df76c16SMatt Jacob isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
10882df76c16SMatt Jacob {
10892df76c16SMatt Jacob 	inot_private_data_t *ntp;
10902df76c16SMatt Jacob 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
10912df76c16SMatt Jacob 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
10922df76c16SMatt Jacob 			return (ntp);
10932df76c16SMatt Jacob 		}
10942df76c16SMatt Jacob 	}
10952df76c16SMatt Jacob 	return (NULL);
10962df76c16SMatt Jacob }
10972df76c16SMatt Jacob 
10982df76c16SMatt Jacob static ISP_INLINE void
10992df76c16SMatt Jacob isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
11002df76c16SMatt Jacob {
11012df76c16SMatt Jacob 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
11022df76c16SMatt Jacob 	ntp->next = tptr->ntfree;
11032df76c16SMatt Jacob 	tptr->ntfree = ntp;
11042df76c16SMatt Jacob }
11052df76c16SMatt Jacob 
1106d81ba9d5SMatt Jacob static cam_status
11072df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1108d81ba9d5SMatt Jacob {
1109d81ba9d5SMatt Jacob 	cam_status status;
1110d81ba9d5SMatt Jacob 	lun_id_t lun;
11112df76c16SMatt Jacob 	struct tslist *lhp;
11122df76c16SMatt Jacob 	tstate_t *tptr;
11132df76c16SMatt Jacob 	int i;
1114d81ba9d5SMatt Jacob 
1115d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
11162df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
11176af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1118d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
1119d81ba9d5SMatt Jacob 		}
11202df76c16SMatt Jacob 	}
1121a1bc34c6SMatt Jacob 	if (is_lun_enabled(isp, bus, lun)) {
1122d81ba9d5SMatt Jacob 		return (CAM_LUN_ALRDY_ENA);
1123d81ba9d5SMatt Jacob 	}
1124e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
11252df76c16SMatt Jacob 	if (tptr == NULL) {
1126d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
1127d81ba9d5SMatt Jacob 	}
1128315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
11292df76c16SMatt Jacob 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1130d81ba9d5SMatt Jacob 	if (status != CAM_REQ_CMP) {
11312df76c16SMatt Jacob 		free(tptr, M_DEVBUF);
1132d81ba9d5SMatt Jacob 		return (status);
1133d81ba9d5SMatt Jacob 	}
11342df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
11352df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
1136523ea374SAlexander Motin 	LIST_INIT(&tptr->atfree);
1137523ea374SAlexander Motin 	for (i = ATPDPSIZE-1; i >= 0; i--)
1138523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1139523ea374SAlexander Motin 	for (i = 0; i < ATPDPHASHSIZE; i++)
1140523ea374SAlexander Motin 		LIST_INIT(&tptr->atused[i]);
1141523ea374SAlexander Motin 	for (i = 0; i < ATPDPSIZE-1; i++)
11422df76c16SMatt Jacob 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
11432df76c16SMatt Jacob 	tptr->ntfree = tptr->ntpool;
11442df76c16SMatt Jacob 	tptr->hold = 1;
1145315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
11462df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
11472df76c16SMatt Jacob 	*rslt = tptr;
11482df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1149d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1150d81ba9d5SMatt Jacob }
1151d81ba9d5SMatt Jacob 
11522df76c16SMatt Jacob static ISP_INLINE void
11539cd7268eSMatt Jacob destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1154d81ba9d5SMatt Jacob {
1155344aebe2SMatt Jacob 	union ccb *ccb;
11562df76c16SMatt Jacob 	struct tslist *lhp;
1157e2873b76SMatt Jacob 
115845210a25SAlexander Motin 	KASSERT((tptr->hold != 0), ("tptr is not held"));
115945210a25SAlexander Motin 	KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1160344aebe2SMatt Jacob 	do {
1161344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1162344aebe2SMatt Jacob 		if (ccb) {
1163344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1164344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1165344aebe2SMatt Jacob 			xpt_done(ccb);
1166344aebe2SMatt Jacob 		}
1167344aebe2SMatt Jacob 	} while (ccb);
1168344aebe2SMatt Jacob 	do {
1169344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1170344aebe2SMatt Jacob 		if (ccb) {
1171344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1172344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1173344aebe2SMatt Jacob 			xpt_done(ccb);
1174344aebe2SMatt Jacob 		}
1175344aebe2SMatt Jacob 	} while (ccb);
1176315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
11772df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1178e2873b76SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
11792df76c16SMatt Jacob 	xpt_free_path(tptr->owner);
1180d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1181d81ba9d5SMatt Jacob }
1182d81ba9d5SMatt Jacob 
11832df76c16SMatt Jacob static void
11842df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1185d81ba9d5SMatt Jacob {
11863e6deb33SAlexander Motin 	tstate_t *tptr;
11873e6deb33SAlexander Motin 	int bus;
11882df76c16SMatt Jacob 	target_id_t target;
1189d81ba9d5SMatt Jacob 	lun_id_t lun;
1190d81ba9d5SMatt Jacob 
11913e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
11923e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
11933e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
11943e6deb33SAlexander Motin 		xpt_done(ccb);
11953e6deb33SAlexander Motin 		return;
11963e6deb33SAlexander Motin 	}
1197e2873b76SMatt Jacob 
1198746e9c85SMatt Jacob 	/*
11993e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
12003e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1201746e9c85SMatt Jacob 	 */
12022df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12032df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12042df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
12056af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
12066af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
12073e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
12082df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12092df76c16SMatt Jacob 		xpt_done(ccb);
12102df76c16SMatt Jacob 		return;
12112df76c16SMatt Jacob 	}
12122df76c16SMatt Jacob 
12133e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1214a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
12152df76c16SMatt Jacob 	if (tptr) {
12162df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
12173e6deb33SAlexander Motin 		xpt_done(ccb);
12183e6deb33SAlexander Motin 		return;
1219d81ba9d5SMatt Jacob 	}
12202df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
12212df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
12222df76c16SMatt Jacob 		xpt_done(ccb);
12233e6deb33SAlexander Motin 		return;
12242df76c16SMatt Jacob 	}
12252df76c16SMatt Jacob 
12263e6deb33SAlexander Motin 	rls_lun_statep(isp, tptr);
12273e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
12283e6deb33SAlexander Motin 	xpt_done(ccb);
12292df76c16SMatt Jacob }
12302df76c16SMatt Jacob 
12312df76c16SMatt Jacob static void
12322df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
12332df76c16SMatt Jacob {
12342df76c16SMatt Jacob 	tstate_t *tptr = NULL;
12352df76c16SMatt Jacob 	int bus;
12362df76c16SMatt Jacob 	target_id_t target;
12372df76c16SMatt Jacob 	lun_id_t lun;
12382df76c16SMatt Jacob 
12392df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12402df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12412df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
12426af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
12436af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
12443e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
12452df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12462df76c16SMatt Jacob 		xpt_done(ccb);
12472df76c16SMatt Jacob 		return;
12482df76c16SMatt Jacob 	}
12492df76c16SMatt Jacob 
12503e6deb33SAlexander Motin 	/* Find the state pointer. */
12512df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
12523e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
12532df76c16SMatt Jacob 		xpt_done(ccb);
12543e6deb33SAlexander Motin 		return;
12552df76c16SMatt Jacob 	}
12562df76c16SMatt Jacob 
12573e6deb33SAlexander Motin 	destroy_lun_state(isp, tptr);
12583e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
12593e6deb33SAlexander Motin 	xpt_done(ccb);
1260d81ba9d5SMatt Jacob }
1261d81ba9d5SMatt Jacob 
12629cd7268eSMatt Jacob static void
1263387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1264d81ba9d5SMatt Jacob {
126594dff771SMatt Jacob 	int fctape, sendstatus, resid;
12662df76c16SMatt Jacob 	tstate_t *tptr;
1267387d8239SMatt Jacob 	fcparam *fcp;
12682df76c16SMatt Jacob 	atio_private_data_t *atp;
126994dff771SMatt Jacob 	struct ccb_scsiio *cso;
127009ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
127194dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
12721dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1273d81ba9d5SMatt Jacob 
127494dff771SMatt Jacob 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
127594dff771SMatt Jacob 	if (tptr == NULL) {
127694dff771SMatt Jacob 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
127794dff771SMatt Jacob 		if (tptr == NULL) {
127894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
127994dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
128094dff771SMatt Jacob 			xpt_done(ccb);
128194dff771SMatt Jacob 			return;
128294dff771SMatt Jacob 		}
128394dff771SMatt Jacob 	}
128494dff771SMatt 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,
128594dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
128694dff771SMatt Jacob 
128709ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
128894dff771SMatt Jacob 	switch (how) {
128994dff771SMatt Jacob 	case FROM_CAM:
12902df76c16SMatt Jacob 		/*
129194dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
12922df76c16SMatt Jacob 		 */
129309ddc7adSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
129494dff771SMatt Jacob 		break;
129509ddc7adSAlexander Motin 	case FROM_TIMER:
129694dff771SMatt Jacob 	case FROM_SRR:
129794dff771SMatt Jacob 	case FROM_CTIO_DONE:
129809ddc7adSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
129994dff771SMatt Jacob 		break;
130094dff771SMatt Jacob 	}
130194dff771SMatt Jacob 
130209ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
130309ddc7adSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
130494dff771SMatt Jacob 
130594dff771SMatt Jacob 		cso = &ccb->csio;
1306387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1307387d8239SMatt Jacob 		if (xfrlen == 0) {
13082df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1309387d8239SMatt 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");
13102df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
13112df76c16SMatt Jacob 				xpt_done(ccb);
131294dff771SMatt Jacob 				continue;
13132df76c16SMatt Jacob 			}
13142df76c16SMatt Jacob 		}
13152df76c16SMatt Jacob 
1316523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, cso->tag_id);
13172df76c16SMatt Jacob 		if (atp == NULL) {
131894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
13192df76c16SMatt Jacob 			isp_dump_atpd(isp, tptr);
13202df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
13212df76c16SMatt Jacob 			xpt_done(ccb);
132294dff771SMatt Jacob 			continue;
13232df76c16SMatt Jacob 		}
1324387d8239SMatt Jacob 
1325387d8239SMatt Jacob 		/*
1326387d8239SMatt Jacob 		 * Is this command a dead duck?
1327387d8239SMatt Jacob 		 */
13282df76c16SMatt Jacob 		if (atp->dead) {
132994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
13302df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
13312df76c16SMatt Jacob 			xpt_done(ccb);
133294dff771SMatt Jacob 			continue;
13332df76c16SMatt Jacob 		}
13342df76c16SMatt Jacob 
13352df76c16SMatt Jacob 		/*
13362df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
13372df76c16SMatt Jacob 		 */
1338387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1339387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
134094dff771SMatt 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);
13412df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
13422df76c16SMatt Jacob 			xpt_done(ccb);
134394dff771SMatt Jacob 			continue;
13442df76c16SMatt Jacob 		}
13452df76c16SMatt Jacob 
13462df76c16SMatt Jacob 		/*
134794dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
134894dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1349387d8239SMatt Jacob 		 */
135094dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
135194dff771SMatt 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);
135209ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
135394dff771SMatt Jacob 			break;
1354387d8239SMatt Jacob 		}
1355387d8239SMatt Jacob 
1356d81ba9d5SMatt Jacob 		/*
1357387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1358d81ba9d5SMatt Jacob 		 */
1359387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1360387d8239SMatt Jacob 			fctape = 1;
1361387d8239SMatt Jacob 		} else {
1362387d8239SMatt Jacob 			fctape = 0;
1363387d8239SMatt Jacob 		}
1364387d8239SMatt Jacob 
1365387d8239SMatt Jacob 		/*
1366387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1367387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1368387d8239SMatt Jacob 		 */
1369387d8239SMatt Jacob 		if (atp->sendst) {
1370daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
137194dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1372387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1373387d8239SMatt Jacob 			atp->sendst = 0;
1374387d8239SMatt Jacob 		}
1375387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1376387d8239SMatt Jacob 			sendstatus = 1;
1377387d8239SMatt Jacob 		} else {
1378387d8239SMatt Jacob 			sendstatus = 0;
1379387d8239SMatt Jacob 		}
1380387d8239SMatt Jacob 
1381387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
138294dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1383387d8239SMatt Jacob 			/*
1384387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1385387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1386387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1387387d8239SMatt Jacob 			 */
1388387d8239SMatt Jacob 			sense_length = cso->sense_len;
1389387d8239SMatt Jacob 
1390387d8239SMatt Jacob 			/*
1391387d8239SMatt Jacob 			 * This 'cannot' happen
1392387d8239SMatt Jacob 			 */
1393387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1394387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1395387d8239SMatt Jacob 			}
1396387d8239SMatt Jacob 		} else {
1397387d8239SMatt Jacob 			sense_length = 0;
1398387d8239SMatt Jacob 		}
1399387d8239SMatt Jacob 
140094dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
140194dff771SMatt Jacob 
140294dff771SMatt Jacob 		/*
140394dff771SMatt Jacob 		 * Check for overflow
140494dff771SMatt Jacob 		 */
140594dff771SMatt Jacob 		tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
140694dff771SMatt Jacob 		if (tmp > atp->orig_datalen) {
140794dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
140894dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
140994dff771SMatt Jacob 			xpt_done(ccb);
141094dff771SMatt Jacob 			continue;
141194dff771SMatt Jacob 		}
1412387d8239SMatt Jacob 
14132df76c16SMatt Jacob 		if (IS_24XX(isp)) {
14142df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1415d81ba9d5SMatt Jacob 
14162df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
14172df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
141894dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
141994dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
14202df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
14212df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
14222df76c16SMatt Jacob 			cto->ct_iid_lo = atp->portid;
14232df76c16SMatt Jacob 			cto->ct_iid_hi = atp->portid >> 16;
14242df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
14252df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1426a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
14272df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1428387d8239SMatt Jacob 
1429387d8239SMatt Jacob 			/*
1430387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
14316f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1432387d8239SMatt Jacob 			 *
14338b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
14348b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1435387d8239SMatt Jacob 			 *
1436387d8239SMatt Jacob 			 */
1437387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1438387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
143994dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1440387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1441387d8239SMatt Jacob 					if (resid < 0) {
1442387d8239SMatt Jacob 						cto->ct_resid = -resid;
1443387d8239SMatt Jacob 					} else if (resid > 0) {
1444387d8239SMatt Jacob 						cto->ct_resid = resid;
14452df76c16SMatt Jacob 					}
1446387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1447387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1448387d8239SMatt Jacob 					if (resid < 0) {
1449387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1450387d8239SMatt Jacob 					} else if (resid > 0) {
1451387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1452387d8239SMatt Jacob 					}
1453387d8239SMatt Jacob 					if (fctape) {
1454387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1455387d8239SMatt Jacob 					}
1456387d8239SMatt Jacob 					if (sense_length) {
14572df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1458387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1459387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
14602df76c16SMatt Jacob 					}
14612df76c16SMatt Jacob 				} else {
1462387d8239SMatt Jacob 					bus_addr_t addr;
1463387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1464387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1465387d8239SMatt Jacob 
1466387d8239SMatt Jacob 					if (atp->ests == NULL) {
1467387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1468387d8239SMatt Jacob 						if (atp->ests == NULL) {
146909ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
147094dff771SMatt Jacob 							break;
1471387d8239SMatt Jacob 						}
1472387d8239SMatt Jacob 					}
1473387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1474387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1475387d8239SMatt Jacob 					if (fctape) {
1476387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1477387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1478387d8239SMatt Jacob 					}
1479387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1480387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1481387d8239SMatt Jacob 					if (resid < 0) {
1482387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1483387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1484387d8239SMatt Jacob 					} else if (resid > 0) {
1485387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1486387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1487387d8239SMatt Jacob 					}
1488387d8239SMatt Jacob 					if (sense_length) {
1489387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1490387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1491387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1492387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1493387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1494387d8239SMatt Jacob 					} else {
1495387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1496387d8239SMatt Jacob 					}
1497387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1498387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1499387d8239SMatt Jacob 					}
1500387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1501387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
150294dff771SMatt 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,
1503387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1504387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1505387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1506387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1507387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1508387d8239SMatt Jacob 				}
1509387d8239SMatt Jacob 				if (sense_length) {
151094dff771SMatt 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__,
151194dff771SMatt 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,
151294dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1513387d8239SMatt Jacob 				} else {
151494dff771SMatt 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__,
151594dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1516387d8239SMatt Jacob 				}
1517387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1518387d8239SMatt Jacob 			}
1519387d8239SMatt Jacob 
1520387d8239SMatt Jacob 			/*
1521387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1522387d8239SMatt Jacob 			 */
1523387d8239SMatt Jacob 			if (xfrlen != 0) {
15242df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
15252df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
15262df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
15272df76c16SMatt Jacob 				} else {
15282df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
15292df76c16SMatt Jacob 				}
1530387d8239SMatt Jacob 
153194dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1532387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1533387d8239SMatt Jacob 
1534387d8239SMatt Jacob #ifdef	DEBUG
1535387d8239SMatt 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) {
1536387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1537387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1538387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1539387d8239SMatt Jacob 				}
1540387d8239SMatt Jacob #endif
1541387d8239SMatt Jacob 				if (sendstatus) {
154294dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
154394dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1544387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1545387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
154694dff771SMatt Jacob 						if (fctape) {
154794dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
154894dff771SMatt Jacob 						}
1549387d8239SMatt Jacob 					} else {
1550387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
155194dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
15522df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
15532df76c16SMatt Jacob 					}
1554387d8239SMatt Jacob 				} else {
1555387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1556387d8239SMatt Jacob 				}
155794dff771SMatt 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__,
155894dff771SMatt 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);
1559387d8239SMatt Jacob 			}
15603e6deb33SAlexander Motin 		} else {
15614fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
156200a8e174SMatt Jacob 
1563387d8239SMatt Jacob 			if (isp->isp_osinfo.sixtyfourbit)
1564387d8239SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1565387d8239SMatt Jacob 			else
1566d81ba9d5SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1567d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
156894dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
156994dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1570d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1571d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
157229f76675SMatt Jacob 			} else {
1573d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
15742df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1575d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
15762ad50ca5SMatt Jacob 				}
157729f76675SMatt Jacob 			}
1578a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
157900a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1580387d8239SMatt Jacob 
1581387d8239SMatt Jacob 			/*
1582387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1583387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1584387d8239SMatt Jacob 			 *
158596240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1586387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1587387d8239SMatt Jacob 			 *
1588387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1589387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1590387d8239SMatt Jacob 			 * needed based upon this.
1591387d8239SMatt Jacob 			 */
1592387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1593387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
159494dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1595387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1596387d8239SMatt Jacob 					if (resid < 0) {
1597387d8239SMatt Jacob 						cto->ct_resid = -resid;
1598387d8239SMatt Jacob 					} else if (resid > 0) {
1599387d8239SMatt Jacob 						cto->ct_resid = resid;
1600387d8239SMatt Jacob 					}
1601387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1602f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1603387d8239SMatt Jacob 					if (resid < 0) {
16042df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1605387d8239SMatt Jacob 					} else if (resid > 0) {
16062df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1607f48ce188SMatt Jacob 					}
1608387d8239SMatt Jacob 					if (fctape) {
1609387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1610387d8239SMatt Jacob 					}
1611387d8239SMatt Jacob 					if (sense_length) {
161200a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1613387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1614387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
161500a8e174SMatt Jacob 					}
161600a8e174SMatt Jacob 				} else {
1617387d8239SMatt Jacob 					bus_addr_t addr;
1618387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1619387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1620387d8239SMatt Jacob 
1621387d8239SMatt Jacob 					if (atp->ests == NULL) {
1622387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1623387d8239SMatt Jacob 						if (atp->ests == NULL) {
162409ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
162594dff771SMatt Jacob 							break;
1626387d8239SMatt Jacob 						}
1627387d8239SMatt Jacob 					}
1628387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1629387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1630387d8239SMatt Jacob 					if (fctape) {
1631387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1632387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1633387d8239SMatt Jacob 					}
1634387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1635387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1636387d8239SMatt Jacob 					if (resid < 0) {
1637387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1638387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1639387d8239SMatt Jacob 					} else if (resid > 0) {
1640387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1641387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1642387d8239SMatt Jacob 					}
1643387d8239SMatt Jacob 					if (sense_length) {
1644387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1645387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1646387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1647387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1648387d8239SMatt Jacob 					} else {
1649387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1650387d8239SMatt Jacob 					}
1651387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1652387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1653387d8239SMatt Jacob 					}
1654387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1655387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
165694dff771SMatt 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,
1657387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1658387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1659387d8239SMatt Jacob 					if (isp->isp_osinfo.sixtyfourbit) {
1660387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1661387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1662387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1663387d8239SMatt Jacob 					} else {
1664387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1665387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1666387d8239SMatt Jacob 					}
1667387d8239SMatt Jacob 				}
1668387d8239SMatt Jacob 				if (sense_length) {
166994dff771SMatt 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__,
167094dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
167194dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1672387d8239SMatt Jacob 				} else {
167394dff771SMatt 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,
167494dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1675387d8239SMatt Jacob 				}
1676387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1677387d8239SMatt Jacob 			}
1678387d8239SMatt Jacob 
1679387d8239SMatt Jacob 			if (xfrlen != 0) {
168000a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
168100a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
168200a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
168300a8e174SMatt Jacob 				} else {
168400a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1685d81ba9d5SMatt Jacob 				}
168653036e92SMatt Jacob 
168794dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1688387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1689387d8239SMatt Jacob 
1690387d8239SMatt Jacob 				if (sendstatus) {
169194dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
169294dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1693387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1694387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
169594dff771SMatt Jacob 						if (fctape) {
169694dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
169794dff771SMatt Jacob 						}
1698387d8239SMatt Jacob 					} else {
1699387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
170094dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1701387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1702387d8239SMatt Jacob 					}
1703387d8239SMatt Jacob 				} else {
1704387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1705387d8239SMatt Jacob 				}
1706387d8239SMatt Jacob 			}
170794dff771SMatt 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,
170894dff771SMatt 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);
1709d81ba9d5SMatt Jacob 		}
1710d81ba9d5SMatt Jacob 
171194dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
171294dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
171309ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
171494dff771SMatt Jacob 			break;
171594dff771SMatt Jacob 		}
1716970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1717970ceb2fSAlexander Motin 		if (handle == 0) {
1718387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
171909ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
172094dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
172194dff771SMatt Jacob 			break;
1722d81ba9d5SMatt Jacob 		}
172394dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
172494dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
172594dff771SMatt Jacob 
1726d81ba9d5SMatt Jacob 
1727d81ba9d5SMatt Jacob 		/*
1728d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1729d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1730b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1731d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1732d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1733d81ba9d5SMatt Jacob 		 * format.
1734d81ba9d5SMatt Jacob 		 */
1735d81ba9d5SMatt Jacob 
17362df76c16SMatt Jacob 		if (IS_24XX(isp)) {
17372df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
17382df76c16SMatt Jacob 			cto->ct_syshandle = handle;
173910365e5aSMatt Jacob 		} else {
17403e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
174110365e5aSMatt Jacob 			cto->ct_syshandle = handle;
174210365e5aSMatt Jacob 		}
1743a1bc34c6SMatt Jacob 
17442df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
174594dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1746970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
174794dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
174894dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
174909ddc7adSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
175094dff771SMatt Jacob 				break;
175194dff771SMatt Jacob 			}
175294dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
175394dff771SMatt Jacob 			xpt_done(ccb);
175494dff771SMatt Jacob 			continue;
175594dff771SMatt Jacob 		}
17562df76c16SMatt Jacob 		isp->isp_nactive++;
175794dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1758387d8239SMatt Jacob 		if (xfrlen) {
1759387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1760387d8239SMatt Jacob 		} else {
1761387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1762387d8239SMatt Jacob 		}
1763387d8239SMatt Jacob 		atp->ctcnt++;
176494dff771SMatt Jacob 		atp->seqno++;
17652df76c16SMatt Jacob 	}
17662df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
1767d81ba9d5SMatt Jacob }
1768d81ba9d5SMatt Jacob 
1769a1bc34c6SMatt Jacob static void
1770a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1771f48ce188SMatt Jacob {
17722df76c16SMatt Jacob 	union ccb *ccb = arg;
17732a0db815SJohn Baldwin 
1774b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
17752df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1776a1bc34c6SMatt Jacob }
1777a1bc34c6SMatt Jacob 
1778a1bc34c6SMatt Jacob static void
1779387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1780387d8239SMatt Jacob {
1781387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1782387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
17832a0db815SJohn Baldwin 
17842a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1785387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
17862a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1787387d8239SMatt Jacob 	} else {
1788387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1789387d8239SMatt Jacob 	}
1790387d8239SMatt Jacob }
1791387d8239SMatt Jacob 
1792387d8239SMatt Jacob 
1793387d8239SMatt Jacob static void
1794a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1795a1bc34c6SMatt Jacob {
17969cd7268eSMatt Jacob 	ispsoftc_t *isp;
1797a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
1798a1bc34c6SMatt Jacob 	void *qe;
17993e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1800a1bc34c6SMatt Jacob 
1801a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
1802f48ce188SMatt Jacob 
18032df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
18042df76c16SMatt Jacob 	if (qe == NULL) {
1805c3167cabSMatt Jacob 		xpt_print(ccb->ccb_h.path,
1806c3167cabSMatt Jacob 		    "%s: Request Queue Overflow\n", __func__);
18072a0db815SJohn Baldwin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
18082a0db815SJohn Baldwin 		    isp_refire_putback_atio, ccb);
1809a1bc34c6SMatt Jacob 		return;
1810f48ce188SMatt Jacob 	}
181129f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
1812a1bc34c6SMatt Jacob 	cso = &ccb->csio;
18132df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1814f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1815f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
18162df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1817a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
18186af11b82SAlexander Motin #if __FreeBSD_version < 1000700
18196af11b82SAlexander Motin 		if (at->at_scclun >= 256)
18206af11b82SAlexander Motin 			at->at_scclun |= 0x4000;
18216af11b82SAlexander Motin #endif
1822f48ce188SMatt Jacob 	} else {
1823a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1824f48ce188SMatt Jacob 	}
1825f48ce188SMatt Jacob 	at->at_status = CT_OK;
1826a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
1827570c7a3fSMatt Jacob 	at->at_iid = cso->ccb_h.target_id;
18284fd13c1bSMatt Jacob 	isp_put_atio2(isp, at, qe);
18292df76c16SMatt Jacob 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
18302df76c16SMatt Jacob 	ISP_SYNC_REQUEST(isp);
1831a1bc34c6SMatt Jacob 	isp_complete_ctio(ccb);
1832f48ce188SMatt Jacob }
1833f48ce188SMatt Jacob 
1834f48ce188SMatt Jacob static void
1835a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1836f48ce188SMatt Jacob {
1837387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1838a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1839a1bc34c6SMatt Jacob 		xpt_done(ccb);
1840f48ce188SMatt Jacob 	}
1841387d8239SMatt Jacob }
1842f48ce188SMatt Jacob 
18432df76c16SMatt Jacob static void
18449cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1845d81ba9d5SMatt Jacob {
184645b9ad7aSAlexander Motin 	fcparam *fcp;
184792a1e549SMatt Jacob 	lun_id_t lun;
18482df76c16SMatt Jacob 	fcportdb_t *lp;
1849d81ba9d5SMatt Jacob 	tstate_t *tptr;
1850d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
18512df76c16SMatt Jacob 	uint16_t nphdl;
1852a035b0afSMatt Jacob 	atio_private_data_t *atp;
18532df76c16SMatt Jacob 	inot_private_data_t *ntp;
1854d81ba9d5SMatt Jacob 
1855d81ba9d5SMatt Jacob 	/*
1856d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1857d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1858d81ba9d5SMatt Jacob 	 *
1859b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1860d81ba9d5SMatt Jacob 	 */
1861d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
18622df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1863d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
18642df76c16SMatt Jacob 		return;
1865d81ba9d5SMatt Jacob 	}
1866d81ba9d5SMatt Jacob 
186745b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
18682df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
186992a1e549SMatt Jacob 		lun = aep->at_scclun;
18706af11b82SAlexander Motin #if __FreeBSD_version < 1000700
18716af11b82SAlexander Motin 		lun &= 0x3fff;
18726af11b82SAlexander Motin #endif
18732ad50ca5SMatt Jacob 	} else {
187492a1e549SMatt Jacob 		lun = aep->at_lun;
18752ad50ca5SMatt Jacob 	}
18762df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
18772df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
18782df76c16SMatt Jacob 	} else {
18792df76c16SMatt Jacob 		nphdl = aep->at_iid;
18802df76c16SMatt Jacob 	}
1881a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1882d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1883a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1884746e9c85SMatt Jacob 		if (tptr == NULL) {
1885123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1886387d8239SMatt Jacob 			if (lun == 0) {
1887387d8239SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1888387d8239SMatt Jacob 			} else {
18892df76c16SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1890387d8239SMatt Jacob 			}
18912df76c16SMatt Jacob 			return;
1892746e9c85SMatt Jacob 		}
1893d81ba9d5SMatt Jacob 	}
1894d81ba9d5SMatt Jacob 
1895d81ba9d5SMatt Jacob 	/*
18962df76c16SMatt Jacob 	 * Start any commands pending resources first.
1897d81ba9d5SMatt Jacob 	 */
18982df76c16SMatt Jacob 	if (tptr->restart_queue) {
18992df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
19002df76c16SMatt Jacob 		tptr->restart_queue = NULL;
19012df76c16SMatt Jacob 		while (restart_queue) {
19022df76c16SMatt Jacob 			ntp = restart_queue;
19032df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
19042df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
19052df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
19062df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
19072df76c16SMatt Jacob 			/*
19082df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
19092df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
19102df76c16SMatt Jacob 			 * it and go to noresrc.
19112df76c16SMatt Jacob 			 */
19122df76c16SMatt Jacob 			if (tptr->restart_queue) {
19132df76c16SMatt Jacob 				ntp = tptr->restart_queue;
19142df76c16SMatt Jacob 				tptr->restart_queue = restart_queue;
19152df76c16SMatt Jacob 				while (restart_queue->rd.nt.nt_hba) {
19162df76c16SMatt Jacob 					restart_queue = restart_queue->rd.nt.nt_hba;
1917d81ba9d5SMatt Jacob 				}
19182df76c16SMatt Jacob 				restart_queue->rd.nt.nt_hba = ntp;
19192df76c16SMatt Jacob 				goto noresrc;
19202df76c16SMatt Jacob 			}
19212df76c16SMatt Jacob 		}
19222df76c16SMatt Jacob 	}
19232df76c16SMatt Jacob 
19242df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
19252df76c16SMatt Jacob 	if (atiop == NULL) {
19262df76c16SMatt Jacob 		goto noresrc;
19272df76c16SMatt Jacob 	}
19282df76c16SMatt Jacob 
1929523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
19302df76c16SMatt Jacob 	if (atp == NULL) {
19312df76c16SMatt Jacob 		goto noresrc;
19322df76c16SMatt Jacob 	}
19332df76c16SMatt Jacob 
1934570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1935d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1936570c7a3fSMatt Jacob 	tptr->atio_count--;
1937387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
193845b9ad7aSAlexander Motin 	atiop->ccb_h.target_id = fcp->isp_loopid;
193992a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
19402df76c16SMatt Jacob 
1941b0a3ba7eSMatt Jacob 	/*
1942b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1943b0a3ba7eSMatt Jacob 	 */
1944f48ce188SMatt Jacob 	atiop->sense_len = 0;
19452df76c16SMatt Jacob 
19462df76c16SMatt Jacob 	/*
19472df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
19482df76c16SMatt Jacob 	 */
1949d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1950d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1951d4f3ad3aSAlexander Motin 	else {
1952d4f3ad3aSAlexander Motin 		if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1953e68eef14SAlexander Motin 		     lp->state == FC_PORTDB_STATE_ZOMBIE)) {
195437a7daacSAlexander Motin 			uint64_t wwpn =
19552df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[0]) << 48) |
19562df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[1]) << 32) |
19572df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[2]) << 16) |
19582df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[3]) <<  0);
195937a7daacSAlexander Motin 			isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
196037a7daacSAlexander Motin 			    nphdl, PORT_ANY, 0);
196145b9ad7aSAlexander Motin 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
196245b9ad7aSAlexander Motin 				fcp->isp_loopstate = LOOP_LTEST_DONE;
196345b9ad7aSAlexander Motin 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
196445b9ad7aSAlexander Motin 			    ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1965d4f3ad3aSAlexander Motin 			isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1966d4f3ad3aSAlexander Motin 		}
1967d4f3ad3aSAlexander Motin 		atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
19682df76c16SMatt Jacob 	}
1969d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
19702df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1971d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
19722df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1973d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1974d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
19755e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1976d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1977d81ba9d5SMatt Jacob 		break;
1978d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
19795e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1980d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1981d81ba9d5SMatt Jacob 		break;
1982d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
19835e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1984d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1985d81ba9d5SMatt Jacob 		break;
1986d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1987d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1988d81ba9d5SMatt Jacob 	default:
1989d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1990d81ba9d5SMatt Jacob 		break;
1991d81ba9d5SMatt Jacob 	}
1992f48ce188SMatt Jacob 
199353036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
199453036e92SMatt Jacob 	atp->bytes_xfered = 0;
19952df76c16SMatt Jacob 	atp->lun = lun;
1996d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
19972df76c16SMatt Jacob 	atp->sid = PORT_ANY;
19982df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
19992df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
20002df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2001570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
2002d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
2003123055f0SNathan 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);
2004d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
20052df76c16SMatt Jacob 	return;
20062df76c16SMatt Jacob noresrc:
20072df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
20082df76c16SMatt Jacob 	if (ntp == NULL) {
20092df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
2010873b0260SAlexander Motin 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
20112df76c16SMatt Jacob 		return;
20122df76c16SMatt Jacob 	}
20132df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
20142df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
20152df76c16SMatt Jacob 	tptr->restart_queue = ntp;
20162df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2017d81ba9d5SMatt Jacob }
2018d81ba9d5SMatt Jacob 
20192df76c16SMatt Jacob static void
20202df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
20212df76c16SMatt Jacob {
20222df76c16SMatt Jacob 	int cdbxlen;
20236af11b82SAlexander Motin 	lun_id_t lun;
20246af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
20252df76c16SMatt Jacob 	uint32_t did, sid;
20262df76c16SMatt Jacob 	fcportdb_t *lp;
20272df76c16SMatt Jacob 	tstate_t *tptr;
20282df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
20292df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
20309e7d423dSMatt Jacob 	atio_private_data_t *oatp;
20312df76c16SMatt Jacob 	inot_private_data_t *ntp;
20322df76c16SMatt Jacob 
20332df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
20342df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
20356af11b82SAlexander Motin #if __FreeBSD_version >= 1000700
20366af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
20376af11b82SAlexander Motin #else
20386af11b82SAlexander Motin 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] & 0x3f << 8) |
20396af11b82SAlexander Motin 	    aep->at_cmnd.fcp_cmnd_lun[1];
20406af11b82SAlexander Motin #endif
20412df76c16SMatt Jacob 
20422df76c16SMatt Jacob 	/*
20432df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
20442df76c16SMatt Jacob 	 *
20452df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
20462df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
20472df76c16SMatt Jacob 	 */
20487dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
20492df76c16SMatt Jacob 		/*
20502df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
20512df76c16SMatt Jacob 		 */
20522df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
20532df76c16SMatt Jacob 
20542df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
20552df76c16SMatt Jacob 			NANOTIME_T now;
20562df76c16SMatt Jacob 
20572df76c16SMatt Jacob 			/*
20582df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
20592df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
20602df76c16SMatt Jacob 			 */
20612df76c16SMatt Jacob 			GET_NANOTIME(&now);
2062514a71ebSAlexander Motin 			if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
20632df76c16SMatt 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);
20642df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
20652df76c16SMatt Jacob 				return;
20662df76c16SMatt Jacob 			}
20672df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
20682df76c16SMatt Jacob 			if (tptr == NULL) {
20692df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
20702df76c16SMatt Jacob 				if (tptr == NULL) {
20712df76c16SMatt 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);
20722df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
20732df76c16SMatt Jacob 					return;
20742df76c16SMatt Jacob 				}
20752df76c16SMatt Jacob 			}
20762df76c16SMatt 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);
20772df76c16SMatt Jacob 			goto noresrc;
20782df76c16SMatt Jacob 		}
20792df76c16SMatt 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);
20802df76c16SMatt Jacob 	} else {
20812df76c16SMatt Jacob 		chan = 0;
20822df76c16SMatt Jacob 	}
20832df76c16SMatt Jacob 
20842df76c16SMatt Jacob 	/*
20852df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
20862df76c16SMatt Jacob 	 */
2087eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
20882df76c16SMatt Jacob 		/*
20892df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
20902df76c16SMatt Jacob 		 */
20912df76c16SMatt 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",
20922df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
2093e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
20942df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
20952df76c16SMatt Jacob 		return;
20962df76c16SMatt Jacob 	}
20972df76c16SMatt Jacob 	nphdl = lp->handle;
20982df76c16SMatt Jacob 
20992df76c16SMatt Jacob 	/*
21002df76c16SMatt Jacob 	 * Get the tstate pointer
21012df76c16SMatt Jacob 	 */
21022df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
21032df76c16SMatt Jacob 	if (tptr == NULL) {
21042df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
21052df76c16SMatt Jacob 		if (tptr == NULL) {
21066af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
21076af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
21086af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
2109387d8239SMatt Jacob 			if (lun == 0) {
2110352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2111387d8239SMatt Jacob 			} else {
21122df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2113387d8239SMatt Jacob 			}
21142df76c16SMatt Jacob 			return;
21152df76c16SMatt Jacob 		}
21162df76c16SMatt Jacob 	}
21172df76c16SMatt Jacob 
21182df76c16SMatt Jacob 	/*
21192df76c16SMatt Jacob 	 * Start any commands pending resources first.
21202df76c16SMatt Jacob 	 */
21212df76c16SMatt Jacob 	if (tptr->restart_queue) {
21222df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
21232df76c16SMatt Jacob 		tptr->restart_queue = NULL;
21242df76c16SMatt Jacob 		while (restart_queue) {
21252df76c16SMatt Jacob 			ntp = restart_queue;
21262df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
21272df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
21282df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
21292df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
21302df76c16SMatt Jacob 			/*
21312df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
21322df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
21332df76c16SMatt Jacob 			 * it and go to noresrc.
21342df76c16SMatt Jacob 			 */
21352df76c16SMatt Jacob 			if (tptr->restart_queue) {
21369e7d423dSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
21372df76c16SMatt Jacob 				if (restart_queue) {
21382df76c16SMatt Jacob 					ntp = tptr->restart_queue;
21392df76c16SMatt Jacob 					tptr->restart_queue = restart_queue;
21402df76c16SMatt Jacob 					while (restart_queue->rd.nt.nt_hba) {
21412df76c16SMatt Jacob 						restart_queue = restart_queue->rd.nt.nt_hba;
21422df76c16SMatt Jacob 					}
21432df76c16SMatt Jacob 					restart_queue->rd.nt.nt_hba = ntp;
21442df76c16SMatt Jacob 				}
21452df76c16SMatt Jacob 				goto noresrc;
21462df76c16SMatt Jacob 			}
21472df76c16SMatt Jacob 		}
21482df76c16SMatt Jacob 	}
21492df76c16SMatt Jacob 
21502df76c16SMatt Jacob 	/*
21512df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
21522df76c16SMatt Jacob 	 */
21532df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
21542df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
21552df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
21562df76c16SMatt Jacob 		return;
21572df76c16SMatt Jacob 	}
21582df76c16SMatt Jacob 
21592df76c16SMatt Jacob 	/*
21602df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
21612df76c16SMatt Jacob 	 */
21622df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
21632df76c16SMatt Jacob 	if (atiop == NULL) {
21642df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
21652df76c16SMatt Jacob 		goto noresrc;
21662df76c16SMatt Jacob 	}
21672df76c16SMatt Jacob 
2168523ea374SAlexander Motin 	oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
21699e7d423dSMatt Jacob 	if (oatp) {
2170387d8239SMatt 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",
21719e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
21722df76c16SMatt Jacob 		/*
21732df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
21742df76c16SMatt Jacob 		 */
21752df76c16SMatt Jacob 		goto noresrc;
21762df76c16SMatt Jacob 	}
2177523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2178523ea374SAlexander Motin 	if (atp == NULL) {
2179523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2180523ea374SAlexander Motin 		goto noresrc;
2181523ea374SAlexander Motin 	}
2182387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
21832df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
21842df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
21852df76c16SMatt Jacob 	tptr->atio_count--;
2186387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2187d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
21882df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
21892df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
21902df76c16SMatt Jacob 	atiop->sense_len = 0;
21912df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
21922df76c16SMatt Jacob 	if (cdbxlen) {
21932df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
21942df76c16SMatt Jacob 	}
21952df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
21962df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
21972df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
21982df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
21992df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
22002df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
22012df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
22025e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22032df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
22042df76c16SMatt Jacob 		break;
22052df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
22065e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22072df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
22082df76c16SMatt Jacob 		break;
22092df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
22105e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
22112df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
22122df76c16SMatt Jacob 		break;
22132df76c16SMatt Jacob 	default:
22142df76c16SMatt Jacob 		/* FALLTHROUGH */
22152df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
22162df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
22172df76c16SMatt Jacob 		atiop->tag_action = 0;
22182df76c16SMatt Jacob 		break;
22192df76c16SMatt Jacob 	}
22202df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
22212df76c16SMatt Jacob 	atp->bytes_xfered = 0;
22222df76c16SMatt Jacob 	atp->lun = lun;
22232df76c16SMatt Jacob 	atp->nphdl = nphdl;
22242df76c16SMatt Jacob 	atp->portid = sid;
22252df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
22269e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
22272df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
22282df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
22292df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
22306af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
22316af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
22322df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
22332df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22342df76c16SMatt Jacob 	return;
22352df76c16SMatt Jacob noresrc:
22362df76c16SMatt Jacob 	if (atp) {
22372df76c16SMatt Jacob 		isp_put_atpd(isp, tptr, atp);
22382df76c16SMatt Jacob 	}
22392df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
22402df76c16SMatt Jacob 	if (ntp == NULL) {
22412df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
22422df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
22432df76c16SMatt Jacob 		return;
22442df76c16SMatt Jacob 	}
22452df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
22462df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
22472df76c16SMatt Jacob 	tptr->restart_queue = ntp;
22482df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22492df76c16SMatt Jacob }
22502df76c16SMatt Jacob 
2251387d8239SMatt Jacob 
2252387d8239SMatt Jacob /*
2253387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
2254387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
2255387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
2256387d8239SMatt Jacob  * transaction.
2257387d8239SMatt Jacob  */
2258387d8239SMatt Jacob static void
2259387d8239SMatt Jacob isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2260387d8239SMatt Jacob {
2261387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
2262387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2263387d8239SMatt Jacob 	union ccb *ccb;
2264387d8239SMatt Jacob 
2265387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
2266387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2267387d8239SMatt Jacob 	ccb = atp->srr_ccb;
2268387d8239SMatt Jacob 	atp->srr_ccb = NULL;
2269387d8239SMatt Jacob 	atp->nsrr++;
2270387d8239SMatt Jacob 	if (ccb == NULL) {
227194dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2272387d8239SMatt Jacob 		goto fail;
2273387d8239SMatt Jacob 	}
2274387d8239SMatt Jacob 
2275387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
2276387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
2277387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2278387d8239SMatt Jacob 
2279387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
2280387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
2281387d8239SMatt Jacob 		/*
2282387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
2283387d8239SMatt Jacob 		 */
2284387d8239SMatt Jacob 		atp->sendst = 0;
2285387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
2286387d8239SMatt Jacob 		if (ccb_len == 0) {
228794dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2288387d8239SMatt Jacob 			goto mdp;
2289387d8239SMatt Jacob 		}
2290387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
229194dff771SMatt 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);
2292387d8239SMatt Jacob 			goto mdp;
2293387d8239SMatt Jacob 		}
229494dff771SMatt 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);
2295387d8239SMatt Jacob 		break;
2296387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
229794dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2298387d8239SMatt Jacob 		atp->sendst = 1;
2299387d8239SMatt Jacob 		/*
2300387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
2301387d8239SMatt Jacob 		 */
2302387d8239SMatt Jacob 		break;
2303387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
2304387d8239SMatt Jacob 		/*
2305387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2306387d8239SMatt Jacob 		 */
2307387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2308387d8239SMatt Jacob 		goto fail;
2309387d8239SMatt Jacob 
2310387d8239SMatt Jacob 	default:
2311387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2312387d8239SMatt Jacob 		goto fail;
2313387d8239SMatt Jacob 	}
2314387d8239SMatt Jacob 
2315387d8239SMatt Jacob 	/*
2316387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2317387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2318387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2319387d8239SMatt Jacob 	 */
2320387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2321387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2322387d8239SMatt Jacob 		goto fail;
2323387d8239SMatt Jacob 	}
2324387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2325387d8239SMatt Jacob 	return;
2326387d8239SMatt Jacob fail:
2327387d8239SMatt Jacob 	inot->in_reserved = 1;
2328387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2329387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2330387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2331387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2332387d8239SMatt Jacob 	return;
2333387d8239SMatt Jacob mdp:
2334387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2335387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2336387d8239SMatt Jacob 		goto fail;
2337387d8239SMatt Jacob 	}
2338387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2339387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2340387d8239SMatt Jacob 	/*
2341387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2342387d8239SMatt Jacob 	 */
2343387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2344387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2345387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2346387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2347387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2348387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2349387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2350387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2351387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2352387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2353387d8239SMatt Jacob }
2354387d8239SMatt Jacob 
2355387d8239SMatt Jacob 
2356387d8239SMatt Jacob static void
2357387d8239SMatt Jacob isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2358387d8239SMatt Jacob {
2359387d8239SMatt Jacob 	tstate_t *tptr;
2360387d8239SMatt Jacob 	in_fcentry_24xx_t *inot = inot_raw;
2361387d8239SMatt Jacob 	atio_private_data_t *atp;
2362387d8239SMatt Jacob 	uint32_t tag = inot->in_rxid;
2363387d8239SMatt Jacob 	uint32_t bus = inot->in_vpidx;
2364387d8239SMatt Jacob 
2365387d8239SMatt Jacob 	if (!IS_24XX(isp)) {
2366387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2367387d8239SMatt Jacob 		return;
2368387d8239SMatt Jacob 	}
2369387d8239SMatt Jacob 
2370387d8239SMatt Jacob 	tptr = get_lun_statep_from_tag(isp, bus, tag);
2371387d8239SMatt Jacob 	if (tptr == NULL) {
2372387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2373387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2374387d8239SMatt Jacob 		return;
2375387d8239SMatt Jacob 	}
2376523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, tag);
2377387d8239SMatt Jacob 	if (atp == NULL) {
2378387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2379387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2380387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2381387d8239SMatt Jacob 		return;
2382387d8239SMatt Jacob 	}
2383387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2384387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
238594dff771SMatt 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,
2386387d8239SMatt Jacob 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2387387d8239SMatt Jacob 	if (atp->srr_ccb)
2388387d8239SMatt Jacob 		isp_handle_srr_start(isp, tptr, atp);
2389387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
2390387d8239SMatt Jacob }
2391387d8239SMatt Jacob 
23922df76c16SMatt Jacob static void
23939cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2394d81ba9d5SMatt Jacob {
2395d81ba9d5SMatt Jacob 	union ccb *ccb;
239694dff771SMatt Jacob 	int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
23972df76c16SMatt Jacob 	tstate_t *tptr = NULL;
23982df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
23992df76c16SMatt Jacob 	int bus;
240094dff771SMatt Jacob 	uint32_t handle, moved_data = 0, data_requested;
2401d81ba9d5SMatt Jacob 
24022df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2403970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
24042df76c16SMatt Jacob 	if (ccb == NULL) {
24052df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
24062df76c16SMatt Jacob 		return;
24072df76c16SMatt Jacob 	}
2408970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
240994dff771SMatt Jacob 	data_requested = PISP_PCMD(ccb)->datalen;
2410387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2411387d8239SMatt Jacob 	if (isp->isp_nactive) {
2412387d8239SMatt Jacob 		isp->isp_nactive--;
2413387d8239SMatt Jacob 	}
2414387d8239SMatt Jacob 
24152df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
24162df76c16SMatt Jacob 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
24172df76c16SMatt Jacob 	if (tptr == NULL) {
24182df76c16SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
24192df76c16SMatt Jacob 	}
2420387d8239SMatt Jacob 	if (tptr == NULL) {
2421387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2422387d8239SMatt Jacob 		return;
24232df76c16SMatt Jacob 	}
2424387d8239SMatt Jacob 
2425387d8239SMatt Jacob 	if (IS_24XX(isp)) {
2426523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2427387d8239SMatt Jacob 	} else {
24283e6deb33SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2429387d8239SMatt Jacob 	}
2430387d8239SMatt Jacob 	if (atp == NULL) {
2431af207637SAlexander Motin 		/*
24328656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
24338656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
24348656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2435af207637SAlexander Motin 		 */
2436af207637SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2437af207637SAlexander Motin 	}
2438af207637SAlexander Motin 	if (atp == NULL) {
2439387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2440387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2441387d8239SMatt Jacob 		return;
2442387d8239SMatt Jacob 	}
2443387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
244494dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2445387d8239SMatt Jacob 	atp->ctcnt -= 1;
2446387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2447387d8239SMatt Jacob 
24482df76c16SMatt Jacob 	if (IS_24XX(isp)) {
24492df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2450d81ba9d5SMatt Jacob 
2451387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2452387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2453387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2454387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
24552df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
24562df76c16SMatt Jacob 			return;
24572df76c16SMatt Jacob 		}
2458387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
2459387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2460387d8239SMatt Jacob 		} else {
24612df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
24622df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
246394dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
24642df76c16SMatt Jacob 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
24652df76c16SMatt Jacob 				resid = ct->ct_resid;
246694dff771SMatt Jacob 				moved_data = data_requested - resid;
24672df76c16SMatt Jacob 			}
24682df76c16SMatt Jacob 		}
246994dff771SMatt 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),
247094dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
24713e6deb33SAlexander Motin 	} else {
24722df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2473387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2474387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2475387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2476387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
24772df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
2478387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
24792df76c16SMatt Jacob 			return;
2480570c7a3fSMatt Jacob 		}
2481387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
2482387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2483387d8239SMatt Jacob 		} else {
2484d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2485d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
248694dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
24875d571944SMatt Jacob 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2488a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
248994dff771SMatt Jacob 				moved_data = data_requested - resid;
2490570c7a3fSMatt Jacob 			}
249153036e92SMatt Jacob 		}
249294dff771SMatt 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),
249394dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
24945d571944SMatt Jacob 	}
2495387d8239SMatt Jacob 	if (ok) {
2496387d8239SMatt Jacob 		if (moved_data) {
249794dff771SMatt Jacob 			atp->bytes_xfered += moved_data;
249894dff771SMatt Jacob 			ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
2499387d8239SMatt Jacob 		}
2500387d8239SMatt Jacob 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2501387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2502387d8239SMatt Jacob 		}
2503387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2504387d8239SMatt Jacob 	} else {
2505387d8239SMatt Jacob 		notify_cam = 1;
2506387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2507387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2508387d8239SMatt Jacob 		else
2509387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2510387d8239SMatt Jacob 	}
2511387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2512387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
2513a1bc34c6SMatt Jacob 
2514a1bc34c6SMatt Jacob 	/*
2515387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2516387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2517d81ba9d5SMatt Jacob 	 */
251894dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
251994dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2520f48ce188SMatt Jacob 	if (notify_cam == 0) {
2521387d8239SMatt Jacob 		if (atp->sendst) {
2522387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2523387d8239SMatt Jacob 		}
25242df76c16SMatt Jacob 		return;
2525f48ce188SMatt Jacob 	}
2526d81ba9d5SMatt Jacob 
2527387d8239SMatt Jacob 	/*
2528387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2529387d8239SMatt Jacob 	 *
2530387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2531387d8239SMatt Jacob 	 *
2532387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2533387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2534387d8239SMatt Jacob 	 * and try again in case we didn't have
2535387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2536387d8239SMatt Jacob 	 * once the putback is done we do the completion
2537387d8239SMatt Jacob 	 * call.
2538387d8239SMatt Jacob 	 */
2539387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2540a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2541387d8239SMatt Jacob 	} else {
2542387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2543d81ba9d5SMatt Jacob 	}
2544d81ba9d5SMatt Jacob }
2545570c7a3fSMatt Jacob 
25462df76c16SMatt Jacob static void
25479cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2548570c7a3fSMatt Jacob {
25492df76c16SMatt Jacob 	int needack = 1;
2550570c7a3fSMatt Jacob 	switch (inp->in_status) {
2551570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
25522df76c16SMatt Jacob 		/*
25532df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
25542df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
25552df76c16SMatt Jacob 		 */
25562df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2557570c7a3fSMatt Jacob 		break;
2558570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
25592df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2560570c7a3fSMatt Jacob 		break;
2561570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
25622df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
2563570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2564570c7a3fSMatt Jacob 		break;
2565570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
2566570c7a3fSMatt Jacob 	{
2567c5fd36edSAlexander Motin 		uint16_t nphdl, lun;
2568c5fd36edSAlexander Motin 		uint32_t sid;
25692df76c16SMatt Jacob 		uint64_t wwn;
25702df76c16SMatt Jacob 		fcportdb_t *lp;
2571*26719198SAlexander Motin 		isp_notify_t tmp, *nt = &tmp;
25722df76c16SMatt Jacob 
25732df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
25742df76c16SMatt Jacob 			lun = inp->in_scclun;
25756af11b82SAlexander Motin #if __FreeBSD_version < 1000700
25766af11b82SAlexander Motin 			lun &= 0x3fff;
25776af11b82SAlexander Motin #endif
25782df76c16SMatt Jacob 		} else {
25792df76c16SMatt Jacob 			lun = inp->in_lun;
25802df76c16SMatt Jacob 		}
25812df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
2582c5fd36edSAlexander Motin 			nphdl = ((in_fcentry_e_t *)inp)->in_iid;
25832df76c16SMatt Jacob 		} else {
2584c5fd36edSAlexander Motin 			nphdl = inp->in_iid;
25852df76c16SMatt Jacob 		}
2586c5fd36edSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
25872df76c16SMatt Jacob 			wwn = lp->port_wwn;
2588d4f3ad3aSAlexander Motin 			sid = lp->portid;
25892df76c16SMatt Jacob 		} else {
25902df76c16SMatt Jacob 			wwn = INI_ANY;
2591d4f3ad3aSAlexander Motin 			sid = PORT_ANY;
25922df76c16SMatt Jacob 		}
2593*26719198SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx",
2594*26719198SAlexander Motin 		    inp->in_seqid, (unsigned long long) wwn);
2595570c7a3fSMatt Jacob 
25962df76c16SMatt Jacob 		ISP_MEMZERO(nt, sizeof (isp_notify_t));
25972df76c16SMatt Jacob 		nt->nt_hba = isp;
25982df76c16SMatt Jacob 		nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
25992df76c16SMatt Jacob 		nt->nt_wwn = wwn;
2600c5fd36edSAlexander Motin 		nt->nt_nphdl = nphdl;
2601d4f3ad3aSAlexander Motin 		nt->nt_sid = sid;
26022df76c16SMatt Jacob 		nt->nt_did = PORT_ANY;
26032df76c16SMatt Jacob 		nt->nt_lun = lun;
2604*26719198SAlexander Motin 		nt->nt_tagval = inp->in_seqid;
2605*26719198SAlexander Motin 		nt->nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
26062df76c16SMatt Jacob 		nt->nt_need_ack = 1;
26072df76c16SMatt Jacob 		nt->nt_channel = 0;
26082df76c16SMatt Jacob 		nt->nt_ncode = NT_ABORT_TASK;
2609*26719198SAlexander Motin 		nt->nt_lreserved = inp;
26102df76c16SMatt Jacob 		isp_handle_platform_target_tmf(isp, nt);
26112df76c16SMatt Jacob 		needack = 0;
2612570c7a3fSMatt Jacob 		break;
2613570c7a3fSMatt Jacob 	}
2614570c7a3fSMatt Jacob 	default:
2615570c7a3fSMatt Jacob 		break;
2616570c7a3fSMatt Jacob 	}
26172df76c16SMatt Jacob 	if (needack) {
2618387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
26192df76c16SMatt Jacob 	}
26202df76c16SMatt Jacob }
26212df76c16SMatt Jacob 
26222df76c16SMatt Jacob static void
26232df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
26242df76c16SMatt Jacob {
26252df76c16SMatt Jacob 	uint16_t nphdl;
2626387d8239SMatt Jacob 	uint16_t prli_options = 0;
26272df76c16SMatt Jacob 	uint32_t portid;
26282df76c16SMatt Jacob 	fcportdb_t *lp;
262937a7daacSAlexander Motin 	char *msg = NULL;
263037a7daacSAlexander Motin 	uint8_t *ptr = (uint8_t *)inot;
263137a7daacSAlexander Motin 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
26322df76c16SMatt Jacob 
26332df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
26342df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
26352df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
26362df76c16SMatt Jacob 	} else {
26372df76c16SMatt Jacob 		portid = PORT_ANY;
26382df76c16SMatt Jacob 	}
26392df76c16SMatt Jacob 
26402df76c16SMatt Jacob 	switch (inot->in_status) {
26412df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
26422df76c16SMatt Jacob 	{
264337a7daacSAlexander Motin 		char buf[16];
26442df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
26452df76c16SMatt Jacob 
26462df76c16SMatt Jacob 		/*
26472df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
2648b1ce21c6SRebecca Cran 		 * (possibly with some associated information sent upstream). This is
26492df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
26502df76c16SMatt Jacob 		 */
26512df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
26522df76c16SMatt Jacob 		case LOGO:
26532df76c16SMatt Jacob 			msg = "LOGO";
265437a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
265537a7daacSAlexander Motin 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
26562df76c16SMatt Jacob 			break;
26572df76c16SMatt Jacob 		case PRLO:
26582df76c16SMatt Jacob 			msg = "PRLO";
26592df76c16SMatt Jacob 			break;
26602df76c16SMatt Jacob 		case PLOGI:
26612df76c16SMatt Jacob 			msg = "PLOGI";
266237a7daacSAlexander Motin 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
266337a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
266437a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
266537a7daacSAlexander Motin 			    nphdl, portid, prli_options);
266637a7daacSAlexander Motin 			break;
266737a7daacSAlexander Motin 		case PRLI:
2668dad28623SMatt Jacob 			msg = "PRLI";
266937a7daacSAlexander Motin 			prli_options = inot->in_prli_options;
267037a7daacSAlexander Motin 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
267137a7daacSAlexander Motin 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
267237a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
267337a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
267437a7daacSAlexander Motin 			    nphdl, portid, prli_options);
26752df76c16SMatt Jacob 			break;
26762df76c16SMatt Jacob 		case PDISC:
26772df76c16SMatt Jacob 			msg = "PDISC";
26782df76c16SMatt Jacob 			break;
26792df76c16SMatt Jacob 		case ADISC:
26802df76c16SMatt Jacob 			msg = "ADISC";
26812df76c16SMatt Jacob 			break;
26822df76c16SMatt Jacob 		default:
26832df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
26842df76c16SMatt Jacob 			msg = buf;
26852df76c16SMatt Jacob 			break;
26862df76c16SMatt Jacob 		}
26872df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
26882df76c16SMatt 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);
26892df76c16SMatt Jacob 			break;
26902df76c16SMatt Jacob 		}
26912df76c16SMatt 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,
26922df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
2693387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
26942df76c16SMatt Jacob 		break;
26952df76c16SMatt Jacob 	}
26962df76c16SMatt Jacob 
26972df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
269837a7daacSAlexander Motin 		msg = "PORT LOGOUT";
2699e68eef14SAlexander Motin 		if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
27002df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
27012df76c16SMatt Jacob 		}
27022df76c16SMatt Jacob 		/* FALLTHROUGH */
27032df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
270437a7daacSAlexander Motin 		if (msg == NULL)
270537a7daacSAlexander Motin 			msg = "PORT CHANGED";
27062df76c16SMatt Jacob 		/* FALLTHROUGH */
27072df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
270837a7daacSAlexander Motin 		if (msg == NULL)
270937a7daacSAlexander Motin 			msg = "LIP RESET";
271037a7daacSAlexander 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);
27112df76c16SMatt Jacob 
27122df76c16SMatt Jacob 		/*
27132df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
27142df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
27152df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
27162df76c16SMatt Jacob 		 */
27172df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
2718387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
27192df76c16SMatt Jacob 		break;
27202df76c16SMatt Jacob 
27212df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
2722387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
2723387d8239SMatt Jacob 		isp_handle_srr_notify(isp, inot);
2724387d8239SMatt Jacob 		break;
2725387d8239SMatt Jacob #else
272637a7daacSAlexander Motin 		if (msg == NULL)
272737a7daacSAlexander Motin 			msg = "SRR RCVD";
2728387d8239SMatt Jacob 		/* FALLTHROUGH */
2729387d8239SMatt Jacob #endif
2730387d8239SMatt Jacob 	case IN24XX_LINK_RESET:
273137a7daacSAlexander Motin 		if (msg == NULL)
273237a7daacSAlexander Motin 			msg = "LINK RESET";
2733387d8239SMatt Jacob 	case IN24XX_LINK_FAILED:
273437a7daacSAlexander Motin 		if (msg == NULL)
273537a7daacSAlexander Motin 			msg = "LINK FAILED";
27362df76c16SMatt Jacob 	default:
273737a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2738387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
27392df76c16SMatt Jacob 		break;
27402df76c16SMatt Jacob 	}
27412df76c16SMatt Jacob }
27422df76c16SMatt Jacob 
27432df76c16SMatt Jacob static int
274496b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
27452df76c16SMatt Jacob {
27462df76c16SMatt Jacob 
27472df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
27482df76c16SMatt 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);
2749570c7a3fSMatt Jacob 		return (0);
2750570c7a3fSMatt Jacob 	}
27512df76c16SMatt Jacob 
27522df76c16SMatt Jacob 	/*
27532df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
27542df76c16SMatt Jacob 	 */
27552df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
27562df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
27572df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
27582df76c16SMatt Jacob 		fcportdb_t *lp;
27592df76c16SMatt Jacob 		uint32_t sid;
27602df76c16SMatt Jacob 		uint16_t nphdl;
27612df76c16SMatt Jacob 
27622df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2763eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
27642df76c16SMatt Jacob 			nphdl = lp->handle;
27652df76c16SMatt Jacob 		} else {
27662df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
27672df76c16SMatt Jacob 		}
27682df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
27692df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
27702df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
27712df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
27722df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
27732df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
27742df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
27752df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
27762df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
27772df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
27782df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
277996b5475bSAlexander Motin 		if (rsp != 0) {
278096b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
278196b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
278296b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
278396b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
278496b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
278596b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
278696b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
278796b5475bSAlexander Motin 		}
27882df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
27892df76c16SMatt Jacob 	}
27902df76c16SMatt Jacob 
27912df76c16SMatt Jacob 	/*
27922df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
27932df76c16SMatt Jacob 	 */
27942df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
27952df76c16SMatt Jacob 
27962df76c16SMatt Jacob 		/*
27972df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
27982df76c16SMatt Jacob 		 */
27992df76c16SMatt Jacob 		if (mp->nt_need_ack) {
28002df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
28012df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
28022df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
28032df76c16SMatt Jacob 
28042df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
28052df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
28062df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
28072df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
28082df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
28092df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
28102df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
28112df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
28122df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
28132df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
28142df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
28152df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
28162df76c16SMatt Jacob 				return (ENOMEM);
28172df76c16SMatt Jacob 			}
28182df76c16SMatt Jacob 			mp->nt_need_ack = 0;
28192df76c16SMatt Jacob 		}
28202df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
28212df76c16SMatt Jacob 			return (ENOMEM);
28222df76c16SMatt Jacob 		} else {
28232df76c16SMatt Jacob 			return (0);
28242df76c16SMatt Jacob 		}
28252df76c16SMatt Jacob 	}
28262df76c16SMatt Jacob 
28272df76c16SMatt Jacob 	/*
28282df76c16SMatt Jacob 	 * Handle logout cases here
28292df76c16SMatt Jacob 	 */
28302df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
28312df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
28322df76c16SMatt Jacob 	}
28332df76c16SMatt Jacob 
28342df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
28352df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
28362df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
28372df76c16SMatt Jacob 		}
28382df76c16SMatt Jacob 	}
28392df76c16SMatt Jacob 
28402df76c16SMatt Jacob 	/*
28412df76c16SMatt Jacob 	 * General purpose acknowledgement
28422df76c16SMatt Jacob 	 */
28432df76c16SMatt Jacob 	if (mp->nt_need_ack) {
28442df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2845387d8239SMatt Jacob 		/*
2846387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2847387d8239SMatt Jacob 		 */
28482df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
28492df76c16SMatt Jacob 	}
28502df76c16SMatt Jacob 	return (0);
28512df76c16SMatt Jacob }
28522df76c16SMatt Jacob 
28532df76c16SMatt Jacob /*
2854b1ce21c6SRebecca Cran  * Handle task management functions.
28552df76c16SMatt Jacob  *
28562df76c16SMatt Jacob  * We show up here with a notify structure filled out.
28572df76c16SMatt Jacob  *
28582df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
28592df76c16SMatt Jacob  */
28602df76c16SMatt Jacob static void
28612df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
28622df76c16SMatt Jacob {
28632df76c16SMatt Jacob 	tstate_t *tptr;
28642df76c16SMatt Jacob 	fcportdb_t *lp;
28652df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
28662df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
28672df76c16SMatt Jacob 	lun_id_t lun;
28682df76c16SMatt Jacob 
28692df76c16SMatt 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,
28702df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
28712df76c16SMatt Jacob 	/*
28722df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
28732df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
28742df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
28752df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
28762df76c16SMatt Jacob 	 */
28772df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
28782df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
28792df76c16SMatt Jacob 	} else {
28802df76c16SMatt Jacob 		lun = notify->nt_lun;
28812df76c16SMatt Jacob 	}
28822df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
28832df76c16SMatt Jacob 	if (tptr == NULL) {
28842df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
28852df76c16SMatt Jacob 		if (tptr == NULL) {
2886123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
28872df76c16SMatt Jacob 			goto bad;
28882df76c16SMatt Jacob 		}
28892df76c16SMatt Jacob 	}
28902df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
28912df76c16SMatt Jacob 	if (inot == NULL) {
2892123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
28932df76c16SMatt Jacob 		goto bad;
28942df76c16SMatt Jacob 	}
28952df76c16SMatt Jacob 
2896eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2897d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
28982df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
28992df76c16SMatt Jacob 	} else {
2900d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
29012df76c16SMatt Jacob 	}
29022df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
29032df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
29042df76c16SMatt Jacob 
29052df76c16SMatt Jacob 	switch (notify->nt_ncode) {
29062df76c16SMatt Jacob 	case NT_ABORT_TASK:
29072df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
29082df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
29092df76c16SMatt Jacob 		break;
29102df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
29112df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
29122df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
29132df76c16SMatt Jacob 		break;
29142df76c16SMatt Jacob 	case NT_CLEAR_ACA:
29152df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
29162df76c16SMatt Jacob 		break;
29172df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
29182df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
29192df76c16SMatt Jacob 		break;
29202df76c16SMatt Jacob 	case NT_LUN_RESET:
29212df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
29222df76c16SMatt Jacob 		break;
29232df76c16SMatt Jacob 	case NT_TARGET_RESET:
29242df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
29252df76c16SMatt Jacob 		break;
2926c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2927c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2928c98d2b1fSAlexander Motin 		break;
2929c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2930c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2931c98d2b1fSAlexander Motin 		break;
29322df76c16SMatt Jacob 	default:
2933123055f0SNathan 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);
29342df76c16SMatt Jacob 		goto bad;
29352df76c16SMatt Jacob 	}
29362df76c16SMatt Jacob 
29372df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
29382df76c16SMatt Jacob 	if (ntp == NULL) {
29392df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
29402df76c16SMatt Jacob 		goto bad;
29412df76c16SMatt Jacob 	}
29422df76c16SMatt Jacob 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
29432df76c16SMatt Jacob 	if (notify->nt_lreserved) {
29442df76c16SMatt Jacob 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
29452df76c16SMatt Jacob 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
29462df76c16SMatt Jacob 	}
29472df76c16SMatt Jacob 	ntp->rd.seq_id = notify->nt_tagval;
29482df76c16SMatt Jacob 	ntp->rd.tag_id = notify->nt_tagval >> 32;
29492df76c16SMatt Jacob 
29502df76c16SMatt Jacob 	tptr->inot_count--;
29512df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
29522df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2953387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
29542df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
29552df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
29562df76c16SMatt Jacob 	return;
29572df76c16SMatt Jacob bad:
29582df76c16SMatt Jacob 	if (tptr) {
29592df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
29602df76c16SMatt Jacob 	}
29612df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
29622df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2963387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2964387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2965387d8239SMatt Jacob 			}
29662df76c16SMatt Jacob 		} else {
2967387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
29682df76c16SMatt Jacob 		}
29692df76c16SMatt Jacob 	}
29702df76c16SMatt Jacob }
29712df76c16SMatt Jacob 
29722df76c16SMatt Jacob /*
2973b1ce21c6SRebecca Cran  * Find the associated private data and mark it as dead so
29742df76c16SMatt Jacob  * we don't try to work on it any further.
29752df76c16SMatt Jacob  */
29762df76c16SMatt Jacob static void
29772df76c16SMatt Jacob isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
29782df76c16SMatt Jacob {
29792df76c16SMatt Jacob 	tstate_t *tptr;
29802df76c16SMatt Jacob 	atio_private_data_t *atp;
298145210a25SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
29822df76c16SMatt Jacob 
298345210a25SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
29842df76c16SMatt Jacob 	if (tptr == NULL) {
298545210a25SAlexander Motin 		tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
29862df76c16SMatt Jacob 		if (tptr == NULL) {
29872df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
29882df76c16SMatt Jacob 			return;
29892df76c16SMatt Jacob 		}
29902df76c16SMatt Jacob 	}
29912df76c16SMatt Jacob 
2992523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
29932df76c16SMatt Jacob 	if (atp == NULL) {
29942df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INVALID;
299545210a25SAlexander Motin 	} else {
29962df76c16SMatt Jacob 		atp->dead = 1;
29972df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
29982df76c16SMatt Jacob 	}
299945210a25SAlexander Motin 	rls_lun_statep(isp, tptr);
300045210a25SAlexander Motin }
30012df76c16SMatt Jacob 
30022df76c16SMatt Jacob static void
30032df76c16SMatt Jacob isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
30042df76c16SMatt Jacob {
30052df76c16SMatt Jacob 	atio_private_data_t *atp;
30062df76c16SMatt Jacob 	inot_private_data_t *restart_queue = tptr->restart_queue;
30072df76c16SMatt Jacob 
30082df76c16SMatt Jacob 	/*
30092df76c16SMatt Jacob 	 * First, clean any commands pending restart
30102df76c16SMatt Jacob 	 */
30112df76c16SMatt Jacob 	tptr->restart_queue = NULL;
30122df76c16SMatt Jacob 	while (restart_queue) {
30132df76c16SMatt Jacob 		uint32_t this_tag_id;
30142df76c16SMatt Jacob 		inot_private_data_t *ntp = restart_queue;
30152df76c16SMatt Jacob 
30162df76c16SMatt Jacob 		restart_queue = ntp->rd.nt.nt_hba;
30172df76c16SMatt Jacob 
30182df76c16SMatt Jacob 		if (IS_24XX(isp)) {
30192df76c16SMatt Jacob 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
30202df76c16SMatt Jacob 		} else {
30212df76c16SMatt Jacob 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
30222df76c16SMatt Jacob 		}
30232df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
30242df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
30252df76c16SMatt Jacob 		} else {
30262df76c16SMatt Jacob 			ntp->rd.nt.nt_hba = tptr->restart_queue;
30272df76c16SMatt Jacob 			tptr->restart_queue = ntp;
30282df76c16SMatt Jacob 		}
30292df76c16SMatt Jacob 	}
30302df76c16SMatt Jacob 
30312df76c16SMatt Jacob 	/*
30322df76c16SMatt Jacob 	 * Now mark other ones dead as well.
30332df76c16SMatt Jacob 	 */
30342df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
30352df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
30362df76c16SMatt Jacob 			atp->dead = 1;
30372df76c16SMatt Jacob 		}
30382df76c16SMatt Jacob 	}
30392df76c16SMatt Jacob }
3040d81ba9d5SMatt Jacob #endif
3041d81ba9d5SMatt Jacob 
3042478f8a96SJustin T. Gibbs static void
30431dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3044478f8a96SJustin T. Gibbs {
3045478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
3046e95725cbSMatt Jacob 	int bus, tgt;
30479cd7268eSMatt Jacob 	ispsoftc_t *isp;
3048478f8a96SJustin T. Gibbs 
3049478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
30509cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3051e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
3052e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
3053e95725cbSMatt Jacob 
3054478f8a96SJustin T. Gibbs 	switch (code) {
3055478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
3056ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
30571dae40ebSMatt Jacob 			uint16_t oflags, nflags;
30582df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
3059478f8a96SJustin T. Gibbs 
306041ed683eSMatt Jacob 			if (tgt >= 0) {
30619ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
30629ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
3063a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
3064478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3065478f8a96SJustin T. Gibbs 				}
30669ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
30679ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
3068478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
30692df76c16SMatt Jacob 				sdp->update = 1;
30702df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
30719ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
3072478f8a96SJustin T. Gibbs 			}
307341ed683eSMatt Jacob 		}
3074478f8a96SJustin T. Gibbs 		break;
3075478f8a96SJustin T. Gibbs 	default:
30763c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3077478f8a96SJustin T. Gibbs 		break;
3078478f8a96SJustin T. Gibbs 	}
3079478f8a96SJustin T. Gibbs }
3080478f8a96SJustin T. Gibbs 
3081478f8a96SJustin T. Gibbs static void
3082c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
3083478f8a96SJustin T. Gibbs {
30849cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
30856ce548a1SAlexander Motin 	uint16_t isr, sema, info;
3086126ec864SMatt Jacob 
30876ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
30886ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
3089478f8a96SJustin T. Gibbs }
3090478f8a96SJustin T. Gibbs 
3091ab6c4b31SMatt Jacob 
30922df76c16SMatt Jacob static void
30932df76c16SMatt Jacob isp_watchdog(void *arg)
3094cc8df88bSMatt Jacob {
30952df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
30962df76c16SMatt Jacob 	ispsoftc_t *isp;
3097e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
3098b85389e1SMatt Jacob 
30992df76c16SMatt Jacob 	isp = XS_ISP(xs);
31002df76c16SMatt Jacob 
3101cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
3102e95725cbSMatt Jacob 
3103e95725cbSMatt Jacob 	/*
3104e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3105e95725cbSMatt Jacob 	 */
3106e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
31076ce548a1SAlexander Motin 		uint16_t isr, sema, info;
31086ce548a1SAlexander Motin 		if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
31096ce548a1SAlexander Motin 			isp_intr(isp, isr, sema, info);
3110e95725cbSMatt Jacob 		ohandle = handle;
3111e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
3112e95725cbSMatt Jacob 	}
3113c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
31141fcf5debSMatt Jacob 		/*
3115c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
3116c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
3117c8b8a2c4SMatt Jacob 		 *
3118c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
3119c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
3120c8b8a2c4SMatt Jacob 		 * back separately.
31211fcf5debSMatt Jacob 		 */
3122c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3123c8b8a2c4SMatt Jacob 			return;
3124c8b8a2c4SMatt Jacob 		}
31251fcf5debSMatt Jacob 
31261fcf5debSMatt Jacob 		/*
3127c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
3128c8b8a2c4SMatt Jacob 		 * fact have been completed.
3129c8b8a2c4SMatt Jacob 		 */
3130c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
3131c8b8a2c4SMatt Jacob 
3132c8b8a2c4SMatt Jacob 		/*
3133c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
3134c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
3135c8b8a2c4SMatt Jacob 		 */
3136c8b8a2c4SMatt Jacob 		if (xs == NULL) {
3137c8b8a2c4SMatt Jacob 			return;
3138c8b8a2c4SMatt Jacob 		}
3139c8b8a2c4SMatt Jacob 
3140c8b8a2c4SMatt Jacob 		/*
3141c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
31421fcf5debSMatt Jacob 		 */
3143f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
3144f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
3145f6e75de2SMatt Jacob 		}
3146cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
3147c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3148387d8239SMatt Jacob 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
3149387d8239SMatt Jacob 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
3150e95725cbSMatt Jacob 		isp_prt_endcmd(isp, xs);
3151cc8df88bSMatt Jacob 		isp_done(xs);
3152e95725cbSMatt Jacob 	} else {
3153e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
3154e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3155e95725cbSMatt Jacob 		} else {
3156e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3157e95725cbSMatt Jacob 		}
3158b85389e1SMatt Jacob 	}
3159f7c631bcSMatt Jacob }
3160f7c631bcSMatt Jacob 
3161f7c631bcSMatt Jacob static void
31625704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3163f7c631bcSMatt Jacob {
3164ffcf6651SMatt Jacob 	union ccb *ccb;
31652df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
31662df76c16SMatt Jacob 
3167ffcf6651SMatt Jacob 	/*
31680e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3169ffcf6651SMatt Jacob 	 */
31708008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
3171ffcf6651SMatt Jacob 	if (ccb == NULL) {
31722df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3173ffcf6651SMatt Jacob 		return;
3174ffcf6651SMatt Jacob 	}
3175e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
3176e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3177ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3178ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
3179ffcf6651SMatt Jacob 		return;
3180ffcf6651SMatt Jacob 	}
3181ffcf6651SMatt Jacob 	xpt_rescan(ccb);
3182ffcf6651SMatt Jacob }
3183ffcf6651SMatt Jacob 
3184ffcf6651SMatt Jacob static void
31855704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3186ffcf6651SMatt Jacob {
3187ffcf6651SMatt Jacob 	struct cam_path *tp;
31882df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
31892df76c16SMatt Jacob 
31902df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3191ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
3192ffcf6651SMatt Jacob 		xpt_free_path(tp);
3193f7c631bcSMatt Jacob 	}
3194f7c631bcSMatt Jacob }
3195f7c631bcSMatt Jacob 
3196f7c631bcSMatt Jacob /*
3197f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
3198f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
3199f7c631bcSMatt Jacob  * gone away.
3200f7c631bcSMatt Jacob  *
3201f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
3202f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
3203f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
3204f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
3205f7c631bcSMatt Jacob  */
3206f7c631bcSMatt Jacob static void
3207f7c631bcSMatt Jacob isp_gdt(void *arg)
3208f7c631bcSMatt Jacob {
32092df76c16SMatt Jacob 	struct isp_fc *fc = arg;
3210de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3211de461933SMatt Jacob }
3212de461933SMatt Jacob 
3213de461933SMatt Jacob static void
3214de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
3215de461933SMatt Jacob {
3216de461933SMatt Jacob 	struct isp_fc *fc = arg;
32172df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
32182df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3219f7c631bcSMatt Jacob 	fcportdb_t *lp;
3220e68eef14SAlexander Motin 	struct ac_contract ac;
3221e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3222766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
3223f7c631bcSMatt Jacob 
3224de461933SMatt Jacob 	ISP_LOCK(isp);
3225de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3226f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
32272df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3228f7c631bcSMatt Jacob 
3229f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3230f7c631bcSMatt Jacob 			continue;
3231f7c631bcSMatt Jacob 		}
3232427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
3233427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
3234f7c631bcSMatt Jacob 			more_to_do++;
3235f7c631bcSMatt Jacob 			continue;
3236f7c631bcSMatt Jacob 		}
3237e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3238e68eef14SAlexander Motin 		if (lp->is_target) {
3239e68eef14SAlexander Motin 			lp->is_target = 0;
3240766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3241f7c631bcSMatt Jacob 		}
3242e68eef14SAlexander Motin 		if (lp->is_initiator) {
3243e68eef14SAlexander Motin 			lp->is_initiator = 0;
3244e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3245e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3246e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3247e68eef14SAlexander Motin 			adc->port = lp->portid;
3248d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3249e68eef14SAlexander Motin 			adc->arrived = 0;
3250e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3251e68eef14SAlexander Motin 		}
3252e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
3253e68eef14SAlexander Motin 	}
3254a01f5aebSMatt Jacob 	if (fc->ready) {
3255f7c631bcSMatt Jacob 		if (more_to_do) {
32562df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
3257f7c631bcSMatt Jacob 		} else {
3258de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
3259387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3260a01f5aebSMatt Jacob 		}
3261f7c631bcSMatt Jacob 	}
3262de461933SMatt Jacob 	ISP_UNLOCK(isp);
3263f7c631bcSMatt Jacob }
3264f7c631bcSMatt Jacob 
3265f7c631bcSMatt Jacob /*
3266e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
3267e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
3268e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
3269f7c631bcSMatt Jacob  *
3270f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
3271f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
3272f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3273f7c631bcSMatt Jacob  */
3274f7c631bcSMatt Jacob static void
3275e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
3276f7c631bcSMatt Jacob {
3277e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3278e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3279e561aa79SAlexander Motin 
3280e561aa79SAlexander Motin 	if (fc->loop_down_time)
3281e561aa79SAlexander Motin 		return;
3282e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3283e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
3284e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
3285e561aa79SAlexander Motin 	fc->loop_dead = 0;
3286e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
3287e561aa79SAlexander Motin 	wakeup(fc);
3288de461933SMatt Jacob }
3289de461933SMatt Jacob 
3290de461933SMatt Jacob static void
3291e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
3292de461933SMatt Jacob {
3293e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3294e561aa79SAlexander Motin 
3295e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3296e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
3297e561aa79SAlexander Motin 	fc->loop_dead = 0;
3298e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3299e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
3300e561aa79SAlexander Motin }
3301e561aa79SAlexander Motin 
3302e561aa79SAlexander Motin static void
3303e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
3304e561aa79SAlexander Motin {
3305e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3306e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3307f7c631bcSMatt Jacob 	fcportdb_t *lp;
3308e68eef14SAlexander Motin 	struct ac_contract ac;
3309e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3310766a65a5SAlexander Motin 	int dbidx, i;
3311f7c631bcSMatt Jacob 
3312e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
33130a70657fSMatt Jacob 
3314f7c631bcSMatt Jacob 	/*
3315f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
3316f7c631bcSMatt Jacob 	 */
3317f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3318e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
3319f7c631bcSMatt Jacob 
3320e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
3321f7c631bcSMatt Jacob 			continue;
3322f7c631bcSMatt Jacob 
3323f7c631bcSMatt Jacob 		/*
3324f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3325f7c631bcSMatt Jacob 		 */
3326e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
3327e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
3328e95725cbSMatt Jacob 
3329970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3330e95725cbSMatt Jacob 				continue;
3331e95725cbSMatt Jacob 			}
3332e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3333e95725cbSMatt Jacob 				continue;
3334e95725cbSMatt Jacob                         }
3335766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
3336e95725cbSMatt Jacob 				continue;
3337e95725cbSMatt Jacob 			}
33386af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
33396af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
33406af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
3341e95725cbSMatt Jacob 		}
3342e95725cbSMatt Jacob 
3343e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3344e68eef14SAlexander Motin 		if (lp->is_target) {
3345e68eef14SAlexander Motin 			lp->is_target = 0;
3346766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3347f7c631bcSMatt Jacob 		}
3348e68eef14SAlexander Motin 		if (lp->is_initiator) {
3349e68eef14SAlexander Motin 			lp->is_initiator = 0;
3350e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3351e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3352e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3353e68eef14SAlexander Motin 			adc->port = lp->portid;
3354d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3355e68eef14SAlexander Motin 			adc->arrived = 0;
3356e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3357e95725cbSMatt Jacob 		}
3358e68eef14SAlexander Motin 	}
3359e68eef14SAlexander Motin 
3360e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
33612df76c16SMatt Jacob 	fc->loop_dead = 1;
3362e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3363cc8df88bSMatt Jacob }
3364cc8df88bSMatt Jacob 
33655d571944SMatt Jacob static void
33665d571944SMatt Jacob isp_kthread(void *arg)
33675d571944SMatt Jacob {
33682df76c16SMatt Jacob 	struct isp_fc *fc = arg;
33692df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
33702df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3371e561aa79SAlexander Motin 	int slp = 0, d;
3372e561aa79SAlexander Motin 	int lb, lim;
3373a01f5aebSMatt Jacob 
33740a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
33752df76c16SMatt Jacob 
33761d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
3377e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3378e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
33792df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
3380e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3381e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
3382e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
33832df76c16SMatt Jacob 
33842df76c16SMatt Jacob 		/*
33852df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
33862df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
33872df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
33882df76c16SMatt Jacob 		 * check again.
33892df76c16SMatt Jacob 		 *
33902df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
33912df76c16SMatt Jacob 		 */
3392e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
3393e561aa79SAlexander Motin 			slp = 0;
339410365e5aSMatt Jacob 		} else {
339510365e5aSMatt Jacob 			/*
339610365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
3397f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
3398f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
3399f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
340010365e5aSMatt Jacob 			 */
3401e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
3402f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
3403e561aa79SAlexander Motin 			else
34042df76c16SMatt Jacob 				lim = fc->loop_down_limit;
3405e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
3406e561aa79SAlexander Motin 			if (d >= lim)
3407f7c631bcSMatt Jacob 				slp = 0;
3408e561aa79SAlexander Motin 			else if (d < 10)
340910365e5aSMatt Jacob 				slp = 1;
3410e561aa79SAlexander Motin 			else if (d < 30)
3411f7c631bcSMatt Jacob 				slp = 5;
3412e561aa79SAlexander Motin 			else if (d < 60)
3413f7c631bcSMatt Jacob 				slp = 10;
3414e561aa79SAlexander Motin 			else if (d < 120)
3415f7c631bcSMatt Jacob 				slp = 20;
3416e561aa79SAlexander Motin 			else
3417f7c631bcSMatt Jacob 				slp = 30;
3418f44257c2SMatt Jacob 		}
341910365e5aSMatt Jacob 
3420e561aa79SAlexander Motin 		if (slp == 0) {
3421e561aa79SAlexander Motin 			if (lb == LOOP_READY)
3422e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
3423387d8239SMatt Jacob 			else
3424e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
34255d571944SMatt Jacob 		}
3426fdeb9f2fSMatt Jacob 
3427e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3428e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
34292df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
34305d571944SMatt Jacob 	}
34311d0a1de2SWill Andrews 	fc->num_threads -= 1;
34320a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
34331d0a1de2SWill Andrews 	kthread_exit();
34345d571944SMatt Jacob }
34355d571944SMatt Jacob 
3436cc8df88bSMatt Jacob static void
3437c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
3438478f8a96SJustin T. Gibbs {
3439e561aa79SAlexander Motin 	int bus, tgt, ts, error;
34409cd7268eSMatt Jacob 	ispsoftc_t *isp;
34414663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
3442478f8a96SJustin T. Gibbs 
3443478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3444478f8a96SJustin T. Gibbs 
34459cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
34462df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
34475cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
34485cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
34492df76c16SMatt Jacob 
3450478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
3451478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
34522df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
3453478f8a96SJustin T. Gibbs 		/*
3454478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
3455478f8a96SJustin T. Gibbs 		 */
3456478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3457478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3458478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
34595cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3460478f8a96SJustin T. Gibbs 				break;
3461478f8a96SJustin T. Gibbs 			}
3462478f8a96SJustin T. Gibbs 		}
3463387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
34640470d791SMatt Jacob #ifdef	DIAGNOSTIC
34656af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3466dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3467478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
34686af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
34696af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3470dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3471478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3472478f8a96SJustin T. Gibbs 		}
3473478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3474478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3475478f8a96SJustin T. Gibbs 			break;
3476478f8a96SJustin T. Gibbs 		}
34770470d791SMatt Jacob #endif
34780a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
34790a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
34800a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
34810a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
34822df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
34835cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
34840a70657fSMatt Jacob 			xpt_done(ccb);
34850a70657fSMatt Jacob 			break;
34860a70657fSMatt Jacob 		}
3487b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
34880470d791SMatt Jacob 		switch (error) {
3489478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3490478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
34910a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
34920a70657fSMatt Jacob 				break;
3493d69a5f7dSMatt Jacob 			}
34940a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
34950a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
34960a70657fSMatt Jacob 				ts = 60*1000;
3497cc8df88bSMatt Jacob 			}
34980a70657fSMatt Jacob 			ts = isp_mstohz(ts);
34992df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3500478f8a96SJustin T. Gibbs 			break;
35010470d791SMatt Jacob 		case CMD_RQLATER:
3502f44257c2SMatt Jacob 			/*
3503e561aa79SAlexander Motin 			 * We get this result if the loop isn't ready
3504e561aa79SAlexander Motin 			 * or if the device in question has gone zombie.
350510365e5aSMatt Jacob 			 */
3506e561aa79SAlexander Motin 			if (ISP_FC_PC(isp, bus)->loop_dead) {
35076af11b82SAlexander Motin 				isp_prt(isp, ISP_LOGDEBUG0,
3508e561aa79SAlexander Motin 				    "%d.%jx loop is dead",
3509e561aa79SAlexander Motin 				    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
35105cbe3e8eSAlexander Motin 				ccb->ccb_h.status = CAM_SEL_TIMEOUT;
35115cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3512f44257c2SMatt Jacob 				break;
3513f44257c2SMatt Jacob 			}
35146af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
35156af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3516f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
35172df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3518387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
35190a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3520478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3521478f8a96SJustin T. Gibbs 			break;
35220470d791SMatt Jacob 		case CMD_EAGAIN:
35230a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
35242df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
35252df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3526387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3527478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3528478f8a96SJustin T. Gibbs 			break;
35290470d791SMatt Jacob 		case CMD_COMPLETE:
35300470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
35310470d791SMatt Jacob 			break;
35320470d791SMatt Jacob 		default:
35332df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3534387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
35350a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
35360470d791SMatt Jacob 			xpt_done(ccb);
3537478f8a96SJustin T. Gibbs 		}
3538478f8a96SJustin T. Gibbs 		break;
3539478f8a96SJustin T. Gibbs 
3540d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
35412df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
35422df76c16SMatt Jacob 		if (ccb->cel.enable) {
35432df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
35442df76c16SMatt Jacob 		} else {
35452df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
354667ff51f1SMatt Jacob 		}
354767ff51f1SMatt Jacob 		break;
35482df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3549d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3550d81ba9d5SMatt Jacob 	{
35512df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3552d81ba9d5SMatt Jacob 		if (tptr == NULL) {
35532df76c16SMatt Jacob 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
35542df76c16SMatt Jacob 		}
35552df76c16SMatt Jacob 		if (tptr == NULL) {
35562df76c16SMatt Jacob 			const char *str;
35572df76c16SMatt Jacob 			uint32_t tag;
35582df76c16SMatt Jacob 
35592df76c16SMatt Jacob 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
35602df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
35612df76c16SMatt Jacob 				tag = ccb->cin1.seq_id;
35622df76c16SMatt Jacob 			} else {
35632df76c16SMatt Jacob 				tag = ccb->atio.tag_id;
35642df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
35652df76c16SMatt Jacob 			}
35662df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
35672df76c16SMatt Jacob 			dump_tstates(isp, XS_CHANNEL(ccb));
35682df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3569d81ba9d5SMatt Jacob 			break;
3570d81ba9d5SMatt Jacob 		}
3571387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3572387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3573570c7a3fSMatt Jacob 
3574d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
35752df76c16SMatt Jacob 			if (ccb->atio.tag_id) {
3576523ea374SAlexander Motin 				atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
35772df76c16SMatt Jacob 				if (atp) {
35782df76c16SMatt Jacob 					isp_put_atpd(isp, tptr, atp);
35792df76c16SMatt Jacob 				}
35802df76c16SMatt Jacob 			}
3581570c7a3fSMatt Jacob 			tptr->atio_count++;
35822df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3583387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
3584e2873b76SMatt Jacob 			    ccb->atio.tag_id, tptr->atio_count);
358594dff771SMatt Jacob 			ccb->atio.tag_id = 0;
35862df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
35872df76c16SMatt Jacob 			if (ccb->cin1.tag_id) {
35882df76c16SMatt Jacob 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
35892df76c16SMatt Jacob 				if (ntp) {
35902df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
35912df76c16SMatt Jacob 				}
35922df76c16SMatt Jacob 			}
3593746e9c85SMatt Jacob 			tptr->inot_count++;
35942df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3595387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
3596e2873b76SMatt Jacob 			    ccb->cin1.seq_id, tptr->inot_count);
359794dff771SMatt Jacob 			ccb->cin1.seq_id = 0;
3598d81ba9d5SMatt Jacob 		}
3599d81ba9d5SMatt Jacob 		rls_lun_statep(isp, tptr);
3600d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3601d81ba9d5SMatt Jacob 		break;
3602d81ba9d5SMatt Jacob 	}
36032df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3604d81ba9d5SMatt Jacob 	{
36052df76c16SMatt Jacob 		tstate_t *tptr;
36062df76c16SMatt Jacob 		inot_private_data_t *ntp;
36072df76c16SMatt Jacob 
36082df76c16SMatt Jacob 		/*
36092df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
36102df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
36112df76c16SMatt Jacob 		 */
36122df76c16SMatt Jacob 		/*
36132df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
36142df76c16SMatt Jacob 		 */
36152df76c16SMatt 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);
36162df76c16SMatt Jacob 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
36172df76c16SMatt Jacob 		if (ntp == NULL) {
36182df76c16SMatt 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__,
36192df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
36202df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
36212df76c16SMatt Jacob 			xpt_done(ccb);
3622d81ba9d5SMatt Jacob 			break;
3623d81ba9d5SMatt Jacob 		}
362496b5475bSAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt,
362596b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
36262df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
36272df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
36282df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3629387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3630387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
36312df76c16SMatt Jacob 			break;
36322df76c16SMatt Jacob 		}
36332df76c16SMatt Jacob 		isp_put_ntpd(isp, tptr, ntp);
36342df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
36352df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
36362df76c16SMatt 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);
36372df76c16SMatt Jacob 		xpt_done(ccb);
36382df76c16SMatt Jacob 		break;
36392df76c16SMatt Jacob 	}
36402df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3641387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
36422df76c16SMatt Jacob 		break;
3643d81ba9d5SMatt Jacob #endif
3644478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3645d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3646d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3647d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3648d81ba9d5SMatt Jacob 
36492df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3650478f8a96SJustin T. Gibbs 		if (error) {
3651478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3652478f8a96SJustin T. Gibbs 		} else {
3653b44e442eSWill Andrews 			/*
3654b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3655b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3656b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3657b44e442eSWill Andrews 			 */
365821daf914SAlexander Motin 			if (IS_FC(isp))
365921daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3660b44e442eSWill Andrews 
3661478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3662478f8a96SJustin T. Gibbs 		}
3663478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3664478f8a96SJustin T. Gibbs 		break;
3665478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3666d81ba9d5SMatt Jacob 	{
3667d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3668d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3669d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3670d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
367145210a25SAlexander Motin 			isp_target_mark_aborted(isp, ccb);
3672d81ba9d5SMatt Jacob 			break;
3673d81ba9d5SMatt Jacob #endif
3674d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3675950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3676478f8a96SJustin T. Gibbs 			if (error) {
3677d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3678478f8a96SJustin T. Gibbs 			} else {
3679478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3680478f8a96SJustin T. Gibbs 			}
3681d81ba9d5SMatt Jacob 			break;
3682d81ba9d5SMatt Jacob 		default:
3683d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3684d81ba9d5SMatt Jacob 			break;
3685d81ba9d5SMatt Jacob 		}
36861c0a1eb2SMatt Jacob 		/*
36871c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
36881c0a1eb2SMatt Jacob 		 * complete when control is returned.
36891c0a1eb2SMatt Jacob 		 */
3690478f8a96SJustin T. Gibbs 		break;
3691d81ba9d5SMatt Jacob 	}
3692ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3693478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3694478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
36959ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
36969ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
36979ce9bdafSMatt Jacob 			xpt_done(ccb);
36989ce9bdafSMatt Jacob 			break;
36999ce9bdafSMatt Jacob 		}
3700478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3701805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
37022df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
37032df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
37042df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
37052df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
37061dae40ebSMatt Jacob 			uint16_t *dptr;
3707ab163f5fSMatt Jacob 
3708b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3709b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3710b61386a4SMatt Jacob 				xpt_done(ccb);
3711b61386a4SMatt Jacob 				break;
3712b61386a4SMatt Jacob 			}
3713b61386a4SMatt Jacob 
3714ab163f5fSMatt Jacob 			/*
37159ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3716ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3717ab163f5fSMatt Jacob 			 * vectored to that location.
3718ab163f5fSMatt Jacob 			 */
37199ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3720ab163f5fSMatt Jacob 
3721ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3722ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3723ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3724ab163f5fSMatt Jacob 				else
3725ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3726ab163f5fSMatt Jacob 			}
3727ab163f5fSMatt Jacob 
3728ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3729ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3730ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3731ab163f5fSMatt Jacob 				else
3732ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3733ab163f5fSMatt Jacob 			}
3734ab163f5fSMatt Jacob 
3735ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3736ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3737ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3738ab163f5fSMatt Jacob 				else
3739ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3740ab163f5fSMatt Jacob 			}
3741ab163f5fSMatt Jacob 
3742ab163f5fSMatt Jacob 			/*
3743ab163f5fSMatt Jacob 			 * XXX: FIX ME
3744ab163f5fSMatt Jacob 			 */
37452df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3746ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
37479ce9bdafSMatt Jacob 				/*
37489ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
37499ce9bdafSMatt Jacob 				 */
37502df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
37512df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3752ab163f5fSMatt Jacob 			} else {
3753ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3754ab163f5fSMatt Jacob 			}
3755123055f0SNathan 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,
37562df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3757478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
37582df76c16SMatt Jacob 			sdp->update = 1;
3759478f8a96SJustin T. Gibbs 		}
3760478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3761478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3762478f8a96SJustin T. Gibbs 		break;
3763478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3764478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3765478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
37662df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3767ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
37682df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
37692df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
37702df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3771478f8a96SJustin T. Gibbs 
3772ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3773ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3774ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3775ab163f5fSMatt Jacob 			cts->transport_version = 0;
3776ab163f5fSMatt Jacob 
37779b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
37789b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3779ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3780ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3781fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3782766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3783766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3784ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3785ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3786ab163f5fSMatt Jacob 				fc->port = lp->portid;
37872df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3788ab163f5fSMatt Jacob 			}
3789ab163f5fSMatt Jacob 		} else {
37902df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
37912df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
37922df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
37931dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3794ab163f5fSMatt Jacob 
3795ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
379683ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
37972df76c16SMatt Jacob 				sdp->update = 1;
37982df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
37999ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
38009ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
38019ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
38024394c92fSMatt Jacob 			} else {
38039ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
38049ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
38059ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
38064394c92fSMatt Jacob 			}
3807478f8a96SJustin T. Gibbs 
3808ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3809ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3810ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3811ab163f5fSMatt Jacob 			cts->transport_version = 2;
3812ab163f5fSMatt Jacob 
3813b61386a4SMatt Jacob 			spi->valid = 0;
3814b61386a4SMatt Jacob 			scsi->valid = 0;
3815b61386a4SMatt Jacob 			spi->flags = 0;
3816b61386a4SMatt Jacob 			scsi->flags = 0;
3817ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3818ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3819ab163f5fSMatt Jacob 			}
38209ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3821ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3822ab163f5fSMatt Jacob 				spi->sync_period = pval;
3823b61386a4SMatt Jacob 			} else {
3824b61386a4SMatt Jacob 				spi->sync_offset = 0;
3825b61386a4SMatt Jacob 				spi->sync_period = 0;
3826b61386a4SMatt Jacob 			}
3827ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3828ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3829ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3830ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3831ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3832ab163f5fSMatt Jacob 			} else {
3833ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3834ab163f5fSMatt Jacob 			}
3835ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3836ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3837b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3838b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3839b61386a4SMatt Jacob 				}
3840ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3841ab163f5fSMatt Jacob 			}
3842123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3843123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3844478f8a96SJustin T. Gibbs 		}
3845478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3846478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3847478f8a96SJustin T. Gibbs 		break;
3848478f8a96SJustin T. Gibbs 
3849478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
38502df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
38512df76c16SMatt Jacob 		xpt_done(ccb);
38522df76c16SMatt Jacob 		break;
3853478f8a96SJustin T. Gibbs 
3854478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
3855ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
38562df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
38572df76c16SMatt Jacob 		if (error) {
3858478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
38592df76c16SMatt Jacob 			xpt_done(ccb);
38602df76c16SMatt Jacob 			break;
38612df76c16SMatt Jacob 		}
38620a70657fSMatt Jacob 		if (bootverbose) {
38632df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
38640a70657fSMatt Jacob 		}
38652df76c16SMatt Jacob 		if (IS_FC(isp)) {
38662df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
38672df76c16SMatt Jacob 		} else {
38682df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
38692df76c16SMatt Jacob 		}
3870478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3871478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3872478f8a96SJustin T. Gibbs 		break;
3873478f8a96SJustin T. Gibbs 
3874478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3875478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3876478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3877478f8a96SJustin T. Gibbs 		break;
3878478f8a96SJustin T. Gibbs 
38792df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
38802df76c16SMatt Jacob 	{
38812df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
38822df76c16SMatt Jacob 		fcparam *fcp;
38832df76c16SMatt Jacob 
38842df76c16SMatt Jacob 		if (!IS_FC(isp)) {
38852df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
38862df76c16SMatt Jacob 			xpt_done(ccb);
38872df76c16SMatt Jacob 			break;
38882df76c16SMatt Jacob 		}
38892df76c16SMatt Jacob 
38902df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
38912df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
38922df76c16SMatt Jacob 
38932df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
38942df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
38952df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
38962df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
38972df76c16SMatt Jacob 		}
38982df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
38992df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
39002df76c16SMatt Jacob 			int rchange = 0;
39012df76c16SMatt Jacob 			int newrole = 0;
39022df76c16SMatt Jacob 
39032df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
39042df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
39052df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
39062df76c16SMatt Jacob 					rchange = 1;
39072df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
39082df76c16SMatt Jacob 				}
39092df76c16SMatt Jacob 				break;
39102df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
39112df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
39122df76c16SMatt Jacob 					rchange = 1;
39132df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
39142df76c16SMatt Jacob 				}
39152df76c16SMatt Jacob 				break;
39162df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
39172df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
39182df76c16SMatt Jacob 					rchange = 1;
39192df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
39202df76c16SMatt Jacob 				}
39212df76c16SMatt Jacob 				break;
39222df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
39232df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
39242df76c16SMatt Jacob 					rchange = 1;
39252df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
39262df76c16SMatt Jacob 				}
39272df76c16SMatt Jacob 				break;
39282df76c16SMatt Jacob 			}
39292df76c16SMatt Jacob 			if (rchange) {
3930e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
39316bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
39326bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
39332df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3934387d8239SMatt Jacob 					xpt_done(ccb);
3935387d8239SMatt Jacob 					break;
39362df76c16SMatt Jacob 				}
39372df76c16SMatt Jacob 			}
39382df76c16SMatt Jacob 		}
39392df76c16SMatt Jacob 		xpt_done(ccb);
39402df76c16SMatt Jacob 		break;
39412df76c16SMatt Jacob 	}
3942a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3943e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
39442df76c16SMatt Jacob 	{
39452df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
39462df76c16SMatt Jacob 
39472df76c16SMatt Jacob 		if (IS_FC(isp)) {
39482df76c16SMatt Jacob 			fcparam *fcp;
39492df76c16SMatt Jacob 
39502df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
39512df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
39522df76c16SMatt Jacob 
39532df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
39542df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
39552df76c16SMatt Jacob 			switch (fcp->role) {
39562df76c16SMatt Jacob 			case ISP_ROLE_NONE:
39572df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
39582df76c16SMatt Jacob 				break;
39592df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
39602df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
39612df76c16SMatt Jacob 				break;
39622df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
39632df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
39642df76c16SMatt Jacob 				break;
39652df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
39662df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
39672df76c16SMatt Jacob 				break;
39682df76c16SMatt Jacob 			}
39692df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
39702df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
39712df76c16SMatt Jacob 		} else {
39722df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
39732df76c16SMatt Jacob 		}
39742df76c16SMatt Jacob 		xpt_done(ccb);
39752df76c16SMatt Jacob 		break;
39762df76c16SMatt Jacob 	}
3977478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
3978478f8a96SJustin T. Gibbs 	{
3979478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
3980478f8a96SJustin T. Gibbs 
3981478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
3982d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
39833e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3984a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
39853e6deb33SAlexander Motin 		else
3986d81ba9d5SMatt Jacob #endif
39873e6deb33SAlexander Motin 			cpi->target_sprt = 0;
3988478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
39890470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
39906af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
39916af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
39920470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
39937bf825d1SKenneth D. Merry 		if (isp->isp_osinfo.sixtyfourbit)
39947bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
39957bf825d1SKenneth D. Merry 		else
39967bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
39977bf825d1SKenneth D. Merry 
39982df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
39994394c92fSMatt Jacob 		if (IS_FC(isp)) {
40002df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
40012df76c16SMatt Jacob 
4002ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
40036af11b82SAlexander Motin #if __FreeBSD_version >= 1000700
40046af11b82SAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS;
40056af11b82SAlexander Motin #endif
4006c07b9e07SAlexander Motin #if __FreeBSD_version >= 1000039
4007c07b9e07SAlexander Motin 			cpi->hba_misc |= PIM_NOSCAN;
4008c07b9e07SAlexander Motin #endif
40092df76c16SMatt Jacob 
40100470d791SMatt Jacob 			/*
40110470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
40120470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
40130470d791SMatt Jacob 			 */
40140470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
40150470d791SMatt Jacob 
40169deea857SKenneth D. Merry 			/*
40172df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
40189deea857SKenneth D. Merry 			 */
4019e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
4020e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
4021e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
40222df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
40232df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
40242df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
40252df76c16SMatt Jacob 			} else {
40262df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
40272df76c16SMatt Jacob 			}
40280470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
4029ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
4030805e1f82SMatt Jacob 			cpi->transport_version = 0;
40312df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
40322df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
40332df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
40342df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4035478f8a96SJustin T. Gibbs 		} else {
40362df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
40370470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4038ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
4039ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
40409deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
4041ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
4042805e1f82SMatt Jacob 			cpi->transport_version = 2;
4043478f8a96SJustin T. Gibbs 		}
4044ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
4045ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
40464195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
40474195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
40484195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4049478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
4050478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
4051478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4052478f8a96SJustin T. Gibbs 		break;
4053478f8a96SJustin T. Gibbs 	}
4054478f8a96SJustin T. Gibbs 	default:
4055478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
4056478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4057478f8a96SJustin T. Gibbs 		break;
4058478f8a96SJustin T. Gibbs 	}
4059478f8a96SJustin T. Gibbs }
4060d3a9eb2eSMatt Jacob 
4061d3a9eb2eSMatt Jacob #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4062f7c631bcSMatt Jacob 
4063d3a9eb2eSMatt Jacob void
40642df76c16SMatt Jacob isp_done(XS_T *sccb)
4065d3a9eb2eSMatt Jacob {
40669cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
4067e95725cbSMatt Jacob 	uint32_t status;
4068d3a9eb2eSMatt Jacob 
4069d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
4070d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
4071b85389e1SMatt Jacob 
40722df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4073d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
40742df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
407592a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
407692a1e549SMatt Jacob 		} else {
4077d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4078d3a9eb2eSMatt Jacob 		}
407992a1e549SMatt Jacob 	}
4080b85389e1SMatt Jacob 
40810470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4082e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
4083e95725cbSMatt Jacob 	if (status != CAM_REQ_CMP) {
4084e95725cbSMatt Jacob 		if (status != CAM_SEL_TIMEOUT)
40856af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
40866af11b82SAlexander Motin 			    "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
40876af11b82SAlexander Motin 			    XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
40886af11b82SAlexander Motin 			    sccb->ccb_h.status, sccb->scsi_status);
40895704e6f0SKenneth D. Merry 		else if ((IS_FC(isp))
40905704e6f0SKenneth D. Merry 		      && (XS_TGT(sccb) < MAX_FC_TARG)) {
40915704e6f0SKenneth D. Merry 			fcparam *fcp;
40925704e6f0SKenneth D. Merry 
40935704e6f0SKenneth D. Merry 			fcp = FCPARAM(isp, XS_CHANNEL(sccb));
4094e68eef14SAlexander Motin 			fcp->portdb[XS_TGT(sccb)].is_target = 0;
40955704e6f0SKenneth D. Merry 		}
4096d3a9eb2eSMatt Jacob 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4097d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
40980470d791SMatt Jacob 			xpt_freeze_devq(sccb->ccb_h.path, 1);
4099d3a9eb2eSMatt Jacob 		}
4100d3a9eb2eSMatt Jacob 	}
4101b85389e1SMatt Jacob 
41022df76c16SMatt Jacob 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
41032df76c16SMatt Jacob 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4104d3a9eb2eSMatt Jacob 	}
4105b85389e1SMatt Jacob 
41065cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
4107387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
41080ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
41090a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
41105cbe3e8eSAlexander Motin 	}
4111d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
4112d3a9eb2eSMatt Jacob }
4113d3a9eb2eSMatt Jacob 
41142df76c16SMatt Jacob void
41152df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4116cbf57b47SMatt Jacob {
4117e68eef14SAlexander Motin 	int bus;
4118e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
4119387d8239SMatt Jacob 	char buf[64];
4120f7c631bcSMatt Jacob 	char *msg = NULL;
412110365e5aSMatt Jacob 	target_id_t tgt;
412210365e5aSMatt Jacob 	fcportdb_t *lp;
4123a01f5aebSMatt Jacob 	struct isp_fc *fc;
412410365e5aSMatt Jacob 	struct cam_path *tmppath;
4125e68eef14SAlexander Motin 	struct ac_contract ac;
4126e68eef14SAlexander Motin 	struct ac_device_changed *adc;
41272df76c16SMatt Jacob 	va_list ap;
412810365e5aSMatt Jacob 
4129cbf57b47SMatt Jacob 	switch (cmd) {
4130cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
41310470d791SMatt Jacob 	{
4132ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
4133ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
4134cbf57b47SMatt Jacob 		int flags, tgt;
41352df76c16SMatt Jacob 		sdparam *sdp;
4136ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
4137cbf57b47SMatt Jacob 
413829f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
4139ab163f5fSMatt Jacob 
41402df76c16SMatt Jacob 		va_start(ap, cmd);
41412df76c16SMatt Jacob 		bus = va_arg(ap, int);
41422df76c16SMatt Jacob 		tgt = va_arg(ap, int);
41432df76c16SMatt Jacob 		va_end(ap);
41442df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
41452df76c16SMatt Jacob 
41462df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
41472df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4148cbf57b47SMatt Jacob 			break;
4149cbf57b47SMatt Jacob 		}
41509ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
4151ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4152ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
4153ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
4154ab163f5fSMatt Jacob 
4155ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
4156ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
4157ab163f5fSMatt Jacob 
4158ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
4159ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
4160ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4161ab163f5fSMatt Jacob 		}
4162ab163f5fSMatt Jacob 
4163cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
4164ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
4165ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4166ab163f5fSMatt Jacob 		}
4167ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4168ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
4169ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4170ab163f5fSMatt Jacob 		} else {
4171ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4172ab163f5fSMatt Jacob 		}
4173ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
4174ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4175ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
41769ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
41779ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4178ab163f5fSMatt Jacob 		}
41792df76c16SMatt 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);
4180ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4181ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4182cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
4183cbf57b47SMatt Jacob 		break;
41840470d791SMatt Jacob 	}
418557c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
41862df76c16SMatt Jacob 	{
41872df76c16SMatt Jacob 		va_start(ap, cmd);
41882df76c16SMatt Jacob 		bus = va_arg(ap, int);
41892df76c16SMatt Jacob 		va_end(ap);
41902df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
41912df76c16SMatt Jacob 		if (IS_FC(isp)) {
41922df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
41932df76c16SMatt Jacob 		} else {
41942df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
419557c801f5SMatt Jacob 		}
419657c801f5SMatt Jacob 		break;
41972df76c16SMatt Jacob 	}
41985d571944SMatt Jacob 	case ISPASYNC_LIP:
419937a7daacSAlexander Motin 		if (msg == NULL)
4200f7c631bcSMatt Jacob 			msg = "LIP Received";
4201f7c631bcSMatt Jacob 		/* FALLTHROUGH */
42025d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
420337a7daacSAlexander Motin 		if (msg == NULL)
4204f7c631bcSMatt Jacob 			msg = "LOOP Reset";
4205f7c631bcSMatt Jacob 		/* FALLTHROUGH */
420657c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
420737a7daacSAlexander Motin 		if (msg == NULL)
4208f7c631bcSMatt Jacob 			msg = "LOOP Down";
42092df76c16SMatt Jacob 		va_start(ap, cmd);
42102df76c16SMatt Jacob 		bus = va_arg(ap, int);
42112df76c16SMatt Jacob 		va_end(ap);
421221daf914SAlexander Motin 		isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
4213e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
4214e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
421557c801f5SMatt Jacob 		break;
421657c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
42172df76c16SMatt Jacob 		va_start(ap, cmd);
42182df76c16SMatt Jacob 		bus = va_arg(ap, int);
42192df76c16SMatt Jacob 		va_end(ap);
4220e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
42212df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
422257c801f5SMatt Jacob 		break;
422310365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
42242df76c16SMatt Jacob 		va_start(ap, cmd);
42252df76c16SMatt Jacob 		bus = va_arg(ap, int);
42262df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
42272df76c16SMatt Jacob 		va_end(ap);
4228a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4229e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4230387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4231e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4232e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4233e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4234e68eef14SAlexander Motin 			lp->is_target = 1;
423521daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
42365704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
4237e68eef14SAlexander Motin 		}
4238e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4239e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4240e68eef14SAlexander Motin 			lp->is_initiator = 1;
4241e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4242e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4243e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4244e68eef14SAlexander Motin 			adc->port = lp->portid;
4245d4f3ad3aSAlexander Motin 			adc->target = tgt;
4246e68eef14SAlexander Motin 			adc->arrived = 1;
4247e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
424810365e5aSMatt Jacob 		}
424910365e5aSMatt Jacob 		break;
425010365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
42512df76c16SMatt Jacob 		va_start(ap, cmd);
42522df76c16SMatt Jacob 		bus = va_arg(ap, int);
42532df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
42542df76c16SMatt Jacob 		va_end(ap);
4255a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4256e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4257e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4258e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4259e68eef14SAlexander Motin changed:
4260e68eef14SAlexander Motin 		if (lp->is_target !=
4261e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4262e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4263e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
4264e68eef14SAlexander Motin 			if (lp->is_target) {
426521daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4266e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
426710365e5aSMatt Jacob 			} else {
4268e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
426921daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
427010365e5aSMatt Jacob 			}
4271f7c631bcSMatt Jacob 		}
4272e68eef14SAlexander Motin 		if (lp->is_initiator !=
4273e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4274e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4275e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
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 = lp->is_initiator;
4282e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
4283e68eef14SAlexander Motin 		}
428410365e5aSMatt Jacob 		break;
428510365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
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);
4290fb4a4356SKenneth D. Merry 		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->prli_word3);
4293e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4294e68eef14SAlexander Motin 		goto changed;
429510365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
42962df76c16SMatt Jacob 		va_start(ap, cmd);
42972df76c16SMatt Jacob 		bus = va_arg(ap, int);
42982df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
42992df76c16SMatt Jacob 		va_end(ap);
4300a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4301e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4302f7c631bcSMatt Jacob 		/*
4303e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
4304e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
4305f7c631bcSMatt Jacob 		 */
4306387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4307e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
4308f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
4309e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
4310e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4311a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
4312387d8239SMatt 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);
4313a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
4314f7c631bcSMatt Jacob 			}
4315e68eef14SAlexander Motin 			break;
43164b9d588eSMatt Jacob 		}
4317e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
431810365e5aSMatt Jacob 		break;
431910365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
432010365e5aSMatt Jacob 	{
432110365e5aSMatt Jacob 		char *msg;
4322eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
43232df76c16SMatt Jacob 
43242df76c16SMatt Jacob 		va_start(ap, cmd);
43252df76c16SMatt Jacob 		bus = va_arg(ap, int);
43262df76c16SMatt Jacob 		evt = va_arg(ap, int);
4327eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
43282df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
43292df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
43302df76c16SMatt Jacob 			reason = va_arg(ap, int);
4331eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4332eea52482SAlexander Motin 			portid = va_arg(ap, int);
433310365e5aSMatt Jacob 		} else {
43342df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
43352df76c16SMatt Jacob 			nlstate = reason = 0;
433610365e5aSMatt Jacob 		}
43372df76c16SMatt Jacob 		va_end(ap);
4338a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
43392df76c16SMatt Jacob 
43402df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
4341b5d5037bSAlexander Motin 			msg = "Port Database Changed";
4342eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
4343eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4344eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
43452df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4346b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
4347eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4348eea52482SAlexander Motin 			    bus, msg, portid);
43492df76c16SMatt Jacob 		} else {
4350b5d5037bSAlexander Motin 			msg = "Other Change Notify";
4351eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
43522df76c16SMatt Jacob 		}
4353e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
435457c801f5SMatt Jacob 		break;
435502ab3379SMatt Jacob 	}
4356d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4357e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
4358d81ba9d5SMatt Jacob 	{
43592df76c16SMatt Jacob 		isp_notify_t *notify;
43602df76c16SMatt Jacob 		va_start(ap, cmd);
43612df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
43622df76c16SMatt Jacob 		va_end(ap);
43632df76c16SMatt Jacob 		switch (notify->nt_ncode) {
43642df76c16SMatt Jacob 		case NT_ABORT_TASK:
43652df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
43662df76c16SMatt Jacob 		case NT_CLEAR_ACA:
43672df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
43682df76c16SMatt Jacob 		case NT_LUN_RESET:
43692df76c16SMatt Jacob 		case NT_TARGET_RESET:
4370c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
4371c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
43722df76c16SMatt Jacob 			/*
43732df76c16SMatt Jacob 			 * These are task management functions.
43742df76c16SMatt Jacob 			 */
43752df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
43762df76c16SMatt Jacob 			break;
43772df76c16SMatt Jacob 		case NT_BUS_RESET:
43782df76c16SMatt Jacob 		case NT_LIP_RESET:
43792df76c16SMatt Jacob 		case NT_LINK_UP:
43802df76c16SMatt Jacob 		case NT_LINK_DOWN:
4381e68eef14SAlexander Motin 		case NT_HBA_RESET:
43822df76c16SMatt Jacob 			/*
43832df76c16SMatt Jacob 			 * No action need be taken here.
43842df76c16SMatt Jacob 			 */
43852df76c16SMatt Jacob 			break;
4386387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
43872df76c16SMatt Jacob 		case NT_LOGOUT:
43882df76c16SMatt Jacob 			/*
43892df76c16SMatt Jacob 			 * This is device arrival/departure notification
43902df76c16SMatt Jacob 			 */
439196b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
43922df76c16SMatt Jacob 			break;
43932df76c16SMatt Jacob 		default:
43942df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
439596b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
43962df76c16SMatt Jacob 			break;
43972df76c16SMatt Jacob 		}
4398d81ba9d5SMatt Jacob 		break;
4399d81ba9d5SMatt Jacob 	}
4400387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
4401387d8239SMatt Jacob 	{
4402387d8239SMatt Jacob 		void *inot;
4403387d8239SMatt Jacob 		va_start(ap, cmd);
4404387d8239SMatt Jacob 		inot = va_arg(ap, void *);
4405387d8239SMatt Jacob 		va_end(ap);
4406387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
4407387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4408387d8239SMatt Jacob 			if (tp) {
4409387d8239SMatt Jacob 				tp->isp = isp;
4410387d8239SMatt Jacob 				if (inot) {
4411387d8239SMatt Jacob 					memcpy(tp->data, inot, sizeof (tp->data));
4412387d8239SMatt Jacob 					tp->not = tp->data;
4413387d8239SMatt Jacob 				} else {
4414387d8239SMatt Jacob 					tp->not = NULL;
4415387d8239SMatt Jacob 				}
44162a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
44172a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
44182a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
4419387d8239SMatt Jacob 			} else {
4420387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4421387d8239SMatt Jacob 			}
4422387d8239SMatt Jacob 		}
4423387d8239SMatt Jacob 		break;
4424387d8239SMatt Jacob 	}
4425d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
44262df76c16SMatt Jacob 	{
44272df76c16SMatt Jacob 		isphdr_t *hp;
44282df76c16SMatt Jacob 
44292df76c16SMatt Jacob 		va_start(ap, cmd);
44302df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
44312df76c16SMatt Jacob 		va_end(ap);
44322df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
4433cbf57b47SMatt Jacob 		default:
44342df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4435d81ba9d5SMatt Jacob 			break;
4436570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
44373e6deb33SAlexander Motin 			if (IS_24XX(isp)) {
44382df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4439570c7a3fSMatt Jacob 			} else {
44402df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4441570c7a3fSMatt Jacob 			}
4442570c7a3fSMatt Jacob 			break;
4443d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
44442df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4445d81ba9d5SMatt Jacob 			break;
4446d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
44472df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4448d81ba9d5SMatt Jacob 			break;
44492df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
4450d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
4451d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
4452d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
44532df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
4454d81ba9d5SMatt Jacob 			break;
44552df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
44562df76c16SMatt Jacob 		{
44572df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
44582df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
44592df76c16SMatt Jacob 			tstate_t *tptr;
44602df76c16SMatt Jacob 			fcportdb_t *lp;
44612df76c16SMatt Jacob 			uint16_t chan;
44622df76c16SMatt Jacob 			uint32_t sid, did;
44632df76c16SMatt Jacob 
44642df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
44652df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
44662df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
44672df76c16SMatt Jacob 
44682df76c16SMatt Jacob 			nt->nt_hba = isp;
44692df76c16SMatt Jacob 			nt->nt_did = did;
44702df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
44712df76c16SMatt Jacob 			nt->nt_sid = sid;
44722df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
44732df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
44742df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
44752df76c16SMatt Jacob 			} else {
44762df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4477e68eef14SAlexander Motin 				if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
44782df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
44792df76c16SMatt Jacob 				} else {
44802df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
44812df76c16SMatt Jacob 				}
44822df76c16SMatt Jacob 			}
44832df76c16SMatt Jacob 			/*
44842df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
44852df76c16SMatt Jacob 			 */
44862df76c16SMatt Jacob 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
44872df76c16SMatt Jacob 			if (tptr) {
4488315a4d6fSAlexander Motin 				nt->nt_lun = tptr->ts_lun;
44892df76c16SMatt Jacob 				rls_lun_statep(isp, tptr);
44902df76c16SMatt Jacob 			} else {
44912df76c16SMatt Jacob 				nt->nt_lun = LUN_ANY;
44922df76c16SMatt Jacob 			}
44932df76c16SMatt Jacob 			nt->nt_need_ack = 1;
44942df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
44952df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
44962df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
44972df76c16SMatt 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)",
44982df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
44992df76c16SMatt Jacob 			} else {
45002df76c16SMatt 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)",
45012df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
45022df76c16SMatt Jacob 			}
45032df76c16SMatt Jacob 			nt->nt_channel = chan;
45042df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
45052df76c16SMatt Jacob 			nt->nt_lreserved = hp;
45062df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
45072df76c16SMatt Jacob 			break;
45082df76c16SMatt Jacob 		}
4509d81ba9d5SMatt Jacob 		}
4510d81ba9d5SMatt Jacob 		break;
45112df76c16SMatt Jacob 	}
4512d81ba9d5SMatt Jacob #endif
4513ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
4514ab163f5fSMatt Jacob 	{
45151dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
4516ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
4517ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
4518ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
4519ab163f5fSMatt Jacob 		} else {
4520ab163f5fSMatt Jacob 			mbox6 = 0;
4521ab163f5fSMatt Jacob 		}
45222df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
45230a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
45240a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
45252df76c16SMatt Jacob 		isp_reinit(isp, 1);
45260a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
45270a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4528ab163f5fSMatt Jacob 		break;
4529ab163f5fSMatt Jacob 	}
4530d81ba9d5SMatt Jacob 	default:
4531b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4532cbf57b47SMatt Jacob 		break;
4533cbf57b47SMatt Jacob 	}
4534cbf57b47SMatt Jacob }
4535cbf57b47SMatt Jacob 
453692718a7fSMatt Jacob 
453792718a7fSMatt Jacob /*
453892718a7fSMatt Jacob  * Locks are held before coming here.
453992718a7fSMatt Jacob  */
454092718a7fSMatt Jacob void
45419cd7268eSMatt Jacob isp_uninit(ispsoftc_t *isp)
454292718a7fSMatt Jacob {
454310365e5aSMatt Jacob 	if (IS_24XX(isp)) {
454410365e5aSMatt Jacob 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
454510365e5aSMatt Jacob 	} else {
4546ea6f23cdSMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
454710365e5aSMatt Jacob 	}
454810365e5aSMatt Jacob 	ISP_DISABLE_INTS(isp);
454992718a7fSMatt Jacob }
4550b09b0095SMatt Jacob 
45512df76c16SMatt Jacob uint64_t
45522df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
45532df76c16SMatt Jacob {
45542df76c16SMatt Jacob 	uint64_t seed;
45552df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
45562df76c16SMatt Jacob 
4557a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4558a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4559a193dc4bSAlexander Motin 	if (seed)
4560a193dc4bSAlexander Motin 		return (seed);
4561a193dc4bSAlexander Motin 
4562a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
45632df76c16SMatt Jacob 	if (isactive) {
4564a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4565a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4566a193dc4bSAlexander Motin 		if (seed)
45672df76c16SMatt Jacob 			return (seed);
45682df76c16SMatt Jacob 	}
4569a193dc4bSAlexander Motin 
4570a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4571a193dc4bSAlexander Motin 	if (chan > 0) {
4572a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4573a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4574a193dc4bSAlexander Motin 		if (seed == 0) {
4575a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4576a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
45775cc3786cSMatt Jacob 		}
45782df76c16SMatt Jacob 	}
45792df76c16SMatt Jacob 
4580a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4581a193dc4bSAlexander Motin 	if (seed == 0) {
4582a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4583a193dc4bSAlexander Motin 		if (!iswwnn)
4584a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4585a193dc4bSAlexander Motin 	}
45862df76c16SMatt Jacob 
4587a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4588a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
45892df76c16SMatt Jacob 		/*
45902df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
45912df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4592a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
45932df76c16SMatt Jacob 		 */
4594a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4595a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4596a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
45972df76c16SMatt Jacob 	}
45982df76c16SMatt Jacob 	return (seed);
45992df76c16SMatt Jacob }
46002df76c16SMatt Jacob 
4601b09b0095SMatt Jacob void
46029cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4603b09b0095SMatt Jacob {
4604de461933SMatt Jacob 	int loc;
4605387d8239SMatt Jacob 	char lbuf[200];
4606b09b0095SMatt Jacob 	va_list ap;
4607de461933SMatt Jacob 
4608b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4609b09b0095SMatt Jacob 		return;
4610b09b0095SMatt Jacob 	}
4611387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4612de461933SMatt Jacob 	loc = strlen(lbuf);
4613b09b0095SMatt Jacob 	va_start(ap, fmt);
4614de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4615b09b0095SMatt Jacob 	va_end(ap);
4616de461933SMatt Jacob 	printf("%s\n", lbuf);
4617b09b0095SMatt Jacob }
4618f7c631bcSMatt Jacob 
4619670508b1SMatt Jacob void
4620670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4621670508b1SMatt Jacob {
4622670508b1SMatt Jacob 	va_list ap;
4623670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4624670508b1SMatt Jacob 		return;
4625670508b1SMatt Jacob 	}
4626670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4627670508b1SMatt Jacob 	va_start(ap, fmt);
4628670508b1SMatt Jacob 	vprintf(fmt, ap);
4629670508b1SMatt Jacob 	va_end(ap);
4630670508b1SMatt Jacob 	printf("\n");
4631670508b1SMatt Jacob }
4632670508b1SMatt Jacob 
4633f7c631bcSMatt Jacob uint64_t
4634f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4635f7c631bcSMatt Jacob {
4636f7c631bcSMatt Jacob 	uint64_t elapsed;
4637f7c631bcSMatt Jacob 	struct timespec x = *b;
4638f7c631bcSMatt Jacob 	timespecsub(&x, a);
4639f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4640f7c631bcSMatt Jacob 	if (elapsed == 0)
4641f7c631bcSMatt Jacob 		elapsed++;
4642f7c631bcSMatt Jacob 	return (elapsed);
4643f7c631bcSMatt Jacob }
4644f7c631bcSMatt Jacob 
4645f7c631bcSMatt Jacob int
4646f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4647f7c631bcSMatt Jacob {
4648f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4649f7c631bcSMatt Jacob 		return (1);
4650f7c631bcSMatt Jacob 	} else {
4651f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4652f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4653f7c631bcSMatt Jacob 		return (0);
4654f7c631bcSMatt Jacob 	}
4655f7c631bcSMatt Jacob }
4656f7c631bcSMatt Jacob 
4657f7c631bcSMatt Jacob void
4658f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4659f7c631bcSMatt Jacob {
4660a4f3a2beSMatt Jacob 	unsigned int usecs = mbp->timeout;
4661a4f3a2beSMatt Jacob 	unsigned int max, olim, ilim;
4662f7c631bcSMatt Jacob 
4663f7c631bcSMatt Jacob 	if (usecs == 0) {
4664f7c631bcSMatt Jacob 		usecs = MBCMD_DEFAULT_TIMEOUT;
4665f7c631bcSMatt Jacob 	}
4666a4f3a2beSMatt Jacob 	max = isp->isp_mbxwrk0 + 1;
4667a4f3a2beSMatt Jacob 
4668f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4669a4f3a2beSMatt Jacob 		unsigned int ms = (usecs + 999) / 1000;
4670a4f3a2beSMatt Jacob 
4671f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4672f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
4673a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
46742df76c16SMatt Jacob 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
4675a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4676a4f3a2beSMatt Jacob 				break;
4677a4f3a2beSMatt Jacob 			}
4678a4f3a2beSMatt Jacob 		}
4679f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4680f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4681f7c631bcSMatt Jacob 	} else {
4682a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
4683a4f3a2beSMatt Jacob 			for (ilim = 0; ilim < usecs; ilim += 100) {
46846ce548a1SAlexander Motin 				uint16_t isr, sema, info;
4685f7c631bcSMatt Jacob 				if (isp->isp_osinfo.mboxcmd_done) {
4686f7c631bcSMatt Jacob 					break;
4687f7c631bcSMatt Jacob 				}
46886ce548a1SAlexander Motin 				if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
46896ce548a1SAlexander Motin 					isp_intr(isp, isr, sema, info);
4690f7c631bcSMatt Jacob 					if (isp->isp_osinfo.mboxcmd_done) {
4691f7c631bcSMatt Jacob 						break;
4692f7c631bcSMatt Jacob 					}
4693f7c631bcSMatt Jacob 				}
46942df76c16SMatt Jacob 				ISP_DELAY(100);
4695f7c631bcSMatt Jacob 			}
4696a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4697a4f3a2beSMatt Jacob 				break;
4698a4f3a2beSMatt Jacob 			}
4699a4f3a2beSMatt Jacob 		}
4700f7c631bcSMatt Jacob 	}
4701f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
47022df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
47032df76c16SMatt Jacob 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
4704f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4705f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4706f7c631bcSMatt Jacob 	}
4707f7c631bcSMatt Jacob }
4708f7c631bcSMatt Jacob 
4709f7c631bcSMatt Jacob void
4710f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4711f7c631bcSMatt Jacob {
4712f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleeping) {
4713f7c631bcSMatt Jacob 		wakeup(&isp->isp_mbxworkp);
4714f7c631bcSMatt Jacob 	}
4715f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
4716f7c631bcSMatt Jacob }
4717f7c631bcSMatt Jacob 
4718f7c631bcSMatt Jacob void
4719f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4720f7c631bcSMatt Jacob {
4721f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4722f7c631bcSMatt Jacob }
4723f7c631bcSMatt Jacob 
4724f7c631bcSMatt Jacob int
47252df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
47262df76c16SMatt Jacob {
47272df76c16SMatt Jacob 	int ret = 0;
47282df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
47292df76c16SMatt Jacob 		ret = -1;
47302df76c16SMatt Jacob 	} else {
47312df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
47322df76c16SMatt Jacob 	}
47332df76c16SMatt Jacob 	return (ret);
47342df76c16SMatt Jacob }
47352df76c16SMatt Jacob 
47362df76c16SMatt Jacob int
4737f7c631bcSMatt Jacob isp_mstohz(int ms)
4738f7c631bcSMatt Jacob {
4739a4f3a2beSMatt Jacob 	int hz;
4740f7c631bcSMatt Jacob 	struct timeval t;
4741f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
4742f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
4743a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
4744a4f3a2beSMatt Jacob 	if (hz < 0) {
4745a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
4746f7c631bcSMatt Jacob 	}
4747a4f3a2beSMatt Jacob 	if (hz == 0) {
4748a4f3a2beSMatt Jacob 		hz = 1;
4749a4f3a2beSMatt Jacob 	}
4750a4f3a2beSMatt Jacob 	return (hz);
4751f7c631bcSMatt Jacob }
47520a70657fSMatt Jacob 
47530a70657fSMatt Jacob void
47540a70657fSMatt Jacob isp_platform_intr(void *arg)
47550a70657fSMatt Jacob {
47560a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
47576ce548a1SAlexander Motin 	uint16_t isr, sema, info;
47580a70657fSMatt Jacob 
47590a70657fSMatt Jacob 	ISP_LOCK(isp);
47600a70657fSMatt Jacob 	isp->isp_intcnt++;
47616ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
47626ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
47636ce548a1SAlexander Motin 	else
47640a70657fSMatt Jacob 		isp->isp_intbogus++;
47650a70657fSMatt Jacob 	ISP_UNLOCK(isp);
47660a70657fSMatt Jacob }
47670a70657fSMatt Jacob 
47680a70657fSMatt Jacob void
47690a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
47700a70657fSMatt Jacob {
47710a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
47722df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
47730a70657fSMatt Jacob 	} else {
47742df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
47750a70657fSMatt Jacob 	}
47760a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
47770a70657fSMatt Jacob }
47782df76c16SMatt Jacob 
4779fb4a4356SKenneth D. Merry /*
4780fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4781fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4782fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4783fb4a4356SKenneth D. Merry  */
4784fb4a4356SKenneth D. Merry void
478521daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4786fb4a4356SKenneth D. Merry {
478721daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4788fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
478921daf914SAlexander Motin 	int i;
4790fb4a4356SKenneth D. Merry 
4791fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
479221daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
479321daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4794fb4a4356SKenneth D. Merry 	else
479521daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
479621daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4797fb4a4356SKenneth D. Merry 
4798fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
479921daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
480021daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4801fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4802fb4a4356SKenneth D. Merry 		}
4803fb4a4356SKenneth D. Merry 	}
4804fb4a4356SKenneth D. Merry }
4805fb4a4356SKenneth D. Merry 
4806387d8239SMatt Jacob int
4807387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4808387d8239SMatt Jacob {
48096af11b82SAlexander Motin 	lun_id_t lun;
48106af11b82SAlexander Motin 	uint32_t chan, tgt;
4811405b7a29SMatt Jacob 	struct isp_fc *fc;
4812405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4813405b7a29SMatt Jacob 	int idx;
4814405b7a29SMatt Jacob 
4815a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4816405b7a29SMatt Jacob 		return (0);
4817405b7a29SMatt Jacob 
4818405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4819405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4820405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4821405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4822405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4823405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4824387d8239SMatt Jacob 
4825387d8239SMatt Jacob 	while (nxp) {
4826387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4827387d8239SMatt Jacob 			break;
4828387d8239SMatt Jacob 		nxp = nxp->next;
4829387d8239SMatt Jacob 	}
4830387d8239SMatt Jacob 	if (nxp == NULL) {
4831387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4832387d8239SMatt Jacob 		if (nxp == NULL) {
4833387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4834387d8239SMatt Jacob 			if (nxp == NULL) {
4835387d8239SMatt Jacob 				return (-1);
4836387d8239SMatt Jacob 			}
4837387d8239SMatt Jacob 		} else {
4838387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4839387d8239SMatt Jacob 		}
4840387d8239SMatt Jacob 		nxp->tgt = tgt;
4841387d8239SMatt Jacob 		nxp->lun = lun;
4842387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4843387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4844387d8239SMatt Jacob 	}
4845387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4846387d8239SMatt Jacob 		nxp->crnseed = 1;
4847387d8239SMatt Jacob 	PISP_PCMD(cmd)->crn = nxp->crnseed;
4848387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4849387d8239SMatt Jacob 	return (0);
4850387d8239SMatt Jacob }
4851387d8239SMatt Jacob 
485294dff771SMatt Jacob /*
485394dff771SMatt Jacob  * We enter with the lock held
485494dff771SMatt Jacob  */
48552df76c16SMatt Jacob void
48562df76c16SMatt Jacob isp_timer(void *arg)
48572df76c16SMatt Jacob {
48582df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
48592df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
48602df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
48612df76c16SMatt Jacob #endif
486294dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
48632df76c16SMatt Jacob }
4864387d8239SMatt Jacob 
4865387d8239SMatt Jacob isp_ecmd_t *
4866387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4867387d8239SMatt Jacob {
4868387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4869387d8239SMatt Jacob 	if (ecmd) {
4870387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4871387d8239SMatt Jacob 	}
4872387d8239SMatt Jacob 	return (ecmd);
4873387d8239SMatt Jacob }
4874387d8239SMatt Jacob 
4875387d8239SMatt Jacob void
4876387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4877387d8239SMatt Jacob {
4878387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4879387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4880387d8239SMatt Jacob }
4881