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