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.21 2006/03/24 18:35:32 dillon 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 sc->aac_ich.ich_desc = "aac";
291 if (config_intrhook_establish(&sc->aac_ich) != 0) {
292 device_printf(sc->aac_dev,
293 "can't establish configuration hook\n");
298 * Make the control device.
300 unit = device_get_unit(sc->aac_dev);
301 cdevsw_add(&aac_cdevsw, -1, unit);
302 sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
304 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
305 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
306 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
308 sc->aac_dev_t->si_drv1 = sc;
309 reference_dev(sc->aac_dev_t);
311 /* Create the AIF thread */
312 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
313 if (kthread_create((void(*)(void *))aac_host_command, sc,
314 &sc->aifthread, 0, "aac%daif", unit))
316 if (kthread_create((void(*)(void *))aac_host_command, sc,
317 &sc->aifthread, "aac%daif", unit))
319 panic("Could not create AIF thread\n");
321 /* Register the shutdown method to only be called post-dump */
322 if ((EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown, sc->aac_dev,
323 SHUTDOWN_PRI_DEFAULT)) == NULL)
324 device_printf(sc->aac_dev, "shutdown event registration failed\n");
326 /* Register with CAM for the non-DASD devices */
327 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0)
328 aac_get_bus_info(sc);
334 * Probe for containers, create disks.
337 aac_startup(void *arg)
339 struct aac_softc *sc;
341 struct aac_mntinfo *mi;
342 struct aac_mntinforesp *mir = NULL;
347 sc = (struct aac_softc *)arg;
349 /* disconnect ourselves from the intrhook chain */
350 config_intrhook_disestablish(&sc->aac_ich);
352 aac_alloc_sync_fib(sc, &fib, 0);
353 mi = (struct aac_mntinfo *)&fib->data[0];
355 /* loop over possible containers */
357 /* request information on this container */
358 bzero(mi, sizeof(struct aac_mntinfo));
359 mi->Command = VM_NameServe;
360 mi->MntType = FT_FILESYS;
362 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
363 sizeof(struct aac_mntinfo))) {
364 debug(2, "error probing container %d", i);
368 mir = (struct aac_mntinforesp *)&fib->data[0];
369 aac_add_container(sc, mir, 0);
371 } while ((i < mir->MntRespCount) && (i < AAC_MAX_CONTAINERS));
373 aac_release_sync_fib(sc);
375 /* poke the bus to actually attach the child devices */
376 if (bus_generic_attach(sc->aac_dev))
377 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
379 /* mark the controller up */
380 sc->aac_state &= ~AAC_STATE_SUSPEND;
382 /* enable interrupts now */
383 AAC_UNMASK_INTERRUPTS(sc);
385 /* enable the timeout watchdog */
386 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz,
391 * Create a device to respresent a new container
394 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
396 struct aac_container *co;
400 * Check container volume type for validity. Note that many of
401 * the possible types may never show up.
403 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
404 MALLOC(co, struct aac_container *, sizeof *co, M_AACBUF,
406 debug(1, "id %x name '%.16s' size %u type %d",
407 mir->MntTable[0].ObjectId,
408 mir->MntTable[0].FileSystemName,
409 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
411 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
412 device_printf(sc->aac_dev, "device_add_child failed\n");
414 device_set_ivars(child, co);
415 device_set_desc(child, aac_describe_code(aac_container_types,
416 mir->MntTable[0].VolType));
419 bcopy(&mir->MntTable[0], &co->co_mntobj,
420 sizeof(struct aac_mntobj));
421 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
422 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
423 AAC_LOCK_RELEASE(&sc->aac_container_lock);
428 * Free all of the resources associated with (sc)
430 * Should not be called if the controller is active.
433 aac_free(struct aac_softc *sc)
437 /* remove the control device */
438 if (sc->aac_dev_t != NULL)
439 destroy_dev(sc->aac_dev_t);
441 /* throw away any FIB buffers, discard the FIB DMA tag */
442 if (sc->aac_fibs != NULL)
443 aac_free_commands(sc);
444 if (sc->aac_fib_dmat)
445 bus_dma_tag_destroy(sc->aac_fib_dmat);
447 /* destroy the common area */
448 if (sc->aac_common) {
449 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
450 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
451 sc->aac_common_dmamap);
453 if (sc->aac_common_dmat)
454 bus_dma_tag_destroy(sc->aac_common_dmat);
456 /* disconnect the interrupt handler */
458 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
459 if (sc->aac_irq != NULL)
460 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
463 /* destroy data-transfer DMA tag */
464 if (sc->aac_buffer_dmat)
465 bus_dma_tag_destroy(sc->aac_buffer_dmat);
467 /* destroy the parent DMA tag */
468 if (sc->aac_parent_dmat)
469 bus_dma_tag_destroy(sc->aac_parent_dmat);
471 /* release the register window mapping */
472 if (sc->aac_regs_resource != NULL) {
473 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
474 sc->aac_regs_rid, sc->aac_regs_resource);
476 cdevsw_remove(&aac_cdevsw, -1, device_get_unit(sc->aac_dev));
480 * Disconnect from the controller completely, in preparation for unload.
483 aac_detach(device_t dev)
485 struct aac_softc *sc;
492 sc = device_get_softc(dev);
494 callout_stop(&sc->aac_watchdog);
496 if (sc->aac_state & AAC_STATE_OPEN)
500 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
501 sc->aifflags |= AAC_AIFFLAGS_EXIT;
502 wakeup(sc->aifthread);
503 tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz);
506 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
507 panic("Cannot shutdown AIF thread\n");
509 if ((error = aac_shutdown(dev)))
521 * Bring the controller down to a dormant state and detach all child devices.
523 * This function is called before detach or system shutdown.
525 * Note that we can assume that the bioq on the controller is empty, as we won't
526 * allow shutdown if any device is open.
529 aac_shutdown(device_t dev)
531 struct aac_softc *sc;
533 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;
591 sc = device_get_softc(dev);
595 sc->aac_state |= AAC_STATE_SUSPEND;
597 AAC_MASK_INTERRUPTS(sc);
603 * Bring the controller back to a state ready for operation.
606 aac_resume(device_t dev)
608 struct aac_softc *sc;
612 sc = device_get_softc(dev);
614 sc->aac_state &= ~AAC_STATE_SUSPEND;
615 AAC_UNMASK_INTERRUPTS(sc);
625 struct aac_softc *sc;
627 u_int32_t *resp_queue;
631 sc = (struct aac_softc *)arg;
634 * Optimize the common case of adapter response interrupts.
635 * We must read from the card prior to processing the responses
636 * to ensure the clear is flushed prior to accessing the queues.
637 * Reading the queues from local memory might save us a PCI read.
639 resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE];
640 if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX])
641 reason = AAC_DB_RESPONSE_READY;
643 reason = AAC_GET_ISTATUS(sc);
644 AAC_CLEAR_ISTATUS(sc, reason);
645 (void)AAC_GET_ISTATUS(sc);
647 /* It's not ok to return here because of races with the previous step */
648 if (reason & AAC_DB_RESPONSE_READY)
649 aac_host_response(sc);
651 /* controller wants to talk to the log */
652 if (reason & AAC_DB_PRINTF)
653 aac_print_printf(sc);
655 /* controller has a message for us? */
656 if (reason & AAC_DB_COMMAND_READY) {
657 /* XXX What happens if the thread is already awake? */
658 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
659 sc->aifflags |= AAC_AIFFLAGS_PENDING;
660 wakeup(sc->aifthread);
670 * Start as much queued I/O as possible on the controller
673 aac_startio(struct aac_softc *sc)
675 struct aac_command *cm;
681 * Try to get a command that's been put off for lack of
684 cm = aac_dequeue_ready(sc);
687 * Try to build a command off the bio queue (ignore error
691 aac_bio_command(sc, &cm);
697 /* try to give the command to the controller */
698 if (aac_start(cm) == EBUSY) {
699 /* put it on the ready queue for later */
700 aac_requeue_ready(cm);
707 * Deliver a command to the controller; allocate controller resources at the
708 * last moment when possible.
711 aac_start(struct aac_command *cm)
713 struct aac_softc *sc;
720 /* get the command mapped */
723 /* fix up the address values in the FIB */
724 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
725 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
727 /* save a pointer to the command for speedy reverse-lookup */
728 cm->cm_fib->Header.SenderData = (u_int32_t)cm; /* XXX 64-bit physical
730 /* put the FIB on the outbound queue */
731 error = aac_enqueue_fib(sc, cm->cm_queue, cm);
736 * Handle notification of one or more FIBs coming from the controller.
739 aac_host_command(struct aac_softc *sc)
747 sc->aifflags |= AAC_AIFFLAGS_RUNNING;
749 while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) {
750 if (!(sc->aifflags & AAC_AIFFLAGS_PENDING))
751 tsleep(sc->aifthread, 0, "aifthd", 15 * hz);
753 sc->aifflags &= ~AAC_AIFFLAGS_PENDING;
755 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
757 break; /* nothing to do */
759 AAC_PRINT_FIB(sc, fib);
761 switch (fib->Header.Command) {
763 aac_handle_aif(sc, fib);
766 device_printf(sc->aac_dev, "unknown command "
767 "from controller\n");
771 /* Return the AIF to the controller. */
772 if ((fib->Header.XferState == 0) ||
773 (fib->Header.StructType != AAC_FIBTYPE_TFIB))
776 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
777 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
778 *(AAC_FSAStatus*)fib->data = ST_OK;
780 /* XXX Compute the Size field? */
781 size = fib->Header.Size;
782 if (size > sizeof(struct aac_fib)) {
783 size = sizeof(struct aac_fib);
784 fib->Header.Size = size;
787 * Since we did not generate this command, it
788 * cannot go through the normal
789 * enqueue->startio chain.
791 aac_enqueue_response(sc,
792 AAC_ADAP_NORM_RESP_QUEUE,
797 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
800 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
807 * Handle notification of one or more FIBs completed by the controller
810 aac_host_response(struct aac_softc *sc)
812 struct aac_command *cm;
819 /* look for completed FIBs on our queue */
820 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
822 break; /* nothing to do */
824 /* get the command, unmap and queue for later processing */
825 cm = (struct aac_command *)fib->Header.SenderData;
827 AAC_PRINT_FIB(sc, fib);
830 aac_unmap_command(cm); /* XXX defer? */
831 aac_enqueue_complete(cm);
835 /* handle completion processing */
836 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
837 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
844 * Process completed commands.
847 aac_complete(void *context, int pending)
849 struct aac_softc *sc;
850 struct aac_command *cm;
854 sc = (struct aac_softc *)context;
856 /* pull completed commands off the queue */
858 cm = aac_dequeue_complete(sc);
861 cm->cm_flags |= AAC_CMD_COMPLETED;
863 /* is there a completion handler? */
864 if (cm->cm_complete != NULL) {
867 /* assume that someone is sleeping on this command */
872 /* see if we can start some more I/O */
877 * Handle a bio submitted from a disk device.
880 aac_submit_bio(struct aac_disk *ad, struct bio *bio)
882 struct aac_softc *sc;
886 bio->bio_driver_info = ad;
887 sc = ad->ad_controller;
889 /* queue the BIO and try to get some work done */
890 aac_enqueue_bio(sc, bio);
895 * Get a bio and build a command to go with it.
898 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
900 struct aac_command *cm;
902 struct aac_blockread *br;
903 struct aac_blockwrite *bw;
910 /* get the resources we will need */
912 if ((bio = aac_dequeue_bio(sc)) == NULL)
914 if (aac_alloc_command(sc, &cm)) /* get a command */
917 /* fill out the command */
919 cm->cm_data = (void *)bp->b_data;
920 cm->cm_datalen = bp->b_bcount;
921 cm->cm_complete = aac_bio_complete;
922 cm->cm_private = bio;
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 *)bio->bio_driver_info;
942 if (bp->b_flags & B_READ) {
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 = bio->bio_offset / AAC_BLOCK_SIZE;
947 br->ByteCount = bp->b_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 = bio->bio_offset / AAC_BLOCK_SIZE;
956 bw->ByteCount = bp->b_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, bio);
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;
985 AAC_FSAStatus status;
987 /* fetch relevant status and then release the command */
988 bio = (struct bio *)cm->cm_private;
990 if (bp->b_flags & B_READ) {
991 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
992 status = brr->Status;
994 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
995 status = bwr->Status;
997 aac_release_command(cm);
999 /* fix up the bio based on status */
1000 if (status == ST_OK) {
1005 bp->b_flags |= B_ERROR;
1006 /* pass an error string out to the disk layer */
1007 code = aac_describe_code(aac_command_status_table, status);
1009 aac_biodone(bio, code);
1013 * Dump a block of data to the controller. If the queue is full, tell the
1014 * caller to hold off and wait for the queue to drain.
1017 aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba, void *data, int dumppages)
1019 struct aac_softc *sc;
1020 struct aac_command *cm;
1021 struct aac_fib *fib;
1022 struct aac_blockwrite *bw;
1024 sc = ad->ad_controller;
1027 if (aac_alloc_command(sc, &cm))
1030 /* fill out the command */
1032 cm->cm_datalen = dumppages * PAGE_SIZE;
1033 cm->cm_complete = NULL;
1034 cm->cm_private = NULL;
1035 cm->cm_timestamp = time_second;
1036 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1040 fib->Header.XferState =
1041 AAC_FIBSTATE_HOSTOWNED |
1042 AAC_FIBSTATE_INITIALISED |
1043 AAC_FIBSTATE_FROMHOST |
1044 AAC_FIBSTATE_REXPECTED |
1046 fib->Header.Command = ContainerCommand;
1047 fib->Header.Size = sizeof(struct aac_fib_header);
1049 bw = (struct aac_blockwrite *)&fib->data[0];
1050 bw->Command = VM_CtBlockWrite;
1051 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1052 bw->BlockNumber = lba;
1053 bw->ByteCount = dumppages * PAGE_SIZE;
1054 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
1055 fib->Header.Size += sizeof(struct aac_blockwrite);
1056 cm->cm_flags |= AAC_CMD_DATAOUT;
1057 cm->cm_sgtable = &bw->SgMap;
1059 return (aac_start(cm));
1063 * Wait for the card's queue to drain when dumping. Also check for monitor
1067 aac_dump_complete(struct aac_softc *sc)
1069 struct aac_fib *fib;
1070 struct aac_command *cm;
1072 u_int32_t pi, ci, fib_size;
1075 reason = AAC_GET_ISTATUS(sc);
1076 if (reason & AAC_DB_RESPONSE_READY) {
1077 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
1079 if (aac_dequeue_fib(sc,
1080 AAC_HOST_NORM_RESP_QUEUE,
1083 cm = (struct aac_command *)
1084 fib->Header.SenderData;
1086 AAC_PRINT_FIB(sc, fib);
1088 aac_remove_busy(cm);
1089 aac_unmap_command(cm);
1090 aac_enqueue_complete(cm);
1091 aac_release_command(cm);
1095 if (reason & AAC_DB_PRINTF) {
1096 AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
1097 aac_print_printf(sc);
1099 pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1100 AAC_PRODUCER_INDEX];
1101 ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1102 AAC_CONSUMER_INDEX];
1109 * Submit a command to the controller, return when it completes.
1110 * XXX This is very dangerous! If the card has gone out to lunch, we could
1111 * be stuck here forever. At the same time, signals are not caught
1112 * because there is a risk that a signal could wakeup the tsleep before
1113 * the card has a chance to complete the command. The passed in timeout
1114 * is ignored for the same reason. Since there is no way to cancel a
1115 * command in progress, we should probably create a 'dead' queue where
1116 * commands go that have been interrupted/timed-out/etc, that keeps them
1117 * out of the free pool. That way, if the card is just slow, it won't
1118 * spam the memory of a command that has been recycled.
1121 aac_wait_command(struct aac_command *cm, int timeout)
1127 /* Put the command on the ready queue and get things going */
1128 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1129 aac_enqueue_ready(cm);
1130 aac_startio(cm->cm_sc);
1132 while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
1133 error = tsleep(cm, 0, "aacwait", 0);
1140 *Command Buffer Management
1144 * Allocate a command.
1147 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1149 struct aac_command *cm;
1153 if ((cm = aac_dequeue_free(sc)) == NULL)
1161 * Release a command back to the freelist.
1164 aac_release_command(struct aac_command *cm)
1168 /* (re)initialise the command/FIB */
1169 cm->cm_sgtable = NULL;
1171 cm->cm_complete = NULL;
1172 cm->cm_private = NULL;
1173 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1174 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1175 cm->cm_fib->Header.Flags = 0;
1176 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1179 * These are duplicated in aac_start to cover the case where an
1180 * intermediate stage may have destroyed them. They're left
1181 * initialised here for debugging purposes only.
1183 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
1184 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1185 cm->cm_fib->Header.SenderData = 0;
1187 aac_enqueue_free(cm);
1191 * Map helper for command/FIB allocation.
1194 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1196 struct aac_softc *sc;
1198 sc = (struct aac_softc *)arg;
1202 sc->aac_fibphys = segs[0].ds_addr;
1206 * Allocate and initialise commands/FIBs for this adapter.
1209 aac_alloc_commands(struct aac_softc *sc)
1211 struct aac_command *cm;
1216 /* allocate the FIBs in DMAable memory and load them */
1217 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs,
1218 BUS_DMA_NOWAIT, &sc->aac_fibmap)) {
1222 bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs,
1223 AAC_FIB_COUNT * sizeof(struct aac_fib),
1224 aac_map_command_helper, sc, 0);
1226 /* initialise constant fields in the command structure */
1227 bzero(sc->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib));
1228 for (i = 0; i < AAC_FIB_COUNT; i++) {
1229 cm = &sc->aac_command[i];
1231 cm->cm_fib = sc->aac_fibs + i;
1232 cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib));
1234 if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap))
1235 aac_release_command(cm);
1241 * Free FIBs owned by this adapter.
1244 aac_free_commands(struct aac_softc *sc)
1250 for (i = 0; i < AAC_FIB_COUNT; i++)
1251 bus_dmamap_destroy(sc->aac_buffer_dmat,
1252 sc->aac_command[i].cm_datamap);
1254 bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap);
1255 bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap);
1259 * Command-mapping helper function - populate this command's s/g table.
1262 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1264 struct aac_command *cm;
1265 struct aac_fib *fib;
1266 struct aac_sg_table *sg;
1271 cm = (struct aac_command *)arg;
1274 /* find the s/g table */
1275 sg = cm->cm_sgtable;
1277 /* copy into the FIB */
1280 for (i = 0; i < nseg; i++) {
1281 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1282 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1284 /* update the FIB size for the s/g count */
1285 fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1291 * Map a command into controller-visible space.
1294 aac_map_command(struct aac_command *cm)
1296 struct aac_softc *sc;
1302 /* don't map more than once */
1303 if (cm->cm_flags & AAC_CMD_MAPPED)
1306 if (cm->cm_datalen != 0) {
1307 bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
1308 cm->cm_data, cm->cm_datalen,
1309 aac_map_command_sg, cm, 0);
1311 if (cm->cm_flags & AAC_CMD_DATAIN)
1312 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1313 BUS_DMASYNC_PREREAD);
1314 if (cm->cm_flags & AAC_CMD_DATAOUT)
1315 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1316 BUS_DMASYNC_PREWRITE);
1318 cm->cm_flags |= AAC_CMD_MAPPED;
1322 * Unmap a command from controller-visible space.
1325 aac_unmap_command(struct aac_command *cm)
1327 struct aac_softc *sc;
1333 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1336 if (cm->cm_datalen != 0) {
1337 if (cm->cm_flags & AAC_CMD_DATAIN)
1338 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1339 BUS_DMASYNC_POSTREAD);
1340 if (cm->cm_flags & AAC_CMD_DATAOUT)
1341 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1342 BUS_DMASYNC_POSTWRITE);
1344 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1346 cm->cm_flags &= ~AAC_CMD_MAPPED;
1350 * Hardware Interface
1354 * Initialise the adapter.
1357 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1359 struct aac_softc *sc;
1363 sc = (struct aac_softc *)arg;
1365 sc->aac_common_busaddr = segs[0].ds_addr;
1369 aac_check_firmware(struct aac_softc *sc)
1371 u_int32_t major, minor, options;
1376 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1377 * firmware version 1.x are not compatible with this driver.
1379 if (sc->flags & AAC_FLAGS_PERC2QC) {
1380 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1382 device_printf(sc->aac_dev,
1383 "Error reading firmware version\n");
1387 /* These numbers are stored as ASCII! */
1388 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1389 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1391 device_printf(sc->aac_dev,
1392 "Firmware version %d.%d is not supported.\n",
1399 * Retrieve the capabilities/supported options word so we know what
1400 * work-arounds to enable.
1402 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) {
1403 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
1406 options = AAC_GET_MAILBOX(sc, 1);
1407 sc->supported_options = options;
1409 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1410 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1411 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1412 if (options & AAC_SUPPORTED_NONDASD)
1413 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1419 aac_init(struct aac_softc *sc)
1421 struct aac_adapter_init *ip;
1430 * First wait for the adapter to come ready.
1434 code = AAC_GET_FWSTATUS(sc);
1435 if (code & AAC_SELF_TEST_FAILED) {
1436 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1439 if (code & AAC_KERNEL_PANIC) {
1440 device_printf(sc->aac_dev,
1441 "FATAL: controller kernel panic\n");
1444 if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1445 device_printf(sc->aac_dev,
1446 "FATAL: controller not coming ready, "
1447 "status %x\n", code);
1450 } while (!(code & AAC_UP_AND_RUNNING));
1454 * Create DMA tag for mapping buffers into controller-addressable space.
1456 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1457 1, 0, /* algnmnt, boundary */
1458 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1459 BUS_SPACE_MAXADDR, /* highaddr */
1460 NULL, NULL, /* filter, filterarg */
1461 MAXBSIZE, /* maxsize */
1462 AAC_MAXSGENTRIES, /* nsegments */
1463 MAXBSIZE, /* maxsegsize */
1464 BUS_DMA_ALLOCNOW, /* flags */
1465 &sc->aac_buffer_dmat)) {
1466 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1471 * Create DMA tag for mapping FIBs into controller-addressable space..
1473 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1474 1, 0, /* algnmnt, boundary */
1475 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1476 BUS_SPACE_MAXADDR_32BIT :
1477 0x7fffffff, /* lowaddr */
1478 BUS_SPACE_MAXADDR, /* highaddr */
1479 NULL, NULL, /* filter, filterarg */
1481 sizeof(struct aac_fib), /* maxsize */
1484 sizeof(struct aac_fib), /* maxsegsize */
1485 BUS_DMA_ALLOCNOW, /* flags */
1486 &sc->aac_fib_dmat)) {
1487 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
1492 * Create DMA tag for the common structure and allocate it.
1494 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1495 1, 0, /* algnmnt, boundary */
1496 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1497 BUS_SPACE_MAXADDR_32BIT :
1498 0x7fffffff, /* lowaddr */
1499 BUS_SPACE_MAXADDR, /* highaddr */
1500 NULL, NULL, /* filter, filterarg */
1501 8192 + sizeof(struct aac_common), /* maxsize */
1503 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1504 BUS_DMA_ALLOCNOW, /* flags */
1505 &sc->aac_common_dmat)) {
1506 device_printf(sc->aac_dev,
1507 "can't allocate common structure DMA tag\n");
1510 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1511 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1512 device_printf(sc->aac_dev, "can't allocate common structure\n");
1516 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1517 * below address 8192 in physical memory.
1518 * XXX If the padding is not needed, can it be put to use instead
1521 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1522 sc->aac_common, 8192 + sizeof(*sc->aac_common),
1523 aac_common_map, sc, 0);
1525 if (sc->aac_common_busaddr < 8192) {
1527 (struct aac_common *)((uint8_t *)sc->aac_common + 8192);
1528 sc->aac_common_busaddr += 8192;
1530 bzero(sc->aac_common, sizeof(*sc->aac_common));
1532 /* Allocate some FIBs and associated command structs */
1533 if (aac_alloc_commands(sc) != 0)
1537 * Fill in the init structure. This tells the adapter about the
1538 * physical location of various important shared data structures.
1540 ip = &sc->aac_common->ac_init;
1541 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1542 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1544 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1545 offsetof(struct aac_common, ac_fibs);
1546 ip->AdapterFibsVirtualAddress = (aac_phys_addr_t)&sc->aac_common->ac_fibs[0];
1547 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1548 ip->AdapterFibAlign = sizeof(struct aac_fib);
1550 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1551 offsetof(struct aac_common, ac_printf);
1552 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1554 /* The adapter assumes that pages are 4K in size */
1555 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1556 ip->HostElapsedSeconds = time_second; /* reset later if invalid */
1559 * Initialise FIB queues. Note that it appears that the layout of the
1560 * indexes and the segmentation of the entries may be mandated by the
1561 * adapter, which is only told about the base of the queue index fields.
1563 * The initial values of the indices are assumed to inform the adapter
1564 * of the sizes of the respective queues, and theoretically it could
1565 * work out the entire layout of the queue structures from this. We
1566 * take the easy route and just lay this area out like everyone else
1569 * The Linux driver uses a much more complex scheme whereby several
1570 * header records are kept for each queue. We use a couple of generic
1571 * list manipulation functions which 'know' the size of each list by
1572 * virtue of a table.
1574 qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN;
1575 qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN;
1576 sc->aac_queues = (struct aac_queue_table *)qaddr;
1577 ip->CommHeaderAddress = sc->aac_common_busaddr +
1578 ((u_int32_t)sc->aac_queues -
1579 (u_int32_t)sc->aac_common);
1580 bzero(sc->aac_queues, sizeof(struct aac_queue_table));
1582 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1583 AAC_HOST_NORM_CMD_ENTRIES;
1584 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1585 AAC_HOST_NORM_CMD_ENTRIES;
1586 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1587 AAC_HOST_HIGH_CMD_ENTRIES;
1588 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1589 AAC_HOST_HIGH_CMD_ENTRIES;
1590 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1591 AAC_ADAP_NORM_CMD_ENTRIES;
1592 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1593 AAC_ADAP_NORM_CMD_ENTRIES;
1594 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1595 AAC_ADAP_HIGH_CMD_ENTRIES;
1596 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1597 AAC_ADAP_HIGH_CMD_ENTRIES;
1598 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1599 AAC_HOST_NORM_RESP_ENTRIES;
1600 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1601 AAC_HOST_NORM_RESP_ENTRIES;
1602 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1603 AAC_HOST_HIGH_RESP_ENTRIES;
1604 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1605 AAC_HOST_HIGH_RESP_ENTRIES;
1606 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1607 AAC_ADAP_NORM_RESP_ENTRIES;
1608 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1609 AAC_ADAP_NORM_RESP_ENTRIES;
1610 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1611 AAC_ADAP_HIGH_RESP_ENTRIES;
1612 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1613 AAC_ADAP_HIGH_RESP_ENTRIES;
1614 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1615 &sc->aac_queues->qt_HostNormCmdQueue[0];
1616 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1617 &sc->aac_queues->qt_HostHighCmdQueue[0];
1618 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1619 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1620 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1621 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1622 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1623 &sc->aac_queues->qt_HostNormRespQueue[0];
1624 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1625 &sc->aac_queues->qt_HostHighRespQueue[0];
1626 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1627 &sc->aac_queues->qt_AdapNormRespQueue[0];
1628 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1629 &sc->aac_queues->qt_AdapHighRespQueue[0];
1632 * Do controller-type-specific initialisation
1634 switch (sc->aac_hwif) {
1635 case AAC_HWIF_I960RX:
1636 AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1641 * Give the init structure to the controller.
1643 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1644 sc->aac_common_busaddr +
1645 offsetof(struct aac_common, ac_init), 0, 0, 0,
1647 device_printf(sc->aac_dev,
1648 "error establishing init structure\n");
1659 * Send a synchronous command to the controller and wait for a result.
1662 aac_sync_command(struct aac_softc *sc, u_int32_t command,
1663 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1671 /* populate the mailbox */
1672 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1674 /* ensure the sync command doorbell flag is cleared */
1675 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1677 /* then set it to signal the adapter */
1678 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1680 /* spin waiting for the command to complete */
1683 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1684 debug(1, "timed out");
1687 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1689 /* clear the completion flag */
1690 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1692 /* get the command status */
1693 status = AAC_GET_MAILBOX(sc, 0);
1700 * Grab the sync fib area.
1703 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags)
1707 * If the force flag is set, the system is shutting down, or in
1708 * trouble. Ignore the mutex.
1710 if (!(flags & AAC_SYNC_LOCK_FORCE))
1711 AAC_LOCK_ACQUIRE(&sc->aac_sync_lock);
1713 *fib = &sc->aac_common->ac_sync_fib;
1719 * Release the sync fib area.
1722 aac_release_sync_fib(struct aac_softc *sc)
1725 AAC_LOCK_RELEASE(&sc->aac_sync_lock);
1729 * Send a synchronous FIB to the controller and wait for a result.
1732 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1733 struct aac_fib *fib, u_int16_t datasize)
1737 if (datasize > AAC_FIB_DATASIZE)
1741 * Set up the sync FIB
1743 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
1744 AAC_FIBSTATE_INITIALISED |
1746 fib->Header.XferState |= xferstate;
1747 fib->Header.Command = command;
1748 fib->Header.StructType = AAC_FIBTYPE_TFIB;
1749 fib->Header.Size = sizeof(struct aac_fib) + datasize;
1750 fib->Header.SenderSize = sizeof(struct aac_fib);
1751 fib->Header.SenderFibAddress = (u_int32_t)fib;
1752 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1753 offsetof(struct aac_common,
1757 * Give the FIB to the controller, wait for a response.
1759 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
1760 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
1761 debug(2, "IO error");
1769 * Adapter-space FIB queue manipulation
1771 * Note that the queue implementation here is a little funky; neither the PI or
1772 * CI will ever be zero. This behaviour is a controller feature.
1778 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1779 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
1780 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1781 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1782 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1783 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
1784 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1785 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1789 * Atomically insert an entry into the nominated queue, returns 0 on success or
1790 * EBUSY if the queue is full.
1792 * Note: it would be more efficient to defer notifying the controller in
1793 * the case where we may be inserting several entries in rapid succession,
1794 * but implementing this usefully may be difficult (it would involve a
1795 * separate queue/notify interface).
1798 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
1807 fib_size = cm->cm_fib->Header.Size;
1808 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
1812 /* get the producer/consumer indices */
1813 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1814 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1816 /* wrap the queue? */
1817 if (pi >= aac_qinfo[queue].size)
1820 /* check for queue full */
1821 if ((pi + 1) == ci) {
1826 /* populate queue entry */
1827 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1828 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1830 /* update producer index */
1831 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1834 * To avoid a race with its completion interrupt, place this command on
1835 * the busy queue prior to advertising it to the controller.
1837 aac_enqueue_busy(cm);
1839 /* notify the adapter if we know how */
1840 if (aac_qinfo[queue].notify != 0)
1841 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1851 * Atomically remove one entry from the nominated queue, returns 0 on
1852 * success or ENOENT if the queue is empty.
1855 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1856 struct aac_fib **fib_addr)
1866 /* get the producer/consumer indices */
1867 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1868 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1870 /* check for queue empty */
1880 /* wrap the queue? */
1881 if (ci >= aac_qinfo[queue].size)
1884 /* fetch the entry */
1885 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1886 *fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] +
1890 * Is this a fast response? If it is, update the fib fields in
1891 * local memory so the whole fib doesn't have to be DMA'd back up.
1893 if (*(uintptr_t *)fib_addr & 0x01) {
1894 *(uintptr_t *)fib_addr &= ~0x01;
1895 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
1896 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
1898 /* update consumer index */
1899 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1901 /* if we have made the queue un-full, notify the adapter */
1902 if (notify && (aac_qinfo[queue].notify != 0))
1903 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1912 * Put our response to an Adapter Initialed Fib on the response queue
1915 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
1924 /* Tell the adapter where the FIB is */
1925 fib_size = fib->Header.Size;
1926 fib_addr = fib->Header.SenderFibAddress;
1927 fib->Header.ReceiverFibAddress = fib_addr;
1931 /* get the producer/consumer indices */
1932 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1933 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1935 /* wrap the queue? */
1936 if (pi >= aac_qinfo[queue].size)
1939 /* check for queue full */
1940 if ((pi + 1) == ci) {
1945 /* populate queue entry */
1946 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1947 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1949 /* update producer index */
1950 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1952 /* notify the adapter if we know how */
1953 if (aac_qinfo[queue].notify != 0)
1954 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1964 * Check for commands that have been outstanding for a suspiciously long time,
1965 * and complain about them.
1968 aac_timeout(void *xsc)
1970 struct aac_softc *sc = xsc;
1971 struct aac_command *cm;
1975 /* simulate an interrupt to handle possibly-missed interrupts */
1977 * XXX This was done to work around another bug which has since been
1978 * fixed. It is dangerous anyways because you don't want multiple
1979 * threads in the interrupt handler at the same time! If calling
1980 * is deamed neccesary in the future, proper mutexes must be used.
1986 /* kick the I/O queue to restart it in the case of deadlock */
1991 * traverse the busy command list, bitch about late commands once
1994 deadline = time_second - AAC_CMD_TIMEOUT;
1996 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
1997 if ((cm->cm_timestamp < deadline)
1998 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
1999 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2000 device_printf(sc->aac_dev,
2001 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2002 cm, (int)(time_second-cm->cm_timestamp));
2003 AAC_PRINT_FIB(sc, cm->cm_fib);
2008 /* reset the timer for next time */
2009 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz,
2014 * Interface Function Vectors
2018 * Read the current firmware status word.
2021 aac_sa_get_fwstatus(struct aac_softc *sc)
2025 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2029 aac_rx_get_fwstatus(struct aac_softc *sc)
2033 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2037 aac_fa_get_fwstatus(struct aac_softc *sc)
2043 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2048 * Notify the controller of a change in a given queue
2052 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2056 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2060 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2064 AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2068 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2072 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2077 * Get the interrupt reason bits
2080 aac_sa_get_istatus(struct aac_softc *sc)
2084 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2088 aac_rx_get_istatus(struct aac_softc *sc)
2092 return(AAC_GETREG4(sc, AAC_RX_ODBR));
2096 aac_fa_get_istatus(struct aac_softc *sc)
2102 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2107 * Clear some interrupt reason bits
2110 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2114 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2118 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2122 AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2126 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2130 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2135 * Populate the mailbox and set the command word
2138 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2139 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2143 AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2144 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2145 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2146 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2147 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2151 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2152 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2156 AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2157 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2158 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2159 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2160 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2164 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2165 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2169 AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2171 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2173 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2175 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2177 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2182 * Fetch the immediate command status word
2185 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2189 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2193 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2197 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2201 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2207 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2212 * Set/clear interrupt masks
2215 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2217 debug(2, "%sable interrupts", enable ? "en" : "dis");
2220 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2222 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2227 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2229 debug(2, "%sable interrupts", enable ? "en" : "dis");
2232 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2234 AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2239 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2241 debug(2, "%sable interrupts", enable ? "en" : "dis");
2244 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2247 AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2253 * Debugging and Diagnostics
2257 * Print some information about the controller.
2260 aac_describe_controller(struct aac_softc *sc)
2262 struct aac_fib *fib;
2263 struct aac_adapter_info *info;
2267 aac_alloc_sync_fib(sc, &fib, 0);
2270 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2271 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2272 aac_release_sync_fib(sc);
2275 info = (struct aac_adapter_info *)&fib->data[0];
2277 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n",
2278 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2279 info->ClockSpeed, info->BufferMem / (1024 * 1024),
2280 aac_describe_code(aac_battery_platform,
2281 info->batteryPlatform));
2283 /* save the kernel revision structure for later use */
2284 sc->aac_revision = info->KernelRevision;
2285 device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2286 info->KernelRevision.external.comp.major,
2287 info->KernelRevision.external.comp.minor,
2288 info->KernelRevision.external.comp.dash,
2289 info->KernelRevision.buildNumber,
2290 (u_int32_t)(info->SerialNumber & 0xffffff));
2292 aac_release_sync_fib(sc);
2294 if (1 || bootverbose) {
2295 device_printf(sc->aac_dev, "Supported Options=%b\n",
2296 sc->supported_options,
2315 * Look up a text description of a numeric error code and return a pointer to
2319 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2323 for (i = 0; table[i].string != NULL; i++)
2324 if (table[i].code == code)
2325 return(table[i].string);
2326 return(table[i + 1].string);
2330 * Management Interface
2334 aac_open(dev_t dev, int flags, int fmt, d_thread_t *td)
2336 struct aac_softc *sc;
2342 /* Check to make sure the device isn't already open */
2343 if (sc->aac_state & AAC_STATE_OPEN) {
2346 sc->aac_state |= AAC_STATE_OPEN;
2352 aac_close(dev_t dev, int flags, int fmt, d_thread_t *td)
2354 struct aac_softc *sc;
2360 /* Mark this unit as no longer open */
2361 sc->aac_state &= ~AAC_STATE_OPEN;
2367 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2369 struct aac_softc *sc = dev->si_drv1;
2375 if (cmd == AACIO_STATS) {
2376 union aac_statrequest *as = (union aac_statrequest *)arg;
2378 switch (as->as_item) {
2384 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2385 sizeof(struct aac_qstat));
2394 arg = *(caddr_t *)arg;
2397 /* AACIO_STATS already handled above */
2398 case FSACTL_SENDFIB:
2399 debug(1, "FSACTL_SENDFIB");
2400 error = aac_ioctl_sendfib(sc, arg);
2402 case FSACTL_AIF_THREAD:
2403 debug(1, "FSACTL_AIF_THREAD");
2406 case FSACTL_OPEN_GET_ADAPTER_FIB:
2407 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2409 * Pass the caller out an AdapterFibContext.
2411 * Note that because we only support one opener, we
2412 * basically ignore this. Set the caller's context to a magic
2413 * number just in case.
2415 * The Linux code hands the driver a pointer into kernel space,
2416 * and then trusts it when the caller hands it back. Aiee!
2417 * Here, we give it the proc pointer of the per-adapter aif
2418 * thread. It's only used as a sanity check in other calls.
2420 i = (int)sc->aifthread;
2421 error = copyout(&i, arg, sizeof(i));
2423 case FSACTL_GET_NEXT_ADAPTER_FIB:
2424 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2425 error = aac_getnext_aif(sc, arg);
2427 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2428 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2429 /* don't do anything here */
2431 case FSACTL_MINIPORT_REV_CHECK:
2432 debug(1, "FSACTL_MINIPORT_REV_CHECK");
2433 error = aac_rev_check(sc, arg);
2435 case FSACTL_QUERY_DISK:
2436 debug(1, "FSACTL_QUERY_DISK");
2437 error = aac_query_disk(sc, arg);
2439 case FSACTL_DELETE_DISK:
2441 * We don't trust the underland to tell us when to delete a
2442 * container, rather we rely on an AIF coming from the
2448 debug(1, "unsupported cmd 0x%lx\n", cmd);
2456 aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2458 struct aac_softc *sc;
2464 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2465 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2466 if (sc->aac_aifq_tail != sc->aac_aifq_head)
2467 revents |= poll_events & (POLLIN | POLLRDNORM);
2469 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2472 if (poll_events & (POLLIN | POLLRDNORM))
2473 selrecord(td, &sc->rcv_select);
2480 * Send a FIB supplied from userspace
2483 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2485 struct aac_command *cm;
2495 if (aac_alloc_command(sc, &cm)) {
2501 * Fetch the FIB header, then re-copy to get data as well.
2503 if ((error = copyin(ufib, cm->cm_fib,
2504 sizeof(struct aac_fib_header))) != 0)
2506 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2507 if (size > sizeof(struct aac_fib)) {
2508 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
2509 size, sizeof(struct aac_fib));
2510 size = sizeof(struct aac_fib);
2512 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2514 cm->cm_fib->Header.Size = size;
2515 cm->cm_timestamp = time_second;
2518 * Pass the FIB to the controller, wait for it to complete.
2520 if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */
2521 printf("aac_wait_command return %d\n", error);
2526 * Copy the FIB and data back out to the caller.
2528 size = cm->cm_fib->Header.Size;
2529 if (size > sizeof(struct aac_fib)) {
2530 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
2531 size, sizeof(struct aac_fib));
2532 size = sizeof(struct aac_fib);
2534 error = copyout(cm->cm_fib, ufib, size);
2538 aac_release_command(cm);
2544 * Handle an AIF sent to us by the controller; queue it for later reference.
2545 * If the queue fills up, then drop the older entries.
2548 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2550 struct aac_aif_command *aif;
2551 struct aac_container *co, *co_next;
2552 struct aac_mntinfo *mi;
2553 struct aac_mntinforesp *mir = NULL;
2556 int added = 0, i = 0;
2560 aif = (struct aac_aif_command*)&fib->data[0];
2561 aac_print_aif(sc, aif);
2563 /* Is it an event that we should care about? */
2564 switch (aif->command) {
2565 case AifCmdEventNotify:
2566 switch (aif->data.EN.type) {
2567 case AifEnAddContainer:
2568 case AifEnDeleteContainer:
2570 * A container was added or deleted, but the message
2571 * doesn't tell us anything else! Re-enumerate the
2572 * containers and sort things out.
2574 aac_alloc_sync_fib(sc, &fib, 0);
2575 mi = (struct aac_mntinfo *)&fib->data[0];
2578 * Ask the controller for its containers one at
2580 * XXX What if the controller's list changes
2581 * midway through this enumaration?
2582 * XXX This should be done async.
2584 bzero(mi, sizeof(struct aac_mntinfo));
2585 mi->Command = VM_NameServe;
2586 mi->MntType = FT_FILESYS;
2588 rsize = sizeof(mir);
2589 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2590 sizeof(struct aac_mntinfo))) {
2591 debug(2, "Error probing container %d\n",
2595 mir = (struct aac_mntinforesp *)&fib->data[0];
2597 * Check the container against our list.
2598 * co->co_found was already set to 0 in a
2601 if ((mir->Status == ST_OK) &&
2602 (mir->MntTable[0].VolType != CT_NONE)) {
2605 &sc->aac_container_tqh,
2607 if (co->co_mntobj.ObjectId ==
2608 mir->MntTable[0].ObjectId) {
2615 * If the container matched, continue
2624 * This is a new container. Do all the
2625 * appropriate things to set it up. */
2626 aac_add_container(sc, mir, 1);
2630 } while ((i < mir->MntRespCount) &&
2631 (i < AAC_MAX_CONTAINERS));
2632 aac_release_sync_fib(sc);
2635 * Go through our list of containers and see which ones
2636 * were not marked 'found'. Since the controller didn't
2637 * list them they must have been deleted. Do the
2638 * appropriate steps to destroy the device. Also reset
2639 * the co->co_found field.
2641 co = TAILQ_FIRST(&sc->aac_container_tqh);
2642 while (co != NULL) {
2643 if (co->co_found == 0) {
2644 device_delete_child(sc->aac_dev,
2646 co_next = TAILQ_NEXT(co, co_link);
2647 AAC_LOCK_ACQUIRE(&sc->
2648 aac_container_lock);
2649 TAILQ_REMOVE(&sc->aac_container_tqh, co,
2651 AAC_LOCK_RELEASE(&sc->
2652 aac_container_lock);
2657 co = TAILQ_NEXT(co, co_link);
2661 /* Attach the newly created containers */
2663 bus_generic_attach(sc->aac_dev);
2675 /* Copy the AIF data to the AIF queue for ioctl retrieval */
2676 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2677 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2678 if (next != sc->aac_aifq_tail) {
2679 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2680 sc->aac_aifq_head = next;
2682 /* On the off chance that someone is sleeping for an aif... */
2683 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2684 wakeup(sc->aac_aifq);
2685 /* token may have been lost */
2686 /* Wakeup any poll()ers */
2687 selwakeup(&sc->rcv_select);
2688 /* token may have been lost */
2690 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2696 * Return the Revision of the driver to userspace and check to see if the
2697 * userspace app is possibly compatible. This is extremely bogus since
2698 * our driver doesn't follow Adaptec's versioning system. Cheat by just
2699 * returning what the card reported.
2702 aac_rev_check(struct aac_softc *sc, caddr_t udata)
2704 struct aac_rev_check rev_check;
2705 struct aac_rev_check_resp rev_check_resp;
2711 * Copyin the revision struct from userspace
2713 if ((error = copyin(udata, (caddr_t)&rev_check,
2714 sizeof(struct aac_rev_check))) != 0) {
2718 debug(2, "Userland revision= %d\n",
2719 rev_check.callingRevision.buildNumber);
2722 * Doctor up the response struct.
2724 rev_check_resp.possiblyCompatible = 1;
2725 rev_check_resp.adapterSWRevision.external.ul =
2726 sc->aac_revision.external.ul;
2727 rev_check_resp.adapterSWRevision.buildNumber =
2728 sc->aac_revision.buildNumber;
2730 return(copyout((caddr_t)&rev_check_resp, udata,
2731 sizeof(struct aac_rev_check_resp)));
2735 * Pass the caller the next AIF in their queue
2738 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2740 struct get_adapter_fib_ioctl agf;
2745 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2748 * Check the magic number that we gave the caller.
2750 if (agf.AdapterFibContext != (int)sc->aifthread) {
2755 error = aac_return_aif(sc, agf.AifFib);
2757 if ((error == EAGAIN) && (agf.Wait)) {
2758 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2759 while (error == EAGAIN) {
2760 error = tsleep(sc->aac_aifq,
2761 PCATCH, "aacaif", 0);
2763 error = aac_return_aif(sc,
2766 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2775 * Hand the next AIF off the top of the queue out to userspace.
2777 * YYY token could be lost during copyout
2780 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2786 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2787 if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2790 error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2791 sizeof(struct aac_aif_command));
2793 printf("aac_return_aif: copyout returned %d\n", error);
2795 sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) %
2798 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2803 * Give the userland some information about the container. The AAC arch
2804 * expects the driver to be a SCSI passthrough type driver, so it expects
2805 * the containers to have b:t:l numbers. Fake it.
2808 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2810 struct aac_query_disk query_disk;
2811 struct aac_container *co;
2812 struct aac_disk *disk;
2819 error = copyin(uptr, (caddr_t)&query_disk,
2820 sizeof(struct aac_query_disk));
2824 id = query_disk.ContainerNumber;
2828 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2829 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2830 if (co->co_mntobj.ObjectId == id)
2835 query_disk.Valid = 0;
2836 query_disk.Locked = 0;
2837 query_disk.Deleted = 1; /* XXX is this right? */
2839 disk = device_get_softc(co->co_disk);
2840 query_disk.Valid = 1;
2842 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2843 query_disk.Deleted = 0;
2844 query_disk.Bus = device_get_unit(sc->aac_dev);
2845 query_disk.Target = disk->unit;
2847 query_disk.UnMapped = 0;
2848 bcopy(disk->ad_dev_t->si_name,
2849 &query_disk.diskDeviceName[0], 10);
2851 AAC_LOCK_RELEASE(&sc->aac_container_lock);
2853 error = copyout((caddr_t)&query_disk, uptr,
2854 sizeof(struct aac_query_disk));
2860 aac_get_bus_info(struct aac_softc *sc)
2862 struct aac_fib *fib;
2863 struct aac_ctcfg *c_cmd;
2864 struct aac_ctcfg_resp *c_resp;
2865 struct aac_vmioctl *vmi;
2866 struct aac_vmi_businf_resp *vmi_resp;
2867 struct aac_getbusinf businfo;
2868 struct aac_cam_inf *caminf;
2870 int i, found, error;
2872 aac_alloc_sync_fib(sc, &fib, 0);
2873 c_cmd = (struct aac_ctcfg *)&fib->data[0];
2874 bzero(c_cmd, sizeof(struct aac_ctcfg));
2876 c_cmd->Command = VM_ContainerConfig;
2877 c_cmd->cmd = CT_GET_SCSI_METHOD;
2880 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2881 sizeof(struct aac_ctcfg));
2883 device_printf(sc->aac_dev, "Error %d sending "
2884 "VM_ContainerConfig command\n", error);
2885 aac_release_sync_fib(sc);
2889 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2890 if (c_resp->Status != ST_OK) {
2891 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2893 aac_release_sync_fib(sc);
2897 sc->scsi_method_id = c_resp->param;
2899 vmi = (struct aac_vmioctl *)&fib->data[0];
2900 bzero(vmi, sizeof(struct aac_vmioctl));
2902 vmi->Command = VM_Ioctl;
2903 vmi->ObjType = FT_DRIVE;
2904 vmi->MethId = sc->scsi_method_id;
2906 vmi->IoctlCmd = GetBusInfo;
2908 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2909 sizeof(struct aac_vmioctl));
2911 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2913 aac_release_sync_fib(sc);
2917 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2918 if (vmi_resp->Status != ST_OK) {
2919 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
2920 aac_release_sync_fib(sc);
2924 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2925 aac_release_sync_fib(sc);
2928 for (i = 0; i < businfo.BusCount; i++) {
2929 if (businfo.BusValid[i] != AAC_BUS_VALID)
2932 MALLOC(caminf, struct aac_cam_inf *,
2933 sizeof(struct aac_cam_inf), M_AACBUF, M_INTWAIT | M_ZERO);
2935 child = device_add_child(sc->aac_dev, "aacp", -1);
2936 if (child == NULL) {
2937 device_printf(sc->aac_dev, "device_add_child failed\n");
2941 caminf->TargetsPerBus = businfo.TargetsPerBus;
2942 caminf->BusNumber = i;
2943 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2944 caminf->aac_sc = sc;
2946 device_set_ivars(child, caminf);
2947 device_set_desc(child, "SCSI Passthrough Bus");
2953 bus_generic_attach(sc->aac_dev);