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(struct ata_channel *); 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 io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 392 if (!io) 393 return ENXIO; 394 395 rid = ATA_CTLADDR_RID; 396 ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 397 if (!ctlio) { 398 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 399 return ENXIO; 400 } 401 402 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) { 403 ch->r_io[i].res = io; 404 ch->r_io[i].offset = i; 405 } 406 ch->r_io[ATA_CONTROL].res = ctlio; 407 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2; 408 ch->r_io[ATA_IDX_ADDR].res = io; 409 ata_default_registers(ch); 410 if (ctlr->r_res1) { 411 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 412 ch->r_io[i].res = ctlr->r_res1; 413 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE); 414 } 415 } 416 417 ata_generic_hw(ch); 418 419 return 0; 420 } 421 422 static int 423 ata_pci_dmastart(struct ata_channel *ch) 424 { 425 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 426 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 427 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus); 428 ch->dma->flags |= ATA_DMA_ACTIVE; 429 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 430 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) | 431 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) | 432 ATA_BMCMD_START_STOP); 433 return 0; 434 } 435 436 static int 437 ata_pci_dmastop(struct ata_channel *ch) 438 { 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_dmainit(struct ata_channel *ch) 451 { 452 ata_dmainit(ch); 453 if (ch->dma) { 454 ch->dma->start = ata_pci_dmastart; 455 ch->dma->stop = ata_pci_dmastop; 456 } 457 } 458 459 static device_method_t ata_pci_methods[] = { 460 /* device interface */ 461 DEVMETHOD(device_probe, ata_pci_probe), 462 DEVMETHOD(device_attach, ata_pci_attach), 463 DEVMETHOD(device_detach, ata_pci_detach), 464 DEVMETHOD(device_shutdown, bus_generic_shutdown), 465 DEVMETHOD(device_suspend, bus_generic_suspend), 466 DEVMETHOD(device_resume, bus_generic_resume), 467 468 /* bus methods */ 469 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 470 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 471 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 472 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 473 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 474 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 475 476 { 0, 0 } 477 }; 478 479 devclass_t atapci_devclass; 480 481 static driver_t ata_pci_driver = { 482 "atapci", 483 ata_pci_methods, 484 sizeof(struct ata_pci_controller), 485 }; 486 487 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0); 488 MODULE_VERSION(atapci, 1); 489 MODULE_DEPEND(atapci, ata, 1, 1, 1); 490 491 static int 492 ata_pcichannel_probe(device_t dev) 493 { 494 struct ata_channel *ch = device_get_softc(dev); 495 device_t *children; 496 int count, i; 497 char buffer[32]; 498 499 /* take care of green memory */ 500 bzero(ch, sizeof(struct ata_channel)); 501 502 /* find channel number on this controller */ 503 device_get_children(device_get_parent(dev), &children, &count); 504 for (i = 0; i < count; i++) { 505 if (children[i] == dev) 506 ch->unit = i; 507 } 508 free(children, M_TEMP); 509 510 sprintf(buffer, "ATA channel %d", ch->unit); 511 device_set_desc_copy(dev, buffer); 512 513 return ata_probe(dev); 514 } 515 516 static int 517 ata_pcichannel_attach(device_t dev) 518 { 519 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 520 struct ata_channel *ch = device_get_softc(dev); 521 int error; 522 523 if (ctlr->r_res1) 524 ctlr->dmainit(ch); 525 if (ch->dma) 526 ch->dma->alloc(ch); 527 528 if ((error = ctlr->allocate(dev))) 529 return error; 530 531 return ata_attach(dev); 532 } 533 534 static int 535 ata_pcichannel_detach(device_t dev) 536 { 537 struct ata_channel *ch = device_get_softc(dev); 538 int error; 539 540 if ((error = ata_detach(dev))) 541 return error; 542 543 if (ch->dma) 544 ch->dma->free(ch); 545 546 /* free resources for io and ctlio XXX SOS */ 547 548 return 0; 549 } 550 551 static int 552 ata_pcichannel_locking(device_t dev, int mode) 553 { 554 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 555 struct ata_channel *ch = device_get_softc(dev); 556 557 if (ctlr->locking) 558 return ctlr->locking(ch, mode); 559 else 560 return ch->unit; 561 } 562 563 static void 564 ata_pcichannel_reset(device_t dev) 565 { 566 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 567 struct ata_channel *ch = device_get_softc(dev); 568 569 /* if DMA functionality present stop it */ 570 if (ch->dma) { 571 if (ch->dma->stop) 572 ch->dma->stop(ch); 573 if (ch->dma->flags & ATA_DMA_LOADED) 574 ch->dma->unload(ch); 575 } 576 577 /* reset the controller HW */ 578 if (ctlr->reset) 579 ctlr->reset(ch); 580 } 581 582 static void 583 ata_pcichannel_setmode(device_t parent, device_t dev) 584 { 585 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 586 struct ata_device *atadev = device_get_softc(dev); 587 int mode = atadev->mode; 588 589 ctlr->setmode(atadev, ATA_PIO_MAX); 590 if (mode >= ATA_DMA) 591 ctlr->setmode(atadev, mode); 592 } 593 594 static device_method_t ata_pcichannel_methods[] = { 595 /* device interface */ 596 DEVMETHOD(device_probe, ata_pcichannel_probe), 597 DEVMETHOD(device_attach, ata_pcichannel_attach), 598 DEVMETHOD(device_detach, ata_pcichannel_detach), 599 DEVMETHOD(device_shutdown, bus_generic_shutdown), 600 DEVMETHOD(device_suspend, ata_suspend), 601 DEVMETHOD(device_resume, ata_resume), 602 603 /* ATA methods */ 604 DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 605 DEVMETHOD(ata_locking, ata_pcichannel_locking), 606 DEVMETHOD(ata_reset, ata_pcichannel_reset), 607 608 { 0, 0 } 609 }; 610 611 driver_t ata_pcichannel_driver = { 612 "ata", 613 ata_pcichannel_methods, 614 sizeof(struct ata_channel), 615 }; 616 617 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0); 618