xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 5773ac113c5c364ed1f0af12b97b2c3b058e1ee2)
1098ca2bdSWarner Losh /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4a94fab67SAlexander Motin  * Copyright (c) 2009-2017 Alexander Motin <mav@FreeBSD.org>
52df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
6e5265237SMatt Jacob  * All rights reserved.
76054c3f6SMatt Jacob  *
86054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
96054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
106054c3f6SMatt Jacob  * are met:
116054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
126054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
136054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
14aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
15aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
166054c3f6SMatt Jacob  *
176054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
216054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276054c3f6SMatt Jacob  * SUCH DAMAGE.
286054c3f6SMatt Jacob  */
29aad970f1SDavid E. O'Brien 
30799881e0SMatt Jacob /*
31799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
32799881e0SMatt Jacob  */
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
34aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
355b14cb41SWill Andrews 
366054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
375d571944SMatt Jacob #include <sys/unistd.h>
385d571944SMatt Jacob #include <sys/kthread.h>
395d571944SMatt Jacob #include <sys/conf.h>
404eb49427SMatt Jacob #include <sys/module.h>
415d571944SMatt Jacob #include <sys/ioccom.h>
425d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4370273f90SMatt Jacob #include <sys/devicestat.h>
44f7c631bcSMatt Jacob #include <cam/cam_periph.h>
456c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
466054c3f6SMatt Jacob 
474eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4856c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4973030e03SMatt Jacob int isp_announced = 0;
502df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
512df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
52f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
53e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5473030e03SMatt Jacob 
55e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
56e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
575d571944SMatt Jacob static d_ioctl_t ispioctl;
581dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
590470d791SMatt Jacob static void isp_poll(struct cam_sim *);
60*5773ac11SJohn Baldwin static callout_func_t isp_watchdog;
61*5773ac11SJohn Baldwin static callout_func_t isp_gdt;
62de461933SMatt Jacob static task_fn_t isp_gdt_task;
635d571944SMatt Jacob static void isp_kthread(void *);
64d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6594dff771SMatt Jacob static int isp_timer_count;
662df76c16SMatt Jacob static void isp_timer(void *);
67cc8df88bSMatt Jacob 
685d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
69dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
707ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
717ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
725d571944SMatt Jacob };
735d571944SMatt Jacob 
742df76c16SMatt Jacob static int
7507f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7607f56f1cSAlexander Motin {
7707f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7807f56f1cSAlexander Motin 	int chan = arg2;
7907f56f1cSAlexander Motin 	int error, old, value;
8007f56f1cSAlexander Motin 
8107f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8207f56f1cSAlexander Motin 
8307f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8407f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8507f56f1cSAlexander Motin 		return (error);
8607f56f1cSAlexander Motin 
8707f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8807f56f1cSAlexander Motin 		return (EINVAL);
8907f56f1cSAlexander Motin 
9007f56f1cSAlexander Motin 	ISP_LOCK(isp);
9107f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9207f56f1cSAlexander Motin 
93391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
94391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
95391f03daSAlexander Motin 
9607f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9707f56f1cSAlexander Motin 	if (value == old) {
9807f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9907f56f1cSAlexander Motin 		return (0);
10007f56f1cSAlexander Motin 	}
10107f56f1cSAlexander Motin 
10207f56f1cSAlexander Motin 	/* Actually change the role. */
1036bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10407f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10507f56f1cSAlexander Motin 	return (error);
10607f56f1cSAlexander Motin }
10707f56f1cSAlexander Motin 
10807f56f1cSAlexander Motin static int
1092df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
110478f8a96SJustin T. Gibbs {
111478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
112ea6f23cdSMatt Jacob 	struct cam_sim *sim;
113ea6f23cdSMatt Jacob 	struct cam_path *path;
1148290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
1158290ea90SAlexander Motin 	int i;
1168290ea90SAlexander Motin #endif
117478f8a96SJustin T. Gibbs 
11813d9c921SAlexander Motin 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
11913d9c921SAlexander Motin 	    device_get_unit(isp->isp_dev), &isp->isp_lock,
12013d9c921SAlexander Motin 	    isp->isp_maxcmds, isp->isp_maxcmds, devq);
12113d9c921SAlexander Motin 	if (sim == NULL)
1222df76c16SMatt Jacob 		return (ENOMEM);
1232df76c16SMatt Jacob 
1242df76c16SMatt Jacob 	ISP_LOCK(isp);
1252df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1262df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1272df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1282df76c16SMatt Jacob 		return (EIO);
1292df76c16SMatt Jacob 	}
1302df76c16SMatt Jacob 	ISP_UNLOCK(isp);
131227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1322df76c16SMatt Jacob 		ISP_LOCK(isp);
1332df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1342df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1352df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1362df76c16SMatt Jacob 		return (ENXIO);
1372df76c16SMatt Jacob 	}
1382df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1392df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1402df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1412df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1422df76c16SMatt Jacob 	csa.callback_arg = sim;
143387d8239SMatt Jacob 
144387d8239SMatt Jacob 	ISP_LOCK(isp);
1452df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
146387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1472df76c16SMatt Jacob 
1482df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1492df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1502df76c16SMatt Jacob 		spi->sim = sim;
1512df76c16SMatt Jacob 		spi->path = path;
1520d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
15309ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1548290ea90SAlexander Motin 		STAILQ_INIT(&spi->ntfree);
1558290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1568290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
1578290ea90SAlexander Motin 		LIST_INIT(&spi->atfree);
1588290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1598290ea90SAlexander Motin 			LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
1608290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1618290ea90SAlexander Motin 			LIST_INIT(&spi->atused[i]);
1620d965629SAlexander Motin #endif
1632df76c16SMatt Jacob 	} else {
164a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1652df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
16607f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
16707f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
16807f56f1cSAlexander Motin 		char name[16];
1692df76c16SMatt Jacob 
170a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1712df76c16SMatt Jacob 		fc->sim = sim;
1722df76c16SMatt Jacob 		fc->path = path;
1732df76c16SMatt Jacob 		fc->isp = isp;
174a01f5aebSMatt Jacob 		fc->ready = 1;
175a94fab67SAlexander Motin 		fcp->isp_use_gft_id = 1;
176a94fab67SAlexander Motin 		fcp->isp_use_gff_id = 1;
177de461933SMatt Jacob 
17813d9c921SAlexander Motin 		callout_init_mtx(&fc->gdt, &isp->isp_lock, 0);
179de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
1800d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
18109ddc7adSAlexander Motin 		TAILQ_INIT(&fc->waitq);
1828290ea90SAlexander Motin 		STAILQ_INIT(&fc->ntfree);
1838290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1848290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
1858290ea90SAlexander Motin 		LIST_INIT(&fc->atfree);
1868290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1878290ea90SAlexander Motin 			LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
1888290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1898290ea90SAlexander Motin 			LIST_INIT(&fc->atused[i]);
1900d965629SAlexander Motin #endif
191e561aa79SAlexander Motin 		isp_loop_changed(isp, chan);
1922df76c16SMatt Jacob 		ISP_UNLOCK(isp);
193069f5ef9SAlexander Motin 		if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
194069f5ef9SAlexander Motin 		    "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
195a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
196a01f5aebSMatt Jacob 			ISP_LOCK(isp);
197a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
198a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
199a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
200a01f5aebSMatt Jacob 			return (ENOMEM);
201a01f5aebSMatt Jacob 		}
202e596ff7aSAlexander Motin 		fc->num_threads += 1;
20307f56f1cSAlexander Motin 		if (chan > 0) {
20407f56f1cSAlexander Motin 			snprintf(name, sizeof(name), "chan%d", chan);
20507f56f1cSAlexander Motin 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
20607f56f1cSAlexander Motin 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
207a0ec8e99SMatt Jacob 		}
208e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
209e596ff7aSAlexander Motin 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
210e596ff7aSAlexander Motin 		    "World Wide Node Name");
211e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
212e596ff7aSAlexander Motin 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
213e596ff7aSAlexander Motin 		    "World Wide Port Name");
214e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
215e596ff7aSAlexander Motin 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
216e596ff7aSAlexander Motin 		    "Loop Down Limit");
217e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
218e596ff7aSAlexander Motin 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
219e596ff7aSAlexander Motin 		    "Gone Device Time");
22007f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
221e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
222e596ff7aSAlexander Motin 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
223e596ff7aSAlexander Motin 		    "Cause a Lost Frame on a Read");
22407f56f1cSAlexander Motin #endif
22507f56f1cSAlexander Motin 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
22607f56f1cSAlexander Motin 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
22707f56f1cSAlexander Motin 		    isp_role_sysctl, "I", "Current role");
228e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
229e596ff7aSAlexander Motin 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
230e596ff7aSAlexander Motin 		    "Connection speed in gigabits");
231e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
232e596ff7aSAlexander Motin 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
233e596ff7aSAlexander Motin 		    "Link state");
234e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
235e596ff7aSAlexander Motin 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
236e596ff7aSAlexander Motin 		    "Firmware state");
237e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
238e596ff7aSAlexander Motin 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
239e596ff7aSAlexander Motin 		    "Loop state");
240e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
241e596ff7aSAlexander Motin 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
242e596ff7aSAlexander Motin 		    "Connection topology");
243a94fab67SAlexander Motin 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
244a94fab67SAlexander Motin 		    "use_gft_id", CTLFLAG_RWTUN, &fcp->isp_use_gft_id, 0,
245a94fab67SAlexander Motin 		    "Use GFT_ID during fabric scan");
246a94fab67SAlexander Motin 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
247a94fab67SAlexander Motin 		    "use_gff_id", CTLFLAG_RWTUN, &fcp->isp_use_gff_id, 0,
248a94fab67SAlexander Motin 		    "Use GFF_ID during fabric scan");
2492df76c16SMatt Jacob 	}
2502df76c16SMatt Jacob 	return (0);
2512df76c16SMatt Jacob }
2522df76c16SMatt Jacob 
2531d0a1de2SWill Andrews static void
2541d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2551d0a1de2SWill Andrews {
2561d0a1de2SWill Andrews 	struct cam_sim *sim;
2571d0a1de2SWill Andrews 	struct cam_path *path;
2581d0a1de2SWill Andrews 	struct ccb_setasync csa;
2591d0a1de2SWill Andrews 	int *num_threads;
2601d0a1de2SWill Andrews 
2611d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2621d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2631d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2641d0a1de2SWill Andrews 
2651d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2661d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2671d0a1de2SWill Andrews 	csa.event_enable = 0;
2681d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2691d0a1de2SWill Andrews 	csa.callback_arg = sim;
2701d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2711d0a1de2SWill Andrews 	xpt_free_path(path);
2721d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2731d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2741d0a1de2SWill Andrews 
2751d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2761d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2771d0a1de2SWill Andrews 	while (*num_threads != 0)
27813d9c921SAlexander Motin 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "isp_reap", 100);
2791d0a1de2SWill Andrews }
2801d0a1de2SWill Andrews 
2812df76c16SMatt Jacob int
2822df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2832df76c16SMatt Jacob {
2842df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2852df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2862df76c16SMatt Jacob 	int chan;
2872df76c16SMatt Jacob 
288ea6f23cdSMatt Jacob 	/*
289ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
290478f8a96SJustin T. Gibbs 	 */
2912df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2922df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2932df76c16SMatt Jacob 		return (EIO);
294478f8a96SJustin T. Gibbs 	}
295478f8a96SJustin T. Gibbs 
2962df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2972df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2982df76c16SMatt Jacob 			goto unwind;
299ea6f23cdSMatt Jacob 		}
300ea6f23cdSMatt Jacob 	}
301ea6f23cdSMatt Jacob 
30213d9c921SAlexander Motin 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0);
30394dff771SMatt Jacob 	isp_timer_count = hz >> 2;
30494dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
305ea6f23cdSMatt Jacob 
3062df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
3072df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3082df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
309ea6f23cdSMatt Jacob 	}
3102df76c16SMatt Jacob 	return (0);
3115d571944SMatt Jacob 
3122df76c16SMatt Jacob unwind:
3132df76c16SMatt Jacob 	while (--chan >= 0) {
3142df76c16SMatt Jacob 		struct cam_sim *sim;
3152df76c16SMatt Jacob 		struct cam_path *path;
3161d0a1de2SWill Andrews 
3171d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3181d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3192df76c16SMatt Jacob 		xpt_free_path(path);
3200a70657fSMatt Jacob 		ISP_LOCK(isp);
3210a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3222df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3232df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3242df76c16SMatt Jacob 	}
3252df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3262df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3272df76c16SMatt Jacob 	return (-1);
3282df76c16SMatt Jacob }
3292df76c16SMatt Jacob 
330e95725cbSMatt Jacob int
3312df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3322df76c16SMatt Jacob {
3332df76c16SMatt Jacob 	int chan;
3342df76c16SMatt Jacob 
3352df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3362df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3372df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3382df76c16SMatt Jacob 	}
33998339da1SAlexander Motin 	ISP_LOCK(isp);
34098339da1SAlexander Motin 	/* Tell spawned threads that we're exiting. */
34198339da1SAlexander Motin 	isp->isp_osinfo.is_exiting = 1;
34298339da1SAlexander Motin 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
34398339da1SAlexander Motin 		isp_detach_chan(isp, chan);
34498339da1SAlexander Motin 	ISP_UNLOCK(isp);
34598339da1SAlexander Motin 	callout_drain(&isp->isp_osinfo.tmo);
3462df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
347e95725cbSMatt Jacob 	return (0);
3485d571944SMatt Jacob }
3495d571944SMatt Jacob 
350f7c631bcSMatt Jacob static void
351e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
352fdeb9f2fSMatt Jacob {
3532df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
354ce9f3237SAlexander Motin 
355ce9f3237SAlexander Motin 	if (fc->sim == NULL)
356ce9f3237SAlexander Motin 		return;
3572df76c16SMatt Jacob 	if (fc->simqfrozen == 0) {
358b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
359e561aa79SAlexander Motin 		    "Chan %d Freeze simq (loopdown)", chan);
3602df76c16SMatt Jacob 		fc->simqfrozen = SIMQFRZ_LOOPDOWN;
361c07b9e07SAlexander Motin 		xpt_hold_boot();
3622df76c16SMatt Jacob 		xpt_freeze_simq(fc->sim, 1);
363fdeb9f2fSMatt Jacob 	} else {
364b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
365e561aa79SAlexander Motin 		    "Chan %d Mark simq frozen (loopdown)", chan);
3662df76c16SMatt Jacob 		fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3672df76c16SMatt Jacob 	}
368fdeb9f2fSMatt Jacob }
369fdeb9f2fSMatt Jacob 
370e95725cbSMatt Jacob static void
371e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
372e95725cbSMatt Jacob {
373e95725cbSMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
374ce9f3237SAlexander Motin 
375ce9f3237SAlexander Motin 	if (fc->sim == NULL)
376ce9f3237SAlexander Motin 		return;
377e95725cbSMatt Jacob 	int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
378e95725cbSMatt Jacob 	fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
379e95725cbSMatt Jacob 	if (wasfrozen && fc->simqfrozen == 0) {
380e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
381e561aa79SAlexander Motin 		    "Chan %d Release simq", chan);
382e95725cbSMatt Jacob 		xpt_release_simq(fc->sim, 1);
383c07b9e07SAlexander Motin 		xpt_release_boot();
384e95725cbSMatt Jacob 	}
385e95725cbSMatt Jacob }
3869cd7268eSMatt Jacob 
3875d571944SMatt Jacob static int
3882df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
3895d571944SMatt Jacob {
3909cd7268eSMatt Jacob 	ispsoftc_t *isp;
3912df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
3925d571944SMatt Jacob 
3932df76c16SMatt Jacob 	isp = dev->si_drv1;
3945d571944SMatt Jacob 
395c1504bc0SMatt Jacob 	switch (c) {
3965d571944SMatt Jacob 	case ISP_SDBLEV:
3975d571944SMatt Jacob 	{
3985d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
3995d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
4005d571944SMatt Jacob 		*(int *)addr = olddblev;
4015d571944SMatt Jacob 		retval = 0;
4025d571944SMatt Jacob 		break;
4035d571944SMatt Jacob 	}
404746e9c85SMatt Jacob 	case ISP_GETROLE:
4052df76c16SMatt Jacob 		chan = *(int *)addr;
4062df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4072df76c16SMatt Jacob 			retval = -ENXIO;
4082df76c16SMatt Jacob 			break;
4092df76c16SMatt Jacob 		}
4102df76c16SMatt Jacob 		if (IS_FC(isp)) {
4112df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4122df76c16SMatt Jacob 		} else {
4133e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4142df76c16SMatt Jacob 		}
415746e9c85SMatt Jacob 		retval = 0;
416746e9c85SMatt Jacob 		break;
417746e9c85SMatt Jacob 	case ISP_SETROLE:
4183e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4193e6deb33SAlexander Motin 			break;
420746e9c85SMatt Jacob 		nr = *(int *)addr;
4212df76c16SMatt Jacob 		chan = nr >> 8;
4222df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4232df76c16SMatt Jacob 			retval = -ENXIO;
4242df76c16SMatt Jacob 			break;
4252df76c16SMatt Jacob 		}
4262df76c16SMatt Jacob 		nr &= 0xff;
427746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
428746e9c85SMatt Jacob 			retval = EINVAL;
429746e9c85SMatt Jacob 			break;
430746e9c85SMatt Jacob 		}
4312df76c16SMatt Jacob 		ISP_LOCK(isp);
4326bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4336bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4346bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4352df76c16SMatt Jacob 		retval = 0;
4362df76c16SMatt Jacob 		break;
4372df76c16SMatt Jacob 
4385d571944SMatt Jacob 	case ISP_RESETHBA:
439511ced9bSMatt Jacob 		ISP_LOCK(isp);
4402df76c16SMatt Jacob 		isp_reinit(isp, 0);
441511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4425d571944SMatt Jacob 		retval = 0;
4435d571944SMatt Jacob 		break;
4442df76c16SMatt Jacob 
445f553351eSMatt Jacob 	case ISP_RESCAN:
4465d571944SMatt Jacob 		if (IS_FC(isp)) {
4478037cdcdSBrooks Davis 			chan = *(intptr_t *)addr;
4482df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4492df76c16SMatt Jacob 				retval = -ENXIO;
4502df76c16SMatt Jacob 				break;
4512df76c16SMatt Jacob 			}
452511ced9bSMatt Jacob 			ISP_LOCK(isp);
453e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4545d571944SMatt Jacob 				retval = EIO;
4555d571944SMatt Jacob 			} else {
4565d571944SMatt Jacob 				retval = 0;
4575d571944SMatt Jacob 			}
458511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4595d571944SMatt Jacob 		}
4605d571944SMatt Jacob 		break;
4612df76c16SMatt Jacob 
4625d571944SMatt Jacob 	case ISP_FC_LIP:
4635d571944SMatt Jacob 		if (IS_FC(isp)) {
4648037cdcdSBrooks Davis 			chan = *(intptr_t *)addr;
4652df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4662df76c16SMatt Jacob 				retval = -ENXIO;
4672df76c16SMatt Jacob 				break;
4682df76c16SMatt Jacob 			}
469511ced9bSMatt Jacob 			ISP_LOCK(isp);
4702df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4715d571944SMatt Jacob 				retval = EIO;
4725d571944SMatt Jacob 			} else {
4735d571944SMatt Jacob 				retval = 0;
4745d571944SMatt Jacob 			}
475511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4765d571944SMatt Jacob 		}
4775d571944SMatt Jacob 		break;
4785d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4795d571944SMatt Jacob 	{
4805d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
48110365e5aSMatt Jacob 		fcportdb_t *lp;
4825d571944SMatt Jacob 
48302e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
48402e2b2d9SMatt Jacob 			break;
48502e2b2d9SMatt Jacob 		}
4862e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
4875d571944SMatt Jacob 			retval = EINVAL;
4885d571944SMatt Jacob 			break;
4895d571944SMatt Jacob 		}
4902df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
491e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
492387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
49310365e5aSMatt Jacob 			ifc->loopid = lp->handle;
4945d571944SMatt Jacob 			ifc->portid = lp->portid;
4955d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
4965d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
4975d571944SMatt Jacob 			retval = 0;
4985d571944SMatt Jacob 		} else {
4995d571944SMatt Jacob 			retval = ENODEV;
5005d571944SMatt Jacob 		}
5015d571944SMatt Jacob 		break;
5025d571944SMatt Jacob 	}
503570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
504570c7a3fSMatt Jacob 	{
505570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5062df76c16SMatt Jacob 		int chan = hba->fc_channel;
50702e2b2d9SMatt Jacob 
5082df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5092df76c16SMatt Jacob 			retval = ENXIO;
5102df76c16SMatt Jacob 			break;
5112df76c16SMatt Jacob 		}
512a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
513a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
514a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5152df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
51602e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5172df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5182df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5192df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5202df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5212df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5222df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5232df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5242df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5252df76c16SMatt Jacob 		} else {
5262df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5272df76c16SMatt Jacob 			hba->fc_speed = 0;
5282df76c16SMatt Jacob 			hba->fc_topology = 0;
5292df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5302df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5312df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5322df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
53302e2b2d9SMatt Jacob 		}
534570c7a3fSMatt Jacob 		retval = 0;
535570c7a3fSMatt Jacob 		break;
536570c7a3fSMatt Jacob 	}
5378e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5388e62a8acSMatt Jacob 	{
5398e62a8acSMatt Jacob 		int needmarker;
5408e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
541c5fd36edSAlexander Motin 		uint16_t nphdl;
5428e62a8acSMatt Jacob 		mbreg_t mbs;
5438e62a8acSMatt Jacob 
5448e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5458e62a8acSMatt Jacob 			break;
5468e62a8acSMatt Jacob 		}
5478e62a8acSMatt Jacob 
5482df76c16SMatt Jacob 		chan = fct->chan;
5492df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5502df76c16SMatt Jacob 			retval = -ENXIO;
5512df76c16SMatt Jacob 			break;
5522df76c16SMatt Jacob 		}
5532df76c16SMatt Jacob 
5548e62a8acSMatt Jacob 		needmarker = retval = 0;
555c5fd36edSAlexander Motin 		nphdl = fct->loopid;
5562df76c16SMatt Jacob 		ISP_LOCK(isp);
5572df76c16SMatt Jacob 		if (IS_24XX(isp)) {
55853791a95SAlexander Motin 			void *reqp;
55953791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
56053791a95SAlexander Motin 			isp24xx_tmf_t tmf;
56153791a95SAlexander Motin 			isp24xx_statusreq_t sp;
5622df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
5632df76c16SMatt Jacob 			fcportdb_t *lp;
5642df76c16SMatt Jacob 			int i;
5652df76c16SMatt Jacob 
5662df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
5672df76c16SMatt Jacob 				lp = &fcp->portdb[i];
568c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
5692df76c16SMatt Jacob 					break;
5702df76c16SMatt Jacob 				}
5712df76c16SMatt Jacob 			}
5722df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
5732df76c16SMatt Jacob 				retval = ENXIO;
5742df76c16SMatt Jacob 				ISP_UNLOCK(isp);
5752df76c16SMatt Jacob 				break;
5762df76c16SMatt Jacob 			}
57753791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
57853791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
57953791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
58053791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
58153791a95SAlexander Motin 			tmf.tmf_delay = 2;
58253791a95SAlexander Motin 			tmf.tmf_timeout = 4;
58353791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
58453791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
58553791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
58653791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
5872df76c16SMatt Jacob 			if (fct->lun >= 256) {
58853791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
5892df76c16SMatt Jacob 			}
5902df76c16SMatt Jacob 			switch (fct->action) {
5912df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
59253791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
5932df76c16SMatt Jacob 				break;
5942df76c16SMatt Jacob 			case IPT_TARGET_RESET:
59553791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
5962df76c16SMatt Jacob 				needmarker = 1;
5972df76c16SMatt Jacob 				break;
5982df76c16SMatt Jacob 			case IPT_LUN_RESET:
59953791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
6002df76c16SMatt Jacob 				needmarker = 1;
6012df76c16SMatt Jacob 				break;
6022df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
60353791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6042df76c16SMatt Jacob 				needmarker = 1;
6052df76c16SMatt Jacob 				break;
6062df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
60753791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6082df76c16SMatt Jacob 				needmarker = 1;
6092df76c16SMatt Jacob 				break;
6102df76c16SMatt Jacob 			default:
6112df76c16SMatt Jacob 				retval = EINVAL;
6122df76c16SMatt Jacob 				break;
6132df76c16SMatt Jacob 			}
6142df76c16SMatt Jacob 			if (retval) {
6152df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6162df76c16SMatt Jacob 				break;
6172df76c16SMatt Jacob 			}
6182df76c16SMatt Jacob 
61953791a95SAlexander Motin 			/* Prepare space for response in memory */
62053791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
62153791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
62253791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
62353791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
62453791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
62553791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
62653791a95SAlexander Motin 				    __func__, chan);
6272df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6282df76c16SMatt Jacob 				retval = ENOMEM;
6292df76c16SMatt Jacob 				break;
6302df76c16SMatt Jacob 			}
63153791a95SAlexander Motin 
63253791a95SAlexander Motin 			/* Send request and wait for response. */
63353791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
63453791a95SAlexander Motin 			if (reqp == NULL) {
63553791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
63653791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
63753791a95SAlexander Motin 				    __func__, chan);
63853791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
63953791a95SAlexander Motin 				ISP_UNLOCK(isp);
6402df76c16SMatt Jacob 				retval = EIO;
64153791a95SAlexander Motin 				break;
6422df76c16SMatt Jacob 			}
64353791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
64453791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
64553791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
64653791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
64753791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
64853791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
64953791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
65053791a95SAlexander Motin 				    __func__, chan);
65153791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
65253791a95SAlexander Motin 				ISP_UNLOCK(isp);
65353791a95SAlexander Motin 				retval = EIO;
65453791a95SAlexander Motin 				break;
65553791a95SAlexander Motin 			}
65653791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
65753791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
65853791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
65953791a95SAlexander Motin 
66053791a95SAlexander Motin 			if (sp.req_completion_status != 0)
66153791a95SAlexander Motin 				retval = EIO;
66253791a95SAlexander Motin 			else if (needmarker)
6632df76c16SMatt Jacob 				fcp->sendmarker = 1;
6642df76c16SMatt Jacob 		} else {
6652df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
6662df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
667c5fd36edSAlexander Motin 				nphdl <<= 8;
668e5265237SMatt Jacob 			}
6698e62a8acSMatt Jacob 			switch (fct->action) {
670f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
6718e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
672c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6738e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6748e62a8acSMatt Jacob 				break;
675f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
6768e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
677c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6788e62a8acSMatt Jacob 				needmarker = 1;
6798e62a8acSMatt Jacob 				break;
680f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
6818e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
682c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6838e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6848e62a8acSMatt Jacob 				needmarker = 1;
6858e62a8acSMatt Jacob 				break;
686f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
6878e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
688c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6898e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6908e62a8acSMatt Jacob 				needmarker = 1;
6918e62a8acSMatt Jacob 				break;
692f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
6938e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
694c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6958e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6968e62a8acSMatt Jacob 				needmarker = 1;
6978e62a8acSMatt Jacob 				break;
6988e62a8acSMatt Jacob 			default:
6998e62a8acSMatt Jacob 				retval = EINVAL;
7008e62a8acSMatt Jacob 				break;
7018e62a8acSMatt Jacob 			}
7028e62a8acSMatt Jacob 			if (retval == 0) {
7038e62a8acSMatt Jacob 				if (needmarker) {
7042df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7058e62a8acSMatt Jacob 				}
7068e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7072df76c16SMatt Jacob 				if (retval) {
7088e62a8acSMatt Jacob 					retval = EIO;
7098e62a8acSMatt Jacob 				}
7102df76c16SMatt Jacob 			}
7112df76c16SMatt Jacob 		}
7122df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7138e62a8acSMatt Jacob 		break;
7148e62a8acSMatt Jacob 	}
7155d571944SMatt Jacob 	default:
7165d571944SMatt Jacob 		break;
7175d571944SMatt Jacob 	}
7185d571944SMatt Jacob 	return (retval);
7190470d791SMatt Jacob }
720478f8a96SJustin T. Gibbs 
721d81ba9d5SMatt Jacob /*
7222df76c16SMatt Jacob  * Local Inlines
7232df76c16SMatt Jacob  */
7242df76c16SMatt Jacob 
7252df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
7262df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
7272df76c16SMatt Jacob 
7282df76c16SMatt Jacob static ISP_INLINE int
7292df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
7302df76c16SMatt Jacob {
7312df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
7322df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
7332df76c16SMatt Jacob 		return (-1);
7342df76c16SMatt Jacob 	}
7352df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
7362df76c16SMatt Jacob 	return (0);
7372df76c16SMatt Jacob }
7382df76c16SMatt Jacob 
7392df76c16SMatt Jacob static ISP_INLINE void
7402df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
7412df76c16SMatt Jacob {
742387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
743405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
744405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
745405b7a29SMatt Jacob #endif
746405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
7472df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
7482df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
7492df76c16SMatt Jacob 	}
750387d8239SMatt Jacob }
751387d8239SMatt Jacob 
7522df76c16SMatt Jacob /*
753d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
754d81ba9d5SMatt Jacob  */
755d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
7562df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
7578290ea90SAlexander Motin static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
7588290ea90SAlexander Motin static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
7598290ea90SAlexander Motin static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
7608290ea90SAlexander Motin static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
7618290ea90SAlexander Motin static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
7628290ea90SAlexander Motin static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
7632df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
7648290ea90SAlexander Motin static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
7652df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
7662df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
767*5773ac11SJohn Baldwin static callout_func_t isp_refire_putback_atio;
768*5773ac11SJohn Baldwin static callout_func_t isp_refire_notify_ack;
769a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
770a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
77194dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
772387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
7732df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
7742df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
7752df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
77696b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
7772df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
7788290ea90SAlexander Motin static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
779d81ba9d5SMatt Jacob 
7802df76c16SMatt Jacob static ISP_INLINE tstate_t *
7819cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
782d81ba9d5SMatt Jacob {
78364edff94SMatt Jacob 	tstate_t *tptr = NULL;
7842df76c16SMatt Jacob 	struct tslist *lhp;
785d81ba9d5SMatt Jacob 
7862df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
787315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
7882df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
7898290ea90SAlexander Motin 			if (tptr->ts_lun == lun)
790d81ba9d5SMatt Jacob 				return (tptr);
791d81ba9d5SMatt Jacob 		}
79264edff94SMatt Jacob 	}
7932df76c16SMatt Jacob 	return (NULL);
7942df76c16SMatt Jacob }
795d81ba9d5SMatt Jacob 
7968290ea90SAlexander Motin static int
7978290ea90SAlexander Motin isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
7982df76c16SMatt Jacob {
7992df76c16SMatt Jacob 	inot_private_data_t *ntp;
8008290ea90SAlexander Motin 	struct ntpdlist rq;
8012df76c16SMatt Jacob 
8028290ea90SAlexander Motin 	if (STAILQ_EMPTY(&tptr->restart_queue))
8038290ea90SAlexander Motin 		return (0);
8048290ea90SAlexander Motin 	STAILQ_INIT(&rq);
8058290ea90SAlexander Motin 	STAILQ_CONCAT(&rq, &tptr->restart_queue);
8068290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
8078290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&rq, next);
8088290ea90SAlexander Motin 		if (IS_24XX(isp)) {
8098290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8108290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8118290ea90SAlexander Motin 			    ((at7_entry_t *)ntp->data)->at_rxid);
8128290ea90SAlexander Motin 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
8138290ea90SAlexander Motin 		} else {
8148290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8158290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8168290ea90SAlexander Motin 			    ((at2_entry_t *)ntp->data)->at_rxid);
8178290ea90SAlexander Motin 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
8182df76c16SMatt Jacob 		}
8198290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
8208290ea90SAlexander Motin 		if (!STAILQ_EMPTY(&tptr->restart_queue))
8218290ea90SAlexander Motin 			break;
8222df76c16SMatt Jacob 	}
8238290ea90SAlexander Motin 	if (!STAILQ_EMPTY(&rq)) {
8248290ea90SAlexander Motin 		STAILQ_CONCAT(&rq, &tptr->restart_queue);
8258290ea90SAlexander Motin 		STAILQ_CONCAT(&tptr->restart_queue, &rq);
8262df76c16SMatt Jacob 	}
8278290ea90SAlexander Motin 	return (!STAILQ_EMPTY(&tptr->restart_queue));
828d81ba9d5SMatt Jacob }
829d81ba9d5SMatt Jacob 
8302df76c16SMatt Jacob static void
8312df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
8322df76c16SMatt Jacob {
8332df76c16SMatt Jacob 	tstate_t *tptr;
83494dff771SMatt Jacob 	union ccb *ccb;
8352df76c16SMatt Jacob 	struct tslist *lhp;
83609ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
8372df76c16SMatt Jacob 	int bus, i;
8382df76c16SMatt Jacob 
8392df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
8402df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8412df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8428290ea90SAlexander Motin 			SLIST_FOREACH(tptr, lhp, next)
8438290ea90SAlexander Motin 				isp_atio_restart(isp, bus, tptr);
84409ddc7adSAlexander Motin 		}
84509ddc7adSAlexander Motin 
84694dff771SMatt Jacob 		/*
84709ddc7adSAlexander Motin 		 * We only need to do this once per channel.
84894dff771SMatt Jacob 		 */
84909ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
85009ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
85109ddc7adSAlexander Motin 		if (ccb != NULL) {
852ea8e769eSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
85394dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
85494dff771SMatt Jacob 		}
8552df76c16SMatt Jacob 	}
8562df76c16SMatt Jacob }
8572df76c16SMatt Jacob 
8588290ea90SAlexander Motin static atio_private_data_t *
8598290ea90SAlexander Motin isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
86053036e92SMatt Jacob {
8618290ea90SAlexander Motin 	struct atpdlist *atfree;
8628290ea90SAlexander Motin 	struct atpdlist *atused;
86353036e92SMatt Jacob 	atio_private_data_t *atp;
8642df76c16SMatt Jacob 
8658290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
8668290ea90SAlexander Motin 	atp = LIST_FIRST(atfree);
8672df76c16SMatt Jacob 	if (atp) {
868523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
869523ea374SAlexander Motin 		atp->tag = tag;
8708290ea90SAlexander Motin 		ISP_GET_PC(isp, chan, atused, atused);
8718290ea90SAlexander Motin 		LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
8722df76c16SMatt Jacob 	}
87353036e92SMatt Jacob 	return (atp);
87453036e92SMatt Jacob }
875523ea374SAlexander Motin 
8768290ea90SAlexander Motin static atio_private_data_t *
8778290ea90SAlexander Motin isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
878523ea374SAlexander Motin {
8798290ea90SAlexander Motin 	struct atpdlist *atused;
880523ea374SAlexander Motin 	atio_private_data_t *atp;
881523ea374SAlexander Motin 
8828290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atused, atused);
8838290ea90SAlexander Motin 	LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
884523ea374SAlexander Motin 		if (atp->tag == tag)
8852df76c16SMatt Jacob 			return (atp);
8862df76c16SMatt Jacob 	}
88753036e92SMatt Jacob 	return (NULL);
88853036e92SMatt Jacob }
88953036e92SMatt Jacob 
8908290ea90SAlexander Motin static void
8918290ea90SAlexander Motin isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
8922df76c16SMatt Jacob {
8938290ea90SAlexander Motin 	struct atpdlist *atfree;
8948290ea90SAlexander Motin 
895387d8239SMatt Jacob 	if (atp->ests) {
896387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
897387d8239SMatt Jacob 	}
898523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
899387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
9008290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
9018290ea90SAlexander Motin 	LIST_INSERT_HEAD(atfree, atp, next);
9022df76c16SMatt Jacob }
9032df76c16SMatt Jacob 
9042df76c16SMatt Jacob static void
9058290ea90SAlexander Motin isp_dump_atpd(ispsoftc_t *isp, int chan)
9062df76c16SMatt Jacob {
9078290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
9082df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
9092df76c16SMatt Jacob 
9108290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
9118290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
912cfbeb41cSAlexander Motin 		if (atp->state == ATPD_STATE_FREE)
913cfbeb41cSAlexander Motin 			continue;
914cfbeb41cSAlexander Motin 		isp_prt(isp, ISP_LOGALL, "Chan %d ATP [0x%x] origdlen %u bytes_xfrd %u lun %jx nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s",
9153f072d69SAlexander Motin 		    chan, atp->tag, atp->orig_datalen, atp->bytes_xfered, (uintmax_t)atp->lun, atp->nphdl, atp->sid, atp->did, atp->oxid, states[atp->state & 0x7]);
9162df76c16SMatt Jacob 	}
9172df76c16SMatt Jacob }
9182df76c16SMatt Jacob 
9198290ea90SAlexander Motin static inot_private_data_t *
9208290ea90SAlexander Motin isp_get_ntpd(ispsoftc_t *isp, int chan)
9212df76c16SMatt Jacob {
9228290ea90SAlexander Motin 	struct ntpdlist *ntfree;
9232df76c16SMatt Jacob 	inot_private_data_t *ntp;
9248290ea90SAlexander Motin 
9258290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
9268290ea90SAlexander Motin 	ntp = STAILQ_FIRST(ntfree);
9278290ea90SAlexander Motin 	if (ntp)
9288290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(ntfree, next);
9292df76c16SMatt Jacob 	return (ntp);
9302df76c16SMatt Jacob }
9312df76c16SMatt Jacob 
9328290ea90SAlexander Motin static inot_private_data_t *
9338290ea90SAlexander Motin isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
9342df76c16SMatt Jacob {
9358290ea90SAlexander Motin 	inot_private_data_t *ntp, *ntp2;
9368290ea90SAlexander Motin 
9378290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, ntpool, ntp);
9388290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
9398290ea90SAlexander Motin 	for (; ntp < ntp2; ntp++) {
9408290ea90SAlexander Motin 		if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
9412df76c16SMatt Jacob 			return (ntp);
9422df76c16SMatt Jacob 	}
9432df76c16SMatt Jacob 	return (NULL);
9442df76c16SMatt Jacob }
9452df76c16SMatt Jacob 
9468290ea90SAlexander Motin static void
9478290ea90SAlexander Motin isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
9482df76c16SMatt Jacob {
9498290ea90SAlexander Motin 	struct ntpdlist *ntfree;
9508290ea90SAlexander Motin 
9518290ea90SAlexander Motin 	ntp->tag_id = ntp->seq_id = 0;
9528290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
9538290ea90SAlexander Motin 	STAILQ_INSERT_HEAD(ntfree, ntp, next);
9542df76c16SMatt Jacob }
9552df76c16SMatt Jacob 
956d81ba9d5SMatt Jacob static cam_status
9572df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
958d81ba9d5SMatt Jacob {
959d81ba9d5SMatt Jacob 	lun_id_t lun;
9602df76c16SMatt Jacob 	struct tslist *lhp;
9612df76c16SMatt Jacob 	tstate_t *tptr;
962d81ba9d5SMatt Jacob 
963d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
9642df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
9656af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
966d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
967d81ba9d5SMatt Jacob 		}
9682df76c16SMatt Jacob 	}
969e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
9702df76c16SMatt Jacob 	if (tptr == NULL) {
971d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
972d81ba9d5SMatt Jacob 	}
973315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
9742df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
9752df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
976b4af3e8aSAlexander Motin 	STAILQ_INIT(&tptr->restart_queue);
977315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
9782df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
9792df76c16SMatt Jacob 	*rslt = tptr;
9802df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
981d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
982d81ba9d5SMatt Jacob }
983d81ba9d5SMatt Jacob 
9848290ea90SAlexander Motin static void
9858290ea90SAlexander Motin destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
986d81ba9d5SMatt Jacob {
987344aebe2SMatt Jacob 	union ccb *ccb;
9882df76c16SMatt Jacob 	struct tslist *lhp;
9898290ea90SAlexander Motin 	inot_private_data_t *ntp;
990e2873b76SMatt Jacob 
9918290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
992344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
993344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
994344aebe2SMatt Jacob 		xpt_done(ccb);
9958290ea90SAlexander Motin 	};
9968290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
997344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
998344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
999344aebe2SMatt Jacob 		xpt_done(ccb);
1000344aebe2SMatt Jacob 	}
10018290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
10028290ea90SAlexander Motin 		isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
10038290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
10048290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
10058290ea90SAlexander Motin 	}
10068290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
10072df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1008d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1009d81ba9d5SMatt Jacob }
1010d81ba9d5SMatt Jacob 
10112df76c16SMatt Jacob static void
10122df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1013d81ba9d5SMatt Jacob {
10143e6deb33SAlexander Motin 	tstate_t *tptr;
10153e6deb33SAlexander Motin 	int bus;
10162df76c16SMatt Jacob 	target_id_t target;
1017d81ba9d5SMatt Jacob 	lun_id_t lun;
1018d81ba9d5SMatt Jacob 
10193e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
10203e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
10213e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
10223e6deb33SAlexander Motin 		xpt_done(ccb);
10233e6deb33SAlexander Motin 		return;
10243e6deb33SAlexander Motin 	}
1025e2873b76SMatt Jacob 
1026746e9c85SMatt Jacob 	/*
10273e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
10283e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1029746e9c85SMatt Jacob 	 */
10302df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10312df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10322df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10336af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
10346af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
10353e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
10362df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10372df76c16SMatt Jacob 		xpt_done(ccb);
10382df76c16SMatt Jacob 		return;
10392df76c16SMatt Jacob 	}
10402df76c16SMatt Jacob 
10413e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1042a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
10432df76c16SMatt Jacob 	if (tptr) {
10442df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
10453e6deb33SAlexander Motin 		xpt_done(ccb);
10463e6deb33SAlexander Motin 		return;
1047d81ba9d5SMatt Jacob 	}
10482df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
10492df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
10502df76c16SMatt Jacob 		xpt_done(ccb);
10513e6deb33SAlexander Motin 		return;
10522df76c16SMatt Jacob 	}
10532df76c16SMatt Jacob 
10543e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
10553e6deb33SAlexander Motin 	xpt_done(ccb);
10562df76c16SMatt Jacob }
10572df76c16SMatt Jacob 
10582df76c16SMatt Jacob static void
10592df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
10602df76c16SMatt Jacob {
10612df76c16SMatt Jacob 	tstate_t *tptr = NULL;
10622df76c16SMatt Jacob 	int bus;
10632df76c16SMatt Jacob 	target_id_t target;
10642df76c16SMatt Jacob 	lun_id_t lun;
10652df76c16SMatt Jacob 
10662df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10672df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10682df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10696af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
10706af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
10713e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
10722df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10732df76c16SMatt Jacob 		xpt_done(ccb);
10742df76c16SMatt Jacob 		return;
10752df76c16SMatt Jacob 	}
10762df76c16SMatt Jacob 
10773e6deb33SAlexander Motin 	/* Find the state pointer. */
10782df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
10793e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
10802df76c16SMatt Jacob 		xpt_done(ccb);
10813e6deb33SAlexander Motin 		return;
10822df76c16SMatt Jacob 	}
10832df76c16SMatt Jacob 
10848290ea90SAlexander Motin 	destroy_lun_state(isp, bus, tptr);
10853e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
10863e6deb33SAlexander Motin 	xpt_done(ccb);
1087d81ba9d5SMatt Jacob }
1088d81ba9d5SMatt Jacob 
10899cd7268eSMatt Jacob static void
1090387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1091d81ba9d5SMatt Jacob {
109294dff771SMatt Jacob 	int fctape, sendstatus, resid;
1093387d8239SMatt Jacob 	fcparam *fcp;
10942df76c16SMatt Jacob 	atio_private_data_t *atp;
109594dff771SMatt Jacob 	struct ccb_scsiio *cso;
109609ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
109794dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
10981dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1099d81ba9d5SMatt Jacob 
110094dff771SMatt 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,
110194dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
110294dff771SMatt Jacob 
110309ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
110494dff771SMatt Jacob 	switch (how) {
110594dff771SMatt Jacob 	case FROM_CAM:
11062df76c16SMatt Jacob 		/*
110794dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
11082df76c16SMatt Jacob 		 */
1109ea8e769eSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, sim_links.tqe);
111094dff771SMatt Jacob 		break;
111109ddc7adSAlexander Motin 	case FROM_TIMER:
111294dff771SMatt Jacob 	case FROM_SRR:
111394dff771SMatt Jacob 	case FROM_CTIO_DONE:
1114ea8e769eSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
111594dff771SMatt Jacob 		break;
111694dff771SMatt Jacob 	}
111794dff771SMatt Jacob 
111809ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1119ea8e769eSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
112094dff771SMatt Jacob 
112194dff771SMatt Jacob 		cso = &ccb->csio;
1122387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1123387d8239SMatt Jacob 		if (xfrlen == 0) {
11242df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1125387d8239SMatt 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");
11262df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
11272df76c16SMatt Jacob 				xpt_done(ccb);
112894dff771SMatt Jacob 				continue;
11292df76c16SMatt Jacob 			}
11302df76c16SMatt Jacob 		}
11312df76c16SMatt Jacob 
11328290ea90SAlexander Motin 		atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
11332df76c16SMatt Jacob 		if (atp == NULL) {
113494dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
11358290ea90SAlexander Motin 			isp_dump_atpd(isp, XS_CHANNEL(ccb));
11362df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
11372df76c16SMatt Jacob 			xpt_done(ccb);
113894dff771SMatt Jacob 			continue;
11392df76c16SMatt Jacob 		}
1140387d8239SMatt Jacob 
1141387d8239SMatt Jacob 		/*
1142387d8239SMatt Jacob 		 * Is this command a dead duck?
1143387d8239SMatt Jacob 		 */
11442df76c16SMatt Jacob 		if (atp->dead) {
114594dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
11462df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
11472df76c16SMatt Jacob 			xpt_done(ccb);
114894dff771SMatt Jacob 			continue;
11492df76c16SMatt Jacob 		}
11502df76c16SMatt Jacob 
11512df76c16SMatt Jacob 		/*
11522df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
11532df76c16SMatt Jacob 		 */
1154387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1155387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
115694dff771SMatt 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);
11572df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
11582df76c16SMatt Jacob 			xpt_done(ccb);
115994dff771SMatt Jacob 			continue;
11602df76c16SMatt Jacob 		}
11612df76c16SMatt Jacob 
11622df76c16SMatt Jacob 		/*
116394dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
116494dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1165387d8239SMatt Jacob 		 */
116694dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
116794dff771SMatt 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);
1168ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
116994dff771SMatt Jacob 			break;
1170387d8239SMatt Jacob 		}
1171387d8239SMatt Jacob 
1172d81ba9d5SMatt Jacob 		/*
1173387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1174d81ba9d5SMatt Jacob 		 */
1175387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1176387d8239SMatt Jacob 			fctape = 1;
1177387d8239SMatt Jacob 		} else {
1178387d8239SMatt Jacob 			fctape = 0;
1179387d8239SMatt Jacob 		}
1180387d8239SMatt Jacob 
1181387d8239SMatt Jacob 		/*
1182387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1183387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1184387d8239SMatt Jacob 		 */
1185387d8239SMatt Jacob 		if (atp->sendst) {
1186daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
118794dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1188387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1189387d8239SMatt Jacob 			atp->sendst = 0;
1190387d8239SMatt Jacob 		}
1191387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1192387d8239SMatt Jacob 			sendstatus = 1;
1193387d8239SMatt Jacob 		} else {
1194387d8239SMatt Jacob 			sendstatus = 0;
1195387d8239SMatt Jacob 		}
1196387d8239SMatt Jacob 
1197387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
119894dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1199387d8239SMatt Jacob 			/*
1200387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1201387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1202387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1203387d8239SMatt Jacob 			 */
1204387d8239SMatt Jacob 			sense_length = cso->sense_len;
1205387d8239SMatt Jacob 
1206387d8239SMatt Jacob 			/*
1207387d8239SMatt Jacob 			 * This 'cannot' happen
1208387d8239SMatt Jacob 			 */
1209387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1210387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1211387d8239SMatt Jacob 			}
1212387d8239SMatt Jacob 		} else {
1213387d8239SMatt Jacob 			sense_length = 0;
1214387d8239SMatt Jacob 		}
1215387d8239SMatt Jacob 
121694dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
121794dff771SMatt Jacob 
121894dff771SMatt Jacob 		/*
121994dff771SMatt Jacob 		 * Check for overflow
122094dff771SMatt Jacob 		 */
1221a6036a44SAlexander Motin 		tmp = atp->bytes_xfered + atp->bytes_in_transit;
1222a6036a44SAlexander Motin 		if (xfrlen > 0 && tmp > atp->orig_datalen) {
1223a6036a44SAlexander Motin 			isp_prt(isp, ISP_LOGERR,
1224a6036a44SAlexander Motin 			    "%s: [0x%x] data overflow by %u bytes", __func__,
1225a6036a44SAlexander Motin 			    cso->tag_id, tmp + xfrlen - atp->orig_datalen);
122694dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
122794dff771SMatt Jacob 			xpt_done(ccb);
122894dff771SMatt Jacob 			continue;
122994dff771SMatt Jacob 		}
1230a6036a44SAlexander Motin 		if (xfrlen > atp->orig_datalen - tmp) {
1231a6036a44SAlexander Motin 			xfrlen = atp->orig_datalen - tmp;
1232a6036a44SAlexander Motin 			if (xfrlen == 0 && !sendstatus) {
1233a6036a44SAlexander Motin 				cso->resid = cso->dxfer_len;
1234a6036a44SAlexander Motin 				ccb->ccb_h.status = CAM_REQ_CMP;
1235a6036a44SAlexander Motin 				xpt_done(ccb);
1236a6036a44SAlexander Motin 				continue;
1237a6036a44SAlexander Motin 			}
1238a6036a44SAlexander Motin 		}
1239387d8239SMatt Jacob 
12402df76c16SMatt Jacob 		if (IS_24XX(isp)) {
12412df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1242d81ba9d5SMatt Jacob 
12432df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
12442df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
124594dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
124694dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
12472df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
12482df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
12493f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
12503f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
12512df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
12522df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
125331c161a6SAlexander Motin 			cto->ct_timeout = XS_TIME(ccb);
12542df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1255387d8239SMatt Jacob 
1256387d8239SMatt Jacob 			/*
1257387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
12586f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1259387d8239SMatt Jacob 			 *
12608b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
12618b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1262387d8239SMatt Jacob 			 *
1263387d8239SMatt Jacob 			 */
1264387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1265387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
126694dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1267387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1268387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1269387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1270387d8239SMatt Jacob 					if (resid < 0) {
1271a6036a44SAlexander Motin 						cto->ct_resid = -resid;
1272387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1273387d8239SMatt Jacob 					} else if (resid > 0) {
1274a6036a44SAlexander Motin 						cto->ct_resid = resid;
1275387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1276387d8239SMatt Jacob 					}
1277387d8239SMatt Jacob 					if (fctape) {
1278387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1279387d8239SMatt Jacob 					}
1280387d8239SMatt Jacob 					if (sense_length) {
12812df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1282387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1283387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
12842df76c16SMatt Jacob 					}
12852df76c16SMatt Jacob 				} else {
1286387d8239SMatt Jacob 					bus_addr_t addr;
1287387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1288387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1289387d8239SMatt Jacob 
1290387d8239SMatt Jacob 					if (atp->ests == NULL) {
1291387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1292387d8239SMatt Jacob 						if (atp->ests == NULL) {
1293ea8e769eSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
129494dff771SMatt Jacob 							break;
1295387d8239SMatt Jacob 						}
1296387d8239SMatt Jacob 					}
1297387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1298387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1299387d8239SMatt Jacob 					if (fctape) {
1300387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1301387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1302387d8239SMatt Jacob 					}
1303387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1304387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1305387d8239SMatt Jacob 					if (resid < 0) {
1306387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1307387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1308387d8239SMatt Jacob 					} else if (resid > 0) {
1309387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1310387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1311387d8239SMatt Jacob 					}
1312387d8239SMatt Jacob 					if (sense_length) {
1313387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1314387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1315387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1316387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1317387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1318387d8239SMatt Jacob 					} else {
1319387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1320387d8239SMatt Jacob 					}
1321387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1322387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1323387d8239SMatt Jacob 					}
1324387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1325387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
132694dff771SMatt 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,
1327387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1328387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1329387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1330387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1331387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1332387d8239SMatt Jacob 				}
1333387d8239SMatt Jacob 				if (sense_length) {
133494dff771SMatt 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__,
133594dff771SMatt 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,
133694dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1337387d8239SMatt Jacob 				} else {
133894dff771SMatt 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__,
133994dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1340387d8239SMatt Jacob 				}
1341387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1342387d8239SMatt Jacob 			}
1343387d8239SMatt Jacob 
1344387d8239SMatt Jacob 			/*
1345387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1346387d8239SMatt Jacob 			 */
1347387d8239SMatt Jacob 			if (xfrlen != 0) {
13482df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
13492df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
13502df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
13512df76c16SMatt Jacob 				} else {
13522df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
13532df76c16SMatt Jacob 				}
1354387d8239SMatt Jacob 
135594dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1356387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1357387d8239SMatt Jacob 
1358387d8239SMatt Jacob #ifdef	DEBUG
1359387d8239SMatt 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) {
1360387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1361387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1362387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1363387d8239SMatt Jacob 				}
1364387d8239SMatt Jacob #endif
1365387d8239SMatt Jacob 				if (sendstatus) {
136694dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
136794dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1368387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1369387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
137094dff771SMatt Jacob 						if (fctape) {
137194dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
137294dff771SMatt Jacob 						}
1373387d8239SMatt Jacob 					} else {
1374387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
137594dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
13762df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
13772df76c16SMatt Jacob 					}
1378387d8239SMatt Jacob 				} else {
1379387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1380387d8239SMatt Jacob 				}
138194dff771SMatt 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__,
138294dff771SMatt 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);
1383387d8239SMatt Jacob 			}
13843e6deb33SAlexander Motin 		} else {
13854fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
138600a8e174SMatt Jacob 
1387d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1388d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
138994dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
139094dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1391d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1392d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
139329f76675SMatt Jacob 			} else {
1394d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
13952df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1396d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
13972ad50ca5SMatt Jacob 				}
139829f76675SMatt Jacob 			}
139931c161a6SAlexander Motin 			cto->ct_timeout = XS_TIME(ccb);
140000a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1401387d8239SMatt Jacob 
1402387d8239SMatt Jacob 			/*
1403387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1404387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1405387d8239SMatt Jacob 			 *
140696240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1407387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1408387d8239SMatt Jacob 			 *
1409387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1410387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1411387d8239SMatt Jacob 			 * needed based upon this.
1412387d8239SMatt Jacob 			 */
1413387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1414387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
141594dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1416387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1417387d8239SMatt Jacob 					if (resid < 0) {
1418387d8239SMatt Jacob 						cto->ct_resid = -resid;
1419387d8239SMatt Jacob 					} else if (resid > 0) {
1420387d8239SMatt Jacob 						cto->ct_resid = resid;
1421387d8239SMatt Jacob 					}
1422387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1423f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1424387d8239SMatt Jacob 					if (resid < 0) {
14252df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1426387d8239SMatt Jacob 					} else if (resid > 0) {
14272df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1428f48ce188SMatt Jacob 					}
1429387d8239SMatt Jacob 					if (fctape) {
1430387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1431387d8239SMatt Jacob 					}
1432387d8239SMatt Jacob 					if (sense_length) {
143300a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1434387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1435387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
143600a8e174SMatt Jacob 					}
143700a8e174SMatt Jacob 				} else {
1438387d8239SMatt Jacob 					bus_addr_t addr;
1439387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1440387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1441387d8239SMatt Jacob 
1442387d8239SMatt Jacob 					if (atp->ests == NULL) {
1443387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1444387d8239SMatt Jacob 						if (atp->ests == NULL) {
1445ea8e769eSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
144694dff771SMatt Jacob 							break;
1447387d8239SMatt Jacob 						}
1448387d8239SMatt Jacob 					}
1449387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1450387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1451387d8239SMatt Jacob 					if (fctape) {
1452387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1453387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1454387d8239SMatt Jacob 					}
1455387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1456387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1457387d8239SMatt Jacob 					if (resid < 0) {
1458387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1459387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1460387d8239SMatt Jacob 					} else if (resid > 0) {
1461387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1462387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1463387d8239SMatt Jacob 					}
1464387d8239SMatt Jacob 					if (sense_length) {
1465387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1466387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1467387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1468387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1469387d8239SMatt Jacob 					} else {
1470387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1471387d8239SMatt Jacob 					}
1472387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1473387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1474387d8239SMatt Jacob 					}
1475387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1476387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
147794dff771SMatt 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,
1478387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1479387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1480387d8239SMatt Jacob 					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1481387d8239SMatt Jacob 					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1482387d8239SMatt Jacob 				}
1483387d8239SMatt Jacob 				if (sense_length) {
148494dff771SMatt 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__,
148594dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
148694dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1487387d8239SMatt Jacob 				} else {
148894dff771SMatt 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,
148994dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1490387d8239SMatt Jacob 				}
1491387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1492387d8239SMatt Jacob 			}
1493387d8239SMatt Jacob 
1494387d8239SMatt Jacob 			if (xfrlen != 0) {
149500a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
149600a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
149700a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
149800a8e174SMatt Jacob 				} else {
149900a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1500d81ba9d5SMatt Jacob 				}
150153036e92SMatt Jacob 
150294dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1503387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1504387d8239SMatt Jacob 
1505387d8239SMatt Jacob 				if (sendstatus) {
150694dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
150794dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1508387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1509387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
151094dff771SMatt Jacob 						if (fctape) {
151194dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
151294dff771SMatt Jacob 						}
1513387d8239SMatt Jacob 					} else {
1514387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
151594dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1516387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1517387d8239SMatt Jacob 					}
1518387d8239SMatt Jacob 				} else {
1519387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1520387d8239SMatt Jacob 				}
1521387d8239SMatt Jacob 			}
152294dff771SMatt 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,
152394dff771SMatt 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);
1524d81ba9d5SMatt Jacob 		}
1525d81ba9d5SMatt Jacob 
152694dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
152794dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1528ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
152994dff771SMatt Jacob 			break;
153094dff771SMatt Jacob 		}
1531970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1532970ceb2fSAlexander Motin 		if (handle == 0) {
1533387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1534ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
153594dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
153694dff771SMatt Jacob 			break;
1537d81ba9d5SMatt Jacob 		}
153894dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
153994dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
154094dff771SMatt Jacob 
1541d81ba9d5SMatt Jacob 
1542d81ba9d5SMatt Jacob 		/*
1543d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1544d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1545b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1546d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1547d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1548d81ba9d5SMatt Jacob 		 * format.
1549d81ba9d5SMatt Jacob 		 */
1550d81ba9d5SMatt Jacob 
15512df76c16SMatt Jacob 		if (IS_24XX(isp)) {
15522df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
15532df76c16SMatt Jacob 			cto->ct_syshandle = handle;
155410365e5aSMatt Jacob 		} else {
15553e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
155610365e5aSMatt Jacob 			cto->ct_syshandle = handle;
155710365e5aSMatt Jacob 		}
1558a1bc34c6SMatt Jacob 
15592df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
156094dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1561970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
156294dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
156394dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
1564ea8e769eSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
156594dff771SMatt Jacob 				break;
156694dff771SMatt Jacob 			}
156794dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
156894dff771SMatt Jacob 			xpt_done(ccb);
156994dff771SMatt Jacob 			continue;
157094dff771SMatt Jacob 		}
157194dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1572387d8239SMatt Jacob 		if (xfrlen) {
1573387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1574387d8239SMatt Jacob 		} else {
1575387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1576387d8239SMatt Jacob 		}
1577387d8239SMatt Jacob 		atp->ctcnt++;
157894dff771SMatt Jacob 		atp->seqno++;
15792df76c16SMatt Jacob 	}
1580d81ba9d5SMatt Jacob }
1581d81ba9d5SMatt Jacob 
1582a1bc34c6SMatt Jacob static void
1583a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1584f48ce188SMatt Jacob {
15852df76c16SMatt Jacob 	union ccb *ccb = arg;
15862a0db815SJohn Baldwin 
1587b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
15882df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1589a1bc34c6SMatt Jacob }
1590a1bc34c6SMatt Jacob 
1591a1bc34c6SMatt Jacob static void
1592387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1593387d8239SMatt Jacob {
1594387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1595387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
15962a0db815SJohn Baldwin 
15972a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1598387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
15992a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1600387d8239SMatt Jacob 	} else {
1601387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1602387d8239SMatt Jacob 	}
1603387d8239SMatt Jacob }
1604387d8239SMatt Jacob 
1605387d8239SMatt Jacob 
1606387d8239SMatt Jacob static void
1607a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1608a1bc34c6SMatt Jacob {
1609782a8e7cSAlexander Motin 	ispsoftc_t *isp = XS_ISP(ccb);
1610782a8e7cSAlexander Motin 	struct ccb_scsiio *cso = &ccb->csio;
16113e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1612a1bc34c6SMatt Jacob 
16132df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1614f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1615f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
16162df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1617a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1618f48ce188SMatt Jacob 	} else {
1619a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1620f48ce188SMatt Jacob 	}
1621f48ce188SMatt Jacob 	at->at_status = CT_OK;
1622a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
16236ddb8972SAlexander Motin 	at->at_iid = cso->init_id;
1624782a8e7cSAlexander Motin 	if (isp_target_put_entry(isp, at)) {
1625782a8e7cSAlexander Motin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1626782a8e7cSAlexander Motin 		    isp_refire_putback_atio, ccb);
1627782a8e7cSAlexander Motin 	} else
1628a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
1629f48ce188SMatt Jacob }
1630f48ce188SMatt Jacob 
1631f48ce188SMatt Jacob static void
1632a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1633f48ce188SMatt Jacob {
1634387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1635a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1636a1bc34c6SMatt Jacob 		xpt_done(ccb);
1637f48ce188SMatt Jacob 	}
1638387d8239SMatt Jacob }
1639f48ce188SMatt Jacob 
16402df76c16SMatt Jacob static void
16419cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1642d81ba9d5SMatt Jacob {
164345b9ad7aSAlexander Motin 	fcparam *fcp;
164492a1e549SMatt Jacob 	lun_id_t lun;
16452df76c16SMatt Jacob 	fcportdb_t *lp;
1646d81ba9d5SMatt Jacob 	tstate_t *tptr;
1647d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
16482df76c16SMatt Jacob 	uint16_t nphdl;
1649a035b0afSMatt Jacob 	atio_private_data_t *atp;
16502df76c16SMatt Jacob 	inot_private_data_t *ntp;
1651d81ba9d5SMatt Jacob 
1652d81ba9d5SMatt Jacob 	/*
1653d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1654d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1655d81ba9d5SMatt Jacob 	 *
1656b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1657d81ba9d5SMatt Jacob 	 */
1658d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
16592df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
16608290ea90SAlexander Motin 		isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
16612df76c16SMatt Jacob 		return;
1662d81ba9d5SMatt Jacob 	}
1663d81ba9d5SMatt Jacob 
166445b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
16652df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
166692a1e549SMatt Jacob 		lun = aep->at_scclun;
16672ad50ca5SMatt Jacob 	} else {
166892a1e549SMatt Jacob 		lun = aep->at_lun;
16692ad50ca5SMatt Jacob 	}
16702df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
16712df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
16722df76c16SMatt Jacob 	} else {
16732df76c16SMatt Jacob 		nphdl = aep->at_iid;
16742df76c16SMatt Jacob 	}
1675a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1676d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1677a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1678746e9c85SMatt Jacob 		if (tptr == NULL) {
1679123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1680387d8239SMatt Jacob 			if (lun == 0) {
16818290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1682387d8239SMatt Jacob 			} else {
16838290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1684387d8239SMatt Jacob 			}
16852df76c16SMatt Jacob 			return;
1686746e9c85SMatt Jacob 		}
1687d81ba9d5SMatt Jacob 	}
1688d81ba9d5SMatt Jacob 
1689d81ba9d5SMatt Jacob 	/*
16902df76c16SMatt Jacob 	 * Start any commands pending resources first.
1691d81ba9d5SMatt Jacob 	 */
16928290ea90SAlexander Motin 	if (isp_atio_restart(isp, 0, tptr))
16932df76c16SMatt Jacob 		goto noresrc;
16942df76c16SMatt Jacob 
16952df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
16962df76c16SMatt Jacob 	if (atiop == NULL) {
16972df76c16SMatt Jacob 		goto noresrc;
16982df76c16SMatt Jacob 	}
16992df76c16SMatt Jacob 
17008290ea90SAlexander Motin 	atp = isp_get_atpd(isp, 0, aep->at_rxid);
17012df76c16SMatt Jacob 	if (atp == NULL) {
17022df76c16SMatt Jacob 		goto noresrc;
17032df76c16SMatt Jacob 	}
17042df76c16SMatt Jacob 
1705570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1706d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1707d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
17086ddb8972SAlexander Motin 	atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
170992a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
17102df76c16SMatt Jacob 
1711b0a3ba7eSMatt Jacob 	/*
1712b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1713b0a3ba7eSMatt Jacob 	 */
1714f48ce188SMatt Jacob 	atiop->sense_len = 0;
17152df76c16SMatt Jacob 
17162df76c16SMatt Jacob 	/*
17172df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
17182df76c16SMatt Jacob 	 */
1719d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1720d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1721d4f3ad3aSAlexander Motin 	else {
1722e2a658cbSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
1723d4f3ad3aSAlexander Motin 			atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1724e2a658cbSAlexander Motin 		} else {
1725e2a658cbSAlexander Motin 			isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB",
1726e2a658cbSAlexander Motin 			    __func__, nphdl);
1727e2a658cbSAlexander Motin 			isp_dump_portdb(isp, 0);
1728e2a658cbSAlexander Motin 			isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0);
1729e2a658cbSAlexander Motin 			return;
1730e2a658cbSAlexander Motin 		}
17312df76c16SMatt Jacob 	}
1732d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
17332df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1734d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
17352df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1736d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1737d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
17385e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1739d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1740d81ba9d5SMatt Jacob 		break;
1741d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
17425e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1743d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1744d81ba9d5SMatt Jacob 		break;
1745d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
17465e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1747d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1748d81ba9d5SMatt Jacob 		break;
1749d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1750d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1751d81ba9d5SMatt Jacob 	default:
1752d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1753d81ba9d5SMatt Jacob 		break;
1754d81ba9d5SMatt Jacob 	}
1755f48ce188SMatt Jacob 
175653036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
175753036e92SMatt Jacob 	atp->bytes_xfered = 0;
17582df76c16SMatt Jacob 	atp->lun = lun;
1759d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
17602df76c16SMatt Jacob 	atp->sid = PORT_ANY;
17612df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
17622df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
17632df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1764570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
1765d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
1766123055f0SNathan 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);
17672df76c16SMatt Jacob 	return;
17682df76c16SMatt Jacob noresrc:
17698290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, 0);
17702df76c16SMatt Jacob 	if (ntp == NULL) {
17718290ea90SAlexander Motin 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
17722df76c16SMatt Jacob 		return;
17732df76c16SMatt Jacob 	}
17748290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
17758290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1776d81ba9d5SMatt Jacob }
1777d81ba9d5SMatt Jacob 
17782df76c16SMatt Jacob static void
17792df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
17802df76c16SMatt Jacob {
17812df76c16SMatt Jacob 	int cdbxlen;
17826af11b82SAlexander Motin 	lun_id_t lun;
17836af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
17842df76c16SMatt Jacob 	uint32_t did, sid;
17852df76c16SMatt Jacob 	fcportdb_t *lp;
17862df76c16SMatt Jacob 	tstate_t *tptr;
17872df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
17882df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
17899e7d423dSMatt Jacob 	atio_private_data_t *oatp;
17902df76c16SMatt Jacob 	inot_private_data_t *ntp;
17912df76c16SMatt Jacob 
17922df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
17932df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
17946af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
17952df76c16SMatt Jacob 
17967dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
17979c81a61eSAlexander Motin 		/* Channel has to be derived from D_ID */
17982df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
17992df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
18009c81a61eSAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
18019c81a61eSAlexander Motin 			    "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
18029c81a61eSAlexander Motin 			    __func__, aep->at_rxid, did);
18039c81a61eSAlexander Motin 			isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
18049c81a61eSAlexander Motin 			    ECMD_TERMINATE, 0);
18052df76c16SMatt Jacob 			return;
18062df76c16SMatt Jacob 		}
18072df76c16SMatt Jacob 	} else {
18082df76c16SMatt Jacob 		chan = 0;
18092df76c16SMatt Jacob 	}
18102df76c16SMatt Jacob 
18112df76c16SMatt Jacob 	/*
18122df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
18132df76c16SMatt Jacob 	 */
1814eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
18152df76c16SMatt Jacob 		/*
18162df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
18172df76c16SMatt Jacob 		 */
18182df76c16SMatt 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",
18192df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
1820e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
18212df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
18222df76c16SMatt Jacob 		return;
18232df76c16SMatt Jacob 	}
18242df76c16SMatt Jacob 	nphdl = lp->handle;
18252df76c16SMatt Jacob 
18262df76c16SMatt Jacob 	/*
18272df76c16SMatt Jacob 	 * Get the tstate pointer
18282df76c16SMatt Jacob 	 */
18292df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
18302df76c16SMatt Jacob 	if (tptr == NULL) {
18312df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
18322df76c16SMatt Jacob 		if (tptr == NULL) {
18336af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
18346af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
18356af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
1836387d8239SMatt Jacob 			if (lun == 0) {
1837352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1838387d8239SMatt Jacob 			} else {
18392df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1840387d8239SMatt Jacob 			}
18412df76c16SMatt Jacob 			return;
18422df76c16SMatt Jacob 		}
18432df76c16SMatt Jacob 	}
18442df76c16SMatt Jacob 
18452df76c16SMatt Jacob 	/*
18462df76c16SMatt Jacob 	 * Start any commands pending resources first.
18472df76c16SMatt Jacob 	 */
18488290ea90SAlexander Motin 	if (isp_atio_restart(isp, chan, tptr))
18492df76c16SMatt Jacob 		goto noresrc;
18502df76c16SMatt Jacob 
18512df76c16SMatt Jacob 	/*
18522df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
18532df76c16SMatt Jacob 	 */
18542df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
18552df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
18562df76c16SMatt Jacob 		return;
18572df76c16SMatt Jacob 	}
18582df76c16SMatt Jacob 
18592df76c16SMatt Jacob 	/*
18602df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
18612df76c16SMatt Jacob 	 */
18622df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
18632df76c16SMatt Jacob 	if (atiop == NULL) {
18642df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
18652df76c16SMatt Jacob 		goto noresrc;
18662df76c16SMatt Jacob 	}
18672df76c16SMatt Jacob 
18688290ea90SAlexander Motin 	oatp = isp_find_atpd(isp, chan, aep->at_rxid);
18699e7d423dSMatt Jacob 	if (oatp) {
1870387d8239SMatt 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",
18719e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
18722df76c16SMatt Jacob 		/*
18732df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
18742df76c16SMatt Jacob 		 */
18752df76c16SMatt Jacob 		goto noresrc;
18762df76c16SMatt Jacob 	}
18778290ea90SAlexander Motin 	atp = isp_get_atpd(isp, chan, aep->at_rxid);
1878523ea374SAlexander Motin 	if (atp == NULL) {
1879523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1880523ea374SAlexander Motin 		goto noresrc;
1881523ea374SAlexander Motin 	}
1882387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
18832df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
18842df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1885d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1886d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
18876ddb8972SAlexander Motin 	atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
18882df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
18892df76c16SMatt Jacob 	atiop->sense_len = 0;
18902df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
18912df76c16SMatt Jacob 	if (cdbxlen) {
18922df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
18932df76c16SMatt Jacob 	}
18942df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
18952df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
18962df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
18972df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
18982df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
18992df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
19002df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
19015e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19022df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
19032df76c16SMatt Jacob 		break;
19042df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
19055e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19062df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
19072df76c16SMatt Jacob 		break;
19082df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
19095e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19102df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
19112df76c16SMatt Jacob 		break;
19122df76c16SMatt Jacob 	default:
19132df76c16SMatt Jacob 		/* FALLTHROUGH */
19142df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
19152df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
19162df76c16SMatt Jacob 		atiop->tag_action = 0;
19172df76c16SMatt Jacob 		break;
19182df76c16SMatt Jacob 	}
19192df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
19202df76c16SMatt Jacob 	atp->bytes_xfered = 0;
19212df76c16SMatt Jacob 	atp->lun = lun;
19222df76c16SMatt Jacob 	atp->nphdl = nphdl;
19233f072d69SAlexander Motin 	atp->sid = sid;
19243f072d69SAlexander Motin 	atp->did = did;
19252df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
19269e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
19272df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
19282df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
19292df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
19306af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
19316af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
19322df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
19332df76c16SMatt Jacob 	return;
19342df76c16SMatt Jacob noresrc:
19358290ea90SAlexander Motin 	if (atp)
19368290ea90SAlexander Motin 		isp_put_atpd(isp, chan, atp);
19378290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, chan);
19382df76c16SMatt Jacob 	if (ntp == NULL) {
19392df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
19402df76c16SMatt Jacob 		return;
19412df76c16SMatt Jacob 	}
19428290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
19438290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
19442df76c16SMatt Jacob }
19452df76c16SMatt Jacob 
1946387d8239SMatt Jacob 
1947387d8239SMatt Jacob /*
1948387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
1949387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
1950387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
1951387d8239SMatt Jacob  * transaction.
1952387d8239SMatt Jacob  */
1953387d8239SMatt Jacob static void
19548290ea90SAlexander Motin isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1955387d8239SMatt Jacob {
1956387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
1957387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1958387d8239SMatt Jacob 	union ccb *ccb;
1959387d8239SMatt Jacob 
1960387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
1961387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1962387d8239SMatt Jacob 	ccb = atp->srr_ccb;
1963387d8239SMatt Jacob 	atp->srr_ccb = NULL;
1964387d8239SMatt Jacob 	atp->nsrr++;
1965387d8239SMatt Jacob 	if (ccb == NULL) {
196694dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1967387d8239SMatt Jacob 		goto fail;
1968387d8239SMatt Jacob 	}
1969387d8239SMatt Jacob 
1970387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
1971387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
1972387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1973387d8239SMatt Jacob 
1974387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
1975387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
1976387d8239SMatt Jacob 		/*
1977387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
1978387d8239SMatt Jacob 		 */
1979387d8239SMatt Jacob 		atp->sendst = 0;
1980387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
1981387d8239SMatt Jacob 		if (ccb_len == 0) {
198294dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1983387d8239SMatt Jacob 			goto mdp;
1984387d8239SMatt Jacob 		}
1985387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
198694dff771SMatt 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);
1987387d8239SMatt Jacob 			goto mdp;
1988387d8239SMatt Jacob 		}
198994dff771SMatt 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);
1990387d8239SMatt Jacob 		break;
1991387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
199294dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1993387d8239SMatt Jacob 		atp->sendst = 1;
1994387d8239SMatt Jacob 		/*
1995387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
1996387d8239SMatt Jacob 		 */
1997387d8239SMatt Jacob 		break;
1998387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
1999387d8239SMatt Jacob 		/*
2000387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2001387d8239SMatt Jacob 		 */
2002387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2003387d8239SMatt Jacob 		goto fail;
2004387d8239SMatt Jacob 
2005387d8239SMatt Jacob 	default:
2006387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2007387d8239SMatt Jacob 		goto fail;
2008387d8239SMatt Jacob 	}
2009387d8239SMatt Jacob 
2010387d8239SMatt Jacob 	/*
2011387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2012387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2013387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2014387d8239SMatt Jacob 	 */
2015387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2016387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2017387d8239SMatt Jacob 		goto fail;
2018387d8239SMatt Jacob 	}
2019387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2020387d8239SMatt Jacob 	return;
2021387d8239SMatt Jacob fail:
2022387d8239SMatt Jacob 	inot->in_reserved = 1;
2023387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2024387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2025387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2026387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2027387d8239SMatt Jacob 	return;
2028387d8239SMatt Jacob mdp:
2029387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2030387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2031387d8239SMatt Jacob 		goto fail;
2032387d8239SMatt Jacob 	}
2033387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2034387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2035387d8239SMatt Jacob 	/*
2036387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2037387d8239SMatt Jacob 	 */
2038387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2039387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2040387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2041387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2042387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2043387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2044387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2045387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2046387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2047387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2048387d8239SMatt Jacob }
2049387d8239SMatt Jacob 
2050387d8239SMatt Jacob 
2051387d8239SMatt Jacob static void
20525a5632c2SAlexander Motin isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
2053387d8239SMatt Jacob {
20545a5632c2SAlexander Motin 	in_fcentry_24xx_t *inot = notify->nt_lreserved;
2055387d8239SMatt Jacob 	atio_private_data_t *atp;
20565a5632c2SAlexander Motin 	uint32_t tag = notify->nt_tagval & 0xffffffff;
2057387d8239SMatt Jacob 
20585a5632c2SAlexander Motin 	atp = isp_find_atpd(isp, notify->nt_channel, tag);
2059387d8239SMatt Jacob 	if (atp == NULL) {
20605a5632c2SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
20615a5632c2SAlexander Motin 		    __func__, tag);
2062387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2063387d8239SMatt Jacob 		return;
2064387d8239SMatt Jacob 	}
2065387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2066387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
20675a5632c2SAlexander Motin 	isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
20685a5632c2SAlexander Motin 	    inot->in_rxid, inot->in_flags, inot->in_srr_iu,
20695a5632c2SAlexander Motin 	    ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
2070387d8239SMatt Jacob 	if (atp->srr_ccb)
20718290ea90SAlexander Motin 		isp_handle_srr_start(isp, atp);
2072387d8239SMatt Jacob }
2073387d8239SMatt Jacob 
20742df76c16SMatt Jacob static void
20759cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2076d81ba9d5SMatt Jacob {
2077d81ba9d5SMatt Jacob 	union ccb *ccb;
2078a6036a44SAlexander Motin 	int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
20792df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
20802df76c16SMatt Jacob 	int bus;
2081a6036a44SAlexander Motin 	uint32_t handle, data_requested, resid;
2082d81ba9d5SMatt Jacob 
20832df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2084970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
20852df76c16SMatt Jacob 	if (ccb == NULL) {
20862df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
20872df76c16SMatt Jacob 		return;
20882df76c16SMatt Jacob 	}
2089970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
2090a6036a44SAlexander Motin 	resid = data_requested = PISP_PCMD(ccb)->datalen;
2091387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2092387d8239SMatt Jacob 
20932df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
2094387d8239SMatt Jacob 	if (IS_24XX(isp)) {
20958290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2096387d8239SMatt Jacob 	} else {
20978290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2098387d8239SMatt Jacob 	}
2099387d8239SMatt Jacob 	if (atp == NULL) {
2100af207637SAlexander Motin 		/*
21018656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
21028656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
21038656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2104af207637SAlexander Motin 		 */
21058290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2106af207637SAlexander Motin 	}
2107af207637SAlexander Motin 	if (atp == NULL) {
2108387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2109387d8239SMatt Jacob 		return;
2110387d8239SMatt Jacob 	}
2111387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
211294dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2113387d8239SMatt Jacob 	atp->ctcnt -= 1;
2114387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2115387d8239SMatt Jacob 
21162df76c16SMatt Jacob 	if (IS_24XX(isp)) {
21172df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2118d81ba9d5SMatt Jacob 
2119387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2120387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2121387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
21228290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
21232df76c16SMatt Jacob 			return;
21242df76c16SMatt Jacob 		}
2125387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
212687de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
212787de303cSAlexander Motin 			    (atp->sendst == 0);
2128387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2129387d8239SMatt Jacob 		} else {
21302df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
21312df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
213294dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2133a6036a44SAlexander Motin 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
21342df76c16SMatt Jacob 				resid = ct->ct_resid;
21352df76c16SMatt Jacob 		}
213694dff771SMatt 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),
213794dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
21383e6deb33SAlexander Motin 	} else {
21392df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2140387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2141387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2142387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
21438290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
2144387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
21452df76c16SMatt Jacob 			return;
2146570c7a3fSMatt Jacob 		}
2147387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
214887de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
214987de303cSAlexander Motin 			    (atp->sendst == 0);
2150387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2151387d8239SMatt Jacob 		} else {
2152d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2153d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
215494dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2155a6036a44SAlexander Motin 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2156a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
215753036e92SMatt Jacob 		}
215894dff771SMatt 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),
215994dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
21605d571944SMatt Jacob 	}
2161387d8239SMatt Jacob 	if (ok) {
2162a6036a44SAlexander Motin 		if (data_requested > 0) {
2163a6036a44SAlexander Motin 			atp->bytes_xfered += data_requested - resid;
2164a6036a44SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
2165a6036a44SAlexander Motin 			    (data_requested - resid);
2166387d8239SMatt Jacob 		}
2167a6036a44SAlexander Motin 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2168387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2169387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2170387d8239SMatt Jacob 	} else {
2171387d8239SMatt Jacob 		notify_cam = 1;
2172387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2173387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2174387d8239SMatt Jacob 		else
2175387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2176387d8239SMatt Jacob 	}
2177387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2178a1bc34c6SMatt Jacob 
2179a1bc34c6SMatt Jacob 	/*
2180387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2181387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2182d81ba9d5SMatt Jacob 	 */
218394dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
218494dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2185f48ce188SMatt Jacob 	if (notify_cam == 0) {
2186387d8239SMatt Jacob 		if (atp->sendst) {
2187387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2188387d8239SMatt Jacob 		}
21892df76c16SMatt Jacob 		return;
2190f48ce188SMatt Jacob 	}
2191d81ba9d5SMatt Jacob 
2192387d8239SMatt Jacob 	/*
219387de303cSAlexander Motin 	 * We are done with this ATIO if we successfully sent status.
219487de303cSAlexander Motin 	 * In all other cases expect either another CTIO or XPT_ABORT.
21958290ea90SAlexander Motin 	 */
219687de303cSAlexander Motin 	if (ok && sentstatus)
21978290ea90SAlexander Motin 		isp_put_atpd(isp, bus, atp);
21988290ea90SAlexander Motin 
21998290ea90SAlexander Motin 	/*
2200387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2201387d8239SMatt Jacob 	 *
2202387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2203387d8239SMatt Jacob 	 *
2204387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2205387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2206387d8239SMatt Jacob 	 * and try again in case we didn't have
2207387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2208387d8239SMatt Jacob 	 * once the putback is done we do the completion
2209387d8239SMatt Jacob 	 * call.
2210387d8239SMatt Jacob 	 */
2211387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2212a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2213387d8239SMatt Jacob 	} else {
2214387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2215d81ba9d5SMatt Jacob 	}
2216d81ba9d5SMatt Jacob }
2217570c7a3fSMatt Jacob 
22182df76c16SMatt Jacob static int
221996b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
22202df76c16SMatt Jacob {
22212df76c16SMatt Jacob 
22222df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
22232df76c16SMatt 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);
2224570c7a3fSMatt Jacob 		return (0);
2225570c7a3fSMatt Jacob 	}
22262df76c16SMatt Jacob 
22272df76c16SMatt Jacob 	/*
22282df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
22292df76c16SMatt Jacob 	 */
22302df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
22312df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
22322df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
22332df76c16SMatt Jacob 		fcportdb_t *lp;
22342df76c16SMatt Jacob 		uint32_t sid;
22352df76c16SMatt Jacob 		uint16_t nphdl;
22362df76c16SMatt Jacob 
22372df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2238eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
22392df76c16SMatt Jacob 			nphdl = lp->handle;
22402df76c16SMatt Jacob 		} else {
22412df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
22422df76c16SMatt Jacob 		}
22432df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
22442df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
22452df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
22462df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
22472df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
22482df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
22492df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
22502df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
22512df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
22522df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
22532df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
225496b5475bSAlexander Motin 		if (rsp != 0) {
225596b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
225696b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
225796b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
225896b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
225996b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
226096b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
226196b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
226296b5475bSAlexander Motin 		}
22632df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
22642df76c16SMatt Jacob 	}
22652df76c16SMatt Jacob 
22662df76c16SMatt Jacob 	/*
22672df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
22682df76c16SMatt Jacob 	 */
22692df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
22702df76c16SMatt Jacob 
22712df76c16SMatt Jacob 		/*
22722df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
22732df76c16SMatt Jacob 		 */
22742df76c16SMatt Jacob 		if (mp->nt_need_ack) {
22752df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
22762df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
22772df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
22782df76c16SMatt Jacob 
22792df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
22802df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
22812df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
22822df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
22832df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
22842df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
22852df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
22862df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
22872df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
22882df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
22892df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
22902df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
22912df76c16SMatt Jacob 				return (ENOMEM);
22922df76c16SMatt Jacob 			}
22932df76c16SMatt Jacob 			mp->nt_need_ack = 0;
22942df76c16SMatt Jacob 		}
22952df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
22962df76c16SMatt Jacob 			return (ENOMEM);
22972df76c16SMatt Jacob 		} else {
22982df76c16SMatt Jacob 			return (0);
22992df76c16SMatt Jacob 		}
23002df76c16SMatt Jacob 	}
23012df76c16SMatt Jacob 
23022df76c16SMatt Jacob 	/*
23032df76c16SMatt Jacob 	 * Handle logout cases here
23042df76c16SMatt Jacob 	 */
23052df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
23062df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
23072df76c16SMatt Jacob 	}
23082df76c16SMatt Jacob 
23092df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
23102df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
23112df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
23122df76c16SMatt Jacob 		}
23132df76c16SMatt Jacob 	}
23142df76c16SMatt Jacob 
23152df76c16SMatt Jacob 	/*
23162df76c16SMatt Jacob 	 * General purpose acknowledgement
23172df76c16SMatt Jacob 	 */
23182df76c16SMatt Jacob 	if (mp->nt_need_ack) {
23192df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2320387d8239SMatt Jacob 		/*
2321387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2322387d8239SMatt Jacob 		 */
23232df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
23242df76c16SMatt Jacob 	}
23252df76c16SMatt Jacob 	return (0);
23262df76c16SMatt Jacob }
23272df76c16SMatt Jacob 
23282df76c16SMatt Jacob /*
2329b1ce21c6SRebecca Cran  * Handle task management functions.
23302df76c16SMatt Jacob  *
23312df76c16SMatt Jacob  * We show up here with a notify structure filled out.
23322df76c16SMatt Jacob  *
23332df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
23342df76c16SMatt Jacob  */
23352df76c16SMatt Jacob static void
23362df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
23372df76c16SMatt Jacob {
23382df76c16SMatt Jacob 	tstate_t *tptr;
23392df76c16SMatt Jacob 	fcportdb_t *lp;
23402df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
23412df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
2342981ffc4eSAlexander Motin 	atio_private_data_t *atp;
23432df76c16SMatt Jacob 	lun_id_t lun;
23442df76c16SMatt Jacob 
234515c62456SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
23462df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
23472df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
2348981ffc4eSAlexander Motin 		if (notify->nt_tagval == TAG_ANY) {
23492df76c16SMatt Jacob 			lun = CAM_LUN_WILDCARD;
23502df76c16SMatt Jacob 		} else {
2351981ffc4eSAlexander Motin 			atp = isp_find_atpd(isp, notify->nt_channel,
2352981ffc4eSAlexander Motin 			    notify->nt_tagval & 0xffffffff);
2353981ffc4eSAlexander Motin 			lun = atp ? atp->lun : CAM_LUN_WILDCARD;
2354981ffc4eSAlexander Motin 		}
2355981ffc4eSAlexander Motin 	} else {
23562df76c16SMatt Jacob 		lun = notify->nt_lun;
23572df76c16SMatt Jacob 	}
23582df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
23592df76c16SMatt Jacob 	if (tptr == NULL) {
23602df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
23612df76c16SMatt Jacob 		if (tptr == NULL) {
2362123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
23632df76c16SMatt Jacob 			goto bad;
23642df76c16SMatt Jacob 		}
23652df76c16SMatt Jacob 	}
23662df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
23672df76c16SMatt Jacob 	if (inot == NULL) {
2368123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
23692df76c16SMatt Jacob 		goto bad;
23702df76c16SMatt Jacob 	}
23712df76c16SMatt Jacob 
23726ddb8972SAlexander Motin 	inot->ccb_h.target_id = ISP_MAX_TARGETS(isp);
23736ddb8972SAlexander Motin 	inot->ccb_h.target_lun = lun;
2374eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2375d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
23762df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
23772df76c16SMatt Jacob 	} else {
2378d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
23792df76c16SMatt Jacob 	}
23802df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
23812df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
23822df76c16SMatt Jacob 
23832df76c16SMatt Jacob 	switch (notify->nt_ncode) {
23842df76c16SMatt Jacob 	case NT_ABORT_TASK:
23858290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
23862df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
23872df76c16SMatt Jacob 		break;
23882df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
23898290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
23902df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
23912df76c16SMatt Jacob 		break;
23922df76c16SMatt Jacob 	case NT_CLEAR_ACA:
23932df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
23942df76c16SMatt Jacob 		break;
23952df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
23962df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
23972df76c16SMatt Jacob 		break;
23982df76c16SMatt Jacob 	case NT_LUN_RESET:
23992df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
24002df76c16SMatt Jacob 		break;
24012df76c16SMatt Jacob 	case NT_TARGET_RESET:
24022df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
24032df76c16SMatt Jacob 		break;
2404c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2405c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2406c98d2b1fSAlexander Motin 		break;
2407c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2408c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2409c98d2b1fSAlexander Motin 		break;
24102df76c16SMatt Jacob 	default:
2411123055f0SNathan 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);
24122df76c16SMatt Jacob 		goto bad;
24132df76c16SMatt Jacob 	}
24142df76c16SMatt Jacob 
24158290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, notify->nt_channel);
24162df76c16SMatt Jacob 	if (ntp == NULL) {
24172df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
24182df76c16SMatt Jacob 		goto bad;
24192df76c16SMatt Jacob 	}
24208290ea90SAlexander Motin 	ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
24212df76c16SMatt Jacob 	if (notify->nt_lreserved) {
24228290ea90SAlexander Motin 		ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
24238290ea90SAlexander Motin 		ntp->nt.nt_lreserved = &ntp->data;
24242df76c16SMatt Jacob 	}
24258290ea90SAlexander Motin 	ntp->seq_id = notify->nt_tagval;
24268290ea90SAlexander Motin 	ntp->tag_id = notify->nt_tagval >> 32;
24272df76c16SMatt Jacob 
24282df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2429d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
24302df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
24312df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
24322df76c16SMatt Jacob 	return;
24332df76c16SMatt Jacob bad:
243444a2a27aSAlexander Motin 	if (notify->nt_need_ack) {
24352df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2436387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2437387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2438387d8239SMatt Jacob 			}
24392df76c16SMatt Jacob 		} else {
2440387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
24412df76c16SMatt Jacob 		}
24422df76c16SMatt Jacob 	}
24432df76c16SMatt Jacob }
24442df76c16SMatt Jacob 
24452df76c16SMatt Jacob static void
24468290ea90SAlexander Motin isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
24472df76c16SMatt Jacob {
24488290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
24498290ea90SAlexander Motin 	inot_private_data_t *ntp, *tmp;
24508290ea90SAlexander Motin 	uint32_t this_tag_id;
24512df76c16SMatt Jacob 
24522df76c16SMatt Jacob 	/*
24532df76c16SMatt Jacob 	 * First, clean any commands pending restart
24542df76c16SMatt Jacob 	 */
24558290ea90SAlexander Motin 	STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
24568290ea90SAlexander Motin 		if (IS_24XX(isp))
24578290ea90SAlexander Motin 			this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
24588290ea90SAlexander Motin 		else
24598290ea90SAlexander Motin 			this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
24602df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
24618290ea90SAlexander Motin 			isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
24628290ea90SAlexander Motin 			    ECMD_TERMINATE, 0);
24638290ea90SAlexander Motin 			isp_put_ntpd(isp, chan, ntp);
24648290ea90SAlexander Motin 			STAILQ_REMOVE(&tptr->restart_queue, ntp,
24658290ea90SAlexander Motin 			    inot_private_data, next);
24662df76c16SMatt Jacob 		}
24672df76c16SMatt Jacob 	}
24682df76c16SMatt Jacob 
24692df76c16SMatt Jacob 	/*
24702df76c16SMatt Jacob 	 * Now mark other ones dead as well.
24712df76c16SMatt Jacob 	 */
24728290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
24738290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
24748290ea90SAlexander Motin 		if (atp->lun != tptr->ts_lun)
24758290ea90SAlexander Motin 			continue;
24768290ea90SAlexander Motin 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
24772df76c16SMatt Jacob 			atp->dead = 1;
24782df76c16SMatt Jacob 	}
24792df76c16SMatt Jacob }
2480d81ba9d5SMatt Jacob #endif
2481d81ba9d5SMatt Jacob 
2482478f8a96SJustin T. Gibbs static void
24831dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2484478f8a96SJustin T. Gibbs {
2485478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
2486e95725cbSMatt Jacob 	int bus, tgt;
24879cd7268eSMatt Jacob 	ispsoftc_t *isp;
2488478f8a96SJustin T. Gibbs 
2489478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
24909cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
2491e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
2492e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
2493e95725cbSMatt Jacob 
2494478f8a96SJustin T. Gibbs 	switch (code) {
2495478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
2496ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
24971dae40ebSMatt Jacob 			uint16_t oflags, nflags;
24982df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
2499478f8a96SJustin T. Gibbs 
250041ed683eSMatt Jacob 			if (tgt >= 0) {
25019ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
25029ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
2503a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
2504478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
2505478f8a96SJustin T. Gibbs 				}
25069ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
25079ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
2508478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
25092df76c16SMatt Jacob 				sdp->update = 1;
25102df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
25119ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
2512478f8a96SJustin T. Gibbs 			}
251341ed683eSMatt Jacob 		}
2514478f8a96SJustin T. Gibbs 		break;
2515478f8a96SJustin T. Gibbs 	default:
25163c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2517478f8a96SJustin T. Gibbs 		break;
2518478f8a96SJustin T. Gibbs 	}
2519478f8a96SJustin T. Gibbs }
2520478f8a96SJustin T. Gibbs 
2521478f8a96SJustin T. Gibbs static void
2522c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
2523478f8a96SJustin T. Gibbs {
25249cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
2525126ec864SMatt Jacob 
25260e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
2527478f8a96SJustin T. Gibbs }
2528478f8a96SJustin T. Gibbs 
2529ab6c4b31SMatt Jacob 
25302df76c16SMatt Jacob static void
25312df76c16SMatt Jacob isp_watchdog(void *arg)
2532cc8df88bSMatt Jacob {
25332df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
25342df76c16SMatt Jacob 	ispsoftc_t *isp;
2535e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
2536b85389e1SMatt Jacob 
25372df76c16SMatt Jacob 	isp = XS_ISP(xs);
25382df76c16SMatt Jacob 
2539cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
2540e95725cbSMatt Jacob 
2541e95725cbSMatt Jacob 	/*
2542e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2543e95725cbSMatt Jacob 	 */
2544e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
25450e6bc811SAlexander Motin 		ISP_RUN_ISR(isp);
2546e95725cbSMatt Jacob 		ohandle = handle;
2547e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
2548e95725cbSMatt Jacob 	}
2549c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
25501fcf5debSMatt Jacob 		/*
2551c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
2552c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
2553c8b8a2c4SMatt Jacob 		 *
2554c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
2555c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
2556c8b8a2c4SMatt Jacob 		 * back separately.
25571fcf5debSMatt Jacob 		 */
2558c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2559c8b8a2c4SMatt Jacob 			return;
2560c8b8a2c4SMatt Jacob 		}
25611fcf5debSMatt Jacob 
25621fcf5debSMatt Jacob 		/*
2563c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
2564c8b8a2c4SMatt Jacob 		 * fact have been completed.
2565c8b8a2c4SMatt Jacob 		 */
2566c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
2567c8b8a2c4SMatt Jacob 
2568c8b8a2c4SMatt Jacob 		/*
2569c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
2570c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
2571c8b8a2c4SMatt Jacob 		 */
2572c8b8a2c4SMatt Jacob 		if (xs == NULL) {
2573c8b8a2c4SMatt Jacob 			return;
2574c8b8a2c4SMatt Jacob 		}
2575c8b8a2c4SMatt Jacob 
2576c8b8a2c4SMatt Jacob 		/*
2577c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
25781fcf5debSMatt Jacob 		 */
2579f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
2580f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
2581f6e75de2SMatt Jacob 		}
2582cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
2583c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
258457b6261fSKenneth D. Merry 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
2585cc8df88bSMatt Jacob 		isp_done(xs);
2586e95725cbSMatt Jacob 	} else {
2587e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
2588e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2589e95725cbSMatt Jacob 		} else {
2590e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2591e95725cbSMatt Jacob 		}
2592b85389e1SMatt Jacob 	}
2593f7c631bcSMatt Jacob }
2594f7c631bcSMatt Jacob 
2595f7c631bcSMatt Jacob static void
25965704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2597f7c631bcSMatt Jacob {
2598ffcf6651SMatt Jacob 	union ccb *ccb;
25992df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
26002df76c16SMatt Jacob 
2601ffcf6651SMatt Jacob 	/*
26020e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2603ffcf6651SMatt Jacob 	 */
26048008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
2605ffcf6651SMatt Jacob 	if (ccb == NULL) {
26062df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2607ffcf6651SMatt Jacob 		return;
2608ffcf6651SMatt Jacob 	}
2609e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2610e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2611ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2612ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
2613ffcf6651SMatt Jacob 		return;
2614ffcf6651SMatt Jacob 	}
2615ffcf6651SMatt Jacob 	xpt_rescan(ccb);
2616ffcf6651SMatt Jacob }
2617ffcf6651SMatt Jacob 
2618ffcf6651SMatt Jacob static void
26195704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2620ffcf6651SMatt Jacob {
2621ffcf6651SMatt Jacob 	struct cam_path *tp;
26222df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
26232df76c16SMatt Jacob 
26242df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2625ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
2626ffcf6651SMatt Jacob 		xpt_free_path(tp);
2627f7c631bcSMatt Jacob 	}
2628f7c631bcSMatt Jacob }
2629f7c631bcSMatt Jacob 
2630f7c631bcSMatt Jacob /*
2631f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
2632f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
2633f7c631bcSMatt Jacob  * gone away.
2634f7c631bcSMatt Jacob  *
2635f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
2636f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
2637f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
2638f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
2639f7c631bcSMatt Jacob  */
2640f7c631bcSMatt Jacob static void
2641f7c631bcSMatt Jacob isp_gdt(void *arg)
2642f7c631bcSMatt Jacob {
26432df76c16SMatt Jacob 	struct isp_fc *fc = arg;
2644de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2645de461933SMatt Jacob }
2646de461933SMatt Jacob 
2647de461933SMatt Jacob static void
2648de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
2649de461933SMatt Jacob {
2650de461933SMatt Jacob 	struct isp_fc *fc = arg;
26512df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
26522df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
2653f7c631bcSMatt Jacob 	fcportdb_t *lp;
2654e68eef14SAlexander Motin 	struct ac_contract ac;
2655e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2656766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
2657f7c631bcSMatt Jacob 
2658de461933SMatt Jacob 	ISP_LOCK(isp);
2659de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2660f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
26612df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
2662f7c631bcSMatt Jacob 
2663f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2664f7c631bcSMatt Jacob 			continue;
2665f7c631bcSMatt Jacob 		}
2666427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
2667427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
2668f7c631bcSMatt Jacob 			more_to_do++;
2669f7c631bcSMatt Jacob 			continue;
2670f7c631bcSMatt Jacob 		}
2671e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2672e68eef14SAlexander Motin 		if (lp->is_target) {
2673e68eef14SAlexander Motin 			lp->is_target = 0;
2674766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2675f7c631bcSMatt Jacob 		}
2676e68eef14SAlexander Motin 		if (lp->is_initiator) {
2677e68eef14SAlexander Motin 			lp->is_initiator = 0;
2678e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2679e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2680e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2681e68eef14SAlexander Motin 			adc->port = lp->portid;
2682d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2683e68eef14SAlexander Motin 			adc->arrived = 0;
2684e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2685e68eef14SAlexander Motin 		}
2686e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
2687e68eef14SAlexander Motin 	}
2688a01f5aebSMatt Jacob 	if (fc->ready) {
2689f7c631bcSMatt Jacob 		if (more_to_do) {
26902df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
2691f7c631bcSMatt Jacob 		} else {
2692de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
2693387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2694a01f5aebSMatt Jacob 		}
2695f7c631bcSMatt Jacob 	}
2696de461933SMatt Jacob 	ISP_UNLOCK(isp);
2697f7c631bcSMatt Jacob }
2698f7c631bcSMatt Jacob 
2699f7c631bcSMatt Jacob /*
2700e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
2701e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
2702e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
2703f7c631bcSMatt Jacob  *
2704f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
2705f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
2706f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2707f7c631bcSMatt Jacob  */
2708f7c631bcSMatt Jacob static void
2709e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
2710f7c631bcSMatt Jacob {
2711e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2712e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2713e561aa79SAlexander Motin 
2714e561aa79SAlexander Motin 	if (fc->loop_down_time)
2715e561aa79SAlexander Motin 		return;
2716e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2717e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
2718e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
2719e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
2720e561aa79SAlexander Motin 	wakeup(fc);
2721de461933SMatt Jacob }
2722de461933SMatt Jacob 
2723de461933SMatt Jacob static void
2724e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
2725de461933SMatt Jacob {
2726e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2727e561aa79SAlexander Motin 
2728e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2729e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
2730e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2731e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2732e561aa79SAlexander Motin }
2733e561aa79SAlexander Motin 
2734e561aa79SAlexander Motin static void
2735e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
2736e561aa79SAlexander Motin {
2737e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2738e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2739f7c631bcSMatt Jacob 	fcportdb_t *lp;
2740e68eef14SAlexander Motin 	struct ac_contract ac;
2741e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2742766a65a5SAlexander Motin 	int dbidx, i;
2743f7c631bcSMatt Jacob 
2744e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
27450a70657fSMatt Jacob 
2746f7c631bcSMatt Jacob 	/*
2747f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
2748f7c631bcSMatt Jacob 	 */
2749f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2750e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
2751f7c631bcSMatt Jacob 
2752e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
2753f7c631bcSMatt Jacob 			continue;
2754f7c631bcSMatt Jacob 
2755e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
2756e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
2757e95725cbSMatt Jacob 
2758970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2759e95725cbSMatt Jacob 				continue;
2760e95725cbSMatt Jacob 			}
2761e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2762e95725cbSMatt Jacob 				continue;
2763e95725cbSMatt Jacob                         }
2764766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
2765e95725cbSMatt Jacob 				continue;
2766e95725cbSMatt Jacob 			}
27676af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
27686af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
27696af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
277057b6261fSKenneth D. Merry 
277157b6261fSKenneth D. Merry 			/*
277257b6261fSKenneth D. Merry 			 * Just like in isp_watchdog, abort the outstanding
277357b6261fSKenneth D. Merry 			 * command or immediately free its resources if it is
277457b6261fSKenneth D. Merry 			 * not active
277557b6261fSKenneth D. Merry 			 */
277657b6261fSKenneth D. Merry 			if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
277757b6261fSKenneth D. Merry 				continue;
277857b6261fSKenneth D. Merry 			}
277957b6261fSKenneth D. Merry 
278057b6261fSKenneth D. Merry 			if (XS_XFRLEN(xs)) {
278157b6261fSKenneth D. Merry 				ISP_DMAFREE(isp, xs, isp->isp_xflist[i].handle);
278257b6261fSKenneth D. Merry 			}
278357b6261fSKenneth D. Merry 			isp_destroy_handle(isp, isp->isp_xflist[i].handle);
278457b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx could not be aborted and was destroyed",
278557b6261fSKenneth D. Merry 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
278657b6261fSKenneth D. Merry 			    (uintmax_t)XS_LUN(xs));
278757b6261fSKenneth D. Merry 			XS_SETERR(xs, HBA_BUSRESET);
278857b6261fSKenneth D. Merry 			isp_done(xs);
2789e95725cbSMatt Jacob 		}
2790e95725cbSMatt Jacob 
2791e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2792e68eef14SAlexander Motin 		if (lp->is_target) {
2793e68eef14SAlexander Motin 			lp->is_target = 0;
2794766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2795f7c631bcSMatt Jacob 		}
2796e68eef14SAlexander Motin 		if (lp->is_initiator) {
2797e68eef14SAlexander Motin 			lp->is_initiator = 0;
2798e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2799e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2800e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2801e68eef14SAlexander Motin 			adc->port = lp->portid;
2802d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2803e68eef14SAlexander Motin 			adc->arrived = 0;
2804e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2805e95725cbSMatt Jacob 		}
2806e68eef14SAlexander Motin 	}
2807e68eef14SAlexander Motin 
2808e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2809e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2810cc8df88bSMatt Jacob }
2811cc8df88bSMatt Jacob 
28125d571944SMatt Jacob static void
28135d571944SMatt Jacob isp_kthread(void *arg)
28145d571944SMatt Jacob {
28152df76c16SMatt Jacob 	struct isp_fc *fc = arg;
28162df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
28172df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
2818e561aa79SAlexander Motin 	int slp = 0, d;
2819e561aa79SAlexander Motin 	int lb, lim;
2820a01f5aebSMatt Jacob 
282113d9c921SAlexander Motin 	ISP_LOCK(isp);
28221d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
2823e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2824e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
28252df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
2826e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2827e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
2828e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
28292df76c16SMatt Jacob 
28302df76c16SMatt Jacob 		/*
28312df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
28322df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
28332df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
28342df76c16SMatt Jacob 		 * check again.
28352df76c16SMatt Jacob 		 *
28362df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
28372df76c16SMatt Jacob 		 */
2838e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
2839e561aa79SAlexander Motin 			slp = 0;
284010365e5aSMatt Jacob 		} else {
284110365e5aSMatt Jacob 			/*
284210365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
2843f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
2844f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
2845f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
284610365e5aSMatt Jacob 			 */
2847e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
2848f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
2849e561aa79SAlexander Motin 			else
28502df76c16SMatt Jacob 				lim = fc->loop_down_limit;
2851e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
2852e561aa79SAlexander Motin 			if (d >= lim)
2853f7c631bcSMatt Jacob 				slp = 0;
2854e561aa79SAlexander Motin 			else if (d < 10)
285510365e5aSMatt Jacob 				slp = 1;
2856e561aa79SAlexander Motin 			else if (d < 30)
2857f7c631bcSMatt Jacob 				slp = 5;
2858e561aa79SAlexander Motin 			else if (d < 60)
2859f7c631bcSMatt Jacob 				slp = 10;
2860e561aa79SAlexander Motin 			else if (d < 120)
2861f7c631bcSMatt Jacob 				slp = 20;
2862e561aa79SAlexander Motin 			else
2863f7c631bcSMatt Jacob 				slp = 30;
2864f44257c2SMatt Jacob 		}
286510365e5aSMatt Jacob 
2866e561aa79SAlexander Motin 		if (slp == 0) {
2867e561aa79SAlexander Motin 			if (lb == LOOP_READY)
2868e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
2869387d8239SMatt Jacob 			else
2870e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
28715d571944SMatt Jacob 		}
2872fdeb9f2fSMatt Jacob 
2873e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2874e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
287513d9c921SAlexander Motin 		msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz);
28765d571944SMatt Jacob 	}
28771d0a1de2SWill Andrews 	fc->num_threads -= 1;
287813d9c921SAlexander Motin 	ISP_UNLOCK(isp);
28791d0a1de2SWill Andrews 	kthread_exit();
28805d571944SMatt Jacob }
28815d571944SMatt Jacob 
28828290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
28838290ea90SAlexander Motin static void
28848290ea90SAlexander Motin isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
28858290ea90SAlexander Motin {
28868290ea90SAlexander Motin 	atio_private_data_t *atp;
28878290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
28888290ea90SAlexander Motin 	struct ccb_hdr *sccb;
28898290ea90SAlexander Motin 	tstate_t *tptr;
28908290ea90SAlexander Motin 
28918290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
28928290ea90SAlexander Motin 	if (tptr != NULL) {
28938290ea90SAlexander Motin 		/* Search for the ATIO among queueued. */
28948290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
28958290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
28968290ea90SAlexander Motin 				continue;
28978290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2898d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2899d10e4cd0SAlexander Motin 			    "Abort FREE ATIO\n");
29008290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
29018290ea90SAlexander Motin 			xpt_done(accb);
29028290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
29038290ea90SAlexander Motin 			return;
29048290ea90SAlexander Motin 		}
29058290ea90SAlexander Motin 	}
29068290ea90SAlexander Motin 
29078290ea90SAlexander Motin 	/* Search for the ATIO among running. */
29088290ea90SAlexander Motin 	atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
29098290ea90SAlexander Motin 	if (atp != NULL) {
29103f072d69SAlexander Motin 		/* Send TERMINATE to firmware. */
29113f072d69SAlexander Motin 		if (!atp->dead && IS_24XX(isp)) {
29123f072d69SAlexander Motin 			uint8_t storage[QENTRY_LEN];
29133f072d69SAlexander Motin 			ct7_entry_t *cto = (ct7_entry_t *) storage;
29143f072d69SAlexander Motin 
29153f072d69SAlexander Motin 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
29163f072d69SAlexander Motin 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
29173f072d69SAlexander Motin 			cto->ct_header.rqs_entry_count = 1;
29183f072d69SAlexander Motin 			cto->ct_nphdl = atp->nphdl;
29193f072d69SAlexander Motin 			cto->ct_rxid = atp->tag;
29203f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
29213f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
29223f072d69SAlexander Motin 			cto->ct_oxid = atp->oxid;
29233f072d69SAlexander Motin 			cto->ct_vpidx = XS_CHANNEL(accb);
29243f072d69SAlexander Motin 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
29253f072d69SAlexander Motin 			isp_target_put_entry(isp, cto);
29263f072d69SAlexander Motin 		}
29278290ea90SAlexander Motin 		isp_put_atpd(isp, XS_CHANNEL(accb), atp);
29288290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
29298290ea90SAlexander Motin 	} else {
29308290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
29318290ea90SAlexander Motin 	}
29328290ea90SAlexander Motin }
29338290ea90SAlexander Motin 
29348290ea90SAlexander Motin static void
29358290ea90SAlexander Motin isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
29368290ea90SAlexander Motin {
29378290ea90SAlexander Motin 	inot_private_data_t *ntp;
29388290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
29398290ea90SAlexander Motin 	struct ccb_hdr *sccb;
29408290ea90SAlexander Motin 	tstate_t *tptr;
29418290ea90SAlexander Motin 
29428290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
29438290ea90SAlexander Motin 	if (tptr != NULL) {
29448290ea90SAlexander Motin 		/* Search for the INOT among queueued. */
29458290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
29468290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
29478290ea90SAlexander Motin 				continue;
29488290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2949d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2950d10e4cd0SAlexander Motin 			    "Abort FREE INOT\n");
29518290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
29528290ea90SAlexander Motin 			xpt_done(accb);
29538290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
29548290ea90SAlexander Motin 			return;
29558290ea90SAlexander Motin 		}
29568290ea90SAlexander Motin 	}
29578290ea90SAlexander Motin 
29588290ea90SAlexander Motin 	/* Search for the INOT among running. */
29598290ea90SAlexander Motin 	ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
29608290ea90SAlexander Motin 	if (ntp != NULL) {
296144a2a27aSAlexander Motin 		if (ntp->nt.nt_need_ack) {
296244a2a27aSAlexander Motin 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
296344a2a27aSAlexander Motin 			    ntp->nt.nt_lreserved);
296444a2a27aSAlexander Motin 		}
29658290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
29668290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
29678290ea90SAlexander Motin 	} else {
29688290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
29698290ea90SAlexander Motin 		return;
29708290ea90SAlexander Motin 	}
29718290ea90SAlexander Motin }
29728290ea90SAlexander Motin #endif
29738290ea90SAlexander Motin 
2974cc8df88bSMatt Jacob static void
2975c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
2976478f8a96SJustin T. Gibbs {
297731c161a6SAlexander Motin 	int bus, tgt, error;
29789cd7268eSMatt Jacob 	ispsoftc_t *isp;
29794663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
298031c161a6SAlexander Motin 	sbintime_t ts;
2981478f8a96SJustin T. Gibbs 
2982478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2983478f8a96SJustin T. Gibbs 
29849cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
298513d9c921SAlexander Motin 	ISP_ASSERT_LOCKED(isp);
298613d9c921SAlexander Motin 	bus = cam_sim_bus(sim);
29875cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
29885cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
29892df76c16SMatt Jacob 
2990478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
2991478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2992478f8a96SJustin T. Gibbs 		/*
2993478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
2994478f8a96SJustin T. Gibbs 		 */
2995478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2996478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2997478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
29985cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
2999478f8a96SJustin T. Gibbs 				break;
3000478f8a96SJustin T. Gibbs 			}
3001478f8a96SJustin T. Gibbs 		}
3002387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
30030470d791SMatt Jacob #ifdef	DIAGNOSTIC
30046af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3005dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3006478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
30076af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
30086af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3009dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3010478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3011478f8a96SJustin T. Gibbs 		}
3012478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3013478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3014478f8a96SJustin T. Gibbs 			break;
3015478f8a96SJustin T. Gibbs 		}
30160470d791SMatt Jacob #endif
30170a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
30180a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
30190a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
30200a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30212df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
30225cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30230a70657fSMatt Jacob 			xpt_done(ccb);
30240a70657fSMatt Jacob 			break;
30250a70657fSMatt Jacob 		}
3026b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
30270470d791SMatt Jacob 		switch (error) {
3028478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3029478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
303031c161a6SAlexander Motin 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
30310a70657fSMatt Jacob 				break;
303231c161a6SAlexander Motin 			/* Give firmware extra 10s to handle timeout. */
303331c161a6SAlexander Motin 			ts = SBT_1MS * ccb->ccb_h.timeout + 10 * SBT_1S;
303431c161a6SAlexander Motin 			callout_reset_sbt(&PISP_PCMD(ccb)->wdog, ts, 0,
303531c161a6SAlexander Motin 			    isp_watchdog, ccb, 0);
3036478f8a96SJustin T. Gibbs 			break;
30370470d791SMatt Jacob 		case CMD_RQLATER:
30386af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
30396af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3040f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30412df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3042387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30430a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3044478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3045478f8a96SJustin T. Gibbs 			break;
30460470d791SMatt Jacob 		case CMD_EAGAIN:
30470a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
30482df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30492df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3050387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3051478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3052478f8a96SJustin T. Gibbs 			break;
30530470d791SMatt Jacob 		case CMD_COMPLETE:
30540470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
30550470d791SMatt Jacob 			break;
30560470d791SMatt Jacob 		default:
30572df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3058387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30590a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
30600470d791SMatt Jacob 			xpt_done(ccb);
3061478f8a96SJustin T. Gibbs 		}
3062478f8a96SJustin T. Gibbs 		break;
3063478f8a96SJustin T. Gibbs 
3064d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
30652df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
30662df76c16SMatt Jacob 		if (ccb->cel.enable) {
30672df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
30682df76c16SMatt Jacob 		} else {
30692df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
307067ff51f1SMatt Jacob 		}
307167ff51f1SMatt Jacob 		break;
30722df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3073d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3074d81ba9d5SMatt Jacob 	{
30752df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3076d81ba9d5SMatt Jacob 		if (tptr == NULL) {
30772df76c16SMatt Jacob 			const char *str;
30782df76c16SMatt Jacob 
30798290ea90SAlexander Motin 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
30802df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
30818290ea90SAlexander Motin 			else
30822df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
30838290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
30845c0b756aSAlexander Motin 			    "%s: no state pointer found for %s\n",
30858290ea90SAlexander Motin 			    __func__, str);
30862df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
30875c0b756aSAlexander Motin 			xpt_done(ccb);
3088d81ba9d5SMatt Jacob 			break;
3089d81ba9d5SMatt Jacob 		}
3090387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3091387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3092570c7a3fSMatt Jacob 
3093d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
30948290ea90SAlexander Motin 			ccb->atio.tag_id = 0;
30952df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
30968290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3097d10e4cd0SAlexander Motin 			    "Put FREE ATIO\n");
30982df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
30998290ea90SAlexander Motin 			ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
31002df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
31018290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3102d10e4cd0SAlexander Motin 			    "Put FREE INOT\n");
3103d81ba9d5SMatt Jacob 		}
3104d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3105d81ba9d5SMatt Jacob 		break;
3106d81ba9d5SMatt Jacob 	}
31072df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3108d81ba9d5SMatt Jacob 	{
31092df76c16SMatt Jacob 		inot_private_data_t *ntp;
31102df76c16SMatt Jacob 
31112df76c16SMatt Jacob 		/*
31122df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
31132df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
31142df76c16SMatt Jacob 		 */
31152df76c16SMatt Jacob 		/*
31162df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
31172df76c16SMatt Jacob 		 */
31182df76c16SMatt 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);
31198290ea90SAlexander Motin 		ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
31202df76c16SMatt Jacob 		if (ntp == NULL) {
31212df76c16SMatt 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__,
31222df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
31232df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
31242df76c16SMatt Jacob 			xpt_done(ccb);
3125d81ba9d5SMatt Jacob 			break;
3126d81ba9d5SMatt Jacob 		}
31278290ea90SAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
312896b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
31292df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
31302df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3131387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3132387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
31332df76c16SMatt Jacob 			break;
31342df76c16SMatt Jacob 		}
31358290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
31362df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
31372df76c16SMatt 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);
31382df76c16SMatt Jacob 		xpt_done(ccb);
31392df76c16SMatt Jacob 		break;
31402df76c16SMatt Jacob 	}
31412df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3142387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
31432df76c16SMatt Jacob 		break;
3144d81ba9d5SMatt Jacob #endif
3145478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3146d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3147d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3148d81ba9d5SMatt Jacob 
31492df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3150478f8a96SJustin T. Gibbs 		if (error) {
3151478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3152478f8a96SJustin T. Gibbs 		} else {
3153b44e442eSWill Andrews 			/*
3154b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3155b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3156b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3157b44e442eSWill Andrews 			 */
315821daf914SAlexander Motin 			if (IS_FC(isp))
315921daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3160b44e442eSWill Andrews 
3161478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3162478f8a96SJustin T. Gibbs 		}
3163478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3164478f8a96SJustin T. Gibbs 		break;
3165478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3166d81ba9d5SMatt Jacob 	{
3167d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3168d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3169d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3170d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
31718290ea90SAlexander Motin 			isp_abort_atio(isp, ccb);
31728290ea90SAlexander Motin 			break;
31738290ea90SAlexander Motin 		case XPT_IMMEDIATE_NOTIFY:
31748290ea90SAlexander Motin 			isp_abort_inot(isp, ccb);
3175d81ba9d5SMatt Jacob 			break;
3176d81ba9d5SMatt Jacob #endif
3177d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3178950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3179478f8a96SJustin T. Gibbs 			if (error) {
3180d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3181478f8a96SJustin T. Gibbs 			} else {
3182478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3183478f8a96SJustin T. Gibbs 			}
3184d81ba9d5SMatt Jacob 			break;
3185d81ba9d5SMatt Jacob 		default:
3186d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3187d81ba9d5SMatt Jacob 			break;
3188d81ba9d5SMatt Jacob 		}
31891c0a1eb2SMatt Jacob 		/*
31901c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
31911c0a1eb2SMatt Jacob 		 * complete when control is returned.
31921c0a1eb2SMatt Jacob 		 */
3193478f8a96SJustin T. Gibbs 		break;
3194d81ba9d5SMatt Jacob 	}
3195ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3196478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3197478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
31989ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
31999ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
32009ce9bdafSMatt Jacob 			xpt_done(ccb);
32019ce9bdafSMatt Jacob 			break;
32029ce9bdafSMatt Jacob 		}
3203478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
32042df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
32052df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32062df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
32072df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
32081dae40ebSMatt Jacob 			uint16_t *dptr;
3209ab163f5fSMatt Jacob 
3210b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3211b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3212b61386a4SMatt Jacob 				xpt_done(ccb);
3213b61386a4SMatt Jacob 				break;
3214b61386a4SMatt Jacob 			}
3215b61386a4SMatt Jacob 
3216ab163f5fSMatt Jacob 			/*
32179ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3218ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3219ab163f5fSMatt Jacob 			 * vectored to that location.
3220ab163f5fSMatt Jacob 			 */
32219ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3222ab163f5fSMatt Jacob 
3223ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3224ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3225ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3226ab163f5fSMatt Jacob 				else
3227ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3228ab163f5fSMatt Jacob 			}
3229ab163f5fSMatt Jacob 
3230ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3231ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3232ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3233ab163f5fSMatt Jacob 				else
3234ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3235ab163f5fSMatt Jacob 			}
3236ab163f5fSMatt Jacob 
3237ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3238ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3239ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3240ab163f5fSMatt Jacob 				else
3241ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3242ab163f5fSMatt Jacob 			}
3243ab163f5fSMatt Jacob 
3244ab163f5fSMatt Jacob 			/*
3245ab163f5fSMatt Jacob 			 * XXX: FIX ME
3246ab163f5fSMatt Jacob 			 */
32472df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3248ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
32499ce9bdafSMatt Jacob 				/*
32509ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
32519ce9bdafSMatt Jacob 				 */
32522df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
32532df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3254ab163f5fSMatt Jacob 			} else {
3255ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3256ab163f5fSMatt Jacob 			}
3257123055f0SNathan 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,
32582df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3259478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
32602df76c16SMatt Jacob 			sdp->update = 1;
3261478f8a96SJustin T. Gibbs 		}
3262478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3263478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3264478f8a96SJustin T. Gibbs 		break;
3265478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3266478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3267478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3268ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
32692df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
32702df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32712df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3272478f8a96SJustin T. Gibbs 
3273ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3274ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3275ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3276ab163f5fSMatt Jacob 			cts->transport_version = 0;
3277ab163f5fSMatt Jacob 
32789b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
32799b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3280ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3281ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3282fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3283766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3284766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3285ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3286ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3287ab163f5fSMatt Jacob 				fc->port = lp->portid;
32882df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3289ab163f5fSMatt Jacob 			}
3290ab163f5fSMatt Jacob 		} else {
32912df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32922df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
32932df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
32941dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3295ab163f5fSMatt Jacob 
3296ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
329783ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
32982df76c16SMatt Jacob 				sdp->update = 1;
32992df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
33009ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
33019ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
33029ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
33034394c92fSMatt Jacob 			} else {
33049ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
33059ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
33069ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
33074394c92fSMatt Jacob 			}
3308478f8a96SJustin T. Gibbs 
3309ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3310ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3311ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3312ab163f5fSMatt Jacob 			cts->transport_version = 2;
3313ab163f5fSMatt Jacob 
3314b61386a4SMatt Jacob 			spi->valid = 0;
3315b61386a4SMatt Jacob 			scsi->valid = 0;
3316b61386a4SMatt Jacob 			spi->flags = 0;
3317b61386a4SMatt Jacob 			scsi->flags = 0;
3318ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3319ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3320ab163f5fSMatt Jacob 			}
33219ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3322ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3323ab163f5fSMatt Jacob 				spi->sync_period = pval;
3324b61386a4SMatt Jacob 			} else {
3325b61386a4SMatt Jacob 				spi->sync_offset = 0;
3326b61386a4SMatt Jacob 				spi->sync_period = 0;
3327b61386a4SMatt Jacob 			}
3328ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3329ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3330ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3331ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3332ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3333ab163f5fSMatt Jacob 			} else {
3334ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3335ab163f5fSMatt Jacob 			}
3336ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3337ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3338b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3339b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3340b61386a4SMatt Jacob 				}
3341ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3342ab163f5fSMatt Jacob 			}
3343123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3344123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3345478f8a96SJustin T. Gibbs 		}
3346478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3347478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3348478f8a96SJustin T. Gibbs 		break;
3349478f8a96SJustin T. Gibbs 
3350478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
33512df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
33522df76c16SMatt Jacob 		xpt_done(ccb);
33532df76c16SMatt Jacob 		break;
3354478f8a96SJustin T. Gibbs 
3355478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
33562df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
33572df76c16SMatt Jacob 		if (error) {
3358478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
33592df76c16SMatt Jacob 			xpt_done(ccb);
33602df76c16SMatt Jacob 			break;
33612df76c16SMatt Jacob 		}
33620a70657fSMatt Jacob 		if (bootverbose) {
33632df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
33640a70657fSMatt Jacob 		}
33652df76c16SMatt Jacob 		if (IS_FC(isp)) {
33662df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
33672df76c16SMatt Jacob 		} else {
33682df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
33692df76c16SMatt Jacob 		}
3370478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3371478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3372478f8a96SJustin T. Gibbs 		break;
3373478f8a96SJustin T. Gibbs 
3374478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3375478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3376478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3377478f8a96SJustin T. Gibbs 		break;
3378478f8a96SJustin T. Gibbs 
33792df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
33802df76c16SMatt Jacob 	{
33812df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
33822df76c16SMatt Jacob 		fcparam *fcp;
33832df76c16SMatt Jacob 
33842df76c16SMatt Jacob 		if (!IS_FC(isp)) {
33852df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
33862df76c16SMatt Jacob 			xpt_done(ccb);
33872df76c16SMatt Jacob 			break;
33882df76c16SMatt Jacob 		}
33892df76c16SMatt Jacob 
33902df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
33912df76c16SMatt Jacob 
33922df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
33932df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
33942df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
33952df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
33962df76c16SMatt Jacob 		}
33972df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
33982df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
33992df76c16SMatt Jacob 			int rchange = 0;
34002df76c16SMatt Jacob 			int newrole = 0;
34012df76c16SMatt Jacob 
34022df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
34032df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
34042df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
34052df76c16SMatt Jacob 					rchange = 1;
34062df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
34072df76c16SMatt Jacob 				}
34082df76c16SMatt Jacob 				break;
34092df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
34102df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
34112df76c16SMatt Jacob 					rchange = 1;
34122df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
34132df76c16SMatt Jacob 				}
34142df76c16SMatt Jacob 				break;
34152df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
34162df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
34172df76c16SMatt Jacob 					rchange = 1;
34182df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
34192df76c16SMatt Jacob 				}
34202df76c16SMatt Jacob 				break;
34212df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
34222df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
34232df76c16SMatt Jacob 					rchange = 1;
34242df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
34252df76c16SMatt Jacob 				}
34262df76c16SMatt Jacob 				break;
34272df76c16SMatt Jacob 			}
34282df76c16SMatt Jacob 			if (rchange) {
3429e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
34306bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
34316bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
34322df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3433387d8239SMatt Jacob 					xpt_done(ccb);
3434387d8239SMatt Jacob 					break;
34352df76c16SMatt Jacob 				}
34362df76c16SMatt Jacob 			}
34372df76c16SMatt Jacob 		}
34382df76c16SMatt Jacob 		xpt_done(ccb);
34392df76c16SMatt Jacob 		break;
34402df76c16SMatt Jacob 	}
3441a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3442e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
34432df76c16SMatt Jacob 	{
34442df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
34452df76c16SMatt Jacob 
34462df76c16SMatt Jacob 		if (IS_FC(isp)) {
34472df76c16SMatt Jacob 			fcparam *fcp;
34482df76c16SMatt Jacob 
34492df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
34502df76c16SMatt Jacob 
34512df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
34522df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
34532df76c16SMatt Jacob 			switch (fcp->role) {
34542df76c16SMatt Jacob 			case ISP_ROLE_NONE:
34552df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
34562df76c16SMatt Jacob 				break;
34572df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
34582df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
34592df76c16SMatt Jacob 				break;
34602df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
34612df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
34622df76c16SMatt Jacob 				break;
34632df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
34642df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
34652df76c16SMatt Jacob 				break;
34662df76c16SMatt Jacob 			}
34672df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
34682df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
34692df76c16SMatt Jacob 		} else {
34702df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
34712df76c16SMatt Jacob 		}
34722df76c16SMatt Jacob 		xpt_done(ccb);
34732df76c16SMatt Jacob 		break;
34742df76c16SMatt Jacob 	}
3475478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
3476478f8a96SJustin T. Gibbs 	{
3477478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
3478478f8a96SJustin T. Gibbs 
3479478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
3480d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
34813e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3482a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
34833e6deb33SAlexander Motin 		else
3484d81ba9d5SMatt Jacob #endif
34853e6deb33SAlexander Motin 			cpi->target_sprt = 0;
3486478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
34870470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
34886af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
34896af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
34900470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
34917e31684eSAlexander Motin 		if (sizeof (bus_size_t) > 4)
34927bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
34937bf825d1SKenneth D. Merry 		else
34947bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
34957bf825d1SKenneth D. Merry 
34964394c92fSMatt Jacob 		if (IS_FC(isp)) {
34972df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
34982df76c16SMatt Jacob 
3499ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3500229203afSAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
35012df76c16SMatt Jacob 
35020470d791SMatt Jacob 			/*
35030470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
35040470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
35050470d791SMatt Jacob 			 */
35060470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
35070470d791SMatt Jacob 
35089deea857SKenneth D. Merry 			/*
35092df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
35109deea857SKenneth D. Merry 			 */
3511e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
3512e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
3513e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
35142df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
35152df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
35162df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
35172df76c16SMatt Jacob 			} else {
35182df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
35192df76c16SMatt Jacob 			}
35200470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
3521ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
3522805e1f82SMatt Jacob 			cpi->transport_version = 0;
35232df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
35242df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
35252df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
35262df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3527478f8a96SJustin T. Gibbs 		} else {
35282df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
35290470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3530ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
3531ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
35329deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
3533ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
3534805e1f82SMatt Jacob 			cpi->transport_version = 2;
3535478f8a96SJustin T. Gibbs 		}
3536ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
3537ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
35384195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
35394195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
35404195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3541478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
3542478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
3543478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3544478f8a96SJustin T. Gibbs 		break;
3545478f8a96SJustin T. Gibbs 	}
3546478f8a96SJustin T. Gibbs 	default:
3547478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3548478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3549478f8a96SJustin T. Gibbs 		break;
3550478f8a96SJustin T. Gibbs 	}
3551478f8a96SJustin T. Gibbs }
3552d3a9eb2eSMatt Jacob 
3553d3a9eb2eSMatt Jacob void
35542df76c16SMatt Jacob isp_done(XS_T *sccb)
3555d3a9eb2eSMatt Jacob {
35569cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
3557e95725cbSMatt Jacob 	uint32_t status;
3558d3a9eb2eSMatt Jacob 
3559d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
3560d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
3561b85389e1SMatt Jacob 
35622df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3563d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
35642df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
356592a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
356692a1e549SMatt Jacob 		} else {
3567d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3568d3a9eb2eSMatt Jacob 		}
356992a1e549SMatt Jacob 	}
3570b85389e1SMatt Jacob 
35710470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3572e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
3573ab23521aSAlexander Motin 	if (status != CAM_REQ_CMP &&
3574ab23521aSAlexander Motin 	    (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3575d3a9eb2eSMatt Jacob 		sccb->ccb_h.status |= CAM_DEV_QFRZN;
35760470d791SMatt Jacob 		xpt_freeze_devq(sccb->ccb_h.path, 1);
3577d3a9eb2eSMatt Jacob 	}
3578b85389e1SMatt Jacob 
35795cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
3580387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
35810ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
35820a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
35835cbe3e8eSAlexander Motin 	}
3584d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
3585d3a9eb2eSMatt Jacob }
3586d3a9eb2eSMatt Jacob 
35872df76c16SMatt Jacob void
35882df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3589cbf57b47SMatt Jacob {
3590e68eef14SAlexander Motin 	int bus;
3591e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3592387d8239SMatt Jacob 	char buf[64];
3593f7c631bcSMatt Jacob 	char *msg = NULL;
359457b6261fSKenneth D. Merry 	target_id_t tgt = 0;
359510365e5aSMatt Jacob 	fcportdb_t *lp;
3596a01f5aebSMatt Jacob 	struct isp_fc *fc;
359710365e5aSMatt Jacob 	struct cam_path *tmppath;
3598e68eef14SAlexander Motin 	struct ac_contract ac;
3599e68eef14SAlexander Motin 	struct ac_device_changed *adc;
36002df76c16SMatt Jacob 	va_list ap;
360110365e5aSMatt Jacob 
3602cbf57b47SMatt Jacob 	switch (cmd) {
3603cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
36040470d791SMatt Jacob 	{
3605ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
3606ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
3607cbf57b47SMatt Jacob 		int flags, tgt;
36082df76c16SMatt Jacob 		sdparam *sdp;
3609ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
3610cbf57b47SMatt Jacob 
361129f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
3612ab163f5fSMatt Jacob 
36132df76c16SMatt Jacob 		va_start(ap, cmd);
36142df76c16SMatt Jacob 		bus = va_arg(ap, int);
36152df76c16SMatt Jacob 		tgt = va_arg(ap, int);
36162df76c16SMatt Jacob 		va_end(ap);
36172df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
36182df76c16SMatt Jacob 
36192df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
36202df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3621cbf57b47SMatt Jacob 			break;
3622cbf57b47SMatt Jacob 		}
36239ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
3624ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
3625ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
3626ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
3627ab163f5fSMatt Jacob 
3628ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
3629ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
3630ab163f5fSMatt Jacob 
3631ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
3632ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
3633ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3634ab163f5fSMatt Jacob 		}
3635ab163f5fSMatt Jacob 
3636cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
3637ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
3638ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3639ab163f5fSMatt Jacob 		}
3640ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3641ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
3642ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3643ab163f5fSMatt Jacob 		} else {
3644ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3645ab163f5fSMatt Jacob 		}
3646ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
3647ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3648ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
36499ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
36509ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3651ab163f5fSMatt Jacob 		}
36522df76c16SMatt 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);
3653ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3654ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3655cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
3656cbf57b47SMatt Jacob 		break;
36570470d791SMatt Jacob 	}
365857c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
36592df76c16SMatt Jacob 	{
36602df76c16SMatt Jacob 		va_start(ap, cmd);
36612df76c16SMatt Jacob 		bus = va_arg(ap, int);
36622df76c16SMatt Jacob 		va_end(ap);
36632df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
36642df76c16SMatt Jacob 		if (IS_FC(isp)) {
36652df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
36662df76c16SMatt Jacob 		} else {
36672df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
366857c801f5SMatt Jacob 		}
366957c801f5SMatt Jacob 		break;
36702df76c16SMatt Jacob 	}
367157b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_RESET:
367257b6261fSKenneth D. Merry 	{
367357b6261fSKenneth D. Merry 		uint16_t lipp;
367457b6261fSKenneth D. Merry 		fcparam *fcp;
367557b6261fSKenneth D. Merry 		va_start(ap, cmd);
367657b6261fSKenneth D. Merry 		bus = va_arg(ap, int);
367757b6261fSKenneth D. Merry 		va_end(ap);
367857b6261fSKenneth D. Merry 
367957b6261fSKenneth D. Merry 		lipp = ISP_READ(isp, OUTMAILBOX1);
368057b6261fSKenneth D. Merry 		fcp = FCPARAM(isp, bus);
368157b6261fSKenneth D. Merry 
368257b6261fSKenneth D. Merry 		isp_prt(isp, ISP_LOGINFO, "Chan %d LOOP Reset, LIP primitive %x", bus, lipp);
368357b6261fSKenneth D. Merry 		/*
368457b6261fSKenneth D. Merry 		 * Per FCP-4, a Reset LIP should result in a CRN reset. Other
368557b6261fSKenneth D. Merry 		 * LIPs and loop up/down events should never reset the CRN. For
368657b6261fSKenneth D. Merry 		 * an as of yet unknown reason, 24xx series cards (and
368757b6261fSKenneth D. Merry 		 * potentially others) can interrupt with a LIP Reset status
368857b6261fSKenneth D. Merry 		 * when no LIP reset came down the wire. Additionally, the LIP
368957b6261fSKenneth D. Merry 		 * primitive accompanying this status would not be a valid LIP
369057b6261fSKenneth D. Merry 		 * Reset primitive, but some variation of an invalid AL_PA
369157b6261fSKenneth D. Merry 		 * LIP. As a result, we have to verify the AL_PD in the LIP
369257b6261fSKenneth D. Merry 		 * addresses our port before blindly resetting.
369357b6261fSKenneth D. Merry 		*/
369457b6261fSKenneth D. Merry 		if (FCP_IS_DEST_ALPD(fcp, (lipp & 0x00FF)))
369557b6261fSKenneth D. Merry 			isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
369657b6261fSKenneth D. Merry 		isp_loop_changed(isp, bus);
369757b6261fSKenneth D. Merry 		break;
369857b6261fSKenneth D. Merry 	}
36995d571944SMatt Jacob 	case ISPASYNC_LIP:
370037a7daacSAlexander Motin 		if (msg == NULL)
3701f7c631bcSMatt Jacob 			msg = "LIP Received";
3702f7c631bcSMatt Jacob 		/* FALLTHROUGH */
370357c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
370437a7daacSAlexander Motin 		if (msg == NULL)
3705f7c631bcSMatt Jacob 			msg = "LOOP Down";
370657b6261fSKenneth D. Merry 		/* FALLTHROUGH */
370757b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_UP:
370857b6261fSKenneth D. Merry 		if (msg == NULL)
370957b6261fSKenneth D. Merry 			msg = "LOOP Up";
37102df76c16SMatt Jacob 		va_start(ap, cmd);
37112df76c16SMatt Jacob 		bus = va_arg(ap, int);
37122df76c16SMatt Jacob 		va_end(ap);
3713e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
3714e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
371557c801f5SMatt Jacob 		break;
371610365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
37172df76c16SMatt Jacob 		va_start(ap, cmd);
37182df76c16SMatt Jacob 		bus = va_arg(ap, int);
37192df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
37202df76c16SMatt Jacob 		va_end(ap);
3721a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3722e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3723387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3724e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
3725e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3726e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
3727e68eef14SAlexander Motin 			lp->is_target = 1;
372821daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
37295704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
3730e68eef14SAlexander Motin 		}
3731e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3732e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
3733e68eef14SAlexander Motin 			lp->is_initiator = 1;
3734e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3735e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3736e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3737e68eef14SAlexander Motin 			adc->port = lp->portid;
3738d4f3ad3aSAlexander Motin 			adc->target = tgt;
3739e68eef14SAlexander Motin 			adc->arrived = 1;
3740e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
374110365e5aSMatt Jacob 		}
374210365e5aSMatt Jacob 		break;
374310365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
374457b6261fSKenneth D. Merry 	case ISPASYNC_DEV_STAYED:
37456f9dbc0eSKenneth D. Merry 	{
37466f9dbc0eSKenneth D. Merry 		int crn_reset_done;
37476f9dbc0eSKenneth D. Merry 
37486f9dbc0eSKenneth D. Merry 		crn_reset_done = 0;
37492df76c16SMatt Jacob 		va_start(ap, cmd);
37502df76c16SMatt Jacob 		bus = va_arg(ap, int);
37512df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
37522df76c16SMatt Jacob 		va_end(ap);
3753a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3754e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3755e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
375657b6261fSKenneth D. Merry 		if (cmd == ISPASYNC_DEV_CHANGED)
3757e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
375857b6261fSKenneth D. Merry 		else
375957b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
376057b6261fSKenneth D. Merry 
3761e68eef14SAlexander Motin 		if (lp->is_target !=
3762e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3763e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
3764e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
3765e68eef14SAlexander Motin 			if (lp->is_target) {
37666f9dbc0eSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED) {
376721daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
37686f9dbc0eSKenneth D. Merry 					crn_reset_done = 1;
37696f9dbc0eSKenneth D. Merry 				}
3770e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
377110365e5aSMatt Jacob 			} else {
3772e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
37736f9dbc0eSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED) {
377421daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
37756f9dbc0eSKenneth D. Merry 					crn_reset_done = 1;
37766f9dbc0eSKenneth D. Merry 				}
377710365e5aSMatt Jacob 			}
3778f7c631bcSMatt Jacob 		}
3779e68eef14SAlexander Motin 		if (lp->is_initiator !=
3780e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3781e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
3782e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
3783e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3784e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3785e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3786e68eef14SAlexander Motin 			adc->port = lp->portid;
3787d4f3ad3aSAlexander Motin 			adc->target = tgt;
3788e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
3789e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3790e68eef14SAlexander Motin 		}
37916f9dbc0eSKenneth D. Merry 
3792e26059caSKenneth D. Merry 		if ((cmd == ISPASYNC_DEV_CHANGED) &&
37936f9dbc0eSKenneth D. Merry 		    (crn_reset_done == 0))
37946f9dbc0eSKenneth D. Merry 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
37956f9dbc0eSKenneth D. Merry 
379610365e5aSMatt Jacob 		break;
37976f9dbc0eSKenneth D. Merry 	}
379810365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
37992df76c16SMatt Jacob 		va_start(ap, cmd);
38002df76c16SMatt Jacob 		bus = va_arg(ap, int);
38012df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
38022df76c16SMatt Jacob 		va_end(ap);
3803a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3804e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3805f7c631bcSMatt Jacob 		/*
3806e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
3807e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
3808f7c631bcSMatt Jacob 		 */
3809387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3810e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
3811f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
3812e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
3813e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
3814a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
3815387d8239SMatt 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);
3816a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
3817f7c631bcSMatt Jacob 			}
3818e68eef14SAlexander Motin 			break;
38194b9d588eSMatt Jacob 		}
3820e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
382110365e5aSMatt Jacob 		break;
382210365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
382310365e5aSMatt Jacob 	{
382410365e5aSMatt Jacob 		char *msg;
3825eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
38262df76c16SMatt Jacob 
38272df76c16SMatt Jacob 		va_start(ap, cmd);
38282df76c16SMatt Jacob 		bus = va_arg(ap, int);
38292df76c16SMatt Jacob 		evt = va_arg(ap, int);
3830eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
38312df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
38322df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
38332df76c16SMatt Jacob 			reason = va_arg(ap, int);
3834eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3835eea52482SAlexander Motin 			portid = va_arg(ap, int);
383610365e5aSMatt Jacob 		} else {
38372df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
38382df76c16SMatt Jacob 			nlstate = reason = 0;
383910365e5aSMatt Jacob 		}
38402df76c16SMatt Jacob 		va_end(ap);
38412df76c16SMatt Jacob 
38422df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
384357b6261fSKenneth D. Merry 			int tgt_set = 0;
3844b5d5037bSAlexander Motin 			msg = "Port Database Changed";
3845eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
3846eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3847eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
384857b6261fSKenneth D. Merry 			/*
384957b6261fSKenneth D. Merry 			 * Port database syncs are not sufficient for
385057b6261fSKenneth D. Merry 			 * determining that logins or logouts are done on the
385157b6261fSKenneth D. Merry 			 * loop, but this information is directly available from
385257b6261fSKenneth D. Merry 			 * the reason code from the incoming mbox. We must reset
385357b6261fSKenneth D. Merry 			 * the fcp crn on these events according to FCP-4
385457b6261fSKenneth D. Merry 			 */
385557b6261fSKenneth D. Merry 			switch (reason) {
385657b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_1:
385757b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_2:
385857b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_3:
385957b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_RCVD:
386057b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_RCVD:
386157b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLO_RCVD:
386257b6261fSKenneth D. Merry 			case PDB24XX_AE_LOGO_RCVD:
386357b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_DONE:
386457b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_DONE:
386557b6261fSKenneth D. Merry 				/*
386657b6261fSKenneth D. Merry 				 * If the event is not global, twiddle tgt and
386757b6261fSKenneth D. Merry 				 * tgt_set to nominate only the target
386857b6261fSKenneth D. Merry 				 * associated with the nphdl.
386957b6261fSKenneth D. Merry 				 */
387057b6261fSKenneth D. Merry 				if (nphdl != PDB24XX_AE_GLOBAL) {
387157b6261fSKenneth D. Merry 					/* Break if we don't yet have the pdb */
387257b6261fSKenneth D. Merry 					if (!isp_find_pdb_by_handle(isp, bus, nphdl, &lp))
387357b6261fSKenneth D. Merry 						break;
387457b6261fSKenneth D. Merry 					tgt = FC_PORTDB_TGT(isp, bus, lp);
387557b6261fSKenneth D. Merry 					tgt_set = 1;
387657b6261fSKenneth D. Merry 				}
387757b6261fSKenneth D. Merry 				isp_fcp_reset_crn(isp, bus, tgt, tgt_set);
387857b6261fSKenneth D. Merry 				break;
387957b6261fSKenneth D. Merry 			default:
388057b6261fSKenneth D. Merry 				break; /* NOP */
388157b6261fSKenneth D. Merry 			}
38822df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3883b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
3884eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3885eea52482SAlexander Motin 			    bus, msg, portid);
38862df76c16SMatt Jacob 		} else {
3887b5d5037bSAlexander Motin 			msg = "Other Change Notify";
3888eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
38892df76c16SMatt Jacob 		}
3890e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
389157c801f5SMatt Jacob 		break;
389202ab3379SMatt Jacob 	}
3893d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3894e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
3895d81ba9d5SMatt Jacob 	{
38962df76c16SMatt Jacob 		isp_notify_t *notify;
38972df76c16SMatt Jacob 		va_start(ap, cmd);
38982df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
38992df76c16SMatt Jacob 		va_end(ap);
39002df76c16SMatt Jacob 		switch (notify->nt_ncode) {
39012df76c16SMatt Jacob 		case NT_ABORT_TASK:
39022df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
39032df76c16SMatt Jacob 		case NT_CLEAR_ACA:
39042df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
39052df76c16SMatt Jacob 		case NT_LUN_RESET:
39062df76c16SMatt Jacob 		case NT_TARGET_RESET:
3907c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
3908c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
39092df76c16SMatt Jacob 			/*
39102df76c16SMatt Jacob 			 * These are task management functions.
39112df76c16SMatt Jacob 			 */
39122df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
39132df76c16SMatt Jacob 			break;
39142df76c16SMatt Jacob 		case NT_BUS_RESET:
39152df76c16SMatt Jacob 		case NT_LIP_RESET:
39162df76c16SMatt Jacob 		case NT_LINK_UP:
39172df76c16SMatt Jacob 		case NT_LINK_DOWN:
3918e68eef14SAlexander Motin 		case NT_HBA_RESET:
39192df76c16SMatt Jacob 			/*
39202df76c16SMatt Jacob 			 * No action need be taken here.
39212df76c16SMatt Jacob 			 */
39222df76c16SMatt Jacob 			break;
3923387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
39242df76c16SMatt Jacob 		case NT_LOGOUT:
39252df76c16SMatt Jacob 			/*
39262df76c16SMatt Jacob 			 * This is device arrival/departure notification
39272df76c16SMatt Jacob 			 */
392896b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
39292df76c16SMatt Jacob 			break;
39305a5632c2SAlexander Motin 		case NT_SRR:
39315a5632c2SAlexander Motin 			isp_handle_platform_srr(isp, notify);
39325a5632c2SAlexander Motin 			break;
39332df76c16SMatt Jacob 		default:
39342df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
393596b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
39362df76c16SMatt Jacob 			break;
39372df76c16SMatt Jacob 		}
3938d81ba9d5SMatt Jacob 		break;
3939d81ba9d5SMatt Jacob 	}
3940387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
3941387d8239SMatt Jacob 	{
3942387d8239SMatt Jacob 		void *inot;
3943387d8239SMatt Jacob 		va_start(ap, cmd);
3944387d8239SMatt Jacob 		inot = va_arg(ap, void *);
3945387d8239SMatt Jacob 		va_end(ap);
3946387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
3947387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3948387d8239SMatt Jacob 			if (tp) {
3949387d8239SMatt Jacob 				tp->isp = isp;
3950387d8239SMatt Jacob 				memcpy(tp->data, inot, sizeof (tp->data));
3951387d8239SMatt Jacob 				tp->not = tp->data;
39522a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
39532a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
39542a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
3955387d8239SMatt Jacob 			} else {
3956387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3957387d8239SMatt Jacob 			}
3958387d8239SMatt Jacob 		}
3959387d8239SMatt Jacob 		break;
3960387d8239SMatt Jacob 	}
3961d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
39622df76c16SMatt Jacob 	{
39632df76c16SMatt Jacob 		isphdr_t *hp;
39642df76c16SMatt Jacob 
39652df76c16SMatt Jacob 		va_start(ap, cmd);
39662df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
39672df76c16SMatt Jacob 		va_end(ap);
39682df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
3969d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
39702df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
3971d81ba9d5SMatt Jacob 			break;
3972d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
39732df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
3974d81ba9d5SMatt Jacob 			break;
39752df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
3976d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
3977d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
3978d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
39792df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
3980d81ba9d5SMatt Jacob 			break;
39815a5632c2SAlexander Motin 		default:
39825a5632c2SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
39835a5632c2SAlexander Motin 			    __func__, hp->rqs_entry_type);
39845a5632c2SAlexander Motin 			break;
3985d81ba9d5SMatt Jacob 		}
3986d81ba9d5SMatt Jacob 		break;
39872df76c16SMatt Jacob 	}
3988d81ba9d5SMatt Jacob #endif
3989ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
3990ab163f5fSMatt Jacob 	{
39911dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
3992ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
3993ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
3994ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
3995ab163f5fSMatt Jacob 		} else {
3996ab163f5fSMatt Jacob 			mbox6 = 0;
3997ab163f5fSMatt Jacob 		}
39982df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
39999c2e9bcfSAlexander Motin #if 0
40000a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
40010a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
40022df76c16SMatt Jacob 		isp_reinit(isp, 1);
40030a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
40040a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
40059c2e9bcfSAlexander Motin #endif
4006ab163f5fSMatt Jacob 		break;
4007ab163f5fSMatt Jacob 	}
4008d81ba9d5SMatt Jacob 	default:
4009b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4010cbf57b47SMatt Jacob 		break;
4011cbf57b47SMatt Jacob 	}
4012cbf57b47SMatt Jacob }
4013cbf57b47SMatt Jacob 
40142df76c16SMatt Jacob uint64_t
40152df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
40162df76c16SMatt Jacob {
40172df76c16SMatt Jacob 	uint64_t seed;
40182df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
40192df76c16SMatt Jacob 
4020a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4021a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4022a193dc4bSAlexander Motin 	if (seed)
4023a193dc4bSAlexander Motin 		return (seed);
4024a193dc4bSAlexander Motin 
4025a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
40262df76c16SMatt Jacob 	if (isactive) {
4027a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4028a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4029a193dc4bSAlexander Motin 		if (seed)
40302df76c16SMatt Jacob 			return (seed);
40312df76c16SMatt Jacob 	}
4032a193dc4bSAlexander Motin 
4033a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4034a193dc4bSAlexander Motin 	if (chan > 0) {
4035a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4036a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4037a193dc4bSAlexander Motin 		if (seed == 0) {
4038a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4039a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
40405cc3786cSMatt Jacob 		}
40412df76c16SMatt Jacob 	}
40422df76c16SMatt Jacob 
4043a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4044a193dc4bSAlexander Motin 	if (seed == 0) {
4045a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4046a193dc4bSAlexander Motin 		if (!iswwnn)
4047a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4048a193dc4bSAlexander Motin 	}
40492df76c16SMatt Jacob 
4050a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4051a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
40522df76c16SMatt Jacob 		/*
40532df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
40542df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4055a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
40562df76c16SMatt Jacob 		 */
4057a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4058a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4059a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
40602df76c16SMatt Jacob 	}
40612df76c16SMatt Jacob 	return (seed);
40622df76c16SMatt Jacob }
40632df76c16SMatt Jacob 
4064b09b0095SMatt Jacob void
40659cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4066b09b0095SMatt Jacob {
4067de461933SMatt Jacob 	int loc;
4068387d8239SMatt Jacob 	char lbuf[200];
4069b09b0095SMatt Jacob 	va_list ap;
4070de461933SMatt Jacob 
4071b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4072b09b0095SMatt Jacob 		return;
4073b09b0095SMatt Jacob 	}
4074387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4075de461933SMatt Jacob 	loc = strlen(lbuf);
4076b09b0095SMatt Jacob 	va_start(ap, fmt);
4077de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4078b09b0095SMatt Jacob 	va_end(ap);
4079de461933SMatt Jacob 	printf("%s\n", lbuf);
4080b09b0095SMatt Jacob }
4081f7c631bcSMatt Jacob 
4082670508b1SMatt Jacob void
4083670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4084670508b1SMatt Jacob {
4085670508b1SMatt Jacob 	va_list ap;
4086670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4087670508b1SMatt Jacob 		return;
4088670508b1SMatt Jacob 	}
4089670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4090670508b1SMatt Jacob 	va_start(ap, fmt);
4091670508b1SMatt Jacob 	vprintf(fmt, ap);
4092670508b1SMatt Jacob 	va_end(ap);
4093670508b1SMatt Jacob 	printf("\n");
4094670508b1SMatt Jacob }
4095670508b1SMatt Jacob 
4096f7c631bcSMatt Jacob uint64_t
4097f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4098f7c631bcSMatt Jacob {
4099f7c631bcSMatt Jacob 	uint64_t elapsed;
41006040822cSAlan Somers 	struct timespec x;
41016040822cSAlan Somers 
41026040822cSAlan Somers 	timespecsub(b, a, &x);
4103f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4104f7c631bcSMatt Jacob 	if (elapsed == 0)
4105f7c631bcSMatt Jacob 		elapsed++;
4106f7c631bcSMatt Jacob 	return (elapsed);
4107f7c631bcSMatt Jacob }
4108f7c631bcSMatt Jacob 
4109f7c631bcSMatt Jacob int
4110f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4111f7c631bcSMatt Jacob {
4112f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4113f7c631bcSMatt Jacob 		return (1);
4114f7c631bcSMatt Jacob 	} else {
4115f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4116f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4117f7c631bcSMatt Jacob 		return (0);
4118f7c631bcSMatt Jacob 	}
4119f7c631bcSMatt Jacob }
4120f7c631bcSMatt Jacob 
4121f7c631bcSMatt Jacob void
4122f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4123f7c631bcSMatt Jacob {
41246327b0d2SAlexander Motin 	u_int t, to;
4125f7c631bcSMatt Jacob 
41266327b0d2SAlexander Motin 	to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4127f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4128f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4129f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
413013d9c921SAlexander Motin 		msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_lock,
41316327b0d2SAlexander Motin 		    PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4132f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4133f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4134f7c631bcSMatt Jacob 	} else {
41356327b0d2SAlexander Motin 		for (t = 0; t < to; t += 100) {
41366327b0d2SAlexander Motin 			if (isp->isp_osinfo.mboxcmd_done)
4137f7c631bcSMatt Jacob 				break;
41380e6bc811SAlexander Motin 			ISP_RUN_ISR(isp);
41396327b0d2SAlexander Motin 			if (isp->isp_osinfo.mboxcmd_done)
4140f7c631bcSMatt Jacob 				break;
41412df76c16SMatt Jacob 			ISP_DELAY(100);
4142f7c631bcSMatt Jacob 		}
4143f7c631bcSMatt Jacob 	}
4144f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
41456327b0d2SAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
41466327b0d2SAlexander Motin 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
41476327b0d2SAlexander Motin 		    isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4148f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4149f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4150f7c631bcSMatt Jacob 	}
4151f7c631bcSMatt Jacob }
4152f7c631bcSMatt Jacob 
4153f7c631bcSMatt Jacob void
4154f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4155f7c631bcSMatt Jacob {
4156f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
41576327b0d2SAlexander Motin 	if (isp->isp_osinfo.mbox_sleeping)
41586327b0d2SAlexander Motin 		wakeup(&isp->isp_osinfo.mboxcmd_done);
4159f7c631bcSMatt Jacob }
4160f7c631bcSMatt Jacob 
4161f7c631bcSMatt Jacob void
4162f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4163f7c631bcSMatt Jacob {
4164f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4165f7c631bcSMatt Jacob }
4166f7c631bcSMatt Jacob 
4167f7c631bcSMatt Jacob int
41682df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
41692df76c16SMatt Jacob {
41702df76c16SMatt Jacob 	int ret = 0;
41712df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
41722df76c16SMatt Jacob 		ret = -1;
41732df76c16SMatt Jacob 	} else {
41742df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
41752df76c16SMatt Jacob 	}
41762df76c16SMatt Jacob 	return (ret);
41772df76c16SMatt Jacob }
41782df76c16SMatt Jacob 
41790a70657fSMatt Jacob void
41800a70657fSMatt Jacob isp_platform_intr(void *arg)
41810a70657fSMatt Jacob {
41820a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
41830a70657fSMatt Jacob 
41840a70657fSMatt Jacob 	ISP_LOCK(isp);
41850e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
41860a70657fSMatt Jacob 	ISP_UNLOCK(isp);
41870a70657fSMatt Jacob }
41880a70657fSMatt Jacob 
41890a70657fSMatt Jacob void
419008826086SAlexander Motin isp_platform_intr_resp(void *arg)
419108826086SAlexander Motin {
419208826086SAlexander Motin 	ispsoftc_t *isp = arg;
419308826086SAlexander Motin 
419408826086SAlexander Motin 	ISP_LOCK(isp);
419508826086SAlexander Motin 	isp_intr_respq(isp);
419608826086SAlexander Motin 	ISP_UNLOCK(isp);
419708826086SAlexander Motin 
419808826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
419908826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
420008826086SAlexander Motin }
420108826086SAlexander Motin 
420208826086SAlexander Motin void
420308826086SAlexander Motin isp_platform_intr_atio(void *arg)
420408826086SAlexander Motin {
420508826086SAlexander Motin 	ispsoftc_t *isp = arg;
420608826086SAlexander Motin 
420708826086SAlexander Motin 	ISP_LOCK(isp);
420808826086SAlexander Motin #ifdef	ISP_TARGET_MODE
420908826086SAlexander Motin 	isp_intr_atioq(isp);
421008826086SAlexander Motin #endif
421108826086SAlexander Motin 	ISP_UNLOCK(isp);
421208826086SAlexander Motin 
421308826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
421408826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
421508826086SAlexander Motin }
421608826086SAlexander Motin 
421708826086SAlexander Motin void
42180a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
42190a70657fSMatt Jacob {
42200a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
42212df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
42220a70657fSMatt Jacob 	} else {
42232df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
42240a70657fSMatt Jacob 	}
42250a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
42260a70657fSMatt Jacob }
42272df76c16SMatt Jacob 
4228fb4a4356SKenneth D. Merry /*
4229fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4230fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4231fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4232fb4a4356SKenneth D. Merry  */
4233fb4a4356SKenneth D. Merry void
423421daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4235fb4a4356SKenneth D. Merry {
423621daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4237fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
423821daf914SAlexander Motin 	int i;
4239fb4a4356SKenneth D. Merry 
4240fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
424121daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
424221daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4243fb4a4356SKenneth D. Merry 	else
424421daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
424521daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4246fb4a4356SKenneth D. Merry 
4247fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
424821daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
424921daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4250fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4251fb4a4356SKenneth D. Merry 		}
4252fb4a4356SKenneth D. Merry 	}
4253fb4a4356SKenneth D. Merry }
4254fb4a4356SKenneth D. Merry 
4255387d8239SMatt Jacob int
4256387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4257387d8239SMatt Jacob {
42586af11b82SAlexander Motin 	lun_id_t lun;
42596af11b82SAlexander Motin 	uint32_t chan, tgt;
4260405b7a29SMatt Jacob 	struct isp_fc *fc;
4261405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4262405b7a29SMatt Jacob 	int idx;
4263405b7a29SMatt Jacob 
4264a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4265405b7a29SMatt Jacob 		return (0);
4266405b7a29SMatt Jacob 
4267405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4268405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4269405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4270405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4271405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4272405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4273387d8239SMatt Jacob 
4274387d8239SMatt Jacob 	while (nxp) {
4275387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4276387d8239SMatt Jacob 			break;
4277387d8239SMatt Jacob 		nxp = nxp->next;
4278387d8239SMatt Jacob 	}
4279387d8239SMatt Jacob 	if (nxp == NULL) {
4280387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4281387d8239SMatt Jacob 		if (nxp == NULL) {
4282387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4283387d8239SMatt Jacob 			if (nxp == NULL) {
4284387d8239SMatt Jacob 				return (-1);
4285387d8239SMatt Jacob 			}
4286387d8239SMatt Jacob 		} else {
4287387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4288387d8239SMatt Jacob 		}
4289387d8239SMatt Jacob 		nxp->tgt = tgt;
4290387d8239SMatt Jacob 		nxp->lun = lun;
4291387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4292387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4293387d8239SMatt Jacob 	}
4294387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4295387d8239SMatt Jacob 		nxp->crnseed = 1;
4296387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4297387d8239SMatt Jacob 	return (0);
4298387d8239SMatt Jacob }
4299387d8239SMatt Jacob 
430094dff771SMatt Jacob /*
430194dff771SMatt Jacob  * We enter with the lock held
430294dff771SMatt Jacob  */
43032df76c16SMatt Jacob void
43042df76c16SMatt Jacob isp_timer(void *arg)
43052df76c16SMatt Jacob {
43062df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
43072df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
43082df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
43092df76c16SMatt Jacob #endif
431094dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
43112df76c16SMatt Jacob }
4312387d8239SMatt Jacob 
4313387d8239SMatt Jacob isp_ecmd_t *
4314387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4315387d8239SMatt Jacob {
4316387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4317387d8239SMatt Jacob 	if (ecmd) {
4318387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4319387d8239SMatt Jacob 	}
4320387d8239SMatt Jacob 	return (ecmd);
4321387d8239SMatt Jacob }
4322387d8239SMatt Jacob 
4323387d8239SMatt Jacob void
4324387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4325387d8239SMatt Jacob {
4326387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4327387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4328387d8239SMatt Jacob }
4329