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