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_HARD_RESET;
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_HARD_RESET;
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 interupts 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 * De-initialize and detach a port.
477 ahci_port_free(struct ahci_softc *sc, u_int port)
479 struct ahci_port *ap = sc->sc_ports[port];
480 struct ahci_ccb *ccb;
483 * Ensure port is disabled and its interrupts are all flushed.
486 ahci_port_stop(ap, 1);
487 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
488 ahci_pwrite(ap, AHCI_PREG_IE, 0);
489 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
490 ahci_write(sc, AHCI_REG_IS, 1 << port);
494 while ((ccb = ahci_get_ccb(ap)) != NULL) {
495 if (ccb->ccb_dmamap) {
496 bus_dmamap_destroy(sc->sc_tag_data,
498 ccb->ccb_dmamap = NULL;
501 kfree(ap->ap_ccbs, M_DEVBUF);
505 if (ap->ap_dmamem_cmd_list) {
506 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
507 ap->ap_dmamem_cmd_list = NULL;
509 if (ap->ap_dmamem_rfis) {
510 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
511 ap->ap_dmamem_rfis = NULL;
513 if (ap->ap_dmamem_cmd_table) {
514 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
515 ap->ap_dmamem_cmd_table = NULL;
518 kfree(ap->ap_ata, M_DEVBUF);
522 /* bus_space(9) says we dont free the subregions handle */
525 sc->sc_ports[port] = NULL;
529 * Start high-level command processing on the port
532 ahci_port_start(struct ahci_port *ap)
534 u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd;
537 * FRE must be turned on before ST. Wait for FR to go active
538 * before turning on ST. The spec doesn't seem to think this
539 * is necessary but waiting here avoids an on-off race in the
540 * ahci_port_stop() code.
542 olds = ahci_pread(ap, AHCI_PREG_SERR);
543 oldis= ahci_pread(ap, AHCI_PREG_IS);
544 oldtfd = ahci_pread(ap, AHCI_PREG_TFD);
545 oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
546 if ((r & AHCI_PREG_CMD_FRE) == 0) {
547 r |= AHCI_PREG_CMD_FRE;
548 ahci_pwrite(ap, AHCI_PREG_CMD, r);
550 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
551 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
552 kprintf("%s: Cannot start FIS reception\n",
559 * Turn on ST, wait for CR to come up.
561 r |= AHCI_PREG_CMD_ST;
562 ahci_pwrite(ap, AHCI_PREG_CMD, r);
563 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
564 s = ahci_pread(ap, AHCI_PREG_SERR);
565 is = ahci_pread(ap, AHCI_PREG_IS);
566 tfd = ahci_pread(ap, AHCI_PREG_TFD);
567 kprintf("%s: Cannot start command DMA\n"
570 "OLDIS=%b\nNEWIS=%b\n"
571 "OLDTFD=%b\nNEWTFD=%b\n",
573 oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR,
574 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
575 oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS,
576 oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS);
582 * (Re-)enable coalescing on the port.
584 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
585 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
586 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
587 ap->ap_sc->sc_ccc_ports_cur);
595 * Stop high-level command processing on a port
598 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
604 * Disable coalescing on the port while it is stopped.
606 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
607 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
608 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
609 ap->ap_sc->sc_ccc_ports_cur);
614 * Turn off ST, then wait for CR to go off.
616 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
617 r &= ~AHCI_PREG_CMD_ST;
618 ahci_pwrite(ap, AHCI_PREG_CMD, r);
620 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
621 kprintf("%s: Port bricked, unable to stop (ST)\n",
628 * Turn off FRE, then wait for FR to go off. FRE cannot
629 * be turned off until CR transitions to 0.
631 if ((r & AHCI_PREG_CMD_FR) == 0) {
632 kprintf("%s: FR stopped, clear FRE for next start\n",
638 r &= ~AHCI_PREG_CMD_FRE;
639 ahci_pwrite(ap, AHCI_PREG_CMD, r);
640 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
641 kprintf("%s: Port bricked, unable to stop (FRE)\n",
651 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
654 ahci_port_clo(struct ahci_port *ap)
656 struct ahci_softc *sc = ap->ap_sc;
659 /* Only attempt CLO if supported by controller */
660 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
664 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
666 if (cmd & AHCI_PREG_CMD_ST) {
667 kprintf("%s: CLO requested while port running\n",
671 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
673 /* Wait for completion */
674 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
675 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
685 * If hard is 0 perform a softreset of the port.
686 * If hard is 1 perform a hard reset of the port.
687 * If hard is 2 perform a hard reset of the port and cycle the phy.
689 * If at is non-NULL an indirect port via a port-multiplier is being
690 * reset, otherwise a direct port is being reset.
692 * NOTE: Indirect ports can only be soft-reset.
695 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
701 rc = ahci_pm_hardreset(ap, at->at_target, hard);
703 rc = ahci_port_hardreset(ap, hard);
706 rc = ahci_pm_softreset(ap, at->at_target);
708 rc = ahci_port_softreset(ap);
710 if (rc && at == NULL)
711 rc = ahci_port_hardreset(ap, hard);
718 * AHCI soft reset, Section 10.4.1
720 * (at) will be NULL when soft-resetting a directly-attached device, and
721 * non-NULL when soft-resetting a device through a port multiplier.
723 * This function keeps port communications intact and attempts to generate
724 * a reset to the connected device using device commands.
727 ahci_port_softreset(struct ahci_port *ap)
729 struct ahci_ccb *ccb = NULL;
730 struct ahci_cmd_hdr *cmd_slot;
736 count = 10; /* device reset delay x 100ms */
738 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
739 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
741 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
744 ap->ap_flags |= AP_F_IN_RESET;
745 ap->ap_state = AP_S_NORMAL;
748 * Remember port state in cmd (main to restore start/stop)
752 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
753 if (ahci_port_stop(ap, 0)) {
754 kprintf("%s: failed to stop port, cannot softreset\n",
760 * Request CLO if device appears hung.
762 if (ahci_pread(ap, AHCI_PREG_TFD) &
763 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
768 * This is an attempt to clear errors so a new signature will
769 * be latched. It isn't working properly. XXX
772 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
775 if (ahci_port_start(ap)) {
776 kprintf("%s: failed to start port, cannot softreset\n",
781 /* Check whether CLO worked */
782 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
783 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
784 kprintf("%s: CLO %s, need port reset\n",
786 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
787 ? "failed" : "unsupported");
793 * Prep first D2H command with SRST feature & clear busy/reset flags
795 * It is unclear which other fields in the FIS are used. Just zero
798 ccb = ahci_get_err_ccb(ap);
799 ccb->ccb_xa.at = NULL;
800 cmd_slot = ccb->ccb_cmd_hdr;
802 fis = ccb->ccb_cmd_table->cfis;
803 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
804 fis[0] = ATA_FIS_TYPE_H2D;
805 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
808 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
809 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
810 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
812 ccb->ccb_xa.state = ATA_S_PENDING;
813 ccb->ccb_xa.flags = 0;
814 if (ahci_poll(ccb, hz, NULL) != 0 ||
815 ccb->ccb_xa.state != ATA_S_COMPLETE) {
816 kprintf("%s: First FIS failed\n", PORTNAME(ap));
821 * The device may muff the PHY up.
823 DELAY(10000); /* XXX 3000 */
826 * Prep second D2H command to read status and complete reset sequence
827 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
828 * Rev 2.6 and it is unclear how the second FIS should be set up
829 * from the AHCI document.
831 * Give the device 3ms before sending the second FIS.
833 * It is unclear which other fields in the FIS are used. Just zero
836 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
837 fis[0] = ATA_FIS_TYPE_H2D;
838 fis[15] = ATA_FIS_CONTROL_4BIT;
841 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
843 ccb->ccb_xa.state = ATA_S_PENDING;
844 ccb->ccb_xa.flags = 0;
845 if (ahci_poll(ccb, hz, NULL) != 0 ||
846 ccb->ccb_xa.state != ATA_S_COMPLETE) {
847 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
851 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
852 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
853 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
855 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
862 * If the softreset is trying to clear a BSY condition after a
863 * normal portreset we assign the port type.
865 * If the softreset is being run first as part of the ccb error
866 * processing code then report if the device signature changed
869 if (ap->ap_type == ATA_PORT_T_NONE) {
870 ap->ap_type = ahci_port_signature_detect(ap, NULL);
872 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
873 kprintf("%s: device signature unexpectedly "
874 "changed\n", PORTNAME(ap));
875 rc = EBUSY; /* XXX */
884 * Abort our command, if it failed, by stopping command DMA.
886 if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
887 kprintf("%s: stopping the port, softreset slot "
888 "%d was still active.\n",
891 ahci_port_stop(ap, 0);
893 ccb->ccb_xa.state = ATA_S_ERROR;
895 ahci_put_err_ccb(ccb);
898 * If the target is busy use CLO to clear the busy
899 * condition. The BSY should be cleared on the next
902 if (ahci_pread(ap, AHCI_PREG_TFD) &
903 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
909 * If we failed to softreset make the port quiescent, otherwise
910 * make sure the port's start/stop state matches what it was on
913 * Don't kill the port if the softreset is on a port multiplier
914 * target, that would kill all the targets!
917 ahci_port_hardstop(ap);
918 } else if (cmd & AHCI_PREG_CMD_ST) {
919 kprintf("%s: STARTING PORT\n", PORTNAME(ap));
922 kprintf("%s: STOPPING PORT\n", PORTNAME(ap));
923 ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE));
926 ap->ap_probe = ATA_PROBE_FAILED;
928 ap->ap_probe = ATA_PROBE_NEED_IDENT;
932 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
933 ap->ap_flags &= ~AP_F_IN_RESET;
939 * AHCI port reset, Section 10.4.2
941 * This function does a hard reset of the port. Note that the device
942 * connected to the port could still end-up hung.
945 ahci_port_hardreset(struct ahci_port *ap, int hard)
952 DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
954 ap->ap_flags |= AP_F_IN_RESET;
959 ahci_port_stop(ap, 0);
960 ap->ap_state = AP_S_NORMAL;
961 ap->ap_probe = ATA_PROBE_FAILED;
965 * The port may have been quiescent with its SUD bit cleared, so
966 * set the SUD (spin up device).
968 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
969 cmd |= AHCI_PREG_CMD_SUD;
970 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
973 * Perform device detection. Cycle the PHY off, wait 10ms.
974 * This simulates the SATA cable being physically unplugged.
976 ap->ap_type = ATA_PORT_T_NONE;
978 r = AHCI_PREG_SCTL_IPM_DISABLED;
980 r |= AHCI_PREG_SCTL_DET_DISABLE;
981 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
985 * Start transmitting COMRESET. COMRESET must be sent for at
988 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
989 if (AhciForceGen1 & (1 << ap->ap_num)) {
990 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
991 r |= AHCI_PREG_SCTL_SPD_GEN1;
993 r |= AHCI_PREG_SCTL_SPD_ANY;
995 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
999 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1000 * since we are hard-resetting the port we might as well clear
1001 * the whole enchillada
1004 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1005 r &= ~AHCI_PREG_SCTL_DET_INIT;
1006 r |= AHCI_PREG_SCTL_DET_NONE;
1007 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1010 * Try to determine if there is a device on the port.
1012 * Give the device 3/10 second to at least be detected.
1013 * If we fail clear PRCS (phy detect) since we may cycled
1014 * the phy and probably caused another PRCS interrupt.
1016 for (loop = 30; loop; --loop) {
1017 r = ahci_pread(ap, AHCI_PREG_SSTS);
1018 if (r & AHCI_PREG_SSTS_DET)
1023 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1024 kprintf("%s: Port appears to be unplugged\n",
1030 * There is something on the port. Give the device 3 seconds
1031 * to fully negotiate.
1034 ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1035 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1036 kprintf("%s: Device may be powered down\n",
1042 * Wait for the device to become ready.
1044 * This can take more then a second, give it 3 seconds. If we
1045 * succeed give the device another 3ms after that.
1047 * NOTE: Port Multipliers can do two things here. First they can
1048 * return device-ready if a device is on target 0 and also
1049 * return the signature for that device. If there is no
1050 * device on target 0 then BSY/DRQ is never cleared and
1051 * it never comes ready.
1054 ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
1055 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1057 * The device is bricked or its a port multiplier and will
1058 * not unbusy until we do the pmprobe CLO softreset sequence.
1060 rc = ahci_port_pmprobe(ap);
1062 kprintf("%s: Device will not come ready 0x%b\n",
1064 ahci_pread(ap, AHCI_PREG_TFD),
1067 ap->ap_type = ATA_PORT_T_PM;
1068 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1069 kprintf("%s: Port Multiplier detected\n",
1072 } else if (rc == 0) {
1074 * We generally will not get a port multiplier signature in
1075 * this case even if this is a port multiplier, because of
1076 * Intel's stupidity. We almost certainly got target 0
1077 * behind the PM, if there is a PM.
1079 * Save the signature and probe for a PM. If we do not
1080 * find a PM then use the saved signature and return
1083 type = ahci_port_signature_detect(ap, NULL);
1084 rc = ahci_port_pmprobe(ap);
1087 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1090 ap->ap_type = ATA_PORT_T_PM;
1091 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1092 kprintf("%s: Port Multiplier detected\n",
1098 * hard-stop the port if we failed. This will set ap_probe
1102 ahci_port_hardstop(ap);
1103 ap->ap_flags &= ~AP_F_IN_RESET;
1108 * AHCI port multiplier probe. This routine is run by the hardreset code
1109 * if it gets past the device detect, whether or not BSY is found to be
1112 * We MUST use CLO to properly probe whether the port multiplier exists
1115 * Return 0 on success, non-zero on failure.
1118 ahci_port_pmprobe(struct ahci_port *ap)
1120 struct ahci_cmd_hdr *cmd_slot;
1121 struct ahci_ccb *ccb = NULL;
1122 u_int8_t *fis = NULL;
1128 * If we don't support port multipliers don't try to detect one.
1130 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0)
1135 kprintf("%s: START PMPROBE\n", PORTNAME(ap));
1139 * This code is only called from hardreset, which does not
1140 * high level command processing. The port should be stopped.
1142 * Set PMA mode while the port is stopped.
1144 * NOTE: On retry the port might be running, stopped, or failed.
1146 ahci_port_stop(ap, 0);
1147 ap->ap_state = AP_S_NORMAL;
1148 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1149 cmd |= AHCI_PREG_CMD_PMA;
1150 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1153 * Flush any errors and request CLO unconditionally, then start
1158 if (ahci_port_start(ap)) {
1159 kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
1165 * Check whether CLO worked
1167 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1168 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1169 kprintf("%s: PMPROBE CLO %s, need port reset\n",
1171 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1172 ? "failed" : "unsupported");
1178 * Prep the first H2D command with SRST feature & clear busy/reset
1181 ccb = ahci_get_err_ccb(ap);
1182 cmd_slot = ccb->ccb_cmd_hdr;
1184 fis = ccb->ccb_cmd_table->cfis;
1185 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1186 fis[0] = ATA_FIS_TYPE_H2D;
1187 fis[1] = 0x0F; /* Target 15 */
1188 fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT;
1190 cmd_slot->prdtl = 0;
1191 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1192 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1193 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1194 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1196 ccb->ccb_xa.state = ATA_S_PENDING;
1197 ccb->ccb_xa.flags = 0;
1199 if (ahci_poll(ccb, hz, NULL) != 0 ||
1200 ccb->ccb_xa.state != ATA_S_COMPLETE) {
1201 kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap));
1204 ahci_put_err_ccb(ccb);
1209 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1210 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1211 kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
1215 * The device may have muffed up the PHY when it reset.
1219 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1220 /* ahci_pm_phy_status(ap, 15, &cmd); */
1223 * Prep second D2H command to read status and complete reset sequence
1224 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1225 * Rev 2.6 and it is unclear how the second FIS should be set up
1226 * from the AHCI document.
1228 * Give the device 3ms before sending the second FIS.
1230 * It is unclear which other fields in the FIS are used. Just zero
1233 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1234 fis[0] = ATA_FIS_TYPE_H2D;
1236 fis[15] = ATA_FIS_CONTROL_4BIT;
1238 cmd_slot->prdtl = 0;
1239 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1240 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
1242 ccb->ccb_xa.state = ATA_S_PENDING;
1243 ccb->ccb_xa.flags = 0;
1245 if (ahci_poll(ccb, hz, NULL) != 0 ||
1246 ccb->ccb_xa.state != ATA_S_COMPLETE) {
1247 kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap));
1250 ahci_put_err_ccb(ccb);
1257 * What? We succeeded? Yup, but for some reason the signature
1258 * is still latched from the original detect (that saw target 0
1259 * behind the PM), and I don't know how to clear the condition
1260 * other then by retrying the whole reset sequence.
1264 ahci_put_err_ccb(ccb);
1269 * Get the signature. The caller sets the ap fields.
1271 if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
1272 ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
1279 * Fall through / clean up the CCB and perform error processing.
1284 * Abort our command, if it failed, by stopping command DMA.
1287 kprintf("rc=%d active=%08x sactive=%08x slot=%d\n",
1288 rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot);
1290 if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
1291 kprintf("%s: PMP stopping the port, softreset slot "
1292 "%d was still active.\n",
1295 ahci_port_stop(ap, 0);
1297 ccb->ccb_xa.state = ATA_S_ERROR;
1299 ahci_put_err_ccb(ccb);
1303 * If we failed turn off PMA, otherwise identify the port multiplier.
1304 * CAM will iterate the devices.
1307 ahci_port_stop(ap, 0);
1308 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1309 cmd &= ~AHCI_PREG_CMD_PMA;
1310 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1312 ahci_pm_identify(ap);
1314 ahci_pm_hardreset(ap, 0);
1315 ahci_pm_hardreset(ap, 1);
1316 ahci_pm_hardreset(ap, 2);
1317 ahci_pm_hardreset(ap, 3);
1318 ahci_pm_hardreset(ap, 4);
1321 ahci_port_stop(ap, 0);
1324 kprintf("%s: END PMPROBE\n", PORTNAME(ap));
1332 * Hard-stop on hot-swap device removal. See 10.10.1
1334 * Place the port in a mode that will allow it to detect hot-swap insertions.
1335 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1336 * seem to do the job.
1339 ahci_port_hardstop(struct ahci_port *ap)
1341 struct ata_port *at;
1347 * Stop the port. We can't modify things like SUD if the port
1350 ap->ap_state = AP_S_FATAL_ERROR;
1351 ap->ap_probe = ATA_PROBE_FAILED;
1352 ap->ap_type = ATA_PORT_T_NONE;
1353 ahci_port_stop(ap, 0);
1354 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1357 * Clean up AT sub-ports on SATA port.
1359 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1360 at = &ap->ap_ata[i];
1361 at->at_type = ATA_PORT_T_NONE;
1362 at->at_probe = ATA_PORT_T_NONE;
1366 * Turn off port-multiplier control bit
1368 if (cmd & AHCI_PREG_CMD_PMA) {
1369 cmd &= ~AHCI_PREG_CMD_PMA;
1370 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1374 * Make sure FRE is active. There isn't anything we can do if it
1375 * fails so just ignore errors.
1377 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1378 cmd |= AHCI_PREG_CMD_FRE;
1379 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1380 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1381 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1385 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1386 * 10.10.1 place us in the Listen state.
1388 * Deactivating SUD only applies if the controller supports SUD.
1390 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
1392 if (cmd & AHCI_PREG_CMD_SUD) {
1393 cmd &= ~AHCI_PREG_CMD_SUD;
1394 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1399 * Transition su to the spin-up state. HVA shall send COMRESET and
1400 * begin initialization sequence (whatever that means).
1402 * This only applies if the controller supports SUD.
1404 cmd |= AHCI_PREG_CMD_SUD;
1405 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1409 * Transition us to the Reset state. Theoretically we send a
1410 * continuous stream of COMRESETs in this state.
1412 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1413 if (AhciForceGen1 & (1 << ap->ap_num)) {
1414 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1415 r |= AHCI_PREG_SCTL_SPD_GEN1;
1417 r |= AHCI_PREG_SCTL_SPD_ANY;
1419 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1423 * Flush SERR_DIAG_X so the TFD can update.
1428 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1429 * hopefully send us a DIAG_X-related interrupt if it receives
1430 * a COMINIT, and if not that then at least a Phy transition
1433 * If we transition INIT from 1->0 to begin the initalization
1434 * sequence it is unclear if that sequence will remain active
1435 * until the next device insertion.
1437 * If we go back to the listen state it is unclear if the
1438 * device will actually send us a COMINIT, since we aren't
1439 * sending any COMRESET's
1445 * Multiple events may have built up in the TFD. The spec is not very
1446 * clear on this but it does seem to serialize events so clearing DIAG_X
1447 * just once might not do the job during a reset sequence.
1450 ahci_flush_tfd(struct ahci_port *ap)
1454 r = ahci_pread(ap, AHCI_PREG_SERR);
1455 while (r & AHCI_PREG_SERR_DIAG_X) {
1456 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1458 r = ahci_pread(ap, AHCI_PREG_SERR);
1463 * Figure out what type of device is connected to the port, ATAPI or
1467 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1471 sig = ahci_pread(ap, AHCI_PREG_SIG);
1472 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1473 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1474 return(ATA_PORT_T_ATAPI);
1475 } else if ((sig & 0xffff0000) ==
1476 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1477 kprintf("found PM\n");
1478 return(ATA_PORT_T_PM);
1480 return(ATA_PORT_T_DISK);
1485 * Load the DMA descriptor table for a CCB's buffer.
1488 ahci_load_prdt(struct ahci_ccb *ccb)
1490 struct ahci_port *ap = ccb->ccb_port;
1491 struct ahci_softc *sc = ap->ap_sc;
1492 struct ata_xfer *xa = &ccb->ccb_xa;
1493 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1494 bus_dmamap_t dmap = ccb->ccb_dmamap;
1495 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1498 if (xa->datalen == 0) {
1499 ccb->ccb_cmd_hdr->prdtl = 0;
1503 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1504 xa->data, xa->datalen,
1505 ahci_load_prdt_callback,
1507 ((xa->flags & ATA_F_NOWAIT) ?
1508 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1510 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1513 if (xa->flags & ATA_F_PIO)
1514 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1516 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1518 bus_dmamap_sync(sc->sc_tag_data, dmap,
1519 (xa->flags & ATA_F_READ) ?
1520 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1526 bus_dmamap_unload(sc->sc_tag_data, dmap);
1532 * Callback from BUSDMA system to load the segment list. The passed segment
1533 * list is a temporary structure.
1537 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1540 struct ahci_prdt *prd = *(void **)info;
1543 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1546 addr = segs->ds_addr;
1547 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1548 prd->dba_lo = htole32((u_int32_t)addr);
1550 KKASSERT((addr & 1) == 0);
1551 KKASSERT((segs->ds_len & 1) == 0);
1553 prd->flags = htole32(segs->ds_len - 1);
1559 *(void **)info = prd; /* return last valid segment */
1563 ahci_unload_prdt(struct ahci_ccb *ccb)
1565 struct ahci_port *ap = ccb->ccb_port;
1566 struct ahci_softc *sc = ap->ap_sc;
1567 struct ata_xfer *xa = &ccb->ccb_xa;
1568 bus_dmamap_t dmap = ccb->ccb_dmamap;
1570 if (xa->datalen != 0) {
1571 bus_dmamap_sync(sc->sc_tag_data, dmap,
1572 (xa->flags & ATA_F_READ) ?
1573 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1575 bus_dmamap_unload(sc->sc_tag_data, dmap);
1577 if (ccb->ccb_xa.flags & ATA_F_NCQ)
1580 xa->resid = xa->datalen -
1581 le32toh(ccb->ccb_cmd_hdr->prdbc);
1586 * Start a command and poll for completion.
1588 * NOTE: If the caller specifies a NULL timeout function the caller is
1589 * responsible for clearing hardware state on failure, but we will
1590 * deal with removing the ccb from any pending queue.
1592 * NOTE: NCQ should never be used with this function.
1594 * NOTE: If the port is in a failed state and stopped we do not try
1595 * to activate the ccb.
1598 ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *))
1600 struct ahci_port *ap = ccb->ccb_port;
1601 u_int32_t slot_mask = 1 << ccb->ccb_slot;
1603 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1604 ccb->ccb_xa.state = ATA_S_ERROR;
1611 if (ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS) & slot_mask) {
1615 if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
1616 ccb->ccb_xa.state != ATA_S_PENDING) {
1619 DELAY(1000000 / hz);
1620 } while (--timeout > 0);
1622 if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
1623 ccb->ccb_xa.state != ATA_S_PENDING) {
1624 kprintf("%s: Warning poll completed unexpectedly for slot %d\n",
1625 PORTNAME(ap), ccb->ccb_slot);
1630 kprintf("%s: Poll timed-out for slot %d state %d\n",
1631 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, ccb->ccb_xa.state);
1633 if (timeout_fn != NULL) {
1636 if (ccb->ccb_xa.state == ATA_S_PENDING)
1637 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1638 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1646 ahci_start(struct ahci_ccb *ccb)
1648 struct ahci_port *ap = ccb->ccb_port;
1649 struct ahci_softc *sc = ap->ap_sc;
1651 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1653 /* Zero transferred byte count before transfer */
1654 ccb->ccb_cmd_hdr->prdbc = 0;
1656 /* Sync command list entry and corresponding command table entry */
1657 bus_dmamap_sync(sc->sc_tag_cmdh,
1658 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1659 BUS_DMASYNC_PREWRITE);
1660 bus_dmamap_sync(sc->sc_tag_cmdt,
1661 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1662 BUS_DMASYNC_PREWRITE);
1664 /* Prepare RFIS area for write by controller */
1665 bus_dmamap_sync(sc->sc_tag_rfis,
1666 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1667 BUS_DMASYNC_PREREAD);
1669 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1671 * Issue NCQ commands only when there are no outstanding
1672 * standard commands.
1674 if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) {
1675 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1677 KKASSERT(ap->ap_active_cnt == 0);
1678 ap->ap_sactive |= (1 << ccb->ccb_slot);
1679 ccb->ccb_xa.state = ATA_S_ONCHIP;
1680 ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
1681 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1685 * Wait for all NCQ commands to finish before issuing standard
1686 * command. Allow up to <limit> non-NCQ commands to be active.
1688 * XXX If ap is a port multiplier only allow 1. At least the
1689 * NVidia-MCP77 part seems to barf if more then one
1690 * command is activated, even though it isn't NCQ.
1692 * If I set up more then one I get phy errors and the
1695 int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2;
1696 if (ap->ap_sactive || ap->ap_active_cnt >= limit) {
1697 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1699 ap->ap_active |= 1 << ccb->ccb_slot;
1700 ccb->ccb_xa.state = ATA_S_ONCHIP;
1701 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1702 ap->ap_active_cnt++;
1708 ahci_issue_pending_ncq_commands(struct ahci_port *ap)
1710 struct ahci_ccb *nextccb;
1711 u_int32_t sact_change = 0;
1713 KKASSERT(ap->ap_active_cnt == 0);
1715 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1716 if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
1719 /* Start all the NCQ commands at the head of the pending list. */
1721 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1722 sact_change |= 1 << nextccb->ccb_slot;
1723 nextccb->ccb_xa.state = ATA_S_ONCHIP;
1724 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1725 } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
1727 ap->ap_sactive |= sact_change;
1728 ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
1729 ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
1735 ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
1737 struct ahci_ccb *nextccb;
1739 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1740 if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) {
1741 KKASSERT(last_was_ncq == 0); /* otherwise it should have
1742 * been started already. */
1745 * Issue NCQ commands only when there are no outstanding
1746 * standard commands.
1748 if (ap->ap_active == 0)
1749 ahci_issue_pending_ncq_commands(ap);
1751 KKASSERT(ap->ap_active_cnt > 0);
1752 } else if (nextccb) {
1753 if (ap->ap_sactive || last_was_ncq)
1754 KKASSERT(ap->ap_active_cnt == 0);
1757 * Wait for all NCQ commands to finish before issuing standard
1758 * command. Then keep up to 2 standard commands on-chip at
1764 while (ap->ap_active_cnt < 2 &&
1765 nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
1766 TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1767 ap->ap_active |= 1 << nextccb->ccb_slot;
1768 nextccb->ccb_xa.state = ATA_S_ONCHIP;
1769 ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
1770 ap->ap_active_cnt++;
1771 nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1777 ahci_intr(void *arg)
1779 struct ahci_softc *sc = arg;
1780 u_int32_t is, ack = 0;
1783 /* Read global interrupt status */
1784 is = ahci_read(sc, AHCI_REG_IS);
1785 if (is == 0 || is == 0xffffffff)
1789 #ifdef AHCI_COALESCE
1790 /* Check coalescing interrupt first */
1791 if (is & sc->sc_ccc_mask) {
1792 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
1794 is &= ~sc->sc_ccc_mask;
1795 is |= sc->sc_ccc_ports_cur;
1799 /* Process interrupts for each port */
1802 if (sc->sc_ports[port]) {
1803 ahci_port_intr(sc->sc_ports[port],
1804 AHCI_PREG_CI_ALL_SLOTS);
1809 /* Finally, acknowledge global interrupt */
1810 ahci_write(sc, AHCI_REG_IS, ack);
1814 ahci_port_intr(struct ahci_port *ap, u_int32_t ci_mask)
1816 struct ahci_softc *sc = ap->ap_sc;
1817 u_int32_t is, ci_saved, ci_masked, processed = 0;
1819 struct ahci_ccb *ccb = NULL;
1820 struct ata_port *ccb_at = NULL;
1821 volatile u_int32_t *active;
1825 enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
1826 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
1828 is = ahci_pread(ap, AHCI_PREG_IS);
1831 kprintf("%s: INTERRUPT %b\n", PORTNAME(ap),
1835 /* Ack port interrupt only if checking all command slots. */
1836 if (ci_mask == AHCI_PREG_CI_ALL_SLOTS)
1837 ahci_pwrite(ap, AHCI_PREG_IS, is);
1840 DPRINTF(AHCI_D_INTR, "%s: interrupt: %b\n", PORTNAME(ap),
1843 if (ap->ap_sactive) {
1844 /* Active NCQ commands - use SActive instead of CI */
1845 KKASSERT(ap->ap_active == 0);
1846 KKASSERT(ap->ap_active_cnt == 0);
1847 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
1848 active = &ap->ap_sactive;
1851 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
1852 active = &ap->ap_active;
1855 if (is & AHCI_PREG_IS_TFES) {
1857 * Command failed. See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
1859 * This stops command processing.
1861 u_int32_t tfd, serr;
1864 tfd = ahci_pread(ap, AHCI_PREG_TFD);
1865 serr = ahci_pread(ap, AHCI_PREG_SERR);
1868 * If no NCQ commands are active the error slot is easily
1869 * determined, otherwise we have to extract the error
1870 * from the log page.
1872 if (ap->ap_sactive == 0) {
1873 err_slot = AHCI_PREG_CMD_CCS(
1874 ahci_pread(ap, AHCI_PREG_CMD));
1875 ccb = &ap->ap_ccbs[err_slot];
1876 ccb_at = ccb->ccb_xa.at; /* can be NULL */
1878 /* Preserve received taskfile data from the RFIS. */
1879 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
1880 sizeof(struct ata_fis_d2h));
1885 DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n",
1886 PORTNAME(ap), err_slot,
1887 tfd, AHCI_PFMT_TFD_STS,
1888 serr, AHCI_PFMT_SERR);
1890 /* Stopping the port clears CI and SACT */
1891 ahci_port_stop(ap, 0);
1892 need = NEED_RESTART;
1895 * Clear SERR (primarily DIAG_X) to enable capturing of the
1898 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
1900 /* Acknowledge the interrupts we can recover from. */
1901 ahci_pwrite(ap, AHCI_PREG_IS,
1902 AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS);
1903 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS);
1905 /* If device hasn't cleared its busy status, try to idle it. */
1906 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1907 kprintf("%s: Attempting to idle device ccb=%p\n",
1908 PORTNAME(ap), ccb_at);
1909 if (ap->ap_flags & AP_F_IN_RESET)
1912 * XXX how do we unbrick a PM target (ccb_at != NULL).
1914 * For now fail the target and use CLO to clear the
1915 * busy condition and make the ahci port usable for
1916 * the remaining devices.
1919 ccb_at->at_probe = ATA_PROBE_FAILED;
1921 } else if (ahci_port_reset(ap, ccb_at, 0)) {
1922 kprintf("%s: Unable to idle device, port "
1928 /* Had to reset device, can't gather extended info. */
1929 } else if (ap->ap_sactive) {
1931 * Recover the NCQ error from log page 10h.
1933 * XXX NCQ currently not supported with port
1936 ahci_port_read_ncq_error(ap, &err_slot);
1937 kprintf("recover from NCQ error err_slot %d\n", err_slot);
1941 DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
1942 PORTNAME(ap), err_slot);
1944 ccb = &ap->ap_ccbs[err_slot];
1946 /* Didn't reset, could gather extended info from log. */
1947 kprintf("%s: didn't reset err_slot %d "
1948 "sact=%08x act=%08x\n",
1950 err_slot, ap->ap_sactive, ap->ap_active);
1954 * If we couldn't determine the errored slot, reset the port
1955 * and fail all the active slots.
1957 if (err_slot == -1) {
1958 if (ap->ap_flags & AP_F_IN_RESET)
1961 * XXX how do we unbrick a PM target (ccb_at != NULL).
1963 * For now fail the target and use CLO to clear the
1964 * busy condition and make the ahci port usable for
1965 * the remaining devices.
1968 ccb_at->at_probe = ATA_PROBE_FAILED;
1970 } else if (ahci_port_reset(ap, ccb_at, 0)) {
1971 kprintf("%s: Unable to idle device after "
1972 "NCQ error, port bricked on us\n",
1976 kprintf("%s: couldn't recover NCQ error, failing "
1977 "all outstanding commands.\n",
1982 /* Clear the failed command in saved CI so completion runs. */
1983 ci_saved &= ~(1 << err_slot);
1985 /* Note the error in the ata_xfer. */
1986 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
1987 ccb->ccb_xa.state = ATA_S_ERROR;
1990 /* There may only be one outstanding standard command now. */
1991 if (ap->ap_sactive == 0) {
1994 slot = ffs(tmp) - 1;
1995 tmp &= ~(1 << slot);
2000 } else if (is & AHCI_PREG_IS_DHRS) {
2002 * Command posted D2H register FIS to the rfis. This
2003 * does NOT stop command processing and it is unclear
2004 * how we are supposed to deal with it other then using
2005 * only a queue of 1.
2007 * We must copy the port rfis to the ccb and restart
2008 * command processing. ahci_pm_read() does not function
2009 * without this support.
2013 if (ap->ap_sactive == 0) {
2014 err_slot = AHCI_PREG_CMD_CCS(
2015 ahci_pread(ap, AHCI_PREG_CMD));
2016 ccb = &ap->ap_ccbs[err_slot];
2017 ccb_at = ccb->ccb_xa.at; /* can be NULL */
2019 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2020 sizeof(struct ata_fis_d2h));
2022 kprintf("%s: Unexpected DHRS posted while "
2023 "NCQ running\n", PORTNAME(ap));
2029 * Device notification to us.
2031 * For some reason this interrupt can occur without any notification
2032 * bits actually being set.
2034 if ((is & AHCI_PREG_IS_SDBS) && (sc->sc_cap & AHCI_REG_CAP_SSNTF)) {
2037 data = ahci_pread(ap, AHCI_PREG_SNTF);
2039 kprintf("%s: NOTIFY %08x\n", PORTNAME(ap), data);
2040 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2041 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2045 /* XXX future IFS recovery code? or just scrap it */
2046 if (is & AHCI_PREG_IS_IFS) {
2047 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2048 kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
2051 serr, AHCI_PFMT_SERR);
2052 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2053 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2054 is &= ~AHCI_PREG_IS_IFS;
2055 ahci_port_stop(ap, 0);
2056 ahci_port_start(ap);
2057 need = NEED_RESTART;
2062 * Port change (hot-plug).
2064 * A PCS interrupt will occur on hot-plug once communication is
2067 * A PRCS interrupt will occur on hot-unplug (and possibly also
2070 * XXX We can then check the CPS (Cold Presence State) bit, if
2071 * supported, to determine if a device is plugged in or not and do
2074 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
2075 * can also occur if an unsolicited COMINIT is received.
2076 * If this occurs command processing is automatically
2077 * stopped (CR goes inactive) and the port must be stopped
2080 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2081 ahci_pwrite(ap, AHCI_PREG_SERR,
2082 (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
2083 ahci_port_stop(ap, 0);
2084 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2085 case AHCI_PREG_SSTS_DET_DEV:
2086 if (ap->ap_type == ATA_PORT_T_NONE) {
2087 need = NEED_HOTPLUG_INSERT;
2090 need = NEED_RESTART;
2093 if (ap->ap_type != ATA_PORT_T_NONE) {
2094 need = NEED_HOTPLUG_REMOVE;
2097 need = NEED_RESTART;
2103 * Check for remaining errors - they are fatal.
2105 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2106 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2107 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2108 kprintf("%s: unrecoverable errors (IS: %b, SERR: %b), "
2109 "disabling port.\n",
2112 serr, AHCI_PFMT_SERR
2114 /* XXX try recovery first */
2119 * Fail all outstanding commands if we know the port won't recover.
2121 * We may have a ccb_at if the failed command is known and was
2122 * being sent to a device over a port multiplier (PM). In this
2123 * case if the port itself has not completely failed we fail just
2124 * the commands related to that target.
2126 if (ap->ap_state == AP_S_FATAL_ERROR) {
2128 ap->ap_state = AP_S_FATAL_ERROR;
2131 /* Stopping the port clears CI/SACT */
2132 ahci_port_stop(ap, 0);
2135 * Error all the active slots. If running across a PM
2136 * try to error out just the slots related to the target.
2138 ci_masked = ci_saved & *active;
2140 slot = ffs(ci_masked) - 1;
2141 ccb = &ap->ap_ccbs[slot];
2142 if (ccb_at == ccb->ccb_xa.at ||
2143 ap->ap_state == AP_S_FATAL_ERROR) {
2144 ci_masked &= ~(1 << slot);
2145 ccb->ccb_xa.state = ATA_S_ERROR;
2149 /* Run completion for all active slots. */
2150 ci_saved &= ~*active;
2153 * Don't restart the port if our problems were deemed fatal.
2155 * Also acknowlege all fatal interrupt sources to prevent
2158 if (ap->ap_state == AP_S_FATAL_ERROR) {
2159 if (need == NEED_RESTART)
2160 need = NEED_NOTHING;
2161 ahci_pwrite(ap, AHCI_PREG_IS,
2162 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2163 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2169 * CCB completion is detected by noticing its slot's bit in CI has
2170 * changed to zero some time after we activated it.
2171 * If we are polling, we may only be interested in particular slot(s).
2173 * Any active bits not saved are completed within the restrictions
2174 * imposed by the caller.
2176 ci_masked = ~ci_saved & *active & ci_mask;
2178 slot = ffs(ci_masked) - 1;
2179 ccb = &ap->ap_ccbs[slot];
2180 ci_masked &= ~(1 << slot);
2182 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2183 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2186 bus_dmamap_sync(sc->sc_tag_cmdh,
2187 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2188 BUS_DMASYNC_POSTWRITE);
2190 bus_dmamap_sync(sc->sc_tag_cmdt,
2191 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2192 BUS_DMASYNC_POSTWRITE);
2194 bus_dmamap_sync(sc->sc_tag_rfis,
2195 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2196 BUS_DMASYNC_POSTREAD);
2198 *active &= ~(1 << ccb->ccb_slot);
2199 if (active == &ap->ap_active) {
2200 KKASSERT(ap->ap_active_cnt > 0);
2201 --ap->ap_active_cnt;
2205 processed |= 1 << ccb->ccb_slot;
2211 * A recoverable error occured and we can restart outstanding
2212 * commands on the port.
2214 ahci_port_start(ap);
2220 slot = ffs(tmp) - 1;
2221 tmp &= ~(1 << slot);
2222 ccb = &ap->ap_ccbs[slot];
2223 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2224 KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
2225 (!!ap->ap_sactive));
2228 DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
2229 "re-enabling%s slots %08x\n", PORTNAME(ap),
2230 ap->ap_sactive ? " NCQ" : "", ci_saved);
2233 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
2234 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
2237 case NEED_HOTPLUG_INSERT:
2239 * A hot-plug insertion event has occured and all
2240 * outstanding commands have already been revoked.
2242 * Don't recurse if this occurs while we are
2243 * resetting the port.
2245 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2246 kprintf("%s: HOTPLUG - Device inserted\n",
2248 if (ahci_port_init(ap, NULL) == 0)
2249 ahci_cam_changed(ap, 1);
2252 case NEED_HOTPLUG_REMOVE:
2254 * A hot-plug removal event has occured and all
2255 * outstanding commands have already been revoked.
2257 * Don't recurse if this occurs while we are
2258 * resetting the port.
2260 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2261 kprintf("%s: HOTPLUG - Device removed\n",
2263 ahci_port_hardstop(ap);
2264 ahci_cam_changed(ap, 0);
2274 ahci_get_ccb(struct ahci_port *ap)
2276 struct ahci_ccb *ccb;
2278 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2279 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2281 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2282 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2283 ccb->ccb_xa.state = ATA_S_SETUP;
2284 ccb->ccb_xa.at = NULL;
2286 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2292 ahci_put_ccb(struct ahci_ccb *ccb)
2294 struct ahci_port *ap = ccb->ccb_port;
2297 if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2298 ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2299 ccb->ccb_xa.state != ATA_S_ERROR) {
2300 kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
2302 PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2307 ccb->ccb_xa.state = ATA_S_PUT;
2308 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2309 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2310 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2314 ahci_get_err_ccb(struct ahci_port *ap)
2316 struct ahci_ccb *err_ccb;
2319 /* No commands may be active on the chip. */
2320 sact = ahci_pread(ap, AHCI_PREG_SACT);
2322 kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
2323 KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
2326 KKASSERT(ap->ap_err_busy == 0);
2327 ap->ap_err_busy = 1;
2329 /* Save outstanding command state. */
2330 ap->ap_err_saved_active = ap->ap_active;
2331 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2332 ap->ap_err_saved_sactive = ap->ap_sactive;
2335 * Pretend we have no commands outstanding, so that completions won't
2338 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2341 * Grab a CCB to use for error recovery. This should never fail, as
2342 * we ask atascsi to reserve one for us at init time.
2344 err_ccb = ahci_get_ccb(ap);
2345 KKASSERT(err_ccb != NULL);
2346 err_ccb->ccb_xa.flags = 0;
2347 err_ccb->ccb_done = ahci_empty_done;
2353 ahci_put_err_ccb(struct ahci_ccb *ccb)
2355 struct ahci_port *ap = ccb->ccb_port;
2360 KKASSERT(ap->ap_err_busy);
2363 * No commands may be active on the chip
2365 sact = ahci_pread(ap, AHCI_PREG_SACT);
2367 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
2368 ccb->ccb_slot, sact);
2370 ci = ahci_pread(ap, AHCI_PREG_CI);
2372 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2373 "(act=%08x sact=%08x)\n",
2375 ap->ap_active, ap->ap_sactive);
2378 /* Done with the CCB */
2381 /* Restore outstanding command state */
2382 ap->ap_sactive = ap->ap_err_saved_sactive;
2383 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2384 ap->ap_active = ap->ap_err_saved_active;
2387 ap->ap_err_busy = 0;
2392 * Read log page to get NCQ error.
2394 * NOTE: NCQ not currently supported on port multipliers. XXX
2397 ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
2399 struct ahci_ccb *ccb;
2400 struct ahci_cmd_hdr *cmd_slot;
2402 struct ata_fis_h2d *fis;
2405 DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2407 /* Save command register state. */
2408 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
2410 /* Port should have been idled already. Start it. */
2411 KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
2412 ahci_port_start(ap);
2414 /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2415 ccb = ahci_get_err_ccb(ap);
2416 ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2417 ccb->ccb_xa.data = ap->ap_err_scratch;
2418 ccb->ccb_xa.datalen = 512;
2419 cmd_slot = ccb->ccb_cmd_hdr;
2420 bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
2422 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
2423 fis->type = ATA_FIS_TYPE_H2D;
2424 fis->flags = ATA_H2D_FLAGS_CMD;
2425 fis->command = ATA_C_READ_LOG_EXT;
2426 fis->lba_low = 0x10; /* queued error log page (10h) */
2427 fis->sector_count = 1; /* number of sectors (1) */
2428 fis->sector_count_exp = 0;
2429 fis->lba_mid = 0; /* starting offset */
2430 fis->lba_mid_exp = 0;
2433 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
2435 if (ahci_load_prdt(ccb) != 0) {
2436 rc = ENOMEM; /* XXX caller must abort all commands */
2440 ccb->ccb_xa.state = ATA_S_PENDING;
2441 if (ahci_poll(ccb, hz, NULL) != 0)
2446 /* Abort our command, if it failed, by stopping command DMA. */
2447 if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
2448 kprintf("%s: log page read failed, slot %d was still active.\n",
2449 PORTNAME(ap), ccb->ccb_slot);
2450 ahci_port_stop(ap, 0);
2453 /* Done with the error CCB now. */
2454 ahci_unload_prdt(ccb);
2455 ahci_put_err_ccb(ccb);
2457 /* Extract failed register set and tags from the scratch space. */
2459 struct ata_log_page_10h *log;
2462 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2463 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2464 /* Not queued bit was set - wasn't an NCQ error? */
2465 kprintf("%s: read NCQ error page, but not an NCQ "
2470 /* Copy back the log record as a D2H register FIS. */
2471 *err_slotp = err_slot = log->err_regs.type &
2472 ATA_LOG_10H_TYPE_TAG_MASK;
2474 ccb = &ap->ap_ccbs[err_slot];
2475 memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
2476 sizeof(struct ata_fis_d2h));
2477 ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
2478 ccb->ccb_xa.rfis.flags = 0;
2482 /* Restore saved CMD register state */
2483 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
2489 * Allocate memory for various structures DMAd by hardware. The maximum
2490 * number of segments for these tags is 1 so the DMA memory will have a
2491 * single physical base address.
2493 struct ahci_dmamem *
2494 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
2496 struct ahci_dmamem *adm;
2499 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2501 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2502 BUS_DMA_ZERO, &adm->adm_map);
2505 error = bus_dmamap_load(tag, adm->adm_map,
2507 bus_dma_tag_getmaxsize(tag),
2508 ahci_dmamem_saveseg, &adm->adm_busaddr,
2513 bus_dmamap_destroy(tag, adm->adm_map);
2514 adm->adm_map = NULL;
2515 adm->adm_tag = NULL;
2516 adm->adm_kva = NULL;
2518 kfree(adm, M_DEVBUF);
2526 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2528 KKASSERT(error == 0);
2529 KKASSERT(nsegs == 1);
2530 *(bus_addr_t *)info = segs->ds_addr;
2535 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
2538 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2539 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2540 adm->adm_map = NULL;
2541 adm->adm_tag = NULL;
2542 adm->adm_kva = NULL;
2544 kfree(adm, M_DEVBUF);
2548 ahci_read(struct ahci_softc *sc, bus_size_t r)
2550 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2551 BUS_SPACE_BARRIER_READ);
2552 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2556 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
2558 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2559 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2560 BUS_SPACE_BARRIER_WRITE);
2564 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
2569 for (i = 0; i < 1000; i++) {
2570 if ((ahci_read(sc, r) & mask) != target)
2579 ahci_pread(struct ahci_port *ap, bus_size_t r)
2581 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2582 BUS_SPACE_BARRIER_READ);
2583 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2587 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
2589 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2590 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2591 BUS_SPACE_BARRIER_WRITE);
2595 ahci_pwait_eq(struct ahci_port *ap, int timeout,
2596 bus_size_t r, u_int32_t mask, u_int32_t target)
2600 for (i = 0; i < timeout; i++) {
2601 if ((ahci_pread(ap, r) & mask) == target)
2610 * Acquire an ata transfer.
2612 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
2613 * targets that go through the port multiplier.
2616 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
2618 struct ahci_ccb *ccb;
2620 ccb = ahci_get_ccb(ap);
2622 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
2627 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
2628 PORTNAME(ap), ccb->ccb_slot);
2630 ccb->ccb_xa.at = at;
2631 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2633 return (&ccb->ccb_xa);
2637 ahci_ata_put_xfer(struct ata_xfer *xa)
2639 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
2641 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
2647 ahci_ata_cmd(struct ata_xfer *xa)
2649 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
2650 struct ahci_cmd_hdr *cmd_slot;
2652 KKASSERT(xa->state == ATA_S_SETUP);
2654 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2657 kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
2658 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2659 ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2662 ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
2665 ccb->ccb_done = ahci_ata_cmd_done;
2667 cmd_slot = ccb->ccb_cmd_hdr;
2668 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
2669 if (ccb->ccb_xa.at) {
2670 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
2671 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
2674 if (xa->flags & ATA_F_WRITE)
2675 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
2677 if (xa->flags & ATA_F_PACKET)
2678 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
2680 if (ahci_load_prdt(ccb) != 0)
2683 xa->state = ATA_S_PENDING;
2685 if (xa->flags & ATA_F_POLL) {
2686 ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout);
2687 return (ATA_COMPLETE);
2691 xa->flags |= ATA_F_TIMEOUT_RUNNING;
2692 callout_reset(&ccb->ccb_timeout, xa->timeout,
2693 ahci_ata_cmd_timeout_unserialized, ccb);
2696 return (ATA_QUEUED);
2700 xa->state = ATA_S_ERROR;
2707 ahci_ata_cmd_done(struct ahci_ccb *ccb)
2709 struct ata_xfer *xa = &ccb->ccb_xa;
2711 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
2712 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2713 callout_stop(&ccb->ccb_timeout);
2716 if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR)
2717 ahci_issue_pending_commands(ccb->ccb_port,
2718 xa->flags & ATA_F_NCQ);
2720 ahci_unload_prdt(ccb);
2722 if (xa->state == ATA_S_ONCHIP)
2723 xa->state = ATA_S_COMPLETE;
2725 else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
2726 kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
2728 PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
2730 if (xa->state != ATA_S_TIMEOUT)
2735 ahci_ata_cmd_timeout_unserialized(void *arg)
2737 struct ahci_ccb *ccb = arg;
2738 struct ahci_port *ap = ccb->ccb_port;
2740 lwkt_serialize_enter(&ap->ap_sc->sc_serializer);
2741 ahci_ata_cmd_timeout(arg);
2742 lwkt_serialize_exit(&ap->ap_sc->sc_serializer);
2746 ahci_ata_cmd_timeout(void *arg)
2748 struct ahci_ccb *ccb = arg;
2749 struct ata_xfer *xa = &ccb->ccb_xa;
2750 struct ahci_port *ap = ccb->ccb_port;
2751 volatile u_int32_t *active;
2752 int ccb_was_started, ncq_cmd;
2755 kprintf("%s: CMD TIMEOUT cmd-reg 0x%b\n"
2756 "\tsactive=%08x active=%08x\n"
2757 "\t sact=%08x ci=%08x\n",
2758 ATANAME(ap, ccb->ccb_xa.at),
2759 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2760 ap->ap_sactive, ap->ap_active,
2761 ahci_pread(ap, AHCI_PREG_SACT),
2762 ahci_pread(ap, AHCI_PREG_CI));
2765 * NOTE: Timeout will not be running if the command was polled.
2767 KKASSERT(xa->flags & (ATA_F_POLL|ATA_F_TIMEOUT_RUNNING));
2768 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
2769 ncq_cmd = (xa->flags & ATA_F_NCQ);
2770 active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
2772 if (ccb->ccb_xa.state == ATA_S_PENDING) {
2773 DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
2774 "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
2775 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2776 ccb_was_started = 0;
2777 } else if (ccb->ccb_xa.state == ATA_S_ONCHIP &&
2778 ahci_port_intr(ap, 1 << ccb->ccb_slot)) {
2779 DPRINTF(AHCI_D_TIMEOUT, "%s: final poll of port completed "
2780 "command in slot %d\n", PORTNAME(ap), ccb->ccb_slot);
2782 } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
2783 DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
2784 "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
2785 (*active & (1 << ccb->ccb_slot)) ?
2786 " but slot is still active?" : ".");
2788 } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
2789 (1 << ccb->ccb_slot)) == 0 &&
2790 (*active & (1 << ccb->ccb_slot))) {
2791 DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but "
2792 "IRQ handler didn't detect it. Why?\n", PORTNAME(ap),
2794 *active &= ~(1 << ccb->ccb_slot);
2796 KKASSERT(ap->ap_active_cnt > 0);
2797 --ap->ap_active_cnt;
2802 ccb_was_started = 1;
2805 /* Complete the slot with a timeout error. */
2806 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2807 *active &= ~(1 << ccb->ccb_slot);
2809 KKASSERT(ap->ap_active_cnt > 0);
2810 --ap->ap_active_cnt;
2812 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
2813 ccb->ccb_done(ccb); /* This won't issue pending commands or run the
2814 atascsi completion. */
2816 /* Reset port to abort running command. */
2817 if (ccb_was_started) {
2818 DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
2819 "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
2820 : "", ccb->ccb_slot, *active);
2822 if (ccb->ccb_xa.at) {
2823 /* XXX how do we unbrick a PM target? */
2824 kprintf("%s: Unable to reset PM target during timeout"
2825 ", port bricked on us\n",
2827 ap->ap_state = AP_S_FATAL_ERROR;
2828 ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS);
2829 } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) {
2831 * If the softreset failed place the port in a
2832 * failed state and use ahci_port_intr() to cancel
2833 * any remaining commands.
2835 kprintf("%s: Unable to reset during timeout, port "
2838 ap->ap_state = AP_S_FATAL_ERROR;
2839 ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS);
2842 * Restart any other commands that were aborted
2846 DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
2847 "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
2850 ahci_pwrite(ap, AHCI_PREG_SACT, *active);
2851 ahci_pwrite(ap, AHCI_PREG_CI, *active);
2856 /* Issue any pending commands now. */
2857 DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
2858 if (ccb_was_started)
2859 ahci_issue_pending_commands(ap, ncq_cmd);
2860 else if (ap->ap_active == 0)
2861 ahci_issue_pending_ncq_commands(ap);
2863 /* Complete the timed out ata_xfer I/O (may generate new I/O). */
2864 DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
2867 DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
2873 ahci_empty_done(struct ahci_ccb *ccb)
2875 ccb->ccb_xa.state = ATA_S_COMPLETE;