xref: /dragonfly/sys/dev/disk/sili/sili_pm.c (revision a68e0df0)
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 	int	  has_dummy_port;
166 
167 	ap->ap_probe = ATA_PROBE_FAILED;
168 	if (sili_pm_read(ap, 15, 0, &chipid))
169 		goto err;
170 	if (sili_pm_read(ap, 15, 1, &rev))
171 		goto err;
172 	if (sili_pm_read(ap, 15, 2, &nports))
173 		goto err;
174 	nports &= 0x0000000F;	/* only the low 4 bits */
175 	ap->ap_probe = ATA_PROBE_GOOD;
176 
177 	/*
178 	 * Ignore fake port on PMs which have it.  We can probe it but the
179 	 * softreset will probably fail.
180 	 */
181 	switch(chipid) {
182 	case 0x37261095:
183 		has_dummy_port = 1;
184 		break;
185 	default:
186 		has_dummy_port = 0;
187 		break;
188 	}
189 	if (has_dummy_port) {
190 		if (nports > 1)
191 			--nports;
192 	}
193 
194 	kprintf("%s: Port multiplier: chip=%08x rev=0x%b nports=%d\n",
195 		PORTNAME(ap),
196 		chipid,
197 		rev, SATA_PFMT_PM_REV,
198 		nports);
199 	if (has_dummy_port) {
200 		kprintf("%s: Port multiplier: Ignoring dummy port #%d\n",
201 			PORTNAME(ap), nports);
202 	}
203 	ap->ap_pmcount = nports;
204 
205 	if (sili_pm_read(ap, 15, SATA_PMREG_FEA, &data1)) {
206 		kprintf("%s: Port multiplier: Warning, "
207 			"cannot read feature register\n", PORTNAME(ap));
208 	} else {
209 		kprintf("%s: Port multiplier features: 0x%b\n",
210 			PORTNAME(ap),
211 			data1,
212 			SATA_PFMT_PM_FEA);
213 	}
214 	if (sili_pm_read(ap, 15, SATA_PMREG_FEAEN, &data2) == 0) {
215 		kprintf("%s: Port multiplier defaults: 0x%b\n",
216 			PORTNAME(ap),
217 			data2,
218 			SATA_PFMT_PM_FEA);
219 	}
220 
221 	/*
222 	 * Turn on async notification if we support and the PM supports it.
223 	 * This allows the PM to forward async notification events to us and
224 	 * it will also generate an event for target 15 for hot-plug events
225 	 * (or is supposed to anyway).
226 	 */
227 	if ((ap->ap_sc->sc_flags & SILI_F_SSNTF) &&
228 	    (data1 & SATA_PMFEA_ASYNCNOTIFY)) {
229 		u_int32_t serr_bits = SATA_PM_SERR_DIAG_N |
230 				      SATA_PM_SERR_DIAG_X;
231 		data2 |= SATA_PMFEA_ASYNCNOTIFY;
232 		if (sili_pm_write(ap, 15, SATA_PMREG_FEAEN, data2)) {
233 			kprintf("%s: Port multiplier: AsyncNotify cannot be "
234 				"enabled\n", PORTNAME(ap));
235 		} else if (sili_pm_write(ap, 15, SATA_PMREG_EEENA, serr_bits)) {
236 			kprintf("%s: Port mulltiplier: AsyncNotify unable "
237 				"to enable error info bits\n", PORTNAME(ap));
238 		} else {
239 			kprintf("%s: Port multiplier: AsyncNotify enabled\n",
240 				PORTNAME(ap));
241 		}
242 	}
243 
244 	return (0);
245 err:
246 	kprintf("%s: Port multiplier cannot be identified\n", PORTNAME(ap));
247 	return (EIO);
248 }
249 
250 /*
251  * Do a COMRESET sequence on the target behind a port multiplier.
252  *
253  * If hard is 2 we also cycle the phy on the target.
254  *
255  * This must be done prior to any softreset or probe attempts on
256  * targets behind the port multiplier.
257  *
258  * Returns 0 on success or an error.
259  */
260 int
261 sili_pm_hardreset(struct sili_port *ap, int target, int hard)
262 {
263 	struct ata_port *at;
264 	u_int32_t data;
265 	int loop;
266 	int error = EIO;
267 
268 	at = &ap->ap_ata[target];
269 
270 	/*
271 	 * Ensure that no other commands are pending.  Our HW reset of
272 	 * the PM target can skewer the port overall!
273 	 */
274 	sili_exclusive_access(ap);
275 
276 	/*
277 	 * Turn off power management and kill the phy on the target
278 	 * if requested.  Hold state for 10ms.
279 	 */
280 	data = SATA_PM_SCTL_IPM_DISABLED;
281 #if 0
282 	if (hard == 2)
283 		data |= SATA_PM_SCTL_DET_DISABLE;
284 #endif
285 	if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1))
286 		goto err;
287 	if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
288 		goto err;
289 	sili_os_sleep(10);
290 
291 	/*
292 	 * Start transmitting COMRESET.  COMRESET must be sent for at
293 	 * least 1ms.
294 	 *
295 	 * It takes about 100ms for the DET logic to settle down,
296 	 * from trial and error testing.  If this is too short
297 	 * the softreset code will fail.
298 	 *
299 	 * It is very important to allow the logic to settle before
300 	 * we issue any additional commands or the target will interfere
301 	 * with our PM commands.
302 	 */
303 	at->at_probe = ATA_PROBE_FAILED;
304 	at->at_type = ATA_PORT_T_NONE;
305 	data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_INIT;
306 	if (SiliForceGen1 & (1 << ap->ap_num)) {
307 		kprintf("%s.%d: Force 1.5GBits\n", PORTNAME(ap), target);
308 		data |= SATA_PM_SCTL_SPD_GEN1;
309 	} else {
310 		data |= SATA_PM_SCTL_SPD_ANY;
311 	}
312 	if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
313 		goto err;
314 	sili_os_sleep(100);
315 
316 	if (sili_pm_phy_status(ap, target, &data)) {
317 		kprintf("%s: (A)Cannot clear phy status\n",
318 			ATANAME(ap ,at));
319 	}
320 
321 	/*
322 	 * Flush any status, then clear DET to initiate negotiation.
323 	 *
324 	 * It is very important to allow the negotiation to settle before
325 	 * we issue any additional commands or the target will interfere
326 	 * with our PM commands.
327 	 */
328 	sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
329 	data = SATA_PM_SCTL_IPM_DISABLED | SATA_PM_SCTL_DET_NONE;
330 	if (sili_pm_write(ap, target, SATA_PMREG_SCTL, data))
331 		goto err;
332 	sili_os_sleep(100);
333 
334 	/*
335 	 * Try to determine if there is a device on the port.
336 	 *
337 	 * Give the device 3/10 second to at least be detected.
338 	 * If we fail clear any pending status since we may have
339 	 * cycled the phy and probably caused another PRCS interrupt.
340 	 */
341 	for (loop = 3; loop; --loop) {
342 		if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
343 			goto err;
344 		if (data & SATA_PM_SSTS_DET)
345 			break;
346 		sili_os_sleep(100);
347 	}
348 	if (loop == 0) {
349 		kprintf("%s.%d: Port appears to be unplugged\n",
350 			PORTNAME(ap), target);
351 		error = ENODEV;
352 		goto err;
353 	}
354 
355 	/*
356 	 * There is something on the port.  Give the device 3 seconds
357 	 * to fully negotiate.
358 	 */
359 	for (loop = 30; loop; --loop) {
360 		if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data))
361 			goto err;
362 		if ((data & SATA_PM_SSTS_DET) == SATA_PM_SSTS_DET_DEV)
363 			break;
364 		sili_os_sleep(100);
365 	}
366 
367 	/*
368 	 * Device not detected
369 	 */
370 	if (loop == 0) {
371 		kprintf("%s: Device may be powered down\n",
372 			PORTNAME(ap));
373 		error = ENODEV;
374 		goto err;
375 	}
376 
377 	/*
378 	 * Device detected.
379 	 *
380 	 * Wait 200ms to give the device time to send its first D2H FIS.
381 	 * If we do not wait long enough our softreset sequence can collide
382 	 * with the end of the device's reset sequence and brick the port.
383 	 * Some devices may need longer and we handle those cases in the
384 	 * pm softreset code.
385 	 *
386 	 * XXX Looks like we have to wait a lot longer.  If the Sili chip's
387 	 *     softreset fails due to a collision with the D2H FIS or the
388 	 *     unbusying it bricks the port.
389 	 *
390 	 * XXX how do we poll that particular target's BSY status via the
391 	 *     PM?
392 	 */
393 	kprintf("%s.%d: PM Device detected ssts=%08x\n",
394 		PORTNAME(ap), target, data);
395 	sili_os_sleep(5000);
396 
397 	error = 0;
398 err:
399 	at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_SOFT_RESET;
400 	return (error);
401 }
402 
403 /*
404  * SILI soft reset through port multiplier.
405  *
406  * This function generates a soft reset through the port multiplier,
407  * keeping port communications intact.
408  *
409  * The SII chip will do the whole mess for us.  However, the command
410  * can brick the port if the target is still busy from the previous
411  * COMRESET.
412  */
413 int
414 sili_pm_softreset(struct sili_port *ap, int target)
415 {
416 	struct ata_port		*at;
417 	struct sili_ccb		*ccb;
418 	struct sili_prb		*prb;
419 	int			error;
420 	u_int32_t		data;
421 	u_int32_t		sig;
422 	int			timeout;
423 
424 	error = EIO;
425 	at = &ap->ap_ata[target];
426 
427 	kprintf("%s: PM softreset\n", ATANAME(ap, at));
428 
429 	/*
430 	 * Prep the special soft-reset SII command.
431 	 */
432 	ccb = sili_get_err_ccb(ap);
433 	ccb->ccb_done = sili_pm_empty_done;
434 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE;
435 	ccb->ccb_xa.complete = sili_pm_dummy_done;
436 	ccb->ccb_xa.at = at;
437 
438 	prb = ccb->ccb_prb;
439 	bzero(&prb->prb_h2d, sizeof(prb->prb_h2d));
440 	prb->prb_h2d.flags = at->at_target;
441 	prb->prb_control = SILI_PRB_CTRL_SOFTRESET;
442 	prb->prb_override = 0;
443 	prb->prb_xfer_count = 0;
444 
445 	ccb->ccb_xa.state = ATA_S_PENDING;
446 
447 	timeout = (target == 15) ? 1000 : 8000;
448 
449 	/*
450 	 * NOTE: Must use sili_quick_timeout() because we hold the err_ccb
451 	 */
452 	if (sili_poll(ccb, timeout, sili_quick_timeout) != ATA_S_COMPLETE) {
453 		if (target != 15) {
454 			kprintf("%s: (PM) Softreset FIS failed\n",
455 				ATANAME(ap, at));
456 		}
457 		sili_put_err_ccb(ccb);
458 		goto err;
459 	}
460 
461 	sig = (prb->prb_d2h.lba_high << 24) |
462 	      (prb->prb_d2h.lba_mid << 16) |
463 	      (prb->prb_d2h.lba_low << 8) |
464 	      (prb->prb_d2h.sector_count);
465 	kprintf("%s: PM SOFTRESET SIGNATURE %08x\n", ATANAME(ap, at), sig);
466 
467 	sili_put_err_ccb(ccb);
468 
469 	/*
470 	 * Clear the phy status of the target so we can get a new event.
471 	 *
472 	 * Target 15 is the PM itself and these registers have
473 	 * different meanings.
474 	 */
475 	if (target != 15) {
476 		if (sili_pm_phy_status(ap, target, &data)) {
477 			kprintf("%s: (C)Cannot clear phy status\n",
478 				ATANAME(ap ,at));
479 		}
480 		sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
481 	}
482 
483 	/*
484 	 * If the softreset is trying to clear a BSY condition after a
485 	 * normal portreset we assign the port type.
486 	 *
487 	 * If the softreset is being run first as part of the ccb error
488 	 * processing code then report if the device signature changed
489 	 * unexpectedly.
490 	 */
491 	if (at->at_type == ATA_PORT_T_NONE) {
492 		at->at_type = sili_port_signature(ap, at, sig);
493 	} else {
494 		if (sili_port_signature(ap, at, sig) != at->at_type) {
495 			kprintf("%s: device signature unexpectedly "
496 				"changed\n", ATANAME(ap, at));
497 			error = EBUSY; /* XXX */
498 		}
499 	}
500 	error = 0;
501 err:
502 	/*
503 	 * Clear error status so we can detect removal.
504 	 *
505 	 * Target 15 is the PM itself and these registers have
506 	 * different meanings.
507 	 */
508 	kprintf("%s: PM softreset done error %d\n", ATANAME(ap, at), error);
509 	if (error == 0 && target != 15) {
510 		if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
511 			kprintf("%s: sili_pm_softreset unable to clear SERR\n",
512 				ATANAME(ap, at));
513 			ap->ap_flags &= ~AP_F_IGNORE_IFS;
514 		}
515 	}
516 
517 	at->at_probe = error ? ATA_PROBE_FAILED : ATA_PROBE_NEED_IDENT;
518 	return (error);
519 }
520 
521 
522 /*
523  * Return the phy status for a target behind a port multiplier and
524  * reset SATA_PMREG_SERR.
525  *
526  * Returned bits follow SILI_PREG_SSTS bits.  The SILI_PREG_SSTS_SPD
527  * bits can be used to determine the link speed and will be 0 if there
528  * is no link.
529  *
530  * 0 is returned if any communications error occurs.
531  */
532 int
533 sili_pm_phy_status(struct sili_port *ap, int target, u_int32_t *datap)
534 {
535 	int error;
536 
537 	error = sili_pm_read(ap, target, SATA_PMREG_SSTS, datap);
538 	if (error == 0)
539 		error = sili_pm_write(ap, target, SATA_PMREG_SERR, -1);
540 	if (error)
541 		*datap = 0;
542 	return(error);
543 }
544 
545 int
546 sili_pm_set_feature(struct sili_port *ap, int feature, int enable)
547 {
548 	struct ata_xfer	*xa;
549 	int error;
550 
551 	xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
552 
553 	xa->fis->type = ATA_FIS_TYPE_H2D;
554 	xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
555 	xa->fis->command = enable ? ATA_C_SATA_FEATURE_ENA :
556 				    ATA_C_SATA_FEATURE_DIS;
557 	xa->fis->sector_count = feature;
558 	xa->fis->control = ATA_FIS_CONTROL_4BIT;
559 
560 	xa->complete = sili_pm_dummy_done;
561 	xa->datalen = 0;
562 	xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
563 	xa->timeout = 1000;
564 
565 	if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
566 		error = 0;
567 	else
568 		error = EIO;
569 	sili_ata_put_xfer(xa);
570 	return(error);
571 }
572 
573 /*
574  * Check that a target is still good.
575  */
576 void
577 sili_pm_check_good(struct sili_port *ap, int target)
578 {
579 	struct ata_port *at;
580 	u_int32_t data;
581 
582 	/*
583 	 * It looks like we might have to read the EINFO register
584 	 * to allow the PM to generate a new event.
585 	 */
586 	if (sili_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
587 		kprintf("%s: Port multiplier EINFO could not be read\n",
588 			PORTNAME(ap));
589 	}
590 
591 	if (sili_pm_write(ap, target, SATA_PMREG_SERR, -1)) {
592 		kprintf("%s: Port multiplier: SERR could not be cleared\n",
593 			PORTNAME(ap));
594 	}
595 
596 	if (target == CAM_TARGET_WILDCARD || target >= ap->ap_pmcount)
597 		return;
598 	at = &ap->ap_ata[target];
599 
600 	/*
601 	 * If the device needs an init or hard reset also make sure the
602 	 * PHY is turned on.
603 	 */
604 	if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) {
605 		/*kprintf("%s DOHARD\n", ATANAME(ap, at));*/
606 		sili_pm_hardreset(ap, target, 1);
607 	}
608 
609 	/*
610 	 * Read the detect status
611 	 */
612 	if (sili_pm_read(ap, target, SATA_PMREG_SSTS, &data)) {
613 		kprintf("%s: Unable to access PM SSTS register target %d\n",
614 			PORTNAME(ap), target);
615 		return;
616 	}
617 	if ((data & SATA_PM_SSTS_DET) != SATA_PM_SSTS_DET_DEV) {
618 		/*kprintf("%s: DETECT %08x\n", ATANAME(ap, at), data);*/
619 		if (at->at_probe != ATA_PROBE_FAILED) {
620 			at->at_probe = ATA_PROBE_FAILED;
621 			at->at_type = ATA_PORT_T_NONE;
622 			at->at_features |= ATA_PORT_F_RESCAN;
623 			kprintf("%s: HOTPLUG (PM) - Device removed\n",
624 				ATANAME(ap, at));
625 		}
626 	} else {
627 		if (at->at_probe == ATA_PROBE_FAILED) {
628 			at->at_probe = ATA_PROBE_NEED_HARD_RESET;
629 			at->at_features |= ATA_PORT_F_RESCAN;
630 			kprintf("%s: HOTPLUG (PM) - Device inserted\n",
631 				ATANAME(ap, at));
632 		}
633 	}
634 }
635 
636 /*
637  * Read a PM register
638  */
639 int
640 sili_pm_read(struct sili_port *ap, int target, int which, u_int32_t *datap)
641 {
642 	struct ata_xfer	*xa;
643 	int error;
644 
645 	xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
646 
647 	xa->fis->type = ATA_FIS_TYPE_H2D;
648 	xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
649 	xa->fis->command = ATA_C_READ_PM;
650 	xa->fis->features = which;
651 	xa->fis->device = target | ATA_H2D_DEVICE_LBA;
652 	xa->fis->control = ATA_FIS_CONTROL_4BIT;
653 
654 	xa->complete = sili_pm_dummy_done;
655 	xa->datalen = 0;
656 	xa->flags = ATA_F_POLL | ATA_F_AUTOSENSE;
657 	xa->timeout = 1000;
658 
659 	if (sili_ata_cmd(xa) == ATA_S_COMPLETE) {
660 		*datap = xa->rfis->sector_count | (xa->rfis->lba_low << 8) |
661 		       (xa->rfis->lba_mid << 16) | (xa->rfis->lba_high << 24);
662 		error = 0;
663 	} else {
664 		kprintf("%s.%d pm_read SCA[%d] failed\n",
665 			PORTNAME(ap), target, which);
666 		*datap = 0;
667 		error = EIO;
668 	}
669 	sili_ata_put_xfer(xa);
670 	return (error);
671 }
672 
673 /*
674  * Write a PM register
675  */
676 int
677 sili_pm_write(struct sili_port *ap, int target, int which, u_int32_t data)
678 {
679 	struct ata_xfer	*xa;
680 	int error;
681 
682 	xa = sili_ata_get_xfer(ap, &ap->ap_ata[15]);
683 
684 	xa->fis->type = ATA_FIS_TYPE_H2D;
685 	xa->fis->flags = ATA_H2D_FLAGS_CMD | 15;
686 	xa->fis->command = ATA_C_WRITE_PM;
687 	xa->fis->features = which;
688 	xa->fis->device = target | ATA_H2D_DEVICE_LBA;
689 	xa->fis->sector_count = (u_int8_t)data;
690 	xa->fis->lba_low = (u_int8_t)(data >> 8);
691 	xa->fis->lba_mid = (u_int8_t)(data >> 16);
692 	xa->fis->lba_high = (u_int8_t)(data >> 24);
693 	xa->fis->control = ATA_FIS_CONTROL_4BIT;
694 
695 	xa->complete = sili_pm_dummy_done;
696 	xa->datalen = 0;
697 	xa->flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
698 	xa->timeout = 1000;
699 
700 	if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
701 		error = 0;
702 	else
703 		error = EIO;
704 	sili_ata_put_xfer(xa);
705 	return(error);
706 }
707 
708 /*
709  * Dummy done callback for xa.
710  */
711 static void
712 sili_pm_dummy_done(struct ata_xfer *xa)
713 {
714 }
715 
716 static void
717 sili_pm_empty_done(struct sili_ccb *ccb)
718 {
719 }
720