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 callout_stop_sync(&sc->aac_daemontime);
680 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
681 while (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
682 sc->aifflags |= AAC_AIFFLAGS_EXIT;
683 wakeup(sc->aifthread);
684 lksleep(sc->aac_dev, &sc->aac_io_lock, 0, "aacdch", 0);
686 lockmgr(&sc->aac_io_lock, LK_RELEASE);
687 KASSERT((sc->aifflags & AAC_AIFFLAGS_RUNNING) == 0,
688 ("%s: invalid detach state", __func__));
690 /* Remove the child containers */
691 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
692 error = device_delete_child(dev, co->co_disk);
695 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
699 /* Remove the CAM SIMs */
700 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
701 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
702 error = device_delete_child(dev, sim->sim_dev);
705 kfree(sim, M_AACBUF);
708 if ((error = aac_shutdown(dev)))
711 EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
715 lockuninit(&sc->aac_aifq_lock);
716 lockuninit(&sc->aac_io_lock);
717 lockuninit(&sc->aac_container_lock);
723 * Bring the controller down to a dormant state and detach all child devices.
725 * This function is called before detach or system shutdown.
727 * Note that we can assume that the bioq on the controller is empty, as we won't
728 * allow shutdown if any device is open.
731 aac_shutdown(device_t dev)
733 struct aac_softc *sc;
735 struct aac_close_command *cc;
737 sc = device_get_softc(dev);
738 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
740 sc->aac_state |= AAC_STATE_SUSPEND;
743 * Send a Container shutdown followed by a HostShutdown FIB to the
744 * controller to convince it that we don't want to talk to it anymore.
745 * We've been closed and all I/O completed already
747 device_printf(sc->aac_dev, "shutting down controller...");
749 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
750 aac_alloc_sync_fib(sc, &fib);
751 cc = (struct aac_close_command *)&fib->data[0];
753 bzero(cc, sizeof(struct aac_close_command));
754 cc->Command = VM_CloseAll;
755 cc->ContainerId = 0xffffffff;
756 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
757 sizeof(struct aac_close_command)))
758 kprintf("FAILED.\n");
765 * XXX Issuing this command to the controller makes it shut down
766 * but also keeps it from coming back up without a reset of the
767 * PCI bus. This is not desirable if you are just unloading the
768 * driver module with the intent to reload it later.
770 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
772 kprintf("FAILED.\n");
779 AAC_MASK_INTERRUPTS(sc);
780 aac_release_sync_fib(sc);
781 lockmgr(&sc->aac_io_lock, LK_RELEASE);
787 * Bring the controller to a quiescent state, ready for system suspend.
790 aac_suspend(device_t dev)
792 struct aac_softc *sc;
794 sc = device_get_softc(dev);
796 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
797 sc->aac_state |= AAC_STATE_SUSPEND;
799 AAC_MASK_INTERRUPTS(sc);
804 * Bring the controller back to a state ready for operation.
807 aac_resume(device_t dev)
809 struct aac_softc *sc;
811 sc = device_get_softc(dev);
813 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
814 sc->aac_state &= ~AAC_STATE_SUSPEND;
815 AAC_UNMASK_INTERRUPTS(sc);
820 * Interrupt handler for NEW_COMM interface.
823 aac_new_intr(void *arg)
825 struct aac_softc *sc;
826 u_int32_t index, fast;
827 struct aac_command *cm;
831 sc = (struct aac_softc *)arg;
833 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
834 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
836 index = AAC_GET_OUTB_QUEUE(sc);
837 if (index == 0xffffffff)
838 index = AAC_GET_OUTB_QUEUE(sc);
839 if (index == 0xffffffff)
842 if (index == 0xfffffffe) {
843 /* XXX This means that the controller wants
844 * more work. Ignore it for now.
849 fib = (struct aac_fib *)kmalloc(sizeof *fib, M_AACBUF,
852 for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
853 ((u_int32_t *)fib)[i] = AAC_MEM1_GETREG4(sc, index + i*4);
854 aac_handle_aif(sc, fib);
855 kfree(fib, M_AACBUF);
858 * AIF memory is owned by the adapter, so let it
859 * know that we are done with it.
861 AAC_SET_OUTB_QUEUE(sc, index);
862 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
865 cm = sc->aac_commands + (index >> 2);
868 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
869 *((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
872 aac_unmap_command(cm);
873 cm->cm_flags |= AAC_CMD_COMPLETED;
875 /* is there a completion handler? */
876 if (cm->cm_complete != NULL) {
879 /* assume that someone is sleeping on this
884 sc->flags &= ~AAC_QUEUE_FRZN;
887 /* see if we can start some more I/O */
888 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
891 lockmgr(&sc->aac_io_lock, LK_RELEASE);
895 * Interrupt filter for !NEW_COMM interface.
898 aac_filter(void *arg)
900 struct aac_softc *sc;
903 sc = (struct aac_softc *)arg;
905 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
907 * Read the status register directly. This is faster than taking the
908 * driver lock and reading the queues directly. It also saves having
909 * to turn parts of the driver lock into a spin mutex, which would be
912 reason = AAC_GET_ISTATUS(sc);
913 AAC_CLEAR_ISTATUS(sc, reason);
915 /* handle completion processing */
916 if (reason & AAC_DB_RESPONSE_READY)
917 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
919 /* controller wants to talk to us */
920 if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
922 * XXX Make sure that we don't get fooled by strange messages
923 * that start with a NULL.
925 if ((reason & AAC_DB_PRINTF) &&
926 (sc->aac_common->ac_printf[0] == 0))
927 sc->aac_common->ac_printf[0] = 32;
930 * This might miss doing the actual wakeup. However, the
931 * lksleep that this is waking up has a timeout, so it will
932 * wake up eventually. AIFs and printfs are low enough
933 * priority that they can handle hanging out for a few seconds
936 wakeup(sc->aifthread);
945 * Start as much queued I/O as possible on the controller
948 aac_startio(struct aac_softc *sc)
950 struct aac_command *cm;
953 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
957 * This flag might be set if the card is out of resources.
958 * Checking it here prevents an infinite loop of deferrals.
960 if (sc->flags & AAC_QUEUE_FRZN)
964 * Try to get a command that's been put off for lack of
967 cm = aac_dequeue_ready(sc);
970 * Try to build a command off the bio queue (ignore error
974 aac_bio_command(sc, &cm);
980 /* don't map more than once */
981 if (cm->cm_flags & AAC_CMD_MAPPED)
982 panic("aac: command %p already mapped", cm);
985 * Set up the command to go to the controller. If there are no
986 * data buffers associated with the command then it can bypass
989 if (cm->cm_datalen != 0) {
990 error = bus_dmamap_load(sc->aac_buffer_dmat,
991 cm->cm_datamap, cm->cm_data,
993 aac_map_command_sg, cm, 0);
994 if (error == EINPROGRESS) {
995 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "freezing queue\n");
996 sc->flags |= AAC_QUEUE_FRZN;
998 } else if (error != 0)
999 panic("aac_startio: unexpected error %d from "
1002 aac_map_command_sg(cm, NULL, 0, 0);
1007 * Handle notification of one or more FIBs coming from the controller.
1010 aac_command_thread(void *arg)
1012 struct aac_softc *sc = arg;
1013 struct aac_fib *fib;
1017 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1019 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1020 sc->aifflags = AAC_AIFFLAGS_RUNNING;
1022 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1025 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1026 retval = lksleep(sc->aifthread, &sc->aac_io_lock, 0,
1027 "aifthd", AAC_PERIODIC_INTERVAL * hz);
1030 * First see if any FIBs need to be allocated. This needs
1031 * to be called without the driver lock because contigmalloc
1034 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1035 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1036 aac_alloc_commands(sc);
1037 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1038 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1043 * While we're here, check to see if any commands are stuck.
1044 * This is pretty low-priority, so it's ok if it doesn't
1047 if (retval == EWOULDBLOCK)
1050 /* Check the hardware printf message buffer */
1051 if (sc->aac_common->ac_printf[0] != 0)
1052 aac_print_printf(sc);
1054 /* Also check to see if the adapter has a command for us. */
1055 if (sc->flags & AAC_FLAGS_NEW_COMM)
1058 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
1062 AAC_PRINT_FIB(sc, fib);
1064 switch (fib->Header.Command) {
1066 aac_handle_aif(sc, fib);
1069 device_printf(sc->aac_dev, "unknown command "
1070 "from controller\n");
1074 if ((fib->Header.XferState == 0) ||
1075 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
1079 /* Return the AIF to the controller. */
1080 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
1081 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
1082 *(AAC_FSAStatus*)fib->data = ST_OK;
1084 /* XXX Compute the Size field? */
1085 size = fib->Header.Size;
1086 if (size > sizeof(struct aac_fib)) {
1087 size = sizeof(struct aac_fib);
1088 fib->Header.Size = size;
1091 * Since we did not generate this command, it
1092 * cannot go through the normal
1093 * enqueue->startio chain.
1095 aac_enqueue_response(sc,
1096 AAC_ADAP_NORM_RESP_QUEUE,
1101 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1102 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1103 wakeup(sc->aac_dev);
1107 * Process completed commands.
1110 aac_complete(void *context, int pending)
1112 struct aac_softc *sc;
1113 struct aac_command *cm;
1114 struct aac_fib *fib;
1117 sc = (struct aac_softc *)context;
1118 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1120 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1122 /* pull completed commands off the queue */
1124 /* look for completed FIBs on our queue */
1125 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1127 break; /* nothing to do */
1129 /* get the command, unmap and hand off for processing */
1130 cm = sc->aac_commands + fib->Header.SenderData;
1132 AAC_PRINT_FIB(sc, fib);
1135 if ((cm->cm_flags & AAC_CMD_TIMEDOUT) != 0)
1136 device_printf(sc->aac_dev,
1137 "COMMAND %p COMPLETED AFTER %d SECONDS\n",
1138 cm, (int)(time_uptime - cm->cm_timestamp));
1140 aac_remove_busy(cm);
1142 aac_unmap_command(cm);
1143 cm->cm_flags |= AAC_CMD_COMPLETED;
1145 /* is there a completion handler? */
1146 if (cm->cm_complete != NULL) {
1147 cm->cm_complete(cm);
1149 /* assume that someone is sleeping on this command */
1154 /* see if we can start some more I/O */
1155 sc->flags &= ~AAC_QUEUE_FRZN;
1158 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1162 * Handle a bio submitted from a disk device.
1165 aac_submit_bio(struct aac_disk *ad, struct bio *bio)
1167 struct aac_softc *sc;
1169 bio->bio_driver_info = ad;
1170 sc = ad->ad_controller;
1171 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1173 /* queue the BIO and try to get some work done */
1174 aac_enqueue_bio(sc, bio);
1179 * Get a bio and build a command to go with it.
1182 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1184 struct aac_command *cm;
1185 struct aac_fib *fib;
1186 struct aac_disk *ad;
1190 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1192 /* get the resources we will need */
1195 if (aac_alloc_command(sc, &cm)) /* get a command */
1197 if ((bio = aac_dequeue_bio(sc)) == NULL)
1200 /* fill out the command */
1202 cm->cm_data = (void *)bp->b_data;
1203 cm->cm_datalen = bp->b_bcount;
1204 cm->cm_complete = aac_bio_complete;
1205 cm->cm_private = bio;
1206 cm->cm_timestamp = time_uptime;
1210 fib->Header.Size = sizeof(struct aac_fib_header);
1211 fib->Header.XferState =
1212 AAC_FIBSTATE_HOSTOWNED |
1213 AAC_FIBSTATE_INITIALISED |
1214 AAC_FIBSTATE_EMPTY |
1215 AAC_FIBSTATE_FROMHOST |
1216 AAC_FIBSTATE_REXPECTED |
1218 AAC_FIBSTATE_ASYNC |
1219 AAC_FIBSTATE_FAST_RESPONSE;
1221 /* build the read/write request */
1222 ad = (struct aac_disk *)bio->bio_driver_info;
1224 if (sc->flags & AAC_FLAGS_RAW_IO) {
1225 struct aac_raw_io *raw;
1226 raw = (struct aac_raw_io *)&fib->data[0];
1227 fib->Header.Command = RawIo;
1228 raw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1229 raw->ByteCount = bp->b_bcount;
1230 raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1232 raw->BpComplete = 0;
1233 fib->Header.Size += sizeof(struct aac_raw_io);
1234 cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1235 if (bp->b_cmd == BUF_CMD_READ) {
1237 cm->cm_flags |= AAC_CMD_DATAIN;
1240 cm->cm_flags |= AAC_CMD_DATAOUT;
1242 } else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1243 fib->Header.Command = ContainerCommand;
1244 if (bp->b_cmd == BUF_CMD_READ) {
1245 struct aac_blockread *br;
1246 br = (struct aac_blockread *)&fib->data[0];
1247 br->Command = VM_CtBlockRead;
1248 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1249 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1250 br->ByteCount = bp->b_bcount;
1251 fib->Header.Size += sizeof(struct aac_blockread);
1252 cm->cm_sgtable = &br->SgMap;
1253 cm->cm_flags |= AAC_CMD_DATAIN;
1255 struct aac_blockwrite *bw;
1256 bw = (struct aac_blockwrite *)&fib->data[0];
1257 bw->Command = VM_CtBlockWrite;
1258 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1259 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1260 bw->ByteCount = bp->b_bcount;
1261 bw->Stable = CUNSTABLE;
1262 fib->Header.Size += sizeof(struct aac_blockwrite);
1263 cm->cm_flags |= AAC_CMD_DATAOUT;
1264 cm->cm_sgtable = &bw->SgMap;
1267 fib->Header.Command = ContainerCommand64;
1268 if (bp->b_cmd == BUF_CMD_READ) {
1269 struct aac_blockread64 *br;
1270 br = (struct aac_blockread64 *)&fib->data[0];
1271 br->Command = VM_CtHostRead64;
1272 br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1273 br->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1274 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1277 fib->Header.Size += sizeof(struct aac_blockread64);
1278 cm->cm_flags |= AAC_CMD_DATAIN;
1279 cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1281 struct aac_blockwrite64 *bw;
1282 bw = (struct aac_blockwrite64 *)&fib->data[0];
1283 bw->Command = VM_CtHostWrite64;
1284 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1285 bw->SectorCount = bp->b_bcount / AAC_BLOCK_SIZE;
1286 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE;
1289 fib->Header.Size += sizeof(struct aac_blockwrite64);
1290 cm->cm_flags |= AAC_CMD_DATAOUT;
1291 cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1300 aac_enqueue_bio(sc, bio);
1302 aac_release_command(cm);
1307 * Handle a bio-instigated command that has been completed.
1310 aac_bio_complete(struct aac_command *cm)
1312 struct aac_blockread_response *brr;
1313 struct aac_blockwrite_response *bwr;
1317 AAC_FSAStatus status;
1319 /* fetch relevant status and then release the command */
1320 bio = (struct bio *)cm->cm_private;
1322 if (bp->b_cmd == BUF_CMD_READ) {
1323 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1324 status = brr->Status;
1326 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1327 status = bwr->Status;
1329 aac_release_command(cm);
1331 /* fix up the bio based on status */
1332 if (status == ST_OK) {
1337 bp->b_flags |= B_ERROR;
1338 /* pass an error string out to the disk layer */
1339 code = aac_describe_code(aac_command_status_table, status);
1341 aac_biodone(bio, code);
1345 * Submit a command to the controller, return when it completes.
1346 * XXX This is very dangerous! If the card has gone out to lunch, we could
1347 * be stuck here forever. At the same time, signals are not caught
1348 * because there is a risk that a signal could wakeup the sleep before
1349 * the card has a chance to complete the command. Since there is no way
1350 * to cancel a command that is in progress, we can't protect against the
1351 * card completing a command late and spamming the command and data
1352 * memory. So, we are held hostage until the command completes.
1355 aac_wait_command(struct aac_command *cm)
1357 struct aac_softc *sc;
1361 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1363 /* Put the command on the ready queue and get things going */
1364 aac_enqueue_ready(cm);
1366 error = lksleep(cm, &sc->aac_io_lock, 0, "aacwait", 0);
1371 *Command Buffer Management
1375 * Allocate a command.
1378 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1380 struct aac_command *cm;
1382 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1384 if ((cm = aac_dequeue_free(sc)) == NULL) {
1385 if (sc->total_fibs < sc->aac_max_fibs) {
1386 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1387 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1388 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1389 wakeup(sc->aifthread);
1399 * Release a command back to the freelist.
1402 aac_release_command(struct aac_command *cm)
1404 struct aac_event *event;
1405 struct aac_softc *sc;
1408 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1410 /* (re)initialize the command/FIB */
1411 cm->cm_sgtable = NULL;
1413 cm->cm_complete = NULL;
1414 cm->cm_private = NULL;
1415 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1416 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1417 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1418 cm->cm_fib->Header.Flags = 0;
1419 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1422 * These are duplicated in aac_start to cover the case where an
1423 * intermediate stage may have destroyed them. They're left
1424 * initialized here for debugging purposes only.
1426 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1427 cm->cm_fib->Header.SenderData = 0;
1429 aac_enqueue_free(cm);
1431 if ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1432 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1433 event->ev_callback(sc, event, event->ev_arg);
1438 * Map helper for command/FIB allocation.
1441 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1445 fibphys = (uint64_t *)arg;
1447 *fibphys = segs[0].ds_addr;
1451 * Allocate and initialize commands/FIBs for this adapter.
1454 aac_alloc_commands(struct aac_softc *sc)
1456 struct aac_command *cm;
1457 struct aac_fibmap *fm;
1461 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1463 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1466 fm = kmalloc(sizeof(struct aac_fibmap), M_AACBUF, M_INTWAIT | M_ZERO);
1468 /* allocate the FIBs in DMAable memory and load them */
1469 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1470 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1471 device_printf(sc->aac_dev,
1472 "Not enough contiguous memory available.\n");
1473 kfree(fm, M_AACBUF);
1477 /* Ignore errors since this doesn't bounce */
1478 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1479 sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1480 aac_map_command_helper, &fibphys, 0);
1482 /* initialize constant fields in the command structure */
1483 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1484 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1485 cm = sc->aac_commands + sc->total_fibs;
1486 fm->aac_commands = cm;
1488 cm->cm_fib = (struct aac_fib *)
1489 ((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1490 cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1491 cm->cm_index = sc->total_fibs;
1493 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1494 &cm->cm_datamap)) != 0)
1496 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1497 aac_release_command(cm);
1499 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1503 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
1504 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1505 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1506 lockmgr(&sc->aac_io_lock, LK_RELEASE);
1510 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1511 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1512 kfree(fm, M_AACBUF);
1517 * Free FIBs owned by this adapter.
1520 aac_free_commands(struct aac_softc *sc)
1522 struct aac_fibmap *fm;
1523 struct aac_command *cm;
1526 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1528 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1530 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1532 * We check against total_fibs to handle partially
1535 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1536 cm = fm->aac_commands + i;
1537 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1539 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1540 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1541 kfree(fm, M_AACBUF);
1546 * Command-mapping helper function - populate this command's s/g table.
1549 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1551 struct aac_softc *sc;
1552 struct aac_command *cm;
1553 struct aac_fib *fib;
1556 cm = (struct aac_command *)arg;
1559 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1561 /* copy into the FIB */
1562 if (cm->cm_sgtable != NULL) {
1563 if (fib->Header.Command == RawIo) {
1564 struct aac_sg_tableraw *sg;
1565 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1567 for (i = 0; i < nseg; i++) {
1568 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1569 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1570 sg->SgEntryRaw[i].Next = 0;
1571 sg->SgEntryRaw[i].Prev = 0;
1572 sg->SgEntryRaw[i].Flags = 0;
1574 /* update the FIB size for the s/g count */
1575 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1576 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1577 struct aac_sg_table *sg;
1578 sg = cm->cm_sgtable;
1580 for (i = 0; i < nseg; i++) {
1581 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1582 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1584 /* update the FIB size for the s/g count */
1585 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1587 struct aac_sg_table64 *sg;
1588 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1590 for (i = 0; i < nseg; i++) {
1591 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1592 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1594 /* update the FIB size for the s/g count */
1595 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1599 /* Fix up the address values in the FIB. Use the command array index
1600 * instead of a pointer since these fields are only 32 bits. Shift
1601 * the SenderFibAddress over to make room for the fast response bit
1602 * and for the AIF bit
1604 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1605 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1607 /* save a pointer to the command for speedy reverse-lookup */
1608 cm->cm_fib->Header.SenderData = cm->cm_index;
1610 if (cm->cm_flags & AAC_CMD_DATAIN)
1611 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1612 BUS_DMASYNC_PREREAD);
1613 if (cm->cm_flags & AAC_CMD_DATAOUT)
1614 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1615 BUS_DMASYNC_PREWRITE);
1616 cm->cm_flags |= AAC_CMD_MAPPED;
1618 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1619 int count = 10000000L;
1620 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1622 aac_unmap_command(cm);
1623 sc->flags |= AAC_QUEUE_FRZN;
1624 aac_requeue_ready(cm);
1626 DELAY(5); /* wait 5 usec. */
1629 /* Put the FIB on the outbound queue */
1630 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1631 aac_unmap_command(cm);
1632 sc->flags |= AAC_QUEUE_FRZN;
1633 aac_requeue_ready(cm);
1641 * Unmap a command from controller-visible space.
1644 aac_unmap_command(struct aac_command *cm)
1646 struct aac_softc *sc;
1649 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1651 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1654 if (cm->cm_datalen != 0) {
1655 if (cm->cm_flags & AAC_CMD_DATAIN)
1656 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1657 BUS_DMASYNC_POSTREAD);
1658 if (cm->cm_flags & AAC_CMD_DATAOUT)
1659 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1660 BUS_DMASYNC_POSTWRITE);
1662 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1664 cm->cm_flags &= ~AAC_CMD_MAPPED;
1668 * Hardware Interface
1672 * Initialize the adapter.
1675 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1677 struct aac_softc *sc;
1679 sc = (struct aac_softc *)arg;
1680 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1682 sc->aac_common_busaddr = segs[0].ds_addr;
1686 aac_check_firmware(struct aac_softc *sc)
1688 u_int32_t code, major, minor, options = 0, atu_size = 0;
1692 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1694 * Wait for the adapter to come ready.
1698 code = AAC_GET_FWSTATUS(sc);
1699 if (code & AAC_SELF_TEST_FAILED) {
1700 device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1703 if (code & AAC_KERNEL_PANIC) {
1704 device_printf(sc->aac_dev,
1705 "FATAL: controller kernel panic");
1708 if (time_uptime > (then + AAC_BOOT_TIMEOUT)) {
1709 device_printf(sc->aac_dev,
1710 "FATAL: controller not coming ready, "
1711 "status %x\n", code);
1714 } while (!(code & AAC_UP_AND_RUNNING));
1717 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1718 * firmware version 1.x are not compatible with this driver.
1720 if (sc->flags & AAC_FLAGS_PERC2QC) {
1721 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1723 device_printf(sc->aac_dev,
1724 "Error reading firmware version\n");
1728 /* These numbers are stored as ASCII! */
1729 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1730 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1732 device_printf(sc->aac_dev,
1733 "Firmware version %d.%d is not supported.\n",
1740 * Retrieve the capabilities/supported options word so we know what
1741 * work-arounds to enable. Some firmware revs don't support this
1744 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1745 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1746 device_printf(sc->aac_dev,
1747 "RequestAdapterInfo failed\n");
1751 options = AAC_GET_MAILBOX(sc, 1);
1752 atu_size = AAC_GET_MAILBOX(sc, 2);
1753 sc->supported_options = options;
1755 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1756 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1757 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1758 if (options & AAC_SUPPORTED_NONDASD)
1759 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1760 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1761 && (sizeof(bus_addr_t) > 4)) {
1762 device_printf(sc->aac_dev,
1763 "Enabling 64-bit address support\n");
1764 sc->flags |= AAC_FLAGS_SG_64BIT;
1766 if ((options & AAC_SUPPORTED_NEW_COMM)
1767 && sc->aac_if.aif_send_command)
1768 sc->flags |= AAC_FLAGS_NEW_COMM;
1769 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1770 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1773 /* Check for broken hardware that does a lower number of commands */
1774 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1776 /* Remap mem. resource, if required */
1777 if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1778 atu_size > rman_get_size(sc->aac_regs_res1)) {
1779 bus_release_resource(
1780 sc->aac_dev, SYS_RES_MEMORY,
1781 sc->aac_regs_rid1, sc->aac_regs_res1);
1782 sc->aac_regs_res1 = bus_alloc_resource(
1783 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid1,
1784 0ul, ~0ul, atu_size, RF_ACTIVE);
1785 if (sc->aac_regs_res1 == NULL) {
1786 sc->aac_regs_res1 = bus_alloc_resource_any(
1787 sc->aac_dev, SYS_RES_MEMORY,
1788 &sc->aac_regs_rid1, RF_ACTIVE);
1789 if (sc->aac_regs_res1 == NULL) {
1790 device_printf(sc->aac_dev,
1791 "couldn't allocate register window\n");
1794 sc->flags &= ~AAC_FLAGS_NEW_COMM;
1796 sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
1797 sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
1799 if (sc->aac_hwif == AAC_HWIF_NARK) {
1800 sc->aac_regs_res0 = sc->aac_regs_res1;
1801 sc->aac_regs_rid0 = sc->aac_regs_rid1;
1802 sc->aac_btag0 = sc->aac_btag1;
1803 sc->aac_bhandle0 = sc->aac_bhandle1;
1807 /* Read preferred settings */
1808 sc->aac_max_fib_size = sizeof(struct aac_fib);
1809 sc->aac_max_sectors = 128; /* 64KB */
1810 if (sc->flags & AAC_FLAGS_SG_64BIT)
1811 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1812 - sizeof(struct aac_blockwrite64))
1813 / sizeof(struct aac_sg_entry64);
1815 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1816 - sizeof(struct aac_blockwrite))
1817 / sizeof(struct aac_sg_entry);
1819 if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1820 options = AAC_GET_MAILBOX(sc, 1);
1821 sc->aac_max_fib_size = (options & 0xFFFF);
1822 sc->aac_max_sectors = (options >> 16) << 1;
1823 options = AAC_GET_MAILBOX(sc, 2);
1824 sc->aac_sg_tablesize = (options >> 16);
1825 options = AAC_GET_MAILBOX(sc, 3);
1826 sc->aac_max_fibs = (options & 0xFFFF);
1828 if (sc->aac_max_fib_size > PAGE_SIZE)
1829 sc->aac_max_fib_size = PAGE_SIZE;
1830 sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1832 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1833 sc->flags |= AAC_FLAGS_RAW_IO;
1834 device_printf(sc->aac_dev, "Enable Raw I/O\n");
1836 if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1837 (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1838 sc->flags |= AAC_FLAGS_LBA_64BIT;
1839 device_printf(sc->aac_dev, "Enable 64-bit array\n");
1846 aac_init(struct aac_softc *sc)
1848 struct aac_adapter_init *ip;
1852 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1855 * Fill in the init structure. This tells the adapter about the
1856 * physical location of various important shared data structures.
1858 ip = &sc->aac_common->ac_init;
1859 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1860 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1861 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1862 sc->flags |= AAC_FLAGS_RAW_IO;
1864 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1866 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1867 offsetof(struct aac_common, ac_fibs);
1868 ip->AdapterFibsVirtualAddress = 0;
1869 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1870 ip->AdapterFibAlign = sizeof(struct aac_fib);
1872 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1873 offsetof(struct aac_common, ac_printf);
1874 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1877 * The adapter assumes that pages are 4K in size, except on some
1878 * broken firmware versions that do the page->byte conversion twice,
1879 * therefore 'assuming' that this value is in 16MB units (2^24).
1880 * Round up since the granularity is so high.
1882 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1883 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1884 ip->HostPhysMemPages =
1885 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1887 ip->HostElapsedSeconds = time_uptime; /* reset later if invalid */
1890 if (sc->flags & AAC_FLAGS_NEW_COMM) {
1891 ip->InitFlags |= AAC_INITFLAGS_NEW_COMM_SUPPORTED;
1892 device_printf(sc->aac_dev, "New comm. interface enabled\n");
1895 ip->MaxIoCommands = sc->aac_max_fibs;
1896 ip->MaxIoSize = sc->aac_max_sectors << 9;
1897 ip->MaxFibSize = sc->aac_max_fib_size;
1900 * Initialize FIB queues. Note that it appears that the layout of the
1901 * indexes and the segmentation of the entries may be mandated by the
1902 * adapter, which is only told about the base of the queue index fields.
1904 * The initial values of the indices are assumed to inform the adapter
1905 * of the sizes of the respective queues, and theoretically it could
1906 * work out the entire layout of the queue structures from this. We
1907 * take the easy route and just lay this area out like everyone else
1910 * The Linux driver uses a much more complex scheme whereby several
1911 * header records are kept for each queue. We use a couple of generic
1912 * list manipulation functions which 'know' the size of each list by
1913 * virtue of a table.
1915 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1916 qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1918 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1919 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1921 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1922 AAC_HOST_NORM_CMD_ENTRIES;
1923 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1924 AAC_HOST_NORM_CMD_ENTRIES;
1925 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1926 AAC_HOST_HIGH_CMD_ENTRIES;
1927 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1928 AAC_HOST_HIGH_CMD_ENTRIES;
1929 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1930 AAC_ADAP_NORM_CMD_ENTRIES;
1931 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1932 AAC_ADAP_NORM_CMD_ENTRIES;
1933 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1934 AAC_ADAP_HIGH_CMD_ENTRIES;
1935 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1936 AAC_ADAP_HIGH_CMD_ENTRIES;
1937 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1938 AAC_HOST_NORM_RESP_ENTRIES;
1939 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1940 AAC_HOST_NORM_RESP_ENTRIES;
1941 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1942 AAC_HOST_HIGH_RESP_ENTRIES;
1943 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1944 AAC_HOST_HIGH_RESP_ENTRIES;
1945 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1946 AAC_ADAP_NORM_RESP_ENTRIES;
1947 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1948 AAC_ADAP_NORM_RESP_ENTRIES;
1949 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1950 AAC_ADAP_HIGH_RESP_ENTRIES;
1951 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1952 AAC_ADAP_HIGH_RESP_ENTRIES;
1953 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1954 &sc->aac_queues->qt_HostNormCmdQueue[0];
1955 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1956 &sc->aac_queues->qt_HostHighCmdQueue[0];
1957 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1958 &sc->aac_queues->qt_AdapNormCmdQueue[0];
1959 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1960 &sc->aac_queues->qt_AdapHighCmdQueue[0];
1961 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1962 &sc->aac_queues->qt_HostNormRespQueue[0];
1963 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1964 &sc->aac_queues->qt_HostHighRespQueue[0];
1965 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1966 &sc->aac_queues->qt_AdapNormRespQueue[0];
1967 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1968 &sc->aac_queues->qt_AdapHighRespQueue[0];
1971 * Do controller-type-specific initialisation
1973 switch (sc->aac_hwif) {
1974 case AAC_HWIF_I960RX:
1975 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, ~0);
1978 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, ~0);
1985 * Give the init structure to the controller.
1987 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1988 sc->aac_common_busaddr +
1989 offsetof(struct aac_common, ac_init), 0, 0, 0,
1991 device_printf(sc->aac_dev,
1992 "error establishing init structure\n");
2003 aac_setup_intr(struct aac_softc *sc)
2005 sc->aac_irq_rid = 0;
2006 if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
2009 RF_ACTIVE)) == NULL) {
2010 device_printf(sc->aac_dev, "can't allocate interrupt\n");
2013 if (sc->flags & AAC_FLAGS_NEW_COMM) {
2014 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2016 aac_new_intr, sc, &sc->aac_intr, NULL)) {
2017 device_printf(sc->aac_dev, "can't set up interrupt\n");
2021 if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2023 sc, &sc->aac_intr, NULL)) {
2024 device_printf(sc->aac_dev,
2025 "can't set up interrupt filter\n");
2033 * Send a synchronous command to the controller and wait for a result.
2034 * Indicate if the controller completed the command with an error status.
2037 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2038 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2044 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2046 /* populate the mailbox */
2047 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2049 /* ensure the sync command doorbell flag is cleared */
2050 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2052 /* then set it to signal the adapter */
2053 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2055 /* spin waiting for the command to complete */
2058 if (time_uptime > (then + AAC_IMMEDIATE_TIMEOUT)) {
2059 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2062 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2064 /* clear the completion flag */
2065 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2067 /* get the command status */
2068 status = AAC_GET_MAILBOX(sc, 0);
2072 if (status != AAC_SRB_STS_SUCCESS)
2078 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2079 struct aac_fib *fib, u_int16_t datasize)
2081 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2082 #if 0 /* XXX swildner */
2083 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2086 if (datasize > AAC_FIB_DATASIZE)
2090 * Set up the sync FIB
2092 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2093 AAC_FIBSTATE_INITIALISED |
2095 fib->Header.XferState |= xferstate;
2096 fib->Header.Command = command;
2097 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2098 fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2099 fib->Header.SenderSize = sizeof(struct aac_fib);
2100 fib->Header.SenderFibAddress = 0; /* Not needed */
2101 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2102 offsetof(struct aac_common,
2106 * Give the FIB to the controller, wait for a response.
2108 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2109 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2110 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2118 * Adapter-space FIB queue manipulation
2120 * Note that the queue implementation here is a little funky; neither the PI or
2121 * CI will ever be zero. This behaviour is a controller feature.
2127 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2128 {AAC_HOST_HIGH_CMD_ENTRIES, 0},
2129 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2130 {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2131 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2132 {AAC_HOST_HIGH_RESP_ENTRIES, 0},
2133 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2134 {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2138 * Atomically insert an entry into the nominated queue, returns 0 on success or
2139 * EBUSY if the queue is full.
2141 * Note: it would be more efficient to defer notifying the controller in
2142 * the case where we may be inserting several entries in rapid succession,
2143 * but implementing this usefully may be difficult (it would involve a
2144 * separate queue/notify interface).
2147 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2154 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2156 fib_size = cm->cm_fib->Header.Size;
2157 fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2159 /* get the producer/consumer indices */
2160 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2161 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2163 /* wrap the queue? */
2164 if (pi >= aac_qinfo[queue].size)
2167 /* check for queue full */
2168 if ((pi + 1) == ci) {
2174 * To avoid a race with its completion interrupt, place this command on
2175 * the busy queue prior to advertising it to the controller.
2177 aac_enqueue_busy(cm);
2179 /* populate queue entry */
2180 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2181 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2183 /* update producer index */
2184 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2186 /* notify the adapter if we know how */
2187 if (aac_qinfo[queue].notify != 0)
2188 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2197 * Atomically remove one entry from the nominated queue, returns 0 on
2198 * success or ENOENT if the queue is empty.
2201 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2202 struct aac_fib **fib_addr)
2205 u_int32_t fib_index;
2209 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2211 /* get the producer/consumer indices */
2212 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2213 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2215 /* check for queue empty */
2221 /* wrap the pi so the following test works */
2222 if (pi >= aac_qinfo[queue].size)
2229 /* wrap the queue? */
2230 if (ci >= aac_qinfo[queue].size)
2233 /* fetch the entry */
2234 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2237 case AAC_HOST_NORM_CMD_QUEUE:
2238 case AAC_HOST_HIGH_CMD_QUEUE:
2240 * The aq_fib_addr is only 32 bits wide so it can't be counted
2241 * on to hold an address. For AIF's, the adapter assumes
2242 * that it's giving us an address into the array of AIF fibs.
2243 * Therefore, we have to convert it to an index.
2245 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2246 sizeof(struct aac_fib);
2247 *fib_addr = &sc->aac_common->ac_fibs[fib_index];
2250 case AAC_HOST_NORM_RESP_QUEUE:
2251 case AAC_HOST_HIGH_RESP_QUEUE:
2253 struct aac_command *cm;
2256 * As above, an index is used instead of an actual address.
2257 * Gotta shift the index to account for the fast response
2258 * bit. No other correction is needed since this value was
2259 * originally provided by the driver via the SenderFibAddress
2262 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2263 cm = sc->aac_commands + (fib_index >> 2);
2264 *fib_addr = cm->cm_fib;
2267 * Is this a fast response? If it is, update the fib fields in
2268 * local memory since the whole fib isn't DMA'd back up.
2270 if (fib_index & 0x01) {
2271 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2272 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2277 panic("Invalid queue in aac_dequeue_fib()");
2281 /* update consumer index */
2282 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2284 /* if we have made the queue un-full, notify the adapter */
2285 if (notify && (aac_qinfo[queue].notify != 0))
2286 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2294 * Put our response to an Adapter Initialed Fib on the response queue
2297 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2304 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2306 /* Tell the adapter where the FIB is */
2307 fib_size = fib->Header.Size;
2308 fib_addr = fib->Header.SenderFibAddress;
2309 fib->Header.ReceiverFibAddress = fib_addr;
2311 /* get the producer/consumer indices */
2312 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2313 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2315 /* wrap the queue? */
2316 if (pi >= aac_qinfo[queue].size)
2319 /* check for queue full */
2320 if ((pi + 1) == ci) {
2325 /* populate queue entry */
2326 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2327 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2329 /* update producer index */
2330 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2332 /* notify the adapter if we know how */
2333 if (aac_qinfo[queue].notify != 0)
2334 AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2343 * Check for commands that have been outstanding for a suspiciously long time,
2344 * and complain about them.
2347 aac_timeout(struct aac_softc *sc)
2349 struct aac_command *cm;
2354 * Traverse the busy command list, bitch about late commands once
2358 deadline = time_uptime - AAC_CMD_TIMEOUT;
2359 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2360 if ((cm->cm_timestamp < deadline)
2361 && !(cm->cm_flags & AAC_CMD_TIMEDOUT)) {
2362 cm->cm_flags |= AAC_CMD_TIMEDOUT;
2363 device_printf(sc->aac_dev,
2364 "COMMAND %p (TYPE %d) TIMEOUT AFTER %d SECONDS\n",
2365 cm, cm->cm_fib->Header.Command,
2366 (int)(time_uptime-cm->cm_timestamp));
2367 AAC_PRINT_FIB(sc, cm->cm_fib);
2373 code = AAC_GET_FWSTATUS(sc);
2374 if (code != AAC_UP_AND_RUNNING) {
2375 device_printf(sc->aac_dev, "WARNING! Controller is no "
2376 "longer running! code= 0x%x\n", code);
2383 * Interface Function Vectors
2387 * Read the current firmware status word.
2390 aac_sa_get_fwstatus(struct aac_softc *sc)
2392 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2394 return(AAC_MEM0_GETREG4(sc, AAC_SA_FWSTATUS));
2398 aac_rx_get_fwstatus(struct aac_softc *sc)
2400 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2402 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2403 AAC_RX_OMR0 : AAC_RX_FWSTATUS));
2407 aac_rkt_get_fwstatus(struct aac_softc *sc)
2409 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2411 return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2412 AAC_RKT_OMR0 : AAC_RKT_FWSTATUS));
2416 * Notify the controller of a change in a given queue
2420 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2422 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2424 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2428 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2430 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2432 AAC_MEM0_SETREG4(sc, AAC_RX_IDBR, qbit);
2436 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2438 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2440 AAC_MEM0_SETREG4(sc, AAC_RKT_IDBR, qbit);
2444 * Get the interrupt reason bits
2447 aac_sa_get_istatus(struct aac_softc *sc)
2449 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2451 return(AAC_MEM0_GETREG2(sc, AAC_SA_DOORBELL0));
2455 aac_rx_get_istatus(struct aac_softc *sc)
2457 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2459 return(AAC_MEM0_GETREG4(sc, AAC_RX_ODBR));
2463 aac_rkt_get_istatus(struct aac_softc *sc)
2465 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2467 return(AAC_MEM0_GETREG4(sc, AAC_RKT_ODBR));
2471 * Clear some interrupt reason bits
2474 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2476 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2478 AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2482 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2484 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2486 AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, mask);
2490 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2492 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2494 AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, mask);
2498 * Populate the mailbox and set the command word
2501 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2502 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2504 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2506 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX, command);
2507 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2508 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2509 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2510 AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2514 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2515 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2517 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2519 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX, command);
2520 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2521 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2522 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2523 AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2527 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2528 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2530 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2532 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX, command);
2533 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2534 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2535 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2536 AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2540 * Fetch the immediate command status word
2543 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2545 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2547 return(AAC_MEM1_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2551 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2553 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2555 return(AAC_MEM1_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2559 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2561 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2563 return(AAC_MEM1_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2567 * Set/clear interrupt masks
2570 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2572 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2575 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2577 AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2582 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2584 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2587 if (sc->flags & AAC_FLAGS_NEW_COMM)
2588 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2590 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2592 AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~0);
2597 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2599 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2602 if (sc->flags & AAC_FLAGS_NEW_COMM)
2603 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2605 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2607 AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~0);
2612 * New comm. interface: Send command functions
2615 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2617 u_int32_t index, device;
2619 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2621 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2622 if (index == 0xffffffffL)
2623 index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2624 if (index == 0xffffffffL)
2626 aac_enqueue_busy(cm);
2628 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2630 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2632 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2633 AAC_MEM0_SETREG4(sc, AAC_RX_IQUE, index);
2638 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2640 u_int32_t index, device;
2642 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2644 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2645 if (index == 0xffffffffL)
2646 index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2647 if (index == 0xffffffffL)
2649 aac_enqueue_busy(cm);
2651 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2653 AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2655 AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2656 AAC_MEM0_SETREG4(sc, AAC_RKT_IQUE, index);
2661 * New comm. interface: get, set outbound queue index
2664 aac_rx_get_outb_queue(struct aac_softc *sc)
2666 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2668 return(AAC_MEM0_GETREG4(sc, AAC_RX_OQUE));
2672 aac_rkt_get_outb_queue(struct aac_softc *sc)
2674 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2676 return(AAC_MEM0_GETREG4(sc, AAC_RKT_OQUE));
2680 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2682 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2684 AAC_MEM0_SETREG4(sc, AAC_RX_OQUE, index);
2688 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2690 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2692 AAC_MEM0_SETREG4(sc, AAC_RKT_OQUE, index);
2696 * Debugging and Diagnostics
2700 * Print some information about the controller.
2703 aac_describe_controller(struct aac_softc *sc)
2705 struct aac_fib *fib;
2706 struct aac_adapter_info *info;
2707 char *adapter_type = "Adaptec RAID controller";
2709 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2711 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
2712 aac_alloc_sync_fib(sc, &fib);
2715 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2716 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2717 aac_release_sync_fib(sc);
2718 lockmgr(&sc->aac_io_lock, LK_RELEASE);
2722 /* save the kernel revision structure for later use */
2723 info = (struct aac_adapter_info *)&fib->data[0];
2724 sc->aac_revision = info->KernelRevision;
2727 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2728 "(%dMB cache, %dMB execution), %s\n",
2729 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2730 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2731 info->BufferMem / (1024 * 1024),
2732 info->ExecutionMem / (1024 * 1024),
2733 aac_describe_code(aac_battery_platform,
2734 info->batteryPlatform));
2736 device_printf(sc->aac_dev,
2737 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2738 info->KernelRevision.external.comp.major,
2739 info->KernelRevision.external.comp.minor,
2740 info->KernelRevision.external.comp.dash,
2741 info->KernelRevision.buildNumber,
2742 (u_int32_t)(info->SerialNumber & 0xffffff));
2744 device_printf(sc->aac_dev, "Supported Options=%b\n",
2745 sc->supported_options,
2768 if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2770 if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2771 device_printf(sc->aac_dev,
2772 "RequestSupplementAdapterInfo failed\n");
2774 adapter_type = ((struct aac_supplement_adapter_info *)
2775 &fib->data[0])->AdapterTypeText;
2777 device_printf(sc->aac_dev, "%s, aac driver %d.%d.%d-%d\n",
2779 AAC_DRIVER_MAJOR_VERSION, AAC_DRIVER_MINOR_VERSION,
2780 AAC_DRIVER_BUGFIX_LEVEL, AAC_DRIVER_BUILD);
2782 aac_release_sync_fib(sc);
2783 lockmgr(&sc->aac_io_lock, LK_RELEASE);
2787 * Look up a text description of a numeric error code and return a pointer to
2791 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2795 for (i = 0; table[i].string != NULL; i++)
2796 if (table[i].code == code)
2797 return(table[i].string);
2798 return(table[i + 1].string);
2802 * Management Interface
2806 aac_open(struct dev_open_args *ap)
2808 cdev_t dev = ap->a_head.a_dev;
2809 struct aac_softc *sc;
2812 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2813 device_busy(sc->aac_dev);
2819 aac_ioctl(struct dev_ioctl_args *ap)
2821 caddr_t arg = ap->a_data;
2822 cdev_t dev = ap->a_head.a_dev;
2823 u_long cmd = ap->a_cmd;
2824 union aac_statrequest *as;
2825 struct aac_softc *sc;
2828 as = (union aac_statrequest *)arg;
2830 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2834 switch (as->as_item) {
2839 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2840 sizeof(struct aac_qstat));
2848 case FSACTL_SENDFIB:
2849 case FSACTL_SEND_LARGE_FIB:
2850 arg = *(caddr_t*)arg;
2851 case FSACTL_LNX_SENDFIB:
2852 case FSACTL_LNX_SEND_LARGE_FIB:
2853 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2854 error = aac_ioctl_sendfib(sc, arg);
2856 case FSACTL_SEND_RAW_SRB:
2857 arg = *(caddr_t*)arg;
2858 case FSACTL_LNX_SEND_RAW_SRB:
2859 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2860 error = aac_ioctl_send_raw_srb(sc, arg);
2862 case FSACTL_AIF_THREAD:
2863 case FSACTL_LNX_AIF_THREAD:
2864 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2867 case FSACTL_OPEN_GET_ADAPTER_FIB:
2868 arg = *(caddr_t*)arg;
2869 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2870 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2871 error = aac_open_aif(sc, arg);
2873 case FSACTL_GET_NEXT_ADAPTER_FIB:
2874 arg = *(caddr_t*)arg;
2875 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2876 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2877 error = aac_getnext_aif(sc, arg);
2879 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2880 arg = *(caddr_t*)arg;
2881 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2882 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2883 error = aac_close_aif(sc, arg);
2885 case FSACTL_MINIPORT_REV_CHECK:
2886 arg = *(caddr_t*)arg;
2887 case FSACTL_LNX_MINIPORT_REV_CHECK:
2888 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
2889 error = aac_rev_check(sc, arg);
2891 case FSACTL_QUERY_DISK:
2892 arg = *(caddr_t*)arg;
2893 case FSACTL_LNX_QUERY_DISK:
2894 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
2895 error = aac_query_disk(sc, arg);
2897 case FSACTL_DELETE_DISK:
2898 case FSACTL_LNX_DELETE_DISK:
2900 * We don't trust the underland to tell us when to delete a
2901 * container, rather we rely on an AIF coming from the
2906 case FSACTL_GET_PCI_INFO:
2907 arg = *(caddr_t*)arg;
2908 case FSACTL_LNX_GET_PCI_INFO:
2909 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
2910 error = aac_get_pci_info(sc, arg);
2912 case FSACTL_GET_FEATURES:
2913 arg = *(caddr_t*)arg;
2914 case FSACTL_LNX_GET_FEATURES:
2915 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
2916 error = aac_supported_features(sc, arg);
2919 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
2926 static struct filterops aac_filterops =
2927 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, aac_filter_detach, aac_filter_read };
2930 aac_kqfilter(struct dev_kqfilter_args *ap)
2932 cdev_t dev = ap->a_head.a_dev;
2933 struct aac_softc *sc = dev->si_drv1;
2934 struct knote *kn = ap->a_kn;
2935 struct klist *klist;
2939 switch (kn->kn_filter) {
2941 kn->kn_fop = &aac_filterops;
2942 kn->kn_hook = (caddr_t)sc;
2945 ap->a_result = EOPNOTSUPP;
2949 klist = &sc->rcv_kq.ki_note;
2950 knote_insert(klist, kn);
2956 aac_filter_detach(struct knote *kn)
2958 struct aac_softc *sc = (struct aac_softc *)kn->kn_hook;
2959 struct klist *klist;
2961 klist = &sc->rcv_kq.ki_note;
2962 knote_remove(klist, kn);
2966 aac_filter_read(struct knote *kn, long hint)
2968 struct aac_softc *sc;
2969 struct aac_fib_context *ctx;
2971 sc = (struct aac_softc *)kn->kn_hook;
2973 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
2974 for (ctx = sc->fibctx; ctx; ctx = ctx->next)
2975 if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap)
2977 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
2983 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
2986 switch (event->ev_type) {
2987 case AAC_EVENT_CMFREE:
2988 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
2989 if (aac_alloc_command(sc, (struct aac_command **)arg)) {
2990 aac_add_event(sc, event);
2993 kfree(event, M_AACBUF);
3002 * Send a FIB supplied from userspace
3005 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
3007 struct aac_command *cm;
3010 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3017 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3018 if (aac_alloc_command(sc, &cm)) {
3019 struct aac_event *event;
3021 event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3022 M_INTWAIT | M_ZERO);
3023 event->ev_type = AAC_EVENT_CMFREE;
3024 event->ev_callback = aac_ioctl_event;
3025 event->ev_arg = &cm;
3026 aac_add_event(sc, event);
3027 lksleep(&cm, &sc->aac_io_lock, 0, "sendfib", 0);
3029 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3032 * Fetch the FIB header, then re-copy to get data as well.
3034 if ((error = copyin(ufib, cm->cm_fib,
3035 sizeof(struct aac_fib_header))) != 0)
3037 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3038 if (size > sc->aac_max_fib_size) {
3039 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
3040 size, sc->aac_max_fib_size);
3041 size = sc->aac_max_fib_size;
3043 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3045 cm->cm_fib->Header.Size = size;
3046 cm->cm_timestamp = time_uptime;
3049 * Pass the FIB to the controller, wait for it to complete.
3051 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3052 error = aac_wait_command(cm);
3053 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3055 device_printf(sc->aac_dev,
3056 "aac_wait_command return %d\n", error);
3061 * Copy the FIB and data back out to the caller.
3063 size = cm->cm_fib->Header.Size;
3064 if (size > sc->aac_max_fib_size) {
3065 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
3066 size, sc->aac_max_fib_size);
3067 size = sc->aac_max_fib_size;
3069 error = copyout(cm->cm_fib, ufib, size);
3073 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3074 aac_release_command(cm);
3075 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3081 * Send a passthrough FIB supplied from userspace
3084 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
3086 struct aac_command *cm;
3087 struct aac_event *event;
3088 struct aac_fib *fib;
3089 struct aac_srb *srbcmd, *user_srb;
3090 struct aac_sg_entry *sge;
3092 struct aac_sg_entry64 *sge64;
3094 void *srb_sg_address, *ureply;
3095 uint32_t fibsize, srb_sg_bytecount;
3096 int error, transfer_data;
3098 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3103 user_srb = (struct aac_srb *)arg;
3105 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3106 if (aac_alloc_command(sc, &cm)) {
3107 event = kmalloc(sizeof(struct aac_event), M_AACBUF,
3109 if (event == NULL) {
3111 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3114 event->ev_type = AAC_EVENT_CMFREE;
3115 event->ev_callback = aac_ioctl_event;
3116 event->ev_arg = &cm;
3117 aac_add_event(sc, event);
3118 lksleep(cm, &sc->aac_io_lock, 0, "aacraw", 0);
3120 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3124 srbcmd = (struct aac_srb *)fib->data;
3125 error = copyin(&user_srb->data_len, &fibsize, sizeof(uint32_t));
3128 if (fibsize > (sc->aac_max_fib_size - sizeof(struct aac_fib_header))) {
3132 error = copyin(user_srb, srbcmd, fibsize);
3135 srbcmd->function = 0;
3136 srbcmd->retry_limit = 0;
3137 if (srbcmd->sg_map.SgCount > 1) {
3142 /* Retrieve correct SG entries. */
3143 if (fibsize == (sizeof(struct aac_srb) +
3144 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry))) {
3145 sge = srbcmd->sg_map.SgEntry;
3146 srb_sg_bytecount = sge->SgByteCount;
3147 srb_sg_address = (void *)(uintptr_t)sge->SgAddress;
3150 else if (fibsize == (sizeof(struct aac_srb) +
3151 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry64))) {
3153 sge64 = (struct aac_sg_entry64 *)srbcmd->sg_map.SgEntry;
3154 srb_sg_bytecount = sge64->SgByteCount;
3155 srb_sg_address = (void *)sge64->SgAddress;
3156 if (sge64->SgAddress > 0xffffffffull &&
3157 (sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
3167 ureply = (char *)arg + fibsize;
3168 srbcmd->data_len = srb_sg_bytecount;
3169 if (srbcmd->sg_map.SgCount == 1)
3172 cm->cm_sgtable = (struct aac_sg_table *)&srbcmd->sg_map;
3173 if (transfer_data) {
3174 cm->cm_datalen = srb_sg_bytecount;
3175 cm->cm_data = kmalloc(cm->cm_datalen, M_AACBUF, M_NOWAIT);
3176 if (cm->cm_data == NULL) {
3180 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN)
3181 cm->cm_flags |= AAC_CMD_DATAIN;
3182 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_OUT) {
3183 cm->cm_flags |= AAC_CMD_DATAOUT;
3184 error = copyin(srb_sg_address, cm->cm_data,
3191 fib->Header.Size = sizeof(struct aac_fib_header) +
3192 sizeof(struct aac_srb);
3193 fib->Header.XferState =
3194 AAC_FIBSTATE_HOSTOWNED |
3195 AAC_FIBSTATE_INITIALISED |
3196 AAC_FIBSTATE_EMPTY |
3197 AAC_FIBSTATE_FROMHOST |
3198 AAC_FIBSTATE_REXPECTED |
3200 AAC_FIBSTATE_ASYNC |
3201 AAC_FIBSTATE_FAST_RESPONSE;
3202 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) != 0 ?
3203 ScsiPortCommandU64 : ScsiPortCommand;
3205 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3206 aac_wait_command(cm);
3207 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3209 if (transfer_data && (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN) != 0) {
3210 error = copyout(cm->cm_data, srb_sg_address, cm->cm_datalen);
3214 error = copyout(fib->data, ureply, sizeof(struct aac_srb_response));
3217 if (cm->cm_data != NULL)
3218 kfree(cm->cm_data, M_AACBUF);
3219 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3220 aac_release_command(cm);
3221 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3227 aac_close(struct dev_close_args *ap)
3229 cdev_t dev = ap->a_head.a_dev;
3230 struct aac_softc *sc;
3233 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3235 device_unbusy(sc->aac_dev);
3242 * Handle an AIF sent to us by the controller; queue it for later reference.
3243 * If the queue fills up, then drop the older entries.
3246 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3248 struct aac_aif_command *aif;
3249 struct aac_container *co, *co_next;
3250 struct aac_fib_context *ctx;
3251 struct aac_mntinforesp *mir;
3252 int next, current, found;
3253 int count = 0, added = 0, i = 0;
3256 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3258 aif = (struct aac_aif_command*)&fib->data[0];
3259 aac_print_aif(sc, aif);
3261 /* Is it an event that we should care about? */
3262 switch (aif->command) {
3263 case AifCmdEventNotify:
3264 switch (aif->data.EN.type) {
3265 case AifEnAddContainer:
3266 case AifEnDeleteContainer:
3268 * A container was added or deleted, but the message
3269 * doesn't tell us anything else! Re-enumerate the
3270 * containers and sort things out.
3272 aac_alloc_sync_fib(sc, &fib);
3275 * Ask the controller for its containers one at
3277 * XXX What if the controller's list changes
3278 * midway through this enumaration?
3279 * XXX This should be done async.
3281 if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
3284 count = mir->MntRespCount;
3286 * Check the container against our list.
3287 * co->co_found was already set to 0 in a
3290 if ((mir->Status == ST_OK) &&
3291 (mir->MntTable[0].VolType != CT_NONE)) {
3294 &sc->aac_container_tqh,
3296 if (co->co_mntobj.ObjectId ==
3297 mir->MntTable[0].ObjectId) {
3304 * If the container matched, continue
3313 * This is a new container. Do all the
3314 * appropriate things to set it up.
3316 aac_add_container(sc, mir, 1);
3320 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3321 aac_release_sync_fib(sc);
3324 * Go through our list of containers and see which ones
3325 * were not marked 'found'. Since the controller didn't
3326 * list them they must have been deleted. Do the
3327 * appropriate steps to destroy the device. Also reset
3328 * the co->co_found field.
3330 co = TAILQ_FIRST(&sc->aac_container_tqh);
3331 while (co != NULL) {
3332 if (co->co_found == 0) {
3333 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3335 device_delete_child(sc->aac_dev,
3338 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3339 co_next = TAILQ_NEXT(co, co_link);
3340 lockmgr(&sc->aac_container_lock, LK_EXCLUSIVE);
3341 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3343 lockmgr(&sc->aac_container_lock, LK_RELEASE);
3344 kfree(co, M_AACBUF);
3348 co = TAILQ_NEXT(co, co_link);
3352 /* Attach the newly created containers */
3354 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3356 bus_generic_attach(sc->aac_dev);
3358 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3363 case AifEnEnclosureManagement:
3364 switch (aif->data.EN.data.EEE.eventType) {
3365 case AIF_EM_DRIVE_INSERTION:
3366 case AIF_EM_DRIVE_REMOVAL:
3367 channel = aif->data.EN.data.EEE.unitID;
3368 if (sc->cam_rescan_cb != NULL)
3369 sc->cam_rescan_cb(sc,
3370 (channel >> 24) & 0xF,
3371 (channel & 0xFFFF));
3377 case AifEnDeleteJBOD:
3378 channel = aif->data.EN.data.ECE.container;
3379 if (sc->cam_rescan_cb != NULL)
3380 sc->cam_rescan_cb(sc, (channel >> 24) & 0xF,
3381 AAC_CAM_TARGET_WILDCARD);
3392 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3393 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3394 current = sc->aifq_idx;
3395 next = (current + 1) % AAC_AIFQ_LENGTH;
3397 sc->aifq_filled = 1;
3398 bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3399 /* modify AIF contexts */
3400 if (sc->aifq_filled) {
3401 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3402 if (next == ctx->ctx_idx)
3404 else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3405 ctx->ctx_idx = next;
3408 sc->aifq_idx = next;
3409 /* On the off chance that someone is sleeping for an aif... */
3410 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3411 wakeup(sc->aac_aifq);
3412 /* token may have been lost */
3413 /* Wakeup any poll()ers */
3414 KNOTE(&sc->rcv_kq.ki_note, 0);
3415 /* token may have been lost */
3416 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3422 * Return the Revision of the driver to userspace and check to see if the
3423 * userspace app is possibly compatible. This is extremely bogus since
3424 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3425 * returning what the card reported.
3428 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3430 struct aac_rev_check rev_check;
3431 struct aac_rev_check_resp rev_check_resp;
3434 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3437 * Copyin the revision struct from userspace
3439 if ((error = copyin(udata, (caddr_t)&rev_check,
3440 sizeof(struct aac_rev_check))) != 0) {
3444 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3445 rev_check.callingRevision.buildNumber);
3448 * Doctor up the response struct.
3450 rev_check_resp.possiblyCompatible = 1;
3451 rev_check_resp.adapterSWRevision.external.comp.major =
3452 AAC_DRIVER_MAJOR_VERSION;
3453 rev_check_resp.adapterSWRevision.external.comp.minor =
3454 AAC_DRIVER_MINOR_VERSION;
3455 rev_check_resp.adapterSWRevision.external.comp.type =
3457 rev_check_resp.adapterSWRevision.external.comp.dash =
3458 AAC_DRIVER_BUGFIX_LEVEL;
3459 rev_check_resp.adapterSWRevision.buildNumber =
3462 return(copyout((caddr_t)&rev_check_resp, udata,
3463 sizeof(struct aac_rev_check_resp)));
3467 * Pass the fib context to the caller
3470 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3472 struct aac_fib_context *fibctx, *ctx;
3475 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3477 fibctx = kmalloc(sizeof(struct aac_fib_context), M_AACBUF, M_NOWAIT|M_ZERO);
3481 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3482 /* all elements are already 0, add to queue */
3483 if (sc->fibctx == NULL)
3484 sc->fibctx = fibctx;
3486 for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3492 /* evaluate unique value */
3493 fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3495 while (ctx != fibctx) {
3496 if (ctx->unique == fibctx->unique) {
3503 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3505 error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3507 aac_close_aif(sc, (caddr_t)ctx);
3512 * Close the caller's fib context
3515 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3517 struct aac_fib_context *ctx;
3519 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3521 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3522 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3523 if (ctx->unique == *(uint32_t *)&arg) {
3524 if (ctx == sc->fibctx)
3527 ctx->prev->next = ctx->next;
3529 ctx->next->prev = ctx->prev;
3534 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3536 kfree(ctx, M_AACBUF);
3542 * Pass the caller the next AIF in their queue
3545 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3547 struct get_adapter_fib_ioctl agf;
3548 struct aac_fib_context *ctx;
3551 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3553 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3554 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3555 if (agf.AdapterFibContext == ctx->unique)
3561 error = aac_return_aif(sc, ctx, agf.AifFib);
3562 if (error == EAGAIN && agf.Wait) {
3563 fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3564 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3565 while (error == EAGAIN) {
3566 error = tsleep(sc->aac_aifq,
3567 PCATCH, "aacaif", 0);
3569 error = aac_return_aif(sc, ctx, agf.AifFib);
3571 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3578 * Hand the next AIF off the top of the queue out to userspace.
3581 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3585 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3587 lockmgr(&sc->aac_aifq_lock, LK_EXCLUSIVE);
3588 current = ctx->ctx_idx;
3589 if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3591 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3595 copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3597 device_printf(sc->aac_dev,
3598 "aac_return_aif: copyout returned %d\n", error);
3601 ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3603 lockmgr(&sc->aac_aifq_lock, LK_RELEASE);
3608 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3610 struct aac_pci_info {
3616 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3618 pciinf.bus = pci_get_bus(sc->aac_dev);
3619 pciinf.slot = pci_get_slot(sc->aac_dev);
3621 error = copyout((caddr_t)&pciinf, uptr,
3622 sizeof(struct aac_pci_info));
3628 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3630 struct aac_features f;
3633 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3635 if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3639 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3640 * ALL zero in the featuresState, the driver will return the current
3641 * state of all the supported features, the data field will not be
3643 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3644 * a specific bit set in the featuresState, the driver will return the
3645 * current state of this specific feature and whatever data that are
3646 * associated with the feature in the data field or perform whatever
3647 * action needed indicates in the data field.
3649 if (f.feat.fValue == 0) {
3650 f.feat.fBits.largeLBA =
3651 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3652 /* TODO: In the future, add other features state here as well */
3654 if (f.feat.fBits.largeLBA)
3655 f.feat.fBits.largeLBA =
3656 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3657 /* TODO: Add other features state and data in the future */
3660 error = copyout(&f, uptr, sizeof (f));
3665 * Give the userland some information about the container. The AAC arch
3666 * expects the driver to be a SCSI passthrough type driver, so it expects
3667 * the containers to have b:t:l numbers. Fake it.
3670 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3672 struct aac_query_disk query_disk;
3673 struct aac_container *co;
3674 struct aac_disk *disk;
3677 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3681 error = copyin(uptr, (caddr_t)&query_disk,
3682 sizeof(struct aac_query_disk));
3686 id = query_disk.ContainerNumber;
3690 lockmgr(&sc->aac_container_lock, LK_EXCLUSIVE);
3691 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3692 if (co->co_mntobj.ObjectId == id)
3697 query_disk.Valid = 0;
3698 query_disk.Locked = 0;
3699 query_disk.Deleted = 1; /* XXX is this right? */
3701 disk = device_get_softc(co->co_disk);
3702 query_disk.Valid = 1;
3704 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3705 query_disk.Deleted = 0;
3706 query_disk.Bus = device_get_unit(sc->aac_dev);
3707 query_disk.Target = disk->unit;
3709 query_disk.UnMapped = 0;
3710 bcopy(disk->ad_dev_t->si_name,
3711 &query_disk.diskDeviceName[0], 10);
3713 lockmgr(&sc->aac_container_lock, LK_RELEASE);
3715 error = copyout((caddr_t)&query_disk, uptr,
3716 sizeof(struct aac_query_disk));
3722 aac_get_bus_info(struct aac_softc *sc)
3724 struct aac_fib *fib;
3725 struct aac_ctcfg *c_cmd;
3726 struct aac_ctcfg_resp *c_resp;
3727 struct aac_vmioctl *vmi;
3728 struct aac_vmi_businf_resp *vmi_resp;
3729 struct aac_getbusinf businfo;
3730 struct aac_sim *caminf;
3732 int i, found, error;
3734 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
3735 aac_alloc_sync_fib(sc, &fib);
3736 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3737 bzero(c_cmd, sizeof(struct aac_ctcfg));
3739 c_cmd->Command = VM_ContainerConfig;
3740 c_cmd->cmd = CT_GET_SCSI_METHOD;
3743 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3744 sizeof(struct aac_ctcfg));
3746 device_printf(sc->aac_dev, "Error %d sending "
3747 "VM_ContainerConfig command\n", error);
3748 aac_release_sync_fib(sc);
3749 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3753 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3754 if (c_resp->Status != ST_OK) {
3755 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3757 aac_release_sync_fib(sc);
3758 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3762 sc->scsi_method_id = c_resp->param;
3764 vmi = (struct aac_vmioctl *)&fib->data[0];
3765 bzero(vmi, sizeof(struct aac_vmioctl));
3767 vmi->Command = VM_Ioctl;
3768 vmi->ObjType = FT_DRIVE;
3769 vmi->MethId = sc->scsi_method_id;
3771 vmi->IoctlCmd = GetBusInfo;
3773 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3774 sizeof(struct aac_vmi_businf_resp));
3776 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3778 aac_release_sync_fib(sc);
3779 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3783 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3784 if (vmi_resp->Status != ST_OK) {
3785 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3787 aac_release_sync_fib(sc);
3788 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3792 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3793 aac_release_sync_fib(sc);
3794 lockmgr(&sc->aac_io_lock, LK_RELEASE);
3797 for (i = 0; i < businfo.BusCount; i++) {
3798 if (businfo.BusValid[i] != AAC_BUS_VALID)
3801 caminf = (struct aac_sim *)kmalloc(sizeof(struct aac_sim),
3802 M_AACBUF, M_INTWAIT | M_ZERO);
3804 child = device_add_child(sc->aac_dev, "aacp", -1);
3805 if (child == NULL) {
3806 device_printf(sc->aac_dev,
3807 "device_add_child failed for passthrough bus %d\n",
3809 kfree(caminf, M_AACBUF);
3813 caminf->TargetsPerBus = businfo.TargetsPerBus;
3814 caminf->BusNumber = i;
3815 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3816 caminf->aac_sc = sc;
3817 caminf->sim_dev = child;
3819 device_set_ivars(child, caminf);
3820 device_set_desc(child, "SCSI Passthrough Bus");
3821 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3827 bus_generic_attach(sc->aac_dev);