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