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