Merge branch 'master' of /repository/git/dragonfly
[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;
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         if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
156                 kprintf("%s: PMPROBE First FIS failed,\n",
157                         PORTNAME(ap));
158                 kprintf("%s: PMPROBE No Port Multiplier was found.\n",
159                         PORTNAME(ap));
160                 if (--count) {
161                         ahci_put_err_ccb(ccb);
162                         goto retry;
163                 }
164                 error = EBUSY;
165                 goto err;
166         }
167         if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
168                                AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
169                 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
170         }
171
172         /*
173          * The device may have muffed up the PHY when it reset.
174          */
175         ahci_os_sleep(100);
176         ahci_flush_tfd(ap);
177         ahci_pwrite(ap, AHCI_PREG_SERR, -1);
178         /* ahci_pm_phy_status(ap, 15, &cmd); */
179
180         /*
181          * Prep second D2H command to read status and complete reset sequence
182          * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
183          * Rev 2.6 and it is unclear how the second FIS should be set up
184          * from the AHCI document.
185          *
186          * Give the device 3ms before sending the second FIS.
187          *
188          * It is unclear which other fields in the FIS are used.  Just zero
189          * everything.
190          */
191         ccb->ccb_xa.flags = ATA_F_POLL;
192
193         bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
194         fis[0] = ATA_FIS_TYPE_H2D;
195         fis[1] = 0x0F;
196         fis[15] = ATA_FIS_CONTROL_4BIT;
197
198         cmd_slot->prdtl = 0;
199         cmd_slot->flags = htole16(5);   /* FIS length: 5 DWORDS */
200         cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
201
202         ccb->ccb_xa.state = ATA_S_PENDING;
203
204         /*
205          * The only way one can determine if a port multiplier is on the
206          * port is to probe target 15, and of course this will fail if
207          * there is no port multiplier.
208          *
209          * The probing has to be done whether or not a device is probed on
210          * target 0, because when a PM is attached target 0 represents
211          * slot #0 behind the PM.
212          */
213         if (ahci_poll(ccb, 5000, ahci_quick_timeout) != ATA_S_COMPLETE) {
214                 kprintf("%s: PMPROBE Second FIS failed,\n",
215                         PORTNAME(ap));
216                 kprintf("%s: PMPROBE No Port Multiplier was found.\n",
217                         PORTNAME(ap));
218                 if (--count) {
219                         ahci_put_err_ccb(ccb);
220                         goto retry;
221                 }
222                 error = EBUSY;
223                 goto err;
224         }
225
226         /*
227          * What? We succeeded?  Yup, but for some reason the signature
228          * is still latched from the original detect (that saw target 0
229          * behind the PM), and I don't know how to clear the condition
230          * other then by retrying the whole reset sequence.
231          */
232         if (--count) {
233                 fis[15] = 0;
234                 ahci_put_err_ccb(ccb);
235                 goto retry;
236         }
237
238         /*
239          * Get the signature.  The caller sets the ap fields.
240          */
241         if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
242                 ap->ap_ata[15]->at_probe = ATA_PROBE_GOOD;
243                 error = 0;
244         } else {
245                 error = EBUSY;
246         }
247
248         /*
249          * Fall through / clean up the CCB and perform error processing.
250          */
251 err:
252         if (ccb != NULL)
253                 ahci_put_err_ccb(ccb);
254
255         if (error == 0 && ahci_pm_identify(ap)) {
256                 kprintf("%s: PM - cannot identify port multiplier\n",
257                         PORTNAME(ap));
258                 error = EBUSY;
259         }
260
261         /*
262          * If we probed the PM reset the state for the targets behind
263          * it so they get probed by the state machine.
264          */
265         if (error == 0) {
266                 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
267                         at = ap->ap_ata[i];
268                         at->at_probe = ATA_PROBE_NEED_INIT;
269                         at->at_features |= ATA_PORT_F_RESCAN;
270                 }
271                 ap->ap_type = ATA_PORT_T_PM;
272                 return (0);
273         }
274
275         /*
276          * If we failed turn off PMA, otherwise identify the port multiplier.
277          * CAM will iterate the devices.
278          */
279         ahci_port_stop(ap, 0);
280         ahci_port_clo(ap);
281         cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
282         cmd &= ~AHCI_PREG_CMD_PMA;
283         ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
284         ahci_port_init(ap);
285         if (orig_error == 0) {
286                 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
287                             AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
288                         kprintf("%s: PM probe: port will not come ready\n",
289                                 PORTNAME(ap));
290                         orig_error = EBUSY;
291                 }
292         }
293         return(orig_error);
294 }
295
296 /*
297  * Identify the port multiplier
298  */
299 int
300 ahci_pm_identify(struct ahci_port *ap)
301 {
302         u_int32_t chipid;
303         u_int32_t rev;
304         u_int32_t nports;
305         u_int32_t data1;
306         u_int32_t data2;
307         int       has_dummy_port;
308
309         ap->ap_probe = ATA_PROBE_FAILED;
310         if (ahci_pm_read(ap, 15, 0, &chipid))
311                 goto err;
312         if (ahci_pm_read(ap, 15, 1, &rev))
313                 goto err;
314         if (ahci_pm_read(ap, 15, 2, &nports))
315                 goto err;
316         nports &= 0x0000000F;   /* only the low 4 bits */
317         ap->ap_probe = ATA_PROBE_GOOD;
318
319         /*
320          * Ignore fake port on PMs which have it.  We can probe it but the
321          * softreset will probably fail.
322          */
323         switch(chipid) {
324         case 0x37261095:
325                 has_dummy_port = 1;
326                 break;
327         default:
328                 has_dummy_port = 0;
329                 break;
330         }
331         if (has_dummy_port) {
332                 if (nports > 1)
333                         --nports;
334         }
335
336         kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
337                 PORTNAME(ap),
338                 chipid,
339                 rev, SATA_PFMT_PM_REV,
340                 nports);
341         if (has_dummy_port) {
342                 kprintf("%s: Port multiplier: Ignoring dummy port #%d\n",
343                 PORTNAME(ap), nports);
344         }
345         ap->ap_pmcount = nports;
346
347         if (ahci_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) {
348                 kprintf("%s: Port multiplier: Warning, "
349                         "cannot read feature register\n", PORTNAME(ap));
350         } else {
351                 kprintf("%s: Port multiplier features: 0x%b\n",
352                         PORTNAME(ap),
353                         data1,
354                         SATA_PFMT_PM_FEA);
355         }
356         if (ahci_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
357                 kprintf("%s: Port multiplier defaults: 0x%b\n",
358                         PORTNAME(ap),
359                         data2,
360                         SATA_PFMT_PM_FEA);
361         }
362
363         /*
364          * Turn on async notification if we support and the PM supports it.
365          * This allows the PM to forward async notification events to us and
366          * it will also generate an event for target 15 for hot-plug events
367          * (or is supposed to anyway).
368          */
369         if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) &&
370             (data1 & SATA_PMFEA_ASYNCNOTIFY)) {
371                 u_int32_t serr_bits = AHCI_PREG_SERR_DIAG_N |
372                                       AHCI_PREG_SERR_DIAG_X;
373                 data2 |= SATA_PMFEA_ASYNCNOTIFY;
374                 if (ahci_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) {
375                         kprintf("%s: Port multiplier: AsyncNotify cannot be "
376                                 "enabled\n", PORTNAME(ap));
377                 } else if (ahci_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) {
378                         kprintf("%s: Port mulltiplier: AsyncNotify unable "
379                                 "to enable error info bits\n", PORTNAME(ap));
380                 } else {
381                         kprintf("%s: Port multiplier: AsyncNotify enabled\n",
382                                 PORTNAME(ap));
383                 }
384         }
385
386         return (0);
387 err:
388         kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
389         return (EIO);
390 }
391
392 /*
393  * Do a COMRESET sequence on the target behind a port multiplier.
394  *
395  * If hard is 2 we also cycle the phy on the target.
396  *
397  * This must be done prior to any softreset or probe attempts on
398  * targets behind the port multiplier.
399  *
400  * Returns 0 on success or an error.
401  */
402 int
403 ahci_pm_hardreset(struct ahci_port *ap, int target, int hard)
404 {
405         struct ata_port *at;
406         u_int32_t data;
407         int loop;
408         int error = EIO;
409
410         at = ap->ap_ata[target];
411
412         /*
413          * Turn off power management and kill the phy on the target
414          * if requested.  Hold state for 10ms.
415          */
416         data = AHCI_PREG_SCTL_IPM_DISABLED;
417         if (hard == 2)
418                 data |= AHCI_PREG_SCTL_DET_DISABLE;
419         if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1))
420                 goto err;
421         if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
422                 goto err;
423         ahci_os_sleep(10);
424
425         /*
426          * Start transmitting COMRESET.  COMRESET must be sent for at
427          * least 1ms.
428          */
429         at->at_probe = ATA_PROBE_FAILED;
430         at->at_type = ATA_PORT_T_NONE;
431         data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
432         if (AhciForceGen1 & (1 << ap->ap_num)) {
433                 kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target);
434                 data |= AHCI_PREG_SCTL_SPD_GEN1;
435         } else {
436                 data |= AHCI_PREG_SCTL_SPD_ANY;
437         }
438         if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
439                 goto err;
440
441         /*
442          * It takes about 100ms for the DET logic to settle down,
443          * from trial and error testing.  If this is too short
444          * the softreset code will fail.
445          */
446         ahci_os_sleep(100);
447
448         if (ahci_pm_phy_status(ap, target, &data)) {
449                 kprintf("%s: (A)Cannot clear phy status\n",
450                         ATANAME(ap ,at));
451         }
452
453         /*
454          * Flush any status, then clear DET to initiate negotiation.
455          */
456         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
457         data = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_NONE;
458         if (ahci_pm_write(ap, target, SATA_PMREG_SCTL, data))
459                 goto err;
460
461         /*
462          * Try to determine if there is a device on the port.
463          *
464          * Give the device 3/10 second to at least be detected.
465          * If we fail clear any pending status since we may have
466          * cycled the phy and probably caused another PRCS interrupt.
467          */
468         for (loop = 3; loop; --loop) {
469                 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data))
470                         goto err;
471                 if (data & AHCI_PREG_SSTS_DET)
472                         break;
473                 ahci_os_sleep(100);
474         }
475         if (loop == 0) {
476                 kprintf("%s.%d: Port appears to be unplugged\n",
477                         PORTNAME(ap), target);
478                 error = ENODEV;
479                 goto err;
480         }
481
482         /*
483          * There is something on the port.  Give the device 3 seconds
484          * to fully negotiate.
485          */
486         for (loop = 30; loop; --loop) {
487                 if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data))
488                         goto err;
489                 if ((data & AHCI_PREG_SSTS_DET) == AHCI_PREG_SSTS_DET_DEV)
490                         break;
491                 ahci_os_sleep(100);
492         }
493
494         /*
495          * Device not detected
496          */
497         if (loop == 0) {
498                 kprintf("%s: Device may be powered down\n",
499                         PORTNAME(ap));
500                 error = ENODEV;
501                 goto err;
502         }
503
504         /*
505          * Device detected
506          */
507         kprintf("%s.%d: Device detected data=%08x\n",
508                 PORTNAME(ap), target, data);
509         /*
510          * Clear SERR on the target so we get a new NOTIFY event if a hot-plug
511          * or hot-unplug occurs.
512          */
513         ahci_os_sleep(100);
514
515         error = 0;
516 err:
517         at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
518         return (error);
519 }
520
521 /*
522  * AHCI soft reset through port multiplier.
523  *
524  * This function keeps port communications intact and attempts to generate
525  * a reset to the connected device using device commands.  Unlike
526  * hard-port operations we can't do fancy stop/starts or stuff like
527  * that without messing up other commands that might be running or
528  * queued.
529  */
530 int
531 ahci_pm_softreset(struct ahci_port *ap, int target)
532 {
533         struct ata_port         *at;
534         struct ahci_ccb         *ccb;
535         struct ahci_cmd_hdr     *cmd_slot;
536         u_int8_t                *fis;
537         int                     count;
538         int                     error;
539         u_int32_t               data;
540         int                     tried_longer;
541
542         error = EIO;
543         at = ap->ap_ata[target];
544
545         DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
546
547         count = 2;
548         tried_longer = 0;
549 retry:
550         /*
551          * Try to clear the phy so we get a good signature, otherwise
552          * the PM may not latch a new signature.
553          *
554          * NOTE: This cannot be safely done between the first and second
555          *       softreset FISs.  It's now or never.
556          */
557 #if 1
558         if (ahci_pm_phy_status(ap, target, &data)) {
559                 kprintf("%s: (B)Cannot clear phy status\n",
560                         ATANAME(ap ,at));
561         }
562         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
563 #endif
564
565         /*
566          * Prep first D2H command with SRST feature & clear busy/reset flags
567          *
568          * It is unclear which other fields in the FIS are used.  Just zero
569          * everything.
570          *
571          * When soft-resetting a port behind a multiplier at will be
572          * non-NULL, assigning it to the ccb prevents the port interrupt
573          * from hard-resetting the port if a problem crops up.
574          */
575         ccb = ahci_get_err_ccb(ap);
576         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
577         ccb->ccb_xa.complete = ahci_pm_dummy_done;
578         ccb->ccb_xa.at = at;
579
580         fis = ccb->ccb_cmd_table->cfis;
581         bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
582         fis[0] = ATA_FIS_TYPE_H2D;
583         fis[1] = at->at_target;
584         fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
585
586         cmd_slot = ccb->ccb_cmd_hdr;
587         cmd_slot->prdtl = 0;
588         cmd_slot->flags = htole16(5);   /* FIS length: 5 DWORDS */
589         cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
590         cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
591         cmd_slot->flags |= htole16(at->at_target <<
592                                    AHCI_CMD_LIST_FLAG_PMP_SHIFT);
593
594         ccb->ccb_xa.state = ATA_S_PENDING;
595
596         /*
597          * XXX hack to ignore IFS errors which can occur during the target
598          *     device's reset.
599          *
600          *     If an IFS error occurs the target is probably powering up,
601          *     so we try for a longer period of time.
602          */
603         ap->ap_flags |= AP_F_IGNORE_IFS;
604         ap->ap_flags &= ~(AP_F_IFS_IGNORED | AP_F_IFS_OCCURED);
605
606         if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
607                 kprintf("%s: (PM) First FIS failed\n", ATANAME(ap, at));
608                 if (ap->ap_flags & AP_F_IFS_OCCURED) {
609                         if (tried_longer == 0)
610                                 count += 4;
611                         ++tried_longer;
612                 }
613                 ahci_put_err_ccb(ccb);
614                 if (--count)
615                         goto retry;
616                 goto err;
617         }
618
619         /*
620          * WARNING!  SENSITIVE TIME PERIOD!  WARNING!
621          *
622          * The first and second FISes are supposed to be back-to-back,
623          * I think the idea is to get the second sent and then after
624          * the device resets it will send a signature.  Do not delay
625          * here and most definitely do not issue any commands to other
626          * targets!
627          */
628
629         /*
630          * Prep second D2H command to read status and complete reset sequence
631          * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
632          * Rev 2.6 and it is unclear how the second FIS should be set up
633          * from the AHCI document.
634          *
635          * Give the device 3ms before sending the second FIS.
636          *
637          * It is unclear which other fields in the FIS are used.  Just zero
638          * everything.
639          */
640         bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
641         fis[0] = ATA_FIS_TYPE_H2D;
642         fis[1] = at->at_target;
643         fis[15] = ATA_FIS_CONTROL_4BIT;
644
645         cmd_slot->prdtl = 0;
646         cmd_slot->flags = htole16(5);   /* FIS length: 5 DWORDS */
647         cmd_slot->flags |= htole16(at->at_target <<
648                                    AHCI_CMD_LIST_FLAG_PMP_SHIFT);
649
650         ccb->ccb_xa.state = ATA_S_PENDING;
651         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
652
653         if (ahci_poll(ccb, 1000, ahci_ata_cmd_timeout) != ATA_S_COMPLETE) {
654                 kprintf("%s: (PM) Second FIS failed\n", ATANAME(ap, at));
655                 ahci_put_err_ccb(ccb);
656 #if 1
657                 if (--count)
658                         goto retry;
659 #endif
660                 goto err;
661         }
662
663         ahci_put_err_ccb(ccb);
664         ahci_os_sleep(100);
665         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
666         if (ahci_pm_phy_status(ap, target, &data)) {
667                 kprintf("%s: (C)Cannot clear phy status\n",
668                         ATANAME(ap ,at));
669         }
670         ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
671
672         /*
673          * Do it again, even if we think we got a good result
674          */
675         if (--count) {
676                 fis[15] = 0;
677                 goto retry;
678         }
679
680         /*
681          * If the softreset is trying to clear a BSY condition after a
682          * normal portreset we assign the port type.
683          *
684          * If the softreset is being run first as part of the ccb error
685          * processing code then report if the device signature changed
686          * unexpectedly.
687          */
688         if (at->at_type == ATA_PORT_T_NONE) {
689                 at->at_type = ahci_port_signature_detect(ap, at);
690         } else {
691                 if (ahci_port_signature_detect(ap, at) != at->at_type) {
692                         kprintf("%s: device signature unexpectedly "
693                                 "changed\n", ATANAME(ap, at));
694                         error = EBUSY; /* XXX */
695                 }
696         }
697         error = 0;
698
699         /*
700          * Who knows what kind of mess occured.  We have exclusive access
701          * to the port so try to clean up potential problems.
702          */
703         ahci_os_sleep(100);
704 err:
705         /*
706          * Clear error status so we can detect removal.
707          */
708         if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
709                 kprintf("%s: ahci_pm_softreset unable to clear SERR\n",
710                         ATANAME(ap, at));
711                 ap->ap_flags &= ~AP_F_IGNORE_IFS;
712         }
713 /*      ahci_pwrite(ap, AHCI_PREG_SERR, -1);*/
714
715         at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
716         return (error);
717 }
718
719
720 /*
721  * Return the phy status for a target behind a port multiplier and
722  * reset SATA_PMREG_SERR.
723  *
724  * Returned bits follow AHCI_PREG_SSTS bits.  The AHCI_PREG_SSTS_SPD
725  * bits can be used to determine the link speed and will be 0 if there
726  * is no link.
727  *
728  * 0 is returned if any communications error occurs.
729  */
730 int
731 ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap)
732 {
733         int error;
734
735         error = ahci_pm_read(ap, target, SATA_PMREG_SSTS, datap);
736         if (error == 0)
737                 error = ahci_pm_write(ap, target, SATA_PMREG_SERR, -1);
738         if (error)
739                 *datap = 0;
740         return(error);
741 }
742
743 /*
744  * Check that a target is still good.
745  */
746 void
747 ahci_pm_check_good(struct ahci_port *ap, int target)
748 {
749         struct ata_port *at;
750         u_int32_t data;
751
752         /*
753          * It looks like we might have to read the EINFO register
754          * to allow the PM to generate a new event.
755          */
756         if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
757                 kprintf("%s: Port multiplier EINFO could not be read\n",
758                         PORTNAME(ap));
759         }
760
761         if (ahci_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
762                 kprintf("%s: Port multiplier: SERR could not be cleared\n",
763                         PORTNAME(ap));
764         }
765
766         if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
767                 return;
768         at = ap->ap_ata[target];
769
770         /*
771          * If the device needs an init or hard reset also make sure the
772          * PHY is turned on.
773          */
774         if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
775                 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
776                 ahci_pm_hardreset(ap, target, 1);
777         }
778
779         /*
780          * Read the detect status
781          */
782         if (ahci_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
783                 kprintf("%s: Unable to access PM SSTS register target %d\n",
784                         PORTNAME(ap), target);
785                 return;
786         }
787         if ((data & AHCI_PREG_SSTS_DET) != AHCI_PREG_SSTS_DET_DEV) {
788                 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
789                 if (at->at_probe != ATA_PROBE_FAILED) {
790                         at->at_probe = ATA_PROBE_FAILED;
791                         at->at_type = ATA_PORT_T_NONE;
792                         at->at_features |= ATA_PORT_F_RESCAN;
793                         kprintf("%s: HOTPLUG (PM) - Device removed\n",
794                                 ATANAME(ap, at));
795                 }
796         } else {
797                 if (at->at_probe == ATA_PROBE_FAILED) {
798                         at->at_probe = ATA_PROBE_NEED_HARD_RESET;
799                         at->at_features |= ATA_PORT_F_RESCAN;
800                         kprintf("%s: HOTPLUG (PM) - Device inserted\n",
801                                 ATANAME(ap, at));
802                 }
803         }
804 }
805
806 /*
807  * Read a PM register
808  */
809 int
810 ahci_pm_read(struct ahci_port *ap, int target, int which, u_int32_t *datap)
811 {
812         struct ata_xfer *xa;
813         int error;
814
815         xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
816
817         xa->fis->type = ATA_FIS_TYPE_H2D;
818         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
819         xa->fis->command = ATA_C_READ_PM;
820         xa->fis->features = which;
821         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
822         xa->fis->control = ATA_FIS_CONTROL_4BIT;
823
824         xa->complete = ahci_pm_dummy_done;
825         xa->datalen = 0;
826         xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
827         xa->timeout = 1000;
828
829         if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) {
830                 *datap = xa->rfis.sector_count | (xa->rfis.lba_low << 8) |
831                        (xa->rfis.lba_mid << 16) | (xa->rfis.lba_high << 24);
832                 error = 0;
833         } else {
834                 kprintf("%s.%d pm_read SCA[%d] failed\n",
835                         PORTNAME(ap), target, which);
836                 *datap = 0;
837                 error = EIO;
838         }
839         ahci_ata_put_xfer(xa);
840         return (error);
841 }
842
843 /*
844  * Write a PM register
845  */
846 int
847 ahci_pm_write(struct ahci_port *ap, int target, int which, u_int32_t data)
848 {
849         struct ata_xfer *xa;
850         int error;
851
852         xa = ahci_ata_get_xfer(ap, ap->ap_ata[15]);
853
854         xa->fis->type = ATA_FIS_TYPE_H2D;
855         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
856         xa->fis->command = ATA_C_WRITE_PM;
857         xa->fis->features = which;
858         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
859         xa->fis->sector_count = (u_int8_t)data;
860         xa->fis->lba_low = (u_int8_t)(data >> 8);
861         xa->fis->lba_mid = (u_int8_t)(data >> 16);
862         xa->fis->lba_high = (u_int8_t)(data >> 24);
863         xa->fis->control = ATA_FIS_CONTROL_4BIT;
864
865         xa->complete = ahci_pm_dummy_done;
866         xa->datalen = 0;
867         xa->flags = ATA_F_POLL;
868         xa->timeout = 1000;
869
870         if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
871                 error = 0;
872         else
873                 error = EIO;
874         ahci_ata_put_xfer(xa);
875         return(error);
876 }
877
878 /*
879  * Dummy done callback for xa.
880  */
881 static void
882 ahci_pm_dummy_done(struct ata_xfer *xa)
883 {
884 }
885