ata-all.c revision 120880
1/*- 2 * Copyright (c) 1998 - 2003 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/dev/ata/ata-all.c 120880 2003-10-07 13:44:15Z sos $"); 31 32#include "opt_ata.h" 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/ata.h> 36#include <sys/kernel.h> 37#include <sys/endian.h> 38#include <sys/conf.h> 39#include <sys/bus.h> 40#include <sys/bio.h> 41#include <sys/malloc.h> 42#include <sys/mutex.h> 43#include <sys/sysctl.h> 44#include <sys/taskqueue.h> 45#include <machine/stdarg.h> 46#include <machine/resource.h> 47#include <machine/bus.h> 48#include <sys/rman.h> 49#ifdef __alpha__ 50#include <machine/md_var.h> 51#endif 52#include <geom/geom_disk.h> 53#include <dev/ata/ata-all.h> 54#include <dev/ata/ata-disk.h> 55#include <dev/ata/ata-raid.h> 56 57/* device structures */ 58static d_ioctl_t ata_ioctl; 59static struct cdevsw ata_cdevsw = { 60 .d_ioctl = ata_ioctl, 61 .d_name = "ata", 62 .d_maj = 159, 63}; 64 65/* prototypes */ 66static void ata_shutdown(void *, int); 67static int ata_getparam(struct ata_device *, u_int8_t); 68static void ata_identify_devices(struct ata_channel *); 69static void ata_boot_attach(void); 70static void bswap(int8_t *, int); 71static void btrim(int8_t *, int); 72static void bpack(int8_t *, int8_t *, int); 73static void ata_init(void); 74 75/* sysctl vars */ 76SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 77TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 78TUNABLE_INT("hw.ata.wc", &ata_wc); 79TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 80int ata_dma = 1; 81int ata_wc = 1; 82int atapi_dma = 0; 83 84/* global vars */ 85struct intr_config_hook *ata_delayed_attach = NULL; 86devclass_t ata_devclass; 87 88/* local vars */ 89static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 90 91/* 92 * newbus device interface related functions 93 */ 94int 95ata_probe(device_t dev) 96{ 97 struct ata_channel *ch; 98 99 if (!dev || !(ch = device_get_softc(dev))) 100 return ENXIO; 101 102 if (ch->r_irq) 103 return EEXIST; 104 105 /* initialize the softc basics */ 106 ata_generic_hw(ch); 107 ch->device[MASTER].channel = ch; 108 ch->device[MASTER].unit = ATA_MASTER; 109 ch->device[MASTER].mode = ATA_PIO; 110 ch->device[SLAVE].channel = ch; 111 ch->device[SLAVE].unit = ATA_SLAVE; 112 ch->device[SLAVE].mode = ATA_PIO; 113 ch->dev = dev; 114 ch->state = ATA_IDLE; 115 bzero(&ch->queue_mtx, sizeof(struct mtx)); 116 mtx_init(&ch->queue_mtx, "ATA queue lock", MTX_DEF, 0); 117 TAILQ_INIT(&ch->ata_queue); 118 119 /* initialise device(s) on this channel */ 120 ch->locking(ch, ATA_LF_LOCK); 121 ch->hw.reset(ch); 122 ch->locking(ch, ATA_LF_UNLOCK); 123 return 0; 124} 125 126int 127ata_attach(device_t dev) 128{ 129 struct ata_channel *ch; 130 int error, rid; 131 132 if (!dev || !(ch = device_get_softc(dev))) 133 return ENXIO; 134 135 rid = ATA_IRQ_RID; 136 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 137 RF_SHAREABLE | RF_ACTIVE); 138 if (!ch->r_irq) { 139 ata_printf(ch, -1, "unable to allocate interrupt\n"); 140 return ENXIO; 141 } 142 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, 143 ch->hw.interrupt, ch, &ch->ih))) { 144 ata_printf(ch, -1, "unable to setup interrupt\n"); 145 return error; 146 } 147 148 if (ch->dma) 149 ch->dma->alloc(ch); 150 151 /* do not attach devices if we are in early boot */ 152 if (ata_delayed_attach) 153 return 0; 154 155 ata_identify_devices(ch); 156 157 if (ch->device[MASTER].attach) 158 ch->device[MASTER].attach(&ch->device[MASTER]); 159 if (ch->device[SLAVE].attach) 160 ch->device[SLAVE].attach(&ch->device[SLAVE]); 161#ifdef DEV_ATAPICAM 162 atapi_cam_attach_bus(ch); 163#endif 164 return 0; 165} 166 167int 168ata_detach(device_t dev) 169{ 170 struct ata_channel *ch; 171 struct ata_request *request; 172 173 if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq) 174 return ENXIO; 175 176 /* detach devices on this channel */ 177 if (ch->device[MASTER].detach) 178 ch->device[MASTER].detach(&ch->device[MASTER]); 179 if (ch->device[SLAVE].detach) 180 ch->device[SLAVE].detach(&ch->device[SLAVE]); 181#ifdef DEV_ATAPICAM 182 atapi_cam_detach_bus(ch); 183#endif 184 185 /* fail outstanding requests on this channel */ 186 mtx_lock(&ch->queue_mtx); 187 while ((request = TAILQ_FIRST(&ch->ata_queue))) { 188 TAILQ_REMOVE(&ch->ata_queue, request, chain); 189 request->status = ATA_S_ERROR; 190 mtx_unlock(&ch->queue_mtx); 191 ata_finish(request); 192 mtx_lock(&ch->queue_mtx); 193 } 194 mtx_unlock(&ch->queue_mtx); 195 196 if (ch->device[MASTER].param) { 197 if (ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 198 ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0); 199 free(ch->device[MASTER].param, M_ATA); 200 ch->device[MASTER].param = NULL; 201 } 202 if (ch->device[SLAVE].param) { 203 if (ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 204 ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0); 205 free(ch->device[SLAVE].param, M_ATA); 206 ch->device[SLAVE].param = NULL; 207 } 208 ch->device[MASTER].mode = ATA_PIO; 209 ch->device[SLAVE].mode = ATA_PIO; 210 ch->devices = 0; 211 212 if (ch->dma) 213 ch->dma->free(ch); 214 215 bus_teardown_intr(dev, ch->r_irq, ch->ih); 216 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 217 ch->r_irq = NULL; 218 return 0; 219} 220 221int 222ata_reinit(struct ata_channel *ch) 223{ 224 struct ata_request *request = ch->running; 225 int devices, misdev, newdev; 226 227 if (!ch->r_irq) 228 return ENXIO; 229 230 /* reset the HW */ 231 ata_printf(ch, -1, "resetting devices ..\n"); 232 ATA_FORCELOCK_CH(ch, ATA_CONTROL); 233 ch->running = NULL; 234 devices = ch->devices; 235 ch->hw.reset(ch); 236 ATA_UNLOCK_CH(ch); 237 238 /* detach what left the channel during reset */ 239 if ((misdev = devices & ~ch->devices)) { 240 if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 241 ch->device[MASTER].detach) { 242 if (request && (request->device == &ch->device[MASTER])) { 243 request->result = ENXIO; 244 request->flags |= ATA_R_DONE; 245 if (request->callback) 246 (request->callback)(request); 247 else 248 wakeup(request); 249 } 250 ch->device[MASTER].detach(&ch->device[MASTER]); 251 } 252 if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 253 ch->device[SLAVE].detach) { 254 if (request && (request->device == &ch->device[SLAVE])) { 255 request->result = ENXIO; 256 request->flags |= ATA_R_DONE; 257 if (request->callback) 258 (request->callback)(request); 259 else 260 wakeup(request); 261 } 262 ch->device[SLAVE].detach(&ch->device[SLAVE]); 263 } 264 } 265 266 /* identify whats present on this channel now */ 267 ata_identify_devices(ch); 268 269 /* attach new devices that appeared during reset */ 270 if ((newdev = ~devices & ch->devices)) { 271 if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 272 ch->device[MASTER].attach) 273 ch->device[MASTER].attach(&ch->device[MASTER]); 274 if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 275 ch->device[SLAVE].attach) 276 ch->device[SLAVE].attach(&ch->device[SLAVE]); 277 } 278#ifdef DEV_ATAPICAM 279 atapi_cam_reinit_bus(ch); 280#endif 281 282 printf("done\n"); 283 return 0; 284} 285 286int 287ata_suspend(device_t dev) 288{ 289 struct ata_channel *ch; 290 291 if (!dev || !(ch = device_get_softc(dev))) 292 return ENXIO; 293 294 ch->locking(ch, ATA_LF_LOCK); 295 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL); 296 return 0; 297} 298 299int 300ata_resume(device_t dev) 301{ 302 struct ata_channel *ch; 303 int error; 304 305 if (!dev || !(ch = device_get_softc(dev))) 306 return ENXIO; 307 308 ch->locking(ch, ATA_LF_LOCK); 309 error = ata_reinit(ch); 310 ch->locking(ch, ATA_LF_UNLOCK); 311 ata_start(ch); 312 return error; 313} 314 315static void 316ata_shutdown(void *arg, int howto) 317{ 318 struct ata_channel *ch; 319 int ctlr; 320 321 /* flush cache on all devices */ 322 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) { 323 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 324 continue; 325 if (ch->device[MASTER].param && 326 ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 327 ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0); 328 if (ch->device[SLAVE].param && 329 ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE) 330 ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0); 331 } 332} 333 334/* 335 * device related interfaces 336 */ 337static int 338ata_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 339{ 340 struct ata_cmd *iocmd = (struct ata_cmd *)addr; 341 device_t device = devclass_get_device(ata_devclass, iocmd->channel); 342 struct ata_channel *ch; 343 struct ata_device *atadev; 344 struct ata_request *request; 345 caddr_t buf; 346 int error = ENOTTY; 347 348 DROP_GIANT(); 349 switch (iocmd->cmd) { 350 case ATAGMAXCHANNEL: 351 iocmd->u.maxchan = devclass_get_maxunit(ata_devclass); 352 error = 0; 353 break; 354 355 case ATAGPARM: 356 if (!device || !(ch = device_get_softc(device))) { 357 error = ENXIO; 358 break; 359 } 360 iocmd->u.param.type[MASTER] = 361 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 362 iocmd->u.param.type[SLAVE] = 363 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 364 if (ch->device[MASTER].name) 365 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 366 if (ch->device[SLAVE].name) 367 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 368 if (ch->device[MASTER].param) 369 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 370 sizeof(struct ata_params)); 371 if (ch->device[SLAVE].param) 372 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 373 sizeof(struct ata_params)); 374 error = 0; 375 break; 376 377 case ATAGMODE: 378 if (!device || !(ch = device_get_softc(device))) { 379 error = ENXIO; 380 break; 381 } 382 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 383 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 384 error = 0; 385 break; 386 387 case ATASMODE: 388 if (!device || !(ch = device_get_softc(device))) { 389 error = ENXIO; 390 break; 391 } 392 if (iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) 393 ch->device[MASTER].setmode(&ch->device[MASTER], 394 iocmd->u.mode.mode[MASTER]); 395 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 396 if (iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) 397 ch->device[SLAVE].setmode(&ch->device[SLAVE], 398 iocmd->u.mode.mode[SLAVE]); 399 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 400 error = 0; 401 break; 402 403 case ATAREQUEST: 404 if (!device || !(ch = device_get_softc(device))) { 405 error = ENXIO; 406 break; 407 } 408 if (!(atadev = &ch->device[iocmd->device])) { 409 error = ENODEV; 410 break; 411 } 412 if (!(buf = malloc(iocmd->u.request.count, M_ATA, M_NOWAIT))) { 413 error = ENOMEM; 414 break; 415 } 416 if (!(request = ata_alloc_request())) { 417 error = ENOMEM; 418 free(buf, M_ATA); 419 break; 420 } 421 if (iocmd->u.request.flags & ATA_CMD_WRITE) { 422 error = copyin(iocmd->u.request.data, buf, iocmd->u.request.count); 423 if (error) { 424 free(buf, M_ATA); 425 ata_free_request(request); 426 break; 427 } 428 } 429 430 request->device = atadev; 431 432 if (iocmd->u.request.flags & ATA_CMD_ATAPI) { 433 request->flags = ATA_R_ATAPI; 434 bcopy(iocmd->u.request.u.atapi.ccb, request->u.atapi.ccb, 16); 435 } 436 else { 437 request->u.ata.command = iocmd->u.request.u.ata.command; 438 request->u.ata.feature = iocmd->u.request.u.ata.feature; 439 request->u.ata.lba = iocmd->u.request.u.ata.lba; 440 request->u.ata.count = iocmd->u.request.u.ata.count; 441 } 442 443 request->timeout = iocmd->u.request.timeout; 444 request->data = buf; 445 request->bytecount = iocmd->u.request.count; 446 request->transfersize = request->bytecount; 447 448 if (iocmd->u.request.flags & ATA_CMD_CONTROL) 449 request->flags |= ATA_R_CONTROL; 450 if (iocmd->u.request.flags & ATA_CMD_READ) 451 request->flags |= ATA_R_READ; 452 if (iocmd->u.request.flags & ATA_CMD_WRITE) 453 request->flags |= ATA_R_WRITE; 454 455 ata_queue_request(request); 456 457 if (request->result) 458 iocmd->u.request.error = request->result; 459 else { 460 if (iocmd->u.request.flags & ATA_CMD_READ) 461 error = copyout(buf, 462 iocmd->u.request.data, iocmd->u.request.count); 463 else 464 error = 0; 465 } 466 free(buf, M_ATA); 467 ata_free_request(request); 468 break; 469 470 case ATAREINIT: 471 if (!device || !(ch = device_get_softc(device))) 472 return ENXIO; 473 error = ata_reinit(ch); 474 ata_start(ch); 475 break; 476 477 case ATAATTACH: 478 if (!device) { 479 error = ENXIO; 480 break; 481 } 482 /* SOS should enable channel HW on controller XXX */ 483 error = ata_probe(device); 484 if (!error) 485 error = ata_attach(device); 486 break; 487 488 case ATADETACH: 489 if (!device) { 490 error = ENXIO; 491 break; 492 } 493 error = ata_detach(device); 494 /* SOS should disable channel HW on controller XXX */ 495 break; 496 497 498#ifdef DEV_ATARAID 499 case ATARAIDCREATE: 500 error = ata_raid_create(&iocmd->u.raid_setup); 501 break; 502 503 case ATARAIDDELETE: 504 error = ata_raid_delete(iocmd->channel); 505 break; 506 507 case ATARAIDSTATUS: 508 error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 509 break; 510 511 case ATARAIDADDSPARE: 512 error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk); 513 break; 514 515 case ATARAIDREBUILD: 516 error = ata_raid_rebuild(iocmd->channel); 517 break; 518#endif 519 } 520 PICKUP_GIANT(); 521 return error; 522} 523 524/* 525 * device probe functions 526 */ 527static int 528ata_getparam(struct ata_device *atadev, u_int8_t command) 529{ 530 struct ata_params *atacap; 531 struct ata_request *request; 532 int error = ENOMEM; 533 534 if (atadev->param) 535 atacap = atadev->param; 536 else 537 atacap = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 538 if (atacap) { 539 request = ata_alloc_request(); 540 if (request) { 541 request->device = atadev; 542 request->u.ata.command = command; 543 request->flags = (ATA_R_READ | ATA_R_QUIET); 544 request->data = (caddr_t)atacap; 545 request->timeout = 2; 546 request->retries = 3; 547 request->bytecount = sizeof(struct ata_params); 548 request->transfersize = DEV_BSIZE; 549 while (request->retries) { 550 ata_queue_request(request); 551 if (!(error = request->result)) 552 break; 553 request->flags &= ~ATA_R_QUIET; 554 request->retries--; 555 } 556 ata_free_request(request); 557 } 558 if (error) { 559 atadev->param = NULL; 560 free(atacap, M_ATA); 561 } 562 else { 563#if BYTE_ORDER == BIG_ENDIAN 564 int16_t *ptr; 565 566 for (ptr = (int16_t *)atacap; 567 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 568 *ptr = bswap16(*ptr); 569 } 570#endif 571 if (!((atacap->model[0] == 'N' && atacap->model[1] == 'E') || 572 (atacap->model[0] == 'F' && atacap->model[1] == 'X') || 573 (atacap->model[0] == 'P' && atacap->model[1] == 'i'))) 574 bswap(atacap->model, sizeof(atacap->model)); 575 btrim(atacap->model, sizeof(atacap->model)); 576 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 577 bswap(atacap->revision, sizeof(atacap->revision)); 578 btrim(atacap->revision, sizeof(atacap->revision)); 579 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 580 bswap(atacap->serial, sizeof(atacap->serial)); 581 btrim(atacap->serial, sizeof(atacap->serial)); 582 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 583 atadev->param = atacap; 584 if (bootverbose) 585 ata_prtdev(atadev, 586 "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n", 587 ata_pmode(atacap), ata_wmode(atacap), 588 ata_umode(atacap), 589 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 590 } 591 } 592 return error; 593} 594 595static void 596ata_identify_devices(struct ata_channel *ch) 597{ 598 if (ch->devices & ATA_ATA_SLAVE) { 599 if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY)) 600 ch->devices &= ~ATA_ATA_SLAVE; 601#ifdef DEV_ATADISK 602 else 603 ch->device[SLAVE].attach = ad_attach; 604#endif 605 } 606 if (ch->devices & ATA_ATAPI_SLAVE) { 607 if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY)) 608 ch->devices &= ~ATA_ATAPI_SLAVE; 609 else { 610 switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) { 611#ifdef DEV_ATAPICD 612 case ATA_ATAPI_TYPE_CDROM: 613 ch->device[SLAVE].attach = acd_attach; 614 break; 615#endif 616#ifdef DEV_ATAPIFD 617 case ATA_ATAPI_TYPE_DIRECT: 618 ch->device[SLAVE].attach = afd_attach; 619 break; 620#endif 621#ifdef DEV_ATAPIST 622 case ATA_ATAPI_TYPE_TAPE: 623 ch->device[SLAVE].attach = ast_attach; 624 break; 625#endif 626 } 627 } 628 } 629 if (ch->devices & ATA_ATA_MASTER) { 630 if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY)) 631 ch->devices &= ~ATA_ATA_MASTER; 632#ifdef DEV_ATADISK 633 else 634 ch->device[MASTER].attach = ad_attach; 635#endif 636 } 637 if (ch->devices & ATA_ATAPI_MASTER) { 638 if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY)) 639 ch->devices &= ~ATA_ATAPI_MASTER; 640 else { 641 switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) { 642#ifdef DEV_ATAPICD 643 case ATA_ATAPI_TYPE_CDROM: 644 ch->device[MASTER].attach = acd_attach; 645 break; 646#endif 647#ifdef DEV_ATAPIFD 648 case ATA_ATAPI_TYPE_DIRECT: 649 ch->device[MASTER].attach = afd_attach; 650 break; 651#endif 652#ifdef DEV_ATAPIST 653 case ATA_ATAPI_TYPE_TAPE: 654 ch->device[MASTER].attach = ast_attach; 655 break; 656#endif 657 } 658 } 659 } 660} 661 662static void 663ata_boot_attach(void) 664{ 665 struct ata_channel *ch; 666 int ctlr; 667 668 /* 669 * run through all ata devices and look for real ATA & ATAPI devices 670 * using the hints we found in the early probe, this avoids some of 671 * the delays probing of non-exsistent devices can cause. 672 */ 673 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 674 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 675 continue; 676 ata_identify_devices(ch); 677 if (ch->device[MASTER].attach) 678 ch->device[MASTER].attach(&ch->device[MASTER]); 679 if (ch->device[SLAVE].attach) 680 ch->device[SLAVE].attach(&ch->device[SLAVE]); 681#ifdef DEV_ATAPICAM 682 atapi_cam_attach_bus(ch); 683#endif 684 } 685#ifdef DEV_ATARAID 686 ata_raid_attach(); 687#endif 688 if (ata_delayed_attach) { 689 config_intrhook_disestablish(ata_delayed_attach); 690 free(ata_delayed_attach, M_TEMP); 691 ata_delayed_attach = NULL; 692 } 693} 694 695/* 696 * misc support functions 697 */ 698static void 699bswap(int8_t *buf, int len) 700{ 701 u_int16_t *ptr = (u_int16_t*)(buf + len); 702 703 while (--ptr >= (u_int16_t*)buf) 704 *ptr = ntohs(*ptr); 705} 706 707static void 708btrim(int8_t *buf, int len) 709{ 710 int8_t *ptr; 711 712 for (ptr = buf; ptr < buf+len; ++ptr) 713 if (!*ptr) 714 *ptr = ' '; 715 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 716 *ptr = 0; 717} 718 719static void 720bpack(int8_t *src, int8_t *dst, int len) 721{ 722 int i, j, blank; 723 724 for (i = j = blank = 0 ; i < len; i++) { 725 if (blank && src[i] == ' ') continue; 726 if (blank && src[i] != ' ') { 727 dst[j++] = src[i]; 728 blank = 0; 729 continue; 730 } 731 if (src[i] == ' ') { 732 blank = 1; 733 if (i == 0) 734 continue; 735 } 736 dst[j++] = src[i]; 737 } 738 if (j < len) 739 dst[j] = 0x00; 740} 741 742int 743ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 744{ 745 va_list ap; 746 int ret; 747 748 if (device == -1) 749 ret = printf("ata%d: ", device_get_unit(ch->dev)); 750 else { 751 if (ch->device[ATA_DEV(device)].name) 752 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 753 else 754 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 755 (device == ATA_MASTER) ? "master" : "slave"); 756 } 757 va_start(ap, fmt); 758 ret += vprintf(fmt, ap); 759 va_end(ap); 760 return ret; 761} 762 763int 764ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 765{ 766 va_list ap; 767 int ret; 768 769 if (atadev->name) 770 ret = printf("%s: ", atadev->name); 771 else 772 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 773 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 774 va_start(ap, fmt); 775 ret += vprintf(fmt, ap); 776 va_end(ap); 777 return ret; 778} 779 780void 781ata_set_name(struct ata_device *atadev, char *name, int lun) 782{ 783 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 784 if (atadev->name) 785 sprintf(atadev->name, "%s%d", name, lun); 786} 787 788void 789ata_free_name(struct ata_device *atadev) 790{ 791 if (atadev->name) 792 free(atadev->name, M_ATA); 793 atadev->name = NULL; 794} 795 796int 797ata_get_lun(u_int32_t *map) 798{ 799 int lun = ffs(~*map) - 1; 800 801 *map |= (1 << lun); 802 return lun; 803} 804 805int 806ata_test_lun(u_int32_t *map, int lun) 807{ 808 return (*map & (1 << lun)); 809} 810 811void 812ata_free_lun(u_int32_t *map, int lun) 813{ 814 *map &= ~(1 << lun); 815} 816 817char * 818ata_mode2str(int mode) 819{ 820 switch (mode) { 821 case ATA_PIO: return "BIOSPIO"; 822 case ATA_PIO0: return "PIO0"; 823 case ATA_PIO1: return "PIO1"; 824 case ATA_PIO2: return "PIO2"; 825 case ATA_PIO3: return "PIO3"; 826 case ATA_PIO4: return "PIO4"; 827 case ATA_DMA: return "BIOSDMA"; 828 case ATA_WDMA0: return "WDMA0"; 829 case ATA_WDMA1: return "WDMA1"; 830 case ATA_WDMA2: return "WDMA2"; 831 case ATA_UDMA0: return "UDMA16"; 832 case ATA_UDMA1: return "UDMA25"; 833 case ATA_UDMA2: return "UDMA33"; 834 case ATA_UDMA3: return "UDMA40"; 835 case ATA_UDMA4: return "UDMA66"; 836 case ATA_UDMA5: return "UDMA100"; 837 case ATA_UDMA6: return "UDMA133"; 838 case ATA_SA150: return "SATA150"; 839 default: return "???"; 840 } 841} 842 843int 844ata_pmode(struct ata_params *ap) 845{ 846 if (ap->atavalid & ATA_FLAG_64_70) { 847 if (ap->apiomodes & 0x02) 848 return ATA_PIO4; 849 if (ap->apiomodes & 0x01) 850 return ATA_PIO3; 851 } 852 if (ap->mwdmamodes & 0x04) 853 return ATA_PIO4; 854 if (ap->mwdmamodes & 0x02) 855 return ATA_PIO3; 856 if (ap->mwdmamodes & 0x01) 857 return ATA_PIO2; 858 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 859 return ATA_PIO2; 860 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 861 return ATA_PIO1; 862 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 863 return ATA_PIO0; 864 return ATA_PIO0; 865} 866 867int 868ata_wmode(struct ata_params *ap) 869{ 870 if (ap->mwdmamodes & 0x04) 871 return ATA_WDMA2; 872 if (ap->mwdmamodes & 0x02) 873 return ATA_WDMA1; 874 if (ap->mwdmamodes & 0x01) 875 return ATA_WDMA0; 876 return -1; 877} 878 879int 880ata_umode(struct ata_params *ap) 881{ 882 if (ap->atavalid & ATA_FLAG_88) { 883 if (ap->udmamodes & 0x40) 884 return ATA_UDMA6; 885 if (ap->udmamodes & 0x20) 886 return ATA_UDMA5; 887 if (ap->udmamodes & 0x10) 888 return ATA_UDMA4; 889 if (ap->udmamodes & 0x08) 890 return ATA_UDMA3; 891 if (ap->udmamodes & 0x04) 892 return ATA_UDMA2; 893 if (ap->udmamodes & 0x02) 894 return ATA_UDMA1; 895 if (ap->udmamodes & 0x01) 896 return ATA_UDMA0; 897 } 898 return -1; 899} 900 901int 902ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 903{ 904 if (maxmode && mode > maxmode) 905 mode = maxmode; 906 907 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 908 return min(mode, ata_umode(atadev->param)); 909 910 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 911 return min(mode, ata_wmode(atadev->param)); 912 913 if (mode > ata_pmode(atadev->param)) 914 return min(mode, ata_pmode(atadev->param)); 915 916 return mode; 917} 918 919static void 920ata_init(void) 921{ 922 /* register controlling device */ 923 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 924 925 /* register boot attach to be run when interrupts are enabled */ 926 if (!(ata_delayed_attach = (struct intr_config_hook *) 927 malloc(sizeof(struct intr_config_hook), 928 M_TEMP, M_NOWAIT | M_ZERO))) { 929 printf("ata: malloc of delayed attach hook failed\n"); 930 return; 931 } 932 933 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 934 if (config_intrhook_establish(ata_delayed_attach) != 0) { 935 printf("ata: config_intrhook_establish failed\n"); 936 free(ata_delayed_attach, M_TEMP); 937 } 938 /* Register a handler to flush write caches on shutdown */ 939 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown, 940 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 941 printf("ata: shutdown event registration failed!\n"); 942 943} 944SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 945