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 (BUS_PROBE_GENERIC); 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 resource_int_value(device_get_name(dev), 560 device_get_unit(dev), "pm_level", &ch->pm_level); 561 562 if ((error = ctlr->ch_attach(dev))) 563 return error; 564 565 return ata_attach(dev); 566 } 567 568 static int 569 ata_pcichannel_detach(device_t dev) 570 { 571 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 572 struct ata_channel *ch = device_get_softc(dev); 573 int error; 574 575 if (!ch->attached) 576 return (0); 577 ch->attached = 0; 578 579 if ((error = ata_detach(dev))) 580 return error; 581 582 if (ctlr->ch_detach) 583 return (ctlr->ch_detach(dev)); 584 585 return (0); 586 } 587 static int 588 ata_pcichannel_suspend(device_t dev) 589 { 590 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 591 struct ata_channel *ch = device_get_softc(dev); 592 int error; 593 594 if (!ch->attached) 595 return (0); 596 597 if ((error = ata_suspend(dev))) 598 return (error); 599 600 if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev))) 601 return (error); 602 603 return (0); 604 } 605 606 static int 607 ata_pcichannel_resume(device_t dev) 608 { 609 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 610 struct ata_channel *ch = device_get_softc(dev); 611 int error; 612 613 if (!ch->attached) 614 return (0); 615 616 if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev))) 617 return (error); 618 619 return ata_resume(dev); 620 } 621 622 623 static int 624 ata_pcichannel_locking(device_t dev, int mode) 625 { 626 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 627 struct ata_channel *ch = device_get_softc(dev); 628 629 if (ctlr->locking) 630 return ctlr->locking(dev, mode); 631 else 632 return ch->unit; 633 } 634 635 static void 636 ata_pcichannel_reset(device_t dev) 637 { 638 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 639 struct ata_channel *ch = device_get_softc(dev); 640 641 /* if DMA engine present reset it */ 642 if (ch->dma.reset) 643 ch->dma.reset(dev); 644 645 /* reset the controller HW */ 646 if (ctlr->reset) 647 ctlr->reset(dev); 648 else 649 ata_generic_reset(dev); 650 } 651 652 static void 653 ata_pcichannel_setmode(device_t parent, device_t dev) 654 { 655 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 656 struct ata_device *atadev = device_get_softc(dev); 657 int mode = atadev->mode; 658 659 ctlr->setmode(dev, ATA_PIO_MAX); 660 if (mode >= ATA_DMA) 661 ctlr->setmode(dev, mode); 662 } 663 664 static device_method_t ata_pcichannel_methods[] = { 665 /* device interface */ 666 DEVMETHOD(device_probe, ata_pcichannel_probe), 667 DEVMETHOD(device_attach, ata_pcichannel_attach), 668 DEVMETHOD(device_detach, ata_pcichannel_detach), 669 DEVMETHOD(device_shutdown, bus_generic_shutdown), 670 DEVMETHOD(device_suspend, ata_pcichannel_suspend), 671 DEVMETHOD(device_resume, ata_pcichannel_resume), 672 673 /* ATA methods */ 674 DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 675 DEVMETHOD(ata_locking, ata_pcichannel_locking), 676 DEVMETHOD(ata_reset, ata_pcichannel_reset), 677 678 { 0, 0 } 679 }; 680 681 driver_t ata_pcichannel_driver = { 682 "ata", 683 ata_pcichannel_methods, 684 sizeof(struct ata_channel), 685 }; 686 687 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0); 688 689 690 /* 691 * misc support fucntions 692 */ 693 int 694 ata_legacy(device_t dev) 695 { 696 return (((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&& 697 ((pci_read_config(dev, PCIR_PROGIF, 1) & 698 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) != 699 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) || 700 (!pci_read_config(dev, PCIR_BAR(0), 4) && 701 !pci_read_config(dev, PCIR_BAR(1), 4) && 702 !pci_read_config(dev, PCIR_BAR(2), 4) && 703 !pci_read_config(dev, PCIR_BAR(3), 4) && 704 !pci_read_config(dev, PCIR_BAR(5), 4))); 705 } 706 707 void 708 ata_generic_intr(void *data) 709 { 710 struct ata_pci_controller *ctlr = data; 711 struct ata_channel *ch; 712 int unit; 713 714 for (unit = 0; unit < ctlr->channels; unit++) { 715 if ((ch = ctlr->interrupt[unit].argument)) 716 ctlr->interrupt[unit].function(ch); 717 } 718 } 719 720 int 721 ata_setup_interrupt(device_t dev, void *intr_func) 722 { 723 struct ata_pci_controller *ctlr = device_get_softc(dev); 724 int i, msi = 0; 725 726 if (!ctlr->legacy) { 727 if (resource_int_value(device_get_name(dev), 728 device_get_unit(dev), "msi", &i) == 0 && i != 0) 729 msi = 1; 730 if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) { 731 ctlr->r_irq_rid = 0x1; 732 } else { 733 ctlr->r_irq_rid = ATA_IRQ_RID; 734 } 735 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 736 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 737 device_printf(dev, "unable to map interrupt\n"); 738 return ENXIO; 739 } 740 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL, 741 intr_func, ctlr, &ctlr->handle))) { 742 /* SOS XXX release r_irq */ 743 device_printf(dev, "unable to setup interrupt\n"); 744 return ENXIO; 745 } 746 } 747 return 0; 748 } 749 750 void 751 ata_set_desc(device_t dev) 752 { 753 struct ata_pci_controller *ctlr = device_get_softc(dev); 754 char buffer[128]; 755 756 sprintf(buffer, "%s %s %s controller", 757 ata_pcivendor2str(dev), ctlr->chip->text, 758 ata_mode2str(ctlr->chip->max_dma)); 759 device_set_desc_copy(dev, buffer); 760 } 761 762 struct ata_chip_id * 763 ata_match_chip(device_t dev, struct ata_chip_id *index) 764 { 765 uint32_t devid; 766 uint8_t revid; 767 768 devid = pci_get_devid(dev); 769 revid = pci_get_revid(dev); 770 while (index->chipid != 0) { 771 if (devid == index->chipid && revid >= index->chiprev) 772 return (index); 773 index++; 774 } 775 return (NULL); 776 } 777 778 struct ata_chip_id * 779 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot) 780 { 781 struct ata_chip_id *idx; 782 device_t *children; 783 int nchildren, i; 784 uint8_t s; 785 786 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 787 return (NULL); 788 789 for (i = 0; i < nchildren; i++) { 790 s = pci_get_slot(children[i]); 791 if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) { 792 idx = ata_match_chip(children[i], index); 793 if (idx != NULL) { 794 free(children, M_TEMP); 795 return (idx); 796 } 797 } 798 } 799 free(children, M_TEMP); 800 return (NULL); 801 } 802 803 void 804 ata_print_cable(device_t dev, u_int8_t *who) 805 { 806 device_printf(dev, 807 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who); 808 } 809 810 int 811 ata_check_80pin(device_t dev, int mode) 812 { 813 struct ata_device *atadev = device_get_softc(dev); 814 815 if (!ata_dma_check_80pin) { 816 if (bootverbose) 817 device_printf(dev, "Skipping 80pin cable check\n"); 818 return mode; 819 } 820 821 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) { 822 ata_print_cable(dev, "device"); 823 mode = ATA_UDMA2; 824 } 825 return mode; 826 } 827 828 char * 829 ata_pcivendor2str(device_t dev) 830 { 831 switch (pci_get_vendor(dev)) { 832 case ATA_ACARD_ID: return "Acard"; 833 case ATA_ACER_LABS_ID: return "AcerLabs"; 834 case ATA_AMD_ID: return "AMD"; 835 case ATA_ADAPTEC_ID: return "Adaptec"; 836 case ATA_ATI_ID: return "ATI"; 837 case ATA_CYRIX_ID: return "Cyrix"; 838 case ATA_CYPRESS_ID: return "Cypress"; 839 case ATA_HIGHPOINT_ID: return "HighPoint"; 840 case ATA_INTEL_ID: return "Intel"; 841 case ATA_ITE_ID: return "ITE"; 842 case ATA_JMICRON_ID: return "JMicron"; 843 case ATA_MARVELL_ID: return "Marvell"; 844 case ATA_NATIONAL_ID: return "National"; 845 case ATA_NETCELL_ID: return "Netcell"; 846 case ATA_NVIDIA_ID: return "nVidia"; 847 case ATA_PROMISE_ID: return "Promise"; 848 case ATA_SERVERWORKS_ID: return "ServerWorks"; 849 case ATA_SILICON_IMAGE_ID: return "SiI"; 850 case ATA_SIS_ID: return "SiS"; 851 case ATA_VIA_ID: return "VIA"; 852 case ATA_CENATEK_ID: return "Cenatek"; 853 case ATA_MICRON_ID: return "Micron"; 854 default: return "Generic"; 855 } 856 } 857 858 int 859 ata_mode2idx(int mode) 860 { 861 if ((mode & ATA_DMA_MASK) == ATA_UDMA0) 862 return (mode & ATA_MODE_MASK) + 8; 863 if ((mode & ATA_DMA_MASK) == ATA_WDMA0) 864 return (mode & ATA_MODE_MASK) + 5; 865 return (mode & ATA_MODE_MASK) - ATA_PIO0; 866 } 867 868