4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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
39 static void ahci_pm_dummy_done(struct ata_xfer *xa);
42 ahci_pm_port_init(struct ahci_port *ap, struct ata_port *at)
44 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
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
53 * We MUST use CLO to properly probe whether the port multiplier exists
56 * Return 0 on success, non-zero on failure.
59 ahci_pm_port_probe(struct ahci_port *ap, int orig_error)
61 struct ahci_cmd_hdr *cmd_slot;
63 struct ahci_ccb *ccb = NULL;
74 * This code is only called from hardreset, which does not
75 * high level command processing. The port should be stopped.
77 * Set PMA mode while the port is stopped.
79 * NOTE: On retry the port might be running, stopped, or failed.
81 ahci_port_stop(ap, 0);
82 ap->ap_state = AP_S_NORMAL;
83 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
84 if ((cmd & AHCI_PREG_CMD_PMA) == 0) {
85 cmd |= AHCI_PREG_CMD_PMA;
86 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
90 * Try to enable FIS-Based switching. We have to probe PREG_FBS
92 * XXX I'm still trying to find an AHCI chipset that actually
93 * supports FBSS so I can finish implementing support, so
94 * this doesn't do a whole lot right now.
96 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_FBSS) {
97 const char *str1 = "";
98 const char *str2 = "";
99 const char *str3 = "";
101 if (cmd & AHCI_PREG_CMD_FBSCP) {
102 str1 = "HW indicates support";
103 } else if (ap->ap_sc->sc_flags & AHCI_F_FORCE_FBSS) {
104 cmd |= AHCI_PREG_CMD_FBSCP; /* Force on */
105 str1 = "HW indicates no-support, force";
107 str1 = "HW indicates no-support";
109 if (cmd & AHCI_PREG_CMD_FBSCP) {
110 fbs = ahci_pread(ap, AHCI_PREG_FBS);
111 ahci_pwrite(ap, AHCI_PREG_FBS, fbs | AHCI_PREG_FBS_EN);
112 fbs = ahci_pread(ap, AHCI_PREG_FBS);
113 if (fbs & AHCI_PREG_FBS_EN) {
114 str2 = ", enable succeeded";
115 str3 = ", (driver support not yet implemented)";
117 str2 = ", enable failed";
119 ahci_pwrite(ap, AHCI_PREG_FBS, fbs & ~AHCI_PREG_FBS_EN);
121 kprintf("%s: Port multiplier: FIS-Based Sw: %s%s%s\n",
122 PORTNAME(ap), str1, str2, str3);
126 * Flush any errors and request CLO unconditionally, then start
131 if (ahci_port_start(ap)) {
132 kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
139 * Check whether CLO worked
141 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
142 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
143 kprintf("%s: PMPROBE CLO %s, need port reset\n",
145 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
146 ? "failed" : "unsupported");
152 * Use the error CCB for all commands
154 * NOTE! This CCB is used for both the first and second commands.
155 * The second command must use CCB slot 1 to properly load
158 ccb = ahci_get_err_ccb(ap);
159 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_SILENT;
160 ccb->ccb_xa.complete = ahci_pm_dummy_done;
161 ccb->ccb_xa.at = ap->ap_ata[15];
162 cmd_slot = ccb->ccb_cmd_hdr;
163 KKASSERT(ccb->ccb_slot == 1);
166 * Prep the first H2D command with SRST feature & clear busy/reset
169 fis = ccb->ccb_cmd_table->cfis;
170 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
171 fis[0] = ATA_FIS_TYPE_H2D;
172 fis[1] = 0x0F; /* Target 15 */
173 fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT;
176 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
177 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
178 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
179 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
181 ccb->ccb_xa.state = ATA_S_PENDING;
184 * The only way one can determine if a port multiplier is on the
185 * port is to probe target 15, and of course this will fail if
186 * there is no port multiplier.
188 * The probing has to be done whether or not a device is probed on
189 * target 0, because when a PM is attached target 0 represents
190 * slot #0 behind the PM.
192 * Port multipliers are expected to answer more quickly than normal
193 * devices, use a shorter timeout than normal.
195 * If there is no PM here this command can still succeed due to
198 if (ahci_poll(ccb, 500, ahci_quick_timeout) != ATA_S_COMPLETE) {
199 kprintf("%s: PMPROBE(1) No Port Multiplier was found.\n",
202 ahci_put_err_ccb(ccb);
209 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
210 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
211 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
215 * The device may have muffed up the PHY when it reset.
218 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
219 /* ahci_pm_phy_status(ap, 15, &cmd); */
222 * Prep second D2H command to read status and complete reset sequence
223 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
224 * Rev 2.6 and it is unclear how the second FIS should be set up
225 * from the AHCI document.
227 * Give the device 3ms before sending the second FIS.
229 * It is unclear which other fields in the FIS are used. Just zero
232 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_SILENT;
234 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
235 fis[0] = ATA_FIS_TYPE_H2D;
237 fis[15] = ATA_FIS_CONTROL_4BIT;
240 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
241 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
243 ccb->ccb_xa.state = ATA_S_PENDING;
246 * The only way one can determine if a port multiplier is on the
247 * port is to probe target 15, and of course this will fail if
248 * there is no port multiplier.
250 * The probing has to be done whether or not a device is probed on
251 * target 0, because when a PM is attached target 0 represents
252 * slot #0 behind the PM.
254 if (ahci_poll(ccb, 5000, ahci_quick_timeout) != ATA_S_COMPLETE) {
255 kprintf("%s: PMPROBE(2) No Port Multiplier was found.\n",
258 ahci_put_err_ccb(ccb);
266 * What? We succeeded? Yup, but for some reason the signature
267 * is still latched from the original detect (that saw target 0
268 * behind the PM), and I don't know how to clear the condition
269 * other then by retrying the whole reset sequence.
273 ahci_put_err_ccb(ccb);
278 * Get the signature. The caller sets the ap fields.
280 if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
281 ap->ap_ata[15]->at_probe = ATA_PROBE_GOOD;
288 * Fall through / clean up the CCB and perform error processing.
292 ahci_put_err_ccb(ccb);
294 if (error == 0 && ahci_pm_identify(ap)) {
295 kprintf("%s: PM - cannot identify port multiplier\n",
301 * If we probed the PM reset the state for the targets behind
302 * it so they get probed by the state machine.
305 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
307 at->at_probe = ATA_PROBE_NEED_INIT;
308 at->at_features |= ATA_PORT_F_RESCAN;
310 ap->ap_type = ATA_PORT_T_PM;
315 * If we failed turn off PMA, otherwise identify the port multiplier.
316 * CAM will iterate the devices.
318 ahci_port_stop(ap, 0);
320 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
321 cmd &= ~AHCI_PREG_CMD_PMA;
322 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
323 if (orig_error == 0) {
324 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
325 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
326 kprintf("%s: PM probe: port will not come ready\n",
336 * Identify the port multiplier
339 ahci_pm_identify(struct ahci_port *ap)
348 ap->ap_probe = ATA_PROBE_FAILED;
349 if (ahci_pm_read(ap, 15, 0, &chipid))
351 if (ahci_pm_read(ap, 15, 1, &rev))
353 if (ahci_pm_read(ap, 15, 2, &nports))
355 nports &= 0x0000000F; /* only the low 4 bits */
356 ap->ap_probe = ATA_PROBE_GOOD;
359 * Ignore fake port on PMs which have it. We can probe it but the
360 * softreset will probably fail.
370 if (has_dummy_port) {
375 kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
378 rev, SATA_PFMT_PM_REV,
380 if (has_dummy_port) {
381 kprintf("%s: Port multiplier: Ignoring dummy port #%d\n",
382 PORTNAME(ap), nports);
384 ap->ap_pmcount = nports;
386 if (ahci_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) {
387 kprintf("%s: Port multiplier: Warning, "
388 "cannot read feature register\n", PORTNAME(ap));
390 kprintf("%s: Port multiplier features: 0x%b\n",
395 if (ahci_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
396 kprintf("%s: Port multiplier defaults: 0x%b\n",
403 * Turn on async notification if we support and the PM supports it.
404 * This allows the PM to forward async notification events to us and
405 * it will also generate an event for target 15 for hot-plug events
406 * (or is supposed to anyway).
408 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) &&
409 (data1 & SATA_PMFEA_ASYNCNOTIFY)) {
410 u_int32_t serr_bits = AHCI_PREG_SERR_DIAG_N |
411 AHCI_PREG_SERR_DIAG_X;
412 data2 |= SATA_PMFEA_ASYNCNOTIFY;
413 if (ahci_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) {
414 kprintf("%s: Port multiplier: AsyncNotify cannot be "
415 "enabled\n", PORTNAME(ap));
416 } else if (ahci_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) {
417 kprintf("%s: Port mulltiplier: AsyncNotify unable "
418 "to enable error info bits\n", PORTNAME(ap));
420 kprintf("%s: Port multiplier: AsyncNotify enabled\n",
427 kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
432 * Do a COMRESET sequence on the target behind a port multiplier.
434 * If hard is 2 we also cycle the phy on the target.
436 * This must be done prior to any softreset or probe attempts on
437 * targets behind the port multiplier.
439 * Returns 0 on success or an error.
442 ahci_pm_hardreset(struct ahci_port *ap, int target, int hard)
449 at = ap->ap_ata[target];
452 * Turn off power management and kill the phy on the target
453 * if requested. Hold state for 10ms.
455 data = AHCI_PREG_SCTL_IPM_DISABLED;
457 data |= AHCI_PREG_SCTL_DET_DISABLE;
458 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1))
460 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
465 * Start transmitting COMRESET. COMRESET must be sent for at
468 at->at_probe = ATA_PROBE_FAILED;
469 at->at_type = ATA_PORT_T_NONE;
470 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
471 switch(AhciForceGen) {
473 data |= AHCI_PREG_SCTL_SPD_ANY;
476 data |= AHCI_PREG_SCTL_SPD_GEN1;
479 data |= AHCI_PREG_SCTL_SPD_GEN2;
482 data |= AHCI_PREG_SCTL_SPD_GEN3;
485 data |= AHCI_PREG_SCTL_SPD_GEN3;
488 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
492 * It takes about 100ms for the DET logic to settle down,
493 * from trial and error testing. If this is too short
494 * the softreset code will fail.
498 if (ahci_pm_phy_status(ap, target, &data)) {
499 kprintf("%s: (A)Cannot clear phy status\n",
504 * Flush any status, then clear DET to initiate negotiation.
506 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
507 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_NONE;
508 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
512 * Try to determine if there is a device on the port.
514 * Give the device 3/10 second to at least be detected.
515 * If we fail clear any pending status since we may have
516 * cycled the phy and probably caused another PRCS interrupt.
518 for (loop = 3; loop; --loop) {
519 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data))
521 if (data & AHCI_PREG_SSTS_DET)
526 kprintf("%s.%d: Port appears to be unplugged\n",
527 PORTNAME(ap), target);
533 * There is something on the port. Give the device 3 seconds
534 * to fully negotiate.
536 for (loop = 30; loop; --loop) {
537 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data))
539 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV)
545 * Device not detected
548 kprintf("%s: Device may be powered down\n",
557 kprintf("%s.%d: Device detected data=%08x\n",
558 PORTNAME(ap), target, data);
560 * Clear SERR on the target so we get a new NOTIFY event if a hot-plug
561 * or hot-unplug occurs. Clear any spurious IFS that may have
562 * occured during the probe.
564 * WARNING! 100ms seems to work in most cases but
567 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
568 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
569 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
573 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
578 * AHCI soft reset through port multiplier.
580 * This function keeps port communications intact and attempts to generate
581 * a reset to the connected device using device commands. Unlike
582 * hard-port operations we can't do fancy stop/starts or stuff like
583 * that without messing up other commands that might be running or
587 ahci_pm_softreset(struct ahci_port *ap, int target)
590 struct ahci_ccb *ccb;
591 struct ahci_cmd_hdr *cmd_slot;
598 at = ap->ap_ata[target];
600 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
605 * Try to clear the phy so we get a good signature, otherwise
606 * the PM may not latch a new signature.
608 * NOTE: This cannot be safely done between the first and second
609 * softreset FISs. It's now or never.
611 if (ahci_pm_phy_status(ap, target, &data)) {
612 kprintf("%s: (B)Cannot clear phy status\n",
615 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
618 * Prep first D2H command with SRST feature & clear busy/reset flags
620 * It is unclear which other fields in the FIS are used. Just zero
623 * When soft-resetting a port behind a multiplier at will be
624 * non-NULL, assigning it to the ccb prevents the port interrupt
625 * from hard-resetting the port if a problem crops up.
627 ccb = ahci_get_err_ccb(ap);
628 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
629 ccb->ccb_xa.complete = ahci_pm_dummy_done;
632 fis = ccb->ccb_cmd_table->cfis;
633 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
634 fis[0] = ATA_FIS_TYPE_H2D;
635 fis[1] = at->at_target;
636 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
638 cmd_slot = ccb->ccb_cmd_hdr;
640 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
641 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
642 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
643 cmd_slot->flags |= htole16(at->at_target <<
644 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
646 ccb->ccb_xa.state = ATA_S_PENDING;
649 * This soft reset of the AP target can cause a stream of IFS
650 * errors to occur. Setting AP_F_IGNORE_IFS prevents the port
651 * from being hard reset (because its the target behind the
652 * port that isn't happy).
654 * The act of sending the soft reset can cause the target to
655 * blow the port up and generate IFS errors.
657 ap->ap_flags |= AP_F_IGNORE_IFS;
658 ap->ap_flags &= ~AP_F_IFS_IGNORED;
660 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
661 kprintf("%s: Soft-reset through PM failed, %s\n",
663 (count > 1 ? "retrying" : "giving up"));
664 ahci_put_err_ccb(ccb);
666 if (ap->ap_flags & AP_F_IFS_IGNORED)
670 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
671 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
678 * WARNING! SENSITIVE TIME PERIOD! WARNING!
680 * The first and second FISes are supposed to be back-to-back,
681 * I think the idea is to get the second sent and then after
682 * the device resets it will send a signature. Do not delay
683 * here and most definitely do not issue any commands to other
688 * Prep second D2H command to read status and complete reset sequence
689 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
690 * Rev 2.6 and it is unclear how the second FIS should be set up
691 * from the AHCI document.
693 * Give the device 3ms before sending the second FIS.
695 * It is unclear which other fields in the FIS are used. Just zero
698 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
699 fis[0] = ATA_FIS_TYPE_H2D;
700 fis[1] = at->at_target;
701 fis[15] = ATA_FIS_CONTROL_4BIT;
704 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
705 cmd_slot->flags |= htole16(at->at_target <<
706 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
708 ccb->ccb_xa.state = ATA_S_PENDING;
709 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
711 ap->ap_flags &= ~AP_F_IFS_IGNORED;
713 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
714 kprintf("%s: Soft-reset(2) through PM failed, %s\n",
716 (count > 1 ? "retrying" : "giving up"));
719 ahci_put_err_ccb(ccb);
720 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
721 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
727 ahci_put_err_ccb(ccb);
729 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
730 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
732 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
733 if (ahci_pm_phy_status(ap, target, &data)) {
734 kprintf("%s: (C)Cannot clear phy status\n",
737 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
740 * Do it again, even if we think we got a good result
748 * If the softreset is trying to clear a BSY condition after a
749 * normal portreset we assign the port type.
751 * If the softreset is being run first as part of the ccb error
752 * processing code then report if the device signature changed
755 if (at->at_type == ATA_PORT_T_NONE) {
756 at->at_type = ahci_port_signature_detect(ap, at);
758 if (ahci_port_signature_detect(ap, at) != at->at_type) {
759 kprintf("%s: device signature unexpectedly "
760 "changed\n", ATANAME(ap, at));
761 error = EBUSY; /* XXX */
767 * Who knows what kind of mess occured. We have exclusive access
768 * to the port so try to clean up potential problems.
774 * Clear error status so we can detect removal.
776 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
777 kprintf("%s: ahci_pm_softreset unable to clear SERR\n",
780 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
781 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
782 ap->ap_flags &= ~(AP_F_IGNORE_IFS | AP_F_IFS_IGNORED);
784 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
790 * Return the phy status for a target behind a port multiplier and
791 * reset SATA_PMREG_SERR.
793 * Returned bits follow AHCI_PREG_SSTS bits. The AHCI_PREG_SSTS_SPD
794 * bits can be used to determine the link speed and will be 0 if there
797 * 0 is returned if any communications error occurs.
800 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap)
804 error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap);
806 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
813 * Check that a target is still good.
816 ahci_pm_check_good(struct ahci_port *ap, int target)
822 * It looks like we might have to read the EINFO register
823 * to allow the PM to generate a new event.
825 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
826 kprintf("%s: Port multiplier EINFO could not be read\n",
830 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
831 kprintf("%s: Port multiplier: SERR could not be cleared\n",
835 if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
837 at = ap->ap_ata[target];
840 * If the device needs an init or hard reset also make sure the
843 if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
844 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
845 ahci_pm_hardreset(ap, target, 1);
849 * Read the detect status
851 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
852 kprintf("%s: Unable to access PM SSTS register target %d\n",
853 PORTNAME(ap), target);
856 if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) {
857 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
858 if (at->at_probe != ATA_PROBE_FAILED) {
859 at->at_probe = ATA_PROBE_FAILED;
860 at->at_type = ATA_PORT_T_NONE;
861 at->at_features |= ATA_PORT_F_RESCAN;
862 kprintf("%s: HOTPLUG (PM) - Device removed\n",
866 if (at->at_probe == ATA_PROBE_FAILED) {
867 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
868 at->at_features |= ATA_PORT_F_RESCAN;
869 kprintf("%s: HOTPLUG (PM) - Device inserted\n",
879 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap)
884 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
886 xa->fis->type = ATA_FIS_TYPE_H2D;
887 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
888 xa->fis->command = ATA_C_READ_PM;
889 xa->fis->features = which;
890 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
891 xa->fis->control = ATA_FIS_CONTROL_4BIT;
893 xa->complete = ahci_pm_dummy_done;
895 xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
898 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) {
899 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) |
900 (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24);
903 kprintf("%s.%d pm_read SCA[%d] failed\n",
904 PORTNAME(ap), target, which);
908 ahci_ata_put_xfer(xa);
913 * Write a PM register
916 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data)
921 xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
923 xa->fis->type = ATA_FIS_TYPE_H2D;
924 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
925 xa->fis->command = ATA_C_WRITE_PM;
926 xa->fis->features = which;
927 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
928 xa->fis->sector_count = (u_int8_t)data;
929 xa->fis->lba_low = (u_int8_t)(data >> 8);
930 xa->fis->lba_mid = (u_int8_t)(data >> 16);
931 xa->fis->lba_high = (u_int8_t)(data >> 24);
932 xa->fis->control = ATA_FIS_CONTROL_4BIT;
934 xa->complete = ahci_pm_dummy_done;
936 xa->flags = ATA_F_POLL;
939 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
943 ahci_ata_put_xfer(xa);
948 * Dummy done callback for xa.
951 ahci_pm_dummy_done(struct ata_xfer *xa)