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