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