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