4 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
21 * This code is derived from software contributed to The DragonFly Project
22 * by Matthew Dillon <dillon@backplane.com>
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
34 * 3. Neither the name of The DragonFly Project nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific, prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
56 void ahci_port_interrupt_enable(struct ahci_port *ap);
58 int ahci_load_prdt(struct ahci_ccb *);
59 void ahci_unload_prdt(struct ahci_ccb *);
60 static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
61 int nsegs, int error);
62 void ahci_start(struct ahci_ccb *);
63 int ahci_port_softreset(struct ahci_port *ap);
64 int ahci_port_hardreset(struct ahci_port *ap, int hard);
65 void ahci_port_hardstop(struct ahci_port *ap);
67 static void ahci_ata_cmd_timeout_unserialized(void *);
68 void ahci_check_active_timeouts(struct ahci_port *ap);
70 void ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at);
71 void ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at);
72 void ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb);
73 void ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t mask);
75 int ahci_port_read_ncq_error(struct ahci_port *, int);
77 struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
78 void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
79 static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
81 static void ahci_dummy_done(struct ata_xfer *xa);
82 static void ahci_empty_done(struct ahci_ccb *ccb);
83 static void ahci_ata_cmd_done(struct ahci_ccb *ccb);
86 * Initialize the global AHCI hardware. This code does not set up any of
90 ahci_init(struct ahci_softc *sc)
92 u_int32_t cap, pi, pleft;
96 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
97 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
100 * save BIOS initialised parameters, enable staggered spin up
102 cap = ahci_read(sc, AHCI_REG_CAP);
103 cap &= AHCI_REG_CAP_SMPS;
104 cap |= AHCI_REG_CAP_SSS;
105 pi = ahci_read(sc, AHCI_REG_PI);
108 * Unconditionally reset the controller, do not conditionalize on
109 * trying to figure it if it was previously active or not.
111 * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section
112 * 5.2.2.1 regarding this. HR should be set to 1 only after
113 * AE is set to 1. The reset sequence will clear HR when
114 * it completes, and will also clear AE if SAM is 0. AE must
115 * then be set again. When SAM is 1 the AE bit typically reads
116 * as 1 (and is read-only).
118 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
119 * otherwise the writes will only be separated by a few
124 * If you have a port multiplier and it does not have a device
125 * in target 0, and it probes normally, but a later operation
126 * mis-probes a target behind that PM, it is possible for the
127 * port to brick such that only (a) a power cycle of the host
128 * or (b) placing a device in target 0 will fix the problem.
129 * Power cycling the PM has no effect (it works fine on another
130 * host port). This issue is unrelated to CLO.
133 * Wait for any prior reset sequence to complete
135 if (ahci_wait_ne(sc, AHCI_REG_GHC,
136 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
137 device_printf(sc->sc_dev, "Controller is stuck in reset\n");
140 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
142 ahci_read(sc, AHCI_REG_GHC); /* flush */
143 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
145 ahci_read(sc, AHCI_REG_GHC); /* flush */
146 if (ahci_wait_ne(sc, AHCI_REG_GHC,
147 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
148 device_printf(sc->sc_dev, "unable to reset controller\n");
151 if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
152 device_printf(sc->sc_dev, "AE did not auto-clear!\n");
153 ahci_write(sc, AHCI_REG_GHC, 0);
158 * Enable ahci (global interrupts disabled)
160 * Restore saved parameters. Avoid pci transaction burst write
161 * by issuing dummy reads.
164 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
167 ahci_read(sc, AHCI_REG_GHC); /* flush */
168 ahci_write(sc, AHCI_REG_CAP, cap);
169 ahci_write(sc, AHCI_REG_PI, pi);
170 ahci_read(sc, AHCI_REG_GHC); /* flush */
173 * Intel hocus pocus in case the BIOS has not set the chip up
174 * properly for AHCI operation.
176 if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
177 if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
178 device_printf(sc->sc_dev, "Intel hocus pocus\n");
179 pci_write_config(sc->sc_dev, 0x92,
180 pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
184 * This is a hack that currently does not appear to have
185 * a significant effect, but I noticed the port registers
186 * do not appear to be completely cleared after the host
187 * controller is reset.
189 * Use a temporary ap structure so we can call ahci_pwrite().
191 * We must be sure to stop the port
193 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
196 for (i = 0; i < AHCI_MAX_PORTS; ++i) {
199 if ((pi & (1 << i)) == 0)
201 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
202 AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
203 device_printf(sc->sc_dev, "can't map port\n");
207 * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
208 * AHCI1.1 can brick the chipset. Not only brick it,
209 * but also crash the PC. The bit seems unreliable
210 * on AHCI1.2 as well.
212 ahci_port_stop(ap, 1);
213 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
214 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
215 ahci_pwrite(ap, AHCI_PREG_IE, 0);
216 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
217 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
218 ahci_pwrite(ap, AHCI_PREG_IS, -1);
219 sc->sc_portmask |= (1 << i);
229 * Allocate and initialize an AHCI port.
232 ahci_port_alloc(struct ahci_softc *sc, u_int port)
234 struct ahci_port *ap;
236 struct ahci_ccb *ccb;
240 struct ahci_cmd_hdr *hdr;
241 struct ahci_cmd_table *table;
246 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
247 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
249 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
250 device_get_name(sc->sc_dev),
251 device_get_unit(sc->sc_dev),
253 sc->sc_ports[port] = ap;
256 * Allocate enough so we never have to reallocate, it makes
259 * ap_pmcount will be reduced by the scan if we encounter the
260 * port multiplier port prior to target 15.
262 * kmalloc power-of-2 allocations are guaranteed not to cross
263 * a page boundary. Make sure the identify sub-structure in the
264 * at structure does not cross a page boundary, just in case the
265 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
267 if (ap->ap_ata[0] == NULL) {
270 for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
272 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
273 at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
275 at->at_ahci_port = ap;
277 at->at_probe = ATA_PROBE_NEED_INIT;
278 at->at_features |= ATA_PORT_F_RESCAN;
279 ksnprintf(at->at_name, sizeof(at->at_name),
280 "%s.%d", ap->ap_name, i);
283 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
284 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
285 device_printf(sc->sc_dev,
286 "unable to create register window for port %d\n",
293 ap->ap_probe = ATA_PROBE_NEED_INIT;
294 ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE;
295 ap->sysctl_tree = NULL;
296 TAILQ_INIT(&ap->ap_ccb_free);
297 TAILQ_INIT(&ap->ap_ccb_pending);
298 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
300 /* Disable port interrupts */
301 ahci_pwrite(ap, AHCI_PREG_IE, 0);
302 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
305 * Sec 10.1.2 - deinitialise port if it is already running
307 cmd = ahci_pread(ap, AHCI_PREG_CMD);
308 kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD);
310 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
311 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
312 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
315 r = ahci_port_stop(ap, 1);
317 device_printf(sc->sc_dev,
318 "unable to disable %s, ignoring port %d\n",
319 ((r == 2) ? "CR" : "FR"), port);
324 /* Write DET to zero */
325 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
329 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
330 if (ap->ap_dmamem_rfis == NULL) {
331 kprintf("%s: NORFIS\n", PORTNAME(ap));
335 /* Setup RFIS base address */
336 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
337 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
338 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
339 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
341 /* Clear SERR before starting FIS reception or ST or anything */
343 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
345 /* Enable FIS reception and activate port. */
346 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
347 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
348 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
349 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
351 /* Check whether port activated. Skip it if not. */
352 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
353 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
354 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
359 /* Allocate a CCB for each command slot */
360 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
362 if (ap->ap_ccbs == NULL) {
363 device_printf(sc->sc_dev,
364 "unable to allocate command list for port %d\n",
369 /* Command List Structures and Command Tables */
370 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
371 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
372 if (ap->ap_dmamem_cmd_table == NULL ||
373 ap->ap_dmamem_cmd_list == NULL) {
375 device_printf(sc->sc_dev,
376 "unable to allocate DMA memory for port %d\n",
381 /* Setup command list base address */
382 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
383 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
384 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
386 /* Split CCB allocation into CCBs and assign to command header/table */
387 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
388 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
389 for (i = 0; i < sc->sc_ncmds; i++) {
390 ccb = &ap->ap_ccbs[i];
392 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
395 device_printf(sc->sc_dev,
396 "unable to create dmamap for port %d "
397 "ccb %d\n", port, i);
401 callout_init(&ccb->ccb_timeout);
404 ccb->ccb_cmd_hdr = &hdr[i];
405 ccb->ccb_cmd_table = &table[i];
406 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
407 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
408 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
409 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
412 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
413 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
416 ccb->ccb_xa.state = ATA_S_COMPLETE;
419 * CCB[1] is the error CCB and is not get or put. It is
420 * also used for probing. Numerous HBAs only load the
421 * signature from CCB[1] so it MUST be used for the second
425 ap->ap_err_ccb = ccb;
431 * Wait for ICC change to complete
433 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
436 * Calculate the interrupt mask
438 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
439 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
440 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
441 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
442 AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
443 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
444 data |= AHCI_PREG_IE_IPME;
446 if (sc->sc_ccc_ports & (1 << port)
447 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
449 ap->ap_intmask = data;
452 * Start the port helper thread. The helper thread will call
453 * ahci_port_init() so the ports can all be started in parallel.
454 * A failure by ahci_port_init() does not deallocate the port
455 * since we still want hot-plug events.
457 ahci_os_start_port(ap);
460 ahci_port_free(sc, port);
465 * [re]initialize an idle port. No CCBs should be active.
467 * This function is called during the initial port allocation sequence
468 * and is also called on hot-plug insertion. We take no chances and
469 * use a portreset instead of a softreset.
471 * This function is the only way to move a failed port back to active
474 * Returns 0 if a device is successfully detected.
477 ahci_port_init(struct ahci_port *ap)
480 * Register [re]initialization
482 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
483 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
484 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
488 * Flush the TFD and SERR and make sure the port is stopped before
489 * enabling its interrupt. We no longer cycle the port start as
490 * the port should not be started unless a device is present.
492 * XXX should we enable FIS reception? (FRE)?
495 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
496 ahci_port_stop(ap, 0);
497 ahci_port_interrupt_enable(ap);
502 * Enable or re-enable interrupts on a port.
504 * This routine is called from the port initialization code or from the
505 * helper thread as the real interrupt may be forced to turn off certain
509 ahci_port_interrupt_enable(struct ahci_port *ap)
511 ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
515 * Manage the agressive link power management capability.
518 ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
522 if (link_pwr_mgmt == ap->link_pwr_mgmt)
525 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
526 kprintf("%s: link power management not supported.\n",
531 ahci_os_lock_port(ap);
533 if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
534 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
535 kprintf("%s: enabling aggressive link power management.\n",
538 ap->link_pwr_mgmt = link_pwr_mgmt;
540 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
541 ahci_port_interrupt_enable(ap);
543 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
544 sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED);
545 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
548 * Enable device initiated link power management for
549 * directly attached devices that support it.
551 if (ap->ap_type != ATA_PORT_T_PM &&
552 ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) {
553 if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
554 kprintf("%s: Could not enable device initiated "
555 "link power management.\n",
559 cmd = ahci_pread(ap, AHCI_PREG_CMD);
560 cmd |= AHCI_PREG_CMD_ASP;
561 cmd |= AHCI_PREG_CMD_ALPE;
562 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
564 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
565 (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
566 kprintf("%s: enabling medium link power management.\n",
569 ap->link_pwr_mgmt = link_pwr_mgmt;
571 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
572 ahci_port_interrupt_enable(ap);
574 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
575 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
576 sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL;
577 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
579 cmd = ahci_pread(ap, AHCI_PREG_CMD);
580 cmd &= ~AHCI_PREG_CMD_ASP;
581 cmd |= AHCI_PREG_CMD_ALPE;
582 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
584 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
585 kprintf("%s: disabling link power management.\n",
588 /* Disable device initiated link power management */
589 if (ap->ap_type != ATA_PORT_T_PM &&
590 ap->ap_ata[0]->at_identify.satafsup & (1 << 3))
591 ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
593 cmd = ahci_pread(ap, AHCI_PREG_CMD);
594 cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
595 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
597 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
598 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
599 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
601 /* let the drive come back to avoid PRCS interrupts later */
602 ahci_os_unlock_port(ap);
604 ahci_os_lock_port(ap);
606 ahci_pwrite(ap, AHCI_PREG_SERR,
607 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
608 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
610 ap->ap_intmask |= AHCI_PREG_IE_PRCE;
611 ahci_port_interrupt_enable(ap);
613 ap->link_pwr_mgmt = link_pwr_mgmt;
615 kprintf("%s: unsupported link power management state %d.\n",
616 PORTNAME(ap), link_pwr_mgmt);
619 ahci_os_unlock_port(ap);
623 * Return current link power state.
626 ahci_port_link_pwr_state(struct ahci_port *ap)
630 r = ahci_pread(ap, AHCI_PREG_SSTS);
631 switch (r & SATA_PM_SSTS_IPM) {
632 case SATA_PM_SSTS_IPM_ACTIVE:
634 case SATA_PM_SSTS_IPM_PARTIAL:
636 case SATA_PM_SSTS_IPM_SLUMBER:
644 * Run the port / target state machine from a main context.
646 * The state machine for the port is always run.
648 * If atx is non-NULL run the state machine for a particular target.
649 * If atx is NULL run the state machine for all targets.
652 ahci_port_state_machine(struct ahci_port *ap, int initial)
661 * State machine for port. Note that CAM is not yet associated
662 * during the initial parallel probe and the port's probe state
663 * will not get past ATA_PROBE_NEED_IDENT.
666 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
667 kprintf("%s: Waiting 10 seconds on insertion\n",
669 ahci_os_sleep(10000);
672 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
674 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
675 ahci_port_reset(ap, NULL, 1);
676 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
677 ahci_port_reset(ap, NULL, 0);
678 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
679 ahci_cam_probe(ap, NULL);
681 if (ap->ap_type != ATA_PORT_T_PM) {
682 if (ap->ap_probe == ATA_PROBE_FAILED) {
683 ahci_cam_changed(ap, NULL, 0);
684 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
685 ahci_cam_changed(ap, NULL, 1);
691 * Port Multiplier state machine.
693 * Get a mask of changed targets and combine with any runnable
694 * states already present.
696 for (loop = 0; ;++loop) {
697 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
698 kprintf("%s: PM unable to read hot-plug bitmap\n",
704 * Do at least one loop, then stop if no more state changes
705 * have occured. The PM might not generate a new
706 * notification until we clear the entire bitmap.
708 if (loop && data == 0)
712 * New devices showing up in the bitmap require some spin-up
713 * time before we start probing them. Reset didsleep. The
714 * first new device we detect will sleep before probing.
716 * This only applies to devices whos change bit is set in
717 * the data, and does not apply to the initial boot-time
722 for (target = 0; target < ap->ap_pmcount; ++target) {
723 at = ap->ap_ata[target];
726 * Check the target state for targets behind the PM
727 * which have changed state. This will adjust
728 * at_probe and set ATA_PORT_F_RESCAN
730 * We want to wait at least 10 seconds before probing
731 * a newly inserted device. If the check status
732 * indicates a device is present and in need of a
733 * hard reset, we make sure we have slept before
736 * We also need to wait at least 1 second for the
737 * PHY state to change after insertion, if we
738 * haven't already waited the 10 seconds.
740 * NOTE: When pm_check_good finds a good port it
741 * typically starts us in probe state
742 * NEED_HARD_RESET rather than INIT.
744 if (data & (1 << target)) {
745 if (initial == 0 && didsleep == 0)
747 ahci_pm_check_good(ap, target);
748 if (initial == 0 && didsleep == 0 &&
749 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
752 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
753 ahci_os_sleep(10000);
758 * Report hot-plug events before the probe state
759 * really gets hot. Only actual events are reported
760 * here to reduce spew.
762 if (data & (1 << target)) {
763 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
764 switch(at->at_probe) {
765 case ATA_PROBE_NEED_INIT:
766 case ATA_PROBE_NEED_HARD_RESET:
767 kprintf("Device inserted\n");
769 case ATA_PROBE_FAILED:
770 kprintf("Device removed\n");
773 kprintf("Device probe in progress\n");
779 * Run through the state machine as necessary if
780 * the port is not marked failed.
782 * The state machine may stop at NEED_IDENT if
783 * CAM is not yet attached.
785 * Acquire exclusive access to the port while we
786 * are doing this. This prevents command-completion
787 * from queueing commands for non-polled targets
788 * inbetween our probe steps. We need to do this
789 * because the reset probes can generate severe PHY
790 * and protocol errors and soft-brick the port.
792 if (at->at_probe != ATA_PROBE_FAILED &&
793 at->at_probe != ATA_PROBE_GOOD) {
794 ahci_beg_exclusive_access(ap, at);
795 if (at->at_probe == ATA_PROBE_NEED_INIT)
796 ahci_pm_port_init(ap, at);
797 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
798 ahci_port_reset(ap, at, 1);
799 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
800 ahci_port_reset(ap, at, 0);
801 if (at->at_probe == ATA_PROBE_NEED_IDENT)
802 ahci_cam_probe(ap, at);
803 ahci_end_exclusive_access(ap, at);
807 * Add or remove from CAM
809 if (at->at_features & ATA_PORT_F_RESCAN) {
810 at->at_features &= ~ATA_PORT_F_RESCAN;
811 if (at->at_probe == ATA_PROBE_FAILED) {
812 ahci_cam_changed(ap, at, 0);
813 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
814 ahci_cam_changed(ap, at, 1);
817 data &= ~(1 << target);
820 kprintf("%s: WARNING (PM): extra bits set in "
821 "EINFO: %08x\n", PORTNAME(ap), data);
822 while (target < AHCI_MAX_PMPORTS) {
823 ahci_pm_check_good(ap, target);
832 * De-initialize and detach a port.
835 ahci_port_free(struct ahci_softc *sc, u_int port)
837 struct ahci_port *ap = sc->sc_ports[port];
838 struct ahci_ccb *ccb;
842 * Ensure port is disabled and its interrupts are all flushed.
845 ahci_port_stop(ap, 1);
846 ahci_os_stop_port(ap);
847 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
848 ahci_pwrite(ap, AHCI_PREG_IE, 0);
849 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
850 ahci_write(sc, AHCI_REG_IS, 1 << port);
854 while ((ccb = ahci_get_ccb(ap)) != NULL) {
855 if (ccb->ccb_dmamap) {
856 bus_dmamap_destroy(sc->sc_tag_data,
858 ccb->ccb_dmamap = NULL;
861 if ((ccb = ap->ap_err_ccb) != NULL) {
862 if (ccb->ccb_dmamap) {
863 bus_dmamap_destroy(sc->sc_tag_data,
865 ccb->ccb_dmamap = NULL;
867 ap->ap_err_ccb = NULL;
869 kfree(ap->ap_ccbs, M_DEVBUF);
873 if (ap->ap_dmamem_cmd_list) {
874 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
875 ap->ap_dmamem_cmd_list = NULL;
877 if (ap->ap_dmamem_rfis) {
878 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
879 ap->ap_dmamem_rfis = NULL;
881 if (ap->ap_dmamem_cmd_table) {
882 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
883 ap->ap_dmamem_cmd_table = NULL;
886 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
888 kfree(ap->ap_ata[i], M_DEVBUF);
889 ap->ap_ata[i] = NULL;
893 if (ap->ap_err_scratch) {
894 kfree(ap->ap_err_scratch, M_DEVBUF);
895 ap->ap_err_scratch = NULL;
898 /* bus_space(9) says we dont free the subregions handle */
901 sc->sc_ports[port] = NULL;
905 * Start high-level command processing on the port
908 ahci_port_start(struct ahci_port *ap)
910 u_int32_t r, s, is, tfd;
913 * FRE must be turned on before ST. Wait for FR to go active
914 * before turning on ST. The spec doesn't seem to think this
915 * is necessary but waiting here avoids an on-off race in the
916 * ahci_port_stop() code.
918 r = ahci_pread(ap, AHCI_PREG_CMD);
919 if ((r & AHCI_PREG_CMD_FRE) == 0) {
920 r |= AHCI_PREG_CMD_FRE;
921 ahci_pwrite(ap, AHCI_PREG_CMD, r);
923 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
924 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
925 kprintf("%s: Cannot start FIS reception\n",
934 * Turn on ST, wait for CR to come up.
936 r |= AHCI_PREG_CMD_ST;
937 ahci_pwrite(ap, AHCI_PREG_CMD, r);
938 if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
939 s = ahci_pread(ap, AHCI_PREG_SERR);
940 is = ahci_pread(ap, AHCI_PREG_IS);
941 tfd = ahci_pread(ap, AHCI_PREG_TFD);
942 kprintf("%s: Cannot start command DMA\n"
947 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
949 tfd, AHCI_PFMT_TFD_STS);
955 * (Re-)enable coalescing on the port.
957 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
958 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
959 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
960 ap->ap_sc->sc_ccc_ports_cur);
968 * Stop high-level command processing on a port
970 * WARNING! If the port is stopped while CR is still active our saved
971 * CI/SACT will race any commands completed by the command
972 * processor prior to being able to stop. Thus we never call
973 * this function unless we intend to dispose of any remaining
974 * active commands. In particular, this complicates the timeout
978 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
984 * Disable coalescing on the port while it is stopped.
986 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
987 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
988 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
989 ap->ap_sc->sc_ccc_ports_cur);
994 * Turn off ST, then wait for CR to go off.
996 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
997 r &= ~AHCI_PREG_CMD_ST;
998 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1000 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1001 kprintf("%s: Port bricked, unable to stop (ST)\n",
1008 * Turn off FRE, then wait for FR to go off. FRE cannot
1009 * be turned off until CR transitions to 0.
1011 if ((r & AHCI_PREG_CMD_FR) == 0) {
1012 kprintf("%s: FR stopped, clear FRE for next start\n",
1018 r &= ~AHCI_PREG_CMD_FRE;
1019 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1020 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
1021 kprintf("%s: Port bricked, unable to stop (FRE)\n",
1031 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1034 ahci_port_clo(struct ahci_port *ap)
1036 struct ahci_softc *sc = ap->ap_sc;
1039 /* Only attempt CLO if supported by controller */
1040 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
1044 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1045 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1047 /* Wait for completion */
1048 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1049 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1059 * If hard is 0 perform a softreset of the port.
1060 * If hard is 1 perform a hard reset of the port.
1062 * If at is non-NULL an indirect port via a port-multiplier is being
1063 * reset, otherwise a direct port is being reset.
1065 * NOTE: Indirect ports can only be soft-reset.
1068 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
1074 rc = ahci_pm_hardreset(ap, at->at_target, hard);
1076 rc = ahci_port_hardreset(ap, hard);
1079 rc = ahci_pm_softreset(ap, at->at_target);
1081 rc = ahci_port_softreset(ap);
1087 * AHCI soft reset, Section 10.4.1
1089 * (at) will be NULL when soft-resetting a directly-attached device, and
1090 * non-NULL when soft-resetting a device through a port multiplier.
1092 * This function keeps port communications intact and attempts to generate
1093 * a reset to the connected device using device commands.
1096 ahci_port_softreset(struct ahci_port *ap)
1098 struct ahci_ccb *ccb = NULL;
1099 struct ahci_cmd_hdr *cmd_slot;
1106 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
1107 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
1110 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1113 ap->ap_flags |= AP_F_IN_RESET;
1114 ap->ap_state = AP_S_NORMAL;
1117 * Remember port state in cmd (main to restore start/stop)
1121 if (ahci_port_stop(ap, 0)) {
1122 kprintf("%s: failed to stop port, cannot softreset\n",
1128 * Request CLO if device appears hung.
1130 if (ahci_pread(ap, AHCI_PREG_TFD) &
1131 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1136 * This is an attempt to clear errors so a new signature will
1137 * be latched. It isn't working properly. XXX
1140 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1143 if (ahci_port_start(ap)) {
1144 kprintf("%s: failed to start port, cannot softreset\n",
1149 /* Check whether CLO worked */
1150 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1151 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1152 kprintf("%s: CLO %s, need port reset\n",
1154 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1155 ? "failed" : "unsupported");
1161 * Prep first D2H command with SRST feature & clear busy/reset flags
1163 * It is unclear which other fields in the FIS are used. Just zero
1166 * NOTE! This CCB is used for both the first and second commands.
1167 * The second command must use CCB slot 1 to properly load
1170 ccb = ahci_get_err_ccb(ap);
1171 ccb->ccb_xa.complete = ahci_dummy_done;
1172 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
1173 KKASSERT(ccb->ccb_slot == 1);
1174 ccb->ccb_xa.at = NULL;
1175 cmd_slot = ccb->ccb_cmd_hdr;
1177 fis = ccb->ccb_cmd_table->cfis;
1178 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1179 fis[0] = ATA_FIS_TYPE_H2D;
1180 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1182 cmd_slot->prdtl = 0;
1183 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1184 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1185 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1187 ccb->ccb_xa.state = ATA_S_PENDING;
1189 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1190 kprintf("%s: First FIS failed\n", PORTNAME(ap));
1195 * WARNING! TIME SENSITIVE SPACE! WARNING!
1197 * The two FISes are supposed to be back to back. Don't issue other
1198 * commands or even delay if we can help it.
1202 * Prep second D2H command to read status and complete reset sequence
1203 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1204 * Rev 2.6 and it is unclear how the second FIS should be set up
1205 * from the AHCI document.
1207 * Give the device 3ms before sending the second FIS.
1209 * It is unclear which other fields in the FIS are used. Just zero
1212 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
1214 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1215 fis[0] = ATA_FIS_TYPE_H2D;
1216 fis[15] = ATA_FIS_CONTROL_4BIT;
1218 cmd_slot->prdtl = 0;
1219 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1221 ccb->ccb_xa.state = ATA_S_PENDING;
1222 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1223 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1227 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1228 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1229 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1231 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
1238 * If the softreset is trying to clear a BSY condition after a
1239 * normal portreset we assign the port type.
1241 * If the softreset is being run first as part of the ccb error
1242 * processing code then report if the device signature changed
1245 if (ap->ap_type == ATA_PORT_T_NONE) {
1246 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1248 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1249 kprintf("%s: device signature unexpectedly "
1250 "changed\n", PORTNAME(ap));
1251 error = EBUSY; /* XXX */
1259 ahci_put_err_ccb(ccb);
1262 * If the target is busy use CLO to clear the busy
1263 * condition. The BSY should be cleared on the next
1266 if (ahci_pread(ap, AHCI_PREG_TFD) &
1267 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1273 * If we failed to softreset make the port quiescent, otherwise
1274 * make sure the port's start/stop state matches what it was on
1277 * Don't kill the port if the softreset is on a port multiplier
1278 * target, that would kill all the targets!
1281 ahci_port_hardstop(ap);
1282 /* ap_probe set to failed */
1284 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1286 ahci_port_start(ap);
1288 ap->ap_flags &= ~AP_F_IN_RESET;
1292 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1298 * AHCI port reset, Section 10.4.2
1300 * This function does a hard reset of the port. Note that the device
1301 * connected to the port could still end-up hung.
1304 ahci_port_hardreset(struct ahci_port *ap, int hard)
1312 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1313 ap->ap_flags |= AP_F_IN_RESET;
1318 ahci_port_stop(ap, 0);
1319 ap->ap_state = AP_S_NORMAL;
1322 * The port may have been quiescent with its SUD bit cleared, so
1323 * set the SUD (spin up device).
1325 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1326 cmd |= AHCI_PREG_CMD_SUD;
1327 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1330 * Perform device detection.
1332 * NOTE! AHCi_PREG_SCTL_DET_DISABLE seems to be highly unreliable
1333 * on multiple chipsets and can brick the chipset or even
1334 * the whole PC. Never use it.
1336 ap->ap_type = ATA_PORT_T_NONE;
1338 r = AHCI_PREG_SCTL_IPM_DISABLED;
1339 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1343 * Start transmitting COMRESET. COMRESET must be sent for at
1346 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1347 if (AhciForceGen1 & (1 << ap->ap_num))
1348 r |= AHCI_PREG_SCTL_SPD_GEN1;
1350 r |= AHCI_PREG_SCTL_SPD_ANY;
1351 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1354 * Through trial and error it seems to take around 100ms
1355 * for the detect logic to settle down. If this is too
1356 * short the softreset code will fail.
1361 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1362 * since we are hard-resetting the port we might as well clear
1363 * the whole enchillada
1366 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1367 r &= ~AHCI_PREG_SCTL_DET_INIT;
1368 r |= AHCI_PREG_SCTL_DET_NONE;
1369 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1372 * Try to determine if there is a device on the port.
1374 * Give the device 3/10 second to at least be detected.
1375 * If we fail clear PRCS (phy detect) since we may cycled
1376 * the phy and probably caused another PRCS interrupt.
1380 r = ahci_pread(ap, AHCI_PREG_SSTS);
1381 if (r & AHCI_PREG_SSTS_DET)
1383 loop -= ahci_os_softsleep();
1386 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1388 kprintf("%s: Port appears to be unplugged\n",
1396 * There is something on the port. Give the device 3 seconds
1397 * to fully negotiate.
1399 if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1400 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1402 kprintf("%s: Device may be powered down\n",
1410 * We got something that definitely looks like a device. Give
1411 * the device time to send us its first D2H FIS. Waiting for
1412 * BSY to clear accomplishes this.
1414 * NOTE that a port multiplier may or may not clear BSY here,
1415 * depending on what is sitting in target 0 behind it.
1419 if (ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
1420 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1428 * Do the PM port probe regardless of how things turned out on
1431 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM)
1432 error = ahci_pm_port_probe(ap, error);
1441 * All good, make sure the port is running and set the
1442 * probe state. Ignore the signature junk (it's unreliable)
1443 * until we get to the softreset code.
1445 if (ahci_port_start(ap)) {
1446 kprintf("%s: failed to start command DMA on port, "
1447 "disabling\n", PORTNAME(ap));
1451 if (ap->ap_type == ATA_PORT_T_PM)
1452 ap->ap_probe = ATA_PROBE_GOOD;
1454 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1458 * Normal device probe failure
1460 data = ahci_pread(ap, AHCI_PREG_SSTS);
1462 switch(data & AHCI_PREG_SSTS_DET) {
1463 case AHCI_PREG_SSTS_DET_DEV_NE:
1464 kprintf("%s: Device not communicating\n",
1467 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
1468 kprintf("%s: PHY offline\n",
1472 kprintf("%s: No device detected\n",
1476 ahci_port_hardstop(ap);
1480 * Abnormal probe (EBUSY)
1482 kprintf("%s: Device on port is bricked\n",
1484 ahci_port_hardstop(ap);
1486 rc = ahci_port_reset(ap, atx, 0);
1488 kprintf("%s: Unable unbrick device\n",
1491 kprintf("%s: Successfully unbricked\n",
1501 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1502 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
1504 ap->ap_flags &= ~AP_F_IN_RESET;
1507 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1512 * Hard-stop on hot-swap device removal. See 10.10.1
1514 * Place the port in a mode that will allow it to detect hot-swap insertions.
1515 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1516 * seem to do the job.
1518 * FIS reception is left enabled but command processing is disabled.
1519 * Cycling FIS reception (FRE) can brick ports.
1522 ahci_port_hardstop(struct ahci_port *ap)
1524 struct ahci_ccb *ccb;
1525 struct ata_port *at;
1532 * Stop the port. We can't modify things like SUD if the port
1535 ap->ap_state = AP_S_FATAL_ERROR;
1536 ap->ap_probe = ATA_PROBE_FAILED;
1537 ap->ap_type = ATA_PORT_T_NONE;
1538 ahci_port_stop(ap, 0);
1539 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1542 * Clean up AT sub-ports on SATA port.
1544 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1546 at->at_type = ATA_PORT_T_NONE;
1547 at->at_probe = ATA_PROBE_FAILED;
1551 * Turn off port-multiplier control bit
1553 if (cmd & AHCI_PREG_CMD_PMA) {
1554 cmd &= ~AHCI_PREG_CMD_PMA;
1555 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1559 * Make sure FRE is active. There isn't anything we can do if it
1560 * fails so just ignore errors.
1562 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1563 cmd |= AHCI_PREG_CMD_FRE;
1564 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1565 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1566 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1570 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1571 * 10.10.1 place us in the Listen state.
1573 * Deactivating SUD only applies if the controller supports SUD.
1575 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
1577 if (cmd & AHCI_PREG_CMD_SUD) {
1578 cmd &= ~AHCI_PREG_CMD_SUD;
1579 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1584 * Transition su to the spin-up state. HVA shall send COMRESET and
1585 * begin initialization sequence (whatever that means).
1587 * This only applies if the controller supports SUD.
1588 * NEVER use AHCI_PREG_DET_DISABLE.
1590 cmd |= AHCI_PREG_CMD_SUD;
1591 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1595 * Transition us to the Reset state. Theoretically we send a
1596 * continuous stream of COMRESETs in this state.
1598 r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1599 if (AhciForceGen1 & (1 << ap->ap_num)) {
1600 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1601 r |= AHCI_PREG_SCTL_SPD_GEN1;
1603 r |= AHCI_PREG_SCTL_SPD_ANY;
1605 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1609 * Flush SERR_DIAG_X so the TFD can update.
1614 * Clean out pending ccbs
1616 while (ap->ap_active) {
1617 slot = ffs(ap->ap_active) - 1;
1618 ap->ap_active &= ~(1 << slot);
1619 ap->ap_expired &= ~(1 << slot);
1620 --ap->ap_active_cnt;
1621 ccb = &ap->ap_ccbs[slot];
1622 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1623 callout_stop(&ccb->ccb_timeout);
1624 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1626 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1627 ATA_F_TIMEOUT_EXPIRED);
1628 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1630 ccb->ccb_xa.complete(&ccb->ccb_xa);
1632 while (ap->ap_sactive) {
1633 slot = ffs(ap->ap_sactive) - 1;
1634 ap->ap_sactive &= ~(1 << slot);
1635 ap->ap_expired &= ~(1 << slot);
1636 ccb = &ap->ap_ccbs[slot];
1637 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1638 callout_stop(&ccb->ccb_timeout);
1639 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1641 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1642 ATA_F_TIMEOUT_EXPIRED);
1643 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1645 ccb->ccb_xa.complete(&ccb->ccb_xa);
1647 KKASSERT(ap->ap_active_cnt == 0);
1649 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1650 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1651 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1652 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1654 ccb->ccb_xa.complete(&ccb->ccb_xa);
1658 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1659 * hopefully send us a DIAG_X-related interrupt if it receives
1660 * a COMINIT, and if not that then at least a Phy transition
1663 * If we transition INIT from 1->0 to begin the initalization
1664 * sequence it is unclear if that sequence will remain active
1665 * until the next device insertion.
1667 * If we go back to the listen state it is unclear if the
1668 * device will actually send us a COMINIT, since we aren't
1669 * sending any COMRESET's
1675 * We can't loop on the X bit, a continuous COMINIT received will make
1676 * it loop forever. Just assume one event has built up and clear X
1677 * so the task file descriptor can update.
1680 ahci_flush_tfd(struct ahci_port *ap)
1684 r = ahci_pread(ap, AHCI_PREG_SERR);
1685 if (r & AHCI_PREG_SERR_DIAG_X)
1686 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1690 * Figure out what type of device is connected to the port, ATAPI or
1694 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1698 sig = ahci_pread(ap, AHCI_PREG_SIG);
1700 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1701 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1702 return(ATA_PORT_T_ATAPI);
1703 } else if ((sig & 0xffff0000) ==
1704 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1705 return(ATA_PORT_T_PM);
1707 return(ATA_PORT_T_DISK);
1712 * Load the DMA descriptor table for a CCB's buffer.
1715 ahci_load_prdt(struct ahci_ccb *ccb)
1717 struct ahci_port *ap = ccb->ccb_port;
1718 struct ahci_softc *sc = ap->ap_sc;
1719 struct ata_xfer *xa = &ccb->ccb_xa;
1720 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1721 bus_dmamap_t dmap = ccb->ccb_dmamap;
1722 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1725 if (xa->datalen == 0) {
1726 ccb->ccb_cmd_hdr->prdtl = 0;
1730 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1731 xa->data, xa->datalen,
1732 ahci_load_prdt_callback,
1734 ((xa->flags & ATA_F_NOWAIT) ?
1735 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1737 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1741 if (xa->flags & ATA_F_PIO)
1742 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1745 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1747 if (xa->flags & ATA_F_READ)
1748 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1749 if (xa->flags & ATA_F_WRITE)
1750 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1756 * Callback from BUSDMA system to load the segment list. The passed segment
1757 * list is a temporary structure.
1761 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1764 struct ahci_prdt *prd = *(void **)info;
1767 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1770 addr = segs->ds_addr;
1771 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1772 prd->dba_lo = htole32((u_int32_t)addr);
1773 prd->flags = htole32(segs->ds_len - 1);
1779 *(void **)info = prd; /* return last valid segment */
1783 ahci_unload_prdt(struct ahci_ccb *ccb)
1785 struct ahci_port *ap = ccb->ccb_port;
1786 struct ahci_softc *sc = ap->ap_sc;
1787 struct ata_xfer *xa = &ccb->ccb_xa;
1788 bus_dmamap_t dmap = ccb->ccb_dmamap;
1790 if (xa->datalen != 0) {
1791 if (xa->flags & ATA_F_READ) {
1792 bus_dmamap_sync(sc->sc_tag_data, dmap,
1793 BUS_DMASYNC_POSTREAD);
1795 if (xa->flags & ATA_F_WRITE) {
1796 bus_dmamap_sync(sc->sc_tag_data, dmap,
1797 BUS_DMASYNC_POSTWRITE);
1799 bus_dmamap_unload(sc->sc_tag_data, dmap);
1802 * prdbc is only updated by hardware for non-NCQ commands.
1804 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1807 if (ccb->ccb_cmd_hdr->prdbc == 0 &&
1808 ccb->ccb_xa.state == ATA_S_COMPLETE) {
1809 kprintf("%s: WARNING! Unload prdbc resid "
1810 "was zero! tag=%d\n",
1811 ATANAME(ap, xa->at), ccb->ccb_slot);
1813 xa->resid = xa->datalen -
1814 le32toh(ccb->ccb_cmd_hdr->prdbc);
1820 * Start a command and poll for completion.
1822 * timeout is in ms and only counts once the command gets on-chip.
1824 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1825 * that no error occured.
1827 * NOTE: If the caller specifies a NULL timeout function the caller is
1828 * responsible for clearing hardware state on failure, but we will
1829 * deal with removing the ccb from any pending queue.
1831 * NOTE: NCQ should never be used with this function.
1833 * NOTE: If the port is in a failed state and stopped we do not try
1834 * to activate the ccb.
1837 ahci_poll(struct ahci_ccb *ccb, int timeout,
1838 void (*timeout_fn)(struct ahci_ccb *))
1840 struct ahci_port *ap = ccb->ccb_port;
1842 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1843 ccb->ccb_xa.state = ATA_S_ERROR;
1844 return(ccb->ccb_xa.state);
1848 kprintf("%s: Start command %02x tag=%d\n",
1849 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
1850 ccb->ccb_xa.fis->command, ccb->ccb_slot);
1855 ahci_port_intr(ap, 1);
1856 switch(ccb->ccb_xa.state) {
1858 timeout -= ahci_os_softsleep();
1861 ahci_os_softsleep();
1862 ahci_check_active_timeouts(ap);
1866 return (ccb->ccb_xa.state);
1868 } while (timeout > 0);
1870 kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
1871 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
1872 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
1873 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
1874 ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
1880 return(ccb->ccb_xa.state);
1884 * When polling we have to check if the currently active CCB(s)
1885 * have timed out as the callout will be deadlocked while we
1886 * hold the port lock.
1889 ahci_check_active_timeouts(struct ahci_port *ap)
1891 struct ahci_ccb *ccb;
1895 mask = ap->ap_active | ap->ap_sactive;
1897 tag = ffs(mask) - 1;
1898 mask &= ~(1 << tag);
1899 ccb = &ap->ap_ccbs[tag];
1900 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1901 ahci_ata_cmd_timeout(ccb);
1909 ahci_start_timeout(struct ahci_ccb *ccb)
1911 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
1912 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
1913 callout_reset(&ccb->ccb_timeout,
1914 (ccb->ccb_xa.timeout * hz + 999) / 1000,
1915 ahci_ata_cmd_timeout_unserialized, ccb);
1920 ahci_start(struct ahci_ccb *ccb)
1922 struct ahci_port *ap = ccb->ccb_port;
1923 struct ahci_softc *sc = ap->ap_sc;
1925 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1927 /* Zero transferred byte count before transfer */
1928 ccb->ccb_cmd_hdr->prdbc = 0;
1930 /* Sync command list entry and corresponding command table entry */
1931 bus_dmamap_sync(sc->sc_tag_cmdh,
1932 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1933 BUS_DMASYNC_PREWRITE);
1934 bus_dmamap_sync(sc->sc_tag_cmdt,
1935 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1936 BUS_DMASYNC_PREWRITE);
1938 /* Prepare RFIS area for write by controller */
1939 bus_dmamap_sync(sc->sc_tag_rfis,
1940 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1941 BUS_DMASYNC_PREREAD);
1944 * There's no point trying to optimize this, it only shaves a few
1945 * nanoseconds so just queue the command and call our generic issue.
1947 ahci_issue_pending_commands(ap, ccb);
1951 * While holding the port lock acquire exclusive access to the port.
1953 * This is used when running the state machine to initialize and identify
1954 * targets over a port multiplier. Setting exclusive access prevents
1955 * ahci_port_intr() from activating any requests sitting on the pending
1959 ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
1961 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1962 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1963 while (ap->ap_active || ap->ap_sactive) {
1964 ahci_port_intr(ap, 1);
1965 ahci_os_softsleep();
1970 ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
1972 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
1973 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
1974 ahci_issue_pending_commands(ap, NULL);
1980 fubar(struct ahci_ccb *ccb)
1982 struct ahci_port *ap = ccb->ccb_port;
1983 struct ahci_cmd_hdr *cmd;
1984 struct ahci_cmd_table *tab;
1985 struct ahci_prdt *prdt;
1988 kprintf("%s: ISSUE %02x\n",
1989 ATANAME(ap, ccb->ccb_xa.at),
1990 ccb->ccb_xa.fis->command);
1991 cmd = ccb->ccb_cmd_hdr;
1992 tab = ccb->ccb_cmd_table;
1993 prdt = ccb->ccb_cmd_table->prdt;
1994 kprintf("cmd flags=%04x prdtl=%d prdbc=%d ctba=%08x%08x\n",
1995 cmd->flags, cmd->prdtl, cmd->prdbc,
1996 cmd->ctba_hi, cmd->ctba_lo);
1997 for (i = 0; i < cmd->prdtl; ++i) {
1998 kprintf("\t%d dba=%08x%08x res=%08x flags=%08x\n",
1999 i, prdt->dba_hi, prdt->dba_lo, prdt->reserved,
2008 * If ccb is not NULL enqueue and/or issue it.
2010 * If ccb is NULL issue whatever we can from the queue. However, nothing
2011 * new is issued if the exclusive access flag is set or expired ccb's are
2014 * If existing commands are still active (ap_active/ap_sactive) we can only
2015 * issue matching new commands.
2018 ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
2026 * If just running the queue and in exclusive access mode we
2027 * just return. Also in this case if there are any expired ccb's
2028 * we want to clear the queue so the port can be safely stopped.
2031 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
2032 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
2037 * Pull the next ccb off the queue and run it if possible.
2039 if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
2043 * Handle exclusivity requirements.
2045 * ATA_F_EXCLUSIVE is used when we want to be the only command
2048 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
2049 * back into the ccb on a normal (non-errored) command completion.
2050 * For example, for PM requests to target 15. Because the AHCI
2051 * spec does not stop the command processor and has only one rfis
2052 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
2053 * Otherwise multiple completions can destroy the rfis data before
2054 * we have a chance to copy it.
2056 if (ap->ap_active & ~ap->ap_expired) {
2058 * There may be multiple ccb's already running,
2059 * if any are running and ap_run_flags sets
2060 * one of these flags then we know only one is
2063 * XXX Current AUTOSENSE code forces exclusivity
2064 * to simplify the code.
2066 if (ap->ap_run_flags &
2067 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2071 if (ccb->ccb_xa.flags &
2072 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2077 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2079 * The next command is a NCQ command and can be issued as
2080 * long as currently active commands are not standard.
2082 if (ap->ap_active) {
2083 KKASSERT(ap->ap_active_cnt > 0);
2086 KKASSERT(ap->ap_active_cnt == 0);
2090 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2091 mask |= 1 << ccb->ccb_slot;
2092 ccb->ccb_xa.state = ATA_S_ONCHIP;
2093 ahci_start_timeout(ccb);
2094 ap->ap_run_flags = ccb->ccb_xa.flags;
2095 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2096 } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
2098 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
2100 ap->ap_sactive |= mask;
2101 ahci_pwrite(ap, AHCI_PREG_SACT, mask);
2102 ahci_pwrite(ap, AHCI_PREG_CI, mask);
2105 * The next command is a standard command and can be issued
2106 * as long as currently active commands are not NCQ.
2108 * We limit ourself to 1 command if we have a port multiplier,
2109 * (at least without FBSS support), otherwise timeouts on
2110 * one port can race completions on other ports (see
2111 * ahci_ata_cmd_timeout() for more information).
2113 * If not on a port multiplier generally allow up to 4
2114 * standard commands to be enqueued. Remember that the
2115 * command processor will still process them sequentially.
2119 if (ap->ap_type == ATA_PORT_T_PM)
2121 else if (ap->ap_sc->sc_ncmds > 4)
2126 while (ap->ap_active_cnt < limit && ccb &&
2127 (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
2128 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2132 ap->ap_active |= 1 << ccb->ccb_slot;
2133 ap->ap_active_cnt++;
2134 ap->ap_run_flags = ccb->ccb_xa.flags;
2135 ccb->ccb_xa.state = ATA_S_ONCHIP;
2136 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
2137 ahci_start_timeout(ccb);
2138 if ((ap->ap_run_flags &
2139 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
2142 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2143 if (ccb && (ccb->ccb_xa.flags &
2144 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
2152 ahci_intr(void *arg)
2154 struct ahci_softc *sc = arg;
2155 struct ahci_port *ap;
2161 * Check if the master enable is up, and whether any interrupts are
2164 if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2166 is = ahci_read(sc, AHCI_REG_IS);
2167 if (is == 0 || is == 0xffffffff) {
2170 is &= sc->sc_portmask;
2172 #ifdef AHCI_COALESCE
2173 /* Check coalescing interrupt first */
2174 if (is & sc->sc_ccc_mask) {
2175 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2177 is &= ~sc->sc_ccc_mask;
2178 is |= sc->sc_ccc_ports_cur;
2183 * Process interrupts for each port in a non-blocking fashion.
2185 * The global IS bit is forced on if any unmasked port interrupts
2186 * are pending, even if we clear.
2188 for (ack = 0; is; is &= ~(1 << port)) {
2192 ap = sc->sc_ports[port];
2196 if (ahci_os_lock_port_nb(ap) == 0) {
2197 ahci_port_intr(ap, 0);
2198 ahci_os_unlock_port(ap);
2200 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2201 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2204 ahci_write(sc, AHCI_REG_IS, ack);
2208 * Core called from helper thread.
2211 ahci_port_thread_core(struct ahci_port *ap, int mask)
2214 * Process any expired timedouts.
2216 ahci_os_lock_port(ap);
2217 if (mask & AP_SIGF_TIMEOUT) {
2218 ahci_check_active_timeouts(ap);
2222 * Process port interrupts which require a higher level of
2225 if (mask & AP_SIGF_PORTINT) {
2226 ahci_port_intr(ap, 1);
2227 ahci_port_interrupt_enable(ap);
2228 ahci_os_unlock_port(ap);
2229 } else if (ap->ap_probe != ATA_PROBE_FAILED) {
2230 ahci_port_intr(ap, 1);
2231 ahci_port_interrupt_enable(ap);
2232 ahci_os_unlock_port(ap);
2234 ahci_os_unlock_port(ap);
2239 * Core per-port interrupt handler.
2241 * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2242 * deal with normal command completions which do not require blocking.
2245 ahci_port_intr(struct ahci_port *ap, int blockable)
2247 struct ahci_softc *sc = ap->ap_sc;
2248 u_int32_t is, ci_saved, ci_masked;
2250 struct ahci_ccb *ccb = NULL;
2251 struct ata_port *ccb_at = NULL;
2252 volatile u_int32_t *active;
2253 const u_int32_t blockable_mask = AHCI_PREG_IS_TFES |
2261 enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
2262 NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2265 * All basic command completions are always processed.
2267 is = ahci_pread(ap, AHCI_PREG_IS);
2268 if (is & AHCI_PREG_IS_DPS)
2269 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2272 * If we can't block then we can't handle these here. Disable
2273 * the interrupts in question so we don't live-lock, the helper
2274 * thread will re-enable them.
2276 * If the port is in a completely failed state we do not want
2277 * to drop through to failed-command-processing if blockable is 0,
2278 * just let the thread deal with it all.
2280 * Otherwise we fall through and still handle DHRS and any commands
2281 * which completed normally. Even if we are errored we haven't
2282 * stopped the port yet so CI/SACT are still good.
2284 if (blockable == 0) {
2285 if (ap->ap_state == AP_S_FATAL_ERROR) {
2286 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2287 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2290 if (is & blockable_mask) {
2291 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2292 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2298 * Either NCQ or non-NCQ commands will be active, never both.
2300 if (ap->ap_sactive) {
2301 KKASSERT(ap->ap_active == 0);
2302 KKASSERT(ap->ap_active_cnt == 0);
2303 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2304 active = &ap->ap_sactive;
2306 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2307 active = &ap->ap_active;
2309 KKASSERT(!(ap->ap_sactive && ap->ap_active));
2311 kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
2312 ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
2313 ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
2316 /* ignore AHCI_PREG_IS_PRCS when link power management is on */
2317 if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2318 is &= ~AHCI_PREG_IS_PRCS;
2319 ahci_pwrite(ap, AHCI_PREG_SERR,
2320 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2323 if (is & AHCI_PREG_IS_TFES) {
2325 * Command failed (blockable).
2327 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2329 * This stops command processing.
2331 u_int32_t tfd, serr;
2335 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2336 serr = ahci_pread(ap, AHCI_PREG_SERR);
2339 * Load the error slot and restart command processing.
2340 * CLO if we need to. The error slot may not be valid.
2341 * MUST BE DONE BEFORE CLEARING ST!
2345 * It is unclear but we may have to clear SERR to reenable
2348 err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
2349 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
2353 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
2354 AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
2355 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2356 ahci_port_stop(ap, 0);
2357 ahci_os_hardsleep(10);
2358 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2359 kprintf("%s: Issuing CLO\n", PORTNAME(ap));
2362 ahci_port_start(ap);
2363 need = NEED_RESTART;
2366 * ATAPI errors are fairly common from probing, just
2367 * report disk errors or if bootverbose is on.
2369 if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
2370 kprintf("%s: TFES slot %d ci_saved = %08x\n",
2371 PORTNAME(ap), err_slot, ci_saved);
2375 * If we got an error on an error CCB just complete it
2376 * with an error. ci_saved has the mask to restart
2377 * (the err_ccb will be removed from it by finish_error).
2379 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2380 err_slot = ap->ap_err_ccb->ccb_slot;
2385 * If NCQ commands were active get the error slot from
2386 * the log page. NCQ is not supported for PM's so this
2387 * is a direct-attached target.
2389 * Otherwise if no commands were active we have a problem.
2391 * Otherwise if the error slot is bad we have a problem.
2393 * Otherwise process the error for the slot.
2395 if (ap->ap_sactive) {
2396 err_slot = ahci_port_read_ncq_error(ap, 0);
2397 } else if (ap->ap_active == 0) {
2398 kprintf("%s: TFES with no commands pending\n",
2401 } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
2402 kprintf("%s: bad error slot %d\n",
2403 PORTNAME(ap), err_slot);
2406 ccb = &ap->ap_ccbs[err_slot];
2409 * Validate the errored ccb. Note that ccb_at can
2410 * be NULL for direct-attached ccb's.
2412 * Copy received taskfile data from the RFIS.
2414 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
2415 ccb_at = ccb->ccb_xa.at;
2416 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2417 sizeof(struct ata_fis_d2h));
2419 kprintf("%s: Copying rfis slot %d\n",
2420 ATANAME(ap, ccb_at), err_slot);
2423 kprintf("%s: Cannot copy rfis, CCB slot "
2424 "%d is not on-chip (state=%d)\n",
2425 ATANAME(ap, ccb->ccb_xa.at),
2426 err_slot, ccb->ccb_xa.state);
2432 * If we could not determine the errored slot then
2436 kprintf("%s: TFES: Unable to determine errored slot\n",
2438 if (ap->ap_flags & AP_F_IN_RESET)
2444 * Finish error on slot. We will restart ci_saved
2445 * commands except the errored slot which we generate
2449 ccb = &ap->ap_ccbs[err_slot];
2450 ci_saved &= ~(1 << err_slot);
2451 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2452 ccb->ccb_xa.state = ATA_S_ERROR;
2453 } else if (is & AHCI_PREG_IS_DHRS) {
2455 * Command posted D2H register FIS to the rfis (non-blocking).
2457 * A normal completion with an error may set DHRS instead
2458 * of TFES. The CCS bits are only valid if ERR was set.
2459 * If ERR is set command processing was probably stopped.
2461 * If ERR was not set we can only copy-back data for
2462 * exclusive-mode commands because otherwise we won't know
2463 * which tag the rfis belonged to.
2465 * err_slot must be read from the CCS before any other port
2466 * action, such as stopping the port.
2468 * WARNING! This is not well documented in the AHCI spec.
2469 * It can be found in the state machine tables
2470 * but not in the explanations.
2476 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2477 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2479 if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
2480 (cmd & AHCI_PREG_CMD_CR) == 0) {
2481 err_slot = AHCI_PREG_CMD_CCS(
2482 ahci_pread(ap, AHCI_PREG_CMD));
2483 ccb = &ap->ap_ccbs[err_slot];
2484 kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
2486 tfd, AHCI_PFMT_TFD_STS,
2487 err_slot, ccb->ccb_xa.fis->command);
2491 * NO ELSE... copy back is in the normal command completion
2492 * code and only if no error occured and ATA_F_AUTOSENSE
2495 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2499 * Device notification to us (non-blocking)
2501 * NOTE! On some parts notification bits can cause an IPMS
2502 * interrupt instead of a SDBS interrupt.
2504 * NOTE! On some parts (e.g. VBOX, probably intel ICHx),
2505 * SDBS notifies us of the completion of a NCQ command
2508 if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
2511 ahci_pwrite(ap, AHCI_PREG_IS,
2512 AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2513 if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
2514 data = ahci_pread(ap, AHCI_PREG_SNTF);
2516 ahci_pwrite(ap, AHCI_PREG_IS,
2518 kprintf("%s: NOTIFY %08x\n",
2519 PORTNAME(ap), data);
2520 ahci_pwrite(ap, AHCI_PREG_SERR,
2521 AHCI_PREG_SERR_DIAG_N);
2522 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2523 ahci_cam_changed(ap, NULL, -1);
2526 is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2530 * Spurious IFS errors (blockable).
2532 * Spurious IFS errors can occur while we are doing a reset
2533 * sequence through a PM. Try to recover if we are being asked
2534 * to ignore IFS errors during these periods.
2536 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2537 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2538 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2539 kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
2542 serr, AHCI_PFMT_SERR);
2543 ap->ap_flags |= AP_F_IFS_IGNORED;
2545 ap->ap_flags |= AP_F_IFS_OCCURED;
2546 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2547 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2548 is &= ~AHCI_PREG_IS_IFS;
2549 ahci_port_stop(ap, 0);
2550 ahci_port_start(ap);
2551 kprintf("%s: Spurious IFS error\n", PORTNAME(ap));
2553 /* need = NEED_RESTART; */
2557 * Port change (hot-plug) (blockable).
2559 * A PCS interrupt will occur on hot-plug once communication is
2562 * A PRCS interrupt will occur on hot-unplug (and possibly also
2565 * XXX We can then check the CPS (Cold Presence State) bit, if
2566 * supported, to determine if a device is plugged in or not and do
2569 * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and
2570 * can also occur if an unsolicited COMINIT is received.
2571 * If this occurs command processing is automatically
2572 * stopped (CR goes inactive) and the port must be stopped
2576 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2577 kprintf("%s: Transient Errors: %b\n",
2578 PORTNAME(ap), is, AHCI_PFMT_IS);
2579 ahci_pwrite(ap, AHCI_PREG_SERR,
2580 (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
2581 ahci_pwrite(ap, AHCI_PREG_IS,
2582 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2583 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2584 ahci_port_stop(ap, 0);
2586 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2587 case AHCI_PREG_SSTS_DET_DEV:
2588 if (ap->ap_probe == ATA_PROBE_FAILED) {
2589 need = NEED_HOTPLUG_INSERT;
2592 need = NEED_RESTART;
2595 if (ap->ap_probe != ATA_PROBE_FAILED) {
2596 need = NEED_HOTPLUG_REMOVE;
2599 need = NEED_RESTART;
2605 * Check for remaining errors - they are fatal. (blockable)
2607 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2608 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2611 ahci_pwrite(ap, AHCI_PREG_IS,
2612 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2613 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2615 serr = ahci_pread(ap, AHCI_PREG_SERR);
2616 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
2617 "disabling port.\n",
2620 serr, AHCI_PFMT_SERR
2622 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2623 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2625 /* XXX try recovery first */
2630 * Fail all outstanding commands if we know the port won't recover.
2632 * We may have a ccb_at if the failed command is known and was
2633 * being sent to a device over a port multiplier (PM). In this
2634 * case if the port itself has not completely failed we fail just
2635 * the commands related to that target.
2637 * ci_saved contains the mask of active commands as of when the
2638 * error occured, prior to any port stops.
2640 if (ap->ap_state == AP_S_FATAL_ERROR) {
2642 ap->ap_state = AP_S_FATAL_ERROR;
2643 ahci_port_stop(ap, 0);
2645 kprintf("%s: Failing all commands\n", PORTNAME(ap));
2648 * Error all the active slots not already errored. If
2649 * running across a PM try to error out just the slots
2650 * related to the target.
2652 ci_masked = ci_saved & *active & ~ap->ap_expired;
2654 slot = ffs(ci_masked) - 1;
2655 ccb = &ap->ap_ccbs[slot];
2656 if (ccb_at == ccb->ccb_xa.at ||
2657 ap->ap_state == AP_S_FATAL_ERROR) {
2658 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2659 ap->ap_expired |= 1 << slot;
2660 ci_saved &= ~(1 << slot);
2662 ci_masked &= ~(1 << slot);
2666 * Clear bits in ci_saved (cause completions to be run)
2667 * for all slots which are not active.
2669 ci_saved &= ~*active;
2672 * Don't restart the port if our problems were deemed fatal.
2674 * Also acknowlege all fatal interrupt sources to prevent
2677 if (ap->ap_state == AP_S_FATAL_ERROR) {
2678 if (need == NEED_RESTART)
2679 need = NEED_NOTHING;
2680 ahci_pwrite(ap, AHCI_PREG_IS,
2681 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2682 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2688 * CCB completion (non blocking).
2690 * CCB completion is detected by noticing its slot's bit in CI has
2691 * changed to zero some time after we activated it.
2692 * If we are polling, we may only be interested in particular slot(s).
2694 * Any active bits not saved are completed within the restrictions
2695 * imposed by the caller.
2697 ci_masked = ~ci_saved & *active;
2699 slot = ffs(ci_masked) - 1;
2700 ccb = &ap->ap_ccbs[slot];
2701 ci_masked &= ~(1 << slot);
2703 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2704 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2707 bus_dmamap_sync(sc->sc_tag_cmdh,
2708 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2709 BUS_DMASYNC_POSTWRITE);
2711 bus_dmamap_sync(sc->sc_tag_cmdt,
2712 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2713 BUS_DMASYNC_POSTWRITE);
2715 bus_dmamap_sync(sc->sc_tag_rfis,
2716 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2717 BUS_DMASYNC_POSTREAD);
2719 *active &= ~(1 << ccb->ccb_slot);
2720 if (active == &ap->ap_active) {
2721 KKASSERT(ap->ap_active_cnt > 0);
2722 --ap->ap_active_cnt;
2726 * Complete the ccb. If the ccb was marked expired it
2727 * was probably already removed from the command processor,
2728 * so don't take the clear ci_saved bit as meaning the
2729 * command actually succeeded, it didn't.
2731 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
2732 ap->ap_expired &= ~(1 << ccb->ccb_slot);
2733 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2735 ccb->ccb_xa.complete(&ccb->ccb_xa);
2737 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
2738 ccb->ccb_xa.state = ATA_S_COMPLETE;
2739 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
2740 memcpy(&ccb->ccb_xa.rfis,
2742 sizeof(struct ata_fis_d2h));
2743 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
2744 ccb->ccb_xa.state = ATA_S_ERROR;
2750 ahci_issue_pending_commands(ap, NULL);
2753 * Cleanup. Will not be set if non-blocking.
2758 * A recoverable error occured and we can restart outstanding
2759 * commands on the port.
2761 ci_saved &= ~ap->ap_expired;
2763 kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
2764 ahci_issue_saved_commands(ap, ci_saved);
2767 case NEED_HOTPLUG_INSERT:
2769 * A hot-plug insertion event has occured and all
2770 * outstanding commands have already been revoked.
2772 * Don't recurse if this occurs while we are
2773 * resetting the port.
2775 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2776 kprintf("%s: HOTPLUG - Device inserted\n",
2778 ap->ap_probe = ATA_PROBE_NEED_INIT;
2779 ahci_cam_changed(ap, NULL, -1);
2782 case NEED_HOTPLUG_REMOVE:
2784 * A hot-plug removal event has occured and all
2785 * outstanding commands have already been revoked.
2787 * Don't recurse if this occurs while we are
2788 * resetting the port.
2790 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
2791 kprintf("%s: HOTPLUG - Device removed\n",
2793 ahci_port_hardstop(ap);
2794 /* ap_probe set to failed */
2795 ahci_cam_changed(ap, NULL, -1);
2804 ahci_get_ccb(struct ahci_port *ap)
2806 struct ahci_ccb *ccb;
2808 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2809 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2811 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2812 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2813 ccb->ccb_xa.state = ATA_S_SETUP;
2814 ccb->ccb_xa.at = NULL;
2816 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2822 ahci_put_ccb(struct ahci_ccb *ccb)
2824 struct ahci_port *ap = ccb->ccb_port;
2826 ccb->ccb_xa.state = ATA_S_PUT;
2827 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2828 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2829 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2833 ahci_get_err_ccb(struct ahci_port *ap)
2835 struct ahci_ccb *err_ccb;
2839 /* No commands may be active on the chip. */
2841 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
2842 sact = ahci_pread(ap, AHCI_PREG_SACT);
2844 kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
2845 PORTNAME(ap), sact);
2848 ci = ahci_pread(ap, AHCI_PREG_CI);
2850 kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
2854 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2855 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2857 /* Save outstanding command state. */
2858 ap->ap_err_saved_active = ap->ap_active;
2859 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2860 ap->ap_err_saved_sactive = ap->ap_sactive;
2863 * Pretend we have no commands outstanding, so that completions won't
2866 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2869 * Grab a CCB to use for error recovery. This should never fail, as
2870 * we ask atascsi to reserve one for us at init time.
2872 err_ccb = ap->ap_err_ccb;
2873 KKASSERT(err_ccb != NULL);
2874 err_ccb->ccb_xa.flags = 0;
2875 err_ccb->ccb_done = ahci_empty_done;
2881 ahci_put_err_ccb(struct ahci_ccb *ccb)
2883 struct ahci_port *ap = ccb->ccb_port;
2887 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2890 * No commands may be active on the chip
2892 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
2893 sact = ahci_pread(ap, AHCI_PREG_SACT);
2895 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
2896 ccb->ccb_slot, sact);
2899 ci = ahci_pread(ap, AHCI_PREG_CI);
2901 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2902 "(act=%08x sact=%08x)\n",
2904 ap->ap_active, ap->ap_sactive);
2907 KKASSERT(ccb == ap->ap_err_ccb);
2909 /* Restore outstanding command state */
2910 ap->ap_sactive = ap->ap_err_saved_sactive;
2911 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2912 ap->ap_active = ap->ap_err_saved_active;
2914 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2918 * Read log page to get NCQ error.
2920 * NOTE: NCQ not currently supported on port multipliers. XXX
2923 ahci_port_read_ncq_error(struct ahci_port *ap, int target)
2925 struct ata_log_page_10h *log;
2926 struct ahci_ccb *ccb;
2927 struct ahci_ccb *ccb2;
2928 struct ahci_cmd_hdr *cmd_slot;
2929 struct ata_fis_h2d *fis;
2933 kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
2938 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
2940 * Getting err_ccb clears active/sactive/active_cnt, putting
2941 * it back restores the fields.
2943 ccb = ahci_get_err_ccb(ap);
2944 ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
2945 ccb->ccb_xa.data = ap->ap_err_scratch;
2946 ccb->ccb_xa.datalen = 512;
2947 ccb->ccb_xa.complete = ahci_dummy_done;
2948 ccb->ccb_xa.at = ap->ap_ata[target];
2950 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
2951 bzero(fis, sizeof(*fis));
2952 fis->type = ATA_FIS_TYPE_H2D;
2953 fis->flags = ATA_H2D_FLAGS_CMD | target;
2954 fis->command = ATA_C_READ_LOG_EXT;
2955 fis->lba_low = 0x10; /* queued error log page (10h) */
2956 fis->sector_count = 1; /* number of sectors (1) */
2957 fis->sector_count_exp = 0;
2958 fis->lba_mid = 0; /* starting offset */
2959 fis->lba_mid_exp = 0;
2962 cmd_slot = ccb->ccb_cmd_hdr;
2963 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
2965 if (ahci_load_prdt(ccb) != 0) {
2970 ccb->ccb_xa.state = ATA_S_PENDING;
2971 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
2973 ahci_unload_prdt(ccb);
2976 ahci_unload_prdt(ccb);
2979 * Success, extract failed register set and tags from the scratch
2982 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2983 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2984 /* Not queued bit was set - wasn't an NCQ error? */
2985 kprintf("%s: read NCQ error page, but not an NCQ error?\n",
2989 /* Copy back the log record as a D2H register FIS. */
2990 err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
2992 ccb2 = &ap->ap_ccbs[err_slot];
2993 if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
2994 kprintf("%s: read NCQ error page slot=%d\n",
2995 ATANAME(ap, ccb2->ccb_xa.at),
2997 memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
2998 sizeof(struct ata_fis_d2h));
2999 ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3000 ccb2->ccb_xa.rfis.flags = 0;
3002 kprintf("%s: read NCQ error page slot=%d, "
3003 "slot does not match any cmds\n",
3004 ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
3010 ahci_put_err_ccb(ccb);
3011 kprintf("%s: DONE log page target %d err_slot=%d\n",
3012 PORTNAME(ap), target, err_slot);
3017 * Allocate memory for various structures DMAd by hardware. The maximum
3018 * number of segments for these tags is 1 so the DMA memory will have a
3019 * single physical base address.
3021 struct ahci_dmamem *
3022 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3024 struct ahci_dmamem *adm;
3027 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3029 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3030 BUS_DMA_ZERO, &adm->adm_map);
3033 error = bus_dmamap_load(tag, adm->adm_map,
3035 bus_dma_tag_getmaxsize(tag),
3036 ahci_dmamem_saveseg, &adm->adm_busaddr,
3041 bus_dmamap_destroy(tag, adm->adm_map);
3042 adm->adm_map = NULL;
3043 adm->adm_tag = NULL;
3044 adm->adm_kva = NULL;
3046 kfree(adm, M_DEVBUF);
3054 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3056 KKASSERT(error == 0);
3057 KKASSERT(nsegs == 1);
3058 *(bus_addr_t *)info = segs->ds_addr;
3063 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3066 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3067 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3068 adm->adm_map = NULL;
3069 adm->adm_tag = NULL;
3070 adm->adm_kva = NULL;
3072 kfree(adm, M_DEVBUF);
3076 ahci_read(struct ahci_softc *sc, bus_size_t r)
3078 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3079 BUS_SPACE_BARRIER_READ);
3080 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3084 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3086 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3087 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3088 BUS_SPACE_BARRIER_WRITE);
3092 ahci_pread(struct ahci_port *ap, bus_size_t r)
3094 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3095 BUS_SPACE_BARRIER_READ);
3096 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3100 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3102 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3103 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3104 BUS_SPACE_BARRIER_WRITE);
3108 * Wait up to (timeout) milliseconds for the masked port register to
3111 * Timeout is in milliseconds.
3114 ahci_pwait_eq(struct ahci_port *ap, int timeout,
3115 bus_size_t r, u_int32_t mask, u_int32_t target)
3120 * Loop hard up to 100uS
3122 for (t = 0; t < 100; ++t) {
3123 if ((ahci_pread(ap, r) & mask) == target)
3125 ahci_os_hardsleep(1); /* us */
3129 timeout -= ahci_os_softsleep();
3130 if ((ahci_pread(ap, r) & mask) == target)
3132 } while (timeout > 0);
3137 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3143 * Loop hard up to 100uS
3145 for (t = 0; t < 100; ++t) {
3146 if ((ahci_read(sc, r) & mask) != target)
3148 ahci_os_hardsleep(1); /* us */
3152 * And one millisecond the slow way
3156 t -= ahci_os_softsleep();
3157 if ((ahci_read(sc, r) & mask) != target)
3166 * Acquire an ata transfer.
3168 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
3169 * targets that go through the port multiplier.
3172 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3174 struct ahci_ccb *ccb;
3176 ccb = ahci_get_ccb(ap);
3178 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3183 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3184 PORTNAME(ap), ccb->ccb_slot);
3186 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
3187 ccb->ccb_xa.at = at;
3188 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3190 return (&ccb->ccb_xa);
3194 ahci_ata_put_xfer(struct ata_xfer *xa)
3196 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3198 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3204 ahci_ata_cmd(struct ata_xfer *xa)
3206 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3207 struct ahci_cmd_hdr *cmd_slot;
3209 KKASSERT(xa->state == ATA_S_SETUP);
3211 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3213 ccb->ccb_done = ahci_ata_cmd_done;
3215 cmd_slot = ccb->ccb_cmd_hdr;
3216 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
3217 if (ccb->ccb_xa.at) {
3218 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
3219 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
3222 if (xa->flags & ATA_F_WRITE)
3223 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3225 if (xa->flags & ATA_F_PACKET)
3226 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3228 if (ahci_load_prdt(ccb) != 0)
3231 xa->state = ATA_S_PENDING;
3233 if (xa->flags & ATA_F_POLL)
3234 return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3237 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
3238 xa->flags |= ATA_F_TIMEOUT_DESIRED;
3245 xa->state = ATA_S_ERROR;
3248 return (ATA_S_ERROR);
3252 ahci_ata_cmd_done(struct ahci_ccb *ccb)
3254 struct ata_xfer *xa = &ccb->ccb_xa;
3257 * NOTE: callout does not lock port and may race us modifying
3258 * the flags, so make sure its stopped.
3260 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3261 callout_stop(&ccb->ccb_timeout);
3262 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3264 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3266 KKASSERT(xa->state != ATA_S_ONCHIP);
3267 ahci_unload_prdt(ccb);
3269 if (xa->state != ATA_S_TIMEOUT)
3274 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3275 * while the callout is runing.
3277 * We can't safely get the port lock here or delay, we could block
3278 * the callout thread.
3281 ahci_ata_cmd_timeout_unserialized(void *arg)
3283 struct ahci_ccb *ccb = arg;
3284 struct ahci_port *ap = ccb->ccb_port;
3286 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3287 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3288 ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3292 * Timeout code, typically called when the port command processor is running.
3294 * We have to be very very careful here. We cannot stop the port unless
3295 * CR is already clear or the only active commands remaining are timed-out
3296 * ones. Otherwise stopping the port will race the command processor and
3297 * we can lose events. While we can theoretically just restart everything
3298 * that could result in a double-issue which will not work for ATAPI commands.
3301 ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3303 struct ata_xfer *xa = &ccb->ccb_xa;
3304 struct ahci_port *ap = ccb->ccb_port;
3305 struct ata_port *at;
3309 at = ccb->ccb_xa.at;
3311 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
3313 "\tsactive=%08x active=%08x expired=%08x\n"
3314 "\t sact=%08x ci=%08x\n"
3317 ccb->ccb_xa.state, ccb->ccb_slot,
3318 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3319 ap->ap_sactive, ap->ap_active, ap->ap_expired,
3320 ahci_pread(ap, AHCI_PREG_SACT),
3321 ahci_pread(ap, AHCI_PREG_CI),
3322 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
3327 * NOTE: Timeout will not be running if the command was polled.
3328 * If we got here at least one of these flags should be set.
3330 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
3331 ATA_F_TIMEOUT_RUNNING));
3332 xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3334 if (ccb->ccb_xa.state == ATA_S_PENDING) {
3335 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3336 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3339 ahci_issue_pending_commands(ap, NULL);
3342 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3343 kprintf("%s: Unexpected state during timeout: %d\n",
3344 ATANAME(ap, at), ccb->ccb_xa.state);
3349 * Ok, we can only get this command off the chip if CR is inactive
3350 * or if the only commands running on the chip are all expired.
3351 * Otherwise we have to wait until the port is in a safe state.
3353 * Do not set state here, it will cause polls to return when the
3354 * ccb is not yet off the chip.
3356 ap->ap_expired |= 1 << ccb->ccb_slot;
3358 if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
3359 (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
3361 * If using FBSS or NCQ we can't safely stop the port
3364 kprintf("%s: Deferred timeout until its safe, slot %d\n",
3365 ATANAME(ap, at), ccb->ccb_slot);
3370 * We can safely stop the port and process all expired ccb's,
3371 * which will include our current ccb.
3373 ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
3374 ahci_pread(ap, AHCI_PREG_CI);
3375 ahci_port_stop(ap, 0);
3377 while (ap->ap_expired) {
3378 slot = ffs(ap->ap_expired) - 1;
3379 ap->ap_expired &= ~(1 << slot);
3380 ci_saved &= ~(1 << slot);
3381 ccb = &ap->ap_ccbs[slot];
3382 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3383 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
3384 KKASSERT(ap->ap_sactive & (1 << slot));
3385 ap->ap_sactive &= ~(1 << slot);
3387 KKASSERT(ap->ap_active & (1 << slot));
3388 ap->ap_active &= ~(1 << slot);
3389 --ap->ap_active_cnt;
3392 ccb->ccb_xa.complete(&ccb->ccb_xa);
3394 /* ccb invalid now */
3397 * We can safely CLO the port to clear any BSY/DRQ, a case which
3398 * can occur with port multipliers. This will unbrick the port
3399 * and allow commands to other targets behind the PM continue.
3402 * Finally, once the port has been restarted we can issue any
3403 * previously saved pending commands, and run the port interrupt
3404 * code to handle any completions which may have occured when
3407 if (ahci_pread(ap, AHCI_PREG_TFD) &
3408 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
3409 kprintf("%s: Warning, issuing CLO after timeout\n",
3413 ahci_port_start(ap);
3414 ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
3415 ahci_issue_pending_commands(ap, NULL);
3416 ahci_port_intr(ap, 0);
3420 * Issue a previously saved set of commands
3423 ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
3426 KKASSERT(!((ap->ap_active & ci_saved) &&
3427 (ap->ap_sactive & ci_saved)));
3428 KKASSERT((ci_saved & ap->ap_expired) == 0);
3429 if (ap->ap_sactive & ci_saved)
3430 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
3431 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3436 * Used by the softreset, pmprobe, and read_ncq_error only, in very
3437 * specialized, controlled circumstances.
3439 * Only one command may be pending.
3442 ahci_quick_timeout(struct ahci_ccb *ccb)
3444 struct ahci_port *ap = ccb->ccb_port;
3446 switch (ccb->ccb_xa.state) {
3448 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3449 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3452 KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3453 ap->ap_sactive == 0);
3454 ahci_port_stop(ap, 0);
3455 ahci_port_start(ap);
3457 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3458 ap->ap_active &= ~(1 << ccb->ccb_slot);
3459 KKASSERT(ap->ap_active_cnt > 0);
3460 --ap->ap_active_cnt;
3463 panic("%s: ahci_quick_timeout: ccb in bad state %d",
3464 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3469 ahci_dummy_done(struct ata_xfer *xa)
3474 ahci_empty_done(struct ahci_ccb *ccb)
3479 ahci_set_feature(struct ahci_port *ap, struct ata_port *atx, int feature, int enable)
3481 struct ata_port *at;
3482 struct ata_xfer *xa;
3485 at = atx ? atx : ap->ap_ata[0];
3487 xa = ahci_ata_get_xfer(ap, atx);
3489 xa->fis->type = ATA_FIS_TYPE_H2D;
3490 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
3491 xa->fis->command = ATA_C_SET_FEATURES;
3492 xa->fis->features = enable ? ATA_C_SATA_FEATURE_ENA :
3493 ATA_C_SATA_FEATURE_DIS;
3494 xa->fis->sector_count = feature;
3495 xa->fis->control = ATA_FIS_CONTROL_4BIT;
3497 xa->complete = ahci_dummy_done;
3499 xa->flags = ATA_F_POLL;
3502 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
3506 ahci_ata_put_xfer(xa);