ata-all.c revision 54969
1/*- 2 * Copyright (c) 1998,1999 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 54969 1999-12-21 20:18:56Z sos $ 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 case 0x71998086: 196 return "Intel PIIX4 ATA controller"; 197 case 0x522910b9: 198 return "AcerLabs Aladdin ATA controller"; 199 case 0x05711106: /* 82c586 & 82c686 */ 200 if (ata_find_dev(dev, 0x05861106)) 201 return "VIA 82C586 ATA controller"; 202 if (ata_find_dev(dev, 0x06861106)) 203 return "VIA 82C686 ATA controller"; 204 return "VIA Apollo ATA controller"; 205 case 0x55131039: 206 return "SiS 5591 ATA controller"; 207 case 0x74091022: 208 return "AMD 756 ATA controller"; 209 case 0x4d33105a: 210 return "Promise Ultra/33 ATA controller"; 211 case 0x4d38105a: 212 return "Promise Ultra/66 ATA controller"; 213 case 0x00041103: 214 return "HighPoint HPT366 ATA controller"; 215 216 /* unsupported but known chipsets, generic DMA only */ 217 case 0x05961106: 218 return "VIA 82C596 ATA controller (generic mode)"; 219 case 0x06401095: 220 return "CMD 640 ATA controller (generic mode)"; 221 case 0x06461095: 222 return "CMD 646 ATA controller (generic mode)"; 223 case 0xc6931080: 224 return "Cypress 82C693 ATA controller (generic mode)"; 225 case 0x01021078: 226 return "Cyrix 5530 ATA controller (generic mode)"; 227 default: 228 if (pci_get_class(dev) == PCIC_STORAGE && 229 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 230 return "Unknown PCI ATA controller (generic mode)"; 231 } 232 return NULL; 233} 234 235static int 236ata_pciprobe(device_t dev) 237{ 238 const char *desc = ata_pcimatch(dev); 239 240 if (desc) { 241 device_set_desc(dev, desc); 242 return 0; 243 } 244 else 245 return ENXIO; 246} 247 248static int 249ata_pciattach(device_t dev) 250{ 251 int unit = device_get_unit(dev); 252 struct ata_softc *scp; 253 u_int32_t type; 254 u_int8_t class, subclass; 255 u_int32_t cmd; 256 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 257 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 258 struct resource *irq = NULL; 259 int32_t lun; 260 261 /* set up vendor-specific stuff */ 262 type = pci_get_devid(dev); 263 class = pci_get_class(dev); 264 subclass = pci_get_subclass(dev); 265 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 266 267#ifdef ATA_DEBUG 268 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n", 269 unit, type, class, subclass, cmd, pci_get_progif(dev)); 270#endif 271 272 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 273 iobase_1 = IO_WD1; 274 altiobase_1 = iobase_1 + ATA_ALTPORT; 275 irq1 = 14; 276 } 277 else { 278 iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK; 279 altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK; 280 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 281 } 282 283 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 284 iobase_2 = IO_WD2; 285 altiobase_2 = iobase_2 + ATA_ALTPORT; 286 irq2 = 15; 287 } 288 else { 289 iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK; 290 altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK; 291 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 292 } 293 294 /* is this controller busmaster DMA capable ? */ 295 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 296 /* is busmastering support turned on ? */ 297 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) { 298 /* is there a valid port range to connect to ? */ 299 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK)) { 300 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 301 printf("ata-pci%d: Busmastering DMA supported\n", unit); 302 } 303 else 304 printf("ata-pci%d: Busmastering DMA not configured\n", unit); 305 } 306 else 307 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 308 } 309 else { 310 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) { 311 /* Promise and HPT366 controllers support busmastering DMA */ 312 bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK; 313 bmaddr_2 = (pci_read_config(dev, 0x20, 4) & IOMASK)+ATA_BM_OFFSET1; 314 printf("ata-pci%d: Busmastering DMA supported\n", unit); 315 } 316 else { 317 /* we dont know this controller, no busmastering DMA */ 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 case 0x74091022: 338 /* the VIA 82C586, VIA 82C686 & AMD 756 needs some sensible defaults */ 339 /* set prefetch, postwrite */ 340 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 341 342 /* set fifo configuration half'n'half */ 343 pci_write_config(dev, 0x43, 344 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 345 346 /* set status register read retry */ 347 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 348 349 /* set DMA read & end-of-sector fifo flush */ 350 pci_write_config(dev, 0x46, 351 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 352 353 /* set sector size */ 354 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 355 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 356 break; 357 } 358 359 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 360 lun = 0; 361 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 362 scp = atadevices[lun]; 363 if (iobase_1 == IO_WD1) 364#ifdef __i386__ 365 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 366 &bio_imask, INTR_EXCL); 367#endif 368#ifdef __alpha__ 369 alpha_platform_setup_ide_intr(0, ataintr, scp); 370#endif 371 else { 372 int rid = 0; 373 void *ih; 374 375 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 376 RF_SHAREABLE | RF_ACTIVE))) 377 printf("ata_pciattach: Unable to alloc interrupt\n"); 378 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 379 } 380 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 381 lun, iobase_1, isa_apic_irq(irq1), unit); 382 } 383 lun = 1; 384 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 385 scp = atadevices[lun]; 386 if (iobase_2 == IO_WD2) 387#ifdef __i386__ 388 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 389 &bio_imask, INTR_EXCL); 390#endif 391#ifdef __alpha__ 392 alpha_platform_setup_ide_intr(1, ataintr, scp); 393#endif 394 else { 395 int rid = 0; 396 void *ih; 397 398 if (irq1 != irq2 || irq == NULL) { 399 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 400 RF_SHAREABLE | RF_ACTIVE))) 401 printf("ata_pciattach: Unable to alloc interrupt\n"); 402 } 403 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 404 } 405 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 406 lun, iobase_2, isa_apic_irq(irq2), unit); 407 } 408 return 0; 409} 410 411int32_t 412ata_find_dev(device_t dev, int32_t type) 413{ 414 device_t *children, child; 415 int nchildren, i; 416 417 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 418 return 0; 419 420 for (i = 0; i < nchildren; i++) { 421 child = children[i]; 422 423 /* check that it's on the same silicon and the device we want */ 424 if (pci_get_slot(dev) == pci_get_slot(child) && 425 pci_get_vendor(child) == (type & 0xffff) && 426 pci_get_device(child) == ((type & 0xffff0000)>>16)) { 427 free(children, M_TEMP); 428 return 1; 429 } 430 } 431 free(children, M_TEMP); 432 return 0; 433} 434 435static device_method_t ata_pci_methods[] = { 436 /* Device interface */ 437 DEVMETHOD(device_probe, ata_pciprobe), 438 DEVMETHOD(device_attach, ata_pciattach), 439 { 0, 0 } 440}; 441 442static driver_t ata_pci_driver = { 443 "ata-pci", 444 ata_pci_methods, 445 sizeof(int), 446}; 447 448DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 449#endif 450 451static int32_t 452ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 453 device_t dev, int32_t *unit) 454{ 455 struct ata_softc *scp; 456 int32_t lun, mask = 0; 457 u_int8_t status0, status1; 458 459 if (atanlun > MAXATA) { 460 printf("ata: unit out of range(%d)\n", atanlun); 461 return 0; 462 } 463 464 /* check if this is located at one of the std addresses */ 465 if (ioaddr == IO_WD1) 466 lun = 0; 467 else if (ioaddr == IO_WD2) 468 lun = 1; 469 else 470 lun = atanlun++; 471 472 if ((scp = atadevices[lun])) { 473 printf("ata%d: unit already attached\n", lun); 474 return 0; 475 } 476 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 477 if (scp == NULL) { 478 printf("ata%d: failed to allocate driver storage\n", lun); 479 return 0; 480 } 481 bzero(scp, sizeof(struct ata_softc)); 482 483 scp->ioaddr = ioaddr; 484 scp->altioaddr = altioaddr; 485 scp->bmaddr = bmaddr; 486 scp->lun = lun; 487 scp->unit = *unit; 488 scp->active = ATA_IDLE; 489 490 if (bootverbose) 491 printf("ata%d: iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 492 scp->lun, scp->ioaddr, scp->altioaddr, scp->bmaddr); 493 494 /* do we have any signs of ATA/ATAPI HW being present ? */ 495 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 496 DELAY(1); 497 status0 = inb(scp->ioaddr + ATA_STATUS); 498 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 499 DELAY(1); 500 status1 = inb(scp->ioaddr + ATA_STATUS); 501 if ((status0 & 0xf8) != 0xf8) 502 mask |= 0x01; 503 if ((status1 & 0xf8) != 0xf8) 504 mask |= 0x02; 505 if (bootverbose) 506 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 507 scp->lun, mask, status0, status1); 508 if (!mask) { 509 free(scp, M_DEVBUF); 510 return 0; 511 } 512 ata_reset(scp, &mask); 513 if (!mask) { 514 free(scp, M_DEVBUF); 515 return 0; 516 } 517 /* 518 * OK, we have at least one device on the chain, 519 * check for ATAPI signatures, if none check if its 520 * a good old ATA device. 521 */ 522 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 523 DELAY(1); 524 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 525 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 526 scp->devices |= ATA_ATAPI_MASTER; 527 } 528 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 529 DELAY(1); 530 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 531 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 532 scp->devices |= ATA_ATAPI_SLAVE; 533 } 534 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 535 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 536 DELAY(1); 537 outb(scp->ioaddr + ATA_ERROR, 0x58); 538 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 539 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 540 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 541 scp->devices |= ATA_ATA_MASTER; 542 } 543 } 544 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 545 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 546 DELAY(1); 547 outb(scp->ioaddr + ATA_ERROR, 0x58); 548 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 549 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 550 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 551 scp->devices |= ATA_ATA_SLAVE; 552 } 553 } 554 if (bootverbose) 555 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 556 if (!scp->devices) { 557 free(scp, M_DEVBUF); 558 return 0; 559 } 560 TAILQ_INIT(&scp->ata_queue); 561 TAILQ_INIT(&scp->atapi_queue); 562 *unit = scp->lun; 563 scp->dev = dev; 564 atadevices[scp->lun] = scp; 565#if NAPM > 0 566 scp->resume_hook.ah_fun = (void *)ata_reinit; 567 scp->resume_hook.ah_arg = scp; 568 scp->resume_hook.ah_name = "ATA driver"; 569 scp->resume_hook.ah_order = APM_MID_ORDER; 570 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 571#endif 572 return ATA_IOSIZE; 573} 574 575static void 576ataintr(void *data) 577{ 578 struct ata_softc *scp =(struct ata_softc *)data; 579 580 /* is this interrupt really for this channel */ 581 if ((scp->flags & ATA_DMA_ACTIVE) && 582 !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 583 return; 584 585 if (((scp->status = inb(scp->ioaddr+ATA_STATUS)) & ATA_S_BUSY)==ATA_S_BUSY) 586 return; 587 588 /* find & call the responsible driver to process this interrupt */ 589 switch (scp->active) { 590#if NATADISK > 0 591 case ATA_ACTIVE_ATA: 592 if (!scp->running) 593 return; 594 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 595 return; 596 break; 597#endif 598#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 599 case ATA_ACTIVE_ATAPI: 600 if (!scp->running) 601 return; 602 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 603 return; 604 break; 605#endif 606 case ATA_WAIT_INTR: 607 wakeup((caddr_t)scp); 608 break; 609 610 case ATA_WAIT_READY: 611 break; 612 613 case ATA_REINITING: 614 return; 615 616 default: 617 case ATA_IDLE: 618#ifdef ATA_DEBUG 619 { 620 static int32_t intr_count = 0; 621 if (intr_count++ < 10) 622 printf("ata%d: unwanted interrupt %d status = %02x\n", 623 scp->lun, intr_count, scp->status); 624 } 625#endif 626 return; 627 } 628 scp->active = ATA_IDLE; 629 scp->running = NULL; 630 ata_start(scp); 631} 632 633void 634ata_start(struct ata_softc *scp) 635{ 636 struct ad_request *ad_request; 637 struct atapi_request *atapi_request; 638 639 if (scp->active != ATA_IDLE) 640 return; 641 642#if NATADISK > 0 643 /* find & call the responsible driver if anything on the ATA queue */ 644 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 645 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 646 scp->active = ATA_ACTIVE_ATA; 647 scp->running = ad_request; 648 ad_transfer(ad_request); 649 return; 650 } 651#endif 652#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 653 /* 654 * find & call the responsible driver if anything on the ATAPI queue. 655 * check for device busy by polling the DSC bit, if busy, check 656 * for requests to the other device on the channel (if any). 657 * if the other device is an ATA disk it already had its chance above. 658 * if no request can be served, timeout a call to ata_start. 659 */ 660 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 661 struct atapi_softc *atp = atapi_request->device; 662 static int32_t interval = 1; 663 664 if (atp->flags & ATAPI_F_DSC_USED) { 665 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 666 DELAY(1); 667 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 668 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 669 if (atapi_request->device->unit != atp->unit) { 670 struct atapi_softc *tmp = atapi_request->device; 671 672 outb(tmp->controller->ioaddr + ATA_DRIVE, 673 ATA_D_IBM | tmp->unit); 674 DELAY(1); 675 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 676 atapi_request = NULL; 677 break; 678 } 679 } 680 } 681 if (!atapi_request) { 682 timeout((timeout_t *)ata_start, atp->controller, interval++); 683 return; 684 } 685 else 686 interval = 1; 687 } 688 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 689 scp->active = ATA_ACTIVE_ATAPI; 690 scp->running = atapi_request; 691 atapi_transfer(atapi_request); 692 return; 693 } 694#endif 695} 696 697void 698ata_reset(struct ata_softc *scp, int32_t *mask) 699{ 700 int32_t timeout; 701 int8_t status0, status1; 702 703 /* reset channel */ 704 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 705 DELAY(1); 706 inb(scp->ioaddr + ATA_STATUS); 707 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 708 DELAY(10000); 709 outb(scp->altioaddr, ATA_A_IDS); 710 DELAY(10000); 711 inb(scp->ioaddr + ATA_ERROR); 712 DELAY(3000); 713 714 /* wait for BUSY to go inactive */ 715 for (timeout = 0; timeout < 310000; timeout++) { 716 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 717 DELAY(1); 718 status0 = inb(scp->ioaddr + ATA_STATUS); 719 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 720 DELAY(1); 721 status1 = inb(scp->ioaddr + ATA_STATUS); 722 if (*mask == 0x01) /* wait for master only */ 723 if (!(status0 & ATA_S_BUSY)) 724 break; 725 if (*mask == 0x02) /* wait for slave only */ 726 if (!(status1 & ATA_S_BUSY)) 727 break; 728 if (*mask == 0x03) /* wait for both master & slave */ 729 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 730 break; 731 DELAY(100); 732 } 733 DELAY(1); 734 outb(scp->altioaddr, ATA_A_4BIT); 735 if (status0 & ATA_S_BUSY) 736 *mask &= ~0x01; 737 if (status1 & ATA_S_BUSY) 738 *mask &= ~0x02; 739 if (bootverbose) 740 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 741 scp->lun, *mask, status0, status1); 742} 743 744int32_t 745ata_reinit(struct ata_softc *scp) 746{ 747 int32_t mask = 0, omask; 748 749 scp->active = ATA_REINITING; 750 scp->running = NULL; 751 printf("ata%d: resetting devices .. ", scp->lun); 752 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 753 mask |= 0x01; 754 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 755 mask |= 0x02; 756 omask = mask; 757 ata_reset(scp, &mask); 758 if (omask != mask) 759 printf(" device dissapeared! %d ", omask & ~mask); 760 761#if NATADISK > 0 762 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 763 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 764 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 765 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 766#endif 767#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 768 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 769 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 770 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 771 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 772#endif 773 printf("done\n"); 774 scp->active = ATA_IDLE; 775 ata_start(scp); 776 return 0; 777} 778 779int32_t 780ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 781{ 782 u_int8_t status; 783 u_int32_t timeout = 0; 784 785 DELAY(1); 786 while (timeout <= 5000000) { /* timeout 5 secs */ 787 status = inb(scp->ioaddr + ATA_STATUS); 788 789 /* if drive fails status, reselect the drive just to be sure */ 790 if (status == 0xff) { 791 printf("ata%d: %s: no status, reselecting device\n", 792 scp->lun, device?"slave":"master"); 793 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 794 DELAY(1); 795 status = inb(scp->ioaddr + ATA_STATUS); 796 } 797 if (status == 0xff) 798 return -1; 799 scp->status = status; 800 if (!(status & ATA_S_BUSY)) { 801 if (status & ATA_S_ERROR) 802 scp->error = inb(scp->ioaddr + ATA_ERROR); 803 if ((status & mask) == mask) 804 return (status & ATA_S_ERROR); 805 } 806 if (timeout > 1000) { 807 timeout += 1000; 808 DELAY(1000); 809 } 810 else { 811 timeout += 10; 812 DELAY(10); 813 } 814 } 815 return -1; 816} 817 818int32_t 819ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 820 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 821 u_int32_t count, u_int32_t feature, int32_t flags) 822{ 823#ifdef ATA_DEBUG 824 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 825 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 826 scp->lun, scp->ioaddr, device, command, 827 cylinder, head, sector, count, flags); 828#endif 829 830 /* ready to issue command ? */ 831 if (ata_wait(scp, device, 0) < 0) { 832 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 833 scp->lun, device ? "slave" : "master", command, 834 scp->status, scp->error); 835 return -1; 836 } 837 outb(scp->ioaddr + ATA_FEATURE, feature); 838 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 839 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 840 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 841 outb(scp->ioaddr + ATA_SECTOR, sector); 842 outb(scp->ioaddr + ATA_COUNT, count); 843 844 switch (flags) { 845 case ATA_WAIT_INTR: 846 if (scp->active != ATA_IDLE) 847 printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active)); 848 scp->active = ATA_WAIT_INTR; 849 outb(scp->ioaddr + ATA_CMD, command); 850 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 851 printf("ata_command: timeout waiting for interrupt\n"); 852 scp->active = ATA_IDLE; 853 return -1; 854 } 855 break; 856 857 case ATA_WAIT_READY: 858 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 859 printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active)); 860 scp->active = ATA_WAIT_READY; 861 outb(scp->ioaddr + ATA_CMD, command); 862 if (ata_wait(scp, device, ATA_S_READY) < 0) { 863 printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n", 864 scp->lun, device ? "slave" : "master", command, 865 scp->status, scp->error); 866 scp->active = ATA_IDLE; 867 return -1; 868 } 869 scp->active = ATA_IDLE; 870 break; 871 872 case ATA_IMMEDIATE: 873 outb(scp->ioaddr + ATA_CMD, command); 874 break; 875 876 default: 877 printf("DANGER: illegal interrupt flag=%s\n", active2str(flags)); 878 } 879 return 0; 880} 881 882int8_t * 883ata_mode2str(int32_t mode) 884{ 885 switch (mode) { 886 case ATA_MODE_PIO: 887 return "PIO"; 888 case ATA_MODE_WDMA2: 889 return "DMA"; 890 case ATA_MODE_UDMA2: 891 return "UDMA33"; 892 case ATA_MODE_UDMA3: 893 return "UDMA3"; 894 case ATA_MODE_UDMA4: 895 return "UDMA66"; 896 default: 897 return "???"; 898 } 899} 900 901static int8_t * 902active2str(int32_t active) 903{ 904 switch (active) { 905 case ATA_IDLE: 906 return("ATA_IDLE"); 907 case ATA_WAIT_INTR: 908 return("ATA_WAIT_INTR"); 909 case ATA_ACTIVE_ATA: 910 return("ATA_ACTIVE_ATA"); 911 case ATA_ACTIVE_ATAPI: 912 return("ATA_ACTIVE_ATAPI"); 913 case ATA_REINITING: 914 return("ATA_REINITING"); 915 default: 916 return("UNKNOWN"); 917 } 918} 919 920void 921bswap(int8_t *buf, int32_t len) 922{ 923 u_int16_t *p = (u_int16_t*)(buf + len); 924 925 while (--p >= (u_int16_t*)buf) 926 *p = ntohs(*p); 927} 928 929void 930btrim(int8_t *buf, int32_t len) 931{ 932 int8_t *p; 933 934 for (p = buf; p < buf+len; ++p) 935 if (!*p) 936 *p = ' '; 937 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 938 *p = 0; 939} 940 941void 942bpack(int8_t *src, int8_t *dst, int32_t len) 943{ 944 int32_t i, j, blank; 945 946 for (i = j = blank = 0 ; i < len-1; i++) { 947 if (blank && src[i] == ' ') continue; 948 if (blank && src[i] != ' ') { 949 dst[j++] = src[i]; 950 blank = 0; 951 continue; 952 } 953 if (src[i] == ' ') { 954 blank = 1; 955 if (i == 0) 956 continue; 957 } 958 dst[j++] = src[i]; 959 } 960 dst[j] = 0x00; 961} 962