SILI - Fix port re-insertion probe problems when coming out of DET_INIT
[dragonfly.git] / sys / dev / disk / sili / sili_pm.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include "sili.h"
36
37 static void sili_pm_dummy_done(struct ata_xfer *xa);
38 static void sili_pm_empty_done(struct sili_ccb *ccb);
39
40 /*
41  * This is called for PM attachments and hot-plug insertion events, and
42  * typically not called again until after an unplug/replug sequence.
43  *
44  * We just fall through to the hard-reset code, we don't need to
45  * set up any initial conditions.
46  */
47 int
48 sili_pm_port_init(struct sili_port *ap, struct ata_port *at)
49 {
50         at->at_probe = ATA_PROBE_NEED_HARD_RESET;
51         return (0);
52 }
53
54 /*
55  * This is called from the port hardreset code.
56  */
57 int
58 sili_pm_port_probe(struct sili_port *ap, int orig_error)
59 {
60         struct ata_port *at;
61         int error;
62         int i;
63
64         /*
65          * Clean up the port state machine
66          */
67         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_PMA);
68         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
69         if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
70                 kprintf("%s: PM probe: unable to init port\n",
71                         PORTNAME(ap));
72                 return (EBUSY);
73         }
74         if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
75                 kprintf("%s: PM probe: port will not come ready\n",
76                         PORTNAME(ap));
77                 return (EBUSY);
78         }
79
80         /*
81          * Issue a soft-reset of target 15
82          */
83         ap->ap_state = AP_S_NORMAL;
84         sili_pwrite(ap, SILI_PREG_SERR, -1);
85         error = sili_pm_softreset(ap, 15);
86
87         if (error == 0)
88                 error = sili_pm_identify(ap);
89
90         /*
91          * Finalize.  If the softreset failed.  Re-init the port
92          * state machine again so the normal non-PM softreset does
93          * not bog down.
94          */
95         if (error == 0) {
96                 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
97                         at = &ap->ap_ata[i];
98                         at->at_probe = ATA_PROBE_NEED_INIT;
99                         at->at_features |= ATA_PORT_F_RESCAN;
100                         at->at_features &= ~ATA_PORT_F_READLOG;
101                 }
102                 ap->ap_type = ATA_PORT_T_PM;
103                 return (0);
104         }
105
106         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
107         sili_port_init(ap);
108         if (orig_error == 0) {
109                 if (sili_pwait_set_to(ap, 5000, SILI_PREG_STATUS,
110                                       SILI_PREG_STATUS_READY)) {
111                         kprintf("%s: PM probe: port will not come ready\n",
112                                 PORTNAME(ap));
113                         orig_error = EBUSY;
114                 }
115         }
116         return (orig_error);
117
118 #if 0
119         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_RESUME);
120         sili_pwrite(ap, SILI_PREG_CTL_CLR, SILI_PREG_CTL_PMA);
121         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_INIT);
122         if (sili_pwait_clr_to(ap, 5000, SILI_PREG_STATUS, SILI_PREG_CTL_INIT)) {
123                 kprintf("%s: PM probe: unable to init port\n",
124                         PORTNAME(ap));
125                 orig_error = EBUSY;
126         }
127         if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
128                 kprintf("%s: PM probe: port will not come ready\n",
129                         PORTNAME(ap));
130                 orig_error = EBUSY;
131         }
132         kprintf("ORIG ERROR %d\n", orig_error);
133         if (orig_error)
134                 return (orig_error);
135
136         /*
137          * If we originally detected a device redo the device reset to
138          * try to clear the mess.
139          */
140         sili_pwrite(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET);
141         if (sili_pwait_clr(ap, SILI_PREG_CTL_SET, SILI_PREG_CTL_DEVRESET)) {
142                 kprintf("%s: PM probe: unable to reset\n", PORTNAME(ap));
143                 orig_error = EBUSY;
144         }
145         if (sili_pwait_set(ap, SILI_PREG_STATUS, SILI_PREG_STATUS_READY)) {
146                 kprintf("%s: PM probe: port will not come ready\n",
147                         PORTNAME(ap));
148                 orig_error = EBUSY;
149         }
150         return (orig_error);
151 #endif
152 }
153
154 /*
155  * Identify the port multiplier
156  */
157 int
158 sili_pm_identify(struct sili_port *ap)
159 {
160         u_int32_t chipid;
161         u_int32_t rev;
162         u_int32_t nports;
163         u_int32_t data1;
164         u_int32_t data2;
165
166         ap->ap_probe = ATA_PROBE_FAILED;
167         if (sili_pm_read(ap, 15, 0, &chipid))
168                 goto err;
169         if (sili_pm_read(ap, 15, 1, &rev))
170                 goto err;
171         if (sili_pm_read(ap, 15, 2, &nports))
172                 goto err;
173         nports &= 0x0000000F;   /* only the low 4 bits */
174         --nports;
175         ap->ap_probe = ATA_PROBE_GOOD;
176         kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
177                 PORTNAME(ap),
178                 chipid,
179                 rev, SATA_PFMT_PM_REV,
180                 nports);
181         ap->ap_pmcount = nports;
182
183         if (sili_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) {
184                 kprintf("%s: Port multiplier: Warning, "
185                         "cannot read feature register\n", PORTNAME(ap));
186         } else {
187                 kprintf("%s: Port multiplier features: 0x%b\n",
188                         PORTNAME(ap),
189                         data1,
190                         SATA_PFMT_PM_FEA);
191         }
192         if (sili_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
193                 kprintf("%s: Port multiplier defaults: 0x%b\n",
194                         PORTNAME(ap),
195                         data2,
196                         SATA_PFMT_PM_FEA);
197         }
198
199         /*
200          * Turn on async notification if we support and the PM supports it.
201          * This allows the PM to forward async notification events to us and
202          * it will also generate an event for target 15 for hot-plug events
203          * (or is supposed to anyway).
204          */
205         if ((ap->ap_sc->sc_flags & SILI_F_SSNTF) &&
206             (data1 & SATA_PMFEA_ASYNCNOTIFY)) {
207                 u_int32_t serr_bits = SATA_PM_SERR_DIAG_N |
208                                       SATA_PM_SERR_DIAG_X;
209                 data2 |= SATA_PMFEA_ASYNCNOTIFY;
210                 if (sili_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) {
211                         kprintf("%s: Port multiplier: AsyncNotify cannot be "
212                                 "enabled\n", PORTNAME(ap));
213                 } else if (sili_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) {
214                         kprintf("%s: Port mulltiplier: AsyncNotify unable "
215                                 "to enable error info bits\n", PORTNAME(ap));
216                 } else {
217                         kprintf("%s: Port multiplier: AsyncNotify enabled\n",
218                                 PORTNAME(ap));
219                 }
220         }
221
222         return (0);
223 err:
224         kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
225         return (EIO);
226 }
227
228 /*
229  * Do a COMRESET sequence on the target behind a port multiplier.
230  *
231  * If hard is 2 we also cycle the phy on the target.
232  *
233  * This must be done prior to any softreset or probe attempts on
234  * targets behind the port multiplier.
235  *
236  * Returns 0 on success or an error.
237  */
238 int
239 sili_pm_hardreset(struct sili_port *ap, int target, int hard)
240 {
241         struct ata_port *at;
242         u_int32_t data;
243         int loop;
244         int error = EIO;
245
246         at = &ap->ap_ata[target];
247
248         /*
249          * Turn off power management and kill the phy on the target
250          * if requested.  Hold state for 10ms.
251          */
252         data = SATA_PM_SCTL_IPM_DISABLED;
253         if (hard == 2)
254                 data |= SATA_PM_SCTL_DET_DISABLE;
255         if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1))
256                 goto err;
257         if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
258                 goto err;
259         sili_os_sleep(10);
260
261         /*
262          * Start transmitting COMRESET.  COMRESET must be sent for at
263          * least 1ms.
264          */
265         at->at_probe = ATA_PROBE_FAILED;
266         at->at_type = ATA_PORT_T_NONE;
267         data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_INIT;
268         if (SiliForceGen1 & (1 << ap->ap_num)) {
269                 kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target);
270                 data |= SATA_PM_SCTL_SPD_GEN1;
271         } else {
272                 data |= SATA_PM_SCTL_SPD_ANY;
273         }
274         if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
275                 goto err;
276
277         /*
278          * It takes about 100ms for the DET logic to settle down,
279          * from trial and error testing.  If this is too short
280          * the softreset code will fail.
281          */
282         sili_os_sleep(100);
283
284         if (sili_pm_phy_status(ap, target, &data)) {
285                 kprintf("%s: (A)Cannot clear phy status\n",
286                         ATANAME(ap ,at));
287         }
288
289         /*
290          * Flush any status, then clear DET to initiate negotiation.
291          *
292          * We need to give the phy layer a bit of time to settle down
293          * or we may catch a detection glitch instead of the actual
294          * device detect.
295          */
296         sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
297         data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_NONE;
298         if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
299                 goto err;
300         sili_os_sleep(10);
301
302         /*
303          * Try to determine if there is a device on the port.
304          *
305          * Give the device 3/10 second to at least be detected.
306          * If we fail clear any pending status since we may have
307          * cycled the phy and probably caused another PRCS interrupt.
308          */
309         for (loop = 3; loop; --loop) {
310                 if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
311                         goto err;
312                 if (data & SATA_PM_SSTS_DET)
313                         break;
314                 sili_os_sleep(100);
315         }
316         if (loop == 0) {
317                 kprintf("%s.%d: Port appears to be unplugged\n",
318                         PORTNAME(ap), target);
319                 error = ENODEV;
320                 goto err;
321         }
322
323         /*
324          * There is something on the port.  Give the device 3 seconds
325          * to fully negotiate.
326          */
327         for (loop = 30; loop; --loop) {
328                 if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
329                         goto err;
330                 if ((data & SATA_PM_SSTS_DET) == SATA_PM_SSTS_DET_DEV)
331                         break;
332                 sili_os_sleep(100);
333         }
334
335         /*
336          * Device not detected
337          */
338         if (loop == 0) {
339                 kprintf("%s: Device may be powered down\n",
340                         PORTNAME(ap));
341                 error = ENODEV;
342                 goto err;
343         }
344
345         /*
346          * Device detected.
347          *
348          * Wait 200ms to give the device time to send its first D2H FIS.
349          * If we do not wait long enough our softreset sequence can collide
350          * with the end of the device's reset sequence.
351          *
352          * XXX how do we poll that particular target's BSY status via the
353          *     PM?
354          */
355         kprintf("%s.%d: Device detected data=%08x\n",
356                 PORTNAME(ap), target, data);
357         sili_os_sleep(200);
358
359         error = 0;
360 err:
361         at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
362         return (error);
363 }
364
365 /*
366  * SILI soft reset through port multiplier.
367  *
368  * This function keeps port communications intact and attempts to generate
369  * a reset to the connected device using device commands.  Unlike
370  * hard-port operations we can't do fancy stop/starts or stuff like
371  * that without messing up other commands that might be running or
372  * queued.
373  *
374  * The SII chip will do the whole mess for us.
375  */
376 int
377 sili_pm_softreset(struct sili_port *ap, int target)
378 {
379         struct ata_port         *at;
380         struct sili_ccb         *ccb;
381         struct sili_prb         *prb;
382         int                     error;
383         u_int32_t               data;
384         u_int32_t               sig;
385         int                     timeout;
386
387         error = EIO;
388         at = &ap->ap_ata[target];
389
390         DPRINTF(SILI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
391
392         /*
393          * Prep the special soft-reset SII command.
394          */
395         ccb = sili_get_err_ccb(ap);
396         ccb->ccb_done = sili_pm_empty_done;
397         ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
398         ccb->ccb_xa.complete = sili_pm_dummy_done;
399         ccb->ccb_xa.at = at;
400
401         prb = ccb->ccb_prb;
402         bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
403         prb->prb_h2d.flags = at->at_target;
404         prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
405         prb->prb_override = 0;
406         prb->prb_xfer_count = 0;
407
408         ccb->ccb_xa.state = ATA_S_PENDING;
409
410         timeout = (target == 15) ? 1000 : 8000;
411
412         /*
413          * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
414          */
415         if (sili_poll(ccb, timeout, sili_quick_timeout) != ATA_S_COMPLETE) {
416                 if (target != 15) {
417                         kprintf("%s: (PM) Softreset FIS failed\n",
418                                 ATANAME(ap, at));
419                 }
420                 sili_put_err_ccb(ccb);
421                 goto err;
422         }
423
424         sig = (prb->prb_d2h.lba_high << 24) |
425               (prb->prb_d2h.lba_mid << 16) |
426               (prb->prb_d2h.lba_low << 8) |
427               (prb->prb_d2h.sector_count);
428         kprintf("%s: PM SOFTRESET SIGNATURE %08x\n", ATANAME(ap, at), sig);
429
430         sili_put_err_ccb(ccb);
431
432         /*
433          * Clear the phy status of the target so we can get a new event.
434          *
435          * Target 15 is the PM itself and these registers have
436          * different meanings.
437          */
438         if (target != 15) {
439                 if (sili_pm_phy_status(ap, target, &data)) {
440                         kprintf("%s: (C)Cannot clear phy status\n",
441                                 ATANAME(ap ,at));
442                 }
443                 sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
444         }
445
446         /*
447          * If the softreset is trying to clear a BSY condition after a
448          * normal portreset we assign the port type.
449          *
450          * If the softreset is being run first as part of the ccb error
451          * processing code then report if the device signature changed
452          * unexpectedly.
453          */
454         if (at->at_type == ATA_PORT_T_NONE) {
455                 at->at_type = sili_port_signature(ap, at, sig);
456         } else {
457                 if (sili_port_signature(ap, at, sig) != at->at_type) {
458                         kprintf("%s: device signature unexpectedly "
459                                 "changed\n", ATANAME(ap, at));
460                         error = EBUSY; /* XXX */
461                 }
462         }
463         error = 0;
464 err:
465         /*
466          * Clear error status so we can detect removal.
467          *
468          * Target 15 is the PM itself and these registers have
469          * different meanings.
470          */
471         if (error == 0 && target != 15) {
472                 if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
473                         kprintf("%s: sili_pm_softreset unable to clear SERR\n",
474                                 ATANAME(ap, at));
475                         ap->ap_flags &= ~AP_F_IGNORE_IFS;
476                 }
477         }
478
479         at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
480         return (error);
481 }
482
483
484 /*
485  * Return the phy status for a target behind a port multiplier and
486  * reset SATA_PMREG_SERR.
487  *
488  * Returned bits follow SILI_PREG_SSTS bits.  The SILI_PREG_SSTS_SPD
489  * bits can be used to determine the link speed and will be 0 if there
490  * is no link.
491  *
492  * 0 is returned if any communications error occurs.
493  */
494 int
495 sili_pm_phy_status(struct sili_port *ap, int target, u_int32_t *datap)
496 {
497         int error;
498
499         error = sili_pm_read(ap, target, SATA_PMREG_SSTS, datap);
500         if (error == 0)
501                 error = sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
502         if (error)
503                 *datap = 0;
504         return(error);
505 }
506
507 int
508 sili_pm_set_feature(struct sili_port *ap, int feature, int enable)
509 {
510         struct ata_xfer *xa;
511         int error;
512
513         xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
514
515         xa->fis->type = ATA_FIS_TYPE_H2D;
516         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
517         xa->fis->command = enable ? ATA_C_SATA_FEATURE_ENA :
518                                     ATA_C_SATA_FEATURE_DIS;
519         xa->fis->sector_count = feature;
520         xa->fis->control = ATA_FIS_CONTROL_4BIT;
521
522         xa->complete = sili_pm_dummy_done;
523         xa->datalen = 0;
524         xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
525         xa->timeout = 1000;
526
527         if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
528                 error = 0;
529         else
530                 error = EIO;
531         sili_ata_put_xfer(xa);
532         return(error);
533 }
534
535 /*
536  * Check that a target is still good.
537  */
538 void
539 sili_pm_check_good(struct sili_port *ap, int target)
540 {
541         struct ata_port *at;
542         u_int32_t data;
543
544         /*
545          * It looks like we might have to read the EINFO register
546          * to allow the PM to generate a new event.
547          */
548         if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
549                 kprintf("%s: Port multiplier EINFO could not be read\n",
550                         PORTNAME(ap));
551         }
552
553         if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
554                 kprintf("%s: Port multiplier: SERR could not be cleared\n",
555                         PORTNAME(ap));
556         }
557
558         if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
559                 return;
560         at = &ap->ap_ata[target];
561
562         /*
563          * If the device needs an init or hard reset also make sure the
564          * PHY is turned on.
565          */
566         if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
567                 /*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
568                 sili_pm_hardreset(ap, target, 1);
569         }
570
571         /*
572          * Read the detect status
573          */
574         if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
575                 kprintf("%s: Unable to access PM SSTS register target %d\n",
576                         PORTNAME(ap), target);
577                 return;
578         }
579         if ((data & SATA_PM_SSTS_DET) != SATA_PM_SSTS_DET_DEV) {
580                 /*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
581                 if (at->at_probe != ATA_PROBE_FAILED) {
582                         at->at_probe = ATA_PROBE_FAILED;
583                         at->at_type = ATA_PORT_T_NONE;
584                         at->at_features |= ATA_PORT_F_RESCAN;
585                         kprintf("%s: HOTPLUG (PM) - Device removed\n",
586                                 ATANAME(ap, at));
587                 }
588         } else {
589                 if (at->at_probe == ATA_PROBE_FAILED) {
590                         at->at_probe = ATA_PROBE_NEED_HARD_RESET;
591                         at->at_features |= ATA_PORT_F_RESCAN;
592                         kprintf("%s: HOTPLUG (PM) - Device inserted\n",
593                                 ATANAME(ap, at));
594                 }
595         }
596 }
597
598 /*
599  * Read a PM register
600  */
601 int
602 sili_pm_read(struct sili_port *ap, int target, int which, u_int32_t *datap)
603 {
604         struct ata_xfer *xa;
605         int error;
606
607         xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
608
609         xa->fis->type = ATA_FIS_TYPE_H2D;
610         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
611         xa->fis->command = ATA_C_READ_PM;
612         xa->fis->features = which;
613         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
614         xa->fis->control = ATA_FIS_CONTROL_4BIT;
615
616         xa->complete = sili_pm_dummy_done;
617         xa->datalen = 0;
618         xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
619         xa->timeout = 1000;
620
621         if (sili_ata_cmd(xa) == ATA_S_COMPLETE) {
622                 *datap = xa->rfis->sector_count | (xa->rfis->lba_low << 8) |
623                        (xa->rfis->lba_mid << 16) | (xa->rfis->lba_high << 24);
624                 error = 0;
625         } else {
626                 kprintf("%s.%d pm_read SCA[%d] failed\n",
627                         PORTNAME(ap), target, which);
628                 *datap = 0;
629                 error = EIO;
630         }
631         sili_ata_put_xfer(xa);
632         return (error);
633 }
634
635 /*
636  * Write a PM register
637  */
638 int
639 sili_pm_write(struct sili_port *ap, int target, int which, u_int32_t data)
640 {
641         struct ata_xfer *xa;
642         int error;
643
644         xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
645
646         xa->fis->type = ATA_FIS_TYPE_H2D;
647         xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
648         xa->fis->command = ATA_C_WRITE_PM;
649         xa->fis->features = which;
650         xa->fis->device = target | ATA_H2D_DEVICE_LBA;
651         xa->fis->sector_count = (u_int8_t)data;
652         xa->fis->lba_low = (u_int8_t)(data >> 8);
653         xa->fis->lba_mid = (u_int8_t)(data >> 16);
654         xa->fis->lba_high = (u_int8_t)(data >> 24);
655         xa->fis->control = ATA_FIS_CONTROL_4BIT;
656
657         xa->complete = sili_pm_dummy_done;
658         xa->datalen = 0;
659         xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
660         xa->timeout = 1000;
661
662         if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
663                 error = 0;
664         else
665                 error = EIO;
666         sili_ata_put_xfer(xa);
667         return(error);
668 }
669
670 /*
671  * Dummy done callback for xa.
672  */
673 static void
674 sili_pm_dummy_done(struct ata_xfer *xa)
675 {
676 }
677
678 static void
679 sili_pm_empty_done(struct sili_ccb *ccb)
680 {
681 }