Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / raid / aac / aacvar.h
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/aacvar.h,v 1.4.2.7 2003/04/08 13:22:08 scottl Exp $
30  */
31
32 #include <sys/buf.h>
33 #include <sys/buf2.h>
34 #include <sys/eventhandler.h>
35 #include <sys/lock.h>
36 #include <sys/event.h>
37 #include <sys/taskqueue.h>
38 #include <sys/thread2.h>
39
40 /*
41  * Driver Parameter Definitions
42  */
43
44 /*
45  * The firmware interface allows for a 16-bit s/g list length.  We limit 
46  * ourselves to a reasonable maximum and ensure alignment.
47  */
48 #define AAC_MAXSGENTRIES        64      /* max S/G entries, limit 65535 */              
49
50 /*
51  * We allocate a small set of FIBs for the adapter to use to send us messages.
52  */
53 #define AAC_ADAPTER_FIBS        8
54
55 /*
56  * FIBs are allocated in page-size chunks and can grow up to the 512
57  * limit imposed by the hardware.
58  */
59 #define AAC_PREALLOCATE_FIBS    128
60 #define AAC_NUM_MGT_FIB         8
61 /*
62  * The controller reports status events in AIFs.  We hang on to a number of
63  * these in order to pass them out to user-space management tools.
64  */
65 #define AAC_AIFQ_LENGTH         64
66
67 /*
68  * Firmware messages are passed in the kprintf buffer.
69  */
70 #define AAC_PRINTF_BUFSIZE      256
71
72 /*
73  * We wait this many seconds for the adapter to come ready if it is still 
74  * booting
75  */
76 #define AAC_BOOT_TIMEOUT        (3 * 60)
77
78 /*
79  * Timeout for immediate commands.
80  */
81 #define AAC_IMMEDIATE_TIMEOUT   30              /* seconds */
82
83 /*
84  * Timeout for normal commands
85  */
86 #define AAC_CMD_TIMEOUT         30              /* seconds */
87
88 /*
89  * Rate at which we periodically check for timed out commands and kick the
90  * controller.
91  */
92 #define AAC_PERIODIC_INTERVAL   20              /* seconds */
93
94 /*
95  * Per-container data structure
96  */
97 struct aac_container
98 {
99         struct aac_mntobj               co_mntobj;
100         device_t                        co_disk;
101         int                             co_found;
102         TAILQ_ENTRY(aac_container)      co_link;
103 };
104
105 /*
106  * Per-SIM data structure
107  */
108 struct aac_sim
109 {
110         device_t                sim_dev;
111         int                     TargetsPerBus;
112         int                     BusNumber;
113         int                     InitiatorBusId;
114         struct aac_softc        *aac_sc;
115         TAILQ_ENTRY(aac_sim)    sim_link;
116 };
117
118 /*
119  * Per-disk structure
120  */
121 struct aac_disk 
122 {
123         device_t                        ad_dev;
124         cdev_t                          ad_dev_t;
125         struct aac_softc                *ad_controller;
126         struct aac_container            *ad_container;
127         struct disk                     ad_disk;
128         struct devstat                  ad_stats;
129         int                             ad_flags;
130 #define AAC_DISK_OPEN   (1<<0)
131         int                             ad_cylinders;
132         int                             ad_heads;
133         int                             ad_sectors;
134         u_int32_t                       ad_size;
135         int                             unit;
136 };
137
138 /*
139  * Per-command control structure.
140  */
141 struct aac_command
142 {
143         TAILQ_ENTRY(aac_command) cm_link;       /* list linkage */
144
145         struct aac_softc        *cm_sc;         /* controller that owns us */
146
147         struct aac_fib          *cm_fib;        /* FIB associated with this
148                                                  * command */
149         u_int64_t               cm_fibphys;     /* bus address of the FIB */
150         struct buf              *cm_data;       /* pointer to data in kernel
151                                                  * space */
152         u_int32_t               cm_datalen;     /* data length */
153         bus_dmamap_t            cm_datamap;     /* DMA map for bio data */
154         struct aac_sg_table     *cm_sgtable;    /* pointer to s/g table in
155                                                  * command */
156         int                     cm_flags;
157 #define AAC_CMD_MAPPED          (1<<0)          /* command has had its data
158                                                  * mapped */
159 #define AAC_CMD_DATAIN          (1<<1)          /* command involves data moving
160                                                  * from controller to host */
161 #define AAC_CMD_DATAOUT         (1<<2)          /* command involves data moving
162                                                  * from host to controller */
163 #define AAC_CMD_COMPLETED       (1<<3)          /* command has been completed */
164 #define AAC_CMD_TIMEDOUT        (1<<4)          /* command taken too long */
165 #define AAC_ON_AACQ_FREE        (1<<5)
166 #define AAC_ON_AACQ_READY       (1<<6)
167 #define AAC_ON_AACQ_BUSY        (1<<7)
168 #define AAC_ON_AACQ_COMPLETE    (1<<8)
169 #define AAC_ON_AACQ_NORM        (1<<10)
170 #define AAC_ON_AACQ_AIF         (1<<11)
171 #define AAC_ON_AACQ_MASK        ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10)|(1<<11))
172 #define AAC_QUEUE_FRZN          (1<<9)          /* Freeze the processing of
173                                                  * commands on the queue. */
174
175         void                    (* cm_complete)(struct aac_command *cm);
176         void                    *cm_private;
177         time_t                  cm_timestamp;   /* command creation time */
178         int                     cm_queue;
179         int                     cm_index;
180 };
181
182 struct aac_fibmap {
183         TAILQ_ENTRY(aac_fibmap) fm_link;        /* list linkage */
184         struct aac_fib          *aac_fibs;
185         bus_dmamap_t            aac_fibmap;
186         struct aac_command      *aac_commands;
187 };
188
189 /*
190  * We gather a number of adapter-visible items into a single structure.
191  *
192  * The ordering of this strucure may be important; we copy the Linux driver:
193  *
194  * Adapter FIBs
195  * Init struct
196  * Queue headers (Comm Area)
197  * Printf buffer
198  *
199  * In addition, we add:
200  * Sync Fib
201  */
202 struct aac_common {
203         /* fibs for the controller to send us messages */
204         struct aac_fib          ac_fibs[AAC_ADAPTER_FIBS];
205
206         /* the init structure */
207         struct aac_adapter_init ac_init;
208
209         /* arena within which the queue structures are kept */
210         u_int8_t                ac_qbuf[sizeof(struct aac_queue_table) +
211                                 AAC_QUEUE_ALIGN];
212
213         /* buffer for text messages from the controller */
214         char                    ac_printf[AAC_PRINTF_BUFSIZE];
215         
216         /* fib for synchronous commands */
217         struct aac_fib          ac_sync_fib;
218 };
219
220 /*
221  * Interface operations
222  */
223 struct aac_interface 
224 {
225         int     (*aif_get_fwstatus)(struct aac_softc *sc);
226         void    (*aif_qnotify)(struct aac_softc *sc, int qbit);
227         int     (*aif_get_istatus)(struct aac_softc *sc);
228         void    (*aif_clr_istatus)(struct aac_softc *sc, int mask);
229         void    (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
230                                    u_int32_t arg0, u_int32_t arg1,
231                                    u_int32_t arg2, u_int32_t arg3);
232         int     (*aif_get_mailbox)(struct aac_softc *sc, int mb);
233         void    (*aif_set_interrupts)(struct aac_softc *sc, int enable);
234         int     (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
235         int     (*aif_get_outb_queue)(struct aac_softc *sc);
236         void    (*aif_set_outb_queue)(struct aac_softc *sc, int index);
237 };
238 extern struct aac_interface     aac_rx_interface;
239 extern struct aac_interface     aac_sa_interface;
240 extern struct aac_interface     aac_fa_interface;
241 extern struct aac_interface     aac_rkt_interface;
242
243 #define AAC_GET_FWSTATUS(sc)            ((sc)->aac_if.aif_get_fwstatus((sc)))
244 #define AAC_QNOTIFY(sc, qbit)           ((sc)->aac_if.aif_qnotify((sc), (qbit)))
245 #define AAC_GET_ISTATUS(sc)             ((sc)->aac_if.aif_get_istatus((sc)))
246 #define AAC_CLEAR_ISTATUS(sc, mask)     ((sc)->aac_if.aif_clr_istatus((sc), \
247                                         (mask)))
248 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
249         ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
250         (arg3)))
251 #define AAC_GET_MAILBOX(sc, mb)         ((sc)->aac_if.aif_get_mailbox((sc), \
252                                         (mb)))
253 #define AAC_MASK_INTERRUPTS(sc)         ((sc)->aac_if.aif_set_interrupts((sc), \
254                                         0))
255 #define AAC_UNMASK_INTERRUPTS(sc)       ((sc)->aac_if.aif_set_interrupts((sc), \
256                                         1))
257 #define AAC_SEND_COMMAND(sc, cm)        ((sc)->aac_if.aif_send_command((sc), (cm)))
258 #define AAC_GET_OUTB_QUEUE(sc)          ((sc)->aac_if.aif_get_outb_queue((sc)))
259 #define AAC_SET_OUTB_QUEUE(sc, idx)     ((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
260
261 #define AAC_SETREG4(sc, reg, val)       bus_space_write_4(sc->aac_btag, \
262                                         sc->aac_bhandle, reg, val)
263 #define AAC_GETREG4(sc, reg)            bus_space_read_4 (sc->aac_btag, \
264                                         sc->aac_bhandle, reg)
265 #define AAC_SETREG2(sc, reg, val)       bus_space_write_2(sc->aac_btag, \
266                                         sc->aac_bhandle, reg, val)
267 #define AAC_GETREG2(sc, reg)            bus_space_read_2 (sc->aac_btag, \
268                                         sc->aac_bhandle, reg)
269 #define AAC_SETREG1(sc, reg, val)       bus_space_write_1(sc->aac_btag, \
270                                         sc->aac_bhandle, reg, val)
271 #define AAC_GETREG1(sc, reg)            bus_space_read_1 (sc->aac_btag, \
272                                         sc->aac_bhandle, reg)
273
274 /* Define the OS version specific locks */
275 typedef struct lock aac_lock_t;
276 #define AAC_LOCK_INIT(l, s)     lockinit(l, s, 0, LK_CANRECURSE)
277 #define AAC_LOCK_ACQUIRE(l)     lockmgr(l, LK_EXCLUSIVE)
278 #define AAC_LOCK_RELEASE(l)     lockmgr(l, LK_RELEASE)
279
280 /*
281  * Per-controller structure.
282  */
283 struct aac_softc 
284 {
285         /* bus connections */
286         device_t                aac_dev;
287         struct callout          aac_watchdog;
288         struct resource         *aac_regs_resource;     /* register interface
289                                                          * window */
290         int                     aac_regs_rid;           /* resource ID */
291         bus_space_handle_t      aac_bhandle;            /* bus space handle */
292         bus_space_tag_t         aac_btag;               /* bus space tag */
293         bus_dma_tag_t           aac_parent_dmat;        /* parent DMA tag */
294         bus_dma_tag_t           aac_buffer_dmat;        /* data buffer/command
295                                                          * DMA tag */
296         struct resource         *aac_irq;               /* interrupt */
297         int                     aac_irq_rid;
298         void                    *aac_intr;              /* interrupt handle */
299         eventhandler_tag        eh;
300
301         /* controller features, limits and status */
302         int                     aac_state;
303 #define AAC_STATE_SUSPEND       (1<<0)
304 #define AAC_STATE_OPEN          (1<<1)
305 #define AAC_STATE_INTERRUPTS_ON (1<<2)
306 #define AAC_STATE_AIF_SLEEPER   (1<<3)
307         struct FsaRevision              aac_revision;
308
309         /* controller hardware interface */
310         int                     aac_hwif;
311 #define AAC_HWIF_I960RX         0
312 #define AAC_HWIF_STRONGARM      1
313 #define AAC_HWIF_FALCON         2
314 #define AAC_HWIF_RKT            3
315 #define AAC_HWIF_UNKNOWN        -1
316         bus_dma_tag_t           aac_common_dmat;        /* common structure
317                                                          * DMA tag */
318         bus_dmamap_t            aac_common_dmamap;      /* common structure
319                                                          * DMA map */
320         struct aac_common       *aac_common;
321         u_int32_t               aac_common_busaddr;
322         struct aac_interface    aac_if;
323
324         /* command/fib resources */
325         bus_dma_tag_t           aac_fib_dmat;   /* DMA tag for allocing FIBs */
326         TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh;
327         u_int                   total_fibs;
328         struct aac_command      *aac_commands;
329
330         /* command management */
331         TAILQ_HEAD(,aac_command) aac_free;      /* command structures 
332                                                  * available for reuse */
333         TAILQ_HEAD(,aac_command) aac_ready;     /* commands on hold for
334                                                  * controller resources */
335         TAILQ_HEAD(,aac_command) aac_busy;
336         TAILQ_HEAD(,aac_command) aac_complete;  /* commands which have been
337                                                  * returned by the controller */
338         TAILQ_HEAD(,aac_command) aac_aif;
339 #if 0
340         TAILQ_HEAD(,aac_command) aac_norm;
341 #endif
342         TAILQ_HEAD(,aac_event)  aac_ev_cmfree;
343         struct bio_queue_head   aac_bioq;
344         struct aac_queue_table  *aac_queues;
345         struct aac_queue_entry  *aac_qentries[AAC_QUEUE_COUNT];
346
347         struct aac_qstat        aac_qstat[AACQ_COUNT];  /* queue statistics */
348
349         /* connected containters */
350         TAILQ_HEAD(,aac_container)      aac_container_tqh;
351         aac_lock_t              aac_container_lock;
352
353         /*
354          * The general I/O lock.  This protects the sync fib, the lists, the
355          * queues, and the registers.
356          */
357         aac_lock_t              aac_io_lock;
358
359         /* delayed activity infrastructure */
360         struct task             aac_task_complete;      /* deferred-completion
361                                                          * task */
362         struct intr_config_hook aac_ich;
363
364         /* management interface */
365         cdev_t                  aac_dev_t;
366         aac_lock_t              aac_aifq_lock;
367         struct aac_aif_command  aac_aifq[AAC_AIFQ_LENGTH];
368         int                     aac_aifq_head;
369         int                     aac_aifq_tail;
370         struct kqinfo           rcv_kq;
371         struct thread           *aifthread;
372         int                     aifflags;
373 #define AAC_AIFFLAGS_RUNNING    (1 << 0)
374 #define AAC_AIFFLAGS_AIF        (1 << 1)
375 #define AAC_AIFFLAGS_EXIT       (1 << 2)
376 #define AAC_AIFFLAGS_EXITED     (1 << 3)
377 #define AAC_AIFFLAGS_PRINTF     (1 << 4)
378 #define AAC_AIFFLAGS_ALLOCFIBS  (1 << 5)
379 #define AAC_AIFFLAGS_PENDING    (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
380                                  AAC_AIFFLAGS_ALLOCFIBS)
381         u_int32_t               flags;
382 #define AAC_FLAGS_PERC2QC       (1 << 0)
383 #define AAC_FLAGS_ENABLE_CAM    (1 << 1)        /* No SCSI passthrough */
384 #define AAC_FLAGS_CAM_NORESET   (1 << 2)        /* Fake SCSI resets */
385 #define AAC_FLAGS_CAM_PASSONLY  (1 << 3)        /* Only create pass devices */
386 #define AAC_FLAGS_SG_64BIT      (1 << 4)        /* Use 64-bit S/G addresses */
387 #define AAC_FLAGS_4GB_WINDOW    (1 << 5)        /* Device can access host mem
388                                                  * 2GB-4GB range */
389 #define AAC_FLAGS_NO4GB         (1 << 6)        /* Can't access host mem >2GB */
390 #define AAC_FLAGS_256FIBS       (1 << 7)        /* Can only do 256 commands */
391 #define AAC_FLAGS_BROKEN_MEMMAP (1 << 8)        /* Broken HostPhysMemPages */
392 #define AAC_FLAGS_SLAVE         (1 << 9)
393 #define AAC_FLAGS_MASTER        (1 << 10)
394 #define AAC_FLAGS_NEW_COMM      (1 << 11)       /* New comm. interface supported */
395 #define AAC_FLAGS_RAW_IO        (1 << 12)       /* Raw I/O interface */
396 #define AAC_FLAGS_ARRAY_64BIT   (1 << 13)       /* 64-bit array size */
397   
398         u_int32_t               supported_options;
399         u_int32_t               scsi_method_id;
400         TAILQ_HEAD(,aac_sim)    aac_sim_tqh;
401
402         u_int32_t       aac_max_fibs;           /* max. FIB count */
403         u_int32_t       aac_max_fibs_alloc;     /* max. alloc. per alloc_commands() */
404         u_int32_t       aac_max_fib_size;       /* max. FIB size */
405         u_int32_t       aac_sg_tablesize;       /* max. sg count from host */
406         u_int32_t       aac_max_sectors;        /* max. I/O size from host (blocks) */
407 };
408
409 /*
410  * Event callback mechanism for the driver
411  */
412 #define AAC_EVENT_NONE          0x00
413 #define AAC_EVENT_CMFREE        0x01
414 #define AAC_EVENT_MASK          0xff
415 #define AAC_EVENT_REPEAT        0x100
416
417 typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
418     void *arg);
419 struct aac_event {
420         TAILQ_ENTRY(aac_event)  ev_links;
421         int                     ev_type;
422         aac_event_cb_t          *ev_callback;
423         void                    *ev_arg;
424 };
425
426 #ifndef AAC_DRIVER_BUILD
427 # define AAC_DRIVER_BUILD 1
428 #endif
429
430 /*
431  * Public functions
432  */
433 extern void             aac_free(struct aac_softc *sc);
434 extern int              aac_attach(struct aac_softc *sc);
435 extern int              aac_detach(device_t dev);
436 extern int              aac_suspend(device_t dev); 
437 extern int              aac_resume(device_t dev);
438 extern void             aac_new_intr(void *arg);
439 extern void             aac_fast_intr(void *arg);
440 extern devclass_t       aac_devclass;
441 extern void             aac_submit_bio(struct aac_disk *ad, struct bio *bio);
442 extern void             aac_biodone(struct bio *bio, const char *code);
443 extern int              aac_dump_enqueue(struct aac_disk *ad, u_int64_t lba,
444                                          void *data, int nblks);
445 extern void             aac_dump_complete(struct aac_softc *sc);
446 extern void             aac_startio(struct aac_softc *sc);
447 extern int              aac_alloc_command(struct aac_softc *sc,
448                                           struct aac_command **cmp);
449 extern void             aac_release_command(struct aac_command *cm);
450 extern int              aac_sync_fib(struct aac_softc *sc, u_int32_t command,
451                                      u_int32_t xferstate, struct aac_fib *fib,
452                                      u_int16_t datasize);
453 extern void             aac_add_event(struct aac_softc *sc, struct aac_event
454                                       *event);
455
456 /*
457  * Debugging levels:
458  *  0 - quiet, only emit warnings
459  *  1 - noisy, emit major function points and things done
460  *  2 - extremely noisy, emit trace items in loops, etc.
461  */
462 #ifdef AAC_DEBUG
463 # define debug(level, fmt, args...)                                     \
464         do {                                                            \
465         if (level <=AAC_DEBUG) kprintf("%s: " fmt "\n", __func__ , ##args); \
466         } while (0)
467 # define debug_called(level)                                            \
468         do {                                                            \
469         if (level <= AAC_DEBUG) kprintf("%s: called\n", __func__); \
470         } while (0)
471
472 extern void     aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
473                               const char *caller);
474 extern void     aac_print_aif(struct aac_softc *sc,
475                               struct aac_aif_command *aif);
476
477 # define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
478
479 #else
480 # define debug(level, fmt, args...)
481 # define debug_called(level)
482
483 # define AAC_PRINT_FIB(sc, fib)
484 # define aac_print_aif(sc, aac_aif_command)
485 #endif
486
487 struct aac_code_lookup {
488         char    *string;
489         u_int32_t       code;
490 };
491
492 /*
493  * Queue primitives for driver queues.
494  */
495 #define AACQ_ADD(sc, qname)                                     \
496         do {                                                    \
497                 struct aac_qstat *qs;                           \
498                                                                 \
499                 qs = &(sc)->aac_qstat[qname];                   \
500                                                                 \
501                 qs->q_length++;                                 \
502                 if (qs->q_length > qs->q_max)                   \
503                         qs->q_max = qs->q_length;               \
504         } while (0)
505
506 #define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
507 #define AACQ_INIT(sc, qname)                            \
508         do {                                            \
509                 sc->aac_qstat[qname].q_length = 0;      \
510                 sc->aac_qstat[qname].q_max = 0;         \
511         } while (0)
512
513
514 #define AACQ_COMMAND_QUEUE(name, index)                                 \
515 static __inline void                                                    \
516 aac_initq_ ## name (struct aac_softc *sc)                               \
517 {                                                                       \
518         TAILQ_INIT(&sc->aac_ ## name);                                  \
519         AACQ_INIT(sc, index);                                           \
520 }                                                                       \
521 static __inline void                                                    \
522 aac_enqueue_ ## name (struct aac_command *cm)                           \
523 {                                                                       \
524         if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {                   \
525                 kprintf("command %p is on another queue, flags = %#x\n",        \
526                        cm, cm->cm_flags);                               \
527                 panic("command is on another queue");                   \
528         }                                                               \
529         TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);       \
530         cm->cm_flags |= AAC_ON_ ## index;                               \
531         AACQ_ADD(cm->cm_sc, index);                                     \
532 }                                                                       \
533 static __inline void                                                    \
534 aac_requeue_ ## name (struct aac_command *cm)                           \
535 {                                                                       \
536         if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {                   \
537                 kprintf("command %p is on another queue, flags = %#x\n",        \
538                        cm, cm->cm_flags);                               \
539                 panic("command is on another queue");                   \
540         }                                                               \
541         TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);       \
542         cm->cm_flags |= AAC_ON_ ## index;                               \
543         AACQ_ADD(cm->cm_sc, index);                                     \
544 }                                                                       \
545 static __inline struct aac_command *                                    \
546 aac_dequeue_ ## name (struct aac_softc *sc)                             \
547 {                                                                       \
548         struct aac_command *cm;                                         \
549                                                                         \
550         if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {            \
551                 if ((cm->cm_flags & AAC_ON_ ## index) == 0) {           \
552                         kprintf("command %p not in queue, flags = %#x, "        \
553                                "bit = %#x\n", cm, cm->cm_flags,         \
554                                AAC_ON_ ## index);                       \
555                         panic("command not in queue");                  \
556                 }                                                       \
557                 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);           \
558                 cm->cm_flags &= ~AAC_ON_ ## index;                      \
559                 AACQ_REMOVE(sc, index);                                 \
560         }                                                               \
561         return(cm);                                                     \
562 }                                                                       \
563 static __inline void                                                    \
564 aac_remove_ ## name (struct aac_command *cm)                            \
565 {                                                                       \
566         if ((cm->cm_flags & AAC_ON_ ## index) == 0) {                   \
567                 kprintf("command %p not in queue, flags = %#x, "                \
568                        "bit = %#x\n", cm, cm->cm_flags,                 \
569                        AAC_ON_ ## index);                               \
570                 panic("command not in queue");                          \
571         }                                                               \
572         TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);            \
573         cm->cm_flags &= ~AAC_ON_ ## index;                              \
574         AACQ_REMOVE(cm->cm_sc, index);                                  \
575 }                                                                       \
576 struct hack
577
578 AACQ_COMMAND_QUEUE(free, AACQ_FREE);
579 AACQ_COMMAND_QUEUE(ready, AACQ_READY);
580 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
581 AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
582
583 /*
584  * outstanding bio queue
585  */
586 static __inline void
587 aac_initq_bio(struct aac_softc *sc)
588 {
589         bioq_init(&sc->aac_bioq);
590         AACQ_INIT(sc, AACQ_BIO);
591 }
592
593 static __inline void
594 aac_enqueue_bio(struct aac_softc *sc, struct bio *bio)
595 {
596         bioqdisksort(&sc->aac_bioq, bio);
597         AACQ_ADD(sc, AACQ_BIO);
598 }
599
600 static __inline struct bio *
601 aac_dequeue_bio(struct aac_softc *sc)
602 {
603         struct bio *bio;
604
605         if ((bio = bioq_first(&sc->aac_bioq)) != NULL) {
606                 bioq_remove(&sc->aac_bioq, bio);
607                 AACQ_REMOVE(sc, AACQ_BIO);
608         }
609         return(bio);
610 }
611
612 static __inline void
613 aac_print_printf(struct aac_softc *sc)
614 {
615         /*
616          * XXX We have the ability to read the length of the kprintf string
617          * from out of the mailboxes.
618          */
619         device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
620                       sc->aac_common->ac_printf);
621         sc->aac_common->ac_printf[0] = 0;
622         AAC_QNOTIFY(sc, AAC_DB_PRINTF);
623 }
624
625 static __inline int
626 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
627 {
628
629         KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
630         *fib = &sc->aac_common->ac_sync_fib;
631         return (0);
632 }
633
634 static __inline void
635 aac_release_sync_fib(struct aac_softc *sc)
636 {
637
638         KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
639 }
640