xref: /freebsd/sys/dev/aac/aac.c (revision 5773cccf19ef7b97e56c1101aa481c43149224da)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 Adaptec, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD$
30  */
31 
32 /*
33  * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
34  */
35 
36 #include "opt_aac.h"
37 
38 /* #include <stddef.h> */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/poll.h>
48 #if __FreeBSD_version >= 500005
49 #include <sys/selinfo.h>
50 #else
51 #include <sys/select.h>
52 #endif
53 
54 #include <dev/aac/aac_compat.h>
55 
56 #include <sys/bus.h>
57 #include <sys/conf.h>
58 #include <sys/devicestat.h>
59 #include <sys/disk.h>
60 #include <sys/signalvar.h>
61 #include <sys/time.h>
62 #include <sys/eventhandler.h>
63 
64 #include <machine/bus_memio.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
67 
68 #include <dev/aac/aacreg.h>
69 #include <dev/aac/aac_ioctl.h>
70 #include <dev/aac/aacvar.h>
71 #include <dev/aac/aac_tables.h>
72 #include <dev/aac/aac_cam.h>
73 
74 static void	aac_startup(void *arg);
75 static void	aac_add_container(struct aac_softc *sc,
76 				  struct aac_mntinforesp *mir, int f);
77 static void	aac_get_bus_info(struct aac_softc *sc);
78 
79 /* Command Processing */
80 static void	aac_timeout(struct aac_softc *sc);
81 static int	aac_start(struct aac_command *cm);
82 static void	aac_complete(void *context, int pending);
83 static int	aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
84 static void	aac_bio_complete(struct aac_command *cm);
85 static int	aac_wait_command(struct aac_command *cm, int timeout);
86 static void	aac_host_command(struct aac_softc *sc);
87 static void	aac_host_response(struct aac_softc *sc);
88 
89 /* Command Buffer Management */
90 static void	aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
91 				       int nseg, int error);
92 static int	aac_alloc_commands(struct aac_softc *sc);
93 static void	aac_free_commands(struct aac_softc *sc);
94 static void	aac_map_command(struct aac_command *cm);
95 static void	aac_unmap_command(struct aac_command *cm);
96 
97 /* Hardware Interface */
98 static void	aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
99 			       int error);
100 static int	aac_check_firmware(struct aac_softc *sc);
101 static int	aac_init(struct aac_softc *sc);
102 static int	aac_sync_command(struct aac_softc *sc, u_int32_t command,
103 				 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
104 				 u_int32_t arg3, u_int32_t *sp);
105 static int	aac_enqueue_fib(struct aac_softc *sc, int queue,
106 				struct aac_command *cm);
107 static int	aac_dequeue_fib(struct aac_softc *sc, int queue,
108 				u_int32_t *fib_size, struct aac_fib **fib_addr);
109 static int	aac_enqueue_response(struct aac_softc *sc, int queue,
110 				     struct aac_fib *fib);
111 
112 /* Falcon/PPC interface */
113 static int	aac_fa_get_fwstatus(struct aac_softc *sc);
114 static void	aac_fa_qnotify(struct aac_softc *sc, int qbit);
115 static int	aac_fa_get_istatus(struct aac_softc *sc);
116 static void	aac_fa_clear_istatus(struct aac_softc *sc, int mask);
117 static void	aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
118 				   u_int32_t arg0, u_int32_t arg1,
119 				   u_int32_t arg2, u_int32_t arg3);
120 static int	aac_fa_get_mailboxstatus(struct aac_softc *sc);
121 static void	aac_fa_set_interrupts(struct aac_softc *sc, int enable);
122 
123 struct aac_interface aac_fa_interface = {
124 	aac_fa_get_fwstatus,
125 	aac_fa_qnotify,
126 	aac_fa_get_istatus,
127 	aac_fa_clear_istatus,
128 	aac_fa_set_mailbox,
129 	aac_fa_get_mailboxstatus,
130 	aac_fa_set_interrupts
131 };
132 
133 /* StrongARM interface */
134 static int	aac_sa_get_fwstatus(struct aac_softc *sc);
135 static void	aac_sa_qnotify(struct aac_softc *sc, int qbit);
136 static int	aac_sa_get_istatus(struct aac_softc *sc);
137 static void	aac_sa_clear_istatus(struct aac_softc *sc, int mask);
138 static void	aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
139 				   u_int32_t arg0, u_int32_t arg1,
140 				   u_int32_t arg2, u_int32_t arg3);
141 static int	aac_sa_get_mailboxstatus(struct aac_softc *sc);
142 static void	aac_sa_set_interrupts(struct aac_softc *sc, int enable);
143 
144 struct aac_interface aac_sa_interface = {
145 	aac_sa_get_fwstatus,
146 	aac_sa_qnotify,
147 	aac_sa_get_istatus,
148 	aac_sa_clear_istatus,
149 	aac_sa_set_mailbox,
150 	aac_sa_get_mailboxstatus,
151 	aac_sa_set_interrupts
152 };
153 
154 /* i960Rx interface */
155 static int	aac_rx_get_fwstatus(struct aac_softc *sc);
156 static void	aac_rx_qnotify(struct aac_softc *sc, int qbit);
157 static int	aac_rx_get_istatus(struct aac_softc *sc);
158 static void	aac_rx_clear_istatus(struct aac_softc *sc, int mask);
159 static void	aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
160 				   u_int32_t arg0, u_int32_t arg1,
161 				   u_int32_t arg2, u_int32_t arg3);
162 static int	aac_rx_get_mailboxstatus(struct aac_softc *sc);
163 static void	aac_rx_set_interrupts(struct aac_softc *sc, int enable);
164 
165 struct aac_interface aac_rx_interface = {
166 	aac_rx_get_fwstatus,
167 	aac_rx_qnotify,
168 	aac_rx_get_istatus,
169 	aac_rx_clear_istatus,
170 	aac_rx_set_mailbox,
171 	aac_rx_get_mailboxstatus,
172 	aac_rx_set_interrupts
173 };
174 
175 /* Debugging and Diagnostics */
176 static void	aac_describe_controller(struct aac_softc *sc);
177 static char	*aac_describe_code(struct aac_code_lookup *table,
178 				   u_int32_t code);
179 
180 /* Management Interface */
181 static d_open_t		aac_open;
182 static d_close_t	aac_close;
183 static d_ioctl_t	aac_ioctl;
184 static d_poll_t		aac_poll;
185 static int		aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
186 static void		aac_handle_aif(struct aac_softc *sc,
187 					   struct aac_fib *fib);
188 static int		aac_rev_check(struct aac_softc *sc, caddr_t udata);
189 static int		aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
190 static int		aac_return_aif(struct aac_softc *sc, caddr_t uptr);
191 static int		aac_query_disk(struct aac_softc *sc, caddr_t uptr);
192 
193 #define AAC_CDEV_MAJOR	150
194 
195 static struct cdevsw aac_cdevsw = {
196 	aac_open,		/* open */
197 	aac_close,		/* close */
198 	noread,			/* read */
199 	nowrite,		/* write */
200 	aac_ioctl,		/* ioctl */
201 	aac_poll,		/* poll */
202 	nommap,			/* mmap */
203 	nostrategy,		/* strategy */
204 	"aac",			/* name */
205 	AAC_CDEV_MAJOR,		/* major */
206 	nodump,			/* dump */
207 	nopsize,		/* psize */
208 	0,			/* flags */
209 #if __FreeBSD_version < 500005
210 	-1,			/* bmaj */
211 #endif
212 };
213 
214 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
215 
216 /* sysctl node */
217 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
218 
219 /*
220  * Device Interface
221  */
222 
223 /*
224  * Initialise the controller and softc
225  */
226 int
227 aac_attach(struct aac_softc *sc)
228 {
229 	int error, unit;
230 
231 	debug_called(1);
232 
233 	/*
234 	 * Initialise per-controller queues.
235 	 */
236 	aac_initq_free(sc);
237 	aac_initq_ready(sc);
238 	aac_initq_busy(sc);
239 	aac_initq_complete(sc);
240 	aac_initq_bio(sc);
241 
242 #if __FreeBSD_version >= 500005
243 	/*
244 	 * Initialise command-completion task.
245 	 */
246 	TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
247 #endif
248 
249 	/* disable interrupts before we enable anything */
250 	AAC_MASK_INTERRUPTS(sc);
251 
252 	/* mark controller as suspended until we get ourselves organised */
253 	sc->aac_state |= AAC_STATE_SUSPEND;
254 
255 	/*
256 	 * Check that the firmware on the card is supported.
257 	 */
258 	if ((error = aac_check_firmware(sc)) != 0)
259 		return(error);
260 
261 	/*
262 	 * Allocate command structures.
263 	 */
264 	if ((error = aac_alloc_commands(sc)) != 0)
265 		return(error);
266 
267 	/* Init the sync fib lock */
268 	AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock");
269 
270 	/*
271 	 * Initialise the adapter.
272 	 */
273 	if ((error = aac_init(sc)) != 0)
274 		return(error);
275 
276 	/*
277 	 * Print a little information about the controller.
278 	 */
279 	aac_describe_controller(sc);
280 
281 	/*
282 	 * Register to probe our containers later.
283 	 */
284 	TAILQ_INIT(&sc->aac_container_tqh);
285 	AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
286 
287 	/*
288 	 * Lock for the AIF queue
289 	 */
290 	AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
291 
292 	sc->aac_ich.ich_func = aac_startup;
293 	sc->aac_ich.ich_arg = sc;
294 	if (config_intrhook_establish(&sc->aac_ich) != 0) {
295 		device_printf(sc->aac_dev,
296 			      "can't establish configuration hook\n");
297 		return(ENXIO);
298 	}
299 
300 	/*
301 	 * Make the control device.
302 	 */
303 	unit = device_get_unit(sc->aac_dev);
304 	sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
305 				 "aac%d", unit);
306 #if __FreeBSD_version > 500005
307 	(void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
308 	(void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
309 #endif
310 	sc->aac_dev_t->si_drv1 = sc;
311 
312 	/* Create the AIF thread */
313 #if __FreeBSD_version > 500005
314 	if (kthread_create((void(*)(void *))aac_host_command, sc,
315 			   &sc->aifthread, 0, 0, "aac%daif", unit))
316 #else
317 	if (kthread_create((void(*)(void *))aac_host_command, sc,
318 			   &sc->aifthread, "aac%daif", unit))
319 #endif
320 		panic("Could not create AIF thread\n");
321 
322 	/* Register the shutdown method to only be called post-dump */
323 	if ((EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown, sc->aac_dev,
324 				   SHUTDOWN_PRI_DEFAULT)) == NULL)
325 	device_printf(sc->aac_dev, "shutdown event registration failed\n");
326 
327 	/* Register with CAM for the non-DASD devices */
328 	if (!(sc->quirks & AAC_QUIRK_NOCAM))
329 		aac_get_bus_info(sc);
330 
331 	return(0);
332 }
333 
334 /*
335  * Probe for containers, create disks.
336  */
337 static void
338 aac_startup(void *arg)
339 {
340 	struct aac_softc *sc;
341 	struct aac_fib *fib;
342 	struct aac_mntinfo *mi;
343 	struct aac_mntinforesp *mir = NULL;
344 	int i = 0;
345 
346 	debug_called(1);
347 
348 	sc = (struct aac_softc *)arg;
349 
350 	/* disconnect ourselves from the intrhook chain */
351 	config_intrhook_disestablish(&sc->aac_ich);
352 
353 	aac_alloc_sync_fib(sc, &fib, 0);
354 	mi = (struct aac_mntinfo *)&fib->data[0];
355 
356 	/* loop over possible containers */
357 	do {
358 		/* request information on this container */
359 		bzero(mi, sizeof(struct aac_mntinfo));
360 		mi->Command = VM_NameServe;
361 		mi->MntType = FT_FILESYS;
362 		mi->MntCount = i;
363 		if (aac_sync_fib(sc, ContainerCommand, 0, fib,
364 				 sizeof(struct aac_mntinfo))) {
365 			debug(2, "error probing container %d", i);
366 			continue;
367 		}
368 
369 		mir = (struct aac_mntinforesp *)&fib->data[0];
370 		aac_add_container(sc, mir, 0);
371 		i++;
372 	} while ((i < mir->MntRespCount) && (i < AAC_MAX_CONTAINERS));
373 
374 	aac_release_sync_fib(sc);
375 
376 	/* poke the bus to actually attach the child devices */
377 	if (bus_generic_attach(sc->aac_dev))
378 		device_printf(sc->aac_dev, "bus_generic_attach failed\n");
379 
380 	/* mark the controller up */
381 	sc->aac_state &= ~AAC_STATE_SUSPEND;
382 
383 	/* enable interrupts now */
384 	AAC_UNMASK_INTERRUPTS(sc);
385 
386 	/* enable the timeout watchdog */
387 	timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
388 }
389 
390 /*
391  * Create a device to respresent a new container
392  */
393 static void
394 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
395 {
396 	struct aac_container *co;
397 	device_t child;
398 
399 	/*
400 	 * Check container volume type for validity.  Note that many of
401 	 * the possible types may never show up.
402 	 */
403 	if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
404 		MALLOC(co, struct aac_container *, sizeof *co, M_AACBUF,
405 		       M_NOWAIT);
406 		if (co == NULL)
407 			panic("Out of memory?!\n");
408 		debug(1, "id %x  name '%.16s'  size %u  type %d",
409 		      mir->MntTable[0].ObjectId,
410 		      mir->MntTable[0].FileSystemName,
411 		      mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
412 
413 		if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
414 			device_printf(sc->aac_dev, "device_add_child failed\n");
415 		else
416 			device_set_ivars(child, co);
417 		device_set_desc(child, aac_describe_code(aac_container_types,
418 				mir->MntTable[0].VolType));
419 		co->co_disk = child;
420 		co->co_found = f;
421 		bcopy(&mir->MntTable[0], &co->co_mntobj,
422 		      sizeof(struct aac_mntobj));
423 		AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
424 		TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
425 		AAC_LOCK_RELEASE(&sc->aac_container_lock);
426 	}
427 }
428 
429 /*
430  * Free all of the resources associated with (sc)
431  *
432  * Should not be called if the controller is active.
433  */
434 void
435 aac_free(struct aac_softc *sc)
436 {
437 	debug_called(1);
438 
439 	/* remove the control device */
440 	if (sc->aac_dev_t != NULL)
441 		destroy_dev(sc->aac_dev_t);
442 
443 	/* throw away any FIB buffers, discard the FIB DMA tag */
444 	if (sc->aac_fibs != NULL)
445 		aac_free_commands(sc);
446 	if (sc->aac_fib_dmat)
447 		bus_dma_tag_destroy(sc->aac_fib_dmat);
448 
449 	/* destroy the common area */
450 	if (sc->aac_common) {
451 		bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
452 		bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
453 				sc->aac_common_dmamap);
454 	}
455 	if (sc->aac_common_dmat)
456 		bus_dma_tag_destroy(sc->aac_common_dmat);
457 
458 	/* disconnect the interrupt handler */
459 	if (sc->aac_intr)
460 		bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
461 	if (sc->aac_irq != NULL)
462 		bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
463 				     sc->aac_irq);
464 
465 	/* destroy data-transfer DMA tag */
466 	if (sc->aac_buffer_dmat)
467 		bus_dma_tag_destroy(sc->aac_buffer_dmat);
468 
469 	/* destroy the parent DMA tag */
470 	if (sc->aac_parent_dmat)
471 		bus_dma_tag_destroy(sc->aac_parent_dmat);
472 
473 	/* release the register window mapping */
474 	if (sc->aac_regs_resource != NULL)
475 		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
476 				     sc->aac_regs_rid, sc->aac_regs_resource);
477 }
478 
479 /*
480  * Disconnect from the controller completely, in preparation for unload.
481  */
482 int
483 aac_detach(device_t dev)
484 {
485 	struct aac_softc *sc;
486 #if AAC_BROKEN
487 	int error;
488 #endif
489 
490 	debug_called(1);
491 
492 	sc = device_get_softc(dev);
493 
494 	if (sc->aac_state & AAC_STATE_OPEN)
495 	return(EBUSY);
496 
497 #if AAC_BROKEN
498 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
499 		sc->aifflags |= AAC_AIFFLAGS_EXIT;
500 		wakeup(sc->aifthread);
501 		tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
502 	}
503 
504 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
505 		panic("Cannot shutdown AIF thread\n");
506 
507 	if ((error = aac_shutdown(dev)))
508 		return(error);
509 
510 	aac_free(sc);
511 
512 	return(0);
513 #else
514 	return (EBUSY);
515 #endif
516 }
517 
518 /*
519  * Bring the controller down to a dormant state and detach all child devices.
520  *
521  * This function is called before detach or system shutdown.
522  *
523  * Note that we can assume that the bioq on the controller is empty, as we won't
524  * allow shutdown if any device is open.
525  */
526 int
527 aac_shutdown(device_t dev)
528 {
529 	struct aac_softc *sc;
530 	struct aac_fib *fib;
531 	struct aac_close_command *cc;
532 	int s;
533 
534 	debug_called(1);
535 
536 	sc = device_get_softc(dev);
537 
538 	s = splbio();
539 
540 	sc->aac_state |= AAC_STATE_SUSPEND;
541 
542 	/*
543 	 * Send a Container shutdown followed by a HostShutdown FIB to the
544 	 * controller to convince it that we don't want to talk to it anymore.
545 	 * We've been closed and all I/O completed already
546 	 */
547 	device_printf(sc->aac_dev, "shutting down controller...");
548 
549 	aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
550 	cc = (struct aac_close_command *)&fib->data[0];
551 
552 	bzero(cc, sizeof(struct aac_close_command));
553 	cc->Command = VM_CloseAll;
554 	cc->ContainerId = 0xffffffff;
555 	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
556 	    sizeof(struct aac_close_command)))
557 		printf("FAILED.\n");
558 	else {
559 		fib->data[0] = 0;
560 		/*
561 		 * XXX Issuing this command to the controller makes it shut down
562 		 * but also keeps it from coming back up without a reset of the
563 		 * PCI bus.  This is not desirable if you are just unloading the
564 		 * driver module with the intent to reload it later.
565 		 */
566 		if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
567 		    fib, 1)) {
568 			printf("FAILED.\n");
569 		} else {
570 			printf("done.\n");
571 		}
572 	}
573 
574 	AAC_MASK_INTERRUPTS(sc);
575 
576 	splx(s);
577 	return(0);
578 }
579 
580 /*
581  * Bring the controller to a quiescent state, ready for system suspend.
582  */
583 int
584 aac_suspend(device_t dev)
585 {
586 	struct aac_softc *sc;
587 	int s;
588 
589 	debug_called(1);
590 
591 	sc = device_get_softc(dev);
592 
593 	s = splbio();
594 
595 	sc->aac_state |= AAC_STATE_SUSPEND;
596 
597 	AAC_MASK_INTERRUPTS(sc);
598 	splx(s);
599 	return(0);
600 }
601 
602 /*
603  * Bring the controller back to a state ready for operation.
604  */
605 int
606 aac_resume(device_t dev)
607 {
608 	struct aac_softc *sc;
609 
610 	debug_called(1);
611 
612 	sc = device_get_softc(dev);
613 
614 	sc->aac_state &= ~AAC_STATE_SUSPEND;
615 	AAC_UNMASK_INTERRUPTS(sc);
616 	return(0);
617 }
618 
619 /*
620  * Take an interrupt.
621  */
622 void
623 aac_intr(void *arg)
624 {
625 	struct aac_softc *sc;
626 	u_int16_t reason;
627 
628 	debug_called(2);
629 
630 	sc = (struct aac_softc *)arg;
631 
632 	reason = AAC_GET_ISTATUS(sc);
633 
634 	/* controller wants to talk to the log */
635 	if (reason & AAC_DB_PRINTF) {
636 		AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
637 		aac_print_printf(sc);
638 	}
639 
640 	/* controller has a message for us? */
641 	if (reason & AAC_DB_COMMAND_READY) {
642 		AAC_CLEAR_ISTATUS(sc, AAC_DB_COMMAND_READY);
643 		/* XXX What happens if the thread is already awake? */
644 		if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
645 			sc->aifflags |= AAC_AIFFLAGS_PENDING;
646 			wakeup(sc->aifthread);
647 		}
648 	}
649 
650 	/* controller has a response for us? */
651 	if (reason & AAC_DB_RESPONSE_READY) {
652 		AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
653 		aac_host_response(sc);
654 	}
655 
656 	/*
657 	 * spurious interrupts that we don't use - reset the mask and clear the
658 	 * interrupts
659 	 */
660 	if (reason & (AAC_DB_COMMAND_NOT_FULL | AAC_DB_RESPONSE_NOT_FULL)) {
661 		AAC_UNMASK_INTERRUPTS(sc);
662 		AAC_CLEAR_ISTATUS(sc, AAC_DB_COMMAND_NOT_FULL |
663 				  AAC_DB_RESPONSE_NOT_FULL);
664 	}
665 };
666 
667 /*
668  * Command Processing
669  */
670 
671 /*
672  * Start as much queued I/O as possible on the controller
673  */
674 void
675 aac_startio(struct aac_softc *sc)
676 {
677 	struct aac_command *cm;
678 
679 	debug_called(2);
680 
681 	for (;;) {
682 		/*
683 		 * Try to get a command that's been put off for lack of
684 		 * resources
685 		 */
686 		cm = aac_dequeue_ready(sc);
687 
688 		/*
689 		 * Try to build a command off the bio queue (ignore error
690 		 * return)
691 		 */
692 		if (cm == NULL)
693 			aac_bio_command(sc, &cm);
694 
695 		/* nothing to do? */
696 		if (cm == NULL)
697 			break;
698 
699 		/* try to give the command to the controller */
700 		if (aac_start(cm) == EBUSY) {
701 			/* put it on the ready queue for later */
702 			aac_requeue_ready(cm);
703 			break;
704 		}
705 	}
706 }
707 
708 /*
709  * Deliver a command to the controller; allocate controller resources at the
710  * last moment when possible.
711  */
712 static int
713 aac_start(struct aac_command *cm)
714 {
715 	struct aac_softc *sc;
716 	int error;
717 
718 	debug_called(2);
719 
720 	sc = cm->cm_sc;
721 
722 	/* get the command mapped */
723 	aac_map_command(cm);
724 
725 	/* fix up the address values in the FIB */
726 	cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
727 	cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
728 
729 	/* save a pointer to the command for speedy reverse-lookup */
730 	cm->cm_fib->Header.SenderData = (u_int32_t)cm;	/* XXX 64-bit physical
731 							 * address issue */
732 
733 	/* put the FIB on the outbound queue */
734 	error = aac_enqueue_fib(sc, cm->cm_queue, cm);
735 	return(error);
736 }
737 
738 /*
739  * Handle notification of one or more FIBs coming from the controller.
740  */
741 static void
742 aac_host_command(struct aac_softc *sc)
743 {
744 	struct aac_fib *fib;
745 	u_int32_t fib_size;
746 	int size;
747 
748 	debug_called(2);
749 
750 	sc->aifflags |= AAC_AIFFLAGS_RUNNING;
751 
752 	while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) {
753 		if (!(sc->aifflags & AAC_AIFFLAGS_PENDING))
754 			tsleep(sc->aifthread, PRIBIO, "aifthd", 15 * hz);
755 
756 		sc->aifflags &= ~AAC_AIFFLAGS_PENDING;
757 		for (;;) {
758 			if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
759 					    &fib_size, &fib))
760 				break;	/* nothing to do */
761 
762 			AAC_PRINT_FIB(sc, fib);
763 
764 			switch (fib->Header.Command) {
765 			case AifRequest:
766 				aac_handle_aif(sc, fib);
767 				break;
768 			default:
769 				device_printf(sc->aac_dev, "unknown command "
770 					      "from controller\n");
771 				break;
772 			}
773 
774 			/* Return the AIF to the controller. */
775 			if ((fib->Header.XferState == 0) ||
776 			    (fib->Header.StructType != AAC_FIBTYPE_TFIB))
777 				break;
778 
779 			if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
780 				fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
781 				*(AAC_FSAStatus*)fib->data = ST_OK;
782 
783 				/* XXX Compute the Size field? */
784 				size = fib->Header.Size;
785 				if (size > sizeof(struct aac_fib)) {
786 					size = sizeof(struct aac_fib);
787 					fib->Header.Size = size;
788 				}
789 				/*
790 				 * Since we did not generate this command, it
791 				 * cannot go through the normal
792 				 * enqueue->startio chain.
793 				 */
794 				aac_enqueue_response(sc,
795 						     AAC_ADAP_NORM_RESP_QUEUE,
796 						     fib);
797 			}
798 		}
799 	}
800 	sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
801 	wakeup(sc->aac_dev);
802 
803 #if __FreeBSD_version > 500005
804 	mtx_lock(&Giant);
805 #endif
806 	kthread_exit(0);
807 }
808 
809 /*
810  * Handle notification of one or more FIBs completed by the controller
811  */
812 static void
813 aac_host_response(struct aac_softc *sc)
814 {
815 	struct aac_command *cm;
816 	struct aac_fib *fib;
817 	u_int32_t fib_size;
818 
819 	debug_called(2);
820 
821 	for (;;) {
822 		/* look for completed FIBs on our queue */
823 		if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
824 				    &fib))
825 			break;	/* nothing to do */
826 
827 		/* get the command, unmap and queue for later processing */
828 		cm = (struct aac_command *)fib->Header.SenderData;
829 		if (cm == NULL) {
830 			AAC_PRINT_FIB(sc, fib);
831 		} else {
832 			aac_remove_busy(cm);
833 			aac_unmap_command(cm);		/* XXX defer? */
834 			aac_enqueue_complete(cm);
835 		}
836 	}
837 
838 	/* handle completion processing */
839 #if __FreeBSD_version >= 500005
840 	taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
841 #else
842 	aac_complete(sc, 0);
843 #endif
844 }
845 
846 /*
847  * Process completed commands.
848  */
849 static void
850 aac_complete(void *context, int pending)
851 {
852 	struct aac_softc *sc;
853 	struct aac_command *cm;
854 
855 	debug_called(2);
856 
857 	sc = (struct aac_softc *)context;
858 
859 	/* pull completed commands off the queue */
860 	for (;;) {
861 		cm = aac_dequeue_complete(sc);
862 		if (cm == NULL)
863 			break;
864 		cm->cm_flags |= AAC_CMD_COMPLETED;
865 
866 		/* is there a completion handler? */
867 		if (cm->cm_complete != NULL) {
868 			cm->cm_complete(cm);
869 		} else {
870 			/* assume that someone is sleeping on this command */
871 			wakeup(cm);
872 		}
873 	}
874 
875 	/* see if we can start some more I/O */
876 	aac_startio(sc);
877 }
878 
879 /*
880  * Handle a bio submitted from a disk device.
881  */
882 void
883 aac_submit_bio(struct bio *bp)
884 {
885 	struct aac_disk *ad;
886 	struct aac_softc *sc;
887 
888 	debug_called(2);
889 
890 	ad = (struct aac_disk *)bp->bio_dev->si_drv1;
891 	sc = ad->ad_controller;
892 
893 	/* queue the BIO and try to get some work done */
894 	aac_enqueue_bio(sc, bp);
895 	aac_startio(sc);
896 }
897 
898 /*
899  * Get a bio and build a command to go with it.
900  */
901 static int
902 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
903 {
904 	struct aac_command *cm;
905 	struct aac_fib *fib;
906 	struct aac_blockread *br;
907 	struct aac_blockwrite *bw;
908 	struct aac_disk *ad;
909 	struct bio *bp;
910 
911 	debug_called(2);
912 
913 	/* get the resources we will need */
914 	cm = NULL;
915 	if ((bp = aac_dequeue_bio(sc)) == NULL)
916 		goto fail;
917 	if (aac_alloc_command(sc, &cm))	/* get a command */
918 		goto fail;
919 
920 	/* fill out the command */
921 	cm->cm_data = (void *)bp->bio_data;
922 	cm->cm_datalen = bp->bio_bcount;
923 	cm->cm_complete = aac_bio_complete;
924 	cm->cm_private = bp;
925 	cm->cm_timestamp = time_second;
926 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
927 
928 	/* build the FIB */
929 	fib = cm->cm_fib;
930 	fib->Header.XferState =
931 	AAC_FIBSTATE_HOSTOWNED   |
932 	AAC_FIBSTATE_INITIALISED |
933 	AAC_FIBSTATE_FROMHOST	 |
934 	AAC_FIBSTATE_REXPECTED   |
935 	AAC_FIBSTATE_NORM;
936 	fib->Header.Command = ContainerCommand;
937 	fib->Header.Size = sizeof(struct aac_fib_header);
938 
939 	/* build the read/write request */
940 	ad = (struct aac_disk *)bp->bio_dev->si_drv1;
941 	if (BIO_IS_READ(bp)) {
942 		br = (struct aac_blockread *)&fib->data[0];
943 		br->Command = VM_CtBlockRead;
944 		br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
945 		br->BlockNumber = bp->bio_pblkno;
946 		br->ByteCount = bp->bio_bcount;
947 		fib->Header.Size += sizeof(struct aac_blockread);
948 		cm->cm_sgtable = &br->SgMap;
949 		cm->cm_flags |= AAC_CMD_DATAIN;
950 	} else {
951 		bw = (struct aac_blockwrite *)&fib->data[0];
952 		bw->Command = VM_CtBlockWrite;
953 		bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
954 		bw->BlockNumber = bp->bio_pblkno;
955 		bw->ByteCount = bp->bio_bcount;
956 		bw->Stable = CUNSTABLE;	/* XXX what's appropriate here? */
957 		fib->Header.Size += sizeof(struct aac_blockwrite);
958 		cm->cm_flags |= AAC_CMD_DATAOUT;
959 		cm->cm_sgtable = &bw->SgMap;
960 	}
961 
962 	*cmp = cm;
963 	return(0);
964 
965 fail:
966 	if (bp != NULL)
967 		aac_enqueue_bio(sc, bp);
968 	if (cm != NULL)
969 		aac_release_command(cm);
970 	return(ENOMEM);
971 }
972 
973 /*
974  * Handle a bio-instigated command that has been completed.
975  */
976 static void
977 aac_bio_complete(struct aac_command *cm)
978 {
979 	struct aac_blockread_response *brr;
980 	struct aac_blockwrite_response *bwr;
981 	struct bio *bp;
982 	AAC_FSAStatus status;
983 
984 	/* fetch relevant status and then release the command */
985 	bp = (struct bio *)cm->cm_private;
986 	if (BIO_IS_READ(bp)) {
987 		brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
988 		status = brr->Status;
989 	} else {
990 		bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
991 		status = bwr->Status;
992 	}
993 	aac_release_command(cm);
994 
995 	/* fix up the bio based on status */
996 	if (status == ST_OK) {
997 		bp->bio_resid = 0;
998 	} else {
999 		bp->bio_error = EIO;
1000 		bp->bio_flags |= BIO_ERROR;
1001 		/* pass an error string out to the disk layer */
1002 		bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1003 						    status);
1004 	}
1005 	aac_biodone(bp);
1006 }
1007 
1008 /*
1009  * Submit a command to the controller, return when it completes.
1010  * XXX This is very dangerous!  If the card has gone out to lunch, we could
1011  *     be stuck here forever.  At the same time, signals are not caught
1012  *     because there is a risk that a signal could wakeup the tsleep before
1013  *     the card has a chance to complete the command.  The passed in timeout
1014  *     is ignored for the same reason.  Since there is no way to cancel a
1015  *     command in progress, we should probably create a 'dead' queue where
1016  *     commands go that have been interrupted/timed-out/etc, that keeps them
1017  *     out of the free pool.  That way, if the card is just slow, it won't
1018  *     spam the memory of a command that has been recycled.
1019  */
1020 static int
1021 aac_wait_command(struct aac_command *cm, int timeout)
1022 {
1023 	int s, error = 0;
1024 
1025 	debug_called(2);
1026 
1027 	/* Put the command on the ready queue and get things going */
1028 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1029 	aac_enqueue_ready(cm);
1030 	aac_startio(cm->cm_sc);
1031 	s = splbio();
1032 	while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
1033 		error = tsleep(cm, PRIBIO, "aacwait", 0);
1034 	}
1035 	splx(s);
1036 	return(error);
1037 }
1038 
1039 /*
1040  *Command Buffer Management
1041  */
1042 
1043 /*
1044  * Allocate a command.
1045  */
1046 int
1047 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1048 {
1049 	struct aac_command *cm;
1050 
1051 	debug_called(3);
1052 
1053 	if ((cm = aac_dequeue_free(sc)) == NULL)
1054 		return(ENOMEM);
1055 
1056 	*cmp = cm;
1057 	return(0);
1058 }
1059 
1060 /*
1061  * Release a command back to the freelist.
1062  */
1063 void
1064 aac_release_command(struct aac_command *cm)
1065 {
1066 	debug_called(3);
1067 
1068 	/* (re)initialise the command/FIB */
1069 	cm->cm_sgtable = NULL;
1070 	cm->cm_flags = 0;
1071 	cm->cm_complete = NULL;
1072 	cm->cm_private = NULL;
1073 	cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1074 	cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1075 	cm->cm_fib->Header.Flags = 0;
1076 	cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1077 
1078 	/*
1079 	 * These are duplicated in aac_start to cover the case where an
1080 	 * intermediate stage may have destroyed them.  They're left
1081 	 * initialised here for debugging purposes only.
1082 	 */
1083 	cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
1084 	cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
1085 
1086 	aac_enqueue_free(cm);
1087 }
1088 
1089 /*
1090  * Map helper for command/FIB allocation.
1091  */
1092 static void
1093 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1094 {
1095 	struct aac_softc *sc;
1096 
1097 	sc = (struct aac_softc *)arg;
1098 
1099 	debug_called(3);
1100 
1101 	sc->aac_fibphys = segs[0].ds_addr;
1102 }
1103 
1104 /*
1105  * Allocate and initialise commands/FIBs for this adapter.
1106  */
1107 static int
1108 aac_alloc_commands(struct aac_softc *sc)
1109 {
1110 	struct aac_command *cm;
1111 	int i;
1112 
1113 	debug_called(1);
1114 
1115 	/* allocate the FIBs in DMAable memory and load them */
1116 	if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs,
1117 			     BUS_DMA_NOWAIT, &sc->aac_fibmap)) {
1118 		printf("Not enough contiguous memory available.\n");
1119 		return (ENOMEM);
1120 	}
1121 	bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs,
1122 			AAC_FIB_COUNT * sizeof(struct aac_fib),
1123 			aac_map_command_helper, sc, 0);
1124 
1125 	/* initialise constant fields in the command structure */
1126 	for (i = 0; i < AAC_FIB_COUNT; i++) {
1127 		cm = &sc->aac_command[i];
1128 		cm->cm_sc = sc;
1129 		cm->cm_fib = sc->aac_fibs + i;
1130 		cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib));
1131 
1132 		if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap))
1133 			aac_release_command(cm);
1134 	}
1135 	return (0);
1136 }
1137 
1138 /*
1139  * Free FIBs owned by this adapter.
1140  */
1141 static void
1142 aac_free_commands(struct aac_softc *sc)
1143 {
1144 	int i;
1145 
1146 	debug_called(1);
1147 
1148 	for (i = 0; i < AAC_FIB_COUNT; i++)
1149 		bus_dmamap_destroy(sc->aac_buffer_dmat,
1150 				   sc->aac_command[i].cm_datamap);
1151 
1152 	bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap);
1153 	bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap);
1154 }
1155 
1156 /*
1157  * Command-mapping helper function - populate this command's s/g table.
1158  */
1159 static void
1160 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1161 {
1162 	struct aac_command *cm;
1163 	struct aac_fib *fib;
1164 	struct aac_sg_table *sg;
1165 	int i;
1166 
1167 	debug_called(3);
1168 
1169 	cm = (struct aac_command *)arg;
1170 	fib = cm->cm_fib;
1171 
1172 	/* find the s/g table */
1173 	sg = cm->cm_sgtable;
1174 
1175 	/* copy into the FIB */
1176 	if (sg != NULL) {
1177 		sg->SgCount = nseg;
1178 		for (i = 0; i < nseg; i++) {
1179 			sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1180 			sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1181 		}
1182 		/* update the FIB size for the s/g count */
1183 		fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1184 	}
1185 
1186 }
1187 
1188 /*
1189  * Map a command into controller-visible space.
1190  */
1191 static void
1192 aac_map_command(struct aac_command *cm)
1193 {
1194 	struct aac_softc *sc;
1195 
1196 	debug_called(2);
1197 
1198 	sc = cm->cm_sc;
1199 
1200 	/* don't map more than once */
1201 	if (cm->cm_flags & AAC_CMD_MAPPED)
1202 		return;
1203 
1204 	if (cm->cm_datalen != 0) {
1205 		bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
1206 				cm->cm_data, cm->cm_datalen,
1207 				aac_map_command_sg, cm, 0);
1208 
1209 	if (cm->cm_flags & AAC_CMD_DATAIN)
1210 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1211 				BUS_DMASYNC_PREREAD);
1212 	if (cm->cm_flags & AAC_CMD_DATAOUT)
1213 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1214 				BUS_DMASYNC_PREWRITE);
1215 	}
1216 	cm->cm_flags |= AAC_CMD_MAPPED;
1217 }
1218 
1219 /*
1220  * Unmap a command from controller-visible space.
1221  */
1222 static void
1223 aac_unmap_command(struct aac_command *cm)
1224 {
1225 	struct aac_softc *sc;
1226 
1227 	debug_called(2);
1228 
1229 	sc = cm->cm_sc;
1230 
1231 	if (!(cm->cm_flags & AAC_CMD_MAPPED))
1232 		return;
1233 
1234 	if (cm->cm_datalen != 0) {
1235 		if (cm->cm_flags & AAC_CMD_DATAIN)
1236 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1237 					BUS_DMASYNC_POSTREAD);
1238 		if (cm->cm_flags & AAC_CMD_DATAOUT)
1239 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1240 					BUS_DMASYNC_POSTWRITE);
1241 
1242 		bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1243 	}
1244 	cm->cm_flags &= ~AAC_CMD_MAPPED;
1245 }
1246 
1247 /*
1248  * Hardware Interface
1249  */
1250 
1251 /*
1252  * Initialise the adapter.
1253  */
1254 static void
1255 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1256 {
1257 	struct aac_softc *sc;
1258 
1259 	debug_called(1);
1260 
1261 	sc = (struct aac_softc *)arg;
1262 
1263 	sc->aac_common_busaddr = segs[0].ds_addr;
1264 }
1265 
1266 /*
1267  * Retrieve the firmware version numbers.  Dell PERC2/QC cards with
1268  * firmware version 1.x are not compatible with this driver.
1269  */
1270 static int
1271 aac_check_firmware(struct aac_softc *sc)
1272 {
1273 	u_int32_t major, minor;
1274 
1275 	debug_called(1);
1276 
1277 	if (sc->quirks & AAC_QUIRK_PERC2QC) {
1278 		if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1279 				     NULL)) {
1280 			device_printf(sc->aac_dev,
1281 				      "Error reading firmware version\n");
1282 			return (EIO);
1283 		}
1284 
1285 		/* These numbers are stored as ASCII! */
1286 		major = (AAC_GETREG4(sc, AAC_SA_MAILBOX + 4) & 0xff) - 0x30;
1287 		minor = (AAC_GETREG4(sc, AAC_SA_MAILBOX + 8) & 0xff) - 0x30;
1288 		if (major == 1) {
1289 			device_printf(sc->aac_dev,
1290 			    "Firmware version %d.%d is not supported.\n",
1291 			    major, minor);
1292 			return (EINVAL);
1293 		}
1294 	}
1295 
1296 	return (0);
1297 }
1298 
1299 static int
1300 aac_init(struct aac_softc *sc)
1301 {
1302 	struct aac_adapter_init	*ip;
1303 	time_t then;
1304 	u_int32_t code;
1305 	u_int8_t *qaddr;
1306 
1307 	debug_called(1);
1308 
1309 	/*
1310 	 * First wait for the adapter to come ready.
1311 	 */
1312 	then = time_second;
1313 	do {
1314 		code = AAC_GET_FWSTATUS(sc);
1315 		if (code & AAC_SELF_TEST_FAILED) {
1316 			device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1317 			return(ENXIO);
1318 		}
1319 		if (code & AAC_KERNEL_PANIC) {
1320 			device_printf(sc->aac_dev,
1321 				      "FATAL: controller kernel panic\n");
1322 			return(ENXIO);
1323 		}
1324 		if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1325 			device_printf(sc->aac_dev,
1326 				      "FATAL: controller not coming ready, "
1327 					   "status %x\n", code);
1328 			return(ENXIO);
1329 		}
1330 	} while (!(code & AAC_UP_AND_RUNNING));
1331 
1332 	/*
1333 	 * Create DMA tag for the common structure and allocate it.
1334 	 */
1335 	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1336 			       1, 0,			/* algnmnt, boundary */
1337 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1338 			       BUS_SPACE_MAXADDR, 	/* highaddr */
1339 			       NULL, NULL, 		/* filter, filterarg */
1340 			       sizeof(struct aac_common), /* maxsize */
1341 			       1,			/* nsegments */
1342 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1343 			       0,			/* flags */
1344 			       &sc->aac_common_dmat)) {
1345 		device_printf(sc->aac_dev,
1346 			      "can't allocate common structure DMA tag\n");
1347 		return(ENOMEM);
1348 	}
1349 	if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1350 			     BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1351 		device_printf(sc->aac_dev, "can't allocate common structure\n");
1352 		return(ENOMEM);
1353 	}
1354 	bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1355 			sc->aac_common, sizeof(*sc->aac_common), aac_common_map,
1356 			sc, 0);
1357 	bzero(sc->aac_common, sizeof(*sc->aac_common));
1358 
1359 	/*
1360 	 * Fill in the init structure.  This tells the adapter about the
1361 	 * physical location of various important shared data structures.
1362 	 */
1363 	ip = &sc->aac_common->ac_init;
1364 	ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1365 
1366 	ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1367 					 offsetof(struct aac_common, ac_fibs);
1368 	ip->AdapterFibsVirtualAddress = &sc->aac_common->ac_fibs[0];
1369 	ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1370 	ip->AdapterFibAlign = sizeof(struct aac_fib);
1371 
1372 	ip->PrintfBufferAddress = sc->aac_common_busaddr +
1373 				  offsetof(struct aac_common, ac_printf);
1374 	ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1375 
1376 	ip->HostPhysMemPages = 0;		/* not used? */
1377 	ip->HostElapsedSeconds = time_second;	/* reset later if invalid */
1378 
1379 	/*
1380 	 * Initialise FIB queues.  Note that it appears that the layout of the
1381 	 * indexes and the segmentation of the entries may be mandated by the
1382 	 * adapter, which is only told about the base of the queue index fields.
1383 	 *
1384 	 * The initial values of the indices are assumed to inform the adapter
1385 	 * of the sizes of the respective queues, and theoretically it could
1386 	 * work out the entire layout of the queue structures from this.  We
1387 	 * take the easy route and just lay this area out like everyone else
1388 	 * does.
1389 	 *
1390 	 * The Linux driver uses a much more complex scheme whereby several
1391 	 * header records are kept for each queue.  We use a couple of generic
1392 	 * list manipulation functions which 'know' the size of each list by
1393 	 * virtue of a table.
1394 	 */
1395 	qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN;
1396 	qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN;
1397 	sc->aac_queues = (struct aac_queue_table *)qaddr;
1398 	ip->CommHeaderAddress = sc->aac_common_busaddr +
1399 				((u_int32_t)sc->aac_queues -
1400 				(u_int32_t)sc->aac_common);
1401 	bzero(sc->aac_queues, sizeof(struct aac_queue_table));
1402 
1403 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1404 		AAC_HOST_NORM_CMD_ENTRIES;
1405 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1406 		AAC_HOST_NORM_CMD_ENTRIES;
1407 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1408 		AAC_HOST_HIGH_CMD_ENTRIES;
1409 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1410 		AAC_HOST_HIGH_CMD_ENTRIES;
1411 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1412 		AAC_ADAP_NORM_CMD_ENTRIES;
1413 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1414 		AAC_ADAP_NORM_CMD_ENTRIES;
1415 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1416 		AAC_ADAP_HIGH_CMD_ENTRIES;
1417 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1418 		AAC_ADAP_HIGH_CMD_ENTRIES;
1419 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1420 		AAC_HOST_NORM_RESP_ENTRIES;
1421 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1422 		AAC_HOST_NORM_RESP_ENTRIES;
1423 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1424 		AAC_HOST_HIGH_RESP_ENTRIES;
1425 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1426 		AAC_HOST_HIGH_RESP_ENTRIES;
1427 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1428 		AAC_ADAP_NORM_RESP_ENTRIES;
1429 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1430 		AAC_ADAP_NORM_RESP_ENTRIES;
1431 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1432 		AAC_ADAP_HIGH_RESP_ENTRIES;
1433 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1434 		AAC_ADAP_HIGH_RESP_ENTRIES;
1435 	sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1436 		&sc->aac_queues->qt_HostNormCmdQueue[0];
1437 	sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1438 		&sc->aac_queues->qt_HostHighCmdQueue[0];
1439 	sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1440 		&sc->aac_queues->qt_AdapNormCmdQueue[0];
1441 	sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1442 		&sc->aac_queues->qt_AdapHighCmdQueue[0];
1443 	sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1444 		&sc->aac_queues->qt_HostNormRespQueue[0];
1445 	sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1446 		&sc->aac_queues->qt_HostHighRespQueue[0];
1447 	sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1448 		&sc->aac_queues->qt_AdapNormRespQueue[0];
1449 	sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1450 		&sc->aac_queues->qt_AdapHighRespQueue[0];
1451 
1452 	/*
1453 	 * Do controller-type-specific initialisation
1454 	 */
1455 	switch (sc->aac_hwif) {
1456 	case AAC_HWIF_I960RX:
1457 		AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1458 		break;
1459 	}
1460 
1461 	/*
1462 	 * Give the init structure to the controller.
1463 	 */
1464 	if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1465 			     sc->aac_common_busaddr +
1466 			     offsetof(struct aac_common, ac_init), 0, 0, 0,
1467 			     NULL)) {
1468 		device_printf(sc->aac_dev,
1469 			      "error establishing init structure\n");
1470 		return(EIO);
1471 	}
1472 
1473 	return(0);
1474 }
1475 
1476 /*
1477  * Send a synchronous command to the controller and wait for a result.
1478  */
1479 static int
1480 aac_sync_command(struct aac_softc *sc, u_int32_t command,
1481 		 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1482 		 u_int32_t *sp)
1483 {
1484 	time_t then;
1485 	u_int32_t status;
1486 
1487 	debug_called(3);
1488 
1489 	/* populate the mailbox */
1490 	AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1491 
1492 	/* ensure the sync command doorbell flag is cleared */
1493 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1494 
1495 	/* then set it to signal the adapter */
1496 	AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1497 
1498 	/* spin waiting for the command to complete */
1499 	then = time_second;
1500 	do {
1501 		if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1502 			debug(2, "timed out");
1503 			return(EIO);
1504 		}
1505 	} while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1506 
1507 	/* clear the completion flag */
1508 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1509 
1510 	/* get the command status */
1511 	status = AAC_GET_MAILBOXSTATUS(sc);
1512 	if (sp != NULL)
1513 		*sp = status;
1514 	return(0);
1515 }
1516 
1517 /*
1518  * Grab the sync fib area.
1519  */
1520 int
1521 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags)
1522 {
1523 
1524 	/*
1525 	 * If the force flag is set, the system is shutting down, or in
1526 	 * trouble.  Ignore the mutex.
1527 	 */
1528 	if (!(flags & AAC_SYNC_LOCK_FORCE))
1529 		AAC_LOCK_ACQUIRE(&sc->aac_sync_lock);
1530 
1531 	*fib = &sc->aac_common->ac_sync_fib;
1532 
1533 	return (1);
1534 }
1535 
1536 /*
1537  * Release the sync fib area.
1538  */
1539 void
1540 aac_release_sync_fib(struct aac_softc *sc)
1541 {
1542 
1543 	AAC_LOCK_RELEASE(&sc->aac_sync_lock);
1544 }
1545 
1546 /*
1547  * Send a synchronous FIB to the controller and wait for a result.
1548  */
1549 int
1550 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1551 		 struct aac_fib *fib, u_int16_t datasize)
1552 {
1553 	debug_called(3);
1554 
1555 	if (datasize > AAC_FIB_DATASIZE)
1556 		return(EINVAL);
1557 
1558 	/*
1559 	 * Set up the sync FIB
1560 	 */
1561 	fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
1562 				AAC_FIBSTATE_INITIALISED |
1563 				AAC_FIBSTATE_EMPTY;
1564 	fib->Header.XferState |= xferstate;
1565 	fib->Header.Command = command;
1566 	fib->Header.StructType = AAC_FIBTYPE_TFIB;
1567 	fib->Header.Size = sizeof(struct aac_fib) + datasize;
1568 	fib->Header.SenderSize = sizeof(struct aac_fib);
1569 	fib->Header.SenderFibAddress = (u_int32_t)fib;
1570 	fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1571 					 offsetof(struct aac_common,
1572 						  ac_sync_fib);
1573 
1574 	/*
1575 	 * Give the FIB to the controller, wait for a response.
1576 	 */
1577 	if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
1578 			     fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
1579 		debug(2, "IO error");
1580 		return(EIO);
1581 	}
1582 
1583 	return (0);
1584 }
1585 
1586 /*
1587  * Adapter-space FIB queue manipulation
1588  *
1589  * Note that the queue implementation here is a little funky; neither the PI or
1590  * CI will ever be zero.  This behaviour is a controller feature.
1591  */
1592 static struct {
1593 	int		size;
1594 	int		notify;
1595 } aac_qinfo[] = {
1596 	{AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1597 	{AAC_HOST_HIGH_CMD_ENTRIES, 0},
1598 	{AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1599 	{AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1600 	{AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1601 	{AAC_HOST_HIGH_RESP_ENTRIES, 0},
1602 	{AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1603 	{AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1604 };
1605 
1606 /*
1607  * Atomically insert an entry into the nominated queue, returns 0 on success or
1608  * EBUSY if the queue is full.
1609  *
1610  * Note: it would be more efficient to defer notifying the controller in
1611  *	 the case where we may be inserting several entries in rapid succession,
1612  *	 but implementing this usefully may be difficult (it would involve a
1613  *	 separate queue/notify interface).
1614  */
1615 static int
1616 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
1617 {
1618 	u_int32_t pi, ci;
1619 	int s, error;
1620 	u_int32_t fib_size;
1621 	u_int32_t fib_addr;
1622 
1623 	debug_called(3);
1624 
1625 	fib_size = cm->cm_fib->Header.Size;
1626 	fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
1627 
1628 	s = splbio();
1629 
1630 	/* get the producer/consumer indices */
1631 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1632 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1633 
1634 	/* wrap the queue? */
1635 	if (pi >= aac_qinfo[queue].size)
1636 		pi = 0;
1637 
1638 	/* check for queue full */
1639 	if ((pi + 1) == ci) {
1640 		error = EBUSY;
1641 		goto out;
1642 	}
1643 
1644 	/* populate queue entry */
1645 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1646 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1647 
1648 	/* update producer index */
1649 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1650 
1651 	/*
1652 	 * To avoid a race with its completion interrupt, place this command on
1653 	 * the busy queue prior to advertising it to the controller.
1654 	 */
1655 	aac_enqueue_busy(cm);
1656 
1657 	/* notify the adapter if we know how */
1658 	if (aac_qinfo[queue].notify != 0)
1659 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1660 
1661 	error = 0;
1662 
1663 out:
1664 	splx(s);
1665 	return(error);
1666 }
1667 
1668 /*
1669  * Atomically remove one entry from the nominated queue, returns 0 on
1670  * success or ENOENT if the queue is empty.
1671  */
1672 static int
1673 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1674 		struct aac_fib **fib_addr)
1675 {
1676 	u_int32_t pi, ci;
1677 	int s, error;
1678 	int notify;
1679 
1680 	debug_called(3);
1681 
1682 	s = splbio();
1683 
1684 	/* get the producer/consumer indices */
1685 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1686 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1687 
1688 	/* check for queue empty */
1689 	if (ci == pi) {
1690 		error = ENOENT;
1691 		goto out;
1692 	}
1693 
1694 	notify = 0;
1695 	if (ci == pi + 1)
1696 		notify++;
1697 
1698 	/* wrap the queue? */
1699 	if (ci >= aac_qinfo[queue].size)
1700 		ci = 0;
1701 
1702 	/* fetch the entry */
1703 	*fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1704 	*fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] +
1705 				       ci)->aq_fib_addr;
1706 
1707 	/* update consumer index */
1708 	sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1709 
1710 	/* if we have made the queue un-full, notify the adapter */
1711 	if (notify && (aac_qinfo[queue].notify != 0))
1712 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1713 	error = 0;
1714 
1715 out:
1716 	splx(s);
1717 	return(error);
1718 }
1719 
1720 /*
1721  * Put our response to an Adapter Initialed Fib on the response queue
1722  */
1723 static int
1724 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
1725 {
1726 	u_int32_t pi, ci;
1727 	int s, error;
1728 	u_int32_t fib_size;
1729 	u_int32_t fib_addr;
1730 
1731 	debug_called(1);
1732 
1733 	/* Tell the adapter where the FIB is */
1734 	fib_size = fib->Header.Size;
1735 	fib_addr = fib->Header.SenderFibAddress;
1736 	fib->Header.ReceiverFibAddress = fib_addr;
1737 
1738 	s = splbio();
1739 
1740 	/* get the producer/consumer indices */
1741 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1742 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1743 
1744 	/* wrap the queue? */
1745 	if (pi >= aac_qinfo[queue].size)
1746 		pi = 0;
1747 
1748 	/* check for queue full */
1749 	if ((pi + 1) == ci) {
1750 		error = EBUSY;
1751 		goto out;
1752 	}
1753 
1754 	/* populate queue entry */
1755 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1756 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1757 
1758 	/* update producer index */
1759 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1760 
1761 	/* notify the adapter if we know how */
1762 	if (aac_qinfo[queue].notify != 0)
1763 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1764 
1765 	error = 0;
1766 
1767 out:
1768 	splx(s);
1769 	return(error);
1770 }
1771 
1772 /*
1773  * Check for commands that have been outstanding for a suspiciously long time,
1774  * and complain about them.
1775  */
1776 static void
1777 aac_timeout(struct aac_softc *sc)
1778 {
1779 	int s;
1780 	struct aac_command *cm;
1781 	time_t deadline;
1782 
1783 #if 0
1784 	/* simulate an interrupt to handle possibly-missed interrupts */
1785 	/*
1786 	 * XXX This was done to work around another bug which has since been
1787 	 * fixed.  It is dangerous anyways because you don't want multiple
1788 	 * threads in the interrupt handler at the same time!  If calling
1789 	 * is deamed neccesary in the future, proper mutexes must be used.
1790 	 */
1791 	s = splbio();
1792 	aac_intr(sc);
1793 	splx(s);
1794 
1795 	/* kick the I/O queue to restart it in the case of deadlock */
1796 	aac_startio(sc);
1797 #endif
1798 
1799 	/*
1800 	 * traverse the busy command list, bitch about late commands once
1801 	 * only.
1802 	 */
1803 	deadline = time_second - AAC_CMD_TIMEOUT;
1804 	s = splbio();
1805 	TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
1806 		if ((cm->cm_timestamp  < deadline)
1807 			/* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
1808 			cm->cm_flags |= AAC_CMD_TIMEDOUT;
1809 			device_printf(sc->aac_dev,
1810 				      "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
1811 				      cm, (int)(time_second-cm->cm_timestamp));
1812 			AAC_PRINT_FIB(sc, cm->cm_fib);
1813 		}
1814 	}
1815 	splx(s);
1816 
1817 	/* reset the timer for next time */
1818 	timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
1819 	return;
1820 }
1821 
1822 /*
1823  * Interface Function Vectors
1824  */
1825 
1826 /*
1827  * Read the current firmware status word.
1828  */
1829 static int
1830 aac_sa_get_fwstatus(struct aac_softc *sc)
1831 {
1832 	debug_called(3);
1833 
1834 	return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
1835 }
1836 
1837 static int
1838 aac_rx_get_fwstatus(struct aac_softc *sc)
1839 {
1840 	debug_called(3);
1841 
1842 	return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
1843 }
1844 
1845 static int
1846 aac_fa_get_fwstatus(struct aac_softc *sc)
1847 {
1848 	int val;
1849 
1850 	debug_called(3);
1851 
1852 	val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
1853 	return (val);
1854 }
1855 
1856 /*
1857  * Notify the controller of a change in a given queue
1858  */
1859 
1860 static void
1861 aac_sa_qnotify(struct aac_softc *sc, int qbit)
1862 {
1863 	debug_called(3);
1864 
1865 	AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
1866 }
1867 
1868 static void
1869 aac_rx_qnotify(struct aac_softc *sc, int qbit)
1870 {
1871 	debug_called(3);
1872 
1873 	AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
1874 }
1875 
1876 static void
1877 aac_fa_qnotify(struct aac_softc *sc, int qbit)
1878 {
1879 	debug_called(3);
1880 
1881 	AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
1882 	AAC_FA_HACK(sc);
1883 }
1884 
1885 /*
1886  * Get the interrupt reason bits
1887  */
1888 static int
1889 aac_sa_get_istatus(struct aac_softc *sc)
1890 {
1891 	debug_called(3);
1892 
1893 	return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
1894 }
1895 
1896 static int
1897 aac_rx_get_istatus(struct aac_softc *sc)
1898 {
1899 	debug_called(3);
1900 
1901 	return(AAC_GETREG4(sc, AAC_RX_ODBR));
1902 }
1903 
1904 static int
1905 aac_fa_get_istatus(struct aac_softc *sc)
1906 {
1907 	int val;
1908 
1909 	debug_called(3);
1910 
1911 	val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
1912 	return (val);
1913 }
1914 
1915 /*
1916  * Clear some interrupt reason bits
1917  */
1918 static void
1919 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
1920 {
1921 	debug_called(3);
1922 
1923 	AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
1924 }
1925 
1926 static void
1927 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
1928 {
1929 	debug_called(3);
1930 
1931 	AAC_SETREG4(sc, AAC_RX_ODBR, mask);
1932 }
1933 
1934 static void
1935 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
1936 {
1937 	debug_called(3);
1938 
1939 	AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
1940 	AAC_FA_HACK(sc);
1941 }
1942 
1943 /*
1944  * Populate the mailbox and set the command word
1945  */
1946 static void
1947 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
1948 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
1949 {
1950 	debug_called(4);
1951 
1952 	AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
1953 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
1954 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
1955 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
1956 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
1957 }
1958 
1959 static void
1960 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
1961 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
1962 {
1963 	debug_called(4);
1964 
1965 	AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
1966 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
1967 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
1968 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
1969 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
1970 }
1971 
1972 static void
1973 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
1974 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
1975 {
1976 	debug_called(4);
1977 
1978 	AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
1979 	AAC_FA_HACK(sc);
1980 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
1981 	AAC_FA_HACK(sc);
1982 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
1983 	AAC_FA_HACK(sc);
1984 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
1985 	AAC_FA_HACK(sc);
1986 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
1987 	AAC_FA_HACK(sc);
1988 }
1989 
1990 /*
1991  * Fetch the immediate command status word
1992  */
1993 static int
1994 aac_sa_get_mailboxstatus(struct aac_softc *sc)
1995 {
1996 	debug_called(4);
1997 
1998 	return(AAC_GETREG4(sc, AAC_SA_MAILBOX));
1999 }
2000 
2001 static int
2002 aac_rx_get_mailboxstatus(struct aac_softc *sc)
2003 {
2004 	debug_called(4);
2005 
2006 	return(AAC_GETREG4(sc, AAC_RX_MAILBOX));
2007 }
2008 
2009 static int
2010 aac_fa_get_mailboxstatus(struct aac_softc *sc)
2011 {
2012 	int val;
2013 
2014 	debug_called(4);
2015 
2016 	val = AAC_GETREG4(sc, AAC_FA_MAILBOX);
2017 	return (val);
2018 }
2019 
2020 /*
2021  * Set/clear interrupt masks
2022  */
2023 static void
2024 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2025 {
2026 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2027 
2028 	if (enable) {
2029 		AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2030 	} else {
2031 		AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2032 	}
2033 }
2034 
2035 static void
2036 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2037 {
2038 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2039 
2040 	if (enable) {
2041 		AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2042 	} else {
2043 		AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2044 	}
2045 }
2046 
2047 static void
2048 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2049 {
2050 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2051 
2052 	if (enable) {
2053 		AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2054 		AAC_FA_HACK(sc);
2055 	} else {
2056 		AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2057 		AAC_FA_HACK(sc);
2058 	}
2059 }
2060 
2061 /*
2062  * Debugging and Diagnostics
2063  */
2064 
2065 /*
2066  * Print some information about the controller.
2067  */
2068 static void
2069 aac_describe_controller(struct aac_softc *sc)
2070 {
2071 	struct aac_fib *fib;
2072 	struct aac_adapter_info	*info;
2073 
2074 	debug_called(2);
2075 
2076 	aac_alloc_sync_fib(sc, &fib, 0);
2077 
2078 	fib->data[0] = 0;
2079 	if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2080 		device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2081 		aac_release_sync_fib(sc);
2082 		return;
2083 	}
2084 	info = (struct aac_adapter_info *)&fib->data[0];
2085 
2086 	device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n",
2087 		      aac_describe_code(aac_cpu_variant, info->CpuVariant),
2088 		      info->ClockSpeed, info->BufferMem / (1024 * 1024),
2089 		      aac_describe_code(aac_battery_platform,
2090 					info->batteryPlatform));
2091 
2092 	/* save the kernel revision structure for later use */
2093 	sc->aac_revision = info->KernelRevision;
2094 	device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2095 		      info->KernelRevision.external.comp.major,
2096 		      info->KernelRevision.external.comp.minor,
2097 		      info->KernelRevision.external.comp.dash,
2098 		      info->KernelRevision.buildNumber,
2099 		      (u_int32_t)(info->SerialNumber & 0xffffff));
2100 
2101 	aac_release_sync_fib(sc);
2102 }
2103 
2104 /*
2105  * Look up a text description of a numeric error code and return a pointer to
2106  * same.
2107  */
2108 static char *
2109 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2110 {
2111 	int i;
2112 
2113 	for (i = 0; table[i].string != NULL; i++)
2114 		if (table[i].code == code)
2115 			return(table[i].string);
2116 	return(table[i + 1].string);
2117 }
2118 
2119 /*
2120  * Management Interface
2121  */
2122 
2123 static int
2124 aac_open(dev_t dev, int flags, int fmt, d_thread_t *td)
2125 {
2126 	struct aac_softc *sc;
2127 
2128 	debug_called(2);
2129 
2130 	sc = dev->si_drv1;
2131 
2132 	/* Check to make sure the device isn't already open */
2133 	if (sc->aac_state & AAC_STATE_OPEN) {
2134 		return EBUSY;
2135 	}
2136 	sc->aac_state |= AAC_STATE_OPEN;
2137 
2138 	return 0;
2139 }
2140 
2141 static int
2142 aac_close(dev_t dev, int flags, int fmt, d_thread_t *td)
2143 {
2144 	struct aac_softc *sc;
2145 
2146 	debug_called(2);
2147 
2148 	sc = dev->si_drv1;
2149 
2150 	/* Mark this unit as no longer open  */
2151 	sc->aac_state &= ~AAC_STATE_OPEN;
2152 
2153 	return 0;
2154 }
2155 
2156 static int
2157 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2158 {
2159 	union aac_statrequest *as;
2160 	struct aac_softc *sc;
2161 	int error = 0;
2162 	int i;
2163 
2164 	debug_called(2);
2165 
2166 	as = (union aac_statrequest *)arg;
2167 	sc = dev->si_drv1;
2168 
2169 	switch (cmd) {
2170 	case AACIO_STATS:
2171 		switch (as->as_item) {
2172 		case AACQ_FREE:
2173 		case AACQ_BIO:
2174 		case AACQ_READY:
2175 		case AACQ_BUSY:
2176 		case AACQ_COMPLETE:
2177 			bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2178 			      sizeof(struct aac_qstat));
2179 			break;
2180 		default:
2181 			error = ENOENT;
2182 			break;
2183 		}
2184 	break;
2185 
2186 	case FSACTL_SENDFIB:
2187 		arg = *(caddr_t*)arg;
2188 	case FSACTL_LNX_SENDFIB:
2189 		debug(1, "FSACTL_SENDFIB");
2190 		error = aac_ioctl_sendfib(sc, arg);
2191 		break;
2192 	case FSACTL_AIF_THREAD:
2193 	case FSACTL_LNX_AIF_THREAD:
2194 		debug(1, "FSACTL_AIF_THREAD");
2195 		error = EINVAL;
2196 		break;
2197 	case FSACTL_OPEN_GET_ADAPTER_FIB:
2198 		arg = *(caddr_t*)arg;
2199 	case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2200 		debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2201 		/*
2202 		 * Pass the caller out an AdapterFibContext.
2203 		 *
2204 		 * Note that because we only support one opener, we
2205 		 * basically ignore this.  Set the caller's context to a magic
2206 		 * number just in case.
2207 		 *
2208 		 * The Linux code hands the driver a pointer into kernel space,
2209 		 * and then trusts it when the caller hands it back.  Aiee!
2210 		 * Here, we give it the proc pointer of the per-adapter aif
2211 		 * thread. It's only used as a sanity check in other calls.
2212 		 */
2213 		i = (int)sc->aifthread;
2214 		error = copyout(&i, arg, sizeof(i));
2215 		break;
2216 	case FSACTL_GET_NEXT_ADAPTER_FIB:
2217 		arg = *(caddr_t*)arg;
2218 	case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2219 		debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2220 		error = aac_getnext_aif(sc, arg);
2221 		break;
2222 	case FSACTL_CLOSE_GET_ADAPTER_FIB:
2223 	case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2224 		debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2225 		/* don't do anything here */
2226 		break;
2227 	case FSACTL_MINIPORT_REV_CHECK:
2228 		arg = *(caddr_t*)arg;
2229 	case FSACTL_LNX_MINIPORT_REV_CHECK:
2230 		debug(1, "FSACTL_MINIPORT_REV_CHECK");
2231 		error = aac_rev_check(sc, arg);
2232 		break;
2233 	case FSACTL_QUERY_DISK:
2234 		arg = *(caddr_t*)arg;
2235 	case FSACTL_LNX_QUERY_DISK:
2236 		debug(1, "FSACTL_QUERY_DISK");
2237 		error = aac_query_disk(sc, arg);
2238 			break;
2239 	case FSACTL_DELETE_DISK:
2240 	case FSACTL_LNX_DELETE_DISK:
2241 		/*
2242 		 * We don't trust the underland to tell us when to delete a
2243 		 * container, rather we rely on an AIF coming from the
2244 		 * controller
2245 		 */
2246 		error = 0;
2247 		break;
2248 	default:
2249 		debug(1, "unsupported cmd 0x%lx\n", cmd);
2250 		error = EINVAL;
2251 		break;
2252 	}
2253 	return(error);
2254 }
2255 
2256 static int
2257 aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2258 {
2259 	struct aac_softc *sc;
2260 	int revents;
2261 
2262 	sc = dev->si_drv1;
2263 	revents = 0;
2264 
2265 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2266 	if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2267 		if (sc->aac_aifq_tail != sc->aac_aifq_head)
2268 			revents |= poll_events & (POLLIN | POLLRDNORM);
2269 	}
2270 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2271 
2272 	if (revents == 0) {
2273 		if (poll_events & (POLLIN | POLLRDNORM))
2274 			selrecord(td, &sc->rcv_select);
2275 	}
2276 
2277 	return (revents);
2278 }
2279 
2280 /*
2281  * Send a FIB supplied from userspace
2282  */
2283 static int
2284 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2285 {
2286 	struct aac_command *cm;
2287 	int size, error;
2288 
2289 	debug_called(2);
2290 
2291 	cm = NULL;
2292 
2293 	/*
2294 	 * Get a command
2295 	 */
2296 	if (aac_alloc_command(sc, &cm)) {
2297 		error = EBUSY;
2298 		goto out;
2299 	}
2300 
2301 	/*
2302 	 * Fetch the FIB header, then re-copy to get data as well.
2303 	 */
2304 	if ((error = copyin(ufib, cm->cm_fib,
2305 			    sizeof(struct aac_fib_header))) != 0)
2306 		goto out;
2307 	size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2308 	if (size > sizeof(struct aac_fib)) {
2309 		device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
2310 			      size, sizeof(struct aac_fib));
2311 		size = sizeof(struct aac_fib);
2312 	}
2313 	if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2314 		goto out;
2315 	cm->cm_fib->Header.Size = size;
2316 	cm->cm_timestamp = time_second;
2317 
2318 	/*
2319 	 * Pass the FIB to the controller, wait for it to complete.
2320 	 */
2321 	if ((error = aac_wait_command(cm, 30)) != 0) {	/* XXX user timeout? */
2322 		printf("aac_wait_command return %d\n", error);
2323 		goto out;
2324 	}
2325 
2326 	/*
2327 	 * Copy the FIB and data back out to the caller.
2328 	 */
2329 	size = cm->cm_fib->Header.Size;
2330 	if (size > sizeof(struct aac_fib)) {
2331 		device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
2332 			      size, sizeof(struct aac_fib));
2333 		size = sizeof(struct aac_fib);
2334 	}
2335 	error = copyout(cm->cm_fib, ufib, size);
2336 
2337 out:
2338 	if (cm != NULL) {
2339 		aac_release_command(cm);
2340 	}
2341 	return(error);
2342 }
2343 
2344 /*
2345  * Handle an AIF sent to us by the controller; queue it for later reference.
2346  * If the queue fills up, then drop the older entries.
2347  */
2348 static void
2349 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2350 {
2351 	struct aac_aif_command *aif;
2352 	struct aac_container *co, *co_next;
2353 	struct aac_mntinfo *mi;
2354 	struct aac_mntinforesp *mir = NULL;
2355 	u_int16_t rsize;
2356 	int next, found;
2357 	int added = 0, i = 0;
2358 
2359 	debug_called(2);
2360 
2361 	aif = (struct aac_aif_command*)&fib->data[0];
2362 	aac_print_aif(sc, aif);
2363 
2364 	/* Is it an event that we should care about? */
2365 	switch (aif->command) {
2366 	case AifCmdEventNotify:
2367 		switch (aif->data.EN.type) {
2368 		case AifEnAddContainer:
2369 		case AifEnDeleteContainer:
2370 			/*
2371 			 * A container was added or deleted, but the message
2372 			 * doesn't tell us anything else!  Re-enumerate the
2373 			 * containers and sort things out.
2374 			 */
2375 			aac_alloc_sync_fib(sc, &fib, 0);
2376 			mi = (struct aac_mntinfo *)&fib->data[0];
2377 			do {
2378 				/*
2379 				 * Ask the controller for its containers one at
2380 				 * a time.
2381 				 * XXX What if the controller's list changes
2382 				 * midway through this enumaration?
2383 				 * XXX This should be done async.
2384 				 */
2385 				bzero(mi, sizeof(struct aac_mntinfo));
2386 				mi->Command = VM_NameServe;
2387 				mi->MntType = FT_FILESYS;
2388 				mi->MntCount = i;
2389 				rsize = sizeof(mir);
2390 				if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2391 						 sizeof(struct aac_mntinfo))) {
2392 					debug(2, "Error probing container %d\n",
2393 					      i);
2394 					continue;
2395 				}
2396 				mir = (struct aac_mntinforesp *)&fib->data[0];
2397 				/*
2398 				 * Check the container against our list.
2399 				 * co->co_found was already set to 0 in a
2400 				 * previous run.
2401 				 */
2402 				if ((mir->Status == ST_OK) &&
2403 				    (mir->MntTable[0].VolType != CT_NONE)) {
2404 					found = 0;
2405 					TAILQ_FOREACH(co,
2406 						      &sc->aac_container_tqh,
2407 						      co_link) {
2408 						if (co->co_mntobj.ObjectId ==
2409 						    mir->MntTable[0].ObjectId) {
2410 							co->co_found = 1;
2411 							found = 1;
2412 							break;
2413 						}
2414 					}
2415 					/*
2416 					 * If the container matched, continue
2417 					 * in the list.
2418 					 */
2419 					if (found) {
2420 						i++;
2421 						continue;
2422 					}
2423 
2424 					/*
2425 					 * This is a new container.  Do all the
2426 					 * appropriate things to set it up.						 */
2427 					aac_add_container(sc, mir, 1);
2428 					added = 1;
2429 				}
2430 				i++;
2431 			} while ((i < mir->MntRespCount) &&
2432 				 (i < AAC_MAX_CONTAINERS));
2433 			aac_release_sync_fib(sc);
2434 
2435 			/*
2436 			 * Go through our list of containers and see which ones
2437 			 * were not marked 'found'.  Since the controller didn't
2438 			 * list them they must have been deleted.  Do the
2439 			 * appropriate steps to destroy the device.  Also reset
2440 			 * the co->co_found field.
2441 			 */
2442 			co = TAILQ_FIRST(&sc->aac_container_tqh);
2443 			while (co != NULL) {
2444 				if (co->co_found == 0) {
2445 					device_delete_child(sc->aac_dev,
2446 							    co->co_disk);
2447 					co_next = TAILQ_NEXT(co, co_link);
2448 					AAC_LOCK_ACQUIRE(&sc->
2449 							aac_container_lock);
2450 					TAILQ_REMOVE(&sc->aac_container_tqh, co,
2451 						     co_link);
2452 					AAC_LOCK_RELEASE(&sc->
2453 							 aac_container_lock);
2454 					FREE(co, M_AACBUF);
2455 					co = co_next;
2456 				} else {
2457 					co->co_found = 0;
2458 					co = TAILQ_NEXT(co, co_link);
2459 				}
2460 			}
2461 
2462 			/* Attach the newly created containers */
2463 			if (added)
2464 				bus_generic_attach(sc->aac_dev);
2465 
2466 			break;
2467 
2468 		default:
2469 			break;
2470 		}
2471 
2472 	default:
2473 		break;
2474 	}
2475 
2476 	/* Copy the AIF data to the AIF queue for ioctl retrieval */
2477 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2478 	next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2479 	if (next != sc->aac_aifq_tail) {
2480 		bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2481 		sc->aac_aifq_head = next;
2482 
2483 		/* On the off chance that someone is sleeping for an aif... */
2484 		if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2485 			wakeup(sc->aac_aifq);
2486 		/* Wakeup any poll()ers */
2487 		selwakeup(&sc->rcv_select);
2488 	}
2489 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2490 
2491 	return;
2492 }
2493 
2494 /*
2495  * Return the Revision of the driver to userspace and check to see if the
2496  * userspace app is possibly compatible.  This is extremely bogus since
2497  * our driver doesn't follow Adaptec's versioning system.  Cheat by just
2498  * returning what the card reported.
2499  */
2500 static int
2501 aac_rev_check(struct aac_softc *sc, caddr_t udata)
2502 {
2503 	struct aac_rev_check rev_check;
2504 	struct aac_rev_check_resp rev_check_resp;
2505 	int error = 0;
2506 
2507 	debug_called(2);
2508 
2509 	/*
2510 	 * Copyin the revision struct from userspace
2511 	 */
2512 	if ((error = copyin(udata, (caddr_t)&rev_check,
2513 			sizeof(struct aac_rev_check))) != 0) {
2514 		return error;
2515 	}
2516 
2517 	debug(2, "Userland revision= %d\n",
2518 	      rev_check.callingRevision.buildNumber);
2519 
2520 	/*
2521 	 * Doctor up the response struct.
2522 	 */
2523 	rev_check_resp.possiblyCompatible = 1;
2524 	rev_check_resp.adapterSWRevision.external.ul =
2525 	    sc->aac_revision.external.ul;
2526 	rev_check_resp.adapterSWRevision.buildNumber =
2527 	    sc->aac_revision.buildNumber;
2528 
2529 	return(copyout((caddr_t)&rev_check_resp, udata,
2530 			sizeof(struct aac_rev_check_resp)));
2531 }
2532 
2533 /*
2534  * Pass the caller the next AIF in their queue
2535  */
2536 static int
2537 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2538 {
2539 	struct get_adapter_fib_ioctl agf;
2540 	int error, s;
2541 
2542 	debug_called(2);
2543 
2544 	if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2545 
2546 		/*
2547 		 * Check the magic number that we gave the caller.
2548 		 */
2549 		if (agf.AdapterFibContext != (int)sc->aifthread) {
2550 			error = EFAULT;
2551 		} else {
2552 
2553 			s = splbio();
2554 			error = aac_return_aif(sc, agf.AifFib);
2555 
2556 			if ((error == EAGAIN) && (agf.Wait)) {
2557 				sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2558 				while (error == EAGAIN) {
2559 					error = tsleep(sc->aac_aifq, PRIBIO |
2560 						       PCATCH, "aacaif", 0);
2561 					if (error == 0)
2562 						error = aac_return_aif(sc,
2563 						    agf.AifFib);
2564 				}
2565 				sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2566 			}
2567 		splx(s);
2568 		}
2569 	}
2570 	return(error);
2571 }
2572 
2573 /*
2574  * Hand the next AIF off the top of the queue out to userspace.
2575  */
2576 static int
2577 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2578 {
2579 	int error;
2580 
2581 	debug_called(2);
2582 
2583 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2584 	if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2585 		error = EAGAIN;
2586 	} else {
2587 		error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2588 				sizeof(struct aac_aif_command));
2589 		if (error)
2590 			printf("aac_return_aif: copyout returned %d\n", error);
2591 		if (!error)
2592 			sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) %
2593 					    AAC_AIFQ_LENGTH;
2594 	}
2595 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2596 	return(error);
2597 }
2598 
2599 /*
2600  * Give the userland some information about the container.  The AAC arch
2601  * expects the driver to be a SCSI passthrough type driver, so it expects
2602  * the containers to have b:t:l numbers.  Fake it.
2603  */
2604 static int
2605 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2606 {
2607 	struct aac_query_disk query_disk;
2608 	struct aac_container *co;
2609 	struct aac_disk	*disk;
2610 	int error, id;
2611 
2612 	debug_called(2);
2613 
2614 	disk = NULL;
2615 
2616 	error = copyin(uptr, (caddr_t)&query_disk,
2617 		       sizeof(struct aac_query_disk));
2618 	if (error)
2619 		return (error);
2620 
2621 	id = query_disk.ContainerNumber;
2622 	if (id == -1)
2623 		return (EINVAL);
2624 
2625 	AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2626 	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2627 		if (co->co_mntobj.ObjectId == id)
2628 			break;
2629 		}
2630 
2631 	if (co == NULL) {
2632 			query_disk.Valid = 0;
2633 			query_disk.Locked = 0;
2634 			query_disk.Deleted = 1;		/* XXX is this right? */
2635 	} else {
2636 		disk = device_get_softc(co->co_disk);
2637 		query_disk.Valid = 1;
2638 		query_disk.Locked =
2639 		    (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2640 		query_disk.Deleted = 0;
2641 		query_disk.Bus = device_get_unit(sc->aac_dev);
2642 		query_disk.Target = disk->unit;
2643 		query_disk.Lun = 0;
2644 		query_disk.UnMapped = 0;
2645 		bcopy(disk->ad_dev_t->si_name,
2646 		      &query_disk.diskDeviceName[0], 10);
2647 	}
2648 	AAC_LOCK_RELEASE(&sc->aac_container_lock);
2649 
2650 	error = copyout((caddr_t)&query_disk, uptr,
2651 			sizeof(struct aac_query_disk));
2652 
2653 	return (error);
2654 }
2655 
2656 static void
2657 aac_get_bus_info(struct aac_softc *sc)
2658 {
2659 	struct aac_fib *fib;
2660 	struct aac_ctcfg *c_cmd;
2661 	struct aac_ctcfg_resp *c_resp;
2662 	struct aac_vmioctl *vmi;
2663 	struct aac_vmi_businf_resp *vmi_resp;
2664 	struct aac_getbusinf businfo;
2665 	struct aac_cam_inf *caminf;
2666 	device_t child;
2667 	int i, found, error;
2668 
2669 	aac_alloc_sync_fib(sc, &fib, 0);
2670 	c_cmd = (struct aac_ctcfg *)&fib->data[0];
2671 	bzero(c_cmd, sizeof(struct aac_ctcfg));
2672 
2673 	c_cmd->Command = VM_ContainerConfig;
2674 	c_cmd->cmd = CT_GET_SCSI_METHOD;
2675 	c_cmd->param = 0;
2676 
2677 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2678 	    sizeof(struct aac_ctcfg));
2679 	if (error) {
2680 		device_printf(sc->aac_dev, "Error %d sending "
2681 		    "VM_ContainerConfig command\n", error);
2682 		aac_release_sync_fib(sc);
2683 		return;
2684 	}
2685 
2686 	c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2687 	if (c_resp->Status != ST_OK) {
2688 		device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2689 		    c_resp->Status);
2690 		aac_release_sync_fib(sc);
2691 		return;
2692 	}
2693 
2694 	sc->scsi_method_id = c_resp->param;
2695 
2696 	vmi = (struct aac_vmioctl *)&fib->data[0];
2697 	bzero(vmi, sizeof(struct aac_vmioctl));
2698 
2699 	vmi->Command = VM_Ioctl;
2700 	vmi->ObjType = FT_DRIVE;
2701 	vmi->MethId = sc->scsi_method_id;
2702 	vmi->ObjId = 0;
2703 	vmi->IoctlCmd = GetBusInfo;
2704 
2705 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2706 	    sizeof(struct aac_vmioctl));
2707 	if (error) {
2708 		device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2709 		    error);
2710 		aac_release_sync_fib(sc);
2711 		return;
2712 	}
2713 
2714 	vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2715 	if (vmi_resp->Status != ST_OK) {
2716 		device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
2717 		    vmi_resp->Status);
2718 		aac_release_sync_fib(sc);
2719 		return;
2720 	}
2721 
2722 	bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2723 	aac_release_sync_fib(sc);
2724 
2725 	found = 0;
2726 	for (i = 0; i < businfo.BusCount; i++) {
2727 		if (businfo.BusValid[i] != AAC_BUS_VALID)
2728 			continue;
2729 
2730 		MALLOC(caminf, struct aac_cam_inf *,
2731 		    sizeof(struct aac_cam_inf), M_AACBUF, M_NOWAIT | M_ZERO);
2732 		if (caminf == NULL)
2733 			continue;
2734 
2735 		child = device_add_child(sc->aac_dev, "aacp", -1);
2736 		if (child == NULL) {
2737 			device_printf(sc->aac_dev, "device_add_child failed\n");
2738 			continue;
2739 		}
2740 
2741 		caminf->TargetsPerBus = businfo.TargetsPerBus;
2742 		caminf->BusNumber = i;
2743 		caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2744 		caminf->aac_sc = sc;
2745 
2746 		device_set_ivars(child, caminf);
2747 		device_set_desc(child, "SCSI Passthrough Bus");
2748 
2749 		found = 1;
2750 	}
2751 
2752 	if (found)
2753 		bus_generic_attach(sc->aac_dev);
2754 
2755 	return;
2756 }
2757