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