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