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