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