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