xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 963e8efffe4ab97233102e0e25f95061b6fefbe3)
1 /*-
2  *
3  * Copyright (c) 1997-2006 by Matthew Jacob
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30  */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <dev/isp/isp_freebsd.h>
34 #include <sys/unistd.h>
35 #include <sys/kthread.h>
36 #include <machine/stdarg.h>	/* for use by isp_prt below */
37 #include <sys/conf.h>
38 #include <sys/module.h>
39 #include <sys/ioccom.h>
40 #include <dev/isp/isp_ioctl.h>
41 #if	__FreeBSD_version >= 500000
42 #include <sys/sysctl.h>
43 #endif
44 #include <cam/cam_periph.h>
45 
46 #if !defined(CAM_NEW_TRAN_CODE) && __FreeBSD_version >= 700025
47 #define	CAM_NEW_TRAN_CODE	1
48 #endif
49 
50 
51 MODULE_VERSION(isp, 1);
52 MODULE_DEPEND(isp, cam, 1, 1, 1);
53 int isp_announced = 0;
54 int isp_fabric_hysteresis = 5;
55 int isp_loop_down_limit = 300;	/* default loop down limit */
56 int isp_change_is_bad = 0;	/* "changed" devices are bad */
57 int isp_quickboot_time = 15;	/* don't wait more than N secs for loop up */
58 int isp_gone_device_time = 30;	/* grace time before reporting device lost */
59 static const char *roles[4] = {
60     "(none)", "Target", "Initiator", "Target/Initiator"
61 };
62 static const char prom3[] =
63     "PortID 0x%06x Departed from Target %u because of %s";
64 
65 static void isp_freeze_loopdown(ispsoftc_t *, char *);
66 static d_ioctl_t ispioctl;
67 static void isp_intr_enable(void *);
68 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
69 static void isp_poll(struct cam_sim *);
70 static timeout_t isp_watchdog;
71 static timeout_t isp_ldt;
72 static void isp_kthread(void *);
73 static void isp_action(struct cam_sim *, union ccb *);
74 
75 #if __FreeBSD_version < 700000
76 ispfwfunc *isp_get_firmware_p = NULL;
77 #endif
78 
79 #if __FreeBSD_version < 500000
80 #define ISP_CDEV_MAJOR	248
81 static struct cdevsw isp_cdevsw = {
82 	/* open */	nullopen,
83 	/* close */	nullclose,
84 	/* read */	noread,
85 	/* write */	nowrite,
86 	/* ioctl */	ispioctl,
87 	/* poll */	nopoll,
88 	/* mmap */	nommap,
89 	/* strategy */	nostrategy,
90 	/* name */	"isp",
91 	/* maj */	ISP_CDEV_MAJOR,
92 	/* dump */	nodump,
93 	/* psize */	nopsize,
94 	/* flags */	D_TAPE,
95 };
96 #define	isp_sysctl_update(x)	do { ; } while (0)
97 #else
98 static struct cdevsw isp_cdevsw = {
99 	.d_version =	D_VERSION,
100 	.d_flags =	D_NEEDGIANT,
101 	.d_ioctl =	ispioctl,
102 	.d_name =	"isp",
103 };
104 static void isp_sysctl_update(ispsoftc_t *);
105 #endif
106 
107 static ispsoftc_t *isplist = NULL;
108 
109 void
110 isp_attach(ispsoftc_t *isp)
111 {
112 	int primary, secondary;
113 	struct ccb_setasync csa;
114 	struct cam_devq *devq;
115 	struct cam_sim *sim;
116 	struct cam_path *path;
117 
118 	/*
119 	 * Establish (in case of 12X0) which bus is the primary.
120 	 */
121 
122 	primary = 0;
123 	secondary = 1;
124 
125 	/*
126 	 * Create the device queue for our SIM(s).
127 	 */
128 	devq = cam_simq_alloc(isp->isp_maxcmds);
129 	if (devq == NULL) {
130 		return;
131 	}
132 
133 	/*
134 	 * Construct our SIM entry.
135 	 */
136 	ISPLOCK_2_CAMLOCK(isp);
137 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
138 	    device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
139 	if (sim == NULL) {
140 		cam_simq_free(devq);
141 		CAMLOCK_2_ISPLOCK(isp);
142 		return;
143 	}
144 	CAMLOCK_2_ISPLOCK(isp);
145 
146 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
147 	isp->isp_osinfo.ehook.ich_arg = isp;
148 	ISPLOCK_2_CAMLOCK(isp);
149 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
150 		cam_sim_free(sim, TRUE);
151 		CAMLOCK_2_ISPLOCK(isp);
152 		isp_prt(isp, ISP_LOGERR,
153 		    "could not establish interrupt enable hook");
154 		return;
155 	}
156 
157 	if (xpt_bus_register(sim, primary) != CAM_SUCCESS) {
158 		cam_sim_free(sim, TRUE);
159 		CAMLOCK_2_ISPLOCK(isp);
160 		return;
161 	}
162 
163 	if (xpt_create_path(&path, NULL, cam_sim_path(sim),
164 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
165 		xpt_bus_deregister(cam_sim_path(sim));
166 		cam_sim_free(sim, TRUE);
167 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
168 		CAMLOCK_2_ISPLOCK(isp);
169 		return;
170 	}
171 
172 	xpt_setup_ccb(&csa.ccb_h, path, 5);
173 	csa.ccb_h.func_code = XPT_SASYNC_CB;
174 	csa.event_enable = AC_LOST_DEVICE;
175 	csa.callback = isp_cam_async;
176 	csa.callback_arg = sim;
177 	xpt_action((union ccb *)&csa);
178 	CAMLOCK_2_ISPLOCK(isp);
179 	isp->isp_sim = sim;
180 	isp->isp_path = path;
181 	/*
182 	 * Create a kernel thread for fibre channel instances. We
183 	 * don't have dual channel FC cards.
184 	 */
185 	if (IS_FC(isp)) {
186 		ISPLOCK_2_CAMLOCK(isp);
187 #if __FreeBSD_version >= 500000
188 		cv_init(&isp->isp_osinfo.kthread_cv, "isp_kthread_cv");
189 		if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
190 		    RFHIGHPID, 0, "%s: fc_thrd",
191 		    device_get_nameunit(isp->isp_dev)))
192 #else
193 		if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
194 		    "%s: fc_thrd", device_get_nameunit(isp->isp_dev)))
195 #endif
196 		{
197 			xpt_bus_deregister(cam_sim_path(sim));
198 			cam_sim_free(sim, TRUE);
199 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
200 			CAMLOCK_2_ISPLOCK(isp);
201 			isp_prt(isp, ISP_LOGERR, "could not create kthread");
202 			return;
203 		}
204 		CAMLOCK_2_ISPLOCK(isp);
205 		/*
206 		 * We start by being "loop down" if we have an initiator role
207 		 */
208 		if (isp->isp_role & ISP_ROLE_INITIATOR) {
209 			isp_freeze_loopdown(isp, "isp_attach");
210 			isp->isp_osinfo.ldt =
211 			    timeout(isp_ldt, isp, isp_quickboot_time * hz);
212 			isp->isp_osinfo.ldt_running = 1;
213 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
214 			   "Starting Initial Loop Down Timer");
215 		}
216 	}
217 
218 
219 	/*
220 	 * If we have a second channel, construct SIM entry for that.
221 	 */
222 	if (IS_DUALBUS(isp)) {
223 		ISPLOCK_2_CAMLOCK(isp);
224 		sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
225 		    device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
226 		if (sim == NULL) {
227 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
228 			xpt_free_path(isp->isp_path);
229 			cam_simq_free(devq);
230 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
231 			return;
232 		}
233 		if (xpt_bus_register(sim, secondary) != CAM_SUCCESS) {
234 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
235 			xpt_free_path(isp->isp_path);
236 			cam_sim_free(sim, TRUE);
237 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
238 			CAMLOCK_2_ISPLOCK(isp);
239 			return;
240 		}
241 
242 		if (xpt_create_path(&path, NULL, cam_sim_path(sim),
243 		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
244 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
245 			xpt_free_path(isp->isp_path);
246 			xpt_bus_deregister(cam_sim_path(sim));
247 			cam_sim_free(sim, TRUE);
248 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
249 			CAMLOCK_2_ISPLOCK(isp);
250 			return;
251 		}
252 
253 		xpt_setup_ccb(&csa.ccb_h, path, 5);
254 		csa.ccb_h.func_code = XPT_SASYNC_CB;
255 		csa.event_enable = AC_LOST_DEVICE;
256 		csa.callback = isp_cam_async;
257 		csa.callback_arg = sim;
258 		xpt_action((union ccb *)&csa);
259 		CAMLOCK_2_ISPLOCK(isp);
260 		isp->isp_sim2 = sim;
261 		isp->isp_path2 = path;
262 	}
263 
264 	/*
265 	 * Create device nodes
266 	 */
267 	(void) make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
268 	    GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
269 
270 	if (isp->isp_role != ISP_ROLE_NONE) {
271 		isp->isp_state = ISP_RUNSTATE;
272 		ISP_ENABLE_INTS(isp);
273 	}
274 	if (isplist == NULL) {
275 		isplist = isp;
276 	} else {
277 		ispsoftc_t *tmp = isplist;
278 		while (tmp->isp_osinfo.next) {
279 			tmp = tmp->isp_osinfo.next;
280 		}
281 		tmp->isp_osinfo.next = isp;
282 	}
283 	isp_sysctl_update(isp);
284 }
285 
286 static void
287 isp_freeze_loopdown(ispsoftc_t *isp, char *msg)
288 {
289 	if (isp->isp_osinfo.simqfrozen == 0) {
290 		isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown)", msg);
291 		isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
292 		ISPLOCK_2_CAMLOCK(isp);
293 		xpt_freeze_simq(isp->isp_sim, 1);
294 		CAMLOCK_2_ISPLOCK(isp);
295 	} else {
296 		isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown)", msg);
297 		isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
298 	}
299 }
300 
301 
302 #if __FreeBSD_version < 500000
303 #define	_DEV	dev_t
304 #define	_IOP	struct proc
305 #else
306 #define	_IOP	struct thread
307 #define	_DEV	struct cdev *
308 #endif
309 
310 static int
311 ispioctl(_DEV dev, u_long c, caddr_t addr, int flags, _IOP *td)
312 {
313 	ispsoftc_t *isp;
314 	int nr, retval = ENOTTY;
315 
316 	isp = isplist;
317 	while (isp) {
318 		if (minor(dev) == device_get_unit(isp->isp_dev)) {
319 			break;
320 		}
321 		isp = isp->isp_osinfo.next;
322 	}
323 	if (isp == NULL)
324 		return (ENXIO);
325 
326 	switch (c) {
327 #ifdef	ISP_FW_CRASH_DUMP
328 	case ISP_GET_FW_CRASH_DUMP:
329 		if (IS_FC(isp)) {
330 			uint16_t *ptr = FCPARAM(isp)->isp_dump_data;
331 			size_t sz;
332 
333 			retval = 0;
334 			if (IS_2200(isp)) {
335 				sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
336 			} else {
337 				sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
338 			}
339 			ISP_LOCK(isp);
340 			if (ptr && *ptr) {
341 				void *uaddr = *((void **) addr);
342 				if (copyout(ptr, uaddr, sz)) {
343 					retval = EFAULT;
344 				} else {
345 					*ptr = 0;
346 				}
347 			} else {
348 				retval = ENXIO;
349 			}
350 			ISP_UNLOCK(isp);
351 		}
352 		break;
353 	case ISP_FORCE_CRASH_DUMP:
354 		if (IS_FC(isp)) {
355 			ISP_LOCK(isp);
356 			isp_freeze_loopdown(isp,
357 			    "ispioctl(ISP_FORCE_CRASH_DUMP)");
358 			isp_fw_dump(isp);
359 			isp_reinit(isp);
360 			ISP_UNLOCK(isp);
361 			retval = 0;
362 		}
363 		break;
364 #endif
365 	case ISP_SDBLEV:
366 	{
367 		int olddblev = isp->isp_dblev;
368 		isp->isp_dblev = *(int *)addr;
369 		*(int *)addr = olddblev;
370 		retval = 0;
371 		break;
372 	}
373 	case ISP_GETROLE:
374 		*(int *)addr = isp->isp_role;
375 		retval = 0;
376 		break;
377 	case ISP_SETROLE:
378 		nr = *(int *)addr;
379 		if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
380 			retval = EINVAL;
381 			break;
382 		}
383 		*(int *)addr = isp->isp_role;
384 		isp->isp_role = nr;
385 		/* FALLTHROUGH */
386 	case ISP_RESETHBA:
387 		ISP_LOCK(isp);
388 		isp_reinit(isp);
389 		ISP_UNLOCK(isp);
390 		retval = 0;
391 		break;
392 	case ISP_RESCAN:
393 		if (IS_FC(isp)) {
394 			ISP_LOCK(isp);
395 			if (isp_fc_runstate(isp, 5 * 1000000)) {
396 				retval = EIO;
397 			} else {
398 				retval = 0;
399 			}
400 			ISP_UNLOCK(isp);
401 		}
402 		break;
403 	case ISP_FC_LIP:
404 		if (IS_FC(isp)) {
405 			ISP_LOCK(isp);
406 			if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
407 				retval = EIO;
408 			} else {
409 				retval = 0;
410 			}
411 			ISP_UNLOCK(isp);
412 		}
413 		break;
414 	case ISP_FC_GETDINFO:
415 	{
416 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
417 		fcportdb_t *lp;
418 
419 		if (IS_SCSI(isp)) {
420 			break;
421 		}
422 		if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
423 			retval = EINVAL;
424 			break;
425 		}
426 		ISP_LOCK(isp);
427 		lp = &FCPARAM(isp)->portdb[ifc->loopid];
428 		if (lp->state == FC_PORTDB_STATE_VALID) {
429 			ifc->role = lp->roles;
430 			ifc->loopid = lp->handle;
431 			ifc->portid = lp->portid;
432 			ifc->node_wwn = lp->node_wwn;
433 			ifc->port_wwn = lp->port_wwn;
434 			retval = 0;
435 		} else {
436 			retval = ENODEV;
437 		}
438 		ISP_UNLOCK(isp);
439 		break;
440 	}
441 	case ISP_GET_STATS:
442 	{
443 		isp_stats_t *sp = (isp_stats_t *) addr;
444 
445 		MEMZERO(sp, sizeof (*sp));
446 		sp->isp_stat_version = ISP_STATS_VERSION;
447 		sp->isp_type = isp->isp_type;
448 		sp->isp_revision = isp->isp_revision;
449 		ISP_LOCK(isp);
450 		sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
451 		sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
452 		sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
453 		sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
454 		sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
455 		sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
456 		sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
457 		sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
458 		ISP_UNLOCK(isp);
459 		retval = 0;
460 		break;
461 	}
462 	case ISP_CLR_STATS:
463 		ISP_LOCK(isp);
464 		isp->isp_intcnt = 0;
465 		isp->isp_intbogus = 0;
466 		isp->isp_intmboxc = 0;
467 		isp->isp_intoasync = 0;
468 		isp->isp_rsltccmplt = 0;
469 		isp->isp_fphccmplt = 0;
470 		isp->isp_rscchiwater = 0;
471 		isp->isp_fpcchiwater = 0;
472 		ISP_UNLOCK(isp);
473 		retval = 0;
474 		break;
475 	case ISP_FC_GETHINFO:
476 	{
477 		struct isp_hba_device *hba = (struct isp_hba_device *) addr;
478 		MEMZERO(hba, sizeof (*hba));
479 
480 		hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
481 		hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
482 		hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
483 		if (IS_FC(isp)) {
484 			hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
485 			hba->fc_scsi_supported = 1;
486 			hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
487 			hba->fc_loopid = FCPARAM(isp)->isp_loopid;
488 			hba->nvram_node_wwn = FCPARAM(isp)->isp_nodewwn;
489 			hba->nvram_port_wwn = FCPARAM(isp)->isp_portwwn;
490 			hba->active_node_wwn = ISP_NODEWWN(isp);
491 			hba->active_port_wwn = ISP_PORTWWN(isp);
492 		}
493 		retval = 0;
494 		break;
495 	}
496 	case ISP_GET_FC_PARAM:
497 	{
498 		struct isp_fc_param *f = (struct isp_fc_param *) addr;
499 
500 		if (IS_SCSI(isp)) {
501 			break;
502 		}
503 		f->parameter = 0;
504 		if (strcmp(f->param_name, "framelength") == 0) {
505 			f->parameter = FCPARAM(isp)->isp_maxfrmlen;
506 			retval = 0;
507 			break;
508 		}
509 		if (strcmp(f->param_name, "exec_throttle") == 0) {
510 			f->parameter = FCPARAM(isp)->isp_execthrottle;
511 			retval = 0;
512 			break;
513 		}
514 		if (strcmp(f->param_name, "fullduplex") == 0) {
515 			if (FCPARAM(isp)->isp_fwoptions & ICBOPT_FULL_DUPLEX)
516 				f->parameter = 1;
517 			retval = 0;
518 			break;
519 		}
520 		if (strcmp(f->param_name, "loopid") == 0) {
521 			f->parameter = FCPARAM(isp)->isp_loopid;
522 			retval = 0;
523 			break;
524 		}
525 		retval = EINVAL;
526 		break;
527 	}
528 	case ISP_SET_FC_PARAM:
529 	{
530 		struct isp_fc_param *f = (struct isp_fc_param *) addr;
531 		uint32_t param = f->parameter;
532 
533 		if (IS_SCSI(isp)) {
534 			break;
535 		}
536 		f->parameter = 0;
537 		if (strcmp(f->param_name, "framelength") == 0) {
538 			if (param != 512 && param != 1024 && param != 1024) {
539 				retval = EINVAL;
540 				break;
541 			}
542 			FCPARAM(isp)->isp_maxfrmlen = param;
543 			retval = 0;
544 			break;
545 		}
546 		if (strcmp(f->param_name, "exec_throttle") == 0) {
547 			if (param < 16 || param > 255) {
548 				retval = EINVAL;
549 				break;
550 			}
551 			FCPARAM(isp)->isp_execthrottle = param;
552 			retval = 0;
553 			break;
554 		}
555 		if (strcmp(f->param_name, "fullduplex") == 0) {
556 			if (param != 0 && param != 1) {
557 				retval = EINVAL;
558 				break;
559 			}
560 			if (param) {
561 				FCPARAM(isp)->isp_fwoptions |=
562 				    ICBOPT_FULL_DUPLEX;
563 			} else {
564 				FCPARAM(isp)->isp_fwoptions &=
565 				    ~ICBOPT_FULL_DUPLEX;
566 			}
567 			retval = 0;
568 			break;
569 		}
570 		if (strcmp(f->param_name, "loopid") == 0) {
571 			if (param < 0 || param > 125) {
572 				retval = EINVAL;
573 				break;
574 			}
575 			FCPARAM(isp)->isp_loopid = param;
576 			retval = 0;
577 			break;
578 		}
579 		retval = EINVAL;
580 		break;
581 	}
582 	case ISP_TSK_MGMT:
583 	{
584 		int needmarker;
585 		struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
586 		uint16_t loopid;
587 		mbreg_t mbs;
588 
589 		if (IS_SCSI(isp)) {
590 			break;
591 		}
592 
593 		memset(&mbs, 0, sizeof (mbs));
594 		needmarker = retval = 0;
595 		loopid = fct->loopid;
596 		if (FCPARAM(isp)->isp_2klogin == 0) {
597 			loopid <<= 8;
598 		}
599 		switch (fct->action) {
600 		case IPT_CLEAR_ACA:
601 			mbs.param[0] = MBOX_CLEAR_ACA;
602 			mbs.param[1] = loopid;
603 			mbs.param[2] = fct->lun;
604 			break;
605 		case IPT_TARGET_RESET:
606 			mbs.param[0] = MBOX_TARGET_RESET;
607 			mbs.param[1] = loopid;
608 			needmarker = 1;
609 			break;
610 		case IPT_LUN_RESET:
611 			mbs.param[0] = MBOX_LUN_RESET;
612 			mbs.param[1] = loopid;
613 			mbs.param[2] = fct->lun;
614 			needmarker = 1;
615 			break;
616 		case IPT_CLEAR_TASK_SET:
617 			mbs.param[0] = MBOX_CLEAR_TASK_SET;
618 			mbs.param[1] = loopid;
619 			mbs.param[2] = fct->lun;
620 			needmarker = 1;
621 			break;
622 		case IPT_ABORT_TASK_SET:
623 			mbs.param[0] = MBOX_ABORT_TASK_SET;
624 			mbs.param[1] = loopid;
625 			mbs.param[2] = fct->lun;
626 			needmarker = 1;
627 			break;
628 		default:
629 			retval = EINVAL;
630 			break;
631 		}
632 		if (retval == 0) {
633 			ISP_LOCK(isp);
634 			if (needmarker) {
635 				isp->isp_sendmarker |= 1;
636 			}
637 			retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
638 			ISP_UNLOCK(isp);
639 			if (retval)
640 				retval = EIO;
641 		}
642 		break;
643 	}
644 	default:
645 		break;
646 	}
647 	return (retval);
648 }
649 
650 #if __FreeBSD_version >= 500000
651 static void
652 isp_sysctl_update(ispsoftc_t *isp)
653 {
654 	struct sysctl_ctx_list *ctx =
655 	    device_get_sysctl_ctx(isp->isp_osinfo.dev);
656 	struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
657 
658 	if (IS_SCSI(isp)) {
659 		return;
660 	}
661 
662 	snprintf(isp->isp_osinfo.sysctl_info.fc.wwnn,
663 	    sizeof (isp->isp_osinfo.sysctl_info.fc.wwnn), "0x%08x%08x",
664 	    (uint32_t) (ISP_NODEWWN(isp) >> 32), (uint32_t) ISP_NODEWWN(isp));
665 
666 	snprintf(isp->isp_osinfo.sysctl_info.fc.wwpn,
667 	    sizeof (isp->isp_osinfo.sysctl_info.fc.wwpn), "0x%08x%08x",
668 	    (uint32_t) (ISP_PORTWWN(isp) >> 32), (uint32_t) ISP_PORTWWN(isp));
669 
670 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
671 	       "wwnn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwnn, 0,
672 	       "World Wide Node Name");
673 
674 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
675 	       "wwpn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwpn, 0,
676 	       "World Wide Port Name");
677 
678 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
679 	    "loop_down_limit",
680 	    CTLFLAG_RW, &isp->isp_osinfo.loop_down_limit, 0,
681 	    "How long to wait for loop to come back up");
682 
683 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
684 	    "gone_device_time",
685 	    CTLFLAG_RW, &isp->isp_osinfo.gone_device_time, 0,
686 	    "How long to wait for a device to reappear");
687 }
688 #endif
689 
690 static void
691 isp_intr_enable(void *arg)
692 {
693 	ispsoftc_t *isp = arg;
694 	if (isp->isp_role != ISP_ROLE_NONE) {
695 		ISP_ENABLE_INTS(isp);
696 	}
697 	/* Release our hook so that the boot can continue. */
698 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
699 }
700 
701 /*
702  * Put the target mode functions here, because some are inlines
703  */
704 
705 #ifdef	ISP_TARGET_MODE
706 
707 static __inline int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
708 static __inline int are_any_luns_enabled(ispsoftc_t *, int);
709 static __inline tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
710 static __inline void rls_lun_statep(ispsoftc_t *, tstate_t *);
711 static __inline atio_private_data_t *isp_get_atpd(ispsoftc_t *, int);
712 static cam_status
713 create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
714 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
715 static int isp_en_lun(ispsoftc_t *, union ccb *);
716 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
717 static cam_status isp_abort_tgt_ccb(ispsoftc_t *, union ccb *);
718 static timeout_t isp_refire_putback_atio;
719 static void isp_complete_ctio(union ccb *);
720 static void isp_target_putback_atio(union ccb *);
721 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
722 static int isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
723 static int isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
724 static int isp_handle_platform_ctio(ispsoftc_t *, void *);
725 static int isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
726 static int isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
727 
728 static __inline int
729 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
730 {
731 	tstate_t *tptr;
732 	tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
733 	if (tptr == NULL) {
734 		return (0);
735 	}
736 	do {
737 		if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
738 			return (1);
739 		}
740 	} while ((tptr = tptr->next) != NULL);
741 	return (0);
742 }
743 
744 static __inline int
745 are_any_luns_enabled(ispsoftc_t *isp, int port)
746 {
747 	int lo, hi;
748 	if (IS_DUALBUS(isp)) {
749 		lo = (port * (LUN_HASH_SIZE >> 1));
750 		hi = lo + (LUN_HASH_SIZE >> 1);
751 	} else {
752 		lo = 0;
753 		hi = LUN_HASH_SIZE;
754 	}
755 	for (lo = 0; lo < hi; lo++) {
756 		if (isp->isp_osinfo.lun_hash[lo]) {
757 			return (1);
758 		}
759 	}
760 	return (0);
761 }
762 
763 static __inline tstate_t *
764 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
765 {
766 	tstate_t *tptr = NULL;
767 
768 	if (lun == CAM_LUN_WILDCARD) {
769 		if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
770 			tptr = &isp->isp_osinfo.tsdflt[bus];
771 			tptr->hold++;
772 			return (tptr);
773 		}
774 		return (NULL);
775 	} else {
776 		tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
777 		if (tptr == NULL) {
778 			return (NULL);
779 		}
780 	}
781 
782 	do {
783 		if (tptr->lun == lun && tptr->bus == bus) {
784 			tptr->hold++;
785 			return (tptr);
786 		}
787 	} while ((tptr = tptr->next) != NULL);
788 	return (tptr);
789 }
790 
791 static __inline void
792 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
793 {
794 	if (tptr->hold)
795 		tptr->hold--;
796 }
797 
798 static __inline atio_private_data_t *
799 isp_get_atpd(ispsoftc_t *isp, int tag)
800 {
801 	atio_private_data_t *atp;
802 	for (atp = isp->isp_osinfo.atpdp;
803 	    atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
804 		if (atp->tag == tag)
805 			return (atp);
806 	}
807 	return (NULL);
808 }
809 
810 static cam_status
811 create_lun_state(ispsoftc_t *isp, int bus,
812     struct cam_path *path, tstate_t **rslt)
813 {
814 	cam_status status;
815 	lun_id_t lun;
816 	int hfx;
817 	tstate_t *tptr, *new;
818 
819 	lun = xpt_path_lun_id(path);
820 	if (lun < 0) {
821 		return (CAM_LUN_INVALID);
822 	}
823 	if (is_lun_enabled(isp, bus, lun)) {
824 		return (CAM_LUN_ALRDY_ENA);
825 	}
826 	new = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
827 	if (new == NULL) {
828 		return (CAM_RESRC_UNAVAIL);
829 	}
830 
831 	status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
832 	    xpt_path_target_id(path), xpt_path_lun_id(path));
833 	if (status != CAM_REQ_CMP) {
834 		free(new, M_DEVBUF);
835 		return (status);
836 	}
837 	new->bus = bus;
838 	new->lun = lun;
839 	SLIST_INIT(&new->atios);
840 	SLIST_INIT(&new->inots);
841 	new->hold = 1;
842 
843 	hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
844 	tptr = isp->isp_osinfo.lun_hash[hfx];
845 	if (tptr == NULL) {
846 		isp->isp_osinfo.lun_hash[hfx] = new;
847 	} else {
848 		while (tptr->next)
849 			tptr = tptr->next;
850 		tptr->next = new;
851 	}
852 	*rslt = new;
853 	return (CAM_REQ_CMP);
854 }
855 
856 static __inline void
857 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
858 {
859 	int hfx;
860 	tstate_t *lw, *pw;
861 
862 	if (tptr->hold) {
863 		return;
864 	}
865 	hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
866 	pw = isp->isp_osinfo.lun_hash[hfx];
867 	if (pw == NULL) {
868 		return;
869 	} else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
870 		isp->isp_osinfo.lun_hash[hfx] = pw->next;
871 	} else {
872 		lw = pw;
873 		pw = lw->next;
874 		while (pw) {
875 			if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
876 				lw->next = pw->next;
877 				break;
878 			}
879 			lw = pw;
880 			pw = pw->next;
881 		}
882 		if (pw == NULL) {
883 			return;
884 		}
885 	}
886 	free(tptr, M_DEVBUF);
887 }
888 
889 /*
890  * Enable luns.
891  */
892 static int
893 isp_en_lun(ispsoftc_t *isp, union ccb *ccb)
894 {
895 	struct ccb_en_lun *cel = &ccb->cel;
896 	tstate_t *tptr;
897 	uint32_t seq;
898 	int bus, cmd, av, wildcard, tm_on;
899 	lun_id_t lun;
900 	target_id_t tgt;
901 
902 	bus = XS_CHANNEL(ccb);
903 	if (bus > 1) {
904 		xpt_print(ccb->ccb_h.path, "illegal bus %d\n", bus);
905 		ccb->ccb_h.status = CAM_PATH_INVALID;
906 		return (-1);
907 	}
908 	tgt = ccb->ccb_h.target_id;
909 	lun = ccb->ccb_h.target_lun;
910 
911 	if (isp->isp_dblev & ISP_LOGTDEBUG0) {
912 		xpt_print(ccb->ccb_h.path, "%sabling lun 0x%x on channel %d\n",
913 	    	    cel->enable? "en" : "dis", lun, bus);
914 	}
915 
916 	if ((lun != CAM_LUN_WILDCARD) &&
917 	    (lun < 0 || lun >= (lun_id_t) isp->isp_maxluns)) {
918 		ccb->ccb_h.status = CAM_LUN_INVALID;
919 		return (-1);
920 	}
921 
922 	if (IS_SCSI(isp)) {
923 		sdparam *sdp = isp->isp_param;
924 		sdp += bus;
925 		if (tgt != CAM_TARGET_WILDCARD &&
926 		    tgt != sdp->isp_initiator_id) {
927 			ccb->ccb_h.status = CAM_TID_INVALID;
928 			return (-1);
929 		}
930 	} else {
931 		/*
932 		 * There's really no point in doing this yet w/o multi-tid
933 		 * capability. Even then, it's problematic.
934 		 */
935 #if	0
936 		if (tgt != CAM_TARGET_WILDCARD &&
937 		    tgt != FCPARAM(isp)->isp_iid) {
938 			ccb->ccb_h.status = CAM_TID_INVALID;
939 			return (-1);
940 		}
941 #endif
942 		/*
943 		 * This is as a good a place as any to check f/w capabilities.
944 		 */
945 		if (FCPARAM(isp)->isp_tmode == 0) {
946 			xpt_print(ccb->ccb_h.path,
947 			    "firmware does not support target mode\n");
948 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
949 			return (-1);
950 		}
951 		/*
952 		 * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
953 		 * XXX: dork with our already fragile enable/disable code.
954 		 */
955 		if (FCPARAM(isp)->isp_sccfw == 0) {
956 			xpt_print(ccb->ccb_h.path,
957 			    "firmware not SCCLUN capable\n");
958 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
959 			return (-1);
960 		}
961 	}
962 
963 	if (tgt == CAM_TARGET_WILDCARD) {
964 		if (lun == CAM_LUN_WILDCARD) {
965 			wildcard = 1;
966 		} else {
967 			ccb->ccb_h.status = CAM_LUN_INVALID;
968 			return (-1);
969 		}
970 	} else {
971 		wildcard = 0;
972 	}
973 
974 	tm_on = (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) != 0;
975 
976 	/*
977 	 * Next check to see whether this is a target/lun wildcard action.
978 	 *
979 	 * If so, we know that we can accept commands for luns that haven't
980 	 * been enabled yet and send them upstream. Otherwise, we have to
981 	 * handle them locally (if we see them at all).
982 	 */
983 
984 	if (wildcard) {
985 		tptr = &isp->isp_osinfo.tsdflt[bus];
986 		if (cel->enable) {
987 			if (tm_on) {
988 				ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
989 				return (-1);
990 			}
991 			ccb->ccb_h.status =
992 			    xpt_create_path(&tptr->owner, NULL,
993 			    xpt_path_path_id(ccb->ccb_h.path),
994 			    xpt_path_target_id(ccb->ccb_h.path),
995 			    xpt_path_lun_id(ccb->ccb_h.path));
996 			if (ccb->ccb_h.status != CAM_REQ_CMP) {
997 				return (-1);
998 			}
999 			SLIST_INIT(&tptr->atios);
1000 			SLIST_INIT(&tptr->inots);
1001 			isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
1002 		} else {
1003 			if (tm_on == 0) {
1004 				ccb->ccb_h.status = CAM_REQ_CMP;
1005 				return (-1);
1006 			}
1007 			if (tptr->hold) {
1008 				ccb->ccb_h.status = CAM_SCSI_BUSY;
1009 				return (-1);
1010 			}
1011 			xpt_free_path(tptr->owner);
1012 			isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
1013 		}
1014 	}
1015 
1016 	/*
1017 	 * Now check to see whether this bus needs to be
1018 	 * enabled/disabled with respect to target mode.
1019 	 */
1020 	av = bus << 31;
1021 	if (cel->enable && tm_on == 0) {
1022 		av |= ENABLE_TARGET_FLAG;
1023 		av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1024 		if (av) {
1025 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1026 			if (wildcard) {
1027 				isp->isp_osinfo.tmflags[bus] &=
1028 				    ~TM_WILDCARD_ENABLED;
1029 				xpt_free_path(tptr->owner);
1030 			}
1031 			return (-1);
1032 		}
1033 		isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
1034 		xpt_print(ccb->ccb_h.path, "Target Mode Enabled\n");
1035 	} else if (cel->enable == 0 && tm_on && wildcard) {
1036 		if (are_any_luns_enabled(isp, bus)) {
1037 			ccb->ccb_h.status = CAM_SCSI_BUSY;
1038 			return (-1);
1039 		}
1040 		av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1041 		if (av) {
1042 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1043 			return (-1);
1044 		}
1045 		isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1046 		xpt_print(ccb->ccb_h.path, "Target Mode Disabled\n");
1047 	}
1048 
1049 	if (wildcard) {
1050 		ccb->ccb_h.status = CAM_REQ_CMP;
1051 		return (-1);
1052 	}
1053 
1054 	/*
1055 	 * Find an empty slot
1056 	 */
1057 	for (seq = 0; seq < NLEACT; seq++) {
1058 		if (isp->isp_osinfo.leact[seq] == 0) {
1059 			break;
1060 		}
1061 	}
1062 	if (seq >= NLEACT) {
1063 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1064 		return (-1);
1065 
1066 	}
1067 	isp->isp_osinfo.leact[seq] = ccb;
1068 
1069 	if (cel->enable) {
1070 		ccb->ccb_h.status =
1071 		    create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1072 		if (ccb->ccb_h.status != CAM_REQ_CMP) {
1073 			isp->isp_osinfo.leact[seq] = 0;
1074 			return (-1);
1075 		}
1076 	} else {
1077 		tptr = get_lun_statep(isp, bus, lun);
1078 		if (tptr == NULL) {
1079 			ccb->ccb_h.status = CAM_LUN_INVALID;
1080 			return (-1);
1081 		}
1082 	}
1083 
1084 	if (cel->enable) {
1085 		int c, n, ulun = lun;
1086 
1087 		cmd = RQSTYPE_ENABLE_LUN;
1088 		c = DFLT_CMND_CNT;
1089 		n = DFLT_INOT_CNT;
1090 		if (IS_FC(isp) && lun != 0) {
1091 			cmd = RQSTYPE_MODIFY_LUN;
1092 			n = 0;
1093 			/*
1094 		 	 * For SCC firmware, we only deal with setting
1095 			 * (enabling or modifying) lun 0.
1096 			 */
1097 			ulun = 0;
1098 		}
1099 		if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1100 			rls_lun_statep(isp, tptr);
1101 			ccb->ccb_h.status = CAM_REQ_INPROG;
1102 			return (seq);
1103 		}
1104 	} else {
1105 		int c, n, ulun = lun;
1106 
1107 		cmd = -RQSTYPE_MODIFY_LUN;
1108 		c = DFLT_CMND_CNT;
1109 		n = DFLT_INOT_CNT;
1110 		if (IS_FC(isp) && lun != 0) {
1111 			n = 0;
1112 			/*
1113 		 	 * For SCC firmware, we only deal with setting
1114 			 * (enabling or modifying) lun 0.
1115 			 */
1116 			ulun = 0;
1117 		}
1118 		if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1119 			rls_lun_statep(isp, tptr);
1120 			ccb->ccb_h.status = CAM_REQ_INPROG;
1121 			return (seq);
1122 		}
1123 	}
1124 	rls_lun_statep(isp, tptr);
1125 	xpt_print(ccb->ccb_h.path, "isp_lun_cmd failed\n");
1126 	isp->isp_osinfo.leact[seq] = 0;
1127 	ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1128 	return (-1);
1129 }
1130 
1131 static void
1132 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1133 {
1134 	const char lfmt[] = "now %sabled for target mode";
1135 	union ccb *ccb;
1136 	uint32_t seq;
1137 	tstate_t *tptr;
1138 	int av;
1139 	struct ccb_en_lun *cel;
1140 
1141 	seq = lep->le_reserved - 1;
1142 	if (seq >= NLEACT) {
1143 		isp_prt(isp, ISP_LOGERR,
1144 		    "seq out of range (%u) in isp_ledone", seq);
1145 		return;
1146 	}
1147 	ccb = isp->isp_osinfo.leact[seq];
1148 	if (ccb == 0) {
1149 		isp_prt(isp, ISP_LOGERR,
1150 		    "no ccb for seq %u in isp_ledone", seq);
1151 		return;
1152 	}
1153 	cel = &ccb->cel;
1154 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1155 	if (tptr == NULL) {
1156 		xpt_print(ccb->ccb_h.path, "null tptr in isp_ledone\n");
1157 		isp->isp_osinfo.leact[seq] = 0;
1158 		return;
1159 	}
1160 
1161 	if (lep->le_status != LUN_OK) {
1162 		xpt_print(ccb->ccb_h.path,
1163 		    "ENABLE/MODIFY LUN returned 0x%x\n", lep->le_status);
1164 err:
1165 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1166 		rls_lun_statep(isp, tptr);
1167 		isp->isp_osinfo.leact[seq] = 0;
1168 		ISPLOCK_2_CAMLOCK(isp);
1169 		xpt_done(ccb);
1170 		CAMLOCK_2_ISPLOCK(isp);
1171 		return;
1172 	} else {
1173 		isp_prt(isp, ISP_LOGTDEBUG0,
1174 		    "isp_ledone: ENABLE/MODIFY done okay");
1175 	}
1176 
1177 
1178 	if (cel->enable) {
1179 		ccb->ccb_h.status = CAM_REQ_CMP;
1180 		xpt_print(ccb->ccb_h.path, lfmt, "en");
1181 		rls_lun_statep(isp, tptr);
1182 		isp->isp_osinfo.leact[seq] = 0;
1183 		ISPLOCK_2_CAMLOCK(isp);
1184 		xpt_done(ccb);
1185 		CAMLOCK_2_ISPLOCK(isp);
1186 		return;
1187 	}
1188 
1189 	if (lep->le_header.rqs_entry_type == RQSTYPE_MODIFY_LUN) {
1190 		if (isp_lun_cmd(isp, -RQSTYPE_ENABLE_LUN, XS_CHANNEL(ccb),
1191 		    XS_TGT(ccb), XS_LUN(ccb), 0, 0, seq+1)) {
1192 			xpt_print(ccb->ccb_h.path,
1193 			    "isp_ledone: isp_lun_cmd failed\n");
1194 			goto err;
1195 		}
1196 		rls_lun_statep(isp, tptr);
1197 		return;
1198 	}
1199 
1200 	xpt_print(ccb->ccb_h.path, lfmt, "dis");
1201 	rls_lun_statep(isp, tptr);
1202 	destroy_lun_state(isp, tptr);
1203 	ccb->ccb_h.status = CAM_REQ_CMP;
1204 	isp->isp_osinfo.leact[seq] = 0;
1205 	ISPLOCK_2_CAMLOCK(isp);
1206 	xpt_done(ccb);
1207 	CAMLOCK_2_ISPLOCK(isp);
1208 	if (are_any_luns_enabled(isp, XS_CHANNEL(ccb)) == 0) {
1209 		int bus = XS_CHANNEL(ccb);
1210 		av = bus << 31;
1211 		av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1212 		if (av) {
1213 			isp_prt(isp, ISP_LOGWARN,
1214 			    "disable target mode on channel %d failed", bus);
1215 		}
1216 		isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1217 	}
1218 }
1219 
1220 
1221 static cam_status
1222 isp_abort_tgt_ccb(ispsoftc_t *isp, union ccb *ccb)
1223 {
1224 	tstate_t *tptr;
1225 	struct ccb_hdr_slist *lp;
1226 	struct ccb_hdr *curelm;
1227 	int found, *ctr;
1228 	union ccb *accb = ccb->cab.abort_ccb;
1229 
1230 	xpt_print(ccb->ccb_h.path, "aborting ccb %p\n", accb);
1231 	if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1232 		int badpath = 0;
1233 		if (IS_FC(isp) && (accb->ccb_h.target_id !=
1234 		    ((fcparam *) isp->isp_param)->isp_loopid)) {
1235 			badpath = 1;
1236 		} else if (IS_SCSI(isp) && (accb->ccb_h.target_id !=
1237 		    ((sdparam *) isp->isp_param)->isp_initiator_id)) {
1238 			badpath = 1;
1239 		}
1240 		if (badpath) {
1241 			/*
1242 			 * Being restrictive about target ids is really about
1243 			 * making sure we're aborting for the right multi-tid
1244 			 * path. This doesn't really make much sense at present.
1245 			 */
1246 #if	0
1247 			return (CAM_PATH_INVALID);
1248 #endif
1249 		}
1250 	}
1251 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
1252 	if (tptr == NULL) {
1253 		xpt_print(ccb->ccb_h.path, "can't get statep\n");
1254 		return (CAM_PATH_INVALID);
1255 	}
1256 	if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1257 		lp = &tptr->atios;
1258 		ctr = &tptr->atio_count;
1259 	} else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
1260 		lp = &tptr->inots;
1261 		ctr = &tptr->inot_count;
1262 	} else {
1263 		rls_lun_statep(isp, tptr);
1264 		xpt_print(ccb->ccb_h.path, "bad function code %d\n",
1265 		    accb->ccb_h.func_code);
1266 		return (CAM_UA_ABORT);
1267 	}
1268 	curelm = SLIST_FIRST(lp);
1269 	found = 0;
1270 	if (curelm == &accb->ccb_h) {
1271 		found = 1;
1272 		SLIST_REMOVE_HEAD(lp, sim_links.sle);
1273 	} else {
1274 		while(curelm != NULL) {
1275 			struct ccb_hdr *nextelm;
1276 
1277 			nextelm = SLIST_NEXT(curelm, sim_links.sle);
1278 			if (nextelm == &accb->ccb_h) {
1279 				found = 1;
1280 				SLIST_NEXT(curelm, sim_links.sle) =
1281 				    SLIST_NEXT(nextelm, sim_links.sle);
1282 				break;
1283 			}
1284 			curelm = nextelm;
1285 		}
1286 	}
1287 	rls_lun_statep(isp, tptr);
1288 	if (found) {
1289 		(*ctr)--;
1290 		accb->ccb_h.status = CAM_REQ_ABORTED;
1291 		xpt_done(accb);
1292 		return (CAM_REQ_CMP);
1293 	}
1294 	xpt_print(ccb->ccb_h.path, "ccb %p not found\n", accb);
1295 	return (CAM_PATH_INVALID);
1296 }
1297 
1298 static void
1299 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1300 {
1301 	void *qe;
1302 	struct ccb_scsiio *cso = &ccb->csio;
1303 	uint32_t nxti, optr, handle;
1304 	uint8_t local[QENTRY_LEN];
1305 
1306 
1307 	if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1308 		xpt_print(ccb->ccb_h.path,
1309 		    "Request Queue Overflow in isp_target_start_ctio\n");
1310 		XS_SETERR(ccb, CAM_REQUEUE_REQ);
1311 		goto out;
1312 	}
1313 	memset(local, 0, QENTRY_LEN);
1314 
1315 	/*
1316 	 * We're either moving data or completing a command here.
1317 	 */
1318 
1319 	if (IS_FC(isp)) {
1320 		atio_private_data_t *atp;
1321 		ct2_entry_t *cto = (ct2_entry_t *) local;
1322 
1323 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1324 		cto->ct_header.rqs_entry_count = 1;
1325 		if (FCPARAM(isp)->isp_2klogin) {
1326 			((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1327 		} else {
1328 			cto->ct_iid = cso->init_id;
1329 			if (FCPARAM(isp)->isp_sccfw == 0) {
1330 				cto->ct_lun = ccb->ccb_h.target_lun;
1331 			}
1332 		}
1333 
1334 		atp = isp_get_atpd(isp, cso->tag_id);
1335 		if (atp == NULL) {
1336 			xpt_print(ccb->ccb_h.path,
1337 			    "cannot find private data adjunct for tag %x\n",
1338 			    cso->tag_id);
1339 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
1340 			goto out;
1341 		}
1342 
1343 		cto->ct_rxid = cso->tag_id;
1344 		if (cso->dxfer_len == 0) {
1345 			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
1346 			if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1347 				cto->ct_flags |= CT2_SENDSTATUS;
1348 				cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1349 				cto->ct_resid =
1350 				    atp->orig_datalen - atp->bytes_xfered;
1351 				if (cto->ct_resid < 0) {
1352 					cto->rsp.m1.ct_scsi_status |=
1353 					    CT2_DATA_OVER;
1354 				} else if (cto->ct_resid > 0) {
1355 					cto->rsp.m1.ct_scsi_status |=
1356 					    CT2_DATA_UNDER;
1357 				}
1358 			}
1359 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1360 				int m = min(cso->sense_len, MAXRESPLEN);
1361 				memcpy(cto->rsp.m1.ct_resp,
1362 				    &cso->sense_data, m);
1363 				cto->rsp.m1.ct_senselen = m;
1364 				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1365 			}
1366 		} else {
1367 			cto->ct_flags |= CT2_FLAG_MODE0;
1368 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1369 				cto->ct_flags |= CT2_DATA_IN;
1370 			} else {
1371 				cto->ct_flags |= CT2_DATA_OUT;
1372 			}
1373 			cto->ct_reloff = atp->bytes_xfered;
1374 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1375 				cto->ct_flags |= CT2_SENDSTATUS;
1376 				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1377 				cto->ct_resid =
1378 				    atp->orig_datalen -
1379 				    (atp->bytes_xfered + cso->dxfer_len);
1380 				if (cto->ct_resid < 0) {
1381 					cto->rsp.m0.ct_scsi_status |=
1382 					    CT2_DATA_OVER;
1383 				} else if (cto->ct_resid > 0) {
1384 					cto->rsp.m0.ct_scsi_status |=
1385 					    CT2_DATA_UNDER;
1386 				}
1387 			} else {
1388 				atp->last_xframt = cso->dxfer_len;
1389 			}
1390 			/*
1391 			 * If we're sending data and status back together,
1392 			 * we can't also send back sense data as well.
1393 			 */
1394 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1395 		}
1396 
1397 		if (cto->ct_flags & CT2_SENDSTATUS) {
1398 			isp_prt(isp, ISP_LOGTDEBUG0,
1399 			    "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1400 			    cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1401 			    cso->dxfer_len, cto->ct_resid);
1402 			cto->ct_flags |= CT2_CCINCR;
1403 			atp->state = ATPD_STATE_LAST_CTIO;
1404 		} else {
1405 			atp->state = ATPD_STATE_CTIO;
1406 		}
1407 		cto->ct_timeout = 10;
1408 	} else {
1409 		ct_entry_t *cto = (ct_entry_t *) local;
1410 
1411 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1412 		cto->ct_header.rqs_entry_count = 1;
1413 		cto->ct_iid = cso->init_id;
1414 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1415 		cto->ct_tgt = ccb->ccb_h.target_id;
1416 		cto->ct_lun = ccb->ccb_h.target_lun;
1417 		cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1418 		if (AT_HAS_TAG(cso->tag_id)) {
1419 			cto->ct_tag_val = (uint8_t) AT_GET_TAG(cso->tag_id);
1420 			cto->ct_flags |= CT_TQAE;
1421 		}
1422 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1423 			cto->ct_flags |= CT_NODISC;
1424 		}
1425 		if (cso->dxfer_len == 0) {
1426 			cto->ct_flags |= CT_NO_DATA;
1427 		} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1428 			cto->ct_flags |= CT_DATA_IN;
1429 		} else {
1430 			cto->ct_flags |= CT_DATA_OUT;
1431 		}
1432 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1433 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1434 			cto->ct_scsi_status = cso->scsi_status;
1435 			cto->ct_resid = cso->resid;
1436 			isp_prt(isp, ISP_LOGTDEBUG0,
1437 			    "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1438 			    cto->ct_fwhandle, cso->scsi_status, cso->resid,
1439 			    cso->tag_id);
1440 		}
1441 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1442 		cto->ct_timeout = 10;
1443 	}
1444 
1445 	if (isp_save_xs_tgt(isp, ccb, &handle)) {
1446 		xpt_print(ccb->ccb_h.path,
1447 		    "No XFLIST pointers for isp_target_start_ctio\n");
1448 		XS_SETERR(ccb, CAM_REQUEUE_REQ);
1449 		goto out;
1450 	}
1451 
1452 
1453 	/*
1454 	 * Call the dma setup routines for this entry (and any subsequent
1455 	 * CTIOs) if there's data to move, and then tell the f/w it's got
1456 	 * new things to play with. As with isp_start's usage of DMA setup,
1457 	 * any swizzling is done in the machine dependent layer. Because
1458 	 * of this, we put the request onto the queue area first in native
1459 	 * format.
1460 	 */
1461 
1462 	if (IS_FC(isp)) {
1463 		ct2_entry_t *cto = (ct2_entry_t *) local;
1464 		cto->ct_syshandle = handle;
1465 	} else {
1466 		ct_entry_t *cto = (ct_entry_t *) local;
1467 		cto->ct_syshandle = handle;
1468 	}
1469 
1470 	switch (ISP_DMASETUP(isp, cso, (ispreq_t *) local, &nxti, optr)) {
1471 	case CMD_QUEUED:
1472 		ISP_ADD_REQUEST(isp, nxti);
1473 		ccb->ccb_h.status |= CAM_SIM_QUEUED;
1474 		return;
1475 
1476 	case CMD_EAGAIN:
1477 		XS_SETERR(ccb, CAM_REQUEUE_REQ);
1478 		break;
1479 
1480 	default:
1481 		break;
1482 	}
1483 	isp_destroy_tgt_handle(isp, handle);
1484 
1485 out:
1486 	ISPLOCK_2_CAMLOCK(isp);
1487 	xpt_done(ccb);
1488 	CAMLOCK_2_ISPLOCK(isp);
1489 }
1490 
1491 static void
1492 isp_refire_putback_atio(void *arg)
1493 {
1494 	int s = splcam();
1495 	isp_target_putback_atio(arg);
1496 	splx(s);
1497 }
1498 
1499 static void
1500 isp_target_putback_atio(union ccb *ccb)
1501 {
1502 	ispsoftc_t *isp;
1503 	struct ccb_scsiio *cso;
1504 	uint32_t nxti, optr;
1505 	void *qe;
1506 
1507 	isp = XS_ISP(ccb);
1508 
1509 	if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1510 		xpt_print(ccb->ccb_h.path,
1511 		    "isp_target_putback_atio: Request Queue Overflow\n");
1512 		(void) timeout(isp_refire_putback_atio, ccb, 10);
1513 		return;
1514 	}
1515 	memset(qe, 0, QENTRY_LEN);
1516 	cso = &ccb->csio;
1517 	if (IS_FC(isp)) {
1518 		at2_entry_t local, *at = &local;
1519 		MEMZERO(at, sizeof (at2_entry_t));
1520 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1521 		at->at_header.rqs_entry_count = 1;
1522 		if (FCPARAM(isp)->isp_sccfw) {
1523 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1524 		} else {
1525 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1526 		}
1527 		at->at_status = CT_OK;
1528 		at->at_rxid = cso->tag_id;
1529 		at->at_iid = cso->ccb_h.target_id;
1530 		isp_put_atio2(isp, at, qe);
1531 	} else {
1532 		at_entry_t local, *at = &local;
1533 		MEMZERO(at, sizeof (at_entry_t));
1534 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1535 		at->at_header.rqs_entry_count = 1;
1536 		at->at_iid = cso->init_id;
1537 		at->at_iid |= XS_CHANNEL(ccb) << 7;
1538 		at->at_tgt = cso->ccb_h.target_id;
1539 		at->at_lun = cso->ccb_h.target_lun;
1540 		at->at_status = CT_OK;
1541 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
1542 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
1543 		isp_put_atio(isp, at, qe);
1544 	}
1545 	ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1546 	ISP_ADD_REQUEST(isp, nxti);
1547 	isp_complete_ctio(ccb);
1548 }
1549 
1550 static void
1551 isp_complete_ctio(union ccb *ccb)
1552 {
1553 	ISPLOCK_2_CAMLOCK(isp);
1554 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1555 		ccb->ccb_h.status |= CAM_REQ_CMP;
1556 	}
1557 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1558 	xpt_done(ccb);
1559 	CAMLOCK_2_ISPLOCK(isp);
1560 }
1561 
1562 /*
1563  * Handle ATIO stuff that the generic code can't.
1564  * This means handling CDBs.
1565  */
1566 
1567 static int
1568 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1569 {
1570 	tstate_t *tptr;
1571 	int status, bus, iswildcard;
1572 	struct ccb_accept_tio *atiop;
1573 
1574 	/*
1575 	 * The firmware status (except for the QLTM_SVALID bit)
1576 	 * indicates why this ATIO was sent to us.
1577 	 *
1578 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1579 	 *
1580 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1581 	 * we're still connected on the SCSI bus.
1582 	 */
1583 	status = aep->at_status;
1584 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1585 		/*
1586 		 * Bus Phase Sequence error. We should have sense data
1587 		 * suggested by the f/w. I'm not sure quite yet what
1588 		 * to do about this for CAM.
1589 		 */
1590 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1591 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1592 		return (0);
1593 	}
1594 	if ((status & ~QLTM_SVALID) != AT_CDB) {
1595 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1596 		    status);
1597 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1598 		return (0);
1599 	}
1600 
1601 	bus = GET_BUS_VAL(aep->at_iid);
1602 	tptr = get_lun_statep(isp, bus, aep->at_lun);
1603 	if (tptr == NULL) {
1604 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1605 		if (tptr == NULL) {
1606 			/*
1607 			 * Because we can't autofeed sense data back with
1608 			 * a command for parallel SCSI, we can't give back
1609 			 * a CHECK CONDITION. We'll give back a BUSY status
1610 			 * instead. This works out okay because the only
1611 			 * time we should, in fact, get this, is in the
1612 			 * case that somebody configured us without the
1613 			 * blackhole driver, so they get what they deserve.
1614 			 */
1615 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1616 			return (0);
1617 		}
1618 		iswildcard = 1;
1619 	} else {
1620 		iswildcard = 0;
1621 	}
1622 
1623 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1624 	if (atiop == NULL) {
1625 		/*
1626 		 * Because we can't autofeed sense data back with
1627 		 * a command for parallel SCSI, we can't give back
1628 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1629 		 * instead. This works out okay because the only time we
1630 		 * should, in fact, get this, is in the case that we've
1631 		 * run out of ATIOS.
1632 		 */
1633 		xpt_print(tptr->owner,
1634 		    "no ATIOS for lun %d from initiator %d on channel %d\n",
1635 		    aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1636 		if (aep->at_flags & AT_TQAE)
1637 			isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1638 		else
1639 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1640 		rls_lun_statep(isp, tptr);
1641 		return (0);
1642 	}
1643 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1644 	tptr->atio_count--;
1645 	isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1646 	    aep->at_lun, tptr->atio_count);
1647 	if (iswildcard) {
1648 		atiop->ccb_h.target_id = aep->at_tgt;
1649 		atiop->ccb_h.target_lun = aep->at_lun;
1650 	}
1651 	if (aep->at_flags & AT_NODISC) {
1652 		atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1653 	} else {
1654 		atiop->ccb_h.flags = 0;
1655 	}
1656 
1657 	if (status & QLTM_SVALID) {
1658 		size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1659 		atiop->sense_len = amt;
1660 		MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1661 	} else {
1662 		atiop->sense_len = 0;
1663 	}
1664 
1665 	atiop->init_id = GET_IID_VAL(aep->at_iid);
1666 	atiop->cdb_len = aep->at_cdblen;
1667 	MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1668 	atiop->ccb_h.status = CAM_CDB_RECVD;
1669 	/*
1670 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
1671 	 * and the handle (which we have to preserve).
1672 	 */
1673 	AT_MAKE_TAGID(atiop->tag_id,  device_get_unit(isp->isp_dev), aep);
1674 	if (aep->at_flags & AT_TQAE) {
1675 		atiop->tag_action = aep->at_tag_type;
1676 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1677 	}
1678 	xpt_done((union ccb*)atiop);
1679 	isp_prt(isp, ISP_LOGTDEBUG0,
1680 	    "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1681 	    aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1682 	    GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1683 	    aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1684 	    "nondisc" : "disconnecting");
1685 	rls_lun_statep(isp, tptr);
1686 	return (0);
1687 }
1688 
1689 static int
1690 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1691 {
1692 	lun_id_t lun;
1693 	tstate_t *tptr;
1694 	struct ccb_accept_tio *atiop;
1695 	atio_private_data_t *atp;
1696 
1697 	/*
1698 	 * The firmware status (except for the QLTM_SVALID bit)
1699 	 * indicates why this ATIO was sent to us.
1700 	 *
1701 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1702 	 */
1703 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1704 		isp_prt(isp, ISP_LOGWARN,
1705 		    "bogus atio (0x%x) leaked to platform", aep->at_status);
1706 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1707 		return (0);
1708 	}
1709 
1710 	if (FCPARAM(isp)->isp_sccfw) {
1711 		lun = aep->at_scclun;
1712 	} else {
1713 		lun = aep->at_lun;
1714 	}
1715 	tptr = get_lun_statep(isp, 0, lun);
1716 	if (tptr == NULL) {
1717 		isp_prt(isp, ISP_LOGTDEBUG0,
1718 		    "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1719 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1720 		if (tptr == NULL) {
1721 			isp_endcmd(isp, aep,
1722 			    SCSI_STATUS_CHECK_COND | ECMD_SVALID |
1723 			    (0x5 << 12) | (0x25 << 16), 0);
1724 			return (0);
1725 		}
1726 	}
1727 
1728 	atp = isp_get_atpd(isp, 0);
1729 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1730 	if (atiop == NULL || atp == NULL) {
1731 
1732 		/*
1733 		 * Because we can't autofeed sense data back with
1734 		 * a command for parallel SCSI, we can't give back
1735 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1736 		 * instead. This works out okay because the only time we
1737 		 * should, in fact, get this, is in the case that we've
1738 		 * run out of ATIOS.
1739 		 */
1740 		xpt_print(tptr->owner,
1741 		    "no %s for lun %d from initiator %d\n",
1742 		    (atp == NULL && atiop == NULL)? "ATIO2s *or* ATPS" :
1743 		    ((atp == NULL)? "ATPs" : "ATIO2s"), lun, aep->at_iid);
1744 		rls_lun_statep(isp, tptr);
1745 		isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1746 		return (0);
1747 	}
1748 	atp->state = ATPD_STATE_ATIO;
1749 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1750 	tptr->atio_count--;
1751 	isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1752 	    lun, tptr->atio_count);
1753 
1754 	if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1755 		atiop->ccb_h.target_id = FCPARAM(isp)->isp_loopid;
1756 		atiop->ccb_h.target_lun = lun;
1757 	}
1758 	/*
1759 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1760 	 */
1761 	atiop->sense_len = 0;
1762 
1763 	atiop->init_id = aep->at_iid;
1764 	atiop->cdb_len = ATIO2_CDBLEN;
1765 	MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1766 	atiop->ccb_h.status = CAM_CDB_RECVD;
1767 	atiop->tag_id = aep->at_rxid;
1768 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1769 	case ATIO2_TC_ATTR_SIMPLEQ:
1770 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1771 		break;
1772         case ATIO2_TC_ATTR_HEADOFQ:
1773 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1774 		break;
1775         case ATIO2_TC_ATTR_ORDERED:
1776 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1777 		break;
1778         case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1779 	case ATIO2_TC_ATTR_UNTAGGED:
1780 	default:
1781 		atiop->tag_action = 0;
1782 		break;
1783 	}
1784 	atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
1785 
1786 	atp->tag = atiop->tag_id;
1787 	atp->lun = lun;
1788 	atp->orig_datalen = aep->at_datalen;
1789 	atp->last_xframt = 0;
1790 	atp->bytes_xfered = 0;
1791 	atp->state = ATPD_STATE_CAM;
1792 	ISPLOCK_2_CAMLOCK(siP);
1793 	xpt_done((union ccb*)atiop);
1794 
1795 	isp_prt(isp, ISP_LOGTDEBUG0,
1796 	    "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1797 	    aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1798 	    lun, aep->at_taskflags, aep->at_datalen);
1799 	rls_lun_statep(isp, tptr);
1800 	return (0);
1801 }
1802 
1803 static int
1804 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
1805 {
1806 	union ccb *ccb;
1807 	int sentstatus, ok, notify_cam, resid = 0;
1808 	uint16_t tval;
1809 
1810 	/*
1811 	 * CTIO and CTIO2 are close enough....
1812 	 */
1813 
1814 	ccb = isp_find_xs_tgt(isp, ((ct_entry_t *)arg)->ct_syshandle);
1815 	KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1816 	isp_destroy_tgt_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1817 
1818 	if (IS_FC(isp)) {
1819 		ct2_entry_t *ct = arg;
1820 		atio_private_data_t *atp = isp_get_atpd(isp, ct->ct_rxid);
1821 		if (atp == NULL) {
1822 			isp_prt(isp, ISP_LOGERR,
1823 			    "cannot find adjunct for %x after I/O",
1824 			    ct->ct_rxid);
1825 			return (0);
1826 		}
1827 		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1828 		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1829 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1830 			ccb->ccb_h.status |= CAM_SENT_SENSE;
1831 		}
1832 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
1833 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1834 			resid = ct->ct_resid;
1835 			atp->bytes_xfered += (atp->last_xframt - resid);
1836 			atp->last_xframt = 0;
1837 		}
1838 		if (sentstatus || !ok) {
1839 			atp->tag = 0;
1840 		}
1841 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN,
1842 		    "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1843 		    ct->ct_rxid, ct->ct_status, ct->ct_flags,
1844 		    (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1845 		    resid, sentstatus? "FIN" : "MID");
1846 		tval = ct->ct_rxid;
1847 
1848 		/* XXX: should really come after isp_complete_ctio */
1849 		atp->state = ATPD_STATE_PDON;
1850 	} else {
1851 		ct_entry_t *ct = arg;
1852 		sentstatus = ct->ct_flags & CT_SENDSTATUS;
1853 		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
1854 		/*
1855 		 * We *ought* to be able to get back to the original ATIO
1856 		 * here, but for some reason this gets lost. It's just as
1857 		 * well because it's squirrelled away as part of periph
1858 		 * private data.
1859 		 *
1860 		 * We can live without it as long as we continue to use
1861 		 * the auto-replenish feature for CTIOs.
1862 		 */
1863 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
1864 		if (ct->ct_status & QLTM_SVALID) {
1865 			char *sp = (char *)ct;
1866 			sp += CTIO_SENSE_OFFSET;
1867 			ccb->csio.sense_len =
1868 			    min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1869 			MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1870 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1871 		}
1872 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1873 			resid = ct->ct_resid;
1874 		}
1875 		isp_prt(isp, ISP_LOGTDEBUG0,
1876 		    "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1877 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1878 		    ct->ct_status, ct->ct_flags, resid,
1879 		    sentstatus? "FIN" : "MID");
1880 		tval = ct->ct_fwhandle;
1881 	}
1882 	ccb->csio.resid += resid;
1883 
1884 	/*
1885 	 * We're here either because intermediate data transfers are done
1886 	 * and/or the final status CTIO (which may have joined with a
1887 	 * Data Transfer) is done.
1888 	 *
1889 	 * In any case, for this platform, the upper layers figure out
1890 	 * what to do next, so all we do here is collect status and
1891 	 * pass information along. Any DMA handles have already been
1892 	 * freed.
1893 	 */
1894 	if (notify_cam == 0) {
1895 		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
1896 		return (0);
1897 	}
1898 
1899 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1900 	    (sentstatus)? "  FINAL " : "MIDTERM ", tval);
1901 
1902 	if (!ok) {
1903 		isp_target_putback_atio(ccb);
1904 	} else {
1905 		isp_complete_ctio(ccb);
1906 
1907 	}
1908 	return (0);
1909 }
1910 
1911 static int
1912 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inp)
1913 {
1914 	return (0);	/* XXXX */
1915 }
1916 
1917 static int
1918 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
1919 {
1920 
1921 	switch (inp->in_status) {
1922 	case IN_PORT_LOGOUT:
1923 		isp_prt(isp, ISP_LOGWARN, "port logout of iid %d",
1924 		   inp->in_iid);
1925 		break;
1926 	case IN_PORT_CHANGED:
1927 		isp_prt(isp, ISP_LOGWARN, "port changed for iid %d",
1928 		   inp->in_iid);
1929 		break;
1930 	case IN_GLOBAL_LOGO:
1931 		isp_prt(isp, ISP_LOGINFO, "all ports logged out");
1932 		break;
1933 	case IN_ABORT_TASK:
1934 	{
1935 		atio_private_data_t *atp = isp_get_atpd(isp, inp->in_seqid);
1936 		struct ccb_immed_notify *inot = NULL;
1937 
1938 		if (atp) {
1939 			tstate_t *tptr = get_lun_statep(isp, 0, atp->lun);
1940 			if (tptr) {
1941 				inot = (struct ccb_immed_notify *)
1942 				    SLIST_FIRST(&tptr->inots);
1943 				if (inot) {
1944 					tptr->inot_count--;
1945 					SLIST_REMOVE_HEAD(&tptr->inots,
1946 					    sim_links.sle);
1947 					isp_prt(isp, ISP_LOGTDEBUG0,
1948 					    "Take FREE INOT count now %d",
1949 					    tptr->inot_count);
1950 				}
1951 			}
1952 			isp_prt(isp, ISP_LOGWARN,
1953 			   "abort task RX_ID %x IID %d state %d",
1954 			   inp->in_seqid, inp->in_iid, atp->state);
1955 		} else {
1956 			isp_prt(isp, ISP_LOGWARN,
1957 			   "abort task RX_ID %x from iid %d, state unknown",
1958 			   inp->in_seqid, inp->in_iid);
1959 		}
1960 		if (inot) {
1961 			inot->initiator_id = inp->in_iid;
1962 			inot->sense_len = 0;
1963 			inot->message_args[0] = MSG_ABORT_TAG;
1964 			inot->message_args[1] = inp->in_seqid & 0xff;
1965 			inot->message_args[2] = (inp->in_seqid >> 8) & 0xff;
1966 			inot->ccb_h.status = CAM_MESSAGE_RECV;
1967 			xpt_done((union ccb *)inot);
1968 		}
1969 		break;
1970 	}
1971 	default:
1972 		break;
1973 	}
1974 	return (0);
1975 }
1976 #endif
1977 
1978 static void
1979 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
1980 {
1981 	struct cam_sim *sim;
1982 	ispsoftc_t *isp;
1983 
1984 	sim = (struct cam_sim *)cbarg;
1985 	isp = (ispsoftc_t *) cam_sim_softc(sim);
1986 	switch (code) {
1987 	case AC_LOST_DEVICE:
1988 		if (IS_SCSI(isp)) {
1989 			uint16_t oflags, nflags;
1990 			sdparam *sdp = isp->isp_param;
1991 			int tgt;
1992 
1993 			tgt = xpt_path_target_id(path);
1994 			if (tgt >= 0) {
1995 				sdp += cam_sim_bus(sim);
1996 				ISP_LOCK(isp);
1997 				nflags = sdp->isp_devparam[tgt].nvrm_flags;
1998 #ifndef	ISP_TARGET_MODE
1999 				nflags &= DPARM_SAFE_DFLT;
2000 				if (isp->isp_loaded_fw) {
2001 					nflags |= DPARM_NARROW | DPARM_ASYNC;
2002 				}
2003 #else
2004 				nflags = DPARM_DEFAULT;
2005 #endif
2006 				oflags = sdp->isp_devparam[tgt].goal_flags;
2007 				sdp->isp_devparam[tgt].goal_flags = nflags;
2008 				sdp->isp_devparam[tgt].dev_update = 1;
2009 				isp->isp_update |= (1 << cam_sim_bus(sim));
2010 				(void) isp_control(isp,
2011 				    ISPCTL_UPDATE_PARAMS, NULL);
2012 				sdp->isp_devparam[tgt].goal_flags = oflags;
2013 				ISP_UNLOCK(isp);
2014 			}
2015 		}
2016 		break;
2017 	default:
2018 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2019 		break;
2020 	}
2021 }
2022 
2023 static void
2024 isp_poll(struct cam_sim *sim)
2025 {
2026 	ispsoftc_t *isp = cam_sim_softc(sim);
2027 	uint32_t isr;
2028 	uint16_t sema, mbox;
2029 
2030 	ISP_LOCK(isp);
2031 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2032 		isp_intr(isp, isr, sema, mbox);
2033 	}
2034 	ISP_UNLOCK(isp);
2035 }
2036 
2037 
2038 static int isp_watchdog_work(ispsoftc_t *, XS_T *);
2039 
2040 static int
2041 isp_watchdog_work(ispsoftc_t *isp, XS_T *xs)
2042 {
2043 	uint32_t handle;
2044 
2045 	/*
2046 	 * We've decided this command is dead. Make sure we're not trying
2047 	 * to kill a command that's already dead by getting it's handle and
2048 	 * and seeing whether it's still alive.
2049 	 */
2050 	ISP_LOCK(isp);
2051 	handle = isp_find_handle(isp, xs);
2052 	if (handle) {
2053 		uint32_t isr;
2054 		uint16_t sema, mbox;
2055 
2056 		if (XS_CMD_DONE_P(xs)) {
2057 			isp_prt(isp, ISP_LOGDEBUG1,
2058 			    "watchdog found done cmd (handle 0x%x)", handle);
2059 			ISP_UNLOCK(isp);
2060 			return (1);;
2061 		}
2062 
2063 		if (XS_CMD_WDOG_P(xs)) {
2064 			isp_prt(isp, ISP_LOGDEBUG2,
2065 			    "recursive watchdog (handle 0x%x)", handle);
2066 			ISP_UNLOCK(isp);
2067 			return (1);
2068 		}
2069 
2070 		XS_CMD_S_WDOG(xs);
2071 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2072 			isp_intr(isp, isr, sema, mbox);
2073 		}
2074 		if (XS_CMD_DONE_P(xs)) {
2075 			isp_prt(isp, ISP_LOGDEBUG2,
2076 			    "watchdog cleanup for handle 0x%x", handle);
2077 			ISPLOCK_2_CAMLOCK(isp);
2078 			xpt_done((union ccb *) xs);
2079 			CAMLOCK_2_ISPLOCK(isp);
2080 		} else if (XS_CMD_GRACE_P(xs)) {
2081 			/*
2082 			 * Make sure the command is *really* dead before we
2083 			 * release the handle (and DMA resources) for reuse.
2084 			 */
2085 			(void) isp_control(isp, ISPCTL_ABORT_CMD, xs);
2086 
2087 			/*
2088 			 * After this point, the comamnd is really dead.
2089 			 */
2090 			if (XS_XFRLEN(xs)) {
2091 				ISP_DMAFREE(isp, xs, handle);
2092                 	}
2093 			isp_destroy_handle(isp, handle);
2094 			xpt_print(xs->ccb_h.path,
2095 			    "watchdog timeout for handle 0x%x\n", handle);
2096 			XS_SETERR(xs, CAM_CMD_TIMEOUT);
2097 			XS_CMD_C_WDOG(xs);
2098 			ISPLOCK_2_CAMLOCK(isp);
2099 			isp_done(xs);
2100 			CAMLOCK_2_ISPLOCK(isp);
2101 		} else {
2102 			XS_CMD_C_WDOG(xs);
2103 			xs->ccb_h.timeout_ch = timeout(isp_watchdog, xs, hz);
2104 			XS_CMD_S_GRACE(xs);
2105 			isp->isp_sendmarker |= 1 << XS_CHANNEL(xs);
2106 		}
2107 		ISP_UNLOCK(isp);
2108 		return (1);
2109 	}
2110 	ISP_UNLOCK(isp);
2111 	return (0);
2112 }
2113 
2114 static void
2115 isp_watchdog(void *arg)
2116 {
2117 	ispsoftc_t *isp;
2118 	XS_T *xs = arg;
2119 	for (isp = isplist; isp != NULL; isp = isp->isp_osinfo.next) {
2120 		if (isp_watchdog_work(isp, xs)) {
2121 			break;
2122 		}
2123 	}
2124 	if (isp == NULL) {
2125 		printf("isp_watchdog: nobody had %p active\n", arg);
2126 	}
2127 }
2128 
2129 
2130 #if __FreeBSD_version >= 500000
2131 #define	isp_make_here(isp, tgt)	isp_announce(isp, tgt, AC_FOUND_DEVICE)
2132 #define	isp_make_gone(isp, tgt)	isp_announce(isp, tgt, AC_LOST_DEVICE)
2133 
2134 /*
2135  * Support function for Announcement
2136  */
2137 static void
2138 isp_announce(ispsoftc_t *isp, int tgt, int action)
2139 {
2140 	struct cam_path *tmppath;
2141 	ISPLOCK_2_CAMLOCK(isp);
2142 	if (xpt_create_path(&tmppath, NULL, cam_sim_path(isp->isp_sim), tgt,
2143 	    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2144 		xpt_async(action, tmppath, NULL);
2145 		xpt_free_path(tmppath);
2146 	}
2147 	CAMLOCK_2_ISPLOCK(isp);
2148 }
2149 #else
2150 #define	isp_make_here(isp, tgt)	do { ; } while (0)
2151 #define	isp_make_gone(isp, tgt)	do { ; } while (0)
2152 #endif
2153 
2154 
2155 /*
2156  * Gone Device Timer Function- when we have decided that a device has gone
2157  * away, we wait a specific period of time prior to telling the OS it has
2158  * gone away.
2159  *
2160  * This timer function fires once a second and then scans the port database
2161  * for devices that are marked dead but still have a virtual target assigned.
2162  * We decrement a counter for that port database entry, and when it hits zero,
2163  * we tell the OS the device has gone away.
2164  */
2165 static void
2166 isp_gdt(void *arg)
2167 {
2168 	ispsoftc_t *isp = arg;
2169 	fcportdb_t *lp;
2170 	int dbidx, tgt, more_to_do = 0;
2171 
2172 	isp_prt(isp, ISP_LOGDEBUG0, "GDT timer expired");
2173 	ISP_LOCK(isp);
2174 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2175 		lp = &FCPARAM(isp)->portdb[dbidx];
2176 
2177 		if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2178 			continue;
2179 		}
2180 		if (lp->ini_map_idx == 0) {
2181 			continue;
2182 		}
2183 		if (lp->new_reserved == 0) {
2184 			continue;
2185 		}
2186 		lp->new_reserved -= 1;
2187 		if (lp->new_reserved != 0) {
2188 			more_to_do++;
2189 			continue;
2190 		}
2191 		tgt = lp->ini_map_idx - 1;
2192 		FCPARAM(isp)->isp_ini_map[tgt] = 0;
2193 		lp->ini_map_idx = 0;
2194 		lp->state = FC_PORTDB_STATE_NIL;
2195 		isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2196 		    "Gone Device Timeout");
2197 		isp_make_gone(isp, tgt);
2198 	}
2199 	if (more_to_do) {
2200 		isp->isp_osinfo.gdt = timeout(isp_gdt, isp, hz);
2201 	} else {
2202 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2203 		    "stopping Gone Device Timer");
2204 		isp->isp_osinfo.gdt_running = 0;
2205 	}
2206 	ISP_UNLOCK(isp);
2207 }
2208 
2209 /*
2210  * Loop Down Timer Function- when loop goes down, a timer is started and
2211  * and after it expires we come here and take all probational devices that
2212  * the OS knows about and the tell the OS that they've gone away.
2213  *
2214  * We don't clear the devices out of our port database because, when loop
2215  * come back up, we have to do some actual cleanup with the chip at that
2216  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2217  */
2218 static void
2219 isp_ldt(void *arg)
2220 {
2221 	ispsoftc_t *isp = arg;
2222 	fcportdb_t *lp;
2223 	int dbidx, tgt;
2224 
2225 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Loop Down Timer expired");
2226 	ISP_LOCK(isp);
2227 
2228 	/*
2229 	 * Notify to the OS all targets who we now consider have departed.
2230 	 */
2231 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2232 		lp = &FCPARAM(isp)->portdb[dbidx];
2233 
2234 		if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
2235 			continue;
2236 		}
2237 		if (lp->ini_map_idx == 0) {
2238 			continue;
2239 		}
2240 
2241 		/*
2242 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
2243 		 */
2244 
2245 		/*
2246 		 * Mark that we've announced that this device is gone....
2247 		 */
2248 		lp->reserved = 1;
2249 
2250 		/*
2251 		 * but *don't* change the state of the entry. Just clear
2252 		 * any target id stuff and announce to CAM that the
2253 		 * device is gone. This way any necessary PLOGO stuff
2254 		 * will happen when loop comes back up.
2255 		 */
2256 
2257 		tgt = lp->ini_map_idx - 1;
2258 		FCPARAM(isp)->isp_ini_map[tgt] = 0;
2259 		lp->ini_map_idx = 0;
2260 		isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2261 		    "Loop Down Timeout");
2262 		isp_make_gone(isp, tgt);
2263 	}
2264 
2265 	/*
2266 	 * The loop down timer has expired. Wake up the kthread
2267 	 * to notice that fact (or make it false).
2268 	 */
2269 	isp->isp_osinfo.loop_down_time = isp->isp_osinfo.loop_down_limit+1;
2270 #if __FreeBSD_version < 500000
2271 	wakeup(&isp->isp_osinfo.kproc);
2272 #else
2273 #ifdef	ISP_SMPLOCK
2274 	cv_signal(&isp->isp_osinfo.kthread_cv);
2275 #else
2276 	wakeup(&isp->isp_osinfo.kthread_cv);
2277 #endif
2278 #endif
2279 	ISP_UNLOCK(isp);
2280 }
2281 
2282 static void
2283 isp_kthread(void *arg)
2284 {
2285 	ispsoftc_t *isp = arg;
2286 	int slp = 0;
2287 #if __FreeBSD_version < 500000
2288         int s;
2289 
2290         s = splcam();
2291 #else
2292 #ifdef	ISP_SMPLOCK
2293 	mtx_lock(&isp->isp_lock);
2294 #else
2295 	mtx_lock(&Giant);
2296 #endif
2297 #endif
2298 	/*
2299 	 * The first loop is for our usage where we have yet to have
2300 	 * gotten good fibre channel state.
2301 	 */
2302 	for (;;) {
2303 		int wasfrozen, lb, lim;
2304 
2305 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2306 		    "isp_kthread: checking FC state");
2307 		isp->isp_osinfo.mbox_sleep_ok = 1;
2308 		lb = isp_fc_runstate(isp, 250000);
2309 		isp->isp_osinfo.mbox_sleep_ok = 0;
2310 		if (lb) {
2311 			/*
2312 			 * Increment loop down time by the last sleep interval
2313 			 */
2314 			isp->isp_osinfo.loop_down_time += slp;
2315 
2316 			if (lb < 0) {
2317 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2318 				    "kthread: FC loop not up (down count %d)",
2319 				    isp->isp_osinfo.loop_down_time);
2320 			} else {
2321 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2322 				    "kthread: FC got to %d (down count %d)",
2323 				    lb, isp->isp_osinfo.loop_down_time);
2324 			}
2325 
2326 
2327 			/*
2328 			 * If we've never seen loop up and we've waited longer
2329 			 * than quickboot time, or we've seen loop up but we've
2330 			 * waited longer than loop_down_limit, give up and go
2331 			 * to sleep until loop comes up.
2332 			 */
2333 			if (FCPARAM(isp)->loop_seen_once == 0) {
2334 				lim = isp_quickboot_time;
2335 			} else {
2336 				lim = isp->isp_osinfo.loop_down_limit;
2337 			}
2338 			if (isp->isp_osinfo.loop_down_time >= lim) {
2339 				isp_freeze_loopdown(isp, "loop limit hit");
2340 				slp = 0;
2341 			} else if (isp->isp_osinfo.loop_down_time < 10) {
2342 				slp = 1;
2343 			} else if (isp->isp_osinfo.loop_down_time < 30) {
2344 				slp = 5;
2345 			} else if (isp->isp_osinfo.loop_down_time < 60) {
2346 				slp = 10;
2347 			} else if (isp->isp_osinfo.loop_down_time < 120) {
2348 				slp = 20;
2349 			} else {
2350 				slp = 30;
2351 			}
2352 
2353 		} else {
2354 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2355 			    "isp_kthread: FC state OK");
2356 			isp->isp_osinfo.loop_down_time = 0;
2357 			slp = 0;
2358 		}
2359 
2360 		/*
2361 		 * If we'd frozen the simq, unfreeze it now so that CAM
2362 		 * can start sending us commands. If the FC state isn't
2363 		 * okay yet, they'll hit that in isp_start which will
2364 		 * freeze the queue again.
2365 		 */
2366 		wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
2367 		isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
2368 		if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
2369 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2370 			    "isp_kthread: releasing simq");
2371 			ISPLOCK_2_CAMLOCK(isp);
2372 			xpt_release_simq(isp->isp_sim, 1);
2373 			CAMLOCK_2_ISPLOCK(isp);
2374 		}
2375 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2376 		    "isp_kthread: sleep time %d", slp);
2377 #if __FreeBSD_version < 500000
2378 		tsleep(&isp->isp_osinfo.kproc, PRIBIO, "ispf",
2379 		    slp * hz);
2380 #else
2381 #ifdef	ISP_SMPLOCK
2382 		cv_timed_wait(&isp->isp_osinfo.kthread_cv, &isp->isp_lock,
2383 		    slp * hz);
2384 #else
2385 		(void) tsleep(&isp->isp_osinfo.kthread_cv, PRIBIO, "ispf",
2386 		    slp * hz);
2387 #endif
2388 #endif
2389 		/*
2390 		 * If slp is zero, we're waking up for the first time after
2391 		 * things have been okay. In this case, we set a deferral state
2392 		 * for all commands and delay hysteresis seconds before starting
2393 		 * the FC state evaluation. This gives the loop/fabric a chance
2394 		 * to settle.
2395 		 */
2396 		if (slp == 0 && isp->isp_osinfo.hysteresis) {
2397 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2398 			    "isp_kthread: sleep hysteresis tick time %d",
2399 			    isp->isp_osinfo.hysteresis * hz);
2400 			(void) tsleep(&isp_fabric_hysteresis, PRIBIO, "ispT",
2401 			    (isp->isp_osinfo.hysteresis * hz));
2402 		}
2403 	}
2404 }
2405 
2406 static void
2407 isp_action(struct cam_sim *sim, union ccb *ccb)
2408 {
2409 	int bus, tgt, error, lim;
2410 	ispsoftc_t *isp;
2411 	struct ccb_trans_settings *cts;
2412 
2413 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2414 
2415 	isp = (ispsoftc_t *)cam_sim_softc(sim);
2416 	ccb->ccb_h.sim_priv.entries[0].field = 0;
2417 	ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2418 	if (isp->isp_state != ISP_RUNSTATE &&
2419 	    ccb->ccb_h.func_code == XPT_SCSI_IO) {
2420 		CAMLOCK_2_ISPLOCK(isp);
2421 		isp_init(isp);
2422 		if (isp->isp_state != ISP_INITSTATE) {
2423 			ISP_UNLOCK(isp);
2424 			/*
2425 			 * Lie. Say it was a selection timeout.
2426 			 */
2427 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
2428 			xpt_freeze_devq(ccb->ccb_h.path, 1);
2429 			xpt_done(ccb);
2430 			return;
2431 		}
2432 		isp->isp_state = ISP_RUNSTATE;
2433 		ISPLOCK_2_CAMLOCK(isp);
2434 	}
2435 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2436 
2437 
2438 	switch (ccb->ccb_h.func_code) {
2439 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2440 		/*
2441 		 * Do a couple of preliminary checks...
2442 		 */
2443 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2444 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2445 				ccb->ccb_h.status = CAM_REQ_INVALID;
2446 				xpt_done(ccb);
2447 				break;
2448 			}
2449 		}
2450 #ifdef	DIAGNOSTIC
2451 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
2452 			xpt_print(ccb->ccb_h.path, "invalid target\n");
2453 			ccb->ccb_h.status = CAM_PATH_INVALID;
2454 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
2455 			xpt_print(ccb->ccb_h.path, "invalid lun\n");
2456 			ccb->ccb_h.status = CAM_PATH_INVALID;
2457 		}
2458 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2459 			xpt_done(ccb);
2460 			break;
2461 		}
2462 #endif
2463 		((struct ccb_scsiio *) ccb)->scsi_status = SCSI_STATUS_OK;
2464 		CAMLOCK_2_ISPLOCK(isp);
2465 		error = isp_start((XS_T *) ccb);
2466 		switch (error) {
2467 		case CMD_QUEUED:
2468 			XS_CMD_S_CLEAR(ccb);
2469 			ISPLOCK_2_CAMLOCK(isp);
2470 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
2471 			if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2472 				int ms = ccb->ccb_h.timeout;
2473 				if (ms == CAM_TIME_DEFAULT) {
2474 					ms = 60*1000;
2475 				}
2476 				ccb->ccb_h.timeout_ch =
2477 				    timeout(isp_watchdog, ccb, isp_mstohz(ms));
2478 			} else {
2479 				callout_handle_init(&ccb->ccb_h.timeout_ch);
2480 			}
2481 			break;
2482 		case CMD_RQLATER:
2483 			/*
2484 			 * This can only happen for Fibre Channel
2485 			 */
2486 			KASSERT((IS_FC(isp)), ("CMD_RQLATER for FC only"));
2487 
2488 			/*
2489 			 * Handle initial and subsequent loop down cases
2490 			 */
2491 			if (FCPARAM(isp)->loop_seen_once == 0) {
2492 				lim = isp_quickboot_time;
2493 			} else {
2494 				lim = isp->isp_osinfo.loop_down_limit;
2495 			}
2496 			if (isp->isp_osinfo.loop_down_time >= lim) {
2497 				isp_prt(isp, ISP_LOGDEBUG0,
2498 				    "%d.%d downtime (%d) > lim (%d)",
2499 				    XS_TGT(ccb), XS_LUN(ccb),
2500 				    isp->isp_osinfo.loop_down_time, lim);
2501 				ccb->ccb_h.status =
2502 				    CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
2503 				xpt_freeze_devq(ccb->ccb_h.path, 1);
2504 				ISPLOCK_2_CAMLOCK(isp);
2505 				xpt_done(ccb);
2506 				break;
2507 			}
2508 			isp_prt(isp, ISP_LOGDEBUG0,
2509 			    "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
2510 			/*
2511 			 * Otherwise, retry in a while.
2512 			 */
2513 			ISPLOCK_2_CAMLOCK(isp);
2514 			cam_freeze_devq(ccb->ccb_h.path);
2515 			cam_release_devq(ccb->ccb_h.path,
2516 			    RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
2517 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
2518 			xpt_done(ccb);
2519 			break;
2520 		case CMD_EAGAIN:
2521 			ISPLOCK_2_CAMLOCK(isp);
2522 			cam_freeze_devq(ccb->ccb_h.path);
2523 			cam_release_devq(ccb->ccb_h.path,
2524 			    RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
2525 			xpt_done(ccb);
2526 			break;
2527 		case CMD_COMPLETE:
2528 			isp_done((struct ccb_scsiio *) ccb);
2529 			ISPLOCK_2_CAMLOCK(isp);
2530 			break;
2531 		default:
2532 			ISPLOCK_2_CAMLOCK(isp);
2533 			isp_prt(isp, ISP_LOGERR,
2534 			    "What's this? 0x%x at %d in file %s",
2535 			    error, __LINE__, __FILE__);
2536 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
2537 			xpt_done(ccb);
2538 		}
2539 		break;
2540 
2541 #ifdef	ISP_TARGET_MODE
2542 	case XPT_EN_LUN:		/* Enable LUN as a target */
2543 	{
2544 		int seq, i;
2545 		CAMLOCK_2_ISPLOCK(isp);
2546 		seq = isp_en_lun(isp, ccb);
2547 		if (seq < 0) {
2548 			ISPLOCK_2_CAMLOCK(isp);
2549 			xpt_done(ccb);
2550 			break;
2551 		}
2552 		for (i = 0; isp->isp_osinfo.leact[seq] && i < 30 * 1000; i++) {
2553 			uint32_t isr;
2554 			uint16_t sema, mbox;
2555 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2556 				isp_intr(isp, isr, sema, mbox);
2557 			}
2558 			DELAY(1000);
2559 		}
2560 		ISPLOCK_2_CAMLOCK(isp);
2561 		break;
2562 	}
2563 	case XPT_NOTIFY_ACK:		/* recycle notify ack */
2564 	case XPT_IMMED_NOTIFY:		/* Add Immediate Notify Resource */
2565 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
2566 	{
2567 		tstate_t *tptr =
2568 		    get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2569 		if (tptr == NULL) {
2570 			ccb->ccb_h.status = CAM_LUN_INVALID;
2571 			xpt_done(ccb);
2572 			break;
2573 		}
2574 		ccb->ccb_h.sim_priv.entries[0].field = 0;
2575 		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2576 		ccb->ccb_h.flags = 0;
2577 
2578 		CAMLOCK_2_ISPLOCK(isp);
2579 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
2580 			/*
2581 			 * Note that the command itself may not be done-
2582 			 * it may not even have had the first CTIO sent.
2583 			 */
2584 			tptr->atio_count++;
2585 			isp_prt(isp, ISP_LOGTDEBUG0,
2586 			    "Put FREE ATIO, lun %d, count now %d",
2587 			    ccb->ccb_h.target_lun, tptr->atio_count);
2588 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h,
2589 			    sim_links.sle);
2590 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
2591 			tptr->inot_count++;
2592 			isp_prt(isp, ISP_LOGTDEBUG0,
2593 			    "Put FREE INOT, lun %d, count now %d",
2594 			    ccb->ccb_h.target_lun, tptr->inot_count);
2595 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
2596 			    sim_links.sle);
2597 		} else {
2598 			isp_prt(isp, ISP_LOGWARN, "Got Notify ACK");;
2599 		}
2600 		rls_lun_statep(isp, tptr);
2601 		ccb->ccb_h.status = CAM_REQ_INPROG;
2602 		ISPLOCK_2_CAMLOCK(isp);
2603 		break;
2604 	}
2605 	case XPT_CONT_TARGET_IO:
2606 	{
2607 		CAMLOCK_2_ISPLOCK(isp);
2608 		isp_target_start_ctio(isp, ccb);
2609 		ISPLOCK_2_CAMLOCK(isp);
2610 		break;
2611 	}
2612 #endif
2613 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
2614 
2615 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
2616 		tgt = ccb->ccb_h.target_id;
2617 		tgt |= (bus << 16);
2618 
2619 		CAMLOCK_2_ISPLOCK(isp);
2620 		error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
2621 		ISPLOCK_2_CAMLOCK(isp);
2622 		if (error) {
2623 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2624 		} else {
2625 			ccb->ccb_h.status = CAM_REQ_CMP;
2626 		}
2627 		xpt_done(ccb);
2628 		break;
2629 	case XPT_ABORT:			/* Abort the specified CCB */
2630 	{
2631 		union ccb *accb = ccb->cab.abort_ccb;
2632 		CAMLOCK_2_ISPLOCK(isp);
2633 		switch (accb->ccb_h.func_code) {
2634 #ifdef	ISP_TARGET_MODE
2635 		case XPT_ACCEPT_TARGET_IO:
2636 		case XPT_IMMED_NOTIFY:
2637         		ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
2638 			break;
2639 		case XPT_CONT_TARGET_IO:
2640 			isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
2641 			ccb->ccb_h.status = CAM_UA_ABORT;
2642 			break;
2643 #endif
2644 		case XPT_SCSI_IO:
2645 			error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
2646 			if (error) {
2647 				ccb->ccb_h.status = CAM_UA_ABORT;
2648 			} else {
2649 				ccb->ccb_h.status = CAM_REQ_CMP;
2650 			}
2651 			break;
2652 		default:
2653 			ccb->ccb_h.status = CAM_REQ_INVALID;
2654 			break;
2655 		}
2656 		ISPLOCK_2_CAMLOCK(isp);
2657 		xpt_done(ccb);
2658 		break;
2659 	}
2660 #ifdef	CAM_NEW_TRAN_CODE
2661 #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
2662 #else
2663 #define	IS_CURRENT_SETTINGS(c)	(c->flags & CCB_TRANS_CURRENT_SETTINGS)
2664 #endif
2665 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
2666 		cts = &ccb->cts;
2667 		if (!IS_CURRENT_SETTINGS(cts)) {
2668 			ccb->ccb_h.status = CAM_REQ_INVALID;
2669 			xpt_done(ccb);
2670 			break;
2671 		}
2672 		tgt = cts->ccb_h.target_id;
2673 		CAMLOCK_2_ISPLOCK(isp);
2674 		if (IS_SCSI(isp)) {
2675 #ifndef	CAM_NEW_TRAN_CODE
2676 			sdparam *sdp = isp->isp_param;
2677 			uint16_t *dptr;
2678 
2679 			bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2680 
2681 			sdp += bus;
2682 			/*
2683 			 * We always update (internally) from goal_flags
2684 			 * so any request to change settings just gets
2685 			 * vectored to that location.
2686 			 */
2687 			dptr = &sdp->isp_devparam[tgt].goal_flags;
2688 
2689 			/*
2690 			 * Note that these operations affect the
2691 			 * the goal flags (goal_flags)- not
2692 			 * the current state flags. Then we mark
2693 			 * things so that the next operation to
2694 			 * this HBA will cause the update to occur.
2695 			 */
2696 			if (cts->valid & CCB_TRANS_DISC_VALID) {
2697 				if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
2698 					*dptr |= DPARM_DISC;
2699 				} else {
2700 					*dptr &= ~DPARM_DISC;
2701 				}
2702 			}
2703 			if (cts->valid & CCB_TRANS_TQ_VALID) {
2704 				if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
2705 					*dptr |= DPARM_TQING;
2706 				} else {
2707 					*dptr &= ~DPARM_TQING;
2708 				}
2709 			}
2710 			if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) {
2711 				switch (cts->bus_width) {
2712 				case MSG_EXT_WDTR_BUS_16_BIT:
2713 					*dptr |= DPARM_WIDE;
2714 					break;
2715 				default:
2716 					*dptr &= ~DPARM_WIDE;
2717 				}
2718 			}
2719 			/*
2720 			 * Any SYNC RATE of nonzero and SYNC_OFFSET
2721 			 * of nonzero will cause us to go to the
2722 			 * selected (from NVRAM) maximum value for
2723 			 * this device. At a later point, we'll
2724 			 * allow finer control.
2725 			 */
2726 			if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
2727 			    (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) &&
2728 			    (cts->sync_offset > 0)) {
2729 				*dptr |= DPARM_SYNC;
2730 			} else {
2731 				*dptr &= ~DPARM_SYNC;
2732 			}
2733 			*dptr |= DPARM_SAFE_DFLT;
2734 #else
2735 			struct ccb_trans_settings_scsi *scsi =
2736 			    &cts->proto_specific.scsi;
2737 			struct ccb_trans_settings_spi *spi =
2738 			    &cts->xport_specific.spi;
2739 			sdparam *sdp = isp->isp_param;
2740 			uint16_t *dptr;
2741 
2742 			if (spi->valid == 0 && scsi->valid == 0) {
2743 				ISPLOCK_2_CAMLOCK(isp);
2744 				ccb->ccb_h.status = CAM_REQ_CMP;
2745 				xpt_done(ccb);
2746 				break;
2747 			}
2748 
2749 			bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2750 			sdp += bus;
2751 			/*
2752 			 * We always update (internally) from goal_flags
2753 			 * so any request to change settings just gets
2754 			 * vectored to that location.
2755 			 */
2756 			dptr = &sdp->isp_devparam[tgt].goal_flags;
2757 
2758 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
2759 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
2760 					*dptr |= DPARM_DISC;
2761 				else
2762 					*dptr &= ~DPARM_DISC;
2763 			}
2764 
2765 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2766 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
2767 					*dptr |= DPARM_TQING;
2768 				else
2769 					*dptr &= ~DPARM_TQING;
2770 			}
2771 
2772 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
2773 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
2774 					*dptr |= DPARM_WIDE;
2775 				else
2776 					*dptr &= ~DPARM_WIDE;
2777 			}
2778 
2779 			/*
2780 			 * XXX: FIX ME
2781 			 */
2782 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) &&
2783 			    (spi->valid & CTS_SPI_VALID_SYNC_RATE) &&
2784 			    (spi->sync_period && spi->sync_offset)) {
2785 				*dptr |= DPARM_SYNC;
2786 				/*
2787 				 * XXX: CHECK FOR LEGALITY
2788 				 */
2789 				sdp->isp_devparam[tgt].goal_period =
2790 				    spi->sync_period;
2791 				sdp->isp_devparam[tgt].goal_offset =
2792 				    spi->sync_offset;
2793 			} else {
2794 				*dptr &= ~DPARM_SYNC;
2795 			}
2796 #endif
2797 			isp_prt(isp, ISP_LOGDEBUG0,
2798 			    "SET (%d.%d.%d) to flags %x off %x per %x",
2799 			    bus, tgt, cts->ccb_h.target_lun,
2800 			    sdp->isp_devparam[tgt].goal_flags,
2801 			    sdp->isp_devparam[tgt].goal_offset,
2802 			    sdp->isp_devparam[tgt].goal_period);
2803 			sdp->isp_devparam[tgt].dev_update = 1;
2804 			isp->isp_update |= (1 << bus);
2805 		}
2806 		ISPLOCK_2_CAMLOCK(isp);
2807 		ccb->ccb_h.status = CAM_REQ_CMP;
2808 		xpt_done(ccb);
2809 		break;
2810 	case XPT_GET_TRAN_SETTINGS:
2811 		cts = &ccb->cts;
2812 		tgt = cts->ccb_h.target_id;
2813 		CAMLOCK_2_ISPLOCK(isp);
2814 		if (IS_FC(isp)) {
2815 #ifndef	CAM_NEW_TRAN_CODE
2816 			/*
2817 			 * a lot of normal SCSI things don't make sense.
2818 			 */
2819 			cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
2820 			cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2821 			/*
2822 			 * How do you measure the width of a high
2823 			 * speed serial bus? Well, in bytes.
2824 			 *
2825 			 * Offset and period make no sense, though, so we set
2826 			 * (above) a 'base' transfer speed to be gigabit.
2827 			 */
2828 			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2829 #else
2830 			fcparam *fcp = isp->isp_param;
2831 			struct ccb_trans_settings_scsi *scsi =
2832 			    &cts->proto_specific.scsi;
2833 			struct ccb_trans_settings_fc *fc =
2834 			    &cts->xport_specific.fc;
2835 
2836 			cts->protocol = PROTO_SCSI;
2837 			cts->protocol_version = SCSI_REV_2;
2838 			cts->transport = XPORT_FC;
2839 			cts->transport_version = 0;
2840 
2841 			scsi->valid = CTS_SCSI_VALID_TQ;
2842 			scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2843 			fc->valid = CTS_FC_VALID_SPEED;
2844 			if (fcp->isp_gbspeed == 2) {
2845 				fc->bitrate = 200000;
2846 			} else {
2847 				fc->bitrate = 100000;
2848 			}
2849 			if (tgt > 0 && tgt < MAX_FC_TARG) {
2850 				fcportdb_t *lp = &fcp->portdb[tgt];
2851 				fc->wwnn = lp->node_wwn;
2852 				fc->wwpn = lp->port_wwn;
2853 				fc->port = lp->portid;
2854 				fc->valid |= CTS_FC_VALID_WWNN |
2855 				    CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
2856 			}
2857 #endif
2858 		} else {
2859 #ifdef	CAM_NEW_TRAN_CODE
2860 			struct ccb_trans_settings_scsi *scsi =
2861 			    &cts->proto_specific.scsi;
2862 			struct ccb_trans_settings_spi *spi =
2863 			    &cts->xport_specific.spi;
2864 #endif
2865 			sdparam *sdp = isp->isp_param;
2866 			int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2867 			uint16_t dval, pval, oval;
2868 
2869 			sdp += bus;
2870 
2871 			if (IS_CURRENT_SETTINGS(cts)) {
2872 				sdp->isp_devparam[tgt].dev_refresh = 1;
2873 				isp->isp_update |= (1 << bus);
2874 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2875 				    NULL);
2876 				dval = sdp->isp_devparam[tgt].actv_flags;
2877 				oval = sdp->isp_devparam[tgt].actv_offset;
2878 				pval = sdp->isp_devparam[tgt].actv_period;
2879 			} else {
2880 				dval = sdp->isp_devparam[tgt].nvrm_flags;
2881 				oval = sdp->isp_devparam[tgt].nvrm_offset;
2882 				pval = sdp->isp_devparam[tgt].nvrm_period;
2883 			}
2884 
2885 #ifndef	CAM_NEW_TRAN_CODE
2886 			cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
2887 
2888 			if (dval & DPARM_DISC) {
2889 				cts->flags |= CCB_TRANS_DISC_ENB;
2890 			}
2891 			if (dval & DPARM_TQING) {
2892 				cts->flags |= CCB_TRANS_TAG_ENB;
2893 			}
2894 			if (dval & DPARM_WIDE) {
2895 				cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2896 			} else {
2897 				cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2898 			}
2899 			cts->valid = CCB_TRANS_BUS_WIDTH_VALID |
2900 			    CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2901 
2902 			if ((dval & DPARM_SYNC) && oval != 0) {
2903 				cts->sync_period = pval;
2904 				cts->sync_offset = oval;
2905 				cts->valid |=
2906 				    CCB_TRANS_SYNC_RATE_VALID |
2907 				    CCB_TRANS_SYNC_OFFSET_VALID;
2908 			}
2909 #else
2910 			cts->protocol = PROTO_SCSI;
2911 			cts->protocol_version = SCSI_REV_2;
2912 			cts->transport = XPORT_SPI;
2913 			cts->transport_version = 2;
2914 
2915 			spi->valid = 0;
2916 			scsi->valid = 0;
2917 			spi->flags = 0;
2918 			scsi->flags = 0;
2919 			if (dval & DPARM_DISC) {
2920 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2921 			}
2922 			if ((dval & DPARM_SYNC) && oval && pval) {
2923 				spi->sync_offset = oval;
2924 				spi->sync_period = pval;
2925 			} else {
2926 				spi->sync_offset = 0;
2927 				spi->sync_period = 0;
2928 			}
2929 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2930 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2931 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2932 			if (dval & DPARM_WIDE) {
2933 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2934 			} else {
2935 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2936 			}
2937 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2938 				scsi->valid = CTS_SCSI_VALID_TQ;
2939 				if (dval & DPARM_TQING) {
2940 					scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2941 				}
2942 				spi->valid |= CTS_SPI_VALID_DISC;
2943 			}
2944 #endif
2945 			isp_prt(isp, ISP_LOGDEBUG0,
2946 			    "GET %s (%d.%d.%d) to flags %x off %x per %x",
2947 			    IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2948 			    bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
2949 		}
2950 		ISPLOCK_2_CAMLOCK(isp);
2951 		ccb->ccb_h.status = CAM_REQ_CMP;
2952 		xpt_done(ccb);
2953 		break;
2954 
2955 	case XPT_CALC_GEOMETRY:
2956 #if __FreeBSD_version < 500000
2957 	{
2958 		struct ccb_calc_geometry *ccg;
2959 		u_int32_t secs_per_cylinder;
2960 		u_int32_t size_mb;
2961 
2962 		ccg = &ccb->ccg;
2963 		if (ccg->block_size == 0) {
2964 			ccb->ccb_h.status = CAM_REQ_INVALID;
2965 			xpt_done(ccb);
2966 			break;
2967 		}
2968 		size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2969 		if (size_mb > 1024) {
2970 			ccg->heads = 255;
2971 			ccg->secs_per_track = 63;
2972 		} else {
2973 			ccg->heads = 64;
2974 			ccg->secs_per_track = 32;
2975 		}
2976 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2977 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2978 		ccb->ccb_h.status = CAM_REQ_CMP;
2979 		xpt_done(ccb);
2980 		break;
2981 	}
2982 #else
2983 	{
2984 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
2985 		xpt_done(ccb);
2986 		break;
2987 	}
2988 #endif
2989 	case XPT_RESET_BUS:		/* Reset the specified bus */
2990 		bus = cam_sim_bus(sim);
2991 		CAMLOCK_2_ISPLOCK(isp);
2992 		error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2993 		ISPLOCK_2_CAMLOCK(isp);
2994 		if (error)
2995 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2996 		else {
2997 			if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2998 				xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2999 			else if (isp->isp_path != NULL)
3000 				xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
3001 			ccb->ccb_h.status = CAM_REQ_CMP;
3002 		}
3003 		xpt_done(ccb);
3004 		break;
3005 
3006 	case XPT_TERM_IO:		/* Terminate the I/O process */
3007 		ccb->ccb_h.status = CAM_REQ_INVALID;
3008 		xpt_done(ccb);
3009 		break;
3010 
3011 	case XPT_PATH_INQ:		/* Path routing inquiry */
3012 	{
3013 		struct ccb_pathinq *cpi = &ccb->cpi;
3014 
3015 		cpi->version_num = 1;
3016 #ifdef	ISP_TARGET_MODE
3017 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3018 #else
3019 		cpi->target_sprt = 0;
3020 #endif
3021 		cpi->hba_eng_cnt = 0;
3022 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
3023 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
3024 		cpi->bus_id = cam_sim_bus(sim);
3025 		if (IS_FC(isp)) {
3026 			cpi->hba_misc = PIM_NOBUSRESET;
3027 			/*
3028 			 * Because our loop ID can shift from time to time,
3029 			 * make our initiator ID out of range of our bus.
3030 			 */
3031 			cpi->initiator_id = cpi->max_target + 1;
3032 
3033 			/*
3034 			 * Set base transfer capabilities for Fibre Channel.
3035 			 * Technically not correct because we don't know
3036 			 * what media we're running on top of- but we'll
3037 			 * look good if we always say 100MB/s.
3038 			 */
3039 			if (FCPARAM(isp)->isp_gbspeed == 2)
3040 				cpi->base_transfer_speed = 200000;
3041 			else
3042 				cpi->base_transfer_speed = 100000;
3043 			cpi->hba_inquiry = PI_TAG_ABLE;
3044 #ifdef	CAM_NEW_TRAN_CODE
3045 			cpi->transport = XPORT_FC;
3046 			cpi->transport_version = 0;
3047 #endif
3048 		} else {
3049 			sdparam *sdp = isp->isp_param;
3050 			sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
3051 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3052 			cpi->hba_misc = 0;
3053 			cpi->initiator_id = sdp->isp_initiator_id;
3054 			cpi->base_transfer_speed = 3300;
3055 #ifdef	CAM_NEW_TRAN_CODE
3056 			cpi->transport = XPORT_SPI;
3057 			cpi->transport_version = 2;
3058 #endif
3059 		}
3060 #ifdef	CAM_NEW_TRAN_CODE
3061 		cpi->protocol = PROTO_SCSI;
3062 		cpi->protocol_version = SCSI_REV_2;
3063 #endif
3064 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3065 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
3066 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3067 		cpi->unit_number = cam_sim_unit(sim);
3068 		cpi->ccb_h.status = CAM_REQ_CMP;
3069 		xpt_done(ccb);
3070 		break;
3071 	}
3072 	default:
3073 		ccb->ccb_h.status = CAM_REQ_INVALID;
3074 		xpt_done(ccb);
3075 		break;
3076 	}
3077 }
3078 
3079 #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
3080 
3081 void
3082 isp_done(struct ccb_scsiio *sccb)
3083 {
3084 	ispsoftc_t *isp = XS_ISP(sccb);
3085 
3086 	if (XS_NOERR(sccb))
3087 		XS_SETERR(sccb, CAM_REQ_CMP);
3088 
3089 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
3090 	    (sccb->scsi_status != SCSI_STATUS_OK)) {
3091 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
3092 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) &&
3093 		    (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
3094 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
3095 		} else {
3096 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3097 		}
3098 	}
3099 
3100 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3101 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3102 		isp_prt(isp, ISP_LOGDEBUG0,
3103 		    "target %d lun %d CAM status 0x%x SCSI status 0x%x",
3104 		    XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status,
3105 		    sccb->scsi_status);
3106 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3107 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
3108 			xpt_freeze_devq(sccb->ccb_h.path, 1);
3109 		}
3110 	}
3111 
3112 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
3113 	    (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3114 		xpt_print(sccb->ccb_h.path,
3115 		    "cam completion status 0x%x\n", sccb->ccb_h.status);
3116 	}
3117 
3118 	XS_CMD_S_DONE(sccb);
3119 	if (XS_CMD_WDOG_P(sccb) == 0) {
3120 		untimeout(isp_watchdog, sccb, sccb->ccb_h.timeout_ch);
3121 		if (XS_CMD_GRACE_P(sccb)) {
3122 			isp_prt(isp, ISP_LOGDEBUG2,
3123 			    "finished command on borrowed time");
3124 		}
3125 		XS_CMD_S_CLEAR(sccb);
3126 		ISPLOCK_2_CAMLOCK(isp);
3127 		xpt_done((union ccb *) sccb);
3128 		CAMLOCK_2_ISPLOCK(isp);
3129 	}
3130 }
3131 
3132 int
3133 isp_async(ispsoftc_t *isp, ispasync_t cmd, void *arg)
3134 {
3135 	int bus, rv = 0;
3136 	static const char prom[] =
3137 	    "PortID 0x%06x handle 0x%x role %s %s\n"
3138 	    "      WWNN 0x%08x%08x WWPN 0x%08x%08x";
3139 	static const char prom2[] =
3140 	    "PortID 0x%06x handle 0x%x role %s %s tgt %u\n"
3141 	    "      WWNN 0x%08x%08x WWPN 0x%08x%08x";
3142 	char *msg = NULL;
3143 	target_id_t tgt;
3144 	fcportdb_t *lp;
3145 	struct cam_path *tmppath;
3146 
3147 	switch (cmd) {
3148 	case ISPASYNC_NEW_TGT_PARAMS:
3149 	{
3150 #ifdef	CAM_NEW_TRAN_CODE
3151 		struct ccb_trans_settings_scsi *scsi;
3152 		struct ccb_trans_settings_spi *spi;
3153 #endif
3154 		int flags, tgt;
3155 		sdparam *sdp = isp->isp_param;
3156 		struct ccb_trans_settings cts;
3157 
3158 		memset(&cts, 0, sizeof (struct ccb_trans_settings));
3159 
3160 		tgt = *((int *)arg);
3161 		bus = (tgt >> 16) & 0xffff;
3162 		tgt &= 0xffff;
3163 		sdp += bus;
3164 		ISPLOCK_2_CAMLOCK(isp);
3165 		if (xpt_create_path(&tmppath, NULL,
3166 		    cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
3167 		    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3168 			CAMLOCK_2_ISPLOCK(isp);
3169 			isp_prt(isp, ISP_LOGWARN,
3170 			    "isp_async cannot make temp path for %d.%d",
3171 			    tgt, bus);
3172 			rv = -1;
3173 			break;
3174 		}
3175 		CAMLOCK_2_ISPLOCK(isp);
3176 		flags = sdp->isp_devparam[tgt].actv_flags;
3177 #ifdef	CAM_NEW_TRAN_CODE
3178 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
3179 		cts.protocol = PROTO_SCSI;
3180 		cts.transport = XPORT_SPI;
3181 
3182 		scsi = &cts.proto_specific.scsi;
3183 		spi = &cts.xport_specific.spi;
3184 
3185 		if (flags & DPARM_TQING) {
3186 			scsi->valid |= CTS_SCSI_VALID_TQ;
3187 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3188 		}
3189 
3190 		if (flags & DPARM_DISC) {
3191 			spi->valid |= CTS_SPI_VALID_DISC;
3192 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3193 		}
3194 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3195 		if (flags & DPARM_WIDE) {
3196 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3197 		} else {
3198 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3199 		}
3200 		if (flags & DPARM_SYNC) {
3201 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3202 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3203 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3204 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3205 		}
3206 #else
3207 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
3208 		cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
3209 		if (flags & DPARM_DISC) {
3210 			cts.flags |= CCB_TRANS_DISC_ENB;
3211 		}
3212 		if (flags & DPARM_TQING) {
3213 			cts.flags |= CCB_TRANS_TAG_ENB;
3214 		}
3215 		cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
3216 		cts.bus_width = (flags & DPARM_WIDE)?
3217 		    MSG_EXT_WDTR_BUS_8_BIT : MSG_EXT_WDTR_BUS_16_BIT;
3218 		cts.sync_period = sdp->isp_devparam[tgt].actv_period;
3219 		cts.sync_offset = sdp->isp_devparam[tgt].actv_offset;
3220 		if (flags & DPARM_SYNC) {
3221 			cts.valid |=
3222 			    CCB_TRANS_SYNC_RATE_VALID |
3223 			    CCB_TRANS_SYNC_OFFSET_VALID;
3224 		}
3225 #endif
3226 		isp_prt(isp, ISP_LOGDEBUG2,
3227 		    "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
3228 		    bus, tgt, sdp->isp_devparam[tgt].actv_period,
3229 		    sdp->isp_devparam[tgt].actv_offset, flags);
3230 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3231 		ISPLOCK_2_CAMLOCK(isp);
3232 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3233 		xpt_free_path(tmppath);
3234 		CAMLOCK_2_ISPLOCK(isp);
3235 		break;
3236 	}
3237 	case ISPASYNC_BUS_RESET:
3238 		bus = *((int *)arg);
3239 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
3240 		    bus);
3241 		if (bus > 0 && isp->isp_path2) {
3242 			ISPLOCK_2_CAMLOCK(isp);
3243 			xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
3244 			CAMLOCK_2_ISPLOCK(isp);
3245 		} else if (isp->isp_path) {
3246 			ISPLOCK_2_CAMLOCK(isp);
3247 			xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
3248 			CAMLOCK_2_ISPLOCK(isp);
3249 		}
3250 		break;
3251 	case ISPASYNC_LIP:
3252 		if (msg == NULL) {
3253 			msg = "LIP Received";
3254 		}
3255 		/* FALLTHROUGH */
3256 	case ISPASYNC_LOOP_RESET:
3257 		if (msg == NULL) {
3258 			msg = "LOOP Reset";
3259 		}
3260 		/* FALLTHROUGH */
3261 	case ISPASYNC_LOOP_DOWN:
3262 		if (msg == NULL) {
3263 			msg = "LOOP Down";
3264 		}
3265 		if (isp->isp_path) {
3266 			isp_freeze_loopdown(isp, msg);
3267 		}
3268 		if (isp->isp_osinfo.ldt_running == 0) {
3269 			isp->isp_osinfo.ldt = timeout(isp_ldt, isp,
3270 			    isp->isp_osinfo.loop_down_limit * hz);
3271 			isp->isp_osinfo.ldt_running = 1;
3272 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3273 			   "starting Loop Down Timer");
3274 		}
3275 		isp_prt(isp, ISP_LOGINFO, msg);
3276 		break;
3277 	case ISPASYNC_LOOP_UP:
3278 		/*
3279 		 * Now we just note that Loop has come up. We don't
3280 		 * actually do anything because we're waiting for a
3281 		 * Change Notify before activating the FC cleanup
3282 		 * thread to look at the state of the loop again.
3283 		 */
3284 		isp_prt(isp, ISP_LOGINFO, "Loop UP");
3285 		break;
3286 	case ISPASYNC_DEV_ARRIVED:
3287 		lp = arg;
3288 		lp->reserved = 0;
3289 		if ((isp->isp_role & ISP_ROLE_INITIATOR) &&
3290 		    (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
3291 			int dbidx = lp - FCPARAM(isp)->portdb;
3292 			int i;
3293 
3294 			for (i = 0; i < MAX_FC_TARG; i++) {
3295 				if (i >= FL_ID && i <= SNS_ID) {
3296 					continue;
3297 				}
3298 				if (FCPARAM(isp)->isp_ini_map[i] == 0) {
3299 					break;
3300 				}
3301 			}
3302 			if (i < MAX_FC_TARG) {
3303 				FCPARAM(isp)->isp_ini_map[i] = dbidx + 1;
3304 				lp->ini_map_idx = i + 1;
3305 			} else {
3306 				isp_prt(isp, ISP_LOGWARN, "out of target ids");
3307 				isp_dump_portdb(isp);
3308 			}
3309 		}
3310 		if (lp->ini_map_idx) {
3311 			tgt = lp->ini_map_idx - 1;
3312 			isp_prt(isp, ISP_LOGCONFIG, prom2,
3313 			    lp->portid, lp->handle,
3314 		            roles[lp->roles], "arrived at", tgt,
3315 		    	    (uint32_t) (lp->node_wwn >> 32),
3316 			    (uint32_t) lp->node_wwn,
3317 		    	    (uint32_t) (lp->port_wwn >> 32),
3318 			    (uint32_t) lp->port_wwn);
3319 			isp_make_here(isp, tgt);
3320 		} else {
3321 			isp_prt(isp, ISP_LOGCONFIG, prom,
3322 			    lp->portid, lp->handle,
3323 		            roles[lp->roles], "arrived",
3324 		    	    (uint32_t) (lp->node_wwn >> 32),
3325 			    (uint32_t) lp->node_wwn,
3326 		    	    (uint32_t) (lp->port_wwn >> 32),
3327 			    (uint32_t) lp->port_wwn);
3328 		}
3329 		break;
3330 	case ISPASYNC_DEV_CHANGED:
3331 		lp = arg;
3332 		if (isp_change_is_bad) {
3333 			lp->state = FC_PORTDB_STATE_NIL;
3334 			if (lp->ini_map_idx) {
3335 				tgt = lp->ini_map_idx - 1;
3336 				FCPARAM(isp)->isp_ini_map[tgt] = 0;
3337 				lp->ini_map_idx = 0;
3338 				isp_prt(isp, ISP_LOGCONFIG, prom3,
3339 				    lp->portid, tgt, "change is bad");
3340 				isp_make_gone(isp, tgt);
3341 			} else {
3342 				isp_prt(isp, ISP_LOGCONFIG, prom,
3343 				    lp->portid, lp->handle,
3344 				    roles[lp->roles],
3345 				    "changed and departed",
3346 				    (uint32_t) (lp->node_wwn >> 32),
3347 				    (uint32_t) lp->node_wwn,
3348 				    (uint32_t) (lp->port_wwn >> 32),
3349 				    (uint32_t) lp->port_wwn);
3350 			}
3351 		} else {
3352 			lp->portid = lp->new_portid;
3353 			lp->roles = lp->new_roles;
3354 			if (lp->ini_map_idx) {
3355 				int t = lp->ini_map_idx - 1;
3356 				FCPARAM(isp)->isp_ini_map[t] =
3357 				    (lp - FCPARAM(isp)->portdb) + 1;
3358 				tgt = lp->ini_map_idx - 1;
3359 				isp_prt(isp, ISP_LOGCONFIG, prom2,
3360 				    lp->portid, lp->handle,
3361 				    roles[lp->roles], "changed at", tgt,
3362 				    (uint32_t) (lp->node_wwn >> 32),
3363 				    (uint32_t) lp->node_wwn,
3364 				    (uint32_t) (lp->port_wwn >> 32),
3365 				    (uint32_t) lp->port_wwn);
3366 			} else {
3367 				isp_prt(isp, ISP_LOGCONFIG, prom,
3368 				    lp->portid, lp->handle,
3369 				    roles[lp->roles], "changed",
3370 				    (uint32_t) (lp->node_wwn >> 32),
3371 				    (uint32_t) lp->node_wwn,
3372 				    (uint32_t) (lp->port_wwn >> 32),
3373 				    (uint32_t) lp->port_wwn);
3374 			}
3375 		}
3376 		break;
3377 	case ISPASYNC_DEV_STAYED:
3378 		lp = arg;
3379 		if (lp->ini_map_idx) {
3380 			tgt = lp->ini_map_idx - 1;
3381 			isp_prt(isp, ISP_LOGCONFIG, prom2,
3382 			    lp->portid, lp->handle,
3383 		    	    roles[lp->roles], "stayed at", tgt,
3384 			    (uint32_t) (lp->node_wwn >> 32),
3385 			    (uint32_t) lp->node_wwn,
3386 		    	    (uint32_t) (lp->port_wwn >> 32),
3387 			    (uint32_t) lp->port_wwn);
3388 		} else {
3389 			isp_prt(isp, ISP_LOGCONFIG, prom,
3390 			    lp->portid, lp->handle,
3391 		    	    roles[lp->roles], "stayed",
3392 			    (uint32_t) (lp->node_wwn >> 32),
3393 			    (uint32_t) lp->node_wwn,
3394 		    	    (uint32_t) (lp->port_wwn >> 32),
3395 			    (uint32_t) lp->port_wwn);
3396 		}
3397 		break;
3398 	case ISPASYNC_DEV_GONE:
3399 		lp = arg;
3400 		/*
3401 		 * If this has a virtual target and we haven't marked it
3402 		 * that we're going to have isp_gdt tell the OS it's gone,
3403 		 * set the isp_gdt timer running on it.
3404 		 *
3405 		 * If it isn't marked that isp_gdt is going to get rid of it,
3406 		 * announce that it's gone.
3407 		 */
3408 		if (lp->ini_map_idx && lp->reserved == 0) {
3409 			lp->reserved = 1;
3410 			lp->new_reserved = isp->isp_osinfo.gone_device_time;
3411 			lp->state = FC_PORTDB_STATE_ZOMBIE;
3412 			if (isp->isp_osinfo.gdt_running == 0) {
3413 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3414 				    "starting Gone Device Timer");
3415 				isp->isp_osinfo.gdt = timeout(isp_gdt, isp, hz);
3416 				isp->isp_osinfo.gdt_running = 1;
3417 			}
3418 			tgt = lp->ini_map_idx - 1;
3419 			isp_prt(isp, ISP_LOGCONFIG, prom2,
3420 			    lp->portid, lp->handle,
3421 		            roles[lp->roles], "gone zombie at", tgt,
3422 		    	    (uint32_t) (lp->node_wwn >> 32),
3423 			    (uint32_t) lp->node_wwn,
3424 		    	    (uint32_t) (lp->port_wwn >> 32),
3425 			    (uint32_t) lp->port_wwn);
3426 		} else if (lp->reserved == 0) {
3427 			isp_prt(isp, ISP_LOGCONFIG, prom,
3428 			    lp->portid, lp->handle,
3429 			    roles[lp->roles], "departed",
3430 			    (uint32_t) (lp->node_wwn >> 32),
3431 			    (uint32_t) lp->node_wwn,
3432 			    (uint32_t) (lp->port_wwn >> 32),
3433 			    (uint32_t) lp->port_wwn);
3434 		}
3435 		break;
3436 	case ISPASYNC_CHANGE_NOTIFY:
3437 	{
3438 		char *msg;
3439 		if (arg == ISPASYNC_CHANGE_PDB) {
3440 			msg = "Port Database Changed";
3441 		} else if (arg == ISPASYNC_CHANGE_SNS) {
3442 			msg = "Name Server Database Changed";
3443 		} else {
3444 			msg = "Other Change Notify";
3445 		}
3446 		/*
3447 		 * If the loop down timer is running, cancel it.
3448 		 */
3449 		if (isp->isp_osinfo.ldt_running) {
3450 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3451 			   "Stopping Loop Down Timer");
3452 			isp->isp_osinfo.ldt_running = 0;
3453 			untimeout(isp_ldt, isp, isp->isp_osinfo.ldt);
3454 			callout_handle_init(&isp->isp_osinfo.ldt);
3455 		}
3456 		isp_prt(isp, ISP_LOGINFO, msg);
3457 		isp_freeze_loopdown(isp, msg);
3458 #if __FreeBSD_version < 500000
3459 		wakeup(&isp->isp_osinfo.kproc);
3460 #else
3461 #ifdef	ISP_SMPLOCK
3462 		cv_signal(&isp->isp_osinfo.kthread_cv);
3463 #else
3464 		wakeup(&isp->isp_osinfo.kthread_cv);
3465 #endif
3466 #endif
3467 		break;
3468 	}
3469 #ifdef	ISP_TARGET_MODE
3470 	case ISPASYNC_TARGET_NOTIFY:
3471 	{
3472 		tmd_notify_t *nt = arg;
3473 		isp_prt(isp, ISP_LOGALL,
3474 		    "target notify code 0x%x", nt->nt_ncode);
3475 		break;
3476 	}
3477 	case ISPASYNC_TARGET_ACTION:
3478 		switch (((isphdr_t *)arg)->rqs_entry_type) {
3479 		default:
3480 			isp_prt(isp, ISP_LOGWARN,
3481 			   "event 0x%x for unhandled target action",
3482 			    ((isphdr_t *)arg)->rqs_entry_type);
3483 			break;
3484 		case RQSTYPE_NOTIFY:
3485 			if (IS_SCSI(isp)) {
3486 				rv = isp_handle_platform_notify_scsi(isp,
3487 				    (in_entry_t *) arg);
3488 			} else {
3489 				rv = isp_handle_platform_notify_fc(isp,
3490 				    (in_fcentry_t *) arg);
3491 			}
3492 			break;
3493 		case RQSTYPE_ATIO:
3494 			rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
3495 			break;
3496 		case RQSTYPE_ATIO2:
3497 			rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
3498 			break;
3499 		case RQSTYPE_CTIO3:
3500 		case RQSTYPE_CTIO2:
3501 		case RQSTYPE_CTIO:
3502 			rv = isp_handle_platform_ctio(isp, arg);
3503 			break;
3504 		case RQSTYPE_ENABLE_LUN:
3505 		case RQSTYPE_MODIFY_LUN:
3506 			isp_ledone(isp, (lun_entry_t *) arg);
3507 			break;
3508 		}
3509 		break;
3510 #endif
3511 	case ISPASYNC_FW_CRASH:
3512 	{
3513 		uint16_t mbox1, mbox6;
3514 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
3515 		if (IS_DUALBUS(isp)) {
3516 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
3517 		} else {
3518 			mbox6 = 0;
3519 		}
3520                 isp_prt(isp, ISP_LOGERR,
3521                     "Internal Firmware Error on bus %d @ RISC Address 0x%x",
3522                     mbox6, mbox1);
3523 #ifdef	ISP_FW_CRASH_DUMP
3524 		/*
3525 		 * XXX: really need a thread to do this right.
3526 		 */
3527 		if (IS_FC(isp)) {
3528 			FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3529 			FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3530 			isp_freeze_loopdown(isp, "f/w crash");
3531 			isp_fw_dump(isp);
3532 		}
3533 		isp_reinit(isp);
3534 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3535 #endif
3536 		break;
3537 	}
3538 	case ISPASYNC_UNHANDLED_RESPONSE:
3539 		break;
3540 	default:
3541 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3542 		break;
3543 	}
3544 	return (rv);
3545 }
3546 
3547 
3548 /*
3549  * Locks are held before coming here.
3550  */
3551 void
3552 isp_uninit(ispsoftc_t *isp)
3553 {
3554 	if (IS_24XX(isp)) {
3555 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
3556 	} else {
3557 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
3558 	}
3559 	ISP_DISABLE_INTS(isp);
3560 }
3561 
3562 void
3563 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3564 {
3565 	va_list ap;
3566 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3567 		return;
3568 	}
3569 	printf("%s: ", device_get_nameunit(isp->isp_dev));
3570 	va_start(ap, fmt);
3571 	vprintf(fmt, ap);
3572 	va_end(ap);
3573 	printf("\n");
3574 }
3575 
3576 uint64_t
3577 isp_nanotime_sub(struct timespec *b, struct timespec *a)
3578 {
3579 	uint64_t elapsed;
3580 	struct timespec x = *b;
3581 	timespecsub(&x, a);
3582 	elapsed = GET_NANOSEC(&x);
3583 	if (elapsed == 0)
3584 		elapsed++;
3585 	return (elapsed);
3586 }
3587 
3588 int
3589 isp_mbox_acquire(ispsoftc_t *isp)
3590 {
3591 	if (isp->isp_osinfo.mboxbsy) {
3592 		return (1);
3593 	} else {
3594 		isp->isp_osinfo.mboxcmd_done = 0;
3595 		isp->isp_osinfo.mboxbsy = 1;
3596 		return (0);
3597 	}
3598 }
3599 
3600 void
3601 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
3602 {
3603 	unsigned int usecs = mbp->timeout;
3604 	unsigned int max, olim, ilim;
3605 
3606 	if (usecs == 0) {
3607 		usecs = MBCMD_DEFAULT_TIMEOUT;
3608 	}
3609 	max = isp->isp_mbxwrk0 + 1;
3610 
3611 	if (isp->isp_osinfo.mbox_sleep_ok) {
3612 		unsigned int ms = (usecs + 999) / 1000;
3613 
3614 		isp->isp_osinfo.mbox_sleep_ok = 0;
3615 		isp->isp_osinfo.mbox_sleeping = 1;
3616 		for (olim = 0; olim < max; olim++) {
3617 #if __FreeBSD_version < 500000  || !defined(ISP_SMPLOCK)
3618 			tsleep(&isp->isp_mbxworkp, PRIBIO, "ispmbx_sleep",
3619 			    isp_mstohz(ms));
3620 #else
3621 			msleep(&isp->isp_mbxworkp, &isp->isp_mtx, PRIBIO,
3622 			    "ispmbx_sleep", isp_mstohz(ms));
3623 #endif
3624 			if (isp->isp_osinfo.mboxcmd_done) {
3625 				break;
3626 			}
3627 		}
3628 		isp->isp_osinfo.mbox_sleep_ok = 1;
3629 		isp->isp_osinfo.mbox_sleeping = 0;
3630 	} else {
3631 		for (olim = 0; olim < max; olim++) {
3632 			for (ilim = 0; ilim < usecs; ilim += 100) {
3633 				uint32_t isr;
3634 				uint16_t sema, mbox;
3635 				if (isp->isp_osinfo.mboxcmd_done) {
3636 					break;
3637 				}
3638 				if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3639 					isp_intr(isp, isr, sema, mbox);
3640 					if (isp->isp_osinfo.mboxcmd_done) {
3641 						break;
3642 					}
3643 				}
3644 				USEC_DELAY(100);
3645 			}
3646 			if (isp->isp_osinfo.mboxcmd_done) {
3647 				break;
3648 			}
3649 		}
3650 	}
3651 	if (isp->isp_osinfo.mboxcmd_done == 0) {
3652 		isp_prt(isp, ISP_LOGWARN,
3653 		    "%s Mailbox Command (0x%x) Timeout",
3654 		    isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
3655 		    isp->isp_lastmbxcmd);
3656 		mbp->param[0] = MBOX_TIMEOUT;
3657 		isp->isp_osinfo.mboxcmd_done = 1;
3658 	}
3659 }
3660 
3661 void
3662 isp_mbox_notify_done(ispsoftc_t *isp)
3663 {
3664 	if (isp->isp_osinfo.mbox_sleeping) {
3665 		wakeup(&isp->isp_mbxworkp);
3666 	}
3667 	isp->isp_osinfo.mboxcmd_done = 1;
3668 }
3669 
3670 void
3671 isp_mbox_release(ispsoftc_t *isp)
3672 {
3673 	isp->isp_osinfo.mboxbsy = 0;
3674 }
3675 
3676 int
3677 isp_mstohz(int ms)
3678 {
3679 	int hz;
3680 	struct timeval t;
3681 	t.tv_sec = ms / 1000;
3682 	t.tv_usec = (ms % 1000) * 1000;
3683 	hz = tvtohz(&t);
3684 	if (hz < 0) {
3685 		hz = 0x7fffffff;
3686 	}
3687 	if (hz == 0) {
3688 		hz = 1;
3689 	}
3690 	return (hz);
3691 }
3692