1 /*- 2 * Copyright (c) 1998 - 2005 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ata.h" 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/ata.h> 38 #include <sys/bus.h> 39 #include <sys/malloc.h> 40 #include <sys/sema.h> 41 #include <sys/taskqueue.h> 42 #include <vm/uma.h> 43 #include <machine/stdarg.h> 44 #include <machine/resource.h> 45 #include <machine/bus.h> 46 #ifdef __alpha__ 47 #include <machine/md_var.h> 48 #endif 49 #include <sys/rman.h> 50 #include <dev/pci/pcivar.h> 51 #include <dev/pci/pcireg.h> 52 #include <dev/ata/ata-all.h> 53 #include <dev/ata/ata-pci.h> 54 #include <ata_if.h> 55 56 /* local vars */ 57 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI"); 58 59 /* misc defines */ 60 #define IOMASK 0xfffffffc 61 62 /* prototypes */ 63 static void ata_pci_dmainit(device_t); 64 65 int 66 ata_legacy(device_t dev) 67 { 68 return ((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV) && 69 ((pci_read_config(dev, PCIR_PROGIF, 1) & 70 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) != 71 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))); 72 } 73 74 int 75 ata_pci_probe(device_t dev) 76 { 77 if (pci_get_class(dev) != PCIC_STORAGE) 78 return ENXIO; 79 80 switch (pci_get_vendor(dev)) { 81 case ATA_ACARD_ID: 82 if (!ata_acard_ident(dev)) 83 return 0; 84 break; 85 case ATA_ACER_LABS_ID: 86 if (!ata_ali_ident(dev)) 87 return 0; 88 break; 89 case ATA_AMD_ID: 90 if (!ata_amd_ident(dev)) 91 return 0; 92 break; 93 case ATA_CYRIX_ID: 94 if (!ata_cyrix_ident(dev)) 95 return 0; 96 break; 97 case ATA_CYPRESS_ID: 98 if (!ata_cypress_ident(dev)) 99 return 0; 100 break; 101 case ATA_HIGHPOINT_ID: 102 if (!ata_highpoint_ident(dev)) 103 return 0; 104 break; 105 case ATA_INTEL_ID: 106 if (!ata_intel_ident(dev)) 107 return 0; 108 break; 109 case ATA_ITE_ID: 110 if (!ata_ite_ident(dev)) 111 return 0; 112 break; 113 case ATA_NATIONAL_ID: 114 if (!ata_national_ident(dev)) 115 return 0; 116 break; 117 case ATA_NVIDIA_ID: 118 if (!ata_nvidia_ident(dev)) 119 return 0; 120 break; 121 case ATA_PROMISE_ID: 122 if (!ata_promise_ident(dev)) 123 return 0; 124 break; 125 case ATA_SERVERWORKS_ID: 126 if (!ata_serverworks_ident(dev)) 127 return 0; 128 break; 129 case ATA_SILICON_IMAGE_ID: 130 if (!ata_sii_ident(dev)) 131 return 0; 132 break; 133 case ATA_SIS_ID: 134 if (!ata_sis_ident(dev)) 135 return 0; 136 break; 137 case ATA_VIA_ID: 138 if (!ata_via_ident(dev)) 139 return 0; 140 break; 141 case ATA_CENATEK_ID: 142 if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) { 143 ata_generic_ident(dev); 144 device_set_desc(dev, "Cenatek Rocket Drive controller"); 145 return 0; 146 } 147 break; 148 case ATA_MICRON_ID: 149 if (pci_get_devid(dev) == ATA_MICRON_RZ1000 || 150 pci_get_devid(dev) == ATA_MICRON_RZ1001) { 151 ata_generic_ident(dev); 152 device_set_desc(dev, 153 "RZ 100? ATA controller !WARNING! data loss/corruption risk"); 154 return 0; 155 } 156 break; 157 } 158 159 /* unknown chipset, try generic DMA if it seems possible */ 160 if ((pci_get_class(dev) == PCIC_STORAGE) && 161 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 162 return ata_generic_ident(dev); 163 164 return ENXIO; 165 } 166 167 int 168 ata_pci_attach(device_t dev) 169 { 170 struct ata_pci_controller *ctlr = device_get_softc(dev); 171 u_int32_t cmd; 172 int unit; 173 174 /* do chipset specific setups only needed once */ 175 if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK) 176 ctlr->channels = 2; 177 else 178 ctlr->channels = 1; 179 ctlr->allocate = ata_pci_allocate; 180 ctlr->dmainit = ata_pci_dmainit; 181 ctlr->dev = dev; 182 183 /* if needed try to enable busmastering */ 184 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 185 if (!(cmd & PCIM_CMD_BUSMASTEREN)) { 186 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2); 187 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 188 } 189 190 /* if busmastering mode "stuck" use it */ 191 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 192 ctlr->r_type1 = SYS_RES_IOPORT; 193 ctlr->r_rid1 = ATA_BMADDR_RID; 194 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1, 195 RF_ACTIVE); 196 } 197 198 ctlr->chipinit(dev); 199 200 /* attach all channels on this controller */ 201 for (unit = 0; unit < ctlr->channels; unit++) { 202 if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) { 203 device_add_child(dev, "ata", unit); 204 continue; 205 } 206 if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) { 207 device_add_child(dev, "ata", unit); 208 continue; 209 } 210 device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2)); 211 } 212 bus_generic_attach(dev); 213 return 0; 214 } 215 216 int 217 ata_pci_detach(device_t dev) 218 { 219 struct ata_pci_controller *ctlr = device_get_softc(dev); 220 device_t *children; 221 int nchildren, i; 222 223 /* detach & delete all children */ 224 if (!device_get_children(dev, &children, &nchildren)) { 225 for (i = 0; i < nchildren; i++) 226 device_delete_child(dev, children[i]); 227 free(children, M_TEMP); 228 } 229 230 if (ctlr->r_irq) { 231 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle); 232 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq); 233 } 234 if (ctlr->r_res2) 235 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2); 236 if (ctlr->r_res1) 237 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1); 238 239 return 0; 240 } 241 242 struct resource * 243 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 244 u_long start, u_long end, u_long count, u_int flags) 245 { 246 struct ata_pci_controller *controller = device_get_softc(dev); 247 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 248 struct resource *res = NULL; 249 int myrid; 250 251 if (type == SYS_RES_IOPORT) { 252 switch (*rid) { 253 case ATA_IOADDR_RID: 254 if (ata_legacy(dev)) { 255 start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 256 count = ATA_IOSIZE; 257 end = start + count - 1; 258 } 259 myrid = PCIR_BAR(0) + (unit << 3); 260 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 261 SYS_RES_IOPORT, &myrid, 262 start, end, count, flags); 263 break; 264 265 case ATA_CTLADDR_RID: 266 if (ata_legacy(dev)) { 267 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET; 268 count = ATA_CTLIOSIZE; 269 end = start + count - 1; 270 } 271 myrid = PCIR_BAR(1) + (unit << 3); 272 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 273 SYS_RES_IOPORT, &myrid, 274 start, end, count, flags); 275 break; 276 } 277 } 278 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 279 if (ata_legacy(dev)) { 280 #ifdef __alpha__ 281 res = alpha_platform_alloc_ide_intr(unit); 282 #else 283 int irq = (unit == 0 ? 14 : 15); 284 285 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 286 SYS_RES_IRQ, rid, irq, irq, 1, flags); 287 #endif 288 } 289 else 290 res = controller->r_irq; 291 } 292 return res; 293 } 294 295 int 296 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 297 struct resource *r) 298 { 299 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 300 301 if (type == SYS_RES_IOPORT) { 302 switch (rid) { 303 case ATA_IOADDR_RID: 304 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 305 SYS_RES_IOPORT, 306 PCIR_BAR(0) + (unit << 3), r); 307 break; 308 309 case ATA_CTLADDR_RID: 310 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 311 SYS_RES_IOPORT, 312 PCIR_BAR(1) + (unit << 3), r); 313 break; 314 default: 315 return ENOENT; 316 } 317 } 318 if (type == SYS_RES_IRQ) { 319 if (rid != ATA_IRQ_RID) 320 return ENOENT; 321 322 if (ata_legacy(dev)) { 323 #ifdef __alpha__ 324 return alpha_platform_release_ide_intr(unit, r); 325 #else 326 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 327 SYS_RES_IRQ, rid, r); 328 #endif 329 } 330 else 331 return 0; 332 } 333 return EINVAL; 334 } 335 336 int 337 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 338 int flags, driver_intr_t *function, void *argument, 339 void **cookiep) 340 { 341 if (ata_legacy(dev)) { 342 #ifdef __alpha__ 343 return alpha_platform_setup_ide_intr(child, irq, function, argument, 344 cookiep); 345 #else 346 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 347 flags, function, argument, cookiep); 348 #endif 349 } 350 else { 351 struct ata_pci_controller *controller = device_get_softc(dev); 352 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 353 354 controller->interrupt[unit].function = function; 355 controller->interrupt[unit].argument = argument; 356 *cookiep = controller; 357 return 0; 358 } 359 } 360 361 int 362 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 363 void *cookie) 364 { 365 if (ata_legacy(dev)) { 366 #ifdef __alpha__ 367 return alpha_platform_teardown_ide_intr(child, irq, cookie); 368 #else 369 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 370 #endif 371 } 372 else { 373 struct ata_pci_controller *controller = device_get_softc(dev); 374 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 375 376 controller->interrupt[unit].function = NULL; 377 controller->interrupt[unit].argument = NULL; 378 return 0; 379 } 380 } 381 382 int 383 ata_pci_allocate(device_t dev) 384 { 385 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 386 struct ata_channel *ch = device_get_softc(dev); 387 struct resource *io = NULL, *ctlio = NULL; 388 int i, rid; 389 390 rid = ATA_IOADDR_RID; 391 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE))) 392 return ENXIO; 393 394 rid = ATA_CTLADDR_RID; 395 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){ 396 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 397 return ENXIO; 398 } 399 400 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) { 401 ch->r_io[i].res = io; 402 ch->r_io[i].offset = i; 403 } 404 ch->r_io[ATA_CONTROL].res = ctlio; 405 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2; 406 ch->r_io[ATA_IDX_ADDR].res = io; 407 ata_default_registers(dev); 408 if (ctlr->r_res1) { 409 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 410 ch->r_io[i].res = ctlr->r_res1; 411 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE); 412 } 413 } 414 415 ata_generic_hw(dev); 416 return 0; 417 } 418 419 static int 420 ata_pci_dmastart(device_t dev) 421 { 422 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 423 424 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 425 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 426 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus); 427 ch->dma->flags |= ATA_DMA_ACTIVE; 428 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 429 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) | 430 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) | 431 ATA_BMCMD_START_STOP); 432 return 0; 433 } 434 435 static int 436 ata_pci_dmastop(device_t dev) 437 { 438 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 439 int error; 440 441 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 442 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 443 ch->dma->flags &= ~ATA_DMA_ACTIVE; 444 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 445 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 446 return error; 447 } 448 449 static void 450 ata_pci_dmareset(device_t dev) 451 { 452 struct ata_channel *ch = device_get_softc(dev); 453 454 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 455 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 456 ch->dma->flags &= ~ATA_DMA_ACTIVE; 457 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 458 ch->dma->unload(dev); 459 } 460 461 static void 462 ata_pci_dmainit(device_t dev) 463 { 464 struct ata_channel *ch = device_get_softc(dev); 465 466 ata_dmainit(dev); 467 if (ch->dma) { 468 ch->dma->start = ata_pci_dmastart; 469 ch->dma->stop = ata_pci_dmastop; 470 ch->dma->reset = ata_pci_dmareset; 471 } 472 } 473 474 static device_method_t ata_pci_methods[] = { 475 /* device interface */ 476 DEVMETHOD(device_probe, ata_pci_probe), 477 DEVMETHOD(device_attach, ata_pci_attach), 478 DEVMETHOD(device_detach, ata_pci_detach), 479 DEVMETHOD(device_shutdown, bus_generic_shutdown), 480 DEVMETHOD(device_suspend, bus_generic_suspend), 481 DEVMETHOD(device_resume, bus_generic_resume), 482 483 /* bus methods */ 484 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 485 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 486 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 487 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 488 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 489 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 490 491 { 0, 0 } 492 }; 493 494 devclass_t atapci_devclass; 495 496 static driver_t ata_pci_driver = { 497 "atapci", 498 ata_pci_methods, 499 sizeof(struct ata_pci_controller), 500 }; 501 502 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0); 503 MODULE_VERSION(atapci, 1); 504 MODULE_DEPEND(atapci, ata, 1, 1, 1); 505 506 static int 507 ata_pcichannel_probe(device_t dev) 508 { 509 struct ata_channel *ch = device_get_softc(dev); 510 device_t *children; 511 int count, i; 512 char buffer[32]; 513 514 /* take care of green memory */ 515 bzero(ch, sizeof(struct ata_channel)); 516 517 /* find channel number on this controller */ 518 device_get_children(device_get_parent(dev), &children, &count); 519 for (i = 0; i < count; i++) { 520 if (children[i] == dev) 521 ch->unit = i; 522 } 523 free(children, M_TEMP); 524 525 sprintf(buffer, "ATA channel %d", ch->unit); 526 device_set_desc_copy(dev, buffer); 527 528 return ata_probe(dev); 529 } 530 531 static int 532 ata_pcichannel_attach(device_t dev) 533 { 534 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 535 struct ata_channel *ch = device_get_softc(dev); 536 int error; 537 538 if (ctlr->r_res1) 539 ctlr->dmainit(dev); 540 if (ch->dma) 541 ch->dma->alloc(dev); 542 543 if ((error = ctlr->allocate(dev))) 544 return error; 545 546 return ata_attach(dev); 547 } 548 549 static int 550 ata_pcichannel_detach(device_t dev) 551 { 552 struct ata_channel *ch = device_get_softc(dev); 553 int error; 554 555 if ((error = ata_detach(dev))) 556 return error; 557 558 if (ch->dma) 559 ch->dma->free(dev); 560 561 /* XXX SOS free resources for io and ctlio ?? */ 562 563 return 0; 564 } 565 566 static int 567 ata_pcichannel_locking(device_t dev, int mode) 568 { 569 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 570 struct ata_channel *ch = device_get_softc(dev); 571 572 if (ctlr->locking) 573 return ctlr->locking(dev, mode); 574 else 575 return ch->unit; 576 } 577 578 static void 579 ata_pcichannel_reset(device_t dev) 580 { 581 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 582 struct ata_channel *ch = device_get_softc(dev); 583 584 /* if DMA engine present reset it */ 585 if (ch->dma) { 586 if (ch->dma->reset) 587 ch->dma->reset(dev); 588 ch->dma->unload(dev); 589 } 590 591 /* reset the controller HW */ 592 if (ctlr->reset) 593 ctlr->reset(dev); 594 else 595 ata_generic_reset(dev); 596 } 597 598 static void 599 ata_pcichannel_setmode(device_t parent, device_t dev) 600 { 601 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 602 struct ata_device *atadev = device_get_softc(dev); 603 int mode = atadev->mode; 604 605 ctlr->setmode(dev, ATA_PIO_MAX); 606 if (mode >= ATA_DMA) 607 ctlr->setmode(dev, mode); 608 } 609 610 static device_method_t ata_pcichannel_methods[] = { 611 /* device interface */ 612 DEVMETHOD(device_probe, ata_pcichannel_probe), 613 DEVMETHOD(device_attach, ata_pcichannel_attach), 614 DEVMETHOD(device_detach, ata_pcichannel_detach), 615 DEVMETHOD(device_shutdown, bus_generic_shutdown), 616 DEVMETHOD(device_suspend, ata_suspend), 617 DEVMETHOD(device_resume, ata_resume), 618 619 /* ATA methods */ 620 DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 621 DEVMETHOD(ata_locking, ata_pcichannel_locking), 622 DEVMETHOD(ata_reset, ata_pcichannel_reset), 623 624 { 0, 0 } 625 }; 626 627 driver_t ata_pcichannel_driver = { 628 "ata", 629 ata_pcichannel_methods, 630 sizeof(struct ata_channel), 631 }; 632 633 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0); 634