1 /*- 2 * Copyright (c) 1998 - 2006 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.273 2006/05/12 05:04:40 jhb Exp $ 27 * $DragonFly: src/sys/dev/disk/nata/ata-all.c,v 1.7 2007/01/09 21:17:00 tgen Exp $ 28 */ 29 30 #include "opt_ata.h" 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/callout.h> 35 #include <sys/conf.h> 36 #include <sys/ctype.h> 37 #include <sys/device.h> 38 #include <sys/endian.h> 39 #include <sys/kernel.h> 40 #include <sys/libkern.h> 41 #include <sys/lock.h> /* for {get,rel}_mplock() */ 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/nata.h> 45 #include <sys/objcache.h> 46 #include <sys/queue.h> 47 #include <sys/spinlock2.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 51 #include "ata-all.h" 52 #include "ata_if.h" 53 54 /* device structure */ 55 static d_ioctl_t ata_ioctl; 56 static struct dev_ops ata_ops = { 57 { "ata", 159, 0 }, 58 .d_open = nullopen, 59 .d_close = nullclose, 60 .d_ioctl = ata_ioctl, 61 }; 62 63 /* prototypes */ 64 static void ata_boot_attach(void); 65 static device_t ata_add_child(device_t, struct ata_device *, int); 66 static int ata_getparam(struct ata_device *, int); 67 static void bswap(int8_t *, int); 68 static void btrim(int8_t *, int); 69 static void bpack(int8_t *, int8_t *, int); 70 71 /* global vars */ 72 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer"); 73 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL; 74 struct intr_config_hook *ata_delayed_attach = NULL; 75 devclass_t ata_devclass; 76 struct objcache *ata_request_cache; 77 struct objcache *ata_composite_cache; 78 struct objcache_malloc_args ata_request_malloc_args = { 79 sizeof(struct ata_request), M_ATA }; 80 struct objcache_malloc_args ata_composite_malloc_args = { 81 sizeof(struct ata_composite), M_ATA }; 82 int ata_wc = 1; 83 84 /* local vars */ 85 static int ata_dma = 1; 86 static int atapi_dma = 1; 87 88 /* sysctl vars */ 89 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 90 TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 91 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RW, &ata_dma, 0, 92 "ATA disk DMA mode control"); 93 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 94 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RW, &atapi_dma, 0, 95 "ATAPI device DMA mode control"); 96 TUNABLE_INT("hw.ata.wc", &ata_wc); 97 SYSCTL_INT(_hw_ata, OID_AUTO, ata_wc, CTLFLAG_RW, &ata_wc, 0, 98 "ATA disk write caching"); 99 100 /* 101 * newbus device interface related functions 102 */ 103 int 104 ata_probe(device_t dev) 105 { 106 return 0; 107 } 108 109 int 110 ata_attach(device_t dev) 111 { 112 struct ata_channel *ch = device_get_softc(dev); 113 int error, rid; 114 115 /* check that we have a virgin channel to attach */ 116 if (ch->r_irq) 117 return EEXIST; 118 119 /* initialize the softc basics */ 120 ch->dev = dev; 121 ch->state = ATA_IDLE; 122 spin_init(&ch->state_mtx); 123 spin_init(&ch->queue_mtx); 124 TAILQ_INIT(&ch->ata_queue); 125 126 /* reset the controller HW, the channel and device(s) */ 127 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit) 128 tsleep(&error, 0, "ataatch", 1); 129 ATA_RESET(dev); 130 ATA_LOCKING(dev, ATA_LF_UNLOCK); 131 132 /* setup interrupt delivery */ 133 rid = ATA_IRQ_RID; 134 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 135 RF_SHAREABLE | RF_ACTIVE); 136 if (!ch->r_irq) { 137 device_printf(dev, "unable to allocate interrupt\n"); 138 return ENXIO; 139 } 140 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, 141 (driver_intr_t *)ata_interrupt, ch, &ch->ih, 142 NULL))) { 143 device_printf(dev, "unable to setup interrupt\n"); 144 return error; 145 } 146 147 /* probe and attach devices on this channel unless we are in early boot */ 148 if (!ata_delayed_attach) 149 ata_identify(dev); 150 return 0; 151 } 152 153 int 154 ata_detach(device_t dev) 155 { 156 struct ata_channel *ch = device_get_softc(dev); 157 device_t *children; 158 int nchildren, i; 159 160 /* check that we have a valid channel to detach */ 161 if (!ch->r_irq) 162 return ENXIO; 163 164 /* grap the channel lock so no new requests gets launched */ 165 spin_lock_wr(&ch->state_mtx); 166 ch->state |= ATA_STALL_QUEUE; 167 spin_unlock_wr(&ch->state_mtx); 168 169 /* detach & delete all children */ 170 if (!device_get_children(dev, &children, &nchildren)) { 171 for (i = 0; i < nchildren; i++) 172 if (children[i]) 173 device_delete_child(dev, children[i]); 174 kfree(children, M_TEMP); 175 } 176 177 /* release resources */ 178 bus_teardown_intr(dev, ch->r_irq, ch->ih); 179 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 180 ch->r_irq = NULL; 181 spin_uninit(&ch->state_mtx); 182 spin_uninit(&ch->queue_mtx); 183 return 0; 184 } 185 186 int 187 ata_reinit(device_t dev) 188 { 189 struct ata_channel *ch = device_get_softc(dev); 190 struct ata_request *request; 191 device_t *children; 192 int nchildren, i; 193 194 /* check that we have a valid channel to reinit */ 195 if (!ch || !ch->r_irq) 196 return ENXIO; 197 198 if (bootverbose) 199 device_printf(dev, "reiniting channel ..\n"); 200 201 /* poll for locking the channel */ 202 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit) 203 tsleep(&dev, 0, "atarini", 1); 204 205 /* catch eventual request in ch->running */ 206 spin_lock_wr(&ch->state_mtx); 207 if ((request = ch->running)) 208 callout_stop(&request->callout); 209 ch->running = NULL; 210 211 /* unconditionally grap the channel lock */ 212 ch->state |= ATA_STALL_QUEUE; 213 spin_unlock_wr(&ch->state_mtx); 214 215 /* reset the controller HW, the channel and device(s) */ 216 ATA_RESET(dev); 217 218 /* reinit the children and delete any that fails */ 219 if (!device_get_children(dev, &children, &nchildren)) { 220 get_mplock(); 221 for (i = 0; i < nchildren; i++) { 222 /* did any children go missing ? */ 223 if (children[i] && device_is_attached(children[i]) && 224 ATA_REINIT(children[i])) { 225 /* 226 * if we had a running request and its device matches 227 * this child we need to inform the request that the 228 * device is gone. 229 */ 230 if (request && request->dev == children[i]) { 231 request->result = ENXIO; 232 device_printf(request->dev, "FAILURE - device detached\n"); 233 234 /* if not timeout finish request here */ 235 if (!(request->flags & ATA_R_TIMEOUT)) 236 ata_finish(request); 237 request = NULL; 238 } 239 device_delete_child(dev, children[i]); 240 } 241 } 242 kfree(children, M_TEMP); 243 rel_mplock(); 244 } 245 246 /* if we still have a good request put it on the queue again */ 247 if (request && !(request->flags & ATA_R_TIMEOUT)) { 248 device_printf(request->dev, 249 "WARNING - %s requeued due to channel reset", 250 ata_cmd2str(request)); 251 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL))) 252 kprintf(" LBA=%ju", request->u.ata.lba); 253 kprintf("\n"); 254 request->flags |= ATA_R_REQUEUE; 255 ata_queue_request(request); 256 } 257 258 /* we're done release the channel for new work */ 259 spin_lock_wr(&ch->state_mtx); 260 ch->state = ATA_IDLE; 261 spin_unlock_wr(&ch->state_mtx); 262 ATA_LOCKING(dev, ATA_LF_UNLOCK); 263 264 if (bootverbose) 265 device_printf(dev, "reinit done ..\n"); 266 267 /* kick off requests on the queue */ 268 ata_start(dev); 269 return 0; 270 } 271 272 int 273 ata_suspend(device_t dev) 274 { 275 struct ata_channel *ch; 276 277 /* check for valid device */ 278 if (!dev || !(ch = device_get_softc(dev))) 279 return ENXIO; 280 281 /* wait for the channel to be IDLE or detached before suspending */ 282 while (ch->r_irq) { 283 spin_lock_wr(&ch->state_mtx); 284 if (ch->state == ATA_IDLE) { 285 ch->state = ATA_ACTIVE; 286 spin_unlock_wr(&ch->state_mtx); 287 break; 288 } 289 spin_unlock_wr(&ch->state_mtx); 290 tsleep(ch, 0, "atasusp", hz/10); 291 } 292 ATA_LOCKING(dev, ATA_LF_UNLOCK); 293 return 0; 294 } 295 296 int 297 ata_resume(device_t dev) 298 { 299 struct ata_channel *ch; 300 int error; 301 302 /* check for valid device */ 303 if (!dev || !(ch = device_get_softc(dev))) 304 return ENXIO; 305 306 /* reinit the devices, we dont know what mode/state they are in */ 307 error = ata_reinit(dev); 308 309 /* kick off requests on the queue */ 310 ata_start(dev); 311 return error; 312 } 313 314 int 315 ata_interrupt(void *data) 316 { 317 struct ata_channel *ch = (struct ata_channel *)data; 318 struct ata_request *request; 319 320 spin_lock_wr(&ch->state_mtx); 321 do { 322 /* ignore interrupt if its not for us */ 323 if (ch->hw.status && !ch->hw.status(ch->dev)) 324 break; 325 326 /* do we have a running request */ 327 if (!(request = ch->running)) 328 break; 329 330 /* XXX TGEN Ignore weird ATAPI+DMA interrupts on SMP */ 331 if (ch->dma && (request->flags & ATA_R_ATAPI)) { 332 int status = ATA_IDX_INB(ch, ATA_STATUS); 333 int error = ATA_IDX_INB(ch, ATA_ERROR); 334 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 335 if (((status & (ATA_S_DWF|ATA_S_DRQ)) == (ATA_S_DWF|ATA_S_DRQ)) && 336 ((error & ATA_E_ILI) == ATA_E_ILI) && 337 !(bmstat & ATA_BMSTAT_ERROR)) { 338 if (bootverbose) 339 device_printf(request->dev, "ignoring weird interrupt\n"); 340 break; 341 } 342 } 343 344 ATA_DEBUG_RQ(request, "interrupt"); 345 346 /* safetycheck for the right state */ 347 if (ch->state == ATA_IDLE) { 348 device_printf(request->dev, "interrupt on idle channel ignored\n"); 349 break; 350 } 351 352 /* 353 * we have the HW locks, so end the transaction for this request 354 * if it finishes immediately otherwise wait for next interrupt 355 */ 356 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) { 357 ch->running = NULL; 358 if (ch->state == ATA_ACTIVE) 359 ch->state = ATA_IDLE; 360 spin_unlock_wr(&ch->state_mtx); 361 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK); 362 ata_finish(request); 363 return 1; 364 } 365 } while (0); 366 spin_unlock_wr(&ch->state_mtx); 367 return 0; 368 } 369 370 /* 371 * device related interfaces 372 */ 373 static int 374 ata_ioctl(struct dev_ioctl_args *ap) 375 { 376 device_t device, *children; 377 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)ap->a_data; 378 int *value = (int *)ap->a_data; 379 int i, nchildren, error = ENOTTY; 380 381 switch (ap->a_cmd) { 382 case IOCATAGMAXCHANNEL: 383 *value = devclass_get_maxunit(ata_devclass); 384 error = 0; 385 break; 386 387 case IOCATAREINIT: 388 if (*value > devclass_get_maxunit(ata_devclass) || 389 !(device = devclass_get_device(ata_devclass, *value))) 390 return ENXIO; 391 error = ata_reinit(device); 392 ata_start(device); 393 break; 394 395 case IOCATAATTACH: 396 if (*value > devclass_get_maxunit(ata_devclass) || 397 !(device = devclass_get_device(ata_devclass, *value))) 398 return ENXIO; 399 /* XXX SOS should enable channel HW on controller */ 400 error = ata_attach(device); 401 break; 402 403 case IOCATADETACH: 404 if (*value > devclass_get_maxunit(ata_devclass) || 405 !(device = devclass_get_device(ata_devclass, *value))) 406 return ENXIO; 407 error = ata_detach(device); 408 /* XXX SOS should disable channel HW on controller */ 409 break; 410 411 case IOCATADEVICES: 412 if (devices->channel > devclass_get_maxunit(ata_devclass) || 413 !(device = devclass_get_device(ata_devclass, devices->channel))) 414 return ENXIO; 415 bzero(devices->name[0], 32); 416 bzero(&devices->params[0], sizeof(struct ata_params)); 417 bzero(devices->name[1], 32); 418 bzero(&devices->params[1], sizeof(struct ata_params)); 419 if (!device_get_children(device, &children, &nchildren)) { 420 for (i = 0; i < nchildren; i++) { 421 if (children[i] && device_is_attached(children[i])) { 422 struct ata_device *atadev = device_get_softc(children[i]); 423 424 if (atadev->unit == ATA_MASTER) { 425 strncpy(devices->name[0], 426 device_get_nameunit(children[i]), 32); 427 bcopy(&atadev->param, &devices->params[0], 428 sizeof(struct ata_params)); 429 } 430 if (atadev->unit == ATA_SLAVE) { 431 strncpy(devices->name[1], 432 device_get_nameunit(children[i]), 32); 433 bcopy(&atadev->param, &devices->params[1], 434 sizeof(struct ata_params)); 435 } 436 } 437 } 438 kfree(children, M_TEMP); 439 error = 0; 440 } 441 else 442 error = ENODEV; 443 break; 444 445 default: 446 if (ata_raid_ioctl_func) 447 error = ata_raid_ioctl_func(ap->a_cmd, ap->a_data); 448 } 449 return error; 450 } 451 452 int 453 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data) 454 { 455 struct ata_device *atadev = device_get_softc(dev); 456 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data; 457 struct ata_params *params = (struct ata_params *)data; 458 int *mode = (int *)data; 459 struct ata_request *request; 460 caddr_t buf; 461 int error; 462 463 switch (cmd) { 464 case IOCATAREQUEST: 465 if (!(buf = kmalloc(ioc_request->count, M_ATA, M_NOWAIT))) { 466 return ENOMEM; 467 } 468 if (!(request = ata_alloc_request())) { 469 kfree(buf, M_ATA); 470 return ENOMEM; 471 } 472 if (ioc_request->flags & ATA_CMD_WRITE) { 473 error = copyin(ioc_request->data, buf, ioc_request->count); 474 if (error) { 475 kfree(buf, M_ATA); 476 ata_free_request(request); 477 return error; 478 } 479 } 480 request->dev = dev; 481 if (ioc_request->flags & ATA_CMD_ATAPI) { 482 request->flags = ATA_R_ATAPI; 483 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16); 484 } 485 else { 486 request->u.ata.command = ioc_request->u.ata.command; 487 request->u.ata.feature = ioc_request->u.ata.feature; 488 request->u.ata.lba = ioc_request->u.ata.lba; 489 request->u.ata.count = ioc_request->u.ata.count; 490 } 491 request->timeout = ioc_request->timeout; 492 request->data = buf; 493 request->bytecount = ioc_request->count; 494 request->transfersize = request->bytecount; 495 if (ioc_request->flags & ATA_CMD_CONTROL) 496 request->flags |= ATA_R_CONTROL; 497 if (ioc_request->flags & ATA_CMD_READ) 498 request->flags |= ATA_R_READ; 499 if (ioc_request->flags & ATA_CMD_WRITE) 500 request->flags |= ATA_R_WRITE; 501 ata_queue_request(request); 502 if (request->flags & ATA_R_ATAPI) { 503 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense, 504 sizeof(struct atapi_sense)); 505 } 506 else { 507 ioc_request->u.ata.command = request->u.ata.command; 508 ioc_request->u.ata.feature = request->u.ata.feature; 509 ioc_request->u.ata.lba = request->u.ata.lba; 510 ioc_request->u.ata.count = request->u.ata.count; 511 } 512 ioc_request->error = request->result; 513 if (ioc_request->flags & ATA_CMD_READ) 514 error = copyout(buf, ioc_request->data, ioc_request->count); 515 else 516 error = 0; 517 kfree(buf, M_ATA); 518 ata_free_request(request); 519 return error; 520 521 case IOCATAGPARM: 522 ata_getparam(atadev, 0); 523 bcopy(&atadev->param, params, sizeof(struct ata_params)); 524 return 0; 525 526 case IOCATASMODE: 527 atadev->mode = *mode; 528 ATA_SETMODE(device_get_parent(dev), dev); 529 return 0; 530 531 case IOCATAGMODE: 532 *mode = atadev->mode; 533 return 0; 534 default: 535 return ENOTTY; 536 } 537 } 538 539 static void 540 ata_boot_attach(void) 541 { 542 struct ata_channel *ch; 543 int ctlr; 544 545 get_mplock(); 546 547 /* kick of probe and attach on all channels */ 548 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) { 549 if ((ch = devclass_get_softc(ata_devclass, ctlr))) { 550 ata_identify(ch->dev); 551 } 552 } 553 554 /* release the hook that got us here, we are only needed once during boot */ 555 if (ata_delayed_attach) { 556 config_intrhook_disestablish(ata_delayed_attach); 557 kfree(ata_delayed_attach, M_TEMP); 558 ata_delayed_attach = NULL; 559 } 560 561 rel_mplock(); 562 } 563 564 565 /* 566 * misc support functions 567 */ 568 static device_t 569 ata_add_child(device_t parent, struct ata_device *atadev, int unit) 570 { 571 device_t child; 572 573 if ((child = device_add_child(parent, NULL, unit))) { 574 device_set_softc(child, atadev); 575 device_quiet(child); 576 atadev->dev = child; 577 atadev->max_iosize = DEV_BSIZE; 578 atadev->mode = ATA_PIO_MAX; 579 } 580 return child; 581 } 582 583 static int 584 ata_getparam(struct ata_device *atadev, int init) 585 { 586 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev)); 587 struct ata_request *request; 588 u_int8_t command = 0; 589 int error = ENOMEM, retries = 2; 590 591 if (ch->devices & 592 (atadev->unit == ATA_MASTER ? ATA_ATA_MASTER : ATA_ATA_SLAVE)) 593 command = ATA_ATA_IDENTIFY; 594 if (ch->devices & 595 (atadev->unit == ATA_MASTER ? ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)) 596 command = ATA_ATAPI_IDENTIFY; 597 if (!command) 598 return ENXIO; 599 600 while (retries-- > 0 && error) { 601 if (!(request = ata_alloc_request())) 602 break; 603 request->dev = atadev->dev; 604 request->timeout = 1; 605 request->retries = 0; 606 request->u.ata.command = command; 607 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET); 608 request->data = (void *)&atadev->param; 609 request->bytecount = sizeof(struct ata_params); 610 request->donecount = 0; 611 request->transfersize = DEV_BSIZE; 612 ata_queue_request(request); 613 error = request->result; 614 ata_free_request(request); 615 } 616 617 if (!error && (isprint(atadev->param.model[0]) || 618 isprint(atadev->param.model[1]))) { 619 struct ata_params *atacap = &atadev->param; 620 char buffer[64]; 621 #if BYTE_ORDER == BIG_ENDIAN 622 int16_t *ptr; 623 624 for (ptr = (int16_t *)atacap; 625 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 626 *ptr = bswap16(*ptr); 627 } 628 #endif 629 if (!(!strncmp(atacap->model, "FX", 2) || 630 !strncmp(atacap->model, "NEC", 3) || 631 !strncmp(atacap->model, "Pioneer", 7) || 632 !strncmp(atacap->model, "SHARP", 5))) { 633 bswap(atacap->model, sizeof(atacap->model)); 634 bswap(atacap->revision, sizeof(atacap->revision)); 635 bswap(atacap->serial, sizeof(atacap->serial)); 636 } 637 btrim(atacap->model, sizeof(atacap->model)); 638 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 639 btrim(atacap->revision, sizeof(atacap->revision)); 640 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 641 btrim(atacap->serial, sizeof(atacap->serial)); 642 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 643 644 if (bootverbose) 645 kprintf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n", 646 device_get_unit(ch->dev), 647 atadev->unit == ATA_MASTER ? "master" : "slave", 648 ata_mode2str(ata_pmode(atacap)), 649 ata_mode2str(ata_wmode(atacap)), 650 ata_mode2str(ata_umode(atacap)), 651 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 652 653 if (init) { 654 ksprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision); 655 device_set_desc_copy(atadev->dev, buffer); 656 if (atadev->param.config & ATA_PROTO_ATAPI) { 657 if (atapi_dma && ch->dma && 658 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR && 659 ata_umode(&atadev->param) >= ATA_UDMA2) 660 atadev->mode = ATA_DMA_MAX; 661 } 662 else { 663 if (ata_dma && ch->dma && 664 (ata_umode(&atadev->param) > 0 || 665 ata_wmode(&atadev->param) > 0)) 666 atadev->mode = ATA_DMA_MAX; 667 } 668 } 669 } 670 else { 671 if (!error) 672 error = ENXIO; 673 } 674 return error; 675 } 676 677 int 678 ata_identify(device_t dev) 679 { 680 struct ata_channel *ch = device_get_softc(dev); 681 struct ata_device *master = NULL, *slave = NULL; 682 device_t master_child = NULL, slave_child = NULL; 683 int master_unit = -1, slave_unit = -1; 684 685 if (ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) { 686 if (!(master = kmalloc(sizeof(struct ata_device), 687 M_ATA, M_NOWAIT | M_ZERO))) { 688 device_printf(dev, "out of memory\n"); 689 return ENOMEM; 690 } 691 master->unit = ATA_MASTER; 692 } 693 if (ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) { 694 if (!(slave = kmalloc(sizeof(struct ata_device), 695 M_ATA, M_NOWAIT | M_ZERO))) { 696 kfree(master, M_ATA); 697 device_printf(dev, "out of memory\n"); 698 return ENOMEM; 699 } 700 slave->unit = ATA_SLAVE; 701 } 702 703 #ifdef ATA_STATIC_ID 704 if (ch->devices & ATA_ATA_MASTER) 705 master_unit = (device_get_unit(dev) << 1); 706 #endif 707 if (master && !(master_child = ata_add_child(dev, master, master_unit))) { 708 kfree(master, M_ATA); 709 master = NULL; 710 } 711 #ifdef ATA_STATIC_ID 712 if (ch->devices & ATA_ATA_SLAVE) 713 slave_unit = (device_get_unit(dev) << 1) + 1; 714 #endif 715 if (slave && !(slave_child = ata_add_child(dev, slave, slave_unit))) { 716 kfree(slave, M_ATA); 717 slave = NULL; 718 } 719 720 if (slave && ata_getparam(slave, 1)) { 721 device_delete_child(dev, slave_child); 722 kfree(slave, M_ATA); 723 } 724 if (master && ata_getparam(master, 1)) { 725 device_delete_child(dev, master_child); 726 kfree(master, M_ATA); 727 } 728 729 bus_generic_probe(dev); 730 bus_generic_attach(dev); 731 return 0; 732 } 733 734 void 735 ata_default_registers(device_t dev) 736 { 737 struct ata_channel *ch = device_get_softc(dev); 738 739 /* fill in the defaults from whats setup already */ 740 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res; 741 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset; 742 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res; 743 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset; 744 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res; 745 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset; 746 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res; 747 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset; 748 } 749 750 void 751 ata_modify_if_48bit(struct ata_request *request) 752 { 753 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev)); 754 struct ata_device *atadev = device_get_softc(request->dev); 755 756 atadev->flags &= ~ATA_D_48BIT_ACTIVE; 757 758 if ((request->u.ata.lba >= ATA_MAX_28BIT_LBA || 759 request->u.ata.count > 256) && 760 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 761 762 /* translate command into 48bit version */ 763 switch (request->u.ata.command) { 764 case ATA_READ: 765 request->u.ata.command = ATA_READ48; 766 break; 767 case ATA_READ_MUL: 768 request->u.ata.command = ATA_READ_MUL48; 769 break; 770 case ATA_READ_DMA: 771 if (ch->flags & ATA_NO_48BIT_DMA) { 772 if (request->transfersize > DEV_BSIZE) 773 request->u.ata.command = ATA_READ_MUL48; 774 else 775 request->u.ata.command = ATA_READ48; 776 request->flags &= ~ATA_R_DMA; 777 } 778 else 779 request->u.ata.command = ATA_READ_DMA48; 780 break; 781 case ATA_READ_DMA_QUEUED: 782 if (ch->flags & ATA_NO_48BIT_DMA) { 783 if (request->transfersize > DEV_BSIZE) 784 request->u.ata.command = ATA_READ_MUL48; 785 else 786 request->u.ata.command = ATA_READ48; 787 request->flags &= ~ATA_R_DMA; 788 } 789 else 790 request->u.ata.command = ATA_READ_DMA_QUEUED48; 791 break; 792 case ATA_WRITE: 793 request->u.ata.command = ATA_WRITE48; 794 break; 795 case ATA_WRITE_MUL: 796 request->u.ata.command = ATA_WRITE_MUL48; 797 break; 798 case ATA_WRITE_DMA: 799 if (ch->flags & ATA_NO_48BIT_DMA) { 800 if (request->transfersize > DEV_BSIZE) 801 request->u.ata.command = ATA_WRITE_MUL48; 802 else 803 request->u.ata.command = ATA_WRITE48; 804 request->flags &= ~ATA_R_DMA; 805 } 806 else 807 request->u.ata.command = ATA_WRITE_DMA48; 808 break; 809 case ATA_WRITE_DMA_QUEUED: 810 if (ch->flags & ATA_NO_48BIT_DMA) { 811 if (request->transfersize > DEV_BSIZE) 812 request->u.ata.command = ATA_WRITE_MUL48; 813 else 814 request->u.ata.command = ATA_WRITE48; 815 request->u.ata.command = ATA_WRITE48; 816 request->flags &= ~ATA_R_DMA; 817 } 818 else 819 request->u.ata.command = ATA_WRITE_DMA_QUEUED48; 820 break; 821 case ATA_FLUSHCACHE: 822 request->u.ata.command = ATA_FLUSHCACHE48; 823 break; 824 case ATA_READ_NATIVE_MAX_ADDDRESS: 825 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDDRESS48; 826 break; 827 case ATA_SET_MAX_ADDRESS: 828 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 829 break; 830 default: 831 return; 832 } 833 atadev->flags |= ATA_D_48BIT_ACTIVE; 834 } 835 } 836 837 void 838 ata_udelay(int interval) 839 { 840 /* for now just use DELAY, the timer/sleep subsytems are not there yet */ 841 if (1 || interval < (1000000/hz) || ata_delayed_attach) 842 DELAY(interval); 843 else 844 tsleep(&interval, 0, "ataslp", interval/(1000000/hz)); 845 } 846 847 char * 848 ata_mode2str(int mode) 849 { 850 switch (mode) { 851 case -1: return "UNSUPPORTED"; 852 case ATA_PIO0: return "PIO0"; 853 case ATA_PIO1: return "PIO1"; 854 case ATA_PIO2: return "PIO2"; 855 case ATA_PIO3: return "PIO3"; 856 case ATA_PIO4: return "PIO4"; 857 case ATA_WDMA0: return "WDMA0"; 858 case ATA_WDMA1: return "WDMA1"; 859 case ATA_WDMA2: return "WDMA2"; 860 case ATA_UDMA0: return "UDMA16"; 861 case ATA_UDMA1: return "UDMA25"; 862 case ATA_UDMA2: return "UDMA33"; 863 case ATA_UDMA3: return "UDMA40"; 864 case ATA_UDMA4: return "UDMA66"; 865 case ATA_UDMA5: return "UDMA100"; 866 case ATA_UDMA6: return "UDMA133"; 867 case ATA_SA150: return "SATA150"; 868 case ATA_SA300: return "SATA300"; 869 case ATA_USB: return "USB"; 870 case ATA_USB1: return "USB1"; 871 case ATA_USB2: return "USB2"; 872 default: 873 if (mode & ATA_DMA_MASK) 874 return "BIOSDMA"; 875 else 876 return "BIOSPIO"; 877 } 878 } 879 880 int 881 ata_pmode(struct ata_params *ap) 882 { 883 if (ap->atavalid & ATA_FLAG_64_70) { 884 if (ap->apiomodes & 0x02) 885 return ATA_PIO4; 886 if (ap->apiomodes & 0x01) 887 return ATA_PIO3; 888 } 889 if (ap->mwdmamodes & 0x04) 890 return ATA_PIO4; 891 if (ap->mwdmamodes & 0x02) 892 return ATA_PIO3; 893 if (ap->mwdmamodes & 0x01) 894 return ATA_PIO2; 895 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 896 return ATA_PIO2; 897 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 898 return ATA_PIO1; 899 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 900 return ATA_PIO0; 901 return ATA_PIO0; 902 } 903 904 int 905 ata_wmode(struct ata_params *ap) 906 { 907 if (ap->mwdmamodes & 0x04) 908 return ATA_WDMA2; 909 if (ap->mwdmamodes & 0x02) 910 return ATA_WDMA1; 911 if (ap->mwdmamodes & 0x01) 912 return ATA_WDMA0; 913 return -1; 914 } 915 916 int 917 ata_umode(struct ata_params *ap) 918 { 919 if (ap->atavalid & ATA_FLAG_88) { 920 if (ap->udmamodes & 0x40) 921 return ATA_UDMA6; 922 if (ap->udmamodes & 0x20) 923 return ATA_UDMA5; 924 if (ap->udmamodes & 0x10) 925 return ATA_UDMA4; 926 if (ap->udmamodes & 0x08) 927 return ATA_UDMA3; 928 if (ap->udmamodes & 0x04) 929 return ATA_UDMA2; 930 if (ap->udmamodes & 0x02) 931 return ATA_UDMA1; 932 if (ap->udmamodes & 0x01) 933 return ATA_UDMA0; 934 } 935 return -1; 936 } 937 938 int 939 ata_limit_mode(device_t dev, int mode, int maxmode) 940 { 941 struct ata_device *atadev = device_get_softc(dev); 942 943 if (maxmode && mode > maxmode) 944 mode = maxmode; 945 946 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0) 947 return min(mode, ata_umode(&atadev->param)); 948 949 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0) 950 return min(mode, ata_wmode(&atadev->param)); 951 952 if (mode > ata_pmode(&atadev->param)) 953 return min(mode, ata_pmode(&atadev->param)); 954 955 return mode; 956 } 957 958 static void 959 bswap(int8_t *buf, int len) 960 { 961 u_int16_t *ptr = (u_int16_t*)(buf + len); 962 963 while (--ptr >= (u_int16_t*)buf) 964 *ptr = ntohs(*ptr); 965 } 966 967 static void 968 btrim(int8_t *buf, int len) 969 { 970 int8_t *ptr; 971 972 for (ptr = buf; ptr < buf+len; ++ptr) 973 if (!*ptr || *ptr == '_') 974 *ptr = ' '; 975 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 976 *ptr = 0; 977 } 978 979 static void 980 bpack(int8_t *src, int8_t *dst, int len) 981 { 982 int i, j, blank; 983 984 for (i = j = blank = 0 ; i < len; i++) { 985 if (blank && src[i] == ' ') continue; 986 if (blank && src[i] != ' ') { 987 dst[j++] = src[i]; 988 blank = 0; 989 continue; 990 } 991 if (src[i] == ' ') { 992 blank = 1; 993 if (i == 0) 994 continue; 995 } 996 dst[j++] = src[i]; 997 } 998 if (j < len) 999 dst[j] = 0x00; 1000 } 1001 1002 1003 /* 1004 * module handeling 1005 */ 1006 static int 1007 ata_module_event_handler(module_t mod, int what, void *arg) 1008 { 1009 /* static because we need the reference at destruction time */ 1010 static cdev_t atacdev; 1011 1012 switch (what) { 1013 case MOD_LOAD: 1014 /* register controlling device */ 1015 dev_ops_add(&ata_ops, 0, 0); 1016 atacdev = make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1017 reference_dev(atacdev); 1018 1019 if (cold) { 1020 /* register boot attach to be run when interrupts are enabled */ 1021 if (!(ata_delayed_attach = (struct intr_config_hook *) 1022 kmalloc(sizeof(struct intr_config_hook), 1023 M_TEMP, M_NOWAIT | M_ZERO))) { 1024 kprintf("ata: kmalloc of delayed attach hook failed\n"); 1025 return EIO; 1026 } 1027 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1028 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1029 kprintf("ata: config_intrhook_establish failed\n"); 1030 kfree(ata_delayed_attach, M_TEMP); 1031 } 1032 } 1033 return 0; 1034 1035 case MOD_UNLOAD: 1036 /* deregister controlling device */ 1037 destroy_dev(atacdev); 1038 dev_ops_remove(&ata_ops, 0, 0); 1039 return 0; 1040 1041 default: 1042 return EOPNOTSUPP; 1043 } 1044 } 1045 1046 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL }; 1047 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 1048 MODULE_VERSION(ata, 1); 1049 1050 /* 1051 * Construct a completely zero'ed ata_request. On objcache_put(), an 1052 * ata_request object is also zero'ed, so objcache_get() is guaranteed to give 1053 * completely zero'ed objects without spending too much time. 1054 */ 1055 static boolean_t 1056 ata_request_cache_ctor(void *obj, void *private, int ocflags) 1057 { 1058 struct ata_request *arp = obj; 1059 1060 bzero(arp, sizeof(struct ata_request)); 1061 return(TRUE); 1062 } 1063 1064 /* 1065 * Construct a completely zero'ed ata_composite. On objcache_put(), an 1066 * ata_composite object is also zero'ed, so objcache_get() is guaranteed to give 1067 * completely zero'ed objects without spending too much time. 1068 */ 1069 static boolean_t 1070 ata_composite_cache_ctor(void *obj, void *private, int ocflags) 1071 { 1072 struct ata_composite *acp = obj; 1073 1074 bzero(acp, sizeof(struct ata_composite)); 1075 return(TRUE); 1076 } 1077 1078 static void 1079 ata_init(void) 1080 { 1081 ata_request_cache = objcache_create("ata_request", 0, 0, 1082 ata_request_cache_ctor, NULL, NULL, 1083 objcache_malloc_alloc, 1084 objcache_malloc_free, 1085 &ata_request_malloc_args); 1086 ata_composite_cache = objcache_create("ata_composite", 0, 0, 1087 ata_composite_cache_ctor, NULL, NULL, 1088 objcache_malloc_alloc, 1089 objcache_malloc_free, 1090 &ata_composite_malloc_args); 1091 } 1092 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL); 1093 1094 static void 1095 ata_uninit(void) 1096 { 1097 objcache_destroy(ata_composite_cache); 1098 objcache_destroy(ata_request_cache); 1099 } 1100 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL); 1101