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 proc *p) 289 { 290 291 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 292 device_t device; 293 int error = 0; 294 295 if (cmd != IOCATA) 296 return ENOTTY; 297 298 if (iocmd->channel >= devclass_get_maxunit(ata_devclass)) 299 return ENXIO; 300 301 if (!(device = devclass_get_device(ata_devclass, iocmd->channel))) 302 return ENODEV; 303 304 switch (iocmd->cmd) { 305 case ATAATTACH: { 306 /* should enable channel HW on controller that can SOS XXX */ 307 if (!(error = ata_probe(device))) 308 error = ata_attach(device); 309 break; 310 } 311 312 case ATADETACH: { 313 error = ata_detach(device); 314 /* should disable channel HW on controller that can SOS XXX */ 315 break; 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 break; 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 break; 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 break; 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 break; 398 } 399 400 default: 401 error = ENOTTY; 402 } 403 return error; 404 } 405 406 static int 407 ata_getparam(struct ata_softc *scp, int device, u_int8_t command) 408 { 409 struct ata_params *ata_parm; 410 int8_t buffer[DEV_BSIZE]; 411 int retry = 0; 412 413 /* select drive */ 414 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 415 DELAY(1); 416 417 /* enable interrupt */ 418 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 419 DELAY(1); 420 421 /* apparently some devices needs this repeated */ 422 do { 423 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) { 424 ata_printf(scp, device, "identify failed\n"); 425 return -1; 426 } 427 if (retry++ > 4) { 428 ata_printf(scp, device, "identify retries exceeded\n"); 429 return -1; 430 } 431 } while (ata_wait(scp, device, 432 ((command == ATA_C_ATAPI_IDENTIFY) ? 433 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)))); 434 435 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer, 436 sizeof(buffer)/sizeof(int16_t)); 437 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 438 if (!ata_parm) { 439 ata_printf(scp, device, "malloc for identify data failed\n"); 440 return -1; 441 } 442 bcopy(buffer, ata_parm, sizeof(struct ata_params)); 443 if (command == ATA_C_ATA_IDENTIFY || 444 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 445 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X'))) 446 bswap(ata_parm->model, sizeof(ata_parm->model)); 447 btrim(ata_parm->model, sizeof(ata_parm->model)); 448 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 449 bswap(ata_parm->revision, sizeof(ata_parm->revision)); 450 btrim(ata_parm->revision, sizeof(ata_parm->revision)); 451 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 452 scp->dev_param[ATA_DEV(device)] = ata_parm; 453 return 0; 454 } 455 456 static void 457 ata_boot_attach(void) 458 { 459 struct ata_softc *scp; 460 int ctlr; 461 462 /* 463 * run through all ata devices and look for real ATA & ATAPI devices 464 * using the hints we found in the early probe, this avoids some of 465 * the delays probing of non-exsistent devices can cause. 466 */ 467 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 468 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 469 continue; 470 if (scp->devices & ATA_ATA_SLAVE) 471 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 472 scp->devices &= ~ATA_ATA_SLAVE; 473 if (scp->devices & ATA_ATAPI_SLAVE) 474 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 475 scp->devices &= ~ATA_ATAPI_SLAVE; 476 if (scp->devices & ATA_ATA_MASTER) 477 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 478 scp->devices &= ~ATA_ATA_MASTER; 479 if (scp->devices & ATA_ATAPI_MASTER) 480 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY)) 481 scp->devices &= ~ATA_ATAPI_MASTER; 482 } 483 484 #ifdef DEV_ATADISK 485 /* now we know whats there, do the real attach, first the ATA disks */ 486 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 487 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 488 continue; 489 if (scp->devices & ATA_ATA_MASTER) 490 ad_attach(scp, ATA_MASTER); 491 if (scp->devices & ATA_ATA_SLAVE) 492 ad_attach(scp, ATA_SLAVE); 493 } 494 #endif 495 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 496 /* then the atapi devices */ 497 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 498 if (!(scp = devclass_get_softc(ata_devclass, ctlr))) 499 continue; 500 if (scp->devices & ATA_ATAPI_MASTER) 501 atapi_attach(scp, ATA_MASTER); 502 if (scp->devices & ATA_ATAPI_SLAVE) 503 atapi_attach(scp, ATA_SLAVE); 504 } 505 #endif 506 if (ata_delayed_attach) { 507 config_intrhook_disestablish(ata_delayed_attach); 508 free(ata_delayed_attach, M_ATA); 509 ata_delayed_attach = NULL; 510 } 511 } 512 513 static void 514 ata_intr(void *data) 515 { 516 struct ata_softc *scp = (struct ata_softc *)data; 517 518 /* 519 * on PCI systems we might share an interrupt line with another 520 * device or our twin ATA channel, so call scp->intr_func to figure 521 * out if it is really an interrupt we should process here 522 */ 523 if (scp->intr_func && scp->intr_func(scp)) 524 return; 525 526 /* if drive is busy it didn't interrupt */ 527 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) { 528 DELAY(100); 529 if (!(ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_DRQ)) 530 return; 531 } 532 533 /* clear interrupt and get status */ 534 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 535 536 if (scp->status & ATA_S_ERROR) 537 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 538 539 /* find & call the responsible driver to process this interrupt */ 540 switch (scp->active) { 541 #ifdef DEV_ATADISK 542 case ATA_ACTIVE_ATA: 543 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES) 544 return; 545 break; 546 #endif 547 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 548 case ATA_ACTIVE_ATAPI: 549 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES) 550 return; 551 break; 552 #endif 553 case ATA_WAIT_INTR: 554 case ATA_WAIT_INTR | ATA_CONTROL: 555 wakeup((caddr_t)scp); 556 break; 557 558 case ATA_WAIT_READY: 559 case ATA_WAIT_READY | ATA_CONTROL: 560 break; 561 562 case ATA_IDLE: 563 if (scp->flags & ATA_QUEUED) { 564 scp->active = ATA_ACTIVE; /* XXX */ 565 if (ata_service(scp) == ATA_OP_CONTINUES) 566 return; 567 } 568 /* FALLTHROUGH */ 569 570 default: 571 #ifdef ATA_DEBUG 572 { 573 static int intr_count = 0; 574 575 if (intr_count++ < 10) 576 ata_printf(scp, -1, "unwanted interrupt %d %sstatus = %02x\n", 577 intr_count, active2str(scp->active), scp->status); 578 } 579 #endif 580 } 581 scp->active &= ATA_CONTROL; 582 if (scp->active & ATA_CONTROL) 583 return; 584 scp->running = NULL; 585 ata_start(scp); 586 return; 587 } 588 589 void 590 ata_start(struct ata_softc *scp) 591 { 592 #ifdef DEV_ATADISK 593 struct ad_request *ad_request; 594 #endif 595 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 596 struct atapi_request *atapi_request; 597 #endif 598 599 if (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE)) 600 return; 601 602 #ifdef DEV_ATADISK 603 /* find & call the responsible driver if anything on the ATA queue */ 604 if (TAILQ_EMPTY(&scp->ata_queue)) { 605 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[MASTER]) 606 ad_start((struct ad_softc *)scp->dev_softc[MASTER]); 607 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 608 ad_start((struct ad_softc *)scp->dev_softc[SLAVE]); 609 } 610 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) { 611 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain); 612 scp->active = ATA_ACTIVE_ATA; 613 scp->running = ad_request; 614 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) 615 return; 616 } 617 618 #endif 619 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 620 /* find & call the responsible driver if anything on the ATAPI queue */ 621 if (TAILQ_EMPTY(&scp->atapi_queue)) { 622 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER]) 623 atapi_start((struct atapi_softc *)scp->dev_softc[MASTER]); 624 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE]) 625 atapi_start((struct atapi_softc *)scp->dev_softc[SLAVE]); 626 } 627 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 628 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 629 scp->active = ATA_ACTIVE_ATAPI; 630 scp->running = atapi_request; 631 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) 632 return; 633 } 634 #endif 635 scp->active = ATA_IDLE; 636 } 637 638 void 639 ata_reset(struct ata_softc *scp) 640 { 641 u_int8_t lsb, msb, ostat0, ostat1; 642 u_int8_t stat0 = ATA_S_BUSY, stat1 = ATA_S_BUSY; 643 int mask = 0, timeout; 644 645 /* do we have any signs of ATA/ATAPI HW being present ? */ 646 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 647 DELAY(10); 648 ostat0 = ATA_INB(scp->r_io, ATA_STATUS); 649 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) 650 mask |= 0x01; 651 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 652 DELAY(10); 653 ostat1 = ATA_INB(scp->r_io, ATA_STATUS); 654 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) 655 mask |= 0x02; 656 657 scp->devices = 0; 658 if (!mask) 659 return; 660 661 /* in some setups we dont want to test for a slave */ 662 if (scp->flags & ATA_NO_SLAVE) 663 mask &= ~0x02; 664 665 if (bootverbose) 666 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n", 667 mask, ostat0, ostat1); 668 669 /* reset channel */ 670 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 671 DELAY(10000); 672 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS); 673 DELAY(100000); 674 ATA_INB(scp->r_io, ATA_ERROR); 675 676 /* wait for BUSY to go inactive */ 677 for (timeout = 0; timeout < 310000; timeout++) { 678 if (stat0 & ATA_S_BUSY) { 679 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 680 DELAY(10); 681 stat0 = ATA_INB(scp->r_io, ATA_STATUS); 682 if (!(stat0 & ATA_S_BUSY)) { 683 /* check for ATAPI signature while its still there */ 684 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB); 685 msb = ATA_INB(scp->r_io, ATA_CYL_MSB); 686 if (bootverbose) 687 ata_printf(scp, ATA_MASTER, 688 "ATAPI probe %02x %02x\n", lsb, msb); 689 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 690 scp->devices |= ATA_ATAPI_MASTER; 691 } 692 } 693 if (stat1 & ATA_S_BUSY) { 694 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 695 DELAY(10); 696 stat1 = ATA_INB(scp->r_io, ATA_STATUS); 697 if (!(stat1 & ATA_S_BUSY)) { 698 /* check for ATAPI signature while its still there */ 699 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB); 700 msb = ATA_INB(scp->r_io, ATA_CYL_MSB); 701 if (bootverbose) 702 ata_printf(scp, ATA_SLAVE, 703 "ATAPI probe %02x %02x\n", lsb, msb); 704 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 705 scp->devices |= ATA_ATAPI_SLAVE; 706 } 707 } 708 if (mask == 0x01) /* wait for master only */ 709 if (!(stat0 & ATA_S_BUSY)) 710 break; 711 if (mask == 0x02) /* wait for slave only */ 712 if (!(stat1 & ATA_S_BUSY)) 713 break; 714 if (mask == 0x03) /* wait for both master & slave */ 715 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 716 break; 717 DELAY(100); 718 } 719 DELAY(10); 720 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 721 722 if (stat0 & ATA_S_BUSY) 723 mask &= ~0x01; 724 if (stat1 & ATA_S_BUSY) 725 mask &= ~0x02; 726 if (bootverbose) 727 ata_printf(scp, -1, "mask=%02x stat0=%02x stat1=%02x\n", 728 mask, stat0, stat1); 729 if (!mask) 730 return; 731 732 if (mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 733 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 734 DELAY(10); 735 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58); 736 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5); 737 lsb = ATA_INB(scp->r_io, ATA_ERROR); 738 msb = ATA_INB(scp->r_io, ATA_CYL_LSB); 739 if (bootverbose) 740 ata_printf(scp, ATA_MASTER, "ATA probe %02x %02x\n", lsb, msb); 741 if (lsb != 0x58 && msb == 0xa5) 742 scp->devices |= ATA_ATA_MASTER; 743 } 744 if (mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 745 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 746 DELAY(10); 747 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58); 748 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5); 749 lsb = ATA_INB(scp->r_io, ATA_ERROR); 750 msb = ATA_INB(scp->r_io, ATA_CYL_LSB); 751 if (bootverbose) 752 ata_printf(scp, ATA_SLAVE, "ATA probe %02x %02x\n", lsb, msb); 753 if (lsb != 0x58 && msb == 0xa5) 754 scp->devices |= ATA_ATA_SLAVE; 755 } 756 if (bootverbose) 757 ata_printf(scp, -1, "devices=%02x\n", scp->devices); 758 } 759 760 int 761 ata_reinit(struct ata_softc *scp) 762 { 763 int devices, misdev, newdev; 764 765 if (!scp->r_io || !scp->r_altio || !scp->r_irq) 766 return ENXIO; 767 scp->active = ATA_CONTROL; 768 scp->running = NULL; 769 devices = scp->devices; 770 ata_printf(scp, -1, "resetting devices .. "); 771 ata_reset(scp); 772 773 if ((misdev = devices & ~scp->devices)) { 774 if (misdev) 775 printf("\n"); 776 #ifdef DEV_ATADISK 777 if (misdev & ATA_ATA_MASTER && scp->dev_softc[MASTER]) 778 ad_detach(scp->dev_softc[MASTER], 0); 779 if (misdev & ATA_ATA_SLAVE && scp->dev_softc[SLAVE]) 780 ad_detach(scp->dev_softc[SLAVE], 0); 781 #endif 782 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 783 if (misdev & ATA_ATAPI_MASTER && scp->dev_softc[MASTER]) 784 atapi_detach(scp->dev_softc[MASTER]); 785 if (misdev & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE]) 786 atapi_detach(scp->dev_softc[SLAVE]); 787 #endif 788 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 789 free(scp->dev_param[MASTER], M_ATA); 790 scp->dev_param[MASTER] = NULL; 791 } 792 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 793 free(scp->dev_param[SLAVE], M_ATA); 794 scp->dev_param[SLAVE] = NULL; 795 } 796 } 797 if ((newdev = ~devices & scp->devices)) { 798 if (newdev & ATA_ATA_MASTER) 799 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY)) 800 newdev &= ~ATA_ATA_MASTER; 801 if (newdev & ATA_ATA_SLAVE) 802 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY)) 803 newdev &= ~ATA_ATA_SLAVE; 804 if (newdev & ATA_ATAPI_MASTER) 805 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY)) 806 newdev &= ~ATA_ATAPI_MASTER; 807 if (newdev & ATA_ATAPI_SLAVE) 808 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY)) 809 newdev &= ~ATA_ATAPI_SLAVE; 810 } 811 if (!misdev && newdev) 812 printf("\n"); 813 #ifdef DEV_ATADISK 814 if (newdev & ATA_ATA_MASTER && !scp->dev_softc[MASTER]) 815 ad_attach(scp, ATA_MASTER); 816 else if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER]) 817 ad_reinit((struct ad_softc *)scp->dev_softc[MASTER]); 818 if (newdev & ATA_ATA_SLAVE && !scp->dev_softc[SLAVE]) 819 ad_attach(scp, ATA_SLAVE); 820 else if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 821 ad_reinit((struct ad_softc *)scp->dev_softc[SLAVE]); 822 #endif 823 #if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST) 824 if (newdev & ATA_ATAPI_MASTER && !scp->dev_softc[MASTER]) 825 atapi_attach(scp, ATA_MASTER); 826 else if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER]) 827 atapi_reinit((struct atapi_softc *)scp->dev_softc[MASTER]); 828 if (newdev & ATA_ATAPI_SLAVE && !scp->dev_softc[SLAVE]) 829 atapi_attach(scp, ATA_SLAVE); 830 else if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE]) 831 atapi_reinit((struct atapi_softc *)scp->dev_softc[SLAVE]); 832 #endif 833 printf("done\n"); 834 scp->active = ATA_IDLE; 835 ata_start(scp); 836 return 0; 837 } 838 839 static int 840 ata_service(struct ata_softc *scp) 841 { 842 /* do we have a SERVICE request from the drive ? */ 843 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){ 844 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, 845 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT); 846 #ifdef DEV_ATADISK 847 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 848 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[MASTER]) 849 return ad_service((struct ad_softc *)scp->dev_softc[MASTER], 0); 850 } 851 else { 852 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[SLAVE]) 853 return ad_service((struct ad_softc *)scp->dev_softc[SLAVE], 0); 854 } 855 #endif 856 } 857 return ATA_OP_FINISHED; 858 } 859 860 int 861 ata_wait(struct ata_softc *scp, int device, u_int8_t mask) 862 { 863 int timeout = 0; 864 865 DELAY(1); 866 while (timeout < 5000000) { /* timeout 5 secs */ 867 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 868 869 /* if drive fails status, reselect the drive just to be sure */ 870 if (scp->status == 0xff) { 871 ata_printf(scp, device, "no status, reselecting device\n"); 872 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 873 DELAY(10); 874 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 875 if (scp->status == 0xff) 876 return -1; 877 } 878 879 /* are we done ? */ 880 if (!(scp->status & ATA_S_BUSY)) 881 break; 882 883 if (timeout > 1000) { 884 timeout += 1000; 885 DELAY(1000); 886 } 887 else { 888 timeout += 10; 889 DELAY(10); 890 } 891 } 892 if (scp->status & ATA_S_ERROR) 893 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 894 if (timeout >= 5000000) 895 return -1; 896 if (!mask) 897 return (scp->status & ATA_S_ERROR); 898 899 /* Wait 50 msec for bits wanted. */ 900 timeout = 5000; 901 while (timeout--) { 902 scp->status = ATA_INB(scp->r_io, ATA_STATUS); 903 if ((scp->status & mask) == mask) { 904 if (scp->status & ATA_S_ERROR) 905 scp->error = ATA_INB(scp->r_io, ATA_ERROR); 906 return (scp->status & ATA_S_ERROR); 907 } 908 DELAY (10); 909 } 910 return -1; 911 } 912 913 int 914 ata_command(struct ata_softc *scp, int device, u_int8_t command, 915 u_int16_t cylinder, u_int8_t head, u_int8_t sector, 916 u_int8_t count, u_int8_t feature, int flags) 917 { 918 int error = 0; 919 #ifdef ATA_DEBUG 920 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, " 921 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n", 922 rman_get_start(scp->r_io), command, cylinder, head, sector, 923 count, feature, flags); 924 925 /* sanity checks */ 926 switch(scp->active) { 927 case ATA_IDLE: 928 break; 929 930 case ATA_CONTROL: 931 if (flags == ATA_WAIT_INTR || flags == ATA_WAIT_READY) 932 break; 933 goto out; 934 935 case ATA_ACTIVE_ATA: 936 case ATA_ACTIVE_ATAPI: 937 if (flags == ATA_IMMEDIATE) 938 break; 939 940 default: 941 out: 942 printf("ata_command called %s flags=%s cmd=%02x\n", 943 active2str(scp->active), active2str(flags), command); 944 break; 945 } 946 #endif 947 948 /* disable interrupt from device */ 949 if (scp->flags & ATA_QUEUED) 950 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 951 952 /* select device */ 953 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device); 954 955 /* ready to issue command ? */ 956 if (ata_wait(scp, device, 0) < 0) { 957 ata_printf(scp, device, 958 "timeout waiting to give command=%02x s=%02x e=%02x\n", 959 command, scp->status, scp->error); 960 return -1; 961 } 962 963 ATA_OUTB(scp->r_io, ATA_FEATURE, feature); 964 ATA_OUTB(scp->r_io, ATA_COUNT, count); 965 ATA_OUTB(scp->r_io, ATA_SECTOR, sector); 966 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8); 967 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder); 968 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head); 969 970 switch (flags) { 971 case ATA_WAIT_INTR: 972 scp->active |= ATA_WAIT_INTR; 973 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz); 974 ATA_OUTB(scp->r_io, ATA_CMD, command); 975 976 /* enable interrupt */ 977 if (scp->flags & ATA_QUEUED) 978 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 979 980 if (await(PRIBIO, 10 * hz)) { 981 ata_printf(scp, device, "ata_command: timeout waiting for intr\n"); 982 scp->active &= ~ATA_WAIT_INTR; 983 error = -1; 984 } 985 break; 986 987 case ATA_WAIT_READY: 988 scp->active |= ATA_WAIT_READY; 989 ATA_OUTB(scp->r_io, ATA_CMD, command); 990 if (ata_wait(scp, device, ATA_S_READY) < 0) { 991 ata_printf(scp, device, 992 "timeout waiting for command=%02x s=%02x e=%02x\n", 993 command, scp->status, scp->error); 994 error = -1; 995 } 996 scp->active &= ~ATA_WAIT_READY; 997 break; 998 999 case ATA_IMMEDIATE: 1000 ATA_OUTB(scp->r_io, ATA_CMD, command); 1001 break; 1002 1003 default: 1004 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n", 1005 active2str(flags)); 1006 } 1007 /* enable interrupt */ 1008 if (scp->flags & ATA_QUEUED) 1009 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1010 return error; 1011 } 1012 1013 void 1014 ata_set_name(struct ata_softc *scp, int device, char *name) 1015 { 1016 scp->dev_name[ATA_DEV(device)] = malloc(strlen(name) + 1, M_ATA, M_NOWAIT); 1017 if (scp->dev_name[ATA_DEV(device)]) 1018 strcpy(scp->dev_name[ATA_DEV(device)], name); 1019 } 1020 1021 void 1022 ata_free_name(struct ata_softc *scp, int device) 1023 { 1024 if (scp->dev_name[ATA_DEV(device)]) 1025 free(scp->dev_name[ATA_DEV(device)], M_ATA); 1026 } 1027 1028 int 1029 ata_get_lun(u_int32_t *map) 1030 { 1031 int lun = ffs(~*map) - 1; 1032 1033 *map |= (1 << lun); 1034 return lun; 1035 } 1036 1037 int 1038 ata_test_lun(u_int32_t *map, int lun) 1039 { 1040 return (*map & (1 << lun)); 1041 } 1042 1043 void 1044 ata_free_lun(u_int32_t *map, int lun) 1045 { 1046 *map &= ~(1 << lun); 1047 } 1048 1049 int 1050 ata_printf(struct ata_softc *scp, int device, const char * fmt, ...) 1051 { 1052 va_list ap; 1053 int ret; 1054 1055 if (device == -1) 1056 ret = printf("ata%d: ", device_get_unit(scp->dev)); 1057 else { 1058 if (scp->dev_name[ATA_DEV(device)]) 1059 ret = printf("%s: ", scp->dev_name[ATA_DEV(device)]); 1060 else 1061 ret = printf("ata%d-%s: ", device_get_unit(scp->dev), 1062 (device == ATA_MASTER) ? "master" : "slave"); 1063 } 1064 va_start(ap, fmt); 1065 ret += vprintf(fmt, ap); 1066 va_end(ap); 1067 return ret; 1068 } 1069 1070 char * 1071 ata_mode2str(int mode) 1072 { 1073 switch (mode) { 1074 case ATA_PIO: return "BIOSPIO"; 1075 case ATA_PIO0: return "PIO0"; 1076 case ATA_PIO1: return "PIO1"; 1077 case ATA_PIO2: return "PIO2"; 1078 case ATA_PIO3: return "PIO3"; 1079 case ATA_PIO4: return "PIO4"; 1080 case ATA_WDMA2: return "WDMA2"; 1081 case ATA_UDMA2: return "UDMA33"; 1082 case ATA_UDMA4: return "UDMA66"; 1083 case ATA_UDMA5: return "UDMA100"; 1084 case ATA_DMA: return "BIOSDMA"; 1085 default: return "???"; 1086 } 1087 } 1088 1089 int 1090 ata_pio2mode(int pio) 1091 { 1092 switch (pio) { 1093 default: 1094 case 0: return ATA_PIO0; 1095 case 1: return ATA_PIO1; 1096 case 2: return ATA_PIO2; 1097 case 3: return ATA_PIO3; 1098 case 4: return ATA_PIO4; 1099 } 1100 } 1101 1102 int 1103 ata_pmode(struct ata_params *ap) 1104 { 1105 if (ap->atavalid & ATA_FLAG_64_70) { 1106 if (ap->apiomodes & 2) 1107 return 4; 1108 if (ap->apiomodes & 1) 1109 return 3; 1110 } 1111 if (ap->opiomode == 2) 1112 return 2; 1113 if (ap->opiomode == 1) 1114 return 1; 1115 if (ap->opiomode == 0) 1116 return 0; 1117 return -1; 1118 } 1119 1120 int 1121 ata_wmode(struct ata_params *ap) 1122 { 1123 if (ap->wdmamodes & 4) 1124 return 2; 1125 if (ap->wdmamodes & 2) 1126 return 1; 1127 if (ap->wdmamodes & 1) 1128 return 0; 1129 return -1; 1130 } 1131 1132 int 1133 ata_umode(struct ata_params *ap) 1134 { 1135 if (ap->atavalid & ATA_FLAG_88) { 1136 if (ap->udmamodes & 0x20) 1137 return 5; 1138 if (ap->udmamodes & 0x10) 1139 return 4; 1140 if (ap->udmamodes & 0x08) 1141 return 3; 1142 if (ap->udmamodes & 0x04) 1143 return 2; 1144 if (ap->udmamodes & 0x02) 1145 return 1; 1146 if (ap->udmamodes & 0x01) 1147 return 0; 1148 } 1149 return -1; 1150 } 1151 1152 static char * 1153 active2str(int active) 1154 { 1155 static char buf[64]; 1156 1157 bzero(buf, sizeof(buf)); 1158 if (active & ATA_IDLE) 1159 strcat(buf, "ATA_IDLE "); 1160 if (active & ATA_IMMEDIATE) 1161 strcat(buf, "ATA_IMMEDIATE "); 1162 if (active & ATA_WAIT_INTR) 1163 strcat(buf, "ATA_WAIT_INTR "); 1164 if (active & ATA_WAIT_READY) 1165 strcat(buf, "ATA_WAIT_READY "); 1166 if (active & ATA_ACTIVE) 1167 strcat(buf, "ATA_ACTIVE "); 1168 if (active & ATA_ACTIVE_ATA) 1169 strcat(buf, "ATA_ACTIVE_ATA "); 1170 if (active & ATA_ACTIVE_ATAPI) 1171 strcat(buf, "ATA_ACTIVE_ATAPI "); 1172 if (active & ATA_CONTROL) 1173 strcat(buf, "ATA_CONTROL "); 1174 return buf; 1175 } 1176 1177 static void 1178 bswap(int8_t *buf, int len) 1179 { 1180 u_int16_t *ptr = (u_int16_t*)(buf + len); 1181 1182 while (--ptr >= (u_int16_t*)buf) 1183 *ptr = ntohs(*ptr); 1184 } 1185 1186 static void 1187 btrim(int8_t *buf, int len) 1188 { 1189 int8_t *ptr; 1190 1191 for (ptr = buf; ptr < buf+len; ++ptr) 1192 if (!*ptr) 1193 *ptr = ' '; 1194 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1195 *ptr = 0; 1196 } 1197 1198 static void 1199 bpack(int8_t *src, int8_t *dst, int len) 1200 { 1201 int i, j, blank; 1202 1203 for (i = j = blank = 0 ; i < len; i++) { 1204 if (blank && src[i] == ' ') continue; 1205 if (blank && src[i] != ' ') { 1206 dst[j++] = src[i]; 1207 blank = 0; 1208 continue; 1209 } 1210 if (src[i] == ' ') { 1211 blank = 1; 1212 if (i == 0) 1213 continue; 1214 } 1215 dst[j++] = src[i]; 1216 } 1217 if (j < len) 1218 dst[j] = 0x00; 1219 } 1220 1221 static void 1222 ata_change_mode(struct ata_softc *scp, int device, int mode) 1223 { 1224 int umode, wmode, pmode; 1225 int s = splbio(); 1226 1227 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE)) 1228 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4); 1229 1230 umode = ata_umode(ATA_PARAM(scp, device)); 1231 wmode = ata_wmode(ATA_PARAM(scp, device)); 1232 pmode = ata_pmode(ATA_PARAM(scp, device)); 1233 1234 switch (mode & ATA_DMA_MASK) { 1235 case ATA_UDMA: 1236 if ((mode & ATA_MODE_MASK) < umode) 1237 umode = mode & ATA_MODE_MASK; 1238 break; 1239 case ATA_WDMA: 1240 if ((mode & ATA_MODE_MASK) < wmode) 1241 wmode = mode & ATA_MODE_MASK; 1242 umode = -1; 1243 break; 1244 default: 1245 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode) 1246 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0; 1247 umode = -1; 1248 wmode = -1; 1249 } 1250 ata_dmainit(scp, device, pmode, wmode, umode); 1251 1252 scp->active = ATA_IDLE; 1253 ata_start(scp); 1254 splx(s); 1255 } 1256 1257 static void 1258 ata_init(void) 1259 { 1260 /* register controlling device */ 1261 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1262 1263 /* register boot attach to be run when interrupts are enabled */ 1264 if (!(ata_delayed_attach = (struct intr_config_hook *) 1265 malloc(sizeof(struct intr_config_hook), 1266 M_TEMP, M_NOWAIT | M_ZERO))) { 1267 printf("ata: malloc of delayed attach hook failed\n"); 1268 return; 1269 } 1270 1271 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1272 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1273 printf("ata: config_intrhook_establish failed\n"); 1274 free(ata_delayed_attach, M_TEMP); 1275 } 1276 } 1277 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1278