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_init(struct ahci_port *ap, struct ata_port *at);
55 int ahci_port_start(struct ahci_port *);
56 int ahci_port_stop(struct ahci_port *, int);
57 int ahci_port_clo(struct ahci_port *);
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 *arg);
72 void ahci_issue_pending_ncq_commands(struct ahci_port *);
73 void ahci_issue_pending_commands(struct ahci_port *, int);
75 struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *);
76 void ahci_put_err_ccb(struct ahci_ccb *);
78 int ahci_port_read_ncq_error(struct ahci_port *, int *);
80 struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
81 void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
82 static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
84 void ahci_empty_done(struct ahci_ccb *ccb);
85 void ahci_ata_cmd_done(struct ahci_ccb *ccb);
87 /* Wait for all bits in _b to be cleared */
88 #define ahci_pwait_clr(_ap, _r, _b) \
89 ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
90 #define ahci_pwait_clr_to(_ap, _to, _r, _b) \
91 ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
93 /* Wait for all bits in _b to be set */
94 #define ahci_pwait_set(_ap, _r, _b) \
95 ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
96 #define ahci_pwait_set_to(_ap, _to, _r, _b) \
97 ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
99 #define AHCI_PWAIT_TIMEOUT 1000
102 * Initialize the global AHCI hardware. This code does not set up any of
106 ahci_init(struct ahci_softc *sc)
110 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
111 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
113 /* save BIOS initialised parameters, enable staggered spin up */
114 cap = ahci_read(sc, AHCI_REG_CAP);
115 cap &= AHCI_REG_CAP_SMPS;
116 cap |= AHCI_REG_CAP_SSS;
117 pi = ahci_read(sc, AHCI_REG_PI);
120 * Unconditionally reset the controller, do not conditionalize on
121 * trying to figure it if it was previously active or not.
123 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR);
124 if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR,
125 AHCI_REG_GHC_HR) != 0) {
126 device_printf(sc->sc_dev,
127 "unable to reset controller\n");
131 /* enable ahci (global interrupts disabled) */
132 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
134 /* restore parameters */
135 ahci_write(sc, AHCI_REG_CAP, cap);
136 ahci_write(sc, AHCI_REG_PI, pi);
142 * Allocate and initialize an AHCI port.
145 ahci_port_alloc(struct ahci_softc *sc, u_int port)
147 struct ahci_port *ap;
149 struct ahci_ccb *ccb;
152 struct ahci_cmd_hdr *hdr;
153 struct ahci_cmd_table *table;
158 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
160 device_printf(sc->sc_dev,
161 "unable to allocate memory for port %d\n",
166 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
167 device_get_name(sc->sc_dev),
168 device_get_unit(sc->sc_dev),
170 sc->sc_ports[port] = ap;
173 * Allocate enough so we never have to reallocate, it makes
176 * ap_pmcount will be reduced by the scan if we encounter the
177 * port multiplier port prior to target 15.
179 if (ap->ap_ata == NULL) {
180 ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * AHCI_MAX_PMPORTS,
181 M_DEVBUF, M_INTWAIT | M_ZERO);
182 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
184 at->at_ahci_port = ap;
186 at->at_probe = ATA_PROBE_NEED_INIT;
187 ksnprintf(at->at_name, sizeof(at->at_name),
188 "%s.%d", ap->ap_name, i);
191 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
192 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
193 device_printf(sc->sc_dev,
194 "unable to create register window for port %d\n",
201 ap->ap_probe = ATA_PROBE_NEED_INIT;
202 TAILQ_INIT(&ap->ap_ccb_free);
203 TAILQ_INIT(&ap->ap_ccb_pending);
204 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
206 /* Disable port interrupts */
207 ahci_pwrite(ap, AHCI_PREG_IE, 0);
210 * Sec 10.1.2 - deinitialise port if it is already running
212 cmd = ahci_pread(ap, AHCI_PREG_CMD);
213 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
214 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
215 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
218 r = ahci_port_stop(ap, 1);
220 device_printf(sc->sc_dev,
221 "unable to disable %s, ignoring port %d\n",
222 ((r == 2) ? "CR" : "FR"), port);
227 /* Write DET to zero */
228 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
232 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
233 if (ap->ap_dmamem_rfis == NULL) {
234 kprintf("%s: NORFIS\n", PORTNAME(ap));
238 /* Setup RFIS base address */
239 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
240 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
241 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
242 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
244 /* Enable FIS reception and activate port. */
245 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
246 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
247 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
248 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
250 /* Check whether port activated. Skip it if not. */
251 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
252 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
253 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
258 /* Allocate a CCB for each command slot */
259 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
261 if (ap->ap_ccbs == NULL) {
262 device_printf(sc->sc_dev,
263 "unable to allocate command list for port %d\n",
268 /* Command List Structures and Command Tables */
269 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
270 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
271 if (ap->ap_dmamem_cmd_table == NULL ||
272 ap->ap_dmamem_cmd_list == NULL) {
274 device_printf(sc->sc_dev,
275 "unable to allocate DMA memory for port %d\n",
280 /* Setup command list base address */
281 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
282 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
283 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
285 /* Split CCB allocation into CCBs and assign to command header/table */
286 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
287 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
288 for (i = 0; i < sc->sc_ncmds; i++) {
289 ccb = &ap->ap_ccbs[i];
291 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
294 device_printf(sc->sc_dev,
295 "unable to create dmamap for port %d "
296 "ccb %d\n", port, i);
300 callout_init(&ccb->ccb_timeout);
303 ccb->ccb_cmd_hdr = &hdr[i];
304 ccb->ccb_cmd_table = &table[i];
305 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
306 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
307 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
308 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
311 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
312 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
315 ccb->ccb_xa.state = ATA_S_COMPLETE;
319 /* Wait for ICC change to complete */
320 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
323 * Do device-related port initialization. A failure here does not
324 * cause the port to be deallocated as we want to receive future
327 ahci_port_init(ap, NULL);
330 ahci_port_free(sc, port);
336 * [re]initialize an idle port. No CCBs should be active.
338 * If at is NULL we are initializing a directly connected port, otherwise
339 * we are indirectly initializing a port multiplier port.
341 * This function is called during the initial port allocation sequence
342 * and is also called on hot-plug insertion. We take no chances and
343 * use a portreset instead of a softreset.
345 * This function is the only way to move a failed port back to active
348 * Returns 0 if a device is successfully detected.
351 ahci_port_init(struct ahci_port *ap, struct ata_port *at)
357 * Clear all notification bits
359 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
360 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
363 * Hard-reset the port. If a device is detected but it is busy
364 * we try a second time, this time cycling the phy as well.
366 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
367 rc = ahci_port_reset(ap, at, 1);
369 rc = ahci_port_reset(ap, at, 2);
375 * We had problems talking to the device on the port.
377 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
378 case AHCI_PREG_SSTS_DET_DEV_NE:
379 kprintf("%s: Device not communicating\n", PORTNAME(ap));
381 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
382 kprintf("%s: PHY offline\n", PORTNAME(ap));
385 kprintf("%s: No device detected\n", PORTNAME(ap));
392 * The device on the port is still telling us its busy,
393 * which means that it is not properly handling a SATA
396 * It may be possible to softreset the device using CLO
397 * and a device reset command.
399 kprintf("%s: Device on port is bricked, trying softreset\n",
402 rc = ahci_port_reset(ap, at, 0);
404 kprintf("%s: Unable unbrick device\n",
407 kprintf("%s: Successfully unbricked\n",
417 * Command transfers can only be enabled if a device was successfully
420 * Allocate or deallocate the ap_ata array here too.
422 switch(ap->ap_type) {
423 case ATA_PORT_T_NONE:
435 * Start the port if we succeeded.
437 * There's nothing to start for devices behind a port multiplier.
439 if (rc == 0 && at == NULL) {
440 if (ahci_port_start(ap)) {
441 kprintf("%s: failed to start command DMA on port, "
442 "disabling\n", PORTNAME(ap));
443 rc = ENXIO; /* couldn't start port */
448 * Flush interrupts on the port. XXX
450 * Enable interrupts on the port whether a device is sitting on
451 * it or not, to handle hot-plug events.
454 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
455 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num);
457 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
458 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
459 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
460 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
462 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
463 data |= AHCI_PREG_IE_SDBE;
465 if (sc->sc_ccc_ports & (1 << port)
466 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
468 ahci_pwrite(ap, AHCI_PREG_IE, data);
474 * Run the port / target state machine from a main context.
476 * The state machine for the port is always run.
478 * If atx is non-NULL run the state machine for a particular target.
479 * If atx is NULL run the state machine for all targets.
482 ahci_port_state_machine(struct ahci_port *ap)
489 if (ap->ap_type == ATA_PORT_T_NONE) {
490 if (ap->ap_probe == ATA_PROBE_NEED_INIT) {
491 for (target = 0; target < AHCI_MAX_PMPORTS; ++target) {
492 at = &ap->ap_ata[target];
493 at->at_probe = ATA_PROBE_NEED_INIT;
495 ahci_port_init(ap, NULL);
497 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
498 ahci_port_reset(ap, NULL, 1);
499 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
500 ahci_port_reset(ap, NULL, 0);
501 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
502 ahci_cam_probe(ap, NULL);
504 if (ap->ap_type != ATA_PORT_T_PM) {
505 if (ap->ap_probe == ATA_PROBE_FAILED) {
506 ahci_cam_changed(ap, NULL, 0);
507 } else if (ap->ap_probe == ATA_PROBE_GOOD) {
508 ahci_cam_changed(ap, NULL, 1);
514 if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) {
515 kprintf("%s: PM unable to read hot-plug bitmap\n",
519 data &= (1 << ap->ap_pmcount) - 1;
522 * Stop if no ports on the target have indicated a state
529 * New devices showing up in the bitmap require some spin-up
530 * time before we start probing them. Reset didsleep. The
531 * first new device we detect will sleep before probing.
535 for (target = 0; target < ap->ap_pmcount; ++target) {
536 at = &ap->ap_ata[target];
539 * Check the target state for targets behind the PM
540 * which have changed state. This will adjust
541 * at_probe and set ATA_PORT_F_RESCAN
543 * We want to wait at least 4 seconds before probing
544 * a newly inserted device. If the check status
545 * indicates a device is present and in need of a
546 * hard reset, we make sure we have slept before
549 if (data & (1 << target)) {
550 ahci_pm_check_good(ap, target);
551 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) {
560 * Run through the state machine as necessary.
562 if (at->at_type == ATA_PORT_T_NONE &&
563 at->at_probe != ATA_PROBE_FAILED) {
564 if (at->at_probe == ATA_PROBE_NEED_INIT)
565 ahci_port_init(ap, at);
566 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
567 ahci_port_reset(ap, at, 1);
568 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
569 ahci_port_reset(ap, at, 0);
570 if (at->at_probe == ATA_PROBE_NEED_IDENT)
571 ahci_cam_probe(ap, at);
574 if (data & (1 << target)) {
575 kprintf("%s: HOTPLUG event, ",
577 if (at->at_probe == ATA_PROBE_GOOD)
578 kprintf("device inserted\n");
580 kprintf("device removed\n");
584 * Initial conditions set automatic add/rem
586 if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET)
587 at->at_features |= ATA_PORT_F_RESCAN;
590 * add or remove from CAM
592 if (at->at_features & ATA_PORT_F_RESCAN) {
593 at->at_features &= ~ATA_PORT_F_RESCAN;
594 if (at->at_probe == ATA_PROBE_FAILED) {
595 ahci_cam_changed(ap, at, 0);
596 } else if (at->at_probe == ATA_PROBE_GOOD) {
597 ahci_cam_changed(ap, at, 1);
606 * De-initialize and detach a port.
609 ahci_port_free(struct ahci_softc *sc, u_int port)
611 struct ahci_port *ap = sc->sc_ports[port];
612 struct ahci_ccb *ccb;
615 * Ensure port is disabled and its interrupts are all flushed.
618 ahci_port_stop(ap, 1);
619 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
620 ahci_pwrite(ap, AHCI_PREG_IE, 0);
621 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
622 ahci_write(sc, AHCI_REG_IS, 1 << port);
626 while ((ccb = ahci_get_ccb(ap)) != NULL) {
627 if (ccb->ccb_dmamap) {
628 bus_dmamap_destroy(sc->sc_tag_data,
630 ccb->ccb_dmamap = NULL;
633 kfree(ap->ap_ccbs, M_DEVBUF);
637 if (ap->ap_dmamem_cmd_list) {
638 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
639 ap->ap_dmamem_cmd_list = NULL;
641 if (ap->ap_dmamem_rfis) {
642 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
643 ap->ap_dmamem_rfis = NULL;
645 if (ap->ap_dmamem_cmd_table) {
646 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
647 ap->ap_dmamem_cmd_table = NULL;
650 kfree(ap->ap_ata, M_DEVBUF);
654 /* bus_space(9) says we dont free the subregions handle */
657 sc->sc_ports[port] = NULL;
661 * Start high-level command processing on the port
664 ahci_port_start(struct ahci_port *ap)
666 u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd;
669 * FRE must be turned on before ST. Wait for FR to go active
670 * before turning on ST. The spec doesn't seem to think this
671 * is necessary but waiting here avoids an on-off race in the
672 * ahci_port_stop() code.
675 olds = ahci_pread(ap, AHCI_PREG_SERR);
676 oldis= ahci_pread(ap, AHCI_PREG_IS);
677 oldtfd = ahci_pread(ap, AHCI_PREG_TFD);
678 oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
679 if ((r & AHCI_PREG_CMD_FRE) == 0) {
680 r |= AHCI_PREG_CMD_FRE;
681 ahci_pwrite(ap, AHCI_PREG_CMD, r);
683 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
684 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
685 kprintf("%s: Cannot start FIS reception\n",
692 * Turn on ST, wait for CR to come up.
694 r |= AHCI_PREG_CMD_ST;
695 ahci_pwrite(ap, AHCI_PREG_CMD, r);
696 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
697 s = ahci_pread(ap, AHCI_PREG_SERR);
698 is = ahci_pread(ap, AHCI_PREG_IS);
699 tfd = ahci_pread(ap, AHCI_PREG_TFD);
700 kprintf("%s: Cannot start command DMA\n"
703 "OLDIS=%b\nNEWIS=%b\n"
704 "OLDTFD=%b\nNEWTFD=%b\n",
706 oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR,
707 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
708 oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS,
709 oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS);
715 * (Re-)enable coalescing on the port.
717 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
718 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
719 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
720 ap->ap_sc->sc_ccc_ports_cur);
728 * Stop high-level command processing on a port
731 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
737 * Disable coalescing on the port while it is stopped.
739 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
740 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
741 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
742 ap->ap_sc->sc_ccc_ports_cur);
747 * Turn off ST, then wait for CR to go off.
749 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
750 r &= ~AHCI_PREG_CMD_ST;
751 ahci_pwrite(ap, AHCI_PREG_CMD, r);
753 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
754 kprintf("%s: Port bricked, unable to stop (ST)\n",
761 * Turn off FRE, then wait for FR to go off. FRE cannot
762 * be turned off until CR transitions to 0.
764 if ((r & AHCI_PREG_CMD_FR) == 0) {
765 kprintf("%s: FR stopped, clear FRE for next start\n",
771 r &= ~AHCI_PREG_CMD_FRE;
772 ahci_pwrite(ap, AHCI_PREG_CMD, r);
773 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
774 kprintf("%s: Port bricked, unable to stop (FRE)\n",
784 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
787 ahci_port_clo(struct ahci_port *ap)
789 struct ahci_softc *sc = ap->ap_sc;
792 /* Only attempt CLO if supported by controller */
793 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
797 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
799 if (cmd & AHCI_PREG_CMD_ST) {
800 kprintf("%s: CLO requested while port running\n",
804 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
806 /* Wait for completion */
807 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
808 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
818 * If hard is 0 perform a softreset of the port.
819 * If hard is 1 perform a hard reset of the port.
820 * If hard is 2 perform a hard reset of the port and cycle the phy.
822 * If at is non-NULL an indirect port via a port-multiplier is being
823 * reset, otherwise a direct port is being reset.
825 * NOTE: Indirect ports can only be soft-reset.
828 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
834 rc = ahci_pm_hardreset(ap, at->at_target, hard);
836 rc = ahci_port_hardreset(ap, hard);
839 rc = ahci_pm_softreset(ap, at->at_target);
841 rc = ahci_port_softreset(ap);
843 if (rc && at == NULL)
844 rc = ahci_port_hardreset(ap, hard);
851 * AHCI soft reset, Section 10.4.1
853 * (at) will be NULL when soft-resetting a directly-attached device, and
854 * non-NULL when soft-resetting a device through a port multiplier.
856 * This function keeps port communications intact and attempts to generate
857 * a reset to the connected device using device commands.
860 ahci_port_softreset(struct ahci_port *ap)
862 struct ahci_ccb *ccb = NULL;
863 struct ahci_cmd_hdr *cmd_slot;
870 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
871 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
873 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
876 ap->ap_flags |= AP_F_IN_RESET;
877 ap->ap_state = AP_S_NORMAL;
880 * Remember port state in cmd (main to restore start/stop)
884 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
885 if (ahci_port_stop(ap, 0)) {
886 kprintf("%s: failed to stop port, cannot softreset\n",
892 * Request CLO if device appears hung.
894 if (ahci_pread(ap, AHCI_PREG_TFD) &
895 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
900 * This is an attempt to clear errors so a new signature will
901 * be latched. It isn't working properly. XXX
904 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
907 if (ahci_port_start(ap)) {
908 kprintf("%s: failed to start port, cannot softreset\n",
913 /* Check whether CLO worked */
914 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
915 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
916 kprintf("%s: CLO %s, need port reset\n",
918 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
919 ? "failed" : "unsupported");
925 * Prep first D2H command with SRST feature & clear busy/reset flags
927 * It is unclear which other fields in the FIS are used. Just zero
930 ccb = ahci_get_err_ccb(ap);
931 ccb->ccb_xa.at = NULL;
932 cmd_slot = ccb->ccb_cmd_hdr;
934 fis = ccb->ccb_cmd_table->cfis;
935 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
936 fis[0] = ATA_FIS_TYPE_H2D;
937 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
940 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
941 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
942 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
944 ccb->ccb_xa.state = ATA_S_PENDING;
945 ccb->ccb_xa.flags = 0;
946 if (ahci_poll(ccb, 1000, NULL) != 0 ||
947 ccb->ccb_xa.state != ATA_S_COMPLETE) {
948 kprintf("%s: First FIS failed\n", PORTNAME(ap));
953 * The device may muff the PHY up.
955 ahci_os_sleep(10); /* 3ms min, use 10 */
958 * Prep second D2H command to read status and complete reset sequence
959 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
960 * Rev 2.6 and it is unclear how the second FIS should be set up
961 * from the AHCI document.
963 * Give the device 3ms before sending the second FIS.
965 * It is unclear which other fields in the FIS are used. Just zero
968 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
969 fis[0] = ATA_FIS_TYPE_H2D;
970 fis[15] = ATA_FIS_CONTROL_4BIT;
973 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
975 ccb->ccb_xa.state = ATA_S_PENDING;
976 ccb->ccb_xa.flags = 0;
977 if (ahci_poll(ccb, 1000, NULL) != 0 ||
978 ccb->ccb_xa.state != ATA_S_COMPLETE) {
979 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
983 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
984 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
985 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
987 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
994 * If the softreset is trying to clear a BSY condition after a
995 * normal portreset we assign the port type.
997 * If the softreset is being run first as part of the ccb error
998 * processing code then report if the device signature changed
1001 if (ap->ap_type == ATA_PORT_T_NONE) {
1002 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1004 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1005 kprintf("%s: device signature unexpectedly "
1006 "changed\n", PORTNAME(ap));
1007 error = EBUSY; /* XXX */
1016 * Abort our command, if it failed, by stopping command DMA.
1018 if (error && (ap->ap_active & (1 << ccb->ccb_slot))) {
1019 kprintf("%s: stopping the port, softreset slot "
1020 "%d was still active.\n",
1023 ahci_port_stop(ap, 0);
1025 ccb->ccb_xa.state = ATA_S_ERROR;
1027 ahci_put_err_ccb(ccb);
1030 * If the target is busy use CLO to clear the busy
1031 * condition. The BSY should be cleared on the next
1034 if (ahci_pread(ap, AHCI_PREG_TFD) &
1035 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1041 * If we failed to softreset make the port quiescent, otherwise
1042 * make sure the port's start/stop state matches what it was on
1045 * Don't kill the port if the softreset is on a port multiplier
1046 * target, that would kill all the targets!
1049 ahci_port_hardstop(ap);
1050 /* ap_probe set to failed */
1051 } else if (cmd & AHCI_PREG_CMD_ST) {
1052 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1053 kprintf("%s: STARTING PORT\n", PORTNAME(ap));
1054 ahci_port_start(ap);
1056 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1057 kprintf("%s: STOPPING PORT\n", PORTNAME(ap));
1058 ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE));
1060 ap->ap_flags &= ~AP_F_IN_RESET;
1063 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1069 * AHCI port reset, Section 10.4.2
1071 * This function does a hard reset of the port. Note that the device
1072 * connected to the port could still end-up hung.
1075 ahci_port_hardreset(struct ahci_port *ap, int hard)
1082 DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
1084 ap->ap_flags |= AP_F_IN_RESET;
1089 ahci_port_stop(ap, 0);
1090 ap->ap_state = AP_S_NORMAL;
1094 * The port may have been quiescent with its SUD bit cleared, so
1095 * set the SUD (spin up device).
1097 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1098 cmd |= AHCI_PREG_CMD_SUD;
1099 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1102 * Perform device detection. Cycle the PHY off, wait 10ms.
1103 * This simulates the SATA cable being physically unplugged.
1105 ap->ap_type = ATA_PORT_T_NONE;
1107 r = AHCI_PREG_SCTL_IPM_DISABLED;
1109 r |= AHCI_PREG_SCTL_DET_DISABLE;
1110 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1114 * Start transmitting COMRESET. COMRESET must be sent for at
1117 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1118 if (AhciForceGen1 & (1 << ap->ap_num)) {
1119 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1120 r |= AHCI_PREG_SCTL_SPD_GEN1;
1122 r |= AHCI_PREG_SCTL_SPD_ANY;
1124 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1128 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1129 * since we are hard-resetting the port we might as well clear
1130 * the whole enchillada
1133 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1134 r &= ~AHCI_PREG_SCTL_DET_INIT;
1135 r |= AHCI_PREG_SCTL_DET_NONE;
1136 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1139 * Try to determine if there is a device on the port.
1141 * Give the device 3/10 second to at least be detected.
1142 * If we fail clear PRCS (phy detect) since we may cycled
1143 * the phy and probably caused another PRCS interrupt.
1145 for (loop = 30; loop; --loop) {
1146 r = ahci_pread(ap, AHCI_PREG_SSTS);
1147 if (r & AHCI_PREG_SSTS_DET)
1152 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1153 kprintf("%s: Port appears to be unplugged\n",
1159 * There is something on the port. Give the device 3 seconds
1160 * to fully negotiate.
1163 ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1164 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1165 kprintf("%s: Device may be powered down\n",
1171 * Wait for the device to become ready.
1173 * This can take more then a second, give it 3 seconds. If we
1174 * succeed give the device another 3ms after that.
1176 * NOTE: Port multipliers can do two things here. First they can
1177 * return device-ready if a device is on target 0 and also
1178 * return the signature for that device. If there is no
1179 * device on target 0 then BSY/DRQ is never cleared and
1180 * it never comes ready.
1183 ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
1184 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1186 * The device is bricked or its a port multiplier and will
1187 * not unbusy until we do the pmprobe CLO softreset sequence.
1189 error = ahci_port_pmprobe(ap);
1191 kprintf("%s: Device will not come ready 0x%b\n",
1193 ahci_pread(ap, AHCI_PREG_TFD),
1196 ap->ap_type = ATA_PORT_T_PM;
1198 } else if (error == 0) {
1200 * We generally will not get a port multiplier signature in
1201 * this case even if this is a port multiplier, because of
1202 * Intel's stupidity. We almost certainly got target 0
1203 * behind the PM, if there is a PM.
1205 * Save the signature and probe for a PM. If we do not
1206 * find a PM then use the saved signature and return
1209 type = ahci_port_signature_detect(ap, NULL);
1210 error = ahci_port_pmprobe(ap);
1213 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1216 ap->ap_type = ATA_PORT_T_PM;
1217 kprintf("%s: Port multiplier detected\n",
1223 * hard-stop the port if we failed. This will set ap_probe
1226 ap->ap_flags &= ~AP_F_IN_RESET;
1228 ahci_port_hardstop(ap);
1229 /* ap_probe set to failed */
1231 ap->ap_probe = (ap->ap_type == ATA_PORT_T_PM) ?
1232 ATA_PROBE_GOOD : ATA_PROBE_NEED_SOFT_RESET;
1238 * AHCI port multiplier probe. This routine is run by the hardreset code
1239 * if it gets past the device detect, whether or not BSY is found to be
1242 * We MUST use CLO to properly probe whether the port multiplier exists
1245 * Return 0 on success, non-zero on failure.
1248 ahci_port_pmprobe(struct ahci_port *ap)
1250 struct ahci_cmd_hdr *cmd_slot;
1251 struct ahci_ccb *ccb = NULL;
1252 u_int8_t *fis = NULL;
1258 * If we don't support port multipliers don't try to detect one.
1260 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0)
1265 kprintf("%s: START PMPROBE\n", PORTNAME(ap));
1269 * This code is only called from hardreset, which does not
1270 * high level command processing. The port should be stopped.
1272 * Set PMA mode while the port is stopped.
1274 * NOTE: On retry the port might be running, stopped, or failed.
1276 ahci_port_stop(ap, 0);
1277 ap->ap_state = AP_S_NORMAL;
1278 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1279 cmd |= AHCI_PREG_CMD_PMA;
1280 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1283 * Flush any errors and request CLO unconditionally, then start
1288 if (ahci_port_start(ap)) {
1289 kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
1295 * Check whether CLO worked
1297 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1298 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1299 kprintf("%s: PMPROBE CLO %s, need port reset\n",
1301 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1302 ? "failed" : "unsupported");
1308 * Prep the first H2D command with SRST feature & clear busy/reset
1311 ccb = ahci_get_err_ccb(ap);
1312 cmd_slot = ccb->ccb_cmd_hdr;
1314 fis = ccb->ccb_cmd_table->cfis;
1315 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1316 fis[0] = ATA_FIS_TYPE_H2D;
1317 fis[1] = 0x0F; /* Target 15 */
1318 fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT;
1320 cmd_slot->prdtl = 0;
1321 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1322 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1323 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1324 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1326 ccb->ccb_xa.state = ATA_S_PENDING;
1327 ccb->ccb_xa.flags = 0;
1329 if (ahci_poll(ccb, 1000, NULL) != 0 ||
1330 ccb->ccb_xa.state != ATA_S_COMPLETE) {
1331 kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap));
1334 ahci_put_err_ccb(ccb);
1339 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1340 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1341 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
1345 * The device may have muffed up the PHY when it reset.
1349 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1350 /* ahci_pm_phy_status(ap, 15, &cmd); */
1353 * Prep second D2H command to read status and complete reset sequence
1354 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1355 * Rev 2.6 and it is unclear how the second FIS should be set up
1356 * from the AHCI document.
1358 * Give the device 3ms before sending the second FIS.
1360 * It is unclear which other fields in the FIS are used. Just zero
1363 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1364 fis[0] = ATA_FIS_TYPE_H2D;
1366 fis[15] = ATA_FIS_CONTROL_4BIT;
1368 cmd_slot->prdtl = 0;
1369 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1370 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1372 ccb->ccb_xa.state = ATA_S_PENDING;
1373 ccb->ccb_xa.flags = 0;
1375 if (ahci_poll(ccb, 1000, NULL) != 0 ||
1376 ccb->ccb_xa.state != ATA_S_COMPLETE) {
1377 kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap));
1380 ahci_put_err_ccb(ccb);
1387 * What? We succeeded? Yup, but for some reason the signature
1388 * is still latched from the original detect (that saw target 0
1389 * behind the PM), and I don't know how to clear the condition
1390 * other then by retrying the whole reset sequence.
1394 ahci_put_err_ccb(ccb);
1399 * Get the signature. The caller sets the ap fields.
1401 if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
1402 ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
1409 * Fall through / clean up the CCB and perform error processing.
1414 * Abort our command, if it failed, by stopping command DMA.
1417 kprintf("rc=%d active=%08x sactive=%08x slot=%d\n",
1418 rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot);
1420 if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
1421 kprintf("%s: PMP stopping the port, softreset slot "
1422 "%d was still active.\n",
1425 ahci_port_stop(ap, 0);
1427 ccb->ccb_xa.state = ATA_S_ERROR;
1429 ahci_put_err_ccb(ccb);
1432 if (rc == 0 && ahci_pm_identify(ap)) {
1433 kprintf("%s: PM - cannot identify port multiplier\n",
1438 if (rc == 0 && ahci_pm_set_feature(ap, ATA_SATAFT_ASYNCNOTIFY, 1)) {
1439 kprintf("%s: PM - Warning, cannot enable async notify\n",
1445 if (ahci_pm_read(ap, 2, 4, &data))
1446 kprintf("Cannot read snotify\n");
1448 kprintf("Read snotify %08x\n", data);
1453 * If we failed turn off PMA, otherwise identify the port multiplier.
1454 * CAM will iterate the devices.
1457 ahci_port_stop(ap, 0);
1458 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1459 cmd &= ~AHCI_PREG_CMD_PMA;
1460 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1462 ahci_port_stop(ap, 0);
1465 kprintf("%s: END PMPROBE\n", PORTNAME(ap));
1473 * Hard-stop on hot-swap device removal. See 10.10.1
1475 * Place the port in a mode that will allow it to detect hot-swap insertions.
1476 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1477 * seem to do the job.
1480 ahci_port_hardstop(struct ahci_port *ap)
1482 struct ata_port *at;
1488 * Stop the port. We can't modify things like SUD if the port
1491 ap->ap_state = AP_S_FATAL_ERROR;
1492 ap->ap_probe = ATA_PROBE_FAILED;
1493 ap->ap_type = ATA_PORT_T_NONE;
1494 ahci_port_stop(ap, 0);
1495 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1498 * Clean up AT sub-ports on SATA port.
1500 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1501 at = &ap->ap_ata[i];
1502 at->at_type = ATA_PORT_T_NONE;
1503 at->at_probe = ATA_PROBE_FAILED;
1507 * Turn off port-multiplier control bit
1509 if (cmd & AHCI_PREG_CMD_PMA) {
1510 cmd &= ~AHCI_PREG_CMD_PMA;
1511 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1515 * Make sure FRE is active. There isn't anything we can do if it
1516 * fails so just ignore errors.
1518 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1519 cmd |= AHCI_PREG_CMD_FRE;
1520 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1521 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1522 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1526 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1527 * 10.10.1 place us in the Listen state.
1529 * Deactivating SUD only applies if the controller supports SUD.
1531 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
1533 if (cmd & AHCI_PREG_CMD_SUD) {
1534 cmd &= ~AHCI_PREG_CMD_SUD;
1535 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1540 * Transition su to the spin-up state. HVA shall send COMRESET and
1541 * begin initialization sequence (whatever that means).
1543 * This only applies if the controller supports SUD.
1545 cmd |= AHCI_PREG_CMD_SUD;
1546 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1550 * Transition us to the Reset state. Theoretically we send a
1551 * continuous stream of COMRESETs in this state.
1553 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1554 if (AhciForceGen1 & (1 << ap->ap_num)) {
1555 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1556 r |= AHCI_PREG_SCTL_SPD_GEN1;
1558 r |= AHCI_PREG_SCTL_SPD_ANY;
1560 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1564 * Flush SERR_DIAG_X so the TFD can update.
1569 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1570 * hopefully send us a DIAG_X-related interrupt if it receives
1571 * a COMINIT, and if not that then at least a Phy transition
1574 * If we transition INIT from 1->0 to begin the initalization
1575 * sequence it is unclear if that sequence will remain active
1576 * until the next device insertion.
1578 * If we go back to the listen state it is unclear if the
1579 * device will actually send us a COMINIT, since we aren't
1580 * sending any COMRESET's
1586 * Multiple events may have built up in the TFD. The spec is not very
1587 * clear on this but it does seem to serialize events so clearing DIAG_X
1588 * just once might not do the job during a reset sequence.
1591 ahci_flush_tfd(struct ahci_port *ap)
1595 r = ahci_pread(ap, AHCI_PREG_SERR);
1596 while (r & AHCI_PREG_SERR_DIAG_X) {
1597 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1599 r = ahci_pread(ap, AHCI_PREG_SERR);
1604 * Figure out what type of device is connected to the port, ATAPI or
1608 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1612 sig = ahci_pread(ap, AHCI_PREG_SIG);
1613 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1614 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1615 return(ATA_PORT_T_ATAPI);
1616 } else if ((sig & 0xffff0000) ==
1617 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1618 return(ATA_PORT_T_PM);
1620 return(ATA_PORT_T_DISK);
1625 * Load the DMA descriptor table for a CCB's buffer.
1628 ahci_load_prdt(struct ahci_ccb *ccb)
1630 struct ahci_port *ap = ccb->ccb_port;
1631 struct ahci_softc *sc = ap->ap_sc;
1632 struct ata_xfer *xa = &ccb->ccb_xa;
1633 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1634 bus_dmamap_t dmap = ccb->ccb_dmamap;
1635 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1638 if (xa->datalen == 0) {
1639 ccb->ccb_cmd_hdr->prdtl = 0;
1643 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1644 xa->data, xa->datalen,
1645 ahci_load_prdt_callback,
1647 ((xa->flags & ATA_F_NOWAIT) ?
1648 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1650 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1653 if (xa->flags & ATA_F_PIO)
1654 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1656 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1658 bus_dmamap_sync(sc->sc_tag_data, dmap,
1659 (xa->flags & ATA_F_READ) ?
1660 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1666 bus_dmamap_unload(sc->sc_tag_data, dmap);
1672 * Callback from BUSDMA system to load the segment list. The passed segment
1673 * list is a temporary structure.
1677 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1680 struct ahci_prdt *prd = *(void **)info;
1683 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1686 addr = segs->ds_addr;
1687 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1688 prd->dba_lo = htole32((u_int32_t)addr);
1690 KKASSERT((addr & 1) == 0);
1691 KKASSERT((segs->ds_len & 1) == 0);
1693 prd->flags = htole32(segs->ds_len - 1);
1699 *(void **)info = prd; /* return last valid segment */
1703 ahci_unload_prdt(struct ahci_ccb *ccb)
1705 struct ahci_port *ap = ccb->ccb_port;
1706 struct ahci_softc *sc = ap->ap_sc;
1707 struct ata_xfer *xa = &ccb->ccb_xa;
1708 bus_dmamap_t dmap = ccb->ccb_dmamap;
1710 if (xa->datalen != 0) {
1711 bus_dmamap_sync(sc->sc_tag_data, dmap,
1712 (xa->flags & ATA_F_READ) ?
1713 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1715 bus_dmamap_unload(sc->sc_tag_data, dmap);
1717 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1720 xa->resid = xa->datalen -
1721 le32toh(ccb->ccb_cmd_hdr->prdbc);
1726 * Start a command and poll for completion.
1728 * timeout is in ms and only counts once the command gets on-chip.
1730 * NOTE: If the caller specifies a NULL timeout function the caller is
1731 * responsible for clearing hardware state on failure, but we will
1732 * deal with removing the ccb from any pending queue.
1734 * NOTE: NCQ should never be used with this function.
1736 * NOTE: If the port is in a failed state and stopped we do not try
1737 * to activate the ccb.
1740 ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *))
1742 struct ahci_port *ap = ccb->ccb_port;
1744 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1745 ccb->ccb_xa.state = ATA_S_ERROR;
1753 if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
1754 ccb->ccb_xa.state != ATA_S_PENDING) {
1759 if (ccb->ccb_xa.state == ATA_S_ONCHIP)
1761 } while (timeout > 0);
1763 kprintf("%s: Poll timed-out for slot %d state %d\n",
1764 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, ccb->ccb_xa.state);
1766 if (timeout_fn != NULL) {
1769 if (ccb->ccb_xa.state == ATA_S_PENDING)
1770 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1771 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1781 ahci_start_timeout(struct ahci_ccb *ccb)
1783 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1784 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1785 callout_reset(&ccb->ccb_timeout,
1786 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1787 ahci_ata_cmd_timeout_unserialized, ccb);
1792 ahci_start(struct ahci_ccb *ccb)
1794 struct ahci_port *ap = ccb->ccb_port;
1795 struct ahci_softc *sc = ap->ap_sc;
1797 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1799 /* Zero transferred byte count before transfer */
1800 ccb->ccb_cmd_hdr->prdbc = 0;
1802 /* Sync command list entry and corresponding command table entry */
1803 bus_dmamap_sync(sc->sc_tag_cmdh,
1804 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1805 BUS_DMASYNC_PREWRITE);
1806 bus_dmamap_sync(sc->sc_tag_cmdt,
1807 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1808 BUS_DMASYNC_PREWRITE);
1810 /* Prepare RFIS area for write by controller */
1811 bus_dmamap_sync(sc->sc_tag_rfis,
1812 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1813 BUS_DMASYNC_PREREAD);
1815 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1817 * Issue NCQ commands only when there are no outstanding
1818 * standard commands.
1820 if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) {
1821 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1823 ahci_start_timeout(ccb);
1824 KKASSERT(ap->ap_active_cnt == 0);
1825 ap->ap_sactive |= (1 << ccb->ccb_slot);
1826 ccb->ccb_xa.state = ATA_S_ONCHIP;
1827 ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
1828 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1832 * Wait for all NCQ commands to finish before issuing standard
1833 * command. Allow up to <limit> non-NCQ commands to be active.
1835 * XXX If ap is a port multiplier only allow 1. At least the
1836 * NVidia-MCP77 part seems to barf if more then one
1837 * command is activated, even though it isn't NCQ.
1839 * If I set up more then one I get phy errors and the
1842 int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2;
1843 if (ap->ap_sactive || ap->ap_active_cnt >= limit) {
1844 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1846 ahci_start_timeout(ccb);
1847 ap->ap_active |= 1 << ccb->ccb_slot;
1848 ccb->ccb_xa.state = ATA_S_ONCHIP;
1849 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1850 ap->ap_active_cnt++;
1856 ahci_issue_pending_ncq_commands(struct ahci_port *ap)
1858 struct ahci_ccb *nextccb;
1859 u_int32_t sact_change = 0;
1861 KKASSERT(ap->ap_active_cnt == 0);
1863 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1864 if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
1867 /* Start all the NCQ commands at the head of the pending list. */
1869 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1870 ahci_start_timeout(nextccb);
1871 sact_change |= 1 << nextccb->ccb_slot;
1872 nextccb->ccb_xa.state = ATA_S_ONCHIP;
1873 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1874 } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
1876 ap->ap_sactive |= sact_change;
1877 ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
1878 ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
1884 ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
1886 struct ahci_ccb *nextccb;
1888 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1889 if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) {
1890 KKASSERT(last_was_ncq == 0); /* otherwise it should have
1891 * been started already. */
1894 * Issue NCQ commands only when there are no outstanding
1895 * standard commands.
1897 if (ap->ap_active == 0)
1898 ahci_issue_pending_ncq_commands(ap);
1900 KKASSERT(ap->ap_active_cnt > 0);
1901 } else if (nextccb) {
1902 if (ap->ap_sactive || last_was_ncq)
1903 KKASSERT(ap->ap_active_cnt == 0);
1906 * Wait for all NCQ commands to finish before issuing standard
1907 * command. Then keep up to 2 standard commands on-chip at
1913 while (ap->ap_active_cnt < 2 &&
1914 nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
1915 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1916 ahci_start_timeout(nextccb);
1917 ap->ap_active |= 1 << nextccb->ccb_slot;
1918 nextccb->ccb_xa.state = ATA_S_ONCHIP;
1919 ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
1920 ap->ap_active_cnt++;
1921 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1927 ahci_intr(void *arg)
1929 struct ahci_softc *sc = arg;
1930 u_int32_t is, ack = 0;
1933 /* Read global interrupt status */
1934 is = ahci_read(sc, AHCI_REG_IS);
1935 if (is == 0 || is == 0xffffffff)
1939 #ifdef AHCI_COALESCE
1940 /* Check coalescing interrupt first */
1941 if (is & sc->sc_ccc_mask) {
1942 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
1944 is &= ~sc->sc_ccc_mask;
1945 is |= sc->sc_ccc_ports_cur;
1949 /* Process interrupts for each port */
1952 if (sc->sc_ports[port])
1953 ahci_port_intr(sc->sc_ports[port]);
1957 /* Finally, acknowledge global interrupt */
1958 ahci_write(sc, AHCI_REG_IS, ack);
1962 ahci_port_intr(struct ahci_port *ap)
1964 struct ahci_softc *sc = ap->ap_sc;
1965 u_int32_t is, ci_saved, ci_masked;
1967 struct ahci_ccb *ccb = NULL;
1968 struct ata_port *ccb_at = NULL;
1969 volatile u_int32_t *active;
1973 enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
1974 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1976 is = ahci_pread(ap, AHCI_PREG_IS);
1977 if (is & AHCI_PREG_IS_DPS)
1978 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
1981 kprintf("%s: INTERRUPT %b\n", PORTNAME(ap),
1986 * Ack the port interrupt
1988 if (ap->ap_sactive) {
1989 /* Active NCQ commands - use SActive instead of CI */
1990 KKASSERT(ap->ap_active == 0);
1991 KKASSERT(ap->ap_active_cnt == 0);
1992 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
1993 active = &ap->ap_sactive;
1996 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
1997 active = &ap->ap_active;
2000 if (is & AHCI_PREG_IS_TFES) {
2002 * Command failed. See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2004 * This stops command processing.
2006 u_int32_t tfd, serr;
2009 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2010 serr = ahci_pread(ap, AHCI_PREG_SERR);
2013 * If no NCQ commands are active the error slot is easily
2014 * determined, otherwise we have to extract the error
2015 * from the log page.
2017 if (ap->ap_sactive == 0) {
2018 err_slot = AHCI_PREG_CMD_CCS(
2019 ahci_pread(ap, AHCI_PREG_CMD));
2020 ccb = &ap->ap_ccbs[err_slot];
2021 ccb_at = ccb->ccb_xa.at; /* can be NULL */
2023 /* Preserve received taskfile data from the RFIS. */
2024 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2025 sizeof(struct ata_fis_d2h));
2030 DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n",
2031 PORTNAME(ap), err_slot,
2032 tfd, AHCI_PFMT_TFD_STS,
2033 serr, AHCI_PFMT_SERR);
2035 /* Stopping the port clears CI and SACT */
2036 ahci_port_stop(ap, 0);
2037 need = NEED_RESTART;
2040 * Clear SERR (primarily DIAG_X) to enable capturing of the
2043 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2045 /* Acknowledge the interrupts we can recover from. */
2046 ahci_pwrite(ap, AHCI_PREG_IS,
2047 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS));
2048 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS);
2050 /* If device hasn't cleared its busy status, try to idle it. */
2051 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2052 kprintf("%s: Attempting to idle device ccb=%p\n",
2053 PORTNAME(ap), ccb_at);
2054 if (ap->ap_flags & AP_F_IN_RESET)
2057 * XXX how do we unbrick a PM target (ccb_at != NULL).
2059 * For now fail the target and use CLO to clear the
2060 * busy condition and make the ahci port usable for
2061 * the remaining devices.
2064 ccb_at->at_probe = ATA_PROBE_FAILED;
2066 } else if (ahci_port_reset(ap, ccb_at, 0)) {
2067 kprintf("%s: Unable to idle device, port "
2073 /* Had to reset device, can't gather extended info. */
2074 } else if (ap->ap_sactive) {
2076 * Recover the NCQ error from log page 10h.
2078 * XXX NCQ currently not supported with port
2081 ahci_port_read_ncq_error(ap, &err_slot);
2082 kprintf("recover from NCQ error err_slot %d\n",
2087 DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
2088 PORTNAME(ap), err_slot);
2090 ccb = &ap->ap_ccbs[err_slot];
2092 /* Didn't reset, could gather extended info from log. */
2093 kprintf("%s: didn't reset err_slot %d "
2094 "sact=%08x act=%08x\n",
2096 err_slot, ap->ap_sactive, ap->ap_active);
2100 * If we couldn't determine the errored slot, reset the port
2101 * and fail all the active slots.
2103 if (err_slot == -1) {
2104 if (ap->ap_flags & AP_F_IN_RESET)
2107 * XXX how do we unbrick a PM target (ccb_at != NULL).
2109 * For now fail the target and use CLO to clear the
2110 * busy condition and make the ahci port usable for
2111 * the remaining devices.
2114 ccb_at->at_probe = ATA_PROBE_FAILED;
2116 } else if (ahci_port_reset(ap, ccb_at, 0)) {
2117 kprintf("%s: Unable to idle device after "
2118 "NCQ error, port bricked on us\n",
2122 kprintf("%s: couldn't recover NCQ error, failing "
2123 "all outstanding commands.\n",
2128 /* Clear the failed command in saved CI so completion runs. */
2129 ci_saved &= ~(1 << err_slot);
2131 /* Note the error in the ata_xfer. */
2132 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2133 ccb->ccb_xa.state = ATA_S_ERROR;
2136 /* There may only be one outstanding standard command now. */
2137 if (ap->ap_sactive == 0) {
2140 slot = ffs(tmp) - 1;
2141 tmp &= ~(1 << slot);
2146 } else if (is & AHCI_PREG_IS_DHRS) {
2148 * Command posted D2H register FIS to the rfis. This
2149 * does NOT stop command processing and it is unclear
2150 * how we are supposed to deal with it other then using
2151 * only a queue of 1.
2153 * We must copy the port rfis to the ccb and restart
2154 * command processing. ahci_pm_read() does not function
2155 * without this support.
2159 if (ap->ap_sactive == 0) {
2160 err_slot = AHCI_PREG_CMD_CCS(
2161 ahci_pread(ap, AHCI_PREG_CMD));
2162 ccb = &ap->ap_ccbs[err_slot];
2163 ccb_at = ccb->ccb_xa.at; /* can be NULL */
2165 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2166 sizeof(struct ata_fis_d2h));
2168 kprintf("%s: Unexpected DHRS posted while "
2169 "NCQ running\n", PORTNAME(ap));
2172 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2173 is &= ~AHCI_PREG_IS_DHRS;
2177 * Device notification to us.
2179 * NOTE! On some parts notification bits can get set without
2180 * generating an interrupt. It is unclear whether this is
2181 * a bug in the PM (sending a DTOH device setbits with 'N' set
2182 * and 'I' not set), or a bug in the host controller.
2184 * It only seems to occur under load.
2186 if (/*(is & AHCI_PREG_IS_SDBS) &&*/ (sc->sc_cap & AHCI_REG_CAP_SSNTF)) {
2190 data = ahci_pread(ap, AHCI_PREG_SNTF);
2191 if (is & AHCI_PREG_IS_SDBS) {
2192 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2193 is &= ~AHCI_PREG_IS_SDBS;
2194 xstr = " (no SDBS!)";
2199 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2201 kprintf("%s: NOTIFY %08x%s\n",
2202 PORTNAME(ap), data, xstr);
2203 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_N);
2204 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2205 ahci_cam_changed(ap, NULL, -1);
2210 * Spurious IFS errors can occur while we are doing a reset
2211 * sequence through a PM. Try to recover if we are being asked
2212 * to ignore IFS errors during these periods.
2214 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2215 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2216 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2217 kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
2220 serr, AHCI_PFMT_SERR);
2221 ap->ap_flags |= AP_F_IFS_IGNORED;
2223 ap->ap_flags |= AP_F_IFS_OCCURED;
2224 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2225 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2226 is &= ~AHCI_PREG_IS_IFS;
2227 ahci_port_stop(ap, 0);
2228 ahci_port_start(ap);
2229 need = NEED_RESTART;
2233 * Port change (hot-plug).
2235 * A PCS interrupt will occur on hot-plug once communication is
2238 * A PRCS interrupt will occur on hot-unplug (and possibly also
2241 * XXX We can then check the CPS (Cold Presence State) bit, if
2242 * supported, to determine if a device is plugged in or not and do
2245 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
2246 * can also occur if an unsolicited COMINIT is received.
2247 * If this occurs command processing is automatically
2248 * stopped (CR goes inactive) and the port must be stopped
2251 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2252 ahci_pwrite(ap, AHCI_PREG_IS,
2253 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2254 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2255 ahci_pwrite(ap, AHCI_PREG_SERR,
2256 (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
2257 ahci_port_stop(ap, 0);
2258 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2259 case AHCI_PREG_SSTS_DET_DEV:
2260 if (ap->ap_type == ATA_PORT_T_NONE) {
2261 need = NEED_HOTPLUG_INSERT;
2264 need = NEED_RESTART;
2267 if (ap->ap_type != ATA_PORT_T_NONE) {
2268 need = NEED_HOTPLUG_REMOVE;
2271 need = NEED_RESTART;
2277 * Check for remaining errors - they are fatal.
2279 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2280 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2283 ahci_pwrite(ap, AHCI_PREG_IS,
2284 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2285 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2287 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2288 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2290 serr = ahci_pread(ap, AHCI_PREG_SERR);
2291 kprintf("%s: unrecoverable errors (IS: %b, SERR: %b), "
2292 "disabling port.\n",
2295 serr, AHCI_PFMT_SERR
2297 /* XXX try recovery first */
2302 * Fail all outstanding commands if we know the port won't recover.
2304 * We may have a ccb_at if the failed command is known and was
2305 * being sent to a device over a port multiplier (PM). In this
2306 * case if the port itself has not completely failed we fail just
2307 * the commands related to that target.
2309 if (ap->ap_state == AP_S_FATAL_ERROR) {
2311 ap->ap_state = AP_S_FATAL_ERROR;
2314 /* Stopping the port clears CI/SACT */
2315 ahci_port_stop(ap, 0);
2318 * Error all the active slots. If running across a PM
2319 * try to error out just the slots related to the target.
2321 ci_masked = ci_saved & *active;
2323 slot = ffs(ci_masked) - 1;
2324 ccb = &ap->ap_ccbs[slot];
2325 if (ccb_at == ccb->ccb_xa.at ||
2326 ap->ap_state == AP_S_FATAL_ERROR) {
2327 ci_masked &= ~(1 << slot);
2328 ccb->ccb_xa.state = ATA_S_ERROR;
2332 /* Run completion for all active slots. */
2333 ci_saved &= ~*active;
2336 * Don't restart the port if our problems were deemed fatal.
2338 * Also acknowlege all fatal interrupt sources to prevent
2341 if (ap->ap_state == AP_S_FATAL_ERROR) {
2342 if (need == NEED_RESTART)
2343 need = NEED_NOTHING;
2344 ahci_pwrite(ap, AHCI_PREG_IS,
2345 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2346 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2352 * CCB completion is detected by noticing its slot's bit in CI has
2353 * changed to zero some time after we activated it.
2354 * If we are polling, we may only be interested in particular slot(s).
2356 * Any active bits not saved are completed within the restrictions
2357 * imposed by the caller.
2359 ci_masked = ~ci_saved & *active;
2361 slot = ffs(ci_masked) - 1;
2362 ccb = &ap->ap_ccbs[slot];
2363 ci_masked &= ~(1 << slot);
2365 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2366 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2369 bus_dmamap_sync(sc->sc_tag_cmdh,
2370 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2371 BUS_DMASYNC_POSTWRITE);
2373 bus_dmamap_sync(sc->sc_tag_cmdt,
2374 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2375 BUS_DMASYNC_POSTWRITE);
2377 bus_dmamap_sync(sc->sc_tag_rfis,
2378 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2379 BUS_DMASYNC_POSTREAD);
2381 *active &= ~(1 << ccb->ccb_slot);
2382 if (active == &ap->ap_active) {
2383 KKASSERT(ap->ap_active_cnt > 0);
2384 --ap->ap_active_cnt;
2392 * A recoverable error occured and we can restart outstanding
2393 * commands on the port.
2395 ahci_port_start(ap);
2401 slot = ffs(tmp) - 1;
2402 tmp &= ~(1 << slot);
2403 ccb = &ap->ap_ccbs[slot];
2404 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2405 KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
2406 (!!ap->ap_sactive));
2409 DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
2410 "re-enabling%s slots %08x\n", PORTNAME(ap),
2411 ap->ap_sactive ? " NCQ" : "", ci_saved);
2414 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
2415 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
2418 case NEED_HOTPLUG_INSERT:
2420 * A hot-plug insertion event has occured and all
2421 * outstanding commands have already been revoked.
2423 * Don't recurse if this occurs while we are
2424 * resetting the port.
2426 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2427 kprintf("%s: HOTPLUG - Device inserted\n",
2429 ap->ap_probe = ATA_PROBE_NEED_INIT;
2430 ahci_cam_changed(ap, NULL, -1);
2433 case NEED_HOTPLUG_REMOVE:
2435 * A hot-plug removal event has occured and all
2436 * outstanding commands have already been revoked.
2438 * Don't recurse if this occurs while we are
2439 * resetting the port.
2441 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2442 kprintf("%s: HOTPLUG - Device removed\n",
2444 ahci_port_hardstop(ap);
2445 /* ap_probe set to failed */
2446 ahci_cam_changed(ap, NULL, -1);
2455 ahci_get_ccb(struct ahci_port *ap)
2457 struct ahci_ccb *ccb;
2459 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2460 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2462 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2463 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2464 ccb->ccb_xa.state = ATA_S_SETUP;
2465 ccb->ccb_xa.at = NULL;
2467 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2473 ahci_put_ccb(struct ahci_ccb *ccb)
2475 struct ahci_port *ap = ccb->ccb_port;
2478 if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2479 ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2480 ccb->ccb_xa.state != ATA_S_ERROR) {
2481 kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
2483 PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2488 ccb->ccb_xa.state = ATA_S_PUT;
2489 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2490 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2491 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2495 ahci_get_err_ccb(struct ahci_port *ap)
2497 struct ahci_ccb *err_ccb;
2500 /* No commands may be active on the chip. */
2501 sact = ahci_pread(ap, AHCI_PREG_SACT);
2503 kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
2504 KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
2507 KKASSERT(ap->ap_err_busy == 0);
2508 ap->ap_err_busy = 1;
2510 /* Save outstanding command state. */
2511 ap->ap_err_saved_active = ap->ap_active;
2512 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2513 ap->ap_err_saved_sactive = ap->ap_sactive;
2516 * Pretend we have no commands outstanding, so that completions won't
2519 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2522 * Grab a CCB to use for error recovery. This should never fail, as
2523 * we ask atascsi to reserve one for us at init time.
2525 err_ccb = ahci_get_ccb(ap);
2526 KKASSERT(err_ccb != NULL);
2527 err_ccb->ccb_xa.flags = 0;
2528 err_ccb->ccb_done = ahci_empty_done;
2534 ahci_put_err_ccb(struct ahci_ccb *ccb)
2536 struct ahci_port *ap = ccb->ccb_port;
2541 KKASSERT(ap->ap_err_busy);
2544 * No commands may be active on the chip
2546 sact = ahci_pread(ap, AHCI_PREG_SACT);
2548 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
2549 ccb->ccb_slot, sact);
2551 ci = ahci_pread(ap, AHCI_PREG_CI);
2553 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2554 "(act=%08x sact=%08x)\n",
2556 ap->ap_active, ap->ap_sactive);
2559 /* Done with the CCB */
2562 /* Restore outstanding command state */
2563 ap->ap_sactive = ap->ap_err_saved_sactive;
2564 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2565 ap->ap_active = ap->ap_err_saved_active;
2568 ap->ap_err_busy = 0;
2573 * Read log page to get NCQ error.
2575 * NOTE: NCQ not currently supported on port multipliers. XXX
2578 ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
2580 struct ahci_ccb *ccb;
2581 struct ahci_cmd_hdr *cmd_slot;
2583 struct ata_fis_h2d *fis;
2586 DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2588 /* Save command register state. */
2589 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
2591 /* Port should have been idled already. Start it. */
2592 KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
2593 ahci_port_start(ap);
2595 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2596 ccb = ahci_get_err_ccb(ap);
2597 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2598 ccb->ccb_xa.data = ap->ap_err_scratch;
2599 ccb->ccb_xa.datalen = 512;
2600 cmd_slot = ccb->ccb_cmd_hdr;
2601 bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
2603 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
2604 fis->type = ATA_FIS_TYPE_H2D;
2605 fis->flags = ATA_H2D_FLAGS_CMD;
2606 fis->command = ATA_C_READ_LOG_EXT;
2607 fis->lba_low = 0x10; /* queued error log page (10h) */
2608 fis->sector_count = 1; /* number of sectors (1) */
2609 fis->sector_count_exp = 0;
2610 fis->lba_mid = 0; /* starting offset */
2611 fis->lba_mid_exp = 0;
2614 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
2616 if (ahci_load_prdt(ccb) != 0) {
2617 rc = ENOMEM; /* XXX caller must abort all commands */
2621 ccb->ccb_xa.state = ATA_S_PENDING;
2622 if (ahci_poll(ccb, 1000, NULL) != 0)
2627 /* Abort our command, if it failed, by stopping command DMA. */
2628 if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
2629 kprintf("%s: log page read failed, slot %d was still active.\n",
2630 PORTNAME(ap), ccb->ccb_slot);
2631 ahci_port_stop(ap, 0);
2634 /* Done with the error CCB now. */
2635 ahci_unload_prdt(ccb);
2636 ahci_put_err_ccb(ccb);
2638 /* Extract failed register set and tags from the scratch space. */
2640 struct ata_log_page_10h *log;
2643 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2644 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2645 /* Not queued bit was set - wasn't an NCQ error? */
2646 kprintf("%s: read NCQ error page, but not an NCQ "
2651 /* Copy back the log record as a D2H register FIS. */
2652 *err_slotp = err_slot = log->err_regs.type &
2653 ATA_LOG_10H_TYPE_TAG_MASK;
2655 ccb = &ap->ap_ccbs[err_slot];
2656 memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
2657 sizeof(struct ata_fis_d2h));
2658 ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
2659 ccb->ccb_xa.rfis.flags = 0;
2663 /* Restore saved CMD register state */
2664 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
2670 * Allocate memory for various structures DMAd by hardware. The maximum
2671 * number of segments for these tags is 1 so the DMA memory will have a
2672 * single physical base address.
2674 struct ahci_dmamem *
2675 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
2677 struct ahci_dmamem *adm;
2680 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2682 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2683 BUS_DMA_ZERO, &adm->adm_map);
2686 error = bus_dmamap_load(tag, adm->adm_map,
2688 bus_dma_tag_getmaxsize(tag),
2689 ahci_dmamem_saveseg, &adm->adm_busaddr,
2694 bus_dmamap_destroy(tag, adm->adm_map);
2695 adm->adm_map = NULL;
2696 adm->adm_tag = NULL;
2697 adm->adm_kva = NULL;
2699 kfree(adm, M_DEVBUF);
2707 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2709 KKASSERT(error == 0);
2710 KKASSERT(nsegs == 1);
2711 *(bus_addr_t *)info = segs->ds_addr;
2716 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
2719 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2720 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2721 adm->adm_map = NULL;
2722 adm->adm_tag = NULL;
2723 adm->adm_kva = NULL;
2725 kfree(adm, M_DEVBUF);
2729 ahci_read(struct ahci_softc *sc, bus_size_t r)
2731 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2732 BUS_SPACE_BARRIER_READ);
2733 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2737 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
2739 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2740 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2741 BUS_SPACE_BARRIER_WRITE);
2745 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
2750 for (i = 0; i < 1000; i++) {
2751 if ((ahci_read(sc, r) & mask) != target)
2760 ahci_pread(struct ahci_port *ap, bus_size_t r)
2762 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2763 BUS_SPACE_BARRIER_READ);
2764 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2768 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
2770 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2771 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2772 BUS_SPACE_BARRIER_WRITE);
2776 ahci_pwait_eq(struct ahci_port *ap, int timeout,
2777 bus_size_t r, u_int32_t mask, u_int32_t target)
2781 for (i = 0; i < timeout; i++) {
2782 if ((ahci_pread(ap, r) & mask) == target)
2791 * Acquire an ata transfer.
2793 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2794 * targets that go through the port multiplier.
2797 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
2799 struct ahci_ccb *ccb;
2801 ccb = ahci_get_ccb(ap);
2803 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
2808 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
2809 PORTNAME(ap), ccb->ccb_slot);
2811 ccb->ccb_xa.at = at;
2812 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2814 return (&ccb->ccb_xa);
2818 ahci_ata_put_xfer(struct ata_xfer *xa)
2820 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
2822 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
2828 ahci_ata_cmd(struct ata_xfer *xa)
2830 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
2831 struct ahci_cmd_hdr *cmd_slot;
2833 KKASSERT(xa->state == ATA_S_SETUP);
2835 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2838 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2839 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2840 ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2843 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2846 ccb->ccb_done = ahci_ata_cmd_done;
2848 cmd_slot = ccb->ccb_cmd_hdr;
2849 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
2850 if (ccb->ccb_xa.at) {
2851 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
2852 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
2855 if (xa->flags & ATA_F_WRITE)
2856 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
2858 if (xa->flags & ATA_F_PACKET)
2859 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
2861 if (ahci_load_prdt(ccb) != 0)
2864 xa->state = ATA_S_PENDING;
2866 if (xa->flags & ATA_F_POLL) {
2867 ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout);
2868 return (ATA_COMPLETE);
2872 xa->flags |= ATA_F_TIMEOUT_DESIRED;
2875 return (ATA_QUEUED);
2879 xa->state = ATA_S_ERROR;
2886 ahci_ata_cmd_done(struct ahci_ccb *ccb)
2888 struct ata_xfer *xa = &ccb->ccb_xa;
2890 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2891 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2892 callout_stop(&ccb->ccb_timeout);
2894 xa->flags &= ~ATA_F_TIMEOUT_DESIRED;
2896 if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR)
2897 ahci_issue_pending_commands(ccb->ccb_port,
2898 xa->flags & ATA_F_NCQ);
2900 ahci_unload_prdt(ccb);
2902 if (xa->state == ATA_S_ONCHIP)
2903 xa->state = ATA_S_COMPLETE;
2905 else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
2906 kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
2908 PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
2910 if (xa->state != ATA_S_TIMEOUT)
2915 ahci_ata_cmd_timeout_unserialized(void *arg)
2917 struct ahci_ccb *ccb = arg;
2918 struct ahci_port *ap = ccb->ccb_port;
2920 lwkt_serialize_enter(&ap->ap_sc->sc_serializer);
2921 ahci_ata_cmd_timeout(arg);
2922 lwkt_serialize_exit(&ap->ap_sc->sc_serializer);
2926 ahci_ata_cmd_timeout(void *arg)
2928 struct ahci_ccb *ccb = arg;
2929 struct ata_xfer *xa = &ccb->ccb_xa;
2930 struct ahci_port *ap = ccb->ccb_port;
2931 volatile u_int32_t *active;
2932 int ccb_was_started, ncq_cmd;
2936 kprintf("%s: CMD TIMEOUT cmd-reg 0x%b\n"
2937 "\tsactive=%08x active=%08x\n"
2938 "\t sact=%08x ci=%08x\n",
2939 ATANAME(ap, ccb->ccb_xa.at),
2940 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2941 ap->ap_sactive, ap->ap_active,
2942 ahci_pread(ap, AHCI_PREG_SACT),
2943 ahci_pread(ap, AHCI_PREG_CI));
2946 * NOTE: Timeout will not be running if the command was polled.
2947 * If we got here at least one of these flags should be set.
2949 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
2950 ATA_F_TIMEOUT_RUNNING));
2951 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2952 ncq_cmd = (xa->flags & ATA_F_NCQ);
2953 active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
2955 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2956 DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
2957 "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
2958 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2959 ccb_was_started = 0;
2960 } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2961 DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
2962 "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
2963 (*active & (1 << ccb->ccb_slot)) ?
2964 " but slot is still active?" : ".");
2966 } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
2967 (1 << ccb->ccb_slot)) == 0 &&
2968 (*active & (1 << ccb->ccb_slot))) {
2969 DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but "
2970 "IRQ handler didn't detect it. Why?\n", PORTNAME(ap),
2972 *active &= ~(1 << ccb->ccb_slot);
2974 KKASSERT(ap->ap_active_cnt > 0);
2975 --ap->ap_active_cnt;
2980 ccb_was_started = 1;
2983 /* Complete the slot with a timeout error. */
2984 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2985 *active &= ~(1 << ccb->ccb_slot);
2987 KKASSERT(ap->ap_active_cnt > 0);
2988 --ap->ap_active_cnt;
2990 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
2991 ccb->ccb_done(ccb); /* This won't issue pending commands or run the
2992 atascsi completion. */
2994 /* Reset port to abort running command. */
2995 if (ccb_was_started) {
2996 DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
2997 "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
2998 : "", ccb->ccb_slot, *active);
3000 if (ccb->ccb_xa.at && ap->ap_type == ATA_PORT_T_PM) {
3001 /* XXX how do we unbrick a PM target? */
3002 kprintf("%s: PM target bricked and timed-out, "
3003 "disabling PM target but trying to "
3004 "leave the port intact\n",
3005 ATANAME(ap, ccb->ccb_xa.at));
3006 ccb->ccb_xa.at->at_probe = ATA_PROBE_FAILED;
3008 ahci_port_stop(ap, 0);
3010 ahci_port_start(ap);
3012 } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) {
3014 * If the softreset failed place the port in a
3015 * failed state and use ahci_port_intr() to cancel
3016 * any remaining commands.
3018 kprintf("%s: Unable to reset during timeout, port "
3021 ap->ap_state = AP_S_FATAL_ERROR;
3029 * Restart any other commands that were aborted
3033 DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
3034 "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
3037 ahci_pwrite(ap, AHCI_PREG_SACT, *active);
3038 ahci_pwrite(ap, AHCI_PREG_CI, *active);
3043 /* Issue any pending commands now. */
3044 DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
3045 if (ccb_was_started)
3046 ahci_issue_pending_commands(ap, ncq_cmd);
3047 else if (ap->ap_active == 0)
3048 ahci_issue_pending_ncq_commands(ap);
3050 /* Complete the timed out ata_xfer I/O (may generate new I/O). */
3051 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
3054 DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
3060 ahci_empty_done(struct ahci_ccb *ccb)
3062 ccb->ccb_xa.state = ATA_S_COMPLETE;