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