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 u_int32_t cmd; 92 int unit; 93 94 /* do chipset specific setups only needed once */ 95 ctlr->legacy = ata_legacy(dev); 96 if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK) 97 ctlr->channels = 2; 98 else 99 ctlr->channels = 1; 100 ctlr->allocate = ata_pci_allocate; 101 ctlr->dmainit = ata_pci_dmainit; 102 ctlr->dev = dev; 103 104 /* if needed try to enable busmastering */ 105 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 106 if (!(cmd & PCIM_CMD_BUSMASTEREN)) { 107 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2); 108 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 109 } 110 111 /* if busmastering mode "stuck" use it */ 112 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 113 ctlr->r_type1 = SYS_RES_IOPORT; 114 ctlr->r_rid1 = ATA_BMADDR_RID; 115 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1, 116 RF_ACTIVE); 117 } 118 119 if (ctlr->chipinit(dev)) 120 return ENXIO; 121 122 /* attach all channels on this controller */ 123 for (unit = 0; unit < ctlr->channels; unit++) { 124 if ((unit == 0 || unit == 1) && ctlr->legacy) { 125 device_add_child(dev, "ata", unit); 126 continue; 127 } 128 device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2)); 129 } 130 bus_generic_attach(dev); 131 return 0; 132 } 133 134 int 135 ata_pci_detach(device_t dev) 136 { 137 struct ata_pci_controller *ctlr = device_get_softc(dev); 138 device_t *children; 139 int nchildren, i; 140 141 /* detach & delete all children */ 142 if (!device_get_children(dev, &children, &nchildren)) { 143 for (i = 0; i < nchildren; i++) 144 device_delete_child(dev, children[i]); 145 free(children, M_TEMP); 146 } 147 148 if (ctlr->r_irq) { 149 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle); 150 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq); 151 } 152 if (ctlr->r_res2) 153 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2); 154 if (ctlr->r_res1) 155 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1); 156 157 return 0; 158 } 159 160 int 161 ata_pci_suspend(device_t dev) 162 { 163 struct ata_pci_controller *ctlr = device_get_softc(dev); 164 int error = 0; 165 166 bus_generic_suspend(dev); 167 if (ctlr->suspend) 168 error = ctlr->suspend(dev); 169 return error; 170 } 171 172 int 173 ata_pci_resume(device_t dev) 174 { 175 struct ata_pci_controller *ctlr = device_get_softc(dev); 176 int error = 0; 177 178 if (ctlr->resume) 179 error = ctlr->resume(dev); 180 bus_generic_resume(dev); 181 return error; 182 } 183 184 struct resource * 185 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 186 u_long start, u_long end, u_long count, u_int flags) 187 { 188 struct ata_pci_controller *controller = device_get_softc(dev); 189 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 190 struct resource *res = NULL; 191 int myrid; 192 193 if (type == SYS_RES_IOPORT) { 194 switch (*rid) { 195 case ATA_IOADDR_RID: 196 if (controller->legacy) { 197 start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 198 count = ATA_IOSIZE; 199 end = start + count - 1; 200 } 201 myrid = PCIR_BAR(0) + (unit << 3); 202 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 203 SYS_RES_IOPORT, &myrid, 204 start, end, count, flags); 205 break; 206 207 case ATA_CTLADDR_RID: 208 if (controller->legacy) { 209 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET; 210 count = ATA_CTLIOSIZE; 211 end = start + count - 1; 212 } 213 myrid = PCIR_BAR(1) + (unit << 3); 214 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 215 SYS_RES_IOPORT, &myrid, 216 start, end, count, flags); 217 break; 218 } 219 } 220 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 221 if (controller->legacy) { 222 int irq = (unit == 0 ? 14 : 15); 223 224 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 225 SYS_RES_IRQ, rid, irq, irq, 1, flags); 226 } 227 else 228 res = controller->r_irq; 229 } 230 return res; 231 } 232 233 int 234 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 235 struct resource *r) 236 { 237 struct ata_pci_controller *controller = device_get_softc(dev); 238 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 239 240 if (type == SYS_RES_IOPORT) { 241 switch (rid) { 242 case ATA_IOADDR_RID: 243 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 244 SYS_RES_IOPORT, 245 PCIR_BAR(0) + (unit << 3), r); 246 break; 247 248 case ATA_CTLADDR_RID: 249 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 250 SYS_RES_IOPORT, 251 PCIR_BAR(1) + (unit << 3), r); 252 break; 253 default: 254 return ENOENT; 255 } 256 } 257 if (type == SYS_RES_IRQ) { 258 if (rid != ATA_IRQ_RID) 259 return ENOENT; 260 261 if (controller->legacy) { 262 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 263 SYS_RES_IRQ, rid, r); 264 } 265 else 266 return 0; 267 } 268 return EINVAL; 269 } 270 271 int 272 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 273 int flags, driver_filter_t *filter, driver_intr_t *function, 274 void *argument, void **cookiep) 275 { 276 struct ata_pci_controller *controller = device_get_softc(dev); 277 278 if (controller->legacy) { 279 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 280 flags, filter, function, argument, cookiep); 281 } 282 else { 283 struct ata_pci_controller *controller = device_get_softc(dev); 284 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 285 286 if (filter != NULL) { 287 printf("ata-pci.c: we cannot use a filter here\n"); 288 return (EINVAL); 289 } 290 controller->interrupt[unit].function = function; 291 controller->interrupt[unit].argument = argument; 292 *cookiep = controller; 293 return 0; 294 } 295 } 296 297 int 298 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 299 void *cookie) 300 { 301 struct ata_pci_controller *controller = device_get_softc(dev); 302 303 if (controller->legacy) { 304 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 305 } 306 else { 307 struct ata_pci_controller *controller = device_get_softc(dev); 308 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 309 310 controller->interrupt[unit].function = NULL; 311 controller->interrupt[unit].argument = NULL; 312 return 0; 313 } 314 } 315 316 static void 317 ata_generic_setmode(device_t dev, int mode) 318 { 319 struct ata_device *atadev = device_get_softc(dev); 320 321 mode = ata_limit_mode(dev, mode, ATA_UDMA2); 322 mode = ata_check_80pin(dev, mode); 323 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode)) 324 atadev->mode = mode; 325 } 326 327 static int 328 ata_generic_chipinit(device_t dev) 329 { 330 struct ata_pci_controller *ctlr = device_get_softc(dev); 331 332 if (ata_setup_interrupt(dev, ata_generic_intr)) 333 return ENXIO; 334 ctlr->setmode = ata_generic_setmode; 335 return 0; 336 } 337 338 int 339 ata_pci_allocate(device_t dev) 340 { 341 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 342 struct ata_channel *ch = device_get_softc(dev); 343 struct resource *io = NULL, *ctlio = NULL; 344 int i, rid; 345 346 rid = ATA_IOADDR_RID; 347 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE))) 348 return ENXIO; 349 350 rid = ATA_CTLADDR_RID; 351 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){ 352 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 353 return ENXIO; 354 } 355 356 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) { 357 ch->r_io[i].res = io; 358 ch->r_io[i].offset = i; 359 } 360 ch->r_io[ATA_CONTROL].res = ctlio; 361 ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2; 362 ch->r_io[ATA_IDX_ADDR].res = io; 363 ata_default_registers(dev); 364 if (ctlr->r_res1) { 365 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 366 ch->r_io[i].res = ctlr->r_res1; 367 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE); 368 } 369 } 370 371 ata_pci_hw(dev); 372 return 0; 373 } 374 375 int 376 ata_pci_status(device_t dev) 377 { 378 struct ata_pci_controller *controller = 379 device_get_softc(device_get_parent(dev)); 380 struct ata_channel *ch = device_get_softc(dev); 381 382 if ((dumping || !controller->legacy) && 383 ((ch->flags & ATA_ALWAYS_DMASTAT) || 384 (ch->dma.flags & ATA_DMA_ACTIVE))) { 385 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 386 387 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != 388 ATA_BMSTAT_INTERRUPT) 389 return 0; 390 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR); 391 DELAY(1); 392 } 393 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 394 DELAY(100); 395 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 396 return 0; 397 } 398 return 1; 399 } 400 401 void 402 ata_pci_hw(device_t dev) 403 { 404 struct ata_channel *ch = device_get_softc(dev); 405 406 ata_generic_hw(dev); 407 ch->hw.status = ata_pci_status; 408 } 409 410 static int 411 ata_pci_dmastart(struct ata_request *request) 412 { 413 struct ata_channel *ch = device_get_softc(request->parent); 414 415 ATA_DEBUG_RQ(request, "dmastart"); 416 417 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 418 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 419 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus); 420 ch->dma.flags |= ATA_DMA_ACTIVE; 421 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 422 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) | 423 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)| 424 ATA_BMCMD_START_STOP); 425 return 0; 426 } 427 428 static int 429 ata_pci_dmastop(struct ata_request *request) 430 { 431 struct ata_channel *ch = device_get_softc(request->parent); 432 int error; 433 434 ATA_DEBUG_RQ(request, "dmastop"); 435 436 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 437 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 438 ch->dma.flags &= ~ATA_DMA_ACTIVE; 439 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 440 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 441 return error; 442 } 443 444 static void 445 ata_pci_dmareset(device_t dev) 446 { 447 struct ata_channel *ch = device_get_softc(dev); 448 struct ata_request *request; 449 450 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 451 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 452 ch->dma.flags &= ~ATA_DMA_ACTIVE; 453 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 454 if ((request = ch->running)) { 455 device_printf(request->dev, "DMA reset calling unload\n"); 456 ch->dma.unload(request); 457 } 458 } 459 460 void 461 ata_pci_dmainit(device_t dev) 462 { 463 struct ata_channel *ch = device_get_softc(dev); 464 465 ata_dmainit(dev); 466 ch->dma.start = ata_pci_dmastart; 467 ch->dma.stop = ata_pci_dmastop; 468 ch->dma.reset = ata_pci_dmareset; 469 } 470 471 472 static device_method_t ata_pci_methods[] = { 473 /* device interface */ 474 DEVMETHOD(device_probe, ata_pci_probe), 475 DEVMETHOD(device_attach, ata_pci_attach), 476 DEVMETHOD(device_detach, ata_pci_detach), 477 DEVMETHOD(device_suspend, ata_pci_suspend), 478 DEVMETHOD(device_resume, ata_pci_resume), 479 DEVMETHOD(device_shutdown, bus_generic_shutdown), 480 481 /* bus methods */ 482 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 483 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 484 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 485 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 486 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 487 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 488 489 { 0, 0 } 490 }; 491 492 devclass_t ata_pci_devclass; 493 494 static driver_t ata_pci_driver = { 495 "atapci", 496 ata_pci_methods, 497 sizeof(struct ata_pci_controller), 498 }; 499 500 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 501 MODULE_VERSION(atapci, 1); 502 MODULE_DEPEND(atapci, ata, 1, 1, 1); 503 504 static int 505 ata_pcichannel_probe(device_t dev) 506 { 507 struct ata_channel *ch = device_get_softc(dev); 508 device_t *children; 509 int count, i; 510 char buffer[32]; 511 512 /* take care of green memory */ 513 bzero(ch, sizeof(struct ata_channel)); 514 515 /* find channel number on this controller */ 516 device_get_children(device_get_parent(dev), &children, &count); 517 for (i = 0; i < count; i++) { 518 if (children[i] == dev) 519 ch->unit = i; 520 } 521 free(children, M_TEMP); 522 523 sprintf(buffer, "ATA channel %d", ch->unit); 524 device_set_desc_copy(dev, buffer); 525 526 return ata_probe(dev); 527 } 528 529 static int 530 ata_pcichannel_attach(device_t dev) 531 { 532 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 533 int error; 534 535 if (ctlr->dmainit) 536 ctlr->dmainit(dev); 537 538 if ((error = ctlr->allocate(dev))) 539 return error; 540 541 return ata_attach(dev); 542 } 543 544 static int 545 ata_pcichannel_detach(device_t dev) 546 { 547 struct ata_channel *ch = device_get_softc(dev); 548 int error; 549 550 if ((error = ata_detach(dev))) 551 return error; 552 553 ch->dma.free(dev); 554 555 /* XXX SOS free resources for io and ctlio ?? */ 556 557 return 0; 558 } 559 560 static int 561 ata_pcichannel_locking(device_t dev, int mode) 562 { 563 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 564 struct ata_channel *ch = device_get_softc(dev); 565 566 if (ctlr->locking) 567 return ctlr->locking(dev, mode); 568 else 569 return ch->unit; 570 } 571 572 static void 573 ata_pcichannel_reset(device_t dev) 574 { 575 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 576 struct ata_channel *ch = device_get_softc(dev); 577 578 /* if DMA engine present reset it */ 579 if (ch->dma.reset) 580 ch->dma.reset(dev); 581 582 /* reset the controller HW */ 583 if (ctlr->reset) 584 ctlr->reset(dev); 585 else 586 ata_generic_reset(dev); 587 } 588 589 static void 590 ata_pcichannel_setmode(device_t parent, device_t dev) 591 { 592 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 593 struct ata_device *atadev = device_get_softc(dev); 594 int mode = atadev->mode; 595 596 ctlr->setmode(dev, ATA_PIO_MAX); 597 if (mode >= ATA_DMA) 598 ctlr->setmode(dev, mode); 599 } 600 601 static device_method_t ata_pcichannel_methods[] = { 602 /* device interface */ 603 DEVMETHOD(device_probe, ata_pcichannel_probe), 604 DEVMETHOD(device_attach, ata_pcichannel_attach), 605 DEVMETHOD(device_detach, ata_pcichannel_detach), 606 DEVMETHOD(device_shutdown, bus_generic_shutdown), 607 DEVMETHOD(device_suspend, ata_suspend), 608 DEVMETHOD(device_resume, ata_resume), 609 610 /* ATA methods */ 611 DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 612 DEVMETHOD(ata_locking, ata_pcichannel_locking), 613 DEVMETHOD(ata_reset, ata_pcichannel_reset), 614 615 { 0, 0 } 616 }; 617 618 driver_t ata_pcichannel_driver = { 619 "ata", 620 ata_pcichannel_methods, 621 sizeof(struct ata_channel), 622 }; 623 624 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0); 625 626 627 /* 628 * misc support fucntions 629 */ 630 int 631 ata_legacy(device_t dev) 632 { 633 return (((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&& 634 ((pci_read_config(dev, PCIR_PROGIF, 1) & 635 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) != 636 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) || 637 (!pci_read_config(dev, PCIR_BAR(0), 4) && 638 !pci_read_config(dev, PCIR_BAR(1), 4) && 639 !pci_read_config(dev, PCIR_BAR(2), 4) && 640 !pci_read_config(dev, PCIR_BAR(3), 4) && 641 !pci_read_config(dev, PCIR_BAR(5), 4))); 642 } 643 644 void 645 ata_generic_intr(void *data) 646 { 647 struct ata_pci_controller *ctlr = data; 648 struct ata_channel *ch; 649 int unit; 650 651 for (unit = 0; unit < ctlr->channels; unit++) { 652 if ((ch = ctlr->interrupt[unit].argument)) 653 ctlr->interrupt[unit].function(ch); 654 } 655 } 656 657 int 658 ata_setup_interrupt(device_t dev, void *intr_func) 659 { 660 struct ata_pci_controller *ctlr = device_get_softc(dev); 661 int rid = ATA_IRQ_RID; 662 663 if (!ctlr->legacy) { 664 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 665 RF_SHAREABLE | RF_ACTIVE))) { 666 device_printf(dev, "unable to map interrupt\n"); 667 return ENXIO; 668 } 669 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL, 670 intr_func, ctlr, &ctlr->handle))) { 671 /* SOS XXX release r_irq */ 672 device_printf(dev, "unable to setup interrupt\n"); 673 return ENXIO; 674 } 675 } 676 return 0; 677 } 678 679 void 680 ata_set_desc(device_t dev) 681 { 682 struct ata_pci_controller *ctlr = device_get_softc(dev); 683 char buffer[128]; 684 685 sprintf(buffer, "%s %s %s controller", 686 ata_pcivendor2str(dev), ctlr->chip->text, 687 ata_mode2str(ctlr->chip->max_dma)); 688 device_set_desc_copy(dev, buffer); 689 } 690 691 struct ata_chip_id * 692 ata_match_chip(device_t dev, struct ata_chip_id *index) 693 { 694 while (index->chipid != 0) { 695 if (pci_get_devid(dev) == index->chipid && 696 pci_get_revid(dev) >= index->chiprev) 697 return index; 698 index++; 699 } 700 return NULL; 701 } 702 703 struct ata_chip_id * 704 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot) 705 { 706 device_t *children; 707 int nchildren, i; 708 709 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 710 return 0; 711 712 while (index->chipid != 0) { 713 for (i = 0; i < nchildren; i++) { 714 if (((slot >= 0 && pci_get_slot(children[i]) == slot) || 715 (slot < 0 && pci_get_slot(children[i]) <= -slot)) && 716 pci_get_devid(children[i]) == index->chipid && 717 pci_get_revid(children[i]) >= index->chiprev) { 718 free(children, M_TEMP); 719 return index; 720 } 721 } 722 index++; 723 } 724 free(children, M_TEMP); 725 return NULL; 726 } 727 728 void 729 ata_print_cable(device_t dev, u_int8_t *who) 730 { 731 device_printf(dev, 732 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who); 733 } 734 735 int 736 ata_check_80pin(device_t dev, int mode) 737 { 738 struct ata_device *atadev = device_get_softc(dev); 739 740 if (!ata_dma_check_80pin) { 741 if (bootverbose) 742 device_printf(dev, "Skipping 80pin cable check\n"); 743 return mode; 744 } 745 746 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) { 747 ata_print_cable(dev, "device"); 748 mode = ATA_UDMA2; 749 } 750 return mode; 751 } 752 753 char * 754 ata_pcivendor2str(device_t dev) 755 { 756 switch (pci_get_vendor(dev)) { 757 case ATA_ACARD_ID: return "Acard"; 758 case ATA_ACER_LABS_ID: return "AcerLabs"; 759 case ATA_AMD_ID: return "AMD"; 760 case ATA_ADAPTEC_ID: return "Adaptec"; 761 case ATA_ATI_ID: return "ATI"; 762 case ATA_CYRIX_ID: return "Cyrix"; 763 case ATA_CYPRESS_ID: return "Cypress"; 764 case ATA_HIGHPOINT_ID: return "HighPoint"; 765 case ATA_INTEL_ID: return "Intel"; 766 case ATA_ITE_ID: return "ITE"; 767 case ATA_JMICRON_ID: return "JMicron"; 768 case ATA_MARVELL_ID: return "Marvell"; 769 case ATA_NATIONAL_ID: return "National"; 770 case ATA_NETCELL_ID: return "Netcell"; 771 case ATA_NVIDIA_ID: return "nVidia"; 772 case ATA_PROMISE_ID: return "Promise"; 773 case ATA_SERVERWORKS_ID: return "ServerWorks"; 774 case ATA_SILICON_IMAGE_ID: return "SiI"; 775 case ATA_SIS_ID: return "SiS"; 776 case ATA_VIA_ID: return "VIA"; 777 case ATA_CENATEK_ID: return "Cenatek"; 778 case ATA_MICRON_ID: return "Micron"; 779 default: return "Generic"; 780 } 781 } 782 783 int 784 ata_mode2idx(int mode) 785 { 786 if ((mode & ATA_DMA_MASK) == ATA_UDMA0) 787 return (mode & ATA_MODE_MASK) + 8; 788 if ((mode & ATA_DMA_MASK) == ATA_WDMA0) 789 return (mode & ATA_MODE_MASK) + 5; 790 return (mode & ATA_MODE_MASK) - ATA_PIO0; 791 } 792 793