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 char buffer[64]; 620 int16_t *ptr; 621 622 for (ptr = (int16_t *)atacap; 623 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 624 *ptr = le16toh(*ptr); 625 } 626 if (!(!strncmp(atacap->model, "FX", 2) || 627 !strncmp(atacap->model, "NEC", 3) || 628 !strncmp(atacap->model, "Pioneer", 7) || 629 !strncmp(atacap->model, "SHARP", 5))) { 630 bswap(atacap->model, sizeof(atacap->model)); 631 bswap(atacap->revision, sizeof(atacap->revision)); 632 bswap(atacap->serial, sizeof(atacap->serial)); 633 } 634 btrim(atacap->model, sizeof(atacap->model)); 635 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 636 btrim(atacap->revision, sizeof(atacap->revision)); 637 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 638 btrim(atacap->serial, sizeof(atacap->serial)); 639 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 640 641 if (bootverbose) 642 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n", 643 device_get_unit(ch->dev), 644 ata_unit2str(atadev), 645 ata_mode2str(ata_pmode(atacap)), 646 ata_mode2str(ata_wmode(atacap)), 647 ata_mode2str(ata_umode(atacap)), 648 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 649 650 if (init) { 651 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision); 652 device_set_desc_copy(atadev->dev, buffer); 653 if ((atadev->param.config & ATA_PROTO_ATAPI) && 654 (atadev->param.config != ATA_CFA_MAGIC1) && 655 (atadev->param.config != ATA_CFA_MAGIC2)) { 656 if (atapi_dma && 657 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR && 658 ata_umode(&atadev->param) >= ATA_UDMA2) 659 atadev->mode = ATA_DMA_MAX; 660 } 661 else { 662 if (ata_dma && 663 (ata_umode(&atadev->param) > 0 || 664 ata_wmode(&atadev->param) > 0)) 665 atadev->mode = ATA_DMA_MAX; 666 } 667 } 668 } 669 else { 670 if (!error) 671 error = ENXIO; 672 } 673 return error; 674 } 675 676 int 677 ata_identify(device_t dev) 678 { 679 struct ata_channel *ch = device_get_softc(dev); 680 struct ata_device *devices[ATA_PM]; 681 device_t childdevs[ATA_PM]; 682 int i; 683 684 if (bootverbose) 685 device_printf(dev, "identify ch->devices=%08x\n", ch->devices); 686 687 for (i = 0; i < ATA_PM; ++i) { 688 if (ch->devices & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) { 689 int unit = -1; 690 691 if (!(devices[i] = malloc(sizeof(struct ata_device), 692 M_ATA, M_NOWAIT | M_ZERO))) { 693 device_printf(dev, "out of memory\n"); 694 return ENOMEM; 695 } 696 devices[i]->unit = i; 697 #ifdef ATA_STATIC_ID 698 if (ch->devices & ((ATA_ATA_MASTER << i))) 699 unit = (device_get_unit(dev) << 1) + i; 700 #endif 701 if (!(childdevs[i] = ata_add_child(dev, devices[i], unit))) { 702 free(devices[i], M_ATA); 703 devices[i]=NULL; 704 } 705 else { 706 if (ata_getparam(devices[i], 1)) { 707 device_delete_child(dev, childdevs[i]); 708 free(devices[i], M_ATA); 709 childdevs[i] = NULL; 710 devices[i] = NULL; 711 } 712 } 713 } 714 devices[i] = NULL; 715 childdevs[i] = NULL; 716 } 717 718 bus_generic_probe(dev); 719 bus_generic_attach(dev); 720 return 0; 721 } 722 723 void 724 ata_default_registers(device_t dev) 725 { 726 struct ata_channel *ch = device_get_softc(dev); 727 728 /* fill in the defaults from whats setup already */ 729 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res; 730 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset; 731 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res; 732 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset; 733 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res; 734 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset; 735 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res; 736 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset; 737 } 738 739 void 740 ata_modify_if_48bit(struct ata_request *request) 741 { 742 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev)); 743 struct ata_device *atadev = device_get_softc(request->dev); 744 745 atadev->flags &= ~ATA_D_48BIT_ACTIVE; 746 747 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA || 748 request->u.ata.count > 256) && 749 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 750 751 /* translate command into 48bit version */ 752 switch (request->u.ata.command) { 753 case ATA_READ: 754 request->u.ata.command = ATA_READ48; 755 break; 756 case ATA_READ_MUL: 757 request->u.ata.command = ATA_READ_MUL48; 758 break; 759 case ATA_READ_DMA: 760 if (ch->flags & ATA_NO_48BIT_DMA) { 761 if (request->transfersize > DEV_BSIZE) 762 request->u.ata.command = ATA_READ_MUL48; 763 else 764 request->u.ata.command = ATA_READ48; 765 request->flags &= ~ATA_R_DMA; 766 } 767 else 768 request->u.ata.command = ATA_READ_DMA48; 769 break; 770 case ATA_READ_DMA_QUEUED: 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_DMA_QUEUED48; 780 break; 781 case ATA_WRITE: 782 request->u.ata.command = ATA_WRITE48; 783 break; 784 case ATA_WRITE_MUL: 785 request->u.ata.command = ATA_WRITE_MUL48; 786 break; 787 case ATA_WRITE_DMA: 788 if (ch->flags & ATA_NO_48BIT_DMA) { 789 if (request->transfersize > DEV_BSIZE) 790 request->u.ata.command = ATA_WRITE_MUL48; 791 else 792 request->u.ata.command = ATA_WRITE48; 793 request->flags &= ~ATA_R_DMA; 794 } 795 else 796 request->u.ata.command = ATA_WRITE_DMA48; 797 break; 798 case ATA_WRITE_DMA_QUEUED: 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->u.ata.command = ATA_WRITE48; 805 request->flags &= ~ATA_R_DMA; 806 } 807 else 808 request->u.ata.command = ATA_WRITE_DMA_QUEUED48; 809 break; 810 case ATA_FLUSHCACHE: 811 request->u.ata.command = ATA_FLUSHCACHE48; 812 break; 813 case ATA_SET_MAX_ADDRESS: 814 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 815 break; 816 default: 817 return; 818 } 819 atadev->flags |= ATA_D_48BIT_ACTIVE; 820 } 821 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 822 823 /* translate command into 48bit version */ 824 switch (request->u.ata.command) { 825 case ATA_FLUSHCACHE: 826 request->u.ata.command = ATA_FLUSHCACHE48; 827 break; 828 case ATA_READ_NATIVE_MAX_ADDRESS: 829 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48; 830 break; 831 case ATA_SET_MAX_ADDRESS: 832 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 833 break; 834 default: 835 return; 836 } 837 atadev->flags |= ATA_D_48BIT_ACTIVE; 838 } 839 } 840 841 void 842 ata_udelay(int interval) 843 { 844 /* for now just use DELAY, the timer/sleep subsytems are not there yet */ 845 if (1 || interval < (1000000/hz) || ata_delayed_attach) 846 DELAY(interval); 847 else 848 pause("ataslp", interval/(1000000/hz)); 849 } 850 851 char * 852 ata_unit2str(struct ata_device *atadev) 853 { 854 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev)); 855 static char str[8]; 856 857 if (ch->devices & ATA_PORTMULTIPLIER) 858 sprintf(str, "port%d", atadev->unit); 859 else 860 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave"); 861 return str; 862 } 863 864 char * 865 ata_mode2str(int mode) 866 { 867 switch (mode) { 868 case -1: return "UNSUPPORTED"; 869 case ATA_PIO0: return "PIO0"; 870 case ATA_PIO1: return "PIO1"; 871 case ATA_PIO2: return "PIO2"; 872 case ATA_PIO3: return "PIO3"; 873 case ATA_PIO4: return "PIO4"; 874 case ATA_WDMA0: return "WDMA0"; 875 case ATA_WDMA1: return "WDMA1"; 876 case ATA_WDMA2: return "WDMA2"; 877 case ATA_UDMA0: return "UDMA16"; 878 case ATA_UDMA1: return "UDMA25"; 879 case ATA_UDMA2: return "UDMA33"; 880 case ATA_UDMA3: return "UDMA40"; 881 case ATA_UDMA4: return "UDMA66"; 882 case ATA_UDMA5: return "UDMA100"; 883 case ATA_UDMA6: return "UDMA133"; 884 case ATA_SA150: return "SATA150"; 885 case ATA_SA300: return "SATA300"; 886 case ATA_USB: return "USB"; 887 case ATA_USB1: return "USB1"; 888 case ATA_USB2: return "USB2"; 889 default: 890 if (mode & ATA_DMA_MASK) 891 return "BIOSDMA"; 892 else 893 return "BIOSPIO"; 894 } 895 } 896 897 int 898 ata_pmode(struct ata_params *ap) 899 { 900 if (ap->atavalid & ATA_FLAG_64_70) { 901 if (ap->apiomodes & 0x02) 902 return ATA_PIO4; 903 if (ap->apiomodes & 0x01) 904 return ATA_PIO3; 905 } 906 if (ap->mwdmamodes & 0x04) 907 return ATA_PIO4; 908 if (ap->mwdmamodes & 0x02) 909 return ATA_PIO3; 910 if (ap->mwdmamodes & 0x01) 911 return ATA_PIO2; 912 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 913 return ATA_PIO2; 914 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 915 return ATA_PIO1; 916 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 917 return ATA_PIO0; 918 return ATA_PIO0; 919 } 920 921 int 922 ata_wmode(struct ata_params *ap) 923 { 924 if (ap->mwdmamodes & 0x04) 925 return ATA_WDMA2; 926 if (ap->mwdmamodes & 0x02) 927 return ATA_WDMA1; 928 if (ap->mwdmamodes & 0x01) 929 return ATA_WDMA0; 930 return -1; 931 } 932 933 int 934 ata_umode(struct ata_params *ap) 935 { 936 if (ap->atavalid & ATA_FLAG_88) { 937 if (ap->udmamodes & 0x40) 938 return ATA_UDMA6; 939 if (ap->udmamodes & 0x20) 940 return ATA_UDMA5; 941 if (ap->udmamodes & 0x10) 942 return ATA_UDMA4; 943 if (ap->udmamodes & 0x08) 944 return ATA_UDMA3; 945 if (ap->udmamodes & 0x04) 946 return ATA_UDMA2; 947 if (ap->udmamodes & 0x02) 948 return ATA_UDMA1; 949 if (ap->udmamodes & 0x01) 950 return ATA_UDMA0; 951 } 952 return -1; 953 } 954 955 int 956 ata_limit_mode(device_t dev, int mode, int maxmode) 957 { 958 struct ata_device *atadev = device_get_softc(dev); 959 960 if (maxmode && mode > maxmode) 961 mode = maxmode; 962 963 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0) 964 return min(mode, ata_umode(&atadev->param)); 965 966 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0) 967 return min(mode, ata_wmode(&atadev->param)); 968 969 if (mode > ata_pmode(&atadev->param)) 970 return min(mode, ata_pmode(&atadev->param)); 971 972 return mode; 973 } 974 975 static void 976 bswap(int8_t *buf, int len) 977 { 978 u_int16_t *ptr = (u_int16_t*)(buf + len); 979 980 while (--ptr >= (u_int16_t*)buf) 981 *ptr = ntohs(*ptr); 982 } 983 984 static void 985 btrim(int8_t *buf, int len) 986 { 987 int8_t *ptr; 988 989 for (ptr = buf; ptr < buf+len; ++ptr) 990 if (!*ptr || *ptr == '_') 991 *ptr = ' '; 992 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 993 *ptr = 0; 994 } 995 996 static void 997 bpack(int8_t *src, int8_t *dst, int len) 998 { 999 int i, j, blank; 1000 1001 for (i = j = blank = 0 ; i < len; i++) { 1002 if (blank && src[i] == ' ') continue; 1003 if (blank && src[i] != ' ') { 1004 dst[j++] = src[i]; 1005 blank = 0; 1006 continue; 1007 } 1008 if (src[i] == ' ') { 1009 blank = 1; 1010 if (i == 0) 1011 continue; 1012 } 1013 dst[j++] = src[i]; 1014 } 1015 if (j < len) 1016 dst[j] = 0x00; 1017 } 1018 1019 1020 /* 1021 * module handeling 1022 */ 1023 static int 1024 ata_module_event_handler(module_t mod, int what, void *arg) 1025 { 1026 static struct cdev *atacdev; 1027 1028 switch (what) { 1029 case MOD_LOAD: 1030 /* register controlling device */ 1031 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1032 1033 if (cold) { 1034 /* register boot attach to be run when interrupts are enabled */ 1035 if (!(ata_delayed_attach = (struct intr_config_hook *) 1036 malloc(sizeof(struct intr_config_hook), 1037 M_TEMP, M_NOWAIT | M_ZERO))) { 1038 printf("ata: malloc of delayed attach hook failed\n"); 1039 return EIO; 1040 } 1041 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1042 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1043 printf("ata: config_intrhook_establish failed\n"); 1044 free(ata_delayed_attach, M_TEMP); 1045 } 1046 } 1047 return 0; 1048 1049 case MOD_UNLOAD: 1050 /* deregister controlling device */ 1051 destroy_dev(atacdev); 1052 return 0; 1053 1054 default: 1055 return EOPNOTSUPP; 1056 } 1057 } 1058 1059 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL }; 1060 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 1061 MODULE_VERSION(ata, 1); 1062 1063 static void 1064 ata_init(void) 1065 { 1066 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request), 1067 NULL, NULL, NULL, NULL, 0, 0); 1068 ata_composite_zone = uma_zcreate("ata_composite", 1069 sizeof(struct ata_composite), 1070 NULL, NULL, NULL, NULL, 0, 0); 1071 } 1072 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL); 1073 1074 static void 1075 ata_uninit(void) 1076 { 1077 uma_zdestroy(ata_composite_zone); 1078 uma_zdestroy(ata_request_zone); 1079 } 1080 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL); 1081