xref: /dragonfly/sys/dev/raid/aac/aac.c (revision a68e0df0)
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: src/sys/dev/aac/aac.c,v 1.9.2.14 2003/04/08 13:22:08 scottl Exp $
30  *	$DragonFly: src/sys/dev/raid/aac/aac.c,v 1.34 2008/01/20 03:40:35 pavalos Exp $
31  */
32 
33 /*
34  * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
35  */
36 #define AAC_DRIVER_VERSION		0x02000000
37 #define AAC_DRIVERNAME			"aac"
38 
39 #include "opt_aac.h"
40 
41 /* #include <stddef.h> */
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/sysctl.h>
48 #include <sys/poll.h>
49 
50 #include <sys/bus.h>
51 #include <sys/conf.h>
52 #include <sys/devicestat.h>
53 #include <sys/disk.h>
54 #include <sys/signalvar.h>
55 #include <sys/time.h>
56 #include <sys/eventhandler.h>
57 #include <sys/rman.h>
58 
59 #include <sys/mplock2.h>
60 
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
63 
64 #include "aacreg.h"
65 #include "aac_ioctl.h"
66 #include "aacvar.h"
67 #include "aac_tables.h"
68 
69 static void	aac_startup(void *arg);
70 static void	aac_add_container(struct aac_softc *sc,
71 				  struct aac_mntinforesp *mir, int f);
72 static void	aac_get_bus_info(struct aac_softc *sc);
73 static int	aac_shutdown(device_t dev);
74 
75 /* Command Processing */
76 static void	aac_timeout(void *ssc);
77 static int	aac_map_command(struct aac_command *cm);
78 static void	aac_complete(void *context, int pending);
79 static int	aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
80 static void	aac_bio_complete(struct aac_command *cm);
81 static int	aac_wait_command(struct aac_command *cm);
82 static void	aac_command_thread(struct aac_softc *sc);
83 
84 /* Command Buffer Management */
85 static void	aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
86 				   int nseg, int error);
87 static void	aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
88 				       int nseg, int error);
89 static int	aac_alloc_commands(struct aac_softc *sc);
90 static void	aac_free_commands(struct aac_softc *sc);
91 static void	aac_unmap_command(struct aac_command *cm);
92 
93 /* Hardware Interface */
94 static void	aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
95 			       int error);
96 static int	aac_check_firmware(struct aac_softc *sc);
97 static int	aac_init(struct aac_softc *sc);
98 static int	aac_sync_command(struct aac_softc *sc, u_int32_t command,
99 				 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
100 				 u_int32_t arg3, u_int32_t *sp);
101 static int	aac_enqueue_fib(struct aac_softc *sc, int queue,
102 				struct aac_command *cm);
103 static int	aac_dequeue_fib(struct aac_softc *sc, int queue,
104 				u_int32_t *fib_size, struct aac_fib **fib_addr);
105 static int	aac_enqueue_response(struct aac_softc *sc, int queue,
106 				     struct aac_fib *fib);
107 
108 /* Falcon/PPC interface */
109 static int	aac_fa_get_fwstatus(struct aac_softc *sc);
110 static void	aac_fa_qnotify(struct aac_softc *sc, int qbit);
111 static int	aac_fa_get_istatus(struct aac_softc *sc);
112 static void	aac_fa_clear_istatus(struct aac_softc *sc, int mask);
113 static void	aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
114 				   u_int32_t arg0, u_int32_t arg1,
115 				   u_int32_t arg2, u_int32_t arg3);
116 static int	aac_fa_get_mailbox(struct aac_softc *sc, int mb);
117 static void	aac_fa_set_interrupts(struct aac_softc *sc, int enable);
118 
119 struct aac_interface aac_fa_interface = {
120 	aac_fa_get_fwstatus,
121 	aac_fa_qnotify,
122 	aac_fa_get_istatus,
123 	aac_fa_clear_istatus,
124 	aac_fa_set_mailbox,
125 	aac_fa_get_mailbox,
126 	aac_fa_set_interrupts,
127 	NULL, NULL, NULL
128 };
129 
130 /* StrongARM interface */
131 static int	aac_sa_get_fwstatus(struct aac_softc *sc);
132 static void	aac_sa_qnotify(struct aac_softc *sc, int qbit);
133 static int	aac_sa_get_istatus(struct aac_softc *sc);
134 static void	aac_sa_clear_istatus(struct aac_softc *sc, int mask);
135 static void	aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
136 				   u_int32_t arg0, u_int32_t arg1,
137 				   u_int32_t arg2, u_int32_t arg3);
138 static int	aac_sa_get_mailbox(struct aac_softc *sc, int mb);
139 static void	aac_sa_set_interrupts(struct aac_softc *sc, int enable);
140 
141 struct aac_interface aac_sa_interface = {
142 	aac_sa_get_fwstatus,
143 	aac_sa_qnotify,
144 	aac_sa_get_istatus,
145 	aac_sa_clear_istatus,
146 	aac_sa_set_mailbox,
147 	aac_sa_get_mailbox,
148 	aac_sa_set_interrupts,
149 	NULL, NULL, NULL
150 };
151 
152 /* i960Rx interface */
153 static int	aac_rx_get_fwstatus(struct aac_softc *sc);
154 static void	aac_rx_qnotify(struct aac_softc *sc, int qbit);
155 static int	aac_rx_get_istatus(struct aac_softc *sc);
156 static void	aac_rx_clear_istatus(struct aac_softc *sc, int mask);
157 static void	aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
158 				   u_int32_t arg0, u_int32_t arg1,
159 				   u_int32_t arg2, u_int32_t arg3);
160 static int	aac_rx_get_mailbox(struct aac_softc *sc, int mb);
161 static void	aac_rx_set_interrupts(struct aac_softc *sc, int enable);
162 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
163 static int aac_rx_get_outb_queue(struct aac_softc *sc);
164 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
165 
166 struct aac_interface aac_rx_interface = {
167 	aac_rx_get_fwstatus,
168 	aac_rx_qnotify,
169 	aac_rx_get_istatus,
170 	aac_rx_clear_istatus,
171 	aac_rx_set_mailbox,
172 	aac_rx_get_mailbox,
173 	aac_rx_set_interrupts,
174 	aac_rx_send_command,
175 	aac_rx_get_outb_queue,
176 	aac_rx_set_outb_queue
177 };
178 
179 /* Rocket/MIPS interface */
180 static int	aac_rkt_get_fwstatus(struct aac_softc *sc);
181 static void	aac_rkt_qnotify(struct aac_softc *sc, int qbit);
182 static int	aac_rkt_get_istatus(struct aac_softc *sc);
183 static void	aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
184 static void	aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
185 				    u_int32_t arg0, u_int32_t arg1,
186 				    u_int32_t arg2, u_int32_t arg3);
187 static int	aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
188 static void	aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
189 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
190 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
191 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
192 
193 struct aac_interface aac_rkt_interface = {
194 	aac_rkt_get_fwstatus,
195 	aac_rkt_qnotify,
196 	aac_rkt_get_istatus,
197 	aac_rkt_clear_istatus,
198 	aac_rkt_set_mailbox,
199 	aac_rkt_get_mailbox,
200 	aac_rkt_set_interrupts,
201 	aac_rkt_send_command,
202 	aac_rkt_get_outb_queue,
203 	aac_rkt_set_outb_queue
204 };
205 
206 /* Debugging and Diagnostics */
207 static void	aac_describe_controller(struct aac_softc *sc);
208 static char	*aac_describe_code(struct aac_code_lookup *table,
209 				   u_int32_t code);
210 
211 /* Management Interface */
212 static d_open_t		aac_open;
213 static d_close_t	aac_close;
214 static d_ioctl_t	aac_ioctl;
215 static d_poll_t		aac_poll;
216 static int		aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) __unused;
217 static void		aac_handle_aif(struct aac_softc *sc,
218 					   struct aac_fib *fib);
219 static int		aac_rev_check(struct aac_softc *sc, caddr_t udata);
220 static int		aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
221 static int		aac_return_aif(struct aac_softc *sc, caddr_t uptr);
222 static int		aac_query_disk(struct aac_softc *sc, caddr_t uptr);
223 static int		aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
224 static void		aac_ioctl_event(struct aac_softc *sc,
225 				        struct aac_event *event, void *arg);
226 
227 #define AAC_CDEV_MAJOR	150
228 
229 static struct dev_ops aac_ops = {
230 	{ "aac", AAC_CDEV_MAJOR, 0 },
231 	.d_open =	aac_open,
232 	.d_close =	aac_close,
233 	.d_ioctl =	aac_ioctl,
234 	.d_poll =	aac_poll,
235 };
236 
237 DECLARE_DUMMY_MODULE(aac);
238 
239 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
240 
241 /* sysctl node */
242 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
243 
244 /*
245  * Device Interface
246  */
247 
248 /*
249  * Initialise the controller and softc
250  */
251 int
252 aac_attach(struct aac_softc *sc)
253 {
254 	int error, unit;
255 
256 	debug_called(1);
257 	callout_init(&sc->aac_watchdog);
258 
259 	/*
260 	 * Initialise per-controller queues.
261 	 */
262 	aac_initq_free(sc);
263 	aac_initq_ready(sc);
264 	aac_initq_busy(sc);
265 	aac_initq_complete(sc);
266 	aac_initq_bio(sc);
267 
268 	/*
269 	 * Initialise command-completion task.
270 	 */
271 	TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
272 
273 	/* mark controller as suspended until we get ourselves organised */
274 	sc->aac_state |= AAC_STATE_SUSPEND;
275 
276 	/*
277 	 * Check that the firmware on the card is supported.
278 	 */
279 	if ((error = aac_check_firmware(sc)) != 0)
280 		return(error);
281 
282 	/*
283 	 * Initialize locks
284 	 */
285 	AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
286 	AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock");
287 	AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
288 	TAILQ_INIT(&sc->aac_container_tqh);
289 	TAILQ_INIT(&sc->aac_ev_cmfree);
290 
291 
292 	/* Initialize the local AIF queue pointers */
293 	sc->aac_aifq_head = sc->aac_aifq_tail = AAC_AIFQ_LENGTH;
294 
295 	/*
296 	 * Initialise the adapter.
297 	 */
298 	if ((error = aac_init(sc)) != 0)
299 		return(error);
300 
301 	/*
302 	 * Allocate and connect our interrupt.
303 	 */
304 	sc->aac_irq_rid = 0;
305 	if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
306 			   			  &sc->aac_irq_rid,
307 			   			  RF_SHAREABLE |
308 						  RF_ACTIVE)) == NULL) {
309 		device_printf(sc->aac_dev, "can't allocate interrupt\n");
310 		return (EINVAL);
311 	}
312 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
313 		if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
314 				   0, aac_new_intr,
315 				   sc, &sc->aac_intr, NULL)) {
316 			device_printf(sc->aac_dev, "can't set up interrupt\n");
317 			return (EINVAL);
318 		}
319 	} else {
320 		if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
321 				   INTR_FAST, aac_fast_intr,
322 				   sc, &sc->aac_intr, NULL)) {
323 			device_printf(sc->aac_dev,
324 				      "can't set up FAST interrupt\n");
325 			if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
326 					   0, aac_fast_intr,
327 					   sc, &sc->aac_intr, NULL)) {
328 				device_printf(sc->aac_dev,
329 					     "can't set up MPSAFE interrupt\n");
330 				return (EINVAL);
331 			}
332 		}
333 	}
334 
335 	/*
336 	 * Print a little information about the controller.
337 	 */
338 	aac_describe_controller(sc);
339 
340 	/*
341 	 * Register to probe our containers later.
342 	 */
343 	sc->aac_ich.ich_func = aac_startup;
344 	sc->aac_ich.ich_arg = sc;
345 	sc->aac_ich.ich_desc = "aac";
346 	if (config_intrhook_establish(&sc->aac_ich) != 0) {
347 		device_printf(sc->aac_dev,
348 			      "can't establish configuration hook\n");
349 		return(ENXIO);
350 	}
351 
352 	/*
353 	 * Make the control device.
354 	 */
355 	unit = device_get_unit(sc->aac_dev);
356 	sc->aac_dev_t = make_dev(&aac_ops, unit, UID_ROOT, GID_OPERATOR,
357 				 0640, "aac%d", unit);
358 	sc->aac_dev_t->si_drv1 = sc;
359 	reference_dev(sc->aac_dev_t);
360 
361 	/* Create the AIF thread */
362 	if (kthread_create((void(*)(void *))aac_command_thread, sc,
363 			   &sc->aifthread, "aac%daif", unit))
364 		panic("Could not create AIF thread\n");
365 
366 	/* Register the shutdown method to only be called post-dump */
367 	if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, aac_shutdown,
368 	    sc->aac_dev, SHUTDOWN_PRI_DRIVER)) == NULL)
369 		device_printf(sc->aac_dev,
370 			      "shutdown event registration failed\n");
371 
372 	/* Register with CAM for the non-DASD devices */
373 	if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
374 		TAILQ_INIT(&sc->aac_sim_tqh);
375 		aac_get_bus_info(sc);
376 	}
377 
378 	return(0);
379 }
380 
381 void
382 aac_add_event(struct aac_softc *sc, struct aac_event *event)
383 {
384 
385 	switch (event->ev_type & AAC_EVENT_MASK) {
386 	case AAC_EVENT_CMFREE:
387 		TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
388 		break;
389 	default:
390 		device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
391 		    event->ev_type);
392 		break;
393 	}
394 
395 	return;
396 }
397 
398 /*
399  * Probe for containers, create disks.
400  */
401 static void
402 aac_startup(void *arg)
403 {
404 	struct aac_softc *sc;
405 	struct aac_fib *fib;
406 	struct aac_mntinfo *mi;
407 	struct aac_mntinforesp *mir = NULL;
408 	int count = 0, i = 0;
409 
410 	debug_called(1);
411 
412 	sc = (struct aac_softc *)arg;
413 
414 	/* disconnect ourselves from the intrhook chain */
415 	config_intrhook_disestablish(&sc->aac_ich);
416 
417 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
418 	aac_alloc_sync_fib(sc, &fib);
419 	mi = (struct aac_mntinfo *)&fib->data[0];
420 
421 	/* loop over possible containers */
422 	do {
423 		/* request information on this container */
424 		bzero(mi, sizeof(struct aac_mntinfo));
425 		mi->Command = VM_NameServe;
426 		mi->MntType = FT_FILESYS;
427 		mi->MntCount = i;
428 		if (aac_sync_fib(sc, ContainerCommand, 0, fib,
429 				 sizeof(struct aac_mntinfo))) {
430 			device_printf(sc->aac_dev,
431 			    "error probing container %d", i);
432 
433 			continue;
434 		}
435 
436 		mir = (struct aac_mntinforesp *)&fib->data[0];
437 		/* XXX Need to check if count changed */
438 		count = mir->MntRespCount;
439 		aac_add_container(sc, mir, 0);
440 		i++;
441 	} while ((i < count) && (i < AAC_MAX_CONTAINERS));
442 
443 	aac_release_sync_fib(sc);
444 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
445 
446 	/* poke the bus to actually attach the child devices */
447 	if (bus_generic_attach(sc->aac_dev))
448 		device_printf(sc->aac_dev, "bus_generic_attach failed\n");
449 
450 	/* mark the controller up */
451 	sc->aac_state &= ~AAC_STATE_SUSPEND;
452 
453 	/* enable interrupts now */
454 	AAC_UNMASK_INTERRUPTS(sc);
455 }
456 
457 /*
458  * Create a device to respresent a new container
459  */
460 static void
461 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
462 {
463 	struct aac_container *co;
464 	device_t child;
465 
466 	/*
467 	 * Check container volume type for validity.  Note that many of
468 	 * the possible types may never show up.
469 	 */
470 	if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
471 		co = (struct aac_container *)kmalloc(sizeof *co, M_AACBUF,
472 		       M_INTWAIT | M_ZERO);
473 		debug(1, "id %x  name '%.16s'  size %u  type %d",
474 		      mir->MntTable[0].ObjectId,
475 		      mir->MntTable[0].FileSystemName,
476 		      mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
477 
478 		if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
479 			device_printf(sc->aac_dev, "device_add_child failed\n");
480 		else
481 			device_set_ivars(child, co);
482 		device_set_desc(child, aac_describe_code(aac_container_types,
483 				mir->MntTable[0].VolType));
484 		co->co_disk = child;
485 		co->co_found = f;
486 		bcopy(&mir->MntTable[0], &co->co_mntobj,
487 		      sizeof(struct aac_mntobj));
488 		AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
489 		TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
490 		AAC_LOCK_RELEASE(&sc->aac_container_lock);
491 	}
492 }
493 
494 /*
495  * Free all of the resources associated with (sc)
496  *
497  * Should not be called if the controller is active.
498  */
499 void
500 aac_free(struct aac_softc *sc)
501 {
502 
503 	debug_called(1);
504 
505 	/* remove the control device */
506 	if (sc->aac_dev_t != NULL)
507 		destroy_dev(sc->aac_dev_t);
508 
509 	/* throw away any FIB buffers, discard the FIB DMA tag */
510 	aac_free_commands(sc);
511 	if (sc->aac_fib_dmat)
512 		bus_dma_tag_destroy(sc->aac_fib_dmat);
513 
514 	kfree(sc->aac_commands, M_AACBUF);
515 
516 	/* destroy the common area */
517 	if (sc->aac_common) {
518 		bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
519 		bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
520 				sc->aac_common_dmamap);
521 	}
522 	if (sc->aac_common_dmat)
523 		bus_dma_tag_destroy(sc->aac_common_dmat);
524 
525 	/* disconnect the interrupt handler */
526 	if (sc->aac_intr)
527 		bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
528 	if (sc->aac_irq != NULL)
529 		bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
530 				     sc->aac_irq);
531 
532 	/* destroy data-transfer DMA tag */
533 	if (sc->aac_buffer_dmat)
534 		bus_dma_tag_destroy(sc->aac_buffer_dmat);
535 
536 	/* destroy the parent DMA tag */
537 	if (sc->aac_parent_dmat)
538 		bus_dma_tag_destroy(sc->aac_parent_dmat);
539 
540 	/* release the register window mapping */
541 	if (sc->aac_regs_resource != NULL) {
542 		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
543 				     sc->aac_regs_rid, sc->aac_regs_resource);
544 	}
545 	dev_ops_remove_minor(&aac_ops, device_get_unit(sc->aac_dev));
546 }
547 
548 /*
549  * Disconnect from the controller completely, in preparation for unload.
550  */
551 int
552 aac_detach(device_t dev)
553 {
554 	struct aac_softc *sc;
555 	struct aac_container *co;
556 	struct aac_sim	*sim;
557 	int error;
558 
559 	debug_called(1);
560 
561 	sc = device_get_softc(dev);
562 
563 	callout_stop(&sc->aac_watchdog);
564 
565 	if (sc->aac_state & AAC_STATE_OPEN)
566 		return(EBUSY);
567 
568 	/* Remove the child containers */
569 	while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
570 		error = device_delete_child(dev, co->co_disk);
571 		if (error)
572 			return (error);
573 		TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
574 		kfree(co, M_AACBUF);
575 	}
576 
577 	/* Remove the CAM SIMs */
578 	while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
579 		TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
580 		error = device_delete_child(dev, sim->sim_dev);
581 		if (error)
582 			return (error);
583 		kfree(sim, M_AACBUF);
584 	}
585 
586 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
587 		sc->aifflags |= AAC_AIFFLAGS_EXIT;
588 		wakeup(sc->aifthread);
589 		tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz);
590 	}
591 
592 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
593 		panic("Cannot shutdown AIF thread\n");
594 
595 	if ((error = aac_shutdown(dev)))
596 		return(error);
597 
598 	EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->eh);
599 
600 	aac_free(sc);
601 
602 	lockuninit(&sc->aac_aifq_lock);
603 	lockuninit(&sc->aac_io_lock);
604 	lockuninit(&sc->aac_container_lock);
605 
606 	return(0);
607 }
608 
609 /*
610  * Bring the controller down to a dormant state and detach all child devices.
611  *
612  * This function is called before detach or system shutdown.
613  *
614  * Note that we can assume that the bioq on the controller is empty, as we won't
615  * allow shutdown if any device is open.
616  */
617 static int
618 aac_shutdown(device_t dev)
619 {
620 	struct aac_softc *sc;
621 	struct aac_fib *fib;
622 	struct aac_close_command *cc;
623 
624 	debug_called(1);
625 
626 	sc = device_get_softc(dev);
627 
628 	sc->aac_state |= AAC_STATE_SUSPEND;
629 
630 	/*
631 	 * Send a Container shutdown followed by a HostShutdown FIB to the
632 	 * controller to convince it that we don't want to talk to it anymore.
633 	 * We've been closed and all I/O completed already
634 	 */
635 	device_printf(sc->aac_dev, "shutting down controller...");
636 
637 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
638 	aac_alloc_sync_fib(sc, &fib);
639 	cc = (struct aac_close_command *)&fib->data[0];
640 
641 	bzero(cc, sizeof(struct aac_close_command));
642 	cc->Command = VM_CloseAll;
643 	cc->ContainerId = 0xffffffff;
644 	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
645 	    sizeof(struct aac_close_command)))
646 		kprintf("FAILED.\n");
647 	else
648 		kprintf("done\n");
649 #if 0
650 	else {
651 		fib->data[0] = 0;
652 		/*
653 		 * XXX Issuing this command to the controller makes it shut down
654 		 * but also keeps it from coming back up without a reset of the
655 		 * PCI bus.  This is not desirable if you are just unloading the
656 		 * driver module with the intent to reload it later.
657 		 */
658 		if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
659 		    fib, 1)) {
660 			kprintf("FAILED.\n");
661 		} else {
662 			kprintf("done.\n");
663 		}
664 	}
665 #endif
666 
667 	AAC_MASK_INTERRUPTS(sc);
668 	aac_release_sync_fib(sc);
669 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
670 
671 	return(0);
672 }
673 
674 /*
675  * Bring the controller to a quiescent state, ready for system suspend.
676  */
677 int
678 aac_suspend(device_t dev)
679 {
680 	struct aac_softc *sc;
681 
682 	debug_called(1);
683 
684 	sc = device_get_softc(dev);
685 
686 	sc->aac_state |= AAC_STATE_SUSPEND;
687 
688 	AAC_MASK_INTERRUPTS(sc);
689 	return(0);
690 }
691 
692 /*
693  * Bring the controller back to a state ready for operation.
694  */
695 int
696 aac_resume(device_t dev)
697 {
698 	struct aac_softc *sc;
699 
700 	debug_called(1);
701 
702 	sc = device_get_softc(dev);
703 
704 	sc->aac_state &= ~AAC_STATE_SUSPEND;
705 	AAC_UNMASK_INTERRUPTS(sc);
706 	return(0);
707 }
708 
709 /*
710  * Interrupt handler for NEW_COMM interface.
711  */
712 void
713 aac_new_intr(void *arg)
714 {
715 	struct aac_softc *sc;
716 	u_int32_t index, fast;
717 	struct aac_command *cm;
718 	struct aac_fib *fib;
719 	int i;
720 
721 	debug_called(2);
722 
723 	sc = (struct aac_softc *)arg;
724 
725 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
726 	while (1) {
727 		index = AAC_GET_OUTB_QUEUE(sc);
728 		if (index == 0xffffffff)
729 			index = AAC_GET_OUTB_QUEUE(sc);
730 		if (index == 0xffffffff)
731 			break;
732 		if (index & 2) {
733 			if (index == 0xfffffffe) {
734 				/* XXX This means that the controller wants
735 				 * more work.  Ignore it for now.
736 				 */
737 				continue;
738 			}
739 			/* AIF */
740 			fib = (struct aac_fib *)kmalloc(sizeof *fib, M_AACBUF,
741 				   M_INTWAIT | M_ZERO);
742 			index &= ~2;
743 			for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
744 				((u_int32_t *)fib)[i] = AAC_GETREG4(sc, index + i*4);
745 			aac_handle_aif(sc, fib);
746 			kfree(fib, M_AACBUF);
747 
748 			/*
749 			 * AIF memory is owned by the adapter, so let it
750 			 * know that we are done with it.
751 			 */
752 			AAC_SET_OUTB_QUEUE(sc, index);
753 			AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
754 		} else {
755 			fast = index & 1;
756 			cm = sc->aac_commands + (index >> 2);
757 			fib = cm->cm_fib;
758 			if (fast) {
759 				fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
760 				*((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
761 			}
762 			aac_remove_busy(cm);
763  			aac_unmap_command(cm);
764 			cm->cm_flags |= AAC_CMD_COMPLETED;
765 
766 			/* is there a completion handler? */
767 			if (cm->cm_complete != NULL) {
768 				cm->cm_complete(cm);
769 			} else {
770 				/* assume that someone is sleeping on this
771 				 * command
772 				 */
773 				wakeup(cm);
774 			}
775 			sc->flags &= ~AAC_QUEUE_FRZN;
776 		}
777 	}
778 	/* see if we can start some more I/O */
779 	if ((sc->flags & AAC_QUEUE_FRZN) == 0)
780 		aac_startio(sc);
781 
782 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
783 }
784 
785 void
786 aac_fast_intr(void *arg)
787 {
788 	struct aac_softc *sc;
789 	u_int16_t reason;
790 
791 	debug_called(2);
792 
793 	sc = (struct aac_softc *)arg;
794 
795 	/*
796 	 * Read the status register directly.  This is faster than taking the
797 	 * driver lock and reading the queues directly.  It also saves having
798 	 * to turn parts of the driver lock into a spin mutex, which would be
799 	 * ugly.
800 	 */
801 	reason = AAC_GET_ISTATUS(sc);
802 	AAC_CLEAR_ISTATUS(sc, reason);
803 
804 	/* handle completion processing */
805 	if (reason & AAC_DB_RESPONSE_READY)
806 		taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
807 
808 	/* controller wants to talk to us */
809 	if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
810 		/*
811 		 * XXX Make sure that we don't get fooled by strange messages
812 		 * that start with a NULL.
813 		 */
814 		if ((reason & AAC_DB_PRINTF) &&
815 			(sc->aac_common->ac_printf[0] == 0))
816 			sc->aac_common->ac_printf[0] = 32;
817 
818 		/*
819 		 * This might miss doing the actual wakeup.  However, the
820 		 * ssleep that this is waking up has a timeout, so it will
821 		 * wake up eventually.  AIFs and printfs are low enough
822 		 * priority that they can handle hanging out for a few seconds
823 		 * if needed.
824 		 */
825 		wakeup(sc->aifthread);
826 	}
827 }
828 
829 /*
830  * Command Processing
831  */
832 
833 /*
834  * Start as much queued I/O as possible on the controller
835  */
836 void
837 aac_startio(struct aac_softc *sc)
838 {
839 	struct aac_command *cm;
840 
841 	debug_called(2);
842 
843 	if (sc->flags & AAC_QUEUE_FRZN)
844 		return;
845 
846 	for (;;) {
847 		/*
848 		 * Try to get a command that's been put off for lack of
849 		 * resources
850 		 */
851 		cm = aac_dequeue_ready(sc);
852 
853 		/*
854 		 * Try to build a command off the bio queue (ignore error
855 		 * return)
856 		 */
857 		if (cm == NULL)
858 			aac_bio_command(sc, &cm);
859 
860 		/* nothing to do? */
861 		if (cm == NULL)
862 			break;
863 
864 		/*
865 		 * Try to give the command to the controller.  Any error is
866 		 * catastrophic since it means that bus_dmamap_load() failed.
867 		 */
868 		if (aac_map_command(cm) != 0)
869 			panic("aac: error mapping command %p\n", cm);
870 	}
871 }
872 
873 /*
874  * Deliver a command to the controller; allocate controller resources at the
875  * last moment when possible.
876  */
877 static int
878 aac_map_command(struct aac_command *cm)
879 {
880 	struct aac_softc *sc;
881 	int error;
882 
883 	debug_called(2);
884 
885 	sc = cm->cm_sc;
886 	error = 0;
887 
888 	/* don't map more than once */
889 	if (cm->cm_flags & AAC_CMD_MAPPED)
890 		panic("aac: command %p already mapped", cm);
891 
892 	if (cm->cm_datalen != 0) {
893 		error = bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
894 					cm->cm_data, cm->cm_datalen,
895 					aac_map_command_sg, cm, 0);
896 		if (error == EINPROGRESS) {
897 			debug(1, "freezing queue\n");
898 			sc->flags |= AAC_QUEUE_FRZN;
899 			error = 0;
900 		}
901 	} else {
902 		aac_map_command_sg(cm, NULL, 0, 0);
903 	}
904 	return (error);
905 }
906 
907 /*
908  * Handle notification of one or more FIBs coming from the controller.
909  */
910 static void
911 aac_command_thread(struct aac_softc *sc)
912 {
913 	struct aac_fib *fib;
914 	u_int32_t fib_size;
915 	int size, retval;
916 
917 	debug_called(2);
918 
919 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
920 	sc->aifflags = AAC_AIFFLAGS_RUNNING;
921 
922 	while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
923 		retval = 0;
924 		if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) {
925 			tsleep_interlock(sc->aifthread, 0);
926 			AAC_LOCK_RELEASE(&sc->aac_io_lock);
927 			retval = tsleep(sc->aifthread, PINTERLOCKED,
928 					"aifthd", AAC_PERIODIC_INTERVAL * hz);
929 			AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
930 		}
931 		/*
932 		 * First see if any FIBs need to be allocated.  This needs
933 		 * to be called without the driver lock because contigmalloc
934 		 * will grab Giant, and would result in an LOR.
935 		 */
936 		if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
937 			AAC_LOCK_RELEASE(&sc->aac_io_lock);
938 			aac_alloc_commands(sc);
939 			AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
940 			sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
941 			aac_startio(sc);
942 		}
943 
944 		/*
945 		 * While we're here, check to see if any commands are stuck.
946 		 * This is pretty low-priority, so it's ok if it doesn't
947 		 * always fire.
948 		 */
949 		if (retval == EWOULDBLOCK)
950 			aac_timeout(sc);
951 
952 		/* Check the hardware printf message buffer */
953 		if (sc->aac_common->ac_printf[0] != 0)
954 			aac_print_printf(sc);
955 
956 		/* Also check to see if the adapter has a command for us. */
957 		if (sc->flags & AAC_FLAGS_NEW_COMM)
958 			continue;
959 		for (;;) {
960 			if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
961 					    &fib_size, &fib))
962 				break;
963 
964 			AAC_PRINT_FIB(sc, fib);
965 
966 			switch (fib->Header.Command) {
967 			case AifRequest:
968 				aac_handle_aif(sc, fib);
969 				break;
970 			default:
971 				device_printf(sc->aac_dev, "unknown command "
972 					      "from controller\n");
973 				break;
974 			}
975 
976 			if ((fib->Header.XferState == 0) ||
977 			    (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
978 				break;
979 			}
980 
981 			/* Return the AIF to the controller. */
982 			if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
983 				fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
984 				*(AAC_FSAStatus*)fib->data = ST_OK;
985 
986 				/* XXX Compute the Size field? */
987 				size = fib->Header.Size;
988 				if (size > sizeof(struct aac_fib)) {
989 					size = sizeof(struct aac_fib);
990 					fib->Header.Size = size;
991 				}
992 				/*
993 				 * Since we did not generate this command, it
994 				 * cannot go through the normal
995 				 * enqueue->startio chain.
996 				 */
997 				aac_enqueue_response(sc,
998 						 AAC_ADAP_NORM_RESP_QUEUE,
999 						 fib);
1000 			}
1001 		}
1002 	}
1003 	sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1004 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
1005 	wakeup(sc->aac_dev);
1006 
1007 	kthread_exit();
1008 }
1009 
1010 /*
1011  * Process completed commands.
1012  */
1013 static void
1014 aac_complete(void *context, int pending)
1015 {
1016 	struct aac_softc *sc;
1017 	struct aac_command *cm;
1018 	struct aac_fib *fib;
1019 	u_int32_t fib_size;
1020 
1021 	debug_called(2);
1022 
1023 	sc = (struct aac_softc *)context;
1024 
1025 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1026 
1027 	/* pull completed commands off the queue */
1028 	for (;;) {
1029 		/* look for completed FIBs on our queue */
1030 		if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1031 							&fib))
1032 			break;	/* nothing to do */
1033 
1034 		/* get the command, unmap and queue for later processing */
1035 		cm = sc->aac_commands + fib->Header.SenderData;
1036 		if (cm == NULL) {
1037 			AAC_PRINT_FIB(sc, fib);
1038 			break;
1039 		}
1040 		aac_remove_busy(cm);
1041 		aac_unmap_command(cm);		/* XXX defer? */
1042 		cm->cm_flags |= AAC_CMD_COMPLETED;
1043 
1044 		/* is there a completion handler? */
1045 		if (cm->cm_complete != NULL) {
1046 			cm->cm_complete(cm);
1047 		} else {
1048 			/* assume that someone is sleeping on this command */
1049 			wakeup(cm);
1050 		}
1051 	}
1052 
1053 	/* see if we can start some more I/O */
1054 	sc->flags &= ~AAC_QUEUE_FRZN;
1055 	aac_startio(sc);
1056 
1057 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
1058 }
1059 
1060 /*
1061  * Handle a bio submitted from a disk device.
1062  */
1063 void
1064 aac_submit_bio(struct aac_disk *ad, struct bio *bio)
1065 {
1066 	struct aac_softc *sc;
1067 
1068 	debug_called(2);
1069 
1070 	bio->bio_driver_info = ad;
1071 	sc = ad->ad_controller;
1072 
1073 	/* queue the BIO and try to get some work done */
1074 	aac_enqueue_bio(sc, bio);
1075 	aac_startio(sc);
1076 }
1077 
1078 /*
1079  * Get a bio and build a command to go with it.
1080  */
1081 static int
1082 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1083 {
1084 	struct aac_command *cm;
1085 	struct aac_fib *fib;
1086 	struct aac_disk *ad;
1087 	struct bio *bio;
1088 	struct buf *bp;
1089 
1090 	debug_called(2);
1091 
1092 	/* get the resources we will need */
1093 	cm = NULL;
1094 	bio = NULL;
1095 	if (aac_alloc_command(sc, &cm))	/* get a command */
1096 		goto fail;
1097 	if ((bio = aac_dequeue_bio(sc)) == NULL)
1098 		goto fail;
1099 
1100 	/* fill out the command */
1101 	bp = bio->bio_buf;
1102 	cm->cm_data = (void *)bp->b_data;
1103 	cm->cm_datalen = bp->b_bcount;
1104 	cm->cm_complete = aac_bio_complete;
1105 	cm->cm_private = bio;
1106 	cm->cm_timestamp = time_second;
1107 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1108 
1109 	/* build the FIB */
1110 	fib = cm->cm_fib;
1111 	fib->Header.Size = sizeof(struct aac_fib_header);
1112 	fib->Header.XferState =
1113 		AAC_FIBSTATE_HOSTOWNED   |
1114 		AAC_FIBSTATE_INITIALISED |
1115 		AAC_FIBSTATE_EMPTY	 |
1116 		AAC_FIBSTATE_FROMHOST	 |
1117 		AAC_FIBSTATE_REXPECTED   |
1118 		AAC_FIBSTATE_NORM	 |
1119 		AAC_FIBSTATE_ASYNC	 |
1120 		AAC_FIBSTATE_FAST_RESPONSE;
1121 
1122 	/* build the read/write request */
1123 	ad = (struct aac_disk *)bio->bio_driver_info;
1124 
1125 	if (sc->flags & AAC_FLAGS_RAW_IO) {
1126 		struct aac_raw_io *raw;
1127 		raw = (struct aac_raw_io *)&fib->data[0];
1128 		fib->Header.Command = RawIo;
1129 		raw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1130 		raw->ByteCount = bp->b_bcount;
1131 		raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1132 		raw->BpTotal = 0;
1133 		raw->BpComplete = 0;
1134 		fib->Header.Size += sizeof(struct aac_raw_io);
1135 		cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1136 		if (bp->b_cmd == BUF_CMD_READ) {
1137 			raw->Flags = 1;
1138 			cm->cm_flags |= AAC_CMD_DATAIN;
1139 		} else {
1140 			raw->Flags = 0;
1141 			cm->cm_flags |= AAC_CMD_DATAOUT;
1142 		}
1143 	} else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1144 		fib->Header.Command = ContainerCommand;
1145 		if (bp->b_cmd == BUF_CMD_READ) {
1146 			struct aac_blockread *br;
1147 			br = (struct aac_blockread *)&fib->data[0];
1148 			br->Command = VM_CtBlockRead;
1149 			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1150 			br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1151 			br->ByteCount = bp->b_bcount;
1152 			fib->Header.Size += sizeof(struct aac_blockread);
1153 			cm->cm_sgtable = &br->SgMap;
1154 			cm->cm_flags |= AAC_CMD_DATAIN;
1155 		} else {
1156 			struct aac_blockwrite *bw;
1157 			bw = (struct aac_blockwrite *)&fib->data[0];
1158 			bw->Command = VM_CtBlockWrite;
1159 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1160 			bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1161 			bw->ByteCount = bp->b_bcount;
1162 			bw->Stable = CUNSTABLE;
1163 			fib->Header.Size += sizeof(struct aac_blockwrite);
1164 			cm->cm_flags |= AAC_CMD_DATAOUT;
1165 			cm->cm_sgtable = &bw->SgMap;
1166 		}
1167 	} else {
1168 		fib->Header.Command = ContainerCommand64;
1169 		if (bp->b_cmd == BUF_CMD_READ) {
1170 			struct aac_blockread64 *br;
1171 			br = (struct aac_blockread64 *)&fib->data[0];
1172 			br->Command = VM_CtHostRead64;
1173 			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1174 			br->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1175 			br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1176 			br->Pad = 0;
1177 			br->Flags = 0;
1178 			fib->Header.Size += sizeof(struct aac_blockread64);
1179 			cm->cm_flags |= AAC_CMD_DATAOUT;
1180 			cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1181 		} else {
1182 			struct aac_blockwrite64 *bw;
1183 			bw = (struct aac_blockwrite64 *)&fib->data[0];
1184 			bw->Command = VM_CtHostWrite64;
1185 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1186 			bw->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1187 			bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1188 			bw->Pad = 0;
1189 			bw->Flags = 0;
1190 			fib->Header.Size += sizeof(struct aac_blockwrite64);
1191 			cm->cm_flags |= AAC_CMD_DATAIN;
1192 			cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1193 		}
1194 	}
1195 
1196 	*cmp = cm;
1197 	return(0);
1198 
1199 fail:
1200 	if (bio != NULL)
1201 		aac_enqueue_bio(sc, bio);
1202 	if (cm != NULL)
1203 		aac_release_command(cm);
1204 	return(ENOMEM);
1205 }
1206 
1207 /*
1208  * Handle a bio-instigated command that has been completed.
1209  */
1210 static void
1211 aac_bio_complete(struct aac_command *cm)
1212 {
1213 	struct aac_blockread_response *brr;
1214 	struct aac_blockwrite_response *bwr;
1215 	struct bio *bio;
1216 	struct buf *bp;
1217 	const char *code;
1218 	AAC_FSAStatus status;
1219 
1220 	/* fetch relevant status and then release the command */
1221 	bio = (struct bio *)cm->cm_private;
1222 	bp = bio->bio_buf;
1223 	if (bp->b_cmd == BUF_CMD_READ) {
1224 		brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1225 		status = brr->Status;
1226 	} else {
1227 		bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1228 		status = bwr->Status;
1229 	}
1230 	aac_release_command(cm);
1231 
1232 	/* fix up the bio based on status */
1233 	if (status == ST_OK) {
1234 		bp->b_resid = 0;
1235 		code = 0;
1236 	} else {
1237 		bp->b_error = EIO;
1238 		bp->b_flags |= B_ERROR;
1239 		/* pass an error string out to the disk layer */
1240 		code = aac_describe_code(aac_command_status_table, status);
1241 	}
1242 	aac_biodone(bio, code);
1243 }
1244 
1245 /*
1246  * Dump a block of data to the controller.  If the queue is full, tell the
1247  * caller to hold off and wait for the queue to drain.
1248  */
1249 int
1250 aac_dump_enqueue(struct aac_disk *ad, u_int64_t lba, void *data, int dumppages)
1251 {
1252 	struct aac_softc *sc;
1253 	struct aac_command *cm;
1254 	struct aac_fib *fib;
1255 	struct aac_blockwrite *bw;
1256 
1257 	sc = ad->ad_controller;
1258 	cm = NULL;
1259 
1260 	KKASSERT(lba <= 0x100000000ULL);
1261 
1262 	if (aac_alloc_command(sc, &cm))
1263 		return (EBUSY);
1264 
1265 	/* fill out the command */
1266 	cm->cm_data = data;
1267 	cm->cm_datalen = dumppages * PAGE_SIZE;
1268 	cm->cm_complete = NULL;
1269 	cm->cm_private = NULL;
1270 	cm->cm_timestamp = time_second;
1271 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1272 
1273 	/* build the FIB */
1274 	fib = cm->cm_fib;
1275 	fib->Header.XferState =
1276 	AAC_FIBSTATE_HOSTOWNED   |
1277 	AAC_FIBSTATE_INITIALISED |
1278 	AAC_FIBSTATE_FROMHOST	 |
1279 	AAC_FIBSTATE_REXPECTED   |
1280 	AAC_FIBSTATE_NORM;
1281 	fib->Header.Command = ContainerCommand;
1282 	fib->Header.Size = sizeof(struct aac_fib_header);
1283 
1284 	bw = (struct aac_blockwrite *)&fib->data[0];
1285 	bw->Command = VM_CtBlockWrite;
1286 	bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1287 	bw->BlockNumber = lba;
1288 	bw->ByteCount = dumppages * PAGE_SIZE;
1289 	bw->Stable = CUNSTABLE;		/* XXX what's appropriate here? */
1290 	fib->Header.Size += sizeof(struct aac_blockwrite);
1291 	cm->cm_flags |= AAC_CMD_DATAOUT;
1292 	cm->cm_sgtable = &bw->SgMap;
1293 
1294 	return (aac_map_command(cm));
1295 }
1296 
1297 /*
1298  * Wait for the card's queue to drain when dumping.  Also check for monitor
1299  * kprintf's
1300  */
1301 void
1302 aac_dump_complete(struct aac_softc *sc)
1303 {
1304 	struct aac_fib *fib;
1305 	struct aac_command *cm;
1306 	u_int16_t reason;
1307 	u_int32_t pi, ci, fib_size;
1308 
1309 	do {
1310 		reason = AAC_GET_ISTATUS(sc);
1311 		if (reason & AAC_DB_RESPONSE_READY) {
1312 			AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
1313 			for (;;) {
1314 				if (aac_dequeue_fib(sc,
1315 						    AAC_HOST_NORM_RESP_QUEUE,
1316 						    &fib_size, &fib))
1317 					break;
1318 				cm = (struct aac_command *)
1319 					fib->Header.SenderData;
1320 				if (cm == NULL)
1321 					AAC_PRINT_FIB(sc, fib);
1322 				else {
1323 					aac_remove_busy(cm);
1324 					aac_unmap_command(cm);
1325 					aac_enqueue_complete(cm);
1326 					aac_release_command(cm);
1327 				}
1328 			}
1329 		}
1330 		if (reason & AAC_DB_PRINTF) {
1331 			AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
1332 			aac_print_printf(sc);
1333 		}
1334 		pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1335 			AAC_PRODUCER_INDEX];
1336 		ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1337 			AAC_CONSUMER_INDEX];
1338 	} while (ci != pi);
1339 
1340 	return;
1341 }
1342 
1343 /*
1344  * Submit a command to the controller, return when it completes.
1345  * XXX This is very dangerous!  If the card has gone out to lunch, we could
1346  *     be stuck here forever.  At the same time, signals are not caught
1347  *     because there is a risk that a signal could wakeup the sleep before
1348  *     the card has a chance to complete the command.  Since there is no way
1349  *     to cancel a command that is in progress, we can't protect against the
1350  *     card completing a command late and spamming the command and data
1351  *     memory.  So, we are held hostage until the command completes.
1352  */
1353 static int
1354 aac_wait_command(struct aac_command *cm)
1355 {
1356 	struct aac_softc *sc;
1357 	int error;
1358 
1359 	debug_called(2);
1360 
1361 	sc = cm->cm_sc;
1362 
1363 	/* Put the command on the ready queue and get things going */
1364 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1365 	aac_enqueue_ready(cm);
1366 	aac_startio(sc);
1367 	/* Lock is held */
1368 	KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
1369 	tsleep_interlock(cm, 0);
1370 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
1371 	error = tsleep(cm, PINTERLOCKED, "aacwait", 0);
1372 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1373 	return(error);
1374 }
1375 
1376 /*
1377  *Command Buffer Management
1378  */
1379 
1380 /*
1381  * Allocate a command.
1382  */
1383 int
1384 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1385 {
1386 	struct aac_command *cm;
1387 
1388 	debug_called(3);
1389 
1390 	if ((cm = aac_dequeue_free(sc)) == NULL) {
1391 		if (sc->total_fibs < sc->aac_max_fibs) {
1392 			sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1393 			wakeup(sc->aifthread);
1394 		}
1395 		return (EBUSY);
1396 	}
1397 
1398 	*cmp = cm;
1399 	return(0);
1400 }
1401 
1402 /*
1403  * Release a command back to the freelist.
1404  */
1405 void
1406 aac_release_command(struct aac_command *cm)
1407 {
1408 	struct aac_event *event;
1409 	struct aac_softc *sc;
1410 
1411 	debug_called(3);
1412 
1413 	/* (re)initialise the command/FIB */
1414 	cm->cm_sgtable = NULL;
1415 	cm->cm_flags = 0;
1416 	cm->cm_complete = NULL;
1417 	cm->cm_private = NULL;
1418 	cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1419 	cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1420 	cm->cm_fib->Header.Flags = 0;
1421 	cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1422 
1423 	/*
1424 	 * These are duplicated in aac_start to cover the case where an
1425 	 * intermediate stage may have destroyed them.  They're left
1426 	 * initialised here for debugging purposes only.
1427 	 */
1428 	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1429 	cm->cm_fib->Header.SenderData = 0;
1430 
1431 	aac_enqueue_free(cm);
1432 
1433 	sc = cm->cm_sc;
1434 	event = TAILQ_FIRST(&sc->aac_ev_cmfree);
1435 	if (event != NULL) {
1436 		TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1437 		event->ev_callback(sc, event, event->ev_arg);
1438 	}
1439 }
1440 
1441 /*
1442  * Map helper for command/FIB allocation.
1443  */
1444 static void
1445 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1446 {
1447 	uint64_t	*fibphys;
1448 
1449 	fibphys = (uint64_t *)arg;
1450 
1451 	debug_called(3);
1452 
1453 	*fibphys = segs[0].ds_addr;
1454 }
1455 
1456 /*
1457  * Allocate and initialise commands/FIBs for this adapter.
1458  */
1459 static int
1460 aac_alloc_commands(struct aac_softc *sc)
1461 {
1462 	struct aac_command *cm;
1463 	struct aac_fibmap *fm;
1464 	uint64_t fibphys;
1465 	int i, error;
1466 
1467 	debug_called(2);
1468 
1469 	if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1470 		return (ENOMEM);
1471 
1472 	fm = kmalloc(sizeof(struct aac_fibmap), M_AACBUF, M_INTWAIT | M_ZERO);
1473 
1474 	/* allocate the FIBs in DMAable memory and load them */
1475 	if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1476 			     BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1477 		device_printf(sc->aac_dev,
1478 			      "Not enough contiguous memory available.\n");
1479 		kfree(fm, M_AACBUF);
1480 		return (ENOMEM);
1481 	}
1482 
1483 	/* Ignore errors since this doesn't bounce */
1484 	bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1485 			sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1486 			aac_map_command_helper, &fibphys, 0);
1487 
1488 	/* initialise constant fields in the command structure */
1489 	bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1490 	for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1491 		cm = sc->aac_commands + sc->total_fibs;
1492 		fm->aac_commands = cm;
1493 		cm->cm_sc = sc;
1494 		cm->cm_fib = (struct aac_fib *)
1495 			((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1496 		cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1497 		cm->cm_index = sc->total_fibs;
1498 
1499 		if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1500 					       &cm->cm_datamap)) != 0)
1501 			break;
1502 		AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1503 		aac_release_command(cm);
1504 		sc->total_fibs++;
1505 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
1506 	}
1507 
1508 	if (i > 0) {
1509 		AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1510 		TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1511 		debug(1, "total_fibs= %d\n", sc->total_fibs);
1512 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
1513 		return (0);
1514 	}
1515 
1516 	bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1517 	bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1518 	kfree(fm, M_AACBUF);
1519 	return (ENOMEM);
1520 }
1521 
1522 /*
1523  * Free FIBs owned by this adapter.
1524  */
1525 static void
1526 aac_free_commands(struct aac_softc *sc)
1527 {
1528 	struct aac_fibmap *fm;
1529 	struct aac_command *cm;
1530 	int i;
1531 
1532 	debug_called(1);
1533 
1534 	while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1535 
1536 		TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1537 		/*
1538 		 * We check against total_fibs to handle partially
1539 		 * allocated blocks.
1540 		 */
1541 		for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1542 			cm = fm->aac_commands + i;
1543 			bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1544 		}
1545 		bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1546 		bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1547 		kfree(fm, M_AACBUF);
1548 	}
1549 }
1550 
1551 /*
1552  * Command-mapping helper function - populate this command's s/g table.
1553  */
1554 static void
1555 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1556 {
1557 	struct aac_softc *sc;
1558 	struct aac_command *cm;
1559 	struct aac_fib *fib;
1560 	int i;
1561 
1562 	debug_called(3);
1563 
1564 	cm = (struct aac_command *)arg;
1565 	sc = cm->cm_sc;
1566 	fib = cm->cm_fib;
1567 
1568 	/* copy into the FIB */
1569 	if (cm->cm_sgtable != NULL) {
1570 		if (fib->Header.Command == RawIo) {
1571 			struct aac_sg_tableraw *sg;
1572 			sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1573 			sg->SgCount = nseg;
1574 			for (i = 0; i < nseg; i++) {
1575 				sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1576 				sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1577 				sg->SgEntryRaw[i].Next = 0;
1578 				sg->SgEntryRaw[i].Prev = 0;
1579 				sg->SgEntryRaw[i].Flags = 0;
1580 			}
1581 			/* update the FIB size for the s/g count */
1582 			fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1583 		} else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1584 			struct aac_sg_table *sg;
1585 			sg = cm->cm_sgtable;
1586 			sg->SgCount = nseg;
1587 			for (i = 0; i < nseg; i++) {
1588 				sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1589 				sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1590 			}
1591 			/* update the FIB size for the s/g count */
1592 			fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1593 		} else {
1594 			struct aac_sg_table64 *sg;
1595 			sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1596 			sg->SgCount = nseg;
1597 			for (i = 0; i < nseg; i++) {
1598 				sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1599 				sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1600 			}
1601 			/* update the FIB size for the s/g count */
1602 			fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1603 		}
1604 	}
1605 
1606 	/* Fix up the address values in the FIB.  Use the command array index
1607 	 * instead of a pointer since these fields are only 32 bits.  Shift
1608 	 * the SenderFibAddress over to make room for the fast response bit
1609 	 * and for the AIF bit
1610 	 */
1611 	cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1612 	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1613 
1614 	/* save a pointer to the command for speedy reverse-lookup */
1615 	cm->cm_fib->Header.SenderData = cm->cm_index;
1616 
1617 	if (cm->cm_flags & AAC_CMD_DATAIN)
1618 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1619 				BUS_DMASYNC_PREREAD);
1620 	if (cm->cm_flags & AAC_CMD_DATAOUT)
1621 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1622 				BUS_DMASYNC_PREWRITE);
1623 	cm->cm_flags |= AAC_CMD_MAPPED;
1624 
1625 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
1626 		int count = 10000000L;
1627 		while (AAC_SEND_COMMAND(sc, cm) != 0) {
1628 			if (--count == 0) {
1629 				aac_unmap_command(cm);
1630 				sc->flags |= AAC_QUEUE_FRZN;
1631 				aac_requeue_ready(cm);
1632 				break;
1633 			}
1634 			DELAY(5);			/* wait 5 usec. */
1635 		}
1636 	} else {
1637 		/* Put the FIB on the outbound queue */
1638 		if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1639 			aac_unmap_command(cm);
1640 			sc->flags |= AAC_QUEUE_FRZN;
1641 			aac_requeue_ready(cm);
1642 		}
1643 	}
1644 }
1645 
1646 /*
1647  * Unmap a command from controller-visible space.
1648  */
1649 static void
1650 aac_unmap_command(struct aac_command *cm)
1651 {
1652 	struct aac_softc *sc;
1653 
1654 	debug_called(2);
1655 
1656 	sc = cm->cm_sc;
1657 
1658 	if (!(cm->cm_flags & AAC_CMD_MAPPED))
1659 		return;
1660 
1661 	if (cm->cm_datalen != 0) {
1662 		if (cm->cm_flags & AAC_CMD_DATAIN)
1663 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1664 					BUS_DMASYNC_POSTREAD);
1665 		if (cm->cm_flags & AAC_CMD_DATAOUT)
1666 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1667 					BUS_DMASYNC_POSTWRITE);
1668 
1669 		bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1670 	}
1671 	cm->cm_flags &= ~AAC_CMD_MAPPED;
1672 }
1673 
1674 /*
1675  * Hardware Interface
1676  */
1677 
1678 /*
1679  * Initialise the adapter.
1680  */
1681 static void
1682 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1683 {
1684 	struct aac_softc *sc;
1685 
1686 	debug_called(1);
1687 
1688 	sc = (struct aac_softc *)arg;
1689 
1690 	sc->aac_common_busaddr = segs[0].ds_addr;
1691 }
1692 
1693 static int
1694 aac_check_firmware(struct aac_softc *sc)
1695 {
1696 	u_int32_t major, minor, options = 0, atu_size = 0;
1697 	int status;
1698 
1699 	debug_called(1);
1700 
1701 	/*
1702 	 * Retrieve the firmware version numbers.  Dell PERC2/QC cards with
1703 	 * firmware version 1.x are not compatible with this driver.
1704 	 */
1705 	if (sc->flags & AAC_FLAGS_PERC2QC) {
1706 		if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1707 				     NULL)) {
1708 			device_printf(sc->aac_dev,
1709 				      "Error reading firmware version\n");
1710 			return (EIO);
1711 		}
1712 
1713 		/* These numbers are stored as ASCII! */
1714 		major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1715 		minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1716 		if (major == 1) {
1717 			device_printf(sc->aac_dev,
1718 			    "Firmware version %d.%d is not supported.\n",
1719 			    major, minor);
1720 			return (EINVAL);
1721 		}
1722 	}
1723 
1724 	/*
1725 	 * Retrieve the capabilities/supported options word so we know what
1726 	 * work-arounds to enable.  Some firmware revs don't support this
1727 	 * command.
1728 	 */
1729 	if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1730 		if (status != AAC_SRB_STS_INVALID_REQUEST) {
1731 			device_printf(sc->aac_dev,
1732 			     "RequestAdapterInfo failed\n");
1733 			return (EIO);
1734 		}
1735 	} else {
1736 		options = AAC_GET_MAILBOX(sc, 1);
1737 		atu_size = AAC_GET_MAILBOX(sc, 2);
1738 		sc->supported_options = options;
1739 
1740 		if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1741 		    (sc->flags & AAC_FLAGS_NO4GB) == 0)
1742 			sc->flags |= AAC_FLAGS_4GB_WINDOW;
1743 		if (options & AAC_SUPPORTED_NONDASD)
1744 			sc->flags |= AAC_FLAGS_ENABLE_CAM;
1745 		if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1746 		     && (sizeof(bus_addr_t) > 4)) {
1747 			device_printf(sc->aac_dev,
1748 			    "Enabling 64-bit address support\n");
1749 			sc->flags |= AAC_FLAGS_SG_64BIT;
1750 		}
1751 		if ((options & AAC_SUPPORTED_NEW_COMM)
1752 		 && sc->aac_if.aif_send_command)
1753 			sc->flags |= AAC_FLAGS_NEW_COMM;
1754 		if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1755 			sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1756 	}
1757 
1758 	/* Check for broken hardware that does a lower number of commands */
1759 	sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1760 
1761 	/* Remap mem. resource, if required */
1762 	if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1763 		atu_size > rman_get_size(sc->aac_regs_resource)) {
1764 		bus_release_resource(
1765 			sc->aac_dev, SYS_RES_MEMORY,
1766 			sc->aac_regs_rid, sc->aac_regs_resource);
1767 		sc->aac_regs_resource = bus_alloc_resource(
1768 			sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
1769 			0ul, ~0ul, atu_size, RF_ACTIVE);
1770 		if (sc->aac_regs_resource == NULL) {
1771 			sc->aac_regs_resource = bus_alloc_resource_any(
1772 				sc->aac_dev, SYS_RES_MEMORY,
1773 				&sc->aac_regs_rid, RF_ACTIVE);
1774 			if (sc->aac_regs_resource == NULL) {
1775 				device_printf(sc->aac_dev,
1776 				    "couldn't allocate register window\n");
1777 				return (ENXIO);
1778 			}
1779 			sc->flags &= ~AAC_FLAGS_NEW_COMM;
1780 		}
1781 		sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
1782 		sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
1783 	}
1784 
1785 	/* Read preferred settings */
1786 	sc->aac_max_fib_size = sizeof(struct aac_fib);
1787 	sc->aac_max_sectors = 128;				/* 64KB */
1788 	if (sc->flags & AAC_FLAGS_SG_64BIT)
1789 		sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1790 		 - sizeof(struct aac_blockwrite64)
1791 		 + sizeof(struct aac_sg_table64))
1792 		 / sizeof(struct aac_sg_table64);
1793 	else
1794 		sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1795 		 - sizeof(struct aac_blockwrite)
1796 		 + sizeof(struct aac_sg_table))
1797 		 / sizeof(struct aac_sg_table);
1798 
1799 	if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1800 		options = AAC_GET_MAILBOX(sc, 1);
1801 		sc->aac_max_fib_size = (options & 0xFFFF);
1802 		sc->aac_max_sectors = (options >> 16) << 1;
1803 		options = AAC_GET_MAILBOX(sc, 2);
1804 		sc->aac_sg_tablesize = (options >> 16);
1805 		options = AAC_GET_MAILBOX(sc, 3);
1806 		sc->aac_max_fibs = (options & 0xFFFF);
1807 	}
1808 	if (sc->aac_max_fib_size > PAGE_SIZE)
1809 		sc->aac_max_fib_size = PAGE_SIZE;
1810 	sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1811 
1812 	return (0);
1813 }
1814 
1815 static int
1816 aac_init(struct aac_softc *sc)
1817 {
1818 	struct aac_adapter_init	*ip;
1819 	time_t then;
1820 	u_int32_t code, qoffset;
1821 	int error;
1822 
1823 	debug_called(1);
1824 
1825 	/*
1826 	 * First wait for the adapter to come ready.
1827 	 */
1828 	then = time_second;
1829 	do {
1830 		code = AAC_GET_FWSTATUS(sc);
1831 		if (code & AAC_SELF_TEST_FAILED) {
1832 			device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1833 			return(ENXIO);
1834 		}
1835 		if (code & AAC_KERNEL_PANIC) {
1836 			device_printf(sc->aac_dev,
1837 				      "FATAL: controller kernel panic\n");
1838 			return(ENXIO);
1839 		}
1840 		if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1841 			device_printf(sc->aac_dev,
1842 				      "FATAL: controller not coming ready, "
1843 					   "status %x\n", code);
1844 			return(ENXIO);
1845 		}
1846 	} while (!(code & AAC_UP_AND_RUNNING));
1847 
1848  	error = ENOMEM;
1849  	/*
1850  	 * Create DMA tag for mapping buffers into controller-addressable space.
1851  	 */
1852  	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1853  			       1, 0, 			/* algnmnt, boundary */
1854 			       (sc->flags & AAC_FLAGS_SG_64BIT) ?
1855 			       BUS_SPACE_MAXADDR :
1856  			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1857  			       BUS_SPACE_MAXADDR, 	/* highaddr */
1858  			       NULL, NULL, 		/* filter, filterarg */
1859  			       MAXBSIZE,		/* maxsize */
1860  			       sc->aac_sg_tablesize,	/* nsegments */
1861  			       MAXBSIZE,		/* maxsegsize */
1862  			       BUS_DMA_ALLOCNOW,	/* flags */
1863  			       &sc->aac_buffer_dmat)) {
1864  		device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1865  		goto out;
1866  	}
1867 
1868  	/*
1869  	 * Create DMA tag for mapping FIBs into controller-addressable space..
1870  	 */
1871  	if (bus_dma_tag_create(sc->aac_parent_dmat,	/* parent */
1872  			       1, 0, 			/* algnmnt, boundary */
1873  			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1874  			       BUS_SPACE_MAXADDR_32BIT :
1875  			       0x7fffffff,		/* lowaddr */
1876  			       BUS_SPACE_MAXADDR, 	/* highaddr */
1877  			       NULL, NULL, 		/* filter, filterarg */
1878 			       sc->aac_max_fibs_alloc *
1879 			       sc->aac_max_fib_size,	/* maxsize */
1880  			       1,			/* nsegments */
1881 			       sc->aac_max_fibs_alloc *
1882 			       sc->aac_max_fib_size,	/* maxsegsize */
1883  			       0,			/* flags */
1884  			       &sc->aac_fib_dmat)) {
1885  		device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
1886  		goto out;
1887  	}
1888 
1889 	/*
1890 	 * Create DMA tag for the common structure and allocate it.
1891 	 */
1892 	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1893 			       1, 0,			/* algnmnt, boundary */
1894 			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1895 			       BUS_SPACE_MAXADDR_32BIT :
1896 			       0x7fffffff,		/* lowaddr */
1897 			       BUS_SPACE_MAXADDR, 	/* highaddr */
1898 			       NULL, NULL, 		/* filter, filterarg */
1899 			       8192 + sizeof(struct aac_common), /* maxsize */
1900 			       1,			/* nsegments */
1901 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1902 			       0,			/* flags */
1903 			       &sc->aac_common_dmat)) {
1904 		device_printf(sc->aac_dev,
1905 			      "can't allocate common structure DMA tag\n");
1906 		goto out;
1907 	}
1908 	if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1909 			     BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1910 		device_printf(sc->aac_dev, "can't allocate common structure\n");
1911 		goto out;
1912 	}
1913 	/*
1914 	 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1915 	 * below address 8192 in physical memory.
1916 	 * XXX If the padding is not needed, can it be put to use instead
1917 	 * of ignored?
1918 	 */
1919 	bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1920 			sc->aac_common, 8192 + sizeof(*sc->aac_common),
1921 			aac_common_map, sc, 0);
1922 
1923 	if (sc->aac_common_busaddr < 8192) {
1924 		sc->aac_common =
1925 		    (struct aac_common *)((uint8_t *)sc->aac_common + 8192);
1926 		sc->aac_common_busaddr += 8192;
1927 	}
1928 	bzero(sc->aac_common, sizeof(*sc->aac_common));
1929 
1930 	/* Allocate some FIBs and associated command structs */
1931 	TAILQ_INIT(&sc->aac_fibmap_tqh);
1932 	sc->aac_commands = kmalloc(sc->aac_max_fibs * sizeof(struct aac_command),
1933 				  M_AACBUF, M_INTWAIT | M_ZERO);
1934 	while (sc->total_fibs < AAC_PREALLOCATE_FIBS) {
1935 		if (aac_alloc_commands(sc) != 0)
1936 			break;
1937 	}
1938 	if (sc->total_fibs == 0)
1939 		goto out;
1940 
1941 	/*
1942 	 * Fill in the init structure.  This tells the adapter about the
1943 	 * physical location of various important shared data structures.
1944 	 */
1945 	ip = &sc->aac_common->ac_init;
1946 	ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1947 	if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1948 		ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1949 		sc->flags |= AAC_FLAGS_RAW_IO;
1950 	}
1951 	ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1952 
1953 	ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1954 					 offsetof(struct aac_common, ac_fibs);
1955 	ip->AdapterFibsVirtualAddress = 0;
1956 	ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1957 	ip->AdapterFibAlign = sizeof(struct aac_fib);
1958 
1959 	ip->PrintfBufferAddress = sc->aac_common_busaddr +
1960 				  offsetof(struct aac_common, ac_printf);
1961 	ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1962 
1963 	/*
1964 	 * The adapter assumes that pages are 4K in size, except on some
1965 	 * broken firmware versions that do the page->byte conversion twice,
1966 	 * therefore 'assuming' that this value is in 16MB units (2^24).
1967 	 * Round up since the granularity is so high.
1968 	 */
1969 	/* XXX why should the adapter care? */
1970 	ip->HostPhysMemPages = ctob((int)Maxmem) / AAC_PAGE_SIZE;
1971 	if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1972 		ip->HostPhysMemPages =
1973 		    (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1974 	}
1975 	ip->HostElapsedSeconds = time_second;	/* reset later if invalid */
1976 
1977 	ip->InitFlags = 0;
1978 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
1979 		ip->InitFlags = INITFLAGS_NEW_COMM_SUPPORTED;
1980 		device_printf(sc->aac_dev, "New comm. interface enabled\n");
1981 	}
1982 
1983 	ip->MaxIoCommands = sc->aac_max_fibs;
1984 	ip->MaxIoSize = sc->aac_max_sectors << 9;
1985 	ip->MaxFibSize = sc->aac_max_fib_size;
1986 
1987 	/*
1988 	 * Initialise FIB queues.  Note that it appears that the layout of the
1989 	 * indexes and the segmentation of the entries may be mandated by the
1990 	 * adapter, which is only told about the base of the queue index fields.
1991 	 *
1992 	 * The initial values of the indices are assumed to inform the adapter
1993 	 * of the sizes of the respective queues, and theoretically it could
1994 	 * work out the entire layout of the queue structures from this.  We
1995 	 * take the easy route and just lay this area out like everyone else
1996 	 * does.
1997 	 *
1998 	 * The Linux driver uses a much more complex scheme whereby several
1999 	 * header records are kept for each queue.  We use a couple of generic
2000 	 * list manipulation functions which 'know' the size of each list by
2001 	 * virtue of a table.
2002 	 */
2003 	qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
2004 	qoffset &= ~(AAC_QUEUE_ALIGN - 1);
2005 	sc->aac_queues =
2006 	    (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
2007 	ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
2008 
2009 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2010 		AAC_HOST_NORM_CMD_ENTRIES;
2011 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2012 		AAC_HOST_NORM_CMD_ENTRIES;
2013 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2014 		AAC_HOST_HIGH_CMD_ENTRIES;
2015 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2016 		AAC_HOST_HIGH_CMD_ENTRIES;
2017 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2018 		AAC_ADAP_NORM_CMD_ENTRIES;
2019 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2020 		AAC_ADAP_NORM_CMD_ENTRIES;
2021 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2022 		AAC_ADAP_HIGH_CMD_ENTRIES;
2023 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2024 		AAC_ADAP_HIGH_CMD_ENTRIES;
2025 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2026 		AAC_HOST_NORM_RESP_ENTRIES;
2027 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2028 		AAC_HOST_NORM_RESP_ENTRIES;
2029 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2030 		AAC_HOST_HIGH_RESP_ENTRIES;
2031 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2032 		AAC_HOST_HIGH_RESP_ENTRIES;
2033 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2034 		AAC_ADAP_NORM_RESP_ENTRIES;
2035 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2036 		AAC_ADAP_NORM_RESP_ENTRIES;
2037 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2038 		AAC_ADAP_HIGH_RESP_ENTRIES;
2039 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2040 		AAC_ADAP_HIGH_RESP_ENTRIES;
2041 	sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
2042 		&sc->aac_queues->qt_HostNormCmdQueue[0];
2043 	sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
2044 		&sc->aac_queues->qt_HostHighCmdQueue[0];
2045 	sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
2046 		&sc->aac_queues->qt_AdapNormCmdQueue[0];
2047 	sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
2048 		&sc->aac_queues->qt_AdapHighCmdQueue[0];
2049 	sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
2050 		&sc->aac_queues->qt_HostNormRespQueue[0];
2051 	sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
2052 		&sc->aac_queues->qt_HostHighRespQueue[0];
2053 	sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
2054 		&sc->aac_queues->qt_AdapNormRespQueue[0];
2055 	sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
2056 		&sc->aac_queues->qt_AdapHighRespQueue[0];
2057 
2058 	/*
2059 	 * Do controller-type-specific initialisation
2060 	 */
2061 	switch (sc->aac_hwif) {
2062 	case AAC_HWIF_I960RX:
2063 		AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
2064 		break;
2065 	case AAC_HWIF_RKT:
2066 		AAC_SETREG4(sc, AAC_RKT_ODBR, ~0);
2067 		break;
2068 	default:
2069 		break;
2070 	}
2071 
2072 	/*
2073 	 * Give the init structure to the controller.
2074 	 */
2075 	if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
2076 			     sc->aac_common_busaddr +
2077 			     offsetof(struct aac_common, ac_init), 0, 0, 0,
2078 			     NULL)) {
2079 		device_printf(sc->aac_dev,
2080 			      "error establishing init structure\n");
2081 		error = EIO;
2082 		goto out;
2083 	}
2084 
2085 	error = 0;
2086 out:
2087 	return(error);
2088 }
2089 
2090 /*
2091  * Send a synchronous command to the controller and wait for a result.
2092  * Indicate if the controller completed the command with an error status.
2093  */
2094 static int
2095 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2096 		 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2097 		 u_int32_t *sp)
2098 {
2099 	time_t then;
2100 	u_int32_t status;
2101 
2102 	debug_called(3);
2103 
2104 	/* populate the mailbox */
2105 	AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2106 
2107 	/* ensure the sync command doorbell flag is cleared */
2108 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2109 
2110 	/* then set it to signal the adapter */
2111 	AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2112 
2113 	/* spin waiting for the command to complete */
2114 	then = time_second;
2115 	do {
2116 		if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
2117 			debug(1, "timed out");
2118 			return(EIO);
2119 		}
2120 	} while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2121 
2122 	/* clear the completion flag */
2123 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2124 
2125 	/* get the command status */
2126 	status = AAC_GET_MAILBOX(sc, 0);
2127 	if (sp != NULL)
2128 		*sp = status;
2129 
2130 	if (status != AAC_SRB_STS_SUCCESS)
2131 		return (-1);
2132 	return(0);
2133 }
2134 
2135 int
2136 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2137 		 struct aac_fib *fib, u_int16_t datasize)
2138 {
2139 	debug_called(3);
2140 	KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2141 
2142 	if (datasize > AAC_FIB_DATASIZE)
2143 		return(EINVAL);
2144 
2145 	/*
2146 	 * Set up the sync FIB
2147 	 */
2148 	fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2149 				AAC_FIBSTATE_INITIALISED |
2150 				AAC_FIBSTATE_EMPTY;
2151 	fib->Header.XferState |= xferstate;
2152 	fib->Header.Command = command;
2153 	fib->Header.StructType = AAC_FIBTYPE_TFIB;
2154 	fib->Header.Size = sizeof(struct aac_fib) + datasize;
2155 	fib->Header.SenderSize = sizeof(struct aac_fib);
2156 	fib->Header.SenderFibAddress = 0;	/* Not needed */
2157 	fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2158 					 offsetof(struct aac_common,
2159 						  ac_sync_fib);
2160 
2161 	/*
2162 	 * Give the FIB to the controller, wait for a response.
2163 	 */
2164 	if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2165 			     fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2166 		debug(2, "IO error");
2167 		return(EIO);
2168 	}
2169 
2170 	return (0);
2171 }
2172 
2173 /*
2174  * Adapter-space FIB queue manipulation
2175  *
2176  * Note that the queue implementation here is a little funky; neither the PI or
2177  * CI will ever be zero.  This behaviour is a controller feature.
2178  */
2179 static struct {
2180 	int		size;
2181 	int		notify;
2182 } aac_qinfo[] = {
2183 	{AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2184 	{AAC_HOST_HIGH_CMD_ENTRIES, 0},
2185 	{AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2186 	{AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2187 	{AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2188 	{AAC_HOST_HIGH_RESP_ENTRIES, 0},
2189 	{AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2190 	{AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2191 };
2192 
2193 /*
2194  * Atomically insert an entry into the nominated queue, returns 0 on success or
2195  * EBUSY if the queue is full.
2196  *
2197  * Note: it would be more efficient to defer notifying the controller in
2198  *	 the case where we may be inserting several entries in rapid succession,
2199  *	 but implementing this usefully may be difficult (it would involve a
2200  *	 separate queue/notify interface).
2201  */
2202 static int
2203 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2204 {
2205 	u_int32_t pi, ci;
2206 	int error;
2207 	u_int32_t fib_size;
2208 	u_int32_t fib_addr;
2209 
2210 	debug_called(3);
2211 
2212 	fib_size = cm->cm_fib->Header.Size;
2213 	fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2214 
2215 	/* get the producer/consumer indices */
2216 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2217 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2218 
2219 	/* wrap the queue? */
2220 	if (pi >= aac_qinfo[queue].size)
2221 		pi = 0;
2222 
2223 	/* check for queue full */
2224 	if ((pi + 1) == ci) {
2225 		error = EBUSY;
2226 		goto out;
2227 	}
2228 	/*
2229 	 * To avoid a race with its completion interrupt, place this command on
2230 	 * the busy queue prior to advertising it to the controller.
2231 	 */
2232 	aac_enqueue_busy(cm);
2233 
2234 
2235 
2236 	/* populate queue entry */
2237 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2238 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2239 
2240 	/* update producer index */
2241 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2242 
2243 	/* notify the adapter if we know how */
2244 	if (aac_qinfo[queue].notify != 0)
2245 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2246 
2247 	error = 0;
2248 
2249 out:
2250 	return(error);
2251 }
2252 
2253 /*
2254  * Atomically remove one entry from the nominated queue, returns 0 on
2255  * success or ENOENT if the queue is empty.
2256  */
2257 static int
2258 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2259 		struct aac_fib **fib_addr)
2260 {
2261 	u_int32_t pi, ci;
2262 	u_int32_t fib_index;
2263 	int error;
2264 	int notify;
2265 
2266 	debug_called(3);
2267 
2268 	/* get the producer/consumer indices */
2269 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2270 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2271 
2272 	/* check for queue empty */
2273 	if (ci == pi) {
2274 		error = ENOENT;
2275 		goto out;
2276 	}
2277 
2278 	/* wrap the pi so the following test works */
2279 	if (pi >= aac_qinfo[queue].size)
2280 		pi = 0;
2281 
2282 	notify = 0;
2283 	if (ci == pi + 1)
2284 		notify++;
2285 
2286 	/* wrap the queue? */
2287 	if (ci >= aac_qinfo[queue].size)
2288 		ci = 0;
2289 
2290 	/* fetch the entry */
2291 	*fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2292 
2293 	switch (queue) {
2294 	case AAC_HOST_NORM_CMD_QUEUE:
2295 	case AAC_HOST_HIGH_CMD_QUEUE:
2296 		/*
2297 		 * The aq_fib_addr is only 32 bits wide so it can't be counted
2298 		 * on to hold an address.  For AIF's, the adapter assumes
2299 		 * that it's giving us an address into the array of AIF fibs.
2300 		 * Therefore, we have to convert it to an index.
2301 		 */
2302 		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2303 			sizeof(struct aac_fib);
2304 		*fib_addr = &sc->aac_common->ac_fibs[fib_index];
2305 		break;
2306 
2307 	case AAC_HOST_NORM_RESP_QUEUE:
2308 	case AAC_HOST_HIGH_RESP_QUEUE:
2309 	{
2310 		struct aac_command *cm;
2311 
2312 		/*
2313 		 * As above, an index is used instead of an actual address.
2314 		 * Gotta shift the index to account for the fast response
2315 		 * bit.  No other correction is needed since this value was
2316 		 * originally provided by the driver via the SenderFibAddress
2317 		 * field.
2318 		 */
2319 		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2320 		cm = sc->aac_commands + (fib_index >> 2);
2321 		*fib_addr = cm->cm_fib;
2322 
2323 		/*
2324 		 * Is this a fast response? If it is, update the fib fields in
2325 		 * local memory since the whole fib isn't DMA'd back up.
2326 		 */
2327 		if (fib_index & 0x01) {
2328 			(*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2329 			*((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2330 		}
2331 		break;
2332 	}
2333 	default:
2334 		panic("Invalid queue in aac_dequeue_fib()");
2335 		break;
2336 	}
2337 
2338 	/* update consumer index */
2339 	sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2340 
2341 	/* if we have made the queue un-full, notify the adapter */
2342 	if (notify && (aac_qinfo[queue].notify != 0))
2343 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2344 	error = 0;
2345 
2346 out:
2347 	return(error);
2348 }
2349 
2350 /*
2351  * Put our response to an Adapter Initialed Fib on the response queue
2352  */
2353 static int
2354 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2355 {
2356 	u_int32_t pi, ci;
2357 	int error;
2358 	u_int32_t fib_size;
2359 	u_int32_t fib_addr;
2360 
2361 	debug_called(1);
2362 
2363 	/* Tell the adapter where the FIB is */
2364 	fib_size = fib->Header.Size;
2365 	fib_addr = fib->Header.SenderFibAddress;
2366 	fib->Header.ReceiverFibAddress = fib_addr;
2367 
2368 	/* get the producer/consumer indices */
2369 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2370 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2371 
2372 	/* wrap the queue? */
2373 	if (pi >= aac_qinfo[queue].size)
2374 		pi = 0;
2375 
2376 	/* check for queue full */
2377 	if ((pi + 1) == ci) {
2378 		error = EBUSY;
2379 		goto out;
2380 	}
2381 
2382 	/* populate queue entry */
2383 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2384 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2385 
2386 	/* update producer index */
2387 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2388 
2389 	/* notify the adapter if we know how */
2390 	if (aac_qinfo[queue].notify != 0)
2391 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2392 
2393 	error = 0;
2394 
2395 out:
2396 	return(error);
2397 }
2398 
2399 /*
2400  * Check for commands that have been outstanding for a suspiciously long time,
2401  * and complain about them.
2402  */
2403 static void
2404 aac_timeout(void *xsc)
2405 {
2406 	struct aac_softc *sc = xsc;
2407 	struct aac_command *cm;
2408 	time_t deadline;
2409 	int timedout, code;
2410 	/*
2411 	 * Traverse the busy command list, bitch about late commands once
2412 	 * only.
2413 	 */
2414 	timedout = 0;
2415 	deadline = time_second - AAC_CMD_TIMEOUT;
2416 	TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2417 		if ((cm->cm_timestamp  < deadline)
2418 			/* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
2419 			cm->cm_flags |= AAC_CMD_TIMEDOUT;
2420 			device_printf(sc->aac_dev,
2421 				      "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2422 				      cm, (int)(time_second-cm->cm_timestamp));
2423 			AAC_PRINT_FIB(sc, cm->cm_fib);
2424 			timedout++;
2425 		}
2426 	}
2427 	if (timedout) {
2428 		code = AAC_GET_FWSTATUS(sc);
2429 		if (code != AAC_UP_AND_RUNNING) {
2430 			device_printf(sc->aac_dev, "WARNING! Controller is no "
2431 				      "longer running! code= 0x%x\n", code);
2432 
2433 		}
2434 	}
2435 }
2436 
2437 /*
2438  * Interface Function Vectors
2439  */
2440 
2441 /*
2442  * Read the current firmware status word.
2443  */
2444 static int
2445 aac_sa_get_fwstatus(struct aac_softc *sc)
2446 {
2447 	debug_called(3);
2448 
2449 	return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2450 }
2451 
2452 static int
2453 aac_rx_get_fwstatus(struct aac_softc *sc)
2454 {
2455 	debug_called(3);
2456 
2457 	return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2458 }
2459 
2460 static int
2461 aac_fa_get_fwstatus(struct aac_softc *sc)
2462 {
2463 	int val;
2464 
2465 	debug_called(3);
2466 
2467 	val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2468 	return (val);
2469 }
2470 
2471 static int
2472 aac_rkt_get_fwstatus(struct aac_softc *sc)
2473 {
2474 	debug_called(3);
2475 
2476 	return(AAC_GETREG4(sc, AAC_RKT_FWSTATUS));
2477 }
2478 
2479 /*
2480  * Notify the controller of a change in a given queue
2481  */
2482 
2483 static void
2484 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2485 {
2486 	debug_called(3);
2487 
2488 	AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2489 }
2490 
2491 static void
2492 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2493 {
2494 	debug_called(3);
2495 
2496 	AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2497 }
2498 
2499 static void
2500 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2501 {
2502 	debug_called(3);
2503 
2504 	AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2505 	AAC_FA_HACK(sc);
2506 }
2507 
2508 static void
2509 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2510 {
2511 	debug_called(3);
2512 
2513 	AAC_SETREG4(sc, AAC_RKT_IDBR, qbit);
2514 }
2515 
2516 /*
2517  * Get the interrupt reason bits
2518  */
2519 static int
2520 aac_sa_get_istatus(struct aac_softc *sc)
2521 {
2522 	debug_called(3);
2523 
2524 	return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2525 }
2526 
2527 static int
2528 aac_rx_get_istatus(struct aac_softc *sc)
2529 {
2530 	debug_called(3);
2531 
2532 	return(AAC_GETREG4(sc, AAC_RX_ODBR));
2533 }
2534 
2535 static int
2536 aac_fa_get_istatus(struct aac_softc *sc)
2537 {
2538 	int val;
2539 
2540 	debug_called(3);
2541 
2542 	val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2543 	return (val);
2544 }
2545 
2546 static int
2547 aac_rkt_get_istatus(struct aac_softc *sc)
2548 {
2549 	debug_called(3);
2550 
2551 	return(AAC_GETREG4(sc, AAC_RKT_ODBR));
2552 }
2553 
2554 /*
2555  * Clear some interrupt reason bits
2556  */
2557 static void
2558 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2559 {
2560 	debug_called(3);
2561 
2562 	AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2563 }
2564 
2565 static void
2566 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2567 {
2568 	debug_called(3);
2569 
2570 	AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2571 }
2572 
2573 static void
2574 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2575 {
2576 	debug_called(3);
2577 
2578 	AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2579 	AAC_FA_HACK(sc);
2580 }
2581 
2582 static void
2583 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2584 {
2585 	debug_called(3);
2586 
2587 	AAC_SETREG4(sc, AAC_RKT_ODBR, mask);
2588 }
2589 
2590 /*
2591  * Populate the mailbox and set the command word
2592  */
2593 static void
2594 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2595 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2596 {
2597 	debug_called(4);
2598 
2599 	AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2600 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2601 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2602 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2603 	AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2604 }
2605 
2606 static void
2607 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2608 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2609 {
2610 	debug_called(4);
2611 
2612 	AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2613 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2614 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2615 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2616 	AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2617 }
2618 
2619 static void
2620 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2621 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2622 {
2623 	debug_called(4);
2624 
2625 	AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2626 	AAC_FA_HACK(sc);
2627 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2628 	AAC_FA_HACK(sc);
2629 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2630 	AAC_FA_HACK(sc);
2631 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2632 	AAC_FA_HACK(sc);
2633 	AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2634 	AAC_FA_HACK(sc);
2635 }
2636 
2637 static void
2638 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2639 		    u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2640 {
2641 	debug_called(4);
2642 
2643 	AAC_SETREG4(sc, AAC_RKT_MAILBOX, command);
2644 	AAC_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2645 	AAC_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2646 	AAC_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2647 	AAC_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2648 }
2649 
2650 /*
2651  * Fetch the immediate command status word
2652  */
2653 static int
2654 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2655 {
2656 	debug_called(4);
2657 
2658 	return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2659 }
2660 
2661 static int
2662 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2663 {
2664 	debug_called(4);
2665 
2666 	return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2667 }
2668 
2669 static int
2670 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2671 {
2672 	int val;
2673 
2674 	debug_called(4);
2675 
2676 	val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2677 	return (val);
2678 }
2679 
2680 static int
2681 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2682 {
2683 	debug_called(4);
2684 
2685 	return(AAC_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2686 }
2687 
2688 /*
2689  * Set/clear interrupt masks
2690  */
2691 static void
2692 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2693 {
2694 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2695 
2696 	if (enable) {
2697 		AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2698 	} else {
2699 		AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2700 	}
2701 }
2702 
2703 static void
2704 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2705 {
2706 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2707 
2708 	if (enable) {
2709 		if (sc->flags & AAC_FLAGS_NEW_COMM)
2710 			AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2711 		else
2712 			AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2713 	} else {
2714 		AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2715 	}
2716 }
2717 
2718 static void
2719 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2720 {
2721 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2722 
2723 	if (enable) {
2724 		AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2725 		AAC_FA_HACK(sc);
2726 	} else {
2727 		AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2728 		AAC_FA_HACK(sc);
2729 	}
2730 }
2731 
2732 static void
2733 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2734 {
2735 	debug(2, "%sable interrupts", enable ? "en" : "dis");
2736 
2737 	if (enable) {
2738 		if (sc->flags & AAC_FLAGS_NEW_COMM)
2739 			AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2740 		else
2741 			AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2742 	} else {
2743 		AAC_SETREG4(sc, AAC_RKT_OIMR, ~0);
2744 	}
2745 }
2746 
2747 /*
2748  * New comm. interface: Send command functions
2749  */
2750 static int
2751 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2752 {
2753 	u_int32_t index, device;
2754 
2755 	debug(2, "send command (new comm.)");
2756 
2757 	index = AAC_GETREG4(sc, AAC_RX_IQUE);
2758 	if (index == 0xffffffffL)
2759 		index = AAC_GETREG4(sc, AAC_RX_IQUE);
2760 	if (index == 0xffffffffL)
2761 		return index;
2762 	aac_enqueue_busy(cm);
2763 	device = index;
2764 	AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2765 	device += 4;
2766 	AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2767 	device += 4;
2768 	AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2769 	AAC_SETREG4(sc, AAC_RX_IQUE, index);
2770 	return 0;
2771 }
2772 
2773 static int
2774 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2775 {
2776 	u_int32_t index, device;
2777 
2778 	debug(2, "send command (new comm.)");
2779 
2780 	index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2781 	if (index == 0xffffffffL)
2782 		index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2783 	if (index == 0xffffffffL)
2784 		return index;
2785 	aac_enqueue_busy(cm);
2786 	device = index;
2787 	AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2788 	device += 4;
2789 	AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2790 	device += 4;
2791 	AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2792 	AAC_SETREG4(sc, AAC_RKT_IQUE, index);
2793 	return 0;
2794 }
2795 
2796 /*
2797  * New comm. interface: get, set outbound queue index
2798  */
2799 static int
2800 aac_rx_get_outb_queue(struct aac_softc *sc)
2801 {
2802 	debug_called(3);
2803 
2804 	return(AAC_GETREG4(sc, AAC_RX_OQUE));
2805 }
2806 
2807 static int
2808 aac_rkt_get_outb_queue(struct aac_softc *sc)
2809 {
2810 	debug_called(3);
2811 
2812 	return(AAC_GETREG4(sc, AAC_RKT_OQUE));
2813 }
2814 
2815 static void
2816 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2817 {
2818 	debug_called(3);
2819 
2820 	AAC_SETREG4(sc, AAC_RX_OQUE, index);
2821 }
2822 
2823 static void
2824 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2825 {
2826 	debug_called(3);
2827 
2828 	AAC_SETREG4(sc, AAC_RKT_OQUE, index);
2829 }
2830 
2831 /*
2832  * Debugging and Diagnostics
2833  */
2834 
2835 /*
2836  * Print some information about the controller.
2837  */
2838 static void
2839 aac_describe_controller(struct aac_softc *sc)
2840 {
2841 	struct aac_fib *fib;
2842 	struct aac_adapter_info	*info;
2843 
2844 	debug_called(2);
2845 
2846 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
2847 	aac_alloc_sync_fib(sc, &fib);
2848 
2849 	fib->data[0] = 0;
2850 	if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2851 		device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2852 		aac_release_sync_fib(sc);
2853 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
2854 		return;
2855 	}
2856 
2857 	/* save the kernel revision structure for later use */
2858 	info = (struct aac_adapter_info *)&fib->data[0];
2859 	sc->aac_revision = info->KernelRevision;
2860 
2861 	device_printf(sc->aac_dev, "Adaptec Raid Controller %d.%d.%d-%d\n",
2862 		AAC_DRIVER_VERSION >> 24,
2863 		(AAC_DRIVER_VERSION >> 16) & 0xFF,
2864 		AAC_DRIVER_VERSION & 0xFF,
2865 		AAC_DRIVER_BUILD);
2866 
2867 	if (bootverbose) {
2868 		device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2869 		    "(%dMB cache, %dMB execution), %s\n",
2870 		    aac_describe_code(aac_cpu_variant, info->CpuVariant),
2871 		    info->ClockSpeed, info->TotalMem / (1024 * 1024),
2872 		    info->BufferMem / (1024 * 1024),
2873 		    info->ExecutionMem / (1024 * 1024),
2874 		    aac_describe_code(aac_battery_platform,
2875 		    info->batteryPlatform));
2876 
2877 		device_printf(sc->aac_dev,
2878 		    "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2879 		    info->KernelRevision.external.comp.major,
2880 		    info->KernelRevision.external.comp.minor,
2881 		    info->KernelRevision.external.comp.dash,
2882 		    info->KernelRevision.buildNumber,
2883 		    (u_int32_t)(info->SerialNumber & 0xffffff));
2884 
2885 		device_printf(sc->aac_dev, "Supported Options=%b\n",
2886 			      sc->supported_options,
2887 			      "\20"
2888 			      "\1SNAPSHOT"
2889 			      "\2CLUSTERS"
2890 			      "\3WCACHE"
2891 			      "\4DATA64"
2892 			      "\5HOSTTIME"
2893 			      "\6RAID50"
2894 			      "\7WINDOW4GB"
2895 			      "\10SCSIUPGD"
2896 			      "\11SOFTERR"
2897 			      "\12NORECOND"
2898 			      "\13SGMAP64"
2899 			      "\14ALARM"
2900 			      "\15NONDASD"
2901 			      "\16SCSIMGT"
2902 			      "\17RAIDSCSI"
2903 			      "\21ADPTINFO"
2904 			      "\22NEWCOMM"
2905 			      "\23ARRAY64BIT"
2906 			      "\24HEATSENSOR");
2907 	}
2908 	aac_release_sync_fib(sc);
2909 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
2910 }
2911 
2912 /*
2913  * Look up a text description of a numeric error code and return a pointer to
2914  * same.
2915  */
2916 static char *
2917 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2918 {
2919 	int i;
2920 
2921 	for (i = 0; table[i].string != NULL; i++)
2922 		if (table[i].code == code)
2923 			return(table[i].string);
2924 	return(table[i + 1].string);
2925 }
2926 
2927 /*
2928  * Management Interface
2929  */
2930 
2931 static int
2932 aac_open(struct dev_open_args *ap)
2933 {
2934 	cdev_t dev = ap->a_head.a_dev;
2935 	struct aac_softc *sc;
2936 
2937 	debug_called(2);
2938 
2939 	sc = dev->si_drv1;
2940 
2941 	/* Check to make sure the device isn't already open */
2942 	if (sc->aac_state & AAC_STATE_OPEN) {
2943 		return EBUSY;
2944 	}
2945 	sc->aac_state |= AAC_STATE_OPEN;
2946 
2947 	return 0;
2948 }
2949 
2950 static int
2951 aac_close(struct dev_close_args *ap)
2952 {
2953 	cdev_t dev = ap->a_head.a_dev;
2954 	struct aac_softc *sc;
2955 
2956 	debug_called(2);
2957 
2958 	sc = dev->si_drv1;
2959 
2960 	/* Mark this unit as no longer open  */
2961 	sc->aac_state &= ~AAC_STATE_OPEN;
2962 
2963 	return 0;
2964 }
2965 
2966 static int
2967 aac_ioctl(struct dev_ioctl_args *ap)
2968 {
2969 	cdev_t dev = ap->a_head.a_dev;
2970 	caddr_t arg = ap->a_data;
2971 	struct aac_softc *sc = dev->si_drv1;
2972 	int error = 0;
2973 	uint32_t cookie;
2974 
2975 	debug_called(2);
2976 
2977 	if (ap->a_cmd == AACIO_STATS) {
2978 		union aac_statrequest *as = (union aac_statrequest *)arg;
2979 
2980 		switch (as->as_item) {
2981 		case AACQ_FREE:
2982 		case AACQ_BIO:
2983 		case AACQ_READY:
2984 		case AACQ_BUSY:
2985 		case AACQ_COMPLETE:
2986 			bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2987 			      sizeof(struct aac_qstat));
2988 			break;
2989 		default:
2990 			error = ENOENT;
2991 			break;
2992 		}
2993 		return(error);
2994 	}
2995 
2996 	arg = *(caddr_t *)arg;
2997 
2998 	switch (ap->a_cmd) {
2999 	/* AACIO_STATS already handled above */
3000 	case FSACTL_SENDFIB:
3001 		debug(1, "FSACTL_SENDFIB");
3002 		error = aac_ioctl_sendfib(sc, arg);
3003 		break;
3004 	case FSACTL_AIF_THREAD:
3005 		debug(1, "FSACTL_AIF_THREAD");
3006 		error = EINVAL;
3007 		break;
3008 	case FSACTL_OPEN_GET_ADAPTER_FIB:
3009 		debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
3010 		/*
3011 		 * Pass the caller out an AdapterFibContext.
3012 		 *
3013 		 * Note that because we only support one opener, we
3014 		 * basically ignore this.  Set the caller's context to a magic
3015 		 * number just in case.
3016 		 *
3017 		 * The Linux code hands the driver a pointer into kernel space,
3018 		 * and then trusts it when the caller hands it back.  Aiee!
3019 		 * Here, we give it the proc pointer of the per-adapter aif
3020 		 * thread. It's only used as a sanity check in other calls.
3021 		 */
3022 		cookie = (uint32_t)(uintptr_t)sc->aifthread;
3023 		error = copyout(&cookie, arg, sizeof(cookie));
3024 		break;
3025 	case FSACTL_GET_NEXT_ADAPTER_FIB:
3026 		debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
3027 		error = aac_getnext_aif(sc, arg);
3028 		break;
3029 	case FSACTL_CLOSE_GET_ADAPTER_FIB:
3030 		debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
3031 		/* don't do anything here */
3032 		break;
3033 	case FSACTL_MINIPORT_REV_CHECK:
3034 		debug(1, "FSACTL_MINIPORT_REV_CHECK");
3035 		error = aac_rev_check(sc, arg);
3036 		break;
3037 	case FSACTL_QUERY_DISK:
3038 		debug(1, "FSACTL_QUERY_DISK");
3039 		error = aac_query_disk(sc, arg);
3040 		break;
3041 	case FSACTL_DELETE_DISK:
3042 		/*
3043 		 * We don't trust the underland to tell us when to delete a
3044 		 * container, rather we rely on an AIF coming from the
3045 		 * controller
3046 		 */
3047 		error = 0;
3048 		break;
3049 	case FSACTL_GET_PCI_INFO:
3050 		arg = *(caddr_t*)arg;
3051 	case FSACTL_LNX_GET_PCI_INFO:
3052 		debug(1, "FSACTL_GET_PCI_INFO");
3053 		error = aac_get_pci_info(sc, arg);
3054 		break;
3055 	default:
3056 		debug(1, "unsupported cmd 0x%lx\n", ap->a_cmd);
3057 		error = EINVAL;
3058 		break;
3059 	}
3060 	return(error);
3061 }
3062 
3063 static int
3064 aac_poll(struct dev_poll_args *ap)
3065 {
3066 	cdev_t dev = ap->a_head.a_dev;
3067 	struct aac_softc *sc;
3068 	int revents;
3069 
3070 	sc = dev->si_drv1;
3071 	revents = 0;
3072 
3073 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3074 	if ((ap->a_events & (POLLRDNORM | POLLIN)) != 0) {
3075 		if (sc->aac_aifq_tail != sc->aac_aifq_head)
3076 			revents |= ap->a_events & (POLLIN | POLLRDNORM);
3077 	}
3078 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3079 
3080 	if (revents == 0) {
3081 		if (ap->a_events & (POLLIN | POLLRDNORM))
3082 			selrecord(curthread, &sc->rcv_select);
3083 	}
3084 	ap->a_events = revents;
3085 	return (0);
3086 }
3087 
3088 static void
3089 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
3090 {
3091 
3092 	switch (event->ev_type) {
3093 	case AAC_EVENT_CMFREE:
3094 		AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3095 		if (aac_alloc_command(sc, (struct aac_command **)arg)) {
3096 			aac_add_event(sc, event);
3097 			AAC_LOCK_RELEASE(&sc->aac_io_lock);
3098 			return;
3099 		}
3100 		kfree(event, M_AACBUF);
3101 		wakeup(arg);
3102 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3103 		break;
3104 	default:
3105 		break;
3106 	}
3107 }
3108 
3109 /*
3110  * Send a FIB supplied from userspace
3111  */
3112 static int
3113 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3114 {
3115 	struct aac_command *cm;
3116 	int size, error;
3117 
3118 	debug_called(2);
3119 
3120 	cm = NULL;
3121 
3122 	/*
3123 	 * Get a command
3124 	 */
3125 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3126 	if (aac_alloc_command(sc, &cm)) {
3127 		struct aac_event *event;
3128 
3129 		event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3130 		    M_INTWAIT | M_ZERO);
3131 		event->ev_type = AAC_EVENT_CMFREE;
3132 		event->ev_callback = aac_ioctl_event;
3133 		event->ev_arg = &cm;
3134 		aac_add_event(sc, event);
3135 		tsleep_interlock(&cm, 0);
3136 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3137 		tsleep(&cm, PINTERLOCKED, "sendfib", 0);
3138 		AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3139 	}
3140 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
3141 
3142 	/*
3143 	 * Fetch the FIB header, then re-copy to get data as well.
3144 	 */
3145 	if ((error = copyin(ufib, cm->cm_fib,
3146 			    sizeof(struct aac_fib_header))) != 0)
3147 		goto out;
3148 	size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3149 	if (size > sizeof(struct aac_fib)) {
3150 		device_printf(sc->aac_dev, "incoming FIB oversized (%d > %zd)\n",
3151 			      size, sizeof(struct aac_fib));
3152 		size = sizeof(struct aac_fib);
3153 	}
3154 	if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3155 		goto out;
3156 	cm->cm_fib->Header.Size = size;
3157 	cm->cm_timestamp = time_second;
3158 
3159 	/*
3160 	 * Pass the FIB to the controller, wait for it to complete.
3161 	 */
3162 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3163 	if ((error = aac_wait_command(cm)) != 0) {
3164 		device_printf(sc->aac_dev,
3165 			      "aac_wait_command return %d\n", error);
3166 		goto out;
3167 	}
3168 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
3169 
3170 	/*
3171 	 * Copy the FIB and data back out to the caller.
3172 	 */
3173 	size = cm->cm_fib->Header.Size;
3174 	if (size > sizeof(struct aac_fib)) {
3175 		device_printf(sc->aac_dev, "outbound FIB oversized (%d > %zd)\n",
3176 			      size, sizeof(struct aac_fib));
3177 		size = sizeof(struct aac_fib);
3178 	}
3179 	error = copyout(cm->cm_fib, ufib, size);
3180 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3181 
3182 out:
3183 	if (cm != NULL) {
3184 		aac_release_command(cm);
3185 	}
3186 
3187 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
3188 	return(error);
3189 }
3190 
3191 /*
3192  * Handle an AIF sent to us by the controller; queue it for later reference.
3193  * If the queue fills up, then drop the older entries.
3194  */
3195 static void
3196 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3197 {
3198 	struct aac_aif_command *aif;
3199 	struct aac_container *co, *co_next;
3200 	struct aac_mntinfo *mi;
3201 	struct aac_mntinforesp *mir = NULL;
3202 	u_int16_t rsize;
3203 	int next, found;
3204 	int count = 0, added = 0, i = 0;
3205 
3206 	debug_called(2);
3207 
3208 	aif = (struct aac_aif_command*)&fib->data[0];
3209 	aac_print_aif(sc, aif);
3210 
3211 	/* Is it an event that we should care about? */
3212 	switch (aif->command) {
3213 	case AifCmdEventNotify:
3214 		switch (aif->data.EN.type) {
3215 		case AifEnAddContainer:
3216 		case AifEnDeleteContainer:
3217 			/*
3218 			 * A container was added or deleted, but the message
3219 			 * doesn't tell us anything else!  Re-enumerate the
3220 			 * containers and sort things out.
3221 			 */
3222 			aac_alloc_sync_fib(sc, &fib);
3223 			mi = (struct aac_mntinfo *)&fib->data[0];
3224 			do {
3225 				/*
3226 				 * Ask the controller for its containers one at
3227 				 * a time.
3228 				 * XXX What if the controller's list changes
3229 				 * midway through this enumaration?
3230 				 * XXX This should be done async.
3231 				 */
3232 				bzero(mi, sizeof(struct aac_mntinfo));
3233 				mi->Command = VM_NameServe;
3234 				mi->MntType = FT_FILESYS;
3235 				mi->MntCount = i;
3236 				rsize = sizeof(mir);
3237 				if (aac_sync_fib(sc, ContainerCommand, 0, fib,
3238 						 sizeof(struct aac_mntinfo))) {
3239 					device_printf(sc->aac_dev,
3240 					    "Error probing container %d\n", i);
3241 
3242 					continue;
3243 				}
3244 				mir = (struct aac_mntinforesp *)&fib->data[0];
3245 				/* XXX Need to check if count changed */
3246 				count = mir->MntRespCount;
3247 
3248 				/*
3249 				 * Check the container against our list.
3250 				 * co->co_found was already set to 0 in a
3251 				 * previous run.
3252 				 */
3253 				if ((mir->Status == ST_OK) &&
3254 				    (mir->MntTable[0].VolType != CT_NONE)) {
3255 					found = 0;
3256 					TAILQ_FOREACH(co,
3257 						      &sc->aac_container_tqh,
3258 						      co_link) {
3259 						if (co->co_mntobj.ObjectId ==
3260 						    mir->MntTable[0].ObjectId) {
3261 							co->co_found = 1;
3262 							found = 1;
3263 							break;
3264 						}
3265 					}
3266 					/*
3267 					 * If the container matched, continue
3268 					 * in the list.
3269 					 */
3270 					if (found) {
3271 						i++;
3272 						continue;
3273 					}
3274 
3275 					/*
3276 					 * This is a new container.  Do all the
3277 					 * appropriate things to set it up.
3278 					 */
3279 					aac_add_container(sc, mir, 1);
3280 					added = 1;
3281 				}
3282 				i++;
3283 			} while ((i < count) && (i < AAC_MAX_CONTAINERS));
3284 			aac_release_sync_fib(sc);
3285 
3286 			/*
3287 			 * Go through our list of containers and see which ones
3288 			 * were not marked 'found'.  Since the controller didn't
3289 			 * list them they must have been deleted.  Do the
3290 			 * appropriate steps to destroy the device.  Also reset
3291 			 * the co->co_found field.
3292 			 */
3293 			co = TAILQ_FIRST(&sc->aac_container_tqh);
3294 			while (co != NULL) {
3295 				if (co->co_found == 0) {
3296 					AAC_LOCK_RELEASE(&sc->aac_io_lock);
3297 					get_mplock();
3298 					device_delete_child(sc->aac_dev,
3299 							    co->co_disk);
3300 					rel_mplock();
3301 					AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3302 					co_next = TAILQ_NEXT(co, co_link);
3303 					AAC_LOCK_ACQUIRE(&sc->
3304 							aac_container_lock);
3305 					TAILQ_REMOVE(&sc->aac_container_tqh, co,
3306 						     co_link);
3307 					AAC_LOCK_RELEASE(&sc->
3308 							 aac_container_lock);
3309 					kfree(co, M_AACBUF);
3310 					co = co_next;
3311 				} else {
3312 					co->co_found = 0;
3313 					co = TAILQ_NEXT(co, co_link);
3314 				}
3315 			}
3316 
3317 			/* Attach the newly created containers */
3318 			if (added) {
3319 				AAC_LOCK_RELEASE(&sc->aac_io_lock);
3320 				get_mplock();
3321 				bus_generic_attach(sc->aac_dev);
3322 				rel_mplock();
3323 				AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3324 			}
3325 
3326 			break;
3327 
3328 		default:
3329 			break;
3330 		}
3331 
3332 	default:
3333 		break;
3334 	}
3335 
3336 	/* Copy the AIF data to the AIF queue for ioctl retrieval */
3337 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3338 	next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
3339 	if (next != sc->aac_aifq_tail) {
3340 		bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
3341 		sc->aac_aifq_head = next;
3342 
3343 		/* On the off chance that someone is sleeping for an aif... */
3344 		if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3345 			wakeup(sc->aac_aifq);
3346 		/* token may have been lost */
3347 		/* Wakeup any poll()ers */
3348 		selwakeup(&sc->rcv_select);
3349 		/* token may have been lost */
3350 	}
3351 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3352 
3353 	return;
3354 }
3355 
3356 /*
3357  * Return the Revision of the driver to userspace and check to see if the
3358  * userspace app is possibly compatible.  This is extremely bogus since
3359  * our driver doesn't follow Adaptec's versioning system.  Cheat by just
3360  * returning what the card reported.
3361  */
3362 static int
3363 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3364 {
3365 	struct aac_rev_check rev_check;
3366 	struct aac_rev_check_resp rev_check_resp;
3367 	int error = 0;
3368 
3369 	debug_called(2);
3370 
3371 	/*
3372 	 * Copyin the revision struct from userspace
3373 	 */
3374 	if ((error = copyin(udata, (caddr_t)&rev_check,
3375 			sizeof(struct aac_rev_check))) != 0) {
3376 		return error;
3377 	}
3378 
3379 	debug(2, "Userland revision= %d\n",
3380 	      rev_check.callingRevision.buildNumber);
3381 
3382 	/*
3383 	 * Doctor up the response struct.
3384 	 */
3385 	rev_check_resp.possiblyCompatible = 1;
3386 	rev_check_resp.adapterSWRevision.external.ul =
3387 	    sc->aac_revision.external.ul;
3388 	rev_check_resp.adapterSWRevision.buildNumber =
3389 	    sc->aac_revision.buildNumber;
3390 
3391 	return(copyout((caddr_t)&rev_check_resp, udata,
3392 			sizeof(struct aac_rev_check_resp)));
3393 }
3394 
3395 /*
3396  * Pass the caller the next AIF in their queue
3397  */
3398 static int
3399 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3400 {
3401 	struct get_adapter_fib_ioctl agf;
3402 	int error;
3403 
3404 	debug_called(2);
3405 
3406 	if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3407 
3408 		/*
3409 		 * Check the magic number that we gave the caller.
3410 		 */
3411 		if (agf.AdapterFibContext != (int)(uintptr_t)sc->aifthread) {
3412 			error = EFAULT;
3413 		} else {
3414 
3415 			error = aac_return_aif(sc, agf.AifFib);
3416 
3417 			if ((error == EAGAIN) && (agf.Wait)) {
3418 				sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3419 				while (error == EAGAIN) {
3420 					error = tsleep(sc->aac_aifq,
3421 						       PCATCH, "aacaif", 0);
3422 					if (error == 0)
3423 						error = aac_return_aif(sc,
3424 						    agf.AifFib);
3425 				}
3426 				sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3427 			}
3428 		}
3429 	}
3430 	return(error);
3431 }
3432 
3433 /*
3434  * Hand the next AIF off the top of the queue out to userspace.
3435  *
3436  * YYY token could be lost during copyout
3437  */
3438 static int
3439 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
3440 {
3441 	int next, error;
3442 
3443 	debug_called(2);
3444 
3445 	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3446 	if (sc->aac_aifq_tail == sc->aac_aifq_head) {
3447 		AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3448 		return (EAGAIN);
3449 	}
3450 
3451 	next = (sc->aac_aifq_tail + 1) % AAC_AIFQ_LENGTH;
3452 	error = copyout(&sc->aac_aifq[next], uptr,
3453 			sizeof(struct aac_aif_command));
3454 	if (error)
3455 		device_printf(sc->aac_dev,
3456 		    "aac_return_aif: copyout returned %d\n", error);
3457 	else
3458 		sc->aac_aifq_tail = next;
3459 
3460 	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3461 	return(error);
3462 }
3463 
3464 static int
3465 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3466 {
3467 	struct aac_pci_info {
3468 		u_int32_t bus;
3469 		u_int32_t slot;
3470 	} pciinf;
3471 	int error;
3472 
3473 	debug_called(2);
3474 
3475 	pciinf.bus = pci_get_bus(sc->aac_dev);
3476 	pciinf.slot = pci_get_slot(sc->aac_dev);
3477 
3478 	error = copyout((caddr_t)&pciinf, uptr,
3479 			sizeof(struct aac_pci_info));
3480 
3481 	return (error);
3482 }
3483 
3484 /*
3485  * Give the userland some information about the container.  The AAC arch
3486  * expects the driver to be a SCSI passthrough type driver, so it expects
3487  * the containers to have b:t:l numbers.  Fake it.
3488  */
3489 static int
3490 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3491 {
3492 	struct aac_query_disk query_disk;
3493 	struct aac_container *co;
3494 	struct aac_disk	*disk;
3495 	int error, id;
3496 
3497 	debug_called(2);
3498 
3499 	disk = NULL;
3500 
3501 	error = copyin(uptr, (caddr_t)&query_disk,
3502 		       sizeof(struct aac_query_disk));
3503 	if (error)
3504 		return (error);
3505 
3506 	id = query_disk.ContainerNumber;
3507 	if (id == -1)
3508 		return (EINVAL);
3509 
3510 	AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
3511 	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3512 		if (co->co_mntobj.ObjectId == id)
3513 			break;
3514 		}
3515 
3516 	if (co == NULL) {
3517 			query_disk.Valid = 0;
3518 			query_disk.Locked = 0;
3519 			query_disk.Deleted = 1;		/* XXX is this right? */
3520 	} else {
3521 		disk = device_get_softc(co->co_disk);
3522 		query_disk.Valid = 1;
3523 		query_disk.Locked =
3524 		    (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3525 		query_disk.Deleted = 0;
3526 		query_disk.Bus = device_get_unit(sc->aac_dev);
3527 		query_disk.Target = disk->unit;
3528 		query_disk.Lun = 0;
3529 		query_disk.UnMapped = 0;
3530 		bcopy(disk->ad_dev_t->si_name,
3531 		      &query_disk.diskDeviceName[0], 10);
3532 	}
3533 	AAC_LOCK_RELEASE(&sc->aac_container_lock);
3534 
3535 	error = copyout((caddr_t)&query_disk, uptr,
3536 			sizeof(struct aac_query_disk));
3537 
3538 	return (error);
3539 }
3540 
3541 static void
3542 aac_get_bus_info(struct aac_softc *sc)
3543 {
3544 	struct aac_fib *fib;
3545 	struct aac_ctcfg *c_cmd;
3546 	struct aac_ctcfg_resp *c_resp;
3547 	struct aac_vmioctl *vmi;
3548 	struct aac_vmi_businf_resp *vmi_resp;
3549 	struct aac_getbusinf businfo;
3550 	struct aac_sim *caminf;
3551 	device_t child;
3552 	int i, found, error;
3553 
3554 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3555 	aac_alloc_sync_fib(sc, &fib);
3556 	c_cmd = (struct aac_ctcfg *)&fib->data[0];
3557 	bzero(c_cmd, sizeof(struct aac_ctcfg));
3558 
3559 	c_cmd->Command = VM_ContainerConfig;
3560 	c_cmd->cmd = CT_GET_SCSI_METHOD;
3561 	c_cmd->param = 0;
3562 
3563 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3564 	    sizeof(struct aac_ctcfg));
3565 	if (error) {
3566 		device_printf(sc->aac_dev, "Error %d sending "
3567 		    "VM_ContainerConfig command\n", error);
3568 		aac_release_sync_fib(sc);
3569 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3570 		return;
3571 	}
3572 
3573 	c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3574 	if (c_resp->Status != ST_OK) {
3575 		device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3576 		    c_resp->Status);
3577 		aac_release_sync_fib(sc);
3578 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3579 		return;
3580 	}
3581 
3582 	sc->scsi_method_id = c_resp->param;
3583 
3584 	vmi = (struct aac_vmioctl *)&fib->data[0];
3585 	bzero(vmi, sizeof(struct aac_vmioctl));
3586 
3587 	vmi->Command = VM_Ioctl;
3588 	vmi->ObjType = FT_DRIVE;
3589 	vmi->MethId = sc->scsi_method_id;
3590 	vmi->ObjId = 0;
3591 	vmi->IoctlCmd = GetBusInfo;
3592 
3593 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3594 	    sizeof(struct aac_vmioctl));
3595 	if (error) {
3596 		device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3597 		    error);
3598 		aac_release_sync_fib(sc);
3599 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3600 		return;
3601 	}
3602 
3603 	vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3604 	if (vmi_resp->Status != ST_OK) {
3605 		debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
3606 		aac_release_sync_fib(sc);
3607 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
3608 		return;
3609 	}
3610 
3611 	bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3612 	aac_release_sync_fib(sc);
3613 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
3614 
3615 	found = 0;
3616 	for (i = 0; i < businfo.BusCount; i++) {
3617 		if (businfo.BusValid[i] != AAC_BUS_VALID)
3618 			continue;
3619 
3620 		caminf = (struct aac_sim *)kmalloc(sizeof(struct aac_sim),
3621 		    M_AACBUF, M_INTWAIT | M_ZERO);
3622 
3623 		child = device_add_child(sc->aac_dev, "aacp", -1);
3624 		if (child == NULL) {
3625 			device_printf(sc->aac_dev,
3626 			    "device_add_child failed for passthrough bus %d\n",
3627 			    i);
3628 			kfree(caminf, M_AACBUF);
3629 			break;
3630 		};
3631 
3632 		caminf->TargetsPerBus = businfo.TargetsPerBus;
3633 		caminf->BusNumber = i;
3634 		caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3635 		caminf->aac_sc = sc;
3636 		caminf->sim_dev = child;
3637 
3638 		device_set_ivars(child, caminf);
3639 		device_set_desc(child, "SCSI Passthrough Bus");
3640 		TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3641 
3642 		found = 1;
3643 	}
3644 
3645 	if (found)
3646 		bus_generic_attach(sc->aac_dev);
3647 
3648 	return;
3649 }
3650