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