xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 229203af6865e153ce0b3f8a15c19a37b49f34f3)
1098ca2bdSWarner Losh /*-
22df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
3e5265237SMatt Jacob  * All rights reserved.
46054c3f6SMatt Jacob  *
56054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
66054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
76054c3f6SMatt Jacob  * are met:
86054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
96054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
106054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
11aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
12aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
136054c3f6SMatt Jacob  *
146054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
186054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246054c3f6SMatt Jacob  * SUCH DAMAGE.
256054c3f6SMatt Jacob  */
26aad970f1SDavid E. O'Brien 
27799881e0SMatt Jacob /*
28799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29799881e0SMatt Jacob  */
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
325b14cb41SWill Andrews 
336054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
345d571944SMatt Jacob #include <sys/unistd.h>
355d571944SMatt Jacob #include <sys/kthread.h>
365d571944SMatt Jacob #include <sys/conf.h>
374eb49427SMatt Jacob #include <sys/module.h>
385d571944SMatt Jacob #include <sys/ioccom.h>
395d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4070273f90SMatt Jacob #include <sys/devicestat.h>
41f7c631bcSMatt Jacob #include <cam/cam_periph.h>
426c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
436054c3f6SMatt Jacob 
444eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4556c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4673030e03SMatt Jacob int isp_announced = 0;
472df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
482df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
49f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
50e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5173030e03SMatt Jacob 
52e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
53e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
545d571944SMatt Jacob static d_ioctl_t ispioctl;
55f6e75de2SMatt Jacob static void isp_intr_enable(void *);
561dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
570470d791SMatt Jacob static void isp_poll(struct cam_sim *);
58b85389e1SMatt Jacob static timeout_t isp_watchdog;
59de461933SMatt Jacob static timeout_t isp_gdt;
60de461933SMatt Jacob static task_fn_t isp_gdt_task;
615d571944SMatt Jacob static void isp_kthread(void *);
62d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6394dff771SMatt Jacob static int isp_timer_count;
642df76c16SMatt Jacob static void isp_timer(void *);
65cc8df88bSMatt Jacob 
665d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
67dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
687ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
697ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
705d571944SMatt Jacob };
715d571944SMatt Jacob 
722df76c16SMatt Jacob static int
7307f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7407f56f1cSAlexander Motin {
7507f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7607f56f1cSAlexander Motin 	int chan = arg2;
7707f56f1cSAlexander Motin 	int error, old, value;
7807f56f1cSAlexander Motin 
7907f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8007f56f1cSAlexander Motin 
8107f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8207f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8307f56f1cSAlexander Motin 		return (error);
8407f56f1cSAlexander Motin 
8507f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8607f56f1cSAlexander Motin 		return (EINVAL);
8707f56f1cSAlexander Motin 
8807f56f1cSAlexander Motin 	ISP_LOCK(isp);
8907f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9007f56f1cSAlexander Motin 
91391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
92391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
93391f03daSAlexander Motin 
9407f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9507f56f1cSAlexander Motin 	if (value == old) {
9607f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9707f56f1cSAlexander Motin 		return (0);
9807f56f1cSAlexander Motin 	}
9907f56f1cSAlexander Motin 
10007f56f1cSAlexander Motin 	/* Actually change the role. */
1016bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10207f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10307f56f1cSAlexander Motin 	return (error);
10407f56f1cSAlexander Motin }
10507f56f1cSAlexander Motin 
10607f56f1cSAlexander Motin static int
1072df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
108478f8a96SJustin T. Gibbs {
109478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
110ea6f23cdSMatt Jacob 	struct cam_sim *sim;
111ea6f23cdSMatt Jacob 	struct cam_path *path;
1128290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
1138290ea90SAlexander Motin 	int i;
1148290ea90SAlexander Motin #endif
115478f8a96SJustin T. Gibbs 
116478f8a96SJustin T. Gibbs 	/*
1172df76c16SMatt Jacob 	 * Construct our SIM entry.
118ea6f23cdSMatt Jacob 	 */
1192df76c16SMatt 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);
120ea6f23cdSMatt Jacob 
1212df76c16SMatt Jacob 	if (sim == NULL) {
1222df76c16SMatt Jacob 		return (ENOMEM);
1232df76c16SMatt Jacob 	}
1242df76c16SMatt Jacob 
1252df76c16SMatt Jacob 	ISP_LOCK(isp);
1262df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1272df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1282df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1292df76c16SMatt Jacob 		return (EIO);
1302df76c16SMatt Jacob 	}
1312df76c16SMatt Jacob 	ISP_UNLOCK(isp);
132227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1332df76c16SMatt Jacob 		ISP_LOCK(isp);
1342df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1352df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1362df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1372df76c16SMatt Jacob 		return (ENXIO);
1382df76c16SMatt Jacob 	}
1392df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1402df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1412df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1422df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1432df76c16SMatt Jacob 	csa.callback_arg = sim;
144387d8239SMatt Jacob 
145387d8239SMatt Jacob 	ISP_LOCK(isp);
1462df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
147387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1482df76c16SMatt Jacob 
1492df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1502df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1512df76c16SMatt Jacob 		spi->sim = sim;
1522df76c16SMatt Jacob 		spi->path = path;
1530d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
15409ddc7adSAlexander Motin 		TAILQ_INIT(&spi->waitq);
1558290ea90SAlexander Motin 		STAILQ_INIT(&spi->ntfree);
1568290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1578290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
1588290ea90SAlexander Motin 		LIST_INIT(&spi->atfree);
1598290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1608290ea90SAlexander Motin 			LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
1618290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1628290ea90SAlexander Motin 			LIST_INIT(&spi->atused[i]);
1630d965629SAlexander Motin #endif
1642df76c16SMatt Jacob 	} else {
165a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1662df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
16707f56f1cSAlexander Motin 		struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
16807f56f1cSAlexander Motin 		struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
16907f56f1cSAlexander Motin 		char name[16];
1702df76c16SMatt Jacob 
171a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1722df76c16SMatt Jacob 		fc->sim = sim;
1732df76c16SMatt Jacob 		fc->path = path;
1742df76c16SMatt Jacob 		fc->isp = isp;
175a01f5aebSMatt Jacob 		fc->ready = 1;
176de461933SMatt Jacob 
1772df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
178de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
1790d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
18009ddc7adSAlexander Motin 		TAILQ_INIT(&fc->waitq);
1818290ea90SAlexander Motin 		STAILQ_INIT(&fc->ntfree);
1828290ea90SAlexander Motin 		for (i = 0; i < ATPDPSIZE; i++)
1838290ea90SAlexander Motin 			STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
1848290ea90SAlexander Motin 		LIST_INIT(&fc->atfree);
1858290ea90SAlexander Motin 		for (i = ATPDPSIZE-1; i >= 0; i--)
1868290ea90SAlexander Motin 			LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
1878290ea90SAlexander Motin 		for (i = 0; i < ATPDPHASHSIZE; i++)
1888290ea90SAlexander Motin 			LIST_INIT(&fc->atused[i]);
1890d965629SAlexander Motin #endif
190e561aa79SAlexander Motin 		isp_loop_changed(isp, chan);
1912df76c16SMatt Jacob 		ISP_UNLOCK(isp);
192069f5ef9SAlexander Motin 		if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
193069f5ef9SAlexander Motin 		    "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
194a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
195a01f5aebSMatt Jacob 			ISP_LOCK(isp);
196a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
197a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
198a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
199a01f5aebSMatt Jacob 			return (ENOMEM);
200a01f5aebSMatt Jacob 		}
201e596ff7aSAlexander Motin 		fc->num_threads += 1;
20207f56f1cSAlexander Motin 		if (chan > 0) {
20307f56f1cSAlexander Motin 			snprintf(name, sizeof(name), "chan%d", chan);
20407f56f1cSAlexander Motin 			tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
20507f56f1cSAlexander Motin 			    OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
206a0ec8e99SMatt Jacob 		}
207e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
208e596ff7aSAlexander Motin 		    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
209e596ff7aSAlexander Motin 		    "World Wide Node Name");
210e596ff7aSAlexander Motin 		SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
211e596ff7aSAlexander Motin 		    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
212e596ff7aSAlexander Motin 		    "World Wide Port Name");
213e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
214e596ff7aSAlexander Motin 		    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
215e596ff7aSAlexander Motin 		    "Loop Down Limit");
216e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
217e596ff7aSAlexander Motin 		    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
218e596ff7aSAlexander Motin 		    "Gone Device Time");
21907f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
220e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
221e596ff7aSAlexander Motin 		    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
222e596ff7aSAlexander Motin 		    "Cause a Lost Frame on a Read");
22307f56f1cSAlexander Motin #endif
22407f56f1cSAlexander Motin 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
22507f56f1cSAlexander Motin 		    "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
22607f56f1cSAlexander Motin 		    isp_role_sysctl, "I", "Current role");
227e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
228e596ff7aSAlexander Motin 		    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
229e596ff7aSAlexander Motin 		    "Connection speed in gigabits");
230e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
231e596ff7aSAlexander Motin 		    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
232e596ff7aSAlexander Motin 		    "Link state");
233e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
234e596ff7aSAlexander Motin 		    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
235e596ff7aSAlexander Motin 		    "Firmware state");
236e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
237e596ff7aSAlexander Motin 		    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
238e596ff7aSAlexander Motin 		    "Loop state");
239e596ff7aSAlexander Motin 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
240e596ff7aSAlexander Motin 		    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
241e596ff7aSAlexander Motin 		    "Connection topology");
2422df76c16SMatt Jacob 	}
2432df76c16SMatt Jacob 	return (0);
2442df76c16SMatt Jacob }
2452df76c16SMatt Jacob 
2461d0a1de2SWill Andrews static void
2471d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2481d0a1de2SWill Andrews {
2491d0a1de2SWill Andrews 	struct cam_sim *sim;
2501d0a1de2SWill Andrews 	struct cam_path *path;
2511d0a1de2SWill Andrews 	struct ccb_setasync csa;
2521d0a1de2SWill Andrews 	int *num_threads;
2531d0a1de2SWill Andrews 
2541d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2551d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2561d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2571d0a1de2SWill Andrews 
2581d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2591d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2601d0a1de2SWill Andrews 	csa.event_enable = 0;
2611d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2621d0a1de2SWill Andrews 	csa.callback_arg = sim;
2631d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2641d0a1de2SWill Andrews 	xpt_free_path(path);
2651d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2661d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2671d0a1de2SWill Andrews 
2681d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2691d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2701d0a1de2SWill Andrews 	while (*num_threads != 0)
2711d0a1de2SWill Andrews 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
2721d0a1de2SWill Andrews }
2731d0a1de2SWill Andrews 
2742df76c16SMatt Jacob int
2752df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2762df76c16SMatt Jacob {
2772df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2782df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2792df76c16SMatt Jacob 	int chan;
2802df76c16SMatt Jacob 
2812df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
2822df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_arg = isp;
283e95725cbSMatt Jacob 	/*
284e95725cbSMatt Jacob 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
285e95725cbSMatt Jacob 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
286e95725cbSMatt Jacob 	 * which would be unwise to then set again later.
287e95725cbSMatt Jacob 	 */
288e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 1;
2892df76c16SMatt Jacob 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
2902df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
2912df76c16SMatt Jacob 		return (-EIO);
2922df76c16SMatt Jacob 	}
293ea6f23cdSMatt Jacob 
294ea6f23cdSMatt Jacob 	/*
295ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
296478f8a96SJustin T. Gibbs 	 */
2972df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2982df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2995d571944SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3002df76c16SMatt Jacob 		return (EIO);
301478f8a96SJustin T. Gibbs 	}
302478f8a96SJustin T. Gibbs 
3032df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
3042df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
3052df76c16SMatt Jacob 			goto unwind;
306ea6f23cdSMatt Jacob 		}
307ea6f23cdSMatt Jacob 	}
308ea6f23cdSMatt Jacob 
3092df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
31094dff771SMatt Jacob 	isp_timer_count = hz >> 2;
31194dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
3122df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
313ea6f23cdSMatt Jacob 
3142df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
3152df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3162df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
317ea6f23cdSMatt Jacob 	}
3182df76c16SMatt Jacob 	return (0);
3195d571944SMatt Jacob 
3202df76c16SMatt Jacob unwind:
3212df76c16SMatt Jacob 	while (--chan >= 0) {
3222df76c16SMatt Jacob 		struct cam_sim *sim;
3232df76c16SMatt Jacob 		struct cam_path *path;
3241d0a1de2SWill Andrews 
3251d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
3261d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
3272df76c16SMatt Jacob 		xpt_free_path(path);
3280a70657fSMatt Jacob 		ISP_LOCK(isp);
3290a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
3302df76c16SMatt Jacob 		ISP_UNLOCK(isp);
3312df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
3322df76c16SMatt Jacob 	}
3332df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3340a70657fSMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3352df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3360a70657fSMatt Jacob 	}
3372df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3382df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3392df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3402df76c16SMatt Jacob 	}
3412df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3422df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3432df76c16SMatt Jacob 	return (-1);
3442df76c16SMatt Jacob }
3452df76c16SMatt Jacob 
346e95725cbSMatt Jacob int
3472df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3482df76c16SMatt Jacob {
349e95725cbSMatt Jacob 	struct cam_sim *sim;
3502df76c16SMatt Jacob 	int chan;
3512df76c16SMatt Jacob 
3520a70657fSMatt Jacob 	ISP_LOCK(isp);
3532df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
3541d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
355e95725cbSMatt Jacob 		if (sim->refcount > 2) {
3562df76c16SMatt Jacob 			ISP_UNLOCK(isp);
357e95725cbSMatt Jacob 			return (EBUSY);
358e95725cbSMatt Jacob 		}
359e95725cbSMatt Jacob 	}
3601d0a1de2SWill Andrews 	/* Tell spawned threads that we're exiting. */
3611d0a1de2SWill Andrews 	isp->isp_osinfo.is_exiting = 1;
362e95725cbSMatt Jacob 	if (isp->isp_osinfo.timer_active) {
363e95725cbSMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
364e95725cbSMatt Jacob 		isp->isp_osinfo.timer_active = 0;
365e95725cbSMatt Jacob 	}
3661d0a1de2SWill Andrews 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
3671d0a1de2SWill Andrews 		isp_detach_chan(isp, chan);
368387d8239SMatt Jacob 	ISP_UNLOCK(isp);
3691d0a1de2SWill Andrews 
3702df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3712df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3722df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3732df76c16SMatt Jacob 	}
3742df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3752df76c16SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3762df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3772df76c16SMatt Jacob 	}
378a035b0afSMatt Jacob 	if (isp->isp_osinfo.devq != NULL) {
3792df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
3802df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
3810a70657fSMatt Jacob 	}
382e95725cbSMatt Jacob 	return (0);
3835d571944SMatt Jacob }
3845d571944SMatt Jacob 
385f7c631bcSMatt Jacob static void
386e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
387fdeb9f2fSMatt Jacob {
3882df76c16SMatt Jacob 	if (IS_FC(isp)) {
3892df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
3902df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
391b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
392e561aa79SAlexander Motin 			    "Chan %d Freeze simq (loopdown)", chan);
3932df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
394c07b9e07SAlexander Motin 			xpt_hold_boot();
3952df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
396fdeb9f2fSMatt Jacob 		} else {
397b5d5037bSAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
398e561aa79SAlexander Motin 			    "Chan %d Mark simq frozen (loopdown)", chan);
3992df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
4002df76c16SMatt Jacob 		}
401fdeb9f2fSMatt Jacob 	}
402fdeb9f2fSMatt Jacob }
403fdeb9f2fSMatt Jacob 
404e95725cbSMatt Jacob static void
405e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
406e95725cbSMatt Jacob {
407e95725cbSMatt Jacob 	if (IS_FC(isp)) {
408e95725cbSMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
409e95725cbSMatt Jacob 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
410e95725cbSMatt Jacob 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
411e95725cbSMatt Jacob 		if (wasfrozen && fc->simqfrozen == 0) {
412e561aa79SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
413e561aa79SAlexander Motin 			    "Chan %d Release simq", chan);
414e95725cbSMatt Jacob 			xpt_release_simq(fc->sim, 1);
415c07b9e07SAlexander Motin 			xpt_release_boot();
416e95725cbSMatt Jacob 		}
417e95725cbSMatt Jacob 	}
418e95725cbSMatt Jacob }
419e95725cbSMatt Jacob 
4209cd7268eSMatt Jacob 
4215d571944SMatt Jacob static int
4222df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
4235d571944SMatt Jacob {
4249cd7268eSMatt Jacob 	ispsoftc_t *isp;
4252df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
4265d571944SMatt Jacob 
4272df76c16SMatt Jacob 	isp = dev->si_drv1;
4285d571944SMatt Jacob 
429c1504bc0SMatt Jacob 	switch (c) {
4305d571944SMatt Jacob 	case ISP_SDBLEV:
4315d571944SMatt Jacob 	{
4325d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
4335d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
4345d571944SMatt Jacob 		*(int *)addr = olddblev;
4355d571944SMatt Jacob 		retval = 0;
4365d571944SMatt Jacob 		break;
4375d571944SMatt Jacob 	}
438746e9c85SMatt Jacob 	case ISP_GETROLE:
4392df76c16SMatt Jacob 		chan = *(int *)addr;
4402df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4412df76c16SMatt Jacob 			retval = -ENXIO;
4422df76c16SMatt Jacob 			break;
4432df76c16SMatt Jacob 		}
4442df76c16SMatt Jacob 		if (IS_FC(isp)) {
4452df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4462df76c16SMatt Jacob 		} else {
4473e6deb33SAlexander Motin 			*(int *)addr = ISP_ROLE_INITIATOR;
4482df76c16SMatt Jacob 		}
449746e9c85SMatt Jacob 		retval = 0;
450746e9c85SMatt Jacob 		break;
451746e9c85SMatt Jacob 	case ISP_SETROLE:
4523e6deb33SAlexander Motin 		if (IS_SCSI(isp))
4533e6deb33SAlexander Motin 			break;
454746e9c85SMatt Jacob 		nr = *(int *)addr;
4552df76c16SMatt Jacob 		chan = nr >> 8;
4562df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4572df76c16SMatt Jacob 			retval = -ENXIO;
4582df76c16SMatt Jacob 			break;
4592df76c16SMatt Jacob 		}
4602df76c16SMatt Jacob 		nr &= 0xff;
461746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
462746e9c85SMatt Jacob 			retval = EINVAL;
463746e9c85SMatt Jacob 			break;
464746e9c85SMatt Jacob 		}
4652df76c16SMatt Jacob 		ISP_LOCK(isp);
4666bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4676bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4686bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4692df76c16SMatt Jacob 		retval = 0;
4702df76c16SMatt Jacob 		break;
4712df76c16SMatt Jacob 
4725d571944SMatt Jacob 	case ISP_RESETHBA:
473511ced9bSMatt Jacob 		ISP_LOCK(isp);
4742df76c16SMatt Jacob 		isp_reinit(isp, 0);
475511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4765d571944SMatt Jacob 		retval = 0;
4775d571944SMatt Jacob 		break;
4782df76c16SMatt Jacob 
479f553351eSMatt Jacob 	case ISP_RESCAN:
4805d571944SMatt Jacob 		if (IS_FC(isp)) {
4812df76c16SMatt Jacob 			chan = *(int *)addr;
4822df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4832df76c16SMatt Jacob 				retval = -ENXIO;
4842df76c16SMatt Jacob 				break;
4852df76c16SMatt Jacob 			}
486511ced9bSMatt Jacob 			ISP_LOCK(isp);
487e561aa79SAlexander Motin 			if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4885d571944SMatt Jacob 				retval = EIO;
4895d571944SMatt Jacob 			} else {
4905d571944SMatt Jacob 				retval = 0;
4915d571944SMatt Jacob 			}
492511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4935d571944SMatt Jacob 		}
4945d571944SMatt Jacob 		break;
4952df76c16SMatt Jacob 
4965d571944SMatt Jacob 	case ISP_FC_LIP:
4975d571944SMatt Jacob 		if (IS_FC(isp)) {
4982df76c16SMatt Jacob 			chan = *(int *)addr;
4992df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
5002df76c16SMatt Jacob 				retval = -ENXIO;
5012df76c16SMatt Jacob 				break;
5022df76c16SMatt Jacob 			}
503511ced9bSMatt Jacob 			ISP_LOCK(isp);
5042df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
5055d571944SMatt Jacob 				retval = EIO;
5065d571944SMatt Jacob 			} else {
5075d571944SMatt Jacob 				retval = 0;
5085d571944SMatt Jacob 			}
509511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
5105d571944SMatt Jacob 		}
5115d571944SMatt Jacob 		break;
5125d571944SMatt Jacob 	case ISP_FC_GETDINFO:
5135d571944SMatt Jacob 	{
5145d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
51510365e5aSMatt Jacob 		fcportdb_t *lp;
5165d571944SMatt Jacob 
51702e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
51802e2b2d9SMatt Jacob 			break;
51902e2b2d9SMatt Jacob 		}
5202e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
5215d571944SMatt Jacob 			retval = EINVAL;
5225d571944SMatt Jacob 			break;
5235d571944SMatt Jacob 		}
5242df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
525e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
526387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
52710365e5aSMatt Jacob 			ifc->loopid = lp->handle;
5285d571944SMatt Jacob 			ifc->portid = lp->portid;
5295d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
5305d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
5315d571944SMatt Jacob 			retval = 0;
5325d571944SMatt Jacob 		} else {
5335d571944SMatt Jacob 			retval = ENODEV;
5345d571944SMatt Jacob 		}
5355d571944SMatt Jacob 		break;
5365d571944SMatt Jacob 	}
5372903b272SMatt Jacob 	case ISP_GET_STATS:
5382903b272SMatt Jacob 	{
5392903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
5402903b272SMatt Jacob 
5412df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
5422903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
5432903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
5442903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
545511ced9bSMatt Jacob 		ISP_LOCK(isp);
5462903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
5472903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
5482903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
5492903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
5502903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
5512903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
5522903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
5532903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
554511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5552903b272SMatt Jacob 		retval = 0;
5562903b272SMatt Jacob 		break;
5572903b272SMatt Jacob 	}
5582903b272SMatt Jacob 	case ISP_CLR_STATS:
559511ced9bSMatt Jacob 		ISP_LOCK(isp);
5602903b272SMatt Jacob 		isp->isp_intcnt = 0;
5612903b272SMatt Jacob 		isp->isp_intbogus = 0;
5622903b272SMatt Jacob 		isp->isp_intmboxc = 0;
5632903b272SMatt Jacob 		isp->isp_intoasync = 0;
5642903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
5652903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
5662903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
5672903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
568511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5692903b272SMatt Jacob 		retval = 0;
5702903b272SMatt Jacob 		break;
571570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
572570c7a3fSMatt Jacob 	{
573570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5742df76c16SMatt Jacob 		int chan = hba->fc_channel;
57502e2b2d9SMatt Jacob 
5762df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5772df76c16SMatt Jacob 			retval = ENXIO;
5782df76c16SMatt Jacob 			break;
5792df76c16SMatt Jacob 		}
580a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
581a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
582a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5832df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
58402e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5852df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5862df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5872df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5882df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5892df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5902df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5912df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5922df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5932df76c16SMatt Jacob 		} else {
5942df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5952df76c16SMatt Jacob 			hba->fc_speed = 0;
5962df76c16SMatt Jacob 			hba->fc_topology = 0;
5972df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5982df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5992df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
6002df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
60102e2b2d9SMatt Jacob 		}
602570c7a3fSMatt Jacob 		retval = 0;
603570c7a3fSMatt Jacob 		break;
604570c7a3fSMatt Jacob 	}
6058e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
6068e62a8acSMatt Jacob 	{
6078e62a8acSMatt Jacob 		int needmarker;
6088e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
609c5fd36edSAlexander Motin 		uint16_t nphdl;
6108e62a8acSMatt Jacob 		mbreg_t mbs;
6118e62a8acSMatt Jacob 
6128e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
6138e62a8acSMatt Jacob 			break;
6148e62a8acSMatt Jacob 		}
6158e62a8acSMatt Jacob 
6162df76c16SMatt Jacob 		chan = fct->chan;
6172df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
6182df76c16SMatt Jacob 			retval = -ENXIO;
6192df76c16SMatt Jacob 			break;
6202df76c16SMatt Jacob 		}
6212df76c16SMatt Jacob 
6228e62a8acSMatt Jacob 		needmarker = retval = 0;
623c5fd36edSAlexander Motin 		nphdl = fct->loopid;
6242df76c16SMatt Jacob 		ISP_LOCK(isp);
6252df76c16SMatt Jacob 		if (IS_24XX(isp)) {
62653791a95SAlexander Motin 			void *reqp;
62753791a95SAlexander Motin 			uint8_t resp[QENTRY_LEN];
62853791a95SAlexander Motin 			isp24xx_tmf_t tmf;
62953791a95SAlexander Motin 			isp24xx_statusreq_t sp;
6302df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
6312df76c16SMatt Jacob 			fcportdb_t *lp;
6322df76c16SMatt Jacob 			int i;
6332df76c16SMatt Jacob 
6342df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
6352df76c16SMatt Jacob 				lp = &fcp->portdb[i];
636c5fd36edSAlexander Motin 				if (lp->handle == nphdl) {
6372df76c16SMatt Jacob 					break;
6382df76c16SMatt Jacob 				}
6392df76c16SMatt Jacob 			}
6402df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
6412df76c16SMatt Jacob 				retval = ENXIO;
6422df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6432df76c16SMatt Jacob 				break;
6442df76c16SMatt Jacob 			}
64553791a95SAlexander Motin 			ISP_MEMZERO(&tmf, sizeof(tmf));
64653791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
64753791a95SAlexander Motin 			tmf.tmf_header.rqs_entry_count = 1;
64853791a95SAlexander Motin 			tmf.tmf_nphdl = lp->handle;
64953791a95SAlexander Motin 			tmf.tmf_delay = 2;
65053791a95SAlexander Motin 			tmf.tmf_timeout = 4;
65153791a95SAlexander Motin 			tmf.tmf_tidlo = lp->portid;
65253791a95SAlexander Motin 			tmf.tmf_tidhi = lp->portid >> 16;
65353791a95SAlexander Motin 			tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
65453791a95SAlexander Motin 			tmf.tmf_lun[1] = fct->lun & 0xff;
6552df76c16SMatt Jacob 			if (fct->lun >= 256) {
65653791a95SAlexander Motin 				tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
6572df76c16SMatt Jacob 			}
6582df76c16SMatt Jacob 			switch (fct->action) {
6592df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
66053791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
6612df76c16SMatt Jacob 				break;
6622df76c16SMatt Jacob 			case IPT_TARGET_RESET:
66353791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
6642df76c16SMatt Jacob 				needmarker = 1;
6652df76c16SMatt Jacob 				break;
6662df76c16SMatt Jacob 			case IPT_LUN_RESET:
66753791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
6682df76c16SMatt Jacob 				needmarker = 1;
6692df76c16SMatt Jacob 				break;
6702df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
67153791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6722df76c16SMatt Jacob 				needmarker = 1;
6732df76c16SMatt Jacob 				break;
6742df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
67553791a95SAlexander Motin 				tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6762df76c16SMatt Jacob 				needmarker = 1;
6772df76c16SMatt Jacob 				break;
6782df76c16SMatt Jacob 			default:
6792df76c16SMatt Jacob 				retval = EINVAL;
6802df76c16SMatt Jacob 				break;
6812df76c16SMatt Jacob 			}
6822df76c16SMatt Jacob 			if (retval) {
6832df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6842df76c16SMatt Jacob 				break;
6852df76c16SMatt Jacob 			}
6862df76c16SMatt Jacob 
68753791a95SAlexander Motin 			/* Prepare space for response in memory */
68853791a95SAlexander Motin 			memset(resp, 0xff, sizeof(resp));
68953791a95SAlexander Motin 			tmf.tmf_handle = isp_allocate_handle(isp, resp,
69053791a95SAlexander Motin 			    ISP_HANDLE_CTRL);
69153791a95SAlexander Motin 			if (tmf.tmf_handle == 0) {
69253791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
69353791a95SAlexander Motin 				    "%s: TMF of Chan %d out of handles",
69453791a95SAlexander Motin 				    __func__, chan);
6952df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6962df76c16SMatt Jacob 				retval = ENOMEM;
6972df76c16SMatt Jacob 				break;
6982df76c16SMatt Jacob 			}
69953791a95SAlexander Motin 
70053791a95SAlexander Motin 			/* Send request and wait for response. */
70153791a95SAlexander Motin 			reqp = isp_getrqentry(isp);
70253791a95SAlexander Motin 			if (reqp == NULL) {
70353791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
70453791a95SAlexander Motin 				    "%s: TMF of Chan %d out of rqent",
70553791a95SAlexander Motin 				    __func__, chan);
70653791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
70753791a95SAlexander Motin 				ISP_UNLOCK(isp);
7082df76c16SMatt Jacob 				retval = EIO;
70953791a95SAlexander Motin 				break;
7102df76c16SMatt Jacob 			}
71153791a95SAlexander Motin 			isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
71253791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
71353791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
71453791a95SAlexander Motin 			ISP_SYNC_REQUEST(isp);
71553791a95SAlexander Motin 			if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
71653791a95SAlexander Motin 				isp_prt(isp, ISP_LOGERR,
71753791a95SAlexander Motin 				    "%s: TMF of Chan %d timed out",
71853791a95SAlexander Motin 				    __func__, chan);
71953791a95SAlexander Motin 				isp_destroy_handle(isp, tmf.tmf_handle);
72053791a95SAlexander Motin 				ISP_UNLOCK(isp);
72153791a95SAlexander Motin 				retval = EIO;
72253791a95SAlexander Motin 				break;
72353791a95SAlexander Motin 			}
72453791a95SAlexander Motin 			if (isp->isp_dblev & ISP_LOGDEBUG1)
72553791a95SAlexander Motin 				isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
72653791a95SAlexander Motin 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
72753791a95SAlexander Motin 
72853791a95SAlexander Motin 			if (sp.req_completion_status != 0)
72953791a95SAlexander Motin 				retval = EIO;
73053791a95SAlexander Motin 			else if (needmarker)
7312df76c16SMatt Jacob 				fcp->sendmarker = 1;
7322df76c16SMatt Jacob 		} else {
7332df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
7342df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
735c5fd36edSAlexander Motin 				nphdl <<= 8;
736e5265237SMatt Jacob 			}
7378e62a8acSMatt Jacob 			switch (fct->action) {
738f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
7398e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
740c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7418e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7428e62a8acSMatt Jacob 				break;
743f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
7448e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
745c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7468e62a8acSMatt Jacob 				needmarker = 1;
7478e62a8acSMatt Jacob 				break;
748f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
7498e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
750c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7518e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7528e62a8acSMatt Jacob 				needmarker = 1;
7538e62a8acSMatt Jacob 				break;
754f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
7558e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
756c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7578e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7588e62a8acSMatt Jacob 				needmarker = 1;
7598e62a8acSMatt Jacob 				break;
760f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
7618e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
762c5fd36edSAlexander Motin 				mbs.param[1] = nphdl;
7638e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7648e62a8acSMatt Jacob 				needmarker = 1;
7658e62a8acSMatt Jacob 				break;
7668e62a8acSMatt Jacob 			default:
7678e62a8acSMatt Jacob 				retval = EINVAL;
7688e62a8acSMatt Jacob 				break;
7698e62a8acSMatt Jacob 			}
7708e62a8acSMatt Jacob 			if (retval == 0) {
7718e62a8acSMatt Jacob 				if (needmarker) {
7722df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7738e62a8acSMatt Jacob 				}
7748e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7752df76c16SMatt Jacob 				if (retval) {
7768e62a8acSMatt Jacob 					retval = EIO;
7778e62a8acSMatt Jacob 				}
7782df76c16SMatt Jacob 			}
7792df76c16SMatt Jacob 		}
7802df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7818e62a8acSMatt Jacob 		break;
7828e62a8acSMatt Jacob 	}
7835d571944SMatt Jacob 	default:
7845d571944SMatt Jacob 		break;
7855d571944SMatt Jacob 	}
7865d571944SMatt Jacob 	return (retval);
7870470d791SMatt Jacob }
788478f8a96SJustin T. Gibbs 
789f6e75de2SMatt Jacob static void
790f6e75de2SMatt Jacob isp_intr_enable(void *arg)
791f6e75de2SMatt Jacob {
7922df76c16SMatt Jacob 	int chan;
7939cd7268eSMatt Jacob 	ispsoftc_t *isp = arg;
7940a70657fSMatt Jacob 	ISP_LOCK(isp);
7952df76c16SMatt Jacob 	if (IS_FC(isp)) {
7963e6deb33SAlexander Motin 		for (chan = 0; chan < isp->isp_nchan; chan++) {
7972df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
79810365e5aSMatt Jacob 				ISP_ENABLE_INTS(isp);
7992df76c16SMatt Jacob 				break;
8002df76c16SMatt Jacob 			}
8013e6deb33SAlexander Motin 		}
8022df76c16SMatt Jacob 	} else {
8032df76c16SMatt Jacob 		ISP_ENABLE_INTS(isp);
804d6e5500fSMatt Jacob 	}
805e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 0;
8060a70657fSMatt Jacob 	ISP_UNLOCK(isp);
807f6e75de2SMatt Jacob 	/* Release our hook so that the boot can continue. */
808f6e75de2SMatt Jacob 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
809f6e75de2SMatt Jacob }
810d81ba9d5SMatt Jacob 
811d81ba9d5SMatt Jacob /*
8122df76c16SMatt Jacob  * Local Inlines
8132df76c16SMatt Jacob  */
8142df76c16SMatt Jacob 
8152df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
8162df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
8172df76c16SMatt Jacob 
8182df76c16SMatt Jacob static ISP_INLINE int
8192df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
8202df76c16SMatt Jacob {
8212df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
8222df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
8232df76c16SMatt Jacob 		return (-1);
8242df76c16SMatt Jacob 	}
8252df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
8262df76c16SMatt Jacob 	return (0);
8272df76c16SMatt Jacob }
8282df76c16SMatt Jacob 
8292df76c16SMatt Jacob static ISP_INLINE void
8302df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
8312df76c16SMatt Jacob {
832387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
833405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
834405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
835405b7a29SMatt Jacob 		PISP_PCMD(ccb)->totslen = 0;
836405b7a29SMatt Jacob 		PISP_PCMD(ccb)->cumslen = 0;
837405b7a29SMatt Jacob 		PISP_PCMD(ccb)->crn = 0;
838405b7a29SMatt Jacob #endif
839405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
8402df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
8412df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
8422df76c16SMatt Jacob 	}
843387d8239SMatt Jacob }
844387d8239SMatt Jacob 
8452df76c16SMatt Jacob /*
846d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
847d81ba9d5SMatt Jacob  */
848d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
8492df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
8508290ea90SAlexander Motin static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
8518290ea90SAlexander Motin static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
8528290ea90SAlexander Motin static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
8538290ea90SAlexander Motin static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
8548290ea90SAlexander Motin static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
8558290ea90SAlexander Motin static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
8562df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
8578290ea90SAlexander Motin static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
8582df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
8592df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
860f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
861387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
862a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
863a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
86494dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
865387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
8662df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
8672df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
8682df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
8692df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
8702df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
87196b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
8722df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
8738290ea90SAlexander Motin static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
874d81ba9d5SMatt Jacob 
8752df76c16SMatt Jacob static ISP_INLINE tstate_t *
8769cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
877d81ba9d5SMatt Jacob {
87864edff94SMatt Jacob 	tstate_t *tptr = NULL;
8792df76c16SMatt Jacob 	struct tslist *lhp;
880d81ba9d5SMatt Jacob 
8812df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
882315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
8832df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
8848290ea90SAlexander Motin 			if (tptr->ts_lun == lun)
885d81ba9d5SMatt Jacob 				return (tptr);
886d81ba9d5SMatt Jacob 		}
88764edff94SMatt Jacob 	}
8882df76c16SMatt Jacob 	return (NULL);
8892df76c16SMatt Jacob }
890d81ba9d5SMatt Jacob 
8918290ea90SAlexander Motin static int
8928290ea90SAlexander Motin isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
8932df76c16SMatt Jacob {
8942df76c16SMatt Jacob 	inot_private_data_t *ntp;
8958290ea90SAlexander Motin 	struct ntpdlist rq;
8962df76c16SMatt Jacob 
8978290ea90SAlexander Motin 	if (STAILQ_EMPTY(&tptr->restart_queue))
8988290ea90SAlexander Motin 		return (0);
8998290ea90SAlexander Motin 	STAILQ_INIT(&rq);
9008290ea90SAlexander Motin 	STAILQ_CONCAT(&rq, &tptr->restart_queue);
9018290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
9028290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&rq, next);
9038290ea90SAlexander Motin 		if (IS_24XX(isp)) {
9048290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
9058290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
9068290ea90SAlexander Motin 			    ((at7_entry_t *)ntp->data)->at_rxid);
9078290ea90SAlexander Motin 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
9088290ea90SAlexander Motin 		} else {
9098290ea90SAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0,
9108290ea90SAlexander Motin 			    "%s: restarting resrc deprived %x", __func__,
9118290ea90SAlexander Motin 			    ((at2_entry_t *)ntp->data)->at_rxid);
9128290ea90SAlexander Motin 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
9132df76c16SMatt Jacob 		}
9148290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
9158290ea90SAlexander Motin 		if (!STAILQ_EMPTY(&tptr->restart_queue))
9168290ea90SAlexander Motin 			break;
9172df76c16SMatt Jacob 	}
9188290ea90SAlexander Motin 	if (!STAILQ_EMPTY(&rq)) {
9198290ea90SAlexander Motin 		STAILQ_CONCAT(&rq, &tptr->restart_queue);
9208290ea90SAlexander Motin 		STAILQ_CONCAT(&tptr->restart_queue, &rq);
9212df76c16SMatt Jacob 	}
9228290ea90SAlexander Motin 	return (!STAILQ_EMPTY(&tptr->restart_queue));
923d81ba9d5SMatt Jacob }
924d81ba9d5SMatt Jacob 
9252df76c16SMatt Jacob static void
9262df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
9272df76c16SMatt Jacob {
9282df76c16SMatt Jacob 	tstate_t *tptr;
92994dff771SMatt Jacob 	union ccb *ccb;
9302df76c16SMatt Jacob 	struct tslist *lhp;
93109ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
9322df76c16SMatt Jacob 	int bus, i;
9332df76c16SMatt Jacob 
9342df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9352df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9362df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9378290ea90SAlexander Motin 			SLIST_FOREACH(tptr, lhp, next)
9388290ea90SAlexander Motin 				isp_atio_restart(isp, bus, tptr);
93909ddc7adSAlexander Motin 		}
94009ddc7adSAlexander Motin 
94194dff771SMatt Jacob 		/*
94209ddc7adSAlexander Motin 		 * We only need to do this once per channel.
94394dff771SMatt Jacob 		 */
94409ddc7adSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
94509ddc7adSAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(waitq);
94609ddc7adSAlexander Motin 		if (ccb != NULL) {
94709ddc7adSAlexander Motin 			TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
94894dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
94994dff771SMatt Jacob 		}
9502df76c16SMatt Jacob 	}
9512df76c16SMatt Jacob }
9522df76c16SMatt Jacob 
9538290ea90SAlexander Motin static atio_private_data_t *
9548290ea90SAlexander Motin isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
95553036e92SMatt Jacob {
9568290ea90SAlexander Motin 	struct atpdlist *atfree;
9578290ea90SAlexander Motin 	struct atpdlist *atused;
95853036e92SMatt Jacob 	atio_private_data_t *atp;
9592df76c16SMatt Jacob 
9608290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
9618290ea90SAlexander Motin 	atp = LIST_FIRST(atfree);
9622df76c16SMatt Jacob 	if (atp) {
963523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
964523ea374SAlexander Motin 		atp->tag = tag;
9658290ea90SAlexander Motin 		ISP_GET_PC(isp, chan, atused, atused);
9668290ea90SAlexander Motin 		LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
9672df76c16SMatt Jacob 	}
96853036e92SMatt Jacob 	return (atp);
96953036e92SMatt Jacob }
970523ea374SAlexander Motin 
9718290ea90SAlexander Motin static atio_private_data_t *
9728290ea90SAlexander Motin isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
973523ea374SAlexander Motin {
9748290ea90SAlexander Motin 	struct atpdlist *atused;
975523ea374SAlexander Motin 	atio_private_data_t *atp;
976523ea374SAlexander Motin 
9778290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atused, atused);
9788290ea90SAlexander Motin 	LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
979523ea374SAlexander Motin 		if (atp->tag == tag)
9802df76c16SMatt Jacob 			return (atp);
9812df76c16SMatt Jacob 	}
98253036e92SMatt Jacob 	return (NULL);
98353036e92SMatt Jacob }
98453036e92SMatt Jacob 
9858290ea90SAlexander Motin static void
9868290ea90SAlexander Motin isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
9872df76c16SMatt Jacob {
9888290ea90SAlexander Motin 	struct atpdlist *atfree;
9898290ea90SAlexander Motin 
990387d8239SMatt Jacob 	if (atp->ests) {
991387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
992387d8239SMatt Jacob 	}
993523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
994387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
9958290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
9968290ea90SAlexander Motin 	LIST_INSERT_HEAD(atfree, atp, next);
9972df76c16SMatt Jacob }
9982df76c16SMatt Jacob 
9992df76c16SMatt Jacob static void
10008290ea90SAlexander Motin isp_dump_atpd(ispsoftc_t *isp, int chan)
10012df76c16SMatt Jacob {
10028290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
10032df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
10042df76c16SMatt Jacob 
10058290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
10068290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
1007cfbeb41cSAlexander Motin 		if (atp->state == ATPD_STATE_FREE)
1008cfbeb41cSAlexander Motin 			continue;
1009cfbeb41cSAlexander 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",
10103f072d69SAlexander 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]);
10112df76c16SMatt Jacob 	}
10122df76c16SMatt Jacob }
10132df76c16SMatt Jacob 
10148290ea90SAlexander Motin static inot_private_data_t *
10158290ea90SAlexander Motin isp_get_ntpd(ispsoftc_t *isp, int chan)
10162df76c16SMatt Jacob {
10178290ea90SAlexander Motin 	struct ntpdlist *ntfree;
10182df76c16SMatt Jacob 	inot_private_data_t *ntp;
10198290ea90SAlexander Motin 
10208290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
10218290ea90SAlexander Motin 	ntp = STAILQ_FIRST(ntfree);
10228290ea90SAlexander Motin 	if (ntp)
10238290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(ntfree, next);
10242df76c16SMatt Jacob 	return (ntp);
10252df76c16SMatt Jacob }
10262df76c16SMatt Jacob 
10278290ea90SAlexander Motin static inot_private_data_t *
10288290ea90SAlexander Motin isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
10292df76c16SMatt Jacob {
10308290ea90SAlexander Motin 	inot_private_data_t *ntp, *ntp2;
10318290ea90SAlexander Motin 
10328290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, ntpool, ntp);
10338290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
10348290ea90SAlexander Motin 	for (; ntp < ntp2; ntp++) {
10358290ea90SAlexander Motin 		if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
10362df76c16SMatt Jacob 			return (ntp);
10372df76c16SMatt Jacob 	}
10382df76c16SMatt Jacob 	return (NULL);
10392df76c16SMatt Jacob }
10402df76c16SMatt Jacob 
10418290ea90SAlexander Motin static void
10428290ea90SAlexander Motin isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
10432df76c16SMatt Jacob {
10448290ea90SAlexander Motin 	struct ntpdlist *ntfree;
10458290ea90SAlexander Motin 
10468290ea90SAlexander Motin 	ntp->tag_id = ntp->seq_id = 0;
10478290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
10488290ea90SAlexander Motin 	STAILQ_INSERT_HEAD(ntfree, ntp, next);
10492df76c16SMatt Jacob }
10502df76c16SMatt Jacob 
1051d81ba9d5SMatt Jacob static cam_status
10522df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1053d81ba9d5SMatt Jacob {
1054d81ba9d5SMatt Jacob 	lun_id_t lun;
10552df76c16SMatt Jacob 	struct tslist *lhp;
10562df76c16SMatt Jacob 	tstate_t *tptr;
1057d81ba9d5SMatt Jacob 
1058d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
10592df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
10606af11b82SAlexander Motin 		if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1061d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
1062d81ba9d5SMatt Jacob 		}
10632df76c16SMatt Jacob 	}
1064e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
10652df76c16SMatt Jacob 	if (tptr == NULL) {
1066d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
1067d81ba9d5SMatt Jacob 	}
1068315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
10692df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
10702df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
1071315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
10722df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
10732df76c16SMatt Jacob 	*rslt = tptr;
10742df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1075d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1076d81ba9d5SMatt Jacob }
1077d81ba9d5SMatt Jacob 
10788290ea90SAlexander Motin static void
10798290ea90SAlexander Motin destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
1080d81ba9d5SMatt Jacob {
1081344aebe2SMatt Jacob 	union ccb *ccb;
10822df76c16SMatt Jacob 	struct tslist *lhp;
10838290ea90SAlexander Motin 	inot_private_data_t *ntp;
1084e2873b76SMatt Jacob 
10858290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
1086344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1087344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1088344aebe2SMatt Jacob 		xpt_done(ccb);
10898290ea90SAlexander Motin 	};
10908290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
1091344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1092344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1093344aebe2SMatt Jacob 		xpt_done(ccb);
1094344aebe2SMatt Jacob 	}
10958290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
10968290ea90SAlexander Motin 		isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
10978290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
10988290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
10998290ea90SAlexander Motin 	}
11008290ea90SAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
11012df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1102d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1103d81ba9d5SMatt Jacob }
1104d81ba9d5SMatt Jacob 
11052df76c16SMatt Jacob static void
11062df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1107d81ba9d5SMatt Jacob {
11083e6deb33SAlexander Motin 	tstate_t *tptr;
11093e6deb33SAlexander Motin 	int bus;
11102df76c16SMatt Jacob 	target_id_t target;
1111d81ba9d5SMatt Jacob 	lun_id_t lun;
1112d81ba9d5SMatt Jacob 
11133e6deb33SAlexander Motin 	if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
11143e6deb33SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
11153e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
11163e6deb33SAlexander Motin 		xpt_done(ccb);
11173e6deb33SAlexander Motin 		return;
11183e6deb33SAlexander Motin 	}
1119e2873b76SMatt Jacob 
1120746e9c85SMatt Jacob 	/*
11213e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
11223e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
1123746e9c85SMatt Jacob 	 */
11242df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
11252df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
11262df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
11276af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
11286af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
11293e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
11302df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
11312df76c16SMatt Jacob 		xpt_done(ccb);
11322df76c16SMatt Jacob 		return;
11332df76c16SMatt Jacob 	}
11342df76c16SMatt Jacob 
11353e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
1136a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
11372df76c16SMatt Jacob 	if (tptr) {
11382df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
11393e6deb33SAlexander Motin 		xpt_done(ccb);
11403e6deb33SAlexander Motin 		return;
1141d81ba9d5SMatt Jacob 	}
11422df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
11432df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
11442df76c16SMatt Jacob 		xpt_done(ccb);
11453e6deb33SAlexander Motin 		return;
11462df76c16SMatt Jacob 	}
11472df76c16SMatt Jacob 
11483e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
11493e6deb33SAlexander Motin 	xpt_done(ccb);
11502df76c16SMatt Jacob }
11512df76c16SMatt Jacob 
11522df76c16SMatt Jacob static void
11532df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
11542df76c16SMatt Jacob {
11552df76c16SMatt Jacob 	tstate_t *tptr = NULL;
11562df76c16SMatt Jacob 	int bus;
11572df76c16SMatt Jacob 	target_id_t target;
11582df76c16SMatt Jacob 	lun_id_t lun;
11592df76c16SMatt Jacob 
11602df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
11612df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
11622df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
11636af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
11646af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
11653e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
11662df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
11672df76c16SMatt Jacob 		xpt_done(ccb);
11682df76c16SMatt Jacob 		return;
11692df76c16SMatt Jacob 	}
11702df76c16SMatt Jacob 
11713e6deb33SAlexander Motin 	/* Find the state pointer. */
11722df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
11733e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
11742df76c16SMatt Jacob 		xpt_done(ccb);
11753e6deb33SAlexander Motin 		return;
11762df76c16SMatt Jacob 	}
11772df76c16SMatt Jacob 
11788290ea90SAlexander Motin 	destroy_lun_state(isp, bus, tptr);
11793e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
11803e6deb33SAlexander Motin 	xpt_done(ccb);
1181d81ba9d5SMatt Jacob }
1182d81ba9d5SMatt Jacob 
11839cd7268eSMatt Jacob static void
1184387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1185d81ba9d5SMatt Jacob {
118694dff771SMatt Jacob 	int fctape, sendstatus, resid;
1187387d8239SMatt Jacob 	fcparam *fcp;
11882df76c16SMatt Jacob 	atio_private_data_t *atp;
118994dff771SMatt Jacob 	struct ccb_scsiio *cso;
119009ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
119194dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
11921dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1193d81ba9d5SMatt Jacob 
119494dff771SMatt 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,
119594dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
119694dff771SMatt Jacob 
119709ddc7adSAlexander Motin 	ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
119894dff771SMatt Jacob 	switch (how) {
119994dff771SMatt Jacob 	case FROM_CAM:
12002df76c16SMatt Jacob 		/*
120194dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
12022df76c16SMatt Jacob 		 */
120309ddc7adSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
120494dff771SMatt Jacob 		break;
120509ddc7adSAlexander Motin 	case FROM_TIMER:
120694dff771SMatt Jacob 	case FROM_SRR:
120794dff771SMatt Jacob 	case FROM_CTIO_DONE:
120809ddc7adSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
120994dff771SMatt Jacob 		break;
121094dff771SMatt Jacob 	}
121194dff771SMatt Jacob 
121209ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
121309ddc7adSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
121494dff771SMatt Jacob 
121594dff771SMatt Jacob 		cso = &ccb->csio;
1216387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1217387d8239SMatt Jacob 		if (xfrlen == 0) {
12182df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1219387d8239SMatt 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");
12202df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
12212df76c16SMatt Jacob 				xpt_done(ccb);
122294dff771SMatt Jacob 				continue;
12232df76c16SMatt Jacob 			}
12242df76c16SMatt Jacob 		}
12252df76c16SMatt Jacob 
12268290ea90SAlexander Motin 		atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
12272df76c16SMatt Jacob 		if (atp == NULL) {
122894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
12298290ea90SAlexander Motin 			isp_dump_atpd(isp, XS_CHANNEL(ccb));
12302df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
12312df76c16SMatt Jacob 			xpt_done(ccb);
123294dff771SMatt Jacob 			continue;
12332df76c16SMatt Jacob 		}
1234387d8239SMatt Jacob 
1235387d8239SMatt Jacob 		/*
1236387d8239SMatt Jacob 		 * Is this command a dead duck?
1237387d8239SMatt Jacob 		 */
12382df76c16SMatt Jacob 		if (atp->dead) {
123994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
12402df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
12412df76c16SMatt Jacob 			xpt_done(ccb);
124294dff771SMatt Jacob 			continue;
12432df76c16SMatt Jacob 		}
12442df76c16SMatt Jacob 
12452df76c16SMatt Jacob 		/*
12462df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
12472df76c16SMatt Jacob 		 */
1248387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1249387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
125094dff771SMatt 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);
12512df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
12522df76c16SMatt Jacob 			xpt_done(ccb);
125394dff771SMatt Jacob 			continue;
12542df76c16SMatt Jacob 		}
12552df76c16SMatt Jacob 
12562df76c16SMatt Jacob 		/*
125794dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
125894dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1259387d8239SMatt Jacob 		 */
126094dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
126194dff771SMatt 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);
126209ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
126394dff771SMatt Jacob 			break;
1264387d8239SMatt Jacob 		}
1265387d8239SMatt Jacob 
1266d81ba9d5SMatt Jacob 		/*
1267387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1268d81ba9d5SMatt Jacob 		 */
1269387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1270387d8239SMatt Jacob 			fctape = 1;
1271387d8239SMatt Jacob 		} else {
1272387d8239SMatt Jacob 			fctape = 0;
1273387d8239SMatt Jacob 		}
1274387d8239SMatt Jacob 
1275387d8239SMatt Jacob 		/*
1276387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1277387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1278387d8239SMatt Jacob 		 */
1279387d8239SMatt Jacob 		if (atp->sendst) {
1280daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
128194dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1282387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1283387d8239SMatt Jacob 			atp->sendst = 0;
1284387d8239SMatt Jacob 		}
1285387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1286387d8239SMatt Jacob 			sendstatus = 1;
1287387d8239SMatt Jacob 		} else {
1288387d8239SMatt Jacob 			sendstatus = 0;
1289387d8239SMatt Jacob 		}
1290387d8239SMatt Jacob 
1291387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
129294dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1293387d8239SMatt Jacob 			/*
1294387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1295387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1296387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1297387d8239SMatt Jacob 			 */
1298387d8239SMatt Jacob 			sense_length = cso->sense_len;
1299387d8239SMatt Jacob 
1300387d8239SMatt Jacob 			/*
1301387d8239SMatt Jacob 			 * This 'cannot' happen
1302387d8239SMatt Jacob 			 */
1303387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1304387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1305387d8239SMatt Jacob 			}
1306387d8239SMatt Jacob 		} else {
1307387d8239SMatt Jacob 			sense_length = 0;
1308387d8239SMatt Jacob 		}
1309387d8239SMatt Jacob 
131094dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
131194dff771SMatt Jacob 
131294dff771SMatt Jacob 		/*
131394dff771SMatt Jacob 		 * Check for overflow
131494dff771SMatt Jacob 		 */
1315a6036a44SAlexander Motin 		tmp = atp->bytes_xfered + atp->bytes_in_transit;
1316a6036a44SAlexander Motin 		if (xfrlen > 0 && tmp > atp->orig_datalen) {
1317a6036a44SAlexander Motin 			isp_prt(isp, ISP_LOGERR,
1318a6036a44SAlexander Motin 			    "%s: [0x%x] data overflow by %u bytes", __func__,
1319a6036a44SAlexander Motin 			    cso->tag_id, tmp + xfrlen - atp->orig_datalen);
132094dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
132194dff771SMatt Jacob 			xpt_done(ccb);
132294dff771SMatt Jacob 			continue;
132394dff771SMatt Jacob 		}
1324a6036a44SAlexander Motin 		if (xfrlen > atp->orig_datalen - tmp) {
1325a6036a44SAlexander Motin 			xfrlen = atp->orig_datalen - tmp;
1326a6036a44SAlexander Motin 			if (xfrlen == 0 && !sendstatus) {
1327a6036a44SAlexander Motin 				cso->resid = cso->dxfer_len;
1328a6036a44SAlexander Motin 				ccb->ccb_h.status = CAM_REQ_CMP;
1329a6036a44SAlexander Motin 				xpt_done(ccb);
1330a6036a44SAlexander Motin 				continue;
1331a6036a44SAlexander Motin 			}
1332a6036a44SAlexander Motin 		}
1333387d8239SMatt Jacob 
13342df76c16SMatt Jacob 		if (IS_24XX(isp)) {
13352df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1336d81ba9d5SMatt Jacob 
13372df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
13382df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
133994dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
134094dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
13412df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
13422df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
13433f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
13443f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
13452df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
13462df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1347a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
13482df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1349387d8239SMatt Jacob 
1350387d8239SMatt Jacob 			/*
1351387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
13526f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1353387d8239SMatt Jacob 			 *
13548b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
13558b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1356387d8239SMatt Jacob 			 *
1357387d8239SMatt Jacob 			 */
1358387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1359387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
136094dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1361387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1362387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1363387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1364387d8239SMatt Jacob 					if (resid < 0) {
1365a6036a44SAlexander Motin 						cto->ct_resid = -resid;
1366387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1367387d8239SMatt Jacob 					} else if (resid > 0) {
1368a6036a44SAlexander Motin 						cto->ct_resid = resid;
1369387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1370387d8239SMatt Jacob 					}
1371387d8239SMatt Jacob 					if (fctape) {
1372387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1373387d8239SMatt Jacob 					}
1374387d8239SMatt Jacob 					if (sense_length) {
13752df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1376387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1377387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
13782df76c16SMatt Jacob 					}
13792df76c16SMatt Jacob 				} else {
1380387d8239SMatt Jacob 					bus_addr_t addr;
1381387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1382387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1383387d8239SMatt Jacob 
1384387d8239SMatt Jacob 					if (atp->ests == NULL) {
1385387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1386387d8239SMatt Jacob 						if (atp->ests == NULL) {
138709ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
138894dff771SMatt Jacob 							break;
1389387d8239SMatt Jacob 						}
1390387d8239SMatt Jacob 					}
1391387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1392387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1393387d8239SMatt Jacob 					if (fctape) {
1394387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1395387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1396387d8239SMatt Jacob 					}
1397387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1398387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1399387d8239SMatt Jacob 					if (resid < 0) {
1400387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1401387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1402387d8239SMatt Jacob 					} else if (resid > 0) {
1403387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1404387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1405387d8239SMatt Jacob 					}
1406387d8239SMatt Jacob 					if (sense_length) {
1407387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1408387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1409387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1410387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1411387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1412387d8239SMatt Jacob 					} else {
1413387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1414387d8239SMatt Jacob 					}
1415387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1416387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1417387d8239SMatt Jacob 					}
1418387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1419387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
142094dff771SMatt 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,
1421387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1422387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1423387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1424387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1425387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1426387d8239SMatt Jacob 				}
1427387d8239SMatt Jacob 				if (sense_length) {
142894dff771SMatt 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__,
142994dff771SMatt 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,
143094dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1431387d8239SMatt Jacob 				} else {
143294dff771SMatt 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__,
143394dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1434387d8239SMatt Jacob 				}
1435387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1436387d8239SMatt Jacob 			}
1437387d8239SMatt Jacob 
1438387d8239SMatt Jacob 			/*
1439387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1440387d8239SMatt Jacob 			 */
1441387d8239SMatt Jacob 			if (xfrlen != 0) {
14422df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
14432df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
14442df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
14452df76c16SMatt Jacob 				} else {
14462df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
14472df76c16SMatt Jacob 				}
1448387d8239SMatt Jacob 
144994dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1450387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1451387d8239SMatt Jacob 
1452387d8239SMatt Jacob #ifdef	DEBUG
1453387d8239SMatt 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) {
1454387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1455387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1456387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1457387d8239SMatt Jacob 				}
1458387d8239SMatt Jacob #endif
1459387d8239SMatt Jacob 				if (sendstatus) {
146094dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
146194dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1462387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1463387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
146494dff771SMatt Jacob 						if (fctape) {
146594dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
146694dff771SMatt Jacob 						}
1467387d8239SMatt Jacob 					} else {
1468387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
146994dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
14702df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
14712df76c16SMatt Jacob 					}
1472387d8239SMatt Jacob 				} else {
1473387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1474387d8239SMatt Jacob 				}
147594dff771SMatt 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__,
147694dff771SMatt 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);
1477387d8239SMatt Jacob 			}
14783e6deb33SAlexander Motin 		} else {
14794fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
148000a8e174SMatt Jacob 
1481387d8239SMatt Jacob 			if (isp->isp_osinfo.sixtyfourbit)
1482387d8239SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1483387d8239SMatt Jacob 			else
1484d81ba9d5SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1485d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
148694dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
148794dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
1488d4f3ad3aSAlexander Motin 			if (ISP_CAP_2KLOGIN(isp)) {
1489d4f3ad3aSAlexander Motin 				((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
149029f76675SMatt Jacob 			} else {
1491d4f3ad3aSAlexander Motin 				cto->ct_iid = atp->nphdl;
14922df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1493d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
14942ad50ca5SMatt Jacob 				}
149529f76675SMatt Jacob 			}
1496a46709e2SAlexander Motin 			cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
149700a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1498387d8239SMatt Jacob 
1499387d8239SMatt Jacob 			/*
1500387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1501387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1502387d8239SMatt Jacob 			 *
150396240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1504387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1505387d8239SMatt Jacob 			 *
1506387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1507387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1508387d8239SMatt Jacob 			 * needed based upon this.
1509387d8239SMatt Jacob 			 */
1510387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1511387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
151294dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1513387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1514387d8239SMatt Jacob 					if (resid < 0) {
1515387d8239SMatt Jacob 						cto->ct_resid = -resid;
1516387d8239SMatt Jacob 					} else if (resid > 0) {
1517387d8239SMatt Jacob 						cto->ct_resid = resid;
1518387d8239SMatt Jacob 					}
1519387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1520f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1521387d8239SMatt Jacob 					if (resid < 0) {
15222df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1523387d8239SMatt Jacob 					} else if (resid > 0) {
15242df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1525f48ce188SMatt Jacob 					}
1526387d8239SMatt Jacob 					if (fctape) {
1527387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1528387d8239SMatt Jacob 					}
1529387d8239SMatt Jacob 					if (sense_length) {
153000a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1531387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1532387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
153300a8e174SMatt Jacob 					}
153400a8e174SMatt Jacob 				} else {
1535387d8239SMatt Jacob 					bus_addr_t addr;
1536387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1537387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1538387d8239SMatt Jacob 
1539387d8239SMatt Jacob 					if (atp->ests == NULL) {
1540387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1541387d8239SMatt Jacob 						if (atp->ests == NULL) {
154209ddc7adSAlexander Motin 							TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
154394dff771SMatt Jacob 							break;
1544387d8239SMatt Jacob 						}
1545387d8239SMatt Jacob 					}
1546387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1547387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1548387d8239SMatt Jacob 					if (fctape) {
1549387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1550387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1551387d8239SMatt Jacob 					}
1552387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1553387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1554387d8239SMatt Jacob 					if (resid < 0) {
1555387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1556387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1557387d8239SMatt Jacob 					} else if (resid > 0) {
1558387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1559387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1560387d8239SMatt Jacob 					}
1561387d8239SMatt Jacob 					if (sense_length) {
1562387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1563387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1564387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1565387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1566387d8239SMatt Jacob 					} else {
1567387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1568387d8239SMatt Jacob 					}
1569387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1570387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1571387d8239SMatt Jacob 					}
1572387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1573387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
157494dff771SMatt 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,
1575387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1576387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1577387d8239SMatt Jacob 					if (isp->isp_osinfo.sixtyfourbit) {
1578387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1579387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1580387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1581387d8239SMatt Jacob 					} else {
1582387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1583387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1584387d8239SMatt Jacob 					}
1585387d8239SMatt Jacob 				}
1586387d8239SMatt Jacob 				if (sense_length) {
158794dff771SMatt 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__,
158894dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
158994dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1590387d8239SMatt Jacob 				} else {
159194dff771SMatt 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,
159294dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1593387d8239SMatt Jacob 				}
1594387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1595387d8239SMatt Jacob 			}
1596387d8239SMatt Jacob 
1597387d8239SMatt Jacob 			if (xfrlen != 0) {
159800a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
159900a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
160000a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
160100a8e174SMatt Jacob 				} else {
160200a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
1603d81ba9d5SMatt Jacob 				}
160453036e92SMatt Jacob 
160594dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1606387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1607387d8239SMatt Jacob 
1608387d8239SMatt Jacob 				if (sendstatus) {
160994dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
161094dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1611387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
1612387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
161394dff771SMatt Jacob 						if (fctape) {
161494dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
161594dff771SMatt Jacob 						}
1616387d8239SMatt Jacob 					} else {
1617387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
161894dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1619387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
1620387d8239SMatt Jacob 					}
1621387d8239SMatt Jacob 				} else {
1622387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1623387d8239SMatt Jacob 				}
1624387d8239SMatt Jacob 			}
162594dff771SMatt 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,
162694dff771SMatt 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);
1627d81ba9d5SMatt Jacob 		}
1628d81ba9d5SMatt Jacob 
162994dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
163094dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
163109ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
163294dff771SMatt Jacob 			break;
163394dff771SMatt Jacob 		}
1634970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1635970ceb2fSAlexander Motin 		if (handle == 0) {
1636387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
163709ddc7adSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
163894dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
163994dff771SMatt Jacob 			break;
1640d81ba9d5SMatt Jacob 		}
164194dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
164294dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
164394dff771SMatt Jacob 
1644d81ba9d5SMatt Jacob 
1645d81ba9d5SMatt Jacob 		/*
1646d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1647d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1648b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1649d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1650d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1651d81ba9d5SMatt Jacob 		 * format.
1652d81ba9d5SMatt Jacob 		 */
1653d81ba9d5SMatt Jacob 
16542df76c16SMatt Jacob 		if (IS_24XX(isp)) {
16552df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
16562df76c16SMatt Jacob 			cto->ct_syshandle = handle;
165710365e5aSMatt Jacob 		} else {
16583e6deb33SAlexander Motin 			ct2_entry_t *cto = (ct2_entry_t *) local;
165910365e5aSMatt Jacob 			cto->ct_syshandle = handle;
166010365e5aSMatt Jacob 		}
1661a1bc34c6SMatt Jacob 
16622df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
166394dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
1664970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
166594dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
166694dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
166709ddc7adSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
166894dff771SMatt Jacob 				break;
166994dff771SMatt Jacob 			}
167094dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
167194dff771SMatt Jacob 			xpt_done(ccb);
167294dff771SMatt Jacob 			continue;
167394dff771SMatt Jacob 		}
16742df76c16SMatt Jacob 		isp->isp_nactive++;
167594dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1676387d8239SMatt Jacob 		if (xfrlen) {
1677387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1678387d8239SMatt Jacob 		} else {
1679387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1680387d8239SMatt Jacob 		}
1681387d8239SMatt Jacob 		atp->ctcnt++;
168294dff771SMatt Jacob 		atp->seqno++;
16832df76c16SMatt Jacob 	}
1684d81ba9d5SMatt Jacob }
1685d81ba9d5SMatt Jacob 
1686a1bc34c6SMatt Jacob static void
1687a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1688f48ce188SMatt Jacob {
16892df76c16SMatt Jacob 	union ccb *ccb = arg;
16902a0db815SJohn Baldwin 
1691b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
16922df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
1693a1bc34c6SMatt Jacob }
1694a1bc34c6SMatt Jacob 
1695a1bc34c6SMatt Jacob static void
1696387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1697387d8239SMatt Jacob {
1698387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1699387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
17002a0db815SJohn Baldwin 
17012a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1702387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
17032a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1704387d8239SMatt Jacob 	} else {
1705387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1706387d8239SMatt Jacob 	}
1707387d8239SMatt Jacob }
1708387d8239SMatt Jacob 
1709387d8239SMatt Jacob 
1710387d8239SMatt Jacob static void
1711a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1712a1bc34c6SMatt Jacob {
17139cd7268eSMatt Jacob 	ispsoftc_t *isp;
1714a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
1715a1bc34c6SMatt Jacob 	void *qe;
17163e6deb33SAlexander Motin 	at2_entry_t local, *at = &local;
1717a1bc34c6SMatt Jacob 
1718a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
1719f48ce188SMatt Jacob 
17202df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
17212df76c16SMatt Jacob 	if (qe == NULL) {
1722c3167cabSMatt Jacob 		xpt_print(ccb->ccb_h.path,
1723c3167cabSMatt Jacob 		    "%s: Request Queue Overflow\n", __func__);
17242a0db815SJohn Baldwin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
17252a0db815SJohn Baldwin 		    isp_refire_putback_atio, ccb);
1726a1bc34c6SMatt Jacob 		return;
1727f48ce188SMatt Jacob 	}
172829f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
1729a1bc34c6SMatt Jacob 	cso = &ccb->csio;
17302df76c16SMatt Jacob 	ISP_MEMZERO(at, sizeof (at2_entry_t));
1731f48ce188SMatt Jacob 	at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1732f48ce188SMatt Jacob 	at->at_header.rqs_entry_count = 1;
17332df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
1734a1bc34c6SMatt Jacob 		at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1735f48ce188SMatt Jacob 	} else {
1736a1bc34c6SMatt Jacob 		at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1737f48ce188SMatt Jacob 	}
1738f48ce188SMatt Jacob 	at->at_status = CT_OK;
1739a1bc34c6SMatt Jacob 	at->at_rxid = cso->tag_id;
1740570c7a3fSMatt Jacob 	at->at_iid = cso->ccb_h.target_id;
17414fd13c1bSMatt Jacob 	isp_put_atio2(isp, at, qe);
17422df76c16SMatt Jacob 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
17432df76c16SMatt Jacob 	ISP_SYNC_REQUEST(isp);
1744a1bc34c6SMatt Jacob 	isp_complete_ctio(ccb);
1745f48ce188SMatt Jacob }
1746f48ce188SMatt Jacob 
1747f48ce188SMatt Jacob static void
1748a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
1749f48ce188SMatt Jacob {
1750387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1751a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1752a1bc34c6SMatt Jacob 		xpt_done(ccb);
1753f48ce188SMatt Jacob 	}
1754387d8239SMatt Jacob }
1755f48ce188SMatt Jacob 
17562df76c16SMatt Jacob static void
17579cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1758d81ba9d5SMatt Jacob {
175945b9ad7aSAlexander Motin 	fcparam *fcp;
176092a1e549SMatt Jacob 	lun_id_t lun;
17612df76c16SMatt Jacob 	fcportdb_t *lp;
1762d81ba9d5SMatt Jacob 	tstate_t *tptr;
1763d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
17642df76c16SMatt Jacob 	uint16_t nphdl;
1765a035b0afSMatt Jacob 	atio_private_data_t *atp;
17662df76c16SMatt Jacob 	inot_private_data_t *ntp;
1767d81ba9d5SMatt Jacob 
1768d81ba9d5SMatt Jacob 	/*
1769d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1770d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1771d81ba9d5SMatt Jacob 	 *
1772b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1773d81ba9d5SMatt Jacob 	 */
1774d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
17752df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
17768290ea90SAlexander Motin 		isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
17772df76c16SMatt Jacob 		return;
1778d81ba9d5SMatt Jacob 	}
1779d81ba9d5SMatt Jacob 
178045b9ad7aSAlexander Motin 	fcp = FCPARAM(isp, 0);
17812df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
178292a1e549SMatt Jacob 		lun = aep->at_scclun;
17832ad50ca5SMatt Jacob 	} else {
178492a1e549SMatt Jacob 		lun = aep->at_lun;
17852ad50ca5SMatt Jacob 	}
17862df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
17872df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
17882df76c16SMatt Jacob 	} else {
17892df76c16SMatt Jacob 		nphdl = aep->at_iid;
17902df76c16SMatt Jacob 	}
1791a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1792d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1793a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1794746e9c85SMatt Jacob 		if (tptr == NULL) {
1795123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1796387d8239SMatt Jacob 			if (lun == 0) {
17978290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1798387d8239SMatt Jacob 			} else {
17998290ea90SAlexander Motin 				isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1800387d8239SMatt Jacob 			}
18012df76c16SMatt Jacob 			return;
1802746e9c85SMatt Jacob 		}
1803d81ba9d5SMatt Jacob 	}
1804d81ba9d5SMatt Jacob 
1805d81ba9d5SMatt Jacob 	/*
18062df76c16SMatt Jacob 	 * Start any commands pending resources first.
1807d81ba9d5SMatt Jacob 	 */
18088290ea90SAlexander Motin 	if (isp_atio_restart(isp, 0, tptr))
18092df76c16SMatt Jacob 		goto noresrc;
18102df76c16SMatt Jacob 
18112df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
18122df76c16SMatt Jacob 	if (atiop == NULL) {
18132df76c16SMatt Jacob 		goto noresrc;
18142df76c16SMatt Jacob 	}
18152df76c16SMatt Jacob 
18168290ea90SAlexander Motin 	atp = isp_get_atpd(isp, 0, aep->at_rxid);
18172df76c16SMatt Jacob 	if (atp == NULL) {
18182df76c16SMatt Jacob 		goto noresrc;
18192df76c16SMatt Jacob 	}
18202df76c16SMatt Jacob 
1821570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1822d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1823570c7a3fSMatt Jacob 	tptr->atio_count--;
1824387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
182545b9ad7aSAlexander Motin 	atiop->ccb_h.target_id = fcp->isp_loopid;
182692a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
18272df76c16SMatt Jacob 
1828b0a3ba7eSMatt Jacob 	/*
1829b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1830b0a3ba7eSMatt Jacob 	 */
1831f48ce188SMatt Jacob 	atiop->sense_len = 0;
18322df76c16SMatt Jacob 
18332df76c16SMatt Jacob 	/*
18342df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
18352df76c16SMatt Jacob 	 */
1836d4f3ad3aSAlexander Motin 	if (IS_2100(isp))
1837d4f3ad3aSAlexander Motin 		atiop->init_id = nphdl;
1838d4f3ad3aSAlexander Motin 	else {
1839d4f3ad3aSAlexander Motin 		if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1840e68eef14SAlexander Motin 		     lp->state == FC_PORTDB_STATE_ZOMBIE)) {
184137a7daacSAlexander Motin 			uint64_t wwpn =
18422df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[0]) << 48) |
18432df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[1]) << 32) |
18442df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[2]) << 16) |
18452df76c16SMatt Jacob 				(((uint64_t) aep->at_wwpn[3]) <<  0);
184637a7daacSAlexander Motin 			isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
184737a7daacSAlexander Motin 			    nphdl, PORT_ANY, 0);
184845b9ad7aSAlexander Motin 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
184945b9ad7aSAlexander Motin 				fcp->isp_loopstate = LOOP_LTEST_DONE;
185045b9ad7aSAlexander Motin 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
185145b9ad7aSAlexander Motin 			    ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1852d4f3ad3aSAlexander Motin 			isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1853d4f3ad3aSAlexander Motin 		}
1854d4f3ad3aSAlexander Motin 		atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
18552df76c16SMatt Jacob 	}
1856d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
18572df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1858d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
18592df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1860d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1861d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
18625e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1863d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1864d81ba9d5SMatt Jacob 		break;
1865d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
18665e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1867d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1868d81ba9d5SMatt Jacob 		break;
1869d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
18705e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1871d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1872d81ba9d5SMatt Jacob 		break;
1873d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1874d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
1875d81ba9d5SMatt Jacob 	default:
1876d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
1877d81ba9d5SMatt Jacob 		break;
1878d81ba9d5SMatt Jacob 	}
1879f48ce188SMatt Jacob 
188053036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
188153036e92SMatt Jacob 	atp->bytes_xfered = 0;
18822df76c16SMatt Jacob 	atp->lun = lun;
1883d4f3ad3aSAlexander Motin 	atp->nphdl = nphdl;
18842df76c16SMatt Jacob 	atp->sid = PORT_ANY;
18852df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
18862df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
18872df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1888570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
1889d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
1890123055f0SNathan 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);
18912df76c16SMatt Jacob 	return;
18922df76c16SMatt Jacob noresrc:
18938290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, 0);
18942df76c16SMatt Jacob 	if (ntp == NULL) {
18958290ea90SAlexander Motin 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
18962df76c16SMatt Jacob 		return;
18972df76c16SMatt Jacob 	}
18988290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
18998290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1900d81ba9d5SMatt Jacob }
1901d81ba9d5SMatt Jacob 
19022df76c16SMatt Jacob static void
19032df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
19042df76c16SMatt Jacob {
19052df76c16SMatt Jacob 	int cdbxlen;
19066af11b82SAlexander Motin 	lun_id_t lun;
19076af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
19082df76c16SMatt Jacob 	uint32_t did, sid;
19092df76c16SMatt Jacob 	fcportdb_t *lp;
19102df76c16SMatt Jacob 	tstate_t *tptr;
19112df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
19122df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
19139e7d423dSMatt Jacob 	atio_private_data_t *oatp;
19142df76c16SMatt Jacob 	inot_private_data_t *ntp;
19152df76c16SMatt Jacob 
19162df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
19172df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
19186af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
19192df76c16SMatt Jacob 
19202df76c16SMatt Jacob 	/*
19212df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
19222df76c16SMatt Jacob 	 *
19232df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
19242df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
19252df76c16SMatt Jacob 	 */
19267dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
19272df76c16SMatt Jacob 		/*
19282df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
19292df76c16SMatt Jacob 		 */
19302df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
19312df76c16SMatt Jacob 
19322df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
19332df76c16SMatt Jacob 			NANOTIME_T now;
19342df76c16SMatt Jacob 
19352df76c16SMatt Jacob 			/*
19362df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
19372df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
19382df76c16SMatt Jacob 			 */
19392df76c16SMatt Jacob 			GET_NANOTIME(&now);
1940514a71ebSAlexander Motin 			if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
19412df76c16SMatt 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);
19422df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
19432df76c16SMatt Jacob 				return;
19442df76c16SMatt Jacob 			}
19452df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
19462df76c16SMatt Jacob 			if (tptr == NULL) {
19472df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
19482df76c16SMatt Jacob 				if (tptr == NULL) {
19492df76c16SMatt 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);
19502df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
19512df76c16SMatt Jacob 					return;
19522df76c16SMatt Jacob 				}
19532df76c16SMatt Jacob 			}
19542df76c16SMatt 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);
19552df76c16SMatt Jacob 			goto noresrc;
19562df76c16SMatt Jacob 		}
19572df76c16SMatt 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);
19582df76c16SMatt Jacob 	} else {
19592df76c16SMatt Jacob 		chan = 0;
19602df76c16SMatt Jacob 	}
19612df76c16SMatt Jacob 
19622df76c16SMatt Jacob 	/*
19632df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
19642df76c16SMatt Jacob 	 */
1965eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
19662df76c16SMatt Jacob 		/*
19672df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
19682df76c16SMatt Jacob 		 */
19692df76c16SMatt 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",
19702df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
1971e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
19722df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
19732df76c16SMatt Jacob 		return;
19742df76c16SMatt Jacob 	}
19752df76c16SMatt Jacob 	nphdl = lp->handle;
19762df76c16SMatt Jacob 
19772df76c16SMatt Jacob 	/*
19782df76c16SMatt Jacob 	 * Get the tstate pointer
19792df76c16SMatt Jacob 	 */
19802df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
19812df76c16SMatt Jacob 	if (tptr == NULL) {
19822df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
19832df76c16SMatt Jacob 		if (tptr == NULL) {
19846af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
19856af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
19866af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
1987387d8239SMatt Jacob 			if (lun == 0) {
1988352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1989387d8239SMatt Jacob 			} else {
19902df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1991387d8239SMatt Jacob 			}
19922df76c16SMatt Jacob 			return;
19932df76c16SMatt Jacob 		}
19942df76c16SMatt Jacob 	}
19952df76c16SMatt Jacob 
19962df76c16SMatt Jacob 	/*
19972df76c16SMatt Jacob 	 * Start any commands pending resources first.
19982df76c16SMatt Jacob 	 */
19998290ea90SAlexander Motin 	if (isp_atio_restart(isp, chan, tptr))
20002df76c16SMatt Jacob 		goto noresrc;
20012df76c16SMatt Jacob 
20022df76c16SMatt Jacob 	/*
20032df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
20042df76c16SMatt Jacob 	 */
20052df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
20062df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
20072df76c16SMatt Jacob 		return;
20082df76c16SMatt Jacob 	}
20092df76c16SMatt Jacob 
20102df76c16SMatt Jacob 	/*
20112df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
20122df76c16SMatt Jacob 	 */
20132df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
20142df76c16SMatt Jacob 	if (atiop == NULL) {
20152df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
20162df76c16SMatt Jacob 		goto noresrc;
20172df76c16SMatt Jacob 	}
20182df76c16SMatt Jacob 
20198290ea90SAlexander Motin 	oatp = isp_find_atpd(isp, chan, aep->at_rxid);
20209e7d423dSMatt Jacob 	if (oatp) {
2021387d8239SMatt 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",
20229e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
20232df76c16SMatt Jacob 		/*
20242df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
20252df76c16SMatt Jacob 		 */
20262df76c16SMatt Jacob 		goto noresrc;
20272df76c16SMatt Jacob 	}
20288290ea90SAlexander Motin 	atp = isp_get_atpd(isp, chan, aep->at_rxid);
2029523ea374SAlexander Motin 	if (atp == NULL) {
2030523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2031523ea374SAlexander Motin 		goto noresrc;
2032523ea374SAlexander Motin 	}
2033387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
20342df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
20352df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
20362df76c16SMatt Jacob 	tptr->atio_count--;
2037387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2038d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
20392df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
20402df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
20412df76c16SMatt Jacob 	atiop->sense_len = 0;
20422df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
20432df76c16SMatt Jacob 	if (cdbxlen) {
20442df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
20452df76c16SMatt Jacob 	}
20462df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
20472df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
20482df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
20492df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
20502df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
20512df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
20522df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
20535e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20542df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
20552df76c16SMatt Jacob 		break;
20562df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
20575e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20582df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
20592df76c16SMatt Jacob 		break;
20602df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
20615e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
20622df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
20632df76c16SMatt Jacob 		break;
20642df76c16SMatt Jacob 	default:
20652df76c16SMatt Jacob 		/* FALLTHROUGH */
20662df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
20672df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
20682df76c16SMatt Jacob 		atiop->tag_action = 0;
20692df76c16SMatt Jacob 		break;
20702df76c16SMatt Jacob 	}
20712df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
20722df76c16SMatt Jacob 	atp->bytes_xfered = 0;
20732df76c16SMatt Jacob 	atp->lun = lun;
20742df76c16SMatt Jacob 	atp->nphdl = nphdl;
20753f072d69SAlexander Motin 	atp->sid = sid;
20763f072d69SAlexander Motin 	atp->did = did;
20772df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
20789e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
20792df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
20802df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
20812df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
20826af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
20836af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
20842df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
20852df76c16SMatt Jacob 	return;
20862df76c16SMatt Jacob noresrc:
20878290ea90SAlexander Motin 	if (atp)
20888290ea90SAlexander Motin 		isp_put_atpd(isp, chan, atp);
20898290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, chan);
20902df76c16SMatt Jacob 	if (ntp == NULL) {
20912df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
20922df76c16SMatt Jacob 		return;
20932df76c16SMatt Jacob 	}
20948290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
20958290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
20962df76c16SMatt Jacob }
20972df76c16SMatt Jacob 
2098387d8239SMatt Jacob 
2099387d8239SMatt Jacob /*
2100387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
2101387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
2102387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
2103387d8239SMatt Jacob  * transaction.
2104387d8239SMatt Jacob  */
2105387d8239SMatt Jacob static void
21068290ea90SAlexander Motin isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
2107387d8239SMatt Jacob {
2108387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
2109387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2110387d8239SMatt Jacob 	union ccb *ccb;
2111387d8239SMatt Jacob 
2112387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
2113387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2114387d8239SMatt Jacob 	ccb = atp->srr_ccb;
2115387d8239SMatt Jacob 	atp->srr_ccb = NULL;
2116387d8239SMatt Jacob 	atp->nsrr++;
2117387d8239SMatt Jacob 	if (ccb == NULL) {
211894dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2119387d8239SMatt Jacob 		goto fail;
2120387d8239SMatt Jacob 	}
2121387d8239SMatt Jacob 
2122387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
2123387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
2124387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2125387d8239SMatt Jacob 
2126387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
2127387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
2128387d8239SMatt Jacob 		/*
2129387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
2130387d8239SMatt Jacob 		 */
2131387d8239SMatt Jacob 		atp->sendst = 0;
2132387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
2133387d8239SMatt Jacob 		if (ccb_len == 0) {
213494dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2135387d8239SMatt Jacob 			goto mdp;
2136387d8239SMatt Jacob 		}
2137387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
213894dff771SMatt 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);
2139387d8239SMatt Jacob 			goto mdp;
2140387d8239SMatt Jacob 		}
214194dff771SMatt 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);
2142387d8239SMatt Jacob 		break;
2143387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
214494dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2145387d8239SMatt Jacob 		atp->sendst = 1;
2146387d8239SMatt Jacob 		/*
2147387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
2148387d8239SMatt Jacob 		 */
2149387d8239SMatt Jacob 		break;
2150387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
2151387d8239SMatt Jacob 		/*
2152387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2153387d8239SMatt Jacob 		 */
2154387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2155387d8239SMatt Jacob 		goto fail;
2156387d8239SMatt Jacob 
2157387d8239SMatt Jacob 	default:
2158387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2159387d8239SMatt Jacob 		goto fail;
2160387d8239SMatt Jacob 	}
2161387d8239SMatt Jacob 
2162387d8239SMatt Jacob 	/*
2163387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2164387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2165387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2166387d8239SMatt Jacob 	 */
2167387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2168387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2169387d8239SMatt Jacob 		goto fail;
2170387d8239SMatt Jacob 	}
2171387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2172387d8239SMatt Jacob 	return;
2173387d8239SMatt Jacob fail:
2174387d8239SMatt Jacob 	inot->in_reserved = 1;
2175387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2176387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2177387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2178387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2179387d8239SMatt Jacob 	return;
2180387d8239SMatt Jacob mdp:
2181387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2182387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2183387d8239SMatt Jacob 		goto fail;
2184387d8239SMatt Jacob 	}
2185387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2186387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2187387d8239SMatt Jacob 	/*
2188387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2189387d8239SMatt Jacob 	 */
2190387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2191387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2192387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2193387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2194387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2195387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2196387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2197387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2198387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2199387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2200387d8239SMatt Jacob }
2201387d8239SMatt Jacob 
2202387d8239SMatt Jacob 
2203387d8239SMatt Jacob static void
2204387d8239SMatt Jacob isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2205387d8239SMatt Jacob {
2206387d8239SMatt Jacob 	in_fcentry_24xx_t *inot = inot_raw;
2207387d8239SMatt Jacob 	atio_private_data_t *atp;
2208387d8239SMatt Jacob 	uint32_t tag = inot->in_rxid;
2209387d8239SMatt Jacob 	uint32_t bus = inot->in_vpidx;
2210387d8239SMatt Jacob 
2211387d8239SMatt Jacob 	if (!IS_24XX(isp)) {
2212387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2213387d8239SMatt Jacob 		return;
2214387d8239SMatt Jacob 	}
2215387d8239SMatt Jacob 
22168290ea90SAlexander Motin 	atp = isp_find_atpd(isp, bus, tag);
2217387d8239SMatt Jacob 	if (atp == NULL) {
2218387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2219387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2220387d8239SMatt Jacob 		return;
2221387d8239SMatt Jacob 	}
2222387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2223387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
222494dff771SMatt 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,
2225387d8239SMatt Jacob 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2226387d8239SMatt Jacob 	if (atp->srr_ccb)
22278290ea90SAlexander Motin 		isp_handle_srr_start(isp, atp);
2228387d8239SMatt Jacob }
2229387d8239SMatt Jacob 
22302df76c16SMatt Jacob static void
22319cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2232d81ba9d5SMatt Jacob {
2233d81ba9d5SMatt Jacob 	union ccb *ccb;
2234a6036a44SAlexander Motin 	int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
22352df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
22362df76c16SMatt Jacob 	int bus;
2237a6036a44SAlexander Motin 	uint32_t handle, data_requested, resid;
2238d81ba9d5SMatt Jacob 
22392df76c16SMatt Jacob 	handle = ((ct2_entry_t *)arg)->ct_syshandle;
2240970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
22412df76c16SMatt Jacob 	if (ccb == NULL) {
22422df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
22432df76c16SMatt Jacob 		return;
22442df76c16SMatt Jacob 	}
2245970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
2246a6036a44SAlexander Motin 	resid = data_requested = PISP_PCMD(ccb)->datalen;
2247387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2248387d8239SMatt Jacob 	if (isp->isp_nactive) {
2249387d8239SMatt Jacob 		isp->isp_nactive--;
2250387d8239SMatt Jacob 	}
2251387d8239SMatt Jacob 
22522df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
2253387d8239SMatt Jacob 	if (IS_24XX(isp)) {
22548290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2255387d8239SMatt Jacob 	} else {
22568290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2257387d8239SMatt Jacob 	}
2258387d8239SMatt Jacob 	if (atp == NULL) {
2259af207637SAlexander Motin 		/*
22608656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
22618656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
22628656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
2263af207637SAlexander Motin 		 */
22648290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2265af207637SAlexander Motin 	}
2266af207637SAlexander Motin 	if (atp == NULL) {
2267387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2268387d8239SMatt Jacob 		return;
2269387d8239SMatt Jacob 	}
2270387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
227194dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2272387d8239SMatt Jacob 	atp->ctcnt -= 1;
2273387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2274387d8239SMatt Jacob 
22752df76c16SMatt Jacob 	if (IS_24XX(isp)) {
22762df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2277d81ba9d5SMatt Jacob 
2278387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2279387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2280387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
22818290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
22822df76c16SMatt Jacob 			return;
22832df76c16SMatt Jacob 		}
2284387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
228587de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
228687de303cSAlexander Motin 			    (atp->sendst == 0);
2287387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2288387d8239SMatt Jacob 		} else {
22892df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
22902df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
229194dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2292a6036a44SAlexander Motin 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
22932df76c16SMatt Jacob 				resid = ct->ct_resid;
22942df76c16SMatt Jacob 		}
229594dff771SMatt 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),
229694dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
22973e6deb33SAlexander Motin 	} else {
22982df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2299387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2300387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2301387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
23028290ea90SAlexander Motin 				isp_handle_srr_start(isp, atp);
2303387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
23042df76c16SMatt Jacob 			return;
2305570c7a3fSMatt Jacob 		}
2306387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
230787de303cSAlexander Motin 			sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
230887de303cSAlexander Motin 			    (atp->sendst == 0);
2309387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2310387d8239SMatt Jacob 		} else {
2311d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2312d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
231394dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2314a6036a44SAlexander Motin 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2315a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
231653036e92SMatt Jacob 		}
231794dff771SMatt 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),
231894dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
23195d571944SMatt Jacob 	}
2320387d8239SMatt Jacob 	if (ok) {
2321a6036a44SAlexander Motin 		if (data_requested > 0) {
2322a6036a44SAlexander Motin 			atp->bytes_xfered += data_requested - resid;
2323a6036a44SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
2324a6036a44SAlexander Motin 			    (data_requested - resid);
2325387d8239SMatt Jacob 		}
2326a6036a44SAlexander Motin 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2327387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2328387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
2329387d8239SMatt Jacob 	} else {
2330387d8239SMatt Jacob 		notify_cam = 1;
2331387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
2332387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2333387d8239SMatt Jacob 		else
2334387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2335387d8239SMatt Jacob 	}
2336387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
2337a1bc34c6SMatt Jacob 
2338a1bc34c6SMatt Jacob 	/*
2339387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
2340387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
2341d81ba9d5SMatt Jacob 	 */
234294dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
234394dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2344f48ce188SMatt Jacob 	if (notify_cam == 0) {
2345387d8239SMatt Jacob 		if (atp->sendst) {
2346387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2347387d8239SMatt Jacob 		}
23482df76c16SMatt Jacob 		return;
2349f48ce188SMatt Jacob 	}
2350d81ba9d5SMatt Jacob 
2351387d8239SMatt Jacob 	/*
235287de303cSAlexander Motin 	 * We are done with this ATIO if we successfully sent status.
235387de303cSAlexander Motin 	 * In all other cases expect either another CTIO or XPT_ABORT.
23548290ea90SAlexander Motin 	 */
235587de303cSAlexander Motin 	if (ok && sentstatus)
23568290ea90SAlexander Motin 		isp_put_atpd(isp, bus, atp);
23578290ea90SAlexander Motin 
23588290ea90SAlexander Motin 	/*
2359387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
2360387d8239SMatt Jacob 	 *
2361387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
2362387d8239SMatt Jacob 	 *
2363387d8239SMatt Jacob 	 * Other cards need one put back only on error.
2364387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
2365387d8239SMatt Jacob 	 * and try again in case we didn't have
2366387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
2367387d8239SMatt Jacob 	 * once the putback is done we do the completion
2368387d8239SMatt Jacob 	 * call.
2369387d8239SMatt Jacob 	 */
2370387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
2371a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2372387d8239SMatt Jacob 	} else {
2373387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
2374d81ba9d5SMatt Jacob 	}
2375d81ba9d5SMatt Jacob }
2376570c7a3fSMatt Jacob 
23772df76c16SMatt Jacob static void
23789cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2379570c7a3fSMatt Jacob {
23802df76c16SMatt Jacob 	int needack = 1;
2381570c7a3fSMatt Jacob 	switch (inp->in_status) {
2382570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
23832df76c16SMatt Jacob 		/*
23842df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
23852df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
23862df76c16SMatt Jacob 		 */
23872df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2388570c7a3fSMatt Jacob 		break;
2389570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
23902df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2391570c7a3fSMatt Jacob 		break;
2392570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
23932df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
2394570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2395570c7a3fSMatt Jacob 		break;
2396570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
2397570c7a3fSMatt Jacob 	{
2398c5fd36edSAlexander Motin 		uint16_t nphdl, lun;
2399c5fd36edSAlexander Motin 		uint32_t sid;
24002df76c16SMatt Jacob 		uint64_t wwn;
24012df76c16SMatt Jacob 		fcportdb_t *lp;
240226719198SAlexander Motin 		isp_notify_t tmp, *nt = &tmp;
24032df76c16SMatt Jacob 
24042df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
24052df76c16SMatt Jacob 			lun = inp->in_scclun;
24062df76c16SMatt Jacob 		} else {
24072df76c16SMatt Jacob 			lun = inp->in_lun;
24082df76c16SMatt Jacob 		}
24092df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
2410c5fd36edSAlexander Motin 			nphdl = ((in_fcentry_e_t *)inp)->in_iid;
24112df76c16SMatt Jacob 		} else {
2412c5fd36edSAlexander Motin 			nphdl = inp->in_iid;
24132df76c16SMatt Jacob 		}
2414c5fd36edSAlexander Motin 		if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
24152df76c16SMatt Jacob 			wwn = lp->port_wwn;
2416d4f3ad3aSAlexander Motin 			sid = lp->portid;
24172df76c16SMatt Jacob 		} else {
24182df76c16SMatt Jacob 			wwn = INI_ANY;
2419d4f3ad3aSAlexander Motin 			sid = PORT_ANY;
24202df76c16SMatt Jacob 		}
242126719198SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx",
242226719198SAlexander Motin 		    inp->in_seqid, (unsigned long long) wwn);
2423570c7a3fSMatt Jacob 
24242df76c16SMatt Jacob 		ISP_MEMZERO(nt, sizeof (isp_notify_t));
24252df76c16SMatt Jacob 		nt->nt_hba = isp;
24262df76c16SMatt Jacob 		nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
24272df76c16SMatt Jacob 		nt->nt_wwn = wwn;
2428c5fd36edSAlexander Motin 		nt->nt_nphdl = nphdl;
2429d4f3ad3aSAlexander Motin 		nt->nt_sid = sid;
24302df76c16SMatt Jacob 		nt->nt_did = PORT_ANY;
24312df76c16SMatt Jacob 		nt->nt_lun = lun;
243226719198SAlexander Motin 		nt->nt_tagval = inp->in_seqid;
243326719198SAlexander Motin 		nt->nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
24342df76c16SMatt Jacob 		nt->nt_need_ack = 1;
24352df76c16SMatt Jacob 		nt->nt_channel = 0;
24362df76c16SMatt Jacob 		nt->nt_ncode = NT_ABORT_TASK;
243726719198SAlexander Motin 		nt->nt_lreserved = inp;
24382df76c16SMatt Jacob 		isp_handle_platform_target_tmf(isp, nt);
24392df76c16SMatt Jacob 		needack = 0;
2440570c7a3fSMatt Jacob 		break;
2441570c7a3fSMatt Jacob 	}
2442570c7a3fSMatt Jacob 	default:
2443570c7a3fSMatt Jacob 		break;
2444570c7a3fSMatt Jacob 	}
24452df76c16SMatt Jacob 	if (needack) {
2446387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
24472df76c16SMatt Jacob 	}
24482df76c16SMatt Jacob }
24492df76c16SMatt Jacob 
24502df76c16SMatt Jacob static void
24512df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
24522df76c16SMatt Jacob {
24532df76c16SMatt Jacob 	uint16_t nphdl;
2454387d8239SMatt Jacob 	uint16_t prli_options = 0;
24552df76c16SMatt Jacob 	uint32_t portid;
24562df76c16SMatt Jacob 	fcportdb_t *lp;
245737a7daacSAlexander Motin 	char *msg = NULL;
245837a7daacSAlexander Motin 	uint8_t *ptr = (uint8_t *)inot;
245937a7daacSAlexander Motin 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
24602df76c16SMatt Jacob 
24612df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
24622df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
24632df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
24642df76c16SMatt Jacob 	} else {
24652df76c16SMatt Jacob 		portid = PORT_ANY;
24662df76c16SMatt Jacob 	}
24672df76c16SMatt Jacob 
24682df76c16SMatt Jacob 	switch (inot->in_status) {
24692df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
24702df76c16SMatt Jacob 	{
247137a7daacSAlexander Motin 		char buf[16];
24722df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
24732df76c16SMatt Jacob 
24742df76c16SMatt Jacob 		/*
24752df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
2476b1ce21c6SRebecca Cran 		 * (possibly with some associated information sent upstream). This is
24772df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
24782df76c16SMatt Jacob 		 */
24792df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
24802df76c16SMatt Jacob 		case LOGO:
24812df76c16SMatt Jacob 			msg = "LOGO";
248237a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
248337a7daacSAlexander Motin 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
24842df76c16SMatt Jacob 			break;
24852df76c16SMatt Jacob 		case PRLO:
24862df76c16SMatt Jacob 			msg = "PRLO";
24872df76c16SMatt Jacob 			break;
24882df76c16SMatt Jacob 		case PLOGI:
24892df76c16SMatt Jacob 			msg = "PLOGI";
249037a7daacSAlexander Motin 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
249137a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
249237a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
249337a7daacSAlexander Motin 			    nphdl, portid, prli_options);
249437a7daacSAlexander Motin 			break;
249537a7daacSAlexander Motin 		case PRLI:
2496dad28623SMatt Jacob 			msg = "PRLI";
249737a7daacSAlexander Motin 			prli_options = inot->in_prli_options;
249837a7daacSAlexander Motin 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
249937a7daacSAlexander Motin 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
250037a7daacSAlexander Motin 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
250137a7daacSAlexander Motin 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
250237a7daacSAlexander Motin 			    nphdl, portid, prli_options);
25032df76c16SMatt Jacob 			break;
25042df76c16SMatt Jacob 		case PDISC:
25052df76c16SMatt Jacob 			msg = "PDISC";
25062df76c16SMatt Jacob 			break;
25072df76c16SMatt Jacob 		case ADISC:
25082df76c16SMatt Jacob 			msg = "ADISC";
25092df76c16SMatt Jacob 			break;
25102df76c16SMatt Jacob 		default:
25112df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
25122df76c16SMatt Jacob 			msg = buf;
25132df76c16SMatt Jacob 			break;
25142df76c16SMatt Jacob 		}
25152df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
25162df76c16SMatt 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);
25172df76c16SMatt Jacob 			break;
25182df76c16SMatt Jacob 		}
25192df76c16SMatt 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,
25202df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
2521387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
25222df76c16SMatt Jacob 		break;
25232df76c16SMatt Jacob 	}
25242df76c16SMatt Jacob 
25252df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
252637a7daacSAlexander Motin 		msg = "PORT LOGOUT";
2527e68eef14SAlexander Motin 		if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
25282df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
25292df76c16SMatt Jacob 		}
25302df76c16SMatt Jacob 		/* FALLTHROUGH */
25312df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
253237a7daacSAlexander Motin 		if (msg == NULL)
253337a7daacSAlexander Motin 			msg = "PORT CHANGED";
25342df76c16SMatt Jacob 		/* FALLTHROUGH */
25352df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
253637a7daacSAlexander Motin 		if (msg == NULL)
253737a7daacSAlexander Motin 			msg = "LIP RESET";
253837a7daacSAlexander 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);
25392df76c16SMatt Jacob 
25402df76c16SMatt Jacob 		/*
25412df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
25422df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
25432df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
25442df76c16SMatt Jacob 		 */
25452df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
2546387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
25472df76c16SMatt Jacob 		break;
25482df76c16SMatt Jacob 
25492df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
2550387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
2551387d8239SMatt Jacob 		isp_handle_srr_notify(isp, inot);
2552387d8239SMatt Jacob 		break;
2553387d8239SMatt Jacob #else
255437a7daacSAlexander Motin 		if (msg == NULL)
255537a7daacSAlexander Motin 			msg = "SRR RCVD";
2556387d8239SMatt Jacob 		/* FALLTHROUGH */
2557387d8239SMatt Jacob #endif
2558387d8239SMatt Jacob 	case IN24XX_LINK_RESET:
255937a7daacSAlexander Motin 		if (msg == NULL)
256037a7daacSAlexander Motin 			msg = "LINK RESET";
2561387d8239SMatt Jacob 	case IN24XX_LINK_FAILED:
256237a7daacSAlexander Motin 		if (msg == NULL)
256337a7daacSAlexander Motin 			msg = "LINK FAILED";
25642df76c16SMatt Jacob 	default:
256537a7daacSAlexander Motin 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2566387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
25672df76c16SMatt Jacob 		break;
25682df76c16SMatt Jacob 	}
25692df76c16SMatt Jacob }
25702df76c16SMatt Jacob 
25712df76c16SMatt Jacob static int
257296b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
25732df76c16SMatt Jacob {
25742df76c16SMatt Jacob 
25752df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
25762df76c16SMatt 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);
2577570c7a3fSMatt Jacob 		return (0);
2578570c7a3fSMatt Jacob 	}
25792df76c16SMatt Jacob 
25802df76c16SMatt Jacob 	/*
25812df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
25822df76c16SMatt Jacob 	 */
25832df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
25842df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
25852df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
25862df76c16SMatt Jacob 		fcportdb_t *lp;
25872df76c16SMatt Jacob 		uint32_t sid;
25882df76c16SMatt Jacob 		uint16_t nphdl;
25892df76c16SMatt Jacob 
25902df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2591eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
25922df76c16SMatt Jacob 			nphdl = lp->handle;
25932df76c16SMatt Jacob 		} else {
25942df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
25952df76c16SMatt Jacob 		}
25962df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
25972df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
25982df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
25992df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
26002df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
26012df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
26022df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
26032df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
26042df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
26052df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
26062df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
260796b5475bSAlexander Motin 		if (rsp != 0) {
260896b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
260996b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
261096b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
261196b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
261296b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
261396b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
261496b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
261596b5475bSAlexander Motin 		}
26162df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
26172df76c16SMatt Jacob 	}
26182df76c16SMatt Jacob 
26192df76c16SMatt Jacob 	/*
26202df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
26212df76c16SMatt Jacob 	 */
26222df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
26232df76c16SMatt Jacob 
26242df76c16SMatt Jacob 		/*
26252df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
26262df76c16SMatt Jacob 		 */
26272df76c16SMatt Jacob 		if (mp->nt_need_ack) {
26282df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
26292df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
26302df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
26312df76c16SMatt Jacob 
26322df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
26332df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
26342df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
26352df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
26362df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
26372df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
26382df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
26392df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
26402df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
26412df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
26422df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
26432df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
26442df76c16SMatt Jacob 				return (ENOMEM);
26452df76c16SMatt Jacob 			}
26462df76c16SMatt Jacob 			mp->nt_need_ack = 0;
26472df76c16SMatt Jacob 		}
26482df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
26492df76c16SMatt Jacob 			return (ENOMEM);
26502df76c16SMatt Jacob 		} else {
26512df76c16SMatt Jacob 			return (0);
26522df76c16SMatt Jacob 		}
26532df76c16SMatt Jacob 	}
26542df76c16SMatt Jacob 
26552df76c16SMatt Jacob 	/*
26562df76c16SMatt Jacob 	 * Handle logout cases here
26572df76c16SMatt Jacob 	 */
26582df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
26592df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
26602df76c16SMatt Jacob 	}
26612df76c16SMatt Jacob 
26622df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
26632df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
26642df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
26652df76c16SMatt Jacob 		}
26662df76c16SMatt Jacob 	}
26672df76c16SMatt Jacob 
26682df76c16SMatt Jacob 	/*
26692df76c16SMatt Jacob 	 * General purpose acknowledgement
26702df76c16SMatt Jacob 	 */
26712df76c16SMatt Jacob 	if (mp->nt_need_ack) {
26722df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2673387d8239SMatt Jacob 		/*
2674387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
2675387d8239SMatt Jacob 		 */
26762df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
26772df76c16SMatt Jacob 	}
26782df76c16SMatt Jacob 	return (0);
26792df76c16SMatt Jacob }
26802df76c16SMatt Jacob 
26812df76c16SMatt Jacob /*
2682b1ce21c6SRebecca Cran  * Handle task management functions.
26832df76c16SMatt Jacob  *
26842df76c16SMatt Jacob  * We show up here with a notify structure filled out.
26852df76c16SMatt Jacob  *
26862df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
26872df76c16SMatt Jacob  */
26882df76c16SMatt Jacob static void
26892df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
26902df76c16SMatt Jacob {
26912df76c16SMatt Jacob 	tstate_t *tptr;
26922df76c16SMatt Jacob 	fcportdb_t *lp;
26932df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
26942df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
26952df76c16SMatt Jacob 	lun_id_t lun;
26962df76c16SMatt Jacob 
26972df76c16SMatt 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,
26982df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
26992df76c16SMatt Jacob 	/*
27002df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
27012df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
27022df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
27032df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
27042df76c16SMatt Jacob 	 */
27052df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
27062df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
27072df76c16SMatt Jacob 	} else {
27082df76c16SMatt Jacob 		lun = notify->nt_lun;
27092df76c16SMatt Jacob 	}
27102df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
27112df76c16SMatt Jacob 	if (tptr == NULL) {
27122df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
27132df76c16SMatt Jacob 		if (tptr == NULL) {
2714123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
27152df76c16SMatt Jacob 			goto bad;
27162df76c16SMatt Jacob 		}
27172df76c16SMatt Jacob 	}
27182df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
27192df76c16SMatt Jacob 	if (inot == NULL) {
2720123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
27212df76c16SMatt Jacob 		goto bad;
27222df76c16SMatt Jacob 	}
27232df76c16SMatt Jacob 
2724eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2725d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
27262df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
27272df76c16SMatt Jacob 	} else {
2728d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
27292df76c16SMatt Jacob 	}
27302df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
27312df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
27322df76c16SMatt Jacob 
27332df76c16SMatt Jacob 	switch (notify->nt_ncode) {
27342df76c16SMatt Jacob 	case NT_ABORT_TASK:
27358290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
27362df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
27372df76c16SMatt Jacob 		break;
27382df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
27398290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
27402df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
27412df76c16SMatt Jacob 		break;
27422df76c16SMatt Jacob 	case NT_CLEAR_ACA:
27432df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
27442df76c16SMatt Jacob 		break;
27452df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
27462df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
27472df76c16SMatt Jacob 		break;
27482df76c16SMatt Jacob 	case NT_LUN_RESET:
27492df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
27502df76c16SMatt Jacob 		break;
27512df76c16SMatt Jacob 	case NT_TARGET_RESET:
27522df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
27532df76c16SMatt Jacob 		break;
2754c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
2755c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
2756c98d2b1fSAlexander Motin 		break;
2757c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
2758c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
2759c98d2b1fSAlexander Motin 		break;
27602df76c16SMatt Jacob 	default:
2761123055f0SNathan 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);
27622df76c16SMatt Jacob 		goto bad;
27632df76c16SMatt Jacob 	}
27642df76c16SMatt Jacob 
27658290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, notify->nt_channel);
27662df76c16SMatt Jacob 	if (ntp == NULL) {
27672df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
27682df76c16SMatt Jacob 		goto bad;
27692df76c16SMatt Jacob 	}
27708290ea90SAlexander Motin 	ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
27712df76c16SMatt Jacob 	if (notify->nt_lreserved) {
27728290ea90SAlexander Motin 		ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
27738290ea90SAlexander Motin 		ntp->nt.nt_lreserved = &ntp->data;
27742df76c16SMatt Jacob 	}
27758290ea90SAlexander Motin 	ntp->seq_id = notify->nt_tagval;
27768290ea90SAlexander Motin 	ntp->tag_id = notify->nt_tagval >> 32;
27772df76c16SMatt Jacob 
27782df76c16SMatt Jacob 	tptr->inot_count--;
27792df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2780387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
27812df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
27822df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
27832df76c16SMatt Jacob 	return;
27842df76c16SMatt Jacob bad:
27852df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
27862df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2787387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2788387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2789387d8239SMatt Jacob 			}
27902df76c16SMatt Jacob 		} else {
2791387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
27922df76c16SMatt Jacob 		}
27932df76c16SMatt Jacob 	}
27942df76c16SMatt Jacob }
27952df76c16SMatt Jacob 
27962df76c16SMatt Jacob static void
27978290ea90SAlexander Motin isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
27982df76c16SMatt Jacob {
27998290ea90SAlexander Motin 	atio_private_data_t *atp, *atpool;
28008290ea90SAlexander Motin 	inot_private_data_t *ntp, *tmp;
28018290ea90SAlexander Motin 	uint32_t this_tag_id;
28022df76c16SMatt Jacob 
28032df76c16SMatt Jacob 	/*
28042df76c16SMatt Jacob 	 * First, clean any commands pending restart
28052df76c16SMatt Jacob 	 */
28068290ea90SAlexander Motin 	STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
28078290ea90SAlexander Motin 		if (IS_24XX(isp))
28088290ea90SAlexander Motin 			this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
28098290ea90SAlexander Motin 		else
28108290ea90SAlexander Motin 			this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
28112df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
28128290ea90SAlexander Motin 			isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
28138290ea90SAlexander Motin 			    ECMD_TERMINATE, 0);
28148290ea90SAlexander Motin 			isp_put_ntpd(isp, chan, ntp);
28158290ea90SAlexander Motin 			STAILQ_REMOVE(&tptr->restart_queue, ntp,
28168290ea90SAlexander Motin 			    inot_private_data, next);
28172df76c16SMatt Jacob 		}
28182df76c16SMatt Jacob 	}
28192df76c16SMatt Jacob 
28202df76c16SMatt Jacob 	/*
28212df76c16SMatt Jacob 	 * Now mark other ones dead as well.
28222df76c16SMatt Jacob 	 */
28238290ea90SAlexander Motin 	ISP_GET_PC(isp, chan, atpool, atpool);
28248290ea90SAlexander Motin 	for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
28258290ea90SAlexander Motin 		if (atp->lun != tptr->ts_lun)
28268290ea90SAlexander Motin 			continue;
28278290ea90SAlexander Motin 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
28282df76c16SMatt Jacob 			atp->dead = 1;
28292df76c16SMatt Jacob 	}
28302df76c16SMatt Jacob }
2831d81ba9d5SMatt Jacob #endif
2832d81ba9d5SMatt Jacob 
2833478f8a96SJustin T. Gibbs static void
28341dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2835478f8a96SJustin T. Gibbs {
2836478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
2837e95725cbSMatt Jacob 	int bus, tgt;
28389cd7268eSMatt Jacob 	ispsoftc_t *isp;
2839478f8a96SJustin T. Gibbs 
2840478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
28419cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
2842e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
2843e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
2844e95725cbSMatt Jacob 
2845478f8a96SJustin T. Gibbs 	switch (code) {
2846478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
2847ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
28481dae40ebSMatt Jacob 			uint16_t oflags, nflags;
28492df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
2850478f8a96SJustin T. Gibbs 
285141ed683eSMatt Jacob 			if (tgt >= 0) {
28529ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
28539ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
2854a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
2855478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
2856478f8a96SJustin T. Gibbs 				}
28579ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
28589ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
2859478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
28602df76c16SMatt Jacob 				sdp->update = 1;
28612df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
28629ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
2863478f8a96SJustin T. Gibbs 			}
286441ed683eSMatt Jacob 		}
2865478f8a96SJustin T. Gibbs 		break;
2866478f8a96SJustin T. Gibbs 	default:
28673c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2868478f8a96SJustin T. Gibbs 		break;
2869478f8a96SJustin T. Gibbs 	}
2870478f8a96SJustin T. Gibbs }
2871478f8a96SJustin T. Gibbs 
2872478f8a96SJustin T. Gibbs static void
2873c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
2874478f8a96SJustin T. Gibbs {
28759cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
28766ce548a1SAlexander Motin 	uint16_t isr, sema, info;
2877126ec864SMatt Jacob 
28786ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
28796ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
2880478f8a96SJustin T. Gibbs }
2881478f8a96SJustin T. Gibbs 
2882ab6c4b31SMatt Jacob 
28832df76c16SMatt Jacob static void
28842df76c16SMatt Jacob isp_watchdog(void *arg)
2885cc8df88bSMatt Jacob {
28862df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
28872df76c16SMatt Jacob 	ispsoftc_t *isp;
2888e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
2889b85389e1SMatt Jacob 
28902df76c16SMatt Jacob 	isp = XS_ISP(xs);
28912df76c16SMatt Jacob 
2892cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
2893e95725cbSMatt Jacob 
2894e95725cbSMatt Jacob 	/*
2895e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2896e95725cbSMatt Jacob 	 */
2897e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
28986ce548a1SAlexander Motin 		uint16_t isr, sema, info;
28996ce548a1SAlexander Motin 		if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
29006ce548a1SAlexander Motin 			isp_intr(isp, isr, sema, info);
2901e95725cbSMatt Jacob 		ohandle = handle;
2902e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
2903e95725cbSMatt Jacob 	}
2904c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
29051fcf5debSMatt Jacob 		/*
2906c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
2907c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
2908c8b8a2c4SMatt Jacob 		 *
2909c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
2910c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
2911c8b8a2c4SMatt Jacob 		 * back separately.
29121fcf5debSMatt Jacob 		 */
2913c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2914c8b8a2c4SMatt Jacob 			return;
2915c8b8a2c4SMatt Jacob 		}
29161fcf5debSMatt Jacob 
29171fcf5debSMatt Jacob 		/*
2918c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
2919c8b8a2c4SMatt Jacob 		 * fact have been completed.
2920c8b8a2c4SMatt Jacob 		 */
2921c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
2922c8b8a2c4SMatt Jacob 
2923c8b8a2c4SMatt Jacob 		/*
2924c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
2925c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
2926c8b8a2c4SMatt Jacob 		 */
2927c8b8a2c4SMatt Jacob 		if (xs == NULL) {
2928c8b8a2c4SMatt Jacob 			return;
2929c8b8a2c4SMatt Jacob 		}
2930c8b8a2c4SMatt Jacob 
2931c8b8a2c4SMatt Jacob 		/*
2932c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
29331fcf5debSMatt Jacob 		 */
2934f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
2935f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
2936f6e75de2SMatt Jacob 		}
2937cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
2938c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2939387d8239SMatt Jacob 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
2940387d8239SMatt Jacob 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
2941e95725cbSMatt Jacob 		isp_prt_endcmd(isp, xs);
2942cc8df88bSMatt Jacob 		isp_done(xs);
2943e95725cbSMatt Jacob 	} else {
2944e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
2945e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2946e95725cbSMatt Jacob 		} else {
2947e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2948e95725cbSMatt Jacob 		}
2949b85389e1SMatt Jacob 	}
2950f7c631bcSMatt Jacob }
2951f7c631bcSMatt Jacob 
2952f7c631bcSMatt Jacob static void
29535704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2954f7c631bcSMatt Jacob {
2955ffcf6651SMatt Jacob 	union ccb *ccb;
29562df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
29572df76c16SMatt Jacob 
2958ffcf6651SMatt Jacob 	/*
29590e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2960ffcf6651SMatt Jacob 	 */
29618008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
2962ffcf6651SMatt Jacob 	if (ccb == NULL) {
29632df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2964ffcf6651SMatt Jacob 		return;
2965ffcf6651SMatt Jacob 	}
2966e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2967e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2968ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2969ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
2970ffcf6651SMatt Jacob 		return;
2971ffcf6651SMatt Jacob 	}
2972ffcf6651SMatt Jacob 	xpt_rescan(ccb);
2973ffcf6651SMatt Jacob }
2974ffcf6651SMatt Jacob 
2975ffcf6651SMatt Jacob static void
29765704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2977ffcf6651SMatt Jacob {
2978ffcf6651SMatt Jacob 	struct cam_path *tp;
29792df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
29802df76c16SMatt Jacob 
29812df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2982ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
2983ffcf6651SMatt Jacob 		xpt_free_path(tp);
2984f7c631bcSMatt Jacob 	}
2985f7c631bcSMatt Jacob }
2986f7c631bcSMatt Jacob 
2987f7c631bcSMatt Jacob /*
2988f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
2989f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
2990f7c631bcSMatt Jacob  * gone away.
2991f7c631bcSMatt Jacob  *
2992f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
2993f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
2994f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
2995f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
2996f7c631bcSMatt Jacob  */
2997f7c631bcSMatt Jacob static void
2998f7c631bcSMatt Jacob isp_gdt(void *arg)
2999f7c631bcSMatt Jacob {
30002df76c16SMatt Jacob 	struct isp_fc *fc = arg;
3001de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3002de461933SMatt Jacob }
3003de461933SMatt Jacob 
3004de461933SMatt Jacob static void
3005de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
3006de461933SMatt Jacob {
3007de461933SMatt Jacob 	struct isp_fc *fc = arg;
30082df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
30092df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3010f7c631bcSMatt Jacob 	fcportdb_t *lp;
3011e68eef14SAlexander Motin 	struct ac_contract ac;
3012e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3013766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
3014f7c631bcSMatt Jacob 
3015de461933SMatt Jacob 	ISP_LOCK(isp);
3016de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3017f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
30182df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3019f7c631bcSMatt Jacob 
3020f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3021f7c631bcSMatt Jacob 			continue;
3022f7c631bcSMatt Jacob 		}
3023427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
3024427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
3025f7c631bcSMatt Jacob 			more_to_do++;
3026f7c631bcSMatt Jacob 			continue;
3027f7c631bcSMatt Jacob 		}
3028e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3029e68eef14SAlexander Motin 		if (lp->is_target) {
3030e68eef14SAlexander Motin 			lp->is_target = 0;
3031766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3032f7c631bcSMatt Jacob 		}
3033e68eef14SAlexander Motin 		if (lp->is_initiator) {
3034e68eef14SAlexander Motin 			lp->is_initiator = 0;
3035e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3036e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3037e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3038e68eef14SAlexander Motin 			adc->port = lp->portid;
3039d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3040e68eef14SAlexander Motin 			adc->arrived = 0;
3041e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3042e68eef14SAlexander Motin 		}
3043e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
3044e68eef14SAlexander Motin 	}
3045a01f5aebSMatt Jacob 	if (fc->ready) {
3046f7c631bcSMatt Jacob 		if (more_to_do) {
30472df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
3048f7c631bcSMatt Jacob 		} else {
3049de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
3050387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3051a01f5aebSMatt Jacob 		}
3052f7c631bcSMatt Jacob 	}
3053de461933SMatt Jacob 	ISP_UNLOCK(isp);
3054f7c631bcSMatt Jacob }
3055f7c631bcSMatt Jacob 
3056f7c631bcSMatt Jacob /*
3057e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
3058e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
3059e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
3060f7c631bcSMatt Jacob  *
3061f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
3062f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
3063f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3064f7c631bcSMatt Jacob  */
3065f7c631bcSMatt Jacob static void
3066e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
3067f7c631bcSMatt Jacob {
3068e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3069e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3070e561aa79SAlexander Motin 
3071e561aa79SAlexander Motin 	if (fc->loop_down_time)
3072e561aa79SAlexander Motin 		return;
3073e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3074e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
3075e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
3076e561aa79SAlexander Motin 	fc->loop_dead = 0;
3077e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
3078e561aa79SAlexander Motin 	wakeup(fc);
3079de461933SMatt Jacob }
3080de461933SMatt Jacob 
3081de461933SMatt Jacob static void
3082e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
3083de461933SMatt Jacob {
3084e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3085e561aa79SAlexander Motin 
3086e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3087e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
3088e561aa79SAlexander Motin 	fc->loop_dead = 0;
3089e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3090e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
3091e561aa79SAlexander Motin }
3092e561aa79SAlexander Motin 
3093e561aa79SAlexander Motin static void
3094e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
3095e561aa79SAlexander Motin {
3096e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
3097e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3098f7c631bcSMatt Jacob 	fcportdb_t *lp;
3099e68eef14SAlexander Motin 	struct ac_contract ac;
3100e68eef14SAlexander Motin 	struct ac_device_changed *adc;
3101766a65a5SAlexander Motin 	int dbidx, i;
3102f7c631bcSMatt Jacob 
3103e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
31040a70657fSMatt Jacob 
3105f7c631bcSMatt Jacob 	/*
3106f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
3107f7c631bcSMatt Jacob 	 */
3108f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3109e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
3110f7c631bcSMatt Jacob 
3111e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
3112f7c631bcSMatt Jacob 			continue;
3113f7c631bcSMatt Jacob 
3114f7c631bcSMatt Jacob 		/*
3115f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3116f7c631bcSMatt Jacob 		 */
3117e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
3118e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
3119e95725cbSMatt Jacob 
3120970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3121e95725cbSMatt Jacob 				continue;
3122e95725cbSMatt Jacob 			}
3123e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3124e95725cbSMatt Jacob 				continue;
3125e95725cbSMatt Jacob                         }
3126766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
3127e95725cbSMatt Jacob 				continue;
3128e95725cbSMatt Jacob 			}
31296af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
31306af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
31316af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
3132e95725cbSMatt Jacob 		}
3133e95725cbSMatt Jacob 
3134e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3135e68eef14SAlexander Motin 		if (lp->is_target) {
3136e68eef14SAlexander Motin 			lp->is_target = 0;
3137766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
3138f7c631bcSMatt Jacob 		}
3139e68eef14SAlexander Motin 		if (lp->is_initiator) {
3140e68eef14SAlexander Motin 			lp->is_initiator = 0;
3141e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
3142e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
3143e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
3144e68eef14SAlexander Motin 			adc->port = lp->portid;
3145d4f3ad3aSAlexander Motin 			adc->target = dbidx;
3146e68eef14SAlexander Motin 			adc->arrived = 0;
3147e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
3148e95725cbSMatt Jacob 		}
3149e68eef14SAlexander Motin 	}
3150e68eef14SAlexander Motin 
3151e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
31522df76c16SMatt Jacob 	fc->loop_dead = 1;
3153e561aa79SAlexander Motin 	fc->loop_down_time = 0;
3154cc8df88bSMatt Jacob }
3155cc8df88bSMatt Jacob 
31565d571944SMatt Jacob static void
31575d571944SMatt Jacob isp_kthread(void *arg)
31585d571944SMatt Jacob {
31592df76c16SMatt Jacob 	struct isp_fc *fc = arg;
31602df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
31612df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3162e561aa79SAlexander Motin 	int slp = 0, d;
3163e561aa79SAlexander Motin 	int lb, lim;
3164a01f5aebSMatt Jacob 
31650a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
31662df76c16SMatt Jacob 
31671d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
3168e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3169e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
31702df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
3171e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3172e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
3173e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
31742df76c16SMatt Jacob 
31752df76c16SMatt Jacob 		/*
31762df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
31772df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
31782df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
31792df76c16SMatt Jacob 		 * check again.
31802df76c16SMatt Jacob 		 *
31812df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
31822df76c16SMatt Jacob 		 */
3183e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
3184e561aa79SAlexander Motin 			slp = 0;
318510365e5aSMatt Jacob 		} else {
318610365e5aSMatt Jacob 			/*
318710365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
3188f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
3189f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
3190f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
319110365e5aSMatt Jacob 			 */
3192e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
3193f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
3194e561aa79SAlexander Motin 			else
31952df76c16SMatt Jacob 				lim = fc->loop_down_limit;
3196e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
3197e561aa79SAlexander Motin 			if (d >= lim)
3198f7c631bcSMatt Jacob 				slp = 0;
3199e561aa79SAlexander Motin 			else if (d < 10)
320010365e5aSMatt Jacob 				slp = 1;
3201e561aa79SAlexander Motin 			else if (d < 30)
3202f7c631bcSMatt Jacob 				slp = 5;
3203e561aa79SAlexander Motin 			else if (d < 60)
3204f7c631bcSMatt Jacob 				slp = 10;
3205e561aa79SAlexander Motin 			else if (d < 120)
3206f7c631bcSMatt Jacob 				slp = 20;
3207e561aa79SAlexander Motin 			else
3208f7c631bcSMatt Jacob 				slp = 30;
3209f44257c2SMatt Jacob 		}
321010365e5aSMatt Jacob 
3211e561aa79SAlexander Motin 		if (slp == 0) {
3212e561aa79SAlexander Motin 			if (lb == LOOP_READY)
3213e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
3214387d8239SMatt Jacob 			else
3215e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
32165d571944SMatt Jacob 		}
3217fdeb9f2fSMatt Jacob 
3218e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3219e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
32202df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
32215d571944SMatt Jacob 	}
32221d0a1de2SWill Andrews 	fc->num_threads -= 1;
32230a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
32241d0a1de2SWill Andrews 	kthread_exit();
32255d571944SMatt Jacob }
32265d571944SMatt Jacob 
32278290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
32288290ea90SAlexander Motin static void
32298290ea90SAlexander Motin isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
32308290ea90SAlexander Motin {
32318290ea90SAlexander Motin 	atio_private_data_t *atp;
32328290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
32338290ea90SAlexander Motin 	struct ccb_hdr *sccb;
32348290ea90SAlexander Motin 	tstate_t *tptr;
32358290ea90SAlexander Motin 
32368290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
32378290ea90SAlexander Motin 	if (tptr != NULL) {
32388290ea90SAlexander Motin 		/* Search for the ATIO among queueued. */
32398290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
32408290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
32418290ea90SAlexander Motin 				continue;
32428290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
32438290ea90SAlexander Motin 			tptr->atio_count--;
32448290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
32458290ea90SAlexander Motin 			xpt_done(accb);
32468290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
32478290ea90SAlexander Motin 			return;
32488290ea90SAlexander Motin 		}
32498290ea90SAlexander Motin 	}
32508290ea90SAlexander Motin 
32518290ea90SAlexander Motin 	/* Search for the ATIO among running. */
32528290ea90SAlexander Motin 	atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
32538290ea90SAlexander Motin 	if (atp != NULL) {
32543f072d69SAlexander Motin 		/* Send TERMINATE to firmware. */
32553f072d69SAlexander Motin 		if (!atp->dead && IS_24XX(isp)) {
32563f072d69SAlexander Motin 			uint8_t storage[QENTRY_LEN];
32573f072d69SAlexander Motin 			ct7_entry_t *cto = (ct7_entry_t *) storage;
32583f072d69SAlexander Motin 
32593f072d69SAlexander Motin 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
32603f072d69SAlexander Motin 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
32613f072d69SAlexander Motin 			cto->ct_header.rqs_entry_count = 1;
32623f072d69SAlexander Motin 			cto->ct_nphdl = atp->nphdl;
32633f072d69SAlexander Motin 			cto->ct_rxid = atp->tag;
32643f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
32653f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
32663f072d69SAlexander Motin 			cto->ct_oxid = atp->oxid;
32673f072d69SAlexander Motin 			cto->ct_vpidx = XS_CHANNEL(accb);
32683f072d69SAlexander Motin 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
32693f072d69SAlexander Motin 			isp_target_put_entry(isp, cto);
32703f072d69SAlexander Motin 		}
32718290ea90SAlexander Motin 		isp_put_atpd(isp, XS_CHANNEL(accb), atp);
32728290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
32738290ea90SAlexander Motin 	} else {
32748290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
32758290ea90SAlexander Motin 	}
32768290ea90SAlexander Motin }
32778290ea90SAlexander Motin 
32788290ea90SAlexander Motin static void
32798290ea90SAlexander Motin isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
32808290ea90SAlexander Motin {
32818290ea90SAlexander Motin 	inot_private_data_t *ntp;
32828290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
32838290ea90SAlexander Motin 	struct ccb_hdr *sccb;
32848290ea90SAlexander Motin 	tstate_t *tptr;
32858290ea90SAlexander Motin 
32868290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
32878290ea90SAlexander Motin 	if (tptr != NULL) {
32888290ea90SAlexander Motin 		/* Search for the INOT among queueued. */
32898290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
32908290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
32918290ea90SAlexander Motin 				continue;
32928290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
32938290ea90SAlexander Motin 			tptr->inot_count--;
32948290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
32958290ea90SAlexander Motin 			xpt_done(accb);
32968290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
32978290ea90SAlexander Motin 			return;
32988290ea90SAlexander Motin 		}
32998290ea90SAlexander Motin 	}
33008290ea90SAlexander Motin 
33018290ea90SAlexander Motin 	/* Search for the INOT among running. */
33028290ea90SAlexander Motin 	ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
33038290ea90SAlexander Motin 	if (ntp != NULL) {
33048290ea90SAlexander Motin 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, ntp->data);
33058290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
33068290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
33078290ea90SAlexander Motin 	} else {
33088290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
33098290ea90SAlexander Motin 		return;
33108290ea90SAlexander Motin 	}
33118290ea90SAlexander Motin }
33128290ea90SAlexander Motin #endif
33138290ea90SAlexander Motin 
3314cc8df88bSMatt Jacob static void
3315c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
3316478f8a96SJustin T. Gibbs {
3317e561aa79SAlexander Motin 	int bus, tgt, ts, error;
33189cd7268eSMatt Jacob 	ispsoftc_t *isp;
33194663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
3320478f8a96SJustin T. Gibbs 
3321478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3322478f8a96SJustin T. Gibbs 
33239cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
33242df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
33255cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
33265cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
33272df76c16SMatt Jacob 
3328478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
3329478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
33302df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
3331478f8a96SJustin T. Gibbs 		/*
3332478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
3333478f8a96SJustin T. Gibbs 		 */
3334478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3335478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3336478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
33375cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3338478f8a96SJustin T. Gibbs 				break;
3339478f8a96SJustin T. Gibbs 			}
3340478f8a96SJustin T. Gibbs 		}
3341387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
33420470d791SMatt Jacob #ifdef	DIAGNOSTIC
33436af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3344dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
3345478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
33466af11b82SAlexander Motin 		} else if (ISP_MAX_LUNS(isp) > 0 &&
33476af11b82SAlexander Motin 		    ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3348dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
3349478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
3350478f8a96SJustin T. Gibbs 		}
3351478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3352478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3353478f8a96SJustin T. Gibbs 			break;
3354478f8a96SJustin T. Gibbs 		}
33550470d791SMatt Jacob #endif
33560a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
33570a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
33580a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
33590a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
33602df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
33615cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
33620a70657fSMatt Jacob 			xpt_done(ccb);
33630a70657fSMatt Jacob 			break;
33640a70657fSMatt Jacob 		}
3365b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
33660470d791SMatt Jacob 		switch (error) {
3367478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
3368478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
33690a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
33700a70657fSMatt Jacob 				break;
3371d69a5f7dSMatt Jacob 			}
33720a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
33730a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
33740a70657fSMatt Jacob 				ts = 60*1000;
3375cc8df88bSMatt Jacob 			}
33760a70657fSMatt Jacob 			ts = isp_mstohz(ts);
33772df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3378478f8a96SJustin T. Gibbs 			break;
33790470d791SMatt Jacob 		case CMD_RQLATER:
3380f44257c2SMatt Jacob 			/*
3381e561aa79SAlexander Motin 			 * We get this result if the loop isn't ready
3382e561aa79SAlexander Motin 			 * or if the device in question has gone zombie.
338310365e5aSMatt Jacob 			 */
3384e561aa79SAlexander Motin 			if (ISP_FC_PC(isp, bus)->loop_dead) {
33856af11b82SAlexander Motin 				isp_prt(isp, ISP_LOGDEBUG0,
3386e561aa79SAlexander Motin 				    "%d.%jx loop is dead",
3387e561aa79SAlexander Motin 				    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
33885cbe3e8eSAlexander Motin 				ccb->ccb_h.status = CAM_SEL_TIMEOUT;
33895cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
3390f44257c2SMatt Jacob 				break;
3391f44257c2SMatt Jacob 			}
33926af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
33936af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3394f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
33952df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3396387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
33970a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
3398478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3399478f8a96SJustin T. Gibbs 			break;
34000470d791SMatt Jacob 		case CMD_EAGAIN:
34010a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
34022df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
34032df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3404387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
3405478f8a96SJustin T. Gibbs 			xpt_done(ccb);
3406478f8a96SJustin T. Gibbs 			break;
34070470d791SMatt Jacob 		case CMD_COMPLETE:
34080470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
34090470d791SMatt Jacob 			break;
34100470d791SMatt Jacob 		default:
34112df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3412387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
34130a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
34140470d791SMatt Jacob 			xpt_done(ccb);
3415478f8a96SJustin T. Gibbs 		}
3416478f8a96SJustin T. Gibbs 		break;
3417478f8a96SJustin T. Gibbs 
3418d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
34192df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
34202df76c16SMatt Jacob 		if (ccb->cel.enable) {
34212df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
34222df76c16SMatt Jacob 		} else {
34232df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
342467ff51f1SMatt Jacob 		}
342567ff51f1SMatt Jacob 		break;
34262df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3427d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3428d81ba9d5SMatt Jacob 	{
34292df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3430d81ba9d5SMatt Jacob 		if (tptr == NULL) {
34312df76c16SMatt Jacob 			const char *str;
34322df76c16SMatt Jacob 
34338290ea90SAlexander Motin 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
34342df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
34358290ea90SAlexander Motin 			else
34362df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
34378290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
34385c0b756aSAlexander Motin 			    "%s: no state pointer found for %s\n",
34398290ea90SAlexander Motin 			    __func__, str);
34402df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
34415c0b756aSAlexander Motin 			xpt_done(ccb);
3442d81ba9d5SMatt Jacob 			break;
3443d81ba9d5SMatt Jacob 		}
3444387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
3445387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
3446570c7a3fSMatt Jacob 
3447d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
34488290ea90SAlexander Motin 			ccb->atio.tag_id = 0;
3449570c7a3fSMatt Jacob 			tptr->atio_count++;
34502df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
34518290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
34528290ea90SAlexander Motin 			    "Put FREE ATIO, count now %d\n", tptr->atio_count);
34532df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
34548290ea90SAlexander Motin 			ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3455746e9c85SMatt Jacob 			tptr->inot_count++;
34562df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
34578290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
34588290ea90SAlexander Motin 			    "Put FREE INOT, count now %d\n", tptr->inot_count);
3459d81ba9d5SMatt Jacob 		}
3460d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
3461d81ba9d5SMatt Jacob 		break;
3462d81ba9d5SMatt Jacob 	}
34632df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
3464d81ba9d5SMatt Jacob 	{
34652df76c16SMatt Jacob 		inot_private_data_t *ntp;
34662df76c16SMatt Jacob 
34672df76c16SMatt Jacob 		/*
34682df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
34692df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
34702df76c16SMatt Jacob 		 */
34712df76c16SMatt Jacob 		/*
34722df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
34732df76c16SMatt Jacob 		 */
34742df76c16SMatt 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);
34758290ea90SAlexander Motin 		ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
34762df76c16SMatt Jacob 		if (ntp == NULL) {
34772df76c16SMatt 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__,
34782df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
34792df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
34802df76c16SMatt Jacob 			xpt_done(ccb);
3481d81ba9d5SMatt Jacob 			break;
3482d81ba9d5SMatt Jacob 		}
34838290ea90SAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
348496b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
34852df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
34862df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3487387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3488387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
34892df76c16SMatt Jacob 			break;
34902df76c16SMatt Jacob 		}
34918290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
34922df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
34932df76c16SMatt 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);
34942df76c16SMatt Jacob 		xpt_done(ccb);
34952df76c16SMatt Jacob 		break;
34962df76c16SMatt Jacob 	}
34972df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
3498387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
34992df76c16SMatt Jacob 		break;
3500d81ba9d5SMatt Jacob #endif
3501478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
3502d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3503d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
3504d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
3505d81ba9d5SMatt Jacob 
35062df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3507478f8a96SJustin T. Gibbs 		if (error) {
3508478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3509478f8a96SJustin T. Gibbs 		} else {
3510b44e442eSWill Andrews 			/*
3511b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
3512b44e442eSWill Andrews 			 * Reference Number, because the target will expect
3513b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
3514b44e442eSWill Andrews 			 */
351521daf914SAlexander Motin 			if (IS_FC(isp))
351621daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3517b44e442eSWill Andrews 
3518478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
3519478f8a96SJustin T. Gibbs 		}
3520478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3521478f8a96SJustin T. Gibbs 		break;
3522478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
3523d81ba9d5SMatt Jacob 	{
3524d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
3525d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
3526d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3527d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
35288290ea90SAlexander Motin 			isp_abort_atio(isp, ccb);
35298290ea90SAlexander Motin 			break;
35308290ea90SAlexander Motin 		case XPT_IMMEDIATE_NOTIFY:
35318290ea90SAlexander Motin 			isp_abort_inot(isp, ccb);
3532d81ba9d5SMatt Jacob 			break;
3533d81ba9d5SMatt Jacob #endif
3534d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
3535950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3536478f8a96SJustin T. Gibbs 			if (error) {
3537d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
3538478f8a96SJustin T. Gibbs 			} else {
3539478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
3540478f8a96SJustin T. Gibbs 			}
3541d81ba9d5SMatt Jacob 			break;
3542d81ba9d5SMatt Jacob 		default:
3543d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
3544d81ba9d5SMatt Jacob 			break;
3545d81ba9d5SMatt Jacob 		}
35461c0a1eb2SMatt Jacob 		/*
35471c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
35481c0a1eb2SMatt Jacob 		 * complete when control is returned.
35491c0a1eb2SMatt Jacob 		 */
3550478f8a96SJustin T. Gibbs 		break;
3551d81ba9d5SMatt Jacob 	}
3552ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
3553478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3554478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
35559ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
35569ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
35579ce9bdafSMatt Jacob 			xpt_done(ccb);
35589ce9bdafSMatt Jacob 			break;
35599ce9bdafSMatt Jacob 		}
3560478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
3561805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
35622df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
35632df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
35642df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
35652df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
35661dae40ebSMatt Jacob 			uint16_t *dptr;
3567ab163f5fSMatt Jacob 
3568b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
3569b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
3570b61386a4SMatt Jacob 				xpt_done(ccb);
3571b61386a4SMatt Jacob 				break;
3572b61386a4SMatt Jacob 			}
3573b61386a4SMatt Jacob 
3574ab163f5fSMatt Jacob 			/*
35759ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
3576ab163f5fSMatt Jacob 			 * so any request to change settings just gets
3577ab163f5fSMatt Jacob 			 * vectored to that location.
3578ab163f5fSMatt Jacob 			 */
35799ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
3580ab163f5fSMatt Jacob 
3581ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3582ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3583ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
3584ab163f5fSMatt Jacob 				else
3585ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
3586ab163f5fSMatt Jacob 			}
3587ab163f5fSMatt Jacob 
3588ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3589ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3590ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
3591ab163f5fSMatt Jacob 				else
3592ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
3593ab163f5fSMatt Jacob 			}
3594ab163f5fSMatt Jacob 
3595ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3596ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3597ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
3598ab163f5fSMatt Jacob 				else
3599ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
3600ab163f5fSMatt Jacob 			}
3601ab163f5fSMatt Jacob 
3602ab163f5fSMatt Jacob 			/*
3603ab163f5fSMatt Jacob 			 * XXX: FIX ME
3604ab163f5fSMatt Jacob 			 */
36052df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3606ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
36079ce9bdafSMatt Jacob 				/*
36089ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
36099ce9bdafSMatt Jacob 				 */
36102df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
36112df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3612ab163f5fSMatt Jacob 			} else {
3613ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
3614ab163f5fSMatt Jacob 			}
3615123055f0SNathan 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,
36162df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3617478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
36182df76c16SMatt Jacob 			sdp->update = 1;
3619478f8a96SJustin T. Gibbs 		}
3620478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3621478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3622478f8a96SJustin T. Gibbs 		break;
3623478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
3624478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
3625478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
36262df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3627ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
36282df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
36292df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
36302df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3631478f8a96SJustin T. Gibbs 
3632ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3633ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3634ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
3635ab163f5fSMatt Jacob 			cts->transport_version = 0;
3636ab163f5fSMatt Jacob 
36379b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
36389b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3639ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
3640ab163f5fSMatt Jacob 			fc->bitrate = 100000;
3641fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
3642766a65a5SAlexander Motin 			if (tgt < MAX_FC_TARG) {
3643766a65a5SAlexander Motin 				fcportdb_t *lp = &fcp->portdb[tgt];
3644ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
3645ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
3646ab163f5fSMatt Jacob 				fc->port = lp->portid;
36472df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3648ab163f5fSMatt Jacob 			}
3649ab163f5fSMatt Jacob 		} else {
36502df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
36512df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
36522df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
36531dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
3654ab163f5fSMatt Jacob 
3655ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
365683ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
36572df76c16SMatt Jacob 				sdp->update = 1;
36582df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
36599ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
36609ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
36619ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
36624394c92fSMatt Jacob 			} else {
36639ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
36649ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
36659ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
36664394c92fSMatt Jacob 			}
3667478f8a96SJustin T. Gibbs 
3668ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
3669ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
3670ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
3671ab163f5fSMatt Jacob 			cts->transport_version = 2;
3672ab163f5fSMatt Jacob 
3673b61386a4SMatt Jacob 			spi->valid = 0;
3674b61386a4SMatt Jacob 			scsi->valid = 0;
3675b61386a4SMatt Jacob 			spi->flags = 0;
3676b61386a4SMatt Jacob 			scsi->flags = 0;
3677ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
3678ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3679ab163f5fSMatt Jacob 			}
36809ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
3681ab163f5fSMatt Jacob 				spi->sync_offset = oval;
3682ab163f5fSMatt Jacob 				spi->sync_period = pval;
3683b61386a4SMatt Jacob 			} else {
3684b61386a4SMatt Jacob 				spi->sync_offset = 0;
3685b61386a4SMatt Jacob 				spi->sync_period = 0;
3686b61386a4SMatt Jacob 			}
3687ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3688ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3689ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3690ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
3691ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3692ab163f5fSMatt Jacob 			} else {
3693ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3694ab163f5fSMatt Jacob 			}
3695ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3696ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
3697b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
3698b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3699b61386a4SMatt Jacob 				}
3700ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
3701ab163f5fSMatt Jacob 			}
3702123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3703123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3704478f8a96SJustin T. Gibbs 		}
3705478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3706478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3707478f8a96SJustin T. Gibbs 		break;
3708478f8a96SJustin T. Gibbs 
3709478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
37102df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
37112df76c16SMatt Jacob 		xpt_done(ccb);
37122df76c16SMatt Jacob 		break;
3713478f8a96SJustin T. Gibbs 
3714478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
3715ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
37162df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
37172df76c16SMatt Jacob 		if (error) {
3718478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
37192df76c16SMatt Jacob 			xpt_done(ccb);
37202df76c16SMatt Jacob 			break;
37212df76c16SMatt Jacob 		}
37220a70657fSMatt Jacob 		if (bootverbose) {
37232df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
37240a70657fSMatt Jacob 		}
37252df76c16SMatt Jacob 		if (IS_FC(isp)) {
37262df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
37272df76c16SMatt Jacob 		} else {
37282df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
37292df76c16SMatt Jacob 		}
3730478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
3731478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3732478f8a96SJustin T. Gibbs 		break;
3733478f8a96SJustin T. Gibbs 
3734478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
3735478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3736478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3737478f8a96SJustin T. Gibbs 		break;
3738478f8a96SJustin T. Gibbs 
37392df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
37402df76c16SMatt Jacob 	{
37412df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
37422df76c16SMatt Jacob 		fcparam *fcp;
37432df76c16SMatt Jacob 
37442df76c16SMatt Jacob 		if (!IS_FC(isp)) {
37452df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
37462df76c16SMatt Jacob 			xpt_done(ccb);
37472df76c16SMatt Jacob 			break;
37482df76c16SMatt Jacob 		}
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 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
37542df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
37552df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
37562df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
37572df76c16SMatt Jacob 		}
37582df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
37592df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
37602df76c16SMatt Jacob 			int rchange = 0;
37612df76c16SMatt Jacob 			int newrole = 0;
37622df76c16SMatt Jacob 
37632df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
37642df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
37652df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
37662df76c16SMatt Jacob 					rchange = 1;
37672df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
37682df76c16SMatt Jacob 				}
37692df76c16SMatt Jacob 				break;
37702df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
37712df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
37722df76c16SMatt Jacob 					rchange = 1;
37732df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
37742df76c16SMatt Jacob 				}
37752df76c16SMatt Jacob 				break;
37762df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
37772df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
37782df76c16SMatt Jacob 					rchange = 1;
37792df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
37802df76c16SMatt Jacob 				}
37812df76c16SMatt Jacob 				break;
37822df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
37832df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
37842df76c16SMatt Jacob 					rchange = 1;
37852df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
37862df76c16SMatt Jacob 				}
37872df76c16SMatt Jacob 				break;
37882df76c16SMatt Jacob 			}
37892df76c16SMatt Jacob 			if (rchange) {
3790e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
37916bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
37926bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
37932df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3794387d8239SMatt Jacob 					xpt_done(ccb);
3795387d8239SMatt Jacob 					break;
37962df76c16SMatt Jacob 				}
37972df76c16SMatt Jacob 			}
37982df76c16SMatt Jacob 		}
37992df76c16SMatt Jacob 		xpt_done(ccb);
38002df76c16SMatt Jacob 		break;
38012df76c16SMatt Jacob 	}
3802a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
3803e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
38042df76c16SMatt Jacob 	{
38052df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
38062df76c16SMatt Jacob 
38072df76c16SMatt Jacob 		if (IS_FC(isp)) {
38082df76c16SMatt Jacob 			fcparam *fcp;
38092df76c16SMatt Jacob 
38102df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
38112df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
38122df76c16SMatt Jacob 
38132df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
38142df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
38152df76c16SMatt Jacob 			switch (fcp->role) {
38162df76c16SMatt Jacob 			case ISP_ROLE_NONE:
38172df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
38182df76c16SMatt Jacob 				break;
38192df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
38202df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
38212df76c16SMatt Jacob 				break;
38222df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
38232df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
38242df76c16SMatt Jacob 				break;
38252df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
38262df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
38272df76c16SMatt Jacob 				break;
38282df76c16SMatt Jacob 			}
38292df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
38302df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
38312df76c16SMatt Jacob 		} else {
38322df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
38332df76c16SMatt Jacob 		}
38342df76c16SMatt Jacob 		xpt_done(ccb);
38352df76c16SMatt Jacob 		break;
38362df76c16SMatt Jacob 	}
3837478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
3838478f8a96SJustin T. Gibbs 	{
3839478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
3840478f8a96SJustin T. Gibbs 
3841478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
3842d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
38433e6deb33SAlexander Motin 		if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3844a1bc34c6SMatt Jacob 			cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
38453e6deb33SAlexander Motin 		else
3846d81ba9d5SMatt Jacob #endif
38473e6deb33SAlexander Motin 			cpi->target_sprt = 0;
3848478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
38490470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
38506af11b82SAlexander Motin 		cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
38516af11b82SAlexander Motin 		    255 : ISP_MAX_LUNS(isp) - 1;
38520470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
38537bf825d1SKenneth D. Merry 		if (isp->isp_osinfo.sixtyfourbit)
38547bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
38557bf825d1SKenneth D. Merry 		else
38567bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
38577bf825d1SKenneth D. Merry 
38582df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
38594394c92fSMatt Jacob 		if (IS_FC(isp)) {
38602df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
38612df76c16SMatt Jacob 
3862ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3863*229203afSAlexander Motin 			cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
38642df76c16SMatt Jacob 
38650470d791SMatt Jacob 			/*
38660470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
38670470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
38680470d791SMatt Jacob 			 */
38690470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
38700470d791SMatt Jacob 
38719deea857SKenneth D. Merry 			/*
38722df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
38739deea857SKenneth D. Merry 			 */
3874e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
3875e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
3876e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
38772df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
38782df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
38792df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
38802df76c16SMatt Jacob 			} else {
38812df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
38822df76c16SMatt Jacob 			}
38830470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
3884ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
3885805e1f82SMatt Jacob 			cpi->transport_version = 0;
38862df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
38872df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
38882df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
38892df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3890478f8a96SJustin T. Gibbs 		} else {
38912df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
38920470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3893ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
3894ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
38959deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
3896ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
3897805e1f82SMatt Jacob 			cpi->transport_version = 2;
3898478f8a96SJustin T. Gibbs 		}
3899ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
3900ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
39014195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
39024195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
39034195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3904478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
3905478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
3906478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3907478f8a96SJustin T. Gibbs 		break;
3908478f8a96SJustin T. Gibbs 	}
3909478f8a96SJustin T. Gibbs 	default:
3910478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
3911478f8a96SJustin T. Gibbs 		xpt_done(ccb);
3912478f8a96SJustin T. Gibbs 		break;
3913478f8a96SJustin T. Gibbs 	}
3914478f8a96SJustin T. Gibbs }
3915d3a9eb2eSMatt Jacob 
3916d3a9eb2eSMatt Jacob #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
3917f7c631bcSMatt Jacob 
3918d3a9eb2eSMatt Jacob void
39192df76c16SMatt Jacob isp_done(XS_T *sccb)
3920d3a9eb2eSMatt Jacob {
39219cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
3922e95725cbSMatt Jacob 	uint32_t status;
3923d3a9eb2eSMatt Jacob 
3924d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
3925d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
3926b85389e1SMatt Jacob 
39272df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3928d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
39292df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
393092a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
393192a1e549SMatt Jacob 		} else {
3932d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3933d3a9eb2eSMatt Jacob 		}
393492a1e549SMatt Jacob 	}
3935b85389e1SMatt Jacob 
39360470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3937e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
3938e95725cbSMatt Jacob 	if (status != CAM_REQ_CMP) {
3939e95725cbSMatt Jacob 		if (status != CAM_SEL_TIMEOUT)
39406af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0,
39416af11b82SAlexander Motin 			    "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
39426af11b82SAlexander Motin 			    XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
39436af11b82SAlexander Motin 			    sccb->ccb_h.status, sccb->scsi_status);
39445704e6f0SKenneth D. Merry 		else if ((IS_FC(isp))
39455704e6f0SKenneth D. Merry 		      && (XS_TGT(sccb) < MAX_FC_TARG)) {
39465704e6f0SKenneth D. Merry 			fcparam *fcp;
39475704e6f0SKenneth D. Merry 
39485704e6f0SKenneth D. Merry 			fcp = FCPARAM(isp, XS_CHANNEL(sccb));
3949e68eef14SAlexander Motin 			fcp->portdb[XS_TGT(sccb)].is_target = 0;
39505704e6f0SKenneth D. Merry 		}
3951d3a9eb2eSMatt Jacob 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3952d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
39530470d791SMatt Jacob 			xpt_freeze_devq(sccb->ccb_h.path, 1);
3954d3a9eb2eSMatt Jacob 		}
3955d3a9eb2eSMatt Jacob 	}
3956b85389e1SMatt Jacob 
39572df76c16SMatt Jacob 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
39582df76c16SMatt Jacob 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
3959d3a9eb2eSMatt Jacob 	}
3960b85389e1SMatt Jacob 
39615cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
3962387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
39630ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
39640a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
39655cbe3e8eSAlexander Motin 	}
3966d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
3967d3a9eb2eSMatt Jacob }
3968d3a9eb2eSMatt Jacob 
39692df76c16SMatt Jacob void
39702df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3971cbf57b47SMatt Jacob {
3972e68eef14SAlexander Motin 	int bus;
3973e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3974387d8239SMatt Jacob 	char buf[64];
3975f7c631bcSMatt Jacob 	char *msg = NULL;
397610365e5aSMatt Jacob 	target_id_t tgt;
397710365e5aSMatt Jacob 	fcportdb_t *lp;
3978a01f5aebSMatt Jacob 	struct isp_fc *fc;
397910365e5aSMatt Jacob 	struct cam_path *tmppath;
3980e68eef14SAlexander Motin 	struct ac_contract ac;
3981e68eef14SAlexander Motin 	struct ac_device_changed *adc;
39822df76c16SMatt Jacob 	va_list ap;
398310365e5aSMatt Jacob 
3984cbf57b47SMatt Jacob 	switch (cmd) {
3985cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
39860470d791SMatt Jacob 	{
3987ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
3988ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
3989cbf57b47SMatt Jacob 		int flags, tgt;
39902df76c16SMatt Jacob 		sdparam *sdp;
3991ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
3992cbf57b47SMatt Jacob 
399329f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
3994ab163f5fSMatt Jacob 
39952df76c16SMatt Jacob 		va_start(ap, cmd);
39962df76c16SMatt Jacob 		bus = va_arg(ap, int);
39972df76c16SMatt Jacob 		tgt = va_arg(ap, int);
39982df76c16SMatt Jacob 		va_end(ap);
39992df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
40002df76c16SMatt Jacob 
40012df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
40022df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4003cbf57b47SMatt Jacob 			break;
4004cbf57b47SMatt Jacob 		}
40059ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
4006ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4007ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
4008ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
4009ab163f5fSMatt Jacob 
4010ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
4011ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
4012ab163f5fSMatt Jacob 
4013ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
4014ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
4015ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4016ab163f5fSMatt Jacob 		}
4017ab163f5fSMatt Jacob 
4018cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
4019ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
4020ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4021ab163f5fSMatt Jacob 		}
4022ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4023ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
4024ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4025ab163f5fSMatt Jacob 		} else {
4026ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4027ab163f5fSMatt Jacob 		}
4028ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
4029ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4030ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
40319ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
40329ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4033ab163f5fSMatt Jacob 		}
40342df76c16SMatt 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);
4035ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4036ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4037cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
4038cbf57b47SMatt Jacob 		break;
40390470d791SMatt Jacob 	}
404057c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
40412df76c16SMatt Jacob 	{
40422df76c16SMatt Jacob 		va_start(ap, cmd);
40432df76c16SMatt Jacob 		bus = va_arg(ap, int);
40442df76c16SMatt Jacob 		va_end(ap);
40452df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
40462df76c16SMatt Jacob 		if (IS_FC(isp)) {
40472df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
40482df76c16SMatt Jacob 		} else {
40492df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
405057c801f5SMatt Jacob 		}
405157c801f5SMatt Jacob 		break;
40522df76c16SMatt Jacob 	}
40535d571944SMatt Jacob 	case ISPASYNC_LIP:
405437a7daacSAlexander Motin 		if (msg == NULL)
4055f7c631bcSMatt Jacob 			msg = "LIP Received";
4056f7c631bcSMatt Jacob 		/* FALLTHROUGH */
40575d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
405837a7daacSAlexander Motin 		if (msg == NULL)
4059f7c631bcSMatt Jacob 			msg = "LOOP Reset";
4060f7c631bcSMatt Jacob 		/* FALLTHROUGH */
406157c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
406237a7daacSAlexander Motin 		if (msg == NULL)
4063f7c631bcSMatt Jacob 			msg = "LOOP Down";
40642df76c16SMatt Jacob 		va_start(ap, cmd);
40652df76c16SMatt Jacob 		bus = va_arg(ap, int);
40662df76c16SMatt Jacob 		va_end(ap);
406721daf914SAlexander Motin 		isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
4068e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
4069e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
407057c801f5SMatt Jacob 		break;
407157c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
40722df76c16SMatt Jacob 		va_start(ap, cmd);
40732df76c16SMatt Jacob 		bus = va_arg(ap, int);
40742df76c16SMatt Jacob 		va_end(ap);
4075e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
40762df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
407757c801f5SMatt Jacob 		break;
407810365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
40792df76c16SMatt Jacob 		va_start(ap, cmd);
40802df76c16SMatt Jacob 		bus = va_arg(ap, int);
40812df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
40822df76c16SMatt Jacob 		va_end(ap);
4083a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4084e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4085387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4086e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4087e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4088e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4089e68eef14SAlexander Motin 			lp->is_target = 1;
409021daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
40915704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
4092e68eef14SAlexander Motin 		}
4093e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4094e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4095e68eef14SAlexander Motin 			lp->is_initiator = 1;
4096e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4097e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4098e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4099e68eef14SAlexander Motin 			adc->port = lp->portid;
4100d4f3ad3aSAlexander Motin 			adc->target = tgt;
4101e68eef14SAlexander Motin 			adc->arrived = 1;
4102e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
410310365e5aSMatt Jacob 		}
410410365e5aSMatt Jacob 		break;
410510365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
41062df76c16SMatt Jacob 		va_start(ap, cmd);
41072df76c16SMatt Jacob 		bus = va_arg(ap, int);
41082df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
41092df76c16SMatt Jacob 		va_end(ap);
4110a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4111e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4112e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4113e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4114e68eef14SAlexander Motin changed:
4115e68eef14SAlexander Motin 		if (lp->is_target !=
4116e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4117e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4118e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
4119e68eef14SAlexander Motin 			if (lp->is_target) {
412021daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4121e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
412210365e5aSMatt Jacob 			} else {
4123e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
412421daf914SAlexander Motin 				isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
412510365e5aSMatt Jacob 			}
4126f7c631bcSMatt Jacob 		}
4127e68eef14SAlexander Motin 		if (lp->is_initiator !=
4128e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4129e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4130e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
4131e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
4132e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
4133e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
4134e68eef14SAlexander Motin 			adc->port = lp->portid;
4135d4f3ad3aSAlexander Motin 			adc->target = tgt;
4136e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
4137e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
4138e68eef14SAlexander Motin 		}
413910365e5aSMatt Jacob 		break;
414010365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
41412df76c16SMatt Jacob 		va_start(ap, cmd);
41422df76c16SMatt Jacob 		bus = va_arg(ap, int);
41432df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
41442df76c16SMatt Jacob 		va_end(ap);
4145fb4a4356SKenneth D. Merry 		fc = ISP_FC_PC(isp, bus);
4146e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4147e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4148e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4149e68eef14SAlexander Motin 		goto changed;
415010365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
41512df76c16SMatt Jacob 		va_start(ap, cmd);
41522df76c16SMatt Jacob 		bus = va_arg(ap, int);
41532df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
41542df76c16SMatt Jacob 		va_end(ap);
4155a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
4156e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
4157f7c631bcSMatt Jacob 		/*
4158e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
4159e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
4160f7c631bcSMatt Jacob 		 */
4161387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4162e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
4163f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
4164e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
4165e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4166a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
4167387d8239SMatt 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);
4168a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
4169f7c631bcSMatt Jacob 			}
4170e68eef14SAlexander Motin 			break;
41714b9d588eSMatt Jacob 		}
4172e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
417310365e5aSMatt Jacob 		break;
417410365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
417510365e5aSMatt Jacob 	{
417610365e5aSMatt Jacob 		char *msg;
4177eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
41782df76c16SMatt Jacob 
41792df76c16SMatt Jacob 		va_start(ap, cmd);
41802df76c16SMatt Jacob 		bus = va_arg(ap, int);
41812df76c16SMatt Jacob 		evt = va_arg(ap, int);
4182eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
41832df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
41842df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
41852df76c16SMatt Jacob 			reason = va_arg(ap, int);
4186eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4187eea52482SAlexander Motin 			portid = va_arg(ap, int);
418810365e5aSMatt Jacob 		} else {
41892df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
41902df76c16SMatt Jacob 			nlstate = reason = 0;
419110365e5aSMatt Jacob 		}
41922df76c16SMatt Jacob 		va_end(ap);
4193a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
41942df76c16SMatt Jacob 
41952df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
4196b5d5037bSAlexander Motin 			msg = "Port Database Changed";
4197eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
4198eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4199eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
42002df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
4201b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
4202eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4203eea52482SAlexander Motin 			    bus, msg, portid);
42042df76c16SMatt Jacob 		} else {
4205b5d5037bSAlexander Motin 			msg = "Other Change Notify";
4206eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
42072df76c16SMatt Jacob 		}
4208e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
420957c801f5SMatt Jacob 		break;
421002ab3379SMatt Jacob 	}
4211d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4212e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
4213d81ba9d5SMatt Jacob 	{
42142df76c16SMatt Jacob 		isp_notify_t *notify;
42152df76c16SMatt Jacob 		va_start(ap, cmd);
42162df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
42172df76c16SMatt Jacob 		va_end(ap);
42182df76c16SMatt Jacob 		switch (notify->nt_ncode) {
42192df76c16SMatt Jacob 		case NT_ABORT_TASK:
42202df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
42212df76c16SMatt Jacob 		case NT_CLEAR_ACA:
42222df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
42232df76c16SMatt Jacob 		case NT_LUN_RESET:
42242df76c16SMatt Jacob 		case NT_TARGET_RESET:
4225c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
4226c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
42272df76c16SMatt Jacob 			/*
42282df76c16SMatt Jacob 			 * These are task management functions.
42292df76c16SMatt Jacob 			 */
42302df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
42312df76c16SMatt Jacob 			break;
42322df76c16SMatt Jacob 		case NT_BUS_RESET:
42332df76c16SMatt Jacob 		case NT_LIP_RESET:
42342df76c16SMatt Jacob 		case NT_LINK_UP:
42352df76c16SMatt Jacob 		case NT_LINK_DOWN:
4236e68eef14SAlexander Motin 		case NT_HBA_RESET:
42372df76c16SMatt Jacob 			/*
42382df76c16SMatt Jacob 			 * No action need be taken here.
42392df76c16SMatt Jacob 			 */
42402df76c16SMatt Jacob 			break;
4241387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
42422df76c16SMatt Jacob 		case NT_LOGOUT:
42432df76c16SMatt Jacob 			/*
42442df76c16SMatt Jacob 			 * This is device arrival/departure notification
42452df76c16SMatt Jacob 			 */
424696b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
42472df76c16SMatt Jacob 			break;
42482df76c16SMatt Jacob 		default:
42492df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
425096b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
42512df76c16SMatt Jacob 			break;
42522df76c16SMatt Jacob 		}
4253d81ba9d5SMatt Jacob 		break;
4254d81ba9d5SMatt Jacob 	}
4255387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
4256387d8239SMatt Jacob 	{
4257387d8239SMatt Jacob 		void *inot;
4258387d8239SMatt Jacob 		va_start(ap, cmd);
4259387d8239SMatt Jacob 		inot = va_arg(ap, void *);
4260387d8239SMatt Jacob 		va_end(ap);
4261387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
4262387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4263387d8239SMatt Jacob 			if (tp) {
4264387d8239SMatt Jacob 				tp->isp = isp;
4265387d8239SMatt Jacob 				if (inot) {
4266387d8239SMatt Jacob 					memcpy(tp->data, inot, sizeof (tp->data));
4267387d8239SMatt Jacob 					tp->not = tp->data;
4268387d8239SMatt Jacob 				} else {
4269387d8239SMatt Jacob 					tp->not = NULL;
4270387d8239SMatt Jacob 				}
42712a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
42722a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
42732a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
4274387d8239SMatt Jacob 			} else {
4275387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4276387d8239SMatt Jacob 			}
4277387d8239SMatt Jacob 		}
4278387d8239SMatt Jacob 		break;
4279387d8239SMatt Jacob 	}
4280d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
42812df76c16SMatt Jacob 	{
42822df76c16SMatt Jacob 		isphdr_t *hp;
42832df76c16SMatt Jacob 
42842df76c16SMatt Jacob 		va_start(ap, cmd);
42852df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
42862df76c16SMatt Jacob 		va_end(ap);
42872df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
4288cbf57b47SMatt Jacob 		default:
42892df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4290d81ba9d5SMatt Jacob 			break;
4291570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
42923e6deb33SAlexander Motin 			if (IS_24XX(isp)) {
42932df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4294570c7a3fSMatt Jacob 			} else {
42952df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4296570c7a3fSMatt Jacob 			}
4297570c7a3fSMatt Jacob 			break;
4298d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
42992df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4300d81ba9d5SMatt Jacob 			break;
4301d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
43022df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4303d81ba9d5SMatt Jacob 			break;
43042df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
4305d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
4306d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
4307d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
43082df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
4309d81ba9d5SMatt Jacob 			break;
43102df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
43112df76c16SMatt Jacob 		{
43122df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
43132df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
43148290ea90SAlexander Motin 			atio_private_data_t *atp;
43152df76c16SMatt Jacob 			fcportdb_t *lp;
43162df76c16SMatt Jacob 			uint16_t chan;
43172df76c16SMatt Jacob 			uint32_t sid, did;
43182df76c16SMatt Jacob 
43192df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
43202df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
43212df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
43222df76c16SMatt Jacob 
43232df76c16SMatt Jacob 			nt->nt_hba = isp;
43242df76c16SMatt Jacob 			nt->nt_did = did;
43252df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
43262df76c16SMatt Jacob 			nt->nt_sid = sid;
43272df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
43282df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
43292df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
43302df76c16SMatt Jacob 			} else {
43312df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4332e68eef14SAlexander Motin 				if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
43332df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
43342df76c16SMatt Jacob 				} else {
43352df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
43362df76c16SMatt Jacob 				}
43372df76c16SMatt Jacob 			}
43382df76c16SMatt Jacob 			/*
43392df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
43402df76c16SMatt Jacob 			 */
43418290ea90SAlexander Motin 			atp = isp_find_atpd(isp, chan, abts->abts_rxid_task);
43428290ea90SAlexander Motin 			nt->nt_lun = atp ? atp->lun : LUN_ANY;
43432df76c16SMatt Jacob 			nt->nt_need_ack = 1;
43442df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
43452df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
43462df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
43472df76c16SMatt 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)",
43482df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
43492df76c16SMatt Jacob 			} else {
43502df76c16SMatt 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)",
43512df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
43522df76c16SMatt Jacob 			}
43532df76c16SMatt Jacob 			nt->nt_channel = chan;
43542df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
43552df76c16SMatt Jacob 			nt->nt_lreserved = hp;
43562df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
43572df76c16SMatt Jacob 			break;
43582df76c16SMatt Jacob 		}
4359d81ba9d5SMatt Jacob 		}
4360d81ba9d5SMatt Jacob 		break;
43612df76c16SMatt Jacob 	}
4362d81ba9d5SMatt Jacob #endif
4363ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
4364ab163f5fSMatt Jacob 	{
43651dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
4366ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
4367ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
4368ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
4369ab163f5fSMatt Jacob 		} else {
4370ab163f5fSMatt Jacob 			mbox6 = 0;
4371ab163f5fSMatt Jacob 		}
43722df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
43730a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
43740a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
43752df76c16SMatt Jacob 		isp_reinit(isp, 1);
43760a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
43770a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4378ab163f5fSMatt Jacob 		break;
4379ab163f5fSMatt Jacob 	}
4380d81ba9d5SMatt Jacob 	default:
4381b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4382cbf57b47SMatt Jacob 		break;
4383cbf57b47SMatt Jacob 	}
4384cbf57b47SMatt Jacob }
4385cbf57b47SMatt Jacob 
438692718a7fSMatt Jacob 
438792718a7fSMatt Jacob /*
438892718a7fSMatt Jacob  * Locks are held before coming here.
438992718a7fSMatt Jacob  */
439092718a7fSMatt Jacob void
43919cd7268eSMatt Jacob isp_uninit(ispsoftc_t *isp)
439292718a7fSMatt Jacob {
439310365e5aSMatt Jacob 	if (IS_24XX(isp)) {
439410365e5aSMatt Jacob 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
439510365e5aSMatt Jacob 	} else {
4396ea6f23cdSMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
439710365e5aSMatt Jacob 	}
439810365e5aSMatt Jacob 	ISP_DISABLE_INTS(isp);
439992718a7fSMatt Jacob }
4400b09b0095SMatt Jacob 
44012df76c16SMatt Jacob uint64_t
44022df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
44032df76c16SMatt Jacob {
44042df76c16SMatt Jacob 	uint64_t seed;
44052df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
44062df76c16SMatt Jacob 
4407a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
4408a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4409a193dc4bSAlexander Motin 	if (seed)
4410a193dc4bSAlexander Motin 		return (seed);
4411a193dc4bSAlexander Motin 
4412a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
44132df76c16SMatt Jacob 	if (isactive) {
4414a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4415a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
4416a193dc4bSAlexander Motin 		if (seed)
44172df76c16SMatt Jacob 			return (seed);
44182df76c16SMatt Jacob 	}
4419a193dc4bSAlexander Motin 
4420a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
4421a193dc4bSAlexander Motin 	if (chan > 0) {
4422a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4423a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
4424a193dc4bSAlexander Motin 		if (seed == 0) {
4425a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4426a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
44275cc3786cSMatt Jacob 		}
44282df76c16SMatt Jacob 	}
44292df76c16SMatt Jacob 
4430a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
4431a193dc4bSAlexander Motin 	if (seed == 0) {
4432a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4433a193dc4bSAlexander Motin 		if (!iswwnn)
4434a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
4435a193dc4bSAlexander Motin 	}
44362df76c16SMatt Jacob 
4437a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
4438a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
44392df76c16SMatt Jacob 		/*
44402df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
44412df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
4442a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
44432df76c16SMatt Jacob 		 */
4444a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
4445a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4446a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
44472df76c16SMatt Jacob 	}
44482df76c16SMatt Jacob 	return (seed);
44492df76c16SMatt Jacob }
44502df76c16SMatt Jacob 
4451b09b0095SMatt Jacob void
44529cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4453b09b0095SMatt Jacob {
4454de461933SMatt Jacob 	int loc;
4455387d8239SMatt Jacob 	char lbuf[200];
4456b09b0095SMatt Jacob 	va_list ap;
4457de461933SMatt Jacob 
4458b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4459b09b0095SMatt Jacob 		return;
4460b09b0095SMatt Jacob 	}
4461387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4462de461933SMatt Jacob 	loc = strlen(lbuf);
4463b09b0095SMatt Jacob 	va_start(ap, fmt);
4464de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4465b09b0095SMatt Jacob 	va_end(ap);
4466de461933SMatt Jacob 	printf("%s\n", lbuf);
4467b09b0095SMatt Jacob }
4468f7c631bcSMatt Jacob 
4469670508b1SMatt Jacob void
4470670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4471670508b1SMatt Jacob {
4472670508b1SMatt Jacob 	va_list ap;
4473670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4474670508b1SMatt Jacob 		return;
4475670508b1SMatt Jacob 	}
4476670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
4477670508b1SMatt Jacob 	va_start(ap, fmt);
4478670508b1SMatt Jacob 	vprintf(fmt, ap);
4479670508b1SMatt Jacob 	va_end(ap);
4480670508b1SMatt Jacob 	printf("\n");
4481670508b1SMatt Jacob }
4482670508b1SMatt Jacob 
4483f7c631bcSMatt Jacob uint64_t
4484f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
4485f7c631bcSMatt Jacob {
4486f7c631bcSMatt Jacob 	uint64_t elapsed;
4487f7c631bcSMatt Jacob 	struct timespec x = *b;
4488f7c631bcSMatt Jacob 	timespecsub(&x, a);
4489f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
4490f7c631bcSMatt Jacob 	if (elapsed == 0)
4491f7c631bcSMatt Jacob 		elapsed++;
4492f7c631bcSMatt Jacob 	return (elapsed);
4493f7c631bcSMatt Jacob }
4494f7c631bcSMatt Jacob 
4495f7c631bcSMatt Jacob int
4496f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
4497f7c631bcSMatt Jacob {
4498f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
4499f7c631bcSMatt Jacob 		return (1);
4500f7c631bcSMatt Jacob 	} else {
4501f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
4502f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
4503f7c631bcSMatt Jacob 		return (0);
4504f7c631bcSMatt Jacob 	}
4505f7c631bcSMatt Jacob }
4506f7c631bcSMatt Jacob 
4507f7c631bcSMatt Jacob void
4508f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4509f7c631bcSMatt Jacob {
4510a4f3a2beSMatt Jacob 	unsigned int usecs = mbp->timeout;
4511a4f3a2beSMatt Jacob 	unsigned int max, olim, ilim;
4512f7c631bcSMatt Jacob 
4513f7c631bcSMatt Jacob 	if (usecs == 0) {
4514f7c631bcSMatt Jacob 		usecs = MBCMD_DEFAULT_TIMEOUT;
4515f7c631bcSMatt Jacob 	}
4516a4f3a2beSMatt Jacob 	max = isp->isp_mbxwrk0 + 1;
4517a4f3a2beSMatt Jacob 
4518f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
4519a4f3a2beSMatt Jacob 		unsigned int ms = (usecs + 999) / 1000;
4520a4f3a2beSMatt Jacob 
4521f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
4522f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
4523a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
45242df76c16SMatt Jacob 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
4525a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4526a4f3a2beSMatt Jacob 				break;
4527a4f3a2beSMatt Jacob 			}
4528a4f3a2beSMatt Jacob 		}
4529f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
4530f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
4531f7c631bcSMatt Jacob 	} else {
4532a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
4533a4f3a2beSMatt Jacob 			for (ilim = 0; ilim < usecs; ilim += 100) {
45346ce548a1SAlexander Motin 				uint16_t isr, sema, info;
4535f7c631bcSMatt Jacob 				if (isp->isp_osinfo.mboxcmd_done) {
4536f7c631bcSMatt Jacob 					break;
4537f7c631bcSMatt Jacob 				}
45386ce548a1SAlexander Motin 				if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
45396ce548a1SAlexander Motin 					isp_intr(isp, isr, sema, info);
4540f7c631bcSMatt Jacob 					if (isp->isp_osinfo.mboxcmd_done) {
4541f7c631bcSMatt Jacob 						break;
4542f7c631bcSMatt Jacob 					}
4543f7c631bcSMatt Jacob 				}
45442df76c16SMatt Jacob 				ISP_DELAY(100);
4545f7c631bcSMatt Jacob 			}
4546a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
4547a4f3a2beSMatt Jacob 				break;
4548a4f3a2beSMatt Jacob 			}
4549a4f3a2beSMatt Jacob 		}
4550f7c631bcSMatt Jacob 	}
4551f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
45522df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
45532df76c16SMatt Jacob 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
4554f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
4555f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
4556f7c631bcSMatt Jacob 	}
4557f7c631bcSMatt Jacob }
4558f7c631bcSMatt Jacob 
4559f7c631bcSMatt Jacob void
4560f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
4561f7c631bcSMatt Jacob {
4562f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleeping) {
4563f7c631bcSMatt Jacob 		wakeup(&isp->isp_mbxworkp);
4564f7c631bcSMatt Jacob 	}
4565f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
4566f7c631bcSMatt Jacob }
4567f7c631bcSMatt Jacob 
4568f7c631bcSMatt Jacob void
4569f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
4570f7c631bcSMatt Jacob {
4571f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
4572f7c631bcSMatt Jacob }
4573f7c631bcSMatt Jacob 
4574f7c631bcSMatt Jacob int
45752df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
45762df76c16SMatt Jacob {
45772df76c16SMatt Jacob 	int ret = 0;
45782df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
45792df76c16SMatt Jacob 		ret = -1;
45802df76c16SMatt Jacob 	} else {
45812df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
45822df76c16SMatt Jacob 	}
45832df76c16SMatt Jacob 	return (ret);
45842df76c16SMatt Jacob }
45852df76c16SMatt Jacob 
45862df76c16SMatt Jacob int
4587f7c631bcSMatt Jacob isp_mstohz(int ms)
4588f7c631bcSMatt Jacob {
4589a4f3a2beSMatt Jacob 	int hz;
4590f7c631bcSMatt Jacob 	struct timeval t;
4591f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
4592f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
4593a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
4594a4f3a2beSMatt Jacob 	if (hz < 0) {
4595a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
4596f7c631bcSMatt Jacob 	}
4597a4f3a2beSMatt Jacob 	if (hz == 0) {
4598a4f3a2beSMatt Jacob 		hz = 1;
4599a4f3a2beSMatt Jacob 	}
4600a4f3a2beSMatt Jacob 	return (hz);
4601f7c631bcSMatt Jacob }
46020a70657fSMatt Jacob 
46030a70657fSMatt Jacob void
46040a70657fSMatt Jacob isp_platform_intr(void *arg)
46050a70657fSMatt Jacob {
46060a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
46076ce548a1SAlexander Motin 	uint16_t isr, sema, info;
46080a70657fSMatt Jacob 
46090a70657fSMatt Jacob 	ISP_LOCK(isp);
46100a70657fSMatt Jacob 	isp->isp_intcnt++;
46116ce548a1SAlexander Motin 	if (ISP_READ_ISR(isp, &isr, &sema, &info))
46126ce548a1SAlexander Motin 		isp_intr(isp, isr, sema, info);
46136ce548a1SAlexander Motin 	else
46140a70657fSMatt Jacob 		isp->isp_intbogus++;
46150a70657fSMatt Jacob 	ISP_UNLOCK(isp);
46160a70657fSMatt Jacob }
46170a70657fSMatt Jacob 
46180a70657fSMatt Jacob void
46190a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
46200a70657fSMatt Jacob {
46210a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
46222df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
46230a70657fSMatt Jacob 	} else {
46242df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
46250a70657fSMatt Jacob 	}
46260a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
46270a70657fSMatt Jacob }
46282df76c16SMatt Jacob 
4629fb4a4356SKenneth D. Merry /*
4630fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
4631fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
4632fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
4633fb4a4356SKenneth D. Merry  */
4634fb4a4356SKenneth D. Merry void
463521daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4636fb4a4356SKenneth D. Merry {
463721daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
4638fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
463921daf914SAlexander Motin 	int i;
4640fb4a4356SKenneth D. Merry 
4641fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
464221daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
464321daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
4644fb4a4356SKenneth D. Merry 	else
464521daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
464621daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
4647fb4a4356SKenneth D. Merry 
4648fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
464921daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
465021daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
4651fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
4652fb4a4356SKenneth D. Merry 		}
4653fb4a4356SKenneth D. Merry 	}
4654fb4a4356SKenneth D. Merry }
4655fb4a4356SKenneth D. Merry 
4656387d8239SMatt Jacob int
4657387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4658387d8239SMatt Jacob {
46596af11b82SAlexander Motin 	lun_id_t lun;
46606af11b82SAlexander Motin 	uint32_t chan, tgt;
4661405b7a29SMatt Jacob 	struct isp_fc *fc;
4662405b7a29SMatt Jacob 	struct isp_nexus *nxp;
4663405b7a29SMatt Jacob 	int idx;
4664405b7a29SMatt Jacob 
4665a4ccb5d6SAlexander Motin 	if (IS_2100(isp))
4666405b7a29SMatt Jacob 		return (0);
4667405b7a29SMatt Jacob 
4668405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
4669405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
4670405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
4671405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
4672405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
4673405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
4674387d8239SMatt Jacob 
4675387d8239SMatt Jacob 	while (nxp) {
4676387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
4677387d8239SMatt Jacob 			break;
4678387d8239SMatt Jacob 		nxp = nxp->next;
4679387d8239SMatt Jacob 	}
4680387d8239SMatt Jacob 	if (nxp == NULL) {
4681387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
4682387d8239SMatt Jacob 		if (nxp == NULL) {
4683387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4684387d8239SMatt Jacob 			if (nxp == NULL) {
4685387d8239SMatt Jacob 				return (-1);
4686387d8239SMatt Jacob 			}
4687387d8239SMatt Jacob 		} else {
4688387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
4689387d8239SMatt Jacob 		}
4690387d8239SMatt Jacob 		nxp->tgt = tgt;
4691387d8239SMatt Jacob 		nxp->lun = lun;
4692387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
4693387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
4694387d8239SMatt Jacob 	}
4695387d8239SMatt Jacob 	if (nxp->crnseed == 0)
4696387d8239SMatt Jacob 		nxp->crnseed = 1;
4697387d8239SMatt Jacob 	PISP_PCMD(cmd)->crn = nxp->crnseed;
4698387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
4699387d8239SMatt Jacob 	return (0);
4700387d8239SMatt Jacob }
4701387d8239SMatt Jacob 
470294dff771SMatt Jacob /*
470394dff771SMatt Jacob  * We enter with the lock held
470494dff771SMatt Jacob  */
47052df76c16SMatt Jacob void
47062df76c16SMatt Jacob isp_timer(void *arg)
47072df76c16SMatt Jacob {
47082df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
47092df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
47102df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
47112df76c16SMatt Jacob #endif
471294dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
47132df76c16SMatt Jacob }
4714387d8239SMatt Jacob 
4715387d8239SMatt Jacob isp_ecmd_t *
4716387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
4717387d8239SMatt Jacob {
4718387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4719387d8239SMatt Jacob 	if (ecmd) {
4720387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
4721387d8239SMatt Jacob 	}
4722387d8239SMatt Jacob 	return (ecmd);
4723387d8239SMatt Jacob }
4724387d8239SMatt Jacob 
4725387d8239SMatt Jacob void
4726387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4727387d8239SMatt Jacob {
4728387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
4729387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
4730387d8239SMatt Jacob }
4731