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