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