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 329e2e96d8SScott Long #include <sys/bio.h> 339e2e96d8SScott Long #include <sys/lock.h> 349e2e96d8SScott Long #include <sys/mutex.h> 359e2e96d8SScott Long #include <sys/taskqueue.h> 369e2e96d8SScott Long #include <sys/selinfo.h> 37891619a6SPoul-Henning Kamp #include <geom/geom_disk.h> 38891619a6SPoul-Henning Kamp 397cb209f5SScott Long #ifndef AAC_DRIVER_BUILD 407cb209f5SScott Long # define AAC_DRIVER_BUILD 1 417cb209f5SScott Long #endif 429e2e96d8SScott Long 43914da7d0SScott Long /* 44914da7d0SScott Long * Driver Parameter Definitions 45914da7d0SScott Long */ 4635863739SMike Smith 4735863739SMike Smith /* 4835863739SMike Smith * The firmware interface allows for a 16-bit s/g list length. We limit 4935863739SMike Smith * ourselves to a reasonable maximum and ensure alignment. 5035863739SMike Smith */ 5135863739SMike Smith #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ 5235863739SMike Smith 5335863739SMike Smith /* 5435863739SMike Smith * We allocate a small set of FIBs for the adapter to use to send us messages. 5535863739SMike Smith */ 5635863739SMike Smith #define AAC_ADAPTER_FIBS 8 5735863739SMike Smith 5835863739SMike Smith /* 59ffb37f33SScott Long * FIBs are allocated in page-size chunks and can grow up to the 512 60ffb37f33SScott Long * limit imposed by the hardware. 6135863739SMike Smith */ 628480cc63SScott Long #define AAC_PREALLOCATE_FIBS 128 637cb209f5SScott Long #define AAC_NUM_MGT_FIB 8 6435863739SMike Smith 6535863739SMike Smith /* 66c6eafcf2SScott Long * The controller reports status events in AIFs. We hang on to a number of 67c6eafcf2SScott Long * these in order to pass them out to user-space management tools. 6835863739SMike Smith */ 6935863739SMike Smith #define AAC_AIFQ_LENGTH 64 7035863739SMike Smith 7135863739SMike Smith /* 7235863739SMike Smith * Firmware messages are passed in the printf buffer. 7335863739SMike Smith */ 7435863739SMike Smith #define AAC_PRINTF_BUFSIZE 256 7535863739SMike Smith 7635863739SMike Smith /* 77c6eafcf2SScott Long * We wait this many seconds for the adapter to come ready if it is still 78c6eafcf2SScott Long * booting 7935863739SMike Smith */ 8035863739SMike Smith #define AAC_BOOT_TIMEOUT (3 * 60) 8135863739SMike Smith 8235863739SMike Smith /* 8335863739SMike Smith * Timeout for immediate commands. 8435863739SMike Smith */ 850b94a66eSMike Smith #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ 860b94a66eSMike Smith 870b94a66eSMike Smith /* 880b94a66eSMike Smith * Timeout for normal commands 890b94a66eSMike Smith */ 900b94a66eSMike Smith #define AAC_CMD_TIMEOUT 30 /* seconds */ 910b94a66eSMike Smith 920b94a66eSMike Smith /* 930b94a66eSMike Smith * Rate at which we periodically check for timed out commands and kick the 940b94a66eSMike Smith * controller. 950b94a66eSMike Smith */ 9636e0bf6eSScott Long #define AAC_PERIODIC_INTERVAL 20 /* seconds */ 9735863739SMike Smith 9835863739SMike Smith /* 9935863739SMike Smith * Per-container data structure 10035863739SMike Smith */ 10135863739SMike Smith struct aac_container 10235863739SMike Smith { 10335863739SMike Smith struct aac_mntobj co_mntobj; 10435863739SMike Smith device_t co_disk; 10536e0bf6eSScott Long int co_found; 10636e0bf6eSScott Long TAILQ_ENTRY(aac_container) co_link; 10735863739SMike Smith }; 10835863739SMike Smith 10935863739SMike Smith /* 11070545d1aSScott Long * Per-SIM data structure 11170545d1aSScott Long */ 11270545d1aSScott Long struct aac_sim 11370545d1aSScott Long { 11470545d1aSScott Long device_t sim_dev; 11570545d1aSScott Long int TargetsPerBus; 11670545d1aSScott Long int BusNumber; 11770545d1aSScott Long int InitiatorBusId; 11870545d1aSScott Long struct aac_softc *aac_sc; 11970545d1aSScott Long TAILQ_ENTRY(aac_sim) sim_link; 12070545d1aSScott Long }; 12170545d1aSScott Long 12270545d1aSScott Long /* 12335863739SMike Smith * Per-disk structure 12435863739SMike Smith */ 12535863739SMike Smith struct aac_disk 12635863739SMike Smith { 12735863739SMike Smith device_t ad_dev; 12835863739SMike Smith struct aac_softc *ad_controller; 12935863739SMike Smith struct aac_container *ad_container; 1300b7ed341SPoul-Henning Kamp struct disk *ad_disk; 13135863739SMike Smith int ad_flags; 13235863739SMike Smith #define AAC_DISK_OPEN (1<<0) 13335863739SMike Smith int ad_cylinders; 13435863739SMike Smith int ad_heads; 13535863739SMike Smith int ad_sectors; 13635863739SMike Smith u_int32_t ad_size; 13736e0bf6eSScott Long int unit; 13835863739SMike Smith }; 13935863739SMike Smith 14035863739SMike Smith /* 14135863739SMike Smith * Per-command control structure. 14235863739SMike Smith */ 14335863739SMike Smith struct aac_command 14435863739SMike Smith { 14535863739SMike Smith TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ 14635863739SMike Smith 14735863739SMike Smith struct aac_softc *cm_sc; /* controller that owns us */ 14835863739SMike Smith 149c6eafcf2SScott Long struct aac_fib *cm_fib; /* FIB associated with this 150c6eafcf2SScott Long * command */ 1517cb209f5SScott Long u_int64_t cm_fibphys; /* bus address of the FIB */ 152c6eafcf2SScott Long struct bio *cm_data; /* pointer to data in kernel 153c6eafcf2SScott Long * space */ 15435863739SMike Smith u_int32_t cm_datalen; /* data length */ 15535863739SMike Smith bus_dmamap_t cm_datamap; /* DMA map for bio data */ 156c6eafcf2SScott Long struct aac_sg_table *cm_sgtable; /* pointer to s/g table in 157c6eafcf2SScott Long * command */ 15835863739SMike Smith int cm_flags; 159c6eafcf2SScott Long #define AAC_CMD_MAPPED (1<<0) /* command has had its data 160c6eafcf2SScott Long * mapped */ 161c6eafcf2SScott Long #define AAC_CMD_DATAIN (1<<1) /* command involves data moving 162c6eafcf2SScott Long * from controller to host */ 163c6eafcf2SScott Long #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving 164c6eafcf2SScott Long * from host to controller */ 16535863739SMike Smith #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ 1660b94a66eSMike Smith #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ 167f6c4dd3fSScott Long #define AAC_ON_AACQ_FREE (1<<5) 168f6c4dd3fSScott Long #define AAC_ON_AACQ_READY (1<<6) 169f6c4dd3fSScott Long #define AAC_ON_AACQ_BUSY (1<<7) 1707cb209f5SScott Long #define AAC_ON_AACQ_AIF (1<<8) 1717cb209f5SScott Long #define AAC_ON_AACQ_NORM (1<<10) 1727cb209f5SScott Long #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10)) 173cd481291SScott Long #define AAC_QUEUE_FRZN (1<<9) /* Freeze the processing of 174cd481291SScott Long * commands on the queue. */ 17535863739SMike Smith 17635863739SMike Smith void (* cm_complete)(struct aac_command *cm); 17735863739SMike Smith void *cm_private; 1780b94a66eSMike Smith time_t cm_timestamp; /* command creation time */ 17936e0bf6eSScott Long int cm_queue; 180cb0d64b9SScott Long int cm_index; 18135863739SMike Smith }; 18235863739SMike Smith 183ffb37f33SScott Long struct aac_fibmap { 184ffb37f33SScott Long TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */ 185ffb37f33SScott Long struct aac_fib *aac_fibs; 186ffb37f33SScott Long bus_dmamap_t aac_fibmap; 187ffb37f33SScott Long struct aac_command *aac_commands; 188ffb37f33SScott Long }; 189ffb37f33SScott Long 19035863739SMike Smith /* 19135863739SMike Smith * We gather a number of adapter-visible items into a single structure. 19235863739SMike Smith * 19335863739SMike Smith * The ordering of this strucure may be important; we copy the Linux driver: 19435863739SMike Smith * 19535863739SMike Smith * Adapter FIBs 19635863739SMike Smith * Init struct 19735863739SMike Smith * Queue headers (Comm Area) 19835863739SMike Smith * Printf buffer 19935863739SMike Smith * 20035863739SMike Smith * In addition, we add: 20135863739SMike Smith * Sync Fib 20235863739SMike Smith */ 20335863739SMike Smith struct aac_common { 20435863739SMike Smith /* fibs for the controller to send us messages */ 20535863739SMike Smith struct aac_fib ac_fibs[AAC_ADAPTER_FIBS]; 20635863739SMike Smith 20735863739SMike Smith /* the init structure */ 20835863739SMike Smith struct aac_adapter_init ac_init; 20935863739SMike Smith 21035863739SMike Smith /* arena within which the queue structures are kept */ 211c6eafcf2SScott Long u_int8_t ac_qbuf[sizeof(struct aac_queue_table) + 212c6eafcf2SScott Long AAC_QUEUE_ALIGN]; 21335863739SMike Smith 21435863739SMike Smith /* buffer for text messages from the controller */ 21535863739SMike Smith char ac_printf[AAC_PRINTF_BUFSIZE]; 21635863739SMike Smith 21735863739SMike Smith /* fib for synchronous commands */ 21835863739SMike Smith struct aac_fib ac_sync_fib; 21935863739SMike Smith }; 22035863739SMike Smith 22135863739SMike Smith /* 22235863739SMike Smith * Interface operations 22335863739SMike Smith */ 22435863739SMike Smith struct aac_interface 22535863739SMike Smith { 22635863739SMike Smith int (*aif_get_fwstatus)(struct aac_softc *sc); 22735863739SMike Smith void (*aif_qnotify)(struct aac_softc *sc, int qbit); 22835863739SMike Smith int (*aif_get_istatus)(struct aac_softc *sc); 229b3457b51SScott Long void (*aif_clr_istatus)(struct aac_softc *sc, int mask); 23035863739SMike Smith void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command, 231c6eafcf2SScott Long u_int32_t arg0, u_int32_t arg1, 232c6eafcf2SScott Long u_int32_t arg2, u_int32_t arg3); 233a6d35632SScott Long int (*aif_get_mailbox)(struct aac_softc *sc, int mb); 23435863739SMike Smith void (*aif_set_interrupts)(struct aac_softc *sc, int enable); 2357cb209f5SScott Long int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm); 2367cb209f5SScott Long int (*aif_get_outb_queue)(struct aac_softc *sc); 2377cb209f5SScott Long void (*aif_set_outb_queue)(struct aac_softc *sc, int index); 23835863739SMike Smith }; 23935863739SMike Smith extern struct aac_interface aac_rx_interface; 24035863739SMike Smith extern struct aac_interface aac_sa_interface; 241b3457b51SScott Long extern struct aac_interface aac_fa_interface; 2424afedc31SScott Long extern struct aac_interface aac_rkt_interface; 24335863739SMike Smith 24435863739SMike Smith #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc))) 24535863739SMike Smith #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit))) 24635863739SMike Smith #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc))) 247b3457b51SScott Long #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \ 248c6eafcf2SScott Long (mask))) 24935863739SMike Smith #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \ 250c6eafcf2SScott Long ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \ 251c6eafcf2SScott Long (arg3))) 252a6d35632SScott Long #define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \ 253a6d35632SScott Long (mb))) 254914da7d0SScott Long #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 255914da7d0SScott Long 0)) 256914da7d0SScott Long #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 257914da7d0SScott Long 1)) 2587cb209f5SScott Long #define AAC_SEND_COMMAND(sc, cm) ((sc)->aac_if.aif_send_command((sc), (cm))) 2597cb209f5SScott Long #define AAC_GET_OUTB_QUEUE(sc) ((sc)->aac_if.aif_get_outb_queue((sc))) 2607cb209f5SScott Long #define AAC_SET_OUTB_QUEUE(sc, idx) ((sc)->aac_if.aif_set_outb_queue((sc), (idx))) 26135863739SMike Smith 262c6eafcf2SScott Long #define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \ 263c6eafcf2SScott Long sc->aac_bhandle, reg, val) 264c6eafcf2SScott Long #define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \ 265c6eafcf2SScott Long sc->aac_bhandle, reg) 266c6eafcf2SScott Long #define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \ 267c6eafcf2SScott Long sc->aac_bhandle, reg, val) 268c6eafcf2SScott Long #define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \ 269c6eafcf2SScott Long sc->aac_bhandle, reg) 270c6eafcf2SScott Long #define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \ 271c6eafcf2SScott Long sc->aac_bhandle, reg, val) 272c6eafcf2SScott Long #define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \ 273c6eafcf2SScott Long sc->aac_bhandle, reg) 27435863739SMike Smith 27535863739SMike Smith /* 27635863739SMike Smith * Per-controller structure. 27735863739SMike Smith */ 27835863739SMike Smith struct aac_softc 27935863739SMike Smith { 28035863739SMike Smith /* bus connections */ 28135863739SMike Smith device_t aac_dev; 282c6eafcf2SScott Long struct resource *aac_regs_resource; /* register interface 283c6eafcf2SScott Long * window */ 28435863739SMike Smith int aac_regs_rid; /* resource ID */ 28535863739SMike Smith bus_space_handle_t aac_bhandle; /* bus space handle */ 28635863739SMike Smith bus_space_tag_t aac_btag; /* bus space tag */ 28735863739SMike Smith bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ 288c6eafcf2SScott Long bus_dma_tag_t aac_buffer_dmat; /* data buffer/command 289c6eafcf2SScott Long * DMA tag */ 29035863739SMike Smith struct resource *aac_irq; /* interrupt */ 29135863739SMike Smith int aac_irq_rid; 29235863739SMike Smith void *aac_intr; /* interrupt handle */ 2935f54d522SScott Long eventhandler_tag eh; 29435863739SMike Smith 29535863739SMike Smith /* controller features, limits and status */ 29635863739SMike Smith int aac_state; 29735863739SMike Smith #define AAC_STATE_SUSPEND (1<<0) 29835863739SMike Smith #define AAC_STATE_OPEN (1<<1) 29935863739SMike Smith #define AAC_STATE_INTERRUPTS_ON (1<<2) 30035863739SMike Smith #define AAC_STATE_AIF_SLEEPER (1<<3) 30135863739SMike Smith struct FsaRevision aac_revision; 30235863739SMike Smith 30335863739SMike Smith /* controller hardware interface */ 30435863739SMike Smith int aac_hwif; 30535863739SMike Smith #define AAC_HWIF_I960RX 0 30635863739SMike Smith #define AAC_HWIF_STRONGARM 1 307b3457b51SScott Long #define AAC_HWIF_FALCON 2 3084afedc31SScott Long #define AAC_HWIF_RKT 3 3090b94a66eSMike Smith #define AAC_HWIF_UNKNOWN -1 310c6eafcf2SScott Long bus_dma_tag_t aac_common_dmat; /* common structure 311c6eafcf2SScott Long * DMA tag */ 312c6eafcf2SScott Long bus_dmamap_t aac_common_dmamap; /* common structure 313c6eafcf2SScott Long * DMA map */ 31435863739SMike Smith struct aac_common *aac_common; 31535863739SMike Smith u_int32_t aac_common_busaddr; 31635863739SMike Smith struct aac_interface aac_if; 31735863739SMike Smith 31835863739SMike Smith /* command/fib resources */ 319c6eafcf2SScott Long bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ 320ffb37f33SScott Long TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh; 3218b149b51SJohn Baldwin u_int total_fibs; 322ffb37f33SScott Long struct aac_command *aac_commands; 32335863739SMike Smith 32435863739SMike Smith /* command management */ 325c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_free; /* command structures 326c6eafcf2SScott Long * available for reuse */ 327c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for 328c6eafcf2SScott Long * controller resources */ 3290b94a66eSMike Smith TAILQ_HEAD(,aac_command) aac_busy; 3307cb209f5SScott Long TAILQ_HEAD(,aac_command) aac_aif; 3317cb209f5SScott Long #if 0 3327cb209f5SScott Long TAILQ_HEAD(,aac_command) aac_norm; 3337cb209f5SScott Long #endif 3347cb209f5SScott Long TAILQ_HEAD(,aac_event) aac_ev_cmfree; 33535863739SMike Smith struct bio_queue_head aac_bioq; 33635863739SMike Smith struct aac_queue_table *aac_queues; 33735863739SMike Smith struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; 33835863739SMike Smith 3390b94a66eSMike Smith struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ 3400b94a66eSMike Smith 34135863739SMike Smith /* connected containters */ 34270545d1aSScott Long TAILQ_HEAD(,aac_container) aac_container_tqh; 343bb6fe253SScott Long struct mtx aac_container_lock; 34435863739SMike Smith 34503b5fe51SScott Long /* 34603b5fe51SScott Long * The general I/O lock. This protects the sync fib, the lists, the 34703b5fe51SScott Long * queues, and the registers. 34803b5fe51SScott Long */ 349bb6fe253SScott Long struct mtx aac_io_lock; 350ae543596SScott Long 35135863739SMike Smith /* delayed activity infrastructure */ 352c6eafcf2SScott Long struct task aac_task_complete; /* deferred-completion 353c6eafcf2SScott Long * task */ 35435863739SMike Smith struct intr_config_hook aac_ich; 35535863739SMike Smith 35635863739SMike Smith /* management interface */ 35789c9c53dSPoul-Henning Kamp struct cdev *aac_dev_t; 358bb6fe253SScott Long struct mtx aac_aifq_lock; 35935863739SMike Smith struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH]; 36035863739SMike Smith int aac_aifq_head; 36135863739SMike Smith int aac_aifq_tail; 362b3457b51SScott Long struct selinfo rcv_select; 36336e0bf6eSScott Long struct proc *aifthread; 36436e0bf6eSScott Long int aifflags; 36536e0bf6eSScott Long #define AAC_AIFFLAGS_RUNNING (1 << 0) 36670545d1aSScott Long #define AAC_AIFFLAGS_AIF (1 << 1) 36736e0bf6eSScott Long #define AAC_AIFFLAGS_EXIT (1 << 2) 36836e0bf6eSScott Long #define AAC_AIFFLAGS_EXITED (1 << 3) 36970545d1aSScott Long #define AAC_AIFFLAGS_PRINTF (1 << 4) 370ae543596SScott Long #define AAC_AIFFLAGS_ALLOCFIBS (1 << 5) 371ae543596SScott Long #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \ 372ae543596SScott Long AAC_AIFFLAGS_ALLOCFIBS) 373a6d35632SScott Long u_int32_t flags; 374a6d35632SScott Long #define AAC_FLAGS_PERC2QC (1 << 0) 375a6d35632SScott Long #define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */ 376a6d35632SScott Long #define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */ 377a6d35632SScott Long #define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */ 378a6d35632SScott Long #define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */ 379a6d35632SScott Long #define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem 380a6d35632SScott Long * 2GB-4GB range */ 381a6d35632SScott Long #define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */ 382a6d35632SScott Long #define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */ 3834b00f859SScott Long #define AAC_FLAGS_BROKEN_MEMMAP (1 << 8) /* Broken HostPhysMemPages */ 3847cb209f5SScott Long #define AAC_FLAGS_SLAVE (1 << 9) 3857cb209f5SScott Long #define AAC_FLAGS_MASTER (1 << 10) 3867cb209f5SScott Long #define AAC_FLAGS_NEW_COMM (1 << 11) /* New comm. interface supported */ 3877cb209f5SScott Long #define AAC_FLAGS_RAW_IO (1 << 12) /* Raw I/O interface */ 3887cb209f5SScott Long #define AAC_FLAGS_ARRAY_64BIT (1 << 13) /* 64-bit array size */ 389fe3cb0e1SScott Long 390a6d35632SScott Long u_int32_t supported_options; 391fe3cb0e1SScott Long u_int32_t scsi_method_id; 39270545d1aSScott Long TAILQ_HEAD(,aac_sim) aac_sim_tqh; 3937cb209f5SScott Long 3947cb209f5SScott Long u_int32_t aac_max_fibs; /* max. FIB count */ 3957cb209f5SScott Long u_int32_t aac_max_fibs_alloc; /* max. alloc. per alloc_commands() */ 3967cb209f5SScott Long u_int32_t aac_max_fib_size; /* max. FIB size */ 3977cb209f5SScott Long u_int32_t aac_sg_tablesize; /* max. sg count from host */ 3987cb209f5SScott Long u_int32_t aac_max_sectors; /* max. I/O size from host (blocks) */ 39935863739SMike Smith }; 40035863739SMike Smith 4017cb209f5SScott Long /* 4027cb209f5SScott Long * Event callback mechanism for the driver 4037cb209f5SScott Long */ 4047cb209f5SScott Long #define AAC_EVENT_NONE 0x00 4057cb209f5SScott Long #define AAC_EVENT_CMFREE 0x01 4067cb209f5SScott Long #define AAC_EVENT_MASK 0xff 4077cb209f5SScott Long #define AAC_EVENT_REPEAT 0x100 4087cb209f5SScott Long 4097cb209f5SScott Long typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event, 4107cb209f5SScott Long void *arg); 4117cb209f5SScott Long struct aac_event { 4127cb209f5SScott Long TAILQ_ENTRY(aac_event) ev_links; 4137cb209f5SScott Long int ev_type; 4147cb209f5SScott Long aac_event_cb_t *ev_callback; 4157cb209f5SScott Long void *ev_arg; 4167cb209f5SScott Long }; 41735863739SMike Smith 41835863739SMike Smith /* 41935863739SMike Smith * Public functions 42035863739SMike Smith */ 42135863739SMike Smith extern void aac_free(struct aac_softc *sc); 42235863739SMike Smith extern int aac_attach(struct aac_softc *sc); 42335863739SMike Smith extern int aac_detach(device_t dev); 42435863739SMike Smith extern int aac_shutdown(device_t dev); 42535863739SMike Smith extern int aac_suspend(device_t dev); 42635863739SMike Smith extern int aac_resume(device_t dev); 4277cb209f5SScott Long extern void aac_new_intr(void *arg); 4287cb209f5SScott Long extern void aac_fast_intr(void *arg); 42935863739SMike Smith extern void aac_submit_bio(struct bio *bp); 4300b94a66eSMike Smith extern void aac_biodone(struct bio *bp); 431fe3cb0e1SScott Long extern void aac_startio(struct aac_softc *sc); 432fe3cb0e1SScott Long extern int aac_alloc_command(struct aac_softc *sc, 433fe3cb0e1SScott Long struct aac_command **cmp); 434fe3cb0e1SScott Long extern void aac_release_command(struct aac_command *cm); 435cbfd045bSScott Long extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, 436cbfd045bSScott Long u_int32_t xferstate, struct aac_fib *fib, 437cbfd045bSScott Long u_int16_t datasize); 4387cb209f5SScott Long extern void aac_add_event(struct aac_softc *sc, struct aac_event 4397cb209f5SScott Long *event); 44035863739SMike Smith 44135863739SMike Smith /* 44235863739SMike Smith * Debugging levels: 44335863739SMike Smith * 0 - quiet, only emit warnings 44435863739SMike Smith * 1 - noisy, emit major function points and things done 44535863739SMike Smith * 2 - extremely noisy, emit trace items in loops, etc. 44635863739SMike Smith */ 44735863739SMike Smith #ifdef AAC_DEBUG 4480b94a66eSMike Smith # define debug(level, fmt, args...) \ 4490b94a66eSMike Smith do { \ 4506e551fb6SDavid E. O'Brien if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ 4510b94a66eSMike Smith } while (0) 4520b94a66eSMike Smith # define debug_called(level) \ 4530b94a66eSMike Smith do { \ 454956d569bSDavid E. O'Brien if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \ 4550b94a66eSMike Smith } while (0) 45635863739SMike Smith 45735863739SMike Smith extern void aac_print_queues(struct aac_softc *sc); 45835863739SMike Smith extern void aac_panic(struct aac_softc *sc, char *reason); 459c6eafcf2SScott Long extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, 46070148712SPeter Wemm const char *caller); 461c6eafcf2SScott Long extern void aac_print_aif(struct aac_softc *sc, 462c6eafcf2SScott Long struct aac_aif_command *aif); 46335863739SMike Smith 4646e551fb6SDavid E. O'Brien #define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) 46535863739SMike Smith 46635863739SMike Smith #else 46735863739SMike Smith # define debug(level, fmt, args...) 46835863739SMike Smith # define debug_called(level) 46935863739SMike Smith 47035863739SMike Smith # define aac_print_queues(sc) 47135863739SMike Smith # define aac_panic(sc, reason) 47235863739SMike Smith 47335863739SMike Smith # define AAC_PRINT_FIB(sc, fib) 47436e0bf6eSScott Long # define aac_print_aif(sc, aac_aif_command) 47535863739SMike Smith #endif 47635863739SMike Smith 47735863739SMike Smith struct aac_code_lookup { 47835863739SMike Smith char *string; 47935863739SMike Smith u_int32_t code; 48035863739SMike Smith }; 48135863739SMike Smith 482914da7d0SScott Long /* 4830b94a66eSMike Smith * Queue primitives for driver queues. 48435863739SMike Smith */ 4850b94a66eSMike Smith #define AACQ_ADD(sc, qname) \ 4860b94a66eSMike Smith do { \ 487914da7d0SScott Long struct aac_qstat *qs; \ 488914da7d0SScott Long \ 489914da7d0SScott Long qs = &(sc)->aac_qstat[qname]; \ 4900b94a66eSMike Smith \ 4910b94a66eSMike Smith qs->q_length++; \ 4920b94a66eSMike Smith if (qs->q_length > qs->q_max) \ 4930b94a66eSMike Smith qs->q_max = qs->q_length; \ 4940b94a66eSMike Smith } while (0) 49535863739SMike Smith 4960b94a66eSMike Smith #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- 4970b94a66eSMike Smith #define AACQ_INIT(sc, qname) \ 4980b94a66eSMike Smith do { \ 4990b94a66eSMike Smith sc->aac_qstat[qname].q_length = 0; \ 5000b94a66eSMike Smith sc->aac_qstat[qname].q_max = 0; \ 5010b94a66eSMike Smith } while (0) 5020b94a66eSMike Smith 5030b94a66eSMike Smith 5040b94a66eSMike Smith #define AACQ_COMMAND_QUEUE(name, index) \ 5050b94a66eSMike Smith static __inline void \ 5060b94a66eSMike Smith aac_initq_ ## name (struct aac_softc *sc) \ 5070b94a66eSMike Smith { \ 5080b94a66eSMike Smith TAILQ_INIT(&sc->aac_ ## name); \ 5090b94a66eSMike Smith AACQ_INIT(sc, index); \ 5100b94a66eSMike Smith } \ 5110b94a66eSMike Smith static __inline void \ 5120b94a66eSMike Smith aac_enqueue_ ## name (struct aac_command *cm) \ 5130b94a66eSMike Smith { \ 514f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 515f6c4dd3fSScott Long printf("command %p is on another queue, flags = %#x\n", \ 516f6c4dd3fSScott Long cm, cm->cm_flags); \ 517f6c4dd3fSScott Long panic("command is on another queue"); \ 518f6c4dd3fSScott Long } \ 5190b94a66eSMike Smith TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 520f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 5210b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 5220b94a66eSMike Smith } \ 5230b94a66eSMike Smith static __inline void \ 5240b94a66eSMike Smith aac_requeue_ ## name (struct aac_command *cm) \ 5250b94a66eSMike Smith { \ 526f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 527f6c4dd3fSScott Long printf("command %p is on another queue, flags = %#x\n", \ 528f6c4dd3fSScott Long cm, cm->cm_flags); \ 529f6c4dd3fSScott Long panic("command is on another queue"); \ 530f6c4dd3fSScott Long } \ 5310b94a66eSMike Smith TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 532f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 5330b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 5340b94a66eSMike Smith } \ 5350b94a66eSMike Smith static __inline struct aac_command * \ 5360b94a66eSMike Smith aac_dequeue_ ## name (struct aac_softc *sc) \ 5370b94a66eSMike Smith { \ 5380b94a66eSMike Smith struct aac_command *cm; \ 5390b94a66eSMike Smith \ 5400b94a66eSMike Smith if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ 541f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 542914da7d0SScott Long printf("command %p not in queue, flags = %#x, " \ 543914da7d0SScott Long "bit = %#x\n", cm, cm->cm_flags, \ 544914da7d0SScott Long AAC_ON_ ## index); \ 545f6c4dd3fSScott Long panic("command not in queue"); \ 546f6c4dd3fSScott Long } \ 5470b94a66eSMike Smith TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ 548f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5490b94a66eSMike Smith AACQ_REMOVE(sc, index); \ 5500b94a66eSMike Smith } \ 5510b94a66eSMike Smith return(cm); \ 5520b94a66eSMike Smith } \ 5530b94a66eSMike Smith static __inline void \ 5540b94a66eSMike Smith aac_remove_ ## name (struct aac_command *cm) \ 5550b94a66eSMike Smith { \ 556f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 557914da7d0SScott Long printf("command %p not in queue, flags = %#x, " \ 558914da7d0SScott Long "bit = %#x\n", cm, cm->cm_flags, \ 559914da7d0SScott Long AAC_ON_ ## index); \ 560f6c4dd3fSScott Long panic("command not in queue"); \ 561f6c4dd3fSScott Long } \ 5620b94a66eSMike Smith TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 563f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5640b94a66eSMike Smith AACQ_REMOVE(cm->cm_sc, index); \ 5650b94a66eSMike Smith } \ 5660b94a66eSMike Smith struct hack 5670b94a66eSMike Smith 5680b94a66eSMike Smith AACQ_COMMAND_QUEUE(free, AACQ_FREE); 5690b94a66eSMike Smith AACQ_COMMAND_QUEUE(ready, AACQ_READY); 5700b94a66eSMike Smith AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); 5710b94a66eSMike Smith 5720b94a66eSMike Smith /* 5730b94a66eSMike Smith * outstanding bio queue 5740b94a66eSMike Smith */ 57535863739SMike Smith static __inline void 5760b94a66eSMike Smith aac_initq_bio(struct aac_softc *sc) 57735863739SMike Smith { 5780b94a66eSMike Smith bioq_init(&sc->aac_bioq); 5790b94a66eSMike Smith AACQ_INIT(sc, AACQ_BIO); 58035863739SMike Smith } 58135863739SMike Smith 58235863739SMike Smith static __inline void 5830b94a66eSMike Smith aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) 58435863739SMike Smith { 5850b94a66eSMike Smith bioq_insert_tail(&sc->aac_bioq, bp); 5860b94a66eSMike Smith AACQ_ADD(sc, AACQ_BIO); 58735863739SMike Smith } 58835863739SMike Smith 5890b94a66eSMike Smith static __inline struct bio * 5900b94a66eSMike Smith aac_dequeue_bio(struct aac_softc *sc) 59135863739SMike Smith { 5920b94a66eSMike Smith struct bio *bp; 59335863739SMike Smith 5940b94a66eSMike Smith if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { 5950b94a66eSMike Smith bioq_remove(&sc->aac_bioq, bp); 5960b94a66eSMike Smith AACQ_REMOVE(sc, AACQ_BIO); 59735863739SMike Smith } 5980b94a66eSMike Smith return(bp); 59935863739SMike Smith } 60036e0bf6eSScott Long 60136e0bf6eSScott Long static __inline void 60236e0bf6eSScott Long aac_print_printf(struct aac_softc *sc) 60336e0bf6eSScott Long { 604b3457b51SScott Long /* 605b3457b51SScott Long * XXX We have the ability to read the length of the printf string 606b3457b51SScott Long * from out of the mailboxes. 607b3457b51SScott Long */ 60836e0bf6eSScott Long device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, 60936e0bf6eSScott Long sc->aac_common->ac_printf); 6109148fa21SScott Long sc->aac_common->ac_printf[0] = 0; 61136e0bf6eSScott Long AAC_QNOTIFY(sc, AAC_DB_PRINTF); 61236e0bf6eSScott Long } 61303b5fe51SScott Long 61403b5fe51SScott Long static __inline int 61503b5fe51SScott Long aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib) 61603b5fe51SScott Long { 61703b5fe51SScott Long 6187cb209f5SScott Long mtx_assert(&sc->aac_io_lock, MA_OWNED); 61903b5fe51SScott Long *fib = &sc->aac_common->ac_sync_fib; 62003b5fe51SScott Long return (0); 62103b5fe51SScott Long } 62203b5fe51SScott Long 62303b5fe51SScott Long static __inline void 62403b5fe51SScott Long aac_release_sync_fib(struct aac_softc *sc) 62503b5fe51SScott Long { 62603b5fe51SScott Long 6277cb209f5SScott Long mtx_assert(&sc->aac_io_lock, MA_OWNED); 62803b5fe51SScott Long } 62903b5fe51SScott Long 630