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