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 switch(AhciForceGen) { 435 case 0: 436 data |= AHCI_PREG_SCTL_SPD_ANY; 437 break; 438 case 1: 439 data |= AHCI_PREG_SCTL_SPD_GEN1; 440 break; 441 case 2: 442 data |= AHCI_PREG_SCTL_SPD_GEN2; 443 break; 444 case 3: 445 data |= AHCI_PREG_SCTL_SPD_GEN3; 446 break; 447 default: 448 data |= AHCI_PREG_SCTL_SPD_GEN3; 449 break; 450 } 451 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data)) 452 goto err; 453 454 /* 455 * It takes about 100ms for the DET logic to settle down, 456 * from trial and error testing. If this is too short 457 * the softreset code will fail. 458 */ 459 ahci_os_sleep(100); 460 461 if (ahci_pm_phy_status(ap, target, &data)) { 462 kprintf("%s: (A)Cannot clear phy status\n", 463 ATANAME(ap ,at)); 464 } 465 466 /* 467 * Flush any status, then clear DET to initiate negotiation. 468 */ 469 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 470 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_NONE; 471 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data)) 472 goto err; 473 474 /* 475 * Try to determine if there is a device on the port. 476 * 477 * Give the device 3/10 second to at least be detected. 478 * If we fail clear any pending status since we may have 479 * cycled the phy and probably caused another PRCS interrupt. 480 */ 481 for (loop = 3; loop; --loop) { 482 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) 483 goto err; 484 if (data & AHCI_PREG_SSTS_DET) 485 break; 486 ahci_os_sleep(100); 487 } 488 if (loop == 0) { 489 kprintf("%s.%d: Port appears to be unplugged\n", 490 PORTNAME(ap), target); 491 error = ENODEV; 492 goto err; 493 } 494 495 /* 496 * There is something on the port. Give the device 3 seconds 497 * to fully negotiate. 498 */ 499 for (loop = 30; loop; --loop) { 500 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) 501 goto err; 502 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV) 503 break; 504 ahci_os_sleep(100); 505 } 506 507 /* 508 * Device not detected 509 */ 510 if (loop == 0) { 511 kprintf("%s: Device may be powered down\n", 512 PORTNAME(ap)); 513 error = ENODEV; 514 goto err; 515 } 516 517 /* 518 * Device detected 519 */ 520 kprintf("%s.%d: Device detected data=%08x\n", 521 PORTNAME(ap), target, data); 522 /* 523 * Clear SERR on the target so we get a new NOTIFY event if a hot-plug 524 * or hot-unplug occurs. Clear any spurious IFS that may have 525 * occured during the probe. 526 * 527 * WARNING! 100ms seems to work in most cases but 528 */ 529 ahci_os_sleep(100); 530 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 531 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 532 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 533 534 error = 0; 535 err: 536 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET; 537 return (error); 538 } 539 540 /* 541 * AHCI soft reset through port multiplier. 542 * 543 * This function keeps port communications intact and attempts to generate 544 * a reset to the connected device using device commands. Unlike 545 * hard-port operations we can't do fancy stop/starts or stuff like 546 * that without messing up other commands that might be running or 547 * queued. 548 */ 549 int 550 ahci_pm_softreset(struct ahci_port *ap, int target) 551 { 552 struct ata_port *at; 553 struct ahci_ccb *ccb; 554 struct ahci_cmd_hdr *cmd_slot; 555 u_int8_t *fis; 556 int count; 557 int error; 558 u_int32_t data; 559 int tried_longer; 560 561 error = EIO; 562 at = ap->ap_ata[target]; 563 564 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 565 566 count = 2; 567 tried_longer = 0; 568 retry: 569 /* 570 * Try to clear the phy so we get a good signature, otherwise 571 * the PM may not latch a new signature. 572 * 573 * NOTE: This cannot be safely done between the first and second 574 * softreset FISs. It's now or never. 575 */ 576 if (ahci_pm_phy_status(ap, target, &data)) { 577 kprintf("%s: (B)Cannot clear phy status\n", 578 ATANAME(ap ,at)); 579 } 580 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 581 582 /* 583 * Prep first D2H command with SRST feature & clear busy/reset flags 584 * 585 * It is unclear which other fields in the FIS are used. Just zero 586 * everything. 587 * 588 * When soft-resetting a port behind a multiplier at will be 589 * non-NULL, assigning it to the ccb prevents the port interrupt 590 * from hard-resetting the port if a problem crops up. 591 */ 592 ccb = ahci_get_err_ccb(ap); 593 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE; 594 ccb->ccb_xa.complete = ahci_pm_dummy_done; 595 ccb->ccb_xa.at = at; 596 597 fis = ccb->ccb_cmd_table->cfis; 598 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 599 fis[0] = ATA_FIS_TYPE_H2D; 600 fis[1] = at->at_target; 601 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 602 603 cmd_slot = ccb->ccb_cmd_hdr; 604 cmd_slot->prdtl = 0; 605 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 606 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 607 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 608 cmd_slot->flags |= htole16(at->at_target << 609 AHCI_CMD_LIST_FLAG_PMP_SHIFT); 610 611 ccb->ccb_xa.state = ATA_S_PENDING; 612 613 /* 614 * This soft reset of the AP target can cause a stream of IFS 615 * errors to occur. Setting AP_F_IGNORE_IFS prevents the port 616 * from being hard reset (because its the target behind the 617 * port that isn't happy). 618 * 619 * The act of sending the soft reset can cause the target to 620 * blow the port up and generate IFS errors. 621 */ 622 ap->ap_flags |= AP_F_IGNORE_IFS; 623 ap->ap_flags &= ~AP_F_IFS_IGNORED; 624 625 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) { 626 kprintf("%s: Soft-reset through PM failed, %s\n", 627 ATANAME(ap, at), 628 (count > 1 ? "retrying" : "giving up")); 629 ahci_put_err_ccb(ccb); 630 if (--count) { 631 if (ap->ap_flags & AP_F_IFS_IGNORED) 632 ahci_os_sleep(5000); 633 else 634 ahci_os_sleep(1000); 635 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 636 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 637 goto retry; 638 } 639 goto err; 640 } 641 642 /* 643 * WARNING! SENSITIVE TIME PERIOD! WARNING! 644 * 645 * The first and second FISes are supposed to be back-to-back, 646 * I think the idea is to get the second sent and then after 647 * the device resets it will send a signature. Do not delay 648 * here and most definitely do not issue any commands to other 649 * targets! 650 */ 651 652 /* 653 * Prep second D2H command to read status and complete reset sequence 654 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 655 * Rev 2.6 and it is unclear how the second FIS should be set up 656 * from the AHCI document. 657 * 658 * Give the device 3ms before sending the second FIS. 659 * 660 * It is unclear which other fields in the FIS are used. Just zero 661 * everything. 662 */ 663 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 664 fis[0] = ATA_FIS_TYPE_H2D; 665 fis[1] = at->at_target; 666 fis[15] = ATA_FIS_CONTROL_4BIT; 667 668 cmd_slot->prdtl = 0; 669 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 670 cmd_slot->flags |= htole16(at->at_target << 671 AHCI_CMD_LIST_FLAG_PMP_SHIFT); 672 673 ccb->ccb_xa.state = ATA_S_PENDING; 674 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE; 675 676 ap->ap_flags &= ~AP_F_IFS_IGNORED; 677 678 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) { 679 kprintf("%s: Soft-reset(2) through PM failed, %s\n", 680 ATANAME(ap, at), 681 (count > 1 ? "retrying" : "giving up")); 682 if (--count) { 683 ahci_os_sleep(1000); 684 ahci_put_err_ccb(ccb); 685 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 686 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 687 goto retry; 688 } 689 goto err; 690 } 691 692 ahci_put_err_ccb(ccb); 693 ahci_os_sleep(100); 694 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 695 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 696 697 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 698 if (ahci_pm_phy_status(ap, target, &data)) { 699 kprintf("%s: (C)Cannot clear phy status\n", 700 ATANAME(ap ,at)); 701 } 702 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 703 704 /* 705 * Do it again, even if we think we got a good result 706 */ 707 if (--count) { 708 fis[15] = 0; 709 goto retry; 710 } 711 712 /* 713 * If the softreset is trying to clear a BSY condition after a 714 * normal portreset we assign the port type. 715 * 716 * If the softreset is being run first as part of the ccb error 717 * processing code then report if the device signature changed 718 * unexpectedly. 719 */ 720 if (at->at_type == ATA_PORT_T_NONE) { 721 at->at_type = ahci_port_signature_detect(ap, at); 722 } else { 723 if (ahci_port_signature_detect(ap, at) != at->at_type) { 724 kprintf("%s: device signature unexpectedly " 725 "changed\n", ATANAME(ap, at)); 726 error = EBUSY; /* XXX */ 727 } 728 } 729 error = 0; 730 731 /* 732 * Who knows what kind of mess occured. We have exclusive access 733 * to the port so try to clean up potential problems. 734 */ 735 err: 736 ahci_os_sleep(100); 737 738 /* 739 * Clear error status so we can detect removal. 740 */ 741 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) { 742 kprintf("%s: ahci_pm_softreset unable to clear SERR\n", 743 ATANAME(ap, at)); 744 } 745 ahci_pwrite(ap, AHCI_PREG_SERR, -1); 746 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 747 ap->ap_flags &= ~(AP_F_IGNORE_IFS | AP_F_IFS_IGNORED); 748 749 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT; 750 return (error); 751 } 752 753 754 /* 755 * Return the phy status for a target behind a port multiplier and 756 * reset SATA_PMREG_SERR. 757 * 758 * Returned bits follow AHCI_PREG_SSTS bits. The AHCI_PREG_SSTS_SPD 759 * bits can be used to determine the link speed and will be 0 if there 760 * is no link. 761 * 762 * 0 is returned if any communications error occurs. 763 */ 764 int 765 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap) 766 { 767 int error; 768 769 error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap); 770 if (error == 0) 771 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1); 772 if (error) 773 *datap = 0; 774 return(error); 775 } 776 777 /* 778 * Check that a target is still good. 779 */ 780 void 781 ahci_pm_check_good(struct ahci_port *ap, int target) 782 { 783 struct ata_port *at; 784 u_int32_t data; 785 786 /* 787 * It looks like we might have to read the EINFO register 788 * to allow the PM to generate a new event. 789 */ 790 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) { 791 kprintf("%s: Port multiplier EINFO could not be read\n", 792 PORTNAME(ap)); 793 } 794 795 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) { 796 kprintf("%s: Port multiplier: SERR could not be cleared\n", 797 PORTNAME(ap)); 798 } 799 800 if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount) 801 return; 802 at = ap->ap_ata[target]; 803 804 /* 805 * If the device needs an init or hard reset also make sure the 806 * PHY is turned on. 807 */ 808 if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) { 809 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/ 810 ahci_pm_hardreset(ap, target, 1); 811 } 812 813 /* 814 * Read the detect status 815 */ 816 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) { 817 kprintf("%s: Unable to access PM SSTS register target %d\n", 818 PORTNAME(ap), target); 819 return; 820 } 821 if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) { 822 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/ 823 if (at->at_probe != ATA_PROBE_FAILED) { 824 at->at_probe = ATA_PROBE_FAILED; 825 at->at_type = ATA_PORT_T_NONE; 826 at->at_features |= ATA_PORT_F_RESCAN; 827 kprintf("%s: HOTPLUG (PM) - Device removed\n", 828 ATANAME(ap, at)); 829 } 830 } else { 831 if (at->at_probe == ATA_PROBE_FAILED) { 832 at->at_probe = ATA_PROBE_NEED_HARD_RESET; 833 at->at_features |= ATA_PORT_F_RESCAN; 834 kprintf("%s: HOTPLUG (PM) - Device inserted\n", 835 ATANAME(ap, at)); 836 } 837 } 838 } 839 840 /* 841 * Read a PM register 842 */ 843 int 844 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap) 845 { 846 struct ata_xfer *xa; 847 int error; 848 849 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]); 850 851 xa->fis->type = ATA_FIS_TYPE_H2D; 852 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15; 853 xa->fis->command = ATA_C_READ_PM; 854 xa->fis->features = which; 855 xa->fis->device = target | ATA_H2D_DEVICE_LBA; 856 xa->fis->control = ATA_FIS_CONTROL_4BIT; 857 858 xa->complete = ahci_pm_dummy_done; 859 xa->datalen = 0; 860 xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE; 861 xa->timeout = 1000; 862 863 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) { 864 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) | 865 (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24); 866 error = 0; 867 } else { 868 kprintf("%s.%d pm_read SCA[%d] failed\n", 869 PORTNAME(ap), target, which); 870 *datap = 0; 871 error = EIO; 872 } 873 ahci_ata_put_xfer(xa); 874 return (error); 875 } 876 877 /* 878 * Write a PM register 879 */ 880 int 881 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data) 882 { 883 struct ata_xfer *xa; 884 int error; 885 886 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]); 887 888 xa->fis->type = ATA_FIS_TYPE_H2D; 889 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15; 890 xa->fis->command = ATA_C_WRITE_PM; 891 xa->fis->features = which; 892 xa->fis->device = target | ATA_H2D_DEVICE_LBA; 893 xa->fis->sector_count = (u_int8_t)data; 894 xa->fis->lba_low = (u_int8_t)(data >> 8); 895 xa->fis->lba_mid = (u_int8_t)(data >> 16); 896 xa->fis->lba_high = (u_int8_t)(data >> 24); 897 xa->fis->control = ATA_FIS_CONTROL_4BIT; 898 899 xa->complete = ahci_pm_dummy_done; 900 xa->datalen = 0; 901 xa->flags = ATA_F_POLL; 902 xa->timeout = 1000; 903 904 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 905 error = 0; 906 else 907 error = EIO; 908 ahci_ata_put_xfer(xa); 909 return(error); 910 } 911 912 /* 913 * Dummy done callback for xa. 914 */ 915 static void 916 ahci_pm_dummy_done(struct ata_xfer *xa) 917 { 918 } 919 920