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