73}; 74 75/* prototypes */ 76static void ata_boot_attach(void); 77static void ata_intr(void *); 78static int ata_getparam(struct ata_device *, u_int8_t); 79static int ata_service(struct ata_channel *); 80static void bswap(int8_t *, int); 81static void btrim(int8_t *, int); 82static void bpack(int8_t *, int8_t *, int); 83static void ata_change_mode(struct ata_device *, int); 84static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t); 85static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *); 86 87/* sysctl vars */ 88SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 89 90/* global vars */ 91struct intr_config_hook *ata_delayed_attach = NULL; 92devclass_t ata_devclass; 93 94/* local vars */ 95static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 96 97/* misc defines */ 98#define DEV_ATAPIALL defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || \ 99 defined(DEV_ATAPIST) || defined(DEV_ATAPICAM) 100 101int 102ata_probe(device_t dev) 103{ 104 struct ata_channel *ch; 105 int rid; 106 107 if (!dev || !(ch = device_get_softc(dev))) 108 return ENXIO; 109 110 if (ch->r_io || ch->r_altio || ch->r_irq) 111 return EEXIST; 112 113 /* initialize the softc basics */ 114 ch->active = ATA_IDLE; 115 ch->dev = dev; 116 117 rid = ATA_IOADDR_RID; 118 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 119 ATA_IOSIZE, RF_ACTIVE); 120 if (!ch->r_io) 121 goto failure; 122 123 rid = ATA_ALTADDR_RID; 124 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 125 ATA_ALTIOSIZE, RF_ACTIVE); 126 if (!ch->r_altio) 127 goto failure; 128 129 rid = ATA_BMADDR_RID; 130 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 131 ATA_BMIOSIZE, RF_ACTIVE); 132 if (bootverbose) 133 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 134 (int)rman_get_start(ch->r_io), 135 (int)rman_get_start(ch->r_altio), 136 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0); 137 138 ch->locking(ch, ATA_LF_LOCK); 139 ata_reset(ch); 140 ch->locking(ch, ATA_LF_UNLOCK); 141 142 ch->device[MASTER].channel = ch; 143 ch->device[MASTER].unit = ATA_MASTER; 144 ch->device[MASTER].mode = ATA_PIO; 145 ch->device[SLAVE].channel = ch; 146 ch->device[SLAVE].unit = ATA_SLAVE; 147 ch->device[SLAVE].mode = ATA_PIO; 148 TAILQ_INIT(&ch->ata_queue); 149 TAILQ_INIT(&ch->atapi_queue); 150 return 0; 151 152failure: 153 if (ch->r_io) 154 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 155 if (ch->r_altio) 156 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 157 if (ch->r_bmio) 158 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 159 if (bootverbose) 160 ata_printf(ch, -1, "probe allocation failed\n"); 161 return ENXIO; 162} 163 164int 165ata_attach(device_t dev) 166{ 167 struct ata_channel *ch; 168 int error, rid; 169 170 if (!dev || !(ch = device_get_softc(dev))) 171 return ENXIO; 172 173 rid = ATA_IRQ_RID; 174 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 175 RF_SHAREABLE | RF_ACTIVE); 176 if (!ch->r_irq) { 177 ata_printf(ch, -1, "unable to allocate interrupt\n"); 178 return ENXIO; 179 } 180 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY, 181 ata_intr, ch, &ch->ih))) { 182 ata_printf(ch, -1, "unable to setup interrupt\n"); 183 return error; 184 } 185 186 if (ch->dma) 187 ch->dma->create(ch); 188 189 /* 190 * do not attach devices if we are in early boot, this is done later 191 * when interrupts are enabled by a hook into the boot process. 192 * otherwise attach what the probe has found in ch->devices. 193 */ 194 if (!ata_delayed_attach) { 195 ch->locking(ch, ATA_LF_LOCK); 196 if (ch->devices & ATA_ATA_SLAVE) 197 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 198 ch->devices &= ~ATA_ATA_SLAVE; 199 if (ch->devices & ATA_ATAPI_SLAVE) 200 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 201 ch->devices &= ~ATA_ATAPI_SLAVE; 202 if (ch->devices & ATA_ATA_MASTER) 203 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 204 ch->devices &= ~ATA_ATA_MASTER; 205 if (ch->devices & ATA_ATAPI_MASTER) 206 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 207 ch->devices &= ~ATA_ATAPI_MASTER; 208#ifdef DEV_ATADISK 209 if (ch->devices & ATA_ATA_MASTER) 210 ad_attach(&ch->device[MASTER]); 211 if (ch->devices & ATA_ATA_SLAVE) 212 ad_attach(&ch->device[SLAVE]); 213#endif 214#if DEV_ATAPIALL 215 if (ch->devices & ATA_ATAPI_MASTER) 216 atapi_attach(&ch->device[MASTER]); 217 if (ch->devices & ATA_ATAPI_SLAVE) 218 atapi_attach(&ch->device[SLAVE]); 219#endif 220#ifdef DEV_ATAPICAM 221 atapi_cam_attach_bus(ch); 222#endif 223 ch->locking(ch, ATA_LF_UNLOCK); 224 } 225 return 0; 226} 227 228int 229ata_detach(device_t dev) 230{ 231 struct ata_channel *ch; 232 int s; 233 234 if (!dev || !(ch = device_get_softc(dev)) || 235 !ch->r_io || !ch->r_altio || !ch->r_irq) 236 return ENXIO; 237 238 /* make sure channel is not busy */ 239 ch->locking(ch, ATA_LF_LOCK); 240 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL); 241 242 s = splbio(); 243#ifdef DEV_ATADISK 244 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) 245 ad_detach(&ch->device[MASTER], 1); 246 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 247 ad_detach(&ch->device[SLAVE], 1); 248#endif 249#if DEV_ATAPIALL 250 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 251 atapi_detach(&ch->device[MASTER]); 252 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 253 atapi_detach(&ch->device[SLAVE]); 254#endif 255#ifdef DEV_ATAPICAM 256 atapi_cam_detach_bus(ch); 257#endif 258 splx(s); 259 260 if (ch->device[MASTER].param) { 261 free(ch->device[MASTER].param, M_ATA); 262 ch->device[MASTER].param = NULL; 263 } 264 if (ch->device[SLAVE].param) { 265 free(ch->device[SLAVE].param, M_ATA); 266 ch->device[SLAVE].param = NULL; 267 } 268 ch->device[MASTER].driver = NULL; 269 ch->device[SLAVE].driver = NULL; 270 ch->device[MASTER].mode = ATA_PIO; 271 ch->device[SLAVE].mode = ATA_PIO; 272 ch->devices = 0; 273 if (ch->dma) 274 ch->dma->destroy(ch); 275 276 bus_teardown_intr(dev, ch->r_irq, ch->ih); 277 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 278 if (ch->r_bmio) 279 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 280 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 281 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 282 ch->r_io = NULL; 283 ch->r_altio = NULL; 284 ch->r_bmio = NULL; 285 ch->r_irq = NULL; 286 ATA_UNLOCK_CH(ch); 287 ch->locking(ch, ATA_LF_UNLOCK); 288 return 0; 289} 290 291int 292ata_resume(device_t dev) 293{ 294 struct ata_channel *ch; 295 int error; 296 297 if (!dev || !(ch = device_get_softc(dev))) 298 return ENXIO; 299 300 ch->locking(ch, ATA_LF_LOCK); 301 error = ata_reinit(ch); 302 ch->locking(ch, ATA_LF_UNLOCK); 303 return error; 304} 305 306static int 307ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 308{ 309 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 310 struct ata_channel *ch; 311 device_t device = devclass_get_device(ata_devclass, iocmd->channel); 312 int error; 313 314 if (cmd != IOCATA) 315 return ENOTTY; 316 317 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE) 318 return ENXIO; 319 320 switch (iocmd->cmd) { 321 case ATAATTACH: 322 /* should enable channel HW on controller that can SOS XXX */ 323 error = ata_probe(device); 324 if (!error) 325 error = ata_attach(device); 326 return error; 327 328 case ATADETACH: 329 error = ata_detach(device); 330 /* should disable channel HW on controller that can SOS XXX */ 331 return error; 332 333 case ATAREINIT: 334 if (!device || !(ch = device_get_softc(device))) 335 return ENXIO; 336 ch->locking(ch, ATA_LF_LOCK); 337 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE); 338 error = ata_reinit(ch); 339 ch->locking(ch, ATA_LF_UNLOCK); 340 return error; 341 342 case ATAGMODE: 343 if (!device || !(ch = device_get_softc(device))) 344 return ENXIO; 345 346 if ((iocmd->device == MASTER || iocmd->device == -1) && 347 ch->device[MASTER].driver) 348 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 349 else 350 iocmd->u.mode.mode[MASTER] = -1; 351 352 if ((iocmd->device == SLAVE || iocmd->device == -1) && 353 ch->device[SLAVE].param) 354 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 355 else 356 iocmd->u.mode.mode[SLAVE] = -1; 357 return 0; 358 359 case ATASMODE: 360 if (!device || !(ch = device_get_softc(device))) 361 return ENXIO; 362 363 ch->locking(ch, ATA_LF_LOCK); 364 if ((iocmd->device == MASTER || iocmd->device == -1) && 365 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) { 366 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]); 367 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 368 } 369 else 370 iocmd->u.mode.mode[MASTER] = -1; 371 372 if ((iocmd->device == SLAVE || iocmd->device == -1) && 373 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) { 374 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]); 375 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 376 } 377 else 378 iocmd->u.mode.mode[SLAVE] = -1; 379 ch->locking(ch, ATA_LF_UNLOCK); 380 return 0; 381 382 case ATAGPARM: 383 if (!device || !(ch = device_get_softc(device))) 384 return ENXIO; 385 386 iocmd->u.param.type[MASTER] = 387 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 388 iocmd->u.param.type[SLAVE] = 389 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 390 391 if (ch->device[MASTER].name) 392 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 393 if (ch->device[SLAVE].name) 394 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 395 396 if (ch->device[MASTER].param) 397 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 398 sizeof(struct ata_params)); 399 if (ch->device[SLAVE].param) 400 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 401 sizeof(struct ata_params)); 402 return 0; 403 404 case ATAENCSTAT: { 405 struct ata_device *atadev; 406 407 if (!device || !(ch = device_get_softc(device))) 408 return ENXIO; 409 410 if (iocmd->device == SLAVE) 411 atadev = &ch->device[SLAVE]; 412 else 413 atadev = &ch->device[MASTER]; 414 415 return ata_enclosure_status(atadev, 416 &iocmd->u.enclosure.fan, 417 &iocmd->u.enclosure.temp, 418 &iocmd->u.enclosure.v05, 419 &iocmd->u.enclosure.v12); 420 } 421 422#ifdef DEV_ATADISK 423 case ATARAIDREBUILD: 424 return ata_raid_rebuild(iocmd->channel); 425 426 case ATARAIDCREATE: 427 return ata_raid_create(&iocmd->u.raid_setup); 428 429 case ATARAIDDELETE: 430 return ata_raid_delete(iocmd->channel); 431 432 case ATARAIDSTATUS: 433 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 434#endif 435#if DEV_ATAPIALL 436 case ATAPICMD: { 437 struct ata_device *atadev; 438 caddr_t buf; 439 440 if (!device || !(ch = device_get_softc(device))) 441 return ENXIO; 442 443 if (!(atadev = &ch->device[iocmd->device]) || 444 !(ch->devices & (iocmd->device == MASTER ? 445 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))) 446 return ENODEV; 447 448 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT))) 449 return ENOMEM; 450 451 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) { 452 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count); 453 if (error) { 454 free(buf, M_ATA); 455 return error; 456 } 457 } 458 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb, 459 buf, iocmd->u.atapi.count, 460 (iocmd->u.atapi.flags == ATAPI_CMD_READ ? 461 ATPR_F_READ : 0) | ATPR_F_QUIET, 462 iocmd->u.atapi.timeout, NULL, NULL); 463 if (error) { 464 iocmd->u.atapi.error = error; 465 bcopy(&atadev->result, iocmd->u.atapi.sense_data, 466 sizeof(struct atapi_reqsense)); 467 error = 0; 468 } 469 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ) 470 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count); 471 472 free(buf, M_ATA); 473 return error; 474 } 475#endif 476 default: 477 break; 478 } 479 return ENOTTY; 480} 481 482static int 483ata_getparam(struct ata_device *atadev, u_int8_t command) 484{ 485 struct ata_params *ata_parm; 486 int retry = 0; 487 488 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) { 489 ata_prtdev(atadev, "malloc for identify data failed\n"); 490 return -1; 491 } 492 493 /* apparently some devices needs this repeated */ 494 do { 495 if (ata_command(atadev, command, 0, 0, 0, 496 dumping ? ATA_WAIT_READY : ATA_WAIT_INTR)) { 497 ata_prtdev(atadev, "%s identify failed\n", 498 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 499 free(ata_parm, M_ATA); 500 return -1; 501 } 502 if (retry++ > 4) { 503 ata_prtdev(atadev, "%s identify retries exceeded\n", 504 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 505 free(ata_parm, M_ATA); 506 return -1; 507 } 508 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ? 509 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ)))); 510 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm, 511 sizeof(struct ata_params)/sizeof(int16_t)); 512 513 if (command == ATA_C_ATA_IDENTIFY || 514 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 515 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') || 516 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i'))) 517 bswap(ata_parm->model, sizeof(ata_parm->model)); 518 btrim(ata_parm->model, sizeof(ata_parm->model)); 519 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 520 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 521 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 522 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 523 bswap(ata_parm->serial, sizeof(ata_parm->serial)); 524 btrim(ata_parm->serial, sizeof(ata_parm->serial)); 525 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial)); 526 atadev->param = ata_parm; 527 return 0; 528} 529 530static void 531ata_boot_attach(void) 532{ 533 struct ata_channel *ch; 534 int ctlr; 535 536 /* 537 * run through all ata devices and look for real ATA & ATAPI devices 538 * using the hints we found in the early probe, this avoids some of 539 * the delays probing of non-exsistent devices can cause. 540 */ 541 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 542 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 543 continue; 544 ch->locking(ch, ATA_LF_LOCK); 545 if (ch->devices & ATA_ATA_SLAVE) 546 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 547 ch->devices &= ~ATA_ATA_SLAVE; 548 if (ch->devices & ATA_ATAPI_SLAVE) 549 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 550 ch->devices &= ~ATA_ATAPI_SLAVE; 551 if (ch->devices & ATA_ATA_MASTER) 552 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 553 ch->devices &= ~ATA_ATA_MASTER; 554 if (ch->devices & ATA_ATAPI_MASTER) 555 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 556 ch->devices &= ~ATA_ATAPI_MASTER; 557 ch->locking(ch, ATA_LF_UNLOCK); 558 } 559#ifdef DEV_ATADISK 560 /* now we know whats there, do the real attach, first the ATA disks */ 561 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 562 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 563 continue; 564 ch->locking(ch, ATA_LF_LOCK); 565 if (ch->devices & ATA_ATA_MASTER) 566 ad_attach(&ch->device[MASTER]); 567 if (ch->devices & ATA_ATA_SLAVE) 568 ad_attach(&ch->device[SLAVE]); 569 ch->locking(ch, ATA_LF_UNLOCK); 570 } 571#endif 572 /* then the atapi devices */ 573 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 574 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 575 continue; 576 ch->locking(ch, ATA_LF_LOCK); 577#if DEV_ATAPIALL 578 if (ch->devices & ATA_ATAPI_MASTER) 579 atapi_attach(&ch->device[MASTER]); 580 if (ch->devices & ATA_ATAPI_SLAVE) 581 atapi_attach(&ch->device[SLAVE]); 582#endif 583#ifdef DEV_ATAPICAM 584 atapi_cam_attach_bus(ch); 585#endif 586 ch->locking(ch, ATA_LF_UNLOCK); 587 } 588 if (ata_delayed_attach) { 589 config_intrhook_disestablish(ata_delayed_attach); 590 free(ata_delayed_attach, M_TEMP); 591 ata_delayed_attach = NULL; 592 } 593#ifdef DEV_ATADISK 594 ata_raid_attach(); 595#endif 596} 597 598static void 599ata_intr(void *data) 600{ 601 struct ata_channel *ch = (struct ata_channel *)data; 602 603 /* if device is busy it didn't interrupt */ 604 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) { 605 DELAY(100); 606 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ)) 607 return; 608 } 609 610 /* clear interrupt and get status */ 611 ch->status = ATA_INB(ch->r_io, ATA_STATUS); 612 613 if (ch->status & ATA_S_ERROR) 614 ch->error = ATA_INB(ch->r_io, ATA_ERROR); 615 616 /* find & call the responsible driver to process this interrupt */ 617 switch (ch->active) { 618#ifdef DEV_ATADISK 619 case ATA_ACTIVE_ATA: 620 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES) 621 return; 622 break; 623#endif 624#if DEV_ATAPIALL 625 case ATA_ACTIVE_ATAPI: 626 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES) 627 return; 628 break; 629#endif 630 default: 631 if (ch->active & ATA_WAIT_INTR) 632 wakeup(ch); 633 } 634 635 if (ch->active & ATA_CONTROL) { 636 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 637 return; 638 } 639 640 if (ch->active & ATA_WAIT_INTR) { 641 ATA_UNLOCK_CH(ch); 642 return; 643 } 644 645 if ((ch->flags & ATA_QUEUED) && 646 ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_SERVICE) { 647 ATA_FORCELOCK_CH(ch, ATA_ACTIVE); 648 if (ata_service(ch) == ATA_OP_CONTINUES) 649 return; 650 } 651 ch->running = NULL; 652 ATA_UNLOCK_CH(ch); 653 ch->locking(ch, ATA_LF_UNLOCK); 654 ata_start(ch); 655 return; 656} 657 658void 659ata_start(struct ata_channel *ch) 660{ 661#ifdef DEV_ATADISK 662 struct ad_request *ad_request; 663#endif 664#if DEV_ATAPIALL 665 struct atapi_request *atapi_request; 666#endif 667 int s; 668 669 ch->locking(ch, ATA_LF_LOCK); 670 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) 671 return; 672 673 s = splbio(); 674#ifdef DEV_ATADISK 675 /* find & call the responsible driver if anything on the ATA queue */ 676 if (TAILQ_EMPTY(&ch->ata_queue)) { 677 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver) 678 ad_start(&ch->device[MASTER]); 679 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 680 ad_start(&ch->device[SLAVE]); 681 } 682 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) { 683 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain); 684 ch->active = ATA_ACTIVE_ATA; 685 ch->running = ad_request; 686 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) { 687 splx(s); 688 return; 689 } 690 } 691 692#endif 693#if DEV_ATAPIALL 694 /* find & call the responsible driver if anything on the ATAPI queue */ 695 if (TAILQ_EMPTY(&ch->atapi_queue)) { 696 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) 697 atapi_start(&ch->device[MASTER]); 698 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) 699 atapi_start(&ch->device[SLAVE]); 700 } 701 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) { 702 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain); 703 ch->active = ATA_ACTIVE_ATAPI; 704 ch->running = atapi_request; 705 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) { 706 splx(s); 707 return; 708 } 709 } 710#endif 711 ATA_UNLOCK_CH(ch); 712 ch->locking(ch, ATA_LF_UNLOCK); 713 splx(s); 714} 715 716void 717ata_reset(struct ata_channel *ch) 718{ 719 u_int8_t lsb, msb, ostat0, ostat1; 720 u_int8_t stat0 = 0, stat1 = 0; 721 int mask = 0, timeout; 722 723 /* do we have any signs of ATA/ATAPI HW being present ? */ 724 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 725 DELAY(10); 726 ostat0 = ATA_INB(ch->r_io, ATA_STATUS); 727 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) { 728 stat0 = ATA_S_BUSY; 729 mask |= 0x01; 730 } 731 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 732 DELAY(10); 733 ostat1 = ATA_INB(ch->r_io, ATA_STATUS); 734 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) { 735 stat1 = ATA_S_BUSY; 736 mask |= 0x02; 737 } 738 739 ch->devices = 0; 740 if (!mask) 741 return; 742 743 /* in some setups we dont want to test for a slave */ 744 if (ch->flags & ATA_NO_SLAVE) { 745 stat1 = 0x0; 746 mask &= ~0x02; 747 } 748 749 if (bootverbose) 750 ata_printf(ch, -1, "pre reset mask=%02x ostat0=%02x ostat2=%02x\n", 751 mask, ostat0, ostat1); 752 753 /* reset channel */ 754 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 755 DELAY(10); 756 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 757 DELAY(10000); 758 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS); 759 DELAY(100000); 760 ATA_INB(ch->r_io, ATA_ERROR); 761 762 /* wait for BUSY to go inactive */ 763 for (timeout = 0; timeout < 310000; timeout++) { 764 if (stat0 & ATA_S_BUSY) { 765 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 766 DELAY(10); 767 768 /* check for ATAPI signature while its still there */ 769 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 770 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 771 stat0 = ATA_INB(ch->r_io, ATA_STATUS); 772 if (!(stat0 & ATA_S_BUSY)) { 773 if (bootverbose) 774 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb); 775 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 776 ch->devices |= ATA_ATAPI_MASTER; 777 } 778 } 779 if (stat1 & ATA_S_BUSY) { 780 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 781 DELAY(10); 782 783 /* check for ATAPI signature while its still there */ 784 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 785 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 786 stat1 = ATA_INB(ch->r_io, ATA_STATUS); 787 if (!(stat1 & ATA_S_BUSY)) { 788 if (bootverbose) 789 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb); 790 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 791 ch->devices |= ATA_ATAPI_SLAVE; 792 } 793 } 794 if (mask == 0x01) /* wait for master only */ 795 if (!(stat0 & ATA_S_BUSY)) 796 break; 797 if (mask == 0x02) /* wait for slave only */ 798 if (!(stat1 & ATA_S_BUSY)) 799 break; 800 if (mask == 0x03) /* wait for both master & slave */ 801 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 802 break; 803 DELAY(100); 804 } 805 DELAY(10); 806 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 807 808 if (stat0 & ATA_S_BUSY) 809 mask &= ~0x01; 810 if (stat1 & ATA_S_BUSY) 811 mask &= ~0x02; 812 if (bootverbose) 813 ata_printf(ch, -1, "after reset mask=%02x stat0=%02x stat1=%02x\n", 814 mask, stat0, stat1); 815 if (!mask) 816 return; 817 818 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) { 819 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 820 DELAY(10); 821 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 822 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 823 lsb = ATA_INB(ch->r_io, ATA_ERROR); 824 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 825 if (bootverbose) 826 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb); 827 if (lsb != 0x58 && msb == 0xa5) 828 ch->devices |= ATA_ATA_MASTER; 829 } 830 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) { 831 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 832 DELAY(10); 833 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 834 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 835 lsb = ATA_INB(ch->r_io, ATA_ERROR); 836 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 837 if (bootverbose) 838 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb); 839 if (lsb != 0x58 && msb == 0xa5) 840 ch->devices |= ATA_ATA_SLAVE; 841 } 842 if (bootverbose) 843 ata_printf(ch, -1, "devices=%02x\n", ch->devices); 844} 845 846int 847ata_reinit(struct ata_channel *ch) 848{ 849 int devices, misdev, newdev; 850 851 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 852 853 if (!ch->r_io || !ch->r_altio || !ch->r_irq) { 854 ATA_UNLOCK_CH(ch); 855 return ENXIO; 856 } 857 858 ch->running = NULL; 859 devices = ch->devices; 860 ata_printf(ch, -1, "resetting devices ..\n"); 861 ata_reset(ch); 862 863 if ((misdev = devices & ~ch->devices)) { 864#ifdef DEV_ATADISK 865 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver) 866 ad_detach(&ch->device[MASTER], 0); 867 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 868 ad_detach(&ch->device[SLAVE], 0); 869#endif 870#if DEV_ATAPIALL 871 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 872 atapi_detach(&ch->device[MASTER]); 873 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 874 atapi_detach(&ch->device[SLAVE]); 875#endif 876 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 877 if (ch->device[MASTER].param) 878 free(ch->device[MASTER].param, M_ATA); 879 ch->device[MASTER].param = NULL; 880 } 881 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 882 if (ch->device[SLAVE].param) 883 free(ch->device[SLAVE].param, M_ATA); 884 ch->device[SLAVE].param = NULL; 885 } 886 } 887 if ((newdev = ~devices & ch->devices)) { 888 if (newdev & ATA_ATA_MASTER) 889 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 890 ch->devices &= ~ATA_ATA_MASTER; 891 if (newdev & ATA_ATA_SLAVE) 892 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 893 ch->devices &= ~ATA_ATA_SLAVE; 894 if (newdev & ATA_ATAPI_MASTER) 895 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 896 ch->devices &= ~ATA_ATAPI_MASTER; 897 if (newdev & ATA_ATAPI_SLAVE) 898 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 899 ch->devices &= ~ATA_ATAPI_SLAVE; 900 } 901 newdev = ~devices & ch->devices; 902#ifdef DEV_ATADISK 903 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver) 904 ad_attach(&ch->device[SLAVE]); 905 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) { 906 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY); 907 ad_reinit(&ch->device[SLAVE]); 908 } 909 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver) 910 ad_attach(&ch->device[MASTER]); 911 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) { 912 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY); 913 ad_reinit(&ch->device[MASTER]); 914 } 915#endif 916#if DEV_ATAPIALL 917 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver) 918 atapi_attach(&ch->device[SLAVE]); 919 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) { 920 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY); 921 atapi_reinit(&ch->device[SLAVE]); 922 } 923 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver) 924 atapi_attach(&ch->device[MASTER]); 925 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) { 926 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY); 927 atapi_reinit(&ch->device[MASTER]); 928 } 929#endif 930#ifdef DEV_ATAPICAM 931 atapi_cam_reinit_bus(ch); 932#endif 933 printf("done\n"); 934 ATA_UNLOCK_CH(ch); 935 ata_start(ch); 936 return 0; 937} 938 939static int 940ata_service(struct ata_channel *ch) 941{ 942 /* do we have a SERVICE request from the drive ? */ 943 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) { 944#if 0 /* XXX */ 945 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 946 ch->dma->status(ch) | ATA_BMSTAT_INTERRUPT); 947#endif 948#ifdef DEV_ATADISK 949 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 950 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver) 951 return ad_service((struct ad_softc *) 952 ch->device[MASTER].driver, 0); 953 } 954 else { 955 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 956 return ad_service((struct ad_softc *) 957 ch->device[SLAVE].driver, 0); 958 } 959#endif 960 } 961 return ATA_OP_FINISHED; 962} 963 964int 965ata_wait(struct ata_device *atadev, u_int8_t mask) 966{ 967 int timeout = 0; 968 969 DELAY(1); 970 while (timeout < 5000000) { /* timeout 5 secs */ 971 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 972 973 /* if drive fails status, reselect the drive just to be sure */ 974 if (atadev->channel->status == 0xff) { 975 ata_prtdev(atadev, "no status, reselecting device\n"); 976 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit); 977 DELAY(10); 978 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS); 979 if (atadev->channel->status == 0xff) 980 return -1; 981 } 982 983 /* are we done ? */ 984 if (!(atadev->channel->status & ATA_S_BUSY)) 985 break; 986 987 if (timeout > 1000) { 988 timeout += 1000; 989 DELAY(1000); 990 } 991 else { 992 timeout += 10; 993 DELAY(10); 994 } 995 } 996 if (atadev->channel->status & ATA_S_ERROR) 997 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR); 998 if (timeout >= 5000000) 999 return -1; 1000 if (!mask) 1001 return (atadev->channel->status & ATA_S_ERROR); 1002 1003 /* Wait 50 msec for bits wanted. */ 1004 timeout = 5000; 1005 while (timeout--) { 1006 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 1007 if ((atadev->channel->status & mask) == mask) { 1008 if (atadev->channel->status & ATA_S_ERROR) 1009 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR); 1010 return (atadev->channel->status & ATA_S_ERROR); 1011 } 1012 DELAY (10); 1013 } 1014 return -1; 1015} 1016 1017int 1018ata_command(struct ata_device *atadev, u_int8_t command, 1019 u_int64_t lba, u_int16_t count, u_int16_t feature, int flags) 1020{ 1021 int error = 0; 1022#ifdef ATA_DEBUG 1023 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, " 1024 "lba=%jd, count=%d, feature=%d, flags=%02x\n", 1025 rman_get_start(atadev->channel->r_io), 1026 command, (intmax_t)lba, count, feature, flags); 1027#endif 1028 1029 /* select device */ 1030 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1031 1032 /* disable interrupt from device */ 1033 if (atadev->channel->flags & ATA_QUEUED) 1034 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 1035 1036 /* ready to issue command ? */ 1037 if (ata_wait(atadev, 0) < 0) { 1038 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n", 1039 command, atadev->channel->status, atadev->channel->error); 1040 return -1; 1041 } 1042 1043 /* only use 48bit addressing if needed because of the overhead */ 1044 if ((lba > 268435455 || count > 256) && atadev->param && 1045 atadev->param->support.address48) { 1046 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff); 1047 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1048 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff); 1049 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff); 1050 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff); 1051 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1052 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff); 1053 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1054 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff); 1055 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1056 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit); 1057 1058 /* translate command into 48bit version */ 1059 switch (command) { 1060 case ATA_C_READ: 1061 command = ATA_C_READ48; break; 1062 case ATA_C_READ_MUL: 1063 command = ATA_C_READ_MUL48; break; 1064 case ATA_C_READ_DMA: 1065 command = ATA_C_READ_DMA48; break; 1066 case ATA_C_READ_DMA_QUEUED: 1067 command = ATA_C_READ_DMA_QUEUED48; break; 1068 case ATA_C_WRITE: 1069 command = ATA_C_WRITE48; break; 1070 case ATA_C_WRITE_MUL: 1071 command = ATA_C_WRITE_MUL48; break; 1072 case ATA_C_WRITE_DMA: 1073 command = ATA_C_WRITE_DMA48; break; 1074 case ATA_C_WRITE_DMA_QUEUED: 1075 command = ATA_C_WRITE_DMA_QUEUED48; break; 1076 case ATA_C_FLUSHCACHE: 1077 command = ATA_C_FLUSHCACHE48; break; 1078 default: 1079 ata_prtdev(atadev, "can't translate cmd to 48bit version\n"); 1080 return -1; 1081 } 1082 atadev->channel->flags |= ATA_48BIT_ACTIVE; 1083 } 1084 else { 1085 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1086 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count); 1087 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1088 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1089 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1090 if (atadev->flags & ATA_D_USE_CHS) 1091 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1092 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf)); 1093 else 1094 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1095 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf)); 1096 atadev->channel->flags &= ~ATA_48BIT_ACTIVE; 1097 } 1098 1099 switch (flags & ATA_WAIT_MASK) { 1100 case ATA_IMMEDIATE: 1101 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1102 1103 /* enable interrupt */ 1104 if (atadev->channel->flags & ATA_QUEUED) 1105 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1106 break; 1107 1108 case ATA_WAIT_INTR: 1109 atadev->channel->active |= ATA_WAIT_INTR; 1110 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1111 1112 /* enable interrupt */ 1113 if (atadev->channel->flags & ATA_QUEUED) 1114 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1115 1116 if (tsleep(atadev->channel, PRIBIO, "atacmd", 10 * hz)) { 1117 ata_prtdev(atadev, "timeout waiting for interrupt\n"); 1118 atadev->channel->active &= ~ATA_WAIT_INTR; 1119 error = -1; 1120 } 1121 break; 1122 1123 case ATA_WAIT_READY: 1124 atadev->channel->active |= ATA_WAIT_READY; 1125 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1126 if (ata_wait(atadev, ATA_S_READY) < 0) { 1127 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n", 1128 command, atadev->channel->status,atadev->channel->error); 1129 error = -1; 1130 } 1131 atadev->channel->active &= ~ATA_WAIT_READY; 1132 break; 1133 } 1134 return error; 1135} 1136 1137static void 1138ata_enclosure_start(struct ata_device *atadev) 1139{ 1140 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1141 DELAY(1); 1142 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1143 DELAY(1); 1144 ATA_INB(atadev->channel->r_io, ATA_CMD); 1145 DELAY(1); 1146 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1147 DELAY(1); 1148 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1149 DELAY(1); 1150 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1151 DELAY(1); 1152 ATA_INB(atadev->channel->r_io, ATA_COUNT); 1153 DELAY(1); 1154 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1155 DELAY(1); 1156} 1157 1158static void 1159ata_enclosure_end(struct ata_device *atadev) 1160{ 1161 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1162 DELAY(1); 1163} 1164 1165static void 1166ata_enclosure_chip_start(struct ata_device *atadev) 1167{ 1168 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1169 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1170 DELAY(25); 1171 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1172} 1173 1174static void 1175ata_enclosure_chip_end(struct ata_device *atadev) 1176{ 1177 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1178 DELAY(64); 1179 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1180 DELAY(25); 1181 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1182 DELAY(64); 1183} 1184 1185static u_int8_t 1186ata_enclosure_chip_rdbit(struct ata_device *atadev) 1187{ 1188 u_int8_t val; 1189 1190 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0); 1191 DELAY(64); 1192 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02); 1193 DELAY(25); 1194 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01; 1195 DELAY(38); 1196 return val; 1197} 1198 1199static void 1200ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data) 1201{ 1202 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01)); 1203 DELAY(64); 1204 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01)); 1205 DELAY(64); 1206} 1207 1208static u_int8_t 1209ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val) 1210{ 1211 int i; 1212 1213 if (rw) { 1214 for (i = 0; i < 8; i++) 1215 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0); 1216 } 1217 else { 1218 for (i = 0; i < 8; i++) 1219 val = (val << 1) | ata_enclosure_chip_rdbit(atadev); 1220 } 1221 ata_enclosure_chip_wrbit(atadev, 0); 1222 return val; 1223} 1224 1225static u_int8_t 1226ata_enclosure_sensor(struct ata_device *atadev, 1227 int rw, u_int8_t idx, u_int8_t data) 1228{ 1229 ata_enclosure_start(atadev); 1230 ata_enclosure_chip_start(atadev); 1231 ata_enclosure_chip_rw(atadev, 1, 0x5a); 1232 ata_enclosure_chip_rw(atadev, 1, idx); 1233 if (rw) { 1234 ata_enclosure_chip_rw(atadev, 1, data); 1235 } 1236 else { 1237 ata_enclosure_chip_end(atadev); 1238 ata_enclosure_chip_start(atadev); 1239 ata_enclosure_chip_rw(atadev, 1, 0x5b); 1240 data = ata_enclosure_chip_rw(atadev, 0, 0); 1241 } 1242 ata_enclosure_chip_end(atadev); 1243 ata_enclosure_end(atadev); 1244 return data; 1245} 1246 1247static int 1248ata_enclosure_status(struct ata_device *atadev, 1249 int *fan, int *temp, int *v05, int *v12) 1250{ 1251 u_int8_t id1, id2, cnt, div; 1252 1253 if (atadev->flags & ATA_D_ENC_PRESENT) { 1254 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1255 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1256 ata_enclosure_sensor(atadev, 1, 0x4e, 0); 1257 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1258 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80); 1259 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1260 if (id1 != 0xa3 || id2 != 0x5c) 1261 return ENXIO; 1262 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0) & 0x20) >> 3)+ 1263 ((ata_enclosure_sensor(atadev, 0, 0x47, 0) & 0x30) >> 4)+1); 1264 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0); 1265 if (cnt == 0xff) 1266 *fan = 0; 1267 else 1268 *fan = 1350000 / cnt / div; 1269 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01); 1270 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) + 1271 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0); 1272 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27; 1273 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61; 1274 ATA_UNLOCK_CH(atadev->channel); 1275 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1276 return 0; 1277 } 1278 return ENXIO; 1279} 1280 1281void 1282ata_enclosure_print(struct ata_device *atadev) 1283{ 1284 u_int8_t id, st; 1285 int fan, temp, v05, v12; 1286 1287 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1288 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1289 ata_enclosure_start(atadev); 1290 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1291 DELAY(1); 1292 st = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1293 DELAY(1); 1294 ata_enclosure_end(atadev); 1295 ATA_UNLOCK_CH(atadev->channel); 1296 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1297 1298 switch (id & 0x93) { 1299 case 0x00: 1300 ata_prtdev(atadev, "Universal enclosure"); 1301 break; 1302 case 0x01: 1303 ata_prtdev(atadev, "FastSwap enclosure"); 1304 break; 1305 case 0x10: 1306 case 0x11: 1307 ata_prtdev(atadev, "SuperSwap enclosure"); 1308 break; 1309 default: 1310 atadev->flags &= ~ATA_D_ENC_PRESENT; 1311 return; 1312 } 1313 atadev->flags |= ATA_D_ENC_PRESENT; 1314 1315 ata_enclosure_leds(atadev, ATA_LED_GREEN); 1316 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12)) 1317 printf(" detected\n"); 1318 else 1319 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n", 1320 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000); 1321} 1322 1323void 1324ata_enclosure_leds(struct ata_device *atadev, u_int8_t color) 1325{ 1326 if (atadev->flags & ATA_D_ENC_PRESENT) { 1327 u_int8_t reg; 1328 1329 ata_enclosure_start(atadev); 1330 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1331 DELAY(1); 1332 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, 1333 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK)); 1334 DELAY(1); 1335 ata_enclosure_end(atadev); 1336 } 1337} 1338 1339static void 1340ata_change_mode(struct ata_device *atadev, int mode) 1341{ 1342 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE); 1343 atadev->setmode(atadev, mode); 1344 ATA_UNLOCK_CH(atadev->channel); 1345 ata_start(atadev->channel); 1346} 1347 1348int 1349ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 1350{ 1351 va_list ap; 1352 int ret; 1353 1354 if (device == -1) 1355 ret = printf("ata%d: ", device_get_unit(ch->dev)); 1356 else { 1357 if (ch->device[ATA_DEV(device)].name) 1358 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 1359 else 1360 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 1361 (device == ATA_MASTER) ? "master" : "slave"); 1362 } 1363 va_start(ap, fmt); 1364 ret += vprintf(fmt, ap); 1365 va_end(ap); 1366 return ret; 1367} 1368 1369int 1370ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 1371{ 1372 va_list ap; 1373 int ret; 1374 1375 if (atadev->name) 1376 ret = printf("%s: ", atadev->name); 1377 else 1378 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 1379 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 1380 va_start(ap, fmt); 1381 ret += vprintf(fmt, ap); 1382 va_end(ap); 1383 return ret; 1384} 1385 1386void 1387ata_set_name(struct ata_device *atadev, char *name, int lun) 1388{ 1389 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 1390 if (atadev->name) 1391 sprintf(atadev->name, "%s%d", name, lun); 1392} 1393 1394void 1395ata_free_name(struct ata_device *atadev) 1396{ 1397 if (atadev->name) 1398 free(atadev->name, M_ATA); 1399 atadev->name = NULL; 1400} 1401 1402int 1403ata_get_lun(u_int32_t *map) 1404{ 1405 int lun = ffs(~*map) - 1; 1406 1407 *map |= (1 << lun); 1408 return lun; 1409} 1410 1411int 1412ata_test_lun(u_int32_t *map, int lun) 1413{ 1414 return (*map & (1 << lun)); 1415} 1416 1417void 1418ata_free_lun(u_int32_t *map, int lun) 1419{ 1420 *map &= ~(1 << lun); 1421} 1422 1423char * 1424ata_mode2str(int mode) 1425{ 1426 switch (mode) { 1427 case ATA_PIO: return "BIOSPIO"; 1428 case ATA_PIO0: return "PIO0"; 1429 case ATA_PIO1: return "PIO1"; 1430 case ATA_PIO2: return "PIO2"; 1431 case ATA_PIO3: return "PIO3"; 1432 case ATA_PIO4: return "PIO4"; 1433 case ATA_DMA: return "BIOSDMA"; 1434 case ATA_WDMA0: return "WDMA0"; 1435 case ATA_WDMA1: return "WDMA1"; 1436 case ATA_WDMA2: return "WDMA2"; 1437 case ATA_UDMA0: return "UDMA16"; 1438 case ATA_UDMA1: return "UDMA25"; 1439 case ATA_UDMA2: return "UDMA33"; 1440 case ATA_UDMA3: return "UDMA40"; 1441 case ATA_UDMA4: return "UDMA66"; 1442 case ATA_UDMA5: return "UDMA100"; 1443 case ATA_UDMA6: return "UDMA133"; 1444 default: return "???"; 1445 } 1446} 1447 1448int 1449ata_pmode(struct ata_params *ap) 1450{ 1451 if (ap->atavalid & ATA_FLAG_64_70) { 1452 if (ap->apiomodes & 0x02) 1453 return ATA_PIO4; 1454 if (ap->apiomodes & 0x01) 1455 return ATA_PIO3; 1456 } 1457 if (ap->retired_piomode == 2) 1458 return ATA_PIO2; 1459 if (ap->retired_piomode == 1) 1460 return ATA_PIO1; 1461 if (ap->retired_piomode == 0) 1462 return ATA_PIO0; 1463 if (ap->support_dma) 1464 return ATA_PIO4; 1465 return ATA_PIO0; 1466} 1467 1468int 1469ata_wmode(struct ata_params *ap) 1470{ 1471 if (ap->mwdmamodes & 0x04) 1472 return ATA_WDMA2; 1473 if (ap->mwdmamodes & 0x02) 1474 return ATA_WDMA1; 1475 if (ap->mwdmamodes & 0x01) 1476 return ATA_WDMA0; 1477 if (ap->support_dma) 1478 return ATA_WDMA2; 1479 return -1; 1480} 1481 1482int 1483ata_umode(struct ata_params *ap) 1484{ 1485 if (ap->atavalid & ATA_FLAG_88) { 1486 if (ap->udmamodes & 0x40) 1487 return ATA_UDMA6; 1488 if (ap->udmamodes & 0x20) 1489 return ATA_UDMA5; 1490 if (ap->udmamodes & 0x10) 1491 return ATA_UDMA4; 1492 if (ap->udmamodes & 0x08) 1493 return ATA_UDMA3; 1494 if (ap->udmamodes & 0x04) 1495 return ATA_UDMA2; 1496 if (ap->udmamodes & 0x02) 1497 return ATA_UDMA1; 1498 if (ap->udmamodes & 0x01) 1499 return ATA_UDMA0; 1500 } 1501 return -1; 1502} 1503 1504int 1505ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 1506{ 1507 if (maxmode && mode > maxmode) 1508 mode = maxmode; 1509 1510 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 1511 return min(mode, ata_umode(atadev->param)); 1512 1513 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 1514 return min(mode, ata_wmode(atadev->param)); 1515 1516 if (mode > ata_pmode(atadev->param)) 1517 return min(mode, ata_pmode(atadev->param)); 1518 1519 return mode; 1520} 1521 1522static void 1523bswap(int8_t *buf, int len) 1524{ 1525 u_int16_t *ptr = (u_int16_t*)(buf + len); 1526 1527 while (--ptr >= (u_int16_t*)buf) 1528 *ptr = ntohs(*ptr); 1529} 1530 1531static void 1532btrim(int8_t *buf, int len) 1533{ 1534 int8_t *ptr; 1535 1536 for (ptr = buf; ptr < buf+len; ++ptr) 1537 if (!*ptr) 1538 *ptr = ' '; 1539 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1540 *ptr = 0; 1541} 1542 1543static void 1544bpack(int8_t *src, int8_t *dst, int len) 1545{ 1546 int i, j, blank; 1547 1548 for (i = j = blank = 0 ; i < len; i++) { 1549 if (blank && src[i] == ' ') continue; 1550 if (blank && src[i] != ' ') { 1551 dst[j++] = src[i]; 1552 blank = 0; 1553 continue; 1554 } 1555 if (src[i] == ' ') { 1556 blank = 1; 1557 if (i == 0) 1558 continue; 1559 } 1560 dst[j++] = src[i]; 1561 } 1562 if (j < len) 1563 dst[j] = 0x00; 1564} 1565 1566static void 1567ata_init(void) 1568{ 1569 /* register controlling device */ 1570 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1571 1572 /* register boot attach to be run when interrupts are enabled */ 1573 if (!(ata_delayed_attach = (struct intr_config_hook *) 1574 malloc(sizeof(struct intr_config_hook), 1575 M_TEMP, M_NOWAIT | M_ZERO))) { 1576 printf("ata: malloc of delayed attach hook failed\n"); 1577 return; 1578 } 1579 1580 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1581 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1582 printf("ata: config_intrhook_establish failed\n"); 1583 free(ata_delayed_attach, M_TEMP); 1584 } 1585} 1586SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
| 65}; 66 67/* prototypes */ 68static void ata_boot_attach(void); 69static void ata_intr(void *); 70static int ata_getparam(struct ata_device *, u_int8_t); 71static int ata_service(struct ata_channel *); 72static void bswap(int8_t *, int); 73static void btrim(int8_t *, int); 74static void bpack(int8_t *, int8_t *, int); 75static void ata_change_mode(struct ata_device *, int); 76static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t); 77static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *); 78 79/* sysctl vars */ 80SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 81 82/* global vars */ 83struct intr_config_hook *ata_delayed_attach = NULL; 84devclass_t ata_devclass; 85 86/* local vars */ 87static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 88 89/* misc defines */ 90#define DEV_ATAPIALL defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || \ 91 defined(DEV_ATAPIST) || defined(DEV_ATAPICAM) 92 93int 94ata_probe(device_t dev) 95{ 96 struct ata_channel *ch; 97 int rid; 98 99 if (!dev || !(ch = device_get_softc(dev))) 100 return ENXIO; 101 102 if (ch->r_io || ch->r_altio || ch->r_irq) 103 return EEXIST; 104 105 /* initialize the softc basics */ 106 ch->active = ATA_IDLE; 107 ch->dev = dev; 108 109 rid = ATA_IOADDR_RID; 110 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 111 ATA_IOSIZE, RF_ACTIVE); 112 if (!ch->r_io) 113 goto failure; 114 115 rid = ATA_ALTADDR_RID; 116 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 117 ATA_ALTIOSIZE, RF_ACTIVE); 118 if (!ch->r_altio) 119 goto failure; 120 121 rid = ATA_BMADDR_RID; 122 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 123 ATA_BMIOSIZE, RF_ACTIVE); 124 if (bootverbose) 125 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 126 (int)rman_get_start(ch->r_io), 127 (int)rman_get_start(ch->r_altio), 128 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0); 129 130 ch->locking(ch, ATA_LF_LOCK); 131 ata_reset(ch); 132 ch->locking(ch, ATA_LF_UNLOCK); 133 134 ch->device[MASTER].channel = ch; 135 ch->device[MASTER].unit = ATA_MASTER; 136 ch->device[MASTER].mode = ATA_PIO; 137 ch->device[SLAVE].channel = ch; 138 ch->device[SLAVE].unit = ATA_SLAVE; 139 ch->device[SLAVE].mode = ATA_PIO; 140 TAILQ_INIT(&ch->ata_queue); 141 TAILQ_INIT(&ch->atapi_queue); 142 return 0; 143 144failure: 145 if (ch->r_io) 146 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 147 if (ch->r_altio) 148 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 149 if (ch->r_bmio) 150 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 151 if (bootverbose) 152 ata_printf(ch, -1, "probe allocation failed\n"); 153 return ENXIO; 154} 155 156int 157ata_attach(device_t dev) 158{ 159 struct ata_channel *ch; 160 int error, rid; 161 162 if (!dev || !(ch = device_get_softc(dev))) 163 return ENXIO; 164 165 rid = ATA_IRQ_RID; 166 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 167 RF_SHAREABLE | RF_ACTIVE); 168 if (!ch->r_irq) { 169 ata_printf(ch, -1, "unable to allocate interrupt\n"); 170 return ENXIO; 171 } 172 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY, 173 ata_intr, ch, &ch->ih))) { 174 ata_printf(ch, -1, "unable to setup interrupt\n"); 175 return error; 176 } 177 178 if (ch->dma) 179 ch->dma->create(ch); 180 181 /* 182 * do not attach devices if we are in early boot, this is done later 183 * when interrupts are enabled by a hook into the boot process. 184 * otherwise attach what the probe has found in ch->devices. 185 */ 186 if (!ata_delayed_attach) { 187 ch->locking(ch, ATA_LF_LOCK); 188 if (ch->devices & ATA_ATA_SLAVE) 189 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 190 ch->devices &= ~ATA_ATA_SLAVE; 191 if (ch->devices & ATA_ATAPI_SLAVE) 192 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 193 ch->devices &= ~ATA_ATAPI_SLAVE; 194 if (ch->devices & ATA_ATA_MASTER) 195 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 196 ch->devices &= ~ATA_ATA_MASTER; 197 if (ch->devices & ATA_ATAPI_MASTER) 198 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 199 ch->devices &= ~ATA_ATAPI_MASTER; 200#ifdef DEV_ATADISK 201 if (ch->devices & ATA_ATA_MASTER) 202 ad_attach(&ch->device[MASTER]); 203 if (ch->devices & ATA_ATA_SLAVE) 204 ad_attach(&ch->device[SLAVE]); 205#endif 206#if DEV_ATAPIALL 207 if (ch->devices & ATA_ATAPI_MASTER) 208 atapi_attach(&ch->device[MASTER]); 209 if (ch->devices & ATA_ATAPI_SLAVE) 210 atapi_attach(&ch->device[SLAVE]); 211#endif 212#ifdef DEV_ATAPICAM 213 atapi_cam_attach_bus(ch); 214#endif 215 ch->locking(ch, ATA_LF_UNLOCK); 216 } 217 return 0; 218} 219 220int 221ata_detach(device_t dev) 222{ 223 struct ata_channel *ch; 224 int s; 225 226 if (!dev || !(ch = device_get_softc(dev)) || 227 !ch->r_io || !ch->r_altio || !ch->r_irq) 228 return ENXIO; 229 230 /* make sure channel is not busy */ 231 ch->locking(ch, ATA_LF_LOCK); 232 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL); 233 234 s = splbio(); 235#ifdef DEV_ATADISK 236 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) 237 ad_detach(&ch->device[MASTER], 1); 238 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 239 ad_detach(&ch->device[SLAVE], 1); 240#endif 241#if DEV_ATAPIALL 242 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 243 atapi_detach(&ch->device[MASTER]); 244 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 245 atapi_detach(&ch->device[SLAVE]); 246#endif 247#ifdef DEV_ATAPICAM 248 atapi_cam_detach_bus(ch); 249#endif 250 splx(s); 251 252 if (ch->device[MASTER].param) { 253 free(ch->device[MASTER].param, M_ATA); 254 ch->device[MASTER].param = NULL; 255 } 256 if (ch->device[SLAVE].param) { 257 free(ch->device[SLAVE].param, M_ATA); 258 ch->device[SLAVE].param = NULL; 259 } 260 ch->device[MASTER].driver = NULL; 261 ch->device[SLAVE].driver = NULL; 262 ch->device[MASTER].mode = ATA_PIO; 263 ch->device[SLAVE].mode = ATA_PIO; 264 ch->devices = 0; 265 if (ch->dma) 266 ch->dma->destroy(ch); 267 268 bus_teardown_intr(dev, ch->r_irq, ch->ih); 269 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 270 if (ch->r_bmio) 271 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 272 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 273 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 274 ch->r_io = NULL; 275 ch->r_altio = NULL; 276 ch->r_bmio = NULL; 277 ch->r_irq = NULL; 278 ATA_UNLOCK_CH(ch); 279 ch->locking(ch, ATA_LF_UNLOCK); 280 return 0; 281} 282 283int 284ata_resume(device_t dev) 285{ 286 struct ata_channel *ch; 287 int error; 288 289 if (!dev || !(ch = device_get_softc(dev))) 290 return ENXIO; 291 292 ch->locking(ch, ATA_LF_LOCK); 293 error = ata_reinit(ch); 294 ch->locking(ch, ATA_LF_UNLOCK); 295 return error; 296} 297 298static int 299ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 300{ 301 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 302 struct ata_channel *ch; 303 device_t device = devclass_get_device(ata_devclass, iocmd->channel); 304 int error; 305 306 if (cmd != IOCATA) 307 return ENOTTY; 308 309 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE) 310 return ENXIO; 311 312 switch (iocmd->cmd) { 313 case ATAATTACH: 314 /* should enable channel HW on controller that can SOS XXX */ 315 error = ata_probe(device); 316 if (!error) 317 error = ata_attach(device); 318 return error; 319 320 case ATADETACH: 321 error = ata_detach(device); 322 /* should disable channel HW on controller that can SOS XXX */ 323 return error; 324 325 case ATAREINIT: 326 if (!device || !(ch = device_get_softc(device))) 327 return ENXIO; 328 ch->locking(ch, ATA_LF_LOCK); 329 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE); 330 error = ata_reinit(ch); 331 ch->locking(ch, ATA_LF_UNLOCK); 332 return error; 333 334 case ATAGMODE: 335 if (!device || !(ch = device_get_softc(device))) 336 return ENXIO; 337 338 if ((iocmd->device == MASTER || iocmd->device == -1) && 339 ch->device[MASTER].driver) 340 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 341 else 342 iocmd->u.mode.mode[MASTER] = -1; 343 344 if ((iocmd->device == SLAVE || iocmd->device == -1) && 345 ch->device[SLAVE].param) 346 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 347 else 348 iocmd->u.mode.mode[SLAVE] = -1; 349 return 0; 350 351 case ATASMODE: 352 if (!device || !(ch = device_get_softc(device))) 353 return ENXIO; 354 355 ch->locking(ch, ATA_LF_LOCK); 356 if ((iocmd->device == MASTER || iocmd->device == -1) && 357 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) { 358 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]); 359 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 360 } 361 else 362 iocmd->u.mode.mode[MASTER] = -1; 363 364 if ((iocmd->device == SLAVE || iocmd->device == -1) && 365 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) { 366 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]); 367 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 368 } 369 else 370 iocmd->u.mode.mode[SLAVE] = -1; 371 ch->locking(ch, ATA_LF_UNLOCK); 372 return 0; 373 374 case ATAGPARM: 375 if (!device || !(ch = device_get_softc(device))) 376 return ENXIO; 377 378 iocmd->u.param.type[MASTER] = 379 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 380 iocmd->u.param.type[SLAVE] = 381 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 382 383 if (ch->device[MASTER].name) 384 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 385 if (ch->device[SLAVE].name) 386 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 387 388 if (ch->device[MASTER].param) 389 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 390 sizeof(struct ata_params)); 391 if (ch->device[SLAVE].param) 392 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 393 sizeof(struct ata_params)); 394 return 0; 395 396 case ATAENCSTAT: { 397 struct ata_device *atadev; 398 399 if (!device || !(ch = device_get_softc(device))) 400 return ENXIO; 401 402 if (iocmd->device == SLAVE) 403 atadev = &ch->device[SLAVE]; 404 else 405 atadev = &ch->device[MASTER]; 406 407 return ata_enclosure_status(atadev, 408 &iocmd->u.enclosure.fan, 409 &iocmd->u.enclosure.temp, 410 &iocmd->u.enclosure.v05, 411 &iocmd->u.enclosure.v12); 412 } 413 414#ifdef DEV_ATADISK 415 case ATARAIDREBUILD: 416 return ata_raid_rebuild(iocmd->channel); 417 418 case ATARAIDCREATE: 419 return ata_raid_create(&iocmd->u.raid_setup); 420 421 case ATARAIDDELETE: 422 return ata_raid_delete(iocmd->channel); 423 424 case ATARAIDSTATUS: 425 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 426#endif 427#if DEV_ATAPIALL 428 case ATAPICMD: { 429 struct ata_device *atadev; 430 caddr_t buf; 431 432 if (!device || !(ch = device_get_softc(device))) 433 return ENXIO; 434 435 if (!(atadev = &ch->device[iocmd->device]) || 436 !(ch->devices & (iocmd->device == MASTER ? 437 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))) 438 return ENODEV; 439 440 if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT))) 441 return ENOMEM; 442 443 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) { 444 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count); 445 if (error) { 446 free(buf, M_ATA); 447 return error; 448 } 449 } 450 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb, 451 buf, iocmd->u.atapi.count, 452 (iocmd->u.atapi.flags == ATAPI_CMD_READ ? 453 ATPR_F_READ : 0) | ATPR_F_QUIET, 454 iocmd->u.atapi.timeout, NULL, NULL); 455 if (error) { 456 iocmd->u.atapi.error = error; 457 bcopy(&atadev->result, iocmd->u.atapi.sense_data, 458 sizeof(struct atapi_reqsense)); 459 error = 0; 460 } 461 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ) 462 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count); 463 464 free(buf, M_ATA); 465 return error; 466 } 467#endif 468 default: 469 break; 470 } 471 return ENOTTY; 472} 473 474static int 475ata_getparam(struct ata_device *atadev, u_int8_t command) 476{ 477 struct ata_params *ata_parm; 478 int retry = 0; 479 480 if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) { 481 ata_prtdev(atadev, "malloc for identify data failed\n"); 482 return -1; 483 } 484 485 /* apparently some devices needs this repeated */ 486 do { 487 if (ata_command(atadev, command, 0, 0, 0, 488 dumping ? ATA_WAIT_READY : ATA_WAIT_INTR)) { 489 ata_prtdev(atadev, "%s identify failed\n", 490 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 491 free(ata_parm, M_ATA); 492 return -1; 493 } 494 if (retry++ > 4) { 495 ata_prtdev(atadev, "%s identify retries exceeded\n", 496 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 497 free(ata_parm, M_ATA); 498 return -1; 499 } 500 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ? 501 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ)))); 502 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm, 503 sizeof(struct ata_params)/sizeof(int16_t)); 504 505 if (command == ATA_C_ATA_IDENTIFY || 506 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 507 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') || 508 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i'))) 509 bswap(ata_parm->model, sizeof(ata_parm->model)); 510 btrim(ata_parm->model, sizeof(ata_parm->model)); 511 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 512 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 513 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 514 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 515 bswap(ata_parm->serial, sizeof(ata_parm->serial)); 516 btrim(ata_parm->serial, sizeof(ata_parm->serial)); 517 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial)); 518 atadev->param = ata_parm; 519 return 0; 520} 521 522static void 523ata_boot_attach(void) 524{ 525 struct ata_channel *ch; 526 int ctlr; 527 528 /* 529 * run through all ata devices and look for real ATA & ATAPI devices 530 * using the hints we found in the early probe, this avoids some of 531 * the delays probing of non-exsistent devices can cause. 532 */ 533 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 534 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 535 continue; 536 ch->locking(ch, ATA_LF_LOCK); 537 if (ch->devices & ATA_ATA_SLAVE) 538 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 539 ch->devices &= ~ATA_ATA_SLAVE; 540 if (ch->devices & ATA_ATAPI_SLAVE) 541 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 542 ch->devices &= ~ATA_ATAPI_SLAVE; 543 if (ch->devices & ATA_ATA_MASTER) 544 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 545 ch->devices &= ~ATA_ATA_MASTER; 546 if (ch->devices & ATA_ATAPI_MASTER) 547 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 548 ch->devices &= ~ATA_ATAPI_MASTER; 549 ch->locking(ch, ATA_LF_UNLOCK); 550 } 551#ifdef DEV_ATADISK 552 /* now we know whats there, do the real attach, first the ATA disks */ 553 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 554 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 555 continue; 556 ch->locking(ch, ATA_LF_LOCK); 557 if (ch->devices & ATA_ATA_MASTER) 558 ad_attach(&ch->device[MASTER]); 559 if (ch->devices & ATA_ATA_SLAVE) 560 ad_attach(&ch->device[SLAVE]); 561 ch->locking(ch, ATA_LF_UNLOCK); 562 } 563#endif 564 /* then the atapi devices */ 565 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 566 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 567 continue; 568 ch->locking(ch, ATA_LF_LOCK); 569#if DEV_ATAPIALL 570 if (ch->devices & ATA_ATAPI_MASTER) 571 atapi_attach(&ch->device[MASTER]); 572 if (ch->devices & ATA_ATAPI_SLAVE) 573 atapi_attach(&ch->device[SLAVE]); 574#endif 575#ifdef DEV_ATAPICAM 576 atapi_cam_attach_bus(ch); 577#endif 578 ch->locking(ch, ATA_LF_UNLOCK); 579 } 580 if (ata_delayed_attach) { 581 config_intrhook_disestablish(ata_delayed_attach); 582 free(ata_delayed_attach, M_TEMP); 583 ata_delayed_attach = NULL; 584 } 585#ifdef DEV_ATADISK 586 ata_raid_attach(); 587#endif 588} 589 590static void 591ata_intr(void *data) 592{ 593 struct ata_channel *ch = (struct ata_channel *)data; 594 595 /* if device is busy it didn't interrupt */ 596 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) { 597 DELAY(100); 598 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ)) 599 return; 600 } 601 602 /* clear interrupt and get status */ 603 ch->status = ATA_INB(ch->r_io, ATA_STATUS); 604 605 if (ch->status & ATA_S_ERROR) 606 ch->error = ATA_INB(ch->r_io, ATA_ERROR); 607 608 /* find & call the responsible driver to process this interrupt */ 609 switch (ch->active) { 610#ifdef DEV_ATADISK 611 case ATA_ACTIVE_ATA: 612 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES) 613 return; 614 break; 615#endif 616#if DEV_ATAPIALL 617 case ATA_ACTIVE_ATAPI: 618 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES) 619 return; 620 break; 621#endif 622 default: 623 if (ch->active & ATA_WAIT_INTR) 624 wakeup(ch); 625 } 626 627 if (ch->active & ATA_CONTROL) { 628 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 629 return; 630 } 631 632 if (ch->active & ATA_WAIT_INTR) { 633 ATA_UNLOCK_CH(ch); 634 return; 635 } 636 637 if ((ch->flags & ATA_QUEUED) && 638 ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_SERVICE) { 639 ATA_FORCELOCK_CH(ch, ATA_ACTIVE); 640 if (ata_service(ch) == ATA_OP_CONTINUES) 641 return; 642 } 643 ch->running = NULL; 644 ATA_UNLOCK_CH(ch); 645 ch->locking(ch, ATA_LF_UNLOCK); 646 ata_start(ch); 647 return; 648} 649 650void 651ata_start(struct ata_channel *ch) 652{ 653#ifdef DEV_ATADISK 654 struct ad_request *ad_request; 655#endif 656#if DEV_ATAPIALL 657 struct atapi_request *atapi_request; 658#endif 659 int s; 660 661 ch->locking(ch, ATA_LF_LOCK); 662 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) 663 return; 664 665 s = splbio(); 666#ifdef DEV_ATADISK 667 /* find & call the responsible driver if anything on the ATA queue */ 668 if (TAILQ_EMPTY(&ch->ata_queue)) { 669 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver) 670 ad_start(&ch->device[MASTER]); 671 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 672 ad_start(&ch->device[SLAVE]); 673 } 674 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) { 675 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain); 676 ch->active = ATA_ACTIVE_ATA; 677 ch->running = ad_request; 678 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) { 679 splx(s); 680 return; 681 } 682 } 683 684#endif 685#if DEV_ATAPIALL 686 /* find & call the responsible driver if anything on the ATAPI queue */ 687 if (TAILQ_EMPTY(&ch->atapi_queue)) { 688 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) 689 atapi_start(&ch->device[MASTER]); 690 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) 691 atapi_start(&ch->device[SLAVE]); 692 } 693 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) { 694 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain); 695 ch->active = ATA_ACTIVE_ATAPI; 696 ch->running = atapi_request; 697 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) { 698 splx(s); 699 return; 700 } 701 } 702#endif 703 ATA_UNLOCK_CH(ch); 704 ch->locking(ch, ATA_LF_UNLOCK); 705 splx(s); 706} 707 708void 709ata_reset(struct ata_channel *ch) 710{ 711 u_int8_t lsb, msb, ostat0, ostat1; 712 u_int8_t stat0 = 0, stat1 = 0; 713 int mask = 0, timeout; 714 715 /* do we have any signs of ATA/ATAPI HW being present ? */ 716 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 717 DELAY(10); 718 ostat0 = ATA_INB(ch->r_io, ATA_STATUS); 719 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) { 720 stat0 = ATA_S_BUSY; 721 mask |= 0x01; 722 } 723 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 724 DELAY(10); 725 ostat1 = ATA_INB(ch->r_io, ATA_STATUS); 726 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) { 727 stat1 = ATA_S_BUSY; 728 mask |= 0x02; 729 } 730 731 ch->devices = 0; 732 if (!mask) 733 return; 734 735 /* in some setups we dont want to test for a slave */ 736 if (ch->flags & ATA_NO_SLAVE) { 737 stat1 = 0x0; 738 mask &= ~0x02; 739 } 740 741 if (bootverbose) 742 ata_printf(ch, -1, "pre reset mask=%02x ostat0=%02x ostat2=%02x\n", 743 mask, ostat0, ostat1); 744 745 /* reset channel */ 746 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 747 DELAY(10); 748 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 749 DELAY(10000); 750 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS); 751 DELAY(100000); 752 ATA_INB(ch->r_io, ATA_ERROR); 753 754 /* wait for BUSY to go inactive */ 755 for (timeout = 0; timeout < 310000; timeout++) { 756 if (stat0 & ATA_S_BUSY) { 757 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 758 DELAY(10); 759 760 /* check for ATAPI signature while its still there */ 761 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 762 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 763 stat0 = ATA_INB(ch->r_io, ATA_STATUS); 764 if (!(stat0 & ATA_S_BUSY)) { 765 if (bootverbose) 766 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb); 767 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 768 ch->devices |= ATA_ATAPI_MASTER; 769 } 770 } 771 if (stat1 & ATA_S_BUSY) { 772 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 773 DELAY(10); 774 775 /* check for ATAPI signature while its still there */ 776 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 777 msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 778 stat1 = ATA_INB(ch->r_io, ATA_STATUS); 779 if (!(stat1 & ATA_S_BUSY)) { 780 if (bootverbose) 781 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb); 782 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 783 ch->devices |= ATA_ATAPI_SLAVE; 784 } 785 } 786 if (mask == 0x01) /* wait for master only */ 787 if (!(stat0 & ATA_S_BUSY)) 788 break; 789 if (mask == 0x02) /* wait for slave only */ 790 if (!(stat1 & ATA_S_BUSY)) 791 break; 792 if (mask == 0x03) /* wait for both master & slave */ 793 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 794 break; 795 DELAY(100); 796 } 797 DELAY(10); 798 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 799 800 if (stat0 & ATA_S_BUSY) 801 mask &= ~0x01; 802 if (stat1 & ATA_S_BUSY) 803 mask &= ~0x02; 804 if (bootverbose) 805 ata_printf(ch, -1, "after reset mask=%02x stat0=%02x stat1=%02x\n", 806 mask, stat0, stat1); 807 if (!mask) 808 return; 809 810 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) { 811 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 812 DELAY(10); 813 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 814 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 815 lsb = ATA_INB(ch->r_io, ATA_ERROR); 816 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 817 if (bootverbose) 818 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb); 819 if (lsb != 0x58 && msb == 0xa5) 820 ch->devices |= ATA_ATA_MASTER; 821 } 822 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) { 823 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 824 DELAY(10); 825 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 826 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 827 lsb = ATA_INB(ch->r_io, ATA_ERROR); 828 msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 829 if (bootverbose) 830 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb); 831 if (lsb != 0x58 && msb == 0xa5) 832 ch->devices |= ATA_ATA_SLAVE; 833 } 834 if (bootverbose) 835 ata_printf(ch, -1, "devices=%02x\n", ch->devices); 836} 837 838int 839ata_reinit(struct ata_channel *ch) 840{ 841 int devices, misdev, newdev; 842 843 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 844 845 if (!ch->r_io || !ch->r_altio || !ch->r_irq) { 846 ATA_UNLOCK_CH(ch); 847 return ENXIO; 848 } 849 850 ch->running = NULL; 851 devices = ch->devices; 852 ata_printf(ch, -1, "resetting devices ..\n"); 853 ata_reset(ch); 854 855 if ((misdev = devices & ~ch->devices)) { 856#ifdef DEV_ATADISK 857 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver) 858 ad_detach(&ch->device[MASTER], 0); 859 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 860 ad_detach(&ch->device[SLAVE], 0); 861#endif 862#if DEV_ATAPIALL 863 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 864 atapi_detach(&ch->device[MASTER]); 865 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 866 atapi_detach(&ch->device[SLAVE]); 867#endif 868 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 869 if (ch->device[MASTER].param) 870 free(ch->device[MASTER].param, M_ATA); 871 ch->device[MASTER].param = NULL; 872 } 873 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 874 if (ch->device[SLAVE].param) 875 free(ch->device[SLAVE].param, M_ATA); 876 ch->device[SLAVE].param = NULL; 877 } 878 } 879 if ((newdev = ~devices & ch->devices)) { 880 if (newdev & ATA_ATA_MASTER) 881 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 882 ch->devices &= ~ATA_ATA_MASTER; 883 if (newdev & ATA_ATA_SLAVE) 884 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 885 ch->devices &= ~ATA_ATA_SLAVE; 886 if (newdev & ATA_ATAPI_MASTER) 887 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 888 ch->devices &= ~ATA_ATAPI_MASTER; 889 if (newdev & ATA_ATAPI_SLAVE) 890 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 891 ch->devices &= ~ATA_ATAPI_SLAVE; 892 } 893 newdev = ~devices & ch->devices; 894#ifdef DEV_ATADISK 895 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver) 896 ad_attach(&ch->device[SLAVE]); 897 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) { 898 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY); 899 ad_reinit(&ch->device[SLAVE]); 900 } 901 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver) 902 ad_attach(&ch->device[MASTER]); 903 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) { 904 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY); 905 ad_reinit(&ch->device[MASTER]); 906 } 907#endif 908#if DEV_ATAPIALL 909 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver) 910 atapi_attach(&ch->device[SLAVE]); 911 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) { 912 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY); 913 atapi_reinit(&ch->device[SLAVE]); 914 } 915 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver) 916 atapi_attach(&ch->device[MASTER]); 917 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) { 918 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY); 919 atapi_reinit(&ch->device[MASTER]); 920 } 921#endif 922#ifdef DEV_ATAPICAM 923 atapi_cam_reinit_bus(ch); 924#endif 925 printf("done\n"); 926 ATA_UNLOCK_CH(ch); 927 ata_start(ch); 928 return 0; 929} 930 931static int 932ata_service(struct ata_channel *ch) 933{ 934 /* do we have a SERVICE request from the drive ? */ 935 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) { 936#if 0 /* XXX */ 937 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 938 ch->dma->status(ch) | ATA_BMSTAT_INTERRUPT); 939#endif 940#ifdef DEV_ATADISK 941 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 942 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver) 943 return ad_service((struct ad_softc *) 944 ch->device[MASTER].driver, 0); 945 } 946 else { 947 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 948 return ad_service((struct ad_softc *) 949 ch->device[SLAVE].driver, 0); 950 } 951#endif 952 } 953 return ATA_OP_FINISHED; 954} 955 956int 957ata_wait(struct ata_device *atadev, u_int8_t mask) 958{ 959 int timeout = 0; 960 961 DELAY(1); 962 while (timeout < 5000000) { /* timeout 5 secs */ 963 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 964 965 /* if drive fails status, reselect the drive just to be sure */ 966 if (atadev->channel->status == 0xff) { 967 ata_prtdev(atadev, "no status, reselecting device\n"); 968 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit); 969 DELAY(10); 970 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS); 971 if (atadev->channel->status == 0xff) 972 return -1; 973 } 974 975 /* are we done ? */ 976 if (!(atadev->channel->status & ATA_S_BUSY)) 977 break; 978 979 if (timeout > 1000) { 980 timeout += 1000; 981 DELAY(1000); 982 } 983 else { 984 timeout += 10; 985 DELAY(10); 986 } 987 } 988 if (atadev->channel->status & ATA_S_ERROR) 989 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR); 990 if (timeout >= 5000000) 991 return -1; 992 if (!mask) 993 return (atadev->channel->status & ATA_S_ERROR); 994 995 /* Wait 50 msec for bits wanted. */ 996 timeout = 5000; 997 while (timeout--) { 998 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 999 if ((atadev->channel->status & mask) == mask) { 1000 if (atadev->channel->status & ATA_S_ERROR) 1001 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR); 1002 return (atadev->channel->status & ATA_S_ERROR); 1003 } 1004 DELAY (10); 1005 } 1006 return -1; 1007} 1008 1009int 1010ata_command(struct ata_device *atadev, u_int8_t command, 1011 u_int64_t lba, u_int16_t count, u_int16_t feature, int flags) 1012{ 1013 int error = 0; 1014#ifdef ATA_DEBUG 1015 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, " 1016 "lba=%jd, count=%d, feature=%d, flags=%02x\n", 1017 rman_get_start(atadev->channel->r_io), 1018 command, (intmax_t)lba, count, feature, flags); 1019#endif 1020 1021 /* select device */ 1022 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1023 1024 /* disable interrupt from device */ 1025 if (atadev->channel->flags & ATA_QUEUED) 1026 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 1027 1028 /* ready to issue command ? */ 1029 if (ata_wait(atadev, 0) < 0) { 1030 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n", 1031 command, atadev->channel->status, atadev->channel->error); 1032 return -1; 1033 } 1034 1035 /* only use 48bit addressing if needed because of the overhead */ 1036 if ((lba > 268435455 || count > 256) && atadev->param && 1037 atadev->param->support.address48) { 1038 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff); 1039 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1040 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff); 1041 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff); 1042 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff); 1043 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1044 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff); 1045 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1046 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff); 1047 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1048 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit); 1049 1050 /* translate command into 48bit version */ 1051 switch (command) { 1052 case ATA_C_READ: 1053 command = ATA_C_READ48; break; 1054 case ATA_C_READ_MUL: 1055 command = ATA_C_READ_MUL48; break; 1056 case ATA_C_READ_DMA: 1057 command = ATA_C_READ_DMA48; break; 1058 case ATA_C_READ_DMA_QUEUED: 1059 command = ATA_C_READ_DMA_QUEUED48; break; 1060 case ATA_C_WRITE: 1061 command = ATA_C_WRITE48; break; 1062 case ATA_C_WRITE_MUL: 1063 command = ATA_C_WRITE_MUL48; break; 1064 case ATA_C_WRITE_DMA: 1065 command = ATA_C_WRITE_DMA48; break; 1066 case ATA_C_WRITE_DMA_QUEUED: 1067 command = ATA_C_WRITE_DMA_QUEUED48; break; 1068 case ATA_C_FLUSHCACHE: 1069 command = ATA_C_FLUSHCACHE48; break; 1070 default: 1071 ata_prtdev(atadev, "can't translate cmd to 48bit version\n"); 1072 return -1; 1073 } 1074 atadev->channel->flags |= ATA_48BIT_ACTIVE; 1075 } 1076 else { 1077 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1078 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count); 1079 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1080 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1081 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1082 if (atadev->flags & ATA_D_USE_CHS) 1083 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1084 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf)); 1085 else 1086 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1087 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf)); 1088 atadev->channel->flags &= ~ATA_48BIT_ACTIVE; 1089 } 1090 1091 switch (flags & ATA_WAIT_MASK) { 1092 case ATA_IMMEDIATE: 1093 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1094 1095 /* enable interrupt */ 1096 if (atadev->channel->flags & ATA_QUEUED) 1097 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1098 break; 1099 1100 case ATA_WAIT_INTR: 1101 atadev->channel->active |= ATA_WAIT_INTR; 1102 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1103 1104 /* enable interrupt */ 1105 if (atadev->channel->flags & ATA_QUEUED) 1106 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1107 1108 if (tsleep(atadev->channel, PRIBIO, "atacmd", 10 * hz)) { 1109 ata_prtdev(atadev, "timeout waiting for interrupt\n"); 1110 atadev->channel->active &= ~ATA_WAIT_INTR; 1111 error = -1; 1112 } 1113 break; 1114 1115 case ATA_WAIT_READY: 1116 atadev->channel->active |= ATA_WAIT_READY; 1117 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1118 if (ata_wait(atadev, ATA_S_READY) < 0) { 1119 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n", 1120 command, atadev->channel->status,atadev->channel->error); 1121 error = -1; 1122 } 1123 atadev->channel->active &= ~ATA_WAIT_READY; 1124 break; 1125 } 1126 return error; 1127} 1128 1129static void 1130ata_enclosure_start(struct ata_device *atadev) 1131{ 1132 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1133 DELAY(1); 1134 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1135 DELAY(1); 1136 ATA_INB(atadev->channel->r_io, ATA_CMD); 1137 DELAY(1); 1138 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1139 DELAY(1); 1140 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1141 DELAY(1); 1142 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1143 DELAY(1); 1144 ATA_INB(atadev->channel->r_io, ATA_COUNT); 1145 DELAY(1); 1146 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1147 DELAY(1); 1148} 1149 1150static void 1151ata_enclosure_end(struct ata_device *atadev) 1152{ 1153 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1154 DELAY(1); 1155} 1156 1157static void 1158ata_enclosure_chip_start(struct ata_device *atadev) 1159{ 1160 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1161 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1162 DELAY(25); 1163 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1164} 1165 1166static void 1167ata_enclosure_chip_end(struct ata_device *atadev) 1168{ 1169 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1170 DELAY(64); 1171 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1172 DELAY(25); 1173 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1174 DELAY(64); 1175} 1176 1177static u_int8_t 1178ata_enclosure_chip_rdbit(struct ata_device *atadev) 1179{ 1180 u_int8_t val; 1181 1182 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0); 1183 DELAY(64); 1184 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02); 1185 DELAY(25); 1186 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01; 1187 DELAY(38); 1188 return val; 1189} 1190 1191static void 1192ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data) 1193{ 1194 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01)); 1195 DELAY(64); 1196 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01)); 1197 DELAY(64); 1198} 1199 1200static u_int8_t 1201ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val) 1202{ 1203 int i; 1204 1205 if (rw) { 1206 for (i = 0; i < 8; i++) 1207 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0); 1208 } 1209 else { 1210 for (i = 0; i < 8; i++) 1211 val = (val << 1) | ata_enclosure_chip_rdbit(atadev); 1212 } 1213 ata_enclosure_chip_wrbit(atadev, 0); 1214 return val; 1215} 1216 1217static u_int8_t 1218ata_enclosure_sensor(struct ata_device *atadev, 1219 int rw, u_int8_t idx, u_int8_t data) 1220{ 1221 ata_enclosure_start(atadev); 1222 ata_enclosure_chip_start(atadev); 1223 ata_enclosure_chip_rw(atadev, 1, 0x5a); 1224 ata_enclosure_chip_rw(atadev, 1, idx); 1225 if (rw) { 1226 ata_enclosure_chip_rw(atadev, 1, data); 1227 } 1228 else { 1229 ata_enclosure_chip_end(atadev); 1230 ata_enclosure_chip_start(atadev); 1231 ata_enclosure_chip_rw(atadev, 1, 0x5b); 1232 data = ata_enclosure_chip_rw(atadev, 0, 0); 1233 } 1234 ata_enclosure_chip_end(atadev); 1235 ata_enclosure_end(atadev); 1236 return data; 1237} 1238 1239static int 1240ata_enclosure_status(struct ata_device *atadev, 1241 int *fan, int *temp, int *v05, int *v12) 1242{ 1243 u_int8_t id1, id2, cnt, div; 1244 1245 if (atadev->flags & ATA_D_ENC_PRESENT) { 1246 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1247 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1248 ata_enclosure_sensor(atadev, 1, 0x4e, 0); 1249 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1250 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80); 1251 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1252 if (id1 != 0xa3 || id2 != 0x5c) 1253 return ENXIO; 1254 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0) & 0x20) >> 3)+ 1255 ((ata_enclosure_sensor(atadev, 0, 0x47, 0) & 0x30) >> 4)+1); 1256 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0); 1257 if (cnt == 0xff) 1258 *fan = 0; 1259 else 1260 *fan = 1350000 / cnt / div; 1261 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01); 1262 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) + 1263 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0); 1264 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27; 1265 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61; 1266 ATA_UNLOCK_CH(atadev->channel); 1267 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1268 return 0; 1269 } 1270 return ENXIO; 1271} 1272 1273void 1274ata_enclosure_print(struct ata_device *atadev) 1275{ 1276 u_int8_t id, st; 1277 int fan, temp, v05, v12; 1278 1279 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1280 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1281 ata_enclosure_start(atadev); 1282 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1283 DELAY(1); 1284 st = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1285 DELAY(1); 1286 ata_enclosure_end(atadev); 1287 ATA_UNLOCK_CH(atadev->channel); 1288 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1289 1290 switch (id & 0x93) { 1291 case 0x00: 1292 ata_prtdev(atadev, "Universal enclosure"); 1293 break; 1294 case 0x01: 1295 ata_prtdev(atadev, "FastSwap enclosure"); 1296 break; 1297 case 0x10: 1298 case 0x11: 1299 ata_prtdev(atadev, "SuperSwap enclosure"); 1300 break; 1301 default: 1302 atadev->flags &= ~ATA_D_ENC_PRESENT; 1303 return; 1304 } 1305 atadev->flags |= ATA_D_ENC_PRESENT; 1306 1307 ata_enclosure_leds(atadev, ATA_LED_GREEN); 1308 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12)) 1309 printf(" detected\n"); 1310 else 1311 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n", 1312 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000); 1313} 1314 1315void 1316ata_enclosure_leds(struct ata_device *atadev, u_int8_t color) 1317{ 1318 if (atadev->flags & ATA_D_ENC_PRESENT) { 1319 u_int8_t reg; 1320 1321 ata_enclosure_start(atadev); 1322 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1323 DELAY(1); 1324 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, 1325 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK)); 1326 DELAY(1); 1327 ata_enclosure_end(atadev); 1328 } 1329} 1330 1331static void 1332ata_change_mode(struct ata_device *atadev, int mode) 1333{ 1334 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE); 1335 atadev->setmode(atadev, mode); 1336 ATA_UNLOCK_CH(atadev->channel); 1337 ata_start(atadev->channel); 1338} 1339 1340int 1341ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 1342{ 1343 va_list ap; 1344 int ret; 1345 1346 if (device == -1) 1347 ret = printf("ata%d: ", device_get_unit(ch->dev)); 1348 else { 1349 if (ch->device[ATA_DEV(device)].name) 1350 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 1351 else 1352 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 1353 (device == ATA_MASTER) ? "master" : "slave"); 1354 } 1355 va_start(ap, fmt); 1356 ret += vprintf(fmt, ap); 1357 va_end(ap); 1358 return ret; 1359} 1360 1361int 1362ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 1363{ 1364 va_list ap; 1365 int ret; 1366 1367 if (atadev->name) 1368 ret = printf("%s: ", atadev->name); 1369 else 1370 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 1371 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 1372 va_start(ap, fmt); 1373 ret += vprintf(fmt, ap); 1374 va_end(ap); 1375 return ret; 1376} 1377 1378void 1379ata_set_name(struct ata_device *atadev, char *name, int lun) 1380{ 1381 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 1382 if (atadev->name) 1383 sprintf(atadev->name, "%s%d", name, lun); 1384} 1385 1386void 1387ata_free_name(struct ata_device *atadev) 1388{ 1389 if (atadev->name) 1390 free(atadev->name, M_ATA); 1391 atadev->name = NULL; 1392} 1393 1394int 1395ata_get_lun(u_int32_t *map) 1396{ 1397 int lun = ffs(~*map) - 1; 1398 1399 *map |= (1 << lun); 1400 return lun; 1401} 1402 1403int 1404ata_test_lun(u_int32_t *map, int lun) 1405{ 1406 return (*map & (1 << lun)); 1407} 1408 1409void 1410ata_free_lun(u_int32_t *map, int lun) 1411{ 1412 *map &= ~(1 << lun); 1413} 1414 1415char * 1416ata_mode2str(int mode) 1417{ 1418 switch (mode) { 1419 case ATA_PIO: return "BIOSPIO"; 1420 case ATA_PIO0: return "PIO0"; 1421 case ATA_PIO1: return "PIO1"; 1422 case ATA_PIO2: return "PIO2"; 1423 case ATA_PIO3: return "PIO3"; 1424 case ATA_PIO4: return "PIO4"; 1425 case ATA_DMA: return "BIOSDMA"; 1426 case ATA_WDMA0: return "WDMA0"; 1427 case ATA_WDMA1: return "WDMA1"; 1428 case ATA_WDMA2: return "WDMA2"; 1429 case ATA_UDMA0: return "UDMA16"; 1430 case ATA_UDMA1: return "UDMA25"; 1431 case ATA_UDMA2: return "UDMA33"; 1432 case ATA_UDMA3: return "UDMA40"; 1433 case ATA_UDMA4: return "UDMA66"; 1434 case ATA_UDMA5: return "UDMA100"; 1435 case ATA_UDMA6: return "UDMA133"; 1436 default: return "???"; 1437 } 1438} 1439 1440int 1441ata_pmode(struct ata_params *ap) 1442{ 1443 if (ap->atavalid & ATA_FLAG_64_70) { 1444 if (ap->apiomodes & 0x02) 1445 return ATA_PIO4; 1446 if (ap->apiomodes & 0x01) 1447 return ATA_PIO3; 1448 } 1449 if (ap->retired_piomode == 2) 1450 return ATA_PIO2; 1451 if (ap->retired_piomode == 1) 1452 return ATA_PIO1; 1453 if (ap->retired_piomode == 0) 1454 return ATA_PIO0; 1455 if (ap->support_dma) 1456 return ATA_PIO4; 1457 return ATA_PIO0; 1458} 1459 1460int 1461ata_wmode(struct ata_params *ap) 1462{ 1463 if (ap->mwdmamodes & 0x04) 1464 return ATA_WDMA2; 1465 if (ap->mwdmamodes & 0x02) 1466 return ATA_WDMA1; 1467 if (ap->mwdmamodes & 0x01) 1468 return ATA_WDMA0; 1469 if (ap->support_dma) 1470 return ATA_WDMA2; 1471 return -1; 1472} 1473 1474int 1475ata_umode(struct ata_params *ap) 1476{ 1477 if (ap->atavalid & ATA_FLAG_88) { 1478 if (ap->udmamodes & 0x40) 1479 return ATA_UDMA6; 1480 if (ap->udmamodes & 0x20) 1481 return ATA_UDMA5; 1482 if (ap->udmamodes & 0x10) 1483 return ATA_UDMA4; 1484 if (ap->udmamodes & 0x08) 1485 return ATA_UDMA3; 1486 if (ap->udmamodes & 0x04) 1487 return ATA_UDMA2; 1488 if (ap->udmamodes & 0x02) 1489 return ATA_UDMA1; 1490 if (ap->udmamodes & 0x01) 1491 return ATA_UDMA0; 1492 } 1493 return -1; 1494} 1495 1496int 1497ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 1498{ 1499 if (maxmode && mode > maxmode) 1500 mode = maxmode; 1501 1502 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 1503 return min(mode, ata_umode(atadev->param)); 1504 1505 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 1506 return min(mode, ata_wmode(atadev->param)); 1507 1508 if (mode > ata_pmode(atadev->param)) 1509 return min(mode, ata_pmode(atadev->param)); 1510 1511 return mode; 1512} 1513 1514static void 1515bswap(int8_t *buf, int len) 1516{ 1517 u_int16_t *ptr = (u_int16_t*)(buf + len); 1518 1519 while (--ptr >= (u_int16_t*)buf) 1520 *ptr = ntohs(*ptr); 1521} 1522 1523static void 1524btrim(int8_t *buf, int len) 1525{ 1526 int8_t *ptr; 1527 1528 for (ptr = buf; ptr < buf+len; ++ptr) 1529 if (!*ptr) 1530 *ptr = ' '; 1531 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1532 *ptr = 0; 1533} 1534 1535static void 1536bpack(int8_t *src, int8_t *dst, int len) 1537{ 1538 int i, j, blank; 1539 1540 for (i = j = blank = 0 ; i < len; i++) { 1541 if (blank && src[i] == ' ') continue; 1542 if (blank && src[i] != ' ') { 1543 dst[j++] = src[i]; 1544 blank = 0; 1545 continue; 1546 } 1547 if (src[i] == ' ') { 1548 blank = 1; 1549 if (i == 0) 1550 continue; 1551 } 1552 dst[j++] = src[i]; 1553 } 1554 if (j < len) 1555 dst[j] = 0x00; 1556} 1557 1558static void 1559ata_init(void) 1560{ 1561 /* register controlling device */ 1562 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1563 1564 /* register boot attach to be run when interrupts are enabled */ 1565 if (!(ata_delayed_attach = (struct intr_config_hook *) 1566 malloc(sizeof(struct intr_config_hook), 1567 M_TEMP, M_NOWAIT | M_ZERO))) { 1568 printf("ata: malloc of delayed attach hook failed\n"); 1569 return; 1570 } 1571 1572 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1573 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1574 printf("ata: config_intrhook_establish failed\n"); 1575 free(ata_delayed_attach, M_TEMP); 1576 } 1577} 1578SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
|