1 /*- 2 * Copyright (c) 1998 - 2008 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ata.h" 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/ata.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/endian.h> 37 #include <sys/ctype.h> 38 #include <sys/conf.h> 39 #include <sys/bus.h> 40 #include <sys/bio.h> 41 #include <sys/malloc.h> 42 #include <sys/sysctl.h> 43 #include <sys/sema.h> 44 #include <sys/taskqueue.h> 45 #include <vm/uma.h> 46 #include <machine/stdarg.h> 47 #include <machine/resource.h> 48 #include <machine/bus.h> 49 #include <sys/rman.h> 50 #include <dev/ata/ata-all.h> 51 #include <dev/pci/pcivar.h> 52 #include <ata_if.h> 53 54 #ifdef ATA_CAM 55 #include <cam/cam.h> 56 #include <cam/cam_ccb.h> 57 #include <cam/cam_sim.h> 58 #include <cam/cam_xpt_sim.h> 59 #include <cam/cam_debug.h> 60 #endif 61 62 #ifndef ATA_CAM 63 /* device structure */ 64 static d_ioctl_t ata_ioctl; 65 static struct cdevsw ata_cdevsw = { 66 .d_version = D_VERSION, 67 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */ 68 .d_ioctl = ata_ioctl, 69 .d_name = "ata", 70 }; 71 #endif 72 73 /* prototypes */ 74 #ifndef ATA_CAM 75 static void ata_boot_attach(void); 76 static device_t ata_add_child(device_t, struct ata_device *, int); 77 #else 78 static void ataaction(struct cam_sim *sim, union ccb *ccb); 79 static void atapoll(struct cam_sim *sim); 80 #endif 81 static void ata_conn_event(void *, int); 82 #ifndef ATA_CAM 83 static void bswap(int8_t *, int); 84 static void btrim(int8_t *, int); 85 static void bpack(int8_t *, int8_t *, int); 86 #endif 87 static void ata_interrupt_locked(void *data); 88 #ifdef ATA_CAM 89 static void ata_periodic_poll(void *data); 90 #endif 91 92 /* global vars */ 93 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer"); 94 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL; 95 #ifndef ATA_CAM 96 struct intr_config_hook *ata_delayed_attach = NULL; 97 #endif 98 devclass_t ata_devclass; 99 uma_zone_t ata_request_zone; 100 uma_zone_t ata_composite_zone; 101 #ifndef ATA_CAM 102 int ata_wc = 1; 103 int ata_setmax = 0; 104 #endif 105 int ata_dma_check_80pin = 1; 106 107 /* local vars */ 108 #ifndef ATA_CAM 109 static int ata_dma = 1; 110 static int atapi_dma = 1; 111 #endif 112 113 /* sysctl vars */ 114 static SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 115 #ifndef ATA_CAM 116 TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 117 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0, 118 "ATA disk DMA mode control"); 119 #endif 120 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin); 121 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin, 122 CTLFLAG_RW, &ata_dma_check_80pin, 1, 123 "Check for 80pin cable before setting ATA DMA mode"); 124 #ifndef ATA_CAM 125 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 126 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0, 127 "ATAPI device DMA mode control"); 128 TUNABLE_INT("hw.ata.wc", &ata_wc); 129 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0, 130 "ATA disk write caching"); 131 TUNABLE_INT("hw.ata.setmax", &ata_setmax); 132 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0, 133 "ATA disk set max native address"); 134 #endif 135 #ifdef ATA_CAM 136 FEATURE(ata_cam, "ATA devices are accessed through the cam(4) driver"); 137 #endif 138 139 /* 140 * newbus device interface related functions 141 */ 142 int 143 ata_probe(device_t dev) 144 { 145 return 0; 146 } 147 148 int 149 ata_attach(device_t dev) 150 { 151 struct ata_channel *ch = device_get_softc(dev); 152 int error, rid; 153 #ifdef ATA_CAM 154 struct cam_devq *devq; 155 const char *res; 156 char buf[64]; 157 int i, mode; 158 #endif 159 160 /* check that we have a virgin channel to attach */ 161 if (ch->r_irq) 162 return EEXIST; 163 164 /* initialize the softc basics */ 165 ch->dev = dev; 166 ch->state = ATA_IDLE; 167 bzero(&ch->state_mtx, sizeof(struct mtx)); 168 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF); 169 bzero(&ch->queue_mtx, sizeof(struct mtx)); 170 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF); 171 TAILQ_INIT(&ch->ata_queue); 172 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev); 173 #ifdef ATA_CAM 174 for (i = 0; i < 16; i++) { 175 ch->user[i].mode = 0; 176 snprintf(buf, sizeof(buf), "dev%d.mode", i); 177 if (resource_string_value(device_get_name(dev), 178 device_get_unit(dev), buf, &res) == 0) 179 mode = ata_str2mode(res); 180 else if (resource_string_value(device_get_name(dev), 181 device_get_unit(dev), "mode", &res) == 0) 182 mode = ata_str2mode(res); 183 else 184 mode = -1; 185 if (mode >= 0) 186 ch->user[i].mode = mode; 187 if (ch->flags & ATA_SATA) 188 ch->user[i].bytecount = 8192; 189 else 190 ch->user[i].bytecount = MAXPHYS; 191 ch->user[i].caps = 0; 192 ch->curr[i] = ch->user[i]; 193 if (ch->pm_level > 0) 194 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ; 195 if (ch->pm_level > 1) 196 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ; 197 } 198 callout_init(&ch->poll_callout, 1); 199 #endif 200 201 #ifndef ATA_CAM 202 /* reset the controller HW, the channel and device(s) */ 203 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit) 204 pause("ataatch", 1); 205 ATA_RESET(dev); 206 ATA_LOCKING(dev, ATA_LF_UNLOCK); 207 #endif 208 209 /* allocate DMA resources if DMA HW present*/ 210 if (ch->dma.alloc) 211 ch->dma.alloc(dev); 212 213 /* setup interrupt delivery */ 214 rid = ATA_IRQ_RID; 215 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 216 RF_SHAREABLE | RF_ACTIVE); 217 if (!ch->r_irq) { 218 device_printf(dev, "unable to allocate interrupt\n"); 219 return ENXIO; 220 } 221 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 222 ata_interrupt, ch, &ch->ih))) { 223 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq); 224 device_printf(dev, "unable to setup interrupt\n"); 225 return error; 226 } 227 228 #ifndef ATA_CAM 229 /* probe and attach devices on this channel unless we are in early boot */ 230 if (!ata_delayed_attach) 231 ata_identify(dev); 232 return (0); 233 #else 234 if (ch->flags & ATA_PERIODIC_POLL) 235 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch); 236 mtx_lock(&ch->state_mtx); 237 /* Create the device queue for our SIM. */ 238 devq = cam_simq_alloc(1); 239 if (devq == NULL) { 240 device_printf(dev, "Unable to allocate simq\n"); 241 error = ENOMEM; 242 goto err1; 243 } 244 /* Construct SIM entry */ 245 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch, 246 device_get_unit(dev), &ch->state_mtx, 1, 0, devq); 247 if (ch->sim == NULL) { 248 device_printf(dev, "unable to allocate sim\n"); 249 cam_simq_free(devq); 250 error = ENOMEM; 251 goto err1; 252 } 253 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 254 device_printf(dev, "unable to register xpt bus\n"); 255 error = ENXIO; 256 goto err2; 257 } 258 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 259 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 260 device_printf(dev, "unable to create path\n"); 261 error = ENXIO; 262 goto err3; 263 } 264 mtx_unlock(&ch->state_mtx); 265 return (0); 266 267 err3: 268 xpt_bus_deregister(cam_sim_path(ch->sim)); 269 err2: 270 cam_sim_free(ch->sim, /*free_devq*/TRUE); 271 ch->sim = NULL; 272 err1: 273 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq); 274 mtx_unlock(&ch->state_mtx); 275 if (ch->flags & ATA_PERIODIC_POLL) 276 callout_drain(&ch->poll_callout); 277 return (error); 278 #endif 279 } 280 281 int 282 ata_detach(device_t dev) 283 { 284 struct ata_channel *ch = device_get_softc(dev); 285 #ifndef ATA_CAM 286 device_t *children; 287 int nchildren, i; 288 #endif 289 290 /* check that we have a valid channel to detach */ 291 if (!ch->r_irq) 292 return ENXIO; 293 294 /* grap the channel lock so no new requests gets launched */ 295 mtx_lock(&ch->state_mtx); 296 ch->state |= ATA_STALL_QUEUE; 297 mtx_unlock(&ch->state_mtx); 298 #ifdef ATA_CAM 299 if (ch->flags & ATA_PERIODIC_POLL) 300 callout_drain(&ch->poll_callout); 301 #endif 302 303 #ifndef ATA_CAM 304 /* detach & delete all children */ 305 if (!device_get_children(dev, &children, &nchildren)) { 306 for (i = 0; i < nchildren; i++) 307 if (children[i]) 308 device_delete_child(dev, children[i]); 309 free(children, M_TEMP); 310 } 311 #endif 312 taskqueue_drain(taskqueue_thread, &ch->conntask); 313 314 #ifdef ATA_CAM 315 mtx_lock(&ch->state_mtx); 316 xpt_async(AC_LOST_DEVICE, ch->path, NULL); 317 xpt_free_path(ch->path); 318 xpt_bus_deregister(cam_sim_path(ch->sim)); 319 cam_sim_free(ch->sim, /*free_devq*/TRUE); 320 ch->sim = NULL; 321 mtx_unlock(&ch->state_mtx); 322 #endif 323 324 /* release resources */ 325 bus_teardown_intr(dev, ch->r_irq, ch->ih); 326 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 327 ch->r_irq = NULL; 328 329 /* free DMA resources if DMA HW present*/ 330 if (ch->dma.free) 331 ch->dma.free(dev); 332 333 mtx_destroy(&ch->state_mtx); 334 mtx_destroy(&ch->queue_mtx); 335 return 0; 336 } 337 338 static void 339 ata_conn_event(void *context, int dummy) 340 { 341 device_t dev = (device_t)context; 342 #ifdef ATA_CAM 343 struct ata_channel *ch = device_get_softc(dev); 344 union ccb *ccb; 345 346 mtx_lock(&ch->state_mtx); 347 if (ch->sim == NULL) { 348 mtx_unlock(&ch->state_mtx); 349 return; 350 } 351 ata_reinit(dev); 352 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 353 return; 354 if (xpt_create_path(&ccb->ccb_h.path, NULL, 355 cam_sim_path(ch->sim), 356 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 357 xpt_free_ccb(ccb); 358 return; 359 } 360 xpt_rescan(ccb); 361 mtx_unlock(&ch->state_mtx); 362 #else 363 ata_reinit(dev); 364 #endif 365 } 366 367 int 368 ata_reinit(device_t dev) 369 { 370 struct ata_channel *ch = device_get_softc(dev); 371 struct ata_request *request; 372 #ifndef ATA_CAM 373 device_t *children; 374 int nchildren, i; 375 376 /* check that we have a valid channel to reinit */ 377 if (!ch || !ch->r_irq) 378 return ENXIO; 379 380 if (bootverbose) 381 device_printf(dev, "reiniting channel ..\n"); 382 383 /* poll for locking the channel */ 384 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit) 385 pause("atarini", 1); 386 387 /* catch eventual request in ch->running */ 388 mtx_lock(&ch->state_mtx); 389 if (ch->state & ATA_STALL_QUEUE) { 390 /* Recursive reinits and reinits during detach prohobited. */ 391 mtx_unlock(&ch->state_mtx); 392 return (ENXIO); 393 } 394 if ((request = ch->running)) 395 callout_stop(&request->callout); 396 ch->running = NULL; 397 398 /* unconditionally grap the channel lock */ 399 ch->state |= ATA_STALL_QUEUE; 400 mtx_unlock(&ch->state_mtx); 401 402 /* reset the controller HW, the channel and device(s) */ 403 ATA_RESET(dev); 404 405 /* reinit the children and delete any that fails */ 406 if (!device_get_children(dev, &children, &nchildren)) { 407 mtx_lock(&Giant); /* newbus suckage it needs Giant */ 408 for (i = 0; i < nchildren; i++) { 409 /* did any children go missing ? */ 410 if (children[i] && device_is_attached(children[i]) && 411 ATA_REINIT(children[i])) { 412 /* 413 * if we had a running request and its device matches 414 * this child we need to inform the request that the 415 * device is gone. 416 */ 417 if (request && request->dev == children[i]) { 418 request->result = ENXIO; 419 device_printf(request->dev, "FAILURE - device detached\n"); 420 421 /* if not timeout finish request here */ 422 if (!(request->flags & ATA_R_TIMEOUT)) 423 ata_finish(request); 424 request = NULL; 425 } 426 device_delete_child(dev, children[i]); 427 } 428 } 429 free(children, M_TEMP); 430 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */ 431 } 432 433 /* if we still have a good request put it on the queue again */ 434 if (request && !(request->flags & ATA_R_TIMEOUT)) { 435 device_printf(request->dev, 436 "WARNING - %s requeued due to channel reset", 437 ata_cmd2str(request)); 438 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL))) 439 printf(" LBA=%ju", request->u.ata.lba); 440 printf("\n"); 441 request->flags |= ATA_R_REQUEUE; 442 ata_queue_request(request); 443 } 444 445 /* we're done release the channel for new work */ 446 mtx_lock(&ch->state_mtx); 447 ch->state = ATA_IDLE; 448 mtx_unlock(&ch->state_mtx); 449 ATA_LOCKING(dev, ATA_LF_UNLOCK); 450 451 /* Add new children. */ 452 /* ata_identify(dev); */ 453 454 if (bootverbose) 455 device_printf(dev, "reinit done ..\n"); 456 457 /* kick off requests on the queue */ 458 ata_start(dev); 459 #else 460 xpt_freeze_simq(ch->sim, 1); 461 if ((request = ch->running)) { 462 ch->running = NULL; 463 if (ch->state == ATA_ACTIVE) 464 ch->state = ATA_IDLE; 465 callout_stop(&request->callout); 466 if (ch->dma.unload) 467 ch->dma.unload(request); 468 request->result = ERESTART; 469 ata_cam_end_transaction(dev, request); 470 } 471 /* reset the controller HW, the channel and device(s) */ 472 ATA_RESET(dev); 473 /* Tell the XPT about the event */ 474 xpt_async(AC_BUS_RESET, ch->path, NULL); 475 xpt_release_simq(ch->sim, TRUE); 476 #endif 477 return(0); 478 } 479 480 int 481 ata_suspend(device_t dev) 482 { 483 struct ata_channel *ch; 484 485 /* check for valid device */ 486 if (!dev || !(ch = device_get_softc(dev))) 487 return ENXIO; 488 489 #ifdef ATA_CAM 490 if (ch->flags & ATA_PERIODIC_POLL) 491 callout_drain(&ch->poll_callout); 492 mtx_lock(&ch->state_mtx); 493 xpt_freeze_simq(ch->sim, 1); 494 while (ch->state != ATA_IDLE) 495 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100); 496 mtx_unlock(&ch->state_mtx); 497 #else 498 /* wait for the channel to be IDLE or detached before suspending */ 499 while (ch->r_irq) { 500 mtx_lock(&ch->state_mtx); 501 if (ch->state == ATA_IDLE) { 502 ch->state = ATA_ACTIVE; 503 mtx_unlock(&ch->state_mtx); 504 break; 505 } 506 mtx_unlock(&ch->state_mtx); 507 tsleep(ch, PRIBIO, "atasusp", hz/10); 508 } 509 ATA_LOCKING(dev, ATA_LF_UNLOCK); 510 #endif 511 return(0); 512 } 513 514 int 515 ata_resume(device_t dev) 516 { 517 struct ata_channel *ch; 518 int error; 519 520 /* check for valid device */ 521 if (!dev || !(ch = device_get_softc(dev))) 522 return ENXIO; 523 524 #ifdef ATA_CAM 525 mtx_lock(&ch->state_mtx); 526 error = ata_reinit(dev); 527 xpt_release_simq(ch->sim, TRUE); 528 mtx_unlock(&ch->state_mtx); 529 if (ch->flags & ATA_PERIODIC_POLL) 530 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch); 531 #else 532 /* reinit the devices, we dont know what mode/state they are in */ 533 error = ata_reinit(dev); 534 /* kick off requests on the queue */ 535 ata_start(dev); 536 #endif 537 return error; 538 } 539 540 void 541 ata_interrupt(void *data) 542 { 543 #ifdef ATA_CAM 544 struct ata_channel *ch = (struct ata_channel *)data; 545 546 mtx_lock(&ch->state_mtx); 547 #endif 548 ata_interrupt_locked(data); 549 #ifdef ATA_CAM 550 mtx_unlock(&ch->state_mtx); 551 #endif 552 } 553 554 static void 555 ata_interrupt_locked(void *data) 556 { 557 struct ata_channel *ch = (struct ata_channel *)data; 558 struct ata_request *request; 559 560 #ifndef ATA_CAM 561 mtx_lock(&ch->state_mtx); 562 #endif 563 do { 564 /* ignore interrupt if its not for us */ 565 if (ch->hw.status && !ch->hw.status(ch->dev)) 566 break; 567 568 /* do we have a running request */ 569 if (!(request = ch->running)) 570 break; 571 572 ATA_DEBUG_RQ(request, "interrupt"); 573 574 /* safetycheck for the right state */ 575 if (ch->state == ATA_IDLE) { 576 device_printf(request->dev, "interrupt on idle channel ignored\n"); 577 break; 578 } 579 580 /* 581 * we have the HW locks, so end the transaction for this request 582 * if it finishes immediately otherwise wait for next interrupt 583 */ 584 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) { 585 ch->running = NULL; 586 if (ch->state == ATA_ACTIVE) 587 ch->state = ATA_IDLE; 588 #ifdef ATA_CAM 589 ata_cam_end_transaction(ch->dev, request); 590 #else 591 mtx_unlock(&ch->state_mtx); 592 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK); 593 ata_finish(request); 594 #endif 595 return; 596 } 597 } while (0); 598 #ifndef ATA_CAM 599 mtx_unlock(&ch->state_mtx); 600 #endif 601 } 602 603 #ifdef ATA_CAM 604 static void 605 ata_periodic_poll(void *data) 606 { 607 struct ata_channel *ch = (struct ata_channel *)data; 608 609 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch); 610 ata_interrupt(ch); 611 } 612 #endif 613 614 void 615 ata_print_cable(device_t dev, u_int8_t *who) 616 { 617 device_printf(dev, 618 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who); 619 } 620 621 #ifndef ATA_CAM 622 int 623 ata_check_80pin(device_t dev, int mode) 624 { 625 struct ata_device *atadev = device_get_softc(dev); 626 627 if (!ata_dma_check_80pin) { 628 if (bootverbose) 629 device_printf(dev, "Skipping 80pin cable check\n"); 630 return mode; 631 } 632 633 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) { 634 ata_print_cable(dev, "device"); 635 mode = ATA_UDMA2; 636 } 637 return mode; 638 } 639 #endif 640 641 #ifndef ATA_CAM 642 void 643 ata_setmode(device_t dev) 644 { 645 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 646 struct ata_device *atadev = device_get_softc(dev); 647 int error, mode, pmode; 648 649 mode = atadev->mode; 650 do { 651 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX); 652 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode); 653 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0) 654 mode = ata_check_80pin(dev, mode); 655 } while (pmode != mode); /* Interate till successfull negotiation. */ 656 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 657 if (bootverbose) 658 device_printf(dev, "%ssetting %s\n", 659 (error) ? "FAILURE " : "", ata_mode2str(mode)); 660 atadev->mode = mode; 661 } 662 #endif 663 664 /* 665 * device related interfaces 666 */ 667 #ifndef ATA_CAM 668 static int 669 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 670 int32_t flag, struct thread *td) 671 { 672 device_t device, *children; 673 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data; 674 int *value = (int *)data; 675 int i, nchildren, error = ENOTTY; 676 677 switch (cmd) { 678 case IOCATAGMAXCHANNEL: 679 /* In case we have channel 0..n this will return n+1. */ 680 *value = devclass_get_maxunit(ata_devclass); 681 error = 0; 682 break; 683 684 case IOCATAREINIT: 685 if (*value >= devclass_get_maxunit(ata_devclass) || 686 !(device = devclass_get_device(ata_devclass, *value)) || 687 !device_is_attached(device)) 688 return ENXIO; 689 error = ata_reinit(device); 690 break; 691 692 case IOCATAATTACH: 693 if (*value >= devclass_get_maxunit(ata_devclass) || 694 !(device = devclass_get_device(ata_devclass, *value)) || 695 !device_is_attached(device)) 696 return ENXIO; 697 error = DEVICE_ATTACH(device); 698 break; 699 700 case IOCATADETACH: 701 if (*value >= devclass_get_maxunit(ata_devclass) || 702 !(device = devclass_get_device(ata_devclass, *value)) || 703 !device_is_attached(device)) 704 return ENXIO; 705 error = DEVICE_DETACH(device); 706 break; 707 708 case IOCATADEVICES: 709 if (devices->channel >= devclass_get_maxunit(ata_devclass) || 710 !(device = devclass_get_device(ata_devclass, devices->channel)) || 711 !device_is_attached(device)) 712 return ENXIO; 713 bzero(devices->name[0], 32); 714 bzero(&devices->params[0], sizeof(struct ata_params)); 715 bzero(devices->name[1], 32); 716 bzero(&devices->params[1], sizeof(struct ata_params)); 717 if (!device_get_children(device, &children, &nchildren)) { 718 for (i = 0; i < nchildren; i++) { 719 if (children[i] && device_is_attached(children[i])) { 720 struct ata_device *atadev = device_get_softc(children[i]); 721 722 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */ 723 strncpy(devices->name[0], 724 device_get_nameunit(children[i]), 32); 725 bcopy(&atadev->param, &devices->params[0], 726 sizeof(struct ata_params)); 727 } 728 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */ 729 strncpy(devices->name[1], 730 device_get_nameunit(children[i]), 32); 731 bcopy(&atadev->param, &devices->params[1], 732 sizeof(struct ata_params)); 733 } 734 } 735 } 736 free(children, M_TEMP); 737 error = 0; 738 } 739 else 740 error = ENODEV; 741 break; 742 743 default: 744 if (ata_raid_ioctl_func) 745 error = ata_raid_ioctl_func(cmd, data); 746 } 747 return error; 748 } 749 #endif 750 751 #ifndef ATA_CAM 752 int 753 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data) 754 { 755 struct ata_device *atadev = device_get_softc(dev); 756 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 757 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data; 758 struct ata_params *params = (struct ata_params *)data; 759 int *mode = (int *)data; 760 struct ata_request *request; 761 caddr_t buf; 762 int error; 763 764 switch (cmd) { 765 case IOCATAREQUEST: 766 if (ioc_request->count > 767 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) { 768 return (EFBIG); 769 } 770 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) { 771 return ENOMEM; 772 } 773 if (!(request = ata_alloc_request())) { 774 free(buf, M_ATA); 775 return ENOMEM; 776 } 777 request->dev = atadev->dev; 778 if (ioc_request->flags & ATA_CMD_WRITE) { 779 error = copyin(ioc_request->data, buf, ioc_request->count); 780 if (error) { 781 free(buf, M_ATA); 782 ata_free_request(request); 783 return error; 784 } 785 } 786 if (ioc_request->flags & ATA_CMD_ATAPI) { 787 request->flags = ATA_R_ATAPI; 788 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16); 789 } 790 else { 791 request->u.ata.command = ioc_request->u.ata.command; 792 request->u.ata.feature = ioc_request->u.ata.feature; 793 request->u.ata.lba = ioc_request->u.ata.lba; 794 request->u.ata.count = ioc_request->u.ata.count; 795 } 796 request->timeout = ioc_request->timeout; 797 request->data = buf; 798 request->bytecount = ioc_request->count; 799 request->transfersize = request->bytecount; 800 if (ioc_request->flags & ATA_CMD_CONTROL) 801 request->flags |= ATA_R_CONTROL; 802 if (ioc_request->flags & ATA_CMD_READ) 803 request->flags |= ATA_R_READ; 804 if (ioc_request->flags & ATA_CMD_WRITE) 805 request->flags |= ATA_R_WRITE; 806 ata_queue_request(request); 807 if (request->flags & ATA_R_ATAPI) { 808 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense, 809 sizeof(struct atapi_sense)); 810 } 811 else { 812 ioc_request->u.ata.command = request->u.ata.command; 813 ioc_request->u.ata.feature = request->u.ata.feature; 814 ioc_request->u.ata.lba = request->u.ata.lba; 815 ioc_request->u.ata.count = request->u.ata.count; 816 } 817 ioc_request->error = request->result; 818 if (ioc_request->flags & ATA_CMD_READ) 819 error = copyout(buf, ioc_request->data, ioc_request->count); 820 else 821 error = 0; 822 free(buf, M_ATA); 823 ata_free_request(request); 824 return error; 825 826 case IOCATAGPARM: 827 ata_getparam(atadev, 0); 828 bcopy(&atadev->param, params, sizeof(struct ata_params)); 829 return 0; 830 831 case IOCATASMODE: 832 atadev->mode = *mode; 833 ata_setmode(dev); 834 return 0; 835 836 case IOCATAGMODE: 837 *mode = atadev->mode | 838 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8); 839 return 0; 840 case IOCATASSPINDOWN: 841 atadev->spindown = *mode; 842 return 0; 843 case IOCATAGSPINDOWN: 844 *mode = atadev->spindown; 845 return 0; 846 default: 847 return ENOTTY; 848 } 849 } 850 #endif 851 852 #ifndef ATA_CAM 853 static void 854 ata_boot_attach(void) 855 { 856 struct ata_channel *ch; 857 int ctlr; 858 859 mtx_lock(&Giant); /* newbus suckage it needs Giant */ 860 861 /* kick off probe and attach on all channels */ 862 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) { 863 if ((ch = devclass_get_softc(ata_devclass, ctlr))) { 864 ata_identify(ch->dev); 865 } 866 } 867 868 /* release the hook that got us here, we are only needed once during boot */ 869 if (ata_delayed_attach) { 870 config_intrhook_disestablish(ata_delayed_attach); 871 free(ata_delayed_attach, M_TEMP); 872 ata_delayed_attach = NULL; 873 } 874 875 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */ 876 } 877 #endif 878 879 /* 880 * misc support functions 881 */ 882 #ifndef ATA_CAM 883 static device_t 884 ata_add_child(device_t parent, struct ata_device *atadev, int unit) 885 { 886 device_t child; 887 888 if ((child = device_add_child(parent, NULL, unit))) { 889 device_set_softc(child, atadev); 890 device_quiet(child); 891 atadev->dev = child; 892 atadev->max_iosize = DEV_BSIZE; 893 atadev->mode = ATA_PIO_MAX; 894 } 895 return child; 896 } 897 #endif 898 899 #ifndef ATA_CAM 900 int 901 ata_getparam(struct ata_device *atadev, int init) 902 { 903 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev)); 904 struct ata_request *request; 905 const char *res; 906 char buf[64]; 907 u_int8_t command = 0; 908 int error = ENOMEM, retries = 2, mode = -1; 909 910 if (ch->devices & (ATA_ATA_MASTER << atadev->unit)) 911 command = ATA_ATA_IDENTIFY; 912 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit)) 913 command = ATA_ATAPI_IDENTIFY; 914 if (!command) 915 return ENXIO; 916 917 while (retries-- > 0 && error) { 918 if (!(request = ata_alloc_request())) 919 break; 920 request->dev = atadev->dev; 921 request->timeout = 1; 922 request->retries = 0; 923 request->u.ata.command = command; 924 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT); 925 if (!bootverbose) 926 request->flags |= ATA_R_QUIET; 927 request->data = (void *)&atadev->param; 928 request->bytecount = sizeof(struct ata_params); 929 request->donecount = 0; 930 request->transfersize = DEV_BSIZE; 931 ata_queue_request(request); 932 error = request->result; 933 ata_free_request(request); 934 } 935 936 if (!error && (isprint(atadev->param.model[0]) || 937 isprint(atadev->param.model[1]))) { 938 struct ata_params *atacap = &atadev->param; 939 int16_t *ptr; 940 941 for (ptr = (int16_t *)atacap; 942 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) { 943 *ptr = le16toh(*ptr); 944 } 945 if (!(!strncmp(atacap->model, "FX", 2) || 946 !strncmp(atacap->model, "NEC", 3) || 947 !strncmp(atacap->model, "Pioneer", 7) || 948 !strncmp(atacap->model, "SHARP", 5))) { 949 bswap(atacap->model, sizeof(atacap->model)); 950 bswap(atacap->revision, sizeof(atacap->revision)); 951 bswap(atacap->serial, sizeof(atacap->serial)); 952 } 953 btrim(atacap->model, sizeof(atacap->model)); 954 bpack(atacap->model, atacap->model, sizeof(atacap->model)); 955 btrim(atacap->revision, sizeof(atacap->revision)); 956 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision)); 957 btrim(atacap->serial, sizeof(atacap->serial)); 958 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial)); 959 960 if (bootverbose) 961 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n", 962 device_get_unit(ch->dev), 963 ata_unit2str(atadev), 964 ata_mode2str(ata_pmode(atacap)), 965 ata_mode2str(ata_wmode(atacap)), 966 ata_mode2str(ata_umode(atacap)), 967 (atacap->hwres & ATA_CABLE_ID) ? "80":"40"); 968 969 if (init) { 970 char buffer[64]; 971 972 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision); 973 device_set_desc_copy(atadev->dev, buffer); 974 if ((atadev->param.config & ATA_PROTO_ATAPI) && 975 (atadev->param.config != ATA_CFA_MAGIC1) && 976 (atadev->param.config != ATA_CFA_MAGIC2)) { 977 if (atapi_dma && 978 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR && 979 ata_umode(&atadev->param) >= ATA_UDMA2) 980 atadev->mode = ATA_DMA_MAX; 981 } 982 else { 983 if (ata_dma && 984 (ata_umode(&atadev->param) > 0 || 985 ata_wmode(&atadev->param) > 0)) 986 atadev->mode = ATA_DMA_MAX; 987 } 988 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit); 989 if (resource_string_value(device_get_name(ch->dev), 990 device_get_unit(ch->dev), buf, &res) == 0) 991 mode = ata_str2mode(res); 992 else if (resource_string_value(device_get_name(ch->dev), 993 device_get_unit(ch->dev), "mode", &res) == 0) 994 mode = ata_str2mode(res); 995 if (mode >= 0) 996 atadev->mode = mode; 997 } 998 } 999 else { 1000 if (!error) 1001 error = ENXIO; 1002 } 1003 return error; 1004 } 1005 #endif 1006 1007 #ifndef ATA_CAM 1008 int 1009 ata_identify(device_t dev) 1010 { 1011 struct ata_channel *ch = device_get_softc(dev); 1012 struct ata_device *atadev; 1013 device_t *children; 1014 device_t child, master = NULL; 1015 int nchildren, i, n = ch->devices; 1016 1017 if (bootverbose) 1018 device_printf(dev, "Identifying devices: %08x\n", ch->devices); 1019 1020 mtx_lock(&Giant); 1021 /* Skip existing devices. */ 1022 if (!device_get_children(dev, &children, &nchildren)) { 1023 for (i = 0; i < nchildren; i++) { 1024 if (children[i] && (atadev = device_get_softc(children[i]))) 1025 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit); 1026 } 1027 free(children, M_TEMP); 1028 } 1029 /* Create new devices. */ 1030 if (bootverbose) 1031 device_printf(dev, "New devices: %08x\n", n); 1032 if (n == 0) { 1033 mtx_unlock(&Giant); 1034 return (0); 1035 } 1036 for (i = 0; i < ATA_PM; ++i) { 1037 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) { 1038 int unit = -1; 1039 1040 if (!(atadev = malloc(sizeof(struct ata_device), 1041 M_ATA, M_NOWAIT | M_ZERO))) { 1042 device_printf(dev, "out of memory\n"); 1043 return ENOMEM; 1044 } 1045 atadev->unit = i; 1046 #ifdef ATA_STATIC_ID 1047 if (n & (ATA_ATA_MASTER << i)) 1048 unit = (device_get_unit(dev) << 1) + i; 1049 #endif 1050 if ((child = ata_add_child(dev, atadev, unit))) { 1051 /* 1052 * PATA slave should be identified first, to allow 1053 * device cable detection on master to work properly. 1054 */ 1055 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 && 1056 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) { 1057 master = child; 1058 continue; 1059 } 1060 if (ata_getparam(atadev, 1)) { 1061 device_delete_child(dev, child); 1062 free(atadev, M_ATA); 1063 } 1064 } 1065 else 1066 free(atadev, M_ATA); 1067 } 1068 } 1069 if (master) { 1070 atadev = device_get_softc(master); 1071 if (ata_getparam(atadev, 1)) { 1072 device_delete_child(dev, master); 1073 free(atadev, M_ATA); 1074 } 1075 } 1076 bus_generic_probe(dev); 1077 bus_generic_attach(dev); 1078 mtx_unlock(&Giant); 1079 return 0; 1080 } 1081 #endif 1082 1083 void 1084 ata_default_registers(device_t dev) 1085 { 1086 struct ata_channel *ch = device_get_softc(dev); 1087 1088 /* fill in the defaults from whats setup already */ 1089 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res; 1090 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset; 1091 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res; 1092 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset; 1093 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res; 1094 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset; 1095 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res; 1096 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset; 1097 } 1098 1099 void 1100 ata_modify_if_48bit(struct ata_request *request) 1101 { 1102 struct ata_channel *ch = device_get_softc(request->parent); 1103 struct ata_device *atadev = device_get_softc(request->dev); 1104 1105 request->flags &= ~ATA_R_48BIT; 1106 1107 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA || 1108 request->u.ata.count > 256) && 1109 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 1110 1111 /* translate command into 48bit version */ 1112 switch (request->u.ata.command) { 1113 case ATA_READ: 1114 request->u.ata.command = ATA_READ48; 1115 break; 1116 case ATA_READ_MUL: 1117 request->u.ata.command = ATA_READ_MUL48; 1118 break; 1119 case ATA_READ_DMA: 1120 if (ch->flags & ATA_NO_48BIT_DMA) { 1121 if (request->transfersize > DEV_BSIZE) 1122 request->u.ata.command = ATA_READ_MUL48; 1123 else 1124 request->u.ata.command = ATA_READ48; 1125 request->flags &= ~ATA_R_DMA; 1126 } 1127 else 1128 request->u.ata.command = ATA_READ_DMA48; 1129 break; 1130 case ATA_READ_DMA_QUEUED: 1131 if (ch->flags & ATA_NO_48BIT_DMA) { 1132 if (request->transfersize > DEV_BSIZE) 1133 request->u.ata.command = ATA_READ_MUL48; 1134 else 1135 request->u.ata.command = ATA_READ48; 1136 request->flags &= ~ATA_R_DMA; 1137 } 1138 else 1139 request->u.ata.command = ATA_READ_DMA_QUEUED48; 1140 break; 1141 case ATA_WRITE: 1142 request->u.ata.command = ATA_WRITE48; 1143 break; 1144 case ATA_WRITE_MUL: 1145 request->u.ata.command = ATA_WRITE_MUL48; 1146 break; 1147 case ATA_WRITE_DMA: 1148 if (ch->flags & ATA_NO_48BIT_DMA) { 1149 if (request->transfersize > DEV_BSIZE) 1150 request->u.ata.command = ATA_WRITE_MUL48; 1151 else 1152 request->u.ata.command = ATA_WRITE48; 1153 request->flags &= ~ATA_R_DMA; 1154 } 1155 else 1156 request->u.ata.command = ATA_WRITE_DMA48; 1157 break; 1158 case ATA_WRITE_DMA_QUEUED: 1159 if (ch->flags & ATA_NO_48BIT_DMA) { 1160 if (request->transfersize > DEV_BSIZE) 1161 request->u.ata.command = ATA_WRITE_MUL48; 1162 else 1163 request->u.ata.command = ATA_WRITE48; 1164 request->u.ata.command = ATA_WRITE48; 1165 request->flags &= ~ATA_R_DMA; 1166 } 1167 else 1168 request->u.ata.command = ATA_WRITE_DMA_QUEUED48; 1169 break; 1170 case ATA_FLUSHCACHE: 1171 request->u.ata.command = ATA_FLUSHCACHE48; 1172 break; 1173 case ATA_SET_MAX_ADDRESS: 1174 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 1175 break; 1176 default: 1177 return; 1178 } 1179 request->flags |= ATA_R_48BIT; 1180 } 1181 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) { 1182 1183 /* translate command into 48bit version */ 1184 switch (request->u.ata.command) { 1185 case ATA_FLUSHCACHE: 1186 request->u.ata.command = ATA_FLUSHCACHE48; 1187 break; 1188 case ATA_READ_NATIVE_MAX_ADDRESS: 1189 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48; 1190 break; 1191 case ATA_SET_MAX_ADDRESS: 1192 request->u.ata.command = ATA_SET_MAX_ADDRESS48; 1193 break; 1194 default: 1195 return; 1196 } 1197 request->flags |= ATA_R_48BIT; 1198 } 1199 } 1200 1201 void 1202 ata_udelay(int interval) 1203 { 1204 /* for now just use DELAY, the timer/sleep subsytems are not there yet */ 1205 if (1 || interval < (1000000/hz) || ata_delayed_attach) 1206 DELAY(interval); 1207 else 1208 pause("ataslp", interval/(1000000/hz)); 1209 } 1210 1211 #ifndef ATA_CAM 1212 const char * 1213 ata_unit2str(struct ata_device *atadev) 1214 { 1215 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev)); 1216 static char str[8]; 1217 1218 if (ch->devices & ATA_PORTMULTIPLIER) 1219 sprintf(str, "port%d", atadev->unit); 1220 else 1221 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave"); 1222 return str; 1223 } 1224 #endif 1225 1226 const char * 1227 ata_mode2str(int mode) 1228 { 1229 switch (mode) { 1230 case -1: return "UNSUPPORTED"; 1231 case ATA_PIO0: return "PIO0"; 1232 case ATA_PIO1: return "PIO1"; 1233 case ATA_PIO2: return "PIO2"; 1234 case ATA_PIO3: return "PIO3"; 1235 case ATA_PIO4: return "PIO4"; 1236 case ATA_WDMA0: return "WDMA0"; 1237 case ATA_WDMA1: return "WDMA1"; 1238 case ATA_WDMA2: return "WDMA2"; 1239 case ATA_UDMA0: return "UDMA16"; 1240 case ATA_UDMA1: return "UDMA25"; 1241 case ATA_UDMA2: return "UDMA33"; 1242 case ATA_UDMA3: return "UDMA40"; 1243 case ATA_UDMA4: return "UDMA66"; 1244 case ATA_UDMA5: return "UDMA100"; 1245 case ATA_UDMA6: return "UDMA133"; 1246 case ATA_SA150: return "SATA150"; 1247 case ATA_SA300: return "SATA300"; 1248 default: 1249 if (mode & ATA_DMA_MASK) 1250 return "BIOSDMA"; 1251 else 1252 return "BIOSPIO"; 1253 } 1254 } 1255 1256 int 1257 ata_str2mode(const char *str) 1258 { 1259 1260 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0); 1261 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1); 1262 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2); 1263 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3); 1264 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4); 1265 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0); 1266 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1); 1267 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2); 1268 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0); 1269 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0); 1270 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1); 1271 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1); 1272 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2); 1273 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2); 1274 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3); 1275 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3); 1276 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4); 1277 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4); 1278 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5); 1279 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5); 1280 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6); 1281 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6); 1282 return (-1); 1283 } 1284 1285 #ifndef ATA_CAM 1286 const char * 1287 ata_satarev2str(int rev) 1288 { 1289 switch (rev) { 1290 case 0: return ""; 1291 case 1: return "SATA 1.5Gb/s"; 1292 case 2: return "SATA 3Gb/s"; 1293 case 3: return "SATA 6Gb/s"; 1294 case 0xff: return "SATA"; 1295 default: return "???"; 1296 } 1297 } 1298 #endif 1299 1300 int 1301 ata_atapi(device_t dev, int target) 1302 { 1303 struct ata_channel *ch = device_get_softc(dev); 1304 1305 return (ch->devices & (ATA_ATAPI_MASTER << target)); 1306 } 1307 1308 #ifndef ATA_CAM 1309 int 1310 ata_pmode(struct ata_params *ap) 1311 { 1312 if (ap->atavalid & ATA_FLAG_64_70) { 1313 if (ap->apiomodes & 0x02) 1314 return ATA_PIO4; 1315 if (ap->apiomodes & 0x01) 1316 return ATA_PIO3; 1317 } 1318 if (ap->mwdmamodes & 0x04) 1319 return ATA_PIO4; 1320 if (ap->mwdmamodes & 0x02) 1321 return ATA_PIO3; 1322 if (ap->mwdmamodes & 0x01) 1323 return ATA_PIO2; 1324 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 1325 return ATA_PIO2; 1326 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 1327 return ATA_PIO1; 1328 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 1329 return ATA_PIO0; 1330 return ATA_PIO0; 1331 } 1332 #endif 1333 1334 #ifndef ATA_CAM 1335 int 1336 ata_wmode(struct ata_params *ap) 1337 { 1338 if (ap->mwdmamodes & 0x04) 1339 return ATA_WDMA2; 1340 if (ap->mwdmamodes & 0x02) 1341 return ATA_WDMA1; 1342 if (ap->mwdmamodes & 0x01) 1343 return ATA_WDMA0; 1344 return -1; 1345 } 1346 #endif 1347 1348 #ifndef ATA_CAM 1349 int 1350 ata_umode(struct ata_params *ap) 1351 { 1352 if (ap->atavalid & ATA_FLAG_88) { 1353 if (ap->udmamodes & 0x40) 1354 return ATA_UDMA6; 1355 if (ap->udmamodes & 0x20) 1356 return ATA_UDMA5; 1357 if (ap->udmamodes & 0x10) 1358 return ATA_UDMA4; 1359 if (ap->udmamodes & 0x08) 1360 return ATA_UDMA3; 1361 if (ap->udmamodes & 0x04) 1362 return ATA_UDMA2; 1363 if (ap->udmamodes & 0x02) 1364 return ATA_UDMA1; 1365 if (ap->udmamodes & 0x01) 1366 return ATA_UDMA0; 1367 } 1368 return -1; 1369 } 1370 #endif 1371 1372 #ifndef ATA_CAM 1373 int 1374 ata_limit_mode(device_t dev, int mode, int maxmode) 1375 { 1376 struct ata_device *atadev = device_get_softc(dev); 1377 1378 if (maxmode && mode > maxmode) 1379 mode = maxmode; 1380 1381 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0) 1382 return min(mode, ata_umode(&atadev->param)); 1383 1384 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0) 1385 return min(mode, ata_wmode(&atadev->param)); 1386 1387 if (mode > ata_pmode(&atadev->param)) 1388 return min(mode, ata_pmode(&atadev->param)); 1389 1390 return mode; 1391 } 1392 #endif 1393 1394 #ifndef ATA_CAM 1395 static void 1396 bswap(int8_t *buf, int len) 1397 { 1398 u_int16_t *ptr = (u_int16_t*)(buf + len); 1399 1400 while (--ptr >= (u_int16_t*)buf) 1401 *ptr = ntohs(*ptr); 1402 } 1403 #endif 1404 1405 #ifndef ATA_CAM 1406 static void 1407 btrim(int8_t *buf, int len) 1408 { 1409 int8_t *ptr; 1410 1411 for (ptr = buf; ptr < buf+len; ++ptr) 1412 if (!*ptr || *ptr == '_') 1413 *ptr = ' '; 1414 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1415 *ptr = 0; 1416 } 1417 #endif 1418 1419 #ifndef ATA_CAM 1420 static void 1421 bpack(int8_t *src, int8_t *dst, int len) 1422 { 1423 int i, j, blank; 1424 1425 for (i = j = blank = 0 ; i < len; i++) { 1426 if (blank && src[i] == ' ') continue; 1427 if (blank && src[i] != ' ') { 1428 dst[j++] = src[i]; 1429 blank = 0; 1430 continue; 1431 } 1432 if (src[i] == ' ') { 1433 blank = 1; 1434 if (i == 0) 1435 continue; 1436 } 1437 dst[j++] = src[i]; 1438 } 1439 if (j < len) 1440 dst[j] = 0x00; 1441 } 1442 #endif 1443 1444 #ifdef ATA_CAM 1445 void 1446 ata_cam_begin_transaction(device_t dev, union ccb *ccb) 1447 { 1448 struct ata_channel *ch = device_get_softc(dev); 1449 struct ata_request *request; 1450 1451 if (!(request = ata_alloc_request())) { 1452 device_printf(dev, "FAILURE - out of memory in start\n"); 1453 ccb->ccb_h.status = CAM_REQ_INVALID; 1454 xpt_done(ccb); 1455 return; 1456 } 1457 bzero(request, sizeof(*request)); 1458 1459 /* setup request */ 1460 request->dev = NULL; 1461 request->parent = dev; 1462 request->unit = ccb->ccb_h.target_id; 1463 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1464 request->data = ccb->ataio.data_ptr; 1465 request->bytecount = ccb->ataio.dxfer_len; 1466 request->u.ata.command = ccb->ataio.cmd.command; 1467 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) | 1468 (uint16_t)ccb->ataio.cmd.features; 1469 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) | 1470 (uint16_t)ccb->ataio.cmd.sector_count; 1471 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) { 1472 request->flags |= ATA_R_48BIT; 1473 request->u.ata.lba = 1474 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) | 1475 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) | 1476 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24); 1477 } else { 1478 request->u.ata.lba = 1479 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24); 1480 } 1481 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) | 1482 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) | 1483 (uint64_t)ccb->ataio.cmd.lba_low; 1484 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1485 ccb->ataio.cmd.flags & CAM_ATAIO_DMA) 1486 request->flags |= ATA_R_DMA; 1487 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1488 request->flags |= ATA_R_READ; 1489 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1490 request->flags |= ATA_R_WRITE; 1491 } else { 1492 request->data = ccb->csio.data_ptr; 1493 request->bytecount = ccb->csio.dxfer_len; 1494 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1495 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1496 request->u.atapi.ccb, ccb->csio.cdb_len); 1497 request->flags |= ATA_R_ATAPI; 1498 if (ch->curr[ccb->ccb_h.target_id].atapi == 16) 1499 request->flags |= ATA_R_ATAPI16; 1500 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1501 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 1502 request->flags |= ATA_R_DMA; 1503 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1504 request->flags |= ATA_R_READ; 1505 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1506 request->flags |= ATA_R_WRITE; 1507 } 1508 request->transfersize = min(request->bytecount, 1509 ch->curr[ccb->ccb_h.target_id].bytecount); 1510 request->retries = 0; 1511 request->timeout = (ccb->ccb_h.timeout + 999) / 1000; 1512 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED); 1513 request->ccb = ccb; 1514 1515 ch->running = request; 1516 ch->state = ATA_ACTIVE; 1517 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) { 1518 ch->running = NULL; 1519 ch->state = ATA_IDLE; 1520 ata_cam_end_transaction(dev, request); 1521 return; 1522 } 1523 } 1524 1525 static void 1526 ata_cam_request_sense(device_t dev, struct ata_request *request) 1527 { 1528 struct ata_channel *ch = device_get_softc(dev); 1529 union ccb *ccb = request->ccb; 1530 1531 ch->requestsense = 1; 1532 1533 bzero(request, sizeof(&request)); 1534 request->dev = NULL; 1535 request->parent = dev; 1536 request->unit = ccb->ccb_h.target_id; 1537 request->data = (void *)&ccb->csio.sense_data; 1538 request->bytecount = ccb->csio.sense_len; 1539 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE; 1540 request->u.atapi.ccb[4] = ccb->csio.sense_len; 1541 request->flags |= ATA_R_ATAPI; 1542 if (ch->curr[ccb->ccb_h.target_id].atapi == 16) 1543 request->flags |= ATA_R_ATAPI16; 1544 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 1545 request->flags |= ATA_R_DMA; 1546 request->flags |= ATA_R_READ; 1547 request->transfersize = min(request->bytecount, 1548 ch->curr[ccb->ccb_h.target_id].bytecount); 1549 request->retries = 0; 1550 request->timeout = (ccb->ccb_h.timeout + 999) / 1000; 1551 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED); 1552 request->ccb = ccb; 1553 1554 ch->running = request; 1555 ch->state = ATA_ACTIVE; 1556 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) { 1557 ch->running = NULL; 1558 ch->state = ATA_IDLE; 1559 ata_cam_end_transaction(dev, request); 1560 return; 1561 } 1562 } 1563 1564 static void 1565 ata_cam_process_sense(device_t dev, struct ata_request *request) 1566 { 1567 struct ata_channel *ch = device_get_softc(dev); 1568 union ccb *ccb = request->ccb; 1569 int fatalerr = 0; 1570 1571 ch->requestsense = 0; 1572 1573 if (request->flags & ATA_R_TIMEOUT) 1574 fatalerr = 1; 1575 if ((request->flags & ATA_R_TIMEOUT) == 0 && 1576 (request->status & ATA_S_ERROR) == 0 && 1577 request->result == 0) { 1578 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 1579 } else { 1580 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1581 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL; 1582 } 1583 1584 ata_free_request(request); 1585 xpt_done(ccb); 1586 /* Do error recovery if needed. */ 1587 if (fatalerr) 1588 ata_reinit(dev); 1589 } 1590 1591 void 1592 ata_cam_end_transaction(device_t dev, struct ata_request *request) 1593 { 1594 struct ata_channel *ch = device_get_softc(dev); 1595 union ccb *ccb = request->ccb; 1596 int fatalerr = 0; 1597 1598 if (ch->requestsense) { 1599 ata_cam_process_sense(dev, request); 1600 return; 1601 } 1602 1603 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1604 if (request->flags & ATA_R_TIMEOUT) { 1605 xpt_freeze_simq(ch->sim, 1); 1606 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1607 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ; 1608 fatalerr = 1; 1609 } else if (request->status & ATA_S_ERROR) { 1610 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1611 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1612 } else { 1613 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1614 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1615 } 1616 } else if (request->result == ERESTART) 1617 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1618 else if (request->result != 0) 1619 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1620 else 1621 ccb->ccb_h.status |= CAM_REQ_CMP; 1622 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP && 1623 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1624 xpt_freeze_devq(ccb->ccb_h.path, 1); 1625 ccb->ccb_h.status |= CAM_DEV_QFRZN; 1626 } 1627 if (ccb->ccb_h.func_code == XPT_ATA_IO && 1628 ((request->status & ATA_S_ERROR) || 1629 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) { 1630 struct ata_res *res = &ccb->ataio.res; 1631 res->status = request->status; 1632 res->error = request->error; 1633 res->lba_low = request->u.ata.lba; 1634 res->lba_mid = request->u.ata.lba >> 8; 1635 res->lba_high = request->u.ata.lba >> 16; 1636 res->device = request->u.ata.lba >> 24; 1637 res->lba_low_exp = request->u.ata.lba >> 24; 1638 res->lba_mid_exp = request->u.ata.lba >> 32; 1639 res->lba_high_exp = request->u.ata.lba >> 40; 1640 res->sector_count = request->u.ata.count; 1641 res->sector_count_exp = request->u.ata.count >> 8; 1642 } 1643 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1644 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1645 ccb->ataio.resid = 1646 ccb->ataio.dxfer_len - request->donecount; 1647 } else { 1648 ccb->csio.resid = 1649 ccb->csio.dxfer_len - request->donecount; 1650 } 1651 } 1652 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 1653 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) 1654 ata_cam_request_sense(dev, request); 1655 else { 1656 ata_free_request(request); 1657 xpt_done(ccb); 1658 } 1659 /* Do error recovery if needed. */ 1660 if (fatalerr) 1661 ata_reinit(dev); 1662 } 1663 1664 static int 1665 ata_check_ids(device_t dev, union ccb *ccb) 1666 { 1667 struct ata_channel *ch = device_get_softc(dev); 1668 1669 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) { 1670 ccb->ccb_h.status = CAM_TID_INVALID; 1671 xpt_done(ccb); 1672 return (-1); 1673 } 1674 if (ccb->ccb_h.target_lun != 0) { 1675 ccb->ccb_h.status = CAM_LUN_INVALID; 1676 xpt_done(ccb); 1677 return (-1); 1678 } 1679 return (0); 1680 } 1681 1682 static void 1683 ataaction(struct cam_sim *sim, union ccb *ccb) 1684 { 1685 device_t dev, parent; 1686 struct ata_channel *ch; 1687 1688 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n", 1689 ccb->ccb_h.func_code)); 1690 1691 ch = (struct ata_channel *)cam_sim_softc(sim); 1692 dev = ch->dev; 1693 switch (ccb->ccb_h.func_code) { 1694 /* Common cases first */ 1695 case XPT_ATA_IO: /* Execute the requested I/O operation */ 1696 case XPT_SCSI_IO: 1697 if (ata_check_ids(dev, ccb)) 1698 return; 1699 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) 1700 << ccb->ccb_h.target_id)) == 0) { 1701 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1702 break; 1703 } 1704 if (ch->running) 1705 device_printf(dev, "already running!\n"); 1706 if (ccb->ccb_h.func_code == XPT_ATA_IO && 1707 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1708 (ccb->ataio.cmd.control & ATA_A_RESET)) { 1709 struct ata_res *res = &ccb->ataio.res; 1710 1711 bzero(res, sizeof(*res)); 1712 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) { 1713 res->lba_high = 0; 1714 res->lba_mid = 0; 1715 } else { 1716 res->lba_high = 0xeb; 1717 res->lba_mid = 0x14; 1718 } 1719 ccb->ccb_h.status = CAM_REQ_CMP; 1720 break; 1721 } 1722 ata_cam_begin_transaction(dev, ccb); 1723 return; 1724 case XPT_EN_LUN: /* Enable LUN as a target */ 1725 case XPT_TARGET_IO: /* Execute target I/O request */ 1726 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1727 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1728 case XPT_ABORT: /* Abort the specified CCB */ 1729 /* XXX Implement */ 1730 ccb->ccb_h.status = CAM_REQ_INVALID; 1731 break; 1732 case XPT_SET_TRAN_SETTINGS: 1733 { 1734 struct ccb_trans_settings *cts = &ccb->cts; 1735 struct ata_cam_device *d; 1736 1737 if (ata_check_ids(dev, ccb)) 1738 return; 1739 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1740 d = &ch->curr[ccb->ccb_h.target_id]; 1741 else 1742 d = &ch->user[ccb->ccb_h.target_id]; 1743 if (ch->flags & ATA_SATA) { 1744 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 1745 d->revision = cts->xport_specific.sata.revision; 1746 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) { 1747 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 1748 d->mode = ATA_SETMODE(ch->dev, 1749 ccb->ccb_h.target_id, 1750 cts->xport_specific.sata.mode); 1751 } else 1752 d->mode = cts->xport_specific.sata.mode; 1753 } 1754 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 1755 d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 1756 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI) 1757 d->atapi = cts->xport_specific.sata.atapi; 1758 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1759 d->caps = cts->xport_specific.sata.caps; 1760 } else { 1761 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) { 1762 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 1763 d->mode = ATA_SETMODE(ch->dev, 1764 ccb->ccb_h.target_id, 1765 cts->xport_specific.ata.mode); 1766 } else 1767 d->mode = cts->xport_specific.ata.mode; 1768 } 1769 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 1770 d->bytecount = cts->xport_specific.ata.bytecount; 1771 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI) 1772 d->atapi = cts->xport_specific.ata.atapi; 1773 } 1774 ccb->ccb_h.status = CAM_REQ_CMP; 1775 break; 1776 } 1777 case XPT_GET_TRAN_SETTINGS: 1778 { 1779 struct ccb_trans_settings *cts = &ccb->cts; 1780 struct ata_cam_device *d; 1781 1782 if (ata_check_ids(dev, ccb)) 1783 return; 1784 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1785 d = &ch->curr[ccb->ccb_h.target_id]; 1786 else 1787 d = &ch->user[ccb->ccb_h.target_id]; 1788 cts->protocol = PROTO_ATA; 1789 cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1790 if (ch->flags & ATA_SATA) { 1791 cts->transport = XPORT_SATA; 1792 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1793 cts->xport_specific.sata.valid = 0; 1794 cts->xport_specific.sata.mode = d->mode; 1795 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 1796 cts->xport_specific.sata.bytecount = d->bytecount; 1797 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 1798 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 1799 cts->xport_specific.sata.revision = 1800 ATA_GETREV(dev, ccb->ccb_h.target_id); 1801 if (cts->xport_specific.sata.revision != 0xff) { 1802 cts->xport_specific.sata.valid |= 1803 CTS_SATA_VALID_REVISION; 1804 } 1805 cts->xport_specific.sata.caps = 1806 d->caps & CTS_SATA_CAPS_D; 1807 if (ch->pm_level) { 1808 cts->xport_specific.sata.caps |= 1809 CTS_SATA_CAPS_H_PMREQ; 1810 } 1811 cts->xport_specific.sata.caps &= 1812 ch->user[ccb->ccb_h.target_id].caps; 1813 cts->xport_specific.sata.valid |= 1814 CTS_SATA_VALID_CAPS; 1815 } else { 1816 cts->xport_specific.sata.revision = d->revision; 1817 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 1818 cts->xport_specific.sata.caps = d->caps; 1819 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1820 } 1821 cts->xport_specific.sata.atapi = d->atapi; 1822 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 1823 } else { 1824 cts->transport = XPORT_ATA; 1825 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1826 cts->xport_specific.ata.valid = 0; 1827 cts->xport_specific.ata.mode = d->mode; 1828 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE; 1829 cts->xport_specific.ata.bytecount = d->bytecount; 1830 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT; 1831 cts->xport_specific.ata.atapi = d->atapi; 1832 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI; 1833 } 1834 ccb->ccb_h.status = CAM_REQ_CMP; 1835 break; 1836 } 1837 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1838 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1839 ata_reinit(dev); 1840 ccb->ccb_h.status = CAM_REQ_CMP; 1841 break; 1842 case XPT_TERM_IO: /* Terminate the I/O process */ 1843 /* XXX Implement */ 1844 ccb->ccb_h.status = CAM_REQ_INVALID; 1845 break; 1846 case XPT_PATH_INQ: /* Path routing inquiry */ 1847 { 1848 struct ccb_pathinq *cpi = &ccb->cpi; 1849 1850 parent = device_get_parent(dev); 1851 cpi->version_num = 1; /* XXX??? */ 1852 cpi->hba_inquiry = PI_SDTR_ABLE; 1853 cpi->target_sprt = 0; 1854 cpi->hba_misc = PIM_SEQSCAN; 1855 cpi->hba_eng_cnt = 0; 1856 if (ch->flags & ATA_NO_SLAVE) 1857 cpi->max_target = 0; 1858 else 1859 cpi->max_target = 1; 1860 cpi->max_lun = 0; 1861 cpi->initiator_id = 0; 1862 cpi->bus_id = cam_sim_bus(sim); 1863 if (ch->flags & ATA_SATA) 1864 cpi->base_transfer_speed = 150000; 1865 else 1866 cpi->base_transfer_speed = 3300; 1867 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1868 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN); 1869 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1870 cpi->unit_number = cam_sim_unit(sim); 1871 if (ch->flags & ATA_SATA) 1872 cpi->transport = XPORT_SATA; 1873 else 1874 cpi->transport = XPORT_ATA; 1875 cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1876 cpi->protocol = PROTO_ATA; 1877 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1878 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS; 1879 if (device_get_devclass(device_get_parent(parent)) == 1880 devclass_find("pci")) { 1881 cpi->hba_vendor = pci_get_vendor(parent); 1882 cpi->hba_device = pci_get_device(parent); 1883 cpi->hba_subvendor = pci_get_subvendor(parent); 1884 cpi->hba_subdevice = pci_get_subdevice(parent); 1885 } 1886 cpi->ccb_h.status = CAM_REQ_CMP; 1887 break; 1888 } 1889 default: 1890 ccb->ccb_h.status = CAM_REQ_INVALID; 1891 break; 1892 } 1893 xpt_done(ccb); 1894 } 1895 1896 static void 1897 atapoll(struct cam_sim *sim) 1898 { 1899 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim); 1900 1901 ata_interrupt_locked(ch); 1902 } 1903 #endif 1904 1905 /* 1906 * module handeling 1907 */ 1908 static int 1909 ata_module_event_handler(module_t mod, int what, void *arg) 1910 { 1911 #ifndef ATA_CAM 1912 static struct cdev *atacdev; 1913 #endif 1914 1915 switch (what) { 1916 case MOD_LOAD: 1917 #ifndef ATA_CAM 1918 /* register controlling device */ 1919 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1920 1921 if (cold) { 1922 /* register boot attach to be run when interrupts are enabled */ 1923 if (!(ata_delayed_attach = (struct intr_config_hook *) 1924 malloc(sizeof(struct intr_config_hook), 1925 M_TEMP, M_NOWAIT | M_ZERO))) { 1926 printf("ata: malloc of delayed attach hook failed\n"); 1927 return EIO; 1928 } 1929 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1930 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1931 printf("ata: config_intrhook_establish failed\n"); 1932 free(ata_delayed_attach, M_TEMP); 1933 } 1934 } 1935 #endif 1936 return 0; 1937 1938 case MOD_UNLOAD: 1939 #ifndef ATA_CAM 1940 /* deregister controlling device */ 1941 destroy_dev(atacdev); 1942 #endif 1943 return 0; 1944 1945 default: 1946 return EOPNOTSUPP; 1947 } 1948 } 1949 1950 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL }; 1951 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 1952 MODULE_VERSION(ata, 1); 1953 #ifdef ATA_CAM 1954 MODULE_DEPEND(ata, cam, 1, 1, 1); 1955 #endif 1956 1957 static void 1958 ata_init(void) 1959 { 1960 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request), 1961 NULL, NULL, NULL, NULL, 0, 0); 1962 ata_composite_zone = uma_zcreate("ata_composite", 1963 sizeof(struct ata_composite), 1964 NULL, NULL, NULL, NULL, 0, 0); 1965 } 1966 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL); 1967 1968 static void 1969 ata_uninit(void) 1970 { 1971 uma_zdestroy(ata_composite_zone); 1972 uma_zdestroy(ata_request_zone); 1973 } 1974 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL); 1975