xref: /freebsd/sys/dev/isp/isp_freebsd.c (revision 68344a95478e7f7a30cc2d93820595aad7e7bc5b)
1 /* $FreeBSD$ */
2 /*
3  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
4  *
5  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <dev/isp/isp_freebsd.h>
29 #include <sys/unistd.h>
30 #include <sys/kthread.h>
31 #include <machine/stdarg.h>	/* for use by isp_prt below */
32 #include <sys/conf.h>
33 #include <sys/ioccom.h>
34 #include <dev/isp/isp_ioctl.h>
35 
36 
37 static d_ioctl_t ispioctl;
38 static void isp_intr_enable(void *);
39 static void isp_cam_async(void *, u_int32_t, struct cam_path *, void *);
40 static void isp_poll(struct cam_sim *);
41 #if	0
42 static void isp_relsim(void *);
43 #endif
44 static timeout_t isp_watchdog;
45 static void isp_kthread(void *);
46 static void isp_action(struct cam_sim *, union ccb *);
47 
48 
49 #define ISP_CDEV_MAJOR	248
50 static struct cdevsw isp_cdevsw = {
51 	/* open */	nullopen,
52 	/* close */	nullclose,
53 	/* read */	noread,
54 	/* write */	nowrite,
55 	/* ioctl */	ispioctl,
56 	/* poll */	nopoll,
57 	/* mmap */	nommap,
58 	/* strategy */	nostrategy,
59 	/* name */	"isp",
60 	/* maj */	ISP_CDEV_MAJOR,
61 	/* dump */	nodump,
62 	/* psize */	nopsize,
63 	/* flags */	D_TAPE,
64 };
65 
66 static struct ispsoftc *isplist = NULL;
67 
68 void
69 isp_attach(struct ispsoftc *isp)
70 {
71 	int primary, secondary;
72 	struct ccb_setasync csa;
73 	struct cam_devq *devq;
74 	struct cam_sim *sim;
75 	struct cam_path *path;
76 
77 	/*
78 	 * Establish (in case of 12X0) which bus is the primary.
79 	 */
80 
81 	primary = 0;
82 	secondary = 1;
83 
84 	/*
85 	 * Create the device queue for our SIM(s).
86 	 */
87 	devq = cam_simq_alloc(isp->isp_maxcmds);
88 	if (devq == NULL) {
89 		return;
90 	}
91 
92 	/*
93 	 * Construct our SIM entry.
94 	 */
95 	ISPLOCK_2_CAMLOCK(isp);
96 	sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
97 	    device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
98 	if (sim == NULL) {
99 		cam_simq_free(devq);
100 		CAMLOCK_2_ISPLOCK(isp);
101 		return;
102 	}
103 	CAMLOCK_2_ISPLOCK(isp);
104 
105 	isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
106 	isp->isp_osinfo.ehook.ich_arg = isp;
107 	ISPLOCK_2_CAMLOCK(isp);
108 	if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
109 		cam_sim_free(sim, TRUE);
110 		CAMLOCK_2_ISPLOCK(isp);
111 		isp_prt(isp, ISP_LOGERR,
112 		    "could not establish interrupt enable hook");
113 		return;
114 	}
115 
116 	if (xpt_bus_register(sim, primary) != CAM_SUCCESS) {
117 		cam_sim_free(sim, TRUE);
118 		CAMLOCK_2_ISPLOCK(isp);
119 		return;
120 	}
121 
122 	if (xpt_create_path(&path, NULL, cam_sim_path(sim),
123 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
124 		xpt_bus_deregister(cam_sim_path(sim));
125 		cam_sim_free(sim, TRUE);
126 		config_intrhook_disestablish(&isp->isp_osinfo.ehook);
127 		CAMLOCK_2_ISPLOCK(isp);
128 		return;
129 	}
130 
131 	xpt_setup_ccb(&csa.ccb_h, path, 5);
132 	csa.ccb_h.func_code = XPT_SASYNC_CB;
133 	csa.event_enable = AC_LOST_DEVICE;
134 	csa.callback = isp_cam_async;
135 	csa.callback_arg = sim;
136 	xpt_action((union ccb *)&csa);
137 	CAMLOCK_2_ISPLOCK(isp);
138 	isp->isp_sim = sim;
139 	isp->isp_path = path;
140 	/*
141 	 * Create a kernel thread for fibre channel instances. We
142 	 * don't have dual channel FC cards.
143 	 */
144 	if (IS_FC(isp)) {
145 		ISPLOCK_2_CAMLOCK(isp);
146 		/* XXX: LOCK VIOLATION */
147 		cv_init(&isp->isp_osinfo.kthread_cv, "isp_kthread_cv");
148 		if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
149 		    RFHIGHPID, "%s: fc_thrd",
150 		    device_get_nameunit(isp->isp_dev))) {
151 			xpt_bus_deregister(cam_sim_path(sim));
152 			cam_sim_free(sim, TRUE);
153 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
154 			CAMLOCK_2_ISPLOCK(isp);
155 			isp_prt(isp, ISP_LOGERR, "could not create kthread");
156 			return;
157 		}
158 		CAMLOCK_2_ISPLOCK(isp);
159 	}
160 
161 
162 	/*
163 	 * If we have a second channel, construct SIM entry for that.
164 	 */
165 	if (IS_DUALBUS(isp)) {
166 		ISPLOCK_2_CAMLOCK(isp);
167 		sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
168 		    device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
169 		if (sim == NULL) {
170 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
171 			xpt_free_path(isp->isp_path);
172 			cam_simq_free(devq);
173 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
174 			return;
175 		}
176 		if (xpt_bus_register(sim, secondary) != CAM_SUCCESS) {
177 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
178 			xpt_free_path(isp->isp_path);
179 			cam_sim_free(sim, TRUE);
180 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
181 			CAMLOCK_2_ISPLOCK(isp);
182 			return;
183 		}
184 
185 		if (xpt_create_path(&path, NULL, cam_sim_path(sim),
186 		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
187 			xpt_bus_deregister(cam_sim_path(isp->isp_sim));
188 			xpt_free_path(isp->isp_path);
189 			xpt_bus_deregister(cam_sim_path(sim));
190 			cam_sim_free(sim, TRUE);
191 			config_intrhook_disestablish(&isp->isp_osinfo.ehook);
192 			CAMLOCK_2_ISPLOCK(isp);
193 			return;
194 		}
195 
196 		xpt_setup_ccb(&csa.ccb_h, path, 5);
197 		csa.ccb_h.func_code = XPT_SASYNC_CB;
198 		csa.event_enable = AC_LOST_DEVICE;
199 		csa.callback = isp_cam_async;
200 		csa.callback_arg = sim;
201 		xpt_action((union ccb *)&csa);
202 		CAMLOCK_2_ISPLOCK(isp);
203 		isp->isp_sim2 = sim;
204 		isp->isp_path2 = path;
205 	}
206 
207 #ifdef	ISP_TARGET_MODE
208 	cv_init(&isp->isp_osinfo.tgtcv0[0], "isp_tgcv0a");
209 	cv_init(&isp->isp_osinfo.tgtcv0[1], "isp_tgcv0b");
210 	cv_init(&isp->isp_osinfo.tgtcv1[0], "isp_tgcv1a");
211 	cv_init(&isp->isp_osinfo.tgtcv1[1], "isp_tgcv1b");
212 #endif
213 	/*
214 	 * Create device nodes
215 	 */
216 	(void) make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
217 	    GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
218 
219 	if (isp->isp_role != ISP_ROLE_NONE) {
220 		isp->isp_state = ISP_RUNSTATE;
221 		ENABLE_INTS(isp);
222 	}
223 	if (isplist == NULL) {
224 		isplist = isp;
225 	} else {
226 		struct ispsoftc *tmp = isplist;
227 		while (tmp->isp_osinfo.next) {
228 			tmp = tmp->isp_osinfo.next;
229 		}
230 		tmp->isp_osinfo.next = isp;
231 	}
232 
233 }
234 
235 static int
236 ispioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
237 {
238 	struct ispsoftc *isp;
239 	int retval = ENOTTY;
240 
241 	isp = isplist;
242 	while (isp) {
243 		if (minor(dev) == device_get_unit(isp->isp_dev)) {
244 			break;
245 		}
246 		isp = isp->isp_osinfo.next;
247 	}
248 	if (isp == NULL)
249 		return (ENXIO);
250 
251 	switch (cmd) {
252 	case ISP_SDBLEV:
253 	{
254 		int olddblev = isp->isp_dblev;
255 		isp->isp_dblev = *(int *)addr;
256 		*(int *)addr = olddblev;
257 		retval = 0;
258 		break;
259 	}
260 	case ISP_RESETHBA:
261 		ISP_LOCK(isp);
262 		isp_reinit(isp);
263 		ISP_UNLOCK(isp);
264 		retval = 0;
265 		break;
266 	case ISP_FC_RESCAN:
267 		if (IS_FC(isp)) {
268 			ISP_LOCK(isp);
269 			if (isp_fc_runstate(isp, 5 * 1000000)) {
270 				retval = EIO;
271 			} else {
272 				retval = 0;
273 			}
274 			ISP_UNLOCK(isp);
275 		}
276 		break;
277 	case ISP_FC_LIP:
278 		if (IS_FC(isp)) {
279 			ISP_LOCK(isp);
280 			if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
281 				retval = EIO;
282 			} else {
283 				retval = 0;
284 			}
285 			ISP_UNLOCK(isp);
286 		}
287 		break;
288 	case ISP_FC_GETDINFO:
289 	{
290 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
291 		struct lportdb *lp;
292 
293 		if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
294 			retval = EINVAL;
295 			break;
296 		}
297 		ISP_LOCK(isp);
298 		lp = &FCPARAM(isp)->portdb[ifc->loopid];
299 		if (lp->valid) {
300 			ifc->loopid = lp->loopid;
301 			ifc->portid = lp->portid;
302 			ifc->node_wwn = lp->node_wwn;
303 			ifc->port_wwn = lp->port_wwn;
304 			retval = 0;
305 		} else {
306 			retval = ENODEV;
307 		}
308 		ISP_UNLOCK(isp);
309 		break;
310 	}
311 	default:
312 		break;
313 	}
314 	return (retval);
315 }
316 
317 static void
318 isp_intr_enable(void *arg)
319 {
320 	struct ispsoftc *isp = arg;
321 	if (isp->isp_role != ISP_ROLE_NONE) {
322 		ENABLE_INTS(isp);
323 		isp->isp_osinfo.intsok = 1;
324 	}
325 	/* Release our hook so that the boot can continue. */
326 	config_intrhook_disestablish(&isp->isp_osinfo.ehook);
327 }
328 
329 /*
330  * Put the target mode functions here, because some are inlines
331  */
332 
333 #ifdef	ISP_TARGET_MODE
334 
335 static __inline int is_lun_enabled(struct ispsoftc *, int, lun_id_t);
336 static __inline int are_any_luns_enabled(struct ispsoftc *, int);
337 static __inline tstate_t *get_lun_statep(struct ispsoftc *, int, lun_id_t);
338 static __inline void rls_lun_statep(struct ispsoftc *, tstate_t *);
339 static __inline int isp_psema_sig_rqe(struct ispsoftc *, int);
340 static __inline int isp_cv_wait_timed_rqe(struct ispsoftc *, int, int);
341 static __inline void isp_cv_signal_rqe(struct ispsoftc *, int, int);
342 static __inline void isp_vsema_rqe(struct ispsoftc *, int);
343 static __inline atio_private_data_t *isp_get_atpd(struct ispsoftc *, int);
344 static cam_status
345 create_lun_state(struct ispsoftc *, int, struct cam_path *, tstate_t **);
346 static void destroy_lun_state(struct ispsoftc *, tstate_t *);
347 static void isp_en_lun(struct ispsoftc *, union ccb *);
348 static cam_status isp_abort_tgt_ccb(struct ispsoftc *, union ccb *);
349 static timeout_t isp_refire_putback_atio;
350 static void isp_complete_ctio(union ccb *);
351 static void isp_target_putback_atio(union ccb *);
352 static cam_status isp_target_start_ctio(struct ispsoftc *, union ccb *);
353 static int isp_handle_platform_atio(struct ispsoftc *, at_entry_t *);
354 static int isp_handle_platform_atio2(struct ispsoftc *, at2_entry_t *);
355 static int isp_handle_platform_ctio(struct ispsoftc *, void *);
356 
357 static __inline int
358 is_lun_enabled(struct ispsoftc *isp, int bus, lun_id_t lun)
359 {
360 	tstate_t *tptr;
361 	tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
362 	if (tptr == NULL) {
363 		return (0);
364 	}
365 	do {
366 		if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
367 			return (1);
368 		}
369 	} while ((tptr = tptr->next) != NULL);
370 	return (0);
371 }
372 
373 static __inline int
374 are_any_luns_enabled(struct ispsoftc *isp, int port)
375 {
376 	int lo, hi;
377 	if (IS_DUALBUS(isp)) {
378 		lo = (port * (LUN_HASH_SIZE >> 1));
379 		hi = lo + (LUN_HASH_SIZE >> 1);
380 	} else {
381 		lo = 0;
382 		hi = LUN_HASH_SIZE;
383 	}
384 	for (lo = 0; lo < hi; lo++) {
385 		if (isp->isp_osinfo.lun_hash[lo]) {
386 			return (1);
387 		}
388 	}
389 	return (0);
390 }
391 
392 static __inline tstate_t *
393 get_lun_statep(struct ispsoftc *isp, int bus, lun_id_t lun)
394 {
395 	tstate_t *tptr = NULL;
396 
397 	if (lun == CAM_LUN_WILDCARD) {
398 		if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
399 			tptr = &isp->isp_osinfo.tsdflt[bus];
400 			tptr->hold++;
401 			return (tptr);
402 		}
403 	} else {
404 		tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
405 		if (tptr == NULL) {
406 			return (NULL);
407 		}
408 	}
409 
410 	do {
411 		if (tptr->lun == lun && tptr->bus == bus) {
412 			tptr->hold++;
413 			return (tptr);
414 		}
415 	} while ((tptr = tptr->next) != NULL);
416 	return (tptr);
417 }
418 
419 static __inline void
420 rls_lun_statep(struct ispsoftc *isp, tstate_t *tptr)
421 {
422 	if (tptr->hold)
423 		tptr->hold--;
424 }
425 
426 static __inline int
427 isp_psema_sig_rqe(struct ispsoftc *isp, int bus)
428 {
429 	while (isp->isp_osinfo.tmflags[bus] & TM_BUSY) {
430 		isp->isp_osinfo.tmflags[bus] |= TM_WANTED;
431 		if (cv_wait_sig(&isp->isp_osinfo.tgtcv0[bus], &isp->isp_lock)) {
432 			return (-1);
433 		}
434 		isp->isp_osinfo.tmflags[bus] |= TM_BUSY;
435 	}
436 	return (0);
437 }
438 
439 static __inline int
440 isp_cv_wait_timed_rqe(struct ispsoftc *isp, int bus, int timo)
441 {
442 	if (cv_timedwait(&isp->isp_osinfo.tgtcv1[bus], &isp->isp_lock, timo)) {
443 		return (-1);
444 	}
445 	return (0);
446 }
447 
448 static __inline void
449 isp_cv_signal_rqe(struct ispsoftc *isp, int bus, int status)
450 {
451 	isp->isp_osinfo.rstatus[bus] = status;
452 	cv_signal(&isp->isp_osinfo.tgtcv1[bus]);
453 }
454 
455 static __inline void
456 isp_vsema_rqe(struct ispsoftc *isp, int bus)
457 {
458 	if (isp->isp_osinfo.tmflags[bus] & TM_WANTED) {
459 		isp->isp_osinfo.tmflags[bus] &= ~TM_WANTED;
460 		cv_signal(&isp->isp_osinfo.tgtcv0[bus]);
461 	}
462 	isp->isp_osinfo.tmflags[bus] &= ~TM_BUSY;
463 }
464 
465 static __inline atio_private_data_t *
466 isp_get_atpd(struct ispsoftc *isp, int tag)
467 {
468 	atio_private_data_t *atp;
469 	for (atp = isp->isp_osinfo.atpdp;
470 	    atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
471 		if (atp->tag == tag)
472 			return (atp);
473 	}
474 	return (NULL);
475 }
476 
477 static cam_status
478 create_lun_state(struct ispsoftc *isp, int bus,
479     struct cam_path *path, tstate_t **rslt)
480 {
481 	cam_status status;
482 	lun_id_t lun;
483 	int hfx;
484 	tstate_t *tptr, *new;
485 
486 	lun = xpt_path_lun_id(path);
487 	if (lun < 0) {
488 		return (CAM_LUN_INVALID);
489 	}
490 	if (is_lun_enabled(isp, bus, lun)) {
491 		return (CAM_LUN_ALRDY_ENA);
492 	}
493 	new = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
494 	if (new == NULL) {
495 		return (CAM_RESRC_UNAVAIL);
496 	}
497 
498 	status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
499 	    xpt_path_target_id(path), xpt_path_lun_id(path));
500 	if (status != CAM_REQ_CMP) {
501 		free(new, M_DEVBUF);
502 		return (status);
503 	}
504 	new->bus = bus;
505 	new->lun = lun;
506 	SLIST_INIT(&new->atios);
507 	SLIST_INIT(&new->inots);
508 	new->hold = 1;
509 
510 	hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
511 	tptr = isp->isp_osinfo.lun_hash[hfx];
512 	if (tptr == NULL) {
513 		isp->isp_osinfo.lun_hash[hfx] = new;
514 	} else {
515 		while (tptr->next)
516 			tptr = tptr->next;
517 		tptr->next = new;
518 	}
519 	*rslt = new;
520 	return (CAM_REQ_CMP);
521 }
522 
523 static __inline void
524 destroy_lun_state(struct ispsoftc *isp, tstate_t *tptr)
525 {
526 	int hfx;
527 	tstate_t *lw, *pw;
528 
529 	hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
530 	if (tptr->hold) {
531 		return;
532 	}
533 	pw = isp->isp_osinfo.lun_hash[hfx];
534 	if (pw == NULL) {
535 		return;
536 	} else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
537 		isp->isp_osinfo.lun_hash[hfx] = pw->next;
538 	} else {
539 		lw = pw;
540 		pw = lw->next;
541 		while (pw) {
542 			if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
543 				lw->next = pw->next;
544 				break;
545 			}
546 			lw = pw;
547 			pw = pw->next;
548 		}
549 		if (pw == NULL) {
550 			return;
551 		}
552 	}
553 	free(tptr, M_DEVBUF);
554 }
555 
556 /*
557  * we enter with our locks held.
558  */
559 static void
560 isp_en_lun(struct ispsoftc *isp, union ccb *ccb)
561 {
562 	const char lfmt[] = "Lun now %sabled for target mode on channel %d";
563 	struct ccb_en_lun *cel = &ccb->cel;
564 	tstate_t *tptr;
565 	u_int16_t rstat;
566 	int bus, cmd, av, wildcard;
567 	lun_id_t lun;
568 	target_id_t tgt;
569 
570 
571 	bus = XS_CHANNEL(ccb) & 0x1;
572 	tgt = ccb->ccb_h.target_id;
573 	lun = ccb->ccb_h.target_lun;
574 
575 	/*
576 	 * Do some sanity checking first.
577 	 */
578 
579 	if ((lun != CAM_LUN_WILDCARD) &&
580 	    (lun < 0 || lun >= (lun_id_t) isp->isp_maxluns)) {
581 		ccb->ccb_h.status = CAM_LUN_INVALID;
582 		return;
583 	}
584 
585 	if (IS_SCSI(isp)) {
586 		sdparam *sdp = isp->isp_param;
587 		sdp += bus;
588 		if (tgt != CAM_TARGET_WILDCARD &&
589 		    tgt != sdp->isp_initiator_id) {
590 			ccb->ccb_h.status = CAM_TID_INVALID;
591 			return;
592 		}
593 	} else {
594 		if (tgt != CAM_TARGET_WILDCARD &&
595 		    tgt != FCPARAM(isp)->isp_iid) {
596 			ccb->ccb_h.status = CAM_TID_INVALID;
597 			return;
598 		}
599 		/*
600 		 * This is as a good a place as any to check f/w capabilities.
601 		 */
602 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_TMODE) == 0) {
603 			isp_prt(isp, ISP_LOGERR,
604 			    "firmware does not support target mode");
605 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
606 			return;
607 		}
608 		/*
609 		 * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
610 		 * XXX: dorks with our already fragile enable/disable code.
611 		 */
612 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
613 			isp_prt(isp, ISP_LOGERR,
614 			    "firmware not SCCLUN capable");
615 		}
616 	}
617 
618 	if (tgt == CAM_TARGET_WILDCARD) {
619 		if (lun == CAM_LUN_WILDCARD) {
620 			wildcard = 1;
621 		} else {
622 			ccb->ccb_h.status = CAM_LUN_INVALID;
623 			return;
624 		}
625 	} else {
626 		wildcard = 0;
627 	}
628 
629 	/*
630 	 * Next check to see whether this is a target/lun wildcard action.
631 	 *
632 	 * If so, we know that we can accept commands for luns that haven't
633 	 * been enabled yet and send them upstream. Otherwise, we have to
634 	 * handle them locally (if we see them at all).
635 	 */
636 
637 	if (wildcard) {
638 		tptr = &isp->isp_osinfo.tsdflt[bus];
639 		if (cel->enable) {
640 			if (isp->isp_osinfo.tmflags[bus] &
641 			    TM_WILDCARD_ENABLED) {
642 				ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
643 				return;
644 			}
645 			ccb->ccb_h.status =
646 			    xpt_create_path(&tptr->owner, NULL,
647 			    xpt_path_path_id(ccb->ccb_h.path),
648 			    xpt_path_target_id(ccb->ccb_h.path),
649 			    xpt_path_lun_id(ccb->ccb_h.path));
650 			if (ccb->ccb_h.status != CAM_REQ_CMP) {
651 				return;
652 			}
653 			SLIST_INIT(&tptr->atios);
654 			SLIST_INIT(&tptr->inots);
655 			isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
656 		} else {
657 			if ((isp->isp_osinfo.tmflags[bus] &
658 			    TM_WILDCARD_ENABLED) == 0) {
659 				ccb->ccb_h.status = CAM_REQ_CMP;
660 				return;
661 			}
662 			if (tptr->hold) {
663 				ccb->ccb_h.status = CAM_SCSI_BUSY;
664 				return;
665 			}
666 			xpt_free_path(tptr->owner);
667 			isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
668 		}
669 	}
670 
671 	/*
672 	 * Now check to see whether this bus needs to be
673 	 * enabled/disabled with respect to target mode.
674 	 */
675 	av = bus << 31;
676 	if (cel->enable && !(isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED)) {
677 		av |= ENABLE_TARGET_FLAG;
678 		av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
679 		if (av) {
680 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
681 			if (wildcard) {
682 				isp->isp_osinfo.tmflags[bus] &=
683 				    ~TM_WILDCARD_ENABLED;
684 				xpt_free_path(tptr->owner);
685 			}
686 			return;
687 		}
688 		isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
689 		isp_prt(isp, ISP_LOGINFO,
690 		    "Target Mode enabled on channel %d", bus);
691 	} else if (cel->enable == 0 &&
692 	    (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) && wildcard) {
693 		if (are_any_luns_enabled(isp, bus)) {
694 			ccb->ccb_h.status = CAM_SCSI_BUSY;
695 			return;
696 		}
697 		av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
698 		if (av) {
699 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
700 			return;
701 		}
702 		isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
703 		isp_prt(isp, ISP_LOGINFO,
704 		    "Target Mode disabled on channel %d", bus);
705 	}
706 
707 	if (wildcard) {
708 		ccb->ccb_h.status = CAM_REQ_CMP;
709 		return;
710 	}
711 
712 	if (cel->enable) {
713 		ccb->ccb_h.status =
714 		    create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
715 		if (ccb->ccb_h.status != CAM_REQ_CMP) {
716 			return;
717 		}
718 	} else {
719 		tptr = get_lun_statep(isp, bus, lun);
720 		if (tptr == NULL) {
721 			ccb->ccb_h.status = CAM_LUN_INVALID;
722 			return;
723 		}
724 	}
725 
726 	if (isp_psema_sig_rqe(isp, bus)) {
727 		rls_lun_statep(isp, tptr);
728 		if (cel->enable)
729 			destroy_lun_state(isp, tptr);
730 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
731 		return;
732 	}
733 
734 	if (cel->enable) {
735 		u_int32_t seq = isp->isp_osinfo.rollinfo++;
736 		int c, n, ulun = lun;
737 
738 		cmd = RQSTYPE_ENABLE_LUN;
739 		c = DFLT_CMND_CNT;
740 		n = DFLT_INOT_CNT;
741 		if (IS_FC(isp) && lun != 0) {
742 			cmd = RQSTYPE_MODIFY_LUN;
743 			n = 0;
744 			/*
745 		 	 * For SCC firmware, we only deal with setting
746 			 * (enabling or modifying) lun 0.
747 			 */
748 			ulun = 0;
749 		}
750 		rstat = LUN_ERR;
751 		if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
752 			xpt_print_path(ccb->ccb_h.path);
753 			isp_prt(isp, ISP_LOGWARN, "isp_lun_cmd failed");
754 			goto out;
755 		}
756 		if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
757 			xpt_print_path(ccb->ccb_h.path);
758 			isp_prt(isp, ISP_LOGERR,
759 			    "wait for ENABLE/MODIFY LUN timed out");
760 			goto out;
761 		}
762 		rstat = isp->isp_osinfo.rstatus[bus];
763 		if (rstat != LUN_OK) {
764 			xpt_print_path(ccb->ccb_h.path);
765 			isp_prt(isp, ISP_LOGERR,
766 			    "ENABLE/MODIFY LUN returned 0x%x", rstat);
767 			goto out;
768 		}
769 	} else {
770 		int c, n, ulun = lun;
771 		u_int32_t seq;
772 
773 		rstat = LUN_ERR;
774 		seq = isp->isp_osinfo.rollinfo++;
775 		cmd = -RQSTYPE_MODIFY_LUN;
776 
777 		c = DFLT_CMND_CNT;
778 		n = DFLT_INOT_CNT;
779 		if (IS_FC(isp) && lun != 0) {
780 			n = 0;
781 			/*
782 		 	 * For SCC firmware, we only deal with setting
783 			 * (enabling or modifying) lun 0.
784 			 */
785 			ulun = 0;
786 		}
787 		if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
788 			xpt_print_path(ccb->ccb_h.path);
789 			isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
790 			goto out;
791 		}
792 		if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
793 			xpt_print_path(ccb->ccb_h.path);
794 			isp_prt(isp, ISP_LOGERR,
795 			    "wait for MODIFY LUN timed out");
796 			goto out;
797 		}
798 		rstat = isp->isp_osinfo.rstatus[bus];
799 		if (rstat != LUN_OK) {
800 			xpt_print_path(ccb->ccb_h.path);
801 			isp_prt(isp, ISP_LOGERR,
802 			    "MODIFY LUN returned 0x%x", rstat);
803 			goto out;
804 		}
805 		if (IS_FC(isp) && lun) {
806 			goto out;
807 		}
808 
809 		seq = isp->isp_osinfo.rollinfo++;
810 
811 		rstat = LUN_ERR;
812 		cmd = -RQSTYPE_ENABLE_LUN;
813 		if (isp_lun_cmd(isp, cmd, bus, tgt, lun, 0, 0, seq)) {
814 			xpt_print_path(ccb->ccb_h.path);
815 			isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
816 			goto out;
817 		}
818 		if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
819 			xpt_print_path(ccb->ccb_h.path);
820 			isp_prt(isp, ISP_LOGERR,
821 			     "wait for DISABLE LUN timed out");
822 			goto out;
823 		}
824 		rstat = isp->isp_osinfo.rstatus[bus];
825 		if (rstat != LUN_OK) {
826 			xpt_print_path(ccb->ccb_h.path);
827 			isp_prt(isp, ISP_LOGWARN,
828 			    "DISABLE LUN returned 0x%x", rstat);
829 			goto out;
830 		}
831 		if (are_any_luns_enabled(isp, bus) == 0) {
832 			av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
833 			if (av) {
834 				isp_prt(isp, ISP_LOGWARN,
835 				    "disable target mode on channel %d failed",
836 				    bus);
837 				goto out;
838 			}
839 			isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
840 			xpt_print_path(ccb->ccb_h.path);
841 			isp_prt(isp, ISP_LOGINFO,
842 			    "Target Mode disabled on channel %d", bus);
843 		}
844 	}
845 
846 out:
847 	isp_vsema_rqe(isp, bus);
848 
849 	if (rstat != LUN_OK) {
850 		xpt_print_path(ccb->ccb_h.path);
851 		isp_prt(isp, ISP_LOGWARN,
852 		    "lun %sable failed", (cel->enable) ? "en" : "dis");
853 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
854 		rls_lun_statep(isp, tptr);
855 		if (cel->enable)
856 			destroy_lun_state(isp, tptr);
857 	} else {
858 		xpt_print_path(ccb->ccb_h.path);
859 		isp_prt(isp, ISP_LOGINFO, lfmt,
860 		    (cel->enable) ? "en" : "dis", bus);
861 		rls_lun_statep(isp, tptr);
862 		if (cel->enable == 0) {
863 			destroy_lun_state(isp, tptr);
864 		}
865 		ccb->ccb_h.status = CAM_REQ_CMP;
866 	}
867 }
868 
869 static cam_status
870 isp_abort_tgt_ccb(struct ispsoftc *isp, union ccb *ccb)
871 {
872 	tstate_t *tptr;
873 	struct ccb_hdr_slist *lp;
874 	struct ccb_hdr *curelm;
875 	int found;
876 	union ccb *accb = ccb->cab.abort_ccb;
877 
878 	if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
879 		if (IS_FC(isp) && (accb->ccb_h.target_id !=
880 		    ((fcparam *) isp->isp_param)->isp_loopid)) {
881 			return (CAM_PATH_INVALID);
882 		} else if (IS_SCSI(isp) && (accb->ccb_h.target_id !=
883 		    ((sdparam *) isp->isp_param)->isp_initiator_id)) {
884 			return (CAM_PATH_INVALID);
885 		}
886 	}
887 	tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
888 	if (tptr == NULL) {
889 		return (CAM_PATH_INVALID);
890 	}
891 	if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
892 		lp = &tptr->atios;
893 	} else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
894 		lp = &tptr->inots;
895 	} else {
896 		rls_lun_statep(isp, tptr);
897 		return (CAM_UA_ABORT);
898 	}
899 	curelm = SLIST_FIRST(lp);
900 	found = 0;
901 	if (curelm == &accb->ccb_h) {
902 		found = 1;
903 		SLIST_REMOVE_HEAD(lp, sim_links.sle);
904 	} else {
905 		while(curelm != NULL) {
906 			struct ccb_hdr *nextelm;
907 
908 			nextelm = SLIST_NEXT(curelm, sim_links.sle);
909 			if (nextelm == &accb->ccb_h) {
910 				found = 1;
911 				SLIST_NEXT(curelm, sim_links.sle) =
912 				    SLIST_NEXT(nextelm, sim_links.sle);
913 				break;
914 			}
915 			curelm = nextelm;
916 		}
917 	}
918 	rls_lun_statep(isp, tptr);
919 	if (found) {
920 		accb->ccb_h.status = CAM_REQ_ABORTED;
921 		return (CAM_REQ_CMP);
922 	}
923 	return(CAM_PATH_INVALID);
924 }
925 
926 static cam_status
927 isp_target_start_ctio(struct ispsoftc *isp, union ccb *ccb)
928 {
929 	void *qe;
930 	struct ccb_scsiio *cso = &ccb->csio;
931 	u_int16_t *hp, save_handle;
932 	u_int16_t iptr, optr;
933 
934 
935 	if (isp_getrqentry(isp, &iptr, &optr, &qe)) {
936 		xpt_print_path(ccb->ccb_h.path);
937 		printf("Request Queue Overflow in isp_target_start_ctio\n");
938 		return (CAM_RESRC_UNAVAIL);
939 	}
940 	bzero(qe, QENTRY_LEN);
941 
942 	/*
943 	 * We're either moving data or completing a command here.
944 	 */
945 
946 	if (IS_FC(isp)) {
947 		atio_private_data_t *atp;
948 		ct2_entry_t *cto = qe;
949 
950 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
951 		cto->ct_header.rqs_entry_count = 1;
952 		cto->ct_iid = cso->init_id;
953 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
954 			cto->ct_lun = ccb->ccb_h.target_lun;
955 		}
956 
957 		atp = isp_get_atpd(isp, cso->tag_id);
958 		if (atp == NULL) {
959 			panic("cannot find private data adjunct for tag %x",
960 			    cso->tag_id);
961 		}
962 
963 		cto->ct_rxid = cso->tag_id;
964 		if (cso->dxfer_len == 0) {
965 			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
966 			if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
967 				cto->ct_flags |= CT2_SENDSTATUS;
968 				cto->rsp.m1.ct_scsi_status = cso->scsi_status;
969 				cto->ct_resid =
970 				    atp->orig_datalen - atp->bytes_xfered;
971 			}
972 			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
973 				int m = min(cso->sense_len, MAXRESPLEN);
974 				bcopy(&cso->sense_data, cto->rsp.m1.ct_resp, m);
975 				cto->rsp.m1.ct_senselen = m;
976 				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
977 			}
978 		} else {
979 			cto->ct_flags |= CT2_FLAG_MODE0;
980 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
981 				cto->ct_flags |= CT2_DATA_IN;
982 			} else {
983 				cto->ct_flags |= CT2_DATA_OUT;
984 			}
985 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
986 				cto->ct_flags |= CT2_SENDSTATUS;
987 				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
988 				cto->ct_resid =
989 				    atp->orig_datalen -
990 				    (atp->bytes_xfered + cso->dxfer_len);
991 			} else {
992 				atp->last_xframt = cso->dxfer_len;
993 			}
994 			/*
995 			 * If we're sending data and status back together,
996 			 * we can't also send back sense data as well.
997 			 */
998 			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
999 		}
1000 
1001 		if (cto->ct_flags & CT2_SENDSTATUS) {
1002 			isp_prt(isp, ISP_LOGTDEBUG0,
1003 			    "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1004 			    cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1005 			    cso->dxfer_len, cto->ct_resid);
1006 			cto->ct_flags |= CT2_CCINCR;
1007 		}
1008 		cto->ct_timeout = 10;
1009 		hp = &cto->ct_syshandle;
1010 	} else {
1011 		ct_entry_t *cto = qe;
1012 
1013 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1014 		cto->ct_header.rqs_entry_count = 1;
1015 		cto->ct_iid = cso->init_id;
1016 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1017 		cto->ct_tgt = ccb->ccb_h.target_id;
1018 		cto->ct_lun = ccb->ccb_h.target_lun;
1019 		cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1020 		if (AT_HAS_TAG(cso->tag_id)) {
1021 			cto->ct_tag_val = (u_int8_t) AT_GET_TAG(cso->tag_id);
1022 			cto->ct_flags |= CT_TQAE;
1023 		}
1024 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1025 			cto->ct_flags |= CT_NODISC;
1026 		}
1027 		if (cso->dxfer_len == 0) {
1028 			cto->ct_flags |= CT_NO_DATA;
1029 		} else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1030 			cto->ct_flags |= CT_DATA_IN;
1031 		} else {
1032 			cto->ct_flags |= CT_DATA_OUT;
1033 		}
1034 		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1035 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1036 			cto->ct_scsi_status = cso->scsi_status;
1037 			cto->ct_resid = cso->resid;
1038 			isp_prt(isp, ISP_LOGTDEBUG0,
1039 			    "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1040 			    cto->ct_fwhandle, cso->scsi_status, cso->resid,
1041 			    cso->tag_id);
1042 		}
1043 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1044 		cto->ct_timeout = 10;
1045 		hp = &cto->ct_syshandle;
1046 	}
1047 
1048 	if (isp_save_xs(isp, (XS_T *)ccb, hp)) {
1049 		xpt_print_path(ccb->ccb_h.path);
1050 		printf("No XFLIST pointers for isp_target_start_ctio\n");
1051 		return (CAM_RESRC_UNAVAIL);
1052 	}
1053 
1054 
1055 	/*
1056 	 * Call the dma setup routines for this entry (and any subsequent
1057 	 * CTIOs) if there's data to move, and then tell the f/w it's got
1058 	 * new things to play with. As with isp_start's usage of DMA setup,
1059 	 * any swizzling is done in the machine dependent layer. Because
1060 	 * of this, we put the request onto the queue area first in native
1061 	 * format.
1062 	 */
1063 
1064 	save_handle = *hp;
1065 
1066 	switch (ISP_DMASETUP(isp, cso, qe, &iptr, optr)) {
1067 	case CMD_QUEUED:
1068 		ISP_ADD_REQUEST(isp, iptr);
1069 		return (CAM_REQ_INPROG);
1070 
1071 	case CMD_EAGAIN:
1072 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1073 		isp_destroy_handle(isp, save_handle);
1074 		return (CAM_RESRC_UNAVAIL);
1075 
1076 	default:
1077 		isp_destroy_handle(isp, save_handle);
1078 		return (XS_ERR(ccb));
1079 	}
1080 }
1081 
1082 static void
1083 isp_refire_putback_atio(void *arg)
1084 {
1085 	int s = splcam();
1086 	isp_target_putback_atio(arg);
1087 	splx(s);
1088 }
1089 
1090 static void
1091 isp_target_putback_atio(union ccb *ccb)
1092 {
1093 	struct ispsoftc *isp;
1094 	struct ccb_scsiio *cso;
1095 	u_int16_t iptr, optr;
1096 	void *qe;
1097 
1098 	isp = XS_ISP(ccb);
1099 
1100 	if (isp_getrqentry(isp, &iptr, &optr, &qe)) {
1101 		(void) timeout(isp_refire_putback_atio, ccb, 10);
1102 		isp_prt(isp, ISP_LOGWARN,
1103 		    "isp_target_putback_atio: Request Queue Overflow");
1104 		return;
1105 	}
1106 	bzero(qe, QENTRY_LEN);
1107 	cso = &ccb->csio;
1108 	if (IS_FC(isp)) {
1109 		at2_entry_t *at = qe;
1110 		at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1111 		at->at_header.rqs_entry_count = 1;
1112 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1113 			at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1114 		} else {
1115 			at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1116 		}
1117 		at->at_status = CT_OK;
1118 		at->at_rxid = cso->tag_id;
1119 		ISP_SWIZ_ATIO2(isp, qe, qe);
1120 	} else {
1121 		at_entry_t *at = qe;
1122 		at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1123 		at->at_header.rqs_entry_count = 1;
1124 		at->at_iid = cso->init_id;
1125 		at->at_iid |= XS_CHANNEL(ccb) << 7;
1126 		at->at_tgt = cso->ccb_h.target_id;
1127 		at->at_lun = cso->ccb_h.target_lun;
1128 		at->at_status = CT_OK;
1129 		at->at_tag_val = AT_GET_TAG(cso->tag_id);
1130 		at->at_handle = AT_GET_HANDLE(cso->tag_id);
1131 		ISP_SWIZ_ATIO(isp, qe, qe);
1132 	}
1133 	ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1134 	ISP_ADD_REQUEST(isp, iptr);
1135 	isp_complete_ctio(ccb);
1136 }
1137 
1138 static void
1139 isp_complete_ctio(union ccb *ccb)
1140 {
1141 	struct ispsoftc *isp = XS_ISP(ccb);
1142 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1143 		ccb->ccb_h.status |= CAM_REQ_CMP;
1144 	}
1145 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1146 	if (isp->isp_osinfo.simqfrozen & SIMQFRZ_RESOURCE) {
1147 		isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_RESOURCE;
1148 		if (isp->isp_osinfo.simqfrozen == 0) {
1149 			if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1150 				isp_prt(isp, ISP_LOGDEBUG2, "ctio->relsimq");
1151 				ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1152 			} else {
1153 				isp_prt(isp, ISP_LOGWARN, "ctio->devqfrozen");
1154 			}
1155 		} else {
1156 			isp_prt(isp, ISP_LOGWARN,
1157 			    "ctio->simqfrozen(%x)", isp->isp_osinfo.simqfrozen);
1158 		}
1159 	}
1160 	xpt_done(ccb);
1161 }
1162 
1163 /*
1164  * Handle ATIO stuff that the generic code can't.
1165  * This means handling CDBs.
1166  */
1167 
1168 static int
1169 isp_handle_platform_atio(struct ispsoftc *isp, at_entry_t *aep)
1170 {
1171 	tstate_t *tptr;
1172 	int status, bus, iswildcard;
1173 	struct ccb_accept_tio *atiop;
1174 
1175 	/*
1176 	 * The firmware status (except for the QLTM_SVALID bit)
1177 	 * indicates why this ATIO was sent to us.
1178 	 *
1179 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1180 	 *
1181 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1182 	 * we're still connected on the SCSI bus.
1183 	 */
1184 	status = aep->at_status;
1185 	if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1186 		/*
1187 		 * Bus Phase Sequence error. We should have sense data
1188 		 * suggested by the f/w. I'm not sure quite yet what
1189 		 * to do about this for CAM.
1190 		 */
1191 		isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1192 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1193 		return (0);
1194 	}
1195 	if ((status & ~QLTM_SVALID) != AT_CDB) {
1196 		isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1197 		    status);
1198 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1199 		return (0);
1200 	}
1201 
1202 	bus = GET_BUS_VAL(aep->at_iid);
1203 	tptr = get_lun_statep(isp, bus, aep->at_lun);
1204 	if (tptr == NULL) {
1205 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1206 		iswildcard = 1;
1207 	} else {
1208 		iswildcard = 0;
1209 	}
1210 
1211 	if (tptr == NULL) {
1212 		/*
1213 		 * Because we can't autofeed sense data back with
1214 		 * a command for parallel SCSI, we can't give back
1215 		 * a CHECK CONDITION. We'll give back a BUSY status
1216 		 * instead. This works out okay because the only
1217 		 * time we should, in fact, get this, is in the
1218 		 * case that somebody configured us without the
1219 		 * blackhole driver, so they get what they deserve.
1220 		 */
1221 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1222 		return (0);
1223 	}
1224 
1225 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1226 	if (atiop == NULL) {
1227 		/*
1228 		 * Because we can't autofeed sense data back with
1229 		 * a command for parallel SCSI, we can't give back
1230 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1231 		 * instead. This works out okay because the only time we
1232 		 * should, in fact, get this, is in the case that we've
1233 		 * run out of ATIOS.
1234 		 */
1235 		xpt_print_path(tptr->owner);
1236 		isp_prt(isp, ISP_LOGWARN,
1237 		    "no ATIOS for lun %d from initiator %d on channel %d",
1238 		    aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1239 		if (aep->at_flags & AT_TQAE)
1240 			isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1241 		else
1242 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1243 		rls_lun_statep(isp, tptr);
1244 		return (0);
1245 	}
1246 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1247 	if (iswildcard) {
1248 		atiop->ccb_h.target_id = aep->at_tgt;
1249 		atiop->ccb_h.target_lun = aep->at_lun;
1250 	}
1251 	if (aep->at_flags & AT_NODISC) {
1252 		atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1253 	} else {
1254 		atiop->ccb_h.flags = 0;
1255 	}
1256 
1257 	if (status & QLTM_SVALID) {
1258 		size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1259 		atiop->sense_len = amt;
1260 		MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1261 	} else {
1262 		atiop->sense_len = 0;
1263 	}
1264 
1265 	atiop->init_id = GET_IID_VAL(aep->at_iid);
1266 	atiop->cdb_len = aep->at_cdblen;
1267 	MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1268 	atiop->ccb_h.status = CAM_CDB_RECVD;
1269 	/*
1270 	 * Construct a tag 'id' based upon tag value (which may be 0..255)
1271 	 * and the handle (which we have to preserve).
1272 	 */
1273 	AT_MAKE_TAGID(atiop->tag_id, aep);
1274 	if (aep->at_flags & AT_TQAE) {
1275 		atiop->tag_action = aep->at_tag_type;
1276 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1277 	}
1278 	xpt_done((union ccb*)atiop);
1279 	isp_prt(isp, ISP_LOGTDEBUG0,
1280 	    "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1281 	    aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1282 	    GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1283 	    aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1284 	    "nondisc" : "disconnecting");
1285 	rls_lun_statep(isp, tptr);
1286 	return (0);
1287 }
1288 
1289 static int
1290 isp_handle_platform_atio2(struct ispsoftc *isp, at2_entry_t *aep)
1291 {
1292 	lun_id_t lun;
1293 	tstate_t *tptr;
1294 	struct ccb_accept_tio *atiop;
1295 	atio_private_data_t *atp;
1296 
1297 	/*
1298 	 * The firmware status (except for the QLTM_SVALID bit)
1299 	 * indicates why this ATIO was sent to us.
1300 	 *
1301 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1302 	 */
1303 	if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1304 		isp_prt(isp, ISP_LOGWARN,
1305 		    "bogus atio (0x%x) leaked to platform", aep->at_status);
1306 		isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1307 		return (0);
1308 	}
1309 
1310 	if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1311 		lun = aep->at_scclun;
1312 	} else {
1313 		lun = aep->at_lun;
1314 	}
1315 	tptr = get_lun_statep(isp, 0, lun);
1316 	if (tptr == NULL) {
1317 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1318 	}
1319 
1320 	if (tptr == NULL) {
1321 		/*
1322 		 * What we'd like to know is whether or not we have a listener
1323 		 * upstream that really hasn't configured yet. If we do, then
1324 		 * we can give a more sensible reply here. If not, then we can
1325 		 * reject this out of hand.
1326 		 *
1327 		 * Choices for what to send were
1328 		 *
1329                  *	Not Ready, Unit Not Self-Configured Yet
1330 		 *	(0x2,0x3e,0x00)
1331 		 *
1332 		 * for the former and
1333 		 *
1334 		 *	Illegal Request, Logical Unit Not Supported
1335 		 *	(0x5,0x25,0x00)
1336 		 *
1337 		 * for the latter.
1338 		 *
1339 		 * We used to decide whether there was at least one listener
1340 		 * based upon whether the black hole driver was configured.
1341 		 * However, recent config(8) changes have made this hard to do
1342 		 * at this time.
1343 		 *
1344 		 */
1345 		u_int32_t ccode = SCSI_STATUS_BUSY;
1346 
1347 		/*
1348 		 * Because we can't autofeed sense data back with
1349 		 * a command for parallel SCSI, we can't give back
1350 		 * a CHECK CONDITION. We'll give back a BUSY status
1351 		 * instead. This works out okay because the only
1352 		 * time we should, in fact, get this, is in the
1353 		 * case that somebody configured us without the
1354 		 * blackhole driver, so they get what they deserve.
1355 		 */
1356 		isp_endcmd(isp, aep, ccode, 0);
1357 		return (0);
1358 	}
1359 
1360 	atp = isp_get_atpd(isp, 0);
1361 	atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1362 	if (atiop == NULL || atp == NULL) {
1363 		/*
1364 		 * Because we can't autofeed sense data back with
1365 		 * a command for parallel SCSI, we can't give back
1366 		 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1367 		 * instead. This works out okay because the only time we
1368 		 * should, in fact, get this, is in the case that we've
1369 		 * run out of ATIOS.
1370 		 */
1371 		xpt_print_path(tptr->owner);
1372 		isp_prt(isp, ISP_LOGWARN,
1373 		    "no ATIOS for lun %d from initiator %d", lun, aep->at_iid);
1374 		rls_lun_statep(isp, tptr);
1375 		if (aep->at_flags & AT_TQAE)
1376 			isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1377 		else
1378 			isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1379 		return (0);
1380 	}
1381 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1382 
1383 	if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1384 		atiop->ccb_h.target_id =
1385 			((fcparam *)isp->isp_param)->isp_loopid;
1386 		atiop->ccb_h.target_lun = lun;
1387 	}
1388 	/*
1389 	 * We don't get 'suggested' sense data as we do with SCSI cards.
1390 	 */
1391 	atiop->sense_len = 0;
1392 
1393 	atiop->init_id = aep->at_iid;
1394 	atiop->cdb_len = ATIO2_CDBLEN;
1395 	MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1396 	atiop->ccb_h.status = CAM_CDB_RECVD;
1397 	atiop->tag_id = aep->at_rxid;
1398 	switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1399 	case ATIO2_TC_ATTR_SIMPLEQ:
1400 		atiop->tag_action = MSG_SIMPLE_Q_TAG;
1401 		break;
1402         case ATIO2_TC_ATTR_HEADOFQ:
1403 		atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1404 		break;
1405         case ATIO2_TC_ATTR_ORDERED:
1406 		atiop->tag_action = MSG_ORDERED_Q_TAG;
1407 		break;
1408         case ATIO2_TC_ATTR_ACAQ:		/* ?? */
1409 	case ATIO2_TC_ATTR_UNTAGGED:
1410 	default:
1411 		atiop->tag_action = 0;
1412 		break;
1413 	}
1414 	if (atiop->tag_action != 0) {
1415 		atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1416 	}
1417 
1418 	atp->tag = atiop->tag_id;
1419 	atp->orig_datalen = aep->at_datalen;
1420 	atp->last_xframt = 0;
1421 	atp->bytes_xfered = 0;
1422 
1423 	xpt_done((union ccb*)atiop);
1424 	isp_prt(isp, ISP_LOGTDEBUG0,
1425 	    "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1426 	    aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1427 	    lun, aep->at_taskflags, aep->at_datalen);
1428 	rls_lun_statep(isp, tptr);
1429 	return (0);
1430 }
1431 
1432 static int
1433 isp_handle_platform_ctio(struct ispsoftc *isp, void *arg)
1434 {
1435 	union ccb *ccb;
1436 	int sentstatus, ok, notify_cam, resid = 0;
1437 	u_int16_t tval;
1438 
1439 	/*
1440 	 * CTIO and CTIO2 are close enough....
1441 	 */
1442 
1443 	ccb = (union ccb *) isp_find_xs(isp, ((ct_entry_t *)arg)->ct_syshandle);
1444 	KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1445 	isp_destroy_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1446 
1447 	if (IS_FC(isp)) {
1448 		ct2_entry_t *ct = arg;
1449 		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1450 		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1451 		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1452 			ccb->ccb_h.status |= CAM_SENT_SENSE;
1453 		}
1454 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
1455 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1456 			atio_private_data_t *atp =
1457 			    isp_get_atpd(isp, ct->ct_rxid);
1458 			if (atp == NULL) {
1459 				panic("cannot find adjunct after I/O");
1460 			}
1461 			resid = ct->ct_resid;
1462 			atp->bytes_xfered += (atp->last_xframt - resid);
1463 			atp->last_xframt = 0;
1464 			if (sentstatus) {
1465 				atp->tag = 0;
1466 			}
1467 		}
1468 		isp_prt(isp, ISP_LOGTDEBUG0,
1469 		    "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1470 		    ct->ct_rxid, ct->ct_status, ct->ct_flags,
1471 		    (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1472 		    resid, sentstatus? "FIN" : "MID");
1473 		tval = ct->ct_rxid;
1474 	} else {
1475 		ct_entry_t *ct = arg;
1476 		sentstatus = ct->ct_flags & CT_SENDSTATUS;
1477 		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
1478 		/*
1479 		 * We *ought* to be able to get back to the original ATIO
1480 		 * here, but for some reason this gets lost. It's just as
1481 		 * well because it's squirrelled away as part of periph
1482 		 * private data.
1483 		 *
1484 		 * We can live without it as long as we continue to use
1485 		 * the auto-replenish feature for CTIOs.
1486 		 */
1487 		notify_cam = ct->ct_header.rqs_seqno & 0x1;
1488 		if (ct->ct_status & QLTM_SVALID) {
1489 			char *sp = (char *)ct;
1490 			sp += CTIO_SENSE_OFFSET;
1491 			ccb->csio.sense_len =
1492 			    min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1493 			MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1494 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1495 		}
1496 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1497 			resid = ct->ct_resid;
1498 		}
1499 		isp_prt(isp, ISP_LOGTDEBUG0,
1500 		    "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1501 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1502 		    ct->ct_status, ct->ct_flags, resid,
1503 		    sentstatus? "FIN" : "MID");
1504 		tval = ct->ct_fwhandle;
1505 	}
1506 	ccb->csio.resid += resid;
1507 
1508 	/*
1509 	 * We're here either because intermediate data transfers are done
1510 	 * and/or the final status CTIO (which may have joined with a
1511 	 * Data Transfer) is done.
1512 	 *
1513 	 * In any case, for this platform, the upper layers figure out
1514 	 * what to do next, so all we do here is collect status and
1515 	 * pass information along. Any DMA handles have already been
1516 	 * freed.
1517 	 */
1518 	if (notify_cam == 0) {
1519 		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
1520 		return (0);
1521 	}
1522 
1523 	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1524 	    (sentstatus)? "  FINAL " : "MIDTERM ", tval);
1525 
1526 	if (!ok) {
1527 		isp_target_putback_atio(ccb);
1528 	} else {
1529 		isp_complete_ctio(ccb);
1530 
1531 	}
1532 	return (0);
1533 }
1534 #endif
1535 
1536 static void
1537 isp_cam_async(void *cbarg, u_int32_t code, struct cam_path *path, void *arg)
1538 {
1539 	struct cam_sim *sim;
1540 	struct ispsoftc *isp;
1541 
1542 	sim = (struct cam_sim *)cbarg;
1543 	isp = (struct ispsoftc *) cam_sim_softc(sim);
1544 	switch (code) {
1545 	case AC_LOST_DEVICE:
1546 		if (IS_SCSI(isp)) {
1547 			u_int16_t oflags, nflags;
1548 			sdparam *sdp = isp->isp_param;
1549 			int tgt;
1550 
1551 			tgt = xpt_path_target_id(path);
1552 			ISP_LOCK(isp);
1553 			sdp += cam_sim_bus(sim);
1554 			nflags = sdp->isp_devparam[tgt].nvrm_flags;
1555 #ifndef	ISP_TARGET_MODE
1556 			nflags &= DPARM_SAFE_DFLT;
1557 			if (isp->isp_loaded_fw) {
1558 				nflags |= DPARM_NARROW | DPARM_ASYNC;
1559 			}
1560 #else
1561 			nflags = DPARM_DEFAULT;
1562 #endif
1563 			oflags = sdp->isp_devparam[tgt].goal_flags;
1564 			sdp->isp_devparam[tgt].goal_flags = nflags;
1565 			sdp->isp_devparam[tgt].dev_update = 1;
1566 			isp->isp_update |= (1 << cam_sim_bus(sim));
1567 			(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, NULL);
1568 			sdp->isp_devparam[tgt].goal_flags = oflags;
1569 			ISP_UNLOCK(isp);
1570 		}
1571 		break;
1572 	default:
1573 		isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
1574 		break;
1575 	}
1576 }
1577 
1578 static void
1579 isp_poll(struct cam_sim *sim)
1580 {
1581 	struct ispsoftc *isp = cam_sim_softc(sim);
1582 	u_int16_t isr, sema, mbox;
1583 
1584 	ISP_LOCK(isp);
1585 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1586 		isp_intr(isp, isr, sema, mbox);
1587 	}
1588 	ISP_UNLOCK(isp);
1589 }
1590 
1591 #if	0
1592 static void
1593 isp_relsim(void *arg)
1594 {
1595 	struct ispsoftc *isp = arg;
1596 	ISP_LOCK(isp);
1597 	if (isp->isp_osinfo.simqfrozen & SIMQFRZ_TIMED) {
1598 		int wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_TIMED;
1599 		isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_TIMED;
1600 		if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
1601 			xpt_release_simq(isp->isp_sim, 1);
1602 			isp_prt(isp, ISP_LOGDEBUG2, "timed relsimq");
1603 		}
1604 	}
1605 	ISP_UNLOCK(isp);
1606 }
1607 #endif
1608 
1609 static void
1610 isp_watchdog(void *arg)
1611 {
1612 	XS_T *xs = arg;
1613 	struct ispsoftc *isp = XS_ISP(xs);
1614 	u_int32_t handle;
1615 
1616 	/*
1617 	 * We've decided this command is dead. Make sure we're not trying
1618 	 * to kill a command that's already dead by getting it's handle and
1619 	 * and seeing whether it's still alive.
1620 	 */
1621 	ISP_LOCK(isp);
1622 	handle = isp_find_handle(isp, xs);
1623 	if (handle) {
1624 		u_int16_t isr, sema, mbox;
1625 
1626 		if (XS_CMD_DONE_P(xs)) {
1627 			isp_prt(isp, ISP_LOGDEBUG1,
1628 			    "watchdog found done cmd (handle 0x%x)", handle);
1629 			ISP_UNLOCK(isp);
1630 			return;
1631 		}
1632 
1633 		if (XS_CMD_WDOG_P(xs)) {
1634 			isp_prt(isp, ISP_LOGDEBUG2,
1635 			    "recursive watchdog (handle 0x%x)", handle);
1636 			ISP_UNLOCK(isp);
1637 			return;
1638 		}
1639 
1640 		XS_CMD_S_WDOG(xs);
1641 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1642 			isp_intr(isp, isr, sema, mbox);
1643 		}
1644 		if (XS_CMD_DONE_P(xs)) {
1645 			isp_prt(isp, ISP_LOGDEBUG2,
1646 			    "watchdog cleanup for handle 0x%x", handle);
1647 			xpt_done((union ccb *) xs);
1648 		} else if (XS_CMD_GRACE_P(xs)) {
1649 			/*
1650 			 * Make sure the command is *really* dead before we
1651 			 * release the handle (and DMA resources) for reuse.
1652 			 */
1653 			(void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
1654 
1655 			/*
1656 			 * After this point, the comamnd is really dead.
1657 			 */
1658 			if (XS_XFRLEN(xs)) {
1659 				ISP_DMAFREE(isp, xs, handle);
1660                 	}
1661 			isp_destroy_handle(isp, handle);
1662 			xpt_print_path(xs->ccb_h.path);
1663 			isp_prt(isp, ISP_LOGWARN,
1664 			    "watchdog timeout for handle %x", handle);
1665 			XS_SETERR(xs, CAM_CMD_TIMEOUT);
1666 			XS_CMD_C_WDOG(xs);
1667 			isp_done(xs);
1668 		} else {
1669 			u_int16_t iptr, optr;
1670 			ispreq_t *mp;
1671 
1672 			XS_CMD_C_WDOG(xs);
1673 			xs->ccb_h.timeout_ch = timeout(isp_watchdog, xs, hz);
1674 			if (isp_getrqentry(isp, &iptr, &optr, (void **) &mp)) {
1675 				ISP_UNLOCK(isp);
1676 				return;
1677 			}
1678 			XS_CMD_S_GRACE(xs);
1679 			MEMZERO((void *) mp, sizeof (*mp));
1680 			mp->req_header.rqs_entry_count = 1;
1681 			mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
1682 			mp->req_modifier = SYNC_ALL;
1683 			mp->req_target = XS_CHANNEL(xs) << 7;
1684 			ISP_SWIZZLE_REQUEST(isp, mp);
1685 			ISP_ADD_REQUEST(isp, iptr);
1686 		}
1687 	} else {
1688 		isp_prt(isp, ISP_LOGDEBUG2, "watchdog with no command");
1689 	}
1690 	ISP_UNLOCK(isp);
1691 }
1692 
1693 static int isp_ktmature = 0;
1694 
1695 static void
1696 isp_kthread(void *arg)
1697 {
1698 	int wasfrozen;
1699 	struct ispsoftc *isp = arg;
1700 
1701 	mtx_lock(&isp->isp_lock);
1702 	for (;;) {
1703 		isp_prt(isp, ISP_LOGDEBUG0, "kthread checking FC state");
1704 		while (isp_fc_runstate(isp, 2 * 1000000) != 0) {
1705 			if (FCPARAM(isp)->isp_fwstate != FW_READY ||
1706 			    FCPARAM(isp)->isp_loopstate < LOOP_PDB_RCVD) {
1707 				if (FCPARAM(isp)->loop_seen_once == 0 ||
1708 				    isp_ktmature == 0) {
1709 					break;
1710 				}
1711 			}
1712 			msleep(isp_kthread, &isp->isp_lock,
1713 			    PRIBIO, "isp_fcthrd", hz);
1714 		}
1715 		/*
1716 		 * Even if we didn't get good loop state we may be
1717 		 * unfreezing the SIMQ so that we can kill off
1718 		 * commands (if we've never seen loop before, e.g.)
1719 		 */
1720 		isp_ktmature = 1;
1721 		wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
1722 		isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
1723 		if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
1724 			isp_prt(isp, ISP_LOGDEBUG0, "kthread up release simq");
1725 			ISPLOCK_2_CAMLOCK(isp);
1726 			xpt_release_simq(isp->isp_sim, 1);
1727 			CAMLOCK_2_ISPLOCK(isp);
1728 		}
1729 		cv_wait(&isp->isp_osinfo.kthread_cv, &isp->isp_lock);
1730 	}
1731 }
1732 
1733 static void
1734 isp_action(struct cam_sim *sim, union ccb *ccb)
1735 {
1736 	int bus, tgt, error;
1737 	struct ispsoftc *isp;
1738 	struct ccb_trans_settings *cts;
1739 
1740 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
1741 
1742 	isp = (struct ispsoftc *)cam_sim_softc(sim);
1743 	ccb->ccb_h.sim_priv.entries[0].field = 0;
1744 	ccb->ccb_h.sim_priv.entries[1].ptr = isp;
1745 	if (isp->isp_state != ISP_RUNSTATE &&
1746 	    ccb->ccb_h.func_code == XPT_SCSI_IO) {
1747 		CAMLOCK_2_ISPLOCK(isp);
1748 		isp_init(isp);
1749 		if (isp->isp_state != ISP_INITSTATE) {
1750 			ISP_UNLOCK(isp);
1751 			/*
1752 			 * Lie. Say it was a selection timeout.
1753 			 */
1754 			ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
1755 			xpt_freeze_devq(ccb->ccb_h.path, 1);
1756 			xpt_done(ccb);
1757 			return;
1758 		}
1759 		isp->isp_state = ISP_RUNSTATE;
1760 		ISPLOCK_2_CAMLOCK(isp);
1761 	}
1762 	isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
1763 
1764 
1765 	switch (ccb->ccb_h.func_code) {
1766 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
1767 		/*
1768 		 * Do a couple of preliminary checks...
1769 		 */
1770 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1771 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
1772 				ccb->ccb_h.status = CAM_REQ_INVALID;
1773 				xpt_done(ccb);
1774 				break;
1775 			}
1776 		}
1777 #ifdef	DIAGNOSTIC
1778 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
1779 			ccb->ccb_h.status = CAM_PATH_INVALID;
1780 		} else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
1781 			ccb->ccb_h.status = CAM_PATH_INVALID;
1782 		}
1783 		if (ccb->ccb_h.status == CAM_PATH_INVALID) {
1784 			isp_prt(isp, ISP_LOGERR,
1785 			    "invalid tgt/lun (%d.%d) in XPT_SCSI_IO",
1786 			    ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
1787 			xpt_done(ccb);
1788 			break;
1789 		}
1790 #endif
1791 		((struct ccb_scsiio *) ccb)->scsi_status = SCSI_STATUS_OK;
1792 		CAMLOCK_2_ISPLOCK(isp);
1793 		error = isp_start((XS_T *) ccb);
1794 		switch (error) {
1795 		case CMD_QUEUED:
1796 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
1797 			if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1798 				u_int64_t ticks = (u_int64_t) hz;
1799 				if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1800 					ticks = 60 * 1000 * ticks;
1801 				else
1802 					ticks = ccb->ccb_h.timeout * hz;
1803 				ticks = ((ticks + 999) / 1000) + hz + hz;
1804 				if (ticks >= 0x80000000) {
1805 					isp_prt(isp, ISP_LOGERR,
1806 					    "timeout overflow");
1807 					ticks = 0x80000000;
1808 				}
1809 				ccb->ccb_h.timeout_ch = timeout(isp_watchdog,
1810 				    (caddr_t)ccb, (int)ticks);
1811 			} else {
1812 				callout_handle_init(&ccb->ccb_h.timeout_ch);
1813 			}
1814 			ISPLOCK_2_CAMLOCK(isp);
1815 			break;
1816 		case CMD_RQLATER:
1817 			/*
1818 			 * This can only happen for Fibre Channel
1819 			 */
1820 			KASSERT((IS_FC(isp)), ("CMD_RQLATER for FC only"));
1821 			if (FCPARAM(isp)->loop_seen_once == 0 && isp_ktmature) {
1822 				ISPLOCK_2_CAMLOCK(isp);
1823 				XS_SETERR(ccb, CAM_SEL_TIMEOUT);
1824 				xpt_done(ccb);
1825 				break;
1826 			}
1827 			cv_signal(&isp->isp_osinfo.kthread_cv);
1828 			if (isp->isp_osinfo.simqfrozen == 0) {
1829 				isp_prt(isp, ISP_LOGDEBUG2,
1830 				    "RQLATER freeze simq");
1831 				isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
1832 				ISPLOCK_2_CAMLOCK(isp);
1833 				xpt_freeze_simq(sim, 1);
1834 			} else {
1835 				ISPLOCK_2_CAMLOCK(isp);
1836 			}
1837 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
1838 			xpt_done(ccb);
1839 			break;
1840 		case CMD_EAGAIN:
1841 			if (isp->isp_osinfo.simqfrozen == 0) {
1842 				xpt_freeze_simq(sim, 1);
1843 				isp_prt(isp, ISP_LOGDEBUG2,
1844 				    "EAGAIN freeze simq");
1845 			}
1846 			isp->isp_osinfo.simqfrozen |= SIMQFRZ_RESOURCE;
1847 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
1848 			ISPLOCK_2_CAMLOCK(isp);
1849 			xpt_done(ccb);
1850 			break;
1851 		case CMD_COMPLETE:
1852 			isp_done((struct ccb_scsiio *) ccb);
1853 			ISPLOCK_2_CAMLOCK(isp);
1854 			break;
1855 		default:
1856 			isp_prt(isp, ISP_LOGERR,
1857 			    "What's this? 0x%x at %d in file %s",
1858 			    error, __LINE__, __FILE__);
1859 			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
1860 			xpt_done(ccb);
1861 			ISPLOCK_2_CAMLOCK(isp);
1862 		}
1863 		break;
1864 
1865 #ifdef	ISP_TARGET_MODE
1866 	case XPT_EN_LUN:		/* Enable LUN as a target */
1867 	{
1868 		int iok;
1869 		CAMLOCK_2_ISPLOCK(isp);
1870 		iok = isp->isp_osinfo.intsok;
1871 		isp->isp_osinfo.intsok = 0;
1872 		isp_en_lun(isp, ccb);
1873 		isp->isp_osinfo.intsok = iok;
1874 		ISPLOCK_2_CAMLOCK(isp);
1875 		xpt_done(ccb);
1876 		break;
1877 	}
1878 	case XPT_NOTIFY_ACK:		/* recycle notify ack */
1879 	case XPT_IMMED_NOTIFY:		/* Add Immediate Notify Resource */
1880 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
1881 	{
1882 		tstate_t *tptr =
1883 		    get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
1884 		if (tptr == NULL) {
1885 			ccb->ccb_h.status = CAM_LUN_INVALID;
1886 			xpt_done(ccb);
1887 			break;
1888 		}
1889 		ccb->ccb_h.sim_priv.entries[0].field = 0;
1890 		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
1891 		CAMLOCK_2_ISPLOCK(isp);
1892 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1893 			SLIST_INSERT_HEAD(&tptr->atios,
1894 			    &ccb->ccb_h, sim_links.sle);
1895 		} else {
1896 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
1897 			    sim_links.sle);
1898 		}
1899 		rls_lun_statep(isp, tptr);
1900 		ccb->ccb_h.status = CAM_REQ_INPROG;
1901 		ISPLOCK_2_CAMLOCK(isp);
1902 		break;
1903 	}
1904 	case XPT_CONT_TARGET_IO:
1905 	{
1906 		CAMLOCK_2_ISPLOCK(isp);
1907 		ccb->ccb_h.status = isp_target_start_ctio(isp, ccb);
1908 		if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1909 			if (isp->isp_osinfo.simqfrozen == 0) {
1910 				xpt_freeze_simq(sim, 1);
1911 				xpt_print_path(ccb->ccb_h.path);
1912 				isp_prt(isp, ISP_LOGINFO,
1913 				    "XPT_CONT_TARGET_IO freeze simq");
1914 			}
1915 			isp->isp_osinfo.simqfrozen |= SIMQFRZ_RESOURCE;
1916 			XS_SETERR(ccb, CAM_REQUEUE_REQ);
1917 			ISPLOCK_2_CAMLOCK(isp);
1918 			xpt_done(ccb);
1919 		} else {
1920 			ISPLOCK_2_CAMLOCK(isp);
1921 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
1922 		}
1923 		break;
1924 	}
1925 #endif
1926 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
1927 
1928 		bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1929 		tgt = ccb->ccb_h.target_id;
1930 		tgt |= (bus << 16);
1931 
1932 		CAMLOCK_2_ISPLOCK(isp);
1933 		error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
1934 		ISPLOCK_2_CAMLOCK(isp);
1935 		if (error) {
1936 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1937 		} else {
1938 			ccb->ccb_h.status = CAM_REQ_CMP;
1939 		}
1940 		xpt_done(ccb);
1941 		break;
1942 	case XPT_ABORT:			/* Abort the specified CCB */
1943 	{
1944 		union ccb *accb = ccb->cab.abort_ccb;
1945 		CAMLOCK_2_ISPLOCK(isp);
1946 		switch (accb->ccb_h.func_code) {
1947 #ifdef	ISP_TARGET_MODE
1948 		case XPT_ACCEPT_TARGET_IO:
1949 		case XPT_IMMED_NOTIFY:
1950         		ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
1951 			break;
1952 		case XPT_CONT_TARGET_IO:
1953 			isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
1954 			ccb->ccb_h.status = CAM_UA_ABORT;
1955 			break;
1956 #endif
1957 		case XPT_SCSI_IO:
1958 			error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
1959 			if (error) {
1960 				ccb->ccb_h.status = CAM_UA_ABORT;
1961 			} else {
1962 				ccb->ccb_h.status = CAM_REQ_CMP;
1963 			}
1964 			break;
1965 		default:
1966 			ccb->ccb_h.status = CAM_REQ_INVALID;
1967 			break;
1968 		}
1969 		ISPLOCK_2_CAMLOCK(isp);
1970 		xpt_done(ccb);
1971 		break;
1972 	}
1973 #ifdef	CAM_NEW_TRAN_CODE
1974 #define	IS_CURRENT_SETTINGS(c)	(c->type == CTS_TYPE_CURRENT_SETTINGS)
1975 #else
1976 #define	IS_CURRENT_SETTINGS(c)	(c->flags & CCB_TRANS_CURRENT_SETTINGS)
1977 #endif
1978 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
1979 		cts = &ccb->cts;
1980 		if (!IS_CURRENT_SETTINGS(cts)) {
1981 			ccb->ccb_h.status = CAM_REQ_INVALID;
1982 			xpt_done(ccb);
1983 			break;
1984 		}
1985 		tgt = cts->ccb_h.target_id;
1986 		CAMLOCK_2_ISPLOCK(isp);
1987 		if (IS_SCSI(isp)) {
1988 #ifndef	CAM_NEW_TRAN_CODE
1989 			sdparam *sdp = isp->isp_param;
1990 			u_int16_t *dptr;
1991 
1992 			bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
1993 
1994 			sdp += bus;
1995 			/*
1996 			 * We always update (internally) from goal_flags
1997 			 * so any request to change settings just gets
1998 			 * vectored to that location.
1999 			 */
2000 			dptr = &sdp->isp_devparam[tgt].goal_flags;
2001 
2002 			/*
2003 			 * Note that these operations affect the
2004 			 * the goal flags (goal_flags)- not
2005 			 * the current state flags. Then we mark
2006 			 * things so that the next operation to
2007 			 * this HBA will cause the update to occur.
2008 			 */
2009 			if (cts->valid & CCB_TRANS_DISC_VALID) {
2010 				if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
2011 					*dptr |= DPARM_DISC;
2012 				} else {
2013 					*dptr &= ~DPARM_DISC;
2014 				}
2015 			}
2016 			if (cts->valid & CCB_TRANS_TQ_VALID) {
2017 				if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
2018 					*dptr |= DPARM_TQING;
2019 				} else {
2020 					*dptr &= ~DPARM_TQING;
2021 				}
2022 			}
2023 			if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) {
2024 				switch (cts->bus_width) {
2025 				case MSG_EXT_WDTR_BUS_16_BIT:
2026 					*dptr |= DPARM_WIDE;
2027 					break;
2028 				default:
2029 					*dptr &= ~DPARM_WIDE;
2030 				}
2031 			}
2032 			/*
2033 			 * Any SYNC RATE of nonzero and SYNC_OFFSET
2034 			 * of nonzero will cause us to go to the
2035 			 * selected (from NVRAM) maximum value for
2036 			 * this device. At a later point, we'll
2037 			 * allow finer control.
2038 			 */
2039 			if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
2040 			    (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) &&
2041 			    (cts->sync_offset > 0)) {
2042 				*dptr |= DPARM_SYNC;
2043 			} else {
2044 				*dptr &= ~DPARM_SYNC;
2045 			}
2046 			*dptr |= DPARM_SAFE_DFLT;
2047 #else
2048 			struct ccb_trans_settings_scsi *scsi =
2049 			    &cts->proto_specific.scsi;
2050 			struct ccb_trans_settings_spi *spi =
2051 			    &cts->xport_specific.spi;
2052 			sdparam *sdp = isp->isp_param;
2053 			u_int16_t *dptr;
2054 
2055 			bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2056 			sdp += bus;
2057 			/*
2058 			 * We always update (internally) from goal_flags
2059 			 * so any request to change settings just gets
2060 			 * vectored to that location.
2061 			 */
2062 			dptr = &sdp->isp_devparam[tgt].goal_flags;
2063 
2064 			if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
2065 				if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
2066 					*dptr |= DPARM_DISC;
2067 				else
2068 					*dptr &= ~DPARM_DISC;
2069 			}
2070 
2071 			if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2072 				if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
2073 					*dptr |= DPARM_TQING;
2074 				else
2075 					*dptr &= ~DPARM_TQING;
2076 			}
2077 
2078 			if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
2079 				if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
2080 					*dptr |= DPARM_WIDE;
2081 				else
2082 					*dptr &= ~DPARM_WIDE;
2083 			}
2084 
2085 			/*
2086 			 * XXX: FIX ME
2087 			 */
2088 			if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) &&
2089 			    (spi->valid & CTS_SPI_VALID_SYNC_RATE) &&
2090 			    (spi->sync_period && spi->sync_offset)) {
2091 				*dptr |= DPARM_SYNC;
2092 				/*
2093 				 * XXX: CHECK FOR LEGALITY
2094 				 */
2095 				sdp->isp_devparam[tgt].goal_period =
2096 				    spi->sync_period;
2097 				sdp->isp_devparam[tgt].goal_offset =
2098 				    spi->sync_offset;
2099 			} else {
2100 				*dptr &= ~DPARM_SYNC;
2101 			}
2102 #endif
2103 			isp_prt(isp, ISP_LOGDEBUG0,
2104 			    "SET bus %d targ %d to flags %x off %x per %x",
2105 			    bus, tgt, sdp->isp_devparam[tgt].goal_flags,
2106 			    sdp->isp_devparam[tgt].goal_offset,
2107 			    sdp->isp_devparam[tgt].goal_period);
2108 			sdp->isp_devparam[tgt].dev_update = 1;
2109 			isp->isp_update |= (1 << bus);
2110 		}
2111 		ISPLOCK_2_CAMLOCK(isp);
2112 		ccb->ccb_h.status = CAM_REQ_CMP;
2113 		xpt_done(ccb);
2114 		break;
2115 	case XPT_GET_TRAN_SETTINGS:
2116 		cts = &ccb->cts;
2117 		tgt = cts->ccb_h.target_id;
2118 		CAMLOCK_2_ISPLOCK(isp);
2119 		if (IS_FC(isp)) {
2120 #ifndef	CAM_NEW_TRAN_CODE
2121 			/*
2122 			 * a lot of normal SCSI things don't make sense.
2123 			 */
2124 			cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
2125 			cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2126 			/*
2127 			 * How do you measure the width of a high
2128 			 * speed serial bus? Well, in bytes.
2129 			 *
2130 			 * Offset and period make no sense, though, so we set
2131 			 * (above) a 'base' transfer speed to be gigabit.
2132 			 */
2133 			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2134 #else
2135 			fcparam *fcp = isp->isp_param;
2136 			struct ccb_trans_settings_fc *fc =
2137 			    &cts->xport_specific.fc;
2138 
2139 			cts->protocol = PROTO_SCSI;
2140 			cts->protocol_version = SCSI_REV_2;
2141 			cts->transport = XPORT_FC;
2142 			cts->transport_version = 0;
2143 
2144 			fc->valid = CTS_FC_VALID_SPEED;
2145 			if (fcp->isp_gbspeed == 2)
2146 				fc->bitrate = 200000;
2147 			else
2148 				fc->bitrate = 100000;
2149 			if (tgt > 0 && tgt < MAX_FC_TARG) {
2150 				struct lportdb *lp = &fcp->portdb[tgt];
2151 				fc->wwnn = lp->node_wwn;
2152 				fc->wwpn = lp->port_wwn;
2153 				fc->port = lp->portid;
2154 				fc->valid |= CTS_FC_VALID_WWNN |
2155 				    CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
2156 			}
2157 #endif
2158 		} else {
2159 #ifdef	CAM_NEW_TRAN_CODE
2160 			struct ccb_trans_settings_scsi *scsi =
2161 			    &cts->proto_specific.scsi;
2162 			struct ccb_trans_settings_spi *spi =
2163 			    &cts->xport_specific.spi;
2164 #endif
2165 			sdparam *sdp = isp->isp_param;
2166 			int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2167 			u_int16_t dval, pval, oval;
2168 
2169 			sdp += bus;
2170 
2171 			if (IS_CURRENT_SETTINGS(cts)) {
2172 				sdp->isp_devparam[tgt].dev_refresh = 1;
2173 				isp->isp_update |= (1 << bus);
2174 				(void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2175 				    NULL);
2176 				dval = sdp->isp_devparam[tgt].actv_flags;
2177 				oval = sdp->isp_devparam[tgt].actv_offset;
2178 				pval = sdp->isp_devparam[tgt].actv_period;
2179 			} else {
2180 				dval = sdp->isp_devparam[tgt].nvrm_flags;
2181 				oval = sdp->isp_devparam[tgt].nvrm_offset;
2182 				pval = sdp->isp_devparam[tgt].nvrm_period;
2183 			}
2184 
2185 #ifndef	CAM_NEW_TRAN_CODE
2186 			cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
2187 
2188 			if (dval & DPARM_DISC) {
2189 				cts->flags |= CCB_TRANS_DISC_ENB;
2190 			}
2191 			if (dval & DPARM_TQING) {
2192 				cts->flags |= CCB_TRANS_TAG_ENB;
2193 			}
2194 			if (dval & DPARM_WIDE) {
2195 				cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2196 			} else {
2197 				cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2198 			}
2199 			cts->valid = CCB_TRANS_BUS_WIDTH_VALID |
2200 			    CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2201 
2202 			if ((dval & DPARM_SYNC) && oval != 0) {
2203 				cts->sync_period = pval;
2204 				cts->sync_offset = oval;
2205 				cts->valid |=
2206 				    CCB_TRANS_SYNC_RATE_VALID |
2207 				    CCB_TRANS_SYNC_OFFSET_VALID;
2208 			}
2209 #else
2210 			cts->protocol = PROTO_SCSI;
2211 			cts->protocol_version = SCSI_REV_2;
2212 			cts->transport = XPORT_SPI;
2213 			cts->transport_version = 2;
2214 
2215 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2216 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2217 			if (dval & DPARM_DISC) {
2218 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2219 			}
2220 			if (dval & DPARM_TQING) {
2221 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2222 			}
2223 			if ((dval & DPARM_SYNC) && oval && pval) {
2224 				spi->sync_offset = oval;
2225 				spi->sync_period = pval;
2226 				spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2227 				spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2228 			}
2229 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2230 			if (dval & DPARM_WIDE) {
2231 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2232 			} else {
2233 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2234 			}
2235 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2236 				scsi->valid = CTS_SCSI_VALID_TQ;
2237 				spi->valid |= CTS_SPI_VALID_DISC;
2238 			} else {
2239 				scsi->valid = 0;
2240 			}
2241 #endif
2242 			isp_prt(isp, ISP_LOGDEBUG0,
2243 			    "GET %s bus %d targ %d to flags %x off %x per %x",
2244 			    IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2245 			    bus, tgt, dval, oval, pval);
2246 		}
2247 		ISPLOCK_2_CAMLOCK(isp);
2248 		ccb->ccb_h.status = CAM_REQ_CMP;
2249 		xpt_done(ccb);
2250 		break;
2251 
2252 	case XPT_CALC_GEOMETRY:
2253 	{
2254 		struct ccb_calc_geometry *ccg;
2255 		u_int32_t secs_per_cylinder;
2256 		u_int32_t size_mb;
2257 
2258 		ccg = &ccb->ccg;
2259 		if (ccg->block_size == 0) {
2260 			isp_prt(isp, ISP_LOGERR,
2261 			    "%d.%d XPT_CALC_GEOMETRY block size 0?",
2262 			    ccg->ccb_h.target_id, ccg->ccb_h.target_lun);
2263 			ccb->ccb_h.status = CAM_REQ_INVALID;
2264 			xpt_done(ccb);
2265 			break;
2266 		}
2267 		size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2268 		if (size_mb > 1024) {
2269 			ccg->heads = 255;
2270 			ccg->secs_per_track = 63;
2271 		} else {
2272 			ccg->heads = 64;
2273 			ccg->secs_per_track = 32;
2274 		}
2275 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2276 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2277 		ccb->ccb_h.status = CAM_REQ_CMP;
2278 		xpt_done(ccb);
2279 		break;
2280 	}
2281 	case XPT_RESET_BUS:		/* Reset the specified bus */
2282 		bus = cam_sim_bus(sim);
2283 		CAMLOCK_2_ISPLOCK(isp);
2284 		error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2285 		ISPLOCK_2_CAMLOCK(isp);
2286 		if (error)
2287 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2288 		else {
2289 			if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2290 				xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2291 			else if (isp->isp_path != NULL)
2292 				xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2293 			ccb->ccb_h.status = CAM_REQ_CMP;
2294 		}
2295 		xpt_done(ccb);
2296 		break;
2297 
2298 	case XPT_TERM_IO:		/* Terminate the I/O process */
2299 		ccb->ccb_h.status = CAM_REQ_INVALID;
2300 		xpt_done(ccb);
2301 		break;
2302 
2303 	case XPT_PATH_INQ:		/* Path routing inquiry */
2304 	{
2305 		struct ccb_pathinq *cpi = &ccb->cpi;
2306 
2307 		cpi->version_num = 1;
2308 #ifdef	ISP_TARGET_MODE
2309 		cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2310 #else
2311 		cpi->target_sprt = 0;
2312 #endif
2313 		cpi->hba_eng_cnt = 0;
2314 		cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
2315 		cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
2316 		cpi->bus_id = cam_sim_bus(sim);
2317 		if (IS_FC(isp)) {
2318 			cpi->hba_misc = PIM_NOBUSRESET;
2319 			/*
2320 			 * Because our loop ID can shift from time to time,
2321 			 * make our initiator ID out of range of our bus.
2322 			 */
2323 			cpi->initiator_id = cpi->max_target + 1;
2324 
2325 			/*
2326 			 * Set base transfer capabilities for Fibre Channel.
2327 			 * Technically not correct because we don't know
2328 			 * what media we're running on top of- but we'll
2329 			 * look good if we always say 100MB/s.
2330 			 */
2331 			if (FCPARAM(isp)->isp_gbspeed == 2)
2332 				cpi->base_transfer_speed = 200000;
2333 			else
2334 				cpi->base_transfer_speed = 100000;
2335 			cpi->hba_inquiry = PI_TAG_ABLE;
2336 #ifdef	CAM_NEW_TRAN_CODE
2337 			cpi->transport = XPORT_FC;
2338 			cpi->transport_version = 0;	/* WHAT'S THIS FOR? */
2339 #endif
2340 		} else {
2341 			sdparam *sdp = isp->isp_param;
2342 			sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
2343 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
2344 			cpi->hba_misc = 0;
2345 			cpi->initiator_id = sdp->isp_initiator_id;
2346 			cpi->base_transfer_speed = 3300;
2347 #ifdef	CAM_NEW_TRAN_CODE
2348 			cpi->transport = XPORT_SPI;
2349 			cpi->transport_version = 2;	/* WHAT'S THIS FOR? */
2350 #endif
2351 		}
2352 #ifdef	CAM_NEW_TRAN_CODE
2353 		cpi->protocol = PROTO_SCSI;
2354 		cpi->protocol_version = SCSI_REV_2;
2355 #endif
2356 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2357 		strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
2358 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2359 		cpi->unit_number = cam_sim_unit(sim);
2360 		cpi->ccb_h.status = CAM_REQ_CMP;
2361 		xpt_done(ccb);
2362 		break;
2363 	}
2364 	default:
2365 		ccb->ccb_h.status = CAM_REQ_INVALID;
2366 		xpt_done(ccb);
2367 		break;
2368 	}
2369 }
2370 
2371 #define	ISPDDB	(CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
2372 void
2373 isp_done(struct ccb_scsiio *sccb)
2374 {
2375 	struct ispsoftc *isp = XS_ISP(sccb);
2376 
2377 	if (XS_NOERR(sccb))
2378 		XS_SETERR(sccb, CAM_REQ_CMP);
2379 
2380 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
2381 	    (sccb->scsi_status != SCSI_STATUS_OK)) {
2382 		sccb->ccb_h.status &= ~CAM_STATUS_MASK;
2383 		if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) &&
2384 		    (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
2385 			sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
2386 		} else {
2387 			sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2388 		}
2389 	}
2390 
2391 	sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2392 	if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2393 		if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2394 			sccb->ccb_h.status |= CAM_DEV_QFRZN;
2395 			xpt_freeze_devq(sccb->ccb_h.path, 1);
2396 			if (sccb->scsi_status != SCSI_STATUS_OK)
2397 				isp_prt(isp, ISP_LOGDEBUG2,
2398 				    "freeze devq %d.%d %x %x",
2399 				    sccb->ccb_h.target_id,
2400 				    sccb->ccb_h.target_lun, sccb->ccb_h.status,
2401 				    sccb->scsi_status);
2402 		}
2403 	}
2404 
2405 	/*
2406 	 * If we were frozen waiting resources, clear that we were frozen
2407 	 * waiting for resources. If we are no longer frozen, and the devq
2408 	 * isn't frozen, mark the completing CCB to have the XPT layer
2409 	 * release the simq.
2410 	 */
2411 	if (isp->isp_osinfo.simqfrozen & SIMQFRZ_RESOURCE) {
2412 		isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_RESOURCE;
2413 		if (isp->isp_osinfo.simqfrozen == 0) {
2414 			if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2415 				isp_prt(isp, ISP_LOGDEBUG2,
2416 				    "isp_done->relsimq");
2417 				sccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2418 			} else {
2419 				isp_prt(isp, ISP_LOGDEBUG2,
2420 				    "isp_done->devq frozen");
2421 			}
2422 		} else {
2423 			isp_prt(isp, ISP_LOGDEBUG2,
2424 			    "isp_done -> simqfrozen = %x",
2425 			    isp->isp_osinfo.simqfrozen);
2426 		}
2427 	}
2428 	if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
2429 	    (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2430 		xpt_print_path(sccb->ccb_h.path);
2431 		isp_prt(isp, ISP_LOGINFO,
2432 		    "cam completion status 0x%x", sccb->ccb_h.status);
2433 	}
2434 
2435 	XS_CMD_S_DONE(sccb);
2436 	if (XS_CMD_WDOG_P(sccb) == 0) {
2437 		untimeout(isp_watchdog, (caddr_t)sccb, sccb->ccb_h.timeout_ch);
2438 		if (XS_CMD_GRACE_P(sccb)) {
2439 			isp_prt(isp, ISP_LOGDEBUG2,
2440 			    "finished command on borrowed time");
2441 		}
2442 		XS_CMD_S_CLEAR(sccb);
2443 		ISPLOCK_2_CAMLOCK(isp);
2444 		xpt_done((union ccb *) sccb);
2445 		CAMLOCK_2_ISPLOCK(isp);
2446 	}
2447 }
2448 
2449 int
2450 isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
2451 {
2452 	int bus, rv = 0;
2453 	switch (cmd) {
2454 	case ISPASYNC_NEW_TGT_PARAMS:
2455 	{
2456 #ifdef	CAM_NEW_TRAN_CODE
2457 		struct ccb_trans_settings_scsi *scsi;
2458 		struct ccb_trans_settings_spi *spi;
2459 #endif
2460 		int flags, tgt;
2461 		sdparam *sdp = isp->isp_param;
2462 		struct ccb_trans_settings cts;
2463 		struct cam_path *tmppath;
2464 
2465 		bzero(&cts, sizeof (struct ccb_trans_settings));
2466 
2467 		tgt = *((int *)arg);
2468 		bus = (tgt >> 16) & 0xffff;
2469 		tgt &= 0xffff;
2470 		sdp += bus;
2471 		ISPLOCK_2_CAMLOCK(isp);
2472 		if (xpt_create_path(&tmppath, NULL,
2473 		    cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
2474 		    tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2475 			CAMLOCK_2_ISPLOCK(isp);
2476 			isp_prt(isp, ISP_LOGWARN,
2477 			    "isp_async cannot make temp path for %d.%d",
2478 			    tgt, bus);
2479 			rv = -1;
2480 			break;
2481 		}
2482 		CAMLOCK_2_ISPLOCK(isp);
2483 		flags = sdp->isp_devparam[tgt].actv_flags;
2484 #ifdef	CAM_NEW_TRAN_CODE
2485 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
2486 		cts.protocol = PROTO_SCSI;
2487 		cts.transport = XPORT_SPI;
2488 
2489 		scsi = &cts.proto_specific.scsi;
2490 		spi = &cts.xport_specific.spi;
2491 
2492 		if (flags & DPARM_TQING) {
2493 			scsi->valid |= CTS_SCSI_VALID_TQ;
2494 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2495 			spi->flags |= CTS_SPI_FLAGS_TAG_ENB;
2496 		}
2497 
2498 		if (flags & DPARM_DISC) {
2499 			spi->valid |= CTS_SPI_VALID_DISC;
2500 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2501 		}
2502 		spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
2503 		if (flags & DPARM_WIDE) {
2504 			spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2505 		} else {
2506 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2507 		}
2508 		if (flags & DPARM_SYNC) {
2509 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2510 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2511 			spi->sync_period = sdp->isp_devparam[tgt].actv_period;
2512 			spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
2513 		}
2514 #else
2515 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2516 		cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2517 		if (flags & DPARM_DISC) {
2518 			cts.flags |= CCB_TRANS_DISC_ENB;
2519 		}
2520 		if (flags & DPARM_TQING) {
2521 			cts.flags |= CCB_TRANS_TAG_ENB;
2522 		}
2523 		cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2524 		cts.bus_width = (flags & DPARM_WIDE)?
2525 		    MSG_EXT_WDTR_BUS_8_BIT : MSG_EXT_WDTR_BUS_16_BIT;
2526 		cts.sync_period = sdp->isp_devparam[tgt].actv_period;
2527 		cts.sync_offset = sdp->isp_devparam[tgt].actv_offset;
2528 		if (flags & DPARM_SYNC) {
2529 			cts.valid |=
2530 			    CCB_TRANS_SYNC_RATE_VALID |
2531 			    CCB_TRANS_SYNC_OFFSET_VALID;
2532 		}
2533 #endif
2534 		isp_prt(isp, ISP_LOGDEBUG2,
2535 		    "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
2536 		    bus, tgt, sdp->isp_devparam[tgt].actv_period,
2537 		    sdp->isp_devparam[tgt].actv_offset, flags);
2538 		xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
2539 		ISPLOCK_2_CAMLOCK(isp);
2540 		xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
2541 		xpt_free_path(tmppath);
2542 		CAMLOCK_2_ISPLOCK(isp);
2543 		break;
2544 	}
2545 	case ISPASYNC_BUS_RESET:
2546 		bus = *((int *)arg);
2547 		isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
2548 		    bus);
2549 		if (bus > 0 && isp->isp_path2) {
2550 			ISPLOCK_2_CAMLOCK(isp);
2551 			xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2552 			CAMLOCK_2_ISPLOCK(isp);
2553 		} else if (isp->isp_path) {
2554 			ISPLOCK_2_CAMLOCK(isp);
2555 			xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2556 			CAMLOCK_2_ISPLOCK(isp);
2557 		}
2558 		break;
2559 	case ISPASYNC_LIP:
2560 		if (isp->isp_path) {
2561 			if (isp->isp_osinfo.simqfrozen == 0) {
2562 				isp_prt(isp, ISP_LOGDEBUG0, "LIP freeze simq");
2563 				ISPLOCK_2_CAMLOCK(isp);
2564 				xpt_freeze_simq(isp->isp_sim, 1);
2565 				CAMLOCK_2_ISPLOCK(isp);
2566 			}
2567 			isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
2568 		}
2569 		isp_prt(isp, ISP_LOGINFO, "LIP Received");
2570 		break;
2571 	case ISPASYNC_LOOP_RESET:
2572 		if (isp->isp_path) {
2573 			if (isp->isp_osinfo.simqfrozen == 0) {
2574 				isp_prt(isp, ISP_LOGDEBUG0,
2575 				    "Loop Reset freeze simq");
2576 				ISPLOCK_2_CAMLOCK(isp);
2577 				xpt_freeze_simq(isp->isp_sim, 1);
2578 				CAMLOCK_2_ISPLOCK(isp);
2579 			}
2580 			isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
2581 		}
2582 		isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
2583 		break;
2584 	case ISPASYNC_LOOP_DOWN:
2585 		if (isp->isp_path) {
2586 			if (isp->isp_osinfo.simqfrozen == 0) {
2587 				isp_prt(isp, ISP_LOGDEBUG0,
2588 				    "loop down freeze simq");
2589 				ISPLOCK_2_CAMLOCK(isp);
2590 				xpt_freeze_simq(isp->isp_sim, 1);
2591 				CAMLOCK_2_ISPLOCK(isp);
2592 			}
2593 			isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
2594 		}
2595 		isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
2596 		break;
2597 	case ISPASYNC_LOOP_UP:
2598 		/*
2599 		 * Now we just note that Loop has come up. We don't
2600 		 * actually do anything because we're waiting for a
2601 		 * Change Notify before activating the FC cleanup
2602 		 * thread to look at the state of the loop again.
2603 		 */
2604 		isp_prt(isp, ISP_LOGINFO, "Loop UP");
2605 		break;
2606 	case ISPASYNC_PROMENADE:
2607 	{
2608 		struct cam_path *tmppath;
2609 		const char *fmt = "Target %d (Loop 0x%x) Port ID 0x%x "
2610 		    "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
2611 		static const char *roles[4] = {
2612 		    "(none)", "Target", "Initiator", "Target/Initiator"
2613 		};
2614 		fcparam *fcp = isp->isp_param;
2615 		int tgt = *((int *) arg);
2616 		int is_tgt_mask = (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2617 		struct lportdb *lp = &fcp->portdb[tgt];
2618 
2619 		isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
2620 		    roles[lp->roles & 0x3],
2621 		    (lp->valid)? "Arrived" : "Departed",
2622 		    (u_int32_t) (lp->port_wwn >> 32),
2623 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
2624 		    (u_int32_t) (lp->node_wwn >> 32),
2625 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
2626 
2627 		ISPLOCK_2_CAMLOCK(isp);
2628 		if (xpt_create_path(&tmppath, NULL, cam_sim_path(isp->isp_sim),
2629 		    (target_id_t)tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2630 			CAMLOCK_2_ISPLOCK(isp);
2631                         break;
2632                 }
2633 		/*
2634 		 * Policy: only announce targets.
2635 		 */
2636 		if (lp->roles & is_tgt_mask) {
2637 			if (lp->valid) {
2638 				xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
2639 			} else {
2640 				xpt_async(AC_LOST_DEVICE, tmppath, NULL);
2641 			}
2642 		}
2643 		xpt_free_path(tmppath);
2644 		CAMLOCK_2_ISPLOCK(isp);
2645 		break;
2646 	}
2647 	case ISPASYNC_CHANGE_NOTIFY:
2648 		if (arg == ISPASYNC_CHANGE_PDB) {
2649 			isp_prt(isp, ISP_LOGINFO,
2650 			    "Port Database Changed");
2651 		} else if (arg == ISPASYNC_CHANGE_SNS) {
2652 			isp_prt(isp, ISP_LOGINFO,
2653 			    "Name Server Database Changed");
2654 		}
2655 		cv_signal(&isp->isp_osinfo.kthread_cv);
2656 		break;
2657 	case ISPASYNC_FABRIC_DEV:
2658 	{
2659 		int target, lrange;
2660 		struct lportdb *lp = NULL;
2661 		char *pt;
2662 		sns_ganrsp_t *resp = (sns_ganrsp_t *) arg;
2663 		u_int32_t portid;
2664 		u_int64_t wwpn, wwnn;
2665 		fcparam *fcp = isp->isp_param;
2666 
2667 		portid =
2668 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
2669 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
2670 		    (((u_int32_t) resp->snscb_port_id[2]));
2671 
2672 		wwpn =
2673 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
2674 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
2675 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
2676 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
2677 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
2678 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
2679 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
2680 		    (((u_int64_t)resp->snscb_portname[7]));
2681 
2682 		wwnn =
2683 		    (((u_int64_t)resp->snscb_nodename[0]) << 56) |
2684 		    (((u_int64_t)resp->snscb_nodename[1]) << 48) |
2685 		    (((u_int64_t)resp->snscb_nodename[2]) << 40) |
2686 		    (((u_int64_t)resp->snscb_nodename[3]) << 32) |
2687 		    (((u_int64_t)resp->snscb_nodename[4]) << 24) |
2688 		    (((u_int64_t)resp->snscb_nodename[5]) << 16) |
2689 		    (((u_int64_t)resp->snscb_nodename[6]) <<  8) |
2690 		    (((u_int64_t)resp->snscb_nodename[7]));
2691 		if (portid == 0 || wwpn == 0) {
2692 			break;
2693 		}
2694 
2695 		switch (resp->snscb_port_type) {
2696 		case 1:
2697 			pt = "   N_Port";
2698 			break;
2699 		case 2:
2700 			pt = "  NL_Port";
2701 			break;
2702 		case 3:
2703 			pt = "F/NL_Port";
2704 			break;
2705 		case 0x7f:
2706 			pt = "  Nx_Port";
2707 			break;
2708 		case 0x81:
2709 			pt = "  F_port";
2710 			break;
2711 		case 0x82:
2712 			pt = "  FL_Port";
2713 			break;
2714 		case 0x84:
2715 			pt = "   E_port";
2716 			break;
2717 		default:
2718 			pt = "?";
2719 			break;
2720 		}
2721 		isp_prt(isp, ISP_LOGINFO,
2722 		    "%s @ 0x%x, Node 0x%08x%08x Port %08x%08x",
2723 		    pt, portid, ((u_int32_t) (wwnn >> 32)), ((u_int32_t) wwnn),
2724 		    ((u_int32_t) (wwpn >> 32)), ((u_int32_t) wwpn));
2725 		/*
2726 		 * We're only interested in SCSI_FCP types (for now)
2727 		 */
2728 		if ((resp->snscb_fc4_types[2] & 1) == 0) {
2729 			break;
2730 		}
2731 		if (fcp->isp_topo != TOPO_F_PORT)
2732 			lrange = FC_SNS_ID+1;
2733 		else
2734 			lrange = 0;
2735 		/*
2736 		 * Is it already in our list?
2737 		 */
2738 		for (target = lrange; target < MAX_FC_TARG; target++) {
2739 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2740 				continue;
2741 			}
2742 			lp = &fcp->portdb[target];
2743 			if (lp->port_wwn == wwpn && lp->node_wwn == wwnn) {
2744 				lp->fabric_dev = 1;
2745 				break;
2746 			}
2747 		}
2748 		if (target < MAX_FC_TARG) {
2749 			break;
2750 		}
2751 		for (target = lrange; target < MAX_FC_TARG; target++) {
2752 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2753 				continue;
2754 			}
2755 			lp = &fcp->portdb[target];
2756 			if (lp->port_wwn == 0) {
2757 				break;
2758 			}
2759 		}
2760 		if (target == MAX_FC_TARG) {
2761 			isp_prt(isp, ISP_LOGWARN,
2762 			    "no more space for fabric devices");
2763 			break;
2764 		}
2765 		lp->node_wwn = wwnn;
2766 		lp->port_wwn = wwpn;
2767 		lp->portid = portid;
2768 		lp->fabric_dev = 1;
2769 		break;
2770 	}
2771 #ifdef	ISP_TARGET_MODE
2772 	case ISPASYNC_TARGET_MESSAGE:
2773 	{
2774 		tmd_msg_t *mp = arg;
2775 		isp_prt(isp, ISP_LOGALL,
2776 		    "bus %d iid %d tgt %d lun %d ttype %x tval %x msg[0]=%x",
2777 		    mp->nt_bus, (int) mp->nt_iid, (int) mp->nt_tgt,
2778 		    (int) mp->nt_lun, mp->nt_tagtype, mp->nt_tagval,
2779 		    mp->nt_msg[0]);
2780 		break;
2781 	}
2782 	case ISPASYNC_TARGET_EVENT:
2783 	{
2784 		tmd_event_t *ep = arg;
2785 		isp_prt(isp, ISP_LOGALL,
2786 		    "bus %d event code 0x%x", ep->ev_bus, ep->ev_event);
2787 		break;
2788 	}
2789 	case ISPASYNC_TARGET_ACTION:
2790 		switch (((isphdr_t *)arg)->rqs_entry_type) {
2791 		default:
2792 			isp_prt(isp, ISP_LOGWARN,
2793 			   "event 0x%x for unhandled target action",
2794 			    ((isphdr_t *)arg)->rqs_entry_type);
2795 			break;
2796 		case RQSTYPE_ATIO:
2797 			rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
2798 			break;
2799 		case RQSTYPE_ATIO2:
2800 			rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
2801 			break;
2802 		case RQSTYPE_CTIO2:
2803 		case RQSTYPE_CTIO:
2804 			rv = isp_handle_platform_ctio(isp, arg);
2805 			break;
2806 		case RQSTYPE_ENABLE_LUN:
2807 		case RQSTYPE_MODIFY_LUN:
2808 			if (IS_DUALBUS(isp)) {
2809 				bus =
2810 				    GET_BUS_VAL(((lun_entry_t *)arg)->le_rsvd);
2811 			} else {
2812 				bus = 0;
2813 			}
2814 			isp_cv_signal_rqe(isp, bus,
2815 			    ((lun_entry_t *)arg)->le_status);
2816 			break;
2817 		}
2818 		break;
2819 #endif
2820 	case ISPASYNC_FW_CRASH:
2821 	{
2822 		u_int16_t mbox1, mbox6;
2823 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
2824 		if (IS_DUALBUS(isp)) {
2825 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
2826 		} else {
2827 			mbox6 = 0;
2828 		}
2829                 isp_prt(isp, ISP_LOGERR,
2830                     "Internal Firmware on bus %d Error @ RISC Address 0x%x",
2831                     mbox6, mbox1);
2832 		isp_reinit(isp);
2833 		break;
2834 	}
2835 	case ISPASYNC_UNHANDLED_RESPONSE:
2836 		break;
2837 	default:
2838 		isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
2839 		break;
2840 	}
2841 	return (rv);
2842 }
2843 
2844 
2845 /*
2846  * Locks are held before coming here.
2847  */
2848 void
2849 isp_uninit(struct ispsoftc *isp)
2850 {
2851 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
2852 	DISABLE_INTS(isp);
2853 }
2854 
2855 void
2856 isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
2857 {
2858 	va_list ap;
2859 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
2860 		return;
2861 	}
2862 	printf("%s: ", device_get_nameunit(isp->isp_dev));
2863 	va_start(ap, fmt);
2864 	vprintf(fmt, ap);
2865 	va_end(ap);
2866 	printf("\n");
2867 }
2868