2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
37 static void ahci_pm_dummy_done(struct ata_xfer *xa);
38 static void ahci_pm_empty_done(struct ahci_ccb *ccb);
41 * Identify the port multiplier
44 ahci_pm_identify(struct ahci_port *ap)
53 ap->ap_probe = ATA_PROBE_FAILED;
54 if (ahci_pm_read(ap, 15, 0, &chipid))
56 if (ahci_pm_read(ap, 15, 1, &rev))
58 if (ahci_pm_read(ap, 15, 2, &nports))
60 nports &= 0x0000000F; /* only the low 4 bits */
61 ap->ap_probe = ATA_PROBE_GOOD;
62 kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
65 rev, AHCI_PFMT_PM_REV,
67 ap->ap_pmcount = nports;
69 if (ahci_pm_read(ap, 15, AHCI_PMREG_FEA, &data1)) {
70 kprintf("%s: Port multiplier: Warning, "
71 "cannot read feature register\n", PORTNAME(ap));
73 kprintf("%s: Port multiplier features: 0x%b\n",
78 if (ahci_pm_read(ap, 15, AHCI_PMREG_FEAEN, &data2) == 0) {
79 kprintf("%s: Port multiplier defaults: 0x%b\n",
86 * Turn on async notification if we support and the PM supports it.
87 * This allows the PM to forward async notification events to us and
88 * it will also generate an event for target 15 for hot-plug events
89 * (or is supposed to anyway).
91 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) &&
92 (data1 & AHCI_PMFEA_ASYNCNOTIFY)) {
93 u_int32_t serr_bits = AHCI_PREG_SERR_DIAG_N |
94 AHCI_PREG_SERR_DIAG_X;
95 data2 |= AHCI_PMFEA_ASYNCNOTIFY;
96 if (ahci_pm_write(ap, 15, AHCI_PMREG_FEAEN, data2)) {
97 kprintf("%s: Port multiplier: AsyncNotify cannot be "
98 "enabled\n", PORTNAME(ap));
99 } else if (ahci_pm_write(ap, 15, AHCI_PMREG_EEENA, serr_bits)) {
100 kprintf("%s: Port mulltiplier: AsyncNotify unable "
101 "to enable error info bits\n", PORTNAME(ap));
103 kprintf("%s: Port multiplier: AsyncNotify enabled\n",
110 kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
115 * Do a COMRESET sequence on the target behind a port multiplier.
117 * If hard is 2 we also cycle the phy on the target.
119 * This must be done prior to any softreset or probe attempts on
120 * targets behind the port multiplier.
122 * Returns 0 on success or an error.
125 ahci_pm_hardreset(struct ahci_port *ap, int target, int hard)
132 kprintf("%s.%d: ahci_pm_hardreset hard=%d\n",
133 PORTNAME(ap), target, hard);
135 at = &ap->ap_ata[target];
138 * Turn off power management and kill the phy on the target
139 * if requested. Hold state for 10ms.
141 data = AHCI_PREG_SCTL_IPM_DISABLED;
143 data |= AHCI_PREG_SCTL_DET_DISABLE;
144 if (ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1))
146 if (ahci_pm_write(ap, target, AHCI_PMREG_SCTL, data))
151 * Start transmitting COMRESET. COMRESET must be sent for at
154 at->at_probe = ATA_PROBE_FAILED;
155 at->at_type = ATA_PORT_T_NONE;
156 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
157 if (AhciForceGen1 & (1 << ap->ap_num)) {
158 kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target);
159 data |= AHCI_PREG_SCTL_SPD_GEN1;
161 data |= AHCI_PREG_SCTL_SPD_ANY;
163 if (ahci_pm_write(ap, target, AHCI_PMREG_SCTL, data))
168 if (ahci_pm_phy_status(ap, target, &data)) {
169 kprintf("%s: Cannot clear phy status\n",
175 * Flush any status, then clear DET to initiate negotiation.
177 ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1);
178 data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_NONE;
179 if (ahci_pm_write(ap, target, AHCI_PMREG_SCTL, data))
183 * Try to determine if there is a device on the port.
185 * Give the device 3/10 second to at least be detected.
186 * If we fail clear any pending status since we may have
187 * cycled the phy and probably caused another PRCS interrupt.
189 for (loop = 3; loop; --loop) {
190 if (ahci_pm_read(ap, target, AHCI_PMREG_SSTS, &data))
192 if (data & AHCI_PREG_SSTS_DET)
197 kprintf("%s.%d: Port appears to be unplugged\n",
198 PORTNAME(ap), target);
204 * There is something on the port. Give the device 3 seconds
205 * to fully negotiate.
207 for (loop = 30; loop; --loop) {
208 if (ahci_pm_read(ap, target, AHCI_PMREG_SSTS, &data))
210 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV)
216 * Clear SERR on the target so we get a new NOTIFY event if a hot-plug
217 * or hot-unplug occurs.
219 ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1);
222 * Device not detected
225 kprintf("%s: Device may be powered down\n",
234 kprintf("%s.%d: Device detected data=%08x\n",
235 PORTNAME(ap), target, data);
239 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
244 * AHCI soft reset through port multiplier.
246 * This function keeps port communications intact and attempts to generate
247 * a reset to the connected device using device commands. Unlike
248 * hard-port operations we can't do fancy stop/starts or stuff like
249 * that without messing up other commands that might be running or
253 ahci_pm_softreset(struct ahci_port *ap, int target)
256 struct ahci_ccb *ccb = NULL;
257 struct ahci_cmd_hdr *cmd_slot;
265 at = &ap->ap_ata[target];
267 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
269 kprintf("%s: ahci_pm_softreset\n", ATANAME(ap, at));
274 * Try to clear the phy so we get a good signature, otherwise
275 * the PM may not latch a new signature.
277 * NOTE: This cannot be safely done between the first and second
278 * softreset FISs. It's now or never.
281 if (ahci_pm_phy_status(ap, target, &data)) {
282 kprintf("%s: Cannot clear phy status\n",
288 * Prep first D2H command with SRST feature & clear busy/reset flags
290 * It is unclear which other fields in the FIS are used. Just zero
293 * When soft-resetting a port behind a multiplier at will be
294 * non-NULL, assigning it to the ccb prevents the port interrupt
295 * from hard-resetting the port if a problem crops up.
297 ccb = ahci_get_ccb(ap);
298 ccb->ccb_done = ahci_pm_empty_done;
299 ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
300 ccb->ccb_xa.complete = ahci_pm_dummy_done;
303 fis = ccb->ccb_cmd_table->cfis;
304 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
305 fis[0] = ATA_FIS_TYPE_H2D;
306 fis[1] = at->at_target;
307 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
309 cmd_slot = ccb->ccb_cmd_hdr;
311 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
312 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
313 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
314 cmd_slot->flags |= htole16(at->at_target <<
315 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
317 ccb->ccb_xa.state = ATA_S_PENDING;
318 ccb->ccb_xa.flags = 0;
321 * XXX hack to ignore IFS errors which can occur during the target
324 * If an IFS error occurs the target is probably powering up,
325 * so we try for a longer period of time.
327 ap->ap_flags |= AP_F_IGNORE_IFS;
328 ap->ap_flags &= ~(AP_F_IFS_IGNORED | AP_F_IFS_OCCURED);
330 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != 0 ||
331 ccb->ccb_xa.state != ATA_S_COMPLETE) {
332 kprintf("%s: First FIS failed\n", ATANAME(ap, at));
333 if (tried_longer == 0 && (ap->ap_flags & AP_F_IFS_OCCURED)) {
346 * The device may muff the PHY up.
348 ahci_os_sleep(100); /* XXX 3ms should do it? */
351 * Prep second D2H command to read status and complete reset sequence
352 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
353 * Rev 2.6 and it is unclear how the second FIS should be set up
354 * from the AHCI document.
356 * Give the device 3ms before sending the second FIS.
358 * It is unclear which other fields in the FIS are used. Just zero
361 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
362 fis[0] = ATA_FIS_TYPE_H2D;
363 fis[1] = at->at_target;
364 fis[15] = ATA_FIS_CONTROL_4BIT;
367 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
368 cmd_slot->flags |= htole16(at->at_target <<
369 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
371 ccb->ccb_xa.state = ATA_S_PENDING;
372 ccb->ccb_xa.flags = 0;
374 if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != 0 ||
375 ccb->ccb_xa.state != ATA_S_COMPLETE) {
376 kprintf("%s: Second FIS failed\n", ATANAME(ap, at));
388 * Do it again, even if we think we got a good result
397 * If the softreset is trying to clear a BSY condition after a
398 * normal portreset we assign the port type.
400 * If the softreset is being run first as part of the ccb error
401 * processing code then report if the device signature changed
404 if (at->at_type == ATA_PORT_T_NONE) {
405 at->at_type = ahci_port_signature_detect(ap, at);
407 if (ahci_port_signature_detect(ap, at) != at->at_type) {
408 kprintf("%s: device signature unexpectedly "
409 "changed\n", ATANAME(ap, at));
410 error = EBUSY; /* XXX */
418 * Clean up the CCB. If the command failed it already went through
419 * the standard timeout handling and should no longer be active.
422 KKASSERT((ap->ap_active & (1 << ccb->ccb_slot)) == 0);
428 * If we failed to softreset make the port quiescent, otherwise
429 * make sure the port's start/stop state matches what it was on
432 * Don't kill the port if the softreset is on a port multiplier
433 * target, that would kill all the targets!
436 ap->ap_flags &= ~AP_F_IGNORE_IFS;
439 * Clear error status so we can detect removal
441 if (ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1)) {
442 kprintf("%s: ahci_pm_softreset unable to clear SERR\n",
446 kprintf("%s: ahci_pm_softreset done %d\n", ATANAME(ap, at), error);
448 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
454 * Return the phy status for a target behind a port multiplier and
455 * reset AHCI_PMREG_SERR.
457 * Returned bits follow AHCI_PREG_SSTS bits. The AHCI_PREG_SSTS_SPD
458 * bits can be used to determine the link speed and will be 0 if there
461 * 0 is returned if any communications error occurs.
464 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap)
468 error = ahci_pm_read(ap, target, AHCI_PMREG_SSTS, datap);
470 error = ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1);
477 ahci_pm_set_feature(struct ahci_port *ap, int feature, int enable)
483 xa = ahci_ata_get_xfer(ap, &ap->ap_ata[15]);
485 bzero(xa->fis, sizeof(*xa->fis));
486 xa->fis->type = ATA_FIS_TYPE_H2D;
487 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
488 xa->fis->command = enable ? ATA_C_SATA_FEATURE_ENA :
489 ATA_C_SATA_FEATURE_DIS;
490 xa->fis->sector_count = feature;
491 xa->fis->control = ATA_FIS_CONTROL_4BIT;
493 xa->complete = ahci_pm_dummy_done;
495 xa->flags = ATA_F_READ | ATA_F_POLL;
498 status = ahci_ata_cmd(xa);
499 error = (status == ATA_COMPLETE && xa->state == ATA_S_COMPLETE) ?
501 ahci_ata_put_xfer(xa);
506 * Check that a target is still good.
509 ahci_pm_check_good(struct ahci_port *ap, int target)
515 * It looks like we might have to read the EINFO register
516 * to allow the PM to generate a new event.
518 if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) {
519 kprintf("%s: Port multiplier EINFO could not be read\n",
522 if (ahci_pm_write(ap, target, AHCI_PMREG_SERR, -1)) {
523 kprintf("%s: Port multiplier: SERR could not be cleared\n",
527 if (target == CAM_TARGET_WILDCARD)
530 at = &ap->ap_ata[target];
531 if (ahci_pm_read(ap, target, AHCI_PMREG_SSTS, &data)) {
532 kprintf("%s: Unable to access PM SSTS register target %d\n",
533 PORTNAME(ap), target);
536 if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) {
537 if (at->at_probe != ATA_PROBE_FAILED) {
538 at->at_probe = ATA_PROBE_FAILED;
539 at->at_type = ATA_PORT_T_NONE;
540 at->at_features |= ATA_PORT_F_RESCAN;
541 kprintf("%s: HOTPLUG - Device removed\n",
545 if (at->at_probe == ATA_PROBE_FAILED) {
546 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
547 at->at_features |= ATA_PORT_F_RESCAN;
548 kprintf("%s: HOTPLUG - Device inserted\n",
558 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap)
564 xa = ahci_ata_get_xfer(ap, &ap->ap_ata[15]);
566 bzero(xa->fis, sizeof(*xa->fis));
567 xa->fis->type = ATA_FIS_TYPE_H2D;
568 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
569 xa->fis->command = ATA_C_READ_PM;
570 xa->fis->features = which;
571 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
572 xa->fis->control = ATA_FIS_CONTROL_4BIT;
574 xa->complete = ahci_pm_dummy_done;
576 xa->flags = ATA_F_READ | ATA_F_POLL;
579 status = ahci_ata_cmd(xa);
580 if (status == ATA_COMPLETE && xa->state == ATA_S_COMPLETE) {
581 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) |
582 (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24);
585 kprintf("pm_read status %d xa->state %d\n", status, xa->state);
589 ahci_ata_put_xfer(xa);
594 * Write a PM register
597 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data)
603 xa = ahci_ata_get_xfer(ap, &ap->ap_ata[15]);
605 bzero(xa->fis, sizeof(*xa->fis));
606 xa->fis->type = ATA_FIS_TYPE_H2D;
607 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
608 xa->fis->command = ATA_C_WRITE_PM;
609 xa->fis->features = which;
610 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
611 xa->fis->sector_count = (u_int8_t)data;
612 xa->fis->lba_low = (u_int8_t)(data >> 8);
613 xa->fis->lba_mid = (u_int8_t)(data >> 16);
614 xa->fis->lba_high = (u_int8_t)(data >> 24);
615 xa->fis->control = ATA_FIS_CONTROL_4BIT;
617 xa->complete = ahci_pm_dummy_done;
619 xa->flags = ATA_F_READ | ATA_F_POLL;
622 status = ahci_ata_cmd(xa);
623 error = (status == ATA_COMPLETE && xa->state == ATA_S_COMPLETE) ?
625 ahci_ata_put_xfer(xa);
630 * Dummy done callback for xa.
633 ahci_pm_dummy_done(struct ata_xfer *xa)
638 ahci_pm_empty_done(struct ahci_ccb *ccb)
640 ccb->ccb_xa.state = ATA_S_COMPLETE;