make sure all mutex elements are set in initializers.
[AROS.git] / rom / devs / ahci / ahci_pm.c
blobc4eecc05b8e3830056a7fe224d04ef6dd0e47d4e
1 /*
2 * (MPSAFE)
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
11 * are met:
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.
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.
37 #include "ahci.h"
39 static void ahci_pm_dummy_done(struct ata_xfer *xa);
41 int
42 ahci_pm_port_init(struct ahci_port *ap, struct ata_port *at)
44 at->at_probe = ATA_PROBE_NEED_HARD_RESET;
45 return (0);
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.
53 * We MUST use CLO to properly probe whether the port multiplier exists
54 * or not.
56 * Return 0 on success, non-zero on failure.
58 int
59 ahci_pm_port_probe(struct ahci_port *ap, int orig_error)
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 u_int32_t fbs;
68 int count;
69 int i;
71 count = 2;
72 retry:
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";
106 } else {
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)";
116 } else {
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
127 * the port.
129 ahci_flush_tfd(ap);
130 ahci_port_clo(ap);
131 if (ahci_port_start(ap)) {
132 kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
133 PORTNAME(ap));
134 error = EIO;
135 goto err;
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",
144 PORTNAME(ap),
145 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
146 ? "failed" : "unsupported");
147 error = EBUSY;
148 goto err;
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
156 * the signature.
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
167 * flags.
169 fis = ccb->ccb_cmd_table->cfis;
170 memset(fis, 0, 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;
175 cmd_slot->prdtl = 0;
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
196 * the _C_
198 if (ahci_poll(ccb, 500, ahci_quick_timeout) != ATA_S_COMPLETE) {
199 kprintf("%s: PMPROBE(1) No Port Multiplier was found.\n",
200 PORTNAME(ap));
201 if (--count) {
202 ahci_put_err_ccb(ccb);
203 goto retry;
205 error = EBUSY;
206 goto err;
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.
217 ahci_flush_tfd(ap);
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
230 * everything.
232 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_SILENT;
234 memset(fis, 0, sizeof(ccb->ccb_cmd_table->cfis));
235 fis[0] = ATA_FIS_TYPE_H2D;
236 fis[1] = 0x0F;
237 fis[15] = ATA_FIS_CONTROL_4BIT;
239 cmd_slot->prdtl = 0;
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",
256 PORTNAME(ap));
257 if (--count) {
258 ahci_put_err_ccb(ccb);
259 goto retry;
261 error = EBUSY;
262 goto err;
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.
271 if (--count) {
272 fis[15] = 0;
273 ahci_put_err_ccb(ccb);
274 goto retry;
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;
282 error = 0;
283 } else {
284 error = EBUSY;
288 * Fall through / clean up the CCB and perform error processing.
290 err:
291 if (ccb != NULL)
292 ahci_put_err_ccb(ccb);
294 if (error == 0 && ahci_pm_identify(ap)) {
295 kprintf("%s: PM - cannot identify port multiplier\n",
296 PORTNAME(ap));
297 error = EBUSY;
301 * If we probed the PM reset the state for the targets behind
302 * it so they get probed by the state machine.
304 if (error == 0) {
305 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
306 at = ap->ap_ata[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;
311 return (0);
315 * If we failed turn off PMA, otherwise identify the port multiplier.
316 * CAM will iterate the devices.
318 ahci_port_stop(ap, 0);
319 ahci_port_clo(ap);
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",
327 PORTNAME(ap));
328 orig_error = EBUSY;
329 ahci_port_init(ap);
332 return(orig_error);
336 * Identify the port multiplier
339 ahci_pm_identify(struct ahci_port *ap)
341 u_int32_t chipid;
342 u_int32_t rev;
343 u_int32_t nports;
344 u_int32_t data1;
345 u_int32_t data2;
346 int has_dummy_port;
348 ap->ap_probe = ATA_PROBE_FAILED;
349 if (ahci_pm_read(ap, 15, 0, &chipid))
350 goto err;
351 if (ahci_pm_read(ap, 15, 1, &rev))
352 goto err;
353 if (ahci_pm_read(ap, 15, 2, &nports))
354 goto err;
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.
362 switch(chipid) {
363 case 0x37261095:
364 has_dummy_port = 1;
365 break;
366 default:
367 has_dummy_port = 0;
368 break;
370 if (has_dummy_port) {
371 if (nports > 1)
372 --nports;
375 kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
376 PORTNAME(ap),
377 chipid,
378 rev, SATA_PFMT_PM_REV,
379 nports);
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));
389 } else {
390 kprintf("%s: Port multiplier features: 0x%b\n",
391 PORTNAME(ap),
392 data1,
393 SATA_PFMT_PM_FEA);
395 if (ahci_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
396 kprintf("%s: Port multiplier defaults: 0x%b\n",
397 PORTNAME(ap),
398 data2,
399 SATA_PFMT_PM_FEA);
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));
419 } else {
420 kprintf("%s: Port multiplier: AsyncNotify enabled\n",
421 PORTNAME(ap));
425 return (0);
426 err:
427 kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
428 return (EIO);
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)
444 struct ata_port *at;
445 u_int32_t data;
446 int loop;
447 int error = EIO;
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;
456 if (hard == 2)
457 data |= AHCI_PREG_SCTL_DET_DISABLE;
458 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1))
459 goto err;
460 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
461 goto err;
462 ahci_os_sleep(10);
465 * Start transmitting COMRESET. COMRESET must be sent for at
466 * least 1ms.
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) {
472 case 0:
473 data |= AHCI_PREG_SCTL_SPD_ANY;
474 break;
475 case 1:
476 data |= AHCI_PREG_SCTL_SPD_GEN1;
477 break;
478 case 2:
479 data |= AHCI_PREG_SCTL_SPD_GEN2;
480 break;
481 case 3:
482 data |= AHCI_PREG_SCTL_SPD_GEN3;
483 break;
484 default:
485 data |= AHCI_PREG_SCTL_SPD_GEN3;
486 break;
488 if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
489 goto err;
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.
496 ahci_os_sleep(100);
498 if (ahci_pm_phy_status(ap, target, &data)) {
499 kprintf("%s: (A)Cannot clear phy status\n",
500 ATANAME(ap ,at));
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))
509 goto err;
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))
520 goto err;
521 if (data & AHCI_PREG_SSTS_DET)
522 break;
523 ahci_os_sleep(100);
525 if (loop == 0) {
526 kprintf("%s.%d: Port appears to be unplugged\n",
527 PORTNAME(ap), target);
528 error = ENODEV;
529 goto err;
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))
538 goto err;
539 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV)
540 break;
541 ahci_os_sleep(100);
545 * Device not detected
547 if (loop == 0) {
548 kprintf("%s: Device may be powered down\n",
549 PORTNAME(ap));
550 error = ENODEV;
551 goto err;
555 * Device detected
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
566 ahci_os_sleep(100);
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);
571 error = 0;
572 err:
573 at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
574 return (error);
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
584 * queued.
587 ahci_pm_softreset(struct ahci_port *ap, int target)
589 struct ata_port *at;
590 struct ahci_ccb *ccb;
591 struct ahci_cmd_hdr *cmd_slot;
592 u_int8_t *fis;
593 int count;
594 int error;
595 u_int32_t data;
597 error = EIO;
598 at = ap->ap_ata[target];
600 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
602 count = 2;
603 retry:
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",
613 ATANAME(ap ,at));
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
621 * everything.
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;
630 ccb->ccb_xa.at = at;
632 fis = ccb->ccb_cmd_table->cfis;
633 memset(fis, 0, 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;
639 cmd_slot->prdtl = 0;
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",
662 ATANAME(ap, at),
663 (count > 1 ? "retrying" : "giving up"));
664 ahci_put_err_ccb(ccb);
665 if (--count) {
666 if (ap->ap_flags & AP_F_IFS_IGNORED)
667 ahci_os_sleep(5000);
668 else
669 ahci_os_sleep(1000);
670 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
671 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
672 goto retry;
674 goto err;
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
684 * targets!
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
696 * everything.
698 memset(fis, 0, 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;
703 cmd_slot->prdtl = 0;
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",
715 ATANAME(ap, at),
716 (count > 1 ? "retrying" : "giving up"));
717 if (--count) {
718 ahci_os_sleep(1000);
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);
722 goto retry;
724 goto err;
727 ahci_put_err_ccb(ccb);
728 ahci_os_sleep(100);
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",
735 ATANAME(ap ,at));
737 ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
740 * Do it again, even if we think we got a good result
742 if (--count) {
743 fis[15] = 0;
744 goto retry;
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
753 * unexpectedly.
755 if (at->at_type == ATA_PORT_T_NONE) {
756 at->at_type = ahci_port_signature_detect(ap, at);
757 } else {
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 */
764 error = 0;
767 * Who knows what kind of mess occured. We have exclusive access
768 * to the port so try to clean up potential problems.
770 err:
771 ahci_os_sleep(100);
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",
778 ATANAME(ap, at));
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;
785 return (error);
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
795 * is no link.
797 * 0 is returned if any communications error occurs.
800 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap)
802 int error;
804 error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap);
805 if (error == 0)
806 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
807 if (error)
808 *datap = 0;
809 return(error);
813 * Check that a target is still good.
815 void
816 ahci_pm_check_good(struct ahci_port *ap, int target)
818 struct ata_port *at;
819 u_int32_t data;
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",
827 PORTNAME(ap));
830 if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
831 kprintf("%s: Port multiplier: SERR could not be cleared\n",
832 PORTNAME(ap));
835 if (target == ~0 || target >= ap->ap_pmcount)
836 return;
837 at = ap->ap_ata[target];
840 * If the device needs an init or hard reset also make sure the
841 * PHY is turned on.
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);
854 return;
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",
863 ATANAME(ap, at));
865 } else {
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",
870 ATANAME(ap, at));
876 * Read a PM register
879 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap)
881 struct ata_xfer *xa;
882 int error;
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;
894 xa->datalen = 0;
895 xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
896 xa->timeout = 1000;
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);
901 error = 0;
902 } else {
903 kprintf("%s.%d pm_read SCA[%d] failed\n",
904 PORTNAME(ap), target, which);
905 *datap = 0;
906 error = EIO;
908 ahci_ata_put_xfer(xa);
909 return (error);
913 * Write a PM register
916 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data)
918 struct ata_xfer *xa;
919 int error;
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;
935 xa->datalen = 0;
936 xa->flags = ATA_F_POLL;
937 xa->timeout = 1000;
939 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
940 error = 0;
941 else
942 error = EIO;
943 ahci_ata_put_xfer(xa);
944 return(error);
948 * Dummy done callback for xa.
950 static void
951 ahci_pm_dummy_done(struct ata_xfer *xa)