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