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