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