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