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