2 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
19 * This code is derived from software contributed to The DragonFly Project
20 * by Matthew Dillon <dillon@backplane.com>
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in
30 * the documentation and/or other materials provided with the
32 * 3. Neither the name of The DragonFly Project nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific, prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
39 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
40 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
41 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
42 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
44 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
54 int ahci_port_start(struct ahci_port *ap);
55 int ahci_port_stop(struct ahci_port *ap, int stop_fis_rx);
56 int ahci_port_clo(struct ahci_port *ap);
57 void ahci_port_interrupt_enable(struct ahci_port *ap);
59 int ahci_load_prdt(struct ahci_ccb *);
60 void ahci_unload_prdt(struct ahci_ccb *);
61 static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
62 int nsegs, int error);
63 void ahci_start(struct ahci_ccb *);
64 int ahci_port_softreset(struct ahci_port *ap);
65 int ahci_port_pmprobe(struct ahci_port *ap);
66 int ahci_port_hardreset(struct ahci_port *ap, int hard);
67 void ahci_port_hardstop(struct ahci_port *ap);
68 void ahci_flush_tfd(struct ahci_port *ap);
70 static void ahci_ata_cmd_timeout_unserialized(void *);
71 void ahci_quick_timeout(struct ahci_ccb *ccb);
72 void ahci_check_active_timeouts(struct ahci_port *ap);
74 void ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at);
75 void ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at);
76 void ahci_issue_pending_ncq_commands(struct ahci_port *);
77 void ahci_issue_pending_commands(struct ahci_port *, int);
79 struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *);
80 void ahci_put_err_ccb(struct ahci_ccb *);
82 int ahci_port_read_ncq_error(struct ahci_port *, int *);
84 struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
85 void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
86 static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
88 void ahci_empty_done(struct ahci_ccb *ccb);
89 void ahci_ata_cmd_done(struct ahci_ccb *ccb);
91 /* Wait for all bits in _b to be cleared */
92 #define ahci_pwait_clr(_ap, _r, _b) \
93 ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
94 #define ahci_pwait_clr_to(_ap, _to, _r, _b) \
95 ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
97 /* Wait for all bits in _b to be set */
98 #define ahci_pwait_set(_ap, _r, _b) \
99 ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
100 #define ahci_pwait_set_to(_ap, _to, _r, _b) \
101 ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
103 #define AHCI_PWAIT_TIMEOUT 1000
106 * Initialize the global AHCI hardware. This code does not set up any of
110 ahci_init(struct ahci_softc *sc)
114 struct ahci_port *ap;
116 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
117 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
119 /* save BIOS initialised parameters, enable staggered spin up */
120 cap = ahci_read(sc, AHCI_REG_CAP);
121 cap &= AHCI_REG_CAP_SMPS;
122 cap |= AHCI_REG_CAP_SSS;
123 pi = ahci_read(sc, AHCI_REG_PI);
127 * This is a hack that currently does not appear to have
128 * a significant effect, but I noticed the port registers
129 * do not appear to be completely cleared after the host
130 * controller is reset.
132 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
134 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
135 if ((pi & (1 << i)) == 0)
137 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
138 AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
139 device_printf(sc->sc_dev, "can't map port\n");
142 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED |
143 AHCI_PREG_SCTL_DET_DISABLE);
144 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
145 ahci_pwrite(ap, AHCI_PREG_IE, 0);
146 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
147 ahci_pwrite(ap, AHCI_PREG_IS, 0);
153 * Unconditionally reset the controller, do not conditionalize on
154 * trying to figure it if it was previously active or not.
158 * If you have a port multiplier and it does not have a device
159 * in target 0, and it probes normally, but a later operation
160 * mis-probes a target behind that PM, it is possible for the
161 * port to brick such that only (a) a power cycle of the host
162 * or (b) placing a device in target 0 will fix the problem.
163 * Power cycling the PM has no effect (it works fine on another
164 * host port). This issue is unrelated to CLO.
166 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR);
167 if (ahci_wait_ne(sc, AHCI_REG_GHC,
168 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
169 device_printf(sc->sc_dev,
170 "unable to reset controller\n");
175 /* enable ahci (global interrupts disabled) */
176 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
178 /* restore parameters */
179 ahci_write(sc, AHCI_REG_CAP, cap);
180 ahci_write(sc, AHCI_REG_PI, pi);
186 * Allocate and initialize an AHCI port.
189 ahci_port_alloc(struct ahci_softc *sc, u_int port)
191 struct ahci_port *ap;
193 struct ahci_ccb *ccb;
196 struct ahci_cmd_hdr *hdr;
197 struct ahci_cmd_table *table;
202 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
204 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
205 device_get_name(sc->sc_dev),
206 device_get_unit(sc->sc_dev),
208 sc->sc_ports[port] = ap;
211 * Allocate enough so we never have to reallocate, it makes
214 * ap_pmcount will be reduced by the scan if we encounter the
215 * port multiplier port prior to target 15.
217 if (ap->ap_ata == NULL) {
218 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * AHCI_MAX_PMPORTS,
219 M_DEVBUF, M_INTWAIT | M_ZERO);
220 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
222 at->at_ahci_port = ap;
224 at->at_probe = ATA_PROBE_NEED_INIT;
225 at->at_features |= ATA_PORT_F_RESCAN;
226 ksnprintf(at->at_name, sizeof(at->at_name),
227 "%s.%d", ap->ap_name, i);
230 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
231 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
232 device_printf(sc->sc_dev,
233 "unable to create register window for port %d\n",
240 ap->ap_probe = ATA_PROBE_NEED_INIT;
241 TAILQ_INIT(&ap->ap_ccb_free);
242 TAILQ_INIT(&ap->ap_ccb_pending);
243 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
245 /* Disable port interrupts */
246 ahci_pwrite(ap, AHCI_PREG_IE, 0);
247 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
250 * Sec 10.1.2 - deinitialise port if it is already running
252 cmd = ahci_pread(ap, AHCI_PREG_CMD);
253 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
254 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
255 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
258 r = ahci_port_stop(ap, 1);
260 device_printf(sc->sc_dev,
261 "unable to disable %s, ignoring port %d\n",
262 ((r == 2) ? "CR" : "FR"), port);
267 /* Write DET to zero */
268 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
272 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
273 if (ap->ap_dmamem_rfis == NULL) {
274 kprintf("%s: NORFIS\n", PORTNAME(ap));
278 /* Setup RFIS base address */
279 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
280 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
281 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
282 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
284 /* Clear SERR before starting FIS reception or ST or anything */
286 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
288 /* Enable FIS reception and activate port. */
289 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
290 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
291 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
292 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
294 /* Check whether port activated. Skip it if not. */
295 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
296 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
297 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
302 /* Allocate a CCB for each command slot */
303 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
305 if (ap->ap_ccbs == NULL) {
306 device_printf(sc->sc_dev,
307 "unable to allocate command list for port %d\n",
312 /* Command List Structures and Command Tables */
313 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
314 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
315 if (ap->ap_dmamem_cmd_table == NULL ||
316 ap->ap_dmamem_cmd_list == NULL) {
318 device_printf(sc->sc_dev,
319 "unable to allocate DMA memory for port %d\n",
324 /* Setup command list base address */
325 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
326 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
327 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
329 /* Split CCB allocation into CCBs and assign to command header/table */
330 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
331 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
332 for (i = 0; i < sc->sc_ncmds; i++) {
333 ccb = &ap->ap_ccbs[i];
335 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
338 device_printf(sc->sc_dev,
339 "unable to create dmamap for port %d "
340 "ccb %d\n", port, i);
344 callout_init(&ccb->ccb_timeout);
347 ccb->ccb_cmd_hdr = &hdr[i];
348 ccb->ccb_cmd_table = &table[i];
349 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
350 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
351 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
352 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
355 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
356 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
359 ccb->ccb_xa.state = ATA_S_COMPLETE;
363 /* Wait for ICC change to complete */
364 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
367 * Start the port. The helper thread will call ahci_port_init()
368 * so the ports can all be started in parallel. A failure by
369 * ahci_port_init() does not deallocate the port since we still
370 * want hot-plug events.
372 ahci_os_start_port(ap);
375 ahci_port_free(sc, port);
380 * [re]initialize an idle port. No CCBs should be active.
382 * If at is NULL we are initializing a directly connected port, otherwise
383 * we are indirectly initializing a port multiplier port.
385 * This function is called during the initial port allocation sequence
386 * and is also called on hot-plug insertion. We take no chances and
387 * use a portreset instead of a softreset.
389 * This function is the only way to move a failed port back to active
392 * Returns 0 if a device is successfully detected.
395 ahci_port_init(struct ahci_port *ap, struct ata_port *atx)
401 * Clear all notification bits
403 if (atx == NULL && (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF))
404 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
407 * Hard-reset the port. If a device is detected but it is busy
408 * we try a second time, this time cycling the phy as well.
411 atx->at_probe = ATA_PROBE_NEED_HARD_RESET;
413 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
414 rc = ahci_port_reset(ap, atx, 2);
416 rc = ahci_port_reset(ap, atx, 1);
418 rc = ahci_port_reset(ap, atx, 2);
425 * We had problems talking to the device on the port.
428 ahci_pm_read(ap, atx->at_target,
429 AHCI_PMREG_SSTS, &data);
431 data = ahci_pread(ap, AHCI_PREG_SSTS);
434 switch(data & AHCI_PREG_SSTS_DET) {
435 case AHCI_PREG_SSTS_DET_DEV_NE:
436 kprintf("%s: Device not communicating\n",
439 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
440 kprintf("%s: PHY offline\n",
444 kprintf("%s: No device detected\n",
452 * The device on the port is still telling us its busy,
453 * which means that it is not properly handling a SATA
456 * It may be possible to softreset the device using CLO
457 * and a device reset command.
460 kprintf("%s: Device on port is bricked, giving up\n",
463 kprintf("%s: Device on port is bricked, "
464 "trying softreset\n", PORTNAME(ap));
466 rc = ahci_port_reset(ap, atx, 0);
468 kprintf("%s: Unable unbrick device\n",
471 kprintf("%s: Successfully unbricked\n",
482 * Command transfers can only be enabled if a device was successfully
485 * Allocate or deallocate the ap_ata array here too.
488 switch(ap->ap_type) {
489 case ATA_PORT_T_NONE:
502 * Start the port if we succeeded.
504 * There's nothing to start for devices behind a port multiplier.
506 if (rc == 0 && atx == NULL) {
507 if (ahci_port_start(ap)) {
508 kprintf("%s: failed to start command DMA on port, "
509 "disabling\n", PORTNAME(ap));
510 rc = ENXIO; /* couldn't start port */
515 * Flush interrupts on the port. XXX
517 * Enable interrupts on the port whether a device is sitting on
518 * it or not, to handle hot-plug events.
521 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
522 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num);
524 ahci_port_interrupt_enable(ap);
530 * Enable or re-enable interrupts on a port.
532 * This routine is called from the port initialization code or from the
533 * helper thread as the real interrupt may be forced to turn off certain
537 ahci_port_interrupt_enable(struct ahci_port *ap)
541 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
542 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
543 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
544 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
546 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
547 data |= AHCI_PREG_IE_SDBE;
549 if (sc->sc_ccc_ports & (1 << port)
550 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
552 ahci_pwrite(ap, AHCI_PREG_IE, data);
556 * Run the port / target state machine from a main context.
558 * The state machine for the port is always run.
560 * If atx is non-NULL run the state machine for a particular target.
561 * If atx is NULL run the state machine for all targets.
564 ahci_port_state_machine(struct ahci_port *ap, int initial)
573 * State machine for port. Note that CAM is not yet associated
574 * during the initial parallel probe and the port's probe state
575 * will not get past ATA_PROBE_NEED_IDENT.
577 if (ap->ap_type == ATA_PORT_T_NONE) {
578 if (ap->ap_probe == ATA_PROBE_NEED_INIT) {
579 for (target = 0; target < AHCI_MAX_PMPORTS; ++target) {
580 at = &ap->ap_ata[target];
581 at->at_probe = ATA_PROBE_NEED_INIT;
582 at->at_features |= ATA_PORT_F_RESCAN;
584 ahci_port_init(ap, NULL);
586 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
587 ahci_port_reset(ap, NULL, 1);
588 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
589 ahci_port_reset(ap, NULL, 0);
590 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
591 ahci_cam_probe(ap, NULL);
593 if (ap->ap_type != ATA_PORT_T_PM) {
594 if (ap->ap_probe == ATA_PROBE_FAILED) {
595 ahci_cam_changed(ap, NULL, 0);
596 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
597 ahci_cam_changed(ap, NULL, 1);
603 * Port Multiplier state machine.
605 * Get a mask of changed targets and combine with any runnable
606 * states already present.
608 for (loop = 0; ;++loop) {
609 if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) {
610 kprintf("%s: PM unable to read hot-plug bitmap\n",
614 data &= (1 << ap->ap_pmcount) - 1;
617 * Do at least one loop, then stop if no more state changes
618 * have occured. The PM might not generate a new
619 * notification until we clear the entire bitmap.
621 if (loop && data == 0)
625 * New devices showing up in the bitmap require some spin-up
626 * time before we start probing them. Reset didsleep. The
627 * first new device we detect will sleep before probing.
629 * This only applies to devices whos change bit is set in
630 * the data, and does not apply to the initial boot-time
635 for (target = 0; target < ap->ap_pmcount; ++target) {
636 at = &ap->ap_ata[target];
639 * Check the target state for targets behind the PM
640 * which have changed state. This will adjust
641 * at_probe and set ATA_PORT_F_RESCAN
643 * We want to wait at least 4 seconds before probing
644 * a newly inserted device. If the check status
645 * indicates a device is present and in need of a
646 * hard reset, we make sure we have slept before
649 * NOTE: When pm_check_good finds a good port it
650 * typically starts us in probe state
651 * NEED_HARD_RESET rather than INIT.
653 if (data & (1 << target)) {
654 ahci_pm_check_good(ap, target);
655 if (initial == 0 && didsleep == 0 &&
656 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
659 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
660 ahci_os_sleep(10000);
665 * Report hot-plug events before the probe state
666 * really gets hot. Only actual events are reported
667 * here to reduce spew.
669 if (data & (1 << target)) {
670 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
671 switch(at->at_probe) {
672 case ATA_PROBE_NEED_INIT:
673 case ATA_PROBE_NEED_HARD_RESET:
674 kprintf("Device inserted\n");
676 case ATA_PROBE_FAILED:
677 kprintf("Device removed\n");
680 kprintf("Device probe in progress\n");
686 * Run through the state machine as necessary if
687 * the port is not marked failed.
689 * The state machine may stop at NEED_IDENT if
690 * CAM is not yet attached.
692 * Acquire exclusive access to the port while we
693 * are doing this. This prevents command-completion
694 * from queueing commands for non-polled targets
695 * inbetween our probe steps. We need to do this
696 * because the reset probes can generate severe PHY
697 * and protocol errors and soft-brick the port.
699 if (at->at_probe != ATA_PROBE_FAILED &&
700 at->at_probe != ATA_PROBE_GOOD) {
701 ahci_beg_exclusive_access(ap, at);
702 if (at->at_probe == ATA_PROBE_NEED_INIT)
703 ahci_port_init(ap, at);
704 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
705 ahci_port_reset(ap, at, 1);
706 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
707 ahci_port_reset(ap, at, 0);
708 if (at->at_probe == ATA_PROBE_NEED_IDENT)
709 ahci_cam_probe(ap, at);
710 ahci_end_exclusive_access(ap, at);
714 * Add or remove from CAM
716 if (at->at_features & ATA_PORT_F_RESCAN) {
717 at->at_features &= ~ATA_PORT_F_RESCAN;
718 if (at->at_probe == ATA_PROBE_FAILED) {
719 ahci_cam_changed(ap, at, 0);
720 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
721 ahci_cam_changed(ap, at, 1);
730 * De-initialize and detach a port.
733 ahci_port_free(struct ahci_softc *sc, u_int port)
735 struct ahci_port *ap = sc->sc_ports[port];
736 struct ahci_ccb *ccb;
739 * Ensure port is disabled and its interrupts are all flushed.
742 ahci_port_stop(ap, 1);
743 ahci_os_stop_port(ap);
744 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
745 ahci_pwrite(ap, AHCI_PREG_IE, 0);
746 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
747 ahci_write(sc, AHCI_REG_IS, 1 << port);
751 while ((ccb = ahci_get_ccb(ap)) != NULL) {
752 if (ccb->ccb_dmamap) {
753 bus_dmamap_destroy(sc->sc_tag_data,
755 ccb->ccb_dmamap = NULL;
758 kfree(ap->ap_ccbs, M_DEVBUF);
762 if (ap->ap_dmamem_cmd_list) {
763 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
764 ap->ap_dmamem_cmd_list = NULL;
766 if (ap->ap_dmamem_rfis) {
767 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
768 ap->ap_dmamem_rfis = NULL;
770 if (ap->ap_dmamem_cmd_table) {
771 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
772 ap->ap_dmamem_cmd_table = NULL;
775 kfree(ap->ap_ata, M_DEVBUF);
779 /* bus_space(9) says we dont free the subregions handle */
782 sc->sc_ports[port] = NULL;
786 * Start high-level command processing on the port
789 ahci_port_start(struct ahci_port *ap)
791 u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd;
794 * FRE must be turned on before ST. Wait for FR to go active
795 * before turning on ST. The spec doesn't seem to think this
796 * is necessary but waiting here avoids an on-off race in the
797 * ahci_port_stop() code.
800 olds = ahci_pread(ap, AHCI_PREG_SERR);
801 oldis= ahci_pread(ap, AHCI_PREG_IS);
802 oldtfd = ahci_pread(ap, AHCI_PREG_TFD);
803 oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
804 if ((r & AHCI_PREG_CMD_FRE) == 0) {
805 r |= AHCI_PREG_CMD_FRE;
806 ahci_pwrite(ap, AHCI_PREG_CMD, r);
808 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
809 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
810 kprintf("%s: Cannot start FIS reception\n",
817 * Turn on ST, wait for CR to come up.
819 r |= AHCI_PREG_CMD_ST;
820 ahci_pwrite(ap, AHCI_PREG_CMD, r);
821 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
822 s = ahci_pread(ap, AHCI_PREG_SERR);
823 is = ahci_pread(ap, AHCI_PREG_IS);
824 tfd = ahci_pread(ap, AHCI_PREG_TFD);
825 kprintf("%s: Cannot start command DMA\n"
828 "OLDIS=%b\nNEWIS=%b\n"
829 "OLDTFD=%b\nNEWTFD=%b\n",
831 oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR,
832 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
833 oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS,
834 oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS);
840 * (Re-)enable coalescing on the port.
842 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
843 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
844 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
845 ap->ap_sc->sc_ccc_ports_cur);
853 * Stop high-level command processing on a port
856 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
862 * Disable coalescing on the port while it is stopped.
864 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
865 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
866 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
867 ap->ap_sc->sc_ccc_ports_cur);
872 * Turn off ST, then wait for CR to go off.
874 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
875 r &= ~AHCI_PREG_CMD_ST;
876 ahci_pwrite(ap, AHCI_PREG_CMD, r);
878 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
879 kprintf("%s: Port bricked, unable to stop (ST)\n",
886 * Turn off FRE, then wait for FR to go off. FRE cannot
887 * be turned off until CR transitions to 0.
889 if ((r & AHCI_PREG_CMD_FR) == 0) {
890 kprintf("%s: FR stopped, clear FRE for next start\n",
896 r &= ~AHCI_PREG_CMD_FRE;
897 ahci_pwrite(ap, AHCI_PREG_CMD, r);
898 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
899 kprintf("%s: Port bricked, unable to stop (FRE)\n",
909 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
912 ahci_port_clo(struct ahci_port *ap)
914 struct ahci_softc *sc = ap->ap_sc;
917 /* Only attempt CLO if supported by controller */
918 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
922 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
924 if (cmd & AHCI_PREG_CMD_ST) {
925 kprintf("%s: CLO requested while port running\n",
929 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
931 /* Wait for completion */
932 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
933 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
943 * If hard is 0 perform a softreset of the port.
944 * If hard is 1 perform a hard reset of the port.
945 * If hard is 2 perform a hard reset of the port and cycle the phy.
947 * If at is non-NULL an indirect port via a port-multiplier is being
948 * reset, otherwise a direct port is being reset.
950 * NOTE: Indirect ports can only be soft-reset.
953 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
959 rc = ahci_pm_hardreset(ap, at->at_target, hard);
961 rc = ahci_port_hardreset(ap, hard);
964 rc = ahci_pm_softreset(ap, at->at_target);
966 rc = ahci_port_softreset(ap);
972 * AHCI soft reset, Section 10.4.1
974 * (at) will be NULL when soft-resetting a directly-attached device, and
975 * non-NULL when soft-resetting a device through a port multiplier.
977 * This function keeps port communications intact and attempts to generate
978 * a reset to the connected device using device commands.
981 ahci_port_softreset(struct ahci_port *ap)
983 struct ahci_ccb *ccb = NULL;
984 struct ahci_cmd_hdr *cmd_slot;
991 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
992 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
994 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
997 ap->ap_flags |= AP_F_IN_RESET;
998 ap->ap_state = AP_S_NORMAL;
1001 * Remember port state in cmd (main to restore start/stop)
1005 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1006 if (ahci_port_stop(ap, 0)) {
1007 kprintf("%s: failed to stop port, cannot softreset\n",
1013 * Request CLO if device appears hung.
1015 if (ahci_pread(ap, AHCI_PREG_TFD) &
1016 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1021 * This is an attempt to clear errors so a new signature will
1022 * be latched. It isn't working properly. XXX
1025 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1028 if (ahci_port_start(ap)) {
1029 kprintf("%s: failed to start port, cannot softreset\n",
1034 /* Check whether CLO worked */
1035 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1036 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1037 kprintf("%s: CLO %s, need port reset\n",
1039 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1040 ? "failed" : "unsupported");
1046 * Prep first D2H command with SRST feature & clear busy/reset flags
1048 * It is unclear which other fields in the FIS are used. Just zero
1051 ccb = ahci_get_err_ccb(ap);
1052 ccb->ccb_xa.at = NULL;
1053 cmd_slot = ccb->ccb_cmd_hdr;
1055 fis = ccb->ccb_cmd_table->cfis;
1056 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1057 fis[0] = ATA_FIS_TYPE_H2D;
1058 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1060 cmd_slot->prdtl = 0;
1061 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1062 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1063 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1065 ccb->ccb_xa.state = ATA_S_PENDING;
1066 ccb->ccb_xa.flags = 0;
1067 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1068 kprintf("%s: First FIS failed\n", PORTNAME(ap));
1073 * WARNING! TIME SENSITIVE SPACE! WARNING!
1075 * The two FISes are supposed to be back to back. Don't issue other
1076 * commands or even delay if we can help it.
1080 * Prep second D2H command to read status and complete reset sequence
1081 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1082 * Rev 2.6 and it is unclear how the second FIS should be set up
1083 * from the AHCI document.
1085 * Give the device 3ms before sending the second FIS.
1087 * It is unclear which other fields in the FIS are used. Just zero
1090 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1091 fis[0] = ATA_FIS_TYPE_H2D;
1092 fis[15] = ATA_FIS_CONTROL_4BIT;
1094 cmd_slot->prdtl = 0;
1095 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1097 ccb->ccb_xa.state = ATA_S_PENDING;
1098 ccb->ccb_xa.flags = 0;
1099 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1100 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1104 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1105 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1106 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1108 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
1115 * If the softreset is trying to clear a BSY condition after a
1116 * normal portreset we assign the port type.
1118 * If the softreset is being run first as part of the ccb error
1119 * processing code then report if the device signature changed
1122 if (ap->ap_type == ATA_PORT_T_NONE) {
1123 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1125 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1126 kprintf("%s: device signature unexpectedly "
1127 "changed\n", PORTNAME(ap));
1128 error = EBUSY; /* XXX */
1136 ahci_put_err_ccb(ccb);
1139 * If the target is busy use CLO to clear the busy
1140 * condition. The BSY should be cleared on the next
1143 if (ahci_pread(ap, AHCI_PREG_TFD) &
1144 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1150 * If we failed to softreset make the port quiescent, otherwise
1151 * make sure the port's start/stop state matches what it was on
1154 * Don't kill the port if the softreset is on a port multiplier
1155 * target, that would kill all the targets!
1158 ahci_port_hardstop(ap);
1159 /* ap_probe set to failed */
1160 } else if (cmd & AHCI_PREG_CMD_ST) {
1161 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1162 kprintf("%s: STARTING PORT\n", PORTNAME(ap));
1163 ahci_port_start(ap);
1165 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1166 kprintf("%s: STOPPING PORT\n", PORTNAME(ap));
1167 ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE));
1169 ap->ap_flags &= ~AP_F_IN_RESET;
1172 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1178 * AHCI port reset, Section 10.4.2
1180 * This function does a hard reset of the port. Note that the device
1181 * connected to the port could still end-up hung.
1184 ahci_port_hardreset(struct ahci_port *ap, int hard)
1191 DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
1193 ap->ap_flags |= AP_F_IN_RESET;
1198 ahci_port_stop(ap, 0);
1199 ap->ap_state = AP_S_NORMAL;
1203 * The port may have been quiescent with its SUD bit cleared, so
1204 * set the SUD (spin up device).
1206 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1207 cmd |= AHCI_PREG_CMD_SUD;
1208 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1211 * Perform device detection. Cycle the PHY off, wait 10ms.
1212 * This simulates the SATA cable being physically unplugged.
1214 ap->ap_type = ATA_PORT_T_NONE;
1216 r = AHCI_PREG_SCTL_IPM_DISABLED;
1218 r |= AHCI_PREG_SCTL_DET_DISABLE;
1219 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1223 * Start transmitting COMRESET. COMRESET must be sent for at
1226 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1227 if (AhciForceGen1 & (1 << ap->ap_num)) {
1228 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1229 r |= AHCI_PREG_SCTL_SPD_GEN1;
1231 r |= AHCI_PREG_SCTL_SPD_ANY;
1233 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1236 * Through trial and error it seems to take around 100ms
1237 * for the detect logic to settle down. If this is too
1238 * short the softreset code will fail.
1243 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1244 * since we are hard-resetting the port we might as well clear
1245 * the whole enchillada
1248 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1249 r &= ~AHCI_PREG_SCTL_DET_INIT;
1250 r |= AHCI_PREG_SCTL_DET_NONE;
1251 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1254 * Try to determine if there is a device on the port.
1256 * Give the device 3/10 second to at least be detected.
1257 * If we fail clear PRCS (phy detect) since we may cycled
1258 * the phy and probably caused another PRCS interrupt.
1260 for (loop = 30; loop; --loop) {
1261 r = ahci_pread(ap, AHCI_PREG_SSTS);
1262 if (r & AHCI_PREG_SSTS_DET)
1267 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1268 kprintf("%s: Port appears to be unplugged\n",
1274 * There is something on the port. Give the device 3 seconds
1275 * to fully negotiate.
1278 ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1279 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1280 kprintf("%s: Device may be powered down\n",
1286 * Wait for the device to become ready.
1288 * This can take more then a second, give it 3 seconds. If we
1289 * succeed give the device another 3ms after that.
1291 * NOTE: Port multipliers can do two things here. First they can
1292 * return device-ready if a device is on target 0 and also
1293 * return the signature for that device. If there is no
1294 * device on target 0 then BSY/DRQ is never cleared and
1295 * it never comes ready.
1298 ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
1299 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1301 * The device is bricked or its a port multiplier and will
1302 * not unbusy until we do the pmprobe CLO softreset sequence.
1304 error = ahci_port_pmprobe(ap);
1306 kprintf("%s: Device will not come ready 0x%b\n",
1308 ahci_pread(ap, AHCI_PREG_TFD),
1311 ap->ap_type = ATA_PORT_T_PM;
1313 } else if (error == 0) {
1315 * We generally will not get a port multiplier signature in
1316 * this case even if this is a port multiplier, because of
1317 * Intel's stupidity. We almost certainly got target 0
1318 * behind the PM, if there is a PM.
1320 * Save the signature and probe for a PM. If we do not
1321 * find a PM then use the saved signature and return
1324 type = ahci_port_signature_detect(ap, NULL);
1325 error = ahci_port_pmprobe(ap);
1330 ap->ap_type = ATA_PORT_T_PM;
1331 kprintf("%s: Port multiplier detected\n",
1337 * hard-stop the port if we failed. This will set ap_probe
1340 ap->ap_flags &= ~AP_F_IN_RESET;
1342 ahci_port_hardstop(ap);
1343 /* ap_probe set to failed */
1345 if (ap->ap_type == ATA_PORT_T_PM)
1346 ap->ap_probe = ATA_PROBE_GOOD;
1348 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1354 * AHCI port multiplier probe. This routine is run by the hardreset code
1355 * if it gets past the device detect, whether or not BSY is found to be
1358 * We MUST use CLO to properly probe whether the port multiplier exists
1361 * Return 0 on success, non-zero on failure.
1364 ahci_port_pmprobe(struct ahci_port *ap)
1366 struct ahci_cmd_hdr *cmd_slot;
1367 struct ahci_ccb *ccb = NULL;
1368 u_int8_t *fis = NULL;
1374 * If we don't support port multipliers don't try to detect one.
1376 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0)
1381 kprintf("%s: START PMPROBE\n", PORTNAME(ap));
1385 * This code is only called from hardreset, which does not
1386 * high level command processing. The port should be stopped.
1388 * Set PMA mode while the port is stopped.
1390 * NOTE: On retry the port might be running, stopped, or failed.
1392 ahci_port_stop(ap, 0);
1393 ap->ap_state = AP_S_NORMAL;
1394 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1395 if ((cmd & AHCI_PREG_CMD_PMA) == 0) {
1396 cmd |= AHCI_PREG_CMD_PMA;
1397 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1401 * Flush any errors and request CLO unconditionally, then start
1406 if (ahci_port_start(ap)) {
1407 kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
1413 * Check whether CLO worked
1415 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1416 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1417 kprintf("%s: PMPROBE CLO %s, need port reset\n",
1419 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1420 ? "failed" : "unsupported");
1426 * Prep the first H2D command with SRST feature & clear busy/reset
1429 ccb = ahci_get_err_ccb(ap);
1430 cmd_slot = ccb->ccb_cmd_hdr;
1432 fis = ccb->ccb_cmd_table->cfis;
1433 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1434 fis[0] = ATA_FIS_TYPE_H2D;
1435 fis[1] = 0x0F; /* Target 15 */
1436 fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT;
1438 cmd_slot->prdtl = 0;
1439 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1440 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1441 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1442 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1444 ccb->ccb_xa.state = ATA_S_PENDING;
1445 ccb->ccb_xa.flags = 0;
1447 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1448 kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap));
1450 ahci_put_err_ccb(ccb);
1455 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1456 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1457 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
1461 * The device may have muffed up the PHY when it reset.
1465 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1466 /* ahci_pm_phy_status(ap, 15, &cmd); */
1469 * Prep second D2H command to read status and complete reset sequence
1470 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1471 * Rev 2.6 and it is unclear how the second FIS should be set up
1472 * from the AHCI document.
1474 * Give the device 3ms before sending the second FIS.
1476 * It is unclear which other fields in the FIS are used. Just zero
1479 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1480 fis[0] = ATA_FIS_TYPE_H2D;
1482 fis[15] = ATA_FIS_CONTROL_4BIT;
1484 cmd_slot->prdtl = 0;
1485 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1486 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1488 ccb->ccb_xa.state = ATA_S_PENDING;
1489 ccb->ccb_xa.flags = 0;
1491 if (ahci_poll(ccb, 5000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1492 kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap));
1494 ahci_put_err_ccb(ccb);
1501 * What? We succeeded? Yup, but for some reason the signature
1502 * is still latched from the original detect (that saw target 0
1503 * behind the PM), and I don't know how to clear the condition
1504 * other then by retrying the whole reset sequence.
1508 ahci_put_err_ccb(ccb);
1513 * Get the signature. The caller sets the ap fields.
1515 if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
1516 ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
1523 * Fall through / clean up the CCB and perform error processing.
1527 ahci_put_err_ccb(ccb);
1529 if (error == 0 && ahci_pm_identify(ap)) {
1530 kprintf("%s: PM - cannot identify port multiplier\n",
1535 if (error == 0 && ahci_pm_set_feature(ap, ATA_SATAFT_ASYNCNOTIFY, 1)) {
1536 kprintf("%s: PM - Warning, cannot enable async notify\n",
1542 if (ahci_pm_read(ap, 2, 4, &data))
1543 kprintf("Cannot read snotify\n");
1545 kprintf("Read snotify %08x\n", data);
1550 * If we failed turn off PMA, otherwise identify the port multiplier.
1551 * CAM will iterate the devices.
1554 ahci_port_stop(ap, 0);
1555 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1556 cmd &= ~AHCI_PREG_CMD_PMA;
1557 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1559 ahci_port_stop(ap, 0);
1566 * Hard-stop on hot-swap device removal. See 10.10.1
1568 * Place the port in a mode that will allow it to detect hot-swap insertions.
1569 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1570 * seem to do the job.
1573 ahci_port_hardstop(struct ahci_port *ap)
1575 struct ata_port *at;
1581 * Stop the port. We can't modify things like SUD if the port
1584 ap->ap_state = AP_S_FATAL_ERROR;
1585 ap->ap_probe = ATA_PROBE_FAILED;
1586 ap->ap_type = ATA_PORT_T_NONE;
1587 ahci_port_stop(ap, 0);
1588 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1591 * Clean up AT sub-ports on SATA port.
1593 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1594 at = &ap->ap_ata[i];
1595 at->at_type = ATA_PORT_T_NONE;
1596 at->at_probe = ATA_PROBE_FAILED;
1600 * Turn off port-multiplier control bit
1602 if (cmd & AHCI_PREG_CMD_PMA) {
1603 cmd &= ~AHCI_PREG_CMD_PMA;
1604 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1608 * Make sure FRE is active. There isn't anything we can do if it
1609 * fails so just ignore errors.
1611 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1612 cmd |= AHCI_PREG_CMD_FRE;
1613 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1614 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1615 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1619 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1620 * 10.10.1 place us in the Listen state.
1622 * Deactivating SUD only applies if the controller supports SUD.
1624 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
1626 if (cmd & AHCI_PREG_CMD_SUD) {
1627 cmd &= ~AHCI_PREG_CMD_SUD;
1628 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1633 * Transition su to the spin-up state. HVA shall send COMRESET and
1634 * begin initialization sequence (whatever that means).
1636 * This only applies if the controller supports SUD.
1638 cmd |= AHCI_PREG_CMD_SUD;
1639 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1643 * Transition us to the Reset state. Theoretically we send a
1644 * continuous stream of COMRESETs in this state.
1646 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1647 if (AhciForceGen1 & (1 << ap->ap_num)) {
1648 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1649 r |= AHCI_PREG_SCTL_SPD_GEN1;
1651 r |= AHCI_PREG_SCTL_SPD_ANY;
1653 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1657 * Flush SERR_DIAG_X so the TFD can update.
1662 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1663 * hopefully send us a DIAG_X-related interrupt if it receives
1664 * a COMINIT, and if not that then at least a Phy transition
1667 * If we transition INIT from 1->0 to begin the initalization
1668 * sequence it is unclear if that sequence will remain active
1669 * until the next device insertion.
1671 * If we go back to the listen state it is unclear if the
1672 * device will actually send us a COMINIT, since we aren't
1673 * sending any COMRESET's
1679 * Multiple events may have built up in the TFD. The spec is not very
1680 * clear on this but it does seem to serialize events so clearing DIAG_X
1681 * just once might not do the job during a reset sequence.
1683 * XXX this probably isn't right.
1686 ahci_flush_tfd(struct ahci_port *ap)
1690 r = ahci_pread(ap, AHCI_PREG_SERR);
1691 while (r & AHCI_PREG_SERR_DIAG_X) {
1692 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1694 r = ahci_pread(ap, AHCI_PREG_SERR);
1699 * Figure out what type of device is connected to the port, ATAPI or
1703 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1707 sig = ahci_pread(ap, AHCI_PREG_SIG);
1708 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1709 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1710 return(ATA_PORT_T_ATAPI);
1711 } else if ((sig & 0xffff0000) ==
1712 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1713 return(ATA_PORT_T_PM);
1715 return(ATA_PORT_T_DISK);
1720 * Load the DMA descriptor table for a CCB's buffer.
1723 ahci_load_prdt(struct ahci_ccb *ccb)
1725 struct ahci_port *ap = ccb->ccb_port;
1726 struct ahci_softc *sc = ap->ap_sc;
1727 struct ata_xfer *xa = &ccb->ccb_xa;
1728 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1729 bus_dmamap_t dmap = ccb->ccb_dmamap;
1730 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1733 if (xa->datalen == 0) {
1734 ccb->ccb_cmd_hdr->prdtl = 0;
1738 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1739 xa->data, xa->datalen,
1740 ahci_load_prdt_callback,
1742 ((xa->flags & ATA_F_NOWAIT) ?
1743 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1745 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1748 if (xa->flags & ATA_F_PIO)
1749 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1751 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1753 bus_dmamap_sync(sc->sc_tag_data, dmap,
1754 (xa->flags & ATA_F_READ) ?
1755 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1761 bus_dmamap_unload(sc->sc_tag_data, dmap);
1767 * Callback from BUSDMA system to load the segment list. The passed segment
1768 * list is a temporary structure.
1772 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1775 struct ahci_prdt *prd = *(void **)info;
1778 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1781 addr = segs->ds_addr;
1782 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1783 prd->dba_lo = htole32((u_int32_t)addr);
1785 KKASSERT((addr & 1) == 0);
1786 KKASSERT((segs->ds_len & 1) == 0);
1788 prd->flags = htole32(segs->ds_len - 1);
1794 *(void **)info = prd; /* return last valid segment */
1798 ahci_unload_prdt(struct ahci_ccb *ccb)
1800 struct ahci_port *ap = ccb->ccb_port;
1801 struct ahci_softc *sc = ap->ap_sc;
1802 struct ata_xfer *xa = &ccb->ccb_xa;
1803 bus_dmamap_t dmap = ccb->ccb_dmamap;
1805 if (xa->datalen != 0) {
1806 bus_dmamap_sync(sc->sc_tag_data, dmap,
1807 (xa->flags & ATA_F_READ) ?
1808 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1810 bus_dmamap_unload(sc->sc_tag_data, dmap);
1812 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1815 xa->resid = xa->datalen -
1816 le32toh(ccb->ccb_cmd_hdr->prdbc);
1821 * Start a command and poll for completion.
1823 * timeout is in ms and only counts once the command gets on-chip.
1825 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1826 * that no error occured.
1828 * NOTE: If the caller specifies a NULL timeout function the caller is
1829 * responsible for clearing hardware state on failure, but we will
1830 * deal with removing the ccb from any pending queue.
1832 * NOTE: NCQ should never be used with this function.
1834 * NOTE: If the port is in a failed state and stopped we do not try
1835 * to activate the ccb.
1838 ahci_poll(struct ahci_ccb *ccb, int timeout,
1839 void (*timeout_fn)(struct ahci_ccb *))
1841 struct ahci_port *ap = ccb->ccb_port;
1843 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1844 ccb->ccb_xa.state = ATA_S_ERROR;
1845 return(ccb->ccb_xa.state);
1851 ahci_port_intr(ap, 1);
1852 switch(ccb->ccb_xa.state) {
1854 timeout -= ahci_os_softsleep();
1857 ahci_os_softsleep();
1858 ahci_check_active_timeouts(ap);
1862 return (ccb->ccb_xa.state);
1864 } while (timeout > 0);
1866 kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
1867 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
1868 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
1869 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
1870 ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
1876 return(ccb->ccb_xa.state);
1880 * When polling we have to check if the currently active CCB(s)
1881 * have timed out as the callout will be deadlocked while we
1882 * hold the port lock.
1885 ahci_check_active_timeouts(struct ahci_port *ap)
1887 struct ahci_ccb *ccb;
1891 mask = ap->ap_active | ap->ap_sactive;
1893 tag = ffs(mask) - 1;
1894 mask &= ~(1 << tag);
1895 ccb = &ap->ap_ccbs[tag];
1896 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1897 ahci_ata_cmd_timeout(ccb);
1905 ahci_start_timeout(struct ahci_ccb *ccb)
1907 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1908 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1909 callout_reset(&ccb->ccb_timeout,
1910 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1911 ahci_ata_cmd_timeout_unserialized, ccb);
1916 ahci_start(struct ahci_ccb *ccb)
1918 struct ahci_port *ap = ccb->ccb_port;
1919 struct ahci_softc *sc = ap->ap_sc;
1921 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1923 /* Zero transferred byte count before transfer */
1924 ccb->ccb_cmd_hdr->prdbc = 0;
1926 /* Sync command list entry and corresponding command table entry */
1927 bus_dmamap_sync(sc->sc_tag_cmdh,
1928 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1929 BUS_DMASYNC_PREWRITE);
1930 bus_dmamap_sync(sc->sc_tag_cmdt,
1931 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1932 BUS_DMASYNC_PREWRITE);
1934 /* Prepare RFIS area for write by controller */
1935 bus_dmamap_sync(sc->sc_tag_rfis,
1936 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1937 BUS_DMASYNC_PREREAD);
1939 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1941 * Issue NCQ commands only when there are no outstanding
1942 * standard commands.
1944 if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) {
1945 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1947 ahci_start_timeout(ccb);
1948 KKASSERT(ap->ap_active_cnt == 0);
1949 ap->ap_sactive |= (1 << ccb->ccb_slot);
1950 ccb->ccb_xa.state = ATA_S_ONCHIP;
1951 ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
1952 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1956 * Wait for all NCQ commands to finish before issuing standard
1957 * command. Allow up to <limit> non-NCQ commands to be active.
1959 * XXX If ap is a port multiplier only allow 1. At least the
1960 * NVidia-MCP77 part seems to barf if more then one
1961 * command is activated, even though it isn't NCQ.
1963 * If I set up more then one I get phy errors and the
1966 int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2;
1967 if (ap->ap_sactive || ap->ap_active_cnt >= limit) {
1968 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1970 ahci_start_timeout(ccb);
1971 ap->ap_active |= 1 << ccb->ccb_slot;
1972 ccb->ccb_xa.state = ATA_S_ONCHIP;
1973 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1974 ap->ap_active_cnt++;
1980 * While holding the port lock acquire exclusive access to the port.
1982 * This is used when running the state machine to initialize and identify
1983 * targets over a port multiplier. Setting exclusive access prevents
1984 * ahci_port_intr() from activating any requests sitting on the pending
1988 ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
1990 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1991 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1992 while (ap->ap_active || ap->ap_sactive) {
1993 ahci_port_intr(ap, 1);
1994 ahci_os_softsleep();
1999 ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2001 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2002 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
2003 if (ap->ap_active == 0 && ap->ap_sactive == 0)
2004 ahci_issue_pending_commands(ap, 0);
2008 ahci_issue_pending_ncq_commands(struct ahci_port *ap)
2010 struct ahci_ccb *nextccb;
2011 u_int32_t sact_change = 0;
2013 KKASSERT(ap->ap_active_cnt == 0);
2015 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2016 if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
2018 if (ap->ap_flags & AP_F_EXCLUSIVE_ACCESS)
2021 /* Start all the NCQ commands at the head of the pending list. */
2023 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
2024 ahci_start_timeout(nextccb);
2025 sact_change |= 1 << nextccb->ccb_slot;
2026 nextccb->ccb_xa.state = ATA_S_ONCHIP;
2027 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2028 } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
2030 ap->ap_sactive |= sact_change;
2031 ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
2032 ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
2038 ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
2040 struct ahci_ccb *nextccb;
2042 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2043 if (nextccb == NULL)
2045 if (ap->ap_flags & AP_F_EXCLUSIVE_ACCESS)
2048 if (nextccb->ccb_xa.flags & ATA_F_NCQ) {
2049 KKASSERT(last_was_ncq == 0); /* otherwise it should have
2050 * been started already. */
2053 * Issue NCQ commands only when there are no outstanding
2054 * standard commands.
2056 if (ap->ap_active == 0)
2057 ahci_issue_pending_ncq_commands(ap);
2059 KKASSERT(ap->ap_active_cnt > 0);
2061 if (ap->ap_sactive || last_was_ncq)
2062 KKASSERT(ap->ap_active_cnt == 0);
2065 * Wait for all NCQ commands to finish before issuing standard
2066 * command. Then keep up to 2 standard commands on-chip at
2072 while (ap->ap_active_cnt < 2 &&
2073 nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
2074 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
2075 ahci_start_timeout(nextccb);
2076 ap->ap_active |= 1 << nextccb->ccb_slot;
2077 nextccb->ccb_xa.state = ATA_S_ONCHIP;
2078 ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
2079 ap->ap_active_cnt++;
2080 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2086 ahci_intr(void *arg)
2088 struct ahci_softc *sc = arg;
2089 struct ahci_port *ap;
2090 u_int32_t is, ack = 0;
2094 * Check if the master enable is up, and whether any interrupts are
2097 if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2099 is = ahci_read(sc, AHCI_REG_IS);
2100 if (is == 0 || is == 0xffffffff)
2104 #ifdef AHCI_COALESCE
2105 /* Check coalescing interrupt first */
2106 if (is & sc->sc_ccc_mask) {
2107 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2109 is &= ~sc->sc_ccc_mask;
2110 is |= sc->sc_ccc_ports_cur;
2115 * Process interrupts for each port in a non-blocking fashion.
2119 ap = sc->sc_ports[port];
2121 if (ahci_os_lock_port_nb(ap) == 0) {
2122 ahci_port_intr(ap, 0);
2123 ahci_os_unlock_port(ap);
2125 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2126 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2132 /* Finally, acknowledge global interrupt */
2133 ahci_write(sc, AHCI_REG_IS, ack);
2137 * Core called from helper thread.
2140 ahci_port_thread_core(struct ahci_port *ap, int mask)
2143 * Process any expired timedouts.
2145 ahci_os_lock_port(ap);
2146 if (mask & AP_SIGF_TIMEOUT) {
2147 ahci_check_active_timeouts(ap);
2151 * Process port interrupts which require a higher level of
2154 if (mask & AP_SIGF_PORTINT) {
2155 ahci_port_intr(ap, 1);
2156 ahci_port_interrupt_enable(ap);
2157 ahci_os_unlock_port(ap);
2159 ahci_os_unlock_port(ap);
2164 * Core per-port interrupt handler.
2166 * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2167 * deal with normal command completions which do not require blocking.
2170 ahci_port_intr(struct ahci_port *ap, int blockable)
2172 struct ahci_softc *sc = ap->ap_sc;
2173 u_int32_t is, ci_saved, ci_masked;
2175 struct ahci_ccb *ccb = NULL;
2176 struct ata_port *ccb_at = NULL;
2177 volatile u_int32_t *active;
2181 const u_int32_t blockable_mask = AHCI_PREG_IS_TFES |
2189 enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
2190 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2192 is = ahci_pread(ap, AHCI_PREG_IS);
2195 * All basic command completions are always processed.
2197 if (is & AHCI_PREG_IS_DPS)
2198 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2201 * If we can't block then we can't handle these here. Disable
2202 * the interrupts in question so we don't live-lock, the helper
2203 * thread will re-enable them.
2205 * If the port is in a completely failed state we do not want
2206 * to drop through to failed-command-processing if blockable is 0,
2207 * just let the thread deal with it all.
2209 * Otherwise we fall through and still handle DHRS and any commands
2210 * which completed normally. Even if we are errored we haven't
2211 * stopped the port yet so CI/SACT are still good.
2213 if (blockable == 0) {
2214 if (ap->ap_state == AP_S_FATAL_ERROR) {
2215 ahci_pwrite(ap, AHCI_PREG_IE,
2216 ahci_pread(ap, AHCI_PREG_IE) & ~is);
2217 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2220 if (is & blockable_mask) {
2221 ahci_pwrite(ap, AHCI_PREG_IE,
2222 ahci_pread(ap, AHCI_PREG_IE) & ~blockable_mask);
2223 is &= ~blockable_mask;
2224 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2229 kprintf("%s: INTERRUPT %b\n", PORTNAME(ap),
2234 * Either NCQ or non-NCQ commands will be active, never both.
2236 if (ap->ap_sactive) {
2237 KKASSERT(ap->ap_active == 0);
2238 KKASSERT(ap->ap_active_cnt == 0);
2239 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2240 active = &ap->ap_sactive;
2242 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2243 active = &ap->ap_active;
2246 if (is & AHCI_PREG_IS_TFES) {
2248 * Command failed (blockable).
2250 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2252 * This stops command processing.
2254 u_int32_t tfd, serr;
2257 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2258 serr = ahci_pread(ap, AHCI_PREG_SERR);
2261 * If no NCQ commands are active the error slot is easily
2262 * determined, otherwise we have to extract the error
2263 * from the log page.
2265 if (ap->ap_sactive == 0) {
2266 err_slot = AHCI_PREG_CMD_CCS(
2267 ahci_pread(ap, AHCI_PREG_CMD));
2268 ccb = &ap->ap_ccbs[err_slot];
2269 ccb_at = ccb->ccb_xa.at; /* can be NULL */
2271 /* Preserve received taskfile data from the RFIS. */
2272 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2273 sizeof(struct ata_fis_d2h));
2278 DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n",
2279 PORTNAME(ap), err_slot,
2280 tfd, AHCI_PFMT_TFD_STS,
2281 serr, AHCI_PFMT_SERR);
2283 /* Stopping the port clears CI and SACT */
2284 ahci_port_stop(ap, 0);
2285 need = NEED_RESTART;
2288 * Clear SERR (primarily DIAG_X) to enable capturing of the
2291 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2293 /* Acknowledge the interrupts we can recover from. */
2294 ahci_pwrite(ap, AHCI_PREG_IS,
2295 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS));
2296 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS);
2298 /* If device hasn't cleared its busy status, try to idle it. */
2299 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2300 kprintf("%s: Attempting to idle device ccb=%p\n",
2301 PORTNAME(ap), ccb_at);
2302 if (ap->ap_flags & AP_F_IN_RESET)
2305 * XXX how do we unbrick a PM target (ccb_at != NULL).
2307 * For now fail the target and use CLO to clear the
2308 * busy condition and make the ahci port usable for
2309 * the remaining devices.
2312 ccb_at->at_probe = ATA_PROBE_FAILED;
2314 } else if (ahci_port_reset(ap, ccb_at, 0)) {
2315 kprintf("%s: Unable to idle device, port "
2321 /* Had to reset device, can't gather extended info. */
2322 } else if (ap->ap_sactive) {
2324 * Recover the NCQ error from log page 10h.
2326 * XXX NCQ currently not supported with port
2329 ahci_port_read_ncq_error(ap, &err_slot);
2330 kprintf("recover from NCQ error err_slot %d\n",
2335 DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
2336 PORTNAME(ap), err_slot);
2338 ccb = &ap->ap_ccbs[err_slot];
2340 /* Didn't reset, could gather extended info from log. */
2341 kprintf("%s: didn't reset err_slot %d "
2342 "sact=%08x act=%08x\n",
2344 err_slot, ap->ap_sactive, ap->ap_active);
2348 * If we couldn't determine the errored slot, reset the port
2349 * and fail all the active slots.
2351 if (err_slot == -1) {
2352 if (ap->ap_flags & AP_F_IN_RESET)
2355 * XXX how do we unbrick a PM target (ccb_at != NULL).
2357 * For now fail the target and use CLO to clear the
2358 * busy condition and make the ahci port usable for
2359 * the remaining devices.
2362 ccb_at->at_probe = ATA_PROBE_FAILED;
2364 } else if (ahci_port_reset(ap, ccb_at, 0)) {
2365 kprintf("%s: Unable to idle device after "
2366 "NCQ error, port bricked on us\n",
2370 kprintf("%s: couldn't recover NCQ error, failing "
2371 "all outstanding commands.\n",
2376 /* Clear the failed command in saved CI so completion runs. */
2377 ci_saved &= ~(1 << err_slot);
2379 /* Note the error in the ata_xfer. */
2380 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2381 ccb->ccb_xa.state = ATA_S_ERROR;
2384 /* There may only be one outstanding standard command now. */
2385 if (ap->ap_sactive == 0) {
2388 slot = ffs(tmp) - 1;
2389 tmp &= ~(1 << slot);
2394 } else if (is & AHCI_PREG_IS_DHRS) {
2396 * Command posted D2H register FIS to the rfis (non-blocking).
2398 * Command posted D2H register FIS to the rfis. This
2399 * does NOT stop command processing and it is unclear
2400 * how we are supposed to deal with it other then using
2401 * only a queue of 1.
2403 * We must copy the port rfis to the ccb and restart
2404 * command processing. ahci_pm_read() does not function
2405 * without this support.
2409 if (ap->ap_sactive == 0) {
2410 err_slot = AHCI_PREG_CMD_CCS(
2411 ahci_pread(ap, AHCI_PREG_CMD));
2412 ccb = &ap->ap_ccbs[err_slot];
2413 ccb_at = ccb->ccb_xa.at; /* can be NULL */
2415 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2416 sizeof(struct ata_fis_d2h));
2418 kprintf("%s: Unexpected DHRS posted while "
2419 "NCQ running\n", PORTNAME(ap));
2422 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2423 is &= ~AHCI_PREG_IS_DHRS;
2427 * Device notification to us (non-blocking)
2429 * NOTE! On some parts notification bits can get set without
2430 * generating an interrupt. It is unclear whether this is
2431 * a bug in the PM (sending a DTOH device setbits with 'N' set
2432 * and 'I' not set), or a bug in the host controller.
2434 * It only seems to occur under load.
2436 if (/*(is & AHCI_PREG_IS_SDBS) &&*/ (sc->sc_cap & AHCI_REG_CAP_SSNTF)) {
2440 data = ahci_pread(ap, AHCI_PREG_SNTF);
2441 if (is & AHCI_PREG_IS_SDBS) {
2442 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2443 is &= ~AHCI_PREG_IS_SDBS;
2444 xstr = " (no SDBS!)";
2449 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2451 kprintf("%s: NOTIFY %08x%s\n",
2452 PORTNAME(ap), data, xstr);
2453 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_N);
2454 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2455 ahci_cam_changed(ap, NULL, -1);
2460 * Spurious IFS errors (blockable).
2462 * Spurious IFS errors can occur while we are doing a reset
2463 * sequence through a PM. Try to recover if we are being asked
2464 * to ignore IFS errors during these periods.
2466 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2467 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2468 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2469 kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
2472 serr, AHCI_PFMT_SERR);
2473 ap->ap_flags |= AP_F_IFS_IGNORED;
2475 ap->ap_flags |= AP_F_IFS_OCCURED;
2476 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2477 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2478 is &= ~AHCI_PREG_IS_IFS;
2479 ahci_port_stop(ap, 0);
2480 ahci_port_start(ap);
2481 need = NEED_RESTART;
2485 * Port change (hot-plug) (blockable).
2487 * A PCS interrupt will occur on hot-plug once communication is
2490 * A PRCS interrupt will occur on hot-unplug (and possibly also
2493 * XXX We can then check the CPS (Cold Presence State) bit, if
2494 * supported, to determine if a device is plugged in or not and do
2497 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
2498 * can also occur if an unsolicited COMINIT is received.
2499 * If this occurs command processing is automatically
2500 * stopped (CR goes inactive) and the port must be stopped
2503 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2504 ahci_pwrite(ap, AHCI_PREG_IS,
2505 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2506 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2507 ahci_pwrite(ap, AHCI_PREG_SERR,
2508 (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
2509 ahci_port_stop(ap, 0);
2510 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2511 case AHCI_PREG_SSTS_DET_DEV:
2512 if (ap->ap_type == ATA_PORT_T_NONE) {
2513 need = NEED_HOTPLUG_INSERT;
2516 need = NEED_RESTART;
2519 if (ap->ap_type != ATA_PORT_T_NONE) {
2520 need = NEED_HOTPLUG_REMOVE;
2523 need = NEED_RESTART;
2529 * Check for remaining errors - they are fatal. (blockable)
2531 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2532 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2535 ahci_pwrite(ap, AHCI_PREG_IS,
2536 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2537 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2539 serr = ahci_pread(ap, AHCI_PREG_SERR);
2540 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
2541 "disabling port.\n",
2544 serr, AHCI_PFMT_SERR
2546 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2547 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2549 /* XXX try recovery first */
2554 * Fail all outstanding commands if we know the port won't recover.
2556 * We may have a ccb_at if the failed command is known and was
2557 * being sent to a device over a port multiplier (PM). In this
2558 * case if the port itself has not completely failed we fail just
2559 * the commands related to that target.
2561 if (ap->ap_state == AP_S_FATAL_ERROR) {
2563 ap->ap_state = AP_S_FATAL_ERROR;
2566 /* Stopping the port clears CI/SACT */
2567 ahci_port_stop(ap, 0);
2570 * Error all the active slots. If running across a PM
2571 * try to error out just the slots related to the target.
2573 ci_masked = ci_saved & *active;
2575 slot = ffs(ci_masked) - 1;
2576 ccb = &ap->ap_ccbs[slot];
2577 if (ccb_at == ccb->ccb_xa.at ||
2578 ap->ap_state == AP_S_FATAL_ERROR) {
2579 ci_masked &= ~(1 << slot);
2580 ccb->ccb_xa.state = ATA_S_ERROR;
2584 /* Run completion for all active slots. */
2585 ci_saved &= ~*active;
2588 * Don't restart the port if our problems were deemed fatal.
2590 * Also acknowlege all fatal interrupt sources to prevent
2593 if (ap->ap_state == AP_S_FATAL_ERROR) {
2594 if (need == NEED_RESTART)
2595 need = NEED_NOTHING;
2596 ahci_pwrite(ap, AHCI_PREG_IS,
2597 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2598 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2604 * CCB completion (non blocking).
2606 * CCB completion is detected by noticing its slot's bit in CI has
2607 * changed to zero some time after we activated it.
2608 * If we are polling, we may only be interested in particular slot(s).
2610 * Any active bits not saved are completed within the restrictions
2611 * imposed by the caller.
2613 ci_masked = ~ci_saved & *active;
2615 slot = ffs(ci_masked) - 1;
2616 ccb = &ap->ap_ccbs[slot];
2617 ci_masked &= ~(1 << slot);
2619 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2620 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2623 bus_dmamap_sync(sc->sc_tag_cmdh,
2624 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2625 BUS_DMASYNC_POSTWRITE);
2627 bus_dmamap_sync(sc->sc_tag_cmdt,
2628 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2629 BUS_DMASYNC_POSTWRITE);
2631 bus_dmamap_sync(sc->sc_tag_rfis,
2632 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2633 BUS_DMASYNC_POSTREAD);
2635 *active &= ~(1 << ccb->ccb_slot);
2636 if (active == &ap->ap_active) {
2637 KKASSERT(ap->ap_active_cnt > 0);
2638 --ap->ap_active_cnt;
2644 * Cleanup. Will not be set if non-blocking.
2649 * A recoverable error occured and we can restart outstanding
2650 * commands on the port.
2652 ahci_port_start(ap);
2658 slot = ffs(tmp) - 1;
2659 tmp &= ~(1 << slot);
2660 ccb = &ap->ap_ccbs[slot];
2661 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2662 KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
2663 (!!ap->ap_sactive));
2666 DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
2667 "re-enabling%s slots %08x\n", PORTNAME(ap),
2668 ap->ap_sactive ? " NCQ" : "", ci_saved);
2671 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
2672 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
2675 case NEED_HOTPLUG_INSERT:
2677 * A hot-plug insertion event has occured and all
2678 * outstanding commands have already been revoked.
2680 * Don't recurse if this occurs while we are
2681 * resetting the port.
2683 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2684 kprintf("%s: HOTPLUG - Device inserted\n",
2686 ap->ap_probe = ATA_PROBE_NEED_INIT;
2687 ahci_cam_changed(ap, NULL, -1);
2690 case NEED_HOTPLUG_REMOVE:
2692 * A hot-plug removal event has occured and all
2693 * outstanding commands have already been revoked.
2695 * Don't recurse if this occurs while we are
2696 * resetting the port.
2698 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2699 kprintf("%s: HOTPLUG - Device removed\n",
2701 ahci_port_hardstop(ap);
2702 /* ap_probe set to failed */
2703 ahci_cam_changed(ap, NULL, -1);
2712 ahci_get_ccb(struct ahci_port *ap)
2714 struct ahci_ccb *ccb;
2716 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2717 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2719 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2720 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2721 ccb->ccb_xa.state = ATA_S_SETUP;
2722 ccb->ccb_xa.at = NULL;
2724 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2730 ahci_put_ccb(struct ahci_ccb *ccb)
2732 struct ahci_port *ap = ccb->ccb_port;
2735 if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2736 ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2737 ccb->ccb_xa.state != ATA_S_ERROR) {
2738 kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
2740 PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2745 ccb->ccb_xa.state = ATA_S_PUT;
2746 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2747 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2748 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2752 ahci_get_err_ccb(struct ahci_port *ap)
2754 struct ahci_ccb *err_ccb;
2757 /* No commands may be active on the chip. */
2758 sact = ahci_pread(ap, AHCI_PREG_SACT);
2760 kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
2761 KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
2764 KKASSERT(ap->ap_err_busy == 0);
2765 ap->ap_err_busy = 1;
2767 /* Save outstanding command state. */
2768 ap->ap_err_saved_active = ap->ap_active;
2769 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2770 ap->ap_err_saved_sactive = ap->ap_sactive;
2773 * Pretend we have no commands outstanding, so that completions won't
2776 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2779 * Grab a CCB to use for error recovery. This should never fail, as
2780 * we ask atascsi to reserve one for us at init time.
2782 err_ccb = ahci_get_ccb(ap);
2783 KKASSERT(err_ccb != NULL);
2784 err_ccb->ccb_xa.flags = 0;
2785 err_ccb->ccb_done = ahci_empty_done;
2791 ahci_put_err_ccb(struct ahci_ccb *ccb)
2793 struct ahci_port *ap = ccb->ccb_port;
2798 KKASSERT(ap->ap_err_busy);
2801 * No commands may be active on the chip
2803 sact = ahci_pread(ap, AHCI_PREG_SACT);
2805 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
2806 ccb->ccb_slot, sact);
2808 ci = ahci_pread(ap, AHCI_PREG_CI);
2810 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2811 "(act=%08x sact=%08x)\n",
2813 ap->ap_active, ap->ap_sactive);
2816 /* Done with the CCB */
2819 /* Restore outstanding command state */
2820 ap->ap_sactive = ap->ap_err_saved_sactive;
2821 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2822 ap->ap_active = ap->ap_err_saved_active;
2825 ap->ap_err_busy = 0;
2830 * Read log page to get NCQ error.
2832 * NOTE: NCQ not currently supported on port multipliers. XXX
2835 ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
2837 struct ahci_ccb *ccb;
2838 struct ahci_cmd_hdr *cmd_slot;
2840 struct ata_fis_h2d *fis;
2843 DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2845 /* Save command register state. */
2846 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
2848 /* Port should have been idled already. Start it. */
2849 KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
2850 ahci_port_start(ap);
2852 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2853 ccb = ahci_get_err_ccb(ap);
2854 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2855 ccb->ccb_xa.data = ap->ap_err_scratch;
2856 ccb->ccb_xa.datalen = 512;
2857 cmd_slot = ccb->ccb_cmd_hdr;
2858 bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
2860 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
2861 fis->type = ATA_FIS_TYPE_H2D;
2862 fis->flags = ATA_H2D_FLAGS_CMD;
2863 fis->command = ATA_C_READ_LOG_EXT;
2864 fis->lba_low = 0x10; /* queued error log page (10h) */
2865 fis->sector_count = 1; /* number of sectors (1) */
2866 fis->sector_count_exp = 0;
2867 fis->lba_mid = 0; /* starting offset */
2868 fis->lba_mid_exp = 0;
2871 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
2873 if (ahci_load_prdt(ccb) != 0) {
2874 rc = ENOMEM; /* XXX caller must abort all commands */
2878 ccb->ccb_xa.state = ATA_S_PENDING;
2879 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != 0)
2884 /* Abort our command, if it failed, by stopping command DMA. */
2886 kprintf("%s: log page read failed, slot %d was still active.\n",
2887 PORTNAME(ap), ccb->ccb_slot);
2890 /* Done with the error CCB now. */
2891 ahci_unload_prdt(ccb);
2892 ahci_put_err_ccb(ccb);
2894 /* Extract failed register set and tags from the scratch space. */
2896 struct ata_log_page_10h *log;
2899 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2900 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2901 /* Not queued bit was set - wasn't an NCQ error? */
2902 kprintf("%s: read NCQ error page, but not an NCQ "
2907 /* Copy back the log record as a D2H register FIS. */
2908 *err_slotp = err_slot = log->err_regs.type &
2909 ATA_LOG_10H_TYPE_TAG_MASK;
2911 ccb = &ap->ap_ccbs[err_slot];
2912 memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
2913 sizeof(struct ata_fis_d2h));
2914 ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
2915 ccb->ccb_xa.rfis.flags = 0;
2919 /* Restore saved CMD register state */
2920 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
2926 * Allocate memory for various structures DMAd by hardware. The maximum
2927 * number of segments for these tags is 1 so the DMA memory will have a
2928 * single physical base address.
2930 struct ahci_dmamem *
2931 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
2933 struct ahci_dmamem *adm;
2936 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2938 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2939 BUS_DMA_ZERO, &adm->adm_map);
2942 error = bus_dmamap_load(tag, adm->adm_map,
2944 bus_dma_tag_getmaxsize(tag),
2945 ahci_dmamem_saveseg, &adm->adm_busaddr,
2950 bus_dmamap_destroy(tag, adm->adm_map);
2951 adm->adm_map = NULL;
2952 adm->adm_tag = NULL;
2953 adm->adm_kva = NULL;
2955 kfree(adm, M_DEVBUF);
2963 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2965 KKASSERT(error == 0);
2966 KKASSERT(nsegs == 1);
2967 *(bus_addr_t *)info = segs->ds_addr;
2972 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
2975 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2976 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2977 adm->adm_map = NULL;
2978 adm->adm_tag = NULL;
2979 adm->adm_kva = NULL;
2981 kfree(adm, M_DEVBUF);
2985 ahci_read(struct ahci_softc *sc, bus_size_t r)
2987 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2988 BUS_SPACE_BARRIER_READ);
2989 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2993 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
2995 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2996 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2997 BUS_SPACE_BARRIER_WRITE);
3001 ahci_pread(struct ahci_port *ap, bus_size_t r)
3003 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3004 BUS_SPACE_BARRIER_READ);
3005 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3009 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3011 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3012 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3013 BUS_SPACE_BARRIER_WRITE);
3017 * Wait up to (timeout) milliseconds for the masked port register to
3020 * Timeout is in milliseconds.
3023 ahci_pwait_eq(struct ahci_port *ap, int timeout,
3024 bus_size_t r, u_int32_t mask, u_int32_t target)
3029 * Loop hard up to 100uS
3031 for (t = 0; t < 100; ++t) {
3032 if ((ahci_pread(ap, r) & mask) == target)
3034 ahci_os_hardsleep(1); /* us */
3038 timeout -= ahci_os_softsleep();
3039 if ((ahci_pread(ap, r) & mask) == target)
3041 } while (timeout > 0);
3046 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3052 * Loop hard up to 100uS
3054 for (t = 0; t < 100; ++t) {
3055 if ((ahci_read(sc, r) & mask) != target)
3057 ahci_os_hardsleep(1); /* us */
3061 * And one millisecond the slow way
3065 t -= ahci_os_softsleep();
3066 if ((ahci_read(sc, r) & mask) != target)
3075 * Acquire an ata transfer.
3077 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
3078 * targets that go through the port multiplier.
3081 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3083 struct ahci_ccb *ccb;
3085 ccb = ahci_get_ccb(ap);
3087 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3092 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3093 PORTNAME(ap), ccb->ccb_slot);
3095 ccb->ccb_xa.at = at;
3096 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3098 return (&ccb->ccb_xa);
3102 ahci_ata_put_xfer(struct ata_xfer *xa)
3104 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3106 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3112 ahci_ata_cmd(struct ata_xfer *xa)
3114 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3115 struct ahci_cmd_hdr *cmd_slot;
3117 KKASSERT(xa->state == ATA_S_SETUP);
3119 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3122 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
3123 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
3124 ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3127 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
3130 ccb->ccb_done = ahci_ata_cmd_done;
3132 cmd_slot = ccb->ccb_cmd_hdr;
3133 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
3134 if (ccb->ccb_xa.at) {
3135 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
3136 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
3139 if (xa->flags & ATA_F_WRITE)
3140 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3142 if (xa->flags & ATA_F_PACKET)
3143 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3145 if (ahci_load_prdt(ccb) != 0)
3148 xa->state = ATA_S_PENDING;
3150 if (xa->flags & ATA_F_POLL)
3151 return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3154 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
3155 xa->flags |= ATA_F_TIMEOUT_DESIRED;
3162 xa->state = ATA_S_ERROR;
3165 return (ATA_S_ERROR);
3169 ahci_ata_cmd_done(struct ahci_ccb *ccb)
3171 struct ata_xfer *xa = &ccb->ccb_xa;
3174 * NOTE: callout does not lock port and may race us modifying
3175 * the flags, so make sure its stopped.
3177 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3178 callout_stop(&ccb->ccb_timeout);
3179 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3181 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3183 if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) {
3184 ahci_issue_pending_commands(ccb->ccb_port,
3185 xa->flags & ATA_F_NCQ);
3188 ahci_unload_prdt(ccb);
3190 if (xa->state == ATA_S_ONCHIP)
3191 xa->state = ATA_S_COMPLETE;
3193 else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
3194 kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
3196 PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
3198 if (xa->state != ATA_S_TIMEOUT)
3203 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3204 * while the callout is runing.
3206 * We can't safely get the port lock here or delay, we could block
3207 * the callout thread.
3210 ahci_ata_cmd_timeout_unserialized(void *arg)
3212 struct ahci_ccb *ccb = arg;
3213 struct ahci_port *ap = ccb->ccb_port;
3215 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3216 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3217 ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3221 ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3223 struct ata_xfer *xa = &ccb->ccb_xa;
3224 struct ahci_port *ap = ccb->ccb_port;
3225 volatile u_int32_t *active;
3226 int ccb_was_started, ncq_cmd;
3230 kprintf("%s: CMD TIMEOUT state=%d cmd-reg 0x%b\n"
3231 "\tsactive=%08x active=%08x\n"
3232 "\t sact=%08x ci=%08x\n",
3233 ATANAME(ap, ccb->ccb_xa.at),
3235 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3236 ap->ap_sactive, ap->ap_active,
3237 ahci_pread(ap, AHCI_PREG_SACT),
3238 ahci_pread(ap, AHCI_PREG_CI));
3241 * NOTE: Timeout will not be running if the command was polled.
3242 * If we got here at least one of these flags should be set.
3244 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
3245 ATA_F_TIMEOUT_RUNNING));
3246 xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3247 ncq_cmd = (xa->flags & ATA_F_NCQ);
3248 active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
3250 if (ccb->ccb_xa.state == ATA_S_PENDING) {
3251 DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
3252 "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
3253 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3254 ccb_was_started = 0;
3255 } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3256 DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
3257 "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
3258 (*active & (1 << ccb->ccb_slot)) ?
3259 " but slot is still active?" : ".");
3261 } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
3262 (1 << ccb->ccb_slot)) == 0 &&
3263 (*active & (1 << ccb->ccb_slot))) {
3264 kprintf("%s: ahci_port_intr() failed to detect "
3265 "completed slot\n", ATANAME(ap, ccb->ccb_xa.at));
3266 *active &= ~(1 << ccb->ccb_slot);
3268 KKASSERT(ap->ap_active_cnt > 0);
3269 --ap->ap_active_cnt;
3274 ccb_was_started = 1;
3277 /* Complete the slot with a timeout error. */
3278 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3279 *active &= ~(1 << ccb->ccb_slot);
3281 KKASSERT(ap->ap_active_cnt > 0);
3282 --ap->ap_active_cnt;
3284 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
3285 ccb->ccb_done(ccb); /* This won't issue pending commands or run the
3286 atascsi completion. */
3288 /* Reset port to abort running command. */
3289 if (ccb_was_started) {
3290 DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
3291 "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
3292 : "", ccb->ccb_slot, *active);
3294 if (ccb->ccb_xa.at && ap->ap_type == ATA_PORT_T_PM) {
3295 /* XXX how do we unbrick a PM target? */
3296 kprintf("%s: PM target bricked and timed-out, "
3297 "disabling PM target but trying to "
3298 "leave the port intact\n",
3299 ATANAME(ap, ccb->ccb_xa.at));
3300 ccb->ccb_xa.at->at_probe = ATA_PROBE_FAILED;
3301 ahci_port_intr(ap, 1);
3302 ahci_port_stop(ap, 0);
3304 ahci_port_start(ap);
3306 } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) {
3308 * If the softreset failed place the port in a
3309 * failed state and use ahci_port_intr() to cancel
3310 * any remaining commands.
3312 kprintf("%s: Unable to reset during timeout, port "
3315 ap->ap_state = AP_S_FATAL_ERROR;
3316 ahci_port_intr(ap, 1);
3323 * Restart any other commands that were aborted
3327 DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
3328 "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
3331 ahci_pwrite(ap, AHCI_PREG_SACT, *active);
3332 ahci_pwrite(ap, AHCI_PREG_CI, *active);
3337 /* Issue any pending commands now. */
3338 DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
3339 if (ccb_was_started)
3340 ahci_issue_pending_commands(ap, ncq_cmd);
3341 else if (ap->ap_active == 0)
3342 ahci_issue_pending_ncq_commands(ap);
3344 /* Complete the timed out ata_xfer I/O (may generate new I/O). */
3345 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
3348 DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
3354 * Used by the softreset, pmprobe, and read_ncq_error only, in very
3355 * specialized, controlled circumstances.
3357 * Only one command may be pending.
3360 ahci_quick_timeout(struct ahci_ccb *ccb)
3362 struct ahci_port *ap = ccb->ccb_port;
3364 switch (ccb->ccb_xa.state) {
3366 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3367 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3370 KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3371 ap->ap_sactive == 0);
3372 ahci_port_stop(ap, 0);
3373 ahci_port_start(ap);
3375 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3376 ap->ap_active &= ~(1 << ccb->ccb_slot);
3377 KKASSERT(ap->ap_active_cnt > 0);
3378 --ap->ap_active_cnt;
3381 panic("%s: ahci_quick_timeout: ccb in bad state %d",
3382 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3387 ahci_empty_done(struct ahci_ccb *ccb)
3389 if (ccb->ccb_xa.state == ATA_S_ONCHIP)
3390 ccb->ccb_xa.state = ATA_S_COMPLETE;