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.16 2004/09/15 16:23:51 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(void *ssc);
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)
233 callout_init(&sc->aac_watchdog);
236 * Initialise per-controller queues.
241 aac_initq_complete(sc);
244 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
246 * Initialise command-completion task.
248 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
251 /* disable interrupts before we enable anything */
252 AAC_MASK_INTERRUPTS(sc);
254 /* mark controller as suspended until we get ourselves organised */
255 sc->aac_state |= AAC_STATE_SUSPEND;
258 * Check that the firmware on the card is supported.
260 if ((error = aac_check_firmware(sc)) != 0)
263 /* Init the sync fib lock */
264 AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock");
267 * Initialise the adapter.
269 if ((error = aac_init(sc)) != 0)
273 * Print a little information about the controller.
275 aac_describe_controller(sc);
278 * Register to probe our containers later.
280 TAILQ_INIT(&sc->aac_container_tqh);
281 AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
284 * Lock for the AIF queue
286 AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
288 sc->aac_ich.ich_func = aac_startup;
289 sc->aac_ich.ich_arg = sc;
290 if (config_intrhook_establish(&sc->aac_ich) != 0) {
291 device_printf(sc->aac_dev,
292 "can't establish configuration hook\n");
297 * Make the control device.
299 unit = device_get_unit(sc->aac_dev);
300 cdevsw_add(&aac_cdevsw, -1, unit);
301 sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
303 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
304 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
305 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
307 sc->aac_dev_t->si_drv1 = sc;
308 reference_dev(sc->aac_dev_t);
310 /* Create the AIF thread */
311 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
312 if (kthread_create((void(*)(void *))aac_host_command, sc,
313 &sc->aifthread, 0, "aac%daif", unit))
315 if (kthread_create((void(*)(void *))aac_host_command, sc,
316 &sc->aifthread, "aac%daif", unit))
318 panic("Could not create AIF thread\n");
320 /* Register the shutdown method to only be called post-dump */
321 if ((EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown, sc->aac_dev,
322 SHUTDOWN_PRI_DEFAULT)) == NULL)
323 device_printf(sc->aac_dev, "shutdown event registration failed\n");
325 /* Register with CAM for the non-DASD devices */
326 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0)
327 aac_get_bus_info(sc);
333 * Probe for containers, create disks.
336 aac_startup(void *arg)
338 struct aac_softc *sc;
340 struct aac_mntinfo *mi;
341 struct aac_mntinforesp *mir = NULL;
346 sc = (struct aac_softc *)arg;
348 /* disconnect ourselves from the intrhook chain */
349 config_intrhook_disestablish(&sc->aac_ich);
351 aac_alloc_sync_fib(sc, &fib, 0);
352 mi = (struct aac_mntinfo *)&fib->data[0];
354 /* loop over possible containers */
356 /* request information on this container */
357 bzero(mi, sizeof(struct aac_mntinfo));
358 mi->Command = VM_NameServe;
359 mi->MntType = FT_FILESYS;
361 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
362 sizeof(struct aac_mntinfo))) {
363 debug(2, "error probing container %d", i);
367 mir = (struct aac_mntinforesp *)&fib->data[0];
368 aac_add_container(sc, mir, 0);
370 } while ((i < mir->MntRespCount) && (i < AAC_MAX_CONTAINERS));
372 aac_release_sync_fib(sc);
374 /* poke the bus to actually attach the child devices */
375 if (bus_generic_attach(sc->aac_dev))
376 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
378 /* mark the controller up */
379 sc->aac_state &= ~AAC_STATE_SUSPEND;
381 /* enable interrupts now */
382 AAC_UNMASK_INTERRUPTS(sc);
384 /* enable the timeout watchdog */
385 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz,
390 * Create a device to respresent a new container
393 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
395 struct aac_container *co;
399 * Check container volume type for validity. Note that many of
400 * the possible types may never show up.
402 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
403 MALLOC(co, struct aac_container *, sizeof *co, M_AACBUF,
405 debug(1, "id %x name '%.16s' size %u type %d",
406 mir->MntTable[0].ObjectId,
407 mir->MntTable[0].FileSystemName,
408 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
410 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
411 device_printf(sc->aac_dev, "device_add_child failed\n");
413 device_set_ivars(child, co);
414 device_set_desc(child, aac_describe_code(aac_container_types,
415 mir->MntTable[0].VolType));
418 bcopy(&mir->MntTable[0], &co->co_mntobj,
419 sizeof(struct aac_mntobj));
420 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
421 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
422 AAC_LOCK_RELEASE(&sc->aac_container_lock);
427 * Free all of the resources associated with (sc)
429 * Should not be called if the controller is active.
432 aac_free(struct aac_softc *sc)
436 /* remove the control device */
437 if (sc->aac_dev_t != NULL)
438 destroy_dev(sc->aac_dev_t);
440 /* throw away any FIB buffers, discard the FIB DMA tag */
441 if (sc->aac_fibs != NULL)
442 aac_free_commands(sc);
443 if (sc->aac_fib_dmat)
444 bus_dma_tag_destroy(sc->aac_fib_dmat);
446 /* destroy the common area */
447 if (sc->aac_common) {
448 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
449 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
450 sc->aac_common_dmamap);
452 if (sc->aac_common_dmat)
453 bus_dma_tag_destroy(sc->aac_common_dmat);
455 /* disconnect the interrupt handler */
457 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
458 if (sc->aac_irq != NULL)
459 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
462 /* destroy data-transfer DMA tag */
463 if (sc->aac_buffer_dmat)
464 bus_dma_tag_destroy(sc->aac_buffer_dmat);
466 /* destroy the parent DMA tag */
467 if (sc->aac_parent_dmat)
468 bus_dma_tag_destroy(sc->aac_parent_dmat);
470 /* release the register window mapping */
471 if (sc->aac_regs_resource != NULL) {
472 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
473 sc->aac_regs_rid, sc->aac_regs_resource);
475 cdevsw_remove(&aac_cdevsw, -1, device_get_unit(sc->aac_dev));
479 * Disconnect from the controller completely, in preparation for unload.
482 aac_detach(device_t dev)
484 struct aac_softc *sc;
491 sc = device_get_softc(dev);
493 callout_stop(&sc->aac_watchdog);
495 if (sc->aac_state & AAC_STATE_OPEN)
499 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
500 sc->aifflags |= AAC_AIFFLAGS_EXIT;
501 wakeup(sc->aifthread);
502 tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz);
505 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
506 panic("Cannot shutdown AIF thread\n");
508 if ((error = aac_shutdown(dev)))
520 * Bring the controller down to a dormant state and detach all child devices.
522 * This function is called before detach or system shutdown.
524 * Note that we can assume that the bioq on the controller is empty, as we won't
525 * allow shutdown if any device is open.
528 aac_shutdown(device_t dev)
530 struct aac_softc *sc;
532 struct aac_close_command *cc;
537 sc = device_get_softc(dev);
541 sc->aac_state |= AAC_STATE_SUSPEND;
544 * Send a Container shutdown followed by a HostShutdown FIB to the
545 * controller to convince it that we don't want to talk to it anymore.
546 * We've been closed and all I/O completed already
548 device_printf(sc->aac_dev, "shutting down controller...");
550 aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
551 cc = (struct aac_close_command *)&fib->data[0];
553 bzero(cc, sizeof(struct aac_close_command));
554 cc->Command = VM_CloseAll;
555 cc->ContainerId = 0xffffffff;
556 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
557 sizeof(struct aac_close_command)))
562 * XXX Issuing this command to the controller makes it shut down
563 * but also keeps it from coming back up without a reset of the
564 * PCI bus. This is not desirable if you are just unloading the
565 * driver module with the intent to reload it later.
567 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
575 AAC_MASK_INTERRUPTS(sc);
582 * Bring the controller to a quiescent state, ready for system suspend.
585 aac_suspend(device_t dev)
587 struct aac_softc *sc;
592 sc = device_get_softc(dev);
596 sc->aac_state |= AAC_STATE_SUSPEND;
598 AAC_MASK_INTERRUPTS(sc);
604 * Bring the controller back to a state ready for operation.
607 aac_resume(device_t dev)
609 struct aac_softc *sc;
613 sc = device_get_softc(dev);
615 sc->aac_state &= ~AAC_STATE_SUSPEND;
616 AAC_UNMASK_INTERRUPTS(sc);
626 struct aac_softc *sc;
628 u_int32_t *resp_queue;
632 sc = (struct aac_softc *)arg;
635 * Optimize the common case of adapter response interrupts.
636 * We must read from the card prior to processing the responses
637 * to ensure the clear is flushed prior to accessing the queues.
638 * Reading the queues from local memory might save us a PCI read.
640 resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE];
641 if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX])
642 reason = AAC_DB_RESPONSE_READY;
644 reason = AAC_GET_ISTATUS(sc);
645 AAC_CLEAR_ISTATUS(sc, reason);
646 (void)AAC_GET_ISTATUS(sc);
648 /* It's not ok to return here because of races with the previous step */
649 if (reason & AAC_DB_RESPONSE_READY)
650 aac_host_response(sc);
652 /* controller wants to talk to the log */
653 if (reason & AAC_DB_PRINTF)
654 aac_print_printf(sc);
656 /* controller has a message for us? */
657 if (reason & AAC_DB_COMMAND_READY) {
658 /* XXX What happens if the thread is already awake? */
659 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
660 sc->aifflags |= AAC_AIFFLAGS_PENDING;
661 wakeup(sc->aifthread);
671 * Start as much queued I/O as possible on the controller
674 aac_startio(struct aac_softc *sc)
676 struct aac_command *cm;
682 * Try to get a command that's been put off for lack of
685 cm = aac_dequeue_ready(sc);
688 * Try to build a command off the bio queue (ignore error
692 aac_bio_command(sc, &cm);
698 /* try to give the command to the controller */
699 if (aac_start(cm) == EBUSY) {
700 /* put it on the ready queue for later */
701 aac_requeue_ready(cm);
708 * Deliver a command to the controller; allocate controller resources at the
709 * last moment when possible.
712 aac_start(struct aac_command *cm)
714 struct aac_softc *sc;
721 /* get the command mapped */
724 /* fix up the address values in the FIB */
725 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
726 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
728 /* save a pointer to the command for speedy reverse-lookup */
729 cm->cm_fib->Header.SenderData = (u_int32_t)cm; /* XXX 64-bit physical
731 /* put the FIB on the outbound queue */
732 error = aac_enqueue_fib(sc, cm->cm_queue, cm);
737 * Handle notification of one or more FIBs coming from the controller.
740 aac_host_command(struct aac_softc *sc)
748 sc->aifflags |= AAC_AIFFLAGS_RUNNING;
750 while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) {
751 if (!(sc->aifflags & AAC_AIFFLAGS_PENDING))
752 tsleep(sc->aifthread, 0, "aifthd", 15 * hz);
754 sc->aifflags &= ~AAC_AIFFLAGS_PENDING;
756 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
758 break; /* nothing to do */
760 AAC_PRINT_FIB(sc, fib);
762 switch (fib->Header.Command) {
764 aac_handle_aif(sc, fib);
767 device_printf(sc->aac_dev, "unknown command "
768 "from controller\n");
772 /* Return the AIF to the controller. */
773 if ((fib->Header.XferState == 0) ||
774 (fib->Header.StructType != AAC_FIBTYPE_TFIB))
777 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
778 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
779 *(AAC_FSAStatus*)fib->data = ST_OK;
781 /* XXX Compute the Size field? */
782 size = fib->Header.Size;
783 if (size > sizeof(struct aac_fib)) {
784 size = sizeof(struct aac_fib);
785 fib->Header.Size = size;
788 * Since we did not generate this command, it
789 * cannot go through the normal
790 * enqueue->startio chain.
792 aac_enqueue_response(sc,
793 AAC_ADAP_NORM_RESP_QUEUE,
798 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
801 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
808 * Handle notification of one or more FIBs completed by the controller
811 aac_host_response(struct aac_softc *sc)
813 struct aac_command *cm;
820 /* look for completed FIBs on our queue */
821 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
823 break; /* nothing to do */
825 /* get the command, unmap and queue for later processing */
826 cm = (struct aac_command *)fib->Header.SenderData;
828 AAC_PRINT_FIB(sc, fib);
831 aac_unmap_command(cm); /* XXX defer? */
832 aac_enqueue_complete(cm);
836 /* handle completion processing */
837 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
838 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
845 * Process completed commands.
848 aac_complete(void *context, int pending)
850 struct aac_softc *sc;
851 struct aac_command *cm;
855 sc = (struct aac_softc *)context;
857 /* pull completed commands off the queue */
859 cm = aac_dequeue_complete(sc);
862 cm->cm_flags |= AAC_CMD_COMPLETED;
864 /* is there a completion handler? */
865 if (cm->cm_complete != NULL) {
868 /* assume that someone is sleeping on this command */
873 /* see if we can start some more I/O */
878 * Handle a bio submitted from a disk device.
881 aac_submit_bio(struct bio *bp)
884 struct aac_softc *sc;
888 ad = (struct aac_disk *)bp->bio_dev->si_drv1;
889 sc = ad->ad_controller;
891 /* queue the BIO and try to get some work done */
892 aac_enqueue_bio(sc, bp);
897 * Get a bio and build a command to go with it.
900 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
902 struct aac_command *cm;
904 struct aac_blockread *br;
905 struct aac_blockwrite *bw;
911 /* get the resources we will need */
913 if ((bp = aac_dequeue_bio(sc)) == NULL)
915 if (aac_alloc_command(sc, &cm)) /* get a command */
918 /* fill out the command */
919 cm->cm_data = (void *)bp->bio_data;
920 cm->cm_datalen = bp->bio_bcount;
921 cm->cm_complete = aac_bio_complete;
923 cm->cm_timestamp = time_second;
924 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
928 fib->Header.XferState =
929 AAC_FIBSTATE_HOSTOWNED |
930 AAC_FIBSTATE_INITIALISED |
932 AAC_FIBSTATE_FROMHOST |
933 AAC_FIBSTATE_REXPECTED |
936 AAC_FIBSTATE_FAST_RESPONSE;
937 fib->Header.Command = ContainerCommand;
938 fib->Header.Size = sizeof(struct aac_fib_header);
940 /* build the read/write request */
941 ad = (struct aac_disk *)bp->bio_dev->si_drv1;
942 if (BIO_IS_READ(bp)) {
943 br = (struct aac_blockread *)&fib->data[0];
944 br->Command = VM_CtBlockRead;
945 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
946 br->BlockNumber = bp->bio_pblkno;
947 br->ByteCount = bp->bio_bcount;
948 fib->Header.Size += sizeof(struct aac_blockread);
949 cm->cm_sgtable = &br->SgMap;
950 cm->cm_flags |= AAC_CMD_DATAIN;
952 bw = (struct aac_blockwrite *)&fib->data[0];
953 bw->Command = VM_CtBlockWrite;
954 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
955 bw->BlockNumber = bp->bio_pblkno;
956 bw->ByteCount = bp->bio_bcount;
957 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
958 fib->Header.Size += sizeof(struct aac_blockwrite);
959 cm->cm_flags |= AAC_CMD_DATAOUT;
960 cm->cm_sgtable = &bw->SgMap;
968 aac_enqueue_bio(sc, bp);
970 aac_release_command(cm);
975 * Handle a bio-instigated command that has been completed.
978 aac_bio_complete(struct aac_command *cm)
980 struct aac_blockread_response *brr;
981 struct aac_blockwrite_response *bwr;
983 AAC_FSAStatus status;
985 /* fetch relevant status and then release the command */
986 bp = (struct bio *)cm->cm_private;
987 if (BIO_IS_READ(bp)) {
988 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
989 status = brr->Status;
991 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
992 status = bwr->Status;
994 aac_release_command(cm);
996 /* fix up the bio based on status */
997 if (status == ST_OK) {
1000 bp->bio_error = EIO;
1001 bp->bio_flags |= BIO_ERROR;
1002 /* pass an error string out to the disk layer */
1003 bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1010 * Dump a block of data to the controller. If the queue is full, tell the
1011 * caller to hold off and wait for the queue to drain.
1014 aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba, void *data, int dumppages)
1016 struct aac_softc *sc;
1017 struct aac_command *cm;
1018 struct aac_fib *fib;
1019 struct aac_blockwrite *bw;
1021 sc = ad->ad_controller;
1024 if (aac_alloc_command(sc, &cm))
1027 /* fill out the command */
1029 cm->cm_datalen = dumppages * PAGE_SIZE;
1030 cm->cm_complete = NULL;
1031 cm->cm_private = NULL;
1032 cm->cm_timestamp = time_second;
1033 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1037 fib->Header.XferState =
1038 AAC_FIBSTATE_HOSTOWNED |
1039 AAC_FIBSTATE_INITIALISED |
1040 AAC_FIBSTATE_FROMHOST |
1041 AAC_FIBSTATE_REXPECTED |
1043 fib->Header.Command = ContainerCommand;
1044 fib->Header.Size = sizeof(struct aac_fib_header);
1046 bw = (struct aac_blockwrite *)&fib->data[0];
1047 bw->Command = VM_CtBlockWrite;
1048 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1049 bw->BlockNumber = lba;
1050 bw->ByteCount = dumppages * PAGE_SIZE;
1051 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
1052 fib->Header.Size += sizeof(struct aac_blockwrite);
1053 cm->cm_flags |= AAC_CMD_DATAOUT;
1054 cm->cm_sgtable = &bw->SgMap;
1056 return (aac_start(cm));
1060 * Wait for the card's queue to drain when dumping. Also check for monitor
1064 aac_dump_complete(struct aac_softc *sc)
1066 struct aac_fib *fib;
1067 struct aac_command *cm;
1069 u_int32_t pi, ci, fib_size;
1072 reason = AAC_GET_ISTATUS(sc);
1073 if (reason & AAC_DB_RESPONSE_READY) {
1074 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
1076 if (aac_dequeue_fib(sc,
1077 AAC_HOST_NORM_RESP_QUEUE,
1080 cm = (struct aac_command *)
1081 fib->Header.SenderData;
1083 AAC_PRINT_FIB(sc, fib);
1085 aac_remove_busy(cm);
1086 aac_unmap_command(cm);
1087 aac_enqueue_complete(cm);
1088 aac_release_command(cm);
1092 if (reason & AAC_DB_PRINTF) {
1093 AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
1094 aac_print_printf(sc);
1096 pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1097 AAC_PRODUCER_INDEX];
1098 ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1099 AAC_CONSUMER_INDEX];
1106 * Submit a command to the controller, return when it completes.
1107 * XXX This is very dangerous! If the card has gone out to lunch, we could
1108 * be stuck here forever. At the same time, signals are not caught
1109 * because there is a risk that a signal could wakeup the tsleep before
1110 * the card has a chance to complete the command. The passed in timeout
1111 * is ignored for the same reason. Since there is no way to cancel a
1112 * command in progress, we should probably create a 'dead' queue where
1113 * commands go that have been interrupted/timed-out/etc, that keeps them
1114 * out of the free pool. That way, if the card is just slow, it won't
1115 * spam the memory of a command that has been recycled.
1118 aac_wait_command(struct aac_command *cm, int timeout)
1124 /* Put the command on the ready queue and get things going */
1125 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1126 aac_enqueue_ready(cm);
1127 aac_startio(cm->cm_sc);
1129 while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
1130 error = tsleep(cm, 0, "aacwait", 0);
1137 *Command Buffer Management
1141 * Allocate a command.
1144 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1146 struct aac_command *cm;
1150 if ((cm = aac_dequeue_free(sc)) == NULL)
1158 * Release a command back to the freelist.
1161 aac_release_command(struct aac_command *cm)
1165 /* (re)initialise the command/FIB */
1166 cm->cm_sgtable = NULL;
1168 cm->cm_complete = NULL;
1169 cm->cm_private = NULL;
1170 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1171 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1172 cm->cm_fib->Header.Flags = 0;
1173 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1176 * These are duplicated in aac_start to cover the case where an
1177 * intermediate stage may have destroyed them. They're left
1178 * initialised here for debugging purposes only.
1180 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
1181 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1182 cm->cm_fib->Header.SenderData = 0;
1184 aac_enqueue_free(cm);
1188 * Map helper for command/FIB allocation.
1191 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1193 struct aac_softc *sc;
1195 sc = (struct aac_softc *)arg;
1199 sc->aac_fibphys = segs[0].ds_addr;
1203 * Allocate and initialise commands/FIBs for this adapter.
1206 aac_alloc_commands(struct aac_softc *sc)
1208 struct aac_command *cm;
1213 /* allocate the FIBs in DMAable memory and load them */
1214 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs,
1215 BUS_DMA_NOWAIT, &sc->aac_fibmap)) {
1219 bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs,
1220 AAC_FIB_COUNT * sizeof(struct aac_fib),
1221 aac_map_command_helper, sc, 0);
1223 /* initialise constant fields in the command structure */
1224 bzero(sc->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib));
1225 for (i = 0; i < AAC_FIB_COUNT; i++) {
1226 cm = &sc->aac_command[i];
1228 cm->cm_fib = sc->aac_fibs + i;
1229 cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib));
1231 if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap))
1232 aac_release_command(cm);
1238 * Free FIBs owned by this adapter.
1241 aac_free_commands(struct aac_softc *sc)
1247 for (i = 0; i < AAC_FIB_COUNT; i++)
1248 bus_dmamap_destroy(sc->aac_buffer_dmat,
1249 sc->aac_command[i].cm_datamap);
1251 bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap);
1252 bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap);
1256 * Command-mapping helper function - populate this command's s/g table.
1259 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1261 struct aac_command *cm;
1262 struct aac_fib *fib;
1263 struct aac_sg_table *sg;
1268 cm = (struct aac_command *)arg;
1271 /* find the s/g table */
1272 sg = cm->cm_sgtable;
1274 /* copy into the FIB */
1277 for (i = 0; i < nseg; i++) {
1278 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1279 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1281 /* update the FIB size for the s/g count */
1282 fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1288 * Map a command into controller-visible space.
1291 aac_map_command(struct aac_command *cm)
1293 struct aac_softc *sc;
1299 /* don't map more than once */
1300 if (cm->cm_flags & AAC_CMD_MAPPED)
1303 if (cm->cm_datalen != 0) {
1304 bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
1305 cm->cm_data, cm->cm_datalen,
1306 aac_map_command_sg, cm, 0);
1308 if (cm->cm_flags & AAC_CMD_DATAIN)
1309 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1310 BUS_DMASYNC_PREREAD);
1311 if (cm->cm_flags & AAC_CMD_DATAOUT)
1312 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1313 BUS_DMASYNC_PREWRITE);
1315 cm->cm_flags |= AAC_CMD_MAPPED;
1319 * Unmap a command from controller-visible space.
1322 aac_unmap_command(struct aac_command *cm)
1324 struct aac_softc *sc;
1330 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1333 if (cm->cm_datalen != 0) {
1334 if (cm->cm_flags & AAC_CMD_DATAIN)
1335 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1336 BUS_DMASYNC_POSTREAD);
1337 if (cm->cm_flags & AAC_CMD_DATAOUT)
1338 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1339 BUS_DMASYNC_POSTWRITE);
1341 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1343 cm->cm_flags &= ~AAC_CMD_MAPPED;
1347 * Hardware Interface
1351 * Initialise the adapter.
1354 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1356 struct aac_softc *sc;
1360 sc = (struct aac_softc *)arg;
1362 sc->aac_common_busaddr = segs[0].ds_addr;
1366 aac_check_firmware(struct aac_softc *sc)
1368 u_int32_t major, minor, options;
1373 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1374 * firmware version 1.x are not compatible with this driver.
1376 if (sc->flags & AAC_FLAGS_PERC2QC) {
1377 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1379 device_printf(sc->aac_dev,
1380 "Error reading firmware version\n");
1384 /* These numbers are stored as ASCII! */
1385 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1386 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1388 device_printf(sc->aac_dev,
1389 "Firmware version %d.%d is not supported.\n",
1396 * Retrieve the capabilities/supported options word so we know what
1397 * work-arounds to enable.
1399 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) {
1400 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
1403 options = AAC_GET_MAILBOX(sc, 1);
1404 sc->supported_options = options;
1406 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1407 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1408 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1409 if (options & AAC_SUPPORTED_NONDASD)
1410 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1416 aac_init(struct aac_softc *sc)
1418 struct aac_adapter_init *ip;
1427 * First wait for the adapter to come ready.
1431 code = AAC_GET_FWSTATUS(sc);
1432 if (code & AAC_SELF_TEST_FAILED) {
1433 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1436 if (code & AAC_KERNEL_PANIC) {
1437 device_printf(sc->aac_dev,
1438 "FATAL: controller kernel panic\n");
1441 if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1442 device_printf(sc->aac_dev,
1443 "FATAL: controller not coming ready, "
1444 "status %x\n", code);
1447 } while (!(code & AAC_UP_AND_RUNNING));
1451 * Create DMA tag for mapping buffers into controller-addressable space.
1453 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1454 1, 0, /* algnmnt, boundary */
1455 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1456 BUS_SPACE_MAXADDR, /* highaddr */
1457 NULL, NULL, /* filter, filterarg */
1458 MAXBSIZE, /* maxsize */
1459 AAC_MAXSGENTRIES, /* nsegments */
1460 MAXBSIZE, /* maxsegsize */
1461 BUS_DMA_ALLOCNOW, /* flags */
1462 &sc->aac_buffer_dmat)) {
1463 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1468 * Create DMA tag for mapping FIBs into controller-addressable space..
1470 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1471 1, 0, /* algnmnt, boundary */
1472 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1473 BUS_SPACE_MAXADDR_32BIT :
1474 0x7fffffff, /* lowaddr */
1475 BUS_SPACE_MAXADDR, /* highaddr */
1476 NULL, NULL, /* filter, filterarg */
1478 sizeof(struct aac_fib), /* maxsize */
1481 sizeof(struct aac_fib), /* maxsegsize */
1482 BUS_DMA_ALLOCNOW, /* flags */
1483 &sc->aac_fib_dmat)) {
1484 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
1489 * Create DMA tag for the common structure and allocate it.
1491 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1492 1, 0, /* algnmnt, boundary */
1493 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1494 BUS_SPACE_MAXADDR_32BIT :
1495 0x7fffffff, /* lowaddr */
1496 BUS_SPACE_MAXADDR, /* highaddr */
1497 NULL, NULL, /* filter, filterarg */
1498 8192 + sizeof(struct aac_common), /* maxsize */
1500 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1501 BUS_DMA_ALLOCNOW, /* flags */
1502 &sc->aac_common_dmat)) {
1503 device_printf(sc->aac_dev,
1504 "can't allocate common structure DMA tag\n");
1507 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1508 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1509 device_printf(sc->aac_dev, "can't allocate common structure\n");
1513 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1514 * below address 8192 in physical memory.
1515 * XXX If the padding is not needed, can it be put to use instead
1518 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1519 sc->aac_common, 8192 + sizeof(*sc->aac_common),
1520 aac_common_map, sc, 0);
1522 if (sc->aac_common_busaddr < 8192) {
1524 (struct aac_common *)((uint8_t *)sc->aac_common + 8192);
1525 sc->aac_common_busaddr += 8192;
1527 bzero(sc->aac_common, sizeof(*sc->aac_common));
1529 /* Allocate some FIBs and associated command structs */
1530 if (aac_alloc_commands(sc) != 0)
1534 * Fill in the init structure. This tells the adapter about the
1535 * physical location of various important shared data structures.
1537 ip = &sc->aac_common->ac_init;
1538 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1539 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1541 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1542 offsetof(struct aac_common, ac_fibs);
1543 ip->AdapterFibsVirtualAddress = (aac_phys_addr_t)&sc->aac_common->ac_fibs[0];
1544 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1545 ip->AdapterFibAlign = sizeof(struct aac_fib);
1547 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1548 offsetof(struct aac_common, ac_printf);
1549 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1551 /* The adapter assumes that pages are 4K in size */
1552 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1553 ip->HostElapsedSeconds = time_second; /* reset later if invalid */
1556 * Initialise FIB queues. Note that it appears that the layout of the
1557 * indexes and the segmentation of the entries may be mandated by the
1558 * adapter, which is only told about the base of the queue index fields.
1560 * The initial values of the indices are assumed to inform the adapter
1561 * of the sizes of the respective queues, and theoretically it could
1562 * work out the entire layout of the queue structures from this. We
1563 * take the easy route and just lay this area out like everyone else
1566 * The Linux driver uses a much more complex scheme whereby several
1567 * header records are kept for each queue. We use a couple of generic
1568 * list manipulation functions which 'know' the size of each list by
1569 * virtue of a table.
1571 qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN;
1572 qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN;
1573 sc->aac_queues = (struct aac_queue_table *)qaddr;
1574 ip->CommHeaderAddress = sc->aac_common_busaddr +
1575 ((u_int32_t)sc->aac_queues -
1576 (u_int32_t)sc->aac_common);
1577 bzero(sc->aac_queues, sizeof(struct aac_queue_table));
1579 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1580 AAC_HOST_NORM_CMD_ENTRIES;
1581 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1582 AAC_HOST_NORM_CMD_ENTRIES;
1583 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1584 AAC_HOST_HIGH_CMD_ENTRIES;
1585 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1586 AAC_HOST_HIGH_CMD_ENTRIES;
1587 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1588 AAC_ADAP_NORM_CMD_ENTRIES;
1589 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1590 AAC_ADAP_NORM_CMD_ENTRIES;
1591 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1592 AAC_ADAP_HIGH_CMD_ENTRIES;
1593 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1594 AAC_ADAP_HIGH_CMD_ENTRIES;
1595 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1596 AAC_HOST_NORM_RESP_ENTRIES;
1597 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1598 AAC_HOST_NORM_RESP_ENTRIES;
1599 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1600 AAC_HOST_HIGH_RESP_ENTRIES;
1601 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1602 AAC_HOST_HIGH_RESP_ENTRIES;
1603 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1604 AAC_ADAP_NORM_RESP_ENTRIES;
1605 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1606 AAC_ADAP_NORM_RESP_ENTRIES;
1607 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1608 AAC_ADAP_HIGH_RESP_ENTRIES;
1609 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1610 AAC_ADAP_HIGH_RESP_ENTRIES;
1611 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1612 &sc->aac_queues->qt_HostNormCmdQueue[0];
1613 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1614 &sc->aac_queues->qt_HostHighCmdQueue[0];
1615 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1616 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1617 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1618 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1619 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1620 &sc->aac_queues->qt_HostNormRespQueue[0];
1621 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1622 &sc->aac_queues->qt_HostHighRespQueue[0];
1623 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1624 &sc->aac_queues->qt_AdapNormRespQueue[0];
1625 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1626 &sc->aac_queues->qt_AdapHighRespQueue[0];
1629 * Do controller-type-specific initialisation
1631 switch (sc->aac_hwif) {
1632 case AAC_HWIF_I960RX:
1633 AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1638 * Give the init structure to the controller.
1640 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1641 sc->aac_common_busaddr +
1642 offsetof(struct aac_common, ac_init), 0, 0, 0,
1644 device_printf(sc->aac_dev,
1645 "error establishing init structure\n");
1656 * Send a synchronous command to the controller and wait for a result.
1659 aac_sync_command(struct aac_softc *sc, u_int32_t command,
1660 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1668 /* populate the mailbox */
1669 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1671 /* ensure the sync command doorbell flag is cleared */
1672 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1674 /* then set it to signal the adapter */
1675 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1677 /* spin waiting for the command to complete */
1680 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1681 debug(1, "timed out");
1684 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1686 /* clear the completion flag */
1687 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1689 /* get the command status */
1690 status = AAC_GET_MAILBOX(sc, 0);
1697 * Grab the sync fib area.
1700 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags)
1704 * If the force flag is set, the system is shutting down, or in
1705 * trouble. Ignore the mutex.
1707 if (!(flags & AAC_SYNC_LOCK_FORCE))
1708 AAC_LOCK_ACQUIRE(&sc->aac_sync_lock);
1710 *fib = &sc->aac_common->ac_sync_fib;
1716 * Release the sync fib area.
1719 aac_release_sync_fib(struct aac_softc *sc)
1722 AAC_LOCK_RELEASE(&sc->aac_sync_lock);
1726 * Send a synchronous FIB to the controller and wait for a result.
1729 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1730 struct aac_fib *fib, u_int16_t datasize)
1734 if (datasize > AAC_FIB_DATASIZE)
1738 * Set up the sync FIB
1740 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
1741 AAC_FIBSTATE_INITIALISED |
1743 fib->Header.XferState |= xferstate;
1744 fib->Header.Command = command;
1745 fib->Header.StructType = AAC_FIBTYPE_TFIB;
1746 fib->Header.Size = sizeof(struct aac_fib) + datasize;
1747 fib->Header.SenderSize = sizeof(struct aac_fib);
1748 fib->Header.SenderFibAddress = (u_int32_t)fib;
1749 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1750 offsetof(struct aac_common,
1754 * Give the FIB to the controller, wait for a response.
1756 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
1757 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
1758 debug(2, "IO error");
1766 * Adapter-space FIB queue manipulation
1768 * Note that the queue implementation here is a little funky; neither the PI or
1769 * CI will ever be zero. This behaviour is a controller feature.
1775 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1776 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
1777 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1778 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1779 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1780 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
1781 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1782 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1786 * Atomically insert an entry into the nominated queue, returns 0 on success or
1787 * EBUSY if the queue is full.
1789 * Note: it would be more efficient to defer notifying the controller in
1790 * the case where we may be inserting several entries in rapid succession,
1791 * but implementing this usefully may be difficult (it would involve a
1792 * separate queue/notify interface).
1795 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
1804 fib_size = cm->cm_fib->Header.Size;
1805 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
1809 /* get the producer/consumer indices */
1810 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1811 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1813 /* wrap the queue? */
1814 if (pi >= aac_qinfo[queue].size)
1817 /* check for queue full */
1818 if ((pi + 1) == ci) {
1823 /* populate queue entry */
1824 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1825 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1827 /* update producer index */
1828 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1831 * To avoid a race with its completion interrupt, place this command on
1832 * the busy queue prior to advertising it to the controller.
1834 aac_enqueue_busy(cm);
1836 /* notify the adapter if we know how */
1837 if (aac_qinfo[queue].notify != 0)
1838 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1848 * Atomically remove one entry from the nominated queue, returns 0 on
1849 * success or ENOENT if the queue is empty.
1852 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1853 struct aac_fib **fib_addr)
1863 /* get the producer/consumer indices */
1864 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1865 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1867 /* check for queue empty */
1877 /* wrap the queue? */
1878 if (ci >= aac_qinfo[queue].size)
1881 /* fetch the entry */
1882 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1883 *fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] +
1887 * Is this a fast response? If it is, update the fib fields in
1888 * local memory so the whole fib doesn't have to be DMA'd back up.
1890 if (*(uintptr_t *)fib_addr & 0x01) {
1891 *(uintptr_t *)fib_addr &= ~0x01;
1892 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
1893 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
1895 /* update consumer index */
1896 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1898 /* if we have made the queue un-full, notify the adapter */
1899 if (notify && (aac_qinfo[queue].notify != 0))
1900 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1909 * Put our response to an Adapter Initialed Fib on the response queue
1912 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
1921 /* Tell the adapter where the FIB is */
1922 fib_size = fib->Header.Size;
1923 fib_addr = fib->Header.SenderFibAddress;
1924 fib->Header.ReceiverFibAddress = fib_addr;
1928 /* get the producer/consumer indices */
1929 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1930 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1932 /* wrap the queue? */
1933 if (pi >= aac_qinfo[queue].size)
1936 /* check for queue full */
1937 if ((pi + 1) == ci) {
1942 /* populate queue entry */
1943 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1944 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1946 /* update producer index */
1947 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1949 /* notify the adapter if we know how */
1950 if (aac_qinfo[queue].notify != 0)
1951 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1961 * Check for commands that have been outstanding for a suspiciously long time,
1962 * and complain about them.
1965 aac_timeout(void *xsc)
1967 struct aac_softc *sc = xsc;
1969 struct aac_command *cm;
1973 /* simulate an interrupt to handle possibly-missed interrupts */
1975 * XXX This was done to work around another bug which has since been
1976 * fixed. It is dangerous anyways because you don't want multiple
1977 * threads in the interrupt handler at the same time! If calling
1978 * is deamed neccesary in the future, proper mutexes must be used.
1984 /* kick the I/O queue to restart it in the case of deadlock */
1989 * traverse the busy command list, bitch about late commands once
1992 deadline = time_second - AAC_CMD_TIMEOUT;
1994 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
1995 if ((cm->cm_timestamp < deadline)
1996 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
1997 cm->cm_flags |= AAC_CMD_TIMEDOUT;
1998 device_printf(sc->aac_dev,
1999 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2000 cm, (int)(time_second-cm->cm_timestamp));
2001 AAC_PRINT_FIB(sc, cm->cm_fib);
2006 /* reset the timer for next time */
2007 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz,
2012 * Interface Function Vectors
2016 * Read the current firmware status word.
2019 aac_sa_get_fwstatus(struct aac_softc *sc)
2023 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2027 aac_rx_get_fwstatus(struct aac_softc *sc)
2031 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2035 aac_fa_get_fwstatus(struct aac_softc *sc)
2041 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2046 * Notify the controller of a change in a given queue
2050 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2054 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2058 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2062 AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2066 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2070 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2075 * Get the interrupt reason bits
2078 aac_sa_get_istatus(struct aac_softc *sc)
2082 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2086 aac_rx_get_istatus(struct aac_softc *sc)
2090 return(AAC_GETREG4(sc, AAC_RX_ODBR));
2094 aac_fa_get_istatus(struct aac_softc *sc)
2100 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2105 * Clear some interrupt reason bits
2108 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2112 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2116 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2120 AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2124 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2128 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2133 * Populate the mailbox and set the command word
2136 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2137 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2141 AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2142 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2143 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2144 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2145 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2149 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2150 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2154 AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2155 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2156 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2157 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2158 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2162 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2163 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2167 AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2169 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2171 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2173 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2175 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2180 * Fetch the immediate command status word
2183 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2187 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2191 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2195 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2199 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2205 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2210 * Set/clear interrupt masks
2213 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2215 debug(2, "%sable interrupts", enable ? "en" : "dis");
2218 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2220 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2225 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2227 debug(2, "%sable interrupts", enable ? "en" : "dis");
2230 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2232 AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2237 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2239 debug(2, "%sable interrupts", enable ? "en" : "dis");
2242 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2245 AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2251 * Debugging and Diagnostics
2255 * Print some information about the controller.
2258 aac_describe_controller(struct aac_softc *sc)
2260 struct aac_fib *fib;
2261 struct aac_adapter_info *info;
2265 aac_alloc_sync_fib(sc, &fib, 0);
2268 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2269 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2270 aac_release_sync_fib(sc);
2273 info = (struct aac_adapter_info *)&fib->data[0];
2275 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n",
2276 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2277 info->ClockSpeed, info->BufferMem / (1024 * 1024),
2278 aac_describe_code(aac_battery_platform,
2279 info->batteryPlatform));
2281 /* save the kernel revision structure for later use */
2282 sc->aac_revision = info->KernelRevision;
2283 device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2284 info->KernelRevision.external.comp.major,
2285 info->KernelRevision.external.comp.minor,
2286 info->KernelRevision.external.comp.dash,
2287 info->KernelRevision.buildNumber,
2288 (u_int32_t)(info->SerialNumber & 0xffffff));
2290 aac_release_sync_fib(sc);
2292 if (1 || bootverbose) {
2293 device_printf(sc->aac_dev, "Supported Options=%b\n",
2294 sc->supported_options,
2313 * Look up a text description of a numeric error code and return a pointer to
2317 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2321 for (i = 0; table[i].string != NULL; i++)
2322 if (table[i].code == code)
2323 return(table[i].string);
2324 return(table[i + 1].string);
2328 * Management Interface
2332 aac_open(dev_t dev, int flags, int fmt, d_thread_t *td)
2334 struct aac_softc *sc;
2340 /* Check to make sure the device isn't already open */
2341 if (sc->aac_state & AAC_STATE_OPEN) {
2344 sc->aac_state |= AAC_STATE_OPEN;
2350 aac_close(dev_t dev, int flags, int fmt, d_thread_t *td)
2352 struct aac_softc *sc;
2358 /* Mark this unit as no longer open */
2359 sc->aac_state &= ~AAC_STATE_OPEN;
2365 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2367 struct aac_softc *sc = dev->si_drv1;
2373 if (cmd == AACIO_STATS) {
2374 union aac_statrequest *as = (union aac_statrequest *)arg;
2376 switch (as->as_item) {
2382 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2383 sizeof(struct aac_qstat));
2392 arg = *(caddr_t *)arg;
2395 /* AACIO_STATS already handled above */
2396 case FSACTL_SENDFIB:
2397 debug(1, "FSACTL_SENDFIB");
2398 error = aac_ioctl_sendfib(sc, arg);
2400 case FSACTL_AIF_THREAD:
2401 debug(1, "FSACTL_AIF_THREAD");
2404 case FSACTL_OPEN_GET_ADAPTER_FIB:
2405 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2407 * Pass the caller out an AdapterFibContext.
2409 * Note that because we only support one opener, we
2410 * basically ignore this. Set the caller's context to a magic
2411 * number just in case.
2413 * The Linux code hands the driver a pointer into kernel space,
2414 * and then trusts it when the caller hands it back. Aiee!
2415 * Here, we give it the proc pointer of the per-adapter aif
2416 * thread. It's only used as a sanity check in other calls.
2418 i = (int)sc->aifthread;
2419 error = copyout(&i, arg, sizeof(i));
2421 case FSACTL_GET_NEXT_ADAPTER_FIB:
2422 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2423 error = aac_getnext_aif(sc, arg);
2425 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2426 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2427 /* don't do anything here */
2429 case FSACTL_MINIPORT_REV_CHECK:
2430 debug(1, "FSACTL_MINIPORT_REV_CHECK");
2431 error = aac_rev_check(sc, arg);
2433 case FSACTL_QUERY_DISK:
2434 debug(1, "FSACTL_QUERY_DISK");
2435 error = aac_query_disk(sc, arg);
2437 case FSACTL_DELETE_DISK:
2439 * We don't trust the underland to tell us when to delete a
2440 * container, rather we rely on an AIF coming from the
2446 debug(1, "unsupported cmd 0x%lx\n", cmd);
2454 aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2456 struct aac_softc *sc;
2462 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2463 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2464 if (sc->aac_aifq_tail != sc->aac_aifq_head)
2465 revents |= poll_events & (POLLIN | POLLRDNORM);
2467 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2470 if (poll_events & (POLLIN | POLLRDNORM))
2471 selrecord(td, &sc->rcv_select);
2478 * Send a FIB supplied from userspace
2481 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2483 struct aac_command *cm;
2493 if (aac_alloc_command(sc, &cm)) {
2499 * Fetch the FIB header, then re-copy to get data as well.
2501 if ((error = copyin(ufib, cm->cm_fib,
2502 sizeof(struct aac_fib_header))) != 0)
2504 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2505 if (size > sizeof(struct aac_fib)) {
2506 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
2507 size, sizeof(struct aac_fib));
2508 size = sizeof(struct aac_fib);
2510 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2512 cm->cm_fib->Header.Size = size;
2513 cm->cm_timestamp = time_second;
2516 * Pass the FIB to the controller, wait for it to complete.
2518 if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */
2519 printf("aac_wait_command return %d\n", error);
2524 * Copy the FIB and data back out to the caller.
2526 size = cm->cm_fib->Header.Size;
2527 if (size > sizeof(struct aac_fib)) {
2528 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
2529 size, sizeof(struct aac_fib));
2530 size = sizeof(struct aac_fib);
2532 error = copyout(cm->cm_fib, ufib, size);
2536 aac_release_command(cm);
2542 * Handle an AIF sent to us by the controller; queue it for later reference.
2543 * If the queue fills up, then drop the older entries.
2546 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2548 struct aac_aif_command *aif;
2549 struct aac_container *co, *co_next;
2550 struct aac_mntinfo *mi;
2551 struct aac_mntinforesp *mir = NULL;
2554 int added = 0, i = 0;
2558 aif = (struct aac_aif_command*)&fib->data[0];
2559 aac_print_aif(sc, aif);
2561 /* Is it an event that we should care about? */
2562 switch (aif->command) {
2563 case AifCmdEventNotify:
2564 switch (aif->data.EN.type) {
2565 case AifEnAddContainer:
2566 case AifEnDeleteContainer:
2568 * A container was added or deleted, but the message
2569 * doesn't tell us anything else! Re-enumerate the
2570 * containers and sort things out.
2572 aac_alloc_sync_fib(sc, &fib, 0);
2573 mi = (struct aac_mntinfo *)&fib->data[0];
2576 * Ask the controller for its containers one at
2578 * XXX What if the controller's list changes
2579 * midway through this enumaration?
2580 * XXX This should be done async.
2582 bzero(mi, sizeof(struct aac_mntinfo));
2583 mi->Command = VM_NameServe;
2584 mi->MntType = FT_FILESYS;
2586 rsize = sizeof(mir);
2587 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2588 sizeof(struct aac_mntinfo))) {
2589 debug(2, "Error probing container %d\n",
2593 mir = (struct aac_mntinforesp *)&fib->data[0];
2595 * Check the container against our list.
2596 * co->co_found was already set to 0 in a
2599 if ((mir->Status == ST_OK) &&
2600 (mir->MntTable[0].VolType != CT_NONE)) {
2603 &sc->aac_container_tqh,
2605 if (co->co_mntobj.ObjectId ==
2606 mir->MntTable[0].ObjectId) {
2613 * If the container matched, continue
2622 * This is a new container. Do all the
2623 * appropriate things to set it up. */
2624 aac_add_container(sc, mir, 1);
2628 } while ((i < mir->MntRespCount) &&
2629 (i < AAC_MAX_CONTAINERS));
2630 aac_release_sync_fib(sc);
2633 * Go through our list of containers and see which ones
2634 * were not marked 'found'. Since the controller didn't
2635 * list them they must have been deleted. Do the
2636 * appropriate steps to destroy the device. Also reset
2637 * the co->co_found field.
2639 co = TAILQ_FIRST(&sc->aac_container_tqh);
2640 while (co != NULL) {
2641 if (co->co_found == 0) {
2642 device_delete_child(sc->aac_dev,
2644 co_next = TAILQ_NEXT(co, co_link);
2645 AAC_LOCK_ACQUIRE(&sc->
2646 aac_container_lock);
2647 TAILQ_REMOVE(&sc->aac_container_tqh, co,
2649 AAC_LOCK_RELEASE(&sc->
2650 aac_container_lock);
2655 co = TAILQ_NEXT(co, co_link);
2659 /* Attach the newly created containers */
2661 bus_generic_attach(sc->aac_dev);
2673 /* Copy the AIF data to the AIF queue for ioctl retrieval */
2674 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2675 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2676 if (next != sc->aac_aifq_tail) {
2677 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2678 sc->aac_aifq_head = next;
2680 /* On the off chance that someone is sleeping for an aif... */
2681 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2682 wakeup(sc->aac_aifq);
2683 /* token may have been lost */
2684 /* Wakeup any poll()ers */
2685 selwakeup(&sc->rcv_select);
2686 /* token may have been lost */
2688 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2694 * Return the Revision of the driver to userspace and check to see if the
2695 * userspace app is possibly compatible. This is extremely bogus since
2696 * our driver doesn't follow Adaptec's versioning system. Cheat by just
2697 * returning what the card reported.
2700 aac_rev_check(struct aac_softc *sc, caddr_t udata)
2702 struct aac_rev_check rev_check;
2703 struct aac_rev_check_resp rev_check_resp;
2709 * Copyin the revision struct from userspace
2711 if ((error = copyin(udata, (caddr_t)&rev_check,
2712 sizeof(struct aac_rev_check))) != 0) {
2716 debug(2, "Userland revision= %d\n",
2717 rev_check.callingRevision.buildNumber);
2720 * Doctor up the response struct.
2722 rev_check_resp.possiblyCompatible = 1;
2723 rev_check_resp.adapterSWRevision.external.ul =
2724 sc->aac_revision.external.ul;
2725 rev_check_resp.adapterSWRevision.buildNumber =
2726 sc->aac_revision.buildNumber;
2728 return(copyout((caddr_t)&rev_check_resp, udata,
2729 sizeof(struct aac_rev_check_resp)));
2733 * Pass the caller the next AIF in their queue
2736 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2738 struct get_adapter_fib_ioctl agf;
2743 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2746 * Check the magic number that we gave the caller.
2748 if (agf.AdapterFibContext != (int)sc->aifthread) {
2753 error = aac_return_aif(sc, agf.AifFib);
2755 if ((error == EAGAIN) && (agf.Wait)) {
2756 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2757 while (error == EAGAIN) {
2758 error = tsleep(sc->aac_aifq,
2759 PCATCH, "aacaif", 0);
2761 error = aac_return_aif(sc,
2764 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2773 * Hand the next AIF off the top of the queue out to userspace.
2775 * YYY token could be lost during copyout
2778 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2784 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2785 if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2788 error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2789 sizeof(struct aac_aif_command));
2791 printf("aac_return_aif: copyout returned %d\n", error);
2793 sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) %
2796 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2801 * Give the userland some information about the container. The AAC arch
2802 * expects the driver to be a SCSI passthrough type driver, so it expects
2803 * the containers to have b:t:l numbers. Fake it.
2806 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2808 struct aac_query_disk query_disk;
2809 struct aac_container *co;
2810 struct aac_disk *disk;
2817 error = copyin(uptr, (caddr_t)&query_disk,
2818 sizeof(struct aac_query_disk));
2822 id = query_disk.ContainerNumber;
2826 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2827 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2828 if (co->co_mntobj.ObjectId == id)
2833 query_disk.Valid = 0;
2834 query_disk.Locked = 0;
2835 query_disk.Deleted = 1; /* XXX is this right? */
2837 disk = device_get_softc(co->co_disk);
2838 query_disk.Valid = 1;
2840 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2841 query_disk.Deleted = 0;
2842 query_disk.Bus = device_get_unit(sc->aac_dev);
2843 query_disk.Target = disk->unit;
2845 query_disk.UnMapped = 0;
2846 bcopy(disk->ad_dev_t->si_name,
2847 &query_disk.diskDeviceName[0], 10);
2849 AAC_LOCK_RELEASE(&sc->aac_container_lock);
2851 error = copyout((caddr_t)&query_disk, uptr,
2852 sizeof(struct aac_query_disk));
2858 aac_get_bus_info(struct aac_softc *sc)
2860 struct aac_fib *fib;
2861 struct aac_ctcfg *c_cmd;
2862 struct aac_ctcfg_resp *c_resp;
2863 struct aac_vmioctl *vmi;
2864 struct aac_vmi_businf_resp *vmi_resp;
2865 struct aac_getbusinf businfo;
2866 struct aac_cam_inf *caminf;
2868 int i, found, error;
2870 aac_alloc_sync_fib(sc, &fib, 0);
2871 c_cmd = (struct aac_ctcfg *)&fib->data[0];
2872 bzero(c_cmd, sizeof(struct aac_ctcfg));
2874 c_cmd->Command = VM_ContainerConfig;
2875 c_cmd->cmd = CT_GET_SCSI_METHOD;
2878 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2879 sizeof(struct aac_ctcfg));
2881 device_printf(sc->aac_dev, "Error %d sending "
2882 "VM_ContainerConfig command\n", error);
2883 aac_release_sync_fib(sc);
2887 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2888 if (c_resp->Status != ST_OK) {
2889 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2891 aac_release_sync_fib(sc);
2895 sc->scsi_method_id = c_resp->param;
2897 vmi = (struct aac_vmioctl *)&fib->data[0];
2898 bzero(vmi, sizeof(struct aac_vmioctl));
2900 vmi->Command = VM_Ioctl;
2901 vmi->ObjType = FT_DRIVE;
2902 vmi->MethId = sc->scsi_method_id;
2904 vmi->IoctlCmd = GetBusInfo;
2906 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2907 sizeof(struct aac_vmioctl));
2909 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2911 aac_release_sync_fib(sc);
2915 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2916 if (vmi_resp->Status != ST_OK) {
2917 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
2918 aac_release_sync_fib(sc);
2922 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2923 aac_release_sync_fib(sc);
2926 for (i = 0; i < businfo.BusCount; i++) {
2927 if (businfo.BusValid[i] != AAC_BUS_VALID)
2930 MALLOC(caminf, struct aac_cam_inf *,
2931 sizeof(struct aac_cam_inf), M_AACBUF, M_INTWAIT | M_ZERO);
2933 child = device_add_child(sc->aac_dev, "aacp", -1);
2934 if (child == NULL) {
2935 device_printf(sc->aac_dev, "device_add_child failed\n");
2939 caminf->TargetsPerBus = businfo.TargetsPerBus;
2940 caminf->BusNumber = i;
2941 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2942 caminf->aac_sc = sc;
2944 device_set_ivars(child, caminf);
2945 device_set_desc(child, "SCSI Passthrough Bus");
2951 bus_generic_attach(sc->aac_dev);