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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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>
49 #include <sys/ioccom.h>
53 #include <sys/signalvar.h>
55 #include <sys/eventhandler.h>
58 #include <machine/bus.h>
59 #include <sys/bus_dma.h>
60 #include <machine/resource.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
65 #include <dev/aac/aacreg.h>
66 #include <sys/aac_ioctl.h>
67 #include <dev/aac/aacvar.h>
68 #include <dev/aac/aac_tables.h>
70 static void aac_startup(void *arg);
71 static void aac_add_container(struct aac_softc *sc,
72 struct aac_mntinforesp *mir, int f);
73 static void aac_get_bus_info(struct aac_softc *sc);
74 static void aac_daemon(void *arg);
76 /* Command Processing */
77 static void aac_timeout(struct aac_softc *sc);
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 int aac_alloc(struct aac_softc *sc);
95 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
97 static int aac_check_firmware(struct aac_softc *sc);
98 static int aac_init(struct aac_softc *sc);
99 static int aac_sync_command(struct aac_softc *sc, u_int32_t command,
100 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
101 u_int32_t arg3, u_int32_t *sp);
102 static int aac_setup_intr(struct aac_softc *sc);
103 static int aac_enqueue_fib(struct aac_softc *sc, int queue,
104 struct aac_command *cm);
105 static int aac_dequeue_fib(struct aac_softc *sc, int queue,
106 u_int32_t *fib_size, struct aac_fib **fib_addr);
107 static int aac_enqueue_response(struct aac_softc *sc, int queue,
108 struct aac_fib *fib);
110 /* Falcon/PPC interface */
111 static int aac_fa_get_fwstatus(struct aac_softc *sc);
112 static void aac_fa_qnotify(struct aac_softc *sc, int qbit);
113 static int aac_fa_get_istatus(struct aac_softc *sc);
114 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask);
115 static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
116 u_int32_t arg0, u_int32_t arg1,
117 u_int32_t arg2, u_int32_t arg3);
118 static int aac_fa_get_mailbox(struct aac_softc *sc, int mb);
119 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable);
121 struct aac_interface aac_fa_interface = {
125 aac_fa_clear_istatus,
128 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,
154 /* i960Rx interface */
155 static int aac_rx_get_fwstatus(struct aac_softc *sc);
156 static void aac_rx_qnotify(struct aac_softc *sc, int qbit);
157 static int aac_rx_get_istatus(struct aac_softc *sc);
158 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask);
159 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
160 u_int32_t arg0, u_int32_t arg1,
161 u_int32_t arg2, u_int32_t arg3);
162 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb);
163 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable);
164 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
165 static int aac_rx_get_outb_queue(struct aac_softc *sc);
166 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
168 struct aac_interface aac_rx_interface = {
172 aac_rx_clear_istatus,
175 aac_rx_set_interrupts,
177 aac_rx_get_outb_queue,
178 aac_rx_set_outb_queue
181 /* Rocket/MIPS interface */
182 static int aac_rkt_get_fwstatus(struct aac_softc *sc);
183 static void aac_rkt_qnotify(struct aac_softc *sc, int qbit);
184 static int aac_rkt_get_istatus(struct aac_softc *sc);
185 static void aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
186 static void aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
187 u_int32_t arg0, u_int32_t arg1,
188 u_int32_t arg2, u_int32_t arg3);
189 static int aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
190 static void aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
191 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
192 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
193 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
195 struct aac_interface aac_rkt_interface = {
196 aac_rkt_get_fwstatus,
199 aac_rkt_clear_istatus,
202 aac_rkt_set_interrupts,
203 aac_rkt_send_command,
204 aac_rkt_get_outb_queue,
205 aac_rkt_set_outb_queue
208 /* Debugging and Diagnostics */
209 static void aac_describe_controller(struct aac_softc *sc);
210 static char *aac_describe_code(struct aac_code_lookup *table,
213 /* Management Interface */
214 static d_open_t aac_open;
215 static d_close_t aac_close;
216 static d_ioctl_t aac_ioctl;
217 static d_poll_t aac_poll;
218 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
219 static int aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg);
220 static void aac_handle_aif(struct aac_softc *sc,
221 struct aac_fib *fib);
222 static int aac_rev_check(struct aac_softc *sc, caddr_t udata);
223 static int aac_open_aif(struct aac_softc *sc, caddr_t arg);
224 static int aac_close_aif(struct aac_softc *sc, caddr_t arg);
225 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
226 static int aac_return_aif(struct aac_softc *sc,
227 struct aac_fib_context *ctx, caddr_t uptr);
228 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr);
229 static int aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
230 static int aac_supported_features(struct aac_softc *sc, caddr_t uptr);
231 static void aac_ioctl_event(struct aac_softc *sc,
232 struct aac_event *event, void *arg);
233 static struct aac_mntinforesp *
234 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid);
236 static struct cdevsw aac_cdevsw = {
237 .d_version = D_VERSION,
238 .d_flags = D_NEEDGIANT,
240 .d_close = aac_close,
241 .d_ioctl = aac_ioctl,
246 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
249 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
256 * Initialize the controller and softc
259 aac_attach(struct aac_softc *sc)
263 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
266 * Initialize per-controller queues.
274 * Initialize command-completion task.
276 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
278 /* mark controller as suspended until we get ourselves organised */
279 sc->aac_state |= AAC_STATE_SUSPEND;
282 * Check that the firmware on the card is supported.
284 if ((error = aac_check_firmware(sc)) != 0)
290 mtx_init(&sc->aac_aifq_lock, "AAC AIF lock", NULL, MTX_DEF);
291 mtx_init(&sc->aac_io_lock, "AAC I/O lock", NULL, MTX_DEF);
292 mtx_init(&sc->aac_container_lock, "AAC container lock", NULL, MTX_DEF);
293 TAILQ_INIT(&sc->aac_container_tqh);
294 TAILQ_INIT(&sc->aac_ev_cmfree);
296 /* Initialize the clock daemon callout. */
297 callout_init_mtx(&sc->aac_daemontime, &sc->aac_io_lock, 0);
300 * Initialize the adapter.
302 if ((error = aac_alloc(sc)) != 0)
304 if ((error = aac_init(sc)) != 0)
308 * Allocate and connect our interrupt.
310 if ((error = aac_setup_intr(sc)) != 0)
314 * Print a little information about the controller.
316 aac_describe_controller(sc);
319 * Register to probe our containers later.
321 sc->aac_ich.ich_func = aac_startup;
322 sc->aac_ich.ich_arg = sc;
323 if (config_intrhook_establish(&sc->aac_ich) != 0) {
324 device_printf(sc->aac_dev,
325 "can't establish configuration hook\n");
330 * Make the control device.
332 unit = device_get_unit(sc->aac_dev);
333 sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR,
334 0640, "aac%d", unit);
335 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
336 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
337 sc->aac_dev_t->si_drv1 = sc;
339 /* Create the AIF thread */
340 if (kproc_create((void(*)(void *))aac_command_thread, sc,
341 &sc->aifthread, 0, 0, "aac%daif", unit))
342 panic("Could not create AIF thread");
344 /* Register the shutdown method to only be called post-dump */
345 if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
346 sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
347 device_printf(sc->aac_dev,
348 "shutdown event registration failed\n");
350 /* Register with CAM for the non-DASD devices */
351 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
352 TAILQ_INIT(&sc->aac_sim_tqh);
353 aac_get_bus_info(sc);
356 mtx_lock(&sc->aac_io_lock);
357 callout_reset(&sc->aac_daemontime, 30 * 60 * hz, aac_daemon, sc);
358 mtx_unlock(&sc->aac_io_lock);
364 aac_daemon(void *arg)
367 struct aac_softc *sc;
371 mtx_assert(&sc->aac_io_lock, MA_OWNED);
373 if (callout_pending(&sc->aac_daemontime) ||
374 callout_active(&sc->aac_daemontime) == 0)
377 aac_alloc_sync_fib(sc, &fib);
378 *(uint32_t *)fib->data = tv.tv_sec;
379 aac_sync_fib(sc, SendHostTime, 0, fib, sizeof(uint32_t));
380 aac_release_sync_fib(sc);
381 callout_schedule(&sc->aac_daemontime, 30 * 60 * hz);
385 aac_add_event(struct aac_softc *sc, struct aac_event *event)
388 switch (event->ev_type & AAC_EVENT_MASK) {
389 case AAC_EVENT_CMFREE:
390 TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
393 device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
402 * Request information of container #cid
404 static struct aac_mntinforesp *
405 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid)
407 struct aac_mntinfo *mi;
409 mi = (struct aac_mntinfo *)&fib->data[0];
410 /* use 64-bit LBA if enabled */
411 mi->Command = (sc->flags & AAC_FLAGS_LBA_64BIT) ?
412 VM_NameServe64 : VM_NameServe;
413 mi->MntType = FT_FILESYS;
416 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
417 sizeof(struct aac_mntinfo))) {
418 printf("Error probing container %d\n", cid);
422 return ((struct aac_mntinforesp *)&fib->data[0]);
426 * Probe for containers, create disks.
429 aac_startup(void *arg)
431 struct aac_softc *sc;
433 struct aac_mntinforesp *mir;
434 int count = 0, i = 0;
436 sc = (struct aac_softc *)arg;
437 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
439 /* disconnect ourselves from the intrhook chain */
440 config_intrhook_disestablish(&sc->aac_ich);
442 mtx_lock(&sc->aac_io_lock);
443 aac_alloc_sync_fib(sc, &fib);
445 /* loop over possible containers */
447 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
450 count = mir->MntRespCount;
451 aac_add_container(sc, mir, 0);
453 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
455 aac_release_sync_fib(sc);
456 mtx_unlock(&sc->aac_io_lock);
458 /* poke the bus to actually attach the child devices */
459 if (bus_generic_attach(sc->aac_dev))
460 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
462 /* mark the controller up */
463 sc->aac_state &= ~AAC_STATE_SUSPEND;
465 /* enable interrupts now */
466 AAC_UNMASK_INTERRUPTS(sc);
470 * Create a device to represent a new container
473 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
475 struct aac_container *co;
479 * Check container volume type for validity. Note that many of
480 * the possible types may never show up.
482 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
483 co = (struct aac_container *)malloc(sizeof *co, M_AACBUF,
486 panic("Out of memory?!");
487 fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "id %x name '%.16s' size %u type %d",
488 mir->MntTable[0].ObjectId,
489 mir->MntTable[0].FileSystemName,
490 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
492 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
493 device_printf(sc->aac_dev, "device_add_child failed\n");
495 device_set_ivars(child, co);
496 device_set_desc(child, aac_describe_code(aac_container_types,
497 mir->MntTable[0].VolType));
500 bcopy(&mir->MntTable[0], &co->co_mntobj,
501 sizeof(struct aac_mntobj));
502 mtx_lock(&sc->aac_container_lock);
503 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
504 mtx_unlock(&sc->aac_container_lock);
509 * Allocate resources associated with (sc)
512 aac_alloc(struct aac_softc *sc)
515 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
518 * Create DMA tag for mapping buffers into controller-addressable space.
520 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
521 1, 0, /* algnmnt, boundary */
522 (sc->flags & AAC_FLAGS_SG_64BIT) ?
524 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
525 BUS_SPACE_MAXADDR, /* highaddr */
526 NULL, NULL, /* filter, filterarg */
527 MAXBSIZE, /* maxsize */
528 sc->aac_sg_tablesize, /* nsegments */
529 MAXBSIZE, /* maxsegsize */
530 BUS_DMA_ALLOCNOW, /* flags */
531 busdma_lock_mutex, /* lockfunc */
532 &sc->aac_io_lock, /* lockfuncarg */
533 &sc->aac_buffer_dmat)) {
534 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
539 * Create DMA tag for mapping FIBs into controller-addressable space..
541 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
542 1, 0, /* algnmnt, boundary */
543 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
544 BUS_SPACE_MAXADDR_32BIT :
545 0x7fffffff, /* lowaddr */
546 BUS_SPACE_MAXADDR, /* highaddr */
547 NULL, NULL, /* filter, filterarg */
548 sc->aac_max_fibs_alloc *
549 sc->aac_max_fib_size, /* maxsize */
551 sc->aac_max_fibs_alloc *
552 sc->aac_max_fib_size, /* maxsize */
554 NULL, NULL, /* No locking needed */
555 &sc->aac_fib_dmat)) {
556 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
561 * Create DMA tag for the common structure and allocate it.
563 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
564 1, 0, /* algnmnt, boundary */
565 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
566 BUS_SPACE_MAXADDR_32BIT :
567 0x7fffffff, /* lowaddr */
568 BUS_SPACE_MAXADDR, /* highaddr */
569 NULL, NULL, /* filter, filterarg */
570 8192 + sizeof(struct aac_common), /* maxsize */
572 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
574 NULL, NULL, /* No locking needed */
575 &sc->aac_common_dmat)) {
576 device_printf(sc->aac_dev,
577 "can't allocate common structure DMA tag\n");
580 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
581 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
582 device_printf(sc->aac_dev, "can't allocate common structure\n");
587 * Work around a bug in the 2120 and 2200 that cannot DMA commands
588 * below address 8192 in physical memory.
589 * XXX If the padding is not needed, can it be put to use instead
592 (void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
593 sc->aac_common, 8192 + sizeof(*sc->aac_common),
594 aac_common_map, sc, 0);
596 if (sc->aac_common_busaddr < 8192) {
597 sc->aac_common = (struct aac_common *)
598 ((uint8_t *)sc->aac_common + 8192);
599 sc->aac_common_busaddr += 8192;
601 bzero(sc->aac_common, sizeof(*sc->aac_common));
603 /* Allocate some FIBs and associated command structs */
604 TAILQ_INIT(&sc->aac_fibmap_tqh);
605 sc->aac_commands = malloc(sc->aac_max_fibs * sizeof(struct aac_command),
606 M_AACBUF, M_WAITOK|M_ZERO);
607 while (sc->total_fibs < AAC_PREALLOCATE_FIBS) {
608 if (aac_alloc_commands(sc) != 0)
611 if (sc->total_fibs == 0)
618 * Free all of the resources associated with (sc)
620 * Should not be called if the controller is active.
623 aac_free(struct aac_softc *sc)
626 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
628 /* remove the control device */
629 if (sc->aac_dev_t != NULL)
630 destroy_dev(sc->aac_dev_t);
632 /* throw away any FIB buffers, discard the FIB DMA tag */
633 aac_free_commands(sc);
634 if (sc->aac_fib_dmat)
635 bus_dma_tag_destroy(sc->aac_fib_dmat);
637 free(sc->aac_commands, M_AACBUF);
639 /* destroy the common area */
640 if (sc->aac_common) {
641 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
642 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
643 sc->aac_common_dmamap);
645 if (sc->aac_common_dmat)
646 bus_dma_tag_destroy(sc->aac_common_dmat);
648 /* disconnect the interrupt handler */
650 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
651 if (sc->aac_irq != NULL)
652 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
655 /* destroy data-transfer DMA tag */
656 if (sc->aac_buffer_dmat)
657 bus_dma_tag_destroy(sc->aac_buffer_dmat);
659 /* destroy the parent DMA tag */
660 if (sc->aac_parent_dmat)
661 bus_dma_tag_destroy(sc->aac_parent_dmat);
663 /* release the register window mapping */
664 if (sc->aac_regs_res0 != NULL)
665 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
666 sc->aac_regs_rid0, sc->aac_regs_res0);
667 if (sc->aac_hwif == AAC_HWIF_NARK && sc->aac_regs_res1 != NULL)
668 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
669 sc->aac_regs_rid1, sc->aac_regs_res1);
673 * Disconnect from the controller completely, in preparation for unload.
676 aac_detach(device_t dev)
678 struct aac_softc *sc;
679 struct aac_container *co;
683 sc = device_get_softc(dev);
684 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
686 if (sc->aac_state & AAC_STATE_OPEN)
689 callout_drain(&sc->aac_daemontime);
691 /* Remove the child containers */
692 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
693 error = device_delete_child(dev, co->co_disk);
696 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
700 /* Remove the CAM SIMs */
701 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
702 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
703 error = device_delete_child(dev, sim->sim_dev);
709 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
710 sc->aifflags |= AAC_AIFFLAGS_EXIT;
711 wakeup(sc->aifthread);
712 tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
715 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
716 panic("Cannot shutdown AIF thread");
718 if ((error = aac_shutdown(dev)))
721 EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
725 mtx_destroy(&sc->aac_aifq_lock);
726 mtx_destroy(&sc->aac_io_lock);
727 mtx_destroy(&sc->aac_container_lock);
733 * Bring the controller down to a dormant state and detach all child devices.
735 * This function is called before detach or system shutdown.
737 * Note that we can assume that the bioq on the controller is empty, as we won't
738 * allow shutdown if any device is open.
741 aac_shutdown(device_t dev)
743 struct aac_softc *sc;
745 struct aac_close_command *cc;
747 sc = device_get_softc(dev);
748 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
750 sc->aac_state |= AAC_STATE_SUSPEND;
753 * Send a Container shutdown followed by a HostShutdown FIB to the
754 * controller to convince it that we don't want to talk to it anymore.
755 * We've been closed and all I/O completed already
757 device_printf(sc->aac_dev, "shutting down controller...");
759 mtx_lock(&sc->aac_io_lock);
760 aac_alloc_sync_fib(sc, &fib);
761 cc = (struct aac_close_command *)&fib->data[0];
763 bzero(cc, sizeof(struct aac_close_command));
764 cc->Command = VM_CloseAll;
765 cc->ContainerId = 0xffffffff;
766 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
767 sizeof(struct aac_close_command)))
775 * XXX Issuing this command to the controller makes it shut down
776 * but also keeps it from coming back up without a reset of the
777 * PCI bus. This is not desirable if you are just unloading the
778 * driver module with the intent to reload it later.
780 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
789 AAC_MASK_INTERRUPTS(sc);
790 aac_release_sync_fib(sc);
791 mtx_unlock(&sc->aac_io_lock);
797 * Bring the controller to a quiescent state, ready for system suspend.
800 aac_suspend(device_t dev)
802 struct aac_softc *sc;
804 sc = device_get_softc(dev);
806 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
807 sc->aac_state |= AAC_STATE_SUSPEND;
809 AAC_MASK_INTERRUPTS(sc);
814 * Bring the controller back to a state ready for operation.
817 aac_resume(device_t dev)
819 struct aac_softc *sc;
821 sc = device_get_softc(dev);
823 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
824 sc->aac_state &= ~AAC_STATE_SUSPEND;
825 AAC_UNMASK_INTERRUPTS(sc);
830 * Interrupt handler for NEW_COMM interface.
833 aac_new_intr(void *arg)
835 struct aac_softc *sc;
836 u_int32_t index, fast;
837 struct aac_command *cm;
841 sc = (struct aac_softc *)arg;
843 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
844 mtx_lock(&sc->aac_io_lock);
846 index = AAC_GET_OUTB_QUEUE(sc);
847 if (index == 0xffffffff)
848 index = AAC_GET_OUTB_QUEUE(sc);
849 if (index == 0xffffffff)
852 if (index == 0xfffffffe) {
853 /* XXX This means that the controller wants
854 * more work. Ignore it for now.
859 fib = (struct aac_fib *)malloc(sizeof *fib, M_AACBUF,
862 /* If we're really this short on memory,
863 * hopefully breaking out of the handler will
864 * allow something to get freed. This
865 * actually sucks a whole lot.
870 for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
871 ((u_int32_t *)fib)[i] = AAC_MEM1_GETREG4(sc, index + i*4);
872 aac_handle_aif(sc, fib);
876 * AIF memory is owned by the adapter, so let it
877 * know that we are done with it.
879 AAC_SET_OUTB_QUEUE(sc, index);
880 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
883 cm = sc->aac_commands + (index >> 2);
886 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
887 *((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
890 aac_unmap_command(cm);
891 cm->cm_flags |= AAC_CMD_COMPLETED;
893 /* is there a completion handler? */
894 if (cm->cm_complete != NULL) {
897 /* assume that someone is sleeping on this
902 sc->flags &= ~AAC_QUEUE_FRZN;
905 /* see if we can start some more I/O */
906 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
909 mtx_unlock(&sc->aac_io_lock);
913 aac_fast_intr(void *arg)
915 struct aac_softc *sc;
918 sc = (struct aac_softc *)arg;
920 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
922 * Read the status register directly. This is faster than taking the
923 * driver lock and reading the queues directly. It also saves having
924 * to turn parts of the driver lock into a spin mutex, which would be
927 reason = AAC_GET_ISTATUS(sc);
928 AAC_CLEAR_ISTATUS(sc, reason);
930 /* handle completion processing */
931 if (reason & AAC_DB_RESPONSE_READY)
932 taskqueue_enqueue_fast(taskqueue_fast, &sc->aac_task_complete);
934 /* controller wants to talk to us */
935 if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
937 * XXX Make sure that we don't get fooled by strange messages
938 * that start with a NULL.
940 if ((reason & AAC_DB_PRINTF) &&
941 (sc->aac_common->ac_printf[0] == 0))
942 sc->aac_common->ac_printf[0] = 32;
945 * This might miss doing the actual wakeup. However, the
946 * msleep that this is waking up has a timeout, so it will
947 * wake up eventually. AIFs and printfs are low enough
948 * priority that they can handle hanging out for a few seconds
951 wakeup(sc->aifthread);
953 return (FILTER_HANDLED);
961 * Start as much queued I/O as possible on the controller
964 aac_startio(struct aac_softc *sc)
966 struct aac_command *cm;
969 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
973 * This flag might be set if the card is out of resources.
974 * Checking it here prevents an infinite loop of deferrals.
976 if (sc->flags & AAC_QUEUE_FRZN)
980 * Try to get a command that's been put off for lack of
983 cm = aac_dequeue_ready(sc);
986 * Try to build a command off the bio queue (ignore error
990 aac_bio_command(sc, &cm);
996 /* don't map more than once */
997 if (cm->cm_flags & AAC_CMD_MAPPED)
998 panic("aac: command %p already mapped", cm);
1001 * Set up the command to go to the controller. If there are no
1002 * data buffers associated with the command then it can bypass
1005 if (cm->cm_datalen != 0) {
1006 error = bus_dmamap_load(sc->aac_buffer_dmat,
1007 cm->cm_datamap, cm->cm_data,
1009 aac_map_command_sg, cm, 0);
1010 if (error == EINPROGRESS) {
1011 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "freezing queue\n");
1012 sc->flags |= AAC_QUEUE_FRZN;
1014 } else if (error != 0)
1015 panic("aac_startio: unexpected error %d from "
1018 aac_map_command_sg(cm, NULL, 0, 0);
1023 * Handle notification of one or more FIBs coming from the controller.
1026 aac_command_thread(struct aac_softc *sc)
1028 struct aac_fib *fib;
1032 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1034 mtx_lock(&sc->aac_io_lock);
1035 sc->aifflags = AAC_AIFFLAGS_RUNNING;
1037 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1040 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1041 retval = msleep(sc->aifthread, &sc->aac_io_lock, PRIBIO,
1042 "aifthd", AAC_PERIODIC_INTERVAL * hz);
1045 * First see if any FIBs need to be allocated. This needs
1046 * to be called without the driver lock because contigmalloc
1047 * will grab Giant, and would result in an LOR.
1049 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1050 mtx_unlock(&sc->aac_io_lock);
1051 aac_alloc_commands(sc);
1052 mtx_lock(&sc->aac_io_lock);
1053 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1058 * While we're here, check to see if any commands are stuck.
1059 * This is pretty low-priority, so it's ok if it doesn't
1062 if (retval == EWOULDBLOCK)
1065 /* Check the hardware printf message buffer */
1066 if (sc->aac_common->ac_printf[0] != 0)
1067 aac_print_printf(sc);
1069 /* Also check to see if the adapter has a command for us. */
1070 if (sc->flags & AAC_FLAGS_NEW_COMM)
1073 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
1077 AAC_PRINT_FIB(sc, fib);
1079 switch (fib->Header.Command) {
1081 aac_handle_aif(sc, fib);
1084 device_printf(sc->aac_dev, "unknown command "
1085 "from controller\n");
1089 if ((fib->Header.XferState == 0) ||
1090 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
1094 /* Return the AIF to the controller. */
1095 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
1096 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
1097 *(AAC_FSAStatus*)fib->data = ST_OK;
1099 /* XXX Compute the Size field? */
1100 size = fib->Header.Size;
1101 if (size > sizeof(struct aac_fib)) {
1102 size = sizeof(struct aac_fib);
1103 fib->Header.Size = size;
1106 * Since we did not generate this command, it
1107 * cannot go through the normal
1108 * enqueue->startio chain.
1110 aac_enqueue_response(sc,
1111 AAC_ADAP_NORM_RESP_QUEUE,
1116 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1117 mtx_unlock(&sc->aac_io_lock);
1118 wakeup(sc->aac_dev);
1124 * Process completed commands.
1127 aac_complete(void *context, int pending)
1129 struct aac_softc *sc;
1130 struct aac_command *cm;
1131 struct aac_fib *fib;
1134 sc = (struct aac_softc *)context;
1135 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1137 mtx_lock(&sc->aac_io_lock);
1139 /* pull completed commands off the queue */
1141 /* look for completed FIBs on our queue */
1142 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1144 break; /* nothing to do */
1146 /* get the command, unmap and hand off for processing */
1147 cm = sc->aac_commands + fib->Header.SenderData;
1149 AAC_PRINT_FIB(sc, fib);
1152 aac_remove_busy(cm);
1154 aac_unmap_command(cm);
1155 cm->cm_flags |= AAC_CMD_COMPLETED;
1157 /* is there a completion handler? */
1158 if (cm->cm_complete != NULL) {
1159 cm->cm_complete(cm);
1161 /* assume that someone is sleeping on this command */
1166 /* see if we can start some more I/O */
1167 sc->flags &= ~AAC_QUEUE_FRZN;
1170 mtx_unlock(&sc->aac_io_lock);
1174 * Handle a bio submitted from a disk device.
1177 aac_submit_bio(struct bio *bp)
1179 struct aac_disk *ad;
1180 struct aac_softc *sc;
1182 ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1183 sc = ad->ad_controller;
1184 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1186 /* queue the BIO and try to get some work done */
1187 aac_enqueue_bio(sc, bp);
1192 * Get a bio and build a command to go with it.
1195 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1197 struct aac_command *cm;
1198 struct aac_fib *fib;
1199 struct aac_disk *ad;
1202 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1204 /* get the resources we will need */
1207 if (aac_alloc_command(sc, &cm)) /* get a command */
1209 if ((bp = aac_dequeue_bio(sc)) == NULL)
1212 /* fill out the command */
1213 cm->cm_data = (void *)bp->bio_data;
1214 cm->cm_datalen = bp->bio_bcount;
1215 cm->cm_complete = aac_bio_complete;
1216 cm->cm_private = bp;
1217 cm->cm_timestamp = time_uptime;
1218 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1222 fib->Header.Size = sizeof(struct aac_fib_header);
1223 fib->Header.XferState =
1224 AAC_FIBSTATE_HOSTOWNED |
1225 AAC_FIBSTATE_INITIALISED |
1226 AAC_FIBSTATE_EMPTY |
1227 AAC_FIBSTATE_FROMHOST |
1228 AAC_FIBSTATE_REXPECTED |
1230 AAC_FIBSTATE_ASYNC |
1231 AAC_FIBSTATE_FAST_RESPONSE;
1233 /* build the read/write request */
1234 ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1236 if (sc->flags & AAC_FLAGS_RAW_IO) {
1237 struct aac_raw_io *raw;
1238 raw = (struct aac_raw_io *)&fib->data[0];
1239 fib->Header.Command = RawIo;
1240 raw->BlockNumber = (u_int64_t)bp->bio_pblkno;
1241 raw->ByteCount = bp->bio_bcount;
1242 raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1244 raw->BpComplete = 0;
1245 fib->Header.Size += sizeof(struct aac_raw_io);
1246 cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1247 if (bp->bio_cmd == BIO_READ) {
1249 cm->cm_flags |= AAC_CMD_DATAIN;
1252 cm->cm_flags |= AAC_CMD_DATAOUT;
1254 } else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1255 fib->Header.Command = ContainerCommand;
1256 if (bp->bio_cmd == BIO_READ) {
1257 struct aac_blockread *br;
1258 br = (struct aac_blockread *)&fib->data[0];
1259 br->Command = VM_CtBlockRead;
1260 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1261 br->BlockNumber = bp->bio_pblkno;
1262 br->ByteCount = bp->bio_bcount;
1263 fib->Header.Size += sizeof(struct aac_blockread);
1264 cm->cm_sgtable = &br->SgMap;
1265 cm->cm_flags |= AAC_CMD_DATAIN;
1267 struct aac_blockwrite *bw;
1268 bw = (struct aac_blockwrite *)&fib->data[0];
1269 bw->Command = VM_CtBlockWrite;
1270 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1271 bw->BlockNumber = bp->bio_pblkno;
1272 bw->ByteCount = bp->bio_bcount;
1273 bw->Stable = CUNSTABLE;
1274 fib->Header.Size += sizeof(struct aac_blockwrite);
1275 cm->cm_flags |= AAC_CMD_DATAOUT;
1276 cm->cm_sgtable = &bw->SgMap;
1279 fib->Header.Command = ContainerCommand64;
1280 if (bp->bio_cmd == BIO_READ) {
1281 struct aac_blockread64 *br;
1282 br = (struct aac_blockread64 *)&fib->data[0];
1283 br->Command = VM_CtHostRead64;
1284 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1285 br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1286 br->BlockNumber = bp->bio_pblkno;
1289 fib->Header.Size += sizeof(struct aac_blockread64);
1290 cm->cm_flags |= AAC_CMD_DATAIN;
1291 cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1293 struct aac_blockwrite64 *bw;
1294 bw = (struct aac_blockwrite64 *)&fib->data[0];
1295 bw->Command = VM_CtHostWrite64;
1296 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1297 bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1298 bw->BlockNumber = bp->bio_pblkno;
1301 fib->Header.Size += sizeof(struct aac_blockwrite64);
1302 cm->cm_flags |= AAC_CMD_DATAOUT;
1303 cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1312 aac_enqueue_bio(sc, bp);
1314 aac_release_command(cm);
1319 * Handle a bio-instigated command that has been completed.
1322 aac_bio_complete(struct aac_command *cm)
1324 struct aac_blockread_response *brr;
1325 struct aac_blockwrite_response *bwr;
1327 AAC_FSAStatus status;
1329 /* fetch relevant status and then release the command */
1330 bp = (struct bio *)cm->cm_private;
1331 if (bp->bio_cmd == BIO_READ) {
1332 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1333 status = brr->Status;
1335 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1336 status = bwr->Status;
1338 aac_release_command(cm);
1340 /* fix up the bio based on status */
1341 if (status == ST_OK) {
1344 bp->bio_error = EIO;
1345 bp->bio_flags |= BIO_ERROR;
1346 /* pass an error string out to the disk layer */
1347 bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1354 * Submit a command to the controller, return when it completes.
1355 * XXX This is very dangerous! If the card has gone out to lunch, we could
1356 * be stuck here forever. At the same time, signals are not caught
1357 * because there is a risk that a signal could wakeup the sleep before
1358 * the card has a chance to complete the command. Since there is no way
1359 * to cancel a command that is in progress, we can't protect against the
1360 * card completing a command late and spamming the command and data
1361 * memory. So, we are held hostage until the command completes.
1364 aac_wait_command(struct aac_command *cm)
1366 struct aac_softc *sc;
1370 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1372 /* Put the command on the ready queue and get things going */
1373 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1374 aac_enqueue_ready(cm);
1376 error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0);
1381 *Command Buffer Management
1385 * Allocate a command.
1388 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1390 struct aac_command *cm;
1392 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1394 if ((cm = aac_dequeue_free(sc)) == NULL) {
1395 if (sc->total_fibs < sc->aac_max_fibs) {
1396 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1397 wakeup(sc->aifthread);
1407 * Release a command back to the freelist.
1410 aac_release_command(struct aac_command *cm)
1412 struct aac_event *event;
1413 struct aac_softc *sc;
1416 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1418 /* (re)initialize the command/FIB */
1419 cm->cm_sgtable = NULL;
1421 cm->cm_complete = NULL;
1422 cm->cm_private = NULL;
1423 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1424 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1425 cm->cm_fib->Header.Flags = 0;
1426 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1429 * These are duplicated in aac_start to cover the case where an
1430 * intermediate stage may have destroyed them. They're left
1431 * initialized here for debugging purposes only.
1433 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1434 cm->cm_fib->Header.SenderData = 0;
1436 aac_enqueue_free(cm);
1439 * Dequeue all events so that there's no risk of events getting
1442 while ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1443 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1444 event->ev_callback(sc, event, event->ev_arg);
1449 * Map helper for command/FIB allocation.
1452 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1456 fibphys = (uint64_t *)arg;
1458 *fibphys = segs[0].ds_addr;
1462 * Allocate and initialize commands/FIBs for this adapter.
1465 aac_alloc_commands(struct aac_softc *sc)
1467 struct aac_command *cm;
1468 struct aac_fibmap *fm;
1472 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1474 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1477 fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO);
1481 /* allocate the FIBs in DMAable memory and load them */
1482 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1483 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1484 device_printf(sc->aac_dev,
1485 "Not enough contiguous memory available.\n");
1490 /* Ignore errors since this doesn't bounce */
1491 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1492 sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1493 aac_map_command_helper, &fibphys, 0);
1495 /* initialize constant fields in the command structure */
1496 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1497 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1498 cm = sc->aac_commands + sc->total_fibs;
1499 fm->aac_commands = cm;
1501 cm->cm_fib = (struct aac_fib *)
1502 ((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1503 cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1504 cm->cm_index = sc->total_fibs;
1506 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1507 &cm->cm_datamap)) != 0)
1509 mtx_lock(&sc->aac_io_lock);
1510 aac_release_command(cm);
1512 mtx_unlock(&sc->aac_io_lock);
1516 mtx_lock(&sc->aac_io_lock);
1517 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1518 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1519 mtx_unlock(&sc->aac_io_lock);
1523 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1524 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1530 * Free FIBs owned by this adapter.
1533 aac_free_commands(struct aac_softc *sc)
1535 struct aac_fibmap *fm;
1536 struct aac_command *cm;
1539 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1541 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1543 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1545 * We check against total_fibs to handle partially
1548 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1549 cm = fm->aac_commands + i;
1550 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1552 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1553 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1559 * Command-mapping helper function - populate this command's s/g table.
1562 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1564 struct aac_softc *sc;
1565 struct aac_command *cm;
1566 struct aac_fib *fib;
1569 cm = (struct aac_command *)arg;
1572 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1574 /* copy into the FIB */
1575 if (cm->cm_sgtable != NULL) {
1576 if (fib->Header.Command == RawIo) {
1577 struct aac_sg_tableraw *sg;
1578 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1580 for (i = 0; i < nseg; i++) {
1581 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1582 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1583 sg->SgEntryRaw[i].Next = 0;
1584 sg->SgEntryRaw[i].Prev = 0;
1585 sg->SgEntryRaw[i].Flags = 0;
1587 /* update the FIB size for the s/g count */
1588 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1589 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1590 struct aac_sg_table *sg;
1591 sg = cm->cm_sgtable;
1593 for (i = 0; i < nseg; i++) {
1594 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1595 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1597 /* update the FIB size for the s/g count */
1598 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1600 struct aac_sg_table64 *sg;
1601 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1603 for (i = 0; i < nseg; i++) {
1604 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1605 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1607 /* update the FIB size for the s/g count */
1608 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1612 /* Fix up the address values in the FIB. Use the command array index
1613 * instead of a pointer since these fields are only 32 bits. Shift
1614 * the SenderFibAddress over to make room for the fast response bit
1615 * and for the AIF bit
1617 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1618 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1620 /* save a pointer to the command for speedy reverse-lookup */
1621 cm->cm_fib->Header.SenderData = cm->cm_index;
1623 if (cm->cm_flags & AAC_CMD_DATAIN)
1624 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1625 BUS_DMASYNC_PREREAD);
1626 if (cm->cm_flags & AAC_CMD_DATAOUT)
1627 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1628 BUS_DMASYNC_PREWRITE);
1629 cm->cm_flags |= AAC_CMD_MAPPED;
1631 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1632 int count = 10000000L;
1633 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1635 aac_unmap_command(cm);
1636 sc->flags |= AAC_QUEUE_FRZN;
1637 aac_requeue_ready(cm);
1639 DELAY(5); /* wait 5 usec. */
1642 /* Put the FIB on the outbound queue */
1643 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1644 aac_unmap_command(cm);
1645 sc->flags |= AAC_QUEUE_FRZN;
1646 aac_requeue_ready(cm);
1654 * Unmap a command from controller-visible space.
1657 aac_unmap_command(struct aac_command *cm)
1659 struct aac_softc *sc;
1662 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1664 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1667 if (cm->cm_datalen != 0) {
1668 if (cm->cm_flags & AAC_CMD_DATAIN)
1669 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1670 BUS_DMASYNC_POSTREAD);
1671 if (cm->cm_flags & AAC_CMD_DATAOUT)
1672 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1673 BUS_DMASYNC_POSTWRITE);
1675 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1677 cm->cm_flags &= ~AAC_CMD_MAPPED;
1681 * Hardware Interface
1685 * Initialize the adapter.
1688 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1690 struct aac_softc *sc;
1692 sc = (struct aac_softc *)arg;
1693 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1695 sc->aac_common_busaddr = segs[0].ds_addr;
1699 aac_check_firmware(struct aac_softc *sc)
1701 u_int32_t code, major, minor, options = 0, atu_size = 0;
1705 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1707 * Wait for the adapter to come ready.
1711 code = AAC_GET_FWSTATUS(sc);
1712 if (code & AAC_SELF_TEST_FAILED) {
1713 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1716 if (code & AAC_KERNEL_PANIC) {
1717 device_printf(sc->aac_dev,
1718 "FATAL: controller kernel panic");
1721 if (time_uptime > (then + AAC_BOOT_TIMEOUT)) {
1722 device_printf(sc->aac_dev,
1723 "FATAL: controller not coming ready, "
1724 "status %x\n", code);
1727 } while (!(code & AAC_UP_AND_RUNNING));
1730 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1731 * firmware version 1.x are not compatible with this driver.
1733 if (sc->flags & AAC_FLAGS_PERC2QC) {
1734 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1736 device_printf(sc->aac_dev,
1737 "Error reading firmware version\n");
1741 /* These numbers are stored as ASCII! */
1742 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1743 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1745 device_printf(sc->aac_dev,
1746 "Firmware version %d.%d is not supported.\n",
1753 * Retrieve the capabilities/supported options word so we know what
1754 * work-arounds to enable. Some firmware revs don't support this
1757 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1758 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1759 device_printf(sc->aac_dev,
1760 "RequestAdapterInfo failed\n");
1764 options = AAC_GET_MAILBOX(sc, 1);
1765 atu_size = AAC_GET_MAILBOX(sc, 2);
1766 sc->supported_options = options;
1768 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1769 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1770 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1771 if (options & AAC_SUPPORTED_NONDASD)
1772 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1773 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1774 && (sizeof(bus_addr_t) > 4)) {
1775 device_printf(sc->aac_dev,
1776 "Enabling 64-bit address support\n");
1777 sc->flags |= AAC_FLAGS_SG_64BIT;
1779 if ((options & AAC_SUPPORTED_NEW_COMM)
1780 && sc->aac_if.aif_send_command)
1781 sc->flags |= AAC_FLAGS_NEW_COMM;
1782 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1783 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1786 /* Check for broken hardware that does a lower number of commands */
1787 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1789 /* Remap mem. resource, if required */
1790 if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1791 atu_size > rman_get_size(sc->aac_regs_res1)) {
1792 bus_release_resource(
1793 sc->aac_dev, SYS_RES_MEMORY,
1794 sc->aac_regs_rid1, sc->aac_regs_res1);
1795 sc->aac_regs_res1 = bus_alloc_resource(
1796 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid1,
1797 0ul, ~0ul, atu_size, RF_ACTIVE);
1798 if (sc->aac_regs_res1 == NULL) {
1799 sc->aac_regs_res1 = bus_alloc_resource_any(
1800 sc->aac_dev, SYS_RES_MEMORY,
1801 &sc->aac_regs_rid1, RF_ACTIVE);
1802 if (sc->aac_regs_res1 == NULL) {
1803 device_printf(sc->aac_dev,
1804 "couldn't allocate register window\n");
1807 sc->flags &= ~AAC_FLAGS_NEW_COMM;
1809 sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
1810 sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
1812 if (sc->aac_hwif == AAC_HWIF_NARK) {
1813 sc->aac_regs_res0 = sc->aac_regs_res1;
1814 sc->aac_regs_rid0 = sc->aac_regs_rid1;
1815 sc->aac_btag0 = sc->aac_btag1;
1816 sc->aac_bhandle0 = sc->aac_bhandle1;
1820 /* Read preferred settings */
1821 sc->aac_max_fib_size = sizeof(struct aac_fib);
1822 sc->aac_max_sectors = 128; /* 64KB */
1823 if (sc->flags & AAC_FLAGS_SG_64BIT)
1824 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1825 - sizeof(struct aac_blockwrite64))
1826 / sizeof(struct aac_sg_entry64);
1828 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1829 - sizeof(struct aac_blockwrite))
1830 / sizeof(struct aac_sg_entry);
1832 if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1833 options = AAC_GET_MAILBOX(sc, 1);
1834 sc->aac_max_fib_size = (options & 0xFFFF);
1835 sc->aac_max_sectors = (options >> 16) << 1;
1836 options = AAC_GET_MAILBOX(sc, 2);
1837 sc->aac_sg_tablesize = (options >> 16);
1838 options = AAC_GET_MAILBOX(sc, 3);
1839 sc->aac_max_fibs = (options & 0xFFFF);
1841 if (sc->aac_max_fib_size > PAGE_SIZE)
1842 sc->aac_max_fib_size = PAGE_SIZE;
1843 sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1845 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1846 sc->flags |= AAC_FLAGS_RAW_IO;
1847 device_printf(sc->aac_dev, "Enable Raw I/O\n");
1849 if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1850 (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1851 sc->flags |= AAC_FLAGS_LBA_64BIT;
1852 device_printf(sc->aac_dev, "Enable 64-bit array\n");
1859 aac_init(struct aac_softc *sc)
1861 struct aac_adapter_init *ip;
1865 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1868 * Fill in the init structure. This tells the adapter about the
1869 * physical location of various important shared data structures.
1871 ip = &sc->aac_common->ac_init;
1872 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1873 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1874 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1875 sc->flags |= AAC_FLAGS_RAW_IO;
1877 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1879 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1880 offsetof(struct aac_common, ac_fibs);
1881 ip->AdapterFibsVirtualAddress = 0;
1882 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1883 ip->AdapterFibAlign = sizeof(struct aac_fib);
1885 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1886 offsetof(struct aac_common, ac_printf);
1887 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1890 * The adapter assumes that pages are 4K in size, except on some
1891 * broken firmware versions that do the page->byte conversion twice,
1892 * therefore 'assuming' that this value is in 16MB units (2^24).
1893 * Round up since the granularity is so high.
1895 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1896 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1897 ip->HostPhysMemPages =
1898 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1900 ip->HostElapsedSeconds = time_uptime; /* reset later if invalid */
1903 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1904 ip->InitFlags = INITFLAGS_NEW_COMM_SUPPORTED;
1905 device_printf(sc->aac_dev, "New comm. interface enabled\n");
1908 ip->MaxIoCommands = sc->aac_max_fibs;
1909 ip->MaxIoSize = sc->aac_max_sectors << 9;
1910 ip->MaxFibSize = sc->aac_max_fib_size;
1913 * Initialize FIB queues. Note that it appears that the layout of the
1914 * indexes and the segmentation of the entries may be mandated by the
1915 * adapter, which is only told about the base of the queue index fields.
1917 * The initial values of the indices are assumed to inform the adapter
1918 * of the sizes of the respective queues, and theoretically it could
1919 * work out the entire layout of the queue structures from this. We
1920 * take the easy route and just lay this area out like everyone else
1923 * The Linux driver uses a much more complex scheme whereby several
1924 * header records are kept for each queue. We use a couple of generic
1925 * list manipulation functions which 'know' the size of each list by
1926 * virtue of a table.
1928 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1929 qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1931 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1932 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1934 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1935 AAC_HOST_NORM_CMD_ENTRIES;
1936 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1937 AAC_HOST_NORM_CMD_ENTRIES;
1938 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1939 AAC_HOST_HIGH_CMD_ENTRIES;
1940 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1941 AAC_HOST_HIGH_CMD_ENTRIES;
1942 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1943 AAC_ADAP_NORM_CMD_ENTRIES;
1944 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1945 AAC_ADAP_NORM_CMD_ENTRIES;
1946 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1947 AAC_ADAP_HIGH_CMD_ENTRIES;
1948 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1949 AAC_ADAP_HIGH_CMD_ENTRIES;
1950 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1951 AAC_HOST_NORM_RESP_ENTRIES;
1952 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1953 AAC_HOST_NORM_RESP_ENTRIES;
1954 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1955 AAC_HOST_HIGH_RESP_ENTRIES;
1956 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1957 AAC_HOST_HIGH_RESP_ENTRIES;
1958 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1959 AAC_ADAP_NORM_RESP_ENTRIES;
1960 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1961 AAC_ADAP_NORM_RESP_ENTRIES;
1962 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1963 AAC_ADAP_HIGH_RESP_ENTRIES;
1964 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1965 AAC_ADAP_HIGH_RESP_ENTRIES;
1966 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1967 &sc->aac_queues->qt_HostNormCmdQueue[0];
1968 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1969 &sc->aac_queues->qt_HostHighCmdQueue[0];
1970 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1971 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1972 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1973 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1974 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1975 &sc->aac_queues->qt_HostNormRespQueue[0];
1976 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1977 &sc->aac_queues->qt_HostHighRespQueue[0];
1978 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1979 &sc->aac_queues->qt_AdapNormRespQueue[0];
1980 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1981 &sc->aac_queues->qt_AdapHighRespQueue[0];
1984 * Do controller-type-specific initialisation
1986 switch (sc->aac_hwif) {
1987 case AAC_HWIF_I960RX:
1988 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, ~0);
1991 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, ~0);
1998 * Give the init structure to the controller.
2000 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
2001 sc->aac_common_busaddr +
2002 offsetof(struct aac_common, ac_init), 0, 0, 0,
2004 device_printf(sc->aac_dev,
2005 "error establishing init structure\n");
2016 aac_setup_intr(struct aac_softc *sc)
2018 sc->aac_irq_rid = 0;
2019 if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
2022 RF_ACTIVE)) == NULL) {
2023 device_printf(sc->aac_dev, "can't allocate interrupt\n");
2026 if (sc->flags & AAC_FLAGS_NEW_COMM) {
2027 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2028 INTR_MPSAFE|INTR_TYPE_BIO, NULL,
2029 aac_new_intr, sc, &sc->aac_intr)) {
2030 device_printf(sc->aac_dev, "can't set up interrupt\n");
2034 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2035 INTR_TYPE_BIO, aac_fast_intr, NULL,
2036 sc, &sc->aac_intr)) {
2037 device_printf(sc->aac_dev,
2038 "can't set up FAST interrupt\n");
2039 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2040 INTR_MPSAFE|INTR_TYPE_BIO,
2041 NULL, (driver_intr_t *)aac_fast_intr,
2042 sc, &sc->aac_intr)) {
2043 device_printf(sc->aac_dev,
2044 "can't set up MPSAFE interrupt\n");
2053 * Send a synchronous command to the controller and wait for a result.
2054 * Indicate if the controller completed the command with an error status.
2057 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2058 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2064 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2066 /* populate the mailbox */
2067 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2069 /* ensure the sync command doorbell flag is cleared */
2070 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2072 /* then set it to signal the adapter */
2073 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2075 /* spin waiting for the command to complete */
2078 if (time_uptime > (then + AAC_IMMEDIATE_TIMEOUT)) {
2079 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2082 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2084 /* clear the completion flag */
2085 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2087 /* get the command status */
2088 status = AAC_GET_MAILBOX(sc, 0);
2092 if (status != AAC_SRB_STS_SUCCESS)
2098 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2099 struct aac_fib *fib, u_int16_t datasize)
2101 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2102 mtx_assert(&sc->aac_io_lock, MA_OWNED);
2104 if (datasize > AAC_FIB_DATASIZE)
2108 * Set up the sync FIB
2110 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2111 AAC_FIBSTATE_INITIALISED |
2113 fib->Header.XferState |= xferstate;
2114 fib->Header.Command = command;
2115 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2116 fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2117 fib->Header.SenderSize = sizeof(struct aac_fib);
2118 fib->Header.SenderFibAddress = 0; /* Not needed */
2119 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2120 offsetof(struct aac_common,
2124 * Give the FIB to the controller, wait for a response.
2126 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2127 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2128 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2136 * Adapter-space FIB queue manipulation
2138 * Note that the queue implementation here is a little funky; neither the PI or
2139 * CI will ever be zero. This behaviour is a controller feature.
2145 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2146 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
2147 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2148 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2149 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2150 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
2151 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2152 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2156 * Atomically insert an entry into the nominated queue, returns 0 on success or
2157 * EBUSY if the queue is full.
2159 * Note: it would be more efficient to defer notifying the controller in
2160 * the case where we may be inserting several entries in rapid succession,
2161 * but implementing this usefully may be difficult (it would involve a
2162 * separate queue/notify interface).
2165 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2172 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2174 fib_size = cm->cm_fib->Header.Size;
2175 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2177 /* get the producer/consumer indices */
2178 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2179 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2181 /* wrap the queue? */
2182 if (pi >= aac_qinfo[queue].size)
2185 /* check for queue full */
2186 if ((pi + 1) == ci) {
2192 * To avoid a race with its completion interrupt, place this command on
2193 * the busy queue prior to advertising it to the controller.
2195 aac_enqueue_busy(cm);
2197 /* populate queue entry */
2198 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2199 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2201 /* update producer index */
2202 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2204 /* notify the adapter if we know how */
2205 if (aac_qinfo[queue].notify != 0)
2206 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2215 * Atomically remove one entry from the nominated queue, returns 0 on
2216 * success or ENOENT if the queue is empty.
2219 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2220 struct aac_fib **fib_addr)
2223 u_int32_t fib_index;
2227 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2229 /* get the producer/consumer indices */
2230 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2231 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2233 /* check for queue empty */
2239 /* wrap the pi so the following test works */
2240 if (pi >= aac_qinfo[queue].size)
2247 /* wrap the queue? */
2248 if (ci >= aac_qinfo[queue].size)
2251 /* fetch the entry */
2252 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2255 case AAC_HOST_NORM_CMD_QUEUE:
2256 case AAC_HOST_HIGH_CMD_QUEUE:
2258 * The aq_fib_addr is only 32 bits wide so it can't be counted
2259 * on to hold an address. For AIF's, the adapter assumes
2260 * that it's giving us an address into the array of AIF fibs.
2261 * Therefore, we have to convert it to an index.
2263 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2264 sizeof(struct aac_fib);
2265 *fib_addr = &sc->aac_common->ac_fibs[fib_index];
2268 case AAC_HOST_NORM_RESP_QUEUE:
2269 case AAC_HOST_HIGH_RESP_QUEUE:
2271 struct aac_command *cm;
2274 * As above, an index is used instead of an actual address.
2275 * Gotta shift the index to account for the fast response
2276 * bit. No other correction is needed since this value was
2277 * originally provided by the driver via the SenderFibAddress
2280 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2281 cm = sc->aac_commands + (fib_index >> 2);
2282 *fib_addr = cm->cm_fib;
2285 * Is this a fast response? If it is, update the fib fields in
2286 * local memory since the whole fib isn't DMA'd back up.
2288 if (fib_index & 0x01) {
2289 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2290 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2295 panic("Invalid queue in aac_dequeue_fib()");
2299 /* update consumer index */
2300 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2302 /* if we have made the queue un-full, notify the adapter */
2303 if (notify && (aac_qinfo[queue].notify != 0))
2304 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2312 * Put our response to an Adapter Initialed Fib on the response queue
2315 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2322 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2324 /* Tell the adapter where the FIB is */
2325 fib_size = fib->Header.Size;
2326 fib_addr = fib->Header.SenderFibAddress;
2327 fib->Header.ReceiverFibAddress = fib_addr;
2329 /* get the producer/consumer indices */
2330 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2331 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2333 /* wrap the queue? */
2334 if (pi >= aac_qinfo[queue].size)
2337 /* check for queue full */
2338 if ((pi + 1) == ci) {
2343 /* populate queue entry */
2344 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2345 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2347 /* update producer index */
2348 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2350 /* notify the adapter if we know how */
2351 if (aac_qinfo[queue].notify != 0)
2352 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2361 * Check for commands that have been outstanding for a suspiciously long time,
2362 * and complain about them.
2365 aac_timeout(struct aac_softc *sc)
2367 struct aac_command *cm;
2372 * Traverse the busy command list, bitch about late commands once
2376 deadline = time_uptime - AAC_CMD_TIMEOUT;
2377 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2378 if ((cm->cm_timestamp < deadline)
2379 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
2380 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2381 device_printf(sc->aac_dev,
2382 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2383 cm, (int)(time_uptime-cm->cm_timestamp));
2384 AAC_PRINT_FIB(sc, cm->cm_fib);
2390 code = AAC_GET_FWSTATUS(sc);
2391 if (code != AAC_UP_AND_RUNNING) {
2392 device_printf(sc->aac_dev, "WARNING! Controller is no "
2393 "longer running! code= 0x%x\n", code);
2400 * Interface Function Vectors
2404 * Read the current firmware status word.
2407 aac_sa_get_fwstatus(struct aac_softc *sc)
2409 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2411 return(AAC_MEM0_GETREG4(sc, AAC_SA_FWSTATUS));
2415 aac_rx_get_fwstatus(struct aac_softc *sc)
2417 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2419 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2420 AAC_RX_OMR0 : AAC_RX_FWSTATUS));
2424 aac_fa_get_fwstatus(struct aac_softc *sc)
2428 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2430 val = AAC_MEM0_GETREG4(sc, AAC_FA_FWSTATUS);
2435 aac_rkt_get_fwstatus(struct aac_softc *sc)
2437 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2439 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2440 AAC_RKT_OMR0 : AAC_RKT_FWSTATUS));
2444 * Notify the controller of a change in a given queue
2448 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2450 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2452 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2456 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2458 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2460 AAC_MEM0_SETREG4(sc, AAC_RX_IDBR, qbit);
2464 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2466 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2468 AAC_MEM0_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2473 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2475 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2477 AAC_MEM0_SETREG4(sc, AAC_RKT_IDBR, qbit);
2481 * Get the interrupt reason bits
2484 aac_sa_get_istatus(struct aac_softc *sc)
2486 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2488 return(AAC_MEM0_GETREG2(sc, AAC_SA_DOORBELL0));
2492 aac_rx_get_istatus(struct aac_softc *sc)
2494 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2496 return(AAC_MEM0_GETREG4(sc, AAC_RX_ODBR));
2500 aac_fa_get_istatus(struct aac_softc *sc)
2504 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2506 val = AAC_MEM0_GETREG2(sc, AAC_FA_DOORBELL0);
2511 aac_rkt_get_istatus(struct aac_softc *sc)
2513 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2515 return(AAC_MEM0_GETREG4(sc, AAC_RKT_ODBR));
2519 * Clear some interrupt reason bits
2522 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2524 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2526 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2530 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2532 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2534 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, mask);
2538 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2540 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2542 AAC_MEM0_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2547 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2549 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2551 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, mask);
2555 * Populate the mailbox and set the command word
2558 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2559 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2561 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2563 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX, command);
2564 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2565 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2566 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2567 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2571 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2572 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2574 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2576 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX, command);
2577 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2578 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2579 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2580 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2584 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2585 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2587 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2589 AAC_MEM1_SETREG4(sc, AAC_FA_MAILBOX, command);
2591 AAC_MEM1_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2593 AAC_MEM1_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2595 AAC_MEM1_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2597 AAC_MEM1_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2602 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2603 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2605 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2607 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX, command);
2608 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2609 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2610 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2611 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2615 * Fetch the immediate command status word
2618 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2620 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2622 return(AAC_MEM1_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2626 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2628 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2630 return(AAC_MEM1_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2634 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2638 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2640 val = AAC_MEM1_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2645 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2647 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2649 return(AAC_MEM1_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2653 * Set/clear interrupt masks
2656 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2658 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2661 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2663 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2668 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2670 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2673 if (sc->flags & AAC_FLAGS_NEW_COMM)
2674 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2676 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2678 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~0);
2683 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2685 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2688 AAC_MEM0_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2691 AAC_MEM0_SETREG2((sc), AAC_FA_MASK0, ~0);
2697 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2699 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2702 if (sc->flags & AAC_FLAGS_NEW_COMM)
2703 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2705 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2707 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~0);
2712 * New comm. interface: Send command functions
2715 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2717 u_int32_t index, device;
2719 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2721 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2722 if (index == 0xffffffffL)
2723 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2724 if (index == 0xffffffffL)
2726 aac_enqueue_busy(cm);
2728 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2730 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2732 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2733 AAC_MEM0_SETREG4(sc, AAC_RX_IQUE, index);
2738 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2740 u_int32_t index, device;
2742 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2744 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2745 if (index == 0xffffffffL)
2746 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2747 if (index == 0xffffffffL)
2749 aac_enqueue_busy(cm);
2751 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2753 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2755 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2756 AAC_MEM0_SETREG4(sc, AAC_RKT_IQUE, index);
2761 * New comm. interface: get, set outbound queue index
2764 aac_rx_get_outb_queue(struct aac_softc *sc)
2766 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2768 return(AAC_MEM0_GETREG4(sc, AAC_RX_OQUE));
2772 aac_rkt_get_outb_queue(struct aac_softc *sc)
2774 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2776 return(AAC_MEM0_GETREG4(sc, AAC_RKT_OQUE));
2780 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2782 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2784 AAC_MEM0_SETREG4(sc, AAC_RX_OQUE, index);
2788 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2790 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2792 AAC_MEM0_SETREG4(sc, AAC_RKT_OQUE, index);
2796 * Debugging and Diagnostics
2800 * Print some information about the controller.
2803 aac_describe_controller(struct aac_softc *sc)
2805 struct aac_fib *fib;
2806 struct aac_adapter_info *info;
2807 char *adapter_type = "Adaptec RAID controller";
2809 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2811 mtx_lock(&sc->aac_io_lock);
2812 aac_alloc_sync_fib(sc, &fib);
2815 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2816 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2817 aac_release_sync_fib(sc);
2818 mtx_unlock(&sc->aac_io_lock);
2822 /* save the kernel revision structure for later use */
2823 info = (struct aac_adapter_info *)&fib->data[0];
2824 sc->aac_revision = info->KernelRevision;
2827 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2828 "(%dMB cache, %dMB execution), %s\n",
2829 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2830 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2831 info->BufferMem / (1024 * 1024),
2832 info->ExecutionMem / (1024 * 1024),
2833 aac_describe_code(aac_battery_platform,
2834 info->batteryPlatform));
2836 device_printf(sc->aac_dev,
2837 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2838 info->KernelRevision.external.comp.major,
2839 info->KernelRevision.external.comp.minor,
2840 info->KernelRevision.external.comp.dash,
2841 info->KernelRevision.buildNumber,
2842 (u_int32_t)(info->SerialNumber & 0xffffff));
2844 device_printf(sc->aac_dev, "Supported Options=%b\n",
2845 sc->supported_options,
2868 if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2870 if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2871 device_printf(sc->aac_dev,
2872 "RequestSupplementAdapterInfo failed\n");
2874 adapter_type = ((struct aac_supplement_adapter_info *)
2875 &fib->data[0])->AdapterTypeText;
2877 device_printf(sc->aac_dev, "%s, aac driver %d.%d.%d-%d\n",
2879 AAC_DRIVER_VERSION >> 24,
2880 (AAC_DRIVER_VERSION >> 16) & 0xFF,
2881 AAC_DRIVER_VERSION & 0xFF,
2884 aac_release_sync_fib(sc);
2885 mtx_unlock(&sc->aac_io_lock);
2889 * Look up a text description of a numeric error code and return a pointer to
2893 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2897 for (i = 0; table[i].string != NULL; i++)
2898 if (table[i].code == code)
2899 return(table[i].string);
2900 return(table[i + 1].string);
2904 * Management Interface
2908 aac_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2910 struct aac_softc *sc;
2913 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2915 sc->aac_state |= AAC_STATE_OPEN;
2921 aac_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2923 struct aac_softc *sc;
2926 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2928 /* Mark this unit as no longer open */
2929 if (sc->aac_open_cnt == 0)
2930 sc->aac_state &= ~AAC_STATE_OPEN;
2936 aac_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
2938 union aac_statrequest *as;
2939 struct aac_softc *sc;
2942 as = (union aac_statrequest *)arg;
2944 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2948 switch (as->as_item) {
2953 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2954 sizeof(struct aac_qstat));
2962 case FSACTL_SENDFIB:
2963 case FSACTL_SEND_LARGE_FIB:
2964 arg = *(caddr_t*)arg;
2965 case FSACTL_LNX_SENDFIB:
2966 case FSACTL_LNX_SEND_LARGE_FIB:
2967 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2968 error = aac_ioctl_sendfib(sc, arg);
2970 case FSACTL_SEND_RAW_SRB:
2971 arg = *(caddr_t*)arg;
2972 case FSACTL_LNX_SEND_RAW_SRB:
2973 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2974 error = aac_ioctl_send_raw_srb(sc, arg);
2976 case FSACTL_AIF_THREAD:
2977 case FSACTL_LNX_AIF_THREAD:
2978 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2981 case FSACTL_OPEN_GET_ADAPTER_FIB:
2982 arg = *(caddr_t*)arg;
2983 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2984 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2985 error = aac_open_aif(sc, arg);
2987 case FSACTL_GET_NEXT_ADAPTER_FIB:
2988 arg = *(caddr_t*)arg;
2989 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2990 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2991 error = aac_getnext_aif(sc, arg);
2993 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2994 arg = *(caddr_t*)arg;
2995 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2996 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2997 error = aac_close_aif(sc, arg);
2999 case FSACTL_MINIPORT_REV_CHECK:
3000 arg = *(caddr_t*)arg;
3001 case FSACTL_LNX_MINIPORT_REV_CHECK:
3002 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
3003 error = aac_rev_check(sc, arg);
3005 case FSACTL_QUERY_DISK:
3006 arg = *(caddr_t*)arg;
3007 case FSACTL_LNX_QUERY_DISK:
3008 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
3009 error = aac_query_disk(sc, arg);
3011 case FSACTL_DELETE_DISK:
3012 case FSACTL_LNX_DELETE_DISK:
3014 * We don't trust the underland to tell us when to delete a
3015 * container, rather we rely on an AIF coming from the
3020 case FSACTL_GET_PCI_INFO:
3021 arg = *(caddr_t*)arg;
3022 case FSACTL_LNX_GET_PCI_INFO:
3023 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
3024 error = aac_get_pci_info(sc, arg);
3026 case FSACTL_GET_FEATURES:
3027 arg = *(caddr_t*)arg;
3028 case FSACTL_LNX_GET_FEATURES:
3029 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
3030 error = aac_supported_features(sc, arg);
3033 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
3041 aac_poll(struct cdev *dev, int poll_events, struct thread *td)
3043 struct aac_softc *sc;
3044 struct aac_fib_context *ctx;
3050 mtx_lock(&sc->aac_aifq_lock);
3051 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
3052 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3053 if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap) {
3054 revents |= poll_events & (POLLIN | POLLRDNORM);
3059 mtx_unlock(&sc->aac_aifq_lock);
3062 if (poll_events & (POLLIN | POLLRDNORM))
3063 selrecord(td, &sc->rcv_select);
3070 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
3073 switch (event->ev_type) {
3074 case AAC_EVENT_CMFREE:
3075 mtx_assert(&sc->aac_io_lock, MA_OWNED);
3076 if (aac_alloc_command(sc, (struct aac_command **)arg)) {
3077 aac_add_event(sc, event);
3080 free(event, M_AACBUF);
3089 * Send a FIB supplied from userspace
3092 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3094 struct aac_command *cm;
3097 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3104 mtx_lock(&sc->aac_io_lock);
3105 if (aac_alloc_command(sc, &cm)) {
3106 struct aac_event *event;
3108 event = malloc(sizeof(struct aac_event), M_AACBUF,
3110 if (event == NULL) {
3112 mtx_unlock(&sc->aac_io_lock);
3115 event->ev_type = AAC_EVENT_CMFREE;
3116 event->ev_callback = aac_ioctl_event;
3117 event->ev_arg = &cm;
3118 aac_add_event(sc, event);
3119 msleep(&cm, &sc->aac_io_lock, 0, "sendfib", 0);
3121 mtx_unlock(&sc->aac_io_lock);
3124 * Fetch the FIB header, then re-copy to get data as well.
3126 if ((error = copyin(ufib, cm->cm_fib,
3127 sizeof(struct aac_fib_header))) != 0)
3129 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3130 if (size > sc->aac_max_fib_size) {
3131 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
3132 size, sc->aac_max_fib_size);
3133 size = sc->aac_max_fib_size;
3135 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3137 cm->cm_fib->Header.Size = size;
3138 cm->cm_timestamp = time_uptime;
3141 * Pass the FIB to the controller, wait for it to complete.
3143 mtx_lock(&sc->aac_io_lock);
3144 error = aac_wait_command(cm);
3145 mtx_unlock(&sc->aac_io_lock);
3147 device_printf(sc->aac_dev,
3148 "aac_wait_command return %d\n", error);
3153 * Copy the FIB and data back out to the caller.
3155 size = cm->cm_fib->Header.Size;
3156 if (size > sc->aac_max_fib_size) {
3157 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
3158 size, sc->aac_max_fib_size);
3159 size = sc->aac_max_fib_size;
3161 error = copyout(cm->cm_fib, ufib, size);
3165 mtx_lock(&sc->aac_io_lock);
3166 aac_release_command(cm);
3167 mtx_unlock(&sc->aac_io_lock);
3173 * Send a passthrough FIB supplied from userspace
3176 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
3182 * Handle an AIF sent to us by the controller; queue it for later reference.
3183 * If the queue fills up, then drop the older entries.
3186 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3188 struct aac_aif_command *aif;
3189 struct aac_container *co, *co_next;
3190 struct aac_fib_context *ctx;
3191 struct aac_mntinforesp *mir;
3192 int next, current, found;
3193 int count = 0, added = 0, i = 0;
3195 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3197 aif = (struct aac_aif_command*)&fib->data[0];
3198 aac_print_aif(sc, aif);
3200 /* Is it an event that we should care about? */
3201 switch (aif->command) {
3202 case AifCmdEventNotify:
3203 switch (aif->data.EN.type) {
3204 case AifEnAddContainer:
3205 case AifEnDeleteContainer:
3207 * A container was added or deleted, but the message
3208 * doesn't tell us anything else! Re-enumerate the
3209 * containers and sort things out.
3211 aac_alloc_sync_fib(sc, &fib);
3214 * Ask the controller for its containers one at
3216 * XXX What if the controller's list changes
3217 * midway through this enumaration?
3218 * XXX This should be done async.
3220 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
3223 count = mir->MntRespCount;
3225 * Check the container against our list.
3226 * co->co_found was already set to 0 in a
3229 if ((mir->Status == ST_OK) &&
3230 (mir->MntTable[0].VolType != CT_NONE)) {
3233 &sc->aac_container_tqh,
3235 if (co->co_mntobj.ObjectId ==
3236 mir->MntTable[0].ObjectId) {
3243 * If the container matched, continue
3252 * This is a new container. Do all the
3253 * appropriate things to set it up.
3255 aac_add_container(sc, mir, 1);
3259 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3260 aac_release_sync_fib(sc);
3263 * Go through our list of containers and see which ones
3264 * were not marked 'found'. Since the controller didn't
3265 * list them they must have been deleted. Do the
3266 * appropriate steps to destroy the device. Also reset
3267 * the co->co_found field.
3269 co = TAILQ_FIRST(&sc->aac_container_tqh);
3270 while (co != NULL) {
3271 if (co->co_found == 0) {
3272 mtx_unlock(&sc->aac_io_lock);
3274 device_delete_child(sc->aac_dev,
3277 mtx_lock(&sc->aac_io_lock);
3278 co_next = TAILQ_NEXT(co, co_link);
3279 mtx_lock(&sc->aac_container_lock);
3280 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3282 mtx_unlock(&sc->aac_container_lock);
3287 co = TAILQ_NEXT(co, co_link);
3291 /* Attach the newly created containers */
3293 mtx_unlock(&sc->aac_io_lock);
3295 bus_generic_attach(sc->aac_dev);
3297 mtx_lock(&sc->aac_io_lock);
3310 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3311 mtx_lock(&sc->aac_aifq_lock);
3312 current = sc->aifq_idx;
3313 next = (current + 1) % AAC_AIFQ_LENGTH;
3315 sc->aifq_filled = 1;
3316 bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3317 /* modify AIF contexts */
3318 if (sc->aifq_filled) {
3319 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3320 if (next == ctx->ctx_idx)
3322 else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3323 ctx->ctx_idx = next;
3326 sc->aifq_idx = next;
3327 /* On the off chance that someone is sleeping for an aif... */
3328 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3329 wakeup(sc->aac_aifq);
3330 /* Wakeup any poll()ers */
3331 selwakeuppri(&sc->rcv_select, PRIBIO);
3332 mtx_unlock(&sc->aac_aifq_lock);
3338 * Return the Revision of the driver to userspace and check to see if the
3339 * userspace app is possibly compatible. This is extremely bogus since
3340 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3341 * returning what the card reported.
3344 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3346 struct aac_rev_check rev_check;
3347 struct aac_rev_check_resp rev_check_resp;
3350 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3353 * Copyin the revision struct from userspace
3355 if ((error = copyin(udata, (caddr_t)&rev_check,
3356 sizeof(struct aac_rev_check))) != 0) {
3360 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3361 rev_check.callingRevision.buildNumber);
3364 * Doctor up the response struct.
3366 rev_check_resp.possiblyCompatible = 1;
3367 rev_check_resp.adapterSWRevision.external.ul =
3368 sc->aac_revision.external.ul;
3369 rev_check_resp.adapterSWRevision.buildNumber =
3370 sc->aac_revision.buildNumber;
3372 return(copyout((caddr_t)&rev_check_resp, udata,
3373 sizeof(struct aac_rev_check_resp)));
3377 * Pass the fib context to the caller
3380 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3382 struct aac_fib_context *fibctx, *ctx;
3385 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3387 fibctx = malloc(sizeof(struct aac_fib_context), M_AACBUF, M_NOWAIT|M_ZERO);
3391 mtx_lock(&sc->aac_aifq_lock);
3392 /* all elements are already 0, add to queue */
3393 if (sc->fibctx == NULL)
3394 sc->fibctx = fibctx;
3396 for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3402 /* evaluate unique value */
3403 fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3405 while (ctx != fibctx) {
3406 if (ctx->unique == fibctx->unique) {
3413 mtx_unlock(&sc->aac_aifq_lock);
3415 error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3417 aac_close_aif(sc, (caddr_t)ctx);
3422 * Close the caller's fib context
3425 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3427 struct aac_fib_context *ctx;
3429 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3431 mtx_lock(&sc->aac_aifq_lock);
3432 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3433 if (ctx->unique == *(uint32_t *)&arg) {
3434 if (ctx == sc->fibctx)
3437 ctx->prev->next = ctx->next;
3439 ctx->next->prev = ctx->prev;
3444 mtx_unlock(&sc->aac_aifq_lock);
3446 free(ctx, M_AACBUF);
3452 * Pass the caller the next AIF in their queue
3455 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3457 struct get_adapter_fib_ioctl agf;
3458 struct aac_fib_context *ctx;
3461 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3463 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3464 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3465 if (agf.AdapterFibContext == ctx->unique)
3471 error = aac_return_aif(sc, ctx, agf.AifFib);
3472 if (error == EAGAIN && agf.Wait) {
3473 fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3474 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3475 while (error == EAGAIN) {
3476 error = tsleep(sc->aac_aifq, PRIBIO |
3477 PCATCH, "aacaif", 0);
3479 error = aac_return_aif(sc, ctx, agf.AifFib);
3481 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3488 * Hand the next AIF off the top of the queue out to userspace.
3491 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3495 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3497 mtx_lock(&sc->aac_aifq_lock);
3498 current = ctx->ctx_idx;
3499 if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3501 mtx_unlock(&sc->aac_aifq_lock);
3505 copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3507 device_printf(sc->aac_dev,
3508 "aac_return_aif: copyout returned %d\n", error);
3511 ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3513 mtx_unlock(&sc->aac_aifq_lock);
3518 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3520 struct aac_pci_info {
3526 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3528 pciinf.bus = pci_get_bus(sc->aac_dev);
3529 pciinf.slot = pci_get_slot(sc->aac_dev);
3531 error = copyout((caddr_t)&pciinf, uptr,
3532 sizeof(struct aac_pci_info));
3538 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3540 struct aac_features f;
3543 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3545 if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3549 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3550 * ALL zero in the featuresState, the driver will return the current
3551 * state of all the supported features, the data field will not be
3553 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3554 * a specific bit set in the featuresState, the driver will return the
3555 * current state of this specific feature and whatever data that are
3556 * associated with the feature in the data field or perform whatever
3557 * action needed indicates in the data field.
3559 if (f.feat.fValue == 0) {
3560 f.feat.fBits.largeLBA =
3561 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3562 /* TODO: In the future, add other features state here as well */
3564 if (f.feat.fBits.largeLBA)
3565 f.feat.fBits.largeLBA =
3566 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3567 /* TODO: Add other features state and data in the future */
3570 error = copyout(&f, uptr, sizeof (f));
3575 * Give the userland some information about the container. The AAC arch
3576 * expects the driver to be a SCSI passthrough type driver, so it expects
3577 * the containers to have b:t:l numbers. Fake it.
3580 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3582 struct aac_query_disk query_disk;
3583 struct aac_container *co;
3584 struct aac_disk *disk;
3587 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3591 error = copyin(uptr, (caddr_t)&query_disk,
3592 sizeof(struct aac_query_disk));
3596 id = query_disk.ContainerNumber;
3600 mtx_lock(&sc->aac_container_lock);
3601 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3602 if (co->co_mntobj.ObjectId == id)
3607 query_disk.Valid = 0;
3608 query_disk.Locked = 0;
3609 query_disk.Deleted = 1; /* XXX is this right? */
3611 disk = device_get_softc(co->co_disk);
3612 query_disk.Valid = 1;
3614 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3615 query_disk.Deleted = 0;
3616 query_disk.Bus = device_get_unit(sc->aac_dev);
3617 query_disk.Target = disk->unit;
3619 query_disk.UnMapped = 0;
3620 sprintf(&query_disk.diskDeviceName[0], "%s%d",
3621 disk->ad_disk->d_name, disk->ad_disk->d_unit);
3623 mtx_unlock(&sc->aac_container_lock);
3625 error = copyout((caddr_t)&query_disk, uptr,
3626 sizeof(struct aac_query_disk));
3632 aac_get_bus_info(struct aac_softc *sc)
3634 struct aac_fib *fib;
3635 struct aac_ctcfg *c_cmd;
3636 struct aac_ctcfg_resp *c_resp;
3637 struct aac_vmioctl *vmi;
3638 struct aac_vmi_businf_resp *vmi_resp;
3639 struct aac_getbusinf businfo;
3640 struct aac_sim *caminf;
3642 int i, found, error;
3644 mtx_lock(&sc->aac_io_lock);
3645 aac_alloc_sync_fib(sc, &fib);
3646 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3647 bzero(c_cmd, sizeof(struct aac_ctcfg));
3649 c_cmd->Command = VM_ContainerConfig;
3650 c_cmd->cmd = CT_GET_SCSI_METHOD;
3653 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3654 sizeof(struct aac_ctcfg));
3656 device_printf(sc->aac_dev, "Error %d sending "
3657 "VM_ContainerConfig command\n", error);
3658 aac_release_sync_fib(sc);
3659 mtx_unlock(&sc->aac_io_lock);
3663 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3664 if (c_resp->Status != ST_OK) {
3665 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3667 aac_release_sync_fib(sc);
3668 mtx_unlock(&sc->aac_io_lock);
3672 sc->scsi_method_id = c_resp->param;
3674 vmi = (struct aac_vmioctl *)&fib->data[0];
3675 bzero(vmi, sizeof(struct aac_vmioctl));
3677 vmi->Command = VM_Ioctl;
3678 vmi->ObjType = FT_DRIVE;
3679 vmi->MethId = sc->scsi_method_id;
3681 vmi->IoctlCmd = GetBusInfo;
3683 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3684 sizeof(struct aac_vmi_businf_resp));
3686 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3688 aac_release_sync_fib(sc);
3689 mtx_unlock(&sc->aac_io_lock);
3693 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3694 if (vmi_resp->Status != ST_OK) {
3695 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3697 aac_release_sync_fib(sc);
3698 mtx_unlock(&sc->aac_io_lock);
3702 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3703 aac_release_sync_fib(sc);
3704 mtx_unlock(&sc->aac_io_lock);
3707 for (i = 0; i < businfo.BusCount; i++) {
3708 if (businfo.BusValid[i] != AAC_BUS_VALID)
3711 caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
3712 M_AACBUF, M_NOWAIT | M_ZERO);
3713 if (caminf == NULL) {
3714 device_printf(sc->aac_dev,
3715 "No memory to add passthrough bus %d\n", i);
3719 child = device_add_child(sc->aac_dev, "aacp", -1);
3720 if (child == NULL) {
3721 device_printf(sc->aac_dev,
3722 "device_add_child failed for passthrough bus %d\n",
3724 free(caminf, M_AACBUF);
3728 caminf->TargetsPerBus = businfo.TargetsPerBus;
3729 caminf->BusNumber = i;
3730 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3731 caminf->aac_sc = sc;
3732 caminf->sim_dev = child;
3734 device_set_ivars(child, caminf);
3735 device_set_desc(child, "SCSI Passthrough Bus");
3736 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3742 bus_generic_attach(sc->aac_dev);