xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision e42fc368672e8c3f1d30fbbd7f1903e3baa69b7a)
1 /*-
2  * Copyright (c) 1997-2009 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <dev/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
35 #include <sys/conf.h>
36 #include <sys/module.h>
37 #include <sys/ioccom.h>
38 #include <dev/isp/isp_ioctl.h>
39 #include <sys/devicestat.h>
40 #include <cam/cam_periph.h>
41 #include <cam/cam_xpt_periph.h>
42 
43 #if	__FreeBSD_version < 800002
44 #define	THREAD_CREATE	kthread_create
45 #else
46 #define	THREAD_CREATE	kproc_create
47 #endif
48 
49 MODULE_VERSION(isp, 1);
50 MODULE_DEPEND(isp, cam, 1, 1, 1);
51 int isp_announced = 0;
52 int isp_fabric_hysteresis = 3;
53 int isp_loop_down_limit = 60;	/* default loop down limit */
54 int isp_change_is_bad = 0;	/* "changed" devices are bad */
55 int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
56 int isp_gone_device_time = 30;	/* grace time before reporting device lost */
57 int isp_autoconfig = 1;		/* automatically attach/detach devices */
58 static const char *roles[4] = {
59     "(none)", "Target", "Initiator", "Target/Initiator"
60 };
61 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
62 static const char rqo[] = "%s: Request Queue Overflow\n";
63 
64 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
65 static d_ioctl_t ispioctl;
66 static void isp_intr_enable(void *);
67 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
68 static void isp_poll(struct cam_sim *);
69 static timeout_t isp_watchdog;
70 static timeout_t isp_ldt;
71 static void isp_kthread(void *);
72 static void isp_action(struct cam_sim *, union ccb *);
73 #ifdef	ISP_INTERNAL_TARGET
74 static void isp_target_thread_pi(void *);
75 static void isp_target_thread_fc(void *);
76 #endif
77 static void isp_timer(void *);
78 
79 static struct cdevsw isp_cdevsw = {
80 	.d_version =	D_VERSION,
81 	.d_ioctl =	ispioctl,
82 	.d_name =	"isp",
83 };
84 
85 static int
86 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
87 {
88 	struct ccb_setasync csa;
89 	struct cam_sim *sim;
90 	struct cam_path *path;
91 
92 	/*
93 	 * Construct our SIM entry.
94 	 */
95 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
96 
97 	if (sim == NULL) {
98 		return (ENOMEM);
99 	}
100 
101 	ISP_LOCK(isp);
102 	if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
103 		ISP_UNLOCK(isp);
104 		cam_sim_free(sim, FALSE);
105 		return (EIO);
106 	}
107 	ISP_UNLOCK(isp);
108 
109 	if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
110 		ISP_LOCK(isp);
111 		xpt_bus_deregister(cam_sim_path(sim));
112 		ISP_UNLOCK(isp);
113 		cam_sim_free(sim, FALSE);
114 		return (ENXIO);
115 	}
116 
117 	xpt_setup_ccb(&csa.ccb_h, path, 5);
118 	csa.ccb_h.func_code = XPT_SASYNC_CB;
119 	csa.event_enable = AC_LOST_DEVICE;
120 	csa.callback = isp_cam_async;
121 	csa.callback_arg = sim;
122 	xpt_action((union ccb *)&csa);
123 
124 	if (IS_SCSI(isp)) {
125 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
126 		spi->sim = sim;
127 		spi->path = path;
128 #ifdef	ISP_INTERNAL_TARGET
129 		ISP_SET_PC(isp, chan, proc_active, 1);
130 		if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
131 			ISP_SET_PC(isp, chan, proc_active, 0);
132 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
133 		}
134 #endif
135 	} else {
136 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
137 
138 		fc->sim = sim;
139 		fc->path = path;
140 		fc->isp = isp;
141 
142 		callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
143 		callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
144 
145 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
146 			xpt_free_path(fc->path);
147 			ISP_LOCK(isp);
148 			xpt_bus_deregister(cam_sim_path(fc->sim));
149 			ISP_UNLOCK(isp);
150 			cam_sim_free(fc->sim, FALSE);
151 		}
152 		/*
153 		 * We start by being "loop down" if we have an initiator role
154 		 */
155 		ISP_LOCK(isp);
156 		if ((FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) && fc->ldt_running == 0) {
157 			isp_freeze_loopdown(isp, chan, "isp_attach");
158 			fc->ldt_running = 1;
159 			callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
160 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
161 		}
162 		ISP_UNLOCK(isp);
163 #ifdef	ISP_INTERNAL_TARGET
164 		ISP_SET_PC(isp, chan, proc_active, 1);
165 		if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
166 			ISP_SET_PC(isp, chan, proc_active, 0);
167 			isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
168 		}
169 #endif
170 	}
171 	return (0);
172 }
173 
174 int
175 isp_attach(ispsoftc_t *isp)
176 {
177 	const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
178 	int du = device_get_unit(isp->isp_dev);
179 	int chan;
180 
181 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
182 	isp->isp_osinfo.ehook.ich_arg = isp;
183 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
184 		isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
185 		return (-EIO);
186 	}
187 	isp->isp_osinfo.ehook_active = 1;
188 
189 
190 	/*
191 	 * Create the device queue for our SIM(s).
192 	 */
193 	isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
194 	if (isp->isp_osinfo.devq == NULL) {
195 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
196 		return (EIO);
197 	}
198 
199 	for (chan = 0; chan < isp->isp_nchan; chan++) {
200 		if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
201 			goto unwind;
202 		}
203 	}
204 
205 	callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
206 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
207 	isp->isp_osinfo.timer_active = 1;
208 
209 	isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
210 	if (isp->isp_osinfo.cdev) {
211 		isp->isp_osinfo.cdev->si_drv1 = isp;
212 	}
213 	return (0);
214 
215 unwind:
216 	while (--chan >= 0) {
217 		struct cam_sim *sim;
218 		struct cam_path *path;
219 		if (IS_FC(isp)) {
220 			sim = ISP_FC_PC(isp, chan)->sim;
221 			path = ISP_FC_PC(isp, chan)->path;
222 		} else {
223 			sim = ISP_SPI_PC(isp, chan)->sim;
224 			path = ISP_SPI_PC(isp, chan)->path;
225 		}
226 		xpt_free_path(path);
227 		ISP_LOCK(isp);
228 		xpt_bus_deregister(cam_sim_path(sim));
229 		ISP_UNLOCK(isp);
230 		cam_sim_free(sim, FALSE);
231 	}
232 	if (isp->isp_osinfo.ehook_active) {
233 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
234 		isp->isp_osinfo.ehook_active = 0;
235 	}
236 	if (isp->isp_osinfo.cdev) {
237 		destroy_dev(isp->isp_osinfo.cdev);
238 		isp->isp_osinfo.cdev = NULL;
239 	}
240 	cam_simq_free(isp->isp_osinfo.devq);
241 	isp->isp_osinfo.devq = NULL;
242 	return (-1);
243 }
244 
245 void
246 isp_detach(ispsoftc_t *isp)
247 {
248 	int chan;
249 
250 	ISP_LOCK(isp);
251 	if (isp->isp_osinfo.timer_active) {
252 		callout_stop(&isp->isp_osinfo.tmo);
253 		isp->isp_osinfo.timer_active = 0;
254 	}
255 	ISP_UNLOCK(isp);
256 	for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
257 		struct cam_sim *sim;
258 		struct cam_path *path;
259 		if (IS_FC(isp)) {
260 			sim = ISP_FC_PC(isp, chan)->sim;
261 			path = ISP_FC_PC(isp, chan)->path;
262 		} else {
263 			sim = ISP_SPI_PC(isp, chan)->sim;
264 			path = ISP_SPI_PC(isp, chan)->path;
265 		}
266 		xpt_free_path(path);
267 		ISP_LOCK(isp);
268 		xpt_bus_deregister(cam_sim_path(sim));
269 		ISP_UNLOCK(isp);
270 		cam_sim_free(sim, FALSE);
271 	}
272 	if (isp->isp_osinfo.cdev) {
273 		destroy_dev(isp->isp_osinfo.cdev);
274 		isp->isp_osinfo.cdev = NULL;
275 	}
276 	if (isp->isp_osinfo.ehook_active) {
277 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
278 		isp->isp_osinfo.ehook_active = 0;
279 	}
280 	if (isp->isp_osinfo.devq == NULL) {
281 		cam_simq_free(isp->isp_osinfo.devq);
282 		isp->isp_osinfo.devq = NULL;
283 	}
284 }
285 
286 static void
287 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
288 {
289 	if (IS_FC(isp)) {
290 		struct isp_fc *fc = ISP_FC_PC(isp, chan);
291 		if (fc->simqfrozen == 0) {
292 			isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
293 			fc->simqfrozen = SIMQFRZ_LOOPDOWN;
294 			xpt_freeze_simq(fc->sim, 1);
295 		} else {
296 			isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
297 			fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
298 		}
299 	}
300 }
301 
302 
303 static int
304 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
305 {
306 	ispsoftc_t *isp;
307 	int nr, chan, retval = ENOTTY;
308 
309 	isp = dev->si_drv1;
310 
311 	switch (c) {
312 	case ISP_SDBLEV:
313 	{
314 		int olddblev = isp->isp_dblev;
315 		isp->isp_dblev = *(int *)addr;
316 		*(int *)addr = olddblev;
317 		retval = 0;
318 		break;
319 	}
320 	case ISP_GETROLE:
321 		chan = *(int *)addr;
322 		if (chan < 0 || chan >= isp->isp_nchan) {
323 			retval = -ENXIO;
324 			break;
325 		}
326 		if (IS_FC(isp)) {
327 			*(int *)addr = FCPARAM(isp, chan)->role;
328 		} else {
329 			*(int *)addr = SDPARAM(isp, chan)->role;
330 		}
331 		retval = 0;
332 		break;
333 	case ISP_SETROLE:
334 		nr = *(int *)addr;
335 		chan = nr >> 8;
336 		if (chan < 0 || chan >= isp->isp_nchan) {
337 			retval = -ENXIO;
338 			break;
339 		}
340 		nr &= 0xff;
341 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
342 			retval = EINVAL;
343 			break;
344 		}
345 		if (IS_FC(isp)) {
346 			/*
347 			 * We don't really support dual role at present on FC cards.
348 			 *
349 			 * We should, but a bunch of things are currently broken,
350 			 * so don't allow it.
351 			 */
352 			if (nr == ISP_ROLE_BOTH) {
353 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
354 				retval = EINVAL;
355 				break;
356 			}
357 			*(int *)addr = FCPARAM(isp, chan)->role;
358 #ifdef	ISP_INTERNAL_TARGET
359 			ISP_LOCK(isp);
360 			retval = isp_fc_change_role(isp, chan, nr);
361 			ISP_UNLOCK(isp);
362 #else
363 			FCPARAM(isp, chan)->role = nr;
364 #endif
365 		} else {
366 			*(int *)addr = SDPARAM(isp, chan)->role;
367 			SDPARAM(isp, chan)->role = nr;
368 		}
369 		retval = 0;
370 		break;
371 
372 	case ISP_RESETHBA:
373 		ISP_LOCK(isp);
374 #ifdef	ISP_TARGET_MODE
375 		isp_del_all_wwn_entries(isp, ISP_NOCHAN);
376 #endif
377 		isp_reinit(isp, 0);
378 		ISP_UNLOCK(isp);
379 		retval = 0;
380 		break;
381 
382 	case ISP_RESCAN:
383 		if (IS_FC(isp)) {
384 			chan = *(int *)addr;
385 			if (chan < 0 || chan >= isp->isp_nchan) {
386 				retval = -ENXIO;
387 				break;
388 			}
389 			ISP_LOCK(isp);
390 			if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
391 				retval = EIO;
392 			} else {
393 				retval = 0;
394 			}
395 			ISP_UNLOCK(isp);
396 		}
397 		break;
398 
399 	case ISP_FC_LIP:
400 		if (IS_FC(isp)) {
401 			chan = *(int *)addr;
402 			if (chan < 0 || chan >= isp->isp_nchan) {
403 				retval = -ENXIO;
404 				break;
405 			}
406 			ISP_LOCK(isp);
407 			if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
408 				retval = EIO;
409 			} else {
410 				retval = 0;
411 			}
412 			ISP_UNLOCK(isp);
413 		}
414 		break;
415 	case ISP_FC_GETDINFO:
416 	{
417 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
418 		fcportdb_t *lp;
419 
420 		if (IS_SCSI(isp)) {
421 			break;
422 		}
423 		if (ifc->loopid >= MAX_FC_TARG) {
424 			retval = EINVAL;
425 			break;
426 		}
427 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
428 		if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
429 			ifc->role = lp->roles;
430 			ifc->loopid = lp->handle;
431 			ifc->portid = lp->portid;
432 			ifc->node_wwn = lp->node_wwn;
433 			ifc->port_wwn = lp->port_wwn;
434 			retval = 0;
435 		} else {
436 			retval = ENODEV;
437 		}
438 		break;
439 	}
440 	case ISP_GET_STATS:
441 	{
442 		isp_stats_t *sp = (isp_stats_t *) addr;
443 
444 		ISP_MEMZERO(sp, sizeof (*sp));
445 		sp->isp_stat_version = ISP_STATS_VERSION;
446 		sp->isp_type = isp->isp_type;
447 		sp->isp_revision = isp->isp_revision;
448 		ISP_LOCK(isp);
449 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
450 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
451 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
452 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
453 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
454 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
455 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
456 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
457 		ISP_UNLOCK(isp);
458 		retval = 0;
459 		break;
460 	}
461 	case ISP_CLR_STATS:
462 		ISP_LOCK(isp);
463 		isp->isp_intcnt = 0;
464 		isp->isp_intbogus = 0;
465 		isp->isp_intmboxc = 0;
466 		isp->isp_intoasync = 0;
467 		isp->isp_rsltccmplt = 0;
468 		isp->isp_fphccmplt = 0;
469 		isp->isp_rscchiwater = 0;
470 		isp->isp_fpcchiwater = 0;
471 		ISP_UNLOCK(isp);
472 		retval = 0;
473 		break;
474 	case ISP_FC_GETHINFO:
475 	{
476 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
477 		int chan = hba->fc_channel;
478 
479 		if (chan < 0 || chan >= isp->isp_nchan) {
480 			retval = ENXIO;
481 			break;
482 		}
483 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
484 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
485 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
486 		hba->fc_nchannels = isp->isp_nchan;
487 		if (IS_FC(isp)) {
488 			hba->fc_nports = MAX_FC_TARG;
489 			hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
490 			hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
491 			hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
492 			hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
493 			hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
494 			hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
495 			hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
496 		} else {
497 			hba->fc_nports = MAX_TARGETS;
498 			hba->fc_speed = 0;
499 			hba->fc_topology = 0;
500 			hba->nvram_node_wwn = 0ull;
501 			hba->nvram_port_wwn = 0ull;
502 			hba->active_node_wwn = 0ull;
503 			hba->active_port_wwn = 0ull;
504 		}
505 		retval = 0;
506 		break;
507 	}
508 	case ISP_TSK_MGMT:
509 	{
510 		int needmarker;
511 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
512 		uint16_t loopid;
513 		mbreg_t mbs;
514 
515 		if (IS_SCSI(isp)) {
516 			break;
517 		}
518 
519 		chan = fct->chan;
520 		if (chan < 0 || chan >= isp->isp_nchan) {
521 			retval = -ENXIO;
522 			break;
523 		}
524 
525 		needmarker = retval = 0;
526 		loopid = fct->loopid;
527 		ISP_LOCK(isp);
528 		if (IS_24XX(isp)) {
529 			uint8_t local[QENTRY_LEN];
530 			isp24xx_tmf_t *tmf;
531 			isp24xx_statusreq_t *sp;
532 			fcparam *fcp = FCPARAM(isp, chan);
533 			fcportdb_t *lp;
534 			int i;
535 
536 			for (i = 0; i < MAX_FC_TARG; i++) {
537 				lp = &fcp->portdb[i];
538 				if (lp->handle == loopid) {
539 					break;
540 				}
541 			}
542 			if (i == MAX_FC_TARG) {
543 				retval = ENXIO;
544 				ISP_UNLOCK(isp);
545 				break;
546 			}
547 			/* XXX VALIDATE LP XXX */
548 			tmf = (isp24xx_tmf_t *) local;
549 			ISP_MEMZERO(tmf, QENTRY_LEN);
550 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
551 			tmf->tmf_header.rqs_entry_count = 1;
552 			tmf->tmf_nphdl = lp->handle;
553 			tmf->tmf_delay = 2;
554 			tmf->tmf_timeout = 2;
555 			tmf->tmf_tidlo = lp->portid;
556 			tmf->tmf_tidhi = lp->portid >> 16;
557 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
558 			tmf->tmf_lun[1] = fct->lun & 0xff;
559 			if (fct->lun >= 256) {
560 				tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
561 			}
562 			switch (fct->action) {
563 			case IPT_CLEAR_ACA:
564 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
565 				break;
566 			case IPT_TARGET_RESET:
567 				tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
568 				needmarker = 1;
569 				break;
570 			case IPT_LUN_RESET:
571 				tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
572 				needmarker = 1;
573 				break;
574 			case IPT_CLEAR_TASK_SET:
575 				tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
576 				needmarker = 1;
577 				break;
578 			case IPT_ABORT_TASK_SET:
579 				tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
580 				needmarker = 1;
581 				break;
582 			default:
583 				retval = EINVAL;
584 				break;
585 			}
586 			if (retval) {
587 				ISP_UNLOCK(isp);
588 				break;
589 			}
590 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
591 			mbs.param[1] = QENTRY_LEN;
592 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
593 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
594 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
595 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
596 
597 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
598 				ISP_UNLOCK(isp);
599 				retval = ENOMEM;
600 				break;
601 			}
602 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
603 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN);
604 			sp = (isp24xx_statusreq_t *) local;
605 			sp->req_completion_status = 1;
606 			retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
607 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN);
608 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
609 			FC_SCRATCH_RELEASE(isp, chan);
610 			if (retval || sp->req_completion_status != 0) {
611 				FC_SCRATCH_RELEASE(isp, chan);
612 				retval = EIO;
613 			}
614 			if (retval == 0) {
615 				if (needmarker) {
616 					fcp->sendmarker = 1;
617 				}
618 			}
619 		} else {
620 			MBSINIT(&mbs, 0, MBLOGALL, 0);
621 			if (ISP_CAP_2KLOGIN(isp) == 0) {
622 				loopid <<= 8;
623 			}
624 			switch (fct->action) {
625 			case IPT_CLEAR_ACA:
626 				mbs.param[0] = MBOX_CLEAR_ACA;
627 				mbs.param[1] = loopid;
628 				mbs.param[2] = fct->lun;
629 				break;
630 			case IPT_TARGET_RESET:
631 				mbs.param[0] = MBOX_TARGET_RESET;
632 				mbs.param[1] = loopid;
633 				needmarker = 1;
634 				break;
635 			case IPT_LUN_RESET:
636 				mbs.param[0] = MBOX_LUN_RESET;
637 				mbs.param[1] = loopid;
638 				mbs.param[2] = fct->lun;
639 				needmarker = 1;
640 				break;
641 			case IPT_CLEAR_TASK_SET:
642 				mbs.param[0] = MBOX_CLEAR_TASK_SET;
643 				mbs.param[1] = loopid;
644 				mbs.param[2] = fct->lun;
645 				needmarker = 1;
646 				break;
647 			case IPT_ABORT_TASK_SET:
648 				mbs.param[0] = MBOX_ABORT_TASK_SET;
649 				mbs.param[1] = loopid;
650 				mbs.param[2] = fct->lun;
651 				needmarker = 1;
652 				break;
653 			default:
654 				retval = EINVAL;
655 				break;
656 			}
657 			if (retval == 0) {
658 				if (needmarker) {
659 					FCPARAM(isp, chan)->sendmarker = 1;
660 				}
661 				retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
662 				if (retval) {
663 					retval = EIO;
664 				}
665 			}
666 		}
667 		ISP_UNLOCK(isp);
668 		break;
669 	}
670 	default:
671 		break;
672 	}
673 	return (retval);
674 }
675 
676 static void
677 isp_intr_enable(void *arg)
678 {
679 	int chan;
680 	ispsoftc_t *isp = arg;
681 	ISP_LOCK(isp);
682 	for (chan = 0; chan < isp->isp_nchan; chan++) {
683 		if (IS_FC(isp)) {
684 			if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
685 				ISP_ENABLE_INTS(isp);
686 				break;
687 			}
688 		} else {
689 			if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
690 				ISP_ENABLE_INTS(isp);
691 				break;
692 			}
693 		}
694 	}
695 	ISP_UNLOCK(isp);
696 	/* Release our hook so that the boot can continue. */
697 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
698 }
699 
700 /*
701  * Local Inlines
702  */
703 
704 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
705 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
706 
707 static ISP_INLINE int
708 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
709 {
710 	ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
711 	if (ISP_PCMD(ccb) == NULL) {
712 		return (-1);
713 	}
714 	isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
715 	return (0);
716 }
717 
718 static ISP_INLINE void
719 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
720 {
721 	((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
722 	isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
723 	ISP_PCMD(ccb) = NULL;
724 }
725 /*
726  * Put the target mode functions here, because some are inlines
727  */
728 
729 #ifdef	ISP_TARGET_MODE
730 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
731 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
732 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
733 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
734 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
735 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
736 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
737 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
738 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
739 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
740 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
741 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
742 static void isp_enable_lun(ispsoftc_t *, union ccb *);
743 static void isp_enable_deferred_luns(ispsoftc_t *, int);
744 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
745 static void isp_disable_lun(ispsoftc_t *, union ccb *);
746 static int isp_enable_target_mode(ispsoftc_t *, int);
747 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
748 static timeout_t isp_refire_putback_atio;
749 static void isp_complete_ctio(union ccb *);
750 static void isp_target_putback_atio(union ccb *);
751 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
752 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
753 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
754 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
755 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
756 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
757 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
758 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
759 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
760 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
761 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
762 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
763 
764 static ISP_INLINE int
765 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
766 {
767 	tstate_t *tptr;
768 	struct tslist *lhp;
769 
770 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
771 	SLIST_FOREACH(tptr, lhp, next) {
772 		if (xpt_path_lun_id(tptr->owner) == lun) {
773 			return (1);
774 		}
775 	}
776 	return (0);
777 }
778 
779 static void
780 dump_tstates(ispsoftc_t *isp, int bus)
781 {
782 	int i, j;
783 	struct tslist *lhp;
784 	tstate_t *tptr = NULL;
785 
786 	if (bus >= isp->isp_nchan) {
787 		return;
788 	}
789 	for (i = 0; i < LUN_HASH_SIZE; i++) {
790 		ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
791 		j = 0;
792 		SLIST_FOREACH(tptr, lhp, next) {
793 			xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
794 			j++;
795 		}
796 	}
797 }
798 
799 static ISP_INLINE tstate_t *
800 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
801 {
802 	tstate_t *tptr = NULL;
803 	struct tslist *lhp;
804 	int i;
805 
806 	if (bus < isp->isp_nchan) {
807 		for (i = 0; i < LUN_HASH_SIZE; i++) {
808 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
809 			SLIST_FOREACH(tptr, lhp, next) {
810 				if (xpt_path_lun_id(tptr->owner) == lun) {
811 					tptr->hold++;
812 					return (tptr);
813 				}
814 			}
815 		}
816 	}
817 	return (NULL);
818 }
819 
820 static ISP_INLINE tstate_t *
821 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
822 {
823 	tstate_t *tptr = NULL;
824 	atio_private_data_t *atp;
825 	struct tslist *lhp;
826 	int i;
827 
828 	if (bus < isp->isp_nchan && tagval != 0) {
829 		for (i = 0; i < LUN_HASH_SIZE; i++) {
830 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
831 			SLIST_FOREACH(tptr, lhp, next) {
832 				atp = isp_get_atpd(isp, tptr, tagval);
833 				if (atp && atp->tag == tagval) {
834 					tptr->hold++;
835 					return (tptr);
836 				}
837 			}
838 		}
839 	}
840 	return (NULL);
841 }
842 
843 static ISP_INLINE inot_private_data_t *
844 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
845 {
846 	inot_private_data_t *ntp;
847 	tstate_t *tptr;
848 	struct tslist *lhp;
849 	int bus, i;
850 
851 	for (bus = 0; bus < isp->isp_nchan; bus++) {
852 		for (i = 0; i < LUN_HASH_SIZE; i++) {
853 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
854 			SLIST_FOREACH(tptr, lhp, next) {
855 				ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
856 				if (ntp) {
857 					*rslt = tptr;
858 					tptr->hold++;
859 					return (ntp);
860 				}
861 			}
862 		}
863 	}
864 	return (NULL);
865 }
866 static ISP_INLINE void
867 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
868 {
869 	KASSERT((tptr->hold), ("tptr not held"));
870 	tptr->hold--;
871 }
872 
873 static void
874 isp_tmcmd_restart(ispsoftc_t *isp)
875 {
876 	inot_private_data_t *ntp;
877 	tstate_t *tptr;
878 	struct tslist *lhp;
879 	int bus, i;
880 
881 	for (bus = 0; bus < isp->isp_nchan; bus++) {
882 		for (i = 0; i < LUN_HASH_SIZE; i++) {
883 			ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
884 			SLIST_FOREACH(tptr, lhp, next) {
885 				inot_private_data_t *restart_queue = tptr->restart_queue;
886 				tptr->restart_queue = NULL;
887 				while (restart_queue) {
888 					ntp = restart_queue;
889 					restart_queue = ntp->rd.nt.nt_hba;
890 					if (IS_24XX(isp)) {
891 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
892 						isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
893 					} else {
894 						isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
895 						isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
896 					}
897 					isp_put_ntpd(isp, tptr, ntp);
898 					if (tptr->restart_queue && restart_queue != NULL) {
899 						ntp = tptr->restart_queue;
900 						tptr->restart_queue = restart_queue;
901 						while (restart_queue->rd.nt.nt_hba) {
902 							restart_queue = restart_queue->rd.nt.nt_hba;
903 						}
904 						restart_queue->rd.nt.nt_hba = ntp;
905 						break;
906 					}
907 				}
908 			}
909 		}
910 	}
911 }
912 
913 static ISP_INLINE atio_private_data_t *
914 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
915 {
916 	atio_private_data_t *atp;
917 
918 	if (tag == 0) {
919 		atp = tptr->atfree;
920 		if (atp) {
921 			tptr->atfree = atp->next;
922 		}
923 		return (atp);
924 	}
925 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
926 		if (atp->tag == tag) {
927 			return (atp);
928 		}
929 	}
930 	return (NULL);
931 }
932 
933 static ISP_INLINE void
934 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
935 {
936 	atp->tag = 0;
937 	atp->dead = 0;
938 	atp->next = tptr->atfree;
939 	tptr->atfree = atp;
940 }
941 
942 static void
943 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
944 {
945 	atio_private_data_t *atp;
946 	const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
947 
948 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
949 		if (atp->tag == 0) {
950 			continue;
951 		}
952 		xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u last_xfr %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
953                     atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->last_xframt, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
954 	}
955 }
956 
957 
958 static ISP_INLINE inot_private_data_t *
959 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
960 {
961 	inot_private_data_t *ntp;
962 	ntp = tptr->ntfree;
963 	if (ntp) {
964 		tptr->ntfree = ntp->next;
965 	}
966 	return (ntp);
967 }
968 
969 static ISP_INLINE inot_private_data_t *
970 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
971 {
972 	inot_private_data_t *ntp;
973 	for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
974 		if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
975 			return (ntp);
976 		}
977 	}
978 	return (NULL);
979 }
980 
981 static ISP_INLINE void
982 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
983 {
984 	ntp->rd.tag_id = ntp->rd.seq_id = 0;
985 	ntp->next = tptr->ntfree;
986 	tptr->ntfree = ntp;
987 }
988 
989 static cam_status
990 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
991 {
992 	cam_status status;
993 	lun_id_t lun;
994 	struct tslist *lhp;
995 	tstate_t *tptr;
996 	int i;
997 
998 	lun = xpt_path_lun_id(path);
999 	if (lun != CAM_LUN_WILDCARD) {
1000 		if (lun >= ISP_MAX_LUNS(isp)) {
1001 			return (CAM_LUN_INVALID);
1002 		}
1003 	}
1004 	if (is_lun_enabled(isp, bus, lun)) {
1005 		return (CAM_LUN_ALRDY_ENA);
1006 	}
1007 	tptr = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1008 	if (tptr == NULL) {
1009 		return (CAM_RESRC_UNAVAIL);
1010 	}
1011 	status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1012 	if (status != CAM_REQ_CMP) {
1013 		free(tptr, M_DEVBUF);
1014 		return (status);
1015 	}
1016 	SLIST_INIT(&tptr->atios);
1017 	SLIST_INIT(&tptr->inots);
1018 	for (i = 0; i < ATPDPSIZE-1; i++) {
1019 		tptr->atpool[i].next = &tptr->atpool[i+1];
1020 		tptr->ntpool[i].next = &tptr->ntpool[i+1];
1021 	}
1022 	tptr->atfree = tptr->atpool;
1023 	tptr->ntfree = tptr->ntpool;
1024 	tptr->hold = 1;
1025 	ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1026 	SLIST_INSERT_HEAD(lhp, tptr, next);
1027 	*rslt = tptr;
1028 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1029 	return (CAM_REQ_CMP);
1030 }
1031 
1032 static ISP_INLINE void
1033 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1034 {
1035 	struct tslist *lhp;
1036 	KASSERT((tptr->hold == 0), ("tptr still held"));
1037 	ISP_GET_PC_ADDR(isp, xpt_path_path_id(tptr->owner), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1038 	SLIST_REMOVE(lhp, tptr, tstate, next);
1039 	xpt_free_path(tptr->owner);
1040 	free(tptr, M_DEVBUF);
1041 }
1042 
1043 /*
1044  * Enable a lun.
1045  */
1046 static void
1047 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1048 {
1049 	tstate_t *tptr = NULL;
1050 	int bus, tm_enabled, target_role;
1051 	target_id_t target;
1052 	lun_id_t lun;
1053 
1054 	/*
1055 	 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1056 	 */
1057 	bus = XS_CHANNEL(ccb);
1058 	target = ccb->ccb_h.target_id;
1059 	lun = ccb->ccb_h.target_lun;
1060 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1061 		ccb->ccb_h.status = CAM_TID_INVALID;
1062 		xpt_done(ccb);
1063 		return;
1064 	}
1065 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1066 		ccb->ccb_h.status = CAM_LUN_INVALID;
1067 		xpt_done(ccb);
1068 		return;
1069 	}
1070 
1071 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1072 		ccb->ccb_h.status = CAM_LUN_INVALID;
1073 		xpt_done(ccb);
1074 		return;
1075 	}
1076 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1077 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1078 	}
1079 
1080 	/*
1081 	 * Wait until we're not busy with the lun enables subsystem
1082 	 */
1083 	while (isp->isp_osinfo.tmbusy) {
1084 		isp->isp_osinfo.tmwanted = 1;
1085 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_enable_lun", 0);
1086 	}
1087 	isp->isp_osinfo.tmbusy = 1;
1088 
1089 	/*
1090 	 * This is as a good a place as any to check f/w capabilities.
1091 	 */
1092 
1093 	if (IS_FC(isp)) {
1094 		if (ISP_CAP_TMODE(isp) == 0) {
1095 			xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1096 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1097 			goto done;
1098 		}
1099 		/*
1100 		 * We *could* handle non-SCCLUN f/w, but we'd have to
1101 		 * dork with our already fragile enable/disable code.
1102 		 */
1103 		if (ISP_CAP_SCCFW(isp) == 0) {
1104 			xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1105 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1106 			goto done;
1107 		}
1108 
1109 		target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1110 
1111 	} else {
1112 		target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1113 	}
1114 
1115 	/*
1116 	 * Create the state pointer.
1117 	 * It should not already exist.
1118 	 */
1119 	tptr = get_lun_statep(isp, bus, lun);
1120 	if (tptr) {
1121 		ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1122 		goto done;
1123 	}
1124 	ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1125 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
1126 		goto done;
1127 	}
1128 
1129 	/*
1130 	 * We have a tricky maneuver to perform here.
1131 	 *
1132 	 * If target mode isn't already enabled here,
1133 	 * *and* our current role includes target mode,
1134 	 * we enable target mode here.
1135 	 *
1136 	 */
1137 	ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1138 	if (tm_enabled == 0 && target_role != 0) {
1139 		if (isp_enable_target_mode(isp, bus)) {
1140 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1141 			destroy_lun_state(isp, tptr);
1142 			tptr = NULL;
1143 			goto done;
1144 		}
1145 		tm_enabled = 1;
1146 	}
1147 
1148 	/*
1149 	 * Now check to see whether this bus is in target mode already.
1150 	 *
1151 	 * If not, a later role change into target mode will finish the job.
1152 	 */
1153 	if (tm_enabled == 0) {
1154 		ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1155 		ccb->ccb_h.status = CAM_REQ_CMP;
1156 		xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
1157 		goto done;
1158 	}
1159 
1160 	/*
1161 	 * Enable the lun.
1162 	 */
1163 	ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1164 
1165 done:
1166 	if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
1167 		destroy_lun_state(isp, tptr);
1168 		tptr = NULL;
1169 	}
1170 	if (tptr) {
1171 		rls_lun_statep(isp, tptr);
1172 	}
1173 	isp->isp_osinfo.tmbusy = 0;
1174 	if (isp->isp_osinfo.tmwanted) {
1175 		isp->isp_osinfo.tmwanted = 0;
1176 		wakeup(isp);
1177 	}
1178 	xpt_done(ccb);
1179 }
1180 
1181 static void
1182 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1183 {
1184 	/*
1185  	 * XXX: not entirely implemented yet
1186 	 */
1187 	(void) isp_enable_deferred(isp, bus, 0);
1188 }
1189 
1190 static uint32_t
1191 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1192 {
1193 	cam_status status;
1194 
1195 	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1196 	if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
1197 		status = CAM_REQ_CMP;
1198 	} else {
1199 		int cmd_cnt, not_cnt;
1200 
1201 		if (IS_23XX(isp)) {
1202 			cmd_cnt = DFLT_CMND_CNT;
1203 			not_cnt = DFLT_INOT_CNT;
1204 		} else {
1205 			cmd_cnt = 64;
1206 			not_cnt = 8;
1207 		}
1208 		status = CAM_REQ_INPROG;
1209 		isp->isp_osinfo.rptr = &status;
1210 		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1211 			status = CAM_RESRC_UNAVAIL;
1212 		} else {
1213 			mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1214 		}
1215 		isp->isp_osinfo.rptr = NULL;
1216 	}
1217 
1218 	if (status == CAM_REQ_CMP) {
1219 		ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1220 		isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1221 	}
1222 	return (status);
1223 }
1224 
1225 static void
1226 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1227 {
1228 	tstate_t *tptr = NULL;
1229 	int bus;
1230 	cam_status status;
1231 	target_id_t target;
1232 	lun_id_t lun;
1233 
1234 	bus = XS_CHANNEL(ccb);
1235 	target = ccb->ccb_h.target_id;
1236 	lun = ccb->ccb_h.target_lun;
1237 	if (target != CAM_TARGET_WILDCARD && target != 0) {
1238 		ccb->ccb_h.status = CAM_TID_INVALID;
1239 		xpt_done(ccb);
1240 		return;
1241 	}
1242 	if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1243 		ccb->ccb_h.status = CAM_LUN_INVALID;
1244 		xpt_done(ccb);
1245 		return;
1246 	}
1247 
1248 	if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1249 		ccb->ccb_h.status = CAM_LUN_INVALID;
1250 		xpt_done(ccb);
1251 		return;
1252 	}
1253 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1254 		xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1255 	}
1256 
1257 	/*
1258 	 * See if we're busy disabling a lun now.
1259 	 */
1260 	while (isp->isp_osinfo.tmbusy) {
1261 		isp->isp_osinfo.tmwanted = 1;
1262 		mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
1263 	}
1264 	isp->isp_osinfo.tmbusy = 1;
1265 
1266 	/*
1267 	 * Find the state pointer.
1268 	 */
1269 	if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1270 		ccb->ccb_h.status = CAM_PATH_INVALID;
1271 		goto done;
1272 	}
1273 
1274 	/*
1275 	 * If we're a 24XX card, we're done.
1276 	 */
1277 	if (IS_24XX(isp)) {
1278 		status = CAM_REQ_CMP;
1279 		goto done;
1280 	}
1281 
1282 	/*
1283 	 * For SCC FW, we only deal with lun zero.
1284 	 */
1285 	if (IS_FC(isp)) {
1286 		lun = 0;
1287 	}
1288 
1289 	isp->isp_osinfo.rptr = &status;
1290 	status = CAM_REQ_INPROG;
1291 	if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1292 		status = CAM_RESRC_UNAVAIL;
1293 	} else {
1294 		mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1295 	}
1296 done:
1297 	if (status == CAM_REQ_CMP) {
1298 		xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
1299 	}
1300 	if (tptr) {
1301 		rls_lun_statep(isp, tptr);
1302 	}
1303 	isp->isp_osinfo.rptr = NULL;
1304 	isp->isp_osinfo.tmbusy = 0;
1305 	if (isp->isp_osinfo.tmwanted) {
1306 		isp->isp_osinfo.tmwanted = 0;
1307 		wakeup(isp);
1308 	}
1309 	xpt_done(ccb);
1310 }
1311 
1312 static int
1313 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1314 {
1315 	int ct;
1316 
1317 	ISP_GET_PC(isp, bus, tm_enabled, ct);
1318 	if (ct != 0) {
1319 		return (0);
1320 	}
1321 
1322 	if (IS_SCSI(isp)) {
1323 		mbreg_t mbs;
1324 
1325 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1326 		mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1327 		mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1328 		mbs.param[2] = bus << 7;
1329 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1330 			isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
1331 			return (EIO);
1332 		}
1333 		SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
1334 	}
1335 	ISP_SET_PC(isp, bus, tm_enabled, 1);
1336 	isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
1337 	return (0);
1338 }
1339 
1340 #ifdef	NEEDED
1341 static int
1342 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1343 {
1344 	int ct;
1345 
1346 	ISP_GET_PC(isp, bus, tm_enabled, ct);
1347 	if (ct == 0) {
1348 		return (0);
1349 	}
1350 
1351 	if (IS_SCSI(isp)) {
1352 		mbreg_t mbs;
1353 
1354 		MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1355 		mbs.param[2] = bus << 7;
1356 		if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1357 			isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
1358 			return (EIO);
1359 		}
1360 		SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
1361 	}
1362 	ISP_SET_PC(isp, bus, tm_enabled, 0);
1363 	isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
1364 	return (0);
1365 }
1366 #endif
1367 
1368 static void
1369 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1370 {
1371 	uint32_t *rptr;
1372 
1373 	rptr = isp->isp_osinfo.rptr;
1374 	if (lep->le_status != LUN_OK) {
1375 		isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1376 		if (rptr) {
1377 			*rptr = CAM_REQ_CMP_ERR;
1378 			wakeup_one(rptr);
1379 		}
1380 	} else {
1381 		if (rptr) {
1382 			*rptr = CAM_REQ_CMP;
1383 			wakeup_one(rptr);
1384 		}
1385 	}
1386 }
1387 
1388 static void
1389 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1390 {
1391 	void *qe;
1392 	tstate_t *tptr;
1393 	atio_private_data_t *atp;
1394 	struct ccb_scsiio *cso = &ccb->csio;
1395 	uint32_t dmaresult, handle;
1396 	uint8_t local[QENTRY_LEN];
1397 
1398 	/*
1399 	 * Do some sanity checks.
1400 	 */
1401 	if (cso->dxfer_len == 0) {
1402 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1403 			xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1404 			ccb->ccb_h.status = CAM_REQ_INVALID;
1405 			xpt_done(ccb);
1406 			return;
1407 		}
1408 	}
1409 
1410 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1411 	if (tptr == NULL) {
1412 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1413 		if (tptr == NULL) {
1414 			xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1415 			dump_tstates(isp, XS_CHANNEL(ccb));
1416 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1417 			xpt_done(ccb);
1418 			return;
1419 		}
1420 	}
1421 
1422 	atp = isp_get_atpd(isp, tptr, cso->tag_id);
1423 	if (atp == NULL) {
1424 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1425 		isp_dump_atpd(isp, tptr);
1426 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1427 		xpt_done(ccb);
1428 		return;
1429 	}
1430 	if (atp->dead) {
1431 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1432 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1433 		xpt_done(ccb);
1434 		return;
1435 	}
1436 
1437 	/*
1438 	 * Check to make sure we're still in target mode.
1439 	 */
1440 	if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1441 		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
1442 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1443 		xpt_done(ccb);
1444 		return;
1445 	}
1446 
1447 	/*
1448 	 * Get some resources
1449 	 */
1450 	if (isp_get_pcmd(isp, ccb)) {
1451 		rls_lun_statep(isp, tptr);
1452 		xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1453 		cam_freeze_devq(ccb->ccb_h.path);
1454 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1455 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1456 		xpt_done(ccb);
1457 		return;
1458 	}
1459 	qe = isp_getrqentry(isp);
1460 	if (qe == NULL) {
1461 		xpt_print(ccb->ccb_h.path, rqo, __func__);
1462 		cam_freeze_devq(ccb->ccb_h.path);
1463 		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1464 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1465 		goto out;
1466 	}
1467 	memset(local, 0, QENTRY_LEN);
1468 
1469 	/*
1470 	 * We're either moving data or completing a command here.
1471 	 */
1472 	if (IS_24XX(isp)) {
1473 		ct7_entry_t *cto = (ct7_entry_t *) local;
1474 
1475 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1476 		cto->ct_header.rqs_entry_count = 1;
1477 		cto->ct_header.rqs_seqno = 1;
1478 		cto->ct_nphdl = atp->nphdl;
1479 		cto->ct_rxid = atp->tag;
1480 		cto->ct_iid_lo = atp->portid;
1481 		cto->ct_iid_hi = atp->portid >> 16;
1482 		cto->ct_oxid = atp->oxid;
1483 		cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1484 		cto->ct_scsi_status = cso->scsi_status;
1485 		cto->ct_timeout = 120;
1486 		cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1487 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1488 			cto->ct_flags |= CT7_SENDSTATUS;
1489 		}
1490 		if (cso->dxfer_len == 0) {
1491 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1492 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1493 				int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1494 				cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1495 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1496 				cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1497 			}
1498 		} else {
1499 			cto->ct_flags |= CT7_FLAG_MODE0;
1500 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1501 				cto->ct_flags |= CT7_DATA_IN;
1502 			} else {
1503 				cto->ct_flags |= CT7_DATA_OUT;
1504 			}
1505 			cto->rsp.m0.reloff = atp->bytes_xfered;
1506 			/*
1507 			 * Don't overrun the limits placed on us
1508 			 */
1509 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1510 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1511 			}
1512 			atp->last_xframt = cso->dxfer_len;
1513 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1514 		}
1515 		if (cto->ct_flags & CT7_SENDSTATUS) {
1516 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1517 			cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1518 			if (cto->ct_resid < 0) {
1519 				cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1520 			} else if (cto->ct_resid > 0) {
1521 				cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1522 			}
1523 			atp->state = ATPD_STATE_LAST_CTIO;
1524 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1525 			    atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1526 		} else {
1527 			cto->ct_resid = 0;
1528 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1529 			    cso->dxfer_len, atp->bytes_xfered);
1530 			atp->state = ATPD_STATE_CTIO;
1531 		}
1532 	} else if (IS_FC(isp)) {
1533 		ct2_entry_t *cto = (ct2_entry_t *) local;
1534 
1535 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1536 		cto->ct_header.rqs_entry_count = 1;
1537 		cto->ct_header.rqs_seqno = 1;
1538 		if (ISP_CAP_2KLOGIN(isp) == 0) {
1539 			((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1540 		} else {
1541 			cto->ct_iid = cso->init_id;
1542 			if (ISP_CAP_SCCFW(isp) == 0) {
1543 				cto->ct_lun = ccb->ccb_h.target_lun;
1544 			}
1545 		}
1546 
1547 
1548 		cto->ct_rxid = cso->tag_id;
1549 		if (cso->dxfer_len == 0) {
1550 			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1551 			cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1552 			cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1553 			if (cto->ct_resid < 0) {
1554 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1555 			} else if (cto->ct_resid > 0) {
1556 				cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1557 			}
1558 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1559 				int m = min(cso->sense_len, MAXRESPLEN);
1560 				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1561 				cto->rsp.m1.ct_senselen = m;
1562 				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1563 			} else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1564 				/*
1565 				 * XXX: DEBUG
1566 				 */
1567 				xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1568 			}
1569 		} else {
1570 			cto->ct_flags |= CT2_FLAG_MODE0;
1571 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1572 				cto->ct_flags |= CT2_DATA_IN;
1573 			} else {
1574 				cto->ct_flags |= CT2_DATA_OUT;
1575 			}
1576 			cto->ct_reloff = atp->bytes_xfered;
1577 			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1578 			/*
1579 			 * Don't overrun the limits placed on us
1580 			 */
1581 			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1582 				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1583 			}
1584 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1585 				cto->ct_flags |= CT2_SENDSTATUS;
1586 				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1587 				cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1588 				if (cto->ct_resid < 0) {
1589 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1590 				} else if (cto->ct_resid > 0) {
1591 					cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1592 				}
1593 			} else {
1594 				atp->last_xframt = cso->dxfer_len;
1595 			}
1596 			/*
1597 			 * If we're sending data and status back together,
1598 			 * we can't also send back sense data as well.
1599 			 */
1600 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1601 		}
1602 
1603 		if (cto->ct_flags & CT2_SENDSTATUS) {
1604 			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1605 			cto->ct_flags |= CT2_CCINCR;
1606 			atp->state = ATPD_STATE_LAST_CTIO;
1607 			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1608 			    atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1609 		} else {
1610 			cto->ct_resid = 0;
1611 			atp->state = ATPD_STATE_CTIO;
1612 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1613 			    cso->dxfer_len, atp->bytes_xfered);
1614 		}
1615 		cto->ct_timeout = 10;
1616 	} else {
1617 		ct_entry_t *cto = (ct_entry_t *) local;
1618 
1619 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1620 		cto->ct_header.rqs_entry_count = 1;
1621 		cto->ct_header.rqs_seqno = 1;
1622 		cto->ct_iid = cso->init_id;
1623 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1624 		cto->ct_tgt = ccb->ccb_h.target_id;
1625 		cto->ct_lun = ccb->ccb_h.target_lun;
1626 		cto->ct_fwhandle = cso->tag_id >> 16;
1627 		if (AT_HAS_TAG(cso->tag_id)) {
1628 			cto->ct_tag_val = cso->tag_id;
1629 			cto->ct_flags |= CT_TQAE;
1630 		}
1631 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1632 			cto->ct_flags |= CT_NODISC;
1633 		}
1634 		if (cso->dxfer_len == 0) {
1635 			cto->ct_flags |= CT_NO_DATA;
1636 		} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1637 			cto->ct_flags |= CT_DATA_IN;
1638 		} else {
1639 			cto->ct_flags |= CT_DATA_OUT;
1640 		}
1641 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1642 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1643 			cto->ct_scsi_status = cso->scsi_status;
1644 			cto->ct_resid = cso->resid;
1645 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1646 			    cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1647 		}
1648 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1649 		cto->ct_timeout = 10;
1650 	}
1651 
1652 	if (isp_save_xs_tgt(isp, ccb, &handle)) {
1653 		xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1654 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1655 		goto out;
1656 	}
1657 
1658 
1659 	/*
1660 	 * Call the dma setup routines for this entry (and any subsequent
1661 	 * CTIOs) if there's data to move, and then tell the f/w it's got
1662 	 * new things to play with. As with isp_start's usage of DMA setup,
1663 	 * any swizzling is done in the machine dependent layer. Because
1664 	 * of this, we put the request onto the queue area first in native
1665 	 * format.
1666 	 */
1667 
1668 	if (IS_24XX(isp)) {
1669 		ct7_entry_t *cto = (ct7_entry_t *) local;
1670 		cto->ct_syshandle = handle;
1671 	} else if (IS_FC(isp)) {
1672 		ct2_entry_t *cto = (ct2_entry_t *) local;
1673 		cto->ct_syshandle = handle;
1674 	} else {
1675 		ct_entry_t *cto = (ct_entry_t *) local;
1676 		cto->ct_syshandle = handle;
1677 	}
1678 
1679 	dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1680 	if (dmaresult == CMD_QUEUED) {
1681 		isp->isp_nactive++;
1682 		ccb->ccb_h.status |= CAM_SIM_QUEUED;
1683 		rls_lun_statep(isp, tptr);
1684 		return;
1685 	}
1686 	if (dmaresult == CMD_EAGAIN) {
1687 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1688 	} else {
1689 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1690 	}
1691 	isp_destroy_tgt_handle(isp, handle);
1692 out:
1693 	rls_lun_statep(isp, tptr);
1694 	isp_free_pcmd(isp, ccb);
1695 	xpt_done(ccb);
1696 }
1697 
1698 static void
1699 isp_refire_putback_atio(void *arg)
1700 {
1701 	union ccb *ccb = arg;
1702 	ispsoftc_t *isp = XS_ISP(ccb);
1703 	ISP_LOCK(isp);
1704 	isp_target_putback_atio(ccb);
1705 	ISP_UNLOCK(isp);
1706 }
1707 
1708 static void
1709 isp_target_putback_atio(union ccb *ccb)
1710 {
1711 	ispsoftc_t *isp;
1712 	struct ccb_scsiio *cso;
1713 	void *qe;
1714 
1715 	isp = XS_ISP(ccb);
1716 
1717 	qe = isp_getrqentry(isp);
1718 	if (qe == NULL) {
1719 		xpt_print(ccb->ccb_h.path, rqo, __func__);
1720 		(void) timeout(isp_refire_putback_atio, ccb, 10);
1721 		return;
1722 	}
1723 	memset(qe, 0, QENTRY_LEN);
1724 	cso = &ccb->csio;
1725 	if (IS_FC(isp)) {
1726 		at2_entry_t local, *at = &local;
1727 		ISP_MEMZERO(at, sizeof (at2_entry_t));
1728 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1729 		at->at_header.rqs_entry_count = 1;
1730 		if (ISP_CAP_SCCFW(isp)) {
1731 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1732 		} else {
1733 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1734 		}
1735 		at->at_status = CT_OK;
1736 		at->at_rxid = cso->tag_id;
1737 		at->at_iid = cso->ccb_h.target_id;
1738 		isp_put_atio2(isp, at, qe);
1739 	} else {
1740 		at_entry_t local, *at = &local;
1741 		ISP_MEMZERO(at, sizeof (at_entry_t));
1742 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1743 		at->at_header.rqs_entry_count = 1;
1744 		at->at_iid = cso->init_id;
1745 		at->at_iid |= XS_CHANNEL(ccb) << 7;
1746 		at->at_tgt = cso->ccb_h.target_id;
1747 		at->at_lun = cso->ccb_h.target_lun;
1748 		at->at_status = CT_OK;
1749 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
1750 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
1751 		isp_put_atio(isp, at, qe);
1752 	}
1753 	ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1754 	ISP_SYNC_REQUEST(isp);
1755 	isp_complete_ctio(ccb);
1756 }
1757 
1758 static void
1759 isp_complete_ctio(union ccb *ccb)
1760 {
1761 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1762 		ccb->ccb_h.status |= CAM_REQ_CMP;
1763 	}
1764 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1765 	isp_free_pcmd(XS_ISP(ccb), ccb);
1766 	xpt_done(ccb);
1767 }
1768 
1769 /*
1770  * Handle ATIO stuff that the generic code can't.
1771  * This means handling CDBs.
1772  */
1773 
1774 static void
1775 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1776 {
1777 	tstate_t *tptr;
1778 	int status, bus;
1779 	struct ccb_accept_tio *atiop;
1780 	atio_private_data_t *atp;
1781 
1782 	/*
1783 	 * The firmware status (except for the QLTM_SVALID bit)
1784 	 * indicates why this ATIO was sent to us.
1785 	 *
1786 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1787 	 *
1788 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1789 	 * we're still connected on the SCSI bus.
1790 	 */
1791 	status = aep->at_status;
1792 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1793 		/*
1794 		 * Bus Phase Sequence error. We should have sense data
1795 		 * suggested by the f/w. I'm not sure quite yet what
1796 		 * to do about this for CAM.
1797 		 */
1798 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1799 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1800 		return;
1801 	}
1802 	if ((status & ~QLTM_SVALID) != AT_CDB) {
1803 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1804 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1805 		return;
1806 	}
1807 
1808 	bus = GET_BUS_VAL(aep->at_iid);
1809 	tptr = get_lun_statep(isp, bus, aep->at_lun);
1810 	if (tptr == NULL) {
1811 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1812 		if (tptr == NULL) {
1813 			/*
1814 			 * Because we can't autofeed sense data back with
1815 			 * a command for parallel SCSI, we can't give back
1816 			 * a CHECK CONDITION. We'll give back a BUSY status
1817 			 * instead. This works out okay because the only
1818 			 * time we should, in fact, get this, is in the
1819 			 * case that somebody configured us without the
1820 			 * blackhole driver, so they get what they deserve.
1821 			 */
1822 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1823 			return;
1824 		}
1825 	}
1826 
1827 	atp = isp_get_atpd(isp, tptr, 0);
1828 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1829 	if (atiop == NULL || atp == NULL) {
1830 		/*
1831 		 * Because we can't autofeed sense data back with
1832 		 * a command for parallel SCSI, we can't give back
1833 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1834 		 * instead. This works out okay because the only time we
1835 		 * should, in fact, get this, is in the case that we've
1836 		 * run out of ATIOS.
1837 		 */
1838 		xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1839 		    ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1840 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1841 		if (atp) {
1842 			isp_put_atpd(isp, tptr, atp);
1843 		}
1844 		rls_lun_statep(isp, tptr);
1845 		return;
1846 	}
1847 	atp->tag = aep->at_tag_val;
1848 	if (atp->tag == 0) {
1849 		atp->tag = ~0;
1850 	}
1851 	atp->state = ATPD_STATE_ATIO;
1852 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1853 	tptr->atio_count--;
1854 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1855 	atiop->ccb_h.target_id = aep->at_tgt;
1856 	atiop->ccb_h.target_lun = aep->at_lun;
1857 	if (aep->at_flags & AT_NODISC) {
1858 		atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1859 	} else {
1860 		atiop->ccb_h.flags = 0;
1861 	}
1862 
1863 	if (status & QLTM_SVALID) {
1864 		size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1865 		atiop->sense_len = amt;
1866 		ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1867 	} else {
1868 		atiop->sense_len = 0;
1869 	}
1870 
1871 	atiop->init_id = GET_IID_VAL(aep->at_iid);
1872 	atiop->cdb_len = aep->at_cdblen;
1873 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1874 	atiop->ccb_h.status = CAM_CDB_RECVD;
1875 	/*
1876 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
1877 	 * and the handle (which we have to preserve).
1878 	 */
1879 	atiop->tag_id = atp->tag;
1880 	if (aep->at_flags & AT_TQAE) {
1881 		atiop->tag_action = aep->at_tag_type;
1882 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1883 	}
1884 	atp->orig_datalen = 0;
1885 	atp->bytes_xfered = 0;
1886 	atp->last_xframt = 0;
1887 	atp->lun = aep->at_lun;
1888 	atp->nphdl = aep->at_iid;
1889 	atp->portid = PORT_NONE;
1890 	atp->oxid = 0;
1891 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1892 	atp->tattr = aep->at_tag_type;
1893 	atp->state = ATPD_STATE_CAM;
1894 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1895 	rls_lun_statep(isp, tptr);
1896 }
1897 
1898 static void
1899 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1900 {
1901 	lun_id_t lun;
1902 	fcportdb_t *lp;
1903 	tstate_t *tptr;
1904 	struct ccb_accept_tio *atiop;
1905 	uint16_t nphdl;
1906 	atio_private_data_t *atp = NULL;
1907 	inot_private_data_t *ntp;
1908 
1909 	/*
1910 	 * The firmware status (except for the QLTM_SVALID bit)
1911 	 * indicates why this ATIO was sent to us.
1912 	 *
1913 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1914 	 */
1915 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1916 		isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1917 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1918 		return;
1919 	}
1920 
1921 	if (ISP_CAP_SCCFW(isp)) {
1922 		lun = aep->at_scclun;
1923 	} else {
1924 		lun = aep->at_lun;
1925 	}
1926 	if (ISP_CAP_2KLOGIN(isp)) {
1927 		nphdl = ((at2e_entry_t *)aep)->at_iid;
1928 	} else {
1929 		nphdl = aep->at_iid;
1930 	}
1931 	tptr = get_lun_statep(isp, 0, lun);
1932 	if (tptr == NULL) {
1933 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1934 		if (tptr == NULL) {
1935 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1936 			isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1937 			return;
1938 		}
1939 	}
1940 
1941 	/*
1942 	 * Start any commands pending resources first.
1943 	 */
1944 	if (tptr->restart_queue) {
1945 		inot_private_data_t *restart_queue = tptr->restart_queue;
1946 		tptr->restart_queue = NULL;
1947 		while (restart_queue) {
1948 			ntp = restart_queue;
1949 			restart_queue = ntp->rd.nt.nt_hba;
1950 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1951 			isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1952 			isp_put_ntpd(isp, tptr, ntp);
1953 			/*
1954 			 * If a recursion caused the restart queue to start to fill again,
1955 			 * stop and splice the new list on top of the old list and restore
1956 			 * it and go to noresrc.
1957 			 */
1958 			if (tptr->restart_queue) {
1959 				ntp = tptr->restart_queue;
1960 				tptr->restart_queue = restart_queue;
1961 				while (restart_queue->rd.nt.nt_hba) {
1962 					restart_queue = restart_queue->rd.nt.nt_hba;
1963 				}
1964 				restart_queue->rd.nt.nt_hba = ntp;
1965 				goto noresrc;
1966 			}
1967 		}
1968 	}
1969 
1970 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1971 	if (atiop == NULL) {
1972 		goto noresrc;
1973 	}
1974 
1975 	atp = isp_get_atpd(isp, tptr, 0);
1976 	if (atp == NULL) {
1977 		goto noresrc;
1978 	}
1979 
1980 	atp->tag = aep->at_rxid;
1981 	atp->state = ATPD_STATE_ATIO;
1982 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1983 	tptr->atio_count--;
1984 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1985 	atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
1986 	atiop->ccb_h.target_lun = lun;
1987 
1988 	/*
1989 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1990 	 */
1991 	atiop->sense_len = 0;
1992 	if (ISP_CAP_2KLOGIN(isp)) {
1993 		/*
1994 		 * NB: We could not possibly have 2K logins if we
1995 		 * NB: also did not have SCC FW.
1996 		 */
1997 		atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
1998 	} else {
1999 		atiop->init_id = aep->at_iid;
2000 	}
2001 
2002 	/*
2003 	 * If we're not in the port database, add ourselves.
2004 	 */
2005 	if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2006     		uint64_t iid =
2007 			(((uint64_t) aep->at_wwpn[0]) << 48) |
2008 			(((uint64_t) aep->at_wwpn[1]) << 32) |
2009 			(((uint64_t) aep->at_wwpn[2]) << 16) |
2010 			(((uint64_t) aep->at_wwpn[3]) <<  0);
2011 		/*
2012 		 * However, make sure we delete ourselves if otherwise
2013 		 * we were there but at a different loop id.
2014 		 */
2015 		if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2016 			isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2017 		}
2018 		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2019 	}
2020 	atiop->cdb_len = ATIO2_CDBLEN;
2021 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2022 	atiop->ccb_h.status = CAM_CDB_RECVD;
2023 	atiop->tag_id = atp->tag;
2024 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2025 	case ATIO2_TC_ATTR_SIMPLEQ:
2026 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2027 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2028 		break;
2029 	case ATIO2_TC_ATTR_HEADOFQ:
2030 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2031 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2032 		break;
2033 	case ATIO2_TC_ATTR_ORDERED:
2034 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2035 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2036 		break;
2037 	case ATIO2_TC_ATTR_ACAQ:		/* ?? */
2038 	case ATIO2_TC_ATTR_UNTAGGED:
2039 	default:
2040 		atiop->tag_action = 0;
2041 		break;
2042 	}
2043 
2044 	atp->orig_datalen = aep->at_datalen;
2045 	atp->bytes_xfered = 0;
2046 	atp->last_xframt = 0;
2047 	atp->lun = lun;
2048 	atp->nphdl = atiop->init_id;
2049 	atp->sid = PORT_ANY;
2050 	atp->oxid = aep->at_oxid;
2051 	atp->cdb0 = aep->at_cdb[0];
2052 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2053 	atp->state = ATPD_STATE_CAM;
2054 	xpt_done((union ccb *)atiop);
2055 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2056 	rls_lun_statep(isp, tptr);
2057 	return;
2058 noresrc:
2059 	if (atp) {
2060 		isp_put_atpd(isp, tptr, atp);
2061 	}
2062 	ntp = isp_get_ntpd(isp, tptr);
2063 	if (ntp == NULL) {
2064 		rls_lun_statep(isp, tptr);
2065 		isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2066 		return;
2067 	}
2068 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2069 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2070 	tptr->restart_queue = ntp;
2071 	rls_lun_statep(isp, tptr);
2072 }
2073 
2074 static void
2075 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2076 {
2077 	int cdbxlen;
2078 	uint16_t lun, chan, nphdl = NIL_HANDLE;
2079 	uint32_t did, sid;
2080 	uint64_t wwn = INI_NONE;
2081 	fcportdb_t *lp;
2082 	tstate_t *tptr;
2083 	struct ccb_accept_tio *atiop;
2084 	atio_private_data_t *atp = NULL;
2085 	inot_private_data_t *ntp;
2086 
2087 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2088 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2089 	lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2090 
2091 	/*
2092 	 * Find the N-port handle, and Virtual Port Index for this command.
2093 	 *
2094 	 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2095 	 * We also, as a matter of course, need to know the WWN of the initiator too.
2096 	 */
2097 	if (ISP_CAP_MULTI_ID(isp)) {
2098 		/*
2099 		 * Find the right channel based upon D_ID
2100 		 */
2101 		isp_find_chan_by_did(isp, did, &chan);
2102 
2103 		if (chan == ISP_NOCHAN) {
2104 			NANOTIME_T now;
2105 
2106 			/*
2107 			 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2108 			 * It's a bit tricky here as we need to stash this command *somewhere*.
2109 			 */
2110 			GET_NANOTIME(&now);
2111 			if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2112 				isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2113 				isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2114 				return;
2115 			}
2116 			tptr = get_lun_statep(isp, 0, 0);
2117 			if (tptr == NULL) {
2118 				tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2119 				if (tptr == NULL) {
2120 					isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2121 					isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2122 					return;
2123 				}
2124 			}
2125 			isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2126 			goto noresrc;
2127 		}
2128 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2129 	} else {
2130 		chan = 0;
2131 	}
2132 
2133 	/*
2134 	 * Find the PDB entry for this initiator
2135 	 */
2136 	if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2137 		/*
2138 		 * If we're not in the port database terminate the exchange.
2139 		 */
2140 		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",
2141 		    __func__, aep->at_rxid, did, chan, sid);
2142 		isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2143 		return;
2144 	}
2145 	nphdl = lp->handle;
2146 	wwn = lp->port_wwn;
2147 
2148 	/*
2149 	 * Get the tstate pointer
2150 	 */
2151 	tptr = get_lun_statep(isp, chan, lun);
2152 	if (tptr == NULL) {
2153 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2154 		if (tptr == NULL) {
2155 			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2156 			isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2157 			return;
2158 		}
2159 	}
2160 
2161 	/*
2162 	 * Start any commands pending resources first.
2163 	 */
2164 	if (tptr->restart_queue) {
2165 		inot_private_data_t *restart_queue = tptr->restart_queue;
2166 		tptr->restart_queue = NULL;
2167 		while (restart_queue) {
2168 			ntp = restart_queue;
2169 			restart_queue = ntp->rd.nt.nt_hba;
2170 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2171 			isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2172 			isp_put_ntpd(isp, tptr, ntp);
2173 			/*
2174 			 * If a recursion caused the restart queue to start to fill again,
2175 			 * stop and splice the new list on top of the old list and restore
2176 			 * it and go to noresrc.
2177 			 */
2178 			if (tptr->restart_queue) {
2179 				if (restart_queue) {
2180 					ntp = tptr->restart_queue;
2181 					tptr->restart_queue = restart_queue;
2182 					while (restart_queue->rd.nt.nt_hba) {
2183 						restart_queue = restart_queue->rd.nt.nt_hba;
2184 					}
2185 					restart_queue->rd.nt.nt_hba = ntp;
2186 				}
2187 				goto noresrc;
2188 			}
2189 		}
2190 	}
2191 
2192 	/*
2193 	 * If the f/w is out of resources, just send a BUSY status back.
2194 	 */
2195 	if (aep->at_rxid == AT7_NORESRC_RXID) {
2196 		rls_lun_statep(isp, tptr);
2197 		isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2198 		return;
2199 	}
2200 
2201 	/*
2202 	 * If we're out of resources, just send a BUSY status back.
2203 	 */
2204 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2205 	if (atiop == NULL) {
2206 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2207 		goto noresrc;
2208 	}
2209 
2210 	atp = isp_get_atpd(isp, tptr, 0);
2211 	if (atp == NULL) {
2212 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2213 		goto noresrc;
2214 	}
2215 	if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2216 		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x)\n",
2217 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2218 		/*
2219 		 * It's not a "no resource" condition- but we can treat it like one
2220 		 */
2221 		goto noresrc;
2222 	}
2223 
2224 	atp->tag = aep->at_rxid;
2225 	atp->state = ATPD_STATE_ATIO;
2226 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2227 	tptr->atio_count--;
2228 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2229 	atiop->init_id = nphdl;
2230 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2231 	atiop->ccb_h.target_lun = lun;
2232 	atiop->sense_len = 0;
2233 	cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2234 	if (cdbxlen) {
2235 		isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2236 	}
2237 	cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2238 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2239 	atiop->cdb_len = cdbxlen;
2240 	atiop->ccb_h.status = CAM_CDB_RECVD;
2241 	atiop->tag_id = atp->tag;
2242 	switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2243 	case FCP_CMND_TASK_ATTR_SIMPLE:
2244 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2245 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
2246 		break;
2247 	case FCP_CMND_TASK_ATTR_HEAD:
2248 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2249 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2250 		break;
2251 	case FCP_CMND_TASK_ATTR_ORDERED:
2252 		atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2253 		atiop->tag_action = MSG_ORDERED_Q_TAG;
2254 		break;
2255 	default:
2256 		/* FALLTHROUGH */
2257 	case FCP_CMND_TASK_ATTR_ACA:
2258 	case FCP_CMND_TASK_ATTR_UNTAGGED:
2259 		atiop->tag_action = 0;
2260 		break;
2261 	}
2262 	atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2263 	atp->bytes_xfered = 0;
2264 	atp->last_xframt = 0;
2265 	atp->lun = lun;
2266 	atp->nphdl = nphdl;
2267 	atp->portid = sid;
2268 	atp->oxid = aep->at_hdr.ox_id;
2269 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2270 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2271 	atp->state = ATPD_STATE_CAM;
2272 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2273 	xpt_done((union ccb *)atiop);
2274 	rls_lun_statep(isp, tptr);
2275 	return;
2276 noresrc:
2277 	if (atp) {
2278 		isp_put_atpd(isp, tptr, atp);
2279 	}
2280 	ntp = isp_get_ntpd(isp, tptr);
2281 	if (ntp == NULL) {
2282 		rls_lun_statep(isp, tptr);
2283 		isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2284 		return;
2285 	}
2286 	memcpy(ntp->rd.data, aep, QENTRY_LEN);
2287 	ntp->rd.nt.nt_hba = tptr->restart_queue;
2288 	tptr->restart_queue = ntp;
2289 	rls_lun_statep(isp, tptr);
2290 }
2291 
2292 static void
2293 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2294 {
2295 	union ccb *ccb;
2296 	int sentstatus, ok, notify_cam, resid = 0;
2297 	tstate_t *tptr = NULL;
2298 	atio_private_data_t *atp = NULL;
2299 	int bus;
2300 	uint32_t tval, handle;
2301 
2302 	/*
2303 	 * CTIO, CTIO2 and CTIO7 are close enough....
2304 	 */
2305 
2306 	if (IS_SCSI(isp)) {
2307 		handle = ((ct_entry_t *)arg)->ct_syshandle;
2308 	} else {
2309 		handle = ((ct2_entry_t *)arg)->ct_syshandle;
2310 	}
2311 	ccb = isp_find_xs_tgt(isp, handle);
2312 	if (ccb == NULL) {
2313 		isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2314 		return;
2315 	}
2316 	isp_destroy_tgt_handle(isp, handle);
2317 	bus = XS_CHANNEL(ccb);
2318 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2319 	if (tptr == NULL) {
2320 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2321 	}
2322 	KASSERT((tptr != NULL), ("cannot get state pointer"));
2323 	if (isp->isp_nactive) {
2324 		isp->isp_nactive++;
2325 	}
2326 	if (IS_24XX(isp)) {
2327 		ct7_entry_t *ct = arg;
2328 
2329 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2330 		if (atp == NULL) {
2331 			rls_lun_statep(isp, tptr);
2332 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2333 			return;
2334 		}
2335 
2336 		sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2337 		ok = (ct->ct_nphdl == CT7_OK);
2338 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2339 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2340 		}
2341 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2342 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2343 			resid = ct->ct_resid;
2344 			atp->bytes_xfered += (atp->last_xframt - resid);
2345 			atp->last_xframt = 0;
2346 		}
2347 		if (ct->ct_nphdl == CT_HBA_RESET) {
2348 			ok = 0;
2349 			notify_cam = 1;
2350 			sentstatus = 1;
2351 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2352 		} else if (!ok) {
2353 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2354 		}
2355 		tval = atp->tag;
2356 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2357 		    ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2358 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2359 	} else if (IS_FC(isp)) {
2360 		ct2_entry_t *ct = arg;
2361 
2362 		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2363 		if (atp == NULL) {
2364 			rls_lun_statep(isp, tptr);
2365 			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2366 			return;
2367 		}
2368 		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2369 		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2370 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2371 			ccb->ccb_h.status |= CAM_SENT_SENSE;
2372 		}
2373 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2374 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2375 			resid = ct->ct_resid;
2376 			atp->bytes_xfered += (atp->last_xframt - resid);
2377 			atp->last_xframt = 0;
2378 		}
2379 		if (ct->ct_status == CT_HBA_RESET) {
2380 			ok = 0;
2381 			notify_cam = 1;
2382 			sentstatus = 1;
2383 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2384 		} else if (!ok) {
2385 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2386 		}
2387 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2388 		    ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2389 		tval = atp->tag;
2390 		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2391 	} else {
2392 		ct_entry_t *ct = arg;
2393 		sentstatus = ct->ct_flags & CT_SENDSTATUS;
2394 		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
2395 		/*
2396 		 * We *ought* to be able to get back to the original ATIO
2397 		 * here, but for some reason this gets lost. It's just as
2398 		 * well because it's squirrelled away as part of periph
2399 		 * private data.
2400 		 *
2401 		 * We can live without it as long as we continue to use
2402 		 * the auto-replenish feature for CTIOs.
2403 		 */
2404 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
2405 		if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2406 			ok = 0;
2407 			notify_cam = 1;
2408 			sentstatus = 1;
2409 			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2410 		} else if (!ok) {
2411 			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2412 		} else if (ct->ct_status & QLTM_SVALID) {
2413 			char *sp = (char *)ct;
2414 			sp += CTIO_SENSE_OFFSET;
2415 			ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2416 			ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2417 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2418 		}
2419 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2420 			resid = ct->ct_resid;
2421 		}
2422 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2423 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2424 		tval = ct->ct_fwhandle;
2425 	}
2426 	ccb->csio.resid += resid;
2427 
2428 	/*
2429 	 * We're here either because intermediate data transfers are done
2430 	 * and/or the final status CTIO (which may have joined with a
2431 	 * Data Transfer) is done.
2432 	 *
2433 	 * In any case, for this platform, the upper layers figure out
2434 	 * what to do next, so all we do here is collect status and
2435 	 * pass information along. Any DMA handles have already been
2436 	 * freed.
2437 	 */
2438 	if (notify_cam == 0) {
2439 		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
2440 		return;
2441 	}
2442 	if (tptr) {
2443 		rls_lun_statep(isp, tptr);
2444 	}
2445 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? "  FINAL " : "MIDTERM ", tval);
2446 
2447 	if (!ok && !IS_24XX(isp)) {
2448 		isp_target_putback_atio(ccb);
2449 	} else {
2450 		isp_complete_ctio(ccb);
2451 	}
2452 }
2453 
2454 static void
2455 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2456 {
2457 	(void) isp_notify_ack(isp, inot);
2458 }
2459 
2460 static void
2461 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2462 {
2463 	int needack = 1;
2464 	switch (inp->in_status) {
2465 	case IN_PORT_LOGOUT:
2466 		/*
2467 		 * XXX: Need to delete this initiator's WWN from the database
2468 		 * XXX: Need to send this LOGOUT upstream
2469 		 */
2470 		isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2471 		break;
2472 	case IN_PORT_CHANGED:
2473 		isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2474 		break;
2475 	case IN_GLOBAL_LOGO:
2476 		isp_del_all_wwn_entries(isp, 0);
2477 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2478 		break;
2479 	case IN_ABORT_TASK:
2480 	{
2481 		tstate_t *tptr;
2482 		uint16_t lun;
2483 		uint32_t loopid;
2484 		uint64_t wwn;
2485 		atio_private_data_t *atp;
2486 		fcportdb_t *lp;
2487 		struct ccb_immediate_notify *inot = NULL;
2488 
2489 		if (ISP_CAP_SCCFW(isp)) {
2490 			lun = inp->in_scclun;
2491 		} else {
2492 			lun = inp->in_lun;
2493 		}
2494 		if (ISP_CAP_2KLOGIN(isp)) {
2495 			loopid = ((in_fcentry_e_t *)inot)->in_iid;
2496 		} else {
2497 			loopid = inp->in_iid;
2498 		}
2499 		if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2500 			wwn = lp->port_wwn;
2501 		} else {
2502 			wwn = INI_ANY;
2503 		}
2504 		tptr = get_lun_statep(isp, 0, lun);
2505 		if (tptr == NULL) {
2506 			tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2507 			if (tptr == NULL) {
2508 				isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2509 				return;
2510 			}
2511 		}
2512 		atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2513 
2514 		if (atp) {
2515 			inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2516 			isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2517 			if (inot) {
2518 				tptr->inot_count--;
2519 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2520 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2521 			} else {
2522 				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2523 			}
2524 		} else {
2525 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2526 		}
2527 		if (inot) {
2528 			isp_notify_t tmp, *nt = &tmp;
2529 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
2530     			nt->nt_hba = isp;
2531 			nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2532 			nt->nt_wwn = wwn;
2533 			nt->nt_nphdl = loopid;
2534 			nt->nt_sid = PORT_ANY;
2535 			nt->nt_did = PORT_ANY;
2536     			nt->nt_lun = lun;
2537             		nt->nt_need_ack = 1;
2538     			nt->nt_channel = 0;
2539     			nt->nt_ncode = NT_ABORT_TASK;
2540     			nt->nt_lreserved = inot;
2541 			isp_handle_platform_target_tmf(isp, nt);
2542 			needack = 0;
2543 		}
2544 		rls_lun_statep(isp, tptr);
2545 		break;
2546 	}
2547 	default:
2548 		break;
2549 	}
2550 	if (needack) {
2551 		(void) isp_notify_ack(isp, inp);
2552 	}
2553 }
2554 
2555 static void
2556 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2557 {
2558 	uint16_t nphdl;
2559 	uint32_t portid;
2560 	fcportdb_t *lp;
2561 	uint8_t *ptr = NULL;
2562 	uint64_t wwn;
2563 
2564 	nphdl = inot->in_nphdl;
2565 	if (nphdl != NIL_HANDLE) {
2566 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2567 	} else {
2568 		portid = PORT_ANY;
2569 	}
2570 
2571 	switch (inot->in_status) {
2572 	case IN24XX_ELS_RCVD:
2573 	{
2574 		char buf[16], *msg;
2575 		int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2576 
2577 		/*
2578 		 * Note that we're just getting notification that an ELS was received
2579 		 * (possibly with some associcated information sent upstream). This is
2580 		 * *not* the same as being given the ELS frame to accept or reject.
2581 		 */
2582 		switch (inot->in_status_subcode) {
2583 		case LOGO:
2584 			msg = "LOGO";
2585 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2586 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2587 				wwn =	(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF])   << 56) |
2588 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2589 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2590 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2591 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2592 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2593 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) <<  8) |
2594 					(((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2595 			} else {
2596 				wwn = INI_ANY;
2597 			}
2598 			isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2599 			break;
2600 		case PRLO:
2601 			msg = "PRLO";
2602 			break;
2603 		case PLOGI:
2604 			msg = "PLOGI";
2605 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2606 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2607 				wwn =	(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
2608 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2609 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2610 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2611 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2612 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2613 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) <<  8) |
2614 					(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2615 			} else {
2616 				wwn = INI_NONE;
2617 			}
2618 			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2619 			break;
2620 		case PRLI:
2621 			msg = "PRLI";
2622 			break;
2623 		case PDISC:
2624 			msg = "PDISC";
2625 			break;
2626 		case ADISC:
2627 			msg = "ADISC";
2628 			break;
2629 		default:
2630 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2631 			msg = buf;
2632 			break;
2633 		}
2634 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2635 			isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
2636 			break;
2637 		}
2638 		isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
2639 		    inot->in_rxid, inot->in_oxid);
2640 		(void) isp_notify_ack(isp, inot);
2641 		break;
2642 	}
2643 
2644 	case IN24XX_PORT_LOGOUT:
2645 		ptr = "PORT LOGOUT";
2646 		if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2647 			isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2648 		}
2649 		/* FALLTHROUGH */
2650 	case IN24XX_PORT_CHANGED:
2651 		if (ptr == NULL) {
2652 			ptr = "PORT CHANGED";
2653 		}
2654 		/* FALLTHROUGH */
2655 	case IN24XX_LIP_RESET:
2656 		if (ptr == NULL) {
2657 			ptr = "LIP RESET";
2658 		}
2659 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr, inot->in_status_subcode, nphdl);
2660 
2661 		/*
2662 		 * All subcodes here are irrelevant. What is relevant
2663 		 * is that we need to terminate all active commands from
2664 		 * this initiator (known by N-port handle).
2665 		 */
2666 		/* XXX IMPLEMENT XXX */
2667 		(void) isp_notify_ack(isp, inot);
2668 		break;
2669 
2670 	case IN24XX_LINK_RESET:
2671 	case IN24XX_LINK_FAILED:
2672 	case IN24XX_SRR_RCVD:
2673 	default:
2674 		(void) isp_notify_ack(isp, inot);
2675 		break;
2676 	}
2677 }
2678 
2679 static int
2680 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2681 {
2682 
2683 	if (isp->isp_state != ISP_RUNSTATE) {
2684 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2685 		return (0);
2686 	}
2687 
2688 	/*
2689 	 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2690 	 */
2691 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2692 		ct7_entry_t local, *cto = &local;
2693 		at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2694 		fcportdb_t *lp;
2695 		uint32_t sid;
2696 		uint16_t nphdl;
2697 
2698 		sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2699 		if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2700 			nphdl = lp->handle;
2701 		} else {
2702 			nphdl = NIL_HANDLE;
2703 		}
2704 		ISP_MEMZERO(&local, sizeof (local));
2705 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2706 		cto->ct_header.rqs_entry_count = 1;
2707 		cto->ct_nphdl = nphdl;
2708 		cto->ct_rxid = aep->at_rxid;
2709 		cto->ct_vpidx = mp->nt_channel;
2710 		cto->ct_iid_lo = sid;
2711 		cto->ct_iid_hi = sid >> 16;
2712 		cto->ct_oxid = aep->at_hdr.ox_id;
2713 		cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2714 		cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2715 		return (isp_target_put_entry(isp, &local));
2716 	}
2717 
2718 	/*
2719 	 * This case is for a responding to an ABTS frame
2720 	 */
2721 	if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2722 
2723 		/*
2724 		 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2725 		 */
2726 		if (mp->nt_need_ack) {
2727 			uint8_t storage[QENTRY_LEN];
2728 			ct7_entry_t *cto = (ct7_entry_t *) storage;
2729 			abts_t *abts = (abts_t *)mp->nt_lreserved;
2730 
2731 			ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2732 			isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2733 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2734 			cto->ct_header.rqs_entry_count = 1;
2735 			cto->ct_nphdl = mp->nt_nphdl;
2736 			cto->ct_rxid = abts->abts_rxid_task;
2737 			cto->ct_iid_lo = mp->nt_sid;
2738 			cto->ct_iid_hi = mp->nt_sid >> 16;
2739 			cto->ct_oxid = abts->abts_ox_id;
2740 			cto->ct_vpidx = mp->nt_channel;
2741 			cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2742 			if (isp_target_put_entry(isp, cto)) {
2743 				return (ENOMEM);
2744 			}
2745 			mp->nt_need_ack = 0;
2746 		}
2747 		if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2748 			return (ENOMEM);
2749 		} else {
2750 			return (0);
2751 		}
2752 	}
2753 
2754 	/*
2755 	 * Handle logout cases here
2756 	 */
2757 	if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2758 		isp_del_all_wwn_entries(isp, mp->nt_channel);
2759 	}
2760 
2761 	if (mp->nt_ncode == NT_LOGOUT) {
2762 		if (!IS_2100(isp) && IS_FC(isp)) {
2763 			isp_del_wwn_entries(isp, mp);
2764 		}
2765 	}
2766 
2767 	/*
2768 	 * General purpose acknowledgement
2769 	 */
2770 	if (mp->nt_need_ack) {
2771 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2772 		return (isp_notify_ack(isp, mp->nt_lreserved));
2773 	}
2774 	return (0);
2775 }
2776 
2777 /*
2778  * Handle task managment functions.
2779  *
2780  * We show up here with a notify structure filled out.
2781  *
2782  * The nt_lreserved tag points to the original queue entry
2783  */
2784 static void
2785 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2786 {
2787 	tstate_t *tptr;
2788 	fcportdb_t *lp;
2789 	struct ccb_immediate_notify *inot;
2790 	inot_private_data_t *ntp = NULL;
2791 	lun_id_t lun;
2792 
2793 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2794 	    notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2795 	/*
2796 	 * NB: This assignment is necessary because of tricky type conversion.
2797 	 * XXX: This is tricky and I need to check this. If the lun isn't known
2798 	 * XXX: for the task management function, it does not of necessity follow
2799 	 * XXX: that it should go up stream to the wildcard listener.
2800 	 */
2801 	if (notify->nt_lun == LUN_ANY) {
2802 		lun = CAM_LUN_WILDCARD;
2803 	} else {
2804 		lun = notify->nt_lun;
2805 	}
2806 	tptr = get_lun_statep(isp, notify->nt_channel, lun);
2807 	if (tptr == NULL) {
2808 		tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2809 		if (tptr == NULL) {
2810 			isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2811 			goto bad;
2812 		}
2813 	}
2814 	inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2815 	if (inot == NULL) {
2816 		isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2817 		goto bad;
2818 	}
2819 
2820 	if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2821 		inot->initiator_id = CAM_TARGET_WILDCARD;
2822 	} else {
2823 		inot->initiator_id = lp->handle;
2824 	}
2825 	inot->seq_id = notify->nt_tagval;
2826 	inot->tag_id = notify->nt_tagval >> 32;
2827 
2828 	switch (notify->nt_ncode) {
2829 	case NT_ABORT_TASK:
2830 		isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2831 		inot->arg = MSG_ABORT_TASK;
2832 		break;
2833 	case NT_ABORT_TASK_SET:
2834 		isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2835 		inot->arg = MSG_ABORT_TASK_SET;
2836 		break;
2837 	case NT_CLEAR_ACA:
2838 		inot->arg = MSG_CLEAR_ACA;
2839 		break;
2840 	case NT_CLEAR_TASK_SET:
2841 		inot->arg = MSG_CLEAR_TASK_SET;
2842 		break;
2843 	case NT_LUN_RESET:
2844 		inot->arg = MSG_LOGICAL_UNIT_RESET;
2845 		break;
2846 	case NT_TARGET_RESET:
2847 		inot->arg = MSG_TARGET_RESET;
2848 		break;
2849 	default:
2850 		isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
2851 		goto bad;
2852 	}
2853 
2854 	ntp = isp_get_ntpd(isp, tptr);
2855 	if (ntp == NULL) {
2856 		isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2857 		goto bad;
2858 	}
2859 	ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2860 	if (notify->nt_lreserved) {
2861 		ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2862 		ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2863 	}
2864 	ntp->rd.seq_id = notify->nt_tagval;
2865 	ntp->rd.tag_id = notify->nt_tagval >> 32;
2866 
2867 	tptr->inot_count--;
2868 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2869 	rls_lun_statep(isp, tptr);
2870 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2871 	inot->ccb_h.status = CAM_MESSAGE_RECV;
2872 	xpt_done((union ccb *)inot);
2873 	return;
2874 bad:
2875 	if (tptr) {
2876 		rls_lun_statep(isp, tptr);
2877 	}
2878 	if (notify->nt_need_ack && notify->nt_lreserved) {
2879 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2880 			(void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
2881 		} else {
2882 			(void) isp_notify_ack(isp, notify->nt_lreserved);
2883 		}
2884 	}
2885 }
2886 
2887 /*
2888  * Find the associated private data and makr it as dead so
2889  * we don't try to work on it any further.
2890  */
2891 static void
2892 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
2893 {
2894 	tstate_t *tptr;
2895 	atio_private_data_t *atp;
2896 
2897 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
2898 	if (tptr == NULL) {
2899 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
2900 		if (tptr == NULL) {
2901 			ccb->ccb_h.status = CAM_REQ_INVALID;
2902 			return;
2903 		}
2904 	}
2905 
2906 	atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
2907 	if (atp == NULL) {
2908 		ccb->ccb_h.status = CAM_REQ_INVALID;
2909 		return;
2910 	}
2911 	atp->dead = 1;
2912 	ccb->ccb_h.status = CAM_REQ_CMP;
2913 }
2914 
2915 static void
2916 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
2917 {
2918 	atio_private_data_t *atp;
2919 	inot_private_data_t *restart_queue = tptr->restart_queue;
2920 
2921 	/*
2922 	 * First, clean any commands pending restart
2923 	 */
2924 	tptr->restart_queue = NULL;
2925 	while (restart_queue) {
2926 		uint32_t this_tag_id;
2927 		inot_private_data_t *ntp = restart_queue;
2928 
2929 		restart_queue = ntp->rd.nt.nt_hba;
2930 
2931 		if (IS_24XX(isp)) {
2932 			this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
2933 		} else {
2934 			this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
2935 		}
2936 		if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2937 			isp_put_ntpd(isp, tptr, ntp);
2938 		} else {
2939 			ntp->rd.nt.nt_hba = tptr->restart_queue;
2940 			tptr->restart_queue = ntp;
2941 		}
2942 	}
2943 
2944 	/*
2945 	 * Now mark other ones dead as well.
2946 	 */
2947 	for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
2948 		if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
2949 			atp->dead = 1;
2950 		}
2951 	}
2952 }
2953 
2954 
2955 #ifdef	ISP_INTERNAL_TARGET
2956 // #define	ISP_FORCE_TIMEOUT		1
2957 // #define	ISP_TEST_WWNS			1
2958 // #define	ISP_TEST_SEPARATE_STATUS	1
2959 
2960 #define	ccb_data_offset		ppriv_field0
2961 #define	ccb_atio		ppriv_ptr1
2962 #define	ccb_inot		ppriv_ptr1
2963 
2964 #define	MAX_ISP_TARG_TRANSFER	(2 << 20)
2965 #define	NISP_TARG_CMDS		1024
2966 #define	NISP_TARG_NOTIFIES	1024
2967 #define	DISK_SHIFT		9
2968 #define	JUNK_SIZE		256
2969 
2970 #ifndef	VERIFY_10
2971 #define	VERIFY_10	0x2f
2972 #endif
2973 
2974 TAILQ_HEAD(ccb_queue, ccb_hdr);
2975 extern u_int vm_kmem_size;
2976 static int ca;
2977 static uint32_t disk_size;
2978 static uint8_t *disk_data = NULL;
2979 static uint8_t *junk_data;
2980 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
2981 struct isptarg_softc {
2982 	/* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
2983 	struct ccb_queue	work_queue;
2984 	struct ccb_queue	rework_queue;
2985 	struct ccb_queue	running_queue;
2986 	struct ccb_queue	inot_queue;
2987 	struct cam_periph       *periph;
2988 	struct cam_path	 	*path;
2989 	ispsoftc_t		*isp;
2990 };
2991 static periph_ctor_t	isptargctor;
2992 static periph_dtor_t	isptargdtor;
2993 static periph_start_t	isptargstart;
2994 static periph_init_t	isptarginit;
2995 static void		isptarg_done(struct cam_periph *, union ccb *);
2996 static void		isptargasync(void *, u_int32_t, struct cam_path *, void *);
2997 
2998 
2999 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3000 
3001 static struct periph_driver isptargdriver =
3002 {
3003 	isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3004 };
3005 
3006 static void
3007 isptarginit(void)
3008 {
3009 }
3010 
3011 static void
3012 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3013 {
3014 	struct ccb_notify_acknowledge *ack = &iccb->cna2;
3015 
3016 	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3017 	    inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3018 	ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3019 	ack->ccb_h.flags = 0;
3020 	ack->ccb_h.retry_count = 0;
3021 	ack->ccb_h.cbfcnp = isptarg_done;
3022 	ack->ccb_h.timeout = 0;
3023 	ack->ccb_h.ccb_inot = inot;
3024 	ack->tag_id = inot->tag_id;
3025 	ack->seq_id = inot->seq_id;
3026 	ack->initiator_id = inot->initiator_id;
3027 	xpt_action(iccb);
3028 }
3029 
3030 static void
3031 isptargstart(struct cam_periph *periph, union ccb *iccb)
3032 {
3033 	const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = { 0x7f };
3034 	const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3035 		0, 0x0, 0x2, 0x2, 32, 0, 0, 0x32,
3036 		'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3037 		'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3038 		'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3039 		'0', '0', '0', '1'
3040 	};
3041 	int i, more = 0, last;
3042 	struct isptarg_softc *softc = periph->softc;
3043 	struct ccb_scsiio *csio;
3044 	lun_id_t return_lun;
3045 	struct ccb_accept_tio *atio;
3046 	uint8_t *cdb, *ptr, status;
3047 	uint8_t *data_ptr;
3048 	uint32_t data_len, flags;
3049 	struct ccb_hdr *ccbh;
3050 
3051 	mtx_assert(periph->sim->mtx, MA_OWNED);
3052 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3053 	    TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3054 	/*
3055 	 * Check for immediate notifies first
3056 	 */
3057 	ccbh = TAILQ_FIRST(&softc->inot_queue);
3058 	if (ccbh) {
3059 		TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3060 		if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3061 			xpt_schedule(periph, 1);
3062 		}
3063 		isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3064 		return;
3065 	}
3066 
3067 	/*
3068 	 * Check the rework (continuation) work queue first.
3069 	 */
3070 	ccbh = TAILQ_FIRST(&softc->rework_queue);
3071 	if (ccbh) {
3072 		atio = (struct ccb_accept_tio *)ccbh;
3073 		TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3074 		more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3075 	} else {
3076 		ccbh = TAILQ_FIRST(&softc->work_queue);
3077 		if (ccbh == NULL) {
3078 			ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3079 			xpt_release_ccb(iccb);
3080 			return;
3081 		}
3082 		atio = (struct ccb_accept_tio *)ccbh;
3083 		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3084 		more = TAILQ_FIRST(&softc->work_queue) != NULL;
3085 		atio->ccb_h.ccb_data_offset = 0;
3086 	}
3087 
3088 	if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3089 		panic("BAD ATIO");
3090 	}
3091 
3092 	data_ptr = NULL;
3093 	data_len = 0;
3094 	csio = &iccb->csio;
3095 	status = SCSI_STATUS_OK;
3096 	flags = CAM_SEND_STATUS;
3097 	memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3098 	cdb = atio->cdb_io.cdb_bytes;
3099 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
3100 	    cdb[0], atio->ccb_h.ccb_data_offset);
3101 
3102 	return_lun = XS_LUN(atio);
3103 	if (return_lun != 0) {
3104 		xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3105 		if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3106 			status = SCSI_STATUS_CHECK_COND;
3107 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3108 			atio->sense_data.add_sense_code = 0x25;
3109 			atio->sense_data.add_sense_code_qual = 0x0;
3110 			atio->sense_len = sizeof (atio->sense_data);
3111 		}
3112 		return_lun = CAM_LUN_WILDCARD;
3113 	}
3114 
3115 	switch (cdb[0]) {
3116 	case REQUEST_SENSE:
3117 		flags |= CAM_DIR_IN;
3118 		data_len = sizeof (atio->sense_data);
3119 		junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3120 		memset(junk_data+1, 0, data_len-1);
3121 		if (data_len > cdb[4]) {
3122 			data_len = cdb[4];
3123 		}
3124 		if (data_len) {
3125 			data_ptr = junk_data;
3126 		}
3127 		break;
3128 	case READ_6:
3129 	case READ_10:
3130 	case READ_12:
3131 	case READ_16:
3132 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3133 			status = SCSI_STATUS_CHECK_COND;
3134 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3135 			atio->sense_data.add_sense_code = 0x5;
3136 			atio->sense_data.add_sense_code_qual = 0x24;
3137 			atio->sense_len = sizeof (atio->sense_data);
3138 		} else {
3139 #ifdef	ISP_FORCE_TIMEOUT
3140 			{
3141 				static int foo;
3142 				if (foo++ == 500) {
3143 					if (more) {
3144 						xpt_schedule(periph, 1);
3145 					}
3146 					foo = 0;
3147 					return;
3148 				}
3149 			}
3150 #endif
3151 #ifdef	ISP_TEST_SEPARATE_STATUS
3152 			if (last && data_len) {
3153 				last = 0;
3154 			}
3155 #endif
3156 			if (last == 0) {
3157 				flags &= ~CAM_SEND_STATUS;
3158 			}
3159 			if (data_len) {
3160 				atio->ccb_h.ccb_data_offset += data_len;
3161 				flags |= CAM_DIR_IN;
3162 			} else {
3163 				flags |= CAM_DIR_NONE;
3164 			}
3165 		}
3166 		break;
3167 	case WRITE_6:
3168 	case WRITE_10:
3169 	case WRITE_12:
3170 	case WRITE_16:
3171 		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3172 			status = SCSI_STATUS_CHECK_COND;
3173 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3174 			atio->sense_data.add_sense_code = 0x5;
3175 			atio->sense_data.add_sense_code_qual = 0x24;
3176 			atio->sense_len = sizeof (atio->sense_data);
3177 		} else {
3178 #ifdef	ISP_FORCE_TIMEOUT
3179 			{
3180 				static int foo;
3181 				if (foo++ == 500) {
3182 					if (more) {
3183 						xpt_schedule(periph, 1);
3184 					}
3185 					foo = 0;
3186 					return;
3187 				}
3188 			}
3189 #endif
3190 #ifdef	ISP_TEST_SEPARATE_STATUS
3191 			if (last && data_len) {
3192 				last = 0;
3193 			}
3194 #endif
3195 			if (last == 0) {
3196 				flags &= ~CAM_SEND_STATUS;
3197 			}
3198 			if (data_len) {
3199 				atio->ccb_h.ccb_data_offset += data_len;
3200 				flags |= CAM_DIR_OUT;
3201 			} else {
3202 				flags |= CAM_DIR_NONE;
3203 			}
3204 		}
3205 		break;
3206 	case INQUIRY:
3207 		flags |= CAM_DIR_IN;
3208 		if (cdb[1] || cdb[2] || cdb[3]) {
3209 			status = SCSI_STATUS_CHECK_COND;
3210 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3211 			atio->sense_data.add_sense_code = 0x5;
3212 			atio->sense_data.add_sense_code_qual = 0x20;
3213 			atio->sense_len = sizeof (atio->sense_data);
3214 			break;
3215 		}
3216 		data_len = sizeof (iqd);
3217 		if (data_len > cdb[4]) {
3218 			data_len = cdb[4];
3219 		}
3220 		if (data_len) {
3221 			if (XS_LUN(iccb) != 0) {
3222 				memcpy(junk_data, niliqd, sizeof (iqd));
3223 			} else {
3224 				memcpy(junk_data, iqd, sizeof (iqd));
3225 			}
3226 			data_ptr = junk_data;
3227 		}
3228 		break;
3229 	case TEST_UNIT_READY:
3230 		flags |= CAM_DIR_NONE;
3231 		if (ca) {
3232 			ca = 0;
3233 			status = SCSI_STATUS_CHECK_COND;
3234 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3235 			atio->sense_data.add_sense_code = 0x28;
3236 			atio->sense_data.add_sense_code_qual = 0x0;
3237 			atio->sense_len = sizeof (atio->sense_data);
3238 		}
3239 		break;
3240 	case SYNCHRONIZE_CACHE:
3241 	case START_STOP:
3242 	case RESERVE:
3243 	case RELEASE:
3244 	case VERIFY_10:
3245 		flags |= CAM_DIR_NONE;
3246 		break;
3247 
3248 	case READ_CAPACITY:
3249 		flags |= CAM_DIR_IN;
3250 		if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3251 			status = SCSI_STATUS_CHECK_COND;
3252 			atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3253 			atio->sense_data.add_sense_code = 0x5;
3254 			atio->sense_data.add_sense_code_qual = 0x24;
3255 			atio->sense_len = sizeof (atio->sense_data);
3256 			break;
3257 		}
3258 		if (cdb[8] & 0x1) { /* PMI */
3259 			junk_data[0] = 0xff;
3260 			junk_data[1] = 0xff;
3261 			junk_data[2] = 0xff;
3262 			junk_data[3] = 0xff;
3263 		} else {
3264 			uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3265 			if (last_blk < 0xffffffffULL) {
3266 			    junk_data[0] = (last_blk >> 24) & 0xff;
3267 			    junk_data[1] = (last_blk >> 16) & 0xff;
3268 			    junk_data[2] = (last_blk >>  8) & 0xff;
3269 			    junk_data[3] = (last_blk) & 0xff;
3270 			} else {
3271 			    junk_data[0] = 0xff;
3272 			    junk_data[1] = 0xff;
3273 			    junk_data[2] = 0xff;
3274 			    junk_data[3] = 0xff;
3275 			}
3276 		}
3277 		junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3278 		junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3279 		junk_data[6] = ((1 << DISK_SHIFT) >>  8) & 0xff;
3280 		junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3281 		data_ptr = junk_data;
3282 		data_len = 8;
3283 		break;
3284 	case REPORT_LUNS:
3285 		flags |= CAM_DIR_IN;
3286 		memset(junk_data, 0, JUNK_SIZE);
3287 		junk_data[0] = (1 << 3) >> 24;
3288 		junk_data[1] = (1 << 3) >> 16;
3289 		junk_data[2] = (1 << 3) >> 8;
3290 		junk_data[3] = (1 << 3);
3291 		ptr = NULL;
3292 		for (i = 0; i < 1; i++) {
3293 			ptr = &junk_data[8 + (1 << 3)];
3294 			if (i >= 256) {
3295 				ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3296 			}
3297 			ptr[1] = i;
3298 		}
3299 		data_ptr = junk_data;
3300 		data_len = (ptr + 8) - junk_data;
3301 		break;
3302 
3303 	default:
3304 		flags |= CAM_DIR_NONE;
3305 		status = SCSI_STATUS_CHECK_COND;
3306 		atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3307 		atio->sense_data.add_sense_code = 0x5;
3308 		atio->sense_data.add_sense_code_qual = 0x20;
3309 		atio->sense_len = sizeof (atio->sense_data);
3310 		break;
3311 	}
3312 
3313 	/*
3314 	 * If we are done with the transaction, tell the
3315 	 * controller to send status and perform a CMD_CMPLT.
3316 	 * If we have associated sense data, see if we can
3317 	 * send that too.
3318 	 */
3319 	if (status == SCSI_STATUS_CHECK_COND) {
3320 		flags |= CAM_SEND_SENSE;
3321 		csio->sense_len = atio->sense_len;
3322 		csio->sense_data = atio->sense_data;
3323 		flags &= ~CAM_DIR_MASK;
3324 		data_len = 0;
3325 		data_ptr = NULL;
3326 	}
3327 	cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3328 	iccb->ccb_h.target_id = atio->ccb_h.target_id;
3329 	iccb->ccb_h.target_lun = return_lun;
3330 	iccb->ccb_h.ccb_atio = atio;
3331 	xpt_action(iccb);
3332 
3333 	if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3334 		cam_release_devq(periph->path, 0, 0, 0, 0);
3335 		atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3336 	}
3337 	if (more) {
3338 		xpt_schedule(periph, 1);
3339 	}
3340 }
3341 
3342 static cam_status
3343 isptargctor(struct cam_periph *periph, void *arg)
3344 {
3345 	struct isptarg_softc *softc;
3346 
3347 	softc = (struct isptarg_softc *)arg;
3348 	periph->softc = softc;
3349 	softc->periph = periph;
3350 	softc->path = periph->path;
3351 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3352 	return (CAM_REQ_CMP);
3353 }
3354 
3355 static void
3356 isptargdtor(struct cam_periph *periph)
3357 {
3358 	struct isptarg_softc *softc;
3359 	softc = (struct isptarg_softc *)periph->softc;
3360 	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3361 	softc->periph = NULL;
3362 	softc->path = NULL;
3363 	periph->softc = NULL;
3364 }
3365 
3366 static void
3367 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3368 {
3369 	struct isptarg_softc *softc;
3370 	ispsoftc_t *isp;
3371 	struct ccb_accept_tio *atio;
3372 	struct ccb_immediate_notify *inot;
3373 	cam_status status;
3374 
3375 	softc = (struct isptarg_softc *)periph->softc;
3376 	isp = softc->isp;
3377 	status = ccb->ccb_h.status & CAM_STATUS_MASK;
3378 
3379 	switch (ccb->ccb_h.func_code) {
3380 	case XPT_ACCEPT_TARGET_IO:
3381 		atio = (struct ccb_accept_tio *) ccb;
3382 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3383 		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3384 		xpt_schedule(periph, 1);
3385 		break;
3386 	case XPT_IMMEDIATE_NOTIFY:
3387 		inot = (struct ccb_immediate_notify *) ccb;
3388 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
3389 		TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3390 		xpt_schedule(periph, 1);
3391 		break;
3392 	case XPT_CONT_TARGET_IO:
3393 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3394 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3395 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3396 		}
3397 		atio = ccb->ccb_h.ccb_atio;
3398 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3399 			cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3400 			xpt_action((union ccb *)atio);
3401 		} else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3402 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3403 			TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3404 			xpt_schedule(periph, 1);
3405 		} else {
3406 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3407 			xpt_action((union ccb *)atio);
3408 		}
3409 		xpt_release_ccb(ccb);
3410 		break;
3411 	case XPT_NOTIFY_ACKNOWLEDGE:
3412 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3413 			cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3414 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3415 		}
3416 		inot = ccb->ccb_h.ccb_inot;
3417 		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
3418 		xpt_release_ccb(ccb);
3419 		xpt_action((union ccb *)inot);
3420 		break;
3421 	default:
3422 		xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3423 		break;
3424 	}
3425 }
3426 
3427 static void
3428 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3429 {
3430 	struct ac_contract *acp = arg;
3431 	struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3432 
3433 	if (code != AC_CONTRACT) {
3434 		return;
3435 	}
3436 	xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3437 }
3438 
3439 static void
3440 isp_target_thread(ispsoftc_t *isp, int chan)
3441 {
3442 	union ccb *ccb = NULL;
3443 	int i;
3444 	void *wchan;
3445 	cam_status status;
3446 	struct isptarg_softc *softc = NULL;
3447 	struct cam_periph *periph = NULL, *wperiph = NULL;
3448 	struct cam_path *path, *wpath;
3449 	struct cam_sim *sim;
3450 
3451 	if (disk_data == NULL) {
3452 		disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3453 		if (disk_size < (50 << 20)) {
3454 			disk_size = 50 << 20;
3455 		}
3456 		disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3457 		if (disk_data == NULL) {
3458 			isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
3459 			goto out;
3460 		}
3461 		isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3462 	}
3463 	junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3464 	if (junk_data == NULL) {
3465 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
3466 		goto out;
3467 	}
3468 
3469 
3470 	softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3471 	if (softc == NULL) {
3472 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
3473 		goto out;
3474 	}
3475 	TAILQ_INIT(&softc->work_queue);
3476 	TAILQ_INIT(&softc->rework_queue);
3477 	TAILQ_INIT(&softc->running_queue);
3478 	TAILQ_INIT(&softc->inot_queue);
3479 	softc->isp = isp;
3480 
3481 	periphdriver_register(&isptargdriver);
3482 	ISP_GET_PC(isp, chan, sim, sim);
3483 	ISP_GET_PC(isp, chan, path,  path);
3484 	status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3485 	if (status != CAM_REQ_CMP) {
3486 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3487 		return;
3488 	}
3489 	status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3490 	if (status != CAM_REQ_CMP) {
3491 		xpt_free_path(wpath);
3492 		isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3493 		return;
3494 	}
3495 
3496 	ccb = xpt_alloc_ccb();
3497 
3498 	ISP_LOCK(isp);
3499 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3500 	if (status != CAM_REQ_CMP) {
3501 		ISP_UNLOCK(isp);
3502 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3503 		goto out;
3504 	}
3505 	wperiph = cam_periph_find(wpath, "isptarg");
3506 	if (wperiph == NULL) {
3507 		ISP_UNLOCK(isp);
3508 		isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3509 		goto out;
3510 	}
3511 
3512 	status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3513 	if (status != CAM_REQ_CMP) {
3514 		ISP_UNLOCK(isp);
3515 		isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3516 		goto out;
3517 	}
3518 
3519 	periph = cam_periph_find(path, "isptarg");
3520 	if (periph == NULL) {
3521 		ISP_UNLOCK(isp);
3522 		isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3523 		goto out;
3524 	}
3525 
3526 	status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3527 	if (status != CAM_REQ_CMP) {
3528 		ISP_UNLOCK(isp);
3529 		isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3530 		goto out;
3531 	}
3532 
3533 	ISP_UNLOCK(isp);
3534 
3535 	ccb = xpt_alloc_ccb();
3536 
3537 	/*
3538 	 * Make sure role is none.
3539 	 */
3540 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3541 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3542 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3543 #ifdef	ISP_TEST_WWNS
3544 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3545 	ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3546 	ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3547 #else
3548 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3549 #endif
3550 
3551 	ISP_LOCK(isp);
3552 	xpt_action(ccb);
3553 	ISP_UNLOCK(isp);
3554 
3555 	/*
3556 	 * Now enable luns
3557 	 */
3558 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3559 	ccb->ccb_h.func_code = XPT_EN_LUN;
3560 	ccb->cel.enable = 1;
3561 	ISP_LOCK(isp);
3562 	xpt_action(ccb);
3563 	ISP_UNLOCK(isp);
3564 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
3565 		xpt_free_ccb(ccb);
3566 		xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3567 		goto out;
3568 	}
3569 
3570 	xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3571 	ccb->ccb_h.func_code = XPT_EN_LUN;
3572 	ccb->cel.enable = 1;
3573 	ISP_LOCK(isp);
3574 	xpt_action(ccb);
3575 	ISP_UNLOCK(isp);
3576 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
3577 		xpt_free_ccb(ccb);
3578 		xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3579 		goto out;
3580 	}
3581 	xpt_free_ccb(ccb);
3582 
3583 	/*
3584 	 * Add resources
3585 	 */
3586 	ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3587 	for (i = 0; i < 4; i++) {
3588 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3589 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3590 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3591 		ccb->ccb_h.cbfcnp = isptarg_done;
3592 		ISP_LOCK(isp);
3593 		xpt_action(ccb);
3594 		ISP_UNLOCK(isp);
3595 	}
3596 	for (i = 0; i < NISP_TARG_CMDS; i++) {
3597 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3598 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3599 		ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3600 		ccb->ccb_h.cbfcnp = isptarg_done;
3601 		ISP_LOCK(isp);
3602 		xpt_action(ccb);
3603 		ISP_UNLOCK(isp);
3604 	}
3605 	for (i = 0; i < 4; i++) {
3606 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3607 		xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3608 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3609 		ccb->ccb_h.cbfcnp = isptarg_done;
3610 		ISP_LOCK(isp);
3611 		xpt_action(ccb);
3612 		ISP_UNLOCK(isp);
3613 	}
3614 	for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3615 		ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3616 		xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3617 		ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3618 		ccb->ccb_h.cbfcnp = isptarg_done;
3619 		ISP_LOCK(isp);
3620 		xpt_action(ccb);
3621 		ISP_UNLOCK(isp);
3622 	}
3623 
3624 	/*
3625 	 * Now turn it all back on
3626 	 */
3627 	xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3628 	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3629 	ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3630 	ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3631 	ISP_LOCK(isp);
3632 	xpt_action(ccb);
3633 	ISP_UNLOCK(isp);
3634 
3635 	/*
3636 	 * Okay, while things are still active, sleep...
3637 	 */
3638 	ISP_LOCK(isp);
3639 	for (;;) {
3640 		ISP_GET_PC(isp, chan, proc_active, i);
3641 		if (i == 0) {
3642 			break;
3643 		}
3644 		msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3645 	}
3646 	ISP_UNLOCK(isp);
3647 
3648 out:
3649 	if (wperiph) {
3650 		cam_periph_invalidate(wperiph);
3651 	}
3652 	if (periph) {
3653 		cam_periph_invalidate(periph);
3654 	}
3655 	if (junk_data) {
3656 		free(junk_data, M_ISPTARG);
3657 	}
3658 	if (disk_data) {
3659 		free(disk_data, M_ISPTARG);
3660 	}
3661 	if (softc) {
3662 		free(softc, M_ISPTARG);
3663 	}
3664 	xpt_free_path(path);
3665 	xpt_free_path(wpath);
3666 }
3667 
3668 static void
3669 isp_target_thread_pi(void *arg)
3670 {
3671 	struct isp_spi *pi = arg;
3672 	isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3673 }
3674 
3675 static void
3676 isp_target_thread_fc(void *arg)
3677 {
3678 	struct isp_fc *fc = arg;
3679 	isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3680 }
3681 
3682 static int
3683 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3684 {
3685 	uint32_t cnt, curcnt;
3686 	uint64_t lba;
3687 
3688 	switch (cdb[0]) {
3689 	case WRITE_16:
3690 	case READ_16:
3691 		cnt =	(((uint32_t)cdb[10]) <<  24) |
3692 			(((uint32_t)cdb[11]) <<  16) |
3693 			(((uint32_t)cdb[12]) <<   8) |
3694 			((uint32_t)cdb[13]);
3695 
3696 		lba =	(((uint64_t)cdb[2]) << 56) |
3697 			(((uint64_t)cdb[3]) << 48) |
3698 			(((uint64_t)cdb[4]) << 40) |
3699 			(((uint64_t)cdb[5]) << 32) |
3700 			(((uint64_t)cdb[6]) << 24) |
3701 			(((uint64_t)cdb[7]) << 16) |
3702 			(((uint64_t)cdb[8]) <<  8) |
3703 			((uint64_t)cdb[9]);
3704 		break;
3705 	case WRITE_12:
3706 	case READ_12:
3707 		cnt =	(((uint32_t)cdb[6]) <<  16) |
3708 			(((uint32_t)cdb[7]) <<   8) |
3709 			((u_int32_t)cdb[8]);
3710 
3711 		lba =	(((uint32_t)cdb[2]) << 24) |
3712 			(((uint32_t)cdb[3]) << 16) |
3713 			(((uint32_t)cdb[4]) <<  8) |
3714 			((uint32_t)cdb[5]);
3715 		break;
3716 	case WRITE_10:
3717 	case READ_10:
3718 		cnt =	(((uint32_t)cdb[7]) <<  8) |
3719 			((u_int32_t)cdb[8]);
3720 
3721 		lba =	(((uint32_t)cdb[2]) << 24) |
3722 			(((uint32_t)cdb[3]) << 16) |
3723 			(((uint32_t)cdb[4]) <<  8) |
3724 			((uint32_t)cdb[5]);
3725 		break;
3726 	case WRITE_6:
3727 	case READ_6:
3728 		cnt = cdb[4];
3729 		if (cnt == 0) {
3730 			cnt = 256;
3731 		}
3732 		lba =	(((uint32_t)cdb[1] & 0x1f) << 16) |
3733 			(((uint32_t)cdb[2]) << 8) |
3734 			((uint32_t)cdb[3]);
3735 		break;
3736 	default:
3737 		return (-1);
3738 	}
3739 
3740 	cnt <<= DISK_SHIFT;
3741 	lba <<= DISK_SHIFT;
3742 
3743 	if (offset == cnt) {
3744 		*lp = 1;
3745 		return (0);
3746 	}
3747 
3748 	if (lba + cnt > dl) {
3749 		return (-1);
3750 	}
3751 
3752 
3753 	curcnt = MAX_ISP_TARG_TRANSFER;
3754 	if (offset + curcnt >= cnt) {
3755 		curcnt = cnt - offset;
3756 		*lp = 1;
3757 	} else {
3758 		*lp = 0;
3759 	}
3760 	*tl = curcnt;
3761 	*kp = &dp[lba + offset];
3762 	return (0);
3763 }
3764 
3765 #endif
3766 #endif
3767 
3768 static void
3769 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3770 {
3771 	struct cam_sim *sim;
3772 	ispsoftc_t *isp;
3773 
3774 	sim = (struct cam_sim *)cbarg;
3775 	isp = (ispsoftc_t *) cam_sim_softc(sim);
3776 	switch (code) {
3777 	case AC_LOST_DEVICE:
3778 		if (IS_SCSI(isp)) {
3779 			uint16_t oflags, nflags;
3780 			int bus = cam_sim_bus(sim);
3781 			sdparam *sdp = SDPARAM(isp, bus);
3782 			int tgt;
3783 
3784 			tgt = xpt_path_target_id(path);
3785 			if (tgt >= 0) {
3786 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
3787 #ifndef	ISP_TARGET_MODE
3788 				nflags &= DPARM_SAFE_DFLT;
3789 				if (isp->isp_loaded_fw) {
3790 					nflags |= DPARM_NARROW | DPARM_ASYNC;
3791 				}
3792 #else
3793 				nflags = DPARM_DEFAULT;
3794 #endif
3795 				oflags = sdp->isp_devparam[tgt].goal_flags;
3796 				sdp->isp_devparam[tgt].goal_flags = nflags;
3797 				sdp->isp_devparam[tgt].dev_update = 1;
3798 				sdp->update = 1;
3799 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3800 				sdp->isp_devparam[tgt].goal_flags = oflags;
3801 			}
3802 		}
3803 		break;
3804 	default:
3805 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3806 		break;
3807 	}
3808 }
3809 
3810 static void
3811 isp_poll(struct cam_sim *sim)
3812 {
3813 	ispsoftc_t *isp = cam_sim_softc(sim);
3814 	uint32_t isr;
3815 	uint16_t sema, mbox;
3816 
3817 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3818 		isp_intr(isp, isr, sema, mbox);
3819 	}
3820 }
3821 
3822 
3823 static void
3824 isp_watchdog(void *arg)
3825 {
3826 	struct ccb_scsiio *xs = arg;
3827 	ispsoftc_t *isp;
3828 	uint32_t handle;
3829 
3830 	isp = XS_ISP(xs);
3831 
3832 	handle = isp_find_handle(isp, xs);
3833 	if (handle) {
3834 		/*
3835 		 * Make sure the command is *really* dead before we
3836 		 * release the handle (and DMA resources) for reuse.
3837 		 */
3838 		(void) isp_control(isp, ISPCTL_ABORT_CMD, xs);
3839 
3840 		/*
3841 		 * After this point, the comamnd is really dead.
3842 		 */
3843 		if (XS_XFRLEN(xs)) {
3844 			ISP_DMAFREE(isp, xs, handle);
3845 		}
3846 		isp_destroy_handle(isp, handle);
3847 		xpt_print(xs->ccb_h.path, "watchdog timeout for handle 0x%x\n", handle);
3848 		XS_SETERR(xs, CAM_CMD_TIMEOUT);
3849 		isp_done(xs);
3850 	}
3851 }
3852 
3853 static void
3854 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3855 {
3856 	union ccb *ccb;
3857 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3858 
3859 	if (isp_autoconfig == 0) {
3860 		return;
3861 	}
3862 
3863 	/*
3864 	 * Allocate a CCB, create a wildcard path for this bus/target and schedule a rescan.
3865 	 */
3866 	ccb = xpt_alloc_ccb_nowait();
3867 	if (ccb == NULL) {
3868 		isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3869 		return;
3870 	}
3871 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3872 		isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3873 		xpt_free_ccb(ccb);
3874 		return;
3875 	}
3876 	xpt_rescan(ccb);
3877 }
3878 
3879 static void
3880 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
3881 {
3882 	struct cam_path *tp;
3883 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
3884 
3885 	if (isp_autoconfig == 0) {
3886 		return;
3887 	}
3888 	if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3889 		xpt_async(AC_LOST_DEVICE, tp, NULL);
3890 		xpt_free_path(tp);
3891 	}
3892 }
3893 
3894 /*
3895  * Gone Device Timer Function- when we have decided that a device has gone
3896  * away, we wait a specific period of time prior to telling the OS it has
3897  * gone away.
3898  *
3899  * This timer function fires once a second and then scans the port database
3900  * for devices that are marked dead but still have a virtual target assigned.
3901  * We decrement a counter for that port database entry, and when it hits zero,
3902  * we tell the OS the device has gone away.
3903  */
3904 static void
3905 isp_gdt(void *arg)
3906 {
3907 	struct isp_fc *fc = arg;
3908 	ispsoftc_t *isp = fc->isp;
3909 	int chan = fc - isp->isp_osinfo.pc.fc;
3910 	fcportdb_t *lp;
3911 	int dbidx, tgt, more_to_do = 0;
3912 
3913 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3914 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3915 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3916 
3917 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3918 			continue;
3919 		}
3920 		if (lp->dev_map_idx == 0 || lp->target_mode) {
3921 			continue;
3922 		}
3923 		if (lp->new_reserved == 0) {
3924 			continue;
3925 		}
3926 		lp->new_reserved -= 1;
3927 		if (lp->new_reserved != 0) {
3928 			more_to_do++;
3929 			continue;
3930 		}
3931 		tgt = lp->dev_map_idx - 1;
3932 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
3933 		lp->dev_map_idx = 0;
3934 		lp->state = FC_PORTDB_STATE_NIL;
3935 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
3936 		isp_make_gone(isp, chan, tgt);
3937 	}
3938 	if (more_to_do) {
3939 		fc->gdt_running = 1;
3940 		callout_reset(&fc->gdt, hz, isp_gdt, fc);
3941 	} else {
3942 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d stopping Gone Device Timer", chan);
3943 		fc->gdt_running = 0;
3944 	}
3945 }
3946 
3947 /*
3948  * Loop Down Timer Function- when loop goes down, a timer is started and
3949  * and after it expires we come here and take all probational devices that
3950  * the OS knows about and the tell the OS that they've gone away.
3951  *
3952  * We don't clear the devices out of our port database because, when loop
3953  * come back up, we have to do some actual cleanup with the chip at that
3954  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3955  */
3956 static void
3957 isp_ldt(void *arg)
3958 {
3959 	struct isp_fc *fc = arg;
3960 	ispsoftc_t *isp = fc->isp;
3961 	int chan = fc - isp->isp_osinfo.pc.fc;
3962 	fcportdb_t *lp;
3963 	int dbidx, tgt;
3964 
3965 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
3966 
3967 	/*
3968 	 * Notify to the OS all targets who we now consider have departed.
3969 	 */
3970 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3971 		lp = &FCPARAM(isp, chan)->portdb[dbidx];
3972 
3973 		if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
3974 			continue;
3975 		}
3976 		if (lp->dev_map_idx == 0 || lp->target_mode) {
3977 			continue;
3978 		}
3979 
3980 		/*
3981 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3982 		 */
3983 
3984 		/*
3985 		 * Mark that we've announced that this device is gone....
3986 		 */
3987 		lp->reserved = 1;
3988 
3989 		/*
3990 		 * but *don't* change the state of the entry. Just clear
3991 		 * any target id stuff and announce to CAM that the
3992 		 * device is gone. This way any necessary PLOGO stuff
3993 		 * will happen when loop comes back up.
3994 		 */
3995 
3996 		tgt = lp->dev_map_idx - 1;
3997 		FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
3998 		lp->dev_map_idx = 0;
3999 		lp->state = FC_PORTDB_STATE_NIL;
4000 		isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4001 		isp_make_gone(isp, chan, tgt);
4002 	}
4003 
4004 	/*
4005 	 * The loop down timer has expired. Wake up the kthread
4006 	 * to notice that fact (or make it false).
4007 	 */
4008 	fc->loop_dead = 1;
4009 	fc->loop_down_time = fc->loop_down_limit+1;
4010 	wakeup(fc);
4011 }
4012 
4013 static void
4014 isp_kthread(void *arg)
4015 {
4016 	struct isp_fc *fc = arg;
4017 	ispsoftc_t *isp = fc->isp;
4018 	int chan = fc - isp->isp_osinfo.pc.fc;
4019 	int slp = 0;
4020 	mtx_lock(&isp->isp_osinfo.lock);
4021 
4022 	for (;;) {
4023 		int wasfrozen, lb, lim;
4024 
4025 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4026 		lb = isp_fc_runstate(isp, chan, 250000);
4027 
4028 		/*
4029 		 * Our action is different based upon whether we're supporting
4030 		 * Initiator mode or not. If we are, we might freeze the simq
4031 		 * when loop is down and set all sorts of different delays to
4032 		 * check again.
4033 		 *
4034 		 * If not, we simply just wait for loop to come up.
4035 		 */
4036 		if (lb && (fc->role & ISP_ROLE_INITIATOR)) {
4037 			/*
4038 			 * Increment loop down time by the last sleep interval
4039 			 */
4040 			fc->loop_down_time += slp;
4041 
4042 			if (lb < 0) {
4043 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4044 			} else {
4045 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4046 			}
4047 
4048 			/*
4049 			 * If we've never seen loop up and we've waited longer
4050 			 * than quickboot time, or we've seen loop up but we've
4051 			 * waited longer than loop_down_limit, give up and go
4052 			 * to sleep until loop comes up.
4053 			 */
4054 			if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4055 				lim = isp_quickboot_time;
4056 			} else {
4057 				lim = fc->loop_down_limit;
4058 			}
4059 			if (fc->loop_down_time >= lim) {
4060 				isp_freeze_loopdown(isp, chan, "loop limit hit");
4061 				slp = 0;
4062 			} else if (fc->loop_down_time < 10) {
4063 				slp = 1;
4064 			} else if (fc->loop_down_time < 30) {
4065 				slp = 5;
4066 			} else if (fc->loop_down_time < 60) {
4067 				slp = 10;
4068 			} else if (fc->loop_down_time < 120) {
4069 				slp = 20;
4070 			} else {
4071 				slp = 30;
4072 			}
4073 
4074 		} else if (lb) {
4075 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4076 			fc->loop_down_time += slp;
4077 			slp = 60;
4078 		} else {
4079 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4080 			fc->loop_down_time = 0;
4081 			slp = 0;
4082 		}
4083 
4084 
4085 		/*
4086 		 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4087 		 * now so that CAM can start sending us commands.
4088 		 *
4089 		 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4090 		 * or kill the commands, as appropriate.
4091 		 */
4092 
4093 		if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4094 			wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
4095 			fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
4096 			if (wasfrozen && fc->simqfrozen == 0) {
4097 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
4098 				xpt_release_simq(fc->sim, 1);
4099 			}
4100 		}
4101 
4102 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4103 
4104 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4105 
4106 		/*
4107 		 * If slp is zero, we're waking up for the first time after
4108 		 * things have been okay. In this case, we set a deferral state
4109 		 * for all commands and delay hysteresis seconds before starting
4110 		 * the FC state evaluation. This gives the loop/fabric a chance
4111 		 * to settle.
4112 		 */
4113 		if (slp == 0 && fc->hysteresis) {
4114 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4115 			(void) msleep(&isp_fabric_hysteresis, &isp->isp_osinfo.lock, PRIBIO, "ispT", (fc->hysteresis * hz));
4116 		}
4117 	}
4118 	mtx_unlock(&isp->isp_osinfo.lock);
4119 }
4120 
4121 static void
4122 isp_action(struct cam_sim *sim, union ccb *ccb)
4123 {
4124 	int bus, tgt, ts, error, lim;
4125 	ispsoftc_t *isp;
4126 	struct ccb_trans_settings *cts;
4127 
4128 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4129 
4130 	isp = (ispsoftc_t *)cam_sim_softc(sim);
4131 	mtx_assert(&isp->isp_lock, MA_OWNED);
4132 
4133 	if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4134 		isp_init(isp);
4135 		if (isp->isp_state != ISP_INITSTATE) {
4136 			/*
4137 			 * Lie. Say it was a selection timeout.
4138 			 */
4139 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4140 			xpt_freeze_devq(ccb->ccb_h.path, 1);
4141 			xpt_done(ccb);
4142 			return;
4143 		}
4144 		isp->isp_state = ISP_RUNSTATE;
4145 	}
4146 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4147 	ISP_PCMD(ccb) = NULL;
4148 
4149 	switch (ccb->ccb_h.func_code) {
4150 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
4151 		bus = XS_CHANNEL(ccb);
4152 		/*
4153 		 * Do a couple of preliminary checks...
4154 		 */
4155 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4156 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4157 				ccb->ccb_h.status = CAM_REQ_INVALID;
4158 				xpt_done(ccb);
4159 				break;
4160 			}
4161 		}
4162 #ifdef	DIAGNOSTIC
4163 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4164 			xpt_print(ccb->ccb_h.path, "invalid target\n");
4165 			ccb->ccb_h.status = CAM_PATH_INVALID;
4166 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4167 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
4168 			ccb->ccb_h.status = CAM_PATH_INVALID;
4169 		}
4170 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4171 			xpt_done(ccb);
4172 			break;
4173 		}
4174 #endif
4175 		ccb->csio.scsi_status = SCSI_STATUS_OK;
4176 		if (isp_get_pcmd(isp, ccb)) {
4177 			isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4178 			cam_freeze_devq(ccb->ccb_h.path);
4179 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4180 			xpt_done(ccb);
4181 			break;
4182 		}
4183 		error = isp_start((XS_T *) ccb);
4184 		switch (error) {
4185 		case CMD_QUEUED:
4186 			XS_CMD_S_CLEAR(ccb);
4187 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
4188 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4189 				break;
4190 			}
4191 			ts = ccb->ccb_h.timeout;
4192 			if (ts == CAM_TIME_DEFAULT) {
4193 				ts = 60*1000;
4194 			}
4195 			ts = isp_mstohz(ts);
4196 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4197 			break;
4198 		case CMD_RQLATER:
4199 			/*
4200 			 * We get this result for FC devices if the loop state isn't ready yet
4201 			 * or if the device in question has gone zombie on us.
4202 			 *
4203 			 * If we've never seen Loop UP at all, we requeue this request and wait
4204 			 * for the initial loop up delay to expire.
4205 			 */
4206 			lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4207 			if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4208 				if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4209 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4210 				} else {
4211 					isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4212 				}
4213 				ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4214 				xpt_freeze_devq(ccb->ccb_h.path, 1);
4215 				isp_free_pcmd(isp, ccb);
4216 				xpt_done(ccb);
4217 				break;
4218 			}
4219 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4220 			cam_freeze_devq(ccb->ccb_h.path);
4221 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4222 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4223 			isp_free_pcmd(isp, ccb);
4224 			xpt_done(ccb);
4225 			break;
4226 		case CMD_EAGAIN:
4227 			isp_free_pcmd(isp, ccb);
4228 			cam_freeze_devq(ccb->ccb_h.path);
4229 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4230 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4231 			xpt_done(ccb);
4232 			break;
4233 		case CMD_COMPLETE:
4234 			isp_done((struct ccb_scsiio *) ccb);
4235 			break;
4236 		default:
4237 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4238 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4239 			isp_free_pcmd(isp, ccb);
4240 			xpt_done(ccb);
4241 		}
4242 		break;
4243 
4244 #ifdef	ISP_TARGET_MODE
4245 	case XPT_EN_LUN:		/* Enable/Disable LUN as a target */
4246 		if (ccb->cel.enable) {
4247 			isp_enable_lun(isp, ccb);
4248 		} else {
4249 			isp_disable_lun(isp, ccb);
4250 		}
4251 		break;
4252 	case XPT_IMMED_NOTIFY:
4253 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
4254 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
4255 	{
4256 		tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4257 		if (tptr == NULL) {
4258 			tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4259 		}
4260 		if (tptr == NULL) {
4261 			const char *str;
4262 			uint32_t tag;
4263 
4264 			if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4265 				str = "XPT_IMMEDIATE_NOTIFY";
4266 				tag = ccb->cin1.seq_id;
4267 			} else {
4268 				tag = ccb->atio.tag_id;
4269 				str = "XPT_ACCEPT_TARGET_IO";
4270 			}
4271 			ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4272 			dump_tstates(isp, XS_CHANNEL(ccb));
4273 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4274 			break;
4275 		}
4276 		ccb->ccb_h.sim_priv.entries[0].field = 0;
4277 		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4278 		ccb->ccb_h.flags = 0;
4279 
4280 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4281 			if (ccb->atio.tag_id) {
4282 				atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4283 				if (atp) {
4284 					isp_put_atpd(isp, tptr, atp);
4285 				}
4286 			}
4287 			tptr->atio_count++;
4288 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4289 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4290 			    ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
4291 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4292 			if (ccb->cin1.tag_id) {
4293 				inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4294 				if (ntp) {
4295 					isp_put_ntpd(isp, tptr, ntp);
4296 				}
4297 			}
4298 			tptr->inot_count++;
4299 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4300 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4301 			    ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4302 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4303 			tptr->inot_count++;
4304 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4305 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4306 			    ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4307 		}
4308 		rls_lun_statep(isp, tptr);
4309 		ccb->ccb_h.status = CAM_REQ_INPROG;
4310 		break;
4311 	}
4312 	case XPT_NOTIFY_ACK:
4313 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4314 		break;
4315 	case XPT_NOTIFY_ACKNOWLEDGE:		/* notify ack */
4316 	{
4317 		tstate_t *tptr;
4318 		inot_private_data_t *ntp;
4319 
4320 		/*
4321 		 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4322 		 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4323 		 */
4324 		/*
4325 		 * All the relevant path information is in the associated immediate notify
4326 		 */
4327 		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);
4328 		ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4329 		if (ntp == NULL) {
4330 			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__,
4331 			     ccb->cna2.tag_id, ccb->cna2.seq_id);
4332 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4333 			xpt_done(ccb);
4334 			break;
4335 		}
4336 		if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4337 			rls_lun_statep(isp, tptr);
4338 			cam_freeze_devq(ccb->ccb_h.path);
4339 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4340 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
4341 			break;
4342 		}
4343 		isp_put_ntpd(isp, tptr, ntp);
4344 		rls_lun_statep(isp, tptr);
4345 		ccb->ccb_h.status = CAM_REQ_CMP;
4346 		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);
4347 		xpt_done(ccb);
4348 		break;
4349 	}
4350 	case XPT_CONT_TARGET_IO:
4351 		isp_target_start_ctio(isp, ccb);
4352 		break;
4353 #endif
4354 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
4355 
4356 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4357 		tgt = ccb->ccb_h.target_id;
4358 		tgt |= (bus << 16);
4359 
4360 		error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4361 		if (error) {
4362 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4363 		} else {
4364 			ccb->ccb_h.status = CAM_REQ_CMP;
4365 		}
4366 		xpt_done(ccb);
4367 		break;
4368 	case XPT_ABORT:			/* Abort the specified CCB */
4369 	{
4370 		union ccb *accb = ccb->cab.abort_ccb;
4371 		switch (accb->ccb_h.func_code) {
4372 #ifdef	ISP_TARGET_MODE
4373 		case XPT_ACCEPT_TARGET_IO:
4374 			isp_target_mark_aborted(isp, accb);
4375 			break;
4376 #endif
4377 		case XPT_SCSI_IO:
4378 			error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4379 			if (error) {
4380 				ccb->ccb_h.status = CAM_UA_ABORT;
4381 			} else {
4382 				ccb->ccb_h.status = CAM_REQ_CMP;
4383 			}
4384 			break;
4385 		default:
4386 			ccb->ccb_h.status = CAM_REQ_INVALID;
4387 			break;
4388 		}
4389 		xpt_done(ccb);
4390 		break;
4391 	}
4392 #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
4393 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
4394 		cts = &ccb->cts;
4395 		if (!IS_CURRENT_SETTINGS(cts)) {
4396 			ccb->ccb_h.status = CAM_REQ_INVALID;
4397 			xpt_done(ccb);
4398 			break;
4399 		}
4400 		tgt = cts->ccb_h.target_id;
4401 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4402 		if (IS_SCSI(isp)) {
4403 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4404 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4405 			sdparam *sdp = SDPARAM(isp, bus);
4406 			uint16_t *dptr;
4407 
4408 			if (spi->valid == 0 && scsi->valid == 0) {
4409 				ccb->ccb_h.status = CAM_REQ_CMP;
4410 				xpt_done(ccb);
4411 				break;
4412 			}
4413 
4414 			/*
4415 			 * We always update (internally) from goal_flags
4416 			 * so any request to change settings just gets
4417 			 * vectored to that location.
4418 			 */
4419 			dptr = &sdp->isp_devparam[tgt].goal_flags;
4420 
4421 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4422 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4423 					*dptr |= DPARM_DISC;
4424 				else
4425 					*dptr &= ~DPARM_DISC;
4426 			}
4427 
4428 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4429 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4430 					*dptr |= DPARM_TQING;
4431 				else
4432 					*dptr &= ~DPARM_TQING;
4433 			}
4434 
4435 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4436 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4437 					*dptr |= DPARM_WIDE;
4438 				else
4439 					*dptr &= ~DPARM_WIDE;
4440 			}
4441 
4442 			/*
4443 			 * XXX: FIX ME
4444 			 */
4445 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4446 				*dptr |= DPARM_SYNC;
4447 				/*
4448 				 * XXX: CHECK FOR LEGALITY
4449 				 */
4450 				sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4451 				sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4452 			} else {
4453 				*dptr &= ~DPARM_SYNC;
4454 			}
4455 			isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
4456 			    sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4457 			sdp->isp_devparam[tgt].dev_update = 1;
4458 			sdp->update = 1;
4459 		}
4460 		ccb->ccb_h.status = CAM_REQ_CMP;
4461 		xpt_done(ccb);
4462 		break;
4463 	case XPT_GET_TRAN_SETTINGS:
4464 		cts = &ccb->cts;
4465 		tgt = cts->ccb_h.target_id;
4466 		bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4467 		if (IS_FC(isp)) {
4468 			fcparam *fcp = FCPARAM(isp, bus);
4469 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4470 			struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4471 
4472 			cts->protocol = PROTO_SCSI;
4473 			cts->protocol_version = SCSI_REV_2;
4474 			cts->transport = XPORT_FC;
4475 			cts->transport_version = 0;
4476 
4477 			scsi->valid = CTS_SCSI_VALID_TQ;
4478 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4479 			fc->valid = CTS_FC_VALID_SPEED;
4480 			fc->bitrate = 100000;
4481 			fc->bitrate *= fcp->isp_gbspeed;
4482 			if (tgt > 0 && tgt < MAX_FC_TARG) {
4483 				fcportdb_t *lp = &fcp->portdb[tgt];
4484 				fc->wwnn = lp->node_wwn;
4485 				fc->wwpn = lp->port_wwn;
4486 				fc->port = lp->portid;
4487 				fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4488 			}
4489 		} else {
4490 			struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4491 			struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4492 			sdparam *sdp = SDPARAM(isp, bus);
4493 			uint16_t dval, pval, oval;
4494 
4495 			if (IS_CURRENT_SETTINGS(cts)) {
4496 				sdp->isp_devparam[tgt].dev_refresh = 1;
4497 				sdp->update = 1;
4498 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4499 				dval = sdp->isp_devparam[tgt].actv_flags;
4500 				oval = sdp->isp_devparam[tgt].actv_offset;
4501 				pval = sdp->isp_devparam[tgt].actv_period;
4502 			} else {
4503 				dval = sdp->isp_devparam[tgt].nvrm_flags;
4504 				oval = sdp->isp_devparam[tgt].nvrm_offset;
4505 				pval = sdp->isp_devparam[tgt].nvrm_period;
4506 			}
4507 
4508 			cts->protocol = PROTO_SCSI;
4509 			cts->protocol_version = SCSI_REV_2;
4510 			cts->transport = XPORT_SPI;
4511 			cts->transport_version = 2;
4512 
4513 			spi->valid = 0;
4514 			scsi->valid = 0;
4515 			spi->flags = 0;
4516 			scsi->flags = 0;
4517 			if (dval & DPARM_DISC) {
4518 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4519 			}
4520 			if ((dval & DPARM_SYNC) && oval && pval) {
4521 				spi->sync_offset = oval;
4522 				spi->sync_period = pval;
4523 			} else {
4524 				spi->sync_offset = 0;
4525 				spi->sync_period = 0;
4526 			}
4527 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4528 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4529 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4530 			if (dval & DPARM_WIDE) {
4531 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4532 			} else {
4533 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4534 			}
4535 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4536 				scsi->valid = CTS_SCSI_VALID_TQ;
4537 				if (dval & DPARM_TQING) {
4538 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4539 				}
4540 				spi->valid |= CTS_SPI_VALID_DISC;
4541 			}
4542 			isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4543 			    bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4544 		}
4545 		ccb->ccb_h.status = CAM_REQ_CMP;
4546 		xpt_done(ccb);
4547 		break;
4548 
4549 	case XPT_CALC_GEOMETRY:
4550 		cam_calc_geometry(&ccb->ccg, 1);
4551 		xpt_done(ccb);
4552 		break;
4553 
4554 	case XPT_RESET_BUS:		/* Reset the specified bus */
4555 		bus = cam_sim_bus(sim);
4556 		error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4557 		if (error) {
4558 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4559 			xpt_done(ccb);
4560 			break;
4561 		}
4562 		if (bootverbose) {
4563 			xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4564 		}
4565 		if (IS_FC(isp)) {
4566 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4567 		} else {
4568 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4569 		}
4570 		ccb->ccb_h.status = CAM_REQ_CMP;
4571 		xpt_done(ccb);
4572 		break;
4573 
4574 	case XPT_TERM_IO:		/* Terminate the I/O process */
4575 		ccb->ccb_h.status = CAM_REQ_INVALID;
4576 		xpt_done(ccb);
4577 		break;
4578 
4579 	case XPT_SET_SIM_KNOB:		/* Set SIM knobs */
4580 	{
4581 		struct ccb_sim_knob *kp = &ccb->knob;
4582 		fcparam *fcp;
4583 
4584 
4585 		if (!IS_FC(isp)) {
4586 			ccb->ccb_h.status = CAM_REQ_INVALID;
4587 			xpt_done(ccb);
4588 			break;
4589 		}
4590 
4591 		bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4592 		fcp = FCPARAM(isp, bus);
4593 
4594 		if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4595 			fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4596 			fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4597 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4598 		}
4599 		ccb->ccb_h.status = CAM_REQ_CMP;
4600 		if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4601 			int rchange = 0;
4602 			int newrole = 0;
4603 
4604 			switch (kp->xport_specific.fc.role) {
4605 			case KNOB_ROLE_NONE:
4606 				if (fcp->role != ISP_ROLE_NONE) {
4607 					rchange = 1;
4608 					newrole = ISP_ROLE_NONE;
4609 				}
4610 				break;
4611 			case KNOB_ROLE_TARGET:
4612 				if (fcp->role != ISP_ROLE_TARGET) {
4613 					rchange = 1;
4614 					newrole = ISP_ROLE_TARGET;
4615 				}
4616 				break;
4617 			case KNOB_ROLE_INITIATOR:
4618 				if (fcp->role != ISP_ROLE_INITIATOR) {
4619 					rchange = 1;
4620 					newrole = ISP_ROLE_INITIATOR;
4621 				}
4622 				break;
4623 			case KNOB_ROLE_BOTH:
4624 #if 0
4625 				if (fcp->role != ISP_ROLE_BOTH) {
4626 					rchange = 1;
4627 					newrole = ISP_ROLE_BOTH;
4628 				}
4629 #else
4630 				/*
4631 				 * We don't really support dual role at present on FC cards.
4632 				 *
4633 				 * We should, but a bunch of things are currently broken,
4634 				 * so don't allow it.
4635 				 */
4636 				isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4637 				ccb->ccb_h.status = CAM_REQ_INVALID;
4638 #endif
4639 				break;
4640 			}
4641 			if (rchange) {
4642 				if (isp_fc_change_role(isp, bus, newrole) != 0) {
4643 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4644 #ifdef	ISP_TARGET_MODE
4645 				} else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4646 					isp_enable_deferred_luns(isp, bus);
4647 #endif
4648 				}
4649 			}
4650 		}
4651 		xpt_done(ccb);
4652 		break;
4653 	}
4654 	case XPT_GET_SIM_KNOB:		/* Set SIM knobs */
4655 	{
4656 		struct ccb_sim_knob *kp = &ccb->knob;
4657 
4658 		if (IS_FC(isp)) {
4659 			fcparam *fcp;
4660 
4661 			bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4662 			fcp = FCPARAM(isp, bus);
4663 
4664 			kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4665 			kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4666 			switch (fcp->role) {
4667 			case ISP_ROLE_NONE:
4668 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4669 				break;
4670 			case ISP_ROLE_TARGET:
4671 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4672 				break;
4673 			case ISP_ROLE_INITIATOR:
4674 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4675 				break;
4676 			case ISP_ROLE_BOTH:
4677 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4678 				break;
4679 			}
4680 			kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4681 			ccb->ccb_h.status = CAM_REQ_CMP;
4682 		} else {
4683 			ccb->ccb_h.status = CAM_REQ_INVALID;
4684 		}
4685 		xpt_done(ccb);
4686 		break;
4687 	}
4688 	case XPT_PATH_INQ:		/* Path routing inquiry */
4689 	{
4690 		struct ccb_pathinq *cpi = &ccb->cpi;
4691 
4692 		cpi->version_num = 1;
4693 #ifdef	ISP_TARGET_MODE
4694 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4695 #else
4696 		cpi->target_sprt = 0;
4697 #endif
4698 		cpi->hba_eng_cnt = 0;
4699 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4700 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4701 		cpi->bus_id = cam_sim_bus(sim);
4702 		bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4703 		if (IS_FC(isp)) {
4704 			fcparam *fcp = FCPARAM(isp, bus);
4705 
4706 			cpi->hba_misc = PIM_NOBUSRESET;
4707 
4708 			/*
4709 			 * Because our loop ID can shift from time to time,
4710 			 * make our initiator ID out of range of our bus.
4711 			 */
4712 			cpi->initiator_id = cpi->max_target + 1;
4713 
4714 			/*
4715 			 * Set base transfer capabilities for Fibre Channel, for this HBA.
4716 			 */
4717 			if (IS_24XX(isp)) {
4718 				cpi->base_transfer_speed = 4000000;
4719 			} else if (IS_23XX(isp)) {
4720 				cpi->base_transfer_speed = 2000000;
4721 			} else {
4722 				cpi->base_transfer_speed = 1000000;
4723 			}
4724 			cpi->hba_inquiry = PI_TAG_ABLE;
4725 			cpi->transport = XPORT_FC;
4726 			cpi->transport_version = 0;
4727 			cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4728 			cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4729 			cpi->xport_specific.fc.port = fcp->isp_portid;
4730 			cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4731 		} else {
4732 			sdparam *sdp = SDPARAM(isp, bus);
4733 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4734 			cpi->hba_misc = 0;
4735 			cpi->initiator_id = sdp->isp_initiator_id;
4736 			cpi->base_transfer_speed = 3300;
4737 			cpi->transport = XPORT_SPI;
4738 			cpi->transport_version = 2;
4739 		}
4740 		cpi->protocol = PROTO_SCSI;
4741 		cpi->protocol_version = SCSI_REV_2;
4742 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4743 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4744 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4745 		cpi->unit_number = cam_sim_unit(sim);
4746 		cpi->ccb_h.status = CAM_REQ_CMP;
4747 		xpt_done(ccb);
4748 		break;
4749 	}
4750 	default:
4751 		ccb->ccb_h.status = CAM_REQ_INVALID;
4752 		xpt_done(ccb);
4753 		break;
4754 	}
4755 }
4756 
4757 #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4758 
4759 void
4760 isp_done(XS_T *sccb)
4761 {
4762 	ispsoftc_t *isp = XS_ISP(sccb);
4763 
4764 	if (XS_NOERR(sccb))
4765 		XS_SETERR(sccb, CAM_REQ_CMP);
4766 
4767 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4768 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4769 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4770 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4771 		} else {
4772 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4773 		}
4774 	}
4775 
4776 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4777 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4778 		isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
4779 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4780 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
4781 			xpt_freeze_devq(sccb->ccb_h.path, 1);
4782 		}
4783 	}
4784 
4785 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4786 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4787 	}
4788 
4789 	XS_CMD_S_DONE(sccb);
4790 	callout_stop(&PISP_PCMD(sccb)->wdog);
4791 	XS_CMD_S_CLEAR(sccb);
4792 	isp_free_pcmd(isp, (union ccb *) sccb);
4793 	xpt_done((union ccb *) sccb);
4794 }
4795 
4796 void
4797 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4798 {
4799 	int bus;
4800 	static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
4801 	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4802 	char *msg = NULL;
4803 	target_id_t tgt;
4804 	fcportdb_t *lp;
4805 	struct cam_path *tmppath;
4806 	va_list ap;
4807 
4808 	switch (cmd) {
4809 	case ISPASYNC_NEW_TGT_PARAMS:
4810 	{
4811 		struct ccb_trans_settings_scsi *scsi;
4812 		struct ccb_trans_settings_spi *spi;
4813 		int flags, tgt;
4814 		sdparam *sdp;
4815 		struct ccb_trans_settings cts;
4816 
4817 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
4818 
4819 		va_start(ap, cmd);
4820 		bus = va_arg(ap, int);
4821 		tgt = va_arg(ap, int);
4822 		va_end(ap);
4823 		sdp = SDPARAM(isp, bus);
4824 
4825 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4826 			isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4827 			break;
4828 		}
4829 		flags = sdp->isp_devparam[tgt].actv_flags;
4830 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4831 		cts.protocol = PROTO_SCSI;
4832 		cts.transport = XPORT_SPI;
4833 
4834 		scsi = &cts.proto_specific.scsi;
4835 		spi = &cts.xport_specific.spi;
4836 
4837 		if (flags & DPARM_TQING) {
4838 			scsi->valid |= CTS_SCSI_VALID_TQ;
4839 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4840 		}
4841 
4842 		if (flags & DPARM_DISC) {
4843 			spi->valid |= CTS_SPI_VALID_DISC;
4844 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4845 		}
4846 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4847 		if (flags & DPARM_WIDE) {
4848 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4849 		} else {
4850 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4851 		}
4852 		if (flags & DPARM_SYNC) {
4853 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4854 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4855 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
4856 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4857 		}
4858 		isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags);
4859 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4860 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4861 		xpt_free_path(tmppath);
4862 		break;
4863 	}
4864 	case ISPASYNC_BUS_RESET:
4865 	{
4866 		va_start(ap, cmd);
4867 		bus = va_arg(ap, int);
4868 		va_end(ap);
4869 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
4870 		if (IS_FC(isp)) {
4871 			xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
4872 		} else {
4873 			xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
4874 		}
4875 		break;
4876 	}
4877 	case ISPASYNC_LIP:
4878 		if (msg == NULL) {
4879 			msg = "LIP Received";
4880 		}
4881 		/* FALLTHROUGH */
4882 	case ISPASYNC_LOOP_RESET:
4883 		if (msg == NULL) {
4884 			msg = "LOOP Reset";
4885 		}
4886 		/* FALLTHROUGH */
4887 	case ISPASYNC_LOOP_DOWN:
4888 	{
4889 		struct isp_fc *fc;
4890 		if (msg == NULL) {
4891 			msg = "LOOP Down";
4892 		}
4893 		va_start(ap, cmd);
4894 		bus = va_arg(ap, int);
4895 		va_end(ap);
4896 
4897 		FCPARAM(isp, bus)->link_active = 1;
4898 
4899 		fc = ISP_FC_PC(isp, bus);
4900 		/*
4901 		 * We don't do any simq freezing if we are only in target mode
4902 		 */
4903 		if (fc->role & ISP_ROLE_INITIATOR) {
4904 			if (fc->path) {
4905 				isp_freeze_loopdown(isp, bus, msg);
4906 			}
4907 			if (fc->ldt_running == 0) {
4908 				fc->ldt_running = 1;
4909 				callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
4910 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
4911 			}
4912 		}
4913 		isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
4914 		break;
4915 	}
4916 	case ISPASYNC_LOOP_UP:
4917 		va_start(ap, cmd);
4918 		bus = va_arg(ap, int);
4919 		va_end(ap);
4920 		/*
4921 		 * Now we just note that Loop has come up. We don't
4922 		 * actually do anything because we're waiting for a
4923 		 * Change Notify before activating the FC cleanup
4924 		 * thread to look at the state of the loop again.
4925 		 */
4926 		FCPARAM(isp, bus)->link_active = 1;
4927 		ISP_FC_PC(isp, bus)->loop_dead = 0;
4928 		ISP_FC_PC(isp, bus)->loop_down_time = 0;
4929 		isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
4930 		break;
4931 	case ISPASYNC_DEV_ARRIVED:
4932 		va_start(ap, cmd);
4933 		bus = va_arg(ap, int);
4934 		lp = va_arg(ap, fcportdb_t *);
4935 		va_end(ap);
4936 		lp->reserved = 0;
4937 		if ((ISP_FC_PC(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
4938 			int dbidx = lp - FCPARAM(isp, bus)->portdb;
4939 			int i;
4940 
4941 			for (i = 0; i < MAX_FC_TARG; i++) {
4942 				if (i >= FL_ID && i <= SNS_ID) {
4943 					continue;
4944 				}
4945 				if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
4946 					break;
4947 				}
4948 			}
4949 			if (i < MAX_FC_TARG) {
4950 				FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
4951 				lp->dev_map_idx = i + 1;
4952 			} else {
4953 				isp_prt(isp, ISP_LOGWARN, "out of target ids");
4954 				isp_dump_portdb(isp, bus);
4955 			}
4956 		}
4957 		if (lp->dev_map_idx) {
4958 			tgt = lp->dev_map_idx - 1;
4959 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4960 			isp_make_here(isp, bus, tgt);
4961 		} else {
4962 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4963 		}
4964 		break;
4965 	case ISPASYNC_DEV_CHANGED:
4966 		va_start(ap, cmd);
4967 		bus = va_arg(ap, int);
4968 		lp = va_arg(ap, fcportdb_t *);
4969 		va_end(ap);
4970 		lp->reserved = 0;
4971 		if (isp_change_is_bad) {
4972 			lp->state = FC_PORTDB_STATE_NIL;
4973 			if (lp->dev_map_idx) {
4974 				tgt = lp->dev_map_idx - 1;
4975 				FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
4976 				lp->dev_map_idx = 0;
4977 				isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
4978 				isp_make_gone(isp, bus, tgt);
4979 			} else {
4980 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
4981 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4982 			}
4983 		} else {
4984 			lp->portid = lp->new_portid;
4985 			lp->roles = lp->new_roles;
4986 			if (lp->dev_map_idx) {
4987 				int t = lp->dev_map_idx - 1;
4988 				FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
4989 				tgt = lp->dev_map_idx - 1;
4990 				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
4991 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4992 			} else {
4993 				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
4994 			}
4995 		}
4996 		break;
4997 	case ISPASYNC_DEV_STAYED:
4998 		va_start(ap, cmd);
4999 		bus = va_arg(ap, int);
5000 		lp = va_arg(ap, fcportdb_t *);
5001 		va_end(ap);
5002 		if (lp->dev_map_idx) {
5003 			tgt = lp->dev_map_idx - 1;
5004 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5005 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5006 		} else {
5007 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5008 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5009 		}
5010 		break;
5011 	case ISPASYNC_DEV_GONE:
5012 		va_start(ap, cmd);
5013 		bus = va_arg(ap, int);
5014 		lp = va_arg(ap, fcportdb_t *);
5015 		va_end(ap);
5016 		/*
5017 		 * If this has a virtual target and we haven't marked it
5018 		 * that we're going to have isp_gdt tell the OS it's gone,
5019 		 * set the isp_gdt timer running on it.
5020 		 *
5021 		 * If it isn't marked that isp_gdt is going to get rid of it,
5022 		 * announce that it's gone.
5023 		 */
5024 		if (lp->dev_map_idx && lp->reserved == 0) {
5025 			lp->reserved = 1;
5026 			lp->new_reserved = ISP_FC_PC(isp, bus)->gone_device_time;
5027 			lp->state = FC_PORTDB_STATE_ZOMBIE;
5028 			if (ISP_FC_PC(isp, bus)->gdt_running == 0) {
5029 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d starting Gone Device Timer", bus);
5030 				ISP_FC_PC(isp, bus)->gdt_running = 1;
5031 				callout_reset(&ISP_FC_PC(isp, bus)->gdt, hz, isp_gdt, ISP_FC_PC(isp, bus));
5032 			}
5033 			tgt = lp->dev_map_idx - 1;
5034 			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5035 		} else if (lp->reserved == 0) {
5036 			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5037 		}
5038 		break;
5039 	case ISPASYNC_CHANGE_NOTIFY:
5040 	{
5041 		char *msg;
5042 		int evt, nphdl, nlstate, reason;
5043 
5044 		va_start(ap, cmd);
5045 		bus = va_arg(ap, int);
5046 		evt = va_arg(ap, int);
5047 		if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5048 			nphdl = va_arg(ap, int);
5049 			nlstate = va_arg(ap, int);
5050 			reason = va_arg(ap, int);
5051 		} else {
5052 			nphdl = NIL_HANDLE;
5053 			nlstate = reason = 0;
5054 		}
5055 		va_end(ap);
5056 
5057 		if (evt == ISPASYNC_CHANGE_PDB) {
5058 			msg = "Chan %d Port Database Changed";
5059 		} else if (evt == ISPASYNC_CHANGE_SNS) {
5060 			msg = "Chan %d Name Server Database Changed";
5061 		} else {
5062 			msg = "Chan %d Other Change Notify";
5063 		}
5064 
5065 		/*
5066 		 * If the loop down timer is running, cancel it.
5067 		 */
5068 		if (ISP_FC_PC(isp, bus)->ldt_running) {
5069 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5070 			ISP_FC_PC(isp, bus)->ldt_running = 0;
5071 			callout_stop(&ISP_FC_PC(isp, bus)->ldt);
5072 		}
5073 		isp_prt(isp, ISP_LOGINFO, msg, bus);
5074 		if (ISP_FC_PC(isp, bus)->role & ISP_ROLE_INITIATOR) {
5075 			isp_freeze_loopdown(isp, bus, msg);
5076 		}
5077 		wakeup(ISP_FC_PC(isp, bus));
5078 		break;
5079 	}
5080 #ifdef	ISP_TARGET_MODE
5081 	case ISPASYNC_TARGET_NOTIFY:
5082 	{
5083 		isp_notify_t *notify;
5084 		va_start(ap, cmd);
5085 		notify = va_arg(ap, isp_notify_t *);
5086 		va_end(ap);
5087 		switch (notify->nt_ncode) {
5088 		case NT_ABORT_TASK:
5089 		case NT_ABORT_TASK_SET:
5090 		case NT_CLEAR_ACA:
5091 		case NT_CLEAR_TASK_SET:
5092 		case NT_LUN_RESET:
5093 		case NT_TARGET_RESET:
5094 			/*
5095 			 * These are task management functions.
5096 			 */
5097 			isp_handle_platform_target_tmf(isp, notify);
5098 			break;
5099 		case NT_BUS_RESET:
5100 		case NT_LIP_RESET:
5101 		case NT_LINK_UP:
5102 		case NT_LINK_DOWN:
5103 			/*
5104 			 * No action need be taken here.
5105 			 */
5106 			break;
5107 		case NT_HBA_RESET:
5108 			isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5109 			break;
5110 		case NT_LOGOUT:
5111 			/*
5112 			 * This is device arrival/departure notification
5113 			 */
5114 			isp_handle_platform_target_notify_ack(isp, notify);
5115 			break;
5116 		case NT_ARRIVED:
5117 		{
5118 			struct ac_contract ac;
5119 			struct ac_device_changed *fc;
5120 
5121 			ac.contract_number = AC_CONTRACT_DEV_CHG;
5122 			fc = (struct ac_device_changed *) ac.contract_data;
5123 			fc->wwpn = notify->nt_wwn;
5124 			fc->port = notify->nt_sid;
5125 			fc->target = notify->nt_nphdl;
5126 			fc->arrived = 1;
5127 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5128 			break;
5129 		}
5130 		case NT_DEPARTED:
5131 		{
5132 			struct ac_contract ac;
5133 			struct ac_device_changed *fc;
5134 
5135 			ac.contract_number = AC_CONTRACT_DEV_CHG;
5136 			fc = (struct ac_device_changed *) ac.contract_data;
5137 			fc->wwpn = notify->nt_wwn;
5138 			fc->port = notify->nt_sid;
5139 			fc->target = notify->nt_nphdl;
5140 			fc->arrived = 0;
5141 			xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5142 			break;
5143 		}
5144 		default:
5145 			isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5146 			isp_handle_platform_target_notify_ack(isp, notify);
5147 			break;
5148 		}
5149 		break;
5150 	}
5151 	case ISPASYNC_TARGET_ACTION:
5152 	{
5153 		isphdr_t *hp;
5154 
5155 		va_start(ap, cmd);
5156 		hp = va_arg(ap, isphdr_t *);
5157 		va_end(ap);
5158 		switch (hp->rqs_entry_type) {
5159 		default:
5160 			isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5161 			break;
5162 		case RQSTYPE_NOTIFY:
5163 			if (IS_SCSI(isp)) {
5164 				isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5165 			} else if (IS_24XX(isp)) {
5166 				isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5167 			} else {
5168 				isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5169 			}
5170 			break;
5171 		case RQSTYPE_ATIO:
5172 			if (IS_24XX(isp)) {
5173 				isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5174 			} else {
5175 				isp_handle_platform_atio(isp, (at_entry_t *) hp);
5176 			}
5177 			break;
5178 		case RQSTYPE_ATIO2:
5179 			isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5180 			break;
5181 		case RQSTYPE_CTIO7:
5182 		case RQSTYPE_CTIO3:
5183 		case RQSTYPE_CTIO2:
5184 		case RQSTYPE_CTIO:
5185 			isp_handle_platform_ctio(isp, hp);
5186 			break;
5187 		case RQSTYPE_ABTS_RCVD:
5188 		{
5189 			abts_t *abts = (abts_t *)hp;
5190 			isp_notify_t notify, *nt = &notify;
5191 			tstate_t *tptr;
5192 			fcportdb_t *lp;
5193 			uint16_t chan;
5194 			uint32_t sid, did;
5195 
5196 			did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5197 			sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5198 			ISP_MEMZERO(nt, sizeof (isp_notify_t));
5199 
5200 			nt->nt_hba = isp;
5201 			nt->nt_did = did;
5202 			nt->nt_nphdl = abts->abts_nphdl;
5203 			nt->nt_sid = sid;
5204 			isp_find_chan_by_did(isp, did, &chan);
5205 			if (chan == ISP_NOCHAN) {
5206 				nt->nt_tgt = TGT_ANY;
5207 			} else {
5208 				nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5209 				if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5210 					nt->nt_wwn = lp->port_wwn;
5211 				} else {
5212 					nt->nt_wwn = INI_ANY;
5213 				}
5214 			}
5215 			/*
5216 			 * Try hard to find the lun for this command.
5217 			 */
5218 			tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5219 			if (tptr) {
5220 				nt->nt_lun = xpt_path_lun_id(tptr->owner);
5221 				rls_lun_statep(isp, tptr);
5222 			} else {
5223 				nt->nt_lun = LUN_ANY;
5224 			}
5225 			nt->nt_need_ack = 1;
5226 			nt->nt_tagval = abts->abts_rxid_task;
5227 			nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5228 			if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5229 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
5230 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5231 			} else {
5232 				isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
5233 				    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5234 			}
5235 			nt->nt_channel = chan;
5236 			nt->nt_ncode = NT_ABORT_TASK;
5237 			nt->nt_lreserved = hp;
5238 			isp_handle_platform_target_tmf(isp, nt);
5239 			break;
5240 		}
5241 		case RQSTYPE_ENABLE_LUN:
5242 		case RQSTYPE_MODIFY_LUN:
5243 			isp_ledone(isp, (lun_entry_t *) hp);
5244 			break;
5245 		}
5246 		break;
5247 	}
5248 #endif
5249 	case ISPASYNC_FW_CRASH:
5250 	{
5251 		uint16_t mbox1, mbox6;
5252 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
5253 		if (IS_DUALBUS(isp)) {
5254 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
5255 		} else {
5256 			mbox6 = 0;
5257 		}
5258 		isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5259 		mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5260 		isp->isp_osinfo.mbox_sleep_ok = 0;
5261 		isp_reinit(isp, 1);
5262 		isp->isp_osinfo.mbox_sleep_ok = mbox1;
5263 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5264 		break;
5265 	}
5266 	default:
5267 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5268 		break;
5269 	}
5270 }
5271 
5272 
5273 /*
5274  * Locks are held before coming here.
5275  */
5276 void
5277 isp_uninit(ispsoftc_t *isp)
5278 {
5279 	if (IS_24XX(isp)) {
5280 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5281 	} else {
5282 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5283 	}
5284 	ISP_DISABLE_INTS(isp);
5285 }
5286 
5287 /*
5288  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5289  * up from our platform default (defww{p|n}n) and morph them based upon
5290  * channel.
5291  *
5292  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5293  * based upon channel.
5294  */
5295 
5296 uint64_t
5297 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5298 {
5299 	uint64_t seed;
5300 	struct isp_fc *fc = ISP_FC_PC(isp, chan);
5301 
5302 	/*
5303 	 * If we're asking for a active WWN, the default overrides get
5304 	 * returned, otherwise the NVRAM value is picked.
5305 	 *
5306 	 * If we're asking for a default WWN, we just pick the default override.
5307 	 */
5308 	if (isactive) {
5309 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5310 		if (seed) {
5311 			return (seed);
5312 		}
5313 		seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5314 		if (seed) {
5315 			return (seed);
5316 		}
5317 		return (0x400000007F000009ull);
5318 	} else {
5319 		seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5320 	}
5321 
5322 
5323 	/*
5324 	 * For channel zero just return what we have. For either ACIIVE or
5325 	 * DEFAULT cases, we depend on default override of NVRAM values for
5326 	 * channel zero.
5327 	 */
5328 	if (chan == 0) {
5329 		return (seed);
5330 	}
5331 
5332 	/*
5333 	 * For other channels, we are doing one of three things:
5334 	 *
5335 	 * 1. If what we have now is non-zero, return it. Otherwise we morph
5336 	 * values from channel 0. 2. If we're here for a WWPN we synthesize
5337 	 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5338 	 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5339 	 */
5340 
5341 	if (seed) {
5342 		return (seed);
5343 	}
5344 	if (isactive) {
5345 		seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5346 	} else {
5347 		seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5348 	}
5349 
5350 	if (((seed >> 60) & 0xf) == 2) {
5351 		/*
5352 		 * The type 2 NAA fields for QLogic cards appear be laid out
5353 		 * thusly:
5354 		 *
5355 		 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5356 		 * port (1) or node (0) WWN distinguishor bit 48
5357 		 * physical port on dual-port chips (23XX/24XX)
5358 		 *
5359 		 * This is somewhat nutty, particularly since bit 48 is
5360 		 * irrelevant as they assign seperate serial numbers to
5361 		 * different physical ports anyway.
5362 		 *
5363 		 * We'll stick our channel number plus one first into bits
5364 		 * 57..59 and thence into bits 52..55 which allows for 8 bits
5365 		 * of channel which is comfortably more than our maximum
5366 		 * (126) now.
5367 		 */
5368 		seed &= ~0x0FF0000000000000ULL;
5369 		if (iswwnn == 0) {
5370 			seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5371 			seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5372 		}
5373 	} else {
5374 		seed = 0;
5375 	}
5376 	return (seed);
5377 }
5378 
5379 void
5380 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5381 {
5382 	va_list ap;
5383 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5384 		return;
5385 	}
5386 	printf("%s: ", device_get_nameunit(isp->isp_dev));
5387 	va_start(ap, fmt);
5388 	vprintf(fmt, ap);
5389 	va_end(ap);
5390 	printf("\n");
5391 }
5392 
5393 uint64_t
5394 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5395 {
5396 	uint64_t elapsed;
5397 	struct timespec x = *b;
5398 	timespecsub(&x, a);
5399 	elapsed = GET_NANOSEC(&x);
5400 	if (elapsed == 0)
5401 		elapsed++;
5402 	return (elapsed);
5403 }
5404 
5405 int
5406 isp_mbox_acquire(ispsoftc_t *isp)
5407 {
5408 	if (isp->isp_osinfo.mboxbsy) {
5409 		return (1);
5410 	} else {
5411 		isp->isp_osinfo.mboxcmd_done = 0;
5412 		isp->isp_osinfo.mboxbsy = 1;
5413 		return (0);
5414 	}
5415 }
5416 
5417 void
5418 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5419 {
5420 	unsigned int usecs = mbp->timeout;
5421 	unsigned int max, olim, ilim;
5422 
5423 	if (usecs == 0) {
5424 		usecs = MBCMD_DEFAULT_TIMEOUT;
5425 	}
5426 	max = isp->isp_mbxwrk0 + 1;
5427 
5428 	if (isp->isp_osinfo.mbox_sleep_ok) {
5429 		unsigned int ms = (usecs + 999) / 1000;
5430 
5431 		isp->isp_osinfo.mbox_sleep_ok = 0;
5432 		isp->isp_osinfo.mbox_sleeping = 1;
5433 		for (olim = 0; olim < max; olim++) {
5434 			msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5435 			if (isp->isp_osinfo.mboxcmd_done) {
5436 				break;
5437 			}
5438 		}
5439 		isp->isp_osinfo.mbox_sleep_ok = 1;
5440 		isp->isp_osinfo.mbox_sleeping = 0;
5441 	} else {
5442 		for (olim = 0; olim < max; olim++) {
5443 			for (ilim = 0; ilim < usecs; ilim += 100) {
5444 				uint32_t isr;
5445 				uint16_t sema, mbox;
5446 				if (isp->isp_osinfo.mboxcmd_done) {
5447 					break;
5448 				}
5449 				if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5450 					isp_intr(isp, isr, sema, mbox);
5451 					if (isp->isp_osinfo.mboxcmd_done) {
5452 						break;
5453 					}
5454 				}
5455 				ISP_DELAY(100);
5456 			}
5457 			if (isp->isp_osinfo.mboxcmd_done) {
5458 				break;
5459 			}
5460 		}
5461 	}
5462 	if (isp->isp_osinfo.mboxcmd_done == 0) {
5463 		isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5464 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5465 		mbp->param[0] = MBOX_TIMEOUT;
5466 		isp->isp_osinfo.mboxcmd_done = 1;
5467 	}
5468 }
5469 
5470 void
5471 isp_mbox_notify_done(ispsoftc_t *isp)
5472 {
5473 	if (isp->isp_osinfo.mbox_sleeping) {
5474 		wakeup(&isp->isp_mbxworkp);
5475 	}
5476 	isp->isp_osinfo.mboxcmd_done = 1;
5477 }
5478 
5479 void
5480 isp_mbox_release(ispsoftc_t *isp)
5481 {
5482 	isp->isp_osinfo.mboxbsy = 0;
5483 }
5484 
5485 int
5486 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5487 {
5488 	int ret = 0;
5489 	if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5490 		ret = -1;
5491 	} else {
5492 		isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5493 	}
5494 	return (ret);
5495 }
5496 
5497 int
5498 isp_mstohz(int ms)
5499 {
5500 	int hz;
5501 	struct timeval t;
5502 	t.tv_sec = ms / 1000;
5503 	t.tv_usec = (ms % 1000) * 1000;
5504 	hz = tvtohz(&t);
5505 	if (hz < 0) {
5506 		hz = 0x7fffffff;
5507 	}
5508 	if (hz == 0) {
5509 		hz = 1;
5510 	}
5511 	return (hz);
5512 }
5513 
5514 void
5515 isp_platform_intr(void *arg)
5516 {
5517 	ispsoftc_t *isp = arg;
5518 	uint32_t isr;
5519 	uint16_t sema, mbox;
5520 
5521 	ISP_LOCK(isp);
5522 	isp->isp_intcnt++;
5523 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5524 		isp->isp_intbogus++;
5525 	} else {
5526 		isp_intr(isp, isr, sema, mbox);
5527 	}
5528 	ISP_UNLOCK(isp);
5529 }
5530 
5531 void
5532 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5533 {
5534 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5535 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5536 	} else {
5537 		bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5538 	}
5539 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5540 }
5541 
5542 void
5543 isp_timer(void *arg)
5544 {
5545 	ispsoftc_t *isp = arg;
5546 #ifdef	ISP_TARGET_MODE
5547 	isp_tmcmd_restart(isp);
5548 #endif
5549 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
5550 }
5551