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