1/*- 2 * Copyright (c) 1998,1999,2000 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 *
| 1/*- 2 * Copyright (c) 1998,1999,2000 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 67058 2000-10-13 11:21:27Z sos $
| 28 * $FreeBSD: head/sys/dev/ata/ata-all.c 67067 2000-10-13 12:09:23Z sos $
|
29 */ 30 31#include "ata.h" 32#include "isa.h" 33#include "card.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/bio.h> 48#include <sys/malloc.h> 49#include <sys/devicestat.h> 50#include <sys/sysctl.h> 51#include <machine/stdarg.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 __alpha__ 63#include <machine/md_var.h> 64#endif 65#include <dev/ata/ata-all.h> 66#include <dev/ata/ata-disk.h> 67#include <dev/ata/atapi-all.h> 68 69/* misc defines */ 70#define IOMASK 0xfffffffc 71#define ATA_IOADDR_RID 0 72#define ATA_ALTADDR_RID 1 73#define ATA_BMADDR_RID 2
| 29 */ 30 31#include "ata.h" 32#include "isa.h" 33#include "card.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/bio.h> 48#include <sys/malloc.h> 49#include <sys/devicestat.h> 50#include <sys/sysctl.h> 51#include <machine/stdarg.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 __alpha__ 63#include <machine/md_var.h> 64#endif 65#include <dev/ata/ata-all.h> 66#include <dev/ata/ata-disk.h> 67#include <dev/ata/atapi-all.h> 68 69/* misc defines */ 70#define IOMASK 0xfffffffc 71#define ATA_IOADDR_RID 0 72#define ATA_ALTADDR_RID 1 73#define ATA_BMADDR_RID 2
|
| 74#if NPCI > 0
|
74#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \ 75 (pci_get_progif(dev) & 0x05) != 0x05)
| 75#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \ 76 (pci_get_progif(dev) & 0x05) != 0x05)
|
| 77#else 78#define ATA_MASTERDEV(dev) (1) 79#endif
|
76 77/* prototypes */ 78static int ata_probe(device_t); 79static int ata_attach(device_t); 80static int ata_detach(device_t); 81static int ata_resume(device_t); 82static void ata_boot_attach(void); 83static void ata_intr(void *); 84static int ata_getparam(struct ata_softc *, int, u_int8_t); 85static int ata_service(struct ata_softc *); 86static char *active2str(int); 87static void bswap(int8_t *, int); 88static void btrim(int8_t *, int); 89static void bpack(int8_t *, int8_t *, int); 90 91/* local vars */ 92static devclass_t ata_devclass; 93static devclass_t ata_pci_devclass; 94static struct intr_config_hook *ata_delayed_attach = NULL; 95static char ata_conf[256]; 96MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 97 98#if NISA > 0 99static struct isa_pnp_id ata_ids[] = { 100 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 101 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 102 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 103 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 104 {0} 105}; 106 107static int 108ata_isa_probe(device_t dev) 109{ 110 struct ata_softc *scp = device_get_softc(dev); 111 struct resource *port; 112 int rid; 113 u_long tmp; 114 115 /* check isapnp ids */ 116 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 117 return ENXIO; 118 119 /* allocate the port range */ 120 rid = ATA_IOADDR_RID; 121 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 122 ATA_IOSIZE, RF_ACTIVE); 123 if (!port) 124 return ENOMEM; 125 126 /* alloctate the altport range */ 127 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) { 128 bus_set_resource(dev, SYS_RES_IOPORT, 1, 129 rman_get_start(port) + ATA_ALTOFFSET, 130 ATA_ALTIOSIZE); 131 } 132 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 133 scp->channel = 0; 134 scp->flags |= ATA_USE_16BIT; 135 return ata_probe(dev); 136} 137 138static device_method_t ata_isa_methods[] = { 139 /* device interface */ 140 DEVMETHOD(device_probe, ata_isa_probe), 141 DEVMETHOD(device_attach, ata_attach), 142 DEVMETHOD(device_resume, ata_resume), 143 { 0, 0 } 144}; 145 146static driver_t ata_isa_driver = { 147 "ata", 148 ata_isa_methods, 149 sizeof(struct ata_softc), 150}; 151 152DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 153#endif 154 155#if NCARD > 0 156static int 157ata_pccard_probe(device_t dev) 158{ 159 struct ata_softc *scp = device_get_softc(dev); 160 struct resource *port; 161 int rid, len; 162 163 /* allocate the port range */ 164 rid = ATA_IOADDR_RID; 165 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid); 166 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE); 167 if (!port) 168 return ENOMEM; 169 170 /* 171 * if we got more than the default ATA_IOSIZE ports, this is likely 172 * a pccard system where the altio ports are located just after the 173 * normal io ports, so no need to allocate them. 174 */ 175 if (len <= ATA_IOSIZE) { 176 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, 177 rman_get_start(port) + ATA_ALTOFFSET, ATA_ALTIOSIZE); 178 } 179 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 180 scp->channel = 0; 181 scp->flags |= ATA_USE_16BIT; 182 return ata_probe(dev); 183} 184 185static device_method_t ata_pccard_methods[] = { 186 /* device interface */ 187 DEVMETHOD(device_probe, ata_pccard_probe), 188 DEVMETHOD(device_attach, ata_attach), 189 DEVMETHOD(device_detach, ata_detach), 190 { 0, 0 } 191}; 192 193static driver_t ata_pccard_driver = { 194 "ata", 195 ata_pccard_methods, 196 sizeof(struct ata_softc), 197}; 198 199DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0); 200#endif 201 202#if NPCI > 0 203struct ata_pci_softc { 204 struct resource *bmio; 205 struct resource bmio_1; 206 struct resource bmio_2; 207 struct resource *irq; 208 int irqcnt; 209}; 210 211int 212ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid) 213{ 214 device_t *children, child; 215 int nchildren, i; 216 217 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 218 return 0; 219 220 for (i = 0; i < nchildren; i++) { 221 child = children[i]; 222 223 /* check that it's on the same silicon and the device we want */ 224 if (pci_get_slot(dev) == pci_get_slot(child) && 225 pci_get_vendor(child) == (type & 0xffff) && 226 pci_get_device(child) == ((type & 0xffff0000) >> 16) && 227 pci_get_revid(child) >= revid) { 228 free(children, M_TEMP); 229 return 1; 230 } 231 } 232 free(children, M_TEMP); 233 return 0; 234} 235 236static const char * 237ata_pci_match(device_t dev) 238{ 239 if (pci_get_class(dev) != PCIC_STORAGE) 240 return NULL; 241 242 switch (pci_get_devid(dev)) { 243 /* supported chipsets */ 244 case 0x12308086: 245 return "Intel PIIX ATA controller"; 246 247 case 0x70108086: 248 return "Intel PIIX3 ATA controller"; 249 250 case 0x71118086: 251 case 0x71998086: 252 return "Intel PIIX4 ATA33 controller"; 253 254 case 0x24218086: 255 return "Intel ICH0 ATA33 controller"; 256 257 case 0x24118086: 258 return "Intel ICH ATA66 controller"; 259 260 case 0x244b8086: 261 return "Intel ICH2 ATA100 controller"; 262 263 case 0x522910b9: 264 return "AcerLabs Aladdin ATA33 controller"; 265 266 case 0x05711106: 267 if (ata_find_dev(dev, 0x05861106, 0)) 268 return "VIA 82C586 ATA33 controller"; 269 if (ata_find_dev(dev, 0x05961106, 0x12)) 270 return "VIA 82C596 ATA66 controller"; 271 if (ata_find_dev(dev, 0x05961106, 0)) 272 return "VIA 82C596 ATA33 controller"; 273 if (ata_find_dev(dev, 0x06861106, 0)) 274 return "VIA 82C686 ATA66 controller"; 275 return "VIA Apollo ATA controller"; 276 277 case 0x55131039: 278 return "SiS 5591 ATA33 controller"; 279 280 case 0x06491095: 281 return "CMD 649 ATA100 controller"; 282 283 case 0x06481095: 284 return "CMD 648 ATA66 controller"; 285 286 case 0x06461095: 287 return "CMD 646 ATA controller"; 288 289 case 0xc6931080: 290 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE) 291 return "Cypress 82C693 ATA controller"; 292 break; 293 294 case 0x01021078: 295 return "Cyrix 5530 ATA33 controller"; 296 297 case 0x74091022: 298 return "AMD 756 ATA66 controller"; 299 300 case 0x02111166: 301 return "ServerWorks ROSB4 ATA33 controller"; 302 303 case 0x4d33105a: 304 return "Promise ATA33 controller"; 305 306 case 0x4d38105a: 307 return "Promise ATA66 controller"; 308 309 case 0x0d30105a: 310 case 0x4d30105a: 311 return "Promise ATA100 controller"; 312 313 case 0x00041103: 314 switch (pci_get_revid(dev)) { 315 case 0x00: 316 case 0x01: 317 return "HighPoint HPT366 ATA66 controller"; 318 case 0x02: 319 return "HighPoint HPT368 ATA66 controller"; 320 case 0x03: 321 case 0x04: 322 return "HighPoint HPT370 ATA100 controller"; 323 default: 324 return "Unknown revision HighPoint ATA controller"; 325 } 326 327 /* unsupported but known chipsets, generic DMA only */ 328 case 0x10001042: 329 case 0x10011042: 330 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible"; 331 332 case 0x06401095: 333 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible"; 334 335 /* unknown chipsets, try generic DMA if it seems possible */ 336 default: 337 if (pci_get_class(dev) == PCIC_STORAGE && 338 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 339 return "Generic PCI ATA controller"; 340 } 341 return NULL; 342} 343 344static int 345ata_pci_probe(device_t dev) 346{ 347 const char *desc = ata_pci_match(dev); 348 349 if (desc) { 350 device_set_desc(dev, desc); 351 return 0; 352 } 353 else 354 return ENXIO; 355} 356 357static int 358ata_pci_add_child(device_t dev, int unit) 359{ 360 device_t child; 361 362 /* check if this is located at one of the std addresses */ 363 if (ATA_MASTERDEV(dev)) { 364 if (!(child = device_add_child(dev, "ata", unit))) 365 return ENOMEM; 366 } 367 else { 368 if (!(child = device_add_child(dev, "ata", 2))) 369 return ENOMEM; 370 } 371 return 0; 372} 373 374static int 375ata_pci_attach(device_t dev) 376{ 377 struct ata_pci_softc *sc = device_get_softc(dev); 378 u_int8_t class, subclass; 379 u_int32_t type, cmd; 380 int rid; 381 382 /* set up vendor-specific stuff */ 383 type = pci_get_devid(dev); 384 class = pci_get_class(dev); 385 subclass = pci_get_subclass(dev); 386 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 387 388 /* is busmastering supported ? */ 389 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 390 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) { 391 392 /* is there a valid port range to connect to ? */ 393 rid = 0x20; 394 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 395 0, ~0, 1, RF_ACTIVE); 396 if (!sc->bmio) 397 device_printf(dev, "Busmastering DMA not supported\n"); 398 } 399 400 /* do extra chipset specific setups */ 401 switch (type) { 402 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */ 403 pci_write_config(dev, 0x53, 404 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 405 break; 406 407 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */ 408 case 0x4d30105a: 409 case 0x0d30105a: 410 outb(rman_get_start(sc->bmio) + 0x11, 411 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a); 412 /* FALLTHROUGH */ 413 414 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */ 415 outb(rman_get_start(sc->bmio) + 0x1f, 416 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01); 417 break; 418 419 case 0x00041103: /* HighPoint */ 420 switch (pci_get_revid(dev)) { 421 case 0x00: 422 case 0x01: 423 /* turn off interrupt prediction */ 424 pci_write_config(dev, 0x51, 425 (pci_read_config(dev, 0x51, 1) & ~0x80), 1); 426 break; 427 428 case 0x02: 429 case 0x03: 430 case 0x04: 431 /* turn off interrupt prediction */ 432 pci_write_config(dev, 0x51, 433 (pci_read_config(dev, 0x51, 1) & ~0x02), 1); 434 pci_write_config(dev, 0x55, 435 (pci_read_config(dev, 0x55, 1) & ~0x02), 1); 436 /* turn on interrupts */ 437 pci_write_config(dev, 0x5a, 438 (pci_read_config(dev, 0x5a, 1) & ~0x10), 1); 439 440 } 441 break; 442 443 case 0x05711106: 444 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */ 445 /* set prefetch, postwrite */ 446 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 447 448 /* set fifo configuration half'n'half */ 449 pci_write_config(dev, 0x43, 450 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 451 452 /* set status register read retry */ 453 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 454 455 /* set DMA read & end-of-sector fifo flush */ 456 pci_write_config(dev, 0x46, 457 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 458 459 /* set sector size */ 460 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 461 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 462 463 /* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */ 464 if (ata_find_dev(dev, 0x06861106, 0) || 465 ata_find_dev(dev, 0x05961106, 0x12)) { 466 pci_write_config(dev, 0x50, 467 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4); 468 } 469 break; 470 } 471 472 /* 473 * the Cypress chip is a mess, it contains two ATA functions, but 474 * both channels are visible on the first one. 475 * simply ignore the second function for now, as the right 476 * solution (ignoring the second channel on the first function) 477 * doesn't work with the crappy ATA interrupt setup on the alpha. 478 */ 479 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1) 480 return 0; 481 482 ata_pci_add_child(dev, 0); 483 484 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK) 485 ata_pci_add_child(dev, 1); 486 487 return bus_generic_attach(dev); 488} 489 490static int 491ata_pci_print_child(device_t dev, device_t child) 492{ 493 struct ata_softc *scp = device_get_softc(child); 494 int retval = 0; 495 496 retval += bus_print_child_header(dev, child); 497 retval += printf(": at 0x%x", scp->ioaddr); 498 499 if (ATA_MASTERDEV(dev)) 500 retval += printf(" irq %d", 14 + scp->channel); 501 502 retval += bus_print_child_footer(dev, child); 503 504 return retval; 505} 506 507static struct resource * 508ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 509 u_long start, u_long end, u_long count, u_int flags) 510{ 511 struct ata_pci_softc *sc = device_get_softc(dev); 512 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 513 int myrid; 514 515 if (type == SYS_RES_IOPORT) { 516 switch (*rid) { 517 case ATA_IOADDR_RID: 518 if (ATA_MASTERDEV(dev)) { 519 myrid = 0; 520 start = (channel == 0 ? IO_WD1 : IO_WD2); 521 end = start + ATA_IOSIZE - 1; 522 count = ATA_IOSIZE; 523 } 524 else 525 myrid = 0x10 + 8 * channel; 526 break; 527 528 case ATA_ALTADDR_RID: 529 if (ATA_MASTERDEV(dev)) { 530 myrid = 0; 531 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET; 532 end = start + ATA_ALTIOSIZE - 1; 533 count = ATA_ALTIOSIZE; 534 } 535 else 536 myrid = 0x14 + 8 * channel; 537 break; 538 539 case ATA_BMADDR_RID: 540 /* the busmaster resource is shared between the two channels */ 541 if (sc->bmio) { 542 if (channel == 0) { 543 sc->bmio_1 = *sc->bmio; 544 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1; 545 return &sc->bmio_1; 546 } else { 547 sc->bmio_2 = *sc->bmio; 548 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1; 549 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1; 550 return &sc->bmio_2; 551 } 552 } 553 break; 554 555 default: 556 return 0; 557 } 558 559 if (ATA_MASTERDEV(dev)) 560 /* make the parent just pass through the allocation. */ 561 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 562 SYS_RES_IOPORT, &myrid, 563 start, end, count, flags); 564 else 565 /* we are using the parent resource directly. */ 566 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 567 SYS_RES_IOPORT, &myrid, 568 start, end, count, flags); 569 } 570 571 if (type == SYS_RES_IRQ) { 572 if (*rid != 0) 573 return 0; 574 575 if (ATA_MASTERDEV(dev)) { 576#ifdef __alpha__ 577 return alpha_platform_alloc_ide_intr(channel); 578#else 579 int irq = (channel == 0 ? 14 : 15); 580 581 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 582 SYS_RES_IRQ, rid, 583 irq, irq, 1, flags & ~RF_SHAREABLE); 584#endif 585 } else { 586 /* primary and secondary channels share the same interrupt */ 587 sc->irqcnt++; 588 if (!sc->irq) 589 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 590 SYS_RES_IRQ, rid, 0, ~0, 1, flags); 591 return sc->irq; 592 } 593 } 594 return 0; 595} 596 597static int 598ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 599 struct resource *r) 600{ 601 struct ata_pci_softc *sc = device_get_softc(dev); 602 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 603 int myrid = 0; 604 605 if (type == SYS_RES_IOPORT) { 606 switch (rid) { 607 case ATA_IOADDR_RID: 608 if (ATA_MASTERDEV(dev)) 609 myrid = 0; 610 else 611 myrid = 0x10 + 8 * channel; 612 break; 613 614 case ATA_ALTADDR_RID: 615 if (ATA_MASTERDEV(dev)) 616 myrid = 0; 617 else 618 myrid = 0x14 + 8 * channel; 619 break; 620 621 case ATA_BMADDR_RID: 622 return 0; 623 624 default: 625 return ENOENT; 626 } 627 628 if (ATA_MASTERDEV(dev)) 629 /* make the parent just pass through the allocation. */ 630 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 631 SYS_RES_IOPORT, myrid, r); 632 else 633 /* we are using the parent resource directly. */ 634 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 635 SYS_RES_IOPORT, myrid, r); 636 } 637 if (type == SYS_RES_IRQ) { 638 if (rid != 0) 639 return ENOENT; 640 641 if (ATA_MASTERDEV(dev)) { 642#ifdef __alpha__ 643 return alpha_platform_release_ide_intr(channel, r); 644#else 645 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 646 child, SYS_RES_IRQ, rid, r); 647#endif 648 } 649 else { 650 if (--sc->irqcnt) 651 return 0; 652 653 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 654 dev, SYS_RES_IRQ, rid, r); 655 } 656 } 657 return EINVAL; 658} 659 660static int 661ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 662 int flags, driver_intr_t *intr, void *arg, 663 void **cookiep) 664{ 665 if (ATA_MASTERDEV(dev)) { 666#ifdef __alpha__ 667 return alpha_platform_setup_ide_intr(child, irq, intr, arg, cookiep); 668#else 669 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 670 flags, intr, arg, cookiep); 671#endif 672 } 673 else 674 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq, 675 flags, intr, arg, cookiep); 676} 677 678static int 679ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 680 void *cookie) 681{ 682 if (ATA_MASTERDEV(dev)) { 683#ifdef __alpha__ 684 return alpha_platform_teardown_ide_intr(child, irq, cookie); 685#else 686 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 687#endif 688 } 689 else 690 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie); 691} 692 693static device_method_t ata_pci_methods[] = { 694 /* device interface */ 695 DEVMETHOD(device_probe, ata_pci_probe), 696 DEVMETHOD(device_attach, ata_pci_attach), 697 DEVMETHOD(device_shutdown, bus_generic_shutdown), 698 DEVMETHOD(device_suspend, bus_generic_suspend), 699 DEVMETHOD(device_resume, bus_generic_resume), 700 701 /* bus methods */ 702 DEVMETHOD(bus_print_child, ata_pci_print_child), 703 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 704 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 705 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 706 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 707 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 708 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 709 { 0, 0 } 710}; 711 712static driver_t ata_pci_driver = { 713 "atapci", 714 ata_pci_methods, 715 sizeof(struct ata_pci_softc), 716}; 717 718DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 719 720static int 721ata_pcisub_probe(device_t dev) 722{ 723 struct ata_softc *scp = device_get_softc(dev); 724 device_t *list; 725 int count, i; 726 727 /* find channel number on this controller */ 728 device_get_children(device_get_parent(dev), &list, &count); 729 for (i = 0; i < count; i++) { 730 if (list[i] == dev) 731 scp->channel = i; 732 } 733 734 scp->chiptype = pci_get_devid(device_get_parent(dev)); 735 736 /* is this an ATA RAID setup ? */ 737 if (((pci_get_subclass(device_get_parent(dev)) == PCIS_STORAGE_RAID) && 738 (scp->chiptype == 0x4d33105a || scp->chiptype == 0x4d38105a || 739 scp->chiptype == 0x4d30105a || scp->chiptype == 0x0d30105a)) || 740 scp->chiptype == 0x00041103) { 741 scp->flags |= ATA_RAID; 742 } 743 return ata_probe(dev); 744} 745 746static device_method_t ata_pcisub_methods[] = { 747 /* device interface */ 748 DEVMETHOD(device_probe, ata_pcisub_probe), 749 DEVMETHOD(device_attach, ata_attach), 750 DEVMETHOD(device_detach, ata_detach), 751 DEVMETHOD(device_resume, ata_resume), 752 { 0, 0 } 753}; 754 755static driver_t ata_pcisub_driver = { 756 "ata", 757 ata_pcisub_methods, 758 sizeof(struct ata_softc), 759}; 760 761DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0); 762#endif 763 764static int 765ata_probe(device_t dev) 766{ 767 struct ata_softc *scp = device_get_softc(dev); 768 struct resource *io = 0; 769 struct resource *altio = 0; 770 struct resource *bmio = 0; 771 int rid; 772 u_int32_t ioaddr, altioaddr, bmaddr; 773 int mask = 0; 774 u_int8_t status0, status1; 775 776 if (!scp || scp->flags & ATA_ATTACHED) 777 return ENXIO; 778 779 /* initialize the softc basics */ 780 scp->active = ATA_IDLE; 781 scp->dev = dev; 782 scp->devices = 0; 783 784 rid = ATA_IOADDR_RID; 785 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 786 ATA_IOSIZE, RF_ACTIVE); 787 if (!io) 788 goto failure; 789 ioaddr = rman_get_start(io); 790 791 rid = ATA_ALTADDR_RID; 792 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 793 ATA_ALTIOSIZE, RF_ACTIVE); 794 if (altio) {
| 80 81/* prototypes */ 82static int ata_probe(device_t); 83static int ata_attach(device_t); 84static int ata_detach(device_t); 85static int ata_resume(device_t); 86static void ata_boot_attach(void); 87static void ata_intr(void *); 88static int ata_getparam(struct ata_softc *, int, u_int8_t); 89static int ata_service(struct ata_softc *); 90static char *active2str(int); 91static void bswap(int8_t *, int); 92static void btrim(int8_t *, int); 93static void bpack(int8_t *, int8_t *, int); 94 95/* local vars */ 96static devclass_t ata_devclass; 97static devclass_t ata_pci_devclass; 98static struct intr_config_hook *ata_delayed_attach = NULL; 99static char ata_conf[256]; 100MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 101 102#if NISA > 0 103static struct isa_pnp_id ata_ids[] = { 104 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 105 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 106 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 107 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 108 {0} 109}; 110 111static int 112ata_isa_probe(device_t dev) 113{ 114 struct ata_softc *scp = device_get_softc(dev); 115 struct resource *port; 116 int rid; 117 u_long tmp; 118 119 /* check isapnp ids */ 120 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 121 return ENXIO; 122 123 /* allocate the port range */ 124 rid = ATA_IOADDR_RID; 125 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 126 ATA_IOSIZE, RF_ACTIVE); 127 if (!port) 128 return ENOMEM; 129 130 /* alloctate the altport range */ 131 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) { 132 bus_set_resource(dev, SYS_RES_IOPORT, 1, 133 rman_get_start(port) + ATA_ALTOFFSET, 134 ATA_ALTIOSIZE); 135 } 136 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 137 scp->channel = 0; 138 scp->flags |= ATA_USE_16BIT; 139 return ata_probe(dev); 140} 141 142static device_method_t ata_isa_methods[] = { 143 /* device interface */ 144 DEVMETHOD(device_probe, ata_isa_probe), 145 DEVMETHOD(device_attach, ata_attach), 146 DEVMETHOD(device_resume, ata_resume), 147 { 0, 0 } 148}; 149 150static driver_t ata_isa_driver = { 151 "ata", 152 ata_isa_methods, 153 sizeof(struct ata_softc), 154}; 155 156DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 157#endif 158 159#if NCARD > 0 160static int 161ata_pccard_probe(device_t dev) 162{ 163 struct ata_softc *scp = device_get_softc(dev); 164 struct resource *port; 165 int rid, len; 166 167 /* allocate the port range */ 168 rid = ATA_IOADDR_RID; 169 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid); 170 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE); 171 if (!port) 172 return ENOMEM; 173 174 /* 175 * if we got more than the default ATA_IOSIZE ports, this is likely 176 * a pccard system where the altio ports are located just after the 177 * normal io ports, so no need to allocate them. 178 */ 179 if (len <= ATA_IOSIZE) { 180 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, 181 rman_get_start(port) + ATA_ALTOFFSET, ATA_ALTIOSIZE); 182 } 183 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 184 scp->channel = 0; 185 scp->flags |= ATA_USE_16BIT; 186 return ata_probe(dev); 187} 188 189static device_method_t ata_pccard_methods[] = { 190 /* device interface */ 191 DEVMETHOD(device_probe, ata_pccard_probe), 192 DEVMETHOD(device_attach, ata_attach), 193 DEVMETHOD(device_detach, ata_detach), 194 { 0, 0 } 195}; 196 197static driver_t ata_pccard_driver = { 198 "ata", 199 ata_pccard_methods, 200 sizeof(struct ata_softc), 201}; 202 203DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0); 204#endif 205 206#if NPCI > 0 207struct ata_pci_softc { 208 struct resource *bmio; 209 struct resource bmio_1; 210 struct resource bmio_2; 211 struct resource *irq; 212 int irqcnt; 213}; 214 215int 216ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid) 217{ 218 device_t *children, child; 219 int nchildren, i; 220 221 if (device_get_children(device_get_parent(dev), &children, &nchildren)) 222 return 0; 223 224 for (i = 0; i < nchildren; i++) { 225 child = children[i]; 226 227 /* check that it's on the same silicon and the device we want */ 228 if (pci_get_slot(dev) == pci_get_slot(child) && 229 pci_get_vendor(child) == (type & 0xffff) && 230 pci_get_device(child) == ((type & 0xffff0000) >> 16) && 231 pci_get_revid(child) >= revid) { 232 free(children, M_TEMP); 233 return 1; 234 } 235 } 236 free(children, M_TEMP); 237 return 0; 238} 239 240static const char * 241ata_pci_match(device_t dev) 242{ 243 if (pci_get_class(dev) != PCIC_STORAGE) 244 return NULL; 245 246 switch (pci_get_devid(dev)) { 247 /* supported chipsets */ 248 case 0x12308086: 249 return "Intel PIIX ATA controller"; 250 251 case 0x70108086: 252 return "Intel PIIX3 ATA controller"; 253 254 case 0x71118086: 255 case 0x71998086: 256 return "Intel PIIX4 ATA33 controller"; 257 258 case 0x24218086: 259 return "Intel ICH0 ATA33 controller"; 260 261 case 0x24118086: 262 return "Intel ICH ATA66 controller"; 263 264 case 0x244b8086: 265 return "Intel ICH2 ATA100 controller"; 266 267 case 0x522910b9: 268 return "AcerLabs Aladdin ATA33 controller"; 269 270 case 0x05711106: 271 if (ata_find_dev(dev, 0x05861106, 0)) 272 return "VIA 82C586 ATA33 controller"; 273 if (ata_find_dev(dev, 0x05961106, 0x12)) 274 return "VIA 82C596 ATA66 controller"; 275 if (ata_find_dev(dev, 0x05961106, 0)) 276 return "VIA 82C596 ATA33 controller"; 277 if (ata_find_dev(dev, 0x06861106, 0)) 278 return "VIA 82C686 ATA66 controller"; 279 return "VIA Apollo ATA controller"; 280 281 case 0x55131039: 282 return "SiS 5591 ATA33 controller"; 283 284 case 0x06491095: 285 return "CMD 649 ATA100 controller"; 286 287 case 0x06481095: 288 return "CMD 648 ATA66 controller"; 289 290 case 0x06461095: 291 return "CMD 646 ATA controller"; 292 293 case 0xc6931080: 294 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE) 295 return "Cypress 82C693 ATA controller"; 296 break; 297 298 case 0x01021078: 299 return "Cyrix 5530 ATA33 controller"; 300 301 case 0x74091022: 302 return "AMD 756 ATA66 controller"; 303 304 case 0x02111166: 305 return "ServerWorks ROSB4 ATA33 controller"; 306 307 case 0x4d33105a: 308 return "Promise ATA33 controller"; 309 310 case 0x4d38105a: 311 return "Promise ATA66 controller"; 312 313 case 0x0d30105a: 314 case 0x4d30105a: 315 return "Promise ATA100 controller"; 316 317 case 0x00041103: 318 switch (pci_get_revid(dev)) { 319 case 0x00: 320 case 0x01: 321 return "HighPoint HPT366 ATA66 controller"; 322 case 0x02: 323 return "HighPoint HPT368 ATA66 controller"; 324 case 0x03: 325 case 0x04: 326 return "HighPoint HPT370 ATA100 controller"; 327 default: 328 return "Unknown revision HighPoint ATA controller"; 329 } 330 331 /* unsupported but known chipsets, generic DMA only */ 332 case 0x10001042: 333 case 0x10011042: 334 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible"; 335 336 case 0x06401095: 337 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible"; 338 339 /* unknown chipsets, try generic DMA if it seems possible */ 340 default: 341 if (pci_get_class(dev) == PCIC_STORAGE && 342 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 343 return "Generic PCI ATA controller"; 344 } 345 return NULL; 346} 347 348static int 349ata_pci_probe(device_t dev) 350{ 351 const char *desc = ata_pci_match(dev); 352 353 if (desc) { 354 device_set_desc(dev, desc); 355 return 0; 356 } 357 else 358 return ENXIO; 359} 360 361static int 362ata_pci_add_child(device_t dev, int unit) 363{ 364 device_t child; 365 366 /* check if this is located at one of the std addresses */ 367 if (ATA_MASTERDEV(dev)) { 368 if (!(child = device_add_child(dev, "ata", unit))) 369 return ENOMEM; 370 } 371 else { 372 if (!(child = device_add_child(dev, "ata", 2))) 373 return ENOMEM; 374 } 375 return 0; 376} 377 378static int 379ata_pci_attach(device_t dev) 380{ 381 struct ata_pci_softc *sc = device_get_softc(dev); 382 u_int8_t class, subclass; 383 u_int32_t type, cmd; 384 int rid; 385 386 /* set up vendor-specific stuff */ 387 type = pci_get_devid(dev); 388 class = pci_get_class(dev); 389 subclass = pci_get_subclass(dev); 390 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 391 392 /* is busmastering supported ? */ 393 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 394 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) { 395 396 /* is there a valid port range to connect to ? */ 397 rid = 0x20; 398 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 399 0, ~0, 1, RF_ACTIVE); 400 if (!sc->bmio) 401 device_printf(dev, "Busmastering DMA not supported\n"); 402 } 403 404 /* do extra chipset specific setups */ 405 switch (type) { 406 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */ 407 pci_write_config(dev, 0x53, 408 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1); 409 break; 410 411 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */ 412 case 0x4d30105a: 413 case 0x0d30105a: 414 outb(rman_get_start(sc->bmio) + 0x11, 415 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a); 416 /* FALLTHROUGH */ 417 418 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */ 419 outb(rman_get_start(sc->bmio) + 0x1f, 420 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01); 421 break; 422 423 case 0x00041103: /* HighPoint */ 424 switch (pci_get_revid(dev)) { 425 case 0x00: 426 case 0x01: 427 /* turn off interrupt prediction */ 428 pci_write_config(dev, 0x51, 429 (pci_read_config(dev, 0x51, 1) & ~0x80), 1); 430 break; 431 432 case 0x02: 433 case 0x03: 434 case 0x04: 435 /* turn off interrupt prediction */ 436 pci_write_config(dev, 0x51, 437 (pci_read_config(dev, 0x51, 1) & ~0x02), 1); 438 pci_write_config(dev, 0x55, 439 (pci_read_config(dev, 0x55, 1) & ~0x02), 1); 440 /* turn on interrupts */ 441 pci_write_config(dev, 0x5a, 442 (pci_read_config(dev, 0x5a, 1) & ~0x10), 1); 443 444 } 445 break; 446 447 case 0x05711106: 448 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */ 449 /* set prefetch, postwrite */ 450 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1); 451 452 /* set fifo configuration half'n'half */ 453 pci_write_config(dev, 0x43, 454 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1); 455 456 /* set status register read retry */ 457 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1); 458 459 /* set DMA read & end-of-sector fifo flush */ 460 pci_write_config(dev, 0x46, 461 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1); 462 463 /* set sector size */ 464 pci_write_config(dev, 0x60, DEV_BSIZE, 2); 465 pci_write_config(dev, 0x68, DEV_BSIZE, 2); 466 467 /* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */ 468 if (ata_find_dev(dev, 0x06861106, 0) || 469 ata_find_dev(dev, 0x05961106, 0x12)) { 470 pci_write_config(dev, 0x50, 471 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4); 472 } 473 break; 474 } 475 476 /* 477 * the Cypress chip is a mess, it contains two ATA functions, but 478 * both channels are visible on the first one. 479 * simply ignore the second function for now, as the right 480 * solution (ignoring the second channel on the first function) 481 * doesn't work with the crappy ATA interrupt setup on the alpha. 482 */ 483 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1) 484 return 0; 485 486 ata_pci_add_child(dev, 0); 487 488 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK) 489 ata_pci_add_child(dev, 1); 490 491 return bus_generic_attach(dev); 492} 493 494static int 495ata_pci_print_child(device_t dev, device_t child) 496{ 497 struct ata_softc *scp = device_get_softc(child); 498 int retval = 0; 499 500 retval += bus_print_child_header(dev, child); 501 retval += printf(": at 0x%x", scp->ioaddr); 502 503 if (ATA_MASTERDEV(dev)) 504 retval += printf(" irq %d", 14 + scp->channel); 505 506 retval += bus_print_child_footer(dev, child); 507 508 return retval; 509} 510 511static struct resource * 512ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 513 u_long start, u_long end, u_long count, u_int flags) 514{ 515 struct ata_pci_softc *sc = device_get_softc(dev); 516 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 517 int myrid; 518 519 if (type == SYS_RES_IOPORT) { 520 switch (*rid) { 521 case ATA_IOADDR_RID: 522 if (ATA_MASTERDEV(dev)) { 523 myrid = 0; 524 start = (channel == 0 ? IO_WD1 : IO_WD2); 525 end = start + ATA_IOSIZE - 1; 526 count = ATA_IOSIZE; 527 } 528 else 529 myrid = 0x10 + 8 * channel; 530 break; 531 532 case ATA_ALTADDR_RID: 533 if (ATA_MASTERDEV(dev)) { 534 myrid = 0; 535 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET; 536 end = start + ATA_ALTIOSIZE - 1; 537 count = ATA_ALTIOSIZE; 538 } 539 else 540 myrid = 0x14 + 8 * channel; 541 break; 542 543 case ATA_BMADDR_RID: 544 /* the busmaster resource is shared between the two channels */ 545 if (sc->bmio) { 546 if (channel == 0) { 547 sc->bmio_1 = *sc->bmio; 548 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1; 549 return &sc->bmio_1; 550 } else { 551 sc->bmio_2 = *sc->bmio; 552 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1; 553 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1; 554 return &sc->bmio_2; 555 } 556 } 557 break; 558 559 default: 560 return 0; 561 } 562 563 if (ATA_MASTERDEV(dev)) 564 /* make the parent just pass through the allocation. */ 565 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 566 SYS_RES_IOPORT, &myrid, 567 start, end, count, flags); 568 else 569 /* we are using the parent resource directly. */ 570 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 571 SYS_RES_IOPORT, &myrid, 572 start, end, count, flags); 573 } 574 575 if (type == SYS_RES_IRQ) { 576 if (*rid != 0) 577 return 0; 578 579 if (ATA_MASTERDEV(dev)) { 580#ifdef __alpha__ 581 return alpha_platform_alloc_ide_intr(channel); 582#else 583 int irq = (channel == 0 ? 14 : 15); 584 585 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 586 SYS_RES_IRQ, rid, 587 irq, irq, 1, flags & ~RF_SHAREABLE); 588#endif 589 } else { 590 /* primary and secondary channels share the same interrupt */ 591 sc->irqcnt++; 592 if (!sc->irq) 593 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 594 SYS_RES_IRQ, rid, 0, ~0, 1, flags); 595 return sc->irq; 596 } 597 } 598 return 0; 599} 600 601static int 602ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 603 struct resource *r) 604{ 605 struct ata_pci_softc *sc = device_get_softc(dev); 606 int channel = ((struct ata_softc *)device_get_softc(child))->channel; 607 int myrid = 0; 608 609 if (type == SYS_RES_IOPORT) { 610 switch (rid) { 611 case ATA_IOADDR_RID: 612 if (ATA_MASTERDEV(dev)) 613 myrid = 0; 614 else 615 myrid = 0x10 + 8 * channel; 616 break; 617 618 case ATA_ALTADDR_RID: 619 if (ATA_MASTERDEV(dev)) 620 myrid = 0; 621 else 622 myrid = 0x14 + 8 * channel; 623 break; 624 625 case ATA_BMADDR_RID: 626 return 0; 627 628 default: 629 return ENOENT; 630 } 631 632 if (ATA_MASTERDEV(dev)) 633 /* make the parent just pass through the allocation. */ 634 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 635 SYS_RES_IOPORT, myrid, r); 636 else 637 /* we are using the parent resource directly. */ 638 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 639 SYS_RES_IOPORT, myrid, r); 640 } 641 if (type == SYS_RES_IRQ) { 642 if (rid != 0) 643 return ENOENT; 644 645 if (ATA_MASTERDEV(dev)) { 646#ifdef __alpha__ 647 return alpha_platform_release_ide_intr(channel, r); 648#else 649 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 650 child, SYS_RES_IRQ, rid, r); 651#endif 652 } 653 else { 654 if (--sc->irqcnt) 655 return 0; 656 657 return BUS_RELEASE_RESOURCE(device_get_parent(dev), 658 dev, SYS_RES_IRQ, rid, r); 659 } 660 } 661 return EINVAL; 662} 663 664static int 665ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 666 int flags, driver_intr_t *intr, void *arg, 667 void **cookiep) 668{ 669 if (ATA_MASTERDEV(dev)) { 670#ifdef __alpha__ 671 return alpha_platform_setup_ide_intr(child, irq, intr, arg, cookiep); 672#else 673 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 674 flags, intr, arg, cookiep); 675#endif 676 } 677 else 678 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq, 679 flags, intr, arg, cookiep); 680} 681 682static int 683ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 684 void *cookie) 685{ 686 if (ATA_MASTERDEV(dev)) { 687#ifdef __alpha__ 688 return alpha_platform_teardown_ide_intr(child, irq, cookie); 689#else 690 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 691#endif 692 } 693 else 694 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie); 695} 696 697static device_method_t ata_pci_methods[] = { 698 /* device interface */ 699 DEVMETHOD(device_probe, ata_pci_probe), 700 DEVMETHOD(device_attach, ata_pci_attach), 701 DEVMETHOD(device_shutdown, bus_generic_shutdown), 702 DEVMETHOD(device_suspend, bus_generic_suspend), 703 DEVMETHOD(device_resume, bus_generic_resume), 704 705 /* bus methods */ 706 DEVMETHOD(bus_print_child, ata_pci_print_child), 707 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 708 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 709 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 710 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 711 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 712 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 713 { 0, 0 } 714}; 715 716static driver_t ata_pci_driver = { 717 "atapci", 718 ata_pci_methods, 719 sizeof(struct ata_pci_softc), 720}; 721 722DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 723 724static int 725ata_pcisub_probe(device_t dev) 726{ 727 struct ata_softc *scp = device_get_softc(dev); 728 device_t *list; 729 int count, i; 730 731 /* find channel number on this controller */ 732 device_get_children(device_get_parent(dev), &list, &count); 733 for (i = 0; i < count; i++) { 734 if (list[i] == dev) 735 scp->channel = i; 736 } 737 738 scp->chiptype = pci_get_devid(device_get_parent(dev)); 739 740 /* is this an ATA RAID setup ? */ 741 if (((pci_get_subclass(device_get_parent(dev)) == PCIS_STORAGE_RAID) && 742 (scp->chiptype == 0x4d33105a || scp->chiptype == 0x4d38105a || 743 scp->chiptype == 0x4d30105a || scp->chiptype == 0x0d30105a)) || 744 scp->chiptype == 0x00041103) { 745 scp->flags |= ATA_RAID; 746 } 747 return ata_probe(dev); 748} 749 750static device_method_t ata_pcisub_methods[] = { 751 /* device interface */ 752 DEVMETHOD(device_probe, ata_pcisub_probe), 753 DEVMETHOD(device_attach, ata_attach), 754 DEVMETHOD(device_detach, ata_detach), 755 DEVMETHOD(device_resume, ata_resume), 756 { 0, 0 } 757}; 758 759static driver_t ata_pcisub_driver = { 760 "ata", 761 ata_pcisub_methods, 762 sizeof(struct ata_softc), 763}; 764 765DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0); 766#endif 767 768static int 769ata_probe(device_t dev) 770{ 771 struct ata_softc *scp = device_get_softc(dev); 772 struct resource *io = 0; 773 struct resource *altio = 0; 774 struct resource *bmio = 0; 775 int rid; 776 u_int32_t ioaddr, altioaddr, bmaddr; 777 int mask = 0; 778 u_int8_t status0, status1; 779 780 if (!scp || scp->flags & ATA_ATTACHED) 781 return ENXIO; 782 783 /* initialize the softc basics */ 784 scp->active = ATA_IDLE; 785 scp->dev = dev; 786 scp->devices = 0; 787 788 rid = ATA_IOADDR_RID; 789 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 790 ATA_IOSIZE, RF_ACTIVE); 791 if (!io) 792 goto failure; 793 ioaddr = rman_get_start(io); 794 795 rid = ATA_ALTADDR_RID; 796 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 797 ATA_ALTIOSIZE, RF_ACTIVE); 798 if (altio) {
|
795 if (ATA_MASTERDEV(device_get_parent(dev)))
| 799 if (scp->flags & ATA_USE_16BIT || ATA_MASTERDEV(device_get_parent(dev)))
|
796 altioaddr = rman_get_start(altio); 797 else 798 altioaddr = rman_get_start(altio) + 0x02; 799 } 800 else 801 altioaddr = ioaddr + ATA_IOSIZE; 802 803 rid = ATA_BMADDR_RID; 804 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 805 bmaddr = bmio ? rman_get_start(bmio) : 0; 806 807 /* store the IO resources for eventual later release */ 808 scp->r_io = io; 809 scp->r_altio = altio; 810 scp->r_bmio = bmio; 811 812 /* store the physical IO addresse for easy access */ 813 scp->ioaddr = ioaddr; 814 scp->altioaddr = altioaddr; 815 scp->bmaddr = bmaddr; 816 817 if (bootverbose) 818 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 819 scp->ioaddr, scp->altioaddr, scp->bmaddr); 820 821 /* do we have any signs of ATA/ATAPI HW being present ? */ 822 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 823 DELAY(1); 824 status0 = inb(scp->ioaddr + ATA_STATUS); 825 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 826 DELAY(1); 827 status1 = inb(scp->ioaddr + ATA_STATUS); 828 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5) 829 mask |= 0x01; 830 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5) 831 mask |= 0x02; 832 if (bootverbose) 833 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 834 mask, status0, status1); 835 if (!mask) 836 goto failure; 837 838 ata_reset(scp, &mask); 839 840 if (bootverbose) 841 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices); 842 843 if (!mask) 844 goto failure; 845 846 TAILQ_INIT(&scp->ata_queue); 847 TAILQ_INIT(&scp->atapi_queue); 848 return 0; 849 850failure: 851 if (io) 852 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 853 if (altio) 854 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio); 855 if (bmio) 856 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio); 857 if (bootverbose) 858 ata_printf(scp, -1, "probe allocation failed\n"); 859 return ENXIO; 860} 861 862static int 863ata_attach(device_t dev) 864{ 865 struct ata_softc *scp = device_get_softc(dev); 866 int error, rid = 0; 867 868 if (!scp || scp->flags & ATA_ATTACHED) 869 return ENXIO; 870 871 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 872 RF_SHAREABLE | RF_ACTIVE); 873 if (!scp->r_irq) { 874 ata_printf(scp, -1, "unable to allocate interrupt\n"); 875 return ENXIO; 876 } 877 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr, 878 scp, &scp->ih))) 879 return error; 880 881 /* 882 * do not attach devices if we are in early boot, this is done later 883 * when interrupts are enabled by a hook into the boot process. 884 * otherwise attach what the probe has found in scp->devices. 885 */ 886 if (!ata_delayed_attach) { 887 if (scp->devices & ATA_ATA_SLAVE) 888 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 889 scp->devices &= ~ATA_ATA_SLAVE; 890 if (scp->devices & ATA_ATAPI_SLAVE) 891 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 892 scp->devices &= ~ATA_ATAPI_SLAVE; 893 if (scp->devices & ATA_ATA_MASTER) 894 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 895 scp->devices &= ~ATA_ATA_MASTER; 896 if (scp->devices & ATA_ATAPI_MASTER) 897 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 898 scp->devices &= ~ATA_ATAPI_MASTER; 899#if NATADISK > 0 900 if (scp->devices & ATA_ATA_MASTER) 901 ad_attach(scp, ATA_MASTER); 902 if (scp->devices & ATA_ATA_SLAVE) 903 ad_attach(scp, ATA_SLAVE); 904#endif 905#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 906 if (scp->devices & ATA_ATAPI_MASTER) 907 atapi_attach(scp, ATA_MASTER); 908 if (scp->devices & ATA_ATAPI_SLAVE) 909 atapi_attach(scp, ATA_SLAVE); 910#endif 911 } 912 scp->flags |= ATA_ATTACHED; 913 return 0; 914} 915 916static int 917ata_detach(device_t dev) 918{ 919 struct ata_softc *scp = device_get_softc(dev); 920 921 if (!scp || !(scp->flags & ATA_ATTACHED)) 922 return ENXIO; 923 924#if NATADISK > 0 925 if (scp->devices & ATA_ATA_MASTER) 926 ad_detach(scp->dev_softc[0]); 927 if (scp->devices & ATA_ATA_SLAVE) 928 ad_detach(scp->dev_softc[1]); 929#endif 930#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 931 if (scp->devices & ATA_ATAPI_MASTER) 932 atapi_detach(scp->dev_softc[0]); 933 if (scp->devices & ATA_ATAPI_SLAVE) 934 atapi_detach(scp->dev_softc[1]); 935#endif 936 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) { 937 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA); 938 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL; 939 } 940 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) { 941 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA); 942 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL; 943 } 944 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL; 945 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL; 946 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO; 947 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO; 948 bus_teardown_intr(dev, scp->r_irq, scp->ih); 949 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq); 950 if (scp->r_bmio) 951 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio); 952 if (scp->r_altio) 953 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio); 954 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io); 955 scp->flags &= ~ATA_ATTACHED; 956 return 0; 957} 958 959static int 960ata_resume(device_t dev) 961{ 962 struct ata_softc *scp = device_get_softc(dev); 963 964 ata_reinit(scp); 965 return 0; 966} 967 968static int 969ata_getparam(struct ata_softc *scp, int device, u_int8_t command) 970{ 971 struct ata_params *ata_parm; 972 int8_t buffer[DEV_BSIZE]; 973 int retry = 0; 974 975 /* select drive */ 976 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 977 DELAY(1); 978 979 /* enable interrupt */ 980 outb(scp->altioaddr, ATA_A_4BIT); 981 DELAY(1); 982 983 /* apparently some devices needs this repeated */ 984 do { 985 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) { 986 ata_printf(scp, device, "identify failed\n"); 987 return -1; 988 } 989 if (retry++ > 4) { 990 ata_printf(scp, device, "identify retries exceeded\n"); 991 return -1; 992 } 993 } while (ata_wait(scp, device, 994 ((command == ATA_C_ATAPI_IDENTIFY) ? 995 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)))); 996 997 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t)); 998 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 999 if (!ata_parm) { 1000 ata_printf(scp, device, "malloc for identify data failed\n"); 1001 return -1; 1002 } 1003 bcopy(buffer, ata_parm, sizeof(struct ata_params)); 1004 if (command == ATA_C_ATA_IDENTIFY || 1005 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 1006 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X'))) 1007 bswap(ata_parm->model, sizeof(ata_parm->model)); 1008 btrim(ata_parm->model, sizeof(ata_parm->model)); 1009 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 1010 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 1011 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 1012 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 1013 scp->dev_param[ATA_DEV(device)] = ata_parm; 1014 return 0; 1015} 1016 1017static void 1018ata_boot_attach(void) 1019{ 1020 struct ata_softc *scp; 1021 int ctlr; 1022 1023 /* 1024 * run through all ata devices and look for real ATA & ATAPI devices 1025 * using the hints we found in the early probe, this avoids some of 1026 * the delays probing of non-exsistent devices can cause. 1027 */ 1028 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1029 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1030 continue; 1031 if (scp->devices & ATA_ATA_SLAVE) 1032 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 1033 scp->devices &= ~ATA_ATA_SLAVE; 1034 if (scp->devices & ATA_ATAPI_SLAVE) 1035 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 1036 scp->devices &= ~ATA_ATAPI_SLAVE; 1037 if (scp->devices & ATA_ATA_MASTER) 1038 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 1039 scp->devices &= ~ATA_ATA_MASTER; 1040 if (scp->devices & ATA_ATAPI_MASTER) 1041 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 1042 scp->devices &= ~ATA_ATAPI_MASTER; 1043 } 1044 1045#if NATADISK > 0 1046 /* now we know whats there, do the real attach, first the ATA disks */ 1047 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1048 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1049 continue; 1050 if (scp->devices & ATA_ATA_MASTER) 1051 ad_attach(scp, ATA_MASTER); 1052 if (scp->devices & ATA_ATA_SLAVE) 1053 ad_attach(scp, ATA_SLAVE); 1054 } 1055#endif 1056#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1057 /* then the atapi devices */ 1058 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1059 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1060 continue; 1061 if (scp->devices & ATA_ATAPI_MASTER) 1062 atapi_attach(scp, ATA_MASTER); 1063 if (scp->devices & ATA_ATAPI_SLAVE) 1064 atapi_attach(scp, ATA_SLAVE); 1065 } 1066#endif 1067 if (ata_delayed_attach) { 1068 config_intrhook_disestablish(ata_delayed_attach); 1069 free(ata_delayed_attach, M_ATA); 1070 ata_delayed_attach = NULL; 1071 } 1072} 1073 1074static void 1075ata_intr(void *data) 1076{ 1077 struct ata_softc *scp = (struct ata_softc *)data; 1078 struct ata_pci_softc *sc = device_get_softc(device_get_parent(scp->dev)); 1079 u_int8_t dmastat = 0; 1080 1081 /* 1082 * since we might share the IRQ with another device, and in some 1083 * cases with our twin channel, we only want to process interrupts 1084 * that we know this channel generated. 1085 */ 1086 switch (scp->chiptype) { 1087#if NPCI > 0 1088 case 0x00041103: /* HighPoint HPT366/368/370 */ 1089 if (((dmastat = ata_dmastatus(scp)) & 1090 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT) 1091 return; 1092 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1093 break; 1094 1095 case 0x06481095: /* CMD 648 */ 1096 case 0x06491095: /* CMD 649 */ 1097 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) & 1098 (scp->channel ? 0x08 : 0x04))) 1099 return; 1100 goto out; 1101 1102 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */ 1103 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */ 1104 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */ 1105 case 0x0d30105a: /* Promise OEM ATA100 */ 1106 if (!(inl(rman_get_start(sc->bmio) + 0x1c) & 1107 (scp->channel ? 0x00004000 : 0x00000400))) 1108 return; 1109 /* FALLTHROUGH */ 1110out: 1111#endif 1112 default: 1113 if (scp->flags & ATA_DMA_ACTIVE) { 1114 if (((dmastat = ata_dmastatus(scp)) & 1115 (ATA_BMSTAT_ACTIVE|ATA_BMSTAT_INTERRUPT))!=ATA_BMSTAT_INTERRUPT) 1116 return; 1117 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1118 } 1119 } 1120 DELAY(1); 1121 1122 /* if drive is busy it didn't interrupt */ 1123 if (inb(scp->altioaddr) & ATA_S_BUSY) 1124 return; 1125 1126 /* clear interrupt and get status */ 1127 scp->status = inb(scp->ioaddr + ATA_STATUS); 1128 1129 if (scp->status & ATA_S_ERROR) 1130 scp->error = inb(scp->ioaddr + ATA_ERROR); 1131 1132 /* find & call the responsible driver to process this interrupt */ 1133 switch (scp->active) { 1134#if NATADISK > 0 1135 case ATA_ACTIVE_ATA: 1136 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES) 1137 return; 1138 break; 1139#endif 1140#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1141 case ATA_ACTIVE_ATAPI: 1142 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 1143 return; 1144 break; 1145#endif 1146 case ATA_WAIT_INTR: 1147 wakeup((caddr_t)scp); 1148 break; 1149 1150 case ATA_WAIT_READY: 1151 break; 1152 1153 case ATA_REINITING: 1154 return; 1155 1156 case ATA_IDLE: 1157 if (scp->flags & ATA_QUEUED) { 1158 scp->active = ATA_ACTIVE; 1159 if (ata_service(scp) == ATA_OP_CONTINUES) 1160 return; 1161 } 1162 /* FALLTHROUGH */ 1163 1164 default: 1165#ifdef ATA_DEBUG 1166 { 1167 static int intr_count = 0; 1168 1169 if (intr_count++ < 10) 1170 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n", 1171 intr_count, scp->status); 1172 } 1173#endif 1174 } 1175 scp->active = ATA_IDLE; 1176 scp->running = NULL; 1177 ata_start(scp); 1178 return; 1179} 1180 1181void 1182ata_start(struct ata_softc *scp) 1183{ 1184#if NATADISK > 0 1185 struct ad_request *ad_request; 1186#endif 1187#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1188 struct atapi_request *atapi_request; 1189#endif 1190 1191 if (scp->active != ATA_IDLE) 1192 return; 1193 1194 scp->active = ATA_ACTIVE; 1195 1196#if NATADISK > 0 1197 /* find & call the responsible driver if anything on the ATA queue */ 1198 if (TAILQ_EMPTY(&scp->ata_queue)) { 1199 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1200 ad_start((struct ad_softc *)scp->dev_softc[0]); 1201 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1202 ad_start((struct ad_softc *)scp->dev_softc[1]); 1203 } 1204 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 1205 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 1206 scp->active = ATA_ACTIVE_ATA; 1207 scp->running = ad_request; 1208 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) 1209 return; 1210 } 1211 1212#endif 1213#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1214 /* find & call the responsible driver if anything on the ATAPI queue */ 1215 if (TAILQ_EMPTY(&scp->atapi_queue)) { 1216 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1217 atapi_start((struct atapi_softc *)scp->dev_softc[0]); 1218 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1219 atapi_start((struct atapi_softc *)scp->dev_softc[1]); 1220 } 1221 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 1222 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 1223 scp->active = ATA_ACTIVE_ATAPI; 1224 scp->running = atapi_request; 1225 atapi_transfer(atapi_request); 1226 return; 1227 } 1228#endif 1229 scp->active = ATA_IDLE; 1230} 1231 1232void 1233ata_reset(struct ata_softc *scp, int *mask) 1234{ 1235 int timeout; 1236 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY; 1237 1238 /* reset channel */ 1239 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1240 DELAY(1); 1241 inb(scp->ioaddr + ATA_STATUS); 1242 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 1243 DELAY(10000); 1244 outb(scp->altioaddr, ATA_A_IDS); 1245 DELAY(10000); 1246 inb(scp->ioaddr + ATA_ERROR); 1247 DELAY(3000); 1248 1249 /* wait for BUSY to go inactive */ 1250 for (timeout = 0; timeout < 310000; timeout++) { 1251 if (status0 & ATA_S_BUSY) { 1252 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1253 DELAY(1); 1254 status0 = inb(scp->ioaddr + ATA_STATUS); 1255 if (!(status0 & ATA_S_BUSY)) { 1256 /* check for ATAPI signature while its still there */ 1257 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 1258 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) 1259 scp->devices |= ATA_ATAPI_MASTER; 1260 } 1261 } 1262 if (status1 & ATA_S_BUSY) { 1263 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 1264 DELAY(1); 1265 status1 = inb(scp->ioaddr + ATA_STATUS); 1266 if (!(status1 & ATA_S_BUSY)) { 1267 /* check for ATAPI signature while its still there */ 1268 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 1269 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) 1270 scp->devices |= ATA_ATAPI_SLAVE; 1271 } 1272 } 1273 if (*mask == 0x01) /* wait for master only */ 1274 if (!(status0 & ATA_S_BUSY)) 1275 break; 1276 if (*mask == 0x02) /* wait for slave only */ 1277 if (!(status1 & ATA_S_BUSY)) 1278 break; 1279 if (*mask == 0x03) /* wait for both master & slave */ 1280 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 1281 break; 1282 DELAY(100); 1283 } 1284 DELAY(1); 1285 outb(scp->altioaddr, ATA_A_4BIT); 1286 if (status0 & ATA_S_BUSY) 1287 *mask &= ~0x01; 1288 if (status1 & ATA_S_BUSY) 1289 *mask &= ~0x02; 1290 if (bootverbose) 1291 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 1292 *mask, status0, status1); 1293 if (!mask) { 1294 scp->devices = 0; 1295 return; 1296 } 1297 /* 1298 * OK, we have at least one device on the chain, checks for ATAPI 1299 * already done, if none check if its a good old ATA device. 1300 */ 1301 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 1302 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 1303 DELAY(1); 1304 outb(scp->ioaddr + ATA_ERROR, 0x58); 1305 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1306 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 1307 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 1308 scp->devices |= ATA_ATA_MASTER; 1309 } 1310 } 1311 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 1312 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 1313 DELAY(1); 1314 outb(scp->ioaddr + ATA_ERROR, 0x58); 1315 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1316 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 1317 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 1318 scp->devices |= ATA_ATA_SLAVE; 1319 } 1320 } 1321} 1322 1323int 1324ata_reinit(struct ata_softc *scp) 1325{ 1326 int mask = 0, omask; 1327 1328 scp->active = ATA_REINITING; 1329 scp->running = NULL; 1330 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 1331 mask |= 0x01; 1332 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 1333 mask |= 0x02; 1334 if (mask) { 1335 omask = mask; 1336 ata_printf(scp, -1, "resetting devices .. "); 1337 ata_reset(scp, &mask); 1338 if (omask != mask) 1339 printf(" device dissapeared! %d ", omask & ~mask); 1340 1341#if NATADISK > 0 1342 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1343 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 1344 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1345 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 1346#endif 1347#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1348 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1349 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 1350 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1351 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 1352#endif 1353 printf("done\n"); 1354 } 1355 scp->active = ATA_IDLE; 1356 ata_start(scp); 1357 return 0; 1358} 1359 1360static int 1361ata_service(struct ata_softc *scp) 1362{ 1363 /* do we have a SERVICE request from the drive ? */ 1364 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){ 1365 outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT); 1366#if NATADISK > 0 1367 if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 1368 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0]) 1369 return ad_service((struct ad_softc *)scp->dev_softc[0], 0); 1370 } 1371 else { 1372 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1]) 1373 return ad_service((struct ad_softc *)scp->dev_softc[1], 0); 1374 } 1375#endif 1376 } 1377 return ATA_OP_FINISHED; 1378} 1379 1380int 1381ata_wait(struct ata_softc *scp, int device, u_int8_t mask) 1382{ 1383 int timeout = 0; 1384 int statio = scp->ioaddr + ATA_STATUS; 1385 1386 DELAY(1); 1387 while (timeout < 5000000) { /* timeout 5 secs */ 1388 scp->status = inb(statio); 1389 1390 /* if drive fails status, reselect the drive just to be sure */ 1391 if (scp->status == 0xff) { 1392 ata_printf(scp, device, "no status, reselecting device\n"); 1393 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1394 DELAY(1); 1395 scp->status = inb(statio); 1396 } 1397 1398 /* are we done ? */ 1399 if (!(scp->status & ATA_S_BUSY)) 1400 break; 1401 1402 if (timeout > 1000) { 1403 timeout += 1000; 1404 DELAY(1000); 1405 } 1406 else { 1407 timeout += 10; 1408 DELAY(10); 1409 } 1410 } 1411 if (scp->status & ATA_S_ERROR) 1412 scp->error = inb(scp->ioaddr + ATA_ERROR); 1413 if (timeout >= 5000000) 1414 return -1; 1415 if (!mask) 1416 return (scp->status & ATA_S_ERROR); 1417 1418 /* Wait 50 msec for bits wanted. */ 1419 timeout = 5000; 1420 while (timeout--) { 1421 scp->status = inb(statio); 1422 if ((scp->status & mask) == mask) { 1423 if (scp->status & ATA_S_ERROR) 1424 scp->error = inb(scp->ioaddr + ATA_ERROR); 1425 return (scp->status & ATA_S_ERROR); 1426 } 1427 DELAY (10); 1428 } 1429 return -1; 1430} 1431 1432int 1433ata_command(struct ata_softc *scp, int device, u_int8_t command, 1434 u_int16_t cylinder, u_int8_t head, u_int8_t sector, 1435 u_int8_t count, u_int8_t feature, int flags) 1436{ 1437 int error = 0; 1438#ifdef ATA_DEBUG 1439 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, " 1440 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n", 1441 scp->ioaddr, command, cylinder, head, sector, 1442 count, feature, flags); 1443#endif 1444 1445 /* disable interrupt from device */ 1446 if (scp->flags & ATA_QUEUED) 1447 outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT); 1448 1449 /* select device */ 1450 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1451 1452 /* ready to issue command ? */ 1453 if (ata_wait(scp, device, 0) < 0) { 1454 ata_printf(scp, device, 1455 "timeout waiting to give command=%02x s=%02x e=%02x\n", 1456 command, scp->status, scp->error); 1457 return -1; 1458 } 1459 1460 outb(scp->ioaddr + ATA_FEATURE, feature); 1461 outb(scp->ioaddr + ATA_COUNT, count); 1462 outb(scp->ioaddr + ATA_SECTOR, sector); 1463 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 1464 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 1465 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 1466 1467 switch (flags) { 1468 case ATA_WAIT_INTR: 1469 if (scp->active != ATA_IDLE) 1470 ata_printf(scp, device, "WARNING: WAIT_INTR active=%s\n", 1471 active2str(scp->active)); 1472 scp->active = ATA_WAIT_INTR; 1473 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz); 1474 outb(scp->ioaddr + ATA_CMD, command); 1475 1476 /* enable interrupt */ 1477 if (scp->flags & ATA_QUEUED) 1478 outb(scp->altioaddr, ATA_A_4BIT); 1479 1480 if (await(PRIBIO, 10 * hz)) { 1481 ata_printf(scp, device, "ata_command: timeout waiting for intr\n"); 1482 scp->active = ATA_IDLE; 1483 error = -1; 1484 } 1485 break; 1486 1487 case ATA_WAIT_READY: 1488 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 1489 ata_printf(scp, device, "WARNING: WAIT_READY active=%s\n", 1490 active2str(scp->active)); 1491 if (scp->active != ATA_REINITING) 1492 scp->active = ATA_WAIT_READY; 1493 outb(scp->ioaddr + ATA_CMD, command); 1494 if (ata_wait(scp, device, ATA_S_READY) < 0) { 1495 ata_printf(scp, device, 1496 "timeout waiting for command=%02x s=%02x e=%02x\n", 1497 command, scp->status, scp->error); 1498 error = -1; 1499 } 1500 if (scp->active != ATA_REINITING) 1501 scp->active = ATA_IDLE; 1502 break; 1503 1504 case ATA_IMMEDIATE: 1505 outb(scp->ioaddr + ATA_CMD, command); 1506 break; 1507 1508 default: 1509 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n", 1510 active2str(flags)); 1511 } 1512 /* enable interrupt */ 1513 if (scp->flags & ATA_QUEUED) 1514 outb(scp->altioaddr, ATA_A_4BIT); 1515 return error; 1516} 1517 1518int 1519ata_get_lun(u_int32_t *map) 1520{ 1521 int lun = ffs(~*map) - 1; 1522 1523 *map |= (1 << lun); 1524 return lun; 1525} 1526 1527void 1528ata_free_lun(u_int32_t *map, int lun) 1529{ 1530 *map &= ~(1 << lun); 1531} 1532 1533int 1534ata_printf(struct ata_softc *scp, int device, const char * fmt, ...) 1535{ 1536 va_list ap; 1537 int ret; 1538 1539 if (device == -1) 1540 ret = printf("ata%d: ", device_get_unit(scp->dev)); 1541 else 1542 ret = printf("ata%d-%s: ", device_get_unit(scp->dev), 1543 (device == ATA_MASTER) ? "master" : "slave"); 1544 va_start(ap, fmt); 1545 ret += vprintf(fmt, ap); 1546 va_end(ap); 1547 return ret; 1548} 1549 1550char * 1551ata_mode2str(int mode) 1552{ 1553 switch (mode) { 1554 case ATA_PIO: return "BIOSPIO"; 1555 case ATA_PIO0: return "PIO0"; 1556 case ATA_PIO1: return "PIO1"; 1557 case ATA_PIO2: return "PIO2"; 1558 case ATA_PIO3: return "PIO3"; 1559 case ATA_PIO4: return "PIO4"; 1560 case ATA_WDMA2: return "WDMA2"; 1561 case ATA_UDMA2: return "UDMA33"; 1562 case ATA_UDMA4: return "UDMA66"; 1563 case ATA_UDMA5: return "UDMA100"; 1564 case ATA_DMA: return "BIOSDMA"; 1565 default: return "???"; 1566 } 1567} 1568 1569int 1570ata_pio2mode(int pio) 1571{ 1572 switch (pio) { 1573 default: 1574 case 0: return ATA_PIO0; 1575 case 1: return ATA_PIO1; 1576 case 2: return ATA_PIO2; 1577 case 3: return ATA_PIO3; 1578 case 4: return ATA_PIO4; 1579 } 1580} 1581 1582int 1583ata_pmode(struct ata_params *ap) 1584{ 1585 if (ap->atavalid & ATA_FLAG_64_70) { 1586 if (ap->apiomodes & 2) 1587 return 4; 1588 if (ap->apiomodes & 1) 1589 return 3; 1590 } 1591 if (ap->opiomode == 2) 1592 return 2; 1593 if (ap->opiomode == 1) 1594 return 1; 1595 if (ap->opiomode == 0) 1596 return 0; 1597 return -1; 1598} 1599 1600int 1601ata_wmode(struct ata_params *ap) 1602{ 1603 if (ap->wdmamodes & 4) 1604 return 2; 1605 if (ap->wdmamodes & 2) 1606 return 1; 1607 if (ap->wdmamodes & 1) 1608 return 0; 1609 return -1; 1610} 1611 1612int 1613ata_umode(struct ata_params *ap) 1614{ 1615 if (ap->atavalid & ATA_FLAG_88) { 1616 if (ap->udmamodes & 0x20) 1617 return 5; 1618 if (ap->udmamodes & 0x10) 1619 return 4; 1620 if (ap->udmamodes & 0x08) 1621 return 3; 1622 if (ap->udmamodes & 0x04) 1623 return 2; 1624 if (ap->udmamodes & 0x02) 1625 return 1; 1626 if (ap->udmamodes & 0x01) 1627 return 0; 1628 } 1629 return -1; 1630} 1631 1632static char * 1633active2str(int active) 1634{ 1635 static char buf[8]; 1636 1637 switch (active) { 1638 case ATA_IDLE: 1639 return("ATA_IDLE"); 1640 case ATA_IMMEDIATE: 1641 return("ATA_IMMEDIATE"); 1642 case ATA_WAIT_INTR: 1643 return("ATA_WAIT_INTR"); 1644 case ATA_WAIT_READY: 1645 return("ATA_WAIT_READY"); 1646 case ATA_ACTIVE: 1647 return("ATA_ACTIVE"); 1648 case ATA_ACTIVE_ATA: 1649 return("ATA_ACTIVE_ATA"); 1650 case ATA_ACTIVE_ATAPI: 1651 return("ATA_ACTIVE_ATAPI"); 1652 case ATA_REINITING: 1653 return("ATA_REINITING"); 1654 default: 1655 sprintf(buf, "0x%02x", active); 1656 return buf; 1657 } 1658} 1659 1660static void 1661bswap(int8_t *buf, int len) 1662{ 1663 u_int16_t *ptr = (u_int16_t*)(buf + len); 1664 1665 while (--ptr >= (u_int16_t*)buf) 1666 *ptr = ntohs(*ptr); 1667} 1668 1669static void 1670btrim(int8_t *buf, int len) 1671{ 1672 int8_t *ptr; 1673 1674 for (ptr = buf; ptr < buf+len; ++ptr) 1675 if (!*ptr) 1676 *ptr = ' '; 1677 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1678 *ptr = 0; 1679} 1680 1681static void 1682bpack(int8_t *src, int8_t *dst, int len) 1683{ 1684 int i, j, blank; 1685 1686 for (i = j = blank = 0 ; i < len; i++) { 1687 if (blank && src[i] == ' ') continue; 1688 if (blank && src[i] != ' ') { 1689 dst[j++] = src[i]; 1690 blank = 0; 1691 continue; 1692 } 1693 if (src[i] == ' ') { 1694 blank = 1; 1695 if (i == 0) 1696 continue; 1697 } 1698 dst[j++] = src[i]; 1699 } 1700 if (j < len) 1701 dst[j] = 0x00; 1702} 1703 1704static void 1705ata_change_mode(struct ata_softc *scp, int device, int mode) 1706{ 1707 int s = splbio(); 1708 1709 while (scp->active != ATA_IDLE) 1710 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 1711 scp->active = ATA_REINITING; 1712 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)), 1713 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)), 1714 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device))); 1715 scp->active = ATA_IDLE; 1716 ata_start(scp); 1717 splx(s); 1718} 1719 1720static int 1721sysctl_hw_ata(SYSCTL_HANDLER_ARGS) 1722{ 1723 struct ata_softc *scp; 1724 int ctlr, error, i; 1725 1726 /* readout internal state */ 1727 bzero(ata_conf, sizeof(ata_conf)); 1728 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1729 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1730 continue; 1731 for (i = 0; i < 2; i++) { 1732 if (!scp->dev_softc[i]) 1733 strcat(ata_conf, "---,"); 1734 else if (scp->mode[i] >= ATA_DMA) 1735 strcat(ata_conf, "dma,"); 1736 else 1737 strcat(ata_conf, "pio,"); 1738 } 1739 } 1740 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req); 1741 if (error == 0 && req->newptr != NULL) { 1742 char *ptr = ata_conf; 1743 1744 /* update internal state */ 1745 i = 0; 1746 while (*ptr) { 1747 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) { 1748 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1749 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA) 1750 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO); 1751 } 1752 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) { 1753 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1754 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA) 1755 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA); 1756 } 1757 else if (strncmp(ptr, "---", 3)) 1758 break; 1759 ptr+=3; 1760 if (*ptr++ != ',' || 1761 ++i > (devclass_get_maxunit(ata_devclass) << 1)) 1762 break; 1763 } 1764 } 1765 return error; 1766} 1767SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW, 1768 0, sizeof(ata_conf), sysctl_hw_ata, "A", ""); 1769 1770static void 1771ata_init(void) 1772{ 1773 /* register boot attach to be run when interrupts are enabled */ 1774 if (!(ata_delayed_attach = (struct intr_config_hook *) 1775 malloc(sizeof(struct intr_config_hook), 1776 M_TEMP, M_NOWAIT))) { 1777 printf("ata: malloc of delayed attach hook failed\n"); 1778 return; 1779 } 1780 bzero(ata_delayed_attach, sizeof(struct intr_config_hook)); 1781 1782 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1783 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1784 printf("ata: config_intrhook_establish failed\n"); 1785 free(ata_delayed_attach, M_TEMP); 1786 } 1787} 1788SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
| 800 altioaddr = rman_get_start(altio); 801 else 802 altioaddr = rman_get_start(altio) + 0x02; 803 } 804 else 805 altioaddr = ioaddr + ATA_IOSIZE; 806 807 rid = ATA_BMADDR_RID; 808 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 809 bmaddr = bmio ? rman_get_start(bmio) : 0; 810 811 /* store the IO resources for eventual later release */ 812 scp->r_io = io; 813 scp->r_altio = altio; 814 scp->r_bmio = bmio; 815 816 /* store the physical IO addresse for easy access */ 817 scp->ioaddr = ioaddr; 818 scp->altioaddr = altioaddr; 819 scp->bmaddr = bmaddr; 820 821 if (bootverbose) 822 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 823 scp->ioaddr, scp->altioaddr, scp->bmaddr); 824 825 /* do we have any signs of ATA/ATAPI HW being present ? */ 826 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 827 DELAY(1); 828 status0 = inb(scp->ioaddr + ATA_STATUS); 829 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 830 DELAY(1); 831 status1 = inb(scp->ioaddr + ATA_STATUS); 832 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5) 833 mask |= 0x01; 834 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5) 835 mask |= 0x02; 836 if (bootverbose) 837 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 838 mask, status0, status1); 839 if (!mask) 840 goto failure; 841 842 ata_reset(scp, &mask); 843 844 if (bootverbose) 845 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices); 846 847 if (!mask) 848 goto failure; 849 850 TAILQ_INIT(&scp->ata_queue); 851 TAILQ_INIT(&scp->atapi_queue); 852 return 0; 853 854failure: 855 if (io) 856 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 857 if (altio) 858 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio); 859 if (bmio) 860 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio); 861 if (bootverbose) 862 ata_printf(scp, -1, "probe allocation failed\n"); 863 return ENXIO; 864} 865 866static int 867ata_attach(device_t dev) 868{ 869 struct ata_softc *scp = device_get_softc(dev); 870 int error, rid = 0; 871 872 if (!scp || scp->flags & ATA_ATTACHED) 873 return ENXIO; 874 875 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 876 RF_SHAREABLE | RF_ACTIVE); 877 if (!scp->r_irq) { 878 ata_printf(scp, -1, "unable to allocate interrupt\n"); 879 return ENXIO; 880 } 881 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr, 882 scp, &scp->ih))) 883 return error; 884 885 /* 886 * do not attach devices if we are in early boot, this is done later 887 * when interrupts are enabled by a hook into the boot process. 888 * otherwise attach what the probe has found in scp->devices. 889 */ 890 if (!ata_delayed_attach) { 891 if (scp->devices & ATA_ATA_SLAVE) 892 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 893 scp->devices &= ~ATA_ATA_SLAVE; 894 if (scp->devices & ATA_ATAPI_SLAVE) 895 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 896 scp->devices &= ~ATA_ATAPI_SLAVE; 897 if (scp->devices & ATA_ATA_MASTER) 898 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 899 scp->devices &= ~ATA_ATA_MASTER; 900 if (scp->devices & ATA_ATAPI_MASTER) 901 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 902 scp->devices &= ~ATA_ATAPI_MASTER; 903#if NATADISK > 0 904 if (scp->devices & ATA_ATA_MASTER) 905 ad_attach(scp, ATA_MASTER); 906 if (scp->devices & ATA_ATA_SLAVE) 907 ad_attach(scp, ATA_SLAVE); 908#endif 909#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 910 if (scp->devices & ATA_ATAPI_MASTER) 911 atapi_attach(scp, ATA_MASTER); 912 if (scp->devices & ATA_ATAPI_SLAVE) 913 atapi_attach(scp, ATA_SLAVE); 914#endif 915 } 916 scp->flags |= ATA_ATTACHED; 917 return 0; 918} 919 920static int 921ata_detach(device_t dev) 922{ 923 struct ata_softc *scp = device_get_softc(dev); 924 925 if (!scp || !(scp->flags & ATA_ATTACHED)) 926 return ENXIO; 927 928#if NATADISK > 0 929 if (scp->devices & ATA_ATA_MASTER) 930 ad_detach(scp->dev_softc[0]); 931 if (scp->devices & ATA_ATA_SLAVE) 932 ad_detach(scp->dev_softc[1]); 933#endif 934#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 935 if (scp->devices & ATA_ATAPI_MASTER) 936 atapi_detach(scp->dev_softc[0]); 937 if (scp->devices & ATA_ATAPI_SLAVE) 938 atapi_detach(scp->dev_softc[1]); 939#endif 940 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) { 941 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA); 942 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL; 943 } 944 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) { 945 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA); 946 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL; 947 } 948 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL; 949 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL; 950 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO; 951 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO; 952 bus_teardown_intr(dev, scp->r_irq, scp->ih); 953 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq); 954 if (scp->r_bmio) 955 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio); 956 if (scp->r_altio) 957 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio); 958 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io); 959 scp->flags &= ~ATA_ATTACHED; 960 return 0; 961} 962 963static int 964ata_resume(device_t dev) 965{ 966 struct ata_softc *scp = device_get_softc(dev); 967 968 ata_reinit(scp); 969 return 0; 970} 971 972static int 973ata_getparam(struct ata_softc *scp, int device, u_int8_t command) 974{ 975 struct ata_params *ata_parm; 976 int8_t buffer[DEV_BSIZE]; 977 int retry = 0; 978 979 /* select drive */ 980 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 981 DELAY(1); 982 983 /* enable interrupt */ 984 outb(scp->altioaddr, ATA_A_4BIT); 985 DELAY(1); 986 987 /* apparently some devices needs this repeated */ 988 do { 989 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) { 990 ata_printf(scp, device, "identify failed\n"); 991 return -1; 992 } 993 if (retry++ > 4) { 994 ata_printf(scp, device, "identify retries exceeded\n"); 995 return -1; 996 } 997 } while (ata_wait(scp, device, 998 ((command == ATA_C_ATAPI_IDENTIFY) ? 999 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)))); 1000 1001 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t)); 1002 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 1003 if (!ata_parm) { 1004 ata_printf(scp, device, "malloc for identify data failed\n"); 1005 return -1; 1006 } 1007 bcopy(buffer, ata_parm, sizeof(struct ata_params)); 1008 if (command == ATA_C_ATA_IDENTIFY || 1009 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 1010 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X'))) 1011 bswap(ata_parm->model, sizeof(ata_parm->model)); 1012 btrim(ata_parm->model, sizeof(ata_parm->model)); 1013 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 1014 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 1015 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 1016 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 1017 scp->dev_param[ATA_DEV(device)] = ata_parm; 1018 return 0; 1019} 1020 1021static void 1022ata_boot_attach(void) 1023{ 1024 struct ata_softc *scp; 1025 int ctlr; 1026 1027 /* 1028 * run through all ata devices and look for real ATA & ATAPI devices 1029 * using the hints we found in the early probe, this avoids some of 1030 * the delays probing of non-exsistent devices can cause. 1031 */ 1032 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1033 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1034 continue; 1035 if (scp->devices & ATA_ATA_SLAVE) 1036 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 1037 scp->devices &= ~ATA_ATA_SLAVE; 1038 if (scp->devices & ATA_ATAPI_SLAVE) 1039 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 1040 scp->devices &= ~ATA_ATAPI_SLAVE; 1041 if (scp->devices & ATA_ATA_MASTER) 1042 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 1043 scp->devices &= ~ATA_ATA_MASTER; 1044 if (scp->devices & ATA_ATAPI_MASTER) 1045 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 1046 scp->devices &= ~ATA_ATAPI_MASTER; 1047 } 1048 1049#if NATADISK > 0 1050 /* now we know whats there, do the real attach, first the ATA disks */ 1051 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1052 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1053 continue; 1054 if (scp->devices & ATA_ATA_MASTER) 1055 ad_attach(scp, ATA_MASTER); 1056 if (scp->devices & ATA_ATA_SLAVE) 1057 ad_attach(scp, ATA_SLAVE); 1058 } 1059#endif 1060#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1061 /* then the atapi devices */ 1062 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1063 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1064 continue; 1065 if (scp->devices & ATA_ATAPI_MASTER) 1066 atapi_attach(scp, ATA_MASTER); 1067 if (scp->devices & ATA_ATAPI_SLAVE) 1068 atapi_attach(scp, ATA_SLAVE); 1069 } 1070#endif 1071 if (ata_delayed_attach) { 1072 config_intrhook_disestablish(ata_delayed_attach); 1073 free(ata_delayed_attach, M_ATA); 1074 ata_delayed_attach = NULL; 1075 } 1076} 1077 1078static void 1079ata_intr(void *data) 1080{ 1081 struct ata_softc *scp = (struct ata_softc *)data; 1082 struct ata_pci_softc *sc = device_get_softc(device_get_parent(scp->dev)); 1083 u_int8_t dmastat = 0; 1084 1085 /* 1086 * since we might share the IRQ with another device, and in some 1087 * cases with our twin channel, we only want to process interrupts 1088 * that we know this channel generated. 1089 */ 1090 switch (scp->chiptype) { 1091#if NPCI > 0 1092 case 0x00041103: /* HighPoint HPT366/368/370 */ 1093 if (((dmastat = ata_dmastatus(scp)) & 1094 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT) 1095 return; 1096 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1097 break; 1098 1099 case 0x06481095: /* CMD 648 */ 1100 case 0x06491095: /* CMD 649 */ 1101 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) & 1102 (scp->channel ? 0x08 : 0x04))) 1103 return; 1104 goto out; 1105 1106 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */ 1107 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */ 1108 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */ 1109 case 0x0d30105a: /* Promise OEM ATA100 */ 1110 if (!(inl(rman_get_start(sc->bmio) + 0x1c) & 1111 (scp->channel ? 0x00004000 : 0x00000400))) 1112 return; 1113 /* FALLTHROUGH */ 1114out: 1115#endif 1116 default: 1117 if (scp->flags & ATA_DMA_ACTIVE) { 1118 if (((dmastat = ata_dmastatus(scp)) & 1119 (ATA_BMSTAT_ACTIVE|ATA_BMSTAT_INTERRUPT))!=ATA_BMSTAT_INTERRUPT) 1120 return; 1121 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT); 1122 } 1123 } 1124 DELAY(1); 1125 1126 /* if drive is busy it didn't interrupt */ 1127 if (inb(scp->altioaddr) & ATA_S_BUSY) 1128 return; 1129 1130 /* clear interrupt and get status */ 1131 scp->status = inb(scp->ioaddr + ATA_STATUS); 1132 1133 if (scp->status & ATA_S_ERROR) 1134 scp->error = inb(scp->ioaddr + ATA_ERROR); 1135 1136 /* find & call the responsible driver to process this interrupt */ 1137 switch (scp->active) { 1138#if NATADISK > 0 1139 case ATA_ACTIVE_ATA: 1140 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES) 1141 return; 1142 break; 1143#endif 1144#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1145 case ATA_ACTIVE_ATAPI: 1146 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 1147 return; 1148 break; 1149#endif 1150 case ATA_WAIT_INTR: 1151 wakeup((caddr_t)scp); 1152 break; 1153 1154 case ATA_WAIT_READY: 1155 break; 1156 1157 case ATA_REINITING: 1158 return; 1159 1160 case ATA_IDLE: 1161 if (scp->flags & ATA_QUEUED) { 1162 scp->active = ATA_ACTIVE; 1163 if (ata_service(scp) == ATA_OP_CONTINUES) 1164 return; 1165 } 1166 /* FALLTHROUGH */ 1167 1168 default: 1169#ifdef ATA_DEBUG 1170 { 1171 static int intr_count = 0; 1172 1173 if (intr_count++ < 10) 1174 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n", 1175 intr_count, scp->status); 1176 } 1177#endif 1178 } 1179 scp->active = ATA_IDLE; 1180 scp->running = NULL; 1181 ata_start(scp); 1182 return; 1183} 1184 1185void 1186ata_start(struct ata_softc *scp) 1187{ 1188#if NATADISK > 0 1189 struct ad_request *ad_request; 1190#endif 1191#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1192 struct atapi_request *atapi_request; 1193#endif 1194 1195 if (scp->active != ATA_IDLE) 1196 return; 1197 1198 scp->active = ATA_ACTIVE; 1199 1200#if NATADISK > 0 1201 /* find & call the responsible driver if anything on the ATA queue */ 1202 if (TAILQ_EMPTY(&scp->ata_queue)) { 1203 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1204 ad_start((struct ad_softc *)scp->dev_softc[0]); 1205 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1206 ad_start((struct ad_softc *)scp->dev_softc[1]); 1207 } 1208 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 1209 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 1210 scp->active = ATA_ACTIVE_ATA; 1211 scp->running = ad_request; 1212 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) 1213 return; 1214 } 1215 1216#endif 1217#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1218 /* find & call the responsible driver if anything on the ATAPI queue */ 1219 if (TAILQ_EMPTY(&scp->atapi_queue)) { 1220 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1221 atapi_start((struct atapi_softc *)scp->dev_softc[0]); 1222 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1223 atapi_start((struct atapi_softc *)scp->dev_softc[1]); 1224 } 1225 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 1226 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 1227 scp->active = ATA_ACTIVE_ATAPI; 1228 scp->running = atapi_request; 1229 atapi_transfer(atapi_request); 1230 return; 1231 } 1232#endif 1233 scp->active = ATA_IDLE; 1234} 1235 1236void 1237ata_reset(struct ata_softc *scp, int *mask) 1238{ 1239 int timeout; 1240 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY; 1241 1242 /* reset channel */ 1243 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1244 DELAY(1); 1245 inb(scp->ioaddr + ATA_STATUS); 1246 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 1247 DELAY(10000); 1248 outb(scp->altioaddr, ATA_A_IDS); 1249 DELAY(10000); 1250 inb(scp->ioaddr + ATA_ERROR); 1251 DELAY(3000); 1252 1253 /* wait for BUSY to go inactive */ 1254 for (timeout = 0; timeout < 310000; timeout++) { 1255 if (status0 & ATA_S_BUSY) { 1256 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 1257 DELAY(1); 1258 status0 = inb(scp->ioaddr + ATA_STATUS); 1259 if (!(status0 & ATA_S_BUSY)) { 1260 /* check for ATAPI signature while its still there */ 1261 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 1262 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) 1263 scp->devices |= ATA_ATAPI_MASTER; 1264 } 1265 } 1266 if (status1 & ATA_S_BUSY) { 1267 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 1268 DELAY(1); 1269 status1 = inb(scp->ioaddr + ATA_STATUS); 1270 if (!(status1 & ATA_S_BUSY)) { 1271 /* check for ATAPI signature while its still there */ 1272 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 1273 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) 1274 scp->devices |= ATA_ATAPI_SLAVE; 1275 } 1276 } 1277 if (*mask == 0x01) /* wait for master only */ 1278 if (!(status0 & ATA_S_BUSY)) 1279 break; 1280 if (*mask == 0x02) /* wait for slave only */ 1281 if (!(status1 & ATA_S_BUSY)) 1282 break; 1283 if (*mask == 0x03) /* wait for both master & slave */ 1284 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY)) 1285 break; 1286 DELAY(100); 1287 } 1288 DELAY(1); 1289 outb(scp->altioaddr, ATA_A_4BIT); 1290 if (status0 & ATA_S_BUSY) 1291 *mask &= ~0x01; 1292 if (status1 & ATA_S_BUSY) 1293 *mask &= ~0x02; 1294 if (bootverbose) 1295 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n", 1296 *mask, status0, status1); 1297 if (!mask) { 1298 scp->devices = 0; 1299 return; 1300 } 1301 /* 1302 * OK, we have at least one device on the chain, checks for ATAPI 1303 * already done, if none check if its a good old ATA device. 1304 */ 1305 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 1306 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 1307 DELAY(1); 1308 outb(scp->ioaddr + ATA_ERROR, 0x58); 1309 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1310 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 1311 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 1312 scp->devices |= ATA_ATA_MASTER; 1313 } 1314 } 1315 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 1316 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 1317 DELAY(1); 1318 outb(scp->ioaddr + ATA_ERROR, 0x58); 1319 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 1320 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 1321 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 1322 scp->devices |= ATA_ATA_SLAVE; 1323 } 1324 } 1325} 1326 1327int 1328ata_reinit(struct ata_softc *scp) 1329{ 1330 int mask = 0, omask; 1331 1332 scp->active = ATA_REINITING; 1333 scp->running = NULL; 1334 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 1335 mask |= 0x01; 1336 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 1337 mask |= 0x02; 1338 if (mask) { 1339 omask = mask; 1340 ata_printf(scp, -1, "resetting devices .. "); 1341 ata_reset(scp, &mask); 1342 if (omask != mask) 1343 printf(" device dissapeared! %d ", omask & ~mask); 1344 1345#if NATADISK > 0 1346 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0]) 1347 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 1348 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1]) 1349 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 1350#endif 1351#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 1352 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0]) 1353 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 1354 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1]) 1355 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 1356#endif 1357 printf("done\n"); 1358 } 1359 scp->active = ATA_IDLE; 1360 ata_start(scp); 1361 return 0; 1362} 1363 1364static int 1365ata_service(struct ata_softc *scp) 1366{ 1367 /* do we have a SERVICE request from the drive ? */ 1368 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){ 1369 outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT); 1370#if NATADISK > 0 1371 if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 1372 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0]) 1373 return ad_service((struct ad_softc *)scp->dev_softc[0], 0); 1374 } 1375 else { 1376 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1]) 1377 return ad_service((struct ad_softc *)scp->dev_softc[1], 0); 1378 } 1379#endif 1380 } 1381 return ATA_OP_FINISHED; 1382} 1383 1384int 1385ata_wait(struct ata_softc *scp, int device, u_int8_t mask) 1386{ 1387 int timeout = 0; 1388 int statio = scp->ioaddr + ATA_STATUS; 1389 1390 DELAY(1); 1391 while (timeout < 5000000) { /* timeout 5 secs */ 1392 scp->status = inb(statio); 1393 1394 /* if drive fails status, reselect the drive just to be sure */ 1395 if (scp->status == 0xff) { 1396 ata_printf(scp, device, "no status, reselecting device\n"); 1397 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1398 DELAY(1); 1399 scp->status = inb(statio); 1400 } 1401 1402 /* are we done ? */ 1403 if (!(scp->status & ATA_S_BUSY)) 1404 break; 1405 1406 if (timeout > 1000) { 1407 timeout += 1000; 1408 DELAY(1000); 1409 } 1410 else { 1411 timeout += 10; 1412 DELAY(10); 1413 } 1414 } 1415 if (scp->status & ATA_S_ERROR) 1416 scp->error = inb(scp->ioaddr + ATA_ERROR); 1417 if (timeout >= 5000000) 1418 return -1; 1419 if (!mask) 1420 return (scp->status & ATA_S_ERROR); 1421 1422 /* Wait 50 msec for bits wanted. */ 1423 timeout = 5000; 1424 while (timeout--) { 1425 scp->status = inb(statio); 1426 if ((scp->status & mask) == mask) { 1427 if (scp->status & ATA_S_ERROR) 1428 scp->error = inb(scp->ioaddr + ATA_ERROR); 1429 return (scp->status & ATA_S_ERROR); 1430 } 1431 DELAY (10); 1432 } 1433 return -1; 1434} 1435 1436int 1437ata_command(struct ata_softc *scp, int device, u_int8_t command, 1438 u_int16_t cylinder, u_int8_t head, u_int8_t sector, 1439 u_int8_t count, u_int8_t feature, int flags) 1440{ 1441 int error = 0; 1442#ifdef ATA_DEBUG 1443 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, " 1444 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n", 1445 scp->ioaddr, command, cylinder, head, sector, 1446 count, feature, flags); 1447#endif 1448 1449 /* disable interrupt from device */ 1450 if (scp->flags & ATA_QUEUED) 1451 outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT); 1452 1453 /* select device */ 1454 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 1455 1456 /* ready to issue command ? */ 1457 if (ata_wait(scp, device, 0) < 0) { 1458 ata_printf(scp, device, 1459 "timeout waiting to give command=%02x s=%02x e=%02x\n", 1460 command, scp->status, scp->error); 1461 return -1; 1462 } 1463 1464 outb(scp->ioaddr + ATA_FEATURE, feature); 1465 outb(scp->ioaddr + ATA_COUNT, count); 1466 outb(scp->ioaddr + ATA_SECTOR, sector); 1467 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 1468 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 1469 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 1470 1471 switch (flags) { 1472 case ATA_WAIT_INTR: 1473 if (scp->active != ATA_IDLE) 1474 ata_printf(scp, device, "WARNING: WAIT_INTR active=%s\n", 1475 active2str(scp->active)); 1476 scp->active = ATA_WAIT_INTR; 1477 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz); 1478 outb(scp->ioaddr + ATA_CMD, command); 1479 1480 /* enable interrupt */ 1481 if (scp->flags & ATA_QUEUED) 1482 outb(scp->altioaddr, ATA_A_4BIT); 1483 1484 if (await(PRIBIO, 10 * hz)) { 1485 ata_printf(scp, device, "ata_command: timeout waiting for intr\n"); 1486 scp->active = ATA_IDLE; 1487 error = -1; 1488 } 1489 break; 1490 1491 case ATA_WAIT_READY: 1492 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING) 1493 ata_printf(scp, device, "WARNING: WAIT_READY active=%s\n", 1494 active2str(scp->active)); 1495 if (scp->active != ATA_REINITING) 1496 scp->active = ATA_WAIT_READY; 1497 outb(scp->ioaddr + ATA_CMD, command); 1498 if (ata_wait(scp, device, ATA_S_READY) < 0) { 1499 ata_printf(scp, device, 1500 "timeout waiting for command=%02x s=%02x e=%02x\n", 1501 command, scp->status, scp->error); 1502 error = -1; 1503 } 1504 if (scp->active != ATA_REINITING) 1505 scp->active = ATA_IDLE; 1506 break; 1507 1508 case ATA_IMMEDIATE: 1509 outb(scp->ioaddr + ATA_CMD, command); 1510 break; 1511 1512 default: 1513 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n", 1514 active2str(flags)); 1515 } 1516 /* enable interrupt */ 1517 if (scp->flags & ATA_QUEUED) 1518 outb(scp->altioaddr, ATA_A_4BIT); 1519 return error; 1520} 1521 1522int 1523ata_get_lun(u_int32_t *map) 1524{ 1525 int lun = ffs(~*map) - 1; 1526 1527 *map |= (1 << lun); 1528 return lun; 1529} 1530 1531void 1532ata_free_lun(u_int32_t *map, int lun) 1533{ 1534 *map &= ~(1 << lun); 1535} 1536 1537int 1538ata_printf(struct ata_softc *scp, int device, const char * fmt, ...) 1539{ 1540 va_list ap; 1541 int ret; 1542 1543 if (device == -1) 1544 ret = printf("ata%d: ", device_get_unit(scp->dev)); 1545 else 1546 ret = printf("ata%d-%s: ", device_get_unit(scp->dev), 1547 (device == ATA_MASTER) ? "master" : "slave"); 1548 va_start(ap, fmt); 1549 ret += vprintf(fmt, ap); 1550 va_end(ap); 1551 return ret; 1552} 1553 1554char * 1555ata_mode2str(int mode) 1556{ 1557 switch (mode) { 1558 case ATA_PIO: return "BIOSPIO"; 1559 case ATA_PIO0: return "PIO0"; 1560 case ATA_PIO1: return "PIO1"; 1561 case ATA_PIO2: return "PIO2"; 1562 case ATA_PIO3: return "PIO3"; 1563 case ATA_PIO4: return "PIO4"; 1564 case ATA_WDMA2: return "WDMA2"; 1565 case ATA_UDMA2: return "UDMA33"; 1566 case ATA_UDMA4: return "UDMA66"; 1567 case ATA_UDMA5: return "UDMA100"; 1568 case ATA_DMA: return "BIOSDMA"; 1569 default: return "???"; 1570 } 1571} 1572 1573int 1574ata_pio2mode(int pio) 1575{ 1576 switch (pio) { 1577 default: 1578 case 0: return ATA_PIO0; 1579 case 1: return ATA_PIO1; 1580 case 2: return ATA_PIO2; 1581 case 3: return ATA_PIO3; 1582 case 4: return ATA_PIO4; 1583 } 1584} 1585 1586int 1587ata_pmode(struct ata_params *ap) 1588{ 1589 if (ap->atavalid & ATA_FLAG_64_70) { 1590 if (ap->apiomodes & 2) 1591 return 4; 1592 if (ap->apiomodes & 1) 1593 return 3; 1594 } 1595 if (ap->opiomode == 2) 1596 return 2; 1597 if (ap->opiomode == 1) 1598 return 1; 1599 if (ap->opiomode == 0) 1600 return 0; 1601 return -1; 1602} 1603 1604int 1605ata_wmode(struct ata_params *ap) 1606{ 1607 if (ap->wdmamodes & 4) 1608 return 2; 1609 if (ap->wdmamodes & 2) 1610 return 1; 1611 if (ap->wdmamodes & 1) 1612 return 0; 1613 return -1; 1614} 1615 1616int 1617ata_umode(struct ata_params *ap) 1618{ 1619 if (ap->atavalid & ATA_FLAG_88) { 1620 if (ap->udmamodes & 0x20) 1621 return 5; 1622 if (ap->udmamodes & 0x10) 1623 return 4; 1624 if (ap->udmamodes & 0x08) 1625 return 3; 1626 if (ap->udmamodes & 0x04) 1627 return 2; 1628 if (ap->udmamodes & 0x02) 1629 return 1; 1630 if (ap->udmamodes & 0x01) 1631 return 0; 1632 } 1633 return -1; 1634} 1635 1636static char * 1637active2str(int active) 1638{ 1639 static char buf[8]; 1640 1641 switch (active) { 1642 case ATA_IDLE: 1643 return("ATA_IDLE"); 1644 case ATA_IMMEDIATE: 1645 return("ATA_IMMEDIATE"); 1646 case ATA_WAIT_INTR: 1647 return("ATA_WAIT_INTR"); 1648 case ATA_WAIT_READY: 1649 return("ATA_WAIT_READY"); 1650 case ATA_ACTIVE: 1651 return("ATA_ACTIVE"); 1652 case ATA_ACTIVE_ATA: 1653 return("ATA_ACTIVE_ATA"); 1654 case ATA_ACTIVE_ATAPI: 1655 return("ATA_ACTIVE_ATAPI"); 1656 case ATA_REINITING: 1657 return("ATA_REINITING"); 1658 default: 1659 sprintf(buf, "0x%02x", active); 1660 return buf; 1661 } 1662} 1663 1664static void 1665bswap(int8_t *buf, int len) 1666{ 1667 u_int16_t *ptr = (u_int16_t*)(buf + len); 1668 1669 while (--ptr >= (u_int16_t*)buf) 1670 *ptr = ntohs(*ptr); 1671} 1672 1673static void 1674btrim(int8_t *buf, int len) 1675{ 1676 int8_t *ptr; 1677 1678 for (ptr = buf; ptr < buf+len; ++ptr) 1679 if (!*ptr) 1680 *ptr = ' '; 1681 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1682 *ptr = 0; 1683} 1684 1685static void 1686bpack(int8_t *src, int8_t *dst, int len) 1687{ 1688 int i, j, blank; 1689 1690 for (i = j = blank = 0 ; i < len; i++) { 1691 if (blank && src[i] == ' ') continue; 1692 if (blank && src[i] != ' ') { 1693 dst[j++] = src[i]; 1694 blank = 0; 1695 continue; 1696 } 1697 if (src[i] == ' ') { 1698 blank = 1; 1699 if (i == 0) 1700 continue; 1701 } 1702 dst[j++] = src[i]; 1703 } 1704 if (j < len) 1705 dst[j] = 0x00; 1706} 1707 1708static void 1709ata_change_mode(struct ata_softc *scp, int device, int mode) 1710{ 1711 int s = splbio(); 1712 1713 while (scp->active != ATA_IDLE) 1714 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 1715 scp->active = ATA_REINITING; 1716 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)), 1717 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)), 1718 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device))); 1719 scp->active = ATA_IDLE; 1720 ata_start(scp); 1721 splx(s); 1722} 1723 1724static int 1725sysctl_hw_ata(SYSCTL_HANDLER_ARGS) 1726{ 1727 struct ata_softc *scp; 1728 int ctlr, error, i; 1729 1730 /* readout internal state */ 1731 bzero(ata_conf, sizeof(ata_conf)); 1732 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1733 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1734 continue; 1735 for (i = 0; i < 2; i++) { 1736 if (!scp->dev_softc[i]) 1737 strcat(ata_conf, "---,"); 1738 else if (scp->mode[i] >= ATA_DMA) 1739 strcat(ata_conf, "dma,"); 1740 else 1741 strcat(ata_conf, "pio,"); 1742 } 1743 } 1744 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req); 1745 if (error == 0 && req->newptr != NULL) { 1746 char *ptr = ata_conf; 1747 1748 /* update internal state */ 1749 i = 0; 1750 while (*ptr) { 1751 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) { 1752 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1753 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA) 1754 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO); 1755 } 1756 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) { 1757 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1758 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA) 1759 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA); 1760 } 1761 else if (strncmp(ptr, "---", 3)) 1762 break; 1763 ptr+=3; 1764 if (*ptr++ != ',' || 1765 ++i > (devclass_get_maxunit(ata_devclass) << 1)) 1766 break; 1767 } 1768 } 1769 return error; 1770} 1771SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW, 1772 0, sizeof(ata_conf), sysctl_hw_ata, "A", ""); 1773 1774static void 1775ata_init(void) 1776{ 1777 /* register boot attach to be run when interrupts are enabled */ 1778 if (!(ata_delayed_attach = (struct intr_config_hook *) 1779 malloc(sizeof(struct intr_config_hook), 1780 M_TEMP, M_NOWAIT))) { 1781 printf("ata: malloc of delayed attach hook failed\n"); 1782 return; 1783 } 1784 bzero(ata_delayed_attach, sizeof(struct intr_config_hook)); 1785 1786 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1787 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1788 printf("ata: config_intrhook_establish failed\n"); 1789 free(ata_delayed_attach, M_TEMP); 1790 } 1791} 1792SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
|