xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 10ed63fc)
1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
41b760be4SAlexander Motin  * Copyright (c) 2009-2020 Alexander Motin <mav@FreeBSD.org>
52df76c16SMatt Jacob  * Copyright (c) 1997-2009 by Matthew Jacob
6e5265237SMatt Jacob  * All rights reserved.
76054c3f6SMatt Jacob  *
86054c3f6SMatt Jacob  * Redistribution and use in source and binary forms, with or without
96054c3f6SMatt Jacob  * modification, are permitted provided that the following conditions
106054c3f6SMatt Jacob  * are met:
116054c3f6SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
126054c3f6SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
136054c3f6SMatt Jacob  *    this list of conditions, and the following disclaimer.
14aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
15aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
166054c3f6SMatt Jacob  *
176054c3f6SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186054c3f6SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196054c3f6SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206054c3f6SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
216054c3f6SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226054c3f6SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236054c3f6SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246054c3f6SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256054c3f6SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266054c3f6SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276054c3f6SMatt Jacob  * SUCH DAMAGE.
286054c3f6SMatt Jacob  */
29aad970f1SDavid E. O'Brien 
30799881e0SMatt Jacob /*
31799881e0SMatt Jacob  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
32799881e0SMatt Jacob  */
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
346054c3f6SMatt Jacob #include <dev/isp/isp_freebsd.h>
355d571944SMatt Jacob #include <sys/unistd.h>
365d571944SMatt Jacob #include <sys/kthread.h>
375d571944SMatt Jacob #include <sys/conf.h>
384eb49427SMatt Jacob #include <sys/module.h>
395d571944SMatt Jacob #include <sys/ioccom.h>
405d571944SMatt Jacob #include <dev/isp/isp_ioctl.h>
4170273f90SMatt Jacob #include <sys/devicestat.h>
42f7c631bcSMatt Jacob #include <cam/cam_periph.h>
436c81a0aeSMatt Jacob #include <cam/cam_xpt_periph.h>
446054c3f6SMatt Jacob 
454eb49427SMatt Jacob MODULE_VERSION(isp, 1);
4656c5b842SMark Murray MODULE_DEPEND(isp, cam, 1, 1, 1);
4773030e03SMatt Jacob int isp_announced = 0;
482df76c16SMatt Jacob int isp_loop_down_limit = 60;	/* default loop down limit */
492df76c16SMatt Jacob int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
50f7c631bcSMatt Jacob int isp_gone_device_time = 30;	/* grace time before reporting device lost */
51e68eef14SAlexander Motin static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
5273030e03SMatt Jacob 
53e561aa79SAlexander Motin static void isp_freeze_loopdown(ispsoftc_t *, int);
54e561aa79SAlexander Motin static void isp_loop_changed(ispsoftc_t *isp, int chan);
550f99cb55SAlexander Motin static void isp_rq_check_above(ispsoftc_t *);
560f99cb55SAlexander Motin static void isp_rq_check_below(ispsoftc_t *);
575d571944SMatt Jacob static d_ioctl_t ispioctl;
580470d791SMatt Jacob static void isp_poll(struct cam_sim *);
595773ac11SJohn Baldwin static callout_func_t isp_watchdog;
605773ac11SJohn Baldwin static callout_func_t isp_gdt;
61de461933SMatt Jacob static task_fn_t isp_gdt_task;
625d571944SMatt Jacob static void isp_kthread(void *);
63d81ba9d5SMatt Jacob static void isp_action(struct cam_sim *, union ccb *);
6494dff771SMatt Jacob static int isp_timer_count;
652df76c16SMatt Jacob static void isp_timer(void *);
66cc8df88bSMatt Jacob 
675d571944SMatt Jacob static struct cdevsw isp_cdevsw = {
68dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
697ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ispioctl,
707ac40f5fSPoul-Henning Kamp 	.d_name =	"isp",
715d571944SMatt Jacob };
725d571944SMatt Jacob 
732df76c16SMatt Jacob static int
isp_role_sysctl(SYSCTL_HANDLER_ARGS)7407f56f1cSAlexander Motin isp_role_sysctl(SYSCTL_HANDLER_ARGS)
7507f56f1cSAlexander Motin {
7607f56f1cSAlexander Motin 	ispsoftc_t *isp = (ispsoftc_t *)arg1;
7707f56f1cSAlexander Motin 	int chan = arg2;
7807f56f1cSAlexander Motin 	int error, old, value;
7907f56f1cSAlexander Motin 
8007f56f1cSAlexander Motin 	value = FCPARAM(isp, chan)->role;
8107f56f1cSAlexander Motin 
8207f56f1cSAlexander Motin 	error = sysctl_handle_int(oidp, &value, 0, req);
8307f56f1cSAlexander Motin 	if ((error != 0) || (req->newptr == NULL))
8407f56f1cSAlexander Motin 		return (error);
8507f56f1cSAlexander Motin 
8607f56f1cSAlexander Motin 	if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
8707f56f1cSAlexander Motin 		return (EINVAL);
8807f56f1cSAlexander Motin 
8907f56f1cSAlexander Motin 	ISP_LOCK(isp);
9007f56f1cSAlexander Motin 	old = FCPARAM(isp, chan)->role;
9107f56f1cSAlexander Motin 
92391f03daSAlexander Motin 	/* We don't allow target mode switch from here. */
93391f03daSAlexander Motin 	value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
94391f03daSAlexander Motin 
9507f56f1cSAlexander Motin 	/* If nothing has changed -- we are done. */
9607f56f1cSAlexander Motin 	if (value == old) {
9707f56f1cSAlexander Motin 		ISP_UNLOCK(isp);
9807f56f1cSAlexander Motin 		return (0);
9907f56f1cSAlexander Motin 	}
10007f56f1cSAlexander Motin 
10107f56f1cSAlexander Motin 	/* Actually change the role. */
1026bef0aa0SAlexander Motin 	error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
10307f56f1cSAlexander Motin 	ISP_UNLOCK(isp);
10407f56f1cSAlexander Motin 	return (error);
10507f56f1cSAlexander Motin }
10607f56f1cSAlexander Motin 
10707f56f1cSAlexander Motin static int
isp_attach_chan(ispsoftc_t * isp,struct cam_devq * devq,int chan)1082df76c16SMatt Jacob isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
109478f8a96SJustin T. Gibbs {
110a6222dd7SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
111a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
112a6222dd7SAlexander Motin 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
113a6222dd7SAlexander Motin 	struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
114a6222dd7SAlexander Motin 	char name[16];
115ea6f23cdSMatt Jacob 	struct cam_sim *sim;
116ea6f23cdSMatt Jacob 	struct cam_path *path;
1178290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
1188290ea90SAlexander Motin 	int i;
1198290ea90SAlexander Motin #endif
120478f8a96SJustin T. Gibbs 
12113d9c921SAlexander Motin 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
12213d9c921SAlexander Motin 	    device_get_unit(isp->isp_dev), &isp->isp_lock,
12313d9c921SAlexander Motin 	    isp->isp_maxcmds, isp->isp_maxcmds, devq);
12413d9c921SAlexander Motin 	if (sim == NULL)
1252df76c16SMatt Jacob 		return (ENOMEM);
1262df76c16SMatt Jacob 
1272df76c16SMatt Jacob 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
1282df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1292df76c16SMatt Jacob 		return (EIO);
1302df76c16SMatt Jacob 	}
131227d67aaSAlexander Motin 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1322df76c16SMatt Jacob 		xpt_bus_deregister(cam_sim_path(sim));
1332df76c16SMatt Jacob 		cam_sim_free(sim, FALSE);
1342df76c16SMatt Jacob 		return (ENXIO);
1352df76c16SMatt Jacob 	}
136387d8239SMatt Jacob 
137a01f5aebSMatt Jacob 	ISP_LOCK(isp);
1382df76c16SMatt Jacob 	fc->sim = sim;
1392df76c16SMatt Jacob 	fc->path = path;
1402df76c16SMatt Jacob 	fc->isp = isp;
141a01f5aebSMatt Jacob 	fc->ready = 1;
142a94fab67SAlexander Motin 	fcp->isp_use_gft_id = 1;
143a94fab67SAlexander Motin 	fcp->isp_use_gff_id = 1;
144de461933SMatt Jacob 
14513d9c921SAlexander Motin 	callout_init_mtx(&fc->gdt, &isp->isp_lock, 0);
146de461933SMatt Jacob 	TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
1470d965629SAlexander Motin #ifdef	ISP_TARGET_MODE
14809ddc7adSAlexander Motin 	TAILQ_INIT(&fc->waitq);
1498290ea90SAlexander Motin 	STAILQ_INIT(&fc->ntfree);
1508290ea90SAlexander Motin 	for (i = 0; i < ATPDPSIZE; i++)
1518290ea90SAlexander Motin 		STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
1528290ea90SAlexander Motin 	LIST_INIT(&fc->atfree);
1538290ea90SAlexander Motin 	for (i = ATPDPSIZE-1; i >= 0; i--)
1548290ea90SAlexander Motin 		LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
1558290ea90SAlexander Motin 	for (i = 0; i < ATPDPHASHSIZE; i++)
1568290ea90SAlexander Motin 		LIST_INIT(&fc->atused[i]);
1570d965629SAlexander Motin #endif
158e561aa79SAlexander Motin 	isp_loop_changed(isp, chan);
1592df76c16SMatt Jacob 	ISP_UNLOCK(isp);
160069f5ef9SAlexander Motin 	if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
161069f5ef9SAlexander Motin 	    "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
162a01f5aebSMatt Jacob 		xpt_free_path(fc->path);
163a01f5aebSMatt Jacob 		xpt_bus_deregister(cam_sim_path(fc->sim));
164a01f5aebSMatt Jacob 		cam_sim_free(fc->sim, FALSE);
165a01f5aebSMatt Jacob 		return (ENOMEM);
166a01f5aebSMatt Jacob 	}
167e596ff7aSAlexander Motin 	fc->num_threads += 1;
16807f56f1cSAlexander Motin 	if (chan > 0) {
16907f56f1cSAlexander Motin 		snprintf(name, sizeof(name), "chan%d", chan);
17007f56f1cSAlexander Motin 		tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
1717029da5cSPawel Biernacki 		    OID_AUTO, name, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1727029da5cSPawel Biernacki 		    "Virtual channel");
173a0ec8e99SMatt Jacob 	}
174e596ff7aSAlexander Motin 	SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
175e596ff7aSAlexander Motin 	    "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
176e596ff7aSAlexander Motin 	    "World Wide Node Name");
177e596ff7aSAlexander Motin 	SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
178e596ff7aSAlexander Motin 	    "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
179e596ff7aSAlexander Motin 	    "World Wide Port Name");
180e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
181e596ff7aSAlexander Motin 	    "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
182e596ff7aSAlexander Motin 	    "Loop Down Limit");
183e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
184e596ff7aSAlexander Motin 	    "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
185e596ff7aSAlexander Motin 	    "Gone Device Time");
18607f56f1cSAlexander Motin #if defined(ISP_TARGET_MODE) && defined(DEBUG)
187e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
188e596ff7aSAlexander Motin 	    "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
189e596ff7aSAlexander Motin 	    "Cause a Lost Frame on a Read");
19007f56f1cSAlexander Motin #endif
19107f56f1cSAlexander Motin 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
192b776de67SAlexander Motin 	    "role", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1937029da5cSPawel Biernacki 	    isp, chan, isp_role_sysctl, "I", "Current role");
194e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
195e596ff7aSAlexander Motin 	    "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
196e596ff7aSAlexander Motin 	    "Connection speed in gigabits");
197e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
198e596ff7aSAlexander Motin 	    "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
199e596ff7aSAlexander Motin 	    "Link state");
200e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
201e596ff7aSAlexander Motin 	    "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
202e596ff7aSAlexander Motin 	    "Firmware state");
203e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
204e596ff7aSAlexander Motin 	    "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
205e596ff7aSAlexander Motin 	    "Loop state");
206e596ff7aSAlexander Motin 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
207e596ff7aSAlexander Motin 	    "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
208e596ff7aSAlexander Motin 	    "Connection topology");
209a94fab67SAlexander Motin 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210a94fab67SAlexander Motin 	    "use_gft_id", CTLFLAG_RWTUN, &fcp->isp_use_gft_id, 0,
211a94fab67SAlexander Motin 	    "Use GFT_ID during fabric scan");
212a94fab67SAlexander Motin 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213a94fab67SAlexander Motin 	    "use_gff_id", CTLFLAG_RWTUN, &fcp->isp_use_gff_id, 0,
214a94fab67SAlexander Motin 	    "Use GFF_ID during fabric scan");
21510ed63fcSJoerg Pulz 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
21610ed63fcSJoerg Pulz 	    "fw_version_flash", CTLFLAG_RD, fcp->fw_version_flash, 0,
21710ed63fcSJoerg Pulz 	    "Firmware version in (active) flash region");
21810ed63fcSJoerg Pulz 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
21910ed63fcSJoerg Pulz 	    "fw_version_ispfw", CTLFLAG_RD, fcp->fw_version_ispfw, 0,
22010ed63fcSJoerg Pulz 	    "Firmware version loaded from ispfw(4)");
22110ed63fcSJoerg Pulz 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
22210ed63fcSJoerg Pulz 	    "fw_version_run", CTLFLAG_RD, fcp->fw_version_run, 0,
22310ed63fcSJoerg Pulz 	    "Firmware version currently running");
2242df76c16SMatt Jacob 	return (0);
2252df76c16SMatt Jacob }
2262df76c16SMatt Jacob 
2271d0a1de2SWill Andrews static void
isp_detach_chan(ispsoftc_t * isp,int chan)2281d0a1de2SWill Andrews isp_detach_chan(ispsoftc_t *isp, int chan)
2291d0a1de2SWill Andrews {
230a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2311d0a1de2SWill Andrews 
232a6222dd7SAlexander Motin 	xpt_free_path(fc->path);
233a6222dd7SAlexander Motin 	xpt_bus_deregister(cam_sim_path(fc->sim));
234a6222dd7SAlexander Motin 	cam_sim_free(fc->sim, FALSE);
2351d0a1de2SWill Andrews 
2361d0a1de2SWill Andrews 	/* Wait for the channel's spawned threads to exit. */
237a6222dd7SAlexander Motin 	wakeup(fc);
238a6222dd7SAlexander Motin 	while (fc->num_threads != 0)
239a6222dd7SAlexander Motin 		mtx_sleep(&fc->num_threads, &isp->isp_lock, PRIBIO, "isp_reap", 0);
2401d0a1de2SWill Andrews }
2411d0a1de2SWill Andrews 
2422df76c16SMatt Jacob int
isp_attach(ispsoftc_t * isp)2432df76c16SMatt Jacob isp_attach(ispsoftc_t *isp)
2442df76c16SMatt Jacob {
2452df76c16SMatt Jacob 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
2462df76c16SMatt Jacob 	int du = device_get_unit(isp->isp_dev);
2472df76c16SMatt Jacob 	int chan;
2482df76c16SMatt Jacob 
249ea6f23cdSMatt Jacob 	/*
250ea6f23cdSMatt Jacob 	 * Create the device queue for our SIM(s).
251478f8a96SJustin T. Gibbs 	 */
2522df76c16SMatt Jacob 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
2532df76c16SMatt Jacob 	if (isp->isp_osinfo.devq == NULL) {
2542df76c16SMatt Jacob 		return (EIO);
255478f8a96SJustin T. Gibbs 	}
256478f8a96SJustin T. Gibbs 
2572df76c16SMatt Jacob 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2582df76c16SMatt Jacob 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
2592df76c16SMatt Jacob 			goto unwind;
260ea6f23cdSMatt Jacob 		}
261ea6f23cdSMatt Jacob 	}
262ea6f23cdSMatt Jacob 
26313d9c921SAlexander Motin 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0);
26494dff771SMatt Jacob 	isp_timer_count = hz >> 2;
26594dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
266ea6f23cdSMatt Jacob 
2672df76c16SMatt Jacob 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
2682df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2692df76c16SMatt Jacob 		isp->isp_osinfo.cdev->si_drv1 = isp;
270ea6f23cdSMatt Jacob 	}
2712df76c16SMatt Jacob 	return (0);
2725d571944SMatt Jacob 
2732df76c16SMatt Jacob unwind:
274a6222dd7SAlexander Motin 	ISP_LOCK(isp);
275a6222dd7SAlexander Motin 	isp->isp_osinfo.is_exiting = 1;
276a6222dd7SAlexander Motin 	while (--chan >= 0)
277a6222dd7SAlexander Motin 		isp_detach_chan(isp, chan);
278a6222dd7SAlexander Motin 	ISP_UNLOCK(isp);
2792df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
2802df76c16SMatt Jacob 	isp->isp_osinfo.devq = NULL;
2812df76c16SMatt Jacob 	return (-1);
2822df76c16SMatt Jacob }
2832df76c16SMatt Jacob 
284e95725cbSMatt Jacob int
isp_detach(ispsoftc_t * isp)2852df76c16SMatt Jacob isp_detach(ispsoftc_t *isp)
2862df76c16SMatt Jacob {
2872df76c16SMatt Jacob 	int chan;
2882df76c16SMatt Jacob 
2892df76c16SMatt Jacob 	if (isp->isp_osinfo.cdev) {
2902df76c16SMatt Jacob 		destroy_dev(isp->isp_osinfo.cdev);
2912df76c16SMatt Jacob 		isp->isp_osinfo.cdev = NULL;
2922df76c16SMatt Jacob 	}
29398339da1SAlexander Motin 	ISP_LOCK(isp);
29498339da1SAlexander Motin 	/* Tell spawned threads that we're exiting. */
29598339da1SAlexander Motin 	isp->isp_osinfo.is_exiting = 1;
29698339da1SAlexander Motin 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
29798339da1SAlexander Motin 		isp_detach_chan(isp, chan);
29898339da1SAlexander Motin 	ISP_UNLOCK(isp);
29998339da1SAlexander Motin 	callout_drain(&isp->isp_osinfo.tmo);
3002df76c16SMatt Jacob 	cam_simq_free(isp->isp_osinfo.devq);
301e95725cbSMatt Jacob 	return (0);
3025d571944SMatt Jacob }
3035d571944SMatt Jacob 
304f7c631bcSMatt Jacob static void
isp_freeze_loopdown(ispsoftc_t * isp,int chan)305e561aa79SAlexander Motin isp_freeze_loopdown(ispsoftc_t *isp, int chan)
306fdeb9f2fSMatt Jacob {
3072df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
308ce9f3237SAlexander Motin 
309ce9f3237SAlexander Motin 	if (fc->sim == NULL)
310ce9f3237SAlexander Motin 		return;
3112df76c16SMatt Jacob 	if (fc->simqfrozen == 0) {
312b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
313e561aa79SAlexander Motin 		    "Chan %d Freeze simq (loopdown)", chan);
3142df76c16SMatt Jacob 		fc->simqfrozen = SIMQFRZ_LOOPDOWN;
315c07b9e07SAlexander Motin 		xpt_hold_boot();
3162df76c16SMatt Jacob 		xpt_freeze_simq(fc->sim, 1);
317fdeb9f2fSMatt Jacob 	} else {
318b5d5037bSAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
319e561aa79SAlexander Motin 		    "Chan %d Mark simq frozen (loopdown)", chan);
3202df76c16SMatt Jacob 		fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
3212df76c16SMatt Jacob 	}
322fdeb9f2fSMatt Jacob }
323fdeb9f2fSMatt Jacob 
324e95725cbSMatt Jacob static void
isp_unfreeze_loopdown(ispsoftc_t * isp,int chan)325e95725cbSMatt Jacob isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
326e95725cbSMatt Jacob {
327e95725cbSMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
328ce9f3237SAlexander Motin 
329ce9f3237SAlexander Motin 	if (fc->sim == NULL)
330ce9f3237SAlexander Motin 		return;
331e95725cbSMatt Jacob 	int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
332e95725cbSMatt Jacob 	fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
333e95725cbSMatt Jacob 	if (wasfrozen && fc->simqfrozen == 0) {
334e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
335e561aa79SAlexander Motin 		    "Chan %d Release simq", chan);
336e95725cbSMatt Jacob 		xpt_release_simq(fc->sim, 1);
337c07b9e07SAlexander Motin 		xpt_release_boot();
338e95725cbSMatt Jacob 	}
339e95725cbSMatt Jacob }
3409cd7268eSMatt Jacob 
3410f99cb55SAlexander Motin /*
3420f99cb55SAlexander Motin  * Functions to protect from request queue overflow by freezing SIM queue.
3430f99cb55SAlexander Motin  * XXX: freezing only one arbitrary SIM, since they all share the queue.
3440f99cb55SAlexander Motin  */
3450f99cb55SAlexander Motin static void
isp_rq_check_above(ispsoftc_t * isp)3460f99cb55SAlexander Motin isp_rq_check_above(ispsoftc_t *isp)
3470f99cb55SAlexander Motin {
3480f99cb55SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, 0);
3490f99cb55SAlexander Motin 
3500f99cb55SAlexander Motin 	if (isp->isp_rqovf || fc->sim == NULL)
3510f99cb55SAlexander Motin 		return;
3520f99cb55SAlexander Motin 	if (!isp_rqentry_avail(isp, QENTRY_MAX)) {
3530f99cb55SAlexander Motin 		xpt_freeze_simq(fc->sim, 1);
3540f99cb55SAlexander Motin 		isp->isp_rqovf = 1;
3550f99cb55SAlexander Motin 	}
3560f99cb55SAlexander Motin }
3570f99cb55SAlexander Motin 
3580f99cb55SAlexander Motin static void
isp_rq_check_below(ispsoftc_t * isp)3590f99cb55SAlexander Motin isp_rq_check_below(ispsoftc_t *isp)
3600f99cb55SAlexander Motin {
3610f99cb55SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, 0);
3620f99cb55SAlexander Motin 
3630f99cb55SAlexander Motin 	if (!isp->isp_rqovf || fc->sim == NULL)
3640f99cb55SAlexander Motin 		return;
3650f99cb55SAlexander Motin 	if (isp_rqentry_avail(isp, QENTRY_MAX)) {
3660f99cb55SAlexander Motin 		xpt_release_simq(fc->sim, 0);
3670f99cb55SAlexander Motin 		isp->isp_rqovf = 0;
3680f99cb55SAlexander Motin 	}
3690f99cb55SAlexander Motin }
3700f99cb55SAlexander Motin 
3715d571944SMatt Jacob static int
ispioctl(struct cdev * dev,u_long c,caddr_t addr,int flags,struct thread * td)3722df76c16SMatt Jacob ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
3735d571944SMatt Jacob {
3749cd7268eSMatt Jacob 	ispsoftc_t *isp;
3752df76c16SMatt Jacob 	int nr, chan, retval = ENOTTY;
3765d571944SMatt Jacob 
3772df76c16SMatt Jacob 	isp = dev->si_drv1;
3785d571944SMatt Jacob 
379c1504bc0SMatt Jacob 	switch (c) {
3805d571944SMatt Jacob 	case ISP_SDBLEV:
3815d571944SMatt Jacob 	{
3825d571944SMatt Jacob 		int olddblev = isp->isp_dblev;
3835d571944SMatt Jacob 		isp->isp_dblev = *(int *)addr;
3845d571944SMatt Jacob 		*(int *)addr = olddblev;
3855d571944SMatt Jacob 		retval = 0;
3865d571944SMatt Jacob 		break;
3875d571944SMatt Jacob 	}
388746e9c85SMatt Jacob 	case ISP_GETROLE:
3892df76c16SMatt Jacob 		chan = *(int *)addr;
3902df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
3912df76c16SMatt Jacob 			retval = -ENXIO;
3922df76c16SMatt Jacob 			break;
3932df76c16SMatt Jacob 		}
3942df76c16SMatt Jacob 		*(int *)addr = FCPARAM(isp, chan)->role;
395746e9c85SMatt Jacob 		retval = 0;
396746e9c85SMatt Jacob 		break;
397746e9c85SMatt Jacob 	case ISP_SETROLE:
398746e9c85SMatt Jacob 		nr = *(int *)addr;
3992df76c16SMatt Jacob 		chan = nr >> 8;
4002df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4012df76c16SMatt Jacob 			retval = -ENXIO;
4022df76c16SMatt Jacob 			break;
4032df76c16SMatt Jacob 		}
4042df76c16SMatt Jacob 		nr &= 0xff;
405746e9c85SMatt Jacob 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
406746e9c85SMatt Jacob 			retval = EINVAL;
407746e9c85SMatt Jacob 			break;
408746e9c85SMatt Jacob 		}
4092df76c16SMatt Jacob 		ISP_LOCK(isp);
4106bef0aa0SAlexander Motin 		*(int *)addr = FCPARAM(isp, chan)->role;
4116bef0aa0SAlexander Motin 		retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
4126bef0aa0SAlexander Motin 		ISP_UNLOCK(isp);
4132df76c16SMatt Jacob 		break;
4142df76c16SMatt Jacob 
4155d571944SMatt Jacob 	case ISP_RESETHBA:
416511ced9bSMatt Jacob 		ISP_LOCK(isp);
4172df76c16SMatt Jacob 		isp_reinit(isp, 0);
418511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4195d571944SMatt Jacob 		retval = 0;
4205d571944SMatt Jacob 		break;
4212df76c16SMatt Jacob 
422f553351eSMatt Jacob 	case ISP_RESCAN:
4238037cdcdSBrooks Davis 		chan = *(intptr_t *)addr;
4242df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4252df76c16SMatt Jacob 			retval = -ENXIO;
4262df76c16SMatt Jacob 			break;
4272df76c16SMatt Jacob 		}
428511ced9bSMatt Jacob 		ISP_LOCK(isp);
429e561aa79SAlexander Motin 		if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
4305d571944SMatt Jacob 			retval = EIO;
4315d571944SMatt Jacob 		} else {
4325d571944SMatt Jacob 			retval = 0;
4335d571944SMatt Jacob 		}
434511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4355d571944SMatt Jacob 		break;
4362df76c16SMatt Jacob 
4375d571944SMatt Jacob 	case ISP_FC_LIP:
4388037cdcdSBrooks Davis 		chan = *(intptr_t *)addr;
4392df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4402df76c16SMatt Jacob 			retval = -ENXIO;
4412df76c16SMatt Jacob 			break;
4422df76c16SMatt Jacob 		}
443511ced9bSMatt Jacob 		ISP_LOCK(isp);
4442df76c16SMatt Jacob 		if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
4455d571944SMatt Jacob 			retval = EIO;
4465d571944SMatt Jacob 		} else {
4475d571944SMatt Jacob 			retval = 0;
4485d571944SMatt Jacob 		}
449511ced9bSMatt Jacob 		ISP_UNLOCK(isp);
4505d571944SMatt Jacob 		break;
4515d571944SMatt Jacob 	case ISP_FC_GETDINFO:
4525d571944SMatt Jacob 	{
4535d571944SMatt Jacob 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
45410365e5aSMatt Jacob 		fcportdb_t *lp;
4555d571944SMatt Jacob 
4562e4637cdSMatt Jacob 		if (ifc->loopid >= MAX_FC_TARG) {
4575d571944SMatt Jacob 			retval = EINVAL;
4585d571944SMatt Jacob 			break;
4595d571944SMatt Jacob 		}
4602df76c16SMatt Jacob 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
461e68eef14SAlexander Motin 		if (lp->state != FC_PORTDB_STATE_NIL) {
462387d8239SMatt Jacob 			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
46310365e5aSMatt Jacob 			ifc->loopid = lp->handle;
4645d571944SMatt Jacob 			ifc->portid = lp->portid;
4655d571944SMatt Jacob 			ifc->node_wwn = lp->node_wwn;
4665d571944SMatt Jacob 			ifc->port_wwn = lp->port_wwn;
4675d571944SMatt Jacob 			retval = 0;
4685d571944SMatt Jacob 		} else {
4695d571944SMatt Jacob 			retval = ENODEV;
4705d571944SMatt Jacob 		}
4715d571944SMatt Jacob 		break;
4725d571944SMatt Jacob 	}
473570c7a3fSMatt Jacob 	case ISP_FC_GETHINFO:
474570c7a3fSMatt Jacob 	{
475570c7a3fSMatt Jacob 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
4762df76c16SMatt Jacob 		int chan = hba->fc_channel;
47702e2b2d9SMatt Jacob 
4782df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
4792df76c16SMatt Jacob 			retval = ENXIO;
4802df76c16SMatt Jacob 			break;
4812df76c16SMatt Jacob 		}
482a556b68eSMatt Jacob 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
483a556b68eSMatt Jacob 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
484a556b68eSMatt Jacob 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
4852df76c16SMatt Jacob 		hba->fc_nchannels = isp->isp_nchan;
4862df76c16SMatt Jacob 		hba->fc_nports = MAX_FC_TARG;
4872df76c16SMatt Jacob 		hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
4882df76c16SMatt Jacob 		hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
4892df76c16SMatt Jacob 		hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
4902df76c16SMatt Jacob 		hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
4912df76c16SMatt Jacob 		hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
4922df76c16SMatt Jacob 		hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
4932df76c16SMatt Jacob 		hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
494570c7a3fSMatt Jacob 		retval = 0;
495570c7a3fSMatt Jacob 		break;
496570c7a3fSMatt Jacob 	}
4978e62a8acSMatt Jacob 	case ISP_TSK_MGMT:
4988e62a8acSMatt Jacob 	{
4998e62a8acSMatt Jacob 		int needmarker;
5008e62a8acSMatt Jacob 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
501c5fd36edSAlexander Motin 		uint16_t nphdl;
5021b760be4SAlexander Motin 		isp24xx_tmf_t tmf;
5031b760be4SAlexander Motin 		isp24xx_statusreq_t sp;
5041b760be4SAlexander Motin 		fcparam *fcp;
5051b760be4SAlexander Motin 		fcportdb_t *lp;
5061b760be4SAlexander Motin 		int i;
5078e62a8acSMatt Jacob 
5082df76c16SMatt Jacob 		chan = fct->chan;
5092df76c16SMatt Jacob 		if (chan < 0 || chan >= isp->isp_nchan) {
5102df76c16SMatt Jacob 			retval = -ENXIO;
5112df76c16SMatt Jacob 			break;
5122df76c16SMatt Jacob 		}
5132df76c16SMatt Jacob 
5148e62a8acSMatt Jacob 		needmarker = retval = 0;
515c5fd36edSAlexander Motin 		nphdl = fct->loopid;
5162df76c16SMatt Jacob 		ISP_LOCK(isp);
5171b760be4SAlexander Motin 		fcp = FCPARAM(isp, chan);
5182df76c16SMatt Jacob 
5192df76c16SMatt Jacob 		for (i = 0; i < MAX_FC_TARG; i++) {
5202df76c16SMatt Jacob 			lp = &fcp->portdb[i];
521c5fd36edSAlexander Motin 			if (lp->handle == nphdl) {
5222df76c16SMatt Jacob 				break;
5232df76c16SMatt Jacob 			}
5242df76c16SMatt Jacob 		}
5252df76c16SMatt Jacob 		if (i == MAX_FC_TARG) {
5262df76c16SMatt Jacob 			retval = ENXIO;
5272df76c16SMatt Jacob 			ISP_UNLOCK(isp);
5282df76c16SMatt Jacob 			break;
5292df76c16SMatt Jacob 		}
53053791a95SAlexander Motin 		ISP_MEMZERO(&tmf, sizeof(tmf));
53153791a95SAlexander Motin 		tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
53253791a95SAlexander Motin 		tmf.tmf_header.rqs_entry_count = 1;
53353791a95SAlexander Motin 		tmf.tmf_nphdl = lp->handle;
53453791a95SAlexander Motin 		tmf.tmf_delay = 2;
53553791a95SAlexander Motin 		tmf.tmf_timeout = 4;
53653791a95SAlexander Motin 		tmf.tmf_tidlo = lp->portid;
53753791a95SAlexander Motin 		tmf.tmf_tidhi = lp->portid >> 16;
53853791a95SAlexander Motin 		tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
53953791a95SAlexander Motin 		tmf.tmf_lun[1] = fct->lun & 0xff;
5402df76c16SMatt Jacob 		if (fct->lun >= 256) {
54153791a95SAlexander Motin 			tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
5422df76c16SMatt Jacob 		}
5432df76c16SMatt Jacob 		switch (fct->action) {
5442df76c16SMatt Jacob 		case IPT_CLEAR_ACA:
54553791a95SAlexander Motin 			tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
5462df76c16SMatt Jacob 			break;
5472df76c16SMatt Jacob 		case IPT_TARGET_RESET:
54853791a95SAlexander Motin 			tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
5492df76c16SMatt Jacob 			needmarker = 1;
5502df76c16SMatt Jacob 			break;
5512df76c16SMatt Jacob 		case IPT_LUN_RESET:
55253791a95SAlexander Motin 			tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
5532df76c16SMatt Jacob 			needmarker = 1;
5542df76c16SMatt Jacob 			break;
5552df76c16SMatt Jacob 		case IPT_CLEAR_TASK_SET:
55653791a95SAlexander Motin 			tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
5572df76c16SMatt Jacob 			needmarker = 1;
5582df76c16SMatt Jacob 			break;
5592df76c16SMatt Jacob 		case IPT_ABORT_TASK_SET:
56053791a95SAlexander Motin 			tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
5612df76c16SMatt Jacob 			needmarker = 1;
5622df76c16SMatt Jacob 			break;
5632df76c16SMatt Jacob 		default:
5642df76c16SMatt Jacob 			retval = EINVAL;
5652df76c16SMatt Jacob 			break;
5662df76c16SMatt Jacob 		}
5672df76c16SMatt Jacob 		if (retval) {
5682df76c16SMatt Jacob 			ISP_UNLOCK(isp);
5692df76c16SMatt Jacob 			break;
5702df76c16SMatt Jacob 		}
5712df76c16SMatt Jacob 
572156c1ebeSAlexander Motin 		retval = isp_exec_entry_queue(isp, &tmf, &sp, 5);
573156c1ebeSAlexander Motin 		if (retval != 0) {
574156c1ebeSAlexander Motin 			isp_prt(isp, ISP_LOGERR, "%s: TMF of chan %d error %d",
575156c1ebeSAlexander Motin 			    __func__, chan, retval);
5762df76c16SMatt Jacob 			ISP_UNLOCK(isp);
5772df76c16SMatt Jacob 			break;
5782df76c16SMatt Jacob 		}
57953791a95SAlexander Motin 
58053791a95SAlexander Motin 		if (sp.req_completion_status != 0)
58153791a95SAlexander Motin 			retval = EIO;
58253791a95SAlexander Motin 		else if (needmarker)
5832df76c16SMatt Jacob 			fcp->sendmarker = 1;
5842df76c16SMatt Jacob 		ISP_UNLOCK(isp);
5858e62a8acSMatt Jacob 		break;
5868e62a8acSMatt Jacob 	}
5875d571944SMatt Jacob 	default:
5885d571944SMatt Jacob 		break;
5895d571944SMatt Jacob 	}
5905d571944SMatt Jacob 	return (retval);
5910470d791SMatt Jacob }
592478f8a96SJustin T. Gibbs 
593d81ba9d5SMatt Jacob /*
5942df76c16SMatt Jacob  * Local Inlines
5952df76c16SMatt Jacob  */
5962df76c16SMatt Jacob 
5972df76c16SMatt Jacob static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
5982df76c16SMatt Jacob static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
5992df76c16SMatt Jacob 
6002df76c16SMatt Jacob static ISP_INLINE int
isp_get_pcmd(ispsoftc_t * isp,union ccb * ccb)6012df76c16SMatt Jacob isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
6022df76c16SMatt Jacob {
6032df76c16SMatt Jacob 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
6042df76c16SMatt Jacob 	if (ISP_PCMD(ccb) == NULL) {
6052df76c16SMatt Jacob 		return (-1);
6062df76c16SMatt Jacob 	}
6072df76c16SMatt Jacob 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
6082df76c16SMatt Jacob 	return (0);
6092df76c16SMatt Jacob }
6102df76c16SMatt Jacob 
6112df76c16SMatt Jacob static ISP_INLINE void
isp_free_pcmd(ispsoftc_t * isp,union ccb * ccb)6122df76c16SMatt Jacob isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
6132df76c16SMatt Jacob {
614387d8239SMatt Jacob 	if (ISP_PCMD(ccb)) {
615405b7a29SMatt Jacob #ifdef	ISP_TARGET_MODE
616405b7a29SMatt Jacob 		PISP_PCMD(ccb)->datalen = 0;
617405b7a29SMatt Jacob #endif
618405b7a29SMatt Jacob 		PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
6192df76c16SMatt Jacob 		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
6202df76c16SMatt Jacob 		ISP_PCMD(ccb) = NULL;
6212df76c16SMatt Jacob 	}
622387d8239SMatt Jacob }
623387d8239SMatt Jacob 
6242df76c16SMatt Jacob /*
625d81ba9d5SMatt Jacob  * Put the target mode functions here, because some are inlines
626d81ba9d5SMatt Jacob  */
627d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
6282df76c16SMatt Jacob static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
6298290ea90SAlexander Motin static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
6308290ea90SAlexander Motin static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
6318290ea90SAlexander Motin static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
6328290ea90SAlexander Motin static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
6338290ea90SAlexander Motin static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
6348290ea90SAlexander Motin static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
635a6222dd7SAlexander Motin static tstate_t *create_lun_state(ispsoftc_t *, int, struct cam_path *);
6368290ea90SAlexander Motin static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
6372df76c16SMatt Jacob static void isp_enable_lun(ispsoftc_t *, union ccb *);
6382df76c16SMatt Jacob static void isp_disable_lun(ispsoftc_t *, union ccb *);
6395773ac11SJohn Baldwin static callout_func_t isp_refire_notify_ack;
6400f99cb55SAlexander Motin static void isp_complete_ctio(ispsoftc_t *isp, union ccb *);
64194dff771SMatt Jacob enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
642387d8239SMatt Jacob static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
6432df76c16SMatt Jacob static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
6441b760be4SAlexander Motin static void isp_handle_platform_ctio(ispsoftc_t *, ct7_entry_t *);
64596b5475bSAlexander Motin static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
6462df76c16SMatt Jacob static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
6478290ea90SAlexander Motin static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
648d81ba9d5SMatt Jacob 
6492df76c16SMatt Jacob static ISP_INLINE tstate_t *
get_lun_statep(ispsoftc_t * isp,int bus,lun_id_t lun)6509cd7268eSMatt Jacob get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
651d81ba9d5SMatt Jacob {
652a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, bus);
653a6222dd7SAlexander Motin 	tstate_t *tptr;
654d81ba9d5SMatt Jacob 
655a6222dd7SAlexander Motin 	SLIST_FOREACH(tptr, &fc->lun_hash[LUN_HASH_FUNC(lun)], next) {
6568290ea90SAlexander Motin 		if (tptr->ts_lun == lun)
657d81ba9d5SMatt Jacob 			return (tptr);
658d81ba9d5SMatt Jacob 	}
6592df76c16SMatt Jacob 	return (NULL);
6602df76c16SMatt Jacob }
661d81ba9d5SMatt Jacob 
6628290ea90SAlexander Motin static int
isp_atio_restart(ispsoftc_t * isp,int bus,tstate_t * tptr)6638290ea90SAlexander Motin isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
6642df76c16SMatt Jacob {
6652df76c16SMatt Jacob 	inot_private_data_t *ntp;
6668290ea90SAlexander Motin 	struct ntpdlist rq;
6672df76c16SMatt Jacob 
6688290ea90SAlexander Motin 	if (STAILQ_EMPTY(&tptr->restart_queue))
6698290ea90SAlexander Motin 		return (0);
6708290ea90SAlexander Motin 	STAILQ_INIT(&rq);
6718290ea90SAlexander Motin 	STAILQ_CONCAT(&rq, &tptr->restart_queue);
6728290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
6738290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&rq, next);
6748290ea90SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0,
6758290ea90SAlexander Motin 		    "%s: restarting resrc deprived %x", __func__,
6768290ea90SAlexander Motin 		    ((at7_entry_t *)ntp->data)->at_rxid);
6778290ea90SAlexander Motin 		isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
6788290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
6798290ea90SAlexander Motin 		if (!STAILQ_EMPTY(&tptr->restart_queue))
6808290ea90SAlexander Motin 			break;
6812df76c16SMatt Jacob 	}
6828290ea90SAlexander Motin 	if (!STAILQ_EMPTY(&rq)) {
6838290ea90SAlexander Motin 		STAILQ_CONCAT(&rq, &tptr->restart_queue);
6848290ea90SAlexander Motin 		STAILQ_CONCAT(&tptr->restart_queue, &rq);
6852df76c16SMatt Jacob 	}
6868290ea90SAlexander Motin 	return (!STAILQ_EMPTY(&tptr->restart_queue));
687d81ba9d5SMatt Jacob }
688d81ba9d5SMatt Jacob 
6892df76c16SMatt Jacob static void
isp_tmcmd_restart(ispsoftc_t * isp)6902df76c16SMatt Jacob isp_tmcmd_restart(ispsoftc_t *isp)
6912df76c16SMatt Jacob {
692a6222dd7SAlexander Motin 	struct isp_fc *fc;
6932df76c16SMatt Jacob 	tstate_t *tptr;
69494dff771SMatt Jacob 	union ccb *ccb;
6952df76c16SMatt Jacob 	int bus, i;
6962df76c16SMatt Jacob 
6972df76c16SMatt Jacob 	for (bus = 0; bus < isp->isp_nchan; bus++) {
698a6222dd7SAlexander Motin 		fc = ISP_FC_PC(isp, bus);
6992df76c16SMatt Jacob 		for (i = 0; i < LUN_HASH_SIZE; i++) {
700a6222dd7SAlexander Motin 			SLIST_FOREACH(tptr, &fc->lun_hash[i], next)
7018290ea90SAlexander Motin 				isp_atio_restart(isp, bus, tptr);
70209ddc7adSAlexander Motin 		}
70309ddc7adSAlexander Motin 
70494dff771SMatt Jacob 		/*
70509ddc7adSAlexander Motin 		 * We only need to do this once per channel.
70694dff771SMatt Jacob 		 */
707a6222dd7SAlexander Motin 		ccb = (union ccb *)TAILQ_FIRST(&fc->waitq);
70809ddc7adSAlexander Motin 		if (ccb != NULL) {
709a6222dd7SAlexander Motin 			TAILQ_REMOVE(&fc->waitq, &ccb->ccb_h, sim_links.tqe);
71094dff771SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_TIMER);
71194dff771SMatt Jacob 		}
7122df76c16SMatt Jacob 	}
7130f99cb55SAlexander Motin 	isp_rq_check_above(isp);
7140f99cb55SAlexander Motin 	isp_rq_check_below(isp);
7152df76c16SMatt Jacob }
7162df76c16SMatt Jacob 
7178290ea90SAlexander Motin static atio_private_data_t *
isp_get_atpd(ispsoftc_t * isp,int chan,uint32_t tag)7188290ea90SAlexander Motin isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
71953036e92SMatt Jacob {
720a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
72153036e92SMatt Jacob 	atio_private_data_t *atp;
7222df76c16SMatt Jacob 
723a6222dd7SAlexander Motin 	atp = LIST_FIRST(&fc->atfree);
7242df76c16SMatt Jacob 	if (atp) {
725523ea374SAlexander Motin 		LIST_REMOVE(atp, next);
726523ea374SAlexander Motin 		atp->tag = tag;
727a6222dd7SAlexander Motin 		LIST_INSERT_HEAD(&fc->atused[ATPDPHASH(tag)], atp, next);
7282df76c16SMatt Jacob 	}
72953036e92SMatt Jacob 	return (atp);
73053036e92SMatt Jacob }
731523ea374SAlexander Motin 
7328290ea90SAlexander Motin static atio_private_data_t *
isp_find_atpd(ispsoftc_t * isp,int chan,uint32_t tag)7338290ea90SAlexander Motin isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
734523ea374SAlexander Motin {
735a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
736523ea374SAlexander Motin 	atio_private_data_t *atp;
737523ea374SAlexander Motin 
738a6222dd7SAlexander Motin 	LIST_FOREACH(atp, &fc->atused[ATPDPHASH(tag)], next) {
739523ea374SAlexander Motin 		if (atp->tag == tag)
7402df76c16SMatt Jacob 			return (atp);
7412df76c16SMatt Jacob 	}
74253036e92SMatt Jacob 	return (NULL);
74353036e92SMatt Jacob }
74453036e92SMatt Jacob 
7458290ea90SAlexander Motin static void
isp_put_atpd(ispsoftc_t * isp,int chan,atio_private_data_t * atp)7468290ea90SAlexander Motin isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
7472df76c16SMatt Jacob {
748a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
7498290ea90SAlexander Motin 
750a6222dd7SAlexander Motin 	if (atp->ests)
751387d8239SMatt Jacob 		isp_put_ecmd(isp, atp->ests);
752523ea374SAlexander Motin 	LIST_REMOVE(atp, next);
753387d8239SMatt Jacob 	memset(atp, 0, sizeof (*atp));
754a6222dd7SAlexander Motin 	LIST_INSERT_HEAD(&fc->atfree, atp, next);
7552df76c16SMatt Jacob }
7562df76c16SMatt Jacob 
7572df76c16SMatt Jacob static void
isp_dump_atpd(ispsoftc_t * isp,int chan)7588290ea90SAlexander Motin isp_dump_atpd(ispsoftc_t *isp, int chan)
7592df76c16SMatt Jacob {
760a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
761a6222dd7SAlexander Motin 	atio_private_data_t *atp;
7622df76c16SMatt Jacob 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
7632df76c16SMatt Jacob 
764a6222dd7SAlexander Motin 	for (atp = fc->atpool; atp < &fc->atpool[ATPDPSIZE]; atp++) {
765cfbeb41cSAlexander Motin 		if (atp->state == ATPD_STATE_FREE)
766cfbeb41cSAlexander Motin 			continue;
767cfbeb41cSAlexander Motin 		isp_prt(isp, ISP_LOGALL, "Chan %d ATP [0x%x] origdlen %u bytes_xfrd %u lun %jx nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s",
7683f072d69SAlexander Motin 		    chan, atp->tag, atp->orig_datalen, atp->bytes_xfered, (uintmax_t)atp->lun, atp->nphdl, atp->sid, atp->did, atp->oxid, states[atp->state & 0x7]);
7692df76c16SMatt Jacob 	}
7702df76c16SMatt Jacob }
7712df76c16SMatt Jacob 
7728290ea90SAlexander Motin static inot_private_data_t *
isp_get_ntpd(ispsoftc_t * isp,int chan)7738290ea90SAlexander Motin isp_get_ntpd(ispsoftc_t *isp, int chan)
7742df76c16SMatt Jacob {
775a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
7762df76c16SMatt Jacob 	inot_private_data_t *ntp;
7778290ea90SAlexander Motin 
778a6222dd7SAlexander Motin 	ntp = STAILQ_FIRST(&fc->ntfree);
7798290ea90SAlexander Motin 	if (ntp)
780a6222dd7SAlexander Motin 		STAILQ_REMOVE_HEAD(&fc->ntfree, next);
7812df76c16SMatt Jacob 	return (ntp);
7822df76c16SMatt Jacob }
7832df76c16SMatt Jacob 
7848290ea90SAlexander Motin static inot_private_data_t *
isp_find_ntpd(ispsoftc_t * isp,int chan,uint32_t tag_id,uint32_t seq_id)7858290ea90SAlexander Motin isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
7862df76c16SMatt Jacob {
787a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
788a6222dd7SAlexander Motin 	inot_private_data_t *ntp;
7898290ea90SAlexander Motin 
790a6222dd7SAlexander Motin 	for (ntp = fc->ntpool; ntp < &fc->ntpool[ATPDPSIZE]; ntp++) {
7918290ea90SAlexander Motin 		if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
7922df76c16SMatt Jacob 			return (ntp);
7932df76c16SMatt Jacob 	}
7942df76c16SMatt Jacob 	return (NULL);
7952df76c16SMatt Jacob }
7962df76c16SMatt Jacob 
7978290ea90SAlexander Motin static void
isp_put_ntpd(ispsoftc_t * isp,int chan,inot_private_data_t * ntp)7988290ea90SAlexander Motin isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
7992df76c16SMatt Jacob {
800a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
8018290ea90SAlexander Motin 
8028290ea90SAlexander Motin 	ntp->tag_id = ntp->seq_id = 0;
803a6222dd7SAlexander Motin 	STAILQ_INSERT_HEAD(&fc->ntfree, ntp, next);
8042df76c16SMatt Jacob }
8052df76c16SMatt Jacob 
806a6222dd7SAlexander Motin tstate_t *
create_lun_state(ispsoftc_t * isp,int bus,struct cam_path * path)807a6222dd7SAlexander Motin create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path)
808d81ba9d5SMatt Jacob {
809a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, bus);
810d81ba9d5SMatt Jacob 	lun_id_t lun;
8112df76c16SMatt Jacob 	tstate_t *tptr;
812d81ba9d5SMatt Jacob 
813d81ba9d5SMatt Jacob 	lun = xpt_path_lun_id(path);
814e2873b76SMatt Jacob 	tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
815a6222dd7SAlexander Motin 	if (tptr == NULL)
816a6222dd7SAlexander Motin 		return (NULL);
817315a4d6fSAlexander Motin 	tptr->ts_lun = lun;
8182df76c16SMatt Jacob 	SLIST_INIT(&tptr->atios);
8192df76c16SMatt Jacob 	SLIST_INIT(&tptr->inots);
820b4af3e8aSAlexander Motin 	STAILQ_INIT(&tptr->restart_queue);
821a6222dd7SAlexander Motin 	SLIST_INSERT_HEAD(&fc->lun_hash[LUN_HASH_FUNC(lun)], tptr, next);
8222df76c16SMatt Jacob 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
823a6222dd7SAlexander Motin 	return (tptr);
824d81ba9d5SMatt Jacob }
825d81ba9d5SMatt Jacob 
8268290ea90SAlexander Motin static void
destroy_lun_state(ispsoftc_t * isp,int bus,tstate_t * tptr)8278290ea90SAlexander Motin destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
828d81ba9d5SMatt Jacob {
829a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, bus);
830344aebe2SMatt Jacob 	union ccb *ccb;
8318290ea90SAlexander Motin 	inot_private_data_t *ntp;
832e2873b76SMatt Jacob 
8338290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
834344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
835344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
836344aebe2SMatt Jacob 		xpt_done(ccb);
8378290ea90SAlexander Motin 	};
8388290ea90SAlexander Motin 	while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
839344aebe2SMatt Jacob 		SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
840344aebe2SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_ABORTED;
841344aebe2SMatt Jacob 		xpt_done(ccb);
842344aebe2SMatt Jacob 	}
8438290ea90SAlexander Motin 	while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
8448290ea90SAlexander Motin 		isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
8458290ea90SAlexander Motin 		STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
8468290ea90SAlexander Motin 		isp_put_ntpd(isp, bus, ntp);
8478290ea90SAlexander Motin 	}
848a6222dd7SAlexander Motin 	SLIST_REMOVE(&fc->lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], tptr, tstate, next);
849d81ba9d5SMatt Jacob 	free(tptr, M_DEVBUF);
850d81ba9d5SMatt Jacob }
851d81ba9d5SMatt Jacob 
8522df76c16SMatt Jacob static void
isp_enable_lun(ispsoftc_t * isp,union ccb * ccb)8532df76c16SMatt Jacob isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
854d81ba9d5SMatt Jacob {
8553e6deb33SAlexander Motin 	tstate_t *tptr;
856a6222dd7SAlexander Motin 	int bus = XS_CHANNEL(ccb);
857a6222dd7SAlexander Motin 	target_id_t target = ccb->ccb_h.target_id;
858a6222dd7SAlexander Motin 	lun_id_t lun = ccb->ccb_h.target_lun;
859d81ba9d5SMatt Jacob 
860746e9c85SMatt Jacob 	/*
8613e6deb33SAlexander Motin 	 * We only support either target and lun both wildcard
8623e6deb33SAlexander Motin 	 * or target and lun both non-wildcard.
863746e9c85SMatt Jacob 	 */
8646af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
8656af11b82SAlexander Motin 	    "enabling lun %jx\n", (uintmax_t)lun);
8663e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
8672df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
8682df76c16SMatt Jacob 		xpt_done(ccb);
8692df76c16SMatt Jacob 		return;
8702df76c16SMatt Jacob 	}
8712df76c16SMatt Jacob 
8723e6deb33SAlexander Motin 	/* Create the state pointer. It should not already exist. */
873a1bc34c6SMatt Jacob 	tptr = get_lun_statep(isp, bus, lun);
8742df76c16SMatt Jacob 	if (tptr) {
8752df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
8763e6deb33SAlexander Motin 		xpt_done(ccb);
8773e6deb33SAlexander Motin 		return;
878d81ba9d5SMatt Jacob 	}
879a6222dd7SAlexander Motin 	tptr = create_lun_state(isp, bus, ccb->ccb_h.path);
880a6222dd7SAlexander Motin 	if (tptr == NULL) {
881a6222dd7SAlexander Motin 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
8822df76c16SMatt Jacob 		xpt_done(ccb);
8833e6deb33SAlexander Motin 		return;
8842df76c16SMatt Jacob 	}
8852df76c16SMatt Jacob 
8863e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
8873e6deb33SAlexander Motin 	xpt_done(ccb);
8882df76c16SMatt Jacob }
8892df76c16SMatt Jacob 
8902df76c16SMatt Jacob static void
isp_disable_lun(ispsoftc_t * isp,union ccb * ccb)8912df76c16SMatt Jacob isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
8922df76c16SMatt Jacob {
893a6222dd7SAlexander Motin 	tstate_t *tptr;
894a6222dd7SAlexander Motin 	int bus = XS_CHANNEL(ccb);
895a6222dd7SAlexander Motin 	target_id_t target = ccb->ccb_h.target_id;
896a6222dd7SAlexander Motin 	lun_id_t lun = ccb->ccb_h.target_lun;
8972df76c16SMatt Jacob 
8986af11b82SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
8996af11b82SAlexander Motin 	    "disabling lun %jx\n", (uintmax_t)lun);
9003e6deb33SAlexander Motin 	if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
9012df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_LUN_INVALID;
9022df76c16SMatt Jacob 		xpt_done(ccb);
9032df76c16SMatt Jacob 		return;
9042df76c16SMatt Jacob 	}
9052df76c16SMatt Jacob 
9063e6deb33SAlexander Motin 	/* Find the state pointer. */
9072df76c16SMatt Jacob 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
9083e6deb33SAlexander Motin 		ccb->ccb_h.status = CAM_PATH_INVALID;
9092df76c16SMatt Jacob 		xpt_done(ccb);
9103e6deb33SAlexander Motin 		return;
9112df76c16SMatt Jacob 	}
9122df76c16SMatt Jacob 
9138290ea90SAlexander Motin 	destroy_lun_state(isp, bus, tptr);
9143e6deb33SAlexander Motin 	ccb->ccb_h.status = CAM_REQ_CMP;
9153e6deb33SAlexander Motin 	xpt_done(ccb);
916d81ba9d5SMatt Jacob }
917d81ba9d5SMatt Jacob 
9189cd7268eSMatt Jacob static void
isp_target_start_ctio(ispsoftc_t * isp,union ccb * ccb,enum Start_Ctio_How how)919387d8239SMatt Jacob isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
920d81ba9d5SMatt Jacob {
92194dff771SMatt Jacob 	int fctape, sendstatus, resid;
922387d8239SMatt Jacob 	fcparam *fcp;
9232df76c16SMatt Jacob 	atio_private_data_t *atp;
92494dff771SMatt Jacob 	struct ccb_scsiio *cso;
92509ddc7adSAlexander Motin 	struct isp_ccbq *waitq;
92694dff771SMatt Jacob 	uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
9271b760be4SAlexander Motin 	ct7_entry_t local, *cto = &local;
928d81ba9d5SMatt Jacob 
92994dff771SMatt 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,
93094dff771SMatt Jacob 	    (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
93194dff771SMatt Jacob 
932a6222dd7SAlexander Motin 	waitq = &ISP_FC_PC(isp, XS_CHANNEL(ccb))->waitq;
93394dff771SMatt Jacob 	switch (how) {
93494dff771SMatt Jacob 	case FROM_CAM:
9352df76c16SMatt Jacob 		/*
93694dff771SMatt Jacob 		 * Insert at the tail of the list, if any, waiting CTIO CCBs
9372df76c16SMatt Jacob 		 */
938ea8e769eSAlexander Motin 		TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, sim_links.tqe);
93994dff771SMatt Jacob 		break;
94009ddc7adSAlexander Motin 	case FROM_TIMER:
94194dff771SMatt Jacob 	case FROM_SRR:
94294dff771SMatt Jacob 	case FROM_CTIO_DONE:
943ea8e769eSAlexander Motin 		TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
94494dff771SMatt Jacob 		break;
94594dff771SMatt Jacob 	}
94694dff771SMatt Jacob 
94709ddc7adSAlexander Motin 	while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
948ea8e769eSAlexander Motin 		TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
94994dff771SMatt Jacob 
95094dff771SMatt Jacob 		cso = &ccb->csio;
951387d8239SMatt Jacob 		xfrlen = cso->dxfer_len;
952387d8239SMatt Jacob 		if (xfrlen == 0) {
9532df76c16SMatt Jacob 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
954387d8239SMatt 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");
9552df76c16SMatt Jacob 				ccb->ccb_h.status = CAM_REQ_INVALID;
9562df76c16SMatt Jacob 				xpt_done(ccb);
95794dff771SMatt Jacob 				continue;
9582df76c16SMatt Jacob 			}
9592df76c16SMatt Jacob 		}
9602df76c16SMatt Jacob 
9618290ea90SAlexander Motin 		atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
9622df76c16SMatt Jacob 		if (atp == NULL) {
96394dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
9648290ea90SAlexander Motin 			isp_dump_atpd(isp, XS_CHANNEL(ccb));
9652df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
9662df76c16SMatt Jacob 			xpt_done(ccb);
96794dff771SMatt Jacob 			continue;
9682df76c16SMatt Jacob 		}
969387d8239SMatt Jacob 
970387d8239SMatt Jacob 		/*
971387d8239SMatt Jacob 		 * Is this command a dead duck?
972387d8239SMatt Jacob 		 */
9732df76c16SMatt Jacob 		if (atp->dead) {
97494dff771SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
9752df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_ABORTED;
9762df76c16SMatt Jacob 			xpt_done(ccb);
97794dff771SMatt Jacob 			continue;
9782df76c16SMatt Jacob 		}
9792df76c16SMatt Jacob 
9802df76c16SMatt Jacob 		/*
9812df76c16SMatt Jacob 		 * Check to make sure we're still in target mode.
9822df76c16SMatt Jacob 		 */
983387d8239SMatt Jacob 		fcp = FCPARAM(isp, XS_CHANNEL(ccb));
984387d8239SMatt Jacob 		if ((fcp->role & ISP_ROLE_TARGET) == 0) {
98594dff771SMatt 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);
9862df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
9872df76c16SMatt Jacob 			xpt_done(ccb);
98894dff771SMatt Jacob 			continue;
9892df76c16SMatt Jacob 		}
9902df76c16SMatt Jacob 
9912df76c16SMatt Jacob 		/*
99294dff771SMatt Jacob 		 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
99394dff771SMatt Jacob 		 * could be split into two CTIOs to split data and status).
994387d8239SMatt Jacob 		 */
99594dff771SMatt Jacob 		if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
99694dff771SMatt 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);
997ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
99894dff771SMatt Jacob 			break;
999387d8239SMatt Jacob 		}
1000387d8239SMatt Jacob 
1001d81ba9d5SMatt Jacob 		/*
1002387d8239SMatt Jacob 		 * Does the initiator expect FC-Tape style responses?
1003d81ba9d5SMatt Jacob 		 */
1004387d8239SMatt Jacob 		if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1005387d8239SMatt Jacob 			fctape = 1;
1006387d8239SMatt Jacob 		} else {
1007387d8239SMatt Jacob 			fctape = 0;
1008387d8239SMatt Jacob 		}
1009387d8239SMatt Jacob 
1010387d8239SMatt Jacob 		/*
1011387d8239SMatt Jacob 		 * If we already did the data xfer portion of a CTIO that sends data
1012387d8239SMatt Jacob 		 * and status, don't do it again and do the status portion now.
1013387d8239SMatt Jacob 		 */
1014387d8239SMatt Jacob 		if (atp->sendst) {
1015daa0dffbSAlexander Motin 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
101694dff771SMatt Jacob 			    cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1017387d8239SMatt Jacob 			xfrlen = 0;	/* we already did the data transfer */
1018387d8239SMatt Jacob 			atp->sendst = 0;
1019387d8239SMatt Jacob 		}
1020387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1021387d8239SMatt Jacob 			sendstatus = 1;
1022387d8239SMatt Jacob 		} else {
1023387d8239SMatt Jacob 			sendstatus = 0;
1024387d8239SMatt Jacob 		}
1025387d8239SMatt Jacob 
1026387d8239SMatt Jacob 		if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
102794dff771SMatt Jacob 			KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1028387d8239SMatt Jacob 			/*
1029387d8239SMatt Jacob 			 * Sense length is not the entire sense data structure size. Periph
1030387d8239SMatt Jacob 			 * drivers don't seem to be setting sense_len to reflect the actual
1031387d8239SMatt Jacob 			 * size. We'll peek inside to get the right amount.
1032387d8239SMatt Jacob 			 */
1033387d8239SMatt Jacob 			sense_length = cso->sense_len;
1034387d8239SMatt Jacob 
1035387d8239SMatt Jacob 			/*
1036387d8239SMatt Jacob 			 * This 'cannot' happen
1037387d8239SMatt Jacob 			 */
1038387d8239SMatt Jacob 			if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1039387d8239SMatt Jacob 				sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1040387d8239SMatt Jacob 			}
1041387d8239SMatt Jacob 		} else {
1042387d8239SMatt Jacob 			sense_length = 0;
1043387d8239SMatt Jacob 		}
1044387d8239SMatt Jacob 
104594dff771SMatt Jacob 		/*
104694dff771SMatt Jacob 		 * Check for overflow
104794dff771SMatt Jacob 		 */
1048a6036a44SAlexander Motin 		tmp = atp->bytes_xfered + atp->bytes_in_transit;
1049a6036a44SAlexander Motin 		if (xfrlen > 0 && tmp > atp->orig_datalen) {
1050a6036a44SAlexander Motin 			isp_prt(isp, ISP_LOGERR,
1051a6036a44SAlexander Motin 			    "%s: [0x%x] data overflow by %u bytes", __func__,
1052a6036a44SAlexander Motin 			    cso->tag_id, tmp + xfrlen - atp->orig_datalen);
105394dff771SMatt Jacob 			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
105494dff771SMatt Jacob 			xpt_done(ccb);
105594dff771SMatt Jacob 			continue;
105694dff771SMatt Jacob 		}
1057a6036a44SAlexander Motin 		if (xfrlen > atp->orig_datalen - tmp) {
1058a6036a44SAlexander Motin 			xfrlen = atp->orig_datalen - tmp;
1059a6036a44SAlexander Motin 			if (xfrlen == 0 && !sendstatus) {
1060a6036a44SAlexander Motin 				cso->resid = cso->dxfer_len;
1061a6036a44SAlexander Motin 				ccb->ccb_h.status = CAM_REQ_CMP;
1062a6036a44SAlexander Motin 				xpt_done(ccb);
1063a6036a44SAlexander Motin 				continue;
1064a6036a44SAlexander Motin 			}
1065a6036a44SAlexander Motin 		}
1066387d8239SMatt Jacob 
10671b760be4SAlexander Motin 		memset(cto, 0, QENTRY_LEN);
10682df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
10692df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
107094dff771SMatt Jacob 		cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
107194dff771SMatt Jacob 		ATPD_SET_SEQNO(cto, atp);
10722df76c16SMatt Jacob 		cto->ct_nphdl = atp->nphdl;
10732df76c16SMatt Jacob 		cto->ct_rxid = atp->tag;
10743f072d69SAlexander Motin 		cto->ct_iid_lo = atp->sid;
10753f072d69SAlexander Motin 		cto->ct_iid_hi = atp->sid >> 16;
10762df76c16SMatt Jacob 		cto->ct_oxid = atp->oxid;
10772df76c16SMatt Jacob 		cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
107831c161a6SAlexander Motin 		cto->ct_timeout = XS_TIME(ccb);
10792df76c16SMatt Jacob 		cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1080387d8239SMatt Jacob 
1081387d8239SMatt Jacob 		/*
1082387d8239SMatt Jacob 		 * Mode 1, status, no data. Only possible when we are sending status, have
10836f7aeb5fSMatt Jacob 		 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1084387d8239SMatt Jacob 		 *
10858b382bc2SMatt Jacob 		 * Mode 2, status, no data. We have to use this in the case that
10868b382bc2SMatt Jacob 		 * the sense data won't fit into a ct7_entry_t.
1087387d8239SMatt Jacob 		 *
1088387d8239SMatt Jacob 		 */
1089387d8239SMatt Jacob 		if (sendstatus && xfrlen == 0) {
1090387d8239SMatt Jacob 			cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
109194dff771SMatt Jacob 			resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1092387d8239SMatt Jacob 			if (sense_length <= MAXRESPLEN_24XX) {
1093387d8239SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE1;
1094387d8239SMatt Jacob 				cto->ct_scsi_status = cso->scsi_status;
1095387d8239SMatt Jacob 				if (resid < 0) {
1096a6036a44SAlexander Motin 					cto->ct_resid = -resid;
1097387d8239SMatt Jacob 					cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1098387d8239SMatt Jacob 				} else if (resid > 0) {
1099a6036a44SAlexander Motin 					cto->ct_resid = resid;
1100387d8239SMatt Jacob 					cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1101387d8239SMatt Jacob 				}
1102387d8239SMatt Jacob 				if (fctape) {
1103387d8239SMatt Jacob 					cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1104387d8239SMatt Jacob 				}
1105387d8239SMatt Jacob 				if (sense_length) {
11062df76c16SMatt Jacob 					cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1107387d8239SMatt Jacob 					cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1108387d8239SMatt Jacob 					memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
11092df76c16SMatt Jacob 				}
11102df76c16SMatt Jacob 			} else {
1111387d8239SMatt Jacob 				bus_addr_t addr;
111257713edaSAlexander Motin 				fcp_rsp_iu_t rp;
1113387d8239SMatt Jacob 
1114387d8239SMatt Jacob 				if (atp->ests == NULL) {
1115387d8239SMatt Jacob 					atp->ests = isp_get_ecmd(isp);
1116387d8239SMatt Jacob 					if (atp->ests == NULL) {
1117ea8e769eSAlexander Motin 						TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
111894dff771SMatt Jacob 						break;
1119387d8239SMatt Jacob 					}
1120387d8239SMatt Jacob 				}
112157713edaSAlexander Motin 				memset(&rp, 0, sizeof(rp));
1122387d8239SMatt Jacob 				if (fctape) {
1123387d8239SMatt Jacob 					cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
112457713edaSAlexander Motin 					rp.fcp_rsp_bits |= FCP_CONF_REQ;
1125387d8239SMatt Jacob 				}
1126387d8239SMatt Jacob 				cto->ct_flags |= CT7_FLAG_MODE2;
112757713edaSAlexander Motin 				rp.fcp_rsp_scsi_status = cso->scsi_status;
1128387d8239SMatt Jacob 				if (resid < 0) {
112957713edaSAlexander Motin 					rp.fcp_rsp_resid = -resid;
113057713edaSAlexander Motin 					rp.fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1131387d8239SMatt Jacob 				} else if (resid > 0) {
113257713edaSAlexander Motin 					rp.fcp_rsp_resid = resid;
113357713edaSAlexander Motin 					rp.fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1134387d8239SMatt Jacob 				}
1135387d8239SMatt Jacob 				if (sense_length) {
113657713edaSAlexander Motin 					rp.fcp_rsp_snslen = sense_length;
1137387d8239SMatt Jacob 					cto->ct_senselen = sense_length;
113857713edaSAlexander Motin 					rp.fcp_rsp_bits |= FCP_SNSLEN_VALID;
113957713edaSAlexander Motin 					isp_put_fcp_rsp_iu(isp, &rp, atp->ests);
1140387d8239SMatt Jacob 					memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1141387d8239SMatt Jacob 				} else {
114257713edaSAlexander Motin 					isp_put_fcp_rsp_iu(isp, &rp, atp->ests);
1143387d8239SMatt Jacob 				}
1144387d8239SMatt Jacob 				if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1145387d8239SMatt Jacob 					isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1146387d8239SMatt Jacob 				}
1147cf770ba3SAlexander Motin 				bus_dmamap_sync(isp->isp_osinfo.ecmd_dmat, isp->isp_osinfo.ecmd_map, BUS_DMASYNC_PREWRITE);
1148387d8239SMatt Jacob 				addr = isp->isp_osinfo.ecmd_dma;
1149387d8239SMatt Jacob 				addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
115094dff771SMatt 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,
1151387d8239SMatt Jacob 				    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1152387d8239SMatt Jacob 				cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1153387d8239SMatt Jacob 				cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1154387d8239SMatt Jacob 				cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1155387d8239SMatt Jacob 				cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1156387d8239SMatt Jacob 			}
1157387d8239SMatt Jacob 			if (sense_length) {
115894dff771SMatt 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__,
115994dff771SMatt 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,
116094dff771SMatt Jacob 				    cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1161387d8239SMatt Jacob 			} else {
116294dff771SMatt 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__,
116394dff771SMatt Jacob 				    cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1164387d8239SMatt Jacob 			}
1165387d8239SMatt Jacob 			atp->state = ATPD_STATE_LAST_CTIO;
1166387d8239SMatt Jacob 		}
1167387d8239SMatt Jacob 
1168387d8239SMatt Jacob 		/*
1169387d8239SMatt Jacob 		 * Mode 0 data transfers, *possibly* with status.
1170387d8239SMatt Jacob 		 */
1171387d8239SMatt Jacob 		if (xfrlen != 0) {
11722df76c16SMatt Jacob 			cto->ct_flags |= CT7_FLAG_MODE0;
11732df76c16SMatt Jacob 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
11742df76c16SMatt Jacob 				cto->ct_flags |= CT7_DATA_IN;
11752df76c16SMatt Jacob 			} else {
11762df76c16SMatt Jacob 				cto->ct_flags |= CT7_DATA_OUT;
11772df76c16SMatt Jacob 			}
1178387d8239SMatt Jacob 
117994dff771SMatt Jacob 			cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1180387d8239SMatt Jacob 			cto->rsp.m0.ct_xfrlen = xfrlen;
1181387d8239SMatt Jacob 
1182387d8239SMatt Jacob #ifdef	DEBUG
1183387d8239SMatt 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) {
1184387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1185387d8239SMatt Jacob 				ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1186387d8239SMatt Jacob 				cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1187387d8239SMatt Jacob 			}
1188387d8239SMatt Jacob #endif
1189387d8239SMatt Jacob 			if (sendstatus) {
119094dff771SMatt Jacob 				resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
119194dff771SMatt Jacob 				if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1192387d8239SMatt Jacob 					cto->ct_flags |= CT7_SENDSTATUS;
1193387d8239SMatt Jacob 					atp->state = ATPD_STATE_LAST_CTIO;
119494dff771SMatt Jacob 					if (fctape) {
119594dff771SMatt Jacob 						cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
119694dff771SMatt Jacob 					}
1197387d8239SMatt Jacob 				} else {
1198387d8239SMatt Jacob 					atp->sendst = 1;	/* send status later */
119994dff771SMatt Jacob 					cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
12002df76c16SMatt Jacob 					atp->state = ATPD_STATE_CTIO;
12012df76c16SMatt Jacob 				}
1202387d8239SMatt Jacob 			} else {
1203387d8239SMatt Jacob 				atp->state = ATPD_STATE_CTIO;
1204387d8239SMatt Jacob 			}
120594dff771SMatt 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__,
120694dff771SMatt 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);
1207387d8239SMatt Jacob 		}
1208d81ba9d5SMatt Jacob 
120994dff771SMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
121094dff771SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1211ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
121294dff771SMatt Jacob 			break;
121394dff771SMatt Jacob 		}
1214970ceb2fSAlexander Motin 		handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1215970ceb2fSAlexander Motin 		if (handle == 0) {
1216387d8239SMatt Jacob 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1217ea8e769eSAlexander Motin 			TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
121894dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
121994dff771SMatt Jacob 			break;
1220d81ba9d5SMatt Jacob 		}
122194dff771SMatt Jacob 		atp->bytes_in_transit += xfrlen;
122294dff771SMatt Jacob 		PISP_PCMD(ccb)->datalen = xfrlen;
122394dff771SMatt Jacob 
1224d81ba9d5SMatt Jacob 		/*
1225d81ba9d5SMatt Jacob 		 * Call the dma setup routines for this entry (and any subsequent
1226d81ba9d5SMatt Jacob 		 * CTIOs) if there's data to move, and then tell the f/w it's got
1227b09b0095SMatt Jacob 		 * new things to play with. As with isp_start's usage of DMA setup,
1228d81ba9d5SMatt Jacob 		 * any swizzling is done in the machine dependent layer. Because
1229d81ba9d5SMatt Jacob 		 * of this, we put the request onto the queue area first in native
1230d81ba9d5SMatt Jacob 		 * format.
1231d81ba9d5SMatt Jacob 		 */
12322df76c16SMatt Jacob 		cto->ct_syshandle = handle;
12331b760be4SAlexander Motin 		dmaresult = ISP_DMASETUP(isp, cso, cto);
1234f6854a0cSAlexander Motin 		if (dmaresult != 0) {
1235970ceb2fSAlexander Motin 			isp_destroy_handle(isp, handle);
123694dff771SMatt Jacob 			isp_free_pcmd(isp, ccb);
123794dff771SMatt Jacob 			if (dmaresult == CMD_EAGAIN) {
1238ea8e769eSAlexander Motin 				TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
123994dff771SMatt Jacob 				break;
124094dff771SMatt Jacob 			}
124194dff771SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
124294dff771SMatt Jacob 			xpt_done(ccb);
124394dff771SMatt Jacob 			continue;
124494dff771SMatt Jacob 		}
124594dff771SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1246387d8239SMatt Jacob 		if (xfrlen) {
1247387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1248387d8239SMatt Jacob 		} else {
1249387d8239SMatt Jacob 			ccb->ccb_h.spriv_field0 = ~0;
1250387d8239SMatt Jacob 		}
1251387d8239SMatt Jacob 		atp->ctcnt++;
125294dff771SMatt Jacob 		atp->seqno++;
12532df76c16SMatt Jacob 	}
1254d81ba9d5SMatt Jacob }
1255d81ba9d5SMatt Jacob 
1256a1bc34c6SMatt Jacob static void
isp_refire_notify_ack(void * arg)1257387d8239SMatt Jacob isp_refire_notify_ack(void *arg)
1258387d8239SMatt Jacob {
1259387d8239SMatt Jacob 	isp_tna_t *tp  = arg;
1260387d8239SMatt Jacob 	ispsoftc_t *isp = tp->isp;
12612a0db815SJohn Baldwin 
12622a0db815SJohn Baldwin 	ISP_ASSERT_LOCKED(isp);
1263387d8239SMatt Jacob 	if (isp_notify_ack(isp, tp->not)) {
12642a0db815SJohn Baldwin 		callout_schedule(&tp->timer, 5);
1265387d8239SMatt Jacob 	} else {
1266387d8239SMatt Jacob 		free(tp, M_DEVBUF);
1267387d8239SMatt Jacob 	}
1268387d8239SMatt Jacob }
1269387d8239SMatt Jacob 
1270387d8239SMatt Jacob 
1271387d8239SMatt Jacob static void
isp_complete_ctio(ispsoftc_t * isp,union ccb * ccb)12720f99cb55SAlexander Motin isp_complete_ctio(ispsoftc_t *isp, union ccb *ccb)
1273f48ce188SMatt Jacob {
12740f99cb55SAlexander Motin 
12750f99cb55SAlexander Motin 	isp_rq_check_below(isp);
1276a1bc34c6SMatt Jacob 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1277a1bc34c6SMatt Jacob 	xpt_done(ccb);
1278f48ce188SMatt Jacob }
1279f48ce188SMatt Jacob 
12802df76c16SMatt Jacob static void
isp_handle_platform_atio7(ispsoftc_t * isp,at7_entry_t * aep)12812df76c16SMatt Jacob isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
12822df76c16SMatt Jacob {
12832df76c16SMatt Jacob 	int cdbxlen;
12846af11b82SAlexander Motin 	lun_id_t lun;
12856af11b82SAlexander Motin 	uint16_t chan, nphdl = NIL_HANDLE;
12862df76c16SMatt Jacob 	uint32_t did, sid;
12872df76c16SMatt Jacob 	fcportdb_t *lp;
12882df76c16SMatt Jacob 	tstate_t *tptr;
12892df76c16SMatt Jacob 	struct ccb_accept_tio *atiop;
12902df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
12919e7d423dSMatt Jacob 	atio_private_data_t *oatp;
12922df76c16SMatt Jacob 	inot_private_data_t *ntp;
12932df76c16SMatt Jacob 
12942df76c16SMatt Jacob 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
12952df76c16SMatt Jacob 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
12966af11b82SAlexander Motin 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
12972df76c16SMatt Jacob 
12987dbe8f17SAlexander Motin 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
12999c81a61eSAlexander Motin 		/* Channel has to be derived from D_ID */
13002df76c16SMatt Jacob 		isp_find_chan_by_did(isp, did, &chan);
13012df76c16SMatt Jacob 		if (chan == ISP_NOCHAN) {
13029c81a61eSAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
13039c81a61eSAlexander Motin 			    "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
13049c81a61eSAlexander Motin 			    __func__, aep->at_rxid, did);
13059c81a61eSAlexander Motin 			isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
13069c81a61eSAlexander Motin 			    ECMD_TERMINATE, 0);
13072df76c16SMatt Jacob 			return;
13082df76c16SMatt Jacob 		}
13092df76c16SMatt Jacob 	} else {
13102df76c16SMatt Jacob 		chan = 0;
13112df76c16SMatt Jacob 	}
13122df76c16SMatt Jacob 
13132df76c16SMatt Jacob 	/*
13142df76c16SMatt Jacob 	 * Find the PDB entry for this initiator
13152df76c16SMatt Jacob 	 */
1316eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
13172df76c16SMatt Jacob 		/*
13182df76c16SMatt Jacob 		 * If we're not in the port database terminate the exchange.
13192df76c16SMatt Jacob 		 */
13202df76c16SMatt 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",
13212df76c16SMatt Jacob 		    __func__, aep->at_rxid, did, chan, sid);
1322e68eef14SAlexander Motin 		isp_dump_portdb(isp, chan);
13232df76c16SMatt Jacob 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
13242df76c16SMatt Jacob 		return;
13252df76c16SMatt Jacob 	}
13262df76c16SMatt Jacob 	nphdl = lp->handle;
13272df76c16SMatt Jacob 
13282df76c16SMatt Jacob 	/*
13292df76c16SMatt Jacob 	 * Get the tstate pointer
13302df76c16SMatt Jacob 	 */
13312df76c16SMatt Jacob 	tptr = get_lun_statep(isp, chan, lun);
13322df76c16SMatt Jacob 	if (tptr == NULL) {
13332df76c16SMatt Jacob 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
13342df76c16SMatt Jacob 		if (tptr == NULL) {
13356af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN,
13366af11b82SAlexander Motin 			    "%s: [0x%x] no state pointer for lun %jx or wildcard",
13376af11b82SAlexander Motin 			    __func__, aep->at_rxid, (uintmax_t)lun);
1338387d8239SMatt Jacob 			if (lun == 0) {
1339352427b3SAlexander Motin 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1340387d8239SMatt Jacob 			} else {
13412df76c16SMatt Jacob 				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1342387d8239SMatt Jacob 			}
13432df76c16SMatt Jacob 			return;
13442df76c16SMatt Jacob 		}
13452df76c16SMatt Jacob 	}
13462df76c16SMatt Jacob 
13472df76c16SMatt Jacob 	/*
13482df76c16SMatt Jacob 	 * Start any commands pending resources first.
13492df76c16SMatt Jacob 	 */
13508290ea90SAlexander Motin 	if (isp_atio_restart(isp, chan, tptr))
13512df76c16SMatt Jacob 		goto noresrc;
13522df76c16SMatt Jacob 
13532df76c16SMatt Jacob 	/*
13542df76c16SMatt Jacob 	 * If the f/w is out of resources, just send a BUSY status back.
13552df76c16SMatt Jacob 	 */
13562df76c16SMatt Jacob 	if (aep->at_rxid == AT7_NORESRC_RXID) {
13572df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
13582df76c16SMatt Jacob 		return;
13592df76c16SMatt Jacob 	}
13602df76c16SMatt Jacob 
13612df76c16SMatt Jacob 	/*
13622df76c16SMatt Jacob 	 * If we're out of resources, just send a BUSY status back.
13632df76c16SMatt Jacob 	 */
13642df76c16SMatt Jacob 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
13652df76c16SMatt Jacob 	if (atiop == NULL) {
13662df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
13672df76c16SMatt Jacob 		goto noresrc;
13682df76c16SMatt Jacob 	}
13692df76c16SMatt Jacob 
13708290ea90SAlexander Motin 	oatp = isp_find_atpd(isp, chan, aep->at_rxid);
13719e7d423dSMatt Jacob 	if (oatp) {
13720b19f90aSAlexander Motin 		isp_prt(isp, oatp->state == ATPD_STATE_LAST_CTIO ? ISP_LOGTDEBUG0 :
13730b19f90aSAlexander Motin 		    ISP_LOGWARN, "[0x%x] tag wraparound (N-Port Handle "
13740b19f90aSAlexander Motin 		    "0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
13759e7d423dSMatt Jacob 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
13762df76c16SMatt Jacob 		/*
13772df76c16SMatt Jacob 		 * It's not a "no resource" condition- but we can treat it like one
13782df76c16SMatt Jacob 		 */
13792df76c16SMatt Jacob 		goto noresrc;
13802df76c16SMatt Jacob 	}
13818290ea90SAlexander Motin 	atp = isp_get_atpd(isp, chan, aep->at_rxid);
1382523ea374SAlexander Motin 	if (atp == NULL) {
1383523ea374SAlexander Motin 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
13840b19f90aSAlexander Motin 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
13850b19f90aSAlexander Motin 		return;
1386523ea374SAlexander Motin 	}
1387387d8239SMatt Jacob 	atp->word3 = lp->prli_word3;
13882df76c16SMatt Jacob 	atp->state = ATPD_STATE_ATIO;
13892df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1390d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1391d4f3ad3aSAlexander Motin 	atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
13926ddb8972SAlexander Motin 	atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
13932df76c16SMatt Jacob 	atiop->ccb_h.target_lun = lun;
13942df76c16SMatt Jacob 	atiop->sense_len = 0;
13952df76c16SMatt Jacob 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
13962df76c16SMatt Jacob 	if (cdbxlen) {
13972df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
13982df76c16SMatt Jacob 	}
13992df76c16SMatt Jacob 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
14002df76c16SMatt Jacob 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
14012df76c16SMatt Jacob 	atiop->cdb_len = cdbxlen;
14022df76c16SMatt Jacob 	atiop->ccb_h.status = CAM_CDB_RECVD;
14032df76c16SMatt Jacob 	atiop->tag_id = atp->tag;
14042df76c16SMatt Jacob 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
14052df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_SIMPLE:
14065e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
14071b760be4SAlexander Motin 		atiop->tag_action = MSG_SIMPLE_TASK;
14082df76c16SMatt Jacob 		break;
14092df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_HEAD:
14105e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
14111b760be4SAlexander Motin 		atiop->tag_action = MSG_HEAD_OF_QUEUE_TASK;
14122df76c16SMatt Jacob 		break;
14132df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ORDERED:
14145e63cdb4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
14151b760be4SAlexander Motin 		atiop->tag_action = MSG_ORDERED_TASK;
14162df76c16SMatt Jacob 		break;
14172df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_ACA:
14181b760be4SAlexander Motin 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
14191b760be4SAlexander Motin 		atiop->tag_action = MSG_ACA_TASK;
14201b760be4SAlexander Motin 		break;
14212df76c16SMatt Jacob 	case FCP_CMND_TASK_ATTR_UNTAGGED:
14221b760be4SAlexander Motin 	default:
14232df76c16SMatt Jacob 		atiop->tag_action = 0;
14242df76c16SMatt Jacob 		break;
14252df76c16SMatt Jacob 	}
142688364968SAlexander Motin 	atiop->priority = (aep->at_cmnd.fcp_cmnd_task_attribute &
142788364968SAlexander Motin 	    FCP_CMND_PRIO_MASK) >> FCP_CMND_PRIO_SHIFT;
14282df76c16SMatt Jacob 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
14292df76c16SMatt Jacob 	atp->bytes_xfered = 0;
14302df76c16SMatt Jacob 	atp->lun = lun;
14312df76c16SMatt Jacob 	atp->nphdl = nphdl;
14323f072d69SAlexander Motin 	atp->sid = sid;
14333f072d69SAlexander Motin 	atp->did = did;
14342df76c16SMatt Jacob 	atp->oxid = aep->at_hdr.ox_id;
14359e7d423dSMatt Jacob 	atp->rxid = aep->at_hdr.rx_id;
14362df76c16SMatt Jacob 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
14372df76c16SMatt Jacob 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
14382df76c16SMatt Jacob 	atp->state = ATPD_STATE_CAM;
14396af11b82SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
14406af11b82SAlexander Motin 	    aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
14412df76c16SMatt Jacob 	xpt_done((union ccb *)atiop);
14422df76c16SMatt Jacob 	return;
14432df76c16SMatt Jacob noresrc:
14443037002eSAlexander Motin 	KASSERT(atp == NULL, ("%s: atp is not NULL on noresrc!\n", __func__));
14458290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, chan);
14462df76c16SMatt Jacob 	if (ntp == NULL) {
14472df76c16SMatt Jacob 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
14482df76c16SMatt Jacob 		return;
14492df76c16SMatt Jacob 	}
14508290ea90SAlexander Motin 	memcpy(ntp->data, aep, QENTRY_LEN);
14518290ea90SAlexander Motin 	STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
14522df76c16SMatt Jacob }
14532df76c16SMatt Jacob 
1454387d8239SMatt Jacob 
1455387d8239SMatt Jacob /*
1456387d8239SMatt Jacob  * Handle starting an SRR (sequence retransmit request)
1457387d8239SMatt Jacob  * We get here when we've gotten the immediate notify
1458387d8239SMatt Jacob  * and the return of all outstanding CTIOs for this
1459387d8239SMatt Jacob  * transaction.
1460387d8239SMatt Jacob  */
1461387d8239SMatt Jacob static void
isp_handle_srr_start(ispsoftc_t * isp,atio_private_data_t * atp)14628290ea90SAlexander Motin isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1463387d8239SMatt Jacob {
1464387d8239SMatt Jacob 	in_fcentry_24xx_t *inot;
1465387d8239SMatt Jacob 	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1466387d8239SMatt Jacob 	union ccb *ccb;
1467387d8239SMatt Jacob 
1468387d8239SMatt Jacob 	inot = (in_fcentry_24xx_t *)atp->srr;
1469387d8239SMatt Jacob 	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1470387d8239SMatt Jacob 	ccb = atp->srr_ccb;
1471387d8239SMatt Jacob 	atp->srr_ccb = NULL;
1472387d8239SMatt Jacob 	atp->nsrr++;
1473387d8239SMatt Jacob 	if (ccb == NULL) {
147494dff771SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1475387d8239SMatt Jacob 		goto fail;
1476387d8239SMatt Jacob 	}
1477387d8239SMatt Jacob 
1478387d8239SMatt Jacob 	ccb_off = ccb->ccb_h.spriv_field0;
1479387d8239SMatt Jacob 	ccb_len = ccb->csio.dxfer_len;
1480387d8239SMatt Jacob         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1481387d8239SMatt Jacob 
1482387d8239SMatt Jacob 	switch (inot->in_srr_iu) {
1483387d8239SMatt Jacob 	case R_CTL_INFO_SOLICITED_DATA:
1484387d8239SMatt Jacob 		/*
1485387d8239SMatt Jacob 		 * We have to restart a FCP_DATA data out transaction
1486387d8239SMatt Jacob 		 */
1487387d8239SMatt Jacob 		atp->sendst = 0;
1488387d8239SMatt Jacob 		atp->bytes_xfered = srr_off;
1489387d8239SMatt Jacob 		if (ccb_len == 0) {
149094dff771SMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1491387d8239SMatt Jacob 			goto mdp;
1492387d8239SMatt Jacob 		}
1493387d8239SMatt Jacob  		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
149494dff771SMatt 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);
1495387d8239SMatt Jacob 			goto mdp;
1496387d8239SMatt Jacob 		}
149794dff771SMatt 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);
1498387d8239SMatt Jacob 		break;
1499387d8239SMatt Jacob 	case R_CTL_INFO_COMMAND_STATUS:
150094dff771SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1501387d8239SMatt Jacob 		atp->sendst = 1;
1502387d8239SMatt Jacob 		/*
1503387d8239SMatt Jacob 		 * We have to restart a FCP_RSP IU transaction
1504387d8239SMatt Jacob 		 */
1505387d8239SMatt Jacob 		break;
1506387d8239SMatt Jacob 	case R_CTL_INFO_DATA_DESCRIPTOR:
1507387d8239SMatt Jacob 		/*
1508387d8239SMatt Jacob 		 * We have to restart an FCP DATA in transaction
1509387d8239SMatt Jacob 		 */
1510387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
1511387d8239SMatt Jacob 		goto fail;
1512387d8239SMatt Jacob 
1513387d8239SMatt Jacob 	default:
1514387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
1515387d8239SMatt Jacob 		goto fail;
1516387d8239SMatt Jacob 	}
1517387d8239SMatt Jacob 
1518387d8239SMatt Jacob 	/*
1519387d8239SMatt Jacob 	 * We can't do anything until this is acked, so we might as well start it now.
1520387d8239SMatt Jacob 	 * We aren't going to do the usual asynchronous ack issue because we need
1521387d8239SMatt Jacob 	 * to make sure this gets on the wire first.
1522387d8239SMatt Jacob 	 */
1523387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
1524387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
1525387d8239SMatt Jacob 		goto fail;
1526387d8239SMatt Jacob 	}
1527387d8239SMatt Jacob 	isp_target_start_ctio(isp, ccb, FROM_SRR);
1528387d8239SMatt Jacob 	return;
1529387d8239SMatt Jacob fail:
1530387d8239SMatt Jacob 	inot->in_reserved = 1;
1531387d8239SMatt Jacob 	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
1532387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1533387d8239SMatt Jacob 	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
15340f99cb55SAlexander Motin 	isp_complete_ctio(isp, ccb);
1535387d8239SMatt Jacob 	return;
1536387d8239SMatt Jacob mdp:
1537387d8239SMatt Jacob 	if (isp_notify_ack(isp, inot)) {
1538387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
1539387d8239SMatt Jacob 		goto fail;
1540387d8239SMatt Jacob 	}
1541387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
15420f99cb55SAlexander Motin 	ccb->ccb_h.status |= CAM_MESSAGE_RECV;
1543387d8239SMatt Jacob 	/*
1544387d8239SMatt Jacob 	 * This is not a strict interpretation of MDP, but it's close
1545387d8239SMatt Jacob 	 */
1546387d8239SMatt Jacob 	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
1547387d8239SMatt Jacob 	ccb->csio.msg_len = 7;
1548387d8239SMatt Jacob 	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
1549387d8239SMatt Jacob 	ccb->csio.msg_ptr[1] = 5;
1550387d8239SMatt Jacob 	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
1551387d8239SMatt Jacob 	ccb->csio.msg_ptr[3] = srr_off >> 24;
1552387d8239SMatt Jacob 	ccb->csio.msg_ptr[4] = srr_off >> 16;
1553387d8239SMatt Jacob 	ccb->csio.msg_ptr[5] = srr_off >> 8;
1554387d8239SMatt Jacob 	ccb->csio.msg_ptr[6] = srr_off;
15550f99cb55SAlexander Motin 	isp_complete_ctio(isp, ccb);
1556387d8239SMatt Jacob }
1557387d8239SMatt Jacob 
1558387d8239SMatt Jacob 
1559387d8239SMatt Jacob static void
isp_handle_platform_srr(ispsoftc_t * isp,isp_notify_t * notify)15605a5632c2SAlexander Motin isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
1561387d8239SMatt Jacob {
15625a5632c2SAlexander Motin 	in_fcentry_24xx_t *inot = notify->nt_lreserved;
1563387d8239SMatt Jacob 	atio_private_data_t *atp;
15645a5632c2SAlexander Motin 	uint32_t tag = notify->nt_tagval & 0xffffffff;
1565387d8239SMatt Jacob 
15665a5632c2SAlexander Motin 	atp = isp_find_atpd(isp, notify->nt_channel, tag);
1567387d8239SMatt Jacob 	if (atp == NULL) {
15685a5632c2SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
15695a5632c2SAlexander Motin 		    __func__, tag);
1570387d8239SMatt Jacob 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
1571387d8239SMatt Jacob 		return;
1572387d8239SMatt Jacob 	}
1573387d8239SMatt Jacob 	atp->srr_notify_rcvd = 1;
1574387d8239SMatt Jacob 	memcpy(atp->srr, inot, sizeof (atp->srr));
15755a5632c2SAlexander Motin 	isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
15765a5632c2SAlexander Motin 	    inot->in_rxid, inot->in_flags, inot->in_srr_iu,
15775a5632c2SAlexander Motin 	    ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
1578387d8239SMatt Jacob 	if (atp->srr_ccb)
15798290ea90SAlexander Motin 		isp_handle_srr_start(isp, atp);
1580387d8239SMatt Jacob }
1581387d8239SMatt Jacob 
15822df76c16SMatt Jacob static void
isp_handle_platform_ctio(ispsoftc_t * isp,ct7_entry_t * ct)15831b760be4SAlexander Motin isp_handle_platform_ctio(ispsoftc_t *isp, ct7_entry_t *ct)
1584d81ba9d5SMatt Jacob {
1585d81ba9d5SMatt Jacob 	union ccb *ccb;
1586a6036a44SAlexander Motin 	int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
15872df76c16SMatt Jacob 	atio_private_data_t *atp = NULL;
15882df76c16SMatt Jacob 	int bus;
1589a6036a44SAlexander Motin 	uint32_t handle, data_requested, resid;
1590d81ba9d5SMatt Jacob 
15911b760be4SAlexander Motin 	handle = ct->ct_syshandle;
1592970ceb2fSAlexander Motin 	ccb = isp_find_xs(isp, handle);
15932df76c16SMatt Jacob 	if (ccb == NULL) {
15941b760be4SAlexander Motin 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, ct);
15952df76c16SMatt Jacob 		return;
15962df76c16SMatt Jacob 	}
1597970ceb2fSAlexander Motin 	isp_destroy_handle(isp, handle);
1598a6036a44SAlexander Motin 	resid = data_requested = PISP_PCMD(ccb)->datalen;
1599387d8239SMatt Jacob 	isp_free_pcmd(isp, ccb);
1600387d8239SMatt Jacob 
16012df76c16SMatt Jacob 	bus = XS_CHANNEL(ccb);
16021b760be4SAlexander Motin 	atp = isp_find_atpd(isp, bus, ct->ct_rxid);
1603387d8239SMatt Jacob 	if (atp == NULL) {
1604af207637SAlexander Motin 		/*
16058656f200SAlexander Motin 		 * XXX: isp_clear_commands() generates fake CTIO with zero
16068656f200SAlexander Motin 		 * ct_rxid value, filling only ct_syshandle.  Workaround
16078656f200SAlexander Motin 		 * that using tag_id from the CCB, pointed by ct_syshandle.
1608af207637SAlexander Motin 		 */
16098290ea90SAlexander Motin 		atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
1610af207637SAlexander Motin 	}
1611af207637SAlexander Motin 	if (atp == NULL) {
1612387d8239SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
1613387d8239SMatt Jacob 		return;
1614387d8239SMatt Jacob 	}
1615387d8239SMatt Jacob 	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
161694dff771SMatt Jacob 	atp->bytes_in_transit -= data_requested;
1617387d8239SMatt Jacob 	atp->ctcnt -= 1;
1618387d8239SMatt Jacob 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1619387d8239SMatt Jacob 
1620387d8239SMatt Jacob 	if (ct->ct_nphdl == CT7_SRR) {
1621387d8239SMatt Jacob 		atp->srr_ccb = ccb;
1622387d8239SMatt Jacob 		if (atp->srr_notify_rcvd)
16238290ea90SAlexander Motin 			isp_handle_srr_start(isp, atp);
16242df76c16SMatt Jacob 		return;
16252df76c16SMatt Jacob 	}
1626387d8239SMatt Jacob 	if (ct->ct_nphdl == CT_HBA_RESET) {
162787de303cSAlexander Motin 		sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
162887de303cSAlexander Motin 		    (atp->sendst == 0);
1629387d8239SMatt Jacob 		failure = CAM_UNREC_HBA_ERROR;
1630387d8239SMatt Jacob 	} else {
16312df76c16SMatt Jacob 		sentstatus = ct->ct_flags & CT7_SENDSTATUS;
16322df76c16SMatt Jacob 		ok = (ct->ct_nphdl == CT7_OK);
163394dff771SMatt Jacob 		notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
1634a6036a44SAlexander Motin 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
16352df76c16SMatt Jacob 			resid = ct->ct_resid;
16362df76c16SMatt Jacob 	}
163794dff771SMatt 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),
163894dff771SMatt Jacob 	   notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
1639387d8239SMatt Jacob 	if (ok) {
1640a6036a44SAlexander Motin 		if (data_requested > 0) {
1641a6036a44SAlexander Motin 			atp->bytes_xfered += data_requested - resid;
1642a6036a44SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
1643a6036a44SAlexander Motin 			    (data_requested - resid);
1644387d8239SMatt Jacob 		}
1645a6036a44SAlexander Motin 		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
1646387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_SENT_SENSE;
1647387d8239SMatt Jacob 		ccb->ccb_h.status |= CAM_REQ_CMP;
1648387d8239SMatt Jacob 	} else {
1649387d8239SMatt Jacob 		notify_cam = 1;
1650387d8239SMatt Jacob 		if (failure == CAM_UNREC_HBA_ERROR)
1651387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
1652387d8239SMatt Jacob 		else
1653387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1654387d8239SMatt Jacob 	}
1655387d8239SMatt Jacob 	atp->state = ATPD_STATE_PDON;
1656a1bc34c6SMatt Jacob 
1657a1bc34c6SMatt Jacob 	/*
1658387d8239SMatt Jacob 	 * We never *not* notify CAM when there has been any error (ok == 0),
1659387d8239SMatt Jacob 	 * so we never need to do an ATIO putback if we're not notifying CAM.
1660d81ba9d5SMatt Jacob 	 */
166194dff771SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
166294dff771SMatt Jacob 	    (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
1663f48ce188SMatt Jacob 	if (notify_cam == 0) {
1664387d8239SMatt Jacob 		if (atp->sendst) {
1665387d8239SMatt Jacob 			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
1666387d8239SMatt Jacob 		}
16672df76c16SMatt Jacob 		return;
1668f48ce188SMatt Jacob 	}
1669d81ba9d5SMatt Jacob 
1670387d8239SMatt Jacob 	/*
167187de303cSAlexander Motin 	 * We are done with this ATIO if we successfully sent status.
167287de303cSAlexander Motin 	 * In all other cases expect either another CTIO or XPT_ABORT.
16738290ea90SAlexander Motin 	 */
167487de303cSAlexander Motin 	if (ok && sentstatus)
16758290ea90SAlexander Motin 		isp_put_atpd(isp, bus, atp);
16768290ea90SAlexander Motin 
16778290ea90SAlexander Motin 	/*
1678387d8239SMatt Jacob 	 * We're telling CAM we're done with this CTIO transaction.
1679387d8239SMatt Jacob 	 *
1680387d8239SMatt Jacob 	 * 24XX cards never need an ATIO put back.
1681387d8239SMatt Jacob 	 */
16820f99cb55SAlexander Motin 	isp_complete_ctio(isp, ccb);
1683d81ba9d5SMatt Jacob }
1684570c7a3fSMatt Jacob 
16852df76c16SMatt Jacob static int
isp_handle_platform_target_notify_ack(ispsoftc_t * isp,isp_notify_t * mp,uint32_t rsp)168696b5475bSAlexander Motin isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
16872df76c16SMatt Jacob {
16881b760be4SAlexander Motin 	ct7_entry_t local, *cto = &local;
16892df76c16SMatt Jacob 
16902df76c16SMatt Jacob 	if (isp->isp_state != ISP_RUNSTATE) {
16912df76c16SMatt 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);
1692570c7a3fSMatt Jacob 		return (0);
1693570c7a3fSMatt Jacob 	}
16942df76c16SMatt Jacob 
16952df76c16SMatt Jacob 	/*
16962df76c16SMatt Jacob 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
16972df76c16SMatt Jacob 	 */
16981b760be4SAlexander Motin 	if (mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
16992df76c16SMatt Jacob 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
17002df76c16SMatt Jacob 		fcportdb_t *lp;
17012df76c16SMatt Jacob 		uint32_t sid;
17022df76c16SMatt Jacob 		uint16_t nphdl;
17032df76c16SMatt Jacob 
17042df76c16SMatt Jacob 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
1705eea52482SAlexander Motin 		if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
17062df76c16SMatt Jacob 			nphdl = lp->handle;
17072df76c16SMatt Jacob 		} else {
17082df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
17092df76c16SMatt Jacob 		}
17101b760be4SAlexander Motin 		ISP_MEMZERO(cto, sizeof (ct7_entry_t));
17112df76c16SMatt Jacob 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
17122df76c16SMatt Jacob 		cto->ct_header.rqs_entry_count = 1;
17132df76c16SMatt Jacob 		cto->ct_nphdl = nphdl;
17142df76c16SMatt Jacob 		cto->ct_rxid = aep->at_rxid;
17152df76c16SMatt Jacob 		cto->ct_vpidx = mp->nt_channel;
17162df76c16SMatt Jacob 		cto->ct_iid_lo = sid;
17172df76c16SMatt Jacob 		cto->ct_iid_hi = sid >> 16;
17182df76c16SMatt Jacob 		cto->ct_oxid = aep->at_hdr.ox_id;
17192df76c16SMatt Jacob 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
17202df76c16SMatt Jacob 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
172196b5475bSAlexander Motin 		if (rsp != 0) {
172296b5475bSAlexander Motin 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
172396b5475bSAlexander Motin 			cto->rsp.m1.ct_resplen = 4;
172496b5475bSAlexander Motin 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
172596b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[0] = rsp & 0xff;
172696b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
172796b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
172896b5475bSAlexander Motin 			cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
172996b5475bSAlexander Motin 		}
1730156c1ebeSAlexander Motin 		return (isp_send_entry(isp, cto));
17312df76c16SMatt Jacob 	}
17322df76c16SMatt Jacob 
17332df76c16SMatt Jacob 	/*
17342df76c16SMatt Jacob 	 * This case is for a responding to an ABTS frame
17352df76c16SMatt Jacob 	 */
17361b760be4SAlexander Motin 	if (mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
17372df76c16SMatt Jacob 
17382df76c16SMatt Jacob 		/*
17392df76c16SMatt Jacob 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
17402df76c16SMatt Jacob 		 */
17412df76c16SMatt Jacob 		if (mp->nt_need_ack) {
17422df76c16SMatt Jacob 			abts_t *abts = (abts_t *)mp->nt_lreserved;
17432df76c16SMatt Jacob 
17442df76c16SMatt Jacob 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
17452df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
17462df76c16SMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
17472df76c16SMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
17482df76c16SMatt Jacob 			cto->ct_nphdl = mp->nt_nphdl;
17492df76c16SMatt Jacob 			cto->ct_rxid = abts->abts_rxid_task;
17502df76c16SMatt Jacob 			cto->ct_iid_lo = mp->nt_sid;
17512df76c16SMatt Jacob 			cto->ct_iid_hi = mp->nt_sid >> 16;
17522df76c16SMatt Jacob 			cto->ct_oxid = abts->abts_ox_id;
17532df76c16SMatt Jacob 			cto->ct_vpidx = mp->nt_channel;
17542df76c16SMatt Jacob 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
1755156c1ebeSAlexander Motin 			if (isp_send_entry(isp, cto)) {
17562df76c16SMatt Jacob 				return (ENOMEM);
17572df76c16SMatt Jacob 			}
17582df76c16SMatt Jacob 			mp->nt_need_ack = 0;
17592df76c16SMatt Jacob 		}
1760b760d2ecSAlexander Motin 		return (isp_acknak_abts(isp, mp->nt_lreserved, 0));
17612df76c16SMatt Jacob 	}
17622df76c16SMatt Jacob 
17632df76c16SMatt Jacob 	/*
17642df76c16SMatt Jacob 	 * General purpose acknowledgement
17652df76c16SMatt Jacob 	 */
17662df76c16SMatt Jacob 	if (mp->nt_need_ack) {
17672df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
1768387d8239SMatt Jacob 		/*
1769387d8239SMatt Jacob 		 * Don't need to use the guaranteed send because the caller can retry
1770387d8239SMatt Jacob 		 */
17712df76c16SMatt Jacob 		return (isp_notify_ack(isp, mp->nt_lreserved));
17722df76c16SMatt Jacob 	}
17732df76c16SMatt Jacob 	return (0);
17742df76c16SMatt Jacob }
17752df76c16SMatt Jacob 
17762df76c16SMatt Jacob /*
1777b1ce21c6SRebecca Cran  * Handle task management functions.
17782df76c16SMatt Jacob  *
17792df76c16SMatt Jacob  * We show up here with a notify structure filled out.
17802df76c16SMatt Jacob  *
17812df76c16SMatt Jacob  * The nt_lreserved tag points to the original queue entry
17822df76c16SMatt Jacob  */
17832df76c16SMatt Jacob static void
isp_handle_platform_target_tmf(ispsoftc_t * isp,isp_notify_t * notify)17842df76c16SMatt Jacob isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
17852df76c16SMatt Jacob {
17862df76c16SMatt Jacob 	tstate_t *tptr;
17872df76c16SMatt Jacob 	fcportdb_t *lp;
17882df76c16SMatt Jacob 	struct ccb_immediate_notify *inot;
17892df76c16SMatt Jacob 	inot_private_data_t *ntp = NULL;
1790981ffc4eSAlexander Motin 	atio_private_data_t *atp;
17912df76c16SMatt Jacob 	lun_id_t lun;
17922df76c16SMatt Jacob 
179315c62456SAlexander Motin 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
17942df76c16SMatt Jacob 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
17952df76c16SMatt Jacob 	if (notify->nt_lun == LUN_ANY) {
1796981ffc4eSAlexander Motin 		if (notify->nt_tagval == TAG_ANY) {
17972df76c16SMatt Jacob 			lun = CAM_LUN_WILDCARD;
17982df76c16SMatt Jacob 		} else {
1799981ffc4eSAlexander Motin 			atp = isp_find_atpd(isp, notify->nt_channel,
1800981ffc4eSAlexander Motin 			    notify->nt_tagval & 0xffffffff);
1801981ffc4eSAlexander Motin 			lun = atp ? atp->lun : CAM_LUN_WILDCARD;
1802981ffc4eSAlexander Motin 		}
1803981ffc4eSAlexander Motin 	} else {
18042df76c16SMatt Jacob 		lun = notify->nt_lun;
18052df76c16SMatt Jacob 	}
18062df76c16SMatt Jacob 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
18072df76c16SMatt Jacob 	if (tptr == NULL) {
18082df76c16SMatt Jacob 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
18092df76c16SMatt Jacob 		if (tptr == NULL) {
1810123055f0SNathan Whitehorn 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
18112df76c16SMatt Jacob 			goto bad;
18122df76c16SMatt Jacob 		}
18132df76c16SMatt Jacob 	}
18142df76c16SMatt Jacob 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
18152df76c16SMatt Jacob 	if (inot == NULL) {
1816123055f0SNathan Whitehorn 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
18172df76c16SMatt Jacob 		goto bad;
18182df76c16SMatt Jacob 	}
18192df76c16SMatt Jacob 
18206ddb8972SAlexander Motin 	inot->ccb_h.target_id = ISP_MAX_TARGETS(isp);
18216ddb8972SAlexander Motin 	inot->ccb_h.target_lun = lun;
1822eea52482SAlexander Motin 	if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
1823d4f3ad3aSAlexander Motin 	    isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
18242df76c16SMatt Jacob 		inot->initiator_id = CAM_TARGET_WILDCARD;
18252df76c16SMatt Jacob 	} else {
1826d4f3ad3aSAlexander Motin 		inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
18272df76c16SMatt Jacob 	}
18282df76c16SMatt Jacob 	inot->seq_id = notify->nt_tagval;
18292df76c16SMatt Jacob 	inot->tag_id = notify->nt_tagval >> 32;
18302df76c16SMatt Jacob 
18312df76c16SMatt Jacob 	switch (notify->nt_ncode) {
18322df76c16SMatt Jacob 	case NT_ABORT_TASK:
18338290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
18342df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK;
18352df76c16SMatt Jacob 		break;
18362df76c16SMatt Jacob 	case NT_ABORT_TASK_SET:
18378290ea90SAlexander Motin 		isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
18382df76c16SMatt Jacob 		inot->arg = MSG_ABORT_TASK_SET;
18392df76c16SMatt Jacob 		break;
18402df76c16SMatt Jacob 	case NT_CLEAR_ACA:
18412df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_ACA;
18422df76c16SMatt Jacob 		break;
18432df76c16SMatt Jacob 	case NT_CLEAR_TASK_SET:
18442df76c16SMatt Jacob 		inot->arg = MSG_CLEAR_TASK_SET;
18452df76c16SMatt Jacob 		break;
18462df76c16SMatt Jacob 	case NT_LUN_RESET:
18472df76c16SMatt Jacob 		inot->arg = MSG_LOGICAL_UNIT_RESET;
18482df76c16SMatt Jacob 		break;
18492df76c16SMatt Jacob 	case NT_TARGET_RESET:
18502df76c16SMatt Jacob 		inot->arg = MSG_TARGET_RESET;
18512df76c16SMatt Jacob 		break;
1852c98d2b1fSAlexander Motin 	case NT_QUERY_TASK_SET:
1853c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_TASK_SET;
1854c98d2b1fSAlexander Motin 		break;
1855c98d2b1fSAlexander Motin 	case NT_QUERY_ASYNC_EVENT:
1856c98d2b1fSAlexander Motin 		inot->arg = MSG_QUERY_ASYNC_EVENT;
1857c98d2b1fSAlexander Motin 		break;
18582df76c16SMatt Jacob 	default:
1859123055f0SNathan 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);
18602df76c16SMatt Jacob 		goto bad;
18612df76c16SMatt Jacob 	}
18622df76c16SMatt Jacob 
18638290ea90SAlexander Motin 	ntp = isp_get_ntpd(isp, notify->nt_channel);
18642df76c16SMatt Jacob 	if (ntp == NULL) {
18652df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
18662df76c16SMatt Jacob 		goto bad;
18672df76c16SMatt Jacob 	}
18688290ea90SAlexander Motin 	ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
18692df76c16SMatt Jacob 	if (notify->nt_lreserved) {
18708290ea90SAlexander Motin 		ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
18718290ea90SAlexander Motin 		ntp->nt.nt_lreserved = &ntp->data;
18722df76c16SMatt Jacob 	}
18738290ea90SAlexander Motin 	ntp->seq_id = notify->nt_tagval;
18748290ea90SAlexander Motin 	ntp->tag_id = notify->nt_tagval >> 32;
18752df76c16SMatt Jacob 
18762df76c16SMatt Jacob 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1877d10e4cd0SAlexander Motin 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
18782df76c16SMatt Jacob 	inot->ccb_h.status = CAM_MESSAGE_RECV;
18792df76c16SMatt Jacob 	xpt_done((union ccb *)inot);
18802df76c16SMatt Jacob 	return;
18812df76c16SMatt Jacob bad:
188244a2a27aSAlexander Motin 	if (notify->nt_need_ack) {
18832df76c16SMatt Jacob 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
1884387d8239SMatt Jacob 			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
1885387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
1886387d8239SMatt Jacob 			}
18872df76c16SMatt Jacob 		} else {
1888387d8239SMatt Jacob 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
18892df76c16SMatt Jacob 		}
18902df76c16SMatt Jacob 	}
18912df76c16SMatt Jacob }
18922df76c16SMatt Jacob 
18932df76c16SMatt Jacob static void
isp_target_mark_aborted_early(ispsoftc_t * isp,int chan,tstate_t * tptr,uint32_t tag_id)18948290ea90SAlexander Motin isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
18952df76c16SMatt Jacob {
1896a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
1897a6222dd7SAlexander Motin 	atio_private_data_t *atp;
18988290ea90SAlexander Motin 	inot_private_data_t *ntp, *tmp;
18998290ea90SAlexander Motin 	uint32_t this_tag_id;
19002df76c16SMatt Jacob 
19012df76c16SMatt Jacob 	/*
19022df76c16SMatt Jacob 	 * First, clean any commands pending restart
19032df76c16SMatt Jacob 	 */
19048290ea90SAlexander Motin 	STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
19058290ea90SAlexander Motin 		this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
19062df76c16SMatt Jacob 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
19078290ea90SAlexander Motin 			isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
19088290ea90SAlexander Motin 			    ECMD_TERMINATE, 0);
19098290ea90SAlexander Motin 			isp_put_ntpd(isp, chan, ntp);
19108290ea90SAlexander Motin 			STAILQ_REMOVE(&tptr->restart_queue, ntp,
19118290ea90SAlexander Motin 			    inot_private_data, next);
19122df76c16SMatt Jacob 		}
19132df76c16SMatt Jacob 	}
19142df76c16SMatt Jacob 
19152df76c16SMatt Jacob 	/*
19162df76c16SMatt Jacob 	 * Now mark other ones dead as well.
19172df76c16SMatt Jacob 	 */
1918a6222dd7SAlexander Motin 	for (atp = fc->atpool; atp < &fc->atpool[ATPDPSIZE]; atp++) {
19198290ea90SAlexander Motin 		if (atp->lun != tptr->ts_lun)
19208290ea90SAlexander Motin 			continue;
19218290ea90SAlexander Motin 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
19222df76c16SMatt Jacob 			atp->dead = 1;
19232df76c16SMatt Jacob 	}
19242df76c16SMatt Jacob }
1925d81ba9d5SMatt Jacob #endif
1926d81ba9d5SMatt Jacob 
1927478f8a96SJustin T. Gibbs static void
isp_poll(struct cam_sim * sim)1928c3055363SMatt Jacob isp_poll(struct cam_sim *sim)
1929478f8a96SJustin T. Gibbs {
19309cd7268eSMatt Jacob 	ispsoftc_t *isp = cam_sim_softc(sim);
1931126ec864SMatt Jacob 
19320e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
1933478f8a96SJustin T. Gibbs }
1934478f8a96SJustin T. Gibbs 
1935ab6c4b31SMatt Jacob 
19362df76c16SMatt Jacob static void
isp_watchdog(void * arg)19372df76c16SMatt Jacob isp_watchdog(void *arg)
1938cc8df88bSMatt Jacob {
19392df76c16SMatt Jacob 	struct ccb_scsiio *xs = arg;
19402df76c16SMatt Jacob 	ispsoftc_t *isp;
1941e95725cbSMatt Jacob 	uint32_t ohandle = ISP_HANDLE_FREE, handle;
1942b85389e1SMatt Jacob 
19432df76c16SMatt Jacob 	isp = XS_ISP(xs);
19442df76c16SMatt Jacob 
1945cc8df88bSMatt Jacob 	handle = isp_find_handle(isp, xs);
1946e95725cbSMatt Jacob 
1947e95725cbSMatt Jacob 	/*
1948e95725cbSMatt Jacob 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
1949e95725cbSMatt Jacob 	 */
1950e95725cbSMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
19510e6bc811SAlexander Motin 		ISP_RUN_ISR(isp);
1952e95725cbSMatt Jacob 		ohandle = handle;
1953e95725cbSMatt Jacob 		handle = isp_find_handle(isp, xs);
1954e95725cbSMatt Jacob 	}
1955c8b8a2c4SMatt Jacob 	if (handle != ISP_HANDLE_FREE) {
19561fcf5debSMatt Jacob 		/*
1957c8b8a2c4SMatt Jacob 		 * Try and make sure the command is really dead before
1958c8b8a2c4SMatt Jacob 		 * we release the handle (and DMA resources) for reuse.
1959c8b8a2c4SMatt Jacob 		 *
1960c8b8a2c4SMatt Jacob 		 * If we are successful in aborting the command then
1961c8b8a2c4SMatt Jacob 		 * we're done here because we'll get the command returned
1962c8b8a2c4SMatt Jacob 		 * back separately.
19631fcf5debSMatt Jacob 		 */
1964c8b8a2c4SMatt Jacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
1965c8b8a2c4SMatt Jacob 			return;
1966c8b8a2c4SMatt Jacob 		}
19671fcf5debSMatt Jacob 
19681fcf5debSMatt Jacob 		/*
1969c8b8a2c4SMatt Jacob 		 * Note that after calling the above, the command may in
1970c8b8a2c4SMatt Jacob 		 * fact have been completed.
1971c8b8a2c4SMatt Jacob 		 */
1972c8b8a2c4SMatt Jacob 		xs = isp_find_xs(isp, handle);
1973c8b8a2c4SMatt Jacob 
1974c8b8a2c4SMatt Jacob 		/*
1975c8b8a2c4SMatt Jacob 		 * If the command no longer exists, then we won't
1976c8b8a2c4SMatt Jacob 		 * be able to find the xs again with this handle.
1977c8b8a2c4SMatt Jacob 		 */
1978c8b8a2c4SMatt Jacob 		if (xs == NULL) {
1979c8b8a2c4SMatt Jacob 			return;
1980c8b8a2c4SMatt Jacob 		}
1981c8b8a2c4SMatt Jacob 
1982c8b8a2c4SMatt Jacob 		/*
1983c8b8a2c4SMatt Jacob 		 * After this point, the command is really dead.
19841fcf5debSMatt Jacob 		 */
1985f6854a0cSAlexander Motin 		ISP_DMAFREE(isp, xs);
1986cc8df88bSMatt Jacob 		isp_destroy_handle(isp, handle);
1987c8b8a2c4SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
198857b6261fSKenneth D. Merry 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
1989cc8df88bSMatt Jacob 		isp_done(xs);
1990e95725cbSMatt Jacob 	} else {
1991e95725cbSMatt Jacob 		if (ohandle != ISP_HANDLE_FREE) {
1992e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
1993e95725cbSMatt Jacob 		} else {
1994e95725cbSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
1995e95725cbSMatt Jacob 		}
1996b85389e1SMatt Jacob 	}
1997f7c631bcSMatt Jacob }
1998f7c631bcSMatt Jacob 
1999f7c631bcSMatt Jacob static void
isp_make_here(ispsoftc_t * isp,fcportdb_t * fcp,int chan,int tgt)20005704e6f0SKenneth D. Merry isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2001f7c631bcSMatt Jacob {
2002ffcf6651SMatt Jacob 	union ccb *ccb;
20032df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
20042df76c16SMatt Jacob 
2005ffcf6651SMatt Jacob 	/*
20060e85f214SMatt Jacob 	 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2007ffcf6651SMatt Jacob 	 */
20088008a935SScott Long 	ccb = xpt_alloc_ccb_nowait();
2009ffcf6651SMatt Jacob 	if (ccb == NULL) {
20102df76c16SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2011ffcf6651SMatt Jacob 		return;
2012ffcf6651SMatt Jacob 	}
2013e5dfa058SAlexander Motin 	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2014e5dfa058SAlexander Motin 	    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2015ffcf6651SMatt Jacob 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2016ffcf6651SMatt Jacob 		xpt_free_ccb(ccb);
2017ffcf6651SMatt Jacob 		return;
2018ffcf6651SMatt Jacob 	}
2019ffcf6651SMatt Jacob 	xpt_rescan(ccb);
2020ffcf6651SMatt Jacob }
2021ffcf6651SMatt Jacob 
2022ffcf6651SMatt Jacob static void
isp_make_gone(ispsoftc_t * isp,fcportdb_t * fcp,int chan,int tgt)20235704e6f0SKenneth D. Merry isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2024ffcf6651SMatt Jacob {
2025ffcf6651SMatt Jacob 	struct cam_path *tp;
20262df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
20272df76c16SMatt Jacob 
20282df76c16SMatt Jacob 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2029ffcf6651SMatt Jacob 		xpt_async(AC_LOST_DEVICE, tp, NULL);
2030ffcf6651SMatt Jacob 		xpt_free_path(tp);
2031f7c631bcSMatt Jacob 	}
2032f7c631bcSMatt Jacob }
2033f7c631bcSMatt Jacob 
2034f7c631bcSMatt Jacob /*
2035f7c631bcSMatt Jacob  * Gone Device Timer Function- when we have decided that a device has gone
2036f7c631bcSMatt Jacob  * away, we wait a specific period of time prior to telling the OS it has
2037f7c631bcSMatt Jacob  * gone away.
2038f7c631bcSMatt Jacob  *
2039f7c631bcSMatt Jacob  * This timer function fires once a second and then scans the port database
2040f7c631bcSMatt Jacob  * for devices that are marked dead but still have a virtual target assigned.
2041f7c631bcSMatt Jacob  * We decrement a counter for that port database entry, and when it hits zero,
2042f7c631bcSMatt Jacob  * we tell the OS the device has gone away.
2043f7c631bcSMatt Jacob  */
2044f7c631bcSMatt Jacob static void
isp_gdt(void * arg)2045f7c631bcSMatt Jacob isp_gdt(void *arg)
2046f7c631bcSMatt Jacob {
20472df76c16SMatt Jacob 	struct isp_fc *fc = arg;
2048de461933SMatt Jacob 	taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2049de461933SMatt Jacob }
2050de461933SMatt Jacob 
2051de461933SMatt Jacob static void
isp_gdt_task(void * arg,int pending)2052de461933SMatt Jacob isp_gdt_task(void *arg, int pending)
2053de461933SMatt Jacob {
2054de461933SMatt Jacob 	struct isp_fc *fc = arg;
20552df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
2056a6222dd7SAlexander Motin 	int chan = fc - ISP_FC_PC(isp, 0);
2057f7c631bcSMatt Jacob 	fcportdb_t *lp;
2058e68eef14SAlexander Motin 	struct ac_contract ac;
2059e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2060766a65a5SAlexander Motin 	int dbidx, more_to_do = 0;
2061f7c631bcSMatt Jacob 
2062de461933SMatt Jacob 	ISP_LOCK(isp);
2063de461933SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2064f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
20652df76c16SMatt Jacob 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
2066f7c631bcSMatt Jacob 
2067f7c631bcSMatt Jacob 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2068f7c631bcSMatt Jacob 			continue;
2069f7c631bcSMatt Jacob 		}
2070427fa8f9SMatt Jacob 		if (lp->gone_timer != 0) {
2071427fa8f9SMatt Jacob 			lp->gone_timer -= 1;
2072f7c631bcSMatt Jacob 			more_to_do++;
2073f7c631bcSMatt Jacob 			continue;
2074f7c631bcSMatt Jacob 		}
2075e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2076e68eef14SAlexander Motin 		if (lp->is_target) {
2077e68eef14SAlexander Motin 			lp->is_target = 0;
2078766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2079f7c631bcSMatt Jacob 		}
2080e68eef14SAlexander Motin 		if (lp->is_initiator) {
2081e68eef14SAlexander Motin 			lp->is_initiator = 0;
2082e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2083e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2084e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2085e68eef14SAlexander Motin 			adc->port = lp->portid;
2086d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2087e68eef14SAlexander Motin 			adc->arrived = 0;
2088e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2089e68eef14SAlexander Motin 		}
2090e68eef14SAlexander Motin 		lp->state = FC_PORTDB_STATE_NIL;
2091e68eef14SAlexander Motin 	}
2092a01f5aebSMatt Jacob 	if (fc->ready) {
2093f7c631bcSMatt Jacob 		if (more_to_do) {
20942df76c16SMatt Jacob 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
2095f7c631bcSMatt Jacob 		} else {
2096de461933SMatt Jacob 			callout_deactivate(&fc->gdt);
2097387d8239SMatt Jacob 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2098a01f5aebSMatt Jacob 		}
2099f7c631bcSMatt Jacob 	}
2100de461933SMatt Jacob 	ISP_UNLOCK(isp);
2101f7c631bcSMatt Jacob }
2102f7c631bcSMatt Jacob 
2103f7c631bcSMatt Jacob /*
2104e561aa79SAlexander Motin  * When loop goes down we remember the time and freeze CAM command queue.
2105e561aa79SAlexander Motin  * During some time period we are trying to reprobe the loop.  But if we
2106e561aa79SAlexander Motin  * fail, we tell the OS that devices have gone away and drop the freeze.
2107f7c631bcSMatt Jacob  *
2108f7c631bcSMatt Jacob  * We don't clear the devices out of our port database because, when loop
2109f7c631bcSMatt Jacob  * come back up, we have to do some actual cleanup with the chip at that
2110f7c631bcSMatt Jacob  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2111f7c631bcSMatt Jacob  */
2112f7c631bcSMatt Jacob static void
isp_loop_changed(ispsoftc_t * isp,int chan)2113e561aa79SAlexander Motin isp_loop_changed(ispsoftc_t *isp, int chan)
2114f7c631bcSMatt Jacob {
2115e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2116e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2117e561aa79SAlexander Motin 
2118e561aa79SAlexander Motin 	if (fc->loop_down_time)
2119e561aa79SAlexander Motin 		return;
2120e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2121e561aa79SAlexander Motin 	if (fcp->role & ISP_ROLE_INITIATOR)
2122e561aa79SAlexander Motin 		isp_freeze_loopdown(isp, chan);
2123e561aa79SAlexander Motin 	fc->loop_down_time = time_uptime;
2124e561aa79SAlexander Motin 	wakeup(fc);
2125de461933SMatt Jacob }
2126de461933SMatt Jacob 
2127de461933SMatt Jacob static void
isp_loop_up(ispsoftc_t * isp,int chan)2128e561aa79SAlexander Motin isp_loop_up(ispsoftc_t *isp, int chan)
2129de461933SMatt Jacob {
2130e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2131e561aa79SAlexander Motin 
2132e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2133e561aa79SAlexander Motin 	fc->loop_seen_once = 1;
2134e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2135e561aa79SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2136e561aa79SAlexander Motin }
2137e561aa79SAlexander Motin 
2138e561aa79SAlexander Motin static void
isp_loop_dead(ispsoftc_t * isp,int chan)2139e561aa79SAlexander Motin isp_loop_dead(ispsoftc_t *isp, int chan)
2140e561aa79SAlexander Motin {
2141e561aa79SAlexander Motin 	fcparam *fcp = FCPARAM(isp, chan);
2142e561aa79SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
2143f7c631bcSMatt Jacob 	fcportdb_t *lp;
2144e68eef14SAlexander Motin 	struct ac_contract ac;
2145e68eef14SAlexander Motin 	struct ac_device_changed *adc;
2146766a65a5SAlexander Motin 	int dbidx, i;
2147f7c631bcSMatt Jacob 
2148e561aa79SAlexander Motin 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
21490a70657fSMatt Jacob 
2150f7c631bcSMatt Jacob 	/*
2151f7c631bcSMatt Jacob 	 * Notify to the OS all targets who we now consider have departed.
2152f7c631bcSMatt Jacob 	 */
2153f7c631bcSMatt Jacob 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2154e561aa79SAlexander Motin 		lp = &fcp->portdb[dbidx];
2155f7c631bcSMatt Jacob 
2156e68eef14SAlexander Motin 		if (lp->state == FC_PORTDB_STATE_NIL)
2157f7c631bcSMatt Jacob 			continue;
2158f7c631bcSMatt Jacob 
21590b19f90aSAlexander Motin 		for (i = 0; i < ISP_HANDLE_NUM(isp); i++) {
2160e95725cbSMatt Jacob 			struct ccb_scsiio *xs;
2161e95725cbSMatt Jacob 
2162970ceb2fSAlexander Motin 			if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2163e95725cbSMatt Jacob 				continue;
2164e95725cbSMatt Jacob 			}
2165e95725cbSMatt Jacob 			if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2166e95725cbSMatt Jacob 				continue;
2167e95725cbSMatt Jacob                         }
2168766a65a5SAlexander Motin 			if (dbidx != XS_TGT(xs)) {
2169e95725cbSMatt Jacob 				continue;
2170e95725cbSMatt Jacob 			}
21716af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
21726af11b82SAlexander Motin 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
21736af11b82SAlexander Motin 			    (uintmax_t)XS_LUN(xs));
217457b6261fSKenneth D. Merry 
217557b6261fSKenneth D. Merry 			/*
217657b6261fSKenneth D. Merry 			 * Just like in isp_watchdog, abort the outstanding
217757b6261fSKenneth D. Merry 			 * command or immediately free its resources if it is
217857b6261fSKenneth D. Merry 			 * not active
217957b6261fSKenneth D. Merry 			 */
218057b6261fSKenneth D. Merry 			if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
218157b6261fSKenneth D. Merry 				continue;
218257b6261fSKenneth D. Merry 			}
218357b6261fSKenneth D. Merry 
2184f6854a0cSAlexander Motin 			ISP_DMAFREE(isp, xs);
218557b6261fSKenneth D. Merry 			isp_destroy_handle(isp, isp->isp_xflist[i].handle);
218657b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx could not be aborted and was destroyed",
218757b6261fSKenneth D. Merry 			    isp->isp_xflist[i].handle, chan, XS_TGT(xs),
218857b6261fSKenneth D. Merry 			    (uintmax_t)XS_LUN(xs));
218957b6261fSKenneth D. Merry 			XS_SETERR(xs, HBA_BUSRESET);
219057b6261fSKenneth D. Merry 			isp_done(xs);
2191e95725cbSMatt Jacob 		}
2192e95725cbSMatt Jacob 
2193e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2194e68eef14SAlexander Motin 		if (lp->is_target) {
2195e68eef14SAlexander Motin 			lp->is_target = 0;
2196766a65a5SAlexander Motin 			isp_make_gone(isp, lp, chan, dbidx);
2197f7c631bcSMatt Jacob 		}
2198e68eef14SAlexander Motin 		if (lp->is_initiator) {
2199e68eef14SAlexander Motin 			lp->is_initiator = 0;
2200e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2201e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2202e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2203e68eef14SAlexander Motin 			adc->port = lp->portid;
2204d4f3ad3aSAlexander Motin 			adc->target = dbidx;
2205e68eef14SAlexander Motin 			adc->arrived = 0;
2206e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2207e95725cbSMatt Jacob 		}
2208e68eef14SAlexander Motin 	}
2209e68eef14SAlexander Motin 
2210e68eef14SAlexander Motin 	isp_unfreeze_loopdown(isp, chan);
2211e561aa79SAlexander Motin 	fc->loop_down_time = 0;
2212cc8df88bSMatt Jacob }
2213cc8df88bSMatt Jacob 
22145d571944SMatt Jacob static void
isp_kthread(void * arg)22155d571944SMatt Jacob isp_kthread(void *arg)
22165d571944SMatt Jacob {
22172df76c16SMatt Jacob 	struct isp_fc *fc = arg;
22182df76c16SMatt Jacob 	ispsoftc_t *isp = fc->isp;
2219a6222dd7SAlexander Motin 	int chan = fc - ISP_FC_PC(isp, 0);
2220e561aa79SAlexander Motin 	int slp = 0, d;
2221e561aa79SAlexander Motin 	int lb, lim;
2222a01f5aebSMatt Jacob 
222313d9c921SAlexander Motin 	ISP_LOCK(isp);
22241d0a1de2SWill Andrews 	while (isp->isp_osinfo.is_exiting == 0) {
2225e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2226e561aa79SAlexander Motin 		    "Chan %d Checking FC state", chan);
22272df76c16SMatt Jacob 		lb = isp_fc_runstate(isp, chan, 250000);
2228e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2229e561aa79SAlexander Motin 		    "Chan %d FC got to %s state", chan,
2230e561aa79SAlexander Motin 		    isp_fc_loop_statename(lb));
22312df76c16SMatt Jacob 
22322df76c16SMatt Jacob 		/*
22332df76c16SMatt Jacob 		 * Our action is different based upon whether we're supporting
22342df76c16SMatt Jacob 		 * Initiator mode or not. If we are, we might freeze the simq
22352df76c16SMatt Jacob 		 * when loop is down and set all sorts of different delays to
22362df76c16SMatt Jacob 		 * check again.
22372df76c16SMatt Jacob 		 *
22382df76c16SMatt Jacob 		 * If not, we simply just wait for loop to come up.
22392df76c16SMatt Jacob 		 */
2240e561aa79SAlexander Motin 		if (lb == LOOP_READY || lb < 0) {
2241e561aa79SAlexander Motin 			slp = 0;
224210365e5aSMatt Jacob 		} else {
224310365e5aSMatt Jacob 			/*
224410365e5aSMatt Jacob 			 * If we've never seen loop up and we've waited longer
2245f7c631bcSMatt Jacob 			 * than quickboot time, or we've seen loop up but we've
2246f7c631bcSMatt Jacob 			 * waited longer than loop_down_limit, give up and go
2247f7c631bcSMatt Jacob 			 * to sleep until loop comes up.
224810365e5aSMatt Jacob 			 */
2249e561aa79SAlexander Motin 			if (fc->loop_seen_once == 0)
2250f7c631bcSMatt Jacob 				lim = isp_quickboot_time;
2251e561aa79SAlexander Motin 			else
22522df76c16SMatt Jacob 				lim = fc->loop_down_limit;
2253e561aa79SAlexander Motin 			d = time_uptime - fc->loop_down_time;
2254e561aa79SAlexander Motin 			if (d >= lim)
2255f7c631bcSMatt Jacob 				slp = 0;
2256e561aa79SAlexander Motin 			else if (d < 10)
225710365e5aSMatt Jacob 				slp = 1;
2258e561aa79SAlexander Motin 			else if (d < 30)
2259f7c631bcSMatt Jacob 				slp = 5;
2260e561aa79SAlexander Motin 			else if (d < 60)
2261f7c631bcSMatt Jacob 				slp = 10;
2262e561aa79SAlexander Motin 			else if (d < 120)
2263f7c631bcSMatt Jacob 				slp = 20;
2264e561aa79SAlexander Motin 			else
2265f7c631bcSMatt Jacob 				slp = 30;
2266f44257c2SMatt Jacob 		}
226710365e5aSMatt Jacob 
2268e561aa79SAlexander Motin 		if (slp == 0) {
2269e561aa79SAlexander Motin 			if (lb == LOOP_READY)
2270e561aa79SAlexander Motin 				isp_loop_up(isp, chan);
2271387d8239SMatt Jacob 			else
2272e561aa79SAlexander Motin 				isp_loop_dead(isp, chan);
22735d571944SMatt Jacob 		}
2274fdeb9f2fSMatt Jacob 
2275e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2276e561aa79SAlexander Motin 		    "Chan %d sleep for %d seconds", chan, slp);
227713d9c921SAlexander Motin 		msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz);
22785d571944SMatt Jacob 	}
22791d0a1de2SWill Andrews 	fc->num_threads -= 1;
2280a6222dd7SAlexander Motin 	wakeup(&fc->num_threads);
228113d9c921SAlexander Motin 	ISP_UNLOCK(isp);
22821d0a1de2SWill Andrews 	kthread_exit();
22835d571944SMatt Jacob }
22845d571944SMatt Jacob 
22858290ea90SAlexander Motin #ifdef	ISP_TARGET_MODE
22868290ea90SAlexander Motin static void
isp_abort_atio(ispsoftc_t * isp,union ccb * ccb)22878290ea90SAlexander Motin isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
22888290ea90SAlexander Motin {
22898290ea90SAlexander Motin 	atio_private_data_t *atp;
22908290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
22918290ea90SAlexander Motin 	struct ccb_hdr *sccb;
22928290ea90SAlexander Motin 	tstate_t *tptr;
22938290ea90SAlexander Motin 
22948290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
22958290ea90SAlexander Motin 	if (tptr != NULL) {
22968290ea90SAlexander Motin 		/* Search for the ATIO among queueued. */
22978290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
22988290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
22998290ea90SAlexander Motin 				continue;
23008290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2301d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2302d10e4cd0SAlexander Motin 			    "Abort FREE ATIO\n");
23038290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
23048290ea90SAlexander Motin 			xpt_done(accb);
23058290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
23068290ea90SAlexander Motin 			return;
23078290ea90SAlexander Motin 		}
23088290ea90SAlexander Motin 	}
23098290ea90SAlexander Motin 
23108290ea90SAlexander Motin 	/* Search for the ATIO among running. */
23118290ea90SAlexander Motin 	atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
23128290ea90SAlexander Motin 	if (atp != NULL) {
23133f072d69SAlexander Motin 		/* Send TERMINATE to firmware. */
23141b760be4SAlexander Motin 		if (!atp->dead) {
23153f072d69SAlexander Motin 			uint8_t storage[QENTRY_LEN];
23163f072d69SAlexander Motin 			ct7_entry_t *cto = (ct7_entry_t *) storage;
23173f072d69SAlexander Motin 
23183f072d69SAlexander Motin 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
23193f072d69SAlexander Motin 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
23203f072d69SAlexander Motin 			cto->ct_header.rqs_entry_count = 1;
23213f072d69SAlexander Motin 			cto->ct_nphdl = atp->nphdl;
23223f072d69SAlexander Motin 			cto->ct_rxid = atp->tag;
23233f072d69SAlexander Motin 			cto->ct_iid_lo = atp->sid;
23243f072d69SAlexander Motin 			cto->ct_iid_hi = atp->sid >> 16;
23253f072d69SAlexander Motin 			cto->ct_oxid = atp->oxid;
23263f072d69SAlexander Motin 			cto->ct_vpidx = XS_CHANNEL(accb);
23273f072d69SAlexander Motin 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2328156c1ebeSAlexander Motin 			isp_send_entry(isp, cto);
23293f072d69SAlexander Motin 		}
23308290ea90SAlexander Motin 		isp_put_atpd(isp, XS_CHANNEL(accb), atp);
23318290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
23328290ea90SAlexander Motin 	} else {
23338290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
23348290ea90SAlexander Motin 	}
23358290ea90SAlexander Motin }
23368290ea90SAlexander Motin 
23378290ea90SAlexander Motin static void
isp_abort_inot(ispsoftc_t * isp,union ccb * ccb)23388290ea90SAlexander Motin isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
23398290ea90SAlexander Motin {
23408290ea90SAlexander Motin 	inot_private_data_t *ntp;
23418290ea90SAlexander Motin 	union ccb *accb = ccb->cab.abort_ccb;
23428290ea90SAlexander Motin 	struct ccb_hdr *sccb;
23438290ea90SAlexander Motin 	tstate_t *tptr;
23448290ea90SAlexander Motin 
23458290ea90SAlexander Motin 	tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
23468290ea90SAlexander Motin 	if (tptr != NULL) {
23478290ea90SAlexander Motin 		/* Search for the INOT among queueued. */
23488290ea90SAlexander Motin 		SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
23498290ea90SAlexander Motin 			if (sccb != &accb->ccb_h)
23508290ea90SAlexander Motin 				continue;
23518290ea90SAlexander Motin 			SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2352d10e4cd0SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2353d10e4cd0SAlexander Motin 			    "Abort FREE INOT\n");
23548290ea90SAlexander Motin 			accb->ccb_h.status = CAM_REQ_ABORTED;
23558290ea90SAlexander Motin 			xpt_done(accb);
23568290ea90SAlexander Motin 			ccb->ccb_h.status = CAM_REQ_CMP;
23578290ea90SAlexander Motin 			return;
23588290ea90SAlexander Motin 		}
23598290ea90SAlexander Motin 	}
23608290ea90SAlexander Motin 
23618290ea90SAlexander Motin 	/* Search for the INOT among running. */
23628290ea90SAlexander Motin 	ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
23638290ea90SAlexander Motin 	if (ntp != NULL) {
236444a2a27aSAlexander Motin 		if (ntp->nt.nt_need_ack) {
236544a2a27aSAlexander Motin 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
236644a2a27aSAlexander Motin 			    ntp->nt.nt_lreserved);
236744a2a27aSAlexander Motin 		}
23688290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
23698290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
23708290ea90SAlexander Motin 	} else {
23718290ea90SAlexander Motin 		ccb->ccb_h.status = CAM_UA_ABORT;
23728290ea90SAlexander Motin 		return;
23738290ea90SAlexander Motin 	}
23748290ea90SAlexander Motin }
23758290ea90SAlexander Motin #endif
23768290ea90SAlexander Motin 
2377cc8df88bSMatt Jacob static void
isp_action(struct cam_sim * sim,union ccb * ccb)2378c3055363SMatt Jacob isp_action(struct cam_sim *sim, union ccb *ccb)
2379478f8a96SJustin T. Gibbs {
238031c161a6SAlexander Motin 	int bus, tgt, error;
23819cd7268eSMatt Jacob 	ispsoftc_t *isp;
23821b760be4SAlexander Motin 	fcparam *fcp;
23834663e367SJustin T. Gibbs 	struct ccb_trans_settings *cts;
238431c161a6SAlexander Motin 	sbintime_t ts;
2385478f8a96SJustin T. Gibbs 
2386478f8a96SJustin T. Gibbs 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2387478f8a96SJustin T. Gibbs 
23889cd7268eSMatt Jacob 	isp = (ispsoftc_t *)cam_sim_softc(sim);
238913d9c921SAlexander Motin 	ISP_ASSERT_LOCKED(isp);
239013d9c921SAlexander Motin 	bus = cam_sim_bus(sim);
23915cbe3e8eSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
23925cbe3e8eSAlexander Motin 	ISP_PCMD(ccb) = NULL;
23932df76c16SMatt Jacob 
2394478f8a96SJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
2395478f8a96SJustin T. Gibbs 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2396478f8a96SJustin T. Gibbs 		/*
2397478f8a96SJustin T. Gibbs 		 * Do a couple of preliminary checks...
2398478f8a96SJustin T. Gibbs 		 */
2399478f8a96SJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2400478f8a96SJustin T. Gibbs 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2401478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_INVALID;
24025cbe3e8eSAlexander Motin 				isp_done((struct ccb_scsiio *) ccb);
2403478f8a96SJustin T. Gibbs 				break;
2404478f8a96SJustin T. Gibbs 			}
2405478f8a96SJustin T. Gibbs 		}
24060470d791SMatt Jacob #ifdef	DIAGNOSTIC
24076af11b82SAlexander Motin 		if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
2408dd9fc7c3SMatt Jacob 			xpt_print(ccb->ccb_h.path, "invalid target\n");
2409478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_PATH_INVALID;
2410478f8a96SJustin T. Gibbs 		}
2411478f8a96SJustin T. Gibbs 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2412478f8a96SJustin T. Gibbs 			xpt_done(ccb);
2413478f8a96SJustin T. Gibbs 			break;
2414478f8a96SJustin T. Gibbs 		}
24150470d791SMatt Jacob #endif
24160a70657fSMatt Jacob 		ccb->csio.scsi_status = SCSI_STATUS_OK;
24170a70657fSMatt Jacob 		if (isp_get_pcmd(isp, ccb)) {
24180a70657fSMatt Jacob 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
24190a70657fSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
24202df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
24215cbe3e8eSAlexander Motin 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
24220a70657fSMatt Jacob 			xpt_done(ccb);
24230a70657fSMatt Jacob 			break;
24240a70657fSMatt Jacob 		}
2425b09b0095SMatt Jacob 		error = isp_start((XS_T *) ccb);
24260f99cb55SAlexander Motin 		isp_rq_check_above(isp);
24270470d791SMatt Jacob 		switch (error) {
2428f6854a0cSAlexander Motin 		case 0:
2429478f8a96SJustin T. Gibbs 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
243031c161a6SAlexander Motin 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
24310a70657fSMatt Jacob 				break;
243231c161a6SAlexander Motin 			/* Give firmware extra 10s to handle timeout. */
243331c161a6SAlexander Motin 			ts = SBT_1MS * ccb->ccb_h.timeout + 10 * SBT_1S;
243431c161a6SAlexander Motin 			callout_reset_sbt(&PISP_PCMD(ccb)->wdog, ts, 0,
243531c161a6SAlexander Motin 			    isp_watchdog, ccb, 0);
2436478f8a96SJustin T. Gibbs 			break;
24370470d791SMatt Jacob 		case CMD_RQLATER:
24386af11b82SAlexander Motin 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
24396af11b82SAlexander Motin 			    XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
2440f7c631bcSMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
24412df76c16SMatt Jacob 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
2442387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
24430a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
2444478f8a96SJustin T. Gibbs 			xpt_done(ccb);
2445478f8a96SJustin T. Gibbs 			break;
24460470d791SMatt Jacob 		case CMD_EAGAIN:
24470a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
24482df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
24490f99cb55SAlexander Motin 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 10, 0);
2450387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2451478f8a96SJustin T. Gibbs 			xpt_done(ccb);
2452478f8a96SJustin T. Gibbs 			break;
24530470d791SMatt Jacob 		case CMD_COMPLETE:
24540470d791SMatt Jacob 			isp_done((struct ccb_scsiio *) ccb);
24550470d791SMatt Jacob 			break;
24560470d791SMatt Jacob 		default:
24572df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
2458387d8239SMatt Jacob 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
24590a70657fSMatt Jacob 			isp_free_pcmd(isp, ccb);
24600470d791SMatt Jacob 			xpt_done(ccb);
2461478f8a96SJustin T. Gibbs 		}
2462478f8a96SJustin T. Gibbs 		break;
2463478f8a96SJustin T. Gibbs 
2464d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
24652df76c16SMatt Jacob 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
24662df76c16SMatt Jacob 		if (ccb->cel.enable) {
24672df76c16SMatt Jacob 			isp_enable_lun(isp, ccb);
24682df76c16SMatt Jacob 		} else {
24692df76c16SMatt Jacob 			isp_disable_lun(isp, ccb);
247067ff51f1SMatt Jacob 		}
247167ff51f1SMatt Jacob 		break;
24722df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
2473d81ba9d5SMatt Jacob 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
2474d81ba9d5SMatt Jacob 	{
24752df76c16SMatt Jacob 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2476d81ba9d5SMatt Jacob 		if (tptr == NULL) {
24772df76c16SMatt Jacob 			const char *str;
24782df76c16SMatt Jacob 
24798290ea90SAlexander Motin 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
24802df76c16SMatt Jacob 				str = "XPT_IMMEDIATE_NOTIFY";
24818290ea90SAlexander Motin 			else
24822df76c16SMatt Jacob 				str = "XPT_ACCEPT_TARGET_IO";
24838290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
24845c0b756aSAlexander Motin 			    "%s: no state pointer found for %s\n",
24858290ea90SAlexander Motin 			    __func__, str);
24862df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
24875c0b756aSAlexander Motin 			xpt_done(ccb);
2488d81ba9d5SMatt Jacob 			break;
2489d81ba9d5SMatt Jacob 		}
2490570c7a3fSMatt Jacob 
2491d81ba9d5SMatt Jacob 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
24928290ea90SAlexander Motin 			ccb->atio.tag_id = 0;
24932df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
24948290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
2495d10e4cd0SAlexander Motin 			    "Put FREE ATIO\n");
24962df76c16SMatt Jacob 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
24978290ea90SAlexander Motin 			ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
24982df76c16SMatt Jacob 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
24998290ea90SAlexander Motin 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
2500d10e4cd0SAlexander Motin 			    "Put FREE INOT\n");
2501d81ba9d5SMatt Jacob 		}
2502d81ba9d5SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_INPROG;
2503d81ba9d5SMatt Jacob 		break;
2504d81ba9d5SMatt Jacob 	}
25052df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
2506d81ba9d5SMatt Jacob 	{
25072df76c16SMatt Jacob 		inot_private_data_t *ntp;
25082df76c16SMatt Jacob 
25092df76c16SMatt Jacob 		/*
25102df76c16SMatt Jacob 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
25112df76c16SMatt Jacob 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
25122df76c16SMatt Jacob 		 */
25132df76c16SMatt Jacob 		/*
25142df76c16SMatt Jacob 		 * All the relevant path information is in the associated immediate notify
25152df76c16SMatt Jacob 		 */
25162df76c16SMatt 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);
25178290ea90SAlexander Motin 		ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
25182df76c16SMatt Jacob 		if (ntp == NULL) {
25192df76c16SMatt 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__,
25202df76c16SMatt Jacob 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
25212df76c16SMatt Jacob 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
25222df76c16SMatt Jacob 			xpt_done(ccb);
2523d81ba9d5SMatt Jacob 			break;
2524d81ba9d5SMatt Jacob 		}
25258290ea90SAlexander Motin 		if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
252696b5475bSAlexander Motin 		    (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
25272df76c16SMatt Jacob 			cam_freeze_devq(ccb->ccb_h.path);
2528b760d2ecSAlexander Motin 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 10, 0);
2529387d8239SMatt Jacob 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2530387d8239SMatt Jacob 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
25312df76c16SMatt Jacob 			break;
25322df76c16SMatt Jacob 		}
25338290ea90SAlexander Motin 		isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
25342df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
25352df76c16SMatt 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);
25362df76c16SMatt Jacob 		xpt_done(ccb);
25372df76c16SMatt Jacob 		break;
25382df76c16SMatt Jacob 	}
25392df76c16SMatt Jacob 	case XPT_CONT_TARGET_IO:
2540387d8239SMatt Jacob 		isp_target_start_ctio(isp, ccb, FROM_CAM);
25410f99cb55SAlexander Motin 		isp_rq_check_above(isp);
25422df76c16SMatt Jacob 		break;
2543d81ba9d5SMatt Jacob #endif
2544478f8a96SJustin T. Gibbs 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
2545d81ba9d5SMatt Jacob 		tgt = ccb->ccb_h.target_id;
2546d81ba9d5SMatt Jacob 		tgt |= (bus << 16);
2547d81ba9d5SMatt Jacob 
25482df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
2549478f8a96SJustin T. Gibbs 		if (error) {
2550478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2551478f8a96SJustin T. Gibbs 		} else {
2552b44e442eSWill Andrews 			/*
2553b44e442eSWill Andrews 			 * If we have a FC device, reset the Command
2554b44e442eSWill Andrews 			 * Reference Number, because the target will expect
2555b44e442eSWill Andrews 			 * that we re-start the CRN at 1 after a reset.
2556b44e442eSWill Andrews 			 */
255721daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
2558b44e442eSWill Andrews 
2559478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP;
2560478f8a96SJustin T. Gibbs 		}
2561478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2562478f8a96SJustin T. Gibbs 		break;
2563478f8a96SJustin T. Gibbs 	case XPT_ABORT:			/* Abort the specified CCB */
2564d81ba9d5SMatt Jacob 	{
2565d81ba9d5SMatt Jacob 		union ccb *accb = ccb->cab.abort_ccb;
2566d81ba9d5SMatt Jacob 		switch (accb->ccb_h.func_code) {
2567d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
2568d81ba9d5SMatt Jacob 		case XPT_ACCEPT_TARGET_IO:
25698290ea90SAlexander Motin 			isp_abort_atio(isp, ccb);
25708290ea90SAlexander Motin 			break;
25718290ea90SAlexander Motin 		case XPT_IMMEDIATE_NOTIFY:
25728290ea90SAlexander Motin 			isp_abort_inot(isp, ccb);
2573d81ba9d5SMatt Jacob 			break;
2574d81ba9d5SMatt Jacob #endif
2575d81ba9d5SMatt Jacob 		case XPT_SCSI_IO:
2576950b6e12SAlexander Motin 			error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
2577478f8a96SJustin T. Gibbs 			if (error) {
2578d81ba9d5SMatt Jacob 				ccb->ccb_h.status = CAM_UA_ABORT;
2579478f8a96SJustin T. Gibbs 			} else {
2580478f8a96SJustin T. Gibbs 				ccb->ccb_h.status = CAM_REQ_CMP;
2581478f8a96SJustin T. Gibbs 			}
2582d81ba9d5SMatt Jacob 			break;
2583d81ba9d5SMatt Jacob 		default:
2584d81ba9d5SMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
2585d81ba9d5SMatt Jacob 			break;
2586d81ba9d5SMatt Jacob 		}
25871c0a1eb2SMatt Jacob 		/*
25881c0a1eb2SMatt Jacob 		 * This is not a queued CCB, so the caller expects it to be
25891c0a1eb2SMatt Jacob 		 * complete when control is returned.
25901c0a1eb2SMatt Jacob 		 */
2591478f8a96SJustin T. Gibbs 		break;
2592d81ba9d5SMatt Jacob 	}
2593ab163f5fSMatt Jacob #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
2594478f8a96SJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
2595478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
25969ce9bdafSMatt Jacob 		if (!IS_CURRENT_SETTINGS(cts)) {
25979ce9bdafSMatt Jacob 			ccb->ccb_h.status = CAM_REQ_INVALID;
25989ce9bdafSMatt Jacob 			xpt_done(ccb);
25999ce9bdafSMatt Jacob 			break;
26009ce9bdafSMatt Jacob 		}
2601478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
2602478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2603478f8a96SJustin T. Gibbs 		break;
2604478f8a96SJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
26051b760be4SAlexander Motin 	{
26061b760be4SAlexander Motin 		struct ccb_trans_settings_scsi *scsi;
26071b760be4SAlexander Motin 		struct ccb_trans_settings_fc *fc;
26081b760be4SAlexander Motin 
2609478f8a96SJustin T. Gibbs 		cts = &ccb->cts;
26101b760be4SAlexander Motin 		scsi = &cts->proto_specific.scsi;
26111b760be4SAlexander Motin 		fc = &cts->xport_specific.fc;
2612478f8a96SJustin T. Gibbs 		tgt = cts->ccb_h.target_id;
26131b760be4SAlexander Motin 		fcp = FCPARAM(isp, bus);
2614478f8a96SJustin T. Gibbs 
2615ab163f5fSMatt Jacob 		cts->protocol = PROTO_SCSI;
2616ab163f5fSMatt Jacob 		cts->protocol_version = SCSI_REV_2;
2617ab163f5fSMatt Jacob 		cts->transport = XPORT_FC;
2618ab163f5fSMatt Jacob 		cts->transport_version = 0;
2619ab163f5fSMatt Jacob 
26209b03492aSMatt Jacob 		scsi->valid = CTS_SCSI_VALID_TQ;
26219b03492aSMatt Jacob 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2622ab163f5fSMatt Jacob 		fc->valid = CTS_FC_VALID_SPEED;
26231b760be4SAlexander Motin 		fc->bitrate = fcp->isp_gbspeed * 100000;
2624766a65a5SAlexander Motin 		if (tgt < MAX_FC_TARG) {
2625766a65a5SAlexander Motin 			fcportdb_t *lp = &fcp->portdb[tgt];
2626ab163f5fSMatt Jacob 			fc->wwnn = lp->node_wwn;
2627ab163f5fSMatt Jacob 			fc->wwpn = lp->port_wwn;
2628ab163f5fSMatt Jacob 			fc->port = lp->portid;
26292df76c16SMatt Jacob 			fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
2630ab163f5fSMatt Jacob 		}
2631478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
2632478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2633478f8a96SJustin T. Gibbs 		break;
26341b760be4SAlexander Motin 	}
2635478f8a96SJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
26362df76c16SMatt Jacob 		cam_calc_geometry(&ccb->ccg, 1);
26372df76c16SMatt Jacob 		xpt_done(ccb);
26382df76c16SMatt Jacob 		break;
2639478f8a96SJustin T. Gibbs 
2640478f8a96SJustin T. Gibbs 	case XPT_RESET_BUS:		/* Reset the specified bus */
26412df76c16SMatt Jacob 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
26422df76c16SMatt Jacob 		if (error) {
2643478f8a96SJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
26442df76c16SMatt Jacob 			xpt_done(ccb);
26452df76c16SMatt Jacob 			break;
26462df76c16SMatt Jacob 		}
26470a70657fSMatt Jacob 		if (bootverbose) {
26482df76c16SMatt Jacob 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
26490a70657fSMatt Jacob 		}
26502df76c16SMatt Jacob 		xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
2651478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_CMP;
2652478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2653478f8a96SJustin T. Gibbs 		break;
2654478f8a96SJustin T. Gibbs 
2655478f8a96SJustin T. Gibbs 	case XPT_TERM_IO:		/* Terminate the I/O process */
2656478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
2657478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2658478f8a96SJustin T. Gibbs 		break;
2659478f8a96SJustin T. Gibbs 
26602df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
26612df76c16SMatt Jacob 	{
26622df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
26631b760be4SAlexander Motin 		fcparam *fcp = FCPARAM(isp, bus);
26642df76c16SMatt Jacob 
26652df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
26662df76c16SMatt Jacob 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
26672df76c16SMatt Jacob 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
26682df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
26692df76c16SMatt Jacob 		}
26702df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
26712df76c16SMatt Jacob 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
26722df76c16SMatt Jacob 			int rchange = 0;
26732df76c16SMatt Jacob 			int newrole = 0;
26742df76c16SMatt Jacob 
26752df76c16SMatt Jacob 			switch (kp->xport_specific.fc.role) {
26762df76c16SMatt Jacob 			case KNOB_ROLE_NONE:
26772df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_NONE) {
26782df76c16SMatt Jacob 					rchange = 1;
26792df76c16SMatt Jacob 					newrole = ISP_ROLE_NONE;
26802df76c16SMatt Jacob 				}
26812df76c16SMatt Jacob 				break;
26822df76c16SMatt Jacob 			case KNOB_ROLE_TARGET:
26832df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_TARGET) {
26842df76c16SMatt Jacob 					rchange = 1;
26852df76c16SMatt Jacob 					newrole = ISP_ROLE_TARGET;
26862df76c16SMatt Jacob 				}
26872df76c16SMatt Jacob 				break;
26882df76c16SMatt Jacob 			case KNOB_ROLE_INITIATOR:
26892df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_INITIATOR) {
26902df76c16SMatt Jacob 					rchange = 1;
26912df76c16SMatt Jacob 					newrole = ISP_ROLE_INITIATOR;
26922df76c16SMatt Jacob 				}
26932df76c16SMatt Jacob 				break;
26942df76c16SMatt Jacob 			case KNOB_ROLE_BOTH:
26952df76c16SMatt Jacob 				if (fcp->role != ISP_ROLE_BOTH) {
26962df76c16SMatt Jacob 					rchange = 1;
26972df76c16SMatt Jacob 					newrole = ISP_ROLE_BOTH;
26982df76c16SMatt Jacob 				}
26992df76c16SMatt Jacob 				break;
27002df76c16SMatt Jacob 			}
27012df76c16SMatt Jacob 			if (rchange) {
2702e2873b76SMatt Jacob 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
27036bef0aa0SAlexander Motin 				if (isp_control(isp, ISPCTL_CHANGE_ROLE,
27046bef0aa0SAlexander Motin 				    bus, newrole) != 0) {
27052df76c16SMatt Jacob 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2706387d8239SMatt Jacob 					xpt_done(ccb);
2707387d8239SMatt Jacob 					break;
27082df76c16SMatt Jacob 				}
27092df76c16SMatt Jacob 			}
27102df76c16SMatt Jacob 		}
27112df76c16SMatt Jacob 		xpt_done(ccb);
27122df76c16SMatt Jacob 		break;
27132df76c16SMatt Jacob 	}
2714a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:	/* Get SIM knobs -- compat value */
2715e2873b76SMatt Jacob 	case XPT_GET_SIM_KNOB:		/* Get SIM knobs */
27162df76c16SMatt Jacob 	{
27172df76c16SMatt Jacob 		struct ccb_sim_knob *kp = &ccb->knob;
27181b760be4SAlexander Motin 		fcparam *fcp = FCPARAM(isp, bus);
27192df76c16SMatt Jacob 
27202df76c16SMatt Jacob 		kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
27212df76c16SMatt Jacob 		kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
27222df76c16SMatt Jacob 		switch (fcp->role) {
27232df76c16SMatt Jacob 		case ISP_ROLE_NONE:
27242df76c16SMatt Jacob 			kp->xport_specific.fc.role = KNOB_ROLE_NONE;
27252df76c16SMatt Jacob 			break;
27262df76c16SMatt Jacob 		case ISP_ROLE_TARGET:
27272df76c16SMatt Jacob 			kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
27282df76c16SMatt Jacob 			break;
27292df76c16SMatt Jacob 		case ISP_ROLE_INITIATOR:
27302df76c16SMatt Jacob 			kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
27312df76c16SMatt Jacob 			break;
27322df76c16SMatt Jacob 		case ISP_ROLE_BOTH:
27332df76c16SMatt Jacob 			kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
27342df76c16SMatt Jacob 			break;
27352df76c16SMatt Jacob 		}
27362df76c16SMatt Jacob 		kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
27372df76c16SMatt Jacob 		ccb->ccb_h.status = CAM_REQ_CMP;
27382df76c16SMatt Jacob 		xpt_done(ccb);
27392df76c16SMatt Jacob 		break;
27402df76c16SMatt Jacob 	}
2741478f8a96SJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
2742478f8a96SJustin T. Gibbs 	{
2743478f8a96SJustin T. Gibbs 		struct ccb_pathinq *cpi = &ccb->cpi;
2744478f8a96SJustin T. Gibbs 
2745478f8a96SJustin T. Gibbs 		cpi->version_num = 1;
2746d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
2747a1bc34c6SMatt Jacob 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
27481b760be4SAlexander Motin #else
27493e6deb33SAlexander Motin 		cpi->target_sprt = 0;
27501b760be4SAlexander Motin #endif
2751478f8a96SJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
27520470d791SMatt Jacob 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
27531b760be4SAlexander Motin 		cpi->max_lun = 255;
27540470d791SMatt Jacob 		cpi->bus_id = cam_sim_bus(sim);
27557bf825d1SKenneth D. Merry 		cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
27567bf825d1SKenneth D. Merry 
27571b760be4SAlexander Motin 		fcp = FCPARAM(isp, bus);
27582df76c16SMatt Jacob 
2759ab1aa38bSMarius Strobl 		cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
2760229203afSAlexander Motin 		cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
27612df76c16SMatt Jacob 
27620470d791SMatt Jacob 		/*
27630470d791SMatt Jacob 		 * Because our loop ID can shift from time to time,
27640470d791SMatt Jacob 		 * make our initiator ID out of range of our bus.
27650470d791SMatt Jacob 		 */
27660470d791SMatt Jacob 		cpi->initiator_id = cpi->max_target + 1;
27670470d791SMatt Jacob 
27689deea857SKenneth D. Merry 		/*
27692df76c16SMatt Jacob 		 * Set base transfer capabilities for Fibre Channel, for this HBA.
27709deea857SKenneth D. Merry 		 */
27711b760be4SAlexander Motin 		if (IS_25XX(isp))
2772e95725cbSMatt Jacob 			cpi->base_transfer_speed = 8000000;
27731b760be4SAlexander Motin 		else
27742df76c16SMatt Jacob 			cpi->base_transfer_speed = 4000000;
27750470d791SMatt Jacob 		cpi->hba_inquiry = PI_TAG_ABLE;
2776ab163f5fSMatt Jacob 		cpi->transport = XPORT_FC;
2777805e1f82SMatt Jacob 		cpi->transport_version = 0;
27782df76c16SMatt Jacob 		cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
27792df76c16SMatt Jacob 		cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
27802df76c16SMatt Jacob 		cpi->xport_specific.fc.port = fcp->isp_portid;
27812df76c16SMatt Jacob 		cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
2782ab163f5fSMatt Jacob 		cpi->protocol = PROTO_SCSI;
2783ab163f5fSMatt Jacob 		cpi->protocol_version = SCSI_REV_2;
27844195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
27854195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
27864195c7deSAlan Somers 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2787478f8a96SJustin T. Gibbs 		cpi->unit_number = cam_sim_unit(sim);
2788478f8a96SJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
2789478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2790478f8a96SJustin T. Gibbs 		break;
2791478f8a96SJustin T. Gibbs 	}
2792478f8a96SJustin T. Gibbs 	default:
2793478f8a96SJustin T. Gibbs 		ccb->ccb_h.status = CAM_REQ_INVALID;
2794478f8a96SJustin T. Gibbs 		xpt_done(ccb);
2795478f8a96SJustin T. Gibbs 		break;
2796478f8a96SJustin T. Gibbs 	}
2797478f8a96SJustin T. Gibbs }
2798d3a9eb2eSMatt Jacob 
2799d3a9eb2eSMatt Jacob void
isp_done(XS_T * sccb)28002df76c16SMatt Jacob isp_done(XS_T *sccb)
2801d3a9eb2eSMatt Jacob {
28029cd7268eSMatt Jacob 	ispsoftc_t *isp = XS_ISP(sccb);
2803e95725cbSMatt Jacob 	uint32_t status;
2804d3a9eb2eSMatt Jacob 
2805d3a9eb2eSMatt Jacob 	if (XS_NOERR(sccb))
2806d3a9eb2eSMatt Jacob 		XS_SETERR(sccb, CAM_REQ_CMP);
2807b85389e1SMatt Jacob 
28082df76c16SMatt Jacob 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
2809d3a9eb2eSMatt Jacob 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
28102df76c16SMatt Jacob 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
281192a1e549SMatt Jacob 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
281292a1e549SMatt Jacob 		} else {
2813d3a9eb2eSMatt Jacob 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2814d3a9eb2eSMatt Jacob 		}
281592a1e549SMatt Jacob 	}
2816b85389e1SMatt Jacob 
28170470d791SMatt Jacob 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2818e95725cbSMatt Jacob 	status = sccb->ccb_h.status & CAM_STATUS_MASK;
2819ab23521aSAlexander Motin 	if (status != CAM_REQ_CMP &&
2820ab23521aSAlexander Motin 	    (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2821d3a9eb2eSMatt Jacob 		sccb->ccb_h.status |= CAM_DEV_QFRZN;
28220470d791SMatt Jacob 		xpt_freeze_devq(sccb->ccb_h.path, 1);
2823d3a9eb2eSMatt Jacob 	}
2824b85389e1SMatt Jacob 
28255cbe3e8eSAlexander Motin 	if (ISP_PCMD(sccb)) {
2826387d8239SMatt Jacob 		if (callout_active(&PISP_PCMD(sccb)->wdog))
28270ec60713SMarius Strobl 			callout_stop(&PISP_PCMD(sccb)->wdog);
28280a70657fSMatt Jacob 		isp_free_pcmd(isp, (union ccb *) sccb);
28295cbe3e8eSAlexander Motin 	}
28300f99cb55SAlexander Motin 	isp_rq_check_below(isp);
2831d3a9eb2eSMatt Jacob 	xpt_done((union ccb *) sccb);
2832d3a9eb2eSMatt Jacob }
2833d3a9eb2eSMatt Jacob 
28342df76c16SMatt Jacob void
isp_async(ispsoftc_t * isp,ispasync_t cmd,...)28352df76c16SMatt Jacob isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
2836cbf57b47SMatt Jacob {
2837e68eef14SAlexander Motin 	int bus;
2838e68eef14SAlexander Motin 	static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
2839387d8239SMatt Jacob 	char buf[64];
2840f7c631bcSMatt Jacob 	char *msg = NULL;
284157b6261fSKenneth D. Merry 	target_id_t tgt = 0;
284210365e5aSMatt Jacob 	fcportdb_t *lp;
2843a01f5aebSMatt Jacob 	struct isp_fc *fc;
2844e68eef14SAlexander Motin 	struct ac_contract ac;
2845e68eef14SAlexander Motin 	struct ac_device_changed *adc;
28462df76c16SMatt Jacob 	va_list ap;
284710365e5aSMatt Jacob 
2848cbf57b47SMatt Jacob 	switch (cmd) {
284957b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_RESET:
285057b6261fSKenneth D. Merry 	{
285157b6261fSKenneth D. Merry 		uint16_t lipp;
285257b6261fSKenneth D. Merry 		fcparam *fcp;
285357b6261fSKenneth D. Merry 		va_start(ap, cmd);
285457b6261fSKenneth D. Merry 		bus = va_arg(ap, int);
285557b6261fSKenneth D. Merry 		va_end(ap);
285657b6261fSKenneth D. Merry 
285757b6261fSKenneth D. Merry 		lipp = ISP_READ(isp, OUTMAILBOX1);
285857b6261fSKenneth D. Merry 		fcp = FCPARAM(isp, bus);
285957b6261fSKenneth D. Merry 
286057b6261fSKenneth D. Merry 		isp_prt(isp, ISP_LOGINFO, "Chan %d LOOP Reset, LIP primitive %x", bus, lipp);
286157b6261fSKenneth D. Merry 		/*
286257b6261fSKenneth D. Merry 		 * Per FCP-4, a Reset LIP should result in a CRN reset. Other
286357b6261fSKenneth D. Merry 		 * LIPs and loop up/down events should never reset the CRN. For
286457b6261fSKenneth D. Merry 		 * an as of yet unknown reason, 24xx series cards (and
286557b6261fSKenneth D. Merry 		 * potentially others) can interrupt with a LIP Reset status
286657b6261fSKenneth D. Merry 		 * when no LIP reset came down the wire. Additionally, the LIP
286757b6261fSKenneth D. Merry 		 * primitive accompanying this status would not be a valid LIP
286857b6261fSKenneth D. Merry 		 * Reset primitive, but some variation of an invalid AL_PA
286957b6261fSKenneth D. Merry 		 * LIP. As a result, we have to verify the AL_PD in the LIP
287057b6261fSKenneth D. Merry 		 * addresses our port before blindly resetting.
287157b6261fSKenneth D. Merry 		*/
287257b6261fSKenneth D. Merry 		if (FCP_IS_DEST_ALPD(fcp, (lipp & 0x00FF)))
287357b6261fSKenneth D. Merry 			isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
287457b6261fSKenneth D. Merry 		isp_loop_changed(isp, bus);
287557b6261fSKenneth D. Merry 		break;
287657b6261fSKenneth D. Merry 	}
28775d571944SMatt Jacob 	case ISPASYNC_LIP:
287837a7daacSAlexander Motin 		if (msg == NULL)
2879f7c631bcSMatt Jacob 			msg = "LIP Received";
2880f7c631bcSMatt Jacob 		/* FALLTHROUGH */
288157c801f5SMatt Jacob 	case ISPASYNC_LOOP_DOWN:
288237a7daacSAlexander Motin 		if (msg == NULL)
2883f7c631bcSMatt Jacob 			msg = "LOOP Down";
288457b6261fSKenneth D. Merry 		/* FALLTHROUGH */
288557b6261fSKenneth D. Merry 	case ISPASYNC_LOOP_UP:
288657b6261fSKenneth D. Merry 		if (msg == NULL)
288757b6261fSKenneth D. Merry 			msg = "LOOP Up";
28882df76c16SMatt Jacob 		va_start(ap, cmd);
28892df76c16SMatt Jacob 		bus = va_arg(ap, int);
28902df76c16SMatt Jacob 		va_end(ap);
2891e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
2892e561aa79SAlexander Motin 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
289357c801f5SMatt Jacob 		break;
289410365e5aSMatt Jacob 	case ISPASYNC_DEV_ARRIVED:
28952df76c16SMatt Jacob 		va_start(ap, cmd);
28962df76c16SMatt Jacob 		bus = va_arg(ap, int);
28972df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
28982df76c16SMatt Jacob 		va_end(ap);
2899a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
2900e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
2901387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
2902e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
2903e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
2904e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
2905e68eef14SAlexander Motin 			lp->is_target = 1;
290621daf914SAlexander Motin 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
29075704e6f0SKenneth D. Merry 			isp_make_here(isp, lp, bus, tgt);
2908e68eef14SAlexander Motin 		}
2909e68eef14SAlexander Motin 		if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
2910e68eef14SAlexander Motin 		    (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
2911e68eef14SAlexander Motin 			lp->is_initiator = 1;
2912e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2913e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2914e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2915e68eef14SAlexander Motin 			adc->port = lp->portid;
2916d4f3ad3aSAlexander Motin 			adc->target = tgt;
2917e68eef14SAlexander Motin 			adc->arrived = 1;
2918e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
291910365e5aSMatt Jacob 		}
292010365e5aSMatt Jacob 		break;
292110365e5aSMatt Jacob 	case ISPASYNC_DEV_CHANGED:
292257b6261fSKenneth D. Merry 	case ISPASYNC_DEV_STAYED:
29236f9dbc0eSKenneth D. Merry 	{
29246f9dbc0eSKenneth D. Merry 		int crn_reset_done;
29256f9dbc0eSKenneth D. Merry 
29266f9dbc0eSKenneth D. Merry 		crn_reset_done = 0;
29272df76c16SMatt Jacob 		va_start(ap, cmd);
29282df76c16SMatt Jacob 		bus = va_arg(ap, int);
29292df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
29302df76c16SMatt Jacob 		va_end(ap);
2931a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
2932e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
2933e68eef14SAlexander Motin 		isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
293457b6261fSKenneth D. Merry 		if (cmd == ISPASYNC_DEV_CHANGED)
2935e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
293657b6261fSKenneth D. Merry 		else
293757b6261fSKenneth D. Merry 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
293857b6261fSKenneth D. Merry 
2939e68eef14SAlexander Motin 		if (lp->is_target !=
2940e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
2941e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
2942e68eef14SAlexander Motin 			lp->is_target = !lp->is_target;
2943e68eef14SAlexander Motin 			if (lp->is_target) {
29446f9dbc0eSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED) {
294521daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
29466f9dbc0eSKenneth D. Merry 					crn_reset_done = 1;
29476f9dbc0eSKenneth D. Merry 				}
2948e68eef14SAlexander Motin 				isp_make_here(isp, lp, bus, tgt);
294910365e5aSMatt Jacob 			} else {
2950e68eef14SAlexander Motin 				isp_make_gone(isp, lp, bus, tgt);
29516f9dbc0eSKenneth D. Merry 				if (cmd == ISPASYNC_DEV_CHANGED) {
295221daf914SAlexander Motin 					isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
29536f9dbc0eSKenneth D. Merry 					crn_reset_done = 1;
29546f9dbc0eSKenneth D. Merry 				}
295510365e5aSMatt Jacob 			}
2956f7c631bcSMatt Jacob 		}
2957e68eef14SAlexander Motin 		if (lp->is_initiator !=
2958e68eef14SAlexander Motin 		    ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
2959e68eef14SAlexander Motin 		     (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
2960e68eef14SAlexander Motin 			lp->is_initiator = !lp->is_initiator;
2961e68eef14SAlexander Motin 			ac.contract_number = AC_CONTRACT_DEV_CHG;
2962e68eef14SAlexander Motin 			adc = (struct ac_device_changed *) ac.contract_data;
2963e68eef14SAlexander Motin 			adc->wwpn = lp->port_wwn;
2964e68eef14SAlexander Motin 			adc->port = lp->portid;
2965d4f3ad3aSAlexander Motin 			adc->target = tgt;
2966e68eef14SAlexander Motin 			adc->arrived = lp->is_initiator;
2967e68eef14SAlexander Motin 			xpt_async(AC_CONTRACT, fc->path, &ac);
2968e68eef14SAlexander Motin 		}
29696f9dbc0eSKenneth D. Merry 
2970e26059caSKenneth D. Merry 		if ((cmd == ISPASYNC_DEV_CHANGED) &&
29716f9dbc0eSKenneth D. Merry 		    (crn_reset_done == 0))
29726f9dbc0eSKenneth D. Merry 			isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
29736f9dbc0eSKenneth D. Merry 
297410365e5aSMatt Jacob 		break;
29756f9dbc0eSKenneth D. Merry 	}
297610365e5aSMatt Jacob 	case ISPASYNC_DEV_GONE:
29772df76c16SMatt Jacob 		va_start(ap, cmd);
29782df76c16SMatt Jacob 		bus = va_arg(ap, int);
29792df76c16SMatt Jacob 		lp = va_arg(ap, fcportdb_t *);
29802df76c16SMatt Jacob 		va_end(ap);
2981a01f5aebSMatt Jacob 		fc = ISP_FC_PC(isp, bus);
2982e68eef14SAlexander Motin 		tgt = FC_PORTDB_TGT(isp, bus, lp);
2983f7c631bcSMatt Jacob 		/*
2984e68eef14SAlexander Motin 		 * If this has a virtual target or initiator set the isp_gdt
2985e68eef14SAlexander Motin 		 * timer running on it to delay its departure.
2986f7c631bcSMatt Jacob 		 */
2987387d8239SMatt Jacob 		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
2988e68eef14SAlexander Motin 		if (lp->is_target || lp->is_initiator) {
2989f7c631bcSMatt Jacob 			lp->state = FC_PORTDB_STATE_ZOMBIE;
2990e68eef14SAlexander Motin 			lp->gone_timer = fc->gone_device_time;
2991e68eef14SAlexander Motin 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
2992a01f5aebSMatt Jacob 			if (fc->ready && !callout_active(&fc->gdt)) {
2993387d8239SMatt 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);
2994a01f5aebSMatt Jacob 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
2995f7c631bcSMatt Jacob 			}
2996e68eef14SAlexander Motin 			break;
29974b9d588eSMatt Jacob 		}
2998e68eef14SAlexander Motin 		isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
299910365e5aSMatt Jacob 		break;
300010365e5aSMatt Jacob 	case ISPASYNC_CHANGE_NOTIFY:
300110365e5aSMatt Jacob 	{
300210365e5aSMatt Jacob 		char *msg;
3003eea52482SAlexander Motin 		int evt, nphdl, nlstate, portid, reason;
30042df76c16SMatt Jacob 
30052df76c16SMatt Jacob 		va_start(ap, cmd);
30062df76c16SMatt Jacob 		bus = va_arg(ap, int);
30072df76c16SMatt Jacob 		evt = va_arg(ap, int);
3008eea52482SAlexander Motin 		if (evt == ISPASYNC_CHANGE_PDB) {
30092df76c16SMatt Jacob 			nphdl = va_arg(ap, int);
30102df76c16SMatt Jacob 			nlstate = va_arg(ap, int);
30112df76c16SMatt Jacob 			reason = va_arg(ap, int);
3012eea52482SAlexander Motin 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3013eea52482SAlexander Motin 			portid = va_arg(ap, int);
301410365e5aSMatt Jacob 		} else {
30152df76c16SMatt Jacob 			nphdl = NIL_HANDLE;
30162df76c16SMatt Jacob 			nlstate = reason = 0;
301710365e5aSMatt Jacob 		}
30182df76c16SMatt Jacob 		va_end(ap);
30192df76c16SMatt Jacob 
30202df76c16SMatt Jacob 		if (evt == ISPASYNC_CHANGE_PDB) {
302157b6261fSKenneth D. Merry 			int tgt_set = 0;
3022b5d5037bSAlexander Motin 			msg = "Port Database Changed";
3023eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO,
3024eea52482SAlexander Motin 			    "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3025eea52482SAlexander Motin 			    bus, msg, nphdl, nlstate, reason);
302657b6261fSKenneth D. Merry 			/*
302757b6261fSKenneth D. Merry 			 * Port database syncs are not sufficient for
302857b6261fSKenneth D. Merry 			 * determining that logins or logouts are done on the
302957b6261fSKenneth D. Merry 			 * loop, but this information is directly available from
303057b6261fSKenneth D. Merry 			 * the reason code from the incoming mbox. We must reset
303157b6261fSKenneth D. Merry 			 * the fcp crn on these events according to FCP-4
303257b6261fSKenneth D. Merry 			 */
303357b6261fSKenneth D. Merry 			switch (reason) {
303457b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_1:
303557b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_2:
303657b6261fSKenneth D. Merry 			case PDB24XX_AE_IMPL_LOGO_3:
303757b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_RCVD:
303857b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_RCVD:
303957b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLO_RCVD:
304057b6261fSKenneth D. Merry 			case PDB24XX_AE_LOGO_RCVD:
304157b6261fSKenneth D. Merry 			case PDB24XX_AE_PLOGI_DONE:
304257b6261fSKenneth D. Merry 			case PDB24XX_AE_PRLI_DONE:
304357b6261fSKenneth D. Merry 				/*
304457b6261fSKenneth D. Merry 				 * If the event is not global, twiddle tgt and
304557b6261fSKenneth D. Merry 				 * tgt_set to nominate only the target
304657b6261fSKenneth D. Merry 				 * associated with the nphdl.
304757b6261fSKenneth D. Merry 				 */
304857b6261fSKenneth D. Merry 				if (nphdl != PDB24XX_AE_GLOBAL) {
304957b6261fSKenneth D. Merry 					/* Break if we don't yet have the pdb */
305057b6261fSKenneth D. Merry 					if (!isp_find_pdb_by_handle(isp, bus, nphdl, &lp))
305157b6261fSKenneth D. Merry 						break;
305257b6261fSKenneth D. Merry 					tgt = FC_PORTDB_TGT(isp, bus, lp);
305357b6261fSKenneth D. Merry 					tgt_set = 1;
305457b6261fSKenneth D. Merry 				}
305557b6261fSKenneth D. Merry 				isp_fcp_reset_crn(isp, bus, tgt, tgt_set);
305657b6261fSKenneth D. Merry 				break;
305757b6261fSKenneth D. Merry 			default:
305857b6261fSKenneth D. Merry 				break; /* NOP */
305957b6261fSKenneth D. Merry 			}
30602df76c16SMatt Jacob 		} else if (evt == ISPASYNC_CHANGE_SNS) {
3061b5d5037bSAlexander Motin 			msg = "Name Server Database Changed";
3062eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3063eea52482SAlexander Motin 			    bus, msg, portid);
30642df76c16SMatt Jacob 		} else {
3065b5d5037bSAlexander Motin 			msg = "Other Change Notify";
3066eea52482SAlexander Motin 			isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
30672df76c16SMatt Jacob 		}
3068e561aa79SAlexander Motin 		isp_loop_changed(isp, bus);
306957c801f5SMatt Jacob 		break;
307002ab3379SMatt Jacob 	}
3071d81ba9d5SMatt Jacob #ifdef	ISP_TARGET_MODE
3072e5265237SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY:
3073d81ba9d5SMatt Jacob 	{
30742df76c16SMatt Jacob 		isp_notify_t *notify;
30752df76c16SMatt Jacob 		va_start(ap, cmd);
30762df76c16SMatt Jacob 		notify = va_arg(ap, isp_notify_t *);
30772df76c16SMatt Jacob 		va_end(ap);
30782df76c16SMatt Jacob 		switch (notify->nt_ncode) {
30792df76c16SMatt Jacob 		case NT_ABORT_TASK:
30802df76c16SMatt Jacob 		case NT_ABORT_TASK_SET:
30812df76c16SMatt Jacob 		case NT_CLEAR_ACA:
30822df76c16SMatt Jacob 		case NT_CLEAR_TASK_SET:
30832df76c16SMatt Jacob 		case NT_LUN_RESET:
30842df76c16SMatt Jacob 		case NT_TARGET_RESET:
3085c98d2b1fSAlexander Motin 		case NT_QUERY_TASK_SET:
3086c98d2b1fSAlexander Motin 		case NT_QUERY_ASYNC_EVENT:
30872df76c16SMatt Jacob 			/*
30882df76c16SMatt Jacob 			 * These are task management functions.
30892df76c16SMatt Jacob 			 */
30902df76c16SMatt Jacob 			isp_handle_platform_target_tmf(isp, notify);
30912df76c16SMatt Jacob 			break;
30922df76c16SMatt Jacob 		case NT_LIP_RESET:
30932df76c16SMatt Jacob 		case NT_LINK_UP:
30942df76c16SMatt Jacob 		case NT_LINK_DOWN:
3095e68eef14SAlexander Motin 		case NT_HBA_RESET:
30962df76c16SMatt Jacob 			/*
30972df76c16SMatt Jacob 			 * No action need be taken here.
30982df76c16SMatt Jacob 			 */
30992df76c16SMatt Jacob 			break;
31005a5632c2SAlexander Motin 		case NT_SRR:
31015a5632c2SAlexander Motin 			isp_handle_platform_srr(isp, notify);
31025a5632c2SAlexander Motin 			break;
31032df76c16SMatt Jacob 		default:
31042df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
310596b5475bSAlexander Motin 			isp_handle_platform_target_notify_ack(isp, notify, 0);
31062df76c16SMatt Jacob 			break;
31072df76c16SMatt Jacob 		}
3108d81ba9d5SMatt Jacob 		break;
3109d81ba9d5SMatt Jacob 	}
3110387d8239SMatt Jacob 	case ISPASYNC_TARGET_NOTIFY_ACK:
3111387d8239SMatt Jacob 	{
3112387d8239SMatt Jacob 		void *inot;
3113387d8239SMatt Jacob 		va_start(ap, cmd);
3114387d8239SMatt Jacob 		inot = va_arg(ap, void *);
3115387d8239SMatt Jacob 		va_end(ap);
3116387d8239SMatt Jacob 		if (isp_notify_ack(isp, inot)) {
3117387d8239SMatt Jacob 			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3118387d8239SMatt Jacob 			if (tp) {
3119387d8239SMatt Jacob 				tp->isp = isp;
3120387d8239SMatt Jacob 				memcpy(tp->data, inot, sizeof (tp->data));
3121387d8239SMatt Jacob 				tp->not = tp->data;
31222a0db815SJohn Baldwin 				callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
31232a0db815SJohn Baldwin 				callout_reset(&tp->timer, 5,
31242a0db815SJohn Baldwin 				    isp_refire_notify_ack, tp);
3125387d8239SMatt Jacob 			} else {
3126387d8239SMatt Jacob 				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3127387d8239SMatt Jacob 			}
3128387d8239SMatt Jacob 		}
3129387d8239SMatt Jacob 		break;
3130387d8239SMatt Jacob 	}
3131d81ba9d5SMatt Jacob 	case ISPASYNC_TARGET_ACTION:
31322df76c16SMatt Jacob 	{
31332df76c16SMatt Jacob 		isphdr_t *hp;
31342df76c16SMatt Jacob 
31352df76c16SMatt Jacob 		va_start(ap, cmd);
31362df76c16SMatt Jacob 		hp = va_arg(ap, isphdr_t *);
31372df76c16SMatt Jacob 		va_end(ap);
31382df76c16SMatt Jacob 		switch (hp->rqs_entry_type) {
3139d81ba9d5SMatt Jacob 		case RQSTYPE_ATIO:
31402df76c16SMatt Jacob 			isp_handle_platform_atio7(isp, (at7_entry_t *)hp);
3141d81ba9d5SMatt Jacob 			break;
31422df76c16SMatt Jacob 		case RQSTYPE_CTIO7:
31431b760be4SAlexander Motin 			isp_handle_platform_ctio(isp, (ct7_entry_t *)hp);
3144d81ba9d5SMatt Jacob 			break;
31455a5632c2SAlexander Motin 		default:
31465a5632c2SAlexander Motin 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
31475a5632c2SAlexander Motin 			    __func__, hp->rqs_entry_type);
31485a5632c2SAlexander Motin 			break;
3149d81ba9d5SMatt Jacob 		}
3150d81ba9d5SMatt Jacob 		break;
31512df76c16SMatt Jacob 	}
3152d81ba9d5SMatt Jacob #endif
3153ab163f5fSMatt Jacob 	case ISPASYNC_FW_CRASH:
3154ab163f5fSMatt Jacob 	{
31551b760be4SAlexander Motin 		uint16_t mbox1;
3156ab163f5fSMatt Jacob 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
31571b760be4SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error @ RISC Address 0x%x", mbox1);
31589c2e9bcfSAlexander Motin #if 0
31592df76c16SMatt Jacob 		isp_reinit(isp, 1);
31600a70657fSMatt Jacob 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
31619c2e9bcfSAlexander Motin #endif
3162ab163f5fSMatt Jacob 		break;
3163ab163f5fSMatt Jacob 	}
3164d81ba9d5SMatt Jacob 	default:
3165b09b0095SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3166cbf57b47SMatt Jacob 		break;
3167cbf57b47SMatt Jacob 	}
3168cbf57b47SMatt Jacob }
3169cbf57b47SMatt Jacob 
31702df76c16SMatt Jacob uint64_t
isp_default_wwn(ispsoftc_t * isp,int chan,int isactive,int iswwnn)31712df76c16SMatt Jacob isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
31722df76c16SMatt Jacob {
31732df76c16SMatt Jacob 	uint64_t seed;
31742df76c16SMatt Jacob 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
31752df76c16SMatt Jacob 
3176a193dc4bSAlexander Motin 	/* First try to use explicitly configured WWNs. */
3177a193dc4bSAlexander Motin 	seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
3178a193dc4bSAlexander Motin 	if (seed)
3179a193dc4bSAlexander Motin 		return (seed);
3180a193dc4bSAlexander Motin 
3181a193dc4bSAlexander Motin 	/* Otherwise try to use WWNs from NVRAM. */
31822df76c16SMatt Jacob 	if (isactive) {
3183a193dc4bSAlexander Motin 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
3184a193dc4bSAlexander Motin 		    FCPARAM(isp, chan)->isp_wwpn_nvram;
3185a193dc4bSAlexander Motin 		if (seed)
31862df76c16SMatt Jacob 			return (seed);
31872df76c16SMatt Jacob 	}
3188a193dc4bSAlexander Motin 
3189a193dc4bSAlexander Motin 	/* If still no WWNs, try to steal them from the first channel. */
3190a193dc4bSAlexander Motin 	if (chan > 0) {
3191a193dc4bSAlexander Motin 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
3192a193dc4bSAlexander Motin 		    ISP_FC_PC(isp, 0)->def_wwpn;
3193a193dc4bSAlexander Motin 		if (seed == 0) {
3194a193dc4bSAlexander Motin 			seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
3195a193dc4bSAlexander Motin 			    FCPARAM(isp, 0)->isp_wwpn_nvram;
31965cc3786cSMatt Jacob 		}
31972df76c16SMatt Jacob 	}
31982df76c16SMatt Jacob 
3199a193dc4bSAlexander Motin 	/* If still nothing -- improvise. */
3200a193dc4bSAlexander Motin 	if (seed == 0) {
3201a193dc4bSAlexander Motin 		seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
3202a193dc4bSAlexander Motin 		if (!iswwnn)
3203a193dc4bSAlexander Motin 			seed ^= 0x0100000000000000ULL;
3204a193dc4bSAlexander Motin 	}
32052df76c16SMatt Jacob 
3206a193dc4bSAlexander Motin 	/* For additional channels we have to improvise even more. */
3207a193dc4bSAlexander Motin 	if (!iswwnn && chan > 0) {
32082df76c16SMatt Jacob 		/*
32092df76c16SMatt Jacob 		 * We'll stick our channel number plus one first into bits
32102df76c16SMatt Jacob 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
3211a193dc4bSAlexander Motin 		 * of channel which is enough for our maximum of 255 channels.
32122df76c16SMatt Jacob 		 */
3213a193dc4bSAlexander Motin 		seed ^= 0x0100000000000000ULL;
3214a193dc4bSAlexander Motin 		seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
3215a193dc4bSAlexander Motin 		seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
32162df76c16SMatt Jacob 	}
32172df76c16SMatt Jacob 	return (seed);
32182df76c16SMatt Jacob }
32192df76c16SMatt Jacob 
3220b09b0095SMatt Jacob void
isp_prt(ispsoftc_t * isp,int level,const char * fmt,...)32219cd7268eSMatt Jacob isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3222b09b0095SMatt Jacob {
3223de461933SMatt Jacob 	int loc;
3224387d8239SMatt Jacob 	char lbuf[200];
3225b09b0095SMatt Jacob 	va_list ap;
3226de461933SMatt Jacob 
3227b09b0095SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3228b09b0095SMatt Jacob 		return;
3229b09b0095SMatt Jacob 	}
3230387d8239SMatt Jacob 	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
3231de461933SMatt Jacob 	loc = strlen(lbuf);
3232b09b0095SMatt Jacob 	va_start(ap, fmt);
3233de461933SMatt Jacob 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
3234b09b0095SMatt Jacob 	va_end(ap);
3235de461933SMatt Jacob 	printf("%s\n", lbuf);
3236b09b0095SMatt Jacob }
3237f7c631bcSMatt Jacob 
3238670508b1SMatt Jacob void
isp_xs_prt(ispsoftc_t * isp,XS_T * xs,int level,const char * fmt,...)3239670508b1SMatt Jacob isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
3240670508b1SMatt Jacob {
3241670508b1SMatt Jacob 	va_list ap;
3242670508b1SMatt Jacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3243670508b1SMatt Jacob 		return;
3244670508b1SMatt Jacob 	}
3245670508b1SMatt Jacob 	xpt_print_path(xs->ccb_h.path);
3246670508b1SMatt Jacob 	va_start(ap, fmt);
3247670508b1SMatt Jacob 	vprintf(fmt, ap);
3248670508b1SMatt Jacob 	va_end(ap);
3249670508b1SMatt Jacob 	printf("\n");
3250670508b1SMatt Jacob }
3251670508b1SMatt Jacob 
3252f7c631bcSMatt Jacob uint64_t
isp_nanotime_sub(struct timespec * b,struct timespec * a)3253f7c631bcSMatt Jacob isp_nanotime_sub(struct timespec *b, struct timespec *a)
3254f7c631bcSMatt Jacob {
3255f7c631bcSMatt Jacob 	uint64_t elapsed;
32566040822cSAlan Somers 	struct timespec x;
32576040822cSAlan Somers 
32586040822cSAlan Somers 	timespecsub(b, a, &x);
3259f7c631bcSMatt Jacob 	elapsed = GET_NANOSEC(&x);
3260f7c631bcSMatt Jacob 	if (elapsed == 0)
3261f7c631bcSMatt Jacob 		elapsed++;
3262f7c631bcSMatt Jacob 	return (elapsed);
3263f7c631bcSMatt Jacob }
3264f7c631bcSMatt Jacob 
3265f7c631bcSMatt Jacob int
isp_fc_scratch_acquire(ispsoftc_t * isp,int chan)32662df76c16SMatt Jacob isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
32672df76c16SMatt Jacob {
3268a6222dd7SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3269a6222dd7SAlexander Motin 
3270a6222dd7SAlexander Motin 	if (fc->fcbsy)
3271a6222dd7SAlexander Motin 		return (-1);
3272a6222dd7SAlexander Motin 	fc->fcbsy = 1;
3273a6222dd7SAlexander Motin 	return (0);
32742df76c16SMatt Jacob }
32752df76c16SMatt Jacob 
32760a70657fSMatt Jacob void
isp_platform_intr(void * arg)32770a70657fSMatt Jacob isp_platform_intr(void *arg)
32780a70657fSMatt Jacob {
32790a70657fSMatt Jacob 	ispsoftc_t *isp = arg;
32800a70657fSMatt Jacob 
32810a70657fSMatt Jacob 	ISP_LOCK(isp);
32820e6bc811SAlexander Motin 	ISP_RUN_ISR(isp);
32830a70657fSMatt Jacob 	ISP_UNLOCK(isp);
32840a70657fSMatt Jacob }
32850a70657fSMatt Jacob 
32860a70657fSMatt Jacob void
isp_platform_intr_resp(void * arg)328708826086SAlexander Motin isp_platform_intr_resp(void *arg)
328808826086SAlexander Motin {
328908826086SAlexander Motin 	ispsoftc_t *isp = arg;
329008826086SAlexander Motin 
329108826086SAlexander Motin 	ISP_LOCK(isp);
329208826086SAlexander Motin 	isp_intr_respq(isp);
329308826086SAlexander Motin 	ISP_UNLOCK(isp);
329408826086SAlexander Motin 
329508826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
329608826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
329708826086SAlexander Motin }
329808826086SAlexander Motin 
329908826086SAlexander Motin void
isp_platform_intr_atio(void * arg)330008826086SAlexander Motin isp_platform_intr_atio(void *arg)
330108826086SAlexander Motin {
330208826086SAlexander Motin 	ispsoftc_t *isp = arg;
330308826086SAlexander Motin 
330408826086SAlexander Motin 	ISP_LOCK(isp);
330508826086SAlexander Motin #ifdef	ISP_TARGET_MODE
330608826086SAlexander Motin 	isp_intr_atioq(isp);
330708826086SAlexander Motin #endif
330808826086SAlexander Motin 	ISP_UNLOCK(isp);
330908826086SAlexander Motin 
331008826086SAlexander Motin 	/* We have handshake enabled, so explicitly complete interrupt */
331108826086SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
331208826086SAlexander Motin }
331308826086SAlexander Motin 
3314f6854a0cSAlexander Motin typedef struct {
3315f6854a0cSAlexander Motin 	ispsoftc_t		*isp;
3316f6854a0cSAlexander Motin 	struct ccb_scsiio	*csio;
3317f6854a0cSAlexander Motin 	void			*qe;
3318f6854a0cSAlexander Motin 	int			error;
3319f6854a0cSAlexander Motin } mush_t;
3320f6854a0cSAlexander Motin 
3321f6854a0cSAlexander Motin static void
isp_dma2(void * arg,bus_dma_segment_t * dm_segs,int nseg,int error)3322f6854a0cSAlexander Motin isp_dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
33230a70657fSMatt Jacob {
3324f6854a0cSAlexander Motin 	mush_t *mp = (mush_t *) arg;
3325f6854a0cSAlexander Motin 	ispsoftc_t *isp= mp->isp;
3326f6854a0cSAlexander Motin 	struct ccb_scsiio *csio = mp->csio;
3327f6854a0cSAlexander Motin 	bus_dmasync_op_t op;
3328f6854a0cSAlexander Motin 
3329f6854a0cSAlexander Motin 	if (error) {
3330f6854a0cSAlexander Motin 		mp->error = error;
3331f6854a0cSAlexander Motin 		return;
33320a70657fSMatt Jacob 	}
3333f6854a0cSAlexander Motin 	if ((csio->ccb_h.func_code == XPT_CONT_TARGET_IO) ^
3334f6854a0cSAlexander Motin 	    ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN))
3335f6854a0cSAlexander Motin 		op = BUS_DMASYNC_PREREAD;
3336f6854a0cSAlexander Motin 	else
3337f6854a0cSAlexander Motin 		op = BUS_DMASYNC_PREWRITE;
3338f6854a0cSAlexander Motin 	bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, op);
3339f6854a0cSAlexander Motin 
3340f6854a0cSAlexander Motin 	mp->error = ISP_SEND_CMD(isp, mp->qe, dm_segs, nseg);
3341f6854a0cSAlexander Motin 	if (mp->error)
3342f6854a0cSAlexander Motin 		isp_dmafree(isp, csio);
3343f6854a0cSAlexander Motin }
3344f6854a0cSAlexander Motin 
3345f6854a0cSAlexander Motin int
isp_dmasetup(ispsoftc_t * isp,struct ccb_scsiio * csio,void * qe)3346f6854a0cSAlexander Motin isp_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *qe)
3347f6854a0cSAlexander Motin {
3348f6854a0cSAlexander Motin 	mush_t mp;
3349f6854a0cSAlexander Motin 	int error;
3350f6854a0cSAlexander Motin 
3351f6854a0cSAlexander Motin 	if (XS_XFRLEN(csio)) {
3352f6854a0cSAlexander Motin 		mp.isp = isp;
3353f6854a0cSAlexander Motin 		mp.csio = csio;
3354f6854a0cSAlexander Motin 		mp.qe = qe;
3355f6854a0cSAlexander Motin 		mp.error = 0;
3356f6854a0cSAlexander Motin 		error = bus_dmamap_load_ccb(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap,
3357f6854a0cSAlexander Motin 		    (union ccb *)csio, isp_dma2, &mp, BUS_DMA_NOWAIT);
3358f6854a0cSAlexander Motin 		if (error == 0)
3359f6854a0cSAlexander Motin 			error = mp.error;
3360f6854a0cSAlexander Motin 	} else {
3361f6854a0cSAlexander Motin 		error = ISP_SEND_CMD(isp, qe, NULL, 0);
3362f6854a0cSAlexander Motin 	}
3363f6854a0cSAlexander Motin 	switch (error) {
3364f6854a0cSAlexander Motin 	case 0:
3365f6854a0cSAlexander Motin 	case CMD_COMPLETE:
3366f6854a0cSAlexander Motin 	case CMD_EAGAIN:
3367f6854a0cSAlexander Motin 	case CMD_RQLATER:
3368f6854a0cSAlexander Motin 		break;
3369f6854a0cSAlexander Motin 	case ENOMEM:
3370f6854a0cSAlexander Motin 		error = CMD_EAGAIN;
3371f6854a0cSAlexander Motin 		break;
3372f6854a0cSAlexander Motin 	case EINVAL:
3373f6854a0cSAlexander Motin 	case EFBIG:
3374f6854a0cSAlexander Motin 		csio->ccb_h.status = CAM_REQ_INVALID;
3375f6854a0cSAlexander Motin 		error = CMD_COMPLETE;
3376f6854a0cSAlexander Motin 		break;
3377f6854a0cSAlexander Motin 	default:
3378f6854a0cSAlexander Motin 		csio->ccb_h.status = CAM_UNREC_HBA_ERROR;
3379f6854a0cSAlexander Motin 		error = CMD_COMPLETE;
3380f6854a0cSAlexander Motin 		break;
3381f6854a0cSAlexander Motin 	}
3382f6854a0cSAlexander Motin 	return (error);
3383f6854a0cSAlexander Motin }
3384f6854a0cSAlexander Motin 
3385f6854a0cSAlexander Motin void
isp_dmafree(ispsoftc_t * isp,struct ccb_scsiio * csio)3386f6854a0cSAlexander Motin isp_dmafree(ispsoftc_t *isp, struct ccb_scsiio *csio)
3387f6854a0cSAlexander Motin {
3388f6854a0cSAlexander Motin 	bus_dmasync_op_t op;
3389f6854a0cSAlexander Motin 
3390f6854a0cSAlexander Motin 	if (XS_XFRLEN(csio) == 0)
3391f6854a0cSAlexander Motin 		return;
3392f6854a0cSAlexander Motin 
3393f6854a0cSAlexander Motin 	if ((csio->ccb_h.func_code == XPT_CONT_TARGET_IO) ^
3394f6854a0cSAlexander Motin 	    ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN))
3395f6854a0cSAlexander Motin 		op = BUS_DMASYNC_POSTREAD;
3396f6854a0cSAlexander Motin 	else
3397f6854a0cSAlexander Motin 		op = BUS_DMASYNC_POSTWRITE;
3398f6854a0cSAlexander Motin 	bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, op);
33990a70657fSMatt Jacob 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
34000a70657fSMatt Jacob }
34012df76c16SMatt Jacob 
3402fb4a4356SKenneth D. Merry /*
3403fb4a4356SKenneth D. Merry  * Reset the command reference number for all LUNs on a specific target
3404fb4a4356SKenneth D. Merry  * (needed when a target arrives again) or for all targets on a port
3405fb4a4356SKenneth D. Merry  * (needed for events like a LIP).
3406fb4a4356SKenneth D. Merry  */
3407fb4a4356SKenneth D. Merry void
isp_fcp_reset_crn(ispsoftc_t * isp,int chan,uint32_t tgt,int tgt_set)340821daf914SAlexander Motin isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
3409fb4a4356SKenneth D. Merry {
341021daf914SAlexander Motin 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3411fb4a4356SKenneth D. Merry 	struct isp_nexus *nxp;
341221daf914SAlexander Motin 	int i;
3413fb4a4356SKenneth D. Merry 
3414fb4a4356SKenneth D. Merry 	if (tgt_set == 0)
341521daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
341621daf914SAlexander Motin 		    "Chan %d resetting CRN on all targets", chan);
3417fb4a4356SKenneth D. Merry 	else
341821daf914SAlexander Motin 		isp_prt(isp, ISP_LOGDEBUG0,
341921daf914SAlexander Motin 		    "Chan %d resetting CRN on target %u", chan, tgt);
3420fb4a4356SKenneth D. Merry 
3421fb4a4356SKenneth D. Merry 	for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
342221daf914SAlexander Motin 		for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
342321daf914SAlexander Motin 			if (tgt_set == 0 || tgt == nxp->tgt)
3424fb4a4356SKenneth D. Merry 				nxp->crnseed = 0;
3425fb4a4356SKenneth D. Merry 		}
3426fb4a4356SKenneth D. Merry 	}
3427fb4a4356SKenneth D. Merry }
3428fb4a4356SKenneth D. Merry 
3429387d8239SMatt Jacob int
isp_fcp_next_crn(ispsoftc_t * isp,uint8_t * crnp,XS_T * cmd)3430387d8239SMatt Jacob isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
3431387d8239SMatt Jacob {
34326af11b82SAlexander Motin 	lun_id_t lun;
34336af11b82SAlexander Motin 	uint32_t chan, tgt;
3434405b7a29SMatt Jacob 	struct isp_fc *fc;
3435405b7a29SMatt Jacob 	struct isp_nexus *nxp;
3436405b7a29SMatt Jacob 	int idx;
3437405b7a29SMatt Jacob 
3438405b7a29SMatt Jacob 	chan = XS_CHANNEL(cmd);
3439405b7a29SMatt Jacob 	tgt = XS_TGT(cmd);
3440405b7a29SMatt Jacob 	lun = XS_LUN(cmd);
3441a6222dd7SAlexander Motin 	fc = ISP_FC_PC(isp, chan);
3442405b7a29SMatt Jacob 	idx = NEXUS_HASH(tgt, lun);
3443405b7a29SMatt Jacob 	nxp = fc->nexus_hash[idx];
3444387d8239SMatt Jacob 
3445387d8239SMatt Jacob 	while (nxp) {
3446387d8239SMatt Jacob 		if (nxp->tgt == tgt && nxp->lun == lun)
3447387d8239SMatt Jacob 			break;
3448387d8239SMatt Jacob 		nxp = nxp->next;
3449387d8239SMatt Jacob 	}
3450387d8239SMatt Jacob 	if (nxp == NULL) {
3451387d8239SMatt Jacob 		nxp = fc->nexus_free_list;
3452387d8239SMatt Jacob 		if (nxp == NULL) {
3453387d8239SMatt Jacob 			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
3454387d8239SMatt Jacob 			if (nxp == NULL) {
3455387d8239SMatt Jacob 				return (-1);
3456387d8239SMatt Jacob 			}
3457387d8239SMatt Jacob 		} else {
3458387d8239SMatt Jacob 			fc->nexus_free_list = nxp->next;
3459387d8239SMatt Jacob 		}
3460387d8239SMatt Jacob 		nxp->tgt = tgt;
3461387d8239SMatt Jacob 		nxp->lun = lun;
3462387d8239SMatt Jacob 		nxp->next = fc->nexus_hash[idx];
3463387d8239SMatt Jacob 		fc->nexus_hash[idx] = nxp;
3464387d8239SMatt Jacob 	}
3465387d8239SMatt Jacob 	if (nxp->crnseed == 0)
3466387d8239SMatt Jacob 		nxp->crnseed = 1;
3467387d8239SMatt Jacob 	*crnp = nxp->crnseed++;
3468387d8239SMatt Jacob 	return (0);
3469387d8239SMatt Jacob }
3470387d8239SMatt Jacob 
347194dff771SMatt Jacob /*
347294dff771SMatt Jacob  * We enter with the lock held
347394dff771SMatt Jacob  */
34742df76c16SMatt Jacob void
isp_timer(void * arg)34752df76c16SMatt Jacob isp_timer(void *arg)
34762df76c16SMatt Jacob {
34772df76c16SMatt Jacob 	ispsoftc_t *isp = arg;
34782df76c16SMatt Jacob #ifdef	ISP_TARGET_MODE
34792df76c16SMatt Jacob 	isp_tmcmd_restart(isp);
34802df76c16SMatt Jacob #endif
348194dff771SMatt Jacob 	callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
34822df76c16SMatt Jacob }
3483387d8239SMatt Jacob 
3484cf770ba3SAlexander Motin #ifdef	ISP_TARGET_MODE
3485387d8239SMatt Jacob isp_ecmd_t *
isp_get_ecmd(ispsoftc_t * isp)3486387d8239SMatt Jacob isp_get_ecmd(ispsoftc_t *isp)
3487387d8239SMatt Jacob {
3488387d8239SMatt Jacob 	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
3489387d8239SMatt Jacob 	if (ecmd) {
3490387d8239SMatt Jacob 		isp->isp_osinfo.ecmd_free = ecmd->next;
3491387d8239SMatt Jacob 	}
3492387d8239SMatt Jacob 	return (ecmd);
3493387d8239SMatt Jacob }
3494387d8239SMatt Jacob 
3495387d8239SMatt Jacob void
isp_put_ecmd(ispsoftc_t * isp,isp_ecmd_t * ecmd)3496387d8239SMatt Jacob isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
3497387d8239SMatt Jacob {
3498387d8239SMatt Jacob 	ecmd->next = isp->isp_osinfo.ecmd_free;
3499387d8239SMatt Jacob 	isp->isp_osinfo.ecmd_free = ecmd;
3500387d8239SMatt Jacob }
3501cf770ba3SAlexander Motin #endif
3502