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