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