ata-all.c revision 74253
1/*- 2 * Copyright (c) 1998,1999,2000,2001 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 74253 2001-03-14 14:00:09Z sos $ 29 */ 30 31#include "pci.h" 32#include "opt_ata.h" 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/disk.h> 37#include <sys/module.h> 38#include <sys/bus.h> 39#include <sys/bio.h> 40#include <sys/malloc.h> 41#include <sys/devicestat.h> 42#include <sys/sysctl.h> 43#include <machine/stdarg.h> 44#include <machine/resource.h> 45#include <machine/bus.h> 46#include <sys/rman.h> 47#ifdef __alpha__ 48#include <machine/md_var.h> 49#endif 50#include <dev/ata/ata-all.h> 51#include <dev/ata/ata-disk.h> 52#include <dev/ata/atapi-all.h> 53 54/* prototypes */ 55static void ata_boot_attach(void); 56static void ata_intr(void *); 57static int ata_getparam(struct ata_softc *, int, u_int8_t); 58static int ata_service(struct ata_softc *); 59static char *active2str(int); 60static void bswap(int8_t *, int); 61static void btrim(int8_t *, int); 62static void bpack(int8_t *, int8_t *, int); 63 64/* global vars */ 65devclass_t ata_devclass; 66 67/* local vars */ 68static struct intr_config_hook *ata_delayed_attach = NULL; 69static char ata_conf[256]; 70static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 71 72/* misc defines */ 73#define MASTER 0 74#define SLAVE 1 75 76int 77ata_probe(device_t dev) 78{ 79 struct ata_softc *scp; 80 int rid; 81 82 if (!dev) 83 return ENXIO; 84 scp = device_get_softc(dev); 85 if (!scp || scp->devices) 86 return ENXIO; 87 88 /* initialize the softc basics */ 89 scp->active = ATA_IDLE; 90 scp->dev = dev; 91 92 rid = ATA_IOADDR_RID; 93 scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 94 ATA_IOSIZE, RF_ACTIVE); 95 if (!scp->r_io) 96 goto failure; 97 98 rid = ATA_ALTADDR_RID; 99 scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 100 ATA_ALTIOSIZE, RF_ACTIVE); 101 if (!scp->r_altio) 102 goto failure; 103 104 rid = ATA_BMADDR_RID; 105 scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 106 ATA_BMIOSIZE, RF_ACTIVE); 107 108 if (bootverbose) 109 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 110 (int)rman_get_start(scp->r_io), 111 (int)rman_get_start(scp->r_altio), 112 (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0); 113 114 ata_reset(scp); 115 116 TAILQ_INIT(&scp->ata_queue); 117 TAILQ_INIT(&scp->atapi_queue); 118 return 0; 119 120failure: 121 if (scp->r_io) 122 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io); 123 if (scp->r_altio) 124 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio); 125 if (scp->r_bmio) 126 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio); 127 if (bootverbose) 128 ata_printf(scp, -1, "probe allocation failed\n"); 129 return ENXIO; 130} 131 132int 133ata_attach(device_t dev) 134{ 135 struct ata_softc *scp; 136 int error, rid; 137 138 if (!dev) 139 return ENXIO; 140 scp = device_get_softc(dev); 141 if (!scp) 142 return ENXIO; 143 144 rid = ATA_IRQ_RID; 145 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 146 RF_SHAREABLE | RF_ACTIVE); 147 if (!scp->r_irq) { 148 ata_printf(scp, -1, "unable to allocate interrupt\n"); 149 return ENXIO; 150 } 151 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO|INTR_ENTROPY, 152 ata_intr, scp, &scp->ih))) 153 return error; 154 155 /* 156 * do not attach devices if we are in early boot, this is done later 157 * when interrupts are enabled by a hook into the boot process. 158 * otherwise attach what the probe has found in scp->devices. 159 */ 160 if (!ata_delayed_attach) { 161 if (scp->devices & ATA_ATA_SLAVE) 162 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 163 scp->devices &= ~ATA_ATA_SLAVE; 164 if (scp->devices & ATA_ATAPI_SLAVE) 165 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 166 scp->devices &= ~ATA_ATAPI_SLAVE; 167 if (scp->devices & ATA_ATA_MASTER) 168 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 169 scp->devices &= ~ATA_ATA_MASTER; 170 if (scp->devices & ATA_ATAPI_MASTER) 171 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY)) 172 scp->devices &= ~ATA_ATAPI_MASTER; 173#ifdef DEV_ATADISK 174 if (scp->devices & ATA_ATA_MASTER) 175 ad_attach(scp, ATA_MASTER); 176 if (scp->devices & ATA_ATA_SLAVE) 177 ad_attach(scp, ATA_SLAVE); 178#endif 179#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 180 if (scp->devices & ATA_ATAPI_MASTER) 181 atapi_attach(scp, ATA_MASTER); 182 if (scp->devices & ATA_ATAPI_SLAVE) 183 atapi_attach(scp, ATA_SLAVE); 184#endif 185 } 186 return 0; 187} 188 189int 190ata_detach(device_t dev) 191{ 192 struct ata_softc *scp; 193 int s; 194 195 if (!dev) 196 return ENXIO; 197 scp = device_get_softc(dev); 198 if (!scp || !scp->devices) 199 return ENXIO; 200 201 /* make sure channel is not busy SOS XXX */ 202 s = splbio(); 203 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE)) 204 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 205 splx(s); 206 207 /* disable interrupts on devices */ 208 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 209 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 210 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 211 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 212 213#ifdef DEV_ATADISK 214 if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER]) 215 ad_detach(scp->dev_softc[MASTER], 1); 216 if (scp->devices & ATA_ATA_SLAVE && scp->dev_softc[SLAVE]) 217 ad_detach(scp->dev_softc[SLAVE], 1); 218#endif 219#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 220 if (scp->devices & ATA_ATAPI_MASTER && scp->dev_softc[MASTER]) 221 atapi_detach(scp->dev_softc[MASTER]); 222 if (scp->devices & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE]) 223 atapi_detach(scp->dev_softc[SLAVE]); 224#endif 225 226 if (scp->dev_param[MASTER]) { 227 free(scp->dev_param[MASTER], M_ATA); 228 scp->dev_param[MASTER] = NULL; 229 } 230 if (scp->dev_param[SLAVE]) { 231 free(scp->dev_param[SLAVE], M_ATA); 232 scp->dev_param[SLAVE] = NULL; 233 } 234 scp->dev_softc[MASTER] = NULL; 235 scp->dev_softc[SLAVE] = NULL; 236 scp->mode[MASTER] = ATA_PIO; 237 scp->mode[SLAVE] = ATA_PIO; 238 scp->devices = 0; 239 240 bus_teardown_intr(dev, scp->r_irq, scp->ih); 241 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq); 242 if (scp->r_bmio) 243 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio); 244 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio); 245 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io); 246 scp->active = ATA_IDLE; 247 return 0; 248} 249 250int 251ata_resume(device_t dev) 252{ 253 struct ata_softc *scp = device_get_softc(dev); 254 255 ata_reinit(scp); 256 return 0; 257} 258 259static int 260ata_getparam(struct ata_softc *scp, int device, u_int8_t command) 261{ 262 struct ata_params *ata_parm; 263 int8_t buffer[DEV_BSIZE]; 264 int retry = 0; 265 266 /* select drive */ 267 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 268 DELAY(1); 269 270 /* enable interrupt */ 271 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 272 DELAY(1); 273 274 /* apparently some devices needs this repeated */ 275 do { 276 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) { 277 ata_printf(scp, device, "identify failed\n"); 278 return -1; 279 } 280 if (retry++ > 4) { 281 ata_printf(scp, device, "identify retries exceeded\n"); 282 return -1; 283 } 284 } while (ata_wait(scp, device, 285 ((command == ATA_C_ATAPI_IDENTIFY) ? 286 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)))); 287 288 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer, 289 sizeof(buffer)/sizeof(int16_t)); 290 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 291 if (!ata_parm) { 292 ata_printf(scp, device, "malloc for identify data failed\n"); 293 return -1; 294 } 295 bcopy(buffer, ata_parm, sizeof(struct ata_params)); 296 if (command == ATA_C_ATA_IDENTIFY || 297 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 298 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X'))) 299 bswap(ata_parm->model, sizeof(ata_parm->model)); 300 btrim(ata_parm->model, sizeof(ata_parm->model)); 301 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 302 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 303 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 304 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 305 scp->dev_param[ATA_DEV(device)] = ata_parm; 306 return 0; 307} 308 309static void 310ata_boot_attach(void) 311{ 312 struct ata_softc *scp; 313 int ctlr; 314 315 /* 316 * run through all ata devices and look for real ATA & ATAPI devices 317 * using the hints we found in the early probe, this avoids some of 318 * the delays probing of non-exsistent devices can cause. 319 */ 320 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 321 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 322 continue; 323 if (scp->devices & ATA_ATA_SLAVE) 324 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 325 scp->devices &= ~ATA_ATA_SLAVE; 326 if (scp->devices & ATA_ATAPI_SLAVE) 327 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 328 scp->devices &= ~ATA_ATAPI_SLAVE; 329 if (scp->devices & ATA_ATA_MASTER) 330 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 331 scp->devices &= ~ATA_ATA_MASTER; 332 if (scp->devices & ATA_ATAPI_MASTER) 333 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY)) 334 scp->devices &= ~ATA_ATAPI_MASTER; 335 } 336 337#ifdef DEV_ATADISK 338 /* now we know whats there, do the real attach, first the ATA disks */ 339 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 340 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 341 continue; 342 if (scp->devices & ATA_ATA_MASTER) 343 ad_attach(scp, ATA_MASTER); 344 if (scp->devices & ATA_ATA_SLAVE) 345 ad_attach(scp, ATA_SLAVE); 346 } 347#endif 348#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 349 /* then the atapi devices */ 350 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 351 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 352 continue; 353 if (scp->devices & ATA_ATAPI_MASTER) 354 atapi_attach(scp, ATA_MASTER); 355 if (scp->devices & ATA_ATAPI_SLAVE) 356 atapi_attach(scp, ATA_SLAVE); 357 } 358#endif 359 if (ata_delayed_attach) { 360 config_intrhook_disestablish(ata_delayed_attach); 361 free(ata_delayed_attach, M_ATA); 362 ata_delayed_attach = NULL; 363 } 364} 365 366static void 367ata_intr(void *data) 368{ 369 struct ata_softc *scp = (struct ata_softc *)data; 370 371 /* 372 * on PCI systems we might share an interrupt line with another 373 * device or our twin ATA channel, so call scp->intr_func to figure 374 * out if it is really an interrupt we should process here 375 */ 376 if (scp->intr_func && scp->intr_func(scp)) 377 return; 378 379 /* if drive is busy it didn't interrupt */ 380 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) 381 return; 382 383 /* clear interrupt and get status */ 384 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 385 386 if (scp->status & ATA_S_ERROR) 387 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 388 389 /* find & call the responsible driver to process this interrupt */ 390 switch (scp->active) { 391#ifdef DEV_ATADISK 392 case ATA_ACTIVE_ATA: 393 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES) 394 return; 395 break; 396#endif 397#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 398 case ATA_ACTIVE_ATAPI: 399 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 400 return; 401 break; 402#endif 403 case ATA_WAIT_INTR: 404 case ATA_WAIT_INTR | ATA_REINITING: 405 wakeup((caddr_t)scp); 406 break; 407 408 case ATA_WAIT_READY: 409 case ATA_WAIT_READY | ATA_REINITING: 410 break; 411 412 case ATA_IDLE: 413 if (scp->flags & ATA_QUEUED) { 414 scp->active = ATA_ACTIVE; /* XXX */ 415 if (ata_service(scp) == ATA_OP_CONTINUES) 416 return; 417 } 418 /* FALLTHROUGH */ 419 420 default: 421#ifdef ATA_DEBUG 422 { 423 static int intr_count = 0; 424 425 if (intr_count++ < 10) 426 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n", 427 intr_count, scp->status); 428 } 429#endif 430 } 431 scp->active &= ATA_REINITING; 432 if (scp->active & ATA_REINITING) 433 return; 434 scp->running = NULL; 435 ata_start(scp); 436 return; 437} 438 439void 440ata_start(struct ata_softc *scp) 441{ 442#ifdef DEV_ATADISK 443 struct ad_request *ad_request; 444#endif 445#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 446 struct atapi_request *atapi_request; 447#endif 448 449 if (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE)) 450 return; 451 452#ifdef DEV_ATADISK 453 /* find & call the responsible driver if anything on the ATA queue */ 454 if (TAILQ_EMPTY(&scp->ata_queue)) { 455 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[MASTER]) 456 ad_start((struct ad_softc *)scp->dev_softc[MASTER]); 457 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 458 ad_start((struct ad_softc *)scp->dev_softc[SLAVE]); 459 } 460 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 461 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 462 scp->active = ATA_ACTIVE_ATA; 463 scp->running = ad_request; 464 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) 465 return; 466 } 467 468#endif 469#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 470 /* find & call the responsible driver if anything on the ATAPI queue */ 471 if (TAILQ_EMPTY(&scp->atapi_queue)) { 472 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER]) 473 atapi_start((struct atapi_softc *)scp->dev_softc[MASTER]); 474 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE]) 475 atapi_start((struct atapi_softc *)scp->dev_softc[SLAVE]); 476 } 477 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 478 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 479 scp->active = ATA_ACTIVE_ATAPI; 480 scp->running = atapi_request; 481 atapi_transfer(atapi_request); 482 return; 483 } 484#endif 485 scp->active = ATA_IDLE; 486} 487 488void 489ata_reset(struct ata_softc *scp) 490{ 491 u_int8_t lsb, msb, ostat0, ostat1; 492 u_int8_t stat0 = ATA_S_BUSY, stat1 = ATA_S_BUSY; 493 int mask = 0, timeout; 494 495 /* do we have any signs of ATA/ATAPI HW being present ? */ 496 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 497 DELAY(10); 498 ostat0 = ATA_INB(scp->r_io, ATA_STATUS); 499 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) 500 mask |= 0x01; 501 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 502 DELAY(10); 503 ostat1 = ATA_INB(scp->r_io, ATA_STATUS); 504 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) 505 mask |= 0x02; 506 507 scp->devices = 0; 508 if (!mask) 509 return; 510 511 /* in some setups we dont want to test for a slave */ 512 if (scp->flags & ATA_NO_SLAVE) 513 mask &= ~0x02; 514 515 if (bootverbose) 516 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n", 517 mask, ostat0, ostat1); 518 519 /* reset channel */ 520 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 521 DELAY(10000); 522 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS); 523 DELAY(100000); 524 ATA_INB(scp->r_io, ATA_ERROR); 525 526 /* wait for BUSY to go inactive */ 527 for (timeout = 0; timeout < 310000; timeout++) { 528 if (stat0 & ATA_S_BUSY) { 529 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 530 DELAY(10); 531 stat0 = ATA_INB(scp->r_io, ATA_STATUS); 532 if (!(stat0 & ATA_S_BUSY)) { 533 /* check for ATAPI signature while its still there */ 534 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB); 535 msb = ATA_INB(scp->r_io, ATA_CYL_MSB); 536 if (bootverbose) 537 ata_printf(scp, ATA_MASTER, 538 "ATAPI probe %02x %02x\n", lsb, msb); 539 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 540 scp->devices |= ATA_ATAPI_MASTER; 541 } 542 } 543 if (stat1 & ATA_S_BUSY) { 544 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 545 DELAY(10); 546 stat1 = ATA_INB(scp->r_io, ATA_STATUS); 547 if (!(stat1 & ATA_S_BUSY)) { 548 /* check for ATAPI signature while its still there */ 549 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB); 550 msb = ATA_INB(scp->r_io, ATA_CYL_MSB); 551 if (bootverbose) 552 ata_printf(scp, ATA_SLAVE, 553 "ATAPI probe %02x %02x\n", lsb, msb); 554 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 555 scp->devices |= ATA_ATAPI_SLAVE; 556 } 557 } 558 if (mask == 0x01) /* wait for master only */ 559 if (!(stat0 & ATA_S_BUSY)) 560 break; 561 if (mask == 0x02) /* wait for slave only */ 562 if (!(stat1 & ATA_S_BUSY)) 563 break; 564 if (mask == 0x03) /* wait for both master & slave */ 565 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 566 break; 567 DELAY(100); 568 } 569 DELAY(10); 570 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 571 572 if (stat0 & ATA_S_BUSY) 573 mask &= ~0x01; 574 if (stat1 & ATA_S_BUSY) 575 mask &= ~0x02; 576 if (bootverbose) 577 ata_printf(scp, -1, "mask=%02x stat0=%02x stat1=%02x\n", 578 mask, stat0, stat1); 579 if (!mask) 580 return; 581 582 if (mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 583 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 584 DELAY(10); 585 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58); 586 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5); 587 lsb = ATA_INB(scp->r_io, ATA_ERROR); 588 msb = ATA_INB(scp->r_io, ATA_CYL_LSB); 589 if (bootverbose) 590 ata_printf(scp, ATA_MASTER, "ATA probe %02x %02x\n", lsb, msb); 591 if (lsb != 0x58 && msb == 0xa5) 592 scp->devices |= ATA_ATA_MASTER; 593 } 594 if (mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 595 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 596 DELAY(10); 597 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58); 598 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5); 599 lsb = ATA_INB(scp->r_io, ATA_ERROR); 600 msb = ATA_INB(scp->r_io, ATA_CYL_LSB); 601 if (bootverbose) 602 ata_printf(scp, ATA_SLAVE, "ATA probe %02x %02x\n", lsb, msb); 603 if (lsb != 0x58 && msb == 0xa5) 604 scp->devices |= ATA_ATA_SLAVE; 605 } 606 if (bootverbose) 607 ata_printf(scp, -1, "devices=%02x\n", scp->devices); 608} 609 610int 611ata_reinit(struct ata_softc *scp) 612{ 613 int devices, misdev, newdev; 614 615 scp->active = ATA_REINITING; 616 scp->running = NULL; 617 devices = scp->devices; 618 ata_printf(scp, -1, "resetting devices .. "); 619 ata_reset(scp); 620 621 if ((misdev = devices & ~scp->devices)) { 622#ifdef DEV_ATADISK 623 if (misdev & ATA_ATA_MASTER && scp->dev_softc[MASTER]) 624 ad_detach(scp->dev_softc[MASTER], 0); 625 if (misdev & ATA_ATA_SLAVE && scp->dev_softc[SLAVE]) 626 ad_detach(scp->dev_softc[SLAVE], 0); 627#endif 628#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 629 if (misdev & ATA_ATAPI_MASTER && scp->dev_softc[MASTER]) 630 atapi_detach(scp->dev_softc[MASTER]); 631 if (misdev & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE]) 632 atapi_detach(scp->dev_softc[SLAVE]); 633#endif 634 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 635 free(scp->dev_param[MASTER], M_ATA); 636 scp->dev_param[MASTER] = NULL; 637 } 638 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 639 free(scp->dev_param[SLAVE], M_ATA); 640 scp->dev_param[SLAVE] = NULL; 641 } 642 } 643 if ((newdev = ~devices & scp->devices)) { 644 if (newdev & ATA_ATA_MASTER) 645 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 646 newdev &= ~ATA_ATA_MASTER; 647 if (newdev & ATA_ATA_SLAVE) 648 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 649 newdev &= ~ATA_ATA_SLAVE; 650 if (newdev & ATA_ATAPI_MASTER) 651 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY)) 652 newdev &= ~ATA_ATAPI_MASTER; 653 if (newdev & ATA_ATAPI_SLAVE) 654 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 655 newdev &= ~ATA_ATAPI_SLAVE; 656 if (newdev) 657 printf("\n"); 658 } 659 scp->active = ATA_IDLE; 660#ifdef DEV_ATADISK 661 if (newdev & ATA_ATA_MASTER && !scp->dev_softc[MASTER]) 662 ad_attach(scp, ATA_MASTER); 663 else if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER]) 664 ad_reinit((struct ad_softc *)scp->dev_softc[MASTER]); 665 if (newdev & ATA_ATA_SLAVE && !scp->dev_softc[SLAVE]) 666 ad_attach(scp, ATA_SLAVE); 667 else if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 668 ad_reinit((struct ad_softc *)scp->dev_softc[SLAVE]); 669#endif 670#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 671 if (newdev & ATA_ATAPI_MASTER && !scp->dev_softc[MASTER]) 672 atapi_attach(scp, ATA_MASTER); 673 else if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER]) 674 atapi_reinit((struct atapi_softc *)scp->dev_softc[MASTER]); 675 if (newdev & ATA_ATAPI_SLAVE && !scp->dev_softc[SLAVE]) 676 atapi_attach(scp, ATA_SLAVE); 677 else if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE]) 678 atapi_reinit((struct atapi_softc *)scp->dev_softc[SLAVE]); 679#endif 680 printf("done\n"); 681 ata_start(scp); 682 return 0; 683} 684 685static int 686ata_service(struct ata_softc *scp) 687{ 688 /* do we have a SERVICE request from the drive ? */ 689 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){ 690 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, 691 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT); 692#ifdef DEV_ATADISK 693 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 694 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[MASTER]) 695 return ad_service((struct ad_softc *)scp->dev_softc[MASTER], 0); 696 } 697 else { 698 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 699 return ad_service((struct ad_softc *)scp->dev_softc[SLAVE], 0); 700 } 701#endif 702 } 703 return ATA_OP_FINISHED; 704} 705 706int 707ata_wait(struct ata_softc *scp, int device, u_int8_t mask) 708{ 709 int timeout = 0; 710 711 DELAY(1); 712 while (timeout < 5000000) { /* timeout 5 secs */ 713 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 714 715 /* if drive fails status, reselect the drive just to be sure */ 716 if (scp->status == 0xff) { 717 ata_printf(scp, device, "no status, reselecting device\n"); 718 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 719 DELAY(10); 720 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 721 if (scp->status == 0xff) 722 return -1; 723 } 724 725 /* are we done ? */ 726 if (!(scp->status & ATA_S_BUSY)) 727 break; 728 729 if (timeout > 1000) { 730 timeout += 1000; 731 DELAY(1000); 732 } 733 else { 734 timeout += 10; 735 DELAY(10); 736 } 737 } 738 if (scp->status & ATA_S_ERROR) 739 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 740 if (timeout >= 5000000) 741 return -1; 742 if (!mask) 743 return (scp->status & ATA_S_ERROR); 744 745 /* Wait 50 msec for bits wanted. */ 746 timeout = 5000; 747 while (timeout--) { 748 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 749 if ((scp->status & mask) == mask) { 750 if (scp->status & ATA_S_ERROR) 751 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 752 return (scp->status & ATA_S_ERROR); 753 } 754 DELAY (10); 755 } 756 return -1; 757} 758 759int 760ata_command(struct ata_softc *scp, int device, u_int8_t command, 761 u_int16_t cylinder, u_int8_t head, u_int8_t sector, 762 u_int8_t count, u_int8_t feature, int flags) 763{ 764 int error = 0; 765#ifdef ATA_DEBUG 766 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, " 767 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n", 768 rman_get_start(scp->r_io), command, cylinder, head, sector, 769 count, feature, flags); 770#endif 771 772 /* disable interrupt from device */ 773 if (scp->flags & ATA_QUEUED) 774 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 775 776 /* select device */ 777 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 778 779 /* ready to issue command ? */ 780 if (ata_wait(scp, device, 0) < 0) { 781 ata_printf(scp, device, 782 "timeout waiting to give command=%02x s=%02x e=%02x\n", 783 command, scp->status, scp->error); 784 return -1; 785 } 786 787 ATA_OUTB(scp->r_io, ATA_FEATURE, feature); 788 ATA_OUTB(scp->r_io, ATA_COUNT, count); 789 ATA_OUTB(scp->r_io, ATA_SECTOR, sector); 790 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8); 791 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder); 792 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head); 793 794 switch (flags) { 795 case ATA_WAIT_INTR: 796 scp->active |= ATA_WAIT_INTR; 797 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz); 798 ATA_OUTB(scp->r_io, ATA_CMD, command); 799 800 /* enable interrupt */ 801 if (scp->flags & ATA_QUEUED) 802 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 803 804 if (await(PRIBIO, 10 * hz)) { 805 ata_printf(scp, device, "ata_command: timeout waiting for intr\n"); 806 scp->active &= ~ATA_WAIT_INTR; 807 error = -1; 808 } 809 break; 810 811 case ATA_WAIT_READY: 812 scp->active |= ATA_WAIT_READY; 813 ATA_OUTB(scp->r_io, ATA_CMD, command); 814 if (ata_wait(scp, device, ATA_S_READY) < 0) { 815 ata_printf(scp, device, 816 "timeout waiting for command=%02x s=%02x e=%02x\n", 817 command, scp->status, scp->error); 818 error = -1; 819 } 820 scp->active &= ~ATA_WAIT_READY; 821 break; 822 823 case ATA_IMMEDIATE: 824 ATA_OUTB(scp->r_io, ATA_CMD, command); 825 break; 826 827 default: 828 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n", 829 active2str(flags)); 830 } 831 /* enable interrupt */ 832 if (scp->flags & ATA_QUEUED) 833 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 834 return error; 835} 836 837int 838ata_get_lun(u_int32_t *map) 839{ 840 int lun = ffs(~*map) - 1; 841 842 *map |= (1 << lun); 843 return lun; 844} 845 846int 847ata_test_lun(u_int32_t *map, int lun) 848{ 849 return (*map & (1 << lun)); 850} 851 852void 853ata_free_lun(u_int32_t *map, int lun) 854{ 855 *map &= ~(1 << lun); 856} 857 858int 859ata_printf(struct ata_softc *scp, int device, const char * fmt, ...) 860{ 861 va_list ap; 862 int ret; 863 864 if (device == -1) 865 ret = printf("ata%d: ", device_get_unit(scp->dev)); 866 else 867 ret = printf("ata%d-%s: ", device_get_unit(scp->dev), 868 (device == ATA_MASTER) ? "master" : "slave"); 869 va_start(ap, fmt); 870 ret += vprintf(fmt, ap); 871 va_end(ap); 872 return ret; 873} 874 875char * 876ata_mode2str(int mode) 877{ 878 switch (mode) { 879 case ATA_PIO: return "BIOSPIO"; 880 case ATA_PIO0: return "PIO0"; 881 case ATA_PIO1: return "PIO1"; 882 case ATA_PIO2: return "PIO2"; 883 case ATA_PIO3: return "PIO3"; 884 case ATA_PIO4: return "PIO4"; 885 case ATA_WDMA2: return "WDMA2"; 886 case ATA_UDMA2: return "UDMA33"; 887 case ATA_UDMA4: return "UDMA66"; 888 case ATA_UDMA5: return "UDMA100"; 889 case ATA_DMA: return "BIOSDMA"; 890 default: return "???"; 891 } 892} 893 894int 895ata_pio2mode(int pio) 896{ 897 switch (pio) { 898 default: 899 case 0: return ATA_PIO0; 900 case 1: return ATA_PIO1; 901 case 2: return ATA_PIO2; 902 case 3: return ATA_PIO3; 903 case 4: return ATA_PIO4; 904 } 905} 906 907int 908ata_pmode(struct ata_params *ap) 909{ 910 if (ap->atavalid & ATA_FLAG_64_70) { 911 if (ap->apiomodes & 2) 912 return 4; 913 if (ap->apiomodes & 1) 914 return 3; 915 } 916 if (ap->opiomode == 2) 917 return 2; 918 if (ap->opiomode == 1) 919 return 1; 920 if (ap->opiomode == 0) 921 return 0; 922 return -1; 923} 924 925int 926ata_wmode(struct ata_params *ap) 927{ 928 if (ap->wdmamodes & 4) 929 return 2; 930 if (ap->wdmamodes & 2) 931 return 1; 932 if (ap->wdmamodes & 1) 933 return 0; 934 return -1; 935} 936 937int 938ata_umode(struct ata_params *ap) 939{ 940 if (ap->atavalid & ATA_FLAG_88) { 941 if (ap->udmamodes & 0x20) 942 return 5; 943 if (ap->udmamodes & 0x10) 944 return 4; 945 if (ap->udmamodes & 0x08) 946 return 3; 947 if (ap->udmamodes & 0x04) 948 return 2; 949 if (ap->udmamodes & 0x02) 950 return 1; 951 if (ap->udmamodes & 0x01) 952 return 0; 953 } 954 return -1; 955} 956 957static char * 958active2str(int active) 959{ 960 static char buf[8]; 961 962 switch (active) { 963 case ATA_IDLE: 964 return("ATA_IDLE"); 965 case ATA_IMMEDIATE: 966 return("ATA_IMMEDIATE"); 967 case ATA_WAIT_INTR: 968 return("ATA_WAIT_INTR"); 969 case ATA_WAIT_READY: 970 return("ATA_WAIT_READY"); 971 case ATA_ACTIVE: 972 return("ATA_ACTIVE"); 973 case ATA_ACTIVE_ATA: 974 return("ATA_ACTIVE_ATA"); 975 case ATA_ACTIVE_ATAPI: 976 return("ATA_ACTIVE_ATAPI"); 977 case ATA_REINITING: 978 return("ATA_REINITING"); 979 default: 980 sprintf(buf, "0x%02x", active); 981 return buf; 982 } 983} 984 985static void 986bswap(int8_t *buf, int len) 987{ 988 u_int16_t *ptr = (u_int16_t*)(buf + len); 989 990 while (--ptr >= (u_int16_t*)buf) 991 *ptr = ntohs(*ptr); 992} 993 994static void 995btrim(int8_t *buf, int len) 996{ 997 int8_t *ptr; 998 999 for (ptr = buf; ptr < buf+len; ++ptr) 1000 if (!*ptr) 1001 *ptr = ' '; 1002 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1003 *ptr = 0; 1004} 1005 1006static void 1007bpack(int8_t *src, int8_t *dst, int len) 1008{ 1009 int i, j, blank; 1010 1011 for (i = j = blank = 0 ; i < len; i++) { 1012 if (blank && src[i] == ' ') continue; 1013 if (blank && src[i] != ' ') { 1014 dst[j++] = src[i]; 1015 blank = 0; 1016 continue; 1017 } 1018 if (src[i] == ' ') { 1019 blank = 1; 1020 if (i == 0) 1021 continue; 1022 } 1023 dst[j++] = src[i]; 1024 } 1025 if (j < len) 1026 dst[j] = 0x00; 1027} 1028 1029static void 1030ata_change_mode(struct ata_softc *scp, int device, int mode) 1031{ 1032 int s = splbio(); 1033 1034 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE)) 1035 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 1036 1037 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)), 1038 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)), 1039 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device))); 1040 scp->active = ATA_IDLE; 1041 ata_start(scp); 1042 splx(s); 1043} 1044 1045static int 1046sysctl_hw_ata(SYSCTL_HANDLER_ARGS) 1047{ 1048 struct ata_softc *scp; 1049 int ctlr, error, i; 1050 1051 /* readout internal state */ 1052 bzero(ata_conf, sizeof(ata_conf)); 1053 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 1054 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 1055 continue; 1056 for (i = 0; i < 2; i++) { 1057 if (!scp->dev_softc[i]) 1058 strcat(ata_conf, "---,"); 1059 else if (scp->mode[i] >= ATA_DMA) 1060 strcat(ata_conf, "dma,"); 1061 else 1062 strcat(ata_conf, "pio,"); 1063 } 1064 } 1065 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req); 1066 if (error == 0 && req->newptr != NULL) { 1067 char *ptr = ata_conf; 1068 1069 /* update internal state */ 1070 i = 0; 1071 while (*ptr) { 1072 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) { 1073 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1074 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA) 1075 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO); 1076 } 1077 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) { 1078 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) && 1079 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA) 1080 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA); 1081 } 1082 else if (strncmp(ptr, "---", 3)) 1083 break; 1084 ptr+=3; 1085 if (*ptr++ != ',' || 1086 ++i > (devclass_get_maxunit(ata_devclass) << 1)) 1087 break; 1088 } 1089 } 1090 return error; 1091} 1092SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW, 1093 0, sizeof(ata_conf), sysctl_hw_ata, "A", ""); 1094 1095static void 1096ata_init(void) 1097{ 1098 /* register boot attach to be run when interrupts are enabled */ 1099 if (!(ata_delayed_attach = (struct intr_config_hook *) 1100 malloc(sizeof(struct intr_config_hook), 1101 M_TEMP, M_NOWAIT | M_ZERO))) { 1102 printf("ata: malloc of delayed attach hook failed\n"); 1103 return; 1104 } 1105 1106 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1107 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1108 printf("ata: config_intrhook_establish failed\n"); 1109 free(ata_delayed_attach, M_TEMP); 1110 } 1111} 1112SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1113