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$ 30 */ 31 32 /* 33 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters. 34 */ 35 36 #include "opt_aac.h" 37 38 /* #include <stddef.h> */ 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/kthread.h> 44 #include <sys/sysctl.h> 45 #include <sys/poll.h> 46 #include <sys/ioccom.h> 47 48 #include <sys/bus.h> 49 #include <sys/conf.h> 50 #include <sys/signalvar.h> 51 #include <sys/time.h> 52 #include <sys/eventhandler.h> 53 54 #include <machine/bus_memio.h> 55 #include <machine/bus.h> 56 #include <machine/resource.h> 57 58 #include <dev/aac/aacreg.h> 59 #include <dev/aac/aac_ioctl.h> 60 #include <dev/aac/aacvar.h> 61 #include <dev/aac/aac_tables.h> 62 63 static void aac_startup(void *arg); 64 static void aac_add_container(struct aac_softc *sc, 65 struct aac_mntinforesp *mir, int f); 66 static void aac_get_bus_info(struct aac_softc *sc); 67 68 /* Command Processing */ 69 static void aac_timeout(struct aac_softc *sc); 70 static int aac_start(struct aac_command *cm); 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, int timeout); 75 static void aac_command_thread(struct aac_softc *sc); 76 77 /* Command Buffer Management */ 78 static void aac_map_command_helper(void *arg, bus_dma_segment_t *segs, 79 int nseg, int error); 80 static int aac_alloc_commands(struct aac_softc *sc); 81 static void aac_free_commands(struct aac_softc *sc); 82 static void aac_map_command(struct aac_command *cm); 83 static void aac_unmap_command(struct aac_command *cm); 84 85 /* Hardware Interface */ 86 static void aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, 87 int error); 88 static int aac_check_firmware(struct aac_softc *sc); 89 static int aac_init(struct aac_softc *sc); 90 static int aac_sync_command(struct aac_softc *sc, u_int32_t command, 91 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, 92 u_int32_t arg3, u_int32_t *sp); 93 static int aac_enqueue_fib(struct aac_softc *sc, int queue, 94 struct aac_command *cm); 95 static int aac_dequeue_fib(struct aac_softc *sc, int queue, 96 u_int32_t *fib_size, struct aac_fib **fib_addr); 97 static int aac_enqueue_response(struct aac_softc *sc, int queue, 98 struct aac_fib *fib); 99 100 /* Falcon/PPC interface */ 101 static int aac_fa_get_fwstatus(struct aac_softc *sc); 102 static void aac_fa_qnotify(struct aac_softc *sc, int qbit); 103 static int aac_fa_get_istatus(struct aac_softc *sc); 104 static void aac_fa_clear_istatus(struct aac_softc *sc, int mask); 105 static void aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, 106 u_int32_t arg0, u_int32_t arg1, 107 u_int32_t arg2, u_int32_t arg3); 108 static int aac_fa_get_mailbox(struct aac_softc *sc, int mb); 109 static void aac_fa_set_interrupts(struct aac_softc *sc, int enable); 110 111 struct aac_interface aac_fa_interface = { 112 aac_fa_get_fwstatus, 113 aac_fa_qnotify, 114 aac_fa_get_istatus, 115 aac_fa_clear_istatus, 116 aac_fa_set_mailbox, 117 aac_fa_get_mailbox, 118 aac_fa_set_interrupts 119 }; 120 121 /* StrongARM interface */ 122 static int aac_sa_get_fwstatus(struct aac_softc *sc); 123 static void aac_sa_qnotify(struct aac_softc *sc, int qbit); 124 static int aac_sa_get_istatus(struct aac_softc *sc); 125 static void aac_sa_clear_istatus(struct aac_softc *sc, int mask); 126 static void aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, 127 u_int32_t arg0, u_int32_t arg1, 128 u_int32_t arg2, u_int32_t arg3); 129 static int aac_sa_get_mailbox(struct aac_softc *sc, int mb); 130 static void aac_sa_set_interrupts(struct aac_softc *sc, int enable); 131 132 struct aac_interface aac_sa_interface = { 133 aac_sa_get_fwstatus, 134 aac_sa_qnotify, 135 aac_sa_get_istatus, 136 aac_sa_clear_istatus, 137 aac_sa_set_mailbox, 138 aac_sa_get_mailbox, 139 aac_sa_set_interrupts 140 }; 141 142 /* i960Rx interface */ 143 static int aac_rx_get_fwstatus(struct aac_softc *sc); 144 static void aac_rx_qnotify(struct aac_softc *sc, int qbit); 145 static int aac_rx_get_istatus(struct aac_softc *sc); 146 static void aac_rx_clear_istatus(struct aac_softc *sc, int mask); 147 static void aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, 148 u_int32_t arg0, u_int32_t arg1, 149 u_int32_t arg2, u_int32_t arg3); 150 static int aac_rx_get_mailbox(struct aac_softc *sc, int mb); 151 static void aac_rx_set_interrupts(struct aac_softc *sc, int enable); 152 153 struct aac_interface aac_rx_interface = { 154 aac_rx_get_fwstatus, 155 aac_rx_qnotify, 156 aac_rx_get_istatus, 157 aac_rx_clear_istatus, 158 aac_rx_set_mailbox, 159 aac_rx_get_mailbox, 160 aac_rx_set_interrupts 161 }; 162 163 /* Debugging and Diagnostics */ 164 static void aac_describe_controller(struct aac_softc *sc); 165 static char *aac_describe_code(struct aac_code_lookup *table, 166 u_int32_t code); 167 168 /* Management Interface */ 169 static d_open_t aac_open; 170 static d_close_t aac_close; 171 static d_ioctl_t aac_ioctl; 172 static d_poll_t aac_poll; 173 static int aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib); 174 static void aac_handle_aif(struct aac_softc *sc, 175 struct aac_fib *fib); 176 static int aac_rev_check(struct aac_softc *sc, caddr_t udata); 177 static int aac_getnext_aif(struct aac_softc *sc, caddr_t arg); 178 static int aac_return_aif(struct aac_softc *sc, caddr_t uptr); 179 static int aac_query_disk(struct aac_softc *sc, caddr_t uptr); 180 181 #define AAC_CDEV_MAJOR 150 182 183 static struct cdevsw aac_cdevsw = { 184 .d_open = aac_open, 185 .d_close = aac_close, 186 .d_ioctl = aac_ioctl, 187 .d_poll = aac_poll, 188 .d_name = "aac", 189 .d_maj = AAC_CDEV_MAJOR, 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 /* Init the sync fib lock */ 237 AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock"); 238 239 /* 240 * Initialise the adapter. 241 */ 242 if ((error = aac_init(sc)) != 0) 243 return(error); 244 245 /* 246 * Print a little information about the controller. 247 */ 248 aac_describe_controller(sc); 249 250 /* 251 * Initialize locks 252 */ 253 AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock"); 254 TAILQ_INIT(&sc->aac_container_tqh); 255 AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock"); 256 AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock"); 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, 0); 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, AAC_SYNC_LOCK_FORCE); 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_int32_t *resp_queue; 605 u_int16_t reason; 606 607 debug_called(2); 608 609 sc = (struct aac_softc *)arg; 610 611 /* 612 * Optimize the common case of adapter response interrupts. 613 * We must read from the card prior to processing the responses 614 * to ensure the clear is flushed prior to accessing the queues. 615 * Reading the queues from local memory might save us a PCI read. 616 */ 617 resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE]; 618 if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX]) 619 reason = AAC_DB_RESPONSE_READY; 620 else 621 reason = AAC_GET_ISTATUS(sc); 622 AAC_CLEAR_ISTATUS(sc, reason); 623 (void)AAC_GET_ISTATUS(sc); 624 625 /* It's not ok to return here because of races with the previous step */ 626 if (reason & AAC_DB_RESPONSE_READY) 627 /* handle completion processing */ 628 taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete); 629 630 /* controller wants to talk to the log */ 631 if (reason & AAC_DB_PRINTF) { 632 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { 633 sc->aifflags |= AAC_AIFFLAGS_PRINTF; 634 } else 635 aac_print_printf(sc); 636 } 637 638 /* controller has a message for us? */ 639 if (reason & AAC_DB_COMMAND_READY) { 640 if (sc->aifflags & AAC_AIFFLAGS_RUNNING) { 641 sc->aifflags |= AAC_AIFFLAGS_AIF; 642 } else { 643 /* 644 * XXX If the kthread is dead and we're at this point, 645 * there are bigger problems than just figuring out 646 * what to do with an AIF. 647 */ 648 } 649 650 } 651 652 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) != 0) 653 /* XXX Should this be done with cv_signal? */ 654 wakeup(sc->aifthread); 655 } 656 657 /* 658 * Command Processing 659 */ 660 661 /* 662 * Start as much queued I/O as possible on the controller 663 */ 664 void 665 aac_startio(struct aac_softc *sc) 666 { 667 struct aac_command *cm; 668 669 debug_called(2); 670 671 for (;;) { 672 /* 673 * Try to get a command that's been put off for lack of 674 * resources 675 */ 676 cm = aac_dequeue_ready(sc); 677 678 /* 679 * Try to build a command off the bio queue (ignore error 680 * return) 681 */ 682 if (cm == NULL) 683 aac_bio_command(sc, &cm); 684 685 /* nothing to do? */ 686 if (cm == NULL) 687 break; 688 689 /* try to give the command to the controller */ 690 if (aac_start(cm) == EBUSY) { 691 /* put it on the ready queue for later */ 692 aac_requeue_ready(cm); 693 break; 694 } 695 } 696 } 697 698 /* 699 * Deliver a command to the controller; allocate controller resources at the 700 * last moment when possible. 701 */ 702 static int 703 aac_start(struct aac_command *cm) 704 { 705 struct aac_softc *sc; 706 int error; 707 708 debug_called(2); 709 710 sc = cm->cm_sc; 711 712 /* get the command mapped */ 713 aac_map_command(cm); 714 715 /* Fix up the address values in the FIB. Use the command array index 716 * instead of a pointer since these fields are only 32 bits. Shift 717 * the SenderFibAddress over to make room for the fast response bit. 718 */ 719 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 1); 720 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys; 721 722 /* save a pointer to the command for speedy reverse-lookup */ 723 cm->cm_fib->Header.SenderData = cm->cm_index; 724 /* put the FIB on the outbound queue */ 725 error = aac_enqueue_fib(sc, cm->cm_queue, cm); 726 return(error); 727 } 728 729 /* 730 * Handle notification of one or more FIBs coming from the controller. 731 */ 732 static void 733 aac_command_thread(struct aac_softc *sc) 734 { 735 struct aac_fib *fib; 736 u_int32_t fib_size; 737 int size; 738 739 debug_called(2); 740 741 sc->aifflags |= AAC_AIFFLAGS_RUNNING; 742 743 while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) { 744 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) 745 tsleep(sc->aifthread, PRIBIO, "aifthd", 746 AAC_PERIODIC_INTERVAL * hz); 747 748 if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0) 749 aac_timeout(sc); 750 751 /* Check the hardware printf message buffer */ 752 if ((sc->aifflags & AAC_AIFFLAGS_PRINTF) != 0) { 753 sc->aifflags &= ~AAC_AIFFLAGS_PRINTF; 754 aac_print_printf(sc); 755 } 756 757 /* See if any FIBs need to be allocated */ 758 if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) { 759 AAC_LOCK_ACQUIRE(&sc->aac_io_lock); 760 aac_alloc_commands(sc); 761 sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS; 762 AAC_LOCK_RELEASE(&sc->aac_io_lock); 763 } 764 765 /* While we're here, check to see if any commands are stuck */ 766 while (sc->aifflags & AAC_AIFFLAGS_AIF) { 767 if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE, 768 &fib_size, &fib)) { 769 sc->aifflags &= ~AAC_AIFFLAGS_AIF; 770 break; /* nothing to do */ 771 } 772 773 AAC_PRINT_FIB(sc, fib); 774 775 switch (fib->Header.Command) { 776 case AifRequest: 777 aac_handle_aif(sc, fib); 778 break; 779 default: 780 device_printf(sc->aac_dev, "unknown command " 781 "from controller\n"); 782 break; 783 } 784 785 if ((fib->Header.XferState == 0) || 786 (fib->Header.StructType != AAC_FIBTYPE_TFIB)) 787 break; 788 789 /* Return the AIF to the controller. */ 790 if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) { 791 fib->Header.XferState |= AAC_FIBSTATE_DONEHOST; 792 *(AAC_FSAStatus*)fib->data = ST_OK; 793 794 /* XXX Compute the Size field? */ 795 size = fib->Header.Size; 796 if (size > sizeof(struct aac_fib)) { 797 size = sizeof(struct aac_fib); 798 fib->Header.Size = size; 799 } 800 /* 801 * Since we did not generate this command, it 802 * cannot go through the normal 803 * enqueue->startio chain. 804 */ 805 aac_enqueue_response(sc, 806 AAC_ADAP_NORM_RESP_QUEUE, 807 fib); 808 } 809 } 810 } 811 sc->aifflags &= ~AAC_AIFFLAGS_RUNNING; 812 wakeup(sc->aac_dev); 813 814 mtx_lock(&Giant); 815 kthread_exit(0); 816 } 817 818 /* 819 * Process completed commands. 820 */ 821 static void 822 aac_complete(void *context, int pending) 823 { 824 struct aac_softc *sc; 825 struct aac_command *cm; 826 struct aac_fib *fib; 827 u_int32_t fib_size; 828 829 debug_called(2); 830 831 sc = (struct aac_softc *)context; 832 833 AAC_LOCK_ACQUIRE(&sc->aac_io_lock); 834 835 /* pull completed commands off the queue */ 836 for (;;) { 837 /* look for completed FIBs on our queue */ 838 if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size, 839 &fib)) 840 break; /* nothing to do */ 841 842 /* get the command, unmap and queue for later processing */ 843 cm = sc->aac_commands + fib->Header.SenderData; 844 if (cm == NULL) { 845 AAC_PRINT_FIB(sc, fib); 846 break; 847 } 848 849 aac_remove_busy(cm); 850 aac_unmap_command(cm); /* XXX defer? */ 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 AAC_LOCK_RELEASE(&sc->aac_io_lock); 866 } 867 868 /* 869 * Handle a bio submitted from a disk device. 870 */ 871 void 872 aac_submit_bio(struct bio *bp) 873 { 874 struct aac_disk *ad; 875 struct aac_softc *sc; 876 877 debug_called(2); 878 879 ad = (struct aac_disk *)bp->bio_disk->d_drv1; 880 sc = ad->ad_controller; 881 882 /* queue the BIO and try to get some work done */ 883 aac_enqueue_bio(sc, bp); 884 aac_startio(sc); 885 } 886 887 /* 888 * Get a bio and build a command to go with it. 889 */ 890 static int 891 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp) 892 { 893 struct aac_command *cm; 894 struct aac_fib *fib; 895 struct aac_disk *ad; 896 struct bio *bp; 897 898 debug_called(2); 899 900 /* get the resources we will need */ 901 cm = NULL; 902 if ((bp = 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 cm->cm_data = (void *)bp->bio_data; 909 cm->cm_datalen = bp->bio_bcount; 910 cm->cm_complete = aac_bio_complete; 911 cm->cm_private = bp; 912 cm->cm_timestamp = time_second; 913 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 914 915 /* build the FIB */ 916 fib = cm->cm_fib; 917 fib->Header.Size = sizeof(struct aac_fib_header); 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 928 /* build the read/write request */ 929 ad = (struct aac_disk *)bp->bio_disk->d_drv1; 930 931 if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) { 932 fib->Header.Command = ContainerCommand; 933 if (bp->bio_cmd == BIO_READ) { 934 struct aac_blockread *br; 935 br = (struct aac_blockread *)&fib->data[0]; 936 br->Command = VM_CtBlockRead; 937 br->ContainerId = ad->ad_container->co_mntobj.ObjectId; 938 br->BlockNumber = bp->bio_pblkno; 939 br->ByteCount = bp->bio_bcount; 940 fib->Header.Size += sizeof(struct aac_blockread); 941 cm->cm_sgtable = &br->SgMap; 942 cm->cm_flags |= AAC_CMD_DATAIN; 943 } else { 944 struct aac_blockwrite *bw; 945 bw = (struct aac_blockwrite *)&fib->data[0]; 946 bw->Command = VM_CtBlockWrite; 947 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 948 bw->BlockNumber = bp->bio_pblkno; 949 bw->ByteCount = bp->bio_bcount; 950 bw->Stable = CUNSTABLE; 951 fib->Header.Size += sizeof(struct aac_blockwrite); 952 cm->cm_flags |= AAC_CMD_DATAOUT; 953 cm->cm_sgtable = &bw->SgMap; 954 } 955 } else { 956 fib->Header.Command = ContainerCommand64; 957 if (bp->bio_cmd == BIO_READ) { 958 struct aac_blockread64 *br; 959 br = (struct aac_blockread64 *)&fib->data[0]; 960 br->Command = VM_CtHostRead64; 961 br->ContainerId = ad->ad_container->co_mntobj.ObjectId; 962 br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE; 963 br->BlockNumber = bp->bio_pblkno; 964 br->Pad = 0; 965 br->Flags = 0; 966 fib->Header.Size += sizeof(struct aac_blockread64); 967 cm->cm_flags |= AAC_CMD_DATAOUT; 968 (struct aac_sg_table64 *)cm->cm_sgtable = &br->SgMap64; 969 } else { 970 struct aac_blockwrite64 *bw; 971 bw = (struct aac_blockwrite64 *)&fib->data[0]; 972 bw->Command = VM_CtHostWrite64; 973 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 974 bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE; 975 bw->BlockNumber = bp->bio_pblkno; 976 bw->Pad = 0; 977 bw->Flags = 0; 978 fib->Header.Size += sizeof(struct aac_blockwrite64); 979 cm->cm_flags |= AAC_CMD_DATAIN; 980 (struct aac_sg_table64 *)cm->cm_sgtable = &bw->SgMap64; 981 } 982 } 983 984 *cmp = cm; 985 return(0); 986 987 fail: 988 if (bp != NULL) 989 aac_enqueue_bio(sc, bp); 990 if (cm != NULL) 991 aac_release_command(cm); 992 return(ENOMEM); 993 } 994 995 /* 996 * Handle a bio-instigated command that has been completed. 997 */ 998 static void 999 aac_bio_complete(struct aac_command *cm) 1000 { 1001 struct aac_blockread_response *brr; 1002 struct aac_blockwrite_response *bwr; 1003 struct bio *bp; 1004 AAC_FSAStatus status; 1005 1006 /* fetch relevant status and then release the command */ 1007 bp = (struct bio *)cm->cm_private; 1008 if (bp->bio_cmd == BIO_READ) { 1009 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0]; 1010 status = brr->Status; 1011 } else { 1012 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0]; 1013 status = bwr->Status; 1014 } 1015 aac_release_command(cm); 1016 1017 /* fix up the bio based on status */ 1018 if (status == ST_OK) { 1019 bp->bio_resid = 0; 1020 } else { 1021 bp->bio_error = EIO; 1022 bp->bio_flags |= BIO_ERROR; 1023 /* pass an error string out to the disk layer */ 1024 bp->bio_driver1 = aac_describe_code(aac_command_status_table, 1025 status); 1026 } 1027 aac_biodone(bp); 1028 } 1029 1030 /* 1031 * Submit a command to the controller, return when it completes. 1032 * XXX This is very dangerous! If the card has gone out to lunch, we could 1033 * be stuck here forever. At the same time, signals are not caught 1034 * because there is a risk that a signal could wakeup the tsleep before 1035 * the card has a chance to complete the command. The passed in timeout 1036 * is ignored for the same reason. Since there is no way to cancel a 1037 * command in progress, we should probably create a 'dead' queue where 1038 * commands go that have been interrupted/timed-out/etc, that keeps them 1039 * out of the free pool. That way, if the card is just slow, it won't 1040 * spam the memory of a command that has been recycled. 1041 */ 1042 static int 1043 aac_wait_command(struct aac_command *cm, int timeout) 1044 { 1045 struct aac_softc *sc; 1046 int error = 0; 1047 1048 debug_called(2); 1049 1050 sc = cm->cm_sc; 1051 1052 /* Put the command on the ready queue and get things going */ 1053 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 1054 aac_enqueue_ready(cm); 1055 aac_startio(sc); 1056 while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) { 1057 error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0); 1058 } 1059 return(error); 1060 } 1061 1062 /* 1063 *Command Buffer Management 1064 */ 1065 1066 /* 1067 * Allocate a command. 1068 */ 1069 int 1070 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp) 1071 { 1072 struct aac_command *cm; 1073 1074 debug_called(3); 1075 1076 if ((cm = aac_dequeue_free(sc)) == NULL) { 1077 if (sc->total_fibs < sc->aac_max_fibs) { 1078 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS; 1079 wakeup(sc->aifthread); 1080 } 1081 return (EBUSY); 1082 } 1083 1084 *cmp = cm; 1085 return(0); 1086 } 1087 1088 /* 1089 * Release a command back to the freelist. 1090 */ 1091 void 1092 aac_release_command(struct aac_command *cm) 1093 { 1094 debug_called(3); 1095 1096 /* (re)initialise the command/FIB */ 1097 cm->cm_sgtable = NULL; 1098 cm->cm_flags = 0; 1099 cm->cm_complete = NULL; 1100 cm->cm_private = NULL; 1101 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY; 1102 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB; 1103 cm->cm_fib->Header.Flags = 0; 1104 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib); 1105 1106 /* 1107 * These are duplicated in aac_start to cover the case where an 1108 * intermediate stage may have destroyed them. They're left 1109 * initialised here for debugging purposes only. 1110 */ 1111 cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib; 1112 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys; 1113 cm->cm_fib->Header.SenderData = 0; 1114 1115 aac_enqueue_free(cm); 1116 } 1117 1118 /* 1119 * Map helper for command/FIB allocation. 1120 */ 1121 static void 1122 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1123 { 1124 uint32_t *fibphys; 1125 1126 fibphys = (uint32_t *)arg; 1127 1128 debug_called(3); 1129 1130 *fibphys = segs[0].ds_addr; 1131 } 1132 1133 /* 1134 * Allocate and initialise commands/FIBs for this adapter. 1135 */ 1136 static int 1137 aac_alloc_commands(struct aac_softc *sc) 1138 { 1139 struct aac_command *cm; 1140 struct aac_fibmap *fm; 1141 uint32_t fibphys; 1142 int i, error; 1143 1144 debug_called(2); 1145 1146 if (sc->total_fibs + AAC_FIB_COUNT > sc->aac_max_fibs) 1147 return (ENOMEM); 1148 1149 fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO); 1150 if (fm == NULL) 1151 return (ENOMEM); 1152 1153 /* allocate the FIBs in DMAable memory and load them */ 1154 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs, 1155 BUS_DMA_NOWAIT, &fm->aac_fibmap)) { 1156 device_printf(sc->aac_dev, 1157 "Not enough contiguous memory available.\n"); 1158 free(fm, M_AACBUF); 1159 return (ENOMEM); 1160 } 1161 1162 bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs, 1163 AAC_FIB_COUNT * sizeof(struct aac_fib), 1164 aac_map_command_helper, &fibphys, 0); 1165 1166 /* initialise constant fields in the command structure */ 1167 bzero(fm->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib)); 1168 for (i = 0; i < AAC_FIB_COUNT; i++) { 1169 cm = sc->aac_commands + sc->total_fibs; 1170 fm->aac_commands = cm; 1171 cm->cm_sc = sc; 1172 cm->cm_fib = fm->aac_fibs + i; 1173 cm->cm_fibphys = fibphys + (i * sizeof(struct aac_fib)); 1174 cm->cm_index = sc->total_fibs; 1175 1176 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0, 1177 &cm->cm_datamap)) == 0) 1178 aac_release_command(cm); 1179 else 1180 break; 1181 sc->total_fibs++; 1182 } 1183 1184 if (i > 0) { 1185 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link); 1186 debug(1, "total_fibs= %d\n", sc->total_fibs); 1187 return (0); 1188 } 1189 1190 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); 1191 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); 1192 free(fm, M_AACBUF); 1193 return (ENOMEM); 1194 } 1195 1196 /* 1197 * Free FIBs owned by this adapter. 1198 */ 1199 static void 1200 aac_free_commands(struct aac_softc *sc) 1201 { 1202 struct aac_fibmap *fm; 1203 struct aac_command *cm; 1204 int i; 1205 1206 debug_called(1); 1207 1208 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) { 1209 1210 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link); 1211 /* 1212 * We check against total_fibs to handle partially 1213 * allocated blocks. 1214 */ 1215 for (i = 0; i < AAC_FIB_COUNT && sc->total_fibs--; i++) { 1216 cm = fm->aac_commands + i; 1217 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap); 1218 } 1219 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); 1220 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); 1221 free(fm, M_AACBUF); 1222 } 1223 } 1224 1225 /* 1226 * Command-mapping helper function - populate this command's s/g table. 1227 */ 1228 static void 1229 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1230 { 1231 struct aac_command *cm; 1232 struct aac_fib *fib; 1233 int i; 1234 1235 debug_called(3); 1236 1237 cm = (struct aac_command *)arg; 1238 fib = cm->cm_fib; 1239 1240 /* copy into the FIB */ 1241 if (cm->cm_sgtable != NULL) { 1242 if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) { 1243 struct aac_sg_table *sg; 1244 sg = cm->cm_sgtable; 1245 sg->SgCount = nseg; 1246 for (i = 0; i < nseg; i++) { 1247 sg->SgEntry[i].SgAddress = segs[i].ds_addr; 1248 sg->SgEntry[i].SgByteCount = segs[i].ds_len; 1249 } 1250 /* update the FIB size for the s/g count */ 1251 fib->Header.Size += nseg * sizeof(struct aac_sg_entry); 1252 } else { 1253 struct aac_sg_table64 *sg; 1254 sg = (struct aac_sg_table64 *)cm->cm_sgtable; 1255 sg->SgCount = nseg; 1256 for (i = 0; i < nseg; i++) { 1257 sg->SgEntry64[i].SgAddress = segs[i].ds_addr; 1258 sg->SgEntry64[i].SgByteCount = segs[i].ds_len; 1259 } 1260 /* update the FIB size for the s/g count */ 1261 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64); 1262 } 1263 } 1264 } 1265 1266 /* 1267 * Map a command into controller-visible space. 1268 */ 1269 static void 1270 aac_map_command(struct aac_command *cm) 1271 { 1272 struct aac_softc *sc; 1273 1274 debug_called(2); 1275 1276 sc = cm->cm_sc; 1277 1278 /* don't map more than once */ 1279 if (cm->cm_flags & AAC_CMD_MAPPED) 1280 return; 1281 1282 if (cm->cm_datalen != 0) { 1283 bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap, 1284 cm->cm_data, cm->cm_datalen, 1285 aac_map_command_sg, cm, 0); 1286 1287 if (cm->cm_flags & AAC_CMD_DATAIN) 1288 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1289 BUS_DMASYNC_PREREAD); 1290 if (cm->cm_flags & AAC_CMD_DATAOUT) 1291 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1292 BUS_DMASYNC_PREWRITE); 1293 } 1294 cm->cm_flags |= AAC_CMD_MAPPED; 1295 } 1296 1297 /* 1298 * Unmap a command from controller-visible space. 1299 */ 1300 static void 1301 aac_unmap_command(struct aac_command *cm) 1302 { 1303 struct aac_softc *sc; 1304 1305 debug_called(2); 1306 1307 sc = cm->cm_sc; 1308 1309 if (!(cm->cm_flags & AAC_CMD_MAPPED)) 1310 return; 1311 1312 if (cm->cm_datalen != 0) { 1313 if (cm->cm_flags & AAC_CMD_DATAIN) 1314 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1315 BUS_DMASYNC_POSTREAD); 1316 if (cm->cm_flags & AAC_CMD_DATAOUT) 1317 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1318 BUS_DMASYNC_POSTWRITE); 1319 1320 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap); 1321 } 1322 cm->cm_flags &= ~AAC_CMD_MAPPED; 1323 } 1324 1325 /* 1326 * Hardware Interface 1327 */ 1328 1329 /* 1330 * Initialise the adapter. 1331 */ 1332 static void 1333 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1334 { 1335 struct aac_softc *sc; 1336 1337 debug_called(1); 1338 1339 sc = (struct aac_softc *)arg; 1340 1341 sc->aac_common_busaddr = segs[0].ds_addr; 1342 } 1343 1344 static int 1345 aac_check_firmware(struct aac_softc *sc) 1346 { 1347 u_int32_t major, minor, options; 1348 1349 debug_called(1); 1350 1351 /* 1352 * Retrieve the firmware version numbers. Dell PERC2/QC cards with 1353 * firmware version 1.x are not compatible with this driver. 1354 */ 1355 if (sc->flags & AAC_FLAGS_PERC2QC) { 1356 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0, 1357 NULL)) { 1358 device_printf(sc->aac_dev, 1359 "Error reading firmware version\n"); 1360 return (EIO); 1361 } 1362 1363 /* These numbers are stored as ASCII! */ 1364 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30; 1365 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30; 1366 if (major == 1) { 1367 device_printf(sc->aac_dev, 1368 "Firmware version %d.%d is not supported.\n", 1369 major, minor); 1370 return (EINVAL); 1371 } 1372 } 1373 1374 /* 1375 * Retrieve the capabilities/supported options word so we know what 1376 * work-arounds to enable. 1377 */ 1378 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) { 1379 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 1380 return (EIO); 1381 } 1382 options = AAC_GET_MAILBOX(sc, 1); 1383 sc->supported_options = options; 1384 1385 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 && 1386 (sc->flags & AAC_FLAGS_NO4GB) == 0) 1387 sc->flags |= AAC_FLAGS_4GB_WINDOW; 1388 if (options & AAC_SUPPORTED_NONDASD) 1389 sc->flags |= AAC_FLAGS_ENABLE_CAM; 1390 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0 && (sizeof(bus_addr_t) > 4)) { 1391 device_printf(sc->aac_dev, "Enabling 64-bit address support\n"); 1392 sc->flags |= AAC_FLAGS_SG_64BIT; 1393 } 1394 1395 /* Check for broken hardware that does a lower number of commands */ 1396 if ((sc->flags & AAC_FLAGS_256FIBS) == 0) 1397 sc->aac_max_fibs = AAC_MAX_FIBS; 1398 else 1399 sc->aac_max_fibs = 256; 1400 1401 return (0); 1402 } 1403 1404 static int 1405 aac_init(struct aac_softc *sc) 1406 { 1407 struct aac_adapter_init *ip; 1408 time_t then; 1409 u_int32_t code; 1410 u_int8_t *qaddr; 1411 int error; 1412 1413 debug_called(1); 1414 1415 /* 1416 * First wait for the adapter to come ready. 1417 */ 1418 then = time_second; 1419 do { 1420 code = AAC_GET_FWSTATUS(sc); 1421 if (code & AAC_SELF_TEST_FAILED) { 1422 device_printf(sc->aac_dev, "FATAL: selftest failed\n"); 1423 return(ENXIO); 1424 } 1425 if (code & AAC_KERNEL_PANIC) { 1426 device_printf(sc->aac_dev, 1427 "FATAL: controller kernel panic\n"); 1428 return(ENXIO); 1429 } 1430 if (time_second > (then + AAC_BOOT_TIMEOUT)) { 1431 device_printf(sc->aac_dev, 1432 "FATAL: controller not coming ready, " 1433 "status %x\n", code); 1434 return(ENXIO); 1435 } 1436 } while (!(code & AAC_UP_AND_RUNNING)); 1437 1438 error = ENOMEM; 1439 /* 1440 * Create DMA tag for mapping buffers into controller-addressable space. 1441 */ 1442 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1443 1, 0, /* algnmnt, boundary */ 1444 (sc->flags & AAC_FLAGS_SG_64BIT) ? 1445 BUS_SPACE_MAXADDR : 1446 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1447 BUS_SPACE_MAXADDR, /* highaddr */ 1448 NULL, NULL, /* filter, filterarg */ 1449 MAXBSIZE, /* maxsize */ 1450 AAC_MAXSGENTRIES, /* nsegments */ 1451 MAXBSIZE, /* maxsegsize */ 1452 BUS_DMA_ALLOCNOW, /* flags */ 1453 &sc->aac_buffer_dmat)) { 1454 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n"); 1455 goto out; 1456 } 1457 1458 /* 1459 * Create DMA tag for mapping FIBs into controller-addressable space.. 1460 */ 1461 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1462 1, 0, /* algnmnt, boundary */ 1463 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1464 BUS_SPACE_MAXADDR_32BIT : 1465 0x7fffffff, /* lowaddr */ 1466 BUS_SPACE_MAXADDR, /* highaddr */ 1467 NULL, NULL, /* filter, filterarg */ 1468 AAC_FIB_COUNT * 1469 sizeof(struct aac_fib), /* maxsize */ 1470 1, /* nsegments */ 1471 AAC_FIB_COUNT * 1472 sizeof(struct aac_fib), /* maxsegsize */ 1473 BUS_DMA_ALLOCNOW, /* flags */ 1474 &sc->aac_fib_dmat)) { 1475 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");; 1476 goto out; 1477 } 1478 1479 /* 1480 * Create DMA tag for the common structure and allocate it. 1481 */ 1482 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1483 1, 0, /* algnmnt, boundary */ 1484 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1485 BUS_SPACE_MAXADDR_32BIT : 1486 0x7fffffff, /* lowaddr */ 1487 BUS_SPACE_MAXADDR, /* highaddr */ 1488 NULL, NULL, /* filter, filterarg */ 1489 8192 + sizeof(struct aac_common), /* maxsize */ 1490 1, /* nsegments */ 1491 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1492 BUS_DMA_ALLOCNOW, /* flags */ 1493 &sc->aac_common_dmat)) { 1494 device_printf(sc->aac_dev, 1495 "can't allocate common structure DMA tag\n"); 1496 goto out; 1497 } 1498 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common, 1499 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) { 1500 device_printf(sc->aac_dev, "can't allocate common structure\n"); 1501 goto out; 1502 } 1503 1504 /* 1505 * Work around a bug in the 2120 and 2200 that cannot DMA commands 1506 * below address 8192 in physical memory. 1507 * XXX If the padding is not needed, can it be put to use instead 1508 * of ignored? 1509 */ 1510 bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap, 1511 sc->aac_common, 8192 + sizeof(*sc->aac_common), 1512 aac_common_map, sc, 0); 1513 1514 if (sc->aac_common_busaddr < 8192) { 1515 (uint8_t *)sc->aac_common += 8192; 1516 sc->aac_common_busaddr += 8192; 1517 } 1518 bzero(sc->aac_common, sizeof(*sc->aac_common)); 1519 1520 /* Allocate some FIBs and associated command structs */ 1521 TAILQ_INIT(&sc->aac_fibmap_tqh); 1522 sc->aac_commands = malloc(AAC_MAX_FIBS * sizeof(struct aac_command), 1523 M_AACBUF, M_WAITOK|M_ZERO); 1524 while (sc->total_fibs < AAC_PREALLOCATE_FIBS) { 1525 if (aac_alloc_commands(sc) != 0) 1526 break; 1527 } 1528 if (sc->total_fibs == 0) 1529 goto out; 1530 1531 /* 1532 * Fill in the init structure. This tells the adapter about the 1533 * physical location of various important shared data structures. 1534 */ 1535 ip = &sc->aac_common->ac_init; 1536 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION; 1537 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION; 1538 1539 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr + 1540 offsetof(struct aac_common, ac_fibs); 1541 ip->AdapterFibsVirtualAddress = 0; 1542 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib); 1543 ip->AdapterFibAlign = sizeof(struct aac_fib); 1544 1545 ip->PrintfBufferAddress = sc->aac_common_busaddr + 1546 offsetof(struct aac_common, ac_printf); 1547 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE; 1548 1549 /* The adapter assumes that pages are 4K in size */ 1550 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE; 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 qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN; 1570 qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN; 1571 sc->aac_queues = (struct aac_queue_table *)qaddr; 1572 ip->CommHeaderAddress = sc->aac_common_busaddr + 1573 ((u_int32_t)sc->aac_queues - 1574 (u_int32_t)sc->aac_common); 1575 1576 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1577 AAC_HOST_NORM_CMD_ENTRIES; 1578 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1579 AAC_HOST_NORM_CMD_ENTRIES; 1580 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1581 AAC_HOST_HIGH_CMD_ENTRIES; 1582 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1583 AAC_HOST_HIGH_CMD_ENTRIES; 1584 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1585 AAC_ADAP_NORM_CMD_ENTRIES; 1586 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1587 AAC_ADAP_NORM_CMD_ENTRIES; 1588 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1589 AAC_ADAP_HIGH_CMD_ENTRIES; 1590 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1591 AAC_ADAP_HIGH_CMD_ENTRIES; 1592 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1593 AAC_HOST_NORM_RESP_ENTRIES; 1594 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1595 AAC_HOST_NORM_RESP_ENTRIES; 1596 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1597 AAC_HOST_HIGH_RESP_ENTRIES; 1598 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1599 AAC_HOST_HIGH_RESP_ENTRIES; 1600 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1601 AAC_ADAP_NORM_RESP_ENTRIES; 1602 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1603 AAC_ADAP_NORM_RESP_ENTRIES; 1604 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1605 AAC_ADAP_HIGH_RESP_ENTRIES; 1606 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1607 AAC_ADAP_HIGH_RESP_ENTRIES; 1608 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] = 1609 &sc->aac_queues->qt_HostNormCmdQueue[0]; 1610 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] = 1611 &sc->aac_queues->qt_HostHighCmdQueue[0]; 1612 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] = 1613 &sc->aac_queues->qt_AdapNormCmdQueue[0]; 1614 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] = 1615 &sc->aac_queues->qt_AdapHighCmdQueue[0]; 1616 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] = 1617 &sc->aac_queues->qt_HostNormRespQueue[0]; 1618 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] = 1619 &sc->aac_queues->qt_HostHighRespQueue[0]; 1620 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] = 1621 &sc->aac_queues->qt_AdapNormRespQueue[0]; 1622 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] = 1623 &sc->aac_queues->qt_AdapHighRespQueue[0]; 1624 1625 /* 1626 * Do controller-type-specific initialisation 1627 */ 1628 switch (sc->aac_hwif) { 1629 case AAC_HWIF_I960RX: 1630 AAC_SETREG4(sc, AAC_RX_ODBR, ~0); 1631 break; 1632 } 1633 1634 /* 1635 * Give the init structure to the controller. 1636 */ 1637 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT, 1638 sc->aac_common_busaddr + 1639 offsetof(struct aac_common, ac_init), 0, 0, 0, 1640 NULL)) { 1641 device_printf(sc->aac_dev, 1642 "error establishing init structure\n"); 1643 error = EIO; 1644 goto out; 1645 } 1646 1647 error = 0; 1648 out: 1649 return(error); 1650 } 1651 1652 /* 1653 * Send a synchronous command to the controller and wait for a result. 1654 */ 1655 static int 1656 aac_sync_command(struct aac_softc *sc, u_int32_t command, 1657 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3, 1658 u_int32_t *sp) 1659 { 1660 time_t then; 1661 u_int32_t status; 1662 1663 debug_called(3); 1664 1665 /* populate the mailbox */ 1666 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3); 1667 1668 /* ensure the sync command doorbell flag is cleared */ 1669 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1670 1671 /* then set it to signal the adapter */ 1672 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND); 1673 1674 /* spin waiting for the command to complete */ 1675 then = time_second; 1676 do { 1677 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) { 1678 debug(1, "timed out"); 1679 return(EIO); 1680 } 1681 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND)); 1682 1683 /* clear the completion flag */ 1684 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1685 1686 /* get the command status */ 1687 status = AAC_GET_MAILBOX(sc, 0); 1688 if (sp != NULL) 1689 *sp = status; 1690 return(0); 1691 } 1692 1693 /* 1694 * Grab the sync fib area. 1695 */ 1696 int 1697 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags) 1698 { 1699 1700 /* 1701 * If the force flag is set, the system is shutting down, or in 1702 * trouble. Ignore the mutex. 1703 */ 1704 if (!(flags & AAC_SYNC_LOCK_FORCE)) 1705 AAC_LOCK_ACQUIRE(&sc->aac_sync_lock); 1706 1707 *fib = &sc->aac_common->ac_sync_fib; 1708 1709 return (1); 1710 } 1711 1712 /* 1713 * Release the sync fib area. 1714 */ 1715 void 1716 aac_release_sync_fib(struct aac_softc *sc) 1717 { 1718 1719 AAC_LOCK_RELEASE(&sc->aac_sync_lock); 1720 } 1721 1722 /* 1723 * Send a synchronous FIB to the controller and wait for a result. 1724 */ 1725 int 1726 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate, 1727 struct aac_fib *fib, u_int16_t datasize) 1728 { 1729 debug_called(3); 1730 1731 if (datasize > AAC_FIB_DATASIZE) 1732 return(EINVAL); 1733 1734 /* 1735 * Set up the sync FIB 1736 */ 1737 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED | 1738 AAC_FIBSTATE_INITIALISED | 1739 AAC_FIBSTATE_EMPTY; 1740 fib->Header.XferState |= xferstate; 1741 fib->Header.Command = command; 1742 fib->Header.StructType = AAC_FIBTYPE_TFIB; 1743 fib->Header.Size = sizeof(struct aac_fib) + datasize; 1744 fib->Header.SenderSize = sizeof(struct aac_fib); 1745 fib->Header.SenderFibAddress = (u_int32_t)fib; 1746 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr + 1747 offsetof(struct aac_common, 1748 ac_sync_fib); 1749 1750 /* 1751 * Give the FIB to the controller, wait for a response. 1752 */ 1753 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB, 1754 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) { 1755 debug(2, "IO error"); 1756 return(EIO); 1757 } 1758 1759 return (0); 1760 } 1761 1762 /* 1763 * Adapter-space FIB queue manipulation 1764 * 1765 * Note that the queue implementation here is a little funky; neither the PI or 1766 * CI will ever be zero. This behaviour is a controller feature. 1767 */ 1768 static struct { 1769 int size; 1770 int notify; 1771 } aac_qinfo[] = { 1772 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL}, 1773 {AAC_HOST_HIGH_CMD_ENTRIES, 0}, 1774 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY}, 1775 {AAC_ADAP_HIGH_CMD_ENTRIES, 0}, 1776 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL}, 1777 {AAC_HOST_HIGH_RESP_ENTRIES, 0}, 1778 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY}, 1779 {AAC_ADAP_HIGH_RESP_ENTRIES, 0} 1780 }; 1781 1782 /* 1783 * Atomically insert an entry into the nominated queue, returns 0 on success or 1784 * EBUSY if the queue is full. 1785 * 1786 * Note: it would be more efficient to defer notifying the controller in 1787 * the case where we may be inserting several entries in rapid succession, 1788 * but implementing this usefully may be difficult (it would involve a 1789 * separate queue/notify interface). 1790 */ 1791 static int 1792 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm) 1793 { 1794 u_int32_t pi, ci; 1795 int error; 1796 u_int32_t fib_size; 1797 u_int32_t fib_addr; 1798 1799 debug_called(3); 1800 1801 fib_size = cm->cm_fib->Header.Size; 1802 fib_addr = cm->cm_fib->Header.ReceiverFibAddress; 1803 1804 /* get the producer/consumer indices */ 1805 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1806 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1807 1808 /* wrap the queue? */ 1809 if (pi >= aac_qinfo[queue].size) 1810 pi = 0; 1811 1812 /* check for queue full */ 1813 if ((pi + 1) == ci) { 1814 error = EBUSY; 1815 goto out; 1816 } 1817 1818 /* populate queue entry */ 1819 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; 1820 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; 1821 1822 /* update producer index */ 1823 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; 1824 1825 /* 1826 * To avoid a race with its completion interrupt, place this command on 1827 * the busy queue prior to advertising it to the controller. 1828 */ 1829 aac_enqueue_busy(cm); 1830 1831 /* notify the adapter if we know how */ 1832 if (aac_qinfo[queue].notify != 0) 1833 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1834 1835 error = 0; 1836 1837 out: 1838 return(error); 1839 } 1840 1841 /* 1842 * Atomically remove one entry from the nominated queue, returns 0 on 1843 * success or ENOENT if the queue is empty. 1844 */ 1845 static int 1846 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size, 1847 struct aac_fib **fib_addr) 1848 { 1849 u_int32_t pi, ci; 1850 u_int32_t fib_index; 1851 int error; 1852 int notify; 1853 1854 debug_called(3); 1855 1856 /* get the producer/consumer indices */ 1857 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1858 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1859 1860 /* check for queue empty */ 1861 if (ci == pi) { 1862 error = ENOENT; 1863 goto out; 1864 } 1865 1866 notify = 0; 1867 if (ci == pi + 1) 1868 notify++; 1869 1870 /* wrap the queue? */ 1871 if (ci >= aac_qinfo[queue].size) 1872 ci = 0; 1873 1874 /* fetch the entry */ 1875 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size; 1876 1877 switch (queue) { 1878 case AAC_HOST_NORM_CMD_QUEUE: 1879 case AAC_HOST_HIGH_CMD_QUEUE: 1880 /* 1881 * The aq_fib_addr is only 32 bits wide so it can't be counted 1882 * on to hold an address. For AIF's, the adapter assumes 1883 * that it's giving us an address into the array of AIF fibs. 1884 * Therefore, we have to convert it to an index. 1885 */ 1886 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr / 1887 sizeof(struct aac_fib); 1888 *fib_addr = &sc->aac_common->ac_fibs[fib_index]; 1889 break; 1890 1891 case AAC_HOST_NORM_RESP_QUEUE: 1892 case AAC_HOST_HIGH_RESP_QUEUE: 1893 { 1894 struct aac_command *cm; 1895 1896 /* 1897 * As above, an index is used instead of an actual address. 1898 * Gotta shift the index to account for the fast response 1899 * bit. No other correction is needed since this value was 1900 * originally provided by the driver via the SenderFibAddress 1901 * field. 1902 */ 1903 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr; 1904 cm = sc->aac_commands + (fib_index >> 1); 1905 *fib_addr = cm->cm_fib; 1906 1907 /* 1908 * Is this a fast response? If it is, update the fib fields in 1909 * local memory since the whole fib isn't DMA'd back up. 1910 */ 1911 if (fib_index & 0x01) { 1912 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP; 1913 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL; 1914 } 1915 break; 1916 } 1917 default: 1918 panic("Invalid queue in aac_dequeue_fib()"); 1919 break; 1920 } 1921 1922 /* update consumer index */ 1923 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1; 1924 1925 /* if we have made the queue un-full, notify the adapter */ 1926 if (notify && (aac_qinfo[queue].notify != 0)) 1927 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1928 error = 0; 1929 1930 out: 1931 return(error); 1932 } 1933 1934 /* 1935 * Put our response to an Adapter Initialed Fib on the response queue 1936 */ 1937 static int 1938 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib) 1939 { 1940 u_int32_t pi, ci; 1941 int error; 1942 u_int32_t fib_size; 1943 u_int32_t fib_addr; 1944 1945 debug_called(1); 1946 1947 /* Tell the adapter where the FIB is */ 1948 fib_size = fib->Header.Size; 1949 fib_addr = fib->Header.SenderFibAddress; 1950 fib->Header.ReceiverFibAddress = fib_addr; 1951 1952 /* get the producer/consumer indices */ 1953 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1954 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1955 1956 /* wrap the queue? */ 1957 if (pi >= aac_qinfo[queue].size) 1958 pi = 0; 1959 1960 /* check for queue full */ 1961 if ((pi + 1) == ci) { 1962 error = EBUSY; 1963 goto out; 1964 } 1965 1966 /* populate queue entry */ 1967 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; 1968 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; 1969 1970 /* update producer index */ 1971 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; 1972 1973 /* notify the adapter if we know how */ 1974 if (aac_qinfo[queue].notify != 0) 1975 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1976 1977 error = 0; 1978 1979 out: 1980 return(error); 1981 } 1982 1983 /* 1984 * Check for commands that have been outstanding for a suspiciously long time, 1985 * and complain about them. 1986 */ 1987 static void 1988 aac_timeout(struct aac_softc *sc) 1989 { 1990 struct aac_command *cm; 1991 time_t deadline; 1992 1993 /* 1994 * Traverse the busy command list, bitch about late commands once 1995 * only. 1996 */ 1997 deadline = time_second - AAC_CMD_TIMEOUT; 1998 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) { 1999 if ((cm->cm_timestamp < deadline) 2000 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) { 2001 cm->cm_flags |= AAC_CMD_TIMEDOUT; 2002 device_printf(sc->aac_dev, 2003 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", 2004 cm, (int)(time_second-cm->cm_timestamp)); 2005 AAC_PRINT_FIB(sc, cm->cm_fib); 2006 } 2007 } 2008 2009 return; 2010 } 2011 2012 /* 2013 * Interface Function Vectors 2014 */ 2015 2016 /* 2017 * Read the current firmware status word. 2018 */ 2019 static int 2020 aac_sa_get_fwstatus(struct aac_softc *sc) 2021 { 2022 debug_called(3); 2023 2024 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS)); 2025 } 2026 2027 static int 2028 aac_rx_get_fwstatus(struct aac_softc *sc) 2029 { 2030 debug_called(3); 2031 2032 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS)); 2033 } 2034 2035 static int 2036 aac_fa_get_fwstatus(struct aac_softc *sc) 2037 { 2038 int val; 2039 2040 debug_called(3); 2041 2042 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS); 2043 return (val); 2044 } 2045 2046 /* 2047 * Notify the controller of a change in a given queue 2048 */ 2049 2050 static void 2051 aac_sa_qnotify(struct aac_softc *sc, int qbit) 2052 { 2053 debug_called(3); 2054 2055 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit); 2056 } 2057 2058 static void 2059 aac_rx_qnotify(struct aac_softc *sc, int qbit) 2060 { 2061 debug_called(3); 2062 2063 AAC_SETREG4(sc, AAC_RX_IDBR, qbit); 2064 } 2065 2066 static void 2067 aac_fa_qnotify(struct aac_softc *sc, int qbit) 2068 { 2069 debug_called(3); 2070 2071 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit); 2072 AAC_FA_HACK(sc); 2073 } 2074 2075 /* 2076 * Get the interrupt reason bits 2077 */ 2078 static int 2079 aac_sa_get_istatus(struct aac_softc *sc) 2080 { 2081 debug_called(3); 2082 2083 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0)); 2084 } 2085 2086 static int 2087 aac_rx_get_istatus(struct aac_softc *sc) 2088 { 2089 debug_called(3); 2090 2091 return(AAC_GETREG4(sc, AAC_RX_ODBR)); 2092 } 2093 2094 static int 2095 aac_fa_get_istatus(struct aac_softc *sc) 2096 { 2097 int val; 2098 2099 debug_called(3); 2100 2101 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0); 2102 return (val); 2103 } 2104 2105 /* 2106 * Clear some interrupt reason bits 2107 */ 2108 static void 2109 aac_sa_clear_istatus(struct aac_softc *sc, int mask) 2110 { 2111 debug_called(3); 2112 2113 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask); 2114 } 2115 2116 static void 2117 aac_rx_clear_istatus(struct aac_softc *sc, int mask) 2118 { 2119 debug_called(3); 2120 2121 AAC_SETREG4(sc, AAC_RX_ODBR, mask); 2122 } 2123 2124 static void 2125 aac_fa_clear_istatus(struct aac_softc *sc, int mask) 2126 { 2127 debug_called(3); 2128 2129 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask); 2130 AAC_FA_HACK(sc); 2131 } 2132 2133 /* 2134 * Populate the mailbox and set the command word 2135 */ 2136 static void 2137 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2138 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2139 { 2140 debug_called(4); 2141 2142 AAC_SETREG4(sc, AAC_SA_MAILBOX, command); 2143 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0); 2144 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1); 2145 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2); 2146 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3); 2147 } 2148 2149 static void 2150 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, 2151 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2152 { 2153 debug_called(4); 2154 2155 AAC_SETREG4(sc, AAC_RX_MAILBOX, command); 2156 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0); 2157 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1); 2158 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2); 2159 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3); 2160 } 2161 2162 static void 2163 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2164 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2165 { 2166 debug_called(4); 2167 2168 AAC_SETREG4(sc, AAC_FA_MAILBOX, command); 2169 AAC_FA_HACK(sc); 2170 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0); 2171 AAC_FA_HACK(sc); 2172 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1); 2173 AAC_FA_HACK(sc); 2174 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2); 2175 AAC_FA_HACK(sc); 2176 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3); 2177 AAC_FA_HACK(sc); 2178 } 2179 2180 /* 2181 * Fetch the immediate command status word 2182 */ 2183 static int 2184 aac_sa_get_mailbox(struct aac_softc *sc, int mb) 2185 { 2186 debug_called(4); 2187 2188 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4))); 2189 } 2190 2191 static int 2192 aac_rx_get_mailbox(struct aac_softc *sc, int mb) 2193 { 2194 debug_called(4); 2195 2196 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4))); 2197 } 2198 2199 static int 2200 aac_fa_get_mailbox(struct aac_softc *sc, int mb) 2201 { 2202 int val; 2203 2204 debug_called(4); 2205 2206 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4)); 2207 return (val); 2208 } 2209 2210 /* 2211 * Set/clear interrupt masks 2212 */ 2213 static void 2214 aac_sa_set_interrupts(struct aac_softc *sc, int enable) 2215 { 2216 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2217 2218 if (enable) { 2219 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2220 } else { 2221 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0); 2222 } 2223 } 2224 2225 static void 2226 aac_rx_set_interrupts(struct aac_softc *sc, int enable) 2227 { 2228 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2229 2230 if (enable) { 2231 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS); 2232 } else { 2233 AAC_SETREG4(sc, AAC_RX_OIMR, ~0); 2234 } 2235 } 2236 2237 static void 2238 aac_fa_set_interrupts(struct aac_softc *sc, int enable) 2239 { 2240 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2241 2242 if (enable) { 2243 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2244 AAC_FA_HACK(sc); 2245 } else { 2246 AAC_SETREG2((sc), AAC_FA_MASK0, ~0); 2247 AAC_FA_HACK(sc); 2248 } 2249 } 2250 2251 /* 2252 * Debugging and Diagnostics 2253 */ 2254 2255 /* 2256 * Print some information about the controller. 2257 */ 2258 static void 2259 aac_describe_controller(struct aac_softc *sc) 2260 { 2261 struct aac_fib *fib; 2262 struct aac_adapter_info *info; 2263 2264 debug_called(2); 2265 2266 aac_alloc_sync_fib(sc, &fib, 0); 2267 2268 fib->data[0] = 0; 2269 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) { 2270 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 2271 aac_release_sync_fib(sc); 2272 return; 2273 } 2274 info = (struct aac_adapter_info *)&fib->data[0]; 2275 2276 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n", 2277 aac_describe_code(aac_cpu_variant, info->CpuVariant), 2278 info->ClockSpeed, info->BufferMem / (1024 * 1024), 2279 aac_describe_code(aac_battery_platform, 2280 info->batteryPlatform)); 2281 2282 /* save the kernel revision structure for later use */ 2283 sc->aac_revision = info->KernelRevision; 2284 device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n", 2285 info->KernelRevision.external.comp.major, 2286 info->KernelRevision.external.comp.minor, 2287 info->KernelRevision.external.comp.dash, 2288 info->KernelRevision.buildNumber, 2289 (u_int32_t)(info->SerialNumber & 0xffffff)); 2290 2291 aac_release_sync_fib(sc); 2292 2293 if (1 || bootverbose) { 2294 device_printf(sc->aac_dev, "Supported Options=%b\n", 2295 sc->supported_options, 2296 "\20" 2297 "\1SNAPSHOT" 2298 "\2CLUSTERS" 2299 "\3WCACHE" 2300 "\4DATA64" 2301 "\5HOSTTIME" 2302 "\6RAID50" 2303 "\7WINDOW4GB" 2304 "\10SCSIUPGD" 2305 "\11SOFTERR" 2306 "\12NORECOND" 2307 "\13SGMAP64" 2308 "\14ALARM" 2309 "\15NONDASD"); 2310 } 2311 } 2312 2313 /* 2314 * Look up a text description of a numeric error code and return a pointer to 2315 * same. 2316 */ 2317 static char * 2318 aac_describe_code(struct aac_code_lookup *table, u_int32_t code) 2319 { 2320 int i; 2321 2322 for (i = 0; table[i].string != NULL; i++) 2323 if (table[i].code == code) 2324 return(table[i].string); 2325 return(table[i + 1].string); 2326 } 2327 2328 /* 2329 * Management Interface 2330 */ 2331 2332 static int 2333 aac_open(dev_t dev, int flags, int fmt, d_thread_t *td) 2334 { 2335 struct aac_softc *sc; 2336 2337 debug_called(2); 2338 2339 sc = dev->si_drv1; 2340 2341 /* Check to make sure the device isn't already open */ 2342 if (sc->aac_state & AAC_STATE_OPEN) { 2343 return EBUSY; 2344 } 2345 sc->aac_state |= AAC_STATE_OPEN; 2346 2347 return 0; 2348 } 2349 2350 static int 2351 aac_close(dev_t dev, int flags, int fmt, d_thread_t *td) 2352 { 2353 struct aac_softc *sc; 2354 2355 debug_called(2); 2356 2357 sc = dev->si_drv1; 2358 2359 /* Mark this unit as no longer open */ 2360 sc->aac_state &= ~AAC_STATE_OPEN; 2361 2362 return 0; 2363 } 2364 2365 static int 2366 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td) 2367 { 2368 union aac_statrequest *as; 2369 struct aac_softc *sc; 2370 int error = 0; 2371 int i; 2372 2373 debug_called(2); 2374 2375 as = (union aac_statrequest *)arg; 2376 sc = dev->si_drv1; 2377 2378 switch (cmd) { 2379 case AACIO_STATS: 2380 switch (as->as_item) { 2381 case AACQ_FREE: 2382 case AACQ_BIO: 2383 case AACQ_READY: 2384 case AACQ_BUSY: 2385 case AACQ_COMPLETE: 2386 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat, 2387 sizeof(struct aac_qstat)); 2388 break; 2389 default: 2390 error = ENOENT; 2391 break; 2392 } 2393 break; 2394 2395 case FSACTL_SENDFIB: 2396 arg = *(caddr_t*)arg; 2397 case FSACTL_LNX_SENDFIB: 2398 debug(1, "FSACTL_SENDFIB"); 2399 error = aac_ioctl_sendfib(sc, arg); 2400 break; 2401 case FSACTL_AIF_THREAD: 2402 case FSACTL_LNX_AIF_THREAD: 2403 debug(1, "FSACTL_AIF_THREAD"); 2404 error = EINVAL; 2405 break; 2406 case FSACTL_OPEN_GET_ADAPTER_FIB: 2407 arg = *(caddr_t*)arg; 2408 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB: 2409 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB"); 2410 /* 2411 * Pass the caller out an AdapterFibContext. 2412 * 2413 * Note that because we only support one opener, we 2414 * basically ignore this. Set the caller's context to a magic 2415 * number just in case. 2416 * 2417 * The Linux code hands the driver a pointer into kernel space, 2418 * and then trusts it when the caller hands it back. Aiee! 2419 * Here, we give it the proc pointer of the per-adapter aif 2420 * thread. It's only used as a sanity check in other calls. 2421 */ 2422 i = (int)sc->aifthread; 2423 error = copyout(&i, arg, sizeof(i)); 2424 break; 2425 case FSACTL_GET_NEXT_ADAPTER_FIB: 2426 arg = *(caddr_t*)arg; 2427 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB: 2428 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB"); 2429 error = aac_getnext_aif(sc, arg); 2430 break; 2431 case FSACTL_CLOSE_GET_ADAPTER_FIB: 2432 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB: 2433 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB"); 2434 /* don't do anything here */ 2435 break; 2436 case FSACTL_MINIPORT_REV_CHECK: 2437 arg = *(caddr_t*)arg; 2438 case FSACTL_LNX_MINIPORT_REV_CHECK: 2439 debug(1, "FSACTL_MINIPORT_REV_CHECK"); 2440 error = aac_rev_check(sc, arg); 2441 break; 2442 case FSACTL_QUERY_DISK: 2443 arg = *(caddr_t*)arg; 2444 case FSACTL_LNX_QUERY_DISK: 2445 debug(1, "FSACTL_QUERY_DISK"); 2446 error = aac_query_disk(sc, arg); 2447 break; 2448 case FSACTL_DELETE_DISK: 2449 case FSACTL_LNX_DELETE_DISK: 2450 /* 2451 * We don't trust the underland to tell us when to delete a 2452 * container, rather we rely on an AIF coming from the 2453 * controller 2454 */ 2455 error = 0; 2456 break; 2457 default: 2458 debug(1, "unsupported cmd 0x%lx\n", cmd); 2459 error = EINVAL; 2460 break; 2461 } 2462 return(error); 2463 } 2464 2465 static int 2466 aac_poll(dev_t dev, int poll_events, d_thread_t *td) 2467 { 2468 struct aac_softc *sc; 2469 int revents; 2470 2471 sc = dev->si_drv1; 2472 revents = 0; 2473 2474 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); 2475 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) { 2476 if (sc->aac_aifq_tail != sc->aac_aifq_head) 2477 revents |= poll_events & (POLLIN | POLLRDNORM); 2478 } 2479 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2480 2481 if (revents == 0) { 2482 if (poll_events & (POLLIN | POLLRDNORM)) 2483 selrecord(td, &sc->rcv_select); 2484 } 2485 2486 return (revents); 2487 } 2488 2489 /* 2490 * Send a FIB supplied from userspace 2491 */ 2492 static int 2493 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) 2494 { 2495 struct aac_command *cm; 2496 int size, error; 2497 2498 debug_called(2); 2499 2500 cm = NULL; 2501 2502 /* 2503 * Get a command 2504 */ 2505 AAC_LOCK_ACQUIRE(&sc->aac_io_lock); 2506 if (aac_alloc_command(sc, &cm)) { 2507 error = EBUSY; 2508 goto out; 2509 } 2510 2511 /* 2512 * Fetch the FIB header, then re-copy to get data as well. 2513 */ 2514 if ((error = copyin(ufib, cm->cm_fib, 2515 sizeof(struct aac_fib_header))) != 0) 2516 goto out; 2517 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header); 2518 if (size > sizeof(struct aac_fib)) { 2519 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n", 2520 size, sizeof(struct aac_fib)); 2521 size = sizeof(struct aac_fib); 2522 } 2523 if ((error = copyin(ufib, cm->cm_fib, size)) != 0) 2524 goto out; 2525 cm->cm_fib->Header.Size = size; 2526 cm->cm_timestamp = time_second; 2527 2528 /* 2529 * Pass the FIB to the controller, wait for it to complete. 2530 */ 2531 if ((error = aac_wait_command(cm, 30)) != 0) { /* XXX user timeout? */ 2532 device_printf(sc->aac_dev, 2533 "aac_wait_command return %d\n", error); 2534 goto out; 2535 } 2536 2537 /* 2538 * Copy the FIB and data back out to the caller. 2539 */ 2540 size = cm->cm_fib->Header.Size; 2541 if (size > sizeof(struct aac_fib)) { 2542 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n", 2543 size, sizeof(struct aac_fib)); 2544 size = sizeof(struct aac_fib); 2545 } 2546 error = copyout(cm->cm_fib, ufib, size); 2547 2548 out: 2549 if (cm != NULL) { 2550 aac_release_command(cm); 2551 } 2552 2553 AAC_LOCK_RELEASE(&sc->aac_io_lock); 2554 return(error); 2555 } 2556 2557 /* 2558 * Handle an AIF sent to us by the controller; queue it for later reference. 2559 * If the queue fills up, then drop the older entries. 2560 */ 2561 static void 2562 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib) 2563 { 2564 struct aac_aif_command *aif; 2565 struct aac_container *co, *co_next; 2566 struct aac_mntinfo *mi; 2567 struct aac_mntinforesp *mir = NULL; 2568 u_int16_t rsize; 2569 int next, found; 2570 int count = 0, added = 0, i = 0; 2571 2572 debug_called(2); 2573 2574 aif = (struct aac_aif_command*)&fib->data[0]; 2575 aac_print_aif(sc, aif); 2576 2577 /* Is it an event that we should care about? */ 2578 switch (aif->command) { 2579 case AifCmdEventNotify: 2580 switch (aif->data.EN.type) { 2581 case AifEnAddContainer: 2582 case AifEnDeleteContainer: 2583 /* 2584 * A container was added or deleted, but the message 2585 * doesn't tell us anything else! Re-enumerate the 2586 * containers and sort things out. 2587 */ 2588 aac_alloc_sync_fib(sc, &fib, 0); 2589 mi = (struct aac_mntinfo *)&fib->data[0]; 2590 do { 2591 /* 2592 * Ask the controller for its containers one at 2593 * a time. 2594 * XXX What if the controller's list changes 2595 * midway through this enumaration? 2596 * XXX This should be done async. 2597 */ 2598 bzero(mi, sizeof(struct aac_mntinfo)); 2599 mi->Command = VM_NameServe; 2600 mi->MntType = FT_FILESYS; 2601 mi->MntCount = i; 2602 rsize = sizeof(mir); 2603 if (aac_sync_fib(sc, ContainerCommand, 0, fib, 2604 sizeof(struct aac_mntinfo))) { 2605 printf("Error probing container %d\n", 2606 i); 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 * Check the container against our list. 2614 * co->co_found was already set to 0 in a 2615 * previous run. 2616 */ 2617 if ((mir->Status == ST_OK) && 2618 (mir->MntTable[0].VolType != CT_NONE)) { 2619 found = 0; 2620 TAILQ_FOREACH(co, 2621 &sc->aac_container_tqh, 2622 co_link) { 2623 if (co->co_mntobj.ObjectId == 2624 mir->MntTable[0].ObjectId) { 2625 co->co_found = 1; 2626 found = 1; 2627 break; 2628 } 2629 } 2630 /* 2631 * If the container matched, continue 2632 * in the list. 2633 */ 2634 if (found) { 2635 i++; 2636 continue; 2637 } 2638 2639 /* 2640 * This is a new container. Do all the 2641 * appropriate things to set it up. 2642 */ 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 /* Wakeup any poll()ers */ 2702 selwakeup(&sc->rcv_select); 2703 } 2704 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2705 2706 return; 2707 } 2708 2709 /* 2710 * Return the Revision of the driver to userspace and check to see if the 2711 * userspace app is possibly compatible. This is extremely bogus since 2712 * our driver doesn't follow Adaptec's versioning system. Cheat by just 2713 * returning what the card reported. 2714 */ 2715 static int 2716 aac_rev_check(struct aac_softc *sc, caddr_t udata) 2717 { 2718 struct aac_rev_check rev_check; 2719 struct aac_rev_check_resp rev_check_resp; 2720 int error = 0; 2721 2722 debug_called(2); 2723 2724 /* 2725 * Copyin the revision struct from userspace 2726 */ 2727 if ((error = copyin(udata, (caddr_t)&rev_check, 2728 sizeof(struct aac_rev_check))) != 0) { 2729 return error; 2730 } 2731 2732 debug(2, "Userland revision= %d\n", 2733 rev_check.callingRevision.buildNumber); 2734 2735 /* 2736 * Doctor up the response struct. 2737 */ 2738 rev_check_resp.possiblyCompatible = 1; 2739 rev_check_resp.adapterSWRevision.external.ul = 2740 sc->aac_revision.external.ul; 2741 rev_check_resp.adapterSWRevision.buildNumber = 2742 sc->aac_revision.buildNumber; 2743 2744 return(copyout((caddr_t)&rev_check_resp, udata, 2745 sizeof(struct aac_rev_check_resp))); 2746 } 2747 2748 /* 2749 * Pass the caller the next AIF in their queue 2750 */ 2751 static int 2752 aac_getnext_aif(struct aac_softc *sc, caddr_t arg) 2753 { 2754 struct get_adapter_fib_ioctl agf; 2755 int error; 2756 2757 debug_called(2); 2758 2759 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) { 2760 2761 /* 2762 * Check the magic number that we gave the caller. 2763 */ 2764 if (agf.AdapterFibContext != (int)sc->aifthread) { 2765 error = EFAULT; 2766 } else { 2767 error = aac_return_aif(sc, agf.AifFib); 2768 if ((error == EAGAIN) && (agf.Wait)) { 2769 sc->aac_state |= AAC_STATE_AIF_SLEEPER; 2770 while (error == EAGAIN) { 2771 error = tsleep(sc->aac_aifq, PRIBIO | 2772 PCATCH, "aacaif", 0); 2773 if (error == 0) 2774 error = aac_return_aif(sc, 2775 agf.AifFib); 2776 } 2777 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER; 2778 } 2779 } 2780 } 2781 return(error); 2782 } 2783 2784 /* 2785 * Hand the next AIF off the top of the queue out to userspace. 2786 */ 2787 static int 2788 aac_return_aif(struct aac_softc *sc, caddr_t uptr) 2789 { 2790 int error; 2791 2792 debug_called(2); 2793 2794 AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock); 2795 if (sc->aac_aifq_tail == sc->aac_aifq_head) { 2796 error = EAGAIN; 2797 } else { 2798 error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr, 2799 sizeof(struct aac_aif_command)); 2800 if (error) 2801 device_printf(sc->aac_dev, 2802 "aac_return_aif: copyout returned %d\n", error); 2803 if (!error) 2804 sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) % 2805 AAC_AIFQ_LENGTH; 2806 } 2807 AAC_LOCK_RELEASE(&sc->aac_aifq_lock); 2808 return(error); 2809 } 2810 2811 /* 2812 * Give the userland some information about the container. The AAC arch 2813 * expects the driver to be a SCSI passthrough type driver, so it expects 2814 * the containers to have b:t:l numbers. Fake it. 2815 */ 2816 static int 2817 aac_query_disk(struct aac_softc *sc, caddr_t uptr) 2818 { 2819 struct aac_query_disk query_disk; 2820 struct aac_container *co; 2821 struct aac_disk *disk; 2822 int error, id; 2823 2824 debug_called(2); 2825 2826 disk = NULL; 2827 2828 error = copyin(uptr, (caddr_t)&query_disk, 2829 sizeof(struct aac_query_disk)); 2830 if (error) 2831 return (error); 2832 2833 id = query_disk.ContainerNumber; 2834 if (id == -1) 2835 return (EINVAL); 2836 2837 AAC_LOCK_ACQUIRE(&sc->aac_container_lock); 2838 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) { 2839 if (co->co_mntobj.ObjectId == id) 2840 break; 2841 } 2842 2843 if (co == NULL) { 2844 query_disk.Valid = 0; 2845 query_disk.Locked = 0; 2846 query_disk.Deleted = 1; /* XXX is this right? */ 2847 } else { 2848 disk = device_get_softc(co->co_disk); 2849 query_disk.Valid = 1; 2850 query_disk.Locked = 2851 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0; 2852 query_disk.Deleted = 0; 2853 query_disk.Bus = device_get_unit(sc->aac_dev); 2854 query_disk.Target = disk->unit; 2855 query_disk.Lun = 0; 2856 query_disk.UnMapped = 0; 2857 sprintf(&query_disk.diskDeviceName[0], "%s%d", 2858 disk->ad_disk.d_name, disk->ad_disk.d_unit); 2859 } 2860 AAC_LOCK_RELEASE(&sc->aac_container_lock); 2861 2862 error = copyout((caddr_t)&query_disk, uptr, 2863 sizeof(struct aac_query_disk)); 2864 2865 return (error); 2866 } 2867 2868 static void 2869 aac_get_bus_info(struct aac_softc *sc) 2870 { 2871 struct aac_fib *fib; 2872 struct aac_ctcfg *c_cmd; 2873 struct aac_ctcfg_resp *c_resp; 2874 struct aac_vmioctl *vmi; 2875 struct aac_vmi_businf_resp *vmi_resp; 2876 struct aac_getbusinf businfo; 2877 struct aac_sim *caminf; 2878 device_t child; 2879 int i, found, error; 2880 2881 aac_alloc_sync_fib(sc, &fib, 0); 2882 c_cmd = (struct aac_ctcfg *)&fib->data[0]; 2883 bzero(c_cmd, sizeof(struct aac_ctcfg)); 2884 2885 c_cmd->Command = VM_ContainerConfig; 2886 c_cmd->cmd = CT_GET_SCSI_METHOD; 2887 c_cmd->param = 0; 2888 2889 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2890 sizeof(struct aac_ctcfg)); 2891 if (error) { 2892 device_printf(sc->aac_dev, "Error %d sending " 2893 "VM_ContainerConfig command\n", error); 2894 aac_release_sync_fib(sc); 2895 return; 2896 } 2897 2898 c_resp = (struct aac_ctcfg_resp *)&fib->data[0]; 2899 if (c_resp->Status != ST_OK) { 2900 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n", 2901 c_resp->Status); 2902 aac_release_sync_fib(sc); 2903 return; 2904 } 2905 2906 sc->scsi_method_id = c_resp->param; 2907 2908 vmi = (struct aac_vmioctl *)&fib->data[0]; 2909 bzero(vmi, sizeof(struct aac_vmioctl)); 2910 2911 vmi->Command = VM_Ioctl; 2912 vmi->ObjType = FT_DRIVE; 2913 vmi->MethId = sc->scsi_method_id; 2914 vmi->ObjId = 0; 2915 vmi->IoctlCmd = GetBusInfo; 2916 2917 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2918 sizeof(struct aac_vmioctl)); 2919 if (error) { 2920 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n", 2921 error); 2922 aac_release_sync_fib(sc); 2923 return; 2924 } 2925 2926 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0]; 2927 if (vmi_resp->Status != ST_OK) { 2928 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n", 2929 vmi_resp->Status); 2930 aac_release_sync_fib(sc); 2931 return; 2932 } 2933 2934 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf)); 2935 aac_release_sync_fib(sc); 2936 2937 found = 0; 2938 for (i = 0; i < businfo.BusCount; i++) { 2939 if (businfo.BusValid[i] != AAC_BUS_VALID) 2940 continue; 2941 2942 caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim), 2943 M_AACBUF, M_NOWAIT | M_ZERO); 2944 if (caminf == NULL) 2945 continue; 2946 2947 child = device_add_child(sc->aac_dev, "aacp", -1); 2948 if (child == NULL) { 2949 device_printf(sc->aac_dev, "device_add_child failed\n"); 2950 continue; 2951 } 2952 2953 caminf->TargetsPerBus = businfo.TargetsPerBus; 2954 caminf->BusNumber = i; 2955 caminf->InitiatorBusId = businfo.InitiatorBusId[i]; 2956 caminf->aac_sc = sc; 2957 caminf->sim_dev = child; 2958 2959 device_set_ivars(child, caminf); 2960 device_set_desc(child, "SCSI Passthrough Bus"); 2961 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link); 2962 2963 found = 1; 2964 } 2965 2966 if (found) 2967 bus_generic_attach(sc->aac_dev); 2968 2969 return; 2970 } 2971