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 ch->device[MASTER].channel = ch; 117 ch->device[MASTER].unit = ATA_MASTER; 118 ch->device[MASTER].mode = ATA_PIO; 119 ch->device[SLAVE].channel = ch; 120 ch->device[SLAVE].unit = ATA_SLAVE; 121 ch->device[SLAVE].mode = ATA_PIO; 122 ch->dev = dev; 123 ch->lock = ATA_IDLE; 124 125 /* initialise device(s) on this channel */ 126 ch->locking(ch, ATA_LF_LOCK); 127 ch->hw.reset(ch); 128 ch->locking(ch, ATA_LF_UNLOCK); 129 return 0; 130 } 131 132 int 133 ata_attach(device_t dev) 134 { 135 struct ata_channel *ch; 136 int error, rid; 137 138 if (!dev || !(ch = device_get_softc(dev))) 139 return ENXIO; 140 141 rid = ATA_IRQ_RID; 142 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 143 RF_SHAREABLE | RF_ACTIVE); 144 if (!ch->r_irq) { 145 ata_printf(ch, -1, "unable to allocate interrupt\n"); 146 return ENXIO; 147 } 148 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, 149 ch->hw.interrupt, ch, &ch->ih))) { 150 ata_printf(ch, -1, "unable to setup interrupt\n"); 151 return error; 152 } 153 154 if (ch->dma) 155 ch->dma->alloc(ch); 156 157 /* initialize queue and associated lock */ 158 bzero(&ch->queue_mtx, sizeof(struct mtx)); 159 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF); 160 TAILQ_INIT(&ch->ata_queue); 161 162 /* do not attach devices if we are in early boot */ 163 if (ata_delayed_attach) 164 return 0; 165 166 ata_identify_devices(ch); 167 168 if (ch->device[MASTER].attach) 169 ch->device[MASTER].attach(&ch->device[MASTER]); 170 if (ch->device[SLAVE].attach) 171 ch->device[SLAVE].attach(&ch->device[SLAVE]); 172 #ifdef DEV_ATAPICAM 173 atapi_cam_attach_bus(ch); 174 #endif 175 return 0; 176 } 177 178 int 179 ata_detach(device_t dev) 180 { 181 struct ata_channel *ch; 182 183 if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq) 184 return ENXIO; 185 186 /* mark devices on this channel as detaching */ 187 ch->device[MASTER].flags |= ATA_D_DETACHING; 188 ch->device[SLAVE].flags |= ATA_D_DETACHING; 189 190 /* fail outstanding requests on this channel */ 191 ata_fail_requests(ch, NULL); 192 193 /* detach devices on this channel */ 194 if (ch->device[MASTER].detach) 195 ch->device[MASTER].detach(&ch->device[MASTER]); 196 if (ch->device[SLAVE].detach) 197 ch->device[SLAVE].detach(&ch->device[SLAVE]); 198 #ifdef DEV_ATAPICAM 199 atapi_cam_detach_bus(ch); 200 #endif 201 202 /* flush cache and powerdown device */ 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 mtx_destroy(&ch->queue_mtx); 228 return 0; 229 } 230 231 int 232 ata_reinit(struct ata_channel *ch) 233 { 234 struct ata_request *request = ch->running; 235 int devices, misdev, newdev; 236 237 if (!ch->r_irq) 238 return ENXIO; 239 240 /* reset the HW */ 241 if (bootverbose) 242 ata_printf(ch, -1, "reiniting channel ..\n"); 243 ATA_FORCELOCK_CH(ch); 244 ch->flags |= ATA_IMMEDIATE_MODE; 245 ch->running = NULL; 246 devices = ch->devices; 247 ch->hw.reset(ch); 248 ATA_UNLOCK_CH(ch); 249 250 if (bootverbose) 251 ata_printf(ch, -1, "resetting done ..\n"); 252 253 /* detach what left the channel during reset */ 254 if ((misdev = devices & ~ch->devices)) { 255 if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 256 ch->device[MASTER].detach) { 257 if (request && (request->device == &ch->device[MASTER])) { 258 request->result = ENXIO; 259 request->retries = 0; 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 request->retries = 0; 271 } 272 ch->device[SLAVE].detach(&ch->device[SLAVE]); 273 ata_fail_requests(ch, &ch->device[SLAVE]); 274 free(ch->device[SLAVE].param, M_ATA); 275 ch->device[SLAVE].param = NULL; 276 } 277 } 278 279 /* identify what is present on the channel now */ 280 ata_identify_devices(ch); 281 282 /* attach new devices that appeared during reset */ 283 if ((newdev = ~devices & ch->devices)) { 284 if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) && 285 ch->device[MASTER].attach) 286 ch->device[MASTER].attach(&ch->device[MASTER]); 287 if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) && 288 ch->device[SLAVE].attach) 289 ch->device[SLAVE].attach(&ch->device[SLAVE]); 290 } 291 292 #ifdef DEV_ATAPICAM 293 atapi_cam_reinit_bus(ch); 294 #endif 295 296 if (bootverbose) 297 ata_printf(ch, -1, "device config done ..\n"); 298 ch->flags &= ~ATA_IMMEDIATE_MODE; 299 ata_start(ch); 300 return 0; 301 } 302 303 int 304 ata_suspend(device_t dev) 305 { 306 struct ata_channel *ch; 307 308 if (!dev || !(ch = device_get_softc(dev))) 309 return ENXIO; 310 ch->locking(ch, ATA_LF_LOCK); 311 ATA_SLEEPLOCK_CH(ch); 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(struct cdev *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_request *request; 547 int error = ENOMEM; 548 549 if (!atadev->param) 550 atadev->param = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT); 551 if (atadev->param) { 552 request = ata_alloc_request(); 553 if (request) { 554 int retries = 2; 555 while (retries-- > 0) { 556 request->device = atadev; 557 request->timeout = 5; 558 request->retries = -1; 559 request->u.ata.command = command; 560 request->flags = (ATA_R_READ | ATA_R_IMMEDIATE); 561 request->data = (caddr_t)atadev->param; 562 request->bytecount = sizeof(struct ata_params); 563 request->donecount = 0; 564 request->transfersize = DEV_BSIZE; 565 ata_queue_request(request); 566 if (!(error = request->result)) 567 break; 568 } 569 ata_free_request(request); 570 } 571 if (!error && (isprint(atadev->param->model[0]) || 572 isprint(atadev->param->model[1]))) { 573 struct ata_params *atacap = atadev->param; 574 #if BYTE_ORDER == BIG_ENDIAN 575 int16_t *ptr; 576 577 for (ptr = (int16_t *)atacap; 578 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 579 *ptr = bswap16(*ptr); 580 } 581 #endif 582 if (!((atacap->model[0] == 'N' && atacap->model[1] == 'E') || 583 (atacap->model[0] == 'F' && atacap->model[1] == 'X') || 584 (atacap->model[0] == 'P' && atacap->model[1] == 'i'))) 585 bswap(atacap->model, sizeof(atacap->model)); 586 btrim(atacap->model, sizeof(atacap->model)); 587 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 588 bswap(atacap->revision, sizeof(atacap->revision)); 589 btrim(atacap->revision, sizeof(atacap->revision)); 590 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 591 bswap(atacap->serial, sizeof(atacap->serial)); 592 btrim(atacap->serial, sizeof(atacap->serial)); 593 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 594 if (bootverbose) 595 ata_prtdev(atadev, 596 "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n", 597 ata_pmode(atacap), ata_wmode(atacap), 598 ata_umode(atacap), 599 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 600 } 601 else { 602 if (!error) 603 error = ENXIO; 604 if (atadev->param) { 605 free(atadev->param, M_ATA); 606 atadev->param = NULL; 607 } 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 ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0); 629 switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) { 630 #ifdef DEV_ATAPICD 631 case ATA_ATAPI_TYPE_CDROM: 632 ch->device[SLAVE].attach = acd_attach; 633 break; 634 #endif 635 #ifdef DEV_ATAPIFD 636 case ATA_ATAPI_TYPE_DIRECT: 637 ch->device[SLAVE].attach = afd_attach; 638 break; 639 #endif 640 #ifdef DEV_ATAPIST 641 case ATA_ATAPI_TYPE_TAPE: 642 ch->device[SLAVE].attach = ast_attach; 643 break; 644 #endif 645 } 646 } 647 } 648 if (ch->devices & ATA_ATA_MASTER) { 649 if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY)) 650 ch->devices &= ~ATA_ATA_MASTER; 651 #ifdef DEV_ATADISK 652 else 653 ch->device[MASTER].attach = ad_attach; 654 #endif 655 } 656 if (ch->devices & ATA_ATAPI_MASTER) { 657 if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY)) 658 ch->devices &= ~ATA_ATAPI_MASTER; 659 else { 660 ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0); 661 switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) { 662 #ifdef DEV_ATAPICD 663 case ATA_ATAPI_TYPE_CDROM: 664 ch->device[MASTER].attach = acd_attach; 665 break; 666 #endif 667 #ifdef DEV_ATAPIFD 668 case ATA_ATAPI_TYPE_DIRECT: 669 ch->device[MASTER].attach = afd_attach; 670 break; 671 #endif 672 #ifdef DEV_ATAPIST 673 case ATA_ATAPI_TYPE_TAPE: 674 ch->device[MASTER].attach = ast_attach; 675 break; 676 #endif 677 } 678 } 679 } 680 681 /* setup basic transfer mode by setting PIO mode and DMA if supported */ 682 if (ch->device[MASTER].param) { 683 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX); 684 if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma && 685 (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR)|| 686 ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma) 687 ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX); 688 689 } 690 if (ch->device[SLAVE].param) { 691 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX); 692 if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma && 693 (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR) || 694 ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma) 695 ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX); 696 } 697 } 698 699 static void 700 ata_boot_attach(void) 701 { 702 struct ata_channel *ch; 703 int ctlr; 704 705 /* 706 * run through all ata devices and look for real ATA & ATAPI devices 707 * using the hints we found in the early probe, this avoids some of 708 * the delays probing of non-exsistent devices can cause. 709 */ 710 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 711 if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 712 continue; 713 ata_identify_devices(ch); 714 if (ch->device[MASTER].attach) 715 ch->device[MASTER].attach(&ch->device[MASTER]); 716 if (ch->device[SLAVE].attach) 717 ch->device[SLAVE].attach(&ch->device[SLAVE]); 718 #ifdef DEV_ATAPICAM 719 atapi_cam_attach_bus(ch); 720 #endif 721 } 722 #ifdef DEV_ATARAID 723 ata_raid_attach(); 724 #endif 725 if (ata_delayed_attach) { 726 config_intrhook_disestablish(ata_delayed_attach); 727 free(ata_delayed_attach, M_TEMP); 728 ata_delayed_attach = NULL; 729 } 730 } 731 732 /* 733 * misc support functions 734 */ 735 static void 736 bswap(int8_t *buf, int len) 737 { 738 u_int16_t *ptr = (u_int16_t*)(buf + len); 739 740 while (--ptr >= (u_int16_t*)buf) 741 *ptr = ntohs(*ptr); 742 } 743 744 static void 745 btrim(int8_t *buf, int len) 746 { 747 int8_t *ptr; 748 749 for (ptr = buf; ptr < buf+len; ++ptr) 750 if (!*ptr) 751 *ptr = ' '; 752 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 753 *ptr = 0; 754 } 755 756 static void 757 bpack(int8_t *src, int8_t *dst, int len) 758 { 759 int i, j, blank; 760 761 for (i = j = blank = 0 ; i < len; i++) { 762 if (blank && src[i] == ' ') continue; 763 if (blank && src[i] != ' ') { 764 dst[j++] = src[i]; 765 blank = 0; 766 continue; 767 } 768 if (src[i] == ' ') { 769 blank = 1; 770 if (i == 0) 771 continue; 772 } 773 dst[j++] = src[i]; 774 } 775 if (j < len) 776 dst[j] = 0x00; 777 } 778 779 int 780 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 781 { 782 va_list ap; 783 int ret; 784 785 if (device == -1) 786 ret = printf("ata%d: ", device_get_unit(ch->dev)); 787 else { 788 if (ch->device[ATA_DEV(device)].name) 789 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 790 else 791 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 792 (device == ATA_MASTER) ? "master" : "slave"); 793 } 794 va_start(ap, fmt); 795 ret += vprintf(fmt, ap); 796 va_end(ap); 797 return ret; 798 } 799 800 int 801 ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 802 { 803 va_list ap; 804 int ret; 805 806 if (atadev->name) 807 ret = printf("%s: ", atadev->name); 808 else 809 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 810 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 811 va_start(ap, fmt); 812 ret += vprintf(fmt, ap); 813 va_end(ap); 814 return ret; 815 } 816 817 void 818 ata_set_name(struct ata_device *atadev, char *name, int lun) 819 { 820 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 821 if (atadev->name) 822 sprintf(atadev->name, "%s%d", name, lun); 823 } 824 825 void 826 ata_free_name(struct ata_device *atadev) 827 { 828 if (atadev->name) 829 free(atadev->name, M_ATA); 830 atadev->name = NULL; 831 } 832 833 int 834 ata_get_lun(u_int32_t *map) 835 { 836 int lun = ffs(~*map) - 1; 837 838 *map |= (1 << lun); 839 return lun; 840 } 841 842 int 843 ata_test_lun(u_int32_t *map, int lun) 844 { 845 return (*map & (1 << lun)); 846 } 847 848 void 849 ata_free_lun(u_int32_t *map, int lun) 850 { 851 *map &= ~(1 << lun); 852 } 853 854 char * 855 ata_mode2str(int mode) 856 { 857 switch (mode) { 858 case ATA_PIO: return "BIOSPIO"; 859 case ATA_PIO0: return "PIO0"; 860 case ATA_PIO1: return "PIO1"; 861 case ATA_PIO2: return "PIO2"; 862 case ATA_PIO3: return "PIO3"; 863 case ATA_PIO4: return "PIO4"; 864 case ATA_DMA: return "BIOSDMA"; 865 case ATA_WDMA0: return "WDMA0"; 866 case ATA_WDMA1: return "WDMA1"; 867 case ATA_WDMA2: return "WDMA2"; 868 case ATA_UDMA0: return "UDMA16"; 869 case ATA_UDMA1: return "UDMA25"; 870 case ATA_UDMA2: return "UDMA33"; 871 case ATA_UDMA3: return "UDMA40"; 872 case ATA_UDMA4: return "UDMA66"; 873 case ATA_UDMA5: return "UDMA100"; 874 case ATA_UDMA6: return "UDMA133"; 875 case ATA_SA150: return "SATA150"; 876 default: return "???"; 877 } 878 } 879 880 int 881 ata_pmode(struct ata_params *ap) 882 { 883 if (ap->atavalid & ATA_FLAG_64_70) { 884 if (ap->apiomodes & 0x02) 885 return ATA_PIO4; 886 if (ap->apiomodes & 0x01) 887 return ATA_PIO3; 888 } 889 if (ap->mwdmamodes & 0x04) 890 return ATA_PIO4; 891 if (ap->mwdmamodes & 0x02) 892 return ATA_PIO3; 893 if (ap->mwdmamodes & 0x01) 894 return ATA_PIO2; 895 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 896 return ATA_PIO2; 897 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 898 return ATA_PIO1; 899 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 900 return ATA_PIO0; 901 return ATA_PIO0; 902 } 903 904 int 905 ata_wmode(struct ata_params *ap) 906 { 907 if (ap->mwdmamodes & 0x04) 908 return ATA_WDMA2; 909 if (ap->mwdmamodes & 0x02) 910 return ATA_WDMA1; 911 if (ap->mwdmamodes & 0x01) 912 return ATA_WDMA0; 913 return -1; 914 } 915 916 int 917 ata_umode(struct ata_params *ap) 918 { 919 if (ap->atavalid & ATA_FLAG_88) { 920 if (ap->udmamodes & 0x40) 921 return ATA_UDMA6; 922 if (ap->udmamodes & 0x20) 923 return ATA_UDMA5; 924 if (ap->udmamodes & 0x10) 925 return ATA_UDMA4; 926 if (ap->udmamodes & 0x08) 927 return ATA_UDMA3; 928 if (ap->udmamodes & 0x04) 929 return ATA_UDMA2; 930 if (ap->udmamodes & 0x02) 931 return ATA_UDMA1; 932 if (ap->udmamodes & 0x01) 933 return ATA_UDMA0; 934 } 935 return -1; 936 } 937 938 int 939 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 940 { 941 if (maxmode && mode > maxmode) 942 mode = maxmode; 943 944 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 945 return min(mode, ata_umode(atadev->param)); 946 947 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 948 return min(mode, ata_wmode(atadev->param)); 949 950 if (mode > ata_pmode(atadev->param)) 951 return min(mode, ata_pmode(atadev->param)); 952 953 return mode; 954 } 955 956 static void 957 ata_init(void) 958 { 959 /* register controlling device */ 960 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 961 962 /* register boot attach to be run when interrupts are enabled */ 963 if (!(ata_delayed_attach = (struct intr_config_hook *) 964 malloc(sizeof(struct intr_config_hook), 965 M_TEMP, M_NOWAIT | M_ZERO))) { 966 printf("ata: malloc of delayed attach hook failed\n"); 967 return; 968 } 969 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 970 if (config_intrhook_establish(ata_delayed_attach) != 0) { 971 printf("ata: config_intrhook_establish failed\n"); 972 free(ata_delayed_attach, M_TEMP); 973 } 974 975 /* register handler to flush write caches on shutdown */ 976 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown, 977 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 978 printf("ata: shutdown event registration failed!\n"); 979 980 /* init our UMA zone for ATA requests */ 981 ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request), 982 NULL, NULL, NULL, NULL, 0, 0); 983 } 984 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 985