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 (bp != NULL) 1006 aac_enqueue_bio(sc, bp); 1007 if (cm != NULL) 1008 aac_release_command(cm); 1009 return(ENOMEM); 1010 } 1011 1012 /* 1013 * Handle a bio-instigated command that has been completed. 1014 */ 1015 static void 1016 aac_bio_complete(struct aac_command *cm) 1017 { 1018 struct aac_blockread_response *brr; 1019 struct aac_blockwrite_response *bwr; 1020 struct bio *bp; 1021 AAC_FSAStatus status; 1022 1023 /* fetch relevant status and then release the command */ 1024 bp = (struct bio *)cm->cm_private; 1025 if (bp->bio_cmd == BIO_READ) { 1026 brr = (struct aac_blockread_response *)&cm->cm_fib->data[0]; 1027 status = brr->Status; 1028 } else { 1029 bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0]; 1030 status = bwr->Status; 1031 } 1032 aac_release_command(cm); 1033 1034 /* fix up the bio based on status */ 1035 if (status == ST_OK) { 1036 bp->bio_resid = 0; 1037 } else { 1038 bp->bio_error = EIO; 1039 bp->bio_flags |= BIO_ERROR; 1040 /* pass an error string out to the disk layer */ 1041 bp->bio_driver1 = aac_describe_code(aac_command_status_table, 1042 status); 1043 } 1044 aac_biodone(bp); 1045 } 1046 1047 /* 1048 * Submit a command to the controller, return when it completes. 1049 * XXX This is very dangerous! If the card has gone out to lunch, we could 1050 * be stuck here forever. At the same time, signals are not caught 1051 * because there is a risk that a signal could wakeup the sleep before 1052 * the card has a chance to complete the command. Since there is no way 1053 * to cancel a command that is in progress, we can't protect against the 1054 * card completing a command late and spamming the command and data 1055 * memory. So, we are held hostage until the command completes. 1056 */ 1057 static int 1058 aac_wait_command(struct aac_command *cm) 1059 { 1060 struct aac_softc *sc; 1061 int error; 1062 1063 debug_called(2); 1064 1065 sc = cm->cm_sc; 1066 1067 /* Put the command on the ready queue and get things going */ 1068 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 1069 aac_enqueue_ready(cm); 1070 aac_startio(sc); 1071 error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0); 1072 return(error); 1073 } 1074 1075 /* 1076 *Command Buffer Management 1077 */ 1078 1079 /* 1080 * Allocate a command. 1081 */ 1082 int 1083 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp) 1084 { 1085 struct aac_command *cm; 1086 1087 debug_called(3); 1088 1089 if ((cm = aac_dequeue_free(sc)) == NULL) { 1090 if (sc->total_fibs < sc->aac_max_fibs) { 1091 sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS; 1092 wakeup(sc->aifthread); 1093 } 1094 return (EBUSY); 1095 } 1096 1097 *cmp = cm; 1098 return(0); 1099 } 1100 1101 /* 1102 * Release a command back to the freelist. 1103 */ 1104 void 1105 aac_release_command(struct aac_command *cm) 1106 { 1107 debug_called(3); 1108 1109 /* (re)initialise the command/FIB */ 1110 cm->cm_sgtable = NULL; 1111 cm->cm_flags = 0; 1112 cm->cm_complete = NULL; 1113 cm->cm_private = NULL; 1114 cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY; 1115 cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB; 1116 cm->cm_fib->Header.Flags = 0; 1117 cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib); 1118 1119 /* 1120 * These are duplicated in aac_start to cover the case where an 1121 * intermediate stage may have destroyed them. They're left 1122 * initialised here for debugging purposes only. 1123 */ 1124 cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys; 1125 cm->cm_fib->Header.SenderData = 0; 1126 1127 aac_enqueue_free(cm); 1128 } 1129 1130 /* 1131 * Map helper for command/FIB allocation. 1132 */ 1133 static void 1134 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1135 { 1136 uint32_t *fibphys; 1137 1138 fibphys = (uint32_t *)arg; 1139 1140 debug_called(3); 1141 1142 *fibphys = segs[0].ds_addr; 1143 } 1144 1145 /* 1146 * Allocate and initialise commands/FIBs for this adapter. 1147 */ 1148 static int 1149 aac_alloc_commands(struct aac_softc *sc) 1150 { 1151 struct aac_command *cm; 1152 struct aac_fibmap *fm; 1153 uint32_t fibphys; 1154 int i, error; 1155 1156 debug_called(2); 1157 1158 if (sc->total_fibs + AAC_FIB_COUNT > sc->aac_max_fibs) 1159 return (ENOMEM); 1160 1161 fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO); 1162 if (fm == NULL) 1163 return (ENOMEM); 1164 1165 /* allocate the FIBs in DMAable memory and load them */ 1166 if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs, 1167 BUS_DMA_NOWAIT, &fm->aac_fibmap)) { 1168 device_printf(sc->aac_dev, 1169 "Not enough contiguous memory available.\n"); 1170 free(fm, M_AACBUF); 1171 return (ENOMEM); 1172 } 1173 1174 /* Ignore errors since this doesn't bounce */ 1175 (void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs, 1176 AAC_FIB_COUNT * sizeof(struct aac_fib), 1177 aac_map_command_helper, &fibphys, 0); 1178 1179 /* initialise constant fields in the command structure */ 1180 mtx_lock(&sc->aac_io_lock); 1181 bzero(fm->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib)); 1182 for (i = 0; i < AAC_FIB_COUNT; i++) { 1183 cm = sc->aac_commands + sc->total_fibs; 1184 fm->aac_commands = cm; 1185 cm->cm_sc = sc; 1186 cm->cm_fib = fm->aac_fibs + i; 1187 cm->cm_fibphys = fibphys + (i * sizeof(struct aac_fib)); 1188 cm->cm_index = sc->total_fibs; 1189 1190 if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0, 1191 &cm->cm_datamap)) == 0) 1192 aac_release_command(cm); 1193 else 1194 break; 1195 sc->total_fibs++; 1196 } 1197 1198 if (i > 0) { 1199 TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link); 1200 debug(1, "total_fibs= %d\n", sc->total_fibs); 1201 mtx_unlock(&sc->aac_io_lock); 1202 return (0); 1203 } 1204 1205 mtx_unlock(&sc->aac_io_lock); 1206 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); 1207 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); 1208 free(fm, M_AACBUF); 1209 return (ENOMEM); 1210 } 1211 1212 /* 1213 * Free FIBs owned by this adapter. 1214 */ 1215 static void 1216 aac_free_commands(struct aac_softc *sc) 1217 { 1218 struct aac_fibmap *fm; 1219 struct aac_command *cm; 1220 int i; 1221 1222 debug_called(1); 1223 1224 while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) { 1225 1226 TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link); 1227 /* 1228 * We check against total_fibs to handle partially 1229 * allocated blocks. 1230 */ 1231 for (i = 0; i < AAC_FIB_COUNT && sc->total_fibs--; i++) { 1232 cm = fm->aac_commands + i; 1233 bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap); 1234 } 1235 bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap); 1236 bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap); 1237 free(fm, M_AACBUF); 1238 } 1239 } 1240 1241 /* 1242 * Command-mapping helper function - populate this command's s/g table. 1243 */ 1244 static void 1245 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1246 { 1247 struct aac_softc *sc; 1248 struct aac_command *cm; 1249 struct aac_fib *fib; 1250 int i; 1251 1252 debug_called(3); 1253 1254 cm = (struct aac_command *)arg; 1255 sc = cm->cm_sc; 1256 fib = cm->cm_fib; 1257 1258 /* copy into the FIB */ 1259 if (cm->cm_sgtable != NULL) { 1260 if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) { 1261 struct aac_sg_table *sg; 1262 sg = cm->cm_sgtable; 1263 sg->SgCount = nseg; 1264 for (i = 0; i < nseg; i++) { 1265 sg->SgEntry[i].SgAddress = segs[i].ds_addr; 1266 sg->SgEntry[i].SgByteCount = segs[i].ds_len; 1267 } 1268 /* update the FIB size for the s/g count */ 1269 fib->Header.Size += nseg * sizeof(struct aac_sg_entry); 1270 } else { 1271 struct aac_sg_table64 *sg; 1272 sg = (struct aac_sg_table64 *)cm->cm_sgtable; 1273 sg->SgCount = nseg; 1274 for (i = 0; i < nseg; i++) { 1275 sg->SgEntry64[i].SgAddress = segs[i].ds_addr; 1276 sg->SgEntry64[i].SgByteCount = segs[i].ds_len; 1277 } 1278 /* update the FIB size for the s/g count */ 1279 fib->Header.Size += nseg*sizeof(struct aac_sg_entry64); 1280 } 1281 } 1282 1283 /* Fix up the address values in the FIB. Use the command array index 1284 * instead of a pointer since these fields are only 32 bits. Shift 1285 * the SenderFibAddress over to make room for the fast response bit. 1286 */ 1287 cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 1); 1288 cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys; 1289 1290 /* save a pointer to the command for speedy reverse-lookup */ 1291 cm->cm_fib->Header.SenderData = cm->cm_index; 1292 1293 if (cm->cm_flags & AAC_CMD_DATAIN) 1294 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1295 BUS_DMASYNC_PREREAD); 1296 if (cm->cm_flags & AAC_CMD_DATAOUT) 1297 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1298 BUS_DMASYNC_PREWRITE); 1299 cm->cm_flags |= AAC_CMD_MAPPED; 1300 1301 /* Put the FIB on the outbound queue */ 1302 if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) { 1303 aac_unmap_command(cm); 1304 sc->flags |= AAC_QUEUE_FRZN; 1305 aac_requeue_ready(cm); 1306 } 1307 1308 return; 1309 } 1310 1311 /* 1312 * Unmap a command from controller-visible space. 1313 */ 1314 static void 1315 aac_unmap_command(struct aac_command *cm) 1316 { 1317 struct aac_softc *sc; 1318 1319 debug_called(2); 1320 1321 sc = cm->cm_sc; 1322 1323 if (!(cm->cm_flags & AAC_CMD_MAPPED)) 1324 return; 1325 1326 if (cm->cm_datalen != 0) { 1327 if (cm->cm_flags & AAC_CMD_DATAIN) 1328 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1329 BUS_DMASYNC_POSTREAD); 1330 if (cm->cm_flags & AAC_CMD_DATAOUT) 1331 bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap, 1332 BUS_DMASYNC_POSTWRITE); 1333 1334 bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap); 1335 } 1336 cm->cm_flags &= ~AAC_CMD_MAPPED; 1337 } 1338 1339 /* 1340 * Hardware Interface 1341 */ 1342 1343 /* 1344 * Initialise the adapter. 1345 */ 1346 static void 1347 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1348 { 1349 struct aac_softc *sc; 1350 1351 debug_called(1); 1352 1353 sc = (struct aac_softc *)arg; 1354 1355 sc->aac_common_busaddr = segs[0].ds_addr; 1356 } 1357 1358 static int 1359 aac_check_firmware(struct aac_softc *sc) 1360 { 1361 u_int32_t major, minor, options; 1362 1363 debug_called(1); 1364 1365 /* 1366 * Retrieve the firmware version numbers. Dell PERC2/QC cards with 1367 * firmware version 1.x are not compatible with this driver. 1368 */ 1369 if (sc->flags & AAC_FLAGS_PERC2QC) { 1370 if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0, 1371 NULL)) { 1372 device_printf(sc->aac_dev, 1373 "Error reading firmware version\n"); 1374 return (EIO); 1375 } 1376 1377 /* These numbers are stored as ASCII! */ 1378 major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30; 1379 minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30; 1380 if (major == 1) { 1381 device_printf(sc->aac_dev, 1382 "Firmware version %d.%d is not supported.\n", 1383 major, minor); 1384 return (EINVAL); 1385 } 1386 } 1387 1388 /* 1389 * Retrieve the capabilities/supported options word so we know what 1390 * work-arounds to enable. 1391 */ 1392 if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) { 1393 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 1394 return (EIO); 1395 } 1396 options = AAC_GET_MAILBOX(sc, 1); 1397 sc->supported_options = options; 1398 1399 if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 && 1400 (sc->flags & AAC_FLAGS_NO4GB) == 0) 1401 sc->flags |= AAC_FLAGS_4GB_WINDOW; 1402 if (options & AAC_SUPPORTED_NONDASD) 1403 sc->flags |= AAC_FLAGS_ENABLE_CAM; 1404 if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0 1405 && (sizeof(bus_addr_t) > 4)) { 1406 device_printf(sc->aac_dev, "Enabling 64-bit address support\n"); 1407 sc->flags |= AAC_FLAGS_SG_64BIT; 1408 } 1409 1410 /* Check for broken hardware that does a lower number of commands */ 1411 if ((sc->flags & AAC_FLAGS_256FIBS) == 0) 1412 sc->aac_max_fibs = AAC_MAX_FIBS; 1413 else 1414 sc->aac_max_fibs = 256; 1415 1416 return (0); 1417 } 1418 1419 static int 1420 aac_init(struct aac_softc *sc) 1421 { 1422 struct aac_adapter_init *ip; 1423 time_t then; 1424 u_int32_t code, qoffset; 1425 int error; 1426 1427 debug_called(1); 1428 1429 /* 1430 * First wait for the adapter to come ready. 1431 */ 1432 then = time_second; 1433 do { 1434 code = AAC_GET_FWSTATUS(sc); 1435 if (code & AAC_SELF_TEST_FAILED) { 1436 device_printf(sc->aac_dev, "FATAL: selftest failed\n"); 1437 return(ENXIO); 1438 } 1439 if (code & AAC_KERNEL_PANIC) { 1440 device_printf(sc->aac_dev, 1441 "FATAL: controller kernel panic\n"); 1442 return(ENXIO); 1443 } 1444 if (time_second > (then + AAC_BOOT_TIMEOUT)) { 1445 device_printf(sc->aac_dev, 1446 "FATAL: controller not coming ready, " 1447 "status %x\n", code); 1448 return(ENXIO); 1449 } 1450 } while (!(code & AAC_UP_AND_RUNNING)); 1451 1452 error = ENOMEM; 1453 /* 1454 * Create DMA tag for mapping buffers into controller-addressable space. 1455 */ 1456 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1457 1, 0, /* algnmnt, boundary */ 1458 (sc->flags & AAC_FLAGS_SG_64BIT) ? 1459 BUS_SPACE_MAXADDR : 1460 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1461 BUS_SPACE_MAXADDR, /* highaddr */ 1462 NULL, NULL, /* filter, filterarg */ 1463 MAXBSIZE, /* maxsize */ 1464 AAC_MAXSGENTRIES, /* nsegments */ 1465 MAXBSIZE, /* maxsegsize */ 1466 BUS_DMA_ALLOCNOW, /* flags */ 1467 busdma_lock_mutex, /* lockfunc */ 1468 &sc->aac_io_lock, /* lockfuncarg */ 1469 &sc->aac_buffer_dmat)) { 1470 device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n"); 1471 goto out; 1472 } 1473 1474 /* 1475 * Create DMA tag for mapping FIBs into controller-addressable space.. 1476 */ 1477 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1478 1, 0, /* algnmnt, boundary */ 1479 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1480 BUS_SPACE_MAXADDR_32BIT : 1481 0x7fffffff, /* lowaddr */ 1482 BUS_SPACE_MAXADDR, /* highaddr */ 1483 NULL, NULL, /* filter, filterarg */ 1484 AAC_FIB_COUNT * 1485 sizeof(struct aac_fib), /* maxsize */ 1486 1, /* nsegments */ 1487 AAC_FIB_COUNT * 1488 sizeof(struct aac_fib), /* maxsegsize */ 1489 0, /* flags */ 1490 NULL, NULL, /* No locking needed */ 1491 &sc->aac_fib_dmat)) { 1492 device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");; 1493 goto out; 1494 } 1495 1496 /* 1497 * Create DMA tag for the common structure and allocate it. 1498 */ 1499 if (bus_dma_tag_create(sc->aac_parent_dmat, /* parent */ 1500 1, 0, /* algnmnt, boundary */ 1501 (sc->flags & AAC_FLAGS_4GB_WINDOW) ? 1502 BUS_SPACE_MAXADDR_32BIT : 1503 0x7fffffff, /* lowaddr */ 1504 BUS_SPACE_MAXADDR, /* highaddr */ 1505 NULL, NULL, /* filter, filterarg */ 1506 8192 + sizeof(struct aac_common), /* maxsize */ 1507 1, /* nsegments */ 1508 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1509 0, /* flags */ 1510 NULL, NULL, /* No locking needed */ 1511 &sc->aac_common_dmat)) { 1512 device_printf(sc->aac_dev, 1513 "can't allocate common structure DMA tag\n"); 1514 goto out; 1515 } 1516 if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common, 1517 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) { 1518 device_printf(sc->aac_dev, "can't allocate common structure\n"); 1519 goto out; 1520 } 1521 1522 /* 1523 * Work around a bug in the 2120 and 2200 that cannot DMA commands 1524 * below address 8192 in physical memory. 1525 * XXX If the padding is not needed, can it be put to use instead 1526 * of ignored? 1527 */ 1528 (void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap, 1529 sc->aac_common, 8192 + sizeof(*sc->aac_common), 1530 aac_common_map, sc, 0); 1531 1532 if (sc->aac_common_busaddr < 8192) { 1533 sc->aac_common = (struct aac_common *) 1534 ((uint8_t *)sc->aac_common + 8192); 1535 sc->aac_common_busaddr += 8192; 1536 } 1537 bzero(sc->aac_common, sizeof(*sc->aac_common)); 1538 1539 /* Allocate some FIBs and associated command structs */ 1540 TAILQ_INIT(&sc->aac_fibmap_tqh); 1541 sc->aac_commands = malloc(AAC_MAX_FIBS * sizeof(struct aac_command), 1542 M_AACBUF, M_WAITOK|M_ZERO); 1543 while (sc->total_fibs < AAC_PREALLOCATE_FIBS) { 1544 if (aac_alloc_commands(sc) != 0) 1545 break; 1546 } 1547 if (sc->total_fibs == 0) 1548 goto out; 1549 1550 /* 1551 * Fill in the init structure. This tells the adapter about the 1552 * physical location of various important shared data structures. 1553 */ 1554 ip = &sc->aac_common->ac_init; 1555 ip->InitStructRevision = AAC_INIT_STRUCT_REVISION; 1556 ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION; 1557 1558 ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr + 1559 offsetof(struct aac_common, ac_fibs); 1560 ip->AdapterFibsVirtualAddress = 0; 1561 ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib); 1562 ip->AdapterFibAlign = sizeof(struct aac_fib); 1563 1564 ip->PrintfBufferAddress = sc->aac_common_busaddr + 1565 offsetof(struct aac_common, ac_printf); 1566 ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE; 1567 1568 /* 1569 * The adapter assumes that pages are 4K in size, except on some 1570 * broken firmware versions that do the page->byte conversion twice, 1571 * therefore 'assuming' that this value is in 16MB units (2^24). 1572 * Round up since the granularity is so high. 1573 */ 1574 ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE; 1575 if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) { 1576 ip->HostPhysMemPages = 1577 (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE; 1578 } 1579 ip->HostElapsedSeconds = time_second; /* reset later if invalid */ 1580 1581 /* 1582 * Initialise FIB queues. Note that it appears that the layout of the 1583 * indexes and the segmentation of the entries may be mandated by the 1584 * adapter, which is only told about the base of the queue index fields. 1585 * 1586 * The initial values of the indices are assumed to inform the adapter 1587 * of the sizes of the respective queues, and theoretically it could 1588 * work out the entire layout of the queue structures from this. We 1589 * take the easy route and just lay this area out like everyone else 1590 * does. 1591 * 1592 * The Linux driver uses a much more complex scheme whereby several 1593 * header records are kept for each queue. We use a couple of generic 1594 * list manipulation functions which 'know' the size of each list by 1595 * virtue of a table. 1596 */ 1597 qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN; 1598 qoffset &= ~(AAC_QUEUE_ALIGN - 1); 1599 sc->aac_queues = 1600 (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset); 1601 ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset; 1602 1603 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1604 AAC_HOST_NORM_CMD_ENTRIES; 1605 sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1606 AAC_HOST_NORM_CMD_ENTRIES; 1607 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1608 AAC_HOST_HIGH_CMD_ENTRIES; 1609 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1610 AAC_HOST_HIGH_CMD_ENTRIES; 1611 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1612 AAC_ADAP_NORM_CMD_ENTRIES; 1613 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1614 AAC_ADAP_NORM_CMD_ENTRIES; 1615 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] = 1616 AAC_ADAP_HIGH_CMD_ENTRIES; 1617 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] = 1618 AAC_ADAP_HIGH_CMD_ENTRIES; 1619 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1620 AAC_HOST_NORM_RESP_ENTRIES; 1621 sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1622 AAC_HOST_NORM_RESP_ENTRIES; 1623 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1624 AAC_HOST_HIGH_RESP_ENTRIES; 1625 sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1626 AAC_HOST_HIGH_RESP_ENTRIES; 1627 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1628 AAC_ADAP_NORM_RESP_ENTRIES; 1629 sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1630 AAC_ADAP_NORM_RESP_ENTRIES; 1631 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]= 1632 AAC_ADAP_HIGH_RESP_ENTRIES; 1633 sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]= 1634 AAC_ADAP_HIGH_RESP_ENTRIES; 1635 sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] = 1636 &sc->aac_queues->qt_HostNormCmdQueue[0]; 1637 sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] = 1638 &sc->aac_queues->qt_HostHighCmdQueue[0]; 1639 sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] = 1640 &sc->aac_queues->qt_AdapNormCmdQueue[0]; 1641 sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] = 1642 &sc->aac_queues->qt_AdapHighCmdQueue[0]; 1643 sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] = 1644 &sc->aac_queues->qt_HostNormRespQueue[0]; 1645 sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] = 1646 &sc->aac_queues->qt_HostHighRespQueue[0]; 1647 sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] = 1648 &sc->aac_queues->qt_AdapNormRespQueue[0]; 1649 sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] = 1650 &sc->aac_queues->qt_AdapHighRespQueue[0]; 1651 1652 /* 1653 * Do controller-type-specific initialisation 1654 */ 1655 switch (sc->aac_hwif) { 1656 case AAC_HWIF_I960RX: 1657 AAC_SETREG4(sc, AAC_RX_ODBR, ~0); 1658 break; 1659 case AAC_HWIF_RKT: 1660 AAC_SETREG4(sc, AAC_RKT_ODBR, ~0); 1661 break; 1662 default: 1663 break; 1664 } 1665 1666 /* 1667 * Give the init structure to the controller. 1668 */ 1669 if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT, 1670 sc->aac_common_busaddr + 1671 offsetof(struct aac_common, ac_init), 0, 0, 0, 1672 NULL)) { 1673 device_printf(sc->aac_dev, 1674 "error establishing init structure\n"); 1675 error = EIO; 1676 goto out; 1677 } 1678 1679 error = 0; 1680 out: 1681 return(error); 1682 } 1683 1684 /* 1685 * Send a synchronous command to the controller and wait for a result. 1686 */ 1687 static int 1688 aac_sync_command(struct aac_softc *sc, u_int32_t command, 1689 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3, 1690 u_int32_t *sp) 1691 { 1692 time_t then; 1693 u_int32_t status; 1694 1695 debug_called(3); 1696 1697 /* populate the mailbox */ 1698 AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3); 1699 1700 /* ensure the sync command doorbell flag is cleared */ 1701 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1702 1703 /* then set it to signal the adapter */ 1704 AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND); 1705 1706 /* spin waiting for the command to complete */ 1707 then = time_second; 1708 do { 1709 if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) { 1710 debug(1, "timed out"); 1711 return(EIO); 1712 } 1713 } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND)); 1714 1715 /* clear the completion flag */ 1716 AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND); 1717 1718 /* get the command status */ 1719 status = AAC_GET_MAILBOX(sc, 0); 1720 if (sp != NULL) 1721 *sp = status; 1722 return(0); 1723 } 1724 1725 int 1726 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate, 1727 struct aac_fib *fib, u_int16_t datasize) 1728 { 1729 debug_called(3); 1730 1731 if (datasize > AAC_FIB_DATASIZE) 1732 return(EINVAL); 1733 1734 /* 1735 * Set up the sync FIB 1736 */ 1737 fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED | 1738 AAC_FIBSTATE_INITIALISED | 1739 AAC_FIBSTATE_EMPTY; 1740 fib->Header.XferState |= xferstate; 1741 fib->Header.Command = command; 1742 fib->Header.StructType = AAC_FIBTYPE_TFIB; 1743 fib->Header.Size = sizeof(struct aac_fib) + datasize; 1744 fib->Header.SenderSize = sizeof(struct aac_fib); 1745 fib->Header.SenderFibAddress = 0; /* Not needed */ 1746 fib->Header.ReceiverFibAddress = sc->aac_common_busaddr + 1747 offsetof(struct aac_common, 1748 ac_sync_fib); 1749 1750 /* 1751 * Give the FIB to the controller, wait for a response. 1752 */ 1753 if (aac_sync_command(sc, AAC_MONKER_SYNCFIB, 1754 fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) { 1755 debug(2, "IO error"); 1756 return(EIO); 1757 } 1758 1759 return (0); 1760 } 1761 1762 /* 1763 * Adapter-space FIB queue manipulation 1764 * 1765 * Note that the queue implementation here is a little funky; neither the PI or 1766 * CI will ever be zero. This behaviour is a controller feature. 1767 */ 1768 static struct { 1769 int size; 1770 int notify; 1771 } aac_qinfo[] = { 1772 {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL}, 1773 {AAC_HOST_HIGH_CMD_ENTRIES, 0}, 1774 {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY}, 1775 {AAC_ADAP_HIGH_CMD_ENTRIES, 0}, 1776 {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL}, 1777 {AAC_HOST_HIGH_RESP_ENTRIES, 0}, 1778 {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY}, 1779 {AAC_ADAP_HIGH_RESP_ENTRIES, 0} 1780 }; 1781 1782 /* 1783 * Atomically insert an entry into the nominated queue, returns 0 on success or 1784 * EBUSY if the queue is full. 1785 * 1786 * Note: it would be more efficient to defer notifying the controller in 1787 * the case where we may be inserting several entries in rapid succession, 1788 * but implementing this usefully may be difficult (it would involve a 1789 * separate queue/notify interface). 1790 */ 1791 static int 1792 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm) 1793 { 1794 u_int32_t pi, ci; 1795 int error; 1796 u_int32_t fib_size; 1797 u_int32_t fib_addr; 1798 1799 debug_called(3); 1800 1801 fib_size = cm->cm_fib->Header.Size; 1802 fib_addr = cm->cm_fib->Header.ReceiverFibAddress; 1803 1804 /* get the producer/consumer indices */ 1805 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1806 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1807 1808 /* wrap the queue? */ 1809 if (pi >= aac_qinfo[queue].size) 1810 pi = 0; 1811 1812 /* check for queue full */ 1813 if ((pi + 1) == ci) { 1814 error = EBUSY; 1815 goto out; 1816 } 1817 1818 /* 1819 * To avoid a race with its completion interrupt, place this command on 1820 * the busy queue prior to advertising it to the controller. 1821 */ 1822 aac_enqueue_busy(cm); 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 /* notify the adapter if we know how */ 1832 if (aac_qinfo[queue].notify != 0) 1833 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1834 1835 error = 0; 1836 1837 out: 1838 return(error); 1839 } 1840 1841 /* 1842 * Atomically remove one entry from the nominated queue, returns 0 on 1843 * success or ENOENT if the queue is empty. 1844 */ 1845 static int 1846 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size, 1847 struct aac_fib **fib_addr) 1848 { 1849 u_int32_t pi, ci; 1850 u_int32_t fib_index; 1851 int error; 1852 int notify; 1853 1854 debug_called(3); 1855 1856 /* get the producer/consumer indices */ 1857 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1858 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1859 1860 /* check for queue empty */ 1861 if (ci == pi) { 1862 error = ENOENT; 1863 goto out; 1864 } 1865 1866 /* wrap the pi so the following test works */ 1867 if (pi >= aac_qinfo[queue].size) 1868 pi = 0; 1869 1870 notify = 0; 1871 if (ci == pi + 1) 1872 notify++; 1873 1874 /* wrap the queue? */ 1875 if (ci >= aac_qinfo[queue].size) 1876 ci = 0; 1877 1878 /* fetch the entry */ 1879 *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size; 1880 1881 switch (queue) { 1882 case AAC_HOST_NORM_CMD_QUEUE: 1883 case AAC_HOST_HIGH_CMD_QUEUE: 1884 /* 1885 * The aq_fib_addr is only 32 bits wide so it can't be counted 1886 * on to hold an address. For AIF's, the adapter assumes 1887 * that it's giving us an address into the array of AIF fibs. 1888 * Therefore, we have to convert it to an index. 1889 */ 1890 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr / 1891 sizeof(struct aac_fib); 1892 *fib_addr = &sc->aac_common->ac_fibs[fib_index]; 1893 break; 1894 1895 case AAC_HOST_NORM_RESP_QUEUE: 1896 case AAC_HOST_HIGH_RESP_QUEUE: 1897 { 1898 struct aac_command *cm; 1899 1900 /* 1901 * As above, an index is used instead of an actual address. 1902 * Gotta shift the index to account for the fast response 1903 * bit. No other correction is needed since this value was 1904 * originally provided by the driver via the SenderFibAddress 1905 * field. 1906 */ 1907 fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr; 1908 cm = sc->aac_commands + (fib_index >> 1); 1909 *fib_addr = cm->cm_fib; 1910 1911 /* 1912 * Is this a fast response? If it is, update the fib fields in 1913 * local memory since the whole fib isn't DMA'd back up. 1914 */ 1915 if (fib_index & 0x01) { 1916 (*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP; 1917 *((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL; 1918 } 1919 break; 1920 } 1921 default: 1922 panic("Invalid queue in aac_dequeue_fib()"); 1923 break; 1924 } 1925 1926 /* update consumer index */ 1927 sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1; 1928 1929 /* if we have made the queue un-full, notify the adapter */ 1930 if (notify && (aac_qinfo[queue].notify != 0)) 1931 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1932 error = 0; 1933 1934 out: 1935 return(error); 1936 } 1937 1938 /* 1939 * Put our response to an Adapter Initialed Fib on the response queue 1940 */ 1941 static int 1942 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib) 1943 { 1944 u_int32_t pi, ci; 1945 int error; 1946 u_int32_t fib_size; 1947 u_int32_t fib_addr; 1948 1949 debug_called(1); 1950 1951 /* Tell the adapter where the FIB is */ 1952 fib_size = fib->Header.Size; 1953 fib_addr = fib->Header.SenderFibAddress; 1954 fib->Header.ReceiverFibAddress = fib_addr; 1955 1956 /* get the producer/consumer indices */ 1957 pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX]; 1958 ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX]; 1959 1960 /* wrap the queue? */ 1961 if (pi >= aac_qinfo[queue].size) 1962 pi = 0; 1963 1964 /* check for queue full */ 1965 if ((pi + 1) == ci) { 1966 error = EBUSY; 1967 goto out; 1968 } 1969 1970 /* populate queue entry */ 1971 (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size; 1972 (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr; 1973 1974 /* update producer index */ 1975 sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1; 1976 1977 /* notify the adapter if we know how */ 1978 if (aac_qinfo[queue].notify != 0) 1979 AAC_QNOTIFY(sc, aac_qinfo[queue].notify); 1980 1981 error = 0; 1982 1983 out: 1984 return(error); 1985 } 1986 1987 /* 1988 * Check for commands that have been outstanding for a suspiciously long time, 1989 * and complain about them. 1990 */ 1991 static void 1992 aac_timeout(struct aac_softc *sc) 1993 { 1994 struct aac_command *cm; 1995 time_t deadline; 1996 int timedout, code; 1997 1998 /* 1999 * Traverse the busy command list, bitch about late commands once 2000 * only. 2001 */ 2002 timedout = 0; 2003 deadline = time_second - AAC_CMD_TIMEOUT; 2004 TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) { 2005 if ((cm->cm_timestamp < deadline) 2006 /* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) { 2007 cm->cm_flags |= AAC_CMD_TIMEDOUT; 2008 device_printf(sc->aac_dev, 2009 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", 2010 cm, (int)(time_second-cm->cm_timestamp)); 2011 AAC_PRINT_FIB(sc, cm->cm_fib); 2012 timedout++; 2013 } 2014 } 2015 2016 if (timedout) { 2017 code = AAC_GET_FWSTATUS(sc); 2018 if (code != AAC_UP_AND_RUNNING) { 2019 device_printf(sc->aac_dev, "WARNING! Controller is no " 2020 "longer running! code= 0x%x\n", code); 2021 } 2022 } 2023 return; 2024 } 2025 2026 /* 2027 * Interface Function Vectors 2028 */ 2029 2030 /* 2031 * Read the current firmware status word. 2032 */ 2033 static int 2034 aac_sa_get_fwstatus(struct aac_softc *sc) 2035 { 2036 debug_called(3); 2037 2038 return(AAC_GETREG4(sc, AAC_SA_FWSTATUS)); 2039 } 2040 2041 static int 2042 aac_rx_get_fwstatus(struct aac_softc *sc) 2043 { 2044 debug_called(3); 2045 2046 return(AAC_GETREG4(sc, AAC_RX_FWSTATUS)); 2047 } 2048 2049 static int 2050 aac_fa_get_fwstatus(struct aac_softc *sc) 2051 { 2052 int val; 2053 2054 debug_called(3); 2055 2056 val = AAC_GETREG4(sc, AAC_FA_FWSTATUS); 2057 return (val); 2058 } 2059 2060 static int 2061 aac_rkt_get_fwstatus(struct aac_softc *sc) 2062 { 2063 debug_called(3); 2064 2065 return(AAC_GETREG4(sc, AAC_RKT_FWSTATUS)); 2066 } 2067 2068 /* 2069 * Notify the controller of a change in a given queue 2070 */ 2071 2072 static void 2073 aac_sa_qnotify(struct aac_softc *sc, int qbit) 2074 { 2075 debug_called(3); 2076 2077 AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit); 2078 } 2079 2080 static void 2081 aac_rx_qnotify(struct aac_softc *sc, int qbit) 2082 { 2083 debug_called(3); 2084 2085 AAC_SETREG4(sc, AAC_RX_IDBR, qbit); 2086 } 2087 2088 static void 2089 aac_fa_qnotify(struct aac_softc *sc, int qbit) 2090 { 2091 debug_called(3); 2092 2093 AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit); 2094 AAC_FA_HACK(sc); 2095 } 2096 2097 static void 2098 aac_rkt_qnotify(struct aac_softc *sc, int qbit) 2099 { 2100 debug_called(3); 2101 2102 AAC_SETREG4(sc, AAC_RKT_IDBR, qbit); 2103 } 2104 2105 /* 2106 * Get the interrupt reason bits 2107 */ 2108 static int 2109 aac_sa_get_istatus(struct aac_softc *sc) 2110 { 2111 debug_called(3); 2112 2113 return(AAC_GETREG2(sc, AAC_SA_DOORBELL0)); 2114 } 2115 2116 static int 2117 aac_rx_get_istatus(struct aac_softc *sc) 2118 { 2119 debug_called(3); 2120 2121 return(AAC_GETREG4(sc, AAC_RX_ODBR)); 2122 } 2123 2124 static int 2125 aac_fa_get_istatus(struct aac_softc *sc) 2126 { 2127 int val; 2128 2129 debug_called(3); 2130 2131 val = AAC_GETREG2(sc, AAC_FA_DOORBELL0); 2132 return (val); 2133 } 2134 2135 static int 2136 aac_rkt_get_istatus(struct aac_softc *sc) 2137 { 2138 debug_called(3); 2139 2140 return(AAC_GETREG4(sc, AAC_RKT_ODBR)); 2141 } 2142 2143 /* 2144 * Clear some interrupt reason bits 2145 */ 2146 static void 2147 aac_sa_clear_istatus(struct aac_softc *sc, int mask) 2148 { 2149 debug_called(3); 2150 2151 AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask); 2152 } 2153 2154 static void 2155 aac_rx_clear_istatus(struct aac_softc *sc, int mask) 2156 { 2157 debug_called(3); 2158 2159 AAC_SETREG4(sc, AAC_RX_ODBR, mask); 2160 } 2161 2162 static void 2163 aac_fa_clear_istatus(struct aac_softc *sc, int mask) 2164 { 2165 debug_called(3); 2166 2167 AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask); 2168 AAC_FA_HACK(sc); 2169 } 2170 2171 static void 2172 aac_rkt_clear_istatus(struct aac_softc *sc, int mask) 2173 { 2174 debug_called(3); 2175 2176 AAC_SETREG4(sc, AAC_RKT_ODBR, mask); 2177 } 2178 2179 /* 2180 * Populate the mailbox and set the command word 2181 */ 2182 static void 2183 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2184 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2185 { 2186 debug_called(4); 2187 2188 AAC_SETREG4(sc, AAC_SA_MAILBOX, command); 2189 AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0); 2190 AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1); 2191 AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2); 2192 AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3); 2193 } 2194 2195 static void 2196 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command, 2197 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2198 { 2199 debug_called(4); 2200 2201 AAC_SETREG4(sc, AAC_RX_MAILBOX, command); 2202 AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0); 2203 AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1); 2204 AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2); 2205 AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3); 2206 } 2207 2208 static void 2209 aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command, 2210 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2211 { 2212 debug_called(4); 2213 2214 AAC_SETREG4(sc, AAC_FA_MAILBOX, command); 2215 AAC_FA_HACK(sc); 2216 AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0); 2217 AAC_FA_HACK(sc); 2218 AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1); 2219 AAC_FA_HACK(sc); 2220 AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2); 2221 AAC_FA_HACK(sc); 2222 AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3); 2223 AAC_FA_HACK(sc); 2224 } 2225 2226 static void 2227 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0, 2228 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3) 2229 { 2230 debug_called(4); 2231 2232 AAC_SETREG4(sc, AAC_RKT_MAILBOX, command); 2233 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0); 2234 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1); 2235 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2); 2236 AAC_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3); 2237 } 2238 2239 /* 2240 * Fetch the immediate command status word 2241 */ 2242 static int 2243 aac_sa_get_mailbox(struct aac_softc *sc, int mb) 2244 { 2245 debug_called(4); 2246 2247 return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4))); 2248 } 2249 2250 static int 2251 aac_rx_get_mailbox(struct aac_softc *sc, int mb) 2252 { 2253 debug_called(4); 2254 2255 return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4))); 2256 } 2257 2258 static int 2259 aac_fa_get_mailbox(struct aac_softc *sc, int mb) 2260 { 2261 int val; 2262 2263 debug_called(4); 2264 2265 val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4)); 2266 return (val); 2267 } 2268 2269 static int 2270 aac_rkt_get_mailbox(struct aac_softc *sc, int mb) 2271 { 2272 debug_called(4); 2273 2274 return(AAC_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4))); 2275 } 2276 2277 /* 2278 * Set/clear interrupt masks 2279 */ 2280 static void 2281 aac_sa_set_interrupts(struct aac_softc *sc, int enable) 2282 { 2283 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2284 2285 if (enable) { 2286 AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2287 } else { 2288 AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0); 2289 } 2290 } 2291 2292 static void 2293 aac_rx_set_interrupts(struct aac_softc *sc, int enable) 2294 { 2295 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2296 2297 if (enable) { 2298 AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS); 2299 } else { 2300 AAC_SETREG4(sc, AAC_RX_OIMR, ~0); 2301 } 2302 } 2303 2304 static void 2305 aac_fa_set_interrupts(struct aac_softc *sc, int enable) 2306 { 2307 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2308 2309 if (enable) { 2310 AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS); 2311 AAC_FA_HACK(sc); 2312 } else { 2313 AAC_SETREG2((sc), AAC_FA_MASK0, ~0); 2314 AAC_FA_HACK(sc); 2315 } 2316 } 2317 2318 static void 2319 aac_rkt_set_interrupts(struct aac_softc *sc, int enable) 2320 { 2321 debug(2, "%sable interrupts", enable ? "en" : "dis"); 2322 2323 if (enable) { 2324 AAC_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS); 2325 } else { 2326 AAC_SETREG4(sc, AAC_RKT_OIMR, ~0); 2327 } 2328 } 2329 2330 /* 2331 * Debugging and Diagnostics 2332 */ 2333 2334 /* 2335 * Print some information about the controller. 2336 */ 2337 static void 2338 aac_describe_controller(struct aac_softc *sc) 2339 { 2340 struct aac_fib *fib; 2341 struct aac_adapter_info *info; 2342 2343 debug_called(2); 2344 2345 aac_alloc_sync_fib(sc, &fib); 2346 2347 fib->data[0] = 0; 2348 if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) { 2349 device_printf(sc->aac_dev, "RequestAdapterInfo failed\n"); 2350 aac_release_sync_fib(sc); 2351 return; 2352 } 2353 2354 /* save the kernel revision structure for later use */ 2355 info = (struct aac_adapter_info *)&fib->data[0]; 2356 sc->aac_revision = info->KernelRevision; 2357 2358 if (bootverbose) { 2359 device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n", 2360 aac_describe_code(aac_cpu_variant, info->CpuVariant), 2361 info->ClockSpeed, info->BufferMem / (1024 * 1024), 2362 aac_describe_code(aac_battery_platform, 2363 info->batteryPlatform)); 2364 2365 device_printf(sc->aac_dev, 2366 "Kernel %d.%d-%d, Build %d, S/N %6X\n", 2367 info->KernelRevision.external.comp.major, 2368 info->KernelRevision.external.comp.minor, 2369 info->KernelRevision.external.comp.dash, 2370 info->KernelRevision.buildNumber, 2371 (u_int32_t)(info->SerialNumber & 0xffffff)); 2372 2373 device_printf(sc->aac_dev, "Supported Options=%b\n", 2374 sc->supported_options, 2375 "\20" 2376 "\1SNAPSHOT" 2377 "\2CLUSTERS" 2378 "\3WCACHE" 2379 "\4DATA64" 2380 "\5HOSTTIME" 2381 "\6RAID50" 2382 "\7WINDOW4GB" 2383 "\10SCSIUPGD" 2384 "\11SOFTERR" 2385 "\12NORECOND" 2386 "\13SGMAP64" 2387 "\14ALARM" 2388 "\15NONDASD"); 2389 } 2390 aac_release_sync_fib(sc); 2391 } 2392 2393 /* 2394 * Look up a text description of a numeric error code and return a pointer to 2395 * same. 2396 */ 2397 static char * 2398 aac_describe_code(struct aac_code_lookup *table, u_int32_t code) 2399 { 2400 int i; 2401 2402 for (i = 0; table[i].string != NULL; i++) 2403 if (table[i].code == code) 2404 return(table[i].string); 2405 return(table[i + 1].string); 2406 } 2407 2408 /* 2409 * Management Interface 2410 */ 2411 2412 static int 2413 aac_open(struct cdev *dev, int flags, int fmt, d_thread_t *td) 2414 { 2415 struct aac_softc *sc; 2416 2417 debug_called(2); 2418 2419 sc = dev->si_drv1; 2420 2421 /* Check to make sure the device isn't already open */ 2422 if (sc->aac_state & AAC_STATE_OPEN) { 2423 return EBUSY; 2424 } 2425 sc->aac_state |= AAC_STATE_OPEN; 2426 2427 return 0; 2428 } 2429 2430 static int 2431 aac_close(struct cdev *dev, int flags, int fmt, d_thread_t *td) 2432 { 2433 struct aac_softc *sc; 2434 2435 debug_called(2); 2436 2437 sc = dev->si_drv1; 2438 2439 /* Mark this unit as no longer open */ 2440 sc->aac_state &= ~AAC_STATE_OPEN; 2441 2442 return 0; 2443 } 2444 2445 static int 2446 aac_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td) 2447 { 2448 union aac_statrequest *as; 2449 struct aac_softc *sc; 2450 int error = 0; 2451 uint32_t cookie; 2452 2453 debug_called(2); 2454 2455 as = (union aac_statrequest *)arg; 2456 sc = dev->si_drv1; 2457 2458 switch (cmd) { 2459 case AACIO_STATS: 2460 switch (as->as_item) { 2461 case AACQ_FREE: 2462 case AACQ_BIO: 2463 case AACQ_READY: 2464 case AACQ_BUSY: 2465 bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat, 2466 sizeof(struct aac_qstat)); 2467 break; 2468 default: 2469 error = ENOENT; 2470 break; 2471 } 2472 break; 2473 2474 case FSACTL_SENDFIB: 2475 arg = *(caddr_t*)arg; 2476 case FSACTL_LNX_SENDFIB: 2477 debug(1, "FSACTL_SENDFIB"); 2478 error = aac_ioctl_sendfib(sc, arg); 2479 break; 2480 case FSACTL_AIF_THREAD: 2481 case FSACTL_LNX_AIF_THREAD: 2482 debug(1, "FSACTL_AIF_THREAD"); 2483 error = EINVAL; 2484 break; 2485 case FSACTL_OPEN_GET_ADAPTER_FIB: 2486 arg = *(caddr_t*)arg; 2487 case FSACTL_LNX_OPEN_GET_ADAPTER_FIB: 2488 debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB"); 2489 /* 2490 * Pass the caller out an AdapterFibContext. 2491 * 2492 * Note that because we only support one opener, we 2493 * basically ignore this. Set the caller's context to a magic 2494 * number just in case. 2495 * 2496 * The Linux code hands the driver a pointer into kernel space, 2497 * and then trusts it when the caller hands it back. Aiee! 2498 * Here, we give it the proc pointer of the per-adapter aif 2499 * thread. It's only used as a sanity check in other calls. 2500 */ 2501 cookie = (uint32_t)(uintptr_t)sc->aifthread; 2502 error = copyout(&cookie, arg, sizeof(cookie)); 2503 break; 2504 case FSACTL_GET_NEXT_ADAPTER_FIB: 2505 arg = *(caddr_t*)arg; 2506 case FSACTL_LNX_GET_NEXT_ADAPTER_FIB: 2507 debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB"); 2508 error = aac_getnext_aif(sc, arg); 2509 break; 2510 case FSACTL_CLOSE_GET_ADAPTER_FIB: 2511 case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB: 2512 debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB"); 2513 /* don't do anything here */ 2514 break; 2515 case FSACTL_MINIPORT_REV_CHECK: 2516 arg = *(caddr_t*)arg; 2517 case FSACTL_LNX_MINIPORT_REV_CHECK: 2518 debug(1, "FSACTL_MINIPORT_REV_CHECK"); 2519 error = aac_rev_check(sc, arg); 2520 break; 2521 case FSACTL_QUERY_DISK: 2522 arg = *(caddr_t*)arg; 2523 case FSACTL_LNX_QUERY_DISK: 2524 debug(1, "FSACTL_QUERY_DISK"); 2525 error = aac_query_disk(sc, arg); 2526 break; 2527 case FSACTL_DELETE_DISK: 2528 case FSACTL_LNX_DELETE_DISK: 2529 /* 2530 * We don't trust the underland to tell us when to delete a 2531 * container, rather we rely on an AIF coming from the 2532 * controller 2533 */ 2534 error = 0; 2535 break; 2536 default: 2537 debug(1, "unsupported cmd 0x%lx\n", cmd); 2538 error = EINVAL; 2539 break; 2540 } 2541 return(error); 2542 } 2543 2544 static int 2545 aac_poll(struct cdev *dev, int poll_events, d_thread_t *td) 2546 { 2547 struct aac_softc *sc; 2548 int revents; 2549 2550 sc = dev->si_drv1; 2551 revents = 0; 2552 2553 mtx_lock(&sc->aac_aifq_lock); 2554 if ((poll_events & (POLLRDNORM | POLLIN)) != 0) { 2555 if (sc->aac_aifq_tail != sc->aac_aifq_head) 2556 revents |= poll_events & (POLLIN | POLLRDNORM); 2557 } 2558 mtx_unlock(&sc->aac_aifq_lock); 2559 2560 if (revents == 0) { 2561 if (poll_events & (POLLIN | POLLRDNORM)) 2562 selrecord(td, &sc->rcv_select); 2563 } 2564 2565 return (revents); 2566 } 2567 2568 /* 2569 * Send a FIB supplied from userspace 2570 */ 2571 static int 2572 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib) 2573 { 2574 struct aac_command *cm; 2575 int size, error; 2576 2577 debug_called(2); 2578 2579 cm = NULL; 2580 2581 /* 2582 * Get a command 2583 */ 2584 mtx_lock(&sc->aac_io_lock); 2585 if (aac_alloc_command(sc, &cm)) { 2586 error = EBUSY; 2587 goto out; 2588 } 2589 2590 /* 2591 * Fetch the FIB header, then re-copy to get data as well. 2592 */ 2593 if ((error = copyin(ufib, cm->cm_fib, 2594 sizeof(struct aac_fib_header))) != 0) 2595 goto out; 2596 size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header); 2597 if (size > sizeof(struct aac_fib)) { 2598 device_printf(sc->aac_dev, "incoming FIB oversized (%d > %zd)\n", 2599 size, sizeof(struct aac_fib)); 2600 size = sizeof(struct aac_fib); 2601 } 2602 if ((error = copyin(ufib, cm->cm_fib, size)) != 0) 2603 goto out; 2604 cm->cm_fib->Header.Size = size; 2605 cm->cm_timestamp = time_second; 2606 2607 /* 2608 * Pass the FIB to the controller, wait for it to complete. 2609 */ 2610 if ((error = aac_wait_command(cm)) != 0) { 2611 device_printf(sc->aac_dev, 2612 "aac_wait_command return %d\n", error); 2613 goto out; 2614 } 2615 2616 /* 2617 * Copy the FIB and data back out to the caller. 2618 */ 2619 size = cm->cm_fib->Header.Size; 2620 if (size > sizeof(struct aac_fib)) { 2621 device_printf(sc->aac_dev, "outbound FIB oversized (%d > %zd)\n", 2622 size, sizeof(struct aac_fib)); 2623 size = sizeof(struct aac_fib); 2624 } 2625 error = copyout(cm->cm_fib, ufib, size); 2626 2627 out: 2628 if (cm != NULL) { 2629 aac_release_command(cm); 2630 } 2631 2632 mtx_unlock(&sc->aac_io_lock); 2633 return(error); 2634 } 2635 2636 /* 2637 * Handle an AIF sent to us by the controller; queue it for later reference. 2638 * If the queue fills up, then drop the older entries. 2639 */ 2640 static void 2641 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib) 2642 { 2643 struct aac_aif_command *aif; 2644 struct aac_container *co, *co_next; 2645 struct aac_mntinfo *mi; 2646 struct aac_mntinforesp *mir = NULL; 2647 u_int16_t rsize; 2648 int next, found; 2649 int count = 0, added = 0, i = 0; 2650 2651 debug_called(2); 2652 2653 aif = (struct aac_aif_command*)&fib->data[0]; 2654 aac_print_aif(sc, aif); 2655 2656 /* Is it an event that we should care about? */ 2657 switch (aif->command) { 2658 case AifCmdEventNotify: 2659 switch (aif->data.EN.type) { 2660 case AifEnAddContainer: 2661 case AifEnDeleteContainer: 2662 /* 2663 * A container was added or deleted, but the message 2664 * doesn't tell us anything else! Re-enumerate the 2665 * containers and sort things out. 2666 */ 2667 aac_alloc_sync_fib(sc, &fib); 2668 mi = (struct aac_mntinfo *)&fib->data[0]; 2669 do { 2670 /* 2671 * Ask the controller for its containers one at 2672 * a time. 2673 * XXX What if the controller's list changes 2674 * midway through this enumaration? 2675 * XXX This should be done async. 2676 */ 2677 bzero(mi, sizeof(struct aac_mntinfo)); 2678 mi->Command = VM_NameServe; 2679 mi->MntType = FT_FILESYS; 2680 mi->MntCount = i; 2681 rsize = sizeof(mir); 2682 if (aac_sync_fib(sc, ContainerCommand, 0, fib, 2683 sizeof(struct aac_mntinfo))) { 2684 printf("Error probing container %d\n", 2685 i); 2686 continue; 2687 } 2688 mir = (struct aac_mntinforesp *)&fib->data[0]; 2689 /* XXX Need to check if count changed */ 2690 count = mir->MntRespCount; 2691 /* 2692 * Check the container against our list. 2693 * co->co_found was already set to 0 in a 2694 * previous run. 2695 */ 2696 if ((mir->Status == ST_OK) && 2697 (mir->MntTable[0].VolType != CT_NONE)) { 2698 found = 0; 2699 TAILQ_FOREACH(co, 2700 &sc->aac_container_tqh, 2701 co_link) { 2702 if (co->co_mntobj.ObjectId == 2703 mir->MntTable[0].ObjectId) { 2704 co->co_found = 1; 2705 found = 1; 2706 break; 2707 } 2708 } 2709 /* 2710 * If the container matched, continue 2711 * in the list. 2712 */ 2713 if (found) { 2714 i++; 2715 continue; 2716 } 2717 2718 /* 2719 * This is a new container. Do all the 2720 * appropriate things to set it up. 2721 */ 2722 aac_add_container(sc, mir, 1); 2723 added = 1; 2724 } 2725 i++; 2726 } while ((i < count) && (i < AAC_MAX_CONTAINERS)); 2727 aac_release_sync_fib(sc); 2728 2729 /* 2730 * Go through our list of containers and see which ones 2731 * were not marked 'found'. Since the controller didn't 2732 * list them they must have been deleted. Do the 2733 * appropriate steps to destroy the device. Also reset 2734 * the co->co_found field. 2735 */ 2736 co = TAILQ_FIRST(&sc->aac_container_tqh); 2737 while (co != NULL) { 2738 if (co->co_found == 0) { 2739 device_delete_child(sc->aac_dev, 2740 co->co_disk); 2741 co_next = TAILQ_NEXT(co, co_link); 2742 mtx_lock(&sc->aac_container_lock); 2743 TAILQ_REMOVE(&sc->aac_container_tqh, co, 2744 co_link); 2745 mtx_unlock(&sc->aac_container_lock); 2746 free(co, M_AACBUF); 2747 co = co_next; 2748 } else { 2749 co->co_found = 0; 2750 co = TAILQ_NEXT(co, co_link); 2751 } 2752 } 2753 2754 /* Attach the newly created containers */ 2755 if (added) 2756 bus_generic_attach(sc->aac_dev); 2757 2758 break; 2759 2760 default: 2761 break; 2762 } 2763 2764 default: 2765 break; 2766 } 2767 2768 /* Copy the AIF data to the AIF queue for ioctl retrieval */ 2769 mtx_lock(&sc->aac_aifq_lock); 2770 next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH; 2771 if (next != sc->aac_aifq_tail) { 2772 bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command)); 2773 sc->aac_aifq_head = next; 2774 2775 /* On the off chance that someone is sleeping for an aif... */ 2776 if (sc->aac_state & AAC_STATE_AIF_SLEEPER) 2777 wakeup(sc->aac_aifq); 2778 /* Wakeup any poll()ers */ 2779 selwakeuppri(&sc->rcv_select, PRIBIO); 2780 } 2781 mtx_unlock(&sc->aac_aifq_lock); 2782 2783 return; 2784 } 2785 2786 /* 2787 * Return the Revision of the driver to userspace and check to see if the 2788 * userspace app is possibly compatible. This is extremely bogus since 2789 * our driver doesn't follow Adaptec's versioning system. Cheat by just 2790 * returning what the card reported. 2791 */ 2792 static int 2793 aac_rev_check(struct aac_softc *sc, caddr_t udata) 2794 { 2795 struct aac_rev_check rev_check; 2796 struct aac_rev_check_resp rev_check_resp; 2797 int error = 0; 2798 2799 debug_called(2); 2800 2801 /* 2802 * Copyin the revision struct from userspace 2803 */ 2804 if ((error = copyin(udata, (caddr_t)&rev_check, 2805 sizeof(struct aac_rev_check))) != 0) { 2806 return error; 2807 } 2808 2809 debug(2, "Userland revision= %d\n", 2810 rev_check.callingRevision.buildNumber); 2811 2812 /* 2813 * Doctor up the response struct. 2814 */ 2815 rev_check_resp.possiblyCompatible = 1; 2816 rev_check_resp.adapterSWRevision.external.ul = 2817 sc->aac_revision.external.ul; 2818 rev_check_resp.adapterSWRevision.buildNumber = 2819 sc->aac_revision.buildNumber; 2820 2821 return(copyout((caddr_t)&rev_check_resp, udata, 2822 sizeof(struct aac_rev_check_resp))); 2823 } 2824 2825 /* 2826 * Pass the caller the next AIF in their queue 2827 */ 2828 static int 2829 aac_getnext_aif(struct aac_softc *sc, caddr_t arg) 2830 { 2831 struct get_adapter_fib_ioctl agf; 2832 int error; 2833 2834 debug_called(2); 2835 2836 if ((error = copyin(arg, &agf, sizeof(agf))) == 0) { 2837 2838 /* 2839 * Check the magic number that we gave the caller. 2840 */ 2841 if (agf.AdapterFibContext != (int)(uintptr_t)sc->aifthread) { 2842 error = EFAULT; 2843 } else { 2844 error = aac_return_aif(sc, agf.AifFib); 2845 if ((error == EAGAIN) && (agf.Wait)) { 2846 sc->aac_state |= AAC_STATE_AIF_SLEEPER; 2847 while (error == EAGAIN) { 2848 error = tsleep(sc->aac_aifq, PRIBIO | 2849 PCATCH, "aacaif", 0); 2850 if (error == 0) 2851 error = aac_return_aif(sc, 2852 agf.AifFib); 2853 } 2854 sc->aac_state &= ~AAC_STATE_AIF_SLEEPER; 2855 } 2856 } 2857 } 2858 return(error); 2859 } 2860 2861 /* 2862 * Hand the next AIF off the top of the queue out to userspace. 2863 */ 2864 static int 2865 aac_return_aif(struct aac_softc *sc, caddr_t uptr) 2866 { 2867 int next, error; 2868 2869 debug_called(2); 2870 2871 mtx_lock(&sc->aac_aifq_lock); 2872 if (sc->aac_aifq_tail == sc->aac_aifq_head) { 2873 mtx_unlock(&sc->aac_aifq_lock); 2874 return (EAGAIN); 2875 } 2876 2877 next = (sc->aac_aifq_tail + 1) % AAC_AIFQ_LENGTH; 2878 error = copyout(&sc->aac_aifq[next], uptr, 2879 sizeof(struct aac_aif_command)); 2880 if (error) 2881 device_printf(sc->aac_dev, 2882 "aac_return_aif: copyout returned %d\n", error); 2883 else 2884 sc->aac_aifq_tail = next; 2885 2886 mtx_unlock(&sc->aac_aifq_lock); 2887 return(error); 2888 } 2889 2890 /* 2891 * Give the userland some information about the container. The AAC arch 2892 * expects the driver to be a SCSI passthrough type driver, so it expects 2893 * the containers to have b:t:l numbers. Fake it. 2894 */ 2895 static int 2896 aac_query_disk(struct aac_softc *sc, caddr_t uptr) 2897 { 2898 struct aac_query_disk query_disk; 2899 struct aac_container *co; 2900 struct aac_disk *disk; 2901 int error, id; 2902 2903 debug_called(2); 2904 2905 disk = NULL; 2906 2907 error = copyin(uptr, (caddr_t)&query_disk, 2908 sizeof(struct aac_query_disk)); 2909 if (error) 2910 return (error); 2911 2912 id = query_disk.ContainerNumber; 2913 if (id == -1) 2914 return (EINVAL); 2915 2916 mtx_lock(&sc->aac_container_lock); 2917 TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) { 2918 if (co->co_mntobj.ObjectId == id) 2919 break; 2920 } 2921 2922 if (co == NULL) { 2923 query_disk.Valid = 0; 2924 query_disk.Locked = 0; 2925 query_disk.Deleted = 1; /* XXX is this right? */ 2926 } else { 2927 disk = device_get_softc(co->co_disk); 2928 query_disk.Valid = 1; 2929 query_disk.Locked = 2930 (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0; 2931 query_disk.Deleted = 0; 2932 query_disk.Bus = device_get_unit(sc->aac_dev); 2933 query_disk.Target = disk->unit; 2934 query_disk.Lun = 0; 2935 query_disk.UnMapped = 0; 2936 sprintf(&query_disk.diskDeviceName[0], "%s%d", 2937 disk->ad_disk->d_name, disk->ad_disk->d_unit); 2938 } 2939 mtx_unlock(&sc->aac_container_lock); 2940 2941 error = copyout((caddr_t)&query_disk, uptr, 2942 sizeof(struct aac_query_disk)); 2943 2944 return (error); 2945 } 2946 2947 static void 2948 aac_get_bus_info(struct aac_softc *sc) 2949 { 2950 struct aac_fib *fib; 2951 struct aac_ctcfg *c_cmd; 2952 struct aac_ctcfg_resp *c_resp; 2953 struct aac_vmioctl *vmi; 2954 struct aac_vmi_businf_resp *vmi_resp; 2955 struct aac_getbusinf businfo; 2956 struct aac_sim *caminf; 2957 device_t child; 2958 int i, found, error; 2959 2960 aac_alloc_sync_fib(sc, &fib); 2961 c_cmd = (struct aac_ctcfg *)&fib->data[0]; 2962 bzero(c_cmd, sizeof(struct aac_ctcfg)); 2963 2964 c_cmd->Command = VM_ContainerConfig; 2965 c_cmd->cmd = CT_GET_SCSI_METHOD; 2966 c_cmd->param = 0; 2967 2968 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2969 sizeof(struct aac_ctcfg)); 2970 if (error) { 2971 device_printf(sc->aac_dev, "Error %d sending " 2972 "VM_ContainerConfig command\n", error); 2973 aac_release_sync_fib(sc); 2974 return; 2975 } 2976 2977 c_resp = (struct aac_ctcfg_resp *)&fib->data[0]; 2978 if (c_resp->Status != ST_OK) { 2979 device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n", 2980 c_resp->Status); 2981 aac_release_sync_fib(sc); 2982 return; 2983 } 2984 2985 sc->scsi_method_id = c_resp->param; 2986 2987 vmi = (struct aac_vmioctl *)&fib->data[0]; 2988 bzero(vmi, sizeof(struct aac_vmioctl)); 2989 2990 vmi->Command = VM_Ioctl; 2991 vmi->ObjType = FT_DRIVE; 2992 vmi->MethId = sc->scsi_method_id; 2993 vmi->ObjId = 0; 2994 vmi->IoctlCmd = GetBusInfo; 2995 2996 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 2997 sizeof(struct aac_vmioctl)); 2998 if (error) { 2999 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n", 3000 error); 3001 aac_release_sync_fib(sc); 3002 return; 3003 } 3004 3005 vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0]; 3006 if (vmi_resp->Status != ST_OK) { 3007 device_printf(sc->aac_dev, "VM_Ioctl returned %d\n", 3008 vmi_resp->Status); 3009 aac_release_sync_fib(sc); 3010 return; 3011 } 3012 3013 bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf)); 3014 aac_release_sync_fib(sc); 3015 3016 found = 0; 3017 for (i = 0; i < businfo.BusCount; i++) { 3018 if (businfo.BusValid[i] != AAC_BUS_VALID) 3019 continue; 3020 3021 caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim), 3022 M_AACBUF, M_NOWAIT | M_ZERO); 3023 if (caminf == NULL) { 3024 device_printf(sc->aac_dev, 3025 "No memory to add passthrough bus %d\n", i); 3026 break; 3027 } 3028 3029 child = device_add_child(sc->aac_dev, "aacp", -1); 3030 if (child == NULL) { 3031 device_printf(sc->aac_dev, 3032 "device_add_child failed for passthrough bus %d\n", 3033 i); 3034 free(caminf, M_AACBUF); 3035 break; 3036 } 3037 3038 caminf->TargetsPerBus = businfo.TargetsPerBus; 3039 caminf->BusNumber = i; 3040 caminf->InitiatorBusId = businfo.InitiatorBusId[i]; 3041 caminf->aac_sc = sc; 3042 caminf->sim_dev = child; 3043 3044 device_set_ivars(child, caminf); 3045 device_set_desc(child, "SCSI Passthrough Bus"); 3046 TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link); 3047 3048 found = 1; 3049 } 3050 3051 if (found) 3052 bus_generic_attach(sc->aac_dev); 3053 3054 return; 3055 } 3056