2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/dev/aac/aac.c,v 1.9.2.14 2003/04/08 13:22:08 scottl Exp $
30 * $DragonFly: src/sys/dev/raid/aac/aac.c,v 1.15 2004/08/15 14:15:00 joerg Exp $
34 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
39 /* #include <stddef.h> */
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/sysctl.h>
47 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
48 #include <sys/selinfo.h>
50 #include <sys/select.h>
53 #include "aac_compat.h"
57 #include <sys/devicestat.h>
59 #include <sys/signalvar.h>
61 #include <sys/eventhandler.h>
63 #include <machine/bus_memio.h>
64 #include <machine/bus.h>
65 #include <machine/resource.h>
68 #include "aac_ioctl.h"
70 #include "aac_tables.h"
73 static void aac_startup(void *arg);
74 static void aac_add_container(struct aac_softc *sc,
75 struct aac_mntinforesp *mir, int f);
76 static void aac_get_bus_info(struct aac_softc *sc);
78 /* Command Processing */
79 static void aac_timeout(struct aac_softc *sc);
80 static int aac_start(struct aac_command *cm);
81 static void aac_complete(void *context, int pending);
82 static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
83 static void aac_bio_complete(struct aac_command *cm);
84 static int aac_wait_command(struct aac_command *cm, int timeout);
85 static void aac_host_command(struct aac_softc *sc);
86 static void aac_host_response(struct aac_softc *sc);
88 /* Command Buffer Management */
89 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
91 static int aac_alloc_commands(struct aac_softc *sc);
92 static void aac_free_commands(struct aac_softc *sc);
93 static void aac_map_command(struct aac_command *cm);
94 static void aac_unmap_command(struct aac_command *cm);
96 /* Hardware Interface */
97 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
99 static int aac_check_firmware(struct aac_softc *sc);
100 static int aac_init(struct aac_softc *sc);
101 static int aac_sync_command(struct aac_softc *sc, u_int32_t command,
102 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
103 u_int32_t arg3, u_int32_t *sp);
104 static int aac_enqueue_fib(struct aac_softc *sc, int queue,
105 struct aac_command *cm);
106 static int aac_dequeue_fib(struct aac_softc *sc, int queue,
107 u_int32_t *fib_size, struct aac_fib **fib_addr);
108 static int aac_enqueue_response(struct aac_softc *sc, int queue,
109 struct aac_fib *fib);
111 /* Falcon/PPC interface */
112 static int aac_fa_get_fwstatus(struct aac_softc *sc);
113 static void aac_fa_qnotify(struct aac_softc *sc, int qbit);
114 static int aac_fa_get_istatus(struct aac_softc *sc);
115 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask);
116 static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
117 u_int32_t arg0, u_int32_t arg1,
118 u_int32_t arg2, u_int32_t arg3);
119 static int aac_fa_get_mailbox(struct aac_softc *sc, int mb);
120 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable);
122 struct aac_interface aac_fa_interface = {
126 aac_fa_clear_istatus,
129 aac_fa_set_interrupts
132 /* StrongARM interface */
133 static int aac_sa_get_fwstatus(struct aac_softc *sc);
134 static void aac_sa_qnotify(struct aac_softc *sc, int qbit);
135 static int aac_sa_get_istatus(struct aac_softc *sc);
136 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask);
137 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
138 u_int32_t arg0, u_int32_t arg1,
139 u_int32_t arg2, u_int32_t arg3);
140 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb);
141 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable);
143 struct aac_interface aac_sa_interface = {
147 aac_sa_clear_istatus,
150 aac_sa_set_interrupts
153 /* i960Rx interface */
154 static int aac_rx_get_fwstatus(struct aac_softc *sc);
155 static void aac_rx_qnotify(struct aac_softc *sc, int qbit);
156 static int aac_rx_get_istatus(struct aac_softc *sc);
157 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask);
158 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
159 u_int32_t arg0, u_int32_t arg1,
160 u_int32_t arg2, u_int32_t arg3);
161 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb);
162 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable);
164 struct aac_interface aac_rx_interface = {
168 aac_rx_clear_istatus,
171 aac_rx_set_interrupts
174 /* Debugging and Diagnostics */
175 static void aac_describe_controller(struct aac_softc *sc);
176 static char *aac_describe_code(struct aac_code_lookup *table,
179 /* Management Interface */
180 static d_open_t aac_open;
181 static d_close_t aac_close;
182 static d_ioctl_t aac_ioctl;
183 static d_poll_t aac_poll;
184 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
185 static void aac_handle_aif(struct aac_softc *sc,
186 struct aac_fib *fib);
187 static int aac_rev_check(struct aac_softc *sc, caddr_t udata);
188 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
189 static int aac_return_aif(struct aac_softc *sc, caddr_t uptr);
190 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr);
192 #define AAC_CDEV_MAJOR 150
194 static struct cdevsw aac_cdevsw = {
196 AAC_CDEV_MAJOR, /* major */
202 aac_close, /* close */
205 aac_ioctl, /* ioctl */
208 nostrategy, /* strategy */
213 DECLARE_DUMMY_MODULE(aac);
215 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
218 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
225 * Initialise the controller and softc
228 aac_attach(struct aac_softc *sc)
235 * Initialise per-controller queues.
240 aac_initq_complete(sc);
243 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
245 * Initialise command-completion task.
247 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
250 /* disable interrupts before we enable anything */
251 AAC_MASK_INTERRUPTS(sc);
253 /* mark controller as suspended until we get ourselves organised */
254 sc->aac_state |= AAC_STATE_SUSPEND;
257 * Check that the firmware on the card is supported.
259 if ((error = aac_check_firmware(sc)) != 0)
262 /* Init the sync fib lock */
263 AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock");
266 * Initialise the adapter.
268 if ((error = aac_init(sc)) != 0)
272 * Print a little information about the controller.
274 aac_describe_controller(sc);
277 * Register to probe our containers later.
279 TAILQ_INIT(&sc->aac_container_tqh);
280 AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
283 * Lock for the AIF queue
285 AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
287 sc->aac_ich.ich_func = aac_startup;
288 sc->aac_ich.ich_arg = sc;
289 if (config_intrhook_establish(&sc->aac_ich) != 0) {
290 device_printf(sc->aac_dev,
291 "can't establish configuration hook\n");
296 * Make the control device.
298 unit = device_get_unit(sc->aac_dev);
299 cdevsw_add(&aac_cdevsw, -1, unit);
300 sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
302 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
303 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
304 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
306 sc->aac_dev_t->si_drv1 = sc;
307 reference_dev(sc->aac_dev_t);
309 /* Create the AIF thread */
310 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
311 if (kthread_create((void(*)(void *))aac_host_command, sc,
312 &sc->aifthread, 0, "aac%daif", unit))
314 if (kthread_create((void(*)(void *))aac_host_command, sc,
315 &sc->aifthread, "aac%daif", unit))
317 panic("Could not create AIF thread\n");
319 /* Register the shutdown method to only be called post-dump */
320 if ((EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown, sc->aac_dev,
321 SHUTDOWN_PRI_DEFAULT)) == NULL)
322 device_printf(sc->aac_dev, "shutdown event registration failed\n");
324 /* Register with CAM for the non-DASD devices */
325 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0)
326 aac_get_bus_info(sc);
332 * Probe for containers, create disks.
335 aac_startup(void *arg)
337 struct aac_softc *sc;
339 struct aac_mntinfo *mi;
340 struct aac_mntinforesp *mir = NULL;
345 sc = (struct aac_softc *)arg;
347 /* disconnect ourselves from the intrhook chain */
348 config_intrhook_disestablish(&sc->aac_ich);
350 aac_alloc_sync_fib(sc, &fib, 0);
351 mi = (struct aac_mntinfo *)&fib->data[0];
353 /* loop over possible containers */
355 /* request information on this container */
356 bzero(mi, sizeof(struct aac_mntinfo));
357 mi->Command = VM_NameServe;
358 mi->MntType = FT_FILESYS;
360 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
361 sizeof(struct aac_mntinfo))) {
362 debug(2, "error probing container %d", i);
366 mir = (struct aac_mntinforesp *)&fib->data[0];
367 aac_add_container(sc, mir, 0);
369 } while ((i < mir->MntRespCount) && (i < AAC_MAX_CONTAINERS));
371 aac_release_sync_fib(sc);
373 /* poke the bus to actually attach the child devices */
374 if (bus_generic_attach(sc->aac_dev))
375 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
377 /* mark the controller up */
378 sc->aac_state &= ~AAC_STATE_SUSPEND;
380 /* enable interrupts now */
381 AAC_UNMASK_INTERRUPTS(sc);
383 /* enable the timeout watchdog */
384 timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
388 * Create a device to respresent a new container
391 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
393 struct aac_container *co;
397 * Check container volume type for validity. Note that many of
398 * the possible types may never show up.
400 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
401 MALLOC(co, struct aac_container *, sizeof *co, M_AACBUF,
403 debug(1, "id %x name '%.16s' size %u type %d",
404 mir->MntTable[0].ObjectId,
405 mir->MntTable[0].FileSystemName,
406 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
408 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
409 device_printf(sc->aac_dev, "device_add_child failed\n");
411 device_set_ivars(child, co);
412 device_set_desc(child, aac_describe_code(aac_container_types,
413 mir->MntTable[0].VolType));
416 bcopy(&mir->MntTable[0], &co->co_mntobj,
417 sizeof(struct aac_mntobj));
418 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
419 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
420 AAC_LOCK_RELEASE(&sc->aac_container_lock);
425 * Free all of the resources associated with (sc)
427 * Should not be called if the controller is active.
430 aac_free(struct aac_softc *sc)
434 /* remove the control device */
435 if (sc->aac_dev_t != NULL)
436 destroy_dev(sc->aac_dev_t);
438 /* throw away any FIB buffers, discard the FIB DMA tag */
439 if (sc->aac_fibs != NULL)
440 aac_free_commands(sc);
441 if (sc->aac_fib_dmat)
442 bus_dma_tag_destroy(sc->aac_fib_dmat);
444 /* destroy the common area */
445 if (sc->aac_common) {
446 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
447 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
448 sc->aac_common_dmamap);
450 if (sc->aac_common_dmat)
451 bus_dma_tag_destroy(sc->aac_common_dmat);
453 /* disconnect the interrupt handler */
455 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
456 if (sc->aac_irq != NULL)
457 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
460 /* destroy data-transfer DMA tag */
461 if (sc->aac_buffer_dmat)
462 bus_dma_tag_destroy(sc->aac_buffer_dmat);
464 /* destroy the parent DMA tag */
465 if (sc->aac_parent_dmat)
466 bus_dma_tag_destroy(sc->aac_parent_dmat);
468 /* release the register window mapping */
469 if (sc->aac_regs_resource != NULL) {
470 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
471 sc->aac_regs_rid, sc->aac_regs_resource);
473 cdevsw_remove(&aac_cdevsw, -1, device_get_unit(sc->aac_dev));
477 * Disconnect from the controller completely, in preparation for unload.
480 aac_detach(device_t dev)
482 struct aac_softc *sc;
489 sc = device_get_softc(dev);
491 if (sc->aac_state & AAC_STATE_OPEN)
495 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
496 sc->aifflags |= AAC_AIFFLAGS_EXIT;
497 wakeup(sc->aifthread);
498 tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz);
501 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
502 panic("Cannot shutdown AIF thread\n");
504 if ((error = aac_shutdown(dev)))
516 * Bring the controller down to a dormant state and detach all child devices.
518 * This function is called before detach or system shutdown.
520 * Note that we can assume that the bioq on the controller is empty, as we won't
521 * allow shutdown if any device is open.
524 aac_shutdown(device_t dev)
526 struct aac_softc *sc;
528 struct aac_close_command *cc;
533 sc = device_get_softc(dev);
537 sc->aac_state |= AAC_STATE_SUSPEND;
540 * Send a Container shutdown followed by a HostShutdown FIB to the
541 * controller to convince it that we don't want to talk to it anymore.
542 * We've been closed and all I/O completed already
544 device_printf(sc->aac_dev, "shutting down controller...");
546 aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
547 cc = (struct aac_close_command *)&fib->data[0];
549 bzero(cc, sizeof(struct aac_close_command));
550 cc->Command = VM_CloseAll;
551 cc->ContainerId = 0xffffffff;
552 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
553 sizeof(struct aac_close_command)))
558 * XXX Issuing this command to the controller makes it shut down
559 * but also keeps it from coming back up without a reset of the
560 * PCI bus. This is not desirable if you are just unloading the
561 * driver module with the intent to reload it later.
563 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
571 AAC_MASK_INTERRUPTS(sc);
578 * Bring the controller to a quiescent state, ready for system suspend.
581 aac_suspend(device_t dev)
583 struct aac_softc *sc;
588 sc = device_get_softc(dev);
592 sc->aac_state |= AAC_STATE_SUSPEND;
594 AAC_MASK_INTERRUPTS(sc);
600 * Bring the controller back to a state ready for operation.
603 aac_resume(device_t dev)
605 struct aac_softc *sc;
609 sc = device_get_softc(dev);
611 sc->aac_state &= ~AAC_STATE_SUSPEND;
612 AAC_UNMASK_INTERRUPTS(sc);
622 struct aac_softc *sc;
624 u_int32_t *resp_queue;
628 sc = (struct aac_softc *)arg;
631 * Optimize the common case of adapter response interrupts.
632 * We must read from the card prior to processing the responses
633 * to ensure the clear is flushed prior to accessing the queues.
634 * Reading the queues from local memory might save us a PCI read.
636 resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE];
637 if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX])
638 reason = AAC_DB_RESPONSE_READY;
640 reason = AAC_GET_ISTATUS(sc);
641 AAC_CLEAR_ISTATUS(sc, reason);
642 (void)AAC_GET_ISTATUS(sc);
644 /* It's not ok to return here because of races with the previous step */
645 if (reason & AAC_DB_RESPONSE_READY)
646 aac_host_response(sc);
648 /* controller wants to talk to the log */
649 if (reason & AAC_DB_PRINTF)
650 aac_print_printf(sc);
652 /* controller has a message for us? */
653 if (reason & AAC_DB_COMMAND_READY) {
654 /* XXX What happens if the thread is already awake? */
655 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
656 sc->aifflags |= AAC_AIFFLAGS_PENDING;
657 wakeup(sc->aifthread);
667 * Start as much queued I/O as possible on the controller
670 aac_startio(struct aac_softc *sc)
672 struct aac_command *cm;
678 * Try to get a command that's been put off for lack of
681 cm = aac_dequeue_ready(sc);
684 * Try to build a command off the bio queue (ignore error
688 aac_bio_command(sc, &cm);
694 /* try to give the command to the controller */
695 if (aac_start(cm) == EBUSY) {
696 /* put it on the ready queue for later */
697 aac_requeue_ready(cm);
704 * Deliver a command to the controller; allocate controller resources at the
705 * last moment when possible.
708 aac_start(struct aac_command *cm)
710 struct aac_softc *sc;
717 /* get the command mapped */
720 /* fix up the address values in the FIB */
721 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
722 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
724 /* save a pointer to the command for speedy reverse-lookup */
725 cm->cm_fib->Header.SenderData = (u_int32_t)cm; /* XXX 64-bit physical
727 /* put the FIB on the outbound queue */
728 error = aac_enqueue_fib(sc, cm->cm_queue, cm);
733 * Handle notification of one or more FIBs coming from the controller.
736 aac_host_command(struct aac_softc *sc)
744 sc->aifflags |= AAC_AIFFLAGS_RUNNING;
746 while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) {
747 if (!(sc->aifflags & AAC_AIFFLAGS_PENDING))
748 tsleep(sc->aifthread, 0, "aifthd", 15 * hz);
750 sc->aifflags &= ~AAC_AIFFLAGS_PENDING;
752 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
754 break; /* nothing to do */
756 AAC_PRINT_FIB(sc, fib);
758 switch (fib->Header.Command) {
760 aac_handle_aif(sc, fib);
763 device_printf(sc->aac_dev, "unknown command "
764 "from controller\n");
768 /* Return the AIF to the controller. */
769 if ((fib->Header.XferState == 0) ||
770 (fib->Header.StructType != AAC_FIBTYPE_TFIB))
773 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
774 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
775 *(AAC_FSAStatus*)fib->data = ST_OK;
777 /* XXX Compute the Size field? */
778 size = fib->Header.Size;
779 if (size > sizeof(struct aac_fib)) {
780 size = sizeof(struct aac_fib);
781 fib->Header.Size = size;
784 * Since we did not generate this command, it
785 * cannot go through the normal
786 * enqueue->startio chain.
788 aac_enqueue_response(sc,
789 AAC_ADAP_NORM_RESP_QUEUE,
794 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
797 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
804 * Handle notification of one or more FIBs completed by the controller
807 aac_host_response(struct aac_softc *sc)
809 struct aac_command *cm;
816 /* look for completed FIBs on our queue */
817 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
819 break; /* nothing to do */
821 /* get the command, unmap and queue for later processing */
822 cm = (struct aac_command *)fib->Header.SenderData;
824 AAC_PRINT_FIB(sc, fib);
827 aac_unmap_command(cm); /* XXX defer? */
828 aac_enqueue_complete(cm);
832 /* handle completion processing */
833 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
834 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
841 * Process completed commands.
844 aac_complete(void *context, int pending)
846 struct aac_softc *sc;
847 struct aac_command *cm;
851 sc = (struct aac_softc *)context;
853 /* pull completed commands off the queue */
855 cm = aac_dequeue_complete(sc);
858 cm->cm_flags |= AAC_CMD_COMPLETED;
860 /* is there a completion handler? */
861 if (cm->cm_complete != NULL) {
864 /* assume that someone is sleeping on this command */
869 /* see if we can start some more I/O */
874 * Handle a bio submitted from a disk device.
877 aac_submit_bio(struct bio *bp)
880 struct aac_softc *sc;
884 ad = (struct aac_disk *)bp->bio_dev->si_drv1;
885 sc = ad->ad_controller;
887 /* queue the BIO and try to get some work done */
888 aac_enqueue_bio(sc, bp);
893 * Get a bio and build a command to go with it.
896 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
898 struct aac_command *cm;
900 struct aac_blockread *br;
901 struct aac_blockwrite *bw;
907 /* get the resources we will need */
909 if ((bp = aac_dequeue_bio(sc)) == NULL)
911 if (aac_alloc_command(sc, &cm)) /* get a command */
914 /* fill out the command */
915 cm->cm_data = (void *)bp->bio_data;
916 cm->cm_datalen = bp->bio_bcount;
917 cm->cm_complete = aac_bio_complete;
919 cm->cm_timestamp = time_second;
920 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
924 fib->Header.XferState =
925 AAC_FIBSTATE_HOSTOWNED |
926 AAC_FIBSTATE_INITIALISED |
928 AAC_FIBSTATE_FROMHOST |
929 AAC_FIBSTATE_REXPECTED |
932 AAC_FIBSTATE_FAST_RESPONSE;
933 fib->Header.Command = ContainerCommand;
934 fib->Header.Size = sizeof(struct aac_fib_header);
936 /* build the read/write request */
937 ad = (struct aac_disk *)bp->bio_dev->si_drv1;
938 if (BIO_IS_READ(bp)) {
939 br = (struct aac_blockread *)&fib->data[0];
940 br->Command = VM_CtBlockRead;
941 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
942 br->BlockNumber = bp->bio_pblkno;
943 br->ByteCount = bp->bio_bcount;
944 fib->Header.Size += sizeof(struct aac_blockread);
945 cm->cm_sgtable = &br->SgMap;
946 cm->cm_flags |= AAC_CMD_DATAIN;
948 bw = (struct aac_blockwrite *)&fib->data[0];
949 bw->Command = VM_CtBlockWrite;
950 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
951 bw->BlockNumber = bp->bio_pblkno;
952 bw->ByteCount = bp->bio_bcount;
953 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
954 fib->Header.Size += sizeof(struct aac_blockwrite);
955 cm->cm_flags |= AAC_CMD_DATAOUT;
956 cm->cm_sgtable = &bw->SgMap;
964 aac_enqueue_bio(sc, bp);
966 aac_release_command(cm);
971 * Handle a bio-instigated command that has been completed.
974 aac_bio_complete(struct aac_command *cm)
976 struct aac_blockread_response *brr;
977 struct aac_blockwrite_response *bwr;
979 AAC_FSAStatus status;
981 /* fetch relevant status and then release the command */
982 bp = (struct bio *)cm->cm_private;
983 if (BIO_IS_READ(bp)) {
984 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
985 status = brr->Status;
987 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
988 status = bwr->Status;
990 aac_release_command(cm);
992 /* fix up the bio based on status */
993 if (status == ST_OK) {
997 bp->bio_flags |= BIO_ERROR;
998 /* pass an error string out to the disk layer */
999 bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1006 * Dump a block of data to the controller. If the queue is full, tell the
1007 * caller to hold off and wait for the queue to drain.
1010 aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba, void *data, int dumppages)
1012 struct aac_softc *sc;
1013 struct aac_command *cm;
1014 struct aac_fib *fib;
1015 struct aac_blockwrite *bw;
1017 sc = ad->ad_controller;
1020 if (aac_alloc_command(sc, &cm))
1023 /* fill out the command */
1025 cm->cm_datalen = dumppages * PAGE_SIZE;
1026 cm->cm_complete = NULL;
1027 cm->cm_private = NULL;
1028 cm->cm_timestamp = time_second;
1029 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1033 fib->Header.XferState =
1034 AAC_FIBSTATE_HOSTOWNED |
1035 AAC_FIBSTATE_INITIALISED |
1036 AAC_FIBSTATE_FROMHOST |
1037 AAC_FIBSTATE_REXPECTED |
1039 fib->Header.Command = ContainerCommand;
1040 fib->Header.Size = sizeof(struct aac_fib_header);
1042 bw = (struct aac_blockwrite *)&fib->data[0];
1043 bw->Command = VM_CtBlockWrite;
1044 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1045 bw->BlockNumber = lba;
1046 bw->ByteCount = dumppages * PAGE_SIZE;
1047 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
1048 fib->Header.Size += sizeof(struct aac_blockwrite);
1049 cm->cm_flags |= AAC_CMD_DATAOUT;
1050 cm->cm_sgtable = &bw->SgMap;
1052 return (aac_start(cm));
1056 * Wait for the card's queue to drain when dumping. Also check for monitor
1060 aac_dump_complete(struct aac_softc *sc)
1062 struct aac_fib *fib;
1063 struct aac_command *cm;
1065 u_int32_t pi, ci, fib_size;
1068 reason = AAC_GET_ISTATUS(sc);
1069 if (reason & AAC_DB_RESPONSE_READY) {
1070 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
1072 if (aac_dequeue_fib(sc,
1073 AAC_HOST_NORM_RESP_QUEUE,
1076 cm = (struct aac_command *)
1077 fib->Header.SenderData;
1079 AAC_PRINT_FIB(sc, fib);
1081 aac_remove_busy(cm);
1082 aac_unmap_command(cm);
1083 aac_enqueue_complete(cm);
1084 aac_release_command(cm);
1088 if (reason & AAC_DB_PRINTF) {
1089 AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
1090 aac_print_printf(sc);
1092 pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1093 AAC_PRODUCER_INDEX];
1094 ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1095 AAC_CONSUMER_INDEX];
1102 * Submit a command to the controller, return when it completes.
1103 * XXX This is very dangerous! If the card has gone out to lunch, we could
1104 * be stuck here forever. At the same time, signals are not caught
1105 * because there is a risk that a signal could wakeup the tsleep before
1106 * the card has a chance to complete the command. The passed in timeout
1107 * is ignored for the same reason. Since there is no way to cancel a
1108 * command in progress, we should probably create a 'dead' queue where
1109 * commands go that have been interrupted/timed-out/etc, that keeps them
1110 * out of the free pool. That way, if the card is just slow, it won't
1111 * spam the memory of a command that has been recycled.
1114 aac_wait_command(struct aac_command *cm, int timeout)
1120 /* Put the command on the ready queue and get things going */
1121 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1122 aac_enqueue_ready(cm);
1123 aac_startio(cm->cm_sc);
1125 while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
1126 error = tsleep(cm, 0, "aacwait", 0);
1133 *Command Buffer Management
1137 * Allocate a command.
1140 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1142 struct aac_command *cm;
1146 if ((cm = aac_dequeue_free(sc)) == NULL)
1154 * Release a command back to the freelist.
1157 aac_release_command(struct aac_command *cm)
1161 /* (re)initialise the command/FIB */
1162 cm->cm_sgtable = NULL;
1164 cm->cm_complete = NULL;
1165 cm->cm_private = NULL;
1166 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1167 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1168 cm->cm_fib->Header.Flags = 0;
1169 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1172 * These are duplicated in aac_start to cover the case where an
1173 * intermediate stage may have destroyed them. They're left
1174 * initialised here for debugging purposes only.
1176 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
1177 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1178 cm->cm_fib->Header.SenderData = 0;
1180 aac_enqueue_free(cm);
1184 * Map helper for command/FIB allocation.
1187 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1189 struct aac_softc *sc;
1191 sc = (struct aac_softc *)arg;
1195 sc->aac_fibphys = segs[0].ds_addr;
1199 * Allocate and initialise commands/FIBs for this adapter.
1202 aac_alloc_commands(struct aac_softc *sc)
1204 struct aac_command *cm;
1209 /* allocate the FIBs in DMAable memory and load them */
1210 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs,
1211 BUS_DMA_NOWAIT, &sc->aac_fibmap)) {
1215 bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs,
1216 AAC_FIB_COUNT * sizeof(struct aac_fib),
1217 aac_map_command_helper, sc, 0);
1219 /* initialise constant fields in the command structure */
1220 bzero(sc->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib));
1221 for (i = 0; i < AAC_FIB_COUNT; i++) {
1222 cm = &sc->aac_command[i];
1224 cm->cm_fib = sc->aac_fibs + i;
1225 cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib));
1227 if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap))
1228 aac_release_command(cm);
1234 * Free FIBs owned by this adapter.
1237 aac_free_commands(struct aac_softc *sc)
1243 for (i = 0; i < AAC_FIB_COUNT; i++)
1244 bus_dmamap_destroy(sc->aac_buffer_dmat,
1245 sc->aac_command[i].cm_datamap);
1247 bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap);
1248 bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap);
1252 * Command-mapping helper function - populate this command's s/g table.
1255 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1257 struct aac_command *cm;
1258 struct aac_fib *fib;
1259 struct aac_sg_table *sg;
1264 cm = (struct aac_command *)arg;
1267 /* find the s/g table */
1268 sg = cm->cm_sgtable;
1270 /* copy into the FIB */
1273 for (i = 0; i < nseg; i++) {
1274 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1275 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1277 /* update the FIB size for the s/g count */
1278 fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1284 * Map a command into controller-visible space.
1287 aac_map_command(struct aac_command *cm)
1289 struct aac_softc *sc;
1295 /* don't map more than once */
1296 if (cm->cm_flags & AAC_CMD_MAPPED)
1299 if (cm->cm_datalen != 0) {
1300 bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
1301 cm->cm_data, cm->cm_datalen,
1302 aac_map_command_sg, cm, 0);
1304 if (cm->cm_flags & AAC_CMD_DATAIN)
1305 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1306 BUS_DMASYNC_PREREAD);
1307 if (cm->cm_flags & AAC_CMD_DATAOUT)
1308 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1309 BUS_DMASYNC_PREWRITE);
1311 cm->cm_flags |= AAC_CMD_MAPPED;
1315 * Unmap a command from controller-visible space.
1318 aac_unmap_command(struct aac_command *cm)
1320 struct aac_softc *sc;
1326 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1329 if (cm->cm_datalen != 0) {
1330 if (cm->cm_flags & AAC_CMD_DATAIN)
1331 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1332 BUS_DMASYNC_POSTREAD);
1333 if (cm->cm_flags & AAC_CMD_DATAOUT)
1334 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1335 BUS_DMASYNC_POSTWRITE);
1337 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1339 cm->cm_flags &= ~AAC_CMD_MAPPED;
1343 * Hardware Interface
1347 * Initialise the adapter.
1350 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1352 struct aac_softc *sc;
1356 sc = (struct aac_softc *)arg;
1358 sc->aac_common_busaddr = segs[0].ds_addr;
1362 aac_check_firmware(struct aac_softc *sc)
1364 u_int32_t major, minor, options;
1369 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1370 * firmware version 1.x are not compatible with this driver.
1372 if (sc->flags & AAC_FLAGS_PERC2QC) {
1373 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1375 device_printf(sc->aac_dev,
1376 "Error reading firmware version\n");
1380 /* These numbers are stored as ASCII! */
1381 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1382 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1384 device_printf(sc->aac_dev,
1385 "Firmware version %d.%d is not supported.\n",
1392 * Retrieve the capabilities/supported options word so we know what
1393 * work-arounds to enable.
1395 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) {
1396 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
1399 options = AAC_GET_MAILBOX(sc, 1);
1400 sc->supported_options = options;
1402 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1403 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1404 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1405 if (options & AAC_SUPPORTED_NONDASD)
1406 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1412 aac_init(struct aac_softc *sc)
1414 struct aac_adapter_init *ip;
1423 * First wait for the adapter to come ready.
1427 code = AAC_GET_FWSTATUS(sc);
1428 if (code & AAC_SELF_TEST_FAILED) {
1429 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1432 if (code & AAC_KERNEL_PANIC) {
1433 device_printf(sc->aac_dev,
1434 "FATAL: controller kernel panic\n");
1437 if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1438 device_printf(sc->aac_dev,
1439 "FATAL: controller not coming ready, "
1440 "status %x\n", code);
1443 } while (!(code & AAC_UP_AND_RUNNING));
1447 * Create DMA tag for mapping buffers into controller-addressable space.
1449 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1450 1, 0, /* algnmnt, boundary */
1451 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1452 BUS_SPACE_MAXADDR, /* highaddr */
1453 NULL, NULL, /* filter, filterarg */
1454 MAXBSIZE, /* maxsize */
1455 AAC_MAXSGENTRIES, /* nsegments */
1456 MAXBSIZE, /* maxsegsize */
1457 BUS_DMA_ALLOCNOW, /* flags */
1458 &sc->aac_buffer_dmat)) {
1459 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1464 * Create DMA tag for mapping FIBs into controller-addressable space..
1466 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1467 1, 0, /* algnmnt, boundary */
1468 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1469 BUS_SPACE_MAXADDR_32BIT :
1470 0x7fffffff, /* lowaddr */
1471 BUS_SPACE_MAXADDR, /* highaddr */
1472 NULL, NULL, /* filter, filterarg */
1474 sizeof(struct aac_fib), /* maxsize */
1477 sizeof(struct aac_fib), /* maxsegsize */
1478 BUS_DMA_ALLOCNOW, /* flags */
1479 &sc->aac_fib_dmat)) {
1480 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
1485 * Create DMA tag for the common structure and allocate it.
1487 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1488 1, 0, /* algnmnt, boundary */
1489 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1490 BUS_SPACE_MAXADDR_32BIT :
1491 0x7fffffff, /* lowaddr */
1492 BUS_SPACE_MAXADDR, /* highaddr */
1493 NULL, NULL, /* filter, filterarg */
1494 8192 + sizeof(struct aac_common), /* maxsize */
1496 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1497 BUS_DMA_ALLOCNOW, /* flags */
1498 &sc->aac_common_dmat)) {
1499 device_printf(sc->aac_dev,
1500 "can't allocate common structure DMA tag\n");
1503 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1504 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1505 device_printf(sc->aac_dev, "can't allocate common structure\n");
1509 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1510 * below address 8192 in physical memory.
1511 * XXX If the padding is not needed, can it be put to use instead
1514 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1515 sc->aac_common, 8192 + sizeof(*sc->aac_common),
1516 aac_common_map, sc, 0);
1518 if (sc->aac_common_busaddr < 8192) {
1520 (struct aac_common *)((uint8_t *)sc->aac_common + 8192);
1521 sc->aac_common_busaddr += 8192;
1523 bzero(sc->aac_common, sizeof(*sc->aac_common));
1525 /* Allocate some FIBs and associated command structs */
1526 if (aac_alloc_commands(sc) != 0)
1530 * Fill in the init structure. This tells the adapter about the
1531 * physical location of various important shared data structures.
1533 ip = &sc->aac_common->ac_init;
1534 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1535 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1537 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1538 offsetof(struct aac_common, ac_fibs);
1539 ip->AdapterFibsVirtualAddress = (aac_phys_addr_t)&sc->aac_common->ac_fibs[0];
1540 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1541 ip->AdapterFibAlign = sizeof(struct aac_fib);
1543 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1544 offsetof(struct aac_common, ac_printf);
1545 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1547 /* The adapter assumes that pages are 4K in size */
1548 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1549 ip->HostElapsedSeconds = time_second; /* reset later if invalid */
1552 * Initialise FIB queues. Note that it appears that the layout of the
1553 * indexes and the segmentation of the entries may be mandated by the
1554 * adapter, which is only told about the base of the queue index fields.
1556 * The initial values of the indices are assumed to inform the adapter
1557 * of the sizes of the respective queues, and theoretically it could
1558 * work out the entire layout of the queue structures from this. We
1559 * take the easy route and just lay this area out like everyone else
1562 * The Linux driver uses a much more complex scheme whereby several
1563 * header records are kept for each queue. We use a couple of generic
1564 * list manipulation functions which 'know' the size of each list by
1565 * virtue of a table.
1567 qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN;
1568 qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN;
1569 sc->aac_queues = (struct aac_queue_table *)qaddr;
1570 ip->CommHeaderAddress = sc->aac_common_busaddr +
1571 ((u_int32_t)sc->aac_queues -
1572 (u_int32_t)sc->aac_common);
1573 bzero(sc->aac_queues, sizeof(struct aac_queue_table));
1575 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1576 AAC_HOST_NORM_CMD_ENTRIES;
1577 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1578 AAC_HOST_NORM_CMD_ENTRIES;
1579 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1580 AAC_HOST_HIGH_CMD_ENTRIES;
1581 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1582 AAC_HOST_HIGH_CMD_ENTRIES;
1583 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1584 AAC_ADAP_NORM_CMD_ENTRIES;
1585 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1586 AAC_ADAP_NORM_CMD_ENTRIES;
1587 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1588 AAC_ADAP_HIGH_CMD_ENTRIES;
1589 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1590 AAC_ADAP_HIGH_CMD_ENTRIES;
1591 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1592 AAC_HOST_NORM_RESP_ENTRIES;
1593 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1594 AAC_HOST_NORM_RESP_ENTRIES;
1595 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1596 AAC_HOST_HIGH_RESP_ENTRIES;
1597 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1598 AAC_HOST_HIGH_RESP_ENTRIES;
1599 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1600 AAC_ADAP_NORM_RESP_ENTRIES;
1601 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1602 AAC_ADAP_NORM_RESP_ENTRIES;
1603 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1604 AAC_ADAP_HIGH_RESP_ENTRIES;
1605 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1606 AAC_ADAP_HIGH_RESP_ENTRIES;
1607 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1608 &sc->aac_queues->qt_HostNormCmdQueue[0];
1609 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1610 &sc->aac_queues->qt_HostHighCmdQueue[0];
1611 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1612 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1613 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1614 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1615 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1616 &sc->aac_queues->qt_HostNormRespQueue[0];
1617 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1618 &sc->aac_queues->qt_HostHighRespQueue[0];
1619 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1620 &sc->aac_queues->qt_AdapNormRespQueue[0];
1621 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1622 &sc->aac_queues->qt_AdapHighRespQueue[0];
1625 * Do controller-type-specific initialisation
1627 switch (sc->aac_hwif) {
1628 case AAC_HWIF_I960RX:
1629 AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1634 * Give the init structure to the controller.
1636 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1637 sc->aac_common_busaddr +
1638 offsetof(struct aac_common, ac_init), 0, 0, 0,
1640 device_printf(sc->aac_dev,
1641 "error establishing init structure\n");
1652 * Send a synchronous command to the controller and wait for a result.
1655 aac_sync_command(struct aac_softc *sc, u_int32_t command,
1656 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1664 /* populate the mailbox */
1665 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1667 /* ensure the sync command doorbell flag is cleared */
1668 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1670 /* then set it to signal the adapter */
1671 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1673 /* spin waiting for the command to complete */
1676 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1677 debug(1, "timed out");
1680 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1682 /* clear the completion flag */
1683 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1685 /* get the command status */
1686 status = AAC_GET_MAILBOX(sc, 0);
1693 * Grab the sync fib area.
1696 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags)
1700 * If the force flag is set, the system is shutting down, or in
1701 * trouble. Ignore the mutex.
1703 if (!(flags & AAC_SYNC_LOCK_FORCE))
1704 AAC_LOCK_ACQUIRE(&sc->aac_sync_lock);
1706 *fib = &sc->aac_common->ac_sync_fib;
1712 * Release the sync fib area.
1715 aac_release_sync_fib(struct aac_softc *sc)
1718 AAC_LOCK_RELEASE(&sc->aac_sync_lock);
1722 * Send a synchronous FIB to the controller and wait for a result.
1725 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1726 struct aac_fib *fib, u_int16_t datasize)
1730 if (datasize > AAC_FIB_DATASIZE)
1734 * Set up the sync FIB
1736 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
1737 AAC_FIBSTATE_INITIALISED |
1739 fib->Header.XferState |= xferstate;
1740 fib->Header.Command = command;
1741 fib->Header.StructType = AAC_FIBTYPE_TFIB;
1742 fib->Header.Size = sizeof(struct aac_fib) + datasize;
1743 fib->Header.SenderSize = sizeof(struct aac_fib);
1744 fib->Header.SenderFibAddress = (u_int32_t)fib;
1745 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1746 offsetof(struct aac_common,
1750 * Give the FIB to the controller, wait for a response.
1752 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
1753 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
1754 debug(2, "IO error");
1762 * Adapter-space FIB queue manipulation
1764 * Note that the queue implementation here is a little funky; neither the PI or
1765 * CI will ever be zero. This behaviour is a controller feature.
1771 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1772 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
1773 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1774 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1775 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1776 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
1777 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1778 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1782 * Atomically insert an entry into the nominated queue, returns 0 on success or
1783 * EBUSY if the queue is full.
1785 * Note: it would be more efficient to defer notifying the controller in
1786 * the case where we may be inserting several entries in rapid succession,
1787 * but implementing this usefully may be difficult (it would involve a
1788 * separate queue/notify interface).
1791 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
1800 fib_size = cm->cm_fib->Header.Size;
1801 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
1805 /* get the producer/consumer indices */
1806 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1807 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1809 /* wrap the queue? */
1810 if (pi >= aac_qinfo[queue].size)
1813 /* check for queue full */
1814 if ((pi + 1) == ci) {
1819 /* populate queue entry */
1820 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1821 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1823 /* update producer index */
1824 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1827 * To avoid a race with its completion interrupt, place this command on
1828 * the busy queue prior to advertising it to the controller.
1830 aac_enqueue_busy(cm);
1832 /* notify the adapter if we know how */
1833 if (aac_qinfo[queue].notify != 0)
1834 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1844 * Atomically remove one entry from the nominated queue, returns 0 on
1845 * success or ENOENT if the queue is empty.
1848 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1849 struct aac_fib **fib_addr)
1859 /* get the producer/consumer indices */
1860 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1861 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1863 /* check for queue empty */
1873 /* wrap the queue? */
1874 if (ci >= aac_qinfo[queue].size)
1877 /* fetch the entry */
1878 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1879 *fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] +
1883 * Is this a fast response? If it is, update the fib fields in
1884 * local memory so the whole fib doesn't have to be DMA'd back up.
1886 if (*(uintptr_t *)fib_addr & 0x01) {
1887 *(uintptr_t *)fib_addr &= ~0x01;
1888 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
1889 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
1891 /* update consumer index */
1892 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1894 /* if we have made the queue un-full, notify the adapter */
1895 if (notify && (aac_qinfo[queue].notify != 0))
1896 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1905 * Put our response to an Adapter Initialed Fib on the response queue
1908 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
1917 /* Tell the adapter where the FIB is */
1918 fib_size = fib->Header.Size;
1919 fib_addr = fib->Header.SenderFibAddress;
1920 fib->Header.ReceiverFibAddress = fib_addr;
1924 /* get the producer/consumer indices */
1925 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1926 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1928 /* wrap the queue? */
1929 if (pi >= aac_qinfo[queue].size)
1932 /* check for queue full */
1933 if ((pi + 1) == ci) {
1938 /* populate queue entry */
1939 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1940 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1942 /* update producer index */
1943 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1945 /* notify the adapter if we know how */
1946 if (aac_qinfo[queue].notify != 0)
1947 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1957 * Check for commands that have been outstanding for a suspiciously long time,
1958 * and complain about them.
1961 aac_timeout(struct aac_softc *sc)
1964 struct aac_command *cm;
1968 /* simulate an interrupt to handle possibly-missed interrupts */
1970 * XXX This was done to work around another bug which has since been
1971 * fixed. It is dangerous anyways because you don't want multiple
1972 * threads in the interrupt handler at the same time! If calling
1973 * is deamed neccesary in the future, proper mutexes must be used.
1979 /* kick the I/O queue to restart it in the case of deadlock */
1984 * traverse the busy command list, bitch about late commands once
1987 deadline = time_second - AAC_CMD_TIMEOUT;
1989 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
1990 if ((cm->cm_timestamp < deadline)
1991 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
1992 cm->cm_flags |= AAC_CMD_TIMEDOUT;
1993 device_printf(sc->aac_dev,
1994 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
1995 cm, (int)(time_second-cm->cm_timestamp));
1996 AAC_PRINT_FIB(sc, cm->cm_fib);
2001 /* reset the timer for next time */
2002 timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
2007 * Interface Function Vectors
2011 * Read the current firmware status word.
2014 aac_sa_get_fwstatus(struct aac_softc *sc)
2018 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2022 aac_rx_get_fwstatus(struct aac_softc *sc)
2026 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2030 aac_fa_get_fwstatus(struct aac_softc *sc)
2036 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2041 * Notify the controller of a change in a given queue
2045 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2049 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2053 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2057 AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2061 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2065 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2070 * Get the interrupt reason bits
2073 aac_sa_get_istatus(struct aac_softc *sc)
2077 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2081 aac_rx_get_istatus(struct aac_softc *sc)
2085 return(AAC_GETREG4(sc, AAC_RX_ODBR));
2089 aac_fa_get_istatus(struct aac_softc *sc)
2095 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2100 * Clear some interrupt reason bits
2103 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2107 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2111 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2115 AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2119 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2123 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2128 * Populate the mailbox and set the command word
2131 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2132 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2136 AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2137 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2138 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2139 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2140 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2144 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2145 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2149 AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2150 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2151 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2152 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2153 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2157 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2158 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2162 AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2164 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2166 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2168 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2170 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2175 * Fetch the immediate command status word
2178 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2182 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2186 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2190 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2194 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2200 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2205 * Set/clear interrupt masks
2208 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2210 debug(2, "%sable interrupts", enable ? "en" : "dis");
2213 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2215 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2220 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2222 debug(2, "%sable interrupts", enable ? "en" : "dis");
2225 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2227 AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2232 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2234 debug(2, "%sable interrupts", enable ? "en" : "dis");
2237 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2240 AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2246 * Debugging and Diagnostics
2250 * Print some information about the controller.
2253 aac_describe_controller(struct aac_softc *sc)
2255 struct aac_fib *fib;
2256 struct aac_adapter_info *info;
2260 aac_alloc_sync_fib(sc, &fib, 0);
2263 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2264 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2265 aac_release_sync_fib(sc);
2268 info = (struct aac_adapter_info *)&fib->data[0];
2270 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n",
2271 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2272 info->ClockSpeed, info->BufferMem / (1024 * 1024),
2273 aac_describe_code(aac_battery_platform,
2274 info->batteryPlatform));
2276 /* save the kernel revision structure for later use */
2277 sc->aac_revision = info->KernelRevision;
2278 device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2279 info->KernelRevision.external.comp.major,
2280 info->KernelRevision.external.comp.minor,
2281 info->KernelRevision.external.comp.dash,
2282 info->KernelRevision.buildNumber,
2283 (u_int32_t)(info->SerialNumber & 0xffffff));
2285 aac_release_sync_fib(sc);
2287 if (1 || bootverbose) {
2288 device_printf(sc->aac_dev, "Supported Options=%b\n",
2289 sc->supported_options,
2308 * Look up a text description of a numeric error code and return a pointer to
2312 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2316 for (i = 0; table[i].string != NULL; i++)
2317 if (table[i].code == code)
2318 return(table[i].string);
2319 return(table[i + 1].string);
2323 * Management Interface
2327 aac_open(dev_t dev, int flags, int fmt, d_thread_t *td)
2329 struct aac_softc *sc;
2335 /* Check to make sure the device isn't already open */
2336 if (sc->aac_state & AAC_STATE_OPEN) {
2339 sc->aac_state |= AAC_STATE_OPEN;
2345 aac_close(dev_t dev, int flags, int fmt, d_thread_t *td)
2347 struct aac_softc *sc;
2353 /* Mark this unit as no longer open */
2354 sc->aac_state &= ~AAC_STATE_OPEN;
2360 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2362 struct aac_softc *sc = dev->si_drv1;
2368 if (cmd == AACIO_STATS) {
2369 union aac_statrequest *as = (union aac_statrequest *)arg;
2371 switch (as->as_item) {
2377 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2378 sizeof(struct aac_qstat));
2387 arg = *(caddr_t *)arg;
2390 /* AACIO_STATS already handled above */
2391 case FSACTL_SENDFIB:
2392 debug(1, "FSACTL_SENDFIB");
2393 error = aac_ioctl_sendfib(sc, arg);
2395 case FSACTL_AIF_THREAD:
2396 debug(1, "FSACTL_AIF_THREAD");
2399 case FSACTL_OPEN_GET_ADAPTER_FIB:
2400 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2402 * Pass the caller out an AdapterFibContext.
2404 * Note that because we only support one opener, we
2405 * basically ignore this. Set the caller's context to a magic
2406 * number just in case.
2408 * The Linux code hands the driver a pointer into kernel space,
2409 * and then trusts it when the caller hands it back. Aiee!
2410 * Here, we give it the proc pointer of the per-adapter aif
2411 * thread. It's only used as a sanity check in other calls.
2413 i = (int)sc->aifthread;
2414 error = copyout(&i, arg, sizeof(i));
2416 case FSACTL_GET_NEXT_ADAPTER_FIB:
2417 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2418 error = aac_getnext_aif(sc, arg);
2420 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2421 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2422 /* don't do anything here */
2424 case FSACTL_MINIPORT_REV_CHECK:
2425 debug(1, "FSACTL_MINIPORT_REV_CHECK");
2426 error = aac_rev_check(sc, arg);
2428 case FSACTL_QUERY_DISK:
2429 debug(1, "FSACTL_QUERY_DISK");
2430 error = aac_query_disk(sc, arg);
2432 case FSACTL_DELETE_DISK:
2434 * We don't trust the underland to tell us when to delete a
2435 * container, rather we rely on an AIF coming from the
2441 debug(1, "unsupported cmd 0x%lx\n", cmd);
2449 aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2451 struct aac_softc *sc;
2457 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2458 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2459 if (sc->aac_aifq_tail != sc->aac_aifq_head)
2460 revents |= poll_events & (POLLIN | POLLRDNORM);
2462 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2465 if (poll_events & (POLLIN | POLLRDNORM))
2466 selrecord(td, &sc->rcv_select);
2473 * Send a FIB supplied from userspace
2476 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2478 struct aac_command *cm;
2488 if (aac_alloc_command(sc, &cm)) {
2494 * Fetch the FIB header, then re-copy to get data as well.
2496 if ((error = copyin(ufib, cm->cm_fib,
2497 sizeof(struct aac_fib_header))) != 0)
2499 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2500 if (size > sizeof(struct aac_fib)) {
2501 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
2502 size, sizeof(struct aac_fib));
2503 size = sizeof(struct aac_fib);
2505 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2507 cm->cm_fib->Header.Size = size;
2508 cm->cm_timestamp = time_second;
2511 * Pass the FIB to the controller, wait for it to complete.
2513 if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */
2514 printf("aac_wait_command return %d\n", error);
2519 * Copy the FIB and data back out to the caller.
2521 size = cm->cm_fib->Header.Size;
2522 if (size > sizeof(struct aac_fib)) {
2523 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
2524 size, sizeof(struct aac_fib));
2525 size = sizeof(struct aac_fib);
2527 error = copyout(cm->cm_fib, ufib, size);
2531 aac_release_command(cm);
2537 * Handle an AIF sent to us by the controller; queue it for later reference.
2538 * If the queue fills up, then drop the older entries.
2541 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2543 struct aac_aif_command *aif;
2544 struct aac_container *co, *co_next;
2545 struct aac_mntinfo *mi;
2546 struct aac_mntinforesp *mir = NULL;
2549 int added = 0, i = 0;
2553 aif = (struct aac_aif_command*)&fib->data[0];
2554 aac_print_aif(sc, aif);
2556 /* Is it an event that we should care about? */
2557 switch (aif->command) {
2558 case AifCmdEventNotify:
2559 switch (aif->data.EN.type) {
2560 case AifEnAddContainer:
2561 case AifEnDeleteContainer:
2563 * A container was added or deleted, but the message
2564 * doesn't tell us anything else! Re-enumerate the
2565 * containers and sort things out.
2567 aac_alloc_sync_fib(sc, &fib, 0);
2568 mi = (struct aac_mntinfo *)&fib->data[0];
2571 * Ask the controller for its containers one at
2573 * XXX What if the controller's list changes
2574 * midway through this enumaration?
2575 * XXX This should be done async.
2577 bzero(mi, sizeof(struct aac_mntinfo));
2578 mi->Command = VM_NameServe;
2579 mi->MntType = FT_FILESYS;
2581 rsize = sizeof(mir);
2582 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2583 sizeof(struct aac_mntinfo))) {
2584 debug(2, "Error probing container %d\n",
2588 mir = (struct aac_mntinforesp *)&fib->data[0];
2590 * Check the container against our list.
2591 * co->co_found was already set to 0 in a
2594 if ((mir->Status == ST_OK) &&
2595 (mir->MntTable[0].VolType != CT_NONE)) {
2598 &sc->aac_container_tqh,
2600 if (co->co_mntobj.ObjectId ==
2601 mir->MntTable[0].ObjectId) {
2608 * If the container matched, continue
2617 * This is a new container. Do all the
2618 * appropriate things to set it up. */
2619 aac_add_container(sc, mir, 1);
2623 } while ((i < mir->MntRespCount) &&
2624 (i < AAC_MAX_CONTAINERS));
2625 aac_release_sync_fib(sc);
2628 * Go through our list of containers and see which ones
2629 * were not marked 'found'. Since the controller didn't
2630 * list them they must have been deleted. Do the
2631 * appropriate steps to destroy the device. Also reset
2632 * the co->co_found field.
2634 co = TAILQ_FIRST(&sc->aac_container_tqh);
2635 while (co != NULL) {
2636 if (co->co_found == 0) {
2637 device_delete_child(sc->aac_dev,
2639 co_next = TAILQ_NEXT(co, co_link);
2640 AAC_LOCK_ACQUIRE(&sc->
2641 aac_container_lock);
2642 TAILQ_REMOVE(&sc->aac_container_tqh, co,
2644 AAC_LOCK_RELEASE(&sc->
2645 aac_container_lock);
2650 co = TAILQ_NEXT(co, co_link);
2654 /* Attach the newly created containers */
2656 bus_generic_attach(sc->aac_dev);
2668 /* Copy the AIF data to the AIF queue for ioctl retrieval */
2669 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2670 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2671 if (next != sc->aac_aifq_tail) {
2672 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2673 sc->aac_aifq_head = next;
2675 /* On the off chance that someone is sleeping for an aif... */
2676 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2677 wakeup(sc->aac_aifq);
2678 /* token may have been lost */
2679 /* Wakeup any poll()ers */
2680 selwakeup(&sc->rcv_select);
2681 /* token may have been lost */
2683 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2689 * Return the Revision of the driver to userspace and check to see if the
2690 * userspace app is possibly compatible. This is extremely bogus since
2691 * our driver doesn't follow Adaptec's versioning system. Cheat by just
2692 * returning what the card reported.
2695 aac_rev_check(struct aac_softc *sc, caddr_t udata)
2697 struct aac_rev_check rev_check;
2698 struct aac_rev_check_resp rev_check_resp;
2704 * Copyin the revision struct from userspace
2706 if ((error = copyin(udata, (caddr_t)&rev_check,
2707 sizeof(struct aac_rev_check))) != 0) {
2711 debug(2, "Userland revision= %d\n",
2712 rev_check.callingRevision.buildNumber);
2715 * Doctor up the response struct.
2717 rev_check_resp.possiblyCompatible = 1;
2718 rev_check_resp.adapterSWRevision.external.ul =
2719 sc->aac_revision.external.ul;
2720 rev_check_resp.adapterSWRevision.buildNumber =
2721 sc->aac_revision.buildNumber;
2723 return(copyout((caddr_t)&rev_check_resp, udata,
2724 sizeof(struct aac_rev_check_resp)));
2728 * Pass the caller the next AIF in their queue
2731 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2733 struct get_adapter_fib_ioctl agf;
2738 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2741 * Check the magic number that we gave the caller.
2743 if (agf.AdapterFibContext != (int)sc->aifthread) {
2748 error = aac_return_aif(sc, agf.AifFib);
2750 if ((error == EAGAIN) && (agf.Wait)) {
2751 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2752 while (error == EAGAIN) {
2753 error = tsleep(sc->aac_aifq,
2754 PCATCH, "aacaif", 0);
2756 error = aac_return_aif(sc,
2759 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2768 * Hand the next AIF off the top of the queue out to userspace.
2770 * YYY token could be lost during copyout
2773 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2779 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2780 if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2783 error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2784 sizeof(struct aac_aif_command));
2786 printf("aac_return_aif: copyout returned %d\n", error);
2788 sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) %
2791 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2796 * Give the userland some information about the container. The AAC arch
2797 * expects the driver to be a SCSI passthrough type driver, so it expects
2798 * the containers to have b:t:l numbers. Fake it.
2801 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2803 struct aac_query_disk query_disk;
2804 struct aac_container *co;
2805 struct aac_disk *disk;
2812 error = copyin(uptr, (caddr_t)&query_disk,
2813 sizeof(struct aac_query_disk));
2817 id = query_disk.ContainerNumber;
2821 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2822 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2823 if (co->co_mntobj.ObjectId == id)
2828 query_disk.Valid = 0;
2829 query_disk.Locked = 0;
2830 query_disk.Deleted = 1; /* XXX is this right? */
2832 disk = device_get_softc(co->co_disk);
2833 query_disk.Valid = 1;
2835 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2836 query_disk.Deleted = 0;
2837 query_disk.Bus = device_get_unit(sc->aac_dev);
2838 query_disk.Target = disk->unit;
2840 query_disk.UnMapped = 0;
2841 bcopy(disk->ad_dev_t->si_name,
2842 &query_disk.diskDeviceName[0], 10);
2844 AAC_LOCK_RELEASE(&sc->aac_container_lock);
2846 error = copyout((caddr_t)&query_disk, uptr,
2847 sizeof(struct aac_query_disk));
2853 aac_get_bus_info(struct aac_softc *sc)
2855 struct aac_fib *fib;
2856 struct aac_ctcfg *c_cmd;
2857 struct aac_ctcfg_resp *c_resp;
2858 struct aac_vmioctl *vmi;
2859 struct aac_vmi_businf_resp *vmi_resp;
2860 struct aac_getbusinf businfo;
2861 struct aac_cam_inf *caminf;
2863 int i, found, error;
2865 aac_alloc_sync_fib(sc, &fib, 0);
2866 c_cmd = (struct aac_ctcfg *)&fib->data[0];
2867 bzero(c_cmd, sizeof(struct aac_ctcfg));
2869 c_cmd->Command = VM_ContainerConfig;
2870 c_cmd->cmd = CT_GET_SCSI_METHOD;
2873 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2874 sizeof(struct aac_ctcfg));
2876 device_printf(sc->aac_dev, "Error %d sending "
2877 "VM_ContainerConfig command\n", error);
2878 aac_release_sync_fib(sc);
2882 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2883 if (c_resp->Status != ST_OK) {
2884 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2886 aac_release_sync_fib(sc);
2890 sc->scsi_method_id = c_resp->param;
2892 vmi = (struct aac_vmioctl *)&fib->data[0];
2893 bzero(vmi, sizeof(struct aac_vmioctl));
2895 vmi->Command = VM_Ioctl;
2896 vmi->ObjType = FT_DRIVE;
2897 vmi->MethId = sc->scsi_method_id;
2899 vmi->IoctlCmd = GetBusInfo;
2901 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2902 sizeof(struct aac_vmioctl));
2904 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2906 aac_release_sync_fib(sc);
2910 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2911 if (vmi_resp->Status != ST_OK) {
2912 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
2913 aac_release_sync_fib(sc);
2917 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2918 aac_release_sync_fib(sc);
2921 for (i = 0; i < businfo.BusCount; i++) {
2922 if (businfo.BusValid[i] != AAC_BUS_VALID)
2925 MALLOC(caminf, struct aac_cam_inf *,
2926 sizeof(struct aac_cam_inf), M_AACBUF, M_INTWAIT | M_ZERO);
2928 child = device_add_child(sc->aac_dev, "aacp", -1);
2929 if (child == NULL) {
2930 device_printf(sc->aac_dev, "device_add_child failed\n");
2934 caminf->TargetsPerBus = businfo.TargetsPerBus;
2935 caminf->BusNumber = i;
2936 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2937 caminf->aac_sc = sc;
2939 device_set_ivars(child, caminf);
2940 device_set_desc(child, "SCSI Passthrough Bus");
2946 bus_generic_attach(sc->aac_dev);