ata-all.c revision 53719
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 53719 1999-11-26 19:24:26Z 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 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 case 0x05711106: /* 82c586 & 82c686 */ 205 return "VIA Apollo IDE controller"; 206 207 /* unsupported but known chipsets, generic DMA only */ 208 case 0x05961106: /* 82c596 */ 209 return "VIA Apollo IDE controller (generic mode)"; 210 case 0x06401095: 211 return "CMD 640 IDE controller (generic mode)"; 212 case 0x06461095: 213 return "CMD 646 IDE controller (generic mode)"; 214 case 0xc6931080: 215 return "Cypress 82C693 IDE controller (generic mode)"; 216 case 0x01021078: 217 return "Cyrix 5530 IDE controller (generic mode)"; 218 default: 219 if (pci_get_class(dev) == PCIC_STORAGE && 220 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 221 return "Unknown PCI IDE controller (generic mode)"; 222 } 223 return NULL; 224} 225 226static int 227ata_pciprobe(device_t dev) 228{ 229 const char *desc = ata_pcimatch(dev); 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); 842 return -1; 843 } 844 break; 845 846 case ATA_IMMEDIATE: 847 outb(scp->ioaddr + ATA_CMD, command); 848 break; 849 850 default: 851 printf("DANGER: illegal interrupt flag=%s\n", active2str(flags)); 852 } 853#ifdef ATA_DEBUG 854 printf("ata_command: leaving\n"); 855#endif 856 return 0; 857} 858 859int8_t * 860ata_mode2str(int32_t mode) 861{ 862 switch (mode) { 863 case ATA_MODE_PIO: 864 return "PIO"; 865 case ATA_MODE_WDMA2: 866 return "DMA"; 867 case ATA_MODE_UDMA2: 868 return "UDMA33"; 869 case ATA_MODE_UDMA3: 870 return "UDMA3"; 871 case ATA_MODE_UDMA4: 872 return "UDMA66"; 873 default: 874 return "???"; 875 } 876} 877 878static int8_t * 879active2str(int32_t active) 880{ 881 switch (active) { 882 case ATA_IDLE: 883 return("ATA_IDLE"); 884 case ATA_WAIT_INTR: 885 return("ATA_WAIT_INTR"); 886 case ATA_ACTIVE_ATA: 887 return("ATA_ACTIVE_ATA"); 888 case ATA_ACTIVE_ATAPI: 889 return("ATA_ACTIVE_ATAPI"); 890 case ATA_REINITING: 891 return("ATA_REINITING"); 892 default: 893 return("UNKNOWN"); 894 } 895} 896 897void 898bswap(int8_t *buf, int32_t len) 899{ 900 u_int16_t *p = (u_int16_t*)(buf + len); 901 902 while (--p >= (u_int16_t*)buf) 903 *p = ntohs(*p); 904} 905 906void 907btrim(int8_t *buf, int32_t len) 908{ 909 int8_t *p; 910 911 for (p = buf; p < buf+len; ++p) 912 if (!*p) 913 *p = ' '; 914 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 915 *p = 0; 916} 917 918void 919bpack(int8_t *src, int8_t *dst, int32_t len) 920{ 921 int32_t i, j, blank; 922 923 for (i = j = blank = 0 ; i < len-1; i++) { 924 if (blank && src[i] == ' ') continue; 925 if (blank && src[i] != ' ') { 926 dst[j++] = src[i]; 927 blank = 0; 928 continue; 929 } 930 if (src[i] == ' ') { 931 blank = 1; 932 if (i == 0) 933 continue; 934 } 935 dst[j++] = src[i]; 936 } 937 dst[j] = 0x00; 938} 939