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