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