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