1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2003 Paul Saab 4 * Copyright (c) 2003 Vinod Kashyap 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/twe/twe_freebsd.c,v 1.2.2.9 2004/06/11 18:57:31 vkashyap Exp $ 30 * $DragonFly: src/sys/dev/raid/twe/twe_freebsd.c,v 1.26 2007/05/15 00:01:04 dillon Exp $ 31 */ 32 33 /* 34 * FreeBSD-specific code. 35 */ 36 37 #include <dev/raid/twe/twe_compat.h> 38 #include <dev/raid/twe/twereg.h> 39 #include <dev/raid/twe/twe_tables.h> 40 #include <dev/raid/twe/tweio.h> 41 #include <dev/raid/twe/twevar.h> 42 43 static devclass_t twe_devclass; 44 45 #ifdef TWE_DEBUG 46 static u_int32_t twed_bio_in; 47 #define TWED_BIO_IN twed_bio_in++ 48 static u_int32_t twed_bio_out; 49 #define TWED_BIO_OUT twed_bio_out++ 50 #else 51 #define TWED_BIO_IN 52 #define TWED_BIO_OUT 53 #endif 54 55 /******************************************************************************** 56 ******************************************************************************** 57 Control device interface 58 ******************************************************************************** 59 ********************************************************************************/ 60 61 static d_open_t twe_open; 62 static d_close_t twe_close; 63 static d_ioctl_t twe_ioctl_wrapper; 64 65 static struct dev_ops twe_ops = { 66 { "twe", TWE_CDEV_MAJOR, 0 }, 67 .d_open = twe_open, 68 .d_close = twe_close, 69 .d_ioctl = twe_ioctl_wrapper, 70 }; 71 72 /******************************************************************************** 73 * Accept an open operation on the control device. 74 */ 75 static int 76 twe_open(struct dev_open_args *ap) 77 { 78 cdev_t dev = ap->a_head.a_dev; 79 int unit = minor(dev); 80 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit); 81 82 sc->twe_state |= TWE_STATE_OPEN; 83 return(0); 84 } 85 86 /******************************************************************************** 87 * Accept the last close on the control device. 88 */ 89 static int 90 twe_close(struct dev_close_args *ap) 91 { 92 cdev_t dev = ap->a_head.a_dev; 93 int unit = minor(dev); 94 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit); 95 96 sc->twe_state &= ~TWE_STATE_OPEN; 97 return (0); 98 } 99 100 /******************************************************************************** 101 * Handle controller-specific control operations. 102 */ 103 static int 104 twe_ioctl_wrapper(struct dev_ioctl_args *ap) 105 { 106 cdev_t dev = ap->a_head.a_dev; 107 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1; 108 109 return(twe_ioctl(sc, ap->a_cmd, ap->a_data)); 110 } 111 112 /******************************************************************************** 113 ******************************************************************************** 114 PCI device interface 115 ******************************************************************************** 116 ********************************************************************************/ 117 118 static int twe_probe(device_t dev); 119 static int twe_attach(device_t dev); 120 static void twe_free(struct twe_softc *sc); 121 static int twe_detach(device_t dev); 122 static int twe_shutdown(device_t dev); 123 static int twe_suspend(device_t dev); 124 static int twe_resume(device_t dev); 125 static void twe_pci_intr(void *arg); 126 static void twe_intrhook(void *arg); 127 static void twe_free_request(struct twe_request *tr); 128 static void twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, 129 int nsegments, int error); 130 static void twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, 131 int nsegments, int error); 132 133 static device_method_t twe_methods[] = { 134 /* Device interface */ 135 DEVMETHOD(device_probe, twe_probe), 136 DEVMETHOD(device_attach, twe_attach), 137 DEVMETHOD(device_detach, twe_detach), 138 DEVMETHOD(device_shutdown, twe_shutdown), 139 DEVMETHOD(device_suspend, twe_suspend), 140 DEVMETHOD(device_resume, twe_resume), 141 142 DEVMETHOD(bus_print_child, bus_generic_print_child), 143 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 144 { 0, 0 } 145 }; 146 147 static driver_t twe_pci_driver = { 148 "twe", 149 twe_methods, 150 sizeof(struct twe_softc) 151 }; 152 153 #ifdef TWE_OVERRIDE 154 DRIVER_MODULE(Xtwe, pci, twe_pci_driver, twe_devclass, 0, 0); 155 #else 156 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0); 157 #endif 158 159 /******************************************************************************** 160 * Match a 3ware Escalade ATA RAID controller. 161 */ 162 static int 163 twe_probe(device_t dev) 164 { 165 166 debug_called(4); 167 168 if ((pci_get_vendor(dev) == TWE_VENDOR_ID) && 169 ((pci_get_device(dev) == TWE_DEVICE_ID) || 170 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) { 171 device_set_desc(dev, TWE_DEVICE_NAME " driver ver. " TWE_DRIVER_VERSION_STRING); 172 #ifdef TWE_OVERRIDE 173 return(0); 174 #else 175 return(-10); 176 #endif 177 } 178 return(ENXIO); 179 } 180 181 /******************************************************************************** 182 * Allocate resources, initialise the controller. 183 */ 184 static int 185 twe_attach(device_t dev) 186 { 187 struct twe_softc *sc; 188 int rid, error; 189 u_int32_t command; 190 191 debug_called(4); 192 193 /* 194 * Initialise the softc structure. 195 */ 196 sc = device_get_softc(dev); 197 sc->twe_dev = dev; 198 199 sysctl_ctx_init(&sc->sysctl_ctx); 200 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 201 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 202 device_get_nameunit(dev), CTLFLAG_RD, 0, ""); 203 if (sc->sysctl_tree == NULL) { 204 twe_printf(sc, "cannot add sysctl tree node\n"); 205 return (ENXIO); 206 } 207 SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree), 208 OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0, 209 "TWE driver version"); 210 211 /* 212 * Make sure we are going to be able to talk to this board. 213 */ 214 command = pci_read_config(dev, PCIR_COMMAND, 2); 215 if ((command & PCIM_CMD_PORTEN) == 0) { 216 twe_printf(sc, "register window not available\n"); 217 return(ENXIO); 218 } 219 /* 220 * Force the busmaster enable bit on, in case the BIOS forgot. 221 */ 222 command |= PCIM_CMD_BUSMASTEREN; 223 pci_write_config(dev, PCIR_COMMAND, command, 2); 224 225 /* 226 * Allocate the PCI register window. 227 */ 228 rid = TWE_IO_CONFIG_REG; 229 if ((sc->twe_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) { 230 twe_printf(sc, "can't allocate register window\n"); 231 twe_free(sc); 232 return(ENXIO); 233 } 234 sc->twe_btag = rman_get_bustag(sc->twe_io); 235 sc->twe_bhandle = rman_get_bushandle(sc->twe_io); 236 237 /* 238 * Allocate the parent bus DMA tag appropriate for PCI. 239 */ 240 if (bus_dma_tag_create(NULL, /* parent */ 241 1, 0, /* alignment, boundary */ 242 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 243 BUS_SPACE_MAXADDR, /* highaddr */ 244 NULL, NULL, /* filter, filterarg */ 245 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */ 246 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 247 BUS_DMA_ALLOCNOW, /* flags */ 248 &sc->twe_parent_dmat)) { 249 twe_printf(sc, "can't allocate parent DMA tag\n"); 250 twe_free(sc); 251 return(ENOMEM); 252 } 253 254 /* 255 * Allocate and connect our interrupt. 256 */ 257 rid = 0; 258 if ((sc->twe_irq = bus_alloc_resource(sc->twe_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 259 twe_printf(sc, "can't allocate interrupt\n"); 260 twe_free(sc); 261 return(ENXIO); 262 } 263 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, 0, 264 twe_pci_intr, sc, &sc->twe_intr, NULL)) { 265 twe_printf(sc, "can't set up interrupt\n"); 266 twe_free(sc); 267 return(ENXIO); 268 } 269 270 /* 271 * Create DMA tag for mapping objects into controller-addressable space. 272 */ 273 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */ 274 1, 0, /* alignment, boundary */ 275 BUS_SPACE_MAXADDR, /* lowaddr */ 276 BUS_SPACE_MAXADDR, /* highaddr */ 277 NULL, NULL, /* filter, filterarg */ 278 MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */ 279 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 280 0, /* flags */ 281 &sc->twe_buffer_dmat)) { 282 twe_printf(sc, "can't allocate data buffer DMA tag\n"); 283 twe_free(sc); 284 return(ENOMEM); 285 } 286 287 /* 288 * Initialise the controller and driver core. 289 */ 290 if ((error = twe_setup(sc))) { 291 twe_free(sc); 292 return(error); 293 } 294 295 /* 296 * Print some information about the controller and configuration. 297 */ 298 twe_describe_controller(sc); 299 300 /* 301 * Create the control device. 302 */ 303 dev_ops_add(&twe_ops, -1, device_get_unit(sc->twe_dev)); 304 sc->twe_dev_t = make_dev(&twe_ops, device_get_unit(sc->twe_dev), 305 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, "twe%d", 306 device_get_unit(sc->twe_dev)); 307 sc->twe_dev_t->si_drv1 = sc; 308 /* 309 * Schedule ourselves to bring the controller up once interrupts are available. 310 * This isn't strictly necessary, since we disable interrupts while probing the 311 * controller, but it is more in keeping with common practice for other disk 312 * devices. 313 */ 314 sc->twe_ich.ich_func = twe_intrhook; 315 sc->twe_ich.ich_arg = sc; 316 if (config_intrhook_establish(&sc->twe_ich) != 0) { 317 twe_printf(sc, "can't establish configuration hook\n"); 318 twe_free(sc); 319 return(ENXIO); 320 } 321 322 return(0); 323 } 324 325 /******************************************************************************** 326 * Free all of the resources associated with (sc). 327 * 328 * Should not be called if the controller is active. 329 */ 330 static void 331 twe_free(struct twe_softc *sc) 332 { 333 struct twe_request *tr; 334 335 debug_called(4); 336 337 /* throw away any command buffers */ 338 while ((tr = twe_dequeue_free(sc)) != NULL) 339 twe_free_request(tr); 340 341 /* destroy the data-transfer DMA tag */ 342 if (sc->twe_buffer_dmat) 343 bus_dma_tag_destroy(sc->twe_buffer_dmat); 344 345 /* disconnect the interrupt handler */ 346 if (sc->twe_intr) 347 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr); 348 if (sc->twe_irq != NULL) 349 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq); 350 351 /* destroy the parent DMA tag */ 352 if (sc->twe_parent_dmat) 353 bus_dma_tag_destroy(sc->twe_parent_dmat); 354 355 /* release the register window mapping */ 356 if (sc->twe_io != NULL) 357 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io); 358 359 dev_ops_remove(&twe_ops, -1, device_get_unit(sc->twe_dev)); 360 /* destroy control device */ 361 if (sc->twe_dev_t != (cdev_t)NULL) 362 destroy_dev(sc->twe_dev_t); 363 364 sysctl_ctx_free(&sc->sysctl_ctx); 365 } 366 367 /******************************************************************************** 368 * Disconnect from the controller completely, in preparation for unload. 369 */ 370 static int 371 twe_detach(device_t dev) 372 { 373 struct twe_softc *sc = device_get_softc(dev); 374 int error; 375 376 debug_called(4); 377 378 error = EBUSY; 379 crit_enter(); 380 if (sc->twe_state & TWE_STATE_OPEN) 381 goto out; 382 383 /* 384 * Shut the controller down. 385 */ 386 if ((error = twe_shutdown(dev))) 387 goto out; 388 389 twe_free(sc); 390 391 error = 0; 392 out: 393 crit_exit(); 394 return(error); 395 } 396 397 /******************************************************************************** 398 * Bring the controller down to a dormant state and detach all child devices. 399 * 400 * Note that we can assume that the bioq on the controller is empty, as we won't 401 * allow shutdown if any device is open. 402 */ 403 static int 404 twe_shutdown(device_t dev) 405 { 406 struct twe_softc *sc = device_get_softc(dev); 407 int i, error = 0; 408 409 debug_called(4); 410 411 crit_enter(); 412 413 /* 414 * Delete all our child devices. 415 */ 416 for (i = 0; i < TWE_MAX_UNITS; i++) { 417 if (sc->twe_drive[i].td_disk != 0) 418 if ((error = twe_detach_drive(sc, i)) != 0) 419 goto out; 420 } 421 422 /* 423 * Bring the controller down. 424 */ 425 twe_deinit(sc); 426 427 out: 428 crit_exit(); 429 return(error); 430 } 431 432 /******************************************************************************** 433 * Bring the controller to a quiescent state, ready for system suspend. 434 */ 435 static int 436 twe_suspend(device_t dev) 437 { 438 struct twe_softc *sc = device_get_softc(dev); 439 440 debug_called(4); 441 442 crit_enter(); 443 sc->twe_state |= TWE_STATE_SUSPEND; 444 445 twe_disable_interrupts(sc); 446 crit_exit(); 447 448 return(0); 449 } 450 451 /******************************************************************************** 452 * Bring the controller back to a state ready for operation. 453 */ 454 static int 455 twe_resume(device_t dev) 456 { 457 struct twe_softc *sc = device_get_softc(dev); 458 459 debug_called(4); 460 461 sc->twe_state &= ~TWE_STATE_SUSPEND; 462 twe_enable_interrupts(sc); 463 464 return(0); 465 } 466 467 /******************************************************************************* 468 * Take an interrupt, or be poked by other code to look for interrupt-worthy 469 * status. 470 */ 471 static void 472 twe_pci_intr(void *arg) 473 { 474 twe_intr((struct twe_softc *)arg); 475 } 476 477 /******************************************************************************** 478 * Delayed-startup hook 479 */ 480 static void 481 twe_intrhook(void *arg) 482 { 483 struct twe_softc *sc = (struct twe_softc *)arg; 484 485 /* pull ourselves off the intrhook chain */ 486 config_intrhook_disestablish(&sc->twe_ich); 487 488 /* call core startup routine */ 489 twe_init(sc); 490 } 491 492 /******************************************************************************** 493 * Given a detected drive, attach it to the bio interface. 494 * 495 * This is called from twe_add_unit. 496 */ 497 int 498 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr) 499 { 500 char buf[80]; 501 int error = 0; 502 503 dr->td_disk = device_add_child(sc->twe_dev, NULL, -1); 504 if (dr->td_disk == NULL) { 505 twe_printf(sc, "Cannot add unit\n"); 506 return (EIO); 507 } 508 device_set_ivars(dr->td_disk, dr); 509 510 /* 511 * XXX It would make sense to test the online/initialising bits, but they seem to be 512 * always set... 513 */ 514 ksprintf(buf, "Unit %d, %s, %s", 515 dr->td_twe_unit, 516 twe_describe_code(twe_table_unittype, dr->td_type), 517 twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK)); 518 device_set_desc_copy(dr->td_disk, buf); 519 520 if ((error = bus_generic_attach(sc->twe_dev)) != 0) { 521 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error); 522 error = EIO; 523 } 524 return (error); 525 } 526 527 /******************************************************************************** 528 * Detach the specified unit if it exsists 529 * 530 * This is called from twe_del_unit. 531 */ 532 int 533 twe_detach_drive(struct twe_softc *sc, int unit) 534 { 535 int error = 0; 536 537 if ((error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk))) { 538 twe_printf(sc, "Cannot delete unit. error = %d\n", error); 539 return (error); 540 } 541 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit])); 542 return (error); 543 } 544 545 /******************************************************************************** 546 * Clear a PCI parity error. 547 */ 548 void 549 twe_clear_pci_parity_error(struct twe_softc *sc) 550 { 551 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR); 552 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2); 553 } 554 555 /******************************************************************************** 556 * Clear a PCI abort. 557 */ 558 void 559 twe_clear_pci_abort(struct twe_softc *sc) 560 { 561 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT); 562 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2); 563 } 564 565 /******************************************************************************** 566 ******************************************************************************** 567 Disk device 568 ******************************************************************************** 569 ********************************************************************************/ 570 571 /* 572 * Disk device bus interface 573 */ 574 static int twed_probe(device_t dev); 575 static int twed_attach(device_t dev); 576 static int twed_detach(device_t dev); 577 578 static device_method_t twed_methods[] = { 579 DEVMETHOD(device_probe, twed_probe), 580 DEVMETHOD(device_attach, twed_attach), 581 DEVMETHOD(device_detach, twed_detach), 582 { 0, 0 } 583 }; 584 585 static driver_t twed_driver = { 586 "twed", 587 twed_methods, 588 sizeof(struct twed_softc) 589 }; 590 591 static devclass_t twed_devclass; 592 #ifdef TWE_OVERRIDE 593 DRIVER_MODULE(Xtwed, Xtwe, twed_driver, twed_devclass, 0, 0); 594 #else 595 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0); 596 #endif 597 598 /* 599 * Disk device control interface. 600 */ 601 static d_open_t twed_open; 602 static d_close_t twed_close; 603 static d_strategy_t twed_strategy; 604 static d_dump_t twed_dump; 605 606 static struct dev_ops twed_ops = { 607 { "twed", TWED_CDEV_MAJOR, D_DISK }, 608 .d_open = twed_open, 609 .d_close = twed_close, 610 .d_read = physread, 611 .d_write = physwrite, 612 .d_strategy = twed_strategy, 613 .d_dump = twed_dump, 614 }; 615 616 #ifdef FREEBSD_4 617 static int disks_registered = 0; 618 #endif 619 620 /******************************************************************************** 621 * Handle open from generic layer. 622 * 623 * Note that this is typically only called by the diskslice code, and not 624 * for opens on subdevices (eg. slices, partitions). 625 */ 626 static int 627 twed_open(struct dev_open_args *ap) 628 { 629 cdev_t dev = ap->a_head.a_dev; 630 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1; 631 struct disk_info info; 632 633 debug_called(4); 634 635 if (sc == NULL) 636 return (ENXIO); 637 638 /* check that the controller is up and running */ 639 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN) 640 return(ENXIO); 641 642 /* build disk info */ 643 bzero(&info, sizeof(info)); 644 info.d_media_blksize = TWE_BLOCK_SIZE; /* mandatory */ 645 info.d_media_blocks = sc->twed_drive->td_size; 646 647 info.d_type = DTYPE_ESDI; /* optional */ 648 info.d_secpertrack = sc->twed_drive->td_sectors; 649 info.d_nheads = sc->twed_drive->td_heads; 650 info.d_ncylinders = sc->twed_drive->td_cylinders; 651 info.d_secpercyl = sc->twed_drive->td_sectors * sc->twed_drive->td_heads; 652 653 disk_setdiskinfo(&sc->twed_disk, &info); 654 655 sc->twed_flags |= TWED_OPEN; 656 return (0); 657 } 658 659 /******************************************************************************** 660 * Handle last close of the disk device. 661 */ 662 static int 663 twed_close(struct dev_close_args *ap) 664 { 665 cdev_t dev = ap->a_head.a_dev; 666 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1; 667 668 debug_called(4); 669 670 if (sc == NULL) 671 return (ENXIO); 672 673 sc->twed_flags &= ~TWED_OPEN; 674 return (0); 675 } 676 677 /******************************************************************************** 678 * Handle an I/O request. 679 */ 680 static int 681 twed_strategy(struct dev_strategy_args *ap) 682 { 683 cdev_t dev = ap->a_head.a_dev; 684 struct bio *bio = ap->a_bio; 685 struct twed_softc *sc = dev->si_drv1; 686 struct buf *bp = bio->bio_buf; 687 688 bio->bio_driver_info = sc; 689 690 debug_called(4); 691 692 TWED_BIO_IN; 693 694 /* bogus disk? */ 695 if ((sc == NULL) || (!sc->twed_drive->td_disk)) { 696 bp->b_error = EINVAL; 697 bp->b_flags |= B_ERROR; 698 kprintf("twe: bio for invalid disk!\n"); 699 biodone(bio); 700 TWED_BIO_OUT; 701 return(0); 702 } 703 704 /* perform accounting */ 705 devstat_start_transaction(&sc->twed_stats); 706 707 /* queue the bio on the controller */ 708 twe_enqueue_bio(sc->twed_controller, bio); 709 710 /* poke the controller to start I/O */ 711 twe_startio(sc->twed_controller); 712 return(0); 713 } 714 715 /******************************************************************************** 716 * System crashdump support 717 */ 718 static int 719 twed_dump(struct dev_dump_args *ap) 720 { 721 cdev_t dev = ap->a_head.a_dev; 722 struct twed_softc *twed_sc = (struct twed_softc *)dev->si_drv1; 723 struct twe_softc *twe_sc = (struct twe_softc *)twed_sc->twed_controller; 724 vm_paddr_t addr = 0; 725 long blkcnt; 726 int dumppages = MAXDUMPPGS; 727 int error; 728 int i; 729 730 if (!twed_sc || !twe_sc) 731 return(ENXIO); 732 733 blkcnt = howmany(PAGE_SIZE, ap->a_secsize); 734 735 while (ap->a_count > 0) { 736 caddr_t va = NULL; 737 738 if ((ap->a_count / blkcnt) < dumppages) 739 dumppages = ap->a_count / blkcnt; 740 741 for (i = 0; i < dumppages; ++i) { 742 vm_paddr_t a = addr + (i * PAGE_SIZE); 743 if (is_physical_memory(a)) 744 va = pmap_kenter_temporary(trunc_page(a), i); 745 else 746 va = pmap_kenter_temporary(trunc_page(0), i); 747 } 748 749 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, ap->a_blkno, va, 750 (PAGE_SIZE * dumppages) / TWE_BLOCK_SIZE)) != 0) 751 return(error); 752 753 754 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0) 755 return(EINTR); 756 757 ap->a_blkno += blkcnt * dumppages; 758 ap->a_count -= blkcnt * dumppages; 759 addr += PAGE_SIZE * dumppages; 760 } 761 return(0); 762 } 763 764 /******************************************************************************** 765 * Handle completion of an I/O request. 766 */ 767 void 768 twed_intr(struct bio *bio) 769 { 770 struct buf *bp = bio->bio_buf; 771 struct twed_softc *sc = bio->bio_driver_info; 772 debug_called(4); 773 774 /* if no error, transfer completed */ 775 if ((bp->b_flags & B_ERROR) == 0) 776 bp->b_resid = 0; 777 devstat_end_transaction_buf(&sc->twed_stats, bp); 778 biodone(bio); 779 TWED_BIO_OUT; 780 } 781 782 /******************************************************************************** 783 * Default probe stub. 784 */ 785 static int 786 twed_probe(device_t dev) 787 { 788 return (0); 789 } 790 791 /******************************************************************************** 792 * Attach a unit to the controller. 793 */ 794 static int 795 twed_attach(device_t dev) 796 { 797 struct twed_softc *sc; 798 device_t parent; 799 cdev_t dsk; 800 801 debug_called(4); 802 803 /* initialise our softc */ 804 sc = device_get_softc(dev); 805 parent = device_get_parent(dev); 806 sc->twed_controller = (struct twe_softc *)device_get_softc(parent); 807 sc->twed_drive = device_get_ivars(dev); 808 sc->twed_drive->td_sys_unit = device_get_unit(dev); 809 sc->twed_dev = dev; 810 811 /* report the drive */ 812 twed_printf(sc, "%uMB (%u sectors)\n", 813 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE), 814 sc->twed_drive->td_size); 815 816 devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit, 817 TWE_BLOCK_SIZE, 818 DEVSTAT_NO_ORDERED_TAGS, 819 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 820 DEVSTAT_PRIORITY_ARRAY); 821 822 /* attach a generic disk device to ourselves */ 823 dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk, &twed_ops); 824 dsk->si_drv1 = sc; 825 /* dsk->si_drv2 = sc->twed_drive;*/ 826 sc->twed_dev_t = dsk; 827 #ifdef FREEBSD_4 828 disks_registered++; 829 #endif 830 831 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */ 832 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE; 833 834 return (0); 835 } 836 837 /******************************************************************************** 838 * Disconnect ourselves from the system. 839 */ 840 static int 841 twed_detach(device_t dev) 842 { 843 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev); 844 845 debug_called(4); 846 847 if (sc->twed_flags & TWED_OPEN) 848 return(EBUSY); 849 850 devstat_remove_entry(&sc->twed_stats); 851 disk_destroy(&sc->twed_disk); 852 #ifdef FREEBSD_4 853 kprintf("Disks registered: %d\n", disks_registered); 854 #if 0 855 if (--disks_registered == 0) 856 dev_ops_remove(&tweddisk_ops); 857 #endif 858 #endif 859 860 return(0); 861 } 862 863 /******************************************************************************** 864 ******************************************************************************** 865 Misc 866 ******************************************************************************** 867 ********************************************************************************/ 868 869 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe commands", "twe commands"); 870 /******************************************************************************** 871 * Allocate a command buffer 872 */ 873 struct twe_request * 874 twe_allocate_request(struct twe_softc *sc) 875 { 876 struct twe_request *tr; 877 int aligned_size; 878 879 /* 880 * TWE requires requests to be 512-byte aligned. Depend on malloc() 881 * guarenteeing alignment for power-of-2 requests. Note that the old 882 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab 883 * allocator only guarentees same-size alignment for power-of-2 requests. 884 */ 885 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) & 886 ~TWE_ALIGNMASK; 887 tr = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO); 888 tr->tr_sc = sc; 889 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_cmdmap)) { 890 twe_free_request(tr); 891 return(NULL); 892 } 893 bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_cmdmap, &tr->tr_command, 894 sizeof(tr->tr_command), twe_setup_request_dmamap, tr, 0); 895 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) { 896 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap); 897 twe_free_request(tr); 898 return(NULL); 899 } 900 return(tr); 901 } 902 903 /******************************************************************************** 904 * Permanently discard a command buffer. 905 */ 906 static void 907 twe_free_request(struct twe_request *tr) 908 { 909 struct twe_softc *sc = tr->tr_sc; 910 911 debug_called(4); 912 913 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_cmdmap); 914 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap); 915 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap); 916 kfree(tr, TWE_MALLOC_CLASS); 917 } 918 919 /******************************************************************************** 920 * Map/unmap (tr)'s command and data in the controller's addressable space. 921 * 922 * These routines ensure that the data which the controller is going to try to 923 * access is actually visible to the controller, in a machine-independant 924 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned 925 * and we take care of that here as well. 926 */ 927 static void 928 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl) 929 { 930 int i; 931 932 for (i = 0; i < nsegments; i++) { 933 sgl[i].address = segs[i].ds_addr; 934 sgl[i].length = segs[i].ds_len; 935 } 936 for (; i < max_sgl; i++) { /* XXX necessary? */ 937 sgl[i].address = 0; 938 sgl[i].length = 0; 939 } 940 } 941 942 static void 943 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 944 { 945 struct twe_request *tr = (struct twe_request *)arg; 946 TWE_Command *cmd = &tr->tr_command; 947 948 debug_called(4); 949 950 if (tr->tr_flags & TWE_CMD_MAPPED) 951 panic("already mapped command"); 952 953 tr->tr_flags |= TWE_CMD_MAPPED; 954 955 if (tr->tr_flags & TWE_CMD_IN_PROGRESS) 956 tr->tr_sc->twe_state &= ~TWE_STATE_FRZN; 957 /* save base of first segment in command (applicable if there only one segment) */ 958 tr->tr_dataphys = segs[0].ds_addr; 959 960 /* correct command size for s/g list size */ 961 tr->tr_command.generic.size += 2 * nsegments; 962 963 /* 964 * Due to the fact that parameter and I/O commands have the scatter/gather list in 965 * different places, we need to determine which sort of command this actually is 966 * before we can populate it correctly. 967 */ 968 switch(cmd->generic.opcode) { 969 case TWE_OP_GET_PARAM: 970 case TWE_OP_SET_PARAM: 971 cmd->generic.sgl_offset = 2; 972 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH); 973 break; 974 case TWE_OP_READ: 975 case TWE_OP_WRITE: 976 cmd->generic.sgl_offset = 3; 977 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH); 978 break; 979 case TWE_OP_ATA_PASSTHROUGH: 980 cmd->generic.sgl_offset = 5; 981 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH); 982 break; 983 default: 984 /* 985 * Fall back to what the linux driver does. 986 * Do this because the API may send an opcode 987 * the driver knows nothing about and this will 988 * at least stop PCIABRT's from hosing us. 989 */ 990 switch (cmd->generic.sgl_offset) { 991 case 2: 992 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH); 993 break; 994 case 3: 995 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH); 996 break; 997 case 5: 998 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH); 999 break; 1000 } 1001 } 1002 if (tr->tr_flags & TWE_CMD_DATAIN) 1003 bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREREAD); 1004 if (tr->tr_flags & TWE_CMD_DATAOUT) { 1005 /* if we're using an alignment buffer, and we're writing data, copy the real data out */ 1006 if (tr->tr_flags & TWE_CMD_ALIGNBUF) 1007 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length); 1008 bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREWRITE); 1009 } 1010 if (twe_start(tr) == EBUSY) { 1011 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY; 1012 twe_requeue_ready(tr); 1013 } 1014 } 1015 1016 static void 1017 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 1018 { 1019 struct twe_request *tr = (struct twe_request *)arg; 1020 1021 debug_called(4); 1022 1023 /* command can't cross a page boundary */ 1024 tr->tr_cmdphys = segs[0].ds_addr; 1025 } 1026 1027 int 1028 twe_map_request(struct twe_request *tr) 1029 { 1030 struct twe_softc *sc = tr->tr_sc; 1031 int error = 0; 1032 1033 debug_called(4); 1034 1035 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) { 1036 twe_requeue_ready(tr); 1037 return (EBUSY); 1038 } 1039 1040 /* 1041 * Map the command into bus space. 1042 */ 1043 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_PREWRITE); 1044 1045 /* 1046 * If the command involves data, map that too. 1047 */ 1048 if ((tr->tr_data != NULL) && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) { 1049 1050 /* 1051 * Data must be 512-byte aligned; allocate a fixup buffer if it's not. 1052 * 1053 * DragonFly's malloc only guarentees alignment for requests which 1054 * are power-of-2 sized. 1055 */ 1056 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) { 1057 int aligned_size; 1058 1059 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */ 1060 aligned_size = TWE_ALIGNMENT; 1061 while (aligned_size < tr->tr_length) 1062 aligned_size <<= 1; 1063 tr->tr_flags |= TWE_CMD_ALIGNBUF; 1064 tr->tr_data = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT); 1065 if (tr->tr_data == NULL) { 1066 twe_printf(sc, "%s: malloc failed\n", __func__); 1067 tr->tr_data = tr->tr_realdata; /* restore original data pointer */ 1068 return(ENOMEM); 1069 } 1070 } 1071 1072 /* 1073 * Map the data buffer into bus space and build the s/g list. 1074 */ 1075 if ((error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, 1076 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT) 1077 == EINPROGRESS)) { 1078 tr->tr_flags |= TWE_CMD_IN_PROGRESS; 1079 sc->twe_state |= TWE_STATE_FRZN; 1080 error = 0; 1081 } 1082 } else { 1083 if ((error = twe_start(tr)) == EBUSY) { 1084 sc->twe_state |= TWE_STATE_CTLR_BUSY; 1085 twe_requeue_ready(tr); 1086 } 1087 } 1088 1089 return(error); 1090 } 1091 1092 void 1093 twe_unmap_request(struct twe_request *tr) 1094 { 1095 struct twe_softc *sc = tr->tr_sc; 1096 debug_called(4); 1097 1098 /* 1099 * Unmap the command from bus space. 1100 */ 1101 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_POSTWRITE); 1102 1103 /* 1104 * If the command involved data, unmap that too. 1105 */ 1106 if (tr->tr_data != NULL) { 1107 1108 if (tr->tr_flags & TWE_CMD_DATAIN) { 1109 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTREAD); 1110 /* if we're using an alignment buffer, and we're reading data, copy the real data in */ 1111 if (tr->tr_flags & TWE_CMD_ALIGNBUF) 1112 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length); 1113 } 1114 if (tr->tr_flags & TWE_CMD_DATAOUT) 1115 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTWRITE); 1116 1117 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap); 1118 } 1119 1120 /* free alignment buffer if it was used */ 1121 if (tr->tr_flags & TWE_CMD_ALIGNBUF) { 1122 kfree(tr->tr_data, TWE_MALLOC_CLASS); 1123 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */ 1124 } 1125 } 1126 1127 #ifdef TWE_DEBUG 1128 void twe_report(void); 1129 /******************************************************************************** 1130 * Print current controller status, call from DDB. 1131 */ 1132 void 1133 twe_report(void) 1134 { 1135 struct twe_softc *sc; 1136 int i; 1137 1138 crit_enter(); 1139 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++) 1140 twe_print_controller(sc); 1141 kprintf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out); 1142 crit_exit(); 1143 } 1144 #endif 1145