1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2001 Scott Long 4 * Copyright (c) 2000 BSDi 5 * Copyright (c) 2001 Adaptec, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/aac/aac.c,v 1.9.2.14 2003/04/08 13:22:08 scottl Exp $ 30 * $DragonFly: src/sys/dev/raid/aac/aac.c,v 1.33 2007/11/10 19:02:04 swildner Exp $ 31 */ 32 33 /* 34 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters. 35 */ 36 37 #include "opt_aac.h" 38 39 /* #include <stddef.h> */ 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/kernel.h> 44 #include <sys/kthread.h> 45 #include <sys/sysctl.h> 46 #include <sys/poll.h> 47 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005 48 #include <sys/selinfo.h> 49 #else 50 #include <sys/select.h> 51 #endif 52 53 #include "aac_compat.h" 54 55 #include <sys/bus.h> 56 #include <sys/conf.h> 57 #include <sys/devicestat.h> 58 #include <sys/disk.h> 59 #include <sys/signalvar.h> 60 #include <sys/time.h> 61 #include <sys/eventhandler.h> 62 63 #include "aacreg.h" 64 #include "aac_ioctl.h" 65 #include "aacvar.h" 66 #include "aac_tables.h" 67 #include "aac_cam.h" 68 69 static void aac_startup(void *arg); 70 static void aac_add_container(struct aac_softc *sc, 71 struct aac_mntinforesp *mir, int f); 72 static void aac_get_bus_info(struct aac_softc *sc); 73 static int aac_shutdown(device_t dev); 74 75 /* Command Processing */ 76 static void aac_timeout(void *ssc); 77 static int aac_start(struct aac_command *cm); 78 static void aac_complete(void *context, int pending); 79 static int aac_bio_command(struct aac_softc *sc, struct aac_command **cmp); 80 static void aac_bio_complete(struct aac_command *cm); 81 static int aac_wait_command(struct aac_command *cm, int timeout); 82 static void aac_host_command(struct aac_softc *sc); 83 static void aac_host_response(struct aac_softc *sc); 84 85 /* Command Buffer Management */ 86 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs, 87 int nseg, int error); 88 static int aac_alloc_commands(struct aac_softc *sc); 89 static void aac_free_commands(struct aac_softc *sc); 90 static void aac_map_command(struct aac_command *cm); 91 static void aac_unmap_command(struct aac_command *cm); 92 93 /* Hardware Interface */ 94 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, 95 int error); 96 static int aac_check_firmware(struct aac_softc *sc); 97 static int aac_init(struct aac_softc *sc); 98 static int aac_sync_command(struct aac_softc *sc, u_int32_t command, 99 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, 100 u_int32_t arg3, u_int32_t *sp); 101 static int aac_enqueue_fib(struct aac_softc *sc, int queue, 102 struct aac_command *cm); 103 static int aac_dequeue_fib(struct aac_softc *sc, int queue, 104 u_int32_t *fib_size, struct aac_fib **fib_addr); 105 static int aac_enqueue_response(struct aac_softc *sc, int queue, 106 struct aac_fib *fib); 107 108 /* Falcon/PPC interface */ 109 static int aac_fa_get_fwstatus(struct aac_softc *sc); 110 static void aac_fa_qnotify(struct aac_softc *sc, int qbit); 111 static int aac_fa_get_istatus(struct aac_softc *sc); 112 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask); 113 static void aac_fa_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_fa_get_mailbox(struct aac_softc *sc, int mb); 117 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable); 118 119 struct aac_interface aac_fa_interface = { 120 aac_fa_get_fwstatus, 121 aac_fa_qnotify, 122 aac_fa_get_istatus, 123 aac_fa_clear_istatus, 124 aac_fa_set_mailbox, 125 aac_fa_get_mailbox, 126 aac_fa_set_interrupts 127 }; 128 129 /* StrongARM interface */ 130 static int aac_sa_get_fwstatus(struct aac_softc *sc); 131 static void aac_sa_qnotify(struct aac_softc *sc, int qbit); 132 static int aac_sa_get_istatus(struct aac_softc *sc); 133 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask); 134 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, 135 u_int32_t arg0, u_int32_t arg1, 136 u_int32_t arg2, u_int32_t arg3); 137 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb); 138 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable); 139 140 struct aac_interface aac_sa_interface = { 141 aac_sa_get_fwstatus, 142 aac_sa_qnotify, 143 aac_sa_get_istatus, 144 aac_sa_clear_istatus, 145 aac_sa_set_mailbox, 146 aac_sa_get_mailbox, 147 aac_sa_set_interrupts 148 }; 149 150 /* i960Rx interface */ 151 static int aac_rx_get_fwstatus(struct aac_softc *sc); 152 static void aac_rx_qnotify(struct aac_softc *sc, int qbit); 153 static int aac_rx_get_istatus(struct aac_softc *sc); 154 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask); 155 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, 156 u_int32_t arg0, u_int32_t arg1, 157 u_int32_t arg2, u_int32_t arg3); 158 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb); 159 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable); 160 161 struct aac_interface aac_rx_interface = { 162 aac_rx_get_fwstatus, 163 aac_rx_qnotify, 164 aac_rx_get_istatus, 165 aac_rx_clear_istatus, 166 aac_rx_set_mailbox, 167 aac_rx_get_mailbox, 168 aac_rx_set_interrupts 169 }; 170 171 /* Debugging and Diagnostics */ 172 static void aac_describe_controller(struct aac_softc *sc); 173 static char *aac_describe_code(struct aac_code_lookup *table, 174 u_int32_t code); 175 176 /* Management Interface */ 177 static d_open_t aac_open; 178 static d_close_t aac_close; 179 static d_ioctl_t aac_ioctl; 180 static d_poll_t aac_poll; 181 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib); 182 static void aac_handle_aif(struct aac_softc *sc, 183 struct aac_fib *fib); 184 static int aac_rev_check(struct aac_softc *sc, caddr_t udata); 185 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg); 186 static int aac_return_aif(struct aac_softc *sc, caddr_t uptr); 187 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr); 188 189 #define AAC_CDEV_MAJOR 150 190 191 static struct dev_ops aac_ops = { 192 { "aac", AAC_CDEV_MAJOR, 0 }, 193 .d_open = aac_open, 194 .d_close = aac_close, 195 .d_ioctl = aac_ioctl, 196 .d_poll = aac_poll, 197 }; 198 199 DECLARE_DUMMY_MODULE(aac); 200 201 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver"); 202 203 /* sysctl node */ 204 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters"); 205 206 /* 207 * Device Interface 208 */ 209 210 /* 211 * Initialise the controller and softc 212 */ 213 int 214 aac_attach(struct aac_softc *sc) 215 { 216 int error, unit; 217 218 debug_called(1); 219 callout_init(&sc->aac_watchdog); 220 221 /* 222 * Initialise per-controller queues. 223 */ 224 aac_initq_free(sc); 225 aac_initq_ready(sc); 226 aac_initq_busy(sc); 227 aac_initq_complete(sc); 228 aac_initq_bio(sc); 229 230 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005 231 /* 232 * Initialise command-completion task. 233 */ 234 TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc); 235 #endif 236 237 /* disable interrupts before we enable anything */ 238 AAC_MASK_INTERRUPTS(sc); 239 240 /* mark controller as suspended until we get ourselves organised */ 241 sc->aac_state |= AAC_STATE_SUSPEND; 242 243 /* 244 * Check that the firmware on the card is supported. 245 */ 246 if ((error = aac_check_firmware(sc)) != 0) 247 return(error); 248 249 /* Init the sync fib lock */ 250 AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock"); 251 252 /* 253 * Initialise the adapter. 254 */ 255 if ((error = aac_init(sc)) != 0) 256 return(error); 257 258 /* 259 * Print a little information about the controller. 260 */ 261 aac_describe_controller(sc); 262 263 /* 264 * Register to probe our containers later. 265 */ 266 TAILQ_INIT(&sc->aac_container_tqh); 267 AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock"); 268 269 /* 270 * Lock for the AIF queue 271 */ 272 AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock"); 273 274 sc->aac_ich.ich_func = aac_startup; 275 sc->aac_ich.ich_arg = sc; 276 sc->aac_ich.ich_desc = "aac"; 277 if (config_intrhook_establish(&sc->aac_ich) != 0) { 278 device_printf(sc->aac_dev, 279 "can't establish configuration hook\n"); 280 return(ENXIO); 281 } 282 283 /* 284 * Make the control device. 285 */ 286 unit = device_get_unit(sc->aac_dev); 287 dev_ops_add(&aac_ops, -1, unit); 288 sc->aac_dev_t = make_dev(&aac_ops, unit, UID_ROOT, GID_WHEEL, 0644, 289 "aac%d", unit); 290 #if defined(__FreeBSD__) && __FreeBSD_version > 500005 291 (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit); 292 (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit); 293 #endif 294 sc->aac_dev_t->si_drv1 = sc; 295 reference_dev(sc->aac_dev_t); 296 297 /* Create the AIF thread */ 298 #if defined(__FreeBSD__) && __FreeBSD_version > 500005 299 if (kthread_create((void(*)(void *))aac_host_command, sc, 300 &sc->aifthread, 0, "aac%daif", unit)) 301 #else 302 if (kthread_create((void(*)(void *))aac_host_command, sc, 303 &sc->aifthread, "aac%daif", unit)) 304 #endif 305 panic("Could not create AIF thread\n"); 306 307 /* Register the shutdown method to only be called post-dump */ 308 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, aac_shutdown, sc->aac_dev, 309 SHUTDOWN_PRI_DRIVER)) == NULL) 310 device_printf(sc->aac_dev, "shutdown event registration failed\n"); 311 312 /* Register with CAM for the non-DASD devices */ 313 if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) 314 aac_get_bus_info(sc); 315 316 return(0); 317 } 318 319 /* 320 * Probe for containers, create disks. 321 */ 322 static void 323 aac_startup(void *arg) 324 { 325 struct aac_softc *sc; 326 struct aac_fib *fib; 327 struct aac_mntinfo *mi; 328 struct aac_mntinforesp *mir = NULL; 329 int count = 0, i = 0; 330 331 debug_called(1); 332 333 sc = (struct aac_softc *)arg; 334 335 /* disconnect ourselves from the intrhook chain */ 336 config_intrhook_disestablish(&sc->aac_ich); 337 338 aac_alloc_sync_fib(sc, &fib, 0); 339 mi = (struct aac_mntinfo *)&fib->data[0]; 340 341 /* loop over possible containers */ 342 do { 343 /* request information on this container */ 344 bzero(mi, sizeof(struct aac_mntinfo)); 345 mi->Command = VM_NameServe; 346 mi->MntType = FT_FILESYS; 347 mi->MntCount = i; 348 if (aac_sync_fib(sc, ContainerCommand, 0, fib, 349 sizeof(struct aac_mntinfo))) { 350 device_printf(sc->aac_dev, 351 "error probing container %d", i); 352 353 continue; 354 } 355 356 mir = (struct aac_mntinforesp *)&fib->data[0]; 357 /* XXX Need to check if count changed */ 358 count = mir->MntRespCount; 359 aac_add_container(sc, mir, 0); 360 i++; 361 } while ((i < count) && (i < AAC_MAX_CONTAINERS)); 362 363 aac_release_sync_fib(sc); 364 365 /* poke the bus to actually attach the child devices */ 366 if (bus_generic_attach(sc->aac_dev)) 367 device_printf(sc->aac_dev, "bus_generic_attach failed\n"); 368 369 /* mark the controller up */ 370 sc->aac_state &= ~AAC_STATE_SUSPEND; 371 372 /* enable interrupts now */ 373 AAC_UNMASK_INTERRUPTS(sc); 374 375 /* enable the timeout watchdog */ 376 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz, 377 aac_timeout, sc); 378 } 379 380 /* 381 * Create a device to respresent a new container 382 */ 383 static void 384 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f) 385 { 386 struct aac_container *co; 387 device_t child; 388 389 /* 390 * Check container volume type for validity. Note that many of 391 * the possible types may never show up. 392 */ 393 if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) { 394 MALLOC(co, struct aac_container *, sizeof *co, M_AACBUF, 395 M_INTWAIT); 396 debug(1, "id %x name '%.16s' size %u type %d", 397 mir->MntTable[0].ObjectId, 398 mir->MntTable[0].FileSystemName, 399 mir->MntTable[0].Capacity, mir->MntTable[0].VolType); 400 401 if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL) 402 device_printf(sc->aac_dev, "device_add_child failed\n"); 403 else 404 device_set_ivars(child, co); 405 device_set_desc(child, aac_describe_code(aac_container_types, 406 mir->MntTable[0].VolType)); 407 co->co_disk = child; 408 co->co_found = f; 409 bcopy(&mir->MntTable[0], &co->co_mntobj, 410 sizeof(struct aac_mntobj)); 411 AAC_LOCK_ACQUIRE(&sc->aac_container_lock); 412 TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link); 413 AAC_LOCK_RELEASE(&sc->aac_container_lock); 414 } 415 } 416 417 /* 418 * Free all of the resources associated with (sc) 419 * 420 * Should not be called if the controller is active. 421 */ 422 void 423 aac_free(struct aac_softc *sc) 424 { 425 debug_called(1); 426 427 /* remove the control device */ 428 if (sc->aac_dev_t != NULL) 429 destroy_dev(sc->aac_dev_t); 430 431 /* throw away any FIB buffers, discard the FIB DMA tag */ 432 if (sc->aac_fibs != NULL) 433 aac_free_commands(sc); 434 if (sc->aac_fib_dmat) 435 bus_dma_tag_destroy(sc->aac_fib_dmat); 436 437 /* destroy the common area */ 438 if (sc->aac_common) { 439 bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap); 440 bus_dmamem_free(sc->aac_common_dmat, sc->aac_common, 441 sc->aac_common_dmamap); 442 } 443 if (sc->aac_common_dmat) 444 bus_dma_tag_destroy(sc->aac_common_dmat); 445 446 /* disconnect the interrupt handler */ 447 if (sc->aac_intr) 448 bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr); 449 if (sc->aac_irq != NULL) 450 bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid, 451 sc->aac_irq); 452 453 /* destroy data-transfer DMA tag */ 454 if (sc->aac_buffer_dmat) 455 bus_dma_tag_destroy(sc->aac_buffer_dmat); 456 457 /* destroy the parent DMA tag */ 458 if (sc->aac_parent_dmat) 459 bus_dma_tag_destroy(sc->aac_parent_dmat); 460 461 /* release the register window mapping */ 462 if (sc->aac_regs_resource != NULL) { 463 bus_release_resource(sc->aac_dev, SYS_RES_MEMORY, 464 sc->aac_regs_rid, sc->aac_regs_resource); 465 } 466 dev_ops_remove(&aac_ops, -1, device_get_unit(sc->aac_dev)); 467 } 468 469 /* 470 * Disconnect from the controller completely, in preparation for unload. 471 */ 472 int 473 aac_detach(device_t dev) 474 { 475 struct aac_softc *sc; 476 #if AAC_BROKEN 477 int error; 478 #endif 479 480 debug_called(1); 481 482 sc = device_get_softc(dev); 483 484 callout_stop(&sc->aac_watchdog); 485 486 if (sc->aac_state & AAC_STATE_OPEN) 487 return(EBUSY); 488 489 #if AAC_BROKEN 490 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { 491 sc->aifflags |= AAC_AIFFLAGS_EXIT; 492 wakeup(sc->aifthread); 493 tsleep(sc->aac_dev, PCATCH, "aacdch", 30 * hz); 494 } 495 496 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) 497 panic("Cannot shutdown AIF thread\n"); 498 499 if ((error = aac_shutdown(dev))) 500 return(error); 501 502 aac_free(sc); 503 504 return(0); 505 #else 506 return (EBUSY); 507 #endif 508 } 509 510 /* 511 * Bring the controller down to a dormant state and detach all child devices. 512 * 513 * This function is called before detach or system shutdown. 514 * 515 * Note that we can assume that the bioq on the controller is empty, as we won't 516 * allow shutdown if any device is open. 517 */ 518 static int 519 aac_shutdown(device_t dev) 520 { 521 struct aac_softc *sc; 522 struct aac_fib *fib; 523 struct aac_close_command *cc; 524 525 debug_called(1); 526 527 sc = device_get_softc(dev); 528 529 crit_enter(); 530 531 sc->aac_state |= AAC_STATE_SUSPEND; 532 533 /* 534 * Send a Container shutdown followed by a HostShutdown FIB to the 535 * controller to convince it that we don't want to talk to it anymore. 536 * We've been closed and all I/O completed already 537 */ 538 device_printf(sc->aac_dev, "shutting down controller..."); 539 540 aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE); 541 cc = (struct aac_close_command *)&fib->data[0]; 542 543 bzero(cc, sizeof(struct aac_close_command)); 544 cc->Command = VM_CloseAll; 545 cc->ContainerId = 0xffffffff; 546 if (aac_sync_fib(sc, ContainerCommand, 0, fib, 547 sizeof(struct aac_close_command))) 548 kprintf("FAILED.\n"); 549 else { 550 fib->data[0] = 0; 551 /* 552 * XXX Issuing this command to the controller makes it shut down 553 * but also keeps it from coming back up without a reset of the 554 * PCI bus. This is not desirable if you are just unloading the 555 * driver module with the intent to reload it later. 556 */ 557 if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN, 558 fib, 1)) { 559 kprintf("FAILED.\n"); 560 } else { 561 kprintf("done.\n"); 562 } 563 } 564 565 AAC_MASK_INTERRUPTS(sc); 566 567 crit_exit(); 568 return(0); 569 } 570 571 /* 572 * Bring the controller to a quiescent state, ready for system suspend. 573 */ 574 int 575 aac_suspend(device_t dev) 576 { 577 struct aac_softc *sc; 578 579 debug_called(1); 580 581 sc = device_get_softc(dev); 582 583 crit_enter(); 584 585 sc->aac_state |= AAC_STATE_SUSPEND; 586 587 AAC_MASK_INTERRUPTS(sc); 588 crit_exit(); 589 return(0); 590 } 591 592 /* 593 * Bring the controller back to a state ready for operation. 594 */ 595 int 596 aac_resume(device_t dev) 597 { 598 struct aac_softc *sc; 599 600 debug_called(1); 601 602 sc = device_get_softc(dev); 603 604 sc->aac_state &= ~AAC_STATE_SUSPEND; 605 AAC_UNMASK_INTERRUPTS(sc); 606 return(0); 607 } 608 609 /* 610 * Take an interrupt. 611 */ 612 void 613 aac_intr(void *arg) 614 { 615 struct aac_softc *sc; 616 u_int16_t reason; 617 u_int32_t *resp_queue; 618 619 debug_called(2); 620 621 sc = (struct aac_softc *)arg; 622 623 /* 624 * Optimize the common case of adapter response interrupts. 625 * We must read from the card prior to processing the responses 626 * to ensure the clear is flushed prior to accessing the queues. 627 * Reading the queues from local memory might save us a PCI read. 628 */ 629 resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE]; 630 if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX]) 631 reason = AAC_DB_RESPONSE_READY; 632 else 633 reason = AAC_GET_ISTATUS(sc); 634 AAC_CLEAR_ISTATUS(sc, reason); 635 (void)AAC_GET_ISTATUS(sc); 636 637 /* It's not ok to return here because of races with the previous step */ 638 if (reason & AAC_DB_RESPONSE_READY) 639 aac_host_response(sc); 640 641 /* controller wants to talk to the log */ 642 if (reason & AAC_DB_PRINTF) 643 aac_print_printf(sc); 644 645 /* controller has a message for us? */ 646 if (reason & AAC_DB_COMMAND_READY) { 647 /* XXX What happens if the thread is already awake? */ 648 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { 649 sc->aifflags |= AAC_AIFFLAGS_PENDING; 650 wakeup(sc->aifthread); 651 } 652 } 653 } 654 655 /* 656 * Command Processing 657 */ 658 659 /* 660 * Start as much queued I/O as possible on the controller 661 */ 662 void 663 aac_startio(struct aac_softc *sc) 664 { 665 struct aac_command *cm; 666 667 debug_called(2); 668 669 for (;;) { 670 /* 671 * Try to get a command that's been put off for lack of 672 * resources 673 */ 674 cm = aac_dequeue_ready(sc); 675 676 /* 677 * Try to build a command off the bio queue (ignore error 678 * return) 679 */ 680 if (cm == NULL) 681 aac_bio_command(sc, &cm); 682 683 /* nothing to do? */ 684 if (cm == NULL) 685 break; 686 687 /* try to give the command to the controller */ 688 if (aac_start(cm) == EBUSY) { 689 /* put it on the ready queue for later */ 690 aac_requeue_ready(cm); 691 break; 692 } 693 } 694 } 695 696 /* 697 * Deliver a command to the controller; allocate controller resources at the 698 * last moment when possible. 699 */ 700 static int 701 aac_start(struct aac_command *cm) 702 { 703 struct aac_softc *sc; 704 int error; 705 706 debug_called(2); 707 708 sc = cm->cm_sc; 709 710 /* get the command mapped */ 711 aac_map_command(cm); 712 713 /* fix up the address values in the FIB */ 714 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib; 715 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys; 716 717 /* save a pointer to the command for speedy reverse-lookup */ 718 cm->cm_fib->Header.SenderData = (u_int32_t)cm; /* XXX 64-bit physical 719 * address issue */ 720 /* put the FIB on the outbound queue */ 721 error = aac_enqueue_fib(sc, cm->cm_queue, cm); 722 return(error); 723 } 724 725 /* 726 * Handle notification of one or more FIBs coming from the controller. 727 */ 728 static void 729 aac_host_command(struct aac_softc *sc) 730 { 731 struct aac_fib *fib; 732 u_int32_t fib_size; 733 int size; 734 735 debug_called(2); 736 737 sc->aifflags |= AAC_AIFFLAGS_RUNNING; 738 739 while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) { 740 if (!(sc->aifflags & AAC_AIFFLAGS_PENDING)) 741 tsleep(sc->aifthread, 0, "aifthd", 15 * hz); 742 743 sc->aifflags &= ~AAC_AIFFLAGS_PENDING; 744 for (;;) { 745 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE, 746 &fib_size, &fib)) 747 break; /* nothing to do */ 748 749 AAC_PRINT_FIB(sc, fib); 750 751 switch (fib->Header.Command) { 752 case AifRequest: 753 aac_handle_aif(sc, fib); 754 break; 755 default: 756 device_printf(sc->aac_dev, "unknown command " 757 "from controller\n"); 758 break; 759 } 760 761 /* Return the AIF to the controller. */ 762 if ((fib->Header.XferState == 0) || 763 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) 764 break; 765 766 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) { 767 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST; 768 *(AAC_FSAStatus*)fib->data = ST_OK; 769 770 /* XXX Compute the Size field? */ 771 size = fib->Header.Size; 772 if (size > sizeof(struct aac_fib)) { 773 size = sizeof(struct aac_fib); 774 fib->Header.Size = size; 775 } 776 /* 777 * Since we did not generate this command, it 778 * cannot go through the normal 779 * enqueue->startio chain. 780 */ 781 aac_enqueue_response(sc, 782 AAC_ADAP_NORM_RESP_QUEUE, 783 fib); 784 } 785 } 786 } 787 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING; 788 wakeup(sc->aac_dev); 789 790 #if defined(__FreeBSD__) && __FreeBSD_version > 500005 791 mtx_lock(&Giant); 792 #endif 793 kthread_exit(); 794 } 795 796 /* 797 * Handle notification of one or more FIBs completed by the controller 798 */ 799 static void 800 aac_host_response(struct aac_softc *sc) 801 { 802 struct aac_command *cm; 803 struct aac_fib *fib; 804 u_int32_t fib_size; 805 806 debug_called(2); 807 808 for (;;) { 809 /* look for completed FIBs on our queue */ 810 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size, 811 &fib)) 812 break; /* nothing to do */ 813 814 /* get the command, unmap and queue for later processing */ 815 cm = (struct aac_command *)fib->Header.SenderData; 816 if (cm == NULL) { 817 AAC_PRINT_FIB(sc, fib); 818 } else { 819 aac_remove_busy(cm); 820 aac_unmap_command(cm); /* XXX defer? */ 821 aac_enqueue_complete(cm); 822 } 823 } 824 825 /* handle completion processing */ 826 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005 827 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete); 828 #else 829 aac_complete(sc, 0); 830 #endif 831 } 832 833 /* 834 * Process completed commands. 835 */ 836 static void 837 aac_complete(void *context, int pending) 838 { 839 struct aac_softc *sc; 840 struct aac_command *cm; 841 842 debug_called(2); 843 844 sc = (struct aac_softc *)context; 845 846 /* pull completed commands off the queue */ 847 for (;;) { 848 cm = aac_dequeue_complete(sc); 849 if (cm == NULL) 850 break; 851 cm->cm_flags |= AAC_CMD_COMPLETED; 852 853 /* is there a completion handler? */ 854 if (cm->cm_complete != NULL) { 855 cm->cm_complete(cm); 856 } else { 857 /* assume that someone is sleeping on this command */ 858 wakeup(cm); 859 } 860 } 861 862 /* see if we can start some more I/O */ 863 aac_startio(sc); 864 } 865 866 /* 867 * Handle a bio submitted from a disk device. 868 */ 869 void 870 aac_submit_bio(struct aac_disk *ad, struct bio *bio) 871 { 872 struct aac_softc *sc; 873 874 debug_called(2); 875 876 bio->bio_driver_info = ad; 877 sc = ad->ad_controller; 878 879 /* queue the BIO and try to get some work done */ 880 aac_enqueue_bio(sc, bio); 881 aac_startio(sc); 882 } 883 884 /* 885 * Get a bio and build a command to go with it. 886 */ 887 static int 888 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp) 889 { 890 struct aac_command *cm; 891 struct aac_fib *fib; 892 struct aac_blockread *br; 893 struct aac_blockwrite *bw; 894 struct aac_disk *ad; 895 struct bio *bio; 896 struct buf *bp; 897 898 debug_called(2); 899 900 /* get the resources we will need */ 901 cm = NULL; 902 if ((bio = aac_dequeue_bio(sc)) == NULL) 903 goto fail; 904 if (aac_alloc_command(sc, &cm)) /* get a command */ 905 goto fail; 906 907 /* fill out the command */ 908 bp = bio->bio_buf; 909 cm->cm_data = (void *)bp->b_data; 910 cm->cm_datalen = bp->b_bcount; 911 cm->cm_complete = aac_bio_complete; 912 cm->cm_private = bio; 913 cm->cm_timestamp = time_second; 914 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 915 916 /* build the FIB */ 917 fib = cm->cm_fib; 918 fib->Header.XferState = 919 AAC_FIBSTATE_HOSTOWNED | 920 AAC_FIBSTATE_INITIALISED | 921 AAC_FIBSTATE_EMPTY | 922 AAC_FIBSTATE_FROMHOST | 923 AAC_FIBSTATE_REXPECTED | 924 AAC_FIBSTATE_NORM | 925 AAC_FIBSTATE_ASYNC | 926 AAC_FIBSTATE_FAST_RESPONSE; 927 fib->Header.Command = ContainerCommand; 928 fib->Header.Size = sizeof(struct aac_fib_header); 929 930 /* build the read/write request */ 931 ad = (struct aac_disk *)bio->bio_driver_info; 932 if (bp->b_cmd == BUF_CMD_READ) { 933 br = (struct aac_blockread *)&fib->data[0]; 934 br->Command = VM_CtBlockRead; 935 br->ContainerId = ad->ad_container->co_mntobj.ObjectId; 936 br->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE; 937 br->ByteCount = bp->b_bcount; 938 fib->Header.Size += sizeof(struct aac_blockread); 939 cm->cm_sgtable = &br->SgMap; 940 cm->cm_flags |= AAC_CMD_DATAIN; 941 } else { 942 bw = (struct aac_blockwrite *)&fib->data[0]; 943 bw->Command = VM_CtBlockWrite; 944 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 945 bw->BlockNumber = bio->bio_offset / AAC_BLOCK_SIZE; 946 bw->ByteCount = bp->b_bcount; 947 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */ 948 fib->Header.Size += sizeof(struct aac_blockwrite); 949 cm->cm_flags |= AAC_CMD_DATAOUT; 950 cm->cm_sgtable = &bw->SgMap; 951 } 952 953 *cmp = cm; 954 return(0); 955 956 fail: 957 if (bio != NULL) 958 aac_enqueue_bio(sc, bio); 959 if (cm != NULL) 960 aac_release_command(cm); 961 return(ENOMEM); 962 } 963 964 /* 965 * Handle a bio-instigated command that has been completed. 966 */ 967 static void 968 aac_bio_complete(struct aac_command *cm) 969 { 970 struct aac_blockread_response *brr; 971 struct aac_blockwrite_response *bwr; 972 struct bio *bio; 973 struct buf *bp; 974 const char *code; 975 AAC_FSAStatus status; 976 977 /* fetch relevant status and then release the command */ 978 bio = (struct bio *)cm->cm_private; 979 bp = bio->bio_buf; 980 if (bp->b_cmd == BUF_CMD_READ) { 981 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0]; 982 status = brr->Status; 983 } else { 984 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0]; 985 status = bwr->Status; 986 } 987 aac_release_command(cm); 988 989 /* fix up the bio based on status */ 990 if (status == ST_OK) { 991 bp->b_resid = 0; 992 code = 0; 993 } else { 994 bp->b_error = EIO; 995 bp->b_flags |= B_ERROR; 996 /* pass an error string out to the disk layer */ 997 code = aac_describe_code(aac_command_status_table, status); 998 } 999 aac_biodone(bio, code); 1000 } 1001 1002 /* 1003 * Dump a block of data to the controller. If the queue is full, tell the 1004 * caller to hold off and wait for the queue to drain. 1005 */ 1006 int 1007 aac_dump_enqueue(struct aac_disk *ad, u_int64_t lba, void *data, int dumppages) 1008 { 1009 struct aac_softc *sc; 1010 struct aac_command *cm; 1011 struct aac_fib *fib; 1012 struct aac_blockwrite *bw; 1013 1014 sc = ad->ad_controller; 1015 cm = NULL; 1016 1017 KKASSERT(lba <= 0x100000000ULL); 1018 1019 if (aac_alloc_command(sc, &cm)) 1020 return (EBUSY); 1021 1022 /* fill out the command */ 1023 cm->cm_data = data; 1024 cm->cm_datalen = dumppages * PAGE_SIZE; 1025 cm->cm_complete = NULL; 1026 cm->cm_private = NULL; 1027 cm->cm_timestamp = time_second; 1028 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 1029 1030 /* build the FIB */ 1031 fib = cm->cm_fib; 1032 fib->Header.XferState = 1033 AAC_FIBSTATE_HOSTOWNED | 1034 AAC_FIBSTATE_INITIALISED | 1035 AAC_FIBSTATE_FROMHOST | 1036 AAC_FIBSTATE_REXPECTED | 1037 AAC_FIBSTATE_NORM; 1038 fib->Header.Command = ContainerCommand; 1039 fib->Header.Size = sizeof(struct aac_fib_header); 1040 1041 bw = (struct aac_blockwrite *)&fib->data[0]; 1042 bw->Command = VM_CtBlockWrite; 1043 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 1044 bw->BlockNumber = lba; 1045 bw->ByteCount = dumppages * PAGE_SIZE; 1046 bw->Stable = CUNSTABLE; /* XXX what's appropriate here? */ 1047 fib->Header.Size += sizeof(struct aac_blockwrite); 1048 cm->cm_flags |= AAC_CMD_DATAOUT; 1049 cm->cm_sgtable = &bw->SgMap; 1050 1051 return (aac_start(cm)); 1052 } 1053 1054 /* 1055 * Wait for the card's queue to drain when dumping. Also check for monitor 1056 * kprintf's 1057 */ 1058 void 1059 aac_dump_complete(struct aac_softc *sc) 1060 { 1061 struct aac_fib *fib; 1062 struct aac_command *cm; 1063 u_int16_t reason; 1064 u_int32_t pi, ci, fib_size; 1065 1066 do { 1067 reason = AAC_GET_ISTATUS(sc); 1068 if (reason & AAC_DB_RESPONSE_READY) { 1069 AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY); 1070 for (;;) { 1071 if (aac_dequeue_fib(sc, 1072 AAC_HOST_NORM_RESP_QUEUE, 1073 &fib_size, &fib)) 1074 break; 1075 cm = (struct aac_command *) 1076 fib->Header.SenderData; 1077 if (cm == NULL) 1078 AAC_PRINT_FIB(sc, fib); 1079 else { 1080 aac_remove_busy(cm); 1081 aac_unmap_command(cm); 1082 aac_enqueue_complete(cm); 1083 aac_release_command(cm); 1084 } 1085 } 1086 } 1087 if (reason & AAC_DB_PRINTF) { 1088 AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF); 1089 aac_print_printf(sc); 1090 } 1091 pi = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][ 1092 AAC_PRODUCER_INDEX]; 1093 ci = sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][ 1094 AAC_CONSUMER_INDEX]; 1095 } while (ci != pi); 1096 1097 return; 1098 } 1099 1100 /* 1101 * Submit a command to the controller, return when it completes. 1102 * XXX This is very dangerous! If the card has gone out to lunch, we could 1103 * be stuck here forever. At the same time, signals are not caught 1104 * because there is a risk that a signal could wakeup the tsleep before 1105 * the card has a chance to complete the command. The passed in timeout 1106 * is ignored for the same reason. Since there is no way to cancel a 1107 * command in progress, we should probably create a 'dead' queue where 1108 * commands go that have been interrupted/timed-out/etc, that keeps them 1109 * out of the free pool. That way, if the card is just slow, it won't 1110 * spam the memory of a command that has been recycled. 1111 */ 1112 static int 1113 aac_wait_command(struct aac_command *cm, int timeout) 1114 { 1115 int error = 0; 1116 1117 debug_called(2); 1118 1119 /* Put the command on the ready queue and get things going */ 1120 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 1121 aac_enqueue_ready(cm); 1122 aac_startio(cm->cm_sc); 1123 crit_enter(); 1124 while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) { 1125 error = tsleep(cm, 0, "aacwait", 0); 1126 } 1127 crit_exit(); 1128 return(error); 1129 } 1130 1131 /* 1132 *Command Buffer Management 1133 */ 1134 1135 /* 1136 * Allocate a command. 1137 */ 1138 int 1139 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp) 1140 { 1141 struct aac_command *cm; 1142 1143 debug_called(3); 1144 1145 if ((cm = aac_dequeue_free(sc)) == NULL) 1146 return(ENOMEM); 1147 1148 *cmp = cm; 1149 return(0); 1150 } 1151 1152 /* 1153 * Release a command back to the freelist. 1154 */ 1155 void 1156 aac_release_command(struct aac_command *cm) 1157 { 1158 debug_called(3); 1159 1160 /* (re)initialise the command/FIB */ 1161 cm->cm_sgtable = NULL; 1162 cm->cm_flags = 0; 1163 cm->cm_complete = NULL; 1164 cm->cm_private = NULL; 1165 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY; 1166 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB; 1167 cm->cm_fib->Header.Flags = 0; 1168 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib); 1169 1170 /* 1171 * These are duplicated in aac_start to cover the case where an 1172 * intermediate stage may have destroyed them. They're left 1173 * initialised here for debugging purposes only. 1174 */ 1175 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib; 1176 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys; 1177 cm->cm_fib->Header.SenderData = 0; 1178 1179 aac_enqueue_free(cm); 1180 } 1181 1182 /* 1183 * Map helper for command/FIB allocation. 1184 */ 1185 static void 1186 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1187 { 1188 struct aac_softc *sc; 1189 1190 sc = (struct aac_softc *)arg; 1191 1192 debug_called(3); 1193 1194 sc->aac_fibphys = segs[0].ds_addr; 1195 } 1196 1197 /* 1198 * Allocate and initialise commands/FIBs for this adapter. 1199 */ 1200 static int 1201 aac_alloc_commands(struct aac_softc *sc) 1202 { 1203 struct aac_command *cm; 1204 int i; 1205 1206 debug_called(1); 1207 1208 /* allocate the FIBs in DMAable memory and load them */ 1209 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs, 1210 BUS_DMA_NOWAIT, &sc->aac_fibmap)) { 1211 return(ENOMEM); 1212 } 1213 1214 bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs, 1215 AAC_FIB_COUNT * sizeof(struct aac_fib), 1216 aac_map_command_helper, sc, 0); 1217 1218 /* initialise constant fields in the command structure */ 1219 bzero(sc->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib)); 1220 for (i = 0; i < AAC_FIB_COUNT; i++) { 1221 cm = &sc->aac_command[i]; 1222 cm->cm_sc = sc; 1223 cm->cm_fib = sc->aac_fibs + i; 1224 cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib)); 1225 1226 if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap)) 1227 aac_release_command(cm); 1228 } 1229 return(0); 1230 } 1231 1232 /* 1233 * Free FIBs owned by this adapter. 1234 */ 1235 static void 1236 aac_free_commands(struct aac_softc *sc) 1237 { 1238 int i; 1239 1240 debug_called(1); 1241 1242 for (i = 0; i < AAC_FIB_COUNT; i++) 1243 bus_dmamap_destroy(sc->aac_buffer_dmat, 1244 sc->aac_command[i].cm_datamap); 1245 1246 bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap); 1247 bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap); 1248 } 1249 1250 /* 1251 * Command-mapping helper function - populate this command's s/g table. 1252 */ 1253 static void 1254 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1255 { 1256 struct aac_command *cm; 1257 struct aac_fib *fib; 1258 struct aac_sg_table *sg; 1259 int i; 1260 1261 debug_called(3); 1262 1263 cm = (struct aac_command *)arg; 1264 fib = cm->cm_fib; 1265 1266 /* find the s/g table */ 1267 sg = cm->cm_sgtable; 1268 1269 /* copy into the FIB */ 1270 if (sg != NULL) { 1271 sg->SgCount = nseg; 1272 for (i = 0; i < nseg; i++) { 1273 sg->SgEntry[i].SgAddress = segs[i].ds_addr; 1274 sg->SgEntry[i].SgByteCount = segs[i].ds_len; 1275 } 1276 /* update the FIB size for the s/g count */ 1277 fib->Header.Size += nseg * sizeof(struct aac_sg_entry); 1278 } 1279 1280 } 1281 1282 /* 1283 * Map a command into controller-visible space. 1284 */ 1285 static void 1286 aac_map_command(struct aac_command *cm) 1287 { 1288 struct aac_softc *sc; 1289 1290 debug_called(2); 1291 1292 sc = cm->cm_sc; 1293 1294 /* don't map more than once */ 1295 if (cm->cm_flags & AAC_CMD_MAPPED) 1296 return; 1297 1298 if (cm->cm_datalen != 0) { 1299 bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap, 1300 cm->cm_data, cm->cm_datalen, 1301 aac_map_command_sg, cm, 0); 1302 1303 if (cm->cm_flags & AAC_CMD_DATAIN) 1304 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1305 BUS_DMASYNC_PREREAD); 1306 if (cm->cm_flags & AAC_CMD_DATAOUT) 1307 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1308 BUS_DMASYNC_PREWRITE); 1309 } 1310 cm->cm_flags |= AAC_CMD_MAPPED; 1311 } 1312 1313 /* 1314 * Unmap a command from controller-visible space. 1315 */ 1316 static void 1317 aac_unmap_command(struct aac_command *cm) 1318 { 1319 struct aac_softc *sc; 1320 1321 debug_called(2); 1322 1323 sc = cm->cm_sc; 1324 1325 if (!(cm->cm_flags & AAC_CMD_MAPPED)) 1326 return; 1327 1328 if (cm->cm_datalen != 0) { 1329 if (cm->cm_flags & AAC_CMD_DATAIN) 1330 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1331 BUS_DMASYNC_POSTREAD); 1332 if (cm->cm_flags & AAC_CMD_DATAOUT) 1333 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1334 BUS_DMASYNC_POSTWRITE); 1335 1336 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap); 1337 } 1338 cm->cm_flags &= ~AAC_CMD_MAPPED; 1339 } 1340 1341 /* 1342 * Hardware Interface 1343 */ 1344 1345 /* 1346 * Initialise the adapter. 1347 */ 1348 static void 1349 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1350 { 1351 struct aac_softc *sc; 1352 1353 debug_called(1); 1354 1355 sc = (struct aac_softc *)arg; 1356 1357 sc->aac_common_busaddr = segs[0].ds_addr; 1358 } 1359 1360 static int 1361 aac_check_firmware(struct aac_softc *sc) 1362 { 1363 u_int32_t major, minor, options; 1364 1365 debug_called(1); 1366 1367 /* 1368 * Retrieve the firmware version numbers. Dell PERC2/QC cards with 1369 * firmware version 1.x are not compatible with this driver. 1370 */ 1371 if (sc->flags & AAC_FLAGS_PERC2QC) { 1372 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0, 1373 NULL)) { 1374 device_printf(sc->aac_dev, 1375 "Error reading firmware version\n"); 1376 return (EIO); 1377 } 1378 1379 /* These numbers are stored as ASCII! */ 1380 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30; 1381 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30; 1382 if (major == 1) { 1383 device_printf(sc->aac_dev, 1384 "Firmware version %d.%d is not supported.\n", 1385 major, minor); 1386 return (EINVAL); 1387 } 1388 } 1389 1390 /* 1391 * Retrieve the capabilities/supported options word so we know what 1392 * work-arounds to enable. 1393 */ 1394 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) { 1395 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 1396 return (EIO); 1397 } 1398 options = AAC_GET_MAILBOX(sc, 1); 1399 sc->supported_options = options; 1400 1401 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 && 1402 (sc->flags & AAC_FLAGS_NO4GB) == 0) 1403 sc->flags |= AAC_FLAGS_4GB_WINDOW; 1404 if (options & AAC_SUPPORTED_NONDASD) 1405 sc->flags |= AAC_FLAGS_ENABLE_CAM; 1406 1407 return (0); 1408 } 1409 1410 static int 1411 aac_init(struct aac_softc *sc) 1412 { 1413 struct aac_adapter_init *ip; 1414 time_t then; 1415 u_int32_t code; 1416 u_int8_t *qaddr; 1417 int error; 1418 1419 debug_called(1); 1420 1421 /* 1422 * First wait for the adapter to come ready. 1423 */ 1424 then = time_second; 1425 do { 1426 code = AAC_GET_FWSTATUS(sc); 1427 if (code & AAC_SELF_TEST_FAILED) { 1428 device_printf(sc->aac_dev, "FATAL: selftest failed\n"); 1429 return(ENXIO); 1430 } 1431 if (code & AAC_KERNEL_PANIC) { 1432 device_printf(sc->aac_dev, 1433 "FATAL: controller kernel panic\n"); 1434 return(ENXIO); 1435 } 1436 if (time_second > (then + AAC_BOOT_TIMEOUT)) { 1437 device_printf(sc->aac_dev, 1438 "FATAL: controller not coming ready, " 1439 "status %x\n", code); 1440 return(ENXIO); 1441 } 1442 } while (!(code & AAC_UP_AND_RUNNING)); 1443 1444 error = ENOMEM; 1445 /* 1446 * Create DMA tag for mapping buffers into controller-addressable space. 1447 */ 1448 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1449 1, 0, /* algnmnt, boundary */ 1450 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1451 BUS_SPACE_MAXADDR, /* highaddr */ 1452 NULL, NULL, /* filter, filterarg */ 1453 MAXBSIZE, /* maxsize */ 1454 AAC_MAXSGENTRIES, /* nsegments */ 1455 MAXBSIZE, /* maxsegsize */ 1456 BUS_DMA_ALLOCNOW, /* flags */ 1457 &sc->aac_buffer_dmat)) { 1458 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n"); 1459 goto out; 1460 } 1461 1462 /* 1463 * Create DMA tag for mapping FIBs into controller-addressable space.. 1464 */ 1465 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1466 1, 0, /* algnmnt, boundary */ 1467 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1468 BUS_SPACE_MAXADDR_32BIT : 1469 0x7fffffff, /* lowaddr */ 1470 BUS_SPACE_MAXADDR, /* highaddr */ 1471 NULL, NULL, /* filter, filterarg */ 1472 AAC_FIB_COUNT * 1473 sizeof(struct aac_fib), /* maxsize */ 1474 1, /* nsegments */ 1475 AAC_FIB_COUNT * 1476 sizeof(struct aac_fib), /* maxsegsize */ 1477 BUS_DMA_ALLOCNOW, /* flags */ 1478 &sc->aac_fib_dmat)) { 1479 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n"); 1480 goto out; 1481 } 1482 1483 /* 1484 * Create DMA tag for the common structure and allocate it. 1485 */ 1486 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1487 1, 0, /* algnmnt, boundary */ 1488 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1489 BUS_SPACE_MAXADDR_32BIT : 1490 0x7fffffff, /* lowaddr */ 1491 BUS_SPACE_MAXADDR, /* highaddr */ 1492 NULL, NULL, /* filter, filterarg */ 1493 8192 + sizeof(struct aac_common), /* maxsize */ 1494 1, /* nsegments */ 1495 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1496 BUS_DMA_ALLOCNOW, /* flags */ 1497 &sc->aac_common_dmat)) { 1498 device_printf(sc->aac_dev, 1499 "can't allocate common structure DMA tag\n"); 1500 goto out; 1501 } 1502 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common, 1503 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) { 1504 device_printf(sc->aac_dev, "can't allocate common structure\n"); 1505 goto out; 1506 } 1507 /* 1508 * Work around a bug in the 2120 and 2200 that cannot DMA commands 1509 * below address 8192 in physical memory. 1510 * XXX If the padding is not needed, can it be put to use instead 1511 * of ignored? 1512 */ 1513 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap, 1514 sc->aac_common, 8192 + sizeof(*sc->aac_common), 1515 aac_common_map, sc, 0); 1516 1517 if (sc->aac_common_busaddr < 8192) { 1518 sc->aac_common = 1519 (struct aac_common *)((uint8_t *)sc->aac_common + 8192); 1520 sc->aac_common_busaddr += 8192; 1521 } 1522 bzero(sc->aac_common, sizeof(*sc->aac_common)); 1523 1524 /* Allocate some FIBs and associated command structs */ 1525 if (aac_alloc_commands(sc) != 0) 1526 goto out; 1527 1528 /* 1529 * Fill in the init structure. This tells the adapter about the 1530 * physical location of various important shared data structures. 1531 */ 1532 ip = &sc->aac_common->ac_init; 1533 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION; 1534 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION; 1535 1536 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr + 1537 offsetof(struct aac_common, ac_fibs); 1538 ip->AdapterFibsVirtualAddress = (aac_phys_addr_t)&sc->aac_common->ac_fibs[0]; 1539 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib); 1540 ip->AdapterFibAlign = sizeof(struct aac_fib); 1541 1542 ip->PrintfBufferAddress = sc->aac_common_busaddr + 1543 offsetof(struct aac_common, ac_printf); 1544 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE; 1545 1546 /* The adapter assumes that pages are 4K in size */ 1547 /* XXX why should the adapter care? */ 1548 ip->HostPhysMemPages = ctob((int)Maxmem) / AAC_PAGE_SIZE; 1549 ip->HostElapsedSeconds = time_second; /* reset later if invalid */ 1550 1551 /* 1552 * Initialise FIB queues. Note that it appears that the layout of the 1553 * indexes and the segmentation of the entries may be mandated by the 1554 * adapter, which is only told about the base of the queue index fields. 1555 * 1556 * The initial values of the indices are assumed to inform the adapter 1557 * of the sizes of the respective queues, and theoretically it could 1558 * work out the entire layout of the queue structures from this. We 1559 * take the easy route and just lay this area out like everyone else 1560 * does. 1561 * 1562 * The Linux driver uses a much more complex scheme whereby several 1563 * header records are kept for each queue. We use a couple of generic 1564 * list manipulation functions which 'know' the size of each list by 1565 * virtue of a table. 1566 */ 1567 qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN; 1568 qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN; 1569 sc->aac_queues = (struct aac_queue_table *)qaddr; 1570 ip->CommHeaderAddress = sc->aac_common_busaddr + 1571 ((u_int32_t)sc->aac_queues - 1572 (u_int32_t)sc->aac_common); 1573 bzero(sc->aac_queues, sizeof(struct aac_queue_table)); 1574 1575 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1576 AAC_HOST_NORM_CMD_ENTRIES; 1577 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1578 AAC_HOST_NORM_CMD_ENTRIES; 1579 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1580 AAC_HOST_HIGH_CMD_ENTRIES; 1581 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1582 AAC_HOST_HIGH_CMD_ENTRIES; 1583 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1584 AAC_ADAP_NORM_CMD_ENTRIES; 1585 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1586 AAC_ADAP_NORM_CMD_ENTRIES; 1587 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1588 AAC_ADAP_HIGH_CMD_ENTRIES; 1589 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1590 AAC_ADAP_HIGH_CMD_ENTRIES; 1591 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1592 AAC_HOST_NORM_RESP_ENTRIES; 1593 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1594 AAC_HOST_NORM_RESP_ENTRIES; 1595 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1596 AAC_HOST_HIGH_RESP_ENTRIES; 1597 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1598 AAC_HOST_HIGH_RESP_ENTRIES; 1599 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1600 AAC_ADAP_NORM_RESP_ENTRIES; 1601 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1602 AAC_ADAP_NORM_RESP_ENTRIES; 1603 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1604 AAC_ADAP_HIGH_RESP_ENTRIES; 1605 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1606 AAC_ADAP_HIGH_RESP_ENTRIES; 1607 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] = 1608 &sc->aac_queues->qt_HostNormCmdQueue[0]; 1609 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] = 1610 &sc->aac_queues->qt_HostHighCmdQueue[0]; 1611 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] = 1612 &sc->aac_queues->qt_AdapNormCmdQueue[0]; 1613 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] = 1614 &sc->aac_queues->qt_AdapHighCmdQueue[0]; 1615 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] = 1616 &sc->aac_queues->qt_HostNormRespQueue[0]; 1617 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] = 1618 &sc->aac_queues->qt_HostHighRespQueue[0]; 1619 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] = 1620 &sc->aac_queues->qt_AdapNormRespQueue[0]; 1621 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] = 1622 &sc->aac_queues->qt_AdapHighRespQueue[0]; 1623 1624 /* 1625 * Do controller-type-specific initialisation 1626 */ 1627 switch (sc->aac_hwif) { 1628 case AAC_HWIF_I960RX: 1629 AAC_SETREG4(sc, AAC_RX_ODBR, ~0); 1630 break; 1631 } 1632 1633 /* 1634 * Give the init structure to the controller. 1635 */ 1636 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT, 1637 sc->aac_common_busaddr + 1638 offsetof(struct aac_common, ac_init), 0, 0, 0, 1639 NULL)) { 1640 device_printf(sc->aac_dev, 1641 "error establishing init structure\n"); 1642 error = EIO; 1643 goto out; 1644 } 1645 1646 error = 0; 1647 out: 1648 return(error); 1649 } 1650 1651 /* 1652 * Send a synchronous command to the controller and wait for a result. 1653 */ 1654 static int 1655 aac_sync_command(struct aac_softc *sc, u_int32_t command, 1656 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3, 1657 u_int32_t *sp) 1658 { 1659 time_t then; 1660 u_int32_t status; 1661 1662 debug_called(3); 1663 1664 /* populate the mailbox */ 1665 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3); 1666 1667 /* ensure the sync command doorbell flag is cleared */ 1668 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1669 1670 /* then set it to signal the adapter */ 1671 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND); 1672 1673 /* spin waiting for the command to complete */ 1674 then = time_second; 1675 do { 1676 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) { 1677 debug(1, "timed out"); 1678 return(EIO); 1679 } 1680 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND)); 1681 1682 /* clear the completion flag */ 1683 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1684 1685 /* get the command status */ 1686 status = AAC_GET_MAILBOX(sc, 0); 1687 if (sp != NULL) 1688 *sp = status; 1689 return(0); 1690 } 1691 1692 /* 1693 * Grab the sync fib area. 1694 */ 1695 int 1696 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags) 1697 { 1698 1699 /* 1700 * If the force flag is set, the system is shutting down, or in 1701 * trouble. Ignore the mutex. 1702 */ 1703 if (!(flags & AAC_SYNC_LOCK_FORCE)) 1704 AAC_LOCK_ACQUIRE(&sc->aac_sync_lock); 1705 1706 *fib = &sc->aac_common->ac_sync_fib; 1707 1708 return (1); 1709 } 1710 1711 /* 1712 * Release the sync fib area. 1713 */ 1714 void 1715 aac_release_sync_fib(struct aac_softc *sc) 1716 { 1717 1718 AAC_LOCK_RELEASE(&sc->aac_sync_lock); 1719 } 1720 1721 /* 1722 * Send a synchronous FIB to the controller and wait for a result. 1723 */ 1724 int 1725 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate, 1726 struct aac_fib *fib, u_int16_t datasize) 1727 { 1728 debug_called(3); 1729 1730 if (datasize > AAC_FIB_DATASIZE) 1731 return(EINVAL); 1732 1733 /* 1734 * Set up the sync FIB 1735 */ 1736 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED | 1737 AAC_FIBSTATE_INITIALISED | 1738 AAC_FIBSTATE_EMPTY; 1739 fib->Header.XferState |= xferstate; 1740 fib->Header.Command = command; 1741 fib->Header.StructType = AAC_FIBTYPE_TFIB; 1742 fib->Header.Size = sizeof(struct aac_fib) + datasize; 1743 fib->Header.SenderSize = sizeof(struct aac_fib); 1744 fib->Header.SenderFibAddress = (u_int32_t)fib; 1745 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr + 1746 offsetof(struct aac_common, 1747 ac_sync_fib); 1748 1749 /* 1750 * Give the FIB to the controller, wait for a response. 1751 */ 1752 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB, 1753 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) { 1754 debug(2, "IO error"); 1755 return(EIO); 1756 } 1757 1758 return (0); 1759 } 1760 1761 /* 1762 * Adapter-space FIB queue manipulation 1763 * 1764 * Note that the queue implementation here is a little funky; neither the PI or 1765 * CI will ever be zero. This behaviour is a controller feature. 1766 */ 1767 static struct { 1768 int size; 1769 int notify; 1770 } aac_qinfo[] = { 1771 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL}, 1772 {AAC_HOST_HIGH_CMD_ENTRIES, 0}, 1773 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY}, 1774 {AAC_ADAP_HIGH_CMD_ENTRIES, 0}, 1775 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL}, 1776 {AAC_HOST_HIGH_RESP_ENTRIES, 0}, 1777 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY}, 1778 {AAC_ADAP_HIGH_RESP_ENTRIES, 0} 1779 }; 1780 1781 /* 1782 * Atomically insert an entry into the nominated queue, returns 0 on success or 1783 * EBUSY if the queue is full. 1784 * 1785 * Note: it would be more efficient to defer notifying the controller in 1786 * the case where we may be inserting several entries in rapid succession, 1787 * but implementing this usefully may be difficult (it would involve a 1788 * separate queue/notify interface). 1789 */ 1790 static int 1791 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm) 1792 { 1793 u_int32_t pi, ci; 1794 int error; 1795 u_int32_t fib_size; 1796 u_int32_t fib_addr; 1797 1798 debug_called(3); 1799 1800 fib_size = cm->cm_fib->Header.Size; 1801 fib_addr = cm->cm_fib->Header.ReceiverFibAddress; 1802 1803 crit_enter(); 1804 1805 /* get the producer/consumer indices */ 1806 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1807 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1808 1809 /* wrap the queue? */ 1810 if (pi >= aac_qinfo[queue].size) 1811 pi = 0; 1812 1813 /* check for queue full */ 1814 if ((pi + 1) == ci) { 1815 error = EBUSY; 1816 goto out; 1817 } 1818 /* 1819 * To avoid a race with its completion interrupt, place this command on 1820 * the busy queue prior to advertising it to the controller. 1821 */ 1822 aac_enqueue_busy(cm); 1823 1824 1825 1826 /* populate queue entry */ 1827 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; 1828 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; 1829 1830 /* update producer index */ 1831 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; 1832 1833 /* notify the adapter if we know how */ 1834 if (aac_qinfo[queue].notify != 0) 1835 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1836 1837 error = 0; 1838 1839 out: 1840 crit_exit(); 1841 return(error); 1842 } 1843 1844 /* 1845 * Atomically remove one entry from the nominated queue, returns 0 on 1846 * success or ENOENT if the queue is empty. 1847 */ 1848 static int 1849 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size, 1850 struct aac_fib **fib_addr) 1851 { 1852 u_int32_t pi, ci; 1853 int error; 1854 int notify; 1855 1856 debug_called(3); 1857 1858 crit_enter(); 1859 1860 /* get the producer/consumer indices */ 1861 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1862 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1863 1864 /* check for queue empty */ 1865 if (ci == pi) { 1866 error = ENOENT; 1867 goto out; 1868 } 1869 1870 /* wrap the pi so the following test works */ 1871 if (pi >= aac_qinfo[queue].size) 1872 pi = 0; 1873 1874 notify = 0; 1875 if (ci == pi + 1) 1876 notify++; 1877 1878 /* wrap the queue? */ 1879 if (ci >= aac_qinfo[queue].size) 1880 ci = 0; 1881 1882 /* fetch the entry */ 1883 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size; 1884 *fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] + 1885 ci)->aq_fib_addr; 1886 1887 /* 1888 * Is this a fast response? If it is, update the fib fields in 1889 * local memory so the whole fib doesn't have to be DMA'd back up. 1890 */ 1891 if (*(uintptr_t *)fib_addr & 0x01) { 1892 *(uintptr_t *)fib_addr &= ~0x01; 1893 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP; 1894 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL; 1895 } 1896 /* update consumer index */ 1897 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1; 1898 1899 /* if we have made the queue un-full, notify the adapter */ 1900 if (notify && (aac_qinfo[queue].notify != 0)) 1901 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1902 error = 0; 1903 1904 out: 1905 crit_exit(); 1906 return(error); 1907 } 1908 1909 /* 1910 * Put our response to an Adapter Initialed Fib on the response queue 1911 */ 1912 static int 1913 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib) 1914 { 1915 u_int32_t pi, ci; 1916 int error; 1917 u_int32_t fib_size; 1918 u_int32_t fib_addr; 1919 1920 debug_called(1); 1921 1922 /* Tell the adapter where the FIB is */ 1923 fib_size = fib->Header.Size; 1924 fib_addr = fib->Header.SenderFibAddress; 1925 fib->Header.ReceiverFibAddress = fib_addr; 1926 1927 crit_enter(); 1928 1929 /* get the producer/consumer indices */ 1930 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1931 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1932 1933 /* wrap the queue? */ 1934 if (pi >= aac_qinfo[queue].size) 1935 pi = 0; 1936 1937 /* check for queue full */ 1938 if ((pi + 1) == ci) { 1939 error = EBUSY; 1940 goto out; 1941 } 1942 1943 /* populate queue entry */ 1944 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; 1945 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; 1946 1947 /* update producer index */ 1948 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; 1949 1950 /* notify the adapter if we know how */ 1951 if (aac_qinfo[queue].notify != 0) 1952 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1953 1954 error = 0; 1955 1956 out: 1957 crit_exit(); 1958 return(error); 1959 } 1960 1961 /* 1962 * Check for commands that have been outstanding for a suspiciously long time, 1963 * and complain about them. 1964 */ 1965 static void 1966 aac_timeout(void *xsc) 1967 { 1968 struct aac_softc *sc = xsc; 1969 struct aac_command *cm; 1970 time_t deadline; 1971 int timedout, code; 1972 #if 0 1973 /* simulate an interrupt to handle possibly-missed interrupts */ 1974 /* 1975 * XXX This was done to work around another bug which has since been 1976 * fixed. It is dangerous anyways because you don't want multiple 1977 * threads in the interrupt handler at the same time! If calling 1978 * is deamed neccesary in the future, proper mutexes must be used. 1979 */ 1980 crit_enter(); 1981 aac_intr(sc); 1982 crit_exit(); 1983 1984 /* kick the I/O queue to restart it in the case of deadlock */ 1985 aac_startio(sc); 1986 #endif 1987 1988 /* 1989 * traverse the busy command list, bitch about late commands once 1990 * only. 1991 */ 1992 timedout = 0; 1993 deadline = time_second - AAC_CMD_TIMEOUT; 1994 crit_enter(); 1995 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) { 1996 if ((cm->cm_timestamp < deadline) 1997 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) { 1998 cm->cm_flags |= AAC_CMD_TIMEDOUT; 1999 device_printf(sc->aac_dev, 2000 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", 2001 cm, (int)(time_second-cm->cm_timestamp)); 2002 AAC_PRINT_FIB(sc, cm->cm_fib); 2003 timedout++; 2004 } 2005 } 2006 if (timedout) { 2007 code = AAC_GET_FWSTATUS(sc); 2008 if (code != AAC_UP_AND_RUNNING) { 2009 device_printf(sc->aac_dev, "WARNING! Controller is no " 2010 "longer running! code= 0x%x\n", code); 2011 2012 } 2013 } 2014 crit_exit(); 2015 2016 /* reset the timer for next time */ 2017 callout_reset(&sc->aac_watchdog, AAC_PERIODIC_INTERVAL * hz, 2018 aac_timeout, sc); 2019 } 2020 2021 /* 2022 * Interface Function Vectors 2023 */ 2024 2025 /* 2026 * Read the current firmware status word. 2027 */ 2028 static int 2029 aac_sa_get_fwstatus(struct aac_softc *sc) 2030 { 2031 debug_called(3); 2032 2033 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS)); 2034 } 2035 2036 static int 2037 aac_rx_get_fwstatus(struct aac_softc *sc) 2038 { 2039 debug_called(3); 2040 2041 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS)); 2042 } 2043 2044 static int 2045 aac_fa_get_fwstatus(struct aac_softc *sc) 2046 { 2047 int val; 2048 2049 debug_called(3); 2050 2051 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS); 2052 return (val); 2053 } 2054 2055 /* 2056 * Notify the controller of a change in a given queue 2057 */ 2058 2059 static void 2060 aac_sa_qnotify(struct aac_softc *sc, int qbit) 2061 { 2062 debug_called(3); 2063 2064 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit); 2065 } 2066 2067 static void 2068 aac_rx_qnotify(struct aac_softc *sc, int qbit) 2069 { 2070 debug_called(3); 2071 2072 AAC_SETREG4(sc, AAC_RX_IDBR, qbit); 2073 } 2074 2075 static void 2076 aac_fa_qnotify(struct aac_softc *sc, int qbit) 2077 { 2078 debug_called(3); 2079 2080 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit); 2081 AAC_FA_HACK(sc); 2082 } 2083 2084 /* 2085 * Get the interrupt reason bits 2086 */ 2087 static int 2088 aac_sa_get_istatus(struct aac_softc *sc) 2089 { 2090 debug_called(3); 2091 2092 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0)); 2093 } 2094 2095 static int 2096 aac_rx_get_istatus(struct aac_softc *sc) 2097 { 2098 debug_called(3); 2099 2100 return(AAC_GETREG4(sc, AAC_RX_ODBR)); 2101 } 2102 2103 static int 2104 aac_fa_get_istatus(struct aac_softc *sc) 2105 { 2106 int val; 2107 2108 debug_called(3); 2109 2110 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0); 2111 return (val); 2112 } 2113 2114 /* 2115 * Clear some interrupt reason bits 2116 */ 2117 static void 2118 aac_sa_clear_istatus(struct aac_softc *sc, int mask) 2119 { 2120 debug_called(3); 2121 2122 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask); 2123 } 2124 2125 static void 2126 aac_rx_clear_istatus(struct aac_softc *sc, int mask) 2127 { 2128 debug_called(3); 2129 2130 AAC_SETREG4(sc, AAC_RX_ODBR, mask); 2131 } 2132 2133 static void 2134 aac_fa_clear_istatus(struct aac_softc *sc, int mask) 2135 { 2136 debug_called(3); 2137 2138 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask); 2139 AAC_FA_HACK(sc); 2140 } 2141 2142 /* 2143 * Populate the mailbox and set the command word 2144 */ 2145 static void 2146 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2147 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2148 { 2149 debug_called(4); 2150 2151 AAC_SETREG4(sc, AAC_SA_MAILBOX, command); 2152 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0); 2153 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1); 2154 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2); 2155 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3); 2156 } 2157 2158 static void 2159 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, 2160 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2161 { 2162 debug_called(4); 2163 2164 AAC_SETREG4(sc, AAC_RX_MAILBOX, command); 2165 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0); 2166 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1); 2167 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2); 2168 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3); 2169 } 2170 2171 static void 2172 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2173 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2174 { 2175 debug_called(4); 2176 2177 AAC_SETREG4(sc, AAC_FA_MAILBOX, command); 2178 AAC_FA_HACK(sc); 2179 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0); 2180 AAC_FA_HACK(sc); 2181 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1); 2182 AAC_FA_HACK(sc); 2183 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2); 2184 AAC_FA_HACK(sc); 2185 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3); 2186 AAC_FA_HACK(sc); 2187 } 2188 2189 /* 2190 * Fetch the immediate command status word 2191 */ 2192 static int 2193 aac_sa_get_mailbox(struct aac_softc *sc, int mb) 2194 { 2195 debug_called(4); 2196 2197 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4))); 2198 } 2199 2200 static int 2201 aac_rx_get_mailbox(struct aac_softc *sc, int mb) 2202 { 2203 debug_called(4); 2204 2205 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4))); 2206 } 2207 2208 static int 2209 aac_fa_get_mailbox(struct aac_softc *sc, int mb) 2210 { 2211 int val; 2212 2213 debug_called(4); 2214 2215 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4)); 2216 return (val); 2217 } 2218 2219 /* 2220 * Set/clear interrupt masks 2221 */ 2222 static void 2223 aac_sa_set_interrupts(struct aac_softc *sc, int enable) 2224 { 2225 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2226 2227 if (enable) { 2228 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2229 } else { 2230 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0); 2231 } 2232 } 2233 2234 static void 2235 aac_rx_set_interrupts(struct aac_softc *sc, int enable) 2236 { 2237 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2238 2239 if (enable) { 2240 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS); 2241 } else { 2242 AAC_SETREG4(sc, AAC_RX_OIMR, ~0); 2243 } 2244 } 2245 2246 static void 2247 aac_fa_set_interrupts(struct aac_softc *sc, int enable) 2248 { 2249 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2250 2251 if (enable) { 2252 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2253 AAC_FA_HACK(sc); 2254 } else { 2255 AAC_SETREG2((sc), AAC_FA_MASK0, ~0); 2256 AAC_FA_HACK(sc); 2257 } 2258 } 2259 2260 /* 2261 * Debugging and Diagnostics 2262 */ 2263 2264 /* 2265 * Print some information about the controller. 2266 */ 2267 static void 2268 aac_describe_controller(struct aac_softc *sc) 2269 { 2270 struct aac_fib *fib; 2271 struct aac_adapter_info *info; 2272 2273 debug_called(2); 2274 2275 aac_alloc_sync_fib(sc, &fib, 0); 2276 2277 fib->data[0] = 0; 2278 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) { 2279 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 2280 aac_release_sync_fib(sc); 2281 return; 2282 } 2283 info = (struct aac_adapter_info *)&fib->data[0]; 2284 2285 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n", 2286 aac_describe_code(aac_cpu_variant, info->CpuVariant), 2287 info->ClockSpeed, info->BufferMem / (1024 * 1024), 2288 aac_describe_code(aac_battery_platform, 2289 info->batteryPlatform)); 2290 2291 /* save the kernel revision structure for later use */ 2292 sc->aac_revision = info->KernelRevision; 2293 device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n", 2294 info->KernelRevision.external.comp.major, 2295 info->KernelRevision.external.comp.minor, 2296 info->KernelRevision.external.comp.dash, 2297 info->KernelRevision.buildNumber, 2298 (u_int32_t)(info->SerialNumber & 0xffffff)); 2299 2300 aac_release_sync_fib(sc); 2301 2302 if (1 || bootverbose) { 2303 device_printf(sc->aac_dev, "Supported Options=%b\n", 2304 sc->supported_options, 2305 "\20" 2306 "\1SNAPSHOT" 2307 "\2CLUSTERS" 2308 "\3WCACHE" 2309 "\4DATA64" 2310 "\5HOSTTIME" 2311 "\6RAID50" 2312 "\7WINDOW4GB" 2313 "\10SCSIUPGD" 2314 "\11SOFTERR" 2315 "\12NORECOND" 2316 "\13SGMAP64" 2317 "\14ALARM" 2318 "\15NONDASD"); 2319 } 2320 } 2321 2322 /* 2323 * Look up a text description of a numeric error code and return a pointer to 2324 * same. 2325 */ 2326 static char * 2327 aac_describe_code(struct aac_code_lookup *table, u_int32_t code) 2328 { 2329 int i; 2330 2331 for (i = 0; table[i].string != NULL; i++) 2332 if (table[i].code == code) 2333 return(table[i].string); 2334 return(table[i + 1].string); 2335 } 2336 2337 /* 2338 * Management Interface 2339 */ 2340 2341 static int 2342 aac_open(struct dev_open_args *ap) 2343 { 2344 cdev_t dev = ap->a_head.a_dev; 2345 struct aac_softc *sc; 2346 2347 debug_called(2); 2348 2349 sc = dev->si_drv1; 2350 2351 /* Check to make sure the device isn't already open */ 2352 if (sc->aac_state & AAC_STATE_OPEN) { 2353 return EBUSY; 2354 } 2355 sc->aac_state |= AAC_STATE_OPEN; 2356 2357 return 0; 2358 } 2359 2360 static int 2361 aac_close(struct dev_close_args *ap) 2362 { 2363 cdev_t dev = ap->a_head.a_dev; 2364 struct aac_softc *sc; 2365 2366 debug_called(2); 2367 2368 sc = dev->si_drv1; 2369 2370 /* Mark this unit as no longer open */ 2371 sc->aac_state &= ~AAC_STATE_OPEN; 2372 2373 return 0; 2374 } 2375 2376 static int 2377 aac_ioctl(struct dev_ioctl_args *ap) 2378 { 2379 cdev_t dev = ap->a_head.a_dev; 2380 caddr_t arg = ap->a_data; 2381 struct aac_softc *sc = dev->si_drv1; 2382 int error = 0; 2383 int i; 2384 2385 debug_called(2); 2386 2387 if (ap->a_cmd == AACIO_STATS) { 2388 union aac_statrequest *as = (union aac_statrequest *)arg; 2389 2390 switch (as->as_item) { 2391 case AACQ_FREE: 2392 case AACQ_BIO: 2393 case AACQ_READY: 2394 case AACQ_BUSY: 2395 case AACQ_COMPLETE: 2396 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat, 2397 sizeof(struct aac_qstat)); 2398 break; 2399 default: 2400 error = ENOENT; 2401 break; 2402 } 2403 return(error); 2404 } 2405 2406 arg = *(caddr_t *)arg; 2407 2408 switch (ap->a_cmd) { 2409 /* AACIO_STATS already handled above */ 2410 case FSACTL_SENDFIB: 2411 debug(1, "FSACTL_SENDFIB"); 2412 error = aac_ioctl_sendfib(sc, arg); 2413 break; 2414 case FSACTL_AIF_THREAD: 2415 debug(1, "FSACTL_AIF_THREAD"); 2416 error = EINVAL; 2417 break; 2418 case FSACTL_OPEN_GET_ADAPTER_FIB: 2419 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB"); 2420 /* 2421 * Pass the caller out an AdapterFibContext. 2422 * 2423 * Note that because we only support one opener, we 2424 * basically ignore this. Set the caller's context to a magic 2425 * number just in case. 2426 * 2427 * The Linux code hands the driver a pointer into kernel space, 2428 * and then trusts it when the caller hands it back. Aiee! 2429 * Here, we give it the proc pointer of the per-adapter aif 2430 * thread. It's only used as a sanity check in other calls. 2431 */ 2432 i = (int)sc->aifthread; 2433 error = copyout(&i, arg, sizeof(i)); 2434 break; 2435 case FSACTL_GET_NEXT_ADAPTER_FIB: 2436 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB"); 2437 error = aac_getnext_aif(sc, arg); 2438 break; 2439 case FSACTL_CLOSE_GET_ADAPTER_FIB: 2440 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB"); 2441 /* don't do anything here */ 2442 break; 2443 case FSACTL_MINIPORT_REV_CHECK: 2444 debug(1, "FSACTL_MINIPORT_REV_CHECK"); 2445 error = aac_rev_check(sc, arg); 2446 break; 2447 case FSACTL_QUERY_DISK: 2448 debug(1, "FSACTL_QUERY_DISK"); 2449 error = aac_query_disk(sc, arg); 2450 break; 2451 case FSACTL_DELETE_DISK: 2452 /* 2453 * We don't trust the underland to tell us when to delete a 2454 * container, rather we rely on an AIF coming from the 2455 * controller 2456 */ 2457 error = 0; 2458 break; 2459 default: 2460 debug(1, "unsupported cmd 0x%lx\n", ap->a_cmd); 2461 error = EINVAL; 2462 break; 2463 } 2464 return(error); 2465 } 2466 2467 static int 2468 aac_poll(struct dev_poll_args *ap) 2469 { 2470 cdev_t dev = ap->a_head.a_dev; 2471 struct aac_softc *sc; 2472 int revents; 2473 2474 sc = dev->si_drv1; 2475 revents = 0; 2476 2477 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); 2478 if ((ap->a_events & (POLLRDNORM | POLLIN)) != 0) { 2479 if (sc->aac_aifq_tail != sc->aac_aifq_head) 2480 revents |= ap->a_events & (POLLIN | POLLRDNORM); 2481 } 2482 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2483 2484 if (revents == 0) { 2485 if (ap->a_events & (POLLIN | POLLRDNORM)) 2486 selrecord(curthread, &sc->rcv_select); 2487 } 2488 ap->a_events = revents; 2489 return (0); 2490 } 2491 2492 /* 2493 * Send a FIB supplied from userspace 2494 */ 2495 static int 2496 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) 2497 { 2498 struct aac_command *cm; 2499 int size, error; 2500 2501 debug_called(2); 2502 2503 cm = NULL; 2504 2505 /* 2506 * Get a command 2507 */ 2508 if (aac_alloc_command(sc, &cm)) { 2509 error = EBUSY; 2510 goto out; 2511 } 2512 2513 /* 2514 * Fetch the FIB header, then re-copy to get data as well. 2515 */ 2516 if ((error = copyin(ufib, cm->cm_fib, 2517 sizeof(struct aac_fib_header))) != 0) 2518 goto out; 2519 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header); 2520 if (size > sizeof(struct aac_fib)) { 2521 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n", 2522 size, sizeof(struct aac_fib)); 2523 size = sizeof(struct aac_fib); 2524 } 2525 if ((error = copyin(ufib, cm->cm_fib, size)) != 0) 2526 goto out; 2527 cm->cm_fib->Header.Size = size; 2528 cm->cm_timestamp = time_second; 2529 2530 /* 2531 * Pass the FIB to the controller, wait for it to complete. 2532 */ 2533 if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */ 2534 kprintf("aac_wait_command return %d\n", error); 2535 goto out; 2536 } 2537 2538 /* 2539 * Copy the FIB and data back out to the caller. 2540 */ 2541 size = cm->cm_fib->Header.Size; 2542 if (size > sizeof(struct aac_fib)) { 2543 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n", 2544 size, sizeof(struct aac_fib)); 2545 size = sizeof(struct aac_fib); 2546 } 2547 error = copyout(cm->cm_fib, ufib, size); 2548 2549 out: 2550 if (cm != NULL) { 2551 aac_release_command(cm); 2552 } 2553 return(error); 2554 } 2555 2556 /* 2557 * Handle an AIF sent to us by the controller; queue it for later reference. 2558 * If the queue fills up, then drop the older entries. 2559 */ 2560 static void 2561 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib) 2562 { 2563 struct aac_aif_command *aif; 2564 struct aac_container *co, *co_next; 2565 struct aac_mntinfo *mi; 2566 struct aac_mntinforesp *mir = NULL; 2567 u_int16_t rsize; 2568 int next, found; 2569 int count = 0, added = 0, i = 0; 2570 2571 debug_called(2); 2572 2573 aif = (struct aac_aif_command*)&fib->data[0]; 2574 aac_print_aif(sc, aif); 2575 2576 /* Is it an event that we should care about? */ 2577 switch (aif->command) { 2578 case AifCmdEventNotify: 2579 switch (aif->data.EN.type) { 2580 case AifEnAddContainer: 2581 case AifEnDeleteContainer: 2582 /* 2583 * A container was added or deleted, but the message 2584 * doesn't tell us anything else! Re-enumerate the 2585 * containers and sort things out. 2586 */ 2587 aac_alloc_sync_fib(sc, &fib, 0); 2588 mi = (struct aac_mntinfo *)&fib->data[0]; 2589 do { 2590 /* 2591 * Ask the controller for its containers one at 2592 * a time. 2593 * XXX What if the controller's list changes 2594 * midway through this enumaration? 2595 * XXX This should be done async. 2596 */ 2597 bzero(mi, sizeof(struct aac_mntinfo)); 2598 mi->Command = VM_NameServe; 2599 mi->MntType = FT_FILESYS; 2600 mi->MntCount = i; 2601 rsize = sizeof(mir); 2602 if (aac_sync_fib(sc, ContainerCommand, 0, fib, 2603 sizeof(struct aac_mntinfo))) { 2604 device_printf(sc->aac_dev, 2605 "Error probing container %d\n", i); 2606 2607 continue; 2608 } 2609 mir = (struct aac_mntinforesp *)&fib->data[0]; 2610 /* XXX Need to check if count changed */ 2611 count = mir->MntRespCount; 2612 2613 /* 2614 * Check the container against our list. 2615 * co->co_found was already set to 0 in a 2616 * previous run. 2617 */ 2618 if ((mir->Status == ST_OK) && 2619 (mir->MntTable[0].VolType != CT_NONE)) { 2620 found = 0; 2621 TAILQ_FOREACH(co, 2622 &sc->aac_container_tqh, 2623 co_link) { 2624 if (co->co_mntobj.ObjectId == 2625 mir->MntTable[0].ObjectId) { 2626 co->co_found = 1; 2627 found = 1; 2628 break; 2629 } 2630 } 2631 /* 2632 * If the container matched, continue 2633 * in the list. 2634 */ 2635 if (found) { 2636 i++; 2637 continue; 2638 } 2639 2640 /* 2641 * This is a new container. Do all the 2642 * appropriate things to set it up. */ 2643 aac_add_container(sc, mir, 1); 2644 added = 1; 2645 } 2646 i++; 2647 } while ((i < count) && (i < AAC_MAX_CONTAINERS)); 2648 aac_release_sync_fib(sc); 2649 2650 /* 2651 * Go through our list of containers and see which ones 2652 * were not marked 'found'. Since the controller didn't 2653 * list them they must have been deleted. Do the 2654 * appropriate steps to destroy the device. Also reset 2655 * the co->co_found field. 2656 */ 2657 co = TAILQ_FIRST(&sc->aac_container_tqh); 2658 while (co != NULL) { 2659 if (co->co_found == 0) { 2660 device_delete_child(sc->aac_dev, 2661 co->co_disk); 2662 co_next = TAILQ_NEXT(co, co_link); 2663 AAC_LOCK_ACQUIRE(&sc-> 2664 aac_container_lock); 2665 TAILQ_REMOVE(&sc->aac_container_tqh, co, 2666 co_link); 2667 AAC_LOCK_RELEASE(&sc-> 2668 aac_container_lock); 2669 FREE(co, M_AACBUF); 2670 co = co_next; 2671 } else { 2672 co->co_found = 0; 2673 co = TAILQ_NEXT(co, co_link); 2674 } 2675 } 2676 2677 /* Attach the newly created containers */ 2678 if (added) 2679 bus_generic_attach(sc->aac_dev); 2680 2681 break; 2682 2683 default: 2684 break; 2685 } 2686 2687 default: 2688 break; 2689 } 2690 2691 /* Copy the AIF data to the AIF queue for ioctl retrieval */ 2692 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); 2693 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH; 2694 if (next != sc->aac_aifq_tail) { 2695 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command)); 2696 sc->aac_aifq_head = next; 2697 2698 /* On the off chance that someone is sleeping for an aif... */ 2699 if (sc->aac_state & AAC_STATE_AIF_SLEEPER) 2700 wakeup(sc->aac_aifq); 2701 /* token may have been lost */ 2702 /* Wakeup any poll()ers */ 2703 selwakeup(&sc->rcv_select); 2704 /* token may have been lost */ 2705 } 2706 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2707 2708 return; 2709 } 2710 2711 /* 2712 * Return the Revision of the driver to userspace and check to see if the 2713 * userspace app is possibly compatible. This is extremely bogus since 2714 * our driver doesn't follow Adaptec's versioning system. Cheat by just 2715 * returning what the card reported. 2716 */ 2717 static int 2718 aac_rev_check(struct aac_softc *sc, caddr_t udata) 2719 { 2720 struct aac_rev_check rev_check; 2721 struct aac_rev_check_resp rev_check_resp; 2722 int error = 0; 2723 2724 debug_called(2); 2725 2726 /* 2727 * Copyin the revision struct from userspace 2728 */ 2729 if ((error = copyin(udata, (caddr_t)&rev_check, 2730 sizeof(struct aac_rev_check))) != 0) { 2731 return error; 2732 } 2733 2734 debug(2, "Userland revision= %d\n", 2735 rev_check.callingRevision.buildNumber); 2736 2737 /* 2738 * Doctor up the response struct. 2739 */ 2740 rev_check_resp.possiblyCompatible = 1; 2741 rev_check_resp.adapterSWRevision.external.ul = 2742 sc->aac_revision.external.ul; 2743 rev_check_resp.adapterSWRevision.buildNumber = 2744 sc->aac_revision.buildNumber; 2745 2746 return(copyout((caddr_t)&rev_check_resp, udata, 2747 sizeof(struct aac_rev_check_resp))); 2748 } 2749 2750 /* 2751 * Pass the caller the next AIF in their queue 2752 */ 2753 static int 2754 aac_getnext_aif(struct aac_softc *sc, caddr_t arg) 2755 { 2756 struct get_adapter_fib_ioctl agf; 2757 int error; 2758 2759 debug_called(2); 2760 2761 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) { 2762 2763 /* 2764 * Check the magic number that we gave the caller. 2765 */ 2766 if (agf.AdapterFibContext != (int)sc->aifthread) { 2767 error = EFAULT; 2768 } else { 2769 2770 crit_enter(); 2771 error = aac_return_aif(sc, agf.AifFib); 2772 2773 if ((error == EAGAIN) && (agf.Wait)) { 2774 sc->aac_state |= AAC_STATE_AIF_SLEEPER; 2775 while (error == EAGAIN) { 2776 error = tsleep(sc->aac_aifq, 2777 PCATCH, "aacaif", 0); 2778 if (error == 0) 2779 error = aac_return_aif(sc, 2780 agf.AifFib); 2781 } 2782 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER; 2783 } 2784 crit_exit(); 2785 } 2786 } 2787 return(error); 2788 } 2789 2790 /* 2791 * Hand the next AIF off the top of the queue out to userspace. 2792 * 2793 * YYY token could be lost during copyout 2794 */ 2795 static int 2796 aac_return_aif(struct aac_softc *sc, caddr_t uptr) 2797 { 2798 int error; 2799 2800 debug_called(2); 2801 2802 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); 2803 if (sc->aac_aifq_tail == sc->aac_aifq_head) { 2804 error = EAGAIN; 2805 } else { 2806 error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr, 2807 sizeof(struct aac_aif_command)); 2808 if (error) 2809 kprintf("aac_return_aif: copyout returned %d\n", error); 2810 if (!error) 2811 sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) % 2812 AAC_AIFQ_LENGTH; 2813 } 2814 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2815 return(error); 2816 } 2817 2818 /* 2819 * Give the userland some information about the container. The AAC arch 2820 * expects the driver to be a SCSI passthrough type driver, so it expects 2821 * the containers to have b:t:l numbers. Fake it. 2822 */ 2823 static int 2824 aac_query_disk(struct aac_softc *sc, caddr_t uptr) 2825 { 2826 struct aac_query_disk query_disk; 2827 struct aac_container *co; 2828 struct aac_disk *disk; 2829 int error, id; 2830 2831 debug_called(2); 2832 2833 disk = NULL; 2834 2835 error = copyin(uptr, (caddr_t)&query_disk, 2836 sizeof(struct aac_query_disk)); 2837 if (error) 2838 return (error); 2839 2840 id = query_disk.ContainerNumber; 2841 if (id == -1) 2842 return (EINVAL); 2843 2844 AAC_LOCK_ACQUIRE(&sc->aac_container_lock); 2845 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) { 2846 if (co->co_mntobj.ObjectId == id) 2847 break; 2848 } 2849 2850 if (co == NULL) { 2851 query_disk.Valid = 0; 2852 query_disk.Locked = 0; 2853 query_disk.Deleted = 1; /* XXX is this right? */ 2854 } else { 2855 disk = device_get_softc(co->co_disk); 2856 query_disk.Valid = 1; 2857 query_disk.Locked = 2858 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0; 2859 query_disk.Deleted = 0; 2860 query_disk.Bus = device_get_unit(sc->aac_dev); 2861 query_disk.Target = disk->unit; 2862 query_disk.Lun = 0; 2863 query_disk.UnMapped = 0; 2864 bcopy(disk->ad_dev_t->si_name, 2865 &query_disk.diskDeviceName[0], 10); 2866 } 2867 AAC_LOCK_RELEASE(&sc->aac_container_lock); 2868 2869 error = copyout((caddr_t)&query_disk, uptr, 2870 sizeof(struct aac_query_disk)); 2871 2872 return (error); 2873 } 2874 2875 static void 2876 aac_get_bus_info(struct aac_softc *sc) 2877 { 2878 struct aac_fib *fib; 2879 struct aac_ctcfg *c_cmd; 2880 struct aac_ctcfg_resp *c_resp; 2881 struct aac_vmioctl *vmi; 2882 struct aac_vmi_businf_resp *vmi_resp; 2883 struct aac_getbusinf businfo; 2884 struct aac_cam_inf *caminf; 2885 device_t child; 2886 int i, found, error; 2887 2888 aac_alloc_sync_fib(sc, &fib, 0); 2889 c_cmd = (struct aac_ctcfg *)&fib->data[0]; 2890 bzero(c_cmd, sizeof(struct aac_ctcfg)); 2891 2892 c_cmd->Command = VM_ContainerConfig; 2893 c_cmd->cmd = CT_GET_SCSI_METHOD; 2894 c_cmd->param = 0; 2895 2896 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2897 sizeof(struct aac_ctcfg)); 2898 if (error) { 2899 device_printf(sc->aac_dev, "Error %d sending " 2900 "VM_ContainerConfig command\n", error); 2901 aac_release_sync_fib(sc); 2902 return; 2903 } 2904 2905 c_resp = (struct aac_ctcfg_resp *)&fib->data[0]; 2906 if (c_resp->Status != ST_OK) { 2907 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n", 2908 c_resp->Status); 2909 aac_release_sync_fib(sc); 2910 return; 2911 } 2912 2913 sc->scsi_method_id = c_resp->param; 2914 2915 vmi = (struct aac_vmioctl *)&fib->data[0]; 2916 bzero(vmi, sizeof(struct aac_vmioctl)); 2917 2918 vmi->Command = VM_Ioctl; 2919 vmi->ObjType = FT_DRIVE; 2920 vmi->MethId = sc->scsi_method_id; 2921 vmi->ObjId = 0; 2922 vmi->IoctlCmd = GetBusInfo; 2923 2924 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2925 sizeof(struct aac_vmioctl)); 2926 if (error) { 2927 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n", 2928 error); 2929 aac_release_sync_fib(sc); 2930 return; 2931 } 2932 2933 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0]; 2934 if (vmi_resp->Status != ST_OK) { 2935 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status); 2936 aac_release_sync_fib(sc); 2937 return; 2938 } 2939 2940 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf)); 2941 aac_release_sync_fib(sc); 2942 2943 found = 0; 2944 for (i = 0; i < businfo.BusCount; i++) { 2945 if (businfo.BusValid[i] != AAC_BUS_VALID) 2946 continue; 2947 2948 MALLOC(caminf, struct aac_cam_inf *, 2949 sizeof(struct aac_cam_inf), M_AACBUF, M_INTWAIT | M_ZERO); 2950 2951 child = device_add_child(sc->aac_dev, "aacp", -1); 2952 if (child == NULL) { 2953 device_printf(sc->aac_dev, "device_add_child failed\n"); 2954 continue; 2955 } 2956 2957 caminf->TargetsPerBus = businfo.TargetsPerBus; 2958 caminf->BusNumber = i; 2959 caminf->InitiatorBusId = businfo.InitiatorBusId[i]; 2960 caminf->aac_sc = sc; 2961 2962 device_set_ivars(child, caminf); 2963 device_set_desc(child, "SCSI Passthrough Bus"); 2964 2965 found = 1; 2966 } 2967 2968 if (found) 2969 bus_generic_attach(sc->aac_dev); 2970 2971 return; 2972 } 2973