1 /*- 2 * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ata.h" 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/ata.h> 36 #include <sys/bus.h> 37 #include <sys/conf.h> 38 #include <sys/malloc.h> 39 #include <sys/sema.h> 40 #include <sys/taskqueue.h> 41 #include <vm/uma.h> 42 #include <machine/stdarg.h> 43 #include <machine/resource.h> 44 #include <machine/bus.h> 45 #include <sys/rman.h> 46 #include <dev/pci/pcivar.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/ata/ata-all.h> 49 #include <dev/ata/ata-pci.h> 50 #include <ata_if.h> 51 52 /* local vars */ 53 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI"); 54 55 /* misc defines */ 56 #define IOMASK 0xfffffffc 57 58 /* local prototypes */ 59 static int ata_generic_chipinit(device_t dev); 60 static void ata_generic_setmode(device_t dev, int mode); 61 62 /* 63 * generic PCI ATA device probe 64 */ 65 int 66 ata_pci_probe(device_t dev) 67 { 68 struct ata_pci_controller *ctlr = device_get_softc(dev); 69 char buffer[64]; 70 71 /* is this a storage class device ? */ 72 if (pci_get_class(dev) != PCIC_STORAGE) 73 return ENXIO; 74 75 /* is this an IDE/ATA type device ? */ 76 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) 77 return ENXIO; 78 79 sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev)); 80 device_set_desc_copy(dev, buffer); 81 ctlr->chipinit = ata_generic_chipinit; 82 83 /* we are a low priority handler */ 84 return -100; 85 } 86 87 int 88 ata_pci_attach(device_t dev) 89 { 90 struct ata_pci_controller *ctlr = device_get_softc(dev); 91 device_t child; 92 u_int32_t cmd; 93 int unit; 94 95 /* do chipset specific setups only needed once */ 96 ctlr->legacy = ata_legacy(dev); 97 if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK) 98 ctlr->channels = 2; 99 else 100 ctlr->channels = 1; 101 ctlr->ichannels = -1; 102 ctlr->ch_attach = ata_pci_ch_attach; 103 ctlr->ch_detach = ata_pci_ch_detach; 104 ctlr->dev = dev; 105 106 /* if needed try to enable busmastering */ 107 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 108 if (!(cmd & PCIM_CMD_BUSMASTEREN)) { 109 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2); 110 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 111 } 112 113 /* if busmastering mode "stuck" use it */ 114 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 115 ctlr->r_type1 = SYS_RES_IOPORT; 116 ctlr->r_rid1 = ATA_BMADDR_RID; 117 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1, 118 RF_ACTIVE); 119 } 120 121 if (ctlr->chipinit(dev)) 122 return ENXIO; 123 124 /* attach all channels on this controller */ 125 for (unit = 0; unit < ctlr->channels; unit++) { 126 if ((ctlr->ichannels & (1 << unit)) == 0) 127 continue; 128 child = device_add_child(dev, "ata", 129 ((unit == 0 || unit == 1) && ctlr->legacy) ? 130 unit : devclass_find_free_unit(ata_devclass, 2)); 131 if (child == NULL) 132 device_printf(dev, "failed to add ata child device\n"); 133 else 134 device_set_ivars(child, (void *)(intptr_t)unit); 135 } 136 bus_generic_attach(dev); 137 return 0; 138 } 139 140 int 141 ata_pci_detach(device_t dev) 142 { 143 struct ata_pci_controller *ctlr = device_get_softc(dev); 144 device_t *children; 145 int nchildren, i; 146 147 /* detach & delete all children */ 148 if (!device_get_children(dev, &children, &nchildren)) { 149 for (i = 0; i < nchildren; i++) 150 device_delete_child(dev, children[i]); 151 free(children, M_TEMP); 152 } 153 154 if (ctlr->r_irq) { 155 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle); 156 bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq); 157 if (ctlr->r_irq_rid != ATA_IRQ_RID) 158 pci_release_msi(dev); 159 } 160 if (ctlr->r_res2) 161 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2); 162 if (ctlr->r_res1) 163 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1); 164 165 return 0; 166 } 167 168 int 169 ata_pci_suspend(device_t dev) 170 { 171 struct ata_pci_controller *ctlr = device_get_softc(dev); 172 int error = 0; 173 174 bus_generic_suspend(dev); 175 if (ctlr->suspend) 176 error = ctlr->suspend(dev); 177 return error; 178 } 179 180 int 181 ata_pci_resume(device_t dev) 182 { 183 struct ata_pci_controller *ctlr = device_get_softc(dev); 184 int error = 0; 185 186 if (ctlr->resume) 187 error = ctlr->resume(dev); 188 bus_generic_resume(dev); 189 return error; 190 } 191 192 struct resource * 193 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 194 u_long start, u_long end, u_long count, u_int flags) 195 { 196 struct ata_pci_controller *controller = device_get_softc(dev); 197 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 198 struct resource *res = NULL; 199 int myrid; 200 201 if (type == SYS_RES_IOPORT) { 202 switch (*rid) { 203 case ATA_IOADDR_RID: 204 if (controller->legacy) { 205 start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 206 count = ATA_IOSIZE; 207 end = start + count - 1; 208 } 209 myrid = PCIR_BAR(0) + (unit << 3); 210 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 211 SYS_RES_IOPORT, &myrid, 212 start, end, count, flags); 213 break; 214 215 case ATA_CTLADDR_RID: 216 if (controller->legacy) { 217 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET; 218 count = ATA_CTLIOSIZE; 219 end = start + count - 1; 220 } 221 myrid = PCIR_BAR(1) + (unit << 3); 222 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 223 SYS_RES_IOPORT, &myrid, 224 start, end, count, flags); 225 break; 226 } 227 } 228 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 229 if (controller->legacy) { 230 int irq = (unit == 0 ? 14 : 15); 231 232 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 233 SYS_RES_IRQ, rid, irq, irq, 1, flags); 234 } 235 else 236 res = controller->r_irq; 237 } 238 return res; 239 } 240 241 int 242 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 243 struct resource *r) 244 { 245 struct ata_pci_controller *controller = device_get_softc(dev); 246 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 247 248 if (type == SYS_RES_IOPORT) { 249 switch (rid) { 250 case ATA_IOADDR_RID: 251 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 252 SYS_RES_IOPORT, 253 PCIR_BAR(0) + (unit << 3), r); 254 break; 255 256 case ATA_CTLADDR_RID: 257 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 258 SYS_RES_IOPORT, 259 PCIR_BAR(1) + (unit << 3), r); 260 break; 261 default: 262 return ENOENT; 263 } 264 } 265 if (type == SYS_RES_IRQ) { 266 if (rid != ATA_IRQ_RID) 267 return ENOENT; 268 269 if (controller->legacy) { 270 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 271 SYS_RES_IRQ, rid, r); 272 } 273 else 274 return 0; 275 } 276 return EINVAL; 277 } 278 279 int 280 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 281 int flags, driver_filter_t *filter, driver_intr_t *function, 282 void *argument, void **cookiep) 283 { 284 struct ata_pci_controller *controller = device_get_softc(dev); 285 286 if (controller->legacy) { 287 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 288 flags, filter, function, argument, cookiep); 289 } 290 else { 291 struct ata_pci_controller *controller = device_get_softc(dev); 292 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 293 294 if (filter != NULL) { 295 printf("ata-pci.c: we cannot use a filter here\n"); 296 return (EINVAL); 297 } 298 controller->interrupt[unit].function = function; 299 controller->interrupt[unit].argument = argument; 300 *cookiep = controller; 301 return 0; 302 } 303 } 304 305 int 306 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 307 void *cookie) 308 { 309 struct ata_pci_controller *controller = device_get_softc(dev); 310 311 if (controller->legacy) { 312 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 313 } 314 else { 315 struct ata_pci_controller *controller = device_get_softc(dev); 316 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 317 318 controller->interrupt[unit].function = NULL; 319 controller->interrupt[unit].argument = NULL; 320 return 0; 321 } 322 } 323 324 static void 325 ata_generic_setmode(device_t dev, int mode) 326 { 327 struct ata_device *atadev = device_get_softc(dev); 328 329 mode = ata_limit_mode(dev, mode, ATA_UDMA2); 330 mode = ata_check_80pin(dev, mode); 331 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode)) 332 atadev->mode = mode; 333 } 334 335 static int 336 ata_generic_chipinit(device_t dev) 337 { 338 struct ata_pci_controller *ctlr = device_get_softc(dev); 339 340 if (ata_setup_interrupt(dev, ata_generic_intr)) 341 return ENXIO; 342 ctlr->setmode = ata_generic_setmode; 343 return 0; 344 } 345 346 int 347 ata_pci_ch_attach(device_t dev) 348 { 349 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 350 struct ata_channel *ch = device_get_softc(dev); 351 struct resource *io = NULL, *ctlio = NULL; 352 int i, rid; 353 354 rid = ATA_IOADDR_RID; 355 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE))) 356 return ENXIO; 357 358 rid = ATA_CTLADDR_RID; 359 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){ 360 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 361 return ENXIO; 362 } 363 364 ata_pci_dmainit(dev); 365 366 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) { 367 ch->r_io[i].res = io; 368 ch->r_io[i].offset = i; 369 } 370 ch->r_io[ATA_CONTROL].res = ctlio; 371 ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2; 372 ch->r_io[ATA_IDX_ADDR].res = io; 373 ata_default_registers(dev); 374 if (ctlr->r_res1) { 375 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 376 ch->r_io[i].res = ctlr->r_res1; 377 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE); 378 } 379 } 380 381 ata_pci_hw(dev); 382 return 0; 383 } 384 385 int 386 ata_pci_ch_detach(device_t dev) 387 { 388 struct ata_channel *ch = device_get_softc(dev); 389 390 ata_pci_dmafini(dev); 391 392 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 393 ch->r_io[ATA_CONTROL].res); 394 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, 395 ch->r_io[ATA_IDX_ADDR].res); 396 397 return (0); 398 } 399 400 int 401 ata_pci_status(device_t dev) 402 { 403 struct ata_pci_controller *controller = 404 device_get_softc(device_get_parent(dev)); 405 struct ata_channel *ch = device_get_softc(dev); 406 407 if ((dumping || !controller->legacy) && 408 ((ch->flags & ATA_ALWAYS_DMASTAT) || 409 (ch->dma.flags & ATA_DMA_ACTIVE))) { 410 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 411 412 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != 413 ATA_BMSTAT_INTERRUPT) 414 return 0; 415 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR); 416 DELAY(1); 417 } 418 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 419 DELAY(100); 420 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 421 return 0; 422 } 423 return 1; 424 } 425 426 void 427 ata_pci_hw(device_t dev) 428 { 429 struct ata_channel *ch = device_get_softc(dev); 430 431 ata_generic_hw(dev); 432 ch->hw.status = ata_pci_status; 433 } 434 435 static int 436 ata_pci_dmastart(struct ata_request *request) 437 { 438 struct ata_channel *ch = device_get_softc(request->parent); 439 440 ATA_DEBUG_RQ(request, "dmastart"); 441 442 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 443 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 444 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus); 445 ch->dma.flags |= ATA_DMA_ACTIVE; 446 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 447 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) | 448 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)| 449 ATA_BMCMD_START_STOP); 450 return 0; 451 } 452 453 static int 454 ata_pci_dmastop(struct ata_request *request) 455 { 456 struct ata_channel *ch = device_get_softc(request->parent); 457 int error; 458 459 ATA_DEBUG_RQ(request, "dmastop"); 460 461 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 462 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 463 ch->dma.flags &= ~ATA_DMA_ACTIVE; 464 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 465 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 466 return error; 467 } 468 469 static void 470 ata_pci_dmareset(device_t dev) 471 { 472 struct ata_channel *ch = device_get_softc(dev); 473 struct ata_request *request; 474 475 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 476 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 477 ch->dma.flags &= ~ATA_DMA_ACTIVE; 478 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 479 if ((request = ch->running)) { 480 device_printf(request->dev, "DMA reset calling unload\n"); 481 ch->dma.unload(request); 482 } 483 } 484 485 void 486 ata_pci_dmainit(device_t dev) 487 { 488 struct ata_channel *ch = device_get_softc(dev); 489 490 ata_dmainit(dev); 491 ch->dma.start = ata_pci_dmastart; 492 ch->dma.stop = ata_pci_dmastop; 493 ch->dma.reset = ata_pci_dmareset; 494 } 495 496 void 497 ata_pci_dmafini(device_t dev) 498 { 499 500 ata_dmafini(dev); 501 } 502 503 static device_method_t ata_pci_methods[] = { 504 /* device interface */ 505 DEVMETHOD(device_probe, ata_pci_probe), 506 DEVMETHOD(device_attach, ata_pci_attach), 507 DEVMETHOD(device_detach, ata_pci_detach), 508 DEVMETHOD(device_suspend, ata_pci_suspend), 509 DEVMETHOD(device_resume, ata_pci_resume), 510 DEVMETHOD(device_shutdown, bus_generic_shutdown), 511 512 /* bus methods */ 513 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 514 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 515 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 516 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 517 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 518 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 519 520 { 0, 0 } 521 }; 522 523 devclass_t ata_pci_devclass; 524 525 static driver_t ata_pci_driver = { 526 "atapci", 527 ata_pci_methods, 528 sizeof(struct ata_pci_controller), 529 }; 530 531 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 532 MODULE_VERSION(atapci, 1); 533 MODULE_DEPEND(atapci, ata, 1, 1, 1); 534 535 static int 536 ata_pcichannel_probe(device_t dev) 537 { 538 char buffer[32]; 539 540 sprintf(buffer, "ATA channel %d", (int)(intptr_t)device_get_ivars(dev)); 541 device_set_desc_copy(dev, buffer); 542 543 return ata_probe(dev); 544 } 545 546 static int 547 ata_pcichannel_attach(device_t dev) 548 { 549 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 550 struct ata_channel *ch = device_get_softc(dev); 551 int error; 552 553 if (ch->attached) 554 return (0); 555 ch->attached = 1; 556 557 ch->unit = (intptr_t)device_get_ivars(dev); 558 559 if ((error = ctlr->ch_attach(dev))) 560 return error; 561 562 return ata_attach(dev); 563 } 564 565 static int 566 ata_pcichannel_detach(device_t dev) 567 { 568 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 569 struct ata_channel *ch = device_get_softc(dev); 570 int error; 571 572 if (!ch->attached) 573 return (0); 574 ch->attached = 0; 575 576 if ((error = ata_detach(dev))) 577 return error; 578 579 if (ctlr->ch_detach) 580 return (ctlr->ch_detach(dev)); 581 582 return (0); 583 } 584 static int 585 ata_pcichannel_suspend(device_t dev) 586 { 587 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 588 struct ata_channel *ch = device_get_softc(dev); 589 int error; 590 591 if (!ch->attached) 592 return (0); 593 594 if ((error = ata_suspend(dev))) 595 return (error); 596 597 if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev))) 598 return (error); 599 600 return (0); 601 } 602 603 static int 604 ata_pcichannel_resume(device_t dev) 605 { 606 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 607 struct ata_channel *ch = device_get_softc(dev); 608 int error; 609 610 if (!ch->attached) 611 return (0); 612 613 if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev))) 614 return (error); 615 616 return ata_resume(dev); 617 } 618 619 620 static int 621 ata_pcichannel_locking(device_t dev, int mode) 622 { 623 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 624 struct ata_channel *ch = device_get_softc(dev); 625 626 if (ctlr->locking) 627 return ctlr->locking(dev, mode); 628 else 629 return ch->unit; 630 } 631 632 static void 633 ata_pcichannel_reset(device_t dev) 634 { 635 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 636 struct ata_channel *ch = device_get_softc(dev); 637 638 /* if DMA engine present reset it */ 639 if (ch->dma.reset) 640 ch->dma.reset(dev); 641 642 /* reset the controller HW */ 643 if (ctlr->reset) 644 ctlr->reset(dev); 645 else 646 ata_generic_reset(dev); 647 } 648 649 static void 650 ata_pcichannel_setmode(device_t parent, device_t dev) 651 { 652 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 653 struct ata_device *atadev = device_get_softc(dev); 654 int mode = atadev->mode; 655 656 ctlr->setmode(dev, ATA_PIO_MAX); 657 if (mode >= ATA_DMA) 658 ctlr->setmode(dev, mode); 659 } 660 661 static device_method_t ata_pcichannel_methods[] = { 662 /* device interface */ 663 DEVMETHOD(device_probe, ata_pcichannel_probe), 664 DEVMETHOD(device_attach, ata_pcichannel_attach), 665 DEVMETHOD(device_detach, ata_pcichannel_detach), 666 DEVMETHOD(device_shutdown, bus_generic_shutdown), 667 DEVMETHOD(device_suspend, ata_pcichannel_suspend), 668 DEVMETHOD(device_resume, ata_pcichannel_resume), 669 670 /* ATA methods */ 671 DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 672 DEVMETHOD(ata_locking, ata_pcichannel_locking), 673 DEVMETHOD(ata_reset, ata_pcichannel_reset), 674 675 { 0, 0 } 676 }; 677 678 driver_t ata_pcichannel_driver = { 679 "ata", 680 ata_pcichannel_methods, 681 sizeof(struct ata_channel), 682 }; 683 684 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0); 685 686 687 /* 688 * misc support fucntions 689 */ 690 int 691 ata_legacy(device_t dev) 692 { 693 return (((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&& 694 ((pci_read_config(dev, PCIR_PROGIF, 1) & 695 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) != 696 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) || 697 (!pci_read_config(dev, PCIR_BAR(0), 4) && 698 !pci_read_config(dev, PCIR_BAR(1), 4) && 699 !pci_read_config(dev, PCIR_BAR(2), 4) && 700 !pci_read_config(dev, PCIR_BAR(3), 4) && 701 !pci_read_config(dev, PCIR_BAR(5), 4))); 702 } 703 704 void 705 ata_generic_intr(void *data) 706 { 707 struct ata_pci_controller *ctlr = data; 708 struct ata_channel *ch; 709 int unit; 710 711 for (unit = 0; unit < ctlr->channels; unit++) { 712 if ((ch = ctlr->interrupt[unit].argument)) 713 ctlr->interrupt[unit].function(ch); 714 } 715 } 716 717 int 718 ata_setup_interrupt(device_t dev, void *intr_func) 719 { 720 struct ata_pci_controller *ctlr = device_get_softc(dev); 721 int i, msi = 0; 722 723 if (!ctlr->legacy) { 724 if (resource_int_value(device_get_name(dev), 725 device_get_unit(dev), "msi", &i) == 0 && i != 0) 726 msi = 1; 727 if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) { 728 ctlr->r_irq_rid = 0x1; 729 } else { 730 ctlr->r_irq_rid = ATA_IRQ_RID; 731 } 732 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 733 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 734 device_printf(dev, "unable to map interrupt\n"); 735 return ENXIO; 736 } 737 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL, 738 intr_func, ctlr, &ctlr->handle))) { 739 /* SOS XXX release r_irq */ 740 device_printf(dev, "unable to setup interrupt\n"); 741 return ENXIO; 742 } 743 } 744 return 0; 745 } 746 747 void 748 ata_set_desc(device_t dev) 749 { 750 struct ata_pci_controller *ctlr = device_get_softc(dev); 751 char buffer[128]; 752 753 sprintf(buffer, "%s %s %s controller", 754 ata_pcivendor2str(dev), ctlr->chip->text, 755 ata_mode2str(ctlr->chip->max_dma)); 756 device_set_desc_copy(dev, buffer); 757 } 758 759 struct ata_chip_id * 760 ata_match_chip(device_t dev, struct ata_chip_id *index) 761 { 762 while (index->chipid != 0) { 763 if (pci_get_devid(dev) == index->chipid && 764 pci_get_revid(dev) >= index->chiprev) 765 return index; 766 index++; 767 } 768 return NULL; 769 } 770 771 struct ata_chip_id * 772 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot) 773 { 774 device_t *children; 775 int nchildren, i; 776 777 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 778 return 0; 779 780 while (index->chipid != 0) { 781 for (i = 0; i < nchildren; i++) { 782 if (((slot >= 0 && pci_get_slot(children[i]) == slot) || 783 (slot < 0 && pci_get_slot(children[i]) <= -slot)) && 784 pci_get_devid(children[i]) == index->chipid && 785 pci_get_revid(children[i]) >= index->chiprev) { 786 free(children, M_TEMP); 787 return index; 788 } 789 } 790 index++; 791 } 792 free(children, M_TEMP); 793 return NULL; 794 } 795 796 void 797 ata_print_cable(device_t dev, u_int8_t *who) 798 { 799 device_printf(dev, 800 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who); 801 } 802 803 int 804 ata_check_80pin(device_t dev, int mode) 805 { 806 struct ata_device *atadev = device_get_softc(dev); 807 808 if (!ata_dma_check_80pin) { 809 if (bootverbose) 810 device_printf(dev, "Skipping 80pin cable check\n"); 811 return mode; 812 } 813 814 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) { 815 ata_print_cable(dev, "device"); 816 mode = ATA_UDMA2; 817 } 818 return mode; 819 } 820 821 char * 822 ata_pcivendor2str(device_t dev) 823 { 824 switch (pci_get_vendor(dev)) { 825 case ATA_ACARD_ID: return "Acard"; 826 case ATA_ACER_LABS_ID: return "AcerLabs"; 827 case ATA_AMD_ID: return "AMD"; 828 case ATA_ADAPTEC_ID: return "Adaptec"; 829 case ATA_ATI_ID: return "ATI"; 830 case ATA_CYRIX_ID: return "Cyrix"; 831 case ATA_CYPRESS_ID: return "Cypress"; 832 case ATA_HIGHPOINT_ID: return "HighPoint"; 833 case ATA_INTEL_ID: return "Intel"; 834 case ATA_ITE_ID: return "ITE"; 835 case ATA_JMICRON_ID: return "JMicron"; 836 case ATA_MARVELL_ID: return "Marvell"; 837 case ATA_NATIONAL_ID: return "National"; 838 case ATA_NETCELL_ID: return "Netcell"; 839 case ATA_NVIDIA_ID: return "nVidia"; 840 case ATA_PROMISE_ID: return "Promise"; 841 case ATA_SERVERWORKS_ID: return "ServerWorks"; 842 case ATA_SILICON_IMAGE_ID: return "SiI"; 843 case ATA_SIS_ID: return "SiS"; 844 case ATA_VIA_ID: return "VIA"; 845 case ATA_CENATEK_ID: return "Cenatek"; 846 case ATA_MICRON_ID: return "Micron"; 847 default: return "Generic"; 848 } 849 } 850 851 int 852 ata_mode2idx(int mode) 853 { 854 if ((mode & ATA_DMA_MASK) == ATA_UDMA0) 855 return (mode & ATA_MODE_MASK) + 8; 856 if ((mode & ATA_DMA_MASK) == ATA_WDMA0) 857 return (mode & ATA_MODE_MASK) + 5; 858 return (mode & ATA_MODE_MASK) - ATA_PIO0; 859 } 860 861