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