Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / disk / ahci / ahci_pm.c
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
560         error = EIO;
561         at = ap->ap_ata[target];
562
563         DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
564
565         count = 2;
566 retry:
567         /*
568          * Try to clear the phy so we get a good signature, otherwise
569          * the PM may not latch a new signature.
570          *
571          * NOTE: This cannot be safely done between the first and second
572          *       softreset FISs.  It's now or never.
573          */
574         if (ahci_pm_phy_status(ap, target, &data)) {
575                 kprintf("%s: (B)Cannot clear phy status\n",
576                         ATANAME(ap ,at));
577         }
578         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
579
580         /*
581          * Prep first D2H command with SRST feature & clear busy/reset flags
582          *
583          * It is unclear which other fields in the FIS are used.  Just zero
584          * everything.
585          *
586          * When soft-resetting a port behind a multiplier at will be
587          * non-NULL, assigning it to the ccb prevents the port interrupt
588          * from hard-resetting the port if a problem crops up.
589          */
590         ccb = ahci_get_err_ccb(ap);
591         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
592         ccb->ccb_xa.complete = ahci_pm_dummy_done;
593         ccb->ccb_xa.at = at;
594
595         fis = ccb->ccb_cmd_table->cfis;
596         bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
597         fis[0] = ATA_FIS_TYPE_H2D;
598         fis[1] = at->at_target;
599         fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
600
601         cmd_slot = ccb->ccb_cmd_hdr;
602         cmd_slot->prdtl = 0;
603         cmd_slot->flags = htole16(5);   /* FIS length: 5 DWORDS */
604         cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
605         cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
606         cmd_slot->flags |= htole16(at->at_target <<
607                                    AHCI_CMD_LIST_FLAG_PMP_SHIFT);
608
609         ccb->ccb_xa.state = ATA_S_PENDING;
610
611         /*
612          * This soft reset of the AP target can cause a stream of IFS
613          * errors to occur.  Setting AP_F_IGNORE_IFS prevents the port
614          * from being hard reset (because its the target behind the
615          * port that isn't happy).
616          *
617          * The act of sending the soft reset can cause the target to
618          * blow the port up and generate IFS errors.
619          */
620         ap->ap_flags |= AP_F_IGNORE_IFS;
621         ap->ap_flags &= ~AP_F_IFS_IGNORED;
622
623         if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
624                 kprintf("%s: Soft-reset through PM failed, %s\n",
625                         ATANAME(ap, at),
626                         (count > 1 ? "retrying" : "giving up"));
627                 ahci_put_err_ccb(ccb);
628                 if (--count) {
629                         if (ap->ap_flags & AP_F_IFS_IGNORED)
630                                 ahci_os_sleep(5000);
631                         else
632                                 ahci_os_sleep(1000);
633                         ahci_pwrite(ap, AHCI_PREG_SERR, -1);
634                         ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
635                         goto retry;
636                 }
637                 goto err;
638         }
639
640         /*
641          * WARNING!  SENSITIVE TIME PERIOD!  WARNING!
642          *
643          * The first and second FISes are supposed to be back-to-back,
644          * I think the idea is to get the second sent and then after
645          * the device resets it will send a signature.  Do not delay
646          * here and most definitely do not issue any commands to other
647          * targets!
648          */
649
650         /*
651          * Prep second D2H command to read status and complete reset sequence
652          * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
653          * Rev 2.6 and it is unclear how the second FIS should be set up
654          * from the AHCI document.
655          *
656          * Give the device 3ms before sending the second FIS.
657          *
658          * It is unclear which other fields in the FIS are used.  Just zero
659          * everything.
660          */
661         bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
662         fis[0] = ATA_FIS_TYPE_H2D;
663         fis[1] = at->at_target;
664         fis[15] = ATA_FIS_CONTROL_4BIT;
665
666         cmd_slot->prdtl = 0;
667         cmd_slot->flags = htole16(5);   /* FIS length: 5 DWORDS */
668         cmd_slot->flags |= htole16(at->at_target <<
669                                    AHCI_CMD_LIST_FLAG_PMP_SHIFT);
670
671         ccb->ccb_xa.state = ATA_S_PENDING;
672         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
673
674         ap->ap_flags &= ~AP_F_IFS_IGNORED;
675
676         if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
677                 kprintf("%s: Soft-reset(2) through PM failed, %s\n",
678                         ATANAME(ap, at),
679                         (count > 1 ? "retrying" : "giving up"));
680                 if (--count) {
681                         ahci_os_sleep(1000);
682                         ahci_put_err_ccb(ccb);
683                         ahci_pwrite(ap, AHCI_PREG_SERR, -1);
684                         ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
685                         goto retry;
686                 }
687                 goto err;
688         }
689
690         ahci_put_err_ccb(ccb);
691         ahci_os_sleep(100);
692         ahci_pwrite(ap, AHCI_PREG_SERR, -1);
693         ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
694
695         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
696         if (ahci_pm_phy_status(ap, target, &data)) {
697                 kprintf("%s: (C)Cannot clear phy status\n",
698                         ATANAME(ap ,at));
699         }
700         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
701
702         /*
703          * Do it again, even if we think we got a good result
704          */
705         if (--count) {
706                 fis[15] = 0;
707                 goto retry;
708         }
709
710         /*
711          * If the softreset is trying to clear a BSY condition after a
712          * normal portreset we assign the port type.
713          *
714          * If the softreset is being run first as part of the ccb error
715          * processing code then report if the device signature changed
716          * unexpectedly.
717          */
718         if (at->at_type == ATA_PORT_T_NONE) {
719                 at->at_type = ahci_port_signature_detect(ap, at);
720         } else {
721                 if (ahci_port_signature_detect(ap, at) != at->at_type) {
722                         kprintf("%s: device signature unexpectedly "
723                                 "changed\n", ATANAME(ap, at));
724                         error = EBUSY; /* XXX */
725                 }
726         }
727         error = 0;
728
729         /*
730          * Who knows what kind of mess occured.  We have exclusive access
731          * to the port so try to clean up potential problems.
732          */
733 err:
734         ahci_os_sleep(100);
735
736         /*
737          * Clear error status so we can detect removal.
738          */
739         if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
740                 kprintf("%s: ahci_pm_softreset unable to clear SERR\n",
741                         ATANAME(ap, at));
742         }
743         ahci_pwrite(ap, AHCI_PREG_SERR, -1);
744         ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
745         ap->ap_flags &= ~(AP_F_IGNORE_IFS | AP_F_IFS_IGNORED);
746
747         at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
748         return (error);
749 }
750
751
752 /*
753  * Return the phy status for a target behind a port multiplier and
754  * reset SATA_PMREG_SERR.
755  *
756  * Returned bits follow AHCI_PREG_SSTS bits.  The AHCI_PREG_SSTS_SPD
757  * bits can be used to determine the link speed and will be 0 if there
758  * is no link.
759  *
760  * 0 is returned if any communications error occurs.
761  */
762 int
763 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap)
764 {
765         int error;
766
767         error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap);
768         if (error == 0)
769                 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
770         if (error)
771                 *datap = 0;
772         return(error);
773 }
774
775 /*
776  * Check that a target is still good.
777  */
778 void
779 ahci_pm_check_good(struct ahci_port *ap, int target)
780 {
781         struct ata_port *at;
782         u_int32_t data;
783
784         /*
785          * It looks like we might have to read the EINFO register
786          * to allow the PM to generate a new event.
787          */
788         if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
789                 kprintf("%s: Port multiplier EINFO could not be read\n",
790                         PORTNAME(ap));
791         }
792
793         if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
794                 kprintf("%s: Port multiplier: SERR could not be cleared\n",
795                         PORTNAME(ap));
796         }
797
798         if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
799                 return;
800         at = ap->ap_ata[target];
801
802         /*
803          * If the device needs an init or hard reset also make sure the
804          * PHY is turned on.
805          */
806         if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
807                 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
808                 ahci_pm_hardreset(ap, target, 1);
809         }
810
811         /*
812          * Read the detect status
813          */
814         if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
815                 kprintf("%s: Unable to access PM SSTS register target %d\n",
816                         PORTNAME(ap), target);
817                 return;
818         }
819         if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) {
820                 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
821                 if (at->at_probe != ATA_PROBE_FAILED) {
822                         at->at_probe = ATA_PROBE_FAILED;
823                         at->at_type = ATA_PORT_T_NONE;
824                         at->at_features |= ATA_PORT_F_RESCAN;
825                         kprintf("%s: HOTPLUG (PM) - Device removed\n",
826                                 ATANAME(ap, at));
827                 }
828         } else {
829                 if (at->at_probe == ATA_PROBE_FAILED) {
830                         at->at_probe = ATA_PROBE_NEED_HARD_RESET;
831                         at->at_features |= ATA_PORT_F_RESCAN;
832                         kprintf("%s: HOTPLUG (PM) - Device inserted\n",
833                                 ATANAME(ap, at));
834                 }
835         }
836 }
837
838 /*
839  * Read a PM register
840  */
841 int
842 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap)
843 {
844         struct ata_xfer *xa;
845         int error;
846
847         xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
848
849         xa->fis->type = ATA_FIS_TYPE_H2D;
850         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
851         xa->fis->command = ATA_C_READ_PM;
852         xa->fis->features = which;
853         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
854         xa->fis->control = ATA_FIS_CONTROL_4BIT;
855
856         xa->complete = ahci_pm_dummy_done;
857         xa->datalen = 0;
858         xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
859         xa->timeout = 1000;
860
861         if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) {
862                 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) |
863                        (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24);
864                 error = 0;
865         } else {
866                 kprintf("%s.%d pm_read SCA[%d] failed\n",
867                         PORTNAME(ap), target, which);
868                 *datap = 0;
869                 error = EIO;
870         }
871         ahci_ata_put_xfer(xa);
872         return (error);
873 }
874
875 /*
876  * Write a PM register
877  */
878 int
879 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data)
880 {
881         struct ata_xfer *xa;
882         int error;
883
884         xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
885
886         xa->fis->type = ATA_FIS_TYPE_H2D;
887         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
888         xa->fis->command = ATA_C_WRITE_PM;
889         xa->fis->features = which;
890         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
891         xa->fis->sector_count = (u_int8_t)data;
892         xa->fis->lba_low = (u_int8_t)(data >> 8);
893         xa->fis->lba_mid = (u_int8_t)(data >> 16);
894         xa->fis->lba_high = (u_int8_t)(data >> 24);
895         xa->fis->control = ATA_FIS_CONTROL_4BIT;
896
897         xa->complete = ahci_pm_dummy_done;
898         xa->datalen = 0;
899         xa->flags = ATA_F_POLL;
900         xa->timeout = 1000;
901
902         if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
903                 error = 0;
904         else
905                 error = EIO;
906         ahci_ata_put_xfer(xa);
907         return(error);
908 }
909
910 /*
911  * Dummy done callback for xa.
912  */
913 static void
914 ahci_pm_dummy_done(struct ata_xfer *xa)
915 {
916 }
917