1 /*- 2 * Copyright (c) 1998 - 2008 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.279 2007/02/23 16:25:08 jhb Exp $ 27 */ 28 29 #include "opt_ata.h" 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/callout.h> 34 #include <sys/conf.h> 35 #include <sys/ctype.h> 36 #include <sys/device.h> 37 #include <sys/endian.h> 38 #include <sys/kernel.h> 39 #include <sys/libkern.h> 40 #include <sys/lock.h> /* for {get,rel}_mplock() */ 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/nata.h> 44 #include <sys/objcache.h> 45 #include <sys/queue.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 49 #include <sys/mplock2.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 #if 0 65 static void ata_boot_attach(void); 66 #endif 67 static device_t ata_add_child(device_t, struct ata_device *, int); 68 static int ata_getparam(struct ata_device *, int); 69 static void bswap(int8_t *, int); 70 static void btrim(int8_t *, int); 71 static void bpack(int8_t *, int8_t *, int); 72 73 /* global vars */ 74 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer"); 75 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL; 76 devclass_t ata_devclass; 77 struct objcache *ata_request_cache; 78 struct objcache *ata_composite_cache; 79 struct objcache_malloc_args ata_request_malloc_args = { 80 sizeof(struct ata_request), M_ATA }; 81 struct objcache_malloc_args ata_composite_malloc_args = { 82 sizeof(struct ata_composite), M_ATA }; 83 int ata_wc = 1; 84 85 /* local vars */ 86 static int ata_dma = 1; 87 static int atapi_dma = 1; 88 89 /* sysctl vars */ 90 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 91 TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 92 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RW, &ata_dma, 0, 93 "ATA disk DMA mode control"); 94 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 95 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RW, &atapi_dma, 0, 96 "ATAPI device DMA mode control"); 97 TUNABLE_INT("hw.ata.wc", &ata_wc); 98 SYSCTL_INT(_hw_ata, OID_AUTO, ata_wc, CTLFLAG_RW, &ata_wc, 0, 99 "ATA disk write caching"); 100 101 /* 102 * newbus device interface related functions 103 */ 104 int 105 ata_probe(device_t dev) 106 { 107 return 0; 108 } 109 110 int 111 ata_attach(device_t dev) 112 { 113 struct ata_channel *ch = device_get_softc(dev); 114 int error, rid; 115 116 /* check that we have a virgin channel to attach */ 117 if (ch->r_irq) 118 return EEXIST; 119 120 /* initialize the softc basics */ 121 ch->dev = dev; 122 ch->state = ATA_IDLE; 123 lockinit(&ch->state_mtx, "ataattach_state", 0, 0); 124 lockinit(&ch->queue_mtx, "ataattach_queue", 0, 0); 125 ata_queue_init(ch); 126 127 /* reset the controller HW, the channel and device(s) */ 128 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit) 129 tsleep(&error, 0, "ataatch", 1); 130 ATA_RESET(dev); 131 ATA_LOCKING(dev, ATA_LF_UNLOCK); 132 133 /* setup interrupt delivery */ 134 rid = ATA_IRQ_RID; 135 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 136 RF_SHAREABLE | RF_ACTIVE); 137 if (!ch->r_irq) { 138 device_printf(dev, "unable to allocate interrupt\n"); 139 return ENXIO; 140 } 141 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, 142 (driver_intr_t *)ata_interrupt, ch, &ch->ih, 143 NULL))) { 144 device_printf(dev, "unable to setup interrupt\n"); 145 return error; 146 } 147 148 /* probe and attach devices on this channel unless we are in early boot */ 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 lockmgr(&ch->state_mtx, LK_EXCLUSIVE); 166 ch->state |= ATA_STALL_QUEUE; 167 lockmgr(&ch->state_mtx, LK_RELEASE); 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 lockuninit(&ch->state_mtx); 182 lockuninit(&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 lockmgr(&ch->state_mtx, LK_EXCLUSIVE); 207 if ((request = ch->running)) 208 callout_cancel(&request->callout); 209 ch->running = NULL; 210 211 /* unconditionally grap the channel lock */ 212 ch->state |= ATA_STALL_QUEUE; 213 lockmgr(&ch->state_mtx, LK_RELEASE); 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 lockmgr(&ch->state_mtx, LK_EXCLUSIVE); 260 ch->state = ATA_IDLE; 261 lockmgr(&ch->state_mtx, LK_RELEASE); 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 lockmgr(&ch->state_mtx, LK_EXCLUSIVE); 284 if (ch->state == ATA_IDLE) { 285 ch->state = ATA_ACTIVE; 286 lockmgr(&ch->state_mtx, LK_RELEASE); 287 break; 288 } 289 lockmgr(&ch->state_mtx, LK_RELEASE); 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 int error; 300 301 /* check for valid device */ 302 if (!dev || !device_get_softc(dev)) 303 return ENXIO; 304 305 /* reinit the devices, we dont know what mode/state they are in */ 306 error = ata_reinit(dev); 307 308 /* kick off requests on the queue */ 309 ata_start(dev); 310 return error; 311 } 312 313 int 314 ata_interrupt(void *data) 315 { 316 struct ata_channel *ch = (struct ata_channel *)data; 317 struct ata_request *request; 318 319 lockmgr(&ch->state_mtx, LK_EXCLUSIVE); 320 do { 321 /* 322 * Ignore interrupt if its not for us. This may also have the 323 * side effect of processing events unrelated to I/O requests. 324 */ 325 if (ch->hw.status && !ch->hw.status(ch->dev)) 326 break; 327 328 /* 329 * Check if we have a running request, and make sure it has been 330 * completely queued. Otherwise the channel status may indicate 331 * not-busy when, in fact, the command had not yet been issued. 332 */ 333 if ((request = ch->running) == NULL) 334 break; 335 if ((request->flags & ATA_R_HWCMDQUEUED) == 0) { 336 kprintf("ata_interrupt: early interrupt\n"); 337 break; 338 } 339 340 ATA_DEBUG_RQ(request, "interrupt"); 341 342 /* safetycheck for the right state */ 343 if (ch->state == ATA_IDLE) { 344 device_printf(request->dev, "interrupt on idle channel ignored\n"); 345 break; 346 } 347 348 /* 349 * we have the HW locks, so end the transaction for this request 350 * if it finishes immediately otherwise wait for next interrupt 351 */ 352 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) { 353 ch->running = NULL; 354 if (ch->state == ATA_ACTIVE) 355 ch->state = ATA_IDLE; 356 lockmgr(&ch->state_mtx, LK_RELEASE); 357 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK); 358 ata_finish(request); 359 return 1; 360 } 361 } while (0); 362 lockmgr(&ch->state_mtx, LK_RELEASE); 363 return 0; 364 } 365 366 /* 367 * device related interfaces 368 */ 369 static int 370 ata_ioctl(struct dev_ioctl_args *ap) 371 { 372 device_t device, *children; 373 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)ap->a_data; 374 int *value = (int *)ap->a_data; 375 int i, nchildren, error = ENOTTY; 376 377 switch (ap->a_cmd) { 378 case IOCATAGMAXCHANNEL: 379 /* In case we have channel 0..n this will return n+1. */ 380 *value = devclass_get_maxunit(ata_devclass); 381 error = 0; 382 break; 383 384 case IOCATAREINIT: 385 if (*value >= devclass_get_maxunit(ata_devclass) || 386 !(device = devclass_get_device(ata_devclass, *value))) 387 return ENXIO; 388 error = ata_reinit(device); 389 ata_start(device); 390 break; 391 392 case IOCATAATTACH: 393 if (*value >= devclass_get_maxunit(ata_devclass) || 394 !(device = devclass_get_device(ata_devclass, *value))) 395 return ENXIO; 396 /* XXX SOS should enable channel HW on controller */ 397 error = ata_attach(device); 398 break; 399 400 case IOCATADETACH: 401 if (*value >= devclass_get_maxunit(ata_devclass) || 402 !(device = devclass_get_device(ata_devclass, *value))) 403 return ENXIO; 404 error = ata_detach(device); 405 /* XXX SOS should disable channel HW on controller */ 406 break; 407 408 case IOCATADEVICES: 409 if (devices->channel >= devclass_get_maxunit(ata_devclass) || 410 !(device = devclass_get_device(ata_devclass, devices->channel))) 411 return ENXIO; 412 bzero(devices->name[0], 32); 413 bzero(&devices->params[0], sizeof(struct ata_params)); 414 bzero(devices->name[1], 32); 415 bzero(&devices->params[1], sizeof(struct ata_params)); 416 if (!device_get_children(device, &children, &nchildren)) { 417 for (i = 0; i < nchildren; i++) { 418 if (children[i] && device_is_attached(children[i])) { 419 struct ata_device *atadev = device_get_softc(children[i]); 420 421 if (atadev->unit == ATA_MASTER) { 422 strncpy(devices->name[0], 423 device_get_nameunit(children[i]), 32); 424 bcopy(&atadev->param, &devices->params[0], 425 sizeof(struct ata_params)); 426 } 427 if (atadev->unit == ATA_SLAVE) { 428 strncpy(devices->name[1], 429 device_get_nameunit(children[i]), 32); 430 bcopy(&atadev->param, &devices->params[1], 431 sizeof(struct ata_params)); 432 } 433 } 434 } 435 kfree(children, M_TEMP); 436 error = 0; 437 } 438 else 439 error = ENODEV; 440 break; 441 442 default: 443 if (ata_raid_ioctl_func) 444 error = ata_raid_ioctl_func(ap->a_cmd, ap->a_data); 445 } 446 return error; 447 } 448 449 int 450 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data) 451 { 452 struct ata_device *atadev = device_get_softc(dev); 453 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data; 454 struct ata_params *params = (struct ata_params *)data; 455 int *mode = (int *)data; 456 struct ata_request *request; 457 caddr_t buf; 458 int error; 459 460 switch (cmd) { 461 case IOCATAREQUEST: 462 if (!(buf = kmalloc(ioc_request->count, M_ATA, M_WAITOK | M_NULLOK))) { 463 return ENOMEM; 464 } 465 if (!(request = ata_alloc_request())) { 466 kfree(buf, M_ATA); 467 return ENOMEM; 468 } 469 request->dev = atadev->dev; 470 if (ioc_request->flags & ATA_CMD_WRITE) { 471 error = copyin(ioc_request->data, buf, ioc_request->count); 472 if (error) { 473 kfree(buf, M_ATA); 474 ata_free_request(request); 475 return error; 476 } 477 } 478 if (ioc_request->flags & ATA_CMD_ATAPI) { 479 request->flags = ATA_R_ATAPI; 480 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16); 481 } 482 else { 483 request->u.ata.command = ioc_request->u.ata.command; 484 request->u.ata.feature = ioc_request->u.ata.feature; 485 request->u.ata.lba = ioc_request->u.ata.lba; 486 request->u.ata.count = ioc_request->u.ata.count; 487 } 488 request->timeout = ioc_request->timeout; 489 request->data = buf; 490 request->bytecount = ioc_request->count; 491 request->transfersize = request->bytecount; 492 if (ioc_request->flags & ATA_CMD_CONTROL) 493 request->flags |= ATA_R_CONTROL; 494 if (ioc_request->flags & ATA_CMD_READ) 495 request->flags |= ATA_R_READ; 496 if (ioc_request->flags & ATA_CMD_WRITE) 497 request->flags |= ATA_R_WRITE; 498 ata_queue_request(request); 499 if (request->flags & ATA_R_ATAPI) { 500 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense, 501 sizeof(struct atapi_sense)); 502 } 503 else { 504 ioc_request->u.ata.command = request->u.ata.command; 505 ioc_request->u.ata.feature = request->u.ata.feature; 506 ioc_request->u.ata.lba = request->u.ata.lba; 507 ioc_request->u.ata.count = request->u.ata.count; 508 } 509 ioc_request->error = request->result; 510 if (ioc_request->flags & ATA_CMD_READ) 511 error = copyout(buf, ioc_request->data, ioc_request->count); 512 else 513 error = 0; 514 kfree(buf, M_ATA); 515 ata_free_request(request); 516 return error; 517 518 case IOCATAGPARM: 519 ata_getparam(atadev, 0); 520 bcopy(&atadev->param, params, sizeof(struct ata_params)); 521 return 0; 522 523 case IOCATASMODE: 524 atadev->mode = *mode; 525 ATA_SETMODE(device_get_parent(dev), dev); 526 return 0; 527 528 case IOCATAGMODE: 529 *mode = atadev->mode; 530 return 0; 531 case IOCATASSPINDOWN: 532 atadev->spindown = *mode; 533 return 0; 534 case IOCATAGSPINDOWN: 535 *mode = atadev->spindown; 536 return 0; 537 default: 538 return ENOTTY; 539 } 540 } 541 542 #if 0 543 544 static void 545 ata_boot_attach(void) 546 { 547 struct ata_channel *ch; 548 int ctlr; 549 550 get_mplock(); 551 552 /* kick of probe and attach on all channels */ 553 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) { 554 if ((ch = devclass_get_softc(ata_devclass, ctlr))) { 555 ata_identify(ch->dev); 556 } 557 } 558 559 rel_mplock(); 560 } 561 562 #endif 563 564 void 565 ata_print_cable(device_t dev, u_int8_t *who) 566 { 567 device_printf(dev, 568 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who); 569 } 570 571 /* 572 * misc support functions 573 */ 574 static device_t 575 ata_add_child(device_t parent, struct ata_device *atadev, int unit) 576 { 577 device_t child; 578 579 if ((child = device_add_child(parent, NULL, unit))) { 580 device_set_softc(child, atadev); 581 device_quiet(child); 582 atadev->dev = child; 583 atadev->max_iosize = DEV_BSIZE; 584 atadev->mode = ATA_PIO_MAX; 585 } 586 return child; 587 } 588 589 static int 590 ata_getparam(struct ata_device *atadev, int init) 591 { 592 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev)); 593 struct ata_request *request; 594 u_int8_t command = 0; 595 int error = ENOMEM, retries = 2; 596 597 if (ch->devices & (ATA_ATA_MASTER << atadev->unit)) 598 command = ATA_ATA_IDENTIFY; 599 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit)) 600 command = ATA_ATAPI_IDENTIFY; 601 if (!command) 602 return ENXIO; 603 604 while (retries-- > 0 && error) { 605 if (!(request = ata_alloc_request())) 606 break; 607 request->dev = atadev->dev; 608 request->timeout = 1; 609 request->retries = 0; 610 request->u.ata.command = command; 611 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT|ATA_R_QUIET); 612 request->data = (void *)&atadev->param; 613 request->bytecount = sizeof(struct ata_params); 614 request->donecount = 0; 615 request->transfersize = DEV_BSIZE; 616 ata_queue_request(request); 617 error = request->result; 618 ata_free_request(request); 619 } 620 621 if (!error && (isprint(atadev->param.model[0]) || 622 isprint(atadev->param.model[1]))) { 623 struct ata_params *atacap = &atadev->param; 624 int16_t *ptr; 625 626 for (ptr = (int16_t *)atacap; 627 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 628 *ptr = le16toh(*ptr); 629 } 630 if (!(!strncmp(atacap->model, "FX", 2) || 631 !strncmp(atacap->model, "NEC", 3) || 632 !strncmp(atacap->model, "Pioneer", 7) || 633 !strncmp(atacap->model, "SHARP", 5))) { 634 bswap(atacap->model, sizeof(atacap->model)); 635 bswap(atacap->revision, sizeof(atacap->revision)); 636 bswap(atacap->serial, sizeof(atacap->serial)); 637 } 638 btrim(atacap->model, sizeof(atacap->model)); 639 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 640 btrim(atacap->revision, sizeof(atacap->revision)); 641 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 642 btrim(atacap->serial, sizeof(atacap->serial)); 643 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 644 645 if (bootverbose) 646 kprintf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n", 647 device_get_unit(ch->dev), 648 ata_unit2str(atadev), 649 ata_mode2str(ata_pmode(atacap)), 650 ata_mode2str(ata_wmode(atacap)), 651 ata_mode2str(ata_umode(atacap)), 652 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 653 654 if (init) { 655 char buffer[64]; 656 657 ksprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision); 658 device_set_desc_copy(atadev->dev, buffer); 659 if ((atadev->param.config & ATA_PROTO_ATAPI) && 660 (atadev->param.config != ATA_CFA_MAGIC1) && 661 (atadev->param.config != ATA_CFA_MAGIC2)) { 662 if (atapi_dma && ch->dma && 663 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR && 664 ata_umode(&atadev->param) >= ATA_UDMA2) 665 atadev->mode = ATA_DMA_MAX; 666 } 667 else { 668 if (ata_dma && ch->dma && 669 (ata_umode(&atadev->param) > 0 || 670 ata_wmode(&atadev->param) > 0)) 671 atadev->mode = ATA_DMA_MAX; 672 } 673 } 674 } 675 else { 676 if (!error) 677 error = ENXIO; 678 } 679 return error; 680 } 681 682 int 683 ata_identify(device_t dev) 684 { 685 struct ata_channel *ch = device_get_softc(dev); 686 struct ata_device *atadev; 687 device_t child; 688 int i; 689 690 if (bootverbose) 691 device_printf(dev, "identify ch->devices=%08x\n", ch->devices); 692 693 for (i = 0; i < ATA_PM; ++i) { 694 if (ch->devices & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) { 695 int unit = -1; 696 697 if (!(atadev = kmalloc(sizeof(struct ata_device), 698 M_ATA, M_INTWAIT | M_ZERO))) { 699 device_printf(dev, "out of memory\n"); 700 return ENOMEM; 701 } 702 atadev->unit = i; 703 #ifdef ATA_STATIC_ID 704 if (ch->devices & ((ATA_ATA_MASTER << i))) 705 unit = (device_get_unit(dev) << 1) + i; 706 #endif 707 if ((child = ata_add_child(dev, atadev, unit))) { 708 if (ata_getparam(atadev, 1)) { 709 device_delete_child(dev, child); 710 kfree(atadev, M_ATA); 711 } 712 } 713 else 714 kfree(atadev, M_ATA); 715 } 716 } 717 bus_generic_probe(dev); 718 bus_generic_attach(dev); 719 return 0; 720 } 721 722 void 723 ata_default_registers(device_t dev) 724 { 725 struct ata_channel *ch = device_get_softc(dev); 726 727 /* fill in the defaults from whats setup already */ 728 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res; 729 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset; 730 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res; 731 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset; 732 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res; 733 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset; 734 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res; 735 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset; 736 } 737 738 void 739 ata_modify_if_48bit(struct ata_request *request) 740 { 741 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev)); 742 struct ata_device *atadev = device_get_softc(request->dev); 743 744 atadev->flags &= ~ATA_D_48BIT_ACTIVE; 745 746 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA || 747 request->u.ata.count > 256) && 748 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 749 750 /* translate command into 48bit version */ 751 switch (request->u.ata.command) { 752 case ATA_READ: 753 request->u.ata.command = ATA_READ48; 754 break; 755 case ATA_READ_MUL: 756 request->u.ata.command = ATA_READ_MUL48; 757 break; 758 case ATA_READ_DMA: 759 if (ch->flags & ATA_NO_48BIT_DMA) { 760 if (request->transfersize > DEV_BSIZE) 761 request->u.ata.command = ATA_READ_MUL48; 762 else 763 request->u.ata.command = ATA_READ48; 764 request->flags &= ~ATA_R_DMA; 765 } 766 else 767 request->u.ata.command = ATA_READ_DMA48; 768 break; 769 case ATA_READ_DMA_QUEUED: 770 if (ch->flags & ATA_NO_48BIT_DMA) { 771 if (request->transfersize > DEV_BSIZE) 772 request->u.ata.command = ATA_READ_MUL48; 773 else 774 request->u.ata.command = ATA_READ48; 775 request->flags &= ~ATA_R_DMA; 776 } 777 else 778 request->u.ata.command = ATA_READ_DMA_QUEUED48; 779 break; 780 case ATA_WRITE: 781 request->u.ata.command = ATA_WRITE48; 782 break; 783 case ATA_WRITE_MUL: 784 request->u.ata.command = ATA_WRITE_MUL48; 785 break; 786 case ATA_WRITE_DMA: 787 if (ch->flags & ATA_NO_48BIT_DMA) { 788 if (request->transfersize > DEV_BSIZE) 789 request->u.ata.command = ATA_WRITE_MUL48; 790 else 791 request->u.ata.command = ATA_WRITE48; 792 request->flags &= ~ATA_R_DMA; 793 } 794 else 795 request->u.ata.command = ATA_WRITE_DMA48; 796 break; 797 case ATA_WRITE_DMA_QUEUED: 798 if (ch->flags & ATA_NO_48BIT_DMA) { 799 if (request->transfersize > DEV_BSIZE) 800 request->u.ata.command = ATA_WRITE_MUL48; 801 else 802 request->u.ata.command = ATA_WRITE48; 803 request->u.ata.command = ATA_WRITE48; 804 request->flags &= ~ATA_R_DMA; 805 } 806 else 807 request->u.ata.command = ATA_WRITE_DMA_QUEUED48; 808 break; 809 case ATA_FLUSHCACHE: 810 request->u.ata.command = ATA_FLUSHCACHE48; 811 break; 812 case ATA_READ_NATIVE_MAX_ADDRESS: 813 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48; 814 break; 815 case ATA_SET_MAX_ADDRESS: 816 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 817 break; 818 default: 819 return; 820 } 821 atadev->flags |= ATA_D_48BIT_ACTIVE; 822 } 823 } 824 825 void 826 ata_udelay(int interval) 827 { 828 /* 829 * We can't use tsleep here, because we might be called from callout 830 * context. 831 */ 832 if (1 || interval < (1000000/hz)) 833 DELAY(interval); 834 else 835 tsleep(&interval, 0, "ataslp", 1 + interval / (1000000 / hz)); 836 } 837 838 const char * 839 ata_unit2str(struct ata_device *atadev) 840 { 841 static char str[8]; 842 843 ksprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave"); 844 return str; 845 } 846 847 const 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_atapi(device_t dev) 882 { 883 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 884 struct ata_device *atadev = device_get_softc(dev); 885 886 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) || 887 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE)); 888 } 889 890 int 891 ata_pmode(struct ata_params *ap) 892 { 893 if (ap->atavalid & ATA_FLAG_64_70) { 894 if (ap->apiomodes & 0x02) 895 return ATA_PIO4; 896 if (ap->apiomodes & 0x01) 897 return ATA_PIO3; 898 } 899 if (ap->mwdmamodes & 0x04) 900 return ATA_PIO4; 901 if (ap->mwdmamodes & 0x02) 902 return ATA_PIO3; 903 if (ap->mwdmamodes & 0x01) 904 return ATA_PIO2; 905 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 906 return ATA_PIO2; 907 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 908 return ATA_PIO1; 909 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 910 return ATA_PIO0; 911 return ATA_PIO0; 912 } 913 914 int 915 ata_wmode(struct ata_params *ap) 916 { 917 if (ap->mwdmamodes & 0x04) 918 return ATA_WDMA2; 919 if (ap->mwdmamodes & 0x02) 920 return ATA_WDMA1; 921 if (ap->mwdmamodes & 0x01) 922 return ATA_WDMA0; 923 return -1; 924 } 925 926 int 927 ata_umode(struct ata_params *ap) 928 { 929 if (ap->atavalid & ATA_FLAG_88) { 930 if (ap->udmamodes & 0x40) 931 return ATA_UDMA6; 932 if (ap->udmamodes & 0x20) 933 return ATA_UDMA5; 934 if (ap->udmamodes & 0x10) 935 return ATA_UDMA4; 936 if (ap->udmamodes & 0x08) 937 return ATA_UDMA3; 938 if (ap->udmamodes & 0x04) 939 return ATA_UDMA2; 940 if (ap->udmamodes & 0x02) 941 return ATA_UDMA1; 942 if (ap->udmamodes & 0x01) 943 return ATA_UDMA0; 944 } 945 return -1; 946 } 947 948 int 949 ata_limit_mode(device_t dev, int mode, int maxmode) 950 { 951 struct ata_device *atadev = device_get_softc(dev); 952 953 if (maxmode && mode > maxmode) 954 mode = maxmode; 955 956 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0) 957 return min(mode, ata_umode(&atadev->param)); 958 959 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0) 960 return min(mode, ata_wmode(&atadev->param)); 961 962 if (mode > ata_pmode(&atadev->param)) 963 return min(mode, ata_pmode(&atadev->param)); 964 965 return mode; 966 } 967 968 static void 969 bswap(int8_t *buf, int len) 970 { 971 u_int16_t *ptr = (u_int16_t*)(buf + len); 972 973 while (--ptr >= (u_int16_t*)buf) 974 *ptr = ntohs(*ptr); 975 } 976 977 static void 978 btrim(int8_t *buf, int len) 979 { 980 int8_t *ptr; 981 982 for (ptr = buf; ptr < buf+len; ++ptr) 983 if (!*ptr || *ptr == '_') 984 *ptr = ' '; 985 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 986 *ptr = 0; 987 } 988 989 static void 990 bpack(int8_t *src, int8_t *dst, int len) 991 { 992 int i, j, blank; 993 994 for (i = j = blank = 0 ; i < len; i++) { 995 if (blank && src[i] == ' ') continue; 996 if (blank && src[i] != ' ') { 997 dst[j++] = src[i]; 998 blank = 0; 999 continue; 1000 } 1001 if (src[i] == ' ') { 1002 blank = 1; 1003 if (i == 0) 1004 continue; 1005 } 1006 dst[j++] = src[i]; 1007 } 1008 if (j < len) 1009 dst[j] = 0x00; 1010 } 1011 1012 1013 /* 1014 * module handeling 1015 */ 1016 static int 1017 ata_module_event_handler(module_t mod, int what, void *arg) 1018 { 1019 /* static because we need the reference at destruction time */ 1020 static cdev_t atacdev; 1021 1022 switch (what) { 1023 case MOD_LOAD: 1024 /* register controlling device */ 1025 atacdev = make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1026 reference_dev(atacdev); 1027 return 0; 1028 1029 case MOD_UNLOAD: 1030 /* deregister controlling device */ 1031 destroy_dev(atacdev); 1032 dev_ops_remove_all(&ata_ops); 1033 return 0; 1034 1035 default: 1036 return EOPNOTSUPP; 1037 } 1038 } 1039 1040 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL }; 1041 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 1042 MODULE_VERSION(ata, 1); 1043 1044 /* 1045 * Construct a completely zero'ed ata_request. On objcache_put(), an 1046 * ata_request object is also zero'ed, so objcache_get() is guaranteed to give 1047 * completely zero'ed objects without spending too much time. 1048 */ 1049 static boolean_t 1050 ata_request_cache_ctor(void *obj, void *private, int ocflags) 1051 { 1052 struct ata_request *arp = obj; 1053 1054 bzero(arp, sizeof(struct ata_request)); 1055 return(TRUE); 1056 } 1057 1058 /* 1059 * Construct a completely zero'ed ata_composite. On objcache_put(), an 1060 * ata_composite object is also zero'ed, so objcache_get() is guaranteed to give 1061 * completely zero'ed objects without spending too much time. 1062 */ 1063 static boolean_t 1064 ata_composite_cache_ctor(void *obj, void *private, int ocflags) 1065 { 1066 struct ata_composite *acp = obj; 1067 1068 bzero(acp, sizeof(struct ata_composite)); 1069 return(TRUE); 1070 } 1071 1072 static void 1073 ata_init(void) 1074 { 1075 ata_request_cache = objcache_create("ata_request", 0, 0, 1076 ata_request_cache_ctor, NULL, NULL, 1077 objcache_malloc_alloc, 1078 objcache_malloc_free, 1079 &ata_request_malloc_args); 1080 ata_composite_cache = objcache_create("ata_composite", 0, 0, 1081 ata_composite_cache_ctor, NULL, NULL, 1082 objcache_malloc_alloc, 1083 objcache_malloc_free, 1084 &ata_composite_malloc_args); 1085 } 1086 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL); 1087 1088 static void 1089 ata_uninit(void) 1090 { 1091 objcache_destroy(ata_composite_cache); 1092 objcache_destroy(ata_request_cache); 1093 } 1094 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL); 1095