1 /* 2 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * 35 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org> 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies. 40 * 41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48 * 49 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $ 50 * $DragonFly$ 51 */ 52 /* 53 * Implement each SATA port as its own SCSI bus on CAM. This way we can 54 * implement future port multiplier features as individual devices on the 55 * bus. 56 * 57 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest 58 * was written natively for DragonFly. 59 */ 60 61 #include "ahci.h" 62 63 static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb); 64 static void ahci_xpt_poll(struct cam_sim *sim); 65 static void ahci_xpt_scsi_disk_io(struct ahci_port *ap, 66 struct ata_port *at, union ccb *ccb); 67 static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap, 68 struct ata_port *at, union ccb *ccb); 69 static void ahci_xpt_page_inquiry(struct ahci_port *ap, 70 struct ata_port *at, union ccb *ccb); 71 72 static void ahci_ata_complete_disk_rw(struct ata_xfer *xa); 73 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa); 74 static void ahci_atapi_complete_cmd(struct ata_xfer *xa); 75 static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data); 76 static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, 77 struct scsi_sense_data *sense_data); 78 79 static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at); 80 static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at); 81 static void ahci_ata_dummy_done(struct ata_xfer *xa); 82 static void ata_fix_identify(struct ata_identify *id); 83 static void ahci_cam_rescan(struct ahci_port *ap); 84 static void ahci_strip_string(const char **basep, int *lenp); 85 86 int 87 ahci_cam_attach(struct ahci_port *ap) 88 { 89 struct cam_devq *devq; 90 struct cam_sim *sim; 91 int error; 92 int unit; 93 94 /* 95 * We want at least one ccb to be available for error processing 96 * so don't let CAM use more then ncmds - 1. 97 */ 98 unit = device_get_unit(ap->ap_sc->sc_dev); 99 if (ap->ap_sc->sc_ncmds > 1) 100 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1); 101 else 102 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds); 103 if (devq == NULL) { 104 return (ENOMEM); 105 } 106 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci", 107 (void *)ap, unit, &sim_mplock, 1, 1, devq); 108 cam_simq_release(devq); 109 if (sim == NULL) { 110 return (ENOMEM); 111 } 112 ap->ap_sim = sim; 113 ahci_os_unlock_port(ap); 114 error = xpt_bus_register(ap->ap_sim, ap->ap_num); 115 ahci_os_lock_port(ap); 116 if (error != CAM_SUCCESS) { 117 ahci_cam_detach(ap); 118 return (EINVAL); 119 } 120 ap->ap_flags |= AP_F_BUS_REGISTERED; 121 122 if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 123 error = ahci_cam_probe(ap, NULL); 124 else 125 error = 0; 126 if (error) { 127 ahci_cam_detach(ap); 128 return (EIO); 129 } 130 ap->ap_flags |= AP_F_CAM_ATTACHED; 131 132 return(0); 133 } 134 135 /* 136 * The state of the port has changed. 137 * 138 * If at is NULL the physical port has changed state. 139 * If at is non-NULL a particular target behind a PM has changed state. 140 * 141 * If found is -1 the target state must be queued to a non-interrupt context. 142 * (only works with at == NULL). 143 * 144 * If found is 0 the target was removed. 145 * If found is 1 the target was inserted. 146 */ 147 void 148 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found) 149 { 150 struct cam_path *tmppath; 151 int status; 152 int target; 153 154 target = atx ? atx->at_target : CAM_TARGET_WILDCARD; 155 156 if (ap->ap_sim == NULL) 157 return; 158 if (found == CAM_TARGET_WILDCARD) { 159 status = xpt_create_path(&tmppath, NULL, 160 cam_sim_path(ap->ap_sim), 161 target, CAM_LUN_WILDCARD); 162 if (status != CAM_REQ_CMP) 163 return; 164 ahci_cam_rescan(ap); 165 } else { 166 status = xpt_create_path(&tmppath, NULL, 167 cam_sim_path(ap->ap_sim), 168 target, 169 CAM_LUN_WILDCARD); 170 if (status != CAM_REQ_CMP) 171 return; 172 #if 0 173 /* 174 * This confuses CAM 175 */ 176 if (found) 177 xpt_async(AC_FOUND_DEVICE, tmppath, NULL); 178 else 179 xpt_async(AC_LOST_DEVICE, tmppath, NULL); 180 #endif 181 } 182 xpt_free_path(tmppath); 183 } 184 185 void 186 ahci_cam_detach(struct ahci_port *ap) 187 { 188 int error; 189 190 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0) 191 return; 192 get_mplock(); 193 if (ap->ap_sim) { 194 xpt_freeze_simq(ap->ap_sim, 1); 195 } 196 if (ap->ap_flags & AP_F_BUS_REGISTERED) { 197 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim)); 198 KKASSERT(error == CAM_REQ_CMP); 199 ap->ap_flags &= ~AP_F_BUS_REGISTERED; 200 } 201 if (ap->ap_sim) { 202 cam_sim_free(ap->ap_sim); 203 ap->ap_sim = NULL; 204 } 205 rel_mplock(); 206 ap->ap_flags &= ~AP_F_CAM_ATTACHED; 207 } 208 209 /* 210 * Once the AHCI port has been attached we need to probe for a device or 211 * devices on the port and setup various options. 212 * 213 * If at is NULL we are probing the direct-attached device on the port, 214 * which may or may not be a port multiplier. 215 */ 216 int 217 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx) 218 { 219 struct ata_port *at; 220 struct ata_xfer *xa; 221 u_int64_t capacity; 222 u_int64_t capacity_bytes; 223 int model_len; 224 int firmware_len; 225 int serial_len; 226 int error; 227 int devncqdepth; 228 int i; 229 const char *model_id; 230 const char *firmware_id; 231 const char *serial_id; 232 const char *wcstr; 233 const char *rastr; 234 const char *scstr; 235 const char *type; 236 237 error = EIO; 238 239 /* 240 * Delayed CAM attachment for initial probe, sim may be NULL 241 */ 242 if (ap->ap_sim == NULL) 243 return(0); 244 245 /* 246 * A NULL atx indicates a probe of the directly connected device. 247 * A non-NULL atx indicates a device connected via a port multiplier. 248 * We need to preserve atx for calls to ahci_ata_get_xfer(). 249 * 250 * at is always non-NULL. For directly connected devices we supply 251 * an (at) pointing to target 0. 252 */ 253 if (atx == NULL) { 254 at = ap->ap_ata; /* direct attached - device 0 */ 255 if (ap->ap_type == ATA_PORT_T_PM) { 256 kprintf("%s: Found Port Multiplier\n", 257 ATANAME(ap, atx)); 258 return (0); 259 } 260 at->at_type = ap->ap_type; 261 } else { 262 at = atx; 263 if (atx->at_type == ATA_PORT_T_PM) { 264 kprintf("%s: Bogus device, reducing port count to %d\n", 265 ATANAME(ap, atx), atx->at_target); 266 if (ap->ap_pmcount > atx->at_target) 267 ap->ap_pmcount = atx->at_target; 268 goto err; 269 } 270 } 271 if (ap->ap_type == ATA_PORT_T_NONE) 272 goto err; 273 if (at->at_type == ATA_PORT_T_NONE) 274 goto err; 275 276 /* 277 * Issue identify, saving the result 278 */ 279 xa = ahci_ata_get_xfer(ap, atx); 280 xa->complete = ahci_ata_dummy_done; 281 xa->data = &at->at_identify; 282 xa->datalen = sizeof(at->at_identify); 283 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; 284 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 285 286 switch(at->at_type) { 287 case ATA_PORT_T_DISK: 288 xa->fis->command = ATA_C_IDENTIFY; 289 type = "DISK"; 290 break; 291 case ATA_PORT_T_ATAPI: 292 xa->fis->command = ATA_C_ATAPI_IDENTIFY; 293 xa->flags |= ATA_F_AUTOSENSE; 294 type = "ATAPI"; 295 break; 296 default: 297 xa->fis->command = ATA_C_ATAPI_IDENTIFY; 298 type = "UNKNOWN(ATAPI?)"; 299 break; 300 } 301 xa->fis->features = 0; 302 xa->fis->device = 0; 303 xa->timeout = 1000; 304 305 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { 306 kprintf("%s: Detected %s device but unable to IDENTIFY\n", 307 ATANAME(ap, atx), type); 308 ahci_ata_put_xfer(xa); 309 goto err; 310 } 311 ahci_ata_put_xfer(xa); 312 313 ata_fix_identify(&at->at_identify); 314 315 /* 316 * Read capacity using SATA probe info. 317 */ 318 if (le16toh(at->at_identify.cmdset83) & 0x0400) { 319 /* LBA48 feature set supported */ 320 capacity = 0; 321 for (i = 3; i >= 0; --i) { 322 capacity <<= 16; 323 capacity += 324 le16toh(at->at_identify.addrsecxt[i]); 325 } 326 } else { 327 capacity = le16toh(at->at_identify.addrsec[1]); 328 capacity <<= 16; 329 capacity += le16toh(at->at_identify.addrsec[0]); 330 } 331 if (capacity == 0) 332 capacity = 1024 * 1024 / 512; 333 at->at_capacity = capacity; 334 if (atx == NULL) 335 ap->ap_probe = ATA_PROBE_GOOD; 336 337 capacity_bytes = capacity * 512; 338 339 /* 340 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated 341 * number of slots and limit the number of CAM ccb's to one less 342 * so we always have a slot available for recovery. 343 * 344 * NCQ is not used if ap_ncqdepth is 1 or the host controller does 345 * not support it, and in that case the driver can handle extra 346 * ccb's. 347 * 348 * NCQ is currently used only with direct-attached disks. It is 349 * not used with port multipliers or direct-attached ATAPI devices. 350 * 351 * Remember at least one extra CCB needs to be reserved for the 352 * error ccb. 353 */ 354 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && 355 ap->ap_type == ATA_PORT_T_DISK && 356 (le16toh(at->at_identify.satacap) & (1 << 8))) { 357 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1; 358 devncqdepth = at->at_ncqdepth; 359 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds) 360 at->at_ncqdepth = ap->ap_sc->sc_ncmds; 361 if (at->at_ncqdepth > 1) { 362 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) { 363 xa = ahci_ata_get_xfer(ap, atx); 364 if (xa->tag < at->at_ncqdepth) { 365 xa->state = ATA_S_COMPLETE; 366 ahci_ata_put_xfer(xa); 367 } 368 } 369 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) { 370 cam_devq_resize(ap->ap_sim->devq, 371 at->at_ncqdepth - 1); 372 } 373 } 374 } else { 375 devncqdepth = 0; 376 } 377 378 model_len = sizeof(at->at_identify.model); 379 model_id = at->at_identify.model; 380 ahci_strip_string(&model_id, &model_len); 381 382 firmware_len = sizeof(at->at_identify.firmware); 383 firmware_id = at->at_identify.firmware; 384 ahci_strip_string(&firmware_id, &firmware_len); 385 386 serial_len = sizeof(at->at_identify.serial); 387 serial_id = at->at_identify.serial; 388 ahci_strip_string(&serial_id, &serial_len); 389 390 /* 391 * Generate informatiive strings. 392 * 393 * NOTE: We do not automatically set write caching, lookahead, 394 * or the security state for ATAPI devices. 395 */ 396 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) { 397 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) 398 wcstr = "enabled"; 399 else if (at->at_type == ATA_PORT_T_ATAPI) 400 wcstr = "disabled"; 401 else 402 wcstr = "enabling"; 403 } else { 404 wcstr = "notsupp"; 405 } 406 407 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) { 408 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) 409 rastr = "enabled"; 410 else if (at->at_type == ATA_PORT_T_ATAPI) 411 rastr = "disabled"; 412 else 413 rastr = "enabling"; 414 } else { 415 rastr = "notsupp"; 416 } 417 418 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) { 419 if (at->at_identify.securestatus & ATA_SECURE_FROZEN) 420 scstr = "frozen"; 421 else if (at->at_type == ATA_PORT_T_ATAPI) 422 scstr = "unfrozen"; 423 else if (AhciNoFeatures & (1 << ap->ap_num)) 424 scstr = "<disabled>"; 425 else 426 scstr = "freezing"; 427 } else { 428 scstr = "notsupp"; 429 } 430 431 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n" 432 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s " 433 "capacity=%lld.%02dMB\n", 434 435 ATANAME(ap, atx), 436 type, 437 model_len, model_len, model_id, 438 firmware_len, firmware_len, firmware_id, 439 serial_len, serial_len, serial_id, 440 441 ATANAME(ap, atx), 442 devncqdepth, ap->ap_sc->sc_ncmds, 443 at->at_identify.satacap, 444 at->at_identify.satafsup, 445 (at->at_ncqdepth > 1 ? "YES" : "NO"), 446 (long long)capacity_bytes / (1024 * 1024), 447 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024) 448 ); 449 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n", 450 ATANAME(ap, atx), 451 at->at_identify.features85, 452 at->at_identify.features86, 453 at->at_identify.features87, 454 wcstr, 455 rastr, 456 scstr 457 ); 458 459 /* 460 * Additional type-specific probing 461 */ 462 switch(at->at_type) { 463 case ATA_PORT_T_DISK: 464 error = ahci_cam_probe_disk(ap, atx); 465 break; 466 case ATA_PORT_T_ATAPI: 467 error = ahci_cam_probe_atapi(ap, atx); 468 break; 469 default: 470 error = EIO; 471 break; 472 } 473 err: 474 if (error) { 475 at->at_probe = ATA_PROBE_FAILED; 476 if (atx == NULL) 477 ap->ap_probe = at->at_probe; 478 } else { 479 at->at_probe = ATA_PROBE_GOOD; 480 if (atx == NULL) 481 ap->ap_probe = at->at_probe; 482 } 483 return (error); 484 } 485 486 /* 487 * DISK-specific probe after initial ident 488 */ 489 static int 490 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx) 491 { 492 struct ata_port *at; 493 struct ata_xfer *xa; 494 495 at = atx ? atx : ap->ap_ata; 496 497 /* 498 * Enable write cache if supported 499 * 500 * NOTE: "WD My Book" external disk devices have a very poor 501 * daughter board between the the ESATA and the HD. Sending 502 * any ATA_C_SET_FEATURES commands will break the hardware port 503 * with a fatal protocol error. However, this device also 504 * indicates that WRITECACHE is already on and READAHEAD is 505 * not supported so we avoid the issue. 506 */ 507 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) && 508 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) { 509 xa = ahci_ata_get_xfer(ap, atx); 510 xa->complete = ahci_ata_dummy_done; 511 xa->fis->command = ATA_C_SET_FEATURES; 512 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/ 513 xa->fis->features = ATA_SF_LOOKAHEAD_EN; 514 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 515 xa->fis->device = 0; 516 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; 517 xa->timeout = 1000; 518 xa->datalen = 0; 519 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 520 at->at_features |= ATA_PORT_F_WCACHE; 521 else 522 kprintf("%s: Unable to enable write-caching\n", 523 ATANAME(ap, atx)); 524 ahci_ata_put_xfer(xa); 525 } 526 527 /* 528 * Enable readahead if supported 529 */ 530 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) && 531 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) { 532 xa = ahci_ata_get_xfer(ap, atx); 533 xa->complete = ahci_ata_dummy_done; 534 xa->fis->command = ATA_C_SET_FEATURES; 535 xa->fis->features = ATA_SF_LOOKAHEAD_EN; 536 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 537 xa->fis->device = 0; 538 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; 539 xa->timeout = 1000; 540 xa->datalen = 0; 541 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 542 at->at_features |= ATA_PORT_F_RAHEAD; 543 else 544 kprintf("%s: Unable to enable read-ahead\n", 545 ATANAME(ap, atx)); 546 ahci_ata_put_xfer(xa); 547 } 548 549 /* 550 * FREEZE LOCK the device so malicious users can't lock it on us. 551 * As there is no harm in issuing this to devices that don't 552 * support the security feature set we just send it, and don't bother 553 * checking if the device sends a command abort to tell us it doesn't 554 * support it 555 */ 556 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) && 557 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 && 558 (AhciNoFeatures & (1 << ap->ap_num)) == 0) { 559 xa = ahci_ata_get_xfer(ap, atx); 560 xa->complete = ahci_ata_dummy_done; 561 xa->fis->command = ATA_C_SEC_FREEZE_LOCK; 562 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 563 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; 564 xa->timeout = 1000; 565 xa->datalen = 0; 566 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 567 at->at_features |= ATA_PORT_F_FRZLCK; 568 else 569 kprintf("%s: Unable to set security freeze\n", 570 ATANAME(ap, atx)); 571 ahci_ata_put_xfer(xa); 572 } 573 574 return (0); 575 } 576 577 /* 578 * ATAPI-specific probe after initial ident 579 */ 580 static int 581 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx) 582 { 583 return(0); 584 } 585 586 /* 587 * Fix byte ordering so buffers can be accessed as 588 * strings. 589 */ 590 static void 591 ata_fix_identify(struct ata_identify *id) 592 { 593 u_int16_t *swap; 594 int i; 595 596 swap = (u_int16_t *)id->serial; 597 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++) 598 swap[i] = bswap16(swap[i]); 599 600 swap = (u_int16_t *)id->firmware; 601 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++) 602 swap[i] = bswap16(swap[i]); 603 604 swap = (u_int16_t *)id->model; 605 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++) 606 swap[i] = bswap16(swap[i]); 607 } 608 609 /* 610 * Dummy done callback for xa. 611 */ 612 static void 613 ahci_ata_dummy_done(struct ata_xfer *xa) 614 { 615 } 616 617 /* 618 * Use an engineering request to initiate a target scan for devices 619 * behind a port multiplier. 620 * 621 * An asynchronous bus scan is used to avoid reentrancy issues. 622 */ 623 static void 624 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 625 { 626 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 627 628 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) { 629 ap->ap_flags &= ~AP_F_SCAN_RUNNING; 630 if (ap->ap_flags & AP_F_SCAN_REQUESTED) { 631 ap->ap_flags &= ~AP_F_SCAN_REQUESTED; 632 ahci_cam_rescan(ap); 633 } 634 ap->ap_flags |= AP_F_SCAN_COMPLETED; 635 wakeup(&ap->ap_flags); 636 } 637 xpt_free_ccb(ccb); 638 } 639 640 static void 641 ahci_cam_rescan(struct ahci_port *ap) 642 { 643 struct cam_path *path; 644 union ccb *ccb; 645 int status; 646 int i; 647 648 if (ap->ap_flags & AP_F_SCAN_RUNNING) { 649 ap->ap_flags |= AP_F_SCAN_REQUESTED; 650 return; 651 } 652 ap->ap_flags |= AP_F_SCAN_RUNNING; 653 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 654 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN; 655 } 656 657 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), 658 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 659 if (status != CAM_REQ_CMP) 660 return; 661 662 ccb = xpt_alloc_ccb(); 663 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ 664 ccb->ccb_h.func_code = XPT_ENG_EXEC; 665 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; 666 ccb->ccb_h.sim_priv.entries[0].ptr = ap; 667 ccb->crcn.flags = CAM_FLAG_NONE; 668 xpt_action_async(ccb); 669 } 670 671 static void 672 ahci_xpt_rescan(struct ahci_port *ap) 673 { 674 struct cam_path *path; 675 union ccb *ccb; 676 int status; 677 678 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), 679 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 680 if (status != CAM_REQ_CMP) 681 return; 682 683 ccb = xpt_alloc_ccb(); 684 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ 685 ccb->ccb_h.func_code = XPT_SCAN_BUS; 686 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; 687 ccb->ccb_h.sim_priv.entries[0].ptr = ap; 688 ccb->crcn.flags = CAM_FLAG_NONE; 689 xpt_action_async(ccb); 690 } 691 692 /* 693 * Action function - dispatch command 694 */ 695 static 696 void 697 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb) 698 { 699 struct ahci_port *ap; 700 struct ata_port *at, *atx; 701 struct ccb_hdr *ccbh; 702 int unit; 703 704 /* XXX lock */ 705 ap = cam_sim_softc(sim); 706 at = ap->ap_ata; 707 atx = NULL; 708 KKASSERT(ap != NULL); 709 ccbh = &ccb->ccb_h; 710 unit = cam_sim_unit(sim); 711 712 /* 713 * Early failure checks. These checks do not apply to XPT_PATH_INQ, 714 * otherwise the bus rescan will not remove the dead devices when 715 * unplugging a PM. 716 * 717 * For non-wildcards we have one target (0) and one lun (0), 718 * unless we have a port multiplier. 719 * 720 * A wildcard target indicates only the general bus is being 721 * probed. 722 * 723 * Calculate at and atx. at is always non-NULL. atx is only 724 * non-NULL for direct-attached devices. It will be NULL for 725 * devices behind a port multiplier. 726 * 727 * XXX What do we do with a LUN wildcard? 728 */ 729 if (ccbh->target_id != CAM_TARGET_WILDCARD && 730 ccbh->func_code != XPT_PATH_INQ) { 731 if (ap->ap_type == ATA_PORT_T_NONE) { 732 ccbh->status = CAM_DEV_NOT_THERE; 733 xpt_done(ccb); 734 return; 735 } 736 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) { 737 ccbh->status = CAM_DEV_NOT_THERE; 738 xpt_done(ccb); 739 return; 740 } 741 at += ccbh->target_id; 742 if (ap->ap_type == ATA_PORT_T_PM) 743 atx = at; 744 745 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) { 746 ccbh->status = CAM_DEV_NOT_THERE; 747 xpt_done(ccb); 748 return; 749 } 750 } 751 752 /* 753 * Switch on the meta XPT command 754 */ 755 switch(ccbh->func_code) { 756 case XPT_ENG_EXEC: 757 /* 758 * This routine is called after a port multiplier has been 759 * probed. 760 */ 761 ccbh->status = CAM_REQ_CMP; 762 ahci_os_lock_port(ap); 763 ahci_port_state_machine(ap, 0); 764 ahci_os_unlock_port(ap); 765 xpt_done(ccb); 766 ahci_xpt_rescan(ap); 767 break; 768 case XPT_PATH_INQ: 769 /* 770 * This command always succeeds, otherwise the bus scan 771 * will not detach dead devices. 772 */ 773 ccb->cpi.version_num = 1; 774 ccb->cpi.hba_inquiry = 0; 775 ccb->cpi.target_sprt = 0; 776 ccb->cpi.hba_misc = PIM_SEQSCAN; 777 ccb->cpi.hba_eng_cnt = 0; 778 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags)); 779 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1; 780 ccb->cpi.max_lun = 0; 781 ccb->cpi.async_flags = 0; 782 ccb->cpi.hpath_id = 0; 783 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1; 784 ccb->cpi.unit_number = cam_sim_unit(sim); 785 ccb->cpi.bus_id = cam_sim_bus(sim); 786 ccb->cpi.base_transfer_speed = 150000; 787 ccb->cpi.transport = XPORT_SATA; 788 ccb->cpi.transport_version = 1; 789 ccb->cpi.protocol = PROTO_SCSI; 790 ccb->cpi.protocol_version = SCSI_REV_2; 791 792 ccbh->status = CAM_REQ_CMP; 793 if (ccbh->target_id == CAM_TARGET_WILDCARD) { 794 ahci_os_lock_port(ap); 795 ahci_port_state_machine(ap, 0); 796 ahci_os_unlock_port(ap); 797 } else { 798 switch(ahci_pread(ap, AHCI_PREG_SSTS) & 799 AHCI_PREG_SSTS_SPD) { 800 case AHCI_PREG_SSTS_SPD_GEN1: 801 ccb->cpi.base_transfer_speed = 150000; 802 break; 803 case AHCI_PREG_SSTS_SPD_GEN2: 804 ccb->cpi.base_transfer_speed = 300000; 805 break; 806 default: 807 /* unknown */ 808 ccb->cpi.base_transfer_speed = 1000; 809 break; 810 } 811 #if 0 812 if (ap->ap_type == ATA_PORT_T_NONE) 813 ccbh->status = CAM_DEV_NOT_THERE; 814 #endif 815 } 816 xpt_done(ccb); 817 break; 818 case XPT_RESET_DEV: 819 ahci_os_lock_port(ap); 820 if (ap->ap_type == ATA_PORT_T_NONE) { 821 ccbh->status = CAM_DEV_NOT_THERE; 822 } else { 823 ahci_port_reset(ap, atx, 0); 824 ccbh->status = CAM_REQ_CMP; 825 } 826 ahci_os_unlock_port(ap); 827 xpt_done(ccb); 828 break; 829 case XPT_RESET_BUS: 830 ahci_os_lock_port(ap); 831 ahci_port_reset(ap, NULL, 1); 832 ahci_os_unlock_port(ap); 833 ccbh->status = CAM_REQ_CMP; 834 xpt_done(ccb); 835 break; 836 case XPT_SET_TRAN_SETTINGS: 837 ccbh->status = CAM_FUNC_NOTAVAIL; 838 xpt_done(ccb); 839 break; 840 case XPT_GET_TRAN_SETTINGS: 841 ccb->cts.protocol = PROTO_SCSI; 842 ccb->cts.protocol_version = SCSI_REV_2; 843 ccb->cts.transport = XPORT_SATA; 844 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED; 845 ccb->cts.proto_specific.valid = 0; 846 ccb->cts.xport_specific.valid = 0; 847 ccbh->status = CAM_REQ_CMP; 848 xpt_done(ccb); 849 break; 850 case XPT_CALC_GEOMETRY: 851 cam_calc_geometry(&ccb->ccg, 1); 852 xpt_done(ccb); 853 break; 854 case XPT_SCSI_IO: 855 /* 856 * Our parallel startup code might have only probed through 857 * to the IDENT, so do the last step if necessary. 858 */ 859 if (at->at_probe == ATA_PROBE_NEED_IDENT) 860 ahci_cam_probe(ap, atx); 861 if (at->at_probe != ATA_PROBE_GOOD) { 862 ccbh->status = CAM_DEV_NOT_THERE; 863 xpt_done(ccb); 864 break; 865 } 866 switch(at->at_type) { 867 case ATA_PORT_T_DISK: 868 ahci_xpt_scsi_disk_io(ap, atx, ccb); 869 break; 870 case ATA_PORT_T_ATAPI: 871 ahci_xpt_scsi_atapi_io(ap, atx, ccb); 872 break; 873 default: 874 ccbh->status = CAM_REQ_INVALID; 875 xpt_done(ccb); 876 break; 877 } 878 break; 879 default: 880 ccbh->status = CAM_REQ_INVALID; 881 xpt_done(ccb); 882 break; 883 } 884 } 885 886 /* 887 * Poll function. 888 * 889 * Generally this function gets called heavily when interrupts might be 890 * non-operational, during a halt/reboot or panic. 891 */ 892 static 893 void 894 ahci_xpt_poll(struct cam_sim *sim) 895 { 896 struct ahci_port *ap; 897 898 ap = cam_sim_softc(sim); 899 crit_enter(); 900 ahci_os_lock_port(ap); 901 ahci_port_intr(ap, 1); 902 ahci_os_unlock_port(ap); 903 crit_exit(); 904 } 905 906 /* 907 * Convert the SCSI command in ccb to an ata_xfer command in xa 908 * for ATA_PORT_T_DISK operations. Set the completion function 909 * to convert the response back, then dispatch to the OpenBSD AHCI 910 * layer. 911 * 912 * AHCI DISK commands only support a limited command set, and we 913 * fake additional commands to make it play nice with the CAM subsystem. 914 */ 915 static 916 void 917 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx, 918 union ccb *ccb) 919 { 920 struct ccb_hdr *ccbh; 921 struct ccb_scsiio *csio; 922 struct ata_xfer *xa; 923 struct ata_port *at; 924 struct ata_fis_h2d *fis; 925 scsi_cdb_t cdb; 926 union scsi_data *rdata; 927 int rdata_len; 928 u_int64_t capacity; 929 u_int64_t lba; 930 u_int32_t count; 931 932 ccbh = &ccb->csio.ccb_h; 933 csio = &ccb->csio; 934 at = atx ? atx : &ap->ap_ata[0]; 935 936 /* 937 * XXX not passing NULL at for direct attach! 938 */ 939 xa = ahci_ata_get_xfer(ap, atx); 940 rdata = (void *)csio->data_ptr; 941 rdata_len = csio->dxfer_len; 942 943 /* 944 * Build the FIS or process the csio to completion. 945 */ 946 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? 947 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); 948 949 switch(cdb->generic.opcode) { 950 case REQUEST_SENSE: 951 /* 952 * Auto-sense everything, so explicit sense requests 953 * return no-sense. 954 */ 955 ccbh->status = CAM_SCSI_STATUS_ERROR; 956 break; 957 case INQUIRY: 958 /* 959 * Inquiry supported features 960 * 961 * [opcode, byte2, page_code, length, control] 962 */ 963 if (cdb->inquiry.byte2 & SI_EVPD) { 964 ahci_xpt_page_inquiry(ap, at, ccb); 965 } else { 966 bzero(rdata, rdata_len); 967 if (rdata_len < SHORT_INQUIRY_LENGTH) { 968 ccbh->status = CAM_CCB_LEN_ERR; 969 break; 970 } 971 if (rdata_len > sizeof(rdata->inquiry_data)) 972 rdata_len = sizeof(rdata->inquiry_data); 973 rdata->inquiry_data.device = T_DIRECT; 974 rdata->inquiry_data.version = SCSI_REV_SPC2; 975 rdata->inquiry_data.response_format = 2; 976 rdata->inquiry_data.additional_length = 32; 977 bcopy("SATA ", rdata->inquiry_data.vendor, 8); 978 bcopy(at->at_identify.model, 979 rdata->inquiry_data.product, 980 sizeof(rdata->inquiry_data.product)); 981 bcopy(at->at_identify.firmware, 982 rdata->inquiry_data.revision, 983 sizeof(rdata->inquiry_data.revision)); 984 ccbh->status = CAM_REQ_CMP; 985 } 986 break; 987 case READ_CAPACITY_16: 988 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) { 989 ccbh->status = CAM_REQ_INVALID; 990 break; 991 } 992 if (rdata_len < sizeof(rdata->read_capacity_data_16)) { 993 ccbh->status = CAM_CCB_LEN_ERR; 994 break; 995 } 996 /* fall through */ 997 case READ_CAPACITY: 998 if (rdata_len < sizeof(rdata->read_capacity_data)) { 999 ccbh->status = CAM_CCB_LEN_ERR; 1000 break; 1001 } 1002 1003 capacity = at->at_capacity; 1004 1005 bzero(rdata, rdata_len); 1006 if (cdb->generic.opcode == READ_CAPACITY) { 1007 rdata_len = sizeof(rdata->read_capacity_data); 1008 if (capacity > 0xFFFFFFFFU) 1009 capacity = 0xFFFFFFFFU; 1010 bzero(&rdata->read_capacity_data, rdata_len); 1011 scsi_ulto4b((u_int32_t)capacity - 1, 1012 rdata->read_capacity_data.addr); 1013 scsi_ulto4b(512, rdata->read_capacity_data.length); 1014 } else { 1015 rdata_len = sizeof(rdata->read_capacity_data_16); 1016 bzero(&rdata->read_capacity_data_16, rdata_len); 1017 scsi_u64to8b(capacity - 1, 1018 rdata->read_capacity_data_16.addr); 1019 scsi_ulto4b(512, rdata->read_capacity_data_16.length); 1020 } 1021 ccbh->status = CAM_REQ_CMP; 1022 break; 1023 case SYNCHRONIZE_CACHE: 1024 /* 1025 * Synchronize cache. Specification says this can take 1026 * greater then 30 seconds so give it at least 45. 1027 */ 1028 fis = xa->fis; 1029 fis->flags = ATA_H2D_FLAGS_CMD; 1030 fis->command = ATA_C_FLUSH_CACHE; 1031 fis->device = 0; 1032 if (xa->timeout < 45000) 1033 xa->timeout = 45000; 1034 xa->datalen = 0; 1035 xa->flags = ATA_F_READ; 1036 xa->complete = ahci_ata_complete_disk_synchronize_cache; 1037 break; 1038 case TEST_UNIT_READY: 1039 case START_STOP_UNIT: 1040 case PREVENT_ALLOW: 1041 /* 1042 * Just silently return success 1043 */ 1044 ccbh->status = CAM_REQ_CMP; 1045 rdata_len = 0; 1046 break; 1047 case ATA_PASS_12: 1048 case ATA_PASS_16: 1049 /* 1050 * XXX implement pass-through 1051 */ 1052 ccbh->status = CAM_FUNC_NOTAVAIL; 1053 break; 1054 default: 1055 switch(cdb->generic.opcode) { 1056 case READ_6: 1057 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; 1058 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; 1059 xa->flags = ATA_F_READ; 1060 break; 1061 case READ_10: 1062 lba = scsi_4btoul(cdb->rw_10.addr); 1063 count = scsi_2btoul(cdb->rw_10.length); 1064 xa->flags = ATA_F_READ; 1065 break; 1066 case READ_12: 1067 lba = scsi_4btoul(cdb->rw_12.addr); 1068 count = scsi_4btoul(cdb->rw_12.length); 1069 xa->flags = ATA_F_READ; 1070 break; 1071 case READ_16: 1072 lba = scsi_8btou64(cdb->rw_16.addr); 1073 count = scsi_4btoul(cdb->rw_16.length); 1074 xa->flags = ATA_F_READ; 1075 break; 1076 case WRITE_6: 1077 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; 1078 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; 1079 xa->flags = ATA_F_WRITE; 1080 break; 1081 case WRITE_10: 1082 lba = scsi_4btoul(cdb->rw_10.addr); 1083 count = scsi_2btoul(cdb->rw_10.length); 1084 xa->flags = ATA_F_WRITE; 1085 break; 1086 case WRITE_12: 1087 lba = scsi_4btoul(cdb->rw_12.addr); 1088 count = scsi_4btoul(cdb->rw_12.length); 1089 xa->flags = ATA_F_WRITE; 1090 break; 1091 case WRITE_16: 1092 lba = scsi_8btou64(cdb->rw_16.addr); 1093 count = scsi_4btoul(cdb->rw_16.length); 1094 xa->flags = ATA_F_WRITE; 1095 break; 1096 default: 1097 ccbh->status = CAM_REQ_INVALID; 1098 break; 1099 } 1100 if (ccbh->status != CAM_REQ_INPROG) 1101 break; 1102 1103 fis = xa->fis; 1104 fis->flags = ATA_H2D_FLAGS_CMD; 1105 fis->lba_low = (u_int8_t)lba; 1106 fis->lba_mid = (u_int8_t)(lba >> 8); 1107 fis->lba_high = (u_int8_t)(lba >> 16); 1108 fis->device = ATA_H2D_DEVICE_LBA; 1109 1110 /* 1111 * NCQ only for direct-attached disks, do not currently 1112 * try to use NCQ with port multipliers. 1113 */ 1114 if (at->at_ncqdepth > 1 && 1115 ap->ap_type == ATA_PORT_T_DISK && 1116 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && 1117 (ccbh->flags & CAM_POLLED) == 0) { 1118 /* 1119 * Use NCQ - always uses 48 bit addressing 1120 */ 1121 xa->flags |= ATA_F_NCQ; 1122 fis->command = (xa->flags & ATA_F_WRITE) ? 1123 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA; 1124 fis->lba_low_exp = (u_int8_t)(lba >> 24); 1125 fis->lba_mid_exp = (u_int8_t)(lba >> 32); 1126 fis->lba_high_exp = (u_int8_t)(lba >> 40); 1127 fis->sector_count = xa->tag << 3; 1128 fis->features = (u_int8_t)count; 1129 fis->features_exp = (u_int8_t)(count >> 8); 1130 } else if (count > 0x100 || lba > 0x0FFFFFFFU) { 1131 /* 1132 * Use LBA48 1133 */ 1134 fis->command = (xa->flags & ATA_F_WRITE) ? 1135 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT; 1136 fis->lba_low_exp = (u_int8_t)(lba >> 24); 1137 fis->lba_mid_exp = (u_int8_t)(lba >> 32); 1138 fis->lba_high_exp = (u_int8_t)(lba >> 40); 1139 fis->sector_count = (u_int8_t)count; 1140 fis->sector_count_exp = (u_int8_t)(count >> 8); 1141 } else { 1142 /* 1143 * Use LBA 1144 * 1145 * NOTE: 256 sectors is supported, stored as 0. 1146 */ 1147 fis->command = (xa->flags & ATA_F_WRITE) ? 1148 ATA_C_WRITEDMA : ATA_C_READDMA; 1149 fis->device |= (u_int8_t)(lba >> 24) & 0x0F; 1150 fis->sector_count = (u_int8_t)count; 1151 } 1152 1153 xa->data = csio->data_ptr; 1154 xa->datalen = csio->dxfer_len; 1155 xa->complete = ahci_ata_complete_disk_rw; 1156 xa->timeout = ccbh->timeout; /* milliseconds */ 1157 #if 0 1158 if (xa->timeout > 10000) /* XXX - debug */ 1159 xa->timeout = 10000; 1160 #endif 1161 if (ccbh->flags & CAM_POLLED) 1162 xa->flags |= ATA_F_POLL; 1163 break; 1164 } 1165 1166 /* 1167 * If the request is still in progress the xa and FIS have 1168 * been set up (except for the PM target), and must be dispatched. 1169 * Otherwise the request was completed. 1170 */ 1171 if (ccbh->status == CAM_REQ_INPROG) { 1172 KKASSERT(xa->complete != NULL); 1173 xa->atascsi_private = ccb; 1174 ccb->ccb_h.sim_priv.entries[0].ptr = ap; 1175 ahci_os_lock_port(ap); 1176 xa->fis->flags |= at->at_target; 1177 ahci_ata_cmd(xa); 1178 ahci_os_unlock_port(ap); 1179 } else { 1180 ahci_ata_put_xfer(xa); 1181 xpt_done(ccb); 1182 } 1183 } 1184 1185 /* 1186 * Convert the SCSI command in ccb to an ata_xfer command in xa 1187 * for ATA_PORT_T_ATAPI operations. Set the completion function 1188 * to convert the response back, then dispatch to the OpenBSD AHCI 1189 * layer. 1190 */ 1191 static 1192 void 1193 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx, 1194 union ccb *ccb) 1195 { 1196 struct ccb_hdr *ccbh; 1197 struct ccb_scsiio *csio; 1198 struct ata_xfer *xa; 1199 struct ata_fis_h2d *fis; 1200 scsi_cdb_t cdbs; 1201 scsi_cdb_t cdbd; 1202 int flags; 1203 struct ata_port *at; 1204 1205 ccbh = &ccb->csio.ccb_h; 1206 csio = &ccb->csio; 1207 at = atx ? atx : &ap->ap_ata[0]; 1208 1209 switch (ccbh->flags & CAM_DIR_MASK) { 1210 case CAM_DIR_IN: 1211 flags = ATA_F_PACKET | ATA_F_READ; 1212 break; 1213 case CAM_DIR_OUT: 1214 flags = ATA_F_PACKET | ATA_F_WRITE; 1215 break; 1216 case CAM_DIR_NONE: 1217 flags = ATA_F_PACKET; 1218 break; 1219 default: 1220 ccbh->status = CAM_REQ_INVALID; 1221 xpt_done(ccb); 1222 return; 1223 /* NOT REACHED */ 1224 } 1225 1226 /* 1227 * Special handling to get the rfis back into host memory while 1228 * still allowing the chip to run commands in parallel to 1229 * ATAPI devices behind a PM. 1230 */ 1231 flags |= ATA_F_AUTOSENSE; 1232 1233 /* 1234 * The command has to fit in the packet command buffer. 1235 */ 1236 if (csio->cdb_len < 6 || csio->cdb_len > 16) { 1237 ccbh->status = CAM_CCB_LEN_ERR; 1238 xpt_done(ccb); 1239 return; 1240 } 1241 1242 /* 1243 * Initialize the XA and FIS. It is unclear how much of 1244 * this has to mimic the equivalent ATA command. 1245 * 1246 * XXX not passing NULL at for direct attach! 1247 */ 1248 xa = ahci_ata_get_xfer(ap, atx); 1249 fis = xa->fis; 1250 1251 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 1252 fis->command = ATA_C_PACKET; 1253 fis->device = ATA_H2D_DEVICE_LBA; 1254 fis->sector_count = xa->tag << 3; 1255 if (flags & (ATA_F_READ | ATA_F_WRITE)) { 1256 if (flags & ATA_F_WRITE) { 1257 fis->features = ATA_H2D_FEATURES_DMA | 1258 ATA_H2D_FEATURES_DIR_WRITE; 1259 } else { 1260 fis->features = ATA_H2D_FEATURES_DMA | 1261 ATA_H2D_FEATURES_DIR_READ; 1262 } 1263 } else { 1264 fis->lba_mid = 0; 1265 fis->lba_high = 0; 1266 } 1267 fis->control = ATA_FIS_CONTROL_4BIT; 1268 1269 xa->flags = flags; 1270 xa->data = csio->data_ptr; 1271 xa->datalen = csio->dxfer_len; 1272 xa->timeout = ccbh->timeout; /* milliseconds */ 1273 1274 if (ccbh->flags & CAM_POLLED) 1275 xa->flags |= ATA_F_POLL; 1276 1277 /* 1278 * Copy the cdb to the packetcmd buffer in the FIS using a 1279 * convenient pointer in the xa. 1280 */ 1281 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ? 1282 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); 1283 bcopy(cdbs, xa->packetcmd, csio->cdb_len); 1284 1285 #if 0 1286 kprintf("opcode %d cdb_len %d dxfer_len %d\n", 1287 cdbs->generic.opcode, 1288 csio->cdb_len, csio->dxfer_len); 1289 #endif 1290 1291 /* 1292 * Some ATAPI commands do not actually follow the SCSI standard. 1293 */ 1294 cdbd = (void *)xa->packetcmd; 1295 1296 switch(cdbd->generic.opcode) { 1297 case REQUEST_SENSE: 1298 /* 1299 * Force SENSE requests to the ATAPI sense length. 1300 * 1301 * It is unclear if this is needed or not. 1302 */ 1303 if (cdbd->sense.length == SSD_FULL_SIZE) { 1304 kprintf("%s: Shortening sense request\n", 1305 PORTNAME(ap)); 1306 cdbd->sense.length = offsetof(struct scsi_sense_data, 1307 extra_bytes[0]); 1308 } 1309 break; 1310 case INQUIRY: 1311 /* 1312 * Some ATAPI devices can't handle long inquiry lengths, 1313 * don't ask me why. Truncate the inquiry length. 1314 */ 1315 if (cdbd->inquiry.page_code == 0 && 1316 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) { 1317 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH; 1318 } 1319 break; 1320 case READ_6: 1321 case WRITE_6: 1322 /* 1323 * Convert *_6 to *_10 commands. Most ATAPI devices 1324 * cannot handle the SCSI READ_6 and WRITE_6 commands. 1325 */ 1326 cdbd->rw_10.opcode |= 0x20; 1327 cdbd->rw_10.byte2 = 0; 1328 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F; 1329 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1]; 1330 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2]; 1331 cdbd->rw_10.addr[3] = 0; 1332 cdbd->rw_10.reserved = 0; 1333 cdbd->rw_10.length[0] = 0; 1334 cdbd->rw_10.length[1] = cdbs->rw_6.length; 1335 cdbd->rw_10.control = cdbs->rw_6.control; 1336 break; 1337 default: 1338 break; 1339 } 1340 1341 /* 1342 * And dispatch 1343 */ 1344 xa->complete = ahci_atapi_complete_cmd; 1345 xa->atascsi_private = ccb; 1346 ccb->ccb_h.sim_priv.entries[0].ptr = ap; 1347 ahci_os_lock_port(ap); 1348 ahci_ata_cmd(xa); 1349 ahci_os_unlock_port(ap); 1350 } 1351 1352 /* 1353 * Simulate page inquiries for disk attachments. 1354 */ 1355 static 1356 void 1357 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb) 1358 { 1359 union { 1360 struct scsi_vpd_supported_page_list list; 1361 struct scsi_vpd_unit_serial_number serno; 1362 struct scsi_vpd_unit_devid devid; 1363 char buf[256]; 1364 } *page; 1365 scsi_cdb_t cdb; 1366 int i; 1367 int j; 1368 int len; 1369 1370 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO); 1371 1372 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1373 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); 1374 1375 switch(cdb->inquiry.page_code) { 1376 case SVPD_SUPPORTED_PAGE_LIST: 1377 i = 0; 1378 page->list.device = T_DIRECT; 1379 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST; 1380 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST; 1381 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER; 1382 page->list.list[i++] = SVPD_UNIT_DEVID; 1383 page->list.length = i; 1384 len = offsetof(struct scsi_vpd_supported_page_list, list[3]); 1385 break; 1386 case SVPD_UNIT_SERIAL_NUMBER: 1387 i = 0; 1388 j = sizeof(at->at_identify.serial); 1389 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i) 1390 ; 1391 while (j > i && at->at_identify.serial[j-1] == ' ') 1392 --j; 1393 page->serno.device = T_DIRECT; 1394 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER; 1395 page->serno.length = j - i; 1396 bcopy(at->at_identify.serial + i, 1397 page->serno.serial_num, j - i); 1398 len = offsetof(struct scsi_vpd_unit_serial_number, 1399 serial_num[j-i]); 1400 break; 1401 case SVPD_UNIT_DEVID: 1402 /* fall through for now */ 1403 default: 1404 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 1405 len = 0; 1406 break; 1407 } 1408 if (ccb->ccb_h.status == CAM_REQ_INPROG) { 1409 if (len <= ccb->csio.dxfer_len) { 1410 ccb->ccb_h.status = CAM_REQ_CMP; 1411 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len); 1412 bcopy(page, ccb->csio.data_ptr, len); 1413 ccb->csio.resid = ccb->csio.dxfer_len - len; 1414 } else { 1415 ccb->ccb_h.status = CAM_CCB_LEN_ERR; 1416 } 1417 } 1418 kfree(page, M_DEVBUF); 1419 } 1420 1421 /* 1422 * Completion function for ATA_PORT_T_DISK cache synchronization. 1423 */ 1424 static 1425 void 1426 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa) 1427 { 1428 union ccb *ccb = xa->atascsi_private; 1429 struct ccb_hdr *ccbh = &ccb->ccb_h; 1430 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1431 1432 switch(xa->state) { 1433 case ATA_S_COMPLETE: 1434 ccbh->status = CAM_REQ_CMP; 1435 ccb->csio.scsi_status = SCSI_STATUS_OK; 1436 break; 1437 case ATA_S_ERROR: 1438 kprintf("%s: synchronize_cache: error\n", 1439 ATANAME(ap, xa->at)); 1440 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 1441 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1442 ahci_ata_dummy_sense(&ccb->csio.sense_data); 1443 break; 1444 case ATA_S_TIMEOUT: 1445 kprintf("%s: synchronize_cache: timeout\n", 1446 ATANAME(ap, xa->at)); 1447 ccbh->status = CAM_CMD_TIMEOUT; 1448 break; 1449 default: 1450 kprintf("%s: synchronize_cache: unknown state %d\n", 1451 ATANAME(ap, xa->at), xa->state); 1452 ccbh->status = CAM_REQ_CMP_ERR; 1453 break; 1454 } 1455 ahci_ata_put_xfer(xa); 1456 ahci_os_unlock_port(ap); 1457 xpt_done(ccb); 1458 ahci_os_lock_port(ap); 1459 } 1460 1461 /* 1462 * Completion function for ATA_PORT_T_DISK I/O 1463 */ 1464 static 1465 void 1466 ahci_ata_complete_disk_rw(struct ata_xfer *xa) 1467 { 1468 union ccb *ccb = xa->atascsi_private; 1469 struct ccb_hdr *ccbh = &ccb->ccb_h; 1470 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1471 1472 switch(xa->state) { 1473 case ATA_S_COMPLETE: 1474 ccbh->status = CAM_REQ_CMP; 1475 ccb->csio.scsi_status = SCSI_STATUS_OK; 1476 break; 1477 case ATA_S_ERROR: 1478 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at)); 1479 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; 1480 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1481 ahci_ata_dummy_sense(&ccb->csio.sense_data); 1482 break; 1483 case ATA_S_TIMEOUT: 1484 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at)); 1485 ccbh->status = CAM_CMD_TIMEOUT; 1486 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1487 ahci_ata_dummy_sense(&ccb->csio.sense_data); 1488 break; 1489 default: 1490 kprintf("%s: disk_rw: unknown state %d\n", 1491 ATANAME(ap, xa->at), xa->state); 1492 ccbh->status = CAM_REQ_CMP_ERR; 1493 break; 1494 } 1495 ccb->csio.resid = xa->resid; 1496 ahci_ata_put_xfer(xa); 1497 ahci_os_unlock_port(ap); 1498 xpt_done(ccb); 1499 ahci_os_lock_port(ap); 1500 } 1501 1502 /* 1503 * Completion function for ATA_PORT_T_ATAPI I/O 1504 * 1505 * Sense data is returned in the rfis. 1506 */ 1507 static 1508 void 1509 ahci_atapi_complete_cmd(struct ata_xfer *xa) 1510 { 1511 union ccb *ccb = xa->atascsi_private; 1512 struct ccb_hdr *ccbh = &ccb->ccb_h; 1513 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; 1514 scsi_cdb_t cdb; 1515 1516 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1517 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); 1518 1519 switch(xa->state) { 1520 case ATA_S_COMPLETE: 1521 ccbh->status = CAM_REQ_CMP; 1522 ccb->csio.scsi_status = SCSI_STATUS_OK; 1523 break; 1524 case ATA_S_ERROR: 1525 ccbh->status = CAM_SCSI_STATUS_ERROR; 1526 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1527 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data); 1528 break; 1529 case ATA_S_TIMEOUT: 1530 kprintf("%s: cmd %d: timeout\n", 1531 PORTNAME(ap), cdb->generic.opcode); 1532 ccbh->status = CAM_CMD_TIMEOUT; 1533 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1534 ahci_ata_dummy_sense(&ccb->csio.sense_data); 1535 break; 1536 default: 1537 kprintf("%s: cmd %d: unknown state %d\n", 1538 PORTNAME(ap), cdb->generic.opcode, xa->state); 1539 ccbh->status = CAM_REQ_CMP_ERR; 1540 break; 1541 } 1542 ccb->csio.resid = xa->resid; 1543 ahci_ata_put_xfer(xa); 1544 ahci_os_unlock_port(ap); 1545 xpt_done(ccb); 1546 ahci_os_lock_port(ap); 1547 } 1548 1549 /* 1550 * Construct dummy sense data for errors on DISKs 1551 */ 1552 static 1553 void 1554 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data) 1555 { 1556 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; 1557 sense_data->segment = 0; 1558 sense_data->flags = SSD_KEY_MEDIUM_ERROR; 1559 sense_data->info[0] = 0; 1560 sense_data->info[1] = 0; 1561 sense_data->info[2] = 0; 1562 sense_data->info[3] = 0; 1563 sense_data->extra_len = 0; 1564 } 1565 1566 /* 1567 * Construct atapi sense data for errors on ATAPI 1568 * 1569 * The ATAPI sense data is stored in the passed rfis and must be converted 1570 * to SCSI sense data. 1571 */ 1572 static 1573 void 1574 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, 1575 struct scsi_sense_data *sense_data) 1576 { 1577 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; 1578 sense_data->segment = 0; 1579 sense_data->flags = (rfis->error & 0xF0) >> 4; 1580 if (rfis->error & 0x04) 1581 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST; 1582 if (rfis->error & 0x02) 1583 sense_data->flags |= SSD_EOM; 1584 if (rfis->error & 0x01) 1585 sense_data->flags |= SSD_ILI; 1586 sense_data->info[0] = 0; 1587 sense_data->info[1] = 0; 1588 sense_data->info[2] = 0; 1589 sense_data->info[3] = 0; 1590 sense_data->extra_len = 0; 1591 } 1592 1593 static 1594 void 1595 ahci_strip_string(const char **basep, int *lenp) 1596 { 1597 const char *base = *basep; 1598 int len = *lenp; 1599 1600 while (len && (*base == 0 || *base == ' ')) { 1601 --len; 1602 ++base; 1603 } 1604 while (len && (base[len-1] == 0 || base[len-1] == ' ')) 1605 --len; 1606 *basep = base; 1607 *lenp = len; 1608 } 1609