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 sili_pm_dummy_done(struct ata_xfer *xa);
40 static void sili_pm_empty_done(struct sili_ccb *ccb);
43 * This is called for PM attachments and hot-plug insertion events, and
44 * typically not called again until after an unplug/replug sequence.
46 * We just fall through to the hard-reset code, we don't need to
47 * set up any initial conditions.
50 sili_pm_port_init(struct sili_port *ap, struct ata_port *at)
52 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
57 * This is called from the port hardreset code.
60 sili_pm_port_probe(struct sili_port *ap, int orig_error)
67 * Clean up the port state machine
69 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_PMA);
70 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
71 if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
72 kprintf("%s: PM probe: unable to init port\n",
76 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
77 kprintf("%s: PM probe: port will not come ready\n",
83 * Issue a soft-reset of target 15
85 ap->ap_state = AP_S_NORMAL;
86 sili_pwrite(ap, SILI_PREG_SERR, -1);
87 error = sili_pm_softreset(ap, 15);
90 error = sili_pm_identify(ap);
93 * Finalize. If the softreset failed. Re-init the port
94 * state machine again so the normal non-PM softreset does
98 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
100 at->at_probe = ATA_PROBE_NEED_INIT;
101 at->at_features |= ATA_PORT_F_RESCAN;
102 at->at_features &= ~ATA_PORT_F_READLOG;
104 ap->ap_type = ATA_PORT_T_PM;
108 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
110 if (orig_error == 0) {
111 if (sili_pwait_set_to(ap, 5000, SILI_PREG_STATUS,
112 SILI_PREG_STATUS_READY)) {
113 kprintf("%s: PM probe: port will not come ready\n",
121 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
122 sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
123 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
124 if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
125 kprintf("%s: PM probe: unable to init port\n",
129 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
130 kprintf("%s: PM probe: port will not come ready\n",
134 kprintf("ORIG ERROR %d\n", orig_error);
139 * If we originally detected a device redo the device reset to
140 * try to clear the mess.
142 sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
143 if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
144 kprintf("%s: PM probe: unable to reset\n", PORTNAME(ap));
147 if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
148 kprintf("%s: PM probe: port will not come ready\n",
157 * Identify the port multiplier
160 sili_pm_identify(struct sili_port *ap)
169 ap->ap_probe = ATA_PROBE_FAILED;
170 if (sili_pm_read(ap, 15, 0, &chipid))
172 if (sili_pm_read(ap, 15, 1, &rev))
174 if (sili_pm_read(ap, 15, 2, &nports))
176 nports &= 0x0000000F; /* only the low 4 bits */
177 ap->ap_probe = ATA_PROBE_GOOD;
180 * Ignore fake port on PMs which have it. We can probe it but the
181 * softreset will probably fail.
191 if (has_dummy_port) {
196 kprintf("%s: Port multiplier: chip=%08x rev=0x%pb%i nports=%d\n",
199 SATA_PFMT_PM_REV, rev,
201 if (has_dummy_port) {
202 kprintf("%s: Port multiplier: Ignoring dummy port #%d\n",
203 PORTNAME(ap), nports);
205 ap->ap_pmcount = nports;
207 if (sili_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) {
208 kprintf("%s: Port multiplier: Warning, "
209 "cannot read feature register\n", PORTNAME(ap));
211 kprintf("%s: Port multiplier features: 0x%pb%i\n",
212 PORTNAME(ap), SATA_PFMT_PM_FEA, data1);
214 if (sili_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
215 kprintf("%s: Port multiplier defaults: 0x%pb%i\n",
216 PORTNAME(ap), SATA_PFMT_PM_FEA, data2);
220 * Turn on async notification if we support and the PM supports it.
221 * This allows the PM to forward async notification events to us and
222 * it will also generate an event for target 15 for hot-plug events
223 * (or is supposed to anyway).
225 if ((ap->ap_sc->sc_flags & SILI_F_SSNTF) &&
226 (data1 & SATA_PMFEA_ASYNCNOTIFY)) {
227 u_int32_t serr_bits = SATA_PM_SERR_DIAG_N |
229 data2 |= SATA_PMFEA_ASYNCNOTIFY;
230 if (sili_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) {
231 kprintf("%s: Port multiplier: AsyncNotify cannot be "
232 "enabled\n", PORTNAME(ap));
233 } else if (sili_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) {
234 kprintf("%s: Port mulltiplier: AsyncNotify unable "
235 "to enable error info bits\n", PORTNAME(ap));
237 kprintf("%s: Port multiplier: AsyncNotify enabled\n",
244 kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
249 * Do a COMRESET sequence on the target behind a port multiplier.
251 * If hard is 2 we also cycle the phy on the target.
253 * This must be done prior to any softreset or probe attempts on
254 * targets behind the port multiplier.
256 * Returns 0 on success or an error.
259 sili_pm_hardreset(struct sili_port *ap, int target, int hard)
266 at = &ap->ap_ata[target];
269 * Ensure that no other commands are pending. Our HW reset of
270 * the PM target can skewer the port overall!
272 sili_exclusive_access(ap);
275 * Turn off power management and kill the phy on the target
276 * if requested. Hold state for 10ms.
278 data = SATA_PM_SCTL_IPM_DISABLED;
281 data |= SATA_PM_SCTL_DET_DISABLE;
283 if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1))
285 if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
290 * Start transmitting COMRESET. COMRESET must be sent for at
293 * It takes about 100ms for the DET logic to settle down,
294 * from trial and error testing. If this is too short
295 * the softreset code will fail.
297 * It is very important to allow the logic to settle before
298 * we issue any additional commands or the target will interfere
299 * with our PM commands.
301 at->at_probe = ATA_PROBE_FAILED;
302 at->at_type = ATA_PORT_T_NONE;
303 data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_INIT;
304 if (SiliForceGen1 & (1 << ap->ap_num)) {
305 kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target);
306 data |= SATA_PM_SCTL_SPD_GEN1;
308 data |= SATA_PM_SCTL_SPD_ANY;
310 if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
314 if (sili_pm_phy_status(ap, target, &data)) {
315 kprintf("%s: (A)Cannot clear phy status\n",
320 * Flush any status, then clear DET to initiate negotiation.
322 * It is very important to allow the negotiation to settle before
323 * we issue any additional commands or the target will interfere
324 * with our PM commands.
326 sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
327 data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_NONE;
328 if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
333 * Try to determine if there is a device on the port.
335 * Give the device 3/10 second to at least be detected.
336 * If we fail clear any pending status since we may have
337 * cycled the phy and probably caused another PRCS interrupt.
339 for (loop = 3; loop; --loop) {
340 if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
342 if (data & SATA_PM_SSTS_DET)
347 kprintf("%s.%d: Port appears to be unplugged\n",
348 PORTNAME(ap), target);
354 * There is something on the port. Give the device 3 seconds
355 * to fully negotiate.
357 for (loop = 30; loop; --loop) {
358 if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
360 if ((data & SATA_PM_SSTS_DET) == SATA_PM_SSTS_DET_DEV)
366 * Device not detected
369 kprintf("%s: Device may be powered down\n",
378 * Wait 200ms to give the device time to send its first D2H FIS.
379 * If we do not wait long enough our softreset sequence can collide
380 * with the end of the device's reset sequence and brick the port.
381 * Some devices may need longer and we handle those cases in the
384 * XXX Looks like we have to wait a lot longer. If the Sili chip's
385 * softreset fails due to a collision with the D2H FIS or the
386 * unbusying it bricks the port.
388 * XXX how do we poll that particular target's BSY status via the
391 kprintf("%s.%d: PM Device detected ssts=%08x\n",
392 PORTNAME(ap), target, data);
397 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
402 * SILI soft reset through port multiplier.
404 * This function generates a soft reset through the port multiplier,
405 * keeping port communications intact.
407 * The SII chip will do the whole mess for us. However, the command
408 * can brick the port if the target is still busy from the previous
412 sili_pm_softreset(struct sili_port *ap, int target)
415 struct sili_ccb *ccb;
416 struct sili_prb *prb;
423 at = &ap->ap_ata[target];
425 kprintf("%s: PM softreset\n", ATANAME(ap, at));
428 * Prep the special soft-reset SII command.
430 ccb = sili_get_err_ccb(ap);
431 ccb->ccb_done = sili_pm_empty_done;
432 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
433 ccb->ccb_xa.complete = sili_pm_dummy_done;
437 bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
438 prb->prb_h2d.flags = at->at_target;
439 prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
440 prb->prb_override = 0;
441 prb->prb_xfer_count = 0;
443 ccb->ccb_xa.state = ATA_S_PENDING;
445 timeout = (target == 15) ? 1000 : 8000;
448 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
450 if (sili_poll(ccb, timeout, sili_quick_timeout) != ATA_S_COMPLETE) {
452 kprintf("%s: (PM) Softreset FIS failed\n",
455 sili_put_err_ccb(ccb);
459 sig = (prb->prb_d2h.lba_high << 24) |
460 (prb->prb_d2h.lba_mid << 16) |
461 (prb->prb_d2h.lba_low << 8) |
462 (prb->prb_d2h.sector_count);
463 kprintf("%s: PM SOFTRESET SIGNATURE %08x\n", ATANAME(ap, at), sig);
465 sili_put_err_ccb(ccb);
468 * Clear the phy status of the target so we can get a new event.
470 * Target 15 is the PM itself and these registers have
471 * different meanings.
474 if (sili_pm_phy_status(ap, target, &data)) {
475 kprintf("%s: (C)Cannot clear phy status\n",
478 sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
482 * If the softreset is trying to clear a BSY condition after a
483 * normal portreset we assign the port type.
485 * If the softreset is being run first as part of the ccb error
486 * processing code then report if the device signature changed
489 if (at->at_type == ATA_PORT_T_NONE) {
490 at->at_type = sili_port_signature(ap, at, sig);
492 if (sili_port_signature(ap, at, sig) != at->at_type) {
493 kprintf("%s: device signature unexpectedly "
494 "changed\n", ATANAME(ap, at));
495 error = EBUSY; /* XXX */
501 * Clear error status so we can detect removal.
503 * Target 15 is the PM itself and these registers have
504 * different meanings.
506 kprintf("%s: PM softreset done error %d\n", ATANAME(ap, at), error);
507 if (error == 0 && target != 15) {
508 if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
509 kprintf("%s: sili_pm_softreset unable to clear SERR\n",
511 ap->ap_flags &= ~AP_F_IGNORE_IFS;
515 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
521 * Return the phy status for a target behind a port multiplier and
522 * reset SATA_PMREG_SERR.
524 * Returned bits follow SILI_PREG_SSTS bits. The SILI_PREG_SSTS_SPD
525 * bits can be used to determine the link speed and will be 0 if there
528 * 0 is returned if any communications error occurs.
531 sili_pm_phy_status(struct sili_port *ap, int target, u_int32_t *datap)
535 error = sili_pm_read(ap, target, SATA_PMREG_SSTS, datap);
537 error = sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
544 sili_pm_set_feature(struct sili_port *ap, int feature, int enable)
549 xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
551 xa->fis->type = ATA_FIS_TYPE_H2D;
552 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
553 xa->fis->command = enable ? ATA_C_SATA_FEATURE_ENA :
554 ATA_C_SATA_FEATURE_DIS;
555 xa->fis->sector_count = feature;
556 xa->fis->control = ATA_FIS_CONTROL_4BIT;
558 xa->complete = sili_pm_dummy_done;
560 xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
563 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
567 sili_ata_put_xfer(xa);
572 * Check that a target is still good.
575 sili_pm_check_good(struct sili_port *ap, int target)
581 * It looks like we might have to read the EINFO register
582 * to allow the PM to generate a new event.
584 if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
585 kprintf("%s: Port multiplier EINFO could not be read\n",
589 if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
590 kprintf("%s: Port multiplier: SERR could not be cleared\n",
594 if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
596 at = &ap->ap_ata[target];
599 * If the device needs an init or hard reset also make sure the
602 if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
603 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
604 sili_pm_hardreset(ap, target, 1);
608 * Read the detect status
610 if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
611 kprintf("%s: Unable to access PM SSTS register target %d\n",
612 PORTNAME(ap), target);
615 if ((data & SATA_PM_SSTS_DET) != SATA_PM_SSTS_DET_DEV) {
616 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
617 if (at->at_probe != ATA_PROBE_FAILED) {
618 at->at_probe = ATA_PROBE_FAILED;
619 at->at_type = ATA_PORT_T_NONE;
620 at->at_features |= ATA_PORT_F_RESCAN;
621 kprintf("%s: HOTPLUG (PM) - Device removed\n",
625 if (at->at_probe == ATA_PROBE_FAILED) {
626 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
627 at->at_features |= ATA_PORT_F_RESCAN;
628 kprintf("%s: HOTPLUG (PM) - Device inserted\n",
638 sili_pm_read(struct sili_port *ap, int target, int which, u_int32_t *datap)
643 xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
645 xa->fis->type = ATA_FIS_TYPE_H2D;
646 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
647 xa->fis->command = ATA_C_READ_PM;
648 xa->fis->features = which;
649 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
650 xa->fis->control = ATA_FIS_CONTROL_4BIT;
652 xa->complete = sili_pm_dummy_done;
654 xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
657 if (sili_ata_cmd(xa) == ATA_S_COMPLETE) {
658 *datap = xa->rfis->sector_count | (xa->rfis->lba_low << 8) |
659 (xa->rfis->lba_mid << 16) | (xa->rfis->lba_high << 24);
662 kprintf("%s.%d pm_read SCA[%d] failed\n",
663 PORTNAME(ap), target, which);
667 sili_ata_put_xfer(xa);
672 * Write a PM register
675 sili_pm_write(struct sili_port *ap, int target, int which, u_int32_t data)
680 xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
682 xa->fis->type = ATA_FIS_TYPE_H2D;
683 xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
684 xa->fis->command = ATA_C_WRITE_PM;
685 xa->fis->features = which;
686 xa->fis->device = target | ATA_H2D_DEVICE_LBA;
687 xa->fis->sector_count = (u_int8_t)data;
688 xa->fis->lba_low = (u_int8_t)(data >> 8);
689 xa->fis->lba_mid = (u_int8_t)(data >> 16);
690 xa->fis->lba_high = (u_int8_t)(data >> 24);
691 xa->fis->control = ATA_FIS_CONTROL_4BIT;
693 xa->complete = sili_pm_dummy_done;
695 xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
698 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
702 sili_ata_put_xfer(xa);
707 * Dummy done callback for xa.
710 sili_pm_dummy_done(struct ata_xfer *xa)
715 sili_pm_empty_done(struct sili_ccb *ccb)