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