ata-all.c revision 52213
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 52213 1999-10-13 18:56:49Z dfr $ 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 78/* prototypes */ 79static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 80static void ataintr(void *); 81static int8_t *active2str(int32_t); 82 83/* local vars */ 84static int32_t atanlun = 2; 85struct ata_softc *atadevices[MAXATA]; 86static devclass_t ata_devclass; 87MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 88 89#if NISA > 0 90static struct isa_pnp_id ata_ids[] = { 91 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 92 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 93 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 94 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 95 {0} 96}; 97 98static int 99ata_isaprobe(device_t dev) 100{ 101 struct resource *port; 102 int rid; 103 int32_t ctlr, res; 104 int32_t lun; 105 106 /* Check isapnp ids */ 107 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 108 return (ENXIO); 109 110 /* Allocate the port range */ 111 rid = 0; 112 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 113 if (!port) 114 return (ENOMEM); 115 116 /* check if allready in use by a PCI device */ 117 for (ctlr = 0; ctlr < atanlun; ctlr++) { 118 if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){ 119 printf("ata-isa%d: already registered as ata%d\n", 120 device_get_unit(dev), ctlr); 121 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 122 return ENXIO; 123 } 124 } 125 126 lun = 0; 127 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 128 0, dev, &lun); 129 130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 131 132 if (res) { 133 isa_set_portsize(dev, res); 134 *(int *)device_get_softc(dev) = lun; 135 return 0; 136 } 137 return ENXIO; 138} 139 140static int 141ata_isaattach(device_t dev) 142{ 143 struct resource *port; 144 struct resource *irq; 145 void *ih; 146 int rid; 147 148 /* Allocate the port range and interrupt */ 149 rid = 0; 150 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 151 if (!port) 152 return (ENOMEM); 153 154 rid = 0; 155 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 156 if (!irq) { 157 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 158 return (ENOMEM); 159 } 160 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 161 atadevices[*(int *)device_get_softc(dev)], &ih); 162} 163 164static device_method_t ata_isa_methods[] = { 165 /* Device interface */ 166 DEVMETHOD(device_probe, ata_isaprobe), 167 DEVMETHOD(device_attach, ata_isaattach), 168 { 0, 0 } 169}; 170 171static driver_t ata_isa_driver = { 172 "ata", 173 ata_isa_methods, 174 sizeof(int), 175}; 176 177DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 178#endif 179 180#if NPCI > 0 181static const char * 182ata_pcimatch(device_t dev) 183{ 184 if (pci_get_class(dev) != PCIC_STORAGE || 185 (pci_get_subclass(dev) != PCIS_STORAGE_IDE)) 186 return NULL; 187 188 switch (pci_get_devid(dev)) { 189 /* supported chipsets */ 190 case 0x12308086: 191 return "Intel PIIX IDE controller"; 192 case 0x70108086: 193 return "Intel PIIX3 IDE controller"; 194 case 0x71118086: 195 return "Intel PIIX4 IDE controller"; 196 case 0x522910b9: 197 return "AcerLabs Aladdin IDE controller"; 198 case 0x4d33105a: 199 return "Promise Ultra/33 IDE controller"; 200 case 0x4d38105a: 201 return "Promise Ultra/66 IDE controller"; 202 case 0x00041103: 203 return "HighPoint HPT366 IDE controller"; 204 205 /* unsupported but known chipsets, generic DMA only */ 206 case 0x05711106: /* 82c586 */ 207 case 0x05961106: /* 82c596 */ 208 return "VIA Apollo IDE controller (generic mode)"; 209 case 0x06401095: 210 return "CMD 640 IDE controller (generic mode)"; 211 case 0x06461095: 212 return "CMD 646 IDE controller (generic mode)"; 213 case 0xc6931080: 214 return "Cypress 82C693 IDE controller (generic mode)"; 215 case 0x01021078: 216 return "Cyrix 5530 IDE controller (generic mode)"; 217 default: 218 if (pci_get_class(dev) == PCIC_STORAGE && 219 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 220 return "Unknown PCI IDE controller (generic mode)"; 221 } 222 return NULL; 223} 224 225static int 226ata_pciprobe(device_t dev) 227{ 228 const char *desc = ata_pcimatch(dev); 229 if (desc) { 230 device_set_desc(dev, desc); 231 return 0; 232 } 233 else 234 return ENXIO; 235} 236 237static int 238ata_pciattach(device_t dev) 239{ 240 int unit = device_get_unit(dev); 241 struct ata_softc *scp; 242 u_int32_t type; 243 u_int8_t class, subclass; 244 u_int32_t cmd; 245 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 246 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 247 int32_t lun; 248 249 /* set up vendor-specific stuff */ 250 type = pci_get_devid(dev); 251 class = pci_get_class(dev); 252 subclass = pci_get_subclass(dev); 253 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 254 255#ifdef ATA_DEBUG 256 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n", 257 unit, type, class, subclass, cmd, pci_get_progif(dev)); 258#endif 259 260 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 261 iobase_1 = IO_WD1; 262 altiobase_1 = iobase_1 + ATA_ALTPORT; 263 irq1 = 14; 264 } 265 else { 266 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 267 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 268 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc; 269 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 270 } 271 272 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 273 iobase_2 = IO_WD2; 274 altiobase_2 = iobase_2 + ATA_ALTPORT; 275 irq2 = 15; 276 } 277 else { 278 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 279 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 280 bmaddr_2 = (pci_read_config(dev, 0x20, 4) & 0xfffc) + ATA_BM_OFFSET1; 281 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 282 } 283 284 /* is this controller busmaster DMA capable ? */ 285 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 286 /* is busmastering support turned on ? */ 287 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) { 288 /* is there a valid port range to connect to ? */ 289 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) { 290 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 291 printf("ata-pci%d: Busmastering DMA supported\n", unit); 292 } 293 else 294 printf("ata-pci%d: Busmastering DMA not configured\n", unit); 295 } 296 else 297 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 298 } 299 else { 300 /* the Promise controllers need this to support burst mode */ 301 if (type == 0x4d33105a || type == 0x4d38105a) 302 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 303 304 /* Promise and HPT366 controllers support busmastering DMA */ 305 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) 306 printf("ata-pci%d: Busmastering DMA supported\n", unit); 307 308 /* we dont know this controller, disable busmastering DMA */ 309 else { 310 bmaddr_1 = bmaddr_2 = 0; 311 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 312 } 313 } 314 315 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 316 lun = 0; 317 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 318 scp = atadevices[lun]; 319 if (iobase_1 == IO_WD1) 320#ifdef __i386__ 321 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 322 &bio_imask, INTR_EXCL); 323#endif 324#ifdef __alpha__ 325 alpha_platform_setup_ide_intr(0, ataintr, scp); 326#endif 327 else { 328 struct resource *irq; 329 int rid = 0; 330 void *ih; 331 332 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 333 RF_SHAREABLE | RF_ACTIVE))) 334 printf("ata_pciattach: Unable to alloc interrupt\n"); 335 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 336 } 337 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 338 lun, iobase_1, isa_apic_irq(irq1), unit); 339 } 340 lun = 1; 341 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 342 scp = atadevices[lun]; 343 if (iobase_2 == IO_WD2) 344#ifdef __i386__ 345 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 346 &bio_imask, INTR_EXCL); 347#endif 348#ifdef __alpha__ 349 alpha_platform_setup_ide_intr(1, ataintr, scp); 350#endif 351 else { 352 struct resource *irq; 353 int rid = 0; 354 void *ih; 355 356 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 357 RF_SHAREABLE | RF_ACTIVE))) 358 printf("ata_pciattach: Unable to alloc interrupt\n"); 359 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 360 } 361 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 362 lun, iobase_2, isa_apic_irq(irq2), unit); 363 } 364 return 0; 365} 366 367static device_method_t ata_pci_methods[] = { 368 /* Device interface */ 369 DEVMETHOD(device_probe, ata_pciprobe), 370 DEVMETHOD(device_attach, ata_pciattach), 371 { 0, 0 } 372}; 373 374static driver_t ata_pci_driver = { 375 "ata-pci", 376 ata_pci_methods, 377 sizeof(int), 378}; 379 380DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 381#endif 382 383static int32_t 384ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 385 device_t dev, int32_t *unit) 386{ 387 struct ata_softc *scp; 388 int32_t lun, mask = 0; 389 u_int8_t status0, status1; 390 391 if (atanlun > MAXATA) { 392 printf("ata: unit out of range(%d)\n", atanlun); 393 return 0; 394 } 395 396 /* check if this is located at one of the std addresses */ 397 if (ioaddr == IO_WD1) 398 lun = 0; 399 else if (ioaddr == IO_WD2) 400 lun = 1; 401 else 402 lun = atanlun++; 403 404 if ((scp = atadevices[lun])) { 405 printf("ata%d: unit already attached\n", lun); 406 return 0; 407 } 408 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT); 409 if (scp == NULL) { 410 printf("ata%d: failed to allocate driver storage\n", lun); 411 return 0; 412 } 413 bzero(scp, sizeof(struct ata_softc)); 414 415 scp->ioaddr = ioaddr; 416 scp->altioaddr = altioaddr; 417 scp->lun = lun; 418 scp->unit = *unit; 419 scp->active = ATA_IDLE; 420 421 if (bootverbose) 422 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 423 scp->lun, scp->ioaddr, scp->altioaddr); 424 425 426 /* do we have any signs of ATA/ATAPI HW being present ? */ 427 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 428 DELAY(1); 429 status0 = inb(scp->ioaddr + ATA_STATUS); 430 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 431 DELAY(1); 432 status1 = inb(scp->ioaddr + ATA_STATUS); 433 if ((status0 & 0xf8) != 0xf8) 434 mask |= 0x01; 435 if ((status1 & 0xf8) != 0xf8) 436 mask |= 0x02; 437 if (bootverbose) 438 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 439 scp->lun, mask, status0, status1); 440 if (!mask) { 441 free(scp, M_DEVBUF); 442 return 0; 443 } 444 ata_reset(scp, &mask); 445 if (!mask) { 446 free(scp, M_DEVBUF); 447 return 0; 448 } 449 /* 450 * OK, we have at least one device on the chain, 451 * check for ATAPI signatures, if none check if its 452 * a good old ATA device. 453 */ 454 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 455 DELAY(1); 456 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 457 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 458 scp->devices |= ATA_ATAPI_MASTER; 459 } 460 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 461 DELAY(1); 462 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 463 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 464 scp->devices |= ATA_ATAPI_SLAVE; 465 } 466 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 467 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 468 DELAY(1); 469 outb(scp->ioaddr + ATA_ERROR, 0x58); 470 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 471 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 472 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 473 scp->devices |= ATA_ATA_MASTER; 474 } 475 } 476 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 477 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 478 DELAY(1); 479 outb(scp->ioaddr + ATA_ERROR, 0x58); 480 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 481 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 482 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 483 scp->devices |= ATA_ATA_SLAVE; 484 } 485 } 486 if (bootverbose) 487 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 488 if (!scp->devices) { 489 free(scp, M_DEVBUF); 490 return 0; 491 } 492 TAILQ_INIT(&scp->ata_queue); 493 TAILQ_INIT(&scp->atapi_queue); 494 *unit = scp->lun; 495 scp->dev = dev; 496 if (bmaddr) 497 scp->bmaddr = bmaddr; 498 atadevices[scp->lun] = scp; 499#if NAPM > 0 500 scp->resume_hook.ah_fun = (void *)ata_reinit; 501 scp->resume_hook.ah_arg = scp; 502 scp->resume_hook.ah_name = "ATA driver"; 503 scp->resume_hook.ah_order = APM_MID_ORDER; 504 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 505#endif 506 return ATA_IOSIZE; 507} 508 509static void 510ataintr(void *data) 511{ 512 struct ata_softc *scp =(struct ata_softc *)data; 513 514 /* is this interrupt really for this channel */ 515 if (scp->flags & ATA_DMA_ACTIVE) 516 if (!(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT)) 517 return; 518 if ((scp->status = inb(scp->ioaddr + ATA_STATUS)) == ATA_S_BUSY) /*XXX SOS*/ 519 return; 520 521 /* find & call the responsible driver to process this interrupt */ 522 switch (scp->active) { 523#if NATADISK > 0 524 case ATA_ACTIVE_ATA: 525 if (scp->running && (ad_interrupt(scp->running) == ATA_OP_CONTINUES)) 526 return; 527 break; 528#endif 529#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 530 case ATA_ACTIVE_ATAPI: 531 if (scp->running && (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)) 532 return; 533 break; 534#endif 535 case ATA_WAIT_INTR: 536 wakeup((caddr_t)scp); 537 break; 538 539 case ATA_REINITING: 540 return; 541 542 case ATA_IGNORE_INTR: 543 break; 544 545 default: 546 case ATA_IDLE: 547#ifdef ATA_DEBUG 548 { 549 static int32_t intr_count = 0; 550 if (intr_count++ < 10) 551 printf("ata%d: unwanted interrupt %d status = %02x\n", 552 scp->lun, intr_count, scp->status); 553 } 554#endif 555 return; 556 } 557 scp->active = ATA_IDLE; 558 scp->running = NULL; 559 ata_start(scp); 560} 561 562void 563ata_start(struct ata_softc *scp) 564{ 565 struct ad_request *ad_request; 566 struct atapi_request *atapi_request; 567 568#ifdef ATA_DEBUG 569 printf("ata_start: entered\n"); 570#endif 571 if (scp->active != ATA_IDLE) 572 return; 573 574#if NATADISK > 0 575 /* find & call the responsible driver if anything on the ATA queue */ 576 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 577 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 578 scp->active = ATA_ACTIVE_ATA; 579 scp->running = ad_request; 580 ad_transfer(ad_request); 581#ifdef ATA_DEBUG 582 printf("ata_start: started ata, leaving\n"); 583#endif 584 return; 585 } 586#endif 587#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 588 /* 589 * find & call the responsible driver if anything on the ATAPI queue. 590 * check for device busy by polling the DSC bit, if busy, check 591 * for requests to the other device on the channel (if any). 592 * if the other device is an ATA disk it already had its chance above. 593 * if no request can be served, timeout a call to ata_start. 594 */ 595 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 596 struct atapi_softc *atp = atapi_request->device; 597 static int32_t interval = 1; 598 599 if (atp->flags & ATAPI_F_DSC_USED) { 600 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 601 DELAY(1); 602 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 603 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 604 if (atapi_request->device->unit != atp->unit) { 605 struct atapi_softc *tmp = atapi_request->device; 606 607 outb(tmp->controller->ioaddr + ATA_DRIVE, 608 ATA_D_IBM | tmp->unit); 609 DELAY(1); 610 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 611 atapi_request = NULL; 612 break; 613 } 614 } 615 } 616 if (!atapi_request) { 617 timeout((timeout_t *)ata_start, atp->controller, interval++); 618 return; 619 } 620 else 621 interval = 1; 622 } 623 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 624 scp->active = ATA_ACTIVE_ATAPI; 625 scp->running = atapi_request; 626 atapi_transfer(atapi_request); 627#ifdef ATA_DEBUG 628 printf("ata_start: started atapi, leaving\n"); 629#endif 630 return; 631 } 632#endif 633} 634 635void 636ata_reset(struct ata_softc *scp, int32_t *mask) 637{ 638 int32_t timeout; 639 int8_t status0, status1; 640 641 /* reset channel */ 642 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 643 DELAY(1); 644 inb(scp->ioaddr + ATA_STATUS); 645 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 646 DELAY(10000); 647 outb(scp->altioaddr, ATA_A_IDS); 648 DELAY(10000); 649 inb(scp->ioaddr + ATA_ERROR); 650 DELAY(3000); 651 652 /* wait for BUSY to go inactive */ 653 for (timeout = 0; timeout < 310000; timeout++) { 654 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 655 DELAY(1); 656 status0 = inb(scp->ioaddr + ATA_STATUS); 657 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 658 DELAY(1); 659 status1 = inb(scp->ioaddr + ATA_STATUS); 660 if (*mask == 0x01) /* wait for master only */ 661 if (!(status0 & ATA_S_BUSY)) 662 break; 663 if (*mask == 0x02) /* wait for slave only */ 664 if (!(status1 & ATA_S_BUSY)) 665 break; 666 if (*mask == 0x03) /* wait for both master & slave */ 667 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 668 break; 669 DELAY(100); 670 } 671 DELAY(1); 672 outb(scp->altioaddr, ATA_A_4BIT); 673 if (status0 & ATA_S_BUSY) 674 *mask &= ~0x01; 675 if (status1 & ATA_S_BUSY) 676 *mask &= ~0x02; 677 if (bootverbose) 678 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 679 scp->lun, *mask, status0, status1); 680} 681 682int32_t 683ata_reinit(struct ata_softc *scp) 684{ 685 int32_t mask = 0, omask; 686 687 scp->active = ATA_REINITING; 688 scp->running = NULL; 689 printf("ata%d: resetting devices .. ", scp->lun); 690 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 691 mask |= 0x01; 692 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 693 mask |= 0x02; 694 omask = mask; 695 ata_reset(scp, &mask); 696 if (omask != mask) 697 printf(" device dissapeared! %d ", omask & ~mask); 698 699#if NATADISK > 0 700 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 701 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 702 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 703 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 704#endif 705#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 706 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 707 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 708 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 709 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 710#endif 711 printf("done\n"); 712 scp->active = ATA_IDLE; 713 ata_start(scp); 714 return 0; 715} 716 717int32_t 718ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 719{ 720 u_int8_t status; 721 u_int32_t timeout = 0; 722 723 while (timeout <= 5000000) { /* timeout 5 secs */ 724 status = inb(scp->ioaddr + ATA_STATUS); 725 726 /* if drive fails status, reselect the drive just to be sure */ 727 if (status == 0xff) { 728 printf("ata%d: %s: no status, reselecting device\n", 729 scp->lun, device?"slave":"master"); 730 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 731 DELAY(1); 732 status = inb(scp->ioaddr + ATA_STATUS); 733 } 734 if (status == 0xff) 735 return -1; 736 scp->status = status; 737 if (!(status & ATA_S_BUSY)) { 738 if (status & ATA_S_ERROR) 739 scp->error = inb(scp->ioaddr + ATA_ERROR); 740 if ((status & mask) == mask) 741 return (status & ATA_S_ERROR); 742 } 743 if (timeout > 1000) { 744 timeout += 1000; 745 DELAY(1000); 746 } 747 else { 748 timeout += 10; 749 DELAY(10); 750 } 751 } 752 return -1; 753} 754 755int32_t 756ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 757 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 758 u_int32_t count, u_int32_t feature, int32_t flags) 759{ 760#ifdef ATA_DEBUG 761 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 762 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 763 scp->lun, scp->ioaddr, device, command, 764 cylinder, head, sector, count, flags); 765#endif 766 767 /* ready to issue command ? */ 768 if (ata_wait(scp, device, 0) < 0) { 769 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 770 scp->lun, device ? "slave" : "master", command, 771 scp->status, scp->error); 772 return -1; 773 } 774 outb(scp->ioaddr + ATA_FEATURE, feature); 775 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 776 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 777 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 778 outb(scp->ioaddr + ATA_SECTOR, sector); 779 outb(scp->ioaddr + ATA_COUNT, count); 780 781 switch (flags) { 782 case ATA_WAIT_INTR: 783 if (scp->active != ATA_IDLE) 784 printf("DANGER wait_intr active=%s\n", active2str(scp->active)); 785 scp->active = ATA_WAIT_INTR; 786 outb(scp->ioaddr + ATA_CMD, command); 787 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 788 printf("ata_command: timeout waiting for interrupt\n"); 789 scp->active = ATA_IDLE; 790 return -1; 791 } 792 break; 793 794 case ATA_IGNORE_INTR: 795 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 796 printf("DANGER ignore_intr active=%s\n", active2str(scp->active)); 797 if (scp->active != ATA_REINITING) 798 scp->active = ATA_IGNORE_INTR; 799 outb(scp->ioaddr + ATA_CMD, command); 800 break; 801 802 case ATA_IMMEDIATE: 803 outb(scp->ioaddr + ATA_CMD, command); 804 break; 805 806 default: 807 printf("DANGER illegal interrupt flag=%s\n", active2str(flags)); 808 } 809#ifdef ATA_DEBUG 810 printf("ata_command: leaving\n"); 811#endif 812 return 0; 813} 814 815int8_t * 816ata_mode2str(int32_t mode) 817{ 818 switch (mode) { 819 case ATA_MODE_PIO: 820 return "PIO"; 821 case ATA_MODE_WDMA2: 822 return "DMA"; 823 case ATA_MODE_UDMA2: 824 return "UDMA33"; 825 case ATA_MODE_UDMA3: 826 return "UDMA3"; 827 case ATA_MODE_UDMA4: 828 return "UDMA66"; 829 default: 830 return "???"; 831 } 832} 833 834static int8_t * 835active2str(int32_t active) 836{ 837 switch (active) { 838 case ATA_IDLE: 839 return("ATA_IDLE"); 840 case ATA_WAIT_INTR: 841 return("ATA_WAIT_INTR"); 842 case ATA_IGNORE_INTR: 843 return("ATA_IGNORE_INTR"); 844 case ATA_ACTIVE_ATA: 845 return("ATA_ACTIVE_ATA"); 846 case ATA_ACTIVE_ATAPI: 847 return("ATA_ACTIVE_ATAPI"); 848 case ATA_REINITING: 849 return("ATA_REINITING"); 850 default: 851 return("UNKNOWN"); 852 } 853} 854 855void 856bswap(int8_t *buf, int32_t len) 857{ 858 u_int16_t *p = (u_int16_t*)(buf + len); 859 860 while (--p >= (u_int16_t*)buf) 861 *p = ntohs(*p); 862} 863 864void 865btrim(int8_t *buf, int32_t len) 866{ 867 int8_t *p; 868 869 for (p = buf; p < buf+len; ++p) 870 if (!*p) 871 *p = ' '; 872 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 873 *p = 0; 874} 875 876void 877bpack(int8_t *src, int8_t *dst, int32_t len) 878{ 879 int32_t i, j, blank; 880 881 for (i = j = blank = 0 ; i < len-1; i++) { 882 if (blank && src[i] == ' ') continue; 883 if (blank && src[i] != ' ') { 884 dst[j++] = src[i]; 885 blank = 0; 886 continue; 887 } 888 if (src[i] == ' ') { 889 blank = 1; 890 if (i == 0) 891 continue; 892 } 893 dst[j++] = src[i]; 894 } 895 dst[j] = 0x00; 896} 897