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 * $Id: ata-all.c,v 1.13 1999/05/17 15:58:44 sos Exp $ 29 */ 30 31 #include "ata.h" 32 #if NATA > 0 33 #include "isa.h" 34 #include "pci.h" 35 #include "atadisk.h" 36 #include "opt_global.h" 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/interrupt.h> 41 #include <sys/conf.h> 42 #include <sys/module.h> 43 #include <sys/bus.h> 44 #include <sys/buf.h> 45 #include <sys/malloc.h> 46 #include <sys/devicestat.h> 47 #include <vm/vm.h> 48 #include <vm/pmap.h> 49 #include <machine/resource.h> 50 #include <machine/bus.h> 51 #include <sys/rman.h> 52 #include <machine/clock.h> 53 #ifdef __i386__ 54 #include <machine/smp.h> 55 #include <i386/isa/intr_machdep.h> 56 #endif 57 #if NPCI > 0 58 #include <pci/pcivar.h> 59 #include <pci/pcireg.h> 60 #endif 61 #include <isa/isavar.h> 62 #include <isa/isareg.h> 63 #include <dev/ata/ata-all.h> 64 #include <dev/ata/ata-disk.h> 65 #include <dev/ata/atapi-all.h> 66 67 /* misc defines */ 68 #define UNIT(dev) (dev>>3 & 0x1f) /* assume 8 minor # per unit */ 69 #define MIN(a,b) ((a)>(b)?(b):(a)) 70 #if SMP == 0 71 #define isa_apic_irq(x) x 72 #endif 73 74 /* prototypes */ 75 #if NPCI > 0 76 static void promise_intr(void *); 77 #endif 78 static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 79 static void ataintr(void *); 80 81 static int32_t atanlun = 0; 82 struct ata_softc *atadevices[MAXATA]; 83 static devclass_t ata_devclass; 84 85 #if NISA > 0 86 87 static int 88 ata_isaprobe(device_t dev) 89 { 90 struct resource *port; 91 int rid; 92 int32_t ctlr, res; 93 int32_t lun; 94 95 /* allocate the port range */ 96 rid = 0; 97 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 98 if (!port) 99 return (ENOMEM); 100 101 /* check if allready in use by a PCI device */ 102 for (ctlr = 0; ctlr < atanlun; ctlr++) { 103 if (atadevices[ctlr]->ioaddr == rman_get_start(port)) { 104 printf("ata-isa%d: already registered as ata%d\n", 105 device_get_unit(dev), ctlr); 106 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 107 return ENXIO; 108 } 109 } 110 111 lun = 0; 112 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 113 0, dev, &lun); 114 115 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 116 117 if (res) { 118 isa_set_portsize(dev, res); 119 *(int *)device_get_softc(dev) = lun; 120 return 0; 121 } 122 123 return ENXIO; 124 } 125 126 static int 127 ata_isaattach(device_t dev) 128 { 129 struct resource *port; 130 struct resource *irq; 131 void *ih; 132 int rid; 133 134 /* Allocate the port range and interrupt */ 135 rid = 0; 136 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 137 if (!port) 138 return (ENOMEM); 139 140 rid = 0; 141 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 142 if (!irq) { 143 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 144 return (ENOMEM); 145 } 146 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 147 atadevices[*(int *)device_get_softc(dev)], &ih); 148 } 149 150 static device_method_t ata_isa_methods[] = { 151 /* Device interface */ 152 DEVMETHOD(device_probe, ata_isaprobe), 153 DEVMETHOD(device_attach, ata_isaattach), 154 { 0, 0 } 155 }; 156 157 static driver_t ata_isa_driver = { 158 "ata", 159 ata_isa_methods, 160 sizeof(int), 161 }; 162 163 DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 164 165 #endif 166 167 #if NPCI > 0 168 169 static const char * 170 ata_pcimatch(device_t dev) 171 { 172 u_int32_t data; 173 174 data = pci_read_config(dev, PCI_CLASS_REG, 4); 175 if (pci_get_class(dev) == PCIC_STORAGE && 176 (pci_get_subclass(dev) == PCIS_STORAGE_IDE || 177 pci_get_subclass(dev) == PCIS_STORAGE_RAID)) { 178 switch (pci_get_devid(dev)) { 179 case 0x12308086: 180 return "Intel PIIX IDE controller"; 181 case 0x70108086: 182 return "Intel PIIX3 IDE controller"; 183 case 0x71118086: 184 return "Intel PIIX4 IDE controller"; 185 case 0x4d33105a: 186 return "Promise Ultra/33 IDE controller"; 187 case 0x522910b9: 188 return "AcerLabs Aladdin IDE controller"; 189 #if 0 190 case 0x05711106: 191 return "VIA Apollo IDE controller"; 192 case 0x06401095: 193 return "CMD 640 IDE controller"; 194 case 0x06461095: 195 return "CMD 646 IDE controller"; 196 case 0xc6931080: 197 return "Cypress 82C693 IDE controller"; 198 case 0x01021078: 199 return "Cyrix 5530 IDE controller"; 200 #endif 201 default: 202 return "Unknown PCI IDE controller"; 203 } 204 } 205 return NULL; 206 } 207 208 static int 209 ata_pciprobe(device_t dev) 210 { 211 const char *desc = ata_pcimatch(dev); 212 if (desc) { 213 device_set_desc(dev, desc); 214 return 0; 215 } 216 else 217 return ENXIO; 218 } 219 220 static int 221 ata_pciattach(device_t dev) 222 { 223 int unit = device_get_unit(dev); 224 struct ata_softc *scp; 225 u_int32_t type; 226 u_int8_t class, subclass; 227 u_int32_t cmd; 228 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 229 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 230 int32_t lun; 231 232 /* set up vendor-specific stuff */ 233 type = pci_get_devid(dev); 234 class = pci_get_class(dev); 235 subclass = pci_get_subclass(dev); 236 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 237 238 #ifdef ATA_DEBUG 239 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n", 240 unit, type, class, subclass, cmd); 241 #endif 242 243 /* if this is a Promise controller handle it specially */ 244 if (type == 0x4d33105a) { 245 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 246 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 247 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 248 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 249 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 250 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc; 251 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 252 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 253 printf("ata-pci%d: Busmastering DMA supported\n", unit); 254 } 255 /* everybody else seems to do it this way */ 256 else { 257 if ((unit == 0) && 258 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) { 259 iobase_1 = IO_WD1; 260 altiobase_1 = iobase_1 + ATA_ALTPORT; 261 irq1 = 14; 262 } 263 else { 264 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 265 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 266 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 267 } 268 if ((unit == 0) && 269 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) { 270 iobase_2 = IO_WD2; 271 altiobase_2 = iobase_2 + ATA_ALTPORT; 272 irq2 = 15; 273 } 274 else { 275 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 276 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 277 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 278 } 279 280 /* is this controller busmaster capable ? */ 281 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) { 282 /* is busmastering support turned on ? */ 283 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) { 284 /* is there a valid port range to connect to ? */ 285 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) { 286 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 287 printf("ata-pci%d: Busmastering DMA supported\n", unit); 288 } 289 else 290 printf("ata-pci%d: Busmastering DMA not configured\n",unit); 291 } 292 else 293 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 294 } 295 else 296 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 297 } 298 299 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 300 lun = 0; 301 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 302 scp = atadevices[lun]; 303 if (iobase_1 == IO_WD1) 304 #ifdef __i386__ 305 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp, 306 &bio_imask, INTR_EXCL); 307 #endif 308 #ifdef __alpha__ 309 alpha_platform_setup_ide_intr(0, ataintr, scp); 310 #endif 311 else { 312 struct resource *irq; 313 int rid = 0; 314 void *ih; 315 316 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 317 RF_SHAREABLE | RF_ACTIVE); 318 if (!irq) 319 printf("ata_pciattach: Unable to alloc interrupt\n"); 320 321 if (type == 0x4d33105a) 322 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih); 323 else 324 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 325 } 326 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 327 lun, iobase_1, isa_apic_irq(irq1), unit); 328 } 329 lun = 1; 330 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 331 scp = atadevices[lun]; 332 if (iobase_2 == IO_WD2) 333 #ifdef __i386__ 334 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp, 335 &bio_imask, INTR_EXCL); 336 #endif 337 #ifdef __alpha__ 338 alpha_platform_setup_ide_intr(1, ataintr, scp); 339 #endif 340 else { 341 struct resource *irq; 342 int rid = 0; 343 void *ih; 344 345 if (type != 0x4d33105a) { 346 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 347 RF_SHAREABLE | RF_ACTIVE); 348 if (!irq) 349 printf("ata_pciattach: Unable to alloc interrupt\n"); 350 351 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 352 } 353 } 354 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 355 lun, iobase_2, isa_apic_irq(irq2), unit); 356 } 357 return 0; 358 } 359 360 static device_method_t ata_pci_methods[] = { 361 /* Device interface */ 362 DEVMETHOD(device_probe, ata_pciprobe), 363 DEVMETHOD(device_attach, ata_pciattach), 364 { 0, 0 } 365 }; 366 367 static driver_t ata_pci_driver = { 368 "ata-pci", 369 ata_pci_methods, 370 sizeof(int), 371 }; 372 373 DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 374 375 static void 376 promise_intr(void *data) 377 { 378 struct ata_softc *scp = (struct ata_softc *)data; 379 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c); 380 381 if (channel & 0x00000400) 382 ataintr(data); 383 384 if (channel & 0x00004000) 385 ataintr(atadevices[scp->lun + 1]); 386 } 387 #endif 388 389 static int32_t 390 ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 391 device_t dev, int32_t *unit) 392 { 393 struct ata_softc *scp = atadevices[atanlun]; 394 int32_t mask = 0; 395 int32_t timeout; 396 int32_t lun = atanlun; 397 u_int8_t status0, status1; 398 399 #ifdef ATA_STATIC_ID 400 atanlun++; 401 #endif 402 if (lun > MAXATA) { 403 printf("ata: unit out of range(%d)\n", lun); 404 return 0; 405 } 406 if (scp) { 407 printf("ata%d: unit already attached\n", lun); 408 return 0; 409 } 410 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT); 411 if (scp == NULL) { 412 printf("ata%d: failed to allocate driver storage\n", lun); 413 return 0; 414 } 415 bzero(scp, sizeof(struct ata_softc)); 416 417 scp->unit = *unit; 418 scp->lun = lun; 419 scp->ioaddr = ioaddr; 420 scp->altioaddr = altioaddr; 421 scp->active = ATA_IDLE; 422 423 #ifdef ATA_DEBUG 424 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 425 scp->lun, scp->ioaddr, scp->altioaddr); 426 #endif 427 428 /* do we have any signs of ATA/ATAPI HW being present ? */ 429 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 430 DELAY(1); 431 status0 = inb(scp->ioaddr + ATA_STATUS); 432 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 433 DELAY(1); 434 status1 = inb(scp->ioaddr + ATA_STATUS); 435 if ((status0 & 0xf8) != 0xf8) 436 mask |= 0x01; 437 if ((status1 & 0xf8) != 0xf8) 438 mask |= 0x02; 439 #ifdef ATA_DEBUG 440 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 441 scp->lun, mask, status0, status1); 442 #endif 443 if (!mask) { 444 free(scp, M_DEVBUF); 445 return 0; 446 } 447 /* assert reset for devices and wait for completition */ 448 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 449 DELAY(1); 450 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 451 DELAY(1000); 452 outb(scp->altioaddr, ATA_A_IDS); 453 DELAY(1000); 454 inb(scp->ioaddr + ATA_ERROR); 455 DELAY(1); 456 outb(scp->altioaddr, ATA_A_4BIT); 457 DELAY(1); 458 459 /* wait for BUSY to go inactive */ 460 for (timeout = 0; timeout < 30000*10; timeout++) { 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 (mask == 0x01) /* wait for master only */ 468 if (!(status0 & ATA_S_BSY)) 469 break; 470 if (mask == 0x02) /* wait for slave only */ 471 if (!(status1 & ATA_S_BSY)) 472 break; 473 if (mask == 0x03) /* wait for both master & slave */ 474 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY)) 475 break; 476 DELAY(100); 477 } 478 if (status0 & ATA_S_BSY) 479 mask &= ~0x01; 480 if (status1 & ATA_S_BSY) 481 mask &= ~0x02; 482 #ifdef ATA_DEBUG 483 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 484 scp->lun, mask, status0, status1); 485 #endif 486 if (!mask) { 487 free(scp, M_DEVBUF); 488 return 0; 489 } 490 /* 491 * OK, we have at least one device on the chain, 492 * check for ATAPI signatures, if none check if its 493 * a good old ATA device. 494 */ 495 496 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 497 DELAY(1); 498 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 499 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 500 scp->devices |= ATA_ATAPI_MASTER; 501 } 502 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 503 DELAY(1); 504 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 505 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 506 scp->devices |= ATA_ATAPI_SLAVE; 507 } 508 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 509 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 510 DELAY(1); 511 outb(scp->ioaddr + ATA_ERROR, 0x58); 512 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 513 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 514 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 515 scp->devices |= ATA_ATA_MASTER; 516 } 517 } 518 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 519 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 520 DELAY(1); 521 outb(scp->ioaddr + ATA_ERROR, 0x58); 522 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 523 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 524 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 525 scp->devices |= ATA_ATA_SLAVE; 526 } 527 } 528 #ifdef ATA_DEBUG 529 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 530 #endif 531 if (!scp->devices) { 532 free(scp, M_DEVBUF); 533 return 0; 534 } 535 bufq_init(&scp->ata_queue); 536 TAILQ_INIT(&scp->atapi_queue); 537 *unit = scp->lun; 538 scp->dev = dev; 539 if (bmaddr) 540 scp->bmaddr = bmaddr; 541 atadevices[scp->lun] = scp; 542 #ifndef ATA_STATIC_ID 543 atanlun++; 544 #endif 545 return ATA_IOSIZE; 546 } 547 548 static void 549 ataintr(void *data) 550 { 551 struct ata_softc *scp; 552 struct atapi_request *atapi_request; 553 struct buf *ata_request; 554 u_int8_t status; 555 static int32_t intr_count = 0; 556 557 scp = (struct ata_softc *)data; 558 559 /* find & call the responsible driver to process this interrupt */ 560 switch (scp->active) { 561 #if NATADISK > 0 562 case ATA_ACTIVE_ATA: 563 if ((ata_request = bufq_first(&scp->ata_queue))) 564 if (ad_interrupt(ata_request) == ATA_OP_CONTINUES) 565 return; 566 break; 567 #endif 568 case ATA_ACTIVE_ATAPI: 569 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) 570 if (atapi_interrupt(atapi_request) == ATA_OP_CONTINUES) 571 return; 572 break; 573 574 case ATA_WAIT_INTR: 575 wakeup((caddr_t)scp); 576 break; 577 578 case ATA_IGNORE_INTR: 579 break; 580 581 default: 582 case ATA_IDLE: 583 status = inb(scp->ioaddr + ATA_STATUS); 584 if (intr_count++ < 10) 585 printf("ata%d: unwanted interrupt %d status = %02x\n", 586 scp->lun, intr_count, status); 587 return; 588 } 589 scp->active = ATA_IDLE; 590 ata_start(scp); 591 } 592 593 void 594 ata_start(struct ata_softc *scp) 595 { 596 struct buf *ata_request; 597 struct atapi_request *atapi_request; 598 599 #ifdef ATA_DEBUG 600 printf("ata_start: entered\n"); 601 #endif 602 if (scp->active != ATA_IDLE) { 603 printf("ata: unwanted ata_start\n"); 604 return; 605 } 606 607 #if NATADISK > 0 608 /* find & call the responsible driver if anything on ATA queue */ 609 if ((ata_request = bufq_first(&scp->ata_queue))) { 610 scp->active = ATA_ACTIVE_ATA; 611 ad_transfer(ata_request); 612 #ifdef ATA_DEBUG 613 printf("ata_start: started ata, leaving\n"); 614 #endif 615 return; 616 } 617 #endif 618 619 /* find & call the responsible driver if anything on ATAPI queue */ 620 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 621 scp->active = ATA_ACTIVE_ATAPI; 622 atapi_transfer(atapi_request); 623 #ifdef ATA_DEBUG 624 printf("ata_start: started atapi, leaving\n"); 625 #endif 626 return; 627 } 628 } 629 630 int32_t 631 ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 632 { 633 u_int8_t status; 634 u_int32_t timeout = 0; 635 636 while (timeout++ <= 500000) { /* timeout 5 secs */ 637 status = inb(scp->ioaddr + ATA_STATUS); 638 639 /* if drive fails status, reselect the drive just to be sure */ 640 if (status == 0xff) { 641 printf("ata%d: %s: no status, reselecting device\n", 642 scp->lun, device?"slave":"master"); 643 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 644 DELAY(1); 645 status = inb(scp->ioaddr + ATA_STATUS); 646 } 647 if (status == 0xff) 648 return -1; 649 scp->status = status; 650 if (!(status & ATA_S_BSY)) { 651 if (status & ATA_S_ERROR) 652 scp->error = inb(scp->ioaddr + ATA_ERROR); 653 if ((status & mask) == mask) 654 return (status & ATA_S_ERROR); 655 } 656 if (timeout > 1000) 657 DELAY(1000); 658 else 659 DELAY(10); 660 } 661 return -1; 662 } 663 664 int32_t 665 ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 666 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 667 u_int32_t count, u_int32_t feature, int32_t flags) 668 { 669 #ifdef ATA_DEBUG 670 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, c=%d, h=%d, s=%d, count=%d, flags=%02x\n", scp->lun, scp->ioaddr, device, command, cylinder, head, sector, count, flags); 671 #endif 672 673 /* ready to issue command ? */ 674 if (ata_wait(scp, device, 0) < 0) { 675 printf("ata%d: %s: timeout waiting to give command s=%02x e=%02x\n", 676 scp->lun, device?"slave":"master", scp->status, scp->error); 677 } 678 outb(scp->ioaddr + ATA_FEATURE, feature); 679 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 680 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 681 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 682 outb(scp->ioaddr + ATA_SECTOR, sector); 683 outb(scp->ioaddr + ATA_COUNT, count); 684 685 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE) 686 printf("DANGER active=%d\n", scp->active); 687 688 switch (flags) { 689 case ATA_WAIT_INTR: 690 scp->active = ATA_WAIT_INTR; 691 outb(scp->ioaddr + ATA_CMD, command); 692 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 693 printf("ata_command: timeout waiting for interrupt\n"); 694 scp->active = ATA_IDLE; 695 return -1; 696 } 697 break; 698 699 case ATA_IGNORE_INTR: 700 scp->active = ATA_IGNORE_INTR; 701 outb(scp->ioaddr + ATA_CMD, command); 702 break; 703 704 case ATA_IMMEDIATE: 705 default: 706 outb(scp->ioaddr + ATA_CMD, command); 707 break; 708 } 709 #ifdef ATA_DEBUG 710 printf("ata_command: leaving\n"); 711 #endif 712 return 0; 713 } 714 715 void 716 bswap(int8_t *buf, int32_t len) 717 { 718 u_int16_t *p = (u_int16_t*)(buf + len); 719 720 while (--p >= (u_int16_t*)buf) 721 *p = ntohs(*p); 722 } 723 724 void 725 btrim(int8_t *buf, int32_t len) 726 { 727 int8_t *p; 728 729 for (p = buf; p < buf+len; ++p) 730 if (!*p) 731 *p = ' '; 732 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 733 *p = 0; 734 } 735 736 void 737 bpack(int8_t *src, int8_t *dst, int32_t len) 738 { 739 int32_t i, j, blank; 740 741 for (i = j = blank = 0 ; i < len-1; i++) { 742 if (blank && src[i] == ' ') continue; 743 if (blank && src[i] != ' ') { 744 dst[j++] = src[i]; 745 blank = 0; 746 continue; 747 } 748 if (src[i] == ' ') 749 blank = 1; 750 dst[j++] = src[i]; 751 } 752 dst[j] = 0x00; 753 } 754 #endif /* NATA > 0 */ 755