xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 5b14cb4136509a17c4d67778293456ab1faa88e9)
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$");
32*5b14cb41SWill Andrews 
336054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
345d571944SMatt Jacob #include <sys/unistd.h>
355d571944SMatt Jacob #include <sys/kthread.h>
365d571944SMatt Jacob #include <sys/conf.h>
374eb49427SMatt Jacob #include <sys/module.h>
385d571944SMatt Jacob #include <sys/ioccom.h>
395d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4070273f90SMatt Jacob #include <sys/devicestat.h>
41f7c631bcSMatt Jacob #include <cam/cam_periph.h>
426c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
436054c3f6SMatt Jacob 
442df76c16SMatt Jacob #if	__FreeBSD_version < 800002
452df76c16SMatt Jacob #define	THREAD_CREATE	kthread_create
462df76c16SMatt Jacob #else
472df76c16SMatt Jacob #define	THREAD_CREATE	kproc_create
48805e1f82SMatt Jacob #endif
49805e1f82SMatt Jacob 
504eb49427SMatt Jacob MODULE_VERSION(isp, 1);
5156c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
5273030e03SMatt Jacob int isp_announced = 0;
53427fa8f9SMatt Jacob int isp_fabric_hysteresis = 5;
542df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
55f7c631bcSMatt Jacob int isp_change_is_bad = 0;	/* "changed" devices are bad */
562df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
57f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
582df76c16SMatt Jacob int isp_autoconfig = 1;		/* automatically attach/detach devices */
592df76c16SMatt Jacob static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
6073030e03SMatt Jacob 
612df76c16SMatt Jacob static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
625d571944SMatt Jacob static d_ioctl_t ispioctl;
63f6e75de2SMatt Jacob static void isp_intr_enable(void *);
641dae40ebSMatt Jacob static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
650470d791SMatt Jacob static void isp_poll(struct cam_sim *);
66b85389e1SMatt Jacob static timeout_t isp_watchdog;
67de461933SMatt Jacob static timeout_t isp_gdt;
68de461933SMatt Jacob static task_fn_t isp_gdt_task;
69f7c631bcSMatt Jacob static timeout_t isp_ldt;
70de461933SMatt Jacob static task_fn_t isp_ldt_task;
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
7794dff771SMatt Jacob static int isp_timer_count;
782df76c16SMatt Jacob static void isp_timer(void *);
79cc8df88bSMatt Jacob 
805d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
81dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
827ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
837ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
845d571944SMatt Jacob };
855d571944SMatt Jacob 
862df76c16SMatt Jacob static int
872df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
88478f8a96SJustin T. Gibbs {
89478f8a96SJustin T. Gibbs 	struct ccb_setasync csa;
90ea6f23cdSMatt Jacob 	struct cam_sim *sim;
91ea6f23cdSMatt Jacob 	struct cam_path *path;
92478f8a96SJustin T. Gibbs 
93478f8a96SJustin T. Gibbs 	/*
942df76c16SMatt Jacob 	 * Construct our SIM entry.
95ea6f23cdSMatt Jacob 	 */
962df76c16SMatt 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);
97ea6f23cdSMatt Jacob 
982df76c16SMatt Jacob 	if (sim == NULL) {
992df76c16SMatt Jacob 		return (ENOMEM);
1002df76c16SMatt Jacob 	}
1012df76c16SMatt Jacob 
1022df76c16SMatt Jacob 	ISP_LOCK(isp);
1032df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1042df76c16SMatt Jacob 		ISP_UNLOCK(isp);
1052df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1062df76c16SMatt Jacob 		return (EIO);
1072df76c16SMatt Jacob 	}
1082df76c16SMatt Jacob 	ISP_UNLOCK(isp);
109227d67aaSAlexander Motin 	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 	xpt_setup_ccb(&csa.ccb_h, path, 5);
1172df76c16SMatt Jacob 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1182df76c16SMatt Jacob 	csa.event_enable = AC_LOST_DEVICE;
1192df76c16SMatt Jacob 	csa.callback = isp_cam_async;
1202df76c16SMatt Jacob 	csa.callback_arg = sim;
121387d8239SMatt Jacob 
122387d8239SMatt Jacob 	ISP_LOCK(isp);
1232df76c16SMatt Jacob 	xpt_action((union ccb *)&csa);
124387d8239SMatt Jacob 	ISP_UNLOCK(isp);
1252df76c16SMatt Jacob 
1262df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
1272df76c16SMatt Jacob 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
1282df76c16SMatt Jacob 		spi->sim = sim;
1292df76c16SMatt Jacob 		spi->path = path;
1302df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
1312df76c16SMatt Jacob 		ISP_SET_PC(isp, chan, proc_active, 1);
1322df76c16SMatt 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)) {
1332df76c16SMatt Jacob 			ISP_SET_PC(isp, chan, proc_active, 0);
1342df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
1352df76c16SMatt Jacob 		}
1361d0a1de2SWill Andrews 		ISP_SPI_PC(isp, chan)->num_threads += 1;
1372df76c16SMatt Jacob #endif
1382df76c16SMatt Jacob 	} else {
139a01f5aebSMatt Jacob 		fcparam *fcp = FCPARAM(isp, chan);
1402df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
1412df76c16SMatt Jacob 
142a01f5aebSMatt Jacob 		ISP_LOCK(isp);
1432df76c16SMatt Jacob 		fc->sim = sim;
1442df76c16SMatt Jacob 		fc->path = path;
1452df76c16SMatt Jacob 		fc->isp = isp;
146a01f5aebSMatt Jacob 		fc->ready = 1;
147de461933SMatt Jacob 
1482df76c16SMatt Jacob 		callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
1492df76c16SMatt Jacob 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
150de461933SMatt Jacob 		TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
151de461933SMatt Jacob 		TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
152427fa8f9SMatt Jacob 
1532df76c16SMatt Jacob 		/*
1542df76c16SMatt Jacob 		 * We start by being "loop down" if we have an initiator role
1552df76c16SMatt Jacob 		 */
156a01f5aebSMatt Jacob 		if (fcp->role & ISP_ROLE_INITIATOR) {
1572df76c16SMatt Jacob 			isp_freeze_loopdown(isp, chan, "isp_attach");
1582df76c16SMatt Jacob 			callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
159387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
1602df76c16SMatt Jacob 		}
1612df76c16SMatt Jacob 		ISP_UNLOCK(isp);
162a01f5aebSMatt Jacob 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
163a01f5aebSMatt Jacob 			xpt_free_path(fc->path);
164a01f5aebSMatt Jacob 			ISP_LOCK(isp);
165387d8239SMatt Jacob 			if (callout_active(&fc->ldt))
166a01f5aebSMatt Jacob 				callout_stop(&fc->ldt);
167a01f5aebSMatt Jacob 			xpt_bus_deregister(cam_sim_path(fc->sim));
168a01f5aebSMatt Jacob 			ISP_UNLOCK(isp);
169a01f5aebSMatt Jacob 			cam_sim_free(fc->sim, FALSE);
170a01f5aebSMatt Jacob 			return (ENOMEM);
171a01f5aebSMatt Jacob 		}
1721d0a1de2SWill Andrews 		ISP_FC_PC(isp, chan)->num_threads += 1;
1732df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
1742df76c16SMatt Jacob 		ISP_SET_PC(isp, chan, proc_active, 1);
1752df76c16SMatt 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)) {
1762df76c16SMatt Jacob 			ISP_SET_PC(isp, chan, proc_active, 0);
1772df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
1782df76c16SMatt Jacob 		}
1791d0a1de2SWill Andrews 		ISP_FC_PC(isp, chan)->num_threads += 1;
1802df76c16SMatt Jacob #endif
181a0ec8e99SMatt Jacob 		if (chan == 0) {
182a0ec8e99SMatt Jacob 			struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
183a0ec8e99SMatt Jacob 			struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
184a0ec8e99SMatt Jacob 			SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwnn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwnn, "World Wide Node Name");
185a0ec8e99SMatt Jacob 			SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, "World Wide Port Name");
186a0ec8e99SMatt Jacob 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "loop_down_limit", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->loop_down_limit, 0, "Loop Down Limit");
187a0ec8e99SMatt Jacob 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "gone_device_time", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->gone_device_time, 0, "Gone Device Time");
188387d8239SMatt Jacob #if defined(ISP_TARGET_MODE) && defined(DEBUG)
189387d8239SMatt Jacob 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "inject_lost_data_frame", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->inject_lost_data_frame, 0, "Cause a Lost Frame on a Read");
190387d8239SMatt Jacob #endif
191a0ec8e99SMatt Jacob 		}
1922df76c16SMatt Jacob 	}
1932df76c16SMatt Jacob 	return (0);
1942df76c16SMatt Jacob }
1952df76c16SMatt Jacob 
1961d0a1de2SWill Andrews static void
1971d0a1de2SWill Andrews isp_detach_internal_target(ispsoftc_t *isp, int chan)
1981d0a1de2SWill Andrews {
1991d0a1de2SWill Andrews #ifdef ISP_INTERNAL_TARGET
2001d0a1de2SWill Andrews 	void *wchan;
2011d0a1de2SWill Andrews 
2021d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, target_proc, wchan);
2031d0a1de2SWill Andrews 	ISP_SET_PC(isp, chan, proc_active, 0);
2041d0a1de2SWill Andrews 	wakeup(wchan);
2051d0a1de2SWill Andrews #endif
2061d0a1de2SWill Andrews }
2071d0a1de2SWill Andrews 
2081d0a1de2SWill Andrews static void
2091d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2101d0a1de2SWill Andrews {
2111d0a1de2SWill Andrews 	struct cam_sim *sim;
2121d0a1de2SWill Andrews 	struct cam_path *path;
2131d0a1de2SWill Andrews 	struct ccb_setasync csa;
2141d0a1de2SWill Andrews 	int *num_threads;
2151d0a1de2SWill Andrews 
2161d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, sim, sim);
2171d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, path, path);
2181d0a1de2SWill Andrews 	ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
2191d0a1de2SWill Andrews 
2201d0a1de2SWill Andrews 	xpt_setup_ccb(&csa.ccb_h, path, 5);
2211d0a1de2SWill Andrews 	csa.ccb_h.func_code = XPT_SASYNC_CB;
2221d0a1de2SWill Andrews 	csa.event_enable = 0;
2231d0a1de2SWill Andrews 	csa.callback = isp_cam_async;
2241d0a1de2SWill Andrews 	csa.callback_arg = sim;
2251d0a1de2SWill Andrews 	xpt_action((union ccb *)&csa);
2261d0a1de2SWill Andrews 	xpt_free_path(path);
2271d0a1de2SWill Andrews 	xpt_bus_deregister(cam_sim_path(sim));
2281d0a1de2SWill Andrews 	cam_sim_free(sim, FALSE);
2291d0a1de2SWill Andrews 
2301d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
2311d0a1de2SWill Andrews 	wakeup(isp->isp_osinfo.pc.ptr);
2321d0a1de2SWill Andrews 	isp_detach_internal_target(isp, chan);
2331d0a1de2SWill Andrews 	while (*num_threads != 0)
2341d0a1de2SWill Andrews 		mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
2351d0a1de2SWill Andrews }
2361d0a1de2SWill Andrews 
2372df76c16SMatt Jacob int
2382df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2392df76c16SMatt Jacob {
2402df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2412df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2422df76c16SMatt Jacob 	int chan;
2432df76c16SMatt Jacob 
2442df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
2452df76c16SMatt Jacob 	isp->isp_osinfo.ehook.ich_arg = isp;
246e95725cbSMatt Jacob 	/*
247e95725cbSMatt Jacob 	 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
248e95725cbSMatt Jacob 	 * will be called right awawy, which will clear isp_osinfo.ehook_active,
249e95725cbSMatt Jacob 	 * which would be unwise to then set again later.
250e95725cbSMatt Jacob 	 */
251e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 1;
2522df76c16SMatt Jacob 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
2532df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
2542df76c16SMatt Jacob 		return (-EIO);
2552df76c16SMatt Jacob 	}
256ea6f23cdSMatt Jacob 
257ea6f23cdSMatt Jacob 	/*
258ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
259478f8a96SJustin T. Gibbs 	 */
2602df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2612df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2625d571944SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2632df76c16SMatt Jacob 		return (EIO);
264478f8a96SJustin T. Gibbs 	}
265478f8a96SJustin T. Gibbs 
2662df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2672df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2682df76c16SMatt Jacob 			goto unwind;
269ea6f23cdSMatt Jacob 		}
270ea6f23cdSMatt Jacob 	}
271ea6f23cdSMatt Jacob 
2722df76c16SMatt Jacob 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
27394dff771SMatt Jacob 	isp_timer_count = hz >> 2;
27494dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
2752df76c16SMatt Jacob 	isp->isp_osinfo.timer_active = 1;
276ea6f23cdSMatt Jacob 
2772df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
2782df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2792df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
280ea6f23cdSMatt Jacob 	}
2812df76c16SMatt Jacob 	return (0);
2825d571944SMatt Jacob 
2832df76c16SMatt Jacob unwind:
2842df76c16SMatt Jacob 	while (--chan >= 0) {
2852df76c16SMatt Jacob 		struct cam_sim *sim;
2862df76c16SMatt Jacob 		struct cam_path *path;
2871d0a1de2SWill Andrews 
2881d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
2891d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, path, path);
2902df76c16SMatt Jacob 		xpt_free_path(path);
2910a70657fSMatt Jacob 		ISP_LOCK(isp);
2920a70657fSMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
2932df76c16SMatt Jacob 		ISP_UNLOCK(isp);
2942df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
2952df76c16SMatt Jacob 	}
2962df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
2970a70657fSMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
2982df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
2990a70657fSMatt Jacob 	}
3002df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3012df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3022df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3032df76c16SMatt Jacob 	}
3042df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
3052df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
3062df76c16SMatt Jacob 	return (-1);
3072df76c16SMatt Jacob }
3082df76c16SMatt Jacob 
309e95725cbSMatt Jacob int
3102df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
3112df76c16SMatt Jacob {
312e95725cbSMatt Jacob 	struct cam_sim *sim;
3132df76c16SMatt Jacob 	int chan;
3142df76c16SMatt Jacob 
3150a70657fSMatt Jacob 	ISP_LOCK(isp);
3162df76c16SMatt Jacob 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
3171d0a1de2SWill Andrews 		ISP_GET_PC(isp, chan, sim, sim);
318e95725cbSMatt Jacob 		if (sim->refcount > 2) {
3192df76c16SMatt Jacob 			ISP_UNLOCK(isp);
320e95725cbSMatt Jacob 			return (EBUSY);
321e95725cbSMatt Jacob 		}
322e95725cbSMatt Jacob 	}
3231d0a1de2SWill Andrews 	/* Tell spawned threads that we're exiting. */
3241d0a1de2SWill Andrews 	isp->isp_osinfo.is_exiting = 1;
325e95725cbSMatt Jacob 	if (isp->isp_osinfo.timer_active) {
326e95725cbSMatt Jacob 		callout_stop(&isp->isp_osinfo.tmo);
327e95725cbSMatt Jacob 		isp->isp_osinfo.timer_active = 0;
328e95725cbSMatt Jacob 	}
3291d0a1de2SWill Andrews 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
3301d0a1de2SWill Andrews 		isp_detach_chan(isp, chan);
331387d8239SMatt Jacob 	ISP_UNLOCK(isp);
3321d0a1de2SWill Andrews 
3332df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
3342df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
3352df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
3362df76c16SMatt Jacob 	}
3372df76c16SMatt Jacob 	if (isp->isp_osinfo.ehook_active) {
3382df76c16SMatt Jacob 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
3392df76c16SMatt Jacob 		isp->isp_osinfo.ehook_active = 0;
3402df76c16SMatt Jacob 	}
341a035b0afSMatt Jacob 	if (isp->isp_osinfo.devq != NULL) {
3422df76c16SMatt Jacob 		cam_simq_free(isp->isp_osinfo.devq);
3432df76c16SMatt Jacob 		isp->isp_osinfo.devq = NULL;
3440a70657fSMatt Jacob 	}
345e95725cbSMatt Jacob 	return (0);
3465d571944SMatt Jacob }
3475d571944SMatt Jacob 
348f7c631bcSMatt Jacob static void
3492df76c16SMatt Jacob isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
350fdeb9f2fSMatt Jacob {
3512df76c16SMatt Jacob 	if (IS_FC(isp)) {
3522df76c16SMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
3532df76c16SMatt Jacob 		if (fc->simqfrozen == 0) {
3542df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
3552df76c16SMatt Jacob 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
3562df76c16SMatt Jacob 			xpt_freeze_simq(fc->sim, 1);
357fdeb9f2fSMatt Jacob 		} else {
3582df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
3592df76c16SMatt Jacob 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3602df76c16SMatt Jacob 		}
361fdeb9f2fSMatt Jacob 	}
362fdeb9f2fSMatt Jacob }
363fdeb9f2fSMatt Jacob 
364e95725cbSMatt Jacob static void
365e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
366e95725cbSMatt Jacob {
367e95725cbSMatt Jacob 	if (IS_FC(isp)) {
368e95725cbSMatt Jacob 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
369e95725cbSMatt Jacob 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
370e95725cbSMatt Jacob 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
371e95725cbSMatt Jacob 		if (wasfrozen && fc->simqfrozen == 0) {
372387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
373e95725cbSMatt Jacob 			xpt_release_simq(fc->sim, 1);
374e95725cbSMatt Jacob 		}
375e95725cbSMatt Jacob 	}
376e95725cbSMatt Jacob }
377e95725cbSMatt Jacob 
3789cd7268eSMatt Jacob 
3795d571944SMatt Jacob static int
3802df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
3815d571944SMatt Jacob {
3829cd7268eSMatt Jacob 	ispsoftc_t *isp;
3832df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
3845d571944SMatt Jacob 
3852df76c16SMatt Jacob 	isp = dev->si_drv1;
3865d571944SMatt Jacob 
387c1504bc0SMatt Jacob 	switch (c) {
3885d571944SMatt Jacob 	case ISP_SDBLEV:
3895d571944SMatt Jacob 	{
3905d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
3915d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
3925d571944SMatt Jacob 		*(int *)addr = olddblev;
3935d571944SMatt Jacob 		retval = 0;
3945d571944SMatt Jacob 		break;
3955d571944SMatt Jacob 	}
396746e9c85SMatt Jacob 	case ISP_GETROLE:
3972df76c16SMatt Jacob 		chan = *(int *)addr;
3982df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
3992df76c16SMatt Jacob 			retval = -ENXIO;
4002df76c16SMatt Jacob 			break;
4012df76c16SMatt Jacob 		}
4022df76c16SMatt Jacob 		if (IS_FC(isp)) {
4032df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4042df76c16SMatt Jacob 		} else {
4052df76c16SMatt Jacob 			*(int *)addr = SDPARAM(isp, chan)->role;
4062df76c16SMatt Jacob 		}
407746e9c85SMatt Jacob 		retval = 0;
408746e9c85SMatt Jacob 		break;
409746e9c85SMatt Jacob 	case ISP_SETROLE:
410746e9c85SMatt Jacob 		nr = *(int *)addr;
4112df76c16SMatt Jacob 		chan = nr >> 8;
4122df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4132df76c16SMatt Jacob 			retval = -ENXIO;
4142df76c16SMatt Jacob 			break;
4152df76c16SMatt Jacob 		}
4162df76c16SMatt Jacob 		nr &= 0xff;
417746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
418746e9c85SMatt Jacob 			retval = EINVAL;
419746e9c85SMatt Jacob 			break;
420746e9c85SMatt Jacob 		}
4212df76c16SMatt Jacob 		if (IS_FC(isp)) {
422ae5db118SMatt Jacob 			/*
423ae5db118SMatt Jacob 			 * We don't really support dual role at present on FC cards.
424ae5db118SMatt Jacob 			 *
425ae5db118SMatt Jacob 			 * We should, but a bunch of things are currently broken,
426ae5db118SMatt Jacob 			 * so don't allow it.
427ae5db118SMatt Jacob 			 */
428ae5db118SMatt Jacob 			if (nr == ISP_ROLE_BOTH) {
429ae5db118SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
430ae5db118SMatt Jacob 				retval = EINVAL;
431ae5db118SMatt Jacob 				break;
432ae5db118SMatt Jacob 			}
4332df76c16SMatt Jacob 			*(int *)addr = FCPARAM(isp, chan)->role;
4342df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
4352df76c16SMatt Jacob 			ISP_LOCK(isp);
4362df76c16SMatt Jacob 			retval = isp_fc_change_role(isp, chan, nr);
4372df76c16SMatt Jacob 			ISP_UNLOCK(isp);
4382df76c16SMatt Jacob #else
4392df76c16SMatt Jacob 			FCPARAM(isp, chan)->role = nr;
4402df76c16SMatt Jacob #endif
4412df76c16SMatt Jacob 		} else {
4422df76c16SMatt Jacob 			*(int *)addr = SDPARAM(isp, chan)->role;
4432df76c16SMatt Jacob 			SDPARAM(isp, chan)->role = nr;
4442df76c16SMatt Jacob 		}
4452df76c16SMatt Jacob 		retval = 0;
4462df76c16SMatt Jacob 		break;
4472df76c16SMatt Jacob 
4485d571944SMatt Jacob 	case ISP_RESETHBA:
449511ced9bSMatt Jacob 		ISP_LOCK(isp);
4502df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
4512df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, ISP_NOCHAN);
4522df76c16SMatt Jacob #endif
4532df76c16SMatt Jacob 		isp_reinit(isp, 0);
454511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4555d571944SMatt Jacob 		retval = 0;
4565d571944SMatt Jacob 		break;
4572df76c16SMatt Jacob 
458f553351eSMatt Jacob 	case ISP_RESCAN:
4595d571944SMatt Jacob 		if (IS_FC(isp)) {
4602df76c16SMatt Jacob 			chan = *(int *)addr;
4612df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4622df76c16SMatt Jacob 				retval = -ENXIO;
4632df76c16SMatt Jacob 				break;
4642df76c16SMatt Jacob 			}
465511ced9bSMatt Jacob 			ISP_LOCK(isp);
4662df76c16SMatt Jacob 			if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
4675d571944SMatt Jacob 				retval = EIO;
4685d571944SMatt Jacob 			} else {
4695d571944SMatt Jacob 				retval = 0;
4705d571944SMatt Jacob 			}
471511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4725d571944SMatt Jacob 		}
4735d571944SMatt Jacob 		break;
4742df76c16SMatt Jacob 
4755d571944SMatt Jacob 	case ISP_FC_LIP:
4765d571944SMatt Jacob 		if (IS_FC(isp)) {
4772df76c16SMatt Jacob 			chan = *(int *)addr;
4782df76c16SMatt Jacob 			if (chan < 0 || chan >= isp->isp_nchan) {
4792df76c16SMatt Jacob 				retval = -ENXIO;
4802df76c16SMatt Jacob 				break;
4812df76c16SMatt Jacob 			}
482511ced9bSMatt Jacob 			ISP_LOCK(isp);
4832df76c16SMatt Jacob 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4845d571944SMatt Jacob 				retval = EIO;
4855d571944SMatt Jacob 			} else {
4865d571944SMatt Jacob 				retval = 0;
4875d571944SMatt Jacob 			}
488511ced9bSMatt Jacob 			ISP_UNLOCK(isp);
4895d571944SMatt Jacob 		}
4905d571944SMatt Jacob 		break;
4915d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4925d571944SMatt Jacob 	{
4935d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
49410365e5aSMatt Jacob 		fcportdb_t *lp;
4955d571944SMatt Jacob 
49602e2b2d9SMatt Jacob 		if (IS_SCSI(isp)) {
49702e2b2d9SMatt Jacob 			break;
49802e2b2d9SMatt Jacob 		}
4992e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
5005d571944SMatt Jacob 			retval = EINVAL;
5015d571944SMatt Jacob 			break;
5025d571944SMatt Jacob 		}
5032df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
5042df76c16SMatt Jacob 		if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
505387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
50610365e5aSMatt Jacob 			ifc->loopid = lp->handle;
5075d571944SMatt Jacob 			ifc->portid = lp->portid;
5085d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
5095d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
5105d571944SMatt Jacob 			retval = 0;
5115d571944SMatt Jacob 		} else {
5125d571944SMatt Jacob 			retval = ENODEV;
5135d571944SMatt Jacob 		}
5145d571944SMatt Jacob 		break;
5155d571944SMatt Jacob 	}
5162903b272SMatt Jacob 	case ISP_GET_STATS:
5172903b272SMatt Jacob 	{
5182903b272SMatt Jacob 		isp_stats_t *sp = (isp_stats_t *) addr;
5192903b272SMatt Jacob 
5202df76c16SMatt Jacob 		ISP_MEMZERO(sp, sizeof (*sp));
5212903b272SMatt Jacob 		sp->isp_stat_version = ISP_STATS_VERSION;
5222903b272SMatt Jacob 		sp->isp_type = isp->isp_type;
5232903b272SMatt Jacob 		sp->isp_revision = isp->isp_revision;
524511ced9bSMatt Jacob 		ISP_LOCK(isp);
5252903b272SMatt Jacob 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
5262903b272SMatt Jacob 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
5272903b272SMatt Jacob 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
5282903b272SMatt Jacob 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
5292903b272SMatt Jacob 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
5302903b272SMatt Jacob 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
5312903b272SMatt Jacob 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
5322903b272SMatt Jacob 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
533511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5342903b272SMatt Jacob 		retval = 0;
5352903b272SMatt Jacob 		break;
5362903b272SMatt Jacob 	}
5372903b272SMatt Jacob 	case ISP_CLR_STATS:
538511ced9bSMatt Jacob 		ISP_LOCK(isp);
5392903b272SMatt Jacob 		isp->isp_intcnt = 0;
5402903b272SMatt Jacob 		isp->isp_intbogus = 0;
5412903b272SMatt Jacob 		isp->isp_intmboxc = 0;
5422903b272SMatt Jacob 		isp->isp_intoasync = 0;
5432903b272SMatt Jacob 		isp->isp_rsltccmplt = 0;
5442903b272SMatt Jacob 		isp->isp_fphccmplt = 0;
5452903b272SMatt Jacob 		isp->isp_rscchiwater = 0;
5462903b272SMatt Jacob 		isp->isp_fpcchiwater = 0;
547511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
5482903b272SMatt Jacob 		retval = 0;
5492903b272SMatt Jacob 		break;
550570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
551570c7a3fSMatt Jacob 	{
552570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
5532df76c16SMatt Jacob 		int chan = hba->fc_channel;
55402e2b2d9SMatt Jacob 
5552df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5562df76c16SMatt Jacob 			retval = ENXIO;
5572df76c16SMatt Jacob 			break;
5582df76c16SMatt Jacob 		}
559a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
560a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
561a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
5622df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
56302e2b2d9SMatt Jacob 		if (IS_FC(isp)) {
5642df76c16SMatt Jacob 			hba->fc_nports = MAX_FC_TARG;
5652df76c16SMatt Jacob 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
5662df76c16SMatt Jacob 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
5672df76c16SMatt Jacob 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
5682df76c16SMatt Jacob 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
5692df76c16SMatt Jacob 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
5702df76c16SMatt Jacob 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
5712df76c16SMatt Jacob 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
5722df76c16SMatt Jacob 		} else {
5732df76c16SMatt Jacob 			hba->fc_nports = MAX_TARGETS;
5742df76c16SMatt Jacob 			hba->fc_speed = 0;
5752df76c16SMatt Jacob 			hba->fc_topology = 0;
5762df76c16SMatt Jacob 			hba->nvram_node_wwn = 0ull;
5772df76c16SMatt Jacob 			hba->nvram_port_wwn = 0ull;
5782df76c16SMatt Jacob 			hba->active_node_wwn = 0ull;
5792df76c16SMatt Jacob 			hba->active_port_wwn = 0ull;
58002e2b2d9SMatt Jacob 		}
581570c7a3fSMatt Jacob 		retval = 0;
582570c7a3fSMatt Jacob 		break;
583570c7a3fSMatt Jacob 	}
5848e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
5858e62a8acSMatt Jacob 	{
5868e62a8acSMatt Jacob 		int needmarker;
5878e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
5881dae40ebSMatt Jacob 		uint16_t loopid;
5898e62a8acSMatt Jacob 		mbreg_t mbs;
5908e62a8acSMatt Jacob 
5918e62a8acSMatt Jacob 		if (IS_SCSI(isp)) {
5928e62a8acSMatt Jacob 			break;
5938e62a8acSMatt Jacob 		}
5948e62a8acSMatt Jacob 
5952df76c16SMatt Jacob 		chan = fct->chan;
5962df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5972df76c16SMatt Jacob 			retval = -ENXIO;
5982df76c16SMatt Jacob 			break;
5992df76c16SMatt Jacob 		}
6002df76c16SMatt Jacob 
6018e62a8acSMatt Jacob 		needmarker = retval = 0;
602e5265237SMatt Jacob 		loopid = fct->loopid;
6032df76c16SMatt Jacob 		ISP_LOCK(isp);
6042df76c16SMatt Jacob 		if (IS_24XX(isp)) {
6052df76c16SMatt Jacob 			uint8_t local[QENTRY_LEN];
6062df76c16SMatt Jacob 			isp24xx_tmf_t *tmf;
6072df76c16SMatt Jacob 			isp24xx_statusreq_t *sp;
6082df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, chan);
6092df76c16SMatt Jacob 			fcportdb_t *lp;
6102df76c16SMatt Jacob 			int i;
6112df76c16SMatt Jacob 
6122df76c16SMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
6132df76c16SMatt Jacob 				lp = &fcp->portdb[i];
6142df76c16SMatt Jacob 				if (lp->handle == loopid) {
6152df76c16SMatt Jacob 					break;
6162df76c16SMatt Jacob 				}
6172df76c16SMatt Jacob 			}
6182df76c16SMatt Jacob 			if (i == MAX_FC_TARG) {
6192df76c16SMatt Jacob 				retval = ENXIO;
6202df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6212df76c16SMatt Jacob 				break;
6222df76c16SMatt Jacob 			}
6232df76c16SMatt Jacob 			/* XXX VALIDATE LP XXX */
6242df76c16SMatt Jacob 			tmf = (isp24xx_tmf_t *) local;
6252df76c16SMatt Jacob 			ISP_MEMZERO(tmf, QENTRY_LEN);
6262df76c16SMatt Jacob 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
6272df76c16SMatt Jacob 			tmf->tmf_header.rqs_entry_count = 1;
6282df76c16SMatt Jacob 			tmf->tmf_nphdl = lp->handle;
6292df76c16SMatt Jacob 			tmf->tmf_delay = 2;
6302df76c16SMatt Jacob 			tmf->tmf_timeout = 2;
6312df76c16SMatt Jacob 			tmf->tmf_tidlo = lp->portid;
6322df76c16SMatt Jacob 			tmf->tmf_tidhi = lp->portid >> 16;
6332df76c16SMatt Jacob 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
6342df76c16SMatt Jacob 			tmf->tmf_lun[1] = fct->lun & 0xff;
6352df76c16SMatt Jacob 			if (fct->lun >= 256) {
6362df76c16SMatt Jacob 				tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
6372df76c16SMatt Jacob 			}
6382df76c16SMatt Jacob 			switch (fct->action) {
6392df76c16SMatt Jacob 			case IPT_CLEAR_ACA:
6402df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
6412df76c16SMatt Jacob 				break;
6422df76c16SMatt Jacob 			case IPT_TARGET_RESET:
6432df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
6442df76c16SMatt Jacob 				needmarker = 1;
6452df76c16SMatt Jacob 				break;
6462df76c16SMatt Jacob 			case IPT_LUN_RESET:
6472df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
6482df76c16SMatt Jacob 				needmarker = 1;
6492df76c16SMatt Jacob 				break;
6502df76c16SMatt Jacob 			case IPT_CLEAR_TASK_SET:
6512df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
6522df76c16SMatt Jacob 				needmarker = 1;
6532df76c16SMatt Jacob 				break;
6542df76c16SMatt Jacob 			case IPT_ABORT_TASK_SET:
6552df76c16SMatt Jacob 				tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
6562df76c16SMatt Jacob 				needmarker = 1;
6572df76c16SMatt Jacob 				break;
6582df76c16SMatt Jacob 			default:
6592df76c16SMatt Jacob 				retval = EINVAL;
6602df76c16SMatt Jacob 				break;
6612df76c16SMatt Jacob 			}
6622df76c16SMatt Jacob 			if (retval) {
6632df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6642df76c16SMatt Jacob 				break;
6652df76c16SMatt Jacob 			}
6662df76c16SMatt Jacob 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
6672df76c16SMatt Jacob 			mbs.param[1] = QENTRY_LEN;
6682df76c16SMatt Jacob 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
6692df76c16SMatt Jacob 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
6702df76c16SMatt Jacob 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
6712df76c16SMatt Jacob 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
6722df76c16SMatt Jacob 
6732df76c16SMatt Jacob 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
6742df76c16SMatt Jacob 				ISP_UNLOCK(isp);
6752df76c16SMatt Jacob 				retval = ENOMEM;
6762df76c16SMatt Jacob 				break;
6772df76c16SMatt Jacob 			}
6782df76c16SMatt Jacob 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
67937bb79f1SMarius Strobl 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
6802df76c16SMatt Jacob 			sp = (isp24xx_statusreq_t *) local;
6812df76c16SMatt Jacob 			sp->req_completion_status = 1;
6822df76c16SMatt Jacob 			retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
68337bb79f1SMarius Strobl 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
6842df76c16SMatt Jacob 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
6852df76c16SMatt Jacob 			FC_SCRATCH_RELEASE(isp, chan);
6862df76c16SMatt Jacob 			if (retval || sp->req_completion_status != 0) {
6872df76c16SMatt Jacob 				FC_SCRATCH_RELEASE(isp, chan);
6882df76c16SMatt Jacob 				retval = EIO;
6892df76c16SMatt Jacob 			}
6902df76c16SMatt Jacob 			if (retval == 0) {
6912df76c16SMatt Jacob 				if (needmarker) {
6922df76c16SMatt Jacob 					fcp->sendmarker = 1;
6932df76c16SMatt Jacob 				}
6942df76c16SMatt Jacob 			}
6952df76c16SMatt Jacob 		} else {
6962df76c16SMatt Jacob 			MBSINIT(&mbs, 0, MBLOGALL, 0);
6972df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
698e5265237SMatt Jacob 				loopid <<= 8;
699e5265237SMatt Jacob 			}
7008e62a8acSMatt Jacob 			switch (fct->action) {
701f0f536d1SMatt Jacob 			case IPT_CLEAR_ACA:
7028e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_ACA;
703e5265237SMatt Jacob 				mbs.param[1] = loopid;
7048e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7058e62a8acSMatt Jacob 				break;
706f0f536d1SMatt Jacob 			case IPT_TARGET_RESET:
7078e62a8acSMatt Jacob 				mbs.param[0] = MBOX_TARGET_RESET;
708e5265237SMatt Jacob 				mbs.param[1] = loopid;
7098e62a8acSMatt Jacob 				needmarker = 1;
7108e62a8acSMatt Jacob 				break;
711f0f536d1SMatt Jacob 			case IPT_LUN_RESET:
7128e62a8acSMatt Jacob 				mbs.param[0] = MBOX_LUN_RESET;
713e5265237SMatt Jacob 				mbs.param[1] = loopid;
7148e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7158e62a8acSMatt Jacob 				needmarker = 1;
7168e62a8acSMatt Jacob 				break;
717f0f536d1SMatt Jacob 			case IPT_CLEAR_TASK_SET:
7188e62a8acSMatt Jacob 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
719e5265237SMatt Jacob 				mbs.param[1] = loopid;
7208e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7218e62a8acSMatt Jacob 				needmarker = 1;
7228e62a8acSMatt Jacob 				break;
723f0f536d1SMatt Jacob 			case IPT_ABORT_TASK_SET:
7248e62a8acSMatt Jacob 				mbs.param[0] = MBOX_ABORT_TASK_SET;
725e5265237SMatt Jacob 				mbs.param[1] = loopid;
7268e62a8acSMatt Jacob 				mbs.param[2] = fct->lun;
7278e62a8acSMatt Jacob 				needmarker = 1;
7288e62a8acSMatt Jacob 				break;
7298e62a8acSMatt Jacob 			default:
7308e62a8acSMatt Jacob 				retval = EINVAL;
7318e62a8acSMatt Jacob 				break;
7328e62a8acSMatt Jacob 			}
7338e62a8acSMatt Jacob 			if (retval == 0) {
7348e62a8acSMatt Jacob 				if (needmarker) {
7352df76c16SMatt Jacob 					FCPARAM(isp, chan)->sendmarker = 1;
7368e62a8acSMatt Jacob 				}
7378e62a8acSMatt Jacob 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
7382df76c16SMatt Jacob 				if (retval) {
7398e62a8acSMatt Jacob 					retval = EIO;
7408e62a8acSMatt Jacob 				}
7412df76c16SMatt Jacob 			}
7422df76c16SMatt Jacob 		}
7432df76c16SMatt Jacob 		ISP_UNLOCK(isp);
7448e62a8acSMatt Jacob 		break;
7458e62a8acSMatt Jacob 	}
7465d571944SMatt Jacob 	default:
7475d571944SMatt Jacob 		break;
7485d571944SMatt Jacob 	}
7495d571944SMatt Jacob 	return (retval);
7500470d791SMatt Jacob }
751478f8a96SJustin T. Gibbs 
752f6e75de2SMatt Jacob static void
753f6e75de2SMatt Jacob isp_intr_enable(void *arg)
754f6e75de2SMatt Jacob {
7552df76c16SMatt Jacob 	int chan;
7569cd7268eSMatt Jacob 	ispsoftc_t *isp = arg;
7570a70657fSMatt Jacob 	ISP_LOCK(isp);
7582df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
7592df76c16SMatt Jacob 		if (IS_FC(isp)) {
7602df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
76110365e5aSMatt Jacob 				ISP_ENABLE_INTS(isp);
7622df76c16SMatt Jacob 				break;
7632df76c16SMatt Jacob 			}
7642df76c16SMatt Jacob 		} else {
7652df76c16SMatt Jacob 			if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
7662df76c16SMatt Jacob 				ISP_ENABLE_INTS(isp);
7672df76c16SMatt Jacob 				break;
7682df76c16SMatt Jacob 			}
7692df76c16SMatt Jacob 		}
770d6e5500fSMatt Jacob 	}
771e95725cbSMatt Jacob 	isp->isp_osinfo.ehook_active = 0;
7720a70657fSMatt Jacob 	ISP_UNLOCK(isp);
773f6e75de2SMatt Jacob 	/* Release our hook so that the boot can continue. */
774f6e75de2SMatt Jacob 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
775f6e75de2SMatt Jacob }
776d81ba9d5SMatt Jacob 
777d81ba9d5SMatt Jacob /*
7782df76c16SMatt Jacob  * Local Inlines
7792df76c16SMatt Jacob  */
7802df76c16SMatt Jacob 
7812df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
7822df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
7832df76c16SMatt Jacob 
7842df76c16SMatt Jacob static ISP_INLINE int
7852df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
7862df76c16SMatt Jacob {
7872df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
7882df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
7892df76c16SMatt Jacob 		return (-1);
7902df76c16SMatt Jacob 	}
7912df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
7922df76c16SMatt Jacob 	return (0);
7932df76c16SMatt Jacob }
7942df76c16SMatt Jacob 
7952df76c16SMatt Jacob static ISP_INLINE void
7962df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
7972df76c16SMatt Jacob {
798387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
799405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
800405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
801405b7a29SMatt Jacob 		PISP_PCMD(ccb)->totslen = 0;
802405b7a29SMatt Jacob 		PISP_PCMD(ccb)->cumslen = 0;
803405b7a29SMatt Jacob 		PISP_PCMD(ccb)->crn = 0;
804405b7a29SMatt Jacob #endif
805405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
8062df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
8072df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
8082df76c16SMatt Jacob 	}
809387d8239SMatt Jacob }
810387d8239SMatt Jacob 
8112df76c16SMatt Jacob /*
812d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
813d81ba9d5SMatt Jacob  */
814d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
815e2873b76SMatt Jacob static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
816e2873b76SMatt Jacob static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
817e2873b76SMatt Jacob static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
8182df76c16SMatt Jacob static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
8192df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
8202df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
8212df76c16SMatt Jacob static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
8222df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
8232df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
824523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
8252df76c16SMatt Jacob static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
8262df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
8272df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
8282df76c16SMatt Jacob static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
8292df76c16SMatt Jacob static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
8309cd7268eSMatt Jacob static void destroy_lun_state(ispsoftc_t *, tstate_t *);
8312df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
832e2873b76SMatt Jacob static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
8332df76c16SMatt Jacob static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
8342df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
8352df76c16SMatt Jacob static int isp_enable_target_mode(ispsoftc_t *, int);
836e2873b76SMatt Jacob static int isp_disable_target_mode(ispsoftc_t *, int);
8379cd7268eSMatt Jacob static void isp_ledone(ispsoftc_t *, lun_entry_t *);
838f48ce188SMatt Jacob static timeout_t isp_refire_putback_atio;
839387d8239SMatt Jacob static timeout_t isp_refire_notify_ack;
840a1bc34c6SMatt Jacob static void isp_complete_ctio(union ccb *);
841a1bc34c6SMatt Jacob static void isp_target_putback_atio(union ccb *);
84294dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
843387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
8442df76c16SMatt Jacob static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
8452df76c16SMatt Jacob static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
8462df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
8472df76c16SMatt Jacob static void isp_handle_platform_ctio(ispsoftc_t *, void *);
8482df76c16SMatt Jacob static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
8492df76c16SMatt Jacob static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
8502df76c16SMatt Jacob static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
8512df76c16SMatt Jacob static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
8522df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
8532df76c16SMatt Jacob static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
8542df76c16SMatt Jacob static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
855d81ba9d5SMatt Jacob 
856e2873b76SMatt Jacob static ISP_INLINE void
857e2873b76SMatt Jacob isp_tmlock(ispsoftc_t *isp, const char *msg)
858e2873b76SMatt Jacob {
859e2873b76SMatt Jacob 	while (isp->isp_osinfo.tmbusy) {
860e2873b76SMatt Jacob 		isp->isp_osinfo.tmwanted = 1;
861e2873b76SMatt Jacob 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
862e2873b76SMatt Jacob 	}
863e2873b76SMatt Jacob 	isp->isp_osinfo.tmbusy = 1;
864e2873b76SMatt Jacob }
865e2873b76SMatt Jacob 
866e2873b76SMatt Jacob static ISP_INLINE void
867e2873b76SMatt Jacob isp_tmunlk(ispsoftc_t *isp)
868e2873b76SMatt Jacob {
869e2873b76SMatt Jacob 	isp->isp_osinfo.tmbusy = 0;
870e2873b76SMatt Jacob 	if (isp->isp_osinfo.tmwanted) {
871e2873b76SMatt Jacob 		isp->isp_osinfo.tmwanted = 0;
872e2873b76SMatt Jacob 		wakeup(isp);
873e2873b76SMatt Jacob 	}
874e2873b76SMatt Jacob }
875e2873b76SMatt Jacob 
876e2873b76SMatt Jacob static ISP_INLINE int
877e2873b76SMatt Jacob is_any_lun_enabled(ispsoftc_t *isp, int bus)
878e2873b76SMatt Jacob {
879e2873b76SMatt Jacob 	struct tslist *lhp;
880e2873b76SMatt Jacob 	int i;
881e2873b76SMatt Jacob 
882e2873b76SMatt Jacob 	for (i = 0; i < LUN_HASH_SIZE; i++) {
883e2873b76SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
884e2873b76SMatt Jacob 		if (SLIST_FIRST(lhp))
885e2873b76SMatt Jacob 			return (1);
886e2873b76SMatt Jacob 	}
887e2873b76SMatt Jacob 	return (0);
888e2873b76SMatt Jacob }
889e2873b76SMatt Jacob 
8902df76c16SMatt Jacob static ISP_INLINE int
8919cd7268eSMatt Jacob is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
892d81ba9d5SMatt Jacob {
893d81ba9d5SMatt Jacob 	tstate_t *tptr;
8942df76c16SMatt Jacob 	struct tslist *lhp;
8952df76c16SMatt Jacob 
8962df76c16SMatt Jacob 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
8972df76c16SMatt Jacob 	SLIST_FOREACH(tptr, lhp, next) {
898315a4d6fSAlexander Motin 		if (tptr->ts_lun == lun) {
899d81ba9d5SMatt Jacob 			return (1);
900d81ba9d5SMatt Jacob 		}
9012df76c16SMatt Jacob 	}
902d81ba9d5SMatt Jacob 	return (0);
903d81ba9d5SMatt Jacob }
904d81ba9d5SMatt Jacob 
9052df76c16SMatt Jacob static void
9062df76c16SMatt Jacob dump_tstates(ispsoftc_t *isp, int bus)
907d81ba9d5SMatt Jacob {
9082df76c16SMatt Jacob 	int i, j;
9092df76c16SMatt Jacob 	struct tslist *lhp;
9102df76c16SMatt Jacob 	tstate_t *tptr = NULL;
9112df76c16SMatt Jacob 
9122df76c16SMatt Jacob 	if (bus >= isp->isp_nchan) {
9132df76c16SMatt Jacob 		return;
914a1bc34c6SMatt Jacob 	}
9152df76c16SMatt Jacob 	for (i = 0; i < LUN_HASH_SIZE; i++) {
9162df76c16SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9172df76c16SMatt Jacob 		j = 0;
9182df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
9192df76c16SMatt Jacob 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
9202df76c16SMatt Jacob 			j++;
921d81ba9d5SMatt Jacob 		}
922d81ba9d5SMatt Jacob 	}
923d81ba9d5SMatt Jacob }
924d81ba9d5SMatt Jacob 
9252df76c16SMatt Jacob static ISP_INLINE tstate_t *
9269cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
927d81ba9d5SMatt Jacob {
92864edff94SMatt Jacob 	tstate_t *tptr = NULL;
9292df76c16SMatt Jacob 	struct tslist *lhp;
930d81ba9d5SMatt Jacob 
9312df76c16SMatt Jacob 	if (bus < isp->isp_nchan) {
932315a4d6fSAlexander Motin 		ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
9332df76c16SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
934315a4d6fSAlexander Motin 			if (tptr->ts_lun == lun) {
935d81ba9d5SMatt Jacob 				tptr->hold++;
936d81ba9d5SMatt Jacob 				return (tptr);
937d81ba9d5SMatt Jacob 			}
93864edff94SMatt Jacob 		}
939126ec864SMatt Jacob 	}
9402df76c16SMatt Jacob 	return (NULL);
9412df76c16SMatt Jacob }
942d81ba9d5SMatt Jacob 
9432df76c16SMatt Jacob static ISP_INLINE tstate_t *
9442df76c16SMatt Jacob get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
9452df76c16SMatt Jacob {
9462df76c16SMatt Jacob 	tstate_t *tptr = NULL;
9472df76c16SMatt Jacob 	atio_private_data_t *atp;
9482df76c16SMatt Jacob 	struct tslist *lhp;
9492df76c16SMatt Jacob 	int i;
9502df76c16SMatt Jacob 
9512df76c16SMatt Jacob 	if (bus < isp->isp_nchan && tagval != 0) {
9522df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9532df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9542df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
955523ea374SAlexander Motin 				atp = isp_find_atpd(isp, tptr, tagval);
956523ea374SAlexander Motin 				if (atp) {
957d81ba9d5SMatt Jacob 					tptr->hold++;
958d81ba9d5SMatt Jacob 					return (tptr);
959d81ba9d5SMatt Jacob 				}
9602df76c16SMatt Jacob 			}
9612df76c16SMatt Jacob 		}
9622df76c16SMatt Jacob 	}
9632df76c16SMatt Jacob 	return (NULL);
964d81ba9d5SMatt Jacob }
965d81ba9d5SMatt Jacob 
9662df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
9672df76c16SMatt Jacob get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
9682df76c16SMatt Jacob {
9692df76c16SMatt Jacob 	inot_private_data_t *ntp;
9702df76c16SMatt Jacob 	tstate_t *tptr;
9712df76c16SMatt Jacob 	struct tslist *lhp;
9722df76c16SMatt Jacob 	int bus, i;
9732df76c16SMatt Jacob 
9742df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
9752df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
9762df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
9772df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
9782df76c16SMatt Jacob 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
9792df76c16SMatt Jacob 				if (ntp) {
9802df76c16SMatt Jacob 					*rslt = tptr;
9812df76c16SMatt Jacob 					tptr->hold++;
9822df76c16SMatt Jacob 					return (ntp);
9832df76c16SMatt Jacob 				}
9842df76c16SMatt Jacob 			}
9852df76c16SMatt Jacob 		}
9862df76c16SMatt Jacob 	}
9872df76c16SMatt Jacob 	return (NULL);
9882df76c16SMatt Jacob }
989e2873b76SMatt Jacob 
9902df76c16SMatt Jacob static ISP_INLINE void
9919cd7268eSMatt Jacob rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
992d81ba9d5SMatt Jacob {
9932df76c16SMatt Jacob 	KASSERT((tptr->hold), ("tptr not held"));
994d81ba9d5SMatt Jacob 	tptr->hold--;
995d81ba9d5SMatt Jacob }
996d81ba9d5SMatt Jacob 
9972df76c16SMatt Jacob static void
9982df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
9992df76c16SMatt Jacob {
10002df76c16SMatt Jacob 	inot_private_data_t *ntp;
100194dff771SMatt Jacob 	inot_private_data_t *restart_queue;
10022df76c16SMatt Jacob 	tstate_t *tptr;
100394dff771SMatt Jacob 	union ccb *ccb;
10042df76c16SMatt Jacob 	struct tslist *lhp;
10052df76c16SMatt Jacob 	int bus, i;
10062df76c16SMatt Jacob 
10072df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
10082df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
10092df76c16SMatt Jacob 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
10102df76c16SMatt Jacob 			SLIST_FOREACH(tptr, lhp, next) {
101194dff771SMatt Jacob 				if ((restart_queue = tptr->restart_queue) != NULL)
10122df76c16SMatt Jacob 					tptr->restart_queue = NULL;
10132df76c16SMatt Jacob 				while (restart_queue) {
10142df76c16SMatt Jacob 					ntp = restart_queue;
10152df76c16SMatt Jacob 					restart_queue = ntp->rd.nt.nt_hba;
10162df76c16SMatt Jacob 					if (IS_24XX(isp)) {
10172df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
10182df76c16SMatt Jacob 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
10192df76c16SMatt Jacob 					} else {
10202df76c16SMatt Jacob 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
10212df76c16SMatt Jacob 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
10222df76c16SMatt Jacob 					}
10232df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
10242df76c16SMatt Jacob 					if (tptr->restart_queue && restart_queue != NULL) {
10252df76c16SMatt Jacob 						ntp = tptr->restart_queue;
10262df76c16SMatt Jacob 						tptr->restart_queue = restart_queue;
10272df76c16SMatt Jacob 						while (restart_queue->rd.nt.nt_hba) {
10282df76c16SMatt Jacob 							restart_queue = restart_queue->rd.nt.nt_hba;
10292df76c16SMatt Jacob 						}
10302df76c16SMatt Jacob 						restart_queue->rd.nt.nt_hba = ntp;
10312df76c16SMatt Jacob 						break;
10322df76c16SMatt Jacob 					}
10332df76c16SMatt Jacob 				}
103494dff771SMatt Jacob 				/*
103594dff771SMatt Jacob 				 * We only need to do this once per tptr
103694dff771SMatt Jacob 				 */
103794dff771SMatt Jacob 				if (!TAILQ_EMPTY(&tptr->waitq)) {
103894dff771SMatt Jacob 					ccb = (union ccb *)TAILQ_LAST(&tptr->waitq, isp_ccbq);
103994dff771SMatt Jacob 					TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
104094dff771SMatt Jacob 					isp_target_start_ctio(isp, ccb, FROM_TIMER);
104194dff771SMatt Jacob 				}
10422df76c16SMatt Jacob 			}
10432df76c16SMatt Jacob 		}
10442df76c16SMatt Jacob 	}
10452df76c16SMatt Jacob }
10462df76c16SMatt Jacob 
10472df76c16SMatt Jacob static ISP_INLINE atio_private_data_t *
10482df76c16SMatt Jacob isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
104953036e92SMatt Jacob {
105053036e92SMatt Jacob 	atio_private_data_t *atp;
10512df76c16SMatt Jacob 
1052523ea374SAlexander Motin 	atp = LIST_FIRST(&tptr->atfree);
10532df76c16SMatt Jacob 	if (atp) {
1054523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
1055523ea374SAlexander Motin 		atp->tag = tag;
1056523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
10572df76c16SMatt Jacob 	}
105853036e92SMatt Jacob 	return (atp);
105953036e92SMatt Jacob }
1060523ea374SAlexander Motin 
1061523ea374SAlexander Motin static ISP_INLINE atio_private_data_t *
1062523ea374SAlexander Motin isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1063523ea374SAlexander Motin {
1064523ea374SAlexander Motin 	atio_private_data_t *atp;
1065523ea374SAlexander Motin 
1066523ea374SAlexander Motin 	LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1067523ea374SAlexander Motin 		if (atp->tag == tag)
10682df76c16SMatt Jacob 			return (atp);
10692df76c16SMatt Jacob 	}
107053036e92SMatt Jacob 	return (NULL);
107153036e92SMatt Jacob }
107253036e92SMatt Jacob 
10732df76c16SMatt Jacob static ISP_INLINE void
10742df76c16SMatt Jacob isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
10752df76c16SMatt Jacob {
1076387d8239SMatt Jacob 	if (atp->ests) {
1077387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
1078387d8239SMatt Jacob 	}
1079523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
1080387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
1081523ea374SAlexander Motin 	LIST_INSERT_HEAD(&tptr->atfree, atp, next);
10822df76c16SMatt Jacob }
10832df76c16SMatt Jacob 
10842df76c16SMatt Jacob static void
10852df76c16SMatt Jacob isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
10862df76c16SMatt Jacob {
10872df76c16SMatt Jacob 	atio_private_data_t *atp;
10882df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
10892df76c16SMatt Jacob 
10902df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
109194dff771SMatt Jacob 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
109294dff771SMatt Jacob 		    atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
10932df76c16SMatt Jacob 	}
10942df76c16SMatt Jacob }
10952df76c16SMatt Jacob 
10962df76c16SMatt Jacob 
10972df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
10982df76c16SMatt Jacob isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
10992df76c16SMatt Jacob {
11002df76c16SMatt Jacob 	inot_private_data_t *ntp;
11012df76c16SMatt Jacob 	ntp = tptr->ntfree;
11022df76c16SMatt Jacob 	if (ntp) {
11032df76c16SMatt Jacob 		tptr->ntfree = ntp->next;
11042df76c16SMatt Jacob 	}
11052df76c16SMatt Jacob 	return (ntp);
11062df76c16SMatt Jacob }
11072df76c16SMatt Jacob 
11082df76c16SMatt Jacob static ISP_INLINE inot_private_data_t *
11092df76c16SMatt Jacob isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
11102df76c16SMatt Jacob {
11112df76c16SMatt Jacob 	inot_private_data_t *ntp;
11122df76c16SMatt Jacob 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
11132df76c16SMatt Jacob 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
11142df76c16SMatt Jacob 			return (ntp);
11152df76c16SMatt Jacob 		}
11162df76c16SMatt Jacob 	}
11172df76c16SMatt Jacob 	return (NULL);
11182df76c16SMatt Jacob }
11192df76c16SMatt Jacob 
11202df76c16SMatt Jacob static ISP_INLINE void
11212df76c16SMatt Jacob isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
11222df76c16SMatt Jacob {
11232df76c16SMatt Jacob 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
11242df76c16SMatt Jacob 	ntp->next = tptr->ntfree;
11252df76c16SMatt Jacob 	tptr->ntfree = ntp;
11262df76c16SMatt Jacob }
11272df76c16SMatt Jacob 
1128d81ba9d5SMatt Jacob static cam_status
11292df76c16SMatt Jacob create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1130d81ba9d5SMatt Jacob {
1131d81ba9d5SMatt Jacob 	cam_status status;
1132d81ba9d5SMatt Jacob 	lun_id_t lun;
11332df76c16SMatt Jacob 	struct tslist *lhp;
11342df76c16SMatt Jacob 	tstate_t *tptr;
11352df76c16SMatt Jacob 	int i;
1136d81ba9d5SMatt Jacob 
1137d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
11382df76c16SMatt Jacob 	if (lun != CAM_LUN_WILDCARD) {
11392e4637cdSMatt Jacob 		if (lun >= ISP_MAX_LUNS(isp)) {
1140d81ba9d5SMatt Jacob 			return (CAM_LUN_INVALID);
1141d81ba9d5SMatt Jacob 		}
11422df76c16SMatt Jacob 	}
1143a1bc34c6SMatt Jacob 	if (is_lun_enabled(isp, bus, lun)) {
1144d81ba9d5SMatt Jacob 		return (CAM_LUN_ALRDY_ENA);
1145d81ba9d5SMatt Jacob 	}
1146e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
11472df76c16SMatt Jacob 	if (tptr == NULL) {
1148d81ba9d5SMatt Jacob 		return (CAM_RESRC_UNAVAIL);
1149d81ba9d5SMatt Jacob 	}
1150315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
11512df76c16SMatt Jacob 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1152d81ba9d5SMatt Jacob 	if (status != CAM_REQ_CMP) {
11532df76c16SMatt Jacob 		free(tptr, M_DEVBUF);
1154d81ba9d5SMatt Jacob 		return (status);
1155d81ba9d5SMatt Jacob 	}
11562df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
11572df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
115894dff771SMatt Jacob 	TAILQ_INIT(&tptr->waitq);
1159523ea374SAlexander Motin 	LIST_INIT(&tptr->atfree);
1160523ea374SAlexander Motin 	for (i = ATPDPSIZE-1; i >= 0; i--)
1161523ea374SAlexander Motin 		LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1162523ea374SAlexander Motin 	for (i = 0; i < ATPDPHASHSIZE; i++)
1163523ea374SAlexander Motin 		LIST_INIT(&tptr->atused[i]);
1164523ea374SAlexander Motin 	for (i = 0; i < ATPDPSIZE-1; i++)
11652df76c16SMatt Jacob 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
11662df76c16SMatt Jacob 	tptr->ntfree = tptr->ntpool;
11672df76c16SMatt Jacob 	tptr->hold = 1;
1168315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
11692df76c16SMatt Jacob 	SLIST_INSERT_HEAD(lhp, tptr, next);
11702df76c16SMatt Jacob 	*rslt = tptr;
11712df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1172d81ba9d5SMatt Jacob 	return (CAM_REQ_CMP);
1173d81ba9d5SMatt Jacob }
1174d81ba9d5SMatt Jacob 
11752df76c16SMatt Jacob static ISP_INLINE void
11769cd7268eSMatt Jacob destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1177d81ba9d5SMatt Jacob {
1178344aebe2SMatt Jacob 	union ccb *ccb;
11792df76c16SMatt Jacob 	struct tslist *lhp;
1180e2873b76SMatt Jacob 
118145210a25SAlexander Motin 	KASSERT((tptr->hold != 0), ("tptr is not held"));
118245210a25SAlexander Motin 	KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1183344aebe2SMatt Jacob 	do {
1184344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1185344aebe2SMatt Jacob 		if (ccb) {
1186344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1187344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1188344aebe2SMatt Jacob 			xpt_done(ccb);
1189344aebe2SMatt Jacob 		}
1190344aebe2SMatt Jacob 	} while (ccb);
1191344aebe2SMatt Jacob 	do {
1192344aebe2SMatt Jacob 		ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1193344aebe2SMatt Jacob 		if (ccb) {
1194344aebe2SMatt Jacob 			SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1195344aebe2SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1196344aebe2SMatt Jacob 			xpt_done(ccb);
1197344aebe2SMatt Jacob 		}
1198344aebe2SMatt Jacob 	} while (ccb);
1199315a4d6fSAlexander Motin 	ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
12002df76c16SMatt Jacob 	SLIST_REMOVE(lhp, tptr, tstate, next);
1201e2873b76SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
12022df76c16SMatt Jacob 	xpt_free_path(tptr->owner);
1203d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
1204d81ba9d5SMatt Jacob }
1205d81ba9d5SMatt Jacob 
12065d571944SMatt Jacob /*
12072df76c16SMatt Jacob  * Enable a lun.
12085d571944SMatt Jacob  */
12092df76c16SMatt Jacob static void
12102df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1211d81ba9d5SMatt Jacob {
12121fd47020SMatt Jacob 	tstate_t *tptr = NULL;
12132df76c16SMatt Jacob 	int bus, tm_enabled, target_role;
12142df76c16SMatt Jacob 	target_id_t target;
1215d81ba9d5SMatt Jacob 	lun_id_t lun;
1216d81ba9d5SMatt Jacob 
1217e2873b76SMatt Jacob 
1218746e9c85SMatt Jacob 	/*
12192df76c16SMatt Jacob 	 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1220746e9c85SMatt Jacob 	 */
12212df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
12222df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
12232df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
1224e2873b76SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "enabling lun %u\n", lun);
12252df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1226d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_TID_INVALID;
12272df76c16SMatt Jacob 		xpt_done(ccb);
12282df76c16SMatt Jacob 		return;
1229d81ba9d5SMatt Jacob 	}
12302df76c16SMatt Jacob 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
12312df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12322df76c16SMatt Jacob 		xpt_done(ccb);
12332df76c16SMatt Jacob 		return;
12342df76c16SMatt Jacob 	}
12352df76c16SMatt Jacob 
12362df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
12372df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
12382df76c16SMatt Jacob 		xpt_done(ccb);
12392df76c16SMatt Jacob 		return;
12402df76c16SMatt Jacob 	}
12412df76c16SMatt Jacob 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
12422df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
12432df76c16SMatt Jacob 	}
12442df76c16SMatt Jacob 
12452df76c16SMatt Jacob 	/*
12462df76c16SMatt Jacob 	 * Wait until we're not busy with the lun enables subsystem
12472df76c16SMatt Jacob 	 */
1248e2873b76SMatt Jacob 	isp_tmlock(isp, "isp_enable_lun");
12492df76c16SMatt Jacob 
125064edff94SMatt Jacob 	/*
125164edff94SMatt Jacob 	 * This is as a good a place as any to check f/w capabilities.
125264edff94SMatt Jacob 	 */
12532df76c16SMatt Jacob 
12542df76c16SMatt Jacob 	if (IS_FC(isp)) {
12552df76c16SMatt Jacob 		if (ISP_CAP_TMODE(isp) == 0) {
12562df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
125764edff94SMatt Jacob 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
12582df76c16SMatt Jacob 			goto done;
125964edff94SMatt Jacob 		}
126064edff94SMatt Jacob 		/*
12612df76c16SMatt Jacob 		 * We *could* handle non-SCCLUN f/w, but we'd have to
12622df76c16SMatt Jacob 		 * dork with our already fragile enable/disable code.
126364edff94SMatt Jacob 		 */
12642df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp) == 0) {
12652df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1266746e9c85SMatt Jacob 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
12672df76c16SMatt Jacob 			goto done;
1268d81ba9d5SMatt Jacob 		}
1269d81ba9d5SMatt Jacob 
12702df76c16SMatt Jacob 		target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
12712df76c16SMatt Jacob 
127264edff94SMatt Jacob 	} else {
12732df76c16SMatt Jacob 		target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1274126ec864SMatt Jacob 	}
1275126ec864SMatt Jacob 
1276126ec864SMatt Jacob 	/*
12772df76c16SMatt Jacob 	 * Create the state pointer.
12782df76c16SMatt Jacob 	 * It should not already exist.
1279126ec864SMatt Jacob 	 */
1280a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
12812df76c16SMatt Jacob 	if (tptr) {
12822df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
12832df76c16SMatt Jacob 		goto done;
1284d81ba9d5SMatt Jacob 	}
12852df76c16SMatt Jacob 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
12862df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
12872df76c16SMatt Jacob 		goto done;
1288d81ba9d5SMatt Jacob 	}
1289d81ba9d5SMatt Jacob 
12905d571944SMatt Jacob 	/*
12912df76c16SMatt Jacob 	 * We have a tricky maneuver to perform here.
12922df76c16SMatt Jacob 	 *
12932df76c16SMatt Jacob 	 * If target mode isn't already enabled here,
12942df76c16SMatt Jacob 	 * *and* our current role includes target mode,
12952df76c16SMatt Jacob 	 * we enable target mode here.
12962df76c16SMatt Jacob 	 *
12975d571944SMatt Jacob 	 */
12982df76c16SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
12992df76c16SMatt Jacob 	if (tm_enabled == 0 && target_role != 0) {
13002df76c16SMatt Jacob 		if (isp_enable_target_mode(isp, bus)) {
130167ff51f1SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
13022df76c16SMatt Jacob 			destroy_lun_state(isp, tptr);
13032df76c16SMatt Jacob 			tptr = NULL;
13042df76c16SMatt Jacob 			goto done;
13055d571944SMatt Jacob 		}
13062df76c16SMatt Jacob 		tm_enabled = 1;
13072df76c16SMatt Jacob 	}
13082df76c16SMatt Jacob 
13092df76c16SMatt Jacob 	/*
13102df76c16SMatt Jacob 	 * Now check to see whether this bus is in target mode already.
13112df76c16SMatt Jacob 	 *
13122df76c16SMatt Jacob 	 * If not, a later role change into target mode will finish the job.
13132df76c16SMatt Jacob 	 */
13142df76c16SMatt Jacob 	if (tm_enabled == 0) {
13152df76c16SMatt Jacob 		ISP_SET_PC(isp, bus, tm_enable_defer, 1);
13162df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
1317e2873b76SMatt Jacob 		xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1318387d8239SMatt Jacob 		goto done1;
13192df76c16SMatt Jacob 	}
13202df76c16SMatt Jacob 
13212df76c16SMatt Jacob 	/*
13222df76c16SMatt Jacob 	 * Enable the lun.
13232df76c16SMatt Jacob 	 */
13242df76c16SMatt Jacob 	ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
13252df76c16SMatt Jacob 
13262df76c16SMatt Jacob done:
1327e2873b76SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP)  {
1328e2873b76SMatt Jacob 		if (tptr) {
13292df76c16SMatt Jacob 			destroy_lun_state(isp, tptr);
13302df76c16SMatt Jacob 			tptr = NULL;
13312df76c16SMatt Jacob 		}
1332e2873b76SMatt Jacob 	} else {
1333e2873b76SMatt Jacob 		tptr->enabled = 1;
1334e2873b76SMatt Jacob 	}
1335387d8239SMatt Jacob done1:
13362df76c16SMatt Jacob 	if (tptr) {
13372df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
13382df76c16SMatt Jacob 	}
1339e2873b76SMatt Jacob 
1340e2873b76SMatt Jacob 	/*
1341e2873b76SMatt Jacob 	 * And we're outta here....
1342e2873b76SMatt Jacob 	 */
1343e2873b76SMatt Jacob 	isp_tmunlk(isp);
13442df76c16SMatt Jacob 	xpt_done(ccb);
13452df76c16SMatt Jacob }
13462df76c16SMatt Jacob 
1347e2873b76SMatt Jacob static cam_status
13482df76c16SMatt Jacob isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
13492df76c16SMatt Jacob {
1350e2873b76SMatt Jacob 	tstate_t *tptr = NULL;
1351e2873b76SMatt Jacob 	struct tslist *lhp;
1352e2873b76SMatt Jacob 	int i, n;
1353e2873b76SMatt Jacob 
1354387d8239SMatt Jacob 
1355e2873b76SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, i);
1356e2873b76SMatt Jacob 	if (i == 1) {
1357e2873b76SMatt Jacob 		return (CAM_REQ_CMP);
1358e2873b76SMatt Jacob 	}
1359e2873b76SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enable_defer, i);
1360e2873b76SMatt Jacob 	if (i == 0) {
1361e2873b76SMatt Jacob 		return (CAM_REQ_CMP);
1362e2873b76SMatt Jacob 	}
13632df76c16SMatt Jacob 	/*
1364e2873b76SMatt Jacob 	 * If this succeeds, it will set tm_enable
13652df76c16SMatt Jacob 	 */
1366e2873b76SMatt Jacob 	if (isp_enable_target_mode(isp, bus)) {
1367e2873b76SMatt Jacob 		return (CAM_REQ_CMP_ERR);
1368e2873b76SMatt Jacob 	}
1369e2873b76SMatt Jacob 	isp_tmlock(isp, "isp_enable_deferred_luns");
1370e2873b76SMatt Jacob 	for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1371e2873b76SMatt Jacob 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1372e2873b76SMatt Jacob 		SLIST_FOREACH(tptr, lhp, next) {
1373e2873b76SMatt Jacob 			tptr->hold++;
1374e2873b76SMatt Jacob 			if (tptr->enabled == 0) {
1375315a4d6fSAlexander Motin 				if (isp_enable_deferred(isp, bus, tptr->ts_lun) == CAM_REQ_CMP) {
1376e2873b76SMatt Jacob 					tptr->enabled = 1;
1377e2873b76SMatt Jacob 					n++;
1378e2873b76SMatt Jacob 				}
1379e2873b76SMatt Jacob 			} else {
1380e2873b76SMatt Jacob 				n++;
1381e2873b76SMatt Jacob 			}
1382e2873b76SMatt Jacob 			tptr->hold--;
1383e2873b76SMatt Jacob 		}
1384e2873b76SMatt Jacob 	}
1385e2873b76SMatt Jacob 	isp_tmunlk(isp);
1386e2873b76SMatt Jacob 	if (n == 0) {
1387e2873b76SMatt Jacob 		return (CAM_REQ_CMP_ERR);
1388e2873b76SMatt Jacob 	}
1389e2873b76SMatt Jacob 	ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1390e2873b76SMatt Jacob 	return (CAM_REQ_CMP);
13912df76c16SMatt Jacob }
13922df76c16SMatt Jacob 
1393e2873b76SMatt Jacob static cam_status
13942df76c16SMatt Jacob isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
13952df76c16SMatt Jacob {
13962df76c16SMatt Jacob 	cam_status status;
1397387d8239SMatt Jacob 	int luns_already_enabled;
13982df76c16SMatt Jacob 
1399387d8239SMatt Jacob 	ISP_GET_PC(isp, bus, tm_luns_enabled, luns_already_enabled);
1400123055f0SNathan Whitehorn 	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %jx luns_enabled %d", __func__, bus, (uintmax_t)lun, luns_already_enabled);
1401e2873b76SMatt Jacob 	if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
14022df76c16SMatt Jacob 		status = CAM_REQ_CMP;
14032df76c16SMatt Jacob 	} else {
14042df76c16SMatt Jacob 		int cmd_cnt, not_cnt;
14052df76c16SMatt Jacob 
14062df76c16SMatt Jacob 		if (IS_23XX(isp)) {
14072df76c16SMatt Jacob 			cmd_cnt = DFLT_CMND_CNT;
14082df76c16SMatt Jacob 			not_cnt = DFLT_INOT_CNT;
14092df76c16SMatt Jacob 		} else {
14102df76c16SMatt Jacob 			cmd_cnt = 64;
14112df76c16SMatt Jacob 			not_cnt = 8;
14122df76c16SMatt Jacob 		}
14132df76c16SMatt Jacob 		status = CAM_REQ_INPROG;
14142df76c16SMatt Jacob 		isp->isp_osinfo.rptr = &status;
1415387d8239SMatt Jacob 		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun == CAM_LUN_WILDCARD? 0 : lun, cmd_cnt, not_cnt)) {
14162df76c16SMatt Jacob 			status = CAM_RESRC_UNAVAIL;
14172df76c16SMatt Jacob 		} else {
14182df76c16SMatt Jacob 			mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
14192df76c16SMatt Jacob 		}
14202df76c16SMatt Jacob 		isp->isp_osinfo.rptr = NULL;
14212df76c16SMatt Jacob 	}
14222df76c16SMatt Jacob 	if (status == CAM_REQ_CMP) {
14232df76c16SMatt Jacob 		ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1424123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %jx now enabled for target mode", bus, (uintmax_t)lun);
14252df76c16SMatt Jacob 	}
14262df76c16SMatt Jacob 	return (status);
14272df76c16SMatt Jacob }
14282df76c16SMatt Jacob 
14292df76c16SMatt Jacob static void
14302df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
14312df76c16SMatt Jacob {
14322df76c16SMatt Jacob 	tstate_t *tptr = NULL;
14332df76c16SMatt Jacob 	int bus;
14342df76c16SMatt Jacob 	cam_status status;
14352df76c16SMatt Jacob 	target_id_t target;
14362df76c16SMatt Jacob 	lun_id_t lun;
14372df76c16SMatt Jacob 
14382df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
14392df76c16SMatt Jacob 	target = ccb->ccb_h.target_id;
14402df76c16SMatt Jacob 	lun = ccb->ccb_h.target_lun;
1441e2873b76SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "disabling lun %u\n", lun);
14422df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && target != 0) {
14432df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_TID_INVALID;
14442df76c16SMatt Jacob 		xpt_done(ccb);
14452df76c16SMatt Jacob 		return;
14462df76c16SMatt Jacob 	}
1447e2873b76SMatt Jacob 
14482df76c16SMatt Jacob 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
14492df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
14502df76c16SMatt Jacob 		xpt_done(ccb);
14512df76c16SMatt Jacob 		return;
14522df76c16SMatt Jacob 	}
14532df76c16SMatt Jacob 
14542df76c16SMatt Jacob 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
14552df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
14562df76c16SMatt Jacob 		xpt_done(ccb);
14572df76c16SMatt Jacob 		return;
14582df76c16SMatt Jacob 	}
14592df76c16SMatt Jacob 
14602df76c16SMatt Jacob 	/*
14612df76c16SMatt Jacob 	 * See if we're busy disabling a lun now.
14622df76c16SMatt Jacob 	 */
1463e2873b76SMatt Jacob 	isp_tmlock(isp, "isp_disable_lun");
146445210a25SAlexander Motin 	status = CAM_REQ_INPROG;
14652df76c16SMatt Jacob 
14662df76c16SMatt Jacob 	/*
14672df76c16SMatt Jacob 	 * Find the state pointer.
14682df76c16SMatt Jacob 	 */
14692df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
147045210a25SAlexander Motin 		status = CAM_PATH_INVALID;
14712df76c16SMatt Jacob 		goto done;
14722df76c16SMatt Jacob 	}
14732df76c16SMatt Jacob 
14742df76c16SMatt Jacob 	/*
14752df76c16SMatt Jacob 	 * If we're a 24XX card, we're done.
14762df76c16SMatt Jacob 	 */
14772df76c16SMatt Jacob 	if (IS_24XX(isp)) {
14782df76c16SMatt Jacob 		status = CAM_REQ_CMP;
14792df76c16SMatt Jacob 		goto done;
14802df76c16SMatt Jacob 	}
14812df76c16SMatt Jacob 
14822df76c16SMatt Jacob 	/*
14832df76c16SMatt Jacob 	 * For SCC FW, we only deal with lun zero.
14842df76c16SMatt Jacob 	 */
1485387d8239SMatt Jacob 	if (IS_FC(isp) && lun > 0) {
1486387d8239SMatt Jacob 		status = CAM_REQ_CMP;
1487387d8239SMatt Jacob 		goto done;
14882df76c16SMatt Jacob 	}
14892df76c16SMatt Jacob 	isp->isp_osinfo.rptr = &status;
14902df76c16SMatt Jacob 	if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
14912df76c16SMatt Jacob 		status = CAM_RESRC_UNAVAIL;
14922df76c16SMatt Jacob 	} else {
14932df76c16SMatt Jacob 		mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
14942df76c16SMatt Jacob 	}
1495e2873b76SMatt Jacob 	isp->isp_osinfo.rptr = NULL;
14962df76c16SMatt Jacob done:
1497e2873b76SMatt Jacob 	if (status == CAM_REQ_CMP) {
1498e2873b76SMatt Jacob 		tptr->enabled = 0;
1499e2873b76SMatt Jacob 		/*
1500387d8239SMatt Jacob 		 * If we have no more luns enabled for this bus,
1501387d8239SMatt Jacob 		 * delete all tracked wwns for it (if we are FC),
1502e2873b76SMatt Jacob 		 * and disable target mode.
1503e2873b76SMatt Jacob 		 */
1504e2873b76SMatt Jacob 		if (is_any_lun_enabled(isp, bus) == 0) {
1505e2873b76SMatt Jacob 			isp_del_all_wwn_entries(isp, bus);
1506e2873b76SMatt Jacob 			if (isp_disable_target_mode(isp, bus)) {
1507e2873b76SMatt Jacob 				status = CAM_REQ_CMP_ERR;
1508e2873b76SMatt Jacob 			}
1509e2873b76SMatt Jacob 		}
1510e2873b76SMatt Jacob 	}
151145210a25SAlexander Motin 	ccb->ccb_h.status = status;
15122df76c16SMatt Jacob 	if (status == CAM_REQ_CMP) {
1513e95725cbSMatt Jacob 		destroy_lun_state(isp, tptr);
1514344aebe2SMatt Jacob 		xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1515e2873b76SMatt Jacob 	} else {
1516e2873b76SMatt Jacob 		if (tptr)
1517e2873b76SMatt Jacob 			rls_lun_statep(isp, tptr);
15182df76c16SMatt Jacob 	}
1519e2873b76SMatt Jacob 	isp_tmunlk(isp);
15202df76c16SMatt Jacob 	xpt_done(ccb);
15212df76c16SMatt Jacob }
15222df76c16SMatt Jacob 
15232df76c16SMatt Jacob static int
15242df76c16SMatt Jacob isp_enable_target_mode(ispsoftc_t *isp, int bus)
15252df76c16SMatt Jacob {
1526e2873b76SMatt Jacob 	int tm_enabled;
15272df76c16SMatt Jacob 
1528e2873b76SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1529e2873b76SMatt Jacob 	if (tm_enabled != 0) {
15302df76c16SMatt Jacob 		return (0);
15312df76c16SMatt Jacob 	}
15322df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
15332df76c16SMatt Jacob 		mbreg_t mbs;
15342df76c16SMatt Jacob 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
15352df76c16SMatt Jacob 		mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
15362df76c16SMatt Jacob 		mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
15372df76c16SMatt Jacob 		mbs.param[2] = bus << 7;
15382df76c16SMatt Jacob 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1539e2873b76SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
15402df76c16SMatt Jacob 			return (EIO);
15412df76c16SMatt Jacob 		}
15422df76c16SMatt Jacob 	}
15432df76c16SMatt Jacob 	ISP_SET_PC(isp, bus, tm_enabled, 1);
1544e2873b76SMatt Jacob 	isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
15452df76c16SMatt Jacob 	return (0);
15462df76c16SMatt Jacob }
15472df76c16SMatt Jacob 
15482df76c16SMatt Jacob static int
15492df76c16SMatt Jacob isp_disable_target_mode(ispsoftc_t *isp, int bus)
15502df76c16SMatt Jacob {
1551e2873b76SMatt Jacob 	int tm_enabled;
15522df76c16SMatt Jacob 
1553e2873b76SMatt Jacob 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1554e2873b76SMatt Jacob 	if (tm_enabled == 0) {
15552df76c16SMatt Jacob 		return (0);
15562df76c16SMatt Jacob 	}
15572df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
15582df76c16SMatt Jacob 		mbreg_t mbs;
15592df76c16SMatt Jacob 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
15602df76c16SMatt Jacob 		mbs.param[2] = bus << 7;
15612df76c16SMatt Jacob 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1562e2873b76SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
15632df76c16SMatt Jacob 			return (EIO);
15642df76c16SMatt Jacob 		}
15652df76c16SMatt Jacob 	}
15662df76c16SMatt Jacob 	ISP_SET_PC(isp, bus, tm_enabled, 0);
1567387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGINFO, "Target Role disabled on Bus %d", bus);
15682df76c16SMatt Jacob 	return (0);
15692df76c16SMatt Jacob }
15705d571944SMatt Jacob 
157167ff51f1SMatt Jacob static void
15729cd7268eSMatt Jacob isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
157367ff51f1SMatt Jacob {
15742df76c16SMatt Jacob 	uint32_t *rptr;
1575d81ba9d5SMatt Jacob 
15762df76c16SMatt Jacob 	rptr = isp->isp_osinfo.rptr;
157767ff51f1SMatt Jacob 	if (lep->le_status != LUN_OK) {
15782df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
15792df76c16SMatt Jacob 		if (rptr) {
15802df76c16SMatt Jacob 			*rptr = CAM_REQ_CMP_ERR;
15812df76c16SMatt Jacob 			wakeup_one(rptr);
15822df76c16SMatt Jacob 		}
158367ff51f1SMatt Jacob 	} else {
15842df76c16SMatt Jacob 		if (rptr) {
15852df76c16SMatt Jacob 			*rptr = CAM_REQ_CMP;
15862df76c16SMatt Jacob 			wakeup_one(rptr);
158767ff51f1SMatt Jacob 		}
1588126ec864SMatt Jacob 	}
1589d81ba9d5SMatt Jacob }
1590d81ba9d5SMatt Jacob 
15919cd7268eSMatt Jacob static void
1592387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1593d81ba9d5SMatt Jacob {
159494dff771SMatt Jacob 	int fctape, sendstatus, resid;
15952df76c16SMatt Jacob 	tstate_t *tptr;
1596387d8239SMatt Jacob 	fcparam *fcp;
15972df76c16SMatt Jacob 	atio_private_data_t *atp;
159894dff771SMatt Jacob 	struct ccb_scsiio *cso;
159994dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
16001dae40ebSMatt Jacob 	uint8_t local[QENTRY_LEN];
1601d81ba9d5SMatt Jacob 
160294dff771SMatt Jacob 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
160394dff771SMatt Jacob 	if (tptr == NULL) {
160494dff771SMatt Jacob 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
160594dff771SMatt Jacob 		if (tptr == NULL) {
160694dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
160794dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
160894dff771SMatt Jacob 			xpt_done(ccb);
160994dff771SMatt Jacob 			return;
161094dff771SMatt Jacob 		}
161194dff771SMatt Jacob 	}
161294dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
161394dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
161494dff771SMatt Jacob 
161594dff771SMatt Jacob 	switch (how) {
161694dff771SMatt Jacob 	case FROM_TIMER:
161794dff771SMatt Jacob 	case FROM_CAM:
16182df76c16SMatt Jacob 		/*
161994dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
16202df76c16SMatt Jacob 		 */
162194dff771SMatt Jacob 		TAILQ_INSERT_TAIL(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
162294dff771SMatt Jacob 		break;
162394dff771SMatt Jacob 	case FROM_SRR:
162494dff771SMatt Jacob 	case FROM_CTIO_DONE:
162594dff771SMatt Jacob 		TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
162694dff771SMatt Jacob 		break;
162794dff771SMatt Jacob 	}
162894dff771SMatt Jacob 
162994dff771SMatt Jacob 	while (TAILQ_FIRST(&tptr->waitq) != NULL) {
163094dff771SMatt Jacob 		ccb = (union ccb *) TAILQ_FIRST(&tptr->waitq);
163194dff771SMatt Jacob 		TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
163294dff771SMatt Jacob 
163394dff771SMatt Jacob 		cso = &ccb->csio;
1634387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
1635387d8239SMatt Jacob 		if (xfrlen == 0) {
16362df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1637387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
16382df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
16392df76c16SMatt Jacob 				xpt_done(ccb);
164094dff771SMatt Jacob 				continue;
16412df76c16SMatt Jacob 			}
16422df76c16SMatt Jacob 		}
16432df76c16SMatt Jacob 
1644523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, cso->tag_id);
16452df76c16SMatt Jacob 		if (atp == NULL) {
164694dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
16472df76c16SMatt Jacob 			isp_dump_atpd(isp, tptr);
16482df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
16492df76c16SMatt Jacob 			xpt_done(ccb);
165094dff771SMatt Jacob 			continue;
16512df76c16SMatt Jacob 		}
1652387d8239SMatt Jacob 
1653387d8239SMatt Jacob 		/*
1654387d8239SMatt Jacob 		 * Is this command a dead duck?
1655387d8239SMatt Jacob 		 */
16562df76c16SMatt Jacob 		if (atp->dead) {
165794dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
16582df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
16592df76c16SMatt Jacob 			xpt_done(ccb);
166094dff771SMatt Jacob 			continue;
16612df76c16SMatt Jacob 		}
16622df76c16SMatt Jacob 
16632df76c16SMatt Jacob 		/*
16642df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
16652df76c16SMatt Jacob 		 */
1666387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1667387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
166894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
16692df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
16702df76c16SMatt Jacob 			xpt_done(ccb);
167194dff771SMatt Jacob 			continue;
16722df76c16SMatt Jacob 		}
16732df76c16SMatt Jacob 
16742df76c16SMatt Jacob 		/*
167594dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
167694dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
1677387d8239SMatt Jacob 		 */
167894dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
167994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
168094dff771SMatt Jacob 			TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
168194dff771SMatt Jacob 			break;
1682387d8239SMatt Jacob 		}
1683387d8239SMatt Jacob 
1684d81ba9d5SMatt Jacob 		/*
1685387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1686d81ba9d5SMatt Jacob 		 */
1687387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1688387d8239SMatt Jacob 			fctape = 1;
1689387d8239SMatt Jacob 		} else {
1690387d8239SMatt Jacob 			fctape = 0;
1691387d8239SMatt Jacob 		}
1692387d8239SMatt Jacob 
1693387d8239SMatt Jacob 		/*
1694387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1695387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1696387d8239SMatt Jacob 		 */
1697387d8239SMatt Jacob 		if (atp->sendst) {
169894dff771SMatt Jacob 			isp_prt(isp, ISP_LOGTINFO, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
169994dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1700387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1701387d8239SMatt Jacob 			atp->sendst = 0;
1702387d8239SMatt Jacob 		}
1703387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1704387d8239SMatt Jacob 			sendstatus = 1;
1705387d8239SMatt Jacob 		} else {
1706387d8239SMatt Jacob 			sendstatus = 0;
1707387d8239SMatt Jacob 		}
1708387d8239SMatt Jacob 
1709387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
171094dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1711387d8239SMatt Jacob 			/*
1712387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1713387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1714387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1715387d8239SMatt Jacob 			 */
1716387d8239SMatt Jacob 			sense_length = cso->sense_len;
1717387d8239SMatt Jacob 
1718387d8239SMatt Jacob 			/*
1719387d8239SMatt Jacob 			 * This 'cannot' happen
1720387d8239SMatt Jacob 			 */
1721387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1722387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1723387d8239SMatt Jacob 			}
1724387d8239SMatt Jacob 		} else {
1725387d8239SMatt Jacob 			sense_length = 0;
1726387d8239SMatt Jacob 		}
1727387d8239SMatt Jacob 
172894dff771SMatt Jacob 		memset(local, 0, QENTRY_LEN);
172994dff771SMatt Jacob 
173094dff771SMatt Jacob 		/*
173194dff771SMatt Jacob 		 * Check for overflow
173294dff771SMatt Jacob 		 */
173394dff771SMatt Jacob 		tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
173494dff771SMatt Jacob 		if (tmp > atp->orig_datalen) {
173594dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
173694dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
173794dff771SMatt Jacob 			xpt_done(ccb);
173894dff771SMatt Jacob 			continue;
173994dff771SMatt Jacob 		}
1740387d8239SMatt Jacob 
17412df76c16SMatt Jacob 		if (IS_24XX(isp)) {
17422df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
1743d81ba9d5SMatt Jacob 
17442df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
17452df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
174694dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
174794dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
17482df76c16SMatt Jacob 			cto->ct_nphdl = atp->nphdl;
17492df76c16SMatt Jacob 			cto->ct_rxid = atp->tag;
17502df76c16SMatt Jacob 			cto->ct_iid_lo = atp->portid;
17512df76c16SMatt Jacob 			cto->ct_iid_hi = atp->portid >> 16;
17522df76c16SMatt Jacob 			cto->ct_oxid = atp->oxid;
17532df76c16SMatt Jacob 			cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
17542df76c16SMatt Jacob 			cto->ct_timeout = 120;
17552df76c16SMatt Jacob 			cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1756387d8239SMatt Jacob 
1757387d8239SMatt Jacob 			/*
1758387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
17596f7aeb5fSMatt Jacob 			 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1760387d8239SMatt Jacob 			 *
17618b382bc2SMatt Jacob 			 * Mode 2, status, no data. We have to use this in the case that
17628b382bc2SMatt Jacob 			 * the sense data won't fit into a ct7_entry_t.
1763387d8239SMatt Jacob 			 *
1764387d8239SMatt Jacob 			 */
1765387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1766387d8239SMatt Jacob 				cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
176794dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1768387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN_24XX) {
1769387d8239SMatt Jacob 					if (resid < 0) {
1770387d8239SMatt Jacob 						cto->ct_resid = -resid;
1771387d8239SMatt Jacob 					} else if (resid > 0) {
1772387d8239SMatt Jacob 						cto->ct_resid = resid;
17732df76c16SMatt Jacob 					}
1774387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE1;
1775387d8239SMatt Jacob 					cto->ct_scsi_status = cso->scsi_status;
1776387d8239SMatt Jacob 					if (resid < 0) {
1777387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1778387d8239SMatt Jacob 					} else if (resid > 0) {
1779387d8239SMatt Jacob 						cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1780387d8239SMatt Jacob 					}
1781387d8239SMatt Jacob 					if (fctape) {
1782387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1783387d8239SMatt Jacob 					}
1784387d8239SMatt Jacob 					if (sense_length) {
17852df76c16SMatt Jacob 						cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1786387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1787387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
17882df76c16SMatt Jacob 					}
17892df76c16SMatt Jacob 				} else {
1790387d8239SMatt Jacob 					bus_addr_t addr;
1791387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1792387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1793387d8239SMatt Jacob 
1794387d8239SMatt Jacob 					if (atp->ests == NULL) {
1795387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1796387d8239SMatt Jacob 						if (atp->ests == NULL) {
179794dff771SMatt Jacob 							TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
179894dff771SMatt Jacob 							break;
1799387d8239SMatt Jacob 						}
1800387d8239SMatt Jacob 					}
1801387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1802387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1803387d8239SMatt Jacob 					if (fctape) {
1804387d8239SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1805387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1806387d8239SMatt Jacob 					}
1807387d8239SMatt Jacob 					cto->ct_flags |= CT7_FLAG_MODE2;
1808387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1809387d8239SMatt Jacob 					if (resid < 0) {
1810387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1811387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1812387d8239SMatt Jacob 					} else if (resid > 0) {
1813387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1814387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1815387d8239SMatt Jacob 					}
1816387d8239SMatt Jacob 					if (sense_length) {
1817387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1818387d8239SMatt Jacob 						cto->ct_senselen = sense_length;
1819387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1820387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1821387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1822387d8239SMatt Jacob 					} else {
1823387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1824387d8239SMatt Jacob 					}
1825387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1826387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1827387d8239SMatt Jacob 					}
1828387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1829387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
183094dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1831387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1832387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1833387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1834387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1835387d8239SMatt Jacob 					cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1836387d8239SMatt Jacob 				}
1837387d8239SMatt Jacob 				if (sense_length) {
183894dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
183994dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
184094dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1841387d8239SMatt Jacob 				} else {
184294dff771SMatt Jacob 					isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
184394dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1844387d8239SMatt Jacob 				}
1845387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
1846387d8239SMatt Jacob 			}
1847387d8239SMatt Jacob 
1848387d8239SMatt Jacob 			/*
1849387d8239SMatt Jacob 			 * Mode 0 data transfers, *possibly* with status.
1850387d8239SMatt Jacob 			 */
1851387d8239SMatt Jacob 			if (xfrlen != 0) {
18522df76c16SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE0;
18532df76c16SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
18542df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_IN;
18552df76c16SMatt Jacob 				} else {
18562df76c16SMatt Jacob 					cto->ct_flags |= CT7_DATA_OUT;
18572df76c16SMatt Jacob 				}
1858387d8239SMatt Jacob 
185994dff771SMatt Jacob 				cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1860387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
1861387d8239SMatt Jacob 
1862387d8239SMatt Jacob #ifdef	DEBUG
1863387d8239SMatt Jacob 				if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1864387d8239SMatt Jacob 					isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1865387d8239SMatt Jacob 					ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1866387d8239SMatt Jacob 					cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1867387d8239SMatt Jacob 				}
1868387d8239SMatt Jacob #endif
1869387d8239SMatt Jacob 				if (sendstatus) {
187094dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
187194dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1872387d8239SMatt Jacob 						cto->ct_flags |= CT7_SENDSTATUS;
1873387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
187494dff771SMatt Jacob 						if (fctape) {
187594dff771SMatt Jacob 							cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
187694dff771SMatt Jacob 						}
1877387d8239SMatt Jacob 					} else {
1878387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
187994dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
18802df76c16SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
18812df76c16SMatt Jacob 					}
1882387d8239SMatt Jacob 				} else {
1883387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
1884387d8239SMatt Jacob 				}
188594dff771SMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
188694dff771SMatt Jacob 				    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1887387d8239SMatt Jacob 			}
18882df76c16SMatt Jacob 		} else if (IS_FC(isp)) {
18894fd13c1bSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
189000a8e174SMatt Jacob 
1891387d8239SMatt Jacob 			if (isp->isp_osinfo.sixtyfourbit)
1892387d8239SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1893387d8239SMatt Jacob 			else
1894d81ba9d5SMatt Jacob 				cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1895d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
189694dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
189794dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
18982df76c16SMatt Jacob 			if (ISP_CAP_2KLOGIN(isp) == 0) {
189929f76675SMatt Jacob 				((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
190029f76675SMatt Jacob 			} else {
190100a8e174SMatt Jacob 				cto->ct_iid = cso->init_id;
19022df76c16SMatt Jacob 				if (ISP_CAP_SCCFW(isp) == 0) {
1903d81ba9d5SMatt Jacob 					cto->ct_lun = ccb->ccb_h.target_lun;
19042ad50ca5SMatt Jacob 				}
190529f76675SMatt Jacob 			}
1906387d8239SMatt Jacob 			cto->ct_timeout = 10;
190700a8e174SMatt Jacob 			cto->ct_rxid = cso->tag_id;
1908387d8239SMatt Jacob 
1909387d8239SMatt Jacob 			/*
1910387d8239SMatt Jacob 			 * Mode 1, status, no data. Only possible when we are sending status, have
1911387d8239SMatt Jacob 			 * no data to transfer, and the sense length can fit in the ct7_entry.
1912387d8239SMatt Jacob 			 *
191396240c89SEitan Adler 			 * Mode 2, status, no data. We have to use this in the case the response
1914387d8239SMatt Jacob 			 * length won't fit into a ct2_entry_t.
1915387d8239SMatt Jacob 			 *
1916387d8239SMatt Jacob 			 * We'll fill out this structure with information as if this were a
1917387d8239SMatt Jacob 			 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1918387d8239SMatt Jacob 			 * needed based upon this.
1919387d8239SMatt Jacob 			 */
1920387d8239SMatt Jacob 			if (sendstatus && xfrlen == 0) {
1921387d8239SMatt Jacob 				cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
192294dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1923387d8239SMatt Jacob 				if (sense_length <= MAXRESPLEN) {
1924387d8239SMatt Jacob 					if (resid < 0) {
1925387d8239SMatt Jacob 						cto->ct_resid = -resid;
1926387d8239SMatt Jacob 					} else if (resid > 0) {
1927387d8239SMatt Jacob 						cto->ct_resid = resid;
1928387d8239SMatt Jacob 					}
1929387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE1;
1930f48ce188SMatt Jacob 					cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1931387d8239SMatt Jacob 					if (resid < 0) {
19322df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1933387d8239SMatt Jacob 					} else if (resid > 0) {
19342df76c16SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1935f48ce188SMatt Jacob 					}
1936387d8239SMatt Jacob 					if (fctape) {
1937387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1938387d8239SMatt Jacob 					}
1939387d8239SMatt Jacob 					if (sense_length) {
194000a8e174SMatt Jacob 						cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1941387d8239SMatt Jacob 						cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1942387d8239SMatt Jacob 						memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
194300a8e174SMatt Jacob 					}
194400a8e174SMatt Jacob 				} else {
1945387d8239SMatt Jacob 					bus_addr_t addr;
1946387d8239SMatt Jacob 					char buf[XCMD_SIZE];
1947387d8239SMatt Jacob 					fcp_rsp_iu_t *rp;
1948387d8239SMatt Jacob 
1949387d8239SMatt Jacob 					if (atp->ests == NULL) {
1950387d8239SMatt Jacob 						atp->ests = isp_get_ecmd(isp);
1951387d8239SMatt Jacob 						if (atp->ests == NULL) {
195294dff771SMatt Jacob 							TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
195394dff771SMatt Jacob 							break;
1954387d8239SMatt Jacob 						}
1955387d8239SMatt Jacob 					}
1956387d8239SMatt Jacob 					memset(buf, 0, sizeof (buf));
1957387d8239SMatt Jacob 					rp = (fcp_rsp_iu_t *)buf;
1958387d8239SMatt Jacob 					if (fctape) {
1959387d8239SMatt Jacob 						cto->ct_flags |= CT2_CONFIRM;
1960387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_CONF_REQ;
1961387d8239SMatt Jacob 					}
1962387d8239SMatt Jacob 					cto->ct_flags |= CT2_FLAG_MODE2;
1963387d8239SMatt Jacob 	        			rp->fcp_rsp_scsi_status = cso->scsi_status;
1964387d8239SMatt Jacob 					if (resid < 0) {
1965387d8239SMatt Jacob 						rp->fcp_rsp_resid = -resid;
1966387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1967387d8239SMatt Jacob 					} else if (resid > 0) {
1968387d8239SMatt Jacob 						rp->fcp_rsp_resid = resid;
1969387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1970387d8239SMatt Jacob 					}
1971387d8239SMatt Jacob 					if (sense_length) {
1972387d8239SMatt Jacob 	        				rp->fcp_rsp_snslen = sense_length;
1973387d8239SMatt Jacob 						rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1974387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1975387d8239SMatt Jacob 						memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1976387d8239SMatt Jacob 					} else {
1977387d8239SMatt Jacob 						isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1978387d8239SMatt Jacob 					}
1979387d8239SMatt Jacob 					if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1980387d8239SMatt Jacob 						isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1981387d8239SMatt Jacob 					}
1982387d8239SMatt Jacob 					addr = isp->isp_osinfo.ecmd_dma;
1983387d8239SMatt Jacob 					addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
198494dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1985387d8239SMatt Jacob 					    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1986387d8239SMatt Jacob 					cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1987387d8239SMatt Jacob 					if (isp->isp_osinfo.sixtyfourbit) {
1988387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1989387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1990387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1991387d8239SMatt Jacob 					} else {
1992387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1993387d8239SMatt Jacob 						cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1994387d8239SMatt Jacob 					}
1995387d8239SMatt Jacob 				}
1996387d8239SMatt Jacob 				if (sense_length) {
199794dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
199894dff771SMatt Jacob 					    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
199994dff771SMatt Jacob 					    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
2000387d8239SMatt Jacob 				} else {
200194dff771SMatt Jacob 					isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
200294dff771SMatt Jacob 					    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
2003387d8239SMatt Jacob 				}
2004387d8239SMatt Jacob 				atp->state = ATPD_STATE_LAST_CTIO;
2005387d8239SMatt Jacob 			}
2006387d8239SMatt Jacob 
2007387d8239SMatt Jacob 			if (xfrlen != 0) {
200800a8e174SMatt Jacob 				cto->ct_flags |= CT2_FLAG_MODE0;
200900a8e174SMatt Jacob 				if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
201000a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_IN;
201100a8e174SMatt Jacob 				} else {
201200a8e174SMatt Jacob 					cto->ct_flags |= CT2_DATA_OUT;
2013d81ba9d5SMatt Jacob 				}
201453036e92SMatt Jacob 
201594dff771SMatt Jacob 				cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
2016387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen = xfrlen;
2017387d8239SMatt Jacob 
2018387d8239SMatt Jacob 				if (sendstatus) {
201994dff771SMatt Jacob 					resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
202094dff771SMatt Jacob 					if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
2021387d8239SMatt Jacob 						cto->ct_flags |= CT2_SENDSTATUS;
2022387d8239SMatt Jacob 						atp->state = ATPD_STATE_LAST_CTIO;
202394dff771SMatt Jacob 						if (fctape) {
202494dff771SMatt Jacob 							cto->ct_flags |= CT2_CONFIRM;
202594dff771SMatt Jacob 						}
2026387d8239SMatt Jacob 					} else {
2027387d8239SMatt Jacob 						atp->sendst = 1;	/* send status later */
202894dff771SMatt Jacob 						cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
2029387d8239SMatt Jacob 						atp->state = ATPD_STATE_CTIO;
2030387d8239SMatt Jacob 					}
2031387d8239SMatt Jacob 				} else {
2032387d8239SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
2033387d8239SMatt Jacob 				}
2034387d8239SMatt Jacob 			}
203594dff771SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
203694dff771SMatt Jacob 			    ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
2037d81ba9d5SMatt Jacob 		} else {
20384fd13c1bSMatt Jacob 			ct_entry_t *cto = (ct_entry_t *) local;
203900a8e174SMatt Jacob 
2040d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
2041d81ba9d5SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
204294dff771SMatt Jacob 			cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
204394dff771SMatt Jacob 			ATPD_SET_SEQNO(cto, atp);
204400a8e174SMatt Jacob 			cto->ct_iid = cso->init_id;
2045a1bc34c6SMatt Jacob 			cto->ct_iid |= XS_CHANNEL(ccb) << 7;
2046d81ba9d5SMatt Jacob 			cto->ct_tgt = ccb->ccb_h.target_id;
2047d81ba9d5SMatt Jacob 			cto->ct_lun = ccb->ccb_h.target_lun;
2048387d8239SMatt Jacob 			cto->ct_fwhandle = cso->tag_id;
2049387d8239SMatt Jacob 			if (atp->rxid) {
2050387d8239SMatt Jacob 				cto->ct_tag_val = atp->rxid;
2051f48ce188SMatt Jacob 				cto->ct_flags |= CT_TQAE;
2052f48ce188SMatt Jacob 			}
2053f48ce188SMatt Jacob 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2054f48ce188SMatt Jacob 				cto->ct_flags |= CT_NODISC;
2055f48ce188SMatt Jacob 			}
2056f48ce188SMatt Jacob 			if (cso->dxfer_len == 0) {
2057d81ba9d5SMatt Jacob 				cto->ct_flags |= CT_NO_DATA;
205800a8e174SMatt Jacob 			} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
205900a8e174SMatt Jacob 				cto->ct_flags |= CT_DATA_IN;
206000a8e174SMatt Jacob 			} else {
206100a8e174SMatt Jacob 				cto->ct_flags |= CT_DATA_OUT;
2062d81ba9d5SMatt Jacob 			}
2063f48ce188SMatt Jacob 			if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
206453036e92SMatt Jacob 				cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
206500a8e174SMatt Jacob 				cto->ct_scsi_status = cso->scsi_status;
206694dff771SMatt Jacob 				cto->ct_resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit - xfrlen;
206794dff771SMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d scsi status %x resid %d tag_id %x", __func__,
206894dff771SMatt Jacob 				    cto->ct_fwhandle, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), cso->scsi_status, cso->resid, cso->tag_id);
206992a1e549SMatt Jacob 			}
207064edff94SMatt Jacob 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
2071a1bc34c6SMatt Jacob 			cto->ct_timeout = 10;
2072d81ba9d5SMatt Jacob 		}
2073d81ba9d5SMatt Jacob 
207494dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
207594dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
207694dff771SMatt Jacob 			TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
207794dff771SMatt Jacob 			break;
207894dff771SMatt Jacob 		}
2079c8b8a2c4SMatt Jacob 		if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
2080387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
208194dff771SMatt Jacob 			TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
208294dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
208394dff771SMatt Jacob 			break;
2084d81ba9d5SMatt Jacob 		}
208594dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
208694dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
208794dff771SMatt Jacob 
2088d81ba9d5SMatt Jacob 
2089d81ba9d5SMatt Jacob 		/*
2090d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
2091d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
2092b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
2093d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
2094d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
2095d81ba9d5SMatt Jacob 		 * format.
2096d81ba9d5SMatt Jacob 		 */
2097d81ba9d5SMatt Jacob 
20982df76c16SMatt Jacob 		if (IS_24XX(isp)) {
20992df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) local;
21002df76c16SMatt Jacob 			cto->ct_syshandle = handle;
21012df76c16SMatt Jacob 		} else if (IS_FC(isp)) {
210210365e5aSMatt Jacob 			ct2_entry_t *cto = (ct2_entry_t *) local;
210310365e5aSMatt Jacob 			cto->ct_syshandle = handle;
210410365e5aSMatt Jacob 		} else {
210510365e5aSMatt Jacob 			ct_entry_t *cto = (ct_entry_t *) local;
210610365e5aSMatt Jacob 			cto->ct_syshandle = handle;
210710365e5aSMatt Jacob 		}
2108a1bc34c6SMatt Jacob 
21092df76c16SMatt Jacob 		dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
211094dff771SMatt Jacob 		if (dmaresult != CMD_QUEUED) {
211194dff771SMatt Jacob 			isp_destroy_tgt_handle(isp, handle);
211294dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
211394dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
211494dff771SMatt Jacob 				TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
211594dff771SMatt Jacob 				break;
211694dff771SMatt Jacob 			}
211794dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
211894dff771SMatt Jacob 			xpt_done(ccb);
211994dff771SMatt Jacob 			continue;
212094dff771SMatt Jacob 		}
21212df76c16SMatt Jacob 		isp->isp_nactive++;
212294dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2123387d8239SMatt Jacob 		if (xfrlen) {
2124387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
2125387d8239SMatt Jacob 		} else {
2126387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
2127387d8239SMatt Jacob 		}
2128387d8239SMatt Jacob 		atp->ctcnt++;
212994dff771SMatt Jacob 		atp->seqno++;
21302df76c16SMatt Jacob 	}
21312df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2132d81ba9d5SMatt Jacob }
2133d81ba9d5SMatt Jacob 
2134a1bc34c6SMatt Jacob static void
2135a1bc34c6SMatt Jacob isp_refire_putback_atio(void *arg)
2136f48ce188SMatt Jacob {
21372df76c16SMatt Jacob 	union ccb *ccb = arg;
21382a0db815SJohn Baldwin 
2139b3a9e657SAlexander Motin 	ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
21402df76c16SMatt Jacob 	isp_target_putback_atio(ccb);
2141a1bc34c6SMatt Jacob }
2142a1bc34c6SMatt Jacob 
2143a1bc34c6SMatt Jacob static void
2144387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
2145387d8239SMatt Jacob {
2146387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
2147387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
21482a0db815SJohn Baldwin 
21492a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
2150387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
21512a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
2152387d8239SMatt Jacob 	} else {
2153387d8239SMatt Jacob 		free(tp, M_DEVBUF);
2154387d8239SMatt Jacob 	}
2155387d8239SMatt Jacob }
2156387d8239SMatt Jacob 
2157387d8239SMatt Jacob 
2158387d8239SMatt Jacob static void
2159a1bc34c6SMatt Jacob isp_target_putback_atio(union ccb *ccb)
2160a1bc34c6SMatt Jacob {
21619cd7268eSMatt Jacob 	ispsoftc_t *isp;
2162a1bc34c6SMatt Jacob 	struct ccb_scsiio *cso;
2163a1bc34c6SMatt Jacob 	void *qe;
2164a1bc34c6SMatt Jacob 
2165a1bc34c6SMatt Jacob 	isp = XS_ISP(ccb);
2166f48ce188SMatt Jacob 
21672df76c16SMatt Jacob 	qe = isp_getrqentry(isp);
21682df76c16SMatt Jacob 	if (qe == NULL) {
2169c3167cabSMatt Jacob 		xpt_print(ccb->ccb_h.path,
2170c3167cabSMatt Jacob 		    "%s: Request Queue Overflow\n", __func__);
21712a0db815SJohn Baldwin 		callout_reset(&PISP_PCMD(ccb)->wdog, 10,
21722a0db815SJohn Baldwin 		    isp_refire_putback_atio, ccb);
2173a1bc34c6SMatt Jacob 		return;
2174f48ce188SMatt Jacob 	}
217529f76675SMatt Jacob 	memset(qe, 0, QENTRY_LEN);
2176a1bc34c6SMatt Jacob 	cso = &ccb->csio;
2177f48ce188SMatt Jacob 	if (IS_FC(isp)) {
21784fd13c1bSMatt Jacob 		at2_entry_t local, *at = &local;
21792df76c16SMatt Jacob 		ISP_MEMZERO(at, sizeof (at2_entry_t));
2180f48ce188SMatt Jacob 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
2181f48ce188SMatt Jacob 		at->at_header.rqs_entry_count = 1;
21822df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
2183a1bc34c6SMatt Jacob 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
2184f48ce188SMatt Jacob 		} else {
2185a1bc34c6SMatt Jacob 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
2186f48ce188SMatt Jacob 		}
2187f48ce188SMatt Jacob 		at->at_status = CT_OK;
2188a1bc34c6SMatt Jacob 		at->at_rxid = cso->tag_id;
2189570c7a3fSMatt Jacob 		at->at_iid = cso->ccb_h.target_id;
21904fd13c1bSMatt Jacob 		isp_put_atio2(isp, at, qe);
2191f48ce188SMatt Jacob 	} else {
21924fd13c1bSMatt Jacob 		at_entry_t local, *at = &local;
21932df76c16SMatt Jacob 		ISP_MEMZERO(at, sizeof (at_entry_t));
2194f48ce188SMatt Jacob 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
2195f48ce188SMatt Jacob 		at->at_header.rqs_entry_count = 1;
2196a1bc34c6SMatt Jacob 		at->at_iid = cso->init_id;
2197a1bc34c6SMatt Jacob 		at->at_iid |= XS_CHANNEL(ccb) << 7;
2198a1bc34c6SMatt Jacob 		at->at_tgt = cso->ccb_h.target_id;
2199a1bc34c6SMatt Jacob 		at->at_lun = cso->ccb_h.target_lun;
2200f48ce188SMatt Jacob 		at->at_status = CT_OK;
2201a1bc34c6SMatt Jacob 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
2202a1bc34c6SMatt Jacob 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
22034fd13c1bSMatt Jacob 		isp_put_atio(isp, at, qe);
2204f48ce188SMatt Jacob 	}
22052df76c16SMatt Jacob 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
22062df76c16SMatt Jacob 	ISP_SYNC_REQUEST(isp);
2207a1bc34c6SMatt Jacob 	isp_complete_ctio(ccb);
2208f48ce188SMatt Jacob }
2209f48ce188SMatt Jacob 
2210f48ce188SMatt Jacob static void
2211a1bc34c6SMatt Jacob isp_complete_ctio(union ccb *ccb)
2212f48ce188SMatt Jacob {
2213387d8239SMatt Jacob 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2214a1bc34c6SMatt Jacob 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2215a1bc34c6SMatt Jacob 		xpt_done(ccb);
2216f48ce188SMatt Jacob 	}
2217387d8239SMatt Jacob }
2218f48ce188SMatt Jacob 
2219d81ba9d5SMatt Jacob /*
2220d81ba9d5SMatt Jacob  * Handle ATIO stuff that the generic code can't.
2221d81ba9d5SMatt Jacob  * This means handling CDBs.
2222d81ba9d5SMatt Jacob  */
2223d81ba9d5SMatt Jacob 
22242df76c16SMatt Jacob static void
22259cd7268eSMatt Jacob isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
2226d81ba9d5SMatt Jacob {
2227d81ba9d5SMatt Jacob 	tstate_t *tptr;
22282df76c16SMatt Jacob 	int status, bus;
2229d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
22302df76c16SMatt Jacob 	atio_private_data_t *atp;
2231d81ba9d5SMatt Jacob 
2232d81ba9d5SMatt Jacob 	/*
2233d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
2234d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
2235d81ba9d5SMatt Jacob 	 *
2236b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2237d81ba9d5SMatt Jacob 	 *
2238d81ba9d5SMatt Jacob 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
22395d571944SMatt Jacob 	 * we're still connected on the SCSI bus.
2240d81ba9d5SMatt Jacob 	 */
2241d81ba9d5SMatt Jacob 	status = aep->at_status;
2242d81ba9d5SMatt Jacob 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
2243d81ba9d5SMatt Jacob 		/*
2244d81ba9d5SMatt Jacob 		 * Bus Phase Sequence error. We should have sense data
2245d81ba9d5SMatt Jacob 		 * suggested by the f/w. I'm not sure quite yet what
2246d81ba9d5SMatt Jacob 		 * to do about this for CAM.
2247d81ba9d5SMatt Jacob 		 */
22483c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
2249d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
22502df76c16SMatt Jacob 		return;
2251d81ba9d5SMatt Jacob 	}
2252d81ba9d5SMatt Jacob 	if ((status & ~QLTM_SVALID) != AT_CDB) {
22532df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
2254d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
22552df76c16SMatt Jacob 		return;
2256d81ba9d5SMatt Jacob 	}
2257d81ba9d5SMatt Jacob 
22585d571944SMatt Jacob 	bus = GET_BUS_VAL(aep->at_iid);
2259a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, aep->at_lun);
2260d81ba9d5SMatt Jacob 	if (tptr == NULL) {
2261a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2262746e9c85SMatt Jacob 		if (tptr == NULL) {
2263d81ba9d5SMatt Jacob 			/*
2264d81ba9d5SMatt Jacob 			 * Because we can't autofeed sense data back with
2265d81ba9d5SMatt Jacob 			 * a command for parallel SCSI, we can't give back
2266d81ba9d5SMatt Jacob 			 * a CHECK CONDITION. We'll give back a BUSY status
2267d81ba9d5SMatt Jacob 			 * instead. This works out okay because the only
2268d81ba9d5SMatt Jacob 			 * time we should, in fact, get this, is in the
2269d81ba9d5SMatt Jacob 			 * case that somebody configured us without the
2270d81ba9d5SMatt Jacob 			 * blackhole driver, so they get what they deserve.
2271d81ba9d5SMatt Jacob 			 */
2272d81ba9d5SMatt Jacob 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
22732df76c16SMatt Jacob 			return;
2274d81ba9d5SMatt Jacob 		}
22758c4e89e2SMatt Jacob 	}
2276d81ba9d5SMatt Jacob 
2277523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_handle);
2278d81ba9d5SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
22792df76c16SMatt Jacob 	if (atiop == NULL || atp == NULL) {
2280d81ba9d5SMatt Jacob 		/*
2281d81ba9d5SMatt Jacob 		 * Because we can't autofeed sense data back with
2282d81ba9d5SMatt Jacob 		 * a command for parallel SCSI, we can't give back
2283d81ba9d5SMatt Jacob 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
2284d81ba9d5SMatt Jacob 		 * instead. This works out okay because the only time we
2285d81ba9d5SMatt Jacob 		 * should, in fact, get this, is in the case that we've
2286d81ba9d5SMatt Jacob 		 * run out of ATIOS.
2287d81ba9d5SMatt Jacob 		 */
22882df76c16SMatt Jacob 		xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
22892df76c16SMatt Jacob 		    ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
2290d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
22912df76c16SMatt Jacob 		if (atp) {
22922df76c16SMatt Jacob 			isp_put_atpd(isp, tptr, atp);
2293d81ba9d5SMatt Jacob 		}
22942df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
22952df76c16SMatt Jacob 		return;
22962df76c16SMatt Jacob 	}
2297387d8239SMatt Jacob 	atp->rxid = aep->at_tag_val;
22982df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
2299d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2300746e9c85SMatt Jacob 	tptr->atio_count--;
2301387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2302d81ba9d5SMatt Jacob 	atiop->ccb_h.target_id = aep->at_tgt;
2303d81ba9d5SMatt Jacob 	atiop->ccb_h.target_lun = aep->at_lun;
2304d81ba9d5SMatt Jacob 	if (aep->at_flags & AT_NODISC) {
23055e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_DIS_DISCONNECT;
2306f48ce188SMatt Jacob 	} else {
23075e63cdb4SAlexander Motin 		atiop->ccb_h.flags &= ~CAM_DIS_DISCONNECT;
2308d81ba9d5SMatt Jacob 	}
2309d81ba9d5SMatt Jacob 
2310f48ce188SMatt Jacob 	if (status & QLTM_SVALID) {
2311898899d9SMatt Jacob 		size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
2312f48ce188SMatt Jacob 		atiop->sense_len = amt;
23132df76c16SMatt Jacob 		ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
2314f48ce188SMatt Jacob 	} else {
2315f48ce188SMatt Jacob 		atiop->sense_len = 0;
2316f48ce188SMatt Jacob 	}
2317d81ba9d5SMatt Jacob 
23185d571944SMatt Jacob 	atiop->init_id = GET_IID_VAL(aep->at_iid);
2319d81ba9d5SMatt Jacob 	atiop->cdb_len = aep->at_cdblen;
23202df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2321d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
2322a1bc34c6SMatt Jacob 	/*
2323a1bc34c6SMatt Jacob 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
2324a1bc34c6SMatt Jacob 	 * and the handle (which we have to preserve).
2325a1bc34c6SMatt Jacob 	 */
23262df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
2327a1bc34c6SMatt Jacob 	if (aep->at_flags & AT_TQAE) {
2328a1bc34c6SMatt Jacob 		atiop->tag_action = aep->at_tag_type;
2329d81ba9d5SMatt Jacob 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2330d81ba9d5SMatt Jacob 	}
23312df76c16SMatt Jacob 	atp->orig_datalen = 0;
23322df76c16SMatt Jacob 	atp->bytes_xfered = 0;
23332df76c16SMatt Jacob 	atp->lun = aep->at_lun;
23342df76c16SMatt Jacob 	atp->nphdl = aep->at_iid;
23352df76c16SMatt Jacob 	atp->portid = PORT_NONE;
23362df76c16SMatt Jacob 	atp->oxid = 0;
23372df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
23382df76c16SMatt Jacob 	atp->tattr = aep->at_tag_type;
23392df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
2340387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO[0x%x] CDB=0x%x lun %d", aep->at_tag_val, atp->cdb0, atp->lun);
2341d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
2342d81ba9d5SMatt Jacob }
2343d81ba9d5SMatt Jacob 
23442df76c16SMatt Jacob static void
23459cd7268eSMatt Jacob isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2346d81ba9d5SMatt Jacob {
234792a1e549SMatt Jacob 	lun_id_t lun;
23482df76c16SMatt Jacob 	fcportdb_t *lp;
2349d81ba9d5SMatt Jacob 	tstate_t *tptr;
2350d81ba9d5SMatt Jacob 	struct ccb_accept_tio *atiop;
23512df76c16SMatt Jacob 	uint16_t nphdl;
2352a035b0afSMatt Jacob 	atio_private_data_t *atp;
23532df76c16SMatt Jacob 	inot_private_data_t *ntp;
2354d81ba9d5SMatt Jacob 
2355d81ba9d5SMatt Jacob 	/*
2356d81ba9d5SMatt Jacob 	 * The firmware status (except for the QLTM_SVALID bit)
2357d81ba9d5SMatt Jacob 	 * indicates why this ATIO was sent to us.
2358d81ba9d5SMatt Jacob 	 *
2359b1ce21c6SRebecca Cran 	 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2360d81ba9d5SMatt Jacob 	 */
2361d81ba9d5SMatt Jacob 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
23622df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2363d81ba9d5SMatt Jacob 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
23642df76c16SMatt Jacob 		return;
2365d81ba9d5SMatt Jacob 	}
2366d81ba9d5SMatt Jacob 
23672df76c16SMatt Jacob 	if (ISP_CAP_SCCFW(isp)) {
236892a1e549SMatt Jacob 		lun = aep->at_scclun;
23692ad50ca5SMatt Jacob 	} else {
237092a1e549SMatt Jacob 		lun = aep->at_lun;
23712ad50ca5SMatt Jacob 	}
23722df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
23732df76c16SMatt Jacob 		nphdl = ((at2e_entry_t *)aep)->at_iid;
23742df76c16SMatt Jacob 	} else {
23752df76c16SMatt Jacob 		nphdl = aep->at_iid;
23762df76c16SMatt Jacob 	}
2377a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, 0, lun);
2378d81ba9d5SMatt Jacob 	if (tptr == NULL) {
2379a1bc34c6SMatt Jacob 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2380746e9c85SMatt Jacob 		if (tptr == NULL) {
2381123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
2382387d8239SMatt Jacob 			if (lun == 0) {
2383387d8239SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2384387d8239SMatt Jacob 			} else {
23852df76c16SMatt Jacob 				isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2386387d8239SMatt Jacob 			}
23872df76c16SMatt Jacob 			return;
2388746e9c85SMatt Jacob 		}
2389d81ba9d5SMatt Jacob 	}
2390d81ba9d5SMatt Jacob 
2391d81ba9d5SMatt Jacob 	/*
23922df76c16SMatt Jacob 	 * Start any commands pending resources first.
2393d81ba9d5SMatt Jacob 	 */
23942df76c16SMatt Jacob 	if (tptr->restart_queue) {
23952df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
23962df76c16SMatt Jacob 		tptr->restart_queue = NULL;
23972df76c16SMatt Jacob 		while (restart_queue) {
23982df76c16SMatt Jacob 			ntp = restart_queue;
23992df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
24002df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
24012df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
24022df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
24032df76c16SMatt Jacob 			/*
24042df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
24052df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
24062df76c16SMatt Jacob 			 * it and go to noresrc.
24072df76c16SMatt Jacob 			 */
24082df76c16SMatt Jacob 			if (tptr->restart_queue) {
24092df76c16SMatt Jacob 				ntp = tptr->restart_queue;
24102df76c16SMatt Jacob 				tptr->restart_queue = restart_queue;
24112df76c16SMatt Jacob 				while (restart_queue->rd.nt.nt_hba) {
24122df76c16SMatt Jacob 					restart_queue = restart_queue->rd.nt.nt_hba;
2413d81ba9d5SMatt Jacob 				}
24142df76c16SMatt Jacob 				restart_queue->rd.nt.nt_hba = ntp;
24152df76c16SMatt Jacob 				goto noresrc;
24162df76c16SMatt Jacob 			}
24172df76c16SMatt Jacob 		}
24182df76c16SMatt Jacob 	}
24192df76c16SMatt Jacob 
24202df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
24212df76c16SMatt Jacob 	if (atiop == NULL) {
24222df76c16SMatt Jacob 		goto noresrc;
24232df76c16SMatt Jacob 	}
24242df76c16SMatt Jacob 
2425523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
24262df76c16SMatt Jacob 	if (atp == NULL) {
24272df76c16SMatt Jacob 		goto noresrc;
24282df76c16SMatt Jacob 	}
24292df76c16SMatt Jacob 
2430570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_ATIO;
2431d81ba9d5SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2432570c7a3fSMatt Jacob 	tptr->atio_count--;
2433387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
24342df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
243592a1e549SMatt Jacob 	atiop->ccb_h.target_lun = lun;
24362df76c16SMatt Jacob 
2437b0a3ba7eSMatt Jacob 	/*
2438b0a3ba7eSMatt Jacob 	 * We don't get 'suggested' sense data as we do with SCSI cards.
2439b0a3ba7eSMatt Jacob 	 */
2440f48ce188SMatt Jacob 	atiop->sense_len = 0;
24412df76c16SMatt Jacob 	if (ISP_CAP_2KLOGIN(isp)) {
24422df76c16SMatt Jacob 		/*
24432df76c16SMatt Jacob 		 * NB: We could not possibly have 2K logins if we
24442df76c16SMatt Jacob 		 * NB: also did not have SCC FW.
24452df76c16SMatt Jacob 		 */
24462df76c16SMatt Jacob 		atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
24472df76c16SMatt Jacob 	} else {
2448d81ba9d5SMatt Jacob 		atiop->init_id = aep->at_iid;
24492df76c16SMatt Jacob 	}
24502df76c16SMatt Jacob 
24512df76c16SMatt Jacob 	/*
24522df76c16SMatt Jacob 	 * If we're not in the port database, add ourselves.
24532df76c16SMatt Jacob 	 */
24542df76c16SMatt Jacob 	if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
24552df76c16SMatt Jacob     		uint64_t iid =
24562df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[0]) << 48) |
24572df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[1]) << 32) |
24582df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[2]) << 16) |
24592df76c16SMatt Jacob 			(((uint64_t) aep->at_wwpn[3]) <<  0);
24602df76c16SMatt Jacob 		/*
24612df76c16SMatt Jacob 		 * However, make sure we delete ourselves if otherwise
24622df76c16SMatt Jacob 		 * we were there but at a different loop id.
24632df76c16SMatt Jacob 		 */
24642df76c16SMatt Jacob 		if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
24652df76c16SMatt Jacob 			isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
24662df76c16SMatt Jacob 		}
2467387d8239SMatt Jacob 		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY, 0);
24682df76c16SMatt Jacob 	}
2469d81ba9d5SMatt Jacob 	atiop->cdb_len = ATIO2_CDBLEN;
24702df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2471d81ba9d5SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
24722df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
2473d81ba9d5SMatt Jacob 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2474d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_SIMPLEQ:
24755e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2476d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2477d81ba9d5SMatt Jacob 		break;
2478d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_HEADOFQ:
24795e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2480d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2481d81ba9d5SMatt Jacob 		break;
2482d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ORDERED:
24835e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2484d81ba9d5SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2485d81ba9d5SMatt Jacob 		break;
2486d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
2487d81ba9d5SMatt Jacob 	case ATIO2_TC_ATTR_UNTAGGED:
2488d81ba9d5SMatt Jacob 	default:
2489d81ba9d5SMatt Jacob 		atiop->tag_action = 0;
2490d81ba9d5SMatt Jacob 		break;
2491d81ba9d5SMatt Jacob 	}
2492f48ce188SMatt Jacob 
249353036e92SMatt Jacob 	atp->orig_datalen = aep->at_datalen;
249453036e92SMatt Jacob 	atp->bytes_xfered = 0;
24952df76c16SMatt Jacob 	atp->lun = lun;
24962df76c16SMatt Jacob 	atp->nphdl = atiop->init_id;
24972df76c16SMatt Jacob 	atp->sid = PORT_ANY;
24982df76c16SMatt Jacob 	atp->oxid = aep->at_oxid;
24992df76c16SMatt Jacob 	atp->cdb0 = aep->at_cdb[0];
25002df76c16SMatt Jacob 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2501570c7a3fSMatt Jacob 	atp->state = ATPD_STATE_CAM;
2502d81ba9d5SMatt Jacob 	xpt_done((union ccb *)atiop);
2503123055f0SNathan Whitehorn 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %jx datalen %u", aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
2504d81ba9d5SMatt Jacob 	rls_lun_statep(isp, tptr);
25052df76c16SMatt Jacob 	return;
25062df76c16SMatt Jacob noresrc:
25072df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
25082df76c16SMatt Jacob 	if (ntp == NULL) {
25092df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
25102df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
25112df76c16SMatt Jacob 		return;
25122df76c16SMatt Jacob 	}
25132df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
25142df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
25152df76c16SMatt Jacob 	tptr->restart_queue = ntp;
25162df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
2517d81ba9d5SMatt Jacob }
2518d81ba9d5SMatt Jacob 
25192df76c16SMatt Jacob static void
25202df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
25212df76c16SMatt Jacob {
25222df76c16SMatt Jacob 	int cdbxlen;
25232df76c16SMatt Jacob 	uint16_t lun, chan, nphdl = NIL_HANDLE;
25242df76c16SMatt Jacob 	uint32_t did, sid;
25252df76c16SMatt Jacob 	uint64_t wwn = INI_NONE;
25262df76c16SMatt Jacob 	fcportdb_t *lp;
25272df76c16SMatt Jacob 	tstate_t *tptr;
25282df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
25292df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
25309e7d423dSMatt Jacob 	atio_private_data_t *oatp;
25312df76c16SMatt Jacob 	inot_private_data_t *ntp;
25322df76c16SMatt Jacob 
25332df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
25342df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
25352df76c16SMatt Jacob 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
25362df76c16SMatt Jacob 
25372df76c16SMatt Jacob 	/*
25382df76c16SMatt Jacob 	 * Find the N-port handle, and Virtual Port Index for this command.
25392df76c16SMatt Jacob 	 *
25402df76c16SMatt Jacob 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
25412df76c16SMatt Jacob 	 * We also, as a matter of course, need to know the WWN of the initiator too.
25422df76c16SMatt Jacob 	 */
25432df76c16SMatt Jacob 	if (ISP_CAP_MULTI_ID(isp)) {
25442df76c16SMatt Jacob 		/*
25452df76c16SMatt Jacob 		 * Find the right channel based upon D_ID
25462df76c16SMatt Jacob 		 */
25472df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
25482df76c16SMatt Jacob 
25492df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
25502df76c16SMatt Jacob 			NANOTIME_T now;
25512df76c16SMatt Jacob 
25522df76c16SMatt Jacob 			/*
25532df76c16SMatt Jacob 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
25542df76c16SMatt Jacob 			 * It's a bit tricky here as we need to stash this command *somewhere*.
25552df76c16SMatt Jacob 			 */
25562df76c16SMatt Jacob 			GET_NANOTIME(&now);
25572df76c16SMatt Jacob 			if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
25582df76c16SMatt 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);
25592df76c16SMatt Jacob 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
25602df76c16SMatt Jacob 				return;
25612df76c16SMatt Jacob 			}
25622df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, 0);
25632df76c16SMatt Jacob 			if (tptr == NULL) {
25642df76c16SMatt Jacob 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
25652df76c16SMatt Jacob 				if (tptr == NULL) {
25662df76c16SMatt 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);
25672df76c16SMatt Jacob 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
25682df76c16SMatt Jacob 					return;
25692df76c16SMatt Jacob 				}
25702df76c16SMatt Jacob 			}
25712df76c16SMatt 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);
25722df76c16SMatt Jacob 			goto noresrc;
25732df76c16SMatt Jacob 		}
25742df76c16SMatt 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);
25752df76c16SMatt Jacob 	} else {
25762df76c16SMatt Jacob 		chan = 0;
25772df76c16SMatt Jacob 	}
25782df76c16SMatt Jacob 
25792df76c16SMatt Jacob 	/*
25802df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
25812df76c16SMatt Jacob 	 */
25822df76c16SMatt Jacob 	if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
25832df76c16SMatt Jacob 		/*
25842df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
25852df76c16SMatt Jacob 		 */
25862df76c16SMatt 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",
25872df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
25882df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
25892df76c16SMatt Jacob 		return;
25902df76c16SMatt Jacob 	}
25912df76c16SMatt Jacob 	nphdl = lp->handle;
25922df76c16SMatt Jacob 	wwn = lp->port_wwn;
25932df76c16SMatt Jacob 
25942df76c16SMatt Jacob 	/*
25952df76c16SMatt Jacob 	 * Get the tstate pointer
25962df76c16SMatt Jacob 	 */
25972df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
25982df76c16SMatt Jacob 	if (tptr == NULL) {
25992df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
26002df76c16SMatt Jacob 		if (tptr == NULL) {
2601387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2602387d8239SMatt Jacob 			if (lun == 0) {
2603387d8239SMatt Jacob 				isp_endcmd(isp, aep, nphdl, SCSI_STATUS_BUSY, 0);
2604387d8239SMatt Jacob 			} else {
26052df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2606387d8239SMatt Jacob 			}
26072df76c16SMatt Jacob 			return;
26082df76c16SMatt Jacob 		}
26092df76c16SMatt Jacob 	}
26102df76c16SMatt Jacob 
26112df76c16SMatt Jacob 	/*
26122df76c16SMatt Jacob 	 * Start any commands pending resources first.
26132df76c16SMatt Jacob 	 */
26142df76c16SMatt Jacob 	if (tptr->restart_queue) {
26152df76c16SMatt Jacob 		inot_private_data_t *restart_queue = tptr->restart_queue;
26162df76c16SMatt Jacob 		tptr->restart_queue = NULL;
26172df76c16SMatt Jacob 		while (restart_queue) {
26182df76c16SMatt Jacob 			ntp = restart_queue;
26192df76c16SMatt Jacob 			restart_queue = ntp->rd.nt.nt_hba;
26202df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
26212df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
26222df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
26232df76c16SMatt Jacob 			/*
26242df76c16SMatt Jacob 			 * If a recursion caused the restart queue to start to fill again,
26252df76c16SMatt Jacob 			 * stop and splice the new list on top of the old list and restore
26262df76c16SMatt Jacob 			 * it and go to noresrc.
26272df76c16SMatt Jacob 			 */
26282df76c16SMatt Jacob 			if (tptr->restart_queue) {
26299e7d423dSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
26302df76c16SMatt Jacob 				if (restart_queue) {
26312df76c16SMatt Jacob 					ntp = tptr->restart_queue;
26322df76c16SMatt Jacob 					tptr->restart_queue = restart_queue;
26332df76c16SMatt Jacob 					while (restart_queue->rd.nt.nt_hba) {
26342df76c16SMatt Jacob 						restart_queue = restart_queue->rd.nt.nt_hba;
26352df76c16SMatt Jacob 					}
26362df76c16SMatt Jacob 					restart_queue->rd.nt.nt_hba = ntp;
26372df76c16SMatt Jacob 				}
26382df76c16SMatt Jacob 				goto noresrc;
26392df76c16SMatt Jacob 			}
26402df76c16SMatt Jacob 		}
26412df76c16SMatt Jacob 	}
26422df76c16SMatt Jacob 
26432df76c16SMatt Jacob 	/*
26442df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
26452df76c16SMatt Jacob 	 */
26462df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
26472df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
26482df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
26492df76c16SMatt Jacob 		return;
26502df76c16SMatt Jacob 	}
26512df76c16SMatt Jacob 
26522df76c16SMatt Jacob 	/*
26532df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
26542df76c16SMatt Jacob 	 */
26552df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
26562df76c16SMatt Jacob 	if (atiop == NULL) {
26572df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
26582df76c16SMatt Jacob 		goto noresrc;
26592df76c16SMatt Jacob 	}
26602df76c16SMatt Jacob 
2661523ea374SAlexander Motin 	oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
26629e7d423dSMatt Jacob 	if (oatp) {
2663387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
26649e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
26652df76c16SMatt Jacob 		/*
26662df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
26672df76c16SMatt Jacob 		 */
26682df76c16SMatt Jacob 		goto noresrc;
26692df76c16SMatt Jacob 	}
2670523ea374SAlexander Motin 	atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2671523ea374SAlexander Motin 	if (atp == NULL) {
2672523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2673523ea374SAlexander Motin 		goto noresrc;
2674523ea374SAlexander Motin 	}
2675387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
26762df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
26772df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
26782df76c16SMatt Jacob 	tptr->atio_count--;
2679387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
26802df76c16SMatt Jacob 	atiop->init_id = nphdl;
26812df76c16SMatt Jacob 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
26822df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
26832df76c16SMatt Jacob 	atiop->sense_len = 0;
26842df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
26852df76c16SMatt Jacob 	if (cdbxlen) {
26862df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
26872df76c16SMatt Jacob 	}
26882df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
26892df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
26902df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
26912df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
26922df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
26932df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
26942df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
26955e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
26962df76c16SMatt Jacob 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
26972df76c16SMatt Jacob 		break;
26982df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
26995e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
27002df76c16SMatt Jacob 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
27012df76c16SMatt Jacob 		break;
27022df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
27035e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
27042df76c16SMatt Jacob 		atiop->tag_action = MSG_ORDERED_Q_TAG;
27052df76c16SMatt Jacob 		break;
27062df76c16SMatt Jacob 	default:
27072df76c16SMatt Jacob 		/* FALLTHROUGH */
27082df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
27092df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
27102df76c16SMatt Jacob 		atiop->tag_action = 0;
27112df76c16SMatt Jacob 		break;
27122df76c16SMatt Jacob 	}
27132df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
27142df76c16SMatt Jacob 	atp->bytes_xfered = 0;
27152df76c16SMatt Jacob 	atp->lun = lun;
27162df76c16SMatt Jacob 	atp->nphdl = nphdl;
27172df76c16SMatt Jacob 	atp->portid = sid;
27182df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
27199e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
27202df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
27212df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
27222df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
2723387d8239SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
27242df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
27252df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
27262df76c16SMatt Jacob 	return;
27272df76c16SMatt Jacob noresrc:
27282df76c16SMatt Jacob 	if (atp) {
27292df76c16SMatt Jacob 		isp_put_atpd(isp, tptr, atp);
27302df76c16SMatt Jacob 	}
27312df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
27322df76c16SMatt Jacob 	if (ntp == NULL) {
27332df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
27342df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
27352df76c16SMatt Jacob 		return;
27362df76c16SMatt Jacob 	}
27372df76c16SMatt Jacob 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
27382df76c16SMatt Jacob 	ntp->rd.nt.nt_hba = tptr->restart_queue;
27392df76c16SMatt Jacob 	tptr->restart_queue = ntp;
27402df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
27412df76c16SMatt Jacob }
27422df76c16SMatt Jacob 
2743387d8239SMatt Jacob 
2744387d8239SMatt Jacob /*
2745387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
2746387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
2747387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
2748387d8239SMatt Jacob  * transaction.
2749387d8239SMatt Jacob  */
2750387d8239SMatt Jacob static void
2751387d8239SMatt Jacob isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2752387d8239SMatt Jacob {
2753387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
2754387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2755387d8239SMatt Jacob 	union ccb *ccb;
2756387d8239SMatt Jacob 
2757387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
2758387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2759387d8239SMatt Jacob 	ccb = atp->srr_ccb;
2760387d8239SMatt Jacob 	atp->srr_ccb = NULL;
2761387d8239SMatt Jacob 	atp->nsrr++;
2762387d8239SMatt Jacob 	if (ccb == NULL) {
276394dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2764387d8239SMatt Jacob 		goto fail;
2765387d8239SMatt Jacob 	}
2766387d8239SMatt Jacob 
2767387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
2768387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
2769387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2770387d8239SMatt Jacob 
2771387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
2772387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
2773387d8239SMatt Jacob 		/*
2774387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
2775387d8239SMatt Jacob 		 */
2776387d8239SMatt Jacob 		atp->sendst = 0;
2777387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
2778387d8239SMatt Jacob 		if (ccb_len == 0) {
277994dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2780387d8239SMatt Jacob 			goto mdp;
2781387d8239SMatt Jacob 		}
2782387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
278394dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2784387d8239SMatt Jacob 			goto mdp;
2785387d8239SMatt Jacob 		}
278694dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2787387d8239SMatt Jacob 		break;
2788387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
278994dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2790387d8239SMatt Jacob 		atp->sendst = 1;
2791387d8239SMatt Jacob 		/*
2792387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
2793387d8239SMatt Jacob 		 */
2794387d8239SMatt Jacob 		break;
2795387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
2796387d8239SMatt Jacob 		/*
2797387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
2798387d8239SMatt Jacob 		 */
2799387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2800387d8239SMatt Jacob 		goto fail;
2801387d8239SMatt Jacob 
2802387d8239SMatt Jacob 	default:
2803387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2804387d8239SMatt Jacob 		goto fail;
2805387d8239SMatt Jacob 	}
2806387d8239SMatt Jacob 
2807387d8239SMatt Jacob 	/*
2808387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
2809387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
2810387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
2811387d8239SMatt Jacob 	 */
2812387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2813387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2814387d8239SMatt Jacob 		goto fail;
2815387d8239SMatt Jacob 	}
2816387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
2817387d8239SMatt Jacob 	return;
2818387d8239SMatt Jacob fail:
2819387d8239SMatt Jacob 	inot->in_reserved = 1;
2820387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2821387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2822387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2823387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2824387d8239SMatt Jacob 	return;
2825387d8239SMatt Jacob mdp:
2826387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
2827387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2828387d8239SMatt Jacob 		goto fail;
2829387d8239SMatt Jacob 	}
2830387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2831387d8239SMatt Jacob 	ccb->ccb_h.status = CAM_MESSAGE_RECV;
2832387d8239SMatt Jacob 	/*
2833387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
2834387d8239SMatt Jacob 	 */
2835387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2836387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
2837387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2838387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
2839387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
2840387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
2841387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
2842387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
2843387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
2844387d8239SMatt Jacob 	isp_complete_ctio(ccb);
2845387d8239SMatt Jacob }
2846387d8239SMatt Jacob 
2847387d8239SMatt Jacob 
2848387d8239SMatt Jacob static void
2849387d8239SMatt Jacob isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2850387d8239SMatt Jacob {
2851387d8239SMatt Jacob 	tstate_t *tptr;
2852387d8239SMatt Jacob 	in_fcentry_24xx_t *inot = inot_raw;
2853387d8239SMatt Jacob 	atio_private_data_t *atp;
2854387d8239SMatt Jacob 	uint32_t tag = inot->in_rxid;
2855387d8239SMatt Jacob 	uint32_t bus = inot->in_vpidx;
2856387d8239SMatt Jacob 
2857387d8239SMatt Jacob 	if (!IS_24XX(isp)) {
2858387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2859387d8239SMatt Jacob 		return;
2860387d8239SMatt Jacob 	}
2861387d8239SMatt Jacob 
2862387d8239SMatt Jacob 	tptr = get_lun_statep_from_tag(isp, bus, tag);
2863387d8239SMatt Jacob 	if (tptr == NULL) {
2864387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2865387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2866387d8239SMatt Jacob 		return;
2867387d8239SMatt Jacob 	}
2868523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, tag);
2869387d8239SMatt Jacob 	if (atp == NULL) {
2870387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2871387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2872387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2873387d8239SMatt Jacob 		return;
2874387d8239SMatt Jacob 	}
2875387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
2876387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
287794dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2878387d8239SMatt Jacob 	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2879387d8239SMatt Jacob 	if (atp->srr_ccb)
2880387d8239SMatt Jacob 		isp_handle_srr_start(isp, tptr, atp);
2881387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
2882387d8239SMatt Jacob }
2883387d8239SMatt Jacob 
28842df76c16SMatt Jacob static void
28859cd7268eSMatt Jacob isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2886d81ba9d5SMatt Jacob {
2887d81ba9d5SMatt Jacob 	union ccb *ccb;
288894dff771SMatt Jacob 	int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
28892df76c16SMatt Jacob 	tstate_t *tptr = NULL;
28902df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
28912df76c16SMatt Jacob 	int bus;
289294dff771SMatt Jacob 	uint32_t handle, moved_data = 0, data_requested;
2893d81ba9d5SMatt Jacob 
2894d81ba9d5SMatt Jacob 	/*
2895443e752dSMatt Jacob 	 * CTIO handles are 16 bits.
2896443e752dSMatt Jacob 	 * CTIO2 and CTIO7 are 32 bits.
2897d81ba9d5SMatt Jacob 	 */
2898d81ba9d5SMatt Jacob 
28992df76c16SMatt Jacob 	if (IS_SCSI(isp)) {
29002df76c16SMatt Jacob 		handle = ((ct_entry_t *)arg)->ct_syshandle;
29012df76c16SMatt Jacob 	} else {
29022df76c16SMatt Jacob 		handle = ((ct2_entry_t *)arg)->ct_syshandle;
29032df76c16SMatt Jacob 	}
29042df76c16SMatt Jacob 	ccb = isp_find_xs_tgt(isp, handle);
29052df76c16SMatt Jacob 	if (ccb == NULL) {
29062df76c16SMatt Jacob 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
29072df76c16SMatt Jacob 		return;
29082df76c16SMatt Jacob 	}
29092df76c16SMatt Jacob 	isp_destroy_tgt_handle(isp, handle);
291094dff771SMatt Jacob 	data_requested = PISP_PCMD(ccb)->datalen;
2911387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
2912387d8239SMatt Jacob 	if (isp->isp_nactive) {
2913387d8239SMatt Jacob 		isp->isp_nactive--;
2914387d8239SMatt Jacob 	}
2915387d8239SMatt Jacob 
29162df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
29172df76c16SMatt Jacob 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
29182df76c16SMatt Jacob 	if (tptr == NULL) {
29192df76c16SMatt Jacob 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
29202df76c16SMatt Jacob 	}
2921387d8239SMatt Jacob 	if (tptr == NULL) {
2922387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2923387d8239SMatt Jacob 		return;
29242df76c16SMatt Jacob 	}
2925387d8239SMatt Jacob 
2926387d8239SMatt Jacob 	if (IS_24XX(isp)) {
2927523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2928387d8239SMatt Jacob 	} else if (IS_FC(isp)) {
2929523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2930387d8239SMatt Jacob 	} else {
2931523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, ((ct_entry_t *)arg)->ct_fwhandle);
2932387d8239SMatt Jacob 	}
2933387d8239SMatt Jacob 	if (atp == NULL) {
2934387d8239SMatt Jacob 		rls_lun_statep(isp, tptr);
2935387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2936387d8239SMatt Jacob 		return;
2937387d8239SMatt Jacob 	}
2938387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
293994dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
2940387d8239SMatt Jacob 	atp->ctcnt -= 1;
2941387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2942387d8239SMatt Jacob 
29432df76c16SMatt Jacob 	if (IS_24XX(isp)) {
29442df76c16SMatt Jacob 		ct7_entry_t *ct = arg;
2945d81ba9d5SMatt Jacob 
2946387d8239SMatt Jacob 		if (ct->ct_nphdl == CT7_SRR) {
2947387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2948387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2949387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
29502df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
29512df76c16SMatt Jacob 			return;
29522df76c16SMatt Jacob 		}
2953387d8239SMatt Jacob 		if (ct->ct_nphdl == CT_HBA_RESET) {
2954387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2955387d8239SMatt Jacob 		} else {
29562df76c16SMatt Jacob 			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
29572df76c16SMatt Jacob 			ok = (ct->ct_nphdl == CT7_OK);
295894dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
29592df76c16SMatt Jacob 			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
29602df76c16SMatt Jacob 				resid = ct->ct_resid;
296194dff771SMatt Jacob 				moved_data = data_requested - resid;
29622df76c16SMatt Jacob 			}
29632df76c16SMatt Jacob 		}
296494dff771SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
296594dff771SMatt Jacob 		   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
29662df76c16SMatt Jacob 	} else if (IS_FC(isp)) {
29672df76c16SMatt Jacob 		ct2_entry_t *ct = arg;
2968387d8239SMatt Jacob 		if (ct->ct_status == CT_SRR) {
2969387d8239SMatt Jacob 			atp->srr_ccb = ccb;
2970387d8239SMatt Jacob 			if (atp->srr_notify_rcvd)
2971387d8239SMatt Jacob 				isp_handle_srr_start(isp, tptr, atp);
29722df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
2973387d8239SMatt Jacob 			isp_target_putback_atio(ccb);
29742df76c16SMatt Jacob 			return;
2975570c7a3fSMatt Jacob 		}
2976387d8239SMatt Jacob 		if (ct->ct_status == CT_HBA_RESET) {
2977387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2978387d8239SMatt Jacob 		} else {
2979d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2980d81ba9d5SMatt Jacob 			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
298194dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
29825d571944SMatt Jacob 			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2983a1bc34c6SMatt Jacob 				resid = ct->ct_resid;
298494dff771SMatt Jacob 				moved_data = data_requested - resid;
2985570c7a3fSMatt Jacob 			}
298653036e92SMatt Jacob 		}
298794dff771SMatt Jacob 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
298894dff771SMatt Jacob 		    notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2989d81ba9d5SMatt Jacob 	} else {
2990d81ba9d5SMatt Jacob 		ct_entry_t *ct = arg;
2991387d8239SMatt Jacob 
2992387d8239SMatt Jacob 		if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2993387d8239SMatt Jacob 			failure = CAM_UNREC_HBA_ERROR;
2994387d8239SMatt Jacob 		} else {
2995d81ba9d5SMatt Jacob 			sentstatus = ct->ct_flags & CT_SENDSTATUS;
2996d81ba9d5SMatt Jacob 			ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
299794dff771SMatt Jacob 			notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2998a1bc34c6SMatt Jacob 		}
29995d571944SMatt Jacob 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
3000a1bc34c6SMatt Jacob 			resid = ct->ct_resid;
300194dff771SMatt Jacob 			moved_data = data_requested - resid;
3002a1bc34c6SMatt Jacob 		}
300394dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__, ct->ct_fwhandle, ATPD_GET_SEQNO(ct),
300494dff771SMatt Jacob 		    notify_cam, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
30055d571944SMatt Jacob 	}
3006387d8239SMatt Jacob 	if (ok) {
3007387d8239SMatt Jacob 		if (moved_data) {
300894dff771SMatt Jacob 			atp->bytes_xfered += moved_data;
300994dff771SMatt Jacob 			ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
3010387d8239SMatt Jacob 		}
3011387d8239SMatt Jacob 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
3012387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
3013387d8239SMatt Jacob 		}
3014387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
3015387d8239SMatt Jacob 	} else {
3016387d8239SMatt Jacob 		notify_cam = 1;
3017387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
3018387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
3019387d8239SMatt Jacob 		else
3020387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
3021387d8239SMatt Jacob 	}
3022387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
3023387d8239SMatt Jacob 	rls_lun_statep(isp, tptr);
3024a1bc34c6SMatt Jacob 
3025a1bc34c6SMatt Jacob 	/*
3026387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
3027387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
3028d81ba9d5SMatt Jacob 	 */
302994dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
303094dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
3031f48ce188SMatt Jacob 	if (notify_cam == 0) {
3032387d8239SMatt Jacob 		if (atp->sendst) {
3033387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
3034387d8239SMatt Jacob 		}
30352df76c16SMatt Jacob 		return;
3036f48ce188SMatt Jacob 	}
3037d81ba9d5SMatt Jacob 
3038387d8239SMatt Jacob 	/*
3039387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
3040387d8239SMatt Jacob 	 *
3041387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
3042387d8239SMatt Jacob 	 *
3043387d8239SMatt Jacob 	 * Other cards need one put back only on error.
3044387d8239SMatt Jacob 	 * In the latter case, a timeout will re-fire
3045387d8239SMatt Jacob 	 * and try again in case we didn't have
3046387d8239SMatt Jacob 	 * queue resources to do so at first. In any case,
3047387d8239SMatt Jacob 	 * once the putback is done we do the completion
3048387d8239SMatt Jacob 	 * call.
3049387d8239SMatt Jacob 	 */
3050387d8239SMatt Jacob 	if (ok || IS_24XX(isp)) {
3051a1bc34c6SMatt Jacob 		isp_complete_ctio(ccb);
3052387d8239SMatt Jacob 	} else {
3053387d8239SMatt Jacob 		isp_target_putback_atio(ccb);
3054d81ba9d5SMatt Jacob 	}
3055d81ba9d5SMatt Jacob }
3056570c7a3fSMatt Jacob 
30572df76c16SMatt Jacob static void
30582df76c16SMatt Jacob isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
3059570c7a3fSMatt Jacob {
3060387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3061570c7a3fSMatt Jacob }
3062570c7a3fSMatt Jacob 
30632df76c16SMatt Jacob static void
30649cd7268eSMatt Jacob isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
3065570c7a3fSMatt Jacob {
30662df76c16SMatt Jacob 	int needack = 1;
3067570c7a3fSMatt Jacob 	switch (inp->in_status) {
3068570c7a3fSMatt Jacob 	case IN_PORT_LOGOUT:
30692df76c16SMatt Jacob 		/*
30702df76c16SMatt Jacob 		 * XXX: Need to delete this initiator's WWN from the database
30712df76c16SMatt Jacob 		 * XXX: Need to send this LOGOUT upstream
30722df76c16SMatt Jacob 		 */
30732df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
3074570c7a3fSMatt Jacob 		break;
3075570c7a3fSMatt Jacob 	case IN_PORT_CHANGED:
30762df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
3077570c7a3fSMatt Jacob 		break;
3078570c7a3fSMatt Jacob 	case IN_GLOBAL_LOGO:
30792df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, 0);
3080570c7a3fSMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
3081570c7a3fSMatt Jacob 		break;
3082570c7a3fSMatt Jacob 	case IN_ABORT_TASK:
3083570c7a3fSMatt Jacob 	{
30842df76c16SMatt Jacob 		tstate_t *tptr;
30852df76c16SMatt Jacob 		uint16_t lun;
30862df76c16SMatt Jacob 		uint32_t loopid;
30872df76c16SMatt Jacob 		uint64_t wwn;
30882df76c16SMatt Jacob 		atio_private_data_t *atp;
30892df76c16SMatt Jacob 		fcportdb_t *lp;
30902df76c16SMatt Jacob 		struct ccb_immediate_notify *inot = NULL;
30912df76c16SMatt Jacob 
30922df76c16SMatt Jacob 		if (ISP_CAP_SCCFW(isp)) {
30932df76c16SMatt Jacob 			lun = inp->in_scclun;
30942df76c16SMatt Jacob 		} else {
30952df76c16SMatt Jacob 			lun = inp->in_lun;
30962df76c16SMatt Jacob 		}
30972df76c16SMatt Jacob 		if (ISP_CAP_2KLOGIN(isp)) {
3098a035b0afSMatt Jacob 			loopid = ((in_fcentry_e_t *)inp)->in_iid;
30992df76c16SMatt Jacob 		} else {
31002df76c16SMatt Jacob 			loopid = inp->in_iid;
31012df76c16SMatt Jacob 		}
31022df76c16SMatt Jacob 		if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
31032df76c16SMatt Jacob 			wwn = lp->port_wwn;
31042df76c16SMatt Jacob 		} else {
31052df76c16SMatt Jacob 			wwn = INI_ANY;
31062df76c16SMatt Jacob 		}
31072df76c16SMatt Jacob 		tptr = get_lun_statep(isp, 0, lun);
31082df76c16SMatt Jacob 		if (tptr == NULL) {
31092df76c16SMatt Jacob 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
31102df76c16SMatt Jacob 			if (tptr == NULL) {
31112df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
31122df76c16SMatt Jacob 				return;
31132df76c16SMatt Jacob 			}
31142df76c16SMatt Jacob 		}
3115523ea374SAlexander Motin 		atp = isp_find_atpd(isp, tptr, inp->in_seqid);
3116570c7a3fSMatt Jacob 
3117570c7a3fSMatt Jacob 		if (atp) {
31182df76c16SMatt Jacob 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
31192df76c16SMatt 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);
3120570c7a3fSMatt Jacob 			if (inot) {
3121746e9c85SMatt Jacob 				tptr->inot_count--;
31222df76c16SMatt Jacob 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3123387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3124570c7a3fSMatt Jacob 			} else {
3125387d8239SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
31262df76c16SMatt Jacob 			}
31272df76c16SMatt Jacob 		} else {
31282df76c16SMatt 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);
3129570c7a3fSMatt Jacob 		}
3130570c7a3fSMatt Jacob 		if (inot) {
31312df76c16SMatt Jacob 			isp_notify_t tmp, *nt = &tmp;
31322df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
31332df76c16SMatt Jacob     			nt->nt_hba = isp;
31342df76c16SMatt Jacob 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
31352df76c16SMatt Jacob 			nt->nt_wwn = wwn;
31362df76c16SMatt Jacob 			nt->nt_nphdl = loopid;
31372df76c16SMatt Jacob 			nt->nt_sid = PORT_ANY;
31382df76c16SMatt Jacob 			nt->nt_did = PORT_ANY;
31392df76c16SMatt Jacob     			nt->nt_lun = lun;
31402df76c16SMatt Jacob             		nt->nt_need_ack = 1;
31412df76c16SMatt Jacob     			nt->nt_channel = 0;
31422df76c16SMatt Jacob     			nt->nt_ncode = NT_ABORT_TASK;
31432df76c16SMatt Jacob     			nt->nt_lreserved = inot;
31442df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
31452df76c16SMatt Jacob 			needack = 0;
3146570c7a3fSMatt Jacob 		}
31472df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
3148570c7a3fSMatt Jacob 		break;
3149570c7a3fSMatt Jacob 	}
3150570c7a3fSMatt Jacob 	default:
3151570c7a3fSMatt Jacob 		break;
3152570c7a3fSMatt Jacob 	}
31532df76c16SMatt Jacob 	if (needack) {
3154387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
31552df76c16SMatt Jacob 	}
31562df76c16SMatt Jacob }
31572df76c16SMatt Jacob 
31582df76c16SMatt Jacob static void
31592df76c16SMatt Jacob isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
31602df76c16SMatt Jacob {
31612df76c16SMatt Jacob 	uint16_t nphdl;
3162387d8239SMatt Jacob 	uint16_t prli_options = 0;
31632df76c16SMatt Jacob 	uint32_t portid;
31642df76c16SMatt Jacob 	fcportdb_t *lp;
31652df76c16SMatt Jacob 	uint8_t *ptr = NULL;
31662df76c16SMatt Jacob 	uint64_t wwn;
31672df76c16SMatt Jacob 
31682df76c16SMatt Jacob 	nphdl = inot->in_nphdl;
31692df76c16SMatt Jacob 	if (nphdl != NIL_HANDLE) {
31702df76c16SMatt Jacob 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
31712df76c16SMatt Jacob 	} else {
31722df76c16SMatt Jacob 		portid = PORT_ANY;
31732df76c16SMatt Jacob 	}
31742df76c16SMatt Jacob 
31752df76c16SMatt Jacob 	switch (inot->in_status) {
31762df76c16SMatt Jacob 	case IN24XX_ELS_RCVD:
31772df76c16SMatt Jacob 	{
31782df76c16SMatt Jacob 		char buf[16], *msg;
31792df76c16SMatt Jacob 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
31802df76c16SMatt Jacob 
31812df76c16SMatt Jacob 		/*
31822df76c16SMatt Jacob 		 * Note that we're just getting notification that an ELS was received
3183b1ce21c6SRebecca Cran 		 * (possibly with some associated information sent upstream). This is
31842df76c16SMatt Jacob 		 * *not* the same as being given the ELS frame to accept or reject.
31852df76c16SMatt Jacob 		 */
31862df76c16SMatt Jacob 		switch (inot->in_status_subcode) {
31872df76c16SMatt Jacob 		case LOGO:
31882df76c16SMatt Jacob 			msg = "LOGO";
31892df76c16SMatt Jacob 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
31902df76c16SMatt Jacob 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
31912df76c16SMatt Jacob 				wwn =	(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF])   << 56) |
31922df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
31932df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
31942df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
31952df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
31962df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
31972df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) <<  8) |
31982df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
31992df76c16SMatt Jacob 			} else {
32002df76c16SMatt Jacob 				wwn = INI_ANY;
32012df76c16SMatt Jacob 			}
32022df76c16SMatt Jacob 			isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
32032df76c16SMatt Jacob 			break;
32042df76c16SMatt Jacob 		case PRLO:
32052df76c16SMatt Jacob 			msg = "PRLO";
32062df76c16SMatt Jacob 			break;
32072df76c16SMatt Jacob 		case PLOGI:
3208dad28623SMatt Jacob 		case PRLI:
3209dad28623SMatt Jacob 			/*
3210dad28623SMatt Jacob 			 * Treat PRLI the same as PLOGI and make a database entry for it.
3211dad28623SMatt Jacob 			 */
3212387d8239SMatt Jacob 			if (inot->in_status_subcode == PLOGI) {
32132df76c16SMatt Jacob 				msg = "PLOGI";
3214387d8239SMatt Jacob 			} else {
3215387d8239SMatt Jacob 				prli_options = inot->in_prli_options;
3216dad28623SMatt Jacob 				msg = "PRLI";
3217387d8239SMatt Jacob 			}
32182df76c16SMatt Jacob 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
32192df76c16SMatt Jacob 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
32202df76c16SMatt Jacob 				wwn =	(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
32212df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
32222df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
32232df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
32242df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
32252df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
32262df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) <<  8) |
32272df76c16SMatt Jacob 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
32282df76c16SMatt Jacob 			} else {
32292df76c16SMatt Jacob 				wwn = INI_NONE;
32302df76c16SMatt Jacob 			}
3231387d8239SMatt Jacob 			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid, prli_options);
32322df76c16SMatt Jacob 			break;
32332df76c16SMatt Jacob 		case PDISC:
32342df76c16SMatt Jacob 			msg = "PDISC";
32352df76c16SMatt Jacob 			break;
32362df76c16SMatt Jacob 		case ADISC:
32372df76c16SMatt Jacob 			msg = "ADISC";
32382df76c16SMatt Jacob 			break;
32392df76c16SMatt Jacob 		default:
32402df76c16SMatt Jacob 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
32412df76c16SMatt Jacob 			msg = buf;
32422df76c16SMatt Jacob 			break;
32432df76c16SMatt Jacob 		}
32442df76c16SMatt Jacob 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
32452df76c16SMatt 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);
32462df76c16SMatt Jacob 			break;
32472df76c16SMatt Jacob 		}
32482df76c16SMatt 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,
32492df76c16SMatt Jacob 		    inot->in_rxid, inot->in_oxid);
3250387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
32512df76c16SMatt Jacob 		break;
32522df76c16SMatt Jacob 	}
32532df76c16SMatt Jacob 
32542df76c16SMatt Jacob 	case IN24XX_PORT_LOGOUT:
32552df76c16SMatt Jacob 		ptr = "PORT LOGOUT";
32562df76c16SMatt Jacob 		if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
32572df76c16SMatt Jacob 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
32582df76c16SMatt Jacob 		}
32592df76c16SMatt Jacob 		/* FALLTHROUGH */
32602df76c16SMatt Jacob 	case IN24XX_PORT_CHANGED:
32612df76c16SMatt Jacob 		if (ptr == NULL) {
32622df76c16SMatt Jacob 			ptr = "PORT CHANGED";
32632df76c16SMatt Jacob 		}
32642df76c16SMatt Jacob 		/* FALLTHROUGH */
32652df76c16SMatt Jacob 	case IN24XX_LIP_RESET:
32662df76c16SMatt Jacob 		if (ptr == NULL) {
32672df76c16SMatt Jacob 			ptr = "LIP RESET";
32682df76c16SMatt Jacob 		}
32692df76c16SMatt 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);
32702df76c16SMatt Jacob 
32712df76c16SMatt Jacob 		/*
32722df76c16SMatt Jacob 		 * All subcodes here are irrelevant. What is relevant
32732df76c16SMatt Jacob 		 * is that we need to terminate all active commands from
32742df76c16SMatt Jacob 		 * this initiator (known by N-port handle).
32752df76c16SMatt Jacob 		 */
32762df76c16SMatt Jacob 		/* XXX IMPLEMENT XXX */
3277387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
32782df76c16SMatt Jacob 		break;
32792df76c16SMatt Jacob 
32802df76c16SMatt Jacob 	case IN24XX_SRR_RCVD:
3281387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
3282387d8239SMatt Jacob 		isp_handle_srr_notify(isp, inot);
3283387d8239SMatt Jacob 		break;
3284387d8239SMatt Jacob #else
3285387d8239SMatt Jacob 		if (ptr == NULL) {
3286387d8239SMatt Jacob 			ptr = "SRR RCVD";
3287387d8239SMatt Jacob 		}
3288387d8239SMatt Jacob 		/* FALLTHROUGH */
3289387d8239SMatt Jacob #endif
3290387d8239SMatt Jacob 	case IN24XX_LINK_RESET:
3291387d8239SMatt Jacob 		if (ptr == NULL) {
3292387d8239SMatt Jacob 			ptr = "LINK RESET";
3293387d8239SMatt Jacob 		}
3294387d8239SMatt Jacob 	case IN24XX_LINK_FAILED:
3295387d8239SMatt Jacob 		if (ptr == NULL) {
3296387d8239SMatt Jacob 			ptr = "LINK FAILED";
3297387d8239SMatt Jacob 		}
32982df76c16SMatt Jacob 	default:
3299387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr);
3300387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
33012df76c16SMatt Jacob 		break;
33022df76c16SMatt Jacob 	}
33032df76c16SMatt Jacob }
33042df76c16SMatt Jacob 
33052df76c16SMatt Jacob static int
33062df76c16SMatt Jacob isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
33072df76c16SMatt Jacob {
33082df76c16SMatt Jacob 
33092df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
33102df76c16SMatt 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);
3311570c7a3fSMatt Jacob 		return (0);
3312570c7a3fSMatt Jacob 	}
33132df76c16SMatt Jacob 
33142df76c16SMatt Jacob 	/*
33152df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
33162df76c16SMatt Jacob 	 */
33172df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
33182df76c16SMatt Jacob 		ct7_entry_t local, *cto = &local;
33192df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
33202df76c16SMatt Jacob 		fcportdb_t *lp;
33212df76c16SMatt Jacob 		uint32_t sid;
33222df76c16SMatt Jacob 		uint16_t nphdl;
33232df76c16SMatt Jacob 
33242df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
33252df76c16SMatt Jacob 		if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
33262df76c16SMatt Jacob 			nphdl = lp->handle;
33272df76c16SMatt Jacob 		} else {
33282df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
33292df76c16SMatt Jacob 		}
33302df76c16SMatt Jacob 		ISP_MEMZERO(&local, sizeof (local));
33312df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
33322df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
33332df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
33342df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
33352df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
33362df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
33372df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
33382df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
33392df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
33402df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
33412df76c16SMatt Jacob 		return (isp_target_put_entry(isp, &local));
33422df76c16SMatt Jacob 	}
33432df76c16SMatt Jacob 
33442df76c16SMatt Jacob 	/*
33452df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
33462df76c16SMatt Jacob 	 */
33472df76c16SMatt Jacob 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
33482df76c16SMatt Jacob 
33492df76c16SMatt Jacob 		/*
33502df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
33512df76c16SMatt Jacob 		 */
33522df76c16SMatt Jacob 		if (mp->nt_need_ack) {
33532df76c16SMatt Jacob 			uint8_t storage[QENTRY_LEN];
33542df76c16SMatt Jacob 			ct7_entry_t *cto = (ct7_entry_t *) storage;
33552df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
33562df76c16SMatt Jacob 
33572df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
33582df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
33592df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
33602df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
33612df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
33622df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
33632df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
33642df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
33652df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
33662df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
33672df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
33682df76c16SMatt Jacob 			if (isp_target_put_entry(isp, cto)) {
33692df76c16SMatt Jacob 				return (ENOMEM);
33702df76c16SMatt Jacob 			}
33712df76c16SMatt Jacob 			mp->nt_need_ack = 0;
33722df76c16SMatt Jacob 		}
33732df76c16SMatt Jacob 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
33742df76c16SMatt Jacob 			return (ENOMEM);
33752df76c16SMatt Jacob 		} else {
33762df76c16SMatt Jacob 			return (0);
33772df76c16SMatt Jacob 		}
33782df76c16SMatt Jacob 	}
33792df76c16SMatt Jacob 
33802df76c16SMatt Jacob 	/*
33812df76c16SMatt Jacob 	 * Handle logout cases here
33822df76c16SMatt Jacob 	 */
33832df76c16SMatt Jacob 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
33842df76c16SMatt Jacob 		isp_del_all_wwn_entries(isp, mp->nt_channel);
33852df76c16SMatt Jacob 	}
33862df76c16SMatt Jacob 
33872df76c16SMatt Jacob 	if (mp->nt_ncode == NT_LOGOUT) {
33882df76c16SMatt Jacob 		if (!IS_2100(isp) && IS_FC(isp)) {
33892df76c16SMatt Jacob 			isp_del_wwn_entries(isp, mp);
33902df76c16SMatt Jacob 		}
33912df76c16SMatt Jacob 	}
33922df76c16SMatt Jacob 
33932df76c16SMatt Jacob 	/*
33942df76c16SMatt Jacob 	 * General purpose acknowledgement
33952df76c16SMatt Jacob 	 */
33962df76c16SMatt Jacob 	if (mp->nt_need_ack) {
33972df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
3398387d8239SMatt Jacob 		/*
3399387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
3400387d8239SMatt Jacob 		 */
34012df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
34022df76c16SMatt Jacob 	}
34032df76c16SMatt Jacob 	return (0);
34042df76c16SMatt Jacob }
34052df76c16SMatt Jacob 
34062df76c16SMatt Jacob /*
3407b1ce21c6SRebecca Cran  * Handle task management functions.
34082df76c16SMatt Jacob  *
34092df76c16SMatt Jacob  * We show up here with a notify structure filled out.
34102df76c16SMatt Jacob  *
34112df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
34122df76c16SMatt Jacob  */
34132df76c16SMatt Jacob static void
34142df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
34152df76c16SMatt Jacob {
34162df76c16SMatt Jacob 	tstate_t *tptr;
34172df76c16SMatt Jacob 	fcportdb_t *lp;
34182df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
34192df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
34202df76c16SMatt Jacob 	lun_id_t lun;
34212df76c16SMatt Jacob 
34222df76c16SMatt 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,
34232df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
34242df76c16SMatt Jacob 	/*
34252df76c16SMatt Jacob 	 * NB: This assignment is necessary because of tricky type conversion.
34262df76c16SMatt Jacob 	 * XXX: This is tricky and I need to check this. If the lun isn't known
34272df76c16SMatt Jacob 	 * XXX: for the task management function, it does not of necessity follow
34282df76c16SMatt Jacob 	 * XXX: that it should go up stream to the wildcard listener.
34292df76c16SMatt Jacob 	 */
34302df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
34312df76c16SMatt Jacob 		lun = CAM_LUN_WILDCARD;
34322df76c16SMatt Jacob 	} else {
34332df76c16SMatt Jacob 		lun = notify->nt_lun;
34342df76c16SMatt Jacob 	}
34352df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
34362df76c16SMatt Jacob 	if (tptr == NULL) {
34372df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
34382df76c16SMatt Jacob 		if (tptr == NULL) {
3439123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
34402df76c16SMatt Jacob 			goto bad;
34412df76c16SMatt Jacob 		}
34422df76c16SMatt Jacob 	}
34432df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
34442df76c16SMatt Jacob 	if (inot == NULL) {
3445123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
34462df76c16SMatt Jacob 		goto bad;
34472df76c16SMatt Jacob 	}
34482df76c16SMatt Jacob 
34492df76c16SMatt Jacob 	if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
34502df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
34512df76c16SMatt Jacob 	} else {
34522df76c16SMatt Jacob 		inot->initiator_id = lp->handle;
34532df76c16SMatt Jacob 	}
34542df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
34552df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
34562df76c16SMatt Jacob 
34572df76c16SMatt Jacob 	switch (notify->nt_ncode) {
34582df76c16SMatt Jacob 	case NT_ABORT_TASK:
34592df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
34602df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
34612df76c16SMatt Jacob 		break;
34622df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
34632df76c16SMatt Jacob 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
34642df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
34652df76c16SMatt Jacob 		break;
34662df76c16SMatt Jacob 	case NT_CLEAR_ACA:
34672df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
34682df76c16SMatt Jacob 		break;
34692df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
34702df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
34712df76c16SMatt Jacob 		break;
34722df76c16SMatt Jacob 	case NT_LUN_RESET:
34732df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
34742df76c16SMatt Jacob 		break;
34752df76c16SMatt Jacob 	case NT_TARGET_RESET:
34762df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
34772df76c16SMatt Jacob 		break;
34782df76c16SMatt Jacob 	default:
3479123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun %#jx", __func__, notify->nt_ncode, notify->nt_channel, (uintmax_t)lun);
34802df76c16SMatt Jacob 		goto bad;
34812df76c16SMatt Jacob 	}
34822df76c16SMatt Jacob 
34832df76c16SMatt Jacob 	ntp = isp_get_ntpd(isp, tptr);
34842df76c16SMatt Jacob 	if (ntp == NULL) {
34852df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
34862df76c16SMatt Jacob 		goto bad;
34872df76c16SMatt Jacob 	}
34882df76c16SMatt Jacob 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
34892df76c16SMatt Jacob 	if (notify->nt_lreserved) {
34902df76c16SMatt Jacob 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
34912df76c16SMatt Jacob 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
34922df76c16SMatt Jacob 	}
34932df76c16SMatt Jacob 	ntp->rd.seq_id = notify->nt_tagval;
34942df76c16SMatt Jacob 	ntp->rd.tag_id = notify->nt_tagval >> 32;
34952df76c16SMatt Jacob 
34962df76c16SMatt Jacob 	tptr->inot_count--;
34972df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
34982df76c16SMatt Jacob 	rls_lun_statep(isp, tptr);
3499387d8239SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
35002df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
35012df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
35022df76c16SMatt Jacob 	return;
35032df76c16SMatt Jacob bad:
35042df76c16SMatt Jacob 	if (tptr) {
35052df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
35062df76c16SMatt Jacob 	}
35072df76c16SMatt Jacob 	if (notify->nt_need_ack && notify->nt_lreserved) {
35082df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3509387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
3510387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
3511387d8239SMatt Jacob 			}
35122df76c16SMatt Jacob 		} else {
3513387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
35142df76c16SMatt Jacob 		}
35152df76c16SMatt Jacob 	}
35162df76c16SMatt Jacob }
35172df76c16SMatt Jacob 
35182df76c16SMatt Jacob /*
3519b1ce21c6SRebecca Cran  * Find the associated private data and mark it as dead so
35202df76c16SMatt Jacob  * we don't try to work on it any further.
35212df76c16SMatt Jacob  */
35222df76c16SMatt Jacob static void
35232df76c16SMatt Jacob isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
35242df76c16SMatt Jacob {
35252df76c16SMatt Jacob 	tstate_t *tptr;
35262df76c16SMatt Jacob 	atio_private_data_t *atp;
352745210a25SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
35282df76c16SMatt Jacob 
352945210a25SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
35302df76c16SMatt Jacob 	if (tptr == NULL) {
353145210a25SAlexander Motin 		tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
35322df76c16SMatt Jacob 		if (tptr == NULL) {
35332df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
35342df76c16SMatt Jacob 			return;
35352df76c16SMatt Jacob 		}
35362df76c16SMatt Jacob 	}
35372df76c16SMatt Jacob 
3538523ea374SAlexander Motin 	atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
35392df76c16SMatt Jacob 	if (atp == NULL) {
35402df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INVALID;
354145210a25SAlexander Motin 	} else {
35422df76c16SMatt Jacob 		atp->dead = 1;
35432df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
35442df76c16SMatt Jacob 	}
354545210a25SAlexander Motin 	rls_lun_statep(isp, tptr);
354645210a25SAlexander Motin }
35472df76c16SMatt Jacob 
35482df76c16SMatt Jacob static void
35492df76c16SMatt Jacob isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
35502df76c16SMatt Jacob {
35512df76c16SMatt Jacob 	atio_private_data_t *atp;
35522df76c16SMatt Jacob 	inot_private_data_t *restart_queue = tptr->restart_queue;
35532df76c16SMatt Jacob 
35542df76c16SMatt Jacob 	/*
35552df76c16SMatt Jacob 	 * First, clean any commands pending restart
35562df76c16SMatt Jacob 	 */
35572df76c16SMatt Jacob 	tptr->restart_queue = NULL;
35582df76c16SMatt Jacob 	while (restart_queue) {
35592df76c16SMatt Jacob 		uint32_t this_tag_id;
35602df76c16SMatt Jacob 		inot_private_data_t *ntp = restart_queue;
35612df76c16SMatt Jacob 
35622df76c16SMatt Jacob 		restart_queue = ntp->rd.nt.nt_hba;
35632df76c16SMatt Jacob 
35642df76c16SMatt Jacob 		if (IS_24XX(isp)) {
35652df76c16SMatt Jacob 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
35662df76c16SMatt Jacob 		} else {
35672df76c16SMatt Jacob 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
35682df76c16SMatt Jacob 		}
35692df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
35702df76c16SMatt Jacob 			isp_put_ntpd(isp, tptr, ntp);
35712df76c16SMatt Jacob 		} else {
35722df76c16SMatt Jacob 			ntp->rd.nt.nt_hba = tptr->restart_queue;
35732df76c16SMatt Jacob 			tptr->restart_queue = ntp;
35742df76c16SMatt Jacob 		}
35752df76c16SMatt Jacob 	}
35762df76c16SMatt Jacob 
35772df76c16SMatt Jacob 	/*
35782df76c16SMatt Jacob 	 * Now mark other ones dead as well.
35792df76c16SMatt Jacob 	 */
35802df76c16SMatt Jacob 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
35812df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
35822df76c16SMatt Jacob 			atp->dead = 1;
35832df76c16SMatt Jacob 		}
35842df76c16SMatt Jacob 	}
35852df76c16SMatt Jacob }
35862df76c16SMatt Jacob 
35872df76c16SMatt Jacob 
35882df76c16SMatt Jacob #ifdef	ISP_INTERNAL_TARGET
358994dff771SMatt Jacob //#define	ISP_SEPARATE_STATUS	1
359094dff771SMatt Jacob #define	ISP_MULTI_CCBS		1
359194dff771SMatt Jacob #if defined(ISP_MULTI_CCBS) && !defined(ISP_SEPARATE_STATUS)
359294dff771SMatt Jacob #define	ISP_SEPARATE_STATUS 1
359394dff771SMatt Jacob #endif
35942df76c16SMatt Jacob 
359594dff771SMatt Jacob typedef struct periph_private_data_t {
359694dff771SMatt Jacob 	union ccb *ccb;			/* original ATIO or Immediate Notify */
359794dff771SMatt Jacob 	unsigned long	offset;		/* current offset */
359894dff771SMatt Jacob 	int		sequence;	/* current CTIO sequence */
359994dff771SMatt Jacob 	int		ctio_cnt;	/* current # of ctio's outstanding */
360094dff771SMatt Jacob 	int
360194dff771SMatt Jacob 		status_sent	: 1,
360294dff771SMatt Jacob 		on_queue	: 1;	/* on restart queue */
360394dff771SMatt Jacob } ppd_t;
360494dff771SMatt Jacob /*
360594dff771SMatt Jacob  * Each ATIO we allocate will have periph private data associated with it
360694dff771SMatt Jacob  * that maintains per-command state. This private to each ATIO.
360794dff771SMatt Jacob  */
360894dff771SMatt Jacob #define	ATIO_PPD(ccb)		((ppd_t *)(((struct ccb_hdr *)ccb)->ppriv_ptr0))
360994dff771SMatt Jacob /*
361094dff771SMatt Jacob  * Each CTIO we send downstream will get a pointer to the ATIO itself
361194dff771SMatt Jacob  * so that on completion we can retrieve that pointer.
361294dff771SMatt Jacob  */
36132df76c16SMatt Jacob #define	ccb_atio		ppriv_ptr1
36142df76c16SMatt Jacob #define	ccb_inot		ppriv_ptr1
36152df76c16SMatt Jacob 
361694dff771SMatt Jacob /*
361794dff771SMatt Jacob  * Each CTIO we send downstream will contain a sequence number
361894dff771SMatt Jacob  */
361994dff771SMatt Jacob #define	CTIO_SEQ(ccb)		ccb->ccb_h.ppriv_field0
362094dff771SMatt Jacob 
36212df76c16SMatt Jacob #define	MAX_ISP_TARG_TRANSFER	(2 << 20)
362294dff771SMatt Jacob #define	NISP_TARG_CMDS		64
362394dff771SMatt Jacob #define	NISP_TARG_NOTIFIES	64
36242df76c16SMatt Jacob #define	DISK_SHIFT		9
36252df76c16SMatt Jacob #define	JUNK_SIZE		256
362694dff771SMatt Jacob #define	MULTI_CCB_DATA_LIM	8192
362794dff771SMatt Jacob //#define	MULTI_CCB_DATA_CNT	64
362894dff771SMatt Jacob #define	MULTI_CCB_DATA_CNT	8
36292df76c16SMatt Jacob 
36302df76c16SMatt Jacob extern u_int vm_kmem_size;
36312df76c16SMatt Jacob static int ca;
36322df76c16SMatt Jacob static uint32_t disk_size;
36332df76c16SMatt Jacob static uint8_t *disk_data = NULL;
36342df76c16SMatt Jacob static uint8_t *junk_data;
36352df76c16SMatt Jacob static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
36362df76c16SMatt Jacob struct isptarg_softc {
36372df76c16SMatt Jacob 	/* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
363894dff771SMatt Jacob 	struct isp_ccbq		work_queue;
363994dff771SMatt Jacob 	struct isp_ccbq		rework_queue;
364094dff771SMatt Jacob 	struct isp_ccbq		running_queue;
364194dff771SMatt Jacob 	struct isp_ccbq		inot_queue;
36422df76c16SMatt Jacob 	struct cam_periph       *periph;
36432df76c16SMatt Jacob 	struct cam_path	 	*path;
36442df76c16SMatt Jacob 	ispsoftc_t		*isp;
36452df76c16SMatt Jacob };
36462df76c16SMatt Jacob static periph_ctor_t	isptargctor;
36472df76c16SMatt Jacob static periph_dtor_t	isptargdtor;
36482df76c16SMatt Jacob static periph_start_t	isptargstart;
36492df76c16SMatt Jacob static periph_init_t	isptarginit;
36502df76c16SMatt Jacob static void		isptarg_done(struct cam_periph *, union ccb *);
36512df76c16SMatt Jacob static void		isptargasync(void *, u_int32_t, struct cam_path *, void *);
36522df76c16SMatt Jacob 
36532df76c16SMatt Jacob 
36542df76c16SMatt Jacob static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
36552df76c16SMatt Jacob 
36562df76c16SMatt Jacob static struct periph_driver isptargdriver =
36572df76c16SMatt Jacob {
365894dff771SMatt Jacob 	isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), 0
36592df76c16SMatt Jacob };
36602df76c16SMatt Jacob 
36612df76c16SMatt Jacob static void
36622df76c16SMatt Jacob isptarginit(void)
36632df76c16SMatt Jacob {
36642df76c16SMatt Jacob }
36652df76c16SMatt Jacob 
36662df76c16SMatt Jacob static void
36672df76c16SMatt Jacob isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
36682df76c16SMatt Jacob {
36692df76c16SMatt Jacob 	struct ccb_notify_acknowledge *ack = &iccb->cna2;
36702df76c16SMatt Jacob 
36712df76c16SMatt 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__,
36722df76c16SMatt Jacob 	    inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
36732df76c16SMatt Jacob 	ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
36742df76c16SMatt Jacob 	ack->ccb_h.flags = 0;
36752df76c16SMatt Jacob 	ack->ccb_h.retry_count = 0;
36762df76c16SMatt Jacob 	ack->ccb_h.cbfcnp = isptarg_done;
36772df76c16SMatt Jacob 	ack->ccb_h.timeout = 0;
36782df76c16SMatt Jacob 	ack->ccb_h.ccb_inot = inot;
36792df76c16SMatt Jacob 	ack->tag_id = inot->tag_id;
36802df76c16SMatt Jacob 	ack->seq_id = inot->seq_id;
36812df76c16SMatt Jacob 	ack->initiator_id = inot->initiator_id;
36822df76c16SMatt Jacob 	xpt_action(iccb);
36832df76c16SMatt Jacob }
36842df76c16SMatt Jacob 
36852df76c16SMatt Jacob static void
36862df76c16SMatt Jacob isptargstart(struct cam_periph *periph, union ccb *iccb)
36872df76c16SMatt Jacob {
36880a100e5bSMatt Jacob 	const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
36890a100e5bSMatt Jacob 		0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
36900a100e5bSMatt Jacob 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
36910a100e5bSMatt Jacob 		'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
36920a100e5bSMatt Jacob 		'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
36930a100e5bSMatt Jacob 		'0', '0', '0', '1'
36940a100e5bSMatt Jacob 	};
36952df76c16SMatt Jacob 	const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
36960a100e5bSMatt Jacob 		0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
36972df76c16SMatt Jacob 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
36982df76c16SMatt Jacob 		'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
36992df76c16SMatt Jacob 		'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
37002df76c16SMatt Jacob 		'0', '0', '0', '1'
37012df76c16SMatt Jacob 	};
370294dff771SMatt Jacob 	int r, i, more = 0, last, is_data_cmd = 0, is_write;
370394dff771SMatt Jacob 	char *queue;
37042df76c16SMatt Jacob 	struct isptarg_softc *softc = periph->softc;
37052df76c16SMatt Jacob 	struct ccb_scsiio *csio;
37062df76c16SMatt Jacob 	lun_id_t return_lun;
37072df76c16SMatt Jacob 	struct ccb_accept_tio *atio;
37082df76c16SMatt Jacob 	uint8_t *cdb, *ptr, status;
37092df76c16SMatt Jacob 	uint8_t *data_ptr;
37102df76c16SMatt Jacob 	uint32_t data_len, flags;
37112df76c16SMatt Jacob 	struct ccb_hdr *ccbh;
37122df76c16SMatt Jacob 
37132df76c16SMatt Jacob 	mtx_assert(periph->sim->mtx, MA_OWNED);
3714387d8239SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
37152df76c16SMatt Jacob 	    TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
37162df76c16SMatt Jacob 	/*
37172df76c16SMatt Jacob 	 * Check for immediate notifies first
37182df76c16SMatt Jacob 	 */
37192df76c16SMatt Jacob 	ccbh = TAILQ_FIRST(&softc->inot_queue);
37202df76c16SMatt Jacob 	if (ccbh) {
37212df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
37222df76c16SMatt Jacob 		if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
37232df76c16SMatt Jacob 			xpt_schedule(periph, 1);
37242df76c16SMatt Jacob 		}
37252df76c16SMatt Jacob 		isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
37262df76c16SMatt Jacob 		return;
37272df76c16SMatt Jacob 	}
37282df76c16SMatt Jacob 
37292df76c16SMatt Jacob 	/*
37302df76c16SMatt Jacob 	 * Check the rework (continuation) work queue first.
37312df76c16SMatt Jacob 	 */
37322df76c16SMatt Jacob 	ccbh = TAILQ_FIRST(&softc->rework_queue);
37332df76c16SMatt Jacob 	if (ccbh) {
37342df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *)ccbh;
37352df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
37362df76c16SMatt Jacob 		more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
373794dff771SMatt Jacob 		queue = "rework";
37382df76c16SMatt Jacob 	} else {
37392df76c16SMatt Jacob 		ccbh = TAILQ_FIRST(&softc->work_queue);
37402df76c16SMatt Jacob 		if (ccbh == NULL) {
37412df76c16SMatt Jacob 			xpt_release_ccb(iccb);
37422df76c16SMatt Jacob 			return;
37432df76c16SMatt Jacob 		}
37442df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *)ccbh;
37452df76c16SMatt Jacob 		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
37462df76c16SMatt Jacob 		more = TAILQ_FIRST(&softc->work_queue) != NULL;
374794dff771SMatt Jacob 		queue = "work";
37482df76c16SMatt Jacob 	}
374994dff771SMatt Jacob 	ATIO_PPD(atio)->on_queue = 0;
37502df76c16SMatt Jacob 
37512df76c16SMatt Jacob 	if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
37522df76c16SMatt Jacob 		panic("BAD ATIO");
37532df76c16SMatt Jacob 	}
37542df76c16SMatt Jacob 
375594dff771SMatt Jacob 	data_len = is_write = 0;
37562df76c16SMatt Jacob 	data_ptr = NULL;
37572df76c16SMatt Jacob 	csio = &iccb->csio;
37582df76c16SMatt Jacob 	status = SCSI_STATUS_OK;
37592df76c16SMatt Jacob 	flags = CAM_SEND_STATUS;
37602df76c16SMatt Jacob 	memset(&atio->sense_data, 0, sizeof (atio->sense_data));
37612df76c16SMatt Jacob 	cdb = atio->cdb_io.cdb_bytes;
376294dff771SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from %s queue initiator 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id,
376394dff771SMatt Jacob 	    queue, atio->init_id, cdb[0], ATIO_PPD(atio)->offset);
37642df76c16SMatt Jacob 
37652df76c16SMatt Jacob 	return_lun = XS_LUN(atio);
37662df76c16SMatt Jacob 	if (return_lun != 0) {
37672df76c16SMatt Jacob 		xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
37682df76c16SMatt Jacob 		if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
37692df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
3770387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3771387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3772387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->add_sense_code = 0x25;	/* LOGICAL UNIT NOT SUPPORTED */
3773f6683e55SMatt Jacob 			atio->sense_len = SSD_MIN_SIZE;
37742df76c16SMatt Jacob 		}
37752df76c16SMatt Jacob 		return_lun = CAM_LUN_WILDCARD;
37762df76c16SMatt Jacob 	}
37772df76c16SMatt Jacob 
37782df76c16SMatt Jacob 	switch (cdb[0]) {
37792df76c16SMatt Jacob 	case REQUEST_SENSE:
37802df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
37812df76c16SMatt Jacob 		data_len = sizeof (atio->sense_data);
37822df76c16SMatt Jacob 		junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
37832df76c16SMatt Jacob 		memset(junk_data+1, 0, data_len-1);
37842df76c16SMatt Jacob 		if (data_len > cdb[4]) {
37852df76c16SMatt Jacob 			data_len = cdb[4];
37862df76c16SMatt Jacob 		}
37872df76c16SMatt Jacob 		if (data_len) {
37882df76c16SMatt Jacob 			data_ptr = junk_data;
37892df76c16SMatt Jacob 		}
37902df76c16SMatt Jacob 		break;
37912df76c16SMatt Jacob 	case WRITE_6:
37922df76c16SMatt Jacob 	case WRITE_10:
37932df76c16SMatt Jacob 	case WRITE_12:
37942df76c16SMatt Jacob 	case WRITE_16:
379594dff771SMatt Jacob 		is_write = 1;
379694dff771SMatt Jacob 		/* FALLTHROUGH */
379794dff771SMatt Jacob 	case READ_6:
379894dff771SMatt Jacob 	case READ_10:
379994dff771SMatt Jacob 	case READ_12:
380094dff771SMatt Jacob 	case READ_16:
380194dff771SMatt Jacob 		is_data_cmd = 1;
380294dff771SMatt Jacob 		r = isptarg_rwparm(cdb, disk_data, disk_size, ATIO_PPD(atio)->offset, &data_ptr, &data_len, &last);
3803387d8239SMatt Jacob 		if (r != 0) {
38042df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
3805387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3806387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3807387d8239SMatt Jacob 			if (r == -1) {
3808387d8239SMatt Jacob 				SDFIXED(atio->sense_data)->add_sense_code = 0x21;	/* LOGICAL BLOCK ADDRESS OUT OF RANGE */
3809387d8239SMatt Jacob 			} else {
3810387d8239SMatt Jacob 				SDFIXED(atio->sense_data)->add_sense_code = 0x20;	/* INVALID COMMAND OPERATION CODE */
3811387d8239SMatt Jacob 			}
3812f6683e55SMatt Jacob 			atio->sense_len = SSD_MIN_SIZE;
38132df76c16SMatt Jacob 		} else {
381494dff771SMatt Jacob #ifdef	ISP_SEPARATE_STATUS
38152df76c16SMatt Jacob 			if (last && data_len) {
38162df76c16SMatt Jacob 				last = 0;
38172df76c16SMatt Jacob 			}
38182df76c16SMatt Jacob #endif
38192df76c16SMatt Jacob 			if (last == 0) {
38202df76c16SMatt Jacob 				flags &= ~CAM_SEND_STATUS;
38212df76c16SMatt Jacob 			}
38222df76c16SMatt Jacob 			if (data_len) {
382394dff771SMatt Jacob 				ATIO_PPD(atio)->offset += data_len;
382494dff771SMatt Jacob 				if (is_write)
38252df76c16SMatt Jacob 					flags |= CAM_DIR_OUT;
382694dff771SMatt Jacob 				else
382794dff771SMatt Jacob 					flags |= CAM_DIR_IN;
38282df76c16SMatt Jacob 			} else {
38292df76c16SMatt Jacob 				flags |= CAM_DIR_NONE;
38302df76c16SMatt Jacob 			}
38312df76c16SMatt Jacob 		}
38322df76c16SMatt Jacob 		break;
38332df76c16SMatt Jacob 	case INQUIRY:
38342df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
38352df76c16SMatt Jacob 		if (cdb[1] || cdb[2] || cdb[3]) {
38362df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
3837387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3838387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3839387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->add_sense_code = 0x24;	/* INVALID FIELD IN CDB */
3840f6683e55SMatt Jacob 			atio->sense_len = SSD_MIN_SIZE;
38412df76c16SMatt Jacob 			break;
38422df76c16SMatt Jacob 		}
38432df76c16SMatt Jacob 		data_len = sizeof (iqd);
38442df76c16SMatt Jacob 		if (data_len > cdb[4]) {
38452df76c16SMatt Jacob 			data_len = cdb[4];
38462df76c16SMatt Jacob 		}
38472df76c16SMatt Jacob 		if (data_len) {
38482df76c16SMatt Jacob 			if (XS_LUN(iccb) != 0) {
38492df76c16SMatt Jacob 				memcpy(junk_data, niliqd, sizeof (iqd));
38502df76c16SMatt Jacob 			} else {
38512df76c16SMatt Jacob 				memcpy(junk_data, iqd, sizeof (iqd));
38522df76c16SMatt Jacob 			}
38532df76c16SMatt Jacob 			data_ptr = junk_data;
38542df76c16SMatt Jacob 		}
38552df76c16SMatt Jacob 		break;
38562df76c16SMatt Jacob 	case TEST_UNIT_READY:
38572df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
38582df76c16SMatt Jacob 		if (ca) {
38592df76c16SMatt Jacob 			ca = 0;
38602df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
3861387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3862387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3863387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->add_sense_code = 0x29;	/* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
3864f6683e55SMatt Jacob 			atio->sense_len = SSD_MIN_SIZE;
38652df76c16SMatt Jacob 		}
38662df76c16SMatt Jacob 		break;
38672df76c16SMatt Jacob 	case SYNCHRONIZE_CACHE:
38682df76c16SMatt Jacob 	case START_STOP:
38692df76c16SMatt Jacob 	case RESERVE:
38702df76c16SMatt Jacob 	case RELEASE:
38712df76c16SMatt Jacob 	case VERIFY_10:
38722df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
38732df76c16SMatt Jacob 		break;
38742df76c16SMatt Jacob 
38752df76c16SMatt Jacob 	case READ_CAPACITY:
38762df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
38772df76c16SMatt Jacob 		if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
38782df76c16SMatt Jacob 			status = SCSI_STATUS_CHECK_COND;
3879387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3880387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3881387d8239SMatt Jacob 			SDFIXED(atio->sense_data)->add_sense_code =  0x24;	/* INVALID FIELD IN CDB */
3882f6683e55SMatt Jacob 			atio->sense_len = SSD_MIN_SIZE;
38832df76c16SMatt Jacob 			break;
38842df76c16SMatt Jacob 		}
38852df76c16SMatt Jacob 		if (cdb[8] & 0x1) { /* PMI */
38862df76c16SMatt Jacob 			junk_data[0] = 0xff;
38872df76c16SMatt Jacob 			junk_data[1] = 0xff;
38882df76c16SMatt Jacob 			junk_data[2] = 0xff;
38892df76c16SMatt Jacob 			junk_data[3] = 0xff;
38902df76c16SMatt Jacob 		} else {
38912df76c16SMatt Jacob 			uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
38922df76c16SMatt Jacob 			if (last_blk < 0xffffffffULL) {
38932df76c16SMatt Jacob 			    junk_data[0] = (last_blk >> 24) & 0xff;
38942df76c16SMatt Jacob 			    junk_data[1] = (last_blk >> 16) & 0xff;
38952df76c16SMatt Jacob 			    junk_data[2] = (last_blk >>  8) & 0xff;
38962df76c16SMatt Jacob 			    junk_data[3] = (last_blk) & 0xff;
38972df76c16SMatt Jacob 			} else {
38982df76c16SMatt Jacob 			    junk_data[0] = 0xff;
38992df76c16SMatt Jacob 			    junk_data[1] = 0xff;
39002df76c16SMatt Jacob 			    junk_data[2] = 0xff;
39012df76c16SMatt Jacob 			    junk_data[3] = 0xff;
39022df76c16SMatt Jacob 			}
39032df76c16SMatt Jacob 		}
39042df76c16SMatt Jacob 		junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
39052df76c16SMatt Jacob 		junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
39062df76c16SMatt Jacob 		junk_data[6] = ((1 << DISK_SHIFT) >>  8) & 0xff;
39072df76c16SMatt Jacob 		junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
39082df76c16SMatt Jacob 		data_ptr = junk_data;
39092df76c16SMatt Jacob 		data_len = 8;
39102df76c16SMatt Jacob 		break;
39112df76c16SMatt Jacob 	case REPORT_LUNS:
39122df76c16SMatt Jacob 		flags |= CAM_DIR_IN;
39132df76c16SMatt Jacob 		memset(junk_data, 0, JUNK_SIZE);
39142df76c16SMatt Jacob 		junk_data[0] = (1 << 3) >> 24;
39152df76c16SMatt Jacob 		junk_data[1] = (1 << 3) >> 16;
39162df76c16SMatt Jacob 		junk_data[2] = (1 << 3) >> 8;
39172df76c16SMatt Jacob 		junk_data[3] = (1 << 3);
39182df76c16SMatt Jacob 		ptr = NULL;
39192df76c16SMatt Jacob 		for (i = 0; i < 1; i++) {
392094dff771SMatt Jacob 			ptr = &junk_data[8 + (i << 3)];
39212df76c16SMatt Jacob 			if (i >= 256) {
39222df76c16SMatt Jacob 				ptr[0] = 0x40 | ((i >> 8) & 0x3f);
39232df76c16SMatt Jacob 			}
39242df76c16SMatt Jacob 			ptr[1] = i;
39252df76c16SMatt Jacob 		}
39262df76c16SMatt Jacob 		data_ptr = junk_data;
39272df76c16SMatt Jacob 		data_len = (ptr + 8) - junk_data;
39282df76c16SMatt Jacob 		break;
39292df76c16SMatt Jacob 
39302df76c16SMatt Jacob 	default:
39312df76c16SMatt Jacob 		flags |= CAM_DIR_NONE;
39322df76c16SMatt Jacob 		status = SCSI_STATUS_CHECK_COND;
3933387d8239SMatt Jacob 		SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3934387d8239SMatt Jacob 		SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3935387d8239SMatt Jacob 		SDFIXED(atio->sense_data)->add_sense_code = 0x20;	/* INVALID COMMAND OPERATION CODE */
3936f6683e55SMatt Jacob 		atio->sense_len = SSD_MIN_SIZE;
39372df76c16SMatt Jacob 		break;
39382df76c16SMatt Jacob 	}
39392df76c16SMatt Jacob 
39402df76c16SMatt Jacob 	/*
39412df76c16SMatt Jacob 	 * If we are done with the transaction, tell the
39422df76c16SMatt Jacob 	 * controller to send status and perform a CMD_CMPLT.
39432df76c16SMatt Jacob 	 * If we have associated sense data, see if we can
39442df76c16SMatt Jacob 	 * send that too.
39452df76c16SMatt Jacob 	 */
39462df76c16SMatt Jacob 	if (status == SCSI_STATUS_CHECK_COND) {
39472df76c16SMatt Jacob 		flags |= CAM_SEND_SENSE;
39482df76c16SMatt Jacob 		csio->sense_len = atio->sense_len;
39492df76c16SMatt Jacob 		csio->sense_data = atio->sense_data;
39502df76c16SMatt Jacob 		flags &= ~CAM_DIR_MASK;
39512df76c16SMatt Jacob 		data_len = 0;
39522df76c16SMatt Jacob 		data_ptr = NULL;
39532df76c16SMatt Jacob 	}
395494dff771SMatt Jacob 	cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 30 * hz);
39552df76c16SMatt Jacob 	iccb->ccb_h.target_id = atio->ccb_h.target_id;
39562df76c16SMatt Jacob 	iccb->ccb_h.target_lun = return_lun;
39572df76c16SMatt Jacob 	iccb->ccb_h.ccb_atio = atio;
395894dff771SMatt Jacob 	CTIO_SEQ(iccb) = ATIO_PPD(atio)->sequence++;
395994dff771SMatt Jacob 	ATIO_PPD(atio)->ctio_cnt++;
396094dff771SMatt Jacob 	if (flags & CAM_SEND_STATUS) {
396194dff771SMatt Jacob 		KASSERT((ATIO_PPD(atio)->status_sent == 0), ("we have already sent status for 0x%x in %s", atio->tag_id, __func__));
396294dff771SMatt Jacob 		ATIO_PPD(atio)->status_sent = 1;
396394dff771SMatt Jacob 	}
396494dff771SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: sending downstream for  0x%x sequence %u len %u flags %x\n", __func__, atio->tag_id, CTIO_SEQ(iccb), data_len, flags);
39652df76c16SMatt Jacob 	xpt_action(iccb);
39662df76c16SMatt Jacob 
39672df76c16SMatt Jacob 	if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
39682df76c16SMatt Jacob 		cam_release_devq(periph->path, 0, 0, 0, 0);
39692df76c16SMatt Jacob 		atio->ccb_h.status &= ~CAM_DEV_QFRZN;
39702df76c16SMatt Jacob 	}
397194dff771SMatt Jacob #ifdef	ISP_MULTI_CCBS
397294dff771SMatt Jacob 	if (is_data_cmd && ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->ctio_cnt < MULTI_CCB_DATA_CNT && ATIO_PPD(atio)->on_queue == 0) {
397394dff771SMatt Jacob 		ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: more still to do for 0x%x\n", __func__, atio->tag_id);
397494dff771SMatt Jacob 		TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
397594dff771SMatt Jacob 		ATIO_PPD(atio)->on_queue = 1;
397694dff771SMatt Jacob 		more = 1;
397794dff771SMatt Jacob 	}
397894dff771SMatt Jacob #endif
39792df76c16SMatt Jacob 	if (more) {
39802df76c16SMatt Jacob 		xpt_schedule(periph, 1);
39812df76c16SMatt Jacob 	}
39822df76c16SMatt Jacob }
39832df76c16SMatt Jacob 
39842df76c16SMatt Jacob static cam_status
39852df76c16SMatt Jacob isptargctor(struct cam_periph *periph, void *arg)
39862df76c16SMatt Jacob {
39872df76c16SMatt Jacob 	struct isptarg_softc *softc;
39882df76c16SMatt Jacob 
39892df76c16SMatt Jacob 	softc = (struct isptarg_softc *)arg;
39902df76c16SMatt Jacob 	periph->softc = softc;
39912df76c16SMatt Jacob 	softc->periph = periph;
39922df76c16SMatt Jacob 	softc->path = periph->path;
3993387d8239SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
39942df76c16SMatt Jacob 	return (CAM_REQ_CMP);
39952df76c16SMatt Jacob }
39962df76c16SMatt Jacob 
39972df76c16SMatt Jacob static void
39982df76c16SMatt Jacob isptargdtor(struct cam_periph *periph)
39992df76c16SMatt Jacob {
40002df76c16SMatt Jacob 	struct isptarg_softc *softc;
40012df76c16SMatt Jacob 	softc = (struct isptarg_softc *)periph->softc;
4002387d8239SMatt Jacob 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
40032df76c16SMatt Jacob 	softc->periph = NULL;
40042df76c16SMatt Jacob 	softc->path = NULL;
40052df76c16SMatt Jacob 	periph->softc = NULL;
40062df76c16SMatt Jacob }
40072df76c16SMatt Jacob 
40082df76c16SMatt Jacob static void
40092df76c16SMatt Jacob isptarg_done(struct cam_periph *periph, union ccb *ccb)
40102df76c16SMatt Jacob {
40112df76c16SMatt Jacob 	struct isptarg_softc *softc;
40122df76c16SMatt Jacob 	ispsoftc_t *isp;
401394dff771SMatt Jacob 	uint32_t newoff;
40142df76c16SMatt Jacob 	struct ccb_accept_tio *atio;
40152df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
40162df76c16SMatt Jacob 	cam_status status;
40172df76c16SMatt Jacob 
40182df76c16SMatt Jacob 	softc = (struct isptarg_softc *)periph->softc;
40192df76c16SMatt Jacob 	isp = softc->isp;
40202df76c16SMatt Jacob 	status = ccb->ccb_h.status & CAM_STATUS_MASK;
40212df76c16SMatt Jacob 
40222df76c16SMatt Jacob 	switch (ccb->ccb_h.func_code) {
40232df76c16SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:
40242df76c16SMatt Jacob 		atio = (struct ccb_accept_tio *) ccb;
402594dff771SMatt Jacob 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
402694dff771SMatt Jacob 		memset(ATIO_PPD(atio), 0, sizeof (ppd_t));
40272df76c16SMatt Jacob 		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
402894dff771SMatt Jacob 		ATIO_PPD(atio)->on_queue = 1;
40292df76c16SMatt Jacob 		xpt_schedule(periph, 1);
40302df76c16SMatt Jacob 		break;
40312df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
40322df76c16SMatt Jacob 		inot = (struct ccb_immediate_notify *) ccb;
403394dff771SMatt 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__);
40342df76c16SMatt Jacob 		TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
40352df76c16SMatt Jacob 		xpt_schedule(periph, 1);
40362df76c16SMatt Jacob 		break;
40372df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
403894dff771SMatt Jacob 		atio = ccb->ccb_h.ccb_atio;
403994dff771SMatt Jacob 		KASSERT((ATIO_PPD(atio)->ctio_cnt != 0), ("ctio zero when finishing a CTIO"));
404094dff771SMatt Jacob 		ATIO_PPD(atio)->ctio_cnt--;
404194dff771SMatt Jacob 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
404294dff771SMatt Jacob 			switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
404394dff771SMatt Jacob 			case CAM_MESSAGE_RECV:
404494dff771SMatt Jacob 				newoff = (ccb->csio.msg_ptr[3] << 24) | (ccb->csio.msg_ptr[4] << 16) | (ccb->csio.msg_ptr[5] << 8) | (ccb->csio.msg_ptr[6]);
404594dff771SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "[0x%x] got message to return to reset offset to 0x%x at sequence %u\n", atio->tag_id, newoff, CTIO_SEQ(ccb));
404694dff771SMatt Jacob 				ATIO_PPD(atio)->offset = newoff;
404794dff771SMatt Jacob 				ATIO_PPD(atio)->status_sent = 0;
404894dff771SMatt Jacob 				if (ATIO_PPD(atio)->on_queue == 0) {
404994dff771SMatt Jacob 					TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
405094dff771SMatt Jacob 					ATIO_PPD(atio)->on_queue = 1;
405194dff771SMatt Jacob 				}
405294dff771SMatt Jacob 				xpt_schedule(periph, 1);
405394dff771SMatt Jacob 				break;
405494dff771SMatt Jacob 			default:
405594dff771SMatt Jacob 				cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
405694dff771SMatt Jacob 				xpt_action((union ccb *)atio);
405794dff771SMatt Jacob 				break;
405894dff771SMatt Jacob 			}
405994dff771SMatt Jacob 		} else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
406094dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
406194dff771SMatt Jacob 			if (ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->on_queue == 0) {
406294dff771SMatt Jacob 				TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
406394dff771SMatt Jacob 				ATIO_PPD(atio)->on_queue = 1;
406494dff771SMatt Jacob 			}
406594dff771SMatt Jacob 			xpt_schedule(periph, 1);
406694dff771SMatt Jacob 		} else {
406794dff771SMatt Jacob 			KASSERT((ATIO_PPD(atio)->ctio_cnt == 0), ("ctio count still %d when we think we've sent the STATUS ctio", ATIO_PPD(atio)->ctio_cnt));
406894dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
406994dff771SMatt Jacob 			xpt_action((union ccb *)atio);
407094dff771SMatt Jacob 		}
40712df76c16SMatt Jacob 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
40722df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
40732df76c16SMatt Jacob 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
40742df76c16SMatt Jacob 		}
40752df76c16SMatt Jacob 		xpt_release_ccb(ccb);
40762df76c16SMatt Jacob 		break;
40772df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
40782df76c16SMatt Jacob 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
40792df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
40802df76c16SMatt Jacob 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
40812df76c16SMatt Jacob 		}
40822df76c16SMatt Jacob 		inot = ccb->ccb_h.ccb_inot;
4083387d8239SMatt Jacob 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
40842df76c16SMatt Jacob 		xpt_release_ccb(ccb);
40852df76c16SMatt Jacob 		xpt_action((union ccb *)inot);
40862df76c16SMatt Jacob 		break;
40872df76c16SMatt Jacob 	default:
40882df76c16SMatt Jacob 		xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
40892df76c16SMatt Jacob 		break;
40902df76c16SMatt Jacob 	}
40912df76c16SMatt Jacob }
40922df76c16SMatt Jacob 
40932df76c16SMatt Jacob static void
40942df76c16SMatt Jacob isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
40952df76c16SMatt Jacob {
40962df76c16SMatt Jacob 	struct ac_contract *acp = arg;
40972df76c16SMatt Jacob 	struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
40982df76c16SMatt Jacob 
40992df76c16SMatt Jacob 	if (code != AC_CONTRACT) {
41002df76c16SMatt Jacob 		return;
41012df76c16SMatt Jacob 	}
41022df76c16SMatt Jacob 	xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
41032df76c16SMatt Jacob }
41042df76c16SMatt Jacob 
41052df76c16SMatt Jacob static void
41062df76c16SMatt Jacob isp_target_thread(ispsoftc_t *isp, int chan)
41072df76c16SMatt Jacob {
41082df76c16SMatt Jacob 	union ccb *ccb = NULL;
41092df76c16SMatt Jacob 	int i;
41102df76c16SMatt Jacob 	void *wchan;
41112df76c16SMatt Jacob 	cam_status status;
41122df76c16SMatt Jacob 	struct isptarg_softc *softc = NULL;
41132df76c16SMatt Jacob 	struct cam_periph *periph = NULL, *wperiph = NULL;
41142df76c16SMatt Jacob 	struct cam_path *path, *wpath;
41152df76c16SMatt Jacob 	struct cam_sim *sim;
41162df76c16SMatt Jacob 
41172df76c16SMatt Jacob 	if (disk_data == NULL) {
41182df76c16SMatt Jacob 		disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
41192df76c16SMatt Jacob 		if (disk_size < (50 << 20)) {
41202df76c16SMatt Jacob 			disk_size = 50 << 20;
41212df76c16SMatt Jacob 		}
41222df76c16SMatt Jacob 		disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
41232df76c16SMatt Jacob 		if (disk_data == NULL) {
41242df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
41252df76c16SMatt Jacob 			goto out;
41262df76c16SMatt Jacob 		}
41272df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
41282df76c16SMatt Jacob 	}
41292df76c16SMatt Jacob 	junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
41302df76c16SMatt Jacob 	if (junk_data == NULL) {
41312df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
41322df76c16SMatt Jacob 		goto out;
41332df76c16SMatt Jacob 	}
41342df76c16SMatt Jacob 
41352df76c16SMatt Jacob 
41362df76c16SMatt Jacob 	softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
41372df76c16SMatt Jacob 	if (softc == NULL) {
41382df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
41392df76c16SMatt Jacob 		goto out;
41402df76c16SMatt Jacob 	}
41412df76c16SMatt Jacob 	TAILQ_INIT(&softc->work_queue);
41422df76c16SMatt Jacob 	TAILQ_INIT(&softc->rework_queue);
41432df76c16SMatt Jacob 	TAILQ_INIT(&softc->running_queue);
41442df76c16SMatt Jacob 	TAILQ_INIT(&softc->inot_queue);
41452df76c16SMatt Jacob 	softc->isp = isp;
41462df76c16SMatt Jacob 
41472df76c16SMatt Jacob 	periphdriver_register(&isptargdriver);
41482df76c16SMatt Jacob 	ISP_GET_PC(isp, chan, sim, sim);
41492df76c16SMatt Jacob 	ISP_GET_PC(isp, chan, path,  path);
4150227d67aaSAlexander Motin 	status = xpt_create_path(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
41512df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
41522df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
41532df76c16SMatt Jacob 		return;
41542df76c16SMatt Jacob 	}
4155227d67aaSAlexander Motin 	status = xpt_create_path(&path, NULL, cam_sim_path(sim), 0, 0);
41562df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
41572df76c16SMatt Jacob 		xpt_free_path(wpath);
41582df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
41592df76c16SMatt Jacob 		return;
41602df76c16SMatt Jacob 	}
41612df76c16SMatt Jacob 
41622df76c16SMatt Jacob 	ISP_LOCK(isp);
41632df76c16SMatt Jacob 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
41642df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
41652df76c16SMatt Jacob 		ISP_UNLOCK(isp);
41662df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
41672df76c16SMatt Jacob 		goto out;
41682df76c16SMatt Jacob 	}
41692df76c16SMatt Jacob 	wperiph = cam_periph_find(wpath, "isptarg");
41702df76c16SMatt Jacob 	if (wperiph == NULL) {
41712df76c16SMatt Jacob 		ISP_UNLOCK(isp);
41722df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
41732df76c16SMatt Jacob 		goto out;
41742df76c16SMatt Jacob 	}
41752df76c16SMatt Jacob 
41762df76c16SMatt Jacob 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
41772df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
41782df76c16SMatt Jacob 		ISP_UNLOCK(isp);
41792df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
41802df76c16SMatt Jacob 		goto out;
41812df76c16SMatt Jacob 	}
41822df76c16SMatt Jacob 
41832df76c16SMatt Jacob 	periph = cam_periph_find(path, "isptarg");
41842df76c16SMatt Jacob 	if (periph == NULL) {
41852df76c16SMatt Jacob 		ISP_UNLOCK(isp);
41862df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
41872df76c16SMatt Jacob 		goto out;
41882df76c16SMatt Jacob 	}
41892df76c16SMatt Jacob 
41902df76c16SMatt Jacob 	status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
41912df76c16SMatt Jacob 	if (status != CAM_REQ_CMP) {
41922df76c16SMatt Jacob 		ISP_UNLOCK(isp);
41932df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
41942df76c16SMatt Jacob 		goto out;
41952df76c16SMatt Jacob 	}
41962df76c16SMatt Jacob 
41972df76c16SMatt Jacob 	ISP_UNLOCK(isp);
41982df76c16SMatt Jacob 
41992df76c16SMatt Jacob 	ccb = xpt_alloc_ccb();
42002df76c16SMatt Jacob 
42012df76c16SMatt Jacob 	/*
42022df76c16SMatt Jacob 	 * Make sure role is none.
42032df76c16SMatt Jacob 	 */
42042df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
42052df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
42062df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
42072df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
42082df76c16SMatt Jacob 
42092df76c16SMatt Jacob 	ISP_LOCK(isp);
42102df76c16SMatt Jacob 	xpt_action(ccb);
42112df76c16SMatt Jacob 	ISP_UNLOCK(isp);
42122df76c16SMatt Jacob 
42132df76c16SMatt Jacob 	/*
42142df76c16SMatt Jacob 	 * Now enable luns
42152df76c16SMatt Jacob 	 */
42162df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
42172df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_EN_LUN;
42182df76c16SMatt Jacob 	ccb->cel.enable = 1;
42192df76c16SMatt Jacob 	ISP_LOCK(isp);
42202df76c16SMatt Jacob 	xpt_action(ccb);
42212df76c16SMatt Jacob 	ISP_UNLOCK(isp);
42222df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
42232df76c16SMatt Jacob 		xpt_free_ccb(ccb);
42242df76c16SMatt Jacob 		xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
42252df76c16SMatt Jacob 		goto out;
42262df76c16SMatt Jacob 	}
42272df76c16SMatt Jacob 
42282df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
42292df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_EN_LUN;
42302df76c16SMatt Jacob 	ccb->cel.enable = 1;
42312df76c16SMatt Jacob 	ISP_LOCK(isp);
42322df76c16SMatt Jacob 	xpt_action(ccb);
42332df76c16SMatt Jacob 	ISP_UNLOCK(isp);
42342df76c16SMatt Jacob 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
42352df76c16SMatt Jacob 		xpt_free_ccb(ccb);
42362df76c16SMatt Jacob 		xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
42372df76c16SMatt Jacob 		goto out;
42382df76c16SMatt Jacob 	}
42392df76c16SMatt Jacob 	xpt_free_ccb(ccb);
42402df76c16SMatt Jacob 
42412df76c16SMatt Jacob 	/*
42422df76c16SMatt Jacob 	 * Add resources
42432df76c16SMatt Jacob 	 */
42441d0a1de2SWill Andrews 	ISP_GET_PC(isp, chan, target_proc, wchan);
42452df76c16SMatt Jacob 	for (i = 0; i < 4; i++) {
42462df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
42472df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
42482df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
42492df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
425094dff771SMatt Jacob 		ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
42512df76c16SMatt Jacob 		ISP_LOCK(isp);
42522df76c16SMatt Jacob 		xpt_action(ccb);
42532df76c16SMatt Jacob 		ISP_UNLOCK(isp);
42542df76c16SMatt Jacob 	}
42552df76c16SMatt Jacob 	for (i = 0; i < NISP_TARG_CMDS; i++) {
42562df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
42572df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
42582df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
42592df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
426094dff771SMatt Jacob 		ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
42612df76c16SMatt Jacob 		ISP_LOCK(isp);
42622df76c16SMatt Jacob 		xpt_action(ccb);
42632df76c16SMatt Jacob 		ISP_UNLOCK(isp);
42642df76c16SMatt Jacob 	}
42652df76c16SMatt Jacob 	for (i = 0; i < 4; i++) {
42662df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
42672df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
42682df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
42692df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
42702df76c16SMatt Jacob 		ISP_LOCK(isp);
42712df76c16SMatt Jacob 		xpt_action(ccb);
42722df76c16SMatt Jacob 		ISP_UNLOCK(isp);
42732df76c16SMatt Jacob 	}
42742df76c16SMatt Jacob 	for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
42752df76c16SMatt Jacob 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
42762df76c16SMatt Jacob 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
42772df76c16SMatt Jacob 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
42782df76c16SMatt Jacob 		ccb->ccb_h.cbfcnp = isptarg_done;
42792df76c16SMatt Jacob 		ISP_LOCK(isp);
42802df76c16SMatt Jacob 		xpt_action(ccb);
42812df76c16SMatt Jacob 		ISP_UNLOCK(isp);
42822df76c16SMatt Jacob 	}
42832df76c16SMatt Jacob 
42842df76c16SMatt Jacob 	/*
42852df76c16SMatt Jacob 	 * Now turn it all back on
42862df76c16SMatt Jacob 	 */
42872df76c16SMatt Jacob 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
42882df76c16SMatt Jacob 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
42892df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
42902df76c16SMatt Jacob 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
42912df76c16SMatt Jacob 	ISP_LOCK(isp);
42922df76c16SMatt Jacob 	xpt_action(ccb);
42932df76c16SMatt Jacob 	ISP_UNLOCK(isp);
42942df76c16SMatt Jacob 
42952df76c16SMatt Jacob 	/*
42962df76c16SMatt Jacob 	 * Okay, while things are still active, sleep...
42972df76c16SMatt Jacob 	 */
42982df76c16SMatt Jacob 	ISP_LOCK(isp);
42992df76c16SMatt Jacob 	for (;;) {
43002df76c16SMatt Jacob 		ISP_GET_PC(isp, chan, proc_active, i);
43012df76c16SMatt Jacob 		if (i == 0) {
43022df76c16SMatt Jacob 			break;
43032df76c16SMatt Jacob 		}
43042df76c16SMatt Jacob 		msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
43052df76c16SMatt Jacob 	}
43062df76c16SMatt Jacob 	ISP_UNLOCK(isp);
43072df76c16SMatt Jacob 
43082df76c16SMatt Jacob out:
43092df76c16SMatt Jacob 	if (wperiph) {
43102df76c16SMatt Jacob 		cam_periph_invalidate(wperiph);
43112df76c16SMatt Jacob 	}
43122df76c16SMatt Jacob 	if (periph) {
43132df76c16SMatt Jacob 		cam_periph_invalidate(periph);
43142df76c16SMatt Jacob 	}
43152df76c16SMatt Jacob 	if (junk_data) {
43162df76c16SMatt Jacob 		free(junk_data, M_ISPTARG);
43172df76c16SMatt Jacob 	}
43182df76c16SMatt Jacob 	if (disk_data) {
43192df76c16SMatt Jacob 		free(disk_data, M_ISPTARG);
43202df76c16SMatt Jacob 	}
43212df76c16SMatt Jacob 	if (softc) {
43222df76c16SMatt Jacob 		free(softc, M_ISPTARG);
43232df76c16SMatt Jacob 	}
43242df76c16SMatt Jacob 	xpt_free_path(path);
43252df76c16SMatt Jacob 	xpt_free_path(wpath);
43262df76c16SMatt Jacob }
43272df76c16SMatt Jacob 
43282df76c16SMatt Jacob static void
43292df76c16SMatt Jacob isp_target_thread_pi(void *arg)
43302df76c16SMatt Jacob {
43312df76c16SMatt Jacob 	struct isp_spi *pi = arg;
43321d0a1de2SWill Andrews 	ispsoftc_t *isp = cam_sim_softc(pi->sim);
43331d0a1de2SWill Andrews 	int chan = cam_sim_bus(pi->sim);
43341d0a1de2SWill Andrews 
43351d0a1de2SWill Andrews 	isp_target_thread(isp, chan);
43361d0a1de2SWill Andrews 	ISP_SPI_PC(isp, chan)->num_threads -= 1;
43371d0a1de2SWill Andrews 	kthread_exit();
43382df76c16SMatt Jacob }
43392df76c16SMatt Jacob 
43402df76c16SMatt Jacob static void
43412df76c16SMatt Jacob isp_target_thread_fc(void *arg)
43422df76c16SMatt Jacob {
43432df76c16SMatt Jacob 	struct isp_fc *fc = arg;
43441d0a1de2SWill Andrews 	ispsoftc_t *isp = cam_sim_softc(pi->sim);
43451d0a1de2SWill Andrews 	int chan = cam_sim_bus(pi->sim);
43461d0a1de2SWill Andrews 
43471d0a1de2SWill Andrews 	isp_target_thread(isp, chan);
43481d0a1de2SWill Andrews 	ISP_FC_PC(isp, chan)->num_threads -= 1;
43491d0a1de2SWill Andrews 	kthread_exit();
43502df76c16SMatt Jacob }
43512df76c16SMatt Jacob 
43522df76c16SMatt Jacob static int
43532df76c16SMatt Jacob isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
43542df76c16SMatt Jacob {
43552df76c16SMatt Jacob 	uint32_t cnt, curcnt;
43562df76c16SMatt Jacob 	uint64_t lba;
43572df76c16SMatt Jacob 
43582df76c16SMatt Jacob 	switch (cdb[0]) {
43592df76c16SMatt Jacob 	case WRITE_16:
43602df76c16SMatt Jacob 	case READ_16:
43612df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[10]) <<  24) |
43622df76c16SMatt Jacob 			(((uint32_t)cdb[11]) <<  16) |
43632df76c16SMatt Jacob 			(((uint32_t)cdb[12]) <<   8) |
43642df76c16SMatt Jacob 			((uint32_t)cdb[13]);
43652df76c16SMatt Jacob 
43662df76c16SMatt Jacob 		lba =	(((uint64_t)cdb[2]) << 56) |
43672df76c16SMatt Jacob 			(((uint64_t)cdb[3]) << 48) |
43682df76c16SMatt Jacob 			(((uint64_t)cdb[4]) << 40) |
43692df76c16SMatt Jacob 			(((uint64_t)cdb[5]) << 32) |
43702df76c16SMatt Jacob 			(((uint64_t)cdb[6]) << 24) |
43712df76c16SMatt Jacob 			(((uint64_t)cdb[7]) << 16) |
43722df76c16SMatt Jacob 			(((uint64_t)cdb[8]) <<  8) |
43732df76c16SMatt Jacob 			((uint64_t)cdb[9]);
43742df76c16SMatt Jacob 		break;
43752df76c16SMatt Jacob 	case WRITE_12:
43762df76c16SMatt Jacob 	case READ_12:
43772df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[6]) <<  16) |
43782df76c16SMatt Jacob 			(((uint32_t)cdb[7]) <<   8) |
43792df76c16SMatt Jacob 			((u_int32_t)cdb[8]);
43802df76c16SMatt Jacob 
43812df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[2]) << 24) |
43822df76c16SMatt Jacob 			(((uint32_t)cdb[3]) << 16) |
43832df76c16SMatt Jacob 			(((uint32_t)cdb[4]) <<  8) |
43842df76c16SMatt Jacob 			((uint32_t)cdb[5]);
43852df76c16SMatt Jacob 		break;
43862df76c16SMatt Jacob 	case WRITE_10:
43872df76c16SMatt Jacob 	case READ_10:
43882df76c16SMatt Jacob 		cnt =	(((uint32_t)cdb[7]) <<  8) |
43892df76c16SMatt Jacob 			((u_int32_t)cdb[8]);
43902df76c16SMatt Jacob 
43912df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[2]) << 24) |
43922df76c16SMatt Jacob 			(((uint32_t)cdb[3]) << 16) |
43932df76c16SMatt Jacob 			(((uint32_t)cdb[4]) <<  8) |
43942df76c16SMatt Jacob 			((uint32_t)cdb[5]);
43952df76c16SMatt Jacob 		break;
43962df76c16SMatt Jacob 	case WRITE_6:
43972df76c16SMatt Jacob 	case READ_6:
43982df76c16SMatt Jacob 		cnt = cdb[4];
43992df76c16SMatt Jacob 		if (cnt == 0) {
44002df76c16SMatt Jacob 			cnt = 256;
44012df76c16SMatt Jacob 		}
44022df76c16SMatt Jacob 		lba =	(((uint32_t)cdb[1] & 0x1f) << 16) |
44032df76c16SMatt Jacob 			(((uint32_t)cdb[2]) << 8) |
44042df76c16SMatt Jacob 			((uint32_t)cdb[3]);
44052df76c16SMatt Jacob 		break;
44062df76c16SMatt Jacob 	default:
44072df76c16SMatt Jacob 		return (-1);
44082df76c16SMatt Jacob 	}
44092df76c16SMatt Jacob 
44102df76c16SMatt Jacob 	cnt <<= DISK_SHIFT;
44112df76c16SMatt Jacob 	lba <<= DISK_SHIFT;
44122df76c16SMatt Jacob 
44132df76c16SMatt Jacob 	if (offset == cnt) {
44142df76c16SMatt Jacob 		*lp = 1;
44152df76c16SMatt Jacob 		return (0);
44162df76c16SMatt Jacob 	}
44172df76c16SMatt Jacob 
44182df76c16SMatt Jacob 	if (lba + cnt > dl) {
4419387d8239SMatt Jacob 		return (-2);
44202df76c16SMatt Jacob 	}
44212df76c16SMatt Jacob 
44222df76c16SMatt Jacob 	curcnt = MAX_ISP_TARG_TRANSFER;
44232df76c16SMatt Jacob 	if (offset + curcnt >= cnt) {
44242df76c16SMatt Jacob 		curcnt = cnt - offset;
44252df76c16SMatt Jacob 		*lp = 1;
44262df76c16SMatt Jacob 	} else {
44272df76c16SMatt Jacob 		*lp = 0;
44282df76c16SMatt Jacob 	}
442994dff771SMatt Jacob #ifdef	ISP_MULTI_CCBS
443094dff771SMatt Jacob 	if (curcnt > MULTI_CCB_DATA_LIM)
443194dff771SMatt Jacob 		curcnt = MULTI_CCB_DATA_LIM;
443294dff771SMatt Jacob #endif
44332df76c16SMatt Jacob 	*tl = curcnt;
44342df76c16SMatt Jacob 	*kp = &dp[lba + offset];
44352df76c16SMatt Jacob 	return (0);
44362df76c16SMatt Jacob }
44372df76c16SMatt Jacob 
44382df76c16SMatt Jacob #endif
4439d81ba9d5SMatt Jacob #endif
4440d81ba9d5SMatt Jacob 
4441478f8a96SJustin T. Gibbs static void
44421dae40ebSMatt Jacob isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
4443478f8a96SJustin T. Gibbs {
4444478f8a96SJustin T. Gibbs 	struct cam_sim *sim;
4445e95725cbSMatt Jacob 	int bus, tgt;
44469cd7268eSMatt Jacob 	ispsoftc_t *isp;
4447478f8a96SJustin T. Gibbs 
4448478f8a96SJustin T. Gibbs 	sim = (struct cam_sim *)cbarg;
44499cd7268eSMatt Jacob 	isp = (ispsoftc_t *) cam_sim_softc(sim);
4450e95725cbSMatt Jacob 	bus = cam_sim_bus(sim);
4451e95725cbSMatt Jacob 	tgt = xpt_path_target_id(path);
4452e95725cbSMatt Jacob 
4453478f8a96SJustin T. Gibbs 	switch (code) {
4454478f8a96SJustin T. Gibbs 	case AC_LOST_DEVICE:
4455ab6c4b31SMatt Jacob 		if (IS_SCSI(isp)) {
44561dae40ebSMatt Jacob 			uint16_t oflags, nflags;
44572df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
4458478f8a96SJustin T. Gibbs 
445941ed683eSMatt Jacob 			if (tgt >= 0) {
44609ce9bdafSMatt Jacob 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
4461a1bc34c6SMatt Jacob #ifndef	ISP_TARGET_MODE
44629ce9bdafSMatt Jacob 				nflags &= DPARM_SAFE_DFLT;
4463a1bc34c6SMatt Jacob 				if (isp->isp_loaded_fw) {
4464478f8a96SJustin T. Gibbs 					nflags |= DPARM_NARROW | DPARM_ASYNC;
4465478f8a96SJustin T. Gibbs 				}
4466a1bc34c6SMatt Jacob #else
4467a1bc34c6SMatt Jacob 				nflags = DPARM_DEFAULT;
4468a1bc34c6SMatt Jacob #endif
44699ce9bdafSMatt Jacob 				oflags = sdp->isp_devparam[tgt].goal_flags;
44709ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = nflags;
4471478f8a96SJustin T. Gibbs 				sdp->isp_devparam[tgt].dev_update = 1;
44722df76c16SMatt Jacob 				sdp->update = 1;
44732df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
44749ce9bdafSMatt Jacob 				sdp->isp_devparam[tgt].goal_flags = oflags;
4475478f8a96SJustin T. Gibbs 			}
447641ed683eSMatt Jacob 		}
4477478f8a96SJustin T. Gibbs 		break;
4478478f8a96SJustin T. Gibbs 	default:
44793c75bb14SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
4480478f8a96SJustin T. Gibbs 		break;
4481478f8a96SJustin T. Gibbs 	}
4482478f8a96SJustin T. Gibbs }
4483478f8a96SJustin T. Gibbs 
4484478f8a96SJustin T. Gibbs static void
4485c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
4486478f8a96SJustin T. Gibbs {
44879cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
448810365e5aSMatt Jacob 	uint32_t isr;
448910365e5aSMatt Jacob 	uint16_t sema, mbox;
4490126ec864SMatt Jacob 
4491126ec864SMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
4492126ec864SMatt Jacob 		isp_intr(isp, isr, sema, mbox);
4493126ec864SMatt Jacob 	}
4494478f8a96SJustin T. Gibbs }
4495478f8a96SJustin T. Gibbs 
4496ab6c4b31SMatt Jacob 
44972df76c16SMatt Jacob static void
44982df76c16SMatt Jacob isp_watchdog(void *arg)
4499cc8df88bSMatt Jacob {
45002df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
45012df76c16SMatt Jacob 	ispsoftc_t *isp;
4502e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
4503b85389e1SMatt Jacob 
45042df76c16SMatt Jacob 	isp = XS_ISP(xs);
45052df76c16SMatt Jacob 
4506cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
4507e95725cbSMatt Jacob 
4508e95725cbSMatt Jacob 	/*
4509e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
4510e95725cbSMatt Jacob 	 */
4511e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
4512e95725cbSMatt Jacob 		uint32_t isr;
4513e95725cbSMatt Jacob 		uint16_t sema, mbox;
4514e95725cbSMatt Jacob 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
4515e95725cbSMatt Jacob 			isp_intr(isp, isr, sema, mbox);
4516e95725cbSMatt Jacob 		}
4517e95725cbSMatt Jacob 		ohandle = handle;
4518e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
4519e95725cbSMatt Jacob 	}
4520c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
45211fcf5debSMatt Jacob 		/*
4522c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
4523c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
4524c8b8a2c4SMatt Jacob 		 *
4525c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
4526c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
4527c8b8a2c4SMatt Jacob 		 * back separately.
45281fcf5debSMatt Jacob 		 */
4529c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
4530c8b8a2c4SMatt Jacob 			return;
4531c8b8a2c4SMatt Jacob 		}
45321fcf5debSMatt Jacob 
45331fcf5debSMatt Jacob 		/*
4534c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
4535c8b8a2c4SMatt Jacob 		 * fact have been completed.
4536c8b8a2c4SMatt Jacob 		 */
4537c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
4538c8b8a2c4SMatt Jacob 
4539c8b8a2c4SMatt Jacob 		/*
4540c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
4541c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
4542c8b8a2c4SMatt Jacob 		 */
4543c8b8a2c4SMatt Jacob 		if (xs == NULL) {
4544c8b8a2c4SMatt Jacob 			return;
4545c8b8a2c4SMatt Jacob 		}
4546c8b8a2c4SMatt Jacob 
4547c8b8a2c4SMatt Jacob 		/*
4548c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
45491fcf5debSMatt Jacob 		 */
4550f6e75de2SMatt Jacob 		if (XS_XFRLEN(xs)) {
4551f6e75de2SMatt Jacob 			ISP_DMAFREE(isp, xs, handle);
4552f6e75de2SMatt Jacob 		}
4553cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
4554c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
4555387d8239SMatt Jacob 		xs->ccb_h.status &= ~CAM_STATUS_MASK;
4556387d8239SMatt Jacob 		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
4557e95725cbSMatt Jacob 		isp_prt_endcmd(isp, xs);
4558cc8df88bSMatt Jacob 		isp_done(xs);
4559e95725cbSMatt Jacob 	} else {
4560e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
4561e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
4562e95725cbSMatt Jacob 		} else {
4563e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
4564e95725cbSMatt Jacob 		}
4565b85389e1SMatt Jacob 	}
4566f7c631bcSMatt Jacob }
4567f7c631bcSMatt Jacob 
4568f7c631bcSMatt Jacob static void
45695704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4570f7c631bcSMatt Jacob {
4571ffcf6651SMatt Jacob 	union ccb *ccb;
45722df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
45732df76c16SMatt Jacob 
45742df76c16SMatt Jacob 	if (isp_autoconfig == 0) {
45752df76c16SMatt Jacob 		return;
45762df76c16SMatt Jacob 	}
45772df76c16SMatt Jacob 
4578ffcf6651SMatt Jacob 	/*
45790e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
4580ffcf6651SMatt Jacob 	 */
45818008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
4582ffcf6651SMatt Jacob 	if (ccb == NULL) {
45832df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
4584ffcf6651SMatt Jacob 		return;
4585ffcf6651SMatt Jacob 	}
4586e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
4587e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4588ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
4589ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
4590ffcf6651SMatt Jacob 		return;
4591ffcf6651SMatt Jacob 	}
45925704e6f0SKenneth D. Merry 
45935704e6f0SKenneth D. Merry 	/*
45945704e6f0SKenneth D. Merry 	 * Since we're about to issue a rescan, mark this device as not
45955704e6f0SKenneth D. Merry 	 * reported gone.
45965704e6f0SKenneth D. Merry 	 */
45975704e6f0SKenneth D. Merry 	fcp->reported_gone = 0;
45985704e6f0SKenneth D. Merry 
4599ffcf6651SMatt Jacob 	xpt_rescan(ccb);
4600ffcf6651SMatt Jacob }
4601ffcf6651SMatt Jacob 
4602ffcf6651SMatt Jacob static void
46035704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4604ffcf6651SMatt Jacob {
4605ffcf6651SMatt Jacob 	struct cam_path *tp;
46062df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
46072df76c16SMatt Jacob 
46082df76c16SMatt Jacob 	if (isp_autoconfig == 0) {
46092df76c16SMatt Jacob 		return;
46102df76c16SMatt Jacob 	}
46112df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
46125704e6f0SKenneth D. Merry 		/*
46135704e6f0SKenneth D. Merry 		 * We're about to send out the lost device async
46145704e6f0SKenneth D. Merry 		 * notification, so indicate that we have reported it gone.
46155704e6f0SKenneth D. Merry 		 */
46165704e6f0SKenneth D. Merry 		fcp->reported_gone = 1;
4617ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
4618ffcf6651SMatt Jacob 		xpt_free_path(tp);
4619f7c631bcSMatt Jacob 	}
4620f7c631bcSMatt Jacob }
4621f7c631bcSMatt Jacob 
4622f7c631bcSMatt Jacob /*
4623f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
4624f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
4625f7c631bcSMatt Jacob  * gone away.
4626f7c631bcSMatt Jacob  *
4627f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
4628f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
4629f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
4630f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
4631f7c631bcSMatt Jacob  */
4632f7c631bcSMatt Jacob static void
4633f7c631bcSMatt Jacob isp_gdt(void *arg)
4634f7c631bcSMatt Jacob {
46352df76c16SMatt Jacob 	struct isp_fc *fc = arg;
4636de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4637de461933SMatt Jacob }
4638de461933SMatt Jacob 
4639de461933SMatt Jacob static void
4640de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
4641de461933SMatt Jacob {
4642de461933SMatt Jacob 	struct isp_fc *fc = arg;
46432df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
46442df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
4645f7c631bcSMatt Jacob 	fcportdb_t *lp;
4646f7c631bcSMatt Jacob 	int dbidx, tgt, more_to_do = 0;
4647f7c631bcSMatt Jacob 
4648de461933SMatt Jacob 	ISP_LOCK(isp);
4649de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4650f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
46512df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
4652f7c631bcSMatt Jacob 
4653f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4654f7c631bcSMatt Jacob 			continue;
4655f7c631bcSMatt Jacob 		}
46562df76c16SMatt Jacob 		if (lp->dev_map_idx == 0 || lp->target_mode) {
4657f7c631bcSMatt Jacob 			continue;
4658f7c631bcSMatt Jacob 		}
4659427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
4660387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
4661427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
4662f7c631bcSMatt Jacob 			more_to_do++;
4663f7c631bcSMatt Jacob 			continue;
4664f7c631bcSMatt Jacob 		}
46652df76c16SMatt Jacob 		tgt = lp->dev_map_idx - 1;
46662df76c16SMatt Jacob 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
46672df76c16SMatt Jacob 		lp->dev_map_idx = 0;
4668f7c631bcSMatt Jacob 		lp->state = FC_PORTDB_STATE_NIL;
46692df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
46705704e6f0SKenneth D. Merry 		isp_make_gone(isp, lp, chan, tgt);
4671f7c631bcSMatt Jacob 	}
4672a01f5aebSMatt Jacob 	if (fc->ready) {
4673f7c631bcSMatt Jacob 		if (more_to_do) {
46742df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
4675f7c631bcSMatt Jacob 		} else {
4676de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
4677387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4678a01f5aebSMatt Jacob 		}
4679f7c631bcSMatt Jacob 	}
4680de461933SMatt Jacob 	ISP_UNLOCK(isp);
4681f7c631bcSMatt Jacob }
4682f7c631bcSMatt Jacob 
4683f7c631bcSMatt Jacob /*
4684f7c631bcSMatt Jacob  * Loop Down Timer Function- when loop goes down, a timer is started and
4685f7c631bcSMatt Jacob  * and after it expires we come here and take all probational devices that
4686f7c631bcSMatt Jacob  * the OS knows about and the tell the OS that they've gone away.
4687f7c631bcSMatt Jacob  *
4688f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
4689f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
4690f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
4691f7c631bcSMatt Jacob  */
4692f7c631bcSMatt Jacob static void
4693f7c631bcSMatt Jacob isp_ldt(void *arg)
4694f7c631bcSMatt Jacob {
46952df76c16SMatt Jacob 	struct isp_fc *fc = arg;
4696de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4697de461933SMatt Jacob }
4698de461933SMatt Jacob 
4699de461933SMatt Jacob static void
4700de461933SMatt Jacob isp_ldt_task(void *arg, int pending)
4701de461933SMatt Jacob {
4702de461933SMatt Jacob 	struct isp_fc *fc = arg;
47032df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
47042df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
4705f7c631bcSMatt Jacob 	fcportdb_t *lp;
4706e95725cbSMatt Jacob 	int dbidx, tgt, i;
4707f7c631bcSMatt Jacob 
4708de461933SMatt Jacob 	ISP_LOCK(isp);
4709387d8239SMatt Jacob 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4710de461933SMatt Jacob 	callout_deactivate(&fc->ldt);
47110a70657fSMatt Jacob 
4712f7c631bcSMatt Jacob 	/*
4713f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
4714f7c631bcSMatt Jacob 	 */
4715f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
47162df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
4717f7c631bcSMatt Jacob 
4718f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4719f7c631bcSMatt Jacob 			continue;
4720f7c631bcSMatt Jacob 		}
47212df76c16SMatt Jacob 		if (lp->dev_map_idx == 0 || lp->target_mode) {
4722f7c631bcSMatt Jacob 			continue;
4723f7c631bcSMatt Jacob 		}
4724f7c631bcSMatt Jacob 
4725f7c631bcSMatt Jacob 		/*
4726f7c631bcSMatt Jacob 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4727f7c631bcSMatt Jacob 		 */
4728f7c631bcSMatt Jacob 
4729e95725cbSMatt Jacob 
4730e95725cbSMatt Jacob 		for (i = 0; i < isp->isp_maxcmds; i++) {
4731e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
4732e95725cbSMatt Jacob 
4733e95725cbSMatt Jacob 			if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4734e95725cbSMatt Jacob 				continue;
4735e95725cbSMatt Jacob 			}
4736e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4737e95725cbSMatt Jacob 				continue;
4738e95725cbSMatt Jacob                         }
4739e95725cbSMatt Jacob 			if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4740e95725cbSMatt Jacob 				continue;
4741e95725cbSMatt Jacob 			}
474294dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%d orphaned by loop down timeout",
4743e95725cbSMatt Jacob 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4744e95725cbSMatt Jacob 		}
4745e95725cbSMatt Jacob 
4746f7c631bcSMatt Jacob 		/*
4747f7c631bcSMatt Jacob 		 * Mark that we've announced that this device is gone....
4748f7c631bcSMatt Jacob 		 */
4749387d8239SMatt Jacob 		lp->announced = 1;
4750f7c631bcSMatt Jacob 
4751f7c631bcSMatt Jacob 		/*
4752f7c631bcSMatt Jacob 		 * but *don't* change the state of the entry. Just clear
4753f7c631bcSMatt Jacob 		 * any target id stuff and announce to CAM that the
4754f7c631bcSMatt Jacob 		 * device is gone. This way any necessary PLOGO stuff
4755f7c631bcSMatt Jacob 		 * will happen when loop comes back up.
4756f7c631bcSMatt Jacob 		 */
4757f7c631bcSMatt Jacob 
47582df76c16SMatt Jacob 		tgt = lp->dev_map_idx - 1;
47592df76c16SMatt Jacob 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
47602df76c16SMatt Jacob 		lp->dev_map_idx = 0;
47612df76c16SMatt Jacob 		lp->state = FC_PORTDB_STATE_NIL;
47622df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
47635704e6f0SKenneth D. Merry 		isp_make_gone(isp, lp, chan, tgt);
4764f7c631bcSMatt Jacob 	}
4765f7c631bcSMatt Jacob 
4766e95725cbSMatt Jacob 	if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4767e95725cbSMatt Jacob 		isp_unfreeze_loopdown(isp, chan);
4768e95725cbSMatt Jacob 	}
4769f7c631bcSMatt Jacob 	/*
4770f7c631bcSMatt Jacob 	 * The loop down timer has expired. Wake up the kthread
4771f7c631bcSMatt Jacob 	 * to notice that fact (or make it false).
4772f7c631bcSMatt Jacob 	 */
47732df76c16SMatt Jacob 	fc->loop_dead = 1;
47742df76c16SMatt Jacob 	fc->loop_down_time = fc->loop_down_limit+1;
47752df76c16SMatt Jacob 	wakeup(fc);
4776e95725cbSMatt Jacob 	ISP_UNLOCK(isp);
4777cc8df88bSMatt Jacob }
4778cc8df88bSMatt Jacob 
47795d571944SMatt Jacob static void
47805d571944SMatt Jacob isp_kthread(void *arg)
47815d571944SMatt Jacob {
47822df76c16SMatt Jacob 	struct isp_fc *fc = arg;
47832df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
47842df76c16SMatt Jacob 	int chan = fc - isp->isp_osinfo.pc.fc;
4785f7c631bcSMatt Jacob 	int slp = 0;
4786a01f5aebSMatt Jacob 
47870a70657fSMatt Jacob 	mtx_lock(&isp->isp_osinfo.lock);
47882df76c16SMatt Jacob 
47891d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
4790e95725cbSMatt Jacob 		int lb, lim;
4791fdeb9f2fSMatt Jacob 
4792387d8239SMatt Jacob 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
47932df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
47942df76c16SMatt Jacob 
47952df76c16SMatt Jacob 		/*
47962df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
47972df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
47982df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
47992df76c16SMatt Jacob 		 * check again.
48002df76c16SMatt Jacob 		 *
48012df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
48022df76c16SMatt Jacob 		 */
48034ecb1d4aSMatt Jacob 		if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4804f7c631bcSMatt Jacob 			/*
4805f7c631bcSMatt Jacob 			 * Increment loop down time by the last sleep interval
4806f7c631bcSMatt Jacob 			 */
48072df76c16SMatt Jacob 			fc->loop_down_time += slp;
480810365e5aSMatt Jacob 
480910365e5aSMatt Jacob 			if (lb < 0) {
4810387d8239SMatt Jacob 				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
481110365e5aSMatt Jacob 			} else {
4812387d8239SMatt Jacob 				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4813f44257c2SMatt Jacob 			}
481410365e5aSMatt Jacob 
481510365e5aSMatt Jacob 			/*
481610365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
4817f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
4818f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
4819f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
482010365e5aSMatt Jacob 			 */
48212df76c16SMatt Jacob 			if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4822f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
482310365e5aSMatt Jacob 			} else {
48242df76c16SMatt Jacob 				lim = fc->loop_down_limit;
4825f7c631bcSMatt Jacob 			}
48262df76c16SMatt Jacob 			if (fc->loop_down_time >= lim) {
48272df76c16SMatt Jacob 				isp_freeze_loopdown(isp, chan, "loop limit hit");
4828f7c631bcSMatt Jacob 				slp = 0;
48292df76c16SMatt Jacob 			} else if (fc->loop_down_time < 10) {
483010365e5aSMatt Jacob 				slp = 1;
48312df76c16SMatt Jacob 			} else if (fc->loop_down_time < 30) {
4832f7c631bcSMatt Jacob 				slp = 5;
48332df76c16SMatt Jacob 			} else if (fc->loop_down_time < 60) {
4834f7c631bcSMatt Jacob 				slp = 10;
48352df76c16SMatt Jacob 			} else if (fc->loop_down_time < 120) {
4836f7c631bcSMatt Jacob 				slp = 20;
4837f7c631bcSMatt Jacob 			} else {
4838f7c631bcSMatt Jacob 				slp = 30;
4839f44257c2SMatt Jacob 			}
484010365e5aSMatt Jacob 
48412df76c16SMatt Jacob 		} else if (lb) {
4842387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
48432df76c16SMatt Jacob 			fc->loop_down_time += slp;
4844387d8239SMatt Jacob 			if (fc->loop_down_time > 300)
4845387d8239SMatt Jacob 				slp = 0;
4846387d8239SMatt Jacob 			else
48472df76c16SMatt Jacob 				slp = 60;
484810365e5aSMatt Jacob 		} else {
4849387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
48502df76c16SMatt Jacob 			fc->loop_down_time = 0;
485110365e5aSMatt Jacob 			slp = 0;
48525d571944SMatt Jacob 		}
4853fdeb9f2fSMatt Jacob 
48542df76c16SMatt Jacob 
4855f44257c2SMatt Jacob 		/*
48562df76c16SMatt Jacob 		 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
48572df76c16SMatt Jacob 		 * now so that CAM can start sending us commands.
48582df76c16SMatt Jacob 		 *
48592df76c16SMatt Jacob 		 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
48602df76c16SMatt Jacob 		 * or kill the commands, as appropriate.
4861f44257c2SMatt Jacob 		 */
48622df76c16SMatt Jacob 
48632df76c16SMatt Jacob 		if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4864e95725cbSMatt Jacob 			isp_unfreeze_loopdown(isp, chan);
48652df76c16SMatt Jacob 		}
48662df76c16SMatt Jacob 
4867387d8239SMatt Jacob 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
48682df76c16SMatt Jacob 
48692df76c16SMatt Jacob 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
48702df76c16SMatt Jacob 
487110365e5aSMatt Jacob 		/*
487210365e5aSMatt Jacob 		 * If slp is zero, we're waking up for the first time after
487310365e5aSMatt Jacob 		 * things have been okay. In this case, we set a deferral state
487410365e5aSMatt Jacob 		 * for all commands and delay hysteresis seconds before starting
487510365e5aSMatt Jacob 		 * the FC state evaluation. This gives the loop/fabric a chance
487610365e5aSMatt Jacob 		 * to settle.
487710365e5aSMatt Jacob 		 */
48782df76c16SMatt Jacob 		if (slp == 0 && fc->hysteresis) {
4879387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
48804962e51bSMatt Jacob 			mtx_unlock(&isp->isp_osinfo.lock);
48814962e51bSMatt Jacob 			pause("ispt", fc->hysteresis * hz);
48824962e51bSMatt Jacob 			mtx_lock(&isp->isp_osinfo.lock);
488310365e5aSMatt Jacob 		}
48845d571944SMatt Jacob 	}
48851d0a1de2SWill Andrews 	fc->num_threads -= 1;
48860a70657fSMatt Jacob 	mtx_unlock(&isp->isp_osinfo.lock);
48871d0a1de2SWill Andrews 	kthread_exit();
48885d571944SMatt Jacob }
48895d571944SMatt Jacob 
4890cc8df88bSMatt Jacob static void
4891c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
4892478f8a96SJustin T. Gibbs {
48930a70657fSMatt Jacob 	int bus, tgt, ts, error, lim;
48949cd7268eSMatt Jacob 	ispsoftc_t *isp;
48954663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
4896478f8a96SJustin T. Gibbs 
4897478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4898478f8a96SJustin T. Gibbs 
48999cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
49002df76c16SMatt Jacob 	mtx_assert(&isp->isp_lock, MA_OWNED);
49012df76c16SMatt Jacob 
49022df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
490357c801f5SMatt Jacob 		isp_init(isp);
490457c801f5SMatt Jacob 		if (isp->isp_state != ISP_INITSTATE) {
490557c801f5SMatt Jacob 			/*
490657c801f5SMatt Jacob 			 * Lie. Say it was a selection timeout.
490757c801f5SMatt Jacob 			 */
4908b85389e1SMatt Jacob 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
49090470d791SMatt Jacob 			xpt_freeze_devq(ccb->ccb_h.path, 1);
491057c801f5SMatt Jacob 			xpt_done(ccb);
491157c801f5SMatt Jacob 			return;
491257c801f5SMatt Jacob 		}
491357c801f5SMatt Jacob 		isp->isp_state = ISP_RUNSTATE;
491457c801f5SMatt Jacob 	}
4915b09b0095SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
49160a70657fSMatt Jacob 	ISP_PCMD(ccb) = NULL;
49175d571944SMatt Jacob 
4918478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
4919478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
49202df76c16SMatt Jacob 		bus = XS_CHANNEL(ccb);
4921478f8a96SJustin T. Gibbs 		/*
4922478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
4923478f8a96SJustin T. Gibbs 		 */
4924478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4925478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4926478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
4927478f8a96SJustin T. Gibbs 				xpt_done(ccb);
4928478f8a96SJustin T. Gibbs 				break;
4929478f8a96SJustin T. Gibbs 			}
4930478f8a96SJustin T. Gibbs 		}
4931387d8239SMatt Jacob 		ccb->csio.req_map = NULL;
49320470d791SMatt Jacob #ifdef	DIAGNOSTIC
49330470d791SMatt Jacob 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4934dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
4935478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
49360470d791SMatt Jacob 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4937dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
4938478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
4939478f8a96SJustin T. Gibbs 		}
4940478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4941478f8a96SJustin T. Gibbs 			xpt_done(ccb);
4942478f8a96SJustin T. Gibbs 			break;
4943478f8a96SJustin T. Gibbs 		}
49440470d791SMatt Jacob #endif
49450a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
49460a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
49470a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
49480a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
49492df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
49500a70657fSMatt Jacob 			xpt_done(ccb);
49510a70657fSMatt Jacob 			break;
49520a70657fSMatt Jacob 		}
4953b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
49540470d791SMatt Jacob 		switch (error) {
4955478f8a96SJustin T. Gibbs 		case CMD_QUEUED:
4956478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
49570a70657fSMatt Jacob 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
49580a70657fSMatt Jacob 				break;
4959d69a5f7dSMatt Jacob 			}
49600a70657fSMatt Jacob 			ts = ccb->ccb_h.timeout;
49610a70657fSMatt Jacob 			if (ts == CAM_TIME_DEFAULT) {
49620a70657fSMatt Jacob 				ts = 60*1000;
4963cc8df88bSMatt Jacob 			}
49640a70657fSMatt Jacob 			ts = isp_mstohz(ts);
49652df76c16SMatt Jacob 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4966478f8a96SJustin T. Gibbs 			break;
49670470d791SMatt Jacob 		case CMD_RQLATER:
4968f44257c2SMatt Jacob 			/*
49692df76c16SMatt Jacob 			 * We get this result for FC devices if the loop state isn't ready yet
49702df76c16SMatt Jacob 			 * or if the device in question has gone zombie on us.
49712df76c16SMatt Jacob 			 *
49722df76c16SMatt Jacob 			 * If we've never seen Loop UP at all, we requeue this request and wait
49732df76c16SMatt Jacob 			 * for the initial loop up delay to expire.
497410365e5aSMatt Jacob 			 */
49752df76c16SMatt Jacob 			lim = ISP_FC_PC(isp, bus)->loop_down_limit;
49762df76c16SMatt Jacob 			if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
49772df76c16SMatt Jacob 				if (FCPARAM(isp, bus)->loop_seen_once == 0) {
49782df76c16SMatt Jacob 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4979f7c631bcSMatt Jacob 				} else {
49802df76c16SMatt 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);
4981f7c631bcSMatt Jacob 				}
49822df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4983f7c631bcSMatt Jacob 				xpt_freeze_devq(ccb->ccb_h.path, 1);
49840a70657fSMatt Jacob 				isp_free_pcmd(isp, ccb);
4985f44257c2SMatt Jacob 				xpt_done(ccb);
4986f44257c2SMatt Jacob 				break;
4987f44257c2SMatt Jacob 			}
49882df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4989f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
49902df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4991387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
49920a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
4993478f8a96SJustin T. Gibbs 			xpt_done(ccb);
4994478f8a96SJustin T. Gibbs 			break;
49950470d791SMatt Jacob 		case CMD_EAGAIN:
49960a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
49972df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
49982df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4999387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
5000478f8a96SJustin T. Gibbs 			xpt_done(ccb);
5001478f8a96SJustin T. Gibbs 			break;
50020470d791SMatt Jacob 		case CMD_COMPLETE:
50030470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
50040470d791SMatt Jacob 			break;
50050470d791SMatt Jacob 		default:
50062df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
5007387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
50080a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
50090470d791SMatt Jacob 			xpt_done(ccb);
5010478f8a96SJustin T. Gibbs 		}
5011478f8a96SJustin T. Gibbs 		break;
5012478f8a96SJustin T. Gibbs 
5013d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
50142df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
50152df76c16SMatt Jacob 		if (ccb->cel.enable) {
50162df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
50172df76c16SMatt Jacob 		} else {
50182df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
501967ff51f1SMatt Jacob 		}
502067ff51f1SMatt Jacob 		break;
5021ae5db118SMatt Jacob 	case XPT_IMMED_NOTIFY:
50222df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
5023d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
5024d81ba9d5SMatt Jacob 	{
50252df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
5026d81ba9d5SMatt Jacob 		if (tptr == NULL) {
50272df76c16SMatt Jacob 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
50282df76c16SMatt Jacob 		}
50292df76c16SMatt Jacob 		if (tptr == NULL) {
50302df76c16SMatt Jacob 			const char *str;
50312df76c16SMatt Jacob 			uint32_t tag;
50322df76c16SMatt Jacob 
50332df76c16SMatt Jacob 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
50342df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
50352df76c16SMatt Jacob 				tag = ccb->cin1.seq_id;
50362df76c16SMatt Jacob 			} else {
50372df76c16SMatt Jacob 				tag = ccb->atio.tag_id;
50382df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
50392df76c16SMatt Jacob 			}
50402df76c16SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
50412df76c16SMatt Jacob 			dump_tstates(isp, XS_CHANNEL(ccb));
50422df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5043d81ba9d5SMatt Jacob 			break;
5044d81ba9d5SMatt Jacob 		}
5045387d8239SMatt Jacob 		ccb->ccb_h.spriv_field0 = 0;
5046387d8239SMatt Jacob 		ccb->ccb_h.spriv_ptr1 = isp;
5047570c7a3fSMatt Jacob 
5048d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
50492df76c16SMatt Jacob 			if (ccb->atio.tag_id) {
5050523ea374SAlexander Motin 				atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
50512df76c16SMatt Jacob 				if (atp) {
50522df76c16SMatt Jacob 					isp_put_atpd(isp, tptr, atp);
50532df76c16SMatt Jacob 				}
50542df76c16SMatt Jacob 			}
5055570c7a3fSMatt Jacob 			tptr->atio_count++;
50562df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
5057387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
5058e2873b76SMatt Jacob 			    ccb->atio.tag_id, tptr->atio_count);
505994dff771SMatt Jacob 			ccb->atio.tag_id = 0;
50602df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
50612df76c16SMatt Jacob 			if (ccb->cin1.tag_id) {
50622df76c16SMatt Jacob 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
50632df76c16SMatt Jacob 				if (ntp) {
50642df76c16SMatt Jacob 					isp_put_ntpd(isp, tptr, ntp);
50652df76c16SMatt Jacob 				}
50662df76c16SMatt Jacob 			}
5067746e9c85SMatt Jacob 			tptr->inot_count++;
50682df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5069387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5070e2873b76SMatt Jacob 			    ccb->cin1.seq_id, tptr->inot_count);
507194dff771SMatt Jacob 			ccb->cin1.seq_id = 0;
5072ae5db118SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
5073ae5db118SMatt Jacob 			tptr->inot_count++;
5074ae5db118SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5075387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5076e2873b76SMatt Jacob 			    ccb->cin1.seq_id, tptr->inot_count);
507794dff771SMatt Jacob 			ccb->cin1.seq_id = 0;
5078d81ba9d5SMatt Jacob 		}
5079d81ba9d5SMatt Jacob 		rls_lun_statep(isp, tptr);
5080d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
5081d81ba9d5SMatt Jacob 		break;
5082d81ba9d5SMatt Jacob 	}
5083ae5db118SMatt Jacob 	case XPT_NOTIFY_ACK:
5084ae5db118SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5085ae5db118SMatt Jacob 		break;
50862df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
5087d81ba9d5SMatt Jacob 	{
50882df76c16SMatt Jacob 		tstate_t *tptr;
50892df76c16SMatt Jacob 		inot_private_data_t *ntp;
50902df76c16SMatt Jacob 
50912df76c16SMatt Jacob 		/*
50922df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
50932df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
50942df76c16SMatt Jacob 		 */
50952df76c16SMatt Jacob 		/*
50962df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
50972df76c16SMatt Jacob 		 */
50982df76c16SMatt 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);
50992df76c16SMatt Jacob 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
51002df76c16SMatt Jacob 		if (ntp == NULL) {
51012df76c16SMatt 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__,
51022df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
51032df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
51042df76c16SMatt Jacob 			xpt_done(ccb);
5105d81ba9d5SMatt Jacob 			break;
5106d81ba9d5SMatt Jacob 		}
51072df76c16SMatt Jacob 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
51082df76c16SMatt Jacob 			rls_lun_statep(isp, tptr);
51092df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
51102df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
5111387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
5112387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
51132df76c16SMatt Jacob 			break;
51142df76c16SMatt Jacob 		}
51152df76c16SMatt Jacob 		isp_put_ntpd(isp, tptr, ntp);
51162df76c16SMatt Jacob 		rls_lun_statep(isp, tptr);
51172df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
51182df76c16SMatt 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);
51192df76c16SMatt Jacob 		xpt_done(ccb);
51202df76c16SMatt Jacob 		break;
51212df76c16SMatt Jacob 	}
51222df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
5123387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
51242df76c16SMatt Jacob 		break;
5125d81ba9d5SMatt Jacob #endif
5126478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
5127b44e442eSWill Andrews 	{
5128b44e442eSWill Andrews 		struct isp_fc *fc;
5129d81ba9d5SMatt Jacob 
5130d81ba9d5SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
5131d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
5132d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
5133b44e442eSWill Andrews 		if (IS_FC(isp))
5134b44e442eSWill Andrews 			fc = ISP_FC_PC(isp, bus);
5135b44e442eSWill Andrews 		else
5136b44e442eSWill Andrews 			fc = NULL;
5137d81ba9d5SMatt Jacob 
51382df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
5139478f8a96SJustin T. Gibbs 		if (error) {
5140478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5141478f8a96SJustin T. Gibbs 		} else {
5142b44e442eSWill Andrews 			/*
5143b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
5144b44e442eSWill Andrews 			 * Reference Number, because the target will expect
5145b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
5146b44e442eSWill Andrews 			 */
5147b44e442eSWill Andrews 			if (fc != NULL)
5148b44e442eSWill Andrews 				isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5149b44e442eSWill Andrews 
5150478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
5151478f8a96SJustin T. Gibbs 		}
5152478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5153478f8a96SJustin T. Gibbs 		break;
5154b44e442eSWill Andrews 	}
5155478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
5156d81ba9d5SMatt Jacob 	{
5157d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
5158d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
5159d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
5160d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
516145210a25SAlexander Motin 			isp_target_mark_aborted(isp, ccb);
5162d81ba9d5SMatt Jacob 			break;
5163d81ba9d5SMatt Jacob #endif
5164d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
5165950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
5166478f8a96SJustin T. Gibbs 			if (error) {
5167d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
5168478f8a96SJustin T. Gibbs 			} else {
5169478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
5170478f8a96SJustin T. Gibbs 			}
5171d81ba9d5SMatt Jacob 			break;
5172d81ba9d5SMatt Jacob 		default:
5173d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
5174d81ba9d5SMatt Jacob 			break;
5175d81ba9d5SMatt Jacob 		}
51761c0a1eb2SMatt Jacob 		/*
51771c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
51781c0a1eb2SMatt Jacob 		 * complete when control is returned.
51791c0a1eb2SMatt Jacob 		 */
5180478f8a96SJustin T. Gibbs 		break;
5181d81ba9d5SMatt Jacob 	}
5182ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
5183478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
5184478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
51859ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
51869ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
51879ce9bdafSMatt Jacob 			xpt_done(ccb);
51889ce9bdafSMatt Jacob 			break;
51899ce9bdafSMatt Jacob 		}
5190478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
5191805e1f82SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
51922df76c16SMatt Jacob 		if (IS_SCSI(isp)) {
51932df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
51942df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
51952df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
51961dae40ebSMatt Jacob 			uint16_t *dptr;
5197ab163f5fSMatt Jacob 
5198b61386a4SMatt Jacob 			if (spi->valid == 0 && scsi->valid == 0) {
5199b61386a4SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_CMP;
5200b61386a4SMatt Jacob 				xpt_done(ccb);
5201b61386a4SMatt Jacob 				break;
5202b61386a4SMatt Jacob 			}
5203b61386a4SMatt Jacob 
5204ab163f5fSMatt Jacob 			/*
52059ce9bdafSMatt Jacob 			 * We always update (internally) from goal_flags
5206ab163f5fSMatt Jacob 			 * so any request to change settings just gets
5207ab163f5fSMatt Jacob 			 * vectored to that location.
5208ab163f5fSMatt Jacob 			 */
52099ce9bdafSMatt Jacob 			dptr = &sdp->isp_devparam[tgt].goal_flags;
5210ab163f5fSMatt Jacob 
5211ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
5212ab163f5fSMatt Jacob 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
5213ab163f5fSMatt Jacob 					*dptr |= DPARM_DISC;
5214ab163f5fSMatt Jacob 				else
5215ab163f5fSMatt Jacob 					*dptr &= ~DPARM_DISC;
5216ab163f5fSMatt Jacob 			}
5217ab163f5fSMatt Jacob 
5218ab163f5fSMatt Jacob 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
5219ab163f5fSMatt Jacob 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
5220ab163f5fSMatt Jacob 					*dptr |= DPARM_TQING;
5221ab163f5fSMatt Jacob 				else
5222ab163f5fSMatt Jacob 					*dptr &= ~DPARM_TQING;
5223ab163f5fSMatt Jacob 			}
5224ab163f5fSMatt Jacob 
5225ab163f5fSMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
5226ab163f5fSMatt Jacob 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
5227ab163f5fSMatt Jacob 					*dptr |= DPARM_WIDE;
5228ab163f5fSMatt Jacob 				else
5229ab163f5fSMatt Jacob 					*dptr &= ~DPARM_WIDE;
5230ab163f5fSMatt Jacob 			}
5231ab163f5fSMatt Jacob 
5232ab163f5fSMatt Jacob 			/*
5233ab163f5fSMatt Jacob 			 * XXX: FIX ME
5234ab163f5fSMatt Jacob 			 */
52352df76c16SMatt Jacob 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
5236ab163f5fSMatt Jacob 				*dptr |= DPARM_SYNC;
52379ce9bdafSMatt Jacob 				/*
52389ce9bdafSMatt Jacob 				 * XXX: CHECK FOR LEGALITY
52399ce9bdafSMatt Jacob 				 */
52402df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
52412df76c16SMatt Jacob 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
5242ab163f5fSMatt Jacob 			} else {
5243ab163f5fSMatt Jacob 				*dptr &= ~DPARM_SYNC;
5244ab163f5fSMatt Jacob 			}
5245123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%jx) to flags %x off %x per %x", bus, tgt, (uintmax_t)cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
52462df76c16SMatt Jacob 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
5247478f8a96SJustin T. Gibbs 			sdp->isp_devparam[tgt].dev_update = 1;
52482df76c16SMatt Jacob 			sdp->update = 1;
5249478f8a96SJustin T. Gibbs 		}
5250478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
5251478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5252478f8a96SJustin T. Gibbs 		break;
5253478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
5254478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
5255478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
52562df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
5257ab6c4b31SMatt Jacob 		if (IS_FC(isp)) {
52582df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
52592df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
52602df76c16SMatt Jacob 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
526195f7dfb2SMatt Jacob 			unsigned int hdlidx;
5262478f8a96SJustin T. Gibbs 
5263ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
5264ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
5265ab163f5fSMatt Jacob 			cts->transport = XPORT_FC;
5266ab163f5fSMatt Jacob 			cts->transport_version = 0;
5267ab163f5fSMatt Jacob 
52689b03492aSMatt Jacob 			scsi->valid = CTS_SCSI_VALID_TQ;
52699b03492aSMatt Jacob 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
5270ab163f5fSMatt Jacob 			fc->valid = CTS_FC_VALID_SPEED;
5271ab163f5fSMatt Jacob 			fc->bitrate = 100000;
5272fada2376SJung-uk Kim 			fc->bitrate *= fcp->isp_gbspeed;
527395f7dfb2SMatt Jacob 			hdlidx = fcp->isp_dev_map[tgt] - 1;
527495f7dfb2SMatt Jacob 			if (hdlidx < MAX_FC_TARG) {
527595f7dfb2SMatt Jacob 				fcportdb_t *lp = &fcp->portdb[hdlidx];
5276ab163f5fSMatt Jacob 				fc->wwnn = lp->node_wwn;
5277ab163f5fSMatt Jacob 				fc->wwpn = lp->port_wwn;
5278ab163f5fSMatt Jacob 				fc->port = lp->portid;
52792df76c16SMatt Jacob 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
5280ab163f5fSMatt Jacob 			}
5281ab163f5fSMatt Jacob 		} else {
52822df76c16SMatt Jacob 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
52832df76c16SMatt Jacob 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
52842df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
52851dae40ebSMatt Jacob 			uint16_t dval, pval, oval;
5286ab163f5fSMatt Jacob 
5287ab163f5fSMatt Jacob 			if (IS_CURRENT_SETTINGS(cts)) {
528883ae4407SMatt Jacob 				sdp->isp_devparam[tgt].dev_refresh = 1;
52892df76c16SMatt Jacob 				sdp->update = 1;
52902df76c16SMatt Jacob 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
52919ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].actv_flags;
52929ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].actv_offset;
52939ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].actv_period;
52944394c92fSMatt Jacob 			} else {
52959ce9bdafSMatt Jacob 				dval = sdp->isp_devparam[tgt].nvrm_flags;
52969ce9bdafSMatt Jacob 				oval = sdp->isp_devparam[tgt].nvrm_offset;
52979ce9bdafSMatt Jacob 				pval = sdp->isp_devparam[tgt].nvrm_period;
52984394c92fSMatt Jacob 			}
5299478f8a96SJustin T. Gibbs 
5300ab163f5fSMatt Jacob 			cts->protocol = PROTO_SCSI;
5301ab163f5fSMatt Jacob 			cts->protocol_version = SCSI_REV_2;
5302ab163f5fSMatt Jacob 			cts->transport = XPORT_SPI;
5303ab163f5fSMatt Jacob 			cts->transport_version = 2;
5304ab163f5fSMatt Jacob 
5305b61386a4SMatt Jacob 			spi->valid = 0;
5306b61386a4SMatt Jacob 			scsi->valid = 0;
5307b61386a4SMatt Jacob 			spi->flags = 0;
5308b61386a4SMatt Jacob 			scsi->flags = 0;
5309ab163f5fSMatt Jacob 			if (dval & DPARM_DISC) {
5310ab163f5fSMatt Jacob 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5311ab163f5fSMatt Jacob 			}
53129ce9bdafSMatt Jacob 			if ((dval & DPARM_SYNC) && oval && pval) {
5313ab163f5fSMatt Jacob 				spi->sync_offset = oval;
5314ab163f5fSMatt Jacob 				spi->sync_period = pval;
5315b61386a4SMatt Jacob 			} else {
5316b61386a4SMatt Jacob 				spi->sync_offset = 0;
5317b61386a4SMatt Jacob 				spi->sync_period = 0;
5318b61386a4SMatt Jacob 			}
5319ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5320ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5321ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
5322ab163f5fSMatt Jacob 			if (dval & DPARM_WIDE) {
5323ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5324ab163f5fSMatt Jacob 			} else {
5325ab163f5fSMatt Jacob 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5326ab163f5fSMatt Jacob 			}
5327ab163f5fSMatt Jacob 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
5328ab163f5fSMatt Jacob 				scsi->valid = CTS_SCSI_VALID_TQ;
5329b61386a4SMatt Jacob 				if (dval & DPARM_TQING) {
5330b61386a4SMatt Jacob 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5331b61386a4SMatt Jacob 				}
5332ab163f5fSMatt Jacob 				spi->valid |= CTS_SPI_VALID_DISC;
5333ab163f5fSMatt Jacob 			}
5334123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
5335123055f0SNathan Whitehorn 			    bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
5336478f8a96SJustin T. Gibbs 		}
5337478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
5338478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5339478f8a96SJustin T. Gibbs 		break;
5340478f8a96SJustin T. Gibbs 
5341478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
53422df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
53432df76c16SMatt Jacob 		xpt_done(ccb);
53442df76c16SMatt Jacob 		break;
5345478f8a96SJustin T. Gibbs 
5346478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
5347ab6c4b31SMatt Jacob 		bus = cam_sim_bus(sim);
53482df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
53492df76c16SMatt Jacob 		if (error) {
5350478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
53512df76c16SMatt Jacob 			xpt_done(ccb);
53522df76c16SMatt Jacob 			break;
53532df76c16SMatt Jacob 		}
53540a70657fSMatt Jacob 		if (bootverbose) {
53552df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
53560a70657fSMatt Jacob 		}
53572df76c16SMatt Jacob 		if (IS_FC(isp)) {
53582df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
53592df76c16SMatt Jacob 		} else {
53602df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
53612df76c16SMatt Jacob 		}
5362478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
5363478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5364478f8a96SJustin T. Gibbs 		break;
5365478f8a96SJustin T. Gibbs 
5366478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
5367478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
5368478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5369478f8a96SJustin T. Gibbs 		break;
5370478f8a96SJustin T. Gibbs 
53712df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
53722df76c16SMatt Jacob 	{
53732df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
53742df76c16SMatt Jacob 		fcparam *fcp;
53752df76c16SMatt Jacob 
53762df76c16SMatt Jacob 		if (!IS_FC(isp)) {
53772df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
53782df76c16SMatt Jacob 			xpt_done(ccb);
53792df76c16SMatt Jacob 			break;
53802df76c16SMatt Jacob 		}
53812df76c16SMatt Jacob 
53822df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
53832df76c16SMatt Jacob 		fcp = FCPARAM(isp, bus);
53842df76c16SMatt Jacob 
53852df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
53862df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
53872df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
53882df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
53892df76c16SMatt Jacob 		}
53902df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
53912df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
53922df76c16SMatt Jacob 			int rchange = 0;
53932df76c16SMatt Jacob 			int newrole = 0;
53942df76c16SMatt Jacob 
53952df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
53962df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
53972df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
53982df76c16SMatt Jacob 					rchange = 1;
53992df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
54002df76c16SMatt Jacob 				}
54012df76c16SMatt Jacob 				break;
54022df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
54032df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
54042df76c16SMatt Jacob 					rchange = 1;
54052df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
54062df76c16SMatt Jacob 				}
54072df76c16SMatt Jacob 				break;
54082df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
54092df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
54102df76c16SMatt Jacob 					rchange = 1;
54112df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
54122df76c16SMatt Jacob 				}
54132df76c16SMatt Jacob 				break;
54142df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
5415ae5db118SMatt Jacob #if 0
54162df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
54172df76c16SMatt Jacob 					rchange = 1;
54182df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
54192df76c16SMatt Jacob 				}
5420ae5db118SMatt Jacob #else
5421ae5db118SMatt Jacob 				/*
5422ae5db118SMatt Jacob 				 * We don't really support dual role at present on FC cards.
5423ae5db118SMatt Jacob 				 *
5424ae5db118SMatt Jacob 				 * We should, but a bunch of things are currently broken,
5425ae5db118SMatt Jacob 				 * so don't allow it.
5426ae5db118SMatt Jacob 				 */
5427ae5db118SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
5428ae5db118SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
5429ae5db118SMatt Jacob #endif
54302df76c16SMatt Jacob 				break;
54312df76c16SMatt Jacob 			}
54322df76c16SMatt Jacob 			if (rchange) {
5433e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
5434387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
5435387d8239SMatt Jacob 				ISP_SET_PC(isp, bus, tm_enabled, 0);
5436387d8239SMatt Jacob 				ISP_SET_PC(isp, bus, tm_luns_enabled, 0);
5437387d8239SMatt Jacob #endif
54382df76c16SMatt Jacob 				if (isp_fc_change_role(isp, bus, newrole) != 0) {
54392df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5440387d8239SMatt Jacob 					xpt_done(ccb);
5441387d8239SMatt Jacob 					break;
54422df76c16SMatt Jacob 				}
5443387d8239SMatt Jacob #ifdef	ISP_TARGET_MODE
5444387d8239SMatt Jacob 				if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
5445387d8239SMatt Jacob 					/*
5446387d8239SMatt Jacob 					 * Give the new role a chance to complain and settle
5447387d8239SMatt Jacob 					 */
5448387d8239SMatt Jacob 					msleep(isp, &isp->isp_lock, PRIBIO, "taking a breather", 2);
5449387d8239SMatt Jacob 					ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
5450387d8239SMatt Jacob 				}
5451387d8239SMatt Jacob #endif
54522df76c16SMatt Jacob 			}
54532df76c16SMatt Jacob 		}
54542df76c16SMatt Jacob 		xpt_done(ccb);
54552df76c16SMatt Jacob 		break;
54562df76c16SMatt Jacob 	}
5457e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
54582df76c16SMatt Jacob 	{
54592df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
54602df76c16SMatt Jacob 
54612df76c16SMatt Jacob 		if (IS_FC(isp)) {
54622df76c16SMatt Jacob 			fcparam *fcp;
54632df76c16SMatt Jacob 
54642df76c16SMatt Jacob 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
54652df76c16SMatt Jacob 			fcp = FCPARAM(isp, bus);
54662df76c16SMatt Jacob 
54672df76c16SMatt Jacob 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
54682df76c16SMatt Jacob 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
54692df76c16SMatt Jacob 			switch (fcp->role) {
54702df76c16SMatt Jacob 			case ISP_ROLE_NONE:
54712df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
54722df76c16SMatt Jacob 				break;
54732df76c16SMatt Jacob 			case ISP_ROLE_TARGET:
54742df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
54752df76c16SMatt Jacob 				break;
54762df76c16SMatt Jacob 			case ISP_ROLE_INITIATOR:
54772df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
54782df76c16SMatt Jacob 				break;
54792df76c16SMatt Jacob 			case ISP_ROLE_BOTH:
54802df76c16SMatt Jacob 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
54812df76c16SMatt Jacob 				break;
54822df76c16SMatt Jacob 			}
54832df76c16SMatt Jacob 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
54842df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP;
54852df76c16SMatt Jacob 		} else {
54862df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
54872df76c16SMatt Jacob 		}
54882df76c16SMatt Jacob 		xpt_done(ccb);
54892df76c16SMatt Jacob 		break;
54902df76c16SMatt Jacob 	}
5491478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
5492478f8a96SJustin T. Gibbs 	{
5493478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
5494478f8a96SJustin T. Gibbs 
5495478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
5496d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
5497a1bc34c6SMatt Jacob 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
5498d81ba9d5SMatt Jacob #else
5499478f8a96SJustin T. Gibbs 		cpi->target_sprt = 0;
5500d81ba9d5SMatt Jacob #endif
5501478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
55020470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
55030470d791SMatt Jacob 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
55040470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
55057bf825d1SKenneth D. Merry 		if (isp->isp_osinfo.sixtyfourbit)
55067bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
55077bf825d1SKenneth D. Merry 		else
55087bf825d1SKenneth D. Merry 			cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
55097bf825d1SKenneth D. Merry 
55102df76c16SMatt Jacob 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
55114394c92fSMatt Jacob 		if (IS_FC(isp)) {
55122df76c16SMatt Jacob 			fcparam *fcp = FCPARAM(isp, bus);
55132df76c16SMatt Jacob 
5514ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
55152df76c16SMatt Jacob 
55160470d791SMatt Jacob 			/*
55170470d791SMatt Jacob 			 * Because our loop ID can shift from time to time,
55180470d791SMatt Jacob 			 * make our initiator ID out of range of our bus.
55190470d791SMatt Jacob 			 */
55200470d791SMatt Jacob 			cpi->initiator_id = cpi->max_target + 1;
55210470d791SMatt Jacob 
55229deea857SKenneth D. Merry 			/*
55232df76c16SMatt Jacob 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
55249deea857SKenneth D. Merry 			 */
5525e95725cbSMatt Jacob 			if (IS_25XX(isp)) {
5526e95725cbSMatt Jacob 				cpi->base_transfer_speed = 8000000;
5527e95725cbSMatt Jacob 			} else if (IS_24XX(isp)) {
55282df76c16SMatt Jacob 				cpi->base_transfer_speed = 4000000;
55292df76c16SMatt Jacob 			} else if (IS_23XX(isp)) {
55302df76c16SMatt Jacob 				cpi->base_transfer_speed = 2000000;
55312df76c16SMatt Jacob 			} else {
55322df76c16SMatt Jacob 				cpi->base_transfer_speed = 1000000;
55332df76c16SMatt Jacob 			}
55340470d791SMatt Jacob 			cpi->hba_inquiry = PI_TAG_ABLE;
5535ab163f5fSMatt Jacob 			cpi->transport = XPORT_FC;
5536805e1f82SMatt Jacob 			cpi->transport_version = 0;
55372df76c16SMatt Jacob 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
55382df76c16SMatt Jacob 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
55392df76c16SMatt Jacob 			cpi->xport_specific.fc.port = fcp->isp_portid;
55402df76c16SMatt Jacob 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
5541478f8a96SJustin T. Gibbs 		} else {
55422df76c16SMatt Jacob 			sdparam *sdp = SDPARAM(isp, bus);
55430470d791SMatt Jacob 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
5544ab1aa38bSMarius Strobl 			cpi->hba_misc = PIM_UNMAPPED;
5545ea6f23cdSMatt Jacob 			cpi->initiator_id = sdp->isp_initiator_id;
55469deea857SKenneth D. Merry 			cpi->base_transfer_speed = 3300;
5547ab163f5fSMatt Jacob 			cpi->transport = XPORT_SPI;
5548805e1f82SMatt Jacob 			cpi->transport_version = 2;
5549478f8a96SJustin T. Gibbs 		}
5550ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
5551ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
5552478f8a96SJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5553478f8a96SJustin T. Gibbs 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
5554478f8a96SJustin T. Gibbs 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
5555478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
5556478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
5557478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5558478f8a96SJustin T. Gibbs 		break;
5559478f8a96SJustin T. Gibbs 	}
5560478f8a96SJustin T. Gibbs 	default:
5561478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
5562478f8a96SJustin T. Gibbs 		xpt_done(ccb);
5563478f8a96SJustin T. Gibbs 		break;
5564478f8a96SJustin T. Gibbs 	}
5565478f8a96SJustin T. Gibbs }
5566d3a9eb2eSMatt Jacob 
5567d3a9eb2eSMatt Jacob #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
5568f7c631bcSMatt Jacob 
5569d3a9eb2eSMatt Jacob void
55702df76c16SMatt Jacob isp_done(XS_T *sccb)
5571d3a9eb2eSMatt Jacob {
55729cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
5573e95725cbSMatt Jacob 	uint32_t status;
5574d3a9eb2eSMatt Jacob 
5575d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
5576d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
5577b85389e1SMatt Jacob 
55782df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
5579d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
55802df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
558192a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
558292a1e549SMatt Jacob 		} else {
5583d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
5584d3a9eb2eSMatt Jacob 		}
558592a1e549SMatt Jacob 	}
5586b85389e1SMatt Jacob 
55870470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5588e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
5589e95725cbSMatt Jacob 	if (status != CAM_REQ_CMP) {
5590e95725cbSMatt Jacob 		if (status != CAM_SEL_TIMEOUT)
55912df76c16SMatt 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);
55925704e6f0SKenneth D. Merry 		else if ((IS_FC(isp))
55935704e6f0SKenneth D. Merry 		      && (XS_TGT(sccb) < MAX_FC_TARG)) {
55945704e6f0SKenneth D. Merry 			fcparam *fcp;
55955704e6f0SKenneth D. Merry 			int hdlidx;
55965704e6f0SKenneth D. Merry 
55975704e6f0SKenneth D. Merry 			fcp = FCPARAM(isp, XS_CHANNEL(sccb));
55985704e6f0SKenneth D. Merry 			hdlidx = fcp->isp_dev_map[XS_TGT(sccb)] - 1;
55995704e6f0SKenneth D. Merry 			/*
56005704e6f0SKenneth D. Merry 			 * Note that we have reported that this device is
56015704e6f0SKenneth D. Merry 			 * gone.  If it reappears, we'll need to issue a
56025704e6f0SKenneth D. Merry 			 * rescan.
56035704e6f0SKenneth D. Merry 			 */
56045704e6f0SKenneth D. Merry 			if (hdlidx > 0 && hdlidx < MAX_FC_TARG)
56055704e6f0SKenneth D. Merry 				fcp->portdb[hdlidx].reported_gone = 1;
56065704e6f0SKenneth D. Merry 		}
5607d3a9eb2eSMatt Jacob 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
5608d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
56090470d791SMatt Jacob 			xpt_freeze_devq(sccb->ccb_h.path, 1);
5610d3a9eb2eSMatt Jacob 		}
5611d3a9eb2eSMatt Jacob 	}
5612b85389e1SMatt Jacob 
56132df76c16SMatt Jacob 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
56142df76c16SMatt Jacob 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
5615d3a9eb2eSMatt Jacob 	}
5616b85389e1SMatt Jacob 
5617387d8239SMatt Jacob 	if (callout_active(&PISP_PCMD(sccb)->wdog))
56180ec60713SMarius Strobl 		callout_stop(&PISP_PCMD(sccb)->wdog);
56190a70657fSMatt Jacob 	isp_free_pcmd(isp, (union ccb *) sccb);
5620d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
5621d3a9eb2eSMatt Jacob }
5622d3a9eb2eSMatt Jacob 
56232df76c16SMatt Jacob void
56242df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
5625cbf57b47SMatt Jacob {
56262df76c16SMatt Jacob 	int bus;
5627387d8239SMatt Jacob 	static const char prom0[] = "Chan %d PortID 0x%06x handle 0x%x %s %s WWPN 0x%08x%08x";
5628387d8239SMatt Jacob 	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x %s %s tgt %u WWPN 0x%08x%08x";
5629387d8239SMatt Jacob 	char buf[64];
5630f7c631bcSMatt Jacob 	char *msg = NULL;
563110365e5aSMatt Jacob 	target_id_t tgt;
563210365e5aSMatt Jacob 	fcportdb_t *lp;
5633a01f5aebSMatt Jacob 	struct isp_fc *fc;
563410365e5aSMatt Jacob 	struct cam_path *tmppath;
56352df76c16SMatt Jacob 	va_list ap;
563610365e5aSMatt Jacob 
5637cbf57b47SMatt Jacob 	switch (cmd) {
5638cbf57b47SMatt Jacob 	case ISPASYNC_NEW_TGT_PARAMS:
56390470d791SMatt Jacob 	{
5640ab163f5fSMatt Jacob 		struct ccb_trans_settings_scsi *scsi;
5641ab163f5fSMatt Jacob 		struct ccb_trans_settings_spi *spi;
5642cbf57b47SMatt Jacob 		int flags, tgt;
56432df76c16SMatt Jacob 		sdparam *sdp;
5644ab163f5fSMatt Jacob 		struct ccb_trans_settings cts;
5645cbf57b47SMatt Jacob 
564629f76675SMatt Jacob 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
5647ab163f5fSMatt Jacob 
56482df76c16SMatt Jacob 		va_start(ap, cmd);
56492df76c16SMatt Jacob 		bus = va_arg(ap, int);
56502df76c16SMatt Jacob 		tgt = va_arg(ap, int);
56512df76c16SMatt Jacob 		va_end(ap);
56522df76c16SMatt Jacob 		sdp = SDPARAM(isp, bus);
56532df76c16SMatt Jacob 
56542df76c16SMatt Jacob 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
56552df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5656cbf57b47SMatt Jacob 			break;
5657cbf57b47SMatt Jacob 		}
56589ce9bdafSMatt Jacob 		flags = sdp->isp_devparam[tgt].actv_flags;
5659ab163f5fSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
5660ab163f5fSMatt Jacob 		cts.protocol = PROTO_SCSI;
5661ab163f5fSMatt Jacob 		cts.transport = XPORT_SPI;
5662ab163f5fSMatt Jacob 
5663ab163f5fSMatt Jacob 		scsi = &cts.proto_specific.scsi;
5664ab163f5fSMatt Jacob 		spi = &cts.xport_specific.spi;
5665ab163f5fSMatt Jacob 
5666ab163f5fSMatt Jacob 		if (flags & DPARM_TQING) {
5667ab163f5fSMatt Jacob 			scsi->valid |= CTS_SCSI_VALID_TQ;
5668ab163f5fSMatt Jacob 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5669ab163f5fSMatt Jacob 		}
5670ab163f5fSMatt Jacob 
5671cbf57b47SMatt Jacob 		if (flags & DPARM_DISC) {
5672ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_DISC;
5673ab163f5fSMatt Jacob 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5674ab163f5fSMatt Jacob 		}
5675ab163f5fSMatt Jacob 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5676ab163f5fSMatt Jacob 		if (flags & DPARM_WIDE) {
5677ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5678ab163f5fSMatt Jacob 		} else {
5679ab163f5fSMatt Jacob 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5680ab163f5fSMatt Jacob 		}
5681ab163f5fSMatt Jacob 		if (flags & DPARM_SYNC) {
5682ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5683ab163f5fSMatt Jacob 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
56849ce9bdafSMatt Jacob 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
56859ce9bdafSMatt Jacob 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5686ab163f5fSMatt Jacob 		}
56872df76c16SMatt 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);
5688ab163f5fSMatt Jacob 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5689ab163f5fSMatt Jacob 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5690cbf57b47SMatt Jacob 		xpt_free_path(tmppath);
5691cbf57b47SMatt Jacob 		break;
56920470d791SMatt Jacob 	}
569357c801f5SMatt Jacob 	case ISPASYNC_BUS_RESET:
56942df76c16SMatt Jacob 	{
56952df76c16SMatt Jacob 		va_start(ap, cmd);
56962df76c16SMatt Jacob 		bus = va_arg(ap, int);
56972df76c16SMatt Jacob 		va_end(ap);
56982df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
56992df76c16SMatt Jacob 		if (IS_FC(isp)) {
57002df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
57012df76c16SMatt Jacob 		} else {
57022df76c16SMatt Jacob 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
570357c801f5SMatt Jacob 		}
570457c801f5SMatt Jacob 		break;
57052df76c16SMatt Jacob 	}
57065d571944SMatt Jacob 	case ISPASYNC_LIP:
5707f7c631bcSMatt Jacob 		if (msg == NULL) {
5708f7c631bcSMatt Jacob 			msg = "LIP Received";
57095d571944SMatt Jacob 		}
5710f7c631bcSMatt Jacob 		/* FALLTHROUGH */
57115d571944SMatt Jacob 	case ISPASYNC_LOOP_RESET:
5712f7c631bcSMatt Jacob 		if (msg == NULL) {
5713f7c631bcSMatt Jacob 			msg = "LOOP Reset";
57145d571944SMatt Jacob 		}
5715f7c631bcSMatt Jacob 		/* FALLTHROUGH */
571657c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
57172df76c16SMatt Jacob 	{
5718f7c631bcSMatt Jacob 		if (msg == NULL) {
5719f7c631bcSMatt Jacob 			msg = "LOOP Down";
572057c801f5SMatt Jacob 		}
57212df76c16SMatt Jacob 		va_start(ap, cmd);
57222df76c16SMatt Jacob 		bus = va_arg(ap, int);
57232df76c16SMatt Jacob 		va_end(ap);
57242df76c16SMatt Jacob 
5725a01f5aebSMatt Jacob 		FCPARAM(isp, bus)->link_active = 0;
57262df76c16SMatt Jacob 
57272df76c16SMatt Jacob 		fc = ISP_FC_PC(isp, bus);
5728a01f5aebSMatt Jacob 		if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
57292df76c16SMatt Jacob 			/*
57302df76c16SMatt Jacob 			 * We don't do any simq freezing if we are only in target mode
57312df76c16SMatt Jacob 			 */
57324ecb1d4aSMatt Jacob 			if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
57332df76c16SMatt Jacob 				if (fc->path) {
57342df76c16SMatt Jacob 					isp_freeze_loopdown(isp, bus, msg);
5735f7c631bcSMatt Jacob 				}
5736a01f5aebSMatt Jacob 				if (!callout_active(&fc->ldt)) {
57372df76c16SMatt Jacob 					callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5738387d8239SMatt Jacob 					isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5739f7c631bcSMatt Jacob 				}
57402df76c16SMatt Jacob 			}
5741a01f5aebSMatt Jacob 		}
5742fb4a4356SKenneth D. Merry 		isp_fcp_reset_crn(fc, /*tgt*/0, /*tgt_set*/ 0);
5743fb4a4356SKenneth D. Merry 
57442df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
574557c801f5SMatt Jacob 		break;
57462df76c16SMatt Jacob 	}
574757c801f5SMatt Jacob 	case ISPASYNC_LOOP_UP:
57482df76c16SMatt Jacob 		va_start(ap, cmd);
57492df76c16SMatt Jacob 		bus = va_arg(ap, int);
57502df76c16SMatt Jacob 		va_end(ap);
5751a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
57525d571944SMatt Jacob 		/*
57535d571944SMatt Jacob 		 * Now we just note that Loop has come up. We don't
57545d571944SMatt Jacob 		 * actually do anything because we're waiting for a
57555d571944SMatt Jacob 		 * Change Notify before activating the FC cleanup
57565d571944SMatt Jacob 		 * thread to look at the state of the loop again.
57575d571944SMatt Jacob 		 */
57582df76c16SMatt Jacob 		FCPARAM(isp, bus)->link_active = 1;
5759a01f5aebSMatt Jacob 		fc->loop_dead = 0;
5760a01f5aebSMatt Jacob 		fc->loop_down_time = 0;
57612df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
576257c801f5SMatt Jacob 		break;
576310365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
57642df76c16SMatt Jacob 		va_start(ap, cmd);
57652df76c16SMatt Jacob 		bus = va_arg(ap, int);
57662df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
57672df76c16SMatt Jacob 		va_end(ap);
5768a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
5769387d8239SMatt Jacob 		lp->announced = 0;
5770427fa8f9SMatt Jacob 		lp->gone_timer = 0;
5771387d8239SMatt Jacob 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
57722df76c16SMatt Jacob 			int dbidx = lp - FCPARAM(isp, bus)->portdb;
5773f7c631bcSMatt Jacob 			int i;
577402ab3379SMatt Jacob 
5775f7c631bcSMatt Jacob 			for (i = 0; i < MAX_FC_TARG; i++) {
5776f7c631bcSMatt Jacob 				if (i >= FL_ID && i <= SNS_ID) {
5777f7c631bcSMatt Jacob 					continue;
5778f7c631bcSMatt Jacob 				}
57792df76c16SMatt Jacob 				if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5780f7c631bcSMatt Jacob 					break;
5781f7c631bcSMatt Jacob 				}
5782f7c631bcSMatt Jacob 			}
5783f7c631bcSMatt Jacob 			if (i < MAX_FC_TARG) {
57842df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
57852df76c16SMatt Jacob 				lp->dev_map_idx = i + 1;
5786f7c631bcSMatt Jacob 			} else {
5787f7c631bcSMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "out of target ids");
57882df76c16SMatt Jacob 				isp_dump_portdb(isp, bus);
5789f7c631bcSMatt Jacob 			}
5790f7c631bcSMatt Jacob 		}
5791387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
57922df76c16SMatt Jacob 		if (lp->dev_map_idx) {
57932df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
5794387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5795fb4a4356SKenneth D. Merry 			isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
57965704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
579710365e5aSMatt Jacob 		} else {
5798387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
579910365e5aSMatt Jacob 		}
580010365e5aSMatt Jacob 		break;
580110365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
58022df76c16SMatt Jacob 		va_start(ap, cmd);
58032df76c16SMatt Jacob 		bus = va_arg(ap, int);
58042df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
58052df76c16SMatt Jacob 		va_end(ap);
5806a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
5807387d8239SMatt Jacob 		lp->announced = 0;
5808427fa8f9SMatt Jacob 		lp->gone_timer = 0;
5809f7c631bcSMatt Jacob 		if (isp_change_is_bad) {
5810f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_NIL;
58112df76c16SMatt Jacob 			if (lp->dev_map_idx) {
58122df76c16SMatt Jacob 				tgt = lp->dev_map_idx - 1;
58132df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
58142df76c16SMatt Jacob 				lp->dev_map_idx = 0;
58152df76c16SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
58165704e6f0SKenneth D. Merry 				isp_make_gone(isp, lp, bus, tgt);
5817f7c631bcSMatt Jacob 			} else {
5818387d8239SMatt Jacob 				isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5819387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "changed and departed",
58202df76c16SMatt Jacob 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5821f7c631bcSMatt Jacob 			}
5822f7c631bcSMatt Jacob 		} else {
5823f7c631bcSMatt Jacob 			lp->portid = lp->new_portid;
5824387d8239SMatt Jacob 			lp->prli_word3 = lp->new_prli_word3;
5825387d8239SMatt Jacob 			isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
58262df76c16SMatt Jacob 			if (lp->dev_map_idx) {
58272df76c16SMatt Jacob 				int t = lp->dev_map_idx - 1;
58282df76c16SMatt Jacob 				FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
58292df76c16SMatt Jacob 				tgt = lp->dev_map_idx - 1;
5830387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "changed at", tgt,
58312df76c16SMatt Jacob 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5832fb4a4356SKenneth D. Merry 				isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
583310365e5aSMatt Jacob 			} else {
5834387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
583510365e5aSMatt Jacob 			}
5836f7c631bcSMatt Jacob 		}
583710365e5aSMatt Jacob 		break;
583810365e5aSMatt Jacob 	case ISPASYNC_DEV_STAYED:
58392df76c16SMatt Jacob 		va_start(ap, cmd);
58402df76c16SMatt Jacob 		bus = va_arg(ap, int);
58412df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
58422df76c16SMatt Jacob 		va_end(ap);
5843387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
58442df76c16SMatt Jacob 		if (lp->dev_map_idx) {
5845fb4a4356SKenneth D. Merry 			fc = ISP_FC_PC(isp, bus);
58462df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
5847387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "stayed at", tgt,
58482df76c16SMatt Jacob 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
58495704e6f0SKenneth D. Merry 			/*
58505704e6f0SKenneth D. Merry 			 * Only issue a rescan if we've actually reported
58515704e6f0SKenneth D. Merry 			 * that this device is gone.
58525704e6f0SKenneth D. Merry 			 */
58535704e6f0SKenneth D. Merry 			if (lp->reported_gone != 0) {
58545704e6f0SKenneth D. Merry 				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "rescanned at", tgt,
58555704e6f0SKenneth D. Merry 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
58565704e6f0SKenneth D. Merry 				isp_make_here(isp, lp, bus, tgt);
58575704e6f0SKenneth D. Merry 			}
585810365e5aSMatt Jacob 		} else {
5859387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "stayed",
58602df76c16SMatt Jacob 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
586110365e5aSMatt Jacob 		}
586210365e5aSMatt Jacob 		break;
586310365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
58642df76c16SMatt Jacob 		va_start(ap, cmd);
58652df76c16SMatt Jacob 		bus = va_arg(ap, int);
58662df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
58672df76c16SMatt Jacob 		va_end(ap);
5868a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
5869f7c631bcSMatt Jacob 		/*
5870f7c631bcSMatt Jacob 		 * If this has a virtual target and we haven't marked it
5871f7c631bcSMatt Jacob 		 * that we're going to have isp_gdt tell the OS it's gone,
5872f7c631bcSMatt Jacob 		 * set the isp_gdt timer running on it.
5873f7c631bcSMatt Jacob 		 *
5874f7c631bcSMatt Jacob 		 * If it isn't marked that isp_gdt is going to get rid of it,
5875f7c631bcSMatt Jacob 		 * announce that it's gone.
58764ecb1d4aSMatt Jacob 		 *
5877f7c631bcSMatt Jacob 		 */
5878387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5879387d8239SMatt Jacob 		if (lp->dev_map_idx && lp->announced == 0) {
5880387d8239SMatt Jacob 			lp->announced = 1;
5881f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
5882427fa8f9SMatt Jacob 			lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5883a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
5884387d8239SMatt Jacob 				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
5885a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
5886f7c631bcSMatt Jacob 			}
58872df76c16SMatt Jacob 			tgt = lp->dev_map_idx - 1;
5888387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5889fb4a4356SKenneth D. Merry 			isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5890387d8239SMatt Jacob 		} else if (lp->announced == 0) {
5891387d8239SMatt Jacob 			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
58924b9d588eSMatt Jacob 		}
589310365e5aSMatt Jacob 		break;
589410365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
589510365e5aSMatt Jacob 	{
589610365e5aSMatt Jacob 		char *msg;
58972df76c16SMatt Jacob 		int evt, nphdl, nlstate, reason;
58982df76c16SMatt Jacob 
58992df76c16SMatt Jacob 		va_start(ap, cmd);
59002df76c16SMatt Jacob 		bus = va_arg(ap, int);
59012df76c16SMatt Jacob 		evt = va_arg(ap, int);
59022df76c16SMatt Jacob 		if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
59032df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
59042df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
59052df76c16SMatt Jacob 			reason = va_arg(ap, int);
590610365e5aSMatt Jacob 		} else {
59072df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
59082df76c16SMatt Jacob 			nlstate = reason = 0;
590910365e5aSMatt Jacob 		}
59102df76c16SMatt Jacob 		va_end(ap);
5911a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
59122df76c16SMatt Jacob 
59132df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
59142df76c16SMatt Jacob 			msg = "Chan %d Port Database Changed";
59152df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
59162df76c16SMatt Jacob 			msg = "Chan %d Name Server Database Changed";
59172df76c16SMatt Jacob 		} else {
59182df76c16SMatt Jacob 			msg = "Chan %d Other Change Notify";
59192df76c16SMatt Jacob 		}
59202df76c16SMatt Jacob 
5921f7c631bcSMatt Jacob 		/*
5922f7c631bcSMatt Jacob 		 * If the loop down timer is running, cancel it.
5923f7c631bcSMatt Jacob 		 */
5924a01f5aebSMatt Jacob 		if (fc->ready && callout_active(&fc->ldt)) {
5925387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5926a01f5aebSMatt Jacob 			callout_stop(&fc->ldt);
5927f7c631bcSMatt Jacob 		}
59282df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGINFO, msg, bus);
59294ecb1d4aSMatt Jacob 		if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
59302df76c16SMatt Jacob 			isp_freeze_loopdown(isp, bus, msg);
59312df76c16SMatt Jacob 		}
5932a01f5aebSMatt Jacob 		wakeup(fc);
593357c801f5SMatt Jacob 		break;
593402ab3379SMatt Jacob 	}
5935d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
5936e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
5937d81ba9d5SMatt Jacob 	{
59382df76c16SMatt Jacob 		isp_notify_t *notify;
59392df76c16SMatt Jacob 		va_start(ap, cmd);
59402df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
59412df76c16SMatt Jacob 		va_end(ap);
59422df76c16SMatt Jacob 		switch (notify->nt_ncode) {
59432df76c16SMatt Jacob 		case NT_ABORT_TASK:
59442df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
59452df76c16SMatt Jacob 		case NT_CLEAR_ACA:
59462df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
59472df76c16SMatt Jacob 		case NT_LUN_RESET:
59482df76c16SMatt Jacob 		case NT_TARGET_RESET:
59492df76c16SMatt Jacob 			/*
59502df76c16SMatt Jacob 			 * These are task management functions.
59512df76c16SMatt Jacob 			 */
59522df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
59532df76c16SMatt Jacob 			break;
59542df76c16SMatt Jacob 		case NT_BUS_RESET:
59552df76c16SMatt Jacob 		case NT_LIP_RESET:
59562df76c16SMatt Jacob 		case NT_LINK_UP:
59572df76c16SMatt Jacob 		case NT_LINK_DOWN:
59582df76c16SMatt Jacob 			/*
59592df76c16SMatt Jacob 			 * No action need be taken here.
59602df76c16SMatt Jacob 			 */
59612df76c16SMatt Jacob 			break;
59622df76c16SMatt Jacob 		case NT_HBA_RESET:
59632df76c16SMatt Jacob 			isp_del_all_wwn_entries(isp, ISP_NOCHAN);
59642df76c16SMatt Jacob 			break;
5965387d8239SMatt Jacob 		case NT_GLOBAL_LOGOUT:
59662df76c16SMatt Jacob 		case NT_LOGOUT:
59672df76c16SMatt Jacob 			/*
59682df76c16SMatt Jacob 			 * This is device arrival/departure notification
59692df76c16SMatt Jacob 			 */
59702df76c16SMatt Jacob 			isp_handle_platform_target_notify_ack(isp, notify);
59712df76c16SMatt Jacob 			break;
59722df76c16SMatt Jacob 		case NT_ARRIVED:
59732df76c16SMatt Jacob 		{
59742df76c16SMatt Jacob 			struct ac_contract ac;
59752df76c16SMatt Jacob 			struct ac_device_changed *fc;
59762df76c16SMatt Jacob 
59772df76c16SMatt Jacob 			ac.contract_number = AC_CONTRACT_DEV_CHG;
59782df76c16SMatt Jacob 			fc = (struct ac_device_changed *) ac.contract_data;
59792df76c16SMatt Jacob 			fc->wwpn = notify->nt_wwn;
59802df76c16SMatt Jacob 			fc->port = notify->nt_sid;
59812df76c16SMatt Jacob 			fc->target = notify->nt_nphdl;
59822df76c16SMatt Jacob 			fc->arrived = 1;
59832df76c16SMatt Jacob 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
59842df76c16SMatt Jacob 			break;
59852df76c16SMatt Jacob 		}
59862df76c16SMatt Jacob 		case NT_DEPARTED:
59872df76c16SMatt Jacob 		{
59882df76c16SMatt Jacob 			struct ac_contract ac;
59892df76c16SMatt Jacob 			struct ac_device_changed *fc;
59902df76c16SMatt Jacob 
59912df76c16SMatt Jacob 			ac.contract_number = AC_CONTRACT_DEV_CHG;
59922df76c16SMatt Jacob 			fc = (struct ac_device_changed *) ac.contract_data;
59932df76c16SMatt Jacob 			fc->wwpn = notify->nt_wwn;
59942df76c16SMatt Jacob 			fc->port = notify->nt_sid;
59952df76c16SMatt Jacob 			fc->target = notify->nt_nphdl;
59962df76c16SMatt Jacob 			fc->arrived = 0;
59972df76c16SMatt Jacob 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
59982df76c16SMatt Jacob 			break;
59992df76c16SMatt Jacob 		}
60002df76c16SMatt Jacob 		default:
60012df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
60022df76c16SMatt Jacob 			isp_handle_platform_target_notify_ack(isp, notify);
60032df76c16SMatt Jacob 			break;
60042df76c16SMatt Jacob 		}
6005d81ba9d5SMatt Jacob 		break;
6006d81ba9d5SMatt Jacob 	}
6007387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
6008387d8239SMatt Jacob 	{
6009387d8239SMatt Jacob 		void *inot;
6010387d8239SMatt Jacob 		va_start(ap, cmd);
6011387d8239SMatt Jacob 		inot = va_arg(ap, void *);
6012387d8239SMatt Jacob 		va_end(ap);
6013387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
6014387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
6015387d8239SMatt Jacob 			if (tp) {
6016387d8239SMatt Jacob 				tp->isp = isp;
6017387d8239SMatt Jacob 				if (inot) {
6018387d8239SMatt Jacob 					memcpy(tp->data, inot, sizeof (tp->data));
6019387d8239SMatt Jacob 					tp->not = tp->data;
6020387d8239SMatt Jacob 				} else {
6021387d8239SMatt Jacob 					tp->not = NULL;
6022387d8239SMatt Jacob 				}
60232a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
60242a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
60252a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
6026387d8239SMatt Jacob 			} else {
6027387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
6028387d8239SMatt Jacob 			}
6029387d8239SMatt Jacob 		}
6030387d8239SMatt Jacob 		break;
6031387d8239SMatt Jacob 	}
6032d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
60332df76c16SMatt Jacob 	{
60342df76c16SMatt Jacob 		isphdr_t *hp;
60352df76c16SMatt Jacob 
60362df76c16SMatt Jacob 		va_start(ap, cmd);
60372df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
60382df76c16SMatt Jacob 		va_end(ap);
60392df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
6040cbf57b47SMatt Jacob 		default:
60412df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
6042d81ba9d5SMatt Jacob 			break;
6043570c7a3fSMatt Jacob 		case RQSTYPE_NOTIFY:
6044570c7a3fSMatt Jacob 			if (IS_SCSI(isp)) {
60452df76c16SMatt Jacob 				isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
60462df76c16SMatt Jacob 			} else if (IS_24XX(isp)) {
60472df76c16SMatt Jacob 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
6048570c7a3fSMatt Jacob 			} else {
60492df76c16SMatt Jacob 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
6050570c7a3fSMatt Jacob 			}
6051570c7a3fSMatt Jacob 			break;
6052d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
60532df76c16SMatt Jacob 			if (IS_24XX(isp)) {
60542df76c16SMatt Jacob 				isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
60552df76c16SMatt Jacob 			} else {
60562df76c16SMatt Jacob 				isp_handle_platform_atio(isp, (at_entry_t *) hp);
60572df76c16SMatt Jacob 			}
6058d81ba9d5SMatt Jacob 			break;
6059d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO2:
60602df76c16SMatt Jacob 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
6061d81ba9d5SMatt Jacob 			break;
60622df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
6063d4a6993aSMatt Jacob 		case RQSTYPE_CTIO3:
6064d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO2:
6065d81ba9d5SMatt Jacob 		case RQSTYPE_CTIO:
60662df76c16SMatt Jacob 			isp_handle_platform_ctio(isp, hp);
6067d81ba9d5SMatt Jacob 			break;
60682df76c16SMatt Jacob 		case RQSTYPE_ABTS_RCVD:
60692df76c16SMatt Jacob 		{
60702df76c16SMatt Jacob 			abts_t *abts = (abts_t *)hp;
60712df76c16SMatt Jacob 			isp_notify_t notify, *nt = &notify;
60722df76c16SMatt Jacob 			tstate_t *tptr;
60732df76c16SMatt Jacob 			fcportdb_t *lp;
60742df76c16SMatt Jacob 			uint16_t chan;
60752df76c16SMatt Jacob 			uint32_t sid, did;
60762df76c16SMatt Jacob 
60772df76c16SMatt Jacob 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
60782df76c16SMatt Jacob 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
60792df76c16SMatt Jacob 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
60802df76c16SMatt Jacob 
60812df76c16SMatt Jacob 			nt->nt_hba = isp;
60822df76c16SMatt Jacob 			nt->nt_did = did;
60832df76c16SMatt Jacob 			nt->nt_nphdl = abts->abts_nphdl;
60842df76c16SMatt Jacob 			nt->nt_sid = sid;
60852df76c16SMatt Jacob 			isp_find_chan_by_did(isp, did, &chan);
60862df76c16SMatt Jacob 			if (chan == ISP_NOCHAN) {
60872df76c16SMatt Jacob 				nt->nt_tgt = TGT_ANY;
60882df76c16SMatt Jacob 			} else {
60892df76c16SMatt Jacob 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
60902df76c16SMatt Jacob 				if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
60912df76c16SMatt Jacob 					nt->nt_wwn = lp->port_wwn;
60922df76c16SMatt Jacob 				} else {
60932df76c16SMatt Jacob 					nt->nt_wwn = INI_ANY;
60942df76c16SMatt Jacob 				}
60952df76c16SMatt Jacob 			}
60962df76c16SMatt Jacob 			/*
60972df76c16SMatt Jacob 			 * Try hard to find the lun for this command.
60982df76c16SMatt Jacob 			 */
60992df76c16SMatt Jacob 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
61002df76c16SMatt Jacob 			if (tptr) {
6101315a4d6fSAlexander Motin 				nt->nt_lun = tptr->ts_lun;
61022df76c16SMatt Jacob 				rls_lun_statep(isp, tptr);
61032df76c16SMatt Jacob 			} else {
61042df76c16SMatt Jacob 				nt->nt_lun = LUN_ANY;
61052df76c16SMatt Jacob 			}
61062df76c16SMatt Jacob 			nt->nt_need_ack = 1;
61072df76c16SMatt Jacob 			nt->nt_tagval = abts->abts_rxid_task;
61082df76c16SMatt Jacob 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
61092df76c16SMatt Jacob 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
61102df76c16SMatt 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)",
61112df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
61122df76c16SMatt Jacob 			} else {
61132df76c16SMatt 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)",
61142df76c16SMatt Jacob 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
61152df76c16SMatt Jacob 			}
61162df76c16SMatt Jacob 			nt->nt_channel = chan;
61172df76c16SMatt Jacob 			nt->nt_ncode = NT_ABORT_TASK;
61182df76c16SMatt Jacob 			nt->nt_lreserved = hp;
61192df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, nt);
61202df76c16SMatt Jacob 			break;
61212df76c16SMatt Jacob 		}
6122d81ba9d5SMatt Jacob 		case RQSTYPE_ENABLE_LUN:
6123d81ba9d5SMatt Jacob 		case RQSTYPE_MODIFY_LUN:
61242df76c16SMatt Jacob 			isp_ledone(isp, (lun_entry_t *) hp);
6125d81ba9d5SMatt Jacob 			break;
6126d81ba9d5SMatt Jacob 		}
6127d81ba9d5SMatt Jacob 		break;
61282df76c16SMatt Jacob 	}
6129d81ba9d5SMatt Jacob #endif
6130ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
6131ab163f5fSMatt Jacob 	{
61321dae40ebSMatt Jacob 		uint16_t mbox1, mbox6;
6133ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
6134ab163f5fSMatt Jacob 		if (IS_DUALBUS(isp)) {
6135ab163f5fSMatt Jacob 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
6136ab163f5fSMatt Jacob 		} else {
6137ab163f5fSMatt Jacob 			mbox6 = 0;
6138ab163f5fSMatt Jacob 		}
61392df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
61400a70657fSMatt Jacob 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
61410a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
61422df76c16SMatt Jacob 		isp_reinit(isp, 1);
61430a70657fSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
61440a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
6145ab163f5fSMatt Jacob 		break;
6146ab163f5fSMatt Jacob 	}
6147d81ba9d5SMatt Jacob 	default:
6148b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
6149cbf57b47SMatt Jacob 		break;
6150cbf57b47SMatt Jacob 	}
6151cbf57b47SMatt Jacob }
6152cbf57b47SMatt Jacob 
615392718a7fSMatt Jacob 
615492718a7fSMatt Jacob /*
615592718a7fSMatt Jacob  * Locks are held before coming here.
615692718a7fSMatt Jacob  */
615792718a7fSMatt Jacob void
61589cd7268eSMatt Jacob isp_uninit(ispsoftc_t *isp)
615992718a7fSMatt Jacob {
616010365e5aSMatt Jacob 	if (IS_24XX(isp)) {
616110365e5aSMatt Jacob 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
616210365e5aSMatt Jacob 	} else {
6163ea6f23cdSMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
616410365e5aSMatt Jacob 	}
616510365e5aSMatt Jacob 	ISP_DISABLE_INTS(isp);
616692718a7fSMatt Jacob }
6167b09b0095SMatt Jacob 
61682df76c16SMatt Jacob /*
61692df76c16SMatt Jacob  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
61702df76c16SMatt Jacob  * up from our platform default (defww{p|n}n) and morph them based upon
61712df76c16SMatt Jacob  * channel.
61722df76c16SMatt Jacob  *
61732df76c16SMatt Jacob  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
61742df76c16SMatt Jacob  * based upon channel.
61752df76c16SMatt Jacob  */
61762df76c16SMatt Jacob 
61772df76c16SMatt Jacob uint64_t
61782df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
61792df76c16SMatt Jacob {
61802df76c16SMatt Jacob 	uint64_t seed;
61812df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
61822df76c16SMatt Jacob 
61832df76c16SMatt Jacob 	/*
61842df76c16SMatt Jacob 	 * If we're asking for a active WWN, the default overrides get
61852df76c16SMatt Jacob 	 * returned, otherwise the NVRAM value is picked.
61862df76c16SMatt Jacob 	 *
61872df76c16SMatt Jacob 	 * If we're asking for a default WWN, we just pick the default override.
61882df76c16SMatt Jacob 	 */
61892df76c16SMatt Jacob 	if (isactive) {
61902df76c16SMatt Jacob 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
61912df76c16SMatt Jacob 		if (seed) {
61922df76c16SMatt Jacob 			return (seed);
61932df76c16SMatt Jacob 		}
61942df76c16SMatt Jacob 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
61955cc3786cSMatt Jacob 		if (seed) {
61965cc3786cSMatt Jacob 			return (seed);
61975cc3786cSMatt Jacob 		}
61985cc3786cSMatt Jacob 		return (0x400000007F000009ull);
61992df76c16SMatt Jacob 	}
62002df76c16SMatt Jacob 
62012731e062SAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
62022df76c16SMatt Jacob 
62032df76c16SMatt Jacob 	/*
6204b1ce21c6SRebecca Cran 	 * For channel zero just return what we have. For either ACTIVE or
62052df76c16SMatt Jacob 	 * DEFAULT cases, we depend on default override of NVRAM values for
62062df76c16SMatt Jacob 	 * channel zero.
62072df76c16SMatt Jacob 	 */
62082df76c16SMatt Jacob 	if (chan == 0) {
62092df76c16SMatt Jacob 		return (seed);
62102df76c16SMatt Jacob 	}
62112df76c16SMatt Jacob 
62122df76c16SMatt Jacob 	/*
62132df76c16SMatt Jacob 	 * For other channels, we are doing one of three things:
62142df76c16SMatt Jacob 	 *
62152df76c16SMatt Jacob 	 * 1. If what we have now is non-zero, return it. Otherwise we morph
62162df76c16SMatt Jacob 	 * values from channel 0. 2. If we're here for a WWPN we synthesize
62172df76c16SMatt Jacob 	 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
62182df76c16SMatt Jacob 	 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
62192df76c16SMatt Jacob 	 */
62202df76c16SMatt Jacob 
62212df76c16SMatt Jacob 	if (seed) {
62222df76c16SMatt Jacob 		return (seed);
62232df76c16SMatt Jacob 	}
62242df76c16SMatt Jacob 	seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
62252731e062SAlexander Motin 	if (seed == 0)
62262731e062SAlexander Motin 		seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
62272df76c16SMatt Jacob 
62282df76c16SMatt Jacob 	if (((seed >> 60) & 0xf) == 2) {
62292df76c16SMatt Jacob 		/*
62302df76c16SMatt Jacob 		 * The type 2 NAA fields for QLogic cards appear be laid out
62312df76c16SMatt Jacob 		 * thusly:
62322df76c16SMatt Jacob 		 *
62332df76c16SMatt Jacob 		 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
62342df76c16SMatt Jacob 		 * port (1) or node (0) WWN distinguishor bit 48
62352df76c16SMatt Jacob 		 * physical port on dual-port chips (23XX/24XX)
62362df76c16SMatt Jacob 		 *
62372df76c16SMatt Jacob 		 * This is somewhat nutty, particularly since bit 48 is
6238b1ce21c6SRebecca Cran 		 * irrelevant as they assign separate serial numbers to
62392df76c16SMatt Jacob 		 * different physical ports anyway.
62402df76c16SMatt Jacob 		 *
62412df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
62422df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
62432df76c16SMatt Jacob 		 * of channel which is comfortably more than our maximum
62442df76c16SMatt Jacob 		 * (126) now.
62452df76c16SMatt Jacob 		 */
62462df76c16SMatt Jacob 		seed &= ~0x0FF0000000000000ULL;
62472df76c16SMatt Jacob 		if (iswwnn == 0) {
62482df76c16SMatt Jacob 			seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
62492df76c16SMatt Jacob 			seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
62502df76c16SMatt Jacob 		}
62512df76c16SMatt Jacob 	} else {
62522df76c16SMatt Jacob 		seed = 0;
62532df76c16SMatt Jacob 	}
62542df76c16SMatt Jacob 	return (seed);
62552df76c16SMatt Jacob }
62562df76c16SMatt Jacob 
6257b09b0095SMatt Jacob void
62589cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
6259b09b0095SMatt Jacob {
6260de461933SMatt Jacob 	int loc;
6261387d8239SMatt Jacob 	char lbuf[200];
6262b09b0095SMatt Jacob 	va_list ap;
6263de461933SMatt Jacob 
6264b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6265b09b0095SMatt Jacob 		return;
6266b09b0095SMatt Jacob 	}
6267387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
6268de461933SMatt Jacob 	loc = strlen(lbuf);
6269b09b0095SMatt Jacob 	va_start(ap, fmt);
6270de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
6271b09b0095SMatt Jacob 	va_end(ap);
6272de461933SMatt Jacob 	printf("%s\n", lbuf);
6273b09b0095SMatt Jacob }
6274f7c631bcSMatt Jacob 
6275670508b1SMatt Jacob void
6276670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
6277670508b1SMatt Jacob {
6278670508b1SMatt Jacob 	va_list ap;
6279670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6280670508b1SMatt Jacob 		return;
6281670508b1SMatt Jacob 	}
6282670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
6283670508b1SMatt Jacob 	va_start(ap, fmt);
6284670508b1SMatt Jacob 	vprintf(fmt, ap);
6285670508b1SMatt Jacob 	va_end(ap);
6286670508b1SMatt Jacob 	printf("\n");
6287670508b1SMatt Jacob }
6288670508b1SMatt Jacob 
6289f7c631bcSMatt Jacob uint64_t
6290f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
6291f7c631bcSMatt Jacob {
6292f7c631bcSMatt Jacob 	uint64_t elapsed;
6293f7c631bcSMatt Jacob 	struct timespec x = *b;
6294f7c631bcSMatt Jacob 	timespecsub(&x, a);
6295f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
6296f7c631bcSMatt Jacob 	if (elapsed == 0)
6297f7c631bcSMatt Jacob 		elapsed++;
6298f7c631bcSMatt Jacob 	return (elapsed);
6299f7c631bcSMatt Jacob }
6300f7c631bcSMatt Jacob 
6301f7c631bcSMatt Jacob int
6302f7c631bcSMatt Jacob isp_mbox_acquire(ispsoftc_t *isp)
6303f7c631bcSMatt Jacob {
6304f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxbsy) {
6305f7c631bcSMatt Jacob 		return (1);
6306f7c631bcSMatt Jacob 	} else {
6307f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 0;
6308f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxbsy = 1;
6309f7c631bcSMatt Jacob 		return (0);
6310f7c631bcSMatt Jacob 	}
6311f7c631bcSMatt Jacob }
6312f7c631bcSMatt Jacob 
6313f7c631bcSMatt Jacob void
6314f7c631bcSMatt Jacob isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
6315f7c631bcSMatt Jacob {
6316a4f3a2beSMatt Jacob 	unsigned int usecs = mbp->timeout;
6317a4f3a2beSMatt Jacob 	unsigned int max, olim, ilim;
6318f7c631bcSMatt Jacob 
6319f7c631bcSMatt Jacob 	if (usecs == 0) {
6320f7c631bcSMatt Jacob 		usecs = MBCMD_DEFAULT_TIMEOUT;
6321f7c631bcSMatt Jacob 	}
6322a4f3a2beSMatt Jacob 	max = isp->isp_mbxwrk0 + 1;
6323a4f3a2beSMatt Jacob 
6324f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleep_ok) {
6325a4f3a2beSMatt Jacob 		unsigned int ms = (usecs + 999) / 1000;
6326a4f3a2beSMatt Jacob 
6327f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 0;
6328f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 1;
6329a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
63302df76c16SMatt Jacob 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
6331a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
6332a4f3a2beSMatt Jacob 				break;
6333a4f3a2beSMatt Jacob 			}
6334a4f3a2beSMatt Jacob 		}
6335f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleep_ok = 1;
6336f7c631bcSMatt Jacob 		isp->isp_osinfo.mbox_sleeping = 0;
6337f7c631bcSMatt Jacob 	} else {
6338a4f3a2beSMatt Jacob 		for (olim = 0; olim < max; olim++) {
6339a4f3a2beSMatt Jacob 			for (ilim = 0; ilim < usecs; ilim += 100) {
6340f7c631bcSMatt Jacob 				uint32_t isr;
6341f7c631bcSMatt Jacob 				uint16_t sema, mbox;
6342f7c631bcSMatt Jacob 				if (isp->isp_osinfo.mboxcmd_done) {
6343f7c631bcSMatt Jacob 					break;
6344f7c631bcSMatt Jacob 				}
6345f7c631bcSMatt Jacob 				if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
6346f7c631bcSMatt Jacob 					isp_intr(isp, isr, sema, mbox);
6347f7c631bcSMatt Jacob 					if (isp->isp_osinfo.mboxcmd_done) {
6348f7c631bcSMatt Jacob 						break;
6349f7c631bcSMatt Jacob 					}
6350f7c631bcSMatt Jacob 				}
63512df76c16SMatt Jacob 				ISP_DELAY(100);
6352f7c631bcSMatt Jacob 			}
6353a4f3a2beSMatt Jacob 			if (isp->isp_osinfo.mboxcmd_done) {
6354a4f3a2beSMatt Jacob 				break;
6355a4f3a2beSMatt Jacob 			}
6356a4f3a2beSMatt Jacob 		}
6357f7c631bcSMatt Jacob 	}
6358f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mboxcmd_done == 0) {
63592df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
63602df76c16SMatt Jacob 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
6361f7c631bcSMatt Jacob 		mbp->param[0] = MBOX_TIMEOUT;
6362f7c631bcSMatt Jacob 		isp->isp_osinfo.mboxcmd_done = 1;
6363f7c631bcSMatt Jacob 	}
6364f7c631bcSMatt Jacob }
6365f7c631bcSMatt Jacob 
6366f7c631bcSMatt Jacob void
6367f7c631bcSMatt Jacob isp_mbox_notify_done(ispsoftc_t *isp)
6368f7c631bcSMatt Jacob {
6369f7c631bcSMatt Jacob 	if (isp->isp_osinfo.mbox_sleeping) {
6370f7c631bcSMatt Jacob 		wakeup(&isp->isp_mbxworkp);
6371f7c631bcSMatt Jacob 	}
6372f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxcmd_done = 1;
6373f7c631bcSMatt Jacob }
6374f7c631bcSMatt Jacob 
6375f7c631bcSMatt Jacob void
6376f7c631bcSMatt Jacob isp_mbox_release(ispsoftc_t *isp)
6377f7c631bcSMatt Jacob {
6378f7c631bcSMatt Jacob 	isp->isp_osinfo.mboxbsy = 0;
6379f7c631bcSMatt Jacob }
6380f7c631bcSMatt Jacob 
6381f7c631bcSMatt Jacob int
63822df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
63832df76c16SMatt Jacob {
63842df76c16SMatt Jacob 	int ret = 0;
63852df76c16SMatt Jacob 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
63862df76c16SMatt Jacob 		ret = -1;
63872df76c16SMatt Jacob 	} else {
63882df76c16SMatt Jacob 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
63892df76c16SMatt Jacob 	}
63902df76c16SMatt Jacob 	return (ret);
63912df76c16SMatt Jacob }
63922df76c16SMatt Jacob 
63932df76c16SMatt Jacob int
6394f7c631bcSMatt Jacob isp_mstohz(int ms)
6395f7c631bcSMatt Jacob {
6396a4f3a2beSMatt Jacob 	int hz;
6397f7c631bcSMatt Jacob 	struct timeval t;
6398f7c631bcSMatt Jacob 	t.tv_sec = ms / 1000;
6399f7c631bcSMatt Jacob 	t.tv_usec = (ms % 1000) * 1000;
6400a4f3a2beSMatt Jacob 	hz = tvtohz(&t);
6401a4f3a2beSMatt Jacob 	if (hz < 0) {
6402a4f3a2beSMatt Jacob 		hz = 0x7fffffff;
6403f7c631bcSMatt Jacob 	}
6404a4f3a2beSMatt Jacob 	if (hz == 0) {
6405a4f3a2beSMatt Jacob 		hz = 1;
6406a4f3a2beSMatt Jacob 	}
6407a4f3a2beSMatt Jacob 	return (hz);
6408f7c631bcSMatt Jacob }
64090a70657fSMatt Jacob 
64100a70657fSMatt Jacob void
64110a70657fSMatt Jacob isp_platform_intr(void *arg)
64120a70657fSMatt Jacob {
64130a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
64140a70657fSMatt Jacob 	uint32_t isr;
64150a70657fSMatt Jacob 	uint16_t sema, mbox;
64160a70657fSMatt Jacob 
64170a70657fSMatt Jacob 	ISP_LOCK(isp);
64180a70657fSMatt Jacob 	isp->isp_intcnt++;
64190a70657fSMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
64200a70657fSMatt Jacob 		isp->isp_intbogus++;
64210a70657fSMatt Jacob 	} else {
64220a70657fSMatt Jacob 		isp_intr(isp, isr, sema, mbox);
64230a70657fSMatt Jacob 	}
64240a70657fSMatt Jacob 	ISP_UNLOCK(isp);
64250a70657fSMatt Jacob }
64260a70657fSMatt Jacob 
64270a70657fSMatt Jacob void
64280a70657fSMatt Jacob isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
64290a70657fSMatt Jacob {
64300a70657fSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
64312df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
64320a70657fSMatt Jacob 	} else {
64332df76c16SMatt Jacob 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
64340a70657fSMatt Jacob 	}
64350a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
64360a70657fSMatt Jacob }
64372df76c16SMatt Jacob 
6438fb4a4356SKenneth D. Merry /*
6439fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
6440fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
6441fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
6442fb4a4356SKenneth D. Merry  */
6443fb4a4356SKenneth D. Merry void
6444fb4a4356SKenneth D. Merry isp_fcp_reset_crn(struct isp_fc *fc, uint32_t tgt, int tgt_set)
6445fb4a4356SKenneth D. Merry {
6446fb4a4356SKenneth D. Merry 	int i;
6447fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
6448fb4a4356SKenneth D. Merry 
6449fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
6450fb4a4356SKenneth D. Merry 		isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN on all targets");
6451fb4a4356SKenneth D. Merry 	else
6452fb4a4356SKenneth D. Merry 		isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN target %u", tgt);
6453fb4a4356SKenneth D. Merry 
6454fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
6455fb4a4356SKenneth D. Merry 		nxp = fc->nexus_hash[i];
6456fb4a4356SKenneth D. Merry 		while (nxp) {
6457fb4a4356SKenneth D. Merry 			if ((tgt_set != 0) && (tgt == nxp->tgt))
6458fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
6459fb4a4356SKenneth D. Merry 
6460fb4a4356SKenneth D. Merry 			nxp = nxp->next;
6461fb4a4356SKenneth D. Merry 		}
6462fb4a4356SKenneth D. Merry 	}
6463fb4a4356SKenneth D. Merry }
6464fb4a4356SKenneth D. Merry 
6465387d8239SMatt Jacob int
6466387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
6467387d8239SMatt Jacob {
6468405b7a29SMatt Jacob 	uint32_t chan, tgt, lun;
6469405b7a29SMatt Jacob 	struct isp_fc *fc;
6470405b7a29SMatt Jacob 	struct isp_nexus *nxp;
6471405b7a29SMatt Jacob 	int idx;
6472405b7a29SMatt Jacob 
6473405b7a29SMatt Jacob 	if (isp->isp_type < ISP_HA_FC_2300)
6474405b7a29SMatt Jacob 		return (0);
6475405b7a29SMatt Jacob 
6476405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
6477405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
6478405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
6479405b7a29SMatt Jacob 	fc = &isp->isp_osinfo.pc.fc[chan];
6480405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
6481405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
6482387d8239SMatt Jacob 
6483387d8239SMatt Jacob 	while (nxp) {
6484387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
6485387d8239SMatt Jacob 			break;
6486387d8239SMatt Jacob 		nxp = nxp->next;
6487387d8239SMatt Jacob 	}
6488387d8239SMatt Jacob 	if (nxp == NULL) {
6489387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
6490387d8239SMatt Jacob 		if (nxp == NULL) {
6491387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
6492387d8239SMatt Jacob 			if (nxp == NULL) {
6493387d8239SMatt Jacob 				return (-1);
6494387d8239SMatt Jacob 			}
6495387d8239SMatt Jacob 		} else {
6496387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
6497387d8239SMatt Jacob 		}
6498387d8239SMatt Jacob 		nxp->tgt = tgt;
6499387d8239SMatt Jacob 		nxp->lun = lun;
6500387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
6501387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
6502387d8239SMatt Jacob 	}
6503387d8239SMatt Jacob 	if (nxp) {
6504387d8239SMatt Jacob 		if (nxp->crnseed == 0)
6505387d8239SMatt Jacob 			nxp->crnseed = 1;
6506387d8239SMatt Jacob 		if (cmd)
6507387d8239SMatt Jacob 			PISP_PCMD(cmd)->crn = nxp->crnseed;
6508387d8239SMatt Jacob 		*crnp = nxp->crnseed++;
6509387d8239SMatt Jacob 		return (0);
6510387d8239SMatt Jacob 	}
6511387d8239SMatt Jacob 	return (-1);
6512387d8239SMatt Jacob }
6513387d8239SMatt Jacob 
651494dff771SMatt Jacob /*
651594dff771SMatt Jacob  * We enter with the lock held
651694dff771SMatt Jacob  */
65172df76c16SMatt Jacob void
65182df76c16SMatt Jacob isp_timer(void *arg)
65192df76c16SMatt Jacob {
65202df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
65212df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
65222df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
65232df76c16SMatt Jacob #endif
652494dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
65252df76c16SMatt Jacob }
6526387d8239SMatt Jacob 
6527387d8239SMatt Jacob isp_ecmd_t *
6528387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
6529387d8239SMatt Jacob {
6530387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
6531387d8239SMatt Jacob 	if (ecmd) {
6532387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
6533387d8239SMatt Jacob 	}
6534387d8239SMatt Jacob 	return (ecmd);
6535387d8239SMatt Jacob }
6536387d8239SMatt Jacob 
6537387d8239SMatt Jacob void
6538387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
6539387d8239SMatt Jacob {
6540387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
6541387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
6542387d8239SMatt Jacob }
6543