1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 5 * 6 * This code is derived from software contributed to The DragonFly Project 7 * by Matthew Dillon <dillon@backplane.com> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 3. Neither the name of The DragonFly Project nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific, prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include "ahci.h" 38 39 static void ahci_pm_dummy_done(struct ata_xfer *xa); 40 41 int 42 ahci_pm_port_init(struct ahci_port *ap, struct ata_port *at) 43 { 44 at->at_probe = ATA_PROBE_NEED_HARD_RESET; 45 return (0); 46 } 47 48 /* 49 * AHCI port multiplier probe. This routine is run by the hardreset code 50 * if it gets past the device detect, whether or not BSY is found to be 51 * stuck. 52 * 53 * We MUST use CLO to properly probe whether the port multiplier exists 54 * or not. 55 * 56 * Return 0 on success, non-zero on failure. 57 */ 58 int 59 ahci_pm_port_probe(struct ahci_port *ap, int orig_error) 60 { 61 struct ahci_cmd_hdr *cmd_slot; 62 struct ata_port *at; 63 struct ahci_ccb *ccb = NULL; 64 u_int8_t *fis = NULL; 65 int error; 66 u_int32_t cmd; 67 int count; 68 int i; 69 70 count = 2; 71 retry: 72 /* 73 * This code is only called from hardreset, which does not 74 * high level command processing. The port should be stopped. 75 * 76 * Set PMA mode while the port is stopped. 77 * 78 * NOTE: On retry the port might be running, stopped, or failed. 79 */ 80 ahci_port_stop(ap, 0); 81 ap->ap_state = AP_S_NORMAL; 82 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 83 if ((cmd & AHCI_PREG_CMD_PMA) == 0) { 84 cmd |= AHCI_PREG_CMD_PMA; 85 ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 86 } 87 88 /* 89 * Flush any errors and request CLO unconditionally, then start 90 * the port. 91 */ 92 ahci_flush_tfd(ap); 93 ahci_port_clo(ap); 94 if (ahci_port_start(ap)) { 95 kprintf("%s: PMPROBE failed to start port, cannot softreset\n", 96 PORTNAME(ap)); 97 error = EIO; 98 goto err; 99 } 100 101 /* 102 * Check whether CLO worked 103 */ 104 if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 105 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 106 kprintf("%s: PMPROBE CLO %s, need port reset\n", 107 PORTNAME(ap), 108 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 109 ? "failed" : "unsupported"); 110 error = EBUSY; 111 goto err; 112 } 113 114 /* 115 * Use the error CCB for all commands 116 * 117 * NOTE! This CCB is used for both the first and second commands. 118 * The second command must use CCB slot 1 to properly load 119 * the signature. 120 */ 121 ccb = ahci_get_err_ccb(ap); 122 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_SILENT; 123 ccb->ccb_xa.complete = ahci_pm_dummy_done; 124 ccb->ccb_xa.at = ap->ap_ata[15]; 125 cmd_slot = ccb->ccb_cmd_hdr; 126 KKASSERT(ccb->ccb_slot == 1); 127 128 /* 129 * Prep the first H2D command with SRST feature & clear busy/reset 130 * flags. 131 */ 132 fis = ccb->ccb_cmd_table->cfis; 133 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 134 fis[0] = ATA_FIS_TYPE_H2D; 135 fis[1] = 0x0F; /* Target 15 */ 136 fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT; 137 138 cmd_slot->prdtl = 0; 139 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 140 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 141 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 142 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 143 144 ccb->ccb_xa.state = ATA_S_PENDING; 145 146 /* 147 * The only way one can determine if a port multiplier is on the 148 * port is to probe target 15, and of course this will fail if 149 * there is no port multiplier. 150 * 151 * The probing has to be done whether or not a device is probed on 152 * target 0, because when a PM is attached target 0 represents 153 * slot #0 behind the PM. 154 * 155 * Port multipliers are expected to answer more quickly than normal 156 * devices, use a shorter timeout than normal. 157 * 158 * If there is no PM here this command can still succeed due to 159 * the _C_ 160 */ 161 if (ahci_poll(ccb, 500, ahci_quick_timeout) != ATA_S_COMPLETE) { 162 kprintf("%s: PMPROBE(1) No Port Multiplier was found.\n", 163 PORTNAME(ap)); 164 if (--count) { 165 ahci_put_err_ccb(ccb); 166 goto retry; 167 } 168 error = EBUSY; 169 goto err; 170 } 171 172 if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 173 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 174 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap)); 175 } 176 177 /* 178 * The device may have muffed up the PHY when it reset. 179 */ 180 ahci_flush_tfd(ap); 181 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 182 /* ahci_pm_phy_status(ap, 15, &cmd); */ 183 184 /* 185 * Prep second D2H command to read status and complete reset sequence 186 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 187 * Rev 2.6 and it is unclear how the second FIS should be set up 188 * from the AHCI document. 189 * 190 * Give the device 3ms before sending the second FIS. 191 * 192 * It is unclear which other fields in the FIS are used. Just zero 193 * everything. 194 */ 195 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_SILENT; 196 197 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 198 fis[0] = ATA_FIS_TYPE_H2D; 199 fis[1] = 0x0F; 200 fis[15] = ATA_FIS_CONTROL_4BIT; 201 202 cmd_slot->prdtl = 0; 203 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 204 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 205 206 ccb->ccb_xa.state = ATA_S_PENDING; 207 208 /* 209 * The only way one can determine if a port multiplier is on the 210 * port is to probe target 15, and of course this will fail if 211 * there is no port multiplier. 212 * 213 * The probing has to be done whether or not a device is probed on 214 * target 0, because when a PM is attached target 0 represents 215 * slot #0 behind the PM. 216 */ 217 if (ahci_poll(ccb, 5000, ahci_quick_timeout) != ATA_S_COMPLETE) { 218 kprintf("%s: PMPROBE(2) No Port Multiplier was found.\n", 219 PORTNAME(ap)); 220 if (--count) { 221 ahci_put_err_ccb(ccb); 222 goto retry; 223 } 224 error = EBUSY; 225 goto err; 226 } 227 228 /* 229 * What? We succeeded? Yup, but for some reason the signature 230 * is still latched from the original detect (that saw target 0 231 * behind the PM), and I don't know how to clear the condition 232 * other then by retrying the whole reset sequence. 233 */ 234 if (--count) { 235 fis[15] = 0; 236 ahci_put_err_ccb(ccb); 237 goto retry; 238 } 239 240 /* 241 * Get the signature. The caller sets the ap fields. 242 */ 243 if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) { 244 ap->ap_ata[15]->at_probe = ATA_PROBE_GOOD; 245 error = 0; 246 } else { 247 error = EBUSY; 248 } 249 250 /* 251 * Fall through / clean up the CCB and perform error processing. 252 */ 253 err: 254 if (ccb != NULL) 255 ahci_put_err_ccb(ccb); 256 257 if (error == 0 && ahci_pm_identify(ap)) { 258 kprintf("%s: PM - cannot identify port multiplier\n", 259 PORTNAME(ap)); 260 error = EBUSY; 261 } 262 263 /* 264 * If we probed the PM reset the state for the targets behind 265 * it so they get probed by the state machine. 266 */ 267 if (error == 0) { 268 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 269 at = ap->ap_ata[i]; 270 at->at_probe = ATA_PROBE_NEED_INIT; 271 at->at_features |= ATA_PORT_F_RESCAN; 272 } 273 ap->ap_type = ATA_PORT_T_PM; 274 return (0); 275 } 276 277 /* 278 * If we failed turn off PMA, otherwise identify the port multiplier. 279 * CAM will iterate the devices. 280 */ 281 ahci_port_stop(ap, 0); 282 ahci_port_clo(ap); 283 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 284 cmd &= ~AHCI_PREG_CMD_PMA; 285 ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 286 if (orig_error == 0) { 287 if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 288 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 289 kprintf("%s: PM probe: port will not come ready\n", 290 PORTNAME(ap)); 291 orig_error = EBUSY; 292 ahci_port_init(ap); 293 } 294 } 295 return(orig_error); 296 } 297 298 /* 299 * Identify the port multiplier 300 */ 301 int 302 ahci_pm_identify(struct ahci_port *ap) 303 { 304 u_int32_t chipid; 305 u_int32_t rev; 306 u_int32_t nports; 307 u_int32_t data1; 308 u_int32_t data2; 309 int has_dummy_port; 310 311 ap->ap_probe = ATA_PROBE_FAILED; 312 if (ahci_pm_read(ap, 15, 0, &chipid)) 313 goto err; 314 if (ahci_pm_read(ap, 15, 1, &rev)) 315 goto err; 316 if (ahci_pm_read(ap, 15, 2, &nports)) 317 goto err; 318 nports &= 0x0000000F; /* only the low 4 bits */ 319 ap->ap_probe = ATA_PROBE_GOOD; 320 321 /* 322 * Ignore fake port on PMs which have it. We can probe it but the 323 * softreset will probably fail. 324 */ 325 switch(chipid) { 326 case 0x37261095: 327 has_dummy_port = 1; 328 break; 329 default: 330 has_dummy_port = 0; 331 break; 332 } 333 if (has_dummy_port) { 334 if (nports > 1) 335 --nports; 336 } 337 338 kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n", 339 PORTNAME(ap), 340 chipid, 341 rev, SATA_PFMT_PM_REV, 342 nports); 343 if (has_dummy_port) { 344 kprintf("%s: Port multiplier: Ignoring dummy port #%d\n", 345 PORTNAME(ap), nports); 346 } 347 ap->ap_pmcount = nports; 348 349 if (ahci_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) { 350 kprintf("%s: Port multiplier: Warning, " 351 "cannot read feature register\n", PORTNAME(ap)); 352 } else { 353 kprintf("%s: Port multiplier features: 0x%b\n", 354 PORTNAME(ap), 355 data1, 356 SATA_PFMT_PM_FEA); 357 } 358 if (ahci_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) { 359 kprintf("%s: Port multiplier defaults: 0x%b\n", 360 PORTNAME(ap), 361 data2, 362 SATA_PFMT_PM_FEA); 363 } 364 365 /* 366 * Turn on async notification if we support and the PM supports it. 367 * This allows the PM to forward async notification events to us and 368 * it will also generate an event for target 15 for hot-plug events 369 * (or is supposed to anyway). 370 */ 371 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) && 372 (data1 & SATA_PMFEA_ASYNCNOTIFY)) { 373 u_int32_t serr_bits = AHCI_PREG_SERR_DIAG_N | 374 AHCI_PREG_SERR_DIAG_X; 375 data2 |= SATA_PMFEA_ASYNCNOTIFY; 376 if (ahci_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) { 377 kprintf("%s: Port multiplier: AsyncNotify cannot be " 378 "enabled\n", PORTNAME(ap)); 379 } else if (ahci_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) { 380 kprintf("%s: Port mulltiplier: AsyncNotify unable " 381 "to enable error info bits\n", PORTNAME(ap)); 382 } else { 383 kprintf("%s: Port multiplier: AsyncNotify enabled\n", 384 PORTNAME(ap)); 385 } 386 } 387 388 return (0); 389 err: 390 kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap)); 391 return (EIO); 392 } 393 394 /* 395 * Do a COMRESET sequence on the target behind a port multiplier. 396 * 397 * If hard is 2 we also cycle the phy on the target. 398 * 399 * This must be done prior to any softreset or probe attempts on 400 * targets behind the port multiplier. 401 * 402 * Returns 0 on success or an error. 403 */ 404 int 405 ahci_pm_hardreset(struct ahci_port *ap, int target, int hard) 406 { 407 struct ata_port *at; 408 u_int32_t data; 409 int loop; 410 int error = EIO; 411 412 at = ap->ap_ata[target]; 413 414 /* 415 * Turn off power management and kill the phy on the target 416 * if requested. Hold state for 10ms. 417 */ 418 data = AHCI_PREG_SCTL_IPM_DISABLED; 419 if (hard == 2) 420 data |= AHCI_PREG_SCTL_DET_DISABLE; 421 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) 422 goto err; 423 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data)) 424 goto err; 425 ahci_os_sleep(10); 426 427 /* 428 * Start transmitting COMRESET. COMRESET must be sent for at 429 * least 1ms. 430 */ 431 at->at_probe = ATA_PROBE_FAILED; 432 at->at_type = ATA_PORT_T_NONE; 433 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 434 if (AhciForceGen1 & (1 << ap->ap_num)) { 435 kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target); 436 data |= AHCI_PREG_SCTL_SPD_GEN1; 437 } else { 438 data |= AHCI_PREG_SCTL_SPD_ANY; 439 } 440 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data)) 441 goto err; 442 443 /* 444 * It takes about 100ms for the DET logic to settle down, 445 * from trial and error testing. If this is too short 446 * the softreset code will fail. 447 */ 448 ahci_os_sleep(100); 449 450 if (ahci_pm_phy_status(ap, target, &data)) { 451 kprintf("%s: (A)Cannot clear phy status\n", 452 ATANAME(ap ,at)); 453 } 454 455 /* 456 * Flush any status, then clear DET to initiate negotiation. 457 */ 458 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 459 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_NONE; 460 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data)) 461 goto err; 462 463 /* 464 * Try to determine if there is a device on the port. 465 * 466 * Give the device 3/10 second to at least be detected. 467 * If we fail clear any pending status since we may have 468 * cycled the phy and probably caused another PRCS interrupt. 469 */ 470 for (loop = 3; loop; --loop) { 471 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) 472 goto err; 473 if (data & AHCI_PREG_SSTS_DET) 474 break; 475 ahci_os_sleep(100); 476 } 477 if (loop == 0) { 478 kprintf("%s.%d: Port appears to be unplugged\n", 479 PORTNAME(ap), target); 480 error = ENODEV; 481 goto err; 482 } 483 484 /* 485 * There is something on the port. Give the device 3 seconds 486 * to fully negotiate. 487 */ 488 for (loop = 30; loop; --loop) { 489 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) 490 goto err; 491 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV) 492 break; 493 ahci_os_sleep(100); 494 } 495 496 /* 497 * Device not detected 498 */ 499 if (loop == 0) { 500 kprintf("%s: Device may be powered down\n", 501 PORTNAME(ap)); 502 error = ENODEV; 503 goto err; 504 } 505 506 /* 507 * Device detected 508 */ 509 kprintf("%s.%d: Device detected data=%08x\n", 510 PORTNAME(ap), target, data); 511 /* 512 * Clear SERR on the target so we get a new NOTIFY event if a hot-plug 513 * or hot-unplug occurs. Clear any spurious IFS that may have 514 * occured during the probe. 515 * 516 * WARNING! 100ms seems to work in most cases but 517 */ 518 ahci_os_sleep(100); 519 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 520 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 521 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 522 523 error = 0; 524 err: 525 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET; 526 return (error); 527 } 528 529 /* 530 * AHCI soft reset through port multiplier. 531 * 532 * This function keeps port communications intact and attempts to generate 533 * a reset to the connected device using device commands. Unlike 534 * hard-port operations we can't do fancy stop/starts or stuff like 535 * that without messing up other commands that might be running or 536 * queued. 537 */ 538 int 539 ahci_pm_softreset(struct ahci_port *ap, int target) 540 { 541 struct ata_port *at; 542 struct ahci_ccb *ccb; 543 struct ahci_cmd_hdr *cmd_slot; 544 u_int8_t *fis; 545 int count; 546 int error; 547 u_int32_t data; 548 int tried_longer; 549 550 error = EIO; 551 at = ap->ap_ata[target]; 552 553 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 554 555 count = 2; 556 tried_longer = 0; 557 retry: 558 /* 559 * Try to clear the phy so we get a good signature, otherwise 560 * the PM may not latch a new signature. 561 * 562 * NOTE: This cannot be safely done between the first and second 563 * softreset FISs. It's now or never. 564 */ 565 if (ahci_pm_phy_status(ap, target, &data)) { 566 kprintf("%s: (B)Cannot clear phy status\n", 567 ATANAME(ap ,at)); 568 } 569 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 570 571 /* 572 * Prep first D2H command with SRST feature & clear busy/reset flags 573 * 574 * It is unclear which other fields in the FIS are used. Just zero 575 * everything. 576 * 577 * When soft-resetting a port behind a multiplier at will be 578 * non-NULL, assigning it to the ccb prevents the port interrupt 579 * from hard-resetting the port if a problem crops up. 580 */ 581 ccb = ahci_get_err_ccb(ap); 582 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE; 583 ccb->ccb_xa.complete = ahci_pm_dummy_done; 584 ccb->ccb_xa.at = at; 585 586 fis = ccb->ccb_cmd_table->cfis; 587 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 588 fis[0] = ATA_FIS_TYPE_H2D; 589 fis[1] = at->at_target; 590 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 591 592 cmd_slot = ccb->ccb_cmd_hdr; 593 cmd_slot->prdtl = 0; 594 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 595 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 596 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 597 cmd_slot->flags |= htole16(at->at_target << 598 AHCI_CMD_LIST_FLAG_PMP_SHIFT); 599 600 ccb->ccb_xa.state = ATA_S_PENDING; 601 602 /* 603 * This soft reset of the AP target can cause a stream of IFS 604 * errors to occur. Setting AP_F_IGNORE_IFS prevents the port 605 * from being hard reset (because its the target behind the 606 * port that isn't happy). 607 * 608 * The act of sending the soft reset can cause the target to 609 * blow the port up and generate IFS errors. 610 */ 611 ap->ap_flags |= AP_F_IGNORE_IFS; 612 ap->ap_flags &= ~AP_F_IFS_IGNORED; 613 614 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) { 615 kprintf("%s: Soft-reset through PM failed, %s\n", 616 ATANAME(ap, at), 617 (count > 1 ? "retrying" : "giving up")); 618 ahci_put_err_ccb(ccb); 619 if (--count) { 620 if (ap->ap_flags & AP_F_IFS_IGNORED) 621 ahci_os_sleep(5000); 622 else 623 ahci_os_sleep(1000); 624 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 625 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 626 goto retry; 627 } 628 goto err; 629 } 630 631 /* 632 * WARNING! SENSITIVE TIME PERIOD! WARNING! 633 * 634 * The first and second FISes are supposed to be back-to-back, 635 * I think the idea is to get the second sent and then after 636 * the device resets it will send a signature. Do not delay 637 * here and most definitely do not issue any commands to other 638 * targets! 639 */ 640 641 /* 642 * Prep second D2H command to read status and complete reset sequence 643 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 644 * Rev 2.6 and it is unclear how the second FIS should be set up 645 * from the AHCI document. 646 * 647 * Give the device 3ms before sending the second FIS. 648 * 649 * It is unclear which other fields in the FIS are used. Just zero 650 * everything. 651 */ 652 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 653 fis[0] = ATA_FIS_TYPE_H2D; 654 fis[1] = at->at_target; 655 fis[15] = ATA_FIS_CONTROL_4BIT; 656 657 cmd_slot->prdtl = 0; 658 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 659 cmd_slot->flags |= htole16(at->at_target << 660 AHCI_CMD_LIST_FLAG_PMP_SHIFT); 661 662 ccb->ccb_xa.state = ATA_S_PENDING; 663 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE; 664 665 ap->ap_flags &= ~AP_F_IFS_IGNORED; 666 667 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) { 668 kprintf("%s: Soft-reset(2) through PM failed, %s\n", 669 ATANAME(ap, at), 670 (count > 1 ? "retrying" : "giving up")); 671 if (--count) { 672 ahci_os_sleep(1000); 673 ahci_put_err_ccb(ccb); 674 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 675 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 676 goto retry; 677 } 678 goto err; 679 } 680 681 ahci_put_err_ccb(ccb); 682 ahci_os_sleep(100); 683 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 684 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 685 686 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 687 if (ahci_pm_phy_status(ap, target, &data)) { 688 kprintf("%s: (C)Cannot clear phy status\n", 689 ATANAME(ap ,at)); 690 } 691 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 692 693 /* 694 * Do it again, even if we think we got a good result 695 */ 696 if (--count) { 697 fis[15] = 0; 698 goto retry; 699 } 700 701 /* 702 * If the softreset is trying to clear a BSY condition after a 703 * normal portreset we assign the port type. 704 * 705 * If the softreset is being run first as part of the ccb error 706 * processing code then report if the device signature changed 707 * unexpectedly. 708 */ 709 if (at->at_type == ATA_PORT_T_NONE) { 710 at->at_type = ahci_port_signature_detect(ap, at); 711 } else { 712 if (ahci_port_signature_detect(ap, at) != at->at_type) { 713 kprintf("%s: device signature unexpectedly " 714 "changed\n", ATANAME(ap, at)); 715 error = EBUSY; /* XXX */ 716 } 717 } 718 error = 0; 719 720 /* 721 * Who knows what kind of mess occured. We have exclusive access 722 * to the port so try to clean up potential problems. 723 */ 724 err: 725 ahci_os_sleep(100); 726 727 /* 728 * Clear error status so we can detect removal. 729 */ 730 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) { 731 kprintf("%s: ahci_pm_softreset unable to clear SERR\n", 732 ATANAME(ap, at)); 733 } 734 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 735 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 736 ap->ap_flags &= ~(AP_F_IGNORE_IFS | AP_F_IFS_IGNORED); 737 738 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT; 739 return (error); 740 } 741 742 743 /* 744 * Return the phy status for a target behind a port multiplier and 745 * reset SATA_PMREG_SERR. 746 * 747 * Returned bits follow AHCI_PREG_SSTS bits. The AHCI_PREG_SSTS_SPD 748 * bits can be used to determine the link speed and will be 0 if there 749 * is no link. 750 * 751 * 0 is returned if any communications error occurs. 752 */ 753 int 754 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap) 755 { 756 int error; 757 758 error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap); 759 if (error == 0) 760 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 761 if (error) 762 *datap = 0; 763 return(error); 764 } 765 766 /* 767 * Check that a target is still good. 768 */ 769 void 770 ahci_pm_check_good(struct ahci_port *ap, int target) 771 { 772 struct ata_port *at; 773 u_int32_t data; 774 775 /* 776 * It looks like we might have to read the EINFO register 777 * to allow the PM to generate a new event. 778 */ 779 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) { 780 kprintf("%s: Port multiplier EINFO could not be read\n", 781 PORTNAME(ap)); 782 } 783 784 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) { 785 kprintf("%s: Port multiplier: SERR could not be cleared\n", 786 PORTNAME(ap)); 787 } 788 789 if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount) 790 return; 791 at = ap->ap_ata[target]; 792 793 /* 794 * If the device needs an init or hard reset also make sure the 795 * PHY is turned on. 796 */ 797 if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) { 798 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/ 799 ahci_pm_hardreset(ap, target, 1); 800 } 801 802 /* 803 * Read the detect status 804 */ 805 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) { 806 kprintf("%s: Unable to access PM SSTS register target %d\n", 807 PORTNAME(ap), target); 808 return; 809 } 810 if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) { 811 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/ 812 if (at->at_probe != ATA_PROBE_FAILED) { 813 at->at_probe = ATA_PROBE_FAILED; 814 at->at_type = ATA_PORT_T_NONE; 815 at->at_features |= ATA_PORT_F_RESCAN; 816 kprintf("%s: HOTPLUG (PM) - Device removed\n", 817 ATANAME(ap, at)); 818 } 819 } else { 820 if (at->at_probe == ATA_PROBE_FAILED) { 821 at->at_probe = ATA_PROBE_NEED_HARD_RESET; 822 at->at_features |= ATA_PORT_F_RESCAN; 823 kprintf("%s: HOTPLUG (PM) - Device inserted\n", 824 ATANAME(ap, at)); 825 } 826 } 827 } 828 829 /* 830 * Read a PM register 831 */ 832 int 833 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap) 834 { 835 struct ata_xfer *xa; 836 int error; 837 838 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]); 839 840 xa->fis->type = ATA_FIS_TYPE_H2D; 841 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15; 842 xa->fis->command = ATA_C_READ_PM; 843 xa->fis->features = which; 844 xa->fis->device = target | ATA_H2D_DEVICE_LBA; 845 xa->fis->control = ATA_FIS_CONTROL_4BIT; 846 847 xa->complete = ahci_pm_dummy_done; 848 xa->datalen = 0; 849 xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE; 850 xa->timeout = 1000; 851 852 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) { 853 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) | 854 (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24); 855 error = 0; 856 } else { 857 kprintf("%s.%d pm_read SCA[%d] failed\n", 858 PORTNAME(ap), target, which); 859 *datap = 0; 860 error = EIO; 861 } 862 ahci_ata_put_xfer(xa); 863 return (error); 864 } 865 866 /* 867 * Write a PM register 868 */ 869 int 870 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data) 871 { 872 struct ata_xfer *xa; 873 int error; 874 875 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]); 876 877 xa->fis->type = ATA_FIS_TYPE_H2D; 878 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15; 879 xa->fis->command = ATA_C_WRITE_PM; 880 xa->fis->features = which; 881 xa->fis->device = target | ATA_H2D_DEVICE_LBA; 882 xa->fis->sector_count = (u_int8_t)data; 883 xa->fis->lba_low = (u_int8_t)(data >> 8); 884 xa->fis->lba_mid = (u_int8_t)(data >> 16); 885 xa->fis->lba_high = (u_int8_t)(data >> 24); 886 xa->fis->control = ATA_FIS_CONTROL_4BIT; 887 888 xa->complete = ahci_pm_dummy_done; 889 xa->datalen = 0; 890 xa->flags = ATA_F_POLL; 891 xa->timeout = 1000; 892 893 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 894 error = 0; 895 else 896 error = EIO; 897 ahci_ata_put_xfer(xa); 898 return(error); 899 } 900 901 /* 902 * Dummy done callback for xa. 903 */ 904 static void 905 ahci_pm_dummy_done(struct ata_xfer *xa) 906 { 907 } 908 909