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$ 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 */ 79 static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 80 static void ataintr(void *); 81 static int8_t *active2str(int32_t); 82 83 /* local vars */ 84 static int32_t atanlun = 2; 85 struct ata_softc *atadevices[MAXATA]; 86 static devclass_t ata_devclass; 87 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 88 89 #if NISA > 0 90 static 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 98 static int 99 ata_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 140 static int 141 ata_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 164 static 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 171 static driver_t ata_isa_driver = { 172 "ata", 173 ata_isa_methods, 174 sizeof(int), 175 }; 176 177 DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 178 #endif 179 180 #if NPCI > 0 181 static const char * 182 ata_pcimatch(device_t dev) 183 { 184 if (pci_get_class(dev) != PCIC_STORAGE) 185 return NULL; 186 187 switch (pci_get_devid(dev)) { 188 /* supported chipsets */ 189 case 0x12308086: 190 return "Intel PIIX IDE controller"; 191 case 0x70108086: 192 return "Intel PIIX3 IDE controller"; 193 case 0x71118086: 194 return "Intel PIIX4 IDE controller"; 195 case 0x522910b9: 196 return "AcerLabs Aladdin IDE controller"; 197 case 0x4d33105a: 198 return "Promise Ultra/33 IDE controller"; 199 case 0x4d38105a: 200 return "Promise Ultra/66 IDE controller"; 201 case 0x00041103: 202 return "HighPoint HPT366 IDE controller"; 203 204 /* unsupported but known chipsets, generic DMA only */ 205 case 0x05711106: /* 82c586 */ 206 case 0x05961106: /* 82c596 */ 207 return "VIA Apollo IDE controller (generic mode)"; 208 case 0x06401095: 209 return "CMD 640 IDE controller (generic mode)"; 210 case 0x06461095: 211 return "CMD 646 IDE controller (generic mode)"; 212 case 0xc6931080: 213 return "Cypress 82C693 IDE controller (generic mode)"; 214 case 0x01021078: 215 return "Cyrix 5530 IDE controller (generic mode)"; 216 default: 217 if (pci_get_class(dev) == PCIC_STORAGE && 218 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 219 return "Unknown PCI IDE controller (generic mode)"; 220 } 221 return NULL; 222 } 223 224 static int 225 ata_pciprobe(device_t dev) 226 { 227 const char *desc = ata_pcimatch(dev); 228 if (desc) { 229 device_set_desc(dev, desc); 230 return 0; 231 } 232 else 233 return ENXIO; 234 } 235 236 static int 237 ata_pciattach(device_t dev) 238 { 239 int unit = device_get_unit(dev); 240 struct ata_softc *scp; 241 u_int32_t type; 242 u_int8_t class, subclass; 243 u_int32_t cmd; 244 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 245 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 246 struct resource *irq = NULL; 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 int rid = 0; 329 void *ih; 330 331 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 332 RF_SHAREABLE | RF_ACTIVE))) 333 printf("ata_pciattach: Unable to alloc interrupt\n"); 334 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 335 } 336 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 337 lun, iobase_1, isa_apic_irq(irq1), unit); 338 } 339 lun = 1; 340 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 341 scp = atadevices[lun]; 342 if (iobase_2 == IO_WD2) 343 #ifdef __i386__ 344 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 345 &bio_imask, INTR_EXCL); 346 #endif 347 #ifdef __alpha__ 348 alpha_platform_setup_ide_intr(1, ataintr, scp); 349 #endif 350 else { 351 int rid = 0; 352 void *ih; 353 354 if (irq1 != irq2 || irq == NULL) { 355 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 356 RF_SHAREABLE | RF_ACTIVE))) 357 printf("ata_pciattach: Unable to alloc interrupt\n"); 358 } 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 367 static 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 374 static driver_t ata_pci_driver = { 375 "ata-pci", 376 ata_pci_methods, 377 sizeof(int), 378 }; 379 380 DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 381 #endif 382 383 static int32_t 384 ata_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 509 static void 510 ataintr(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) 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) 526 return; 527 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES) 528 return; 529 break; 530 #endif 531 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 532 case ATA_ACTIVE_ATAPI: 533 if (!scp->running) 534 return; 535 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 536 return; 537 break; 538 #endif 539 case ATA_WAIT_INTR: 540 wakeup((caddr_t)scp); 541 break; 542 543 case ATA_WAIT_READY: 544 break; 545 546 case ATA_REINITING: 547 return; 548 549 default: 550 case ATA_IDLE: 551 #ifdef ATA_DEBUG 552 { 553 static int32_t intr_count = 0; 554 if (intr_count++ < 10) 555 printf("ata%d: unwanted interrupt %d status = %02x\n", 556 scp->lun, intr_count, scp->status); 557 } 558 #endif 559 return; 560 } 561 scp->active = ATA_IDLE; 562 scp->running = NULL; 563 ata_start(scp); 564 } 565 566 void 567 ata_start(struct ata_softc *scp) 568 { 569 struct ad_request *ad_request; 570 struct atapi_request *atapi_request; 571 572 #ifdef ATA_DEBUG 573 printf("ata_start: entered\n"); 574 #endif 575 if (scp->active != ATA_IDLE) 576 return; 577 578 #if NATADISK > 0 579 /* find & call the responsible driver if anything on the ATA queue */ 580 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 581 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 582 scp->active = ATA_ACTIVE_ATA; 583 scp->running = ad_request; 584 ad_transfer(ad_request); 585 #ifdef ATA_DEBUG 586 printf("ata_start: started ata, leaving\n"); 587 #endif 588 return; 589 } 590 #endif 591 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 592 /* 593 * find & call the responsible driver if anything on the ATAPI queue. 594 * check for device busy by polling the DSC bit, if busy, check 595 * for requests to the other device on the channel (if any). 596 * if the other device is an ATA disk it already had its chance above. 597 * if no request can be served, timeout a call to ata_start. 598 */ 599 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 600 struct atapi_softc *atp = atapi_request->device; 601 static int32_t interval = 1; 602 603 if (atp->flags & ATAPI_F_DSC_USED) { 604 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 605 DELAY(1); 606 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 607 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 608 if (atapi_request->device->unit != atp->unit) { 609 struct atapi_softc *tmp = atapi_request->device; 610 611 outb(tmp->controller->ioaddr + ATA_DRIVE, 612 ATA_D_IBM | tmp->unit); 613 DELAY(1); 614 if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC) 615 atapi_request = NULL; 616 break; 617 } 618 } 619 } 620 if (!atapi_request) { 621 timeout((timeout_t *)ata_start, atp->controller, interval++); 622 return; 623 } 624 else 625 interval = 1; 626 } 627 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 628 scp->active = ATA_ACTIVE_ATAPI; 629 scp->running = atapi_request; 630 atapi_transfer(atapi_request); 631 #ifdef ATA_DEBUG 632 printf("ata_start: started atapi, leaving\n"); 633 #endif 634 return; 635 } 636 #endif 637 } 638 639 void 640 ata_reset(struct ata_softc *scp, int32_t *mask) 641 { 642 int32_t timeout; 643 int8_t status0, status1; 644 645 /* reset channel */ 646 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 647 DELAY(1); 648 inb(scp->ioaddr + ATA_STATUS); 649 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 650 DELAY(10000); 651 outb(scp->altioaddr, ATA_A_IDS); 652 DELAY(10000); 653 inb(scp->ioaddr + ATA_ERROR); 654 DELAY(3000); 655 656 /* wait for BUSY to go inactive */ 657 for (timeout = 0; timeout < 310000; timeout++) { 658 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 659 DELAY(1); 660 status0 = inb(scp->ioaddr + ATA_STATUS); 661 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 662 DELAY(1); 663 status1 = inb(scp->ioaddr + ATA_STATUS); 664 if (*mask == 0x01) /* wait for master only */ 665 if (!(status0 & ATA_S_BUSY)) 666 break; 667 if (*mask == 0x02) /* wait for slave only */ 668 if (!(status1 & ATA_S_BUSY)) 669 break; 670 if (*mask == 0x03) /* wait for both master & slave */ 671 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 672 break; 673 DELAY(100); 674 } 675 DELAY(1); 676 outb(scp->altioaddr, ATA_A_4BIT); 677 if (status0 & ATA_S_BUSY) 678 *mask &= ~0x01; 679 if (status1 & ATA_S_BUSY) 680 *mask &= ~0x02; 681 if (bootverbose) 682 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 683 scp->lun, *mask, status0, status1); 684 } 685 686 int32_t 687 ata_reinit(struct ata_softc *scp) 688 { 689 int32_t mask = 0, omask; 690 691 scp->active = ATA_REINITING; 692 scp->running = NULL; 693 printf("ata%d: resetting devices .. ", scp->lun); 694 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 695 mask |= 0x01; 696 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 697 mask |= 0x02; 698 omask = mask; 699 ata_reset(scp, &mask); 700 if (omask != mask) 701 printf(" device dissapeared! %d ", omask & ~mask); 702 703 #if NATADISK > 0 704 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 705 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 706 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 707 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 708 #endif 709 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 710 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 711 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 712 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 713 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 714 #endif 715 printf("done\n"); 716 scp->active = ATA_IDLE; 717 ata_start(scp); 718 return 0; 719 } 720 721 int32_t 722 ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 723 { 724 u_int8_t status; 725 u_int32_t timeout = 0; 726 727 while (timeout <= 5000000) { /* timeout 5 secs */ 728 status = inb(scp->ioaddr + ATA_STATUS); 729 730 /* if drive fails status, reselect the drive just to be sure */ 731 if (status == 0xff) { 732 printf("ata%d: %s: no status, reselecting device\n", 733 scp->lun, device?"slave":"master"); 734 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 735 DELAY(1); 736 status = inb(scp->ioaddr + ATA_STATUS); 737 } 738 if (status == 0xff) 739 return -1; 740 scp->status = status; 741 if (!(status & ATA_S_BUSY)) { 742 if (status & ATA_S_ERROR) 743 scp->error = inb(scp->ioaddr + ATA_ERROR); 744 if ((status & mask) == mask) 745 return (status & ATA_S_ERROR); 746 } 747 if (timeout > 1000) { 748 timeout += 1000; 749 DELAY(1000); 750 } 751 else { 752 timeout += 10; 753 DELAY(10); 754 } 755 } 756 return -1; 757 } 758 759 int32_t 760 ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 761 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 762 u_int32_t count, u_int32_t feature, int32_t flags) 763 { 764 #ifdef ATA_DEBUG 765 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 766 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 767 scp->lun, scp->ioaddr, device, command, 768 cylinder, head, sector, count, flags); 769 #endif 770 771 /* ready to issue command ? */ 772 if (ata_wait(scp, device, 0) < 0) { 773 printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n", 774 scp->lun, device ? "slave" : "master", command, 775 scp->status, scp->error); 776 return -1; 777 } 778 outb(scp->ioaddr + ATA_FEATURE, feature); 779 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 780 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 781 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 782 outb(scp->ioaddr + ATA_SECTOR, sector); 783 outb(scp->ioaddr + ATA_COUNT, count); 784 785 switch (flags) { 786 case ATA_WAIT_INTR: 787 if (scp->active != ATA_IDLE) 788 printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active)); 789 scp->active = ATA_WAIT_INTR; 790 outb(scp->ioaddr + ATA_CMD, command); 791 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 792 printf("ata_command: timeout waiting for interrupt\n"); 793 scp->active = ATA_IDLE; 794 return -1; 795 } 796 break; 797 798 case ATA_WAIT_READY: 799 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 800 printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active)); 801 scp->active = ATA_WAIT_READY; 802 outb(scp->ioaddr + ATA_CMD, command); 803 if (ata_wait(scp, device, ATA_S_READY) < 0) { 804 printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n", 805 scp->lun, device ? "slave" : "master", command, 806 scp->status, scp->error); 807 return -1; 808 } 809 break; 810 811 case ATA_IMMEDIATE: 812 outb(scp->ioaddr + ATA_CMD, command); 813 break; 814 815 default: 816 printf("DANGER: illegal interrupt flag=%s\n", active2str(flags)); 817 } 818 #ifdef ATA_DEBUG 819 printf("ata_command: leaving\n"); 820 #endif 821 return 0; 822 } 823 824 int8_t * 825 ata_mode2str(int32_t mode) 826 { 827 switch (mode) { 828 case ATA_MODE_PIO: 829 return "PIO"; 830 case ATA_MODE_WDMA2: 831 return "DMA"; 832 case ATA_MODE_UDMA2: 833 return "UDMA33"; 834 case ATA_MODE_UDMA3: 835 return "UDMA3"; 836 case ATA_MODE_UDMA4: 837 return "UDMA66"; 838 default: 839 return "???"; 840 } 841 } 842 843 static int8_t * 844 active2str(int32_t active) 845 { 846 switch (active) { 847 case ATA_IDLE: 848 return("ATA_IDLE"); 849 case ATA_WAIT_INTR: 850 return("ATA_WAIT_INTR"); 851 case ATA_ACTIVE_ATA: 852 return("ATA_ACTIVE_ATA"); 853 case ATA_ACTIVE_ATAPI: 854 return("ATA_ACTIVE_ATAPI"); 855 case ATA_REINITING: 856 return("ATA_REINITING"); 857 default: 858 return("UNKNOWN"); 859 } 860 } 861 862 void 863 bswap(int8_t *buf, int32_t len) 864 { 865 u_int16_t *p = (u_int16_t*)(buf + len); 866 867 while (--p >= (u_int16_t*)buf) 868 *p = ntohs(*p); 869 } 870 871 void 872 btrim(int8_t *buf, int32_t len) 873 { 874 int8_t *p; 875 876 for (p = buf; p < buf+len; ++p) 877 if (!*p) 878 *p = ' '; 879 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 880 *p = 0; 881 } 882 883 void 884 bpack(int8_t *src, int8_t *dst, int32_t len) 885 { 886 int32_t i, j, blank; 887 888 for (i = j = blank = 0 ; i < len-1; i++) { 889 if (blank && src[i] == ' ') continue; 890 if (blank && src[i] != ' ') { 891 dst[j++] = src[i]; 892 blank = 0; 893 continue; 894 } 895 if (src[i] == ' ') { 896 blank = 1; 897 if (i == 0) 898 continue; 899 } 900 dst[j++] = src[i]; 901 } 902 dst[j] = 0x00; 903 } 904