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