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