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