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