29 */ 30 31#include "ata.h" 32#include "apm.h" 33#include "isa.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/buf.h> 48#include <sys/malloc.h> 49#include <sys/devicestat.h> 50#include <vm/vm.h> 51#include <vm/pmap.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 __i386__ 63#include <machine/smp.h> 64#include <i386/isa/intr_machdep.h> 65#endif 66#if NAPM > 0 67#include <machine/apm_bios.h> 68#endif 69#include <dev/ata/ata-all.h> 70#include <dev/ata/ata-disk.h> 71#include <dev/ata/atapi-all.h> 72 73/* misc defines */ 74#if SMP == 0 75#define isa_apic_irq(x) x 76#endif 77#define IOMASK 0xfffffffc /* XXX SOS 0xfffc */ 78 79/* prototypes */ 80static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 81static void ataintr(void *); 82static int8_t *active2str(int32_t); 83 84/* local vars */ 85static int32_t atanlun = 2; 86struct ata_softc *atadevices[MAXATA]; 87static devclass_t ata_devclass; 88MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 89 90#if NISA > 0 91static struct isa_pnp_id ata_ids[] = { 92 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 93 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 94 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 95 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 96 {0} 97}; 98 99static int 100ata_isaprobe(device_t dev) 101{ 102 struct resource *port; 103 int rid; 104 int32_t ctlr, res; 105 int32_t lun; 106 107 /* Check isapnp ids */ 108 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 109 return (ENXIO); 110 111 /* Allocate the port range */ 112 rid = 0; 113 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 114 if (!port) 115 return (ENOMEM); 116 117 /* check if allready in use by a PCI device */ 118 for (ctlr = 0; ctlr < atanlun; ctlr++) { 119 if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){ 120 printf("ata-isa%d: already registered as ata%d\n", 121 device_get_unit(dev), ctlr); 122 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 123 return ENXIO; 124 } 125 } 126 127 lun = 0; 128 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 129 0, dev, &lun); 130 131 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 132 133 if (res) { 134 isa_set_portsize(dev, res); 135 *(int *)device_get_softc(dev) = lun; 136 return 0; 137 } 138 return ENXIO; 139} 140 141static int 142ata_isaattach(device_t dev) 143{ 144 struct resource *port; 145 struct resource *irq; 146 void *ih; 147 int rid; 148 149 /* Allocate the port range and interrupt */ 150 rid = 0; 151 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 152 if (!port) 153 return (ENOMEM); 154 155 rid = 0; 156 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 157 if (!irq) { 158 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 159 return (ENOMEM); 160 } 161 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 162 atadevices[*(int *)device_get_softc(dev)], &ih); 163} 164 165static device_method_t ata_isa_methods[] = { 166 /* Device interface */ 167 DEVMETHOD(device_probe, ata_isaprobe), 168 DEVMETHOD(device_attach, ata_isaattach), 169 { 0, 0 } 170}; 171 172static driver_t ata_isa_driver = { 173 "ata", 174 ata_isa_methods, 175 sizeof(int), 176}; 177 178DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 179#endif 180 181#if NPCI > 0 182static const char * 183ata_pcimatch(device_t dev) 184{ 185 if (pci_get_class(dev) != PCIC_STORAGE) 186 return NULL; 187 188 switch (pci_get_devid(dev)) { 189 /* supported chipsets */ 190 case 0x12308086: 191 return "Intel PIIX ATA controller"; 192 case 0x70108086: 193 return "Intel PIIX3 ATA controller"; 194 case 0x71118086: 195 return "Intel PIIX4 ATA controller"; 196 case 0x522910b9: 197 return "AcerLabs Aladdin ATA controller"; 198 case 0x05711106: /* 82c586 & 82c686 */
| 29 */ 30 31#include "ata.h" 32#include "apm.h" 33#include "isa.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/buf.h> 48#include <sys/malloc.h> 49#include <sys/devicestat.h> 50#include <vm/vm.h> 51#include <vm/pmap.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 __i386__ 63#include <machine/smp.h> 64#include <i386/isa/intr_machdep.h> 65#endif 66#if NAPM > 0 67#include <machine/apm_bios.h> 68#endif 69#include <dev/ata/ata-all.h> 70#include <dev/ata/ata-disk.h> 71#include <dev/ata/atapi-all.h> 72 73/* misc defines */ 74#if SMP == 0 75#define isa_apic_irq(x) x 76#endif 77#define IOMASK 0xfffffffc /* XXX SOS 0xfffc */ 78 79/* prototypes */ 80static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 81static void ataintr(void *); 82static int8_t *active2str(int32_t); 83 84/* local vars */ 85static int32_t atanlun = 2; 86struct ata_softc *atadevices[MAXATA]; 87static devclass_t ata_devclass; 88MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 89 90#if NISA > 0 91static struct isa_pnp_id ata_ids[] = { 92 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 93 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 94 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 95 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 96 {0} 97}; 98 99static int 100ata_isaprobe(device_t dev) 101{ 102 struct resource *port; 103 int rid; 104 int32_t ctlr, res; 105 int32_t lun; 106 107 /* Check isapnp ids */ 108 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 109 return (ENXIO); 110 111 /* Allocate the port range */ 112 rid = 0; 113 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 114 if (!port) 115 return (ENOMEM); 116 117 /* check if allready in use by a PCI device */ 118 for (ctlr = 0; ctlr < atanlun; ctlr++) { 119 if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){ 120 printf("ata-isa%d: already registered as ata%d\n", 121 device_get_unit(dev), ctlr); 122 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 123 return ENXIO; 124 } 125 } 126 127 lun = 0; 128 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 129 0, dev, &lun); 130 131 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 132 133 if (res) { 134 isa_set_portsize(dev, res); 135 *(int *)device_get_softc(dev) = lun; 136 return 0; 137 } 138 return ENXIO; 139} 140 141static int 142ata_isaattach(device_t dev) 143{ 144 struct resource *port; 145 struct resource *irq; 146 void *ih; 147 int rid; 148 149 /* Allocate the port range and interrupt */ 150 rid = 0; 151 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 152 if (!port) 153 return (ENOMEM); 154 155 rid = 0; 156 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 157 if (!irq) { 158 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 159 return (ENOMEM); 160 } 161 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 162 atadevices[*(int *)device_get_softc(dev)], &ih); 163} 164 165static device_method_t ata_isa_methods[] = { 166 /* Device interface */ 167 DEVMETHOD(device_probe, ata_isaprobe), 168 DEVMETHOD(device_attach, ata_isaattach), 169 { 0, 0 } 170}; 171 172static driver_t ata_isa_driver = { 173 "ata", 174 ata_isa_methods, 175 sizeof(int), 176}; 177 178DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 179#endif 180 181#if NPCI > 0 182static const char * 183ata_pcimatch(device_t dev) 184{ 185 if (pci_get_class(dev) != PCIC_STORAGE) 186 return NULL; 187 188 switch (pci_get_devid(dev)) { 189 /* supported chipsets */ 190 case 0x12308086: 191 return "Intel PIIX ATA controller"; 192 case 0x70108086: 193 return "Intel PIIX3 ATA controller"; 194 case 0x71118086: 195 return "Intel PIIX4 ATA controller"; 196 case 0x522910b9: 197 return "AcerLabs Aladdin ATA controller"; 198 case 0x05711106: /* 82c586 & 82c686 */
|
318 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 319 } 320 } 321 322 /* do extra chipset specific setups */ 323 switch (type) { 324 case 0x522910b9: 325 /* on the Aladdin activate the ATAPI FIFO */ 326 pci_write_config(dev, 0x53, 327 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 328 break; 329 330 case 0x4d33105a: 331 case 0x4d38105a: 332 /* the Promise's need burst mode to be turned on explicitly */ 333 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 334 break; 335 336 case 0x05711106: 337 /* the VIA Apollo needs some sensible defaults */ 338 /* set prefetch, postwrite */ 339 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 340 341 /* set fifo configuration half'n'half */ 342 pci_write_config(dev, 0x43, 343 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 344 345 /* set status register read retry */ 346 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 347 348 /* set DMA read & end-of-sector fifo flush */ 349 pci_write_config(dev, 0x46, 350 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 351 352 /* set sector size */ 353 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 354 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 355 break; 356 } 357 358 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 359 lun = 0; 360 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 361 scp = atadevices[lun]; 362 if (iobase_1 == IO_WD1) 363#ifdef __i386__ 364 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 365 &bio_imask, INTR_EXCL); 366#endif 367#ifdef __alpha__ 368 alpha_platform_setup_ide_intr(0, ataintr, scp); 369#endif 370 else { 371 int rid = 0; 372 void *ih; 373 374 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 375 RF_SHAREABLE | RF_ACTIVE))) 376 printf("ata_pciattach: Unable to alloc interrupt\n"); 377 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 378 } 379 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 380 lun, iobase_1, isa_apic_irq(irq1), unit); 381 } 382 lun = 1; 383 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 384 scp = atadevices[lun]; 385 if (iobase_2 == IO_WD2) 386#ifdef __i386__ 387 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 388 &bio_imask, INTR_EXCL); 389#endif 390#ifdef __alpha__ 391 alpha_platform_setup_ide_intr(1, ataintr, scp); 392#endif 393 else { 394 int rid = 0; 395 void *ih; 396 397 if (irq1 != irq2 || irq == NULL) { 398 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 399 RF_SHAREABLE | RF_ACTIVE))) 400 printf("ata_pciattach: Unable to alloc interrupt\n"); 401 } 402 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 403 } 404 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 405 lun, iobase_2, isa_apic_irq(irq2), unit); 406 } 407 return 0; 408} 409 410static device_method_t ata_pci_methods[] = { 411 /* Device interface */ 412 DEVMETHOD(device_probe, ata_pciprobe), 413 DEVMETHOD(device_attach, ata_pciattach), 414 { 0, 0 } 415}; 416 417static driver_t ata_pci_driver = { 418 "ata-pci", 419 ata_pci_methods, 420 sizeof(int), 421}; 422 423DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 424#endif 425 426static int32_t 427ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 428 device_t dev, int32_t *unit) 429{ 430 struct ata_softc *scp; 431 int32_t lun, mask = 0; 432 u_int8_t status0, status1; 433 434 if (atanlun > MAXATA) { 435 printf("ata: unit out of range(%d)\n", atanlun); 436 return 0; 437 } 438 439 /* check if this is located at one of the std addresses */ 440 if (ioaddr == IO_WD1) 441 lun = 0; 442 else if (ioaddr == IO_WD2) 443 lun = 1; 444 else 445 lun = atanlun++; 446 447 if ((scp = atadevices[lun])) { 448 printf("ata%d: unit already attached\n", lun); 449 return 0; 450 } 451 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 452 if (scp == NULL) { 453 printf("ata%d: failed to allocate driver storage\n", lun); 454 return 0; 455 } 456 bzero(scp, sizeof(struct ata_softc)); 457 458 scp->ioaddr = ioaddr; 459 scp->altioaddr = altioaddr; 460 scp->lun = lun; 461 scp->unit = *unit; 462 scp->active = ATA_IDLE; 463 464 if (bootverbose) 465 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 466 scp->lun, scp->ioaddr, scp->altioaddr); 467 468 469 /* do we have any signs of ATA/ATAPI HW being present ? */ 470 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 471 DELAY(1); 472 status0 = inb(scp->ioaddr + ATA_STATUS); 473 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 474 DELAY(1); 475 status1 = inb(scp->ioaddr + ATA_STATUS); 476 if ((status0 & 0xf8) != 0xf8) 477 mask |= 0x01; 478 if ((status1 & 0xf8) != 0xf8) 479 mask |= 0x02; 480 if (bootverbose) 481 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 482 scp->lun, mask, status0, status1); 483 if (!mask) { 484 free(scp, M_DEVBUF); 485 return 0; 486 } 487 ata_reset(scp, &mask); 488 if (!mask) { 489 free(scp, M_DEVBUF); 490 return 0; 491 } 492 /* 493 * OK, we have at least one device on the chain, 494 * check for ATAPI signatures, if none check if its 495 * a good old ATA device. 496 */ 497 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 498 DELAY(1); 499 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 500 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 501 scp->devices |= ATA_ATAPI_MASTER; 502 } 503 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 504 DELAY(1); 505 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 506 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 507 scp->devices |= ATA_ATAPI_SLAVE; 508 } 509 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 510 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 511 DELAY(1); 512 outb(scp->ioaddr + ATA_ERROR, 0x58); 513 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 514 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 515 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 516 scp->devices |= ATA_ATA_MASTER; 517 } 518 } 519 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 520 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 521 DELAY(1); 522 outb(scp->ioaddr + ATA_ERROR, 0x58); 523 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 524 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 525 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 526 scp->devices |= ATA_ATA_SLAVE; 527 } 528 } 529 if (bootverbose) 530 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 531 if (!scp->devices) { 532 free(scp, M_DEVBUF); 533 return 0; 534 } 535 TAILQ_INIT(&scp->ata_queue); 536 TAILQ_INIT(&scp->atapi_queue); 537 *unit = scp->lun; 538 scp->dev = dev; 539 if (bmaddr) 540 scp->bmaddr = bmaddr; 541 atadevices[scp->lun] = scp; 542#if NAPM > 0 543 scp->resume_hook.ah_fun = (void *)ata_reinit; 544 scp->resume_hook.ah_arg = scp; 545 scp->resume_hook.ah_name = "ATA driver"; 546 scp->resume_hook.ah_order = APM_MID_ORDER; 547 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 548#endif 549 return ATA_IOSIZE; 550} 551 552static void 553ataintr(void *data) 554{ 555 struct ata_softc *scp =(struct ata_softc *)data; 556 557 /* is this interrupt really for this channel */ 558 if ((scp->flags & ATA_DMA_ACTIVE) && 559 !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 560 return; 561 562 if (((scp->status = inb(scp->ioaddr+ATA_STATUS)) & ATA_S_BUSY)==ATA_S_BUSY) 563 return; 564 565 /* find & call the responsible driver to process this interrupt */ 566 switch (scp->active) { 567#if NATADISK > 0 568 case ATA_ACTIVE_ATA: 569 if (!scp->running) 570 return; 571 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 572 return; 573 break; 574#endif 575#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 576 case ATA_ACTIVE_ATAPI: 577 if (!scp->running) 578 return; 579 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 580 return; 581 break; 582#endif 583 case ATA_WAIT_INTR: 584 wakeup((caddr_t)scp); 585 break; 586 587 case ATA_WAIT_READY: 588 break; 589 590 case ATA_REINITING: 591 return; 592 593 default: 594 case ATA_IDLE: 595#ifdef ATA_DEBUG 596 { 597 static int32_t intr_count = 0; 598 if (intr_count++ < 10) 599 printf("ata%d: unwanted interrupt %d status = %02x\n", 600 scp->lun, intr_count, scp->status); 601 } 602#endif 603 return; 604 } 605 scp->active = ATA_IDLE; 606 scp->running = NULL; 607 ata_start(scp); 608} 609 610void 611ata_start(struct ata_softc *scp) 612{ 613 struct ad_request *ad_request; 614 struct atapi_request *atapi_request; 615 616#ifdef ATA_DEBUG 617 printf("ata_start: entered\n"); 618#endif 619 if (scp->active != ATA_IDLE) 620 return; 621 622#if NATADISK > 0 623 /* find & call the responsible driver if anything on the ATA queue */ 624 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 625 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 626 scp->active = ATA_ACTIVE_ATA; 627 scp->running = ad_request; 628 ad_transfer(ad_request); 629#ifdef ATA_DEBUG 630 printf("ata_start: started ata, leaving\n"); 631#endif 632 return; 633 } 634#endif 635#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 636 /* 637 * find & call the responsible driver if anything on the ATAPI queue. 638 * check for device busy by polling the DSC bit, if busy, check 639 * for requests to the other device on the channel (if any). 640 * if the other device is an ATA disk it already had its chance above. 641 * if no request can be served, timeout a call to ata_start. 642 */ 643 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 644 struct atapi_softc *atp = atapi_request->device; 645 static int32_t interval = 1; 646 647 if (atp->flags & ATAPI_F_DSC_USED) { 648 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 649 DELAY(1); 650 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 651 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 652 if (atapi_request->device->unit != atp->unit) { 653 struct atapi_softc *tmp = atapi_request->device; 654 655 outb(tmp->controller->ioaddr + ATA_DRIVE, 656 ATA_D_IBM | tmp->unit); 657 DELAY(1); 658 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 659 atapi_request = NULL; 660 break; 661 } 662 } 663 } 664 if (!atapi_request) { 665 timeout((timeout_t *)ata_start, atp->controller, interval++); 666 return; 667 } 668 else 669 interval = 1; 670 } 671 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 672 scp->active = ATA_ACTIVE_ATAPI; 673 scp->running = atapi_request; 674 atapi_transfer(atapi_request); 675#ifdef ATA_DEBUG 676 printf("ata_start: started atapi, leaving\n"); 677#endif 678 return; 679 } 680#endif 681} 682 683void 684ata_reset(struct ata_softc *scp, int32_t *mask) 685{ 686 int32_t timeout; 687 int8_t status0, status1; 688 689 /* reset channel */ 690 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 691 DELAY(1); 692 inb(scp->ioaddr + ATA_STATUS); 693 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 694 DELAY(10000); 695 outb(scp->altioaddr, ATA_A_IDS); 696 DELAY(10000); 697 inb(scp->ioaddr + ATA_ERROR); 698 DELAY(3000); 699 700 /* wait for BUSY to go inactive */ 701 for (timeout = 0; timeout < 310000; timeout++) { 702 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 703 DELAY(1); 704 status0 = inb(scp->ioaddr + ATA_STATUS); 705 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 706 DELAY(1); 707 status1 = inb(scp->ioaddr + ATA_STATUS); 708 if (*mask == 0x01) /* wait for master only */ 709 if (!(status0 & ATA_S_BUSY)) 710 break; 711 if (*mask == 0x02) /* wait for slave only */ 712 if (!(status1 & ATA_S_BUSY)) 713 break; 714 if (*mask == 0x03) /* wait for both master & slave */ 715 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 716 break; 717 DELAY(100); 718 } 719 DELAY(1); 720 outb(scp->altioaddr, ATA_A_4BIT); 721 if (status0 & ATA_S_BUSY) 722 *mask &= ~0x01; 723 if (status1 & ATA_S_BUSY) 724 *mask &= ~0x02; 725 if (bootverbose) 726 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 727 scp->lun, *mask, status0, status1); 728} 729 730int32_t 731ata_reinit(struct ata_softc *scp) 732{ 733 int32_t mask = 0, omask; 734 735 scp->active = ATA_REINITING; 736 scp->running = NULL; 737 printf("ata%d: resetting devices .. ", scp->lun); 738 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 739 mask |= 0x01; 740 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 741 mask |= 0x02; 742 omask = mask; 743 ata_reset(scp, &mask); 744 if (omask != mask) 745 printf(" device dissapeared! %d ", omask & ~mask); 746 747#if NATADISK > 0 748 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 749 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 750 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 751 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 752#endif 753#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 754 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 755 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 756 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 757 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 758#endif 759 printf("done\n"); 760 scp->active = ATA_IDLE; 761 ata_start(scp); 762 return 0; 763} 764 765int32_t 766ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 767{ 768 u_int8_t status; 769 u_int32_t timeout = 0; 770 771 DELAY(1); 772 while (timeout <= 5000000) { /* timeout 5 secs */ 773 status = inb(scp->ioaddr + ATA_STATUS); 774 775 /* if drive fails status, reselect the drive just to be sure */ 776 if (status == 0xff) { 777 printf("ata%d: %s: no status, reselecting device\n", 778 scp->lun, device?"slave":"master"); 779 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 780 DELAY(1); 781 status = inb(scp->ioaddr + ATA_STATUS); 782 } 783 if (status == 0xff) 784 return -1; 785 scp->status = status; 786 if (!(status & ATA_S_BUSY)) { 787 if (status & ATA_S_ERROR) 788 scp->error = inb(scp->ioaddr + ATA_ERROR); 789 if ((status & mask) == mask) 790 return (status & ATA_S_ERROR); 791 } 792 if (timeout > 1000) { 793 timeout += 1000; 794 DELAY(1000); 795 } 796 else { 797 timeout += 10; 798 DELAY(10); 799 } 800 } 801 return -1; 802} 803 804int32_t 805ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 806 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 807 u_int32_t count, u_int32_t feature, int32_t flags) 808{ 809#ifdef ATA_DEBUG 810 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 811 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 812 scp->lun, scp->ioaddr, device, command, 813 cylinder, head, sector, count, flags); 814#endif 815 816 /* ready to issue command ? */ 817 if (ata_wait(scp, device, 0) < 0) { 818 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 819 scp->lun, device ? "slave" : "master", command, 820 scp->status, scp->error); 821 return -1; 822 } 823 outb(scp->ioaddr + ATA_FEATURE, feature); 824 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 825 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 826 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 827 outb(scp->ioaddr + ATA_SECTOR, sector); 828 outb(scp->ioaddr + ATA_COUNT, count); 829 830 switch (flags) { 831 case ATA_WAIT_INTR: 832 if (scp->active != ATA_IDLE) 833 printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active)); 834 scp->active = ATA_WAIT_INTR; 835 outb(scp->ioaddr + ATA_CMD, command); 836 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 837 printf("ata_command: timeout waiting for interrupt\n"); 838 scp->active = ATA_IDLE; 839 return -1; 840 } 841 break; 842 843 case ATA_WAIT_READY: 844 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 845 printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active)); 846 scp->active = ATA_WAIT_READY; 847 outb(scp->ioaddr + ATA_CMD, command); 848 if (ata_wait(scp, device, ATA_S_READY) < 0) { 849 printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n", 850 scp->lun, device ? "slave" : "master", command, 851 scp->status, scp->error); 852 scp->active = ATA_IDLE; 853 return -1; 854 } 855 scp->active = ATA_IDLE; 856 break; 857 858 case ATA_IMMEDIATE: 859 outb(scp->ioaddr + ATA_CMD, command); 860 break; 861 862 default: 863 printf("DANGER: illegal interrupt flag=%s\n", active2str(flags)); 864 } 865#ifdef ATA_DEBUG 866 printf("ata_command: leaving\n"); 867#endif 868 return 0; 869} 870 871int8_t * 872ata_mode2str(int32_t mode) 873{ 874 switch (mode) { 875 case ATA_MODE_PIO: 876 return "PIO"; 877 case ATA_MODE_WDMA2: 878 return "DMA"; 879 case ATA_MODE_UDMA2: 880 return "UDMA33"; 881 case ATA_MODE_UDMA3: 882 return "UDMA3"; 883 case ATA_MODE_UDMA4: 884 return "UDMA66"; 885 default: 886 return "???"; 887 } 888} 889 890static int8_t * 891active2str(int32_t active) 892{ 893 switch (active) { 894 case ATA_IDLE: 895 return("ATA_IDLE"); 896 case ATA_WAIT_INTR: 897 return("ATA_WAIT_INTR"); 898 case ATA_ACTIVE_ATA: 899 return("ATA_ACTIVE_ATA"); 900 case ATA_ACTIVE_ATAPI: 901 return("ATA_ACTIVE_ATAPI"); 902 case ATA_REINITING: 903 return("ATA_REINITING"); 904 default: 905 return("UNKNOWN"); 906 } 907} 908 909void 910bswap(int8_t *buf, int32_t len) 911{ 912 u_int16_t *p = (u_int16_t*)(buf + len); 913 914 while (--p >= (u_int16_t*)buf) 915 *p = ntohs(*p); 916} 917 918void 919btrim(int8_t *buf, int32_t len) 920{ 921 int8_t *p; 922 923 for (p = buf; p < buf+len; ++p) 924 if (!*p) 925 *p = ' '; 926 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 927 *p = 0; 928} 929 930void 931bpack(int8_t *src, int8_t *dst, int32_t len) 932{ 933 int32_t i, j, blank; 934 935 for (i = j = blank = 0 ; i < len-1; i++) { 936 if (blank && src[i] == ' ') continue; 937 if (blank && src[i] != ' ') { 938 dst[j++] = src[i]; 939 blank = 0; 940 continue; 941 } 942 if (src[i] == ' ') { 943 blank = 1; 944 if (i == 0) 945 continue; 946 } 947 dst[j++] = src[i]; 948 } 949 dst[j] = 0x00; 950}
| 315 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 316 } 317 } 318 319 /* do extra chipset specific setups */ 320 switch (type) { 321 case 0x522910b9: 322 /* on the Aladdin activate the ATAPI FIFO */ 323 pci_write_config(dev, 0x53, 324 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 325 break; 326 327 case 0x4d33105a: 328 case 0x4d38105a: 329 /* the Promise's need burst mode to be turned on explicitly */ 330 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 331 break; 332 333 case 0x05711106: 334 /* the VIA Apollo needs some sensible defaults */ 335 /* set prefetch, postwrite */ 336 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 337 338 /* set fifo configuration half'n'half */ 339 pci_write_config(dev, 0x43, 340 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 341 342 /* set status register read retry */ 343 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 344 345 /* set DMA read & end-of-sector fifo flush */ 346 pci_write_config(dev, 0x46, 347 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 348 349 /* set sector size */ 350 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 351 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 352 break; 353 } 354 355 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 356 lun = 0; 357 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 358 scp = atadevices[lun]; 359 if (iobase_1 == IO_WD1) 360#ifdef __i386__ 361 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 362 &bio_imask, INTR_EXCL); 363#endif 364#ifdef __alpha__ 365 alpha_platform_setup_ide_intr(0, ataintr, scp); 366#endif 367 else { 368 int rid = 0; 369 void *ih; 370 371 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 372 RF_SHAREABLE | RF_ACTIVE))) 373 printf("ata_pciattach: Unable to alloc interrupt\n"); 374 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 375 } 376 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 377 lun, iobase_1, isa_apic_irq(irq1), unit); 378 } 379 lun = 1; 380 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 381 scp = atadevices[lun]; 382 if (iobase_2 == IO_WD2) 383#ifdef __i386__ 384 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 385 &bio_imask, INTR_EXCL); 386#endif 387#ifdef __alpha__ 388 alpha_platform_setup_ide_intr(1, ataintr, scp); 389#endif 390 else { 391 int rid = 0; 392 void *ih; 393 394 if (irq1 != irq2 || irq == NULL) { 395 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 396 RF_SHAREABLE | RF_ACTIVE))) 397 printf("ata_pciattach: Unable to alloc interrupt\n"); 398 } 399 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 400 } 401 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 402 lun, iobase_2, isa_apic_irq(irq2), unit); 403 } 404 return 0; 405} 406 407static device_method_t ata_pci_methods[] = { 408 /* Device interface */ 409 DEVMETHOD(device_probe, ata_pciprobe), 410 DEVMETHOD(device_attach, ata_pciattach), 411 { 0, 0 } 412}; 413 414static driver_t ata_pci_driver = { 415 "ata-pci", 416 ata_pci_methods, 417 sizeof(int), 418}; 419 420DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 421#endif 422 423static int32_t 424ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 425 device_t dev, int32_t *unit) 426{ 427 struct ata_softc *scp; 428 int32_t lun, mask = 0; 429 u_int8_t status0, status1; 430 431 if (atanlun > MAXATA) { 432 printf("ata: unit out of range(%d)\n", atanlun); 433 return 0; 434 } 435 436 /* check if this is located at one of the std addresses */ 437 if (ioaddr == IO_WD1) 438 lun = 0; 439 else if (ioaddr == IO_WD2) 440 lun = 1; 441 else 442 lun = atanlun++; 443 444 if ((scp = atadevices[lun])) { 445 printf("ata%d: unit already attached\n", lun); 446 return 0; 447 } 448 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 449 if (scp == NULL) { 450 printf("ata%d: failed to allocate driver storage\n", lun); 451 return 0; 452 } 453 bzero(scp, sizeof(struct ata_softc)); 454 455 scp->ioaddr = ioaddr; 456 scp->altioaddr = altioaddr; 457 scp->lun = lun; 458 scp->unit = *unit; 459 scp->active = ATA_IDLE; 460 461 if (bootverbose) 462 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 463 scp->lun, scp->ioaddr, scp->altioaddr); 464 465 466 /* do we have any signs of ATA/ATAPI HW being present ? */ 467 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 468 DELAY(1); 469 status0 = inb(scp->ioaddr + ATA_STATUS); 470 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 471 DELAY(1); 472 status1 = inb(scp->ioaddr + ATA_STATUS); 473 if ((status0 & 0xf8) != 0xf8) 474 mask |= 0x01; 475 if ((status1 & 0xf8) != 0xf8) 476 mask |= 0x02; 477 if (bootverbose) 478 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 479 scp->lun, mask, status0, status1); 480 if (!mask) { 481 free(scp, M_DEVBUF); 482 return 0; 483 } 484 ata_reset(scp, &mask); 485 if (!mask) { 486 free(scp, M_DEVBUF); 487 return 0; 488 } 489 /* 490 * OK, we have at least one device on the chain, 491 * check for ATAPI signatures, if none check if its 492 * a good old ATA device. 493 */ 494 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 495 DELAY(1); 496 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 497 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 498 scp->devices |= ATA_ATAPI_MASTER; 499 } 500 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 501 DELAY(1); 502 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 503 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 504 scp->devices |= ATA_ATAPI_SLAVE; 505 } 506 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 507 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 508 DELAY(1); 509 outb(scp->ioaddr + ATA_ERROR, 0x58); 510 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 511 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 512 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 513 scp->devices |= ATA_ATA_MASTER; 514 } 515 } 516 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 517 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 518 DELAY(1); 519 outb(scp->ioaddr + ATA_ERROR, 0x58); 520 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 521 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 522 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 523 scp->devices |= ATA_ATA_SLAVE; 524 } 525 } 526 if (bootverbose) 527 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 528 if (!scp->devices) { 529 free(scp, M_DEVBUF); 530 return 0; 531 } 532 TAILQ_INIT(&scp->ata_queue); 533 TAILQ_INIT(&scp->atapi_queue); 534 *unit = scp->lun; 535 scp->dev = dev; 536 if (bmaddr) 537 scp->bmaddr = bmaddr; 538 atadevices[scp->lun] = scp; 539#if NAPM > 0 540 scp->resume_hook.ah_fun = (void *)ata_reinit; 541 scp->resume_hook.ah_arg = scp; 542 scp->resume_hook.ah_name = "ATA driver"; 543 scp->resume_hook.ah_order = APM_MID_ORDER; 544 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 545#endif 546 return ATA_IOSIZE; 547} 548 549static void 550ataintr(void *data) 551{ 552 struct ata_softc *scp =(struct ata_softc *)data; 553 554 /* is this interrupt really for this channel */ 555 if ((scp->flags & ATA_DMA_ACTIVE) && 556 !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 557 return; 558 559 if (((scp->status = inb(scp->ioaddr+ATA_STATUS)) & ATA_S_BUSY)==ATA_S_BUSY) 560 return; 561 562 /* find & call the responsible driver to process this interrupt */ 563 switch (scp->active) { 564#if NATADISK > 0 565 case ATA_ACTIVE_ATA: 566 if (!scp->running) 567 return; 568 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 569 return; 570 break; 571#endif 572#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 573 case ATA_ACTIVE_ATAPI: 574 if (!scp->running) 575 return; 576 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 577 return; 578 break; 579#endif 580 case ATA_WAIT_INTR: 581 wakeup((caddr_t)scp); 582 break; 583 584 case ATA_WAIT_READY: 585 break; 586 587 case ATA_REINITING: 588 return; 589 590 default: 591 case ATA_IDLE: 592#ifdef ATA_DEBUG 593 { 594 static int32_t intr_count = 0; 595 if (intr_count++ < 10) 596 printf("ata%d: unwanted interrupt %d status = %02x\n", 597 scp->lun, intr_count, scp->status); 598 } 599#endif 600 return; 601 } 602 scp->active = ATA_IDLE; 603 scp->running = NULL; 604 ata_start(scp); 605} 606 607void 608ata_start(struct ata_softc *scp) 609{ 610 struct ad_request *ad_request; 611 struct atapi_request *atapi_request; 612 613#ifdef ATA_DEBUG 614 printf("ata_start: entered\n"); 615#endif 616 if (scp->active != ATA_IDLE) 617 return; 618 619#if NATADISK > 0 620 /* find & call the responsible driver if anything on the ATA queue */ 621 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 622 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 623 scp->active = ATA_ACTIVE_ATA; 624 scp->running = ad_request; 625 ad_transfer(ad_request); 626#ifdef ATA_DEBUG 627 printf("ata_start: started ata, leaving\n"); 628#endif 629 return; 630 } 631#endif 632#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 633 /* 634 * find & call the responsible driver if anything on the ATAPI queue. 635 * check for device busy by polling the DSC bit, if busy, check 636 * for requests to the other device on the channel (if any). 637 * if the other device is an ATA disk it already had its chance above. 638 * if no request can be served, timeout a call to ata_start. 639 */ 640 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 641 struct atapi_softc *atp = atapi_request->device; 642 static int32_t interval = 1; 643 644 if (atp->flags & ATAPI_F_DSC_USED) { 645 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 646 DELAY(1); 647 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 648 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 649 if (atapi_request->device->unit != atp->unit) { 650 struct atapi_softc *tmp = atapi_request->device; 651 652 outb(tmp->controller->ioaddr + ATA_DRIVE, 653 ATA_D_IBM | tmp->unit); 654 DELAY(1); 655 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 656 atapi_request = NULL; 657 break; 658 } 659 } 660 } 661 if (!atapi_request) { 662 timeout((timeout_t *)ata_start, atp->controller, interval++); 663 return; 664 } 665 else 666 interval = 1; 667 } 668 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 669 scp->active = ATA_ACTIVE_ATAPI; 670 scp->running = atapi_request; 671 atapi_transfer(atapi_request); 672#ifdef ATA_DEBUG 673 printf("ata_start: started atapi, leaving\n"); 674#endif 675 return; 676 } 677#endif 678} 679 680void 681ata_reset(struct ata_softc *scp, int32_t *mask) 682{ 683 int32_t timeout; 684 int8_t status0, status1; 685 686 /* reset channel */ 687 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 688 DELAY(1); 689 inb(scp->ioaddr + ATA_STATUS); 690 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 691 DELAY(10000); 692 outb(scp->altioaddr, ATA_A_IDS); 693 DELAY(10000); 694 inb(scp->ioaddr + ATA_ERROR); 695 DELAY(3000); 696 697 /* wait for BUSY to go inactive */ 698 for (timeout = 0; timeout < 310000; timeout++) { 699 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 700 DELAY(1); 701 status0 = inb(scp->ioaddr + ATA_STATUS); 702 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 703 DELAY(1); 704 status1 = inb(scp->ioaddr + ATA_STATUS); 705 if (*mask == 0x01) /* wait for master only */ 706 if (!(status0 & ATA_S_BUSY)) 707 break; 708 if (*mask == 0x02) /* wait for slave only */ 709 if (!(status1 & ATA_S_BUSY)) 710 break; 711 if (*mask == 0x03) /* wait for both master & slave */ 712 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 713 break; 714 DELAY(100); 715 } 716 DELAY(1); 717 outb(scp->altioaddr, ATA_A_4BIT); 718 if (status0 & ATA_S_BUSY) 719 *mask &= ~0x01; 720 if (status1 & ATA_S_BUSY) 721 *mask &= ~0x02; 722 if (bootverbose) 723 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 724 scp->lun, *mask, status0, status1); 725} 726 727int32_t 728ata_reinit(struct ata_softc *scp) 729{ 730 int32_t mask = 0, omask; 731 732 scp->active = ATA_REINITING; 733 scp->running = NULL; 734 printf("ata%d: resetting devices .. ", scp->lun); 735 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 736 mask |= 0x01; 737 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 738 mask |= 0x02; 739 omask = mask; 740 ata_reset(scp, &mask); 741 if (omask != mask) 742 printf(" device dissapeared! %d ", omask & ~mask); 743 744#if NATADISK > 0 745 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 746 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 747 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 748 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 749#endif 750#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 751 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 752 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 753 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 754 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 755#endif 756 printf("done\n"); 757 scp->active = ATA_IDLE; 758 ata_start(scp); 759 return 0; 760} 761 762int32_t 763ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 764{ 765 u_int8_t status; 766 u_int32_t timeout = 0; 767 768 DELAY(1); 769 while (timeout <= 5000000) { /* timeout 5 secs */ 770 status = inb(scp->ioaddr + ATA_STATUS); 771 772 /* if drive fails status, reselect the drive just to be sure */ 773 if (status == 0xff) { 774 printf("ata%d: %s: no status, reselecting device\n", 775 scp->lun, device?"slave":"master"); 776 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 777 DELAY(1); 778 status = inb(scp->ioaddr + ATA_STATUS); 779 } 780 if (status == 0xff) 781 return -1; 782 scp->status = status; 783 if (!(status & ATA_S_BUSY)) { 784 if (status & ATA_S_ERROR) 785 scp->error = inb(scp->ioaddr + ATA_ERROR); 786 if ((status & mask) == mask) 787 return (status & ATA_S_ERROR); 788 } 789 if (timeout > 1000) { 790 timeout += 1000; 791 DELAY(1000); 792 } 793 else { 794 timeout += 10; 795 DELAY(10); 796 } 797 } 798 return -1; 799} 800 801int32_t 802ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 803 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 804 u_int32_t count, u_int32_t feature, int32_t flags) 805{ 806#ifdef ATA_DEBUG 807 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 808 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 809 scp->lun, scp->ioaddr, device, command, 810 cylinder, head, sector, count, flags); 811#endif 812 813 /* ready to issue command ? */ 814 if (ata_wait(scp, device, 0) < 0) { 815 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 816 scp->lun, device ? "slave" : "master", command, 817 scp->status, scp->error); 818 return -1; 819 } 820 outb(scp->ioaddr + ATA_FEATURE, feature); 821 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 822 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 823 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 824 outb(scp->ioaddr + ATA_SECTOR, sector); 825 outb(scp->ioaddr + ATA_COUNT, count); 826 827 switch (flags) { 828 case ATA_WAIT_INTR: 829 if (scp->active != ATA_IDLE) 830 printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active)); 831 scp->active = ATA_WAIT_INTR; 832 outb(scp->ioaddr + ATA_CMD, command); 833 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 834 printf("ata_command: timeout waiting for interrupt\n"); 835 scp->active = ATA_IDLE; 836 return -1; 837 } 838 break; 839 840 case ATA_WAIT_READY: 841 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 842 printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active)); 843 scp->active = ATA_WAIT_READY; 844 outb(scp->ioaddr + ATA_CMD, command); 845 if (ata_wait(scp, device, ATA_S_READY) < 0) { 846 printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n", 847 scp->lun, device ? "slave" : "master", command, 848 scp->status, scp->error); 849 scp->active = ATA_IDLE; 850 return -1; 851 } 852 scp->active = ATA_IDLE; 853 break; 854 855 case ATA_IMMEDIATE: 856 outb(scp->ioaddr + ATA_CMD, command); 857 break; 858 859 default: 860 printf("DANGER: illegal interrupt flag=%s\n", active2str(flags)); 861 } 862#ifdef ATA_DEBUG 863 printf("ata_command: leaving\n"); 864#endif 865 return 0; 866} 867 868int8_t * 869ata_mode2str(int32_t mode) 870{ 871 switch (mode) { 872 case ATA_MODE_PIO: 873 return "PIO"; 874 case ATA_MODE_WDMA2: 875 return "DMA"; 876 case ATA_MODE_UDMA2: 877 return "UDMA33"; 878 case ATA_MODE_UDMA3: 879 return "UDMA3"; 880 case ATA_MODE_UDMA4: 881 return "UDMA66"; 882 default: 883 return "???"; 884 } 885} 886 887static int8_t * 888active2str(int32_t active) 889{ 890 switch (active) { 891 case ATA_IDLE: 892 return("ATA_IDLE"); 893 case ATA_WAIT_INTR: 894 return("ATA_WAIT_INTR"); 895 case ATA_ACTIVE_ATA: 896 return("ATA_ACTIVE_ATA"); 897 case ATA_ACTIVE_ATAPI: 898 return("ATA_ACTIVE_ATAPI"); 899 case ATA_REINITING: 900 return("ATA_REINITING"); 901 default: 902 return("UNKNOWN"); 903 } 904} 905 906void 907bswap(int8_t *buf, int32_t len) 908{ 909 u_int16_t *p = (u_int16_t*)(buf + len); 910 911 while (--p >= (u_int16_t*)buf) 912 *p = ntohs(*p); 913} 914 915void 916btrim(int8_t *buf, int32_t len) 917{ 918 int8_t *p; 919 920 for (p = buf; p < buf+len; ++p) 921 if (!*p) 922 *p = ' '; 923 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 924 *p = 0; 925} 926 927void 928bpack(int8_t *src, int8_t *dst, int32_t len) 929{ 930 int32_t i, j, blank; 931 932 for (i = j = blank = 0 ; i < len-1; i++) { 933 if (blank && src[i] == ' ') continue; 934 if (blank && src[i] != ' ') { 935 dst[j++] = src[i]; 936 blank = 0; 937 continue; 938 } 939 if (src[i] == ' ') { 940 blank = 1; 941 if (i == 0) 942 continue; 943 } 944 dst[j++] = src[i]; 945 } 946 dst[j] = 0x00; 947}
|