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