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