xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 5cc3786c64c96a7322439fd9ae357dcb74efaafc)
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$");
326054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
335d571944SMatt Jacob #include <sys/unistd.h>
345d571944SMatt Jacob #include <sys/kthread.h>
355d571944SMatt Jacob #include <sys/conf.h>
364eb49427SMatt Jacob #include <sys/module.h>
375d571944SMatt Jacob #include <sys/ioccom.h>
385d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
3970273f90SMatt Jacob #include <sys/devicestat.h>
40f7c631bcSMatt Jacob #include <cam/cam_periph.h>
416c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
426054c3f6SMatt Jacob 
432df76c16SMatt Jacob #if	__FreeBSD_version < 800002
442df76c16SMatt Jacob #define	THREAD_CREATE	kthread_create
452df76c16SMatt Jacob #else
462df76c16SMatt Jacob #define	THREAD_CREATE	kproc_create
47805e1f82SMatt Jacob #endif
48805e1f82SMatt Jacob 
494eb49427SMatt Jacob MODULE_VERSION(isp, 1);
5056c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
5173030e03SMatt Jacob int isp_announced = 0;
522df76c16SMatt Jacob int isp_fabric_hysteresis = 3;
532df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
54f7c631bcSMatt Jacob int isp_change_is_bad = 0;	/* "changed" devices are bad */
552df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
56f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
572df76c16SMatt Jacob int isp_autoconfig = 1;		/* automatically attach/detach devices */
58f7c631bcSMatt Jacob static const char *roles[4] = {
59f7c631bcSMatt Jacob     "(none)", "Target", "Initiator", "Target/Initiator"
60f7c631bcSMatt Jacob };
612df76c16SMatt Jacob static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
622df76c16SMatt Jacob static const char rqo[] = "%s: Request Queue Overflow\n";
6373030e03SMatt Jacob 
642df76c16SMatt Jacob static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
655d571944SMatt Jacob static d_ioctl_t ispioctl;
66f6e75de2SMatt Jacob static void isp_intr_enable(void *);
671dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
680470d791SMatt Jacob static void isp_poll(struct cam_sim *);
69b85389e1SMatt Jacob static timeout_t isp_watchdog;
70f7c631bcSMatt Jacob static timeout_t isp_ldt;
715d571944SMatt Jacob static void isp_kthread(void *);
72d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
732df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
742df76c16SMatt Jacob static void isp_target_thread_pi(void *);
752df76c16SMatt Jacob static void isp_target_thread_fc(void *);
76784ed707SMatt Jacob #endif
772df76c16SMatt Jacob static void isp_timer(void *);
78cc8df88bSMatt Jacob 
795d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
80dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
817ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
827ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
835d571944SMatt Jacob };
845d571944SMatt Jacob 
852df76c16SMatt Jacob static int
862df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
87478f8a96SJustin T. Gibbs {
88478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
89ea6f23cdSMatt Jacob 	struct cam_sim *sim;
90ea6f23cdSMatt Jacob 	struct cam_path *path;
91478f8a96SJustin T. Gibbs 
92478f8a96SJustin T. Gibbs 	/*
932df76c16SMatt Jacob 	 * Construct our SIM entry.
94ea6f23cdSMatt Jacob 	 */
952df76c16SMatt 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);
96ea6f23cdSMatt Jacob 
972df76c16SMatt Jacob 	if (sim == NULL) {
982df76c16SMatt Jacob 		return (ENOMEM);
992df76c16SMatt Jacob 	}
1002df76c16SMatt Jacob 
1012df76c16SMatt Jacob 	ISP_LOCK(isp);
1022df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1032df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1042df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1052df76c16SMatt Jacob 		return (EIO);
1062df76c16SMatt Jacob 	}
1072df76c16SMatt Jacob 	ISP_UNLOCK(isp);
1082df76c16SMatt Jacob 
1092df76c16SMatt Jacob 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1102df76c16SMatt Jacob 		ISP_LOCK(isp);
1112df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1122df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1132df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1142df76c16SMatt Jacob 		return (ENXIO);
1152df76c16SMatt Jacob 	}
1162df76c16SMatt Jacob 
1172df76c16SMatt Jacob 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1182df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1192df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1202df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1212df76c16SMatt Jacob 	csa.callback_arg = sim;
1222df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
1232df76c16SMatt Jacob 
1242df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1252df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1262df76c16SMatt Jacob 		spi->sim = sim;
1272df76c16SMatt Jacob 		spi->path = path;
1282df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
1292df76c16SMatt Jacob 		ISP_SET_PC(isp, chan, proc_active, 1);
1302df76c16SMatt Jacob 		if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
1312df76c16SMatt Jacob 			ISP_SET_PC(isp, chan, proc_active, 0);
1322df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
1332df76c16SMatt Jacob 		}
1342df76c16SMatt Jacob #endif
1352df76c16SMatt Jacob 	} else {
1362df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
1372df76c16SMatt Jacob 
1382df76c16SMatt Jacob 		fc->sim = sim;
1392df76c16SMatt Jacob 		fc->path = path;
1402df76c16SMatt Jacob 		fc->isp = isp;
1412df76c16SMatt Jacob 
1422df76c16SMatt Jacob 		callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
1432df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
1442df76c16SMatt Jacob 
1452df76c16SMatt Jacob 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
1462df76c16SMatt Jacob 			xpt_free_path(fc->path);
1472df76c16SMatt Jacob 			ISP_LOCK(isp);
1482df76c16SMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
1492df76c16SMatt Jacob 			ISP_UNLOCK(isp);
1502df76c16SMatt Jacob 			cam_sim_free(fc->sim, FALSE);
1512df76c16SMatt Jacob 		}
1522df76c16SMatt Jacob 		/*
1532df76c16SMatt Jacob 		 * We start by being "loop down" if we have an initiator role
1542df76c16SMatt Jacob 		 */
1552df76c16SMatt Jacob 		ISP_LOCK(isp);
1562df76c16SMatt Jacob 		if ((FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) && fc->ldt_running == 0) {
1572df76c16SMatt Jacob 			isp_freeze_loopdown(isp, chan, "isp_attach");
1582df76c16SMatt Jacob 			fc->ldt_running = 1;
1592df76c16SMatt Jacob 			callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
1602df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
1612df76c16SMatt Jacob 		}
1622df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1632df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
1642df76c16SMatt Jacob 		ISP_SET_PC(isp, chan, proc_active, 1);
1652df76c16SMatt Jacob 		if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
1662df76c16SMatt Jacob 			ISP_SET_PC(isp, chan, proc_active, 0);
1672df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
1682df76c16SMatt Jacob 		}
1692df76c16SMatt Jacob #endif
1702df76c16SMatt Jacob 	}
1712df76c16SMatt Jacob 	return (0);
1722df76c16SMatt Jacob }
1732df76c16SMatt Jacob 
1742df76c16SMatt Jacob int
1752df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
1762df76c16SMatt Jacob {
1772df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
1782df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
1792df76c16SMatt Jacob 	int chan;
1802df76c16SMatt Jacob 
1812df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
1822df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_arg = isp;
1832df76c16SMatt Jacob 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
1842df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
1852df76c16SMatt Jacob 		return (-EIO);
1862df76c16SMatt Jacob 	}
1872df76c16SMatt Jacob 	isp->isp_osinfo.ehook_active = 1;
1882df76c16SMatt Jacob 
189ea6f23cdSMatt Jacob 
190ea6f23cdSMatt Jacob 	/*
191ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
192478f8a96SJustin T. Gibbs 	 */
1932df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
1942df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
1955d571944SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
1962df76c16SMatt Jacob 		return (EIO);
197478f8a96SJustin T. Gibbs 	}
198478f8a96SJustin T. Gibbs 
1992df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2002df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2012df76c16SMatt Jacob 			goto unwind;
202ea6f23cdSMatt Jacob 		}
203ea6f23cdSMatt Jacob 	}
204ea6f23cdSMatt Jacob 
2052df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
2062df76c16SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
2072df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
208ea6f23cdSMatt Jacob 
2092df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
2102df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2112df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
212ea6f23cdSMatt Jacob 	}
2132df76c16SMatt Jacob 	return (0);
2145d571944SMatt Jacob 
2152df76c16SMatt Jacob unwind:
2162df76c16SMatt Jacob 	while (--chan >= 0) {
2172df76c16SMatt Jacob 		struct cam_sim *sim;
2182df76c16SMatt Jacob 		struct cam_path *path;
2190a70657fSMatt Jacob 		if (IS_FC(isp)) {
2202df76c16SMatt Jacob 			sim = ISP_FC_PC(isp, chan)->sim;
2212df76c16SMatt Jacob 			path = ISP_FC_PC(isp, chan)->path;
2222df76c16SMatt Jacob 		} else {
2232df76c16SMatt Jacob 			sim = ISP_SPI_PC(isp, chan)->sim;
2242df76c16SMatt Jacob 			path = ISP_SPI_PC(isp, chan)->path;
2252df76c16SMatt Jacob 		}
2262df76c16SMatt Jacob 		xpt_free_path(path);
2270a70657fSMatt Jacob 		ISP_LOCK(isp);
2280a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
2292df76c16SMatt Jacob 		ISP_UNLOCK(isp);
2302df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
2312df76c16SMatt Jacob 	}
2322df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
2330a70657fSMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2342df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
2350a70657fSMatt Jacob 	}
2362df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2372df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
2382df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
2392df76c16SMatt Jacob 	}
2402df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
2412df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
2422df76c16SMatt Jacob 	return (-1);
2432df76c16SMatt Jacob }
2442df76c16SMatt Jacob 
2452df76c16SMatt Jacob void
2462df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
2472df76c16SMatt Jacob {
2482df76c16SMatt Jacob 	int chan;
2492df76c16SMatt Jacob 
2500a70657fSMatt Jacob 	ISP_LOCK(isp);
2512df76c16SMatt Jacob 	if (isp->isp_osinfo.timer_active) {
2522df76c16SMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
2532df76c16SMatt Jacob 		isp->isp_osinfo.timer_active = 0;
2540a70657fSMatt Jacob 	}
2552df76c16SMatt Jacob 	ISP_UNLOCK(isp);
2562df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
2572df76c16SMatt Jacob 		struct cam_sim *sim;
2582df76c16SMatt Jacob 		struct cam_path *path;
2592df76c16SMatt Jacob 		if (IS_FC(isp)) {
2602df76c16SMatt Jacob 			sim = ISP_FC_PC(isp, chan)->sim;
2612df76c16SMatt Jacob 			path = ISP_FC_PC(isp, chan)->path;
2622df76c16SMatt Jacob 		} else {
2632df76c16SMatt Jacob 			sim = ISP_SPI_PC(isp, chan)->sim;
2642df76c16SMatt Jacob 			path = ISP_SPI_PC(isp, chan)->path;
2652df76c16SMatt Jacob 		}
2662df76c16SMatt Jacob 		xpt_free_path(path);
2672df76c16SMatt Jacob 		ISP_LOCK(isp);
2682df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
2692df76c16SMatt Jacob 		ISP_UNLOCK(isp);
2702df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
2712df76c16SMatt Jacob 	}
2722df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2732df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
2742df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
2752df76c16SMatt Jacob 	}
2762df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
2772df76c16SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2782df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
2792df76c16SMatt Jacob 	}
2802df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2812df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
2822df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
2830a70657fSMatt Jacob 	}
2845d571944SMatt Jacob }
2855d571944SMatt Jacob 
286f7c631bcSMatt Jacob static void
2872df76c16SMatt Jacob isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
288fdeb9f2fSMatt Jacob {
2892df76c16SMatt Jacob 	if (IS_FC(isp)) {
2902df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
2912df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
2922df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
2932df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
2942df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
295fdeb9f2fSMatt Jacob 		} else {
2962df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
2972df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
2982df76c16SMatt Jacob 		}
299fdeb9f2fSMatt Jacob 	}
300fdeb9f2fSMatt Jacob }
301fdeb9f2fSMatt Jacob 
3029cd7268eSMatt Jacob 
3035d571944SMatt Jacob static int
3042df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
3055d571944SMatt Jacob {
3069cd7268eSMatt Jacob 	ispsoftc_t *isp;
3072df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
3085d571944SMatt Jacob 
3092df76c16SMatt Jacob 	isp = dev->si_drv1;
3105d571944SMatt Jacob 
311c1504bc0SMatt Jacob 	switch (c) {
3125d571944SMatt Jacob 	case ISP_SDBLEV:
3135d571944SMatt Jacob 	{
3145d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
3155d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
3165d571944SMatt Jacob 		*(int *)addr = olddblev;
3175d571944SMatt Jacob 		retval = 0;
3185d571944SMatt Jacob 		break;
3195d571944SMatt Jacob 	}
320746e9c85SMatt Jacob 	case ISP_GETROLE:
3212df76c16SMatt Jacob 		chan = *(int *)addr;
3222df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
3232df76c16SMatt Jacob 			retval = -ENXIO;
3242df76c16SMatt Jacob 			break;
3252df76c16SMatt Jacob 		}
3262df76c16SMatt Jacob 		if (IS_FC(isp)) {
3272df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
3282df76c16SMatt Jacob 		} else {
3292df76c16SMatt Jacob 			*(int *)addr = SDPARAM(isp, chan)->role;
3302df76c16SMatt Jacob 		}
331746e9c85SMatt Jacob 		retval = 0;
332746e9c85SMatt Jacob 		break;
333746e9c85SMatt Jacob 	case ISP_SETROLE:
334746e9c85SMatt Jacob 		nr = *(int *)addr;
3352df76c16SMatt Jacob 		chan = nr >> 8;
3362df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
3372df76c16SMatt Jacob 			retval = -ENXIO;
3382df76c16SMatt Jacob 			break;
3392df76c16SMatt Jacob 		}
3402df76c16SMatt Jacob 		nr &= 0xff;
341746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
342746e9c85SMatt Jacob 			retval = EINVAL;
343746e9c85SMatt Jacob 			break;
344746e9c85SMatt Jacob 		}
3452df76c16SMatt Jacob 		if (IS_FC(isp)) {
3462df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
3472df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
3482df76c16SMatt Jacob 			ISP_LOCK(isp);
3492df76c16SMatt Jacob 			retval = isp_fc_change_role(isp, chan, nr);
3502df76c16SMatt Jacob 			ISP_UNLOCK(isp);
3512df76c16SMatt Jacob #else
3522df76c16SMatt Jacob 			FCPARAM(isp, chan)->role = nr;
3532df76c16SMatt Jacob #endif
3542df76c16SMatt Jacob 		} else {
3552df76c16SMatt Jacob 			*(int *)addr = SDPARAM(isp, chan)->role;
3562df76c16SMatt Jacob 			SDPARAM(isp, chan)->role = nr;
3572df76c16SMatt Jacob 		}
3582df76c16SMatt Jacob 		retval = 0;
3592df76c16SMatt Jacob 		break;
3602df76c16SMatt Jacob 
3615d571944SMatt Jacob 	case ISP_RESETHBA:
362511ced9bSMatt Jacob 		ISP_LOCK(isp);
3632df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
3642df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, ISP_NOCHAN);
3652df76c16SMatt Jacob #endif
3662df76c16SMatt Jacob 		isp_reinit(isp, 0);
367511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
3685d571944SMatt Jacob 		retval = 0;
3695d571944SMatt Jacob 		break;
3702df76c16SMatt Jacob 
371f553351eSMatt Jacob 	case ISP_RESCAN:
3725d571944SMatt Jacob 		if (IS_FC(isp)) {
3732df76c16SMatt Jacob 			chan = *(int *)addr;
3742df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
3752df76c16SMatt Jacob 				retval = -ENXIO;
3762df76c16SMatt Jacob 				break;
3772df76c16SMatt Jacob 			}
378511ced9bSMatt Jacob 			ISP_LOCK(isp);
3792df76c16SMatt Jacob 			if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
3805d571944SMatt Jacob 				retval = EIO;
3815d571944SMatt Jacob 			} else {
3825d571944SMatt Jacob 				retval = 0;
3835d571944SMatt Jacob 			}
384511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
3855d571944SMatt Jacob 		}
3865d571944SMatt Jacob 		break;
3872df76c16SMatt Jacob 
3885d571944SMatt Jacob 	case ISP_FC_LIP:
3895d571944SMatt Jacob 		if (IS_FC(isp)) {
3902df76c16SMatt Jacob 			chan = *(int *)addr;
3912df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
3922df76c16SMatt Jacob 				retval = -ENXIO;
3932df76c16SMatt Jacob 				break;
3942df76c16SMatt Jacob 			}
395511ced9bSMatt Jacob 			ISP_LOCK(isp);
3962df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
3975d571944SMatt Jacob 				retval = EIO;
3985d571944SMatt Jacob 			} else {
3995d571944SMatt Jacob 				retval = 0;
4005d571944SMatt Jacob 			}
401511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4025d571944SMatt Jacob 		}
4035d571944SMatt Jacob 		break;
4045d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4055d571944SMatt Jacob 	{
4065d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
40710365e5aSMatt Jacob 		fcportdb_t *lp;
4085d571944SMatt Jacob 
40902e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
41002e2b2d9SMatt Jacob 			break;
41102e2b2d9SMatt Jacob 		}
4122e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
4135d571944SMatt Jacob 			retval = EINVAL;
4145d571944SMatt Jacob 			break;
4155d571944SMatt Jacob 		}
4162df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
4172df76c16SMatt Jacob 		if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
418c6435ff3SMatt Jacob 			ifc->role = lp->roles;
41910365e5aSMatt Jacob 			ifc->loopid = lp->handle;
4205d571944SMatt Jacob 			ifc->portid = lp->portid;
4215d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
4225d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
4235d571944SMatt Jacob 			retval = 0;
4245d571944SMatt Jacob 		} else {
4255d571944SMatt Jacob 			retval = ENODEV;
4265d571944SMatt Jacob 		}
4275d571944SMatt Jacob 		break;
4285d571944SMatt Jacob 	}
4292903b272SMatt Jacob 	case ISP_GET_STATS:
4302903b272SMatt Jacob 	{
4312903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
4322903b272SMatt Jacob 
4332df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
4342903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
4352903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
4362903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
437511ced9bSMatt Jacob 		ISP_LOCK(isp);
4382903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
4392903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
4402903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
4412903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
4422903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
4432903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
4442903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
4452903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
446511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4472903b272SMatt Jacob 		retval = 0;
4482903b272SMatt Jacob 		break;
4492903b272SMatt Jacob 	}
4502903b272SMatt Jacob 	case ISP_CLR_STATS:
451511ced9bSMatt Jacob 		ISP_LOCK(isp);
4522903b272SMatt Jacob 		isp->isp_intcnt = 0;
4532903b272SMatt Jacob 		isp->isp_intbogus = 0;
4542903b272SMatt Jacob 		isp->isp_intmboxc = 0;
4552903b272SMatt Jacob 		isp->isp_intoasync = 0;
4562903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
4572903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
4582903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
4592903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
460511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4612903b272SMatt Jacob 		retval = 0;
4622903b272SMatt Jacob 		break;
463570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
464570c7a3fSMatt Jacob 	{
465570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
4662df76c16SMatt Jacob 		int chan = hba->fc_channel;
46702e2b2d9SMatt Jacob 
4682df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4692df76c16SMatt Jacob 			retval = ENXIO;
4702df76c16SMatt Jacob 			break;
4712df76c16SMatt Jacob 		}
472a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
473a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
474a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
4752df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
47602e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
4772df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
4782df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
4792df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
4802df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
4812df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
4822df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
4832df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
4842df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
4852df76c16SMatt Jacob 		} else {
4862df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
4872df76c16SMatt Jacob 			hba->fc_speed = 0;
4882df76c16SMatt Jacob 			hba->fc_topology = 0;
4892df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
4902df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
4912df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
4922df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
49302e2b2d9SMatt Jacob 		}
494570c7a3fSMatt Jacob 		retval = 0;
495570c7a3fSMatt Jacob 		break;
496570c7a3fSMatt Jacob 	}
4978e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
4988e62a8acSMatt Jacob 	{
4998e62a8acSMatt Jacob 		int needmarker;
5008e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
5011dae40ebSMatt Jacob 		uint16_t loopid;
5028e62a8acSMatt Jacob 		mbreg_t mbs;
5038e62a8acSMatt Jacob 
5048e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5058e62a8acSMatt Jacob 			break;
5068e62a8acSMatt Jacob 		}
5078e62a8acSMatt Jacob 
5082df76c16SMatt Jacob 		chan = fct->chan;
5092df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5102df76c16SMatt Jacob 			retval = -ENXIO;
5112df76c16SMatt Jacob 			break;
5122df76c16SMatt Jacob 		}
5132df76c16SMatt Jacob 
5148e62a8acSMatt Jacob 		needmarker = retval = 0;
515e5265237SMatt Jacob 		loopid = fct->loopid;
5162df76c16SMatt Jacob 		ISP_LOCK(isp);
5172df76c16SMatt Jacob 		if (IS_24XX(isp)) {
5182df76c16SMatt Jacob 			uint8_t local[QENTRY_LEN];
5192df76c16SMatt Jacob 			isp24xx_tmf_t *tmf;
5202df76c16SMatt Jacob 			isp24xx_statusreq_t *sp;
5212df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
5222df76c16SMatt Jacob 			fcportdb_t *lp;
5232df76c16SMatt Jacob 			int i;
5242df76c16SMatt Jacob 
5252df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
5262df76c16SMatt Jacob 				lp = &fcp->portdb[i];
5272df76c16SMatt Jacob 				if (lp->handle == loopid) {
5282df76c16SMatt Jacob 					break;
5292df76c16SMatt Jacob 				}
5302df76c16SMatt Jacob 			}
5312df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
5322df76c16SMatt Jacob 				retval = ENXIO;
5332df76c16SMatt Jacob 				ISP_UNLOCK(isp);
5342df76c16SMatt Jacob 				break;
5352df76c16SMatt Jacob 			}
5362df76c16SMatt Jacob 			/* XXX VALIDATE LP XXX */
5372df76c16SMatt Jacob 			tmf = (isp24xx_tmf_t *) local;
5382df76c16SMatt Jacob 			ISP_MEMZERO(tmf, QENTRY_LEN);
5392df76c16SMatt Jacob 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
5402df76c16SMatt Jacob 			tmf->tmf_header.rqs_entry_count = 1;
5412df76c16SMatt Jacob 			tmf->tmf_nphdl = lp->handle;
5422df76c16SMatt Jacob 			tmf->tmf_delay = 2;
5432df76c16SMatt Jacob 			tmf->tmf_timeout = 2;
5442df76c16SMatt Jacob 			tmf->tmf_tidlo = lp->portid;
5452df76c16SMatt Jacob 			tmf->tmf_tidhi = lp->portid >> 16;
5462df76c16SMatt Jacob 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
5472df76c16SMatt Jacob 			tmf->tmf_lun[1] = fct->lun & 0xff;
5482df76c16SMatt Jacob 			if (fct->lun >= 256) {
5492df76c16SMatt Jacob 				tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
5502df76c16SMatt Jacob 			}
5512df76c16SMatt Jacob 			switch (fct->action) {
5522df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
5532df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
5542df76c16SMatt Jacob 				break;
5552df76c16SMatt Jacob 			case IPT_TARGET_RESET:
5562df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
5572df76c16SMatt Jacob 				needmarker = 1;
5582df76c16SMatt Jacob 				break;
5592df76c16SMatt Jacob 			case IPT_LUN_RESET:
5602df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
5612df76c16SMatt Jacob 				needmarker = 1;
5622df76c16SMatt Jacob 				break;
5632df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
5642df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
5652df76c16SMatt Jacob 				needmarker = 1;
5662df76c16SMatt Jacob 				break;
5672df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
5682df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
5692df76c16SMatt Jacob 				needmarker = 1;
5702df76c16SMatt Jacob 				break;
5712df76c16SMatt Jacob 			default:
5722df76c16SMatt Jacob 				retval = EINVAL;
5732df76c16SMatt Jacob 				break;
5742df76c16SMatt Jacob 			}
5752df76c16SMatt Jacob 			if (retval) {
5762df76c16SMatt Jacob 				ISP_UNLOCK(isp);
5772df76c16SMatt Jacob 				break;
5782df76c16SMatt Jacob 			}
5792df76c16SMatt Jacob 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
5802df76c16SMatt Jacob 			mbs.param[1] = QENTRY_LEN;
5812df76c16SMatt Jacob 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
5822df76c16SMatt Jacob 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
5832df76c16SMatt Jacob 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
5842df76c16SMatt Jacob 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
5852df76c16SMatt Jacob 
5862df76c16SMatt Jacob 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
5872df76c16SMatt Jacob 				ISP_UNLOCK(isp);
5882df76c16SMatt Jacob 				retval = ENOMEM;
5892df76c16SMatt Jacob 				break;
5902df76c16SMatt Jacob 			}
5912df76c16SMatt Jacob 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
5922df76c16SMatt Jacob 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN);
5932df76c16SMatt Jacob 			sp = (isp24xx_statusreq_t *) local;
5942df76c16SMatt Jacob 			sp->req_completion_status = 1;
5952df76c16SMatt Jacob 			retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
5962df76c16SMatt Jacob 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN);
5972df76c16SMatt Jacob 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
5982df76c16SMatt Jacob 			FC_SCRATCH_RELEASE(isp, chan);
5992df76c16SMatt Jacob 			if (retval || sp->req_completion_status != 0) {
6002df76c16SMatt Jacob 				FC_SCRATCH_RELEASE(isp, chan);
6012df76c16SMatt Jacob 				retval = EIO;
6022df76c16SMatt Jacob 			}
6032df76c16SMatt Jacob 			if (retval == 0) {
6042df76c16SMatt Jacob 				if (needmarker) {
6052df76c16SMatt Jacob 					fcp->sendmarker = 1;
6062df76c16SMatt Jacob 				}
6072df76c16SMatt Jacob 			}
6082df76c16SMatt Jacob 		} else {
6092df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
6102df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
611e5265237SMatt Jacob 				loopid <<= 8;
612e5265237SMatt Jacob 			}
6138e62a8acSMatt Jacob 			switch (fct->action) {
614f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
6158e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
616e5265237SMatt Jacob 				mbs.param[1] = loopid;
6178e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6188e62a8acSMatt Jacob 				break;
619f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
6208e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
621e5265237SMatt Jacob 				mbs.param[1] = loopid;
6228e62a8acSMatt Jacob 				needmarker = 1;
6238e62a8acSMatt Jacob 				break;
624f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
6258e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
626e5265237SMatt Jacob 				mbs.param[1] = loopid;
6278e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6288e62a8acSMatt Jacob 				needmarker = 1;
6298e62a8acSMatt Jacob 				break;
630f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
6318e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
632e5265237SMatt Jacob 				mbs.param[1] = loopid;
6338e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6348e62a8acSMatt Jacob 				needmarker = 1;
6358e62a8acSMatt Jacob 				break;
636f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
6378e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
638e5265237SMatt Jacob 				mbs.param[1] = loopid;
6398e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
6408e62a8acSMatt Jacob 				needmarker = 1;
6418e62a8acSMatt Jacob 				break;
6428e62a8acSMatt Jacob 			default:
6438e62a8acSMatt Jacob 				retval = EINVAL;
6448e62a8acSMatt Jacob 				break;
6458e62a8acSMatt Jacob 			}
6468e62a8acSMatt Jacob 			if (retval == 0) {
6478e62a8acSMatt Jacob 				if (needmarker) {
6482df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
6498e62a8acSMatt Jacob 				}
6508e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6512df76c16SMatt Jacob 				if (retval) {
6528e62a8acSMatt Jacob 					retval = EIO;
6538e62a8acSMatt Jacob 				}
6542df76c16SMatt Jacob 			}
6552df76c16SMatt Jacob 		}
6562df76c16SMatt Jacob 		ISP_UNLOCK(isp);
6578e62a8acSMatt Jacob 		break;
6588e62a8acSMatt Jacob 	}
6595d571944SMatt Jacob 	default:
6605d571944SMatt Jacob 		break;
6615d571944SMatt Jacob 	}
6625d571944SMatt Jacob 	return (retval);
6630470d791SMatt Jacob }
664478f8a96SJustin T. Gibbs 
665f6e75de2SMatt Jacob static void
666f6e75de2SMatt Jacob isp_intr_enable(void *arg)
667f6e75de2SMatt Jacob {
6682df76c16SMatt Jacob 	int chan;
6699cd7268eSMatt Jacob 	ispsoftc_t *isp = arg;
6700a70657fSMatt Jacob 	ISP_LOCK(isp);
6712df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
6722df76c16SMatt Jacob 		if (IS_FC(isp)) {
6732df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
67410365e5aSMatt Jacob 				ISP_ENABLE_INTS(isp);
6752df76c16SMatt Jacob 				break;
6762df76c16SMatt Jacob 			}
6772df76c16SMatt Jacob 		} else {
6782df76c16SMatt Jacob 			if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
6792df76c16SMatt Jacob 				ISP_ENABLE_INTS(isp);
6802df76c16SMatt Jacob 				break;
6812df76c16SMatt Jacob 			}
6822df76c16SMatt Jacob 		}
683d6e5500fSMatt Jacob 	}
6840a70657fSMatt Jacob 	ISP_UNLOCK(isp);
685f6e75de2SMatt Jacob 	/* Release our hook so that the boot can continue. */
686f6e75de2SMatt Jacob 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
687f6e75de2SMatt Jacob }
688d81ba9d5SMatt Jacob 
689d81ba9d5SMatt Jacob /*
6902df76c16SMatt Jacob  * Local Inlines
6912df76c16SMatt Jacob  */
6922df76c16SMatt Jacob 
6932df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
6942df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
6952df76c16SMatt Jacob 
6962df76c16SMatt Jacob static ISP_INLINE int
6972df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
6982df76c16SMatt Jacob {
6992df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
7002df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
7012df76c16SMatt Jacob 		return (-1);
7022df76c16SMatt Jacob 	}
7032df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
7042df76c16SMatt Jacob 	return (0);
7052df76c16SMatt Jacob }
7062df76c16SMatt Jacob 
7072df76c16SMatt Jacob static ISP_INLINE void
7082df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
7092df76c16SMatt Jacob {
7102df76c16SMatt Jacob 	((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
7112df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
7122df76c16SMatt Jacob 	ISP_PCMD(ccb) = NULL;
7132df76c16SMatt Jacob }
7142df76c16SMatt Jacob /*
715d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
716d81ba9d5SMatt Jacob  */
717d81ba9d5SMatt Jacob 
718d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
7192df76c16SMatt Jacob static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
7202df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
7212df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
7222df76c16SMatt Jacob static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
7232df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
7242df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
7252df76c16SMatt Jacob static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
7262df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
7272df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
7282df76c16SMatt Jacob static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
7292df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
7309cd7268eSMatt Jacob static void destroy_lun_state(ispsoftc_t *, tstate_t *);
7312df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
7322df76c16SMatt Jacob static void isp_enable_deferred_luns(ispsoftc_t *, int);
7332df76c16SMatt Jacob static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
7342df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
7352df76c16SMatt Jacob static int isp_enable_target_mode(ispsoftc_t *, int);
7369cd7268eSMatt Jacob static void isp_ledone(ispsoftc_t *, lun_entry_t *);
737f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
738a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
739a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
7409cd7268eSMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
7412df76c16SMatt Jacob static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
7422df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
7432df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
7442df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
7452df76c16SMatt Jacob static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
7462df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
7472df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
7482df76c16SMatt Jacob static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
7492df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
7502df76c16SMatt Jacob static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
7512df76c16SMatt Jacob static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
752d81ba9d5SMatt Jacob 
7532df76c16SMatt Jacob static ISP_INLINE int
7549cd7268eSMatt Jacob is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
755d81ba9d5SMatt Jacob {
756d81ba9d5SMatt Jacob 	tstate_t *tptr;
7572df76c16SMatt Jacob 	struct tslist *lhp;
7582df76c16SMatt Jacob 
7592df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
7602df76c16SMatt Jacob 	SLIST_FOREACH(tptr, lhp, next) {
7612df76c16SMatt Jacob 		if (xpt_path_lun_id(tptr->owner) == lun) {
762d81ba9d5SMatt Jacob 			return (1);
763d81ba9d5SMatt Jacob 		}
7642df76c16SMatt Jacob 	}
765d81ba9d5SMatt Jacob 	return (0);
766d81ba9d5SMatt Jacob }
767d81ba9d5SMatt Jacob 
7682df76c16SMatt Jacob static void
7692df76c16SMatt Jacob dump_tstates(ispsoftc_t *isp, int bus)
770d81ba9d5SMatt Jacob {
7712df76c16SMatt Jacob 	int i, j;
7722df76c16SMatt Jacob 	struct tslist *lhp;
7732df76c16SMatt Jacob 	tstate_t *tptr = NULL;
7742df76c16SMatt Jacob 
7752df76c16SMatt Jacob 	if (bus >= isp->isp_nchan) {
7762df76c16SMatt Jacob 		return;
777a1bc34c6SMatt Jacob 	}
7782df76c16SMatt Jacob 	for (i = 0; i < LUN_HASH_SIZE; i++) {
7792df76c16SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
7802df76c16SMatt Jacob 		j = 0;
7812df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
7822df76c16SMatt Jacob 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
7832df76c16SMatt Jacob 			j++;
784d81ba9d5SMatt Jacob 		}
785d81ba9d5SMatt Jacob 	}
786d81ba9d5SMatt Jacob }
787d81ba9d5SMatt Jacob 
7882df76c16SMatt Jacob static ISP_INLINE tstate_t *
7899cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
790d81ba9d5SMatt Jacob {
79164edff94SMatt Jacob 	tstate_t *tptr = NULL;
7922df76c16SMatt Jacob 	struct tslist *lhp;
7932df76c16SMatt Jacob 	int i;
794d81ba9d5SMatt Jacob 
7952df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
7962df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
7972df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
7982df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
7992df76c16SMatt Jacob 				if (xpt_path_lun_id(tptr->owner) == lun) {
800d81ba9d5SMatt Jacob 					tptr->hold++;
801d81ba9d5SMatt Jacob 					return (tptr);
802d81ba9d5SMatt Jacob 				}
80364edff94SMatt Jacob 			}
804126ec864SMatt Jacob 		}
8052df76c16SMatt Jacob 	}
8062df76c16SMatt Jacob 	return (NULL);
8072df76c16SMatt Jacob }
808d81ba9d5SMatt Jacob 
8092df76c16SMatt Jacob static ISP_INLINE tstate_t *
8102df76c16SMatt Jacob get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
8112df76c16SMatt Jacob {
8122df76c16SMatt Jacob 	tstate_t *tptr = NULL;
8132df76c16SMatt Jacob 	atio_private_data_t *atp;
8142df76c16SMatt Jacob 	struct tslist *lhp;
8152df76c16SMatt Jacob 	int i;
8162df76c16SMatt Jacob 
8172df76c16SMatt Jacob 	if (bus < isp->isp_nchan && tagval != 0) {
8182df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8192df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8202df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
8212df76c16SMatt Jacob 				atp = isp_get_atpd(isp, tptr, tagval);
8222df76c16SMatt Jacob 				if (atp && atp->tag == tagval) {
823d81ba9d5SMatt Jacob 					tptr->hold++;
824d81ba9d5SMatt Jacob 					return (tptr);
825d81ba9d5SMatt Jacob 				}
8262df76c16SMatt Jacob 			}
8272df76c16SMatt Jacob 		}
8282df76c16SMatt Jacob 	}
8292df76c16SMatt Jacob 	return (NULL);
830d81ba9d5SMatt Jacob }
831d81ba9d5SMatt Jacob 
8322df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
8332df76c16SMatt Jacob get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
8342df76c16SMatt Jacob {
8352df76c16SMatt Jacob 	inot_private_data_t *ntp;
8362df76c16SMatt Jacob 	tstate_t *tptr;
8372df76c16SMatt Jacob 	struct tslist *lhp;
8382df76c16SMatt Jacob 	int bus, i;
8392df76c16SMatt Jacob 
8402df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
8412df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8422df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8432df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
8442df76c16SMatt Jacob 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
8452df76c16SMatt Jacob 				if (ntp) {
8462df76c16SMatt Jacob 					*rslt = tptr;
8472df76c16SMatt Jacob 					tptr->hold++;
8482df76c16SMatt Jacob 					return (ntp);
8492df76c16SMatt Jacob 				}
8502df76c16SMatt Jacob 			}
8512df76c16SMatt Jacob 		}
8522df76c16SMatt Jacob 	}
8532df76c16SMatt Jacob 	return (NULL);
8542df76c16SMatt Jacob }
8552df76c16SMatt Jacob static ISP_INLINE void
8569cd7268eSMatt Jacob rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
857d81ba9d5SMatt Jacob {
8582df76c16SMatt Jacob 	KASSERT((tptr->hold), ("tptr not held"));
859d81ba9d5SMatt Jacob 	tptr->hold--;
860d81ba9d5SMatt Jacob }
861d81ba9d5SMatt Jacob 
8622df76c16SMatt Jacob static void
8632df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
8642df76c16SMatt Jacob {
8652df76c16SMatt Jacob 	inot_private_data_t *ntp;
8662df76c16SMatt Jacob 	tstate_t *tptr;
8672df76c16SMatt Jacob 	struct tslist *lhp;
8682df76c16SMatt Jacob 	int bus, i;
8692df76c16SMatt Jacob 
8702df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
8712df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
8722df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
8732df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
8742df76c16SMatt Jacob 				inot_private_data_t *restart_queue = tptr->restart_queue;
8752df76c16SMatt Jacob 				tptr->restart_queue = NULL;
8762df76c16SMatt Jacob 				while (restart_queue) {
8772df76c16SMatt Jacob 					ntp = restart_queue;
8782df76c16SMatt Jacob 					restart_queue = ntp->rd.nt.nt_hba;
8792df76c16SMatt Jacob 					if (IS_24XX(isp)) {
8802df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
8812df76c16SMatt Jacob 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
8822df76c16SMatt Jacob 					} else {
8832df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
8842df76c16SMatt Jacob 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
8852df76c16SMatt Jacob 					}
8862df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
8872df76c16SMatt Jacob 					if (tptr->restart_queue && restart_queue != NULL) {
8882df76c16SMatt Jacob 						ntp = tptr->restart_queue;
8892df76c16SMatt Jacob 						tptr->restart_queue = restart_queue;
8902df76c16SMatt Jacob 						while (restart_queue->rd.nt.nt_hba) {
8912df76c16SMatt Jacob 							restart_queue = restart_queue->rd.nt.nt_hba;
8922df76c16SMatt Jacob 						}
8932df76c16SMatt Jacob 						restart_queue->rd.nt.nt_hba = ntp;
8942df76c16SMatt Jacob 						break;
8952df76c16SMatt Jacob 					}
8962df76c16SMatt Jacob 				}
8972df76c16SMatt Jacob 			}
8982df76c16SMatt Jacob 		}
8992df76c16SMatt Jacob 	}
9002df76c16SMatt Jacob }
9012df76c16SMatt Jacob 
9022df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *
9032df76c16SMatt Jacob isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
90453036e92SMatt Jacob {
90553036e92SMatt Jacob 	atio_private_data_t *atp;
9062df76c16SMatt Jacob 
9072df76c16SMatt Jacob 	if (tag == 0) {
9082df76c16SMatt Jacob 		atp = tptr->atfree;
9092df76c16SMatt Jacob 		if (atp) {
9102df76c16SMatt Jacob 			tptr->atfree = atp->next;
9112df76c16SMatt Jacob 		}
91253036e92SMatt Jacob 		return (atp);
91353036e92SMatt Jacob 	}
9142df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
9152df76c16SMatt Jacob 		if (atp->tag == tag) {
9162df76c16SMatt Jacob 			return (atp);
9172df76c16SMatt Jacob 		}
9182df76c16SMatt Jacob 	}
91953036e92SMatt Jacob 	return (NULL);
92053036e92SMatt Jacob }
92153036e92SMatt Jacob 
9222df76c16SMatt Jacob static ISP_INLINE void
9232df76c16SMatt Jacob isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
9242df76c16SMatt Jacob {
9252df76c16SMatt Jacob 	atp->tag = 0;
9262df76c16SMatt Jacob 	atp->dead = 0;
9272df76c16SMatt Jacob 	atp->next = tptr->atfree;
9282df76c16SMatt Jacob 	tptr->atfree = atp;
9292df76c16SMatt Jacob }
9302df76c16SMatt Jacob 
9312df76c16SMatt Jacob static void
9322df76c16SMatt Jacob isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
9332df76c16SMatt Jacob {
9342df76c16SMatt Jacob 	atio_private_data_t *atp;
9352df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
9362df76c16SMatt Jacob 
9372df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
9382df76c16SMatt Jacob 		if (atp->tag == 0) {
9392df76c16SMatt Jacob 			continue;
9402df76c16SMatt Jacob 		}
9412df76c16SMatt Jacob 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u last_xfr %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
9422df76c16SMatt Jacob                     atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->last_xframt, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
9432df76c16SMatt Jacob 	}
9442df76c16SMatt Jacob }
9452df76c16SMatt Jacob 
9462df76c16SMatt Jacob 
9472df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
9482df76c16SMatt Jacob isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
9492df76c16SMatt Jacob {
9502df76c16SMatt Jacob 	inot_private_data_t *ntp;
9512df76c16SMatt Jacob 	ntp = tptr->ntfree;
9522df76c16SMatt Jacob 	if (ntp) {
9532df76c16SMatt Jacob 		tptr->ntfree = ntp->next;
9542df76c16SMatt Jacob 	}
9552df76c16SMatt Jacob 	return (ntp);
9562df76c16SMatt Jacob }
9572df76c16SMatt Jacob 
9582df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
9592df76c16SMatt Jacob isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
9602df76c16SMatt Jacob {
9612df76c16SMatt Jacob 	inot_private_data_t *ntp;
9622df76c16SMatt Jacob 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
9632df76c16SMatt Jacob 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
9642df76c16SMatt Jacob 			return (ntp);
9652df76c16SMatt Jacob 		}
9662df76c16SMatt Jacob 	}
9672df76c16SMatt Jacob 	return (NULL);
9682df76c16SMatt Jacob }
9692df76c16SMatt Jacob 
9702df76c16SMatt Jacob static ISP_INLINE void
9712df76c16SMatt Jacob isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
9722df76c16SMatt Jacob {
9732df76c16SMatt Jacob 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
9742df76c16SMatt Jacob 	ntp->next = tptr->ntfree;
9752df76c16SMatt Jacob 	tptr->ntfree = ntp;
9762df76c16SMatt Jacob }
9772df76c16SMatt Jacob 
978d81ba9d5SMatt Jacob static cam_status
9792df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
980d81ba9d5SMatt Jacob {
981d81ba9d5SMatt Jacob 	cam_status status;
982d81ba9d5SMatt Jacob 	lun_id_t lun;
9832df76c16SMatt Jacob 	struct tslist *lhp;
9842df76c16SMatt Jacob 	tstate_t *tptr;
9852df76c16SMatt Jacob 	int i;
986d81ba9d5SMatt Jacob 
987d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
9882df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
9892e4637cdSMatt Jacob 		if (lun >= ISP_MAX_LUNS(isp)) {
990d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
991d81ba9d5SMatt Jacob 		}
9922df76c16SMatt Jacob 	}
993a1bc34c6SMatt Jacob 	if (is_lun_enabled(isp, bus, lun)) {
994d81ba9d5SMatt Jacob 		return (CAM_LUN_ALRDY_ENA);
995d81ba9d5SMatt Jacob 	}
9962df76c16SMatt Jacob 	tptr = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
9972df76c16SMatt Jacob 	if (tptr == NULL) {
998d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
999d81ba9d5SMatt Jacob 	}
10002df76c16SMatt Jacob 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1001d81ba9d5SMatt Jacob 	if (status != CAM_REQ_CMP) {
10022df76c16SMatt Jacob 		free(tptr, M_DEVBUF);
1003d81ba9d5SMatt Jacob 		return (status);
1004d81ba9d5SMatt Jacob 	}
10052df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
10062df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
10072df76c16SMatt Jacob 	for (i = 0; i < ATPDPSIZE-1; i++) {
10082df76c16SMatt Jacob 		tptr->atpool[i].next = &tptr->atpool[i+1];
10092df76c16SMatt Jacob 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
1010d81ba9d5SMatt Jacob 	}
10112df76c16SMatt Jacob 	tptr->atfree = tptr->atpool;
10122df76c16SMatt Jacob 	tptr->ntfree = tptr->ntpool;
10132df76c16SMatt Jacob 	tptr->hold = 1;
10142df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
10152df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
10162df76c16SMatt Jacob 	*rslt = tptr;
10172df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1018d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1019d81ba9d5SMatt Jacob }
1020d81ba9d5SMatt Jacob 
10212df76c16SMatt Jacob static ISP_INLINE void
10229cd7268eSMatt Jacob destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1023d81ba9d5SMatt Jacob {
10242df76c16SMatt Jacob 	struct tslist *lhp;
10252df76c16SMatt Jacob 	KASSERT((tptr->hold == 0), ("tptr still held"));
10262df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, xpt_path_path_id(tptr->owner), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
10272df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
10282df76c16SMatt Jacob 	xpt_free_path(tptr->owner);
1029d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1030d81ba9d5SMatt Jacob }
1031d81ba9d5SMatt Jacob 
10325d571944SMatt Jacob /*
10332df76c16SMatt Jacob  * Enable a lun.
10345d571944SMatt Jacob  */
10352df76c16SMatt Jacob static void
10362df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1037d81ba9d5SMatt Jacob {
10381fd47020SMatt Jacob 	tstate_t *tptr = NULL;
10392df76c16SMatt Jacob 	int bus, tm_enabled, target_role;
10402df76c16SMatt Jacob 	target_id_t target;
1041d81ba9d5SMatt Jacob 	lun_id_t lun;
1042d81ba9d5SMatt Jacob 
1043746e9c85SMatt Jacob 	/*
10442df76c16SMatt Jacob 	 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1045746e9c85SMatt Jacob 	 */
10462df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
10472df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
10482df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
10492df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1050d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_TID_INVALID;
10512df76c16SMatt Jacob 		xpt_done(ccb);
10522df76c16SMatt Jacob 		return;
1053d81ba9d5SMatt Jacob 	}
10542df76c16SMatt Jacob 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
10552df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10562df76c16SMatt Jacob 		xpt_done(ccb);
10572df76c16SMatt Jacob 		return;
10582df76c16SMatt Jacob 	}
10592df76c16SMatt Jacob 
10602df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
10612df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
10622df76c16SMatt Jacob 		xpt_done(ccb);
10632df76c16SMatt Jacob 		return;
10642df76c16SMatt Jacob 	}
10652df76c16SMatt Jacob 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
10662df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
10672df76c16SMatt Jacob 	}
10682df76c16SMatt Jacob 
10692df76c16SMatt Jacob 	/*
10702df76c16SMatt Jacob 	 * Wait until we're not busy with the lun enables subsystem
10712df76c16SMatt Jacob 	 */
10722df76c16SMatt Jacob 	while (isp->isp_osinfo.tmbusy) {
10732df76c16SMatt Jacob 		isp->isp_osinfo.tmwanted = 1;
10742df76c16SMatt Jacob 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_enable_lun", 0);
10752df76c16SMatt Jacob 	}
10762df76c16SMatt Jacob 	isp->isp_osinfo.tmbusy = 1;
10772df76c16SMatt Jacob 
107864edff94SMatt Jacob 	/*
107964edff94SMatt Jacob 	 * This is as a good a place as any to check f/w capabilities.
108064edff94SMatt Jacob 	 */
10812df76c16SMatt Jacob 
10822df76c16SMatt Jacob 	if (IS_FC(isp)) {
10832df76c16SMatt Jacob 		if (ISP_CAP_TMODE(isp) == 0) {
10842df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
108564edff94SMatt Jacob 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
10862df76c16SMatt Jacob 			goto done;
108764edff94SMatt Jacob 		}
108864edff94SMatt Jacob 		/*
10892df76c16SMatt Jacob 		 * We *could* handle non-SCCLUN f/w, but we'd have to
10902df76c16SMatt Jacob 		 * dork with our already fragile enable/disable code.
109164edff94SMatt Jacob 		 */
10922df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp) == 0) {
10932df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1094746e9c85SMatt Jacob 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
10952df76c16SMatt Jacob 			goto done;
1096d81ba9d5SMatt Jacob 		}
1097d81ba9d5SMatt Jacob 
10982df76c16SMatt Jacob 		target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
10992df76c16SMatt Jacob 
110064edff94SMatt Jacob 	} else {
11012df76c16SMatt Jacob 		target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1102126ec864SMatt Jacob 	}
1103126ec864SMatt Jacob 
1104126ec864SMatt Jacob 	/*
11052df76c16SMatt Jacob 	 * Create the state pointer.
11062df76c16SMatt Jacob 	 * It should not already exist.
1107126ec864SMatt Jacob 	 */
1108a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
11092df76c16SMatt Jacob 	if (tptr) {
11102df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
11112df76c16SMatt Jacob 		goto done;
1112d81ba9d5SMatt Jacob 	}
11132df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
11142df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
11152df76c16SMatt Jacob 		goto done;
1116d81ba9d5SMatt Jacob 	}
1117d81ba9d5SMatt Jacob 
11185d571944SMatt Jacob 	/*
11192df76c16SMatt Jacob 	 * We have a tricky maneuver to perform here.
11202df76c16SMatt Jacob 	 *
11212df76c16SMatt Jacob 	 * If target mode isn't already enabled here,
11222df76c16SMatt Jacob 	 * *and* our current role includes target mode,
11232df76c16SMatt Jacob 	 * we enable target mode here.
11242df76c16SMatt Jacob 	 *
11255d571944SMatt Jacob 	 */
11262df76c16SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
11272df76c16SMatt Jacob 	if (tm_enabled == 0 && target_role != 0) {
11282df76c16SMatt Jacob 		if (isp_enable_target_mode(isp, bus)) {
112967ff51f1SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
11302df76c16SMatt Jacob 			destroy_lun_state(isp, tptr);
11312df76c16SMatt Jacob 			tptr = NULL;
11322df76c16SMatt Jacob 			goto done;
11335d571944SMatt Jacob 		}
11342df76c16SMatt Jacob 		tm_enabled = 1;
11352df76c16SMatt Jacob 	}
11362df76c16SMatt Jacob 
11372df76c16SMatt Jacob 	/*
11382df76c16SMatt Jacob 	 * Now check to see whether this bus is in target mode already.
11392df76c16SMatt Jacob 	 *
11402df76c16SMatt Jacob 	 * If not, a later role change into target mode will finish the job.
11412df76c16SMatt Jacob 	 */
11422df76c16SMatt Jacob 	if (tm_enabled == 0) {
11432df76c16SMatt Jacob 		ISP_SET_PC(isp, bus, tm_enable_defer, 1);
11442df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
11452df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
11462df76c16SMatt Jacob 		goto done;
11472df76c16SMatt Jacob 	}
11482df76c16SMatt Jacob 
11492df76c16SMatt Jacob 	/*
11502df76c16SMatt Jacob 	 * Enable the lun.
11512df76c16SMatt Jacob 	 */
11522df76c16SMatt Jacob 	ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
11532df76c16SMatt Jacob 
11542df76c16SMatt Jacob done:
11552df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
11562df76c16SMatt Jacob 		destroy_lun_state(isp, tptr);
11572df76c16SMatt Jacob 		tptr = NULL;
11582df76c16SMatt Jacob 	}
11592df76c16SMatt Jacob 	if (tptr) {
11602df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
11612df76c16SMatt Jacob 	}
11622df76c16SMatt Jacob 	isp->isp_osinfo.tmbusy = 0;
11632df76c16SMatt Jacob 	if (isp->isp_osinfo.tmwanted) {
11642df76c16SMatt Jacob 		isp->isp_osinfo.tmwanted = 0;
11652df76c16SMatt Jacob 		wakeup(isp);
11662df76c16SMatt Jacob 	}
11672df76c16SMatt Jacob 	xpt_done(ccb);
11682df76c16SMatt Jacob }
11692df76c16SMatt Jacob 
11702df76c16SMatt Jacob static void
11712df76c16SMatt Jacob isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
11722df76c16SMatt Jacob {
11732df76c16SMatt Jacob 	/*
11742df76c16SMatt Jacob  	 * XXX: not entirely implemented yet
11752df76c16SMatt Jacob 	 */
11762df76c16SMatt Jacob 	(void) isp_enable_deferred(isp, bus, 0);
11772df76c16SMatt Jacob }
11782df76c16SMatt Jacob 
11792df76c16SMatt Jacob static uint32_t
11802df76c16SMatt Jacob isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
11812df76c16SMatt Jacob {
11822df76c16SMatt Jacob 	cam_status status;
11832df76c16SMatt Jacob 
11842df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
11852df76c16SMatt Jacob 	if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
11862df76c16SMatt Jacob 		status = CAM_REQ_CMP;
11872df76c16SMatt Jacob 	} else {
11882df76c16SMatt Jacob 		int cmd_cnt, not_cnt;
11892df76c16SMatt Jacob 
11902df76c16SMatt Jacob 		if (IS_23XX(isp)) {
11912df76c16SMatt Jacob 			cmd_cnt = DFLT_CMND_CNT;
11922df76c16SMatt Jacob 			not_cnt = DFLT_INOT_CNT;
11932df76c16SMatt Jacob 		} else {
11942df76c16SMatt Jacob 			cmd_cnt = 64;
11952df76c16SMatt Jacob 			not_cnt = 8;
11962df76c16SMatt Jacob 		}
11972df76c16SMatt Jacob 		status = CAM_REQ_INPROG;
11982df76c16SMatt Jacob 		isp->isp_osinfo.rptr = &status;
11992df76c16SMatt Jacob 		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
12002df76c16SMatt Jacob 			status = CAM_RESRC_UNAVAIL;
12012df76c16SMatt Jacob 		} else {
12022df76c16SMatt Jacob 			mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
12032df76c16SMatt Jacob 		}
12042df76c16SMatt Jacob 		isp->isp_osinfo.rptr = NULL;
12052df76c16SMatt Jacob 	}
12062df76c16SMatt Jacob 
12072df76c16SMatt Jacob 	if (status == CAM_REQ_CMP) {
12082df76c16SMatt Jacob 		ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
12092df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
12102df76c16SMatt Jacob 	}
12112df76c16SMatt Jacob 	return (status);
12122df76c16SMatt Jacob }
12132df76c16SMatt Jacob 
12142df76c16SMatt Jacob static void
12152df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
12162df76c16SMatt Jacob {
12172df76c16SMatt Jacob 	tstate_t *tptr = NULL;
12182df76c16SMatt Jacob 	int bus;
12192df76c16SMatt Jacob 	cam_status status;
12202df76c16SMatt Jacob 	target_id_t target;
12212df76c16SMatt Jacob 	lun_id_t lun;
12222df76c16SMatt Jacob 
12232df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12242df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12252df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
12262df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && target != 0) {
12272df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_TID_INVALID;
12282df76c16SMatt Jacob 		xpt_done(ccb);
12292df76c16SMatt Jacob 		return;
12302df76c16SMatt Jacob 	}
12312df76c16SMatt Jacob 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
12322df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12332df76c16SMatt Jacob 		xpt_done(ccb);
12342df76c16SMatt Jacob 		return;
12352df76c16SMatt Jacob 	}
12362df76c16SMatt Jacob 
12372df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
12382df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12392df76c16SMatt Jacob 		xpt_done(ccb);
12402df76c16SMatt Jacob 		return;
12412df76c16SMatt Jacob 	}
12422df76c16SMatt Jacob 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
12432df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
12442df76c16SMatt Jacob 	}
12452df76c16SMatt Jacob 
12462df76c16SMatt Jacob 	/*
12472df76c16SMatt Jacob 	 * See if we're busy disabling a lun now.
12482df76c16SMatt Jacob 	 */
12492df76c16SMatt Jacob 	while (isp->isp_osinfo.tmbusy) {
12502df76c16SMatt Jacob 		isp->isp_osinfo.tmwanted = 1;
12512df76c16SMatt Jacob 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
12522df76c16SMatt Jacob 	}
12532df76c16SMatt Jacob 	isp->isp_osinfo.tmbusy = 1;
12542df76c16SMatt Jacob 
12552df76c16SMatt Jacob 	/*
12562df76c16SMatt Jacob 	 * Find the state pointer.
12572df76c16SMatt Jacob 	 */
12582df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
12592df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_PATH_INVALID;
12602df76c16SMatt Jacob 		goto done;
12612df76c16SMatt Jacob 	}
12622df76c16SMatt Jacob 
12632df76c16SMatt Jacob 	/*
12642df76c16SMatt Jacob 	 * If we're a 24XX card, we're done.
12652df76c16SMatt Jacob 	 */
12662df76c16SMatt Jacob 	if (IS_24XX(isp)) {
12672df76c16SMatt Jacob 		status = CAM_REQ_CMP;
12682df76c16SMatt Jacob 		goto done;
12692df76c16SMatt Jacob 	}
12702df76c16SMatt Jacob 
12712df76c16SMatt Jacob 	/*
12722df76c16SMatt Jacob 	 * For SCC FW, we only deal with lun zero.
12732df76c16SMatt Jacob 	 */
12742df76c16SMatt Jacob 	if (IS_FC(isp)) {
12752df76c16SMatt Jacob 		lun = 0;
12762df76c16SMatt Jacob 	}
12772df76c16SMatt Jacob 
12782df76c16SMatt Jacob 	isp->isp_osinfo.rptr = &status;
12792df76c16SMatt Jacob 	status = CAM_REQ_INPROG;
12802df76c16SMatt Jacob 	if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
12812df76c16SMatt Jacob 		status = CAM_RESRC_UNAVAIL;
12822df76c16SMatt Jacob 	} else {
12832df76c16SMatt Jacob 		mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
12842df76c16SMatt Jacob 	}
12852df76c16SMatt Jacob done:
12862df76c16SMatt Jacob 	if (status == CAM_REQ_CMP) {
12872df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
12882df76c16SMatt Jacob 	}
12892df76c16SMatt Jacob 	if (tptr) {
12902df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
12912df76c16SMatt Jacob 	}
12922df76c16SMatt Jacob 	isp->isp_osinfo.rptr = NULL;
12932df76c16SMatt Jacob 	isp->isp_osinfo.tmbusy = 0;
12942df76c16SMatt Jacob 	if (isp->isp_osinfo.tmwanted) {
12952df76c16SMatt Jacob 		isp->isp_osinfo.tmwanted = 0;
12962df76c16SMatt Jacob 		wakeup(isp);
12972df76c16SMatt Jacob 	}
12982df76c16SMatt Jacob 	xpt_done(ccb);
12992df76c16SMatt Jacob }
13002df76c16SMatt Jacob 
13012df76c16SMatt Jacob static int
13022df76c16SMatt Jacob isp_enable_target_mode(ispsoftc_t *isp, int bus)
13032df76c16SMatt Jacob {
13042df76c16SMatt Jacob 	int ct;
13052df76c16SMatt Jacob 
13062df76c16SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, ct);
13072df76c16SMatt Jacob 	if (ct != 0) {
13082df76c16SMatt Jacob 		return (0);
13092df76c16SMatt Jacob 	}
13102df76c16SMatt Jacob 
13112df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
13122df76c16SMatt Jacob 		mbreg_t mbs;
13132df76c16SMatt Jacob 
13142df76c16SMatt Jacob 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
13152df76c16SMatt Jacob 		mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
13162df76c16SMatt Jacob 		mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
13172df76c16SMatt Jacob 		mbs.param[2] = bus << 7;
13182df76c16SMatt Jacob 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
13192df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
13202df76c16SMatt Jacob 			return (EIO);
13212df76c16SMatt Jacob 		}
13222df76c16SMatt Jacob 		SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
13232df76c16SMatt Jacob 	}
13242df76c16SMatt Jacob 	ISP_SET_PC(isp, bus, tm_enabled, 1);
13252df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
13262df76c16SMatt Jacob 	return (0);
13272df76c16SMatt Jacob }
13282df76c16SMatt Jacob 
13292df76c16SMatt Jacob #ifdef	NEEDED
13302df76c16SMatt Jacob static int
13312df76c16SMatt Jacob isp_disable_target_mode(ispsoftc_t *isp, int bus)
13322df76c16SMatt Jacob {
13332df76c16SMatt Jacob 	int ct;
13342df76c16SMatt Jacob 
13352df76c16SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, ct);
13362df76c16SMatt Jacob 	if (ct == 0) {
13372df76c16SMatt Jacob 		return (0);
13382df76c16SMatt Jacob 	}
13392df76c16SMatt Jacob 
13402df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
13412df76c16SMatt Jacob 		mbreg_t mbs;
13422df76c16SMatt Jacob 
13432df76c16SMatt Jacob 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
13442df76c16SMatt Jacob 		mbs.param[2] = bus << 7;
13452df76c16SMatt Jacob 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
13462df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
13472df76c16SMatt Jacob 			return (EIO);
13482df76c16SMatt Jacob 		}
13492df76c16SMatt Jacob 		SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
13502df76c16SMatt Jacob 	}
13512df76c16SMatt Jacob 	ISP_SET_PC(isp, bus, tm_enabled, 0);
13522df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
13532df76c16SMatt Jacob 	return (0);
13542df76c16SMatt Jacob }
13552df76c16SMatt Jacob #endif
13565d571944SMatt Jacob 
135767ff51f1SMatt Jacob static void
13589cd7268eSMatt Jacob isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
135967ff51f1SMatt Jacob {
13602df76c16SMatt Jacob 	uint32_t *rptr;
1361d81ba9d5SMatt Jacob 
13622df76c16SMatt Jacob 	rptr = isp->isp_osinfo.rptr;
136367ff51f1SMatt Jacob 	if (lep->le_status != LUN_OK) {
13642df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
13652df76c16SMatt Jacob 		if (rptr) {
13662df76c16SMatt Jacob 			*rptr = CAM_REQ_CMP_ERR;
13672df76c16SMatt Jacob 			wakeup_one(rptr);
13682df76c16SMatt Jacob 		}
136967ff51f1SMatt Jacob 	} else {
13702df76c16SMatt Jacob 		if (rptr) {
13712df76c16SMatt Jacob 			*rptr = CAM_REQ_CMP;
13722df76c16SMatt Jacob 			wakeup_one(rptr);
137367ff51f1SMatt Jacob 		}
1374126ec864SMatt Jacob 	}
1375d81ba9d5SMatt Jacob }
1376d81ba9d5SMatt Jacob 
13779cd7268eSMatt Jacob static void
13789cd7268eSMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1379d81ba9d5SMatt Jacob {
1380d81ba9d5SMatt Jacob 	void *qe;
13812df76c16SMatt Jacob 	tstate_t *tptr;
13822df76c16SMatt Jacob 	atio_private_data_t *atp;
138300a8e174SMatt Jacob 	struct ccb_scsiio *cso = &ccb->csio;
13842df76c16SMatt Jacob 	uint32_t dmaresult, handle;
13851dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1386d81ba9d5SMatt Jacob 
13872df76c16SMatt Jacob 	/*
13882df76c16SMatt Jacob 	 * Do some sanity checks.
13892df76c16SMatt Jacob 	 */
13902df76c16SMatt Jacob 	if (cso->dxfer_len == 0) {
13912df76c16SMatt Jacob 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
13922df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
13932df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
13942df76c16SMatt Jacob 			xpt_done(ccb);
13952df76c16SMatt Jacob 			return;
13962df76c16SMatt Jacob 		}
13972df76c16SMatt Jacob 	}
1398f48ce188SMatt Jacob 
13992df76c16SMatt Jacob 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
14002df76c16SMatt Jacob 	if (tptr == NULL) {
14012df76c16SMatt Jacob 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
14022df76c16SMatt Jacob 		if (tptr == NULL) {
14032df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
14042df76c16SMatt Jacob 			dump_tstates(isp, XS_CHANNEL(ccb));
14052df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
14062df76c16SMatt Jacob 			xpt_done(ccb);
14072df76c16SMatt Jacob 			return;
14082df76c16SMatt Jacob 		}
14092df76c16SMatt Jacob 	}
14102df76c16SMatt Jacob 
14112df76c16SMatt Jacob 	atp = isp_get_atpd(isp, tptr, cso->tag_id);
14122df76c16SMatt Jacob 	if (atp == NULL) {
14132df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
14142df76c16SMatt Jacob 		isp_dump_atpd(isp, tptr);
14152df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
14162df76c16SMatt Jacob 		xpt_done(ccb);
14172df76c16SMatt Jacob 		return;
14182df76c16SMatt Jacob 	}
14192df76c16SMatt Jacob 	if (atp->dead) {
14202df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
14212df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
14222df76c16SMatt Jacob 		xpt_done(ccb);
14232df76c16SMatt Jacob 		return;
14242df76c16SMatt Jacob 	}
14252df76c16SMatt Jacob 
14262df76c16SMatt Jacob 	/*
14272df76c16SMatt Jacob 	 * Check to make sure we're still in target mode.
14282df76c16SMatt Jacob 	 */
14292df76c16SMatt Jacob 	if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
14302df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
14312df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
14322df76c16SMatt Jacob 		xpt_done(ccb);
14332df76c16SMatt Jacob 		return;
14342df76c16SMatt Jacob 	}
14352df76c16SMatt Jacob 
14362df76c16SMatt Jacob 	/*
14372df76c16SMatt Jacob 	 * Get some resources
14382df76c16SMatt Jacob 	 */
14392df76c16SMatt Jacob 	if (isp_get_pcmd(isp, ccb)) {
14402df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
14412df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
14422df76c16SMatt Jacob 		cam_freeze_devq(ccb->ccb_h.path);
14432df76c16SMatt Jacob 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
14442df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
14452df76c16SMatt Jacob 		xpt_done(ccb);
14462df76c16SMatt Jacob 		return;
14472df76c16SMatt Jacob 	}
14482df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
14492df76c16SMatt Jacob 	if (qe == NULL) {
14502df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, rqo, __func__);
14512df76c16SMatt Jacob 		cam_freeze_devq(ccb->ccb_h.path);
14522df76c16SMatt Jacob 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
14532df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
14549cd7268eSMatt Jacob 		goto out;
1455d81ba9d5SMatt Jacob 	}
145629f76675SMatt Jacob 	memset(local, 0, QENTRY_LEN);
1457d81ba9d5SMatt Jacob 
1458d81ba9d5SMatt Jacob 	/*
1459d81ba9d5SMatt Jacob 	 * We're either moving data or completing a command here.
1460d81ba9d5SMatt Jacob 	 */
14612df76c16SMatt Jacob 	if (IS_24XX(isp)) {
14622df76c16SMatt Jacob 		ct7_entry_t *cto = (ct7_entry_t *) local;
1463d81ba9d5SMatt Jacob 
14642df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
14652df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
14662df76c16SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
14672df76c16SMatt Jacob 		cto->ct_nphdl = atp->nphdl;
14682df76c16SMatt Jacob 		cto->ct_rxid = atp->tag;
14692df76c16SMatt Jacob 		cto->ct_iid_lo = atp->portid;
14702df76c16SMatt Jacob 		cto->ct_iid_hi = atp->portid >> 16;
14712df76c16SMatt Jacob 		cto->ct_oxid = atp->oxid;
14722df76c16SMatt Jacob 		cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
14732df76c16SMatt Jacob 		cto->ct_scsi_status = cso->scsi_status;
14742df76c16SMatt Jacob 		cto->ct_timeout = 120;
14752df76c16SMatt Jacob 		cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
14762df76c16SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
14772df76c16SMatt Jacob 			cto->ct_flags |= CT7_SENDSTATUS;
14782df76c16SMatt Jacob 		}
14792df76c16SMatt Jacob 		if (cso->dxfer_len == 0) {
14802df76c16SMatt Jacob 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
14812df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
14822df76c16SMatt Jacob 				int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
14832df76c16SMatt Jacob 				cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
14842df76c16SMatt Jacob 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
14852df76c16SMatt Jacob 				cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
14862df76c16SMatt Jacob 			}
14872df76c16SMatt Jacob 		} else {
14882df76c16SMatt Jacob 			cto->ct_flags |= CT7_FLAG_MODE0;
14892df76c16SMatt Jacob 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
14902df76c16SMatt Jacob 				cto->ct_flags |= CT7_DATA_IN;
14912df76c16SMatt Jacob 			} else {
14922df76c16SMatt Jacob 				cto->ct_flags |= CT7_DATA_OUT;
14932df76c16SMatt Jacob 			}
14942df76c16SMatt Jacob 			cto->rsp.m0.reloff = atp->bytes_xfered;
14952df76c16SMatt Jacob 			/*
14962df76c16SMatt Jacob 			 * Don't overrun the limits placed on us
14972df76c16SMatt Jacob 			 */
14982df76c16SMatt Jacob 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
14992df76c16SMatt Jacob 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
15002df76c16SMatt Jacob 			}
15012df76c16SMatt Jacob 			atp->last_xframt = cso->dxfer_len;
15022df76c16SMatt Jacob 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
15032df76c16SMatt Jacob 		}
15042df76c16SMatt Jacob 		if (cto->ct_flags & CT7_SENDSTATUS) {
15052df76c16SMatt Jacob 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
15062df76c16SMatt Jacob 			cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
15072df76c16SMatt Jacob 			if (cto->ct_resid < 0) {
15082df76c16SMatt Jacob 				cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
15092df76c16SMatt Jacob 			} else if (cto->ct_resid > 0) {
15102df76c16SMatt Jacob 				cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
15112df76c16SMatt Jacob 			}
15122df76c16SMatt Jacob 			atp->state = ATPD_STATE_LAST_CTIO;
15132df76c16SMatt Jacob 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
15142df76c16SMatt Jacob 			    atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
15152df76c16SMatt Jacob 		} else {
15162df76c16SMatt Jacob 			cto->ct_resid = 0;
15172df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
15182df76c16SMatt Jacob 			    cso->dxfer_len, atp->bytes_xfered);
15192df76c16SMatt Jacob 			atp->state = ATPD_STATE_CTIO;
15202df76c16SMatt Jacob 		}
15212df76c16SMatt Jacob 	} else if (IS_FC(isp)) {
15224fd13c1bSMatt Jacob 		ct2_entry_t *cto = (ct2_entry_t *) local;
152300a8e174SMatt Jacob 
1524d81ba9d5SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1525d81ba9d5SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
15262df76c16SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
15272df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp) == 0) {
152829f76675SMatt Jacob 			((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
152929f76675SMatt Jacob 		} else {
153000a8e174SMatt Jacob 			cto->ct_iid = cso->init_id;
15312df76c16SMatt Jacob 			if (ISP_CAP_SCCFW(isp) == 0) {
1532d81ba9d5SMatt Jacob 				cto->ct_lun = ccb->ccb_h.target_lun;
15332ad50ca5SMatt Jacob 			}
153429f76675SMatt Jacob 		}
153553036e92SMatt Jacob 
1536f48ce188SMatt Jacob 
153700a8e174SMatt Jacob 		cto->ct_rxid = cso->tag_id;
153800a8e174SMatt Jacob 		if (cso->dxfer_len == 0) {
15392df76c16SMatt Jacob 			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1540f48ce188SMatt Jacob 			cto->rsp.m1.ct_scsi_status = cso->scsi_status;
15412df76c16SMatt Jacob 			cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1542570c7a3fSMatt Jacob 			if (cto->ct_resid < 0) {
15432df76c16SMatt Jacob 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1544570c7a3fSMatt Jacob 			} else if (cto->ct_resid > 0) {
15452df76c16SMatt Jacob 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1546f48ce188SMatt Jacob 			}
154700a8e174SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
154800a8e174SMatt Jacob 				int m = min(cso->sense_len, MAXRESPLEN);
15492df76c16SMatt Jacob 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
155000a8e174SMatt Jacob 				cto->rsp.m1.ct_senselen = m;
155100a8e174SMatt Jacob 				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
15522df76c16SMatt Jacob 			} else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
15532df76c16SMatt Jacob 				/*
15542df76c16SMatt Jacob 				 * XXX: DEBUG
15552df76c16SMatt Jacob 				 */
15562df76c16SMatt Jacob 				xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
155700a8e174SMatt Jacob 			}
155800a8e174SMatt Jacob 		} else {
155900a8e174SMatt Jacob 			cto->ct_flags |= CT2_FLAG_MODE0;
156000a8e174SMatt Jacob 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
156100a8e174SMatt Jacob 				cto->ct_flags |= CT2_DATA_IN;
156200a8e174SMatt Jacob 			} else {
156300a8e174SMatt Jacob 				cto->ct_flags |= CT2_DATA_OUT;
1564d81ba9d5SMatt Jacob 			}
1565570c7a3fSMatt Jacob 			cto->ct_reloff = atp->bytes_xfered;
15662df76c16SMatt Jacob 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
15672df76c16SMatt Jacob 			/*
15682df76c16SMatt Jacob 			 * Don't overrun the limits placed on us
15692df76c16SMatt Jacob 			 */
15702df76c16SMatt Jacob 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
15712df76c16SMatt Jacob 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
15722df76c16SMatt Jacob 			}
1573d81ba9d5SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1574d81ba9d5SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS;
157500a8e174SMatt Jacob 				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
15762df76c16SMatt Jacob 				cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1577570c7a3fSMatt Jacob 				if (cto->ct_resid < 0) {
15782df76c16SMatt Jacob 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1579570c7a3fSMatt Jacob 				} else if (cto->ct_resid > 0) {
15802df76c16SMatt Jacob 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1581570c7a3fSMatt Jacob 				}
158253036e92SMatt Jacob 			} else {
158353036e92SMatt Jacob 				atp->last_xframt = cso->dxfer_len;
1584d81ba9d5SMatt Jacob 			}
1585f48ce188SMatt Jacob 			/*
1586f48ce188SMatt Jacob 			 * If we're sending data and status back together,
1587f48ce188SMatt Jacob 			 * we can't also send back sense data as well.
1588f48ce188SMatt Jacob 			 */
158900a8e174SMatt Jacob 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
159000a8e174SMatt Jacob 		}
159153036e92SMatt Jacob 
1592290dc24bSMatt Jacob 		if (cto->ct_flags & CT2_SENDSTATUS) {
15932df76c16SMatt Jacob 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1594a1bc34c6SMatt Jacob 			cto->ct_flags |= CT2_CCINCR;
1595570c7a3fSMatt Jacob 			atp->state = ATPD_STATE_LAST_CTIO;
15962df76c16SMatt Jacob 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
15972df76c16SMatt Jacob 			    atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
15989cd7268eSMatt Jacob 		} else {
15992df76c16SMatt Jacob 			cto->ct_resid = 0;
1600570c7a3fSMatt Jacob 			atp->state = ATPD_STATE_CTIO;
16012df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
16022df76c16SMatt Jacob 			    cso->dxfer_len, atp->bytes_xfered);
16039cd7268eSMatt Jacob 		}
1604a1bc34c6SMatt Jacob 		cto->ct_timeout = 10;
1605d81ba9d5SMatt Jacob 	} else {
16064fd13c1bSMatt Jacob 		ct_entry_t *cto = (ct_entry_t *) local;
160700a8e174SMatt Jacob 
1608d81ba9d5SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1609d81ba9d5SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
16102df76c16SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
161100a8e174SMatt Jacob 		cto->ct_iid = cso->init_id;
1612a1bc34c6SMatt Jacob 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1613d81ba9d5SMatt Jacob 		cto->ct_tgt = ccb->ccb_h.target_id;
1614d81ba9d5SMatt Jacob 		cto->ct_lun = ccb->ccb_h.target_lun;
16152df76c16SMatt Jacob 		cto->ct_fwhandle = cso->tag_id >> 16;
1616a1bc34c6SMatt Jacob 		if (AT_HAS_TAG(cso->tag_id)) {
16172df76c16SMatt Jacob 			cto->ct_tag_val = cso->tag_id;
1618f48ce188SMatt Jacob 			cto->ct_flags |= CT_TQAE;
1619f48ce188SMatt Jacob 		}
1620f48ce188SMatt Jacob 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1621f48ce188SMatt Jacob 			cto->ct_flags |= CT_NODISC;
1622f48ce188SMatt Jacob 		}
1623f48ce188SMatt Jacob 		if (cso->dxfer_len == 0) {
1624d81ba9d5SMatt Jacob 			cto->ct_flags |= CT_NO_DATA;
162500a8e174SMatt Jacob 		} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
162600a8e174SMatt Jacob 			cto->ct_flags |= CT_DATA_IN;
162700a8e174SMatt Jacob 		} else {
162800a8e174SMatt Jacob 			cto->ct_flags |= CT_DATA_OUT;
1629d81ba9d5SMatt Jacob 		}
1630f48ce188SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
163153036e92SMatt Jacob 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
163200a8e174SMatt Jacob 			cto->ct_scsi_status = cso->scsi_status;
163300a8e174SMatt Jacob 			cto->ct_resid = cso->resid;
16342df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
16352df76c16SMatt Jacob 			    cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
163692a1e549SMatt Jacob 		}
163764edff94SMatt Jacob 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1638a1bc34c6SMatt Jacob 		cto->ct_timeout = 10;
1639d81ba9d5SMatt Jacob 	}
1640d81ba9d5SMatt Jacob 
164110365e5aSMatt Jacob 	if (isp_save_xs_tgt(isp, ccb, &handle)) {
16422df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
16432df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
16449cd7268eSMatt Jacob 		goto out;
1645d81ba9d5SMatt Jacob 	}
1646d81ba9d5SMatt Jacob 
1647d81ba9d5SMatt Jacob 
1648d81ba9d5SMatt Jacob 	/*
1649d81ba9d5SMatt Jacob 	 * Call the dma setup routines for this entry (and any subsequent
1650d81ba9d5SMatt Jacob 	 * CTIOs) if there's data to move, and then tell the f/w it's got
1651b09b0095SMatt Jacob 	 * new things to play with. As with isp_start's usage of DMA setup,
1652d81ba9d5SMatt Jacob 	 * any swizzling is done in the machine dependent layer. Because
1653d81ba9d5SMatt Jacob 	 * of this, we put the request onto the queue area first in native
1654d81ba9d5SMatt Jacob 	 * format.
1655d81ba9d5SMatt Jacob 	 */
1656d81ba9d5SMatt Jacob 
16572df76c16SMatt Jacob 	if (IS_24XX(isp)) {
16582df76c16SMatt Jacob 		ct7_entry_t *cto = (ct7_entry_t *) local;
16592df76c16SMatt Jacob 		cto->ct_syshandle = handle;
16602df76c16SMatt Jacob 	} else if (IS_FC(isp)) {
166110365e5aSMatt Jacob 		ct2_entry_t *cto = (ct2_entry_t *) local;
166210365e5aSMatt Jacob 		cto->ct_syshandle = handle;
166310365e5aSMatt Jacob 	} else {
166410365e5aSMatt Jacob 		ct_entry_t *cto = (ct_entry_t *) local;
166510365e5aSMatt Jacob 		cto->ct_syshandle = handle;
166610365e5aSMatt Jacob 	}
1667a1bc34c6SMatt Jacob 
16682df76c16SMatt Jacob 	dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
16692df76c16SMatt Jacob 	if (dmaresult == CMD_QUEUED) {
16702df76c16SMatt Jacob 		isp->isp_nactive++;
16719cd7268eSMatt Jacob 		ccb->ccb_h.status |= CAM_SIM_QUEUED;
16722df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
16739cd7268eSMatt Jacob 		return;
16742df76c16SMatt Jacob 	}
16752df76c16SMatt Jacob 	if (dmaresult == CMD_EAGAIN) {
16762df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
16772df76c16SMatt Jacob 	} else {
16782df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1679d81ba9d5SMatt Jacob 	}
168010365e5aSMatt Jacob 	isp_destroy_tgt_handle(isp, handle);
16819cd7268eSMatt Jacob out:
16822df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
16832df76c16SMatt Jacob 	isp_free_pcmd(isp, ccb);
16849cd7268eSMatt Jacob 	xpt_done(ccb);
1685d81ba9d5SMatt Jacob }
1686d81ba9d5SMatt Jacob 
1687a1bc34c6SMatt Jacob static void
1688a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
1689f48ce188SMatt Jacob {
16902df76c16SMatt Jacob 	union ccb *ccb = arg;
16912df76c16SMatt Jacob 	ispsoftc_t *isp = XS_ISP(ccb);
16922df76c16SMatt Jacob 	ISP_LOCK(isp);
16932df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
16942df76c16SMatt Jacob 	ISP_UNLOCK(isp);
1695a1bc34c6SMatt Jacob }
1696a1bc34c6SMatt Jacob 
1697a1bc34c6SMatt Jacob static void
1698a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
1699a1bc34c6SMatt Jacob {
17009cd7268eSMatt Jacob 	ispsoftc_t *isp;
1701a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
1702a1bc34c6SMatt Jacob 	void *qe;
1703a1bc34c6SMatt Jacob 
1704a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
1705f48ce188SMatt Jacob 
17062df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
17072df76c16SMatt Jacob 	if (qe == NULL) {
17082df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, rqo, __func__);
1709a1bc34c6SMatt Jacob 		(void) timeout(isp_refire_putback_atio, ccb, 10);
1710a1bc34c6SMatt Jacob 		return;
1711f48ce188SMatt Jacob 	}
171229f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
1713a1bc34c6SMatt Jacob 	cso = &ccb->csio;
1714f48ce188SMatt Jacob 	if (IS_FC(isp)) {
17154fd13c1bSMatt Jacob 		at2_entry_t local, *at = &local;
17162df76c16SMatt Jacob 		ISP_MEMZERO(at, sizeof (at2_entry_t));
1717f48ce188SMatt Jacob 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1718f48ce188SMatt Jacob 		at->at_header.rqs_entry_count = 1;
17192df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
1720a1bc34c6SMatt Jacob 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1721f48ce188SMatt Jacob 		} else {
1722a1bc34c6SMatt Jacob 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1723f48ce188SMatt Jacob 		}
1724f48ce188SMatt Jacob 		at->at_status = CT_OK;
1725a1bc34c6SMatt Jacob 		at->at_rxid = cso->tag_id;
1726570c7a3fSMatt Jacob 		at->at_iid = cso->ccb_h.target_id;
17274fd13c1bSMatt Jacob 		isp_put_atio2(isp, at, qe);
1728f48ce188SMatt Jacob 	} else {
17294fd13c1bSMatt Jacob 		at_entry_t local, *at = &local;
17302df76c16SMatt Jacob 		ISP_MEMZERO(at, sizeof (at_entry_t));
1731f48ce188SMatt Jacob 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1732f48ce188SMatt Jacob 		at->at_header.rqs_entry_count = 1;
1733a1bc34c6SMatt Jacob 		at->at_iid = cso->init_id;
1734a1bc34c6SMatt Jacob 		at->at_iid |= XS_CHANNEL(ccb) << 7;
1735a1bc34c6SMatt Jacob 		at->at_tgt = cso->ccb_h.target_id;
1736a1bc34c6SMatt Jacob 		at->at_lun = cso->ccb_h.target_lun;
1737f48ce188SMatt Jacob 		at->at_status = CT_OK;
1738a1bc34c6SMatt Jacob 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
1739a1bc34c6SMatt Jacob 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
17404fd13c1bSMatt Jacob 		isp_put_atio(isp, at, qe);
1741f48ce188SMatt Jacob 	}
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 {
1750a1bc34c6SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1751a1bc34c6SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
1752f48ce188SMatt Jacob 	}
1753a1bc34c6SMatt Jacob 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
17542df76c16SMatt Jacob 	isp_free_pcmd(XS_ISP(ccb), ccb);
1755a1bc34c6SMatt Jacob 	xpt_done(ccb);
1756f48ce188SMatt Jacob }
1757f48ce188SMatt Jacob 
1758d81ba9d5SMatt Jacob /*
1759d81ba9d5SMatt Jacob  * Handle ATIO stuff that the generic code can't.
1760d81ba9d5SMatt Jacob  * This means handling CDBs.
1761d81ba9d5SMatt Jacob  */
1762d81ba9d5SMatt Jacob 
17632df76c16SMatt Jacob static void
17649cd7268eSMatt Jacob isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1765d81ba9d5SMatt Jacob {
1766d81ba9d5SMatt Jacob 	tstate_t *tptr;
17672df76c16SMatt Jacob 	int status, bus;
1768d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
17692df76c16SMatt Jacob 	atio_private_data_t *atp;
1770d81ba9d5SMatt Jacob 
1771d81ba9d5SMatt Jacob 	/*
1772d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1773d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1774d81ba9d5SMatt Jacob 	 *
1775d81ba9d5SMatt Jacob 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1776d81ba9d5SMatt Jacob 	 *
1777d81ba9d5SMatt Jacob 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
17785d571944SMatt Jacob 	 * we're still connected on the SCSI bus.
1779d81ba9d5SMatt Jacob 	 */
1780d81ba9d5SMatt Jacob 	status = aep->at_status;
1781d81ba9d5SMatt Jacob 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1782d81ba9d5SMatt Jacob 		/*
1783d81ba9d5SMatt Jacob 		 * Bus Phase Sequence error. We should have sense data
1784d81ba9d5SMatt Jacob 		 * suggested by the f/w. I'm not sure quite yet what
1785d81ba9d5SMatt Jacob 		 * to do about this for CAM.
1786d81ba9d5SMatt Jacob 		 */
17873c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1788d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
17892df76c16SMatt Jacob 		return;
1790d81ba9d5SMatt Jacob 	}
1791d81ba9d5SMatt Jacob 	if ((status & ~QLTM_SVALID) != AT_CDB) {
17922df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1793d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
17942df76c16SMatt Jacob 		return;
1795d81ba9d5SMatt Jacob 	}
1796d81ba9d5SMatt Jacob 
17975d571944SMatt Jacob 	bus = GET_BUS_VAL(aep->at_iid);
1798a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, aep->at_lun);
1799d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1800a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1801746e9c85SMatt Jacob 		if (tptr == NULL) {
1802d81ba9d5SMatt Jacob 			/*
1803d81ba9d5SMatt Jacob 			 * Because we can't autofeed sense data back with
1804d81ba9d5SMatt Jacob 			 * a command for parallel SCSI, we can't give back
1805d81ba9d5SMatt Jacob 			 * a CHECK CONDITION. We'll give back a BUSY status
1806d81ba9d5SMatt Jacob 			 * instead. This works out okay because the only
1807d81ba9d5SMatt Jacob 			 * time we should, in fact, get this, is in the
1808d81ba9d5SMatt Jacob 			 * case that somebody configured us without the
1809d81ba9d5SMatt Jacob 			 * blackhole driver, so they get what they deserve.
1810d81ba9d5SMatt Jacob 			 */
1811d81ba9d5SMatt Jacob 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
18122df76c16SMatt Jacob 			return;
1813d81ba9d5SMatt Jacob 		}
18148c4e89e2SMatt Jacob 	}
1815d81ba9d5SMatt Jacob 
18162df76c16SMatt Jacob 	atp = isp_get_atpd(isp, tptr, 0);
1817d81ba9d5SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
18182df76c16SMatt Jacob 	if (atiop == NULL || atp == NULL) {
1819d81ba9d5SMatt Jacob 		/*
1820d81ba9d5SMatt Jacob 		 * Because we can't autofeed sense data back with
1821d81ba9d5SMatt Jacob 		 * a command for parallel SCSI, we can't give back
1822d81ba9d5SMatt Jacob 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1823d81ba9d5SMatt Jacob 		 * instead. This works out okay because the only time we
1824d81ba9d5SMatt Jacob 		 * should, in fact, get this, is in the case that we've
1825d81ba9d5SMatt Jacob 		 * run out of ATIOS.
1826d81ba9d5SMatt Jacob 		 */
18272df76c16SMatt Jacob 		xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
18282df76c16SMatt Jacob 		    ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1829d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
18302df76c16SMatt Jacob 		if (atp) {
18312df76c16SMatt Jacob 			isp_put_atpd(isp, tptr, atp);
1832d81ba9d5SMatt Jacob 		}
18332df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
18342df76c16SMatt Jacob 		return;
18352df76c16SMatt Jacob 	}
18362df76c16SMatt Jacob 	atp->tag = aep->at_tag_val;
18372df76c16SMatt Jacob 	if (atp->tag == 0) {
18382df76c16SMatt Jacob 		atp->tag = ~0;
18392df76c16SMatt Jacob 	}
18402df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1841d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1842746e9c85SMatt Jacob 	tptr->atio_count--;
18432df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1844d81ba9d5SMatt Jacob 	atiop->ccb_h.target_id = aep->at_tgt;
1845d81ba9d5SMatt Jacob 	atiop->ccb_h.target_lun = aep->at_lun;
1846d81ba9d5SMatt Jacob 	if (aep->at_flags & AT_NODISC) {
1847f48ce188SMatt Jacob 		atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1848f48ce188SMatt Jacob 	} else {
1849f48ce188SMatt Jacob 		atiop->ccb_h.flags = 0;
1850d81ba9d5SMatt Jacob 	}
1851d81ba9d5SMatt Jacob 
1852f48ce188SMatt Jacob 	if (status & QLTM_SVALID) {
1853f48ce188SMatt Jacob 		size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1854f48ce188SMatt Jacob 		atiop->sense_len = amt;
18552df76c16SMatt Jacob 		ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1856f48ce188SMatt Jacob 	} else {
1857f48ce188SMatt Jacob 		atiop->sense_len = 0;
1858f48ce188SMatt Jacob 	}
1859d81ba9d5SMatt Jacob 
18605d571944SMatt Jacob 	atiop->init_id = GET_IID_VAL(aep->at_iid);
1861d81ba9d5SMatt Jacob 	atiop->cdb_len = aep->at_cdblen;
18622df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1863d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
1864a1bc34c6SMatt Jacob 	/*
1865a1bc34c6SMatt Jacob 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
1866a1bc34c6SMatt Jacob 	 * and the handle (which we have to preserve).
1867a1bc34c6SMatt Jacob 	 */
18682df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
1869a1bc34c6SMatt Jacob 	if (aep->at_flags & AT_TQAE) {
1870a1bc34c6SMatt Jacob 		atiop->tag_action = aep->at_tag_type;
1871d81ba9d5SMatt Jacob 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1872d81ba9d5SMatt Jacob 	}
18732df76c16SMatt Jacob 	atp->orig_datalen = 0;
18742df76c16SMatt Jacob 	atp->bytes_xfered = 0;
18752df76c16SMatt Jacob 	atp->last_xframt = 0;
18762df76c16SMatt Jacob 	atp->lun = aep->at_lun;
18772df76c16SMatt Jacob 	atp->nphdl = aep->at_iid;
18782df76c16SMatt Jacob 	atp->portid = PORT_NONE;
18792df76c16SMatt Jacob 	atp->oxid = 0;
18802df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
18812df76c16SMatt Jacob 	atp->tattr = aep->at_tag_type;
18822df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
18832df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1884d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
1885d81ba9d5SMatt Jacob }
1886d81ba9d5SMatt Jacob 
18872df76c16SMatt Jacob static void
18889cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1889d81ba9d5SMatt Jacob {
189092a1e549SMatt Jacob 	lun_id_t lun;
18912df76c16SMatt Jacob 	fcportdb_t *lp;
1892d81ba9d5SMatt Jacob 	tstate_t *tptr;
1893d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
18942df76c16SMatt Jacob 	uint16_t nphdl;
18952df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
18962df76c16SMatt Jacob 	inot_private_data_t *ntp;
1897d81ba9d5SMatt Jacob 
1898d81ba9d5SMatt Jacob 	/*
1899d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
1900d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
1901d81ba9d5SMatt Jacob 	 *
1902d81ba9d5SMatt Jacob 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1903d81ba9d5SMatt Jacob 	 */
1904d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
19052df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1906d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
19072df76c16SMatt Jacob 		return;
1908d81ba9d5SMatt Jacob 	}
1909d81ba9d5SMatt Jacob 
19102df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
191192a1e549SMatt Jacob 		lun = aep->at_scclun;
19122ad50ca5SMatt Jacob 	} else {
191392a1e549SMatt Jacob 		lun = aep->at_lun;
19142ad50ca5SMatt Jacob 	}
19152df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
19162df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
19172df76c16SMatt Jacob 	} else {
19182df76c16SMatt Jacob 		nphdl = aep->at_iid;
19192df76c16SMatt Jacob 	}
1920a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
1921d81ba9d5SMatt Jacob 	if (tptr == NULL) {
1922a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1923746e9c85SMatt Jacob 		if (tptr == NULL) {
19242df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
19252df76c16SMatt Jacob 			isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
19262df76c16SMatt Jacob 			return;
1927746e9c85SMatt Jacob 		}
1928d81ba9d5SMatt Jacob 	}
1929d81ba9d5SMatt Jacob 
1930d81ba9d5SMatt Jacob 	/*
19312df76c16SMatt Jacob 	 * Start any commands pending resources first.
1932d81ba9d5SMatt Jacob 	 */
19332df76c16SMatt Jacob 	if (tptr->restart_queue) {
19342df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
19352df76c16SMatt Jacob 		tptr->restart_queue = NULL;
19362df76c16SMatt Jacob 		while (restart_queue) {
19372df76c16SMatt Jacob 			ntp = restart_queue;
19382df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
19392df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
19402df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
19412df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
19422df76c16SMatt Jacob 			/*
19432df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
19442df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
19452df76c16SMatt Jacob 			 * it and go to noresrc.
19462df76c16SMatt Jacob 			 */
19472df76c16SMatt Jacob 			if (tptr->restart_queue) {
19482df76c16SMatt Jacob 				ntp = tptr->restart_queue;
19492df76c16SMatt Jacob 				tptr->restart_queue = restart_queue;
19502df76c16SMatt Jacob 				while (restart_queue->rd.nt.nt_hba) {
19512df76c16SMatt Jacob 					restart_queue = restart_queue->rd.nt.nt_hba;
1952d81ba9d5SMatt Jacob 				}
19532df76c16SMatt Jacob 				restart_queue->rd.nt.nt_hba = ntp;
19542df76c16SMatt Jacob 				goto noresrc;
19552df76c16SMatt Jacob 			}
19562df76c16SMatt Jacob 		}
19572df76c16SMatt Jacob 	}
19582df76c16SMatt Jacob 
19592df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
19602df76c16SMatt Jacob 	if (atiop == NULL) {
19612df76c16SMatt Jacob 		goto noresrc;
19622df76c16SMatt Jacob 	}
19632df76c16SMatt Jacob 
19642df76c16SMatt Jacob 	atp = isp_get_atpd(isp, tptr, 0);
19652df76c16SMatt Jacob 	if (atp == NULL) {
19662df76c16SMatt Jacob 		goto noresrc;
19672df76c16SMatt Jacob 	}
19682df76c16SMatt Jacob 
19692df76c16SMatt Jacob 	atp->tag = aep->at_rxid;
1970570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
1971d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1972570c7a3fSMatt Jacob 	tptr->atio_count--;
19732df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
19742df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
197592a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
19762df76c16SMatt Jacob 
1977b0a3ba7eSMatt Jacob 	/*
1978b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1979b0a3ba7eSMatt Jacob 	 */
1980f48ce188SMatt Jacob 	atiop->sense_len = 0;
19812df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
19822df76c16SMatt Jacob 		/*
19832df76c16SMatt Jacob 		 * NB: We could not possibly have 2K logins if we
19842df76c16SMatt Jacob 		 * NB: also did not have SCC FW.
19852df76c16SMatt Jacob 		 */
19862df76c16SMatt Jacob 		atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
19872df76c16SMatt Jacob 	} else {
1988d81ba9d5SMatt Jacob 		atiop->init_id = aep->at_iid;
19892df76c16SMatt Jacob 	}
19902df76c16SMatt Jacob 
19912df76c16SMatt Jacob 	/*
19922df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
19932df76c16SMatt Jacob 	 */
19942df76c16SMatt Jacob 	if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
19952df76c16SMatt Jacob     		uint64_t iid =
19962df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[0]) << 48) |
19972df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[1]) << 32) |
19982df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[2]) << 16) |
19992df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[3]) <<  0);
20002df76c16SMatt Jacob 		/*
20012df76c16SMatt Jacob 		 * However, make sure we delete ourselves if otherwise
20022df76c16SMatt Jacob 		 * we were there but at a different loop id.
20032df76c16SMatt Jacob 		 */
20042df76c16SMatt Jacob 		if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
20052df76c16SMatt Jacob 			isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
20062df76c16SMatt Jacob 		}
20072df76c16SMatt Jacob 		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
20082df76c16SMatt Jacob 	}
2009d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
20102df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2011d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
20122df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
2013d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2014d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
20152df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2016d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2017d81ba9d5SMatt Jacob 		break;
2018d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
20192df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2020d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2021d81ba9d5SMatt Jacob 		break;
2022d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
20232df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2024d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2025d81ba9d5SMatt Jacob 		break;
2026d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
2027d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
2028d81ba9d5SMatt Jacob 	default:
2029d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
2030d81ba9d5SMatt Jacob 		break;
2031d81ba9d5SMatt Jacob 	}
2032f48ce188SMatt Jacob 
203353036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
203453036e92SMatt Jacob 	atp->bytes_xfered = 0;
20352df76c16SMatt Jacob 	atp->last_xframt = 0;
20362df76c16SMatt Jacob 	atp->lun = lun;
20372df76c16SMatt Jacob 	atp->nphdl = atiop->init_id;
20382df76c16SMatt Jacob 	atp->sid = PORT_ANY;
20392df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
20402df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
20412df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2042570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
2043d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
20442df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2045d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
20462df76c16SMatt Jacob 	return;
20472df76c16SMatt Jacob noresrc:
20482df76c16SMatt Jacob 	if (atp) {
20492df76c16SMatt Jacob 		isp_put_atpd(isp, tptr, atp);
20502df76c16SMatt Jacob 	}
20512df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
20522df76c16SMatt Jacob 	if (ntp == NULL) {
20532df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
20542df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
20552df76c16SMatt Jacob 		return;
20562df76c16SMatt Jacob 	}
20572df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
20582df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
20592df76c16SMatt Jacob 	tptr->restart_queue = ntp;
20602df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2061d81ba9d5SMatt Jacob }
2062d81ba9d5SMatt Jacob 
20632df76c16SMatt Jacob static void
20642df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
20652df76c16SMatt Jacob {
20662df76c16SMatt Jacob 	int cdbxlen;
20672df76c16SMatt Jacob 	uint16_t lun, chan, nphdl = NIL_HANDLE;
20682df76c16SMatt Jacob 	uint32_t did, sid;
20692df76c16SMatt Jacob 	uint64_t wwn = INI_NONE;
20702df76c16SMatt Jacob 	fcportdb_t *lp;
20712df76c16SMatt Jacob 	tstate_t *tptr;
20722df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
20732df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
20742df76c16SMatt Jacob 	inot_private_data_t *ntp;
20752df76c16SMatt Jacob 
20762df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
20772df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
20782df76c16SMatt Jacob 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
20792df76c16SMatt Jacob 
20802df76c16SMatt Jacob 	/*
20812df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
20822df76c16SMatt Jacob 	 *
20832df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
20842df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
20852df76c16SMatt Jacob 	 */
20862df76c16SMatt Jacob 	if (ISP_CAP_MULTI_ID(isp)) {
20872df76c16SMatt Jacob 		/*
20882df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
20892df76c16SMatt Jacob 		 */
20902df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
20912df76c16SMatt Jacob 
20922df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
20932df76c16SMatt Jacob 			NANOTIME_T now;
20942df76c16SMatt Jacob 
20952df76c16SMatt Jacob 			/*
20962df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
20972df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
20982df76c16SMatt Jacob 			 */
20992df76c16SMatt Jacob 			GET_NANOTIME(&now);
21002df76c16SMatt Jacob 			if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
21012df76c16SMatt 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);
21022df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
21032df76c16SMatt Jacob 				return;
21042df76c16SMatt Jacob 			}
21052df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
21062df76c16SMatt Jacob 			if (tptr == NULL) {
21072df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
21082df76c16SMatt Jacob 				if (tptr == NULL) {
21092df76c16SMatt 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);
21102df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
21112df76c16SMatt Jacob 					return;
21122df76c16SMatt Jacob 				}
21132df76c16SMatt Jacob 			}
21142df76c16SMatt 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);
21152df76c16SMatt Jacob 			goto noresrc;
21162df76c16SMatt Jacob 		}
21172df76c16SMatt 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);
21182df76c16SMatt Jacob 	} else {
21192df76c16SMatt Jacob 		chan = 0;
21202df76c16SMatt Jacob 	}
21212df76c16SMatt Jacob 
21222df76c16SMatt Jacob 	/*
21232df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
21242df76c16SMatt Jacob 	 */
21252df76c16SMatt Jacob 	if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
21262df76c16SMatt Jacob 		/*
21272df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
21282df76c16SMatt Jacob 		 */
21292df76c16SMatt 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",
21302df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
21312df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
21322df76c16SMatt Jacob 		return;
21332df76c16SMatt Jacob 	}
21342df76c16SMatt Jacob 	nphdl = lp->handle;
21352df76c16SMatt Jacob 	wwn = lp->port_wwn;
21362df76c16SMatt Jacob 
21372df76c16SMatt Jacob 	/*
21382df76c16SMatt Jacob 	 * Get the tstate pointer
21392df76c16SMatt Jacob 	 */
21402df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
21412df76c16SMatt Jacob 	if (tptr == NULL) {
21422df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
21432df76c16SMatt Jacob 		if (tptr == NULL) {
21442df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
21452df76c16SMatt Jacob 			isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
21462df76c16SMatt Jacob 			return;
21472df76c16SMatt Jacob 		}
21482df76c16SMatt Jacob 	}
21492df76c16SMatt Jacob 
21502df76c16SMatt Jacob 	/*
21512df76c16SMatt Jacob 	 * Start any commands pending resources first.
21522df76c16SMatt Jacob 	 */
21532df76c16SMatt Jacob 	if (tptr->restart_queue) {
21542df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
21552df76c16SMatt Jacob 		tptr->restart_queue = NULL;
21562df76c16SMatt Jacob 		while (restart_queue) {
21572df76c16SMatt Jacob 			ntp = restart_queue;
21582df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
21592df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
21602df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
21612df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
21622df76c16SMatt Jacob 			/*
21632df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
21642df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
21652df76c16SMatt Jacob 			 * it and go to noresrc.
21662df76c16SMatt Jacob 			 */
21672df76c16SMatt Jacob 			if (tptr->restart_queue) {
21682df76c16SMatt Jacob 				if (restart_queue) {
21692df76c16SMatt Jacob 					ntp = tptr->restart_queue;
21702df76c16SMatt Jacob 					tptr->restart_queue = restart_queue;
21712df76c16SMatt Jacob 					while (restart_queue->rd.nt.nt_hba) {
21722df76c16SMatt Jacob 						restart_queue = restart_queue->rd.nt.nt_hba;
21732df76c16SMatt Jacob 					}
21742df76c16SMatt Jacob 					restart_queue->rd.nt.nt_hba = ntp;
21752df76c16SMatt Jacob 				}
21762df76c16SMatt Jacob 				goto noresrc;
21772df76c16SMatt Jacob 			}
21782df76c16SMatt Jacob 		}
21792df76c16SMatt Jacob 	}
21802df76c16SMatt Jacob 
21812df76c16SMatt Jacob 	/*
21822df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
21832df76c16SMatt Jacob 	 */
21842df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
21852df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
21862df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
21872df76c16SMatt Jacob 		return;
21882df76c16SMatt Jacob 	}
21892df76c16SMatt Jacob 
21902df76c16SMatt Jacob 	/*
21912df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
21922df76c16SMatt Jacob 	 */
21932df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
21942df76c16SMatt Jacob 	if (atiop == NULL) {
21952df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
21962df76c16SMatt Jacob 		goto noresrc;
21972df76c16SMatt Jacob 	}
21982df76c16SMatt Jacob 
21992df76c16SMatt Jacob 	atp = isp_get_atpd(isp, tptr, 0);
22002df76c16SMatt Jacob 	if (atp == NULL) {
22012df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
22022df76c16SMatt Jacob 		goto noresrc;
22032df76c16SMatt Jacob 	}
22042df76c16SMatt Jacob 	if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
22052df76c16SMatt 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)\n",
22062df76c16SMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
22072df76c16SMatt Jacob 		/*
22082df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
22092df76c16SMatt Jacob 		 */
22102df76c16SMatt Jacob 		goto noresrc;
22112df76c16SMatt Jacob 	}
22122df76c16SMatt Jacob 
22132df76c16SMatt Jacob 	atp->tag = aep->at_rxid;
22142df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
22152df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
22162df76c16SMatt Jacob 	tptr->atio_count--;
22172df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
22182df76c16SMatt Jacob 	atiop->init_id = nphdl;
22192df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
22202df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
22212df76c16SMatt Jacob 	atiop->sense_len = 0;
22222df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
22232df76c16SMatt Jacob 	if (cdbxlen) {
22242df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
22252df76c16SMatt Jacob 	}
22262df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
22272df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
22282df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
22292df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
22302df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
22312df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
22322df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
22332df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
22342df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
22352df76c16SMatt Jacob 		break;
22362df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
22372df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
22382df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
22392df76c16SMatt Jacob 		break;
22402df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
22412df76c16SMatt Jacob 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
22422df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
22432df76c16SMatt Jacob 		break;
22442df76c16SMatt Jacob 	default:
22452df76c16SMatt Jacob 		/* FALLTHROUGH */
22462df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
22472df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
22482df76c16SMatt Jacob 		atiop->tag_action = 0;
22492df76c16SMatt Jacob 		break;
22502df76c16SMatt Jacob 	}
22512df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
22522df76c16SMatt Jacob 	atp->bytes_xfered = 0;
22532df76c16SMatt Jacob 	atp->last_xframt = 0;
22542df76c16SMatt Jacob 	atp->lun = lun;
22552df76c16SMatt Jacob 	atp->nphdl = nphdl;
22562df76c16SMatt Jacob 	atp->portid = sid;
22572df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
22582df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
22592df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
22602df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
22612df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
22622df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
22632df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22642df76c16SMatt Jacob 	return;
22652df76c16SMatt Jacob noresrc:
22662df76c16SMatt Jacob 	if (atp) {
22672df76c16SMatt Jacob 		isp_put_atpd(isp, tptr, atp);
22682df76c16SMatt Jacob 	}
22692df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
22702df76c16SMatt Jacob 	if (ntp == NULL) {
22712df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
22722df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
22732df76c16SMatt Jacob 		return;
22742df76c16SMatt Jacob 	}
22752df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
22762df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
22772df76c16SMatt Jacob 	tptr->restart_queue = ntp;
22782df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
22792df76c16SMatt Jacob }
22802df76c16SMatt Jacob 
22812df76c16SMatt Jacob static void
22829cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2283d81ba9d5SMatt Jacob {
2284d81ba9d5SMatt Jacob 	union ccb *ccb;
2285a1bc34c6SMatt Jacob 	int sentstatus, ok, notify_cam, resid = 0;
22862df76c16SMatt Jacob 	tstate_t *tptr = NULL;
22872df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
22882df76c16SMatt Jacob 	int bus;
22892df76c16SMatt Jacob 	uint32_t tval, handle;
2290d81ba9d5SMatt Jacob 
2291d81ba9d5SMatt Jacob 	/*
22922df76c16SMatt Jacob 	 * CTIO, CTIO2 and CTIO7 are close enough....
2293d81ba9d5SMatt Jacob 	 */
2294d81ba9d5SMatt Jacob 
22952df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
22962df76c16SMatt Jacob 		handle = ((ct_entry_t *)arg)->ct_syshandle;
22972df76c16SMatt Jacob 	} else {
22982df76c16SMatt Jacob 		handle = ((ct2_entry_t *)arg)->ct_syshandle;
22992df76c16SMatt Jacob 	}
23002df76c16SMatt Jacob 	ccb = isp_find_xs_tgt(isp, handle);
23012df76c16SMatt Jacob 	if (ccb == NULL) {
23022df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
23032df76c16SMatt Jacob 		return;
23042df76c16SMatt Jacob 	}
23052df76c16SMatt Jacob 	isp_destroy_tgt_handle(isp, handle);
23062df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
23072df76c16SMatt Jacob 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
23082df76c16SMatt Jacob 	if (tptr == NULL) {
23092df76c16SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
23102df76c16SMatt Jacob 	}
23112df76c16SMatt Jacob 	KASSERT((tptr != NULL), ("cannot get state pointer"));
23122df76c16SMatt Jacob 	if (isp->isp_nactive) {
23132df76c16SMatt Jacob 		isp->isp_nactive++;
23142df76c16SMatt Jacob 	}
23152df76c16SMatt Jacob 	if (IS_24XX(isp)) {
23162df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2317d81ba9d5SMatt Jacob 
23182df76c16SMatt Jacob 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2319570c7a3fSMatt Jacob 		if (atp == NULL) {
23202df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
23212df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
23222df76c16SMatt Jacob 			return;
23232df76c16SMatt Jacob 		}
23242df76c16SMatt Jacob 
23252df76c16SMatt Jacob 		sentstatus = ct->ct_flags & CT7_SENDSTATUS;
23262df76c16SMatt Jacob 		ok = (ct->ct_nphdl == CT7_OK);
23272df76c16SMatt Jacob 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
23282df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
23292df76c16SMatt Jacob 		}
23302df76c16SMatt Jacob 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
23312df76c16SMatt Jacob 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
23322df76c16SMatt Jacob 			resid = ct->ct_resid;
23332df76c16SMatt Jacob 			atp->bytes_xfered += (atp->last_xframt - resid);
23342df76c16SMatt Jacob 			atp->last_xframt = 0;
23352df76c16SMatt Jacob 		}
23362df76c16SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
23372df76c16SMatt Jacob 			ok = 0;
23382df76c16SMatt Jacob 			notify_cam = 1;
23392df76c16SMatt Jacob 			sentstatus = 1;
23402df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
23412df76c16SMatt Jacob 		} else if (!ok) {
23422df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
23432df76c16SMatt Jacob 		}
23442df76c16SMatt Jacob 		tval = atp->tag;
23452df76c16SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
23462df76c16SMatt Jacob 		    ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
23472df76c16SMatt Jacob 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
23482df76c16SMatt Jacob 	} else if (IS_FC(isp)) {
23492df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
23502df76c16SMatt Jacob 
23512df76c16SMatt Jacob 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
23522df76c16SMatt Jacob 		if (atp == NULL) {
23532df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
23542df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
23552df76c16SMatt Jacob 			return;
2356570c7a3fSMatt Jacob 		}
2357d81ba9d5SMatt Jacob 		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2358d81ba9d5SMatt Jacob 		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2359a1bc34c6SMatt Jacob 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
236000a8e174SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
236100a8e174SMatt Jacob 		}
2362a1bc34c6SMatt Jacob 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
23635d571944SMatt Jacob 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2364a1bc34c6SMatt Jacob 			resid = ct->ct_resid;
236553036e92SMatt Jacob 			atp->bytes_xfered += (atp->last_xframt - resid);
236653036e92SMatt Jacob 			atp->last_xframt = 0;
2367570c7a3fSMatt Jacob 		}
23682df76c16SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
23692df76c16SMatt Jacob 			ok = 0;
23702df76c16SMatt Jacob 			notify_cam = 1;
23712df76c16SMatt Jacob 			sentstatus = 1;
23722df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
23732df76c16SMatt Jacob 		} else if (!ok) {
23742df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
237553036e92SMatt Jacob 		}
23762df76c16SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
23772df76c16SMatt Jacob 		    ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
23782df76c16SMatt Jacob 		tval = atp->tag;
23792df76c16SMatt Jacob 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2380d81ba9d5SMatt Jacob 	} else {
2381d81ba9d5SMatt Jacob 		ct_entry_t *ct = arg;
2382d81ba9d5SMatt Jacob 		sentstatus = ct->ct_flags & CT_SENDSTATUS;
2383d81ba9d5SMatt Jacob 		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
2384d81ba9d5SMatt Jacob 		/*
2385a1bc34c6SMatt Jacob 		 * We *ought* to be able to get back to the original ATIO
2386a1bc34c6SMatt Jacob 		 * here, but for some reason this gets lost. It's just as
2387a1bc34c6SMatt Jacob 		 * well because it's squirrelled away as part of periph
2388a1bc34c6SMatt Jacob 		 * private data.
2389a1bc34c6SMatt Jacob 		 *
2390a1bc34c6SMatt Jacob 		 * We can live without it as long as we continue to use
2391a1bc34c6SMatt Jacob 		 * the auto-replenish feature for CTIOs.
2392a1bc34c6SMatt Jacob 		 */
2393a1bc34c6SMatt Jacob 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
23942df76c16SMatt Jacob 		if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
23952df76c16SMatt Jacob 			ok = 0;
23962df76c16SMatt Jacob 			notify_cam = 1;
23972df76c16SMatt Jacob 			sentstatus = 1;
23982df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
23992df76c16SMatt Jacob 		} else if (!ok) {
24002df76c16SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
24012df76c16SMatt Jacob 		} else if (ct->ct_status & QLTM_SVALID) {
2402a1bc34c6SMatt Jacob 			char *sp = (char *)ct;
2403a1bc34c6SMatt Jacob 			sp += CTIO_SENSE_OFFSET;
24042df76c16SMatt Jacob 			ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
24052df76c16SMatt Jacob 			ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2406a1bc34c6SMatt Jacob 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2407a1bc34c6SMatt Jacob 		}
24085d571944SMatt Jacob 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2409a1bc34c6SMatt Jacob 			resid = ct->ct_resid;
2410a1bc34c6SMatt Jacob 		}
24112df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
24122df76c16SMatt Jacob 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
241364edff94SMatt Jacob 		tval = ct->ct_fwhandle;
24145d571944SMatt Jacob 	}
2415a1bc34c6SMatt Jacob 	ccb->csio.resid += resid;
2416a1bc34c6SMatt Jacob 
2417a1bc34c6SMatt Jacob 	/*
2418a1bc34c6SMatt Jacob 	 * We're here either because intermediate data transfers are done
2419a1bc34c6SMatt Jacob 	 * and/or the final status CTIO (which may have joined with a
2420a1bc34c6SMatt Jacob 	 * Data Transfer) is done.
2421d81ba9d5SMatt Jacob 	 *
2422d81ba9d5SMatt Jacob 	 * In any case, for this platform, the upper layers figure out
2423d81ba9d5SMatt Jacob 	 * what to do next, so all we do here is collect status and
2424a1bc34c6SMatt Jacob 	 * pass information along. Any DMA handles have already been
2425a1bc34c6SMatt Jacob 	 * freed.
2426d81ba9d5SMatt Jacob 	 */
2427f48ce188SMatt Jacob 	if (notify_cam == 0) {
242864edff94SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
24292df76c16SMatt Jacob 		return;
2430f48ce188SMatt Jacob 	}
24312df76c16SMatt Jacob 	if (tptr) {
24322df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
24332df76c16SMatt Jacob 	}
24342df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? "  FINAL " : "MIDTERM ", tval);
2435d81ba9d5SMatt Jacob 
24362df76c16SMatt Jacob 	if (!ok && !IS_24XX(isp)) {
2437a1bc34c6SMatt Jacob 		isp_target_putback_atio(ccb);
2438d81ba9d5SMatt Jacob 	} else {
2439a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
2440d81ba9d5SMatt Jacob 	}
2441d81ba9d5SMatt Jacob }
2442570c7a3fSMatt Jacob 
24432df76c16SMatt Jacob static void
24442df76c16SMatt Jacob isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2445570c7a3fSMatt Jacob {
24462df76c16SMatt Jacob 	(void) isp_notify_ack(isp, inot);
2447570c7a3fSMatt Jacob }
2448570c7a3fSMatt Jacob 
24492df76c16SMatt Jacob static void
24509cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2451570c7a3fSMatt Jacob {
24522df76c16SMatt Jacob 	int needack = 1;
2453570c7a3fSMatt Jacob 	switch (inp->in_status) {
2454570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
24552df76c16SMatt Jacob 		/*
24562df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
24572df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
24582df76c16SMatt Jacob 		 */
24592df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2460570c7a3fSMatt Jacob 		break;
2461570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
24622df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2463570c7a3fSMatt Jacob 		break;
2464570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
24652df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
2466570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2467570c7a3fSMatt Jacob 		break;
2468570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
2469570c7a3fSMatt Jacob 	{
24702df76c16SMatt Jacob 		tstate_t *tptr;
24712df76c16SMatt Jacob 		uint16_t lun;
24722df76c16SMatt Jacob 		uint32_t loopid;
24732df76c16SMatt Jacob 		uint64_t wwn;
24742df76c16SMatt Jacob 		atio_private_data_t *atp;
24752df76c16SMatt Jacob 		fcportdb_t *lp;
24762df76c16SMatt Jacob 		struct ccb_immediate_notify *inot = NULL;
24772df76c16SMatt Jacob 
24782df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
24792df76c16SMatt Jacob 			lun = inp->in_scclun;
24802df76c16SMatt Jacob 		} else {
24812df76c16SMatt Jacob 			lun = inp->in_lun;
24822df76c16SMatt Jacob 		}
24832df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
24842df76c16SMatt Jacob 			loopid = ((in_fcentry_e_t *)inot)->in_iid;
24852df76c16SMatt Jacob 		} else {
24862df76c16SMatt Jacob 			loopid = inp->in_iid;
24872df76c16SMatt Jacob 		}
24882df76c16SMatt Jacob 		if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
24892df76c16SMatt Jacob 			wwn = lp->port_wwn;
24902df76c16SMatt Jacob 		} else {
24912df76c16SMatt Jacob 			wwn = INI_ANY;
24922df76c16SMatt Jacob 		}
24932df76c16SMatt Jacob 		tptr = get_lun_statep(isp, 0, lun);
24942df76c16SMatt Jacob 		if (tptr == NULL) {
24952df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
24962df76c16SMatt Jacob 			if (tptr == NULL) {
24972df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
24982df76c16SMatt Jacob 				return;
24992df76c16SMatt Jacob 			}
25002df76c16SMatt Jacob 		}
25012df76c16SMatt Jacob 		atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2502570c7a3fSMatt Jacob 
2503570c7a3fSMatt Jacob 		if (atp) {
25042df76c16SMatt Jacob 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
25052df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2506570c7a3fSMatt Jacob 			if (inot) {
2507746e9c85SMatt Jacob 				tptr->inot_count--;
25082df76c16SMatt Jacob 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
25092df76c16SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2510570c7a3fSMatt Jacob 			} else {
25112df76c16SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
25122df76c16SMatt Jacob 			}
25132df76c16SMatt Jacob 		} else {
25142df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2515570c7a3fSMatt Jacob 		}
2516570c7a3fSMatt Jacob 		if (inot) {
25172df76c16SMatt Jacob 			isp_notify_t tmp, *nt = &tmp;
25182df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
25192df76c16SMatt Jacob     			nt->nt_hba = isp;
25202df76c16SMatt Jacob 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
25212df76c16SMatt Jacob 			nt->nt_wwn = wwn;
25222df76c16SMatt Jacob 			nt->nt_nphdl = loopid;
25232df76c16SMatt Jacob 			nt->nt_sid = PORT_ANY;
25242df76c16SMatt Jacob 			nt->nt_did = PORT_ANY;
25252df76c16SMatt Jacob     			nt->nt_lun = lun;
25262df76c16SMatt Jacob             		nt->nt_need_ack = 1;
25272df76c16SMatt Jacob     			nt->nt_channel = 0;
25282df76c16SMatt Jacob     			nt->nt_ncode = NT_ABORT_TASK;
25292df76c16SMatt Jacob     			nt->nt_lreserved = inot;
25302df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
25312df76c16SMatt Jacob 			needack = 0;
2532570c7a3fSMatt Jacob 		}
25332df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
2534570c7a3fSMatt Jacob 		break;
2535570c7a3fSMatt Jacob 	}
2536570c7a3fSMatt Jacob 	default:
2537570c7a3fSMatt Jacob 		break;
2538570c7a3fSMatt Jacob 	}
25392df76c16SMatt Jacob 	if (needack) {
25402df76c16SMatt Jacob 		(void) isp_notify_ack(isp, inp);
25412df76c16SMatt Jacob 	}
25422df76c16SMatt Jacob }
25432df76c16SMatt Jacob 
25442df76c16SMatt Jacob static void
25452df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
25462df76c16SMatt Jacob {
25472df76c16SMatt Jacob 	uint16_t nphdl;
25482df76c16SMatt Jacob 	uint32_t portid;
25492df76c16SMatt Jacob 	fcportdb_t *lp;
25502df76c16SMatt Jacob 	uint8_t *ptr = NULL;
25512df76c16SMatt Jacob 	uint64_t wwn;
25522df76c16SMatt Jacob 
25532df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
25542df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
25552df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
25562df76c16SMatt Jacob 	} else {
25572df76c16SMatt Jacob 		portid = PORT_ANY;
25582df76c16SMatt Jacob 	}
25592df76c16SMatt Jacob 
25602df76c16SMatt Jacob 	switch (inot->in_status) {
25612df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
25622df76c16SMatt Jacob 	{
25632df76c16SMatt Jacob 		char buf[16], *msg;
25642df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
25652df76c16SMatt Jacob 
25662df76c16SMatt Jacob 		/*
25672df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
25682df76c16SMatt Jacob 		 * (possibly with some associcated information sent upstream). This is
25692df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
25702df76c16SMatt Jacob 		 */
25712df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
25722df76c16SMatt Jacob 		case LOGO:
25732df76c16SMatt Jacob 			msg = "LOGO";
25742df76c16SMatt Jacob 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
25752df76c16SMatt Jacob 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
25762df76c16SMatt Jacob 				wwn =	(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF])   << 56) |
25772df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
25782df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
25792df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
25802df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
25812df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
25822df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) <<  8) |
25832df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
25842df76c16SMatt Jacob 			} else {
25852df76c16SMatt Jacob 				wwn = INI_ANY;
25862df76c16SMatt Jacob 			}
25872df76c16SMatt Jacob 			isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
25882df76c16SMatt Jacob 			break;
25892df76c16SMatt Jacob 		case PRLO:
25902df76c16SMatt Jacob 			msg = "PRLO";
25912df76c16SMatt Jacob 			break;
25922df76c16SMatt Jacob 		case PLOGI:
25932df76c16SMatt Jacob 			msg = "PLOGI";
25942df76c16SMatt Jacob 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
25952df76c16SMatt Jacob 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
25962df76c16SMatt Jacob 				wwn =	(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
25972df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
25982df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
25992df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
26002df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
26012df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
26022df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) <<  8) |
26032df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
26042df76c16SMatt Jacob 			} else {
26052df76c16SMatt Jacob 				wwn = INI_NONE;
26062df76c16SMatt Jacob 			}
26072df76c16SMatt Jacob 			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
26082df76c16SMatt Jacob 			break;
26092df76c16SMatt Jacob 		case PRLI:
26102df76c16SMatt Jacob 			msg = "PRLI";
26112df76c16SMatt Jacob 			break;
26122df76c16SMatt Jacob 		case PDISC:
26132df76c16SMatt Jacob 			msg = "PDISC";
26142df76c16SMatt Jacob 			break;
26152df76c16SMatt Jacob 		case ADISC:
26162df76c16SMatt Jacob 			msg = "ADISC";
26172df76c16SMatt Jacob 			break;
26182df76c16SMatt Jacob 		default:
26192df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
26202df76c16SMatt Jacob 			msg = buf;
26212df76c16SMatt Jacob 			break;
26222df76c16SMatt Jacob 		}
26232df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
26242df76c16SMatt 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);
26252df76c16SMatt Jacob 			break;
26262df76c16SMatt Jacob 		}
26272df76c16SMatt 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,
26282df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
26292df76c16SMatt Jacob 		(void) isp_notify_ack(isp, inot);
26302df76c16SMatt Jacob 		break;
26312df76c16SMatt Jacob 	}
26322df76c16SMatt Jacob 
26332df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
26342df76c16SMatt Jacob 		ptr = "PORT LOGOUT";
26352df76c16SMatt Jacob 		if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
26362df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
26372df76c16SMatt Jacob 		}
26382df76c16SMatt Jacob 		/* FALLTHROUGH */
26392df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
26402df76c16SMatt Jacob 		if (ptr == NULL) {
26412df76c16SMatt Jacob 			ptr = "PORT CHANGED";
26422df76c16SMatt Jacob 		}
26432df76c16SMatt Jacob 		/* FALLTHROUGH */
26442df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
26452df76c16SMatt Jacob 		if (ptr == NULL) {
26462df76c16SMatt Jacob 			ptr = "LIP RESET";
26472df76c16SMatt Jacob 		}
26482df76c16SMatt Jacob 		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), ptr, inot->in_status_subcode, nphdl);
26492df76c16SMatt Jacob 
26502df76c16SMatt Jacob 		/*
26512df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
26522df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
26532df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
26542df76c16SMatt Jacob 		 */
26552df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
26562df76c16SMatt Jacob 		(void) isp_notify_ack(isp, inot);
26572df76c16SMatt Jacob 		break;
26582df76c16SMatt Jacob 
26592df76c16SMatt Jacob 	case IN24XX_LINK_RESET:
26602df76c16SMatt Jacob 	case IN24XX_LINK_FAILED:
26612df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
26622df76c16SMatt Jacob 	default:
26632df76c16SMatt Jacob 		(void) isp_notify_ack(isp, inot);
26642df76c16SMatt Jacob 		break;
26652df76c16SMatt Jacob 	}
26662df76c16SMatt Jacob }
26672df76c16SMatt Jacob 
26682df76c16SMatt Jacob static int
26692df76c16SMatt Jacob isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
26702df76c16SMatt Jacob {
26712df76c16SMatt Jacob 
26722df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
26732df76c16SMatt 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);
2674570c7a3fSMatt Jacob 		return (0);
2675570c7a3fSMatt Jacob 	}
26762df76c16SMatt Jacob 
26772df76c16SMatt Jacob 	/*
26782df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
26792df76c16SMatt Jacob 	 */
26802df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
26812df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
26822df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
26832df76c16SMatt Jacob 		fcportdb_t *lp;
26842df76c16SMatt Jacob 		uint32_t sid;
26852df76c16SMatt Jacob 		uint16_t nphdl;
26862df76c16SMatt Jacob 
26872df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
26882df76c16SMatt Jacob 		if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
26892df76c16SMatt Jacob 			nphdl = lp->handle;
26902df76c16SMatt Jacob 		} else {
26912df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
26922df76c16SMatt Jacob 		}
26932df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
26942df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
26952df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
26962df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
26972df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
26982df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
26992df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
27002df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
27012df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
27022df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
27032df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
27042df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
27052df76c16SMatt Jacob 	}
27062df76c16SMatt Jacob 
27072df76c16SMatt Jacob 	/*
27082df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
27092df76c16SMatt Jacob 	 */
27102df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
27112df76c16SMatt Jacob 
27122df76c16SMatt Jacob 		/*
27132df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
27142df76c16SMatt Jacob 		 */
27152df76c16SMatt Jacob 		if (mp->nt_need_ack) {
27162df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
27172df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
27182df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
27192df76c16SMatt Jacob 
27202df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
27212df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
27222df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
27232df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
27242df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
27252df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
27262df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
27272df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
27282df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
27292df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
27302df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
27312df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
27322df76c16SMatt Jacob 				return (ENOMEM);
27332df76c16SMatt Jacob 			}
27342df76c16SMatt Jacob 			mp->nt_need_ack = 0;
27352df76c16SMatt Jacob 		}
27362df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
27372df76c16SMatt Jacob 			return (ENOMEM);
27382df76c16SMatt Jacob 		} else {
27392df76c16SMatt Jacob 			return (0);
27402df76c16SMatt Jacob 		}
27412df76c16SMatt Jacob 	}
27422df76c16SMatt Jacob 
27432df76c16SMatt Jacob 	/*
27442df76c16SMatt Jacob 	 * Handle logout cases here
27452df76c16SMatt Jacob 	 */
27462df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
27472df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
27482df76c16SMatt Jacob 	}
27492df76c16SMatt Jacob 
27502df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
27512df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
27522df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
27532df76c16SMatt Jacob 		}
27542df76c16SMatt Jacob 	}
27552df76c16SMatt Jacob 
27562df76c16SMatt Jacob 	/*
27572df76c16SMatt Jacob 	 * General purpose acknowledgement
27582df76c16SMatt Jacob 	 */
27592df76c16SMatt Jacob 	if (mp->nt_need_ack) {
27602df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
27612df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
27622df76c16SMatt Jacob 	}
27632df76c16SMatt Jacob 	return (0);
27642df76c16SMatt Jacob }
27652df76c16SMatt Jacob 
27662df76c16SMatt Jacob /*
27672df76c16SMatt Jacob  * Handle task managment functions.
27682df76c16SMatt Jacob  *
27692df76c16SMatt Jacob  * We show up here with a notify structure filled out.
27702df76c16SMatt Jacob  *
27712df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
27722df76c16SMatt Jacob  */
27732df76c16SMatt Jacob static void
27742df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
27752df76c16SMatt Jacob {
27762df76c16SMatt Jacob 	tstate_t *tptr;
27772df76c16SMatt Jacob 	fcportdb_t *lp;
27782df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
27792df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
27802df76c16SMatt Jacob 	lun_id_t lun;
27812df76c16SMatt Jacob 
27822df76c16SMatt 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,
27832df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
27842df76c16SMatt Jacob 	/*
27852df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
27862df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
27872df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
27882df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
27892df76c16SMatt Jacob 	 */
27902df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
27912df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
27922df76c16SMatt Jacob 	} else {
27932df76c16SMatt Jacob 		lun = notify->nt_lun;
27942df76c16SMatt Jacob 	}
27952df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
27962df76c16SMatt Jacob 	if (tptr == NULL) {
27972df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
27982df76c16SMatt Jacob 		if (tptr == NULL) {
27992df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
28002df76c16SMatt Jacob 			goto bad;
28012df76c16SMatt Jacob 		}
28022df76c16SMatt Jacob 	}
28032df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
28042df76c16SMatt Jacob 	if (inot == NULL) {
28052df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
28062df76c16SMatt Jacob 		goto bad;
28072df76c16SMatt Jacob 	}
28082df76c16SMatt Jacob 
28092df76c16SMatt Jacob 	if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
28102df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
28112df76c16SMatt Jacob 	} else {
28122df76c16SMatt Jacob 		inot->initiator_id = lp->handle;
28132df76c16SMatt Jacob 	}
28142df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
28152df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
28162df76c16SMatt Jacob 
28172df76c16SMatt Jacob 	switch (notify->nt_ncode) {
28182df76c16SMatt Jacob 	case NT_ABORT_TASK:
28192df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
28202df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
28212df76c16SMatt Jacob 		break;
28222df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
28232df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
28242df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
28252df76c16SMatt Jacob 		break;
28262df76c16SMatt Jacob 	case NT_CLEAR_ACA:
28272df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
28282df76c16SMatt Jacob 		break;
28292df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
28302df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
28312df76c16SMatt Jacob 		break;
28322df76c16SMatt Jacob 	case NT_LUN_RESET:
28332df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
28342df76c16SMatt Jacob 		break;
28352df76c16SMatt Jacob 	case NT_TARGET_RESET:
28362df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
28372df76c16SMatt Jacob 		break;
28382df76c16SMatt Jacob 	default:
28392df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
28402df76c16SMatt Jacob 		goto bad;
28412df76c16SMatt Jacob 	}
28422df76c16SMatt Jacob 
28432df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
28442df76c16SMatt Jacob 	if (ntp == NULL) {
28452df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
28462df76c16SMatt Jacob 		goto bad;
28472df76c16SMatt Jacob 	}
28482df76c16SMatt Jacob 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
28492df76c16SMatt Jacob 	if (notify->nt_lreserved) {
28502df76c16SMatt Jacob 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
28512df76c16SMatt Jacob 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
28522df76c16SMatt Jacob 	}
28532df76c16SMatt Jacob 	ntp->rd.seq_id = notify->nt_tagval;
28542df76c16SMatt Jacob 	ntp->rd.tag_id = notify->nt_tagval >> 32;
28552df76c16SMatt Jacob 
28562df76c16SMatt Jacob 	tptr->inot_count--;
28572df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
28582df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
28592df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
28602df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
28612df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
28622df76c16SMatt Jacob 	return;
28632df76c16SMatt Jacob bad:
28642df76c16SMatt Jacob 	if (tptr) {
28652df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
28662df76c16SMatt Jacob 	}
28672df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
28682df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
28692df76c16SMatt Jacob 			(void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
28702df76c16SMatt Jacob 		} else {
28712df76c16SMatt Jacob 			(void) isp_notify_ack(isp, notify->nt_lreserved);
28722df76c16SMatt Jacob 		}
28732df76c16SMatt Jacob 	}
28742df76c16SMatt Jacob }
28752df76c16SMatt Jacob 
28762df76c16SMatt Jacob /*
28772df76c16SMatt Jacob  * Find the associated private data and makr it as dead so
28782df76c16SMatt Jacob  * we don't try to work on it any further.
28792df76c16SMatt Jacob  */
28802df76c16SMatt Jacob static void
28812df76c16SMatt Jacob isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
28822df76c16SMatt Jacob {
28832df76c16SMatt Jacob 	tstate_t *tptr;
28842df76c16SMatt Jacob 	atio_private_data_t *atp;
28852df76c16SMatt Jacob 
28862df76c16SMatt Jacob 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
28872df76c16SMatt Jacob 	if (tptr == NULL) {
28882df76c16SMatt Jacob 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
28892df76c16SMatt Jacob 		if (tptr == NULL) {
28902df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
28912df76c16SMatt Jacob 			return;
28922df76c16SMatt Jacob 		}
28932df76c16SMatt Jacob 	}
28942df76c16SMatt Jacob 
28952df76c16SMatt Jacob 	atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
28962df76c16SMatt Jacob 	if (atp == NULL) {
28972df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INVALID;
28982df76c16SMatt Jacob 		return;
28992df76c16SMatt Jacob 	}
29002df76c16SMatt Jacob 	atp->dead = 1;
29012df76c16SMatt Jacob 	ccb->ccb_h.status = CAM_REQ_CMP;
29022df76c16SMatt Jacob }
29032df76c16SMatt Jacob 
29042df76c16SMatt Jacob static void
29052df76c16SMatt Jacob isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
29062df76c16SMatt Jacob {
29072df76c16SMatt Jacob 	atio_private_data_t *atp;
29082df76c16SMatt Jacob 	inot_private_data_t *restart_queue = tptr->restart_queue;
29092df76c16SMatt Jacob 
29102df76c16SMatt Jacob 	/*
29112df76c16SMatt Jacob 	 * First, clean any commands pending restart
29122df76c16SMatt Jacob 	 */
29132df76c16SMatt Jacob 	tptr->restart_queue = NULL;
29142df76c16SMatt Jacob 	while (restart_queue) {
29152df76c16SMatt Jacob 		uint32_t this_tag_id;
29162df76c16SMatt Jacob 		inot_private_data_t *ntp = restart_queue;
29172df76c16SMatt Jacob 
29182df76c16SMatt Jacob 		restart_queue = ntp->rd.nt.nt_hba;
29192df76c16SMatt Jacob 
29202df76c16SMatt Jacob 		if (IS_24XX(isp)) {
29212df76c16SMatt Jacob 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
29222df76c16SMatt Jacob 		} else {
29232df76c16SMatt Jacob 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
29242df76c16SMatt Jacob 		}
29252df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
29262df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
29272df76c16SMatt Jacob 		} else {
29282df76c16SMatt Jacob 			ntp->rd.nt.nt_hba = tptr->restart_queue;
29292df76c16SMatt Jacob 			tptr->restart_queue = ntp;
29302df76c16SMatt Jacob 		}
29312df76c16SMatt Jacob 	}
29322df76c16SMatt Jacob 
29332df76c16SMatt Jacob 	/*
29342df76c16SMatt Jacob 	 * Now mark other ones dead as well.
29352df76c16SMatt Jacob 	 */
29362df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
29372df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
29382df76c16SMatt Jacob 			atp->dead = 1;
29392df76c16SMatt Jacob 		}
29402df76c16SMatt Jacob 	}
29412df76c16SMatt Jacob }
29422df76c16SMatt Jacob 
29432df76c16SMatt Jacob 
29442df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
29452df76c16SMatt Jacob // #define	ISP_FORCE_TIMEOUT		1
29462df76c16SMatt Jacob #define	ISP_TEST_WWNS			1
29472df76c16SMatt Jacob #define	ISP_TEST_SEPARATE_STATUS	1
29482df76c16SMatt Jacob 
29492df76c16SMatt Jacob #define	ccb_data_offset		ppriv_field0
29502df76c16SMatt Jacob #define	ccb_atio		ppriv_ptr1
29512df76c16SMatt Jacob #define	ccb_inot		ppriv_ptr1
29522df76c16SMatt Jacob 
29532df76c16SMatt Jacob #define	MAX_ISP_TARG_TRANSFER	(2 << 20)
29542df76c16SMatt Jacob #define	NISP_TARG_CMDS		1024
29552df76c16SMatt Jacob #define	NISP_TARG_NOTIFIES	1024
29562df76c16SMatt Jacob #define	DISK_SHIFT		9
29572df76c16SMatt Jacob #define	JUNK_SIZE		256
29582df76c16SMatt Jacob 
29592df76c16SMatt Jacob #ifndef	VERIFY_10
29602df76c16SMatt Jacob #define	VERIFY_10	0x2f
29612df76c16SMatt Jacob #endif
29622df76c16SMatt Jacob 
29632df76c16SMatt Jacob TAILQ_HEAD(ccb_queue, ccb_hdr);
29642df76c16SMatt Jacob extern u_int vm_kmem_size;
29652df76c16SMatt Jacob static int ca;
29662df76c16SMatt Jacob static uint32_t disk_size;
29672df76c16SMatt Jacob static uint8_t *disk_data = NULL;
29682df76c16SMatt Jacob static uint8_t *junk_data;
29692df76c16SMatt Jacob static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
29702df76c16SMatt Jacob struct isptarg_softc {
29712df76c16SMatt Jacob 	/* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
29722df76c16SMatt Jacob 	struct ccb_queue	work_queue;
29732df76c16SMatt Jacob 	struct ccb_queue	rework_queue;
29742df76c16SMatt Jacob 	struct ccb_queue	running_queue;
29752df76c16SMatt Jacob 	struct ccb_queue	inot_queue;
29762df76c16SMatt Jacob 	struct cam_periph       *periph;
29772df76c16SMatt Jacob 	struct cam_path	 	*path;
29782df76c16SMatt Jacob 	ispsoftc_t		*isp;
29792df76c16SMatt Jacob };
29802df76c16SMatt Jacob static periph_ctor_t	isptargctor;
29812df76c16SMatt Jacob static periph_dtor_t	isptargdtor;
29822df76c16SMatt Jacob static periph_start_t	isptargstart;
29832df76c16SMatt Jacob static periph_init_t	isptarginit;
29842df76c16SMatt Jacob static void		isptarg_done(struct cam_periph *, union ccb *);
29852df76c16SMatt Jacob static void		isptargasync(void *, u_int32_t, struct cam_path *, void *);
29862df76c16SMatt Jacob 
29872df76c16SMatt Jacob 
29882df76c16SMatt Jacob static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
29892df76c16SMatt Jacob 
29902df76c16SMatt Jacob static struct periph_driver isptargdriver =
29912df76c16SMatt Jacob {
29922df76c16SMatt Jacob 	isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
29932df76c16SMatt Jacob };
29942df76c16SMatt Jacob 
29952df76c16SMatt Jacob static void
29962df76c16SMatt Jacob isptarginit(void)
29972df76c16SMatt Jacob {
29982df76c16SMatt Jacob }
29992df76c16SMatt Jacob 
30002df76c16SMatt Jacob static void
30012df76c16SMatt Jacob isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
30022df76c16SMatt Jacob {
30032df76c16SMatt Jacob 	struct ccb_notify_acknowledge *ack = &iccb->cna2;
30042df76c16SMatt Jacob 
30052df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
30062df76c16SMatt Jacob 	    inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
30072df76c16SMatt Jacob 	ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
30082df76c16SMatt Jacob 	ack->ccb_h.flags = 0;
30092df76c16SMatt Jacob 	ack->ccb_h.retry_count = 0;
30102df76c16SMatt Jacob 	ack->ccb_h.cbfcnp = isptarg_done;
30112df76c16SMatt Jacob 	ack->ccb_h.timeout = 0;
30122df76c16SMatt Jacob 	ack->ccb_h.ccb_inot = inot;
30132df76c16SMatt Jacob 	ack->tag_id = inot->tag_id;
30142df76c16SMatt Jacob 	ack->seq_id = inot->seq_id;
30152df76c16SMatt Jacob 	ack->initiator_id = inot->initiator_id;
30162df76c16SMatt Jacob 	xpt_action(iccb);
30172df76c16SMatt Jacob }
30182df76c16SMatt Jacob 
30192df76c16SMatt Jacob static void
30202df76c16SMatt Jacob isptargstart(struct cam_periph *periph, union ccb *iccb)
30212df76c16SMatt Jacob {
30222df76c16SMatt Jacob 	const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = { 0x7f };
30232df76c16SMatt Jacob 	const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
30242df76c16SMatt Jacob 		0, 0x0, 0x2, 0x2, 32, 0, 0, 0x32,
30252df76c16SMatt Jacob 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
30262df76c16SMatt Jacob 		'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
30272df76c16SMatt Jacob 		'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
30282df76c16SMatt Jacob 		'0', '0', '0', '1'
30292df76c16SMatt Jacob 	};
30302df76c16SMatt Jacob 	int i, more = 0, last;
30312df76c16SMatt Jacob 	struct isptarg_softc *softc = periph->softc;
30322df76c16SMatt Jacob 	struct ccb_scsiio *csio;
30332df76c16SMatt Jacob 	lun_id_t return_lun;
30342df76c16SMatt Jacob 	struct ccb_accept_tio *atio;
30352df76c16SMatt Jacob 	uint8_t *cdb, *ptr, status;
30362df76c16SMatt Jacob 	uint8_t *data_ptr;
30372df76c16SMatt Jacob 	uint32_t data_len, flags;
30382df76c16SMatt Jacob 	struct ccb_hdr *ccbh;
30392df76c16SMatt Jacob 
30402df76c16SMatt Jacob 	mtx_assert(periph->sim->mtx, MA_OWNED);
30412df76c16SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
30422df76c16SMatt Jacob 	    TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
30432df76c16SMatt Jacob 	/*
30442df76c16SMatt Jacob 	 * Check for immediate notifies first
30452df76c16SMatt Jacob 	 */
30462df76c16SMatt Jacob 	ccbh = TAILQ_FIRST(&softc->inot_queue);
30472df76c16SMatt Jacob 	if (ccbh) {
30482df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
30492df76c16SMatt Jacob 		if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
30502df76c16SMatt Jacob 			xpt_schedule(periph, 1);
30512df76c16SMatt Jacob 		}
30522df76c16SMatt Jacob 		isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
30532df76c16SMatt Jacob 		return;
30542df76c16SMatt Jacob 	}
30552df76c16SMatt Jacob 
30562df76c16SMatt Jacob 	/*
30572df76c16SMatt Jacob 	 * Check the rework (continuation) work queue first.
30582df76c16SMatt Jacob 	 */
30592df76c16SMatt Jacob 	ccbh = TAILQ_FIRST(&softc->rework_queue);
30602df76c16SMatt Jacob 	if (ccbh) {
30612df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *)ccbh;
30622df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
30632df76c16SMatt Jacob 		more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
30642df76c16SMatt Jacob 	} else {
30652df76c16SMatt Jacob 		ccbh = TAILQ_FIRST(&softc->work_queue);
30662df76c16SMatt Jacob 		if (ccbh == NULL) {
30672df76c16SMatt Jacob 			ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
30682df76c16SMatt Jacob 			xpt_release_ccb(iccb);
30692df76c16SMatt Jacob 			return;
30702df76c16SMatt Jacob 		}
30712df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *)ccbh;
30722df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
30732df76c16SMatt Jacob 		more = TAILQ_FIRST(&softc->work_queue) != NULL;
30742df76c16SMatt Jacob 		atio->ccb_h.ccb_data_offset = 0;
30752df76c16SMatt Jacob 	}
30762df76c16SMatt Jacob 
30772df76c16SMatt Jacob 	if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
30782df76c16SMatt Jacob 		panic("BAD ATIO");
30792df76c16SMatt Jacob 	}
30802df76c16SMatt Jacob 
30812df76c16SMatt Jacob 	data_ptr = NULL;
30822df76c16SMatt Jacob 	data_len = 0;
30832df76c16SMatt Jacob 	csio = &iccb->csio;
30842df76c16SMatt Jacob 	status = SCSI_STATUS_OK;
30852df76c16SMatt Jacob 	flags = CAM_SEND_STATUS;
30862df76c16SMatt Jacob 	memset(&atio->sense_data, 0, sizeof (atio->sense_data));
30872df76c16SMatt Jacob 	cdb = atio->cdb_io.cdb_bytes;
30882df76c16SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
30892df76c16SMatt Jacob 	    cdb[0], atio->ccb_h.ccb_data_offset);
30902df76c16SMatt Jacob 
30912df76c16SMatt Jacob 	return_lun = XS_LUN(atio);
30922df76c16SMatt Jacob 	if (return_lun != 0) {
30932df76c16SMatt Jacob 		xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
30942df76c16SMatt Jacob 		if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
30952df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
30962df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
30972df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x25;
30982df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x0;
30992df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
31002df76c16SMatt Jacob 		}
31012df76c16SMatt Jacob 		return_lun = CAM_LUN_WILDCARD;
31022df76c16SMatt Jacob 	}
31032df76c16SMatt Jacob 
31042df76c16SMatt Jacob 	switch (cdb[0]) {
31052df76c16SMatt Jacob 	case REQUEST_SENSE:
31062df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
31072df76c16SMatt Jacob 		data_len = sizeof (atio->sense_data);
31082df76c16SMatt Jacob 		junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
31092df76c16SMatt Jacob 		memset(junk_data+1, 0, data_len-1);
31102df76c16SMatt Jacob 		if (data_len > cdb[4]) {
31112df76c16SMatt Jacob 			data_len = cdb[4];
31122df76c16SMatt Jacob 		}
31132df76c16SMatt Jacob 		if (data_len) {
31142df76c16SMatt Jacob 			data_ptr = junk_data;
31152df76c16SMatt Jacob 		}
31162df76c16SMatt Jacob 		break;
31172df76c16SMatt Jacob 	case READ_6:
31182df76c16SMatt Jacob 	case READ_10:
31192df76c16SMatt Jacob 	case READ_12:
31202df76c16SMatt Jacob 	case READ_16:
31212df76c16SMatt Jacob 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
31222df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
31232df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
31242df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x5;
31252df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x24;
31262df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
31272df76c16SMatt Jacob 		} else {
31282df76c16SMatt Jacob #ifdef	ISP_FORCE_TIMEOUT
31292df76c16SMatt Jacob 			{
31302df76c16SMatt Jacob 				static int foo;
31312df76c16SMatt Jacob 				if (foo++ == 500) {
31322df76c16SMatt Jacob 					if (more) {
31332df76c16SMatt Jacob 						xpt_schedule(periph, 1);
31342df76c16SMatt Jacob 					}
31352df76c16SMatt Jacob 					foo = 0;
31362df76c16SMatt Jacob 					return;
31372df76c16SMatt Jacob 				}
31382df76c16SMatt Jacob 			}
31392df76c16SMatt Jacob #endif
31402df76c16SMatt Jacob #ifdef	ISP_TEST_SEPARATE_STATUS
31412df76c16SMatt Jacob 			if (last && data_len) {
31422df76c16SMatt Jacob 				last = 0;
31432df76c16SMatt Jacob 			}
31442df76c16SMatt Jacob #endif
31452df76c16SMatt Jacob 			if (last == 0) {
31462df76c16SMatt Jacob 				flags &= ~CAM_SEND_STATUS;
31472df76c16SMatt Jacob 			}
31482df76c16SMatt Jacob 			if (data_len) {
31492df76c16SMatt Jacob 				atio->ccb_h.ccb_data_offset += data_len;
31502df76c16SMatt Jacob 				flags |= CAM_DIR_IN;
31512df76c16SMatt Jacob 			} else {
31522df76c16SMatt Jacob 				flags |= CAM_DIR_NONE;
31532df76c16SMatt Jacob 			}
31542df76c16SMatt Jacob 		}
31552df76c16SMatt Jacob 		break;
31562df76c16SMatt Jacob 	case WRITE_6:
31572df76c16SMatt Jacob 	case WRITE_10:
31582df76c16SMatt Jacob 	case WRITE_12:
31592df76c16SMatt Jacob 	case WRITE_16:
31602df76c16SMatt Jacob 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
31612df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
31622df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
31632df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x5;
31642df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x24;
31652df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
31662df76c16SMatt Jacob 		} else {
31672df76c16SMatt Jacob #ifdef	ISP_FORCE_TIMEOUT
31682df76c16SMatt Jacob 			{
31692df76c16SMatt Jacob 				static int foo;
31702df76c16SMatt Jacob 				if (foo++ == 500) {
31712df76c16SMatt Jacob 					if (more) {
31722df76c16SMatt Jacob 						xpt_schedule(periph, 1);
31732df76c16SMatt Jacob 					}
31742df76c16SMatt Jacob 					foo = 0;
31752df76c16SMatt Jacob 					return;
31762df76c16SMatt Jacob 				}
31772df76c16SMatt Jacob 			}
31782df76c16SMatt Jacob #endif
31792df76c16SMatt Jacob #ifdef	ISP_TEST_SEPARATE_STATUS
31802df76c16SMatt Jacob 			if (last && data_len) {
31812df76c16SMatt Jacob 				last = 0;
31822df76c16SMatt Jacob 			}
31832df76c16SMatt Jacob #endif
31842df76c16SMatt Jacob 			if (last == 0) {
31852df76c16SMatt Jacob 				flags &= ~CAM_SEND_STATUS;
31862df76c16SMatt Jacob 			}
31872df76c16SMatt Jacob 			if (data_len) {
31882df76c16SMatt Jacob 				atio->ccb_h.ccb_data_offset += data_len;
31892df76c16SMatt Jacob 				flags |= CAM_DIR_OUT;
31902df76c16SMatt Jacob 			} else {
31912df76c16SMatt Jacob 				flags |= CAM_DIR_NONE;
31922df76c16SMatt Jacob 			}
31932df76c16SMatt Jacob 		}
31942df76c16SMatt Jacob 		break;
31952df76c16SMatt Jacob 	case INQUIRY:
31962df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
31972df76c16SMatt Jacob 		if (cdb[1] || cdb[2] || cdb[3]) {
31982df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
31992df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
32002df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x5;
32012df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x20;
32022df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
32032df76c16SMatt Jacob 			break;
32042df76c16SMatt Jacob 		}
32052df76c16SMatt Jacob 		data_len = sizeof (iqd);
32062df76c16SMatt Jacob 		if (data_len > cdb[4]) {
32072df76c16SMatt Jacob 			data_len = cdb[4];
32082df76c16SMatt Jacob 		}
32092df76c16SMatt Jacob 		if (data_len) {
32102df76c16SMatt Jacob 			if (XS_LUN(iccb) != 0) {
32112df76c16SMatt Jacob 				memcpy(junk_data, niliqd, sizeof (iqd));
32122df76c16SMatt Jacob 			} else {
32132df76c16SMatt Jacob 				memcpy(junk_data, iqd, sizeof (iqd));
32142df76c16SMatt Jacob 			}
32152df76c16SMatt Jacob 			data_ptr = junk_data;
32162df76c16SMatt Jacob 		}
32172df76c16SMatt Jacob 		break;
32182df76c16SMatt Jacob 	case TEST_UNIT_READY:
32192df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
32202df76c16SMatt Jacob 		if (ca) {
32212df76c16SMatt Jacob 			ca = 0;
32222df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
32232df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
32242df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x28;
32252df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x0;
32262df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
32272df76c16SMatt Jacob 		}
32282df76c16SMatt Jacob 		break;
32292df76c16SMatt Jacob 	case SYNCHRONIZE_CACHE:
32302df76c16SMatt Jacob 	case START_STOP:
32312df76c16SMatt Jacob 	case RESERVE:
32322df76c16SMatt Jacob 	case RELEASE:
32332df76c16SMatt Jacob 	case VERIFY_10:
32342df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
32352df76c16SMatt Jacob 		break;
32362df76c16SMatt Jacob 
32372df76c16SMatt Jacob 	case READ_CAPACITY:
32382df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
32392df76c16SMatt Jacob 		if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
32402df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
32412df76c16SMatt Jacob 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
32422df76c16SMatt Jacob 			atio->sense_data.add_sense_code = 0x5;
32432df76c16SMatt Jacob 			atio->sense_data.add_sense_code_qual = 0x24;
32442df76c16SMatt Jacob 			atio->sense_len = sizeof (atio->sense_data);
32452df76c16SMatt Jacob 			break;
32462df76c16SMatt Jacob 		}
32472df76c16SMatt Jacob 		if (cdb[8] & 0x1) { /* PMI */
32482df76c16SMatt Jacob 			junk_data[0] = 0xff;
32492df76c16SMatt Jacob 			junk_data[1] = 0xff;
32502df76c16SMatt Jacob 			junk_data[2] = 0xff;
32512df76c16SMatt Jacob 			junk_data[3] = 0xff;
32522df76c16SMatt Jacob 		} else {
32532df76c16SMatt Jacob 			uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
32542df76c16SMatt Jacob 			if (last_blk < 0xffffffffULL) {
32552df76c16SMatt Jacob 			    junk_data[0] = (last_blk >> 24) & 0xff;
32562df76c16SMatt Jacob 			    junk_data[1] = (last_blk >> 16) & 0xff;
32572df76c16SMatt Jacob 			    junk_data[2] = (last_blk >>  8) & 0xff;
32582df76c16SMatt Jacob 			    junk_data[3] = (last_blk) & 0xff;
32592df76c16SMatt Jacob 			} else {
32602df76c16SMatt Jacob 			    junk_data[0] = 0xff;
32612df76c16SMatt Jacob 			    junk_data[1] = 0xff;
32622df76c16SMatt Jacob 			    junk_data[2] = 0xff;
32632df76c16SMatt Jacob 			    junk_data[3] = 0xff;
32642df76c16SMatt Jacob 			}
32652df76c16SMatt Jacob 		}
32662df76c16SMatt Jacob 		junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
32672df76c16SMatt Jacob 		junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
32682df76c16SMatt Jacob 		junk_data[6] = ((1 << DISK_SHIFT) >>  8) & 0xff;
32692df76c16SMatt Jacob 		junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
32702df76c16SMatt Jacob 		data_ptr = junk_data;
32712df76c16SMatt Jacob 		data_len = 8;
32722df76c16SMatt Jacob 		break;
32732df76c16SMatt Jacob 	case REPORT_LUNS:
32742df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
32752df76c16SMatt Jacob 		memset(junk_data, 0, JUNK_SIZE);
32762df76c16SMatt Jacob 		junk_data[0] = (1 << 3) >> 24;
32772df76c16SMatt Jacob 		junk_data[1] = (1 << 3) >> 16;
32782df76c16SMatt Jacob 		junk_data[2] = (1 << 3) >> 8;
32792df76c16SMatt Jacob 		junk_data[3] = (1 << 3);
32802df76c16SMatt Jacob 		ptr = NULL;
32812df76c16SMatt Jacob 		for (i = 0; i < 1; i++) {
32822df76c16SMatt Jacob 			ptr = &junk_data[8 + (1 << 3)];
32832df76c16SMatt Jacob 			if (i >= 256) {
32842df76c16SMatt Jacob 				ptr[0] = 0x40 | ((i >> 8) & 0x3f);
32852df76c16SMatt Jacob 			}
32862df76c16SMatt Jacob 			ptr[1] = i;
32872df76c16SMatt Jacob 		}
32882df76c16SMatt Jacob 		data_ptr = junk_data;
32892df76c16SMatt Jacob 		data_len = (ptr + 8) - junk_data;
32902df76c16SMatt Jacob 		break;
32912df76c16SMatt Jacob 
32922df76c16SMatt Jacob 	default:
32932df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
32942df76c16SMatt Jacob 		status = SCSI_STATUS_CHECK_COND;
32952df76c16SMatt Jacob 		atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
32962df76c16SMatt Jacob 		atio->sense_data.add_sense_code = 0x5;
32972df76c16SMatt Jacob 		atio->sense_data.add_sense_code_qual = 0x20;
32982df76c16SMatt Jacob 		atio->sense_len = sizeof (atio->sense_data);
32992df76c16SMatt Jacob 		break;
33002df76c16SMatt Jacob 	}
33012df76c16SMatt Jacob 
33022df76c16SMatt Jacob 	/*
33032df76c16SMatt Jacob 	 * If we are done with the transaction, tell the
33042df76c16SMatt Jacob 	 * controller to send status and perform a CMD_CMPLT.
33052df76c16SMatt Jacob 	 * If we have associated sense data, see if we can
33062df76c16SMatt Jacob 	 * send that too.
33072df76c16SMatt Jacob 	 */
33082df76c16SMatt Jacob 	if (status == SCSI_STATUS_CHECK_COND) {
33092df76c16SMatt Jacob 		flags |= CAM_SEND_SENSE;
33102df76c16SMatt Jacob 		csio->sense_len = atio->sense_len;
33112df76c16SMatt Jacob 		csio->sense_data = atio->sense_data;
33122df76c16SMatt Jacob 		flags &= ~CAM_DIR_MASK;
33132df76c16SMatt Jacob 		data_len = 0;
33142df76c16SMatt Jacob 		data_ptr = NULL;
33152df76c16SMatt Jacob 	}
33162df76c16SMatt Jacob 	cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
33172df76c16SMatt Jacob 	iccb->ccb_h.target_id = atio->ccb_h.target_id;
33182df76c16SMatt Jacob 	iccb->ccb_h.target_lun = return_lun;
33192df76c16SMatt Jacob 	iccb->ccb_h.ccb_atio = atio;
33202df76c16SMatt Jacob 	xpt_action(iccb);
33212df76c16SMatt Jacob 
33222df76c16SMatt Jacob 	if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
33232df76c16SMatt Jacob 		cam_release_devq(periph->path, 0, 0, 0, 0);
33242df76c16SMatt Jacob 		atio->ccb_h.status &= ~CAM_DEV_QFRZN;
33252df76c16SMatt Jacob 	}
33262df76c16SMatt Jacob 	if (more) {
33272df76c16SMatt Jacob 		xpt_schedule(periph, 1);
33282df76c16SMatt Jacob 	}
33292df76c16SMatt Jacob }
33302df76c16SMatt Jacob 
33312df76c16SMatt Jacob static cam_status
33322df76c16SMatt Jacob isptargctor(struct cam_periph *periph, void *arg)
33332df76c16SMatt Jacob {
33342df76c16SMatt Jacob 	struct isptarg_softc *softc;
33352df76c16SMatt Jacob 
33362df76c16SMatt Jacob 	softc = (struct isptarg_softc *)arg;
33372df76c16SMatt Jacob 	periph->softc = softc;
33382df76c16SMatt Jacob 	softc->periph = periph;
33392df76c16SMatt Jacob 	softc->path = periph->path;
33402df76c16SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
33412df76c16SMatt Jacob 	return (CAM_REQ_CMP);
33422df76c16SMatt Jacob }
33432df76c16SMatt Jacob 
33442df76c16SMatt Jacob static void
33452df76c16SMatt Jacob isptargdtor(struct cam_periph *periph)
33462df76c16SMatt Jacob {
33472df76c16SMatt Jacob 	struct isptarg_softc *softc;
33482df76c16SMatt Jacob 	softc = (struct isptarg_softc *)periph->softc;
33492df76c16SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
33502df76c16SMatt Jacob 	softc->periph = NULL;
33512df76c16SMatt Jacob 	softc->path = NULL;
33522df76c16SMatt Jacob 	periph->softc = NULL;
33532df76c16SMatt Jacob }
33542df76c16SMatt Jacob 
33552df76c16SMatt Jacob static void
33562df76c16SMatt Jacob isptarg_done(struct cam_periph *periph, union ccb *ccb)
33572df76c16SMatt Jacob {
33582df76c16SMatt Jacob 	struct isptarg_softc *softc;
33592df76c16SMatt Jacob 	ispsoftc_t *isp;
33602df76c16SMatt Jacob 	struct ccb_accept_tio *atio;
33612df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
33622df76c16SMatt Jacob 	cam_status status;
33632df76c16SMatt Jacob 
33642df76c16SMatt Jacob 	softc = (struct isptarg_softc *)periph->softc;
33652df76c16SMatt Jacob 	isp = softc->isp;
33662df76c16SMatt Jacob 	status = ccb->ccb_h.status & CAM_STATUS_MASK;
33672df76c16SMatt Jacob 
33682df76c16SMatt Jacob 	switch (ccb->ccb_h.func_code) {
33692df76c16SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:
33702df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *) ccb;
33712df76c16SMatt Jacob 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
33722df76c16SMatt Jacob 		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
33732df76c16SMatt Jacob 		xpt_schedule(periph, 1);
33742df76c16SMatt Jacob 		break;
33752df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
33762df76c16SMatt Jacob 		inot = (struct ccb_immediate_notify *) ccb;
33772df76c16SMatt Jacob 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
33782df76c16SMatt Jacob 		TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
33792df76c16SMatt Jacob 		xpt_schedule(periph, 1);
33802df76c16SMatt Jacob 		break;
33812df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
33822df76c16SMatt Jacob 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
33832df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
33842df76c16SMatt Jacob 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
33852df76c16SMatt Jacob 		}
33862df76c16SMatt Jacob 		atio = ccb->ccb_h.ccb_atio;
33872df76c16SMatt Jacob 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
33882df76c16SMatt Jacob 			cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
33892df76c16SMatt Jacob 			xpt_action((union ccb *)atio);
33902df76c16SMatt Jacob 		} else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
33912df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
33922df76c16SMatt Jacob 			TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
33932df76c16SMatt Jacob 			xpt_schedule(periph, 1);
33942df76c16SMatt Jacob 		} else {
33952df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
33962df76c16SMatt Jacob 			xpt_action((union ccb *)atio);
33972df76c16SMatt Jacob 		}
33982df76c16SMatt Jacob 		xpt_release_ccb(ccb);
33992df76c16SMatt Jacob 		break;
34002df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
34012df76c16SMatt Jacob 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
34022df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
34032df76c16SMatt Jacob 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
34042df76c16SMatt Jacob 		}
34052df76c16SMatt Jacob 		inot = ccb->ccb_h.ccb_inot;
34062df76c16SMatt Jacob 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
34072df76c16SMatt Jacob 		xpt_release_ccb(ccb);
34082df76c16SMatt Jacob 		xpt_action((union ccb *)inot);
34092df76c16SMatt Jacob 		break;
34102df76c16SMatt Jacob 	default:
34112df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
34122df76c16SMatt Jacob 		break;
34132df76c16SMatt Jacob 	}
34142df76c16SMatt Jacob }
34152df76c16SMatt Jacob 
34162df76c16SMatt Jacob static void
34172df76c16SMatt Jacob isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
34182df76c16SMatt Jacob {
34192df76c16SMatt Jacob 	struct ac_contract *acp = arg;
34202df76c16SMatt Jacob 	struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
34212df76c16SMatt Jacob 
34222df76c16SMatt Jacob 	if (code != AC_CONTRACT) {
34232df76c16SMatt Jacob 		return;
34242df76c16SMatt Jacob 	}
34252df76c16SMatt Jacob 	xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
34262df76c16SMatt Jacob }
34272df76c16SMatt Jacob 
34282df76c16SMatt Jacob static void
34292df76c16SMatt Jacob isp_target_thread(ispsoftc_t *isp, int chan)
34302df76c16SMatt Jacob {
34312df76c16SMatt Jacob 	union ccb *ccb = NULL;
34322df76c16SMatt Jacob 	int i;
34332df76c16SMatt Jacob 	void *wchan;
34342df76c16SMatt Jacob 	cam_status status;
34352df76c16SMatt Jacob 	struct isptarg_softc *softc = NULL;
34362df76c16SMatt Jacob 	struct cam_periph *periph = NULL, *wperiph = NULL;
34372df76c16SMatt Jacob 	struct cam_path *path, *wpath;
34382df76c16SMatt Jacob 	struct cam_sim *sim;
34392df76c16SMatt Jacob 
34402df76c16SMatt Jacob 	if (disk_data == NULL) {
34412df76c16SMatt Jacob 		disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
34422df76c16SMatt Jacob 		if (disk_size < (50 << 20)) {
34432df76c16SMatt Jacob 			disk_size = 50 << 20;
34442df76c16SMatt Jacob 		}
34452df76c16SMatt Jacob 		disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
34462df76c16SMatt Jacob 		if (disk_data == NULL) {
34472df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
34482df76c16SMatt Jacob 			goto out;
34492df76c16SMatt Jacob 		}
34502df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
34512df76c16SMatt Jacob 	}
34522df76c16SMatt Jacob 	junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
34532df76c16SMatt Jacob 	if (junk_data == NULL) {
34542df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
34552df76c16SMatt Jacob 		goto out;
34562df76c16SMatt Jacob 	}
34572df76c16SMatt Jacob 
34582df76c16SMatt Jacob 
34592df76c16SMatt Jacob 	softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
34602df76c16SMatt Jacob 	if (softc == NULL) {
34612df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
34622df76c16SMatt Jacob 		goto out;
34632df76c16SMatt Jacob 	}
34642df76c16SMatt Jacob 	TAILQ_INIT(&softc->work_queue);
34652df76c16SMatt Jacob 	TAILQ_INIT(&softc->rework_queue);
34662df76c16SMatt Jacob 	TAILQ_INIT(&softc->running_queue);
34672df76c16SMatt Jacob 	TAILQ_INIT(&softc->inot_queue);
34682df76c16SMatt Jacob 	softc->isp = isp;
34692df76c16SMatt Jacob 
34702df76c16SMatt Jacob 	periphdriver_register(&isptargdriver);
34712df76c16SMatt Jacob 	ISP_GET_PC(isp, chan, sim, sim);
34722df76c16SMatt Jacob 	ISP_GET_PC(isp, chan, path,  path);
34732df76c16SMatt Jacob 	status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
34742df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
34752df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
34762df76c16SMatt Jacob 		return;
34772df76c16SMatt Jacob 	}
34782df76c16SMatt Jacob 	status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
34792df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
34802df76c16SMatt Jacob 		xpt_free_path(wpath);
34812df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
34822df76c16SMatt Jacob 		return;
34832df76c16SMatt Jacob 	}
34842df76c16SMatt Jacob 
34852df76c16SMatt Jacob 	ccb = xpt_alloc_ccb();
34862df76c16SMatt Jacob 
34872df76c16SMatt Jacob 	ISP_LOCK(isp);
34882df76c16SMatt Jacob 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
34892df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
34902df76c16SMatt Jacob 		ISP_UNLOCK(isp);
34912df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
34922df76c16SMatt Jacob 		goto out;
34932df76c16SMatt Jacob 	}
34942df76c16SMatt Jacob 	wperiph = cam_periph_find(wpath, "isptarg");
34952df76c16SMatt Jacob 	if (wperiph == NULL) {
34962df76c16SMatt Jacob 		ISP_UNLOCK(isp);
34972df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
34982df76c16SMatt Jacob 		goto out;
34992df76c16SMatt Jacob 	}
35002df76c16SMatt Jacob 
35012df76c16SMatt Jacob 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
35022df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
35032df76c16SMatt Jacob 		ISP_UNLOCK(isp);
35042df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
35052df76c16SMatt Jacob 		goto out;
35062df76c16SMatt Jacob 	}
35072df76c16SMatt Jacob 
35082df76c16SMatt Jacob 	periph = cam_periph_find(path, "isptarg");
35092df76c16SMatt Jacob 	if (periph == NULL) {
35102df76c16SMatt Jacob 		ISP_UNLOCK(isp);
35112df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
35122df76c16SMatt Jacob 		goto out;
35132df76c16SMatt Jacob 	}
35142df76c16SMatt Jacob 
35152df76c16SMatt Jacob 	status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
35162df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
35172df76c16SMatt Jacob 		ISP_UNLOCK(isp);
35182df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
35192df76c16SMatt Jacob 		goto out;
35202df76c16SMatt Jacob 	}
35212df76c16SMatt Jacob 
35222df76c16SMatt Jacob 	ISP_UNLOCK(isp);
35232df76c16SMatt Jacob 
35242df76c16SMatt Jacob 	ccb = xpt_alloc_ccb();
35252df76c16SMatt Jacob 
35262df76c16SMatt Jacob 	/*
35272df76c16SMatt Jacob 	 * Make sure role is none.
35282df76c16SMatt Jacob 	 */
35292df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
35302df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
35312df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
35322df76c16SMatt Jacob #ifdef	ISP_TEST_WWNS
35332df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
35342df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
35352df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
35362df76c16SMatt Jacob #else
35372df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
35382df76c16SMatt Jacob #endif
35392df76c16SMatt Jacob 
35402df76c16SMatt Jacob 	ISP_LOCK(isp);
35412df76c16SMatt Jacob 	xpt_action(ccb);
35422df76c16SMatt Jacob 	ISP_UNLOCK(isp);
35432df76c16SMatt Jacob 
35442df76c16SMatt Jacob 	/*
35452df76c16SMatt Jacob 	 * Now enable luns
35462df76c16SMatt Jacob 	 */
35472df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
35482df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_EN_LUN;
35492df76c16SMatt Jacob 	ccb->cel.enable = 1;
35502df76c16SMatt Jacob 	ISP_LOCK(isp);
35512df76c16SMatt Jacob 	xpt_action(ccb);
35522df76c16SMatt Jacob 	ISP_UNLOCK(isp);
35532df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
35542df76c16SMatt Jacob 		xpt_free_ccb(ccb);
35552df76c16SMatt Jacob 		xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
35562df76c16SMatt Jacob 		goto out;
35572df76c16SMatt Jacob 	}
35582df76c16SMatt Jacob 
35592df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
35602df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_EN_LUN;
35612df76c16SMatt Jacob 	ccb->cel.enable = 1;
35622df76c16SMatt Jacob 	ISP_LOCK(isp);
35632df76c16SMatt Jacob 	xpt_action(ccb);
35642df76c16SMatt Jacob 	ISP_UNLOCK(isp);
35652df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
35662df76c16SMatt Jacob 		xpt_free_ccb(ccb);
35672df76c16SMatt Jacob 		xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
35682df76c16SMatt Jacob 		goto out;
35692df76c16SMatt Jacob 	}
35702df76c16SMatt Jacob 	xpt_free_ccb(ccb);
35712df76c16SMatt Jacob 
35722df76c16SMatt Jacob 	/*
35732df76c16SMatt Jacob 	 * Add resources
35742df76c16SMatt Jacob 	 */
35752df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
35762df76c16SMatt Jacob 	for (i = 0; i < 4; i++) {
35772df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
35782df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
35792df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
35802df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
35812df76c16SMatt Jacob 		ISP_LOCK(isp);
35822df76c16SMatt Jacob 		xpt_action(ccb);
35832df76c16SMatt Jacob 		ISP_UNLOCK(isp);
35842df76c16SMatt Jacob 	}
35852df76c16SMatt Jacob 	for (i = 0; i < NISP_TARG_CMDS; i++) {
35862df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
35872df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
35882df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
35892df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
35902df76c16SMatt Jacob 		ISP_LOCK(isp);
35912df76c16SMatt Jacob 		xpt_action(ccb);
35922df76c16SMatt Jacob 		ISP_UNLOCK(isp);
35932df76c16SMatt Jacob 	}
35942df76c16SMatt Jacob 	for (i = 0; i < 4; i++) {
35952df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
35962df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
35972df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
35982df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
35992df76c16SMatt Jacob 		ISP_LOCK(isp);
36002df76c16SMatt Jacob 		xpt_action(ccb);
36012df76c16SMatt Jacob 		ISP_UNLOCK(isp);
36022df76c16SMatt Jacob 	}
36032df76c16SMatt Jacob 	for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
36042df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
36052df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
36062df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
36072df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
36082df76c16SMatt Jacob 		ISP_LOCK(isp);
36092df76c16SMatt Jacob 		xpt_action(ccb);
36102df76c16SMatt Jacob 		ISP_UNLOCK(isp);
36112df76c16SMatt Jacob 	}
36122df76c16SMatt Jacob 
36132df76c16SMatt Jacob 	/*
36142df76c16SMatt Jacob 	 * Now turn it all back on
36152df76c16SMatt Jacob 	 */
36162df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
36172df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
36182df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
36192df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
36202df76c16SMatt Jacob 	ISP_LOCK(isp);
36212df76c16SMatt Jacob 	xpt_action(ccb);
36222df76c16SMatt Jacob 	ISP_UNLOCK(isp);
36232df76c16SMatt Jacob 
36242df76c16SMatt Jacob 	/*
36252df76c16SMatt Jacob 	 * Okay, while things are still active, sleep...
36262df76c16SMatt Jacob 	 */
36272df76c16SMatt Jacob 	ISP_LOCK(isp);
36282df76c16SMatt Jacob 	for (;;) {
36292df76c16SMatt Jacob 		ISP_GET_PC(isp, chan, proc_active, i);
36302df76c16SMatt Jacob 		if (i == 0) {
36312df76c16SMatt Jacob 			break;
36322df76c16SMatt Jacob 		}
36332df76c16SMatt Jacob 		msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
36342df76c16SMatt Jacob 	}
36352df76c16SMatt Jacob 	ISP_UNLOCK(isp);
36362df76c16SMatt Jacob 
36372df76c16SMatt Jacob out:
36382df76c16SMatt Jacob 	if (wperiph) {
36392df76c16SMatt Jacob 		cam_periph_invalidate(wperiph);
36402df76c16SMatt Jacob 	}
36412df76c16SMatt Jacob 	if (periph) {
36422df76c16SMatt Jacob 		cam_periph_invalidate(periph);
36432df76c16SMatt Jacob 	}
36442df76c16SMatt Jacob 	if (junk_data) {
36452df76c16SMatt Jacob 		free(junk_data, M_ISPTARG);
36462df76c16SMatt Jacob 	}
36472df76c16SMatt Jacob 	if (disk_data) {
36482df76c16SMatt Jacob 		free(disk_data, M_ISPTARG);
36492df76c16SMatt Jacob 	}
36502df76c16SMatt Jacob 	if (softc) {
36512df76c16SMatt Jacob 		free(softc, M_ISPTARG);
36522df76c16SMatt Jacob 	}
36532df76c16SMatt Jacob 	xpt_free_path(path);
36542df76c16SMatt Jacob 	xpt_free_path(wpath);
36552df76c16SMatt Jacob }
36562df76c16SMatt Jacob 
36572df76c16SMatt Jacob static void
36582df76c16SMatt Jacob isp_target_thread_pi(void *arg)
36592df76c16SMatt Jacob {
36602df76c16SMatt Jacob 	struct isp_spi *pi = arg;
36612df76c16SMatt Jacob 	isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
36622df76c16SMatt Jacob }
36632df76c16SMatt Jacob 
36642df76c16SMatt Jacob static void
36652df76c16SMatt Jacob isp_target_thread_fc(void *arg)
36662df76c16SMatt Jacob {
36672df76c16SMatt Jacob 	struct isp_fc *fc = arg;
36682df76c16SMatt Jacob 	isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
36692df76c16SMatt Jacob }
36702df76c16SMatt Jacob 
36712df76c16SMatt Jacob static int
36722df76c16SMatt Jacob isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
36732df76c16SMatt Jacob {
36742df76c16SMatt Jacob 	uint32_t cnt, curcnt;
36752df76c16SMatt Jacob 	uint64_t lba;
36762df76c16SMatt Jacob 
36772df76c16SMatt Jacob 	switch (cdb[0]) {
36782df76c16SMatt Jacob 	case WRITE_16:
36792df76c16SMatt Jacob 	case READ_16:
36802df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[10]) <<  24) |
36812df76c16SMatt Jacob 			(((uint32_t)cdb[11]) <<  16) |
36822df76c16SMatt Jacob 			(((uint32_t)cdb[12]) <<   8) |
36832df76c16SMatt Jacob 			((uint32_t)cdb[13]);
36842df76c16SMatt Jacob 
36852df76c16SMatt Jacob 		lba =	(((uint64_t)cdb[2]) << 56) |
36862df76c16SMatt Jacob 			(((uint64_t)cdb[3]) << 48) |
36872df76c16SMatt Jacob 			(((uint64_t)cdb[4]) << 40) |
36882df76c16SMatt Jacob 			(((uint64_t)cdb[5]) << 32) |
36892df76c16SMatt Jacob 			(((uint64_t)cdb[6]) << 24) |
36902df76c16SMatt Jacob 			(((uint64_t)cdb[7]) << 16) |
36912df76c16SMatt Jacob 			(((uint64_t)cdb[8]) <<  8) |
36922df76c16SMatt Jacob 			((uint64_t)cdb[9]);
36932df76c16SMatt Jacob 		break;
36942df76c16SMatt Jacob 	case WRITE_12:
36952df76c16SMatt Jacob 	case READ_12:
36962df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[6]) <<  16) |
36972df76c16SMatt Jacob 			(((uint32_t)cdb[7]) <<   8) |
36982df76c16SMatt Jacob 			((u_int32_t)cdb[8]);
36992df76c16SMatt Jacob 
37002df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[2]) << 24) |
37012df76c16SMatt Jacob 			(((uint32_t)cdb[3]) << 16) |
37022df76c16SMatt Jacob 			(((uint32_t)cdb[4]) <<  8) |
37032df76c16SMatt Jacob 			((uint32_t)cdb[5]);
37042df76c16SMatt Jacob 		break;
37052df76c16SMatt Jacob 	case WRITE_10:
37062df76c16SMatt Jacob 	case READ_10:
37072df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[7]) <<  8) |
37082df76c16SMatt Jacob 			((u_int32_t)cdb[8]);
37092df76c16SMatt Jacob 
37102df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[2]) << 24) |
37112df76c16SMatt Jacob 			(((uint32_t)cdb[3]) << 16) |
37122df76c16SMatt Jacob 			(((uint32_t)cdb[4]) <<  8) |
37132df76c16SMatt Jacob 			((uint32_t)cdb[5]);
37142df76c16SMatt Jacob 		break;
37152df76c16SMatt Jacob 	case WRITE_6:
37162df76c16SMatt Jacob 	case READ_6:
37172df76c16SMatt Jacob 		cnt = cdb[4];
37182df76c16SMatt Jacob 		if (cnt == 0) {
37192df76c16SMatt Jacob 			cnt = 256;
37202df76c16SMatt Jacob 		}
37212df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[1] & 0x1f) << 16) |
37222df76c16SMatt Jacob 			(((uint32_t)cdb[2]) << 8) |
37232df76c16SMatt Jacob 			((uint32_t)cdb[3]);
37242df76c16SMatt Jacob 		break;
37252df76c16SMatt Jacob 	default:
37262df76c16SMatt Jacob 		return (-1);
37272df76c16SMatt Jacob 	}
37282df76c16SMatt Jacob 
37292df76c16SMatt Jacob 	cnt <<= DISK_SHIFT;
37302df76c16SMatt Jacob 	lba <<= DISK_SHIFT;
37312df76c16SMatt Jacob 
37322df76c16SMatt Jacob 	if (offset == cnt) {
37332df76c16SMatt Jacob 		*lp = 1;
37342df76c16SMatt Jacob 		return (0);
37352df76c16SMatt Jacob 	}
37362df76c16SMatt Jacob 
37372df76c16SMatt Jacob 	if (lba + cnt > dl) {
37382df76c16SMatt Jacob 		return (-1);
37392df76c16SMatt Jacob 	}
37402df76c16SMatt Jacob 
37412df76c16SMatt Jacob 
37422df76c16SMatt Jacob 	curcnt = MAX_ISP_TARG_TRANSFER;
37432df76c16SMatt Jacob 	if (offset + curcnt >= cnt) {
37442df76c16SMatt Jacob 		curcnt = cnt - offset;
37452df76c16SMatt Jacob 		*lp = 1;
37462df76c16SMatt Jacob 	} else {
37472df76c16SMatt Jacob 		*lp = 0;
37482df76c16SMatt Jacob 	}
37492df76c16SMatt Jacob 	*tl = curcnt;
37502df76c16SMatt Jacob 	*kp = &dp[lba + offset];
37512df76c16SMatt Jacob 	return (0);
37522df76c16SMatt Jacob }
37532df76c16SMatt Jacob 
37542df76c16SMatt Jacob #endif
3755d81ba9d5SMatt Jacob #endif
3756d81ba9d5SMatt Jacob 
3757478f8a96SJustin T. Gibbs static void
37581dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3759478f8a96SJustin T. Gibbs {
3760478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
37619cd7268eSMatt Jacob 	ispsoftc_t *isp;
3762478f8a96SJustin T. Gibbs 
3763478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
37649cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3765478f8a96SJustin T. Gibbs 	switch (code) {
3766478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
3767ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
37681dae40ebSMatt Jacob 			uint16_t oflags, nflags;
37692df76c16SMatt Jacob 			int bus = cam_sim_bus(sim);
37702df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
3771a1bc34c6SMatt Jacob 			int tgt;
3772478f8a96SJustin T. Gibbs 
3773f9e908dcSMatt Jacob 			tgt = xpt_path_target_id(path);
377441ed683eSMatt Jacob 			if (tgt >= 0) {
37759ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
3776a1bc34c6SMatt Jacob #ifndef	ISP_TARGET_MODE
37779ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
3778a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
3779478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3780478f8a96SJustin T. Gibbs 				}
3781a1bc34c6SMatt Jacob #else
3782a1bc34c6SMatt Jacob 				nflags = DPARM_DEFAULT;
3783a1bc34c6SMatt Jacob #endif
37849ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
37859ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
3786478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
37872df76c16SMatt Jacob 				sdp->update = 1;
37882df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
37899ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
3790478f8a96SJustin T. Gibbs 			}
379141ed683eSMatt Jacob 		}
3792478f8a96SJustin T. Gibbs 		break;
3793478f8a96SJustin T. Gibbs 	default:
37943c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3795478f8a96SJustin T. Gibbs 		break;
3796478f8a96SJustin T. Gibbs 	}
3797478f8a96SJustin T. Gibbs }
3798478f8a96SJustin T. Gibbs 
3799478f8a96SJustin T. Gibbs static void
3800c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
3801478f8a96SJustin T. Gibbs {
38029cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
380310365e5aSMatt Jacob 	uint32_t isr;
380410365e5aSMatt Jacob 	uint16_t sema, mbox;
3805126ec864SMatt Jacob 
3806126ec864SMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3807126ec864SMatt Jacob 		isp_intr(isp, isr, sema, mbox);
3808126ec864SMatt Jacob 	}
3809478f8a96SJustin T. Gibbs }
3810478f8a96SJustin T. Gibbs 
3811ab6c4b31SMatt Jacob 
38122df76c16SMatt Jacob static void
38132df76c16SMatt Jacob isp_watchdog(void *arg)
3814cc8df88bSMatt Jacob {
38152df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
38162df76c16SMatt Jacob 	ispsoftc_t *isp;
38171dae40ebSMatt Jacob 	uint32_t handle;
3818b85389e1SMatt Jacob 
38192df76c16SMatt Jacob 	isp = XS_ISP(xs);
38202df76c16SMatt Jacob 
3821cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
3822cc8df88bSMatt Jacob 	if (handle) {
38231fcf5debSMatt Jacob 		/*
38241fcf5debSMatt Jacob 		 * Make sure the command is *really* dead before we
38251fcf5debSMatt Jacob 		 * release the handle (and DMA resources) for reuse.
38261fcf5debSMatt Jacob 		 */
3827f7c631bcSMatt Jacob 		(void) isp_control(isp, ISPCTL_ABORT_CMD, xs);
38281fcf5debSMatt Jacob 
38291fcf5debSMatt Jacob 		/*
38301fcf5debSMatt Jacob 		 * After this point, the comamnd is really dead.
38311fcf5debSMatt Jacob 		 */
3832f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
3833f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
3834f6e75de2SMatt Jacob 		}
3835cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
38362df76c16SMatt Jacob 		xpt_print(xs->ccb_h.path, "watchdog timeout for handle 0x%x\n", handle);
3837cc8df88bSMatt Jacob 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
3838cc8df88bSMatt Jacob 		isp_done(xs);
3839b85389e1SMatt Jacob 	}
3840f7c631bcSMatt Jacob }
3841f7c631bcSMatt Jacob 
3842f7c631bcSMatt Jacob static void
38432df76c16SMatt Jacob isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3844f7c631bcSMatt Jacob {
3845ffcf6651SMatt Jacob 	union ccb *ccb;
38462df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
38472df76c16SMatt Jacob 
38482df76c16SMatt Jacob 	if (isp_autoconfig == 0) {
38492df76c16SMatt Jacob 		return;
38502df76c16SMatt Jacob 	}
38512df76c16SMatt Jacob 
3852ffcf6651SMatt Jacob 	/*
38532df76c16SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this bus/target and schedule a rescan.
3854ffcf6651SMatt Jacob 	 */
38558008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
3856ffcf6651SMatt Jacob 	if (ccb == NULL) {
38572df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3858ffcf6651SMatt Jacob 		return;
3859ffcf6651SMatt Jacob 	}
38602df76c16SMatt Jacob 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3861ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3862ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
3863ffcf6651SMatt Jacob 		return;
3864ffcf6651SMatt Jacob 	}
3865ffcf6651SMatt Jacob 	xpt_rescan(ccb);
3866ffcf6651SMatt Jacob }
3867ffcf6651SMatt Jacob 
3868ffcf6651SMatt Jacob static void
38692df76c16SMatt Jacob isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
3870ffcf6651SMatt Jacob {
3871ffcf6651SMatt Jacob 	struct cam_path *tp;
38722df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
38732df76c16SMatt Jacob 
38742df76c16SMatt Jacob 	if (isp_autoconfig == 0) {
38752df76c16SMatt Jacob 		return;
38762df76c16SMatt Jacob 	}
38772df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3878ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
3879ffcf6651SMatt Jacob 		xpt_free_path(tp);
3880f7c631bcSMatt Jacob 	}
3881f7c631bcSMatt Jacob }
3882f7c631bcSMatt Jacob 
3883f7c631bcSMatt Jacob /*
3884f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
3885f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
3886f7c631bcSMatt Jacob  * gone away.
3887f7c631bcSMatt Jacob  *
3888f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
3889f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
3890f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
3891f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
3892f7c631bcSMatt Jacob  */
3893f7c631bcSMatt Jacob static void
3894f7c631bcSMatt Jacob isp_gdt(void *arg)
3895f7c631bcSMatt Jacob {
38962df76c16SMatt Jacob 	struct isp_fc *fc = arg;
38972df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
38982df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3899f7c631bcSMatt Jacob 	fcportdb_t *lp;
3900f7c631bcSMatt Jacob 	int dbidx, tgt, more_to_do = 0;
3901f7c631bcSMatt Jacob 
39022df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3903f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
39042df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3905f7c631bcSMatt Jacob 
3906f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3907f7c631bcSMatt Jacob 			continue;
3908f7c631bcSMatt Jacob 		}
39092df76c16SMatt Jacob 		if (lp->dev_map_idx == 0 || lp->target_mode) {
3910f7c631bcSMatt Jacob 			continue;
3911f7c631bcSMatt Jacob 		}
3912f7c631bcSMatt Jacob 		if (lp->new_reserved == 0) {
3913f7c631bcSMatt Jacob 			continue;
3914f7c631bcSMatt Jacob 		}
3915f7c631bcSMatt Jacob 		lp->new_reserved -= 1;
3916f7c631bcSMatt Jacob 		if (lp->new_reserved != 0) {
3917f7c631bcSMatt Jacob 			more_to_do++;
3918f7c631bcSMatt Jacob 			continue;
3919f7c631bcSMatt Jacob 		}
39202df76c16SMatt Jacob 		tgt = lp->dev_map_idx - 1;
39212df76c16SMatt Jacob 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
39222df76c16SMatt Jacob 		lp->dev_map_idx = 0;
3923f7c631bcSMatt Jacob 		lp->state = FC_PORTDB_STATE_NIL;
39242df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
39252df76c16SMatt Jacob 		isp_make_gone(isp, chan, tgt);
3926f7c631bcSMatt Jacob 	}
3927f7c631bcSMatt Jacob 	if (more_to_do) {
39282df76c16SMatt Jacob 		fc->gdt_running = 1;
39292df76c16SMatt Jacob 		callout_reset(&fc->gdt, hz, isp_gdt, fc);
3930f7c631bcSMatt Jacob 	} else {
39312df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d stopping Gone Device Timer", chan);
39322df76c16SMatt Jacob 		fc->gdt_running = 0;
3933f7c631bcSMatt Jacob 	}
3934f7c631bcSMatt Jacob }
3935f7c631bcSMatt Jacob 
3936f7c631bcSMatt Jacob /*
3937f7c631bcSMatt Jacob  * Loop Down Timer Function- when loop goes down, a timer is started and
3938f7c631bcSMatt Jacob  * and after it expires we come here and take all probational devices that
3939f7c631bcSMatt Jacob  * the OS knows about and the tell the OS that they've gone away.
3940f7c631bcSMatt Jacob  *
3941f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
3942f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
3943f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3944f7c631bcSMatt Jacob  */
3945f7c631bcSMatt Jacob static void
3946f7c631bcSMatt Jacob isp_ldt(void *arg)
3947f7c631bcSMatt Jacob {
39482df76c16SMatt Jacob 	struct isp_fc *fc = arg;
39492df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
39502df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
3951f7c631bcSMatt Jacob 	fcportdb_t *lp;
3952f7c631bcSMatt Jacob 	int dbidx, tgt;
3953f7c631bcSMatt Jacob 
39542df76c16SMatt Jacob 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
39550a70657fSMatt Jacob 
3956f7c631bcSMatt Jacob 	/*
3957f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
3958f7c631bcSMatt Jacob 	 */
3959f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
39602df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3961f7c631bcSMatt Jacob 
3962f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
3963f7c631bcSMatt Jacob 			continue;
3964f7c631bcSMatt Jacob 		}
39652df76c16SMatt Jacob 		if (lp->dev_map_idx == 0 || lp->target_mode) {
3966f7c631bcSMatt Jacob 			continue;
3967f7c631bcSMatt Jacob 		}
3968f7c631bcSMatt Jacob 
3969f7c631bcSMatt Jacob 		/*
3970f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3971f7c631bcSMatt Jacob 		 */
3972f7c631bcSMatt Jacob 
3973f7c631bcSMatt Jacob 		/*
3974f7c631bcSMatt Jacob 		 * Mark that we've announced that this device is gone....
3975f7c631bcSMatt Jacob 		 */
3976f7c631bcSMatt Jacob 		lp->reserved = 1;
3977f7c631bcSMatt Jacob 
3978f7c631bcSMatt Jacob 		/*
3979f7c631bcSMatt Jacob 		 * but *don't* change the state of the entry. Just clear
3980f7c631bcSMatt Jacob 		 * any target id stuff and announce to CAM that the
3981f7c631bcSMatt Jacob 		 * device is gone. This way any necessary PLOGO stuff
3982f7c631bcSMatt Jacob 		 * will happen when loop comes back up.
3983f7c631bcSMatt Jacob 		 */
3984f7c631bcSMatt Jacob 
39852df76c16SMatt Jacob 		tgt = lp->dev_map_idx - 1;
39862df76c16SMatt Jacob 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
39872df76c16SMatt Jacob 		lp->dev_map_idx = 0;
39882df76c16SMatt Jacob 		lp->state = FC_PORTDB_STATE_NIL;
39892df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
39902df76c16SMatt Jacob 		isp_make_gone(isp, chan, tgt);
3991f7c631bcSMatt Jacob 	}
3992f7c631bcSMatt Jacob 
3993f7c631bcSMatt Jacob 	/*
3994f7c631bcSMatt Jacob 	 * The loop down timer has expired. Wake up the kthread
3995f7c631bcSMatt Jacob 	 * to notice that fact (or make it false).
3996f7c631bcSMatt Jacob 	 */
39972df76c16SMatt Jacob 	fc->loop_dead = 1;
39982df76c16SMatt Jacob 	fc->loop_down_time = fc->loop_down_limit+1;
39992df76c16SMatt Jacob 	wakeup(fc);
4000cc8df88bSMatt Jacob }
4001cc8df88bSMatt Jacob 
40025d571944SMatt Jacob static void
40035d571944SMatt Jacob isp_kthread(void *arg)
40045d571944SMatt Jacob {
40052df76c16SMatt Jacob 	struct isp_fc *fc = arg;
40062df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
40072df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
4008f7c631bcSMatt Jacob 	int slp = 0;
40090a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
40102df76c16SMatt Jacob 
40115d571944SMatt Jacob 	for (;;) {
4012f7c631bcSMatt Jacob 		int wasfrozen, lb, lim;
4013fdeb9f2fSMatt Jacob 
40142df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
40152df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
40162df76c16SMatt Jacob 
40172df76c16SMatt Jacob 		/*
40182df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
40192df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
40202df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
40212df76c16SMatt Jacob 		 * check again.
40222df76c16SMatt Jacob 		 *
40232df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
40242df76c16SMatt Jacob 		 */
40252df76c16SMatt Jacob 		if (lb && (fc->role & ISP_ROLE_INITIATOR)) {
4026f7c631bcSMatt Jacob 			/*
4027f7c631bcSMatt Jacob 			 * Increment loop down time by the last sleep interval
4028f7c631bcSMatt Jacob 			 */
40292df76c16SMatt Jacob 			fc->loop_down_time += slp;
403010365e5aSMatt Jacob 
403110365e5aSMatt Jacob 			if (lb < 0) {
40322df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
403310365e5aSMatt Jacob 			} else {
40342df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4035f44257c2SMatt Jacob 			}
403610365e5aSMatt Jacob 
403710365e5aSMatt Jacob 			/*
403810365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
4039f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
4040f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
4041f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
404210365e5aSMatt Jacob 			 */
40432df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4044f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
404510365e5aSMatt Jacob 			} else {
40462df76c16SMatt Jacob 				lim = fc->loop_down_limit;
4047f7c631bcSMatt Jacob 			}
40482df76c16SMatt Jacob 			if (fc->loop_down_time >= lim) {
40492df76c16SMatt Jacob 				isp_freeze_loopdown(isp, chan, "loop limit hit");
4050f7c631bcSMatt Jacob 				slp = 0;
40512df76c16SMatt Jacob 			} else if (fc->loop_down_time < 10) {
405210365e5aSMatt Jacob 				slp = 1;
40532df76c16SMatt Jacob 			} else if (fc->loop_down_time < 30) {
4054f7c631bcSMatt Jacob 				slp = 5;
40552df76c16SMatt Jacob 			} else if (fc->loop_down_time < 60) {
4056f7c631bcSMatt Jacob 				slp = 10;
40572df76c16SMatt Jacob 			} else if (fc->loop_down_time < 120) {
4058f7c631bcSMatt Jacob 				slp = 20;
4059f7c631bcSMatt Jacob 			} else {
4060f7c631bcSMatt Jacob 				slp = 30;
4061f44257c2SMatt Jacob 			}
406210365e5aSMatt Jacob 
40632df76c16SMatt Jacob 		} else if (lb) {
40642df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
40652df76c16SMatt Jacob 			fc->loop_down_time += slp;
40662df76c16SMatt Jacob 			slp = 60;
406710365e5aSMatt Jacob 		} else {
40682df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
40692df76c16SMatt Jacob 			fc->loop_down_time = 0;
407010365e5aSMatt Jacob 			slp = 0;
40715d571944SMatt Jacob 		}
4072fdeb9f2fSMatt Jacob 
40732df76c16SMatt Jacob 
4074f44257c2SMatt Jacob 		/*
40752df76c16SMatt Jacob 		 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
40762df76c16SMatt Jacob 		 * now so that CAM can start sending us commands.
40772df76c16SMatt Jacob 		 *
40782df76c16SMatt Jacob 		 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
40792df76c16SMatt Jacob 		 * or kill the commands, as appropriate.
4080f44257c2SMatt Jacob 		 */
40812df76c16SMatt Jacob 
40822df76c16SMatt Jacob 		if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
40832df76c16SMatt Jacob 			wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
40842df76c16SMatt Jacob 			fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
40852df76c16SMatt Jacob 			if (wasfrozen && fc->simqfrozen == 0) {
40862df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
40872df76c16SMatt Jacob 				xpt_release_simq(fc->sim, 1);
40885d571944SMatt Jacob 			}
40892df76c16SMatt Jacob 		}
40902df76c16SMatt Jacob 
40912df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
40922df76c16SMatt Jacob 
40932df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
40942df76c16SMatt Jacob 
409510365e5aSMatt Jacob 		/*
409610365e5aSMatt Jacob 		 * If slp is zero, we're waking up for the first time after
409710365e5aSMatt Jacob 		 * things have been okay. In this case, we set a deferral state
409810365e5aSMatt Jacob 		 * for all commands and delay hysteresis seconds before starting
409910365e5aSMatt Jacob 		 * the FC state evaluation. This gives the loop/fabric a chance
410010365e5aSMatt Jacob 		 * to settle.
410110365e5aSMatt Jacob 		 */
41022df76c16SMatt Jacob 		if (slp == 0 && fc->hysteresis) {
41032df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
41042df76c16SMatt Jacob 			(void) msleep(&isp_fabric_hysteresis, &isp->isp_osinfo.lock, PRIBIO, "ispT", (fc->hysteresis * hz));
410510365e5aSMatt Jacob 		}
41065d571944SMatt Jacob 	}
41070a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
41085d571944SMatt Jacob }
41095d571944SMatt Jacob 
4110cc8df88bSMatt Jacob static void
4111c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
4112478f8a96SJustin T. Gibbs {
41130a70657fSMatt Jacob 	int bus, tgt, ts, error, lim;
41149cd7268eSMatt Jacob 	ispsoftc_t *isp;
41154663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
4116478f8a96SJustin T. Gibbs 
4117478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4118478f8a96SJustin T. Gibbs 
41199cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
41202df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
41212df76c16SMatt Jacob 
41222df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
412357c801f5SMatt Jacob 		isp_init(isp);
412457c801f5SMatt Jacob 		if (isp->isp_state != ISP_INITSTATE) {
412557c801f5SMatt Jacob 			/*
412657c801f5SMatt Jacob 			 * Lie. Say it was a selection timeout.
412757c801f5SMatt Jacob 			 */
4128b85389e1SMatt Jacob 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
41290470d791SMatt Jacob 			xpt_freeze_devq(ccb->ccb_h.path, 1);
413057c801f5SMatt Jacob 			xpt_done(ccb);
413157c801f5SMatt Jacob 			return;
413257c801f5SMatt Jacob 		}
413357c801f5SMatt Jacob 		isp->isp_state = ISP_RUNSTATE;
413457c801f5SMatt Jacob 	}
4135b09b0095SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
41360a70657fSMatt Jacob 	ISP_PCMD(ccb) = NULL;
41375d571944SMatt Jacob 
4138478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
4139478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
41402df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
4141478f8a96SJustin T. Gibbs 		/*
4142478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
4143478f8a96SJustin T. Gibbs 		 */
4144478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4145478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4146478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
4147478f8a96SJustin T. Gibbs 				xpt_done(ccb);
4148478f8a96SJustin T. Gibbs 				break;
4149478f8a96SJustin T. Gibbs 			}
4150478f8a96SJustin T. Gibbs 		}
41510470d791SMatt Jacob #ifdef	DIAGNOSTIC
41520470d791SMatt Jacob 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4153dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
4154478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
41550470d791SMatt Jacob 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4156dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
4157478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
4158478f8a96SJustin T. Gibbs 		}
4159478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4160478f8a96SJustin T. Gibbs 			xpt_done(ccb);
4161478f8a96SJustin T. Gibbs 			break;
4162478f8a96SJustin T. Gibbs 		}
41630470d791SMatt Jacob #endif
41640a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
41650a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
41660a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
41670a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
41682df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
41690a70657fSMatt Jacob 			xpt_done(ccb);
41700a70657fSMatt Jacob 			break;
41710a70657fSMatt Jacob 		}
4172b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
41730470d791SMatt Jacob 		switch (error) {
4174478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
4175f7c631bcSMatt Jacob 			XS_CMD_S_CLEAR(ccb);
4176478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
41770a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
41780a70657fSMatt Jacob 				break;
4179d69a5f7dSMatt Jacob 			}
41800a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
41810a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
41820a70657fSMatt Jacob 				ts = 60*1000;
4183cc8df88bSMatt Jacob 			}
41840a70657fSMatt Jacob 			ts = isp_mstohz(ts);
41852df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4186478f8a96SJustin T. Gibbs 			break;
41870470d791SMatt Jacob 		case CMD_RQLATER:
4188f44257c2SMatt Jacob 			/*
41892df76c16SMatt Jacob 			 * We get this result for FC devices if the loop state isn't ready yet
41902df76c16SMatt Jacob 			 * or if the device in question has gone zombie on us.
41912df76c16SMatt Jacob 			 *
41922df76c16SMatt Jacob 			 * If we've never seen Loop UP at all, we requeue this request and wait
41932df76c16SMatt Jacob 			 * for the initial loop up delay to expire.
419410365e5aSMatt Jacob 			 */
41952df76c16SMatt Jacob 			lim = ISP_FC_PC(isp, bus)->loop_down_limit;
41962df76c16SMatt Jacob 			if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
41972df76c16SMatt Jacob 				if (FCPARAM(isp, bus)->loop_seen_once == 0) {
41982df76c16SMatt Jacob 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4199f7c631bcSMatt Jacob 				} else {
42002df76c16SMatt Jacob 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4201f7c631bcSMatt Jacob 				}
42022df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4203f7c631bcSMatt Jacob 				xpt_freeze_devq(ccb->ccb_h.path, 1);
42040a70657fSMatt Jacob 				isp_free_pcmd(isp, ccb);
4205f44257c2SMatt Jacob 				xpt_done(ccb);
4206f44257c2SMatt Jacob 				break;
4207f44257c2SMatt Jacob 			}
42082df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4209f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
42102df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4211f7c631bcSMatt Jacob 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
42120a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
4213478f8a96SJustin T. Gibbs 			xpt_done(ccb);
4214478f8a96SJustin T. Gibbs 			break;
42150470d791SMatt Jacob 		case CMD_EAGAIN:
42160a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
42172df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
42182df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
42192df76c16SMatt Jacob 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4220478f8a96SJustin T. Gibbs 			xpt_done(ccb);
4221478f8a96SJustin T. Gibbs 			break;
42220470d791SMatt Jacob 		case CMD_COMPLETE:
42230470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
42240470d791SMatt Jacob 			break;
42250470d791SMatt Jacob 		default:
42262df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4227b85389e1SMatt Jacob 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
42280a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
42290470d791SMatt Jacob 			xpt_done(ccb);
4230478f8a96SJustin T. Gibbs 		}
4231478f8a96SJustin T. Gibbs 		break;
4232478f8a96SJustin T. Gibbs 
4233d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
42342df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
42352df76c16SMatt Jacob 		if (ccb->cel.enable) {
42362df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
42372df76c16SMatt Jacob 		} else {
42382df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
423967ff51f1SMatt Jacob 		}
424067ff51f1SMatt Jacob 		break;
42412df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
4242d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
4243d81ba9d5SMatt Jacob 	{
42442df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4245d81ba9d5SMatt Jacob 		if (tptr == NULL) {
42462df76c16SMatt Jacob 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
42472df76c16SMatt Jacob 		}
42482df76c16SMatt Jacob 		if (tptr == NULL) {
42492df76c16SMatt Jacob 			const char *str;
42502df76c16SMatt Jacob 			uint32_t tag;
42512df76c16SMatt Jacob 
42522df76c16SMatt Jacob 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
42532df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
42542df76c16SMatt Jacob 				tag = ccb->cin1.seq_id;
42552df76c16SMatt Jacob 			} else {
42562df76c16SMatt Jacob 				tag = ccb->atio.tag_id;
42572df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
42582df76c16SMatt Jacob 			}
42592df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
42602df76c16SMatt Jacob 			dump_tstates(isp, XS_CHANNEL(ccb));
42612df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4262d81ba9d5SMatt Jacob 			break;
4263d81ba9d5SMatt Jacob 		}
4264f48ce188SMatt Jacob 		ccb->ccb_h.sim_priv.entries[0].field = 0;
4265f48ce188SMatt Jacob 		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4266570c7a3fSMatt Jacob 		ccb->ccb_h.flags = 0;
4267570c7a3fSMatt Jacob 
4268d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
42692df76c16SMatt Jacob 			if (ccb->atio.tag_id) {
42702df76c16SMatt Jacob 				atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
42712df76c16SMatt Jacob 				if (atp) {
42722df76c16SMatt Jacob 					isp_put_atpd(isp, tptr, atp);
42732df76c16SMatt Jacob 				}
42742df76c16SMatt Jacob 			}
4275570c7a3fSMatt Jacob 			tptr->atio_count++;
42762df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
42772df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
42782df76c16SMatt Jacob 			    ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
42792df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
42802df76c16SMatt Jacob 			if (ccb->cin1.tag_id) {
42812df76c16SMatt Jacob 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
42822df76c16SMatt Jacob 				if (ntp) {
42832df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
42842df76c16SMatt Jacob 				}
42852df76c16SMatt Jacob 			}
4286746e9c85SMatt Jacob 			tptr->inot_count++;
42872df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
42882df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
42892df76c16SMatt Jacob 			    ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4290d81ba9d5SMatt Jacob 		}
4291d81ba9d5SMatt Jacob 		rls_lun_statep(isp, tptr);
4292d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
4293d81ba9d5SMatt Jacob 		break;
4294d81ba9d5SMatt Jacob 	}
42952df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
4296d81ba9d5SMatt Jacob 	{
42972df76c16SMatt Jacob 		tstate_t *tptr;
42982df76c16SMatt Jacob 		inot_private_data_t *ntp;
42992df76c16SMatt Jacob 
43002df76c16SMatt Jacob 		/*
43012df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
43022df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
43032df76c16SMatt Jacob 		 */
43042df76c16SMatt Jacob 		/*
43052df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
43062df76c16SMatt Jacob 		 */
43072df76c16SMatt 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);
43082df76c16SMatt Jacob 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
43092df76c16SMatt Jacob 		if (ntp == NULL) {
43102df76c16SMatt 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__,
43112df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
43122df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
43132df76c16SMatt Jacob 			xpt_done(ccb);
4314d81ba9d5SMatt Jacob 			break;
4315d81ba9d5SMatt Jacob 		}
43162df76c16SMatt Jacob 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
43172df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
43182df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
43192df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
43202df76c16SMatt Jacob 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
43212df76c16SMatt Jacob 			break;
43222df76c16SMatt Jacob 		}
43232df76c16SMatt Jacob 		isp_put_ntpd(isp, tptr, ntp);
43242df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
43252df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
43262df76c16SMatt 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);
43272df76c16SMatt Jacob 		xpt_done(ccb);
43282df76c16SMatt Jacob 		break;
43292df76c16SMatt Jacob 	}
43302df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
43312df76c16SMatt Jacob 		isp_target_start_ctio(isp, ccb);
43322df76c16SMatt Jacob 		break;
4333d81ba9d5SMatt Jacob #endif
4334478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
4335d81ba9d5SMatt Jacob 
4336d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4337d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
4338d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
4339d81ba9d5SMatt Jacob 
43402df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4341478f8a96SJustin T. Gibbs 		if (error) {
4342478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4343478f8a96SJustin T. Gibbs 		} else {
4344478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
4345478f8a96SJustin T. Gibbs 		}
4346478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4347478f8a96SJustin T. Gibbs 		break;
4348478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
4349d81ba9d5SMatt Jacob 	{
4350d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
4351d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
4352d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4353d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
43542df76c16SMatt Jacob 			isp_target_mark_aborted(isp, accb);
4355d81ba9d5SMatt Jacob 			break;
4356d81ba9d5SMatt Jacob #endif
4357d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
4358478f8a96SJustin T. Gibbs 			error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4359478f8a96SJustin T. Gibbs 			if (error) {
4360d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
4361478f8a96SJustin T. Gibbs 			} else {
4362478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
4363478f8a96SJustin T. Gibbs 			}
4364d81ba9d5SMatt Jacob 			break;
4365d81ba9d5SMatt Jacob 		default:
4366d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
4367d81ba9d5SMatt Jacob 			break;
4368d81ba9d5SMatt Jacob 		}
4369478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4370478f8a96SJustin T. Gibbs 		break;
4371d81ba9d5SMatt Jacob 	}
4372ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
4373478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
4374478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
43759ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
43769ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
43779ce9bdafSMatt Jacob 			xpt_done(ccb);
43789ce9bdafSMatt Jacob 			break;
43799ce9bdafSMatt Jacob 		}
4380478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
4381805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
43822df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
43832df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
43842df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
43852df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
43861dae40ebSMatt Jacob 			uint16_t *dptr;
4387ab163f5fSMatt Jacob 
4388b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
4389b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
4390b61386a4SMatt Jacob 				xpt_done(ccb);
4391b61386a4SMatt Jacob 				break;
4392b61386a4SMatt Jacob 			}
4393b61386a4SMatt Jacob 
4394ab163f5fSMatt Jacob 			/*
43959ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
4396ab163f5fSMatt Jacob 			 * so any request to change settings just gets
4397ab163f5fSMatt Jacob 			 * vectored to that location.
4398ab163f5fSMatt Jacob 			 */
43999ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
4400ab163f5fSMatt Jacob 
4401ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4402ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4403ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
4404ab163f5fSMatt Jacob 				else
4405ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
4406ab163f5fSMatt Jacob 			}
4407ab163f5fSMatt Jacob 
4408ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4409ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4410ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
4411ab163f5fSMatt Jacob 				else
4412ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
4413ab163f5fSMatt Jacob 			}
4414ab163f5fSMatt Jacob 
4415ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4416ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4417ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
4418ab163f5fSMatt Jacob 				else
4419ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
4420ab163f5fSMatt Jacob 			}
4421ab163f5fSMatt Jacob 
4422ab163f5fSMatt Jacob 			/*
4423ab163f5fSMatt Jacob 			 * XXX: FIX ME
4424ab163f5fSMatt Jacob 			 */
44252df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4426ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
44279ce9bdafSMatt Jacob 				/*
44289ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
44299ce9bdafSMatt Jacob 				 */
44302df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
44312df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4432ab163f5fSMatt Jacob 			} else {
4433ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
4434ab163f5fSMatt Jacob 			}
44352df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
44362df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4437478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
44382df76c16SMatt Jacob 			sdp->update = 1;
4439478f8a96SJustin T. Gibbs 		}
4440478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
4441478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4442478f8a96SJustin T. Gibbs 		break;
4443478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
4444478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
4445478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
44462df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4447ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
44482df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
44492df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
44502df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4451478f8a96SJustin T. Gibbs 
4452ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
4453ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
4454ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
4455ab163f5fSMatt Jacob 			cts->transport_version = 0;
4456ab163f5fSMatt Jacob 
44579b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
44589b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4459ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
4460ab163f5fSMatt Jacob 			fc->bitrate = 100000;
4461fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
4462ab163f5fSMatt Jacob 			if (tgt > 0 && tgt < MAX_FC_TARG) {
446310365e5aSMatt Jacob 				fcportdb_t *lp = &fcp->portdb[tgt];
4464ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
4465ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
4466ab163f5fSMatt Jacob 				fc->port = lp->portid;
44672df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4468ab163f5fSMatt Jacob 			}
4469ab163f5fSMatt Jacob 		} else {
44702df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
44712df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
44722df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
44731dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
4474ab163f5fSMatt Jacob 
4475ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
447683ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
44772df76c16SMatt Jacob 				sdp->update = 1;
44782df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
44799ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
44809ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
44819ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
44824394c92fSMatt Jacob 			} else {
44839ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
44849ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
44859ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
44864394c92fSMatt Jacob 			}
4487478f8a96SJustin T. Gibbs 
4488ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
4489ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
4490ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
4491ab163f5fSMatt Jacob 			cts->transport_version = 2;
4492ab163f5fSMatt Jacob 
4493b61386a4SMatt Jacob 			spi->valid = 0;
4494b61386a4SMatt Jacob 			scsi->valid = 0;
4495b61386a4SMatt Jacob 			spi->flags = 0;
4496b61386a4SMatt Jacob 			scsi->flags = 0;
4497ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
4498ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4499ab163f5fSMatt Jacob 			}
45009ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
4501ab163f5fSMatt Jacob 				spi->sync_offset = oval;
4502ab163f5fSMatt Jacob 				spi->sync_period = pval;
4503b61386a4SMatt Jacob 			} else {
4504b61386a4SMatt Jacob 				spi->sync_offset = 0;
4505b61386a4SMatt Jacob 				spi->sync_period = 0;
4506b61386a4SMatt Jacob 			}
4507ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4508ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4509ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4510ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
4511ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4512ab163f5fSMatt Jacob 			} else {
4513ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4514ab163f5fSMatt Jacob 			}
4515ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4516ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
4517b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
4518b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4519b61386a4SMatt Jacob 				}
4520ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
4521ab163f5fSMatt Jacob 			}
45222df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4523b61386a4SMatt Jacob 			    bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4524478f8a96SJustin T. Gibbs 		}
4525478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
4526478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4527478f8a96SJustin T. Gibbs 		break;
4528478f8a96SJustin T. Gibbs 
4529478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
45302df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
45312df76c16SMatt Jacob 		xpt_done(ccb);
45322df76c16SMatt Jacob 		break;
4533478f8a96SJustin T. Gibbs 
4534478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
4535ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
45362df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
45372df76c16SMatt Jacob 		if (error) {
4538478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
45392df76c16SMatt Jacob 			xpt_done(ccb);
45402df76c16SMatt Jacob 			break;
45412df76c16SMatt Jacob 		}
45420a70657fSMatt Jacob 		if (bootverbose) {
45432df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
45440a70657fSMatt Jacob 		}
45452df76c16SMatt Jacob 		if (IS_FC(isp)) {
45462df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
45472df76c16SMatt Jacob 		} else {
45482df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
45492df76c16SMatt Jacob 		}
4550478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
4551478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4552478f8a96SJustin T. Gibbs 		break;
4553478f8a96SJustin T. Gibbs 
4554478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
4555478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
4556478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4557478f8a96SJustin T. Gibbs 		break;
4558478f8a96SJustin T. Gibbs 
45592df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
45602df76c16SMatt Jacob 	{
45612df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
45622df76c16SMatt Jacob 		fcparam *fcp;
45632df76c16SMatt Jacob 
45642df76c16SMatt Jacob 
45652df76c16SMatt Jacob 		if (!IS_FC(isp)) {
45662df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
45672df76c16SMatt Jacob 			xpt_done(ccb);
45682df76c16SMatt Jacob 			break;
45692df76c16SMatt Jacob 		}
45702df76c16SMatt Jacob 
45712df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
45722df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
45732df76c16SMatt Jacob 
45742df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
45752df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
45762df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
45772df76c16SMatt Jacob isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
45782df76c16SMatt Jacob 		}
45792df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
45802df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
45812df76c16SMatt Jacob 			int rchange = 0;
45822df76c16SMatt Jacob 			int newrole = 0;
45832df76c16SMatt Jacob 
45842df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
45852df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
45862df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
45872df76c16SMatt Jacob 					rchange = 1;
45882df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
45892df76c16SMatt Jacob 				}
45902df76c16SMatt Jacob 				break;
45912df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
45922df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
45932df76c16SMatt Jacob 					rchange = 1;
45942df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
45952df76c16SMatt Jacob 				}
45962df76c16SMatt Jacob 				break;
45972df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
45982df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
45992df76c16SMatt Jacob 					rchange = 1;
46002df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
46012df76c16SMatt Jacob 				}
46022df76c16SMatt Jacob 				break;
46032df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
46042df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
46052df76c16SMatt Jacob 					rchange = 1;
46062df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
46072df76c16SMatt Jacob 				}
46082df76c16SMatt Jacob 				break;
46092df76c16SMatt Jacob 			}
46102df76c16SMatt Jacob 			if (rchange) {
46112df76c16SMatt Jacob 				if (isp_fc_change_role(isp, bus, newrole) != 0) {
46122df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
46132df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
46142df76c16SMatt Jacob 				} else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
46152df76c16SMatt Jacob 					isp_enable_deferred_luns(isp, bus);
46162df76c16SMatt Jacob #endif
46172df76c16SMatt Jacob 				}
46182df76c16SMatt Jacob 			}
46192df76c16SMatt Jacob 		}
46202df76c16SMatt Jacob 		xpt_done(ccb);
46212df76c16SMatt Jacob 		break;
46222df76c16SMatt Jacob 	}
46232df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Set SIM knobs */
46242df76c16SMatt Jacob 	{
46252df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
46262df76c16SMatt Jacob 
46272df76c16SMatt Jacob 		if (IS_FC(isp)) {
46282df76c16SMatt Jacob 			fcparam *fcp;
46292df76c16SMatt Jacob 
46302df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
46312df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
46322df76c16SMatt Jacob 
46332df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
46342df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
46352df76c16SMatt Jacob 			switch (fcp->role) {
46362df76c16SMatt Jacob 			case ISP_ROLE_NONE:
46372df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
46382df76c16SMatt Jacob 				break;
46392df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
46402df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
46412df76c16SMatt Jacob 				break;
46422df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
46432df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
46442df76c16SMatt Jacob 				break;
46452df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
46462df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
46472df76c16SMatt Jacob 				break;
46482df76c16SMatt Jacob 			}
46492df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
46502df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
46512df76c16SMatt Jacob 		} else {
46522df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
46532df76c16SMatt Jacob 		}
46542df76c16SMatt Jacob 		xpt_done(ccb);
46552df76c16SMatt Jacob 		break;
46562df76c16SMatt Jacob 	}
4657478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
4658478f8a96SJustin T. Gibbs 	{
4659478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
4660478f8a96SJustin T. Gibbs 
4661478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
4662d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
4663a1bc34c6SMatt Jacob 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4664d81ba9d5SMatt Jacob #else
4665478f8a96SJustin T. Gibbs 		cpi->target_sprt = 0;
4666d81ba9d5SMatt Jacob #endif
4667478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
46680470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
46690470d791SMatt Jacob 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
46700470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
46712df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
46724394c92fSMatt Jacob 		if (IS_FC(isp)) {
46732df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
46742df76c16SMatt Jacob 
46754394c92fSMatt Jacob 			cpi->hba_misc = PIM_NOBUSRESET;
46762df76c16SMatt Jacob 
46770470d791SMatt Jacob 			/*
46780470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
46790470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
46800470d791SMatt Jacob 			 */
46810470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
46820470d791SMatt Jacob 
46839deea857SKenneth D. Merry 			/*
46842df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
46859deea857SKenneth D. Merry 			 */
46862df76c16SMatt Jacob 			if (IS_24XX(isp)) {
46872df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
46882df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
46892df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
46902df76c16SMatt Jacob 			} else {
46912df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
46922df76c16SMatt Jacob 			}
46930470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
4694ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
4695805e1f82SMatt Jacob 			cpi->transport_version = 0;
46962df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
46972df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
46982df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
46992df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4700478f8a96SJustin T. Gibbs 		} else {
47012df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
47020470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
47034394c92fSMatt Jacob 			cpi->hba_misc = 0;
4704ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
47059deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
4706ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
4707805e1f82SMatt Jacob 			cpi->transport_version = 2;
4708478f8a96SJustin T. Gibbs 		}
4709ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
4710ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
4711478f8a96SJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4712478f8a96SJustin T. Gibbs 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4713478f8a96SJustin T. Gibbs 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4714478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
4715478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
4716478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4717478f8a96SJustin T. Gibbs 		break;
4718478f8a96SJustin T. Gibbs 	}
4719478f8a96SJustin T. Gibbs 	default:
4720478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
4721478f8a96SJustin T. Gibbs 		xpt_done(ccb);
4722478f8a96SJustin T. Gibbs 		break;
4723478f8a96SJustin T. Gibbs 	}
4724478f8a96SJustin T. Gibbs }
4725d3a9eb2eSMatt Jacob 
4726d3a9eb2eSMatt Jacob #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4727f7c631bcSMatt Jacob 
4728d3a9eb2eSMatt Jacob void
47292df76c16SMatt Jacob isp_done(XS_T *sccb)
4730d3a9eb2eSMatt Jacob {
47319cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
4732d3a9eb2eSMatt Jacob 
4733d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
4734d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
4735b85389e1SMatt Jacob 
47362df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4737d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
47382df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
473992a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
474092a1e549SMatt Jacob 		} else {
4741d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4742d3a9eb2eSMatt Jacob 		}
474392a1e549SMatt Jacob 	}
4744b85389e1SMatt Jacob 
47450470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4746d3a9eb2eSMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
47472df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
4748d3a9eb2eSMatt Jacob 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4749d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
47500470d791SMatt Jacob 			xpt_freeze_devq(sccb->ccb_h.path, 1);
4751d3a9eb2eSMatt Jacob 		}
4752d3a9eb2eSMatt Jacob 	}
4753b85389e1SMatt Jacob 
47542df76c16SMatt Jacob 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
47552df76c16SMatt Jacob 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4756d3a9eb2eSMatt Jacob 	}
4757b85389e1SMatt Jacob 
4758b85389e1SMatt Jacob 	XS_CMD_S_DONE(sccb);
47590ec60713SMarius Strobl 	callout_stop(&PISP_PCMD(sccb)->wdog);
4760b85389e1SMatt Jacob 	XS_CMD_S_CLEAR(sccb);
47610a70657fSMatt Jacob 	isp_free_pcmd(isp, (union ccb *) sccb);
4762d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
4763d3a9eb2eSMatt Jacob }
4764d3a9eb2eSMatt Jacob 
47652df76c16SMatt Jacob void
47662df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4767cbf57b47SMatt Jacob {
47682df76c16SMatt Jacob 	int bus;
47692df76c16SMatt Jacob 	static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
47702df76c16SMatt Jacob 	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4771f7c631bcSMatt Jacob 	char *msg = NULL;
477210365e5aSMatt Jacob 	target_id_t tgt;
477310365e5aSMatt Jacob 	fcportdb_t *lp;
477410365e5aSMatt Jacob 	struct cam_path *tmppath;
47752df76c16SMatt Jacob 	va_list ap;
477610365e5aSMatt Jacob 
4777cbf57b47SMatt Jacob 	switch (cmd) {
4778cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
47790470d791SMatt Jacob 	{
4780ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
4781ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
4782cbf57b47SMatt Jacob 		int flags, tgt;
47832df76c16SMatt Jacob 		sdparam *sdp;
4784ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
4785cbf57b47SMatt Jacob 
478629f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
4787ab163f5fSMatt Jacob 
47882df76c16SMatt Jacob 		va_start(ap, cmd);
47892df76c16SMatt Jacob 		bus = va_arg(ap, int);
47902df76c16SMatt Jacob 		tgt = va_arg(ap, int);
47912df76c16SMatt Jacob 		va_end(ap);
47922df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
47932df76c16SMatt Jacob 
47942df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
47952df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4796cbf57b47SMatt Jacob 			break;
4797cbf57b47SMatt Jacob 		}
47989ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
4799ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4800ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
4801ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
4802ab163f5fSMatt Jacob 
4803ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
4804ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
4805ab163f5fSMatt Jacob 
4806ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
4807ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
4808ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4809ab163f5fSMatt Jacob 		}
4810ab163f5fSMatt Jacob 
4811cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
4812ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
4813ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4814ab163f5fSMatt Jacob 		}
4815ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4816ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
4817ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4818ab163f5fSMatt Jacob 		} else {
4819ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4820ab163f5fSMatt Jacob 		}
4821ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
4822ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4823ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
48249ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
48259ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4826ab163f5fSMatt Jacob 		}
48272df76c16SMatt 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);
4828ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4829ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4830cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
4831cbf57b47SMatt Jacob 		break;
48320470d791SMatt Jacob 	}
483357c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
48342df76c16SMatt Jacob 	{
48352df76c16SMatt Jacob 		va_start(ap, cmd);
48362df76c16SMatt Jacob 		bus = va_arg(ap, int);
48372df76c16SMatt Jacob 		va_end(ap);
48382df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
48392df76c16SMatt Jacob 		if (IS_FC(isp)) {
48402df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
48412df76c16SMatt Jacob 		} else {
48422df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
484357c801f5SMatt Jacob 		}
484457c801f5SMatt Jacob 		break;
48452df76c16SMatt Jacob 	}
48465d571944SMatt Jacob 	case ISPASYNC_LIP:
4847f7c631bcSMatt Jacob 		if (msg == NULL) {
4848f7c631bcSMatt Jacob 			msg = "LIP Received";
48495d571944SMatt Jacob 		}
4850f7c631bcSMatt Jacob 		/* FALLTHROUGH */
48515d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
4852f7c631bcSMatt Jacob 		if (msg == NULL) {
4853f7c631bcSMatt Jacob 			msg = "LOOP Reset";
48545d571944SMatt Jacob 		}
4855f7c631bcSMatt Jacob 		/* FALLTHROUGH */
485657c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
48572df76c16SMatt Jacob 	{
48582df76c16SMatt Jacob 		struct isp_fc *fc;
4859f7c631bcSMatt Jacob 		if (msg == NULL) {
4860f7c631bcSMatt Jacob 			msg = "LOOP Down";
486157c801f5SMatt Jacob 		}
48622df76c16SMatt Jacob 		va_start(ap, cmd);
48632df76c16SMatt Jacob 		bus = va_arg(ap, int);
48642df76c16SMatt Jacob 		va_end(ap);
48652df76c16SMatt Jacob 
48662df76c16SMatt Jacob 		FCPARAM(isp, bus)->link_active = 1;
48672df76c16SMatt Jacob 
48682df76c16SMatt Jacob 		fc = ISP_FC_PC(isp, bus);
48692df76c16SMatt Jacob 		/*
48702df76c16SMatt Jacob 		 * We don't do any simq freezing if we are only in target mode
48712df76c16SMatt Jacob 		 */
48722df76c16SMatt Jacob 		if (fc->role & ISP_ROLE_INITIATOR) {
48732df76c16SMatt Jacob 			if (fc->path) {
48742df76c16SMatt Jacob 				isp_freeze_loopdown(isp, bus, msg);
4875f7c631bcSMatt Jacob 			}
48762df76c16SMatt Jacob 			if (fc->ldt_running == 0) {
48772df76c16SMatt Jacob 				fc->ldt_running = 1;
48782df76c16SMatt Jacob 				callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
48792df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
4880f7c631bcSMatt Jacob 			}
48812df76c16SMatt Jacob 		}
48822df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
488357c801f5SMatt Jacob 		break;
48842df76c16SMatt Jacob 	}
488557c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
48862df76c16SMatt Jacob 		va_start(ap, cmd);
48872df76c16SMatt Jacob 		bus = va_arg(ap, int);
48882df76c16SMatt Jacob 		va_end(ap);
48895d571944SMatt Jacob 		/*
48905d571944SMatt Jacob 		 * Now we just note that Loop has come up. We don't
48915d571944SMatt Jacob 		 * actually do anything because we're waiting for a
48925d571944SMatt Jacob 		 * Change Notify before activating the FC cleanup
48935d571944SMatt Jacob 		 * thread to look at the state of the loop again.
48945d571944SMatt Jacob 		 */
48952df76c16SMatt Jacob 		FCPARAM(isp, bus)->link_active = 1;
48962df76c16SMatt Jacob 		ISP_FC_PC(isp, bus)->loop_dead = 0;
48972df76c16SMatt Jacob 		ISP_FC_PC(isp, bus)->loop_down_time = 0;
48982df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
489957c801f5SMatt Jacob 		break;
490010365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
49012df76c16SMatt Jacob 		va_start(ap, cmd);
49022df76c16SMatt Jacob 		bus = va_arg(ap, int);
49032df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
49042df76c16SMatt Jacob 		va_end(ap);
4905f7c631bcSMatt Jacob 		lp->reserved = 0;
49062df76c16SMatt Jacob 		if ((ISP_FC_PC(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
49072df76c16SMatt Jacob 			int dbidx = lp - FCPARAM(isp, bus)->portdb;
4908f7c631bcSMatt Jacob 			int i;
490902ab3379SMatt Jacob 
4910f7c631bcSMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
4911f7c631bcSMatt Jacob 				if (i >= FL_ID && i <= SNS_ID) {
4912f7c631bcSMatt Jacob 					continue;
4913f7c631bcSMatt Jacob 				}
49142df76c16SMatt Jacob 				if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
4915f7c631bcSMatt Jacob 					break;
4916f7c631bcSMatt Jacob 				}
4917f7c631bcSMatt Jacob 			}
4918f7c631bcSMatt Jacob 			if (i < MAX_FC_TARG) {
49192df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
49202df76c16SMatt Jacob 				lp->dev_map_idx = i + 1;
4921f7c631bcSMatt Jacob 			} else {
4922f7c631bcSMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "out of target ids");
49232df76c16SMatt Jacob 				isp_dump_portdb(isp, bus);
4924f7c631bcSMatt Jacob 			}
4925f7c631bcSMatt Jacob 		}
49262df76c16SMatt Jacob 		if (lp->dev_map_idx) {
49272df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
49282df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
49292df76c16SMatt Jacob 			isp_make_here(isp, bus, tgt);
493010365e5aSMatt Jacob 		} else {
49312df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
493210365e5aSMatt Jacob 		}
493310365e5aSMatt Jacob 		break;
493410365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
49352df76c16SMatt Jacob 		va_start(ap, cmd);
49362df76c16SMatt Jacob 		bus = va_arg(ap, int);
49372df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
49382df76c16SMatt Jacob 		va_end(ap);
49392df76c16SMatt Jacob 		lp->reserved = 0;
4940f7c631bcSMatt Jacob 		if (isp_change_is_bad) {
4941f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_NIL;
49422df76c16SMatt Jacob 			if (lp->dev_map_idx) {
49432df76c16SMatt Jacob 				tgt = lp->dev_map_idx - 1;
49442df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
49452df76c16SMatt Jacob 				lp->dev_map_idx = 0;
49462df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
49472df76c16SMatt Jacob 				isp_make_gone(isp, bus, tgt);
4948f7c631bcSMatt Jacob 			} else {
49492df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
49502df76c16SMatt Jacob 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4951f7c631bcSMatt Jacob 			}
4952f7c631bcSMatt Jacob 		} else {
4953f7c631bcSMatt Jacob 			lp->portid = lp->new_portid;
4954f7c631bcSMatt Jacob 			lp->roles = lp->new_roles;
49552df76c16SMatt Jacob 			if (lp->dev_map_idx) {
49562df76c16SMatt Jacob 				int t = lp->dev_map_idx - 1;
49572df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
49582df76c16SMatt Jacob 				tgt = lp->dev_map_idx - 1;
49592df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
49602df76c16SMatt Jacob 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
496110365e5aSMatt Jacob 			} else {
49622df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
496310365e5aSMatt Jacob 			}
4964f7c631bcSMatt Jacob 		}
496510365e5aSMatt Jacob 		break;
496610365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
49672df76c16SMatt Jacob 		va_start(ap, cmd);
49682df76c16SMatt Jacob 		bus = va_arg(ap, int);
49692df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
49702df76c16SMatt Jacob 		va_end(ap);
49712df76c16SMatt Jacob 		if (lp->dev_map_idx) {
49722df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
49732df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
49742df76c16SMatt Jacob 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
497510365e5aSMatt Jacob 		} else {
49762df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
49772df76c16SMatt Jacob 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
497810365e5aSMatt Jacob 		}
497910365e5aSMatt Jacob 		break;
498010365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
49812df76c16SMatt Jacob 		va_start(ap, cmd);
49822df76c16SMatt Jacob 		bus = va_arg(ap, int);
49832df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
49842df76c16SMatt Jacob 		va_end(ap);
4985f7c631bcSMatt Jacob 		/*
4986f7c631bcSMatt Jacob 		 * If this has a virtual target and we haven't marked it
4987f7c631bcSMatt Jacob 		 * that we're going to have isp_gdt tell the OS it's gone,
4988f7c631bcSMatt Jacob 		 * set the isp_gdt timer running on it.
4989f7c631bcSMatt Jacob 		 *
4990f7c631bcSMatt Jacob 		 * If it isn't marked that isp_gdt is going to get rid of it,
4991f7c631bcSMatt Jacob 		 * announce that it's gone.
4992f7c631bcSMatt Jacob 		 */
49932df76c16SMatt Jacob 		if (lp->dev_map_idx && lp->reserved == 0) {
4994f7c631bcSMatt Jacob 			lp->reserved = 1;
49952df76c16SMatt Jacob 			lp->new_reserved = ISP_FC_PC(isp, bus)->gone_device_time;
4996f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
49972df76c16SMatt Jacob 			if (ISP_FC_PC(isp, bus)->gdt_running == 0) {
49982df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d starting Gone Device Timer", bus);
49992df76c16SMatt Jacob 				ISP_FC_PC(isp, bus)->gdt_running = 1;
50002df76c16SMatt Jacob 				callout_reset(&ISP_FC_PC(isp, bus)->gdt, hz, isp_gdt, ISP_FC_PC(isp, bus));
5001f7c631bcSMatt Jacob 			}
50022df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
50032df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5004f7c631bcSMatt Jacob 		} else if (lp->reserved == 0) {
50052df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
50064b9d588eSMatt Jacob 		}
500710365e5aSMatt Jacob 		break;
500810365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
500910365e5aSMatt Jacob 	{
501010365e5aSMatt Jacob 		char *msg;
50112df76c16SMatt Jacob 		int evt, nphdl, nlstate, reason;
50122df76c16SMatt Jacob 
50132df76c16SMatt Jacob 		va_start(ap, cmd);
50142df76c16SMatt Jacob 		bus = va_arg(ap, int);
50152df76c16SMatt Jacob 		evt = va_arg(ap, int);
50162df76c16SMatt Jacob 		if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
50172df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
50182df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
50192df76c16SMatt Jacob 			reason = va_arg(ap, int);
502010365e5aSMatt Jacob 		} else {
50212df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
50222df76c16SMatt Jacob 			nlstate = reason = 0;
502310365e5aSMatt Jacob 		}
50242df76c16SMatt Jacob 		va_end(ap);
50252df76c16SMatt Jacob 
50262df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
50272df76c16SMatt Jacob 			msg = "Chan %d Port Database Changed";
50282df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
50292df76c16SMatt Jacob 			msg = "Chan %d Name Server Database Changed";
50302df76c16SMatt Jacob 		} else {
50312df76c16SMatt Jacob 			msg = "Chan %d Other Change Notify";
50322df76c16SMatt Jacob 		}
50332df76c16SMatt Jacob 
5034f7c631bcSMatt Jacob 		/*
5035f7c631bcSMatt Jacob 		 * If the loop down timer is running, cancel it.
5036f7c631bcSMatt Jacob 		 */
50372df76c16SMatt Jacob 		if (ISP_FC_PC(isp, bus)->ldt_running) {
50382df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
50392df76c16SMatt Jacob 			ISP_FC_PC(isp, bus)->ldt_running = 0;
50402df76c16SMatt Jacob 			callout_stop(&ISP_FC_PC(isp, bus)->ldt);
5041f7c631bcSMatt Jacob 		}
50422df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, msg, bus);
50432df76c16SMatt Jacob 		if (ISP_FC_PC(isp, bus)->role & ISP_ROLE_INITIATOR) {
50442df76c16SMatt Jacob 			isp_freeze_loopdown(isp, bus, msg);
50452df76c16SMatt Jacob 		}
50462df76c16SMatt Jacob 		wakeup(ISP_FC_PC(isp, bus));
504757c801f5SMatt Jacob 		break;
504802ab3379SMatt Jacob 	}
5049d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
5050e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
5051d81ba9d5SMatt Jacob 	{
50522df76c16SMatt Jacob 		isp_notify_t *notify;
50532df76c16SMatt Jacob 		va_start(ap, cmd);
50542df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
50552df76c16SMatt Jacob 		va_end(ap);
50562df76c16SMatt Jacob 		switch (notify->nt_ncode) {
50572df76c16SMatt Jacob 		case NT_ABORT_TASK:
50582df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
50592df76c16SMatt Jacob 		case NT_CLEAR_ACA:
50602df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
50612df76c16SMatt Jacob 		case NT_LUN_RESET:
50622df76c16SMatt Jacob 		case NT_TARGET_RESET:
50632df76c16SMatt Jacob 			/*
50642df76c16SMatt Jacob 			 * These are task management functions.
50652df76c16SMatt Jacob 			 */
50662df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
50672df76c16SMatt Jacob 			break;
50682df76c16SMatt Jacob 		case NT_BUS_RESET:
50692df76c16SMatt Jacob 		case NT_LIP_RESET:
50702df76c16SMatt Jacob 		case NT_LINK_UP:
50712df76c16SMatt Jacob 		case NT_LINK_DOWN:
50722df76c16SMatt Jacob 			/*
50732df76c16SMatt Jacob 			 * No action need be taken here.
50742df76c16SMatt Jacob 			 */
50752df76c16SMatt Jacob 			break;
50762df76c16SMatt Jacob 		case NT_HBA_RESET:
50772df76c16SMatt Jacob 			isp_del_all_wwn_entries(isp, ISP_NOCHAN);
50782df76c16SMatt Jacob 			break;
50792df76c16SMatt Jacob 		case NT_LOGOUT:
50802df76c16SMatt Jacob 			/*
50812df76c16SMatt Jacob 			 * This is device arrival/departure notification
50822df76c16SMatt Jacob 			 */
50832df76c16SMatt Jacob 			isp_handle_platform_target_notify_ack(isp, notify);
50842df76c16SMatt Jacob 			break;
50852df76c16SMatt Jacob 		case NT_ARRIVED:
50862df76c16SMatt Jacob 		{
50872df76c16SMatt Jacob 			struct ac_contract ac;
50882df76c16SMatt Jacob 			struct ac_device_changed *fc;
50892df76c16SMatt Jacob 
50902df76c16SMatt Jacob 			ac.contract_number = AC_CONTRACT_DEV_CHG;
50912df76c16SMatt Jacob 			fc = (struct ac_device_changed *) ac.contract_data;
50922df76c16SMatt Jacob 			fc->wwpn = notify->nt_wwn;
50932df76c16SMatt Jacob 			fc->port = notify->nt_sid;
50942df76c16SMatt Jacob 			fc->target = notify->nt_nphdl;
50952df76c16SMatt Jacob 			fc->arrived = 1;
50962df76c16SMatt Jacob 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
50972df76c16SMatt Jacob 			break;
50982df76c16SMatt Jacob 		}
50992df76c16SMatt Jacob 		case NT_DEPARTED:
51002df76c16SMatt Jacob 		{
51012df76c16SMatt Jacob 			struct ac_contract ac;
51022df76c16SMatt Jacob 			struct ac_device_changed *fc;
51032df76c16SMatt Jacob 
51042df76c16SMatt Jacob 			ac.contract_number = AC_CONTRACT_DEV_CHG;
51052df76c16SMatt Jacob 			fc = (struct ac_device_changed *) ac.contract_data;
51062df76c16SMatt Jacob 			fc->wwpn = notify->nt_wwn;
51072df76c16SMatt Jacob 			fc->port = notify->nt_sid;
51082df76c16SMatt Jacob 			fc->target = notify->nt_nphdl;
51092df76c16SMatt Jacob 			fc->arrived = 0;
51102df76c16SMatt Jacob 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
51112df76c16SMatt Jacob 			break;
51122df76c16SMatt Jacob 		}
51132df76c16SMatt Jacob 		default:
51142df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
51152df76c16SMatt Jacob 			isp_handle_platform_target_notify_ack(isp, notify);
51162df76c16SMatt Jacob 			break;
51172df76c16SMatt Jacob 		}
5118d81ba9d5SMatt Jacob 		break;
5119d81ba9d5SMatt Jacob 	}
5120d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
51212df76c16SMatt Jacob 	{
51222df76c16SMatt Jacob 		isphdr_t *hp;
51232df76c16SMatt Jacob 
51242df76c16SMatt Jacob 		va_start(ap, cmd);
51252df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
51262df76c16SMatt Jacob 		va_end(ap);
51272df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
5128cbf57b47SMatt Jacob 		default:
51292df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5130d81ba9d5SMatt Jacob 			break;
5131570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
5132570c7a3fSMatt Jacob 			if (IS_SCSI(isp)) {
51332df76c16SMatt Jacob 				isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
51342df76c16SMatt Jacob 			} else if (IS_24XX(isp)) {
51352df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5136570c7a3fSMatt Jacob 			} else {
51372df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5138570c7a3fSMatt Jacob 			}
5139570c7a3fSMatt Jacob 			break;
5140d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
51412df76c16SMatt Jacob 			if (IS_24XX(isp)) {
51422df76c16SMatt Jacob 				isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
51432df76c16SMatt Jacob 			} else {
51442df76c16SMatt Jacob 				isp_handle_platform_atio(isp, (at_entry_t *) hp);
51452df76c16SMatt Jacob 			}
5146d81ba9d5SMatt Jacob 			break;
5147d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
51482df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5149d81ba9d5SMatt Jacob 			break;
51502df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
5151d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
5152d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
5153d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
51542df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
5155d81ba9d5SMatt Jacob 			break;
51562df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
51572df76c16SMatt Jacob 		{
51582df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
51592df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
51602df76c16SMatt Jacob 			tstate_t *tptr;
51612df76c16SMatt Jacob 			fcportdb_t *lp;
51622df76c16SMatt Jacob 			uint16_t chan;
51632df76c16SMatt Jacob 			uint32_t sid, did;
51642df76c16SMatt Jacob 
51652df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
51662df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
51672df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
51682df76c16SMatt Jacob 
51692df76c16SMatt Jacob 			nt->nt_hba = isp;
51702df76c16SMatt Jacob 			nt->nt_did = did;
51712df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
51722df76c16SMatt Jacob 			nt->nt_sid = sid;
51732df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
51742df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
51752df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
51762df76c16SMatt Jacob 			} else {
51772df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
51782df76c16SMatt Jacob 				if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
51792df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
51802df76c16SMatt Jacob 				} else {
51812df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
51822df76c16SMatt Jacob 				}
51832df76c16SMatt Jacob 			}
51842df76c16SMatt Jacob 			/*
51852df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
51862df76c16SMatt Jacob 			 */
51872df76c16SMatt Jacob 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
51882df76c16SMatt Jacob 			if (tptr) {
51892df76c16SMatt Jacob 				nt->nt_lun = xpt_path_lun_id(tptr->owner);
51902df76c16SMatt Jacob 				rls_lun_statep(isp, tptr);
51912df76c16SMatt Jacob 			} else {
51922df76c16SMatt Jacob 				nt->nt_lun = LUN_ANY;
51932df76c16SMatt Jacob 			}
51942df76c16SMatt Jacob 			nt->nt_need_ack = 1;
51952df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
51962df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
51972df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
51982df76c16SMatt 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)",
51992df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
52002df76c16SMatt Jacob 			} else {
52012df76c16SMatt 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)",
52022df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
52032df76c16SMatt Jacob 			}
52042df76c16SMatt Jacob 			nt->nt_channel = chan;
52052df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
52062df76c16SMatt Jacob 			nt->nt_lreserved = hp;
52072df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
52082df76c16SMatt Jacob 			break;
52092df76c16SMatt Jacob 		}
5210d81ba9d5SMatt Jacob 		case RQSTYPE_ENABLE_LUN:
5211d81ba9d5SMatt Jacob 		case RQSTYPE_MODIFY_LUN:
52122df76c16SMatt Jacob 			isp_ledone(isp, (lun_entry_t *) hp);
5213d81ba9d5SMatt Jacob 			break;
5214d81ba9d5SMatt Jacob 		}
5215d81ba9d5SMatt Jacob 		break;
52162df76c16SMatt Jacob 	}
5217d81ba9d5SMatt Jacob #endif
5218ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
5219ab163f5fSMatt Jacob 	{
52201dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
5221ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
5222ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
5223ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
5224ab163f5fSMatt Jacob 		} else {
5225ab163f5fSMatt Jacob 			mbox6 = 0;
5226ab163f5fSMatt Jacob 		}
52272df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
52280a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
52290a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
52302df76c16SMatt Jacob 		isp_reinit(isp, 1);
52310a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
52320a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5233ab163f5fSMatt Jacob 		break;
5234ab163f5fSMatt Jacob 	}
5235d81ba9d5SMatt Jacob 	default:
5236b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5237cbf57b47SMatt Jacob 		break;
5238cbf57b47SMatt Jacob 	}
5239cbf57b47SMatt Jacob }
5240cbf57b47SMatt Jacob 
524192718a7fSMatt Jacob 
524292718a7fSMatt Jacob /*
524392718a7fSMatt Jacob  * Locks are held before coming here.
524492718a7fSMatt Jacob  */
524592718a7fSMatt Jacob void
52469cd7268eSMatt Jacob isp_uninit(ispsoftc_t *isp)
524792718a7fSMatt Jacob {
524810365e5aSMatt Jacob 	if (IS_24XX(isp)) {
524910365e5aSMatt Jacob 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
525010365e5aSMatt Jacob 	} else {
5251ea6f23cdSMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
525210365e5aSMatt Jacob 	}
525310365e5aSMatt Jacob 	ISP_DISABLE_INTS(isp);
525492718a7fSMatt Jacob }
5255b09b0095SMatt Jacob 
52562df76c16SMatt Jacob /*
52572df76c16SMatt Jacob  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
52582df76c16SMatt Jacob  * up from our platform default (defww{p|n}n) and morph them based upon
52592df76c16SMatt Jacob  * channel.
52602df76c16SMatt Jacob  *
52612df76c16SMatt Jacob  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
52622df76c16SMatt Jacob  * based upon channel.
52632df76c16SMatt Jacob  */
52642df76c16SMatt Jacob 
52652df76c16SMatt Jacob uint64_t
52662df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
52672df76c16SMatt Jacob {
52682df76c16SMatt Jacob 	uint64_t seed;
52692df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
52702df76c16SMatt Jacob 
52712df76c16SMatt Jacob 	/*
52722df76c16SMatt Jacob 	 * If we're asking for a active WWN, the default overrides get
52732df76c16SMatt Jacob 	 * returned, otherwise the NVRAM value is picked.
52742df76c16SMatt Jacob 	 *
52752df76c16SMatt Jacob 	 * If we're asking for a default WWN, we just pick the default override.
52762df76c16SMatt Jacob 	 */
52772df76c16SMatt Jacob 	if (isactive) {
52782df76c16SMatt Jacob 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
52792df76c16SMatt Jacob 		if (seed) {
52802df76c16SMatt Jacob 			return (seed);
52812df76c16SMatt Jacob 		}
52822df76c16SMatt Jacob 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
52835cc3786cSMatt Jacob 		if (seed) {
52845cc3786cSMatt Jacob 			return (seed);
52855cc3786cSMatt Jacob 		}
52865cc3786cSMatt Jacob 		return (0x400000007F000009ull);
52872df76c16SMatt Jacob 	} else {
52882df76c16SMatt Jacob 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
52892df76c16SMatt Jacob 	}
52902df76c16SMatt Jacob 
52912df76c16SMatt Jacob 
52922df76c16SMatt Jacob 	/*
52932df76c16SMatt Jacob 	 * For channel zero just return what we have. For either ACIIVE or
52942df76c16SMatt Jacob 	 * DEFAULT cases, we depend on default override of NVRAM values for
52952df76c16SMatt Jacob 	 * channel zero.
52962df76c16SMatt Jacob 	 */
52972df76c16SMatt Jacob 	if (chan == 0) {
52982df76c16SMatt Jacob 		return (seed);
52992df76c16SMatt Jacob 	}
53002df76c16SMatt Jacob 
53012df76c16SMatt Jacob 	/*
53022df76c16SMatt Jacob 	 * For other channels, we are doing one of three things:
53032df76c16SMatt Jacob 	 *
53042df76c16SMatt Jacob 	 * 1. If what we have now is non-zero, return it. Otherwise we morph
53052df76c16SMatt Jacob 	 * values from channel 0. 2. If we're here for a WWPN we synthesize
53062df76c16SMatt Jacob 	 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
53072df76c16SMatt Jacob 	 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
53082df76c16SMatt Jacob 	 */
53092df76c16SMatt Jacob 
53102df76c16SMatt Jacob 	if (seed) {
53112df76c16SMatt Jacob 		return (seed);
53122df76c16SMatt Jacob 	}
53132df76c16SMatt Jacob 	if (isactive) {
53142df76c16SMatt Jacob 		seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
53152df76c16SMatt Jacob 	} else {
53162df76c16SMatt Jacob 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
53172df76c16SMatt Jacob 	}
53182df76c16SMatt Jacob 
53192df76c16SMatt Jacob 	if (((seed >> 60) & 0xf) == 2) {
53202df76c16SMatt Jacob 		/*
53212df76c16SMatt Jacob 		 * The type 2 NAA fields for QLogic cards appear be laid out
53222df76c16SMatt Jacob 		 * thusly:
53232df76c16SMatt Jacob 		 *
53242df76c16SMatt Jacob 		 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
53252df76c16SMatt Jacob 		 * port (1) or node (0) WWN distinguishor bit 48
53262df76c16SMatt Jacob 		 * physical port on dual-port chips (23XX/24XX)
53272df76c16SMatt Jacob 		 *
53282df76c16SMatt Jacob 		 * This is somewhat nutty, particularly since bit 48 is
53292df76c16SMatt Jacob 		 * irrelevant as they assign seperate serial numbers to
53302df76c16SMatt Jacob 		 * different physical ports anyway.
53312df76c16SMatt Jacob 		 *
53322df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
53332df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
53342df76c16SMatt Jacob 		 * of channel which is comfortably more than our maximum
53352df76c16SMatt Jacob 		 * (126) now.
53362df76c16SMatt Jacob 		 */
53372df76c16SMatt Jacob 		seed &= ~0x0FF0000000000000ULL;
53382df76c16SMatt Jacob 		if (iswwnn == 0) {
53392df76c16SMatt Jacob 			seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
53402df76c16SMatt Jacob 			seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
53412df76c16SMatt Jacob 		}
53422df76c16SMatt Jacob 	} else {
53432df76c16SMatt Jacob 		seed = 0;
53442df76c16SMatt Jacob 	}
53452df76c16SMatt Jacob 	return (seed);
53462df76c16SMatt Jacob }
53472df76c16SMatt Jacob 
5348b09b0095SMatt Jacob void
53499cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5350b09b0095SMatt Jacob {
5351b09b0095SMatt Jacob 	va_list ap;
5352b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5353b09b0095SMatt Jacob 		return;
5354b09b0095SMatt Jacob 	}
53553c75bb14SMatt Jacob 	printf("%s: ", device_get_nameunit(isp->isp_dev));
5356b09b0095SMatt Jacob 	va_start(ap, fmt);
5357b09b0095SMatt Jacob 	vprintf(fmt, ap);
5358b09b0095SMatt Jacob 	va_end(ap);
5359b09b0095SMatt Jacob 	printf("\n");
5360b09b0095SMatt Jacob }
5361f7c631bcSMatt Jacob 
5362f7c631bcSMatt Jacob uint64_t
5363f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
5364f7c631bcSMatt Jacob {
5365f7c631bcSMatt Jacob 	uint64_t elapsed;
5366f7c631bcSMatt Jacob 	struct timespec x = *b;
5367f7c631bcSMatt Jacob 	timespecsub(&x, a);
5368f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
5369f7c631bcSMatt Jacob 	if (elapsed == 0)
5370f7c631bcSMatt Jacob 		elapsed++;
5371f7c631bcSMatt Jacob 	return (elapsed);
5372f7c631bcSMatt Jacob }
5373f7c631bcSMatt Jacob 
5374f7c631bcSMatt Jacob int
5375f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
5376f7c631bcSMatt Jacob {
5377f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
5378f7c631bcSMatt Jacob 		return (1);
5379f7c631bcSMatt Jacob 	} else {
5380f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
5381f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
5382f7c631bcSMatt Jacob 		return (0);
5383f7c631bcSMatt Jacob 	}
5384f7c631bcSMatt Jacob }
5385f7c631bcSMatt Jacob 
5386f7c631bcSMatt Jacob void
5387f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5388f7c631bcSMatt Jacob {
5389a4f3a2beSMatt Jacob 	unsigned int usecs = mbp->timeout;
5390a4f3a2beSMatt Jacob 	unsigned int max, olim, ilim;
5391f7c631bcSMatt Jacob 
5392f7c631bcSMatt Jacob 	if (usecs == 0) {
5393f7c631bcSMatt Jacob 		usecs = MBCMD_DEFAULT_TIMEOUT;
5394f7c631bcSMatt Jacob 	}
5395a4f3a2beSMatt Jacob 	max = isp->isp_mbxwrk0 + 1;
5396a4f3a2beSMatt Jacob 
5397f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
5398a4f3a2beSMatt Jacob 		unsigned int ms = (usecs + 999) / 1000;
5399a4f3a2beSMatt Jacob 
5400f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
5401f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
5402a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
54032df76c16SMatt Jacob 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5404a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
5405a4f3a2beSMatt Jacob 				break;
5406a4f3a2beSMatt Jacob 			}
5407a4f3a2beSMatt Jacob 		}
5408f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
5409f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
5410f7c631bcSMatt Jacob 	} else {
5411a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
5412a4f3a2beSMatt Jacob 			for (ilim = 0; ilim < usecs; ilim += 100) {
5413f7c631bcSMatt Jacob 				uint32_t isr;
5414f7c631bcSMatt Jacob 				uint16_t sema, mbox;
5415f7c631bcSMatt Jacob 				if (isp->isp_osinfo.mboxcmd_done) {
5416f7c631bcSMatt Jacob 					break;
5417f7c631bcSMatt Jacob 				}
5418f7c631bcSMatt Jacob 				if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5419f7c631bcSMatt Jacob 					isp_intr(isp, isr, sema, mbox);
5420f7c631bcSMatt Jacob 					if (isp->isp_osinfo.mboxcmd_done) {
5421f7c631bcSMatt Jacob 						break;
5422f7c631bcSMatt Jacob 					}
5423f7c631bcSMatt Jacob 				}
54242df76c16SMatt Jacob 				ISP_DELAY(100);
5425f7c631bcSMatt Jacob 			}
5426a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
5427a4f3a2beSMatt Jacob 				break;
5428a4f3a2beSMatt Jacob 			}
5429a4f3a2beSMatt Jacob 		}
5430f7c631bcSMatt Jacob 	}
5431f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
54322df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
54332df76c16SMatt Jacob 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5434f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
5435f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
5436f7c631bcSMatt Jacob 	}
5437f7c631bcSMatt Jacob }
5438f7c631bcSMatt Jacob 
5439f7c631bcSMatt Jacob void
5440f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
5441f7c631bcSMatt Jacob {
5442f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleeping) {
5443f7c631bcSMatt Jacob 		wakeup(&isp->isp_mbxworkp);
5444f7c631bcSMatt Jacob 	}
5445f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
5446f7c631bcSMatt Jacob }
5447f7c631bcSMatt Jacob 
5448f7c631bcSMatt Jacob void
5449f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
5450f7c631bcSMatt Jacob {
5451f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
5452f7c631bcSMatt Jacob }
5453f7c631bcSMatt Jacob 
5454f7c631bcSMatt Jacob int
54552df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
54562df76c16SMatt Jacob {
54572df76c16SMatt Jacob 	int ret = 0;
54582df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
54592df76c16SMatt Jacob 		ret = -1;
54602df76c16SMatt Jacob 	} else {
54612df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
54622df76c16SMatt Jacob 	}
54632df76c16SMatt Jacob 	return (ret);
54642df76c16SMatt Jacob }
54652df76c16SMatt Jacob 
54662df76c16SMatt Jacob int
5467f7c631bcSMatt Jacob isp_mstohz(int ms)
5468f7c631bcSMatt Jacob {
5469a4f3a2beSMatt Jacob 	int hz;
5470f7c631bcSMatt Jacob 	struct timeval t;
5471f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
5472f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
5473a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
5474a4f3a2beSMatt Jacob 	if (hz < 0) {
5475a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
5476f7c631bcSMatt Jacob 	}
5477a4f3a2beSMatt Jacob 	if (hz == 0) {
5478a4f3a2beSMatt Jacob 		hz = 1;
5479a4f3a2beSMatt Jacob 	}
5480a4f3a2beSMatt Jacob 	return (hz);
5481f7c631bcSMatt Jacob }
54820a70657fSMatt Jacob 
54830a70657fSMatt Jacob void
54840a70657fSMatt Jacob isp_platform_intr(void *arg)
54850a70657fSMatt Jacob {
54860a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
54870a70657fSMatt Jacob 	uint32_t isr;
54880a70657fSMatt Jacob 	uint16_t sema, mbox;
54890a70657fSMatt Jacob 
54900a70657fSMatt Jacob 	ISP_LOCK(isp);
54910a70657fSMatt Jacob 	isp->isp_intcnt++;
54920a70657fSMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
54930a70657fSMatt Jacob 		isp->isp_intbogus++;
54940a70657fSMatt Jacob 	} else {
54950a70657fSMatt Jacob 		isp_intr(isp, isr, sema, mbox);
54960a70657fSMatt Jacob 	}
54970a70657fSMatt Jacob 	ISP_UNLOCK(isp);
54980a70657fSMatt Jacob }
54990a70657fSMatt Jacob 
55000a70657fSMatt Jacob void
55010a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
55020a70657fSMatt Jacob {
55030a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
55042df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
55050a70657fSMatt Jacob 	} else {
55062df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
55070a70657fSMatt Jacob 	}
55080a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
55090a70657fSMatt Jacob }
55102df76c16SMatt Jacob 
55112df76c16SMatt Jacob void
55122df76c16SMatt Jacob isp_timer(void *arg)
55132df76c16SMatt Jacob {
55142df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
55152df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
55162df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
55172df76c16SMatt Jacob #endif
55182df76c16SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
55192df76c16SMatt Jacob }
5520