diff options
Diffstat (limited to 'sys/dev/aac')
-rw-r--r-- | sys/dev/aac/aac.c | 2970 | ||||
-rw-r--r-- | sys/dev/aac/aac_cam.c | 605 | ||||
-rw-r--r-- | sys/dev/aac/aac_compat.h | 64 | ||||
-rw-r--r-- | sys/dev/aac/aac_debug.c | 552 | ||||
-rw-r--r-- | sys/dev/aac/aac_disk.c | 392 | ||||
-rw-r--r-- | sys/dev/aac/aac_ioctl.h | 179 | ||||
-rw-r--r-- | sys/dev/aac/aac_linux.c | 84 | ||||
-rw-r--r-- | sys/dev/aac/aac_pci.c | 305 | ||||
-rw-r--r-- | sys/dev/aac/aac_tables.h | 118 | ||||
-rw-r--r-- | sys/dev/aac/aacreg.h | 1367 | ||||
-rw-r--r-- | sys/dev/aac/aacvar.h | 573 |
11 files changed, 7209 insertions, 0 deletions
diff --git a/sys/dev/aac/aac.c b/sys/dev/aac/aac.c new file mode 100644 index 0000000..21935a8 --- /dev/null +++ b/sys/dev/aac/aac.c @@ -0,0 +1,2970 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters. + */ + +#include "opt_aac.h" + +/* #include <stddef.h> */ +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/malloc.h> +#include <sys/kernel.h> +#include <sys/kthread.h> +#include <sys/sysctl.h> +#include <sys/poll.h> +#include <sys/ioccom.h> + +#include <sys/bus.h> +#include <sys/conf.h> +#include <sys/signalvar.h> +#include <sys/time.h> +#include <sys/eventhandler.h> + +#include <machine/bus_memio.h> +#include <machine/bus.h> +#include <machine/resource.h> + +#include <dev/aac/aacreg.h> +#include <dev/aac/aac_ioctl.h> +#include <dev/aac/aacvar.h> +#include <dev/aac/aac_tables.h> + +static void aac_startup(void *arg); +static void aac_add_container(struct aac_softc *sc, + struct aac_mntinforesp *mir, int f); +static void aac_get_bus_info(struct aac_softc *sc); + +/* Command Processing */ +static void aac_timeout(struct aac_softc *sc); +static int aac_start(struct aac_command *cm); +static void aac_complete(void *context, int pending); +static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp); +static void aac_bio_complete(struct aac_command *cm); +static int aac_wait_command(struct aac_command *cm, int timeout); +static void aac_command_thread(struct aac_softc *sc); + +/* Command Buffer Management */ +static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs, + int nseg, int error); +static int aac_alloc_commands(struct aac_softc *sc); +static void aac_free_commands(struct aac_softc *sc); +static void aac_map_command(struct aac_command *cm); +static void aac_unmap_command(struct aac_command *cm); + +/* Hardware Interface */ +static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, + int error); +static int aac_check_firmware(struct aac_softc *sc); +static int aac_init(struct aac_softc *sc); +static int aac_sync_command(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, + u_int32_t arg3, u_int32_t *sp); +static int aac_enqueue_fib(struct aac_softc *sc, int queue, + struct aac_command *cm); +static int aac_dequeue_fib(struct aac_softc *sc, int queue, + u_int32_t *fib_size, struct aac_fib **fib_addr); +static int aac_enqueue_response(struct aac_softc *sc, int queue, + struct aac_fib *fib); + +/* Falcon/PPC interface */ +static int aac_fa_get_fwstatus(struct aac_softc *sc); +static void aac_fa_qnotify(struct aac_softc *sc, int qbit); +static int aac_fa_get_istatus(struct aac_softc *sc); +static void aac_fa_clear_istatus(struct aac_softc *sc, int mask); +static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, + u_int32_t arg2, u_int32_t arg3); +static int aac_fa_get_mailbox(struct aac_softc *sc, int mb); +static void aac_fa_set_interrupts(struct aac_softc *sc, int enable); + +struct aac_interface aac_fa_interface = { + aac_fa_get_fwstatus, + aac_fa_qnotify, + aac_fa_get_istatus, + aac_fa_clear_istatus, + aac_fa_set_mailbox, + aac_fa_get_mailbox, + aac_fa_set_interrupts +}; + +/* StrongARM interface */ +static int aac_sa_get_fwstatus(struct aac_softc *sc); +static void aac_sa_qnotify(struct aac_softc *sc, int qbit); +static int aac_sa_get_istatus(struct aac_softc *sc); +static void aac_sa_clear_istatus(struct aac_softc *sc, int mask); +static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, + u_int32_t arg2, u_int32_t arg3); +static int aac_sa_get_mailbox(struct aac_softc *sc, int mb); +static void aac_sa_set_interrupts(struct aac_softc *sc, int enable); + +struct aac_interface aac_sa_interface = { + aac_sa_get_fwstatus, + aac_sa_qnotify, + aac_sa_get_istatus, + aac_sa_clear_istatus, + aac_sa_set_mailbox, + aac_sa_get_mailbox, + aac_sa_set_interrupts +}; + +/* i960Rx interface */ +static int aac_rx_get_fwstatus(struct aac_softc *sc); +static void aac_rx_qnotify(struct aac_softc *sc, int qbit); +static int aac_rx_get_istatus(struct aac_softc *sc); +static void aac_rx_clear_istatus(struct aac_softc *sc, int mask); +static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, + u_int32_t arg2, u_int32_t arg3); +static int aac_rx_get_mailbox(struct aac_softc *sc, int mb); +static void aac_rx_set_interrupts(struct aac_softc *sc, int enable); + +struct aac_interface aac_rx_interface = { + aac_rx_get_fwstatus, + aac_rx_qnotify, + aac_rx_get_istatus, + aac_rx_clear_istatus, + aac_rx_set_mailbox, + aac_rx_get_mailbox, + aac_rx_set_interrupts +}; + +/* Debugging and Diagnostics */ +static void aac_describe_controller(struct aac_softc *sc); +static char *aac_describe_code(struct aac_code_lookup *table, + u_int32_t code); + +/* Management Interface */ +static d_open_t aac_open; +static d_close_t aac_close; +static d_ioctl_t aac_ioctl; +static d_poll_t aac_poll; +static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib); +static void aac_handle_aif(struct aac_softc *sc, + struct aac_fib *fib); +static int aac_rev_check(struct aac_softc *sc, caddr_t udata); +static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg); +static int aac_return_aif(struct aac_softc *sc, caddr_t uptr); +static int aac_query_disk(struct aac_softc *sc, caddr_t uptr); + +#define AAC_CDEV_MAJOR 150 + +static struct cdevsw aac_cdevsw = { + .d_open = aac_open, + .d_close = aac_close, + .d_ioctl = aac_ioctl, + .d_poll = aac_poll, + .d_name = "aac", + .d_maj = AAC_CDEV_MAJOR, +}; + +MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver"); + +/* sysctl node */ +SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters"); + +/* + * Device Interface + */ + +/* + * Initialise the controller and softc + */ +int +aac_attach(struct aac_softc *sc) +{ + int error, unit; + + debug_called(1); + + /* + * Initialise per-controller queues. + */ + aac_initq_free(sc); + aac_initq_ready(sc); + aac_initq_busy(sc); + aac_initq_bio(sc); + + /* + * Initialise command-completion task. + */ + TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc); + + /* disable interrupts before we enable anything */ + AAC_MASK_INTERRUPTS(sc); + + /* mark controller as suspended until we get ourselves organised */ + sc->aac_state |= AAC_STATE_SUSPEND; + + /* + * Check that the firmware on the card is supported. + */ + if ((error = aac_check_firmware(sc)) != 0) + return(error); + + /* Init the sync fib lock */ + AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock"); + + /* + * Initialise the adapter. + */ + if ((error = aac_init(sc)) != 0) + return(error); + + /* + * Print a little information about the controller. + */ + aac_describe_controller(sc); + + /* + * Initialize locks + */ + AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock"); + TAILQ_INIT(&sc->aac_container_tqh); + AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock"); + AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock"); + + /* + * Register to probe our containers later. + */ + sc->aac_ich.ich_func = aac_startup; + sc->aac_ich.ich_arg = sc; + if (config_intrhook_establish(&sc->aac_ich) != 0) { + device_printf(sc->aac_dev, + "can't establish configuration hook\n"); + return(ENXIO); + } + + /* + * Make the control device. + */ + unit = device_get_unit(sc->aac_dev); + sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR, + 0640, "aac%d", unit); + (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit); + (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit); + sc->aac_dev_t->si_drv1 = sc; + + /* Create the AIF thread */ + if (kthread_create((void(*)(void *))aac_command_thread, sc, + &sc->aifthread, 0, 0, "aac%daif", unit)) + panic("Could not create AIF thread\n"); + + /* Register the shutdown method to only be called post-dump */ + if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown, + sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL) + device_printf(sc->aac_dev, + "shutdown event registration failed\n"); + + /* Register with CAM for the non-DASD devices */ + if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) { + TAILQ_INIT(&sc->aac_sim_tqh); + aac_get_bus_info(sc); + } + + return(0); +} + +/* + * Probe for containers, create disks. + */ +static void +aac_startup(void *arg) +{ + struct aac_softc *sc; + struct aac_fib *fib; + struct aac_mntinfo *mi; + struct aac_mntinforesp *mir = NULL; + int count = 0, i = 0; + + debug_called(1); + + sc = (struct aac_softc *)arg; + + /* disconnect ourselves from the intrhook chain */ + config_intrhook_disestablish(&sc->aac_ich); + + aac_alloc_sync_fib(sc, &fib, 0); + mi = (struct aac_mntinfo *)&fib->data[0]; + + /* loop over possible containers */ + do { + /* request information on this container */ + bzero(mi, sizeof(struct aac_mntinfo)); + mi->Command = VM_NameServe; + mi->MntType = FT_FILESYS; + mi->MntCount = i; + if (aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_mntinfo))) { + printf("error probing container %d", i); + continue; + } + + mir = (struct aac_mntinforesp *)&fib->data[0]; + /* XXX Need to check if count changed */ + count = mir->MntRespCount; + aac_add_container(sc, mir, 0); + i++; + } while ((i < count) && (i < AAC_MAX_CONTAINERS)); + + aac_release_sync_fib(sc); + + /* poke the bus to actually attach the child devices */ + if (bus_generic_attach(sc->aac_dev)) + device_printf(sc->aac_dev, "bus_generic_attach failed\n"); + + /* mark the controller up */ + sc->aac_state &= ~AAC_STATE_SUSPEND; + + /* enable interrupts now */ + AAC_UNMASK_INTERRUPTS(sc); +} + +/* + * Create a device to respresent a new container + */ +static void +aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f) +{ + struct aac_container *co; + device_t child; + + /* + * Check container volume type for validity. Note that many of + * the possible types may never show up. + */ + if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) { + co = (struct aac_container *)malloc(sizeof *co, M_AACBUF, + M_NOWAIT | M_ZERO); + if (co == NULL) + panic("Out of memory?!\n"); + debug(1, "id %x name '%.16s' size %u type %d", + mir->MntTable[0].ObjectId, + mir->MntTable[0].FileSystemName, + mir->MntTable[0].Capacity, mir->MntTable[0].VolType); + + if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL) + device_printf(sc->aac_dev, "device_add_child failed\n"); + else + device_set_ivars(child, co); + device_set_desc(child, aac_describe_code(aac_container_types, + mir->MntTable[0].VolType)); + co->co_disk = child; + co->co_found = f; + bcopy(&mir->MntTable[0], &co->co_mntobj, + sizeof(struct aac_mntobj)); + AAC_LOCK_ACQUIRE(&sc->aac_container_lock); + TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link); + AAC_LOCK_RELEASE(&sc->aac_container_lock); + } +} + +/* + * Free all of the resources associated with (sc) + * + * Should not be called if the controller is active. + */ +void +aac_free(struct aac_softc *sc) +{ + + debug_called(1); + + /* remove the control device */ + if (sc->aac_dev_t != NULL) + destroy_dev(sc->aac_dev_t); + + /* throw away any FIB buffers, discard the FIB DMA tag */ + aac_free_commands(sc); + if (sc->aac_fib_dmat) + bus_dma_tag_destroy(sc->aac_fib_dmat); + + free(sc->aac_commands, M_AACBUF); + + /* destroy the common area */ + if (sc->aac_common) { + bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap); + bus_dmamem_free(sc->aac_common_dmat, sc->aac_common, + sc->aac_common_dmamap); + } + if (sc->aac_common_dmat) + bus_dma_tag_destroy(sc->aac_common_dmat); + + /* disconnect the interrupt handler */ + if (sc->aac_intr) + bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr); + if (sc->aac_irq != NULL) + bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid, + sc->aac_irq); + + /* destroy data-transfer DMA tag */ + if (sc->aac_buffer_dmat) + bus_dma_tag_destroy(sc->aac_buffer_dmat); + + /* destroy the parent DMA tag */ + if (sc->aac_parent_dmat) + bus_dma_tag_destroy(sc->aac_parent_dmat); + + /* release the register window mapping */ + if (sc->aac_regs_resource != NULL) + bus_release_resource(sc->aac_dev, SYS_RES_MEMORY, + sc->aac_regs_rid, sc->aac_regs_resource); +} + +/* + * Disconnect from the controller completely, in preparation for unload. + */ +int +aac_detach(device_t dev) +{ + struct aac_softc *sc; + struct aac_container *co; + struct aac_sim *sim; + int error; + + debug_called(1); + + sc = device_get_softc(dev); + + if (sc->aac_state & AAC_STATE_OPEN) + return(EBUSY); + + /* Remove the child containers */ + while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) { + error = device_delete_child(dev, co->co_disk); + if (error) + return (error); + TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link); + free(co, M_AACBUF); + } + + /* Remove the CAM SIMs */ + while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) { + TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link); + error = device_delete_child(dev, sim->sim_dev); + if (error) + return (error); + free(sim, M_AACBUF); + } + + if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { + sc->aifflags |= AAC_AIFFLAGS_EXIT; + wakeup(sc->aifthread); + tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz); + } + + if (sc->aifflags & AAC_AIFFLAGS_RUNNING) + panic("Cannot shutdown AIF thread\n"); + + if ((error = aac_shutdown(dev))) + return(error); + + EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh); + + aac_free(sc); + + return(0); +} + +/* + * Bring the controller down to a dormant state and detach all child devices. + * + * This function is called before detach or system shutdown. + * + * Note that we can assume that the bioq on the controller is empty, as we won't + * allow shutdown if any device is open. + */ +int +aac_shutdown(device_t dev) +{ + struct aac_softc *sc; + struct aac_fib *fib; + struct aac_close_command *cc; + + debug_called(1); + + sc = device_get_softc(dev); + + sc->aac_state |= AAC_STATE_SUSPEND; + + /* + * Send a Container shutdown followed by a HostShutdown FIB to the + * controller to convince it that we don't want to talk to it anymore. + * We've been closed and all I/O completed already + */ + device_printf(sc->aac_dev, "shutting down controller..."); + + aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE); + cc = (struct aac_close_command *)&fib->data[0]; + + bzero(cc, sizeof(struct aac_close_command)); + cc->Command = VM_CloseAll; + cc->ContainerId = 0xffffffff; + if (aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_close_command))) + printf("FAILED.\n"); + else + printf("done\n"); +#if 0 + else { + fib->data[0] = 0; + /* + * XXX Issuing this command to the controller makes it shut down + * but also keeps it from coming back up without a reset of the + * PCI bus. This is not desirable if you are just unloading the + * driver module with the intent to reload it later. + */ + if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN, + fib, 1)) { + printf("FAILED.\n"); + } else { + printf("done.\n"); + } + } +#endif + + AAC_MASK_INTERRUPTS(sc); + + return(0); +} + +/* + * Bring the controller to a quiescent state, ready for system suspend. + */ +int +aac_suspend(device_t dev) +{ + struct aac_softc *sc; + + debug_called(1); + + sc = device_get_softc(dev); + + sc->aac_state |= AAC_STATE_SUSPEND; + + AAC_MASK_INTERRUPTS(sc); + return(0); +} + +/* + * Bring the controller back to a state ready for operation. + */ +int +aac_resume(device_t dev) +{ + struct aac_softc *sc; + + debug_called(1); + + sc = device_get_softc(dev); + + sc->aac_state &= ~AAC_STATE_SUSPEND; + AAC_UNMASK_INTERRUPTS(sc); + return(0); +} + +/* + * Take an interrupt. + */ +void +aac_intr(void *arg) +{ + struct aac_softc *sc; + u_int32_t *resp_queue; + u_int16_t reason; + + debug_called(2); + + sc = (struct aac_softc *)arg; + + /* + * Optimize the common case of adapter response interrupts. + * We must read from the card prior to processing the responses + * to ensure the clear is flushed prior to accessing the queues. + * Reading the queues from local memory might save us a PCI read. + */ + resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE]; + if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX]) + reason = AAC_DB_RESPONSE_READY; + else + reason = AAC_GET_ISTATUS(sc); + AAC_CLEAR_ISTATUS(sc, reason); + (void)AAC_GET_ISTATUS(sc); + + /* It's not ok to return here because of races with the previous step */ + if (reason & AAC_DB_RESPONSE_READY) + /* handle completion processing */ + taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete); + + /* controller wants to talk to the log */ + if (reason & AAC_DB_PRINTF) { + if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { + sc->aifflags |= AAC_AIFFLAGS_PRINTF; + } else + aac_print_printf(sc); + } + + /* controller has a message for us? */ + if (reason & AAC_DB_COMMAND_READY) { + if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { + sc->aifflags |= AAC_AIFFLAGS_AIF; + } else { + /* + * XXX If the kthread is dead and we're at this point, + * there are bigger problems than just figuring out + * what to do with an AIF. + */ + } + + } + + if ((sc->aifflags & AAC_AIFFLAGS_PENDING) != 0) + /* XXX Should this be done with cv_signal? */ + wakeup(sc->aifthread); +} + +/* + * Command Processing + */ + +/* + * Start as much queued I/O as possible on the controller + */ +void +aac_startio(struct aac_softc *sc) +{ + struct aac_command *cm; + + debug_called(2); + + for (;;) { + /* + * Try to get a command that's been put off for lack of + * resources + */ + cm = aac_dequeue_ready(sc); + + /* + * Try to build a command off the bio queue (ignore error + * return) + */ + if (cm == NULL) + aac_bio_command(sc, &cm); + + /* nothing to do? */ + if (cm == NULL) + break; + + /* try to give the command to the controller */ + if (aac_start(cm) == EBUSY) { + /* put it on the ready queue for later */ + aac_requeue_ready(cm); + break; + } + } +} + +/* + * Deliver a command to the controller; allocate controller resources at the + * last moment when possible. + */ +static int +aac_start(struct aac_command *cm) +{ + struct aac_softc *sc; + int error; + + debug_called(2); + + sc = cm->cm_sc; + + /* get the command mapped */ + aac_map_command(cm); + + /* Fix up the address values in the FIB. Use the command array index + * instead of a pointer since these fields are only 32 bits. Shift + * the SenderFibAddress over to make room for the fast response bit. + */ + cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 1); + cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys; + + /* save a pointer to the command for speedy reverse-lookup */ + cm->cm_fib->Header.SenderData = cm->cm_index; + /* put the FIB on the outbound queue */ + error = aac_enqueue_fib(sc, cm->cm_queue, cm); + return(error); +} + +/* + * Handle notification of one or more FIBs coming from the controller. + */ +static void +aac_command_thread(struct aac_softc *sc) +{ + struct aac_fib *fib; + u_int32_t fib_size; + int size; + + debug_called(2); + + sc->aifflags |= AAC_AIFFLAGS_RUNNING; + + while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) { + if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) + tsleep(sc->aifthread, PRIBIO, "aifthd", + AAC_PERIODIC_INTERVAL * hz); + + if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) + aac_timeout(sc); + + /* Check the hardware printf message buffer */ + if ((sc->aifflags & AAC_AIFFLAGS_PRINTF) != 0) { + sc->aifflags &= ~AAC_AIFFLAGS_PRINTF; + aac_print_printf(sc); + } + + /* See if any FIBs need to be allocated */ + if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) { + AAC_LOCK_ACQUIRE(&sc->aac_io_lock); + aac_alloc_commands(sc); + sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS; + AAC_LOCK_RELEASE(&sc->aac_io_lock); + } + + /* While we're here, check to see if any commands are stuck */ + while (sc->aifflags & AAC_AIFFLAGS_AIF) { + if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE, + &fib_size, &fib)) { + sc->aifflags &= ~AAC_AIFFLAGS_AIF; + break; /* nothing to do */ + } + + AAC_PRINT_FIB(sc, fib); + + switch (fib->Header.Command) { + case AifRequest: + aac_handle_aif(sc, fib); + break; + default: + device_printf(sc->aac_dev, "unknown command " + "from controller\n"); + break; + } + + if ((fib->Header.XferState == 0) || + (fib->Header.StructType != AAC_FIBTYPE_TFIB)) + break; + + /* Return the AIF to the controller. */ + if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) { + fib->Header.XferState |= AAC_FIBSTATE_DONEHOST; + *(AAC_FSAStatus*)fib->data = ST_OK; + + /* XXX Compute the Size field? */ + size = fib->Header.Size; + if (size > sizeof(struct aac_fib)) { + size = sizeof(struct aac_fib); + fib->Header.Size = size; + } + /* + * Since we did not generate this command, it + * cannot go through the normal + * enqueue->startio chain. + */ + aac_enqueue_response(sc, + AAC_ADAP_NORM_RESP_QUEUE, + fib); + } + } + } + sc->aifflags &= ~AAC_AIFFLAGS_RUNNING; + wakeup(sc->aac_dev); + + mtx_lock(&Giant); + kthread_exit(0); +} + +/* + * Process completed commands. + */ +static void +aac_complete(void *context, int pending) +{ + struct aac_softc *sc; + struct aac_command *cm; + struct aac_fib *fib; + u_int32_t fib_size; + + debug_called(2); + + sc = (struct aac_softc *)context; + + AAC_LOCK_ACQUIRE(&sc->aac_io_lock); + + /* pull completed commands off the queue */ + for (;;) { + /* look for completed FIBs on our queue */ + if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size, + &fib)) + break; /* nothing to do */ + + /* get the command, unmap and queue for later processing */ + cm = sc->aac_commands + fib->Header.SenderData; + if (cm == NULL) { + AAC_PRINT_FIB(sc, fib); + break; + } + + aac_remove_busy(cm); + aac_unmap_command(cm); /* XXX defer? */ + cm->cm_flags |= AAC_CMD_COMPLETED; + + /* is there a completion handler? */ + if (cm->cm_complete != NULL) { + cm->cm_complete(cm); + } else { + /* assume that someone is sleeping on this command */ + wakeup(cm); + } + } + + /* see if we can start some more I/O */ + aac_startio(sc); + + AAC_LOCK_RELEASE(&sc->aac_io_lock); +} + +/* + * Handle a bio submitted from a disk device. + */ +void +aac_submit_bio(struct bio *bp) +{ + struct aac_disk *ad; + struct aac_softc *sc; + + debug_called(2); + + ad = (struct aac_disk *)bp->bio_disk->d_drv1; + sc = ad->ad_controller; + + /* queue the BIO and try to get some work done */ + aac_enqueue_bio(sc, bp); + aac_startio(sc); +} + +/* + * Get a bio and build a command to go with it. + */ +static int +aac_bio_command(struct aac_softc *sc, struct aac_command **cmp) +{ + struct aac_command *cm; + struct aac_fib *fib; + struct aac_disk *ad; + struct bio *bp; + + debug_called(2); + + /* get the resources we will need */ + cm = NULL; + if ((bp = aac_dequeue_bio(sc)) == NULL) + goto fail; + if (aac_alloc_command(sc, &cm)) /* get a command */ + goto fail; + + /* fill out the command */ + cm->cm_data = (void *)bp->bio_data; + cm->cm_datalen = bp->bio_bcount; + cm->cm_complete = aac_bio_complete; + cm->cm_private = bp; + cm->cm_timestamp = time_second; + cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; + + /* build the FIB */ + fib = cm->cm_fib; + fib->Header.Size = sizeof(struct aac_fib_header); + fib->Header.XferState = + AAC_FIBSTATE_HOSTOWNED | + AAC_FIBSTATE_INITIALISED | + AAC_FIBSTATE_EMPTY | + AAC_FIBSTATE_FROMHOST | + AAC_FIBSTATE_REXPECTED | + AAC_FIBSTATE_NORM | + AAC_FIBSTATE_ASYNC | + AAC_FIBSTATE_FAST_RESPONSE; + + /* build the read/write request */ + ad = (struct aac_disk *)bp->bio_disk->d_drv1; + + if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) { + fib->Header.Command = ContainerCommand; + if (bp->bio_cmd == BIO_READ) { + struct aac_blockread *br; + br = (struct aac_blockread *)&fib->data[0]; + br->Command = VM_CtBlockRead; + br->ContainerId = ad->ad_container->co_mntobj.ObjectId; + br->BlockNumber = bp->bio_pblkno; + br->ByteCount = bp->bio_bcount; + fib->Header.Size += sizeof(struct aac_blockread); + cm->cm_sgtable = &br->SgMap; + cm->cm_flags |= AAC_CMD_DATAIN; + } else { + struct aac_blockwrite *bw; + bw = (struct aac_blockwrite *)&fib->data[0]; + bw->Command = VM_CtBlockWrite; + bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; + bw->BlockNumber = bp->bio_pblkno; + bw->ByteCount = bp->bio_bcount; + bw->Stable = CUNSTABLE; + fib->Header.Size += sizeof(struct aac_blockwrite); + cm->cm_flags |= AAC_CMD_DATAOUT; + cm->cm_sgtable = &bw->SgMap; + } + } else { + fib->Header.Command = ContainerCommand64; + if (bp->bio_cmd == BIO_READ) { + struct aac_blockread64 *br; + br = (struct aac_blockread64 *)&fib->data[0]; + br->Command = VM_CtHostRead64; + br->ContainerId = ad->ad_container->co_mntobj.ObjectId; + br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE; + br->BlockNumber = bp->bio_pblkno; + br->Pad = 0; + br->Flags = 0; + fib->Header.Size += sizeof(struct aac_blockread64); + cm->cm_flags |= AAC_CMD_DATAOUT; + (struct aac_sg_table64 *)cm->cm_sgtable = &br->SgMap64; + } else { + struct aac_blockwrite64 *bw; + bw = (struct aac_blockwrite64 *)&fib->data[0]; + bw->Command = VM_CtHostWrite64; + bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; + bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE; + bw->BlockNumber = bp->bio_pblkno; + bw->Pad = 0; + bw->Flags = 0; + fib->Header.Size += sizeof(struct aac_blockwrite64); + cm->cm_flags |= AAC_CMD_DATAIN; + (struct aac_sg_table64 *)cm->cm_sgtable = &bw->SgMap64; + } + } + + *cmp = cm; + return(0); + +fail: + if (bp != NULL) + aac_enqueue_bio(sc, bp); + if (cm != NULL) + aac_release_command(cm); + return(ENOMEM); +} + +/* + * Handle a bio-instigated command that has been completed. + */ +static void +aac_bio_complete(struct aac_command *cm) +{ + struct aac_blockread_response *brr; + struct aac_blockwrite_response *bwr; + struct bio *bp; + AAC_FSAStatus status; + + /* fetch relevant status and then release the command */ + bp = (struct bio *)cm->cm_private; + if (bp->bio_cmd == BIO_READ) { + brr = (struct aac_blockread_response *)&cm->cm_fib->data[0]; + status = brr->Status; + } else { + bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0]; + status = bwr->Status; + } + aac_release_command(cm); + + /* fix up the bio based on status */ + if (status == ST_OK) { + bp->bio_resid = 0; + } else { + bp->bio_error = EIO; + bp->bio_flags |= BIO_ERROR; + /* pass an error string out to the disk layer */ + bp->bio_driver1 = aac_describe_code(aac_command_status_table, + status); + } + aac_biodone(bp); +} + +/* + * Submit a command to the controller, return when it completes. + * XXX This is very dangerous! If the card has gone out to lunch, we could + * be stuck here forever. At the same time, signals are not caught + * because there is a risk that a signal could wakeup the tsleep before + * the card has a chance to complete the command. The passed in timeout + * is ignored for the same reason. Since there is no way to cancel a + * command in progress, we should probably create a 'dead' queue where + * commands go that have been interrupted/timed-out/etc, that keeps them + * out of the free pool. That way, if the card is just slow, it won't + * spam the memory of a command that has been recycled. + */ +static int +aac_wait_command(struct aac_command *cm, int timeout) +{ + struct aac_softc *sc; + int error = 0; + + debug_called(2); + + sc = cm->cm_sc; + + /* Put the command on the ready queue and get things going */ + cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; + aac_enqueue_ready(cm); + aac_startio(sc); + while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) { + error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0); + } + return(error); +} + +/* + *Command Buffer Management + */ + +/* + * Allocate a command. + */ +int +aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp) +{ + struct aac_command *cm; + + debug_called(3); + + if ((cm = aac_dequeue_free(sc)) == NULL) { + if (sc->total_fibs < sc->aac_max_fibs) { + sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS; + wakeup(sc->aifthread); + } + return (EBUSY); + } + + *cmp = cm; + return(0); +} + +/* + * Release a command back to the freelist. + */ +void +aac_release_command(struct aac_command *cm) +{ + debug_called(3); + + /* (re)initialise the command/FIB */ + cm->cm_sgtable = NULL; + cm->cm_flags = 0; + cm->cm_complete = NULL; + cm->cm_private = NULL; + cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY; + cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB; + cm->cm_fib->Header.Flags = 0; + cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib); + + /* + * These are duplicated in aac_start to cover the case where an + * intermediate stage may have destroyed them. They're left + * initialised here for debugging purposes only. + */ + cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib; + cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys; + cm->cm_fib->Header.SenderData = 0; + + aac_enqueue_free(cm); +} + +/* + * Map helper for command/FIB allocation. + */ +static void +aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) +{ + uint32_t *fibphys; + + fibphys = (uint32_t *)arg; + + debug_called(3); + + *fibphys = segs[0].ds_addr; +} + +/* + * Allocate and initialise commands/FIBs for this adapter. + */ +static int +aac_alloc_commands(struct aac_softc *sc) +{ + struct aac_command *cm; + struct aac_fibmap *fm; + uint32_t fibphys; + int i, error; + + debug_called(2); + + if (sc->total_fibs + AAC_FIB_COUNT > sc->aac_max_fibs) + return (ENOMEM); + + fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO); + if (fm == NULL) + return (ENOMEM); + + /* allocate the FIBs in DMAable memory and load them */ + if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs, + BUS_DMA_NOWAIT, &fm->aac_fibmap)) { + device_printf(sc->aac_dev, + "Not enough contiguous memory available.\n"); + free(fm, M_AACBUF); + return (ENOMEM); + } + + bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs, + AAC_FIB_COUNT * sizeof(struct aac_fib), + aac_map_command_helper, &fibphys, 0); + + /* initialise constant fields in the command structure */ + bzero(fm->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib)); + for (i = 0; i < AAC_FIB_COUNT; i++) { + cm = sc->aac_commands + sc->total_fibs; + fm->aac_commands = cm; + cm->cm_sc = sc; + cm->cm_fib = fm->aac_fibs + i; + cm->cm_fibphys = fibphys + (i * sizeof(struct aac_fib)); + cm->cm_index = sc->total_fibs; + + if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0, + &cm->cm_datamap)) == 0) + aac_release_command(cm); + else + break; + sc->total_fibs++; + } + + if (i > 0) { + TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link); + debug(1, "total_fibs= %d\n", sc->total_fibs); + return (0); + } + + bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); + bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); + free(fm, M_AACBUF); + return (ENOMEM); +} + +/* + * Free FIBs owned by this adapter. + */ +static void +aac_free_commands(struct aac_softc *sc) +{ + struct aac_fibmap *fm; + struct aac_command *cm; + int i; + + debug_called(1); + + while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) { + + TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link); + /* + * We check against total_fibs to handle partially + * allocated blocks. + */ + for (i = 0; i < AAC_FIB_COUNT && sc->total_fibs--; i++) { + cm = fm->aac_commands + i; + bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap); + } + bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); + bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); + free(fm, M_AACBUF); + } +} + +/* + * Command-mapping helper function - populate this command's s/g table. + */ +static void +aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) +{ + struct aac_command *cm; + struct aac_fib *fib; + int i; + + debug_called(3); + + cm = (struct aac_command *)arg; + fib = cm->cm_fib; + + /* copy into the FIB */ + if (cm->cm_sgtable != NULL) { + if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) { + struct aac_sg_table *sg; + sg = cm->cm_sgtable; + sg->SgCount = nseg; + for (i = 0; i < nseg; i++) { + sg->SgEntry[i].SgAddress = segs[i].ds_addr; + sg->SgEntry[i].SgByteCount = segs[i].ds_len; + } + /* update the FIB size for the s/g count */ + fib->Header.Size += nseg * sizeof(struct aac_sg_entry); + } else { + struct aac_sg_table64 *sg; + sg = (struct aac_sg_table64 *)cm->cm_sgtable; + sg->SgCount = nseg; + for (i = 0; i < nseg; i++) { + sg->SgEntry64[i].SgAddress = segs[i].ds_addr; + sg->SgEntry64[i].SgByteCount = segs[i].ds_len; + } + /* update the FIB size for the s/g count */ + fib->Header.Size += nseg*sizeof(struct aac_sg_entry64); + } + } +} + +/* + * Map a command into controller-visible space. + */ +static void +aac_map_command(struct aac_command *cm) +{ + struct aac_softc *sc; + + debug_called(2); + + sc = cm->cm_sc; + + /* don't map more than once */ + if (cm->cm_flags & AAC_CMD_MAPPED) + return; + + if (cm->cm_datalen != 0) { + bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap, + cm->cm_data, cm->cm_datalen, + aac_map_command_sg, cm, 0); + + if (cm->cm_flags & AAC_CMD_DATAIN) + bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, + BUS_DMASYNC_PREREAD); + if (cm->cm_flags & AAC_CMD_DATAOUT) + bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, + BUS_DMASYNC_PREWRITE); + } + cm->cm_flags |= AAC_CMD_MAPPED; +} + +/* + * Unmap a command from controller-visible space. + */ +static void +aac_unmap_command(struct aac_command *cm) +{ + struct aac_softc *sc; + + debug_called(2); + + sc = cm->cm_sc; + + if (!(cm->cm_flags & AAC_CMD_MAPPED)) + return; + + if (cm->cm_datalen != 0) { + if (cm->cm_flags & AAC_CMD_DATAIN) + bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, + BUS_DMASYNC_POSTREAD); + if (cm->cm_flags & AAC_CMD_DATAOUT) + bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, + BUS_DMASYNC_POSTWRITE); + + bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap); + } + cm->cm_flags &= ~AAC_CMD_MAPPED; +} + +/* + * Hardware Interface + */ + +/* + * Initialise the adapter. + */ +static void +aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error) +{ + struct aac_softc *sc; + + debug_called(1); + + sc = (struct aac_softc *)arg; + + sc->aac_common_busaddr = segs[0].ds_addr; +} + +static int +aac_check_firmware(struct aac_softc *sc) +{ + u_int32_t major, minor, options; + + debug_called(1); + + /* + * Retrieve the firmware version numbers. Dell PERC2/QC cards with + * firmware version 1.x are not compatible with this driver. + */ + if (sc->flags & AAC_FLAGS_PERC2QC) { + if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0, + NULL)) { + device_printf(sc->aac_dev, + "Error reading firmware version\n"); + return (EIO); + } + + /* These numbers are stored as ASCII! */ + major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30; + minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30; + if (major == 1) { + device_printf(sc->aac_dev, + "Firmware version %d.%d is not supported.\n", + major, minor); + return (EINVAL); + } + } + + /* + * Retrieve the capabilities/supported options word so we know what + * work-arounds to enable. + */ + if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) { + device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); + return (EIO); + } + options = AAC_GET_MAILBOX(sc, 1); + sc->supported_options = options; + + if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 && + (sc->flags & AAC_FLAGS_NO4GB) == 0) + sc->flags |= AAC_FLAGS_4GB_WINDOW; + if (options & AAC_SUPPORTED_NONDASD) + sc->flags |= AAC_FLAGS_ENABLE_CAM; + if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0 && (sizeof(bus_addr_t) > 4)) { + device_printf(sc->aac_dev, "Enabling 64-bit address support\n"); + sc->flags |= AAC_FLAGS_SG_64BIT; + } + + /* Check for broken hardware that does a lower number of commands */ + if ((sc->flags & AAC_FLAGS_256FIBS) == 0) + sc->aac_max_fibs = AAC_MAX_FIBS; + else + sc->aac_max_fibs = 256; + + return (0); +} + +static int +aac_init(struct aac_softc *sc) +{ + struct aac_adapter_init *ip; + time_t then; + u_int32_t code; + u_int8_t *qaddr; + int error; + + debug_called(1); + + /* + * First wait for the adapter to come ready. + */ + then = time_second; + do { + code = AAC_GET_FWSTATUS(sc); + if (code & AAC_SELF_TEST_FAILED) { + device_printf(sc->aac_dev, "FATAL: selftest failed\n"); + return(ENXIO); + } + if (code & AAC_KERNEL_PANIC) { + device_printf(sc->aac_dev, + "FATAL: controller kernel panic\n"); + return(ENXIO); + } + if (time_second > (then + AAC_BOOT_TIMEOUT)) { + device_printf(sc->aac_dev, + "FATAL: controller not coming ready, " + "status %x\n", code); + return(ENXIO); + } + } while (!(code & AAC_UP_AND_RUNNING)); + + error = ENOMEM; + /* + * Create DMA tag for mapping buffers into controller-addressable space. + */ + if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ + 1, 0, /* algnmnt, boundary */ + (sc->flags & AAC_FLAGS_SG_64BIT) ? + BUS_SPACE_MAXADDR : + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ + BUS_SPACE_MAXADDR, /* highaddr */ + NULL, NULL, /* filter, filterarg */ + MAXBSIZE, /* maxsize */ + AAC_MAXSGENTRIES, /* nsegments */ + MAXBSIZE, /* maxsegsize */ + BUS_DMA_ALLOCNOW, /* flags */ + &sc->aac_buffer_dmat)) { + device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n"); + goto out; + } + + /* + * Create DMA tag for mapping FIBs into controller-addressable space.. + */ + if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ + 1, 0, /* algnmnt, boundary */ + (sc->flags & AAC_FLAGS_4GB_WINDOW) ? + BUS_SPACE_MAXADDR_32BIT : + 0x7fffffff, /* lowaddr */ + BUS_SPACE_MAXADDR, /* highaddr */ + NULL, NULL, /* filter, filterarg */ + AAC_FIB_COUNT * + sizeof(struct aac_fib), /* maxsize */ + 1, /* nsegments */ + AAC_FIB_COUNT * + sizeof(struct aac_fib), /* maxsegsize */ + BUS_DMA_ALLOCNOW, /* flags */ + &sc->aac_fib_dmat)) { + device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");; + goto out; + } + + /* + * Create DMA tag for the common structure and allocate it. + */ + if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ + 1, 0, /* algnmnt, boundary */ + (sc->flags & AAC_FLAGS_4GB_WINDOW) ? + BUS_SPACE_MAXADDR_32BIT : + 0x7fffffff, /* lowaddr */ + BUS_SPACE_MAXADDR, /* highaddr */ + NULL, NULL, /* filter, filterarg */ + 8192 + sizeof(struct aac_common), /* maxsize */ + 1, /* nsegments */ + BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ + BUS_DMA_ALLOCNOW, /* flags */ + &sc->aac_common_dmat)) { + device_printf(sc->aac_dev, + "can't allocate common structure DMA tag\n"); + goto out; + } + if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common, + BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) { + device_printf(sc->aac_dev, "can't allocate common structure\n"); + goto out; + } + + /* + * Work around a bug in the 2120 and 2200 that cannot DMA commands + * below address 8192 in physical memory. + * XXX If the padding is not needed, can it be put to use instead + * of ignored? + */ + bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap, + sc->aac_common, 8192 + sizeof(*sc->aac_common), + aac_common_map, sc, 0); + + if (sc->aac_common_busaddr < 8192) { + (uint8_t *)sc->aac_common += 8192; + sc->aac_common_busaddr += 8192; + } + bzero(sc->aac_common, sizeof(*sc->aac_common)); + + /* Allocate some FIBs and associated command structs */ + TAILQ_INIT(&sc->aac_fibmap_tqh); + sc->aac_commands = malloc(AAC_MAX_FIBS * sizeof(struct aac_command), + M_AACBUF, M_WAITOK|M_ZERO); + while (sc->total_fibs < AAC_PREALLOCATE_FIBS) { + if (aac_alloc_commands(sc) != 0) + break; + } + if (sc->total_fibs == 0) + goto out; + + /* + * Fill in the init structure. This tells the adapter about the + * physical location of various important shared data structures. + */ + ip = &sc->aac_common->ac_init; + ip->InitStructRevision = AAC_INIT_STRUCT_REVISION; + ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION; + + ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr + + offsetof(struct aac_common, ac_fibs); + ip->AdapterFibsVirtualAddress = 0; + ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib); + ip->AdapterFibAlign = sizeof(struct aac_fib); + + ip->PrintfBufferAddress = sc->aac_common_busaddr + + offsetof(struct aac_common, ac_printf); + ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE; + + /* The adapter assumes that pages are 4K in size */ + ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE; + ip->HostElapsedSeconds = time_second; /* reset later if invalid */ + + /* + * Initialise FIB queues. Note that it appears that the layout of the + * indexes and the segmentation of the entries may be mandated by the + * adapter, which is only told about the base of the queue index fields. + * + * The initial values of the indices are assumed to inform the adapter + * of the sizes of the respective queues, and theoretically it could + * work out the entire layout of the queue structures from this. We + * take the easy route and just lay this area out like everyone else + * does. + * + * The Linux driver uses a much more complex scheme whereby several + * header records are kept for each queue. We use a couple of generic + * list manipulation functions which 'know' the size of each list by + * virtue of a table. + */ + qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN; + qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN; + sc->aac_queues = (struct aac_queue_table *)qaddr; + ip->CommHeaderAddress = sc->aac_common_busaddr + + ((u_int32_t)sc->aac_queues - + (u_int32_t)sc->aac_common); + + sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = + AAC_HOST_NORM_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = + AAC_HOST_NORM_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = + AAC_HOST_HIGH_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = + AAC_HOST_HIGH_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = + AAC_ADAP_NORM_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = + AAC_ADAP_NORM_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = + AAC_ADAP_HIGH_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = + AAC_ADAP_HIGH_CMD_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= + AAC_HOST_NORM_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= + AAC_HOST_NORM_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= + AAC_HOST_HIGH_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= + AAC_HOST_HIGH_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= + AAC_ADAP_NORM_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= + AAC_ADAP_NORM_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= + AAC_ADAP_HIGH_RESP_ENTRIES; + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= + AAC_ADAP_HIGH_RESP_ENTRIES; + sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] = + &sc->aac_queues->qt_HostNormCmdQueue[0]; + sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] = + &sc->aac_queues->qt_HostHighCmdQueue[0]; + sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] = + &sc->aac_queues->qt_AdapNormCmdQueue[0]; + sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] = + &sc->aac_queues->qt_AdapHighCmdQueue[0]; + sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] = + &sc->aac_queues->qt_HostNormRespQueue[0]; + sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] = + &sc->aac_queues->qt_HostHighRespQueue[0]; + sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] = + &sc->aac_queues->qt_AdapNormRespQueue[0]; + sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] = + &sc->aac_queues->qt_AdapHighRespQueue[0]; + + /* + * Do controller-type-specific initialisation + */ + switch (sc->aac_hwif) { + case AAC_HWIF_I960RX: + AAC_SETREG4(sc, AAC_RX_ODBR, ~0); + break; + } + + /* + * Give the init structure to the controller. + */ + if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT, + sc->aac_common_busaddr + + offsetof(struct aac_common, ac_init), 0, 0, 0, + NULL)) { + device_printf(sc->aac_dev, + "error establishing init structure\n"); + error = EIO; + goto out; + } + + error = 0; +out: + return(error); +} + +/* + * Send a synchronous command to the controller and wait for a result. + */ +static int +aac_sync_command(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3, + u_int32_t *sp) +{ + time_t then; + u_int32_t status; + + debug_called(3); + + /* populate the mailbox */ + AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3); + + /* ensure the sync command doorbell flag is cleared */ + AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); + + /* then set it to signal the adapter */ + AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND); + + /* spin waiting for the command to complete */ + then = time_second; + do { + if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) { + debug(1, "timed out"); + return(EIO); + } + } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND)); + + /* clear the completion flag */ + AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); + + /* get the command status */ + status = AAC_GET_MAILBOX(sc, 0); + if (sp != NULL) + *sp = status; + return(0); +} + +/* + * Grab the sync fib area. + */ +int +aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags) +{ + + /* + * If the force flag is set, the system is shutting down, or in + * trouble. Ignore the mutex. + */ + if (!(flags & AAC_SYNC_LOCK_FORCE)) + AAC_LOCK_ACQUIRE(&sc->aac_sync_lock); + + *fib = &sc->aac_common->ac_sync_fib; + + return (1); +} + +/* + * Release the sync fib area. + */ +void +aac_release_sync_fib(struct aac_softc *sc) +{ + + AAC_LOCK_RELEASE(&sc->aac_sync_lock); +} + +/* + * Send a synchronous FIB to the controller and wait for a result. + */ +int +aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate, + struct aac_fib *fib, u_int16_t datasize) +{ + debug_called(3); + + if (datasize > AAC_FIB_DATASIZE) + return(EINVAL); + + /* + * Set up the sync FIB + */ + fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED | + AAC_FIBSTATE_INITIALISED | + AAC_FIBSTATE_EMPTY; + fib->Header.XferState |= xferstate; + fib->Header.Command = command; + fib->Header.StructType = AAC_FIBTYPE_TFIB; + fib->Header.Size = sizeof(struct aac_fib) + datasize; + fib->Header.SenderSize = sizeof(struct aac_fib); + fib->Header.SenderFibAddress = (u_int32_t)fib; + fib->Header.ReceiverFibAddress = sc->aac_common_busaddr + + offsetof(struct aac_common, + ac_sync_fib); + + /* + * Give the FIB to the controller, wait for a response. + */ + if (aac_sync_command(sc, AAC_MONKER_SYNCFIB, + fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) { + debug(2, "IO error"); + return(EIO); + } + + return (0); +} + +/* + * Adapter-space FIB queue manipulation + * + * Note that the queue implementation here is a little funky; neither the PI or + * CI will ever be zero. This behaviour is a controller feature. + */ +static struct { + int size; + int notify; +} aac_qinfo[] = { + {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL}, + {AAC_HOST_HIGH_CMD_ENTRIES, 0}, + {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY}, + {AAC_ADAP_HIGH_CMD_ENTRIES, 0}, + {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL}, + {AAC_HOST_HIGH_RESP_ENTRIES, 0}, + {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY}, + {AAC_ADAP_HIGH_RESP_ENTRIES, 0} +}; + +/* + * Atomically insert an entry into the nominated queue, returns 0 on success or + * EBUSY if the queue is full. + * + * Note: it would be more efficient to defer notifying the controller in + * the case where we may be inserting several entries in rapid succession, + * but implementing this usefully may be difficult (it would involve a + * separate queue/notify interface). + */ +static int +aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm) +{ + u_int32_t pi, ci; + int error; + u_int32_t fib_size; + u_int32_t fib_addr; + + debug_called(3); + + fib_size = cm->cm_fib->Header.Size; + fib_addr = cm->cm_fib->Header.ReceiverFibAddress; + + /* get the producer/consumer indices */ + pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; + ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; + + /* wrap the queue? */ + if (pi >= aac_qinfo[queue].size) + pi = 0; + + /* check for queue full */ + if ((pi + 1) == ci) { + error = EBUSY; + goto out; + } + + /* populate queue entry */ + (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; + (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; + + /* update producer index */ + sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; + + /* + * To avoid a race with its completion interrupt, place this command on + * the busy queue prior to advertising it to the controller. + */ + aac_enqueue_busy(cm); + + /* notify the adapter if we know how */ + if (aac_qinfo[queue].notify != 0) + AAC_QNOTIFY(sc, aac_qinfo[queue].notify); + + error = 0; + +out: + return(error); +} + +/* + * Atomically remove one entry from the nominated queue, returns 0 on + * success or ENOENT if the queue is empty. + */ +static int +aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size, + struct aac_fib **fib_addr) +{ + u_int32_t pi, ci; + u_int32_t fib_index; + int error; + int notify; + + debug_called(3); + + /* get the producer/consumer indices */ + pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; + ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; + + /* check for queue empty */ + if (ci == pi) { + error = ENOENT; + goto out; + } + + notify = 0; + if (ci == pi + 1) + notify++; + + /* wrap the queue? */ + if (ci >= aac_qinfo[queue].size) + ci = 0; + + /* fetch the entry */ + *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size; + + switch (queue) { + case AAC_HOST_NORM_CMD_QUEUE: + case AAC_HOST_HIGH_CMD_QUEUE: + /* + * The aq_fib_addr is only 32 bits wide so it can't be counted + * on to hold an address. For AIF's, the adapter assumes + * that it's giving us an address into the array of AIF fibs. + * Therefore, we have to convert it to an index. + */ + fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr / + sizeof(struct aac_fib); + *fib_addr = &sc->aac_common->ac_fibs[fib_index]; + break; + + case AAC_HOST_NORM_RESP_QUEUE: + case AAC_HOST_HIGH_RESP_QUEUE: + { + struct aac_command *cm; + + /* + * As above, an index is used instead of an actual address. + * Gotta shift the index to account for the fast response + * bit. No other correction is needed since this value was + * originally provided by the driver via the SenderFibAddress + * field. + */ + fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr; + cm = sc->aac_commands + (fib_index >> 1); + *fib_addr = cm->cm_fib; + + /* + * Is this a fast response? If it is, update the fib fields in + * local memory since the whole fib isn't DMA'd back up. + */ + if (fib_index & 0x01) { + (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP; + *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL; + } + break; + } + default: + panic("Invalid queue in aac_dequeue_fib()"); + break; + } + + /* update consumer index */ + sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1; + + /* if we have made the queue un-full, notify the adapter */ + if (notify && (aac_qinfo[queue].notify != 0)) + AAC_QNOTIFY(sc, aac_qinfo[queue].notify); + error = 0; + +out: + return(error); +} + +/* + * Put our response to an Adapter Initialed Fib on the response queue + */ +static int +aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib) +{ + u_int32_t pi, ci; + int error; + u_int32_t fib_size; + u_int32_t fib_addr; + + debug_called(1); + + /* Tell the adapter where the FIB is */ + fib_size = fib->Header.Size; + fib_addr = fib->Header.SenderFibAddress; + fib->Header.ReceiverFibAddress = fib_addr; + + /* get the producer/consumer indices */ + pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; + ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; + + /* wrap the queue? */ + if (pi >= aac_qinfo[queue].size) + pi = 0; + + /* check for queue full */ + if ((pi + 1) == ci) { + error = EBUSY; + goto out; + } + + /* populate queue entry */ + (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; + (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; + + /* update producer index */ + sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; + + /* notify the adapter if we know how */ + if (aac_qinfo[queue].notify != 0) + AAC_QNOTIFY(sc, aac_qinfo[queue].notify); + + error = 0; + +out: + return(error); +} + +/* + * Check for commands that have been outstanding for a suspiciously long time, + * and complain about them. + */ +static void +aac_timeout(struct aac_softc *sc) +{ + struct aac_command *cm; + time_t deadline; + + /* + * Traverse the busy command list, bitch about late commands once + * only. + */ + deadline = time_second - AAC_CMD_TIMEOUT; + TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) { + if ((cm->cm_timestamp < deadline) + /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) { + cm->cm_flags |= AAC_CMD_TIMEDOUT; + device_printf(sc->aac_dev, + "COMMAND %p TIMEOUT AFTER %d SECONDS\n", + cm, (int)(time_second-cm->cm_timestamp)); + AAC_PRINT_FIB(sc, cm->cm_fib); + } + } + + return; +} + +/* + * Interface Function Vectors + */ + +/* + * Read the current firmware status word. + */ +static int +aac_sa_get_fwstatus(struct aac_softc *sc) +{ + debug_called(3); + + return(AAC_GETREG4(sc, AAC_SA_FWSTATUS)); +} + +static int +aac_rx_get_fwstatus(struct aac_softc *sc) +{ + debug_called(3); + + return(AAC_GETREG4(sc, AAC_RX_FWSTATUS)); +} + +static int +aac_fa_get_fwstatus(struct aac_softc *sc) +{ + int val; + + debug_called(3); + + val = AAC_GETREG4(sc, AAC_FA_FWSTATUS); + return (val); +} + +/* + * Notify the controller of a change in a given queue + */ + +static void +aac_sa_qnotify(struct aac_softc *sc, int qbit) +{ + debug_called(3); + + AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit); +} + +static void +aac_rx_qnotify(struct aac_softc *sc, int qbit) +{ + debug_called(3); + + AAC_SETREG4(sc, AAC_RX_IDBR, qbit); +} + +static void +aac_fa_qnotify(struct aac_softc *sc, int qbit) +{ + debug_called(3); + + AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit); + AAC_FA_HACK(sc); +} + +/* + * Get the interrupt reason bits + */ +static int +aac_sa_get_istatus(struct aac_softc *sc) +{ + debug_called(3); + + return(AAC_GETREG2(sc, AAC_SA_DOORBELL0)); +} + +static int +aac_rx_get_istatus(struct aac_softc *sc) +{ + debug_called(3); + + return(AAC_GETREG4(sc, AAC_RX_ODBR)); +} + +static int +aac_fa_get_istatus(struct aac_softc *sc) +{ + int val; + + debug_called(3); + + val = AAC_GETREG2(sc, AAC_FA_DOORBELL0); + return (val); +} + +/* + * Clear some interrupt reason bits + */ +static void +aac_sa_clear_istatus(struct aac_softc *sc, int mask) +{ + debug_called(3); + + AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask); +} + +static void +aac_rx_clear_istatus(struct aac_softc *sc, int mask) +{ + debug_called(3); + + AAC_SETREG4(sc, AAC_RX_ODBR, mask); +} + +static void +aac_fa_clear_istatus(struct aac_softc *sc, int mask) +{ + debug_called(3); + + AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask); + AAC_FA_HACK(sc); +} + +/* + * Populate the mailbox and set the command word + */ +static void +aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) +{ + debug_called(4); + + AAC_SETREG4(sc, AAC_SA_MAILBOX, command); + AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0); + AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1); + AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2); + AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3); +} + +static void +aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) +{ + debug_called(4); + + AAC_SETREG4(sc, AAC_RX_MAILBOX, command); + AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0); + AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1); + AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2); + AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3); +} + +static void +aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) +{ + debug_called(4); + + AAC_SETREG4(sc, AAC_FA_MAILBOX, command); + AAC_FA_HACK(sc); + AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0); + AAC_FA_HACK(sc); + AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1); + AAC_FA_HACK(sc); + AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2); + AAC_FA_HACK(sc); + AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3); + AAC_FA_HACK(sc); +} + +/* + * Fetch the immediate command status word + */ +static int +aac_sa_get_mailbox(struct aac_softc *sc, int mb) +{ + debug_called(4); + + return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4))); +} + +static int +aac_rx_get_mailbox(struct aac_softc *sc, int mb) +{ + debug_called(4); + + return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4))); +} + +static int +aac_fa_get_mailbox(struct aac_softc *sc, int mb) +{ + int val; + + debug_called(4); + + val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4)); + return (val); +} + +/* + * Set/clear interrupt masks + */ +static void +aac_sa_set_interrupts(struct aac_softc *sc, int enable) +{ + debug(2, "%sable interrupts", enable ? "en" : "dis"); + + if (enable) { + AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS); + } else { + AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0); + } +} + +static void +aac_rx_set_interrupts(struct aac_softc *sc, int enable) +{ + debug(2, "%sable interrupts", enable ? "en" : "dis"); + + if (enable) { + AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS); + } else { + AAC_SETREG4(sc, AAC_RX_OIMR, ~0); + } +} + +static void +aac_fa_set_interrupts(struct aac_softc *sc, int enable) +{ + debug(2, "%sable interrupts", enable ? "en" : "dis"); + + if (enable) { + AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS); + AAC_FA_HACK(sc); + } else { + AAC_SETREG2((sc), AAC_FA_MASK0, ~0); + AAC_FA_HACK(sc); + } +} + +/* + * Debugging and Diagnostics + */ + +/* + * Print some information about the controller. + */ +static void +aac_describe_controller(struct aac_softc *sc) +{ + struct aac_fib *fib; + struct aac_adapter_info *info; + + debug_called(2); + + aac_alloc_sync_fib(sc, &fib, 0); + + fib->data[0] = 0; + if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) { + device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); + aac_release_sync_fib(sc); + return; + } + info = (struct aac_adapter_info *)&fib->data[0]; + + device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n", + aac_describe_code(aac_cpu_variant, info->CpuVariant), + info->ClockSpeed, info->BufferMem / (1024 * 1024), + aac_describe_code(aac_battery_platform, + info->batteryPlatform)); + + /* save the kernel revision structure for later use */ + sc->aac_revision = info->KernelRevision; + device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n", + info->KernelRevision.external.comp.major, + info->KernelRevision.external.comp.minor, + info->KernelRevision.external.comp.dash, + info->KernelRevision.buildNumber, + (u_int32_t)(info->SerialNumber & 0xffffff)); + + aac_release_sync_fib(sc); + + if (1 || bootverbose) { + device_printf(sc->aac_dev, "Supported Options=%b\n", + sc->supported_options, + "\20" + "\1SNAPSHOT" + "\2CLUSTERS" + "\3WCACHE" + "\4DATA64" + "\5HOSTTIME" + "\6RAID50" + "\7WINDOW4GB" + "\10SCSIUPGD" + "\11SOFTERR" + "\12NORECOND" + "\13SGMAP64" + "\14ALARM" + "\15NONDASD"); + } +} + +/* + * Look up a text description of a numeric error code and return a pointer to + * same. + */ +static char * +aac_describe_code(struct aac_code_lookup *table, u_int32_t code) +{ + int i; + + for (i = 0; table[i].string != NULL; i++) + if (table[i].code == code) + return(table[i].string); + return(table[i + 1].string); +} + +/* + * Management Interface + */ + +static int +aac_open(dev_t dev, int flags, int fmt, d_thread_t *td) +{ + struct aac_softc *sc; + + debug_called(2); + + sc = dev->si_drv1; + + /* Check to make sure the device isn't already open */ + if (sc->aac_state & AAC_STATE_OPEN) { + return EBUSY; + } + sc->aac_state |= AAC_STATE_OPEN; + + return 0; +} + +static int +aac_close(dev_t dev, int flags, int fmt, d_thread_t *td) +{ + struct aac_softc *sc; + + debug_called(2); + + sc = dev->si_drv1; + + /* Mark this unit as no longer open */ + sc->aac_state &= ~AAC_STATE_OPEN; + + return 0; +} + +static int +aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td) +{ + union aac_statrequest *as; + struct aac_softc *sc; + int error = 0; + int i; + + debug_called(2); + + as = (union aac_statrequest *)arg; + sc = dev->si_drv1; + + switch (cmd) { + case AACIO_STATS: + switch (as->as_item) { + case AACQ_FREE: + case AACQ_BIO: + case AACQ_READY: + case AACQ_BUSY: + case AACQ_COMPLETE: + bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat, + sizeof(struct aac_qstat)); + break; + default: + error = ENOENT; + break; + } + break; + + case FSACTL_SENDFIB: + arg = *(caddr_t*)arg; + case FSACTL_LNX_SENDFIB: + debug(1, "FSACTL_SENDFIB"); + error = aac_ioctl_sendfib(sc, arg); + break; + case FSACTL_AIF_THREAD: + case FSACTL_LNX_AIF_THREAD: + debug(1, "FSACTL_AIF_THREAD"); + error = EINVAL; + break; + case FSACTL_OPEN_GET_ADAPTER_FIB: + arg = *(caddr_t*)arg; + case FSACTL_LNX_OPEN_GET_ADAPTER_FIB: + debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB"); + /* + * Pass the caller out an AdapterFibContext. + * + * Note that because we only support one opener, we + * basically ignore this. Set the caller's context to a magic + * number just in case. + * + * The Linux code hands the driver a pointer into kernel space, + * and then trusts it when the caller hands it back. Aiee! + * Here, we give it the proc pointer of the per-adapter aif + * thread. It's only used as a sanity check in other calls. + */ + i = (int)sc->aifthread; + error = copyout(&i, arg, sizeof(i)); + break; + case FSACTL_GET_NEXT_ADAPTER_FIB: + arg = *(caddr_t*)arg; + case FSACTL_LNX_GET_NEXT_ADAPTER_FIB: + debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB"); + error = aac_getnext_aif(sc, arg); + break; + case FSACTL_CLOSE_GET_ADAPTER_FIB: + case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB: + debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB"); + /* don't do anything here */ + break; + case FSACTL_MINIPORT_REV_CHECK: + arg = *(caddr_t*)arg; + case FSACTL_LNX_MINIPORT_REV_CHECK: + debug(1, "FSACTL_MINIPORT_REV_CHECK"); + error = aac_rev_check(sc, arg); + break; + case FSACTL_QUERY_DISK: + arg = *(caddr_t*)arg; + case FSACTL_LNX_QUERY_DISK: + debug(1, "FSACTL_QUERY_DISK"); + error = aac_query_disk(sc, arg); + break; + case FSACTL_DELETE_DISK: + case FSACTL_LNX_DELETE_DISK: + /* + * We don't trust the underland to tell us when to delete a + * container, rather we rely on an AIF coming from the + * controller + */ + error = 0; + break; + default: + debug(1, "unsupported cmd 0x%lx\n", cmd); + error = EINVAL; + break; + } + return(error); +} + +static int +aac_poll(dev_t dev, int poll_events, d_thread_t *td) +{ + struct aac_softc *sc; + int revents; + + sc = dev->si_drv1; + revents = 0; + + AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); + if ((poll_events & (POLLRDNORM | POLLIN)) != 0) { + if (sc->aac_aifq_tail != sc->aac_aifq_head) + revents |= poll_events & (POLLIN | POLLRDNORM); + } + AAC_LOCK_RELEASE(&sc->aac_aifq_lock); + + if (revents == 0) { + if (poll_events & (POLLIN | POLLRDNORM)) + selrecord(td, &sc->rcv_select); + } + + return (revents); +} + +/* + * Send a FIB supplied from userspace + */ +static int +aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) +{ + struct aac_command *cm; + int size, error; + + debug_called(2); + + cm = NULL; + + /* + * Get a command + */ + AAC_LOCK_ACQUIRE(&sc->aac_io_lock); + if (aac_alloc_command(sc, &cm)) { + error = EBUSY; + goto out; + } + + /* + * Fetch the FIB header, then re-copy to get data as well. + */ + if ((error = copyin(ufib, cm->cm_fib, + sizeof(struct aac_fib_header))) != 0) + goto out; + size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header); + if (size > sizeof(struct aac_fib)) { + device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n", + size, sizeof(struct aac_fib)); + size = sizeof(struct aac_fib); + } + if ((error = copyin(ufib, cm->cm_fib, size)) != 0) + goto out; + cm->cm_fib->Header.Size = size; + cm->cm_timestamp = time_second; + + /* + * Pass the FIB to the controller, wait for it to complete. + */ + if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */ + device_printf(sc->aac_dev, + "aac_wait_command return %d\n", error); + goto out; + } + + /* + * Copy the FIB and data back out to the caller. + */ + size = cm->cm_fib->Header.Size; + if (size > sizeof(struct aac_fib)) { + device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n", + size, sizeof(struct aac_fib)); + size = sizeof(struct aac_fib); + } + error = copyout(cm->cm_fib, ufib, size); + +out: + if (cm != NULL) { + aac_release_command(cm); + } + + AAC_LOCK_RELEASE(&sc->aac_io_lock); + return(error); +} + +/* + * Handle an AIF sent to us by the controller; queue it for later reference. + * If the queue fills up, then drop the older entries. + */ +static void +aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib) +{ + struct aac_aif_command *aif; + struct aac_container *co, *co_next; + struct aac_mntinfo *mi; + struct aac_mntinforesp *mir = NULL; + u_int16_t rsize; + int next, found; + int count = 0, added = 0, i = 0; + + debug_called(2); + + aif = (struct aac_aif_command*)&fib->data[0]; + aac_print_aif(sc, aif); + + /* Is it an event that we should care about? */ + switch (aif->command) { + case AifCmdEventNotify: + switch (aif->data.EN.type) { + case AifEnAddContainer: + case AifEnDeleteContainer: + /* + * A container was added or deleted, but the message + * doesn't tell us anything else! Re-enumerate the + * containers and sort things out. + */ + aac_alloc_sync_fib(sc, &fib, 0); + mi = (struct aac_mntinfo *)&fib->data[0]; + do { + /* + * Ask the controller for its containers one at + * a time. + * XXX What if the controller's list changes + * midway through this enumaration? + * XXX This should be done async. + */ + bzero(mi, sizeof(struct aac_mntinfo)); + mi->Command = VM_NameServe; + mi->MntType = FT_FILESYS; + mi->MntCount = i; + rsize = sizeof(mir); + if (aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_mntinfo))) { + printf("Error probing container %d\n", + i); + continue; + } + mir = (struct aac_mntinforesp *)&fib->data[0]; + /* XXX Need to check if count changed */ + count = mir->MntRespCount; + /* + * Check the container against our list. + * co->co_found was already set to 0 in a + * previous run. + */ + if ((mir->Status == ST_OK) && + (mir->MntTable[0].VolType != CT_NONE)) { + found = 0; + TAILQ_FOREACH(co, + &sc->aac_container_tqh, + co_link) { + if (co->co_mntobj.ObjectId == + mir->MntTable[0].ObjectId) { + co->co_found = 1; + found = 1; + break; + } + } + /* + * If the container matched, continue + * in the list. + */ + if (found) { + i++; + continue; + } + + /* + * This is a new container. Do all the + * appropriate things to set it up. + */ + aac_add_container(sc, mir, 1); + added = 1; + } + i++; + } while ((i < count) && (i < AAC_MAX_CONTAINERS)); + aac_release_sync_fib(sc); + + /* + * Go through our list of containers and see which ones + * were not marked 'found'. Since the controller didn't + * list them they must have been deleted. Do the + * appropriate steps to destroy the device. Also reset + * the co->co_found field. + */ + co = TAILQ_FIRST(&sc->aac_container_tqh); + while (co != NULL) { + if (co->co_found == 0) { + device_delete_child(sc->aac_dev, + co->co_disk); + co_next = TAILQ_NEXT(co, co_link); + AAC_LOCK_ACQUIRE(&sc-> + aac_container_lock); + TAILQ_REMOVE(&sc->aac_container_tqh, co, + co_link); + AAC_LOCK_RELEASE(&sc-> + aac_container_lock); + FREE(co, M_AACBUF); + co = co_next; + } else { + co->co_found = 0; + co = TAILQ_NEXT(co, co_link); + } + } + + /* Attach the newly created containers */ + if (added) + bus_generic_attach(sc->aac_dev); + + break; + + default: + break; + } + + default: + break; + } + + /* Copy the AIF data to the AIF queue for ioctl retrieval */ + AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); + next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH; + if (next != sc->aac_aifq_tail) { + bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command)); + sc->aac_aifq_head = next; + + /* On the off chance that someone is sleeping for an aif... */ + if (sc->aac_state & AAC_STATE_AIF_SLEEPER) + wakeup(sc->aac_aifq); + /* Wakeup any poll()ers */ + selwakeup(&sc->rcv_select); + } + AAC_LOCK_RELEASE(&sc->aac_aifq_lock); + + return; +} + +/* + * Return the Revision of the driver to userspace and check to see if the + * userspace app is possibly compatible. This is extremely bogus since + * our driver doesn't follow Adaptec's versioning system. Cheat by just + * returning what the card reported. + */ +static int +aac_rev_check(struct aac_softc *sc, caddr_t udata) +{ + struct aac_rev_check rev_check; + struct aac_rev_check_resp rev_check_resp; + int error = 0; + + debug_called(2); + + /* + * Copyin the revision struct from userspace + */ + if ((error = copyin(udata, (caddr_t)&rev_check, + sizeof(struct aac_rev_check))) != 0) { + return error; + } + + debug(2, "Userland revision= %d\n", + rev_check.callingRevision.buildNumber); + + /* + * Doctor up the response struct. + */ + rev_check_resp.possiblyCompatible = 1; + rev_check_resp.adapterSWRevision.external.ul = + sc->aac_revision.external.ul; + rev_check_resp.adapterSWRevision.buildNumber = + sc->aac_revision.buildNumber; + + return(copyout((caddr_t)&rev_check_resp, udata, + sizeof(struct aac_rev_check_resp))); +} + +/* + * Pass the caller the next AIF in their queue + */ +static int +aac_getnext_aif(struct aac_softc *sc, caddr_t arg) +{ + struct get_adapter_fib_ioctl agf; + int error; + + debug_called(2); + + if ((error = copyin(arg, &agf, sizeof(agf))) == 0) { + + /* + * Check the magic number that we gave the caller. + */ + if (agf.AdapterFibContext != (int)sc->aifthread) { + error = EFAULT; + } else { + error = aac_return_aif(sc, agf.AifFib); + if ((error == EAGAIN) && (agf.Wait)) { + sc->aac_state |= AAC_STATE_AIF_SLEEPER; + while (error == EAGAIN) { + error = tsleep(sc->aac_aifq, PRIBIO | + PCATCH, "aacaif", 0); + if (error == 0) + error = aac_return_aif(sc, + agf.AifFib); + } + sc->aac_state &= ~AAC_STATE_AIF_SLEEPER; + } + } + } + return(error); +} + +/* + * Hand the next AIF off the top of the queue out to userspace. + */ +static int +aac_return_aif(struct aac_softc *sc, caddr_t uptr) +{ + int error; + + debug_called(2); + + AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); + if (sc->aac_aifq_tail == sc->aac_aifq_head) { + error = EAGAIN; + } else { + error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr, + sizeof(struct aac_aif_command)); + if (error) + device_printf(sc->aac_dev, + "aac_return_aif: copyout returned %d\n", error); + if (!error) + sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) % + AAC_AIFQ_LENGTH; + } + AAC_LOCK_RELEASE(&sc->aac_aifq_lock); + return(error); +} + +/* + * Give the userland some information about the container. The AAC arch + * expects the driver to be a SCSI passthrough type driver, so it expects + * the containers to have b:t:l numbers. Fake it. + */ +static int +aac_query_disk(struct aac_softc *sc, caddr_t uptr) +{ + struct aac_query_disk query_disk; + struct aac_container *co; + struct aac_disk *disk; + int error, id; + + debug_called(2); + + disk = NULL; + + error = copyin(uptr, (caddr_t)&query_disk, + sizeof(struct aac_query_disk)); + if (error) + return (error); + + id = query_disk.ContainerNumber; + if (id == -1) + return (EINVAL); + + AAC_LOCK_ACQUIRE(&sc->aac_container_lock); + TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) { + if (co->co_mntobj.ObjectId == id) + break; + } + + if (co == NULL) { + query_disk.Valid = 0; + query_disk.Locked = 0; + query_disk.Deleted = 1; /* XXX is this right? */ + } else { + disk = device_get_softc(co->co_disk); + query_disk.Valid = 1; + query_disk.Locked = + (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0; + query_disk.Deleted = 0; + query_disk.Bus = device_get_unit(sc->aac_dev); + query_disk.Target = disk->unit; + query_disk.Lun = 0; + query_disk.UnMapped = 0; + sprintf(&query_disk.diskDeviceName[0], "%s%d", + disk->ad_disk.d_name, disk->ad_disk.d_unit); + } + AAC_LOCK_RELEASE(&sc->aac_container_lock); + + error = copyout((caddr_t)&query_disk, uptr, + sizeof(struct aac_query_disk)); + + return (error); +} + +static void +aac_get_bus_info(struct aac_softc *sc) +{ + struct aac_fib *fib; + struct aac_ctcfg *c_cmd; + struct aac_ctcfg_resp *c_resp; + struct aac_vmioctl *vmi; + struct aac_vmi_businf_resp *vmi_resp; + struct aac_getbusinf businfo; + struct aac_sim *caminf; + device_t child; + int i, found, error; + + aac_alloc_sync_fib(sc, &fib, 0); + c_cmd = (struct aac_ctcfg *)&fib->data[0]; + bzero(c_cmd, sizeof(struct aac_ctcfg)); + + c_cmd->Command = VM_ContainerConfig; + c_cmd->cmd = CT_GET_SCSI_METHOD; + c_cmd->param = 0; + + error = aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_ctcfg)); + if (error) { + device_printf(sc->aac_dev, "Error %d sending " + "VM_ContainerConfig command\n", error); + aac_release_sync_fib(sc); + return; + } + + c_resp = (struct aac_ctcfg_resp *)&fib->data[0]; + if (c_resp->Status != ST_OK) { + device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n", + c_resp->Status); + aac_release_sync_fib(sc); + return; + } + + sc->scsi_method_id = c_resp->param; + + vmi = (struct aac_vmioctl *)&fib->data[0]; + bzero(vmi, sizeof(struct aac_vmioctl)); + + vmi->Command = VM_Ioctl; + vmi->ObjType = FT_DRIVE; + vmi->MethId = sc->scsi_method_id; + vmi->ObjId = 0; + vmi->IoctlCmd = GetBusInfo; + + error = aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_vmioctl)); + if (error) { + device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n", + error); + aac_release_sync_fib(sc); + return; + } + + vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0]; + if (vmi_resp->Status != ST_OK) { + device_printf(sc->aac_dev, "VM_Ioctl returned %d\n", + vmi_resp->Status); + aac_release_sync_fib(sc); + return; + } + + bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf)); + aac_release_sync_fib(sc); + + found = 0; + for (i = 0; i < businfo.BusCount; i++) { + if (businfo.BusValid[i] != AAC_BUS_VALID) + continue; + + caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim), + M_AACBUF, M_NOWAIT | M_ZERO); + if (caminf == NULL) + continue; + + child = device_add_child(sc->aac_dev, "aacp", -1); + if (child == NULL) { + device_printf(sc->aac_dev, "device_add_child failed\n"); + continue; + } + + caminf->TargetsPerBus = businfo.TargetsPerBus; + caminf->BusNumber = i; + caminf->InitiatorBusId = businfo.InitiatorBusId[i]; + caminf->aac_sc = sc; + caminf->sim_dev = child; + + device_set_ivars(child, caminf); + device_set_desc(child, "SCSI Passthrough Bus"); + TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link); + + found = 1; + } + + if (found) + bus_generic_attach(sc->aac_dev); + + return; +} diff --git a/sys/dev/aac/aac_cam.c b/sys/dev/aac/aac_cam.c new file mode 100644 index 0000000..8386aed --- /dev/null +++ b/sys/dev/aac/aac_cam.c @@ -0,0 +1,605 @@ +/* + * Copyright (c) 2002 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * CAM front-end for communicating with non-DASD devices + */ + +#include "opt_aac.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/sysctl.h> +#include <sys/malloc.h> + +#include <cam/cam.h> +#include <cam/cam_ccb.h> +#include <cam/cam_debug.h> +#include <cam/cam_sim.h> +#include <cam/cam_xpt_sim.h> +#include <cam/scsi/scsi_all.h> +#include <cam/scsi/scsi_message.h> + +#include <sys/bus.h> +#include <sys/conf.h> +#include <sys/disk.h> + +#include <machine/md_var.h> +#include <machine/bus.h> +#include <sys/rman.h> + +#include <vm/vm.h> +#include <vm/pmap.h> + +#include <dev/aac/aacreg.h> +#include <dev/aac/aac_ioctl.h> +#include <dev/aac/aacvar.h> + +struct aac_cam { + device_t dev; + struct aac_sim *inf; + struct cam_sim *sim; + struct cam_path *path; +}; + +static int aac_cam_probe(device_t dev); +static int aac_cam_attach(device_t dev); +static int aac_cam_detach(device_t dev); +static void aac_cam_action(struct cam_sim *, union ccb *); +static void aac_cam_poll(struct cam_sim *); +static void aac_cam_complete(struct aac_command *); +static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *); +static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *); +static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *); +static int aac_cam_get_tran_settings(struct aac_softc *, struct ccb_trans_settings *, u_int32_t); + +static devclass_t aac_pass_devclass; + +static device_method_t aac_pass_methods[] = { + DEVMETHOD(device_probe, aac_cam_probe), + DEVMETHOD(device_attach, aac_cam_attach), + DEVMETHOD(device_detach, aac_cam_detach), + { 0, 0 } +}; + +static driver_t aac_pass_driver = { + "aacp", + aac_pass_methods, + sizeof(struct aac_cam) +}; + +DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0); +MODULE_DEPEND(aacp, cam, 1, 1, 1); + +MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info"); + +static int +aac_cam_probe(device_t dev) +{ + debug_called(2); + + return (0); +} + +static int +aac_cam_detach(device_t dev) +{ + struct aac_cam *camsc; + debug_called(2); + + camsc = (struct aac_cam *)device_get_softc(dev); + + xpt_async(AC_LOST_DEVICE, camsc->path, NULL); + xpt_free_path(camsc->path); + xpt_bus_deregister(cam_sim_path(camsc->sim)); + cam_sim_free(camsc->sim, /*free_devq*/TRUE); + + return (0); +} + +/* + * Register the driver as a CAM SIM + */ +static int +aac_cam_attach(device_t dev) +{ + struct cam_devq *devq; + struct cam_sim *sim; + struct cam_path *path; + struct aac_cam *camsc; + struct aac_sim *inf; + + debug_called(1); + + camsc = (struct aac_cam *)device_get_softc(dev); + inf = (struct aac_sim *)device_get_ivars(dev); + camsc->inf = inf; + + devq = cam_simq_alloc(inf->TargetsPerBus); + if (devq == NULL) + return (EIO); + + sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc, + device_get_unit(dev), 1, 1, devq); + if (sim == NULL) { + cam_simq_free(devq); + return (EIO); + } + + /* Since every bus has it's own sim, every bus 'appears' as bus 0 */ + if (xpt_bus_register(sim, 0) != CAM_SUCCESS) { + cam_sim_free(sim, TRUE); + return (EIO); + } + + if (xpt_create_path(&path, NULL, cam_sim_path(sim), + CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { + xpt_bus_deregister(cam_sim_path(sim)); + cam_sim_free(sim, TRUE); + return (EIO); + } + + camsc->sim = sim; + camsc->path = path; + + return (0); +} + +static void +aac_cam_action(struct cam_sim *sim, union ccb *ccb) +{ + struct aac_cam *camsc; + struct aac_softc *sc; + struct aac_srb32 *srb; + struct aac_fib *fib; + struct aac_command *cm; + + debug_called(2); + + camsc = (struct aac_cam *)cam_sim_softc(sim); + sc = camsc->inf->aac_sc; + + /* Synchronous ops, and ops that don't require communication with the + * controller */ + switch(ccb->ccb_h.func_code) { + case XPT_SCSI_IO: + case XPT_RESET_DEV: + /* These are handled down below */ + break; + case XPT_CALC_GEOMETRY: + { + struct ccb_calc_geometry *ccg; + u_int32_t size_mb; + u_int32_t secs_per_cylinder; + + ccg = &ccb->ccg; + size_mb = ccg->volume_size / + ((1024L * 1024L) / ccg->block_size); + if (size_mb >= (2 * 1024)) { /* 2GB */ + ccg->heads = 255; + ccg->secs_per_track = 63; + } else if (size_mb >= (1 * 1024)) { /* 1GB */ + ccg->heads = 128; + ccg->secs_per_track = 32; + } else { + ccg->heads = 64; + ccg->secs_per_track = 32; + } + secs_per_cylinder = ccg->heads * ccg->secs_per_track; + ccg->cylinders = ccg->volume_size / secs_per_cylinder; + + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + return; + } + case XPT_PATH_INQ: + { + struct ccb_pathinq *cpi = &ccb->cpi; + + cpi->version_num = 1; + cpi->hba_inquiry = PI_WIDE_16; + cpi->target_sprt = 0; + + /* Resetting via the passthrough causes problems. */ + cpi->hba_misc = PIM_NOBUSRESET; + cpi->hba_eng_cnt = 0; + cpi->max_target = camsc->inf->TargetsPerBus; + cpi->max_lun = 8; /* Per the controller spec */ + cpi->initiator_id = camsc->inf->InitiatorBusId; + cpi->bus_id = camsc->inf->BusNumber; + cpi->base_transfer_speed = 3300; + strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); + strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); + strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); + cpi->unit_number = cam_sim_unit(sim); + + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + return; + } + case XPT_GET_TRAN_SETTINGS: + { + u_int32_t handle; + + handle = AAC_BTL_TO_HANDLE(camsc->inf->BusNumber, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun); + ccb->ccb_h.status = aac_cam_get_tran_settings(sc, &ccb->cts, + handle); + xpt_done(ccb); + return; + } + case XPT_SET_TRAN_SETTINGS: + ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; + xpt_done(ccb); + return; + case XPT_RESET_BUS: + if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { + ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb); + } else { + ccb->ccb_h.status = CAM_REQ_CMP; + } + xpt_done(ccb); + return; + case XPT_ABORT: + ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb); + xpt_done(ccb); + return; + case XPT_TERM_IO: + ccb->ccb_h.status = aac_cam_term_io(sim, ccb); + xpt_done(ccb); + return; + default: + device_printf(sc->aac_dev, "Unsupported command 0x%x\n", + ccb->ccb_h.func_code); + ccb->ccb_h.status = CAM_PROVIDE_FAIL; + xpt_done(ccb); + return; + } + + /* Async ops that require communcation with the controller */ + + AAC_LOCK_ACQUIRE(&sc->aac_io_lock); + if (aac_alloc_command(sc, &cm)) { + AAC_LOCK_RELEASE(&sc->aac_io_lock); + xpt_freeze_simq(sim, 1); + ccb->ccb_h.status = CAM_REQUEUE_REQ; + xpt_done(ccb); + return; + } + + fib = cm->cm_fib; + srb = (struct aac_srb32 *)&fib->data[0]; + cm->cm_datalen = 0; + + switch (ccb->ccb_h.flags & CAM_DIR_MASK) { + case CAM_DIR_IN: + srb->flags = AAC_SRB_FLAGS_DATA_IN; + cm->cm_flags |= AAC_CMD_DATAIN; + break; + case CAM_DIR_OUT: + srb->flags = AAC_SRB_FLAGS_DATA_OUT; + cm->cm_flags |= AAC_CMD_DATAOUT; + break; + case CAM_DIR_NONE: + srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER; + break; + default: + srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION; + cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT; + break; + } + + switch(ccb->ccb_h.func_code) { + case XPT_SCSI_IO: + { + struct ccb_scsiio *csio = &ccb->csio; + + srb->function = AAC_SRB_FUNC_EXECUTE_SCSI; + + /* + * Copy the CDB into the SRB. It's only 6-16 bytes, + * so a copy is not too expensive. + */ + srb->cdb_len = csio->cdb_len; + if (ccb->ccb_h.flags & CAM_CDB_POINTER) + bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0], + srb->cdb_len); + else + bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0], + srb->cdb_len); + + /* Map the s/g list. XXX 32bit addresses only! */ + if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { + if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { + srb->data_len = csio->dxfer_len; + if (ccb->ccb_h.flags & CAM_DATA_PHYS) { + srb->sg_map32.SgCount = 1; + srb->sg_map32.SgEntry[0].SgAddress = + (u_int32_t)csio->data_ptr; + srb->sg_map32.SgEntry[0].SgByteCount = + csio->dxfer_len; + } else { + /* + * Arrange things so that the S/G + * map will get set up automagically + */ + cm->cm_data = (void *)csio->data_ptr; + cm->cm_datalen = csio->dxfer_len; + cm->cm_sgtable = &srb->sg_map32; + } + } else { + /* XXX Need to handle multiple s/g elements */ + panic("aac_cam: multiple s/g elements"); + } + } else { + srb->sg_map32.SgCount = 0; + srb->sg_map32.SgEntry[0].SgByteCount = 0; + srb->data_len = 0; + } + + break; + } + case XPT_RESET_DEV: + if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { + srb->function = AAC_SRB_FUNC_RESET_DEVICE; + break; + } else { + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + return; + } + default: + break; + } + + srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */ + srb->target = ccb->ccb_h.target_id; + srb->lun = ccb->ccb_h.target_lun; + srb->timeout = ccb->ccb_h.timeout; /* XXX */ + srb->retry_limit = 0; + + cm->cm_complete = aac_cam_complete; + cm->cm_private = ccb; + cm->cm_timestamp = time_second; + cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; + + fib->Header.XferState = + AAC_FIBSTATE_HOSTOWNED | + AAC_FIBSTATE_INITIALISED | + AAC_FIBSTATE_FROMHOST | + AAC_FIBSTATE_REXPECTED | + AAC_FIBSTATE_NORM; + fib->Header.Command = ScsiPortCommand; + fib->Header.Size = sizeof(struct aac_fib_header) + + sizeof(struct aac_srb32); + + aac_enqueue_ready(cm); + aac_startio(cm->cm_sc); + + AAC_LOCK_RELEASE(&sc->aac_io_lock); + + return; +} + +static void +aac_cam_poll(struct cam_sim *sim) +{ + /* + * Pinging the interrupt routine isn't very safe, nor is it + * really necessary. Do nothing. + */ +} + +static void +aac_cam_complete(struct aac_command *cm) +{ + union ccb *ccb; + struct aac_srb_response *srbr; + struct aac_softc *sc; + + debug_called(2); + + sc = cm->cm_sc; + ccb = cm->cm_private; + srbr = (struct aac_srb_response *)&cm->cm_fib->data[0]; + + if (srbr->fib_status != 0) { + device_printf(sc->aac_dev, "Passthru FIB failed!\n"); + ccb->ccb_h.status = CAM_REQ_ABORTED; + } else { + /* + * The SRB error codes just happen to match the CAM error + * codes. How convienient! + */ + ccb->ccb_h.status = srbr->srb_status; + + /* Take care of SCSI_IO ops. */ + if (ccb->ccb_h.func_code == XPT_SCSI_IO) { + u_int8_t command, device; + + ccb->csio.scsi_status = srbr->scsi_status; + + /* Take care of autosense */ + if (srbr->sense_len) { + int sense_len, scsi_sense_len; + + scsi_sense_len = sizeof(struct scsi_sense_data); + bzero(&ccb->csio.sense_data, scsi_sense_len); + sense_len = (srbr->sense_len > + scsi_sense_len) ? scsi_sense_len : + srbr->sense_len; + bcopy(&srbr->sense[0], &ccb->csio.sense_data, + srbr->sense_len); + ccb->csio.sense_len = sense_len; + ccb->ccb_h.status |= CAM_AUTOSNS_VALID; + scsi_sense_print(&ccb->csio); + } + + /* If this is an inquiry command, fake things out */ + if (ccb->ccb_h.flags & CAM_CDB_POINTER) + command = ccb->csio.cdb_io.cdb_ptr[0]; + else + command = ccb->csio.cdb_io.cdb_bytes[0]; + + if ((command == INQUIRY) && + (ccb->ccb_h.status == CAM_REQ_CMP)) { + device = ccb->csio.data_ptr[0] & 0x1f; + /* + * We want DASD and PROC devices to only be + * visible through the pass device. + */ + if ((device == T_DIRECT) || + (device == T_PROCESSOR) || + (sc->flags & AAC_FLAGS_CAM_PASSONLY)) + ccb->csio.data_ptr[0] = + ((device & 0xe0) | T_NODEVICE); + } + } + } + + aac_release_command(cm); + + xpt_done(ccb); + + return; +} + +static u_int32_t +aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb) +{ + struct aac_fib *fib; + struct aac_softc *sc; + struct aac_cam *camsc; + struct aac_vmioctl *vmi; + struct aac_resetbus *rbc; + int e; + + camsc = (struct aac_cam *)cam_sim_softc(sim); + sc = camsc->inf->aac_sc; + + if (sc == NULL) { + printf("Null sc?\n"); + return (CAM_REQ_ABORTED); + } + + aac_alloc_sync_fib(sc, &fib, 0); + + vmi = (struct aac_vmioctl *)&fib->data[0]; + bzero(vmi, sizeof(struct aac_vmioctl)); + + vmi->Command = VM_Ioctl; + vmi->ObjType = FT_DRIVE; + vmi->MethId = sc->scsi_method_id; + vmi->ObjId = 0; + vmi->IoctlCmd = ResetBus; + + rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0]; + rbc->BusNumber = camsc->inf->BusNumber; + + e = aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_vmioctl)); + if (e) { + device_printf(sc->aac_dev,"Error %d sending ResetBus command\n", + e); + aac_release_sync_fib(sc); + return (CAM_REQ_ABORTED); + } + + aac_release_sync_fib(sc); + return (CAM_REQ_CMP); +} + +static u_int32_t +aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb) +{ + return (CAM_UA_ABORT); +} + +static u_int32_t +aac_cam_term_io(struct cam_sim *sim, union ccb *ccb) +{ + return (CAM_UA_TERMIO); +} + +static int +aac_cam_get_tran_settings(struct aac_softc *sc, struct ccb_trans_settings *cts, u_int32_t handle) +{ + struct aac_fib *fib; + struct aac_vmioctl *vmi; + struct aac_vmi_devinfo_resp *vmi_resp; + int error; + + aac_alloc_sync_fib(sc, &fib, 0); + vmi = (struct aac_vmioctl *)&fib->data[0]; + bzero(vmi, sizeof(struct aac_vmioctl)); + + vmi->Command = VM_Ioctl; + vmi->ObjType = FT_DRIVE; + vmi->MethId = sc->scsi_method_id; + vmi->ObjId = handle; + vmi->IoctlCmd = GetDeviceProbeInfo; + + error = aac_sync_fib(sc, ContainerCommand, 0, fib, + sizeof(struct aac_vmioctl)); + if (error) { + device_printf(sc->aac_dev, "Error %d sending GetDeviceProbeInfo" + " command\n", error); + aac_release_sync_fib(sc); + return (CAM_REQ_INVALID); + } + + vmi_resp = (struct aac_vmi_devinfo_resp *)&fib->data[0]; + if (vmi_resp->Status != ST_OK) { + /* + * The only reason why this command will return an error is + * if the requested device doesn't exist. + */ + debug(1, "GetDeviceProbeInfo returned %d\n", vmi_resp->Status); + aac_release_sync_fib(sc); + return (CAM_DEV_NOT_THERE); + } + + cts->bus_width = ((vmi_resp->Inquiry7 & 0x60) >> 5); + cts->valid = CCB_TRANS_BUS_WIDTH_VALID; + + if (vmi_resp->ScsiRate) { + cts->sync_period = + scsi_calc_syncparam((10000 / vmi_resp->ScsiRate)); + cts->sync_offset = vmi_resp->ScsiOffset; + cts->valid |= CCB_TRANS_SYNC_RATE_VALID | + CCB_TRANS_SYNC_OFFSET_VALID; + } + + cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB); + cts->valid |= CCB_TRANS_DISC_VALID | + CCB_TRANS_TQ_VALID; + + aac_release_sync_fib(sc); + return (CAM_REQ_CMP); +} diff --git a/sys/dev/aac/aac_compat.h b/sys/dev/aac/aac_compat.h new file mode 100644 index 0000000..ef61c0a --- /dev/null +++ b/sys/dev/aac/aac_compat.h @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2000 BSDi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ +/* + * Backwards compatibility support. + */ + +/* + * Handle the new/old bio/buf changeover + */ + +#if __FreeBSD_version < 500003 /* old buf style */ +#include <sys/buf.h> +#define FREEBSD_4 +#define bio buf +#define bioq_init(x) bufq_init(x) +#define bioq_insert_tail(x, y) bufq_insert_tail(x, y) +#define bioq_remove(x, y) bufq_remove(x, y) +#define bioq_first(x) bufq_first(x) +#define bio_queue_head buf_queue_head +#define bio_bcount b_bcount +#define bio_blkno b_blkno +#define bio_caller1 b_caller1 +#define bio_data b_data +#define bio_dev b_dev +#define bio_driver1 b_driver1 +#define bio_driver2 b_driver2 +#define bio_error b_error +#define bio_flags b_flags +#define bio_pblkno b_pblkno +#define bio_resid b_resid +#define BIO_ERROR B_ERROR +#define devstat_end_transaction_bio(x, y) devstat_end_transaction_buf(x, y) +#define BIO_IS_READ(x) ((x)->b_flags & B_READ) + +#else /* new bio style */ +#include <sys/bio.h> +#define BIO_IS_READ(x) ((x)->bio_cmd == BIO_READ) +#endif diff --git a/sys/dev/aac/aac_debug.c b/sys/dev/aac/aac_debug.c new file mode 100644 index 0000000..78ae942 --- /dev/null +++ b/sys/dev/aac/aac_debug.c @@ -0,0 +1,552 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Debugging support. + */ +#include "opt_aac.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/conf.h> + +#include <sys/bus.h> + +#include <machine/resource.h> +#include <machine/bus.h> + +#include <dev/aac/aac_compat.h> +#include <dev/aac/aacreg.h> +#include <dev/aac/aac_ioctl.h> +#include <dev/aac/aacvar.h> + +#ifdef AAC_DEBUG +void aac_printstate0(void); +void aac_intr0(void); + +/* + * Dump the command queue indices + */ +void +aac_print_queues(struct aac_softc *sc) +{ + device_printf(sc->aac_dev, "FIB queue header at %p queues at %p\n", + &sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][0], + &sc->aac_queues->qt_HostNormCmdQueue[0]); + device_printf(sc->aac_dev, "HOST_NORM_CMD %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_HOST_NORM_CMD_ENTRIES); + device_printf(sc->aac_dev, "HOST_HIGH_CMD %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_HOST_HIGH_CMD_ENTRIES); + device_printf(sc->aac_dev, "ADAP_NORM_CMD %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_ADAP_NORM_CMD_ENTRIES); + device_printf(sc->aac_dev, "ADAP_HIGH_CMD %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_ADAP_HIGH_CMD_ENTRIES); + device_printf(sc->aac_dev, "HOST_NORM_RESP %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_HOST_NORM_RESP_ENTRIES); + device_printf(sc->aac_dev, "HOST_HIGH_RESP %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_HOST_HIGH_RESP_ENTRIES); + device_printf(sc->aac_dev, "ADAP_NORM_RESP %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_ADAP_NORM_RESP_ENTRIES); + device_printf(sc->aac_dev, "ADAP_HIGH_RESP %d/%d (%d)\n", + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][ + AAC_PRODUCER_INDEX], + sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][ + AAC_CONSUMER_INDEX], + AAC_ADAP_HIGH_RESP_ENTRIES); + device_printf(sc->aac_dev, "AACQ_FREE %d/%d\n", + sc->aac_qstat[AACQ_FREE].q_length, sc->aac_qstat[AACQ_FREE].q_max); + device_printf(sc->aac_dev, "AACQ_BIO %d/%d\n", + sc->aac_qstat[AACQ_BIO].q_length, sc->aac_qstat[AACQ_BIO].q_max); + device_printf(sc->aac_dev, "AACQ_READY %d/%d\n", + sc->aac_qstat[AACQ_READY].q_length, + sc->aac_qstat[AACQ_READY].q_max); + device_printf(sc->aac_dev, "AACQ_BUSY %d/%d\n", + sc->aac_qstat[AACQ_BUSY].q_length, sc->aac_qstat[AACQ_BUSY].q_max); + device_printf(sc->aac_dev, "AACQ_COMPLETE %d/%d\n", + sc->aac_qstat[AACQ_COMPLETE].q_length, + sc->aac_qstat[AACQ_COMPLETE].q_max); +} + +/* + * Print the command queue states for controller 0 (callable from DDB) + */ +void +aac_printstate0(void) +{ + struct aac_softc *sc; + + sc = devclass_get_softc(devclass_find("aac"), 0); + + aac_print_queues(sc); + switch (sc->aac_hwif) { + case AAC_HWIF_I960RX: + device_printf(sc->aac_dev, "IDBR 0x%08x IIMR 0x%08x " + "IISR 0x%08x\n", AAC_GETREG4(sc, AAC_RX_IDBR), + AAC_GETREG4(sc, AAC_RX_IIMR), AAC_GETREG4(sc, AAC_RX_IISR)); + device_printf(sc->aac_dev, "ODBR 0x%08x OIMR 0x%08x " + "OISR 0x%08x\n", AAC_GETREG4(sc, AAC_RX_ODBR), + AAC_GETREG4(sc, AAC_RX_OIMR), AAC_GETREG4(sc, AAC_RX_OISR)); + AAC_SETREG4(sc, AAC_RX_OIMR, 0/*~(AAC_DB_COMMAND_READY | + AAC_DB_RESPONSE_READY | AAC_DB_PRINTF)*/); + device_printf(sc->aac_dev, "ODBR 0x%08x OIMR 0x%08x " + "OISR 0x%08x\n", AAC_GETREG4(sc, AAC_RX_ODBR), + AAC_GETREG4(sc, AAC_RX_OIMR), AAC_GETREG4(sc, AAC_RX_OISR)); + break; + case AAC_HWIF_STRONGARM: + /* XXX implement */ + break; + } +} + +/* + * simulate an interrupt for controller 0 + */ +void +aac_intr0(void) +{ + struct aac_softc *sc; + + sc = devclass_get_softc(devclass_find("aac"), 0); + + aac_intr(sc); +} + +/* + * Panic in a slightly informative fashion + */ +void +aac_panic(struct aac_softc *sc, char *reason) +{ + aac_print_queues(sc); + panic(reason); +} + +/* + * Print a FIB + */ +void +aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, const char *caller) +{ + if (fib == NULL) { + device_printf(sc->aac_dev, + "aac_print_fib called with NULL fib\n"); + return; + } + device_printf(sc->aac_dev, "%s: FIB @ %p\n", caller, fib); + device_printf(sc->aac_dev, " XferState %b\n", fib->Header.XferState, + "\20" + "\1HOSTOWNED" + "\2ADAPTEROWNED" + "\3INITIALISED" + "\4EMPTY" + "\5FROMPOOL" + "\6FROMHOST" + "\7FROMADAP" + "\10REXPECTED" + "\11RNOTEXPECTED" + "\12DONEADAP" + "\13DONEHOST" + "\14HIGH" + "\15NORM" + "\16ASYNC" + "\17PAGEFILEIO" + "\20SHUTDOWN" + "\21LAZYWRITE" + "\22ADAPMICROFIB" + "\23BIOSFIB" + "\24FAST_RESPONSE" + "\25APIFIB\n"); + device_printf(sc->aac_dev, " Command %d\n", fib->Header.Command); + device_printf(sc->aac_dev, " StructType %d\n", + fib->Header.StructType); + device_printf(sc->aac_dev, " Flags 0x%x\n", fib->Header.Flags); + device_printf(sc->aac_dev, " Size %d\n", fib->Header.Size); + device_printf(sc->aac_dev, " SenderSize %d\n", + fib->Header.SenderSize); + device_printf(sc->aac_dev, " SenderAddress 0x%x\n", + fib->Header.SenderFibAddress); + device_printf(sc->aac_dev, " RcvrAddress 0x%x\n", + fib->Header.ReceiverFibAddress); + device_printf(sc->aac_dev, " SenderData 0x%x\n", + fib->Header.SenderData); + switch(fib->Header.Command) { + case ContainerCommand: + { + struct aac_blockread *br; + struct aac_blockwrite *bw; + struct aac_sg_table *sg; + int i; + + br = (struct aac_blockread*)fib->data; + bw = (struct aac_blockwrite*)fib->data; + sg = NULL; + + if (br->Command == VM_CtBlockRead) { + device_printf(sc->aac_dev, + " BlockRead: container %d 0x%x/%d\n", + br->ContainerId, br->BlockNumber, + br->ByteCount); + sg = &br->SgMap; + } + if (bw->Command == VM_CtBlockWrite) { + device_printf(sc->aac_dev, + " BlockWrite: container %d 0x%x/%d " + "(%s)\n", bw->ContainerId, + bw->BlockNumber, bw->ByteCount, + bw->Stable == CSTABLE ? "stable" : + "unstable"); + sg = &bw->SgMap; + } + if (sg != NULL) { + device_printf(sc->aac_dev, + " %d s/g entries\n", sg->SgCount); + for (i = 0; i < sg->SgCount; i++) + device_printf(sc->aac_dev, " 0x%08x/%d\n", + sg->SgEntry[i].SgAddress, + sg->SgEntry[i].SgByteCount); + } + break; + } + default: + device_printf(sc->aac_dev, " %16D\n", fib->data, " "); + device_printf(sc->aac_dev, " %16D\n", fib->data + 16, " "); + break; + } +} + +/* + * Describe an AIF we have received. + */ +void +aac_print_aif(struct aac_softc *sc, struct aac_aif_command *aif) +{ + switch(aif->command) { + case AifCmdEventNotify: + device_printf(sc->aac_dev, "EventNotify(%d)\n", aif->seqNumber); + switch(aif->data.EN.type) { + case AifEnGeneric: /* Generic notification */ + device_printf(sc->aac_dev, "(Generic) %.*s\n", + (int)sizeof(aif->data.EN.data.EG), + aif->data.EN.data.EG.text); + break; + case AifEnTaskComplete: /* Task has completed */ + device_printf(sc->aac_dev, "(TaskComplete)\n"); + break; + case AifEnConfigChange: /* Adapter configuration change + * occurred */ + device_printf(sc->aac_dev, "(ConfigChange)\n"); + break; + case AifEnContainerChange: /* Adapter specific container + * configuration change */ + device_printf(sc->aac_dev, "(ContainerChange) " + "container %d,%d\n", + aif->data.EN.data.ECC.container[0], + aif->data.EN.data.ECC.container[1]); + break; + case AifEnDeviceFailure: /* SCSI device failed */ + device_printf(sc->aac_dev, "(DeviceFailure) " + "handle %d\n", + aif->data.EN.data.EDF.deviceHandle); + break; + case AifEnMirrorFailover: /* Mirror failover started */ + device_printf(sc->aac_dev, "(MirrorFailover) " + "container %d failed, " + "migrating from slice %d to %d\n", + aif->data.EN.data.EMF.container, + aif->data.EN.data.EMF.failedSlice, + aif->data.EN.data.EMF.creatingSlice); + break; + case AifEnContainerEvent: /* Significant container + * event */ + device_printf(sc->aac_dev, "(ContainerEvent) " + "container %d event " + "%d\n", aif->data.EN.data.ECE.container, + aif->data.EN.data.ECE.eventType); + break; + case AifEnFileSystemChange: /* File system changed */ + device_printf(sc->aac_dev, "(FileSystemChange)\n"); + break; + case AifEnConfigPause: /* Container pause event */ + device_printf(sc->aac_dev, "(ConfigPause)\n"); + break; + case AifEnConfigResume: /* Container resume event */ + device_printf(sc->aac_dev, "(ConfigResume)\n"); + break; + case AifEnFailoverChange: /* Failover space assignment + * changed */ + device_printf(sc->aac_dev, "(FailoverChange)\n"); + break; + case AifEnRAID5RebuildDone: /* RAID5 rebuild finished */ + device_printf(sc->aac_dev, "(RAID5RebuildDone)\n"); + break; + case AifEnEnclosureManagement: /* Enclosure management event */ + device_printf(sc->aac_dev, "(EnclosureManagement) " + "EMPID %d unit %d " + "event %d\n", aif->data.EN.data.EEE.empID, + aif->data.EN.data.EEE.unitID, + aif->data.EN.data.EEE.eventType); + break; + case AifEnBatteryEvent: /* Significant NV battery + * event */ + device_printf(sc->aac_dev, "(BatteryEvent) %d " + "(state was %d, is %d\n", + aif->data.EN.data.EBE.transition_type, + aif->data.EN.data.EBE.current_state, + aif->data.EN.data.EBE.prior_state); + break; + case AifEnAddContainer: /* A new container was + * created. */ + device_printf(sc->aac_dev, "(AddContainer)\n"); + break; + case AifEnDeleteContainer: /* A container was deleted. */ + device_printf(sc->aac_dev, "(DeleteContainer)\n"); + break; + case AifEnBatteryNeedsRecond: /* The battery needs + * reconditioning */ + device_printf(sc->aac_dev, "(BatteryNeedsRecond)\n"); + break; + case AifEnClusterEvent: /* Some cluster event */ + device_printf(sc->aac_dev, "(ClusterEvent) event %d\n", + aif->data.EN.data.ECLE.eventType); + break; + case AifEnDiskSetEvent: /* A disk set event occured. */ + device_printf(sc->aac_dev, "(DiskSetEvent) event %d " + "diskset %lld creator %lld\n", + aif->data.EN.data.EDS.eventType, + aif->data.EN.data.EDS.DsNum, + aif->data.EN.data.EDS.CreatorId); + break; + case AifDenMorphComplete: /* A morph operation + * completed */ + device_printf(sc->aac_dev, "(MorphComplete)\n"); + break; + case AifDenVolumeExtendComplete: /* A volume expand operation + * completed */ + device_printf(sc->aac_dev, "(VolumeExtendComplete)\n"); + break; + default: + device_printf(sc->aac_dev, "(%d)\n", aif->data.EN.type); + break; + } + break; + case AifCmdJobProgress: + { + char *status; + switch(aif->data.PR[0].status) { + case AifJobStsSuccess: + status = "success"; break; + case AifJobStsFinished: + status = "finished"; break; + case AifJobStsAborted: + status = "aborted"; break; + case AifJobStsFailed: + status = "failed"; break; + case AifJobStsSuspended: + status = "suspended"; break; + case AifJobStsRunning: + status = "running"; break; + default: + status = "unknown status"; break; + } + + device_printf(sc->aac_dev, "JobProgress (%d) - %s (%d, %d)\n", + aif->seqNumber, status, + aif->data.PR[0].currentTick, + aif->data.PR[0].finalTick); + switch(aif->data.PR[0].jd.type) { + case AifJobScsiZero: /* SCSI dev clear operation */ + device_printf(sc->aac_dev, "(ScsiZero) handle %d\n", + aif->data.PR[0].jd.client.scsi_dh); + break; + case AifJobScsiVerify: /* SCSI device Verify operation + * NO REPAIR */ + device_printf(sc->aac_dev, "(ScsiVerify) handle %d\n", + aif->data.PR[0].jd.client.scsi_dh); + break; + case AifJobScsiExercise: /* SCSI device Exercise + * operation */ + device_printf(sc->aac_dev, "(ScsiExercise) handle %d\n", + aif->data.PR[0].jd.client.scsi_dh); + break; + case AifJobScsiVerifyRepair: /* SCSI device Verify operation + * WITH repair */ + device_printf(sc->aac_dev, + "(ScsiVerifyRepair) handle %d\n", + aif->data.PR[0].jd.client.scsi_dh); + break; + case AifJobCtrZero: /* Container clear operation */ + device_printf(sc->aac_dev, + "(ConatainerZero) container %d\n", + aif->data.PR[0].jd.client.container.src); + break; + case AifJobCtrCopy: /* Container copy operation */ + device_printf(sc->aac_dev, + "(ConatainerCopy) container %d to %d\n", + aif->data.PR[0].jd.client.container.src, + aif->data.PR[0].jd.client.container.dst); + break; + case AifJobCtrCreateMirror: /* Container Create Mirror + * operation */ + device_printf(sc->aac_dev, + "(ConatainerCreateMirror) container %d\n", + aif->data.PR[0].jd.client.container.src); + /* XXX two containers? */ + break; + case AifJobCtrMergeMirror: /* Container Merge Mirror + * operation */ + device_printf(sc->aac_dev, + "(ConatainerMergeMirror) container %d\n", + aif->data.PR[0].jd.client.container.src); + /* XXX two containers? */ + break; + case AifJobCtrScrubMirror: /* Container Scrub Mirror + * operation */ + device_printf(sc->aac_dev, + "(ConatainerScrubMirror) container %d\n", + aif->data.PR[0].jd.client.container.src); + break; + case AifJobCtrRebuildRaid5: /* Container Rebuild Raid5 + * operation */ + device_printf(sc->aac_dev, + "(ConatainerRebuildRaid5) container %d\n", + aif->data.PR[0].jd.client.container.src); + break; + case AifJobCtrScrubRaid5: /* Container Scrub Raid5 + * operation */ + device_printf(sc->aac_dev, + "(ConatainerScrubRaid5) container %d\n", + aif->data.PR[0].jd.client.container.src); + break; + case AifJobCtrMorph: /* Container morph operation */ + device_printf(sc->aac_dev, + "(ConatainerMorph) container %d\n", + aif->data.PR[0].jd.client.container.src); + /* XXX two containers? */ + break; + case AifJobCtrPartCopy: /* Container Partition copy + * operation */ + device_printf(sc->aac_dev, + "(ConatainerPartCopy) container %d to " + "%d\n", + aif->data.PR[0].jd.client.container.src, + aif->data.PR[0].jd.client.container.dst); + break; + case AifJobCtrRebuildMirror: /* Container Rebuild Mirror + * operation */ + device_printf(sc->aac_dev, + "(ConatainerRebuildMirror) container " + "%d\n", + aif->data.PR[0].jd.client.container.src); + break; + case AifJobCtrCrazyCache: /* crazy cache */ + device_printf(sc->aac_dev, + "(ConatainerCrazyCache) container %d\n", + aif->data.PR[0].jd.client.container.src); + /* XXX two containers? */ + break; + case AifJobFsCreate: /* File System Create + * operation */ + device_printf(sc->aac_dev, "(FsCreate)\n"); + break; + case AifJobFsVerify: /* File System Verify + * operation */ + device_printf(sc->aac_dev, "(FsVerivy)\n"); + break; + case AifJobFsExtend: /* File System Extend + * operation */ + device_printf(sc->aac_dev, "(FsExtend)\n"); + break; + case AifJobApiFormatNTFS: /* Format a drive to NTFS */ + device_printf(sc->aac_dev, "(FormatNTFS)\n"); + break; + case AifJobApiFormatFAT: /* Format a drive to FAT */ + device_printf(sc->aac_dev, "(FormatFAT)\n"); + break; + case AifJobApiUpdateSnapshot: /* update the read/write half + * of a snapshot */ + device_printf(sc->aac_dev, "(UpdateSnapshot)\n"); + break; + case AifJobApiFormatFAT32: /* Format a drive to FAT32 */ + device_printf(sc->aac_dev, "(FormatFAT32)\n"); + break; + case AifJobCtlContinuousCtrVerify: /* Adapter operation */ + device_printf(sc->aac_dev, "(ContinuousCtrVerify)\n"); + break; + default: + device_printf(sc->aac_dev, "(%d)\n", + aif->data.PR[0].jd.type); + break; + } + break; + } + case AifCmdAPIReport: + device_printf(sc->aac_dev, "APIReport (%d)\n", aif->seqNumber); + break; + case AifCmdDriverNotify: + device_printf(sc->aac_dev, "DriverNotify (%d)\n", + aif->seqNumber); + break; + default: + device_printf(sc->aac_dev, "AIF %d (%d)\n", aif->command, + aif->seqNumber); + break; + } +} +#endif /* AAC_DEBUG */ diff --git a/sys/dev/aac/aac_disk.c b/sys/dev/aac/aac_disk.c new file mode 100644 index 0000000..2ac9f6b --- /dev/null +++ b/sys/dev/aac/aac_disk.c @@ -0,0 +1,392 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include "opt_aac.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/sysctl.h> + +#include <sys/bus.h> +#include <sys/conf.h> +#include <sys/disk.h> + +#include <vm/vm.h> +#include <vm/pmap.h> + +#include <machine/md_var.h> +#include <machine/bus.h> +#include <sys/rman.h> + +#include <dev/aac/aacreg.h> +#include <dev/aac/aac_ioctl.h> +#include <dev/aac/aacvar.h> + +/* + * Interface to parent. + */ +static int aac_disk_probe(device_t dev); +static int aac_disk_attach(device_t dev); +static int aac_disk_detach(device_t dev); + +/* + * Interface to the device switch. + */ +static disk_open_t aac_disk_open; +static disk_close_t aac_disk_close; +static disk_strategy_t aac_disk_strategy; +static dumper_t aac_disk_dump; + +static devclass_t aac_disk_devclass; + +static device_method_t aac_disk_methods[] = { + DEVMETHOD(device_probe, aac_disk_probe), + DEVMETHOD(device_attach, aac_disk_attach), + DEVMETHOD(device_detach, aac_disk_detach), + { 0, 0 } +}; + +static driver_t aac_disk_driver = { + "aacd", + aac_disk_methods, + sizeof(struct aac_disk) +}; + +#define AAC_MAXIO 65536 + +DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0); + +/* sysctl tunables */ +static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */ +TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max); + +SYSCTL_DECL(_hw_aac); +SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0, + "Max I/O size per transfer to an array"); + +/* + * Handle open from generic layer. + * + * This is called by the diskslice code on first open in order to get the + * basic device geometry paramters. + */ +static int +aac_disk_open(struct disk *dp) +{ + struct aac_disk *sc; + + debug_called(4); + + sc = (struct aac_disk *)dp->d_drv1; + + if (sc == NULL) { + printf("aac_disk_open: No Softc\n"); + return (ENXIO); + } + + /* check that the controller is up and running */ + if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) { + printf("Controller Suspended controller state = 0x%x\n", + sc->ad_controller->aac_state); + return(ENXIO); + } + + sc->ad_flags |= AAC_DISK_OPEN; + return (0); +} + +/* + * Handle last close of the disk device. + */ +static int +aac_disk_close(struct disk *dp) +{ + struct aac_disk *sc; + + debug_called(4); + + sc = (struct aac_disk *)dp->d_drv1; + + if (sc == NULL) + return (ENXIO); + + sc->ad_flags &= ~AAC_DISK_OPEN; + return (0); +} + +/* + * Handle an I/O request. + */ +static void +aac_disk_strategy(struct bio *bp) +{ + struct aac_disk *sc; + + debug_called(4); + + sc = (struct aac_disk *)bp->bio_disk->d_drv1; + + /* bogus disk? */ + if (sc == NULL) { + bp->bio_flags |= BIO_ERROR; + bp->bio_error = EINVAL; + biodone(bp); + return; + } + + /* do-nothing operation? */ + if (bp->bio_bcount == 0) { + bp->bio_resid = bp->bio_bcount; + biodone(bp); + return; + } + + /* perform accounting */ + + /* pass the bio to the controller - it can work out who we are */ + AAC_LOCK_ACQUIRE(&sc->ad_controller->aac_io_lock); + aac_submit_bio(bp); + AAC_LOCK_RELEASE(&sc->ad_controller->aac_io_lock); + + return; +} + +/* + * Map the S/G elements for doing a dump. + * + * XXX This does not handle >4GB of RAM. Fixing it is possible except on + * adapters that cannot do 64bit s/g lists. + */ +static void +aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error) +{ + struct aac_fib *fib; + struct aac_blockwrite *bw; + struct aac_sg_table *sg; + int i; + + fib = (struct aac_fib *)arg; + bw = (struct aac_blockwrite *)&fib->data[0]; + sg = &bw->SgMap; + + if (sg != NULL) { + sg->SgCount = nsegs; + for (i = 0; i < nsegs; i++) { + if (segs[i].ds_addr >= BUS_SPACE_MAXADDR_32BIT) + return; + sg->SgEntry[i].SgAddress = segs[i].ds_addr; + sg->SgEntry[i].SgByteCount = segs[i].ds_len; + } + fib->Header.Size = nsegs * sizeof(struct aac_sg_entry); + } +} + +/* + * Dump memory out to an array + * + * Send out one command at a time with up to AAC_MAXIO of data. + */ +static int +aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) +{ + struct aac_disk *ad; + struct aac_softc *sc; + struct aac_fib *fib; + struct aac_blockwrite *bw; + size_t len; + int size; + static bus_dmamap_t dump_datamap; + static int first = 0; + struct disk *dp; + + dp = arg; + ad = dp->d_drv1; + + if (ad == NULL) + return (EINVAL); + + sc= ad->ad_controller; + + if (!first) { + first = 1; + if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) { + printf("bus_dmamap_create failed\n"); + return (ENOMEM); + } + } + + aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE); + bw = (struct aac_blockwrite *)&fib->data[0]; + + while (length > 0) { + len = (length > AAC_MAXIO) ? AAC_MAXIO : length; + bw->Command = VM_CtBlockWrite; + bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; + bw->BlockNumber = offset / AAC_BLOCK_SIZE; + bw->ByteCount = len; + bw->Stable = CUNSTABLE; + + /* + * There really isn't any way to recover from errors or + * resource shortages here. Oh well. Because of that, don't + * bother trying to send the command from the callback; there + * is too much required context. + */ + if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual, + len, aac_dump_map_sg, fib, 0) != 0) + return (EIO); + + bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap, + BUS_DMASYNC_PREWRITE); + + /* fib->Header.Size is set in aac_dump_map_sg */ + size = fib->Header.Size + sizeof(struct aac_blockwrite); + + if (aac_sync_fib(sc, ContainerCommand, 0, fib, size)) { + printf("Error dumping block 0x%x\n", physical); + return (EIO); + } + + length -= len; + offset += len; + (vm_offset_t)virtual += len; + } + + return (0); +} + +/* + * Handle completion of an I/O request. + */ +void +aac_biodone(struct bio *bp) +{ + struct aac_disk *sc; + + debug_called(4); + + sc = (struct aac_disk *)bp->bio_disk->d_drv1; + + if (bp->bio_flags & BIO_ERROR) + disk_err(bp, "hard error", -1, 1); + + biodone(bp); +} + +/* + * Stub only. + */ +static int +aac_disk_probe(device_t dev) +{ + + debug_called(2); + + return (0); +} + +/* + * Attach a unit to the controller. + */ +static int +aac_disk_attach(device_t dev) +{ + struct aac_disk *sc; + + debug_called(1); + + sc = (struct aac_disk *)device_get_softc(dev); + + /* initialise our softc */ + sc->ad_controller = + (struct aac_softc *)device_get_softc(device_get_parent(dev)); + sc->ad_container = device_get_ivars(dev); + sc->ad_dev = dev; + + /* + * require that extended translation be enabled - other drivers read the + * disk! + */ + sc->ad_size = sc->ad_container->co_mntobj.Capacity; + if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */ + sc->ad_heads = 255; + sc->ad_sectors = 63; + } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */ + sc->ad_heads = 128; + sc->ad_sectors = 32; + } else { + sc->ad_heads = 64; + sc->ad_sectors = 32; + } + sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors)); + + device_printf(dev, "%uMB (%u sectors)\n", + sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE), + sc->ad_size); + + /* attach a generic disk device to ourselves */ + sc->unit = device_get_unit(dev); + sc->ad_disk.d_drv1 = sc; + sc->ad_disk.d_name = "aacd"; + sc->ad_disk.d_maxsize = aac_iosize_max; + sc->ad_disk.d_open = aac_disk_open; + sc->ad_disk.d_close = aac_disk_close; + sc->ad_disk.d_strategy = aac_disk_strategy; + sc->ad_disk.d_dump = aac_disk_dump; + sc->ad_disk.d_sectorsize = AAC_BLOCK_SIZE; + sc->ad_disk.d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE; + sc->ad_disk.d_fwsectors = sc->ad_sectors; + sc->ad_disk.d_fwheads = sc->ad_heads; + disk_create(sc->unit, &sc->ad_disk, DISKFLAG_NOGIANT, NULL, NULL); + + return (0); +} + +/* + * Disconnect ourselves from the system. + */ +static int +aac_disk_detach(device_t dev) +{ + struct aac_disk *sc; + + debug_called(2); + + sc = (struct aac_disk *)device_get_softc(dev); + + if (sc->ad_flags & AAC_DISK_OPEN) + return(EBUSY); + + disk_destroy(&sc->ad_disk); + + return(0); +} diff --git a/sys/dev/aac/aac_ioctl.h b/sys/dev/aac/aac_ioctl.h new file mode 100644 index 0000000..1b798eb --- /dev/null +++ b/sys/dev/aac/aac_ioctl.h @@ -0,0 +1,179 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2000 Scott Long + * Copyright (c) 2000 BSDi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Command queue statistics + */ +#define AACQ_FREE 0 +#define AACQ_BIO 1 +#define AACQ_READY 2 +#define AACQ_BUSY 3 +#define AACQ_COMPLETE 4 +#define AACQ_COUNT 5 /* total number of queues */ + +struct aac_qstat { + u_int32_t q_length; + u_int32_t q_max; +}; + +/* + * Statistics request + */ +union aac_statrequest { + u_int32_t as_item; + struct aac_qstat as_qstat; +}; + +#define AACIO_STATS _IOWR('T', 101, union aac_statrequest) + +/* + * Ioctl commands likely to be submitted from a Linux management application. + * These bit encodings are actually descended from Windows NT. Ick. + */ + +#define CTL_CODE(devType, func, meth, acc) (((devType) << 16) | ((acc) << 14) | ((func) << 2) | (meth)) +#define METHOD_BUFFERED 0 +#define METHOD_IN_DIRECT 1 +#define METHOD_OUT_DIRECT 2 +#define METHOD_NEITHER 3 +#define FILE_ANY_ACCESS 0 +#define FILE_READ_ACCESS ( 0x0001 ) +#define FILE_WRITE_ACCESS ( 0x0002 ) +#define FILE_DEVICE_CONTROLLER 0x00000004 + +#define FSACTL_LNX_SENDFIB CTL_CODE(FILE_DEVICE_CONTROLLER, 2050, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_GET_COMM_PERF_DATA CTL_CODE(FILE_DEVICE_CONTROLLER, 2084, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_OPENCLS_COMM_PERF_DATA CTL_CODE(FILE_DEVICE_CONTROLLER, \ + 2085, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_OPEN_GET_ADAPTER_FIB CTL_CODE(FILE_DEVICE_CONTROLLER, 2100, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_GET_NEXT_ADAPTER_FIB CTL_CODE(FILE_DEVICE_CONTROLLER, 2101, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_CLOSE_GET_ADAPTER_FIB CTL_CODE(FILE_DEVICE_CONTROLLER, \ + 2102, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_CLOSE_ADAPTER_CONFIG CTL_CODE(FILE_DEVICE_CONTROLLER, 2104, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_OPEN_ADAPTER_CONFIG CTL_CODE(FILE_DEVICE_CONTROLLER, 2105, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_MINIPORT_REV_CHECK CTL_CODE(FILE_DEVICE_CONTROLLER, 2107, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_QUERY_ADAPTER_CONFIG CTL_CODE(FILE_DEVICE_CONTROLLER, 2113, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_GET_PCI_INFO CTL_CODE(FILE_DEVICE_CONTROLLER, 2119, \ + METHOD_BUFFERED, FILE_ANY_ACCESS) +#define FSACTL_LNX_FORCE_DELETE_DISK CTL_CODE(FILE_DEVICE_CONTROLLER, 2120, \ + METHOD_NEITHER, FILE_ANY_ACCESS) +#define FSACTL_LNX_AIF_THREAD CTL_CODE(FILE_DEVICE_CONTROLLER, 2127, \ + METHOD_NEITHER, FILE_ANY_ACCESS) + +/* Why these don't follow the previous convention, I don't know */ +#define FSACTL_LNX_NULL_IO_TEST 0x43 +#define FSACTL_LNX_SIM_IO_TEST 0x53 +#define FSACTL_LNX_DOWNLOAD 0x83 +#define FSACTL_LNX_GET_VAR 0x93 +#define FSACTL_LNX_SET_VAR 0xa3 +#define FSACTL_LNX_GET_FIBTIMES 0xb3 +#define FSACTL_LNX_ZERO_FIBTIMES 0xc3 +#define FSACTL_LNX_DELETE_DISK 0x163 +#define FSACTL_LNX_QUERY_DISK 0x173 + +/* Ok, here it gets really lame */ +#define FSACTL_LNX_PROBE_CONTAINERS 2131 /* Just guessing */ + +/* Do the native version of the ioctls. Since the BSD encoding scheme + * conflicts with the 'standard' AAC encoding scheme, the resulting numbers + * will be different. The '8' comes from the fact that the previous scheme + * used 12 bits for the number, with the the 12th bit being the only set + * bit above bit 8. Thus the value of 8, with the lower 8 bits holding the + * command number. 9 is used for the odd overflow case. + */ +#define FSACTL_SENDFIB _IO('8', 2) +#define FSACTL_GET_COMM_PERF_DATA _IO('8', 36) +#define FSACTL_OPENCLS_COMM_PERF_DATA _IO('8', 37) +#define FSACTL_OPEN_GET_ADAPTER_FIB _IO('8', 52) +#define FSACTL_GET_NEXT_ADAPTER_FIB _IO('8', 53) +#define FSACTL_CLOSE_GET_ADAPTER_FIB _IO('8', 54) +#define FSACTL_CLOSE_ADAPTER_CONFIG _IO('8', 56) +#define FSACTL_OPEN_ADAPTER_CONFIG _IO('8', 57) +#define FSACTL_MINIPORT_REV_CHECK _IO('8', 59) +#define FSACTL_QUERY_ADAPTER_CONFIG _IO('8', 65) +#define FSACTL_GET_PCI_INFO _IO('8', 71) +#define FSACTL_FORCE_DELETE_DISK _IO('8', 72) +#define FSACTL_AIF_THREAD _IO('8', 79) + +#define FSACTL_NULL_IO_TEST _IO('8', 67) +#define FSACTL_SIM_IO_TEST _IO('8', 83) +#define FSACTL_DOWNLOAD _IO('8', 131) +#define FSACTL_GET_VAR _IO('8', 147) +#define FSACTL_SET_VAR _IO('8', 163) +#define FSACTL_GET_FIBTIMES _IO('8', 179) +#define FSACTL_ZERO_FIBTIMES _IO('8', 195) +#define FSACTL_DELETE_DISK _IO('8', 99) +#define FSACTL_QUERY_DISK _IO('9', 115) + +#define FSACTL_PROBE_CONTAINERS _IO('9', 83) /* Just guessing */ + + +/* + * Support for faking the "miniport" version. + */ +struct aac_rev_check { + RevComponent callingComponent; + struct FsaRevision callingRevision; +}; + +struct aac_rev_check_resp { + int possiblyCompatible; + struct FsaRevision adapterSWRevision; +}; + +/* + * Context passed in by a consumer looking to collect an AIF. + */ +struct get_adapter_fib_ioctl { + u_int32_t AdapterFibContext; + int Wait; + caddr_t AifFib; +}; + +struct aac_query_disk { + int32_t ContainerNumber; + int32_t Bus; + int32_t Target; + int32_t Lun; + u_int32_t Valid; + u_int32_t Locked; + u_int32_t Deleted; + int32_t Instance; + char diskDeviceName[10]; + u_int32_t UnMapped; +}; diff --git a/sys/dev/aac/aac_linux.c b/sys/dev/aac/aac_linux.c new file mode 100644 index 0000000..2ca8fa3 --- /dev/null +++ b/sys/dev/aac/aac_linux.c @@ -0,0 +1,84 @@ +/*- + * Copyright (c) 2002 Scott Long + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Linux ioctl handler for the aac device driver + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/conf.h> +#include <sys/kernel.h> +#include <sys/file.h> +#include <sys/proc.h> +#include <machine/../linux/linux.h> +#include <machine/../linux/linux_proto.h> +#include <compat/linux/linux_ioctl.h> + +/* There are multiple ioctl number ranges that need to be handled */ +#define AAC_LINUX_IOCTL_MIN 0x0000 +#define AAC_LINUX_IOCTL_MAX 0x21ff + +static linux_ioctl_function_t aac_linux_ioctl; +static struct linux_ioctl_handler aac_linux_handler = {aac_linux_ioctl, + AAC_LINUX_IOCTL_MIN, + AAC_LINUX_IOCTL_MAX}; + +SYSINIT (aac_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE, + linux_ioctl_register_handler, &aac_linux_handler); +SYSUNINIT(aac_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE, + linux_ioctl_unregister_handler, &aac_linux_handler); + +static int +aac_linux_modevent(module_t mod, int type, void *data) +{ + /* Do we care about any specific load/unload actions? */ + return (0); +} + +DEV_MODULE(aac_linux, aac_linux_modevent, NULL); +MODULE_DEPEND(aac_linux, linux, 1, 1, 1); + +static int +aac_linux_ioctl(struct thread *td, struct linux_ioctl_args *args) +{ + struct file *fp; + u_long cmd; + int error; + + if ((error = fget(td, args->fd, &fp)) != 0) + return (error); + cmd = args->cmd; + + /* + * Pass the ioctl off to our standard handler. + */ + error = (fo_ioctl(fp, cmd, (caddr_t)args->arg, td->td_ucred, td)); + fdrop(fp, td); + return (error); +} diff --git a/sys/dev/aac/aac_pci.c b/sys/dev/aac/aac_pci.c new file mode 100644 index 0000000..0a71d02 --- /dev/null +++ b/sys/dev/aac/aac_pci.c @@ -0,0 +1,305 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * PCI bus interface and resource allocation. + */ + +#include "opt_aac.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> + +#include <sys/bio.h> +#include <sys/bus.h> +#include <sys/conf.h> +#include <sys/disk.h> + +#include <machine/bus_memio.h> +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/rman.h> + +#include <pci/pcireg.h> +#include <pci/pcivar.h> + +#include <dev/aac/aacreg.h> +#include <dev/aac/aac_ioctl.h> +#include <dev/aac/aacvar.h> + +static int aac_pci_probe(device_t dev); +static int aac_pci_attach(device_t dev); + +static device_method_t aac_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, aac_pci_probe), + DEVMETHOD(device_attach, aac_pci_attach), + DEVMETHOD(device_detach, aac_detach), + DEVMETHOD(device_suspend, aac_suspend), + DEVMETHOD(device_resume, aac_resume), + + DEVMETHOD(bus_print_child, bus_generic_print_child), + DEVMETHOD(bus_driver_added, bus_generic_driver_added), + { 0, 0 } +}; + +static driver_t aac_pci_driver = { + "aac", + aac_methods, + sizeof(struct aac_softc) +}; + +static devclass_t aac_devclass; + +DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0); + +struct aac_ident +{ + u_int16_t vendor; + u_int16_t device; + u_int16_t subvendor; + u_int16_t subdevice; + int hwif; + int quirks; + char *desc; +} aac_identifiers[] = { + {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0, + "Dell PERC 2/Si"}, + {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Si"}, + {0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Si"}, + {0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x0008, 0x1028, 0x00cf, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0, + "Dell PERC 3/Di"}, + {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0, + "Adaptec AAC-364"}, + {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, 0, + "Adaptec SCSI RAID 5400S"}, + {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC, + "Dell PERC 2/QC"}, + {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0, + "HP NetRaid-4M"}, + {0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | + AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2200S"}, + {0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | + AAC_FLAGS_256FIBS, "Dell PERC 320/DC"}, + {0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB | + AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2120S"}, + {0x9005, 0x0285, 0x9005, 0x0290, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB, + "Adaptec SCSI RAID 2410SA"}, + {0, 0, 0, 0, 0, 0, 0} +}; + +/* + * Determine whether this is one of our supported adapters. + */ +static int +aac_pci_probe(device_t dev) +{ + struct aac_ident *m; + + debug_called(1); + + for (m = aac_identifiers; m->vendor != 0; m++) { + if ((m->vendor == pci_get_vendor(dev)) && + (m->device == pci_get_device(dev)) && + ((m->subvendor == 0) || (m->subvendor == + pci_get_subvendor(dev))) && + ((m->subdevice == 0) || ((m->subdevice == + pci_get_subdevice(dev))))) { + + device_set_desc(dev, m->desc); + return(-10); /* allow room to be overridden */ + } + } + return(ENXIO); +} + +/* + * Allocate resources for our device, set up the bus interface. + */ +static int +aac_pci_attach(device_t dev) +{ + struct aac_softc *sc; + int i, error; + u_int32_t command; + + debug_called(1); + + /* + * Initialise softc. + */ + sc = device_get_softc(dev); + bzero(sc, sizeof(*sc)); + sc->aac_dev = dev; + + /* assume failure is 'not configured' */ + error = ENXIO; + + /* + * Verify that the adapter is correctly set up in PCI space. + */ + command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); + command |= PCIM_CMD_BUSMASTEREN; + pci_write_config(dev, PCIR_COMMAND, command, 2); + command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2); + if (!(command & PCIM_CMD_BUSMASTEREN)) { + device_printf(sc->aac_dev, "can't enable bus-master feature\n"); + goto out; + } + if ((command & PCIM_CMD_MEMEN) == 0) { + device_printf(sc->aac_dev, "memory window not available\n"); + goto out; + } + + /* + * Allocate the PCI register window. + */ + sc->aac_regs_rid = 0x10; /* first base address register */ + if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev, + SYS_RES_MEMORY, + &sc->aac_regs_rid, + 0, ~0, 1, + RF_ACTIVE)) == NULL) { + device_printf(sc->aac_dev, + "couldn't allocate register window\n"); + goto out; + } + sc->aac_btag = rman_get_bustag(sc->aac_regs_resource); + sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource); + + /* + * Allocate and connect our interrupt. + */ + sc->aac_irq_rid = 0; + if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ, + &sc->aac_irq_rid, 0, ~0, 1, + RF_SHAREABLE | + RF_ACTIVE)) == NULL) { + device_printf(sc->aac_dev, "can't allocate interrupt\n"); + goto out; + } +#ifndef INTR_ENTROPY +#define INTR_ENTROPY 0 +#endif + if (bus_setup_intr(sc->aac_dev, sc->aac_irq, + INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY, aac_intr, + sc, &sc->aac_intr)) { + device_printf(sc->aac_dev, "can't set up interrupt\n"); + goto out; + } + + /* assume failure is 'out of memory' */ + error = ENOMEM; + + /* + * Allocate the parent bus DMA tag appropriate for our PCI interface. + * + * Note that some of these controllers are 64-bit capable. + */ + if (bus_dma_tag_create(NULL, /* parent */ + PAGE_SIZE, 0, /* algnmnt, boundary */ + BUS_SPACE_MAXADDR, /* lowaddr */ + BUS_SPACE_MAXADDR, /* highaddr */ + NULL, NULL, /* filter, filterarg */ + BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ + AAC_MAXSGENTRIES, /* nsegments */ + BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ + 0, /* flags */ + &sc->aac_parent_dmat)) { + device_printf(sc->aac_dev, "can't allocate parent DMA tag\n"); + goto out; + } + + /* + * Detect the hardware interface version, set up the bus interface + * indirection. + */ + sc->aac_hwif = AAC_HWIF_UNKNOWN; + for (i = 0; aac_identifiers[i].vendor != 0; i++) { + if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) && + (aac_identifiers[i].device == pci_get_device(dev)) && + (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) && + (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) { + sc->aac_hwif = aac_identifiers[i].hwif; + switch(sc->aac_hwif) { + case AAC_HWIF_I960RX: + debug(2, "set hardware up for i960Rx"); + sc->aac_if = aac_rx_interface; + break; + + case AAC_HWIF_STRONGARM: + debug(2, "set hardware up for StrongARM"); + sc->aac_if = aac_sa_interface; + break; + case AAC_HWIF_FALCON: + debug(2, "set hardware up for Falcon/PPC"); + sc->aac_if = aac_fa_interface; + break; + } + + /* Set up quirks */ + sc->flags = aac_identifiers[i].quirks; + + break; + } + } + if (sc->aac_hwif == AAC_HWIF_UNKNOWN) { + device_printf(sc->aac_dev, "unknown hardware type\n"); + error = ENXIO; + goto out; + } + + + /* + * Do bus-independent initialisation. + */ + error = aac_attach(sc); + +out: + if (error) + aac_free(sc); + return(error); +} diff --git a/sys/dev/aac/aac_tables.h b/sys/dev/aac/aac_tables.h new file mode 100644 index 0000000..47ebd03 --- /dev/null +++ b/sys/dev/aac/aac_tables.h @@ -0,0 +1,118 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2000 BSDi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Status codes for block read/write commands, etc. + * + * XXX many of these would not normally be returned, as they are + * relevant only to FSA operations. + */ +static struct aac_code_lookup aac_command_status_table[] = { + {"OK", 0}, + {"operation not permitted", 1}, + {"not found", 2}, + {"I/O error", 5}, + {"device not configured", 6}, + {"too big", 7}, + {"permission denoed", 13}, + {"file exists", 17}, + {"cross-device link", 18}, + {"operation not supported by device", 19}, + {"not a directory", 20}, + {"is a directory", 21}, + {"invalid argument", 22}, + {"file too large", 27}, + {"no space on device", 28}, + {"readonly filesystem", 30}, + {"too many links", 31}, + {"operation would block", 35}, + {"file name too long", 63}, + {"directory not empty", 66}, + {"quota exceeded", 69}, + {"stale file handle", 70}, + {"too many levels of remote in path", 71}, + {"bad file handle", 10001}, + {"not sync", 10002}, + {"bad cookie", 10003}, + {"operation not supported", 10004}, + {"too small", 10005}, + {"server fault", 10006}, + {"bad type", 10007}, + {"jukebox", 10008}, + {"not mounted", 10009}, + {"in maintenace mode", 10010}, + {"stale ACL", 10011}, + {NULL, 0}, + {"unknown command status", 0} +}; + +#define AAC_COMMAND_STATUS(x) aac_describe_code(aac_command_status_table, x) + +static struct aac_code_lookup aac_cpu_variant[] = { + {"i960JX", CPUI960_JX}, + {"i960CX", CPUI960_CX}, + {"i960HX", CPUI960_HX}, + {"i960RX", CPUI960_RX}, + {"StrongARM SA110", CPUARM_SA110}, + {"MPC824x", CPUMPC_824x}, + {"Unknown StrongARM", CPUARM_xxx}, + {"Unknown PowerPC", CPUPPC_xxx}, + {NULL, 0}, + {"Unknown processor", 0} +}; + +static struct aac_code_lookup aac_battery_platform[] = { + {"required battery present", PLATFORM_BAT_REQ_PRESENT}, + {"REQUIRED BATTERY NOT PRESENT", PLATFORM_BAT_REQ_NOTPRESENT}, + {"optional battery present", PLATFORM_BAT_OPT_PRESENT}, + {"optional battery not installed", PLATFORM_BAT_OPT_NOTPRESENT}, + {"no battery support", PLATFORM_BAT_NOT_SUPPORTED}, + {NULL, 0}, + {"unknown battery platform", 0} +}; + +static struct aac_code_lookup aac_container_types[] = { + {"Volume", CT_VOLUME}, + {"RAID 1 (Mirror)", CT_MIRROR}, + {"RAID 0 (Stripe)", CT_STRIPE}, + {"RAID 5", CT_RAID5}, + {"SSRW", CT_SSRW}, + {"SSRO", CT_SSRO}, + {"Morph", CT_MORPH}, + {"Passthrough", CT_PASSTHRU}, + {"RAID 4", CT_RAID4}, + {"RAID 0/1", CT_RAID10}, + {"RAID 0/0", CT_RAID00}, + {"Volume of Mirrors", CT_VOLUME_OF_MIRRORS}, + {"Pseudo RAID 3", CT_PSEUDO_RAID3}, + {"RAID 0/5", CT_RAID50}, + {NULL, 0}, + {"unknown", 0} +}; + diff --git a/sys/dev/aac/aacreg.h b/sys/dev/aac/aacreg.h new file mode 100644 index 0000000..dd08437 --- /dev/null +++ b/sys/dev/aac/aacreg.h @@ -0,0 +1,1367 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2000-2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Data structures defining the interface between the driver and the Adaptec + * 'FSA' adapters. Note that many field names and comments here are taken + * verbatim from the Adaptec driver source in order to make comparing the + * two slightly easier. + */ + +/* + * Misc. magic numbers. + */ +#define AAC_MAX_CONTAINERS 64 +#define AAC_BLOCK_SIZE 512 + +/* + * Communications interface. + * + * Where datastructure layouts are closely parallel to the Adaptec sample code, + * retain their naming conventions (for now) to aid in cross-referencing. + */ + +/* + * We establish 4 command queues and matching response queues. Queues must + * be 16-byte aligned, and are sized as follows: + */ +#define AAC_HOST_NORM_CMD_ENTRIES 8 /* command adapter->host, + * normal priority */ +#define AAC_HOST_HIGH_CMD_ENTRIES 4 /* command adapter->host, + * high priority */ +#define AAC_ADAP_NORM_CMD_ENTRIES 512 /* command host->adapter, + * normal priority */ +#define AAC_ADAP_HIGH_CMD_ENTRIES 4 /* command host->adapter, + * high priority */ +#define AAC_HOST_NORM_RESP_ENTRIES 512 /* response, adapter->host, + * normal priority */ +#define AAC_HOST_HIGH_RESP_ENTRIES 4 /* response, adapter->host, + * high priority */ +#define AAC_ADAP_NORM_RESP_ENTRIES 8 /* response, host->adapter, + * normal priority */ +#define AAC_ADAP_HIGH_RESP_ENTRIES 4 /* response, host->adapter, + * high priority */ + +#define AAC_TOTALQ_LENGTH (AAC_HOST_HIGH_CMD_ENTRIES + \ + AAC_HOST_NORM_CMD_ENTRIES + \ + AAC_ADAP_HIGH_CMD_ENTRIES + \ + AAC_ADAP_NORM_CMD_ENTRIES + \ + AAC_HOST_HIGH_RESP_ENTRIES + \ + AAC_HOST_NORM_RESP_ENTRIES + \ + AAC_ADAP_HIGH_RESP_ENTRIES + \ + AAC_ADAP_NORM_RESP_ENTRIES) +#define AAC_QUEUE_COUNT 8 +#define AAC_QUEUE_ALIGN 16 + +struct aac_queue_entry { + u_int32_t aq_fib_size; /* FIB size in bytes */ + u_int32_t aq_fib_addr; /* receiver-space address of the FIB */ +} __packed; + +#define AAC_PRODUCER_INDEX 0 +#define AAC_CONSUMER_INDEX 1 + +/* + * Table of queue indices and queues used to communicate with the + * controller. This structure must be aligned to AAC_QUEUE_ALIGN + */ +struct aac_queue_table { + /* queue consumer/producer indexes (layout mandated by adapter) */ + u_int32_t qt_qindex[AAC_QUEUE_COUNT][2]; + + /* queue entry structures (layout mandated by adapter) */ + struct aac_queue_entry qt_HostNormCmdQueue [AAC_HOST_NORM_CMD_ENTRIES]; + struct aac_queue_entry qt_HostHighCmdQueue [AAC_HOST_HIGH_CMD_ENTRIES]; + struct aac_queue_entry qt_AdapNormCmdQueue [AAC_ADAP_NORM_CMD_ENTRIES]; + struct aac_queue_entry qt_AdapHighCmdQueue [AAC_ADAP_HIGH_CMD_ENTRIES]; + struct aac_queue_entry qt_HostNormRespQueue[AAC_HOST_NORM_RESP_ENTRIES]; + struct aac_queue_entry qt_HostHighRespQueue[AAC_HOST_HIGH_RESP_ENTRIES]; + struct aac_queue_entry qt_AdapNormRespQueue[AAC_ADAP_NORM_RESP_ENTRIES]; + struct aac_queue_entry qt_AdapHighRespQueue[AAC_ADAP_HIGH_RESP_ENTRIES]; +} __packed; + +/* + * Queue names + * + * Note that we base these at 0 in order to use them as array indices. Adaptec + * used base 1 for some unknown reason, and sorted them in a different order. + */ +#define AAC_HOST_NORM_CMD_QUEUE 0 +#define AAC_HOST_HIGH_CMD_QUEUE 1 +#define AAC_ADAP_NORM_CMD_QUEUE 2 +#define AAC_ADAP_HIGH_CMD_QUEUE 3 +#define AAC_HOST_NORM_RESP_QUEUE 4 +#define AAC_HOST_HIGH_RESP_QUEUE 5 +#define AAC_ADAP_NORM_RESP_QUEUE 6 +#define AAC_ADAP_HIGH_RESP_QUEUE 7 + +/* + * List structure used to chain FIBs (used by the adapter - we hang FIBs off + * our private command structure and don't touch these) + */ +struct aac_fib_list_entry { + u_int32_t Flink; + u_int32_t Blink; +} __packed; + +/* + * FIB (FSA Interface Block?); this is the datastructure passed between the host + * and adapter. + */ +struct aac_fib_header { + u_int32_t XferState; + u_int16_t Command; + u_int8_t StructType; + u_int8_t Flags; + u_int16_t Size; + u_int16_t SenderSize; + u_int32_t SenderFibAddress; + u_int32_t ReceiverFibAddress; + u_int32_t SenderData; + union { + struct { + u_int32_t ReceiverTimeStart; + u_int32_t ReceiverTimeDone; + } _s; + struct aac_fib_list_entry FibLinks; + } _u; +} __packed; + +#define AAC_FIB_DATASIZE (512 - sizeof(struct aac_fib_header)) + +struct aac_fib { + struct aac_fib_header Header; + u_int8_t data[AAC_FIB_DATASIZE]; +} __packed; + +/* + * FIB commands + */ +typedef enum { + TestCommandResponse = 1, + TestAdapterCommand = 2, + + /* lowlevel and comm commands */ + LastTestCommand = 100, + ReinitHostNormCommandQueue = 101, + ReinitHostHighCommandQueue = 102, + ReinitHostHighRespQueue = 103, + ReinitHostNormRespQueue = 104, + ReinitAdapNormCommandQueue = 105, + ReinitAdapHighCommandQueue = 107, + ReinitAdapHighRespQueue = 108, + ReinitAdapNormRespQueue = 109, + InterfaceShutdown = 110, + DmaCommandFib = 120, + StartProfile = 121, + TermProfile = 122, + SpeedTest = 123, + TakeABreakPt = 124, + RequestPerfData = 125, + SetInterruptDefTimer= 126, + SetInterruptDefCount= 127, + GetInterruptDefStatus= 128, + LastCommCommand = 129, + + /* filesystem commands */ + NuFileSystem = 300, + UFS = 301, + HostFileSystem = 302, + LastFileSystemCommand = 303, + + /* Container Commands */ + ContainerCommand = 500, + ContainerCommand64 = 501, + + /* Cluster Commands */ + ClusterCommand = 550, + + /* Scsi Port commands (scsi passthrough) */ + ScsiPortCommand = 600, + + /* misc house keeping and generic adapter initiated commands */ + AifRequest = 700, + CheckRevision = 701, + FsaHostShutdown = 702, + RequestAdapterInfo = 703, + IsAdapterPaused = 704, + SendHostTime = 705, + LastMiscCommand = 706 +} AAC_FibCommands; + +/* + * FIB types + */ +#define AAC_FIBTYPE_TFIB 1 +#define AAC_FIBTYPE_TQE 2 +#define AAC_FIBTYPE_TCTPERF 3 + +/* + * FIB transfer state + */ +#define AAC_FIBSTATE_HOSTOWNED (1<<0) /* owned by the host */ +#define AAC_FIBSTATE_ADAPTEROWNED (1<<1) /* owned by the adapter */ +#define AAC_FIBSTATE_INITIALISED (1<<2) /* initialised */ +#define AAC_FIBSTATE_EMPTY (1<<3) /* empty */ +#define AAC_FIBSTATE_FROMPOOL (1<<4) /* allocated from pool */ +#define AAC_FIBSTATE_FROMHOST (1<<5) /* sent from the host */ +#define AAC_FIBSTATE_FROMADAP (1<<6) /* sent from the adapter */ +#define AAC_FIBSTATE_REXPECTED (1<<7) /* response is expected */ +#define AAC_FIBSTATE_RNOTEXPECTED (1<<8) /* response is not expected */ +#define AAC_FIBSTATE_DONEADAP (1<<9) /* processed by the adapter */ +#define AAC_FIBSTATE_DONEHOST (1<<10) /* processed by the host */ +#define AAC_FIBSTATE_HIGH (1<<11) /* high priority */ +#define AAC_FIBSTATE_NORM (1<<12) /* normal priority */ +#define AAC_FIBSTATE_ASYNC (1<<13) +#define AAC_FIBSTATE_ASYNCIO (1<<13) /* to be removed */ +#define AAC_FIBSTATE_PAGEFILEIO (1<<14) /* to be removed */ +#define AAC_FIBSTATE_SHUTDOWN (1<<15) +#define AAC_FIBSTATE_LAZYWRITE (1<<16) /* to be removed */ +#define AAC_FIBSTATE_ADAPMICROFIB (1<<17) +#define AAC_FIBSTATE_BIOSFIB (1<<18) +#define AAC_FIBSTATE_FAST_RESPONSE (1<<19) /* fast response capable */ +#define AAC_FIBSTATE_APIFIB (1<<20) + +/* + * FIB error values + */ +#define AAC_ERROR_NORMAL 0x00 +#define AAC_ERROR_PENDING 0x01 +#define AAC_ERROR_FATAL 0x02 +#define AAC_ERROR_INVALID_QUEUE 0x03 +#define AAC_ERROR_NOENTRIES 0x04 +#define AAC_ERROR_SENDFAILED 0x05 +#define AAC_ERROR_INVALID_QUEUE_PRIORITY 0x06 +#define AAC_ERROR_FIB_ALLOCATION_FAILED 0x07 +#define AAC_ERROR_FIB_DEALLOCATION_FAILED 0x08 + +/* + * Adapter Init Structure: this is passed to the adapter with the + * AAC_MONKER_INITSTRUCT command to point it at our control structures. + */ +struct aac_adapter_init { + u_int32_t InitStructRevision; +#define AAC_INIT_STRUCT_REVISION 3 + u_int32_t MiniPortRevision; +#define AAC_INIT_STRUCT_MINIPORT_REVISION 1 + u_int32_t FilesystemRevision; + u_int32_t CommHeaderAddress; + u_int32_t FastIoCommAreaAddress; + u_int32_t AdapterFibsPhysicalAddress; + u_int32_t AdapterFibsVirtualAddress; + u_int32_t AdapterFibsSize; + u_int32_t AdapterFibAlign; + u_int32_t PrintfBufferAddress; + u_int32_t PrintfBufferSize; +#define AAC_PAGE_SIZE 4096 + u_int32_t HostPhysMemPages; + u_int32_t HostElapsedSeconds; +} __packed; + +/* + * Shared data types + */ +/* + * Container types + */ +typedef enum { + CT_NONE = 0, + CT_VOLUME, + CT_MIRROR, + CT_STRIPE, + CT_RAID5, + CT_SSRW, + CT_SSRO, + CT_MORPH, + CT_PASSTHRU, + CT_RAID4, + CT_RAID10, /* stripe of mirror */ + CT_RAID00, /* stripe of stripe */ + CT_VOLUME_OF_MIRRORS, /* volume of mirror */ + CT_PSEUDO_RAID3, /* really raid4 */ + CT_RAID50, /* stripe of raid5 */ +} AAC_FSAVolType; + +/* + * Host-addressable object types + */ +typedef enum { + FT_REG = 1, /* regular file */ + FT_DIR, /* directory */ + FT_BLK, /* "block" device - reserved */ + FT_CHR, /* "character special" device - reserved */ + FT_LNK, /* symbolic link */ + FT_SOCK, /* socket */ + FT_FIFO, /* fifo */ + FT_FILESYS, /* ADAPTEC's "FSA"(tm) filesystem */ + FT_DRIVE, /* physical disk - addressable in scsi by b/t/l */ + FT_SLICE, /* virtual disk - raw volume - slice */ + FT_PARTITION, /* FSA partition - carved out of a slice - building + * block for containers */ + FT_VOLUME, /* Container - Volume Set */ + FT_STRIPE, /* Container - Stripe Set */ + FT_MIRROR, /* Container - Mirror Set */ + FT_RAID5, /* Container - Raid 5 Set */ + FT_DATABASE /* Storage object with "foreign" content manager */ +} AAC_FType; + +/* + * Host-side scatter/gather list for 32-bit commands. + */ +struct aac_sg_entry { + u_int32_t SgAddress; + u_int32_t SgByteCount; +} __packed; + +struct aac_sg_entry64 { + u_int64_t SgAddress; + u_int32_t SgByteCount; +} __packed; + +struct aac_sg_table { + u_int32_t SgCount; + struct aac_sg_entry SgEntry[0]; +} __packed; + +/* + * Host-side scatter/gather list for 64-bit commands. + */ +struct aac_sg_table64 { + u_int32_t SgCount; + struct aac_sg_entry64 SgEntry64[0]; +} __packed; + +/* + * Container creation data + */ +struct aac_container_creation { + u_int8_t ViaBuildNumber; + u_int8_t MicroSecond; + u_int8_t Via; /* 1 = FSU, 2 = API, etc. */ + u_int8_t YearsSince1900; + u_int32_t Month:4; /* 1-12 */ + u_int32_t Day:6; /* 1-32 */ + u_int32_t Hour:6; /* 0-23 */ + u_int32_t Minute:6; /* 0-59 */ + u_int32_t Second:6; /* 0-59 */ + u_int64_t ViaAdapterSerialNumber; +} __packed; + +/* + * Revision number handling + */ + +typedef enum { + RevApplication = 1, + RevDkiCli, + RevNetService, + RevApi, + RevFileSysDriver, + RevMiniportDriver, + RevAdapterSW, + RevMonitor, + RevRemoteApi +} RevComponent; + +struct FsaRevision { + union { + struct { + u_int8_t dash; + u_int8_t type; + u_int8_t minor; + u_int8_t major; + } comp; + u_int32_t ul; + } external; + u_int32_t buildNumber; +} __packed; + +/* + * Adapter Information + */ + +typedef enum { + CPU_NTSIM = 1, + CPU_I960, + CPU_ARM, + CPU_SPARC, + CPU_POWERPC, + CPU_ALPHA, + CPU_P7, + CPU_I960_RX, + CPU__last +} AAC_CpuType; + +typedef enum { + CPUI960_JX = 1, + CPUI960_CX, + CPUI960_HX, + CPUI960_RX, + CPUARM_SA110, + CPUARM_xxx, + CPUMPC_824x, + CPUPPC_xxx, + CPUSUBTYPE__last +} AAC_CpuSubType; + +typedef enum { + PLAT_NTSIM = 1, + PLAT_V3ADU, + PLAT_CYCLONE, + PLAT_CYCLONE_HD, + PLAT_BATBOARD, + PLAT_BATBOARD_HD, + PLAT_YOLO, + PLAT_COBRA, + PLAT_ANAHEIM, + PLAT_JALAPENO, + PLAT_QUEENS, + PLAT_JALAPENO_DELL, + PLAT_POBLANO, + PLAT_POBLANO_OPAL, + PLAT_POBLANO_SL0, + PLAT_POBLANO_SL1, + PLAT_POBLANO_SL2, + PLAT_POBLANO_XXX, + PLAT_JALAPENO_P2, + PLAT_HABANERO, + PLAT__last +} AAC_Platform; + +typedef enum { + OEM_FLAVOR_ADAPTEC = 1, + OEM_FLAVOR_DELL, + OEM_FLAVOR_HP, + OEM_FLAVOR_IBM, + OEM_FLAVOR_CPQ, + OEM_FLAVOR_BRAND_X, + OEM_FLAVOR_BRAND_Y, + OEM_FLAVOR_BRAND_Z, + OEM_FLAVOR__last +} AAC_OemFlavor; + +/* + * XXX the aac-2622 with no battery present reports PLATFORM_BAT_OPT_PRESENT + */ +typedef enum +{ + PLATFORM_BAT_REQ_PRESENT = 1, /* BATTERY REQUIRED AND PRESENT */ + PLATFORM_BAT_REQ_NOTPRESENT, /* BATTERY REQUIRED AND NOT PRESENT */ + PLATFORM_BAT_OPT_PRESENT, /* BATTERY OPTIONAL AND PRESENT */ + PLATFORM_BAT_OPT_NOTPRESENT, /* BATTERY OPTIONAL AND NOT PRESENT */ + PLATFORM_BAT_NOT_SUPPORTED /* BATTERY NOT SUPPORTED */ +} AAC_BatteryPlatform; + +/* + * options supported by this board + * there has to be a one to one mapping of these defines and the ones in + * fsaapi.h, search for FSA_SUPPORT_SNAPSHOT + */ +#define AAC_SUPPORTED_SNAPSHOT 0x01 +#define AAC_SUPPORTED_CLUSTERS 0x02 +#define AAC_SUPPORTED_WRITE_CACHE 0x04 +#define AAC_SUPPORTED_64BIT_DATA 0x08 +#define AAC_SUPPORTED_HOST_TIME_FIB 0x10 +#define AAC_SUPPORTED_RAID50 0x20 +#define AAC_SUPPORTED_4GB_WINDOW 0x40 +#define AAC_SUPPORTED_SCSI_UPGRADEABLE 0x80 +#define AAC_SUPPORTED_SOFT_ERR_REPORT 0x100 +#define AAC_SUPPORTED_NOT_RECONDITION 0x200 +#define AAC_SUPPORTED_SGMAP_HOST64 0x400 +#define AAC_SUPPORTED_ALARM 0x800 +#define AAC_SUPPORTED_NONDASD 0x1000 + +/* + * Structure used to respond to a RequestAdapterInfo fib. + */ +struct aac_adapter_info { + AAC_Platform PlatformBase; /* adapter type */ + AAC_CpuType CpuArchitecture; /* adapter CPU type */ + AAC_CpuSubType CpuVariant; /* adapter CPU subtype */ + u_int32_t ClockSpeed; /* adapter CPU clockspeed */ + u_int32_t ExecutionMem; /* adapter Execution Memory + * size */ + u_int32_t BufferMem; /* adapter Data Memory */ + u_int32_t TotalMem; /* adapter Total Memory */ + struct FsaRevision KernelRevision; /* adapter Kernel Software + * Revision */ + struct FsaRevision MonitorRevision; /* adapter Monitor/Diagnostic + * Software Revision */ + struct FsaRevision HardwareRevision;/* TBD */ + struct FsaRevision BIOSRevision; /* adapter BIOS Revision */ + u_int32_t ClusteringEnabled; + u_int32_t ClusterChannelMask; + u_int64_t SerialNumber; + AAC_BatteryPlatform batteryPlatform; + u_int32_t SupportedOptions; /* supported features of this + * controller */ + AAC_OemFlavor OemVariant; +} __packed; + +/* + * Monitor/Kernel interface. + */ + +/* + * Synchronous commands to the monitor/kernel. + */ +#define AAC_MONKER_INITSTRUCT 0x05 +#define AAC_MONKER_SYNCFIB 0x0c +#define AAC_MONKER_GETKERNVER 0x11 +#define AAC_MONKER_GETINFO 0x19 + +/* + * Adapter Status Register + * + * Phase Staus mailbox is 32bits: + * <31:16> = Phase Status + * <15:0> = Phase + * + * The adapter reports its present state through the phase. Only + * a single phase should be ever be set. Each phase can have multiple + * phase status bits to provide more detailed information about the + * state of the adapter. + */ +#define AAC_SELF_TEST_FAILED 0x00000004 +#define AAC_UP_AND_RUNNING 0x00000080 +#define AAC_KERNEL_PANIC 0x00000100 + +/* + * Data types relating to control and monitoring of the NVRAM/WriteCache + * subsystem. + */ + +#define AAC_NFILESYS 24 /* maximum number of filesystems */ + +/* + * NVRAM/Write Cache subsystem states + */ +typedef enum { + NVSTATUS_DISABLED = 0, /* present, clean, not being used */ + NVSTATUS_ENABLED, /* present, possibly dirty, ready for use */ + NVSTATUS_ERROR, /* present, dirty, contains dirty data */ + NVSTATUS_BATTERY, /* present, bad or low battery, may contain + * dirty data */ + NVSTATUS_UNKNOWN /* for bad/missing device */ +} AAC_NVSTATUS; + +/* + * NVRAM/Write Cache subsystem battery component states + * + */ +typedef enum { + NVBATTSTATUS_NONE = 0, /* battery has no power or is not present */ + NVBATTSTATUS_LOW, /* battery is low on power */ + NVBATTSTATUS_OK, /* battery is okay - normal operation possible + * only in this state */ + NVBATTSTATUS_RECONDITIONING /* no battery present - reconditioning + * in process */ +} AAC_NVBATTSTATUS; + +/* + * Battery transition type + */ +typedef enum { + NVBATT_TRANSITION_NONE = 0, /* battery now has no power or is not + * present */ + NVBATT_TRANSITION_LOW, /* battery is now low on power */ + NVBATT_TRANSITION_OK /* battery is now okay - normal + * operation possible only in this + * state */ +} AAC_NVBATT_TRANSITION; + +/* + * NVRAM Info structure returned for NVRAM_GetInfo call + */ +struct aac_nvramdevinfo { + u_int32_t NV_Enabled; /* write caching enabled */ + u_int32_t NV_Error; /* device in error state */ + u_int32_t NV_NDirty; /* count of dirty NVRAM buffers */ + u_int32_t NV_NActive; /* count of NVRAM buffers being + * written */ +} __packed; + +struct aac_nvraminfo { + AAC_NVSTATUS NV_Status; /* nvram subsystem status */ + AAC_NVBATTSTATUS NV_BattStatus; /* battery status */ + u_int32_t NV_Size; /* size of WriteCache NVRAM in + * bytes */ + u_int32_t NV_BufSize; /* size of NVRAM buffers in + * bytes */ + u_int32_t NV_NBufs; /* number of NVRAM buffers */ + u_int32_t NV_NDirty; /* Num dirty NVRAM buffers */ + u_int32_t NV_NClean; /* Num clean NVRAM buffers */ + u_int32_t NV_NActive; /* Num NVRAM buffers being + * written */ + u_int32_t NV_NBrokered; /* Num brokered NVRAM buffers */ + struct aac_nvramdevinfo NV_DevInfo[AAC_NFILESYS]; /* per device + * info */ + u_int32_t NV_BattNeedsReconditioning; /* boolean */ + u_int32_t NV_TotalSize; /* size of all non-volatile + * memories in bytes */ +} __packed; + +/* + * Data types relating to adapter-initiated FIBs + * + * Based on types and structures in <aifstruc.h> + */ + +/* + * Progress Reports + */ +typedef enum { + AifJobStsSuccess = 1, + AifJobStsFinished, + AifJobStsAborted, + AifJobStsFailed, + AifJobStsLastReportMarker = 100, /* All prior mean last report */ + AifJobStsSuspended, + AifJobStsRunning +} AAC_AifJobStatus; + +typedef enum { + AifJobScsiMin = 1, /* Minimum value for Scsi operation */ + AifJobScsiZero, /* SCSI device clear operation */ + AifJobScsiVerify, /* SCSI device Verify operation NO + * REPAIR */ + AifJobScsiExercise, /* SCSI device Exercise operation */ + AifJobScsiVerifyRepair, /* SCSI device Verify operation WITH + * repair */ + AifJobScsiMax = 99, /* Max Scsi value */ + AifJobCtrMin, /* Min Ctr op value */ + AifJobCtrZero, /* Container clear operation */ + AifJobCtrCopy, /* Container copy operation */ + AifJobCtrCreateMirror, /* Container Create Mirror operation */ + AifJobCtrMergeMirror, /* Container Merge Mirror operation */ + AifJobCtrScrubMirror, /* Container Scrub Mirror operation */ + AifJobCtrRebuildRaid5, /* Container Rebuild Raid5 operation */ + AifJobCtrScrubRaid5, /* Container Scrub Raid5 operation */ + AifJobCtrMorph, /* Container morph operation */ + AifJobCtrPartCopy, /* Container Partition copy operation */ + AifJobCtrRebuildMirror, /* Container Rebuild Mirror operation */ + AifJobCtrCrazyCache, /* crazy cache */ + AifJobCtrMax = 199, /* Max Ctr type operation */ + AifJobFsMin, /* Min Fs type operation */ + AifJobFsCreate, /* File System Create operation */ + AifJobFsVerify, /* File System Verify operation */ + AifJobFsExtend, /* File System Extend operation */ + AifJobFsMax = 299, /* Max Fs type operation */ + AifJobApiFormatNTFS, /* Format a drive to NTFS */ + AifJobApiFormatFAT, /* Format a drive to FAT */ + AifJobApiUpdateSnapshot, /* update the read/write half of a + * snapshot */ + AifJobApiFormatFAT32, /* Format a drive to FAT32 */ + AifJobApiMax = 399, /* Max API type operation */ + AifJobCtlContinuousCtrVerify, /* Adapter operation */ + AifJobCtlMax = 499 /* Max Adapter type operation */ +} AAC_AifJobType; + +struct aac_AifContainers { + u_int32_t src; /* from/master */ + u_int32_t dst; /* to/slave */ +} __packed; + +union aac_AifJobClient { + struct aac_AifContainers container; /* For Container and + * filesystem progress + * ops; */ + int32_t scsi_dh; /* For SCSI progress + * ops */ +}; + +struct aac_AifJobDesc { + u_int32_t jobID; /* DO NOT FILL IN! Will be + * filled in by AIF */ + AAC_AifJobType type; /* Operation that is being + * performed */ + union aac_AifJobClient client; /* Details */ +} __packed; + +struct aac_AifJobProgressReport { + struct aac_AifJobDesc jd; + AAC_AifJobStatus status; + u_int32_t finalTick; + u_int32_t currentTick; + u_int32_t jobSpecificData1; + u_int32_t jobSpecificData2; +} __packed; + +/* + * Event Notification + */ +typedef enum { + /* General application notifies start here */ + AifEnGeneric = 1, /* Generic notification */ + AifEnTaskComplete, /* Task has completed */ + AifEnConfigChange, /* Adapter config change occurred */ + AifEnContainerChange, /* Adapter specific container + * configuration change */ + AifEnDeviceFailure, /* SCSI device failed */ + AifEnMirrorFailover, /* Mirror failover started */ + AifEnContainerEvent, /* Significant container event */ + AifEnFileSystemChange, /* File system changed */ + AifEnConfigPause, /* Container pause event */ + AifEnConfigResume, /* Container resume event */ + AifEnFailoverChange, /* Failover space assignment changed */ + AifEnRAID5RebuildDone, /* RAID5 rebuild finished */ + AifEnEnclosureManagement, /* Enclosure management event */ + AifEnBatteryEvent, /* Significant NV battery event */ + AifEnAddContainer, /* A new container was created. */ + AifEnDeleteContainer, /* A container was deleted. */ + AifEnSMARTEvent, /* SMART Event */ + AifEnBatteryNeedsRecond, /* The battery needs reconditioning */ + AifEnClusterEvent, /* Some cluster event */ + AifEnDiskSetEvent, /* A disk set event occured. */ + AifDriverNotifyStart=199, /* Notifies for host driver go here */ + /* Host driver notifications start here */ + AifDenMorphComplete, /* A morph operation completed */ + AifDenVolumeExtendComplete /* Volume expand operation completed */ +} AAC_AifEventNotifyType; + +struct aac_AifEnsGeneric { + char text[132]; /* Generic text */ +} __packed; + +struct aac_AifEnsDeviceFailure { + u_int32_t deviceHandle; /* SCSI device handle */ +} __packed; + +struct aac_AifEnsMirrorFailover { + u_int32_t container; /* Container with failed element */ + u_int32_t failedSlice; /* Old slice which failed */ + u_int32_t creatingSlice; /* New slice used for auto-create */ +} __packed; + +struct aac_AifEnsContainerChange { + u_int32_t container[2]; /* container that changed, -1 if no + * container */ +} __packed; + +struct aac_AifEnsContainerEvent { + u_int32_t container; /* container number */ + u_int32_t eventType; /* event type */ +} __packed; + +struct aac_AifEnsEnclosureEvent { + u_int32_t empID; /* enclosure management proc number */ + u_int32_t unitID; /* unitId, fan id, power supply id, + * slot id, tempsensor id. */ + u_int32_t eventType; /* event type */ +} __packed; + +struct aac_AifEnsBatteryEvent { + AAC_NVBATT_TRANSITION transition_type; /* eg from low to ok */ + AAC_NVBATTSTATUS current_state; /* current batt state */ + AAC_NVBATTSTATUS prior_state; /* prev batt state */ +} __packed; + +struct aac_AifEnsDiskSetEvent { + u_int32_t eventType; + u_int64_t DsNum; + u_int64_t CreatorId; +} __packed; + +typedef enum { + CLUSTER_NULL_EVENT = 0, + CLUSTER_PARTNER_NAME_EVENT, /* change in partner hostname or + * adaptername from NULL to non-NULL */ + /* (partner's agent may be up) */ + CLUSTER_PARTNER_NULL_NAME_EVENT /* change in partner hostname or + * adaptername from non-null to NULL */ + /* (partner has rebooted) */ +} AAC_ClusterAifEvent; + +struct aac_AifEnsClusterEvent { + AAC_ClusterAifEvent eventType; +} __packed; + +struct aac_AifEventNotify { + AAC_AifEventNotifyType type; + union { + struct aac_AifEnsGeneric EG; + struct aac_AifEnsDeviceFailure EDF; + struct aac_AifEnsMirrorFailover EMF; + struct aac_AifEnsContainerChange ECC; + struct aac_AifEnsContainerEvent ECE; + struct aac_AifEnsEnclosureEvent EEE; + struct aac_AifEnsBatteryEvent EBE; + struct aac_AifEnsDiskSetEvent EDS; +/* struct aac_AifEnsSMARTEvent ES;*/ + struct aac_AifEnsClusterEvent ECLE; + } data; +} __packed; + +/* + * Adapter Initiated FIB command structures. Start with the adapter + * initiated FIBs that really come from the adapter, and get responded + * to by the host. + */ +#define AAC_AIF_REPORT_MAX_SIZE 64 + +typedef enum { + AifCmdEventNotify = 1, /* Notify of event */ + AifCmdJobProgress, /* Progress report */ + AifCmdAPIReport, /* Report from other user of API */ + AifCmdDriverNotify, /* Notify host driver of event */ + AifReqJobList = 100, /* Gets back complete job list */ + AifReqJobsForCtr, /* Gets back jobs for specific container */ + AifReqJobsForScsi, /* Gets back jobs for specific SCSI device */ + AifReqJobReport, /* Gets back a specific job report or list */ + AifReqTerminateJob, /* Terminates job */ + AifReqSuspendJob, /* Suspends a job */ + AifReqResumeJob, /* Resumes a job */ + AifReqSendAPIReport, /* API generic report requests */ + AifReqAPIJobStart, /* Start a job from the API */ + AifReqAPIJobUpdate, /* Update a job report from the API */ + AifReqAPIJobFinish /* Finish a job from the API */ +} AAC_AifCommand; + +struct aac_aif_command { + AAC_AifCommand command; /* Tell host what type of + * notify this is */ + u_int32_t seqNumber; /* To allow ordering of + * reports (if necessary) */ + union { + struct aac_AifEventNotify EN; /* Event notify structure */ + struct aac_AifJobProgressReport PR[1]; /* Progress report */ + u_int8_t AR[AAC_AIF_REPORT_MAX_SIZE]; + } data; +} __packed; + +/* + * Filesystem commands/data + * + * The adapter has a very complex filesystem interface, most of which we ignore. + * (And which seems not to be implemented, anyway.) + */ + +/* + * FSA commands + * (not used?) + */ +typedef enum { + Null = 0, + GetAttributes, + SetAttributes, + Lookup, + ReadLink, + Read, + Write, + Create, + MakeDirectory, + SymbolicLink, + MakeNode, + Removex, + RemoveDirectory, + Rename, + Link, + ReadDirectory, + ReadDirectoryPlus, + FileSystemStatus, + FileSystemInfo, + PathConfigure, + Commit, + Mount, + UnMount, + Newfs, + FsCheck, + FsSync, + SimReadWrite, + SetFileSystemStatus, + BlockRead, + BlockWrite, + NvramIoctl, + FsSyncWait, + ClearArchiveBit, + SetAcl, + GetAcl, + AssignAcl, + FaultInsertion, + CrazyCache +} AAC_FSACommand; + +/* + * Command status values + */ +typedef enum { + ST_OK = 0, + ST_PERM = 1, + ST_NOENT = 2, + ST_IO = 5, + ST_NXIO = 6, + ST_E2BIG = 7, + ST_ACCES = 13, + ST_EXIST = 17, + ST_XDEV = 18, + ST_NODEV = 19, + ST_NOTDIR = 20, + ST_ISDIR = 21, + ST_INVAL = 22, + ST_FBIG = 27, + ST_NOSPC = 28, + ST_ROFS = 30, + ST_MLINK = 31, + ST_WOULDBLOCK = 35, + ST_NAMETOOLONG = 63, + ST_NOTEMPTY = 66, + ST_DQUOT = 69, + ST_STALE = 70, + ST_REMOTE = 71, + ST_BADHANDLE = 10001, + ST_NOT_SYNC = 10002, + ST_BAD_COOKIE = 10003, + ST_NOTSUPP = 10004, + ST_TOOSMALL = 10005, + ST_SERVERFAULT = 10006, + ST_BADTYPE = 10007, + ST_JUKEBOX = 10008, + ST_NOTMOUNTED = 10009, + ST_MAINTMODE = 10010, + ST_STALEACL = 10011 +} AAC_FSAStatus; + +/* + * Volume manager commands + */ +typedef enum _VM_COMMANDS { + VM_Null = 0, + VM_NameServe, + VM_ContainerConfig, + VM_Ioctl, + VM_FilesystemIoctl, + VM_CloseAll, + VM_CtBlockRead, + VM_CtBlockWrite, + VM_SliceBlockRead, /* raw access to configured storage objects */ + VM_SliceBlockWrite, + VM_DriveBlockRead, /* raw access to physical devices */ + VM_DriveBlockWrite, + VM_EnclosureMgt, /* enclosure management */ + VM_Unused, /* used to be diskset management */ + VM_CtBlockVerify, + VM_CtPerf, /* performance test */ + VM_CtBlockRead64, + VM_CtBlockWrite64, + VM_CtBlockVerify64, + VM_CtHostRead64, + VM_CtHostWrite64, +} AAC_VMCommand; + +/* + * "mountable object" + */ +struct aac_mntobj { + u_int32_t ObjectId; + char FileSystemName[16]; + struct aac_container_creation CreateInfo; + u_int32_t Capacity; + u_int32_t VolType; + u_int32_t ObjType; + u_int32_t ContentState; +#define FSCS_READONLY 0x0002 /* XXX need more information + * than this */ + union { + u_int32_t pad[8]; + } ObjExtension; + u_int32_t AlterEgoId; +} __packed; + +struct aac_mntinfo { + u_int32_t Command; + u_int32_t MntType; + u_int32_t MntCount; +} __packed; + +struct aac_mntinforesp { + u_int32_t Status; + u_int32_t MntType; + u_int32_t MntRespCount; + struct aac_mntobj MntTable[1]; +} __packed; + +/* + * Container shutdown command. + */ +struct aac_closecommand { + u_int32_t Command; + u_int32_t ContainerId; +} __packed; + +/* + * Container Config Command + */ +#define CT_GET_SCSI_METHOD 64 +struct aac_ctcfg { + u_int32_t Command; + u_int32_t cmd; + u_int32_t param; +} __packed; + +struct aac_ctcfg_resp { + u_int32_t Status; + u_int32_t resp; + u_int32_t param; +} __packed; + +/* + * 'Ioctl' commads + */ +#define AAC_SCSI_MAX_PORTS 10 +#define AAC_BUS_NO_EXIST 0 +#define AAC_BUS_VALID 1 +#define AAC_BUS_FAULTED 2 +#define AAC_BUS_DISABLED 3 +#define GetBusInfo 0x9 + +struct aac_getbusinf { + u_int32_t ProbeComplete; + u_int32_t BusCount; + u_int32_t TargetsPerBus; + u_int8_t InitiatorBusId[AAC_SCSI_MAX_PORTS]; + u_int8_t BusValid[AAC_SCSI_MAX_PORTS]; +} __packed; + +struct aac_vmioctl { + u_int32_t Command; + u_int32_t ObjType; + u_int32_t MethId; + u_int32_t ObjId; + u_int32_t IoctlCmd; + u_int32_t IoctlBuf[1]; /* Placeholder? */ +} __packed; + +struct aac_vmi_businf_resp { + u_int32_t Status; + u_int32_t ObjType; + u_int32_t MethId; + u_int32_t ObjId; + u_int32_t IoctlCmd; + struct aac_getbusinf BusInf; +} __packed; + +#define AAC_BTL_TO_HANDLE(b, t, l) \ + (((b & 0x3f) << 7) | ((l & 0x7) << 4) | (t & 0xf)) +#define GetDeviceProbeInfo 0x5 + +struct aac_vmi_devinfo_resp { + u_int32_t Status; + u_int32_t ObjType; + u_int32_t MethId; + u_int32_t ObjId; + u_int32_t IoctlCmd; + u_int8_t VendorId[8]; + u_int8_t ProductId[16]; + u_int8_t ProductRev[4]; + u_int32_t Inquiry7; + u_int32_t align1; + u_int32_t Inquiry0; + u_int32_t align2; + u_int32_t Inquiry1; + u_int32_t align3; + u_int32_t reserved[2]; + u_int8_t VendorSpecific[20]; + u_int32_t Smart:1; + u_int32_t AAC_Managed:1; + u_int32_t align4; + u_int32_t reserved2:6; + u_int32_t Bus; + u_int32_t Target; + u_int32_t Lun; + u_int32_t ultraEnable:1, + disconnectEnable:1, + fast20EnabledW:1, + scamDevice:1, + scamTolerant:1, + setForSync:1, + setForWide:1, + syncDevice:1, + wideDevice:1, + reserved1:7, + ScsiRate:8, + ScsiOffset:8; +}; /* Do not pack */ + +#define ResetBus 0x16 +struct aac_resetbus { + u_int32_t BusNumber; +}; + +/* + * Write 'stability' options. + */ +typedef enum { + CSTABLE = 1, + CUNSTABLE +} AAC_CacheLevel; + +/* + * Commit level response for a write request. + */ +typedef enum { + CMFILE_SYNC_NVRAM = 1, + CMDATA_SYNC_NVRAM, + CMFILE_SYNC, + CMDATA_SYNC, + CMUNSTABLE +} AAC_CommitLevel; + +/* + * Block read/write operations. + * These structures are packed into the 'data' area in the FIB. + */ + +struct aac_blockread { + u_int32_t Command; /* not FSACommand! */ + u_int32_t ContainerId; + u_int32_t BlockNumber; + u_int32_t ByteCount; + struct aac_sg_table SgMap; /* variable size */ +} __packed; + +struct aac_blockread64 { + u_int32_t Command; + u_int16_t ContainerId; + u_int16_t SectorCount; + u_int32_t BlockNumber; + u_int16_t Pad; + u_int16_t Flags; + struct aac_sg_table64 SgMap64; +} __packed; + +struct aac_blockread_response { + u_int32_t Status; + u_int32_t ByteCount; +} __packed; + +struct aac_blockwrite { + u_int32_t Command; /* not FSACommand! */ + u_int32_t ContainerId; + u_int32_t BlockNumber; + u_int32_t ByteCount; + u_int32_t Stable; + struct aac_sg_table SgMap; /* variable size */ +} __packed; + +struct aac_blockwrite64 { + u_int32_t Command; /* not FSACommand! */ + u_int16_t ContainerId; + u_int16_t SectorCount; + u_int32_t BlockNumber; + u_int16_t Pad; + u_int16_t Flags; + struct aac_sg_table64 SgMap64; /* variable size */ +} __packed; + +struct aac_blockwrite_response { + u_int32_t Status; + u_int32_t ByteCount; + u_int32_t Committed; +} __packed; + +/* + * Container shutdown command. + */ +struct aac_close_command { + u_int32_t Command; + u_int32_t ContainerId; +}; + +/* + * SCSI Passthrough structures + */ +struct aac_srb32 { + u_int32_t function; + u_int32_t bus; + u_int32_t target; + u_int32_t lun; + u_int32_t timeout; + u_int32_t flags; + u_int32_t data_len; + u_int32_t retry_limit; + u_int32_t cdb_len; + u_int8_t cdb[16]; + struct aac_sg_table sg_map32; +}; + +enum { + AAC_SRB_FUNC_EXECUTE_SCSI = 0x00, + AAC_SRB_FUNC_CLAIM_DEVICE, + AAC_SRB_FUNC_IO_CONTROL, + AAC_SRB_FUNC_RECEIVE_EVENT, + AAC_SRB_FUNC_RELEASE_QUEUE, + AAC_SRB_FUNC_ATTACH_DEVICE, + AAC_SRB_FUNC_RELEASE_DEVICE, + AAC_SRB_FUNC_SHUTDOWN, + AAC_SRB_FUNC_FLUSH, + AAC_SRB_FUNC_ABORT_COMMAND = 0x10, + AAC_SRB_FUNC_RELEASE_RECOVERY, + AAC_SRB_FUNC_RESET_BUS, + AAC_SRB_FUNC_RESET_DEVICE, + AAC_SRB_FUNC_TERMINATE_IO, + AAC_SRB_FUNC_FLUSH_QUEUE, + AAC_SRB_FUNC_REMOVE_DEVICE, + AAC_SRB_FUNC_DOMAIN_VALIDATION +}; + +#define AAC_SRB_FLAGS_NO_DATA_XFER 0x0000 +#define AAC_SRB_FLAGS_DISABLE_DISCONNECT 0x0004 +#define AAC_SRB_FLAGS_DISABLE_SYNC_TRANSFER 0x0008 +#define AAC_SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x0010 +#define AAC_SRB_FLAGS_DISABLE_AUTOSENSE 0x0020 +#define AAC_SRB_FLAGS_DATA_IN 0x0040 +#define AAC_SRB_FLAGS_DATA_OUT 0x0080 +#define AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION \ + (AAC_SRB_FLAGS_DATA_IN | AAC_SRB_FLAGS_DATA_OUT) + +#define AAC_HOST_SENSE_DATA_MAX 30 + +struct aac_srb_response { + u_int32_t fib_status; + u_int32_t srb_status; + u_int32_t scsi_status; + u_int32_t data_len; + u_int32_t sense_len; + u_int8_t sense[AAC_HOST_SENSE_DATA_MAX]; +}; + +enum { + AAC_SRB_STS_PENDING = 0x00, + AAC_SRB_STS_SUCCESS, + AAC_SRB_STS_ABORTED, + AAC_SRB_STS_ABORT_FAILED, + AAC_SRB_STS_ERROR, + AAC_SRB_STS_BUSY, + AAC_SRB_STS_INVALID_REQUEST, + AAC_SRB_STS_INVALID_PATH_ID, + AAC_SRB_STS_NO_DEVICE, + AAC_SRB_STS_TIMEOUT, + AAC_SRB_STS_SELECTION_TIMEOUT, + AAC_SRB_STS_COMMAND_TIMEOUT, + AAC_SRB_STS_MESSAGE_REJECTED = 0x0D, + AAC_SRB_STS_BUS_RESET, + AAC_SRB_STS_PARITY_ERROR, + AAC_SRB_STS_REQUEST_SENSE_FAILED, + AAC_SRB_STS_NO_HBA, + AAC_SRB_STS_DATA_OVERRUN, + AAC_SRB_STS_UNEXPECTED_BUS_FREE, + AAC_SRB_STS_PHASE_SEQUENCE_FAILURE, + AAC_SRB_STS_BAD_SRB_BLOCK_LENGTH, + AAC_SRB_STS_REQUEST_FLUSHED, + AAC_SRB_STS_INVALID_LUN = 0x20, + AAC_SRB_STS_INVALID_TARGET_ID, + AAC_SRB_STS_BAD_FUNCTION, + AAC_SRB_STS_ERROR_RECOVERY +}; + +/* + * Register set for adapters based on the Falcon bridge and PPC core + */ + +#define AAC_FA_DOORBELL0_CLEAR 0x00 +#define AAC_FA_DOORBELL1_CLEAR 0x02 +#define AAC_FA_DOORBELL0 0x04 +#define AAC_FA_DOORBELL1 0x06 +#define AAC_FA_MASK0_CLEAR 0x08 +#define AAC_FA_MASK1_CLEAR 0x0a +#define AAC_FA_MASK0 0x0c +#define AAC_FA_MASK1 0x0e +#define AAC_FA_MAILBOX 0x10 +#define AAC_FA_FWSTATUS 0x2c /* Mailbox 7 */ +#define AAC_FA_INTSRC 0x900 + +#define AAC_FA_HACK(sc) (void)AAC_GETREG4(sc, AAC_FA_INTSRC) + +/* + * Register definitions for the Adaptec AAC-364 'Jalapeno I/II' adapters, based + * on the SA110 'StrongArm'. + */ + +#define AAC_SA_DOORBELL0_CLEAR 0x98 /* doorbell 0 (adapter->host) */ +#define AAC_SA_DOORBELL0_SET 0x9c +#define AAC_SA_DOORBELL0 0x9c +#define AAC_SA_MASK0_CLEAR 0xa0 +#define AAC_SA_MASK0_SET 0xa4 + +#define AAC_SA_DOORBELL1_CLEAR 0x9a /* doorbell 1 (host->adapter) */ +#define AAC_SA_DOORBELL1_SET 0x9e +#define AAC_SA_DOORBELL1 0x9e +#define AAC_SA_MASK1_CLEAR 0xa2 +#define AAC_SA_MASK1_SET 0xa6 + +#define AAC_SA_MAILBOX 0xa8 /* mailbox (20 bytes) */ +#define AAC_SA_FWSTATUS 0xc4 + +/* + * Register definitions for the Adaptec 'Pablano' adapters, based on the i960Rx, + * and other related adapters. + */ + +#define AAC_RX_IDBR 0x20 /* inbound doorbell register */ +#define AAC_RX_IISR 0x24 /* inbound interrupt status register */ +#define AAC_RX_IIMR 0x28 /* inbound interrupt mask register */ +#define AAC_RX_ODBR 0x2c /* outbound doorbell register */ +#define AAC_RX_OISR 0x30 /* outbound interrupt status register */ +#define AAC_RX_OIMR 0x34 /* outbound interrupt mask register */ + +#define AAC_RX_MAILBOX 0x50 /* mailbox (20 bytes) */ +#define AAC_RX_FWSTATUS 0x6c + +/* + * Common bit definitions for the doorbell registers. + */ + +/* + * Status bits in the doorbell registers. + */ +#define AAC_DB_SYNC_COMMAND (1<<0) /* send/completed synchronous FIB */ +#define AAC_DB_COMMAND_READY (1<<1) /* posted one or more commands */ +#define AAC_DB_RESPONSE_READY (1<<2) /* one or more commands complete */ +#define AAC_DB_COMMAND_NOT_FULL (1<<3) /* command queue not full */ +#define AAC_DB_RESPONSE_NOT_FULL (1<<4) /* response queue not full */ + +/* + * The adapter can request the host print a message by setting the + * DB_PRINTF flag in DOORBELL0. The driver responds by collecting the + * message from the printf buffer, clearing the DB_PRINTF flag in + * DOORBELL0 and setting it in DOORBELL1. + * (ODBR and IDBR respectively for the i960Rx adapters) + */ +#define AAC_DB_PRINTF (1<<5) /* adapter requests host printf */ +#define AAC_PRINTF_DONE (1<<5) /* Host completed printf processing */ + +/* + * Mask containing the interrupt bits we care about. We don't anticipate (or + * want) interrupts not in this mask. + */ +#define AAC_DB_INTERRUPTS (AAC_DB_COMMAND_READY | \ + AAC_DB_RESPONSE_READY | \ + AAC_DB_PRINTF) diff --git a/sys/dev/aac/aacvar.h b/sys/dev/aac/aacvar.h new file mode 100644 index 0000000..1ff54112 --- /dev/null +++ b/sys/dev/aac/aacvar.h @@ -0,0 +1,573 @@ +/*- + * Copyright (c) 2000 Michael Smith + * Copyright (c) 2001 Scott Long + * Copyright (c) 2000 BSDi + * Copyright (c) 2001 Adaptec, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/bio.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/taskqueue.h> +#include <sys/selinfo.h> +#include <geom/geom_disk.h> + + +/* + * Driver Parameter Definitions + */ + +/* + * The firmware interface allows for a 16-bit s/g list length. We limit + * ourselves to a reasonable maximum and ensure alignment. + */ +#define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ + +/* + * We allocate a small set of FIBs for the adapter to use to send us messages. + */ +#define AAC_ADAPTER_FIBS 8 + +/* + * FIBs are allocated in page-size chunks and can grow up to the 512 + * limit imposed by the hardware. + */ +#define AAC_FIB_COUNT (PAGE_SIZE/sizeof(struct aac_fib)) +#define AAC_PREALLOCATE_FIBS 128 +#define AAC_MAX_FIBS 512 + +/* + * The controller reports status events in AIFs. We hang on to a number of + * these in order to pass them out to user-space management tools. + */ +#define AAC_AIFQ_LENGTH 64 + +/* + * Firmware messages are passed in the printf buffer. + */ +#define AAC_PRINTF_BUFSIZE 256 + +/* + * We wait this many seconds for the adapter to come ready if it is still + * booting + */ +#define AAC_BOOT_TIMEOUT (3 * 60) + +/* + * Timeout for immediate commands. + */ +#define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ + +/* + * Timeout for normal commands + */ +#define AAC_CMD_TIMEOUT 30 /* seconds */ + +/* + * Rate at which we periodically check for timed out commands and kick the + * controller. + */ +#define AAC_PERIODIC_INTERVAL 20 /* seconds */ + +/* + * Per-container data structure + */ +struct aac_container +{ + struct aac_mntobj co_mntobj; + device_t co_disk; + int co_found; + TAILQ_ENTRY(aac_container) co_link; +}; + +/* + * Per-SIM data structure + */ +struct aac_sim +{ + device_t sim_dev; + int TargetsPerBus; + int BusNumber; + int InitiatorBusId; + struct aac_softc *aac_sc; + TAILQ_ENTRY(aac_sim) sim_link; +}; + +/* + * Per-disk structure + */ +struct aac_disk +{ + device_t ad_dev; + struct aac_softc *ad_controller; + struct aac_container *ad_container; + struct disk ad_disk; + int ad_flags; +#define AAC_DISK_OPEN (1<<0) + int ad_cylinders; + int ad_heads; + int ad_sectors; + u_int32_t ad_size; + int unit; +}; + +/* + * Per-command control structure. + */ +struct aac_command +{ + TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ + + struct aac_softc *cm_sc; /* controller that owns us */ + + struct aac_fib *cm_fib; /* FIB associated with this + * command */ + u_int32_t cm_fibphys; /* bus address of the FIB */ + struct bio *cm_data; /* pointer to data in kernel + * space */ + u_int32_t cm_datalen; /* data length */ + bus_dmamap_t cm_datamap; /* DMA map for bio data */ + struct aac_sg_table *cm_sgtable; /* pointer to s/g table in + * command */ + int cm_flags; +#define AAC_CMD_MAPPED (1<<0) /* command has had its data + * mapped */ +#define AAC_CMD_DATAIN (1<<1) /* command involves data moving + * from controller to host */ +#define AAC_CMD_DATAOUT (1<<2) /* command involves data moving + * from host to controller */ +#define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ +#define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ +#define AAC_ON_AACQ_FREE (1<<5) +#define AAC_ON_AACQ_READY (1<<6) +#define AAC_ON_AACQ_BUSY (1<<7) +#define AAC_ON_AACQ_COMPLETE (1<<8) +#define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)) + + void (* cm_complete)(struct aac_command *cm); + void *cm_private; + time_t cm_timestamp; /* command creation time */ + int cm_queue; + int cm_index; +}; + +struct aac_fibmap { + TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */ + struct aac_fib *aac_fibs; + bus_dmamap_t aac_fibmap; + struct aac_command *aac_commands; +}; + +/* + * We gather a number of adapter-visible items into a single structure. + * + * The ordering of this strucure may be important; we copy the Linux driver: + * + * Adapter FIBs + * Init struct + * Queue headers (Comm Area) + * Printf buffer + * + * In addition, we add: + * Sync Fib + */ +struct aac_common { + /* fibs for the controller to send us messages */ + struct aac_fib ac_fibs[AAC_ADAPTER_FIBS]; + + /* the init structure */ + struct aac_adapter_init ac_init; + + /* arena within which the queue structures are kept */ + u_int8_t ac_qbuf[sizeof(struct aac_queue_table) + + AAC_QUEUE_ALIGN]; + + /* buffer for text messages from the controller */ + char ac_printf[AAC_PRINTF_BUFSIZE]; + + /* fib for synchronous commands */ + struct aac_fib ac_sync_fib; +}; + +/* + * Interface operations + */ +struct aac_interface +{ + int (*aif_get_fwstatus)(struct aac_softc *sc); + void (*aif_qnotify)(struct aac_softc *sc, int qbit); + int (*aif_get_istatus)(struct aac_softc *sc); + void (*aif_clr_istatus)(struct aac_softc *sc, int mask); + void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command, + u_int32_t arg0, u_int32_t arg1, + u_int32_t arg2, u_int32_t arg3); + int (*aif_get_mailbox)(struct aac_softc *sc, int mb); + void (*aif_set_interrupts)(struct aac_softc *sc, int enable); +}; +extern struct aac_interface aac_rx_interface; +extern struct aac_interface aac_sa_interface; +extern struct aac_interface aac_fa_interface; + +#define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc))) +#define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit))) +#define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc))) +#define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \ + (mask))) +#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \ + ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \ + (arg3))) +#define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \ + (mb))) +#define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ + 0)) +#define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ + 1)) + +#define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \ + sc->aac_bhandle, reg, val) +#define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \ + sc->aac_bhandle, reg) +#define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \ + sc->aac_bhandle, reg, val) +#define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \ + sc->aac_bhandle, reg) +#define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \ + sc->aac_bhandle, reg, val) +#define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \ + sc->aac_bhandle, reg) + +/* Define the OS version specific locks */ +typedef struct mtx aac_lock_t; +#define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF) +#define AAC_LOCK_ACQUIRE(l) mtx_lock(l) +#define AAC_LOCK_RELEASE(l) mtx_unlock(l) + + +/* + * Per-controller structure. + */ +struct aac_softc +{ + /* bus connections */ + device_t aac_dev; + struct resource *aac_regs_resource; /* register interface + * window */ + int aac_regs_rid; /* resource ID */ + bus_space_handle_t aac_bhandle; /* bus space handle */ + bus_space_tag_t aac_btag; /* bus space tag */ + bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ + bus_dma_tag_t aac_buffer_dmat; /* data buffer/command + * DMA tag */ + struct resource *aac_irq; /* interrupt */ + int aac_irq_rid; + void *aac_intr; /* interrupt handle */ + eventhandler_tag eh; + + /* controller features, limits and status */ + int aac_state; +#define AAC_STATE_SUSPEND (1<<0) +#define AAC_STATE_OPEN (1<<1) +#define AAC_STATE_INTERRUPTS_ON (1<<2) +#define AAC_STATE_AIF_SLEEPER (1<<3) + struct FsaRevision aac_revision; + + /* controller hardware interface */ + int aac_hwif; +#define AAC_HWIF_I960RX 0 +#define AAC_HWIF_STRONGARM 1 +#define AAC_HWIF_FALCON 2 +#define AAC_HWIF_UNKNOWN -1 + bus_dma_tag_t aac_common_dmat; /* common structure + * DMA tag */ + bus_dmamap_t aac_common_dmamap; /* common structure + * DMA map */ + struct aac_common *aac_common; + u_int32_t aac_common_busaddr; + struct aac_interface aac_if; + + /* command/fib resources */ + bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ + TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh; + uint total_fibs; + struct aac_command *aac_commands; + + /* command management */ + TAILQ_HEAD(,aac_command) aac_free; /* command structures + * available for reuse */ + TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for + * controller resources */ + TAILQ_HEAD(,aac_command) aac_busy; + struct bio_queue_head aac_bioq; + struct aac_queue_table *aac_queues; + struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; + + struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ + + /* connected containters */ + TAILQ_HEAD(,aac_container) aac_container_tqh; + aac_lock_t aac_container_lock; + + /* Protect the sync fib */ +#define AAC_SYNC_LOCK_FORCE (1 << 0) + aac_lock_t aac_sync_lock; + + aac_lock_t aac_io_lock; + + /* delayed activity infrastructure */ + struct task aac_task_complete; /* deferred-completion + * task */ + struct intr_config_hook aac_ich; + + /* management interface */ + dev_t aac_dev_t; + aac_lock_t aac_aifq_lock; + struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH]; + int aac_aifq_head; + int aac_aifq_tail; + struct selinfo rcv_select; + struct proc *aifthread; + int aifflags; +#define AAC_AIFFLAGS_RUNNING (1 << 0) +#define AAC_AIFFLAGS_AIF (1 << 1) +#define AAC_AIFFLAGS_EXIT (1 << 2) +#define AAC_AIFFLAGS_EXITED (1 << 3) +#define AAC_AIFFLAGS_PRINTF (1 << 4) +#define AAC_AIFFLAGS_ALLOCFIBS (1 << 5) +#define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \ + AAC_AIFFLAGS_ALLOCFIBS) + u_int32_t flags; +#define AAC_FLAGS_PERC2QC (1 << 0) +#define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */ +#define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */ +#define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */ +#define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */ +#define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem + * 2GB-4GB range */ +#define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */ +#define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */ + + u_int32_t supported_options; + int aac_max_fibs; + u_int32_t scsi_method_id; + TAILQ_HEAD(,aac_sim) aac_sim_tqh; +}; + + +/* + * Public functions + */ +extern void aac_free(struct aac_softc *sc); +extern int aac_attach(struct aac_softc *sc); +extern int aac_detach(device_t dev); +extern int aac_shutdown(device_t dev); +extern int aac_suspend(device_t dev); +extern int aac_resume(device_t dev); +extern void aac_intr(void *arg); +extern void aac_submit_bio(struct bio *bp); +extern void aac_biodone(struct bio *bp); +extern void aac_startio(struct aac_softc *sc); +extern int aac_alloc_command(struct aac_softc *sc, + struct aac_command **cmp); +extern void aac_release_command(struct aac_command *cm); +extern int aac_alloc_sync_fib(struct aac_softc *sc, + struct aac_fib **fib, int flags); +extern void aac_release_sync_fib(struct aac_softc *sc); +extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, + u_int32_t xferstate, struct aac_fib *fib, + u_int16_t datasize); + +/* + * Debugging levels: + * 0 - quiet, only emit warnings + * 1 - noisy, emit major function points and things done + * 2 - extremely noisy, emit trace items in loops, etc. + */ +#ifdef AAC_DEBUG +# define debug(level, fmt, args...) \ + do { \ + if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ + } while (0) +# define debug_called(level) \ + do { \ + if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \ + } while (0) + +extern void aac_print_queues(struct aac_softc *sc); +extern void aac_panic(struct aac_softc *sc, char *reason); +extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, + const char *caller); +extern void aac_print_aif(struct aac_softc *sc, + struct aac_aif_command *aif); + +#define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) + +#else +# define debug(level, fmt, args...) +# define debug_called(level) + +# define aac_print_queues(sc) +# define aac_panic(sc, reason) + +# define AAC_PRINT_FIB(sc, fib) +# define aac_print_aif(sc, aac_aif_command) +#endif + +struct aac_code_lookup { + char *string; + u_int32_t code; +}; + +/* + * Queue primitives for driver queues. + */ +#define AACQ_ADD(sc, qname) \ + do { \ + struct aac_qstat *qs; \ + \ + qs = &(sc)->aac_qstat[qname]; \ + \ + qs->q_length++; \ + if (qs->q_length > qs->q_max) \ + qs->q_max = qs->q_length; \ + } while (0) + +#define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- +#define AACQ_INIT(sc, qname) \ + do { \ + sc->aac_qstat[qname].q_length = 0; \ + sc->aac_qstat[qname].q_max = 0; \ + } while (0) + + +#define AACQ_COMMAND_QUEUE(name, index) \ +static __inline void \ +aac_initq_ ## name (struct aac_softc *sc) \ +{ \ + TAILQ_INIT(&sc->aac_ ## name); \ + AACQ_INIT(sc, index); \ +} \ +static __inline void \ +aac_enqueue_ ## name (struct aac_command *cm) \ +{ \ + if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ + printf("command %p is on another queue, flags = %#x\n", \ + cm, cm->cm_flags); \ + panic("command is on another queue"); \ + } \ + TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ + cm->cm_flags |= AAC_ON_ ## index; \ + AACQ_ADD(cm->cm_sc, index); \ +} \ +static __inline void \ +aac_requeue_ ## name (struct aac_command *cm) \ +{ \ + if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ + printf("command %p is on another queue, flags = %#x\n", \ + cm, cm->cm_flags); \ + panic("command is on another queue"); \ + } \ + TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ + cm->cm_flags |= AAC_ON_ ## index; \ + AACQ_ADD(cm->cm_sc, index); \ +} \ +static __inline struct aac_command * \ +aac_dequeue_ ## name (struct aac_softc *sc) \ +{ \ + struct aac_command *cm; \ + \ + if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ + if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ + printf("command %p not in queue, flags = %#x, " \ + "bit = %#x\n", cm, cm->cm_flags, \ + AAC_ON_ ## index); \ + panic("command not in queue"); \ + } \ + TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ + cm->cm_flags &= ~AAC_ON_ ## index; \ + AACQ_REMOVE(sc, index); \ + } \ + return(cm); \ +} \ +static __inline void \ +aac_remove_ ## name (struct aac_command *cm) \ +{ \ + if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ + printf("command %p not in queue, flags = %#x, " \ + "bit = %#x\n", cm, cm->cm_flags, \ + AAC_ON_ ## index); \ + panic("command not in queue"); \ + } \ + TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ + cm->cm_flags &= ~AAC_ON_ ## index; \ + AACQ_REMOVE(cm->cm_sc, index); \ +} \ +struct hack + +AACQ_COMMAND_QUEUE(free, AACQ_FREE); +AACQ_COMMAND_QUEUE(ready, AACQ_READY); +AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); + +/* + * outstanding bio queue + */ +static __inline void +aac_initq_bio(struct aac_softc *sc) +{ + bioq_init(&sc->aac_bioq); + AACQ_INIT(sc, AACQ_BIO); +} + +static __inline void +aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) +{ + bioq_insert_tail(&sc->aac_bioq, bp); + AACQ_ADD(sc, AACQ_BIO); +} + +static __inline struct bio * +aac_dequeue_bio(struct aac_softc *sc) +{ + struct bio *bp; + + if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { + bioq_remove(&sc->aac_bioq, bp); + AACQ_REMOVE(sc, AACQ_BIO); + } + return(bp); +} + +static __inline void +aac_print_printf(struct aac_softc *sc) +{ + /* + * XXX We have the ability to read the length of the printf string + * from out of the mailboxes. + */ + device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, + sc->aac_common->ac_printf); + AAC_QNOTIFY(sc, AAC_DB_PRINTF); +} |