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 iocmd->u.request.u.ata.command = request->u.ata.command; 570 iocmd->u.request.u.ata.feature = request->u.ata.feature; 571 iocmd->u.request.u.ata.lba = request->u.ata.lba; 572 iocmd->u.request.u.ata.count = request->u.ata.count; 573 if (request->result) 574 iocmd->u.request.error = request->result; 575 else { 576 if (iocmd->u.request.flags & ATA_CMD_READ) 577 error = copyout(buf, 578 iocmd->u.request.data, iocmd->u.request.count); 579 else 580 error = 0; 581 } 582 free(buf, M_ATA); 583 ata_free_request(request); 584 break; 585 586 case ATAREINIT: 587 if (!device || !(ch = device_get_softc(device))) { 588 error = ENXIO; 589 break; 590 } 591 error = ata_reinit(ch); 592 ata_start(ch); 593 break; 594 595 case ATAATTACH: 596 if (!device) { 597 error = ENXIO; 598 break; 599 } 600 /* SOS should enable channel HW on controller XXX */ 601 error = ata_probe(device); 602 if (!error) 603 error = ata_attach(device); 604 break; 605 606 case ATADETACH: 607 if (!device) { 608 error = ENXIO; 609 break; 610 } 611 error = ata_detach(device); 612 /* SOS should disable channel HW on controller XXX */ 613 break; 614 615 616 #ifdef DEV_ATARAID 617 case ATARAIDCREATE: 618 error = ata_raid_create(&iocmd->u.raid_setup); 619 break; 620 621 case ATARAIDDELETE: 622 error = ata_raid_delete(iocmd->channel); 623 break; 624 625 case ATARAIDSTATUS: 626 error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 627 break; 628 629 case ATARAIDADDSPARE: 630 error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk); 631 break; 632 633 case ATARAIDREBUILD: 634 error = ata_raid_rebuild(iocmd->channel); 635 break; 636 #endif 637 } 638 PICKUP_GIANT(); 639 return error; 640 } 641 642 /* 643 * device probe functions 644 */ 645 static int 646 ata_getparam(struct ata_device *atadev, u_int8_t command) 647 { 648 struct ata_request *request; 649 int error = ENOMEM; 650 651 if (!atadev->param) 652 atadev->param = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 653 if (atadev->param) { 654 request = ata_alloc_request(); 655 if (request) { 656 int retries = 2; 657 while (retries-- > 0) { 658 request->device = atadev; 659 request->timeout = 5; 660 request->retries = 0; 661 request->u.ata.command = command; 662 request->flags = (ATA_R_READ | ATA_R_IMMEDIATE); 663 request->data = (caddr_t)atadev->param; 664 request->bytecount = sizeof(struct ata_params); 665 request->donecount = 0; 666 request->transfersize = DEV_BSIZE; 667 ata_queue_request(request); 668 if (!(error = request->result)) 669 break; 670 } 671 ata_free_request(request); 672 } 673 if (!error && (isprint(atadev->param->model[0]) || 674 isprint(atadev->param->model[1]))) { 675 struct ata_params *atacap = atadev->param; 676 #if BYTE_ORDER == BIG_ENDIAN 677 int16_t *ptr; 678 679 for (ptr = (int16_t *)atacap; 680 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 681 *ptr = bswap16(*ptr); 682 } 683 #endif 684 if (!(!strncmp(atacap->model, "FX", 2) || 685 !strncmp(atacap->model, "NEC", 3) || 686 !strncmp(atacap->model, "Pioneer", 7) || 687 !strncmp(atacap->model, "SHARP", 5))) { 688 bswap(atacap->model, sizeof(atacap->model)); 689 bswap(atacap->revision, sizeof(atacap->revision)); 690 bswap(atacap->serial, sizeof(atacap->serial)); 691 } 692 btrim(atacap->model, sizeof(atacap->model)); 693 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 694 btrim(atacap->revision, sizeof(atacap->revision)); 695 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 696 btrim(atacap->serial, sizeof(atacap->serial)); 697 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 698 if (bootverbose) 699 ata_prtdev(atadev, 700 "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n", 701 ata_pmode(atacap), ata_wmode(atacap), 702 ata_umode(atacap), 703 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 704 } 705 else { 706 if (!error) 707 error = ENXIO; 708 if (atadev->param) { 709 free(atadev->param, M_ATA); 710 atadev->param = NULL; 711 } 712 } 713 } 714 return error; 715 } 716 717 static void 718 ata_identify_devices(struct ata_channel *ch) 719 { 720 if (ch->devices & ATA_ATA_SLAVE) { 721 if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY)) 722 ch->devices &= ~ATA_ATA_SLAVE; 723 #ifdef DEV_ATADISK 724 else 725 ch->device[SLAVE].attach = ad_attach; 726 #endif 727 } 728 if (ch->devices & ATA_ATAPI_SLAVE) { 729 if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY)) 730 ch->devices &= ~ATA_ATAPI_SLAVE; 731 else { 732 ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0); 733 switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) { 734 #ifdef DEV_ATAPICD 735 case ATA_ATAPI_TYPE_CDROM: 736 ch->device[SLAVE].attach = acd_attach; 737 break; 738 #endif 739 #ifdef DEV_ATAPIFD 740 case ATA_ATAPI_TYPE_DIRECT: 741 ch->device[SLAVE].attach = afd_attach; 742 break; 743 #endif 744 #ifdef DEV_ATAPIST 745 case ATA_ATAPI_TYPE_TAPE: 746 ch->device[SLAVE].attach = ast_attach; 747 break; 748 #endif 749 } 750 } 751 } 752 if (ch->devices & ATA_ATA_MASTER) { 753 if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY)) 754 ch->devices &= ~ATA_ATA_MASTER; 755 #ifdef DEV_ATADISK 756 else 757 ch->device[MASTER].attach = ad_attach; 758 #endif 759 } 760 if (ch->devices & ATA_ATAPI_MASTER) { 761 if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY)) 762 ch->devices &= ~ATA_ATAPI_MASTER; 763 else { 764 ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0); 765 switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) { 766 #ifdef DEV_ATAPICD 767 case ATA_ATAPI_TYPE_CDROM: 768 ch->device[MASTER].attach = acd_attach; 769 break; 770 #endif 771 #ifdef DEV_ATAPIFD 772 case ATA_ATAPI_TYPE_DIRECT: 773 ch->device[MASTER].attach = afd_attach; 774 break; 775 #endif 776 #ifdef DEV_ATAPIST 777 case ATA_ATAPI_TYPE_TAPE: 778 ch->device[MASTER].attach = ast_attach; 779 break; 780 #endif 781 } 782 } 783 } 784 785 /* setup basic transfer mode by setting PIO mode and DMA if supported */ 786 if (ch->device[MASTER].param) { 787 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX); 788 if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma && 789 (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR && 790 ata_umode(ch->device[MASTER].param) >= ATA_UDMA2) || 791 ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma) 792 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX); 793 794 } 795 if (ch->device[SLAVE].param) { 796 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX); 797 if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma && 798 (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR && 799 ata_umode(ch->device[SLAVE].param) >= ATA_UDMA2) || 800 ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma) 801 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX); 802 } 803 } 804 805 static void 806 ata_boot_attach(void) 807 { 808 struct ata_channel *ch; 809 int ctlr; 810 811 if (ata_delayed_attach) { 812 config_intrhook_disestablish(ata_delayed_attach); 813 free(ata_delayed_attach, M_TEMP); 814 ata_delayed_attach = NULL; 815 } 816 817 /* 818 * run through all ata devices and look for real ATA & ATAPI devices 819 * using the hints we found in the early probe, this avoids some of 820 * the delays probing of non-exsistent devices can cause. 821 */ 822 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 823 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 824 continue; 825 ata_identify_devices(ch); 826 if (ch->device[MASTER].attach) 827 ch->device[MASTER].attach(&ch->device[MASTER]); 828 if (ch->device[SLAVE].attach) 829 ch->device[SLAVE].attach(&ch->device[SLAVE]); 830 #ifdef DEV_ATAPICAM 831 atapi_cam_attach_bus(ch); 832 #endif 833 } 834 #ifdef DEV_ATARAID 835 ata_raid_attach(); 836 #endif 837 } 838 839 /* 840 * misc support functions 841 */ 842 void 843 ata_udelay(int interval) 844 { 845 if (interval < (1000000/hz) || ata_delayed_attach) 846 DELAY(interval); 847 else 848 tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz)); 849 } 850 851 static void 852 bswap(int8_t *buf, int len) 853 { 854 u_int16_t *ptr = (u_int16_t*)(buf + len); 855 856 while (--ptr >= (u_int16_t*)buf) 857 *ptr = ntohs(*ptr); 858 } 859 860 static void 861 btrim(int8_t *buf, int len) 862 { 863 int8_t *ptr; 864 865 for (ptr = buf; ptr < buf+len; ++ptr) 866 if (!*ptr || *ptr == '_') 867 *ptr = ' '; 868 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 869 *ptr = 0; 870 } 871 872 static void 873 bpack(int8_t *src, int8_t *dst, int len) 874 { 875 int i, j, blank; 876 877 for (i = j = blank = 0 ; i < len; i++) { 878 if (blank && src[i] == ' ') continue; 879 if (blank && src[i] != ' ') { 880 dst[j++] = src[i]; 881 blank = 0; 882 continue; 883 } 884 if (src[i] == ' ') { 885 blank = 1; 886 if (i == 0) 887 continue; 888 } 889 dst[j++] = src[i]; 890 } 891 if (j < len) 892 dst[j] = 0x00; 893 } 894 895 int 896 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 897 { 898 va_list ap; 899 int ret; 900 901 if (device == -1) 902 ret = printf("ata%d: ", device_get_unit(ch->dev)); 903 else { 904 if (ch->device[ATA_DEV(device)].name) 905 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 906 else 907 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 908 (device == ATA_MASTER) ? "master" : "slave"); 909 } 910 va_start(ap, fmt); 911 ret += vprintf(fmt, ap); 912 va_end(ap); 913 return ret; 914 } 915 916 int 917 ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 918 { 919 va_list ap; 920 int ret; 921 922 if (atadev->name) 923 ret = printf("%s: ", atadev->name); 924 else 925 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 926 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 927 va_start(ap, fmt); 928 ret += vprintf(fmt, ap); 929 va_end(ap); 930 return ret; 931 } 932 933 void 934 ata_set_name(struct ata_device *atadev, char *name, int lun) 935 { 936 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 937 if (atadev->name) 938 sprintf(atadev->name, "%s%d", name, lun); 939 } 940 941 void 942 ata_free_name(struct ata_device *atadev) 943 { 944 if (atadev->name) 945 free(atadev->name, M_ATA); 946 atadev->name = NULL; 947 } 948 949 int 950 ata_get_lun(u_int32_t *map) 951 { 952 int lun = ffs(~*map) - 1; 953 954 *map |= (1 << lun); 955 return lun; 956 } 957 958 int 959 ata_test_lun(u_int32_t *map, int lun) 960 { 961 return (*map & (1 << lun)); 962 } 963 964 void 965 ata_free_lun(u_int32_t *map, int lun) 966 { 967 *map &= ~(1 << lun); 968 } 969 970 char * 971 ata_mode2str(int mode) 972 { 973 switch (mode) { 974 case ATA_PIO: return "BIOSPIO"; 975 case ATA_PIO0: return "PIO0"; 976 case ATA_PIO1: return "PIO1"; 977 case ATA_PIO2: return "PIO2"; 978 case ATA_PIO3: return "PIO3"; 979 case ATA_PIO4: return "PIO4"; 980 case ATA_DMA: return "BIOSDMA"; 981 case ATA_WDMA0: return "WDMA0"; 982 case ATA_WDMA1: return "WDMA1"; 983 case ATA_WDMA2: return "WDMA2"; 984 case ATA_UDMA0: return "UDMA16"; 985 case ATA_UDMA1: return "UDMA25"; 986 case ATA_UDMA2: return "UDMA33"; 987 case ATA_UDMA3: return "UDMA40"; 988 case ATA_UDMA4: return "UDMA66"; 989 case ATA_UDMA5: return "UDMA100"; 990 case ATA_UDMA6: return "UDMA133"; 991 case ATA_SA150: return "SATA150"; 992 default: return "???"; 993 } 994 } 995 996 int 997 ata_pmode(struct ata_params *ap) 998 { 999 if (ap->atavalid & ATA_FLAG_64_70) { 1000 if (ap->apiomodes & 0x02) 1001 return ATA_PIO4; 1002 if (ap->apiomodes & 0x01) 1003 return ATA_PIO3; 1004 } 1005 if (ap->mwdmamodes & 0x04) 1006 return ATA_PIO4; 1007 if (ap->mwdmamodes & 0x02) 1008 return ATA_PIO3; 1009 if (ap->mwdmamodes & 0x01) 1010 return ATA_PIO2; 1011 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 1012 return ATA_PIO2; 1013 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 1014 return ATA_PIO1; 1015 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 1016 return ATA_PIO0; 1017 return ATA_PIO0; 1018 } 1019 1020 int 1021 ata_wmode(struct ata_params *ap) 1022 { 1023 if (ap->mwdmamodes & 0x04) 1024 return ATA_WDMA2; 1025 if (ap->mwdmamodes & 0x02) 1026 return ATA_WDMA1; 1027 if (ap->mwdmamodes & 0x01) 1028 return ATA_WDMA0; 1029 return -1; 1030 } 1031 1032 int 1033 ata_umode(struct ata_params *ap) 1034 { 1035 if (ap->atavalid & ATA_FLAG_88) { 1036 if (ap->udmamodes & 0x40) 1037 return ATA_UDMA6; 1038 if (ap->udmamodes & 0x20) 1039 return ATA_UDMA5; 1040 if (ap->udmamodes & 0x10) 1041 return ATA_UDMA4; 1042 if (ap->udmamodes & 0x08) 1043 return ATA_UDMA3; 1044 if (ap->udmamodes & 0x04) 1045 return ATA_UDMA2; 1046 if (ap->udmamodes & 0x02) 1047 return ATA_UDMA1; 1048 if (ap->udmamodes & 0x01) 1049 return ATA_UDMA0; 1050 } 1051 return -1; 1052 } 1053 1054 int 1055 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 1056 { 1057 if (maxmode && mode > maxmode) 1058 mode = maxmode; 1059 1060 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 1061 return min(mode, ata_umode(atadev->param)); 1062 1063 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 1064 return min(mode, ata_wmode(atadev->param)); 1065 1066 if (mode > ata_pmode(atadev->param)) 1067 return min(mode, ata_pmode(atadev->param)); 1068 1069 return mode; 1070 } 1071 1072 static void 1073 ata_init(void) 1074 { 1075 /* register controlling device */ 1076 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1077 1078 /* register boot attach to be run when interrupts are enabled */ 1079 if (!(ata_delayed_attach = (struct intr_config_hook *) 1080 malloc(sizeof(struct intr_config_hook), 1081 M_TEMP, M_NOWAIT | M_ZERO))) { 1082 printf("ata: malloc of delayed attach hook failed\n"); 1083 return; 1084 } 1085 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1086 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1087 printf("ata: config_intrhook_establish failed\n"); 1088 free(ata_delayed_attach, M_TEMP); 1089 } 1090 1091 /* register handler to flush write caches on shutdown */ 1092 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown, 1093 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1094 printf("ata: shutdown event registration failed!\n"); 1095 1096 /* init our UMA zone for ATA requests */ 1097 ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request), 1098 NULL, NULL, NULL, NULL, 0, 0); 1099 } 1100 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1101