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);
75 /* Command Processing */
76 static void aac_timeout(struct aac_softc *sc);
77 static void aac_complete(void *context, int pending);
78 static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
79 static void aac_bio_complete(struct aac_command *cm);
80 static int aac_wait_command(struct aac_command *cm);
81 static void aac_command_thread(struct aac_softc *sc);
83 /* Command Buffer Management */
84 static void aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
86 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
88 static int aac_alloc_commands(struct aac_softc *sc);
89 static void aac_free_commands(struct aac_softc *sc);
90 static void aac_unmap_command(struct aac_command *cm);
92 /* Hardware Interface */
93 static int aac_alloc(struct aac_softc *sc);
94 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
96 static int aac_check_firmware(struct aac_softc *sc);
97 static int aac_init(struct aac_softc *sc);
98 static int aac_sync_command(struct aac_softc *sc, u_int32_t command,
99 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
100 u_int32_t arg3, u_int32_t *sp);
101 static int aac_setup_intr(struct aac_softc *sc);
102 static int aac_enqueue_fib(struct aac_softc *sc, int queue,
103 struct aac_command *cm);
104 static int aac_dequeue_fib(struct aac_softc *sc, int queue,
105 u_int32_t *fib_size, struct aac_fib **fib_addr);
106 static int aac_enqueue_response(struct aac_softc *sc, int queue,
107 struct aac_fib *fib);
109 /* Falcon/PPC interface */
110 static int aac_fa_get_fwstatus(struct aac_softc *sc);
111 static void aac_fa_qnotify(struct aac_softc *sc, int qbit);
112 static int aac_fa_get_istatus(struct aac_softc *sc);
113 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask);
114 static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
115 u_int32_t arg0, u_int32_t arg1,
116 u_int32_t arg2, u_int32_t arg3);
117 static int aac_fa_get_mailbox(struct aac_softc *sc, int mb);
118 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable);
120 struct aac_interface aac_fa_interface = {
124 aac_fa_clear_istatus,
127 aac_fa_set_interrupts,
131 /* StrongARM interface */
132 static int aac_sa_get_fwstatus(struct aac_softc *sc);
133 static void aac_sa_qnotify(struct aac_softc *sc, int qbit);
134 static int aac_sa_get_istatus(struct aac_softc *sc);
135 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask);
136 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
137 u_int32_t arg0, u_int32_t arg1,
138 u_int32_t arg2, u_int32_t arg3);
139 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb);
140 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable);
142 struct aac_interface aac_sa_interface = {
146 aac_sa_clear_istatus,
149 aac_sa_set_interrupts,
153 /* i960Rx interface */
154 static int aac_rx_get_fwstatus(struct aac_softc *sc);
155 static void aac_rx_qnotify(struct aac_softc *sc, int qbit);
156 static int aac_rx_get_istatus(struct aac_softc *sc);
157 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask);
158 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
159 u_int32_t arg0, u_int32_t arg1,
160 u_int32_t arg2, u_int32_t arg3);
161 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb);
162 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable);
163 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
164 static int aac_rx_get_outb_queue(struct aac_softc *sc);
165 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
167 struct aac_interface aac_rx_interface = {
171 aac_rx_clear_istatus,
174 aac_rx_set_interrupts,
176 aac_rx_get_outb_queue,
177 aac_rx_set_outb_queue
180 /* Rocket/MIPS interface */
181 static int aac_rkt_get_fwstatus(struct aac_softc *sc);
182 static void aac_rkt_qnotify(struct aac_softc *sc, int qbit);
183 static int aac_rkt_get_istatus(struct aac_softc *sc);
184 static void aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
185 static void aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
186 u_int32_t arg0, u_int32_t arg1,
187 u_int32_t arg2, u_int32_t arg3);
188 static int aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
189 static void aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
190 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
191 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
192 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
194 struct aac_interface aac_rkt_interface = {
195 aac_rkt_get_fwstatus,
198 aac_rkt_clear_istatus,
201 aac_rkt_set_interrupts,
202 aac_rkt_send_command,
203 aac_rkt_get_outb_queue,
204 aac_rkt_set_outb_queue
207 /* Debugging and Diagnostics */
208 static void aac_describe_controller(struct aac_softc *sc);
209 static char *aac_describe_code(struct aac_code_lookup *table,
212 /* Management Interface */
213 static d_open_t aac_open;
214 static d_close_t aac_close;
215 static d_ioctl_t aac_ioctl;
216 static d_poll_t aac_poll;
217 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
218 static int aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg);
219 static void aac_handle_aif(struct aac_softc *sc,
220 struct aac_fib *fib);
221 static int aac_rev_check(struct aac_softc *sc, caddr_t udata);
222 static int aac_open_aif(struct aac_softc *sc, caddr_t arg);
223 static int aac_close_aif(struct aac_softc *sc, caddr_t arg);
224 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
225 static int aac_return_aif(struct aac_softc *sc,
226 struct aac_fib_context *ctx, caddr_t uptr);
227 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr);
228 static int aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
229 static int aac_supported_features(struct aac_softc *sc, caddr_t uptr);
230 static void aac_ioctl_event(struct aac_softc *sc,
231 struct aac_event *event, void *arg);
232 static struct aac_mntinforesp *
233 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid);
235 static struct cdevsw aac_cdevsw = {
236 .d_version = D_VERSION,
237 .d_flags = D_NEEDGIANT,
239 .d_close = aac_close,
240 .d_ioctl = aac_ioctl,
245 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
248 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
255 * Initialize the controller and softc
258 aac_attach(struct aac_softc *sc)
262 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
265 * Initialize per-controller queues.
273 * Initialize command-completion task.
275 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
277 /* mark controller as suspended until we get ourselves organised */
278 sc->aac_state |= AAC_STATE_SUSPEND;
281 * Check that the firmware on the card is supported.
283 if ((error = aac_check_firmware(sc)) != 0)
289 mtx_init(&sc->aac_aifq_lock, "AAC AIF lock", NULL, MTX_DEF);
290 mtx_init(&sc->aac_io_lock, "AAC I/O lock", NULL, MTX_DEF);
291 mtx_init(&sc->aac_container_lock, "AAC container lock", NULL, MTX_DEF);
292 TAILQ_INIT(&sc->aac_container_tqh);
293 TAILQ_INIT(&sc->aac_ev_cmfree);
296 * Initialize the adapter.
298 if ((error = aac_alloc(sc)) != 0)
300 if ((error = aac_init(sc)) != 0)
304 * Allocate and connect our interrupt.
306 if ((error = aac_setup_intr(sc)) != 0)
310 * Print a little information about the controller.
312 aac_describe_controller(sc);
315 * Register to probe our containers later.
317 sc->aac_ich.ich_func = aac_startup;
318 sc->aac_ich.ich_arg = sc;
319 if (config_intrhook_establish(&sc->aac_ich) != 0) {
320 device_printf(sc->aac_dev,
321 "can't establish configuration hook\n");
326 * Make the control device.
328 unit = device_get_unit(sc->aac_dev);
329 sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR,
330 0640, "aac%d", unit);
331 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
332 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
333 sc->aac_dev_t->si_drv1 = sc;
335 /* Create the AIF thread */
336 if (kthread_create((void(*)(void *))aac_command_thread, sc,
337 &sc->aifthread, 0, 0, "aac%daif", unit))
338 panic("Could not create AIF thread");
340 /* Register the shutdown method to only be called post-dump */
341 if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
342 sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
343 device_printf(sc->aac_dev,
344 "shutdown event registration failed\n");
346 /* Register with CAM for the non-DASD devices */
347 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
348 TAILQ_INIT(&sc->aac_sim_tqh);
349 aac_get_bus_info(sc);
356 aac_add_event(struct aac_softc *sc, struct aac_event *event)
359 switch (event->ev_type & AAC_EVENT_MASK) {
360 case AAC_EVENT_CMFREE:
361 TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
364 device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
373 * Request information of container #cid
375 static struct aac_mntinforesp *
376 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid)
378 struct aac_mntinfo *mi;
380 mi = (struct aac_mntinfo *)&fib->data[0];
381 /* use 64-bit LBA if enabled */
382 mi->Command = (sc->flags & AAC_FLAGS_LBA_64BIT) ?
383 VM_NameServe64 : VM_NameServe;
384 mi->MntType = FT_FILESYS;
387 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
388 sizeof(struct aac_mntinfo))) {
389 printf("Error probing container %d\n", cid);
393 return ((struct aac_mntinforesp *)&fib->data[0]);
397 * Probe for containers, create disks.
400 aac_startup(void *arg)
402 struct aac_softc *sc;
404 struct aac_mntinforesp *mir;
405 int count = 0, i = 0;
407 sc = (struct aac_softc *)arg;
408 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
410 /* disconnect ourselves from the intrhook chain */
411 config_intrhook_disestablish(&sc->aac_ich);
413 mtx_lock(&sc->aac_io_lock);
414 aac_alloc_sync_fib(sc, &fib);
416 /* loop over possible containers */
418 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
421 count = mir->MntRespCount;
422 aac_add_container(sc, mir, 0);
424 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
426 aac_release_sync_fib(sc);
427 mtx_unlock(&sc->aac_io_lock);
429 /* poke the bus to actually attach the child devices */
430 if (bus_generic_attach(sc->aac_dev))
431 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
433 /* mark the controller up */
434 sc->aac_state &= ~AAC_STATE_SUSPEND;
436 /* enable interrupts now */
437 AAC_UNMASK_INTERRUPTS(sc);
441 * Create a device to represent a new container
444 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
446 struct aac_container *co;
450 * Check container volume type for validity. Note that many of
451 * the possible types may never show up.
453 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
454 co = (struct aac_container *)malloc(sizeof *co, M_AACBUF,
457 panic("Out of memory?!");
458 fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "id %x name '%.16s' size %u type %d",
459 mir->MntTable[0].ObjectId,
460 mir->MntTable[0].FileSystemName,
461 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
463 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
464 device_printf(sc->aac_dev, "device_add_child failed\n");
466 device_set_ivars(child, co);
467 device_set_desc(child, aac_describe_code(aac_container_types,
468 mir->MntTable[0].VolType));
471 bcopy(&mir->MntTable[0], &co->co_mntobj,
472 sizeof(struct aac_mntobj));
473 mtx_lock(&sc->aac_container_lock);
474 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
475 mtx_unlock(&sc->aac_container_lock);
480 * Allocate resources associated with (sc)
483 aac_alloc(struct aac_softc *sc)
486 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
489 * Create DMA tag for mapping buffers into controller-addressable space.
491 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
492 1, 0, /* algnmnt, boundary */
493 (sc->flags & AAC_FLAGS_SG_64BIT) ?
495 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
496 BUS_SPACE_MAXADDR, /* highaddr */
497 NULL, NULL, /* filter, filterarg */
498 MAXBSIZE, /* maxsize */
499 sc->aac_sg_tablesize, /* nsegments */
500 MAXBSIZE, /* maxsegsize */
501 BUS_DMA_ALLOCNOW, /* flags */
502 busdma_lock_mutex, /* lockfunc */
503 &sc->aac_io_lock, /* lockfuncarg */
504 &sc->aac_buffer_dmat)) {
505 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
510 * Create DMA tag for mapping FIBs into controller-addressable space..
512 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
513 1, 0, /* algnmnt, boundary */
514 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
515 BUS_SPACE_MAXADDR_32BIT :
516 0x7fffffff, /* lowaddr */
517 BUS_SPACE_MAXADDR, /* highaddr */
518 NULL, NULL, /* filter, filterarg */
519 sc->aac_max_fibs_alloc *
520 sc->aac_max_fib_size, /* maxsize */
522 sc->aac_max_fibs_alloc *
523 sc->aac_max_fib_size, /* maxsize */
525 NULL, NULL, /* No locking needed */
526 &sc->aac_fib_dmat)) {
527 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
532 * Create DMA tag for the common structure and allocate it.
534 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
535 1, 0, /* algnmnt, boundary */
536 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
537 BUS_SPACE_MAXADDR_32BIT :
538 0x7fffffff, /* lowaddr */
539 BUS_SPACE_MAXADDR, /* highaddr */
540 NULL, NULL, /* filter, filterarg */
541 8192 + sizeof(struct aac_common), /* maxsize */
543 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
545 NULL, NULL, /* No locking needed */
546 &sc->aac_common_dmat)) {
547 device_printf(sc->aac_dev,
548 "can't allocate common structure DMA tag\n");
551 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
552 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
553 device_printf(sc->aac_dev, "can't allocate common structure\n");
558 * Work around a bug in the 2120 and 2200 that cannot DMA commands
559 * below address 8192 in physical memory.
560 * XXX If the padding is not needed, can it be put to use instead
563 (void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
564 sc->aac_common, 8192 + sizeof(*sc->aac_common),
565 aac_common_map, sc, 0);
567 if (sc->aac_common_busaddr < 8192) {
568 sc->aac_common = (struct aac_common *)
569 ((uint8_t *)sc->aac_common + 8192);
570 sc->aac_common_busaddr += 8192;
572 bzero(sc->aac_common, sizeof(*sc->aac_common));
574 /* Allocate some FIBs and associated command structs */
575 TAILQ_INIT(&sc->aac_fibmap_tqh);
576 sc->aac_commands = malloc(sc->aac_max_fibs * sizeof(struct aac_command),
577 M_AACBUF, M_WAITOK|M_ZERO);
578 while (sc->total_fibs < AAC_PREALLOCATE_FIBS) {
579 if (aac_alloc_commands(sc) != 0)
582 if (sc->total_fibs == 0)
589 * Free all of the resources associated with (sc)
591 * Should not be called if the controller is active.
594 aac_free(struct aac_softc *sc)
597 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
599 /* remove the control device */
600 if (sc->aac_dev_t != NULL)
601 destroy_dev(sc->aac_dev_t);
603 /* throw away any FIB buffers, discard the FIB DMA tag */
604 aac_free_commands(sc);
605 if (sc->aac_fib_dmat)
606 bus_dma_tag_destroy(sc->aac_fib_dmat);
608 free(sc->aac_commands, M_AACBUF);
610 /* destroy the common area */
611 if (sc->aac_common) {
612 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
613 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
614 sc->aac_common_dmamap);
616 if (sc->aac_common_dmat)
617 bus_dma_tag_destroy(sc->aac_common_dmat);
619 /* disconnect the interrupt handler */
621 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
622 if (sc->aac_irq != NULL)
623 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
626 /* destroy data-transfer DMA tag */
627 if (sc->aac_buffer_dmat)
628 bus_dma_tag_destroy(sc->aac_buffer_dmat);
630 /* destroy the parent DMA tag */
631 if (sc->aac_parent_dmat)
632 bus_dma_tag_destroy(sc->aac_parent_dmat);
634 /* release the register window mapping */
635 if (sc->aac_regs_resource != NULL)
636 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
637 sc->aac_regs_rid, sc->aac_regs_resource);
641 * Disconnect from the controller completely, in preparation for unload.
644 aac_detach(device_t dev)
646 struct aac_softc *sc;
647 struct aac_container *co;
651 sc = device_get_softc(dev);
652 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
654 if (sc->aac_state & AAC_STATE_OPEN)
657 /* Remove the child containers */
658 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
659 error = device_delete_child(dev, co->co_disk);
662 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
666 /* Remove the CAM SIMs */
667 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
668 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
669 error = device_delete_child(dev, sim->sim_dev);
675 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
676 sc->aifflags |= AAC_AIFFLAGS_EXIT;
677 wakeup(sc->aifthread);
678 tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
681 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
682 panic("Cannot shutdown AIF thread");
684 if ((error = aac_shutdown(dev)))
687 EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
691 mtx_destroy(&sc->aac_aifq_lock);
692 mtx_destroy(&sc->aac_io_lock);
693 mtx_destroy(&sc->aac_container_lock);
699 * Bring the controller down to a dormant state and detach all child devices.
701 * This function is called before detach or system shutdown.
703 * Note that we can assume that the bioq on the controller is empty, as we won't
704 * allow shutdown if any device is open.
707 aac_shutdown(device_t dev)
709 struct aac_softc *sc;
711 struct aac_close_command *cc;
713 sc = device_get_softc(dev);
714 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
716 sc->aac_state |= AAC_STATE_SUSPEND;
719 * Send a Container shutdown followed by a HostShutdown FIB to the
720 * controller to convince it that we don't want to talk to it anymore.
721 * We've been closed and all I/O completed already
723 device_printf(sc->aac_dev, "shutting down controller...");
725 mtx_lock(&sc->aac_io_lock);
726 aac_alloc_sync_fib(sc, &fib);
727 cc = (struct aac_close_command *)&fib->data[0];
729 bzero(cc, sizeof(struct aac_close_command));
730 cc->Command = VM_CloseAll;
731 cc->ContainerId = 0xffffffff;
732 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
733 sizeof(struct aac_close_command)))
741 * XXX Issuing this command to the controller makes it shut down
742 * but also keeps it from coming back up without a reset of the
743 * PCI bus. This is not desirable if you are just unloading the
744 * driver module with the intent to reload it later.
746 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
755 AAC_MASK_INTERRUPTS(sc);
756 aac_release_sync_fib(sc);
757 mtx_unlock(&sc->aac_io_lock);
763 * Bring the controller to a quiescent state, ready for system suspend.
766 aac_suspend(device_t dev)
768 struct aac_softc *sc;
770 sc = device_get_softc(dev);
772 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
773 sc->aac_state |= AAC_STATE_SUSPEND;
775 AAC_MASK_INTERRUPTS(sc);
780 * Bring the controller back to a state ready for operation.
783 aac_resume(device_t dev)
785 struct aac_softc *sc;
787 sc = device_get_softc(dev);
789 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
790 sc->aac_state &= ~AAC_STATE_SUSPEND;
791 AAC_UNMASK_INTERRUPTS(sc);
796 * Interrupt handler for NEW_COMM interface.
799 aac_new_intr(void *arg)
801 struct aac_softc *sc;
802 u_int32_t index, fast;
803 struct aac_command *cm;
807 sc = (struct aac_softc *)arg;
809 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
810 mtx_lock(&sc->aac_io_lock);
812 index = AAC_GET_OUTB_QUEUE(sc);
813 if (index == 0xffffffff)
814 index = AAC_GET_OUTB_QUEUE(sc);
815 if (index == 0xffffffff)
818 if (index == 0xfffffffe) {
819 /* XXX This means that the controller wants
820 * more work. Ignore it for now.
825 fib = (struct aac_fib *)malloc(sizeof *fib, M_AACBUF,
828 /* If we're really this short on memory,
829 * hopefully breaking out of the handler will
830 * allow something to get freed. This
831 * actually sucks a whole lot.
836 for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
837 ((u_int32_t *)fib)[i] = AAC_GETREG4(sc, index + i*4);
838 aac_handle_aif(sc, fib);
842 * AIF memory is owned by the adapter, so let it
843 * know that we are done with it.
845 AAC_SET_OUTB_QUEUE(sc, index);
846 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
849 cm = sc->aac_commands + (index >> 2);
852 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
853 *((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
856 aac_unmap_command(cm);
857 cm->cm_flags |= AAC_CMD_COMPLETED;
859 /* is there a completion handler? */
860 if (cm->cm_complete != NULL) {
863 /* assume that someone is sleeping on this
868 sc->flags &= ~AAC_QUEUE_FRZN;
871 /* see if we can start some more I/O */
872 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
875 mtx_unlock(&sc->aac_io_lock);
879 aac_fast_intr(void *arg)
881 struct aac_softc *sc;
884 sc = (struct aac_softc *)arg;
886 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
888 * Read the status register directly. This is faster than taking the
889 * driver lock and reading the queues directly. It also saves having
890 * to turn parts of the driver lock into a spin mutex, which would be
893 reason = AAC_GET_ISTATUS(sc);
894 AAC_CLEAR_ISTATUS(sc, reason);
896 /* handle completion processing */
897 if (reason & AAC_DB_RESPONSE_READY)
898 taskqueue_enqueue_fast(taskqueue_fast, &sc->aac_task_complete);
900 /* controller wants to talk to us */
901 if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
903 * XXX Make sure that we don't get fooled by strange messages
904 * that start with a NULL.
906 if ((reason & AAC_DB_PRINTF) &&
907 (sc->aac_common->ac_printf[0] == 0))
908 sc->aac_common->ac_printf[0] = 32;
911 * This might miss doing the actual wakeup. However, the
912 * msleep that this is waking up has a timeout, so it will
913 * wake up eventually. AIFs and printfs are low enough
914 * priority that they can handle hanging out for a few seconds
917 wakeup(sc->aifthread);
919 return (FILTER_HANDLED);
927 * Start as much queued I/O as possible on the controller
930 aac_startio(struct aac_softc *sc)
932 struct aac_command *cm;
935 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
939 * This flag might be set if the card is out of resources.
940 * Checking it here prevents an infinite loop of deferrals.
942 if (sc->flags & AAC_QUEUE_FRZN)
946 * Try to get a command that's been put off for lack of
949 cm = aac_dequeue_ready(sc);
952 * Try to build a command off the bio queue (ignore error
956 aac_bio_command(sc, &cm);
962 /* don't map more than once */
963 if (cm->cm_flags & AAC_CMD_MAPPED)
964 panic("aac: command %p already mapped", cm);
967 * Set up the command to go to the controller. If there are no
968 * data buffers associated with the command then it can bypass
971 if (cm->cm_datalen != 0) {
972 error = bus_dmamap_load(sc->aac_buffer_dmat,
973 cm->cm_datamap, cm->cm_data,
975 aac_map_command_sg, cm, 0);
976 if (error == EINPROGRESS) {
977 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "freezing queue\n");
978 sc->flags |= AAC_QUEUE_FRZN;
980 } else if (error != 0)
981 panic("aac_startio: unexpected error %d from "
984 aac_map_command_sg(cm, NULL, 0, 0);
989 * Handle notification of one or more FIBs coming from the controller.
992 aac_command_thread(struct aac_softc *sc)
998 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1000 mtx_lock(&sc->aac_io_lock);
1001 sc->aifflags = AAC_AIFFLAGS_RUNNING;
1003 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1006 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1007 retval = msleep(sc->aifthread, &sc->aac_io_lock, PRIBIO,
1008 "aifthd", AAC_PERIODIC_INTERVAL * hz);
1011 * First see if any FIBs need to be allocated. This needs
1012 * to be called without the driver lock because contigmalloc
1013 * will grab Giant, and would result in an LOR.
1015 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1016 mtx_unlock(&sc->aac_io_lock);
1017 aac_alloc_commands(sc);
1018 mtx_lock(&sc->aac_io_lock);
1019 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1024 * While we're here, check to see if any commands are stuck.
1025 * This is pretty low-priority, so it's ok if it doesn't
1028 if (retval == EWOULDBLOCK)
1031 /* Check the hardware printf message buffer */
1032 if (sc->aac_common->ac_printf[0] != 0)
1033 aac_print_printf(sc);
1035 /* Also check to see if the adapter has a command for us. */
1036 if (sc->flags & AAC_FLAGS_NEW_COMM)
1039 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
1043 AAC_PRINT_FIB(sc, fib);
1045 switch (fib->Header.Command) {
1047 aac_handle_aif(sc, fib);
1050 device_printf(sc->aac_dev, "unknown command "
1051 "from controller\n");
1055 if ((fib->Header.XferState == 0) ||
1056 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
1060 /* Return the AIF to the controller. */
1061 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
1062 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
1063 *(AAC_FSAStatus*)fib->data = ST_OK;
1065 /* XXX Compute the Size field? */
1066 size = fib->Header.Size;
1067 if (size > sizeof(struct aac_fib)) {
1068 size = sizeof(struct aac_fib);
1069 fib->Header.Size = size;
1072 * Since we did not generate this command, it
1073 * cannot go through the normal
1074 * enqueue->startio chain.
1076 aac_enqueue_response(sc,
1077 AAC_ADAP_NORM_RESP_QUEUE,
1082 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1083 mtx_unlock(&sc->aac_io_lock);
1084 wakeup(sc->aac_dev);
1090 * Process completed commands.
1093 aac_complete(void *context, int pending)
1095 struct aac_softc *sc;
1096 struct aac_command *cm;
1097 struct aac_fib *fib;
1100 sc = (struct aac_softc *)context;
1101 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1103 mtx_lock(&sc->aac_io_lock);
1105 /* pull completed commands off the queue */
1107 /* look for completed FIBs on our queue */
1108 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1110 break; /* nothing to do */
1112 /* get the command, unmap and hand off for processing */
1113 cm = sc->aac_commands + fib->Header.SenderData;
1115 AAC_PRINT_FIB(sc, fib);
1118 aac_remove_busy(cm);
1120 aac_unmap_command(cm);
1121 cm->cm_flags |= AAC_CMD_COMPLETED;
1123 /* is there a completion handler? */
1124 if (cm->cm_complete != NULL) {
1125 cm->cm_complete(cm);
1127 /* assume that someone is sleeping on this command */
1132 /* see if we can start some more I/O */
1133 sc->flags &= ~AAC_QUEUE_FRZN;
1136 mtx_unlock(&sc->aac_io_lock);
1140 * Handle a bio submitted from a disk device.
1143 aac_submit_bio(struct bio *bp)
1145 struct aac_disk *ad;
1146 struct aac_softc *sc;
1148 ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1149 sc = ad->ad_controller;
1150 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1152 /* queue the BIO and try to get some work done */
1153 aac_enqueue_bio(sc, bp);
1158 * Get a bio and build a command to go with it.
1161 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1163 struct aac_command *cm;
1164 struct aac_fib *fib;
1165 struct aac_disk *ad;
1168 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1170 /* get the resources we will need */
1173 if (aac_alloc_command(sc, &cm)) /* get a command */
1175 if ((bp = aac_dequeue_bio(sc)) == NULL)
1178 /* fill out the command */
1179 cm->cm_data = (void *)bp->bio_data;
1180 cm->cm_datalen = bp->bio_bcount;
1181 cm->cm_complete = aac_bio_complete;
1182 cm->cm_private = bp;
1183 cm->cm_timestamp = time_uptime;
1184 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1188 fib->Header.Size = sizeof(struct aac_fib_header);
1189 fib->Header.XferState =
1190 AAC_FIBSTATE_HOSTOWNED |
1191 AAC_FIBSTATE_INITIALISED |
1192 AAC_FIBSTATE_EMPTY |
1193 AAC_FIBSTATE_FROMHOST |
1194 AAC_FIBSTATE_REXPECTED |
1196 AAC_FIBSTATE_ASYNC |
1197 AAC_FIBSTATE_FAST_RESPONSE;
1199 /* build the read/write request */
1200 ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1202 if (sc->flags & AAC_FLAGS_RAW_IO) {
1203 struct aac_raw_io *raw;
1204 raw = (struct aac_raw_io *)&fib->data[0];
1205 fib->Header.Command = RawIo;
1206 raw->BlockNumber = (u_int64_t)bp->bio_pblkno;
1207 raw->ByteCount = bp->bio_bcount;
1208 raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1210 raw->BpComplete = 0;
1211 fib->Header.Size += sizeof(struct aac_raw_io);
1212 cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1213 if (bp->bio_cmd == BIO_READ) {
1215 cm->cm_flags |= AAC_CMD_DATAIN;
1218 cm->cm_flags |= AAC_CMD_DATAOUT;
1220 } else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1221 fib->Header.Command = ContainerCommand;
1222 if (bp->bio_cmd == BIO_READ) {
1223 struct aac_blockread *br;
1224 br = (struct aac_blockread *)&fib->data[0];
1225 br->Command = VM_CtBlockRead;
1226 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1227 br->BlockNumber = bp->bio_pblkno;
1228 br->ByteCount = bp->bio_bcount;
1229 fib->Header.Size += sizeof(struct aac_blockread);
1230 cm->cm_sgtable = &br->SgMap;
1231 cm->cm_flags |= AAC_CMD_DATAIN;
1233 struct aac_blockwrite *bw;
1234 bw = (struct aac_blockwrite *)&fib->data[0];
1235 bw->Command = VM_CtBlockWrite;
1236 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1237 bw->BlockNumber = bp->bio_pblkno;
1238 bw->ByteCount = bp->bio_bcount;
1239 bw->Stable = CUNSTABLE;
1240 fib->Header.Size += sizeof(struct aac_blockwrite);
1241 cm->cm_flags |= AAC_CMD_DATAOUT;
1242 cm->cm_sgtable = &bw->SgMap;
1245 fib->Header.Command = ContainerCommand64;
1246 if (bp->bio_cmd == BIO_READ) {
1247 struct aac_blockread64 *br;
1248 br = (struct aac_blockread64 *)&fib->data[0];
1249 br->Command = VM_CtHostRead64;
1250 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1251 br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1252 br->BlockNumber = bp->bio_pblkno;
1255 fib->Header.Size += sizeof(struct aac_blockread64);
1256 cm->cm_flags |= AAC_CMD_DATAIN;
1257 cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1259 struct aac_blockwrite64 *bw;
1260 bw = (struct aac_blockwrite64 *)&fib->data[0];
1261 bw->Command = VM_CtHostWrite64;
1262 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1263 bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1264 bw->BlockNumber = bp->bio_pblkno;
1267 fib->Header.Size += sizeof(struct aac_blockwrite64);
1268 cm->cm_flags |= AAC_CMD_DATAOUT;
1269 cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1278 aac_enqueue_bio(sc, bp);
1280 aac_release_command(cm);
1285 * Handle a bio-instigated command that has been completed.
1288 aac_bio_complete(struct aac_command *cm)
1290 struct aac_blockread_response *brr;
1291 struct aac_blockwrite_response *bwr;
1293 AAC_FSAStatus status;
1295 /* fetch relevant status and then release the command */
1296 bp = (struct bio *)cm->cm_private;
1297 if (bp->bio_cmd == BIO_READ) {
1298 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1299 status = brr->Status;
1301 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1302 status = bwr->Status;
1304 aac_release_command(cm);
1306 /* fix up the bio based on status */
1307 if (status == ST_OK) {
1310 bp->bio_error = EIO;
1311 bp->bio_flags |= BIO_ERROR;
1312 /* pass an error string out to the disk layer */
1313 bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1320 * Submit a command to the controller, return when it completes.
1321 * XXX This is very dangerous! If the card has gone out to lunch, we could
1322 * be stuck here forever. At the same time, signals are not caught
1323 * because there is a risk that a signal could wakeup the sleep before
1324 * the card has a chance to complete the command. Since there is no way
1325 * to cancel a command that is in progress, we can't protect against the
1326 * card completing a command late and spamming the command and data
1327 * memory. So, we are held hostage until the command completes.
1330 aac_wait_command(struct aac_command *cm)
1332 struct aac_softc *sc;
1336 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1338 /* Put the command on the ready queue and get things going */
1339 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1340 aac_enqueue_ready(cm);
1342 error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0);
1347 *Command Buffer Management
1351 * Allocate a command.
1354 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1356 struct aac_command *cm;
1358 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1360 if ((cm = aac_dequeue_free(sc)) == NULL) {
1361 if (sc->total_fibs < sc->aac_max_fibs) {
1362 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1363 wakeup(sc->aifthread);
1373 * Release a command back to the freelist.
1376 aac_release_command(struct aac_command *cm)
1378 struct aac_event *event;
1379 struct aac_softc *sc;
1382 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1384 /* (re)initialize the command/FIB */
1385 cm->cm_sgtable = NULL;
1387 cm->cm_complete = NULL;
1388 cm->cm_private = NULL;
1389 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1390 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1391 cm->cm_fib->Header.Flags = 0;
1392 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1395 * These are duplicated in aac_start to cover the case where an
1396 * intermediate stage may have destroyed them. They're left
1397 * initialized here for debugging purposes only.
1399 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1400 cm->cm_fib->Header.SenderData = 0;
1402 aac_enqueue_free(cm);
1405 * Dequeue all events so that there's no risk of events getting
1408 while ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1409 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1410 event->ev_callback(sc, event, event->ev_arg);
1415 * Map helper for command/FIB allocation.
1418 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1422 fibphys = (uint64_t *)arg;
1424 *fibphys = segs[0].ds_addr;
1428 * Allocate and initialize commands/FIBs for this adapter.
1431 aac_alloc_commands(struct aac_softc *sc)
1433 struct aac_command *cm;
1434 struct aac_fibmap *fm;
1438 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1440 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1443 fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO);
1447 /* allocate the FIBs in DMAable memory and load them */
1448 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1449 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1450 device_printf(sc->aac_dev,
1451 "Not enough contiguous memory available.\n");
1456 /* Ignore errors since this doesn't bounce */
1457 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1458 sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1459 aac_map_command_helper, &fibphys, 0);
1461 /* initialize constant fields in the command structure */
1462 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1463 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1464 cm = sc->aac_commands + sc->total_fibs;
1465 fm->aac_commands = cm;
1467 cm->cm_fib = (struct aac_fib *)
1468 ((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1469 cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1470 cm->cm_index = sc->total_fibs;
1472 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1473 &cm->cm_datamap)) != 0)
1475 mtx_lock(&sc->aac_io_lock);
1476 aac_release_command(cm);
1478 mtx_unlock(&sc->aac_io_lock);
1482 mtx_lock(&sc->aac_io_lock);
1483 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1484 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1485 mtx_unlock(&sc->aac_io_lock);
1489 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1490 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1496 * Free FIBs owned by this adapter.
1499 aac_free_commands(struct aac_softc *sc)
1501 struct aac_fibmap *fm;
1502 struct aac_command *cm;
1505 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1507 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1509 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1511 * We check against total_fibs to handle partially
1514 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1515 cm = fm->aac_commands + i;
1516 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1518 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1519 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1525 * Command-mapping helper function - populate this command's s/g table.
1528 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1530 struct aac_softc *sc;
1531 struct aac_command *cm;
1532 struct aac_fib *fib;
1535 cm = (struct aac_command *)arg;
1538 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1540 /* copy into the FIB */
1541 if (cm->cm_sgtable != NULL) {
1542 if (fib->Header.Command == RawIo) {
1543 struct aac_sg_tableraw *sg;
1544 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1546 for (i = 0; i < nseg; i++) {
1547 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1548 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1549 sg->SgEntryRaw[i].Next = 0;
1550 sg->SgEntryRaw[i].Prev = 0;
1551 sg->SgEntryRaw[i].Flags = 0;
1553 /* update the FIB size for the s/g count */
1554 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1555 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1556 struct aac_sg_table *sg;
1557 sg = cm->cm_sgtable;
1559 for (i = 0; i < nseg; i++) {
1560 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1561 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1563 /* update the FIB size for the s/g count */
1564 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1566 struct aac_sg_table64 *sg;
1567 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1569 for (i = 0; i < nseg; i++) {
1570 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1571 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1573 /* update the FIB size for the s/g count */
1574 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1578 /* Fix up the address values in the FIB. Use the command array index
1579 * instead of a pointer since these fields are only 32 bits. Shift
1580 * the SenderFibAddress over to make room for the fast response bit
1581 * and for the AIF bit
1583 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1584 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1586 /* save a pointer to the command for speedy reverse-lookup */
1587 cm->cm_fib->Header.SenderData = cm->cm_index;
1589 if (cm->cm_flags & AAC_CMD_DATAIN)
1590 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1591 BUS_DMASYNC_PREREAD);
1592 if (cm->cm_flags & AAC_CMD_DATAOUT)
1593 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1594 BUS_DMASYNC_PREWRITE);
1595 cm->cm_flags |= AAC_CMD_MAPPED;
1597 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1598 int count = 10000000L;
1599 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1601 aac_unmap_command(cm);
1602 sc->flags |= AAC_QUEUE_FRZN;
1603 aac_requeue_ready(cm);
1605 DELAY(5); /* wait 5 usec. */
1608 /* Put the FIB on the outbound queue */
1609 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1610 aac_unmap_command(cm);
1611 sc->flags |= AAC_QUEUE_FRZN;
1612 aac_requeue_ready(cm);
1620 * Unmap a command from controller-visible space.
1623 aac_unmap_command(struct aac_command *cm)
1625 struct aac_softc *sc;
1628 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1630 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1633 if (cm->cm_datalen != 0) {
1634 if (cm->cm_flags & AAC_CMD_DATAIN)
1635 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1636 BUS_DMASYNC_POSTREAD);
1637 if (cm->cm_flags & AAC_CMD_DATAOUT)
1638 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1639 BUS_DMASYNC_POSTWRITE);
1641 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1643 cm->cm_flags &= ~AAC_CMD_MAPPED;
1647 * Hardware Interface
1651 * Initialize the adapter.
1654 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1656 struct aac_softc *sc;
1658 sc = (struct aac_softc *)arg;
1659 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1661 sc->aac_common_busaddr = segs[0].ds_addr;
1665 aac_check_firmware(struct aac_softc *sc)
1667 u_int32_t code, major, minor, options = 0, atu_size = 0;
1671 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1673 * Wait for the adapter to come ready.
1677 code = AAC_GET_FWSTATUS(sc);
1678 if (code & AAC_SELF_TEST_FAILED) {
1679 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1682 if (code & AAC_KERNEL_PANIC) {
1683 device_printf(sc->aac_dev,
1684 "FATAL: controller kernel panic");
1687 if (time_uptime > (then + AAC_BOOT_TIMEOUT)) {
1688 device_printf(sc->aac_dev,
1689 "FATAL: controller not coming ready, "
1690 "status %x\n", code);
1693 } while (!(code & AAC_UP_AND_RUNNING));
1696 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1697 * firmware version 1.x are not compatible with this driver.
1699 if (sc->flags & AAC_FLAGS_PERC2QC) {
1700 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1702 device_printf(sc->aac_dev,
1703 "Error reading firmware version\n");
1707 /* These numbers are stored as ASCII! */
1708 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1709 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1711 device_printf(sc->aac_dev,
1712 "Firmware version %d.%d is not supported.\n",
1719 * Retrieve the capabilities/supported options word so we know what
1720 * work-arounds to enable. Some firmware revs don't support this
1723 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1724 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1725 device_printf(sc->aac_dev,
1726 "RequestAdapterInfo failed\n");
1730 options = AAC_GET_MAILBOX(sc, 1);
1731 atu_size = AAC_GET_MAILBOX(sc, 2);
1732 sc->supported_options = options;
1734 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1735 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1736 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1737 if (options & AAC_SUPPORTED_NONDASD)
1738 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1739 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1740 && (sizeof(bus_addr_t) > 4)) {
1741 device_printf(sc->aac_dev,
1742 "Enabling 64-bit address support\n");
1743 sc->flags |= AAC_FLAGS_SG_64BIT;
1745 if ((options & AAC_SUPPORTED_NEW_COMM)
1746 && sc->aac_if.aif_send_command)
1747 sc->flags |= AAC_FLAGS_NEW_COMM;
1748 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1749 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1752 /* Check for broken hardware that does a lower number of commands */
1753 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1755 /* Remap mem. resource, if required */
1756 if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1757 atu_size > rman_get_size(sc->aac_regs_resource)) {
1758 bus_release_resource(
1759 sc->aac_dev, SYS_RES_MEMORY,
1760 sc->aac_regs_rid, sc->aac_regs_resource);
1761 sc->aac_regs_resource = bus_alloc_resource(
1762 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
1763 0ul, ~0ul, atu_size, RF_ACTIVE);
1764 if (sc->aac_regs_resource == NULL) {
1765 sc->aac_regs_resource = bus_alloc_resource_any(
1766 sc->aac_dev, SYS_RES_MEMORY,
1767 &sc->aac_regs_rid, RF_ACTIVE);
1768 if (sc->aac_regs_resource == NULL) {
1769 device_printf(sc->aac_dev,
1770 "couldn't allocate register window\n");
1773 sc->flags &= ~AAC_FLAGS_NEW_COMM;
1775 sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
1776 sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
1779 /* Read preferred settings */
1780 sc->aac_max_fib_size = sizeof(struct aac_fib);
1781 sc->aac_max_sectors = 128; /* 64KB */
1782 if (sc->flags & AAC_FLAGS_SG_64BIT)
1783 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1784 - sizeof(struct aac_blockwrite64))
1785 / sizeof(struct aac_sg_entry64);
1787 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1788 - sizeof(struct aac_blockwrite))
1789 / sizeof(struct aac_sg_entry);
1791 if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1792 options = AAC_GET_MAILBOX(sc, 1);
1793 sc->aac_max_fib_size = (options & 0xFFFF);
1794 sc->aac_max_sectors = (options >> 16) << 1;
1795 options = AAC_GET_MAILBOX(sc, 2);
1796 sc->aac_sg_tablesize = (options >> 16);
1797 options = AAC_GET_MAILBOX(sc, 3);
1798 sc->aac_max_fibs = (options & 0xFFFF);
1800 if (sc->aac_max_fib_size > PAGE_SIZE)
1801 sc->aac_max_fib_size = PAGE_SIZE;
1802 sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1804 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1805 sc->flags |= AAC_FLAGS_RAW_IO;
1806 device_printf(sc->aac_dev, "Enable Raw I/O\n");
1808 if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1809 (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1810 sc->flags |= AAC_FLAGS_LBA_64BIT;
1811 device_printf(sc->aac_dev, "Enable 64-bit array\n");
1818 aac_init(struct aac_softc *sc)
1820 struct aac_adapter_init *ip;
1824 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1827 * Fill in the init structure. This tells the adapter about the
1828 * physical location of various important shared data structures.
1830 ip = &sc->aac_common->ac_init;
1831 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1832 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1833 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1834 sc->flags |= AAC_FLAGS_RAW_IO;
1836 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1838 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1839 offsetof(struct aac_common, ac_fibs);
1840 ip->AdapterFibsVirtualAddress = 0;
1841 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1842 ip->AdapterFibAlign = sizeof(struct aac_fib);
1844 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1845 offsetof(struct aac_common, ac_printf);
1846 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1849 * The adapter assumes that pages are 4K in size, except on some
1850 * broken firmware versions that do the page->byte conversion twice,
1851 * therefore 'assuming' that this value is in 16MB units (2^24).
1852 * Round up since the granularity is so high.
1854 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1855 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1856 ip->HostPhysMemPages =
1857 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1859 ip->HostElapsedSeconds = time_uptime; /* reset later if invalid */
1862 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1863 ip->InitFlags = INITFLAGS_NEW_COMM_SUPPORTED;
1864 device_printf(sc->aac_dev, "New comm. interface enabled\n");
1867 ip->MaxIoCommands = sc->aac_max_fibs;
1868 ip->MaxIoSize = sc->aac_max_sectors << 9;
1869 ip->MaxFibSize = sc->aac_max_fib_size;
1872 * Initialize FIB queues. Note that it appears that the layout of the
1873 * indexes and the segmentation of the entries may be mandated by the
1874 * adapter, which is only told about the base of the queue index fields.
1876 * The initial values of the indices are assumed to inform the adapter
1877 * of the sizes of the respective queues, and theoretically it could
1878 * work out the entire layout of the queue structures from this. We
1879 * take the easy route and just lay this area out like everyone else
1882 * The Linux driver uses a much more complex scheme whereby several
1883 * header records are kept for each queue. We use a couple of generic
1884 * list manipulation functions which 'know' the size of each list by
1885 * virtue of a table.
1887 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1888 qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1890 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1891 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1893 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1894 AAC_HOST_NORM_CMD_ENTRIES;
1895 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1896 AAC_HOST_NORM_CMD_ENTRIES;
1897 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1898 AAC_HOST_HIGH_CMD_ENTRIES;
1899 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1900 AAC_HOST_HIGH_CMD_ENTRIES;
1901 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1902 AAC_ADAP_NORM_CMD_ENTRIES;
1903 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1904 AAC_ADAP_NORM_CMD_ENTRIES;
1905 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1906 AAC_ADAP_HIGH_CMD_ENTRIES;
1907 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1908 AAC_ADAP_HIGH_CMD_ENTRIES;
1909 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1910 AAC_HOST_NORM_RESP_ENTRIES;
1911 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1912 AAC_HOST_NORM_RESP_ENTRIES;
1913 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1914 AAC_HOST_HIGH_RESP_ENTRIES;
1915 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1916 AAC_HOST_HIGH_RESP_ENTRIES;
1917 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1918 AAC_ADAP_NORM_RESP_ENTRIES;
1919 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1920 AAC_ADAP_NORM_RESP_ENTRIES;
1921 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1922 AAC_ADAP_HIGH_RESP_ENTRIES;
1923 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1924 AAC_ADAP_HIGH_RESP_ENTRIES;
1925 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1926 &sc->aac_queues->qt_HostNormCmdQueue[0];
1927 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1928 &sc->aac_queues->qt_HostHighCmdQueue[0];
1929 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1930 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1931 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1932 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1933 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1934 &sc->aac_queues->qt_HostNormRespQueue[0];
1935 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1936 &sc->aac_queues->qt_HostHighRespQueue[0];
1937 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1938 &sc->aac_queues->qt_AdapNormRespQueue[0];
1939 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1940 &sc->aac_queues->qt_AdapHighRespQueue[0];
1943 * Do controller-type-specific initialisation
1945 switch (sc->aac_hwif) {
1946 case AAC_HWIF_I960RX:
1947 AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1950 AAC_SETREG4(sc, AAC_RKT_ODBR, ~0);
1957 * Give the init structure to the controller.
1959 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1960 sc->aac_common_busaddr +
1961 offsetof(struct aac_common, ac_init), 0, 0, 0,
1963 device_printf(sc->aac_dev,
1964 "error establishing init structure\n");
1975 aac_setup_intr(struct aac_softc *sc)
1977 sc->aac_irq_rid = 0;
1978 if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
1981 RF_ACTIVE)) == NULL) {
1982 device_printf(sc->aac_dev, "can't allocate interrupt\n");
1985 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1986 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
1987 INTR_MPSAFE|INTR_TYPE_BIO, NULL,
1988 aac_new_intr, sc, &sc->aac_intr)) {
1989 device_printf(sc->aac_dev, "can't set up interrupt\n");
1993 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
1994 INTR_TYPE_BIO, aac_fast_intr, NULL,
1995 sc, &sc->aac_intr)) {
1996 device_printf(sc->aac_dev,
1997 "can't set up FAST interrupt\n");
1998 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
1999 INTR_MPSAFE|INTR_TYPE_BIO,
2000 NULL, (driver_intr_t *)aac_fast_intr,
2001 sc, &sc->aac_intr)) {
2002 device_printf(sc->aac_dev,
2003 "can't set up MPSAFE interrupt\n");
2012 * Send a synchronous command to the controller and wait for a result.
2013 * Indicate if the controller completed the command with an error status.
2016 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2017 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2023 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2025 /* populate the mailbox */
2026 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2028 /* ensure the sync command doorbell flag is cleared */
2029 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2031 /* then set it to signal the adapter */
2032 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2034 /* spin waiting for the command to complete */
2037 if (time_uptime > (then + AAC_IMMEDIATE_TIMEOUT)) {
2038 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2041 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2043 /* clear the completion flag */
2044 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2046 /* get the command status */
2047 status = AAC_GET_MAILBOX(sc, 0);
2051 if (status != AAC_SRB_STS_SUCCESS)
2057 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2058 struct aac_fib *fib, u_int16_t datasize)
2060 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2061 mtx_assert(&sc->aac_io_lock, MA_OWNED);
2063 if (datasize > AAC_FIB_DATASIZE)
2067 * Set up the sync FIB
2069 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2070 AAC_FIBSTATE_INITIALISED |
2072 fib->Header.XferState |= xferstate;
2073 fib->Header.Command = command;
2074 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2075 fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2076 fib->Header.SenderSize = sizeof(struct aac_fib);
2077 fib->Header.SenderFibAddress = 0; /* Not needed */
2078 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2079 offsetof(struct aac_common,
2083 * Give the FIB to the controller, wait for a response.
2085 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2086 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2087 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2095 * Adapter-space FIB queue manipulation
2097 * Note that the queue implementation here is a little funky; neither the PI or
2098 * CI will ever be zero. This behaviour is a controller feature.
2104 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2105 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
2106 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2107 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2108 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2109 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
2110 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2111 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2115 * Atomically insert an entry into the nominated queue, returns 0 on success or
2116 * EBUSY if the queue is full.
2118 * Note: it would be more efficient to defer notifying the controller in
2119 * the case where we may be inserting several entries in rapid succession,
2120 * but implementing this usefully may be difficult (it would involve a
2121 * separate queue/notify interface).
2124 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2131 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2133 fib_size = cm->cm_fib->Header.Size;
2134 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2136 /* get the producer/consumer indices */
2137 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2138 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2140 /* wrap the queue? */
2141 if (pi >= aac_qinfo[queue].size)
2144 /* check for queue full */
2145 if ((pi + 1) == ci) {
2151 * To avoid a race with its completion interrupt, place this command on
2152 * the busy queue prior to advertising it to the controller.
2154 aac_enqueue_busy(cm);
2156 /* populate queue entry */
2157 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2158 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2160 /* update producer index */
2161 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2163 /* notify the adapter if we know how */
2164 if (aac_qinfo[queue].notify != 0)
2165 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2174 * Atomically remove one entry from the nominated queue, returns 0 on
2175 * success or ENOENT if the queue is empty.
2178 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2179 struct aac_fib **fib_addr)
2182 u_int32_t fib_index;
2186 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2188 /* get the producer/consumer indices */
2189 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2190 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2192 /* check for queue empty */
2198 /* wrap the pi so the following test works */
2199 if (pi >= aac_qinfo[queue].size)
2206 /* wrap the queue? */
2207 if (ci >= aac_qinfo[queue].size)
2210 /* fetch the entry */
2211 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2214 case AAC_HOST_NORM_CMD_QUEUE:
2215 case AAC_HOST_HIGH_CMD_QUEUE:
2217 * The aq_fib_addr is only 32 bits wide so it can't be counted
2218 * on to hold an address. For AIF's, the adapter assumes
2219 * that it's giving us an address into the array of AIF fibs.
2220 * Therefore, we have to convert it to an index.
2222 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2223 sizeof(struct aac_fib);
2224 *fib_addr = &sc->aac_common->ac_fibs[fib_index];
2227 case AAC_HOST_NORM_RESP_QUEUE:
2228 case AAC_HOST_HIGH_RESP_QUEUE:
2230 struct aac_command *cm;
2233 * As above, an index is used instead of an actual address.
2234 * Gotta shift the index to account for the fast response
2235 * bit. No other correction is needed since this value was
2236 * originally provided by the driver via the SenderFibAddress
2239 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2240 cm = sc->aac_commands + (fib_index >> 2);
2241 *fib_addr = cm->cm_fib;
2244 * Is this a fast response? If it is, update the fib fields in
2245 * local memory since the whole fib isn't DMA'd back up.
2247 if (fib_index & 0x01) {
2248 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2249 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2254 panic("Invalid queue in aac_dequeue_fib()");
2258 /* update consumer index */
2259 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2261 /* if we have made the queue un-full, notify the adapter */
2262 if (notify && (aac_qinfo[queue].notify != 0))
2263 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2271 * Put our response to an Adapter Initialed Fib on the response queue
2274 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2281 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2283 /* Tell the adapter where the FIB is */
2284 fib_size = fib->Header.Size;
2285 fib_addr = fib->Header.SenderFibAddress;
2286 fib->Header.ReceiverFibAddress = fib_addr;
2288 /* get the producer/consumer indices */
2289 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2290 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2292 /* wrap the queue? */
2293 if (pi >= aac_qinfo[queue].size)
2296 /* check for queue full */
2297 if ((pi + 1) == ci) {
2302 /* populate queue entry */
2303 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2304 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2306 /* update producer index */
2307 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2309 /* notify the adapter if we know how */
2310 if (aac_qinfo[queue].notify != 0)
2311 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2320 * Check for commands that have been outstanding for a suspiciously long time,
2321 * and complain about them.
2324 aac_timeout(struct aac_softc *sc)
2326 struct aac_command *cm;
2331 * Traverse the busy command list, bitch about late commands once
2335 deadline = time_uptime - AAC_CMD_TIMEOUT;
2336 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2337 if ((cm->cm_timestamp < deadline)
2338 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
2339 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2340 device_printf(sc->aac_dev,
2341 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2342 cm, (int)(time_uptime-cm->cm_timestamp));
2343 AAC_PRINT_FIB(sc, cm->cm_fib);
2349 code = AAC_GET_FWSTATUS(sc);
2350 if (code != AAC_UP_AND_RUNNING) {
2351 device_printf(sc->aac_dev, "WARNING! Controller is no "
2352 "longer running! code= 0x%x\n", code);
2359 * Interface Function Vectors
2363 * Read the current firmware status word.
2366 aac_sa_get_fwstatus(struct aac_softc *sc)
2368 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2370 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2374 aac_rx_get_fwstatus(struct aac_softc *sc)
2376 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2378 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2382 aac_fa_get_fwstatus(struct aac_softc *sc)
2386 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2388 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2393 aac_rkt_get_fwstatus(struct aac_softc *sc)
2395 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2397 return(AAC_GETREG4(sc, AAC_RKT_FWSTATUS));
2401 * Notify the controller of a change in a given queue
2405 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2407 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2409 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2413 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2415 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2417 AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2421 aac_fa_qnotify(struct aac_softc *sc, int qbit)
2423 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2425 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2430 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2432 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2434 AAC_SETREG4(sc, AAC_RKT_IDBR, qbit);
2438 * Get the interrupt reason bits
2441 aac_sa_get_istatus(struct aac_softc *sc)
2443 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2445 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2449 aac_rx_get_istatus(struct aac_softc *sc)
2451 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2453 return(AAC_GETREG4(sc, AAC_RX_ODBR));
2457 aac_fa_get_istatus(struct aac_softc *sc)
2461 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2463 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2468 aac_rkt_get_istatus(struct aac_softc *sc)
2470 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2472 return(AAC_GETREG4(sc, AAC_RKT_ODBR));
2476 * Clear some interrupt reason bits
2479 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2481 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2483 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2487 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2489 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2491 AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2495 aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2497 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2499 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2504 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2506 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2508 AAC_SETREG4(sc, AAC_RKT_ODBR, mask);
2512 * Populate the mailbox and set the command word
2515 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2516 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2518 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2520 AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2521 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2522 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2523 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2524 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2528 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2529 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2531 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2533 AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2534 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2535 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2536 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2537 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2541 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2542 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2544 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2546 AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2548 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2550 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2552 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2554 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2559 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2560 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2562 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2564 AAC_SETREG4(sc, AAC_RKT_MAILBOX, command);
2565 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2566 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2567 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2568 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2572 * Fetch the immediate command status word
2575 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2577 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2579 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2583 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2585 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2587 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2591 aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2595 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2597 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2602 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2604 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2606 return(AAC_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2610 * Set/clear interrupt masks
2613 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2615 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2618 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2620 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2625 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2627 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2630 if (sc->flags & AAC_FLAGS_NEW_COMM)
2631 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2633 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2635 AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2640 aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2642 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2645 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2648 AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2654 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2656 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2659 if (sc->flags & AAC_FLAGS_NEW_COMM)
2660 AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2662 AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2664 AAC_SETREG4(sc, AAC_RKT_OIMR, ~0);
2669 * New comm. interface: Send command functions
2672 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2674 u_int32_t index, device;
2676 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2678 index = AAC_GETREG4(sc, AAC_RX_IQUE);
2679 if (index == 0xffffffffL)
2680 index = AAC_GETREG4(sc, AAC_RX_IQUE);
2681 if (index == 0xffffffffL)
2683 aac_enqueue_busy(cm);
2685 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2687 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2689 AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2690 AAC_SETREG4(sc, AAC_RX_IQUE, index);
2695 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2697 u_int32_t index, device;
2699 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2701 index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2702 if (index == 0xffffffffL)
2703 index = AAC_GETREG4(sc, AAC_RKT_IQUE);
2704 if (index == 0xffffffffL)
2706 aac_enqueue_busy(cm);
2708 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2710 AAC_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2712 AAC_SETREG4(sc, device, cm->cm_fib->Header.Size);
2713 AAC_SETREG4(sc, AAC_RKT_IQUE, index);
2718 * New comm. interface: get, set outbound queue index
2721 aac_rx_get_outb_queue(struct aac_softc *sc)
2723 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2725 return(AAC_GETREG4(sc, AAC_RX_OQUE));
2729 aac_rkt_get_outb_queue(struct aac_softc *sc)
2731 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2733 return(AAC_GETREG4(sc, AAC_RKT_OQUE));
2737 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2739 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2741 AAC_SETREG4(sc, AAC_RX_OQUE, index);
2745 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2747 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2749 AAC_SETREG4(sc, AAC_RKT_OQUE, index);
2753 * Debugging and Diagnostics
2757 * Print some information about the controller.
2760 aac_describe_controller(struct aac_softc *sc)
2762 struct aac_fib *fib;
2763 struct aac_adapter_info *info;
2764 char *adapter_type = "Adaptec RAID controller";
2766 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2768 mtx_lock(&sc->aac_io_lock);
2769 aac_alloc_sync_fib(sc, &fib);
2772 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2773 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2774 aac_release_sync_fib(sc);
2775 mtx_unlock(&sc->aac_io_lock);
2779 /* save the kernel revision structure for later use */
2780 info = (struct aac_adapter_info *)&fib->data[0];
2781 sc->aac_revision = info->KernelRevision;
2784 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2785 "(%dMB cache, %dMB execution), %s\n",
2786 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2787 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2788 info->BufferMem / (1024 * 1024),
2789 info->ExecutionMem / (1024 * 1024),
2790 aac_describe_code(aac_battery_platform,
2791 info->batteryPlatform));
2793 device_printf(sc->aac_dev,
2794 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2795 info->KernelRevision.external.comp.major,
2796 info->KernelRevision.external.comp.minor,
2797 info->KernelRevision.external.comp.dash,
2798 info->KernelRevision.buildNumber,
2799 (u_int32_t)(info->SerialNumber & 0xffffff));
2801 device_printf(sc->aac_dev, "Supported Options=%b\n",
2802 sc->supported_options,
2825 if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2827 if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2828 device_printf(sc->aac_dev,
2829 "RequestSupplementAdapterInfo failed\n");
2831 adapter_type = ((struct aac_supplement_adapter_info *)
2832 &fib->data[0])->AdapterTypeText;
2834 device_printf(sc->aac_dev, "%s, aac driver %d.%d.%d-%d\n",
2836 AAC_DRIVER_VERSION >> 24,
2837 (AAC_DRIVER_VERSION >> 16) & 0xFF,
2838 AAC_DRIVER_VERSION & 0xFF,
2841 aac_release_sync_fib(sc);
2842 mtx_unlock(&sc->aac_io_lock);
2846 * Look up a text description of a numeric error code and return a pointer to
2850 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2854 for (i = 0; table[i].string != NULL; i++)
2855 if (table[i].code == code)
2856 return(table[i].string);
2857 return(table[i + 1].string);
2861 * Management Interface
2865 aac_open(struct cdev *dev, int flags, int fmt, d_thread_t *td)
2867 struct aac_softc *sc;
2870 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2872 sc->aac_state |= AAC_STATE_OPEN;
2878 aac_close(struct cdev *dev, int flags, int fmt, d_thread_t *td)
2880 struct aac_softc *sc;
2883 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2885 /* Mark this unit as no longer open */
2886 if (sc->aac_open_cnt == 0)
2887 sc->aac_state &= ~AAC_STATE_OPEN;
2893 aac_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2895 union aac_statrequest *as;
2896 struct aac_softc *sc;
2899 as = (union aac_statrequest *)arg;
2901 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2905 switch (as->as_item) {
2910 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2911 sizeof(struct aac_qstat));
2919 case FSACTL_SENDFIB:
2920 case FSACTL_SEND_LARGE_FIB:
2921 arg = *(caddr_t*)arg;
2922 case FSACTL_LNX_SENDFIB:
2923 case FSACTL_LNX_SEND_LARGE_FIB:
2924 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2925 error = aac_ioctl_sendfib(sc, arg);
2927 case FSACTL_SEND_RAW_SRB:
2928 arg = *(caddr_t*)arg;
2929 case FSACTL_LNX_SEND_RAW_SRB:
2930 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2931 error = aac_ioctl_send_raw_srb(sc, arg);
2933 case FSACTL_AIF_THREAD:
2934 case FSACTL_LNX_AIF_THREAD:
2935 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2938 case FSACTL_OPEN_GET_ADAPTER_FIB:
2939 arg = *(caddr_t*)arg;
2940 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2941 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2942 error = aac_open_aif(sc, arg);
2944 case FSACTL_GET_NEXT_ADAPTER_FIB:
2945 arg = *(caddr_t*)arg;
2946 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2947 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2948 error = aac_getnext_aif(sc, arg);
2950 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2951 arg = *(caddr_t*)arg;
2952 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2953 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2954 error = aac_close_aif(sc, arg);
2956 case FSACTL_MINIPORT_REV_CHECK:
2957 arg = *(caddr_t*)arg;
2958 case FSACTL_LNX_MINIPORT_REV_CHECK:
2959 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
2960 error = aac_rev_check(sc, arg);
2962 case FSACTL_QUERY_DISK:
2963 arg = *(caddr_t*)arg;
2964 case FSACTL_LNX_QUERY_DISK:
2965 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
2966 error = aac_query_disk(sc, arg);
2968 case FSACTL_DELETE_DISK:
2969 case FSACTL_LNX_DELETE_DISK:
2971 * We don't trust the underland to tell us when to delete a
2972 * container, rather we rely on an AIF coming from the
2977 case FSACTL_GET_PCI_INFO:
2978 arg = *(caddr_t*)arg;
2979 case FSACTL_LNX_GET_PCI_INFO:
2980 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
2981 error = aac_get_pci_info(sc, arg);
2983 case FSACTL_GET_FEATURES:
2984 arg = *(caddr_t*)arg;
2985 case FSACTL_LNX_GET_FEATURES:
2986 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
2987 error = aac_supported_features(sc, arg);
2990 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
2998 aac_poll(struct cdev *dev, int poll_events, d_thread_t *td)
3000 struct aac_softc *sc;
3001 struct aac_fib_context *ctx;
3007 mtx_lock(&sc->aac_aifq_lock);
3008 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
3009 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3010 if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap) {
3011 revents |= poll_events & (POLLIN | POLLRDNORM);
3016 mtx_unlock(&sc->aac_aifq_lock);
3019 if (poll_events & (POLLIN | POLLRDNORM))
3020 selrecord(td, &sc->rcv_select);
3027 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
3030 switch (event->ev_type) {
3031 case AAC_EVENT_CMFREE:
3032 mtx_assert(&sc->aac_io_lock, MA_OWNED);
3033 if (aac_alloc_command(sc, (struct aac_command **)arg)) {
3034 aac_add_event(sc, event);
3037 free(event, M_AACBUF);
3046 * Send a FIB supplied from userspace
3049 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3051 struct aac_command *cm;
3054 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3061 mtx_lock(&sc->aac_io_lock);
3062 if (aac_alloc_command(sc, &cm)) {
3063 struct aac_event *event;
3065 event = malloc(sizeof(struct aac_event), M_AACBUF,
3067 if (event == NULL) {
3069 mtx_unlock(&sc->aac_io_lock);
3072 event->ev_type = AAC_EVENT_CMFREE;
3073 event->ev_callback = aac_ioctl_event;
3074 event->ev_arg = &cm;
3075 aac_add_event(sc, event);
3076 msleep(&cm, &sc->aac_io_lock, 0, "sendfib", 0);
3078 mtx_unlock(&sc->aac_io_lock);
3081 * Fetch the FIB header, then re-copy to get data as well.
3083 if ((error = copyin(ufib, cm->cm_fib,
3084 sizeof(struct aac_fib_header))) != 0)
3086 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3087 if (size > sc->aac_max_fib_size) {
3088 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
3089 size, sc->aac_max_fib_size);
3090 size = sc->aac_max_fib_size;
3092 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3094 cm->cm_fib->Header.Size = size;
3095 cm->cm_timestamp = time_uptime;
3098 * Pass the FIB to the controller, wait for it to complete.
3100 mtx_lock(&sc->aac_io_lock);
3101 error = aac_wait_command(cm);
3102 mtx_unlock(&sc->aac_io_lock);
3104 device_printf(sc->aac_dev,
3105 "aac_wait_command return %d\n", error);
3110 * Copy the FIB and data back out to the caller.
3112 size = cm->cm_fib->Header.Size;
3113 if (size > sc->aac_max_fib_size) {
3114 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
3115 size, sc->aac_max_fib_size);
3116 size = sc->aac_max_fib_size;
3118 error = copyout(cm->cm_fib, ufib, size);
3122 mtx_lock(&sc->aac_io_lock);
3123 aac_release_command(cm);
3124 mtx_unlock(&sc->aac_io_lock);
3130 * Send a passthrough FIB supplied from userspace
3133 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
3139 * Handle an AIF sent to us by the controller; queue it for later reference.
3140 * If the queue fills up, then drop the older entries.
3143 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3145 struct aac_aif_command *aif;
3146 struct aac_container *co, *co_next;
3147 struct aac_fib_context *ctx;
3148 struct aac_mntinforesp *mir;
3149 int next, current, found;
3150 int count = 0, added = 0, i = 0;
3152 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3154 aif = (struct aac_aif_command*)&fib->data[0];
3155 aac_print_aif(sc, aif);
3157 /* Is it an event that we should care about? */
3158 switch (aif->command) {
3159 case AifCmdEventNotify:
3160 switch (aif->data.EN.type) {
3161 case AifEnAddContainer:
3162 case AifEnDeleteContainer:
3164 * A container was added or deleted, but the message
3165 * doesn't tell us anything else! Re-enumerate the
3166 * containers and sort things out.
3168 aac_alloc_sync_fib(sc, &fib);
3171 * Ask the controller for its containers one at
3173 * XXX What if the controller's list changes
3174 * midway through this enumaration?
3175 * XXX This should be done async.
3177 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
3180 count = mir->MntRespCount;
3182 * Check the container against our list.
3183 * co->co_found was already set to 0 in a
3186 if ((mir->Status == ST_OK) &&
3187 (mir->MntTable[0].VolType != CT_NONE)) {
3190 &sc->aac_container_tqh,
3192 if (co->co_mntobj.ObjectId ==
3193 mir->MntTable[0].ObjectId) {
3200 * If the container matched, continue
3209 * This is a new container. Do all the
3210 * appropriate things to set it up.
3212 aac_add_container(sc, mir, 1);
3216 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3217 aac_release_sync_fib(sc);
3220 * Go through our list of containers and see which ones
3221 * were not marked 'found'. Since the controller didn't
3222 * list them they must have been deleted. Do the
3223 * appropriate steps to destroy the device. Also reset
3224 * the co->co_found field.
3226 co = TAILQ_FIRST(&sc->aac_container_tqh);
3227 while (co != NULL) {
3228 if (co->co_found == 0) {
3229 mtx_unlock(&sc->aac_io_lock);
3231 device_delete_child(sc->aac_dev,
3234 mtx_lock(&sc->aac_io_lock);
3235 co_next = TAILQ_NEXT(co, co_link);
3236 mtx_lock(&sc->aac_container_lock);
3237 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3239 mtx_unlock(&sc->aac_container_lock);
3244 co = TAILQ_NEXT(co, co_link);
3248 /* Attach the newly created containers */
3250 mtx_unlock(&sc->aac_io_lock);
3252 bus_generic_attach(sc->aac_dev);
3254 mtx_lock(&sc->aac_io_lock);
3267 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3268 mtx_lock(&sc->aac_aifq_lock);
3269 current = sc->aifq_idx;
3270 next = (current + 1) % AAC_AIFQ_LENGTH;
3272 sc->aifq_filled = 1;
3273 bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3274 /* modify AIF contexts */
3275 if (sc->aifq_filled) {
3276 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3277 if (next == ctx->ctx_idx)
3279 else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3280 ctx->ctx_idx = next;
3283 sc->aifq_idx = next;
3284 /* On the off chance that someone is sleeping for an aif... */
3285 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3286 wakeup(sc->aac_aifq);
3287 /* Wakeup any poll()ers */
3288 selwakeuppri(&sc->rcv_select, PRIBIO);
3289 mtx_unlock(&sc->aac_aifq_lock);
3295 * Return the Revision of the driver to userspace and check to see if the
3296 * userspace app is possibly compatible. This is extremely bogus since
3297 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3298 * returning what the card reported.
3301 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3303 struct aac_rev_check rev_check;
3304 struct aac_rev_check_resp rev_check_resp;
3307 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3310 * Copyin the revision struct from userspace
3312 if ((error = copyin(udata, (caddr_t)&rev_check,
3313 sizeof(struct aac_rev_check))) != 0) {
3317 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3318 rev_check.callingRevision.buildNumber);
3321 * Doctor up the response struct.
3323 rev_check_resp.possiblyCompatible = 1;
3324 rev_check_resp.adapterSWRevision.external.ul =
3325 sc->aac_revision.external.ul;
3326 rev_check_resp.adapterSWRevision.buildNumber =
3327 sc->aac_revision.buildNumber;
3329 return(copyout((caddr_t)&rev_check_resp, udata,
3330 sizeof(struct aac_rev_check_resp)));
3334 * Pass the fib context to the caller
3337 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3339 struct aac_fib_context *fibctx, *ctx;
3342 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3344 fibctx = malloc(sizeof(struct aac_fib_context), M_AACBUF, M_NOWAIT|M_ZERO);
3348 mtx_lock(&sc->aac_aifq_lock);
3349 /* all elements are already 0, add to queue */
3350 if (sc->fibctx == NULL)
3351 sc->fibctx = fibctx;
3353 for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3359 /* evaluate unique value */
3360 fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3362 while (ctx != fibctx) {
3363 if (ctx->unique == fibctx->unique) {
3370 mtx_unlock(&sc->aac_aifq_lock);
3372 error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3374 aac_close_aif(sc, (caddr_t)ctx);
3379 * Close the caller's fib context
3382 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3384 struct aac_fib_context *ctx;
3386 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3388 mtx_lock(&sc->aac_aifq_lock);
3389 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3390 if (ctx->unique == *(uint32_t *)&arg) {
3391 if (ctx == sc->fibctx)
3394 ctx->prev->next = ctx->next;
3396 ctx->next->prev = ctx->prev;
3401 mtx_unlock(&sc->aac_aifq_lock);
3403 free(ctx, M_AACBUF);
3409 * Pass the caller the next AIF in their queue
3412 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3414 struct get_adapter_fib_ioctl agf;
3415 struct aac_fib_context *ctx;
3418 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3420 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3421 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3422 if (agf.AdapterFibContext == ctx->unique)
3428 error = aac_return_aif(sc, ctx, agf.AifFib);
3429 if (error == EAGAIN && agf.Wait) {
3430 fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3431 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3432 while (error == EAGAIN) {
3433 error = tsleep(sc->aac_aifq, PRIBIO |
3434 PCATCH, "aacaif", 0);
3436 error = aac_return_aif(sc, ctx, agf.AifFib);
3438 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3445 * Hand the next AIF off the top of the queue out to userspace.
3448 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3452 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3454 mtx_lock(&sc->aac_aifq_lock);
3455 current = ctx->ctx_idx;
3456 if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3458 mtx_unlock(&sc->aac_aifq_lock);
3462 copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3464 device_printf(sc->aac_dev,
3465 "aac_return_aif: copyout returned %d\n", error);
3468 ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3470 mtx_unlock(&sc->aac_aifq_lock);
3475 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3477 struct aac_pci_info {
3483 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3485 pciinf.bus = pci_get_bus(sc->aac_dev);
3486 pciinf.slot = pci_get_slot(sc->aac_dev);
3488 error = copyout((caddr_t)&pciinf, uptr,
3489 sizeof(struct aac_pci_info));
3495 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3497 struct aac_features f;
3500 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3502 if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3506 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3507 * ALL zero in the featuresState, the driver will return the current
3508 * state of all the supported features, the data field will not be
3510 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3511 * a specific bit set in the featuresState, the driver will return the
3512 * current state of this specific feature and whatever data that are
3513 * associated with the feature in the data field or perform whatever
3514 * action needed indicates in the data field.
3516 if (f.feat.fValue == 0) {
3517 f.feat.fBits.largeLBA =
3518 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3519 /* TODO: In the future, add other features state here as well */
3521 if (f.feat.fBits.largeLBA)
3522 f.feat.fBits.largeLBA =
3523 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3524 /* TODO: Add other features state and data in the future */
3527 error = copyout(&f, uptr, sizeof (f));
3532 * Give the userland some information about the container. The AAC arch
3533 * expects the driver to be a SCSI passthrough type driver, so it expects
3534 * the containers to have b:t:l numbers. Fake it.
3537 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3539 struct aac_query_disk query_disk;
3540 struct aac_container *co;
3541 struct aac_disk *disk;
3544 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3548 error = copyin(uptr, (caddr_t)&query_disk,
3549 sizeof(struct aac_query_disk));
3553 id = query_disk.ContainerNumber;
3557 mtx_lock(&sc->aac_container_lock);
3558 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3559 if (co->co_mntobj.ObjectId == id)
3564 query_disk.Valid = 0;
3565 query_disk.Locked = 0;
3566 query_disk.Deleted = 1; /* XXX is this right? */
3568 disk = device_get_softc(co->co_disk);
3569 query_disk.Valid = 1;
3571 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3572 query_disk.Deleted = 0;
3573 query_disk.Bus = device_get_unit(sc->aac_dev);
3574 query_disk.Target = disk->unit;
3576 query_disk.UnMapped = 0;
3577 sprintf(&query_disk.diskDeviceName[0], "%s%d",
3578 disk->ad_disk->d_name, disk->ad_disk->d_unit);
3580 mtx_unlock(&sc->aac_container_lock);
3582 error = copyout((caddr_t)&query_disk, uptr,
3583 sizeof(struct aac_query_disk));
3589 aac_get_bus_info(struct aac_softc *sc)
3591 struct aac_fib *fib;
3592 struct aac_ctcfg *c_cmd;
3593 struct aac_ctcfg_resp *c_resp;
3594 struct aac_vmioctl *vmi;
3595 struct aac_vmi_businf_resp *vmi_resp;
3596 struct aac_getbusinf businfo;
3597 struct aac_sim *caminf;
3599 int i, found, error;
3601 mtx_lock(&sc->aac_io_lock);
3602 aac_alloc_sync_fib(sc, &fib);
3603 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3604 bzero(c_cmd, sizeof(struct aac_ctcfg));
3606 c_cmd->Command = VM_ContainerConfig;
3607 c_cmd->cmd = CT_GET_SCSI_METHOD;
3610 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3611 sizeof(struct aac_ctcfg));
3613 device_printf(sc->aac_dev, "Error %d sending "
3614 "VM_ContainerConfig command\n", error);
3615 aac_release_sync_fib(sc);
3616 mtx_unlock(&sc->aac_io_lock);
3620 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3621 if (c_resp->Status != ST_OK) {
3622 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3624 aac_release_sync_fib(sc);
3625 mtx_unlock(&sc->aac_io_lock);
3629 sc->scsi_method_id = c_resp->param;
3631 vmi = (struct aac_vmioctl *)&fib->data[0];
3632 bzero(vmi, sizeof(struct aac_vmioctl));
3634 vmi->Command = VM_Ioctl;
3635 vmi->ObjType = FT_DRIVE;
3636 vmi->MethId = sc->scsi_method_id;
3638 vmi->IoctlCmd = GetBusInfo;
3640 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3641 sizeof(struct aac_vmi_businf_resp));
3643 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3645 aac_release_sync_fib(sc);
3646 mtx_unlock(&sc->aac_io_lock);
3650 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3651 if (vmi_resp->Status != ST_OK) {
3652 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3654 aac_release_sync_fib(sc);
3655 mtx_unlock(&sc->aac_io_lock);
3659 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3660 aac_release_sync_fib(sc);
3661 mtx_unlock(&sc->aac_io_lock);
3664 for (i = 0; i < businfo.BusCount; i++) {
3665 if (businfo.BusValid[i] != AAC_BUS_VALID)
3668 caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
3669 M_AACBUF, M_NOWAIT | M_ZERO);
3670 if (caminf == NULL) {
3671 device_printf(sc->aac_dev,
3672 "No memory to add passthrough bus %d\n", i);
3676 child = device_add_child(sc->aac_dev, "aacp", -1);
3677 if (child == NULL) {
3678 device_printf(sc->aac_dev,
3679 "device_add_child failed for passthrough bus %d\n",
3681 free(caminf, M_AACBUF);
3685 caminf->TargetsPerBus = businfo.TargetsPerBus;
3686 caminf->BusNumber = i;
3687 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3688 caminf->aac_sc = sc;
3689 caminf->sim_dev = child;
3691 device_set_ivars(child, caminf);
3692 device_set_desc(child, "SCSI Passthrough Bus");
3693 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3699 bus_generic_attach(sc->aac_dev);