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