2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2001 Scott Long
6 * Copyright (c) 2000 BSDi
7 * Copyright (c) 2001-2010 Adaptec, Inc.
8 * Copyright (c) 2010-2012 PMC-Sierra, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Driver for the Adaptec by PMC Series 6,7,8,... families of RAID controllers
39 #define AAC_DRIVERNAME "aacraid"
41 #include "opt_aacraid.h"
43 /* #include <stddef.h> */
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/kthread.h>
49 #include <sys/sysctl.h>
51 #include <sys/ioccom.h>
55 #include <sys/signalvar.h>
57 #include <sys/eventhandler.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcivar.h>
66 #include <dev/aacraid/aacraid_reg.h>
67 #include <sys/aac_ioctl.h>
68 #include <dev/aacraid/aacraid_debug.h>
69 #include <dev/aacraid/aacraid_var.h>
71 #ifndef FILTER_HANDLED
72 #define FILTER_HANDLED 0x02
75 static void aac_add_container(struct aac_softc *sc,
76 struct aac_mntinforesp *mir, int f,
78 static void aac_get_bus_info(struct aac_softc *sc);
79 static void aac_container_bus(struct aac_softc *sc);
80 static void aac_daemon(void *arg);
81 static int aac_convert_sgraw2(struct aac_softc *sc, struct aac_raw_io2 *raw,
82 int pages, int nseg, int nseg_new);
84 /* Command Processing */
85 static void aac_timeout(struct aac_softc *sc);
86 static void aac_command_thread(struct aac_softc *sc);
87 static int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
88 u_int32_t xferstate, struct aac_fib *fib,
90 /* Command Buffer Management */
91 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
93 static int aac_alloc_commands(struct aac_softc *sc);
94 static void aac_free_commands(struct aac_softc *sc);
95 static void aac_unmap_command(struct aac_command *cm);
97 /* Hardware Interface */
98 static int aac_alloc(struct aac_softc *sc);
99 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
101 static int aac_check_firmware(struct aac_softc *sc);
102 static void aac_define_int_mode(struct aac_softc *sc);
103 static int aac_init(struct aac_softc *sc);
104 static int aac_find_pci_capability(struct aac_softc *sc, int cap);
105 static int aac_setup_intr(struct aac_softc *sc);
106 static int aac_check_config(struct aac_softc *sc);
108 /* PMC SRC interface */
109 static int aac_src_get_fwstatus(struct aac_softc *sc);
110 static void aac_src_qnotify(struct aac_softc *sc, int qbit);
111 static int aac_src_get_istatus(struct aac_softc *sc);
112 static void aac_src_clear_istatus(struct aac_softc *sc, int mask);
113 static void aac_src_set_mailbox(struct aac_softc *sc, u_int32_t command,
114 u_int32_t arg0, u_int32_t arg1,
115 u_int32_t arg2, u_int32_t arg3);
116 static int aac_src_get_mailbox(struct aac_softc *sc, int mb);
117 static void aac_src_access_devreg(struct aac_softc *sc, int mode);
118 static int aac_src_send_command(struct aac_softc *sc, struct aac_command *cm);
119 static int aac_src_get_outb_queue(struct aac_softc *sc);
120 static void aac_src_set_outb_queue(struct aac_softc *sc, int index);
122 struct aac_interface aacraid_src_interface = {
123 aac_src_get_fwstatus,
126 aac_src_clear_istatus,
129 aac_src_access_devreg,
130 aac_src_send_command,
131 aac_src_get_outb_queue,
132 aac_src_set_outb_queue
135 /* PMC SRCv interface */
136 static void aac_srcv_set_mailbox(struct aac_softc *sc, u_int32_t command,
137 u_int32_t arg0, u_int32_t arg1,
138 u_int32_t arg2, u_int32_t arg3);
139 static int aac_srcv_get_mailbox(struct aac_softc *sc, int mb);
141 struct aac_interface aacraid_srcv_interface = {
142 aac_src_get_fwstatus,
145 aac_src_clear_istatus,
146 aac_srcv_set_mailbox,
147 aac_srcv_get_mailbox,
148 aac_src_access_devreg,
149 aac_src_send_command,
150 aac_src_get_outb_queue,
151 aac_src_set_outb_queue
154 /* Debugging and Diagnostics */
155 static struct aac_code_lookup aac_cpu_variant[] = {
156 {"i960JX", CPUI960_JX},
157 {"i960CX", CPUI960_CX},
158 {"i960HX", CPUI960_HX},
159 {"i960RX", CPUI960_RX},
160 {"i960 80303", CPUI960_80303},
161 {"StrongARM SA110", CPUARM_SA110},
162 {"PPC603e", CPUPPC_603e},
163 {"XScale 80321", CPU_XSCALE_80321},
164 {"MIPS 4KC", CPU_MIPS_4KC},
165 {"MIPS 5KC", CPU_MIPS_5KC},
166 {"Unknown StrongARM", CPUARM_xxx},
167 {"Unknown PowerPC", CPUPPC_xxx},
169 {"Unknown processor", 0}
172 static struct aac_code_lookup aac_battery_platform[] = {
173 {"required battery present", PLATFORM_BAT_REQ_PRESENT},
174 {"REQUIRED BATTERY NOT PRESENT", PLATFORM_BAT_REQ_NOTPRESENT},
175 {"optional battery present", PLATFORM_BAT_OPT_PRESENT},
176 {"optional battery not installed", PLATFORM_BAT_OPT_NOTPRESENT},
177 {"no battery support", PLATFORM_BAT_NOT_SUPPORTED},
179 {"unknown battery platform", 0}
181 static void aac_describe_controller(struct aac_softc *sc);
182 static char *aac_describe_code(struct aac_code_lookup *table,
185 /* Management Interface */
186 static d_open_t aac_open;
187 static d_ioctl_t aac_ioctl;
188 static d_poll_t aac_poll;
189 #if __FreeBSD_version >= 702000
190 static void aac_cdevpriv_dtor(void *arg);
192 static d_close_t aac_close;
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, struct aac_fib *fib);
197 static void aac_request_aif(struct aac_softc *sc);
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 int aac_reset_adapter(struct aac_softc *sc);
210 static int aac_get_container_info(struct aac_softc *sc,
211 struct aac_fib *fib, int cid,
212 struct aac_mntinforesp *mir,
215 aac_check_adapter_health(struct aac_softc *sc, u_int8_t *bled);
217 static struct cdevsw aacraid_cdevsw = {
218 .d_version = D_VERSION,
219 .d_flags = D_NEEDGIANT,
221 #if __FreeBSD_version < 702000
222 .d_close = aac_close,
224 .d_ioctl = aac_ioctl,
229 MALLOC_DEFINE(M_AACRAIDBUF, "aacraid_buf", "Buffers for the AACRAID driver");
232 SYSCTL_NODE(_hw, OID_AUTO, aacraid, CTLFLAG_RD, 0, "AACRAID driver parameters");
239 * Initialize the controller and softc
242 aacraid_attach(struct aac_softc *sc)
246 struct aac_mntinforesp mir;
247 int count = 0, i = 0;
250 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
251 sc->hint_flags = device_get_flags(sc->aac_dev);
253 * Initialize per-controller queues.
259 /* mark controller as suspended until we get ourselves organised */
260 sc->aac_state |= AAC_STATE_SUSPEND;
263 * Check that the firmware on the card is supported.
265 sc->msi_enabled = FALSE;
266 if ((error = aac_check_firmware(sc)) != 0)
272 mtx_init(&sc->aac_io_lock, "AACRAID I/O lock", NULL, MTX_DEF);
273 TAILQ_INIT(&sc->aac_container_tqh);
274 TAILQ_INIT(&sc->aac_ev_cmfree);
276 #if __FreeBSD_version >= 800000
277 /* Initialize the clock daemon callout. */
278 callout_init_mtx(&sc->aac_daemontime, &sc->aac_io_lock, 0);
281 * Initialize the adapter.
283 if ((error = aac_alloc(sc)) != 0)
285 if (!(sc->flags & AAC_FLAGS_SYNC_MODE)) {
286 aac_define_int_mode(sc);
287 if ((error = aac_init(sc)) != 0)
292 * Allocate and connect our interrupt.
294 if ((error = aac_setup_intr(sc)) != 0)
298 * Print a little information about the controller.
300 aac_describe_controller(sc);
303 * Make the control device.
305 unit = device_get_unit(sc->aac_dev);
306 sc->aac_dev_t = make_dev(&aacraid_cdevsw, unit, UID_ROOT, GID_OPERATOR,
307 0640, "aacraid%d", unit);
308 sc->aac_dev_t->si_drv1 = sc;
310 /* Create the AIF thread */
311 if (aac_kthread_create((void(*)(void *))aac_command_thread, sc,
312 &sc->aifthread, 0, 0, "aacraid%daif", unit))
313 panic("Could not create AIF thread");
315 /* Register the shutdown method to only be called post-dump */
316 if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aacraid_shutdown,
317 sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
318 device_printf(sc->aac_dev,
319 "shutdown event registration failed\n");
321 /* Find containers */
322 mtx_lock(&sc->aac_io_lock);
323 aac_alloc_sync_fib(sc, &fib);
324 /* loop over possible containers */
326 if ((aac_get_container_info(sc, fib, i, &mir, &uid)) != 0)
329 count = mir.MntRespCount;
330 aac_add_container(sc, &mir, 0, uid);
332 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
333 aac_release_sync_fib(sc);
334 mtx_unlock(&sc->aac_io_lock);
336 /* Register with CAM for the containers */
337 TAILQ_INIT(&sc->aac_sim_tqh);
338 aac_container_bus(sc);
339 /* Register with CAM for the non-DASD devices */
340 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0)
341 aac_get_bus_info(sc);
343 /* poke the bus to actually attach the child devices */
344 bus_generic_attach(sc->aac_dev);
346 /* mark the controller up */
347 sc->aac_state &= ~AAC_STATE_SUSPEND;
349 /* enable interrupts now */
350 AAC_ACCESS_DEVREG(sc, AAC_ENABLE_INTERRUPT);
352 #if __FreeBSD_version >= 800000
353 mtx_lock(&sc->aac_io_lock);
354 callout_reset(&sc->aac_daemontime, 60 * hz, aac_daemon, sc);
355 mtx_unlock(&sc->aac_io_lock);
361 sc->timeout_id = timeout(aac_daemon, (void *)sc, tvtohz(&tv));
369 aac_daemon(void *arg)
371 struct aac_softc *sc;
373 struct aac_command *cm;
377 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
379 #if __FreeBSD_version >= 800000
380 mtx_assert(&sc->aac_io_lock, MA_OWNED);
381 if (callout_pending(&sc->aac_daemontime) ||
382 callout_active(&sc->aac_daemontime) == 0)
385 mtx_lock(&sc->aac_io_lock);
389 if (!aacraid_alloc_command(sc, &cm)) {
391 cm->cm_timestamp = time_uptime;
393 cm->cm_flags |= AAC_CMD_WAIT;
396 sizeof(struct aac_fib_header) + sizeof(u_int32_t);
397 fib->Header.XferState =
398 AAC_FIBSTATE_HOSTOWNED |
399 AAC_FIBSTATE_INITIALISED |
401 AAC_FIBSTATE_FROMHOST |
402 AAC_FIBSTATE_REXPECTED |
405 AAC_FIBSTATE_FAST_RESPONSE;
406 fib->Header.Command = SendHostTime;
407 *(uint32_t *)fib->data = tv.tv_sec;
409 aacraid_map_command_sg(cm, NULL, 0, 0);
410 aacraid_release_command(cm);
413 #if __FreeBSD_version >= 800000
414 callout_schedule(&sc->aac_daemontime, 30 * 60 * hz);
416 mtx_unlock(&sc->aac_io_lock);
419 sc->timeout_id = timeout(aac_daemon, (void *)sc, tvtohz(&tv));
424 aacraid_add_event(struct aac_softc *sc, struct aac_event *event)
427 switch (event->ev_type & AAC_EVENT_MASK) {
428 case AAC_EVENT_CMFREE:
429 TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
432 device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
441 * Request information of container #cid
444 aac_get_container_info(struct aac_softc *sc, struct aac_fib *sync_fib, int cid,
445 struct aac_mntinforesp *mir, u_int32_t *uid)
447 struct aac_command *cm;
449 struct aac_mntinfo *mi;
450 struct aac_cnt_config *ccfg;
453 if (sync_fib == NULL) {
454 if (aacraid_alloc_command(sc, &cm)) {
455 device_printf(sc->aac_dev,
456 "Warning, no free command available\n");
464 mi = (struct aac_mntinfo *)&fib->data[0];
465 /* 4KB support?, 64-bit LBA? */
466 if (sc->aac_support_opt2 & AAC_SUPPORTED_VARIABLE_BLOCK_SIZE)
467 mi->Command = VM_NameServeAllBlk;
468 else if (sc->flags & AAC_FLAGS_LBA_64BIT)
469 mi->Command = VM_NameServe64;
471 mi->Command = VM_NameServe;
472 mi->MntType = FT_FILESYS;
476 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
477 sizeof(struct aac_mntinfo))) {
478 device_printf(sc->aac_dev, "Error probing container %d\n", cid);
482 cm->cm_timestamp = time_uptime;
486 sizeof(struct aac_fib_header) + sizeof(struct aac_mntinfo);
487 fib->Header.XferState =
488 AAC_FIBSTATE_HOSTOWNED |
489 AAC_FIBSTATE_INITIALISED |
491 AAC_FIBSTATE_FROMHOST |
492 AAC_FIBSTATE_REXPECTED |
495 AAC_FIBSTATE_FAST_RESPONSE;
496 fib->Header.Command = ContainerCommand;
497 if (aacraid_wait_command(cm) != 0) {
498 device_printf(sc->aac_dev, "Error probing container %d\n", cid);
499 aacraid_release_command(cm);
503 bcopy(&fib->data[0], mir, sizeof(struct aac_mntinforesp));
507 if (mir->MntTable[0].VolType != CT_NONE &&
508 !(mir->MntTable[0].ContentState & AAC_FSCS_HIDDEN)) {
509 if (!(sc->aac_support_opt2 & AAC_SUPPORTED_VARIABLE_BLOCK_SIZE)) {
510 mir->MntTable[0].ObjExtension.BlockDevice.BlockSize = 0x200;
511 mir->MntTable[0].ObjExtension.BlockDevice.bdLgclPhysMap = 0;
513 ccfg = (struct aac_cnt_config *)&fib->data[0];
514 bzero(ccfg, sizeof (*ccfg) - CT_PACKET_SIZE);
515 ccfg->Command = VM_ContainerConfig;
516 ccfg->CTCommand.command = CT_CID_TO_32BITS_UID;
517 ccfg->CTCommand.param[0] = cid;
520 rval = aac_sync_fib(sc, ContainerCommand, 0, fib,
521 sizeof(struct aac_cnt_config));
522 if (rval == 0 && ccfg->Command == ST_OK &&
523 ccfg->CTCommand.param[0] == CT_OK &&
524 mir->MntTable[0].VolType != CT_PASSTHRU)
525 *uid = ccfg->CTCommand.param[1];
528 sizeof(struct aac_fib_header) + sizeof(struct aac_cnt_config);
529 fib->Header.XferState =
530 AAC_FIBSTATE_HOSTOWNED |
531 AAC_FIBSTATE_INITIALISED |
533 AAC_FIBSTATE_FROMHOST |
534 AAC_FIBSTATE_REXPECTED |
537 AAC_FIBSTATE_FAST_RESPONSE;
538 fib->Header.Command = ContainerCommand;
539 rval = aacraid_wait_command(cm);
540 if (rval == 0 && ccfg->Command == ST_OK &&
541 ccfg->CTCommand.param[0] == CT_OK &&
542 mir->MntTable[0].VolType != CT_PASSTHRU)
543 *uid = ccfg->CTCommand.param[1];
544 aacraid_release_command(cm);
552 * Create a device to represent a new container
555 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f,
558 struct aac_container *co;
560 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
563 * Check container volume type for validity. Note that many of
564 * the possible types may never show up.
566 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
567 co = (struct aac_container *)malloc(sizeof *co, M_AACRAIDBUF,
570 panic("Out of memory?!");
574 bcopy(&mir->MntTable[0], &co->co_mntobj,
575 sizeof(struct aac_mntobj));
577 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
582 * Allocate resources associated with (sc)
585 aac_alloc(struct aac_softc *sc)
589 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
592 * Create DMA tag for mapping buffers into controller-addressable space.
594 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
595 1, 0, /* algnmnt, boundary */
596 (sc->flags & AAC_FLAGS_SG_64BIT) ?
598 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
599 BUS_SPACE_MAXADDR, /* highaddr */
600 NULL, NULL, /* filter, filterarg */
601 sc->aac_max_sectors << 9, /* maxsize */
602 sc->aac_sg_tablesize, /* nsegments */
603 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
604 BUS_DMA_ALLOCNOW, /* flags */
605 busdma_lock_mutex, /* lockfunc */
606 &sc->aac_io_lock, /* lockfuncarg */
607 &sc->aac_buffer_dmat)) {
608 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
613 * Create DMA tag for mapping FIBs into controller-addressable space..
615 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE1)
616 maxsize = sc->aac_max_fibs_alloc * (sc->aac_max_fib_size +
617 sizeof(struct aac_fib_xporthdr) + 31);
619 maxsize = sc->aac_max_fibs_alloc * (sc->aac_max_fib_size + 31);
620 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
621 1, 0, /* algnmnt, boundary */
622 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
623 BUS_SPACE_MAXADDR_32BIT :
624 0x7fffffff, /* lowaddr */
625 BUS_SPACE_MAXADDR, /* highaddr */
626 NULL, NULL, /* filter, filterarg */
627 maxsize, /* maxsize */
629 maxsize, /* maxsize */
631 NULL, NULL, /* No locking needed */
632 &sc->aac_fib_dmat)) {
633 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
638 * Create DMA tag for the common structure and allocate it.
640 maxsize = sizeof(struct aac_common);
641 maxsize += sc->aac_max_fibs * sizeof(u_int32_t);
642 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
643 1, 0, /* algnmnt, boundary */
644 (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
645 BUS_SPACE_MAXADDR_32BIT :
646 0x7fffffff, /* lowaddr */
647 BUS_SPACE_MAXADDR, /* highaddr */
648 NULL, NULL, /* filter, filterarg */
649 maxsize, /* maxsize */
651 maxsize, /* maxsegsize */
653 NULL, NULL, /* No locking needed */
654 &sc->aac_common_dmat)) {
655 device_printf(sc->aac_dev,
656 "can't allocate common structure DMA tag\n");
659 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
660 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
661 device_printf(sc->aac_dev, "can't allocate common structure\n");
665 (void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
666 sc->aac_common, maxsize,
667 aac_common_map, sc, 0);
668 bzero(sc->aac_common, maxsize);
670 /* Allocate some FIBs and associated command structs */
671 TAILQ_INIT(&sc->aac_fibmap_tqh);
672 sc->aac_commands = malloc(sc->aac_max_fibs * sizeof(struct aac_command),
673 M_AACRAIDBUF, M_WAITOK|M_ZERO);
674 mtx_lock(&sc->aac_io_lock);
675 while (sc->total_fibs < sc->aac_max_fibs) {
676 if (aac_alloc_commands(sc) != 0)
679 mtx_unlock(&sc->aac_io_lock);
680 if (sc->total_fibs == 0)
687 * Free all of the resources associated with (sc)
689 * Should not be called if the controller is active.
692 aacraid_free(struct aac_softc *sc)
696 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
698 /* remove the control device */
699 if (sc->aac_dev_t != NULL)
700 destroy_dev(sc->aac_dev_t);
702 /* throw away any FIB buffers, discard the FIB DMA tag */
703 aac_free_commands(sc);
704 if (sc->aac_fib_dmat)
705 bus_dma_tag_destroy(sc->aac_fib_dmat);
707 free(sc->aac_commands, M_AACRAIDBUF);
709 /* destroy the common area */
710 if (sc->aac_common) {
711 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
712 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
713 sc->aac_common_dmamap);
715 if (sc->aac_common_dmat)
716 bus_dma_tag_destroy(sc->aac_common_dmat);
718 /* disconnect the interrupt handler */
719 for (i = 0; i < AAC_MAX_MSIX; ++i) {
721 bus_teardown_intr(sc->aac_dev,
722 sc->aac_irq[i], sc->aac_intr[i]);
724 bus_release_resource(sc->aac_dev, SYS_RES_IRQ,
725 sc->aac_irq_rid[i], sc->aac_irq[i]);
730 pci_release_msi(sc->aac_dev);
732 /* destroy data-transfer DMA tag */
733 if (sc->aac_buffer_dmat)
734 bus_dma_tag_destroy(sc->aac_buffer_dmat);
736 /* destroy the parent DMA tag */
737 if (sc->aac_parent_dmat)
738 bus_dma_tag_destroy(sc->aac_parent_dmat);
740 /* release the register window mapping */
741 if (sc->aac_regs_res0 != NULL)
742 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
743 sc->aac_regs_rid0, sc->aac_regs_res0);
744 if (sc->aac_regs_res1 != NULL)
745 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
746 sc->aac_regs_rid1, sc->aac_regs_res1);
750 * Disconnect from the controller completely, in preparation for unload.
753 aacraid_detach(device_t dev)
755 struct aac_softc *sc;
756 struct aac_container *co;
760 sc = device_get_softc(dev);
761 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
763 #if __FreeBSD_version >= 800000
764 callout_drain(&sc->aac_daemontime);
766 untimeout(aac_daemon, (void *)sc, sc->timeout_id);
768 /* Remove the child containers */
769 while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
770 TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
771 free(co, M_AACRAIDBUF);
774 /* Remove the CAM SIMs */
775 while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
776 TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
777 error = device_delete_child(dev, sim->sim_dev);
780 free(sim, M_AACRAIDBUF);
783 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
784 sc->aifflags |= AAC_AIFFLAGS_EXIT;
785 wakeup(sc->aifthread);
786 tsleep(sc->aac_dev, PUSER | PCATCH, "aac_dch", 30 * hz);
789 if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
790 panic("Cannot shutdown AIF thread");
792 if ((error = aacraid_shutdown(dev)))
795 EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
799 mtx_destroy(&sc->aac_io_lock);
805 * Bring the controller down to a dormant state and detach all child devices.
807 * This function is called before detach or system shutdown.
809 * Note that we can assume that the bioq on the controller is empty, as we won't
810 * allow shutdown if any device is open.
813 aacraid_shutdown(device_t dev)
815 struct aac_softc *sc;
817 struct aac_close_command *cc;
819 sc = device_get_softc(dev);
820 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
822 sc->aac_state |= AAC_STATE_SUSPEND;
825 * Send a Container shutdown followed by a HostShutdown FIB to the
826 * controller to convince it that we don't want to talk to it anymore.
827 * We've been closed and all I/O completed already
829 device_printf(sc->aac_dev, "shutting down controller...");
831 mtx_lock(&sc->aac_io_lock);
832 aac_alloc_sync_fib(sc, &fib);
833 cc = (struct aac_close_command *)&fib->data[0];
835 bzero(cc, sizeof(struct aac_close_command));
836 cc->Command = VM_CloseAll;
837 cc->ContainerId = 0xfffffffe;
838 if (aac_sync_fib(sc, ContainerCommand, 0, fib,
839 sizeof(struct aac_close_command)))
844 AAC_ACCESS_DEVREG(sc, AAC_DISABLE_INTERRUPT);
845 aac_release_sync_fib(sc);
846 mtx_unlock(&sc->aac_io_lock);
852 * Bring the controller to a quiescent state, ready for system suspend.
855 aacraid_suspend(device_t dev)
857 struct aac_softc *sc;
859 sc = device_get_softc(dev);
861 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
862 sc->aac_state |= AAC_STATE_SUSPEND;
864 AAC_ACCESS_DEVREG(sc, AAC_DISABLE_INTERRUPT);
869 * Bring the controller back to a state ready for operation.
872 aacraid_resume(device_t dev)
874 struct aac_softc *sc;
876 sc = device_get_softc(dev);
878 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
879 sc->aac_state &= ~AAC_STATE_SUSPEND;
880 AAC_ACCESS_DEVREG(sc, AAC_ENABLE_INTERRUPT);
885 * Interrupt handler for NEW_COMM_TYPE1, NEW_COMM_TYPE2, NEW_COMM_TYPE34 interface.
888 aacraid_new_intr_type1(void *arg)
890 struct aac_msix_ctx *ctx;
891 struct aac_softc *sc;
893 struct aac_command *cm;
895 u_int32_t bellbits, bellbits_shifted, index, handle;
896 int isFastResponse, isAif, noMoreAif, mode;
898 ctx = (struct aac_msix_ctx *)arg;
900 vector_no = ctx->vector_no;
902 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
903 mtx_lock(&sc->aac_io_lock);
905 if (sc->msi_enabled) {
906 mode = AAC_INT_MODE_MSI;
907 if (vector_no == 0) {
908 bellbits = AAC_MEM0_GETREG4(sc, AAC_SRC_ODBR_MSI);
909 if (bellbits & 0x40000)
910 mode |= AAC_INT_MODE_AIF;
911 else if (bellbits & 0x1000)
912 mode |= AAC_INT_MODE_SYNC;
915 mode = AAC_INT_MODE_INTX;
916 bellbits = AAC_MEM0_GETREG4(sc, AAC_SRC_ODBR_R);
917 if (bellbits & AAC_DB_RESPONSE_SENT_NS) {
918 bellbits = AAC_DB_RESPONSE_SENT_NS;
919 AAC_MEM0_SETREG4(sc, AAC_SRC_ODBR_C, bellbits);
921 bellbits_shifted = (bellbits >> AAC_SRC_ODR_SHIFT);
922 AAC_MEM0_SETREG4(sc, AAC_SRC_ODBR_C, bellbits);
923 if (bellbits_shifted & AAC_DB_AIF_PENDING)
924 mode |= AAC_INT_MODE_AIF;
925 else if (bellbits_shifted & AAC_DB_SYNC_COMMAND)
926 mode |= AAC_INT_MODE_SYNC;
928 /* ODR readback, Prep #238630 */
929 AAC_MEM0_GETREG4(sc, AAC_SRC_ODBR_R);
932 if (mode & AAC_INT_MODE_SYNC) {
933 if (sc->aac_sync_cm) {
934 cm = sc->aac_sync_cm;
935 cm->cm_flags |= AAC_CMD_COMPLETED;
936 /* is there a completion handler? */
937 if (cm->cm_complete != NULL) {
940 /* assume that someone is sleeping on this command */
943 sc->flags &= ~AAC_QUEUE_FRZN;
944 sc->aac_sync_cm = NULL;
949 if (mode & AAC_INT_MODE_AIF) {
950 if (mode & AAC_INT_MODE_INTX) {
957 /* handle async. status */
958 index = sc->aac_host_rrq_idx[vector_no];
960 isFastResponse = isAif = noMoreAif = 0;
961 /* remove toggle bit (31) */
962 handle = (sc->aac_common->ac_host_rrq[index] & 0x7fffffff);
963 /* check fast response bit (30) */
964 if (handle & 0x40000000)
966 /* check AIF bit (23) */
967 else if (handle & 0x00800000)
969 handle &= 0x0000ffff;
973 cm = sc->aac_commands + (handle - 1);
975 sc->aac_rrq_outstanding[vector_no]--;
977 noMoreAif = (fib->Header.XferState & AAC_FIBSTATE_NOMOREAIF) ? 1:0;
979 aac_handle_aif(sc, fib);
981 aacraid_release_command(cm);
983 if (isFastResponse) {
984 fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
985 *((u_int32_t *)(fib->data)) = ST_OK;
986 cm->cm_flags |= AAC_CMD_FASTRESP;
989 aac_unmap_command(cm);
990 cm->cm_flags |= AAC_CMD_COMPLETED;
992 /* is there a completion handler? */
993 if (cm->cm_complete != NULL) {
996 /* assume that someone is sleeping on this command */
999 sc->flags &= ~AAC_QUEUE_FRZN;
1002 sc->aac_common->ac_host_rrq[index++] = 0;
1003 if (index == (vector_no + 1) * sc->aac_vector_cap)
1004 index = vector_no * sc->aac_vector_cap;
1005 sc->aac_host_rrq_idx[vector_no] = index;
1007 if ((isAif && !noMoreAif) || sc->aif_pending)
1008 aac_request_aif(sc);
1012 if (mode & AAC_INT_MODE_AIF) {
1013 aac_request_aif(sc);
1014 AAC_ACCESS_DEVREG(sc, AAC_CLEAR_AIF_BIT);
1018 /* see if we can start some more I/O */
1019 if ((sc->flags & AAC_QUEUE_FRZN) == 0)
1020 aacraid_startio(sc);
1021 mtx_unlock(&sc->aac_io_lock);
1025 * Handle notification of one or more FIBs coming from the controller.
1028 aac_command_thread(struct aac_softc *sc)
1032 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1034 mtx_lock(&sc->aac_io_lock);
1035 sc->aifflags = AAC_AIFFLAGS_RUNNING;
1037 while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1040 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1041 retval = msleep(sc->aifthread, &sc->aac_io_lock, PRIBIO,
1042 "aacraid_aifthd", AAC_PERIODIC_INTERVAL * hz);
1045 * First see if any FIBs need to be allocated. This needs
1046 * to be called without the driver lock because contigmalloc
1047 * will grab Giant, and would result in an LOR.
1049 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1050 aac_alloc_commands(sc);
1051 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1052 aacraid_startio(sc);
1056 * While we're here, check to see if any commands are stuck.
1057 * This is pretty low-priority, so it's ok if it doesn't
1060 if (retval == EWOULDBLOCK)
1063 /* Check the hardware printf message buffer */
1064 if (sc->aac_common->ac_printf[0] != 0)
1065 aac_print_printf(sc);
1067 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1068 mtx_unlock(&sc->aac_io_lock);
1069 wakeup(sc->aac_dev);
1071 aac_kthread_exit(0);
1075 * Submit a command to the controller, return when it completes.
1076 * XXX This is very dangerous! If the card has gone out to lunch, we could
1077 * be stuck here forever. At the same time, signals are not caught
1078 * because there is a risk that a signal could wakeup the sleep before
1079 * the card has a chance to complete the command. Since there is no way
1080 * to cancel a command that is in progress, we can't protect against the
1081 * card completing a command late and spamming the command and data
1082 * memory. So, we are held hostage until the command completes.
1085 aacraid_wait_command(struct aac_command *cm)
1087 struct aac_softc *sc;
1091 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1092 mtx_assert(&sc->aac_io_lock, MA_OWNED);
1094 /* Put the command on the ready queue and get things going */
1095 aac_enqueue_ready(cm);
1096 aacraid_startio(sc);
1097 error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacraid_wait", 0);
1102 *Command Buffer Management
1106 * Allocate a command.
1109 aacraid_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1111 struct aac_command *cm;
1113 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1115 if ((cm = aac_dequeue_free(sc)) == NULL) {
1116 if (sc->total_fibs < sc->aac_max_fibs) {
1117 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1118 wakeup(sc->aifthread);
1128 * Release a command back to the freelist.
1131 aacraid_release_command(struct aac_command *cm)
1133 struct aac_event *event;
1134 struct aac_softc *sc;
1137 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1138 mtx_assert(&sc->aac_io_lock, MA_OWNED);
1140 /* (re)initialize the command/FIB */
1141 cm->cm_sgtable = NULL;
1143 cm->cm_complete = NULL;
1145 cm->cm_passthr_dmat = 0;
1146 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1147 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1148 cm->cm_fib->Header.Unused = 0;
1149 cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1152 * These are duplicated in aac_start to cover the case where an
1153 * intermediate stage may have destroyed them. They're left
1154 * initialized here for debugging purposes only.
1156 cm->cm_fib->Header.u.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1157 cm->cm_fib->Header.Handle = 0;
1159 aac_enqueue_free(cm);
1162 * Dequeue all events so that there's no risk of events getting
1165 while ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1166 TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1167 event->ev_callback(sc, event, event->ev_arg);
1172 * Map helper for command/FIB allocation.
1175 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1179 fibphys = (uint64_t *)arg;
1181 *fibphys = segs[0].ds_addr;
1185 * Allocate and initialize commands/FIBs for this adapter.
1188 aac_alloc_commands(struct aac_softc *sc)
1190 struct aac_command *cm;
1191 struct aac_fibmap *fm;
1196 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1197 mtx_assert(&sc->aac_io_lock, MA_OWNED);
1199 if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1202 fm = malloc(sizeof(struct aac_fibmap), M_AACRAIDBUF, M_NOWAIT|M_ZERO);
1206 mtx_unlock(&sc->aac_io_lock);
1207 /* allocate the FIBs in DMAable memory and load them */
1208 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1209 BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1210 device_printf(sc->aac_dev,
1211 "Not enough contiguous memory available.\n");
1212 free(fm, M_AACRAIDBUF);
1213 mtx_lock(&sc->aac_io_lock);
1217 maxsize = sc->aac_max_fib_size + 31;
1218 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE1)
1219 maxsize += sizeof(struct aac_fib_xporthdr);
1220 /* Ignore errors since this doesn't bounce */
1221 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1222 sc->aac_max_fibs_alloc * maxsize,
1223 aac_map_command_helper, &fibphys, 0);
1224 mtx_lock(&sc->aac_io_lock);
1226 /* initialize constant fields in the command structure */
1227 bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * maxsize);
1228 for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1229 cm = sc->aac_commands + sc->total_fibs;
1230 fm->aac_commands = cm;
1232 cm->cm_fib = (struct aac_fib *)
1233 ((u_int8_t *)fm->aac_fibs + i * maxsize);
1234 cm->cm_fibphys = fibphys + i * maxsize;
1235 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE1) {
1236 u_int64_t fibphys_aligned;
1238 (cm->cm_fibphys + sizeof(struct aac_fib_xporthdr) + 31) & ~31;
1239 cm->cm_fib = (struct aac_fib *)
1240 ((u_int8_t *)cm->cm_fib + (fibphys_aligned - cm->cm_fibphys));
1241 cm->cm_fibphys = fibphys_aligned;
1243 u_int64_t fibphys_aligned;
1244 fibphys_aligned = (cm->cm_fibphys + 31) & ~31;
1245 cm->cm_fib = (struct aac_fib *)
1246 ((u_int8_t *)cm->cm_fib + (fibphys_aligned - cm->cm_fibphys));
1247 cm->cm_fibphys = fibphys_aligned;
1249 cm->cm_index = sc->total_fibs;
1251 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1252 &cm->cm_datamap)) != 0)
1254 if (sc->aac_max_fibs <= 1 || sc->aac_max_fibs - sc->total_fibs > 1)
1255 aacraid_release_command(cm);
1260 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1261 fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1265 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1266 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1267 free(fm, M_AACRAIDBUF);
1272 * Free FIBs owned by this adapter.
1275 aac_free_commands(struct aac_softc *sc)
1277 struct aac_fibmap *fm;
1278 struct aac_command *cm;
1281 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1283 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1285 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1287 * We check against total_fibs to handle partially
1290 for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1291 cm = fm->aac_commands + i;
1292 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1294 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1295 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1296 free(fm, M_AACRAIDBUF);
1301 * Command-mapping helper function - populate this command's s/g table.
1304 aacraid_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1306 struct aac_softc *sc;
1307 struct aac_command *cm;
1308 struct aac_fib *fib;
1311 cm = (struct aac_command *)arg;
1314 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "nseg %d", nseg);
1315 mtx_assert(&sc->aac_io_lock, MA_OWNED);
1317 /* copy into the FIB */
1318 if (cm->cm_sgtable != NULL) {
1319 if (fib->Header.Command == RawIo2) {
1320 struct aac_raw_io2 *raw;
1321 struct aac_sge_ieee1212 *sg;
1322 u_int32_t min_size = PAGE_SIZE, cur_size;
1323 int conformable = TRUE;
1325 raw = (struct aac_raw_io2 *)&fib->data[0];
1326 sg = (struct aac_sge_ieee1212 *)cm->cm_sgtable;
1329 for (i = 0; i < nseg; i++) {
1330 cur_size = segs[i].ds_len;
1332 *(bus_addr_t *)&sg[i].addrLow = segs[i].ds_addr;
1333 sg[i].length = cur_size;
1336 raw->sgeFirstSize = cur_size;
1337 } else if (i == 1) {
1338 raw->sgeNominalSize = cur_size;
1339 min_size = cur_size;
1340 } else if ((i+1) < nseg &&
1341 cur_size != raw->sgeNominalSize) {
1342 conformable = FALSE;
1343 if (cur_size < min_size)
1344 min_size = cur_size;
1348 /* not conformable: evaluate required sg elements */
1350 int j, err_found, nseg_new = nseg;
1351 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
1354 for (j = 1; j < nseg - 1; ++j) {
1355 if (sg[j].length % (i*PAGE_SIZE)) {
1359 nseg_new += (sg[j].length / (i*PAGE_SIZE));
1364 if (i>0 && nseg_new<=sc->aac_sg_tablesize &&
1365 !(sc->hint_flags & 4))
1366 nseg = aac_convert_sgraw2(sc,
1367 raw, i, nseg, nseg_new);
1369 raw->flags |= RIO2_SGL_CONFORMANT;
1372 /* update the FIB size for the s/g count */
1373 fib->Header.Size += nseg *
1374 sizeof(struct aac_sge_ieee1212);
1376 } else if (fib->Header.Command == RawIo) {
1377 struct aac_sg_tableraw *sg;
1378 sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1380 for (i = 0; i < nseg; i++) {
1381 sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1382 sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1383 sg->SgEntryRaw[i].Next = 0;
1384 sg->SgEntryRaw[i].Prev = 0;
1385 sg->SgEntryRaw[i].Flags = 0;
1387 /* update the FIB size for the s/g count */
1388 fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1389 } else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1390 struct aac_sg_table *sg;
1391 sg = cm->cm_sgtable;
1393 for (i = 0; i < nseg; i++) {
1394 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1395 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1397 /* update the FIB size for the s/g count */
1398 fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1400 struct aac_sg_table64 *sg;
1401 sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1403 for (i = 0; i < nseg; i++) {
1404 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1405 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1407 /* update the FIB size for the s/g count */
1408 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1412 /* Fix up the address values in the FIB. Use the command array index
1413 * instead of a pointer since these fields are only 32 bits. Shift
1414 * the SenderFibAddress over to make room for the fast response bit
1415 * and for the AIF bit
1417 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1418 cm->cm_fib->Header.u.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1420 /* save a pointer to the command for speedy reverse-lookup */
1421 cm->cm_fib->Header.Handle += cm->cm_index + 1;
1423 if (cm->cm_passthr_dmat == 0) {
1424 if (cm->cm_flags & AAC_CMD_DATAIN)
1425 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1426 BUS_DMASYNC_PREREAD);
1427 if (cm->cm_flags & AAC_CMD_DATAOUT)
1428 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1429 BUS_DMASYNC_PREWRITE);
1432 cm->cm_flags |= AAC_CMD_MAPPED;
1434 if (sc->flags & AAC_FLAGS_SYNC_MODE) {
1436 aacraid_sync_command(sc, AAC_MONKER_SYNCFIB, cm->cm_fibphys, 0, 0, 0, &wait, NULL);
1437 } else if (cm->cm_flags & AAC_CMD_WAIT) {
1438 aacraid_sync_command(sc, AAC_MONKER_SYNCFIB, cm->cm_fibphys, 0, 0, 0, NULL, NULL);
1440 int count = 10000000L;
1441 while (AAC_SEND_COMMAND(sc, cm) != 0) {
1443 aac_unmap_command(cm);
1444 sc->flags |= AAC_QUEUE_FRZN;
1445 aac_requeue_ready(cm);
1447 DELAY(5); /* wait 5 usec. */
1454 aac_convert_sgraw2(struct aac_softc *sc, struct aac_raw_io2 *raw,
1455 int pages, int nseg, int nseg_new)
1457 struct aac_sge_ieee1212 *sge;
1461 sge = malloc(nseg_new * sizeof(struct aac_sge_ieee1212),
1462 M_AACRAIDBUF, M_NOWAIT|M_ZERO);
1466 for (i = 1, pos = 1; i < nseg - 1; ++i) {
1467 for (j = 0; j < raw->sge[i].length / (pages*PAGE_SIZE); ++j) {
1468 addr_low = raw->sge[i].addrLow + j * pages * PAGE_SIZE;
1469 sge[pos].addrLow = addr_low;
1470 sge[pos].addrHigh = raw->sge[i].addrHigh;
1471 if (addr_low < raw->sge[i].addrLow)
1472 sge[pos].addrHigh++;
1473 sge[pos].length = pages * PAGE_SIZE;
1478 sge[pos] = raw->sge[nseg-1];
1479 for (i = 1; i < nseg_new; ++i)
1480 raw->sge[i] = sge[i];
1482 free(sge, M_AACRAIDBUF);
1483 raw->sgeCnt = nseg_new;
1484 raw->flags |= RIO2_SGL_CONFORMANT;
1485 raw->sgeNominalSize = pages * PAGE_SIZE;
1491 * Unmap a command from controller-visible space.
1494 aac_unmap_command(struct aac_command *cm)
1496 struct aac_softc *sc;
1499 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1501 if (!(cm->cm_flags & AAC_CMD_MAPPED))
1504 if (cm->cm_datalen != 0 && cm->cm_passthr_dmat == 0) {
1505 if (cm->cm_flags & AAC_CMD_DATAIN)
1506 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1507 BUS_DMASYNC_POSTREAD);
1508 if (cm->cm_flags & AAC_CMD_DATAOUT)
1509 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1510 BUS_DMASYNC_POSTWRITE);
1512 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1514 cm->cm_flags &= ~AAC_CMD_MAPPED;
1518 * Hardware Interface
1522 * Initialize the adapter.
1525 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1527 struct aac_softc *sc;
1529 sc = (struct aac_softc *)arg;
1530 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1532 sc->aac_common_busaddr = segs[0].ds_addr;
1536 aac_check_firmware(struct aac_softc *sc)
1538 u_int32_t code, major, minor, maxsize;
1539 u_int32_t options = 0, atu_size = 0, status, waitCount;
1542 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1544 /* check if flash update is running */
1545 if (AAC_GET_FWSTATUS(sc) & AAC_FLASH_UPD_PENDING) {
1548 code = AAC_GET_FWSTATUS(sc);
1549 if (time_uptime > (then + AAC_FWUPD_TIMEOUT)) {
1550 device_printf(sc->aac_dev,
1551 "FATAL: controller not coming ready, "
1552 "status %x\n", code);
1555 } while (!(code & AAC_FLASH_UPD_SUCCESS) && !(code & AAC_FLASH_UPD_FAILED));
1557 * Delay 10 seconds. Because right now FW is doing a soft reset,
1558 * do not read scratch pad register at this time
1560 waitCount = 10 * 10000;
1562 DELAY(100); /* delay 100 microseconds */
1568 * Wait for the adapter to come ready.
1572 code = AAC_GET_FWSTATUS(sc);
1573 if (time_uptime > (then + AAC_BOOT_TIMEOUT)) {
1574 device_printf(sc->aac_dev,
1575 "FATAL: controller not coming ready, "
1576 "status %x\n", code);
1579 } while (!(code & AAC_UP_AND_RUNNING) || code == 0xffffffff);
1582 * Retrieve the firmware version numbers. Dell PERC2/QC cards with
1583 * firmware version 1.x are not compatible with this driver.
1585 if (sc->flags & AAC_FLAGS_PERC2QC) {
1586 if (aacraid_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1588 device_printf(sc->aac_dev,
1589 "Error reading firmware version\n");
1593 /* These numbers are stored as ASCII! */
1594 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1595 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1597 device_printf(sc->aac_dev,
1598 "Firmware version %d.%d is not supported.\n",
1604 * Retrieve the capabilities/supported options word so we know what
1605 * work-arounds to enable. Some firmware revs don't support this
1608 if (aacraid_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status, NULL)) {
1609 if (status != AAC_SRB_STS_INVALID_REQUEST) {
1610 device_printf(sc->aac_dev,
1611 "RequestAdapterInfo failed\n");
1615 options = AAC_GET_MAILBOX(sc, 1);
1616 atu_size = AAC_GET_MAILBOX(sc, 2);
1617 sc->supported_options = options;
1619 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1620 (sc->flags & AAC_FLAGS_NO4GB) == 0)
1621 sc->flags |= AAC_FLAGS_4GB_WINDOW;
1622 if (options & AAC_SUPPORTED_NONDASD)
1623 sc->flags |= AAC_FLAGS_ENABLE_CAM;
1624 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1625 && (sizeof(bus_addr_t) > 4)
1626 && (sc->hint_flags & 0x1)) {
1627 device_printf(sc->aac_dev,
1628 "Enabling 64-bit address support\n");
1629 sc->flags |= AAC_FLAGS_SG_64BIT;
1631 if (sc->aac_if.aif_send_command) {
1632 if ((options & AAC_SUPPORTED_NEW_COMM_TYPE3) ||
1633 (options & AAC_SUPPORTED_NEW_COMM_TYPE4))
1634 sc->flags |= AAC_FLAGS_NEW_COMM | AAC_FLAGS_NEW_COMM_TYPE34;
1635 else if (options & AAC_SUPPORTED_NEW_COMM_TYPE1)
1636 sc->flags |= AAC_FLAGS_NEW_COMM | AAC_FLAGS_NEW_COMM_TYPE1;
1637 else if (options & AAC_SUPPORTED_NEW_COMM_TYPE2)
1638 sc->flags |= AAC_FLAGS_NEW_COMM | AAC_FLAGS_NEW_COMM_TYPE2;
1640 if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1641 sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1644 if (!(sc->flags & AAC_FLAGS_NEW_COMM)) {
1645 device_printf(sc->aac_dev, "Communication interface not supported!\n");
1649 if (sc->hint_flags & 2) {
1650 device_printf(sc->aac_dev,
1651 "Sync. mode enforced by driver parameter. This will cause a significant performance decrease!\n");
1652 sc->flags |= AAC_FLAGS_SYNC_MODE;
1653 } else if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE34) {
1654 device_printf(sc->aac_dev,
1655 "Async. mode not supported by current driver, sync. mode enforced.\nPlease update driver to get full performance.\n");
1656 sc->flags |= AAC_FLAGS_SYNC_MODE;
1659 /* Check for broken hardware that does a lower number of commands */
1660 sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1662 /* Remap mem. resource, if required */
1663 if (atu_size > rman_get_size(sc->aac_regs_res0)) {
1664 bus_release_resource(
1665 sc->aac_dev, SYS_RES_MEMORY,
1666 sc->aac_regs_rid0, sc->aac_regs_res0);
1667 sc->aac_regs_res0 = bus_alloc_resource_anywhere(
1668 sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid0,
1669 atu_size, RF_ACTIVE);
1670 if (sc->aac_regs_res0 == NULL) {
1671 sc->aac_regs_res0 = bus_alloc_resource_any(
1672 sc->aac_dev, SYS_RES_MEMORY,
1673 &sc->aac_regs_rid0, RF_ACTIVE);
1674 if (sc->aac_regs_res0 == NULL) {
1675 device_printf(sc->aac_dev,
1676 "couldn't allocate register window\n");
1680 sc->aac_btag0 = rman_get_bustag(sc->aac_regs_res0);
1681 sc->aac_bhandle0 = rman_get_bushandle(sc->aac_regs_res0);
1684 /* Read preferred settings */
1685 sc->aac_max_fib_size = sizeof(struct aac_fib);
1686 sc->aac_max_sectors = 128; /* 64KB */
1687 sc->aac_max_aif = 1;
1688 if (sc->flags & AAC_FLAGS_SG_64BIT)
1689 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1690 - sizeof(struct aac_blockwrite64))
1691 / sizeof(struct aac_sg_entry64);
1693 sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1694 - sizeof(struct aac_blockwrite))
1695 / sizeof(struct aac_sg_entry);
1697 if (!aacraid_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL, NULL)) {
1698 options = AAC_GET_MAILBOX(sc, 1);
1699 sc->aac_max_fib_size = (options & 0xFFFF);
1700 sc->aac_max_sectors = (options >> 16) << 1;
1701 options = AAC_GET_MAILBOX(sc, 2);
1702 sc->aac_sg_tablesize = (options >> 16);
1703 options = AAC_GET_MAILBOX(sc, 3);
1704 sc->aac_max_fibs = ((options >> 16) & 0xFFFF);
1705 if (sc->aac_max_fibs == 0 || sc->aac_hwif != AAC_HWIF_SRCV)
1706 sc->aac_max_fibs = (options & 0xFFFF);
1707 options = AAC_GET_MAILBOX(sc, 4);
1708 sc->aac_max_aif = (options & 0xFFFF);
1709 options = AAC_GET_MAILBOX(sc, 5);
1710 sc->aac_max_msix =(sc->flags & AAC_FLAGS_NEW_COMM_TYPE2) ? options : 0;
1713 maxsize = sc->aac_max_fib_size + 31;
1714 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE1)
1715 maxsize += sizeof(struct aac_fib_xporthdr);
1716 if (maxsize > PAGE_SIZE) {
1717 sc->aac_max_fib_size -= (maxsize - PAGE_SIZE);
1718 maxsize = PAGE_SIZE;
1720 sc->aac_max_fibs_alloc = PAGE_SIZE / maxsize;
1722 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1723 sc->flags |= AAC_FLAGS_RAW_IO;
1724 device_printf(sc->aac_dev, "Enable Raw I/O\n");
1726 if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1727 (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1728 sc->flags |= AAC_FLAGS_LBA_64BIT;
1729 device_printf(sc->aac_dev, "Enable 64-bit array\n");
1732 #ifdef AACRAID_DEBUG
1733 aacraid_get_fw_debug_buffer(sc);
1739 aac_init(struct aac_softc *sc)
1741 struct aac_adapter_init *ip;
1744 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1746 /* reset rrq index */
1747 sc->aac_fibs_pushed_no = 0;
1748 for (i = 0; i < sc->aac_max_msix; i++)
1749 sc->aac_host_rrq_idx[i] = i * sc->aac_vector_cap;
1752 * Fill in the init structure. This tells the adapter about the
1753 * physical location of various important shared data structures.
1755 ip = &sc->aac_common->ac_init;
1756 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1757 if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1758 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1759 sc->flags |= AAC_FLAGS_RAW_IO;
1761 ip->NoOfMSIXVectors = sc->aac_max_msix;
1763 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1764 offsetof(struct aac_common, ac_fibs);
1765 ip->AdapterFibsVirtualAddress = 0;
1766 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1767 ip->AdapterFibAlign = sizeof(struct aac_fib);
1769 ip->PrintfBufferAddress = sc->aac_common_busaddr +
1770 offsetof(struct aac_common, ac_printf);
1771 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1774 * The adapter assumes that pages are 4K in size, except on some
1775 * broken firmware versions that do the page->byte conversion twice,
1776 * therefore 'assuming' that this value is in 16MB units (2^24).
1777 * Round up since the granularity is so high.
1779 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1780 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1781 ip->HostPhysMemPages =
1782 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1784 ip->HostElapsedSeconds = time_uptime; /* reset later if invalid */
1786 ip->InitFlags = AAC_INITFLAGS_NEW_COMM_SUPPORTED;
1787 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE1) {
1788 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_6;
1789 ip->InitFlags |= (AAC_INITFLAGS_NEW_COMM_TYPE1_SUPPORTED |
1790 AAC_INITFLAGS_FAST_JBOD_SUPPORTED);
1791 device_printf(sc->aac_dev, "New comm. interface type1 enabled\n");
1792 } else if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE2) {
1793 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_7;
1794 ip->InitFlags |= (AAC_INITFLAGS_NEW_COMM_TYPE2_SUPPORTED |
1795 AAC_INITFLAGS_FAST_JBOD_SUPPORTED);
1796 device_printf(sc->aac_dev, "New comm. interface type2 enabled\n");
1798 ip->MaxNumAif = sc->aac_max_aif;
1799 ip->HostRRQ_AddrLow =
1800 sc->aac_common_busaddr + offsetof(struct aac_common, ac_host_rrq);
1801 /* always 32-bit address */
1802 ip->HostRRQ_AddrHigh = 0;
1804 if (sc->aac_support_opt2 & AAC_SUPPORTED_POWER_MANAGEMENT) {
1805 ip->InitFlags |= AAC_INITFLAGS_DRIVER_SUPPORTS_PM;
1806 ip->InitFlags |= AAC_INITFLAGS_DRIVER_USES_UTC_TIME;
1807 device_printf(sc->aac_dev, "Power Management enabled\n");
1810 ip->MaxIoCommands = sc->aac_max_fibs;
1811 ip->MaxIoSize = sc->aac_max_sectors << 9;
1812 ip->MaxFibSize = sc->aac_max_fib_size;
1815 * Do controller-type-specific initialisation
1817 AAC_MEM0_SETREG4(sc, AAC_SRC_ODBR_C, ~0);
1820 * Give the init structure to the controller.
1822 if (aacraid_sync_command(sc, AAC_MONKER_INITSTRUCT,
1823 sc->aac_common_busaddr +
1824 offsetof(struct aac_common, ac_init), 0, 0, 0,
1826 device_printf(sc->aac_dev,
1827 "error establishing init structure\n");
1833 * Check configuration issues
1835 if ((error = aac_check_config(sc)) != 0)
1844 aac_define_int_mode(struct aac_softc *sc)
1847 int cap, msi_count, error = 0;
1852 /* max. vectors from AAC_MONKER_GETCOMMPREF */
1853 if (sc->aac_max_msix == 0) {
1854 sc->aac_max_msix = 1;
1855 sc->aac_vector_cap = sc->aac_max_fibs;
1860 msi_count = pci_msix_count(dev);
1861 if (msi_count > AAC_MAX_MSIX)
1862 msi_count = AAC_MAX_MSIX;
1863 if (msi_count > sc->aac_max_msix)
1864 msi_count = sc->aac_max_msix;
1865 if (msi_count == 0 || (error = pci_alloc_msix(dev, &msi_count)) != 0) {
1866 device_printf(dev, "alloc msix failed - msi_count=%d, err=%d; "
1867 "will try MSI\n", msi_count, error);
1868 pci_release_msi(dev);
1870 sc->msi_enabled = TRUE;
1871 device_printf(dev, "using MSI-X interrupts (%u vectors)\n",
1875 if (!sc->msi_enabled) {
1877 if ((error = pci_alloc_msi(dev, &msi_count)) != 0) {
1878 device_printf(dev, "alloc msi failed - err=%d; "
1879 "will use INTx\n", error);
1880 pci_release_msi(dev);
1882 sc->msi_enabled = TRUE;
1883 device_printf(dev, "using MSI interrupts\n");
1887 if (sc->msi_enabled) {
1888 /* now read controller capability from PCI config. space */
1889 cap = aac_find_pci_capability(sc, PCIY_MSIX);
1890 val = (cap != 0 ? pci_read_config(dev, cap + 2, 2) : 0);
1891 if (!(val & AAC_PCI_MSI_ENABLE)) {
1892 pci_release_msi(dev);
1893 sc->msi_enabled = FALSE;
1897 if (!sc->msi_enabled) {
1898 device_printf(dev, "using legacy interrupts\n");
1899 sc->aac_max_msix = 1;
1901 AAC_ACCESS_DEVREG(sc, AAC_ENABLE_MSIX);
1902 if (sc->aac_max_msix > msi_count)
1903 sc->aac_max_msix = msi_count;
1905 sc->aac_vector_cap = sc->aac_max_fibs / sc->aac_max_msix;
1907 fwprintf(sc, HBA_FLAGS_DBG_DEBUG_B, "msi_enabled %d vector_cap %d max_fibs %d max_msix %d",
1908 sc->msi_enabled,sc->aac_vector_cap, sc->aac_max_fibs, sc->aac_max_msix);
1912 aac_find_pci_capability(struct aac_softc *sc, int cap)
1920 status = pci_read_config(dev, PCIR_STATUS, 2);
1921 if (!(status & PCIM_STATUS_CAPPRESENT))
1924 status = pci_read_config(dev, PCIR_HDRTYPE, 1);
1925 switch (status & PCIM_HDRTYPE) {
1931 ptr = PCIR_CAP_PTR_2;
1937 ptr = pci_read_config(dev, ptr, 1);
1941 next = pci_read_config(dev, ptr + PCICAP_NEXTPTR, 1);
1942 val = pci_read_config(dev, ptr + PCICAP_ID, 1);
1952 aac_setup_intr(struct aac_softc *sc)
1954 int i, msi_count, rid;
1955 struct resource *res;
1958 msi_count = sc->aac_max_msix;
1959 rid = (sc->msi_enabled ? 1:0);
1961 for (i = 0; i < msi_count; i++, rid++) {
1962 if ((res = bus_alloc_resource_any(sc->aac_dev,SYS_RES_IRQ, &rid,
1963 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
1964 device_printf(sc->aac_dev,"can't allocate interrupt\n");
1967 sc->aac_irq_rid[i] = rid;
1968 sc->aac_irq[i] = res;
1969 if (aac_bus_setup_intr(sc->aac_dev, res,
1970 INTR_MPSAFE | INTR_TYPE_BIO, NULL,
1971 aacraid_new_intr_type1, &sc->aac_msix[i], &tag)) {
1972 device_printf(sc->aac_dev, "can't set up interrupt\n");
1975 sc->aac_msix[i].vector_no = i;
1976 sc->aac_msix[i].sc = sc;
1977 sc->aac_intr[i] = tag;
1984 aac_check_config(struct aac_softc *sc)
1986 struct aac_fib *fib;
1987 struct aac_cnt_config *ccfg;
1988 struct aac_cf_status_hdr *cf_shdr;
1991 mtx_lock(&sc->aac_io_lock);
1992 aac_alloc_sync_fib(sc, &fib);
1994 ccfg = (struct aac_cnt_config *)&fib->data[0];
1995 bzero(ccfg, sizeof (*ccfg) - CT_PACKET_SIZE);
1996 ccfg->Command = VM_ContainerConfig;
1997 ccfg->CTCommand.command = CT_GET_CONFIG_STATUS;
1998 ccfg->CTCommand.param[CNT_SIZE] = sizeof(struct aac_cf_status_hdr);
2000 rval = aac_sync_fib(sc, ContainerCommand, 0, fib,
2001 sizeof (struct aac_cnt_config));
2002 cf_shdr = (struct aac_cf_status_hdr *)ccfg->CTCommand.data;
2003 if (rval == 0 && ccfg->Command == ST_OK &&
2004 ccfg->CTCommand.param[0] == CT_OK) {
2005 if (cf_shdr->action <= CFACT_PAUSE) {
2006 bzero(ccfg, sizeof (*ccfg) - CT_PACKET_SIZE);
2007 ccfg->Command = VM_ContainerConfig;
2008 ccfg->CTCommand.command = CT_COMMIT_CONFIG;
2010 rval = aac_sync_fib(sc, ContainerCommand, 0, fib,
2011 sizeof (struct aac_cnt_config));
2012 if (rval == 0 && ccfg->Command == ST_OK &&
2013 ccfg->CTCommand.param[0] == CT_OK) {
2014 /* successful completion */
2017 /* auto commit aborted due to error(s) */
2021 /* auto commit aborted due to adapter indicating
2022 config. issues too dangerous to auto commit */
2030 aac_release_sync_fib(sc);
2031 mtx_unlock(&sc->aac_io_lock);
2036 * Send a synchronous command to the controller and wait for a result.
2037 * Indicate if the controller completed the command with an error status.
2040 aacraid_sync_command(struct aac_softc *sc, u_int32_t command,
2041 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2042 u_int32_t *sp, u_int32_t *r1)
2047 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2049 /* populate the mailbox */
2050 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2052 /* ensure the sync command doorbell flag is cleared */
2053 if (!sc->msi_enabled)
2054 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2056 /* then set it to signal the adapter */
2057 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2059 if ((command != AAC_MONKER_SYNCFIB) || (sp == NULL) || (*sp != 0)) {
2060 /* spin waiting for the command to complete */
2063 if (time_uptime > (then + AAC_SYNC_TIMEOUT)) {
2064 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2067 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2069 /* clear the completion flag */
2070 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2072 /* get the command status */
2073 status = AAC_GET_MAILBOX(sc, 0);
2077 /* return parameter */
2079 *r1 = AAC_GET_MAILBOX(sc, 1);
2081 if (status != AAC_SRB_STS_SUCCESS)
2088 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2089 struct aac_fib *fib, u_int16_t datasize)
2091 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2092 mtx_assert(&sc->aac_io_lock, MA_OWNED);
2094 if (datasize > AAC_FIB_DATASIZE)
2098 * Set up the sync FIB
2100 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2101 AAC_FIBSTATE_INITIALISED |
2103 fib->Header.XferState |= xferstate;
2104 fib->Header.Command = command;
2105 fib->Header.StructType = AAC_FIBTYPE_TFIB;
2106 fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2107 fib->Header.SenderSize = sizeof(struct aac_fib);
2108 fib->Header.SenderFibAddress = 0; /* Not needed */
2109 fib->Header.u.ReceiverFibAddress = sc->aac_common_busaddr +
2110 offsetof(struct aac_common, ac_sync_fib);
2113 * Give the FIB to the controller, wait for a response.
2115 if (aacraid_sync_command(sc, AAC_MONKER_SYNCFIB,
2116 fib->Header.u.ReceiverFibAddress, 0, 0, 0, NULL, NULL)) {
2117 fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2125 * Check for commands that have been outstanding for a suspiciously long time,
2126 * and complain about them.
2129 aac_timeout(struct aac_softc *sc)
2131 struct aac_command *cm;
2135 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2137 * Traverse the busy command list, bitch about late commands once
2141 deadline = time_uptime - AAC_CMD_TIMEOUT;
2142 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2143 if (cm->cm_timestamp < deadline) {
2144 device_printf(sc->aac_dev,
2145 "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2146 cm, (int)(time_uptime-cm->cm_timestamp));
2147 AAC_PRINT_FIB(sc, cm->cm_fib);
2153 aac_reset_adapter(sc);
2154 aacraid_print_queues(sc);
2158 * Interface Function Vectors
2162 * Read the current firmware status word.
2165 aac_src_get_fwstatus(struct aac_softc *sc)
2167 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2169 return(AAC_MEM0_GETREG4(sc, AAC_SRC_OMR));
2173 * Notify the controller of a change in a given queue
2176 aac_src_qnotify(struct aac_softc *sc, int qbit)
2178 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2180 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, qbit << AAC_SRC_IDR_SHIFT);
2184 * Get the interrupt reason bits
2187 aac_src_get_istatus(struct aac_softc *sc)
2191 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2193 if (sc->msi_enabled) {
2194 val = AAC_MEM0_GETREG4(sc, AAC_SRC_ODBR_MSI);
2195 if (val & AAC_MSI_SYNC_STATUS)
2196 val = AAC_DB_SYNC_COMMAND;
2200 val = AAC_MEM0_GETREG4(sc, AAC_SRC_ODBR_R) >> AAC_SRC_ODR_SHIFT;
2206 * Clear some interrupt reason bits
2209 aac_src_clear_istatus(struct aac_softc *sc, int mask)
2211 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2213 if (sc->msi_enabled) {
2214 if (mask == AAC_DB_SYNC_COMMAND)
2215 AAC_ACCESS_DEVREG(sc, AAC_CLEAR_SYNC_BIT);
2217 AAC_MEM0_SETREG4(sc, AAC_SRC_ODBR_C, mask << AAC_SRC_ODR_SHIFT);
2222 * Populate the mailbox and set the command word
2225 aac_src_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2226 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2228 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2230 AAC_MEM0_SETREG4(sc, AAC_SRC_MAILBOX, command);
2231 AAC_MEM0_SETREG4(sc, AAC_SRC_MAILBOX + 4, arg0);
2232 AAC_MEM0_SETREG4(sc, AAC_SRC_MAILBOX + 8, arg1);
2233 AAC_MEM0_SETREG4(sc, AAC_SRC_MAILBOX + 12, arg2);
2234 AAC_MEM0_SETREG4(sc, AAC_SRC_MAILBOX + 16, arg3);
2238 aac_srcv_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2239 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2241 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2243 AAC_MEM0_SETREG4(sc, AAC_SRCV_MAILBOX, command);
2244 AAC_MEM0_SETREG4(sc, AAC_SRCV_MAILBOX + 4, arg0);
2245 AAC_MEM0_SETREG4(sc, AAC_SRCV_MAILBOX + 8, arg1);
2246 AAC_MEM0_SETREG4(sc, AAC_SRCV_MAILBOX + 12, arg2);
2247 AAC_MEM0_SETREG4(sc, AAC_SRCV_MAILBOX + 16, arg3);
2251 * Fetch the immediate command status word
2254 aac_src_get_mailbox(struct aac_softc *sc, int mb)
2256 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2258 return(AAC_MEM0_GETREG4(sc, AAC_SRC_MAILBOX + (mb * 4)));
2262 aac_srcv_get_mailbox(struct aac_softc *sc, int mb)
2264 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2266 return(AAC_MEM0_GETREG4(sc, AAC_SRCV_MAILBOX + (mb * 4)));
2270 * Set/clear interrupt masks
2273 aac_src_access_devreg(struct aac_softc *sc, int mode)
2277 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2280 case AAC_ENABLE_INTERRUPT:
2281 AAC_MEM0_SETREG4(sc, AAC_SRC_OIMR,
2282 (sc->msi_enabled ? AAC_INT_ENABLE_TYPE1_MSIX :
2283 AAC_INT_ENABLE_TYPE1_INTX));
2286 case AAC_DISABLE_INTERRUPT:
2287 AAC_MEM0_SETREG4(sc, AAC_SRC_OIMR, AAC_INT_DISABLE_ALL);
2290 case AAC_ENABLE_MSIX:
2292 val = AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2294 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, val);
2295 AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2297 val = PMC_ALL_INTERRUPT_BITS;
2298 AAC_MEM0_SETREG4(sc, AAC_SRC_IOAR, val);
2299 val = AAC_MEM0_GETREG4(sc, AAC_SRC_OIMR);
2300 AAC_MEM0_SETREG4(sc, AAC_SRC_OIMR,
2301 val & (~(PMC_GLOBAL_INT_BIT2 | PMC_GLOBAL_INT_BIT0)));
2304 case AAC_DISABLE_MSIX:
2306 val = AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2308 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, val);
2309 AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2312 case AAC_CLEAR_AIF_BIT:
2314 val = AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2316 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, val);
2317 AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2320 case AAC_CLEAR_SYNC_BIT:
2322 val = AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2324 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, val);
2325 AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2328 case AAC_ENABLE_INTX:
2330 val = AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2332 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, val);
2333 AAC_MEM0_GETREG4(sc, AAC_SRC_IDBR);
2335 val = PMC_ALL_INTERRUPT_BITS;
2336 AAC_MEM0_SETREG4(sc, AAC_SRC_IOAR, val);
2337 val = AAC_MEM0_GETREG4(sc, AAC_SRC_OIMR);
2338 AAC_MEM0_SETREG4(sc, AAC_SRC_OIMR,
2339 val & (~(PMC_GLOBAL_INT_BIT2)));
2348 * New comm. interface: Send command functions
2351 aac_src_send_command(struct aac_softc *sc, struct aac_command *cm)
2353 struct aac_fib_xporthdr *pFibX;
2354 u_int32_t fibsize, high_addr;
2357 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm. type1)");
2359 if (sc->msi_enabled && cm->cm_fib->Header.Command != AifRequest &&
2360 sc->aac_max_msix > 1) {
2361 u_int16_t vector_no, first_choice = 0xffff;
2363 vector_no = sc->aac_fibs_pushed_no % sc->aac_max_msix;
2366 if (vector_no == sc->aac_max_msix)
2368 if (sc->aac_rrq_outstanding[vector_no] <
2371 if (0xffff == first_choice)
2372 first_choice = vector_no;
2373 else if (vector_no == first_choice)
2376 if (vector_no == first_choice)
2378 sc->aac_rrq_outstanding[vector_no]++;
2379 if (sc->aac_fibs_pushed_no == 0xffffffff)
2380 sc->aac_fibs_pushed_no = 0;
2382 sc->aac_fibs_pushed_no++;
2384 cm->cm_fib->Header.Handle += (vector_no << 16);
2387 if (sc->flags & AAC_FLAGS_NEW_COMM_TYPE2) {
2388 /* Calculate the amount to the fibsize bits */
2389 fibsize = (cm->cm_fib->Header.Size + 127) / 128 - 1;
2390 /* Fill new FIB header */
2391 address = cm->cm_fibphys;
2392 high_addr = (u_int32_t)(address >> 32);
2393 if (high_addr == 0L) {
2394 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB2;
2395 cm->cm_fib->Header.u.TimeStamp = 0L;
2397 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB2_64;
2398 cm->cm_fib->Header.u.SenderFibAddressHigh = high_addr;
2400 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)address;
2402 /* Calculate the amount to the fibsize bits */
2403 fibsize = (sizeof(struct aac_fib_xporthdr) +
2404 cm->cm_fib->Header.Size + 127) / 128 - 1;
2405 /* Fill XPORT header */
2406 pFibX = (struct aac_fib_xporthdr *)
2407 ((unsigned char *)cm->cm_fib - sizeof(struct aac_fib_xporthdr));
2408 pFibX->Handle = cm->cm_fib->Header.Handle;
2409 pFibX->HostAddress = cm->cm_fibphys;
2410 pFibX->Size = cm->cm_fib->Header.Size;
2411 address = cm->cm_fibphys - sizeof(struct aac_fib_xporthdr);
2412 high_addr = (u_int32_t)(address >> 32);
2417 aac_enqueue_busy(cm);
2419 AAC_MEM0_SETREG4(sc, AAC_SRC_IQUE64_H, high_addr);
2420 AAC_MEM0_SETREG4(sc, AAC_SRC_IQUE64_L, (u_int32_t)address + fibsize);
2422 AAC_MEM0_SETREG4(sc, AAC_SRC_IQUE32, (u_int32_t)address + fibsize);
2428 * New comm. interface: get, set outbound queue index
2431 aac_src_get_outb_queue(struct aac_softc *sc)
2433 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2439 aac_src_set_outb_queue(struct aac_softc *sc, int index)
2441 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2445 * Debugging and Diagnostics
2449 * Print some information about the controller.
2452 aac_describe_controller(struct aac_softc *sc)
2454 struct aac_fib *fib;
2455 struct aac_adapter_info *info;
2456 char *adapter_type = "Adaptec RAID controller";
2458 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2460 mtx_lock(&sc->aac_io_lock);
2461 aac_alloc_sync_fib(sc, &fib);
2463 if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2465 if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2466 device_printf(sc->aac_dev, "RequestSupplementAdapterInfo failed\n");
2468 struct aac_supplement_adapter_info *supp_info;
2470 supp_info = ((struct aac_supplement_adapter_info *)&fib->data[0]);
2471 adapter_type = (char *)supp_info->AdapterTypeText;
2472 sc->aac_feature_bits = supp_info->FeatureBits;
2473 sc->aac_support_opt2 = supp_info->SupportedOptions2;
2476 device_printf(sc->aac_dev, "%s, aacraid driver %d.%d.%d-%d\n",
2478 AAC_DRIVER_MAJOR_VERSION, AAC_DRIVER_MINOR_VERSION,
2479 AAC_DRIVER_BUGFIX_LEVEL, AAC_DRIVER_BUILD);
2482 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2483 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2484 aac_release_sync_fib(sc);
2485 mtx_unlock(&sc->aac_io_lock);
2489 /* save the kernel revision structure for later use */
2490 info = (struct aac_adapter_info *)&fib->data[0];
2491 sc->aac_revision = info->KernelRevision;
2494 device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2495 "(%dMB cache, %dMB execution), %s\n",
2496 aac_describe_code(aac_cpu_variant, info->CpuVariant),
2497 info->ClockSpeed, info->TotalMem / (1024 * 1024),
2498 info->BufferMem / (1024 * 1024),
2499 info->ExecutionMem / (1024 * 1024),
2500 aac_describe_code(aac_battery_platform,
2501 info->batteryPlatform));
2503 device_printf(sc->aac_dev,
2504 "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2505 info->KernelRevision.external.comp.major,
2506 info->KernelRevision.external.comp.minor,
2507 info->KernelRevision.external.comp.dash,
2508 info->KernelRevision.buildNumber,
2509 (u_int32_t)(info->SerialNumber & 0xffffff));
2511 device_printf(sc->aac_dev, "Supported Options=%b\n",
2512 sc->supported_options,
2535 aac_release_sync_fib(sc);
2536 mtx_unlock(&sc->aac_io_lock);
2540 * Look up a text description of a numeric error code and return a pointer to
2544 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2548 for (i = 0; table[i].string != NULL; i++)
2549 if (table[i].code == code)
2550 return(table[i].string);
2551 return(table[i + 1].string);
2555 * Management Interface
2559 aac_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2561 struct aac_softc *sc;
2564 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2565 #if __FreeBSD_version >= 702000
2566 device_busy(sc->aac_dev);
2567 devfs_set_cdevpriv(sc, aac_cdevpriv_dtor);
2573 aac_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
2575 union aac_statrequest *as;
2576 struct aac_softc *sc;
2579 as = (union aac_statrequest *)arg;
2581 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2585 switch (as->as_item) {
2589 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2590 sizeof(struct aac_qstat));
2598 case FSACTL_SENDFIB:
2599 case FSACTL_SEND_LARGE_FIB:
2600 arg = *(caddr_t*)arg;
2601 case FSACTL_LNX_SENDFIB:
2602 case FSACTL_LNX_SEND_LARGE_FIB:
2603 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2604 error = aac_ioctl_sendfib(sc, arg);
2606 case FSACTL_SEND_RAW_SRB:
2607 arg = *(caddr_t*)arg;
2608 case FSACTL_LNX_SEND_RAW_SRB:
2609 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2610 error = aac_ioctl_send_raw_srb(sc, arg);
2612 case FSACTL_AIF_THREAD:
2613 case FSACTL_LNX_AIF_THREAD:
2614 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2617 case FSACTL_OPEN_GET_ADAPTER_FIB:
2618 arg = *(caddr_t*)arg;
2619 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2620 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2621 error = aac_open_aif(sc, arg);
2623 case FSACTL_GET_NEXT_ADAPTER_FIB:
2624 arg = *(caddr_t*)arg;
2625 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2626 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2627 error = aac_getnext_aif(sc, arg);
2629 case FSACTL_CLOSE_GET_ADAPTER_FIB:
2630 arg = *(caddr_t*)arg;
2631 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2632 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2633 error = aac_close_aif(sc, arg);
2635 case FSACTL_MINIPORT_REV_CHECK:
2636 arg = *(caddr_t*)arg;
2637 case FSACTL_LNX_MINIPORT_REV_CHECK:
2638 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
2639 error = aac_rev_check(sc, arg);
2641 case FSACTL_QUERY_DISK:
2642 arg = *(caddr_t*)arg;
2643 case FSACTL_LNX_QUERY_DISK:
2644 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
2645 error = aac_query_disk(sc, arg);
2647 case FSACTL_DELETE_DISK:
2648 case FSACTL_LNX_DELETE_DISK:
2650 * We don't trust the underland to tell us when to delete a
2651 * container, rather we rely on an AIF coming from the
2656 case FSACTL_GET_PCI_INFO:
2657 arg = *(caddr_t*)arg;
2658 case FSACTL_LNX_GET_PCI_INFO:
2659 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
2660 error = aac_get_pci_info(sc, arg);
2662 case FSACTL_GET_FEATURES:
2663 arg = *(caddr_t*)arg;
2664 case FSACTL_LNX_GET_FEATURES:
2665 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
2666 error = aac_supported_features(sc, arg);
2669 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
2677 aac_poll(struct cdev *dev, int poll_events, struct thread *td)
2679 struct aac_softc *sc;
2680 struct aac_fib_context *ctx;
2686 mtx_lock(&sc->aac_io_lock);
2687 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2688 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
2689 if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap) {
2690 revents |= poll_events & (POLLIN | POLLRDNORM);
2695 mtx_unlock(&sc->aac_io_lock);
2698 if (poll_events & (POLLIN | POLLRDNORM))
2699 selrecord(td, &sc->rcv_select);
2706 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
2709 switch (event->ev_type) {
2710 case AAC_EVENT_CMFREE:
2711 mtx_assert(&sc->aac_io_lock, MA_OWNED);
2712 if (aacraid_alloc_command(sc, (struct aac_command **)arg)) {
2713 aacraid_add_event(sc, event);
2716 free(event, M_AACRAIDBUF);
2725 * Send a FIB supplied from userspace
2728 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2730 struct aac_command *cm;
2733 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2740 mtx_lock(&sc->aac_io_lock);
2741 if (aacraid_alloc_command(sc, &cm)) {
2742 struct aac_event *event;
2744 event = malloc(sizeof(struct aac_event), M_AACRAIDBUF,
2746 if (event == NULL) {
2748 mtx_unlock(&sc->aac_io_lock);
2751 event->ev_type = AAC_EVENT_CMFREE;
2752 event->ev_callback = aac_ioctl_event;
2753 event->ev_arg = &cm;
2754 aacraid_add_event(sc, event);
2755 msleep(cm, &sc->aac_io_lock, 0, "aacraid_ctlsfib", 0);
2757 mtx_unlock(&sc->aac_io_lock);
2760 * Fetch the FIB header, then re-copy to get data as well.
2762 if ((error = copyin(ufib, cm->cm_fib,
2763 sizeof(struct aac_fib_header))) != 0)
2765 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2766 if (size > sc->aac_max_fib_size) {
2767 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
2768 size, sc->aac_max_fib_size);
2769 size = sc->aac_max_fib_size;
2771 if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2773 cm->cm_fib->Header.Size = size;
2774 cm->cm_timestamp = time_uptime;
2778 * Pass the FIB to the controller, wait for it to complete.
2780 mtx_lock(&sc->aac_io_lock);
2781 error = aacraid_wait_command(cm);
2782 mtx_unlock(&sc->aac_io_lock);
2784 device_printf(sc->aac_dev,
2785 "aacraid_wait_command return %d\n", error);
2790 * Copy the FIB and data back out to the caller.
2792 size = cm->cm_fib->Header.Size;
2793 if (size > sc->aac_max_fib_size) {
2794 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
2795 size, sc->aac_max_fib_size);
2796 size = sc->aac_max_fib_size;
2798 error = copyout(cm->cm_fib, ufib, size);
2802 mtx_lock(&sc->aac_io_lock);
2803 aacraid_release_command(cm);
2804 mtx_unlock(&sc->aac_io_lock);
2810 * Send a passthrough FIB supplied from userspace
2813 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
2815 struct aac_command *cm;
2816 struct aac_fib *fib;
2817 struct aac_srb *srbcmd;
2818 struct aac_srb *user_srb = (struct aac_srb *)arg;
2820 int error, transfer_data = 0;
2821 bus_dmamap_t orig_map = 0;
2822 u_int32_t fibsize = 0;
2823 u_int64_t srb_sg_address;
2824 u_int32_t srb_sg_bytecount;
2826 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2830 mtx_lock(&sc->aac_io_lock);
2831 if (aacraid_alloc_command(sc, &cm)) {
2832 struct aac_event *event;
2834 event = malloc(sizeof(struct aac_event), M_AACRAIDBUF,
2836 if (event == NULL) {
2838 mtx_unlock(&sc->aac_io_lock);
2841 event->ev_type = AAC_EVENT_CMFREE;
2842 event->ev_callback = aac_ioctl_event;
2843 event->ev_arg = &cm;
2844 aacraid_add_event(sc, event);
2845 msleep(cm, &sc->aac_io_lock, 0, "aacraid_ctlsraw", 0);
2847 mtx_unlock(&sc->aac_io_lock);
2850 /* save original dma map */
2851 orig_map = cm->cm_datamap;
2854 srbcmd = (struct aac_srb *)fib->data;
2855 if ((error = copyin((void *)&user_srb->data_len, &fibsize,
2856 sizeof (u_int32_t)) != 0))
2858 if (fibsize > (sc->aac_max_fib_size-sizeof(struct aac_fib_header))) {
2862 if ((error = copyin((void *)user_srb, srbcmd, fibsize) != 0))
2865 srbcmd->function = 0; /* SRBF_ExecuteScsi */
2866 srbcmd->retry_limit = 0; /* obsolete */
2868 /* only one sg element from userspace supported */
2869 if (srbcmd->sg_map.SgCount > 1) {
2874 if (fibsize == (sizeof(struct aac_srb) +
2875 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry))) {
2876 struct aac_sg_entry *sgp = srbcmd->sg_map.SgEntry;
2877 struct aac_sg_entry sg;
2879 if ((error = copyin(sgp, &sg, sizeof(sg))) != 0)
2882 srb_sg_bytecount = sg.SgByteCount;
2883 srb_sg_address = (u_int64_t)sg.SgAddress;
2884 } else if (fibsize == (sizeof(struct aac_srb) +
2885 srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry64))) {
2887 struct aac_sg_entry64 *sgp =
2888 (struct aac_sg_entry64 *)srbcmd->sg_map.SgEntry;
2889 struct aac_sg_entry64 sg;
2891 if ((error = copyin(sgp, &sg, sizeof(sg))) != 0)
2894 srb_sg_bytecount = sg.SgByteCount;
2895 srb_sg_address = sg.SgAddress;
2896 if (srb_sg_address > 0xffffffffull &&
2897 !(sc->flags & AAC_FLAGS_SG_64BIT))
2907 user_reply = (char *)arg + fibsize;
2908 srbcmd->data_len = srb_sg_bytecount;
2909 if (srbcmd->sg_map.SgCount == 1)
2912 if (transfer_data) {
2914 * Create DMA tag for the passthr. data buffer and allocate it.
2916 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */
2917 1, 0, /* algnmnt, boundary */
2918 (sc->flags & AAC_FLAGS_SG_64BIT) ?
2919 BUS_SPACE_MAXADDR_32BIT :
2920 0x7fffffff, /* lowaddr */
2921 BUS_SPACE_MAXADDR, /* highaddr */
2922 NULL, NULL, /* filter, filterarg */
2923 srb_sg_bytecount, /* size */
2924 sc->aac_sg_tablesize, /* nsegments */
2925 srb_sg_bytecount, /* maxsegsize */
2927 NULL, NULL, /* No locking needed */
2928 &cm->cm_passthr_dmat)) {
2932 if (bus_dmamem_alloc(cm->cm_passthr_dmat, (void **)&cm->cm_data,
2933 BUS_DMA_NOWAIT, &cm->cm_datamap)) {
2937 /* fill some cm variables */
2938 cm->cm_datalen = srb_sg_bytecount;
2939 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN)
2940 cm->cm_flags |= AAC_CMD_DATAIN;
2941 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_OUT)
2942 cm->cm_flags |= AAC_CMD_DATAOUT;
2944 if (srbcmd->flags & AAC_SRB_FLAGS_DATA_OUT) {
2945 if ((error = copyin((void *)(uintptr_t)srb_sg_address,
2946 cm->cm_data, cm->cm_datalen)) != 0)
2948 /* sync required for bus_dmamem_alloc() alloc. mem.? */
2949 bus_dmamap_sync(cm->cm_passthr_dmat, cm->cm_datamap,
2950 BUS_DMASYNC_PREWRITE);
2955 fib->Header.Size = sizeof(struct aac_fib_header) +
2956 sizeof(struct aac_srb);
2957 fib->Header.XferState =
2958 AAC_FIBSTATE_HOSTOWNED |
2959 AAC_FIBSTATE_INITIALISED |
2960 AAC_FIBSTATE_EMPTY |
2961 AAC_FIBSTATE_FROMHOST |
2962 AAC_FIBSTATE_REXPECTED |
2966 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
2967 ScsiPortCommandU64 : ScsiPortCommand;
2968 cm->cm_sgtable = (struct aac_sg_table *)&srbcmd->sg_map;
2971 if (transfer_data) {
2972 bus_dmamap_load(cm->cm_passthr_dmat,
2973 cm->cm_datamap, cm->cm_data,
2975 aacraid_map_command_sg, cm, 0);
2977 aacraid_map_command_sg(cm, NULL, 0, 0);
2980 /* wait for completion */
2981 mtx_lock(&sc->aac_io_lock);
2982 while (!(cm->cm_flags & AAC_CMD_COMPLETED))
2983 msleep(cm, &sc->aac_io_lock, 0, "aacraid_ctlsrw2", 0);
2984 mtx_unlock(&sc->aac_io_lock);
2987 if (transfer_data && (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN)) {
2988 if ((error = copyout(cm->cm_data,
2989 (void *)(uintptr_t)srb_sg_address,
2990 cm->cm_datalen)) != 0)
2992 /* sync required for bus_dmamem_alloc() allocated mem.? */
2993 bus_dmamap_sync(cm->cm_passthr_dmat, cm->cm_datamap,
2994 BUS_DMASYNC_POSTREAD);
2998 error = copyout(fib->data, user_reply, sizeof(struct aac_srb_response));
3001 if (cm && cm->cm_data) {
3003 bus_dmamap_unload(cm->cm_passthr_dmat, cm->cm_datamap);
3004 bus_dmamem_free(cm->cm_passthr_dmat, cm->cm_data, cm->cm_datamap);
3005 cm->cm_datamap = orig_map;
3007 if (cm && cm->cm_passthr_dmat)
3008 bus_dma_tag_destroy(cm->cm_passthr_dmat);
3010 mtx_lock(&sc->aac_io_lock);
3011 aacraid_release_command(cm);
3012 mtx_unlock(&sc->aac_io_lock);
3018 * Request an AIF from the controller (new comm. type1)
3021 aac_request_aif(struct aac_softc *sc)
3023 struct aac_command *cm;
3024 struct aac_fib *fib;
3026 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3028 if (aacraid_alloc_command(sc, &cm)) {
3029 sc->aif_pending = 1;
3032 sc->aif_pending = 0;
3036 fib->Header.Size = sizeof(struct aac_fib);
3037 fib->Header.XferState =
3038 AAC_FIBSTATE_HOSTOWNED |
3039 AAC_FIBSTATE_INITIALISED |
3040 AAC_FIBSTATE_EMPTY |
3041 AAC_FIBSTATE_FROMHOST |
3042 AAC_FIBSTATE_REXPECTED |
3045 /* set AIF marker */
3046 fib->Header.Handle = 0x00800000;
3047 fib->Header.Command = AifRequest;
3048 ((struct aac_aif_command *)fib->data)->command = AifReqEvent;
3050 aacraid_map_command_sg(cm, NULL, 0, 0);
3054 #if __FreeBSD_version >= 702000
3056 * cdevpriv interface private destructor.
3059 aac_cdevpriv_dtor(void *arg)
3061 struct aac_softc *sc;
3064 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3066 device_unbusy(sc->aac_dev);
3071 aac_close(struct cdev *dev, int flags, int fmt, struct thread *td)
3073 struct aac_softc *sc;
3076 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3082 * Handle an AIF sent to us by the controller; queue it for later reference.
3083 * If the queue fills up, then drop the older entries.
3086 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3088 struct aac_aif_command *aif;
3089 struct aac_container *co, *co_next;
3090 struct aac_fib_context *ctx;
3091 struct aac_fib *sync_fib;
3092 struct aac_mntinforesp mir;
3093 int next, current, found;
3094 int count = 0, changed = 0, i = 0;
3095 u_int32_t channel, uid;
3097 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3099 aif = (struct aac_aif_command*)&fib->data[0];
3100 aacraid_print_aif(sc, aif);
3102 /* Is it an event that we should care about? */
3103 switch (aif->command) {
3104 case AifCmdEventNotify:
3105 switch (aif->data.EN.type) {
3106 case AifEnAddContainer:
3107 case AifEnDeleteContainer:
3109 * A container was added or deleted, but the message
3110 * doesn't tell us anything else! Re-enumerate the
3111 * containers and sort things out.
3113 aac_alloc_sync_fib(sc, &sync_fib);
3116 * Ask the controller for its containers one at
3118 * XXX What if the controller's list changes
3119 * midway through this enumaration?
3120 * XXX This should be done async.
3122 if (aac_get_container_info(sc, sync_fib, i,
3126 count = mir.MntRespCount;
3128 * Check the container against our list.
3129 * co->co_found was already set to 0 in a
3132 if ((mir.Status == ST_OK) &&
3133 (mir.MntTable[0].VolType != CT_NONE)) {
3136 &sc->aac_container_tqh,
3138 if (co->co_mntobj.ObjectId ==
3139 mir.MntTable[0].ObjectId) {
3146 * If the container matched, continue
3155 * This is a new container. Do all the
3156 * appropriate things to set it up.
3158 aac_add_container(sc, &mir, 1, uid);
3162 } while ((i < count) && (i < AAC_MAX_CONTAINERS));
3163 aac_release_sync_fib(sc);
3166 * Go through our list of containers and see which ones
3167 * were not marked 'found'. Since the controller didn't
3168 * list them they must have been deleted. Do the
3169 * appropriate steps to destroy the device. Also reset
3170 * the co->co_found field.
3172 co = TAILQ_FIRST(&sc->aac_container_tqh);
3173 while (co != NULL) {
3174 if (co->co_found == 0) {
3175 co_next = TAILQ_NEXT(co, co_link);
3176 TAILQ_REMOVE(&sc->aac_container_tqh, co,
3178 free(co, M_AACRAIDBUF);
3183 co = TAILQ_NEXT(co, co_link);
3187 /* Attach the newly created containers */
3189 if (sc->cam_rescan_cb != NULL)
3190 sc->cam_rescan_cb(sc, 0,
3191 AAC_CAM_TARGET_WILDCARD);
3196 case AifEnEnclosureManagement:
3197 switch (aif->data.EN.data.EEE.eventType) {
3198 case AIF_EM_DRIVE_INSERTION:
3199 case AIF_EM_DRIVE_REMOVAL:
3200 channel = aif->data.EN.data.EEE.unitID;
3201 if (sc->cam_rescan_cb != NULL)
3202 sc->cam_rescan_cb(sc,
3203 ((channel>>24) & 0xF) + 1,
3204 (channel & 0xFFFF));
3210 case AifEnDeleteJBOD:
3211 case AifRawDeviceRemove:
3212 channel = aif->data.EN.data.ECE.container;
3213 if (sc->cam_rescan_cb != NULL)
3214 sc->cam_rescan_cb(sc, ((channel>>24) & 0xF) + 1,
3215 AAC_CAM_TARGET_WILDCARD);
3226 /* Copy the AIF data to the AIF queue for ioctl retrieval */
3227 current = sc->aifq_idx;
3228 next = (current + 1) % AAC_AIFQ_LENGTH;
3230 sc->aifq_filled = 1;
3231 bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3232 /* modify AIF contexts */
3233 if (sc->aifq_filled) {
3234 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3235 if (next == ctx->ctx_idx)
3237 else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3238 ctx->ctx_idx = next;
3241 sc->aifq_idx = next;
3242 /* On the off chance that someone is sleeping for an aif... */
3243 if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3244 wakeup(sc->aac_aifq);
3245 /* Wakeup any poll()ers */
3246 selwakeuppri(&sc->rcv_select, PRIBIO);
3252 * Return the Revision of the driver to userspace and check to see if the
3253 * userspace app is possibly compatible. This is extremely bogus since
3254 * our driver doesn't follow Adaptec's versioning system. Cheat by just
3255 * returning what the card reported.
3258 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3260 struct aac_rev_check rev_check;
3261 struct aac_rev_check_resp rev_check_resp;
3264 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3267 * Copyin the revision struct from userspace
3269 if ((error = copyin(udata, (caddr_t)&rev_check,
3270 sizeof(struct aac_rev_check))) != 0) {
3274 fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3275 rev_check.callingRevision.buildNumber);
3278 * Doctor up the response struct.
3280 rev_check_resp.possiblyCompatible = 1;
3281 rev_check_resp.adapterSWRevision.external.comp.major =
3282 AAC_DRIVER_MAJOR_VERSION;
3283 rev_check_resp.adapterSWRevision.external.comp.minor =
3284 AAC_DRIVER_MINOR_VERSION;
3285 rev_check_resp.adapterSWRevision.external.comp.type =
3287 rev_check_resp.adapterSWRevision.external.comp.dash =
3288 AAC_DRIVER_BUGFIX_LEVEL;
3289 rev_check_resp.adapterSWRevision.buildNumber =
3292 return(copyout((caddr_t)&rev_check_resp, udata,
3293 sizeof(struct aac_rev_check_resp)));
3297 * Pass the fib context to the caller
3300 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3302 struct aac_fib_context *fibctx, *ctx;
3305 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3307 fibctx = malloc(sizeof(struct aac_fib_context), M_AACRAIDBUF, M_NOWAIT|M_ZERO);
3311 mtx_lock(&sc->aac_io_lock);
3312 /* all elements are already 0, add to queue */
3313 if (sc->fibctx == NULL)
3314 sc->fibctx = fibctx;
3316 for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3322 /* evaluate unique value */
3323 fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3325 while (ctx != fibctx) {
3326 if (ctx->unique == fibctx->unique) {
3334 error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3335 mtx_unlock(&sc->aac_io_lock);
3337 aac_close_aif(sc, (caddr_t)ctx);
3342 * Close the caller's fib context
3345 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3347 struct aac_fib_context *ctx;
3349 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3351 mtx_lock(&sc->aac_io_lock);
3352 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3353 if (ctx->unique == *(uint32_t *)&arg) {
3354 if (ctx == sc->fibctx)
3357 ctx->prev->next = ctx->next;
3359 ctx->next->prev = ctx->prev;
3365 free(ctx, M_AACRAIDBUF);
3367 mtx_unlock(&sc->aac_io_lock);
3372 * Pass the caller the next AIF in their queue
3375 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3377 struct get_adapter_fib_ioctl agf;
3378 struct aac_fib_context *ctx;
3381 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3383 mtx_lock(&sc->aac_io_lock);
3384 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3385 for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3386 if (agf.AdapterFibContext == ctx->unique)
3390 mtx_unlock(&sc->aac_io_lock);
3394 error = aac_return_aif(sc, ctx, agf.AifFib);
3395 if (error == EAGAIN && agf.Wait) {
3396 fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3397 sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3398 while (error == EAGAIN) {
3399 mtx_unlock(&sc->aac_io_lock);
3400 error = tsleep(sc->aac_aifq, PRIBIO |
3401 PCATCH, "aacaif", 0);
3402 mtx_lock(&sc->aac_io_lock);
3404 error = aac_return_aif(sc, ctx, agf.AifFib);
3406 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3409 mtx_unlock(&sc->aac_io_lock);
3414 * Hand the next AIF off the top of the queue out to userspace.
3417 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3421 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3423 current = ctx->ctx_idx;
3424 if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3429 copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3431 device_printf(sc->aac_dev,
3432 "aac_return_aif: copyout returned %d\n", error);
3435 ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3441 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3443 struct aac_pci_info {
3449 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3451 pciinf.bus = pci_get_bus(sc->aac_dev);
3452 pciinf.slot = pci_get_slot(sc->aac_dev);
3454 error = copyout((caddr_t)&pciinf, uptr,
3455 sizeof(struct aac_pci_info));
3461 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3463 struct aac_features f;
3466 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3468 if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3472 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3473 * ALL zero in the featuresState, the driver will return the current
3474 * state of all the supported features, the data field will not be
3476 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3477 * a specific bit set in the featuresState, the driver will return the
3478 * current state of this specific feature and whatever data that are
3479 * associated with the feature in the data field or perform whatever
3480 * action needed indicates in the data field.
3482 if (f.feat.fValue == 0) {
3483 f.feat.fBits.largeLBA =
3484 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3485 f.feat.fBits.JBODSupport = 1;
3486 /* TODO: In the future, add other features state here as well */
3488 if (f.feat.fBits.largeLBA)
3489 f.feat.fBits.largeLBA =
3490 (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3491 /* TODO: Add other features state and data in the future */
3494 error = copyout(&f, uptr, sizeof (f));
3499 * Give the userland some information about the container. The AAC arch
3500 * expects the driver to be a SCSI passthrough type driver, so it expects
3501 * the containers to have b:t:l numbers. Fake it.
3504 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3506 struct aac_query_disk query_disk;
3507 struct aac_container *co;
3510 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3512 mtx_lock(&sc->aac_io_lock);
3513 error = copyin(uptr, (caddr_t)&query_disk,
3514 sizeof(struct aac_query_disk));
3516 mtx_unlock(&sc->aac_io_lock);
3520 id = query_disk.ContainerNumber;
3522 mtx_unlock(&sc->aac_io_lock);
3526 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3527 if (co->co_mntobj.ObjectId == id)
3532 query_disk.Valid = 0;
3533 query_disk.Locked = 0;
3534 query_disk.Deleted = 1; /* XXX is this right? */
3536 query_disk.Valid = 1;
3537 query_disk.Locked = 1;
3538 query_disk.Deleted = 0;
3539 query_disk.Bus = device_get_unit(sc->aac_dev);
3540 query_disk.Target = 0;
3542 query_disk.UnMapped = 0;
3545 error = copyout((caddr_t)&query_disk, uptr,
3546 sizeof(struct aac_query_disk));
3548 mtx_unlock(&sc->aac_io_lock);
3553 aac_container_bus(struct aac_softc *sc)
3555 struct aac_sim *sim;
3558 sim =(struct aac_sim *)malloc(sizeof(struct aac_sim),
3559 M_AACRAIDBUF, M_NOWAIT | M_ZERO);
3561 device_printf(sc->aac_dev,
3562 "No memory to add container bus\n");
3563 panic("Out of memory?!");
3565 child = device_add_child(sc->aac_dev, "aacraidp", -1);
3566 if (child == NULL) {
3567 device_printf(sc->aac_dev,
3568 "device_add_child failed for container bus\n");
3569 free(sim, M_AACRAIDBUF);
3570 panic("Out of memory?!");
3573 sim->TargetsPerBus = AAC_MAX_CONTAINERS;
3575 sim->BusType = CONTAINER_BUS;
3576 sim->InitiatorBusId = -1;
3578 sim->sim_dev = child;
3579 sim->aac_cam = NULL;
3581 device_set_ivars(child, sim);
3582 device_set_desc(child, "Container Bus");
3583 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, sim, sim_link);
3585 device_set_desc(child, aac_describe_code(aac_container_types,
3586 mir->MntTable[0].VolType));
3588 bus_generic_attach(sc->aac_dev);
3592 aac_get_bus_info(struct aac_softc *sc)
3594 struct aac_fib *fib;
3595 struct aac_ctcfg *c_cmd;
3596 struct aac_ctcfg_resp *c_resp;
3597 struct aac_vmioctl *vmi;
3598 struct aac_vmi_businf_resp *vmi_resp;
3599 struct aac_getbusinf businfo;
3600 struct aac_sim *caminf;
3604 mtx_lock(&sc->aac_io_lock);
3605 aac_alloc_sync_fib(sc, &fib);
3606 c_cmd = (struct aac_ctcfg *)&fib->data[0];
3607 bzero(c_cmd, sizeof(struct aac_ctcfg));
3609 c_cmd->Command = VM_ContainerConfig;
3610 c_cmd->cmd = CT_GET_SCSI_METHOD;
3613 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3614 sizeof(struct aac_ctcfg));
3616 device_printf(sc->aac_dev, "Error %d sending "
3617 "VM_ContainerConfig command\n", error);
3618 aac_release_sync_fib(sc);
3619 mtx_unlock(&sc->aac_io_lock);
3623 c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3624 if (c_resp->Status != ST_OK) {
3625 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3627 aac_release_sync_fib(sc);
3628 mtx_unlock(&sc->aac_io_lock);
3632 sc->scsi_method_id = c_resp->param;
3634 vmi = (struct aac_vmioctl *)&fib->data[0];
3635 bzero(vmi, sizeof(struct aac_vmioctl));
3637 vmi->Command = VM_Ioctl;
3638 vmi->ObjType = FT_DRIVE;
3639 vmi->MethId = sc->scsi_method_id;
3641 vmi->IoctlCmd = GetBusInfo;
3643 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3644 sizeof(struct aac_vmi_businf_resp));
3646 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3648 aac_release_sync_fib(sc);
3649 mtx_unlock(&sc->aac_io_lock);
3653 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3654 if (vmi_resp->Status != ST_OK) {
3655 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3657 aac_release_sync_fib(sc);
3658 mtx_unlock(&sc->aac_io_lock);
3662 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3663 aac_release_sync_fib(sc);
3664 mtx_unlock(&sc->aac_io_lock);
3666 for (i = 0; i < businfo.BusCount; i++) {
3667 if (businfo.BusValid[i] != AAC_BUS_VALID)
3670 caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
3671 M_AACRAIDBUF, M_NOWAIT | M_ZERO);
3672 if (caminf == NULL) {
3673 device_printf(sc->aac_dev,
3674 "No memory to add passthrough bus %d\n", i);
3678 child = device_add_child(sc->aac_dev, "aacraidp", -1);
3679 if (child == NULL) {
3680 device_printf(sc->aac_dev,
3681 "device_add_child failed for passthrough bus %d\n",
3683 free(caminf, M_AACRAIDBUF);
3687 caminf->TargetsPerBus = businfo.TargetsPerBus;
3688 caminf->BusNumber = i+1;
3689 caminf->BusType = PASSTHROUGH_BUS;
3690 caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3691 caminf->aac_sc = sc;
3692 caminf->sim_dev = child;
3693 caminf->aac_cam = NULL;
3695 device_set_ivars(child, caminf);
3696 device_set_desc(child, "SCSI Passthrough Bus");
3697 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3702 * Check to see if the kernel is up and running. If we are in a
3703 * BlinkLED state, return the BlinkLED code.
3706 aac_check_adapter_health(struct aac_softc *sc, u_int8_t *bled)
3710 ret = AAC_GET_FWSTATUS(sc);
3712 if (ret & AAC_UP_AND_RUNNING)
3714 else if (ret & AAC_KERNEL_PANIC && bled)
3715 *bled = (ret >> 16) & 0xff;
3721 * Once do an IOP reset, basically have to re-initialize the card as
3722 * if coming up from a cold boot, and the driver is responsible for
3723 * any IO that was outstanding to the adapter at the time of the IOP
3724 * RESET. And prepare the driver for IOP RESET by making the init code
3725 * modular with the ability to call it from multiple places.
3728 aac_reset_adapter(struct aac_softc *sc)
3730 struct aac_command *cm;
3731 struct aac_fib *fib;
3732 struct aac_pause_command *pc;
3733 u_int32_t status, reset_mask, waitCount, max_msix_orig;
3734 int msi_enabled_orig;
3736 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3737 mtx_assert(&sc->aac_io_lock, MA_OWNED);
3739 if (sc->aac_state & AAC_STATE_RESET) {
3740 device_printf(sc->aac_dev, "aac_reset_adapter() already in progress\n");
3743 sc->aac_state |= AAC_STATE_RESET;
3745 /* disable interrupt */
3746 AAC_ACCESS_DEVREG(sc, AAC_DISABLE_INTERRUPT);
3749 * Abort all pending commands:
3750 * a) on the controller
3752 while ((cm = aac_dequeue_busy(sc)) != NULL) {
3753 cm->cm_flags |= AAC_CMD_RESET;
3755 /* is there a completion handler? */
3756 if (cm->cm_complete != NULL) {
3757 cm->cm_complete(cm);
3759 /* assume that someone is sleeping on this
3766 /* b) in the waiting queues */
3767 while ((cm = aac_dequeue_ready(sc)) != NULL) {
3768 cm->cm_flags |= AAC_CMD_RESET;
3770 /* is there a completion handler? */
3771 if (cm->cm_complete != NULL) {
3772 cm->cm_complete(cm);
3774 /* assume that someone is sleeping on this
3782 if (aac_check_adapter_health(sc, NULL) == 0) {
3783 mtx_unlock(&sc->aac_io_lock);
3784 (void) aacraid_shutdown(sc->aac_dev);
3785 mtx_lock(&sc->aac_io_lock);
3788 /* execute IOP reset */
3789 if (sc->aac_support_opt2 & AAC_SUPPORTED_MU_RESET) {
3790 AAC_MEM0_SETREG4(sc, AAC_IRCSR, AAC_IRCSR_CORES_RST);
3792 /* We need to wait for 5 seconds before accessing the MU again
3793 * 10000 * 100us = 1000,000us = 1000ms = 1s
3795 waitCount = 5 * 10000;
3797 DELAY(100); /* delay 100 microseconds */
3800 } else if ((aacraid_sync_command(sc,
3801 AAC_IOP_RESET_ALWAYS, 0, 0, 0, 0, &status, &reset_mask)) != 0) {
3802 /* call IOP_RESET for older firmware */
3803 if ((aacraid_sync_command(sc,
3804 AAC_IOP_RESET, 0, 0, 0, 0, &status, NULL)) != 0) {
3806 if (status == AAC_SRB_STS_INVALID_REQUEST)
3807 device_printf(sc->aac_dev, "IOP_RESET not supported\n");
3809 /* probably timeout */
3810 device_printf(sc->aac_dev, "IOP_RESET failed\n");
3812 /* unwind aac_shutdown() */
3813 aac_alloc_sync_fib(sc, &fib);
3814 pc = (struct aac_pause_command *)&fib->data[0];
3815 pc->Command = VM_ContainerConfig;
3816 pc->Type = CT_PAUSE_IO;
3821 (void) aac_sync_fib(sc, ContainerCommand, 0, fib,
3822 sizeof (struct aac_pause_command));
3823 aac_release_sync_fib(sc);
3827 } else if (sc->aac_support_opt2 & AAC_SUPPORTED_DOORBELL_RESET) {
3828 AAC_MEM0_SETREG4(sc, AAC_SRC_IDBR, reset_mask);
3830 * We need to wait for 5 seconds before accessing the doorbell
3831 * again, 10000 * 100us = 1000,000us = 1000ms = 1s
3833 waitCount = 5 * 10000;
3835 DELAY(100); /* delay 100 microseconds */
3841 * Initialize the adapter.
3843 max_msix_orig = sc->aac_max_msix;
3844 msi_enabled_orig = sc->msi_enabled;
3845 sc->msi_enabled = FALSE;
3846 if (aac_check_firmware(sc) != 0)
3848 if (!(sc->flags & AAC_FLAGS_SYNC_MODE)) {
3849 sc->aac_max_msix = max_msix_orig;
3850 if (msi_enabled_orig) {
3851 sc->msi_enabled = msi_enabled_orig;
3852 AAC_ACCESS_DEVREG(sc, AAC_ENABLE_MSIX);
3854 mtx_unlock(&sc->aac_io_lock);
3856 mtx_lock(&sc->aac_io_lock);
3860 sc->aac_state &= ~AAC_STATE_RESET;
3861 AAC_ACCESS_DEVREG(sc, AAC_ENABLE_INTERRUPT);
3862 aacraid_startio(sc);