1 /*- 2 * Copyright (c) 1998 - 2004 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 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 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/endian.h> 38 #include <sys/ctype.h> 39 #include <sys/conf.h> 40 #include <sys/bus.h> 41 #include <sys/bio.h> 42 #include <sys/malloc.h> 43 #include <sys/sysctl.h> 44 #include <sys/sema.h> 45 #include <sys/taskqueue.h> 46 #include <vm/uma.h> 47 #include <machine/stdarg.h> 48 #include <machine/resource.h> 49 #include <machine/bus.h> 50 #include <sys/rman.h> 51 #ifdef __alpha__ 52 #include <machine/md_var.h> 53 #endif 54 #include <geom/geom_disk.h> 55 #include <dev/ata/ata-all.h> 56 #include <dev/ata/ata-disk.h> 57 #include <dev/ata/ata-raid.h> 58 59 /* device structures */ 60 static d_ioctl_t ata_ioctl; 61 static struct cdevsw ata_cdevsw = { 62 .d_version = D_VERSION, 63 .d_flags = D_NEEDGIANT, 64 .d_ioctl = ata_ioctl, 65 .d_name = "ata", 66 }; 67 68 /* prototypes */ 69 static void ata_shutdown(void *, int); 70 static void ata_interrupt(void *); 71 static int ata_getparam(struct ata_device *, u_int8_t); 72 static void ata_identify_devices(struct ata_channel *); 73 static void ata_boot_attach(void); 74 static void bswap(int8_t *, int); 75 static void btrim(int8_t *, int); 76 static void bpack(int8_t *, int8_t *, int); 77 static void ata_init(void); 78 79 /* global vars */ 80 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 81 devclass_t ata_devclass; 82 uma_zone_t ata_zone; 83 int ata_wc = 1; 84 85 /* local vars */ 86 static struct intr_config_hook *ata_delayed_attach = NULL; 87 static int ata_dma = 1; 88 static int atapi_dma = 1; 89 90 /* sysctl vars */ 91 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 92 TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 93 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0, 94 "ATA disk DMA mode control"); 95 TUNABLE_INT("hw.ata.wc", &ata_wc); 96 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0, 97 "ATA disk write caching"); 98 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 99 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0, 100 "ATAPI device DMA mode control"); 101 102 /* 103 * newbus device interface related functions 104 */ 105 int 106 ata_probe(device_t dev) 107 { 108 struct ata_channel *ch; 109 110 if (!dev || !(ch = device_get_softc(dev))) 111 return ENXIO; 112 113 if (ch->r_irq) 114 return EEXIST; 115 116 return 0; 117 } 118 119 int 120 ata_attach(device_t dev) 121 { 122 struct ata_channel *ch; 123 int error, rid; 124 125 if (!dev || !(ch = device_get_softc(dev))) 126 return ENXIO; 127 128 /* initialize the softc basics */ 129 ch->device[MASTER].channel = ch; 130 ch->device[MASTER].unit = ATA_MASTER; 131 ch->device[MASTER].mode = ATA_PIO; 132 ch->device[SLAVE].channel = ch; 133 ch->device[SLAVE].unit = ATA_SLAVE; 134 ch->device[SLAVE].mode = ATA_PIO; 135 ch->dev = dev; 136 ch->state = ATA_IDLE; 137 bzero(&ch->state_mtx, sizeof(struct mtx)); 138 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF); 139 140 /* initialise device(s) on this channel */ 141 while (ch->locking(ch, ATA_LF_LOCK) != ch->unit) 142 tsleep(&error, PRIBIO, "ataatch", 1); 143 ch->hw.reset(ch); 144 ch->locking(ch, ATA_LF_UNLOCK); 145 146 rid = ATA_IRQ_RID; 147 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 148 RF_SHAREABLE | RF_ACTIVE); 149 if (!ch->r_irq) { 150 ata_printf(ch, -1, "unable to allocate interrupt\n"); 151 return ENXIO; 152 } 153 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, 154 ata_interrupt, ch, &ch->ih))) { 155 ata_printf(ch, -1, "unable to setup interrupt\n"); 156 return error; 157 } 158 159 /* initialize queue and associated lock */ 160 bzero(&ch->queue_mtx, sizeof(struct mtx)); 161 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF); 162 TAILQ_INIT(&ch->ata_queue); 163 164 /* do not attach devices if we are in early boot */ 165 if (ata_delayed_attach) 166 return 0; 167 168 ata_identify_devices(ch); 169 170 if (ch->device[MASTER].attach) 171 ch->device[MASTER].attach(&ch->device[MASTER]); 172 if (ch->device[SLAVE].attach) 173 ch->device[SLAVE].attach(&ch->device[SLAVE]); 174 #ifdef DEV_ATAPICAM 175 atapi_cam_attach_bus(ch); 176 #endif 177 return 0; 178 } 179 180 int 181 ata_detach(device_t dev) 182 { 183 struct ata_channel *ch; 184 185 if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq) 186 return ENXIO; 187 188 /* mark devices on this channel as detaching */ 189 ch->device[MASTER].flags |= ATA_D_DETACHING; 190 ch->device[SLAVE].flags |= ATA_D_DETACHING; 191 192 /* fail outstanding requests on this channel */ 193 ata_fail_requests(ch, NULL); 194 195 /* unlock the channel */ 196 mtx_lock(&ch->state_mtx); 197 ch->state = ATA_IDLE; 198 mtx_unlock(&ch->state_mtx); 199 ch->locking(ch, ATA_LF_UNLOCK); 200 201 /* detach devices on this channel */ 202 if (ch->device[MASTER].detach) 203 ch->device[MASTER].detach(&ch->device[MASTER]); 204 if (ch->device[SLAVE].detach) 205 ch->device[SLAVE].detach(&ch->device[SLAVE]); 206 #ifdef DEV_ATAPICAM 207 atapi_cam_detach_bus(ch); 208 #endif 209 210 /* flush cache and powerdown device */ 211 if (ch->device[MASTER].param) { 212 if (ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 213 ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0); 214 ata_controlcmd(&ch->device[MASTER], ATA_SLEEP, 0, 0, 0); 215 free(ch->device[MASTER].param, M_ATA); 216 ch->device[MASTER].param = NULL; 217 } 218 if (ch->device[SLAVE].param) { 219 if (ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 220 ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0); 221 ata_controlcmd(&ch->device[SLAVE], ATA_SLEEP, 0, 0, 0); 222 free(ch->device[SLAVE].param, M_ATA); 223 ch->device[SLAVE].param = NULL; 224 } 225 ch->device[MASTER].mode = ATA_PIO; 226 ch->device[SLAVE].mode = ATA_PIO; 227 ch->devices = 0; 228 229 bus_teardown_intr(dev, ch->r_irq, ch->ih); 230 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 231 ch->r_irq = NULL; 232 mtx_destroy(&ch->queue_mtx); 233 return 0; 234 } 235 236 int 237 ata_reinit(struct ata_channel *ch) 238 { 239 int devices, misdev, newdev; 240 241 if (!ch->r_irq) 242 return ENXIO; 243 244 if (bootverbose) 245 ata_printf(ch, -1, "reiniting channel ..\n"); 246 247 /* poll for locking of this channel */ 248 while (ch->locking(ch, ATA_LF_LOCK) != ch->unit) 249 tsleep(&devices, PRIBIO, "atarint", 1); 250 251 ata_catch_inflight(ch); 252 253 /* grap the channel lock no matter what */ 254 mtx_lock(&ch->state_mtx); 255 ch->state = ATA_ACTIVE; 256 mtx_unlock(&ch->state_mtx); 257 258 if (ch->flags & ATA_IMMEDIATE_MODE) 259 return EIO; 260 else 261 ch->flags |= ATA_IMMEDIATE_MODE; 262 263 devices = ch->devices; 264 265 ch->hw.reset(ch); 266 267 if (bootverbose) 268 ata_printf(ch, -1, "resetting done ..\n"); 269 270 /* detach what left the channel during reset */ 271 if ((misdev = devices & ~ch->devices)) { 272 if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 273 ch->device[MASTER].detach) { 274 ata_fail_requests(ch, &ch->device[MASTER]); 275 ch->device[MASTER].detach(&ch->device[MASTER]); 276 free(ch->device[MASTER].param, M_ATA); 277 ch->device[MASTER].param = NULL; 278 } 279 if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 280 ch->device[SLAVE].detach) { 281 ata_fail_requests(ch, &ch->device[SLAVE]); 282 ch->device[SLAVE].detach(&ch->device[SLAVE]); 283 free(ch->device[SLAVE].param, M_ATA); 284 ch->device[SLAVE].param = NULL; 285 } 286 } 287 288 /* identify what is present on the channel now */ 289 ata_identify_devices(ch); 290 291 /* detach what left the channel during identify */ 292 if ((misdev = devices & ~ch->devices)) { 293 if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 294 ch->device[MASTER].detach) { 295 ata_fail_requests(ch, &ch->device[MASTER]); 296 ch->device[MASTER].detach(&ch->device[MASTER]); 297 free(ch->device[MASTER].param, M_ATA); 298 ch->device[MASTER].param = NULL; 299 } 300 if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 301 ch->device[SLAVE].detach) { 302 ata_fail_requests(ch, &ch->device[SLAVE]); 303 ch->device[SLAVE].detach(&ch->device[SLAVE]); 304 free(ch->device[SLAVE].param, M_ATA); 305 ch->device[SLAVE].param = NULL; 306 } 307 } 308 309 ch->flags &= ~ATA_IMMEDIATE_MODE; 310 mtx_lock(&ch->state_mtx); 311 ch->state = ATA_IDLE; 312 mtx_unlock(&ch->state_mtx); 313 ch->locking(ch, ATA_LF_UNLOCK); 314 315 /* attach new devices */ 316 if ((newdev = ~devices & ch->devices)) { 317 if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 318 ch->device[MASTER].attach) 319 ch->device[MASTER].attach(&ch->device[MASTER]); 320 if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 321 ch->device[SLAVE].attach) 322 ch->device[SLAVE].attach(&ch->device[SLAVE]); 323 } 324 325 #ifdef DEV_ATAPICAM 326 atapi_cam_reinit_bus(ch); 327 #endif 328 329 if (bootverbose) 330 ata_printf(ch, -1, "device config done ..\n"); 331 332 ata_start(ch); 333 return 0; 334 } 335 336 int 337 ata_suspend(device_t dev) 338 { 339 struct ata_channel *ch; 340 341 if (!dev || !(ch = device_get_softc(dev))) 342 return ENXIO; 343 344 while (1) { 345 mtx_lock(&ch->state_mtx); 346 if (ch->state == ATA_IDLE) { 347 ch->state = ATA_ACTIVE; 348 mtx_unlock(&ch->state_mtx); 349 break; 350 } 351 mtx_unlock(&ch->state_mtx); 352 tsleep(ch, PRIBIO, "atasusp", hz/10); 353 } 354 ch->locking(ch, ATA_LF_UNLOCK); 355 return 0; 356 } 357 358 int 359 ata_resume(device_t dev) 360 { 361 struct ata_channel *ch; 362 int error; 363 364 if (!dev || !(ch = device_get_softc(dev))) 365 return ENXIO; 366 367 error = ata_reinit(ch); 368 ata_start(ch); 369 return error; 370 } 371 372 static void 373 ata_shutdown(void *arg, int howto) 374 { 375 struct ata_channel *ch; 376 int ctlr; 377 378 /* flush cache on all devices */ 379 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) { 380 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 381 continue; 382 if (ch->device[MASTER].param && 383 ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 384 ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0); 385 if (ch->device[SLAVE].param && 386 ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 387 ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0); 388 } 389 } 390 391 static void 392 ata_interrupt(void *data) 393 { 394 struct ata_channel *ch = (struct ata_channel *)data; 395 struct ata_request *request; 396 397 mtx_lock(&ch->state_mtx); 398 do { 399 /* do we have a running request */ 400 if (!(request = ch->running)) 401 break; 402 403 ATA_DEBUG_RQ(request, "interrupt"); 404 405 /* ignore interrupt if device is busy */ 406 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 407 DELAY(100); 408 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 409 break; 410 } 411 412 /* check for the right state */ 413 if (ch->state == ATA_ACTIVE) { 414 request->flags |= ATA_R_INTR_SEEN; 415 ch->state = ATA_INTERRUPT; 416 } 417 else { 418 ata_prtdev(request->device, 419 "interrupt state=%d unexpected\n", ch->state); 420 break; 421 } 422 423 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) { 424 ch->running = NULL; 425 if (ch->flags & ATA_IMMEDIATE_MODE) 426 ch->state = ATA_ACTIVE; 427 else 428 ch->state = ATA_IDLE; 429 mtx_unlock(&ch->state_mtx); 430 ch->locking(ch, ATA_LF_UNLOCK); 431 ata_finish(request); 432 return; 433 } 434 else { 435 request->flags &= ~ATA_R_INTR_SEEN; 436 ch->state = ATA_ACTIVE; 437 } 438 } while (0); 439 mtx_unlock(&ch->state_mtx); 440 } 441 442 /* 443 * device related interfaces 444 */ 445 static int 446 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 447 int32_t flag, struct thread *td) 448 { 449 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 450 device_t device = devclass_get_device(ata_devclass, iocmd->channel); 451 struct ata_channel *ch; 452 struct ata_device *atadev; 453 struct ata_request *request; 454 caddr_t buf; 455 int error = ENOTTY; 456 457 if (cmd != IOCATA) 458 return error; 459 460 DROP_GIANT(); 461 switch (iocmd->cmd) { 462 case ATAGMAXCHANNEL: 463 iocmd->u.maxchan = devclass_get_maxunit(ata_devclass); 464 error = 0; 465 break; 466 467 case ATAGPARM: 468 if (!device || !(ch = device_get_softc(device))) { 469 error = ENXIO; 470 break; 471 } 472 iocmd->u.param.type[MASTER] = 473 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 474 iocmd->u.param.type[SLAVE] = 475 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 476 if (ch->device[MASTER].name) 477 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 478 if (ch->device[SLAVE].name) 479 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 480 if (ch->device[MASTER].param) 481 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 482 sizeof(struct ata_params)); 483 if (ch->device[SLAVE].param) 484 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 485 sizeof(struct ata_params)); 486 error = 0; 487 break; 488 489 case ATAGMODE: 490 if (!device || !(ch = device_get_softc(device))) { 491 error = ENXIO; 492 break; 493 } 494 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 495 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 496 error = 0; 497 break; 498 499 case ATASMODE: 500 if (!device || !(ch = device_get_softc(device))) { 501 error = ENXIO; 502 break; 503 } 504 if (iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) 505 ch->device[MASTER].setmode(&ch->device[MASTER], 506 iocmd->u.mode.mode[MASTER]); 507 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 508 if (iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) 509 ch->device[SLAVE].setmode(&ch->device[SLAVE], 510 iocmd->u.mode.mode[SLAVE]); 511 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 512 error = 0; 513 break; 514 515 case ATAREQUEST: 516 if (!device || !(ch = device_get_softc(device))) { 517 error = ENXIO; 518 break; 519 } 520 if (!(atadev = &ch->device[iocmd->device])) { 521 error = ENODEV; 522 break; 523 } 524 if (!(buf = malloc(iocmd->u.request.count, M_ATA, M_NOWAIT))) { 525 error = ENOMEM; 526 break; 527 } 528 if (!(request = ata_alloc_request())) { 529 error = ENOMEM; 530 free(buf, M_ATA); 531 break; 532 } 533 if (iocmd->u.request.flags & ATA_CMD_WRITE) { 534 error = copyin(iocmd->u.request.data, buf, iocmd->u.request.count); 535 if (error) { 536 free(buf, M_ATA); 537 ata_free_request(request); 538 break; 539 } 540 } 541 542 request->device = atadev; 543 544 if (iocmd->u.request.flags & ATA_CMD_ATAPI) { 545 request->flags = ATA_R_ATAPI; 546 bcopy(iocmd->u.request.u.atapi.ccb, request->u.atapi.ccb, 16); 547 } 548 else { 549 request->u.ata.command = iocmd->u.request.u.ata.command; 550 request->u.ata.feature = iocmd->u.request.u.ata.feature; 551 request->u.ata.lba = iocmd->u.request.u.ata.lba; 552 request->u.ata.count = iocmd->u.request.u.ata.count; 553 } 554 555 request->timeout = iocmd->u.request.timeout; 556 request->data = buf; 557 request->bytecount = iocmd->u.request.count; 558 request->transfersize = request->bytecount; 559 560 if (iocmd->u.request.flags & ATA_CMD_CONTROL) 561 request->flags |= ATA_R_CONTROL; 562 if (iocmd->u.request.flags & ATA_CMD_READ) 563 request->flags |= ATA_R_READ; 564 if (iocmd->u.request.flags & ATA_CMD_WRITE) 565 request->flags |= ATA_R_WRITE; 566 567 ata_queue_request(request); 568 569 if (request->result) 570 iocmd->u.request.error = request->result; 571 else { 572 if (iocmd->u.request.flags & ATA_CMD_READ) 573 error = copyout(buf, 574 iocmd->u.request.data, iocmd->u.request.count); 575 else 576 error = 0; 577 } 578 free(buf, M_ATA); 579 ata_free_request(request); 580 break; 581 582 case ATAREINIT: 583 if (!device || !(ch = device_get_softc(device))) { 584 error = ENXIO; 585 break; 586 } 587 error = ata_reinit(ch); 588 ata_start(ch); 589 break; 590 591 case ATAATTACH: 592 if (!device) { 593 error = ENXIO; 594 break; 595 } 596 /* SOS should enable channel HW on controller XXX */ 597 error = ata_probe(device); 598 if (!error) 599 error = ata_attach(device); 600 break; 601 602 case ATADETACH: 603 if (!device) { 604 error = ENXIO; 605 break; 606 } 607 error = ata_detach(device); 608 /* SOS should disable channel HW on controller XXX */ 609 break; 610 611 612 #ifdef DEV_ATARAID 613 case ATARAIDCREATE: 614 error = ata_raid_create(&iocmd->u.raid_setup); 615 break; 616 617 case ATARAIDDELETE: 618 error = ata_raid_delete(iocmd->channel); 619 break; 620 621 case ATARAIDSTATUS: 622 error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 623 break; 624 625 case ATARAIDADDSPARE: 626 error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk); 627 break; 628 629 case ATARAIDREBUILD: 630 error = ata_raid_rebuild(iocmd->channel); 631 break; 632 #endif 633 } 634 PICKUP_GIANT(); 635 return error; 636 } 637 638 /* 639 * device probe functions 640 */ 641 static int 642 ata_getparam(struct ata_device *atadev, u_int8_t command) 643 { 644 struct ata_request *request; 645 int error = ENOMEM; 646 647 if (!atadev->param) 648 atadev->param = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 649 if (atadev->param) { 650 request = ata_alloc_request(); 651 if (request) { 652 int retries = 2; 653 while (retries-- > 0) { 654 request->device = atadev; 655 request->timeout = 5; 656 request->retries = 0; 657 request->u.ata.command = command; 658 request->flags = (ATA_R_READ | ATA_R_IMMEDIATE); 659 request->data = (caddr_t)atadev->param; 660 request->bytecount = sizeof(struct ata_params); 661 request->donecount = 0; 662 request->transfersize = DEV_BSIZE; 663 ata_queue_request(request); 664 if (!(error = request->result)) 665 break; 666 } 667 ata_free_request(request); 668 } 669 if (!error && (isprint(atadev->param->model[0]) || 670 isprint(atadev->param->model[1]))) { 671 struct ata_params *atacap = atadev->param; 672 #if BYTE_ORDER == BIG_ENDIAN 673 int16_t *ptr; 674 675 for (ptr = (int16_t *)atacap; 676 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 677 *ptr = bswap16(*ptr); 678 } 679 #endif 680 if (!(!strncmp(atacap->model, "FX", 2) || 681 !strncmp(atacap->model, "NEC", 3) || 682 !strncmp(atacap->model, "Pioneer", 7) || 683 !strncmp(atacap->model, "SHARP", 5))) { 684 bswap(atacap->model, sizeof(atacap->model)); 685 bswap(atacap->revision, sizeof(atacap->revision)); 686 bswap(atacap->serial, sizeof(atacap->serial)); 687 } 688 btrim(atacap->model, sizeof(atacap->model)); 689 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 690 btrim(atacap->revision, sizeof(atacap->revision)); 691 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 692 btrim(atacap->serial, sizeof(atacap->serial)); 693 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 694 if (bootverbose) 695 ata_prtdev(atadev, 696 "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n", 697 ata_pmode(atacap), ata_wmode(atacap), 698 ata_umode(atacap), 699 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 700 } 701 else { 702 if (!error) 703 error = ENXIO; 704 if (atadev->param) { 705 free(atadev->param, M_ATA); 706 atadev->param = NULL; 707 } 708 } 709 } 710 return error; 711 } 712 713 static void 714 ata_identify_devices(struct ata_channel *ch) 715 { 716 if (ch->devices & ATA_ATA_SLAVE) { 717 if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY)) 718 ch->devices &= ~ATA_ATA_SLAVE; 719 #ifdef DEV_ATADISK 720 else 721 ch->device[SLAVE].attach = ad_attach; 722 #endif 723 } 724 if (ch->devices & ATA_ATAPI_SLAVE) { 725 if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY)) 726 ch->devices &= ~ATA_ATAPI_SLAVE; 727 else { 728 ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0); 729 switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) { 730 #ifdef DEV_ATAPICD 731 case ATA_ATAPI_TYPE_CDROM: 732 ch->device[SLAVE].attach = acd_attach; 733 break; 734 #endif 735 #ifdef DEV_ATAPIFD 736 case ATA_ATAPI_TYPE_DIRECT: 737 ch->device[SLAVE].attach = afd_attach; 738 break; 739 #endif 740 #ifdef DEV_ATAPIST 741 case ATA_ATAPI_TYPE_TAPE: 742 ch->device[SLAVE].attach = ast_attach; 743 break; 744 #endif 745 } 746 } 747 } 748 if (ch->devices & ATA_ATA_MASTER) { 749 if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY)) 750 ch->devices &= ~ATA_ATA_MASTER; 751 #ifdef DEV_ATADISK 752 else 753 ch->device[MASTER].attach = ad_attach; 754 #endif 755 } 756 if (ch->devices & ATA_ATAPI_MASTER) { 757 if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY)) 758 ch->devices &= ~ATA_ATAPI_MASTER; 759 else { 760 ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0); 761 switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) { 762 #ifdef DEV_ATAPICD 763 case ATA_ATAPI_TYPE_CDROM: 764 ch->device[MASTER].attach = acd_attach; 765 break; 766 #endif 767 #ifdef DEV_ATAPIFD 768 case ATA_ATAPI_TYPE_DIRECT: 769 ch->device[MASTER].attach = afd_attach; 770 break; 771 #endif 772 #ifdef DEV_ATAPIST 773 case ATA_ATAPI_TYPE_TAPE: 774 ch->device[MASTER].attach = ast_attach; 775 break; 776 #endif 777 } 778 } 779 } 780 781 /* setup basic transfer mode by setting PIO mode and DMA if supported */ 782 if (ch->device[MASTER].param) { 783 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX); 784 if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma && 785 (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR && 786 ata_umode(ch->device[MASTER].param) >= ATA_UDMA2) || 787 ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma) 788 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX); 789 790 } 791 if (ch->device[SLAVE].param) { 792 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX); 793 if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma && 794 (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR && 795 ata_umode(ch->device[SLAVE].param) >= ATA_UDMA2) || 796 ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma) 797 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX); 798 } 799 } 800 801 static void 802 ata_boot_attach(void) 803 { 804 struct ata_channel *ch; 805 int ctlr; 806 807 if (ata_delayed_attach) { 808 config_intrhook_disestablish(ata_delayed_attach); 809 free(ata_delayed_attach, M_TEMP); 810 ata_delayed_attach = NULL; 811 } 812 813 /* 814 * run through all ata devices and look for real ATA & ATAPI devices 815 * using the hints we found in the early probe, this avoids some of 816 * the delays probing of non-exsistent devices can cause. 817 */ 818 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 819 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 820 continue; 821 ata_identify_devices(ch); 822 if (ch->device[MASTER].attach) 823 ch->device[MASTER].attach(&ch->device[MASTER]); 824 if (ch->device[SLAVE].attach) 825 ch->device[SLAVE].attach(&ch->device[SLAVE]); 826 #ifdef DEV_ATAPICAM 827 atapi_cam_attach_bus(ch); 828 #endif 829 } 830 #ifdef DEV_ATARAID 831 ata_raid_attach(); 832 #endif 833 } 834 835 /* 836 * misc support functions 837 */ 838 void 839 ata_udelay(int interval) 840 { 841 if (interval < (1000000/hz) || ata_delayed_attach) 842 DELAY(interval); 843 else 844 tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz)); 845 } 846 847 static void 848 bswap(int8_t *buf, int len) 849 { 850 u_int16_t *ptr = (u_int16_t*)(buf + len); 851 852 while (--ptr >= (u_int16_t*)buf) 853 *ptr = ntohs(*ptr); 854 } 855 856 static void 857 btrim(int8_t *buf, int len) 858 { 859 int8_t *ptr; 860 861 for (ptr = buf; ptr < buf+len; ++ptr) 862 if (!*ptr || *ptr == '_') 863 *ptr = ' '; 864 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 865 *ptr = 0; 866 } 867 868 static void 869 bpack(int8_t *src, int8_t *dst, int len) 870 { 871 int i, j, blank; 872 873 for (i = j = blank = 0 ; i < len; i++) { 874 if (blank && src[i] == ' ') continue; 875 if (blank && src[i] != ' ') { 876 dst[j++] = src[i]; 877 blank = 0; 878 continue; 879 } 880 if (src[i] == ' ') { 881 blank = 1; 882 if (i == 0) 883 continue; 884 } 885 dst[j++] = src[i]; 886 } 887 if (j < len) 888 dst[j] = 0x00; 889 } 890 891 int 892 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 893 { 894 va_list ap; 895 int ret; 896 897 if (device == -1) 898 ret = printf("ata%d: ", device_get_unit(ch->dev)); 899 else { 900 if (ch->device[ATA_DEV(device)].name) 901 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 902 else 903 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 904 (device == ATA_MASTER) ? "master" : "slave"); 905 } 906 va_start(ap, fmt); 907 ret += vprintf(fmt, ap); 908 va_end(ap); 909 return ret; 910 } 911 912 int 913 ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 914 { 915 va_list ap; 916 int ret; 917 918 if (atadev->name) 919 ret = printf("%s: ", atadev->name); 920 else 921 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 922 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 923 va_start(ap, fmt); 924 ret += vprintf(fmt, ap); 925 va_end(ap); 926 return ret; 927 } 928 929 void 930 ata_set_name(struct ata_device *atadev, char *name, int lun) 931 { 932 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 933 if (atadev->name) 934 sprintf(atadev->name, "%s%d", name, lun); 935 } 936 937 void 938 ata_free_name(struct ata_device *atadev) 939 { 940 if (atadev->name) 941 free(atadev->name, M_ATA); 942 atadev->name = NULL; 943 } 944 945 int 946 ata_get_lun(u_int32_t *map) 947 { 948 int lun = ffs(~*map) - 1; 949 950 *map |= (1 << lun); 951 return lun; 952 } 953 954 int 955 ata_test_lun(u_int32_t *map, int lun) 956 { 957 return (*map & (1 << lun)); 958 } 959 960 void 961 ata_free_lun(u_int32_t *map, int lun) 962 { 963 *map &= ~(1 << lun); 964 } 965 966 char * 967 ata_mode2str(int mode) 968 { 969 switch (mode) { 970 case ATA_PIO: return "BIOSPIO"; 971 case ATA_PIO0: return "PIO0"; 972 case ATA_PIO1: return "PIO1"; 973 case ATA_PIO2: return "PIO2"; 974 case ATA_PIO3: return "PIO3"; 975 case ATA_PIO4: return "PIO4"; 976 case ATA_DMA: return "BIOSDMA"; 977 case ATA_WDMA0: return "WDMA0"; 978 case ATA_WDMA1: return "WDMA1"; 979 case ATA_WDMA2: return "WDMA2"; 980 case ATA_UDMA0: return "UDMA16"; 981 case ATA_UDMA1: return "UDMA25"; 982 case ATA_UDMA2: return "UDMA33"; 983 case ATA_UDMA3: return "UDMA40"; 984 case ATA_UDMA4: return "UDMA66"; 985 case ATA_UDMA5: return "UDMA100"; 986 case ATA_UDMA6: return "UDMA133"; 987 case ATA_SA150: return "SATA150"; 988 default: return "???"; 989 } 990 } 991 992 int 993 ata_pmode(struct ata_params *ap) 994 { 995 if (ap->atavalid & ATA_FLAG_64_70) { 996 if (ap->apiomodes & 0x02) 997 return ATA_PIO4; 998 if (ap->apiomodes & 0x01) 999 return ATA_PIO3; 1000 } 1001 if (ap->mwdmamodes & 0x04) 1002 return ATA_PIO4; 1003 if (ap->mwdmamodes & 0x02) 1004 return ATA_PIO3; 1005 if (ap->mwdmamodes & 0x01) 1006 return ATA_PIO2; 1007 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 1008 return ATA_PIO2; 1009 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 1010 return ATA_PIO1; 1011 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 1012 return ATA_PIO0; 1013 return ATA_PIO0; 1014 } 1015 1016 int 1017 ata_wmode(struct ata_params *ap) 1018 { 1019 if (ap->mwdmamodes & 0x04) 1020 return ATA_WDMA2; 1021 if (ap->mwdmamodes & 0x02) 1022 return ATA_WDMA1; 1023 if (ap->mwdmamodes & 0x01) 1024 return ATA_WDMA0; 1025 return -1; 1026 } 1027 1028 int 1029 ata_umode(struct ata_params *ap) 1030 { 1031 if (ap->atavalid & ATA_FLAG_88) { 1032 if (ap->udmamodes & 0x40) 1033 return ATA_UDMA6; 1034 if (ap->udmamodes & 0x20) 1035 return ATA_UDMA5; 1036 if (ap->udmamodes & 0x10) 1037 return ATA_UDMA4; 1038 if (ap->udmamodes & 0x08) 1039 return ATA_UDMA3; 1040 if (ap->udmamodes & 0x04) 1041 return ATA_UDMA2; 1042 if (ap->udmamodes & 0x02) 1043 return ATA_UDMA1; 1044 if (ap->udmamodes & 0x01) 1045 return ATA_UDMA0; 1046 } 1047 return -1; 1048 } 1049 1050 int 1051 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 1052 { 1053 if (maxmode && mode > maxmode) 1054 mode = maxmode; 1055 1056 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 1057 return min(mode, ata_umode(atadev->param)); 1058 1059 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 1060 return min(mode, ata_wmode(atadev->param)); 1061 1062 if (mode > ata_pmode(atadev->param)) 1063 return min(mode, ata_pmode(atadev->param)); 1064 1065 return mode; 1066 } 1067 1068 static void 1069 ata_init(void) 1070 { 1071 /* register controlling device */ 1072 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1073 1074 /* register boot attach to be run when interrupts are enabled */ 1075 if (!(ata_delayed_attach = (struct intr_config_hook *) 1076 malloc(sizeof(struct intr_config_hook), 1077 M_TEMP, M_NOWAIT | M_ZERO))) { 1078 printf("ata: malloc of delayed attach hook failed\n"); 1079 return; 1080 } 1081 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1082 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1083 printf("ata: config_intrhook_establish failed\n"); 1084 free(ata_delayed_attach, M_TEMP); 1085 } 1086 1087 /* register handler to flush write caches on shutdown */ 1088 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown, 1089 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1090 printf("ata: shutdown event registration failed!\n"); 1091 1092 /* init our UMA zone for ATA requests */ 1093 ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request), 1094 NULL, NULL, NULL, NULL, 0, 0); 1095 } 1096 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1097