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