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