1 /*- 2 * Copyright (c) 1998,1999,2000 S�ren Schmidt 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 * $FreeBSD$ 29 */ 30 31 #include "ata.h" 32 #include "isa.h" 33 #include "card.h" 34 #include "pci.h" 35 #include "atadisk.h" 36 #include "atapicd.h" 37 #include "atapifd.h" 38 #include "atapist.h" 39 #include "opt_global.h" 40 #include "opt_ata.h" 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/disk.h> 45 #include <sys/module.h> 46 #include <sys/bus.h> 47 #include <sys/bio.h> 48 #include <sys/malloc.h> 49 #include <sys/devicestat.h> 50 #include <sys/sysctl.h> 51 #include <machine/stdarg.h> 52 #include <machine/resource.h> 53 #include <machine/bus.h> 54 #include <sys/rman.h> 55 #if NPCI > 0 56 #include <pci/pcivar.h> 57 #include <pci/pcireg.h> 58 #endif 59 #include <isa/isavar.h> 60 #include <isa/isareg.h> 61 #ifdef __alpha__ 62 #include <machine/md_var.h> 63 #endif 64 #include <dev/ata/ata-all.h> 65 #include <dev/ata/ata-disk.h> 66 #include <dev/ata/atapi-all.h> 67 68 /* misc defines */ 69 #define IOMASK 0xfffffffc 70 #define ATA_IOADDR_RID 0 71 #define ATA_ALTADDR_RID 1 72 #define ATA_BMADDR_RID 2 73 #if NPCI > 0 74 #define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \ 75 (pci_get_progif(dev) & 0x05) != 0x05) 76 #else 77 #define ATA_MASTERDEV(dev) (1) 78 #endif 79 80 /* prototypes */ 81 static int ata_probe(device_t); 82 static int ata_attach(device_t); 83 static int ata_detach(device_t); 84 static int ata_resume(device_t); 85 static void ata_boot_attach(void); 86 static void ata_intr(void *); 87 static int ata_getparam(struct ata_softc *, int, u_int8_t); 88 static int ata_service(struct ata_softc *); 89 static char *active2str(int); 90 static void bswap(int8_t *, int); 91 static void btrim(int8_t *, int); 92 static void bpack(int8_t *, int8_t *, int); 93 94 /* local vars */ 95 static devclass_t ata_devclass; 96 static devclass_t ata_pci_devclass; 97 static struct intr_config_hook *ata_delayed_attach = NULL; 98 static char ata_conf[256]; 99 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 100 101 #if NISA > 0 102 static struct isa_pnp_id ata_ids[] = { 103 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 104 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 105 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 106 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 107 {0} 108 }; 109 110 static int 111 ata_isa_probe(device_t dev) 112 { 113 struct ata_softc *scp = device_get_softc(dev); 114 struct resource *port; 115 int rid; 116 u_long tmp; 117 118 /* check isapnp ids */ 119 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 120 return ENXIO; 121 122 /* allocate the port range */ 123 rid = ATA_IOADDR_RID; 124 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 125 ATA_IOSIZE, RF_ACTIVE); 126 if (!port) 127 return ENOMEM; 128 129 /* alloctate the altport range */ 130 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) { 131 bus_set_resource(dev, SYS_RES_IOPORT, 1, 132 rman_get_start(port) + ATA_ALTOFFSET, 133 ATA_ALTIOSIZE); 134 } 135 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 136 scp->channel = 0; 137 scp->flags |= ATA_USE_16BIT; 138 return ata_probe(dev); 139 } 140 141 static device_method_t ata_isa_methods[] = { 142 /* device interface */ 143 DEVMETHOD(device_probe, ata_isa_probe), 144 DEVMETHOD(device_attach, ata_attach), 145 DEVMETHOD(device_resume, ata_resume), 146 { 0, 0 } 147 }; 148 149 static driver_t ata_isa_driver = { 150 "ata", 151 ata_isa_methods, 152 sizeof(struct ata_softc), 153 }; 154 155 DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 156 #endif 157 158 #if NCARD > 0 159 static int 160 ata_pccard_probe(device_t dev) 161 { 162 struct ata_softc *scp = device_get_softc(dev); 163 struct resource *port; 164 int rid, len; 165 166 /* allocate the port range */ 167 rid = ATA_IOADDR_RID; 168 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid); 169 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE); 170 if (!port) 171 return ENOMEM; 172 173 /* 174 * if we got more than the default ATA_IOSIZE ports, this is likely 175 * a pccard system where the altio ports are located just after the 176 * normal io ports, so no need to allocate them. 177 */ 178 if (len <= ATA_IOSIZE) { 179 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, 180 rman_get_start(port) + ATA_ALTOFFSET, ATA_ALTIOSIZE); 181 } 182 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 183 scp->channel = 0; 184 scp->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE); 185 return ata_probe(dev); 186 } 187 188 static device_method_t ata_pccard_methods[] = { 189 /* device interface */ 190 DEVMETHOD(device_probe, ata_pccard_probe), 191 DEVMETHOD(device_attach, ata_attach), 192 DEVMETHOD(device_detach, ata_detach), 193 { 0, 0 } 194 }; 195 196 static driver_t ata_pccard_driver = { 197 "ata", 198 ata_pccard_methods, 199 sizeof(struct ata_softc), 200 }; 201 202 DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0); 203 #endif 204 205 #if NPCI > 0 206 struct ata_pci_softc { 207 struct resource *bmio; 208 struct resource bmio_1; 209 struct resource bmio_2; 210 struct resource *irq; 211 int irqcnt; 212 }; 213 214 int 215 ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid) 216 { 217 device_t *children, child; 218 int nchildren, i; 219 220 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 221 return 0; 222 223 for (i = 0; i < nchildren; i++) { 224 child = children[i]; 225 226 /* check that it's on the same silicon and the device we want */ 227 if (pci_get_slot(dev) == pci_get_slot(child) && 228 pci_get_vendor(child) == (type & 0xffff) && 229 pci_get_device(child) == ((type & 0xffff0000) >> 16) && 230 pci_get_revid(child) >= revid) { 231 free(children, M_TEMP); 232 return 1; 233 } 234 } 235 free(children, M_TEMP); 236 return 0; 237 } 238 239 static const char * 240 ata_pci_match(device_t dev) 241 { 242 if (pci_get_class(dev) != PCIC_STORAGE) 243 return NULL; 244 245 switch (pci_get_devid(dev)) { 246 /* supported chipsets */ 247 case 0x12308086: 248 return "Intel PIIX ATA controller"; 249 250 case 0x70108086: 251 return "Intel PIIX3 ATA controller"; 252 253 case 0x71118086: 254 case 0x71998086: 255 return "Intel PIIX4 ATA33 controller"; 256 257 case 0x24218086: 258 return "Intel ICH0 ATA33 controller"; 259 260 case 0x24118086: 261 return "Intel ICH ATA66 controller"; 262 263 case 0x244b8086: 264 return "Intel ICH2 ATA100 controller"; 265 266 case 0x522910b9: 267 return "AcerLabs Aladdin ATA33 controller"; 268 269 case 0x05711106: 270 if (ata_find_dev(dev, 0x05861106, 0)) 271 return "VIA 82C586 ATA33 controller"; 272 if (ata_find_dev(dev, 0x05961106, 0x12)) 273 return "VIA 82C596 ATA66 controller"; 274 if (ata_find_dev(dev, 0x05961106, 0)) 275 return "VIA 82C596 ATA33 controller"; 276 if (ata_find_dev(dev, 0x06861106, 0)) 277 return "VIA 82C686 ATA66 controller"; 278 return "VIA Apollo ATA controller"; 279 280 case 0x55131039: 281 return "SiS 5591 ATA33 controller"; 282 283 case 0x06491095: 284 return "CMD 649 ATA100 controller"; 285 286 case 0x06481095: 287 return "CMD 648 ATA66 controller"; 288 289 case 0x06461095: 290 return "CMD 646 ATA controller"; 291 292 case 0xc6931080: 293 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE) 294 return "Cypress 82C693 ATA controller"; 295 break; 296 297 case 0x01021078: 298 return "Cyrix 5530 ATA33 controller"; 299 300 case 0x74091022: 301 return "AMD 756 ATA66 controller"; 302 303 case 0x02111166: 304 return "ServerWorks ROSB4 ATA33 controller"; 305 306 case 0x4d33105a: 307 return "Promise ATA33 controller"; 308 309 case 0x4d38105a: 310 return "Promise ATA66 controller"; 311 312 case 0x0d30105a: 313 case 0x4d30105a: 314 return "Promise ATA100 controller"; 315 316 case 0x00041103: 317 switch (pci_get_revid(dev)) { 318 case 0x00: 319 case 0x01: 320 return "HighPoint HPT366 ATA66 controller"; 321 case 0x02: 322 return "HighPoint HPT368 ATA66 controller"; 323 case 0x03: 324 case 0x04: 325 return "HighPoint HPT370 ATA100 controller"; 326 default: 327 return "Unknown revision HighPoint ATA controller"; 328 } 329 330 /* unsupported but known chipsets, generic DMA only */ 331 case 0x10001042: 332 case 0x10011042: 333 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible"; 334 335 case 0x06401095: 336 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible"; 337 338 /* unknown chipsets, try generic DMA if it seems possible */ 339 default: 340 if (pci_get_class(dev) == PCIC_STORAGE && 341 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 342 return "Generic PCI ATA controller"; 343 } 344 return NULL; 345 } 346 347 static int 348 ata_pci_probe(device_t dev) 349 { 350 const char *desc = ata_pci_match(dev); 351 352 if (desc) { 353 device_set_desc(dev, desc); 354 return 0; 355 } 356 else 357 return ENXIO; 358 } 359 360 static int 361 ata_pci_add_child(device_t dev, int unit) 362 { 363 device_t child; 364 365 /* check if this is located at one of the std addresses */ 366 if (ATA_MASTERDEV(dev)) { 367 if (!(child = device_add_child(dev, "ata", unit))) 368 return ENOMEM; 369 } 370 else { 371 if (!(child = device_add_child(dev, "ata", 2))) 372 return ENOMEM; 373 } 374 return 0; 375 } 376 377 static int 378 ata_pci_attach(device_t dev) 379 { 380 struct ata_pci_softc *sc = device_get_softc(dev); 381 u_int8_t class, subclass; 382 u_int32_t type, cmd; 383 int rid; 384 385 /* set up vendor-specific stuff */ 386 type = pci_get_devid(dev); 387 class = pci_get_class(dev); 388 subclass = pci_get_subclass(dev); 389 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 390 391 /* is busmastering supported ? */ 392 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 393 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) { 394 395 /* is there a valid port range to connect to ? */ 396 rid = 0x20; 397 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 398 0, ~0, 1, RF_ACTIVE); 399 if (!sc->bmio) 400 device_printf(dev, "Busmastering DMA not configured\n"); 401 } 402 else 403 device_printf(dev, "Busmastering DMA not supported\n"); 404 405 /* do extra chipset specific setups */ 406 switch (type) { 407 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */ 408 pci_write_config(dev, 0x53, 409 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 410 break; 411 412 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */ 413 case 0x4d30105a: 414 case 0x0d30105a: 415 outb(rman_get_start(sc->bmio) + 0x11, 416 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a); 417 /* FALLTHROUGH */ 418 419 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */ 420 outb(rman_get_start(sc->bmio) + 0x1f, 421 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01); 422 break; 423 424 case 0x00041103: /* HighPoint */ 425 switch (pci_get_revid(dev)) { 426 case 0x00: 427 case 0x01: 428 /* turn off interrupt prediction */ 429 pci_write_config(dev, 0x51, 430 (pci_read_config(dev, 0x51, 1) & ~0x80), 1); 431 break; 432 433 case 0x02: 434 case 0x03: 435 case 0x04: 436 /* turn off interrupt prediction */ 437 pci_write_config(dev, 0x51, 438 (pci_read_config(dev, 0x51, 1) & ~0x02), 1); 439 pci_write_config(dev, 0x55, 440 (pci_read_config(dev, 0x55, 1) & ~0x02), 1); 441 /* turn on interrupts */ 442 pci_write_config(dev, 0x5a, 443 (pci_read_config(dev, 0x5a, 1) & ~0x10), 1); 444 445 } 446 break; 447 448 case 0x05711106: 449 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */ 450 /* set prefetch, postwrite */ 451 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 452 453 /* set fifo configuration half'n'half */ 454 pci_write_config(dev, 0x43, 455 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 456 457 /* set status register read retry */ 458 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 459 460 /* set DMA read & end-of-sector fifo flush */ 461 pci_write_config(dev, 0x46, 462 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 463 464 /* set sector size */ 465 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 466 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 467 468 /* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */ 469 if (ata_find_dev(dev, 0x06861106, 0) || 470 ata_find_dev(dev, 0x05961106, 0x12)) { 471 pci_write_config(dev, 0x50, 472 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4); 473 } 474 break; 475 476 case 0x10001042: /* RZ 100? known bad, no DMA */ 477 case 0x10011042: 478 case 0x06401095: /* CMD 640 known bad, no DMA */ 479 sc->bmio = 0x0; 480 device_printf(dev, "Busmastering DMA disabled\n"); 481 } 482 483 /* 484 * the Cypress chip is a mess, it contains two ATA functions, but 485 * both channels are visible on the first one. 486 * simply ignore the second function for now, as the right 487 * solution (ignoring the second channel on the first function) 488 * doesn't work with the crappy ATA interrupt setup on the alpha. 489 */ 490 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1) 491 return 0; 492 493 ata_pci_add_child(dev, 0); 494 495 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK) 496 ata_pci_add_child(dev, 1); 497 498 return bus_generic_attach(dev); 499 } 500 501 static int 502 ata_pci_print_child(device_t dev, device_t child) 503 { 504 struct ata_softc *scp = device_get_softc(child); 505 int retval = 0; 506 507 retval += bus_print_child_header(dev, child); 508 retval += printf(": at 0x%x", scp->ioaddr); 509 510 if (ATA_MASTERDEV(dev)) 511 retval += printf(" irq %d", 14 + scp->channel); 512 513 retval += bus_print_child_footer(dev, child); 514 515 return retval; 516 } 517 518 static struct resource * 519 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 520 u_long start, u_long end, u_long count, u_int flags) 521 { 522 struct ata_pci_softc *sc = device_get_softc(dev); 523 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 524 int myrid; 525 526 if (type == SYS_RES_IOPORT) { 527 switch (*rid) { 528 case ATA_IOADDR_RID: 529 if (ATA_MASTERDEV(dev)) { 530 myrid = 0; 531 start = (channel == 0 ? IO_WD1 : IO_WD2); 532 end = start + ATA_IOSIZE - 1; 533 count = ATA_IOSIZE; 534 } 535 else 536 myrid = 0x10 + 8 * channel; 537 break; 538 539 case ATA_ALTADDR_RID: 540 if (ATA_MASTERDEV(dev)) { 541 myrid = 0; 542 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET; 543 end = start + ATA_ALTIOSIZE - 1; 544 count = ATA_ALTIOSIZE; 545 } 546 else 547 myrid = 0x14 + 8 * channel; 548 break; 549 550 case ATA_BMADDR_RID: 551 /* the busmaster resource is shared between the two channels */ 552 if (sc->bmio) { 553 if (channel == 0) { 554 sc->bmio_1 = *sc->bmio; 555 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1; 556 return &sc->bmio_1; 557 } else { 558 sc->bmio_2 = *sc->bmio; 559 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1; 560 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1; 561 return &sc->bmio_2; 562 } 563 } 564 return 0; 565 566 default: 567 return 0; 568 } 569 570 if (ATA_MASTERDEV(dev)) 571 /* make the parent just pass through the allocation. */ 572 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 573 SYS_RES_IOPORT, &myrid, 574 start, end, count, flags); 575 else 576 /* we are using the parent resource directly. */ 577 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 578 SYS_RES_IOPORT, &myrid, 579 start, end, count, flags); 580 } 581 582 if (type == SYS_RES_IRQ) { 583 if (*rid != 0) 584 return 0; 585 586 if (ATA_MASTERDEV(dev)) { 587 #ifdef __alpha__ 588 return alpha_platform_alloc_ide_intr(channel); 589 #else 590 int irq = (channel == 0 ? 14 : 15); 591 592 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 593 SYS_RES_IRQ, rid, 594 irq, irq, 1, flags & ~RF_SHAREABLE); 595 #endif 596 } else { 597 /* primary and secondary channels share the same interrupt */ 598 sc->irqcnt++; 599 if (!sc->irq) 600 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 601 SYS_RES_IRQ, rid, 0, ~0, 1, flags); 602 return sc->irq; 603 } 604 } 605 return 0; 606 } 607 608 static int 609 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 610 struct resource *r) 611 { 612 struct ata_pci_softc *sc = device_get_softc(dev); 613 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 614 int myrid = 0; 615 616 if (type == SYS_RES_IOPORT) { 617 switch (rid) { 618 case ATA_IOADDR_RID: 619 if (ATA_MASTERDEV(dev)) 620 myrid = 0; 621 else 622 myrid = 0x10 + 8 * channel; 623 break; 624 625 case ATA_ALTADDR_RID: 626 if (ATA_MASTERDEV(dev)) 627 myrid = 0; 628 else 629 myrid = 0x14 + 8 * channel; 630 break; 631 632 case ATA_BMADDR_RID: 633 return 0; 634 635 default: 636 return ENOENT; 637 } 638 639 if (ATA_MASTERDEV(dev)) 640 /* make the parent just pass through the allocation. */ 641 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 642 SYS_RES_IOPORT, myrid, r); 643 else 644 /* we are using the parent resource directly. */ 645 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 646 SYS_RES_IOPORT, myrid, r); 647 } 648 if (type == SYS_RES_IRQ) { 649 if (rid != 0) 650 return ENOENT; 651 652 if (ATA_MASTERDEV(dev)) { 653 #ifdef __alpha__ 654 return alpha_platform_release_ide_intr(channel, r); 655 #else 656 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 657 child, SYS_RES_IRQ, rid, r); 658 #endif 659 } 660 else { 661 if (--sc->irqcnt) 662 return 0; 663 664 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 665 dev, SYS_RES_IRQ, rid, r); 666 } 667 } 668 return EINVAL; 669 } 670 671 static int 672 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 673 int flags, driver_intr_t *intr, void *arg, 674 void **cookiep) 675 { 676 if (ATA_MASTERDEV(dev)) { 677 #ifdef __alpha__ 678 return alpha_platform_setup_ide_intr(child, irq, intr, arg, cookiep); 679 #else 680 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 681 flags, intr, arg, cookiep); 682 #endif 683 } 684 else 685 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq, 686 flags, intr, arg, cookiep); 687 } 688 689 static int 690 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 691 void *cookie) 692 { 693 if (ATA_MASTERDEV(dev)) { 694 #ifdef __alpha__ 695 return alpha_platform_teardown_ide_intr(child, irq, cookie); 696 #else 697 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 698 #endif 699 } 700 else 701 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie); 702 } 703 704 static device_method_t ata_pci_methods[] = { 705 /* device interface */ 706 DEVMETHOD(device_probe, ata_pci_probe), 707 DEVMETHOD(device_attach, ata_pci_attach), 708 DEVMETHOD(device_shutdown, bus_generic_shutdown), 709 DEVMETHOD(device_suspend, bus_generic_suspend), 710 DEVMETHOD(device_resume, bus_generic_resume), 711 712 /* bus methods */ 713 DEVMETHOD(bus_print_child, ata_pci_print_child), 714 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 715 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 716 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 717 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 718 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 719 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 720 { 0, 0 } 721 }; 722 723 static driver_t ata_pci_driver = { 724 "atapci", 725 ata_pci_methods, 726 sizeof(struct ata_pci_softc), 727 }; 728 729 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 730 731 static int 732 ata_pcisub_probe(device_t dev) 733 { 734 struct ata_softc *scp = device_get_softc(dev); 735 device_t *list; 736 int count, i; 737 738 /* find channel number on this controller */ 739 device_get_children(device_get_parent(dev), &list, &count); 740 for (i = 0; i < count; i++) { 741 if (list[i] == dev) 742 scp->channel = i; 743 } 744 745 scp->chiptype = pci_get_devid(device_get_parent(dev)); 746 747 return ata_probe(dev); 748 } 749 750 static device_method_t ata_pcisub_methods[] = { 751 /* device interface */ 752 DEVMETHOD(device_probe, ata_pcisub_probe), 753 DEVMETHOD(device_attach, ata_attach), 754 DEVMETHOD(device_detach, ata_detach), 755 DEVMETHOD(device_resume, ata_resume), 756 { 0, 0 } 757 }; 758 759 static driver_t ata_pcisub_driver = { 760 "ata", 761 ata_pcisub_methods, 762 sizeof(struct ata_softc), 763 }; 764 765 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0); 766 #endif 767 768 static int 769 ata_probe(device_t dev) 770 { 771 struct ata_softc *scp = device_get_softc(dev); 772 struct resource *io = 0; 773 struct resource *altio = 0; 774 struct resource *bmio = 0; 775 int rid; 776 u_int32_t ioaddr, altioaddr, bmaddr; 777 int mask = 0; 778 u_int8_t status0, status1; 779 780 if (!scp || scp->flags & ATA_ATTACHED) 781 return ENXIO; 782 783 /* initialize the softc basics */ 784 scp->active = ATA_IDLE; 785 scp->dev = dev; 786 scp->devices = 0; 787 788 rid = ATA_IOADDR_RID; 789 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 790 ATA_IOSIZE, RF_ACTIVE); 791 if (!io) 792 goto failure; 793 ioaddr = rman_get_start(io); 794 795 rid = ATA_ALTADDR_RID; 796 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 797 ATA_ALTIOSIZE, RF_ACTIVE); 798 if (altio) { 799 if (scp->flags & ATA_USE_16BIT || ATA_MASTERDEV(device_get_parent(dev))) 800 altioaddr = rman_get_start(altio); 801 else 802 altioaddr = rman_get_start(altio) + 0x02; 803 } 804 else 805 altioaddr = ioaddr + ATA_PCCARD_ALTOFFSET; 806 807 rid = ATA_BMADDR_RID; 808 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 809 bmaddr = bmio ? rman_get_start(bmio) : 0; 810 811 /* store the IO resources for eventual later release */ 812 scp->r_io = io; 813 scp->r_altio = altio; 814 scp->r_bmio = bmio; 815 816 /* store the physical IO addresse for easy access */ 817 scp->ioaddr = ioaddr; 818 scp->altioaddr = altioaddr; 819 scp->bmaddr = bmaddr; 820 821 if (bootverbose) 822 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 823 scp->ioaddr, scp->altioaddr, scp->bmaddr); 824 825 /* do we have any signs of ATA/ATAPI HW being present ? */ 826 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 827 DELAY(1); 828 status0 = inb(scp->ioaddr + ATA_STATUS); 829 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5) 830 mask |= 0x01; 831 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 832 DELAY(1); 833 status1 = inb(scp->ioaddr + ATA_STATUS); 834 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5) 835 mask |= 0x02; 836 837 if (bootverbose) 838 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 839 mask, status0, status1); 840 if (!mask) 841 goto failure; 842 843 ata_reset(scp, &mask); 844 845 if (!mask) 846 goto failure; 847 848 TAILQ_INIT(&scp->ata_queue); 849 TAILQ_INIT(&scp->atapi_queue); 850 return 0; 851 852 failure: 853 if (io) 854 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 855 if (altio) 856 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio); 857 if (bmio) 858 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio); 859 if (bootverbose) 860 ata_printf(scp, -1, "probe allocation failed\n"); 861 return ENXIO; 862 } 863 864 static int 865 ata_attach(device_t dev) 866 { 867 struct ata_softc *scp = device_get_softc(dev); 868 int error, rid = 0; 869 870 if (!scp || scp->flags & ATA_ATTACHED) 871 return ENXIO; 872 873 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 874 RF_SHAREABLE | RF_ACTIVE); 875 if (!scp->r_irq) { 876 ata_printf(scp, -1, "unable to allocate interrupt\n"); 877 return ENXIO; 878 } 879 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr, 880 scp, &scp->ih))) 881 return error; 882 883 /* 884 * do not attach devices if we are in early boot, this is done later 885 * when interrupts are enabled by a hook into the boot process. 886 * otherwise attach what the probe has found in scp->devices. 887 */ 888 if (!ata_delayed_attach) { 889 if (scp->devices & ATA_ATA_SLAVE) 890 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 891 scp->devices &= ~ATA_ATA_SLAVE; 892 if (scp->devices & ATA_ATAPI_SLAVE) 893 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 894 scp->devices &= ~ATA_ATAPI_SLAVE; 895 if (scp->devices & ATA_ATA_MASTER) 896 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 897 scp->devices &= ~ATA_ATA_MASTER; 898 if (scp->devices & ATA_ATAPI_MASTER) 899 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 900 scp->devices &= ~ATA_ATAPI_MASTER; 901 #if NATADISK > 0 902 if (scp->devices & ATA_ATA_MASTER) 903 ad_attach(scp, ATA_MASTER); 904 if (scp->devices & ATA_ATA_SLAVE) 905 ad_attach(scp, ATA_SLAVE); 906 #endif 907 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 908 if (scp->devices & ATA_ATAPI_MASTER) 909 atapi_attach(scp, ATA_MASTER); 910 if (scp->devices & ATA_ATAPI_SLAVE) 911 atapi_attach(scp, ATA_SLAVE); 912 #endif 913 } 914 scp->flags |= ATA_ATTACHED; 915 return 0; 916 } 917 918 static int 919 ata_detach(device_t dev) 920 { 921 struct ata_softc *scp = device_get_softc(dev); 922 923 if (!scp || !(scp->flags & ATA_ATTACHED)) 924 return ENXIO; 925 926 #if NATADISK > 0 927 if (scp->devices & ATA_ATA_MASTER) 928 ad_detach(scp->dev_softc[0]); 929 if (scp->devices & ATA_ATA_SLAVE) 930 ad_detach(scp->dev_softc[1]); 931 #endif 932 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 933 if (scp->devices & ATA_ATAPI_MASTER) 934 atapi_detach(scp->dev_softc[0]); 935 if (scp->devices & ATA_ATAPI_SLAVE) 936 atapi_detach(scp->dev_softc[1]); 937 #endif 938 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) { 939 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA); 940 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL; 941 } 942 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) { 943 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA); 944 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL; 945 } 946 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL; 947 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL; 948 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO; 949 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO; 950 bus_teardown_intr(dev, scp->r_irq, scp->ih); 951 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq); 952 if (scp->r_bmio) 953 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio); 954 if (scp->r_altio) 955 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio); 956 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io); 957 scp->flags &= ~ATA_ATTACHED; 958 return 0; 959 } 960 961 static int 962 ata_resume(device_t dev) 963 { 964 struct ata_softc *scp = device_get_softc(dev); 965 966 ata_reinit(scp); 967 return 0; 968 } 969 970 static int 971 ata_getparam(struct ata_softc *scp, int device, u_int8_t command) 972 { 973 struct ata_params *ata_parm; 974 int8_t buffer[DEV_BSIZE]; 975 int retry = 0; 976 977 /* select drive */ 978 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 979 DELAY(1); 980 981 /* enable interrupt */ 982 outb(scp->altioaddr, ATA_A_4BIT); 983 DELAY(1); 984 985 /* apparently some devices needs this repeated */ 986 do { 987 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) { 988 ata_printf(scp, device, "identify failed\n"); 989 return -1; 990 } 991 if (retry++ > 4) { 992 ata_printf(scp, device, "identify retries exceeded\n"); 993 return -1; 994 } 995 } while (ata_wait(scp, device, 996 ((command == ATA_C_ATAPI_IDENTIFY) ? 997 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)))); 998 999 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t)); 1000 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 1001 if (!ata_parm) { 1002 ata_printf(scp, device, "malloc for identify data failed\n"); 1003 return -1; 1004 } 1005 bcopy(buffer, ata_parm, sizeof(struct ata_params)); 1006 if (command == ATA_C_ATA_IDENTIFY || 1007 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 1008 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X'))) 1009 bswap(ata_parm->model, sizeof(ata_parm->model)); 1010 btrim(ata_parm->model, sizeof(ata_parm->model)); 1011 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 1012 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 1013 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 1014 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 1015 scp->dev_param[ATA_DEV(device)] = ata_parm; 1016 return 0; 1017 } 1018 1019 static void 1020 ata_boot_attach(void) 1021 { 1022 struct ata_softc *scp; 1023 int ctlr; 1024 1025 /* 1026 * run through all ata devices and look for real ATA & ATAPI devices 1027 * using the hints we found in the early probe, this avoids some of 1028 * the delays probing of non-exsistent devices can cause. 1029 */ 1030 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1031 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1032 continue; 1033 if (scp->devices & ATA_ATA_SLAVE) 1034 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 1035 scp->devices &= ~ATA_ATA_SLAVE; 1036 if (scp->devices & ATA_ATAPI_SLAVE) 1037 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 1038 scp->devices &= ~ATA_ATAPI_SLAVE; 1039 if (scp->devices & ATA_ATA_MASTER) 1040 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 1041 scp->devices &= ~ATA_ATA_MASTER; 1042 if (scp->devices & ATA_ATAPI_MASTER) 1043 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 1044 scp->devices &= ~ATA_ATAPI_MASTER; 1045 } 1046 1047 #if NATADISK > 0 1048 /* now we know whats there, do the real attach, first the ATA disks */ 1049 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1050 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1051 continue; 1052 if (scp->devices & ATA_ATA_MASTER) 1053 ad_attach(scp, ATA_MASTER); 1054 if (scp->devices & ATA_ATA_SLAVE) 1055 ad_attach(scp, ATA_SLAVE); 1056 } 1057 #endif 1058 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1059 /* then the atapi devices */ 1060 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1061 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1062 continue; 1063 if (scp->devices & ATA_ATAPI_MASTER) 1064 atapi_attach(scp, ATA_MASTER); 1065 if (scp->devices & ATA_ATAPI_SLAVE) 1066 atapi_attach(scp, ATA_SLAVE); 1067 } 1068 #endif 1069 if (ata_delayed_attach) { 1070 config_intrhook_disestablish(ata_delayed_attach); 1071 free(ata_delayed_attach, M_ATA); 1072 ata_delayed_attach = NULL; 1073 } 1074 } 1075 1076 static void 1077 ata_intr(void *data) 1078 { 1079 struct ata_softc *scp = (struct ata_softc *)data; 1080 struct ata_pci_softc *sc = device_get_softc(device_get_parent(scp->dev)); 1081 u_int8_t dmastat = 0; 1082 1083 /* 1084 * since we might share the IRQ with another device, and in some 1085 * cases with our twin channel, we only want to process interrupts 1086 * that we know this channel generated. 1087 */ 1088 switch (scp->chiptype) { 1089 #if NPCI > 0 1090 case 0x00041103: /* HighPoint HPT366/368/370 */ 1091 if (((dmastat = ata_dmastatus(scp)) & 1092 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT) 1093 return; 1094 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1095 break; 1096 1097 case 0x06481095: /* CMD 648 */ 1098 case 0x06491095: /* CMD 649 */ 1099 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) & 1100 (scp->channel ? 0x08 : 0x04))) 1101 return; 1102 goto out; 1103 1104 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */ 1105 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */ 1106 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */ 1107 case 0x0d30105a: /* Promise OEM ATA100 */ 1108 if (!(inl(rman_get_start(sc->bmio) + 0x1c) & 1109 (scp->channel ? 0x00004000 : 0x00000400))) 1110 return; 1111 /* FALLTHROUGH */ 1112 out: 1113 #endif 1114 default: 1115 if (scp->flags & ATA_DMA_ACTIVE) { 1116 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)) 1117 return; 1118 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1119 } 1120 } 1121 DELAY(1); 1122 1123 /* if drive is busy it didn't interrupt */ 1124 if (inb(scp->altioaddr) & ATA_S_BUSY) 1125 return; 1126 1127 /* clear interrupt and get status */ 1128 scp->status = inb(scp->ioaddr + ATA_STATUS); 1129 1130 if (scp->status & ATA_S_ERROR) 1131 scp->error = inb(scp->ioaddr + ATA_ERROR); 1132 1133 /* find & call the responsible driver to process this interrupt */ 1134 switch (scp->active) { 1135 #if NATADISK > 0 1136 case ATA_ACTIVE_ATA: 1137 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES) 1138 return; 1139 break; 1140 #endif 1141 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1142 case ATA_ACTIVE_ATAPI: 1143 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 1144 return; 1145 break; 1146 #endif 1147 case ATA_WAIT_INTR: 1148 wakeup((caddr_t)scp); 1149 break; 1150 1151 case ATA_WAIT_READY: 1152 break; 1153 1154 case ATA_REINITING: 1155 return; 1156 1157 case ATA_IDLE: 1158 if (scp->flags & ATA_QUEUED) { 1159 scp->active = ATA_ACTIVE; 1160 if (ata_service(scp) == ATA_OP_CONTINUES) 1161 return; 1162 } 1163 /* FALLTHROUGH */ 1164 1165 default: 1166 #ifdef ATA_DEBUG 1167 { 1168 static int intr_count = 0; 1169 1170 if (intr_count++ < 10) 1171 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n", 1172 intr_count, scp->status); 1173 } 1174 #endif 1175 } 1176 scp->active = ATA_IDLE; 1177 scp->running = NULL; 1178 ata_start(scp); 1179 return; 1180 } 1181 1182 void 1183 ata_start(struct ata_softc *scp) 1184 { 1185 #if NATADISK > 0 1186 struct ad_request *ad_request; 1187 #endif 1188 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1189 struct atapi_request *atapi_request; 1190 #endif 1191 1192 if (scp->active != ATA_IDLE) 1193 return; 1194 1195 scp->active = ATA_ACTIVE; 1196 1197 #if NATADISK > 0 1198 /* find & call the responsible driver if anything on the ATA queue */ 1199 if (TAILQ_EMPTY(&scp->ata_queue)) { 1200 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1201 ad_start((struct ad_softc *)scp->dev_softc[0]); 1202 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1203 ad_start((struct ad_softc *)scp->dev_softc[1]); 1204 } 1205 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 1206 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 1207 scp->active = ATA_ACTIVE_ATA; 1208 scp->running = ad_request; 1209 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) 1210 return; 1211 } 1212 1213 #endif 1214 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1215 /* find & call the responsible driver if anything on the ATAPI queue */ 1216 if (TAILQ_EMPTY(&scp->atapi_queue)) { 1217 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1218 atapi_start((struct atapi_softc *)scp->dev_softc[0]); 1219 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1220 atapi_start((struct atapi_softc *)scp->dev_softc[1]); 1221 } 1222 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 1223 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 1224 scp->active = ATA_ACTIVE_ATAPI; 1225 scp->running = atapi_request; 1226 atapi_transfer(atapi_request); 1227 return; 1228 } 1229 #endif 1230 scp->active = ATA_IDLE; 1231 } 1232 1233 void 1234 ata_reset(struct ata_softc *scp, int *mask) 1235 { 1236 int timeout; 1237 u_int8_t a, b, ostat0, ostat1; 1238 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY; 1239 1240 /* get the current status of the devices */ 1241 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 1242 DELAY(10); 1243 ostat1 = inb(scp->ioaddr + ATA_STATUS); 1244 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1245 DELAY(10); 1246 ostat0 = inb(scp->ioaddr + ATA_STATUS); 1247 1248 /* in some setups we dont want to test for a slave */ 1249 if (scp->flags & ATA_NO_SLAVE) 1250 *mask &= ~0x02; 1251 1252 if (bootverbose) 1253 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n", 1254 *mask, ostat0, ostat1); 1255 1256 /* reset channel */ 1257 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 1258 DELAY(10000); 1259 outb(scp->altioaddr, ATA_A_IDS); 1260 DELAY(100000); 1261 inb(scp->ioaddr + ATA_ERROR); 1262 scp->devices = 0; 1263 1264 /* wait for BUSY to go inactive */ 1265 for (timeout = 0; timeout < 310000; timeout++) { 1266 if (status0 & ATA_S_BUSY) { 1267 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1268 DELAY(10); 1269 status0 = inb(scp->ioaddr + ATA_STATUS); 1270 if (!(status0 & ATA_S_BUSY)) { 1271 /* check for ATAPI signature while its still there */ 1272 a = inb(scp->ioaddr + ATA_CYL_LSB); 1273 b = inb(scp->ioaddr + ATA_CYL_MSB); 1274 if (bootverbose) 1275 ata_printf(scp, ATA_MASTER, 1276 "ATAPI probe a=%02x b=%02x\n", a, b); 1277 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB) 1278 scp->devices |= ATA_ATAPI_MASTER; 1279 } 1280 } 1281 if (status1 & ATA_S_BUSY) { 1282 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 1283 DELAY(10); 1284 status1 = inb(scp->ioaddr + ATA_STATUS); 1285 if (!(status1 & ATA_S_BUSY)) { 1286 /* check for ATAPI signature while its still there */ 1287 a = inb(scp->ioaddr + ATA_CYL_LSB); 1288 b = inb(scp->ioaddr + ATA_CYL_MSB); 1289 if (bootverbose) 1290 ata_printf(scp, ATA_SLAVE, 1291 "ATAPI probe a=%02x b=%02x\n", a, b); 1292 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB) 1293 scp->devices |= ATA_ATAPI_SLAVE; 1294 } 1295 } 1296 if (*mask == 0x01) /* wait for master only */ 1297 if (!(status0 & ATA_S_BUSY)) 1298 break; 1299 if (*mask == 0x02) /* wait for slave only */ 1300 if (!(status1 & ATA_S_BUSY)) 1301 break; 1302 if (*mask == 0x03) /* wait for both master & slave */ 1303 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 1304 break; 1305 DELAY(100); 1306 } 1307 DELAY(10); 1308 outb(scp->altioaddr, ATA_A_4BIT); 1309 1310 if (status0 & ATA_S_BUSY) 1311 *mask &= ~0x01; 1312 if (status1 & ATA_S_BUSY) 1313 *mask &= ~0x02; 1314 if (bootverbose) 1315 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 1316 *mask, status0, status1); 1317 if (!*mask) 1318 return; 1319 1320 if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 1321 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1322 DELAY(10); 1323 outb(scp->ioaddr + ATA_ERROR, 0x58); 1324 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1325 a = inb(scp->ioaddr + ATA_ERROR); 1326 b = inb(scp->ioaddr + ATA_CYL_LSB); 1327 if (bootverbose) 1328 ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b); 1329 if (a != 0x58 && b == 0xa5) 1330 scp->devices |= ATA_ATA_MASTER; 1331 } 1332 if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 1333 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 1334 DELAY(10); 1335 outb(scp->ioaddr + ATA_ERROR, 0x58); 1336 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1337 a = inb(scp->ioaddr + ATA_ERROR); 1338 b = inb(scp->ioaddr + ATA_CYL_LSB); 1339 if (bootverbose) 1340 ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b); 1341 if (a != 0x58 && b == 0xa5) 1342 scp->devices |= ATA_ATA_SLAVE; 1343 } 1344 if (bootverbose) 1345 ata_printf(scp, -1, "devices=%02x\n", scp->devices); 1346 } 1347 1348 int 1349 ata_reinit(struct ata_softc *scp) 1350 { 1351 int mask = 0, omask; 1352 1353 scp->active = ATA_REINITING; 1354 scp->running = NULL; 1355 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 1356 mask |= 0x01; 1357 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 1358 mask |= 0x02; 1359 if (mask) { 1360 omask = mask; 1361 ata_printf(scp, -1, "resetting devices .. "); 1362 ata_reset(scp, &mask); 1363 if (omask != mask) 1364 printf(" device dissapeared! %d ", omask & ~mask); 1365 1366 #if NATADISK > 0 1367 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1368 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 1369 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1370 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 1371 #endif 1372 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1373 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1374 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 1375 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1376 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 1377 #endif 1378 printf("done\n"); 1379 } 1380 scp->active = ATA_IDLE; 1381 ata_start(scp); 1382 return 0; 1383 } 1384 1385 static int 1386 ata_service(struct ata_softc *scp) 1387 { 1388 /* do we have a SERVICE request from the drive ? */ 1389 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){ 1390 outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT); 1391 #if NATADISK > 0 1392 if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 1393 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0]) 1394 return ad_service((struct ad_softc *)scp->dev_softc[0], 0); 1395 } 1396 else { 1397 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1]) 1398 return ad_service((struct ad_softc *)scp->dev_softc[1], 0); 1399 } 1400 #endif 1401 } 1402 return ATA_OP_FINISHED; 1403 } 1404 1405 int 1406 ata_wait(struct ata_softc *scp, int device, u_int8_t mask) 1407 { 1408 int timeout = 0; 1409 int statio = scp->ioaddr + ATA_STATUS; 1410 1411 DELAY(1); 1412 while (timeout < 5000000) { /* timeout 5 secs */ 1413 scp->status = inb(statio); 1414 1415 /* if drive fails status, reselect the drive just to be sure */ 1416 if (scp->status == 0xff) { 1417 ata_printf(scp, device, "no status, reselecting device\n"); 1418 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1419 DELAY(1); 1420 scp->status = inb(statio); 1421 } 1422 1423 /* are we done ? */ 1424 if (!(scp->status & ATA_S_BUSY)) 1425 break; 1426 1427 if (timeout > 1000) { 1428 timeout += 1000; 1429 DELAY(1000); 1430 } 1431 else { 1432 timeout += 10; 1433 DELAY(10); 1434 } 1435 } 1436 if (scp->status & ATA_S_ERROR) 1437 scp->error = inb(scp->ioaddr + ATA_ERROR); 1438 if (timeout >= 5000000) 1439 return -1; 1440 if (!mask) 1441 return (scp->status & ATA_S_ERROR); 1442 1443 /* Wait 50 msec for bits wanted. */ 1444 timeout = 5000; 1445 while (timeout--) { 1446 scp->status = inb(statio); 1447 if ((scp->status & mask) == mask) { 1448 if (scp->status & ATA_S_ERROR) 1449 scp->error = inb(scp->ioaddr + ATA_ERROR); 1450 return (scp->status & ATA_S_ERROR); 1451 } 1452 DELAY (10); 1453 } 1454 return -1; 1455 } 1456 1457 int 1458 ata_command(struct ata_softc *scp, int device, u_int8_t command, 1459 u_int16_t cylinder, u_int8_t head, u_int8_t sector, 1460 u_int8_t count, u_int8_t feature, int flags) 1461 { 1462 int error = 0; 1463 #ifdef ATA_DEBUG 1464 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, " 1465 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n", 1466 scp->ioaddr, command, cylinder, head, sector, 1467 count, feature, flags); 1468 #endif 1469 1470 /* disable interrupt from device */ 1471 if (scp->flags & ATA_QUEUED) 1472 outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT); 1473 1474 /* select device */ 1475 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1476 1477 /* ready to issue command ? */ 1478 if (ata_wait(scp, device, 0) < 0) { 1479 ata_printf(scp, device, 1480 "timeout waiting to give command=%02x s=%02x e=%02x\n", 1481 command, scp->status, scp->error); 1482 return -1; 1483 } 1484 1485 outb(scp->ioaddr + ATA_FEATURE, feature); 1486 outb(scp->ioaddr + ATA_COUNT, count); 1487 outb(scp->ioaddr + ATA_SECTOR, sector); 1488 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 1489 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 1490 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 1491 1492 switch (flags) { 1493 case ATA_WAIT_INTR: 1494 scp->active = ATA_WAIT_INTR; 1495 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz); 1496 outb(scp->ioaddr + ATA_CMD, command); 1497 1498 /* enable interrupt */ 1499 if (scp->flags & ATA_QUEUED) 1500 outb(scp->altioaddr, ATA_A_4BIT); 1501 1502 if (await(PRIBIO, 10 * hz)) { 1503 ata_printf(scp, device, "ata_command: timeout waiting for intr\n"); 1504 scp->active = ATA_IDLE; 1505 error = -1; 1506 } 1507 break; 1508 1509 case ATA_WAIT_READY: 1510 if (scp->active != ATA_REINITING) 1511 scp->active = ATA_WAIT_READY; 1512 outb(scp->ioaddr + ATA_CMD, command); 1513 if (ata_wait(scp, device, ATA_S_READY) < 0) { 1514 ata_printf(scp, device, 1515 "timeout waiting for command=%02x s=%02x e=%02x\n", 1516 command, scp->status, scp->error); 1517 error = -1; 1518 } 1519 if (scp->active != ATA_REINITING) 1520 scp->active = ATA_IDLE; 1521 break; 1522 1523 case ATA_IMMEDIATE: 1524 outb(scp->ioaddr + ATA_CMD, command); 1525 break; 1526 1527 default: 1528 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n", 1529 active2str(flags)); 1530 } 1531 /* enable interrupt */ 1532 if (scp->flags & ATA_QUEUED) 1533 outb(scp->altioaddr, ATA_A_4BIT); 1534 return error; 1535 } 1536 1537 int 1538 ata_get_lun(u_int32_t *map) 1539 { 1540 int lun = ffs(~*map) - 1; 1541 1542 *map |= (1 << lun); 1543 return lun; 1544 } 1545 1546 void 1547 ata_free_lun(u_int32_t *map, int lun) 1548 { 1549 *map &= ~(1 << lun); 1550 } 1551 1552 int 1553 ata_printf(struct ata_softc *scp, int device, const char * fmt, ...) 1554 { 1555 va_list ap; 1556 int ret; 1557 1558 if (device == -1) 1559 ret = printf("ata%d: ", device_get_unit(scp->dev)); 1560 else 1561 ret = printf("ata%d-%s: ", device_get_unit(scp->dev), 1562 (device == ATA_MASTER) ? "master" : "slave"); 1563 va_start(ap, fmt); 1564 ret += vprintf(fmt, ap); 1565 va_end(ap); 1566 return ret; 1567 } 1568 1569 char * 1570 ata_mode2str(int mode) 1571 { 1572 switch (mode) { 1573 case ATA_PIO: return "BIOSPIO"; 1574 case ATA_PIO0: return "PIO0"; 1575 case ATA_PIO1: return "PIO1"; 1576 case ATA_PIO2: return "PIO2"; 1577 case ATA_PIO3: return "PIO3"; 1578 case ATA_PIO4: return "PIO4"; 1579 case ATA_WDMA2: return "WDMA2"; 1580 case ATA_UDMA2: return "UDMA33"; 1581 case ATA_UDMA4: return "UDMA66"; 1582 case ATA_UDMA5: return "UDMA100"; 1583 case ATA_DMA: return "BIOSDMA"; 1584 default: return "???"; 1585 } 1586 } 1587 1588 int 1589 ata_pio2mode(int pio) 1590 { 1591 switch (pio) { 1592 default: 1593 case 0: return ATA_PIO0; 1594 case 1: return ATA_PIO1; 1595 case 2: return ATA_PIO2; 1596 case 3: return ATA_PIO3; 1597 case 4: return ATA_PIO4; 1598 } 1599 } 1600 1601 int 1602 ata_pmode(struct ata_params *ap) 1603 { 1604 if (ap->atavalid & ATA_FLAG_64_70) { 1605 if (ap->apiomodes & 2) 1606 return 4; 1607 if (ap->apiomodes & 1) 1608 return 3; 1609 } 1610 if (ap->opiomode == 2) 1611 return 2; 1612 if (ap->opiomode == 1) 1613 return 1; 1614 if (ap->opiomode == 0) 1615 return 0; 1616 return -1; 1617 } 1618 1619 int 1620 ata_wmode(struct ata_params *ap) 1621 { 1622 if (ap->wdmamodes & 4) 1623 return 2; 1624 if (ap->wdmamodes & 2) 1625 return 1; 1626 if (ap->wdmamodes & 1) 1627 return 0; 1628 return -1; 1629 } 1630 1631 int 1632 ata_umode(struct ata_params *ap) 1633 { 1634 if (ap->atavalid & ATA_FLAG_88) { 1635 if (ap->udmamodes & 0x20) 1636 return 5; 1637 if (ap->udmamodes & 0x10) 1638 return 4; 1639 if (ap->udmamodes & 0x08) 1640 return 3; 1641 if (ap->udmamodes & 0x04) 1642 return 2; 1643 if (ap->udmamodes & 0x02) 1644 return 1; 1645 if (ap->udmamodes & 0x01) 1646 return 0; 1647 } 1648 return -1; 1649 } 1650 1651 static char * 1652 active2str(int active) 1653 { 1654 static char buf[8]; 1655 1656 switch (active) { 1657 case ATA_IDLE: 1658 return("ATA_IDLE"); 1659 case ATA_IMMEDIATE: 1660 return("ATA_IMMEDIATE"); 1661 case ATA_WAIT_INTR: 1662 return("ATA_WAIT_INTR"); 1663 case ATA_WAIT_READY: 1664 return("ATA_WAIT_READY"); 1665 case ATA_ACTIVE: 1666 return("ATA_ACTIVE"); 1667 case ATA_ACTIVE_ATA: 1668 return("ATA_ACTIVE_ATA"); 1669 case ATA_ACTIVE_ATAPI: 1670 return("ATA_ACTIVE_ATAPI"); 1671 case ATA_REINITING: 1672 return("ATA_REINITING"); 1673 default: 1674 sprintf(buf, "0x%02x", active); 1675 return buf; 1676 } 1677 } 1678 1679 static void 1680 bswap(int8_t *buf, int len) 1681 { 1682 u_int16_t *ptr = (u_int16_t*)(buf + len); 1683 1684 while (--ptr >= (u_int16_t*)buf) 1685 *ptr = ntohs(*ptr); 1686 } 1687 1688 static void 1689 btrim(int8_t *buf, int len) 1690 { 1691 int8_t *ptr; 1692 1693 for (ptr = buf; ptr < buf+len; ++ptr) 1694 if (!*ptr) 1695 *ptr = ' '; 1696 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1697 *ptr = 0; 1698 } 1699 1700 static void 1701 bpack(int8_t *src, int8_t *dst, int len) 1702 { 1703 int i, j, blank; 1704 1705 for (i = j = blank = 0 ; i < len; i++) { 1706 if (blank && src[i] == ' ') continue; 1707 if (blank && src[i] != ' ') { 1708 dst[j++] = src[i]; 1709 blank = 0; 1710 continue; 1711 } 1712 if (src[i] == ' ') { 1713 blank = 1; 1714 if (i == 0) 1715 continue; 1716 } 1717 dst[j++] = src[i]; 1718 } 1719 if (j < len) 1720 dst[j] = 0x00; 1721 } 1722 1723 static void 1724 ata_change_mode(struct ata_softc *scp, int device, int mode) 1725 { 1726 int s = splbio(); 1727 1728 while (scp->active != ATA_IDLE) 1729 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 1730 scp->active = ATA_REINITING; 1731 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)), 1732 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)), 1733 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device))); 1734 scp->active = ATA_IDLE; 1735 ata_start(scp); 1736 splx(s); 1737 } 1738 1739 static int 1740 sysctl_hw_ata(SYSCTL_HANDLER_ARGS) 1741 { 1742 struct ata_softc *scp; 1743 int ctlr, error, i; 1744 1745 /* readout internal state */ 1746 bzero(ata_conf, sizeof(ata_conf)); 1747 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1748 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1749 continue; 1750 for (i = 0; i < 2; i++) { 1751 if (!scp->dev_softc[i]) 1752 strcat(ata_conf, "---,"); 1753 else if (scp->mode[i] >= ATA_DMA) 1754 strcat(ata_conf, "dma,"); 1755 else 1756 strcat(ata_conf, "pio,"); 1757 } 1758 } 1759 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req); 1760 if (error == 0 && req->newptr != NULL) { 1761 char *ptr = ata_conf; 1762 1763 /* update internal state */ 1764 i = 0; 1765 while (*ptr) { 1766 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) { 1767 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1768 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA) 1769 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO); 1770 } 1771 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) { 1772 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1773 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA) 1774 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA); 1775 } 1776 else if (strncmp(ptr, "---", 3)) 1777 break; 1778 ptr+=3; 1779 if (*ptr++ != ',' || 1780 ++i > (devclass_get_maxunit(ata_devclass) << 1)) 1781 break; 1782 } 1783 } 1784 return error; 1785 } 1786 SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW, 1787 0, sizeof(ata_conf), sysctl_hw_ata, "A", ""); 1788 1789 static void 1790 ata_init(void) 1791 { 1792 /* register boot attach to be run when interrupts are enabled */ 1793 if (!(ata_delayed_attach = (struct intr_config_hook *) 1794 malloc(sizeof(struct intr_config_hook), 1795 M_TEMP, M_NOWAIT))) { 1796 printf("ata: malloc of delayed attach hook failed\n"); 1797 return; 1798 } 1799 bzero(ata_delayed_attach, sizeof(struct intr_config_hook)); 1800 1801 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1802 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1803 printf("ata: config_intrhook_establish failed\n"); 1804 free(ata_delayed_attach, M_TEMP); 1805 } 1806 } 1807 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1808