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