2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/dev/aac/aac.c,v 1.170 2012/02/13 16:48:49 emaste Exp $
33 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
35 #define AAC_DRIVERNAME "aac"
39 /* #include <stddef.h> */
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
49 #include <sys/signalvar.h>
51 #include <sys/eventhandler.h>
54 #include <sys/bus_dma.h>
55 #include <sys/device.h>
56 #include <sys/mplock2.h>
58 #include <bus/pci/pcireg.h>
59 #include <bus/pci/pcivar.h>
61 #include <dev/raid/aac/aacreg.h>
62 #include <dev/raid/aac/aac_ioctl.h>
63 #include <dev/raid/aac/aacvar.h>
64 #include <dev/raid/aac/aac_tables.h>
66 static void aac_startup(void *arg);
67 static void aac_add_container(struct aac_softc *sc,
68 struct aac_mntinforesp *mir, int f);
69 static void aac_get_bus_info(struct aac_softc *sc);
70 static void aac_daemon(void *arg);
72 /* Command Processing */
73 static void aac_timeout(struct aac_softc *sc);
74 static void aac_complete(void *context, int pending);
75 static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
76 static void aac_bio_complete(struct aac_command *cm);
77 static int aac_wait_command(struct aac_command *cm);
78 static void aac_command_thread(void *arg);
80 /* Command Buffer Management */
81 static void aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
83 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
85 static int aac_alloc_commands(struct aac_softc *sc);
86 static void aac_free_commands(struct aac_softc *sc);
87 static void aac_unmap_command(struct aac_command *cm);
89 /* Hardware Interface */
90 static int aac_alloc(struct aac_softc *sc);
91 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
93 static int aac_check_firmware(struct aac_softc *sc);
94 static int aac_init(struct aac_softc *sc);
95 static int aac_sync_command(struct aac_softc *sc, u_int32_t command,
96 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
97 u_int32_t arg3, u_int32_t *sp);
98 static int aac_setup_intr(struct aac_softc *sc);
99 static int aac_enqueue_fib(struct aac_softc *sc, int queue,
100 struct aac_command *cm);
101 static int aac_dequeue_fib(struct aac_softc *sc, int queue,
102 u_int32_t *fib_size, struct aac_fib **fib_addr);
103 static int aac_enqueue_response(struct aac_softc *sc, int queue,
104 struct aac_fib *fib);
106 /* StrongARM interface */
107 static int aac_sa_get_fwstatus(struct aac_softc *sc);
108 static void aac_sa_qnotify(struct aac_softc *sc, int qbit);
109 static int aac_sa_get_istatus(struct aac_softc *sc);
110 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask);
111 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
112 u_int32_t arg0, u_int32_t arg1,
113 u_int32_t arg2, u_int32_t arg3);
114 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb);
115 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable);
117 struct aac_interface aac_sa_interface = {
121 aac_sa_clear_istatus,
124 aac_sa_set_interrupts,
128 /* i960Rx interface */
129 static int aac_rx_get_fwstatus(struct aac_softc *sc);
130 static void aac_rx_qnotify(struct aac_softc *sc, int qbit);
131 static int aac_rx_get_istatus(struct aac_softc *sc);
132 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask);
133 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
134 u_int32_t arg0, u_int32_t arg1,
135 u_int32_t arg2, u_int32_t arg3);
136 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb);
137 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable);
138 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
139 static int aac_rx_get_outb_queue(struct aac_softc *sc);
140 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
142 struct aac_interface aac_rx_interface = {
146 aac_rx_clear_istatus,
149 aac_rx_set_interrupts,
151 aac_rx_get_outb_queue,
152 aac_rx_set_outb_queue
155 /* Rocket/MIPS interface */
156 static int aac_rkt_get_fwstatus(struct aac_softc *sc);
157 static void aac_rkt_qnotify(struct aac_softc *sc, int qbit);
158 static int aac_rkt_get_istatus(struct aac_softc *sc);
159 static void aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
160 static void aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
161 u_int32_t arg0, u_int32_t arg1,
162 u_int32_t arg2, u_int32_t arg3);
163 static int aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
164 static void aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
165 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
166 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
167 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
169 struct aac_interface aac_rkt_interface = {
170 aac_rkt_get_fwstatus,
173 aac_rkt_clear_istatus,
176 aac_rkt_set_interrupts,
177 aac_rkt_send_command,
178 aac_rkt_get_outb_queue,
179 aac_rkt_set_outb_queue
182 /* Debugging and Diagnostics */
183 static void aac_describe_controller(struct aac_softc *sc);
184 static char *aac_describe_code(struct aac_code_lookup *table,
187 /* Management Interface */
188 static d_open_t aac_open;
189 static d_close_t aac_close;
190 static d_ioctl_t aac_ioctl;
191 static d_kqfilter_t aac_kqfilter;
192 static void aac_filter_detach(struct knote *kn);
193 static int aac_filter_read(struct knote *kn, long hint);
194 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
195 static int aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg);
196 static void aac_handle_aif(struct aac_softc *sc,
197 struct aac_fib *fib);
198 static int aac_rev_check(struct aac_softc *sc, caddr_t udata);
199 static int aac_open_aif(struct aac_softc *sc, caddr_t arg);
200 static int aac_close_aif(struct aac_softc *sc, caddr_t arg);
201 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
202 static int aac_return_aif(struct aac_softc *sc,
203 struct aac_fib_context *ctx, caddr_t uptr);
204 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr);
205 static int aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
206 static int aac_supported_features(struct aac_softc *sc, caddr_t uptr);
207 static void aac_ioctl_event(struct aac_softc *sc,
208 struct aac_event *event, void *arg);
209 static struct aac_mntinforesp *
210 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid);
212 static struct dev_ops aac_ops = {
215 .d_close = aac_close,
216 .d_ioctl = aac_ioctl,
217 .d_kqfilter = aac_kqfilter
220 static MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
223 static SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
230 * Initialize the controller and softc
233 aac_attach(struct aac_softc *sc)
237 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
240 * Initialize per-controller queues.
248 * Initialize command-completion task.
250 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
252 /* mark controller as suspended until we get ourselves organised */
253 sc->aac_state |= AAC_STATE_SUSPEND;
256 * Check that the firmware on the card is supported.
258 if ((error = aac_check_firmware(sc)) != 0)
264 lockinit(&sc->aac_aifq_lock, "AAC AIF lock", 0, LK_CANRECURSE);
265 lockinit(&sc->aac_io_lock, "AAC I/O lock", 0, LK_CANRECURSE);
266 lockinit(&sc->aac_container_lock, "AAC container lock", 0, LK_CANRECURSE);
267 TAILQ_INIT(&sc->aac_container_tqh);
268 TAILQ_INIT(&sc->aac_ev_cmfree);
270 /* Initialize the clock daemon callout. */
271 callout_init(&sc->aac_daemontime);
274 * Initialize the adapter.
276 if ((error = aac_alloc(sc)) != 0)
278 if ((error = aac_init(sc)) != 0)
282 * Allocate and connect our interrupt.
284 if ((error = aac_setup_intr(sc)) != 0)
288 * Print a little information about the controller.
290 aac_describe_controller(sc);
295 sysctl_ctx_init(&sc->aac_sysctl_ctx);
296 sc->aac_sysctl_tree = SYSCTL_ADD_NODE(&sc->aac_sysctl_ctx,
297 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
298 device_get_nameunit(sc->aac_dev), CTLFLAG_RD, 0, "");
299 if (sc->aac_sysctl_tree == NULL) {
300 device_printf(sc->aac_dev, "can't add sysctl node\n");
303 SYSCTL_ADD_INT(&sc->aac_sysctl_ctx,
304 SYSCTL_CHILDREN(sc->aac_sysctl_tree),
305 OID_AUTO, "firmware_build", CTLFLAG_RD,
306 &sc->aac_revision.buildNumber, 0,
307 "firmware build number");
310 * Register to probe our containers later.
312 sc->aac_ich.ich_func = aac_startup;
313 sc->aac_ich.ich_arg = sc;
314 sc->aac_ich.ich_desc = "aac";
315 if (config_intrhook_establish(&sc->aac_ich) != 0) {
316 device_printf(sc->aac_dev,
317 "can't establish configuration hook\n");
322 * Make the control device.
324 unit = device_get_unit(sc->aac_dev);
325 sc->aac_dev_t = make_dev(&aac_ops, unit, UID_ROOT, GID_OPERATOR,
326 0640, "aac%d", unit);
327 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
328 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
329 sc->aac_dev_t->si_drv1 = sc;
331 /* Create the AIF thread */
332 if (kthread_create(aac_command_thread, sc,
333 &sc->aifthread, "aac%daif", unit))
334 panic("Could not create AIF thread");
336 /* Register the shutdown method to only be called post-dump */
337 if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
338 sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
339 device_printf(sc->aac_dev,
340 "shutdown event registration failed\n");
342 /* Register with CAM for the non-DASD devices */
343 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
344 TAILQ_INIT(&sc->aac_sim_tqh);
345 aac_get_bus_info(sc);
348 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
349 callout_reset(&sc->aac_daemontime, 60 * hz, aac_daemon, sc);
350 lockmgr(&sc->aac_io_lock, LK_RELEASE);
356 aac_daemon(void *arg)
359 struct aac_softc *sc;
363 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
365 if (callout_pending(&sc->aac_daemontime) ||
366 callout_active(&sc->aac_daemontime) == 0) {
367 lockmgr(&sc->aac_io_lock, LK_RELEASE);
371 aac_alloc_sync_fib(sc, &fib);
372 *(uint32_t *)fib->data = tv.tv_sec;
373 aac_sync_fib(sc, SendHostTime, 0, fib, sizeof(uint32_t));
374 aac_release_sync_fib(sc);
375 lockmgr(&sc->aac_io_lock, LK_RELEASE);
376 callout_reset(&sc->aac_daemontime, 30 * 60 * hz, aac_daemon, sc);
380 aac_add_event(struct aac_softc *sc, struct aac_event *event)
383 switch (event->ev_type & AAC_EVENT_MASK) {
384 case AAC_EVENT_CMFREE:
385 TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
388 device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
397 * Request information of container #cid
399 static struct aac_mntinforesp *
400 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid)
402 struct aac_mntinfo *mi;
404 mi = (struct aac_mntinfo *)&fib->data[0];
405 /* use 64-bit LBA if enabled */
406 mi->Command = (sc->flags & AAC_FLAGS_LBA_64BIT) ?
407 VM_NameServe64 : VM_NameServe;
408 mi->MntType = FT_FILESYS;
411 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
412 sizeof(struct aac_mntinfo))) {
413 device_printf(sc->aac_dev, "Error probing container %d\n", cid);
417 return ((struct aac_mntinforesp *)&fib->data[0]);
421 * Probe for containers, create disks.
424 aac_startup(void *arg)
426 struct aac_softc *sc;
428 struct aac_mntinforesp *mir;
429 int count = 0, i = 0;
431 sc = (struct aac_softc *)arg;
432 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
434 /* disconnect ourselves from the intrhook chain */
435 config_intrhook_disestablish(&sc->aac_ich);
437 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
438 aac_alloc_sync_fib(sc, &fib);
440 /* loop over possible containers */
442 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
445 count = mir->MntRespCount;
446 aac_add_container(sc, mir, 0);
448 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
450 aac_release_sync_fib(sc);
451 lockmgr(&sc->aac_io_lock, LK_RELEASE);
453 /* poke the bus to actually attach the child devices */
454 if (bus_generic_attach(sc->aac_dev))
455 device_printf(sc->aac_dev, "bus_generic_attach failed\n");
457 /* mark the controller up */
458 sc->aac_state &= ~AAC_STATE_SUSPEND;
460 /* enable interrupts now */
461 AAC_UNMASK_INTERRUPTS(sc);
465 * Create a device to represent a new container
468 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
470 struct aac_container *co;
474 * Check container volume type for validity. Note that many of
475 * the possible types may never show up.
477 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
478 co = (struct aac_container *)kmalloc(sizeof *co, M_AACBUF,
480 fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "id %x name '%.16s' size %u type %d",
481 mir->MntTable[0].ObjectId,
482 mir->MntTable[0].FileSystemName,
483 mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
485 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
486 device_printf(sc->aac_dev, "device_add_child failed\n");
488 device_set_ivars(child, co);
489 device_set_desc(child, aac_describe_code(aac_container_types,
490 mir->MntTable[0].VolType));
493 bcopy(&mir->MntTable[0], &co->co_mntobj,
494 sizeof(struct aac_mntobj));
495 lockmgr(&sc->aac_container_lock, LK_EXCLUSIVE);
496 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
497 lockmgr(&sc->aac_container_lock, LK_RELEASE);
502 * Allocate resources associated with (sc)
505 aac_alloc(struct aac_softc *sc)
508 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
511 * Create DMA tag for mapping buffers into controller-addressable space.
513 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
514 1, 0, /* algnmnt, boundary */
515 (sc->flags & AAC_FLAGS_SG_64BIT) ?
517 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
518 BUS_SPACE_MAXADDR, /* highaddr */
519 NULL, NULL, /* filter, filterarg */
520 MAXBSIZE, /* maxsize */
521 sc->aac_sg_tablesize, /* nsegments */
522 MAXBSIZE, /* maxsegsize */
523 BUS_DMA_ALLOCNOW, /* flags */
524 &sc->aac_buffer_dmat)) {
525 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
530 * Create DMA tag for mapping FIBs into controller-addressable space..
532 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
533 1, 0, /* algnmnt, boundary */
534 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
535 BUS_SPACE_MAXADDR_32BIT :
536 0x7fffffff, /* lowaddr */
537 BUS_SPACE_MAXADDR, /* highaddr */
538 NULL, NULL, /* filter, filterarg */
539 sc->aac_max_fibs_alloc *
540 sc->aac_max_fib_size, /* maxsize */
542 sc->aac_max_fibs_alloc *
543 sc->aac_max_fib_size, /* maxsize */
545 &sc->aac_fib_dmat)) {
546 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
551 * Create DMA tag for the common structure and allocate it.
553 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
554 1, 0, /* algnmnt, boundary */
555 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
556 BUS_SPACE_MAXADDR_32BIT :
557 0x7fffffff, /* lowaddr */
558 BUS_SPACE_MAXADDR, /* highaddr */
559 NULL, NULL, /* filter, filterarg */
560 8192 + sizeof(struct aac_common), /* maxsize */
562 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
564 &sc->aac_common_dmat)) {
565 device_printf(sc->aac_dev,
566 "can't allocate common structure DMA tag\n");
569 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
570 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
571 device_printf(sc->aac_dev, "can't allocate common structure\n");
576 * Work around a bug in the 2120 and 2200 that cannot DMA commands
577 * below address 8192 in physical memory.
578 * XXX If the padding is not needed, can it be put to use instead
581 (void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
582 sc->aac_common, 8192 + sizeof(*sc->aac_common),
583 aac_common_map, sc, 0);
585 if (sc->aac_common_busaddr < 8192) {
586 sc->aac_common = (struct aac_common *)
587 ((uint8_t *)sc->aac_common + 8192);
588 sc->aac_common_busaddr += 8192;
590 bzero(sc->aac_common, sizeof(*sc->aac_common));
592 /* Allocate some FIBs and associated command structs */
593 TAILQ_INIT(&sc->aac_fibmap_tqh);
594 sc->aac_commands = kmalloc(sc->aac_max_fibs * sizeof(struct aac_command),
595 M_AACBUF, M_WAITOK|M_ZERO);
596 while (sc->total_fibs < sc->aac_max_fibs) {
597 if (aac_alloc_commands(sc) != 0)
600 if (sc->total_fibs == 0)
607 * Free all of the resources associated with (sc)
609 * Should not be called if the controller is active.
612 aac_free(struct aac_softc *sc)
615 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
617 /* remove the control device */
618 if (sc->aac_dev_t != NULL)
619 destroy_dev(sc->aac_dev_t);
621 /* throw away any FIB buffers, discard the FIB DMA tag */
622 aac_free_commands(sc);
623 if (sc->aac_fib_dmat)
624 bus_dma_tag_destroy(sc->aac_fib_dmat);
626 kfree(sc->aac_commands, M_AACBUF);
628 /* destroy the common area */
629 if (sc->aac_common) {
630 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
631 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
632 sc->aac_common_dmamap);
634 if (sc->aac_common_dmat)
635 bus_dma_tag_destroy(sc->aac_common_dmat);
637 /* disconnect the interrupt handler */
639 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
640 if (sc->aac_irq != NULL)
641 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
644 /* destroy data-transfer DMA tag */
645 if (sc->aac_buffer_dmat)
646 bus_dma_tag_destroy(sc->aac_buffer_dmat);
648 /* destroy the parent DMA tag */
649 if (sc->aac_parent_dmat)
650 bus_dma_tag_destroy(sc->aac_parent_dmat);
652 /* release the register window mapping */
653 if (sc->aac_regs_res0 != NULL)
654 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
655 sc->aac_regs_rid0, sc->aac_regs_res0);
656 if (sc->aac_hwif == AAC_HWIF_NARK && sc->aac_regs_res1 != NULL)
657 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
658 sc->aac_regs_rid1, sc->aac_regs_res1);
659 dev_ops_remove_minor(&aac_ops, device_get_unit(sc->aac_dev));
661 sysctl_ctx_free(&sc->aac_sysctl_ctx);
665 * Disconnect from the controller completely, in preparation for unload.
668 aac_detach(device_t dev)
670 struct aac_softc *sc;
671 struct aac_container *co;
675 sc = device_get_softc(dev);
676 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
678 #if 0 /* XXX swildner */
679 callout_drain(&sc->aac_daemontime);
681 callout_stop(&sc->aac_daemontime);
684 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
685 while (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
686 sc->aifflags |= AAC_AIFFLAGS_EXIT;
687 wakeup(sc->aifthread);
688 lksleep(sc->aac_dev, &sc->aac_io_lock, 0, "aacdch", 0);
690 lockmgr(&sc->aac_io_lock, LK_RELEASE);
691 KASSERT((sc->aifflags & AAC_AIFFLAGS_RUNNING) == 0,
692 ("%s: invalid detach state", __func__));
694 /* Remove the child containers */
695 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
696 error = device_delete_child(dev, co->co_disk);
699 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
703 /* Remove the CAM SIMs */
704 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
705 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
706 error = device_delete_child(dev, sim->sim_dev);
709 kfree(sim, M_AACBUF);
712 if ((error = aac_shutdown(dev)))
715 EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
719 lockuninit(&sc->aac_aifq_lock);
720 lockuninit(&sc->aac_io_lock);
721 lockuninit(&sc->aac_container_lock);
727 * Bring the controller down to a dormant state and detach all child devices.
729 * This function is called before detach or system shutdown.
731 * Note that we can assume that the bioq on the controller is empty, as we won't
732 * allow shutdown if any device is open.
735 aac_shutdown(device_t dev)
737 struct aac_softc *sc;
739 struct aac_close_command *cc;
741 sc = device_get_softc(dev);
742 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
744 sc->aac_state |= AAC_STATE_SUSPEND;
747 * Send a Container shutdown followed by a HostShutdown FIB to the
748 * controller to convince it that we don't want to talk to it anymore.
749 * We've been closed and all I/O completed already
751 device_printf(sc->aac_dev, "shutting down controller...");
753 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
754 aac_alloc_sync_fib(sc, &fib);
755 cc = (struct aac_close_command *)&fib->data[0];
757 bzero(cc, sizeof(struct aac_close_command));
758 cc->Command = VM_CloseAll;
759 cc->ContainerId = 0xffffffff;
760 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
761 sizeof(struct aac_close_command)))
762 kprintf("FAILED.\n");
769 * XXX Issuing this command to the controller makes it shut down
770 * but also keeps it from coming back up without a reset of the
771 * PCI bus. This is not desirable if you are just unloading the
772 * driver module with the intent to reload it later.
774 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
776 kprintf("FAILED.\n");
783 AAC_MASK_INTERRUPTS(sc);
784 aac_release_sync_fib(sc);
785 lockmgr(&sc->aac_io_lock, LK_RELEASE);
791 * Bring the controller to a quiescent state, ready for system suspend.
794 aac_suspend(device_t dev)
796 struct aac_softc *sc;
798 sc = device_get_softc(dev);
800 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
801 sc->aac_state |= AAC_STATE_SUSPEND;
803 AAC_MASK_INTERRUPTS(sc);
808 * Bring the controller back to a state ready for operation.
811 aac_resume(device_t dev)
813 struct aac_softc *sc;
815 sc = device_get_softc(dev);
817 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
818 sc->aac_state &= ~AAC_STATE_SUSPEND;
819 AAC_UNMASK_INTERRUPTS(sc);
824 * Interrupt handler for NEW_COMM interface.
827 aac_new_intr(void *arg)
829 struct aac_softc *sc;
830 u_int32_t index, fast;
831 struct aac_command *cm;
835 sc = (struct aac_softc *)arg;
837 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
838 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
840 index = AAC_GET_OUTB_QUEUE(sc);
841 if (index == 0xffffffff)
842 index = AAC_GET_OUTB_QUEUE(sc);
843 if (index == 0xffffffff)
846 if (index == 0xfffffffe) {
847 /* XXX This means that the controller wants
848 * more work. Ignore it for now.
853 fib = (struct aac_fib *)kmalloc(sizeof *fib, M_AACBUF,
856 for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
857 ((u_int32_t *)fib)[i] = AAC_MEM1_GETREG4(sc, index + i*4);
858 aac_handle_aif(sc, fib);
859 kfree(fib, M_AACBUF);
862 * AIF memory is owned by the adapter, so let it
863 * know that we are done with it.
865 AAC_SET_OUTB_QUEUE(sc, index);
866 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
869 cm = sc->aac_commands + (index >> 2);
872 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
873 *((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
876 aac_unmap_command(cm);
877 cm->cm_flags |= AAC_CMD_COMPLETED;
879 /* is there a completion handler? */
880 if (cm->cm_complete != NULL) {
883 /* assume that someone is sleeping on this
888 sc->flags &= ~AAC_QUEUE_FRZN;
891 /* see if we can start some more I/O */
892 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
895 lockmgr(&sc->aac_io_lock, LK_RELEASE);
899 * Interrupt filter for !NEW_COMM interface.
902 aac_filter(void *arg)
904 struct aac_softc *sc;
907 sc = (struct aac_softc *)arg;
909 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
911 * Read the status register directly. This is faster than taking the
912 * driver lock and reading the queues directly. It also saves having
913 * to turn parts of the driver lock into a spin mutex, which would be
916 reason = AAC_GET_ISTATUS(sc);
917 AAC_CLEAR_ISTATUS(sc, reason);
919 /* handle completion processing */
920 if (reason & AAC_DB_RESPONSE_READY)
921 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
923 /* controller wants to talk to us */
924 if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
926 * XXX Make sure that we don't get fooled by strange messages
927 * that start with a NULL.
929 if ((reason & AAC_DB_PRINTF) &&
930 (sc->aac_common->ac_printf[0] == 0))
931 sc->aac_common->ac_printf[0] = 32;
934 * This might miss doing the actual wakeup. However, the
935 * lksleep that this is waking up has a timeout, so it will
936 * wake up eventually. AIFs and printfs are low enough
937 * priority that they can handle hanging out for a few seconds
940 wakeup(sc->aifthread);
949 * Start as much queued I/O as possible on the controller
952 aac_startio(struct aac_softc *sc)
954 struct aac_command *cm;
957 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
961 * This flag might be set if the card is out of resources.
962 * Checking it here prevents an infinite loop of deferrals.
964 if (sc->flags & AAC_QUEUE_FRZN)
968 * Try to get a command that's been put off for lack of
971 cm = aac_dequeue_ready(sc);
974 * Try to build a command off the bio queue (ignore error
978 aac_bio_command(sc, &cm);
984 /* don't map more than once */
985 if (cm->cm_flags & AAC_CMD_MAPPED)
986 panic("aac: command %p already mapped", cm);
989 * Set up the command to go to the controller. If there are no
990 * data buffers associated with the command then it can bypass
993 if (cm->cm_datalen != 0) {
994 error = bus_dmamap_load(sc->aac_buffer_dmat,
995 cm->cm_datamap, cm->cm_data,
997 aac_map_command_sg, cm, 0);
998 if (error == EINPROGRESS) {
999 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "freezing queue\n");
1000 sc->flags |= AAC_QUEUE_FRZN;
1002 } else if (error != 0)
1003 panic("aac_startio: unexpected error %d from "
1006 aac_map_command_sg(cm, NULL, 0, 0);
1011 * Handle notification of one or more FIBs coming from the controller.
1014 aac_command_thread(void *arg)
1016 struct aac_softc *sc = arg;
1017 struct aac_fib *fib;
1021 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1023 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1024 sc->aifflags = AAC_AIFFLAGS_RUNNING;
1026 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1029 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1030 retval = lksleep(sc->aifthread, &sc->aac_io_lock, 0,
1031 "aifthd", AAC_PERIODIC_INTERVAL * hz);
1034 * First see if any FIBs need to be allocated. This needs
1035 * to be called without the driver lock because contigmalloc
1038 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1039 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1040 aac_alloc_commands(sc);
1041 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1042 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1047 * While we're here, check to see if any commands are stuck.
1048 * This is pretty low-priority, so it's ok if it doesn't
1051 if (retval == EWOULDBLOCK)
1054 /* Check the hardware printf message buffer */
1055 if (sc->aac_common->ac_printf[0] != 0)
1056 aac_print_printf(sc);
1058 /* Also check to see if the adapter has a command for us. */
1059 if (sc->flags & AAC_FLAGS_NEW_COMM)
1062 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
1066 AAC_PRINT_FIB(sc, fib);
1068 switch (fib->Header.Command) {
1070 aac_handle_aif(sc, fib);
1073 device_printf(sc->aac_dev, "unknown command "
1074 "from controller\n");
1078 if ((fib->Header.XferState == 0) ||
1079 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
1083 /* Return the AIF to the controller. */
1084 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
1085 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
1086 *(AAC_FSAStatus*)fib->data = ST_OK;
1088 /* XXX Compute the Size field? */
1089 size = fib->Header.Size;
1090 if (size > sizeof(struct aac_fib)) {
1091 size = sizeof(struct aac_fib);
1092 fib->Header.Size = size;
1095 * Since we did not generate this command, it
1096 * cannot go through the normal
1097 * enqueue->startio chain.
1099 aac_enqueue_response(sc,
1100 AAC_ADAP_NORM_RESP_QUEUE,
1105 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1106 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1107 wakeup(sc->aac_dev);
1111 * Process completed commands.
1114 aac_complete(void *context, int pending)
1116 struct aac_softc *sc;
1117 struct aac_command *cm;
1118 struct aac_fib *fib;
1121 sc = (struct aac_softc *)context;
1122 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1124 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1126 /* pull completed commands off the queue */
1128 /* look for completed FIBs on our queue */
1129 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1131 break; /* nothing to do */
1133 /* get the command, unmap and hand off for processing */
1134 cm = sc->aac_commands + fib->Header.SenderData;
1136 AAC_PRINT_FIB(sc, fib);
1139 if ((cm->cm_flags & AAC_CMD_TIMEDOUT) != 0)
1140 device_printf(sc->aac_dev,
1141 "COMMAND %p COMPLETED AFTER %d SECONDS\n",
1142 cm, (int)(time_second-cm->cm_timestamp));
1144 aac_remove_busy(cm);
1146 aac_unmap_command(cm);
1147 cm->cm_flags |= AAC_CMD_COMPLETED;
1149 /* is there a completion handler? */
1150 if (cm->cm_complete != NULL) {
1151 cm->cm_complete(cm);
1153 /* assume that someone is sleeping on this command */
1158 /* see if we can start some more I/O */
1159 sc->flags &= ~AAC_QUEUE_FRZN;
1162 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1166 * Handle a bio submitted from a disk device.
1169 aac_submit_bio(struct aac_disk *ad, struct bio *bio)
1171 struct aac_softc *sc;
1173 bio->bio_driver_info = ad;
1174 sc = ad->ad_controller;
1175 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1177 /* queue the BIO and try to get some work done */
1178 aac_enqueue_bio(sc, bio);
1183 * Get a bio and build a command to go with it.
1186 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1188 struct aac_command *cm;
1189 struct aac_fib *fib;
1190 struct aac_disk *ad;
1194 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1196 /* get the resources we will need */
1199 if (aac_alloc_command(sc, &cm)) /* get a command */
1201 if ((bio = aac_dequeue_bio(sc)) == NULL)
1204 /* fill out the command */
1206 cm->cm_data = (void *)bp->b_data;
1207 cm->cm_datalen = bp->b_bcount;
1208 cm->cm_complete = aac_bio_complete;
1209 cm->cm_private = bio;
1210 cm->cm_timestamp = time_second;
1214 fib->Header.Size = sizeof(struct aac_fib_header);
1215 fib->Header.XferState =
1216 AAC_FIBSTATE_HOSTOWNED |
1217 AAC_FIBSTATE_INITIALISED |
1218 AAC_FIBSTATE_EMPTY |
1219 AAC_FIBSTATE_FROMHOST |
1220 AAC_FIBSTATE_REXPECTED |
1222 AAC_FIBSTATE_ASYNC |
1223 AAC_FIBSTATE_FAST_RESPONSE;
1225 /* build the read/write request */
1226 ad = (struct aac_disk *)bio->bio_driver_info;
1228 if (sc->flags & AAC_FLAGS_RAW_IO) {
1229 struct aac_raw_io *raw;
1230 raw = (struct aac_raw_io *)&fib->data[0];
1231 fib->Header.Command = RawIo;
1232 raw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1233 raw->ByteCount = bp->b_bcount;
1234 raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1236 raw->BpComplete = 0;
1237 fib->Header.Size += sizeof(struct aac_raw_io);
1238 cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1239 if (bp->b_cmd == BUF_CMD_READ) {
1241 cm->cm_flags |= AAC_CMD_DATAIN;
1244 cm->cm_flags |= AAC_CMD_DATAOUT;
1246 } else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1247 fib->Header.Command = ContainerCommand;
1248 if (bp->b_cmd == BUF_CMD_READ) {
1249 struct aac_blockread *br;
1250 br = (struct aac_blockread *)&fib->data[0];
1251 br->Command = VM_CtBlockRead;
1252 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1253 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1254 br->ByteCount = bp->b_bcount;
1255 fib->Header.Size += sizeof(struct aac_blockread);
1256 cm->cm_sgtable = &br->SgMap;
1257 cm->cm_flags |= AAC_CMD_DATAIN;
1259 struct aac_blockwrite *bw;
1260 bw = (struct aac_blockwrite *)&fib->data[0];
1261 bw->Command = VM_CtBlockWrite;
1262 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1263 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1264 bw->ByteCount = bp->b_bcount;
1265 bw->Stable = CUNSTABLE;
1266 fib->Header.Size += sizeof(struct aac_blockwrite);
1267 cm->cm_flags |= AAC_CMD_DATAOUT;
1268 cm->cm_sgtable = &bw->SgMap;
1271 fib->Header.Command = ContainerCommand64;
1272 if (bp->b_cmd == BUF_CMD_READ) {
1273 struct aac_blockread64 *br;
1274 br = (struct aac_blockread64 *)&fib->data[0];
1275 br->Command = VM_CtHostRead64;
1276 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1277 br->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1278 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1281 fib->Header.Size += sizeof(struct aac_blockread64);
1282 cm->cm_flags |= AAC_CMD_DATAIN;
1283 cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1285 struct aac_blockwrite64 *bw;
1286 bw = (struct aac_blockwrite64 *)&fib->data[0];
1287 bw->Command = VM_CtHostWrite64;
1288 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1289 bw->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1290 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1293 fib->Header.Size += sizeof(struct aac_blockwrite64);
1294 cm->cm_flags |= AAC_CMD_DATAOUT;
1295 cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1304 aac_enqueue_bio(sc, bio);
1306 aac_release_command(cm);
1311 * Handle a bio-instigated command that has been completed.
1314 aac_bio_complete(struct aac_command *cm)
1316 struct aac_blockread_response *brr;
1317 struct aac_blockwrite_response *bwr;
1321 AAC_FSAStatus status;
1323 /* fetch relevant status and then release the command */
1324 bio = (struct bio *)cm->cm_private;
1326 if (bp->b_cmd == BUF_CMD_READ) {
1327 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1328 status = brr->Status;
1330 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1331 status = bwr->Status;
1333 aac_release_command(cm);
1335 /* fix up the bio based on status */
1336 if (status == ST_OK) {
1341 bp->b_flags |= B_ERROR;
1342 /* pass an error string out to the disk layer */
1343 code = aac_describe_code(aac_command_status_table, status);
1345 aac_biodone(bio, code);
1349 * Submit a command to the controller, return when it completes.
1350 * XXX This is very dangerous! If the card has gone out to lunch, we could
1351 * be stuck here forever. At the same time, signals are not caught
1352 * because there is a risk that a signal could wakeup the sleep before
1353 * the card has a chance to complete the command. Since there is no way
1354 * to cancel a command that is in progress, we can't protect against the
1355 * card completing a command late and spamming the command and data
1356 * memory. So, we are held hostage until the command completes.
1359 aac_wait_command(struct aac_command *cm)
1361 struct aac_softc *sc;
1365 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1367 /* Put the command on the ready queue and get things going */
1368 aac_enqueue_ready(cm);
1370 error = lksleep(cm, &sc->aac_io_lock, 0, "aacwait", 0);
1375 *Command Buffer Management
1379 * Allocate a command.
1382 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1384 struct aac_command *cm;
1386 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1388 if ((cm = aac_dequeue_free(sc)) == NULL) {
1389 if (sc->total_fibs < sc->aac_max_fibs) {
1390 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1391 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1392 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1393 wakeup(sc->aifthread);
1403 * Release a command back to the freelist.
1406 aac_release_command(struct aac_command *cm)
1408 struct aac_event *event;
1409 struct aac_softc *sc;
1412 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1414 /* (re)initialize the command/FIB */
1415 cm->cm_sgtable = NULL;
1417 cm->cm_complete = NULL;
1418 cm->cm_private = NULL;
1419 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1420 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1421 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1422 cm->cm_fib->Header.Flags = 0;
1423 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1426 * These are duplicated in aac_start to cover the case where an
1427 * intermediate stage may have destroyed them. They're left
1428 * initialized here for debugging purposes only.
1430 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1431 cm->cm_fib->Header.SenderData = 0;
1433 aac_enqueue_free(cm);
1435 if ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1436 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1437 event->ev_callback(sc, event, event->ev_arg);
1442 * Map helper for command/FIB allocation.
1445 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1449 fibphys = (uint64_t *)arg;
1451 *fibphys = segs[0].ds_addr;
1455 * Allocate and initialize commands/FIBs for this adapter.
1458 aac_alloc_commands(struct aac_softc *sc)
1460 struct aac_command *cm;
1461 struct aac_fibmap *fm;
1465 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1467 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1470 fm = kmalloc(sizeof(struct aac_fibmap), M_AACBUF, M_INTWAIT | M_ZERO);
1472 /* allocate the FIBs in DMAable memory and load them */
1473 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1474 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1475 device_printf(sc->aac_dev,
1476 "Not enough contiguous memory available.\n");
1477 kfree(fm, M_AACBUF);
1481 /* Ignore errors since this doesn't bounce */
1482 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1483 sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1484 aac_map_command_helper, &fibphys, 0);
1486 /* initialize constant fields in the command structure */
1487 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1488 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1489 cm = sc->aac_commands + sc->total_fibs;
1490 fm->aac_commands = cm;
1492 cm->cm_fib = (struct aac_fib *)
1493 ((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1494 cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1495 cm->cm_index = sc->total_fibs;
1497 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1498 &cm->cm_datamap)) != 0)
1500 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1501 aac_release_command(cm);
1503 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1507 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1508 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1509 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1510 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1514 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1515 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1516 kfree(fm, M_AACBUF);
1521 * Free FIBs owned by this adapter.
1524 aac_free_commands(struct aac_softc *sc)
1526 struct aac_fibmap *fm;
1527 struct aac_command *cm;
1530 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1532 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1534 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1536 * We check against total_fibs to handle partially
1539 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1540 cm = fm->aac_commands + i;
1541 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1543 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1544 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1545 kfree(fm, M_AACBUF);
1550 * Command-mapping helper function - populate this command's s/g table.
1553 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1555 struct aac_softc *sc;
1556 struct aac_command *cm;
1557 struct aac_fib *fib;
1560 cm = (struct aac_command *)arg;
1563 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1565 /* copy into the FIB */
1566 if (cm->cm_sgtable != NULL) {
1567 if (fib->Header.Command == RawIo) {
1568 struct aac_sg_tableraw *sg;
1569 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1571 for (i = 0; i < nseg; i++) {
1572 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1573 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1574 sg->SgEntryRaw[i].Next = 0;
1575 sg->SgEntryRaw[i].Prev = 0;
1576 sg->SgEntryRaw[i].Flags = 0;
1578 /* update the FIB size for the s/g count */
1579 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1580 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1581 struct aac_sg_table *sg;
1582 sg = cm->cm_sgtable;
1584 for (i = 0; i < nseg; i++) {
1585 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1586 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1588 /* update the FIB size for the s/g count */
1589 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1591 struct aac_sg_table64 *sg;
1592 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1594 for (i = 0; i < nseg; i++) {
1595 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1596 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1598 /* update the FIB size for the s/g count */
1599 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1603 /* Fix up the address values in the FIB. Use the command array index
1604 * instead of a pointer since these fields are only 32 bits. Shift
1605 * the SenderFibAddress over to make room for the fast response bit
1606 * and for the AIF bit
1608 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1609 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1611 /* save a pointer to the command for speedy reverse-lookup */
1612 cm->cm_fib->Header.SenderData = cm->cm_index;
1614 if (cm->cm_flags & AAC_CMD_DATAIN)
1615 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1616 BUS_DMASYNC_PREREAD);
1617 if (cm->cm_flags & AAC_CMD_DATAOUT)
1618 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1619 BUS_DMASYNC_PREWRITE);
1620 cm->cm_flags |= AAC_CMD_MAPPED;
1622 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1623 int count = 10000000L;
1624 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1626 aac_unmap_command(cm);
1627 sc->flags |= AAC_QUEUE_FRZN;
1628 aac_requeue_ready(cm);
1630 DELAY(5); /* wait 5 usec. */
1633 /* Put the FIB on the outbound queue */
1634 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1635 aac_unmap_command(cm);
1636 sc->flags |= AAC_QUEUE_FRZN;
1637 aac_requeue_ready(cm);
1645 * Unmap a command from controller-visible space.
1648 aac_unmap_command(struct aac_command *cm)
1650 struct aac_softc *sc;
1653 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1655 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1658 if (cm->cm_datalen != 0) {
1659 if (cm->cm_flags & AAC_CMD_DATAIN)
1660 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1661 BUS_DMASYNC_POSTREAD);
1662 if (cm->cm_flags & AAC_CMD_DATAOUT)
1663 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1664 BUS_DMASYNC_POSTWRITE);
1666 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1668 cm->cm_flags &= ~AAC_CMD_MAPPED;
1672 * Hardware Interface
1676 * Initialize the adapter.
1679 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1681 struct aac_softc *sc;
1683 sc = (struct aac_softc *)arg;
1684 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1686 sc->aac_common_busaddr = segs[0].ds_addr;
1690 aac_check_firmware(struct aac_softc *sc)
1692 u_int32_t code, major, minor, options = 0, atu_size = 0;
1696 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1698 * Wait for the adapter to come ready.
1702 code = AAC_GET_FWSTATUS(sc);
1703 if (code & AAC_SELF_TEST_FAILED) {
1704 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1707 if (code & AAC_KERNEL_PANIC) {
1708 device_printf(sc->aac_dev,
1709 "FATAL: controller kernel panic");
1712 if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1713 device_printf(sc->aac_dev,
1714 "FATAL: controller not coming ready, "
1715 "status %x\n", code);
1718 } while (!(code & AAC_UP_AND_RUNNING));
1721 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1722 * firmware version 1.x are not compatible with this driver.
1724 if (sc->flags & AAC_FLAGS_PERC2QC) {
1725 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1727 device_printf(sc->aac_dev,
1728 "Error reading firmware version\n");
1732 /* These numbers are stored as ASCII! */
1733 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1734 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1736 device_printf(sc->aac_dev,
1737 "Firmware version %d.%d is not supported.\n",
1744 * Retrieve the capabilities/supported options word so we know what
1745 * work-arounds to enable. Some firmware revs don't support this
1748 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1749 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1750 device_printf(sc->aac_dev,
1751 "RequestAdapterInfo failed\n");
1755 options = AAC_GET_MAILBOX(sc, 1);
1756 atu_size = AAC_GET_MAILBOX(sc, 2);
1757 sc->supported_options = options;
1759 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1760 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1761 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1762 if (options & AAC_SUPPORTED_NONDASD)
1763 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1764 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1765 && (sizeof(bus_addr_t) > 4)) {
1766 device_printf(sc->aac_dev,
1767 "Enabling 64-bit address support\n");
1768 sc->flags |= AAC_FLAGS_SG_64BIT;
1770 if ((options & AAC_SUPPORTED_NEW_COMM)
1771 && sc->aac_if.aif_send_command)
1772 sc->flags |= AAC_FLAGS_NEW_COMM;
1773 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1774 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1777 /* Check for broken hardware that does a lower number of commands */
1778 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1780 /* Remap mem. resource, if required */
1781 if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1782 atu_size > rman_get_size(sc->aac_regs_res1)) {
1783 bus_release_resource(
1784 sc->aac_dev, SYS_RES_MEMORY,
1785 sc->aac_regs_rid1, sc->aac_regs_res1);
1786 sc->aac_regs_res1 = bus_alloc_resource(
1787 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid1,
1788 0ul, ~0ul, atu_size, RF_ACTIVE);
1789 if (sc->aac_regs_res1 == NULL) {
1790 sc->aac_regs_res1 = bus_alloc_resource_any(
1791 sc->aac_dev, SYS_RES_MEMORY,
1792 &sc->aac_regs_rid1, RF_ACTIVE);
1793 if (sc->aac_regs_res1 == NULL) {
1794 device_printf(sc->aac_dev,
1795 "couldn't allocate register window\n");
1798 sc->flags &= ~AAC_FLAGS_NEW_COMM;
1800 sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
1801 sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
1803 if (sc->aac_hwif == AAC_HWIF_NARK) {
1804 sc->aac_regs_res0 = sc->aac_regs_res1;
1805 sc->aac_regs_rid0 = sc->aac_regs_rid1;
1806 sc->aac_btag0 = sc->aac_btag1;
1807 sc->aac_bhandle0 = sc->aac_bhandle1;
1811 /* Read preferred settings */
1812 sc->aac_max_fib_size = sizeof(struct aac_fib);
1813 sc->aac_max_sectors = 128; /* 64KB */
1814 if (sc->flags & AAC_FLAGS_SG_64BIT)
1815 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1816 - sizeof(struct aac_blockwrite64))
1817 / sizeof(struct aac_sg_entry64);
1819 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1820 - sizeof(struct aac_blockwrite))
1821 / sizeof(struct aac_sg_entry);
1823 if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1824 options = AAC_GET_MAILBOX(sc, 1);
1825 sc->aac_max_fib_size = (options & 0xFFFF);
1826 sc->aac_max_sectors = (options >> 16) << 1;
1827 options = AAC_GET_MAILBOX(sc, 2);
1828 sc->aac_sg_tablesize = (options >> 16);
1829 options = AAC_GET_MAILBOX(sc, 3);
1830 sc->aac_max_fibs = (options & 0xFFFF);
1832 if (sc->aac_max_fib_size > PAGE_SIZE)
1833 sc->aac_max_fib_size = PAGE_SIZE;
1834 sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1836 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1837 sc->flags |= AAC_FLAGS_RAW_IO;
1838 device_printf(sc->aac_dev, "Enable Raw I/O\n");
1840 if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1841 (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1842 sc->flags |= AAC_FLAGS_LBA_64BIT;
1843 device_printf(sc->aac_dev, "Enable 64-bit array\n");
1850 aac_init(struct aac_softc *sc)
1852 struct aac_adapter_init *ip;
1856 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1859 * Fill in the init structure. This tells the adapter about the
1860 * physical location of various important shared data structures.
1862 ip = &sc->aac_common->ac_init;
1863 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1864 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1865 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1866 sc->flags |= AAC_FLAGS_RAW_IO;
1868 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1870 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1871 offsetof(struct aac_common, ac_fibs);
1872 ip->AdapterFibsVirtualAddress = 0;
1873 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1874 ip->AdapterFibAlign = sizeof(struct aac_fib);
1876 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1877 offsetof(struct aac_common, ac_printf);
1878 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1881 * The adapter assumes that pages are 4K in size, except on some
1882 * broken firmware versions that do the page->byte conversion twice,
1883 * therefore 'assuming' that this value is in 16MB units (2^24).
1884 * Round up since the granularity is so high.
1886 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1887 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1888 ip->HostPhysMemPages =
1889 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1891 ip->HostElapsedSeconds = time_second; /* reset later if invalid */
1894 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1895 ip->InitFlags |= AAC_INITFLAGS_NEW_COMM_SUPPORTED;
1896 device_printf(sc->aac_dev, "New comm. interface enabled\n");
1899 ip->MaxIoCommands = sc->aac_max_fibs;
1900 ip->MaxIoSize = sc->aac_max_sectors << 9;
1901 ip->MaxFibSize = sc->aac_max_fib_size;
1904 * Initialize FIB queues. Note that it appears that the layout of the
1905 * indexes and the segmentation of the entries may be mandated by the
1906 * adapter, which is only told about the base of the queue index fields.
1908 * The initial values of the indices are assumed to inform the adapter
1909 * of the sizes of the respective queues, and theoretically it could
1910 * work out the entire layout of the queue structures from this. We
1911 * take the easy route and just lay this area out like everyone else
1914 * The Linux driver uses a much more complex scheme whereby several
1915 * header records are kept for each queue. We use a couple of generic
1916 * list manipulation functions which 'know' the size of each list by
1917 * virtue of a table.
1919 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1920 qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1922 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1923 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1925 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1926 AAC_HOST_NORM_CMD_ENTRIES;
1927 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1928 AAC_HOST_NORM_CMD_ENTRIES;
1929 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1930 AAC_HOST_HIGH_CMD_ENTRIES;
1931 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1932 AAC_HOST_HIGH_CMD_ENTRIES;
1933 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1934 AAC_ADAP_NORM_CMD_ENTRIES;
1935 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1936 AAC_ADAP_NORM_CMD_ENTRIES;
1937 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1938 AAC_ADAP_HIGH_CMD_ENTRIES;
1939 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1940 AAC_ADAP_HIGH_CMD_ENTRIES;
1941 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1942 AAC_HOST_NORM_RESP_ENTRIES;
1943 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1944 AAC_HOST_NORM_RESP_ENTRIES;
1945 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1946 AAC_HOST_HIGH_RESP_ENTRIES;
1947 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1948 AAC_HOST_HIGH_RESP_ENTRIES;
1949 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1950 AAC_ADAP_NORM_RESP_ENTRIES;
1951 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1952 AAC_ADAP_NORM_RESP_ENTRIES;
1953 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1954 AAC_ADAP_HIGH_RESP_ENTRIES;
1955 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1956 AAC_ADAP_HIGH_RESP_ENTRIES;
1957 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1958 &sc->aac_queues->qt_HostNormCmdQueue[0];
1959 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1960 &sc->aac_queues->qt_HostHighCmdQueue[0];
1961 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1962 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1963 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1964 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1965 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1966 &sc->aac_queues->qt_HostNormRespQueue[0];
1967 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1968 &sc->aac_queues->qt_HostHighRespQueue[0];
1969 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1970 &sc->aac_queues->qt_AdapNormRespQueue[0];
1971 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1972 &sc->aac_queues->qt_AdapHighRespQueue[0];
1975 * Do controller-type-specific initialisation
1977 switch (sc->aac_hwif) {
1978 case AAC_HWIF_I960RX:
1979 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, ~0);
1982 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, ~0);
1989 * Give the init structure to the controller.
1991 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1992 sc->aac_common_busaddr +
1993 offsetof(struct aac_common, ac_init), 0, 0, 0,
1995 device_printf(sc->aac_dev,
1996 "error establishing init structure\n");
2007 aac_setup_intr(struct aac_softc *sc)
2009 sc->aac_irq_rid = 0;
2010 if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
2013 RF_ACTIVE)) == NULL) {
2014 device_printf(sc->aac_dev, "can't allocate interrupt\n");
2017 if (sc->flags & AAC_FLAGS_NEW_COMM) {
2018 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2020 aac_new_intr, sc, &sc->aac_intr, NULL)) {
2021 device_printf(sc->aac_dev, "can't set up interrupt\n");
2025 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2027 sc, &sc->aac_intr, NULL)) {
2028 device_printf(sc->aac_dev,
2029 "can't set up interrupt filter\n");
2037 * Send a synchronous command to the controller and wait for a result.
2038 * Indicate if the controller completed the command with an error status.
2041 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2042 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2048 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2050 /* populate the mailbox */
2051 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2053 /* ensure the sync command doorbell flag is cleared */
2054 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2056 /* then set it to signal the adapter */
2057 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2059 /* spin waiting for the command to complete */
2062 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
2063 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2066 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2068 /* clear the completion flag */
2069 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2071 /* get the command status */
2072 status = AAC_GET_MAILBOX(sc, 0);
2076 if (status != AAC_SRB_STS_SUCCESS)
2082 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2083 struct aac_fib *fib, u_int16_t datasize)
2085 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2086 #if 0 /* XXX swildner */
2087 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2090 if (datasize > AAC_FIB_DATASIZE)
2094 * Set up the sync FIB
2096 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2097 AAC_FIBSTATE_INITIALISED |
2099 fib->Header.XferState |= xferstate;
2100 fib->Header.Command = command;
2101 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2102 fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2103 fib->Header.SenderSize = sizeof(struct aac_fib);
2104 fib->Header.SenderFibAddress = 0; /* Not needed */
2105 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2106 offsetof(struct aac_common,
2110 * Give the FIB to the controller, wait for a response.
2112 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2113 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2114 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2122 * Adapter-space FIB queue manipulation
2124 * Note that the queue implementation here is a little funky; neither the PI or
2125 * CI will ever be zero. This behaviour is a controller feature.
2131 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2132 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
2133 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2134 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2135 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2136 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
2137 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2138 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2142 * Atomically insert an entry into the nominated queue, returns 0 on success or
2143 * EBUSY if the queue is full.
2145 * Note: it would be more efficient to defer notifying the controller in
2146 * the case where we may be inserting several entries in rapid succession,
2147 * but implementing this usefully may be difficult (it would involve a
2148 * separate queue/notify interface).
2151 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2158 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2160 fib_size = cm->cm_fib->Header.Size;
2161 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2163 /* get the producer/consumer indices */
2164 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2165 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2167 /* wrap the queue? */
2168 if (pi >= aac_qinfo[queue].size)
2171 /* check for queue full */
2172 if ((pi + 1) == ci) {
2178 * To avoid a race with its completion interrupt, place this command on
2179 * the busy queue prior to advertising it to the controller.
2181 aac_enqueue_busy(cm);
2183 /* populate queue entry */
2184 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2185 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2187 /* update producer index */
2188 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2190 /* notify the adapter if we know how */
2191 if (aac_qinfo[queue].notify != 0)
2192 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2201 * Atomically remove one entry from the nominated queue, returns 0 on
2202 * success or ENOENT if the queue is empty.
2205 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2206 struct aac_fib **fib_addr)
2209 u_int32_t fib_index;
2213 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2215 /* get the producer/consumer indices */
2216 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2217 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2219 /* check for queue empty */
2225 /* wrap the pi so the following test works */
2226 if (pi >= aac_qinfo[queue].size)
2233 /* wrap the queue? */
2234 if (ci >= aac_qinfo[queue].size)
2237 /* fetch the entry */
2238 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2241 case AAC_HOST_NORM_CMD_QUEUE:
2242 case AAC_HOST_HIGH_CMD_QUEUE:
2244 * The aq_fib_addr is only 32 bits wide so it can't be counted
2245 * on to hold an address. For AIF's, the adapter assumes
2246 * that it's giving us an address into the array of AIF fibs.
2247 * Therefore, we have to convert it to an index.
2249 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2250 sizeof(struct aac_fib);
2251 *fib_addr = &sc->aac_common->ac_fibs[fib_index];
2254 case AAC_HOST_NORM_RESP_QUEUE:
2255 case AAC_HOST_HIGH_RESP_QUEUE:
2257 struct aac_command *cm;
2260 * As above, an index is used instead of an actual address.
2261 * Gotta shift the index to account for the fast response
2262 * bit. No other correction is needed since this value was
2263 * originally provided by the driver via the SenderFibAddress
2266 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2267 cm = sc->aac_commands + (fib_index >> 2);
2268 *fib_addr = cm->cm_fib;
2271 * Is this a fast response? If it is, update the fib fields in
2272 * local memory since the whole fib isn't DMA'd back up.
2274 if (fib_index & 0x01) {
2275 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2276 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2281 panic("Invalid queue in aac_dequeue_fib()");
2285 /* update consumer index */
2286 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2288 /* if we have made the queue un-full, notify the adapter */
2289 if (notify && (aac_qinfo[queue].notify != 0))
2290 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2298 * Put our response to an Adapter Initialed Fib on the response queue
2301 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2308 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2310 /* Tell the adapter where the FIB is */
2311 fib_size = fib->Header.Size;
2312 fib_addr = fib->Header.SenderFibAddress;
2313 fib->Header.ReceiverFibAddress = fib_addr;
2315 /* get the producer/consumer indices */
2316 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2317 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2319 /* wrap the queue? */
2320 if (pi >= aac_qinfo[queue].size)
2323 /* check for queue full */
2324 if ((pi + 1) == ci) {
2329 /* populate queue entry */
2330 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2331 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2333 /* update producer index */
2334 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2336 /* notify the adapter if we know how */
2337 if (aac_qinfo[queue].notify != 0)
2338 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2347 * Check for commands that have been outstanding for a suspiciously long time,
2348 * and complain about them.
2351 aac_timeout(struct aac_softc *sc)
2353 struct aac_command *cm;
2358 * Traverse the busy command list, bitch about late commands once
2362 deadline = time_second - AAC_CMD_TIMEOUT;
2363 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2364 if ((cm->cm_timestamp < deadline)
2365 && !(cm->cm_flags & AAC_CMD_TIMEDOUT)) {
2366 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2367 device_printf(sc->aac_dev,
2368 "COMMAND %p (TYPE %d) TIMEOUT AFTER %d SECONDS\n",
2369 cm, cm->cm_fib->Header.Command,
2370 (int)(time_second-cm->cm_timestamp));
2371 AAC_PRINT_FIB(sc, cm->cm_fib);
2377 code = AAC_GET_FWSTATUS(sc);
2378 if (code != AAC_UP_AND_RUNNING) {
2379 device_printf(sc->aac_dev, "WARNING! Controller is no "
2380 "longer running! code= 0x%x\n", code);
2387 * Interface Function Vectors
2391 * Read the current firmware status word.
2394 aac_sa_get_fwstatus(struct aac_softc *sc)
2396 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2398 return(AAC_MEM0_GETREG4(sc, AAC_SA_FWSTATUS));
2402 aac_rx_get_fwstatus(struct aac_softc *sc)
2404 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2406 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2407 AAC_RX_OMR0 : AAC_RX_FWSTATUS));
2411 aac_rkt_get_fwstatus(struct aac_softc *sc)
2413 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2415 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2416 AAC_RKT_OMR0 : AAC_RKT_FWSTATUS));
2420 * Notify the controller of a change in a given queue
2424 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2426 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2428 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2432 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2434 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2436 AAC_MEM0_SETREG4(sc, AAC_RX_IDBR, qbit);
2440 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2442 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2444 AAC_MEM0_SETREG4(sc, AAC_RKT_IDBR, qbit);
2448 * Get the interrupt reason bits
2451 aac_sa_get_istatus(struct aac_softc *sc)
2453 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2455 return(AAC_MEM0_GETREG2(sc, AAC_SA_DOORBELL0));
2459 aac_rx_get_istatus(struct aac_softc *sc)
2461 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2463 return(AAC_MEM0_GETREG4(sc, AAC_RX_ODBR));
2467 aac_rkt_get_istatus(struct aac_softc *sc)
2469 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2471 return(AAC_MEM0_GETREG4(sc, AAC_RKT_ODBR));
2475 * Clear some interrupt reason bits
2478 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2480 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2482 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2486 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2488 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2490 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, mask);
2494 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2496 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2498 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, mask);
2502 * Populate the mailbox and set the command word
2505 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2506 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2508 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2510 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX, command);
2511 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2512 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2513 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2514 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2518 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2519 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2521 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2523 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX, command);
2524 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2525 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2526 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2527 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2531 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2532 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2534 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2536 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX, command);
2537 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2538 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2539 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2540 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2544 * Fetch the immediate command status word
2547 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2549 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2551 return(AAC_MEM1_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2555 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2557 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2559 return(AAC_MEM1_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2563 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2565 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2567 return(AAC_MEM1_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2571 * Set/clear interrupt masks
2574 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2576 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2579 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2581 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2586 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2588 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2591 if (sc->flags & AAC_FLAGS_NEW_COMM)
2592 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2594 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2596 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~0);
2601 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2603 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2606 if (sc->flags & AAC_FLAGS_NEW_COMM)
2607 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2609 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2611 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~0);
2616 * New comm. interface: Send command functions
2619 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2621 u_int32_t index, device;
2623 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2625 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2626 if (index == 0xffffffffL)
2627 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2628 if (index == 0xffffffffL)
2630 aac_enqueue_busy(cm);
2632 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2634 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2636 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2637 AAC_MEM0_SETREG4(sc, AAC_RX_IQUE, index);
2642 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2644 u_int32_t index, device;
2646 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2648 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2649 if (index == 0xffffffffL)
2650 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2651 if (index == 0xffffffffL)
2653 aac_enqueue_busy(cm);
2655 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2657 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2659 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2660 AAC_MEM0_SETREG4(sc, AAC_RKT_IQUE, index);
2665 * New comm. interface: get, set outbound queue index
2668 aac_rx_get_outb_queue(struct aac_softc *sc)
2670 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2672 return(AAC_MEM0_GETREG4(sc, AAC_RX_OQUE));
2676 aac_rkt_get_outb_queue(struct aac_softc *sc)
2678 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2680 return(AAC_MEM0_GETREG4(sc, AAC_RKT_OQUE));
2684 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2686 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2688 AAC_MEM0_SETREG4(sc, AAC_RX_OQUE, index);
2692 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2694 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2696 AAC_MEM0_SETREG4(sc, AAC_RKT_OQUE, index);
2700 * Debugging and Diagnostics
2704 * Print some information about the controller.
2707 aac_describe_controller(struct aac_softc *sc)
2709 struct aac_fib *fib;
2710 struct aac_adapter_info *info;
2711 char *adapter_type = "Adaptec RAID controller";
2713 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2715 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
2716 aac_alloc_sync_fib(sc, &fib);
2719 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2720 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2721 aac_release_sync_fib(sc);
2722 lockmgr(&sc->aac_io_lock, LK_RELEASE);
2726 /* save the kernel revision structure for later use */
2727 info = (struct aac_adapter_info *)&fib->data[0];
2728 sc->aac_revision = info->KernelRevision;
2731 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2732 "(%dMB cache, %dMB execution), %s\n",
2733 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2734 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2735 info->BufferMem / (1024 * 1024),
2736 info->ExecutionMem / (1024 * 1024),
2737 aac_describe_code(aac_battery_platform,
2738 info->batteryPlatform));
2740 device_printf(sc->aac_dev,
2741 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2742 info->KernelRevision.external.comp.major,
2743 info->KernelRevision.external.comp.minor,
2744 info->KernelRevision.external.comp.dash,
2745 info->KernelRevision.buildNumber,
2746 (u_int32_t)(info->SerialNumber & 0xffffff));
2748 device_printf(sc->aac_dev, "Supported Options=%b\n",
2749 sc->supported_options,
2772 if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2774 if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2775 device_printf(sc->aac_dev,
2776 "RequestSupplementAdapterInfo failed\n");
2778 adapter_type = ((struct aac_supplement_adapter_info *)
2779 &fib->data[0])->AdapterTypeText;
2781 device_printf(sc->aac_dev, "%s, aac driver %d.%d.%d-%d\n",
2783 AAC_DRIVER_MAJOR_VERSION, AAC_DRIVER_MINOR_VERSION,
2784 AAC_DRIVER_BUGFIX_LEVEL, AAC_DRIVER_BUILD);
2786 aac_release_sync_fib(sc);
2787 lockmgr(&sc->aac_io_lock, LK_RELEASE);
2791 * Look up a text description of a numeric error code and return a pointer to
2795 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2799 for (i = 0; table[i].string != NULL; i++)
2800 if (table[i].code == code)
2801 return(table[i].string);
2802 return(table[i + 1].string);
2806 * Management Interface
2810 aac_open(struct dev_open_args *ap)
2812 cdev_t dev = ap->a_head.a_dev;
2813 struct aac_softc *sc;
2816 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2817 device_busy(sc->aac_dev);
2823 aac_ioctl(struct dev_ioctl_args *ap)
2825 caddr_t arg = ap->a_data;
2826 cdev_t dev = ap->a_head.a_dev;
2827 u_long cmd = ap->a_cmd;
2828 union aac_statrequest *as;
2829 struct aac_softc *sc;
2832 as = (union aac_statrequest *)arg;
2834 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2838 switch (as->as_item) {
2843 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2844 sizeof(struct aac_qstat));
2852 case FSACTL_SENDFIB:
2853 case FSACTL_SEND_LARGE_FIB:
2854 arg = *(caddr_t*)arg;
2855 case FSACTL_LNX_SENDFIB:
2856 case FSACTL_LNX_SEND_LARGE_FIB:
2857 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2858 error = aac_ioctl_sendfib(sc, arg);
2860 case FSACTL_SEND_RAW_SRB:
2861 arg = *(caddr_t*)arg;
2862 case FSACTL_LNX_SEND_RAW_SRB:
2863 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2864 error = aac_ioctl_send_raw_srb(sc, arg);
2866 case FSACTL_AIF_THREAD:
2867 case FSACTL_LNX_AIF_THREAD:
2868 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2871 case FSACTL_OPEN_GET_ADAPTER_FIB:
2872 arg = *(caddr_t*)arg;
2873 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2874 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2875 error = aac_open_aif(sc, arg);
2877 case FSACTL_GET_NEXT_ADAPTER_FIB:
2878 arg = *(caddr_t*)arg;
2879 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2880 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2881 error = aac_getnext_aif(sc, arg);
2883 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2884 arg = *(caddr_t*)arg;
2885 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2886 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2887 error = aac_close_aif(sc, arg);
2889 case FSACTL_MINIPORT_REV_CHECK:
2890 arg = *(caddr_t*)arg;
2891 case FSACTL_LNX_MINIPORT_REV_CHECK:
2892 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
2893 error = aac_rev_check(sc, arg);
2895 case FSACTL_QUERY_DISK:
2896 arg = *(caddr_t*)arg;
2897 case FSACTL_LNX_QUERY_DISK:
2898 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
2899 error = aac_query_disk(sc, arg);
2901 case FSACTL_DELETE_DISK:
2902 case FSACTL_LNX_DELETE_DISK:
2904 * We don't trust the underland to tell us when to delete a
2905 * container, rather we rely on an AIF coming from the
2910 case FSACTL_GET_PCI_INFO:
2911 arg = *(caddr_t*)arg;
2912 case FSACTL_LNX_GET_PCI_INFO:
2913 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
2914 error = aac_get_pci_info(sc, arg);
2916 case FSACTL_GET_FEATURES:
2917 arg = *(caddr_t*)arg;
2918 case FSACTL_LNX_GET_FEATURES:
2919 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
2920 error = aac_supported_features(sc, arg);
2923 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
2930 static struct filterops aac_filterops =
2931 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, aac_filter_detach, aac_filter_read };
2934 aac_kqfilter(struct dev_kqfilter_args *ap)
2936 cdev_t dev = ap->a_head.a_dev;
2937 struct aac_softc *sc = dev->si_drv1;
2938 struct knote *kn = ap->a_kn;
2939 struct klist *klist;
2943 switch (kn->kn_filter) {
2945 kn->kn_fop = &aac_filterops;
2946 kn->kn_hook = (caddr_t)sc;
2949 ap->a_result = EOPNOTSUPP;
2953 klist = &sc->rcv_kq.ki_note;
2954 knote_insert(klist, kn);
2960 aac_filter_detach(struct knote *kn)
2962 struct aac_softc *sc = (struct aac_softc *)kn->kn_hook;
2963 struct klist *klist;
2965 klist = &sc->rcv_kq.ki_note;
2966 knote_remove(klist, kn);
2970 aac_filter_read(struct knote *kn, long hint)
2972 struct aac_softc *sc;
2973 struct aac_fib_context *ctx;
2975 sc = (struct aac_softc *)kn->kn_hook;
2977 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
2978 for (ctx = sc->fibctx; ctx; ctx = ctx->next)
2979 if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap)
2981 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
2987 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
2990 switch (event->ev_type) {
2991 case AAC_EVENT_CMFREE:
2992 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2993 if (aac_alloc_command(sc, (struct aac_command **)arg)) {
2994 aac_add_event(sc, event);
2997 kfree(event, M_AACBUF);
3006 * Send a FIB supplied from userspace
3009 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3011 struct aac_command *cm;
3014 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3021 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3022 if (aac_alloc_command(sc, &cm)) {
3023 struct aac_event *event;
3025 event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3026 M_INTWAIT | M_ZERO);
3027 event->ev_type = AAC_EVENT_CMFREE;
3028 event->ev_callback = aac_ioctl_event;
3029 event->ev_arg = &cm;
3030 aac_add_event(sc, event);
3031 lksleep(&cm, &sc->aac_io_lock, 0, "sendfib", 0);
3033 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3036 * Fetch the FIB header, then re-copy to get data as well.
3038 if ((error = copyin(ufib, cm->cm_fib,
3039 sizeof(struct aac_fib_header))) != 0)
3041 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3042 if (size > sc->aac_max_fib_size) {
3043 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
3044 size, sc->aac_max_fib_size);
3045 size = sc->aac_max_fib_size;
3047 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3049 cm->cm_fib->Header.Size = size;
3050 cm->cm_timestamp = time_second;
3053 * Pass the FIB to the controller, wait for it to complete.
3055 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3056 error = aac_wait_command(cm);
3057 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3059 device_printf(sc->aac_dev,
3060 "aac_wait_command return %d\n", error);
3065 * Copy the FIB and data back out to the caller.
3067 size = cm->cm_fib->Header.Size;
3068 if (size > sc->aac_max_fib_size) {
3069 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
3070 size, sc->aac_max_fib_size);
3071 size = sc->aac_max_fib_size;
3073 error = copyout(cm->cm_fib, ufib, size);
3077 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3078 aac_release_command(cm);
3079 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3085 * Send a passthrough FIB supplied from userspace
3088 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
3090 struct aac_command *cm;
3091 struct aac_event *event;
3092 struct aac_fib *fib;
3093 struct aac_srb *srbcmd, *user_srb;
3094 struct aac_sg_entry *sge;
3095 struct aac_sg_entry64 *sge64;
3096 void *srb_sg_address, *ureply;
3097 uint32_t fibsize, srb_sg_bytecount;
3098 int error, transfer_data;
3100 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3105 user_srb = (struct aac_srb *)arg;
3107 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3108 if (aac_alloc_command(sc, &cm)) {
3109 event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3111 if (event == NULL) {
3113 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3116 event->ev_type = AAC_EVENT_CMFREE;
3117 event->ev_callback = aac_ioctl_event;
3118 event->ev_arg = &cm;
3119 aac_add_event(sc, event);
3120 lksleep(cm, &sc->aac_io_lock, 0, "aacraw", 0);
3122 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3126 srbcmd = (struct aac_srb *)fib->data;
3127 error = copyin(&user_srb->data_len, &fibsize, sizeof(uint32_t));
3130 if (fibsize > (sc->aac_max_fib_size - sizeof(struct aac_fib_header))) {
3134 error = copyin(user_srb, srbcmd, fibsize);
3137 srbcmd->function = 0;
3138 srbcmd->retry_limit = 0;
3139 if (srbcmd->sg_map.SgCount > 1) {
3144 /* Retrieve correct SG entries. */
3145 if (fibsize == (sizeof(struct aac_srb) +
3146 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry))) {
3147 sge = srbcmd->sg_map.SgEntry;
3149 srb_sg_bytecount = sge->SgByteCount;
3150 srb_sg_address = (void *)(uintptr_t)sge->SgAddress;
3153 else if (fibsize == (sizeof(struct aac_srb) +
3154 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry64))) {
3156 sge64 = (struct aac_sg_entry64 *)srbcmd->sg_map.SgEntry;
3157 srb_sg_bytecount = sge64->SgByteCount;
3158 srb_sg_address = (void *)sge64->SgAddress;
3159 if (sge64->SgAddress > 0xffffffffull &&
3160 (sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
3170 ureply = (char *)arg + fibsize;
3171 srbcmd->data_len = srb_sg_bytecount;
3172 if (srbcmd->sg_map.SgCount == 1)
3175 cm->cm_sgtable = (struct aac_sg_table *)&srbcmd->sg_map;
3176 if (transfer_data) {
3177 cm->cm_datalen = srb_sg_bytecount;
3178 cm->cm_data = kmalloc(cm->cm_datalen, M_AACBUF, M_NOWAIT);
3179 if (cm->cm_data == NULL) {
3183 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN)
3184 cm->cm_flags |= AAC_CMD_DATAIN;
3185 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_OUT) {
3186 cm->cm_flags |= AAC_CMD_DATAOUT;
3187 error = copyin(srb_sg_address, cm->cm_data,
3194 fib->Header.Size = sizeof(struct aac_fib_header) +
3195 sizeof(struct aac_srb);
3196 fib->Header.XferState =
3197 AAC_FIBSTATE_HOSTOWNED |
3198 AAC_FIBSTATE_INITIALISED |
3199 AAC_FIBSTATE_EMPTY |
3200 AAC_FIBSTATE_FROMHOST |
3201 AAC_FIBSTATE_REXPECTED |
3203 AAC_FIBSTATE_ASYNC |
3204 AAC_FIBSTATE_FAST_RESPONSE;
3205 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) != 0 ?
3206 ScsiPortCommandU64 : ScsiPortCommand;
3208 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3209 aac_wait_command(cm);
3210 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3212 if (transfer_data && (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN) != 0) {
3213 error = copyout(cm->cm_data, srb_sg_address, cm->cm_datalen);
3217 error = copyout(fib->data, ureply, sizeof(struct aac_srb_response));
3220 if (cm->cm_data != NULL)
3221 kfree(cm->cm_data, M_AACBUF);
3222 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3223 aac_release_command(cm);
3224 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3230 aac_close(struct dev_close_args *ap)
3232 cdev_t dev = ap->a_head.a_dev;
3233 struct aac_softc *sc;
3236 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3238 device_unbusy(sc->aac_dev);
3245 * Handle an AIF sent to us by the controller; queue it for later reference.
3246 * If the queue fills up, then drop the older entries.
3249 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3251 struct aac_aif_command *aif;
3252 struct aac_container *co, *co_next;
3253 struct aac_fib_context *ctx;
3254 struct aac_mntinforesp *mir;
3255 int next, current, found;
3256 int count = 0, added = 0, i = 0;
3259 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3261 aif = (struct aac_aif_command*)&fib->data[0];
3262 aac_print_aif(sc, aif);
3264 /* Is it an event that we should care about? */
3265 switch (aif->command) {
3266 case AifCmdEventNotify:
3267 switch (aif->data.EN.type) {
3268 case AifEnAddContainer:
3269 case AifEnDeleteContainer:
3271 * A container was added or deleted, but the message
3272 * doesn't tell us anything else! Re-enumerate the
3273 * containers and sort things out.
3275 aac_alloc_sync_fib(sc, &fib);
3278 * Ask the controller for its containers one at
3280 * XXX What if the controller's list changes
3281 * midway through this enumaration?
3282 * XXX This should be done async.
3284 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
3287 count = mir->MntRespCount;
3289 * Check the container against our list.
3290 * co->co_found was already set to 0 in a
3293 if ((mir->Status == ST_OK) &&
3294 (mir->MntTable[0].VolType != CT_NONE)) {
3297 &sc->aac_container_tqh,
3299 if (co->co_mntobj.ObjectId ==
3300 mir->MntTable[0].ObjectId) {
3307 * If the container matched, continue
3316 * This is a new container. Do all the
3317 * appropriate things to set it up.
3319 aac_add_container(sc, mir, 1);
3323 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3324 aac_release_sync_fib(sc);
3327 * Go through our list of containers and see which ones
3328 * were not marked 'found'. Since the controller didn't
3329 * list them they must have been deleted. Do the
3330 * appropriate steps to destroy the device. Also reset
3331 * the co->co_found field.
3333 co = TAILQ_FIRST(&sc->aac_container_tqh);
3334 while (co != NULL) {
3335 if (co->co_found == 0) {
3336 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3338 device_delete_child(sc->aac_dev,
3341 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3342 co_next = TAILQ_NEXT(co, co_link);
3343 lockmgr(&sc->aac_container_lock, LK_EXCLUSIVE);
3344 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3346 lockmgr(&sc->aac_container_lock, LK_RELEASE);
3347 kfree(co, M_AACBUF);
3351 co = TAILQ_NEXT(co, co_link);
3355 /* Attach the newly created containers */
3357 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3359 bus_generic_attach(sc->aac_dev);
3361 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3366 case AifEnEnclosureManagement:
3367 switch (aif->data.EN.data.EEE.eventType) {
3368 case AIF_EM_DRIVE_INSERTION:
3369 case AIF_EM_DRIVE_REMOVAL:
3370 channel = aif->data.EN.data.EEE.unitID;
3371 if (sc->cam_rescan_cb != NULL)
3372 sc->cam_rescan_cb(sc,
3373 (channel >> 24) & 0xF,
3374 (channel & 0xFFFF));
3380 case AifEnDeleteJBOD:
3381 channel = aif->data.EN.data.ECE.container;
3382 if (sc->cam_rescan_cb != NULL)
3383 sc->cam_rescan_cb(sc, (channel >> 24) & 0xF,
3384 AAC_CAM_TARGET_WILDCARD);
3395 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3396 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3397 current = sc->aifq_idx;
3398 next = (current + 1) % AAC_AIFQ_LENGTH;
3400 sc->aifq_filled = 1;
3401 bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3402 /* modify AIF contexts */
3403 if (sc->aifq_filled) {
3404 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3405 if (next == ctx->ctx_idx)
3407 else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3408 ctx->ctx_idx = next;
3411 sc->aifq_idx = next;
3412 /* On the off chance that someone is sleeping for an aif... */
3413 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3414 wakeup(sc->aac_aifq);
3415 /* token may have been lost */
3416 /* Wakeup any poll()ers */
3417 KNOTE(&sc->rcv_kq.ki_note, 0);
3418 /* token may have been lost */
3419 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3425 * Return the Revision of the driver to userspace and check to see if the
3426 * userspace app is possibly compatible. This is extremely bogus since
3427 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3428 * returning what the card reported.
3431 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3433 struct aac_rev_check rev_check;
3434 struct aac_rev_check_resp rev_check_resp;
3437 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3440 * Copyin the revision struct from userspace
3442 if ((error = copyin(udata, (caddr_t)&rev_check,
3443 sizeof(struct aac_rev_check))) != 0) {
3447 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3448 rev_check.callingRevision.buildNumber);
3451 * Doctor up the response struct.
3453 rev_check_resp.possiblyCompatible = 1;
3454 rev_check_resp.adapterSWRevision.external.comp.major =
3455 AAC_DRIVER_MAJOR_VERSION;
3456 rev_check_resp.adapterSWRevision.external.comp.minor =
3457 AAC_DRIVER_MINOR_VERSION;
3458 rev_check_resp.adapterSWRevision.external.comp.type =
3460 rev_check_resp.adapterSWRevision.external.comp.dash =
3461 AAC_DRIVER_BUGFIX_LEVEL;
3462 rev_check_resp.adapterSWRevision.buildNumber =
3465 return(copyout((caddr_t)&rev_check_resp, udata,
3466 sizeof(struct aac_rev_check_resp)));
3470 * Pass the fib context to the caller
3473 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3475 struct aac_fib_context *fibctx, *ctx;
3478 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3480 fibctx = kmalloc(sizeof(struct aac_fib_context), M_AACBUF, M_NOWAIT|M_ZERO);
3484 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3485 /* all elements are already 0, add to queue */
3486 if (sc->fibctx == NULL)
3487 sc->fibctx = fibctx;
3489 for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3495 /* evaluate unique value */
3496 fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3498 while (ctx != fibctx) {
3499 if (ctx->unique == fibctx->unique) {
3506 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3508 error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3510 aac_close_aif(sc, (caddr_t)ctx);
3515 * Close the caller's fib context
3518 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3520 struct aac_fib_context *ctx;
3522 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3524 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3525 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3526 if (ctx->unique == *(uint32_t *)&arg) {
3527 if (ctx == sc->fibctx)
3530 ctx->prev->next = ctx->next;
3532 ctx->next->prev = ctx->prev;
3537 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3539 kfree(ctx, M_AACBUF);
3545 * Pass the caller the next AIF in their queue
3548 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3550 struct get_adapter_fib_ioctl agf;
3551 struct aac_fib_context *ctx;
3554 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3556 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3557 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3558 if (agf.AdapterFibContext == ctx->unique)
3564 error = aac_return_aif(sc, ctx, agf.AifFib);
3565 if (error == EAGAIN && agf.Wait) {
3566 fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3567 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3568 while (error == EAGAIN) {
3569 error = tsleep(sc->aac_aifq,
3570 PCATCH, "aacaif", 0);
3572 error = aac_return_aif(sc, ctx, agf.AifFib);
3574 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3581 * Hand the next AIF off the top of the queue out to userspace.
3584 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3588 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3590 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3591 current = ctx->ctx_idx;
3592 if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3594 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3598 copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3600 device_printf(sc->aac_dev,
3601 "aac_return_aif: copyout returned %d\n", error);
3604 ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3606 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3611 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3613 struct aac_pci_info {
3619 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3621 pciinf.bus = pci_get_bus(sc->aac_dev);
3622 pciinf.slot = pci_get_slot(sc->aac_dev);
3624 error = copyout((caddr_t)&pciinf, uptr,
3625 sizeof(struct aac_pci_info));
3631 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3633 struct aac_features f;
3636 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3638 if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3642 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3643 * ALL zero in the featuresState, the driver will return the current
3644 * state of all the supported features, the data field will not be
3646 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3647 * a specific bit set in the featuresState, the driver will return the
3648 * current state of this specific feature and whatever data that are
3649 * associated with the feature in the data field or perform whatever
3650 * action needed indicates in the data field.
3652 if (f.feat.fValue == 0) {
3653 f.feat.fBits.largeLBA =
3654 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3655 /* TODO: In the future, add other features state here as well */
3657 if (f.feat.fBits.largeLBA)
3658 f.feat.fBits.largeLBA =
3659 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3660 /* TODO: Add other features state and data in the future */
3663 error = copyout(&f, uptr, sizeof (f));
3668 * Give the userland some information about the container. The AAC arch
3669 * expects the driver to be a SCSI passthrough type driver, so it expects
3670 * the containers to have b:t:l numbers. Fake it.
3673 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3675 struct aac_query_disk query_disk;
3676 struct aac_container *co;
3677 struct aac_disk *disk;
3680 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3684 error = copyin(uptr, (caddr_t)&query_disk,
3685 sizeof(struct aac_query_disk));
3689 id = query_disk.ContainerNumber;
3693 lockmgr(&sc->aac_container_lock, LK_EXCLUSIVE);
3694 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3695 if (co->co_mntobj.ObjectId == id)
3700 query_disk.Valid = 0;
3701 query_disk.Locked = 0;
3702 query_disk.Deleted = 1; /* XXX is this right? */
3704 disk = device_get_softc(co->co_disk);
3705 query_disk.Valid = 1;
3707 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3708 query_disk.Deleted = 0;
3709 query_disk.Bus = device_get_unit(sc->aac_dev);
3710 query_disk.Target = disk->unit;
3712 query_disk.UnMapped = 0;
3713 bcopy(disk->ad_dev_t->si_name,
3714 &query_disk.diskDeviceName[0], 10);
3716 lockmgr(&sc->aac_container_lock, LK_RELEASE);
3718 error = copyout((caddr_t)&query_disk, uptr,
3719 sizeof(struct aac_query_disk));
3725 aac_get_bus_info(struct aac_softc *sc)
3727 struct aac_fib *fib;
3728 struct aac_ctcfg *c_cmd;
3729 struct aac_ctcfg_resp *c_resp;
3730 struct aac_vmioctl *vmi;
3731 struct aac_vmi_businf_resp *vmi_resp;
3732 struct aac_getbusinf businfo;
3733 struct aac_sim *caminf;
3735 int i, found, error;
3737 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3738 aac_alloc_sync_fib(sc, &fib);
3739 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3740 bzero(c_cmd, sizeof(struct aac_ctcfg));
3742 c_cmd->Command = VM_ContainerConfig;
3743 c_cmd->cmd = CT_GET_SCSI_METHOD;
3746 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3747 sizeof(struct aac_ctcfg));
3749 device_printf(sc->aac_dev, "Error %d sending "
3750 "VM_ContainerConfig command\n", error);
3751 aac_release_sync_fib(sc);
3752 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3756 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3757 if (c_resp->Status != ST_OK) {
3758 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3760 aac_release_sync_fib(sc);
3761 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3765 sc->scsi_method_id = c_resp->param;
3767 vmi = (struct aac_vmioctl *)&fib->data[0];
3768 bzero(vmi, sizeof(struct aac_vmioctl));
3770 vmi->Command = VM_Ioctl;
3771 vmi->ObjType = FT_DRIVE;
3772 vmi->MethId = sc->scsi_method_id;
3774 vmi->IoctlCmd = GetBusInfo;
3776 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3777 sizeof(struct aac_vmi_businf_resp));
3779 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3781 aac_release_sync_fib(sc);
3782 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3786 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3787 if (vmi_resp->Status != ST_OK) {
3788 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3790 aac_release_sync_fib(sc);
3791 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3795 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3796 aac_release_sync_fib(sc);
3797 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3800 for (i = 0; i < businfo.BusCount; i++) {
3801 if (businfo.BusValid[i] != AAC_BUS_VALID)
3804 caminf = (struct aac_sim *)kmalloc(sizeof(struct aac_sim),
3805 M_AACBUF, M_INTWAIT | M_ZERO);
3807 child = device_add_child(sc->aac_dev, "aacp", -1);
3808 if (child == NULL) {
3809 device_printf(sc->aac_dev,
3810 "device_add_child failed for passthrough bus %d\n",
3812 kfree(caminf, M_AACBUF);
3816 caminf->TargetsPerBus = businfo.TargetsPerBus;
3817 caminf->BusNumber = i;
3818 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3819 caminf->aac_sc = sc;
3820 caminf->sim_dev = child;
3822 device_set_ivars(child, caminf);
3823 device_set_desc(child, "SCSI Passthrough Bus");
3824 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3830 bus_generic_attach(sc->aac_dev);