1 /*- 2 * Copyright (c) 1998,1999,2000,2001,2002 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include "opt_ata.h" 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/ata.h> 35 #include <sys/kernel.h> 36 #include <sys/conf.h> 37 #include <sys/disk.h> 38 #include <sys/module.h> 39 #include <sys/bus.h> 40 #include <sys/bio.h> 41 #include <sys/malloc.h> 42 #include <sys/devicestat.h> 43 #include <sys/sysctl.h> 44 #include <machine/stdarg.h> 45 #include <machine/resource.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #ifdef __alpha__ 49 #include <machine/md_var.h> 50 #endif 51 #include <dev/ata/ata-all.h> 52 #include <dev/ata/ata-disk.h> 53 #include <dev/ata/ata-raid.h> 54 #include <dev/ata/atapi-all.h> 55 56 /* device structures */ 57 static d_ioctl_t ataioctl; 58 static struct cdevsw ata_cdevsw = { 59 /* open */ nullopen, 60 /* close */ nullclose, 61 /* read */ noread, 62 /* write */ nowrite, 63 /* ioctl */ ataioctl, 64 /* poll */ nopoll, 65 /* mmap */ nommap, 66 /* strategy */ nostrategy, 67 /* name */ "ata", 68 /* maj */ 159, 69 /* dump */ nodump, 70 /* psize */ nopsize, 71 /* flags */ 0, 72 }; 73 74 /* prototypes */ 75 static void ata_boot_attach(void); 76 static void ata_intr(void *); 77 static int ata_getparam(struct ata_device *, u_int8_t); 78 static int ata_service(struct ata_channel *); 79 static void bswap(int8_t *, int); 80 static void btrim(int8_t *, int); 81 static void bpack(int8_t *, int8_t *, int); 82 static void ata_change_mode(struct ata_device *, int); 83 84 /* sysctl vars */ 85 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 86 87 /* global vars */ 88 devclass_t ata_devclass; 89 90 /* local vars */ 91 static struct intr_config_hook *ata_delayed_attach = NULL; 92 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 93 94 int 95 ata_probe(device_t dev) 96 { 97 struct ata_channel *ch; 98 int rid; 99 100 if (!dev || !(ch = device_get_softc(dev))) 101 return ENXIO; 102 103 if (ch->r_io || ch->r_altio || ch->r_irq) 104 return EEXIST; 105 106 /* initialize the softc basics */ 107 ch->active = ATA_IDLE; 108 ch->dev = dev; 109 110 rid = ATA_IOADDR_RID; 111 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 112 ATA_IOSIZE, RF_ACTIVE); 113 if (!ch->r_io) 114 goto failure; 115 116 rid = ATA_ALTADDR_RID; 117 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 118 ATA_ALTIOSIZE, RF_ACTIVE); 119 if (!ch->r_altio) 120 goto failure; 121 122 rid = ATA_BMADDR_RID; 123 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 124 ATA_BMIOSIZE, RF_ACTIVE); 125 if (bootverbose) 126 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 127 (int)rman_get_start(ch->r_io), 128 (int)rman_get_start(ch->r_altio), 129 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0); 130 131 ata_reset(ch); 132 133 ch->device[MASTER].channel = ch; 134 ch->device[MASTER].unit = ATA_MASTER; 135 ch->device[MASTER].mode = ATA_PIO; 136 ch->device[SLAVE].channel = ch; 137 ch->device[SLAVE].unit = ATA_SLAVE; 138 ch->device[SLAVE].mode = ATA_PIO; 139 TAILQ_INIT(&ch->ata_queue); 140 TAILQ_INIT(&ch->atapi_queue); 141 return 0; 142 143 failure: 144 if (ch->r_io) 145 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 146 if (ch->r_altio) 147 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 148 if (ch->r_bmio) 149 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 150 if (bootverbose) 151 ata_printf(ch, -1, "probe allocation failed\n"); 152 return ENXIO; 153 } 154 155 int 156 ata_attach(device_t dev) 157 { 158 struct ata_channel *ch; 159 int error, rid; 160 161 if (!dev || !(ch = device_get_softc(dev))) 162 return ENXIO; 163 164 rid = ATA_IRQ_RID; 165 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 166 RF_SHAREABLE | RF_ACTIVE); 167 if (!ch->r_irq) { 168 ata_printf(ch, -1, "unable to allocate interrupt\n"); 169 return ENXIO; 170 } 171 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY, 172 ata_intr, ch, &ch->ih))) { 173 ata_printf(ch, -1, "unable to setup interrupt\n"); 174 return error; 175 } 176 177 /* 178 * do not attach devices if we are in early boot, this is done later 179 * when interrupts are enabled by a hook into the boot process. 180 * otherwise attach what the probe has found in ch->devices. 181 */ 182 if (!ata_delayed_attach) { 183 if (ch->devices & ATA_ATA_SLAVE) 184 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 185 ch->devices &= ~ATA_ATA_SLAVE; 186 if (ch->devices & ATA_ATAPI_SLAVE) 187 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 188 ch->devices &= ~ATA_ATAPI_SLAVE; 189 if (ch->devices & ATA_ATA_MASTER) 190 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 191 ch->devices &= ~ATA_ATA_MASTER; 192 if (ch->devices & ATA_ATAPI_MASTER) 193 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 194 ch->devices &= ~ATA_ATAPI_MASTER; 195 #ifdef DEV_ATADISK 196 if (ch->devices & ATA_ATA_MASTER) 197 ad_attach(&ch->device[MASTER]); 198 if (ch->devices & ATA_ATA_SLAVE) 199 ad_attach(&ch->device[SLAVE]); 200 #endif 201 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 202 if (ch->devices & ATA_ATAPI_MASTER) 203 atapi_attach(&ch->device[MASTER]); 204 if (ch->devices & ATA_ATAPI_SLAVE) 205 atapi_attach(&ch->device[SLAVE]); 206 #endif 207 } 208 return 0; 209 } 210 211 int 212 ata_detach(device_t dev) 213 { 214 struct ata_channel *ch; 215 int s; 216 217 if (!dev || !(ch = device_get_softc(dev))) 218 return ENXIO; 219 220 /* make sure channel is not busy */ 221 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL); 222 223 s = splbio(); 224 #ifdef DEV_ATADISK 225 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) 226 ad_detach(&ch->device[MASTER], 1); 227 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 228 ad_detach(&ch->device[SLAVE], 1); 229 #endif 230 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 231 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 232 atapi_detach(&ch->device[MASTER]); 233 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 234 atapi_detach(&ch->device[SLAVE]); 235 #endif 236 splx(s); 237 238 if (ch->device[MASTER].param) { 239 free(ch->device[MASTER].param, M_ATA); 240 ch->device[MASTER].param = NULL; 241 } 242 if (ch->device[SLAVE].param) { 243 free(ch->device[SLAVE].param, M_ATA); 244 ch->device[SLAVE].param = NULL; 245 } 246 ch->device[MASTER].driver = NULL; 247 ch->device[SLAVE].driver = NULL; 248 ch->device[MASTER].mode = ATA_PIO; 249 ch->device[SLAVE].mode = ATA_PIO; 250 ch->devices = 0; 251 252 bus_teardown_intr(dev, ch->r_irq, ch->ih); 253 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 254 if (ch->r_bmio) 255 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 256 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 257 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 258 ch->r_io = NULL; 259 ch->r_altio = NULL; 260 ch->r_bmio = NULL; 261 ch->r_irq = NULL; 262 ATA_UNLOCK_CH(ch); 263 return 0; 264 } 265 266 int 267 ata_resume(device_t dev) 268 { 269 return ata_reinit(device_get_softc(dev)); 270 } 271 272 static int 273 ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 274 { 275 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 276 struct ata_channel *ch; 277 device_t device = devclass_get_device(ata_devclass, iocmd->channel); 278 int error; 279 280 if (cmd != IOCATA) 281 return ENOTTY; 282 283 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE) 284 return ENXIO; 285 286 switch (iocmd->cmd) { 287 case ATAATTACH: 288 /* should enable channel HW on controller that can SOS XXX */ 289 error = ata_probe(device); 290 if (!error) 291 error = ata_attach(device); 292 return error; 293 294 case ATADETACH: 295 error = ata_detach(device); 296 /* should disable channel HW on controller that can SOS XXX */ 297 return error; 298 299 case ATAREINIT: 300 if (!device || !(ch = device_get_softc(device))) 301 return ENXIO; 302 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE); 303 error = ata_reinit(ch); 304 return error; 305 306 #ifdef DEV_ATADISK 307 case ATAREBUILD: 308 return ata_raid_rebuild(iocmd->channel); 309 #endif 310 case ATAGMODE: 311 if (!device || !(ch = device_get_softc(device))) 312 return ENXIO; 313 314 if ((iocmd->device == MASTER || iocmd->device == -1) && 315 ch->device[MASTER].driver) 316 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 317 else 318 iocmd->u.mode.mode[MASTER] = -1; 319 320 if ((iocmd->device == SLAVE || iocmd->device == -1) && 321 ch->device[SLAVE].param) 322 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 323 else 324 iocmd->u.mode.mode[SLAVE] = -1; 325 return 0; 326 327 case ATASMODE: 328 if (!device || !(ch = device_get_softc(device))) 329 return ENXIO; 330 331 if ((iocmd->device == MASTER || iocmd->device == -1) && 332 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) { 333 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]); 334 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 335 } 336 else 337 iocmd->u.mode.mode[MASTER] = -1; 338 339 if ((iocmd->device == SLAVE || iocmd->device == -1) && 340 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) { 341 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]); 342 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 343 } 344 else 345 iocmd->u.mode.mode[SLAVE] = -1; 346 return 0; 347 348 case ATAGPARM: 349 if (!device || !(ch = device_get_softc(device))) 350 return ENXIO; 351 352 iocmd->u.param.type[MASTER] = 353 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 354 iocmd->u.param.type[SLAVE] = 355 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 356 357 if (ch->device[MASTER].name) 358 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 359 if (ch->device[SLAVE].name) 360 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 361 362 if (ch->device[MASTER].param) 363 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 364 sizeof(struct ata_params)); 365 if (ch->device[SLAVE].param) 366 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 367 sizeof(struct ata_params)); 368 return 0; 369 370 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 371 case ATAPICMD: { 372 struct ata_device *atadev; 373 caddr_t buf; 374 375 if (!device || !(ch = device_get_softc(device))) 376 return ENXIO; 377 378 if (!(atadev = &ch->device[iocmd->device]) || 379 !(ch->devices & (iocmd->device == MASTER ? 380 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))) 381 return ENODEV; 382 383 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT))) 384 return ENOMEM; 385 386 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) { 387 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count); 388 if (error) 389 return error; 390 } 391 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb, 392 buf, iocmd->u.atapi.count, 393 (iocmd->u.atapi.flags == ATAPI_CMD_READ ? 394 ATPR_F_READ : 0) | ATPR_F_QUIET, 395 iocmd->u.atapi.timeout, NULL, NULL); 396 if (error) { 397 iocmd->u.atapi.error = error; 398 bcopy(&atadev->result, iocmd->u.atapi.sense_data, 399 sizeof(struct atapi_reqsense)); 400 error = 0; 401 } 402 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ) 403 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count); 404 405 free(buf, M_ATA); 406 return error; 407 } 408 #endif 409 default: 410 break; 411 } 412 return ENOTTY; 413 } 414 415 static int 416 ata_getparam(struct ata_device *atadev, u_int8_t command) 417 { 418 struct ata_params *ata_parm; 419 int retry = 0; 420 421 /* apparently some devices needs this repeated */ 422 do { 423 if (ata_command(atadev, command, 0, 0, 0, ATA_WAIT_INTR)) { 424 ata_prtdev(atadev, "%s identify failed\n", 425 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 426 return -1; 427 } 428 if (retry++ > 4) { 429 ata_prtdev(atadev, "%s identify retries exceeded\n", 430 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 431 return -1; 432 } 433 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ? 434 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ)))); 435 436 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 437 if (!ata_parm) { 438 int i; 439 440 for (i = 0; i < sizeof(struct ata_params)/sizeof(int16_t); i++) 441 ATA_INW(atadev->channel->r_io, ATA_DATA); 442 ata_prtdev(atadev, "malloc for identify data failed\n"); 443 return -1; 444 } 445 446 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm, 447 sizeof(struct ata_params)/sizeof(int16_t)); 448 449 if (command == ATA_C_ATA_IDENTIFY || 450 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 451 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') || 452 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i'))) 453 bswap(ata_parm->model, sizeof(ata_parm->model)); 454 btrim(ata_parm->model, sizeof(ata_parm->model)); 455 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 456 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 457 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 458 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 459 atadev->param = ata_parm; 460 return 0; 461 } 462 463 static void 464 ata_boot_attach(void) 465 { 466 struct ata_channel *ch; 467 int ctlr; 468 469 if (ata_delayed_attach) { 470 config_intrhook_disestablish(ata_delayed_attach); 471 free(ata_delayed_attach, M_TEMP); 472 ata_delayed_attach = NULL; 473 } 474 475 /* 476 * run through all ata devices and look for real ATA & ATAPI devices 477 * using the hints we found in the early probe, this avoids some of 478 * the delays probing of non-exsistent devices can cause. 479 */ 480 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 481 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 482 continue; 483 if (ch->devices & ATA_ATA_SLAVE) 484 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 485 ch->devices &= ~ATA_ATA_SLAVE; 486 if (ch->devices & ATA_ATAPI_SLAVE) 487 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 488 ch->devices &= ~ATA_ATAPI_SLAVE; 489 if (ch->devices & ATA_ATA_MASTER) 490 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 491 ch->devices &= ~ATA_ATA_MASTER; 492 if (ch->devices & ATA_ATAPI_MASTER) 493 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 494 ch->devices &= ~ATA_ATAPI_MASTER; 495 } 496 497 #ifdef DEV_ATADISK 498 /* now we know whats there, do the real attach, first the ATA disks */ 499 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 500 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 501 continue; 502 if (ch->devices & ATA_ATA_MASTER) 503 ad_attach(&ch->device[MASTER]); 504 if (ch->devices & ATA_ATA_SLAVE) 505 ad_attach(&ch->device[SLAVE]); 506 } 507 ata_raid_attach(); 508 #endif 509 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 510 /* then the atapi devices */ 511 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 512 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 513 continue; 514 if (ch->devices & ATA_ATAPI_MASTER) 515 atapi_attach(&ch->device[MASTER]); 516 if (ch->devices & ATA_ATAPI_SLAVE) 517 atapi_attach(&ch->device[SLAVE]); 518 } 519 #endif 520 } 521 522 static void 523 ata_intr(void *data) 524 { 525 struct ata_channel *ch = (struct ata_channel *)data; 526 /* 527 * on PCI systems we might share an interrupt line with another 528 * device or our twin ATA channel, so call ch->intr_func to figure 529 * out if it is really an interrupt we should process here 530 */ 531 if (ch->intr_func && ch->intr_func(ch)) 532 return; 533 534 /* if drive is busy it didn't interrupt */ 535 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) { 536 DELAY(100); 537 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ)) 538 return; 539 } 540 541 /* clear interrupt and get status */ 542 ch->status = ATA_INB(ch->r_io, ATA_STATUS); 543 544 if (ch->status & ATA_S_ERROR) 545 ch->error = ATA_INB(ch->r_io, ATA_ERROR); 546 547 /* find & call the responsible driver to process this interrupt */ 548 switch (ch->active) { 549 #ifdef DEV_ATADISK 550 case ATA_ACTIVE_ATA: 551 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES) 552 return; 553 break; 554 #endif 555 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 556 case ATA_ACTIVE_ATAPI: 557 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES) 558 return; 559 break; 560 #endif 561 case ATA_WAIT_INTR: 562 case ATA_WAIT_INTR | ATA_CONTROL: 563 wakeup((caddr_t)ch); 564 break; 565 566 case ATA_WAIT_READY: 567 case ATA_WAIT_READY | ATA_CONTROL: 568 break; 569 570 case ATA_IDLE: 571 if (ch->flags & ATA_QUEUED) { 572 ch->active = ATA_ACTIVE; 573 if (ata_service(ch) == ATA_OP_CONTINUES) 574 return; 575 } 576 /* FALLTHROUGH */ 577 578 default: 579 #ifdef ATA_DEBUG 580 { 581 static int intr_count = 0; 582 583 if (intr_count++ < 10) 584 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n", 585 intr_count, ch->active, ch->status); 586 } 587 #endif 588 break; 589 } 590 ch->active &= ATA_CONTROL; 591 if (ch->active & ATA_CONTROL) 592 return; 593 ch->running = NULL; 594 ata_start(ch); 595 return; 596 } 597 598 void 599 ata_start(struct ata_channel *ch) 600 { 601 #ifdef DEV_ATADISK 602 struct ad_request *ad_request; 603 #endif 604 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 605 struct atapi_request *atapi_request; 606 #endif 607 int s; 608 609 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) 610 return; 611 612 s = splbio(); 613 #ifdef DEV_ATADISK 614 /* find & call the responsible driver if anything on the ATA queue */ 615 if (TAILQ_EMPTY(&ch->ata_queue)) { 616 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver) 617 ad_start(&ch->device[MASTER]); 618 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 619 ad_start(&ch->device[SLAVE]); 620 } 621 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) { 622 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain); 623 ch->active = ATA_ACTIVE_ATA; 624 ch->running = ad_request; 625 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) { 626 splx(s); 627 return; 628 } 629 } 630 631 #endif 632 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 633 /* find & call the responsible driver if anything on the ATAPI queue */ 634 if (TAILQ_EMPTY(&ch->atapi_queue)) { 635 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) 636 atapi_start(&ch->device[MASTER]); 637 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) 638 atapi_start(&ch->device[SLAVE]); 639 } 640 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) { 641 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain); 642 ch->active = ATA_ACTIVE_ATAPI; 643 ch->running = atapi_request; 644 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) { 645 splx(s); 646 return; 647 } 648 } 649 #endif 650 splx(s); 651 ATA_UNLOCK_CH(ch); 652 } 653 654 void 655 ata_reset(struct ata_channel *ch) 656 { 657 u_int8_t lsb, msb, ostat0, ostat1; 658 u_int8_t stat0 = 0, stat1 = 0; 659 int mask = 0, timeout; 660 661 /* do we have any signs of ATA/ATAPI HW being present ? */ 662 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 663 DELAY(10); 664 ostat0 = ATA_INB(ch->r_io, ATA_STATUS); 665 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) { 666 stat0 = ATA_S_BUSY; 667 mask |= 0x01; 668 } 669 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 670 DELAY(10); 671 ostat1 = ATA_INB(ch->r_io, ATA_STATUS); 672 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) { 673 stat1 = ATA_S_BUSY; 674 mask |= 0x02; 675 } 676 677 ch->devices = 0; 678 if (!mask) 679 return; 680 681 /* in some setups we dont want to test for a slave */ 682 if (ch->flags & ATA_NO_SLAVE) { 683 stat1 = 0x0; 684 mask &= ~0x02; 685 } 686 687 if (bootverbose) 688 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n", 689 mask, ostat0, ostat1); 690 691 /* reset channel */ 692 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 693 DELAY(10); 694 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 695 DELAY(10000); 696 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS); 697 DELAY(100000); 698 ATA_INB(ch->r_io, ATA_ERROR); 699 700 /* wait for BUSY to go inactive */ 701 for (timeout = 0; timeout < 310000; timeout++) { 702 if (stat0 & ATA_S_BUSY) { 703 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 704 DELAY(10); 705 stat0 = ATA_INB(ch->r_io, ATA_STATUS); 706 if (!(stat0 & ATA_S_BUSY)) { 707 /* check for ATAPI signature while its still there */ 708 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 709 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 710 if (bootverbose) 711 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb); 712 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 713 ch->devices |= ATA_ATAPI_MASTER; 714 } 715 } 716 if (stat1 & ATA_S_BUSY) { 717 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 718 DELAY(10); 719 stat1 = ATA_INB(ch->r_io, ATA_STATUS); 720 if (!(stat1 & ATA_S_BUSY)) { 721 /* check for ATAPI signature while its still there */ 722 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 723 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 724 if (bootverbose) 725 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb); 726 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 727 ch->devices |= ATA_ATAPI_SLAVE; 728 } 729 } 730 if (mask == 0x01) /* wait for master only */ 731 if (!(stat0 & ATA_S_BUSY)) 732 break; 733 if (mask == 0x02) /* wait for slave only */ 734 if (!(stat1 & ATA_S_BUSY)) 735 break; 736 if (mask == 0x03) /* wait for both master & slave */ 737 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 738 break; 739 DELAY(100); 740 } 741 DELAY(10); 742 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 743 744 if (stat0 & ATA_S_BUSY) 745 mask &= ~0x01; 746 if (stat1 & ATA_S_BUSY) 747 mask &= ~0x02; 748 if (bootverbose) 749 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n", 750 mask, stat0, stat1); 751 if (!mask) 752 return; 753 754 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) { 755 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 756 DELAY(10); 757 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 758 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 759 lsb = ATA_INB(ch->r_io, ATA_ERROR); 760 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 761 if (bootverbose) 762 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb); 763 if (lsb != 0x58 && msb == 0xa5) 764 ch->devices |= ATA_ATA_MASTER; 765 } 766 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) { 767 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 768 DELAY(10); 769 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 770 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 771 lsb = ATA_INB(ch->r_io, ATA_ERROR); 772 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 773 if (bootverbose) 774 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb); 775 if (lsb != 0x58 && msb == 0xa5) 776 ch->devices |= ATA_ATA_SLAVE; 777 } 778 if (bootverbose) 779 ata_printf(ch, -1, "devices=%02x\n", ch->devices); 780 } 781 782 int 783 ata_reinit(struct ata_channel *ch) 784 { 785 int devices, misdev, newdev; 786 787 if (!ch->r_io || !ch->r_altio || !ch->r_irq) 788 return ENXIO; 789 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 790 ch->running = NULL; 791 devices = ch->devices; 792 ata_printf(ch, -1, "resetting devices .. "); 793 ata_reset(ch); 794 795 if ((misdev = devices & ~ch->devices)) { 796 if (misdev) 797 printf("\n"); 798 #ifdef DEV_ATADISK 799 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver) 800 ad_detach(&ch->device[MASTER], 0); 801 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 802 ad_detach(&ch->device[SLAVE], 0); 803 #endif 804 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 805 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 806 atapi_detach(&ch->device[MASTER]); 807 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 808 atapi_detach(&ch->device[SLAVE]); 809 #endif 810 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 811 if (ch->device[MASTER].param) 812 free(ch->device[MASTER].param, M_ATA); 813 ch->device[MASTER].param = NULL; 814 } 815 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 816 if (ch->device[SLAVE].param) 817 free(ch->device[SLAVE].param, M_ATA); 818 ch->device[SLAVE].param = NULL; 819 } 820 } 821 if ((newdev = ~devices & ch->devices)) { 822 if (newdev & ATA_ATA_MASTER) 823 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 824 newdev &= ~ATA_ATA_MASTER; 825 if (newdev & ATA_ATA_SLAVE) 826 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 827 newdev &= ~ATA_ATA_SLAVE; 828 if (newdev & ATA_ATAPI_MASTER) 829 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 830 newdev &= ~ATA_ATAPI_MASTER; 831 if (newdev & ATA_ATAPI_SLAVE) 832 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 833 newdev &= ~ATA_ATAPI_SLAVE; 834 } 835 if (!misdev && newdev) 836 printf("\n"); 837 #ifdef DEV_ATADISK 838 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver) 839 ad_attach(&ch->device[MASTER]); 840 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) { 841 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY); 842 ad_reinit(&ch->device[MASTER]); 843 } 844 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver) 845 ad_attach(&ch->device[SLAVE]); 846 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) { 847 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY); 848 ad_reinit(&ch->device[SLAVE]); 849 } 850 #endif 851 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 852 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver) 853 atapi_attach(&ch->device[MASTER]); 854 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) { 855 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY); 856 atapi_reinit(&ch->device[MASTER]); 857 } 858 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver) 859 atapi_attach(&ch->device[SLAVE]); 860 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) { 861 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY); 862 atapi_reinit(&ch->device[SLAVE]); 863 } 864 #endif 865 printf("done\n"); 866 ATA_UNLOCK_CH(ch); 867 ata_start(ch); 868 return 0; 869 } 870 871 static int 872 ata_service(struct ata_channel *ch) 873 { 874 /* do we have a SERVICE request from the drive ? */ 875 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) { 876 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 877 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT); 878 #ifdef DEV_ATADISK 879 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 880 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver) 881 return ad_service((struct ad_softc *) 882 ch->device[MASTER].driver, 0); 883 } 884 else { 885 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 886 return ad_service((struct ad_softc *) 887 ch->device[SLAVE].driver, 0); 888 } 889 #endif 890 } 891 return ATA_OP_FINISHED; 892 } 893 894 int 895 ata_wait(struct ata_device *atadev, u_int8_t mask) 896 { 897 int timeout = 0; 898 899 DELAY(1); 900 while (timeout < 5000000) { /* timeout 5 secs */ 901 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 902 903 /* if drive fails status, reselect the drive just to be sure */ 904 if (atadev->channel->status == 0xff) { 905 ata_prtdev(atadev, "no status, reselecting device\n"); 906 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit); 907 DELAY(10); 908 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS); 909 if (atadev->channel->status == 0xff) 910 return -1; 911 } 912 913 /* are we done ? */ 914 if (!(atadev->channel->status & ATA_S_BUSY)) 915 break; 916 917 if (timeout > 1000) { 918 timeout += 1000; 919 DELAY(1000); 920 } 921 else { 922 timeout += 10; 923 DELAY(10); 924 } 925 } 926 if (atadev->channel->status & ATA_S_ERROR) 927 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR); 928 if (timeout >= 5000000) 929 return -1; 930 if (!mask) 931 return (atadev->channel->status & ATA_S_ERROR); 932 933 /* Wait 50 msec for bits wanted. */ 934 timeout = 5000; 935 while (timeout--) { 936 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 937 if ((atadev->channel->status & mask) == mask) { 938 if (atadev->channel->status & ATA_S_ERROR) 939 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR); 940 return (atadev->channel->status & ATA_S_ERROR); 941 } 942 DELAY (10); 943 } 944 return -1; 945 } 946 947 int 948 ata_command(struct ata_device *atadev, u_int8_t command, 949 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags) 950 { 951 int error = 0; 952 #ifdef ATA_DEBUG 953 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, " 954 "lba=%lld, count=%d, feature=%d, flags=%02x\n", 955 rman_get_start(atadev->channel->r_io), 956 command, lba, count, feature, flags); 957 #endif 958 959 /* select device */ 960 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 961 962 /* disable interrupt from device */ 963 if (atadev->channel->flags & ATA_QUEUED) 964 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 965 966 /* ready to issue command ? */ 967 if (ata_wait(atadev, 0) < 0) { 968 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n", 969 command, atadev->channel->status, atadev->channel->error); 970 return -1; 971 } 972 973 /* only use 48bit addressing if needed because of the overhead */ 974 if ((lba > 268435455 || count > 256) && atadev->param && 975 atadev->param->support.address48) { 976 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff); 977 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 978 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff); 979 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff); 980 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff); 981 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 982 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba<<32) & 0xff); 983 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 984 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff); 985 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 986 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit); 987 988 /* translate command into 48bit version */ 989 switch (command) { 990 case ATA_C_READ: 991 command = ATA_C_READ48; break; 992 case ATA_C_READ_MUL: 993 command = ATA_C_READ_MUL48; break; 994 case ATA_C_READ_DMA: 995 command = ATA_C_READ_DMA48; break; 996 case ATA_C_READ_DMA_QUEUED: 997 command = ATA_C_READ_DMA_QUEUED48; break; 998 case ATA_C_WRITE: 999 command = ATA_C_WRITE48; break; 1000 case ATA_C_WRITE_MUL: 1001 command = ATA_C_WRITE_MUL48; break; 1002 case ATA_C_WRITE_DMA: 1003 command = ATA_C_WRITE_DMA48; break; 1004 case ATA_C_WRITE_DMA_QUEUED: 1005 command = ATA_C_WRITE_DMA_QUEUED48; break; 1006 case ATA_C_FLUSHCACHE: 1007 command = ATA_C_FLUSHCACHE48; break; 1008 default: 1009 ata_prtdev(atadev, "can't translate cmd to 48bit version\n"); 1010 return -1; 1011 } 1012 } 1013 else { 1014 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1015 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count); 1016 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1017 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1018 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1019 if (atadev->flags & ATA_D_USE_CHS) 1020 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1021 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf)); 1022 else 1023 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1024 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf)); 1025 } 1026 1027 switch (flags & ATA_WAIT_MASK) { 1028 case ATA_IMMEDIATE: 1029 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1030 1031 /* enable interrupt */ 1032 if (atadev->channel->flags & ATA_QUEUED) 1033 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1034 break; 1035 1036 case ATA_WAIT_INTR: 1037 atadev->channel->active |= ATA_WAIT_INTR; 1038 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1039 1040 /* enable interrupt */ 1041 if (atadev->channel->flags & ATA_QUEUED) 1042 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1043 1044 if (tsleep((caddr_t)atadev->channel, PRIBIO, "atacmd", 10 * hz)) { 1045 ata_prtdev(atadev, "timeout waiting for interrupt\n"); 1046 atadev->channel->active &= ~ATA_WAIT_INTR; 1047 error = -1; 1048 } 1049 break; 1050 1051 case ATA_WAIT_READY: 1052 atadev->channel->active |= ATA_WAIT_READY; 1053 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1054 if (ata_wait(atadev, ATA_S_READY) < 0) { 1055 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n", 1056 command, atadev->channel->status,atadev->channel->error); 1057 error = -1; 1058 } 1059 atadev->channel->active &= ~ATA_WAIT_READY; 1060 break; 1061 } 1062 return error; 1063 } 1064 1065 void 1066 ata_drawerleds(struct ata_device *atadev, u_int8_t color) 1067 { 1068 u_int8_t count, drive; 1069 1070 /* magic sequence to set the LED color on the Promise SuperSwap */ 1071 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1072 DELAY(1); 1073 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1074 DELAY(1); 1075 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1076 DELAY(1); 1077 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1078 DELAY(1); 1079 count = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1080 DELAY(1); 1081 drive = ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1082 DELAY(1); 1083 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, color); 1084 DELAY(1); 1085 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1086 DELAY(1); 1087 } 1088 1089 static void 1090 ata_change_mode(struct ata_device *atadev, int mode) 1091 { 1092 int umode, wmode, pmode; 1093 1094 umode = ata_umode(atadev->param); 1095 wmode = ata_wmode(atadev->param); 1096 pmode = ata_pmode(atadev->param); 1097 1098 switch (mode & ATA_DMA_MASK) { 1099 case ATA_UDMA: 1100 if ((mode & ATA_MODE_MASK) < umode) 1101 umode = mode & ATA_MODE_MASK; 1102 break; 1103 case ATA_WDMA: 1104 if ((mode & ATA_MODE_MASK) < wmode) 1105 wmode = mode & ATA_MODE_MASK; 1106 umode = -1; 1107 break; 1108 default: 1109 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode) 1110 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0; 1111 umode = -1; 1112 wmode = -1; 1113 } 1114 1115 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE); 1116 ata_dmainit(atadev->channel, atadev->unit, pmode, wmode, umode); 1117 ATA_UNLOCK_CH(atadev->channel); 1118 ata_start(atadev->channel); /* XXX SOS */ 1119 } 1120 1121 int 1122 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 1123 { 1124 va_list ap; 1125 int ret; 1126 1127 if (device == -1) 1128 ret = printf("ata%d: ", device_get_unit(ch->dev)); 1129 else { 1130 if (ch->device[ATA_DEV(device)].name) 1131 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 1132 else 1133 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 1134 (device == ATA_MASTER) ? "master" : "slave"); 1135 } 1136 va_start(ap, fmt); 1137 ret += vprintf(fmt, ap); 1138 va_end(ap); 1139 return ret; 1140 } 1141 1142 int 1143 ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 1144 { 1145 va_list ap; 1146 int ret; 1147 1148 if (atadev->name) 1149 ret = printf("%s: ", atadev->name); 1150 else 1151 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 1152 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 1153 va_start(ap, fmt); 1154 ret += vprintf(fmt, ap); 1155 va_end(ap); 1156 return ret; 1157 } 1158 1159 void 1160 ata_set_name(struct ata_device *atadev, char *name, int lun) 1161 { 1162 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 1163 if (atadev->name) 1164 sprintf(atadev->name, "%s%d", name, lun); 1165 } 1166 1167 void 1168 ata_free_name(struct ata_device *atadev) 1169 { 1170 if (atadev->name) 1171 free(atadev->name, M_ATA); 1172 atadev->name = NULL; 1173 } 1174 1175 int 1176 ata_get_lun(u_int32_t *map) 1177 { 1178 int lun = ffs(~*map) - 1; 1179 1180 *map |= (1 << lun); 1181 return lun; 1182 } 1183 1184 int 1185 ata_test_lun(u_int32_t *map, int lun) 1186 { 1187 return (*map & (1 << lun)); 1188 } 1189 1190 void 1191 ata_free_lun(u_int32_t *map, int lun) 1192 { 1193 *map &= ~(1 << lun); 1194 } 1195 1196 char * 1197 ata_mode2str(int mode) 1198 { 1199 switch (mode) { 1200 case ATA_PIO: return "BIOSPIO"; 1201 case ATA_PIO0: return "PIO0"; 1202 case ATA_PIO1: return "PIO1"; 1203 case ATA_PIO2: return "PIO2"; 1204 case ATA_PIO3: return "PIO3"; 1205 case ATA_PIO4: return "PIO4"; 1206 case ATA_DMA: return "BIOSDMA"; 1207 case ATA_WDMA2: return "WDMA2"; 1208 case ATA_UDMA2: return "UDMA33"; 1209 case ATA_UDMA4: return "UDMA66"; 1210 case ATA_UDMA5: return "UDMA100"; 1211 case ATA_UDMA6: return "UDMA133"; 1212 default: return "???"; 1213 } 1214 } 1215 1216 int 1217 ata_pmode(struct ata_params *ap) 1218 { 1219 if (ap->atavalid & ATA_FLAG_64_70) { 1220 if (ap->apiomodes & 2) 1221 return 4; 1222 if (ap->apiomodes & 1) 1223 return 3; 1224 } 1225 if (ap->retired_piomode == 2) 1226 return 2; 1227 if (ap->retired_piomode == 1) 1228 return 1; 1229 if (ap->retired_piomode == 0) 1230 return 0; 1231 return -1; 1232 } 1233 1234 int 1235 ata_wmode(struct ata_params *ap) 1236 { 1237 if (ap->mwdmamodes & 0x04) 1238 return 2; 1239 if (ap->mwdmamodes & 0x02) 1240 return 1; 1241 if (ap->mwdmamodes & 0x01) 1242 return 0; 1243 return -1; 1244 } 1245 1246 int 1247 ata_umode(struct ata_params *ap) 1248 { 1249 if (ap->atavalid & ATA_FLAG_88) { 1250 if (ap->udmamodes & 0x40) 1251 return 6; 1252 if (ap->udmamodes & 0x20) 1253 return 5; 1254 if (ap->udmamodes & 0x10) 1255 return 4; 1256 if (ap->udmamodes & 0x08) 1257 return 3; 1258 if (ap->udmamodes & 0x04) 1259 return 2; 1260 if (ap->udmamodes & 0x02) 1261 return 1; 1262 if (ap->udmamodes & 0x01) 1263 return 0; 1264 } 1265 return -1; 1266 } 1267 1268 static void 1269 bswap(int8_t *buf, int len) 1270 { 1271 u_int16_t *ptr = (u_int16_t*)(buf + len); 1272 1273 while (--ptr >= (u_int16_t*)buf) 1274 *ptr = ntohs(*ptr); 1275 } 1276 1277 static void 1278 btrim(int8_t *buf, int len) 1279 { 1280 int8_t *ptr; 1281 1282 for (ptr = buf; ptr < buf+len; ++ptr) 1283 if (!*ptr) 1284 *ptr = ' '; 1285 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1286 *ptr = 0; 1287 } 1288 1289 static void 1290 bpack(int8_t *src, int8_t *dst, int len) 1291 { 1292 int i, j, blank; 1293 1294 for (i = j = blank = 0 ; i < len; i++) { 1295 if (blank && src[i] == ' ') continue; 1296 if (blank && src[i] != ' ') { 1297 dst[j++] = src[i]; 1298 blank = 0; 1299 continue; 1300 } 1301 if (src[i] == ' ') { 1302 blank = 1; 1303 if (i == 0) 1304 continue; 1305 } 1306 dst[j++] = src[i]; 1307 } 1308 if (j < len) 1309 dst[j] = 0x00; 1310 } 1311 1312 static void 1313 ata_init(void) 1314 { 1315 /* register controlling device */ 1316 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1317 1318 /* register boot attach to be run when interrupts are enabled */ 1319 if (!(ata_delayed_attach = (struct intr_config_hook *) 1320 malloc(sizeof(struct intr_config_hook), 1321 M_TEMP, M_NOWAIT | M_ZERO))) { 1322 printf("ata: malloc of delayed attach hook failed\n"); 1323 return; 1324 } 1325 1326 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1327 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1328 printf("ata: config_intrhook_establish failed\n"); 1329 free(ata_delayed_attach, M_TEMP); 1330 } 1331 } 1332 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1333