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> 33ff0991c4SAttilio Rao #include <sys/callout.h> 349e2e96d8SScott Long #include <sys/lock.h> 359e2e96d8SScott Long #include <sys/mutex.h> 369e2e96d8SScott Long #include <sys/taskqueue.h> 379e2e96d8SScott Long #include <sys/selinfo.h> 38891619a6SPoul-Henning Kamp #include <geom/geom_disk.h> 39891619a6SPoul-Henning Kamp 408e7e6335SEd Maste #define AAC_TYPE_DEVO 1 418e7e6335SEd Maste #define AAC_TYPE_ALPHA 2 428e7e6335SEd Maste #define AAC_TYPE_BETA 3 438e7e6335SEd Maste #define AAC_TYPE_RELEASE 4 448e7e6335SEd Maste 458e7e6335SEd Maste #define AAC_DRIVER_MAJOR_VERSION 2 468e7e6335SEd Maste #define AAC_DRIVER_MINOR_VERSION 1 478e7e6335SEd Maste #define AAC_DRIVER_BUGFIX_LEVEL 9 488e7e6335SEd Maste #define AAC_DRIVER_TYPE AAC_TYPE_RELEASE 498e7e6335SEd Maste 507cb209f5SScott Long #ifndef AAC_DRIVER_BUILD 517cb209f5SScott Long # define AAC_DRIVER_BUILD 1 527cb209f5SScott Long #endif 539e2e96d8SScott Long 54914da7d0SScott Long /* 55914da7d0SScott Long * Driver Parameter Definitions 56914da7d0SScott Long */ 5735863739SMike Smith 5835863739SMike Smith /* 5935863739SMike Smith * The firmware interface allows for a 16-bit s/g list length. We limit 6035863739SMike Smith * ourselves to a reasonable maximum and ensure alignment. 6135863739SMike Smith */ 6235863739SMike Smith #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ 6335863739SMike Smith 6435863739SMike Smith /* 6535863739SMike Smith * We allocate a small set of FIBs for the adapter to use to send us messages. 6635863739SMike Smith */ 6735863739SMike Smith #define AAC_ADAPTER_FIBS 8 6835863739SMike Smith 6935863739SMike Smith /* 70c6eafcf2SScott Long * The controller reports status events in AIFs. We hang on to a number of 71c6eafcf2SScott Long * these in order to pass them out to user-space management tools. 7235863739SMike Smith */ 7335863739SMike Smith #define AAC_AIFQ_LENGTH 64 7435863739SMike Smith 7535863739SMike Smith /* 7635863739SMike Smith * Firmware messages are passed in the printf buffer. 7735863739SMike Smith */ 7835863739SMike Smith #define AAC_PRINTF_BUFSIZE 256 7935863739SMike Smith 8035863739SMike Smith /* 81c6eafcf2SScott Long * We wait this many seconds for the adapter to come ready if it is still 82c6eafcf2SScott Long * booting 8335863739SMike Smith */ 8435863739SMike Smith #define AAC_BOOT_TIMEOUT (3 * 60) 8535863739SMike Smith 8635863739SMike Smith /* 8735863739SMike Smith * Timeout for immediate commands. 8835863739SMike Smith */ 890b94a66eSMike Smith #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ 900b94a66eSMike Smith 910b94a66eSMike Smith /* 920b94a66eSMike Smith * Timeout for normal commands 930b94a66eSMike Smith */ 944a42b061SEd Maste #define AAC_CMD_TIMEOUT 120 /* seconds */ 950b94a66eSMike Smith 960b94a66eSMike Smith /* 970b94a66eSMike Smith * Rate at which we periodically check for timed out commands and kick the 980b94a66eSMike Smith * controller. 990b94a66eSMike Smith */ 10036e0bf6eSScott Long #define AAC_PERIODIC_INTERVAL 20 /* seconds */ 10135863739SMike Smith 10235863739SMike Smith /* 10335863739SMike Smith * Per-container data structure 10435863739SMike Smith */ 10535863739SMike Smith struct aac_container 10635863739SMike Smith { 10735863739SMike Smith struct aac_mntobj co_mntobj; 10835863739SMike Smith device_t co_disk; 10936e0bf6eSScott Long int co_found; 11036e0bf6eSScott Long TAILQ_ENTRY(aac_container) co_link; 11135863739SMike Smith }; 11235863739SMike Smith 11335863739SMike Smith /* 11470545d1aSScott Long * Per-SIM data structure 11570545d1aSScott Long */ 116851f59d7SEd Maste struct aac_cam; 11770545d1aSScott Long struct aac_sim 11870545d1aSScott Long { 11970545d1aSScott Long device_t sim_dev; 12070545d1aSScott Long int TargetsPerBus; 12170545d1aSScott Long int BusNumber; 12270545d1aSScott Long int InitiatorBusId; 12370545d1aSScott Long struct aac_softc *aac_sc; 124851f59d7SEd Maste struct aac_cam *aac_cam; 12570545d1aSScott Long TAILQ_ENTRY(aac_sim) sim_link; 12670545d1aSScott Long }; 12770545d1aSScott Long 12870545d1aSScott Long /* 12935863739SMike Smith * Per-disk structure 13035863739SMike Smith */ 13135863739SMike Smith struct aac_disk 13235863739SMike Smith { 13335863739SMike Smith device_t ad_dev; 13435863739SMike Smith struct aac_softc *ad_controller; 13535863739SMike Smith struct aac_container *ad_container; 1360b7ed341SPoul-Henning Kamp struct disk *ad_disk; 13735863739SMike Smith int ad_flags; 13835863739SMike Smith #define AAC_DISK_OPEN (1<<0) 13935863739SMike Smith int ad_cylinders; 14035863739SMike Smith int ad_heads; 14135863739SMike Smith int ad_sectors; 142523da39bSEd Maste u_int64_t ad_size; 14336e0bf6eSScott Long int unit; 14435863739SMike Smith }; 14535863739SMike Smith 14635863739SMike Smith /* 14735863739SMike Smith * Per-command control structure. 14835863739SMike Smith */ 14935863739SMike Smith struct aac_command 15035863739SMike Smith { 15135863739SMike Smith TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ 15235863739SMike Smith 15335863739SMike Smith struct aac_softc *cm_sc; /* controller that owns us */ 15435863739SMike Smith 155c6eafcf2SScott Long struct aac_fib *cm_fib; /* FIB associated with this 156c6eafcf2SScott Long * command */ 1577cb209f5SScott Long u_int64_t cm_fibphys; /* bus address of the FIB */ 158c6eafcf2SScott Long struct bio *cm_data; /* pointer to data in kernel 159c6eafcf2SScott Long * space */ 16035863739SMike Smith u_int32_t cm_datalen; /* data length */ 16135863739SMike Smith bus_dmamap_t cm_datamap; /* DMA map for bio data */ 162c6eafcf2SScott Long struct aac_sg_table *cm_sgtable; /* pointer to s/g table in 163c6eafcf2SScott Long * command */ 16435863739SMike Smith int cm_flags; 165c6eafcf2SScott Long #define AAC_CMD_MAPPED (1<<0) /* command has had its data 166c6eafcf2SScott Long * mapped */ 167c6eafcf2SScott Long #define AAC_CMD_DATAIN (1<<1) /* command involves data moving 168c6eafcf2SScott Long * from controller to host */ 169c6eafcf2SScott Long #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving 170c6eafcf2SScott Long * from host to controller */ 17135863739SMike Smith #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ 1720b94a66eSMike Smith #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ 173f6c4dd3fSScott Long #define AAC_ON_AACQ_FREE (1<<5) 174f6c4dd3fSScott Long #define AAC_ON_AACQ_READY (1<<6) 175f6c4dd3fSScott Long #define AAC_ON_AACQ_BUSY (1<<7) 1767cb209f5SScott Long #define AAC_ON_AACQ_AIF (1<<8) 1777cb209f5SScott Long #define AAC_ON_AACQ_NORM (1<<10) 1787cb209f5SScott Long #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10)) 179cd481291SScott Long #define AAC_QUEUE_FRZN (1<<9) /* Freeze the processing of 180cd481291SScott Long * commands on the queue. */ 18135863739SMike Smith 18235863739SMike Smith void (* cm_complete)(struct aac_command *cm); 18335863739SMike Smith void *cm_private; 1840b94a66eSMike Smith time_t cm_timestamp; /* command creation time */ 18536e0bf6eSScott Long int cm_queue; 186cb0d64b9SScott Long int cm_index; 18735863739SMike Smith }; 18835863739SMike Smith 189ffb37f33SScott Long struct aac_fibmap { 190ffb37f33SScott Long TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */ 191ffb37f33SScott Long struct aac_fib *aac_fibs; 192ffb37f33SScott Long bus_dmamap_t aac_fibmap; 193ffb37f33SScott Long struct aac_command *aac_commands; 194ffb37f33SScott Long }; 195ffb37f33SScott Long 19635863739SMike Smith /* 19735863739SMike Smith * We gather a number of adapter-visible items into a single structure. 19835863739SMike Smith * 19935863739SMike Smith * The ordering of this strucure may be important; we copy the Linux driver: 20035863739SMike Smith * 20135863739SMike Smith * Adapter FIBs 20235863739SMike Smith * Init struct 20335863739SMike Smith * Queue headers (Comm Area) 20435863739SMike Smith * Printf buffer 20535863739SMike Smith * 20635863739SMike Smith * In addition, we add: 20735863739SMike Smith * Sync Fib 20835863739SMike Smith */ 20935863739SMike Smith struct aac_common { 21035863739SMike Smith /* fibs for the controller to send us messages */ 21135863739SMike Smith struct aac_fib ac_fibs[AAC_ADAPTER_FIBS]; 21235863739SMike Smith 21335863739SMike Smith /* the init structure */ 21435863739SMike Smith struct aac_adapter_init ac_init; 21535863739SMike Smith 21635863739SMike Smith /* arena within which the queue structures are kept */ 217c6eafcf2SScott Long u_int8_t ac_qbuf[sizeof(struct aac_queue_table) + 218c6eafcf2SScott Long AAC_QUEUE_ALIGN]; 21935863739SMike Smith 22035863739SMike Smith /* buffer for text messages from the controller */ 22135863739SMike Smith char ac_printf[AAC_PRINTF_BUFSIZE]; 22235863739SMike Smith 22335863739SMike Smith /* fib for synchronous commands */ 22435863739SMike Smith struct aac_fib ac_sync_fib; 22535863739SMike Smith }; 22635863739SMike Smith 22735863739SMike Smith /* 22835863739SMike Smith * Interface operations 22935863739SMike Smith */ 23035863739SMike Smith struct aac_interface 23135863739SMike Smith { 23235863739SMike Smith int (*aif_get_fwstatus)(struct aac_softc *sc); 23335863739SMike Smith void (*aif_qnotify)(struct aac_softc *sc, int qbit); 23435863739SMike Smith int (*aif_get_istatus)(struct aac_softc *sc); 235b3457b51SScott Long void (*aif_clr_istatus)(struct aac_softc *sc, int mask); 23635863739SMike Smith void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command, 237c6eafcf2SScott Long u_int32_t arg0, u_int32_t arg1, 238c6eafcf2SScott Long u_int32_t arg2, u_int32_t arg3); 239a6d35632SScott Long int (*aif_get_mailbox)(struct aac_softc *sc, int mb); 24035863739SMike Smith void (*aif_set_interrupts)(struct aac_softc *sc, int enable); 2417cb209f5SScott Long int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm); 2427cb209f5SScott Long int (*aif_get_outb_queue)(struct aac_softc *sc); 2437cb209f5SScott Long void (*aif_set_outb_queue)(struct aac_softc *sc, int index); 24435863739SMike Smith }; 24535863739SMike Smith extern struct aac_interface aac_rx_interface; 24635863739SMike Smith extern struct aac_interface aac_sa_interface; 247b3457b51SScott Long extern struct aac_interface aac_fa_interface; 2484afedc31SScott Long extern struct aac_interface aac_rkt_interface; 24935863739SMike Smith 25035863739SMike Smith #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc))) 25135863739SMike Smith #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit))) 25235863739SMike Smith #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc))) 253b3457b51SScott Long #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \ 254c6eafcf2SScott Long (mask))) 25535863739SMike Smith #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \ 256c6eafcf2SScott Long ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \ 257c6eafcf2SScott Long (arg3))) 258a6d35632SScott Long #define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \ 259a6d35632SScott Long (mb))) 260914da7d0SScott Long #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 261914da7d0SScott Long 0)) 262914da7d0SScott Long #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 263914da7d0SScott Long 1)) 2647cb209f5SScott Long #define AAC_SEND_COMMAND(sc, cm) ((sc)->aac_if.aif_send_command((sc), (cm))) 2657cb209f5SScott Long #define AAC_GET_OUTB_QUEUE(sc) ((sc)->aac_if.aif_get_outb_queue((sc))) 2667cb209f5SScott Long #define AAC_SET_OUTB_QUEUE(sc, idx) ((sc)->aac_if.aif_set_outb_queue((sc), (idx))) 26735863739SMike Smith 268ff0991c4SAttilio Rao #define AAC_MEM0_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag0, \ 269ff0991c4SAttilio Rao sc->aac_bhandle0, reg, val) 270ff0991c4SAttilio Rao #define AAC_MEM0_GETREG4(sc, reg) bus_space_read_4(sc->aac_btag0, \ 271ff0991c4SAttilio Rao sc->aac_bhandle0, reg) 272ff0991c4SAttilio Rao #define AAC_MEM0_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag0, \ 273ff0991c4SAttilio Rao sc->aac_bhandle0, reg, val) 274ff0991c4SAttilio Rao #define AAC_MEM0_GETREG2(sc, reg) bus_space_read_2(sc->aac_btag0, \ 275ff0991c4SAttilio Rao sc->aac_bhandle0, reg) 276ff0991c4SAttilio Rao #define AAC_MEM0_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag0, \ 277ff0991c4SAttilio Rao sc->aac_bhandle0, reg, val) 278ff0991c4SAttilio Rao #define AAC_MEM0_GETREG1(sc, reg) bus_space_read_1(sc->aac_btag0, \ 279ff0991c4SAttilio Rao sc->aac_bhandle0, reg) 280ff0991c4SAttilio Rao 281ff0991c4SAttilio Rao #define AAC_MEM1_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag1, \ 282ff0991c4SAttilio Rao sc->aac_bhandle1, reg, val) 283ff0991c4SAttilio Rao #define AAC_MEM1_GETREG4(sc, reg) bus_space_read_4(sc->aac_btag1, \ 284ff0991c4SAttilio Rao sc->aac_bhandle1, reg) 285ff0991c4SAttilio Rao #define AAC_MEM1_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag1, \ 286ff0991c4SAttilio Rao sc->aac_bhandle1, reg, val) 287ff0991c4SAttilio Rao #define AAC_MEM1_GETREG2(sc, reg) bus_space_read_2(sc->aac_btag1, \ 288ff0991c4SAttilio Rao sc->aac_bhandle1, reg) 289ff0991c4SAttilio Rao #define AAC_MEM1_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag1, \ 290ff0991c4SAttilio Rao sc->aac_bhandle1, reg, val) 291ff0991c4SAttilio Rao #define AAC_MEM1_GETREG1(sc, reg) bus_space_read_1(sc->aac_btag1, \ 292ff0991c4SAttilio Rao sc->aac_bhandle1, reg) 29335863739SMike Smith 294a723a548SEd Maste /* fib context (IOCTL) */ 295a723a548SEd Maste struct aac_fib_context { 296a723a548SEd Maste u_int32_t unique; 297a723a548SEd Maste int ctx_idx; 298a723a548SEd Maste int ctx_wrap; 299a723a548SEd Maste struct aac_fib_context *next, *prev; 300a723a548SEd Maste }; 301a723a548SEd Maste 30235863739SMike Smith /* 30335863739SMike Smith * Per-controller structure. 30435863739SMike Smith */ 30535863739SMike Smith struct aac_softc 30635863739SMike Smith { 30735863739SMike Smith /* bus connections */ 30835863739SMike Smith device_t aac_dev; 309ff0991c4SAttilio Rao struct resource *aac_regs_res0, *aac_regs_res1; /* reg. if. window */ 310ff0991c4SAttilio Rao int aac_regs_rid0, aac_regs_rid1; /* resource ID */ 311ff0991c4SAttilio Rao bus_space_handle_t aac_bhandle0, aac_bhandle1; /* bus space handle */ 312ff0991c4SAttilio Rao bus_space_tag_t aac_btag0, aac_btag1; /* bus space tag */ 31335863739SMike Smith bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ 314c6eafcf2SScott Long bus_dma_tag_t aac_buffer_dmat; /* data buffer/command 315c6eafcf2SScott Long * DMA tag */ 31635863739SMike Smith struct resource *aac_irq; /* interrupt */ 31735863739SMike Smith int aac_irq_rid; 31835863739SMike Smith void *aac_intr; /* interrupt handle */ 3195f54d522SScott Long eventhandler_tag eh; 32035863739SMike Smith 32135863739SMike Smith /* controller features, limits and status */ 32235863739SMike Smith int aac_state; 32335863739SMike Smith #define AAC_STATE_SUSPEND (1<<0) 32404f798ecSAttilio Rao #define AAC_STATE_UNUSED0 (1<<1) 32535863739SMike Smith #define AAC_STATE_INTERRUPTS_ON (1<<2) 32635863739SMike Smith #define AAC_STATE_AIF_SLEEPER (1<<3) 32735863739SMike Smith struct FsaRevision aac_revision; 32835863739SMike Smith 32935863739SMike Smith /* controller hardware interface */ 33035863739SMike Smith int aac_hwif; 33135863739SMike Smith #define AAC_HWIF_I960RX 0 33235863739SMike Smith #define AAC_HWIF_STRONGARM 1 3334afedc31SScott Long #define AAC_HWIF_RKT 3 334ff0991c4SAttilio Rao #define AAC_HWIF_NARK 4 3350b94a66eSMike Smith #define AAC_HWIF_UNKNOWN -1 336c6eafcf2SScott Long bus_dma_tag_t aac_common_dmat; /* common structure 337c6eafcf2SScott Long * DMA tag */ 338c6eafcf2SScott Long bus_dmamap_t aac_common_dmamap; /* common structure 339c6eafcf2SScott Long * DMA map */ 34035863739SMike Smith struct aac_common *aac_common; 34135863739SMike Smith u_int32_t aac_common_busaddr; 34235863739SMike Smith struct aac_interface aac_if; 34335863739SMike Smith 34435863739SMike Smith /* command/fib resources */ 345c6eafcf2SScott Long bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ 346ffb37f33SScott Long TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh; 3478b149b51SJohn Baldwin u_int total_fibs; 348ffb37f33SScott Long struct aac_command *aac_commands; 34935863739SMike Smith 35035863739SMike Smith /* command management */ 351c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_free; /* command structures 352c6eafcf2SScott Long * available for reuse */ 353c6eafcf2SScott Long TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for 354c6eafcf2SScott Long * controller resources */ 3550b94a66eSMike Smith TAILQ_HEAD(,aac_command) aac_busy; 3567cb209f5SScott Long TAILQ_HEAD(,aac_event) aac_ev_cmfree; 35735863739SMike Smith struct bio_queue_head aac_bioq; 35835863739SMike Smith struct aac_queue_table *aac_queues; 35935863739SMike Smith struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; 36035863739SMike Smith 3610b94a66eSMike Smith struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ 3620b94a66eSMike Smith 36335863739SMike Smith /* connected containters */ 36470545d1aSScott Long TAILQ_HEAD(,aac_container) aac_container_tqh; 365bb6fe253SScott Long struct mtx aac_container_lock; 36635863739SMike Smith 36703b5fe51SScott Long /* 36803b5fe51SScott Long * The general I/O lock. This protects the sync fib, the lists, the 36903b5fe51SScott Long * queues, and the registers. 37003b5fe51SScott Long */ 371bb6fe253SScott Long struct mtx aac_io_lock; 372ae543596SScott Long 37335863739SMike Smith /* delayed activity infrastructure */ 374c6eafcf2SScott Long struct task aac_task_complete; /* deferred-completion 375c6eafcf2SScott Long * task */ 37635863739SMike Smith struct intr_config_hook aac_ich; 37735863739SMike Smith 37835863739SMike Smith /* management interface */ 37989c9c53dSPoul-Henning Kamp struct cdev *aac_dev_t; 380bb6fe253SScott Long struct mtx aac_aifq_lock; 381a723a548SEd Maste struct aac_fib aac_aifq[AAC_AIFQ_LENGTH]; 382a723a548SEd Maste int aifq_idx; 383a723a548SEd Maste int aifq_filled; 384a723a548SEd Maste struct aac_fib_context *fibctx; 385b3457b51SScott Long struct selinfo rcv_select; 38636e0bf6eSScott Long struct proc *aifthread; 38736e0bf6eSScott Long int aifflags; 38836e0bf6eSScott Long #define AAC_AIFFLAGS_RUNNING (1 << 0) 38970545d1aSScott Long #define AAC_AIFFLAGS_AIF (1 << 1) 39036e0bf6eSScott Long #define AAC_AIFFLAGS_EXIT (1 << 2) 39136e0bf6eSScott Long #define AAC_AIFFLAGS_EXITED (1 << 3) 39270545d1aSScott Long #define AAC_AIFFLAGS_PRINTF (1 << 4) 393ae543596SScott Long #define AAC_AIFFLAGS_ALLOCFIBS (1 << 5) 394ae543596SScott Long #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \ 395ae543596SScott Long AAC_AIFFLAGS_ALLOCFIBS) 396a6d35632SScott Long u_int32_t flags; 397a6d35632SScott Long #define AAC_FLAGS_PERC2QC (1 << 0) 398a6d35632SScott Long #define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */ 399a6d35632SScott Long #define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */ 400a6d35632SScott Long #define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */ 401a6d35632SScott Long #define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */ 402a6d35632SScott Long #define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem 403a6d35632SScott Long * 2GB-4GB range */ 404a6d35632SScott Long #define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */ 405a6d35632SScott Long #define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */ 4064b00f859SScott Long #define AAC_FLAGS_BROKEN_MEMMAP (1 << 8) /* Broken HostPhysMemPages */ 4077cb209f5SScott Long #define AAC_FLAGS_SLAVE (1 << 9) 4087cb209f5SScott Long #define AAC_FLAGS_MASTER (1 << 10) 4097cb209f5SScott Long #define AAC_FLAGS_NEW_COMM (1 << 11) /* New comm. interface supported */ 4107cb209f5SScott Long #define AAC_FLAGS_RAW_IO (1 << 12) /* Raw I/O interface */ 4117cb209f5SScott Long #define AAC_FLAGS_ARRAY_64BIT (1 << 13) /* 64-bit array size */ 412523da39bSEd Maste #define AAC_FLAGS_LBA_64BIT (1 << 14) /* 64-bit LBA support */ 413fe3cb0e1SScott Long 414a6d35632SScott Long u_int32_t supported_options; 415fe3cb0e1SScott Long u_int32_t scsi_method_id; 41670545d1aSScott Long TAILQ_HEAD(,aac_sim) aac_sim_tqh; 4177cb209f5SScott Long 418ff0991c4SAttilio Rao struct callout aac_daemontime; /* clock daemon callout */ 419ff0991c4SAttilio Rao 4207cb209f5SScott Long u_int32_t aac_max_fibs; /* max. FIB count */ 4217cb209f5SScott Long u_int32_t aac_max_fibs_alloc; /* max. alloc. per alloc_commands() */ 4227cb209f5SScott Long u_int32_t aac_max_fib_size; /* max. FIB size */ 4237cb209f5SScott Long u_int32_t aac_sg_tablesize; /* max. sg count from host */ 4247cb209f5SScott Long u_int32_t aac_max_sectors; /* max. I/O size from host (blocks) */ 425851f59d7SEd Maste #define AAC_CAM_TARGET_WILDCARD ~0 426851f59d7SEd Maste void (*cam_rescan_cb)(struct aac_softc *, uint32_t, 427851f59d7SEd Maste uint32_t); 42835863739SMike Smith }; 42935863739SMike Smith 4307cb209f5SScott Long /* 4317cb209f5SScott Long * Event callback mechanism for the driver 4327cb209f5SScott Long */ 4337cb209f5SScott Long #define AAC_EVENT_NONE 0x00 4347cb209f5SScott Long #define AAC_EVENT_CMFREE 0x01 4357cb209f5SScott Long #define AAC_EVENT_MASK 0xff 4367cb209f5SScott Long #define AAC_EVENT_REPEAT 0x100 4377cb209f5SScott Long 4387cb209f5SScott Long typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event, 4397cb209f5SScott Long void *arg); 4407cb209f5SScott Long struct aac_event { 4417cb209f5SScott Long TAILQ_ENTRY(aac_event) ev_links; 4427cb209f5SScott Long int ev_type; 4437cb209f5SScott Long aac_event_cb_t *ev_callback; 4447cb209f5SScott Long void *ev_arg; 4457cb209f5SScott Long }; 44635863739SMike Smith 44735863739SMike Smith /* 44835863739SMike Smith * Public functions 44935863739SMike Smith */ 45035863739SMike Smith extern void aac_free(struct aac_softc *sc); 45135863739SMike Smith extern int aac_attach(struct aac_softc *sc); 45235863739SMike Smith extern int aac_detach(device_t dev); 45335863739SMike Smith extern int aac_shutdown(device_t dev); 45435863739SMike Smith extern int aac_suspend(device_t dev); 45535863739SMike Smith extern int aac_resume(device_t dev); 4567cb209f5SScott Long extern void aac_new_intr(void *arg); 457e46b9eeaSEd Maste extern int aac_filter(void *arg); 45835863739SMike Smith extern void aac_submit_bio(struct bio *bp); 4590b94a66eSMike Smith extern void aac_biodone(struct bio *bp); 460fe3cb0e1SScott Long extern void aac_startio(struct aac_softc *sc); 461fe3cb0e1SScott Long extern int aac_alloc_command(struct aac_softc *sc, 462fe3cb0e1SScott Long struct aac_command **cmp); 463fe3cb0e1SScott Long extern void aac_release_command(struct aac_command *cm); 464cbfd045bSScott Long extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, 465cbfd045bSScott Long u_int32_t xferstate, struct aac_fib *fib, 466cbfd045bSScott Long u_int16_t datasize); 4677cb209f5SScott Long extern void aac_add_event(struct aac_softc *sc, struct aac_event 4687cb209f5SScott Long *event); 46935863739SMike Smith 47035863739SMike Smith #ifdef AAC_DEBUG 47131a0399eSEd Maste extern int aac_debug_enable; 47231a0399eSEd Maste # define fwprintf(sc, flags, fmt, args...) \ 4730b94a66eSMike Smith do { \ 47431a0399eSEd Maste if (!aac_debug_enable) \ 47531a0399eSEd Maste break; \ 47631a0399eSEd Maste if (sc != NULL) \ 47731a0399eSEd Maste device_printf(((struct aac_softc *)sc)->aac_dev, \ 47831a0399eSEd Maste "%s: " fmt "\n", __func__, ##args); \ 47931a0399eSEd Maste else \ 48031a0399eSEd Maste printf("%s: " fmt "\n", __func__, ##args); \ 4810b94a66eSMike Smith } while(0) 48235863739SMike Smith 48335863739SMike Smith extern void aac_print_queues(struct aac_softc *sc); 48435863739SMike Smith extern void aac_panic(struct aac_softc *sc, char *reason); 485c6eafcf2SScott Long extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, 48670148712SPeter Wemm const char *caller); 487c6eafcf2SScott Long extern void aac_print_aif(struct aac_softc *sc, 488c6eafcf2SScott Long struct aac_aif_command *aif); 48935863739SMike Smith 4906e551fb6SDavid E. O'Brien #define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) 49135863739SMike Smith 49235863739SMike Smith #else 49331a0399eSEd Maste # define fwprintf(sc, flags, fmt, args...) 49435863739SMike Smith 49535863739SMike Smith # define aac_print_queues(sc) 49635863739SMike Smith # define aac_panic(sc, reason) 49735863739SMike Smith 49835863739SMike Smith # define AAC_PRINT_FIB(sc, fib) 49936e0bf6eSScott Long # define aac_print_aif(sc, aac_aif_command) 50035863739SMike Smith #endif 50135863739SMike Smith 50235863739SMike Smith struct aac_code_lookup { 50335863739SMike Smith char *string; 50435863739SMike Smith u_int32_t code; 50535863739SMike Smith }; 50635863739SMike Smith 507914da7d0SScott Long /* 5080b94a66eSMike Smith * Queue primitives for driver queues. 50935863739SMike Smith */ 5100b94a66eSMike Smith #define AACQ_ADD(sc, qname) \ 5110b94a66eSMike Smith do { \ 512914da7d0SScott Long struct aac_qstat *qs; \ 513914da7d0SScott Long \ 514914da7d0SScott Long qs = &(sc)->aac_qstat[qname]; \ 5150b94a66eSMike Smith \ 5160b94a66eSMike Smith qs->q_length++; \ 5170b94a66eSMike Smith if (qs->q_length > qs->q_max) \ 5180b94a66eSMike Smith qs->q_max = qs->q_length; \ 5190b94a66eSMike Smith } while (0) 52035863739SMike Smith 5210b94a66eSMike Smith #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- 5220b94a66eSMike Smith #define AACQ_INIT(sc, qname) \ 5230b94a66eSMike Smith do { \ 5240b94a66eSMike Smith sc->aac_qstat[qname].q_length = 0; \ 5250b94a66eSMike Smith sc->aac_qstat[qname].q_max = 0; \ 5260b94a66eSMike Smith } while (0) 5270b94a66eSMike Smith 5280b94a66eSMike Smith 5290b94a66eSMike Smith #define AACQ_COMMAND_QUEUE(name, index) \ 5300b94a66eSMike Smith static __inline void \ 5310b94a66eSMike Smith aac_initq_ ## name (struct aac_softc *sc) \ 5320b94a66eSMike Smith { \ 5330b94a66eSMike Smith TAILQ_INIT(&sc->aac_ ## name); \ 5340b94a66eSMike Smith AACQ_INIT(sc, index); \ 5350b94a66eSMike Smith } \ 5360b94a66eSMike Smith static __inline void \ 5370b94a66eSMike Smith aac_enqueue_ ## name (struct aac_command *cm) \ 5380b94a66eSMike Smith { \ 539f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 540*729e10b9SEd Maste panic("aac: command %p is on another queue, flags = %#x", \ 541f6c4dd3fSScott Long cm, cm->cm_flags); \ 542f6c4dd3fSScott Long } \ 5430b94a66eSMike Smith TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 544f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 5450b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 5460b94a66eSMike Smith } \ 5470b94a66eSMike Smith static __inline void \ 5480b94a66eSMike Smith aac_requeue_ ## name (struct aac_command *cm) \ 5490b94a66eSMike Smith { \ 550f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 551*729e10b9SEd Maste panic("aac: command %p is on another queue, flags = %#x", \ 552f6c4dd3fSScott Long cm, cm->cm_flags); \ 553f6c4dd3fSScott Long } \ 5540b94a66eSMike Smith TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 555f6c4dd3fSScott Long cm->cm_flags |= AAC_ON_ ## index; \ 5560b94a66eSMike Smith AACQ_ADD(cm->cm_sc, index); \ 5570b94a66eSMike Smith } \ 5580b94a66eSMike Smith static __inline struct aac_command * \ 5590b94a66eSMike Smith aac_dequeue_ ## name (struct aac_softc *sc) \ 5600b94a66eSMike Smith { \ 5610b94a66eSMike Smith struct aac_command *cm; \ 5620b94a66eSMike Smith \ 5630b94a66eSMike Smith if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ 564f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 565*729e10b9SEd Maste panic("aac: command %p not in queue, flags = %#x, bit = %#x", \ 566*729e10b9SEd Maste cm, cm->cm_flags, AAC_ON_ ## index); \ 567f6c4dd3fSScott Long } \ 5680b94a66eSMike Smith TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ 569f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5700b94a66eSMike Smith AACQ_REMOVE(sc, index); \ 5710b94a66eSMike Smith } \ 5720b94a66eSMike Smith return(cm); \ 5730b94a66eSMike Smith } \ 5740b94a66eSMike Smith static __inline void \ 5750b94a66eSMike Smith aac_remove_ ## name (struct aac_command *cm) \ 5760b94a66eSMike Smith { \ 577f6c4dd3fSScott Long if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 578*729e10b9SEd Maste panic("aac: command %p not in queue, flags = %#x, bit = %#x", \ 579*729e10b9SEd Maste cm, cm->cm_flags, AAC_ON_ ## index); \ 580f6c4dd3fSScott Long } \ 5810b94a66eSMike Smith TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 582f6c4dd3fSScott Long cm->cm_flags &= ~AAC_ON_ ## index; \ 5830b94a66eSMike Smith AACQ_REMOVE(cm->cm_sc, index); \ 5840b94a66eSMike Smith } \ 5850b94a66eSMike Smith struct hack 5860b94a66eSMike Smith 5870b94a66eSMike Smith AACQ_COMMAND_QUEUE(free, AACQ_FREE); 5880b94a66eSMike Smith AACQ_COMMAND_QUEUE(ready, AACQ_READY); 5890b94a66eSMike Smith AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); 5900b94a66eSMike Smith 5910b94a66eSMike Smith /* 5920b94a66eSMike Smith * outstanding bio queue 5930b94a66eSMike Smith */ 59435863739SMike Smith static __inline void 5950b94a66eSMike Smith aac_initq_bio(struct aac_softc *sc) 59635863739SMike Smith { 5970b94a66eSMike Smith bioq_init(&sc->aac_bioq); 5980b94a66eSMike Smith AACQ_INIT(sc, AACQ_BIO); 59935863739SMike Smith } 60035863739SMike Smith 60135863739SMike Smith static __inline void 6020b94a66eSMike Smith aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) 60335863739SMike Smith { 6040b94a66eSMike Smith bioq_insert_tail(&sc->aac_bioq, bp); 6050b94a66eSMike Smith AACQ_ADD(sc, AACQ_BIO); 60635863739SMike Smith } 60735863739SMike Smith 6080b94a66eSMike Smith static __inline struct bio * 6090b94a66eSMike Smith aac_dequeue_bio(struct aac_softc *sc) 61035863739SMike Smith { 6110b94a66eSMike Smith struct bio *bp; 61235863739SMike Smith 6130b94a66eSMike Smith if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { 6140b94a66eSMike Smith bioq_remove(&sc->aac_bioq, bp); 6150b94a66eSMike Smith AACQ_REMOVE(sc, AACQ_BIO); 61635863739SMike Smith } 6170b94a66eSMike Smith return(bp); 61835863739SMike Smith } 61936e0bf6eSScott Long 62036e0bf6eSScott Long static __inline void 62136e0bf6eSScott Long aac_print_printf(struct aac_softc *sc) 62236e0bf6eSScott Long { 623b3457b51SScott Long /* 624b3457b51SScott Long * XXX We have the ability to read the length of the printf string 625b3457b51SScott Long * from out of the mailboxes. 626b3457b51SScott Long */ 62736e0bf6eSScott Long device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, 62836e0bf6eSScott Long sc->aac_common->ac_printf); 6299148fa21SScott Long sc->aac_common->ac_printf[0] = 0; 63036e0bf6eSScott Long AAC_QNOTIFY(sc, AAC_DB_PRINTF); 63136e0bf6eSScott Long } 63203b5fe51SScott Long 63303b5fe51SScott Long static __inline int 63403b5fe51SScott Long aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib) 63503b5fe51SScott Long { 63603b5fe51SScott Long 6377cb209f5SScott Long mtx_assert(&sc->aac_io_lock, MA_OWNED); 63803b5fe51SScott Long *fib = &sc->aac_common->ac_sync_fib; 63903b5fe51SScott Long return (0); 64003b5fe51SScott Long } 64103b5fe51SScott Long 64203b5fe51SScott Long static __inline void 64303b5fe51SScott Long aac_release_sync_fib(struct aac_softc *sc) 64403b5fe51SScott Long { 64503b5fe51SScott Long 6467cb209f5SScott Long mtx_assert(&sc->aac_io_lock, MA_OWNED); 64703b5fe51SScott Long } 64803b5fe51SScott Long 649