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