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