xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 6327b0d287ef60958c80f92f5c69e46a6d29e0a1)
1098ca2bdSWarner Losh /*-
22df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
3e5265237SMatt Jacob  * All rights reserved.
46054c3f6SMatt Jacob  *
56054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
66054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
76054c3f6SMatt Jacob  * are met:
86054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
96054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
106054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
11aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
12aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
136054c3f6SMatt Jacob  *
146054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
186054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246054c3f6SMatt Jacob  * SUCH DAMAGE.
256054c3f6SMatt Jacob  */
26aad970f1SDavid E. O'Brien 
27799881e0SMatt Jacob /*
28799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29799881e0SMatt Jacob  */
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
325b14cb41SWill Andrews 
336054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
345d571944SMatt Jacob #include <sys/unistd.h>
355d571944SMatt Jacob #include <sys/kthread.h>
365d571944SMatt Jacob #include <sys/conf.h>
374eb49427SMatt Jacob #include <sys/module.h>
385d571944SMatt Jacob #include <sys/ioccom.h>
395d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4070273f90SMatt Jacob #include <sys/devicestat.h>
41f7c631bcSMatt Jacob #include <cam/cam_periph.h>
426c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
436054c3f6SMatt Jacob 
444eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4556c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4673030e03SMatt Jacob int isp_announced = 0;
472df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
482df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
49f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
50e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5173030e03SMatt Jacob 
52e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
53e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
545d571944SMatt Jacob static d_ioctl_t ispioctl;
551dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
560470d791SMatt Jacob static void isp_poll(struct cam_sim *);
57b85389e1SMatt Jacob static timeout_t isp_watchdog;
58de461933SMatt Jacob static timeout_t isp_gdt;
59de461933SMatt Jacob static task_fn_t isp_gdt_task;
605d571944SMatt Jacob static void isp_kthread(void *);
61d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6294dff771SMatt Jacob static int isp_timer_count;
632df76c16SMatt Jacob static void isp_timer(void *);
64cc8df88bSMatt Jacob 
655d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
66dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
677ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
687ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
695d571944SMatt Jacob };
705d571944SMatt Jacob 
712df76c16SMatt Jacob static int
7207f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7307f56f1cSAlexander Motin {
7407f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7507f56f1cSAlexander Motin 	int chan = arg2;
7607f56f1cSAlexander Motin 	int error, old, value;
7707f56f1cSAlexander Motin 
7807f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
7907f56f1cSAlexander Motin 
8007f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8107f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8207f56f1cSAlexander Motin 		return (error);
8307f56f1cSAlexander Motin 
8407f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8507f56f1cSAlexander Motin 		return (EINVAL);
8607f56f1cSAlexander Motin 
8707f56f1cSAlexander Motin 	ISP_LOCK(isp);
8807f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
8907f56f1cSAlexander Motin 
90391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
91391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
92391f03daSAlexander Motin 
9307f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9407f56f1cSAlexander Motin 	if (value == old) {
9507f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9607f56f1cSAlexander Motin 		return (0);
9707f56f1cSAlexander Motin 	}
9807f56f1cSAlexander Motin 
9907f56f1cSAlexander Motin 	/* Actually change the role. */
1006bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10107f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10207f56f1cSAlexander Motin 	return (error);
10307f56f1cSAlexander Motin }
10407f56f1cSAlexander Motin 
10507f56f1cSAlexander Motin static int
1062df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
107478f8a96SJustin T. Gibbs {
108478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
109ea6f23cdSMatt Jacob 	struct cam_sim *sim;
110ea6f23cdSMatt Jacob 	struct cam_path *path;
1118290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
1128290ea90SAlexander Motin 	int i;
1138290ea90SAlexander Motin #endif
114478f8a96SJustin T. Gibbs 
115478f8a96SJustin T. Gibbs 	/*
1162df76c16SMatt Jacob 	 * Construct our SIM entry.
117ea6f23cdSMatt Jacob 	 */
1182df76c16SMatt Jacob 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
119ea6f23cdSMatt Jacob 
1202df76c16SMatt Jacob 	if (sim == NULL) {
1212df76c16SMatt Jacob 		return (ENOMEM);
1222df76c16SMatt Jacob 	}
1232df76c16SMatt Jacob 
1242df76c16SMatt Jacob 	ISP_LOCK(isp);
1252df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1262df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1272df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1282df76c16SMatt Jacob 		return (EIO);
1292df76c16SMatt Jacob 	}
1302df76c16SMatt Jacob 	ISP_UNLOCK(isp);
131227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1322df76c16SMatt Jacob 		ISP_LOCK(isp);
1332df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1342df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1352df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1362df76c16SMatt Jacob 		return (ENXIO);
1372df76c16SMatt Jacob 	}
1382df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1392df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1402df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1412df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1422df76c16SMatt Jacob 	csa.callback_arg = sim;
143387d8239SMatt Jacob 
144387d8239SMatt Jacob 	ISP_LOCK(isp);
1452df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
146387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1472df76c16SMatt Jacob 
1482df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1492df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1502df76c16SMatt Jacob 		spi->sim = sim;
1512df76c16SMatt Jacob 		spi->path = path;
1520d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
15309ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1548290ea90SAlexander Motin 		STAILQ_INIT(&spi->ntfree);
1558290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1568290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
1578290ea90SAlexander Motin 		LIST_INIT(&spi->atfree);
1588290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1598290ea90SAlexander Motin 			LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
1608290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1618290ea90SAlexander Motin 			LIST_INIT(&spi->atused[i]);
1620d965629SAlexander Motin #endif
1632df76c16SMatt Jacob 	} else {
164a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1652df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
16607f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
16707f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
16807f56f1cSAlexander Motin 		char name[16];
1692df76c16SMatt Jacob 
170a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1712df76c16SMatt Jacob 		fc->sim = sim;
1722df76c16SMatt Jacob 		fc->path = path;
1732df76c16SMatt Jacob 		fc->isp = isp;
174a01f5aebSMatt Jacob 		fc->ready = 1;
175de461933SMatt Jacob 
1762df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.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");
2412df76c16SMatt Jacob 	}
2422df76c16SMatt Jacob 	return (0);
2432df76c16SMatt Jacob }
2442df76c16SMatt Jacob 
2451d0a1de2SWill Andrews static void
2461d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2471d0a1de2SWill Andrews {
2481d0a1de2SWill Andrews 	struct cam_sim *sim;
2491d0a1de2SWill Andrews 	struct cam_path *path;
2501d0a1de2SWill Andrews 	struct ccb_setasync csa;
2511d0a1de2SWill Andrews 	int *num_threads;
2521d0a1de2SWill Andrews 
2531d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2541d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2551d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2561d0a1de2SWill Andrews 
2571d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2581d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2591d0a1de2SWill Andrews 	csa.event_enable = 0;
2601d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2611d0a1de2SWill Andrews 	csa.callback_arg = sim;
2621d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2631d0a1de2SWill Andrews 	xpt_free_path(path);
2641d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2651d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2661d0a1de2SWill Andrews 
2671d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2681d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2691d0a1de2SWill Andrews 	while (*num_threads != 0)
2701d0a1de2SWill Andrews 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
2711d0a1de2SWill Andrews }
2721d0a1de2SWill Andrews 
2732df76c16SMatt Jacob int
2742df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2752df76c16SMatt Jacob {
2762df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2772df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2782df76c16SMatt Jacob 	int chan;
2792df76c16SMatt Jacob 
280ea6f23cdSMatt Jacob 	/*
281ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
282478f8a96SJustin T. Gibbs 	 */
2832df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2842df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2852df76c16SMatt Jacob 		return (EIO);
286478f8a96SJustin T. Gibbs 	}
287478f8a96SJustin T. Gibbs 
2882df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2892df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2902df76c16SMatt Jacob 			goto unwind;
291ea6f23cdSMatt Jacob 		}
292ea6f23cdSMatt Jacob 	}
293ea6f23cdSMatt Jacob 
2942df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
29594dff771SMatt Jacob 	isp_timer_count = hz >> 2;
29694dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
2972df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
298ea6f23cdSMatt Jacob 
2992df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
3002df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3012df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
302ea6f23cdSMatt Jacob 	}
3032df76c16SMatt Jacob 	return (0);
3045d571944SMatt Jacob 
3052df76c16SMatt Jacob unwind:
3062df76c16SMatt Jacob 	while (--chan >= 0) {
3072df76c16SMatt Jacob 		struct cam_sim *sim;
3082df76c16SMatt Jacob 		struct cam_path *path;
3091d0a1de2SWill Andrews 
3101d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3111d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3122df76c16SMatt Jacob 		xpt_free_path(path);
3130a70657fSMatt Jacob 		ISP_LOCK(isp);
3140a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3152df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3162df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3172df76c16SMatt Jacob 	}
3182df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3192df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3202df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3212df76c16SMatt Jacob 	}
3222df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3232df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3242df76c16SMatt Jacob 	return (-1);
3252df76c16SMatt Jacob }
3262df76c16SMatt Jacob 
327e95725cbSMatt Jacob int
3282df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3292df76c16SMatt Jacob {
330e95725cbSMatt Jacob 	struct cam_sim *sim;
3312df76c16SMatt Jacob 	int chan;
3322df76c16SMatt Jacob 
3330a70657fSMatt Jacob 	ISP_LOCK(isp);
3342df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
3351d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
336e95725cbSMatt Jacob 		if (sim->refcount > 2) {
3372df76c16SMatt Jacob 			ISP_UNLOCK(isp);
338e95725cbSMatt Jacob 			return (EBUSY);
339e95725cbSMatt Jacob 		}
340e95725cbSMatt Jacob 	}
3411d0a1de2SWill Andrews 	/* Tell spawned threads that we're exiting. */
3421d0a1de2SWill Andrews 	isp->isp_osinfo.is_exiting = 1;
343e95725cbSMatt Jacob 	if (isp->isp_osinfo.timer_active) {
344e95725cbSMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
345e95725cbSMatt Jacob 		isp->isp_osinfo.timer_active = 0;
346e95725cbSMatt Jacob 	}
3471d0a1de2SWill Andrews 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
3481d0a1de2SWill Andrews 		isp_detach_chan(isp, chan);
349387d8239SMatt Jacob 	ISP_UNLOCK(isp);
3501d0a1de2SWill Andrews 
3512df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3522df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3532df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3542df76c16SMatt Jacob 	}
355a035b0afSMatt Jacob 	if (isp->isp_osinfo.devq != NULL) {
3562df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
3572df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
3580a70657fSMatt Jacob 	}
359e95725cbSMatt Jacob 	return (0);
3605d571944SMatt Jacob }
3615d571944SMatt Jacob 
362f7c631bcSMatt Jacob static void
363e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
364fdeb9f2fSMatt Jacob {
3652df76c16SMatt Jacob 	if (IS_FC(isp)) {
3662df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
3672df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
368b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
369e561aa79SAlexander Motin 			    "Chan %d Freeze simq (loopdown)", chan);
3702df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
371c07b9e07SAlexander Motin 			xpt_hold_boot();
3722df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
373fdeb9f2fSMatt Jacob 		} else {
374b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
375e561aa79SAlexander Motin 			    "Chan %d Mark simq frozen (loopdown)", chan);
3762df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3772df76c16SMatt Jacob 		}
378fdeb9f2fSMatt Jacob 	}
379fdeb9f2fSMatt Jacob }
380fdeb9f2fSMatt Jacob 
381e95725cbSMatt Jacob static void
382e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
383e95725cbSMatt Jacob {
384e95725cbSMatt Jacob 	if (IS_FC(isp)) {
385e95725cbSMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
386e95725cbSMatt Jacob 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
387e95725cbSMatt Jacob 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
388e95725cbSMatt Jacob 		if (wasfrozen && fc->simqfrozen == 0) {
389e561aa79SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
390e561aa79SAlexander Motin 			    "Chan %d Release simq", chan);
391e95725cbSMatt Jacob 			xpt_release_simq(fc->sim, 1);
392c07b9e07SAlexander Motin 			xpt_release_boot();
393e95725cbSMatt Jacob 		}
394e95725cbSMatt Jacob 	}
395e95725cbSMatt Jacob }
396e95725cbSMatt Jacob 
3979cd7268eSMatt Jacob 
3985d571944SMatt Jacob static int
3992df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
4005d571944SMatt Jacob {
4019cd7268eSMatt Jacob 	ispsoftc_t *isp;
4022df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
4035d571944SMatt Jacob 
4042df76c16SMatt Jacob 	isp = dev->si_drv1;
4055d571944SMatt Jacob 
406c1504bc0SMatt Jacob 	switch (c) {
4075d571944SMatt Jacob 	case ISP_SDBLEV:
4085d571944SMatt Jacob 	{
4095d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
4105d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
4115d571944SMatt Jacob 		*(int *)addr = olddblev;
4125d571944SMatt Jacob 		retval = 0;
4135d571944SMatt Jacob 		break;
4145d571944SMatt Jacob 	}
415746e9c85SMatt Jacob 	case ISP_GETROLE:
4162df76c16SMatt Jacob 		chan = *(int *)addr;
4172df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4182df76c16SMatt Jacob 			retval = -ENXIO;
4192df76c16SMatt Jacob 			break;
4202df76c16SMatt Jacob 		}
4212df76c16SMatt Jacob 		if (IS_FC(isp)) {
4222df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4232df76c16SMatt Jacob 		} else {
4243e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4252df76c16SMatt Jacob 		}
426746e9c85SMatt Jacob 		retval = 0;
427746e9c85SMatt Jacob 		break;
428746e9c85SMatt Jacob 	case ISP_SETROLE:
4293e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4303e6deb33SAlexander Motin 			break;
431746e9c85SMatt Jacob 		nr = *(int *)addr;
4322df76c16SMatt Jacob 		chan = nr >> 8;
4332df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4342df76c16SMatt Jacob 			retval = -ENXIO;
4352df76c16SMatt Jacob 			break;
4362df76c16SMatt Jacob 		}
4372df76c16SMatt Jacob 		nr &= 0xff;
438746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
439746e9c85SMatt Jacob 			retval = EINVAL;
440746e9c85SMatt Jacob 			break;
441746e9c85SMatt Jacob 		}
4422df76c16SMatt Jacob 		ISP_LOCK(isp);
4436bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4446bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4456bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4462df76c16SMatt Jacob 		retval = 0;
4472df76c16SMatt Jacob 		break;
4482df76c16SMatt Jacob 
4495d571944SMatt Jacob 	case ISP_RESETHBA:
450511ced9bSMatt Jacob 		ISP_LOCK(isp);
4512df76c16SMatt Jacob 		isp_reinit(isp, 0);
452511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4535d571944SMatt Jacob 		retval = 0;
4545d571944SMatt Jacob 		break;
4552df76c16SMatt Jacob 
456f553351eSMatt Jacob 	case ISP_RESCAN:
4575d571944SMatt Jacob 		if (IS_FC(isp)) {
4582df76c16SMatt Jacob 			chan = *(int *)addr;
4592df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4602df76c16SMatt Jacob 				retval = -ENXIO;
4612df76c16SMatt Jacob 				break;
4622df76c16SMatt Jacob 			}
463511ced9bSMatt Jacob 			ISP_LOCK(isp);
464e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4655d571944SMatt Jacob 				retval = EIO;
4665d571944SMatt Jacob 			} else {
4675d571944SMatt Jacob 				retval = 0;
4685d571944SMatt Jacob 			}
469511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4705d571944SMatt Jacob 		}
4715d571944SMatt Jacob 		break;
4722df76c16SMatt Jacob 
4735d571944SMatt Jacob 	case ISP_FC_LIP:
4745d571944SMatt Jacob 		if (IS_FC(isp)) {
4752df76c16SMatt Jacob 			chan = *(int *)addr;
4762df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4772df76c16SMatt Jacob 				retval = -ENXIO;
4782df76c16SMatt Jacob 				break;
4792df76c16SMatt Jacob 			}
480511ced9bSMatt Jacob 			ISP_LOCK(isp);
4812df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4825d571944SMatt Jacob 				retval = EIO;
4835d571944SMatt Jacob 			} else {
4845d571944SMatt Jacob 				retval = 0;
4855d571944SMatt Jacob 			}
486511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4875d571944SMatt Jacob 		}
4885d571944SMatt Jacob 		break;
4895d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4905d571944SMatt Jacob 	{
4915d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
49210365e5aSMatt Jacob 		fcportdb_t *lp;
4935d571944SMatt Jacob 
49402e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
49502e2b2d9SMatt Jacob 			break;
49602e2b2d9SMatt Jacob 		}
4972e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
4985d571944SMatt Jacob 			retval = EINVAL;
4995d571944SMatt Jacob 			break;
5005d571944SMatt Jacob 		}
5012df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
502e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
503387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
50410365e5aSMatt Jacob 			ifc->loopid = lp->handle;
5055d571944SMatt Jacob 			ifc->portid = lp->portid;
5065d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
5075d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
5085d571944SMatt Jacob 			retval = 0;
5095d571944SMatt Jacob 		} else {
5105d571944SMatt Jacob 			retval = ENODEV;
5115d571944SMatt Jacob 		}
5125d571944SMatt Jacob 		break;
5135d571944SMatt Jacob 	}
5142903b272SMatt Jacob 	case ISP_GET_STATS:
5152903b272SMatt Jacob 	{
5162903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
5172903b272SMatt Jacob 
5182df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
5192903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
5202903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
5212903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
522511ced9bSMatt Jacob 		ISP_LOCK(isp);
5232903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
5242903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
5252903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
5262903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
5272903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
5282903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
5292903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
5302903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
531511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5322903b272SMatt Jacob 		retval = 0;
5332903b272SMatt Jacob 		break;
5342903b272SMatt Jacob 	}
5352903b272SMatt Jacob 	case ISP_CLR_STATS:
536511ced9bSMatt Jacob 		ISP_LOCK(isp);
5372903b272SMatt Jacob 		isp->isp_intcnt = 0;
5382903b272SMatt Jacob 		isp->isp_intbogus = 0;
5392903b272SMatt Jacob 		isp->isp_intmboxc = 0;
5402903b272SMatt Jacob 		isp->isp_intoasync = 0;
5412903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
5422903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
5432903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
5442903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
545511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5462903b272SMatt Jacob 		retval = 0;
5472903b272SMatt Jacob 		break;
548570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
549570c7a3fSMatt Jacob 	{
550570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5512df76c16SMatt Jacob 		int chan = hba->fc_channel;
55202e2b2d9SMatt Jacob 
5532df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5542df76c16SMatt Jacob 			retval = ENXIO;
5552df76c16SMatt Jacob 			break;
5562df76c16SMatt Jacob 		}
557a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
558a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
559a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5602df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
56102e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5622df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5632df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5642df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5652df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5662df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5672df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5682df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5692df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5702df76c16SMatt Jacob 		} else {
5712df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5722df76c16SMatt Jacob 			hba->fc_speed = 0;
5732df76c16SMatt Jacob 			hba->fc_topology = 0;
5742df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5752df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5762df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5772df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
57802e2b2d9SMatt Jacob 		}
579570c7a3fSMatt Jacob 		retval = 0;
580570c7a3fSMatt Jacob 		break;
581570c7a3fSMatt Jacob 	}
5828e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5838e62a8acSMatt Jacob 	{
5848e62a8acSMatt Jacob 		int needmarker;
5858e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
586c5fd36edSAlexander Motin 		uint16_t nphdl;
5878e62a8acSMatt Jacob 		mbreg_t mbs;
5888e62a8acSMatt Jacob 
5898e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5908e62a8acSMatt Jacob 			break;
5918e62a8acSMatt Jacob 		}
5928e62a8acSMatt Jacob 
5932df76c16SMatt Jacob 		chan = fct->chan;
5942df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5952df76c16SMatt Jacob 			retval = -ENXIO;
5962df76c16SMatt Jacob 			break;
5972df76c16SMatt Jacob 		}
5982df76c16SMatt Jacob 
5998e62a8acSMatt Jacob 		needmarker = retval = 0;
600c5fd36edSAlexander Motin 		nphdl = fct->loopid;
6012df76c16SMatt Jacob 		ISP_LOCK(isp);
6022df76c16SMatt Jacob 		if (IS_24XX(isp)) {
60353791a95SAlexander Motin 			void *reqp;
60453791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
60553791a95SAlexander Motin 			isp24xx_tmf_t tmf;
60653791a95SAlexander Motin 			isp24xx_statusreq_t sp;
6072df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
6082df76c16SMatt Jacob 			fcportdb_t *lp;
6092df76c16SMatt Jacob 			int i;
6102df76c16SMatt Jacob 
6112df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
6122df76c16SMatt Jacob 				lp = &fcp->portdb[i];
613c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
6142df76c16SMatt Jacob 					break;
6152df76c16SMatt Jacob 				}
6162df76c16SMatt Jacob 			}
6172df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
6182df76c16SMatt Jacob 				retval = ENXIO;
6192df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6202df76c16SMatt Jacob 				break;
6212df76c16SMatt Jacob 			}
62253791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
62353791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
62453791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
62553791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
62653791a95SAlexander Motin 			tmf.tmf_delay = 2;
62753791a95SAlexander Motin 			tmf.tmf_timeout = 4;
62853791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
62953791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
63053791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
63153791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
6322df76c16SMatt Jacob 			if (fct->lun >= 256) {
63353791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
6342df76c16SMatt Jacob 			}
6352df76c16SMatt Jacob 			switch (fct->action) {
6362df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
63753791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
6382df76c16SMatt Jacob 				break;
6392df76c16SMatt Jacob 			case IPT_TARGET_RESET:
64053791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
6412df76c16SMatt Jacob 				needmarker = 1;
6422df76c16SMatt Jacob 				break;
6432df76c16SMatt Jacob 			case IPT_LUN_RESET:
64453791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
6452df76c16SMatt Jacob 				needmarker = 1;
6462df76c16SMatt Jacob 				break;
6472df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
64853791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6492df76c16SMatt Jacob 				needmarker = 1;
6502df76c16SMatt Jacob 				break;
6512df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
65253791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6532df76c16SMatt Jacob 				needmarker = 1;
6542df76c16SMatt Jacob 				break;
6552df76c16SMatt Jacob 			default:
6562df76c16SMatt Jacob 				retval = EINVAL;
6572df76c16SMatt Jacob 				break;
6582df76c16SMatt Jacob 			}
6592df76c16SMatt Jacob 			if (retval) {
6602df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6612df76c16SMatt Jacob 				break;
6622df76c16SMatt Jacob 			}
6632df76c16SMatt Jacob 
66453791a95SAlexander Motin 			/* Prepare space for response in memory */
66553791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
66653791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
66753791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
66853791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
66953791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
67053791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
67153791a95SAlexander Motin 				    __func__, chan);
6722df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6732df76c16SMatt Jacob 				retval = ENOMEM;
6742df76c16SMatt Jacob 				break;
6752df76c16SMatt Jacob 			}
67653791a95SAlexander Motin 
67753791a95SAlexander Motin 			/* Send request and wait for response. */
67853791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
67953791a95SAlexander Motin 			if (reqp == NULL) {
68053791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
68153791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
68253791a95SAlexander Motin 				    __func__, chan);
68353791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
68453791a95SAlexander Motin 				ISP_UNLOCK(isp);
6852df76c16SMatt Jacob 				retval = EIO;
68653791a95SAlexander Motin 				break;
6872df76c16SMatt Jacob 			}
68853791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
68953791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
69053791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
69153791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
69253791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
69353791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
69453791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
69553791a95SAlexander Motin 				    __func__, chan);
69653791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
69753791a95SAlexander Motin 				ISP_UNLOCK(isp);
69853791a95SAlexander Motin 				retval = EIO;
69953791a95SAlexander Motin 				break;
70053791a95SAlexander Motin 			}
70153791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
70253791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
70353791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
70453791a95SAlexander Motin 
70553791a95SAlexander Motin 			if (sp.req_completion_status != 0)
70653791a95SAlexander Motin 				retval = EIO;
70753791a95SAlexander Motin 			else if (needmarker)
7082df76c16SMatt Jacob 				fcp->sendmarker = 1;
7092df76c16SMatt Jacob 		} else {
7102df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
7112df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
712c5fd36edSAlexander Motin 				nphdl <<= 8;
713e5265237SMatt Jacob 			}
7148e62a8acSMatt Jacob 			switch (fct->action) {
715f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
7168e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
717c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7188e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7198e62a8acSMatt Jacob 				break;
720f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
7218e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
722c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7238e62a8acSMatt Jacob 				needmarker = 1;
7248e62a8acSMatt Jacob 				break;
725f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
7268e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
727c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7288e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7298e62a8acSMatt Jacob 				needmarker = 1;
7308e62a8acSMatt Jacob 				break;
731f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
7328e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
733c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7348e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7358e62a8acSMatt Jacob 				needmarker = 1;
7368e62a8acSMatt Jacob 				break;
737f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
7388e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
739c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7408e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7418e62a8acSMatt Jacob 				needmarker = 1;
7428e62a8acSMatt Jacob 				break;
7438e62a8acSMatt Jacob 			default:
7448e62a8acSMatt Jacob 				retval = EINVAL;
7458e62a8acSMatt Jacob 				break;
7468e62a8acSMatt Jacob 			}
7478e62a8acSMatt Jacob 			if (retval == 0) {
7488e62a8acSMatt Jacob 				if (needmarker) {
7492df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7508e62a8acSMatt Jacob 				}
7518e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7522df76c16SMatt Jacob 				if (retval) {
7538e62a8acSMatt Jacob 					retval = EIO;
7548e62a8acSMatt Jacob 				}
7552df76c16SMatt Jacob 			}
7562df76c16SMatt Jacob 		}
7572df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7588e62a8acSMatt Jacob 		break;
7598e62a8acSMatt Jacob 	}
7605d571944SMatt Jacob 	default:
7615d571944SMatt Jacob 		break;
7625d571944SMatt Jacob 	}
7635d571944SMatt Jacob 	return (retval);
7640470d791SMatt Jacob }
765478f8a96SJustin T. Gibbs 
766d81ba9d5SMatt Jacob /*
7672df76c16SMatt Jacob  * Local Inlines
7682df76c16SMatt Jacob  */
7692df76c16SMatt Jacob 
7702df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
7712df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
7722df76c16SMatt Jacob 
7732df76c16SMatt Jacob static ISP_INLINE int
7742df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
7752df76c16SMatt Jacob {
7762df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
7772df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
7782df76c16SMatt Jacob 		return (-1);
7792df76c16SMatt Jacob 	}
7802df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
7812df76c16SMatt Jacob 	return (0);
7822df76c16SMatt Jacob }
7832df76c16SMatt Jacob 
7842df76c16SMatt Jacob static ISP_INLINE void
7852df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
7862df76c16SMatt Jacob {
787387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
788405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
789405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
790405b7a29SMatt Jacob 		PISP_PCMD(ccb)->totslen = 0;
791405b7a29SMatt Jacob 		PISP_PCMD(ccb)->cumslen = 0;
792405b7a29SMatt Jacob 		PISP_PCMD(ccb)->crn = 0;
793405b7a29SMatt Jacob #endif
794405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
7952df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
7962df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
7972df76c16SMatt Jacob 	}
798387d8239SMatt Jacob }
799387d8239SMatt Jacob 
8002df76c16SMatt Jacob /*
801d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
802d81ba9d5SMatt Jacob  */
803d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
8042df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
8058290ea90SAlexander Motin static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
8068290ea90SAlexander Motin static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
8078290ea90SAlexander Motin static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
8088290ea90SAlexander Motin static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
8098290ea90SAlexander Motin static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
8108290ea90SAlexander Motin static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
8112df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
8128290ea90SAlexander Motin static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
8132df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
8142df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
815f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
816387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
817a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
818a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
81994dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
820387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
8212df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
8222df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
8232df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
8242df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
8252df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
82696b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
8272df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
8288290ea90SAlexander Motin static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
829d81ba9d5SMatt Jacob 
8302df76c16SMatt Jacob static ISP_INLINE tstate_t *
8319cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
832d81ba9d5SMatt Jacob {
83364edff94SMatt Jacob 	tstate_t *tptr = NULL;
8342df76c16SMatt Jacob 	struct tslist *lhp;
835d81ba9d5SMatt Jacob 
8362df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
837315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
8382df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
8398290ea90SAlexander Motin 			if (tptr->ts_lun == lun)
840d81ba9d5SMatt Jacob 				return (tptr);
841d81ba9d5SMatt Jacob 		}
84264edff94SMatt Jacob 	}
8432df76c16SMatt Jacob 	return (NULL);
8442df76c16SMatt Jacob }
845d81ba9d5SMatt Jacob 
8468290ea90SAlexander Motin static int
8478290ea90SAlexander Motin isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
8482df76c16SMatt Jacob {
8492df76c16SMatt Jacob 	inot_private_data_t *ntp;
8508290ea90SAlexander Motin 	struct ntpdlist rq;
8512df76c16SMatt Jacob 
8528290ea90SAlexander Motin 	if (STAILQ_EMPTY(&tptr->restart_queue))
8538290ea90SAlexander Motin 		return (0);
8548290ea90SAlexander Motin 	STAILQ_INIT(&rq);
8558290ea90SAlexander Motin 	STAILQ_CONCAT(&rq, &tptr->restart_queue);
8568290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
8578290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&rq, next);
8588290ea90SAlexander Motin 		if (IS_24XX(isp)) {
8598290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8608290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8618290ea90SAlexander Motin 			    ((at7_entry_t *)ntp->data)->at_rxid);
8628290ea90SAlexander Motin 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
8638290ea90SAlexander Motin 		} else {
8648290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
8658290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
8668290ea90SAlexander Motin 			    ((at2_entry_t *)ntp->data)->at_rxid);
8678290ea90SAlexander Motin 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
8682df76c16SMatt Jacob 		}
8698290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
8708290ea90SAlexander Motin 		if (!STAILQ_EMPTY(&tptr->restart_queue))
8718290ea90SAlexander Motin 			break;
8722df76c16SMatt Jacob 	}
8738290ea90SAlexander Motin 	if (!STAILQ_EMPTY(&rq)) {
8748290ea90SAlexander Motin 		STAILQ_CONCAT(&rq, &tptr->restart_queue);
8758290ea90SAlexander Motin 		STAILQ_CONCAT(&tptr->restart_queue, &rq);
8762df76c16SMatt Jacob 	}
8778290ea90SAlexander Motin 	return (!STAILQ_EMPTY(&tptr->restart_queue));
878d81ba9d5SMatt Jacob }
879d81ba9d5SMatt Jacob 
8802df76c16SMatt Jacob static void
8812df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
8822df76c16SMatt Jacob {
8832df76c16SMatt Jacob 	tstate_t *tptr;
88494dff771SMatt Jacob 	union ccb *ccb;
8852df76c16SMatt Jacob 	struct tslist *lhp;
88609ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
8872df76c16SMatt Jacob 	int bus, i;
8882df76c16SMatt Jacob 
8892df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
8902df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8912df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8928290ea90SAlexander Motin 			SLIST_FOREACH(tptr, lhp, next)
8938290ea90SAlexander Motin 				isp_atio_restart(isp, bus, tptr);
89409ddc7adSAlexander Motin 		}
89509ddc7adSAlexander Motin 
89694dff771SMatt Jacob 		/*
89709ddc7adSAlexander Motin 		 * We only need to do this once per channel.
89894dff771SMatt Jacob 		 */
89909ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
90009ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
90109ddc7adSAlexander Motin 		if (ccb != NULL) {
90209ddc7adSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
90394dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
90494dff771SMatt Jacob 		}
9052df76c16SMatt Jacob 	}
9062df76c16SMatt Jacob }
9072df76c16SMatt Jacob 
9088290ea90SAlexander Motin static atio_private_data_t *
9098290ea90SAlexander Motin isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
91053036e92SMatt Jacob {
9118290ea90SAlexander Motin 	struct atpdlist *atfree;
9128290ea90SAlexander Motin 	struct atpdlist *atused;
91353036e92SMatt Jacob 	atio_private_data_t *atp;
9142df76c16SMatt Jacob 
9158290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
9168290ea90SAlexander Motin 	atp = LIST_FIRST(atfree);
9172df76c16SMatt Jacob 	if (atp) {
918523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
919523ea374SAlexander Motin 		atp->tag = tag;
9208290ea90SAlexander Motin 		ISP_GET_PC(isp, chan, atused, atused);
9218290ea90SAlexander Motin 		LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
9222df76c16SMatt Jacob 	}
92353036e92SMatt Jacob 	return (atp);
92453036e92SMatt Jacob }
925523ea374SAlexander Motin 
9268290ea90SAlexander Motin static atio_private_data_t *
9278290ea90SAlexander Motin isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
928523ea374SAlexander Motin {
9298290ea90SAlexander Motin 	struct atpdlist *atused;
930523ea374SAlexander Motin 	atio_private_data_t *atp;
931523ea374SAlexander Motin 
9328290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atused, atused);
9338290ea90SAlexander Motin 	LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
934523ea374SAlexander Motin 		if (atp->tag == tag)
9352df76c16SMatt Jacob 			return (atp);
9362df76c16SMatt Jacob 	}
93753036e92SMatt Jacob 	return (NULL);
93853036e92SMatt Jacob }
93953036e92SMatt Jacob 
9408290ea90SAlexander Motin static void
9418290ea90SAlexander Motin isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
9422df76c16SMatt Jacob {
9438290ea90SAlexander Motin 	struct atpdlist *atfree;
9448290ea90SAlexander Motin 
945387d8239SMatt Jacob 	if (atp->ests) {
946387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
947387d8239SMatt Jacob 	}
948523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
949387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
9508290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
9518290ea90SAlexander Motin 	LIST_INSERT_HEAD(atfree, atp, next);
9522df76c16SMatt Jacob }
9532df76c16SMatt Jacob 
9542df76c16SMatt Jacob static void
9558290ea90SAlexander Motin isp_dump_atpd(ispsoftc_t *isp, int chan)
9562df76c16SMatt Jacob {
9578290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
9582df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
9592df76c16SMatt Jacob 
9608290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
9618290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
962cfbeb41cSAlexander Motin 		if (atp->state == ATPD_STATE_FREE)
963cfbeb41cSAlexander Motin 			continue;
964cfbeb41cSAlexander 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",
9653f072d69SAlexander 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]);
9662df76c16SMatt Jacob 	}
9672df76c16SMatt Jacob }
9682df76c16SMatt Jacob 
9698290ea90SAlexander Motin static inot_private_data_t *
9708290ea90SAlexander Motin isp_get_ntpd(ispsoftc_t *isp, int chan)
9712df76c16SMatt Jacob {
9728290ea90SAlexander Motin 	struct ntpdlist *ntfree;
9732df76c16SMatt Jacob 	inot_private_data_t *ntp;
9748290ea90SAlexander Motin 
9758290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
9768290ea90SAlexander Motin 	ntp = STAILQ_FIRST(ntfree);
9778290ea90SAlexander Motin 	if (ntp)
9788290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(ntfree, next);
9792df76c16SMatt Jacob 	return (ntp);
9802df76c16SMatt Jacob }
9812df76c16SMatt Jacob 
9828290ea90SAlexander Motin static inot_private_data_t *
9838290ea90SAlexander Motin isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
9842df76c16SMatt Jacob {
9858290ea90SAlexander Motin 	inot_private_data_t *ntp, *ntp2;
9868290ea90SAlexander Motin 
9878290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, ntpool, ntp);
9888290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
9898290ea90SAlexander Motin 	for (; ntp < ntp2; ntp++) {
9908290ea90SAlexander Motin 		if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
9912df76c16SMatt Jacob 			return (ntp);
9922df76c16SMatt Jacob 	}
9932df76c16SMatt Jacob 	return (NULL);
9942df76c16SMatt Jacob }
9952df76c16SMatt Jacob 
9968290ea90SAlexander Motin static void
9978290ea90SAlexander Motin isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
9982df76c16SMatt Jacob {
9998290ea90SAlexander Motin 	struct ntpdlist *ntfree;
10008290ea90SAlexander Motin 
10018290ea90SAlexander Motin 	ntp->tag_id = ntp->seq_id = 0;
10028290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
10038290ea90SAlexander Motin 	STAILQ_INSERT_HEAD(ntfree, ntp, next);
10042df76c16SMatt Jacob }
10052df76c16SMatt Jacob 
1006d81ba9d5SMatt Jacob static cam_status
10072df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1008d81ba9d5SMatt Jacob {
1009d81ba9d5SMatt Jacob 	lun_id_t lun;
10102df76c16SMatt Jacob 	struct tslist *lhp;
10112df76c16SMatt Jacob 	tstate_t *tptr;
1012d81ba9d5SMatt Jacob 
1013d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
10142df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
10156af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1016d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
1017d81ba9d5SMatt Jacob 		}
10182df76c16SMatt Jacob 	}
1019e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
10202df76c16SMatt Jacob 	if (tptr == NULL) {
1021d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
1022d81ba9d5SMatt Jacob 	}
1023315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
10242df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
10252df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
1026315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
10272df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
10282df76c16SMatt Jacob 	*rslt = tptr;
10292df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1030d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1031d81ba9d5SMatt Jacob }
1032d81ba9d5SMatt Jacob 
10338290ea90SAlexander Motin static void
10348290ea90SAlexander Motin destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
1035d81ba9d5SMatt Jacob {
1036344aebe2SMatt Jacob 	union ccb *ccb;
10372df76c16SMatt Jacob 	struct tslist *lhp;
10388290ea90SAlexander Motin 	inot_private_data_t *ntp;
1039e2873b76SMatt Jacob 
10408290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
1041344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1042344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1043344aebe2SMatt Jacob 		xpt_done(ccb);
10448290ea90SAlexander Motin 	};
10458290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
1046344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1047344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1048344aebe2SMatt Jacob 		xpt_done(ccb);
1049344aebe2SMatt Jacob 	}
10508290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
10518290ea90SAlexander Motin 		isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
10528290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
10538290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
10548290ea90SAlexander Motin 	}
10558290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
10562df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1057d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1058d81ba9d5SMatt Jacob }
1059d81ba9d5SMatt Jacob 
10602df76c16SMatt Jacob static void
10612df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1062d81ba9d5SMatt Jacob {
10633e6deb33SAlexander Motin 	tstate_t *tptr;
10643e6deb33SAlexander Motin 	int bus;
10652df76c16SMatt Jacob 	target_id_t target;
1066d81ba9d5SMatt Jacob 	lun_id_t lun;
1067d81ba9d5SMatt Jacob 
10683e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
10693e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
10703e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
10713e6deb33SAlexander Motin 		xpt_done(ccb);
10723e6deb33SAlexander Motin 		return;
10733e6deb33SAlexander Motin 	}
1074e2873b76SMatt Jacob 
1075746e9c85SMatt Jacob 	/*
10763e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
10773e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1078746e9c85SMatt Jacob 	 */
10792df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10802df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10812df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10826af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
10836af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
10843e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
10852df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10862df76c16SMatt Jacob 		xpt_done(ccb);
10872df76c16SMatt Jacob 		return;
10882df76c16SMatt Jacob 	}
10892df76c16SMatt Jacob 
10903e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1091a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
10922df76c16SMatt Jacob 	if (tptr) {
10932df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
10943e6deb33SAlexander Motin 		xpt_done(ccb);
10953e6deb33SAlexander Motin 		return;
1096d81ba9d5SMatt Jacob 	}
10972df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
10982df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
10992df76c16SMatt Jacob 		xpt_done(ccb);
11003e6deb33SAlexander Motin 		return;
11012df76c16SMatt Jacob 	}
11022df76c16SMatt Jacob 
11033e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
11043e6deb33SAlexander Motin 	xpt_done(ccb);
11052df76c16SMatt Jacob }
11062df76c16SMatt Jacob 
11072df76c16SMatt Jacob static void
11082df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
11092df76c16SMatt Jacob {
11102df76c16SMatt Jacob 	tstate_t *tptr = NULL;
11112df76c16SMatt Jacob 	int bus;
11122df76c16SMatt Jacob 	target_id_t target;
11132df76c16SMatt Jacob 	lun_id_t lun;
11142df76c16SMatt Jacob 
11152df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
11162df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
11172df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
11186af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
11196af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
11203e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
11212df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
11222df76c16SMatt Jacob 		xpt_done(ccb);
11232df76c16SMatt Jacob 		return;
11242df76c16SMatt Jacob 	}
11252df76c16SMatt Jacob 
11263e6deb33SAlexander Motin 	/* Find the state pointer. */
11272df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
11283e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
11292df76c16SMatt Jacob 		xpt_done(ccb);
11303e6deb33SAlexander Motin 		return;
11312df76c16SMatt Jacob 	}
11322df76c16SMatt Jacob 
11338290ea90SAlexander Motin 	destroy_lun_state(isp, bus, tptr);
11343e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
11353e6deb33SAlexander Motin 	xpt_done(ccb);
1136d81ba9d5SMatt Jacob }
1137d81ba9d5SMatt Jacob 
11389cd7268eSMatt Jacob static void
1139387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1140d81ba9d5SMatt Jacob {
114194dff771SMatt Jacob 	int fctape, sendstatus, resid;
1142387d8239SMatt Jacob 	fcparam *fcp;
11432df76c16SMatt Jacob 	atio_private_data_t *atp;
114494dff771SMatt Jacob 	struct ccb_scsiio *cso;
114509ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
114694dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
11471dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1148d81ba9d5SMatt Jacob 
114994dff771SMatt 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,
115094dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
115194dff771SMatt Jacob 
115209ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
115394dff771SMatt Jacob 	switch (how) {
115494dff771SMatt Jacob 	case FROM_CAM:
11552df76c16SMatt Jacob 		/*
115694dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
11572df76c16SMatt Jacob 		 */
115809ddc7adSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
115994dff771SMatt Jacob 		break;
116009ddc7adSAlexander Motin 	case FROM_TIMER:
116194dff771SMatt Jacob 	case FROM_SRR:
116294dff771SMatt Jacob 	case FROM_CTIO_DONE:
116309ddc7adSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
116494dff771SMatt Jacob 		break;
116594dff771SMatt Jacob 	}
116694dff771SMatt Jacob 
116709ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
116809ddc7adSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
116994dff771SMatt Jacob 
117094dff771SMatt Jacob 		cso = &ccb->csio;
1171387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1172387d8239SMatt Jacob 		if (xfrlen == 0) {
11732df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1174387d8239SMatt 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");
11752df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
11762df76c16SMatt Jacob 				xpt_done(ccb);
117794dff771SMatt Jacob 				continue;
11782df76c16SMatt Jacob 			}
11792df76c16SMatt Jacob 		}
11802df76c16SMatt Jacob 
11818290ea90SAlexander Motin 		atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
11822df76c16SMatt Jacob 		if (atp == NULL) {
118394dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
11848290ea90SAlexander Motin 			isp_dump_atpd(isp, XS_CHANNEL(ccb));
11852df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
11862df76c16SMatt Jacob 			xpt_done(ccb);
118794dff771SMatt Jacob 			continue;
11882df76c16SMatt Jacob 		}
1189387d8239SMatt Jacob 
1190387d8239SMatt Jacob 		/*
1191387d8239SMatt Jacob 		 * Is this command a dead duck?
1192387d8239SMatt Jacob 		 */
11932df76c16SMatt Jacob 		if (atp->dead) {
119494dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
11952df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
11962df76c16SMatt Jacob 			xpt_done(ccb);
119794dff771SMatt Jacob 			continue;
11982df76c16SMatt Jacob 		}
11992df76c16SMatt Jacob 
12002df76c16SMatt Jacob 		/*
12012df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
12022df76c16SMatt Jacob 		 */
1203387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1204387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
120594dff771SMatt 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);
12062df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
12072df76c16SMatt Jacob 			xpt_done(ccb);
120894dff771SMatt Jacob 			continue;
12092df76c16SMatt Jacob 		}
12102df76c16SMatt Jacob 
12112df76c16SMatt Jacob 		/*
121294dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
121394dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1214387d8239SMatt Jacob 		 */
121594dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
121694dff771SMatt 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);
121709ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
121894dff771SMatt Jacob 			break;
1219387d8239SMatt Jacob 		}
1220387d8239SMatt Jacob 
1221d81ba9d5SMatt Jacob 		/*
1222387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1223d81ba9d5SMatt Jacob 		 */
1224387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1225387d8239SMatt Jacob 			fctape = 1;
1226387d8239SMatt Jacob 		} else {
1227387d8239SMatt Jacob 			fctape = 0;
1228387d8239SMatt Jacob 		}
1229387d8239SMatt Jacob 
1230387d8239SMatt Jacob 		/*
1231387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1232387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1233387d8239SMatt Jacob 		 */
1234387d8239SMatt Jacob 		if (atp->sendst) {
1235daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
123694dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1237387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1238387d8239SMatt Jacob 			atp->sendst = 0;
1239387d8239SMatt Jacob 		}
1240387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1241387d8239SMatt Jacob 			sendstatus = 1;
1242387d8239SMatt Jacob 		} else {
1243387d8239SMatt Jacob 			sendstatus = 0;
1244387d8239SMatt Jacob 		}
1245387d8239SMatt Jacob 
1246387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
124794dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1248387d8239SMatt Jacob 			/*
1249387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1250387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1251387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1252387d8239SMatt Jacob 			 */
1253387d8239SMatt Jacob 			sense_length = cso->sense_len;
1254387d8239SMatt Jacob 
1255387d8239SMatt Jacob 			/*
1256387d8239SMatt Jacob 			 * This 'cannot' happen
1257387d8239SMatt Jacob 			 */
1258387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1259387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1260387d8239SMatt Jacob 			}
1261387d8239SMatt Jacob 		} else {
1262387d8239SMatt Jacob 			sense_length = 0;
1263387d8239SMatt Jacob 		}
1264387d8239SMatt Jacob 
126594dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
126694dff771SMatt Jacob 
126794dff771SMatt Jacob 		/*
126894dff771SMatt Jacob 		 * Check for overflow
126994dff771SMatt Jacob 		 */
1270a6036a44SAlexander Motin 		tmp = atp->bytes_xfered + atp->bytes_in_transit;
1271a6036a44SAlexander Motin 		if (xfrlen > 0 && tmp > atp->orig_datalen) {
1272a6036a44SAlexander Motin 			isp_prt(isp, ISP_LOGERR,
1273a6036a44SAlexander Motin 			    "%s: [0x%x] data overflow by %u bytes", __func__,
1274a6036a44SAlexander Motin 			    cso->tag_id, tmp + xfrlen - atp->orig_datalen);
127594dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
127694dff771SMatt Jacob 			xpt_done(ccb);
127794dff771SMatt Jacob 			continue;
127894dff771SMatt Jacob 		}
1279a6036a44SAlexander Motin 		if (xfrlen > atp->orig_datalen - tmp) {
1280a6036a44SAlexander Motin 			xfrlen = atp->orig_datalen - tmp;
1281a6036a44SAlexander Motin 			if (xfrlen == 0 && !sendstatus) {
1282a6036a44SAlexander Motin 				cso->resid = cso->dxfer_len;
1283a6036a44SAlexander Motin 				ccb->ccb_h.status = CAM_REQ_CMP;
1284a6036a44SAlexander Motin 				xpt_done(ccb);
1285a6036a44SAlexander Motin 				continue;
1286a6036a44SAlexander Motin 			}
1287a6036a44SAlexander Motin 		}
1288387d8239SMatt Jacob 
12892df76c16SMatt Jacob 		if (IS_24XX(isp)) {
12902df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1291d81ba9d5SMatt Jacob 
12922df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
12932df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
129494dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
129594dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
12962df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
12972df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
12983f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
12993f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
13002df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
13012df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1302a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
13032df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1304387d8239SMatt Jacob 
1305387d8239SMatt Jacob 			/*
1306387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
13076f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1308387d8239SMatt Jacob 			 *
13098b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
13108b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1311387d8239SMatt Jacob 			 *
1312387d8239SMatt Jacob 			 */
1313387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1314387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
131594dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1316387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1317387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1318387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1319387d8239SMatt Jacob 					if (resid < 0) {
1320a6036a44SAlexander Motin 						cto->ct_resid = -resid;
1321387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1322387d8239SMatt Jacob 					} else if (resid > 0) {
1323a6036a44SAlexander Motin 						cto->ct_resid = resid;
1324387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1325387d8239SMatt Jacob 					}
1326387d8239SMatt Jacob 					if (fctape) {
1327387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1328387d8239SMatt Jacob 					}
1329387d8239SMatt Jacob 					if (sense_length) {
13302df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1331387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1332387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
13332df76c16SMatt Jacob 					}
13342df76c16SMatt Jacob 				} else {
1335387d8239SMatt Jacob 					bus_addr_t addr;
1336387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1337387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1338387d8239SMatt Jacob 
1339387d8239SMatt Jacob 					if (atp->ests == NULL) {
1340387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1341387d8239SMatt Jacob 						if (atp->ests == NULL) {
134209ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
134394dff771SMatt Jacob 							break;
1344387d8239SMatt Jacob 						}
1345387d8239SMatt Jacob 					}
1346387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1347387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1348387d8239SMatt Jacob 					if (fctape) {
1349387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1350387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1351387d8239SMatt Jacob 					}
1352387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1353387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1354387d8239SMatt Jacob 					if (resid < 0) {
1355387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1356387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1357387d8239SMatt Jacob 					} else if (resid > 0) {
1358387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1359387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1360387d8239SMatt Jacob 					}
1361387d8239SMatt Jacob 					if (sense_length) {
1362387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1363387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1364387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1365387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1366387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1367387d8239SMatt Jacob 					} else {
1368387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1369387d8239SMatt Jacob 					}
1370387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1371387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1372387d8239SMatt Jacob 					}
1373387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1374387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
137594dff771SMatt 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,
1376387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1377387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1378387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1379387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1380387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1381387d8239SMatt Jacob 				}
1382387d8239SMatt Jacob 				if (sense_length) {
138394dff771SMatt 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__,
138494dff771SMatt 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,
138594dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1386387d8239SMatt Jacob 				} else {
138794dff771SMatt 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__,
138894dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1389387d8239SMatt Jacob 				}
1390387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1391387d8239SMatt Jacob 			}
1392387d8239SMatt Jacob 
1393387d8239SMatt Jacob 			/*
1394387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1395387d8239SMatt Jacob 			 */
1396387d8239SMatt Jacob 			if (xfrlen != 0) {
13972df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
13982df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
13992df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
14002df76c16SMatt Jacob 				} else {
14012df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
14022df76c16SMatt Jacob 				}
1403387d8239SMatt Jacob 
140494dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1405387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1406387d8239SMatt Jacob 
1407387d8239SMatt Jacob #ifdef	DEBUG
1408387d8239SMatt 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) {
1409387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1410387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1411387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1412387d8239SMatt Jacob 				}
1413387d8239SMatt Jacob #endif
1414387d8239SMatt Jacob 				if (sendstatus) {
141594dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
141694dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1417387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1418387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
141994dff771SMatt Jacob 						if (fctape) {
142094dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
142194dff771SMatt Jacob 						}
1422387d8239SMatt Jacob 					} else {
1423387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
142494dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
14252df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
14262df76c16SMatt Jacob 					}
1427387d8239SMatt Jacob 				} else {
1428387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1429387d8239SMatt Jacob 				}
143094dff771SMatt 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__,
143194dff771SMatt 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);
1432387d8239SMatt Jacob 			}
14333e6deb33SAlexander Motin 		} else {
14344fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
143500a8e174SMatt Jacob 
1436387d8239SMatt Jacob 			if (isp->isp_osinfo.sixtyfourbit)
1437387d8239SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1438387d8239SMatt Jacob 			else
1439d81ba9d5SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1440d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
144194dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
144294dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1443d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1444d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
144529f76675SMatt Jacob 			} else {
1446d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
14472df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1448d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
14492ad50ca5SMatt Jacob 				}
145029f76675SMatt Jacob 			}
1451a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
145200a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1453387d8239SMatt Jacob 
1454387d8239SMatt Jacob 			/*
1455387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1456387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1457387d8239SMatt Jacob 			 *
145896240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1459387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1460387d8239SMatt Jacob 			 *
1461387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1462387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1463387d8239SMatt Jacob 			 * needed based upon this.
1464387d8239SMatt Jacob 			 */
1465387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1466387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
146794dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1468387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1469387d8239SMatt Jacob 					if (resid < 0) {
1470387d8239SMatt Jacob 						cto->ct_resid = -resid;
1471387d8239SMatt Jacob 					} else if (resid > 0) {
1472387d8239SMatt Jacob 						cto->ct_resid = resid;
1473387d8239SMatt Jacob 					}
1474387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1475f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1476387d8239SMatt Jacob 					if (resid < 0) {
14772df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1478387d8239SMatt Jacob 					} else if (resid > 0) {
14792df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1480f48ce188SMatt Jacob 					}
1481387d8239SMatt Jacob 					if (fctape) {
1482387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1483387d8239SMatt Jacob 					}
1484387d8239SMatt Jacob 					if (sense_length) {
148500a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1486387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1487387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
148800a8e174SMatt Jacob 					}
148900a8e174SMatt Jacob 				} else {
1490387d8239SMatt Jacob 					bus_addr_t addr;
1491387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1492387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1493387d8239SMatt Jacob 
1494387d8239SMatt Jacob 					if (atp->ests == NULL) {
1495387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1496387d8239SMatt Jacob 						if (atp->ests == NULL) {
149709ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
149894dff771SMatt Jacob 							break;
1499387d8239SMatt Jacob 						}
1500387d8239SMatt Jacob 					}
1501387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1502387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1503387d8239SMatt Jacob 					if (fctape) {
1504387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1505387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1506387d8239SMatt Jacob 					}
1507387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1508387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1509387d8239SMatt Jacob 					if (resid < 0) {
1510387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1511387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1512387d8239SMatt Jacob 					} else if (resid > 0) {
1513387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1514387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1515387d8239SMatt Jacob 					}
1516387d8239SMatt Jacob 					if (sense_length) {
1517387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1518387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1519387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1520387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1521387d8239SMatt Jacob 					} else {
1522387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1523387d8239SMatt Jacob 					}
1524387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1525387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1526387d8239SMatt Jacob 					}
1527387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1528387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
152994dff771SMatt 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,
1530387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1531387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1532a1fa0267SAlexander Motin 					if (cto->ct_header.rqs_entry_type == RQSTYPE_CTIO3) {
1533387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1534387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1535387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1536387d8239SMatt Jacob 					} else {
1537387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1538387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1539387d8239SMatt Jacob 					}
1540387d8239SMatt Jacob 				}
1541387d8239SMatt Jacob 				if (sense_length) {
154294dff771SMatt 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__,
154394dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
154494dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1545387d8239SMatt Jacob 				} else {
154694dff771SMatt 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,
154794dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1548387d8239SMatt Jacob 				}
1549387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1550387d8239SMatt Jacob 			}
1551387d8239SMatt Jacob 
1552387d8239SMatt Jacob 			if (xfrlen != 0) {
155300a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
155400a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
155500a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
155600a8e174SMatt Jacob 				} else {
155700a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1558d81ba9d5SMatt Jacob 				}
155953036e92SMatt Jacob 
156094dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1561387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1562387d8239SMatt Jacob 
1563387d8239SMatt Jacob 				if (sendstatus) {
156494dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
156594dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1566387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1567387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
156894dff771SMatt Jacob 						if (fctape) {
156994dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
157094dff771SMatt Jacob 						}
1571387d8239SMatt Jacob 					} else {
1572387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
157394dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1574387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1575387d8239SMatt Jacob 					}
1576387d8239SMatt Jacob 				} else {
1577387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1578387d8239SMatt Jacob 				}
1579387d8239SMatt Jacob 			}
158094dff771SMatt 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,
158194dff771SMatt 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);
1582d81ba9d5SMatt Jacob 		}
1583d81ba9d5SMatt Jacob 
158494dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
158594dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
158609ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
158794dff771SMatt Jacob 			break;
158894dff771SMatt Jacob 		}
1589970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1590970ceb2fSAlexander Motin 		if (handle == 0) {
1591387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
159209ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
159394dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
159494dff771SMatt Jacob 			break;
1595d81ba9d5SMatt Jacob 		}
159694dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
159794dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
159894dff771SMatt Jacob 
1599d81ba9d5SMatt Jacob 
1600d81ba9d5SMatt Jacob 		/*
1601d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1602d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1603b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1604d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1605d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1606d81ba9d5SMatt Jacob 		 * format.
1607d81ba9d5SMatt Jacob 		 */
1608d81ba9d5SMatt Jacob 
16092df76c16SMatt Jacob 		if (IS_24XX(isp)) {
16102df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
16112df76c16SMatt Jacob 			cto->ct_syshandle = handle;
161210365e5aSMatt Jacob 		} else {
16133e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
161410365e5aSMatt Jacob 			cto->ct_syshandle = handle;
161510365e5aSMatt Jacob 		}
1616a1bc34c6SMatt Jacob 
16172df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
161894dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1619970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
162094dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
162194dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
162209ddc7adSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
162394dff771SMatt Jacob 				break;
162494dff771SMatt Jacob 			}
162594dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
162694dff771SMatt Jacob 			xpt_done(ccb);
162794dff771SMatt Jacob 			continue;
162894dff771SMatt Jacob 		}
16292df76c16SMatt Jacob 		isp->isp_nactive++;
163094dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1631387d8239SMatt Jacob 		if (xfrlen) {
1632387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1633387d8239SMatt Jacob 		} else {
1634387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1635387d8239SMatt Jacob 		}
1636387d8239SMatt Jacob 		atp->ctcnt++;
163794dff771SMatt Jacob 		atp->seqno++;
16382df76c16SMatt Jacob 	}
1639d81ba9d5SMatt Jacob }
1640d81ba9d5SMatt Jacob 
1641a1bc34c6SMatt Jacob static void
1642a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1643f48ce188SMatt Jacob {
16442df76c16SMatt Jacob 	union ccb *ccb = arg;
16452a0db815SJohn Baldwin 
1646b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
16472df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1648a1bc34c6SMatt Jacob }
1649a1bc34c6SMatt Jacob 
1650a1bc34c6SMatt Jacob static void
1651387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1652387d8239SMatt Jacob {
1653387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1654387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
16552a0db815SJohn Baldwin 
16562a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1657387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
16582a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1659387d8239SMatt Jacob 	} else {
1660387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1661387d8239SMatt Jacob 	}
1662387d8239SMatt Jacob }
1663387d8239SMatt Jacob 
1664387d8239SMatt Jacob 
1665387d8239SMatt Jacob static void
1666a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1667a1bc34c6SMatt Jacob {
16689cd7268eSMatt Jacob 	ispsoftc_t *isp;
1669a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
1670a1bc34c6SMatt Jacob 	void *qe;
16713e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1672a1bc34c6SMatt Jacob 
1673a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
1674f48ce188SMatt Jacob 
16752df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
16762df76c16SMatt Jacob 	if (qe == NULL) {
1677c3167cabSMatt Jacob 		xpt_print(ccb->ccb_h.path,
1678c3167cabSMatt Jacob 		    "%s: Request Queue Overflow\n", __func__);
16792a0db815SJohn Baldwin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
16802a0db815SJohn Baldwin 		    isp_refire_putback_atio, ccb);
1681a1bc34c6SMatt Jacob 		return;
1682f48ce188SMatt Jacob 	}
168329f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
1684a1bc34c6SMatt Jacob 	cso = &ccb->csio;
16852df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1686f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1687f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
16882df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1689a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1690f48ce188SMatt Jacob 	} else {
1691a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1692f48ce188SMatt Jacob 	}
1693f48ce188SMatt Jacob 	at->at_status = CT_OK;
1694a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
1695570c7a3fSMatt Jacob 	at->at_iid = cso->ccb_h.target_id;
16964fd13c1bSMatt Jacob 	isp_put_atio2(isp, at, qe);
16972df76c16SMatt Jacob 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
16982df76c16SMatt Jacob 	ISP_SYNC_REQUEST(isp);
1699a1bc34c6SMatt Jacob 	isp_complete_ctio(ccb);
1700f48ce188SMatt Jacob }
1701f48ce188SMatt Jacob 
1702f48ce188SMatt Jacob static void
1703a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1704f48ce188SMatt Jacob {
1705387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1706a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1707a1bc34c6SMatt Jacob 		xpt_done(ccb);
1708f48ce188SMatt Jacob 	}
1709387d8239SMatt Jacob }
1710f48ce188SMatt Jacob 
17112df76c16SMatt Jacob static void
17129cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1713d81ba9d5SMatt Jacob {
171445b9ad7aSAlexander Motin 	fcparam *fcp;
171592a1e549SMatt Jacob 	lun_id_t lun;
17162df76c16SMatt Jacob 	fcportdb_t *lp;
1717d81ba9d5SMatt Jacob 	tstate_t *tptr;
1718d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
17192df76c16SMatt Jacob 	uint16_t nphdl;
1720a035b0afSMatt Jacob 	atio_private_data_t *atp;
17212df76c16SMatt Jacob 	inot_private_data_t *ntp;
1722d81ba9d5SMatt Jacob 
1723d81ba9d5SMatt Jacob 	/*
1724d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1725d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1726d81ba9d5SMatt Jacob 	 *
1727b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1728d81ba9d5SMatt Jacob 	 */
1729d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
17302df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
17318290ea90SAlexander Motin 		isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
17322df76c16SMatt Jacob 		return;
1733d81ba9d5SMatt Jacob 	}
1734d81ba9d5SMatt Jacob 
173545b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
17362df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
173792a1e549SMatt Jacob 		lun = aep->at_scclun;
17382ad50ca5SMatt Jacob 	} else {
173992a1e549SMatt Jacob 		lun = aep->at_lun;
17402ad50ca5SMatt Jacob 	}
17412df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
17422df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
17432df76c16SMatt Jacob 	} else {
17442df76c16SMatt Jacob 		nphdl = aep->at_iid;
17452df76c16SMatt Jacob 	}
1746a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1747d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1748a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1749746e9c85SMatt Jacob 		if (tptr == NULL) {
1750123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1751387d8239SMatt Jacob 			if (lun == 0) {
17528290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1753387d8239SMatt Jacob 			} else {
17548290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1755387d8239SMatt Jacob 			}
17562df76c16SMatt Jacob 			return;
1757746e9c85SMatt Jacob 		}
1758d81ba9d5SMatt Jacob 	}
1759d81ba9d5SMatt Jacob 
1760d81ba9d5SMatt Jacob 	/*
17612df76c16SMatt Jacob 	 * Start any commands pending resources first.
1762d81ba9d5SMatt Jacob 	 */
17638290ea90SAlexander Motin 	if (isp_atio_restart(isp, 0, tptr))
17642df76c16SMatt Jacob 		goto noresrc;
17652df76c16SMatt Jacob 
17662df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
17672df76c16SMatt Jacob 	if (atiop == NULL) {
17682df76c16SMatt Jacob 		goto noresrc;
17692df76c16SMatt Jacob 	}
17702df76c16SMatt Jacob 
17718290ea90SAlexander Motin 	atp = isp_get_atpd(isp, 0, aep->at_rxid);
17722df76c16SMatt Jacob 	if (atp == NULL) {
17732df76c16SMatt Jacob 		goto noresrc;
17742df76c16SMatt Jacob 	}
17752df76c16SMatt Jacob 
1776570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1777d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1778570c7a3fSMatt Jacob 	tptr->atio_count--;
1779387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
178045b9ad7aSAlexander Motin 	atiop->ccb_h.target_id = fcp->isp_loopid;
178192a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
17822df76c16SMatt Jacob 
1783b0a3ba7eSMatt Jacob 	/*
1784b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1785b0a3ba7eSMatt Jacob 	 */
1786f48ce188SMatt Jacob 	atiop->sense_len = 0;
17872df76c16SMatt Jacob 
17882df76c16SMatt Jacob 	/*
17892df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
17902df76c16SMatt Jacob 	 */
1791d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1792d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1793d4f3ad3aSAlexander Motin 	else {
1794d4f3ad3aSAlexander Motin 		if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1795e68eef14SAlexander Motin 		     lp->state == FC_PORTDB_STATE_ZOMBIE)) {
179637a7daacSAlexander Motin 			uint64_t wwpn =
17972df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[0]) << 48) |
17982df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[1]) << 32) |
17992df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[2]) << 16) |
18002df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[3]) <<  0);
180137a7daacSAlexander Motin 			isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
180237a7daacSAlexander Motin 			    nphdl, PORT_ANY, 0);
180345b9ad7aSAlexander Motin 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
180445b9ad7aSAlexander Motin 				fcp->isp_loopstate = LOOP_LTEST_DONE;
180545b9ad7aSAlexander Motin 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
180645b9ad7aSAlexander Motin 			    ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1807d4f3ad3aSAlexander Motin 			isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1808d4f3ad3aSAlexander Motin 		}
1809d4f3ad3aSAlexander Motin 		atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
18102df76c16SMatt Jacob 	}
1811d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
18122df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1813d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
18142df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1815d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1816d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
18175e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1818d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1819d81ba9d5SMatt Jacob 		break;
1820d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
18215e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1822d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1823d81ba9d5SMatt Jacob 		break;
1824d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
18255e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1826d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1827d81ba9d5SMatt Jacob 		break;
1828d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1829d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1830d81ba9d5SMatt Jacob 	default:
1831d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1832d81ba9d5SMatt Jacob 		break;
1833d81ba9d5SMatt Jacob 	}
1834f48ce188SMatt Jacob 
183553036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
183653036e92SMatt Jacob 	atp->bytes_xfered = 0;
18372df76c16SMatt Jacob 	atp->lun = lun;
1838d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
18392df76c16SMatt Jacob 	atp->sid = PORT_ANY;
18402df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
18412df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
18422df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1843570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
1844d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
1845123055f0SNathan 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);
18462df76c16SMatt Jacob 	return;
18472df76c16SMatt Jacob noresrc:
18488290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, 0);
18492df76c16SMatt Jacob 	if (ntp == NULL) {
18508290ea90SAlexander Motin 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
18512df76c16SMatt Jacob 		return;
18522df76c16SMatt Jacob 	}
18538290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
18548290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1855d81ba9d5SMatt Jacob }
1856d81ba9d5SMatt Jacob 
18572df76c16SMatt Jacob static void
18582df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
18592df76c16SMatt Jacob {
18602df76c16SMatt Jacob 	int cdbxlen;
18616af11b82SAlexander Motin 	lun_id_t lun;
18626af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
18632df76c16SMatt Jacob 	uint32_t did, sid;
18642df76c16SMatt Jacob 	fcportdb_t *lp;
18652df76c16SMatt Jacob 	tstate_t *tptr;
18662df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
18672df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
18689e7d423dSMatt Jacob 	atio_private_data_t *oatp;
18692df76c16SMatt Jacob 	inot_private_data_t *ntp;
18702df76c16SMatt Jacob 
18712df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
18722df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
18736af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
18742df76c16SMatt Jacob 
18752df76c16SMatt Jacob 	/*
18762df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
18772df76c16SMatt Jacob 	 *
18782df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
18792df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
18802df76c16SMatt Jacob 	 */
18817dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
18822df76c16SMatt Jacob 		/*
18832df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
18842df76c16SMatt Jacob 		 */
18852df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
18862df76c16SMatt Jacob 
18872df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
18882df76c16SMatt Jacob 			NANOTIME_T now;
18892df76c16SMatt Jacob 
18902df76c16SMatt Jacob 			/*
18912df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
18922df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
18932df76c16SMatt Jacob 			 */
18942df76c16SMatt Jacob 			GET_NANOTIME(&now);
1895514a71ebSAlexander Motin 			if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
18962df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
18972df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
18982df76c16SMatt Jacob 				return;
18992df76c16SMatt Jacob 			}
19002df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
19012df76c16SMatt Jacob 			if (tptr == NULL) {
19022df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
19032df76c16SMatt Jacob 				if (tptr == NULL) {
19042df76c16SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
19052df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
19062df76c16SMatt Jacob 					return;
19072df76c16SMatt Jacob 				}
19082df76c16SMatt Jacob 			}
19092df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
19102df76c16SMatt Jacob 			goto noresrc;
19112df76c16SMatt Jacob 		}
19122df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
19132df76c16SMatt Jacob 	} else {
19142df76c16SMatt Jacob 		chan = 0;
19152df76c16SMatt Jacob 	}
19162df76c16SMatt Jacob 
19172df76c16SMatt Jacob 	/*
19182df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
19192df76c16SMatt Jacob 	 */
1920eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
19212df76c16SMatt Jacob 		/*
19222df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
19232df76c16SMatt Jacob 		 */
19242df76c16SMatt 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",
19252df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
1926e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
19272df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
19282df76c16SMatt Jacob 		return;
19292df76c16SMatt Jacob 	}
19302df76c16SMatt Jacob 	nphdl = lp->handle;
19312df76c16SMatt Jacob 
19322df76c16SMatt Jacob 	/*
19332df76c16SMatt Jacob 	 * Get the tstate pointer
19342df76c16SMatt Jacob 	 */
19352df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
19362df76c16SMatt Jacob 	if (tptr == NULL) {
19372df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
19382df76c16SMatt Jacob 		if (tptr == NULL) {
19396af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
19406af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
19416af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
1942387d8239SMatt Jacob 			if (lun == 0) {
1943352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1944387d8239SMatt Jacob 			} else {
19452df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1946387d8239SMatt Jacob 			}
19472df76c16SMatt Jacob 			return;
19482df76c16SMatt Jacob 		}
19492df76c16SMatt Jacob 	}
19502df76c16SMatt Jacob 
19512df76c16SMatt Jacob 	/*
19522df76c16SMatt Jacob 	 * Start any commands pending resources first.
19532df76c16SMatt Jacob 	 */
19548290ea90SAlexander Motin 	if (isp_atio_restart(isp, chan, tptr))
19552df76c16SMatt Jacob 		goto noresrc;
19562df76c16SMatt Jacob 
19572df76c16SMatt Jacob 	/*
19582df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
19592df76c16SMatt Jacob 	 */
19602df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
19612df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
19622df76c16SMatt Jacob 		return;
19632df76c16SMatt Jacob 	}
19642df76c16SMatt Jacob 
19652df76c16SMatt Jacob 	/*
19662df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
19672df76c16SMatt Jacob 	 */
19682df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
19692df76c16SMatt Jacob 	if (atiop == NULL) {
19702df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
19712df76c16SMatt Jacob 		goto noresrc;
19722df76c16SMatt Jacob 	}
19732df76c16SMatt Jacob 
19748290ea90SAlexander Motin 	oatp = isp_find_atpd(isp, chan, aep->at_rxid);
19759e7d423dSMatt Jacob 	if (oatp) {
1976387d8239SMatt 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",
19779e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
19782df76c16SMatt Jacob 		/*
19792df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
19802df76c16SMatt Jacob 		 */
19812df76c16SMatt Jacob 		goto noresrc;
19822df76c16SMatt Jacob 	}
19838290ea90SAlexander Motin 	atp = isp_get_atpd(isp, chan, aep->at_rxid);
1984523ea374SAlexander Motin 	if (atp == NULL) {
1985523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1986523ea374SAlexander Motin 		goto noresrc;
1987523ea374SAlexander Motin 	}
1988387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
19892df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
19902df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
19912df76c16SMatt Jacob 	tptr->atio_count--;
1992387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1993d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
19942df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
19952df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
19962df76c16SMatt Jacob 	atiop->sense_len = 0;
19972df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
19982df76c16SMatt Jacob 	if (cdbxlen) {
19992df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
20002df76c16SMatt Jacob 	}
20012df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
20022df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
20032df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
20042df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
20052df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
20062df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
20072df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
20085e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20092df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
20102df76c16SMatt Jacob 		break;
20112df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
20125e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20132df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
20142df76c16SMatt Jacob 		break;
20152df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
20165e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20172df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
20182df76c16SMatt Jacob 		break;
20192df76c16SMatt Jacob 	default:
20202df76c16SMatt Jacob 		/* FALLTHROUGH */
20212df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
20222df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
20232df76c16SMatt Jacob 		atiop->tag_action = 0;
20242df76c16SMatt Jacob 		break;
20252df76c16SMatt Jacob 	}
20262df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
20272df76c16SMatt Jacob 	atp->bytes_xfered = 0;
20282df76c16SMatt Jacob 	atp->lun = lun;
20292df76c16SMatt Jacob 	atp->nphdl = nphdl;
20303f072d69SAlexander Motin 	atp->sid = sid;
20313f072d69SAlexander Motin 	atp->did = did;
20322df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
20339e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
20342df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
20352df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
20362df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
20376af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
20386af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
20392df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
20402df76c16SMatt Jacob 	return;
20412df76c16SMatt Jacob noresrc:
20428290ea90SAlexander Motin 	if (atp)
20438290ea90SAlexander Motin 		isp_put_atpd(isp, chan, atp);
20448290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, chan);
20452df76c16SMatt Jacob 	if (ntp == NULL) {
20462df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
20472df76c16SMatt Jacob 		return;
20482df76c16SMatt Jacob 	}
20498290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
20508290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
20512df76c16SMatt Jacob }
20522df76c16SMatt Jacob 
2053387d8239SMatt Jacob 
2054387d8239SMatt Jacob /*
2055387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
2056387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
2057387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
2058387d8239SMatt Jacob  * transaction.
2059387d8239SMatt Jacob  */
2060387d8239SMatt Jacob static void
20618290ea90SAlexander Motin isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
2062387d8239SMatt Jacob {
2063387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
2064387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2065387d8239SMatt Jacob 	union ccb *ccb;
2066387d8239SMatt Jacob 
2067387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
2068387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2069387d8239SMatt Jacob 	ccb = atp->srr_ccb;
2070387d8239SMatt Jacob 	atp->srr_ccb = NULL;
2071387d8239SMatt Jacob 	atp->nsrr++;
2072387d8239SMatt Jacob 	if (ccb == NULL) {
207394dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2074387d8239SMatt Jacob 		goto fail;
2075387d8239SMatt Jacob 	}
2076387d8239SMatt Jacob 
2077387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
2078387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
2079387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2080387d8239SMatt Jacob 
2081387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
2082387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
2083387d8239SMatt Jacob 		/*
2084387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
2085387d8239SMatt Jacob 		 */
2086387d8239SMatt Jacob 		atp->sendst = 0;
2087387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
2088387d8239SMatt Jacob 		if (ccb_len == 0) {
208994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2090387d8239SMatt Jacob 			goto mdp;
2091387d8239SMatt Jacob 		}
2092387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
209394dff771SMatt 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);
2094387d8239SMatt Jacob 			goto mdp;
2095387d8239SMatt Jacob 		}
209694dff771SMatt 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);
2097387d8239SMatt Jacob 		break;
2098387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
209994dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2100387d8239SMatt Jacob 		atp->sendst = 1;
2101387d8239SMatt Jacob 		/*
2102387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
2103387d8239SMatt Jacob 		 */
2104387d8239SMatt Jacob 		break;
2105387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
2106387d8239SMatt Jacob 		/*
2107387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2108387d8239SMatt Jacob 		 */
2109387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2110387d8239SMatt Jacob 		goto fail;
2111387d8239SMatt Jacob 
2112387d8239SMatt Jacob 	default:
2113387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2114387d8239SMatt Jacob 		goto fail;
2115387d8239SMatt Jacob 	}
2116387d8239SMatt Jacob 
2117387d8239SMatt Jacob 	/*
2118387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2119387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2120387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2121387d8239SMatt Jacob 	 */
2122387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2123387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2124387d8239SMatt Jacob 		goto fail;
2125387d8239SMatt Jacob 	}
2126387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2127387d8239SMatt Jacob 	return;
2128387d8239SMatt Jacob fail:
2129387d8239SMatt Jacob 	inot->in_reserved = 1;
2130387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2131387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2132387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2133387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2134387d8239SMatt Jacob 	return;
2135387d8239SMatt Jacob mdp:
2136387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2137387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2138387d8239SMatt Jacob 		goto fail;
2139387d8239SMatt Jacob 	}
2140387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2141387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2142387d8239SMatt Jacob 	/*
2143387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2144387d8239SMatt Jacob 	 */
2145387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2146387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2147387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2148387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2149387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2150387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2151387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2152387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2153387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2154387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2155387d8239SMatt Jacob }
2156387d8239SMatt Jacob 
2157387d8239SMatt Jacob 
2158387d8239SMatt Jacob static void
2159387d8239SMatt Jacob isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2160387d8239SMatt Jacob {
2161387d8239SMatt Jacob 	in_fcentry_24xx_t *inot = inot_raw;
2162387d8239SMatt Jacob 	atio_private_data_t *atp;
2163387d8239SMatt Jacob 	uint32_t tag = inot->in_rxid;
2164387d8239SMatt Jacob 	uint32_t bus = inot->in_vpidx;
2165387d8239SMatt Jacob 
2166387d8239SMatt Jacob 	if (!IS_24XX(isp)) {
2167387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2168387d8239SMatt Jacob 		return;
2169387d8239SMatt Jacob 	}
2170387d8239SMatt Jacob 
21718290ea90SAlexander Motin 	atp = isp_find_atpd(isp, bus, tag);
2172387d8239SMatt Jacob 	if (atp == NULL) {
2173387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2174387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2175387d8239SMatt Jacob 		return;
2176387d8239SMatt Jacob 	}
2177387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2178387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
217994dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2180387d8239SMatt Jacob 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2181387d8239SMatt Jacob 	if (atp->srr_ccb)
21828290ea90SAlexander Motin 		isp_handle_srr_start(isp, atp);
2183387d8239SMatt Jacob }
2184387d8239SMatt Jacob 
21852df76c16SMatt Jacob static void
21869cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2187d81ba9d5SMatt Jacob {
2188d81ba9d5SMatt Jacob 	union ccb *ccb;
2189a6036a44SAlexander Motin 	int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
21902df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
21912df76c16SMatt Jacob 	int bus;
2192a6036a44SAlexander Motin 	uint32_t handle, data_requested, resid;
2193d81ba9d5SMatt Jacob 
21942df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2195970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
21962df76c16SMatt Jacob 	if (ccb == NULL) {
21972df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
21982df76c16SMatt Jacob 		return;
21992df76c16SMatt Jacob 	}
2200970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
2201a6036a44SAlexander Motin 	resid = data_requested = PISP_PCMD(ccb)->datalen;
2202387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2203387d8239SMatt Jacob 	if (isp->isp_nactive) {
2204387d8239SMatt Jacob 		isp->isp_nactive--;
2205387d8239SMatt Jacob 	}
2206387d8239SMatt Jacob 
22072df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
2208387d8239SMatt Jacob 	if (IS_24XX(isp)) {
22098290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2210387d8239SMatt Jacob 	} else {
22118290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2212387d8239SMatt Jacob 	}
2213387d8239SMatt Jacob 	if (atp == NULL) {
2214af207637SAlexander Motin 		/*
22158656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
22168656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
22178656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2218af207637SAlexander Motin 		 */
22198290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2220af207637SAlexander Motin 	}
2221af207637SAlexander Motin 	if (atp == NULL) {
2222387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2223387d8239SMatt Jacob 		return;
2224387d8239SMatt Jacob 	}
2225387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
222694dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2227387d8239SMatt Jacob 	atp->ctcnt -= 1;
2228387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2229387d8239SMatt Jacob 
22302df76c16SMatt Jacob 	if (IS_24XX(isp)) {
22312df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2232d81ba9d5SMatt Jacob 
2233387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2234387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2235387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
22368290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
22372df76c16SMatt Jacob 			return;
22382df76c16SMatt Jacob 		}
2239387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
224087de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
224187de303cSAlexander Motin 			    (atp->sendst == 0);
2242387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2243387d8239SMatt Jacob 		} else {
22442df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
22452df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
224694dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2247a6036a44SAlexander Motin 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
22482df76c16SMatt Jacob 				resid = ct->ct_resid;
22492df76c16SMatt Jacob 		}
225094dff771SMatt 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),
225194dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
22523e6deb33SAlexander Motin 	} else {
22532df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2254387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2255387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2256387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
22578290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
2258387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
22592df76c16SMatt Jacob 			return;
2260570c7a3fSMatt Jacob 		}
2261387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
226287de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
226387de303cSAlexander Motin 			    (atp->sendst == 0);
2264387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2265387d8239SMatt Jacob 		} else {
2266d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2267d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
226894dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2269a6036a44SAlexander Motin 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2270a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
227153036e92SMatt Jacob 		}
227294dff771SMatt 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),
227394dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
22745d571944SMatt Jacob 	}
2275387d8239SMatt Jacob 	if (ok) {
2276a6036a44SAlexander Motin 		if (data_requested > 0) {
2277a6036a44SAlexander Motin 			atp->bytes_xfered += data_requested - resid;
2278a6036a44SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
2279a6036a44SAlexander Motin 			    (data_requested - resid);
2280387d8239SMatt Jacob 		}
2281a6036a44SAlexander Motin 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2282387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2283387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2284387d8239SMatt Jacob 	} else {
2285387d8239SMatt Jacob 		notify_cam = 1;
2286387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2287387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2288387d8239SMatt Jacob 		else
2289387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2290387d8239SMatt Jacob 	}
2291387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2292a1bc34c6SMatt Jacob 
2293a1bc34c6SMatt Jacob 	/*
2294387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2295387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2296d81ba9d5SMatt Jacob 	 */
229794dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
229894dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2299f48ce188SMatt Jacob 	if (notify_cam == 0) {
2300387d8239SMatt Jacob 		if (atp->sendst) {
2301387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2302387d8239SMatt Jacob 		}
23032df76c16SMatt Jacob 		return;
2304f48ce188SMatt Jacob 	}
2305d81ba9d5SMatt Jacob 
2306387d8239SMatt Jacob 	/*
230787de303cSAlexander Motin 	 * We are done with this ATIO if we successfully sent status.
230887de303cSAlexander Motin 	 * In all other cases expect either another CTIO or XPT_ABORT.
23098290ea90SAlexander Motin 	 */
231087de303cSAlexander Motin 	if (ok && sentstatus)
23118290ea90SAlexander Motin 		isp_put_atpd(isp, bus, atp);
23128290ea90SAlexander Motin 
23138290ea90SAlexander Motin 	/*
2314387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2315387d8239SMatt Jacob 	 *
2316387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2317387d8239SMatt Jacob 	 *
2318387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2319387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2320387d8239SMatt Jacob 	 * and try again in case we didn't have
2321387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2322387d8239SMatt Jacob 	 * once the putback is done we do the completion
2323387d8239SMatt Jacob 	 * call.
2324387d8239SMatt Jacob 	 */
2325387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2326a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2327387d8239SMatt Jacob 	} else {
2328387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2329d81ba9d5SMatt Jacob 	}
2330d81ba9d5SMatt Jacob }
2331570c7a3fSMatt Jacob 
23322df76c16SMatt Jacob static void
23339cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2334570c7a3fSMatt Jacob {
23352df76c16SMatt Jacob 	int needack = 1;
2336570c7a3fSMatt Jacob 	switch (inp->in_status) {
2337570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
23382df76c16SMatt Jacob 		/*
23392df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
23402df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
23412df76c16SMatt Jacob 		 */
23422df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2343570c7a3fSMatt Jacob 		break;
2344570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
23452df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2346570c7a3fSMatt Jacob 		break;
2347570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
23482df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
2349570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2350570c7a3fSMatt Jacob 		break;
2351570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
2352570c7a3fSMatt Jacob 	{
2353c5fd36edSAlexander Motin 		uint16_t nphdl, lun;
2354c5fd36edSAlexander Motin 		uint32_t sid;
23552df76c16SMatt Jacob 		uint64_t wwn;
23562df76c16SMatt Jacob 		fcportdb_t *lp;
235726719198SAlexander Motin 		isp_notify_t tmp, *nt = &tmp;
23582df76c16SMatt Jacob 
23592df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
23602df76c16SMatt Jacob 			lun = inp->in_scclun;
23612df76c16SMatt Jacob 		} else {
23622df76c16SMatt Jacob 			lun = inp->in_lun;
23632df76c16SMatt Jacob 		}
23642df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
2365c5fd36edSAlexander Motin 			nphdl = ((in_fcentry_e_t *)inp)->in_iid;
23662df76c16SMatt Jacob 		} else {
2367c5fd36edSAlexander Motin 			nphdl = inp->in_iid;
23682df76c16SMatt Jacob 		}
2369c5fd36edSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
23702df76c16SMatt Jacob 			wwn = lp->port_wwn;
2371d4f3ad3aSAlexander Motin 			sid = lp->portid;
23722df76c16SMatt Jacob 		} else {
23732df76c16SMatt Jacob 			wwn = INI_ANY;
2374d4f3ad3aSAlexander Motin 			sid = PORT_ANY;
23752df76c16SMatt Jacob 		}
237626719198SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx",
237726719198SAlexander Motin 		    inp->in_seqid, (unsigned long long) wwn);
2378570c7a3fSMatt Jacob 
23792df76c16SMatt Jacob 		ISP_MEMZERO(nt, sizeof (isp_notify_t));
23802df76c16SMatt Jacob 		nt->nt_hba = isp;
23812df76c16SMatt Jacob 		nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
23822df76c16SMatt Jacob 		nt->nt_wwn = wwn;
2383c5fd36edSAlexander Motin 		nt->nt_nphdl = nphdl;
2384d4f3ad3aSAlexander Motin 		nt->nt_sid = sid;
23852df76c16SMatt Jacob 		nt->nt_did = PORT_ANY;
23862df76c16SMatt Jacob 		nt->nt_lun = lun;
238726719198SAlexander Motin 		nt->nt_tagval = inp->in_seqid;
238826719198SAlexander Motin 		nt->nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
23892df76c16SMatt Jacob 		nt->nt_need_ack = 1;
23902df76c16SMatt Jacob 		nt->nt_channel = 0;
23912df76c16SMatt Jacob 		nt->nt_ncode = NT_ABORT_TASK;
239226719198SAlexander Motin 		nt->nt_lreserved = inp;
23932df76c16SMatt Jacob 		isp_handle_platform_target_tmf(isp, nt);
23942df76c16SMatt Jacob 		needack = 0;
2395570c7a3fSMatt Jacob 		break;
2396570c7a3fSMatt Jacob 	}
2397570c7a3fSMatt Jacob 	default:
2398570c7a3fSMatt Jacob 		break;
2399570c7a3fSMatt Jacob 	}
24002df76c16SMatt Jacob 	if (needack) {
2401387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
24022df76c16SMatt Jacob 	}
24032df76c16SMatt Jacob }
24042df76c16SMatt Jacob 
24052df76c16SMatt Jacob static void
24062df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
24072df76c16SMatt Jacob {
24082df76c16SMatt Jacob 	uint16_t nphdl;
2409387d8239SMatt Jacob 	uint16_t prli_options = 0;
24102df76c16SMatt Jacob 	uint32_t portid;
24112df76c16SMatt Jacob 	fcportdb_t *lp;
241237a7daacSAlexander Motin 	char *msg = NULL;
241337a7daacSAlexander Motin 	uint8_t *ptr = (uint8_t *)inot;
241437a7daacSAlexander Motin 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
24152df76c16SMatt Jacob 
24162df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
24172df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
24182df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
24192df76c16SMatt Jacob 	} else {
24202df76c16SMatt Jacob 		portid = PORT_ANY;
24212df76c16SMatt Jacob 	}
24222df76c16SMatt Jacob 
24232df76c16SMatt Jacob 	switch (inot->in_status) {
24242df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
24252df76c16SMatt Jacob 	{
242637a7daacSAlexander Motin 		char buf[16];
24272df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
24282df76c16SMatt Jacob 
24292df76c16SMatt Jacob 		/*
24302df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
2431b1ce21c6SRebecca Cran 		 * (possibly with some associated information sent upstream). This is
24322df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
24332df76c16SMatt Jacob 		 */
24342df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
24352df76c16SMatt Jacob 		case LOGO:
24362df76c16SMatt Jacob 			msg = "LOGO";
243737a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
243837a7daacSAlexander Motin 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
24392df76c16SMatt Jacob 			break;
24402df76c16SMatt Jacob 		case PRLO:
24412df76c16SMatt Jacob 			msg = "PRLO";
24422df76c16SMatt Jacob 			break;
24432df76c16SMatt Jacob 		case PLOGI:
24442df76c16SMatt Jacob 			msg = "PLOGI";
244537a7daacSAlexander Motin 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
244637a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
244737a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
244837a7daacSAlexander Motin 			    nphdl, portid, prli_options);
244937a7daacSAlexander Motin 			break;
245037a7daacSAlexander Motin 		case PRLI:
2451dad28623SMatt Jacob 			msg = "PRLI";
245237a7daacSAlexander Motin 			prli_options = inot->in_prli_options;
245337a7daacSAlexander Motin 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
245437a7daacSAlexander Motin 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
245537a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
245637a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
245737a7daacSAlexander Motin 			    nphdl, portid, prli_options);
24582df76c16SMatt Jacob 			break;
24592df76c16SMatt Jacob 		case PDISC:
24602df76c16SMatt Jacob 			msg = "PDISC";
24612df76c16SMatt Jacob 			break;
24622df76c16SMatt Jacob 		case ADISC:
24632df76c16SMatt Jacob 			msg = "ADISC";
24642df76c16SMatt Jacob 			break;
24652df76c16SMatt Jacob 		default:
24662df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
24672df76c16SMatt Jacob 			msg = buf;
24682df76c16SMatt Jacob 			break;
24692df76c16SMatt Jacob 		}
24702df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
24712df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
24722df76c16SMatt Jacob 			break;
24732df76c16SMatt Jacob 		}
24742df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
24752df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
2476387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
24772df76c16SMatt Jacob 		break;
24782df76c16SMatt Jacob 	}
24792df76c16SMatt Jacob 
24802df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
248137a7daacSAlexander Motin 		msg = "PORT LOGOUT";
2482e68eef14SAlexander Motin 		if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
24832df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
24842df76c16SMatt Jacob 		}
24852df76c16SMatt Jacob 		/* FALLTHROUGH */
24862df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
248737a7daacSAlexander Motin 		if (msg == NULL)
248837a7daacSAlexander Motin 			msg = "PORT CHANGED";
24892df76c16SMatt Jacob 		/* FALLTHROUGH */
24902df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
249137a7daacSAlexander Motin 		if (msg == NULL)
249237a7daacSAlexander Motin 			msg = "LIP RESET";
249337a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), msg, inot->in_status_subcode, nphdl);
24942df76c16SMatt Jacob 
24952df76c16SMatt Jacob 		/*
24962df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
24972df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
24982df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
24992df76c16SMatt Jacob 		 */
25002df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
2501387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
25022df76c16SMatt Jacob 		break;
25032df76c16SMatt Jacob 
25042df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
2505387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
2506387d8239SMatt Jacob 		isp_handle_srr_notify(isp, inot);
2507387d8239SMatt Jacob 		break;
2508387d8239SMatt Jacob #else
250937a7daacSAlexander Motin 		if (msg == NULL)
251037a7daacSAlexander Motin 			msg = "SRR RCVD";
2511387d8239SMatt Jacob 		/* FALLTHROUGH */
2512387d8239SMatt Jacob #endif
2513387d8239SMatt Jacob 	case IN24XX_LINK_RESET:
251437a7daacSAlexander Motin 		if (msg == NULL)
251537a7daacSAlexander Motin 			msg = "LINK RESET";
2516387d8239SMatt Jacob 	case IN24XX_LINK_FAILED:
251737a7daacSAlexander Motin 		if (msg == NULL)
251837a7daacSAlexander Motin 			msg = "LINK FAILED";
25192df76c16SMatt Jacob 	default:
252037a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2521387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
25222df76c16SMatt Jacob 		break;
25232df76c16SMatt Jacob 	}
25242df76c16SMatt Jacob }
25252df76c16SMatt Jacob 
25262df76c16SMatt Jacob static int
252796b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
25282df76c16SMatt Jacob {
25292df76c16SMatt Jacob 
25302df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
25312df76c16SMatt 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);
2532570c7a3fSMatt Jacob 		return (0);
2533570c7a3fSMatt Jacob 	}
25342df76c16SMatt Jacob 
25352df76c16SMatt Jacob 	/*
25362df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
25372df76c16SMatt Jacob 	 */
25382df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
25392df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
25402df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
25412df76c16SMatt Jacob 		fcportdb_t *lp;
25422df76c16SMatt Jacob 		uint32_t sid;
25432df76c16SMatt Jacob 		uint16_t nphdl;
25442df76c16SMatt Jacob 
25452df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2546eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
25472df76c16SMatt Jacob 			nphdl = lp->handle;
25482df76c16SMatt Jacob 		} else {
25492df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
25502df76c16SMatt Jacob 		}
25512df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
25522df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
25532df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
25542df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
25552df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
25562df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
25572df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
25582df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
25592df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
25602df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
25612df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
256296b5475bSAlexander Motin 		if (rsp != 0) {
256396b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
256496b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
256596b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
256696b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
256796b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
256896b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
256996b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
257096b5475bSAlexander Motin 		}
25712df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
25722df76c16SMatt Jacob 	}
25732df76c16SMatt Jacob 
25742df76c16SMatt Jacob 	/*
25752df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
25762df76c16SMatt Jacob 	 */
25772df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
25782df76c16SMatt Jacob 
25792df76c16SMatt Jacob 		/*
25802df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
25812df76c16SMatt Jacob 		 */
25822df76c16SMatt Jacob 		if (mp->nt_need_ack) {
25832df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
25842df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
25852df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
25862df76c16SMatt Jacob 
25872df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
25882df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
25892df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
25902df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
25912df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
25922df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
25932df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
25942df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
25952df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
25962df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
25972df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
25982df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
25992df76c16SMatt Jacob 				return (ENOMEM);
26002df76c16SMatt Jacob 			}
26012df76c16SMatt Jacob 			mp->nt_need_ack = 0;
26022df76c16SMatt Jacob 		}
26032df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
26042df76c16SMatt Jacob 			return (ENOMEM);
26052df76c16SMatt Jacob 		} else {
26062df76c16SMatt Jacob 			return (0);
26072df76c16SMatt Jacob 		}
26082df76c16SMatt Jacob 	}
26092df76c16SMatt Jacob 
26102df76c16SMatt Jacob 	/*
26112df76c16SMatt Jacob 	 * Handle logout cases here
26122df76c16SMatt Jacob 	 */
26132df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
26142df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
26152df76c16SMatt Jacob 	}
26162df76c16SMatt Jacob 
26172df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
26182df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
26192df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
26202df76c16SMatt Jacob 		}
26212df76c16SMatt Jacob 	}
26222df76c16SMatt Jacob 
26232df76c16SMatt Jacob 	/*
26242df76c16SMatt Jacob 	 * General purpose acknowledgement
26252df76c16SMatt Jacob 	 */
26262df76c16SMatt Jacob 	if (mp->nt_need_ack) {
26272df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2628387d8239SMatt Jacob 		/*
2629387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2630387d8239SMatt Jacob 		 */
26312df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
26322df76c16SMatt Jacob 	}
26332df76c16SMatt Jacob 	return (0);
26342df76c16SMatt Jacob }
26352df76c16SMatt Jacob 
26362df76c16SMatt Jacob /*
2637b1ce21c6SRebecca Cran  * Handle task management functions.
26382df76c16SMatt Jacob  *
26392df76c16SMatt Jacob  * We show up here with a notify structure filled out.
26402df76c16SMatt Jacob  *
26412df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
26422df76c16SMatt Jacob  */
26432df76c16SMatt Jacob static void
26442df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
26452df76c16SMatt Jacob {
26462df76c16SMatt Jacob 	tstate_t *tptr;
26472df76c16SMatt Jacob 	fcportdb_t *lp;
26482df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
26492df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
26502df76c16SMatt Jacob 	lun_id_t lun;
26512df76c16SMatt Jacob 
26522df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
26532df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
26542df76c16SMatt Jacob 	/*
26552df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
26562df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
26572df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
26582df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
26592df76c16SMatt Jacob 	 */
26602df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
26612df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
26622df76c16SMatt Jacob 	} else {
26632df76c16SMatt Jacob 		lun = notify->nt_lun;
26642df76c16SMatt Jacob 	}
26652df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
26662df76c16SMatt Jacob 	if (tptr == NULL) {
26672df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
26682df76c16SMatt Jacob 		if (tptr == NULL) {
2669123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
26702df76c16SMatt Jacob 			goto bad;
26712df76c16SMatt Jacob 		}
26722df76c16SMatt Jacob 	}
26732df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
26742df76c16SMatt Jacob 	if (inot == NULL) {
2675123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
26762df76c16SMatt Jacob 		goto bad;
26772df76c16SMatt Jacob 	}
26782df76c16SMatt Jacob 
2679eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2680d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
26812df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
26822df76c16SMatt Jacob 	} else {
2683d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
26842df76c16SMatt Jacob 	}
26852df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
26862df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
26872df76c16SMatt Jacob 
26882df76c16SMatt Jacob 	switch (notify->nt_ncode) {
26892df76c16SMatt Jacob 	case NT_ABORT_TASK:
26908290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
26912df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
26922df76c16SMatt Jacob 		break;
26932df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
26948290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
26952df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
26962df76c16SMatt Jacob 		break;
26972df76c16SMatt Jacob 	case NT_CLEAR_ACA:
26982df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
26992df76c16SMatt Jacob 		break;
27002df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
27012df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
27022df76c16SMatt Jacob 		break;
27032df76c16SMatt Jacob 	case NT_LUN_RESET:
27042df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
27052df76c16SMatt Jacob 		break;
27062df76c16SMatt Jacob 	case NT_TARGET_RESET:
27072df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
27082df76c16SMatt Jacob 		break;
2709c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2710c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2711c98d2b1fSAlexander Motin 		break;
2712c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2713c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2714c98d2b1fSAlexander Motin 		break;
27152df76c16SMatt Jacob 	default:
2716123055f0SNathan 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);
27172df76c16SMatt Jacob 		goto bad;
27182df76c16SMatt Jacob 	}
27192df76c16SMatt Jacob 
27208290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, notify->nt_channel);
27212df76c16SMatt Jacob 	if (ntp == NULL) {
27222df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
27232df76c16SMatt Jacob 		goto bad;
27242df76c16SMatt Jacob 	}
27258290ea90SAlexander Motin 	ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
27262df76c16SMatt Jacob 	if (notify->nt_lreserved) {
27278290ea90SAlexander Motin 		ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
27288290ea90SAlexander Motin 		ntp->nt.nt_lreserved = &ntp->data;
27292df76c16SMatt Jacob 	}
27308290ea90SAlexander Motin 	ntp->seq_id = notify->nt_tagval;
27318290ea90SAlexander Motin 	ntp->tag_id = notify->nt_tagval >> 32;
27322df76c16SMatt Jacob 
27332df76c16SMatt Jacob 	tptr->inot_count--;
27342df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2735387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
27362df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
27372df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
27382df76c16SMatt Jacob 	return;
27392df76c16SMatt Jacob bad:
27402df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
27412df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2742387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2743387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2744387d8239SMatt Jacob 			}
27452df76c16SMatt Jacob 		} else {
2746387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
27472df76c16SMatt Jacob 		}
27482df76c16SMatt Jacob 	}
27492df76c16SMatt Jacob }
27502df76c16SMatt Jacob 
27512df76c16SMatt Jacob static void
27528290ea90SAlexander Motin isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
27532df76c16SMatt Jacob {
27548290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
27558290ea90SAlexander Motin 	inot_private_data_t *ntp, *tmp;
27568290ea90SAlexander Motin 	uint32_t this_tag_id;
27572df76c16SMatt Jacob 
27582df76c16SMatt Jacob 	/*
27592df76c16SMatt Jacob 	 * First, clean any commands pending restart
27602df76c16SMatt Jacob 	 */
27618290ea90SAlexander Motin 	STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
27628290ea90SAlexander Motin 		if (IS_24XX(isp))
27638290ea90SAlexander Motin 			this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
27648290ea90SAlexander Motin 		else
27658290ea90SAlexander Motin 			this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
27662df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
27678290ea90SAlexander Motin 			isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
27688290ea90SAlexander Motin 			    ECMD_TERMINATE, 0);
27698290ea90SAlexander Motin 			isp_put_ntpd(isp, chan, ntp);
27708290ea90SAlexander Motin 			STAILQ_REMOVE(&tptr->restart_queue, ntp,
27718290ea90SAlexander Motin 			    inot_private_data, next);
27722df76c16SMatt Jacob 		}
27732df76c16SMatt Jacob 	}
27742df76c16SMatt Jacob 
27752df76c16SMatt Jacob 	/*
27762df76c16SMatt Jacob 	 * Now mark other ones dead as well.
27772df76c16SMatt Jacob 	 */
27788290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
27798290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
27808290ea90SAlexander Motin 		if (atp->lun != tptr->ts_lun)
27818290ea90SAlexander Motin 			continue;
27828290ea90SAlexander Motin 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
27832df76c16SMatt Jacob 			atp->dead = 1;
27842df76c16SMatt Jacob 	}
27852df76c16SMatt Jacob }
2786d81ba9d5SMatt Jacob #endif
2787d81ba9d5SMatt Jacob 
2788478f8a96SJustin T. Gibbs static void
27891dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2790478f8a96SJustin T. Gibbs {
2791478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
2792e95725cbSMatt Jacob 	int bus, tgt;
27939cd7268eSMatt Jacob 	ispsoftc_t *isp;
2794478f8a96SJustin T. Gibbs 
2795478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
27969cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
2797e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
2798e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
2799e95725cbSMatt Jacob 
2800478f8a96SJustin T. Gibbs 	switch (code) {
2801478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
2802ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
28031dae40ebSMatt Jacob 			uint16_t oflags, nflags;
28042df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
2805478f8a96SJustin T. Gibbs 
280641ed683eSMatt Jacob 			if (tgt >= 0) {
28079ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
28089ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
2809a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
2810478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
2811478f8a96SJustin T. Gibbs 				}
28129ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
28139ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
2814478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
28152df76c16SMatt Jacob 				sdp->update = 1;
28162df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
28179ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
2818478f8a96SJustin T. Gibbs 			}
281941ed683eSMatt Jacob 		}
2820478f8a96SJustin T. Gibbs 		break;
2821478f8a96SJustin T. Gibbs 	default:
28223c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2823478f8a96SJustin T. Gibbs 		break;
2824478f8a96SJustin T. Gibbs 	}
2825478f8a96SJustin T. Gibbs }
2826478f8a96SJustin T. Gibbs 
2827478f8a96SJustin T. Gibbs static void
2828c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
2829478f8a96SJustin T. Gibbs {
28309cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
28316ce548a1SAlexander Motin 	uint16_t isr, sema, info;
2832126ec864SMatt Jacob 
28336ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
28346ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
2835478f8a96SJustin T. Gibbs }
2836478f8a96SJustin T. Gibbs 
2837ab6c4b31SMatt Jacob 
28382df76c16SMatt Jacob static void
28392df76c16SMatt Jacob isp_watchdog(void *arg)
2840cc8df88bSMatt Jacob {
28412df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
28422df76c16SMatt Jacob 	ispsoftc_t *isp;
2843e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
2844b85389e1SMatt Jacob 
28452df76c16SMatt Jacob 	isp = XS_ISP(xs);
28462df76c16SMatt Jacob 
2847cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
2848e95725cbSMatt Jacob 
2849e95725cbSMatt Jacob 	/*
2850e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2851e95725cbSMatt Jacob 	 */
2852e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
28536ce548a1SAlexander Motin 		uint16_t isr, sema, info;
28546ce548a1SAlexander Motin 		if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
28556ce548a1SAlexander Motin 			isp_intr(isp, isr, sema, info);
2856e95725cbSMatt Jacob 		ohandle = handle;
2857e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
2858e95725cbSMatt Jacob 	}
2859c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
28601fcf5debSMatt Jacob 		/*
2861c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
2862c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
2863c8b8a2c4SMatt Jacob 		 *
2864c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
2865c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
2866c8b8a2c4SMatt Jacob 		 * back separately.
28671fcf5debSMatt Jacob 		 */
2868c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2869c8b8a2c4SMatt Jacob 			return;
2870c8b8a2c4SMatt Jacob 		}
28711fcf5debSMatt Jacob 
28721fcf5debSMatt Jacob 		/*
2873c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
2874c8b8a2c4SMatt Jacob 		 * fact have been completed.
2875c8b8a2c4SMatt Jacob 		 */
2876c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
2877c8b8a2c4SMatt Jacob 
2878c8b8a2c4SMatt Jacob 		/*
2879c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
2880c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
2881c8b8a2c4SMatt Jacob 		 */
2882c8b8a2c4SMatt Jacob 		if (xs == NULL) {
2883c8b8a2c4SMatt Jacob 			return;
2884c8b8a2c4SMatt Jacob 		}
2885c8b8a2c4SMatt Jacob 
2886c8b8a2c4SMatt Jacob 		/*
2887c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
28881fcf5debSMatt Jacob 		 */
2889f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
2890f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
2891f6e75de2SMatt Jacob 		}
2892cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
2893c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2894387d8239SMatt Jacob 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
2895387d8239SMatt Jacob 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
2896e95725cbSMatt Jacob 		isp_prt_endcmd(isp, xs);
2897cc8df88bSMatt Jacob 		isp_done(xs);
2898e95725cbSMatt Jacob 	} else {
2899e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
2900e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2901e95725cbSMatt Jacob 		} else {
2902e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2903e95725cbSMatt Jacob 		}
2904b85389e1SMatt Jacob 	}
2905f7c631bcSMatt Jacob }
2906f7c631bcSMatt Jacob 
2907f7c631bcSMatt Jacob static void
29085704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2909f7c631bcSMatt Jacob {
2910ffcf6651SMatt Jacob 	union ccb *ccb;
29112df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
29122df76c16SMatt Jacob 
2913ffcf6651SMatt Jacob 	/*
29140e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2915ffcf6651SMatt Jacob 	 */
29168008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
2917ffcf6651SMatt Jacob 	if (ccb == NULL) {
29182df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2919ffcf6651SMatt Jacob 		return;
2920ffcf6651SMatt Jacob 	}
2921e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2922e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2923ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2924ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
2925ffcf6651SMatt Jacob 		return;
2926ffcf6651SMatt Jacob 	}
2927ffcf6651SMatt Jacob 	xpt_rescan(ccb);
2928ffcf6651SMatt Jacob }
2929ffcf6651SMatt Jacob 
2930ffcf6651SMatt Jacob static void
29315704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2932ffcf6651SMatt Jacob {
2933ffcf6651SMatt Jacob 	struct cam_path *tp;
29342df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
29352df76c16SMatt Jacob 
29362df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2937ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
2938ffcf6651SMatt Jacob 		xpt_free_path(tp);
2939f7c631bcSMatt Jacob 	}
2940f7c631bcSMatt Jacob }
2941f7c631bcSMatt Jacob 
2942f7c631bcSMatt Jacob /*
2943f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
2944f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
2945f7c631bcSMatt Jacob  * gone away.
2946f7c631bcSMatt Jacob  *
2947f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
2948f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
2949f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
2950f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
2951f7c631bcSMatt Jacob  */
2952f7c631bcSMatt Jacob static void
2953f7c631bcSMatt Jacob isp_gdt(void *arg)
2954f7c631bcSMatt Jacob {
29552df76c16SMatt Jacob 	struct isp_fc *fc = arg;
2956de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2957de461933SMatt Jacob }
2958de461933SMatt Jacob 
2959de461933SMatt Jacob static void
2960de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
2961de461933SMatt Jacob {
2962de461933SMatt Jacob 	struct isp_fc *fc = arg;
29632df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
29642df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
2965f7c631bcSMatt Jacob 	fcportdb_t *lp;
2966e68eef14SAlexander Motin 	struct ac_contract ac;
2967e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2968766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
2969f7c631bcSMatt Jacob 
2970de461933SMatt Jacob 	ISP_LOCK(isp);
2971de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2972f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
29732df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
2974f7c631bcSMatt Jacob 
2975f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2976f7c631bcSMatt Jacob 			continue;
2977f7c631bcSMatt Jacob 		}
2978427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
2979427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
2980f7c631bcSMatt Jacob 			more_to_do++;
2981f7c631bcSMatt Jacob 			continue;
2982f7c631bcSMatt Jacob 		}
2983e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2984e68eef14SAlexander Motin 		if (lp->is_target) {
2985e68eef14SAlexander Motin 			lp->is_target = 0;
2986766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2987f7c631bcSMatt Jacob 		}
2988e68eef14SAlexander Motin 		if (lp->is_initiator) {
2989e68eef14SAlexander Motin 			lp->is_initiator = 0;
2990e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2991e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2992e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2993e68eef14SAlexander Motin 			adc->port = lp->portid;
2994d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2995e68eef14SAlexander Motin 			adc->arrived = 0;
2996e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2997e68eef14SAlexander Motin 		}
2998e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
2999e68eef14SAlexander Motin 	}
3000a01f5aebSMatt Jacob 	if (fc->ready) {
3001f7c631bcSMatt Jacob 		if (more_to_do) {
30022df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
3003f7c631bcSMatt Jacob 		} else {
3004de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
3005387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3006a01f5aebSMatt Jacob 		}
3007f7c631bcSMatt Jacob 	}
3008de461933SMatt Jacob 	ISP_UNLOCK(isp);
3009f7c631bcSMatt Jacob }
3010f7c631bcSMatt Jacob 
3011f7c631bcSMatt Jacob /*
3012e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
3013e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
3014e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
3015f7c631bcSMatt Jacob  *
3016f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
3017f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
3018f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3019f7c631bcSMatt Jacob  */
3020f7c631bcSMatt Jacob static void
3021e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
3022f7c631bcSMatt Jacob {
3023e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3024e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3025e561aa79SAlexander Motin 
3026e561aa79SAlexander Motin 	if (fc->loop_down_time)
3027e561aa79SAlexander Motin 		return;
3028e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3029e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
3030e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
3031e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
3032e561aa79SAlexander Motin 	wakeup(fc);
3033de461933SMatt Jacob }
3034de461933SMatt Jacob 
3035de461933SMatt Jacob static void
3036e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
3037de461933SMatt Jacob {
3038e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3039e561aa79SAlexander Motin 
3040e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3041e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
3042e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3043e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
3044e561aa79SAlexander Motin }
3045e561aa79SAlexander Motin 
3046e561aa79SAlexander Motin static void
3047e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
3048e561aa79SAlexander Motin {
3049e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3050e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3051f7c631bcSMatt Jacob 	fcportdb_t *lp;
3052e68eef14SAlexander Motin 	struct ac_contract ac;
3053e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3054766a65a5SAlexander Motin 	int dbidx, i;
3055f7c631bcSMatt Jacob 
3056e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
30570a70657fSMatt Jacob 
3058f7c631bcSMatt Jacob 	/*
3059f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
3060f7c631bcSMatt Jacob 	 */
3061f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3062e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
3063f7c631bcSMatt Jacob 
3064e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
3065f7c631bcSMatt Jacob 			continue;
3066f7c631bcSMatt Jacob 
3067f7c631bcSMatt Jacob 		/*
3068f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3069f7c631bcSMatt Jacob 		 */
3070e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
3071e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
3072e95725cbSMatt Jacob 
3073970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3074e95725cbSMatt Jacob 				continue;
3075e95725cbSMatt Jacob 			}
3076e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3077e95725cbSMatt Jacob 				continue;
3078e95725cbSMatt Jacob                         }
3079766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
3080e95725cbSMatt Jacob 				continue;
3081e95725cbSMatt Jacob 			}
30826af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
30836af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
30846af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
3085e95725cbSMatt Jacob 		}
3086e95725cbSMatt Jacob 
3087e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3088e68eef14SAlexander Motin 		if (lp->is_target) {
3089e68eef14SAlexander Motin 			lp->is_target = 0;
3090766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3091f7c631bcSMatt Jacob 		}
3092e68eef14SAlexander Motin 		if (lp->is_initiator) {
3093e68eef14SAlexander Motin 			lp->is_initiator = 0;
3094e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3095e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3096e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3097e68eef14SAlexander Motin 			adc->port = lp->portid;
3098d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3099e68eef14SAlexander Motin 			adc->arrived = 0;
3100e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3101e95725cbSMatt Jacob 		}
3102e68eef14SAlexander Motin 	}
3103e68eef14SAlexander Motin 
3104e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
3105e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3106cc8df88bSMatt Jacob }
3107cc8df88bSMatt Jacob 
31085d571944SMatt Jacob static void
31095d571944SMatt Jacob isp_kthread(void *arg)
31105d571944SMatt Jacob {
31112df76c16SMatt Jacob 	struct isp_fc *fc = arg;
31122df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
31132df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3114e561aa79SAlexander Motin 	int slp = 0, d;
3115e561aa79SAlexander Motin 	int lb, lim;
3116a01f5aebSMatt Jacob 
31170a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
31182df76c16SMatt Jacob 
31191d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
3120e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3121e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
31222df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
3123e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3124e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
3125e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
31262df76c16SMatt Jacob 
31272df76c16SMatt Jacob 		/*
31282df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
31292df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
31302df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
31312df76c16SMatt Jacob 		 * check again.
31322df76c16SMatt Jacob 		 *
31332df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
31342df76c16SMatt Jacob 		 */
3135e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
3136e561aa79SAlexander Motin 			slp = 0;
313710365e5aSMatt Jacob 		} else {
313810365e5aSMatt Jacob 			/*
313910365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
3140f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
3141f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
3142f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
314310365e5aSMatt Jacob 			 */
3144e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
3145f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
3146e561aa79SAlexander Motin 			else
31472df76c16SMatt Jacob 				lim = fc->loop_down_limit;
3148e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
3149e561aa79SAlexander Motin 			if (d >= lim)
3150f7c631bcSMatt Jacob 				slp = 0;
3151e561aa79SAlexander Motin 			else if (d < 10)
315210365e5aSMatt Jacob 				slp = 1;
3153e561aa79SAlexander Motin 			else if (d < 30)
3154f7c631bcSMatt Jacob 				slp = 5;
3155e561aa79SAlexander Motin 			else if (d < 60)
3156f7c631bcSMatt Jacob 				slp = 10;
3157e561aa79SAlexander Motin 			else if (d < 120)
3158f7c631bcSMatt Jacob 				slp = 20;
3159e561aa79SAlexander Motin 			else
3160f7c631bcSMatt Jacob 				slp = 30;
3161f44257c2SMatt Jacob 		}
316210365e5aSMatt Jacob 
3163e561aa79SAlexander Motin 		if (slp == 0) {
3164e561aa79SAlexander Motin 			if (lb == LOOP_READY)
3165e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
3166387d8239SMatt Jacob 			else
3167e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
31685d571944SMatt Jacob 		}
3169fdeb9f2fSMatt Jacob 
3170e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3171e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
31722df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
31735d571944SMatt Jacob 	}
31741d0a1de2SWill Andrews 	fc->num_threads -= 1;
31750a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
31761d0a1de2SWill Andrews 	kthread_exit();
31775d571944SMatt Jacob }
31785d571944SMatt Jacob 
31798290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
31808290ea90SAlexander Motin static void
31818290ea90SAlexander Motin isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
31828290ea90SAlexander Motin {
31838290ea90SAlexander Motin 	atio_private_data_t *atp;
31848290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
31858290ea90SAlexander Motin 	struct ccb_hdr *sccb;
31868290ea90SAlexander Motin 	tstate_t *tptr;
31878290ea90SAlexander Motin 
31888290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
31898290ea90SAlexander Motin 	if (tptr != NULL) {
31908290ea90SAlexander Motin 		/* Search for the ATIO among queueued. */
31918290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
31928290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
31938290ea90SAlexander Motin 				continue;
31948290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
31958290ea90SAlexander Motin 			tptr->atio_count--;
31968290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
31978290ea90SAlexander Motin 			xpt_done(accb);
31988290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
31998290ea90SAlexander Motin 			return;
32008290ea90SAlexander Motin 		}
32018290ea90SAlexander Motin 	}
32028290ea90SAlexander Motin 
32038290ea90SAlexander Motin 	/* Search for the ATIO among running. */
32048290ea90SAlexander Motin 	atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
32058290ea90SAlexander Motin 	if (atp != NULL) {
32063f072d69SAlexander Motin 		/* Send TERMINATE to firmware. */
32073f072d69SAlexander Motin 		if (!atp->dead && IS_24XX(isp)) {
32083f072d69SAlexander Motin 			uint8_t storage[QENTRY_LEN];
32093f072d69SAlexander Motin 			ct7_entry_t *cto = (ct7_entry_t *) storage;
32103f072d69SAlexander Motin 
32113f072d69SAlexander Motin 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
32123f072d69SAlexander Motin 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
32133f072d69SAlexander Motin 			cto->ct_header.rqs_entry_count = 1;
32143f072d69SAlexander Motin 			cto->ct_nphdl = atp->nphdl;
32153f072d69SAlexander Motin 			cto->ct_rxid = atp->tag;
32163f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
32173f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
32183f072d69SAlexander Motin 			cto->ct_oxid = atp->oxid;
32193f072d69SAlexander Motin 			cto->ct_vpidx = XS_CHANNEL(accb);
32203f072d69SAlexander Motin 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
32213f072d69SAlexander Motin 			isp_target_put_entry(isp, cto);
32223f072d69SAlexander Motin 		}
32238290ea90SAlexander Motin 		isp_put_atpd(isp, XS_CHANNEL(accb), atp);
32248290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
32258290ea90SAlexander Motin 	} else {
32268290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
32278290ea90SAlexander Motin 	}
32288290ea90SAlexander Motin }
32298290ea90SAlexander Motin 
32308290ea90SAlexander Motin static void
32318290ea90SAlexander Motin isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
32328290ea90SAlexander Motin {
32338290ea90SAlexander Motin 	inot_private_data_t *ntp;
32348290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
32358290ea90SAlexander Motin 	struct ccb_hdr *sccb;
32368290ea90SAlexander Motin 	tstate_t *tptr;
32378290ea90SAlexander Motin 
32388290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
32398290ea90SAlexander Motin 	if (tptr != NULL) {
32408290ea90SAlexander Motin 		/* Search for the INOT among queueued. */
32418290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
32428290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
32438290ea90SAlexander Motin 				continue;
32448290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
32458290ea90SAlexander Motin 			tptr->inot_count--;
32468290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
32478290ea90SAlexander Motin 			xpt_done(accb);
32488290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
32498290ea90SAlexander Motin 			return;
32508290ea90SAlexander Motin 		}
32518290ea90SAlexander Motin 	}
32528290ea90SAlexander Motin 
32538290ea90SAlexander Motin 	/* Search for the INOT among running. */
32548290ea90SAlexander Motin 	ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
32558290ea90SAlexander Motin 	if (ntp != NULL) {
32568290ea90SAlexander Motin 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, ntp->data);
32578290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
32588290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
32598290ea90SAlexander Motin 	} else {
32608290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
32618290ea90SAlexander Motin 		return;
32628290ea90SAlexander Motin 	}
32638290ea90SAlexander Motin }
32648290ea90SAlexander Motin #endif
32658290ea90SAlexander Motin 
3266cc8df88bSMatt Jacob static void
3267c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
3268478f8a96SJustin T. Gibbs {
3269e561aa79SAlexander Motin 	int bus, tgt, ts, error;
32709cd7268eSMatt Jacob 	ispsoftc_t *isp;
32714663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
3272478f8a96SJustin T. Gibbs 
3273478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3274478f8a96SJustin T. Gibbs 
32759cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
32762df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
32775cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
32785cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
32792df76c16SMatt Jacob 
3280478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
3281478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
32822df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
3283478f8a96SJustin T. Gibbs 		/*
3284478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
3285478f8a96SJustin T. Gibbs 		 */
3286478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3287478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3288478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
32895cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3290478f8a96SJustin T. Gibbs 				break;
3291478f8a96SJustin T. Gibbs 			}
3292478f8a96SJustin T. Gibbs 		}
3293387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
32940470d791SMatt Jacob #ifdef	DIAGNOSTIC
32956af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3296dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3297478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
32986af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
32996af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3300dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3301478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3302478f8a96SJustin T. Gibbs 		}
3303478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3304478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3305478f8a96SJustin T. Gibbs 			break;
3306478f8a96SJustin T. Gibbs 		}
33070470d791SMatt Jacob #endif
33080a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
33090a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
33100a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
33110a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
33122df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
33135cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
33140a70657fSMatt Jacob 			xpt_done(ccb);
33150a70657fSMatt Jacob 			break;
33160a70657fSMatt Jacob 		}
3317b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
33180470d791SMatt Jacob 		switch (error) {
3319478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3320478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
33210a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
33220a70657fSMatt Jacob 				break;
3323d69a5f7dSMatt Jacob 			}
33240a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
33250a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
33260a70657fSMatt Jacob 				ts = 60*1000;
3327cc8df88bSMatt Jacob 			}
33280a70657fSMatt Jacob 			ts = isp_mstohz(ts);
33292df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3330478f8a96SJustin T. Gibbs 			break;
33310470d791SMatt Jacob 		case CMD_RQLATER:
33326af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
33336af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3334f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
33352df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3336387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
33370a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3338478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3339478f8a96SJustin T. Gibbs 			break;
33400470d791SMatt Jacob 		case CMD_EAGAIN:
33410a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
33422df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
33432df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3344387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3345478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3346478f8a96SJustin T. Gibbs 			break;
33470470d791SMatt Jacob 		case CMD_COMPLETE:
33480470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
33490470d791SMatt Jacob 			break;
33500470d791SMatt Jacob 		default:
33512df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3352387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
33530a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
33540470d791SMatt Jacob 			xpt_done(ccb);
3355478f8a96SJustin T. Gibbs 		}
3356478f8a96SJustin T. Gibbs 		break;
3357478f8a96SJustin T. Gibbs 
3358d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
33592df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
33602df76c16SMatt Jacob 		if (ccb->cel.enable) {
33612df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
33622df76c16SMatt Jacob 		} else {
33632df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
336467ff51f1SMatt Jacob 		}
336567ff51f1SMatt Jacob 		break;
33662df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3367d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3368d81ba9d5SMatt Jacob 	{
33692df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3370d81ba9d5SMatt Jacob 		if (tptr == NULL) {
33712df76c16SMatt Jacob 			const char *str;
33722df76c16SMatt Jacob 
33738290ea90SAlexander Motin 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
33742df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
33758290ea90SAlexander Motin 			else
33762df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
33778290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
33785c0b756aSAlexander Motin 			    "%s: no state pointer found for %s\n",
33798290ea90SAlexander Motin 			    __func__, str);
33802df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
33815c0b756aSAlexander Motin 			xpt_done(ccb);
3382d81ba9d5SMatt Jacob 			break;
3383d81ba9d5SMatt Jacob 		}
3384387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3385387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3386570c7a3fSMatt Jacob 
3387d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
33888290ea90SAlexander Motin 			ccb->atio.tag_id = 0;
3389570c7a3fSMatt Jacob 			tptr->atio_count++;
33902df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
33918290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
33928290ea90SAlexander Motin 			    "Put FREE ATIO, count now %d\n", tptr->atio_count);
33932df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
33948290ea90SAlexander Motin 			ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3395746e9c85SMatt Jacob 			tptr->inot_count++;
33962df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
33978290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
33988290ea90SAlexander Motin 			    "Put FREE INOT, count now %d\n", tptr->inot_count);
3399d81ba9d5SMatt Jacob 		}
3400d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3401d81ba9d5SMatt Jacob 		break;
3402d81ba9d5SMatt Jacob 	}
34032df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3404d81ba9d5SMatt Jacob 	{
34052df76c16SMatt Jacob 		inot_private_data_t *ntp;
34062df76c16SMatt Jacob 
34072df76c16SMatt Jacob 		/*
34082df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
34092df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
34102df76c16SMatt Jacob 		 */
34112df76c16SMatt Jacob 		/*
34122df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
34132df76c16SMatt Jacob 		 */
34142df76c16SMatt 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);
34158290ea90SAlexander Motin 		ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
34162df76c16SMatt Jacob 		if (ntp == NULL) {
34172df76c16SMatt 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__,
34182df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
34192df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
34202df76c16SMatt Jacob 			xpt_done(ccb);
3421d81ba9d5SMatt Jacob 			break;
3422d81ba9d5SMatt Jacob 		}
34238290ea90SAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
342496b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
34252df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
34262df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3427387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3428387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
34292df76c16SMatt Jacob 			break;
34302df76c16SMatt Jacob 		}
34318290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
34322df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
34332df76c16SMatt 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);
34342df76c16SMatt Jacob 		xpt_done(ccb);
34352df76c16SMatt Jacob 		break;
34362df76c16SMatt Jacob 	}
34372df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3438387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
34392df76c16SMatt Jacob 		break;
3440d81ba9d5SMatt Jacob #endif
3441478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3442d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3443d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3444d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3445d81ba9d5SMatt Jacob 
34462df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3447478f8a96SJustin T. Gibbs 		if (error) {
3448478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3449478f8a96SJustin T. Gibbs 		} else {
3450b44e442eSWill Andrews 			/*
3451b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3452b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3453b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3454b44e442eSWill Andrews 			 */
345521daf914SAlexander Motin 			if (IS_FC(isp))
345621daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3457b44e442eSWill Andrews 
3458478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3459478f8a96SJustin T. Gibbs 		}
3460478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3461478f8a96SJustin T. Gibbs 		break;
3462478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3463d81ba9d5SMatt Jacob 	{
3464d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3465d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3466d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3467d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
34688290ea90SAlexander Motin 			isp_abort_atio(isp, ccb);
34698290ea90SAlexander Motin 			break;
34708290ea90SAlexander Motin 		case XPT_IMMEDIATE_NOTIFY:
34718290ea90SAlexander Motin 			isp_abort_inot(isp, ccb);
3472d81ba9d5SMatt Jacob 			break;
3473d81ba9d5SMatt Jacob #endif
3474d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3475950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3476478f8a96SJustin T. Gibbs 			if (error) {
3477d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3478478f8a96SJustin T. Gibbs 			} else {
3479478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3480478f8a96SJustin T. Gibbs 			}
3481d81ba9d5SMatt Jacob 			break;
3482d81ba9d5SMatt Jacob 		default:
3483d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3484d81ba9d5SMatt Jacob 			break;
3485d81ba9d5SMatt Jacob 		}
34861c0a1eb2SMatt Jacob 		/*
34871c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
34881c0a1eb2SMatt Jacob 		 * complete when control is returned.
34891c0a1eb2SMatt Jacob 		 */
3490478f8a96SJustin T. Gibbs 		break;
3491d81ba9d5SMatt Jacob 	}
3492ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3493478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3494478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
34959ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
34969ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
34979ce9bdafSMatt Jacob 			xpt_done(ccb);
34989ce9bdafSMatt Jacob 			break;
34999ce9bdafSMatt Jacob 		}
3500478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3501805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
35022df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
35032df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
35042df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
35052df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
35061dae40ebSMatt Jacob 			uint16_t *dptr;
3507ab163f5fSMatt Jacob 
3508b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3509b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3510b61386a4SMatt Jacob 				xpt_done(ccb);
3511b61386a4SMatt Jacob 				break;
3512b61386a4SMatt Jacob 			}
3513b61386a4SMatt Jacob 
3514ab163f5fSMatt Jacob 			/*
35159ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3516ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3517ab163f5fSMatt Jacob 			 * vectored to that location.
3518ab163f5fSMatt Jacob 			 */
35199ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3520ab163f5fSMatt Jacob 
3521ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3522ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3523ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3524ab163f5fSMatt Jacob 				else
3525ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3526ab163f5fSMatt Jacob 			}
3527ab163f5fSMatt Jacob 
3528ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3529ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3530ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3531ab163f5fSMatt Jacob 				else
3532ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3533ab163f5fSMatt Jacob 			}
3534ab163f5fSMatt Jacob 
3535ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3536ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3537ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3538ab163f5fSMatt Jacob 				else
3539ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3540ab163f5fSMatt Jacob 			}
3541ab163f5fSMatt Jacob 
3542ab163f5fSMatt Jacob 			/*
3543ab163f5fSMatt Jacob 			 * XXX: FIX ME
3544ab163f5fSMatt Jacob 			 */
35452df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3546ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
35479ce9bdafSMatt Jacob 				/*
35489ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
35499ce9bdafSMatt Jacob 				 */
35502df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
35512df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3552ab163f5fSMatt Jacob 			} else {
3553ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3554ab163f5fSMatt Jacob 			}
3555123055f0SNathan 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,
35562df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3557478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
35582df76c16SMatt Jacob 			sdp->update = 1;
3559478f8a96SJustin T. Gibbs 		}
3560478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3561478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3562478f8a96SJustin T. Gibbs 		break;
3563478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3564478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3565478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
35662df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3567ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
35682df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
35692df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
35702df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3571478f8a96SJustin T. Gibbs 
3572ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3573ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3574ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3575ab163f5fSMatt Jacob 			cts->transport_version = 0;
3576ab163f5fSMatt Jacob 
35779b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
35789b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3579ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3580ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3581fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3582766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3583766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3584ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3585ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3586ab163f5fSMatt Jacob 				fc->port = lp->portid;
35872df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3588ab163f5fSMatt Jacob 			}
3589ab163f5fSMatt Jacob 		} else {
35902df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
35912df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
35922df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
35931dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3594ab163f5fSMatt Jacob 
3595ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
359683ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
35972df76c16SMatt Jacob 				sdp->update = 1;
35982df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
35999ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
36009ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
36019ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
36024394c92fSMatt Jacob 			} else {
36039ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
36049ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
36059ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
36064394c92fSMatt Jacob 			}
3607478f8a96SJustin T. Gibbs 
3608ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3609ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3610ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3611ab163f5fSMatt Jacob 			cts->transport_version = 2;
3612ab163f5fSMatt Jacob 
3613b61386a4SMatt Jacob 			spi->valid = 0;
3614b61386a4SMatt Jacob 			scsi->valid = 0;
3615b61386a4SMatt Jacob 			spi->flags = 0;
3616b61386a4SMatt Jacob 			scsi->flags = 0;
3617ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3618ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3619ab163f5fSMatt Jacob 			}
36209ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3621ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3622ab163f5fSMatt Jacob 				spi->sync_period = pval;
3623b61386a4SMatt Jacob 			} else {
3624b61386a4SMatt Jacob 				spi->sync_offset = 0;
3625b61386a4SMatt Jacob 				spi->sync_period = 0;
3626b61386a4SMatt Jacob 			}
3627ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3628ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3629ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3630ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3631ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3632ab163f5fSMatt Jacob 			} else {
3633ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3634ab163f5fSMatt Jacob 			}
3635ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3636ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3637b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3638b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3639b61386a4SMatt Jacob 				}
3640ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3641ab163f5fSMatt Jacob 			}
3642123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3643123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3644478f8a96SJustin T. Gibbs 		}
3645478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3646478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3647478f8a96SJustin T. Gibbs 		break;
3648478f8a96SJustin T. Gibbs 
3649478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
36502df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
36512df76c16SMatt Jacob 		xpt_done(ccb);
36522df76c16SMatt Jacob 		break;
3653478f8a96SJustin T. Gibbs 
3654478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
3655ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
36562df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
36572df76c16SMatt Jacob 		if (error) {
3658478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
36592df76c16SMatt Jacob 			xpt_done(ccb);
36602df76c16SMatt Jacob 			break;
36612df76c16SMatt Jacob 		}
36620a70657fSMatt Jacob 		if (bootverbose) {
36632df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
36640a70657fSMatt Jacob 		}
36652df76c16SMatt Jacob 		if (IS_FC(isp)) {
36662df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
36672df76c16SMatt Jacob 		} else {
36682df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
36692df76c16SMatt Jacob 		}
3670478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3671478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3672478f8a96SJustin T. Gibbs 		break;
3673478f8a96SJustin T. Gibbs 
3674478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3675478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3676478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3677478f8a96SJustin T. Gibbs 		break;
3678478f8a96SJustin T. Gibbs 
36792df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
36802df76c16SMatt Jacob 	{
36812df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
36822df76c16SMatt Jacob 		fcparam *fcp;
36832df76c16SMatt Jacob 
36842df76c16SMatt Jacob 		if (!IS_FC(isp)) {
36852df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
36862df76c16SMatt Jacob 			xpt_done(ccb);
36872df76c16SMatt Jacob 			break;
36882df76c16SMatt Jacob 		}
36892df76c16SMatt Jacob 
36902df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
36912df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
36922df76c16SMatt Jacob 
36932df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
36942df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
36952df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
36962df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
36972df76c16SMatt Jacob 		}
36982df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
36992df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
37002df76c16SMatt Jacob 			int rchange = 0;
37012df76c16SMatt Jacob 			int newrole = 0;
37022df76c16SMatt Jacob 
37032df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
37042df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
37052df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
37062df76c16SMatt Jacob 					rchange = 1;
37072df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
37082df76c16SMatt Jacob 				}
37092df76c16SMatt Jacob 				break;
37102df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
37112df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
37122df76c16SMatt Jacob 					rchange = 1;
37132df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
37142df76c16SMatt Jacob 				}
37152df76c16SMatt Jacob 				break;
37162df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
37172df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
37182df76c16SMatt Jacob 					rchange = 1;
37192df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
37202df76c16SMatt Jacob 				}
37212df76c16SMatt Jacob 				break;
37222df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
37232df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
37242df76c16SMatt Jacob 					rchange = 1;
37252df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
37262df76c16SMatt Jacob 				}
37272df76c16SMatt Jacob 				break;
37282df76c16SMatt Jacob 			}
37292df76c16SMatt Jacob 			if (rchange) {
3730e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
37316bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
37326bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
37332df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3734387d8239SMatt Jacob 					xpt_done(ccb);
3735387d8239SMatt Jacob 					break;
37362df76c16SMatt Jacob 				}
37372df76c16SMatt Jacob 			}
37382df76c16SMatt Jacob 		}
37392df76c16SMatt Jacob 		xpt_done(ccb);
37402df76c16SMatt Jacob 		break;
37412df76c16SMatt Jacob 	}
3742a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3743e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
37442df76c16SMatt Jacob 	{
37452df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
37462df76c16SMatt Jacob 
37472df76c16SMatt Jacob 		if (IS_FC(isp)) {
37482df76c16SMatt Jacob 			fcparam *fcp;
37492df76c16SMatt Jacob 
37502df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
37512df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
37522df76c16SMatt Jacob 
37532df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
37542df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
37552df76c16SMatt Jacob 			switch (fcp->role) {
37562df76c16SMatt Jacob 			case ISP_ROLE_NONE:
37572df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
37582df76c16SMatt Jacob 				break;
37592df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
37602df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
37612df76c16SMatt Jacob 				break;
37622df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
37632df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
37642df76c16SMatt Jacob 				break;
37652df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
37662df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
37672df76c16SMatt Jacob 				break;
37682df76c16SMatt Jacob 			}
37692df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
37702df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
37712df76c16SMatt Jacob 		} else {
37722df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
37732df76c16SMatt Jacob 		}
37742df76c16SMatt Jacob 		xpt_done(ccb);
37752df76c16SMatt Jacob 		break;
37762df76c16SMatt Jacob 	}
3777478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
3778478f8a96SJustin T. Gibbs 	{
3779478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
3780478f8a96SJustin T. Gibbs 
3781478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
3782d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
37833e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3784a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
37853e6deb33SAlexander Motin 		else
3786d81ba9d5SMatt Jacob #endif
37873e6deb33SAlexander Motin 			cpi->target_sprt = 0;
3788478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
37890470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
37906af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
37916af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
37920470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
37937bf825d1SKenneth D. Merry 		if (isp->isp_osinfo.sixtyfourbit)
37947bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
37957bf825d1SKenneth D. Merry 		else
37967bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
37977bf825d1SKenneth D. Merry 
37982df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
37994394c92fSMatt Jacob 		if (IS_FC(isp)) {
38002df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
38012df76c16SMatt Jacob 
3802ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3803229203afSAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
38042df76c16SMatt Jacob 
38050470d791SMatt Jacob 			/*
38060470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
38070470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
38080470d791SMatt Jacob 			 */
38090470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
38100470d791SMatt Jacob 
38119deea857SKenneth D. Merry 			/*
38122df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
38139deea857SKenneth D. Merry 			 */
3814e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
3815e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
3816e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
38172df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
38182df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
38192df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
38202df76c16SMatt Jacob 			} else {
38212df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
38222df76c16SMatt Jacob 			}
38230470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
3824ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
3825805e1f82SMatt Jacob 			cpi->transport_version = 0;
38262df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
38272df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
38282df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
38292df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3830478f8a96SJustin T. Gibbs 		} else {
38312df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
38320470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3833ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
3834ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
38359deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
3836ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
3837805e1f82SMatt Jacob 			cpi->transport_version = 2;
3838478f8a96SJustin T. Gibbs 		}
3839ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
3840ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
38414195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
38424195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
38434195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3844478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
3845478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
3846478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3847478f8a96SJustin T. Gibbs 		break;
3848478f8a96SJustin T. Gibbs 	}
3849478f8a96SJustin T. Gibbs 	default:
3850478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3851478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3852478f8a96SJustin T. Gibbs 		break;
3853478f8a96SJustin T. Gibbs 	}
3854478f8a96SJustin T. Gibbs }
3855d3a9eb2eSMatt Jacob 
3856d3a9eb2eSMatt Jacob void
38572df76c16SMatt Jacob isp_done(XS_T *sccb)
3858d3a9eb2eSMatt Jacob {
38599cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
3860e95725cbSMatt Jacob 	uint32_t status;
3861d3a9eb2eSMatt Jacob 
3862d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
3863d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
3864b85389e1SMatt Jacob 
38652df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3866d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
38672df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
386892a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
386992a1e549SMatt Jacob 		} else {
3870d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3871d3a9eb2eSMatt Jacob 		}
387292a1e549SMatt Jacob 	}
3873b85389e1SMatt Jacob 
38740470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3875e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
3876ab23521aSAlexander Motin 	if (status != CAM_REQ_CMP &&
3877ab23521aSAlexander Motin 	    (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3878d3a9eb2eSMatt Jacob 		sccb->ccb_h.status |= CAM_DEV_QFRZN;
38790470d791SMatt Jacob 		xpt_freeze_devq(sccb->ccb_h.path, 1);
3880d3a9eb2eSMatt Jacob 	}
3881b85389e1SMatt Jacob 
38825cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
3883387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
38840ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
38850a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
38865cbe3e8eSAlexander Motin 	}
3887d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
3888d3a9eb2eSMatt Jacob }
3889d3a9eb2eSMatt Jacob 
38902df76c16SMatt Jacob void
38912df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3892cbf57b47SMatt Jacob {
3893e68eef14SAlexander Motin 	int bus;
3894e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3895387d8239SMatt Jacob 	char buf[64];
3896f7c631bcSMatt Jacob 	char *msg = NULL;
389710365e5aSMatt Jacob 	target_id_t tgt;
389810365e5aSMatt Jacob 	fcportdb_t *lp;
3899a01f5aebSMatt Jacob 	struct isp_fc *fc;
390010365e5aSMatt Jacob 	struct cam_path *tmppath;
3901e68eef14SAlexander Motin 	struct ac_contract ac;
3902e68eef14SAlexander Motin 	struct ac_device_changed *adc;
39032df76c16SMatt Jacob 	va_list ap;
390410365e5aSMatt Jacob 
3905cbf57b47SMatt Jacob 	switch (cmd) {
3906cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
39070470d791SMatt Jacob 	{
3908ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
3909ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
3910cbf57b47SMatt Jacob 		int flags, tgt;
39112df76c16SMatt Jacob 		sdparam *sdp;
3912ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
3913cbf57b47SMatt Jacob 
391429f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
3915ab163f5fSMatt Jacob 
39162df76c16SMatt Jacob 		va_start(ap, cmd);
39172df76c16SMatt Jacob 		bus = va_arg(ap, int);
39182df76c16SMatt Jacob 		tgt = va_arg(ap, int);
39192df76c16SMatt Jacob 		va_end(ap);
39202df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
39212df76c16SMatt Jacob 
39222df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
39232df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3924cbf57b47SMatt Jacob 			break;
3925cbf57b47SMatt Jacob 		}
39269ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
3927ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
3928ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
3929ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
3930ab163f5fSMatt Jacob 
3931ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
3932ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
3933ab163f5fSMatt Jacob 
3934ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
3935ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
3936ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3937ab163f5fSMatt Jacob 		}
3938ab163f5fSMatt Jacob 
3939cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
3940ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
3941ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3942ab163f5fSMatt Jacob 		}
3943ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3944ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
3945ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3946ab163f5fSMatt Jacob 		} else {
3947ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3948ab163f5fSMatt Jacob 		}
3949ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
3950ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3951ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
39529ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
39539ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3954ab163f5fSMatt Jacob 		}
39552df76c16SMatt 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);
3956ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3957ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3958cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
3959cbf57b47SMatt Jacob 		break;
39600470d791SMatt Jacob 	}
396157c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
39622df76c16SMatt Jacob 	{
39632df76c16SMatt Jacob 		va_start(ap, cmd);
39642df76c16SMatt Jacob 		bus = va_arg(ap, int);
39652df76c16SMatt Jacob 		va_end(ap);
39662df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
39672df76c16SMatt Jacob 		if (IS_FC(isp)) {
39682df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
39692df76c16SMatt Jacob 		} else {
39702df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
397157c801f5SMatt Jacob 		}
397257c801f5SMatt Jacob 		break;
39732df76c16SMatt Jacob 	}
39745d571944SMatt Jacob 	case ISPASYNC_LIP:
397537a7daacSAlexander Motin 		if (msg == NULL)
3976f7c631bcSMatt Jacob 			msg = "LIP Received";
3977f7c631bcSMatt Jacob 		/* FALLTHROUGH */
39785d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
397937a7daacSAlexander Motin 		if (msg == NULL)
3980f7c631bcSMatt Jacob 			msg = "LOOP Reset";
3981f7c631bcSMatt Jacob 		/* FALLTHROUGH */
398257c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
398337a7daacSAlexander Motin 		if (msg == NULL)
3984f7c631bcSMatt Jacob 			msg = "LOOP Down";
39852df76c16SMatt Jacob 		va_start(ap, cmd);
39862df76c16SMatt Jacob 		bus = va_arg(ap, int);
39872df76c16SMatt Jacob 		va_end(ap);
398821daf914SAlexander Motin 		isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
3989e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
3990e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
399157c801f5SMatt Jacob 		break;
399257c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
39932df76c16SMatt Jacob 		va_start(ap, cmd);
39942df76c16SMatt Jacob 		bus = va_arg(ap, int);
39952df76c16SMatt Jacob 		va_end(ap);
3996e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
39972df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
399857c801f5SMatt Jacob 		break;
399910365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
40002df76c16SMatt Jacob 		va_start(ap, cmd);
40012df76c16SMatt Jacob 		bus = va_arg(ap, int);
40022df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
40032df76c16SMatt Jacob 		va_end(ap);
4004a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4005e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4006387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4007e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4008e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4009e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4010e68eef14SAlexander Motin 			lp->is_target = 1;
401121daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
40125704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
4013e68eef14SAlexander Motin 		}
4014e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4015e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4016e68eef14SAlexander Motin 			lp->is_initiator = 1;
4017e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4018e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4019e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4020e68eef14SAlexander Motin 			adc->port = lp->portid;
4021d4f3ad3aSAlexander Motin 			adc->target = tgt;
4022e68eef14SAlexander Motin 			adc->arrived = 1;
4023e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
402410365e5aSMatt Jacob 		}
402510365e5aSMatt Jacob 		break;
402610365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
40272df76c16SMatt Jacob 		va_start(ap, cmd);
40282df76c16SMatt Jacob 		bus = va_arg(ap, int);
40292df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
40302df76c16SMatt Jacob 		va_end(ap);
4031a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4032e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4033e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4034e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4035e68eef14SAlexander Motin changed:
4036e68eef14SAlexander Motin 		if (lp->is_target !=
4037e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4038e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4039e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
4040e68eef14SAlexander Motin 			if (lp->is_target) {
404121daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4042e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
404310365e5aSMatt Jacob 			} else {
4044e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
404521daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
404610365e5aSMatt Jacob 			}
4047f7c631bcSMatt Jacob 		}
4048e68eef14SAlexander Motin 		if (lp->is_initiator !=
4049e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4050e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4051e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
4052e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4053e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4054e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4055e68eef14SAlexander Motin 			adc->port = lp->portid;
4056d4f3ad3aSAlexander Motin 			adc->target = tgt;
4057e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
4058e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
4059e68eef14SAlexander Motin 		}
406010365e5aSMatt Jacob 		break;
406110365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
40622df76c16SMatt Jacob 		va_start(ap, cmd);
40632df76c16SMatt Jacob 		bus = va_arg(ap, int);
40642df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
40652df76c16SMatt Jacob 		va_end(ap);
4066fb4a4356SKenneth D. Merry 		fc = ISP_FC_PC(isp, bus);
4067e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4068e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4069e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4070e68eef14SAlexander Motin 		goto changed;
407110365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
40722df76c16SMatt Jacob 		va_start(ap, cmd);
40732df76c16SMatt Jacob 		bus = va_arg(ap, int);
40742df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
40752df76c16SMatt Jacob 		va_end(ap);
4076a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4077e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4078f7c631bcSMatt Jacob 		/*
4079e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
4080e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
4081f7c631bcSMatt Jacob 		 */
4082387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4083e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
4084f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
4085e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
4086e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4087a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
4088387d8239SMatt 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);
4089a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
4090f7c631bcSMatt Jacob 			}
4091e68eef14SAlexander Motin 			break;
40924b9d588eSMatt Jacob 		}
4093e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
409410365e5aSMatt Jacob 		break;
409510365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
409610365e5aSMatt Jacob 	{
409710365e5aSMatt Jacob 		char *msg;
4098eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
40992df76c16SMatt Jacob 
41002df76c16SMatt Jacob 		va_start(ap, cmd);
41012df76c16SMatt Jacob 		bus = va_arg(ap, int);
41022df76c16SMatt Jacob 		evt = va_arg(ap, int);
4103eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
41042df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
41052df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
41062df76c16SMatt Jacob 			reason = va_arg(ap, int);
4107eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4108eea52482SAlexander Motin 			portid = va_arg(ap, int);
410910365e5aSMatt Jacob 		} else {
41102df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
41112df76c16SMatt Jacob 			nlstate = reason = 0;
411210365e5aSMatt Jacob 		}
41132df76c16SMatt Jacob 		va_end(ap);
4114a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
41152df76c16SMatt Jacob 
41162df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
4117b5d5037bSAlexander Motin 			msg = "Port Database Changed";
4118eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
4119eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4120eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
41212df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4122b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
4123eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4124eea52482SAlexander Motin 			    bus, msg, portid);
41252df76c16SMatt Jacob 		} else {
4126b5d5037bSAlexander Motin 			msg = "Other Change Notify";
4127eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
41282df76c16SMatt Jacob 		}
4129e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
413057c801f5SMatt Jacob 		break;
413102ab3379SMatt Jacob 	}
4132d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4133e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
4134d81ba9d5SMatt Jacob 	{
41352df76c16SMatt Jacob 		isp_notify_t *notify;
41362df76c16SMatt Jacob 		va_start(ap, cmd);
41372df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
41382df76c16SMatt Jacob 		va_end(ap);
41392df76c16SMatt Jacob 		switch (notify->nt_ncode) {
41402df76c16SMatt Jacob 		case NT_ABORT_TASK:
41412df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
41422df76c16SMatt Jacob 		case NT_CLEAR_ACA:
41432df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
41442df76c16SMatt Jacob 		case NT_LUN_RESET:
41452df76c16SMatt Jacob 		case NT_TARGET_RESET:
4146c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
4147c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
41482df76c16SMatt Jacob 			/*
41492df76c16SMatt Jacob 			 * These are task management functions.
41502df76c16SMatt Jacob 			 */
41512df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
41522df76c16SMatt Jacob 			break;
41532df76c16SMatt Jacob 		case NT_BUS_RESET:
41542df76c16SMatt Jacob 		case NT_LIP_RESET:
41552df76c16SMatt Jacob 		case NT_LINK_UP:
41562df76c16SMatt Jacob 		case NT_LINK_DOWN:
4157e68eef14SAlexander Motin 		case NT_HBA_RESET:
41582df76c16SMatt Jacob 			/*
41592df76c16SMatt Jacob 			 * No action need be taken here.
41602df76c16SMatt Jacob 			 */
41612df76c16SMatt Jacob 			break;
4162387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
41632df76c16SMatt Jacob 		case NT_LOGOUT:
41642df76c16SMatt Jacob 			/*
41652df76c16SMatt Jacob 			 * This is device arrival/departure notification
41662df76c16SMatt Jacob 			 */
416796b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
41682df76c16SMatt Jacob 			break;
41692df76c16SMatt Jacob 		default:
41702df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
417196b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
41722df76c16SMatt Jacob 			break;
41732df76c16SMatt Jacob 		}
4174d81ba9d5SMatt Jacob 		break;
4175d81ba9d5SMatt Jacob 	}
4176387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
4177387d8239SMatt Jacob 	{
4178387d8239SMatt Jacob 		void *inot;
4179387d8239SMatt Jacob 		va_start(ap, cmd);
4180387d8239SMatt Jacob 		inot = va_arg(ap, void *);
4181387d8239SMatt Jacob 		va_end(ap);
4182387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
4183387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4184387d8239SMatt Jacob 			if (tp) {
4185387d8239SMatt Jacob 				tp->isp = isp;
4186387d8239SMatt Jacob 				if (inot) {
4187387d8239SMatt Jacob 					memcpy(tp->data, inot, sizeof (tp->data));
4188387d8239SMatt Jacob 					tp->not = tp->data;
4189387d8239SMatt Jacob 				} else {
4190387d8239SMatt Jacob 					tp->not = NULL;
4191387d8239SMatt Jacob 				}
41922a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
41932a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
41942a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
4195387d8239SMatt Jacob 			} else {
4196387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4197387d8239SMatt Jacob 			}
4198387d8239SMatt Jacob 		}
4199387d8239SMatt Jacob 		break;
4200387d8239SMatt Jacob 	}
4201d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
42022df76c16SMatt Jacob 	{
42032df76c16SMatt Jacob 		isphdr_t *hp;
42042df76c16SMatt Jacob 
42052df76c16SMatt Jacob 		va_start(ap, cmd);
42062df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
42072df76c16SMatt Jacob 		va_end(ap);
42082df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
4209cbf57b47SMatt Jacob 		default:
42102df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4211d81ba9d5SMatt Jacob 			break;
4212570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
42133e6deb33SAlexander Motin 			if (IS_24XX(isp)) {
42142df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4215570c7a3fSMatt Jacob 			} else {
42162df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4217570c7a3fSMatt Jacob 			}
4218570c7a3fSMatt Jacob 			break;
4219d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
42202df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4221d81ba9d5SMatt Jacob 			break;
4222d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
42232df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4224d81ba9d5SMatt Jacob 			break;
42252df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
4226d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
4227d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
4228d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
42292df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
4230d81ba9d5SMatt Jacob 			break;
42312df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
42322df76c16SMatt Jacob 		{
42332df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
42342df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
42358290ea90SAlexander Motin 			atio_private_data_t *atp;
42362df76c16SMatt Jacob 			fcportdb_t *lp;
42372df76c16SMatt Jacob 			uint16_t chan;
42382df76c16SMatt Jacob 			uint32_t sid, did;
42392df76c16SMatt Jacob 
42402df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
42412df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
42422df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
42432df76c16SMatt Jacob 
42442df76c16SMatt Jacob 			nt->nt_hba = isp;
42452df76c16SMatt Jacob 			nt->nt_did = did;
42462df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
42472df76c16SMatt Jacob 			nt->nt_sid = sid;
42482df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
42492df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
42502df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
42512df76c16SMatt Jacob 			} else {
42522df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4253e68eef14SAlexander Motin 				if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
42542df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
42552df76c16SMatt Jacob 				} else {
42562df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
42572df76c16SMatt Jacob 				}
42582df76c16SMatt Jacob 			}
42592df76c16SMatt Jacob 			/*
42602df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
42612df76c16SMatt Jacob 			 */
42628290ea90SAlexander Motin 			atp = isp_find_atpd(isp, chan, abts->abts_rxid_task);
42638290ea90SAlexander Motin 			nt->nt_lun = atp ? atp->lun : LUN_ANY;
42642df76c16SMatt Jacob 			nt->nt_need_ack = 1;
42652df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
42662df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
42672df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
42682df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
42692df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
42702df76c16SMatt Jacob 			} else {
42712df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
42722df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
42732df76c16SMatt Jacob 			}
42742df76c16SMatt Jacob 			nt->nt_channel = chan;
42752df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
42762df76c16SMatt Jacob 			nt->nt_lreserved = hp;
42772df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
42782df76c16SMatt Jacob 			break;
42792df76c16SMatt Jacob 		}
4280d81ba9d5SMatt Jacob 		}
4281d81ba9d5SMatt Jacob 		break;
42822df76c16SMatt Jacob 	}
4283d81ba9d5SMatt Jacob #endif
4284ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
4285ab163f5fSMatt Jacob 	{
42861dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
4287ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
4288ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
4289ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
4290ab163f5fSMatt Jacob 		} else {
4291ab163f5fSMatt Jacob 			mbox6 = 0;
4292ab163f5fSMatt Jacob 		}
42932df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
42940a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
42950a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
42962df76c16SMatt Jacob 		isp_reinit(isp, 1);
42970a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
42980a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4299ab163f5fSMatt Jacob 		break;
4300ab163f5fSMatt Jacob 	}
4301d81ba9d5SMatt Jacob 	default:
4302b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4303cbf57b47SMatt Jacob 		break;
4304cbf57b47SMatt Jacob 	}
4305cbf57b47SMatt Jacob }
4306cbf57b47SMatt Jacob 
43072df76c16SMatt Jacob uint64_t
43082df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
43092df76c16SMatt Jacob {
43102df76c16SMatt Jacob 	uint64_t seed;
43112df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
43122df76c16SMatt Jacob 
4313a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4314a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4315a193dc4bSAlexander Motin 	if (seed)
4316a193dc4bSAlexander Motin 		return (seed);
4317a193dc4bSAlexander Motin 
4318a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
43192df76c16SMatt Jacob 	if (isactive) {
4320a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4321a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4322a193dc4bSAlexander Motin 		if (seed)
43232df76c16SMatt Jacob 			return (seed);
43242df76c16SMatt Jacob 	}
4325a193dc4bSAlexander Motin 
4326a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4327a193dc4bSAlexander Motin 	if (chan > 0) {
4328a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4329a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4330a193dc4bSAlexander Motin 		if (seed == 0) {
4331a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4332a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
43335cc3786cSMatt Jacob 		}
43342df76c16SMatt Jacob 	}
43352df76c16SMatt Jacob 
4336a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4337a193dc4bSAlexander Motin 	if (seed == 0) {
4338a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4339a193dc4bSAlexander Motin 		if (!iswwnn)
4340a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4341a193dc4bSAlexander Motin 	}
43422df76c16SMatt Jacob 
4343a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4344a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
43452df76c16SMatt Jacob 		/*
43462df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
43472df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4348a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
43492df76c16SMatt Jacob 		 */
4350a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4351a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4352a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
43532df76c16SMatt Jacob 	}
43542df76c16SMatt Jacob 	return (seed);
43552df76c16SMatt Jacob }
43562df76c16SMatt Jacob 
4357b09b0095SMatt Jacob void
43589cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4359b09b0095SMatt Jacob {
4360de461933SMatt Jacob 	int loc;
4361387d8239SMatt Jacob 	char lbuf[200];
4362b09b0095SMatt Jacob 	va_list ap;
4363de461933SMatt Jacob 
4364b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4365b09b0095SMatt Jacob 		return;
4366b09b0095SMatt Jacob 	}
4367387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4368de461933SMatt Jacob 	loc = strlen(lbuf);
4369b09b0095SMatt Jacob 	va_start(ap, fmt);
4370de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4371b09b0095SMatt Jacob 	va_end(ap);
4372de461933SMatt Jacob 	printf("%s\n", lbuf);
4373b09b0095SMatt Jacob }
4374f7c631bcSMatt Jacob 
4375670508b1SMatt Jacob void
4376670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4377670508b1SMatt Jacob {
4378670508b1SMatt Jacob 	va_list ap;
4379670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4380670508b1SMatt Jacob 		return;
4381670508b1SMatt Jacob 	}
4382670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4383670508b1SMatt Jacob 	va_start(ap, fmt);
4384670508b1SMatt Jacob 	vprintf(fmt, ap);
4385670508b1SMatt Jacob 	va_end(ap);
4386670508b1SMatt Jacob 	printf("\n");
4387670508b1SMatt Jacob }
4388670508b1SMatt Jacob 
4389f7c631bcSMatt Jacob uint64_t
4390f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4391f7c631bcSMatt Jacob {
4392f7c631bcSMatt Jacob 	uint64_t elapsed;
4393f7c631bcSMatt Jacob 	struct timespec x = *b;
4394f7c631bcSMatt Jacob 	timespecsub(&x, a);
4395f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4396f7c631bcSMatt Jacob 	if (elapsed == 0)
4397f7c631bcSMatt Jacob 		elapsed++;
4398f7c631bcSMatt Jacob 	return (elapsed);
4399f7c631bcSMatt Jacob }
4400f7c631bcSMatt Jacob 
4401f7c631bcSMatt Jacob int
4402f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4403f7c631bcSMatt Jacob {
4404f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4405f7c631bcSMatt Jacob 		return (1);
4406f7c631bcSMatt Jacob 	} else {
4407f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4408f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4409f7c631bcSMatt Jacob 		return (0);
4410f7c631bcSMatt Jacob 	}
4411f7c631bcSMatt Jacob }
4412f7c631bcSMatt Jacob 
4413f7c631bcSMatt Jacob void
4414f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4415f7c631bcSMatt Jacob {
4416*6327b0d2SAlexander Motin 	u_int t, to;
4417f7c631bcSMatt Jacob 
4418*6327b0d2SAlexander Motin 	to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4419f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4420f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4421f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
4422*6327b0d2SAlexander Motin 		msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_osinfo.lock,
4423*6327b0d2SAlexander Motin 		    PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4424f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4425f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4426f7c631bcSMatt Jacob 	} else {
4427*6327b0d2SAlexander Motin 		for (t = 0; t < to; t += 100) {
44286ce548a1SAlexander Motin 			uint16_t isr, sema, info;
4429*6327b0d2SAlexander Motin 			if (isp->isp_osinfo.mboxcmd_done)
4430f7c631bcSMatt Jacob 				break;
44316ce548a1SAlexander Motin 			if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
44326ce548a1SAlexander Motin 				isp_intr(isp, isr, sema, info);
4433*6327b0d2SAlexander Motin 				if (isp->isp_osinfo.mboxcmd_done)
4434f7c631bcSMatt Jacob 					break;
4435f7c631bcSMatt Jacob 			}
44362df76c16SMatt Jacob 			ISP_DELAY(100);
4437f7c631bcSMatt Jacob 		}
4438f7c631bcSMatt Jacob 	}
4439f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
4440*6327b0d2SAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
4441*6327b0d2SAlexander Motin 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
4442*6327b0d2SAlexander Motin 		    isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4443f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4444f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4445f7c631bcSMatt Jacob 	}
4446f7c631bcSMatt Jacob }
4447f7c631bcSMatt Jacob 
4448f7c631bcSMatt Jacob void
4449f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4450f7c631bcSMatt Jacob {
4451f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
4452*6327b0d2SAlexander Motin 	if (isp->isp_osinfo.mbox_sleeping)
4453*6327b0d2SAlexander Motin 		wakeup(&isp->isp_osinfo.mboxcmd_done);
4454f7c631bcSMatt Jacob }
4455f7c631bcSMatt Jacob 
4456f7c631bcSMatt Jacob void
4457f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4458f7c631bcSMatt Jacob {
4459f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4460f7c631bcSMatt Jacob }
4461f7c631bcSMatt Jacob 
4462f7c631bcSMatt Jacob int
44632df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
44642df76c16SMatt Jacob {
44652df76c16SMatt Jacob 	int ret = 0;
44662df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
44672df76c16SMatt Jacob 		ret = -1;
44682df76c16SMatt Jacob 	} else {
44692df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
44702df76c16SMatt Jacob 	}
44712df76c16SMatt Jacob 	return (ret);
44722df76c16SMatt Jacob }
44732df76c16SMatt Jacob 
44742df76c16SMatt Jacob int
4475f7c631bcSMatt Jacob isp_mstohz(int ms)
4476f7c631bcSMatt Jacob {
4477a4f3a2beSMatt Jacob 	int hz;
4478f7c631bcSMatt Jacob 	struct timeval t;
4479f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
4480f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
4481a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
4482a4f3a2beSMatt Jacob 	if (hz < 0) {
4483a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
4484f7c631bcSMatt Jacob 	}
4485a4f3a2beSMatt Jacob 	if (hz == 0) {
4486a4f3a2beSMatt Jacob 		hz = 1;
4487a4f3a2beSMatt Jacob 	}
4488a4f3a2beSMatt Jacob 	return (hz);
4489f7c631bcSMatt Jacob }
44900a70657fSMatt Jacob 
44910a70657fSMatt Jacob void
44920a70657fSMatt Jacob isp_platform_intr(void *arg)
44930a70657fSMatt Jacob {
44940a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
44956ce548a1SAlexander Motin 	uint16_t isr, sema, info;
44960a70657fSMatt Jacob 
44970a70657fSMatt Jacob 	ISP_LOCK(isp);
44980a70657fSMatt Jacob 	isp->isp_intcnt++;
44996ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
45006ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
45016ce548a1SAlexander Motin 	else
45020a70657fSMatt Jacob 		isp->isp_intbogus++;
45030a70657fSMatt Jacob 	ISP_UNLOCK(isp);
45040a70657fSMatt Jacob }
45050a70657fSMatt Jacob 
45060a70657fSMatt Jacob void
45070a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
45080a70657fSMatt Jacob {
45090a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
45102df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
45110a70657fSMatt Jacob 	} else {
45122df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
45130a70657fSMatt Jacob 	}
45140a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
45150a70657fSMatt Jacob }
45162df76c16SMatt Jacob 
4517fb4a4356SKenneth D. Merry /*
4518fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4519fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4520fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4521fb4a4356SKenneth D. Merry  */
4522fb4a4356SKenneth D. Merry void
452321daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4524fb4a4356SKenneth D. Merry {
452521daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4526fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
452721daf914SAlexander Motin 	int i;
4528fb4a4356SKenneth D. Merry 
4529fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
453021daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
453121daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4532fb4a4356SKenneth D. Merry 	else
453321daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
453421daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4535fb4a4356SKenneth D. Merry 
4536fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
453721daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
453821daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4539fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4540fb4a4356SKenneth D. Merry 		}
4541fb4a4356SKenneth D. Merry 	}
4542fb4a4356SKenneth D. Merry }
4543fb4a4356SKenneth D. Merry 
4544387d8239SMatt Jacob int
4545387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4546387d8239SMatt Jacob {
45476af11b82SAlexander Motin 	lun_id_t lun;
45486af11b82SAlexander Motin 	uint32_t chan, tgt;
4549405b7a29SMatt Jacob 	struct isp_fc *fc;
4550405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4551405b7a29SMatt Jacob 	int idx;
4552405b7a29SMatt Jacob 
4553a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4554405b7a29SMatt Jacob 		return (0);
4555405b7a29SMatt Jacob 
4556405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4557405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4558405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4559405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4560405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4561405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4562387d8239SMatt Jacob 
4563387d8239SMatt Jacob 	while (nxp) {
4564387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4565387d8239SMatt Jacob 			break;
4566387d8239SMatt Jacob 		nxp = nxp->next;
4567387d8239SMatt Jacob 	}
4568387d8239SMatt Jacob 	if (nxp == NULL) {
4569387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4570387d8239SMatt Jacob 		if (nxp == NULL) {
4571387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4572387d8239SMatt Jacob 			if (nxp == NULL) {
4573387d8239SMatt Jacob 				return (-1);
4574387d8239SMatt Jacob 			}
4575387d8239SMatt Jacob 		} else {
4576387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4577387d8239SMatt Jacob 		}
4578387d8239SMatt Jacob 		nxp->tgt = tgt;
4579387d8239SMatt Jacob 		nxp->lun = lun;
4580387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4581387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4582387d8239SMatt Jacob 	}
4583387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4584387d8239SMatt Jacob 		nxp->crnseed = 1;
4585387d8239SMatt Jacob 	PISP_PCMD(cmd)->crn = nxp->crnseed;
4586387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4587387d8239SMatt Jacob 	return (0);
4588387d8239SMatt Jacob }
4589387d8239SMatt Jacob 
459094dff771SMatt Jacob /*
459194dff771SMatt Jacob  * We enter with the lock held
459294dff771SMatt Jacob  */
45932df76c16SMatt Jacob void
45942df76c16SMatt Jacob isp_timer(void *arg)
45952df76c16SMatt Jacob {
45962df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
45972df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
45982df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
45992df76c16SMatt Jacob #endif
460094dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
46012df76c16SMatt Jacob }
4602387d8239SMatt Jacob 
4603387d8239SMatt Jacob isp_ecmd_t *
4604387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4605387d8239SMatt Jacob {
4606387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4607387d8239SMatt Jacob 	if (ecmd) {
4608387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4609387d8239SMatt Jacob 	}
4610387d8239SMatt Jacob 	return (ecmd);
4611387d8239SMatt Jacob }
4612387d8239SMatt Jacob 
4613387d8239SMatt Jacob void
4614387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4615387d8239SMatt Jacob {
4616387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4617387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4618387d8239SMatt Jacob }
4619