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.34 2008/01/20 03:40:35 pavalos Exp $
34 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
36 #define AAC_DRIVER_VERSION 0x02000000
37 #define AAC_DRIVERNAME "aac"
41 /* #include <stddef.h> */
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/sysctl.h>
52 #include <sys/devicestat.h>
54 #include <sys/signalvar.h>
56 #include <sys/eventhandler.h>
59 #include <sys/mplock2.h>
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
65 #include "aac_ioctl.h"
67 #include "aac_tables.h"
69 static void aac_startup(void *arg);
70 static void aac_add_container(struct aac_softc *sc,
71 struct aac_mntinforesp *mir, int f);
72 static void aac_get_bus_info(struct aac_softc *sc);
73 static int aac_shutdown(device_t dev);
75 /* Command Processing */
76 static void aac_timeout(void *ssc);
77 static int aac_map_command(struct aac_command *cm);
78 static void aac_complete(void *context, int pending);
79 static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
80 static void aac_bio_complete(struct aac_command *cm);
81 static int aac_wait_command(struct aac_command *cm);
82 static void aac_command_thread(struct aac_softc *sc);
84 /* Command Buffer Management */
85 static void aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
87 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
89 static int aac_alloc_commands(struct aac_softc *sc);
90 static void aac_free_commands(struct aac_softc *sc);
91 static void aac_unmap_command(struct aac_command *cm);
93 /* Hardware Interface */
94 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
96 static int aac_check_firmware(struct aac_softc *sc);
97 static int aac_init(struct aac_softc *sc);
98 static int aac_sync_command(struct aac_softc *sc, u_int32_t command,
99 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
100 u_int32_t arg3, u_int32_t *sp);
101 static int aac_enqueue_fib(struct aac_softc *sc, int queue,
102 struct aac_command *cm);
103 static int aac_dequeue_fib(struct aac_softc *sc, int queue,
104 u_int32_t *fib_size, struct aac_fib **fib_addr);
105 static int aac_enqueue_response(struct aac_softc *sc, int queue,
106 struct aac_fib *fib);
108 /* Falcon/PPC interface */
109 static int aac_fa_get_fwstatus(struct aac_softc *sc);
110 static void aac_fa_qnotify(struct aac_softc *sc, int qbit);
111 static int aac_fa_get_istatus(struct aac_softc *sc);
112 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask);
113 static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
114 u_int32_t arg0, u_int32_t arg1,
115 u_int32_t arg2, u_int32_t arg3);
116 static int aac_fa_get_mailbox(struct aac_softc *sc, int mb);
117 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable);
119 struct aac_interface aac_fa_interface = {
123 aac_fa_clear_istatus,
126 aac_fa_set_interrupts,
130 /* StrongARM interface */
131 static int aac_sa_get_fwstatus(struct aac_softc *sc);
132 static void aac_sa_qnotify(struct aac_softc *sc, int qbit);
133 static int aac_sa_get_istatus(struct aac_softc *sc);
134 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask);
135 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
136 u_int32_t arg0, u_int32_t arg1,
137 u_int32_t arg2, u_int32_t arg3);
138 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb);
139 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable);
141 struct aac_interface aac_sa_interface = {
145 aac_sa_clear_istatus,
148 aac_sa_set_interrupts,
152 /* i960Rx interface */
153 static int aac_rx_get_fwstatus(struct aac_softc *sc);
154 static void aac_rx_qnotify(struct aac_softc *sc, int qbit);
155 static int aac_rx_get_istatus(struct aac_softc *sc);
156 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask);
157 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
158 u_int32_t arg0, u_int32_t arg1,
159 u_int32_t arg2, u_int32_t arg3);
160 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb);
161 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable);
162 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
163 static int aac_rx_get_outb_queue(struct aac_softc *sc);
164 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
166 struct aac_interface aac_rx_interface = {
170 aac_rx_clear_istatus,
173 aac_rx_set_interrupts,
175 aac_rx_get_outb_queue,
176 aac_rx_set_outb_queue
179 /* Rocket/MIPS interface */
180 static int aac_rkt_get_fwstatus(struct aac_softc *sc);
181 static void aac_rkt_qnotify(struct aac_softc *sc, int qbit);
182 static int aac_rkt_get_istatus(struct aac_softc *sc);
183 static void aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
184 static void aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
185 u_int32_t arg0, u_int32_t arg1,
186 u_int32_t arg2, u_int32_t arg3);
187 static int aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
188 static void aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
189 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
190 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
191 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
193 struct aac_interface aac_rkt_interface = {
194 aac_rkt_get_fwstatus,
197 aac_rkt_clear_istatus,
200 aac_rkt_set_interrupts,
201 aac_rkt_send_command,
202 aac_rkt_get_outb_queue,
203 aac_rkt_set_outb_queue
206 /* Debugging and Diagnostics */
207 static void aac_describe_controller(struct aac_softc *sc);
208 static char *aac_describe_code(struct aac_code_lookup *table,
211 /* Management Interface */
212 static d_open_t aac_open;
213 static d_close_t aac_close;
214 static d_ioctl_t aac_ioctl;
215 static d_poll_t aac_poll;
216 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) __unused;
217 static void aac_handle_aif(struct aac_softc *sc,
218 struct aac_fib *fib);
219 static int aac_rev_check(struct aac_softc *sc, caddr_t udata);
220 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
221 static int aac_return_aif(struct aac_softc *sc, caddr_t uptr);
222 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr);
223 static int aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
224 static void aac_ioctl_event(struct aac_softc *sc,
225 struct aac_event *event, void *arg);
227 #define AAC_CDEV_MAJOR 150
229 static struct dev_ops aac_ops = {
230 { "aac", AAC_CDEV_MAJOR, 0 },
232 .d_close = aac_close,
233 .d_ioctl = aac_ioctl,
237 DECLARE_DUMMY_MODULE(aac);
239 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
242 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
249 * Initialise the controller and softc
252 aac_attach(struct aac_softc *sc)
257 callout_init(&sc->aac_watchdog);
260 * Initialise per-controller queues.
265 aac_initq_complete(sc);
269 * Initialise command-completion task.
271 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
273 /* mark controller as suspended until we get ourselves organised */
274 sc->aac_state |= AAC_STATE_SUSPEND;
277 * Check that the firmware on the card is supported.
279 if ((error = aac_check_firmware(sc)) != 0)
285 AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
286 AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock");
287 AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
288 TAILQ_INIT(&sc->aac_container_tqh);
289 TAILQ_INIT(&sc->aac_ev_cmfree);
292 /* Initialize the local AIF queue pointers */
293 sc->aac_aifq_head = sc->aac_aifq_tail = AAC_AIFQ_LENGTH;
296 * Initialise the adapter.
298 if ((error = aac_init(sc)) != 0)
302 * Allocate and connect our interrupt.
305 if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
308 RF_ACTIVE)) == NULL) {
309 device_printf(sc->aac_dev, "can't allocate interrupt\n");
312 if (sc->flags & AAC_FLAGS_NEW_COMM) {
313 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
315 sc, &sc->aac_intr, NULL)) {
316 device_printf(sc->aac_dev, "can't set up interrupt\n");
320 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
321 INTR_FAST, aac_fast_intr,
322 sc, &sc->aac_intr, NULL)) {
323 device_printf(sc->aac_dev,
324 "can't set up FAST interrupt\n");
325 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
327 sc, &sc->aac_intr, NULL)) {
328 device_printf(sc->aac_dev,
329 "can't set up MPSAFE interrupt\n");
336 * Print a little information about the controller.
338 aac_describe_controller(sc);
341 * Register to probe our containers later.
343 sc->aac_ich.ich_func = aac_startup;
344 sc->aac_ich.ich_arg = sc;
345 sc->aac_ich.ich_desc = "aac";
346 if (config_intrhook_establish(&sc->aac_ich) != 0) {
347 device_printf(sc->aac_dev,
348 "can't establish configuration hook\n");
353 * Make the control device.
355 unit = device_get_unit(sc->aac_dev);
356 sc->aac_dev_t = make_dev(&aac_ops, unit, UID_ROOT, GID_OPERATOR,
357 0640, "aac%d", unit);
358 sc->aac_dev_t->si_drv1 = sc;
359 reference_dev(sc->aac_dev_t);
361 /* Create the AIF thread */
362 if (kthread_create((void(*)(void *))aac_command_thread, sc,
363 &sc->aifthread, "aac%daif", unit))
364 panic("Could not create AIF thread\n");
366 /* Register the shutdown method to only be called post-dump */
367 if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, aac_shutdown,
368 sc->aac_dev, SHUTDOWN_PRI_DRIVER)) == NULL)
369 device_printf(sc->aac_dev,
370 "shutdown event registration failed\n");
372 /* Register with CAM for the non-DASD devices */
373 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
374 TAILQ_INIT(&sc->aac_sim_tqh);
375 aac_get_bus_info(sc);
382 aac_add_event(struct aac_softc *sc, struct aac_event *event)
385 switch (event->ev_type & AAC_EVENT_MASK) {
386 case AAC_EVENT_CMFREE:
387 TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
390 device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
399 * Probe for containers, create disks.
402 aac_startup(void *arg)
404 struct aac_softc *sc;
406 struct aac_mntinfo *mi;
407 struct aac_mntinforesp *mir = NULL;
408 int count = 0, i = 0;
412 sc = (struct aac_softc *)arg;
414 /* disconnect ourselves from the intrhook chain */
415 config_intrhook_disestablish(&sc->aac_ich);
417 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
418 aac_alloc_sync_fib(sc, &fib);
419 mi = (struct aac_mntinfo *)&fib->data[0];
421 /* loop over possible containers */
423 /* request information on this container */
424 bzero(mi, sizeof(struct aac_mntinfo));
425 mi->Command = VM_NameServe;
426 mi->MntType = FT_FILESYS;
428 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
429 sizeof(struct aac_mntinfo))) {
430 device_printf(sc->aac_dev,
431 "error probing container %d", i);
436 mir = (struct aac_mntinforesp *)&fib->data[0];
437 /* XXX Need to check if count changed */
438 count = mir->MntRespCount;
439 aac_add_container(sc, mir, 0);
441 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
443 aac_release_sync_fib(sc);
444 AAC_LOCK_RELEASE(&sc->aac_io_lock);
446 /* poke the bus to actually attach the child devices */
447 if (bus_generic_attach(sc->aac_dev))
448 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
450 /* mark the controller up */
451 sc->aac_state &= ~AAC_STATE_SUSPEND;
453 /* enable interrupts now */
454 AAC_UNMASK_INTERRUPTS(sc);
458 * Create a device to respresent a new container
461 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
463 struct aac_container *co;
467 * Check container volume type for validity. Note that many of
468 * the possible types may never show up.
470 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
471 co = (struct aac_container *)kmalloc(sizeof *co, M_AACBUF,
473 debug(1, "id %x name '%.16s' size %u type %d",
474 mir->MntTable[0].ObjectId,
475 mir->MntTable[0].FileSystemName,
476 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
478 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
479 device_printf(sc->aac_dev, "device_add_child failed\n");
481 device_set_ivars(child, co);
482 device_set_desc(child, aac_describe_code(aac_container_types,
483 mir->MntTable[0].VolType));
486 bcopy(&mir->MntTable[0], &co->co_mntobj,
487 sizeof(struct aac_mntobj));
488 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
489 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
490 AAC_LOCK_RELEASE(&sc->aac_container_lock);
495 * Free all of the resources associated with (sc)
497 * Should not be called if the controller is active.
500 aac_free(struct aac_softc *sc)
505 /* remove the control device */
506 if (sc->aac_dev_t != NULL)
507 destroy_dev(sc->aac_dev_t);
509 /* throw away any FIB buffers, discard the FIB DMA tag */
510 aac_free_commands(sc);
511 if (sc->aac_fib_dmat)
512 bus_dma_tag_destroy(sc->aac_fib_dmat);
514 kfree(sc->aac_commands, M_AACBUF);
516 /* destroy the common area */
517 if (sc->aac_common) {
518 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
519 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
520 sc->aac_common_dmamap);
522 if (sc->aac_common_dmat)
523 bus_dma_tag_destroy(sc->aac_common_dmat);
525 /* disconnect the interrupt handler */
527 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
528 if (sc->aac_irq != NULL)
529 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
532 /* destroy data-transfer DMA tag */
533 if (sc->aac_buffer_dmat)
534 bus_dma_tag_destroy(sc->aac_buffer_dmat);
536 /* destroy the parent DMA tag */
537 if (sc->aac_parent_dmat)
538 bus_dma_tag_destroy(sc->aac_parent_dmat);
540 /* release the register window mapping */
541 if (sc->aac_regs_resource != NULL) {
542 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
543 sc->aac_regs_rid, sc->aac_regs_resource);
545 dev_ops_remove_minor(&aac_ops, device_get_unit(sc->aac_dev));
549 * Disconnect from the controller completely, in preparation for unload.
552 aac_detach(device_t dev)
554 struct aac_softc *sc;
555 struct aac_container *co;
561 sc = device_get_softc(dev);
563 callout_stop(&sc->aac_watchdog);
565 if (sc->aac_state & AAC_STATE_OPEN)
568 /* Remove the child containers */
569 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
570 error = device_delete_child(dev, co->co_disk);
573 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
577 /* Remove the CAM SIMs */
578 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
579 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
580 error = device_delete_child(dev, sim->sim_dev);
583 kfree(sim, M_AACBUF);
586 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
587 sc->aifflags |= AAC_AIFFLAGS_EXIT;
588 wakeup(sc->aifthread);
589 tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz);
592 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
593 panic("Cannot shutdown AIF thread\n");
595 if ((error = aac_shutdown(dev)))
598 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->eh);
602 lockuninit(&sc->aac_aifq_lock);
603 lockuninit(&sc->aac_io_lock);
604 lockuninit(&sc->aac_container_lock);
610 * Bring the controller down to a dormant state and detach all child devices.
612 * This function is called before detach or system shutdown.
614 * Note that we can assume that the bioq on the controller is empty, as we won't
615 * allow shutdown if any device is open.
618 aac_shutdown(device_t dev)
620 struct aac_softc *sc;
622 struct aac_close_command *cc;
626 sc = device_get_softc(dev);
628 sc->aac_state |= AAC_STATE_SUSPEND;
631 * Send a Container shutdown followed by a HostShutdown FIB to the
632 * controller to convince it that we don't want to talk to it anymore.
633 * We've been closed and all I/O completed already
635 device_printf(sc->aac_dev, "shutting down controller...");
637 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
638 aac_alloc_sync_fib(sc, &fib);
639 cc = (struct aac_close_command *)&fib->data[0];
641 bzero(cc, sizeof(struct aac_close_command));
642 cc->Command = VM_CloseAll;
643 cc->ContainerId = 0xffffffff;
644 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
645 sizeof(struct aac_close_command)))
646 kprintf("FAILED.\n");
653 * XXX Issuing this command to the controller makes it shut down
654 * but also keeps it from coming back up without a reset of the
655 * PCI bus. This is not desirable if you are just unloading the
656 * driver module with the intent to reload it later.
658 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
660 kprintf("FAILED.\n");
667 AAC_MASK_INTERRUPTS(sc);
668 aac_release_sync_fib(sc);
669 AAC_LOCK_RELEASE(&sc->aac_io_lock);
675 * Bring the controller to a quiescent state, ready for system suspend.
678 aac_suspend(device_t dev)
680 struct aac_softc *sc;
684 sc = device_get_softc(dev);
686 sc->aac_state |= AAC_STATE_SUSPEND;
688 AAC_MASK_INTERRUPTS(sc);
693 * Bring the controller back to a state ready for operation.
696 aac_resume(device_t dev)
698 struct aac_softc *sc;
702 sc = device_get_softc(dev);
704 sc->aac_state &= ~AAC_STATE_SUSPEND;
705 AAC_UNMASK_INTERRUPTS(sc);
710 * Interrupt handler for NEW_COMM interface.
713 aac_new_intr(void *arg)
715 struct aac_softc *sc;
716 u_int32_t index, fast;
717 struct aac_command *cm;
723 sc = (struct aac_softc *)arg;
725 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
727 index = AAC_GET_OUTB_QUEUE(sc);
728 if (index == 0xffffffff)
729 index = AAC_GET_OUTB_QUEUE(sc);
730 if (index == 0xffffffff)
733 if (index == 0xfffffffe) {
734 /* XXX This means that the controller wants
735 * more work. Ignore it for now.
740 fib = (struct aac_fib *)kmalloc(sizeof *fib, M_AACBUF,
743 for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
744 ((u_int32_t *)fib)[i] = AAC_GETREG4(sc, index + i*4);
745 aac_handle_aif(sc, fib);
746 kfree(fib, M_AACBUF);
749 * AIF memory is owned by the adapter, so let it
750 * know that we are done with it.
752 AAC_SET_OUTB_QUEUE(sc, index);
753 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
756 cm = sc->aac_commands + (index >> 2);
759 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
760 *((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
763 aac_unmap_command(cm);
764 cm->cm_flags |= AAC_CMD_COMPLETED;
766 /* is there a completion handler? */
767 if (cm->cm_complete != NULL) {
770 /* assume that someone is sleeping on this
775 sc->flags &= ~AAC_QUEUE_FRZN;
778 /* see if we can start some more I/O */
779 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
782 AAC_LOCK_RELEASE(&sc->aac_io_lock);
786 aac_fast_intr(void *arg)
788 struct aac_softc *sc;
793 sc = (struct aac_softc *)arg;
796 * Read the status register directly. This is faster than taking the
797 * driver lock and reading the queues directly. It also saves having
798 * to turn parts of the driver lock into a spin mutex, which would be
801 reason = AAC_GET_ISTATUS(sc);
802 AAC_CLEAR_ISTATUS(sc, reason);
804 /* handle completion processing */
805 if (reason & AAC_DB_RESPONSE_READY)
806 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
808 /* controller wants to talk to us */
809 if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
811 * XXX Make sure that we don't get fooled by strange messages
812 * that start with a NULL.
814 if ((reason & AAC_DB_PRINTF) &&
815 (sc->aac_common->ac_printf[0] == 0))
816 sc->aac_common->ac_printf[0] = 32;
819 * This might miss doing the actual wakeup. However, the
820 * ssleep that this is waking up has a timeout, so it will
821 * wake up eventually. AIFs and printfs are low enough
822 * priority that they can handle hanging out for a few seconds
825 wakeup(sc->aifthread);
834 * Start as much queued I/O as possible on the controller
837 aac_startio(struct aac_softc *sc)
839 struct aac_command *cm;
843 if (sc->flags & AAC_QUEUE_FRZN)
848 * Try to get a command that's been put off for lack of
851 cm = aac_dequeue_ready(sc);
854 * Try to build a command off the bio queue (ignore error
858 aac_bio_command(sc, &cm);
865 * Try to give the command to the controller. Any error is
866 * catastrophic since it means that bus_dmamap_load() failed.
868 if (aac_map_command(cm) != 0)
869 panic("aac: error mapping command %p\n", cm);
874 * Deliver a command to the controller; allocate controller resources at the
875 * last moment when possible.
878 aac_map_command(struct aac_command *cm)
880 struct aac_softc *sc;
888 /* don't map more than once */
889 if (cm->cm_flags & AAC_CMD_MAPPED)
890 panic("aac: command %p already mapped", cm);
892 if (cm->cm_datalen != 0) {
893 error = bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
894 cm->cm_data, cm->cm_datalen,
895 aac_map_command_sg, cm, 0);
896 if (error == EINPROGRESS) {
897 debug(1, "freezing queue\n");
898 sc->flags |= AAC_QUEUE_FRZN;
902 aac_map_command_sg(cm, NULL, 0, 0);
908 * Handle notification of one or more FIBs coming from the controller.
911 aac_command_thread(struct aac_softc *sc)
919 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
920 sc->aifflags = AAC_AIFFLAGS_RUNNING;
922 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
924 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) {
925 tsleep_interlock(sc->aifthread, 0);
926 AAC_LOCK_RELEASE(&sc->aac_io_lock);
927 retval = tsleep(sc->aifthread, PINTERLOCKED,
928 "aifthd", AAC_PERIODIC_INTERVAL * hz);
929 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
932 * First see if any FIBs need to be allocated. This needs
933 * to be called without the driver lock because contigmalloc
934 * will grab Giant, and would result in an LOR.
936 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
937 AAC_LOCK_RELEASE(&sc->aac_io_lock);
938 aac_alloc_commands(sc);
939 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
940 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
945 * While we're here, check to see if any commands are stuck.
946 * This is pretty low-priority, so it's ok if it doesn't
949 if (retval == EWOULDBLOCK)
952 /* Check the hardware printf message buffer */
953 if (sc->aac_common->ac_printf[0] != 0)
954 aac_print_printf(sc);
956 /* Also check to see if the adapter has a command for us. */
957 if (sc->flags & AAC_FLAGS_NEW_COMM)
960 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
964 AAC_PRINT_FIB(sc, fib);
966 switch (fib->Header.Command) {
968 aac_handle_aif(sc, fib);
971 device_printf(sc->aac_dev, "unknown command "
972 "from controller\n");
976 if ((fib->Header.XferState == 0) ||
977 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
981 /* Return the AIF to the controller. */
982 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
983 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
984 *(AAC_FSAStatus*)fib->data = ST_OK;
986 /* XXX Compute the Size field? */
987 size = fib->Header.Size;
988 if (size > sizeof(struct aac_fib)) {
989 size = sizeof(struct aac_fib);
990 fib->Header.Size = size;
993 * Since we did not generate this command, it
994 * cannot go through the normal
995 * enqueue->startio chain.
997 aac_enqueue_response(sc,
998 AAC_ADAP_NORM_RESP_QUEUE,
1003 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1004 AAC_LOCK_RELEASE(&sc->aac_io_lock);
1005 wakeup(sc->aac_dev);
1011 * Process completed commands.
1014 aac_complete(void *context, int pending)
1016 struct aac_softc *sc;
1017 struct aac_command *cm;
1018 struct aac_fib *fib;
1023 sc = (struct aac_softc *)context;
1025 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1027 /* pull completed commands off the queue */
1029 /* look for completed FIBs on our queue */
1030 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1032 break; /* nothing to do */
1034 /* get the command, unmap and queue for later processing */
1035 cm = sc->aac_commands + fib->Header.SenderData;
1037 AAC_PRINT_FIB(sc, fib);
1040 aac_remove_busy(cm);
1041 aac_unmap_command(cm); /* XXX defer? */
1042 cm->cm_flags |= AAC_CMD_COMPLETED;
1044 /* is there a completion handler? */
1045 if (cm->cm_complete != NULL) {
1046 cm->cm_complete(cm);
1048 /* assume that someone is sleeping on this command */
1053 /* see if we can start some more I/O */
1054 sc->flags &= ~AAC_QUEUE_FRZN;
1057 AAC_LOCK_RELEASE(&sc->aac_io_lock);
1061 * Handle a bio submitted from a disk device.
1064 aac_submit_bio(struct aac_disk *ad, struct bio *bio)
1066 struct aac_softc *sc;
1070 bio->bio_driver_info = ad;
1071 sc = ad->ad_controller;
1073 /* queue the BIO and try to get some work done */
1074 aac_enqueue_bio(sc, bio);
1079 * Get a bio and build a command to go with it.
1082 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1084 struct aac_command *cm;
1085 struct aac_fib *fib;
1086 struct aac_disk *ad;
1092 /* get the resources we will need */
1095 if (aac_alloc_command(sc, &cm)) /* get a command */
1097 if ((bio = aac_dequeue_bio(sc)) == NULL)
1100 /* fill out the command */
1102 cm->cm_data = (void *)bp->b_data;
1103 cm->cm_datalen = bp->b_bcount;
1104 cm->cm_complete = aac_bio_complete;
1105 cm->cm_private = bio;
1106 cm->cm_timestamp = time_second;
1107 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1111 fib->Header.Size = sizeof(struct aac_fib_header);
1112 fib->Header.XferState =
1113 AAC_FIBSTATE_HOSTOWNED |
1114 AAC_FIBSTATE_INITIALISED |
1115 AAC_FIBSTATE_EMPTY |
1116 AAC_FIBSTATE_FROMHOST |
1117 AAC_FIBSTATE_REXPECTED |
1119 AAC_FIBSTATE_ASYNC |
1120 AAC_FIBSTATE_FAST_RESPONSE;
1122 /* build the read/write request */
1123 ad = (struct aac_disk *)bio->bio_driver_info;
1125 if (sc->flags & AAC_FLAGS_RAW_IO) {
1126 struct aac_raw_io *raw;
1127 raw = (struct aac_raw_io *)&fib->data[0];
1128 fib->Header.Command = RawIo;
1129 raw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1130 raw->ByteCount = bp->b_bcount;
1131 raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1133 raw->BpComplete = 0;
1134 fib->Header.Size += sizeof(struct aac_raw_io);
1135 cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1136 if (bp->b_cmd == BUF_CMD_READ) {
1138 cm->cm_flags |= AAC_CMD_DATAIN;
1141 cm->cm_flags |= AAC_CMD_DATAOUT;
1143 } else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1144 fib->Header.Command = ContainerCommand;
1145 if (bp->b_cmd == BUF_CMD_READ) {
1146 struct aac_blockread *br;
1147 br = (struct aac_blockread *)&fib->data[0];
1148 br->Command = VM_CtBlockRead;
1149 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1150 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1151 br->ByteCount = bp->b_bcount;
1152 fib->Header.Size += sizeof(struct aac_blockread);
1153 cm->cm_sgtable = &br->SgMap;
1154 cm->cm_flags |= AAC_CMD_DATAIN;
1156 struct aac_blockwrite *bw;
1157 bw = (struct aac_blockwrite *)&fib->data[0];
1158 bw->Command = VM_CtBlockWrite;
1159 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1160 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1161 bw->ByteCount = bp->b_bcount;
1162 bw->Stable = CUNSTABLE;
1163 fib->Header.Size += sizeof(struct aac_blockwrite);
1164 cm->cm_flags |= AAC_CMD_DATAOUT;
1165 cm->cm_sgtable = &bw->SgMap;
1168 fib->Header.Command = ContainerCommand64;
1169 if (bp->b_cmd == BUF_CMD_READ) {
1170 struct aac_blockread64 *br;
1171 br = (struct aac_blockread64 *)&fib->data[0];
1172 br->Command = VM_CtHostRead64;
1173 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1174 br->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1175 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1178 fib->Header.Size += sizeof(struct aac_blockread64);
1179 cm->cm_flags |= AAC_CMD_DATAOUT;
1180 cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1182 struct aac_blockwrite64 *bw;
1183 bw = (struct aac_blockwrite64 *)&fib->data[0];
1184 bw->Command = VM_CtHostWrite64;
1185 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1186 bw->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1187 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1190 fib->Header.Size += sizeof(struct aac_blockwrite64);
1191 cm->cm_flags |= AAC_CMD_DATAIN;
1192 cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1201 aac_enqueue_bio(sc, bio);
1203 aac_release_command(cm);
1208 * Handle a bio-instigated command that has been completed.
1211 aac_bio_complete(struct aac_command *cm)
1213 struct aac_blockread_response *brr;
1214 struct aac_blockwrite_response *bwr;
1218 AAC_FSAStatus status;
1220 /* fetch relevant status and then release the command */
1221 bio = (struct bio *)cm->cm_private;
1223 if (bp->b_cmd == BUF_CMD_READ) {
1224 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1225 status = brr->Status;
1227 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1228 status = bwr->Status;
1230 aac_release_command(cm);
1232 /* fix up the bio based on status */
1233 if (status == ST_OK) {
1238 bp->b_flags |= B_ERROR;
1239 /* pass an error string out to the disk layer */
1240 code = aac_describe_code(aac_command_status_table, status);
1242 aac_biodone(bio, code);
1246 * Dump a block of data to the controller. If the queue is full, tell the
1247 * caller to hold off and wait for the queue to drain.
1250 aac_dump_enqueue(struct aac_disk *ad, u_int64_t lba, void *data, int dumppages)
1252 struct aac_softc *sc;
1253 struct aac_command *cm;
1254 struct aac_fib *fib;
1255 struct aac_blockwrite *bw;
1257 sc = ad->ad_controller;
1260 KKASSERT(lba <= 0x100000000ULL);
1262 if (aac_alloc_command(sc, &cm))
1265 /* fill out the command */
1267 cm->cm_datalen = dumppages * PAGE_SIZE;
1268 cm->cm_complete = NULL;
1269 cm->cm_private = NULL;
1270 cm->cm_timestamp = time_second;
1271 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1275 fib->Header.XferState =
1276 AAC_FIBSTATE_HOSTOWNED |
1277 AAC_FIBSTATE_INITIALISED |
1278 AAC_FIBSTATE_FROMHOST |
1279 AAC_FIBSTATE_REXPECTED |
1281 fib->Header.Command = ContainerCommand;
1282 fib->Header.Size = sizeof(struct aac_fib_header);
1284 bw = (struct aac_blockwrite *)&fib->data[0];
1285 bw->Command = VM_CtBlockWrite;
1286 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1287 bw->BlockNumber = lba;
1288 bw->ByteCount = dumppages * PAGE_SIZE;
1289 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */
1290 fib->Header.Size += sizeof(struct aac_blockwrite);
1291 cm->cm_flags |= AAC_CMD_DATAOUT;
1292 cm->cm_sgtable = &bw->SgMap;
1294 return (aac_map_command(cm));
1298 * Wait for the card's queue to drain when dumping. Also check for monitor
1302 aac_dump_complete(struct aac_softc *sc)
1304 struct aac_fib *fib;
1305 struct aac_command *cm;
1307 u_int32_t pi, ci, fib_size;
1310 reason = AAC_GET_ISTATUS(sc);
1311 if (reason & AAC_DB_RESPONSE_READY) {
1312 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
1314 if (aac_dequeue_fib(sc,
1315 AAC_HOST_NORM_RESP_QUEUE,
1318 cm = (struct aac_command *)
1319 fib->Header.SenderData;
1321 AAC_PRINT_FIB(sc, fib);
1323 aac_remove_busy(cm);
1324 aac_unmap_command(cm);
1325 aac_enqueue_complete(cm);
1326 aac_release_command(cm);
1330 if (reason & AAC_DB_PRINTF) {
1331 AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
1332 aac_print_printf(sc);
1334 pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1335 AAC_PRODUCER_INDEX];
1336 ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][
1337 AAC_CONSUMER_INDEX];
1344 * Submit a command to the controller, return when it completes.
1345 * XXX This is very dangerous! If the card has gone out to lunch, we could
1346 * be stuck here forever. At the same time, signals are not caught
1347 * because there is a risk that a signal could wakeup the sleep before
1348 * the card has a chance to complete the command. Since there is no way
1349 * to cancel a command that is in progress, we can't protect against the
1350 * card completing a command late and spamming the command and data
1351 * memory. So, we are held hostage until the command completes.
1354 aac_wait_command(struct aac_command *cm)
1356 struct aac_softc *sc;
1363 /* Put the command on the ready queue and get things going */
1364 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1365 aac_enqueue_ready(cm);
1368 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
1369 tsleep_interlock(cm, 0);
1370 AAC_LOCK_RELEASE(&sc->aac_io_lock);
1371 error = tsleep(cm, PINTERLOCKED, "aacwait", 0);
1372 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1377 *Command Buffer Management
1381 * Allocate a command.
1384 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1386 struct aac_command *cm;
1390 if ((cm = aac_dequeue_free(sc)) == NULL) {
1391 if (sc->total_fibs < sc->aac_max_fibs) {
1392 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1393 wakeup(sc->aifthread);
1403 * Release a command back to the freelist.
1406 aac_release_command(struct aac_command *cm)
1408 struct aac_event *event;
1409 struct aac_softc *sc;
1413 /* (re)initialise the command/FIB */
1414 cm->cm_sgtable = NULL;
1416 cm->cm_complete = NULL;
1417 cm->cm_private = NULL;
1418 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1419 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1420 cm->cm_fib->Header.Flags = 0;
1421 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1424 * These are duplicated in aac_start to cover the case where an
1425 * intermediate stage may have destroyed them. They're left
1426 * initialised here for debugging purposes only.
1428 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1429 cm->cm_fib->Header.SenderData = 0;
1431 aac_enqueue_free(cm);
1434 event = TAILQ_FIRST(&sc->aac_ev_cmfree);
1435 if (event != NULL) {
1436 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1437 event->ev_callback(sc, event, event->ev_arg);
1442 * Map helper for command/FIB allocation.
1445 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1449 fibphys = (uint64_t *)arg;
1453 *fibphys = segs[0].ds_addr;
1457 * Allocate and initialise commands/FIBs for this adapter.
1460 aac_alloc_commands(struct aac_softc *sc)
1462 struct aac_command *cm;
1463 struct aac_fibmap *fm;
1469 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1472 fm = kmalloc(sizeof(struct aac_fibmap), M_AACBUF, M_INTWAIT | M_ZERO);
1474 /* allocate the FIBs in DMAable memory and load them */
1475 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1476 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1477 device_printf(sc->aac_dev,
1478 "Not enough contiguous memory available.\n");
1479 kfree(fm, M_AACBUF);
1483 /* Ignore errors since this doesn't bounce */
1484 bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1485 sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1486 aac_map_command_helper, &fibphys, 0);
1488 /* initialise constant fields in the command structure */
1489 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1490 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1491 cm = sc->aac_commands + sc->total_fibs;
1492 fm->aac_commands = cm;
1494 cm->cm_fib = (struct aac_fib *)
1495 ((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1496 cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1497 cm->cm_index = sc->total_fibs;
1499 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1500 &cm->cm_datamap)) != 0)
1502 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1503 aac_release_command(cm);
1505 AAC_LOCK_RELEASE(&sc->aac_io_lock);
1509 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1510 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1511 debug(1, "total_fibs= %d\n", sc->total_fibs);
1512 AAC_LOCK_RELEASE(&sc->aac_io_lock);
1516 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1517 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1518 kfree(fm, M_AACBUF);
1523 * Free FIBs owned by this adapter.
1526 aac_free_commands(struct aac_softc *sc)
1528 struct aac_fibmap *fm;
1529 struct aac_command *cm;
1534 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1536 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1538 * We check against total_fibs to handle partially
1541 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1542 cm = fm->aac_commands + i;
1543 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1545 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1546 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1547 kfree(fm, M_AACBUF);
1552 * Command-mapping helper function - populate this command's s/g table.
1555 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1557 struct aac_softc *sc;
1558 struct aac_command *cm;
1559 struct aac_fib *fib;
1564 cm = (struct aac_command *)arg;
1568 /* copy into the FIB */
1569 if (cm->cm_sgtable != NULL) {
1570 if (fib->Header.Command == RawIo) {
1571 struct aac_sg_tableraw *sg;
1572 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1574 for (i = 0; i < nseg; i++) {
1575 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1576 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1577 sg->SgEntryRaw[i].Next = 0;
1578 sg->SgEntryRaw[i].Prev = 0;
1579 sg->SgEntryRaw[i].Flags = 0;
1581 /* update the FIB size for the s/g count */
1582 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1583 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1584 struct aac_sg_table *sg;
1585 sg = cm->cm_sgtable;
1587 for (i = 0; i < nseg; i++) {
1588 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1589 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1591 /* update the FIB size for the s/g count */
1592 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1594 struct aac_sg_table64 *sg;
1595 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1597 for (i = 0; i < nseg; i++) {
1598 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1599 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1601 /* update the FIB size for the s/g count */
1602 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1606 /* Fix up the address values in the FIB. Use the command array index
1607 * instead of a pointer since these fields are only 32 bits. Shift
1608 * the SenderFibAddress over to make room for the fast response bit
1609 * and for the AIF bit
1611 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1612 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1614 /* save a pointer to the command for speedy reverse-lookup */
1615 cm->cm_fib->Header.SenderData = cm->cm_index;
1617 if (cm->cm_flags & AAC_CMD_DATAIN)
1618 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1619 BUS_DMASYNC_PREREAD);
1620 if (cm->cm_flags & AAC_CMD_DATAOUT)
1621 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1622 BUS_DMASYNC_PREWRITE);
1623 cm->cm_flags |= AAC_CMD_MAPPED;
1625 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1626 int count = 10000000L;
1627 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1629 aac_unmap_command(cm);
1630 sc->flags |= AAC_QUEUE_FRZN;
1631 aac_requeue_ready(cm);
1634 DELAY(5); /* wait 5 usec. */
1637 /* Put the FIB on the outbound queue */
1638 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1639 aac_unmap_command(cm);
1640 sc->flags |= AAC_QUEUE_FRZN;
1641 aac_requeue_ready(cm);
1647 * Unmap a command from controller-visible space.
1650 aac_unmap_command(struct aac_command *cm)
1652 struct aac_softc *sc;
1658 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1661 if (cm->cm_datalen != 0) {
1662 if (cm->cm_flags & AAC_CMD_DATAIN)
1663 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1664 BUS_DMASYNC_POSTREAD);
1665 if (cm->cm_flags & AAC_CMD_DATAOUT)
1666 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1667 BUS_DMASYNC_POSTWRITE);
1669 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1671 cm->cm_flags &= ~AAC_CMD_MAPPED;
1675 * Hardware Interface
1679 * Initialise the adapter.
1682 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1684 struct aac_softc *sc;
1688 sc = (struct aac_softc *)arg;
1690 sc->aac_common_busaddr = segs[0].ds_addr;
1694 aac_check_firmware(struct aac_softc *sc)
1696 u_int32_t major, minor, options = 0, atu_size = 0;
1702 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1703 * firmware version 1.x are not compatible with this driver.
1705 if (sc->flags & AAC_FLAGS_PERC2QC) {
1706 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1708 device_printf(sc->aac_dev,
1709 "Error reading firmware version\n");
1713 /* These numbers are stored as ASCII! */
1714 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1715 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1717 device_printf(sc->aac_dev,
1718 "Firmware version %d.%d is not supported.\n",
1725 * Retrieve the capabilities/supported options word so we know what
1726 * work-arounds to enable. Some firmware revs don't support this
1729 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1730 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1731 device_printf(sc->aac_dev,
1732 "RequestAdapterInfo failed\n");
1736 options = AAC_GET_MAILBOX(sc, 1);
1737 atu_size = AAC_GET_MAILBOX(sc, 2);
1738 sc->supported_options = options;
1740 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1741 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1742 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1743 if (options & AAC_SUPPORTED_NONDASD)
1744 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1745 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1746 && (sizeof(bus_addr_t) > 4)) {
1747 device_printf(sc->aac_dev,
1748 "Enabling 64-bit address support\n");
1749 sc->flags |= AAC_FLAGS_SG_64BIT;
1751 if ((options & AAC_SUPPORTED_NEW_COMM)
1752 && sc->aac_if.aif_send_command)
1753 sc->flags |= AAC_FLAGS_NEW_COMM;
1754 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1755 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1758 /* Check for broken hardware that does a lower number of commands */
1759 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1761 /* Remap mem. resource, if required */
1762 if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1763 atu_size > rman_get_size(sc->aac_regs_resource)) {
1764 bus_release_resource(
1765 sc->aac_dev, SYS_RES_MEMORY,
1766 sc->aac_regs_rid, sc->aac_regs_resource);
1767 sc->aac_regs_resource = bus_alloc_resource(
1768 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
1769 0ul, ~0ul, atu_size, RF_ACTIVE);
1770 if (sc->aac_regs_resource == NULL) {
1771 sc->aac_regs_resource = bus_alloc_resource_any(
1772 sc->aac_dev, SYS_RES_MEMORY,
1773 &sc->aac_regs_rid, RF_ACTIVE);
1774 if (sc->aac_regs_resource == NULL) {
1775 device_printf(sc->aac_dev,
1776 "couldn't allocate register window\n");
1779 sc->flags &= ~AAC_FLAGS_NEW_COMM;
1781 sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
1782 sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
1785 /* Read preferred settings */
1786 sc->aac_max_fib_size = sizeof(struct aac_fib);
1787 sc->aac_max_sectors = 128; /* 64KB */
1788 if (sc->flags & AAC_FLAGS_SG_64BIT)
1789 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1790 - sizeof(struct aac_blockwrite64)
1791 + sizeof(struct aac_sg_table64))
1792 / sizeof(struct aac_sg_table64);
1794 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1795 - sizeof(struct aac_blockwrite)
1796 + sizeof(struct aac_sg_table))
1797 / sizeof(struct aac_sg_table);
1799 if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1800 options = AAC_GET_MAILBOX(sc, 1);
1801 sc->aac_max_fib_size = (options & 0xFFFF);
1802 sc->aac_max_sectors = (options >> 16) << 1;
1803 options = AAC_GET_MAILBOX(sc, 2);
1804 sc->aac_sg_tablesize = (options >> 16);
1805 options = AAC_GET_MAILBOX(sc, 3);
1806 sc->aac_max_fibs = (options & 0xFFFF);
1808 if (sc->aac_max_fib_size > PAGE_SIZE)
1809 sc->aac_max_fib_size = PAGE_SIZE;
1810 sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1816 aac_init(struct aac_softc *sc)
1818 struct aac_adapter_init *ip;
1820 u_int32_t code, qoffset;
1826 * First wait for the adapter to come ready.
1830 code = AAC_GET_FWSTATUS(sc);
1831 if (code & AAC_SELF_TEST_FAILED) {
1832 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1835 if (code & AAC_KERNEL_PANIC) {
1836 device_printf(sc->aac_dev,
1837 "FATAL: controller kernel panic\n");
1840 if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1841 device_printf(sc->aac_dev,
1842 "FATAL: controller not coming ready, "
1843 "status %x\n", code);
1846 } while (!(code & AAC_UP_AND_RUNNING));
1850 * Create DMA tag for mapping buffers into controller-addressable space.
1852 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1853 1, 0, /* algnmnt, boundary */
1854 (sc->flags & AAC_FLAGS_SG_64BIT) ?
1856 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1857 BUS_SPACE_MAXADDR, /* highaddr */
1858 NULL, NULL, /* filter, filterarg */
1859 MAXBSIZE, /* maxsize */
1860 sc->aac_sg_tablesize, /* nsegments */
1861 MAXBSIZE, /* maxsegsize */
1862 BUS_DMA_ALLOCNOW, /* flags */
1863 &sc->aac_buffer_dmat)) {
1864 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1869 * Create DMA tag for mapping FIBs into controller-addressable space..
1871 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1872 1, 0, /* algnmnt, boundary */
1873 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1874 BUS_SPACE_MAXADDR_32BIT :
1875 0x7fffffff, /* lowaddr */
1876 BUS_SPACE_MAXADDR, /* highaddr */
1877 NULL, NULL, /* filter, filterarg */
1878 sc->aac_max_fibs_alloc *
1879 sc->aac_max_fib_size, /* maxsize */
1881 sc->aac_max_fibs_alloc *
1882 sc->aac_max_fib_size, /* maxsegsize */
1884 &sc->aac_fib_dmat)) {
1885 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
1890 * Create DMA tag for the common structure and allocate it.
1892 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
1893 1, 0, /* algnmnt, boundary */
1894 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1895 BUS_SPACE_MAXADDR_32BIT :
1896 0x7fffffff, /* lowaddr */
1897 BUS_SPACE_MAXADDR, /* highaddr */
1898 NULL, NULL, /* filter, filterarg */
1899 8192 + sizeof(struct aac_common), /* maxsize */
1901 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1903 &sc->aac_common_dmat)) {
1904 device_printf(sc->aac_dev,
1905 "can't allocate common structure DMA tag\n");
1908 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1909 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1910 device_printf(sc->aac_dev, "can't allocate common structure\n");
1914 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1915 * below address 8192 in physical memory.
1916 * XXX If the padding is not needed, can it be put to use instead
1919 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1920 sc->aac_common, 8192 + sizeof(*sc->aac_common),
1921 aac_common_map, sc, 0);
1923 if (sc->aac_common_busaddr < 8192) {
1925 (struct aac_common *)((uint8_t *)sc->aac_common + 8192);
1926 sc->aac_common_busaddr += 8192;
1928 bzero(sc->aac_common, sizeof(*sc->aac_common));
1930 /* Allocate some FIBs and associated command structs */
1931 TAILQ_INIT(&sc->aac_fibmap_tqh);
1932 sc->aac_commands = kmalloc(sc->aac_max_fibs * sizeof(struct aac_command),
1933 M_AACBUF, M_INTWAIT | M_ZERO);
1934 while (sc->total_fibs < AAC_PREALLOCATE_FIBS) {
1935 if (aac_alloc_commands(sc) != 0)
1938 if (sc->total_fibs == 0)
1942 * Fill in the init structure. This tells the adapter about the
1943 * physical location of various important shared data structures.
1945 ip = &sc->aac_common->ac_init;
1946 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1947 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1948 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1949 sc->flags |= AAC_FLAGS_RAW_IO;
1951 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1953 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1954 offsetof(struct aac_common, ac_fibs);
1955 ip->AdapterFibsVirtualAddress = 0;
1956 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1957 ip->AdapterFibAlign = sizeof(struct aac_fib);
1959 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1960 offsetof(struct aac_common, ac_printf);
1961 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1964 * The adapter assumes that pages are 4K in size, except on some
1965 * broken firmware versions that do the page->byte conversion twice,
1966 * therefore 'assuming' that this value is in 16MB units (2^24).
1967 * Round up since the granularity is so high.
1969 /* XXX why should the adapter care? */
1970 ip->HostPhysMemPages = ctob((int)Maxmem) / AAC_PAGE_SIZE;
1971 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1972 ip->HostPhysMemPages =
1973 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1975 ip->HostElapsedSeconds = time_second; /* reset later if invalid */
1978 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1979 ip->InitFlags = INITFLAGS_NEW_COMM_SUPPORTED;
1980 device_printf(sc->aac_dev, "New comm. interface enabled\n");
1983 ip->MaxIoCommands = sc->aac_max_fibs;
1984 ip->MaxIoSize = sc->aac_max_sectors << 9;
1985 ip->MaxFibSize = sc->aac_max_fib_size;
1988 * Initialise FIB queues. Note that it appears that the layout of the
1989 * indexes and the segmentation of the entries may be mandated by the
1990 * adapter, which is only told about the base of the queue index fields.
1992 * The initial values of the indices are assumed to inform the adapter
1993 * of the sizes of the respective queues, and theoretically it could
1994 * work out the entire layout of the queue structures from this. We
1995 * take the easy route and just lay this area out like everyone else
1998 * The Linux driver uses a much more complex scheme whereby several
1999 * header records are kept for each queue. We use a couple of generic
2000 * list manipulation functions which 'know' the size of each list by
2001 * virtue of a table.
2003 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
2004 qoffset &= ~(AAC_QUEUE_ALIGN - 1);
2006 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
2007 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
2009 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2010 AAC_HOST_NORM_CMD_ENTRIES;
2011 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2012 AAC_HOST_NORM_CMD_ENTRIES;
2013 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2014 AAC_HOST_HIGH_CMD_ENTRIES;
2015 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2016 AAC_HOST_HIGH_CMD_ENTRIES;
2017 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2018 AAC_ADAP_NORM_CMD_ENTRIES;
2019 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2020 AAC_ADAP_NORM_CMD_ENTRIES;
2021 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
2022 AAC_ADAP_HIGH_CMD_ENTRIES;
2023 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
2024 AAC_ADAP_HIGH_CMD_ENTRIES;
2025 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2026 AAC_HOST_NORM_RESP_ENTRIES;
2027 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2028 AAC_HOST_NORM_RESP_ENTRIES;
2029 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2030 AAC_HOST_HIGH_RESP_ENTRIES;
2031 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2032 AAC_HOST_HIGH_RESP_ENTRIES;
2033 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2034 AAC_ADAP_NORM_RESP_ENTRIES;
2035 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2036 AAC_ADAP_NORM_RESP_ENTRIES;
2037 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
2038 AAC_ADAP_HIGH_RESP_ENTRIES;
2039 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
2040 AAC_ADAP_HIGH_RESP_ENTRIES;
2041 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
2042 &sc->aac_queues->qt_HostNormCmdQueue[0];
2043 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
2044 &sc->aac_queues->qt_HostHighCmdQueue[0];
2045 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
2046 &sc->aac_queues->qt_AdapNormCmdQueue[0];
2047 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
2048 &sc->aac_queues->qt_AdapHighCmdQueue[0];
2049 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
2050 &sc->aac_queues->qt_HostNormRespQueue[0];
2051 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
2052 &sc->aac_queues->qt_HostHighRespQueue[0];
2053 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
2054 &sc->aac_queues->qt_AdapNormRespQueue[0];
2055 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
2056 &sc->aac_queues->qt_AdapHighRespQueue[0];
2059 * Do controller-type-specific initialisation
2061 switch (sc->aac_hwif) {
2062 case AAC_HWIF_I960RX:
2063 AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
2066 AAC_SETREG4(sc, AAC_RKT_ODBR, ~0);
2073 * Give the init structure to the controller.
2075 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
2076 sc->aac_common_busaddr +
2077 offsetof(struct aac_common, ac_init), 0, 0, 0,
2079 device_printf(sc->aac_dev,
2080 "error establishing init structure\n");
2091 * Send a synchronous command to the controller and wait for a result.
2092 * Indicate if the controller completed the command with an error status.
2095 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2096 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2104 /* populate the mailbox */
2105 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2107 /* ensure the sync command doorbell flag is cleared */
2108 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2110 /* then set it to signal the adapter */
2111 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2113 /* spin waiting for the command to complete */
2116 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
2117 debug(1, "timed out");
2120 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2122 /* clear the completion flag */
2123 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2125 /* get the command status */
2126 status = AAC_GET_MAILBOX(sc, 0);
2130 if (status != AAC_SRB_STS_SUCCESS)
2136 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2137 struct aac_fib *fib, u_int16_t datasize)
2140 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2142 if (datasize > AAC_FIB_DATASIZE)
2146 * Set up the sync FIB
2148 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2149 AAC_FIBSTATE_INITIALISED |
2151 fib->Header.XferState |= xferstate;
2152 fib->Header.Command = command;
2153 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2154 fib->Header.Size = sizeof(struct aac_fib) + datasize;
2155 fib->Header.SenderSize = sizeof(struct aac_fib);
2156 fib->Header.SenderFibAddress = 0; /* Not needed */
2157 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2158 offsetof(struct aac_common,
2162 * Give the FIB to the controller, wait for a response.
2164 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2165 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2166 debug(2, "IO error");
2174 * Adapter-space FIB queue manipulation
2176 * Note that the queue implementation here is a little funky; neither the PI or
2177 * CI will ever be zero. This behaviour is a controller feature.
2183 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2184 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
2185 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2186 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2187 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2188 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
2189 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2190 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2194 * Atomically insert an entry into the nominated queue, returns 0 on success or
2195 * EBUSY if the queue is full.
2197 * Note: it would be more efficient to defer notifying the controller in
2198 * the case where we may be inserting several entries in rapid succession,
2199 * but implementing this usefully may be difficult (it would involve a
2200 * separate queue/notify interface).
2203 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2212 fib_size = cm->cm_fib->Header.Size;
2213 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2215 /* get the producer/consumer indices */
2216 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2217 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2219 /* wrap the queue? */
2220 if (pi >= aac_qinfo[queue].size)
2223 /* check for queue full */
2224 if ((pi + 1) == ci) {
2229 * To avoid a race with its completion interrupt, place this command on
2230 * the busy queue prior to advertising it to the controller.
2232 aac_enqueue_busy(cm);
2236 /* populate queue entry */
2237 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2238 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2240 /* update producer index */
2241 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2243 /* notify the adapter if we know how */
2244 if (aac_qinfo[queue].notify != 0)
2245 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2254 * Atomically remove one entry from the nominated queue, returns 0 on
2255 * success or ENOENT if the queue is empty.
2258 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2259 struct aac_fib **fib_addr)
2262 u_int32_t fib_index;
2268 /* get the producer/consumer indices */
2269 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2270 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2272 /* check for queue empty */
2278 /* wrap the pi so the following test works */
2279 if (pi >= aac_qinfo[queue].size)
2286 /* wrap the queue? */
2287 if (ci >= aac_qinfo[queue].size)
2290 /* fetch the entry */
2291 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2294 case AAC_HOST_NORM_CMD_QUEUE:
2295 case AAC_HOST_HIGH_CMD_QUEUE:
2297 * The aq_fib_addr is only 32 bits wide so it can't be counted
2298 * on to hold an address. For AIF's, the adapter assumes
2299 * that it's giving us an address into the array of AIF fibs.
2300 * Therefore, we have to convert it to an index.
2302 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2303 sizeof(struct aac_fib);
2304 *fib_addr = &sc->aac_common->ac_fibs[fib_index];
2307 case AAC_HOST_NORM_RESP_QUEUE:
2308 case AAC_HOST_HIGH_RESP_QUEUE:
2310 struct aac_command *cm;
2313 * As above, an index is used instead of an actual address.
2314 * Gotta shift the index to account for the fast response
2315 * bit. No other correction is needed since this value was
2316 * originally provided by the driver via the SenderFibAddress
2319 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2320 cm = sc->aac_commands + (fib_index >> 2);
2321 *fib_addr = cm->cm_fib;
2324 * Is this a fast response? If it is, update the fib fields in
2325 * local memory since the whole fib isn't DMA'd back up.
2327 if (fib_index & 0x01) {
2328 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2329 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2334 panic("Invalid queue in aac_dequeue_fib()");
2338 /* update consumer index */
2339 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2341 /* if we have made the queue un-full, notify the adapter */
2342 if (notify && (aac_qinfo[queue].notify != 0))
2343 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2351 * Put our response to an Adapter Initialed Fib on the response queue
2354 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2363 /* Tell the adapter where the FIB is */
2364 fib_size = fib->Header.Size;
2365 fib_addr = fib->Header.SenderFibAddress;
2366 fib->Header.ReceiverFibAddress = fib_addr;
2368 /* get the producer/consumer indices */
2369 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2370 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2372 /* wrap the queue? */
2373 if (pi >= aac_qinfo[queue].size)
2376 /* check for queue full */
2377 if ((pi + 1) == ci) {
2382 /* populate queue entry */
2383 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2384 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2386 /* update producer index */
2387 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2389 /* notify the adapter if we know how */
2390 if (aac_qinfo[queue].notify != 0)
2391 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2400 * Check for commands that have been outstanding for a suspiciously long time,
2401 * and complain about them.
2404 aac_timeout(void *xsc)
2406 struct aac_softc *sc = xsc;
2407 struct aac_command *cm;
2411 * Traverse the busy command list, bitch about late commands once
2415 deadline = time_second - AAC_CMD_TIMEOUT;
2416 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2417 if ((cm->cm_timestamp < deadline)
2418 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
2419 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2420 device_printf(sc->aac_dev,
2421 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2422 cm, (int)(time_second-cm->cm_timestamp));
2423 AAC_PRINT_FIB(sc, cm->cm_fib);
2428 code = AAC_GET_FWSTATUS(sc);
2429 if (code != AAC_UP_AND_RUNNING) {
2430 device_printf(sc->aac_dev, "WARNING! Controller is no "
2431 "longer running! code= 0x%x\n", code);
2438 * Interface Function Vectors
2442 * Read the current firmware status word.
2445 aac_sa_get_fwstatus(struct aac_softc *sc)
2449 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2453 aac_rx_get_fwstatus(struct aac_softc *sc)
2457 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2461 aac_fa_get_fwstatus(struct aac_softc *sc)
2467 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2472 aac_rkt_get_fwstatus(struct aac_softc *sc)
2476 return(AAC_GETREG4(sc, AAC_RKT_FWSTATUS));
2480 * Notify the controller of a change in a given queue
2484 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2488 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2492 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2496 AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2500 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2504 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2509 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2513 AAC_SETREG4(sc, AAC_RKT_IDBR, qbit);
2517 * Get the interrupt reason bits
2520 aac_sa_get_istatus(struct aac_softc *sc)
2524 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2528 aac_rx_get_istatus(struct aac_softc *sc)
2532 return(AAC_GETREG4(sc, AAC_RX_ODBR));
2536 aac_fa_get_istatus(struct aac_softc *sc)
2542 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2547 aac_rkt_get_istatus(struct aac_softc *sc)
2551 return(AAC_GETREG4(sc, AAC_RKT_ODBR));
2555 * Clear some interrupt reason bits
2558 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2562 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2566 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2570 AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2574 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2578 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2583 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2587 AAC_SETREG4(sc, AAC_RKT_ODBR, mask);
2591 * Populate the mailbox and set the command word
2594 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2595 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2599 AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2600 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2601 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2602 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2603 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2607 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2608 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2612 AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2613 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2614 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2615 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2616 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2620 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2621 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2625 AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2627 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2629 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2631 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2633 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2638 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2639 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2643 AAC_SETREG4(sc, AAC_RKT_MAILBOX, command);
2644 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2645 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2646 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2647 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2651 * Fetch the immediate command status word
2654 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2658 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2662 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2666 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2670 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2676 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2681 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2685 return(AAC_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2689 * Set/clear interrupt masks
2692 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2694 debug(2, "%sable interrupts", enable ? "en" : "dis");
2697 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2699 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2704 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2706 debug(2, "%sable interrupts", enable ? "en" : "dis");
2709 if (sc->flags & AAC_FLAGS_NEW_COMM)
2710 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2712 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2714 AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2719 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2721 debug(2, "%sable interrupts", enable ? "en" : "dis");
2724 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2727 AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2733 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2735 debug(2, "%sable interrupts", enable ? "en" : "dis");
2738 if (sc->flags & AAC_FLAGS_NEW_COMM)
2739 AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2741 AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2743 AAC_SETREG4(sc, AAC_RKT_OIMR, ~0);
2748 * New comm. interface: Send command functions
2751 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2753 u_int32_t index, device;
2755 debug(2, "send command (new comm.)");
2757 index = AAC_GETREG4(sc, AAC_RX_IQUE);
2758 if (index == 0xffffffffL)
2759 index = AAC_GETREG4(sc, AAC_RX_IQUE);
2760 if (index == 0xffffffffL)
2762 aac_enqueue_busy(cm);
2764 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2766 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2768 AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2769 AAC_SETREG4(sc, AAC_RX_IQUE, index);
2774 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2776 u_int32_t index, device;
2778 debug(2, "send command (new comm.)");
2780 index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2781 if (index == 0xffffffffL)
2782 index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2783 if (index == 0xffffffffL)
2785 aac_enqueue_busy(cm);
2787 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2789 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2791 AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2792 AAC_SETREG4(sc, AAC_RKT_IQUE, index);
2797 * New comm. interface: get, set outbound queue index
2800 aac_rx_get_outb_queue(struct aac_softc *sc)
2804 return(AAC_GETREG4(sc, AAC_RX_OQUE));
2808 aac_rkt_get_outb_queue(struct aac_softc *sc)
2812 return(AAC_GETREG4(sc, AAC_RKT_OQUE));
2816 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2820 AAC_SETREG4(sc, AAC_RX_OQUE, index);
2824 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2828 AAC_SETREG4(sc, AAC_RKT_OQUE, index);
2832 * Debugging and Diagnostics
2836 * Print some information about the controller.
2839 aac_describe_controller(struct aac_softc *sc)
2841 struct aac_fib *fib;
2842 struct aac_adapter_info *info;
2846 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
2847 aac_alloc_sync_fib(sc, &fib);
2850 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2851 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2852 aac_release_sync_fib(sc);
2853 AAC_LOCK_RELEASE(&sc->aac_io_lock);
2857 /* save the kernel revision structure for later use */
2858 info = (struct aac_adapter_info *)&fib->data[0];
2859 sc->aac_revision = info->KernelRevision;
2861 device_printf(sc->aac_dev, "Adaptec Raid Controller %d.%d.%d-%d\n",
2862 AAC_DRIVER_VERSION >> 24,
2863 (AAC_DRIVER_VERSION >> 16) & 0xFF,
2864 AAC_DRIVER_VERSION & 0xFF,
2868 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2869 "(%dMB cache, %dMB execution), %s\n",
2870 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2871 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2872 info->BufferMem / (1024 * 1024),
2873 info->ExecutionMem / (1024 * 1024),
2874 aac_describe_code(aac_battery_platform,
2875 info->batteryPlatform));
2877 device_printf(sc->aac_dev,
2878 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2879 info->KernelRevision.external.comp.major,
2880 info->KernelRevision.external.comp.minor,
2881 info->KernelRevision.external.comp.dash,
2882 info->KernelRevision.buildNumber,
2883 (u_int32_t)(info->SerialNumber & 0xffffff));
2885 device_printf(sc->aac_dev, "Supported Options=%b\n",
2886 sc->supported_options,
2908 aac_release_sync_fib(sc);
2909 AAC_LOCK_RELEASE(&sc->aac_io_lock);
2913 * Look up a text description of a numeric error code and return a pointer to
2917 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2921 for (i = 0; table[i].string != NULL; i++)
2922 if (table[i].code == code)
2923 return(table[i].string);
2924 return(table[i + 1].string);
2928 * Management Interface
2932 aac_open(struct dev_open_args *ap)
2934 cdev_t dev = ap->a_head.a_dev;
2935 struct aac_softc *sc;
2941 /* Check to make sure the device isn't already open */
2942 if (sc->aac_state & AAC_STATE_OPEN) {
2945 sc->aac_state |= AAC_STATE_OPEN;
2951 aac_close(struct dev_close_args *ap)
2953 cdev_t dev = ap->a_head.a_dev;
2954 struct aac_softc *sc;
2960 /* Mark this unit as no longer open */
2961 sc->aac_state &= ~AAC_STATE_OPEN;
2967 aac_ioctl(struct dev_ioctl_args *ap)
2969 cdev_t dev = ap->a_head.a_dev;
2970 caddr_t arg = ap->a_data;
2971 struct aac_softc *sc = dev->si_drv1;
2977 if (ap->a_cmd == AACIO_STATS) {
2978 union aac_statrequest *as = (union aac_statrequest *)arg;
2980 switch (as->as_item) {
2986 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2987 sizeof(struct aac_qstat));
2996 arg = *(caddr_t *)arg;
2998 switch (ap->a_cmd) {
2999 /* AACIO_STATS already handled above */
3000 case FSACTL_SENDFIB:
3001 debug(1, "FSACTL_SENDFIB");
3002 error = aac_ioctl_sendfib(sc, arg);
3004 case FSACTL_AIF_THREAD:
3005 debug(1, "FSACTL_AIF_THREAD");
3008 case FSACTL_OPEN_GET_ADAPTER_FIB:
3009 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
3011 * Pass the caller out an AdapterFibContext.
3013 * Note that because we only support one opener, we
3014 * basically ignore this. Set the caller's context to a magic
3015 * number just in case.
3017 * The Linux code hands the driver a pointer into kernel space,
3018 * and then trusts it when the caller hands it back. Aiee!
3019 * Here, we give it the proc pointer of the per-adapter aif
3020 * thread. It's only used as a sanity check in other calls.
3022 cookie = (uint32_t)(uintptr_t)sc->aifthread;
3023 error = copyout(&cookie, arg, sizeof(cookie));
3025 case FSACTL_GET_NEXT_ADAPTER_FIB:
3026 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
3027 error = aac_getnext_aif(sc, arg);
3029 case FSACTL_CLOSE_GET_ADAPTER_FIB:
3030 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
3031 /* don't do anything here */
3033 case FSACTL_MINIPORT_REV_CHECK:
3034 debug(1, "FSACTL_MINIPORT_REV_CHECK");
3035 error = aac_rev_check(sc, arg);
3037 case FSACTL_QUERY_DISK:
3038 debug(1, "FSACTL_QUERY_DISK");
3039 error = aac_query_disk(sc, arg);
3041 case FSACTL_DELETE_DISK:
3043 * We don't trust the underland to tell us when to delete a
3044 * container, rather we rely on an AIF coming from the
3049 case FSACTL_GET_PCI_INFO:
3050 arg = *(caddr_t*)arg;
3051 case FSACTL_LNX_GET_PCI_INFO:
3052 debug(1, "FSACTL_GET_PCI_INFO");
3053 error = aac_get_pci_info(sc, arg);
3056 debug(1, "unsupported cmd 0x%lx\n", ap->a_cmd);
3064 aac_poll(struct dev_poll_args *ap)
3066 cdev_t dev = ap->a_head.a_dev;
3067 struct aac_softc *sc;
3073 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3074 if ((ap->a_events & (POLLRDNORM | POLLIN)) != 0) {
3075 if (sc->aac_aifq_tail != sc->aac_aifq_head)
3076 revents |= ap->a_events & (POLLIN | POLLRDNORM);
3078 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3081 if (ap->a_events & (POLLIN | POLLRDNORM))
3082 selrecord(curthread, &sc->rcv_select);
3084 ap->a_events = revents;
3089 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
3092 switch (event->ev_type) {
3093 case AAC_EVENT_CMFREE:
3094 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3095 if (aac_alloc_command(sc, (struct aac_command **)arg)) {
3096 aac_add_event(sc, event);
3097 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3100 kfree(event, M_AACBUF);
3102 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3110 * Send a FIB supplied from userspace
3113 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3115 struct aac_command *cm;
3125 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3126 if (aac_alloc_command(sc, &cm)) {
3127 struct aac_event *event;
3129 event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3130 M_INTWAIT | M_ZERO);
3131 event->ev_type = AAC_EVENT_CMFREE;
3132 event->ev_callback = aac_ioctl_event;
3133 event->ev_arg = &cm;
3134 aac_add_event(sc, event);
3135 tsleep_interlock(&cm, 0);
3136 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3137 tsleep(&cm, PINTERLOCKED, "sendfib", 0);
3138 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3140 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3143 * Fetch the FIB header, then re-copy to get data as well.
3145 if ((error = copyin(ufib, cm->cm_fib,
3146 sizeof(struct aac_fib_header))) != 0)
3148 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3149 if (size > sizeof(struct aac_fib)) {
3150 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %zd)\n",
3151 size, sizeof(struct aac_fib));
3152 size = sizeof(struct aac_fib);
3154 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3156 cm->cm_fib->Header.Size = size;
3157 cm->cm_timestamp = time_second;
3160 * Pass the FIB to the controller, wait for it to complete.
3162 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3163 if ((error = aac_wait_command(cm)) != 0) {
3164 device_printf(sc->aac_dev,
3165 "aac_wait_command return %d\n", error);
3168 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3171 * Copy the FIB and data back out to the caller.
3173 size = cm->cm_fib->Header.Size;
3174 if (size > sizeof(struct aac_fib)) {
3175 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %zd)\n",
3176 size, sizeof(struct aac_fib));
3177 size = sizeof(struct aac_fib);
3179 error = copyout(cm->cm_fib, ufib, size);
3180 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3184 aac_release_command(cm);
3187 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3192 * Handle an AIF sent to us by the controller; queue it for later reference.
3193 * If the queue fills up, then drop the older entries.
3196 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3198 struct aac_aif_command *aif;
3199 struct aac_container *co, *co_next;
3200 struct aac_mntinfo *mi;
3201 struct aac_mntinforesp *mir = NULL;
3204 int count = 0, added = 0, i = 0;
3208 aif = (struct aac_aif_command*)&fib->data[0];
3209 aac_print_aif(sc, aif);
3211 /* Is it an event that we should care about? */
3212 switch (aif->command) {
3213 case AifCmdEventNotify:
3214 switch (aif->data.EN.type) {
3215 case AifEnAddContainer:
3216 case AifEnDeleteContainer:
3218 * A container was added or deleted, but the message
3219 * doesn't tell us anything else! Re-enumerate the
3220 * containers and sort things out.
3222 aac_alloc_sync_fib(sc, &fib);
3223 mi = (struct aac_mntinfo *)&fib->data[0];
3226 * Ask the controller for its containers one at
3228 * XXX What if the controller's list changes
3229 * midway through this enumaration?
3230 * XXX This should be done async.
3232 bzero(mi, sizeof(struct aac_mntinfo));
3233 mi->Command = VM_NameServe;
3234 mi->MntType = FT_FILESYS;
3236 rsize = sizeof(mir);
3237 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
3238 sizeof(struct aac_mntinfo))) {
3239 device_printf(sc->aac_dev,
3240 "Error probing container %d\n", i);
3244 mir = (struct aac_mntinforesp *)&fib->data[0];
3245 /* XXX Need to check if count changed */
3246 count = mir->MntRespCount;
3249 * Check the container against our list.
3250 * co->co_found was already set to 0 in a
3253 if ((mir->Status == ST_OK) &&
3254 (mir->MntTable[0].VolType != CT_NONE)) {
3257 &sc->aac_container_tqh,
3259 if (co->co_mntobj.ObjectId ==
3260 mir->MntTable[0].ObjectId) {
3267 * If the container matched, continue
3276 * This is a new container. Do all the
3277 * appropriate things to set it up.
3279 aac_add_container(sc, mir, 1);
3283 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3284 aac_release_sync_fib(sc);
3287 * Go through our list of containers and see which ones
3288 * were not marked 'found'. Since the controller didn't
3289 * list them they must have been deleted. Do the
3290 * appropriate steps to destroy the device. Also reset
3291 * the co->co_found field.
3293 co = TAILQ_FIRST(&sc->aac_container_tqh);
3294 while (co != NULL) {
3295 if (co->co_found == 0) {
3296 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3298 device_delete_child(sc->aac_dev,
3301 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3302 co_next = TAILQ_NEXT(co, co_link);
3303 AAC_LOCK_ACQUIRE(&sc->
3304 aac_container_lock);
3305 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3307 AAC_LOCK_RELEASE(&sc->
3308 aac_container_lock);
3309 kfree(co, M_AACBUF);
3313 co = TAILQ_NEXT(co, co_link);
3317 /* Attach the newly created containers */
3319 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3321 bus_generic_attach(sc->aac_dev);
3323 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3336 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3337 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3338 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
3339 if (next != sc->aac_aifq_tail) {
3340 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
3341 sc->aac_aifq_head = next;
3343 /* On the off chance that someone is sleeping for an aif... */
3344 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3345 wakeup(sc->aac_aifq);
3346 /* token may have been lost */
3347 /* Wakeup any poll()ers */
3348 selwakeup(&sc->rcv_select);
3349 /* token may have been lost */
3351 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3357 * Return the Revision of the driver to userspace and check to see if the
3358 * userspace app is possibly compatible. This is extremely bogus since
3359 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3360 * returning what the card reported.
3363 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3365 struct aac_rev_check rev_check;
3366 struct aac_rev_check_resp rev_check_resp;
3372 * Copyin the revision struct from userspace
3374 if ((error = copyin(udata, (caddr_t)&rev_check,
3375 sizeof(struct aac_rev_check))) != 0) {
3379 debug(2, "Userland revision= %d\n",
3380 rev_check.callingRevision.buildNumber);
3383 * Doctor up the response struct.
3385 rev_check_resp.possiblyCompatible = 1;
3386 rev_check_resp.adapterSWRevision.external.ul =
3387 sc->aac_revision.external.ul;
3388 rev_check_resp.adapterSWRevision.buildNumber =
3389 sc->aac_revision.buildNumber;
3391 return(copyout((caddr_t)&rev_check_resp, udata,
3392 sizeof(struct aac_rev_check_resp)));
3396 * Pass the caller the next AIF in their queue
3399 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3401 struct get_adapter_fib_ioctl agf;
3406 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3409 * Check the magic number that we gave the caller.
3411 if (agf.AdapterFibContext != (int)(uintptr_t)sc->aifthread) {
3415 error = aac_return_aif(sc, agf.AifFib);
3417 if ((error == EAGAIN) && (agf.Wait)) {
3418 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3419 while (error == EAGAIN) {
3420 error = tsleep(sc->aac_aifq,
3421 PCATCH, "aacaif", 0);
3423 error = aac_return_aif(sc,
3426 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3434 * Hand the next AIF off the top of the queue out to userspace.
3436 * YYY token could be lost during copyout
3439 aac_return_aif(struct aac_softc *sc, caddr_t uptr)
3445 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
3446 if (sc->aac_aifq_tail == sc->aac_aifq_head) {
3447 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3451 next = (sc->aac_aifq_tail + 1) % AAC_AIFQ_LENGTH;
3452 error = copyout(&sc->aac_aifq[next], uptr,
3453 sizeof(struct aac_aif_command));
3455 device_printf(sc->aac_dev,
3456 "aac_return_aif: copyout returned %d\n", error);
3458 sc->aac_aifq_tail = next;
3460 AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
3465 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3467 struct aac_pci_info {
3475 pciinf.bus = pci_get_bus(sc->aac_dev);
3476 pciinf.slot = pci_get_slot(sc->aac_dev);
3478 error = copyout((caddr_t)&pciinf, uptr,
3479 sizeof(struct aac_pci_info));
3485 * Give the userland some information about the container. The AAC arch
3486 * expects the driver to be a SCSI passthrough type driver, so it expects
3487 * the containers to have b:t:l numbers. Fake it.
3490 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3492 struct aac_query_disk query_disk;
3493 struct aac_container *co;
3494 struct aac_disk *disk;
3501 error = copyin(uptr, (caddr_t)&query_disk,
3502 sizeof(struct aac_query_disk));
3506 id = query_disk.ContainerNumber;
3510 AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
3511 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3512 if (co->co_mntobj.ObjectId == id)
3517 query_disk.Valid = 0;
3518 query_disk.Locked = 0;
3519 query_disk.Deleted = 1; /* XXX is this right? */
3521 disk = device_get_softc(co->co_disk);
3522 query_disk.Valid = 1;
3524 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3525 query_disk.Deleted = 0;
3526 query_disk.Bus = device_get_unit(sc->aac_dev);
3527 query_disk.Target = disk->unit;
3529 query_disk.UnMapped = 0;
3530 bcopy(disk->ad_dev_t->si_name,
3531 &query_disk.diskDeviceName[0], 10);
3533 AAC_LOCK_RELEASE(&sc->aac_container_lock);
3535 error = copyout((caddr_t)&query_disk, uptr,
3536 sizeof(struct aac_query_disk));
3542 aac_get_bus_info(struct aac_softc *sc)
3544 struct aac_fib *fib;
3545 struct aac_ctcfg *c_cmd;
3546 struct aac_ctcfg_resp *c_resp;
3547 struct aac_vmioctl *vmi;
3548 struct aac_vmi_businf_resp *vmi_resp;
3549 struct aac_getbusinf businfo;
3550 struct aac_sim *caminf;
3552 int i, found, error;
3554 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
3555 aac_alloc_sync_fib(sc, &fib);
3556 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3557 bzero(c_cmd, sizeof(struct aac_ctcfg));
3559 c_cmd->Command = VM_ContainerConfig;
3560 c_cmd->cmd = CT_GET_SCSI_METHOD;
3563 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3564 sizeof(struct aac_ctcfg));
3566 device_printf(sc->aac_dev, "Error %d sending "
3567 "VM_ContainerConfig command\n", error);
3568 aac_release_sync_fib(sc);
3569 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3573 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3574 if (c_resp->Status != ST_OK) {
3575 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3577 aac_release_sync_fib(sc);
3578 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3582 sc->scsi_method_id = c_resp->param;
3584 vmi = (struct aac_vmioctl *)&fib->data[0];
3585 bzero(vmi, sizeof(struct aac_vmioctl));
3587 vmi->Command = VM_Ioctl;
3588 vmi->ObjType = FT_DRIVE;
3589 vmi->MethId = sc->scsi_method_id;
3591 vmi->IoctlCmd = GetBusInfo;
3593 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3594 sizeof(struct aac_vmioctl));
3596 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3598 aac_release_sync_fib(sc);
3599 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3603 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3604 if (vmi_resp->Status != ST_OK) {
3605 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
3606 aac_release_sync_fib(sc);
3607 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3611 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3612 aac_release_sync_fib(sc);
3613 AAC_LOCK_RELEASE(&sc->aac_io_lock);
3616 for (i = 0; i < businfo.BusCount; i++) {
3617 if (businfo.BusValid[i] != AAC_BUS_VALID)
3620 caminf = (struct aac_sim *)kmalloc(sizeof(struct aac_sim),
3621 M_AACBUF, M_INTWAIT | M_ZERO);
3623 child = device_add_child(sc->aac_dev, "aacp", -1);
3624 if (child == NULL) {
3625 device_printf(sc->aac_dev,
3626 "device_add_child failed for passthrough bus %d\n",
3628 kfree(caminf, M_AACBUF);
3632 caminf->TargetsPerBus = businfo.TargetsPerBus;
3633 caminf->BusNumber = i;
3634 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3635 caminf->aac_sc = sc;
3636 caminf->sim_dev = child;
3638 device_set_ivars(child, caminf);
3639 device_set_desc(child, "SCSI Passthrough Bus");
3640 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3646 bus_generic_attach(sc->aac_dev);