xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision b4af3e8a85983959bf9d44643c21976d319196e8)
1098ca2bdSWarner Losh /*-
2a94fab67SAlexander Motin  * Copyright (c) 2009-2017 Alexander Motin <mav@FreeBSD.org>
32df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
4e5265237SMatt Jacob  * All rights reserved.
56054c3f6SMatt Jacob  *
66054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
76054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
86054c3f6SMatt Jacob  * are met:
96054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
106054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
116054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
12aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
13aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
146054c3f6SMatt Jacob  *
156054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
166054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
186054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
196054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
216054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256054c3f6SMatt Jacob  * SUCH DAMAGE.
266054c3f6SMatt Jacob  */
27aad970f1SDavid E. O'Brien 
28799881e0SMatt Jacob /*
29799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30799881e0SMatt Jacob  */
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
335b14cb41SWill Andrews 
346054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
355d571944SMatt Jacob #include <sys/unistd.h>
365d571944SMatt Jacob #include <sys/kthread.h>
375d571944SMatt Jacob #include <sys/conf.h>
384eb49427SMatt Jacob #include <sys/module.h>
395d571944SMatt Jacob #include <sys/ioccom.h>
405d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4170273f90SMatt Jacob #include <sys/devicestat.h>
42f7c631bcSMatt Jacob #include <cam/cam_periph.h>
436c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
446054c3f6SMatt Jacob 
454eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4656c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4773030e03SMatt Jacob int isp_announced = 0;
482df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
492df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
50f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
51e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5273030e03SMatt Jacob 
53e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
54e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
555d571944SMatt Jacob static d_ioctl_t ispioctl;
561dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
570470d791SMatt Jacob static void isp_poll(struct cam_sim *);
58b85389e1SMatt Jacob static timeout_t isp_watchdog;
59de461933SMatt Jacob static timeout_t isp_gdt;
60de461933SMatt Jacob static task_fn_t isp_gdt_task;
615d571944SMatt Jacob static void isp_kthread(void *);
62d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6394dff771SMatt Jacob static int isp_timer_count;
642df76c16SMatt Jacob static void isp_timer(void *);
65cc8df88bSMatt Jacob 
665d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
67dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
687ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
697ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
705d571944SMatt Jacob };
715d571944SMatt Jacob 
722df76c16SMatt Jacob static int
7307f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7407f56f1cSAlexander Motin {
7507f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7607f56f1cSAlexander Motin 	int chan = arg2;
7707f56f1cSAlexander Motin 	int error, old, value;
7807f56f1cSAlexander Motin 
7907f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8007f56f1cSAlexander Motin 
8107f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8207f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8307f56f1cSAlexander Motin 		return (error);
8407f56f1cSAlexander Motin 
8507f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8607f56f1cSAlexander Motin 		return (EINVAL);
8707f56f1cSAlexander Motin 
8807f56f1cSAlexander Motin 	ISP_LOCK(isp);
8907f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9007f56f1cSAlexander Motin 
91391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
92391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
93391f03daSAlexander Motin 
9407f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9507f56f1cSAlexander Motin 	if (value == old) {
9607f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9707f56f1cSAlexander Motin 		return (0);
9807f56f1cSAlexander Motin 	}
9907f56f1cSAlexander Motin 
10007f56f1cSAlexander Motin 	/* Actually change the role. */
1016bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10207f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10307f56f1cSAlexander Motin 	return (error);
10407f56f1cSAlexander Motin }
10507f56f1cSAlexander Motin 
10607f56f1cSAlexander Motin static int
1072df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
108478f8a96SJustin T. Gibbs {
109478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
110ea6f23cdSMatt Jacob 	struct cam_sim *sim;
111ea6f23cdSMatt Jacob 	struct cam_path *path;
1128290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
1138290ea90SAlexander Motin 	int i;
1148290ea90SAlexander Motin #endif
115478f8a96SJustin T. Gibbs 
11613d9c921SAlexander Motin 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
11713d9c921SAlexander Motin 	    device_get_unit(isp->isp_dev), &isp->isp_lock,
11813d9c921SAlexander Motin 	    isp->isp_maxcmds, isp->isp_maxcmds, devq);
11913d9c921SAlexander Motin 	if (sim == NULL)
1202df76c16SMatt Jacob 		return (ENOMEM);
1212df76c16SMatt Jacob 
1222df76c16SMatt Jacob 	ISP_LOCK(isp);
1232df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1242df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1252df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1262df76c16SMatt Jacob 		return (EIO);
1272df76c16SMatt Jacob 	}
1282df76c16SMatt Jacob 	ISP_UNLOCK(isp);
129227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1302df76c16SMatt Jacob 		ISP_LOCK(isp);
1312df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1322df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1332df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1342df76c16SMatt Jacob 		return (ENXIO);
1352df76c16SMatt Jacob 	}
1362df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1372df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1382df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1392df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1402df76c16SMatt Jacob 	csa.callback_arg = sim;
141387d8239SMatt Jacob 
142387d8239SMatt Jacob 	ISP_LOCK(isp);
1432df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
144387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1452df76c16SMatt Jacob 
1462df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1472df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1482df76c16SMatt Jacob 		spi->sim = sim;
1492df76c16SMatt Jacob 		spi->path = path;
1500d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
15109ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1528290ea90SAlexander Motin 		STAILQ_INIT(&spi->ntfree);
1538290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1548290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
1558290ea90SAlexander Motin 		LIST_INIT(&spi->atfree);
1568290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1578290ea90SAlexander Motin 			LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
1588290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1598290ea90SAlexander Motin 			LIST_INIT(&spi->atused[i]);
1600d965629SAlexander Motin #endif
1612df76c16SMatt Jacob 	} else {
162a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1632df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
16407f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
16507f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
16607f56f1cSAlexander Motin 		char name[16];
1672df76c16SMatt Jacob 
168a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1692df76c16SMatt Jacob 		fc->sim = sim;
1702df76c16SMatt Jacob 		fc->path = path;
1712df76c16SMatt Jacob 		fc->isp = isp;
172a01f5aebSMatt Jacob 		fc->ready = 1;
173a94fab67SAlexander Motin 		fcp->isp_use_gft_id = 1;
174a94fab67SAlexander Motin 		fcp->isp_use_gff_id = 1;
175de461933SMatt Jacob 
17613d9c921SAlexander Motin 		callout_init_mtx(&fc->gdt, &isp->isp_lock, 0);
177de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
1780d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
17909ddc7adSAlexander Motin 		TAILQ_INIT(&fc->waitq);
1808290ea90SAlexander Motin 		STAILQ_INIT(&fc->ntfree);
1818290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1828290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
1838290ea90SAlexander Motin 		LIST_INIT(&fc->atfree);
1848290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1858290ea90SAlexander Motin 			LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
1868290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1878290ea90SAlexander Motin 			LIST_INIT(&fc->atused[i]);
1880d965629SAlexander Motin #endif
189e561aa79SAlexander Motin 		isp_loop_changed(isp, chan);
1902df76c16SMatt Jacob 		ISP_UNLOCK(isp);
191069f5ef9SAlexander Motin 		if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
192069f5ef9SAlexander Motin 		    "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
193a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
194a01f5aebSMatt Jacob 			ISP_LOCK(isp);
195a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
196a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
197a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
198a01f5aebSMatt Jacob 			return (ENOMEM);
199a01f5aebSMatt Jacob 		}
200e596ff7aSAlexander Motin 		fc->num_threads += 1;
20107f56f1cSAlexander Motin 		if (chan > 0) {
20207f56f1cSAlexander Motin 			snprintf(name, sizeof(name), "chan%d", chan);
20307f56f1cSAlexander Motin 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
20407f56f1cSAlexander Motin 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
205a0ec8e99SMatt Jacob 		}
206e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
207e596ff7aSAlexander Motin 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
208e596ff7aSAlexander Motin 		    "World Wide Node Name");
209e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210e596ff7aSAlexander Motin 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
211e596ff7aSAlexander Motin 		    "World Wide Port Name");
212e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213e596ff7aSAlexander Motin 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
214e596ff7aSAlexander Motin 		    "Loop Down Limit");
215e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
216e596ff7aSAlexander Motin 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
217e596ff7aSAlexander Motin 		    "Gone Device Time");
21807f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
219e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
220e596ff7aSAlexander Motin 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
221e596ff7aSAlexander Motin 		    "Cause a Lost Frame on a Read");
22207f56f1cSAlexander Motin #endif
22307f56f1cSAlexander Motin 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
22407f56f1cSAlexander Motin 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
22507f56f1cSAlexander Motin 		    isp_role_sysctl, "I", "Current role");
226e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
227e596ff7aSAlexander Motin 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
228e596ff7aSAlexander Motin 		    "Connection speed in gigabits");
229e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
230e596ff7aSAlexander Motin 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
231e596ff7aSAlexander Motin 		    "Link state");
232e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
233e596ff7aSAlexander Motin 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
234e596ff7aSAlexander Motin 		    "Firmware state");
235e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236e596ff7aSAlexander Motin 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
237e596ff7aSAlexander Motin 		    "Loop state");
238e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
239e596ff7aSAlexander Motin 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
240e596ff7aSAlexander Motin 		    "Connection topology");
241a94fab67SAlexander Motin 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
242a94fab67SAlexander Motin 		    "use_gft_id", CTLFLAG_RWTUN, &fcp->isp_use_gft_id, 0,
243a94fab67SAlexander Motin 		    "Use GFT_ID during fabric scan");
244a94fab67SAlexander Motin 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
245a94fab67SAlexander Motin 		    "use_gff_id", CTLFLAG_RWTUN, &fcp->isp_use_gff_id, 0,
246a94fab67SAlexander Motin 		    "Use GFF_ID during fabric scan");
2472df76c16SMatt Jacob 	}
2482df76c16SMatt Jacob 	return (0);
2492df76c16SMatt Jacob }
2502df76c16SMatt Jacob 
2511d0a1de2SWill Andrews static void
2521d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2531d0a1de2SWill Andrews {
2541d0a1de2SWill Andrews 	struct cam_sim *sim;
2551d0a1de2SWill Andrews 	struct cam_path *path;
2561d0a1de2SWill Andrews 	struct ccb_setasync csa;
2571d0a1de2SWill Andrews 	int *num_threads;
2581d0a1de2SWill Andrews 
2591d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2601d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2611d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2621d0a1de2SWill Andrews 
2631d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2641d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2651d0a1de2SWill Andrews 	csa.event_enable = 0;
2661d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2671d0a1de2SWill Andrews 	csa.callback_arg = sim;
2681d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2691d0a1de2SWill Andrews 	xpt_free_path(path);
2701d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2711d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2721d0a1de2SWill Andrews 
2731d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2741d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2751d0a1de2SWill Andrews 	while (*num_threads != 0)
27613d9c921SAlexander Motin 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "isp_reap", 100);
2771d0a1de2SWill Andrews }
2781d0a1de2SWill Andrews 
2792df76c16SMatt Jacob int
2802df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2812df76c16SMatt Jacob {
2822df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2832df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2842df76c16SMatt Jacob 	int chan;
2852df76c16SMatt Jacob 
286ea6f23cdSMatt Jacob 	/*
287ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
288478f8a96SJustin T. Gibbs 	 */
2892df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2902df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2912df76c16SMatt Jacob 		return (EIO);
292478f8a96SJustin T. Gibbs 	}
293478f8a96SJustin T. Gibbs 
2942df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2952df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2962df76c16SMatt Jacob 			goto unwind;
297ea6f23cdSMatt Jacob 		}
298ea6f23cdSMatt Jacob 	}
299ea6f23cdSMatt Jacob 
30013d9c921SAlexander Motin 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0);
30194dff771SMatt Jacob 	isp_timer_count = hz >> 2;
30294dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
303ea6f23cdSMatt Jacob 
3042df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
3052df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3062df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
307ea6f23cdSMatt Jacob 	}
3082df76c16SMatt Jacob 	return (0);
3095d571944SMatt Jacob 
3102df76c16SMatt Jacob unwind:
3112df76c16SMatt Jacob 	while (--chan >= 0) {
3122df76c16SMatt Jacob 		struct cam_sim *sim;
3132df76c16SMatt Jacob 		struct cam_path *path;
3141d0a1de2SWill Andrews 
3151d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3161d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3172df76c16SMatt Jacob 		xpt_free_path(path);
3180a70657fSMatt Jacob 		ISP_LOCK(isp);
3190a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3202df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3212df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3222df76c16SMatt Jacob 	}
3232df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3242df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3252df76c16SMatt Jacob 	return (-1);
3262df76c16SMatt Jacob }
3272df76c16SMatt Jacob 
328e95725cbSMatt Jacob int
3292df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3302df76c16SMatt Jacob {
3312df76c16SMatt Jacob 	int chan;
3322df76c16SMatt Jacob 
3332df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3342df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3352df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3362df76c16SMatt Jacob 	}
33798339da1SAlexander Motin 	ISP_LOCK(isp);
33898339da1SAlexander Motin 	/* Tell spawned threads that we're exiting. */
33998339da1SAlexander Motin 	isp->isp_osinfo.is_exiting = 1;
34098339da1SAlexander Motin 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
34198339da1SAlexander Motin 		isp_detach_chan(isp, chan);
34298339da1SAlexander Motin 	ISP_UNLOCK(isp);
34398339da1SAlexander Motin 	callout_drain(&isp->isp_osinfo.tmo);
3442df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
345e95725cbSMatt Jacob 	return (0);
3465d571944SMatt Jacob }
3475d571944SMatt Jacob 
348f7c631bcSMatt Jacob static void
349e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
350fdeb9f2fSMatt Jacob {
3512df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
352ce9f3237SAlexander Motin 
353ce9f3237SAlexander Motin 	if (fc->sim == NULL)
354ce9f3237SAlexander Motin 		return;
3552df76c16SMatt Jacob 	if (fc->simqfrozen == 0) {
356b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
357e561aa79SAlexander Motin 		    "Chan %d Freeze simq (loopdown)", chan);
3582df76c16SMatt Jacob 		fc->simqfrozen = SIMQFRZ_LOOPDOWN;
359c07b9e07SAlexander Motin 		xpt_hold_boot();
3602df76c16SMatt Jacob 		xpt_freeze_simq(fc->sim, 1);
361fdeb9f2fSMatt Jacob 	} else {
362b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
363e561aa79SAlexander Motin 		    "Chan %d Mark simq frozen (loopdown)", chan);
3642df76c16SMatt Jacob 		fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3652df76c16SMatt Jacob 	}
366fdeb9f2fSMatt Jacob }
367fdeb9f2fSMatt Jacob 
368e95725cbSMatt Jacob static void
369e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
370e95725cbSMatt Jacob {
371e95725cbSMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
372ce9f3237SAlexander Motin 
373ce9f3237SAlexander Motin 	if (fc->sim == NULL)
374ce9f3237SAlexander Motin 		return;
375e95725cbSMatt Jacob 	int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
376e95725cbSMatt Jacob 	fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
377e95725cbSMatt Jacob 	if (wasfrozen && fc->simqfrozen == 0) {
378e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
379e561aa79SAlexander Motin 		    "Chan %d Release simq", chan);
380e95725cbSMatt Jacob 		xpt_release_simq(fc->sim, 1);
381c07b9e07SAlexander Motin 		xpt_release_boot();
382e95725cbSMatt Jacob 	}
383e95725cbSMatt Jacob }
3849cd7268eSMatt Jacob 
3855d571944SMatt Jacob static int
3862df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
3875d571944SMatt Jacob {
3889cd7268eSMatt Jacob 	ispsoftc_t *isp;
3892df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
3905d571944SMatt Jacob 
3912df76c16SMatt Jacob 	isp = dev->si_drv1;
3925d571944SMatt Jacob 
393c1504bc0SMatt Jacob 	switch (c) {
3945d571944SMatt Jacob 	case ISP_SDBLEV:
3955d571944SMatt Jacob 	{
3965d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
3975d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
3985d571944SMatt Jacob 		*(int *)addr = olddblev;
3995d571944SMatt Jacob 		retval = 0;
4005d571944SMatt Jacob 		break;
4015d571944SMatt Jacob 	}
402746e9c85SMatt Jacob 	case ISP_GETROLE:
4032df76c16SMatt Jacob 		chan = *(int *)addr;
4042df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4052df76c16SMatt Jacob 			retval = -ENXIO;
4062df76c16SMatt Jacob 			break;
4072df76c16SMatt Jacob 		}
4082df76c16SMatt Jacob 		if (IS_FC(isp)) {
4092df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4102df76c16SMatt Jacob 		} else {
4113e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4122df76c16SMatt Jacob 		}
413746e9c85SMatt Jacob 		retval = 0;
414746e9c85SMatt Jacob 		break;
415746e9c85SMatt Jacob 	case ISP_SETROLE:
4163e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4173e6deb33SAlexander Motin 			break;
418746e9c85SMatt Jacob 		nr = *(int *)addr;
4192df76c16SMatt Jacob 		chan = nr >> 8;
4202df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4212df76c16SMatt Jacob 			retval = -ENXIO;
4222df76c16SMatt Jacob 			break;
4232df76c16SMatt Jacob 		}
4242df76c16SMatt Jacob 		nr &= 0xff;
425746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
426746e9c85SMatt Jacob 			retval = EINVAL;
427746e9c85SMatt Jacob 			break;
428746e9c85SMatt Jacob 		}
4292df76c16SMatt Jacob 		ISP_LOCK(isp);
4306bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4316bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4326bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4332df76c16SMatt Jacob 		retval = 0;
4342df76c16SMatt Jacob 		break;
4352df76c16SMatt Jacob 
4365d571944SMatt Jacob 	case ISP_RESETHBA:
437511ced9bSMatt Jacob 		ISP_LOCK(isp);
4382df76c16SMatt Jacob 		isp_reinit(isp, 0);
439511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4405d571944SMatt Jacob 		retval = 0;
4415d571944SMatt Jacob 		break;
4422df76c16SMatt Jacob 
443f553351eSMatt Jacob 	case ISP_RESCAN:
4445d571944SMatt Jacob 		if (IS_FC(isp)) {
4452df76c16SMatt Jacob 			chan = *(int *)addr;
4462df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4472df76c16SMatt Jacob 				retval = -ENXIO;
4482df76c16SMatt Jacob 				break;
4492df76c16SMatt Jacob 			}
450511ced9bSMatt Jacob 			ISP_LOCK(isp);
451e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4525d571944SMatt Jacob 				retval = EIO;
4535d571944SMatt Jacob 			} else {
4545d571944SMatt Jacob 				retval = 0;
4555d571944SMatt Jacob 			}
456511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4575d571944SMatt Jacob 		}
4585d571944SMatt Jacob 		break;
4592df76c16SMatt Jacob 
4605d571944SMatt Jacob 	case ISP_FC_LIP:
4615d571944SMatt Jacob 		if (IS_FC(isp)) {
4622df76c16SMatt Jacob 			chan = *(int *)addr;
4632df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4642df76c16SMatt Jacob 				retval = -ENXIO;
4652df76c16SMatt Jacob 				break;
4662df76c16SMatt Jacob 			}
467511ced9bSMatt Jacob 			ISP_LOCK(isp);
4682df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4695d571944SMatt Jacob 				retval = EIO;
4705d571944SMatt Jacob 			} else {
4715d571944SMatt Jacob 				retval = 0;
4725d571944SMatt Jacob 			}
473511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4745d571944SMatt Jacob 		}
4755d571944SMatt Jacob 		break;
4765d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4775d571944SMatt Jacob 	{
4785d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
47910365e5aSMatt Jacob 		fcportdb_t *lp;
4805d571944SMatt Jacob 
48102e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
48202e2b2d9SMatt Jacob 			break;
48302e2b2d9SMatt Jacob 		}
4842e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
4855d571944SMatt Jacob 			retval = EINVAL;
4865d571944SMatt Jacob 			break;
4875d571944SMatt Jacob 		}
4882df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
489e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
490387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
49110365e5aSMatt Jacob 			ifc->loopid = lp->handle;
4925d571944SMatt Jacob 			ifc->portid = lp->portid;
4935d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
4945d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
4955d571944SMatt Jacob 			retval = 0;
4965d571944SMatt Jacob 		} else {
4975d571944SMatt Jacob 			retval = ENODEV;
4985d571944SMatt Jacob 		}
4995d571944SMatt Jacob 		break;
5005d571944SMatt Jacob 	}
501570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
502570c7a3fSMatt Jacob 	{
503570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5042df76c16SMatt Jacob 		int chan = hba->fc_channel;
50502e2b2d9SMatt Jacob 
5062df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5072df76c16SMatt Jacob 			retval = ENXIO;
5082df76c16SMatt Jacob 			break;
5092df76c16SMatt Jacob 		}
510a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
511a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
512a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5132df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
51402e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5152df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5162df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5172df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5182df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5192df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5202df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5212df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5222df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5232df76c16SMatt Jacob 		} else {
5242df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5252df76c16SMatt Jacob 			hba->fc_speed = 0;
5262df76c16SMatt Jacob 			hba->fc_topology = 0;
5272df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5282df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5292df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5302df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
53102e2b2d9SMatt Jacob 		}
532570c7a3fSMatt Jacob 		retval = 0;
533570c7a3fSMatt Jacob 		break;
534570c7a3fSMatt Jacob 	}
5358e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5368e62a8acSMatt Jacob 	{
5378e62a8acSMatt Jacob 		int needmarker;
5388e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
539c5fd36edSAlexander Motin 		uint16_t nphdl;
5408e62a8acSMatt Jacob 		mbreg_t mbs;
5418e62a8acSMatt Jacob 
5428e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5438e62a8acSMatt Jacob 			break;
5448e62a8acSMatt Jacob 		}
5458e62a8acSMatt Jacob 
5462df76c16SMatt Jacob 		chan = fct->chan;
5472df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5482df76c16SMatt Jacob 			retval = -ENXIO;
5492df76c16SMatt Jacob 			break;
5502df76c16SMatt Jacob 		}
5512df76c16SMatt Jacob 
5528e62a8acSMatt Jacob 		needmarker = retval = 0;
553c5fd36edSAlexander Motin 		nphdl = fct->loopid;
5542df76c16SMatt Jacob 		ISP_LOCK(isp);
5552df76c16SMatt Jacob 		if (IS_24XX(isp)) {
55653791a95SAlexander Motin 			void *reqp;
55753791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
55853791a95SAlexander Motin 			isp24xx_tmf_t tmf;
55953791a95SAlexander Motin 			isp24xx_statusreq_t sp;
5602df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
5612df76c16SMatt Jacob 			fcportdb_t *lp;
5622df76c16SMatt Jacob 			int i;
5632df76c16SMatt Jacob 
5642df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
5652df76c16SMatt Jacob 				lp = &fcp->portdb[i];
566c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
5672df76c16SMatt Jacob 					break;
5682df76c16SMatt Jacob 				}
5692df76c16SMatt Jacob 			}
5702df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
5712df76c16SMatt Jacob 				retval = ENXIO;
5722df76c16SMatt Jacob 				ISP_UNLOCK(isp);
5732df76c16SMatt Jacob 				break;
5742df76c16SMatt Jacob 			}
57553791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
57653791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
57753791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
57853791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
57953791a95SAlexander Motin 			tmf.tmf_delay = 2;
58053791a95SAlexander Motin 			tmf.tmf_timeout = 4;
58153791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
58253791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
58353791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
58453791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
5852df76c16SMatt Jacob 			if (fct->lun >= 256) {
58653791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
5872df76c16SMatt Jacob 			}
5882df76c16SMatt Jacob 			switch (fct->action) {
5892df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
59053791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
5912df76c16SMatt Jacob 				break;
5922df76c16SMatt Jacob 			case IPT_TARGET_RESET:
59353791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
5942df76c16SMatt Jacob 				needmarker = 1;
5952df76c16SMatt Jacob 				break;
5962df76c16SMatt Jacob 			case IPT_LUN_RESET:
59753791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
5982df76c16SMatt Jacob 				needmarker = 1;
5992df76c16SMatt Jacob 				break;
6002df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
60153791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6022df76c16SMatt Jacob 				needmarker = 1;
6032df76c16SMatt Jacob 				break;
6042df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
60553791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6062df76c16SMatt Jacob 				needmarker = 1;
6072df76c16SMatt Jacob 				break;
6082df76c16SMatt Jacob 			default:
6092df76c16SMatt Jacob 				retval = EINVAL;
6102df76c16SMatt Jacob 				break;
6112df76c16SMatt Jacob 			}
6122df76c16SMatt Jacob 			if (retval) {
6132df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6142df76c16SMatt Jacob 				break;
6152df76c16SMatt Jacob 			}
6162df76c16SMatt Jacob 
61753791a95SAlexander Motin 			/* Prepare space for response in memory */
61853791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
61953791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
62053791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
62153791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
62253791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
62353791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
62453791a95SAlexander Motin 				    __func__, chan);
6252df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6262df76c16SMatt Jacob 				retval = ENOMEM;
6272df76c16SMatt Jacob 				break;
6282df76c16SMatt Jacob 			}
62953791a95SAlexander Motin 
63053791a95SAlexander Motin 			/* Send request and wait for response. */
63153791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
63253791a95SAlexander Motin 			if (reqp == NULL) {
63353791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
63453791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
63553791a95SAlexander Motin 				    __func__, chan);
63653791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
63753791a95SAlexander Motin 				ISP_UNLOCK(isp);
6382df76c16SMatt Jacob 				retval = EIO;
63953791a95SAlexander Motin 				break;
6402df76c16SMatt Jacob 			}
64153791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
64253791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
64353791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
64453791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
64553791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
64653791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
64753791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
64853791a95SAlexander Motin 				    __func__, chan);
64953791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
65053791a95SAlexander Motin 				ISP_UNLOCK(isp);
65153791a95SAlexander Motin 				retval = EIO;
65253791a95SAlexander Motin 				break;
65353791a95SAlexander Motin 			}
65453791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
65553791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
65653791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
65753791a95SAlexander Motin 
65853791a95SAlexander Motin 			if (sp.req_completion_status != 0)
65953791a95SAlexander Motin 				retval = EIO;
66053791a95SAlexander Motin 			else if (needmarker)
6612df76c16SMatt Jacob 				fcp->sendmarker = 1;
6622df76c16SMatt Jacob 		} else {
6632df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
6642df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
665c5fd36edSAlexander Motin 				nphdl <<= 8;
666e5265237SMatt Jacob 			}
6678e62a8acSMatt Jacob 			switch (fct->action) {
668f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
6698e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
670c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6718e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6728e62a8acSMatt Jacob 				break;
673f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
6748e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
675c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6768e62a8acSMatt Jacob 				needmarker = 1;
6778e62a8acSMatt Jacob 				break;
678f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
6798e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
680c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6818e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6828e62a8acSMatt Jacob 				needmarker = 1;
6838e62a8acSMatt Jacob 				break;
684f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
6858e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
686c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6878e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6888e62a8acSMatt Jacob 				needmarker = 1;
6898e62a8acSMatt Jacob 				break;
690f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
6918e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
692c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
6938e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6948e62a8acSMatt Jacob 				needmarker = 1;
6958e62a8acSMatt Jacob 				break;
6968e62a8acSMatt Jacob 			default:
6978e62a8acSMatt Jacob 				retval = EINVAL;
6988e62a8acSMatt Jacob 				break;
6998e62a8acSMatt Jacob 			}
7008e62a8acSMatt Jacob 			if (retval == 0) {
7018e62a8acSMatt Jacob 				if (needmarker) {
7022df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7038e62a8acSMatt Jacob 				}
7048e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7052df76c16SMatt Jacob 				if (retval) {
7068e62a8acSMatt Jacob 					retval = EIO;
7078e62a8acSMatt Jacob 				}
7082df76c16SMatt Jacob 			}
7092df76c16SMatt Jacob 		}
7102df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7118e62a8acSMatt Jacob 		break;
7128e62a8acSMatt Jacob 	}
7135d571944SMatt Jacob 	default:
7145d571944SMatt Jacob 		break;
7155d571944SMatt Jacob 	}
7165d571944SMatt Jacob 	return (retval);
7170470d791SMatt Jacob }
718478f8a96SJustin T. Gibbs 
719d81ba9d5SMatt Jacob /*
7202df76c16SMatt Jacob  * Local Inlines
7212df76c16SMatt Jacob  */
7222df76c16SMatt Jacob 
7232df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
7242df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
7252df76c16SMatt Jacob 
7262df76c16SMatt Jacob static ISP_INLINE int
7272df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
7282df76c16SMatt Jacob {
7292df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
7302df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
7312df76c16SMatt Jacob 		return (-1);
7322df76c16SMatt Jacob 	}
7332df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
7342df76c16SMatt Jacob 	return (0);
7352df76c16SMatt Jacob }
7362df76c16SMatt Jacob 
7372df76c16SMatt Jacob static ISP_INLINE void
7382df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
7392df76c16SMatt Jacob {
740387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
741405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
742405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
743405b7a29SMatt Jacob #endif
744405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
7452df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
7462df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
7472df76c16SMatt Jacob 	}
748387d8239SMatt Jacob }
749387d8239SMatt Jacob 
7502df76c16SMatt Jacob /*
751d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
752d81ba9d5SMatt Jacob  */
753d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
7542df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
7558290ea90SAlexander Motin static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
7568290ea90SAlexander Motin static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
7578290ea90SAlexander Motin static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
7588290ea90SAlexander Motin static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
7598290ea90SAlexander Motin static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
7608290ea90SAlexander Motin static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
7612df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
7628290ea90SAlexander Motin static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
7632df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
7642df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
765f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
766387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
767a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
768a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
76994dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
770387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
7712df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
7722df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
7732df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
77496b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
7752df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
7768290ea90SAlexander Motin static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
777d81ba9d5SMatt Jacob 
7782df76c16SMatt Jacob static ISP_INLINE tstate_t *
7799cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
780d81ba9d5SMatt Jacob {
78164edff94SMatt Jacob 	tstate_t *tptr = NULL;
7822df76c16SMatt Jacob 	struct tslist *lhp;
783d81ba9d5SMatt Jacob 
7842df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
785315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
7862df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
7878290ea90SAlexander Motin 			if (tptr->ts_lun == lun)
788d81ba9d5SMatt Jacob 				return (tptr);
789d81ba9d5SMatt Jacob 		}
79064edff94SMatt Jacob 	}
7912df76c16SMatt Jacob 	return (NULL);
7922df76c16SMatt Jacob }
793d81ba9d5SMatt Jacob 
7948290ea90SAlexander Motin static int
7958290ea90SAlexander Motin isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
7962df76c16SMatt Jacob {
7972df76c16SMatt Jacob 	inot_private_data_t *ntp;
7988290ea90SAlexander Motin 	struct ntpdlist rq;
7992df76c16SMatt Jacob 
8008290ea90SAlexander Motin 	if (STAILQ_EMPTY(&tptr->restart_queue))
8018290ea90SAlexander Motin 		return (0);
8028290ea90SAlexander Motin 	STAILQ_INIT(&rq);
8038290ea90SAlexander Motin 	STAILQ_CONCAT(&rq, &tptr->restart_queue);
8048290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
8058290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&rq, next);
8068290ea90SAlexander Motin 		if (IS_24XX(isp)) {
8078290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8088290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8098290ea90SAlexander Motin 			    ((at7_entry_t *)ntp->data)->at_rxid);
8108290ea90SAlexander Motin 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
8118290ea90SAlexander Motin 		} else {
8128290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8138290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8148290ea90SAlexander Motin 			    ((at2_entry_t *)ntp->data)->at_rxid);
8158290ea90SAlexander Motin 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
8162df76c16SMatt Jacob 		}
8178290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
8188290ea90SAlexander Motin 		if (!STAILQ_EMPTY(&tptr->restart_queue))
8198290ea90SAlexander Motin 			break;
8202df76c16SMatt Jacob 	}
8218290ea90SAlexander Motin 	if (!STAILQ_EMPTY(&rq)) {
8228290ea90SAlexander Motin 		STAILQ_CONCAT(&rq, &tptr->restart_queue);
8238290ea90SAlexander Motin 		STAILQ_CONCAT(&tptr->restart_queue, &rq);
8242df76c16SMatt Jacob 	}
8258290ea90SAlexander Motin 	return (!STAILQ_EMPTY(&tptr->restart_queue));
826d81ba9d5SMatt Jacob }
827d81ba9d5SMatt Jacob 
8282df76c16SMatt Jacob static void
8292df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
8302df76c16SMatt Jacob {
8312df76c16SMatt Jacob 	tstate_t *tptr;
83294dff771SMatt Jacob 	union ccb *ccb;
8332df76c16SMatt Jacob 	struct tslist *lhp;
83409ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
8352df76c16SMatt Jacob 	int bus, i;
8362df76c16SMatt Jacob 
8372df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
8382df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8392df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8408290ea90SAlexander Motin 			SLIST_FOREACH(tptr, lhp, next)
8418290ea90SAlexander Motin 				isp_atio_restart(isp, bus, tptr);
84209ddc7adSAlexander Motin 		}
84309ddc7adSAlexander Motin 
84494dff771SMatt Jacob 		/*
84509ddc7adSAlexander Motin 		 * We only need to do this once per channel.
84694dff771SMatt Jacob 		 */
84709ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
84809ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
84909ddc7adSAlexander Motin 		if (ccb != NULL) {
850ea8e769eSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
85194dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
85294dff771SMatt Jacob 		}
8532df76c16SMatt Jacob 	}
8542df76c16SMatt Jacob }
8552df76c16SMatt Jacob 
8568290ea90SAlexander Motin static atio_private_data_t *
8578290ea90SAlexander Motin isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
85853036e92SMatt Jacob {
8598290ea90SAlexander Motin 	struct atpdlist *atfree;
8608290ea90SAlexander Motin 	struct atpdlist *atused;
86153036e92SMatt Jacob 	atio_private_data_t *atp;
8622df76c16SMatt Jacob 
8638290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
8648290ea90SAlexander Motin 	atp = LIST_FIRST(atfree);
8652df76c16SMatt Jacob 	if (atp) {
866523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
867523ea374SAlexander Motin 		atp->tag = tag;
8688290ea90SAlexander Motin 		ISP_GET_PC(isp, chan, atused, atused);
8698290ea90SAlexander Motin 		LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
8702df76c16SMatt Jacob 	}
87153036e92SMatt Jacob 	return (atp);
87253036e92SMatt Jacob }
873523ea374SAlexander Motin 
8748290ea90SAlexander Motin static atio_private_data_t *
8758290ea90SAlexander Motin isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
876523ea374SAlexander Motin {
8778290ea90SAlexander Motin 	struct atpdlist *atused;
878523ea374SAlexander Motin 	atio_private_data_t *atp;
879523ea374SAlexander Motin 
8808290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atused, atused);
8818290ea90SAlexander Motin 	LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
882523ea374SAlexander Motin 		if (atp->tag == tag)
8832df76c16SMatt Jacob 			return (atp);
8842df76c16SMatt Jacob 	}
88553036e92SMatt Jacob 	return (NULL);
88653036e92SMatt Jacob }
88753036e92SMatt Jacob 
8888290ea90SAlexander Motin static void
8898290ea90SAlexander Motin isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
8902df76c16SMatt Jacob {
8918290ea90SAlexander Motin 	struct atpdlist *atfree;
8928290ea90SAlexander Motin 
893387d8239SMatt Jacob 	if (atp->ests) {
894387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
895387d8239SMatt Jacob 	}
896523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
897387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
8988290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
8998290ea90SAlexander Motin 	LIST_INSERT_HEAD(atfree, atp, next);
9002df76c16SMatt Jacob }
9012df76c16SMatt Jacob 
9022df76c16SMatt Jacob static void
9038290ea90SAlexander Motin isp_dump_atpd(ispsoftc_t *isp, int chan)
9042df76c16SMatt Jacob {
9058290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
9062df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
9072df76c16SMatt Jacob 
9088290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
9098290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
910cfbeb41cSAlexander Motin 		if (atp->state == ATPD_STATE_FREE)
911cfbeb41cSAlexander Motin 			continue;
912cfbeb41cSAlexander 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",
9133f072d69SAlexander 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]);
9142df76c16SMatt Jacob 	}
9152df76c16SMatt Jacob }
9162df76c16SMatt Jacob 
9178290ea90SAlexander Motin static inot_private_data_t *
9188290ea90SAlexander Motin isp_get_ntpd(ispsoftc_t *isp, int chan)
9192df76c16SMatt Jacob {
9208290ea90SAlexander Motin 	struct ntpdlist *ntfree;
9212df76c16SMatt Jacob 	inot_private_data_t *ntp;
9228290ea90SAlexander Motin 
9238290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
9248290ea90SAlexander Motin 	ntp = STAILQ_FIRST(ntfree);
9258290ea90SAlexander Motin 	if (ntp)
9268290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(ntfree, next);
9272df76c16SMatt Jacob 	return (ntp);
9282df76c16SMatt Jacob }
9292df76c16SMatt Jacob 
9308290ea90SAlexander Motin static inot_private_data_t *
9318290ea90SAlexander Motin isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
9322df76c16SMatt Jacob {
9338290ea90SAlexander Motin 	inot_private_data_t *ntp, *ntp2;
9348290ea90SAlexander Motin 
9358290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, ntpool, ntp);
9368290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
9378290ea90SAlexander Motin 	for (; ntp < ntp2; ntp++) {
9388290ea90SAlexander Motin 		if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
9392df76c16SMatt Jacob 			return (ntp);
9402df76c16SMatt Jacob 	}
9412df76c16SMatt Jacob 	return (NULL);
9422df76c16SMatt Jacob }
9432df76c16SMatt Jacob 
9448290ea90SAlexander Motin static void
9458290ea90SAlexander Motin isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
9462df76c16SMatt Jacob {
9478290ea90SAlexander Motin 	struct ntpdlist *ntfree;
9488290ea90SAlexander Motin 
9498290ea90SAlexander Motin 	ntp->tag_id = ntp->seq_id = 0;
9508290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
9518290ea90SAlexander Motin 	STAILQ_INSERT_HEAD(ntfree, ntp, next);
9522df76c16SMatt Jacob }
9532df76c16SMatt Jacob 
954d81ba9d5SMatt Jacob static cam_status
9552df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
956d81ba9d5SMatt Jacob {
957d81ba9d5SMatt Jacob 	lun_id_t lun;
9582df76c16SMatt Jacob 	struct tslist *lhp;
9592df76c16SMatt Jacob 	tstate_t *tptr;
960d81ba9d5SMatt Jacob 
961d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
9622df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
9636af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
964d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
965d81ba9d5SMatt Jacob 		}
9662df76c16SMatt Jacob 	}
967e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
9682df76c16SMatt Jacob 	if (tptr == NULL) {
969d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
970d81ba9d5SMatt Jacob 	}
971315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
9722df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
9732df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
974*b4af3e8aSAlexander Motin 	STAILQ_INIT(&tptr->restart_queue);
975315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
9762df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
9772df76c16SMatt Jacob 	*rslt = tptr;
9782df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
979d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
980d81ba9d5SMatt Jacob }
981d81ba9d5SMatt Jacob 
9828290ea90SAlexander Motin static void
9838290ea90SAlexander Motin destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
984d81ba9d5SMatt Jacob {
985344aebe2SMatt Jacob 	union ccb *ccb;
9862df76c16SMatt Jacob 	struct tslist *lhp;
9878290ea90SAlexander Motin 	inot_private_data_t *ntp;
988e2873b76SMatt Jacob 
9898290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
990344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
991344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
992344aebe2SMatt Jacob 		xpt_done(ccb);
9938290ea90SAlexander Motin 	};
9948290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
995344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
996344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
997344aebe2SMatt Jacob 		xpt_done(ccb);
998344aebe2SMatt Jacob 	}
9998290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
10008290ea90SAlexander Motin 		isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
10018290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
10028290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
10038290ea90SAlexander Motin 	}
10048290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
10052df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1006d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1007d81ba9d5SMatt Jacob }
1008d81ba9d5SMatt Jacob 
10092df76c16SMatt Jacob static void
10102df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1011d81ba9d5SMatt Jacob {
10123e6deb33SAlexander Motin 	tstate_t *tptr;
10133e6deb33SAlexander Motin 	int bus;
10142df76c16SMatt Jacob 	target_id_t target;
1015d81ba9d5SMatt Jacob 	lun_id_t lun;
1016d81ba9d5SMatt Jacob 
10173e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
10183e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
10193e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
10203e6deb33SAlexander Motin 		xpt_done(ccb);
10213e6deb33SAlexander Motin 		return;
10223e6deb33SAlexander Motin 	}
1023e2873b76SMatt Jacob 
1024746e9c85SMatt Jacob 	/*
10253e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
10263e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1027746e9c85SMatt Jacob 	 */
10282df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10292df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10302df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10316af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
10326af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
10333e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
10342df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10352df76c16SMatt Jacob 		xpt_done(ccb);
10362df76c16SMatt Jacob 		return;
10372df76c16SMatt Jacob 	}
10382df76c16SMatt Jacob 
10393e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1040a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
10412df76c16SMatt Jacob 	if (tptr) {
10422df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
10433e6deb33SAlexander Motin 		xpt_done(ccb);
10443e6deb33SAlexander Motin 		return;
1045d81ba9d5SMatt Jacob 	}
10462df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
10472df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
10482df76c16SMatt Jacob 		xpt_done(ccb);
10493e6deb33SAlexander Motin 		return;
10502df76c16SMatt Jacob 	}
10512df76c16SMatt Jacob 
10523e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
10533e6deb33SAlexander Motin 	xpt_done(ccb);
10542df76c16SMatt Jacob }
10552df76c16SMatt Jacob 
10562df76c16SMatt Jacob static void
10572df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
10582df76c16SMatt Jacob {
10592df76c16SMatt Jacob 	tstate_t *tptr = NULL;
10602df76c16SMatt Jacob 	int bus;
10612df76c16SMatt Jacob 	target_id_t target;
10622df76c16SMatt Jacob 	lun_id_t lun;
10632df76c16SMatt Jacob 
10642df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10652df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10662df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10676af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
10686af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
10693e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
10702df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10712df76c16SMatt Jacob 		xpt_done(ccb);
10722df76c16SMatt Jacob 		return;
10732df76c16SMatt Jacob 	}
10742df76c16SMatt Jacob 
10753e6deb33SAlexander Motin 	/* Find the state pointer. */
10762df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
10773e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
10782df76c16SMatt Jacob 		xpt_done(ccb);
10793e6deb33SAlexander Motin 		return;
10802df76c16SMatt Jacob 	}
10812df76c16SMatt Jacob 
10828290ea90SAlexander Motin 	destroy_lun_state(isp, bus, tptr);
10833e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
10843e6deb33SAlexander Motin 	xpt_done(ccb);
1085d81ba9d5SMatt Jacob }
1086d81ba9d5SMatt Jacob 
10879cd7268eSMatt Jacob static void
1088387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1089d81ba9d5SMatt Jacob {
109094dff771SMatt Jacob 	int fctape, sendstatus, resid;
1091387d8239SMatt Jacob 	fcparam *fcp;
10922df76c16SMatt Jacob 	atio_private_data_t *atp;
109394dff771SMatt Jacob 	struct ccb_scsiio *cso;
109409ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
109594dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
10961dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1097d81ba9d5SMatt Jacob 
109894dff771SMatt 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,
109994dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
110094dff771SMatt Jacob 
110109ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
110294dff771SMatt Jacob 	switch (how) {
110394dff771SMatt Jacob 	case FROM_CAM:
11042df76c16SMatt Jacob 		/*
110594dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
11062df76c16SMatt Jacob 		 */
1107ea8e769eSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, sim_links.tqe);
110894dff771SMatt Jacob 		break;
110909ddc7adSAlexander Motin 	case FROM_TIMER:
111094dff771SMatt Jacob 	case FROM_SRR:
111194dff771SMatt Jacob 	case FROM_CTIO_DONE:
1112ea8e769eSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
111394dff771SMatt Jacob 		break;
111494dff771SMatt Jacob 	}
111594dff771SMatt Jacob 
111609ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1117ea8e769eSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
111894dff771SMatt Jacob 
111994dff771SMatt Jacob 		cso = &ccb->csio;
1120387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1121387d8239SMatt Jacob 		if (xfrlen == 0) {
11222df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1123387d8239SMatt 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");
11242df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
11252df76c16SMatt Jacob 				xpt_done(ccb);
112694dff771SMatt Jacob 				continue;
11272df76c16SMatt Jacob 			}
11282df76c16SMatt Jacob 		}
11292df76c16SMatt Jacob 
11308290ea90SAlexander Motin 		atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
11312df76c16SMatt Jacob 		if (atp == NULL) {
113294dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
11338290ea90SAlexander Motin 			isp_dump_atpd(isp, XS_CHANNEL(ccb));
11342df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
11352df76c16SMatt Jacob 			xpt_done(ccb);
113694dff771SMatt Jacob 			continue;
11372df76c16SMatt Jacob 		}
1138387d8239SMatt Jacob 
1139387d8239SMatt Jacob 		/*
1140387d8239SMatt Jacob 		 * Is this command a dead duck?
1141387d8239SMatt Jacob 		 */
11422df76c16SMatt Jacob 		if (atp->dead) {
114394dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
11442df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
11452df76c16SMatt Jacob 			xpt_done(ccb);
114694dff771SMatt Jacob 			continue;
11472df76c16SMatt Jacob 		}
11482df76c16SMatt Jacob 
11492df76c16SMatt Jacob 		/*
11502df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
11512df76c16SMatt Jacob 		 */
1152387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1153387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
115494dff771SMatt 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);
11552df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
11562df76c16SMatt Jacob 			xpt_done(ccb);
115794dff771SMatt Jacob 			continue;
11582df76c16SMatt Jacob 		}
11592df76c16SMatt Jacob 
11602df76c16SMatt Jacob 		/*
116194dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
116294dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1163387d8239SMatt Jacob 		 */
116494dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
116594dff771SMatt 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);
1166ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
116794dff771SMatt Jacob 			break;
1168387d8239SMatt Jacob 		}
1169387d8239SMatt Jacob 
1170d81ba9d5SMatt Jacob 		/*
1171387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1172d81ba9d5SMatt Jacob 		 */
1173387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1174387d8239SMatt Jacob 			fctape = 1;
1175387d8239SMatt Jacob 		} else {
1176387d8239SMatt Jacob 			fctape = 0;
1177387d8239SMatt Jacob 		}
1178387d8239SMatt Jacob 
1179387d8239SMatt Jacob 		/*
1180387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1181387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1182387d8239SMatt Jacob 		 */
1183387d8239SMatt Jacob 		if (atp->sendst) {
1184daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
118594dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1186387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1187387d8239SMatt Jacob 			atp->sendst = 0;
1188387d8239SMatt Jacob 		}
1189387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1190387d8239SMatt Jacob 			sendstatus = 1;
1191387d8239SMatt Jacob 		} else {
1192387d8239SMatt Jacob 			sendstatus = 0;
1193387d8239SMatt Jacob 		}
1194387d8239SMatt Jacob 
1195387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
119694dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1197387d8239SMatt Jacob 			/*
1198387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1199387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1200387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1201387d8239SMatt Jacob 			 */
1202387d8239SMatt Jacob 			sense_length = cso->sense_len;
1203387d8239SMatt Jacob 
1204387d8239SMatt Jacob 			/*
1205387d8239SMatt Jacob 			 * This 'cannot' happen
1206387d8239SMatt Jacob 			 */
1207387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1208387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1209387d8239SMatt Jacob 			}
1210387d8239SMatt Jacob 		} else {
1211387d8239SMatt Jacob 			sense_length = 0;
1212387d8239SMatt Jacob 		}
1213387d8239SMatt Jacob 
121494dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
121594dff771SMatt Jacob 
121694dff771SMatt Jacob 		/*
121794dff771SMatt Jacob 		 * Check for overflow
121894dff771SMatt Jacob 		 */
1219a6036a44SAlexander Motin 		tmp = atp->bytes_xfered + atp->bytes_in_transit;
1220a6036a44SAlexander Motin 		if (xfrlen > 0 && tmp > atp->orig_datalen) {
1221a6036a44SAlexander Motin 			isp_prt(isp, ISP_LOGERR,
1222a6036a44SAlexander Motin 			    "%s: [0x%x] data overflow by %u bytes", __func__,
1223a6036a44SAlexander Motin 			    cso->tag_id, tmp + xfrlen - atp->orig_datalen);
122494dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
122594dff771SMatt Jacob 			xpt_done(ccb);
122694dff771SMatt Jacob 			continue;
122794dff771SMatt Jacob 		}
1228a6036a44SAlexander Motin 		if (xfrlen > atp->orig_datalen - tmp) {
1229a6036a44SAlexander Motin 			xfrlen = atp->orig_datalen - tmp;
1230a6036a44SAlexander Motin 			if (xfrlen == 0 && !sendstatus) {
1231a6036a44SAlexander Motin 				cso->resid = cso->dxfer_len;
1232a6036a44SAlexander Motin 				ccb->ccb_h.status = CAM_REQ_CMP;
1233a6036a44SAlexander Motin 				xpt_done(ccb);
1234a6036a44SAlexander Motin 				continue;
1235a6036a44SAlexander Motin 			}
1236a6036a44SAlexander Motin 		}
1237387d8239SMatt Jacob 
12382df76c16SMatt Jacob 		if (IS_24XX(isp)) {
12392df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1240d81ba9d5SMatt Jacob 
12412df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
12422df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
124394dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
124494dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
12452df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
12462df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
12473f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
12483f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
12492df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
12502df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
125131c161a6SAlexander Motin 			cto->ct_timeout = XS_TIME(ccb);
12522df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1253387d8239SMatt Jacob 
1254387d8239SMatt Jacob 			/*
1255387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
12566f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1257387d8239SMatt Jacob 			 *
12588b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
12598b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1260387d8239SMatt Jacob 			 *
1261387d8239SMatt Jacob 			 */
1262387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1263387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
126494dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1265387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1266387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1267387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1268387d8239SMatt Jacob 					if (resid < 0) {
1269a6036a44SAlexander Motin 						cto->ct_resid = -resid;
1270387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1271387d8239SMatt Jacob 					} else if (resid > 0) {
1272a6036a44SAlexander Motin 						cto->ct_resid = resid;
1273387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1274387d8239SMatt Jacob 					}
1275387d8239SMatt Jacob 					if (fctape) {
1276387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1277387d8239SMatt Jacob 					}
1278387d8239SMatt Jacob 					if (sense_length) {
12792df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1280387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1281387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
12822df76c16SMatt Jacob 					}
12832df76c16SMatt Jacob 				} else {
1284387d8239SMatt Jacob 					bus_addr_t addr;
1285387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1286387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1287387d8239SMatt Jacob 
1288387d8239SMatt Jacob 					if (atp->ests == NULL) {
1289387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1290387d8239SMatt Jacob 						if (atp->ests == NULL) {
1291ea8e769eSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
129294dff771SMatt Jacob 							break;
1293387d8239SMatt Jacob 						}
1294387d8239SMatt Jacob 					}
1295387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1296387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1297387d8239SMatt Jacob 					if (fctape) {
1298387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1299387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1300387d8239SMatt Jacob 					}
1301387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1302387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1303387d8239SMatt Jacob 					if (resid < 0) {
1304387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1305387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1306387d8239SMatt Jacob 					} else if (resid > 0) {
1307387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1308387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1309387d8239SMatt Jacob 					}
1310387d8239SMatt Jacob 					if (sense_length) {
1311387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1312387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1313387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1314387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1315387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1316387d8239SMatt Jacob 					} else {
1317387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1318387d8239SMatt Jacob 					}
1319387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1320387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1321387d8239SMatt Jacob 					}
1322387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1323387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
132494dff771SMatt 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,
1325387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1326387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1327387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1328387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1329387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1330387d8239SMatt Jacob 				}
1331387d8239SMatt Jacob 				if (sense_length) {
133294dff771SMatt 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__,
133394dff771SMatt 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,
133494dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1335387d8239SMatt Jacob 				} else {
133694dff771SMatt 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__,
133794dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1338387d8239SMatt Jacob 				}
1339387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1340387d8239SMatt Jacob 			}
1341387d8239SMatt Jacob 
1342387d8239SMatt Jacob 			/*
1343387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1344387d8239SMatt Jacob 			 */
1345387d8239SMatt Jacob 			if (xfrlen != 0) {
13462df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
13472df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
13482df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
13492df76c16SMatt Jacob 				} else {
13502df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
13512df76c16SMatt Jacob 				}
1352387d8239SMatt Jacob 
135394dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1354387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1355387d8239SMatt Jacob 
1356387d8239SMatt Jacob #ifdef	DEBUG
1357387d8239SMatt 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) {
1358387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1359387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1360387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1361387d8239SMatt Jacob 				}
1362387d8239SMatt Jacob #endif
1363387d8239SMatt Jacob 				if (sendstatus) {
136494dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
136594dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1366387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1367387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
136894dff771SMatt Jacob 						if (fctape) {
136994dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
137094dff771SMatt Jacob 						}
1371387d8239SMatt Jacob 					} else {
1372387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
137394dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
13742df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
13752df76c16SMatt Jacob 					}
1376387d8239SMatt Jacob 				} else {
1377387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1378387d8239SMatt Jacob 				}
137994dff771SMatt 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__,
138094dff771SMatt 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);
1381387d8239SMatt Jacob 			}
13823e6deb33SAlexander Motin 		} else {
13834fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
138400a8e174SMatt Jacob 
1385d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1386d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
138794dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
138894dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1389d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1390d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
139129f76675SMatt Jacob 			} else {
1392d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
13932df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1394d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
13952ad50ca5SMatt Jacob 				}
139629f76675SMatt Jacob 			}
139731c161a6SAlexander Motin 			cto->ct_timeout = XS_TIME(ccb);
139800a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1399387d8239SMatt Jacob 
1400387d8239SMatt Jacob 			/*
1401387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1402387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1403387d8239SMatt Jacob 			 *
140496240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1405387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1406387d8239SMatt Jacob 			 *
1407387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1408387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1409387d8239SMatt Jacob 			 * needed based upon this.
1410387d8239SMatt Jacob 			 */
1411387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1412387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
141394dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1414387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1415387d8239SMatt Jacob 					if (resid < 0) {
1416387d8239SMatt Jacob 						cto->ct_resid = -resid;
1417387d8239SMatt Jacob 					} else if (resid > 0) {
1418387d8239SMatt Jacob 						cto->ct_resid = resid;
1419387d8239SMatt Jacob 					}
1420387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1421f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1422387d8239SMatt Jacob 					if (resid < 0) {
14232df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1424387d8239SMatt Jacob 					} else if (resid > 0) {
14252df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1426f48ce188SMatt Jacob 					}
1427387d8239SMatt Jacob 					if (fctape) {
1428387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1429387d8239SMatt Jacob 					}
1430387d8239SMatt Jacob 					if (sense_length) {
143100a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1432387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1433387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
143400a8e174SMatt Jacob 					}
143500a8e174SMatt Jacob 				} else {
1436387d8239SMatt Jacob 					bus_addr_t addr;
1437387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1438387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1439387d8239SMatt Jacob 
1440387d8239SMatt Jacob 					if (atp->ests == NULL) {
1441387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1442387d8239SMatt Jacob 						if (atp->ests == NULL) {
1443ea8e769eSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
144494dff771SMatt Jacob 							break;
1445387d8239SMatt Jacob 						}
1446387d8239SMatt Jacob 					}
1447387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1448387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1449387d8239SMatt Jacob 					if (fctape) {
1450387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1451387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1452387d8239SMatt Jacob 					}
1453387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1454387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1455387d8239SMatt Jacob 					if (resid < 0) {
1456387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1457387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1458387d8239SMatt Jacob 					} else if (resid > 0) {
1459387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1460387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1461387d8239SMatt Jacob 					}
1462387d8239SMatt Jacob 					if (sense_length) {
1463387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1464387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1465387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1466387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1467387d8239SMatt Jacob 					} else {
1468387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1469387d8239SMatt Jacob 					}
1470387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1471387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1472387d8239SMatt Jacob 					}
1473387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1474387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
147594dff771SMatt 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,
1476387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1477387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1478387d8239SMatt Jacob 					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1479387d8239SMatt Jacob 					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1480387d8239SMatt Jacob 				}
1481387d8239SMatt Jacob 				if (sense_length) {
148294dff771SMatt 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__,
148394dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
148494dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1485387d8239SMatt Jacob 				} else {
148694dff771SMatt 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,
148794dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1488387d8239SMatt Jacob 				}
1489387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1490387d8239SMatt Jacob 			}
1491387d8239SMatt Jacob 
1492387d8239SMatt Jacob 			if (xfrlen != 0) {
149300a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
149400a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
149500a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
149600a8e174SMatt Jacob 				} else {
149700a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1498d81ba9d5SMatt Jacob 				}
149953036e92SMatt Jacob 
150094dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1501387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1502387d8239SMatt Jacob 
1503387d8239SMatt Jacob 				if (sendstatus) {
150494dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
150594dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1506387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1507387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
150894dff771SMatt Jacob 						if (fctape) {
150994dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
151094dff771SMatt Jacob 						}
1511387d8239SMatt Jacob 					} else {
1512387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
151394dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1514387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1515387d8239SMatt Jacob 					}
1516387d8239SMatt Jacob 				} else {
1517387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1518387d8239SMatt Jacob 				}
1519387d8239SMatt Jacob 			}
152094dff771SMatt 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,
152194dff771SMatt 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);
1522d81ba9d5SMatt Jacob 		}
1523d81ba9d5SMatt Jacob 
152494dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
152594dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1526ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
152794dff771SMatt Jacob 			break;
152894dff771SMatt Jacob 		}
1529970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1530970ceb2fSAlexander Motin 		if (handle == 0) {
1531387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1532ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
153394dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
153494dff771SMatt Jacob 			break;
1535d81ba9d5SMatt Jacob 		}
153694dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
153794dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
153894dff771SMatt Jacob 
1539d81ba9d5SMatt Jacob 
1540d81ba9d5SMatt Jacob 		/*
1541d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1542d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1543b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1544d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1545d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1546d81ba9d5SMatt Jacob 		 * format.
1547d81ba9d5SMatt Jacob 		 */
1548d81ba9d5SMatt Jacob 
15492df76c16SMatt Jacob 		if (IS_24XX(isp)) {
15502df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
15512df76c16SMatt Jacob 			cto->ct_syshandle = handle;
155210365e5aSMatt Jacob 		} else {
15533e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
155410365e5aSMatt Jacob 			cto->ct_syshandle = handle;
155510365e5aSMatt Jacob 		}
1556a1bc34c6SMatt Jacob 
15572df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
155894dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1559970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
156094dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
156194dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
1562ea8e769eSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
156394dff771SMatt Jacob 				break;
156494dff771SMatt Jacob 			}
156594dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
156694dff771SMatt Jacob 			xpt_done(ccb);
156794dff771SMatt Jacob 			continue;
156894dff771SMatt Jacob 		}
156994dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1570387d8239SMatt Jacob 		if (xfrlen) {
1571387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1572387d8239SMatt Jacob 		} else {
1573387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1574387d8239SMatt Jacob 		}
1575387d8239SMatt Jacob 		atp->ctcnt++;
157694dff771SMatt Jacob 		atp->seqno++;
15772df76c16SMatt Jacob 	}
1578d81ba9d5SMatt Jacob }
1579d81ba9d5SMatt Jacob 
1580a1bc34c6SMatt Jacob static void
1581a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1582f48ce188SMatt Jacob {
15832df76c16SMatt Jacob 	union ccb *ccb = arg;
15842a0db815SJohn Baldwin 
1585b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
15862df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1587a1bc34c6SMatt Jacob }
1588a1bc34c6SMatt Jacob 
1589a1bc34c6SMatt Jacob static void
1590387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1591387d8239SMatt Jacob {
1592387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1593387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
15942a0db815SJohn Baldwin 
15952a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1596387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
15972a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1598387d8239SMatt Jacob 	} else {
1599387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1600387d8239SMatt Jacob 	}
1601387d8239SMatt Jacob }
1602387d8239SMatt Jacob 
1603387d8239SMatt Jacob 
1604387d8239SMatt Jacob static void
1605a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1606a1bc34c6SMatt Jacob {
1607782a8e7cSAlexander Motin 	ispsoftc_t *isp = XS_ISP(ccb);
1608782a8e7cSAlexander Motin 	struct ccb_scsiio *cso = &ccb->csio;
16093e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1610a1bc34c6SMatt Jacob 
16112df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1612f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1613f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
16142df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1615a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1616f48ce188SMatt Jacob 	} else {
1617a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1618f48ce188SMatt Jacob 	}
1619f48ce188SMatt Jacob 	at->at_status = CT_OK;
1620a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
16216ddb8972SAlexander Motin 	at->at_iid = cso->init_id;
1622782a8e7cSAlexander Motin 	if (isp_target_put_entry(isp, at)) {
1623782a8e7cSAlexander Motin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1624782a8e7cSAlexander Motin 		    isp_refire_putback_atio, ccb);
1625782a8e7cSAlexander Motin 	} else
1626a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
1627f48ce188SMatt Jacob }
1628f48ce188SMatt Jacob 
1629f48ce188SMatt Jacob static void
1630a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1631f48ce188SMatt Jacob {
1632387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1633a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1634a1bc34c6SMatt Jacob 		xpt_done(ccb);
1635f48ce188SMatt Jacob 	}
1636387d8239SMatt Jacob }
1637f48ce188SMatt Jacob 
16382df76c16SMatt Jacob static void
16399cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1640d81ba9d5SMatt Jacob {
164145b9ad7aSAlexander Motin 	fcparam *fcp;
164292a1e549SMatt Jacob 	lun_id_t lun;
16432df76c16SMatt Jacob 	fcportdb_t *lp;
1644d81ba9d5SMatt Jacob 	tstate_t *tptr;
1645d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
16462df76c16SMatt Jacob 	uint16_t nphdl;
1647a035b0afSMatt Jacob 	atio_private_data_t *atp;
16482df76c16SMatt Jacob 	inot_private_data_t *ntp;
1649d81ba9d5SMatt Jacob 
1650d81ba9d5SMatt Jacob 	/*
1651d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1652d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1653d81ba9d5SMatt Jacob 	 *
1654b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1655d81ba9d5SMatt Jacob 	 */
1656d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
16572df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
16588290ea90SAlexander Motin 		isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
16592df76c16SMatt Jacob 		return;
1660d81ba9d5SMatt Jacob 	}
1661d81ba9d5SMatt Jacob 
166245b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
16632df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
166492a1e549SMatt Jacob 		lun = aep->at_scclun;
16652ad50ca5SMatt Jacob 	} else {
166692a1e549SMatt Jacob 		lun = aep->at_lun;
16672ad50ca5SMatt Jacob 	}
16682df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
16692df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
16702df76c16SMatt Jacob 	} else {
16712df76c16SMatt Jacob 		nphdl = aep->at_iid;
16722df76c16SMatt Jacob 	}
1673a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1674d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1675a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1676746e9c85SMatt Jacob 		if (tptr == NULL) {
1677123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1678387d8239SMatt Jacob 			if (lun == 0) {
16798290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1680387d8239SMatt Jacob 			} else {
16818290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1682387d8239SMatt Jacob 			}
16832df76c16SMatt Jacob 			return;
1684746e9c85SMatt Jacob 		}
1685d81ba9d5SMatt Jacob 	}
1686d81ba9d5SMatt Jacob 
1687d81ba9d5SMatt Jacob 	/*
16882df76c16SMatt Jacob 	 * Start any commands pending resources first.
1689d81ba9d5SMatt Jacob 	 */
16908290ea90SAlexander Motin 	if (isp_atio_restart(isp, 0, tptr))
16912df76c16SMatt Jacob 		goto noresrc;
16922df76c16SMatt Jacob 
16932df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
16942df76c16SMatt Jacob 	if (atiop == NULL) {
16952df76c16SMatt Jacob 		goto noresrc;
16962df76c16SMatt Jacob 	}
16972df76c16SMatt Jacob 
16988290ea90SAlexander Motin 	atp = isp_get_atpd(isp, 0, aep->at_rxid);
16992df76c16SMatt Jacob 	if (atp == NULL) {
17002df76c16SMatt Jacob 		goto noresrc;
17012df76c16SMatt Jacob 	}
17022df76c16SMatt Jacob 
1703570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1704d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1705d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
17066ddb8972SAlexander Motin 	atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
170792a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
17082df76c16SMatt Jacob 
1709b0a3ba7eSMatt Jacob 	/*
1710b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1711b0a3ba7eSMatt Jacob 	 */
1712f48ce188SMatt Jacob 	atiop->sense_len = 0;
17132df76c16SMatt Jacob 
17142df76c16SMatt Jacob 	/*
17152df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
17162df76c16SMatt Jacob 	 */
1717d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1718d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1719d4f3ad3aSAlexander Motin 	else {
1720e2a658cbSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
1721d4f3ad3aSAlexander Motin 			atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1722e2a658cbSAlexander Motin 		} else {
1723e2a658cbSAlexander Motin 			isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB",
1724e2a658cbSAlexander Motin 			    __func__, nphdl);
1725e2a658cbSAlexander Motin 			isp_dump_portdb(isp, 0);
1726e2a658cbSAlexander Motin 			isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0);
1727e2a658cbSAlexander Motin 			return;
1728e2a658cbSAlexander Motin 		}
17292df76c16SMatt Jacob 	}
1730d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
17312df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1732d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
17332df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1734d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1735d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
17365e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1737d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1738d81ba9d5SMatt Jacob 		break;
1739d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
17405e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1741d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1742d81ba9d5SMatt Jacob 		break;
1743d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
17445e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1745d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1746d81ba9d5SMatt Jacob 		break;
1747d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1748d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1749d81ba9d5SMatt Jacob 	default:
1750d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1751d81ba9d5SMatt Jacob 		break;
1752d81ba9d5SMatt Jacob 	}
1753f48ce188SMatt Jacob 
175453036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
175553036e92SMatt Jacob 	atp->bytes_xfered = 0;
17562df76c16SMatt Jacob 	atp->lun = lun;
1757d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
17582df76c16SMatt Jacob 	atp->sid = PORT_ANY;
17592df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
17602df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
17612df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1762570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
1763d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
1764123055f0SNathan 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);
17652df76c16SMatt Jacob 	return;
17662df76c16SMatt Jacob noresrc:
17678290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, 0);
17682df76c16SMatt Jacob 	if (ntp == NULL) {
17698290ea90SAlexander Motin 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
17702df76c16SMatt Jacob 		return;
17712df76c16SMatt Jacob 	}
17728290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
17738290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1774d81ba9d5SMatt Jacob }
1775d81ba9d5SMatt Jacob 
17762df76c16SMatt Jacob static void
17772df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
17782df76c16SMatt Jacob {
17792df76c16SMatt Jacob 	int cdbxlen;
17806af11b82SAlexander Motin 	lun_id_t lun;
17816af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
17822df76c16SMatt Jacob 	uint32_t did, sid;
17832df76c16SMatt Jacob 	fcportdb_t *lp;
17842df76c16SMatt Jacob 	tstate_t *tptr;
17852df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
17862df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
17879e7d423dSMatt Jacob 	atio_private_data_t *oatp;
17882df76c16SMatt Jacob 	inot_private_data_t *ntp;
17892df76c16SMatt Jacob 
17902df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
17912df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
17926af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
17932df76c16SMatt Jacob 
17947dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
17959c81a61eSAlexander Motin 		/* Channel has to be derived from D_ID */
17962df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
17972df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
17989c81a61eSAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
17999c81a61eSAlexander Motin 			    "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
18009c81a61eSAlexander Motin 			    __func__, aep->at_rxid, did);
18019c81a61eSAlexander Motin 			isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
18029c81a61eSAlexander Motin 			    ECMD_TERMINATE, 0);
18032df76c16SMatt Jacob 			return;
18042df76c16SMatt Jacob 		}
18052df76c16SMatt Jacob 	} else {
18062df76c16SMatt Jacob 		chan = 0;
18072df76c16SMatt Jacob 	}
18082df76c16SMatt Jacob 
18092df76c16SMatt Jacob 	/*
18102df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
18112df76c16SMatt Jacob 	 */
1812eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
18132df76c16SMatt Jacob 		/*
18142df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
18152df76c16SMatt Jacob 		 */
18162df76c16SMatt 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",
18172df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
1818e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
18192df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
18202df76c16SMatt Jacob 		return;
18212df76c16SMatt Jacob 	}
18222df76c16SMatt Jacob 	nphdl = lp->handle;
18232df76c16SMatt Jacob 
18242df76c16SMatt Jacob 	/*
18252df76c16SMatt Jacob 	 * Get the tstate pointer
18262df76c16SMatt Jacob 	 */
18272df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
18282df76c16SMatt Jacob 	if (tptr == NULL) {
18292df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
18302df76c16SMatt Jacob 		if (tptr == NULL) {
18316af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
18326af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
18336af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
1834387d8239SMatt Jacob 			if (lun == 0) {
1835352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1836387d8239SMatt Jacob 			} else {
18372df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1838387d8239SMatt Jacob 			}
18392df76c16SMatt Jacob 			return;
18402df76c16SMatt Jacob 		}
18412df76c16SMatt Jacob 	}
18422df76c16SMatt Jacob 
18432df76c16SMatt Jacob 	/*
18442df76c16SMatt Jacob 	 * Start any commands pending resources first.
18452df76c16SMatt Jacob 	 */
18468290ea90SAlexander Motin 	if (isp_atio_restart(isp, chan, tptr))
18472df76c16SMatt Jacob 		goto noresrc;
18482df76c16SMatt Jacob 
18492df76c16SMatt Jacob 	/*
18502df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
18512df76c16SMatt Jacob 	 */
18522df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
18532df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
18542df76c16SMatt Jacob 		return;
18552df76c16SMatt Jacob 	}
18562df76c16SMatt Jacob 
18572df76c16SMatt Jacob 	/*
18582df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
18592df76c16SMatt Jacob 	 */
18602df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
18612df76c16SMatt Jacob 	if (atiop == NULL) {
18622df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
18632df76c16SMatt Jacob 		goto noresrc;
18642df76c16SMatt Jacob 	}
18652df76c16SMatt Jacob 
18668290ea90SAlexander Motin 	oatp = isp_find_atpd(isp, chan, aep->at_rxid);
18679e7d423dSMatt Jacob 	if (oatp) {
1868387d8239SMatt 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",
18699e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
18702df76c16SMatt Jacob 		/*
18712df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
18722df76c16SMatt Jacob 		 */
18732df76c16SMatt Jacob 		goto noresrc;
18742df76c16SMatt Jacob 	}
18758290ea90SAlexander Motin 	atp = isp_get_atpd(isp, chan, aep->at_rxid);
1876523ea374SAlexander Motin 	if (atp == NULL) {
1877523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1878523ea374SAlexander Motin 		goto noresrc;
1879523ea374SAlexander Motin 	}
1880387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
18812df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
18822df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1883d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1884d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
18856ddb8972SAlexander Motin 	atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
18862df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
18872df76c16SMatt Jacob 	atiop->sense_len = 0;
18882df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
18892df76c16SMatt Jacob 	if (cdbxlen) {
18902df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
18912df76c16SMatt Jacob 	}
18922df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
18932df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
18942df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
18952df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
18962df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
18972df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
18982df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
18995e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19002df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
19012df76c16SMatt Jacob 		break;
19022df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
19035e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19042df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
19052df76c16SMatt Jacob 		break;
19062df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
19075e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
19082df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
19092df76c16SMatt Jacob 		break;
19102df76c16SMatt Jacob 	default:
19112df76c16SMatt Jacob 		/* FALLTHROUGH */
19122df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
19132df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
19142df76c16SMatt Jacob 		atiop->tag_action = 0;
19152df76c16SMatt Jacob 		break;
19162df76c16SMatt Jacob 	}
19172df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
19182df76c16SMatt Jacob 	atp->bytes_xfered = 0;
19192df76c16SMatt Jacob 	atp->lun = lun;
19202df76c16SMatt Jacob 	atp->nphdl = nphdl;
19213f072d69SAlexander Motin 	atp->sid = sid;
19223f072d69SAlexander Motin 	atp->did = did;
19232df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
19249e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
19252df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
19262df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
19272df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
19286af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
19296af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
19302df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
19312df76c16SMatt Jacob 	return;
19322df76c16SMatt Jacob noresrc:
19338290ea90SAlexander Motin 	if (atp)
19348290ea90SAlexander Motin 		isp_put_atpd(isp, chan, atp);
19358290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, chan);
19362df76c16SMatt Jacob 	if (ntp == NULL) {
19372df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
19382df76c16SMatt Jacob 		return;
19392df76c16SMatt Jacob 	}
19408290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
19418290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
19422df76c16SMatt Jacob }
19432df76c16SMatt Jacob 
1944387d8239SMatt Jacob 
1945387d8239SMatt Jacob /*
1946387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
1947387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
1948387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
1949387d8239SMatt Jacob  * transaction.
1950387d8239SMatt Jacob  */
1951387d8239SMatt Jacob static void
19528290ea90SAlexander Motin isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1953387d8239SMatt Jacob {
1954387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
1955387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1956387d8239SMatt Jacob 	union ccb *ccb;
1957387d8239SMatt Jacob 
1958387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
1959387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1960387d8239SMatt Jacob 	ccb = atp->srr_ccb;
1961387d8239SMatt Jacob 	atp->srr_ccb = NULL;
1962387d8239SMatt Jacob 	atp->nsrr++;
1963387d8239SMatt Jacob 	if (ccb == NULL) {
196494dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1965387d8239SMatt Jacob 		goto fail;
1966387d8239SMatt Jacob 	}
1967387d8239SMatt Jacob 
1968387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
1969387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
1970387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1971387d8239SMatt Jacob 
1972387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
1973387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
1974387d8239SMatt Jacob 		/*
1975387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
1976387d8239SMatt Jacob 		 */
1977387d8239SMatt Jacob 		atp->sendst = 0;
1978387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
1979387d8239SMatt Jacob 		if (ccb_len == 0) {
198094dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1981387d8239SMatt Jacob 			goto mdp;
1982387d8239SMatt Jacob 		}
1983387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
198494dff771SMatt 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);
1985387d8239SMatt Jacob 			goto mdp;
1986387d8239SMatt Jacob 		}
198794dff771SMatt 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);
1988387d8239SMatt Jacob 		break;
1989387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
199094dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1991387d8239SMatt Jacob 		atp->sendst = 1;
1992387d8239SMatt Jacob 		/*
1993387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
1994387d8239SMatt Jacob 		 */
1995387d8239SMatt Jacob 		break;
1996387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
1997387d8239SMatt Jacob 		/*
1998387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
1999387d8239SMatt Jacob 		 */
2000387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2001387d8239SMatt Jacob 		goto fail;
2002387d8239SMatt Jacob 
2003387d8239SMatt Jacob 	default:
2004387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2005387d8239SMatt Jacob 		goto fail;
2006387d8239SMatt Jacob 	}
2007387d8239SMatt Jacob 
2008387d8239SMatt Jacob 	/*
2009387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2010387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2011387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2012387d8239SMatt Jacob 	 */
2013387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2014387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2015387d8239SMatt Jacob 		goto fail;
2016387d8239SMatt Jacob 	}
2017387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2018387d8239SMatt Jacob 	return;
2019387d8239SMatt Jacob fail:
2020387d8239SMatt Jacob 	inot->in_reserved = 1;
2021387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2022387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2023387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2024387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2025387d8239SMatt Jacob 	return;
2026387d8239SMatt Jacob mdp:
2027387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2028387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2029387d8239SMatt Jacob 		goto fail;
2030387d8239SMatt Jacob 	}
2031387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2032387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2033387d8239SMatt Jacob 	/*
2034387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2035387d8239SMatt Jacob 	 */
2036387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2037387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2038387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2039387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2040387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2041387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2042387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2043387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2044387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2045387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2046387d8239SMatt Jacob }
2047387d8239SMatt Jacob 
2048387d8239SMatt Jacob 
2049387d8239SMatt Jacob static void
20505a5632c2SAlexander Motin isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
2051387d8239SMatt Jacob {
20525a5632c2SAlexander Motin 	in_fcentry_24xx_t *inot = notify->nt_lreserved;
2053387d8239SMatt Jacob 	atio_private_data_t *atp;
20545a5632c2SAlexander Motin 	uint32_t tag = notify->nt_tagval & 0xffffffff;
2055387d8239SMatt Jacob 
20565a5632c2SAlexander Motin 	atp = isp_find_atpd(isp, notify->nt_channel, tag);
2057387d8239SMatt Jacob 	if (atp == NULL) {
20585a5632c2SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
20595a5632c2SAlexander Motin 		    __func__, tag);
2060387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2061387d8239SMatt Jacob 		return;
2062387d8239SMatt Jacob 	}
2063387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2064387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
20655a5632c2SAlexander Motin 	isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
20665a5632c2SAlexander Motin 	    inot->in_rxid, inot->in_flags, inot->in_srr_iu,
20675a5632c2SAlexander Motin 	    ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
2068387d8239SMatt Jacob 	if (atp->srr_ccb)
20698290ea90SAlexander Motin 		isp_handle_srr_start(isp, atp);
2070387d8239SMatt Jacob }
2071387d8239SMatt Jacob 
20722df76c16SMatt Jacob static void
20739cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2074d81ba9d5SMatt Jacob {
2075d81ba9d5SMatt Jacob 	union ccb *ccb;
2076a6036a44SAlexander Motin 	int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
20772df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
20782df76c16SMatt Jacob 	int bus;
2079a6036a44SAlexander Motin 	uint32_t handle, data_requested, resid;
2080d81ba9d5SMatt Jacob 
20812df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2082970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
20832df76c16SMatt Jacob 	if (ccb == NULL) {
20842df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
20852df76c16SMatt Jacob 		return;
20862df76c16SMatt Jacob 	}
2087970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
2088a6036a44SAlexander Motin 	resid = data_requested = PISP_PCMD(ccb)->datalen;
2089387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2090387d8239SMatt Jacob 
20912df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
2092387d8239SMatt Jacob 	if (IS_24XX(isp)) {
20938290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2094387d8239SMatt Jacob 	} else {
20958290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2096387d8239SMatt Jacob 	}
2097387d8239SMatt Jacob 	if (atp == NULL) {
2098af207637SAlexander Motin 		/*
20998656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
21008656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
21018656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2102af207637SAlexander Motin 		 */
21038290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2104af207637SAlexander Motin 	}
2105af207637SAlexander Motin 	if (atp == NULL) {
2106387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2107387d8239SMatt Jacob 		return;
2108387d8239SMatt Jacob 	}
2109387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
211094dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2111387d8239SMatt Jacob 	atp->ctcnt -= 1;
2112387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2113387d8239SMatt Jacob 
21142df76c16SMatt Jacob 	if (IS_24XX(isp)) {
21152df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2116d81ba9d5SMatt Jacob 
2117387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2118387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2119387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
21208290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
21212df76c16SMatt Jacob 			return;
21222df76c16SMatt Jacob 		}
2123387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
212487de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
212587de303cSAlexander Motin 			    (atp->sendst == 0);
2126387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2127387d8239SMatt Jacob 		} else {
21282df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
21292df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
213094dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2131a6036a44SAlexander Motin 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
21322df76c16SMatt Jacob 				resid = ct->ct_resid;
21332df76c16SMatt Jacob 		}
213494dff771SMatt 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),
213594dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
21363e6deb33SAlexander Motin 	} else {
21372df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2138387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2139387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2140387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
21418290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
2142387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
21432df76c16SMatt Jacob 			return;
2144570c7a3fSMatt Jacob 		}
2145387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
214687de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
214787de303cSAlexander Motin 			    (atp->sendst == 0);
2148387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2149387d8239SMatt Jacob 		} else {
2150d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2151d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
215294dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2153a6036a44SAlexander Motin 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2154a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
215553036e92SMatt Jacob 		}
215694dff771SMatt 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),
215794dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
21585d571944SMatt Jacob 	}
2159387d8239SMatt Jacob 	if (ok) {
2160a6036a44SAlexander Motin 		if (data_requested > 0) {
2161a6036a44SAlexander Motin 			atp->bytes_xfered += data_requested - resid;
2162a6036a44SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
2163a6036a44SAlexander Motin 			    (data_requested - resid);
2164387d8239SMatt Jacob 		}
2165a6036a44SAlexander Motin 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2166387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2167387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2168387d8239SMatt Jacob 	} else {
2169387d8239SMatt Jacob 		notify_cam = 1;
2170387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2171387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2172387d8239SMatt Jacob 		else
2173387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2174387d8239SMatt Jacob 	}
2175387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2176a1bc34c6SMatt Jacob 
2177a1bc34c6SMatt Jacob 	/*
2178387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2179387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2180d81ba9d5SMatt Jacob 	 */
218194dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
218294dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2183f48ce188SMatt Jacob 	if (notify_cam == 0) {
2184387d8239SMatt Jacob 		if (atp->sendst) {
2185387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2186387d8239SMatt Jacob 		}
21872df76c16SMatt Jacob 		return;
2188f48ce188SMatt Jacob 	}
2189d81ba9d5SMatt Jacob 
2190387d8239SMatt Jacob 	/*
219187de303cSAlexander Motin 	 * We are done with this ATIO if we successfully sent status.
219287de303cSAlexander Motin 	 * In all other cases expect either another CTIO or XPT_ABORT.
21938290ea90SAlexander Motin 	 */
219487de303cSAlexander Motin 	if (ok && sentstatus)
21958290ea90SAlexander Motin 		isp_put_atpd(isp, bus, atp);
21968290ea90SAlexander Motin 
21978290ea90SAlexander Motin 	/*
2198387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2199387d8239SMatt Jacob 	 *
2200387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2201387d8239SMatt Jacob 	 *
2202387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2203387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2204387d8239SMatt Jacob 	 * and try again in case we didn't have
2205387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2206387d8239SMatt Jacob 	 * once the putback is done we do the completion
2207387d8239SMatt Jacob 	 * call.
2208387d8239SMatt Jacob 	 */
2209387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2210a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2211387d8239SMatt Jacob 	} else {
2212387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2213d81ba9d5SMatt Jacob 	}
2214d81ba9d5SMatt Jacob }
2215570c7a3fSMatt Jacob 
22162df76c16SMatt Jacob static int
221796b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
22182df76c16SMatt Jacob {
22192df76c16SMatt Jacob 
22202df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
22212df76c16SMatt 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);
2222570c7a3fSMatt Jacob 		return (0);
2223570c7a3fSMatt Jacob 	}
22242df76c16SMatt Jacob 
22252df76c16SMatt Jacob 	/*
22262df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
22272df76c16SMatt Jacob 	 */
22282df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
22292df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
22302df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
22312df76c16SMatt Jacob 		fcportdb_t *lp;
22322df76c16SMatt Jacob 		uint32_t sid;
22332df76c16SMatt Jacob 		uint16_t nphdl;
22342df76c16SMatt Jacob 
22352df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2236eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
22372df76c16SMatt Jacob 			nphdl = lp->handle;
22382df76c16SMatt Jacob 		} else {
22392df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
22402df76c16SMatt Jacob 		}
22412df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
22422df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
22432df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
22442df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
22452df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
22462df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
22472df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
22482df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
22492df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
22502df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
22512df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
225296b5475bSAlexander Motin 		if (rsp != 0) {
225396b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
225496b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
225596b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
225696b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
225796b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
225896b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
225996b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
226096b5475bSAlexander Motin 		}
22612df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
22622df76c16SMatt Jacob 	}
22632df76c16SMatt Jacob 
22642df76c16SMatt Jacob 	/*
22652df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
22662df76c16SMatt Jacob 	 */
22672df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
22682df76c16SMatt Jacob 
22692df76c16SMatt Jacob 		/*
22702df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
22712df76c16SMatt Jacob 		 */
22722df76c16SMatt Jacob 		if (mp->nt_need_ack) {
22732df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
22742df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
22752df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
22762df76c16SMatt Jacob 
22772df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
22782df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
22792df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
22802df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
22812df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
22822df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
22832df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
22842df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
22852df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
22862df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
22872df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
22882df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
22892df76c16SMatt Jacob 				return (ENOMEM);
22902df76c16SMatt Jacob 			}
22912df76c16SMatt Jacob 			mp->nt_need_ack = 0;
22922df76c16SMatt Jacob 		}
22932df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
22942df76c16SMatt Jacob 			return (ENOMEM);
22952df76c16SMatt Jacob 		} else {
22962df76c16SMatt Jacob 			return (0);
22972df76c16SMatt Jacob 		}
22982df76c16SMatt Jacob 	}
22992df76c16SMatt Jacob 
23002df76c16SMatt Jacob 	/*
23012df76c16SMatt Jacob 	 * Handle logout cases here
23022df76c16SMatt Jacob 	 */
23032df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
23042df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
23052df76c16SMatt Jacob 	}
23062df76c16SMatt Jacob 
23072df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
23082df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
23092df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
23102df76c16SMatt Jacob 		}
23112df76c16SMatt Jacob 	}
23122df76c16SMatt Jacob 
23132df76c16SMatt Jacob 	/*
23142df76c16SMatt Jacob 	 * General purpose acknowledgement
23152df76c16SMatt Jacob 	 */
23162df76c16SMatt Jacob 	if (mp->nt_need_ack) {
23172df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2318387d8239SMatt Jacob 		/*
2319387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2320387d8239SMatt Jacob 		 */
23212df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
23222df76c16SMatt Jacob 	}
23232df76c16SMatt Jacob 	return (0);
23242df76c16SMatt Jacob }
23252df76c16SMatt Jacob 
23262df76c16SMatt Jacob /*
2327b1ce21c6SRebecca Cran  * Handle task management functions.
23282df76c16SMatt Jacob  *
23292df76c16SMatt Jacob  * We show up here with a notify structure filled out.
23302df76c16SMatt Jacob  *
23312df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
23322df76c16SMatt Jacob  */
23332df76c16SMatt Jacob static void
23342df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
23352df76c16SMatt Jacob {
23362df76c16SMatt Jacob 	tstate_t *tptr;
23372df76c16SMatt Jacob 	fcportdb_t *lp;
23382df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
23392df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
2340981ffc4eSAlexander Motin 	atio_private_data_t *atp;
23412df76c16SMatt Jacob 	lun_id_t lun;
23422df76c16SMatt Jacob 
234315c62456SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
23442df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
23452df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
2346981ffc4eSAlexander Motin 		if (notify->nt_tagval == TAG_ANY) {
23472df76c16SMatt Jacob 			lun = CAM_LUN_WILDCARD;
23482df76c16SMatt Jacob 		} else {
2349981ffc4eSAlexander Motin 			atp = isp_find_atpd(isp, notify->nt_channel,
2350981ffc4eSAlexander Motin 			    notify->nt_tagval & 0xffffffff);
2351981ffc4eSAlexander Motin 			lun = atp ? atp->lun : CAM_LUN_WILDCARD;
2352981ffc4eSAlexander Motin 		}
2353981ffc4eSAlexander Motin 	} else {
23542df76c16SMatt Jacob 		lun = notify->nt_lun;
23552df76c16SMatt Jacob 	}
23562df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
23572df76c16SMatt Jacob 	if (tptr == NULL) {
23582df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
23592df76c16SMatt Jacob 		if (tptr == NULL) {
2360123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
23612df76c16SMatt Jacob 			goto bad;
23622df76c16SMatt Jacob 		}
23632df76c16SMatt Jacob 	}
23642df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
23652df76c16SMatt Jacob 	if (inot == NULL) {
2366123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
23672df76c16SMatt Jacob 		goto bad;
23682df76c16SMatt Jacob 	}
23692df76c16SMatt Jacob 
23706ddb8972SAlexander Motin 	inot->ccb_h.target_id = ISP_MAX_TARGETS(isp);
23716ddb8972SAlexander Motin 	inot->ccb_h.target_lun = lun;
2372eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2373d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
23742df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
23752df76c16SMatt Jacob 	} else {
2376d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
23772df76c16SMatt Jacob 	}
23782df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
23792df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
23802df76c16SMatt Jacob 
23812df76c16SMatt Jacob 	switch (notify->nt_ncode) {
23822df76c16SMatt Jacob 	case NT_ABORT_TASK:
23838290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
23842df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
23852df76c16SMatt Jacob 		break;
23862df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
23878290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
23882df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
23892df76c16SMatt Jacob 		break;
23902df76c16SMatt Jacob 	case NT_CLEAR_ACA:
23912df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
23922df76c16SMatt Jacob 		break;
23932df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
23942df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
23952df76c16SMatt Jacob 		break;
23962df76c16SMatt Jacob 	case NT_LUN_RESET:
23972df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
23982df76c16SMatt Jacob 		break;
23992df76c16SMatt Jacob 	case NT_TARGET_RESET:
24002df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
24012df76c16SMatt Jacob 		break;
2402c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2403c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2404c98d2b1fSAlexander Motin 		break;
2405c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2406c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2407c98d2b1fSAlexander Motin 		break;
24082df76c16SMatt Jacob 	default:
2409123055f0SNathan 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);
24102df76c16SMatt Jacob 		goto bad;
24112df76c16SMatt Jacob 	}
24122df76c16SMatt Jacob 
24138290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, notify->nt_channel);
24142df76c16SMatt Jacob 	if (ntp == NULL) {
24152df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
24162df76c16SMatt Jacob 		goto bad;
24172df76c16SMatt Jacob 	}
24188290ea90SAlexander Motin 	ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
24192df76c16SMatt Jacob 	if (notify->nt_lreserved) {
24208290ea90SAlexander Motin 		ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
24218290ea90SAlexander Motin 		ntp->nt.nt_lreserved = &ntp->data;
24222df76c16SMatt Jacob 	}
24238290ea90SAlexander Motin 	ntp->seq_id = notify->nt_tagval;
24248290ea90SAlexander Motin 	ntp->tag_id = notify->nt_tagval >> 32;
24252df76c16SMatt Jacob 
24262df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2427d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
24282df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
24292df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
24302df76c16SMatt Jacob 	return;
24312df76c16SMatt Jacob bad:
243244a2a27aSAlexander Motin 	if (notify->nt_need_ack) {
24332df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2434387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2435387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2436387d8239SMatt Jacob 			}
24372df76c16SMatt Jacob 		} else {
2438387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
24392df76c16SMatt Jacob 		}
24402df76c16SMatt Jacob 	}
24412df76c16SMatt Jacob }
24422df76c16SMatt Jacob 
24432df76c16SMatt Jacob static void
24448290ea90SAlexander Motin isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
24452df76c16SMatt Jacob {
24468290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
24478290ea90SAlexander Motin 	inot_private_data_t *ntp, *tmp;
24488290ea90SAlexander Motin 	uint32_t this_tag_id;
24492df76c16SMatt Jacob 
24502df76c16SMatt Jacob 	/*
24512df76c16SMatt Jacob 	 * First, clean any commands pending restart
24522df76c16SMatt Jacob 	 */
24538290ea90SAlexander Motin 	STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
24548290ea90SAlexander Motin 		if (IS_24XX(isp))
24558290ea90SAlexander Motin 			this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
24568290ea90SAlexander Motin 		else
24578290ea90SAlexander Motin 			this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
24582df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
24598290ea90SAlexander Motin 			isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
24608290ea90SAlexander Motin 			    ECMD_TERMINATE, 0);
24618290ea90SAlexander Motin 			isp_put_ntpd(isp, chan, ntp);
24628290ea90SAlexander Motin 			STAILQ_REMOVE(&tptr->restart_queue, ntp,
24638290ea90SAlexander Motin 			    inot_private_data, next);
24642df76c16SMatt Jacob 		}
24652df76c16SMatt Jacob 	}
24662df76c16SMatt Jacob 
24672df76c16SMatt Jacob 	/*
24682df76c16SMatt Jacob 	 * Now mark other ones dead as well.
24692df76c16SMatt Jacob 	 */
24708290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
24718290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
24728290ea90SAlexander Motin 		if (atp->lun != tptr->ts_lun)
24738290ea90SAlexander Motin 			continue;
24748290ea90SAlexander Motin 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
24752df76c16SMatt Jacob 			atp->dead = 1;
24762df76c16SMatt Jacob 	}
24772df76c16SMatt Jacob }
2478d81ba9d5SMatt Jacob #endif
2479d81ba9d5SMatt Jacob 
2480478f8a96SJustin T. Gibbs static void
24811dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2482478f8a96SJustin T. Gibbs {
2483478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
2484e95725cbSMatt Jacob 	int bus, tgt;
24859cd7268eSMatt Jacob 	ispsoftc_t *isp;
2486478f8a96SJustin T. Gibbs 
2487478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
24889cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
2489e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
2490e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
2491e95725cbSMatt Jacob 
2492478f8a96SJustin T. Gibbs 	switch (code) {
2493478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
2494ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
24951dae40ebSMatt Jacob 			uint16_t oflags, nflags;
24962df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
2497478f8a96SJustin T. Gibbs 
249841ed683eSMatt Jacob 			if (tgt >= 0) {
24999ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
25009ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
2501a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
2502478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
2503478f8a96SJustin T. Gibbs 				}
25049ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
25059ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
2506478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
25072df76c16SMatt Jacob 				sdp->update = 1;
25082df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
25099ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
2510478f8a96SJustin T. Gibbs 			}
251141ed683eSMatt Jacob 		}
2512478f8a96SJustin T. Gibbs 		break;
2513478f8a96SJustin T. Gibbs 	default:
25143c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2515478f8a96SJustin T. Gibbs 		break;
2516478f8a96SJustin T. Gibbs 	}
2517478f8a96SJustin T. Gibbs }
2518478f8a96SJustin T. Gibbs 
2519478f8a96SJustin T. Gibbs static void
2520c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
2521478f8a96SJustin T. Gibbs {
25229cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
2523126ec864SMatt Jacob 
25240e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
2525478f8a96SJustin T. Gibbs }
2526478f8a96SJustin T. Gibbs 
2527ab6c4b31SMatt Jacob 
25282df76c16SMatt Jacob static void
25292df76c16SMatt Jacob isp_watchdog(void *arg)
2530cc8df88bSMatt Jacob {
25312df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
25322df76c16SMatt Jacob 	ispsoftc_t *isp;
2533e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
2534b85389e1SMatt Jacob 
25352df76c16SMatt Jacob 	isp = XS_ISP(xs);
25362df76c16SMatt Jacob 
2537cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
2538e95725cbSMatt Jacob 
2539e95725cbSMatt Jacob 	/*
2540e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2541e95725cbSMatt Jacob 	 */
2542e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
25430e6bc811SAlexander Motin 		ISP_RUN_ISR(isp);
2544e95725cbSMatt Jacob 		ohandle = handle;
2545e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
2546e95725cbSMatt Jacob 	}
2547c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
25481fcf5debSMatt Jacob 		/*
2549c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
2550c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
2551c8b8a2c4SMatt Jacob 		 *
2552c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
2553c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
2554c8b8a2c4SMatt Jacob 		 * back separately.
25551fcf5debSMatt Jacob 		 */
2556c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2557c8b8a2c4SMatt Jacob 			return;
2558c8b8a2c4SMatt Jacob 		}
25591fcf5debSMatt Jacob 
25601fcf5debSMatt Jacob 		/*
2561c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
2562c8b8a2c4SMatt Jacob 		 * fact have been completed.
2563c8b8a2c4SMatt Jacob 		 */
2564c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
2565c8b8a2c4SMatt Jacob 
2566c8b8a2c4SMatt Jacob 		/*
2567c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
2568c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
2569c8b8a2c4SMatt Jacob 		 */
2570c8b8a2c4SMatt Jacob 		if (xs == NULL) {
2571c8b8a2c4SMatt Jacob 			return;
2572c8b8a2c4SMatt Jacob 		}
2573c8b8a2c4SMatt Jacob 
2574c8b8a2c4SMatt Jacob 		/*
2575c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
25761fcf5debSMatt Jacob 		 */
2577f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
2578f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
2579f6e75de2SMatt Jacob 		}
2580cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
2581c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
258257b6261fSKenneth D. Merry 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
2583cc8df88bSMatt Jacob 		isp_done(xs);
2584e95725cbSMatt Jacob 	} else {
2585e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
2586e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2587e95725cbSMatt Jacob 		} else {
2588e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2589e95725cbSMatt Jacob 		}
2590b85389e1SMatt Jacob 	}
2591f7c631bcSMatt Jacob }
2592f7c631bcSMatt Jacob 
2593f7c631bcSMatt Jacob static void
25945704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2595f7c631bcSMatt Jacob {
2596ffcf6651SMatt Jacob 	union ccb *ccb;
25972df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
25982df76c16SMatt Jacob 
2599ffcf6651SMatt Jacob 	/*
26000e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2601ffcf6651SMatt Jacob 	 */
26028008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
2603ffcf6651SMatt Jacob 	if (ccb == NULL) {
26042df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2605ffcf6651SMatt Jacob 		return;
2606ffcf6651SMatt Jacob 	}
2607e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2608e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2609ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2610ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
2611ffcf6651SMatt Jacob 		return;
2612ffcf6651SMatt Jacob 	}
2613ffcf6651SMatt Jacob 	xpt_rescan(ccb);
2614ffcf6651SMatt Jacob }
2615ffcf6651SMatt Jacob 
2616ffcf6651SMatt Jacob static void
26175704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2618ffcf6651SMatt Jacob {
2619ffcf6651SMatt Jacob 	struct cam_path *tp;
26202df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
26212df76c16SMatt Jacob 
26222df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2623ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
2624ffcf6651SMatt Jacob 		xpt_free_path(tp);
2625f7c631bcSMatt Jacob 	}
2626f7c631bcSMatt Jacob }
2627f7c631bcSMatt Jacob 
2628f7c631bcSMatt Jacob /*
2629f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
2630f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
2631f7c631bcSMatt Jacob  * gone away.
2632f7c631bcSMatt Jacob  *
2633f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
2634f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
2635f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
2636f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
2637f7c631bcSMatt Jacob  */
2638f7c631bcSMatt Jacob static void
2639f7c631bcSMatt Jacob isp_gdt(void *arg)
2640f7c631bcSMatt Jacob {
26412df76c16SMatt Jacob 	struct isp_fc *fc = arg;
2642de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2643de461933SMatt Jacob }
2644de461933SMatt Jacob 
2645de461933SMatt Jacob static void
2646de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
2647de461933SMatt Jacob {
2648de461933SMatt Jacob 	struct isp_fc *fc = arg;
26492df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
26502df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
2651f7c631bcSMatt Jacob 	fcportdb_t *lp;
2652e68eef14SAlexander Motin 	struct ac_contract ac;
2653e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2654766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
2655f7c631bcSMatt Jacob 
2656de461933SMatt Jacob 	ISP_LOCK(isp);
2657de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2658f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
26592df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
2660f7c631bcSMatt Jacob 
2661f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2662f7c631bcSMatt Jacob 			continue;
2663f7c631bcSMatt Jacob 		}
2664427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
2665427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
2666f7c631bcSMatt Jacob 			more_to_do++;
2667f7c631bcSMatt Jacob 			continue;
2668f7c631bcSMatt Jacob 		}
2669e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2670e68eef14SAlexander Motin 		if (lp->is_target) {
2671e68eef14SAlexander Motin 			lp->is_target = 0;
2672766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2673f7c631bcSMatt Jacob 		}
2674e68eef14SAlexander Motin 		if (lp->is_initiator) {
2675e68eef14SAlexander Motin 			lp->is_initiator = 0;
2676e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2677e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2678e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2679e68eef14SAlexander Motin 			adc->port = lp->portid;
2680d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2681e68eef14SAlexander Motin 			adc->arrived = 0;
2682e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2683e68eef14SAlexander Motin 		}
2684e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
2685e68eef14SAlexander Motin 	}
2686a01f5aebSMatt Jacob 	if (fc->ready) {
2687f7c631bcSMatt Jacob 		if (more_to_do) {
26882df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
2689f7c631bcSMatt Jacob 		} else {
2690de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
2691387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2692a01f5aebSMatt Jacob 		}
2693f7c631bcSMatt Jacob 	}
2694de461933SMatt Jacob 	ISP_UNLOCK(isp);
2695f7c631bcSMatt Jacob }
2696f7c631bcSMatt Jacob 
2697f7c631bcSMatt Jacob /*
2698e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
2699e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
2700e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
2701f7c631bcSMatt Jacob  *
2702f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
2703f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
2704f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2705f7c631bcSMatt Jacob  */
2706f7c631bcSMatt Jacob static void
2707e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
2708f7c631bcSMatt Jacob {
2709e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2710e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2711e561aa79SAlexander Motin 
2712e561aa79SAlexander Motin 	if (fc->loop_down_time)
2713e561aa79SAlexander Motin 		return;
2714e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2715e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
2716e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
2717e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
2718e561aa79SAlexander Motin 	wakeup(fc);
2719de461933SMatt Jacob }
2720de461933SMatt Jacob 
2721de461933SMatt Jacob static void
2722e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
2723de461933SMatt Jacob {
2724e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2725e561aa79SAlexander Motin 
2726e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2727e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
2728e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2729e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2730e561aa79SAlexander Motin }
2731e561aa79SAlexander Motin 
2732e561aa79SAlexander Motin static void
2733e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
2734e561aa79SAlexander Motin {
2735e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2736e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2737f7c631bcSMatt Jacob 	fcportdb_t *lp;
2738e68eef14SAlexander Motin 	struct ac_contract ac;
2739e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2740766a65a5SAlexander Motin 	int dbidx, i;
2741f7c631bcSMatt Jacob 
2742e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
27430a70657fSMatt Jacob 
2744f7c631bcSMatt Jacob 	/*
2745f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
2746f7c631bcSMatt Jacob 	 */
2747f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2748e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
2749f7c631bcSMatt Jacob 
2750e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
2751f7c631bcSMatt Jacob 			continue;
2752f7c631bcSMatt Jacob 
2753e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
2754e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
2755e95725cbSMatt Jacob 
2756970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2757e95725cbSMatt Jacob 				continue;
2758e95725cbSMatt Jacob 			}
2759e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2760e95725cbSMatt Jacob 				continue;
2761e95725cbSMatt Jacob                         }
2762766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
2763e95725cbSMatt Jacob 				continue;
2764e95725cbSMatt Jacob 			}
27656af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
27666af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
27676af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
276857b6261fSKenneth D. Merry 
276957b6261fSKenneth D. Merry 			/*
277057b6261fSKenneth D. Merry 			 * Just like in isp_watchdog, abort the outstanding
277157b6261fSKenneth D. Merry 			 * command or immediately free its resources if it is
277257b6261fSKenneth D. Merry 			 * not active
277357b6261fSKenneth D. Merry 			 */
277457b6261fSKenneth D. Merry 			if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
277557b6261fSKenneth D. Merry 				continue;
277657b6261fSKenneth D. Merry 			}
277757b6261fSKenneth D. Merry 
277857b6261fSKenneth D. Merry 			if (XS_XFRLEN(xs)) {
277957b6261fSKenneth D. Merry 				ISP_DMAFREE(isp, xs, isp->isp_xflist[i].handle);
278057b6261fSKenneth D. Merry 			}
278157b6261fSKenneth D. Merry 			isp_destroy_handle(isp, isp->isp_xflist[i].handle);
278257b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx could not be aborted and was destroyed",
278357b6261fSKenneth D. Merry 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
278457b6261fSKenneth D. Merry 			    (uintmax_t)XS_LUN(xs));
278557b6261fSKenneth D. Merry 			XS_SETERR(xs, HBA_BUSRESET);
278657b6261fSKenneth D. Merry 			isp_done(xs);
2787e95725cbSMatt Jacob 		}
2788e95725cbSMatt Jacob 
2789e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2790e68eef14SAlexander Motin 		if (lp->is_target) {
2791e68eef14SAlexander Motin 			lp->is_target = 0;
2792766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2793f7c631bcSMatt Jacob 		}
2794e68eef14SAlexander Motin 		if (lp->is_initiator) {
2795e68eef14SAlexander Motin 			lp->is_initiator = 0;
2796e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2797e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2798e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2799e68eef14SAlexander Motin 			adc->port = lp->portid;
2800d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2801e68eef14SAlexander Motin 			adc->arrived = 0;
2802e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2803e95725cbSMatt Jacob 		}
2804e68eef14SAlexander Motin 	}
2805e68eef14SAlexander Motin 
2806e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2807e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2808cc8df88bSMatt Jacob }
2809cc8df88bSMatt Jacob 
28105d571944SMatt Jacob static void
28115d571944SMatt Jacob isp_kthread(void *arg)
28125d571944SMatt Jacob {
28132df76c16SMatt Jacob 	struct isp_fc *fc = arg;
28142df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
28152df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
2816e561aa79SAlexander Motin 	int slp = 0, d;
2817e561aa79SAlexander Motin 	int lb, lim;
2818a01f5aebSMatt Jacob 
281913d9c921SAlexander Motin 	ISP_LOCK(isp);
28201d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
2821e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2822e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
28232df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
2824e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2825e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
2826e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
28272df76c16SMatt Jacob 
28282df76c16SMatt Jacob 		/*
28292df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
28302df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
28312df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
28322df76c16SMatt Jacob 		 * check again.
28332df76c16SMatt Jacob 		 *
28342df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
28352df76c16SMatt Jacob 		 */
2836e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
2837e561aa79SAlexander Motin 			slp = 0;
283810365e5aSMatt Jacob 		} else {
283910365e5aSMatt Jacob 			/*
284010365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
2841f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
2842f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
2843f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
284410365e5aSMatt Jacob 			 */
2845e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
2846f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
2847e561aa79SAlexander Motin 			else
28482df76c16SMatt Jacob 				lim = fc->loop_down_limit;
2849e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
2850e561aa79SAlexander Motin 			if (d >= lim)
2851f7c631bcSMatt Jacob 				slp = 0;
2852e561aa79SAlexander Motin 			else if (d < 10)
285310365e5aSMatt Jacob 				slp = 1;
2854e561aa79SAlexander Motin 			else if (d < 30)
2855f7c631bcSMatt Jacob 				slp = 5;
2856e561aa79SAlexander Motin 			else if (d < 60)
2857f7c631bcSMatt Jacob 				slp = 10;
2858e561aa79SAlexander Motin 			else if (d < 120)
2859f7c631bcSMatt Jacob 				slp = 20;
2860e561aa79SAlexander Motin 			else
2861f7c631bcSMatt Jacob 				slp = 30;
2862f44257c2SMatt Jacob 		}
286310365e5aSMatt Jacob 
2864e561aa79SAlexander Motin 		if (slp == 0) {
2865e561aa79SAlexander Motin 			if (lb == LOOP_READY)
2866e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
2867387d8239SMatt Jacob 			else
2868e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
28695d571944SMatt Jacob 		}
2870fdeb9f2fSMatt Jacob 
2871e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2872e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
287313d9c921SAlexander Motin 		msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz);
28745d571944SMatt Jacob 	}
28751d0a1de2SWill Andrews 	fc->num_threads -= 1;
287613d9c921SAlexander Motin 	ISP_UNLOCK(isp);
28771d0a1de2SWill Andrews 	kthread_exit();
28785d571944SMatt Jacob }
28795d571944SMatt Jacob 
28808290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
28818290ea90SAlexander Motin static void
28828290ea90SAlexander Motin isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
28838290ea90SAlexander Motin {
28848290ea90SAlexander Motin 	atio_private_data_t *atp;
28858290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
28868290ea90SAlexander Motin 	struct ccb_hdr *sccb;
28878290ea90SAlexander Motin 	tstate_t *tptr;
28888290ea90SAlexander Motin 
28898290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
28908290ea90SAlexander Motin 	if (tptr != NULL) {
28918290ea90SAlexander Motin 		/* Search for the ATIO among queueued. */
28928290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
28938290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
28948290ea90SAlexander Motin 				continue;
28958290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2896d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2897d10e4cd0SAlexander Motin 			    "Abort FREE ATIO\n");
28988290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
28998290ea90SAlexander Motin 			xpt_done(accb);
29008290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
29018290ea90SAlexander Motin 			return;
29028290ea90SAlexander Motin 		}
29038290ea90SAlexander Motin 	}
29048290ea90SAlexander Motin 
29058290ea90SAlexander Motin 	/* Search for the ATIO among running. */
29068290ea90SAlexander Motin 	atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
29078290ea90SAlexander Motin 	if (atp != NULL) {
29083f072d69SAlexander Motin 		/* Send TERMINATE to firmware. */
29093f072d69SAlexander Motin 		if (!atp->dead && IS_24XX(isp)) {
29103f072d69SAlexander Motin 			uint8_t storage[QENTRY_LEN];
29113f072d69SAlexander Motin 			ct7_entry_t *cto = (ct7_entry_t *) storage;
29123f072d69SAlexander Motin 
29133f072d69SAlexander Motin 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
29143f072d69SAlexander Motin 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
29153f072d69SAlexander Motin 			cto->ct_header.rqs_entry_count = 1;
29163f072d69SAlexander Motin 			cto->ct_nphdl = atp->nphdl;
29173f072d69SAlexander Motin 			cto->ct_rxid = atp->tag;
29183f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
29193f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
29203f072d69SAlexander Motin 			cto->ct_oxid = atp->oxid;
29213f072d69SAlexander Motin 			cto->ct_vpidx = XS_CHANNEL(accb);
29223f072d69SAlexander Motin 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
29233f072d69SAlexander Motin 			isp_target_put_entry(isp, cto);
29243f072d69SAlexander Motin 		}
29258290ea90SAlexander Motin 		isp_put_atpd(isp, XS_CHANNEL(accb), atp);
29268290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
29278290ea90SAlexander Motin 	} else {
29288290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
29298290ea90SAlexander Motin 	}
29308290ea90SAlexander Motin }
29318290ea90SAlexander Motin 
29328290ea90SAlexander Motin static void
29338290ea90SAlexander Motin isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
29348290ea90SAlexander Motin {
29358290ea90SAlexander Motin 	inot_private_data_t *ntp;
29368290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
29378290ea90SAlexander Motin 	struct ccb_hdr *sccb;
29388290ea90SAlexander Motin 	tstate_t *tptr;
29398290ea90SAlexander Motin 
29408290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
29418290ea90SAlexander Motin 	if (tptr != NULL) {
29428290ea90SAlexander Motin 		/* Search for the INOT among queueued. */
29438290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
29448290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
29458290ea90SAlexander Motin 				continue;
29468290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2947d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2948d10e4cd0SAlexander Motin 			    "Abort FREE INOT\n");
29498290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
29508290ea90SAlexander Motin 			xpt_done(accb);
29518290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
29528290ea90SAlexander Motin 			return;
29538290ea90SAlexander Motin 		}
29548290ea90SAlexander Motin 	}
29558290ea90SAlexander Motin 
29568290ea90SAlexander Motin 	/* Search for the INOT among running. */
29578290ea90SAlexander Motin 	ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
29588290ea90SAlexander Motin 	if (ntp != NULL) {
295944a2a27aSAlexander Motin 		if (ntp->nt.nt_need_ack) {
296044a2a27aSAlexander Motin 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
296144a2a27aSAlexander Motin 			    ntp->nt.nt_lreserved);
296244a2a27aSAlexander Motin 		}
29638290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
29648290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
29658290ea90SAlexander Motin 	} else {
29668290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
29678290ea90SAlexander Motin 		return;
29688290ea90SAlexander Motin 	}
29698290ea90SAlexander Motin }
29708290ea90SAlexander Motin #endif
29718290ea90SAlexander Motin 
2972cc8df88bSMatt Jacob static void
2973c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
2974478f8a96SJustin T. Gibbs {
297531c161a6SAlexander Motin 	int bus, tgt, error;
29769cd7268eSMatt Jacob 	ispsoftc_t *isp;
29774663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
297831c161a6SAlexander Motin 	sbintime_t ts;
2979478f8a96SJustin T. Gibbs 
2980478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2981478f8a96SJustin T. Gibbs 
29829cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
298313d9c921SAlexander Motin 	ISP_ASSERT_LOCKED(isp);
298413d9c921SAlexander Motin 	bus = cam_sim_bus(sim);
29855cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
29865cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
29872df76c16SMatt Jacob 
2988478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
2989478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2990478f8a96SJustin T. Gibbs 		/*
2991478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
2992478f8a96SJustin T. Gibbs 		 */
2993478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2994478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2995478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
29965cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
2997478f8a96SJustin T. Gibbs 				break;
2998478f8a96SJustin T. Gibbs 			}
2999478f8a96SJustin T. Gibbs 		}
3000387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
30010470d791SMatt Jacob #ifdef	DIAGNOSTIC
30026af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3003dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3004478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
30056af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
30066af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3007dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3008478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3009478f8a96SJustin T. Gibbs 		}
3010478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3011478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3012478f8a96SJustin T. Gibbs 			break;
3013478f8a96SJustin T. Gibbs 		}
30140470d791SMatt Jacob #endif
30150a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
30160a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
30170a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
30180a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30192df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
30205cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30210a70657fSMatt Jacob 			xpt_done(ccb);
30220a70657fSMatt Jacob 			break;
30230a70657fSMatt Jacob 		}
3024b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
30250470d791SMatt Jacob 		switch (error) {
3026478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3027478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
302831c161a6SAlexander Motin 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
30290a70657fSMatt Jacob 				break;
303031c161a6SAlexander Motin 			/* Give firmware extra 10s to handle timeout. */
303131c161a6SAlexander Motin 			ts = SBT_1MS * ccb->ccb_h.timeout + 10 * SBT_1S;
303231c161a6SAlexander Motin 			callout_reset_sbt(&PISP_PCMD(ccb)->wdog, ts, 0,
303331c161a6SAlexander Motin 			    isp_watchdog, ccb, 0);
3034478f8a96SJustin T. Gibbs 			break;
30350470d791SMatt Jacob 		case CMD_RQLATER:
30366af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
30376af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3038f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30392df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3040387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30410a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3042478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3043478f8a96SJustin T. Gibbs 			break;
30440470d791SMatt Jacob 		case CMD_EAGAIN:
30450a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
30462df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
30472df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3048387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3049478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3050478f8a96SJustin T. Gibbs 			break;
30510470d791SMatt Jacob 		case CMD_COMPLETE:
30520470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
30530470d791SMatt Jacob 			break;
30540470d791SMatt Jacob 		default:
30552df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3056387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
30570a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
30580470d791SMatt Jacob 			xpt_done(ccb);
3059478f8a96SJustin T. Gibbs 		}
3060478f8a96SJustin T. Gibbs 		break;
3061478f8a96SJustin T. Gibbs 
3062d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
30632df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
30642df76c16SMatt Jacob 		if (ccb->cel.enable) {
30652df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
30662df76c16SMatt Jacob 		} else {
30672df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
306867ff51f1SMatt Jacob 		}
306967ff51f1SMatt Jacob 		break;
30702df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3071d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3072d81ba9d5SMatt Jacob 	{
30732df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3074d81ba9d5SMatt Jacob 		if (tptr == NULL) {
30752df76c16SMatt Jacob 			const char *str;
30762df76c16SMatt Jacob 
30778290ea90SAlexander Motin 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
30782df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
30798290ea90SAlexander Motin 			else
30802df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
30818290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
30825c0b756aSAlexander Motin 			    "%s: no state pointer found for %s\n",
30838290ea90SAlexander Motin 			    __func__, str);
30842df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
30855c0b756aSAlexander Motin 			xpt_done(ccb);
3086d81ba9d5SMatt Jacob 			break;
3087d81ba9d5SMatt Jacob 		}
3088387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3089387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3090570c7a3fSMatt Jacob 
3091d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
30928290ea90SAlexander Motin 			ccb->atio.tag_id = 0;
30932df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
30948290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3095d10e4cd0SAlexander Motin 			    "Put FREE ATIO\n");
30962df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
30978290ea90SAlexander Motin 			ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
30982df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
30998290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3100d10e4cd0SAlexander Motin 			    "Put FREE INOT\n");
3101d81ba9d5SMatt Jacob 		}
3102d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3103d81ba9d5SMatt Jacob 		break;
3104d81ba9d5SMatt Jacob 	}
31052df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3106d81ba9d5SMatt Jacob 	{
31072df76c16SMatt Jacob 		inot_private_data_t *ntp;
31082df76c16SMatt Jacob 
31092df76c16SMatt Jacob 		/*
31102df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
31112df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
31122df76c16SMatt Jacob 		 */
31132df76c16SMatt Jacob 		/*
31142df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
31152df76c16SMatt Jacob 		 */
31162df76c16SMatt 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);
31178290ea90SAlexander Motin 		ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
31182df76c16SMatt Jacob 		if (ntp == NULL) {
31192df76c16SMatt 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__,
31202df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
31212df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
31222df76c16SMatt Jacob 			xpt_done(ccb);
3123d81ba9d5SMatt Jacob 			break;
3124d81ba9d5SMatt Jacob 		}
31258290ea90SAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
312696b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
31272df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
31282df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3129387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3130387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
31312df76c16SMatt Jacob 			break;
31322df76c16SMatt Jacob 		}
31338290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
31342df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
31352df76c16SMatt 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);
31362df76c16SMatt Jacob 		xpt_done(ccb);
31372df76c16SMatt Jacob 		break;
31382df76c16SMatt Jacob 	}
31392df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3140387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
31412df76c16SMatt Jacob 		break;
3142d81ba9d5SMatt Jacob #endif
3143478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3144d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3145d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3146d81ba9d5SMatt Jacob 
31472df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3148478f8a96SJustin T. Gibbs 		if (error) {
3149478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3150478f8a96SJustin T. Gibbs 		} else {
3151b44e442eSWill Andrews 			/*
3152b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3153b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3154b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3155b44e442eSWill Andrews 			 */
315621daf914SAlexander Motin 			if (IS_FC(isp))
315721daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3158b44e442eSWill Andrews 
3159478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3160478f8a96SJustin T. Gibbs 		}
3161478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3162478f8a96SJustin T. Gibbs 		break;
3163478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3164d81ba9d5SMatt Jacob 	{
3165d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3166d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3167d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3168d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
31698290ea90SAlexander Motin 			isp_abort_atio(isp, ccb);
31708290ea90SAlexander Motin 			break;
31718290ea90SAlexander Motin 		case XPT_IMMEDIATE_NOTIFY:
31728290ea90SAlexander Motin 			isp_abort_inot(isp, ccb);
3173d81ba9d5SMatt Jacob 			break;
3174d81ba9d5SMatt Jacob #endif
3175d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3176950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3177478f8a96SJustin T. Gibbs 			if (error) {
3178d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3179478f8a96SJustin T. Gibbs 			} else {
3180478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3181478f8a96SJustin T. Gibbs 			}
3182d81ba9d5SMatt Jacob 			break;
3183d81ba9d5SMatt Jacob 		default:
3184d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3185d81ba9d5SMatt Jacob 			break;
3186d81ba9d5SMatt Jacob 		}
31871c0a1eb2SMatt Jacob 		/*
31881c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
31891c0a1eb2SMatt Jacob 		 * complete when control is returned.
31901c0a1eb2SMatt Jacob 		 */
3191478f8a96SJustin T. Gibbs 		break;
3192d81ba9d5SMatt Jacob 	}
3193ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3194478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3195478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
31969ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
31979ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
31989ce9bdafSMatt Jacob 			xpt_done(ccb);
31999ce9bdafSMatt Jacob 			break;
32009ce9bdafSMatt Jacob 		}
3201478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
32022df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
32032df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32042df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
32052df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
32061dae40ebSMatt Jacob 			uint16_t *dptr;
3207ab163f5fSMatt Jacob 
3208b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3209b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3210b61386a4SMatt Jacob 				xpt_done(ccb);
3211b61386a4SMatt Jacob 				break;
3212b61386a4SMatt Jacob 			}
3213b61386a4SMatt Jacob 
3214ab163f5fSMatt Jacob 			/*
32159ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3216ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3217ab163f5fSMatt Jacob 			 * vectored to that location.
3218ab163f5fSMatt Jacob 			 */
32199ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3220ab163f5fSMatt Jacob 
3221ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3222ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3223ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3224ab163f5fSMatt Jacob 				else
3225ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3226ab163f5fSMatt Jacob 			}
3227ab163f5fSMatt Jacob 
3228ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3229ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3230ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3231ab163f5fSMatt Jacob 				else
3232ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3233ab163f5fSMatt Jacob 			}
3234ab163f5fSMatt Jacob 
3235ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3236ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3237ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3238ab163f5fSMatt Jacob 				else
3239ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3240ab163f5fSMatt Jacob 			}
3241ab163f5fSMatt Jacob 
3242ab163f5fSMatt Jacob 			/*
3243ab163f5fSMatt Jacob 			 * XXX: FIX ME
3244ab163f5fSMatt Jacob 			 */
32452df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3246ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
32479ce9bdafSMatt Jacob 				/*
32489ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
32499ce9bdafSMatt Jacob 				 */
32502df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
32512df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3252ab163f5fSMatt Jacob 			} else {
3253ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3254ab163f5fSMatt Jacob 			}
3255123055f0SNathan 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,
32562df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3257478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
32582df76c16SMatt Jacob 			sdp->update = 1;
3259478f8a96SJustin T. Gibbs 		}
3260478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3261478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3262478f8a96SJustin T. Gibbs 		break;
3263478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3264478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3265478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3266ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
32672df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
32682df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32692df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3270478f8a96SJustin T. Gibbs 
3271ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3272ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3273ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3274ab163f5fSMatt Jacob 			cts->transport_version = 0;
3275ab163f5fSMatt Jacob 
32769b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
32779b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3278ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3279ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3280fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3281766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3282766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3283ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3284ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3285ab163f5fSMatt Jacob 				fc->port = lp->portid;
32862df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3287ab163f5fSMatt Jacob 			}
3288ab163f5fSMatt Jacob 		} else {
32892df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
32902df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
32912df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
32921dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3293ab163f5fSMatt Jacob 
3294ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
329583ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
32962df76c16SMatt Jacob 				sdp->update = 1;
32972df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
32989ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
32999ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
33009ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
33014394c92fSMatt Jacob 			} else {
33029ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
33039ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
33049ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
33054394c92fSMatt Jacob 			}
3306478f8a96SJustin T. Gibbs 
3307ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3308ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3309ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3310ab163f5fSMatt Jacob 			cts->transport_version = 2;
3311ab163f5fSMatt Jacob 
3312b61386a4SMatt Jacob 			spi->valid = 0;
3313b61386a4SMatt Jacob 			scsi->valid = 0;
3314b61386a4SMatt Jacob 			spi->flags = 0;
3315b61386a4SMatt Jacob 			scsi->flags = 0;
3316ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3317ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3318ab163f5fSMatt Jacob 			}
33199ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3320ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3321ab163f5fSMatt Jacob 				spi->sync_period = pval;
3322b61386a4SMatt Jacob 			} else {
3323b61386a4SMatt Jacob 				spi->sync_offset = 0;
3324b61386a4SMatt Jacob 				spi->sync_period = 0;
3325b61386a4SMatt Jacob 			}
3326ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3327ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3328ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3329ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3330ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3331ab163f5fSMatt Jacob 			} else {
3332ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3333ab163f5fSMatt Jacob 			}
3334ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3335ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3336b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3337b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3338b61386a4SMatt Jacob 				}
3339ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3340ab163f5fSMatt Jacob 			}
3341123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3342123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3343478f8a96SJustin T. Gibbs 		}
3344478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3345478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3346478f8a96SJustin T. Gibbs 		break;
3347478f8a96SJustin T. Gibbs 
3348478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
33492df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
33502df76c16SMatt Jacob 		xpt_done(ccb);
33512df76c16SMatt Jacob 		break;
3352478f8a96SJustin T. Gibbs 
3353478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
33542df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
33552df76c16SMatt Jacob 		if (error) {
3356478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
33572df76c16SMatt Jacob 			xpt_done(ccb);
33582df76c16SMatt Jacob 			break;
33592df76c16SMatt Jacob 		}
33600a70657fSMatt Jacob 		if (bootverbose) {
33612df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
33620a70657fSMatt Jacob 		}
33632df76c16SMatt Jacob 		if (IS_FC(isp)) {
33642df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
33652df76c16SMatt Jacob 		} else {
33662df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
33672df76c16SMatt Jacob 		}
3368478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3369478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3370478f8a96SJustin T. Gibbs 		break;
3371478f8a96SJustin T. Gibbs 
3372478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3373478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3374478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3375478f8a96SJustin T. Gibbs 		break;
3376478f8a96SJustin T. Gibbs 
33772df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
33782df76c16SMatt Jacob 	{
33792df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
33802df76c16SMatt Jacob 		fcparam *fcp;
33812df76c16SMatt Jacob 
33822df76c16SMatt Jacob 		if (!IS_FC(isp)) {
33832df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
33842df76c16SMatt Jacob 			xpt_done(ccb);
33852df76c16SMatt Jacob 			break;
33862df76c16SMatt Jacob 		}
33872df76c16SMatt Jacob 
33882df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
33892df76c16SMatt Jacob 
33902df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
33912df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
33922df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
33932df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
33942df76c16SMatt Jacob 		}
33952df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
33962df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
33972df76c16SMatt Jacob 			int rchange = 0;
33982df76c16SMatt Jacob 			int newrole = 0;
33992df76c16SMatt Jacob 
34002df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
34012df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
34022df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
34032df76c16SMatt Jacob 					rchange = 1;
34042df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
34052df76c16SMatt Jacob 				}
34062df76c16SMatt Jacob 				break;
34072df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
34082df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
34092df76c16SMatt Jacob 					rchange = 1;
34102df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
34112df76c16SMatt Jacob 				}
34122df76c16SMatt Jacob 				break;
34132df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
34142df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
34152df76c16SMatt Jacob 					rchange = 1;
34162df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
34172df76c16SMatt Jacob 				}
34182df76c16SMatt Jacob 				break;
34192df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
34202df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
34212df76c16SMatt Jacob 					rchange = 1;
34222df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
34232df76c16SMatt Jacob 				}
34242df76c16SMatt Jacob 				break;
34252df76c16SMatt Jacob 			}
34262df76c16SMatt Jacob 			if (rchange) {
3427e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
34286bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
34296bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
34302df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3431387d8239SMatt Jacob 					xpt_done(ccb);
3432387d8239SMatt Jacob 					break;
34332df76c16SMatt Jacob 				}
34342df76c16SMatt Jacob 			}
34352df76c16SMatt Jacob 		}
34362df76c16SMatt Jacob 		xpt_done(ccb);
34372df76c16SMatt Jacob 		break;
34382df76c16SMatt Jacob 	}
3439a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3440e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
34412df76c16SMatt Jacob 	{
34422df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
34432df76c16SMatt Jacob 
34442df76c16SMatt Jacob 		if (IS_FC(isp)) {
34452df76c16SMatt Jacob 			fcparam *fcp;
34462df76c16SMatt Jacob 
34472df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
34482df76c16SMatt Jacob 
34492df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
34502df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
34512df76c16SMatt Jacob 			switch (fcp->role) {
34522df76c16SMatt Jacob 			case ISP_ROLE_NONE:
34532df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
34542df76c16SMatt Jacob 				break;
34552df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
34562df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
34572df76c16SMatt Jacob 				break;
34582df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
34592df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
34602df76c16SMatt Jacob 				break;
34612df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
34622df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
34632df76c16SMatt Jacob 				break;
34642df76c16SMatt Jacob 			}
34652df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
34662df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
34672df76c16SMatt Jacob 		} else {
34682df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
34692df76c16SMatt Jacob 		}
34702df76c16SMatt Jacob 		xpt_done(ccb);
34712df76c16SMatt Jacob 		break;
34722df76c16SMatt Jacob 	}
3473478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
3474478f8a96SJustin T. Gibbs 	{
3475478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
3476478f8a96SJustin T. Gibbs 
3477478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
3478d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
34793e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3480a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
34813e6deb33SAlexander Motin 		else
3482d81ba9d5SMatt Jacob #endif
34833e6deb33SAlexander Motin 			cpi->target_sprt = 0;
3484478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
34850470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
34866af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
34876af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
34880470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
34897e31684eSAlexander Motin 		if (sizeof (bus_size_t) > 4)
34907bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
34917bf825d1SKenneth D. Merry 		else
34927bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
34937bf825d1SKenneth D. Merry 
34944394c92fSMatt Jacob 		if (IS_FC(isp)) {
34952df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
34962df76c16SMatt Jacob 
3497ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3498229203afSAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
34992df76c16SMatt Jacob 
35000470d791SMatt Jacob 			/*
35010470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
35020470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
35030470d791SMatt Jacob 			 */
35040470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
35050470d791SMatt Jacob 
35069deea857SKenneth D. Merry 			/*
35072df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
35089deea857SKenneth D. Merry 			 */
3509e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
3510e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
3511e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
35122df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
35132df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
35142df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
35152df76c16SMatt Jacob 			} else {
35162df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
35172df76c16SMatt Jacob 			}
35180470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
3519ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
3520805e1f82SMatt Jacob 			cpi->transport_version = 0;
35212df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
35222df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
35232df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
35242df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3525478f8a96SJustin T. Gibbs 		} else {
35262df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
35270470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3528ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
3529ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
35309deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
3531ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
3532805e1f82SMatt Jacob 			cpi->transport_version = 2;
3533478f8a96SJustin T. Gibbs 		}
3534ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
3535ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
35364195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
35374195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
35384195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3539478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
3540478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
3541478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3542478f8a96SJustin T. Gibbs 		break;
3543478f8a96SJustin T. Gibbs 	}
3544478f8a96SJustin T. Gibbs 	default:
3545478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3546478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3547478f8a96SJustin T. Gibbs 		break;
3548478f8a96SJustin T. Gibbs 	}
3549478f8a96SJustin T. Gibbs }
3550d3a9eb2eSMatt Jacob 
3551d3a9eb2eSMatt Jacob void
35522df76c16SMatt Jacob isp_done(XS_T *sccb)
3553d3a9eb2eSMatt Jacob {
35549cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
3555e95725cbSMatt Jacob 	uint32_t status;
3556d3a9eb2eSMatt Jacob 
3557d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
3558d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
3559b85389e1SMatt Jacob 
35602df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3561d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
35622df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
356392a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
356492a1e549SMatt Jacob 		} else {
3565d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3566d3a9eb2eSMatt Jacob 		}
356792a1e549SMatt Jacob 	}
3568b85389e1SMatt Jacob 
35690470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3570e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
3571ab23521aSAlexander Motin 	if (status != CAM_REQ_CMP &&
3572ab23521aSAlexander Motin 	    (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3573d3a9eb2eSMatt Jacob 		sccb->ccb_h.status |= CAM_DEV_QFRZN;
35740470d791SMatt Jacob 		xpt_freeze_devq(sccb->ccb_h.path, 1);
3575d3a9eb2eSMatt Jacob 	}
3576b85389e1SMatt Jacob 
35775cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
3578387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
35790ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
35800a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
35815cbe3e8eSAlexander Motin 	}
3582d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
3583d3a9eb2eSMatt Jacob }
3584d3a9eb2eSMatt Jacob 
35852df76c16SMatt Jacob void
35862df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3587cbf57b47SMatt Jacob {
3588e68eef14SAlexander Motin 	int bus;
3589e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3590387d8239SMatt Jacob 	char buf[64];
3591f7c631bcSMatt Jacob 	char *msg = NULL;
359257b6261fSKenneth D. Merry 	target_id_t tgt = 0;
359310365e5aSMatt Jacob 	fcportdb_t *lp;
3594a01f5aebSMatt Jacob 	struct isp_fc *fc;
359510365e5aSMatt Jacob 	struct cam_path *tmppath;
3596e68eef14SAlexander Motin 	struct ac_contract ac;
3597e68eef14SAlexander Motin 	struct ac_device_changed *adc;
35982df76c16SMatt Jacob 	va_list ap;
359910365e5aSMatt Jacob 
3600cbf57b47SMatt Jacob 	switch (cmd) {
3601cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
36020470d791SMatt Jacob 	{
3603ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
3604ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
3605cbf57b47SMatt Jacob 		int flags, tgt;
36062df76c16SMatt Jacob 		sdparam *sdp;
3607ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
3608cbf57b47SMatt Jacob 
360929f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
3610ab163f5fSMatt Jacob 
36112df76c16SMatt Jacob 		va_start(ap, cmd);
36122df76c16SMatt Jacob 		bus = va_arg(ap, int);
36132df76c16SMatt Jacob 		tgt = va_arg(ap, int);
36142df76c16SMatt Jacob 		va_end(ap);
36152df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
36162df76c16SMatt Jacob 
36172df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
36182df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3619cbf57b47SMatt Jacob 			break;
3620cbf57b47SMatt Jacob 		}
36219ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
3622ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
3623ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
3624ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
3625ab163f5fSMatt Jacob 
3626ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
3627ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
3628ab163f5fSMatt Jacob 
3629ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
3630ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
3631ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3632ab163f5fSMatt Jacob 		}
3633ab163f5fSMatt Jacob 
3634cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
3635ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
3636ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3637ab163f5fSMatt Jacob 		}
3638ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3639ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
3640ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3641ab163f5fSMatt Jacob 		} else {
3642ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3643ab163f5fSMatt Jacob 		}
3644ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
3645ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3646ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
36479ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
36489ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3649ab163f5fSMatt Jacob 		}
36502df76c16SMatt 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);
3651ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3652ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3653cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
3654cbf57b47SMatt Jacob 		break;
36550470d791SMatt Jacob 	}
365657c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
36572df76c16SMatt Jacob 	{
36582df76c16SMatt Jacob 		va_start(ap, cmd);
36592df76c16SMatt Jacob 		bus = va_arg(ap, int);
36602df76c16SMatt Jacob 		va_end(ap);
36612df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
36622df76c16SMatt Jacob 		if (IS_FC(isp)) {
36632df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
36642df76c16SMatt Jacob 		} else {
36652df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
366657c801f5SMatt Jacob 		}
366757c801f5SMatt Jacob 		break;
36682df76c16SMatt Jacob 	}
366957b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_RESET:
367057b6261fSKenneth D. Merry 	{
367157b6261fSKenneth D. Merry 		uint16_t lipp;
367257b6261fSKenneth D. Merry 		fcparam *fcp;
367357b6261fSKenneth D. Merry 		va_start(ap, cmd);
367457b6261fSKenneth D. Merry 		bus = va_arg(ap, int);
367557b6261fSKenneth D. Merry 		va_end(ap);
367657b6261fSKenneth D. Merry 
367757b6261fSKenneth D. Merry 		lipp = ISP_READ(isp, OUTMAILBOX1);
367857b6261fSKenneth D. Merry 		fcp = FCPARAM(isp, bus);
367957b6261fSKenneth D. Merry 
368057b6261fSKenneth D. Merry 		isp_prt(isp, ISP_LOGINFO, "Chan %d LOOP Reset, LIP primitive %x", bus, lipp);
368157b6261fSKenneth D. Merry 		/*
368257b6261fSKenneth D. Merry 		 * Per FCP-4, a Reset LIP should result in a CRN reset. Other
368357b6261fSKenneth D. Merry 		 * LIPs and loop up/down events should never reset the CRN. For
368457b6261fSKenneth D. Merry 		 * an as of yet unknown reason, 24xx series cards (and
368557b6261fSKenneth D. Merry 		 * potentially others) can interrupt with a LIP Reset status
368657b6261fSKenneth D. Merry 		 * when no LIP reset came down the wire. Additionally, the LIP
368757b6261fSKenneth D. Merry 		 * primitive accompanying this status would not be a valid LIP
368857b6261fSKenneth D. Merry 		 * Reset primitive, but some variation of an invalid AL_PA
368957b6261fSKenneth D. Merry 		 * LIP. As a result, we have to verify the AL_PD in the LIP
369057b6261fSKenneth D. Merry 		 * addresses our port before blindly resetting.
369157b6261fSKenneth D. Merry 		*/
369257b6261fSKenneth D. Merry 		if (FCP_IS_DEST_ALPD(fcp, (lipp & 0x00FF)))
369357b6261fSKenneth D. Merry 			isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
369457b6261fSKenneth D. Merry 		isp_loop_changed(isp, bus);
369557b6261fSKenneth D. Merry 		break;
369657b6261fSKenneth D. Merry 	}
36975d571944SMatt Jacob 	case ISPASYNC_LIP:
369837a7daacSAlexander Motin 		if (msg == NULL)
3699f7c631bcSMatt Jacob 			msg = "LIP Received";
3700f7c631bcSMatt Jacob 		/* FALLTHROUGH */
370157c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
370237a7daacSAlexander Motin 		if (msg == NULL)
3703f7c631bcSMatt Jacob 			msg = "LOOP Down";
370457b6261fSKenneth D. Merry 		/* FALLTHROUGH */
370557b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_UP:
370657b6261fSKenneth D. Merry 		if (msg == NULL)
370757b6261fSKenneth D. Merry 			msg = "LOOP Up";
37082df76c16SMatt Jacob 		va_start(ap, cmd);
37092df76c16SMatt Jacob 		bus = va_arg(ap, int);
37102df76c16SMatt Jacob 		va_end(ap);
3711e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
3712e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
371357c801f5SMatt Jacob 		break;
371410365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
37152df76c16SMatt Jacob 		va_start(ap, cmd);
37162df76c16SMatt Jacob 		bus = va_arg(ap, int);
37172df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
37182df76c16SMatt Jacob 		va_end(ap);
3719a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3720e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3721387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3722e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
3723e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3724e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
3725e68eef14SAlexander Motin 			lp->is_target = 1;
372621daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
37275704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
3728e68eef14SAlexander Motin 		}
3729e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3730e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
3731e68eef14SAlexander Motin 			lp->is_initiator = 1;
3732e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3733e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3734e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3735e68eef14SAlexander Motin 			adc->port = lp->portid;
3736d4f3ad3aSAlexander Motin 			adc->target = tgt;
3737e68eef14SAlexander Motin 			adc->arrived = 1;
3738e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
373910365e5aSMatt Jacob 		}
374010365e5aSMatt Jacob 		break;
374110365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
374257b6261fSKenneth D. Merry 	case ISPASYNC_DEV_STAYED:
37432df76c16SMatt Jacob 		va_start(ap, cmd);
37442df76c16SMatt Jacob 		bus = va_arg(ap, int);
37452df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
37462df76c16SMatt Jacob 		va_end(ap);
3747a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3748e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3749e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
375057b6261fSKenneth D. Merry 		if (cmd == ISPASYNC_DEV_CHANGED)
3751e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
375257b6261fSKenneth D. Merry 		else
375357b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
375457b6261fSKenneth D. Merry 
3755e68eef14SAlexander Motin 		if (lp->is_target !=
3756e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3757e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
3758e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
3759e68eef14SAlexander Motin 			if (lp->is_target) {
376057b6261fSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED)
376121daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3762e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
376310365e5aSMatt Jacob 			} else {
3764e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
376557b6261fSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED)
376621daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
376710365e5aSMatt Jacob 			}
3768f7c631bcSMatt Jacob 		}
3769e68eef14SAlexander Motin 		if (lp->is_initiator !=
3770e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3771e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
3772e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
3773e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3774e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3775e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3776e68eef14SAlexander Motin 			adc->port = lp->portid;
3777d4f3ad3aSAlexander Motin 			adc->target = tgt;
3778e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
3779e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3780e68eef14SAlexander Motin 		}
378110365e5aSMatt Jacob 		break;
378210365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
37832df76c16SMatt Jacob 		va_start(ap, cmd);
37842df76c16SMatt Jacob 		bus = va_arg(ap, int);
37852df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
37862df76c16SMatt Jacob 		va_end(ap);
3787a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
3788e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
3789f7c631bcSMatt Jacob 		/*
3790e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
3791e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
3792f7c631bcSMatt Jacob 		 */
3793387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3794e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
3795f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
3796e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
3797e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
3798a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
3799387d8239SMatt 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);
3800a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
3801f7c631bcSMatt Jacob 			}
3802e68eef14SAlexander Motin 			break;
38034b9d588eSMatt Jacob 		}
3804e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
380510365e5aSMatt Jacob 		break;
380610365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
380710365e5aSMatt Jacob 	{
380810365e5aSMatt Jacob 		char *msg;
3809eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
38102df76c16SMatt Jacob 
38112df76c16SMatt Jacob 		va_start(ap, cmd);
38122df76c16SMatt Jacob 		bus = va_arg(ap, int);
38132df76c16SMatt Jacob 		evt = va_arg(ap, int);
3814eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
38152df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
38162df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
38172df76c16SMatt Jacob 			reason = va_arg(ap, int);
3818eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3819eea52482SAlexander Motin 			portid = va_arg(ap, int);
382010365e5aSMatt Jacob 		} else {
38212df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
38222df76c16SMatt Jacob 			nlstate = reason = 0;
382310365e5aSMatt Jacob 		}
38242df76c16SMatt Jacob 		va_end(ap);
38252df76c16SMatt Jacob 
38262df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
382757b6261fSKenneth D. Merry 			int tgt_set = 0;
3828b5d5037bSAlexander Motin 			msg = "Port Database Changed";
3829eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
3830eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3831eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
383257b6261fSKenneth D. Merry 			/*
383357b6261fSKenneth D. Merry 			 * Port database syncs are not sufficient for
383457b6261fSKenneth D. Merry 			 * determining that logins or logouts are done on the
383557b6261fSKenneth D. Merry 			 * loop, but this information is directly available from
383657b6261fSKenneth D. Merry 			 * the reason code from the incoming mbox. We must reset
383757b6261fSKenneth D. Merry 			 * the fcp crn on these events according to FCP-4
383857b6261fSKenneth D. Merry 			 */
383957b6261fSKenneth D. Merry 			switch (reason) {
384057b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_1:
384157b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_2:
384257b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_3:
384357b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_RCVD:
384457b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_RCVD:
384557b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLO_RCVD:
384657b6261fSKenneth D. Merry 			case PDB24XX_AE_LOGO_RCVD:
384757b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_DONE:
384857b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_DONE:
384957b6261fSKenneth D. Merry 				/*
385057b6261fSKenneth D. Merry 				 * If the event is not global, twiddle tgt and
385157b6261fSKenneth D. Merry 				 * tgt_set to nominate only the target
385257b6261fSKenneth D. Merry 				 * associated with the nphdl.
385357b6261fSKenneth D. Merry 				 */
385457b6261fSKenneth D. Merry 				if (nphdl != PDB24XX_AE_GLOBAL) {
385557b6261fSKenneth D. Merry 					/* Break if we don't yet have the pdb */
385657b6261fSKenneth D. Merry 					if (!isp_find_pdb_by_handle(isp, bus, nphdl, &lp))
385757b6261fSKenneth D. Merry 						break;
385857b6261fSKenneth D. Merry 					tgt = FC_PORTDB_TGT(isp, bus, lp);
385957b6261fSKenneth D. Merry 					tgt_set = 1;
386057b6261fSKenneth D. Merry 				}
386157b6261fSKenneth D. Merry 				isp_fcp_reset_crn(isp, bus, tgt, tgt_set);
386257b6261fSKenneth D. Merry 				break;
386357b6261fSKenneth D. Merry 			default:
386457b6261fSKenneth D. Merry 				break; /* NOP */
386557b6261fSKenneth D. Merry 			}
38662df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3867b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
3868eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3869eea52482SAlexander Motin 			    bus, msg, portid);
38702df76c16SMatt Jacob 		} else {
3871b5d5037bSAlexander Motin 			msg = "Other Change Notify";
3872eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
38732df76c16SMatt Jacob 		}
3874e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
387557c801f5SMatt Jacob 		break;
387602ab3379SMatt Jacob 	}
3877d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3878e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
3879d81ba9d5SMatt Jacob 	{
38802df76c16SMatt Jacob 		isp_notify_t *notify;
38812df76c16SMatt Jacob 		va_start(ap, cmd);
38822df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
38832df76c16SMatt Jacob 		va_end(ap);
38842df76c16SMatt Jacob 		switch (notify->nt_ncode) {
38852df76c16SMatt Jacob 		case NT_ABORT_TASK:
38862df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
38872df76c16SMatt Jacob 		case NT_CLEAR_ACA:
38882df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
38892df76c16SMatt Jacob 		case NT_LUN_RESET:
38902df76c16SMatt Jacob 		case NT_TARGET_RESET:
3891c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
3892c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
38932df76c16SMatt Jacob 			/*
38942df76c16SMatt Jacob 			 * These are task management functions.
38952df76c16SMatt Jacob 			 */
38962df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
38972df76c16SMatt Jacob 			break;
38982df76c16SMatt Jacob 		case NT_BUS_RESET:
38992df76c16SMatt Jacob 		case NT_LIP_RESET:
39002df76c16SMatt Jacob 		case NT_LINK_UP:
39012df76c16SMatt Jacob 		case NT_LINK_DOWN:
3902e68eef14SAlexander Motin 		case NT_HBA_RESET:
39032df76c16SMatt Jacob 			/*
39042df76c16SMatt Jacob 			 * No action need be taken here.
39052df76c16SMatt Jacob 			 */
39062df76c16SMatt Jacob 			break;
3907387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
39082df76c16SMatt Jacob 		case NT_LOGOUT:
39092df76c16SMatt Jacob 			/*
39102df76c16SMatt Jacob 			 * This is device arrival/departure notification
39112df76c16SMatt Jacob 			 */
391296b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
39132df76c16SMatt Jacob 			break;
39145a5632c2SAlexander Motin 		case NT_SRR:
39155a5632c2SAlexander Motin 			isp_handle_platform_srr(isp, notify);
39165a5632c2SAlexander Motin 			break;
39172df76c16SMatt Jacob 		default:
39182df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
391996b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
39202df76c16SMatt Jacob 			break;
39212df76c16SMatt Jacob 		}
3922d81ba9d5SMatt Jacob 		break;
3923d81ba9d5SMatt Jacob 	}
3924387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
3925387d8239SMatt Jacob 	{
3926387d8239SMatt Jacob 		void *inot;
3927387d8239SMatt Jacob 		va_start(ap, cmd);
3928387d8239SMatt Jacob 		inot = va_arg(ap, void *);
3929387d8239SMatt Jacob 		va_end(ap);
3930387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
3931387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3932387d8239SMatt Jacob 			if (tp) {
3933387d8239SMatt Jacob 				tp->isp = isp;
3934387d8239SMatt Jacob 				memcpy(tp->data, inot, sizeof (tp->data));
3935387d8239SMatt Jacob 				tp->not = tp->data;
39362a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
39372a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
39382a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
3939387d8239SMatt Jacob 			} else {
3940387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3941387d8239SMatt Jacob 			}
3942387d8239SMatt Jacob 		}
3943387d8239SMatt Jacob 		break;
3944387d8239SMatt Jacob 	}
3945d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
39462df76c16SMatt Jacob 	{
39472df76c16SMatt Jacob 		isphdr_t *hp;
39482df76c16SMatt Jacob 
39492df76c16SMatt Jacob 		va_start(ap, cmd);
39502df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
39512df76c16SMatt Jacob 		va_end(ap);
39522df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
3953d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
39542df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
3955d81ba9d5SMatt Jacob 			break;
3956d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
39572df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
3958d81ba9d5SMatt Jacob 			break;
39592df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
3960d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
3961d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
3962d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
39632df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
3964d81ba9d5SMatt Jacob 			break;
39655a5632c2SAlexander Motin 		default:
39665a5632c2SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
39675a5632c2SAlexander Motin 			    __func__, hp->rqs_entry_type);
39685a5632c2SAlexander Motin 			break;
3969d81ba9d5SMatt Jacob 		}
3970d81ba9d5SMatt Jacob 		break;
39712df76c16SMatt Jacob 	}
3972d81ba9d5SMatt Jacob #endif
3973ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
3974ab163f5fSMatt Jacob 	{
39751dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
3976ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
3977ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
3978ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
3979ab163f5fSMatt Jacob 		} else {
3980ab163f5fSMatt Jacob 			mbox6 = 0;
3981ab163f5fSMatt Jacob 		}
39822df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
39839c2e9bcfSAlexander Motin #if 0
39840a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
39850a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
39862df76c16SMatt Jacob 		isp_reinit(isp, 1);
39870a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
39880a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
39899c2e9bcfSAlexander Motin #endif
3990ab163f5fSMatt Jacob 		break;
3991ab163f5fSMatt Jacob 	}
3992d81ba9d5SMatt Jacob 	default:
3993b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3994cbf57b47SMatt Jacob 		break;
3995cbf57b47SMatt Jacob 	}
3996cbf57b47SMatt Jacob }
3997cbf57b47SMatt Jacob 
39982df76c16SMatt Jacob uint64_t
39992df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
40002df76c16SMatt Jacob {
40012df76c16SMatt Jacob 	uint64_t seed;
40022df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
40032df76c16SMatt Jacob 
4004a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4005a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4006a193dc4bSAlexander Motin 	if (seed)
4007a193dc4bSAlexander Motin 		return (seed);
4008a193dc4bSAlexander Motin 
4009a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
40102df76c16SMatt Jacob 	if (isactive) {
4011a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4012a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4013a193dc4bSAlexander Motin 		if (seed)
40142df76c16SMatt Jacob 			return (seed);
40152df76c16SMatt Jacob 	}
4016a193dc4bSAlexander Motin 
4017a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4018a193dc4bSAlexander Motin 	if (chan > 0) {
4019a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4020a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4021a193dc4bSAlexander Motin 		if (seed == 0) {
4022a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4023a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
40245cc3786cSMatt Jacob 		}
40252df76c16SMatt Jacob 	}
40262df76c16SMatt Jacob 
4027a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4028a193dc4bSAlexander Motin 	if (seed == 0) {
4029a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4030a193dc4bSAlexander Motin 		if (!iswwnn)
4031a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4032a193dc4bSAlexander Motin 	}
40332df76c16SMatt Jacob 
4034a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4035a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
40362df76c16SMatt Jacob 		/*
40372df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
40382df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4039a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
40402df76c16SMatt Jacob 		 */
4041a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4042a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4043a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
40442df76c16SMatt Jacob 	}
40452df76c16SMatt Jacob 	return (seed);
40462df76c16SMatt Jacob }
40472df76c16SMatt Jacob 
4048b09b0095SMatt Jacob void
40499cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4050b09b0095SMatt Jacob {
4051de461933SMatt Jacob 	int loc;
4052387d8239SMatt Jacob 	char lbuf[200];
4053b09b0095SMatt Jacob 	va_list ap;
4054de461933SMatt Jacob 
4055b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4056b09b0095SMatt Jacob 		return;
4057b09b0095SMatt Jacob 	}
4058387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4059de461933SMatt Jacob 	loc = strlen(lbuf);
4060b09b0095SMatt Jacob 	va_start(ap, fmt);
4061de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4062b09b0095SMatt Jacob 	va_end(ap);
4063de461933SMatt Jacob 	printf("%s\n", lbuf);
4064b09b0095SMatt Jacob }
4065f7c631bcSMatt Jacob 
4066670508b1SMatt Jacob void
4067670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4068670508b1SMatt Jacob {
4069670508b1SMatt Jacob 	va_list ap;
4070670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4071670508b1SMatt Jacob 		return;
4072670508b1SMatt Jacob 	}
4073670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4074670508b1SMatt Jacob 	va_start(ap, fmt);
4075670508b1SMatt Jacob 	vprintf(fmt, ap);
4076670508b1SMatt Jacob 	va_end(ap);
4077670508b1SMatt Jacob 	printf("\n");
4078670508b1SMatt Jacob }
4079670508b1SMatt Jacob 
4080f7c631bcSMatt Jacob uint64_t
4081f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4082f7c631bcSMatt Jacob {
4083f7c631bcSMatt Jacob 	uint64_t elapsed;
4084f7c631bcSMatt Jacob 	struct timespec x = *b;
4085f7c631bcSMatt Jacob 	timespecsub(&x, a);
4086f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4087f7c631bcSMatt Jacob 	if (elapsed == 0)
4088f7c631bcSMatt Jacob 		elapsed++;
4089f7c631bcSMatt Jacob 	return (elapsed);
4090f7c631bcSMatt Jacob }
4091f7c631bcSMatt Jacob 
4092f7c631bcSMatt Jacob int
4093f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4094f7c631bcSMatt Jacob {
4095f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4096f7c631bcSMatt Jacob 		return (1);
4097f7c631bcSMatt Jacob 	} else {
4098f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4099f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4100f7c631bcSMatt Jacob 		return (0);
4101f7c631bcSMatt Jacob 	}
4102f7c631bcSMatt Jacob }
4103f7c631bcSMatt Jacob 
4104f7c631bcSMatt Jacob void
4105f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4106f7c631bcSMatt Jacob {
41076327b0d2SAlexander Motin 	u_int t, to;
4108f7c631bcSMatt Jacob 
41096327b0d2SAlexander Motin 	to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4110f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4111f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4112f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
411313d9c921SAlexander Motin 		msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_lock,
41146327b0d2SAlexander Motin 		    PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4115f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4116f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4117f7c631bcSMatt Jacob 	} else {
41186327b0d2SAlexander Motin 		for (t = 0; t < to; t += 100) {
41196327b0d2SAlexander Motin 			if (isp->isp_osinfo.mboxcmd_done)
4120f7c631bcSMatt Jacob 				break;
41210e6bc811SAlexander Motin 			ISP_RUN_ISR(isp);
41226327b0d2SAlexander Motin 			if (isp->isp_osinfo.mboxcmd_done)
4123f7c631bcSMatt Jacob 				break;
41242df76c16SMatt Jacob 			ISP_DELAY(100);
4125f7c631bcSMatt Jacob 		}
4126f7c631bcSMatt Jacob 	}
4127f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
41286327b0d2SAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
41296327b0d2SAlexander Motin 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
41306327b0d2SAlexander Motin 		    isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4131f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4132f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4133f7c631bcSMatt Jacob 	}
4134f7c631bcSMatt Jacob }
4135f7c631bcSMatt Jacob 
4136f7c631bcSMatt Jacob void
4137f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4138f7c631bcSMatt Jacob {
4139f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
41406327b0d2SAlexander Motin 	if (isp->isp_osinfo.mbox_sleeping)
41416327b0d2SAlexander Motin 		wakeup(&isp->isp_osinfo.mboxcmd_done);
4142f7c631bcSMatt Jacob }
4143f7c631bcSMatt Jacob 
4144f7c631bcSMatt Jacob void
4145f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4146f7c631bcSMatt Jacob {
4147f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4148f7c631bcSMatt Jacob }
4149f7c631bcSMatt Jacob 
4150f7c631bcSMatt Jacob int
41512df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
41522df76c16SMatt Jacob {
41532df76c16SMatt Jacob 	int ret = 0;
41542df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
41552df76c16SMatt Jacob 		ret = -1;
41562df76c16SMatt Jacob 	} else {
41572df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
41582df76c16SMatt Jacob 	}
41592df76c16SMatt Jacob 	return (ret);
41602df76c16SMatt Jacob }
41612df76c16SMatt Jacob 
41620a70657fSMatt Jacob void
41630a70657fSMatt Jacob isp_platform_intr(void *arg)
41640a70657fSMatt Jacob {
41650a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
41660a70657fSMatt Jacob 
41670a70657fSMatt Jacob 	ISP_LOCK(isp);
41680e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
41690a70657fSMatt Jacob 	ISP_UNLOCK(isp);
41700a70657fSMatt Jacob }
41710a70657fSMatt Jacob 
41720a70657fSMatt Jacob void
417308826086SAlexander Motin isp_platform_intr_resp(void *arg)
417408826086SAlexander Motin {
417508826086SAlexander Motin 	ispsoftc_t *isp = arg;
417608826086SAlexander Motin 
417708826086SAlexander Motin 	ISP_LOCK(isp);
417808826086SAlexander Motin 	isp_intr_respq(isp);
417908826086SAlexander Motin 	ISP_UNLOCK(isp);
418008826086SAlexander Motin 
418108826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
418208826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
418308826086SAlexander Motin }
418408826086SAlexander Motin 
418508826086SAlexander Motin void
418608826086SAlexander Motin isp_platform_intr_atio(void *arg)
418708826086SAlexander Motin {
418808826086SAlexander Motin 	ispsoftc_t *isp = arg;
418908826086SAlexander Motin 
419008826086SAlexander Motin 	ISP_LOCK(isp);
419108826086SAlexander Motin #ifdef	ISP_TARGET_MODE
419208826086SAlexander Motin 	isp_intr_atioq(isp);
419308826086SAlexander Motin #endif
419408826086SAlexander Motin 	ISP_UNLOCK(isp);
419508826086SAlexander Motin 
419608826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
419708826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
419808826086SAlexander Motin }
419908826086SAlexander Motin 
420008826086SAlexander Motin void
42010a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
42020a70657fSMatt Jacob {
42030a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
42042df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
42050a70657fSMatt Jacob 	} else {
42062df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
42070a70657fSMatt Jacob 	}
42080a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
42090a70657fSMatt Jacob }
42102df76c16SMatt Jacob 
4211fb4a4356SKenneth D. Merry /*
4212fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4213fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4214fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4215fb4a4356SKenneth D. Merry  */
4216fb4a4356SKenneth D. Merry void
421721daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4218fb4a4356SKenneth D. Merry {
421921daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4220fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
422121daf914SAlexander Motin 	int i;
4222fb4a4356SKenneth D. Merry 
4223fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
422421daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
422521daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4226fb4a4356SKenneth D. Merry 	else
422721daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
422821daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4229fb4a4356SKenneth D. Merry 
4230fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
423121daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
423221daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4233fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4234fb4a4356SKenneth D. Merry 		}
4235fb4a4356SKenneth D. Merry 	}
4236fb4a4356SKenneth D. Merry }
4237fb4a4356SKenneth D. Merry 
4238387d8239SMatt Jacob int
4239387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4240387d8239SMatt Jacob {
42416af11b82SAlexander Motin 	lun_id_t lun;
42426af11b82SAlexander Motin 	uint32_t chan, tgt;
4243405b7a29SMatt Jacob 	struct isp_fc *fc;
4244405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4245405b7a29SMatt Jacob 	int idx;
4246405b7a29SMatt Jacob 
4247a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4248405b7a29SMatt Jacob 		return (0);
4249405b7a29SMatt Jacob 
4250405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4251405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4252405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4253405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4254405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4255405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4256387d8239SMatt Jacob 
4257387d8239SMatt Jacob 	while (nxp) {
4258387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4259387d8239SMatt Jacob 			break;
4260387d8239SMatt Jacob 		nxp = nxp->next;
4261387d8239SMatt Jacob 	}
4262387d8239SMatt Jacob 	if (nxp == NULL) {
4263387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4264387d8239SMatt Jacob 		if (nxp == NULL) {
4265387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4266387d8239SMatt Jacob 			if (nxp == NULL) {
4267387d8239SMatt Jacob 				return (-1);
4268387d8239SMatt Jacob 			}
4269387d8239SMatt Jacob 		} else {
4270387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4271387d8239SMatt Jacob 		}
4272387d8239SMatt Jacob 		nxp->tgt = tgt;
4273387d8239SMatt Jacob 		nxp->lun = lun;
4274387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4275387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4276387d8239SMatt Jacob 	}
4277387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4278387d8239SMatt Jacob 		nxp->crnseed = 1;
4279387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4280387d8239SMatt Jacob 	return (0);
4281387d8239SMatt Jacob }
4282387d8239SMatt Jacob 
428394dff771SMatt Jacob /*
428494dff771SMatt Jacob  * We enter with the lock held
428594dff771SMatt Jacob  */
42862df76c16SMatt Jacob void
42872df76c16SMatt Jacob isp_timer(void *arg)
42882df76c16SMatt Jacob {
42892df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
42902df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
42912df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
42922df76c16SMatt Jacob #endif
429394dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
42942df76c16SMatt Jacob }
4295387d8239SMatt Jacob 
4296387d8239SMatt Jacob isp_ecmd_t *
4297387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4298387d8239SMatt Jacob {
4299387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4300387d8239SMatt Jacob 	if (ecmd) {
4301387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4302387d8239SMatt Jacob 	}
4303387d8239SMatt Jacob 	return (ecmd);
4304387d8239SMatt Jacob }
4305387d8239SMatt Jacob 
4306387d8239SMatt Jacob void
4307387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4308387d8239SMatt Jacob {
4309387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4310387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4311387d8239SMatt Jacob }
4312