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