135863739SMike Smith /*- 235863739SMike Smith * Copyright (c) 2000 Michael Smith 3c6eafcf2SScott Long * Copyright (c) 2001 Scott Long 435863739SMike Smith * Copyright (c) 2000 BSDi 5c6eafcf2SScott Long * Copyright (c) 2001 Adaptec, Inc. 635863739SMike Smith * All rights reserved. 735863739SMike Smith * 835863739SMike Smith * Redistribution and use in source and binary forms, with or without 935863739SMike Smith * modification, are permitted provided that the following conditions 1035863739SMike Smith * are met: 1135863739SMike Smith * 1. Redistributions of source code must retain the above copyright 1235863739SMike Smith * notice, this list of conditions and the following disclaimer. 1335863739SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1435863739SMike Smith * notice, this list of conditions and the following disclaimer in the 1535863739SMike Smith * documentation and/or other materials provided with the distribution. 1635863739SMike Smith * 1735863739SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1835863739SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1935863739SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2035863739SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2135863739SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2235863739SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2335863739SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2435863739SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2535863739SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2635863739SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2735863739SMike Smith * SUCH DAMAGE. 2835863739SMike Smith * 2935863739SMike Smith * $FreeBSD$ 3035863739SMike Smith */ 3135863739SMike Smith 32914da7d0SScott Long /* 33914da7d0SScott Long * Driver Parameter Definitions 34914da7d0SScott Long */ 3535863739SMike Smith 3635863739SMike Smith /* 3735863739SMike Smith * The firmware interface allows for a 16-bit s/g list length. We limit 3835863739SMike Smith * ourselves to a reasonable maximum and ensure alignment. 3935863739SMike Smith */ 4035863739SMike Smith #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ 4135863739SMike Smith 4235863739SMike Smith /* 4335863739SMike Smith * We allocate a small set of FIBs for the adapter to use to send us messages. 4435863739SMike Smith */ 4535863739SMike Smith #define AAC_ADAPTER_FIBS 8 4635863739SMike Smith 4735863739SMike Smith /* 480b94a66eSMike Smith * FIBs are allocated up-front, and the pool isn't grown. We should allocate 490b94a66eSMike Smith * enough here to let us keep the adapter busy without wasting large amounts 500b94a66eSMike Smith * of kernel memory. The current interface implementation limits us to 512 510b94a66eSMike Smith * FIBs queued for the adapter at any one time. 5235863739SMike Smith */ 530b94a66eSMike Smith #define AAC_FIB_COUNT 128 5435863739SMike Smith 5535863739SMike Smith /* 56c6eafcf2SScott Long * The controller reports status events in AIFs. We hang on to a number of 57c6eafcf2SScott Long * these in order to pass them out to user-space management tools. 5835863739SMike Smith */ 5935863739SMike Smith #define AAC_AIFQ_LENGTH 64 6035863739SMike Smith 6135863739SMike Smith /* 6235863739SMike Smith * Firmware messages are passed in the printf buffer. 6335863739SMike Smith */ 6435863739SMike Smith #define AAC_PRINTF_BUFSIZE 256 6535863739SMike Smith 6635863739SMike Smith /* 67c6eafcf2SScott Long * We wait this many seconds for the adapter to come ready if it is still 68c6eafcf2SScott Long * booting 6935863739SMike Smith */ 7035863739SMike Smith #define AAC_BOOT_TIMEOUT (3 * 60) 7135863739SMike Smith 7235863739SMike Smith /* 7335863739SMike Smith * Timeout for immediate commands. 7435863739SMike Smith */ 750b94a66eSMike Smith #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ 760b94a66eSMike Smith 770b94a66eSMike Smith /* 780b94a66eSMike Smith * Timeout for normal commands 790b94a66eSMike Smith */ 800b94a66eSMike Smith #define AAC_CMD_TIMEOUT 30 /* seconds */ 810b94a66eSMike Smith 820b94a66eSMike Smith /* 830b94a66eSMike Smith * Rate at which we periodically check for timed out commands and kick the 840b94a66eSMike Smith * controller. 850b94a66eSMike Smith */ 8636e0bf6eSScott Long #define AAC_PERIODIC_INTERVAL 20 /* seconds */ 8735863739SMike Smith 8835863739SMike Smith /* 8935863739SMike Smith * Character device major numbers. 9035863739SMike Smith */ 9135863739SMike Smith #define AAC_DISK_MAJOR 200 9235863739SMike Smith 93914da7d0SScott Long /* 94914da7d0SScott Long * Driver Variable Definitions 95914da7d0SScott Long */ 9635863739SMike Smith 9735863739SMike Smith #if __FreeBSD_version >= 500005 9835863739SMike Smith # include <sys/taskqueue.h> 9935863739SMike Smith #endif 10035863739SMike Smith 10135863739SMike Smith /* 10235863739SMike Smith * Per-container data structure 10335863739SMike Smith */ 10435863739SMike Smith struct aac_container 10535863739SMike Smith { 10635863739SMike Smith struct aac_mntobj co_mntobj; 10735863739SMike Smith device_t co_disk; 10836e0bf6eSScott Long int co_found; 10936e0bf6eSScott Long TAILQ_ENTRY(aac_container) co_link; 11035863739SMike Smith }; 11135863739SMike Smith 11235863739SMike Smith /* 11370545d1aSScott Long * Per-SIM data structure 11470545d1aSScott Long */ 11570545d1aSScott Long struct aac_sim 11670545d1aSScott Long { 11770545d1aSScott Long device_t sim_dev; 11870545d1aSScott Long int TargetsPerBus; 11970545d1aSScott Long int BusNumber; 12070545d1aSScott Long int InitiatorBusId; 12170545d1aSScott Long struct aac_softc *aac_sc; 12270545d1aSScott Long TAILQ_ENTRY(aac_sim) sim_link; 12370545d1aSScott Long }; 12470545d1aSScott Long 12570545d1aSScott Long /* 12635863739SMike Smith * Per-disk structure 12735863739SMike Smith */ 12835863739SMike Smith struct aac_disk 12935863739SMike Smith { 13035863739SMike Smith device_t ad_dev; 13135863739SMike Smith dev_t ad_dev_t; 13235863739SMike Smith struct aac_softc *ad_controller; 13335863739SMike Smith struct aac_container *ad_container; 13435863739SMike Smith struct disk ad_disk; 13535863739SMike Smith struct devstat ad_stats; 13635863739SMike Smith int ad_flags; 13735863739SMike Smith #define AAC_DISK_OPEN (1<<0) 13835863739SMike Smith int ad_cylinders; 13935863739SMike Smith int ad_heads; 14035863739SMike Smith int ad_sectors; 14135863739SMike Smith u_int32_t ad_size; 14236e0bf6eSScott Long int unit; 14335863739SMike Smith }; 14435863739SMike Smith 14535863739SMike Smith /* 14635863739SMike Smith * Per-command control structure. 14735863739SMike Smith */ 14835863739SMike Smith struct aac_command 14935863739SMike Smith { 15035863739SMike Smith TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ 15135863739SMike Smith 15235863739SMike Smith struct aac_softc *cm_sc; /* controller that owns us */ 15335863739SMike Smith 154c6eafcf2SScott Long struct aac_fib *cm_fib; /* FIB associated with this 155c6eafcf2SScott Long * command */ 15635863739SMike Smith u_int32_t cm_fibphys; /* bus address of the FIB */ 157c6eafcf2SScott Long struct bio *cm_data; /* pointer to data in kernel 158c6eafcf2SScott Long * space */ 15935863739SMike Smith u_int32_t cm_datalen; /* data length */ 16035863739SMike Smith bus_dmamap_t cm_datamap; /* DMA map for bio data */ 161c6eafcf2SScott Long struct aac_sg_table *cm_sgtable; /* pointer to s/g table in 162c6eafcf2SScott Long * command */ 16335863739SMike Smith int cm_flags; 164c6eafcf2SScott Long #define AAC_CMD_MAPPED (1<<0) /* command has had its data 165c6eafcf2SScott Long * mapped */ 166c6eafcf2SScott Long #define AAC_CMD_DATAIN (1<<1) /* command involves data moving 167c6eafcf2SScott Long * from controller to host */ 168c6eafcf2SScott Long #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving 169c6eafcf2SScott Long * from host to controller */ 17035863739SMike Smith #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ 1710b94a66eSMike Smith #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ 172f6c4dd3fSScott Long #define AAC_ON_AACQ_FREE (1<<5) 173f6c4dd3fSScott Long #define AAC_ON_AACQ_READY (1<<6) 174f6c4dd3fSScott Long #define AAC_ON_AACQ_BUSY (1<<7) 175f6c4dd3fSScott Long #define AAC_ON_AACQ_COMPLETE (1<<8) 176f6c4dd3fSScott Long #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)) 17735863739SMike Smith 17835863739SMike Smith void (* cm_complete)(struct aac_command *cm); 17935863739SMike Smith void *cm_private; 1800b94a66eSMike Smith time_t cm_timestamp; /* command creation time */ 18136e0bf6eSScott Long int cm_queue; 18235863739SMike Smith }; 18335863739SMike Smith 18435863739SMike Smith /* 18535863739SMike Smith * We gather a number of adapter-visible items into a single structure. 18635863739SMike Smith * 18735863739SMike Smith * The ordering of this strucure may be important; we copy the Linux driver: 18835863739SMike Smith * 18935863739SMike Smith * Adapter FIBs 19035863739SMike Smith * Init struct 19135863739SMike Smith * Queue headers (Comm Area) 19235863739SMike Smith * Printf buffer 19335863739SMike Smith * 19435863739SMike Smith * In addition, we add: 19535863739SMike Smith * Sync Fib 19635863739SMike Smith */ 19735863739SMike Smith struct aac_common { 19835863739SMike Smith /* fibs for the controller to send us messages */ 19935863739SMike Smith struct aac_fib ac_fibs[AAC_ADAPTER_FIBS]; 20035863739SMike Smith 20135863739SMike Smith /* the init structure */ 20235863739SMike Smith struct aac_adapter_init ac_init; 20335863739SMike Smith 20435863739SMike Smith /* arena within which the queue structures are kept */ 205c6eafcf2SScott Long u_int8_t ac_qbuf[sizeof(struct aac_queue_table) + 206c6eafcf2SScott Long AAC_QUEUE_ALIGN]; 20735863739SMike Smith 20835863739SMike Smith /* buffer for text messages from the controller */ 20935863739SMike Smith char ac_printf[AAC_PRINTF_BUFSIZE]; 21035863739SMike Smith 21135863739SMike Smith /* fib for synchronous commands */ 21235863739SMike Smith struct aac_fib ac_sync_fib; 21335863739SMike Smith }; 21435863739SMike Smith 21535863739SMike Smith /* 21635863739SMike Smith * Interface operations 21735863739SMike Smith */ 21835863739SMike Smith struct aac_interface 21935863739SMike Smith { 22035863739SMike Smith int (*aif_get_fwstatus)(struct aac_softc *sc); 22135863739SMike Smith void (*aif_qnotify)(struct aac_softc *sc, int qbit); 22235863739SMike Smith int (*aif_get_istatus)(struct aac_softc *sc); 223b3457b51SScott Long void (*aif_clr_istatus)(struct aac_softc *sc, int mask); 22435863739SMike Smith void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command, 225c6eafcf2SScott Long u_int32_t arg0, u_int32_t arg1, 226c6eafcf2SScott Long u_int32_t arg2, u_int32_t arg3); 22735863739SMike Smith int (*aif_get_mailboxstatus)(struct aac_softc *sc); 22835863739SMike Smith void (*aif_set_interrupts)(struct aac_softc *sc, int enable); 22935863739SMike Smith }; 23035863739SMike Smith extern struct aac_interface aac_rx_interface; 23135863739SMike Smith extern struct aac_interface aac_sa_interface; 232b3457b51SScott Long extern struct aac_interface aac_fa_interface; 23335863739SMike Smith 23435863739SMike Smith #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc))) 23535863739SMike Smith #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit))) 23635863739SMike Smith #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc))) 237b3457b51SScott Long #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \ 238c6eafcf2SScott Long (mask))) 23935863739SMike Smith #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \ 240c6eafcf2SScott Long ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \ 241c6eafcf2SScott Long (arg3))) 242914da7d0SScott Long #define AAC_GET_MAILBOXSTATUS(sc) ((sc)->aac_if.aif_get_mailboxstatus( \ 243914da7d0SScott Long (sc))) 244914da7d0SScott Long #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 245914da7d0SScott Long 0)) 246914da7d0SScott Long #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 247914da7d0SScott Long 1)) 24835863739SMike Smith 249c6eafcf2SScott Long #define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \ 250c6eafcf2SScott Long sc->aac_bhandle, reg, val) 251c6eafcf2SScott Long #define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \ 252c6eafcf2SScott Long sc->aac_bhandle, reg) 253c6eafcf2SScott Long #define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \ 254c6eafcf2SScott Long sc->aac_bhandle, reg, val) 255c6eafcf2SScott Long #define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \ 256c6eafcf2SScott Long sc->aac_bhandle, reg) 257c6eafcf2SScott Long #define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \ 258c6eafcf2SScott Long sc->aac_bhandle, reg, val) 259c6eafcf2SScott Long #define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \ 260c6eafcf2SScott Long sc->aac_bhandle, reg) 26135863739SMike Smith 26236e0bf6eSScott Long /* Define the OS version specific locks */ 26336e0bf6eSScott Long #if __FreeBSD_version >= 500005 26436e0bf6eSScott Long #include <sys/lock.h> 26536e0bf6eSScott Long #include <sys/mutex.h> 26636e0bf6eSScott Long typedef struct mtx aac_lock_t; 2676008862bSJohn Baldwin #define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF) 268a1078af4SScott Long #define AAC_LOCK_ACQUIRE(l) mtx_lock(l) 26936e0bf6eSScott Long #define AAC_LOCK_RELEASE(l) mtx_unlock(l) 27036e0bf6eSScott Long #else 27136e0bf6eSScott Long typedef struct simplelock aac_lock_t; 272b3457b51SScott Long #define AAC_LOCK_INIT(l, s) simple_lock_init(l) 273a1078af4SScott Long #define AAC_LOCK_ACQUIRE(l) simple_lock(l) 27436e0bf6eSScott Long #define AAC_LOCK_RELEASE(l) simple_unlock(l) 27536e0bf6eSScott Long #endif 27636e0bf6eSScott Long 2771c4c2258SScott Long #if __FreeBSD_version >= 500005 278b3457b51SScott Long #include <sys/selinfo.h> 2791c4c2258SScott Long #else 2801c4c2258SScott Long #include <sys/select.h> 2811c4c2258SScott Long #endif 282b3457b51SScott Long 28335863739SMike Smith /* 28435863739SMike Smith * Per-controller structure. 28535863739SMike Smith */ 28635863739SMike Smith struct aac_softc 28735863739SMike Smith { 28835863739SMike Smith /* bus connections */ 28935863739SMike Smith device_t aac_dev; 290c6eafcf2SScott Long struct resource *aac_regs_resource; /* register interface 291c6eafcf2SScott Long * window */ 29235863739SMike Smith int aac_regs_rid; /* resource ID */ 29335863739SMike Smith bus_space_handle_t aac_bhandle; /* bus space handle */ 29435863739SMike Smith bus_space_tag_t aac_btag; /* bus space tag */ 29535863739SMike Smith bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ 296c6eafcf2SScott Long bus_dma_tag_t aac_buffer_dmat; /* data buffer/command 297c6eafcf2SScott Long * DMA tag */ 29835863739SMike Smith struct resource *aac_irq; /* interrupt */ 29935863739SMike Smith int aac_irq_rid; 30035863739SMike Smith void *aac_intr; /* interrupt handle */ 30135863739SMike Smith 30235863739SMike Smith /* controller features, limits and status */ 30335863739SMike Smith int aac_state; 30435863739SMike Smith #define AAC_STATE_SUSPEND (1<<0) 30535863739SMike Smith #define AAC_STATE_OPEN (1<<1) 30635863739SMike Smith #define AAC_STATE_INTERRUPTS_ON (1<<2) 30735863739SMike Smith #define AAC_STATE_AIF_SLEEPER (1<<3) 30835863739SMike Smith struct FsaRevision aac_revision; 30935863739SMike Smith 31035863739SMike Smith /* controller hardware interface */ 31135863739SMike Smith int aac_hwif; 31235863739SMike Smith #define AAC_HWIF_I960RX 0 31335863739SMike Smith #define AAC_HWIF_STRONGARM 1 314b3457b51SScott Long #define AAC_HWIF_FALCON 2 3150b94a66eSMike Smith #define AAC_HWIF_UNKNOWN -1 316c6eafcf2SScott Long bus_dma_tag_t aac_common_dmat; /* common structure 317c6eafcf2SScott Long * DMA tag */ 318c6eafcf2SScott Long bus_dmamap_t aac_common_dmamap; /* common structure 319c6eafcf2SScott Long * DMA map */ 32035863739SMike Smith struct aac_common *aac_common; 32135863739SMike Smith u_int32_t aac_common_busaddr; 32235863739SMike Smith struct aac_interface aac_if; 32335863739SMike Smith 32435863739SMike Smith /* command/fib resources */ 325c6eafcf2SScott Long bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ 3260b94a66eSMike Smith struct aac_fib *aac_fibs; 3270b94a66eSMike Smith bus_dmamap_t aac_fibmap; 3280b94a66eSMike Smith u_int32_t aac_fibphys; 3290b94a66eSMike Smith struct aac_command aac_command[AAC_FIB_COUNT]; 33035863739SMike Smith 33135863739SMike Smith /* command management */ 332c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_free; /* command structures 333c6eafcf2SScott Long * available for reuse */ 334c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for 335c6eafcf2SScott Long * controller resources */ 3360b94a66eSMike Smith TAILQ_HEAD(,aac_command) aac_busy; 337c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_complete; /* commands which have been 338c6eafcf2SScott Long * returned by the controller */ 33935863739SMike Smith struct bio_queue_head aac_bioq; 34035863739SMike Smith struct aac_queue_table *aac_queues; 34135863739SMike Smith struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; 34235863739SMike Smith 3430b94a66eSMike Smith struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ 3440b94a66eSMike Smith 34535863739SMike Smith /* connected containters */ 34670545d1aSScott Long TAILQ_HEAD(,aac_container) aac_container_tqh; 34736e0bf6eSScott Long aac_lock_t aac_container_lock; 34835863739SMike Smith 349cbfd045bSScott Long /* Protect the sync fib */ 350cbfd045bSScott Long #define AAC_SYNC_LOCK_FORCE (1 << 0) 351cbfd045bSScott Long aac_lock_t aac_sync_lock; 352cbfd045bSScott Long 35335863739SMike Smith /* delayed activity infrastructure */ 35435863739SMike Smith #if __FreeBSD_version >= 500005 355c6eafcf2SScott Long struct task aac_task_complete; /* deferred-completion 356c6eafcf2SScott Long * task */ 35735863739SMike Smith #endif 35835863739SMike Smith struct intr_config_hook aac_ich; 35935863739SMike Smith 36035863739SMike Smith /* management interface */ 36135863739SMike Smith dev_t aac_dev_t; 362b3457b51SScott Long aac_lock_t aac_aifq_lock; 36335863739SMike Smith struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH]; 36435863739SMike Smith int aac_aifq_head; 36535863739SMike Smith int aac_aifq_tail; 366b3457b51SScott Long struct selinfo rcv_select; 36736e0bf6eSScott Long struct proc *aifthread; 36836e0bf6eSScott Long int aifflags; 36936e0bf6eSScott Long #define AAC_AIFFLAGS_RUNNING (1 << 0) 37070545d1aSScott Long #define AAC_AIFFLAGS_AIF (1 << 1) 37136e0bf6eSScott Long #define AAC_AIFFLAGS_EXIT (1 << 2) 37236e0bf6eSScott Long #define AAC_AIFFLAGS_EXITED (1 << 3) 37370545d1aSScott Long #define AAC_AIFFLAGS_PRINTF (1 << 4) 37470545d1aSScott Long #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF) 375fe94b852SScott Long u_int32_t quirks; 376fe94b852SScott Long #define AAC_QUIRK_PERC2QC (1 << 0) 377fe3cb0e1SScott Long #define AAC_QUIRK_NOCAM (1 << 1) /* No SCSI passthrough */ 378fe3cb0e1SScott Long #define AAC_QUIRK_CAM_NORESET (1 << 2) /* Fake SCSI resets */ 379fe3cb0e1SScott Long #define AAC_QUIRK_CAM_PASSONLY (1 << 3) /* Only create pass devices */ 380fe3cb0e1SScott Long 381fe3cb0e1SScott Long u_int32_t scsi_method_id; 38270545d1aSScott Long TAILQ_HEAD(,aac_sim) aac_sim_tqh; 38335863739SMike Smith }; 38435863739SMike Smith 38535863739SMike Smith 38635863739SMike Smith /* 38735863739SMike Smith * Public functions 38835863739SMike Smith */ 38935863739SMike Smith extern void aac_free(struct aac_softc *sc); 39035863739SMike Smith extern int aac_attach(struct aac_softc *sc); 39135863739SMike Smith extern int aac_detach(device_t dev); 39235863739SMike Smith extern int aac_shutdown(device_t dev); 39335863739SMike Smith extern int aac_suspend(device_t dev); 39435863739SMike Smith extern int aac_resume(device_t dev); 39535863739SMike Smith extern void aac_intr(void *arg); 39635863739SMike Smith extern void aac_submit_bio(struct bio *bp); 3970b94a66eSMike Smith extern void aac_biodone(struct bio *bp); 398fe3cb0e1SScott Long extern void aac_startio(struct aac_softc *sc); 399fe3cb0e1SScott Long extern int aac_alloc_command(struct aac_softc *sc, 400fe3cb0e1SScott Long struct aac_command **cmp); 401fe3cb0e1SScott Long extern void aac_release_command(struct aac_command *cm); 402fe3cb0e1SScott Long extern int aac_alloc_sync_fib(struct aac_softc *sc, 403cbfd045bSScott Long struct aac_fib **fib, int flags); 404cbfd045bSScott Long extern void aac_release_sync_fib(struct aac_softc *sc); 405cbfd045bSScott Long extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, 406cbfd045bSScott Long u_int32_t xferstate, struct aac_fib *fib, 407cbfd045bSScott Long u_int16_t datasize); 40835863739SMike Smith 40935863739SMike Smith /* 41035863739SMike Smith * Debugging levels: 41135863739SMike Smith * 0 - quiet, only emit warnings 41235863739SMike Smith * 1 - noisy, emit major function points and things done 41335863739SMike Smith * 2 - extremely noisy, emit trace items in loops, etc. 41435863739SMike Smith */ 41535863739SMike Smith #ifdef AAC_DEBUG 4160b94a66eSMike Smith # define debug(level, fmt, args...) \ 4170b94a66eSMike Smith do { \ 4186e551fb6SDavid E. O'Brien if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ 4190b94a66eSMike Smith } while (0) 4200b94a66eSMike Smith # define debug_called(level) \ 4210b94a66eSMike Smith do { \ 422956d569bSDavid E. O'Brien if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \ 4230b94a66eSMike Smith } while (0) 42435863739SMike Smith 42535863739SMike Smith extern void aac_print_queues(struct aac_softc *sc); 42635863739SMike Smith extern void aac_panic(struct aac_softc *sc, char *reason); 427c6eafcf2SScott Long extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, 42870148712SPeter Wemm const char *caller); 429c6eafcf2SScott Long extern void aac_print_aif(struct aac_softc *sc, 430c6eafcf2SScott Long struct aac_aif_command *aif); 43135863739SMike Smith 4326e551fb6SDavid E. O'Brien #define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) 43335863739SMike Smith 43435863739SMike Smith #else 43535863739SMike Smith # define debug(level, fmt, args...) 43635863739SMike Smith # define debug_called(level) 43735863739SMike Smith 43835863739SMike Smith # define aac_print_queues(sc) 43935863739SMike Smith # define aac_panic(sc, reason) 44035863739SMike Smith 44135863739SMike Smith # define AAC_PRINT_FIB(sc, fib) 44236e0bf6eSScott Long # define aac_print_aif(sc, aac_aif_command) 44335863739SMike Smith #endif 44435863739SMike Smith 44535863739SMike Smith struct aac_code_lookup { 44635863739SMike Smith char *string; 44735863739SMike Smith u_int32_t code; 44835863739SMike Smith }; 44935863739SMike Smith 450914da7d0SScott Long /* 4510b94a66eSMike Smith * Queue primitives for driver queues. 45235863739SMike Smith */ 4530b94a66eSMike Smith #define AACQ_ADD(sc, qname) \ 4540b94a66eSMike Smith do { \ 455914da7d0SScott Long struct aac_qstat *qs; \ 456914da7d0SScott Long \ 457914da7d0SScott Long qs = &(sc)->aac_qstat[qname]; \ 4580b94a66eSMike Smith \ 4590b94a66eSMike Smith qs->q_length++; \ 4600b94a66eSMike Smith if (qs->q_length > qs->q_max) \ 4610b94a66eSMike Smith qs->q_max = qs->q_length; \ 4620b94a66eSMike Smith } while (0) 46335863739SMike Smith 4640b94a66eSMike Smith #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- 4650b94a66eSMike Smith #define AACQ_INIT(sc, qname) \ 4660b94a66eSMike Smith do { \ 4670b94a66eSMike Smith sc->aac_qstat[qname].q_length = 0; \ 4680b94a66eSMike Smith sc->aac_qstat[qname].q_max = 0; \ 4690b94a66eSMike Smith } while (0) 4700b94a66eSMike Smith 4710b94a66eSMike Smith 4720b94a66eSMike Smith #define AACQ_COMMAND_QUEUE(name, index) \ 4730b94a66eSMike Smith static __inline void \ 4740b94a66eSMike Smith aac_initq_ ## name (struct aac_softc *sc) \ 4750b94a66eSMike Smith { \ 4760b94a66eSMike Smith TAILQ_INIT(&sc->aac_ ## name); \ 4770b94a66eSMike Smith AACQ_INIT(sc, index); \ 4780b94a66eSMike Smith } \ 4790b94a66eSMike Smith static __inline void \ 4800b94a66eSMike Smith aac_enqueue_ ## name (struct aac_command *cm) \ 4810b94a66eSMike Smith { \ 4820b94a66eSMike Smith int s; \ 4830b94a66eSMike Smith \ 4840b94a66eSMike Smith s = splbio(); \ 485f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 486f6c4dd3fSScott Long printf("command %p is on another queue, flags = %#x\n", \ 487f6c4dd3fSScott Long cm, cm->cm_flags); \ 488f6c4dd3fSScott Long panic("command is on another queue"); \ 489f6c4dd3fSScott Long } \ 4900b94a66eSMike Smith TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 491f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 4920b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 4930b94a66eSMike Smith splx(s); \ 4940b94a66eSMike Smith } \ 4950b94a66eSMike Smith static __inline void \ 4960b94a66eSMike Smith aac_requeue_ ## name (struct aac_command *cm) \ 4970b94a66eSMike Smith { \ 4980b94a66eSMike Smith int s; \ 4990b94a66eSMike Smith \ 5000b94a66eSMike Smith s = splbio(); \ 501f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 502f6c4dd3fSScott Long printf("command %p is on another queue, flags = %#x\n", \ 503f6c4dd3fSScott Long cm, cm->cm_flags); \ 504f6c4dd3fSScott Long panic("command is on another queue"); \ 505f6c4dd3fSScott Long } \ 5060b94a66eSMike Smith TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 507f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 5080b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 5090b94a66eSMike Smith splx(s); \ 5100b94a66eSMike Smith } \ 5110b94a66eSMike Smith static __inline struct aac_command * \ 5120b94a66eSMike Smith aac_dequeue_ ## name (struct aac_softc *sc) \ 5130b94a66eSMike Smith { \ 5140b94a66eSMike Smith struct aac_command *cm; \ 5150b94a66eSMike Smith int s; \ 5160b94a66eSMike Smith \ 5170b94a66eSMike Smith s = splbio(); \ 5180b94a66eSMike Smith if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ 519f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 520914da7d0SScott Long printf("command %p not in queue, flags = %#x, " \ 521914da7d0SScott Long "bit = %#x\n", cm, cm->cm_flags, \ 522914da7d0SScott Long AAC_ON_ ## index); \ 523f6c4dd3fSScott Long panic("command not in queue"); \ 524f6c4dd3fSScott Long } \ 5250b94a66eSMike Smith TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ 526f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5270b94a66eSMike Smith AACQ_REMOVE(sc, index); \ 5280b94a66eSMike Smith } \ 5290b94a66eSMike Smith splx(s); \ 5300b94a66eSMike Smith return(cm); \ 5310b94a66eSMike Smith } \ 5320b94a66eSMike Smith static __inline void \ 5330b94a66eSMike Smith aac_remove_ ## name (struct aac_command *cm) \ 5340b94a66eSMike Smith { \ 5350b94a66eSMike Smith int s; \ 5360b94a66eSMike Smith \ 5370b94a66eSMike Smith s = splbio(); \ 538f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 539914da7d0SScott Long printf("command %p not in queue, flags = %#x, " \ 540914da7d0SScott Long "bit = %#x\n", cm, cm->cm_flags, \ 541914da7d0SScott Long AAC_ON_ ## index); \ 542f6c4dd3fSScott Long panic("command not in queue"); \ 543f6c4dd3fSScott Long } \ 5440b94a66eSMike Smith TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 545f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5460b94a66eSMike Smith AACQ_REMOVE(cm->cm_sc, index); \ 5470b94a66eSMike Smith splx(s); \ 5480b94a66eSMike Smith } \ 5490b94a66eSMike Smith struct hack 5500b94a66eSMike Smith 5510b94a66eSMike Smith AACQ_COMMAND_QUEUE(free, AACQ_FREE); 5520b94a66eSMike Smith AACQ_COMMAND_QUEUE(ready, AACQ_READY); 5530b94a66eSMike Smith AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); 5540b94a66eSMike Smith AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE); 5550b94a66eSMike Smith 5560b94a66eSMike Smith /* 5570b94a66eSMike Smith * outstanding bio queue 5580b94a66eSMike Smith */ 55935863739SMike Smith static __inline void 5600b94a66eSMike Smith aac_initq_bio(struct aac_softc *sc) 56135863739SMike Smith { 5620b94a66eSMike Smith bioq_init(&sc->aac_bioq); 5630b94a66eSMike Smith AACQ_INIT(sc, AACQ_BIO); 56435863739SMike Smith } 56535863739SMike Smith 56635863739SMike Smith static __inline void 5670b94a66eSMike Smith aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) 56835863739SMike Smith { 56935863739SMike Smith int s; 57035863739SMike Smith 57135863739SMike Smith s = splbio(); 5720b94a66eSMike Smith bioq_insert_tail(&sc->aac_bioq, bp); 5730b94a66eSMike Smith AACQ_ADD(sc, AACQ_BIO); 57435863739SMike Smith splx(s); 57535863739SMike Smith } 57635863739SMike Smith 5770b94a66eSMike Smith static __inline struct bio * 5780b94a66eSMike Smith aac_dequeue_bio(struct aac_softc *sc) 57935863739SMike Smith { 58035863739SMike Smith int s; 5810b94a66eSMike Smith struct bio *bp; 58235863739SMike Smith 58335863739SMike Smith s = splbio(); 5840b94a66eSMike Smith if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { 5850b94a66eSMike Smith bioq_remove(&sc->aac_bioq, bp); 5860b94a66eSMike Smith AACQ_REMOVE(sc, AACQ_BIO); 58735863739SMike Smith } 58835863739SMike Smith splx(s); 5890b94a66eSMike Smith return(bp); 59035863739SMike Smith } 59136e0bf6eSScott Long 59236e0bf6eSScott Long static __inline void 59336e0bf6eSScott Long aac_print_printf(struct aac_softc *sc) 59436e0bf6eSScott Long { 595b3457b51SScott Long /* 596b3457b51SScott Long * XXX We have the ability to read the length of the printf string 597b3457b51SScott Long * from out of the mailboxes. 598b3457b51SScott Long */ 59936e0bf6eSScott Long device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, 60036e0bf6eSScott Long sc->aac_common->ac_printf); 60136e0bf6eSScott Long AAC_QNOTIFY(sc, AAC_DB_PRINTF); 60236e0bf6eSScott Long } 603