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:
| 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:
|
230 if (desc) { 231 device_set_desc(dev, desc); 232 return 0; 233 } 234 else 235 return ENXIO; 236} 237 238static int 239ata_pciattach(device_t dev) 240{ 241 int unit = device_get_unit(dev); 242 struct ata_softc *scp; 243 u_int32_t type; 244 u_int8_t class, subclass; 245 u_int32_t cmd; 246 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 247 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 248 struct resource *irq = NULL; 249 int32_t lun; 250 251 /* set up vendor-specific stuff */ 252 type = pci_get_devid(dev); 253 class = pci_get_class(dev); 254 subclass = pci_get_subclass(dev); 255 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 256 257#ifdef ATA_DEBUG 258 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n", 259 unit, type, class, subclass, cmd, pci_get_progif(dev)); 260#endif 261 262 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 263 iobase_1 = IO_WD1; 264 altiobase_1 = iobase_1 + ATA_ALTPORT; 265 irq1 = 14; 266 } 267 else { 268 iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK; 269 altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK; 270 bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK; 271 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 272 } 273 274 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 275 iobase_2 = IO_WD2; 276 altiobase_2 = iobase_2 + ATA_ALTPORT; 277 irq2 = 15; 278 } 279 else { 280 iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK; 281 altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK; 282 bmaddr_2 = (pci_read_config(dev, 0x20, 4) & IOMASK) + ATA_BM_OFFSET1; 283 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 284 } 285 286 /* is this controller busmaster DMA capable ? */ 287 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 288 /* is busmastering support turned on ? */ 289 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) { 290 /* is there a valid port range to connect to ? */ 291 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK)) { 292 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 293 printf("ata-pci%d: Busmastering DMA supported\n", unit); 294 } 295 else 296 printf("ata-pci%d: Busmastering DMA not configured\n", unit); 297 } 298 else 299 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 300 } 301 else { 302 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) { 303 /* Promise and HPT366 controllers support busmastering DMA */ 304 printf("ata-pci%d: Busmastering DMA supported\n", unit); 305 } 306 else { 307 /* we dont know this controller, disable busmastering DMA */ 308 bmaddr_1 = bmaddr_2 = 0; 309 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 310 } 311 } 312 313 /* do extra chipset specific setups */ 314 switch (type) { 315 case 0x522910b9: 316 /* on the Aladdin activate the ATAPI FIFO */ 317 pci_write_config(dev, 0x53, 318 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 319 break; 320 321 case 0x4d33105a: 322 case 0x4d38105a: 323 /* the Promise's need burst mode to be turned on explicitly */ 324 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 325 break; 326 327 case 0x05711106: 328 /* the VIA Apollo needs some sensible defaults */ 329 /* set prefetch, postwrite */ 330 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 331 332 /* set fifo configuration half'n'half */ 333 pci_write_config(dev, 0x43, 334 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 335 336 /* set status register read retry */ 337 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 338 339 /* set DMA read & end-of-sector fifo flush */ 340 pci_write_config(dev, 0x46, 341 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 342 343 /* set sector size */ 344 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 345 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 346 break; 347 } 348 349 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 350 lun = 0; 351 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 352 scp = atadevices[lun]; 353 if (iobase_1 == IO_WD1) 354#ifdef __i386__ 355 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 356 &bio_imask, INTR_EXCL); 357#endif 358#ifdef __alpha__ 359 alpha_platform_setup_ide_intr(0, ataintr, scp); 360#endif 361 else { 362 int rid = 0; 363 void *ih; 364 365 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 366 RF_SHAREABLE | RF_ACTIVE))) 367 printf("ata_pciattach: Unable to alloc interrupt\n"); 368 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 369 } 370 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 371 lun, iobase_1, isa_apic_irq(irq1), unit); 372 } 373 lun = 1; 374 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 375 scp = atadevices[lun]; 376 if (iobase_2 == IO_WD2) 377#ifdef __i386__ 378 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 379 &bio_imask, INTR_EXCL); 380#endif 381#ifdef __alpha__ 382 alpha_platform_setup_ide_intr(1, ataintr, scp); 383#endif 384 else { 385 int rid = 0; 386 void *ih; 387 388 if (irq1 != irq2 || irq == NULL) { 389 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 390 RF_SHAREABLE | RF_ACTIVE))) 391 printf("ata_pciattach: Unable to alloc interrupt\n"); 392 } 393 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 394 } 395 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 396 lun, iobase_2, isa_apic_irq(irq2), unit); 397 } 398 return 0; 399} 400 401static device_method_t ata_pci_methods[] = { 402 /* Device interface */ 403 DEVMETHOD(device_probe, ata_pciprobe), 404 DEVMETHOD(device_attach, ata_pciattach), 405 { 0, 0 } 406}; 407 408static driver_t ata_pci_driver = { 409 "ata-pci", 410 ata_pci_methods, 411 sizeof(int), 412}; 413 414DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 415#endif 416 417static int32_t 418ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 419 device_t dev, int32_t *unit) 420{ 421 struct ata_softc *scp; 422 int32_t lun, mask = 0; 423 u_int8_t status0, status1; 424 425 if (atanlun > MAXATA) { 426 printf("ata: unit out of range(%d)\n", atanlun); 427 return 0; 428 } 429 430 /* check if this is located at one of the std addresses */ 431 if (ioaddr == IO_WD1) 432 lun = 0; 433 else if (ioaddr == IO_WD2) 434 lun = 1; 435 else 436 lun = atanlun++; 437 438 if ((scp = atadevices[lun])) { 439 printf("ata%d: unit already attached\n", lun); 440 return 0; 441 } 442 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 443 if (scp == NULL) { 444 printf("ata%d: failed to allocate driver storage\n", lun); 445 return 0; 446 } 447 bzero(scp, sizeof(struct ata_softc)); 448 449 scp->ioaddr = ioaddr; 450 scp->altioaddr = altioaddr; 451 scp->lun = lun; 452 scp->unit = *unit; 453 scp->active = ATA_IDLE; 454 455 if (bootverbose) 456 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 457 scp->lun, scp->ioaddr, scp->altioaddr); 458 459 460 /* do we have any signs of ATA/ATAPI HW being present ? */ 461 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 462 DELAY(1); 463 status0 = inb(scp->ioaddr + ATA_STATUS); 464 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 465 DELAY(1); 466 status1 = inb(scp->ioaddr + ATA_STATUS); 467 if ((status0 & 0xf8) != 0xf8) 468 mask |= 0x01; 469 if ((status1 & 0xf8) != 0xf8) 470 mask |= 0x02; 471 if (bootverbose) 472 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 473 scp->lun, mask, status0, status1); 474 if (!mask) { 475 free(scp, M_DEVBUF); 476 return 0; 477 } 478 ata_reset(scp, &mask); 479 if (!mask) { 480 free(scp, M_DEVBUF); 481 return 0; 482 } 483 /* 484 * OK, we have at least one device on the chain, 485 * check for ATAPI signatures, if none check if its 486 * a good old ATA device. 487 */ 488 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 489 DELAY(1); 490 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 491 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 492 scp->devices |= ATA_ATAPI_MASTER; 493 } 494 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 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_SLAVE; 499 } 500 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 501 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 502 DELAY(1); 503 outb(scp->ioaddr + ATA_ERROR, 0x58); 504 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 505 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 506 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 507 scp->devices |= ATA_ATA_MASTER; 508 } 509 } 510 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 511 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 512 DELAY(1); 513 outb(scp->ioaddr + ATA_ERROR, 0x58); 514 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 515 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 516 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 517 scp->devices |= ATA_ATA_SLAVE; 518 } 519 } 520 if (bootverbose) 521 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 522 if (!scp->devices) { 523 free(scp, M_DEVBUF); 524 return 0; 525 } 526 TAILQ_INIT(&scp->ata_queue); 527 TAILQ_INIT(&scp->atapi_queue); 528 *unit = scp->lun; 529 scp->dev = dev; 530 if (bmaddr) 531 scp->bmaddr = bmaddr; 532 atadevices[scp->lun] = scp; 533#if NAPM > 0 534 scp->resume_hook.ah_fun = (void *)ata_reinit; 535 scp->resume_hook.ah_arg = scp; 536 scp->resume_hook.ah_name = "ATA driver"; 537 scp->resume_hook.ah_order = APM_MID_ORDER; 538 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 539#endif 540 return ATA_IOSIZE; 541} 542 543static void 544ataintr(void *data) 545{ 546 struct ata_softc *scp =(struct ata_softc *)data; 547 548 /* is this interrupt really for this channel */ 549 if ((scp->flags & ATA_DMA_ACTIVE) && 550 !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 551 return; 552 553 if (((scp->status = inb(scp->ioaddr+ATA_STATUS)) & ATA_S_BUSY)==ATA_S_BUSY) 554 return; 555 556 /* find & call the responsible driver to process this interrupt */ 557 switch (scp->active) { 558#if NATADISK > 0 559 case ATA_ACTIVE_ATA: 560 if (!scp->running) 561 return; 562 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 563 return; 564 break; 565#endif 566#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 567 case ATA_ACTIVE_ATAPI: 568 if (!scp->running) 569 return; 570 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 571 return; 572 break; 573#endif 574 case ATA_WAIT_INTR: 575 wakeup((caddr_t)scp); 576 break; 577 578 case ATA_WAIT_READY: 579 break; 580 581 case ATA_REINITING: 582 return; 583 584 default: 585 case ATA_IDLE: 586#ifdef ATA_DEBUG 587 { 588 static int32_t intr_count = 0; 589 if (intr_count++ < 10) 590 printf("ata%d: unwanted interrupt %d status = %02x\n", 591 scp->lun, intr_count, scp->status); 592 } 593#endif 594 return; 595 } 596 scp->active = ATA_IDLE; 597 scp->running = NULL; 598 ata_start(scp); 599} 600 601void 602ata_start(struct ata_softc *scp) 603{ 604 struct ad_request *ad_request; 605 struct atapi_request *atapi_request; 606 607#ifdef ATA_DEBUG 608 printf("ata_start: entered\n"); 609#endif 610 if (scp->active != ATA_IDLE) 611 return; 612 613#if NATADISK > 0 614 /* find & call the responsible driver if anything on the ATA queue */ 615 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 616 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 617 scp->active = ATA_ACTIVE_ATA; 618 scp->running = ad_request; 619 ad_transfer(ad_request); 620#ifdef ATA_DEBUG 621 printf("ata_start: started ata, leaving\n"); 622#endif 623 return; 624 } 625#endif 626#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 627 /* 628 * find & call the responsible driver if anything on the ATAPI queue. 629 * check for device busy by polling the DSC bit, if busy, check 630 * for requests to the other device on the channel (if any). 631 * if the other device is an ATA disk it already had its chance above. 632 * if no request can be served, timeout a call to ata_start. 633 */ 634 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 635 struct atapi_softc *atp = atapi_request->device; 636 static int32_t interval = 1; 637 638 if (atp->flags & ATAPI_F_DSC_USED) { 639 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 640 DELAY(1); 641 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 642 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 643 if (atapi_request->device->unit != atp->unit) { 644 struct atapi_softc *tmp = atapi_request->device; 645 646 outb(tmp->controller->ioaddr + ATA_DRIVE, 647 ATA_D_IBM | tmp->unit); 648 DELAY(1); 649 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 650 atapi_request = NULL; 651 break; 652 } 653 } 654 } 655 if (!atapi_request) { 656 timeout((timeout_t *)ata_start, atp->controller, interval++); 657 return; 658 } 659 else 660 interval = 1; 661 } 662 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 663 scp->active = ATA_ACTIVE_ATAPI; 664 scp->running = atapi_request; 665 atapi_transfer(atapi_request); 666#ifdef ATA_DEBUG 667 printf("ata_start: started atapi, leaving\n"); 668#endif 669 return; 670 } 671#endif 672} 673 674void 675ata_reset(struct ata_softc *scp, int32_t *mask) 676{ 677 int32_t timeout; 678 int8_t status0, status1; 679 680 /* reset channel */ 681 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 682 DELAY(1); 683 inb(scp->ioaddr + ATA_STATUS); 684 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 685 DELAY(10000); 686 outb(scp->altioaddr, ATA_A_IDS); 687 DELAY(10000); 688 inb(scp->ioaddr + ATA_ERROR); 689 DELAY(3000); 690 691 /* wait for BUSY to go inactive */ 692 for (timeout = 0; timeout < 310000; timeout++) { 693 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 694 DELAY(1); 695 status0 = inb(scp->ioaddr + ATA_STATUS); 696 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 697 DELAY(1); 698 status1 = inb(scp->ioaddr + ATA_STATUS); 699 if (*mask == 0x01) /* wait for master only */ 700 if (!(status0 & ATA_S_BUSY)) 701 break; 702 if (*mask == 0x02) /* wait for slave only */ 703 if (!(status1 & ATA_S_BUSY)) 704 break; 705 if (*mask == 0x03) /* wait for both master & slave */ 706 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 707 break; 708 DELAY(100); 709 } 710 DELAY(1); 711 outb(scp->altioaddr, ATA_A_4BIT); 712 if (status0 & ATA_S_BUSY) 713 *mask &= ~0x01; 714 if (status1 & ATA_S_BUSY) 715 *mask &= ~0x02; 716 if (bootverbose) 717 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 718 scp->lun, *mask, status0, status1); 719} 720 721int32_t 722ata_reinit(struct ata_softc *scp) 723{ 724 int32_t mask = 0, omask; 725 726 scp->active = ATA_REINITING; 727 scp->running = NULL; 728 printf("ata%d: resetting devices .. ", scp->lun); 729 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 730 mask |= 0x01; 731 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 732 mask |= 0x02; 733 omask = mask; 734 ata_reset(scp, &mask); 735 if (omask != mask) 736 printf(" device dissapeared! %d ", omask & ~mask); 737 738#if NATADISK > 0 739 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 740 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 741 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 742 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 743#endif 744#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 745 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 746 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 747 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 748 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 749#endif 750 printf("done\n"); 751 scp->active = ATA_IDLE; 752 ata_start(scp); 753 return 0; 754} 755 756int32_t 757ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 758{ 759 u_int8_t status; 760 u_int32_t timeout = 0; 761 762 while (timeout <= 5000000) { /* timeout 5 secs */ 763 status = inb(scp->ioaddr + ATA_STATUS); 764 765 /* if drive fails status, reselect the drive just to be sure */ 766 if (status == 0xff) { 767 printf("ata%d: %s: no status, reselecting device\n", 768 scp->lun, device?"slave":"master"); 769 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 770 DELAY(1); 771 status = inb(scp->ioaddr + ATA_STATUS); 772 } 773 if (status == 0xff) 774 return -1; 775 scp->status = status; 776 if (!(status & ATA_S_BUSY)) { 777 if (status & ATA_S_ERROR) 778 scp->error = inb(scp->ioaddr + ATA_ERROR); 779 if ((status & mask) == mask) 780 return (status & ATA_S_ERROR); 781 } 782 if (timeout > 1000) { 783 timeout += 1000; 784 DELAY(1000); 785 } 786 else { 787 timeout += 10; 788 DELAY(10); 789 } 790 } 791 return -1; 792} 793 794int32_t 795ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 796 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 797 u_int32_t count, u_int32_t feature, int32_t flags) 798{ 799#ifdef ATA_DEBUG 800 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 801 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 802 scp->lun, scp->ioaddr, device, command, 803 cylinder, head, sector, count, flags); 804#endif 805 806 /* ready to issue command ? */ 807 if (ata_wait(scp, device, 0) < 0) { 808 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 809 scp->lun, device ? "slave" : "master", command, 810 scp->status, scp->error); 811 return -1; 812 } 813 outb(scp->ioaddr + ATA_FEATURE, feature); 814 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 815 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 816 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 817 outb(scp->ioaddr + ATA_SECTOR, sector); 818 outb(scp->ioaddr + ATA_COUNT, count); 819 820 switch (flags) { 821 case ATA_WAIT_INTR: 822 if (scp->active != ATA_IDLE) 823 printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active)); 824 scp->active = ATA_WAIT_INTR; 825 outb(scp->ioaddr + ATA_CMD, command); 826 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 827 printf("ata_command: timeout waiting for interrupt\n"); 828 scp->active = ATA_IDLE; 829 return -1; 830 } 831 break; 832 833 case ATA_WAIT_READY: 834 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 835 printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active)); 836 scp->active = ATA_WAIT_READY; 837 outb(scp->ioaddr + ATA_CMD, command); 838 if (ata_wait(scp, device, ATA_S_READY) < 0) { 839 printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n", 840 scp->lun, device ? "slave" : "master", command, 841 scp->status, scp->error);
| 237 if (desc) { 238 device_set_desc(dev, desc); 239 return 0; 240 } 241 else 242 return ENXIO; 243} 244 245static int 246ata_pciattach(device_t dev) 247{ 248 int unit = device_get_unit(dev); 249 struct ata_softc *scp; 250 u_int32_t type; 251 u_int8_t class, subclass; 252 u_int32_t cmd; 253 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 254 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 255 struct resource *irq = NULL; 256 int32_t lun; 257 258 /* set up vendor-specific stuff */ 259 type = pci_get_devid(dev); 260 class = pci_get_class(dev); 261 subclass = pci_get_subclass(dev); 262 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 263 264#ifdef ATA_DEBUG 265 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n", 266 unit, type, class, subclass, cmd, pci_get_progif(dev)); 267#endif 268 269 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 270 iobase_1 = IO_WD1; 271 altiobase_1 = iobase_1 + ATA_ALTPORT; 272 irq1 = 14; 273 } 274 else { 275 iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK; 276 altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK; 277 bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK; 278 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 279 } 280 281 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 282 iobase_2 = IO_WD2; 283 altiobase_2 = iobase_2 + ATA_ALTPORT; 284 irq2 = 15; 285 } 286 else { 287 iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK; 288 altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK; 289 bmaddr_2 = (pci_read_config(dev, 0x20, 4) & IOMASK) + ATA_BM_OFFSET1; 290 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 291 } 292 293 /* is this controller busmaster DMA capable ? */ 294 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 295 /* is busmastering support turned on ? */ 296 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) { 297 /* is there a valid port range to connect to ? */ 298 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK)) { 299 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 300 printf("ata-pci%d: Busmastering DMA supported\n", unit); 301 } 302 else 303 printf("ata-pci%d: Busmastering DMA not configured\n", unit); 304 } 305 else 306 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 307 } 308 else { 309 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) { 310 /* Promise and HPT366 controllers support busmastering DMA */ 311 printf("ata-pci%d: Busmastering DMA supported\n", unit); 312 } 313 else { 314 /* we dont know this controller, disable busmastering DMA */ 315 bmaddr_1 = bmaddr_2 = 0; 316 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 317 } 318 } 319 320 /* do extra chipset specific setups */ 321 switch (type) { 322 case 0x522910b9: 323 /* on the Aladdin activate the ATAPI FIFO */ 324 pci_write_config(dev, 0x53, 325 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 326 break; 327 328 case 0x4d33105a: 329 case 0x4d38105a: 330 /* the Promise's need burst mode to be turned on explicitly */ 331 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 332 break; 333 334 case 0x05711106: 335 /* the VIA Apollo needs some sensible defaults */ 336 /* set prefetch, postwrite */ 337 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 338 339 /* set fifo configuration half'n'half */ 340 pci_write_config(dev, 0x43, 341 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 342 343 /* set status register read retry */ 344 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 345 346 /* set DMA read & end-of-sector fifo flush */ 347 pci_write_config(dev, 0x46, 348 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 349 350 /* set sector size */ 351 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 352 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 353 break; 354 } 355 356 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 357 lun = 0; 358 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 359 scp = atadevices[lun]; 360 if (iobase_1 == IO_WD1) 361#ifdef __i386__ 362 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 363 &bio_imask, INTR_EXCL); 364#endif 365#ifdef __alpha__ 366 alpha_platform_setup_ide_intr(0, ataintr, scp); 367#endif 368 else { 369 int rid = 0; 370 void *ih; 371 372 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 373 RF_SHAREABLE | RF_ACTIVE))) 374 printf("ata_pciattach: Unable to alloc interrupt\n"); 375 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 376 } 377 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 378 lun, iobase_1, isa_apic_irq(irq1), unit); 379 } 380 lun = 1; 381 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 382 scp = atadevices[lun]; 383 if (iobase_2 == IO_WD2) 384#ifdef __i386__ 385 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 386 &bio_imask, INTR_EXCL); 387#endif 388#ifdef __alpha__ 389 alpha_platform_setup_ide_intr(1, ataintr, scp); 390#endif 391 else { 392 int rid = 0; 393 void *ih; 394 395 if (irq1 != irq2 || irq == NULL) { 396 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 397 RF_SHAREABLE | RF_ACTIVE))) 398 printf("ata_pciattach: Unable to alloc interrupt\n"); 399 } 400 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 401 } 402 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 403 lun, iobase_2, isa_apic_irq(irq2), unit); 404 } 405 return 0; 406} 407 408static device_method_t ata_pci_methods[] = { 409 /* Device interface */ 410 DEVMETHOD(device_probe, ata_pciprobe), 411 DEVMETHOD(device_attach, ata_pciattach), 412 { 0, 0 } 413}; 414 415static driver_t ata_pci_driver = { 416 "ata-pci", 417 ata_pci_methods, 418 sizeof(int), 419}; 420 421DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 422#endif 423 424static int32_t 425ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 426 device_t dev, int32_t *unit) 427{ 428 struct ata_softc *scp; 429 int32_t lun, mask = 0; 430 u_int8_t status0, status1; 431 432 if (atanlun > MAXATA) { 433 printf("ata: unit out of range(%d)\n", atanlun); 434 return 0; 435 } 436 437 /* check if this is located at one of the std addresses */ 438 if (ioaddr == IO_WD1) 439 lun = 0; 440 else if (ioaddr == IO_WD2) 441 lun = 1; 442 else 443 lun = atanlun++; 444 445 if ((scp = atadevices[lun])) { 446 printf("ata%d: unit already attached\n", lun); 447 return 0; 448 } 449 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 450 if (scp == NULL) { 451 printf("ata%d: failed to allocate driver storage\n", lun); 452 return 0; 453 } 454 bzero(scp, sizeof(struct ata_softc)); 455 456 scp->ioaddr = ioaddr; 457 scp->altioaddr = altioaddr; 458 scp->lun = lun; 459 scp->unit = *unit; 460 scp->active = ATA_IDLE; 461 462 if (bootverbose) 463 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 464 scp->lun, scp->ioaddr, scp->altioaddr); 465 466 467 /* do we have any signs of ATA/ATAPI HW being present ? */ 468 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 469 DELAY(1); 470 status0 = inb(scp->ioaddr + ATA_STATUS); 471 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 472 DELAY(1); 473 status1 = inb(scp->ioaddr + ATA_STATUS); 474 if ((status0 & 0xf8) != 0xf8) 475 mask |= 0x01; 476 if ((status1 & 0xf8) != 0xf8) 477 mask |= 0x02; 478 if (bootverbose) 479 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 480 scp->lun, mask, status0, status1); 481 if (!mask) { 482 free(scp, M_DEVBUF); 483 return 0; 484 } 485 ata_reset(scp, &mask); 486 if (!mask) { 487 free(scp, M_DEVBUF); 488 return 0; 489 } 490 /* 491 * OK, we have at least one device on the chain, 492 * check for ATAPI signatures, if none check if its 493 * a good old ATA device. 494 */ 495 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 496 DELAY(1); 497 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 498 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 499 scp->devices |= ATA_ATAPI_MASTER; 500 } 501 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 502 DELAY(1); 503 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 504 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 505 scp->devices |= ATA_ATAPI_SLAVE; 506 } 507 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 508 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 509 DELAY(1); 510 outb(scp->ioaddr + ATA_ERROR, 0x58); 511 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 512 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 513 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 514 scp->devices |= ATA_ATA_MASTER; 515 } 516 } 517 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 518 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 519 DELAY(1); 520 outb(scp->ioaddr + ATA_ERROR, 0x58); 521 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 522 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 523 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 524 scp->devices |= ATA_ATA_SLAVE; 525 } 526 } 527 if (bootverbose) 528 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 529 if (!scp->devices) { 530 free(scp, M_DEVBUF); 531 return 0; 532 } 533 TAILQ_INIT(&scp->ata_queue); 534 TAILQ_INIT(&scp->atapi_queue); 535 *unit = scp->lun; 536 scp->dev = dev; 537 if (bmaddr) 538 scp->bmaddr = bmaddr; 539 atadevices[scp->lun] = scp; 540#if NAPM > 0 541 scp->resume_hook.ah_fun = (void *)ata_reinit; 542 scp->resume_hook.ah_arg = scp; 543 scp->resume_hook.ah_name = "ATA driver"; 544 scp->resume_hook.ah_order = APM_MID_ORDER; 545 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 546#endif 547 return ATA_IOSIZE; 548} 549 550static void 551ataintr(void *data) 552{ 553 struct ata_softc *scp =(struct ata_softc *)data; 554 555 /* is this interrupt really for this channel */ 556 if ((scp->flags & ATA_DMA_ACTIVE) && 557 !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 558 return; 559 560 if (((scp->status = inb(scp->ioaddr+ATA_STATUS)) & ATA_S_BUSY)==ATA_S_BUSY) 561 return; 562 563 /* find & call the responsible driver to process this interrupt */ 564 switch (scp->active) { 565#if NATADISK > 0 566 case ATA_ACTIVE_ATA: 567 if (!scp->running) 568 return; 569 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 570 return; 571 break; 572#endif 573#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 574 case ATA_ACTIVE_ATAPI: 575 if (!scp->running) 576 return; 577 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 578 return; 579 break; 580#endif 581 case ATA_WAIT_INTR: 582 wakeup((caddr_t)scp); 583 break; 584 585 case ATA_WAIT_READY: 586 break; 587 588 case ATA_REINITING: 589 return; 590 591 default: 592 case ATA_IDLE: 593#ifdef ATA_DEBUG 594 { 595 static int32_t intr_count = 0; 596 if (intr_count++ < 10) 597 printf("ata%d: unwanted interrupt %d status = %02x\n", 598 scp->lun, intr_count, scp->status); 599 } 600#endif 601 return; 602 } 603 scp->active = ATA_IDLE; 604 scp->running = NULL; 605 ata_start(scp); 606} 607 608void 609ata_start(struct ata_softc *scp) 610{ 611 struct ad_request *ad_request; 612 struct atapi_request *atapi_request; 613 614#ifdef ATA_DEBUG 615 printf("ata_start: entered\n"); 616#endif 617 if (scp->active != ATA_IDLE) 618 return; 619 620#if NATADISK > 0 621 /* find & call the responsible driver if anything on the ATA queue */ 622 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 623 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 624 scp->active = ATA_ACTIVE_ATA; 625 scp->running = ad_request; 626 ad_transfer(ad_request); 627#ifdef ATA_DEBUG 628 printf("ata_start: started ata, leaving\n"); 629#endif 630 return; 631 } 632#endif 633#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 634 /* 635 * find & call the responsible driver if anything on the ATAPI queue. 636 * check for device busy by polling the DSC bit, if busy, check 637 * for requests to the other device on the channel (if any). 638 * if the other device is an ATA disk it already had its chance above. 639 * if no request can be served, timeout a call to ata_start. 640 */ 641 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 642 struct atapi_softc *atp = atapi_request->device; 643 static int32_t interval = 1; 644 645 if (atp->flags & ATAPI_F_DSC_USED) { 646 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 647 DELAY(1); 648 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 649 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 650 if (atapi_request->device->unit != atp->unit) { 651 struct atapi_softc *tmp = atapi_request->device; 652 653 outb(tmp->controller->ioaddr + ATA_DRIVE, 654 ATA_D_IBM | tmp->unit); 655 DELAY(1); 656 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 657 atapi_request = NULL; 658 break; 659 } 660 } 661 } 662 if (!atapi_request) { 663 timeout((timeout_t *)ata_start, atp->controller, interval++); 664 return; 665 } 666 else 667 interval = 1; 668 } 669 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 670 scp->active = ATA_ACTIVE_ATAPI; 671 scp->running = atapi_request; 672 atapi_transfer(atapi_request); 673#ifdef ATA_DEBUG 674 printf("ata_start: started atapi, leaving\n"); 675#endif 676 return; 677 } 678#endif 679} 680 681void 682ata_reset(struct ata_softc *scp, int32_t *mask) 683{ 684 int32_t timeout; 685 int8_t status0, status1; 686 687 /* reset channel */ 688 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 689 DELAY(1); 690 inb(scp->ioaddr + ATA_STATUS); 691 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 692 DELAY(10000); 693 outb(scp->altioaddr, ATA_A_IDS); 694 DELAY(10000); 695 inb(scp->ioaddr + ATA_ERROR); 696 DELAY(3000); 697 698 /* wait for BUSY to go inactive */ 699 for (timeout = 0; timeout < 310000; timeout++) { 700 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 701 DELAY(1); 702 status0 = inb(scp->ioaddr + ATA_STATUS); 703 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 704 DELAY(1); 705 status1 = inb(scp->ioaddr + ATA_STATUS); 706 if (*mask == 0x01) /* wait for master only */ 707 if (!(status0 & ATA_S_BUSY)) 708 break; 709 if (*mask == 0x02) /* wait for slave only */ 710 if (!(status1 & ATA_S_BUSY)) 711 break; 712 if (*mask == 0x03) /* wait for both master & slave */ 713 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 714 break; 715 DELAY(100); 716 } 717 DELAY(1); 718 outb(scp->altioaddr, ATA_A_4BIT); 719 if (status0 & ATA_S_BUSY) 720 *mask &= ~0x01; 721 if (status1 & ATA_S_BUSY) 722 *mask &= ~0x02; 723 if (bootverbose) 724 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 725 scp->lun, *mask, status0, status1); 726} 727 728int32_t 729ata_reinit(struct ata_softc *scp) 730{ 731 int32_t mask = 0, omask; 732 733 scp->active = ATA_REINITING; 734 scp->running = NULL; 735 printf("ata%d: resetting devices .. ", scp->lun); 736 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 737 mask |= 0x01; 738 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 739 mask |= 0x02; 740 omask = mask; 741 ata_reset(scp, &mask); 742 if (omask != mask) 743 printf(" device dissapeared! %d ", omask & ~mask); 744 745#if NATADISK > 0 746 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 747 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 748 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 749 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 750#endif 751#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 752 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 753 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 754 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 755 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 756#endif 757 printf("done\n"); 758 scp->active = ATA_IDLE; 759 ata_start(scp); 760 return 0; 761} 762 763int32_t 764ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 765{ 766 u_int8_t status; 767 u_int32_t timeout = 0; 768 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);
|