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