fix some flag usage in mmakefiles. provide suitable names for amiga hardware devices...
[AROS.git] / rom / devs / ahci / ahci.c
blobe814990f189031c1deb9ea0ce932605a6015d83c
1 /*
2 * (MPSAFE)
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
26 * are met:
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
33 * distribution.
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
49 * SUCH DAMAGE.
51 * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
54 #include "ahci.h"
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);
84 static u_int32_t ahci_pactive(struct ahci_port *ap);
87 * Initialize the global AHCI hardware. This code does not set up any of
88 * its ports.
90 int
91 ahci_init(struct ahci_softc *sc)
93 u_int32_t pi;
94 u_int32_t pleft;
95 u_int32_t bios_cap, vers;
96 int i;
97 struct ahci_port *ap;
98 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
100 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
101 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
104 * AHCI version.
106 vers = ahci_read(sc, AHCI_REG_VS);
107 (void)vers; // Unused (for now: jmcmullan)
110 * save BIOS initialised parameters, enable staggered spin up
112 bios_cap = ahci_read(sc, AHCI_REG_CAP);
113 bios_cap &= AHCI_REG_CAP_SMPS | AHCI_REG_CAP_SSS;
115 pi = ahci_read(sc, AHCI_REG_PI);
118 * Unconditionally reset the controller, do not conditionalize on
119 * trying to figure it if it was previously active or not.
121 * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section
122 * 5.2.2.1 regarding this. HR should be set to 1 only after
123 * AE is set to 1. The reset sequence will clear HR when
124 * it completes, and will also clear AE if SAM is 0. AE must
125 * then be set again. When SAM is 1 the AE bit typically reads
126 * as 1 (and is read-only).
128 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
129 * otherwise the writes will only be separated by a few
130 * nanoseconds.
132 * NOTE BRICKS (1)
134 * If you have a port multiplier and it does not have a device
135 * in target 0, and it probes normally, but a later operation
136 * mis-probes a target behind that PM, it is possible for the
137 * port to brick such that only (a) a power cycle of the host
138 * or (b) placing a device in target 0 will fix the problem.
139 * Power cycling the PM has no effect (it works fine on another
140 * host port). This issue is unrelated to CLO.
143 * Wait for any prior reset sequence to complete
145 if (ahci_wait_ne(sc, AHCI_REG_GHC,
146 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
147 device_printf(sc->sc_dev, "Controller is stuck in reset\n");
148 return (1);
150 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
151 ahci_os_sleep(500);
152 ahci_read(sc, AHCI_REG_GHC); /* flush */
153 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
154 ahci_os_sleep(500);
155 ahci_read(sc, AHCI_REG_GHC); /* flush */
156 if (ahci_wait_ne(sc, AHCI_REG_GHC,
157 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
158 device_printf(sc->sc_dev, "unable to reset controller\n");
159 return (1);
161 if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
162 device_printf(sc->sc_dev, "AE did not auto-clear!\n");
163 ahci_write(sc, AHCI_REG_GHC, 0);
164 ahci_os_sleep(500);
168 * Enable ahci (global interrupts disabled)
170 * Restore saved parameters. Avoid pci transaction burst write
171 * by issuing dummy reads.
173 ahci_os_sleep(500);
174 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
175 ahci_os_sleep(500);
177 ahci_read(sc, AHCI_REG_GHC); /* flush */
179 bios_cap |= AHCI_REG_CAP_SSS;
180 ahci_write(sc, AHCI_REG_CAP, ahci_read(sc, AHCI_REG_CAP) | bios_cap);
181 ahci_write(sc, AHCI_REG_PI, pi);
182 ahci_read(sc, AHCI_REG_GHC); /* flush */
185 * Intel hocus pocus in case the BIOS has not set the chip up
186 * properly for AHCI operation.
188 if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
189 if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
190 device_printf(sc->sc_dev, "Intel hocus pocus\n");
191 pci_write_config(sc->sc_dev, 0x92,
192 pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
196 * This is a hack that currently does not appear to have
197 * a significant effect, but I noticed the port registers
198 * do not appear to be completely cleared after the host
199 * controller is reset.
201 * Use a temporary ap structure so we can call ahci_pwrite().
203 * We must be sure to stop the port
205 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
206 ap->ap_sc = sc;
207 pleft = pi;
208 for (i = 0; i < AHCI_MAX_PORTS; ++i) {
209 if (pleft == 0)
210 break;
211 if ((pi & (1 << i)) == 0)
212 continue;
213 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
214 AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
215 device_printf(sc->sc_dev, "can't map port\n");
216 return (1);
219 * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
220 * AHCI1.1 can brick the chipset. Not only brick it,
221 * but also crash the PC. The bit seems unreliable
222 * on AHCI1.2 as well.
224 ahci_port_stop(ap, 1);
225 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
226 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
227 ahci_pwrite(ap, AHCI_PREG_IE, 0);
228 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
229 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
230 ahci_pwrite(ap, AHCI_PREG_IS, -1);
231 sc->sc_portmask |= (1 << i);
232 pleft &= ~(1 << i);
234 sc->sc_numports = i;
235 kfree(ap, M_DEVBUF);
237 return (0);
241 * Allocate and initialize an AHCI port.
244 ahci_port_alloc(struct ahci_softc *sc, u_int port)
246 struct ahci_port *ap;
247 struct ata_port *at;
248 struct ahci_ccb *ccb;
249 u_int64_t dva;
250 u_int32_t cmd;
251 u_int32_t data;
252 struct ahci_cmd_hdr *hdr;
253 struct ahci_cmd_table *table;
254 int rc = ENOMEM;
255 int error;
256 int i;
257 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
259 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
260 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
262 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%02d",
263 device_get_name(sc->sc_dev),
264 device_get_unit(sc->sc_dev),
265 port);
266 sc->sc_ports[port] = ap;
269 * Allocate enough so we never have to reallocate, it makes
270 * it easier.
272 * ap_pmcount will be reduced by the scan if we encounter the
273 * port multiplier port prior to target 15.
275 * kmalloc power-of-2 allocations are guaranteed not to cross
276 * a page boundary. Make sure the identify sub-structure in the
277 * at structure does not cross a page boundary, just in case the
278 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
280 if (ap->ap_ata[0] == NULL) {
281 int pw2;
283 for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
285 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
286 at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
287 ap->ap_ata[i] = at;
288 at->at_ahci_port = ap;
289 at->at_target = i;
290 at->at_probe = ATA_PROBE_NEED_INIT;
291 at->at_features |= ATA_PORT_F_RESCAN;
292 ksnprintf(at->at_name, sizeof(at->at_name),
293 "%s.%d", PORTNAME(ap), i);
296 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
297 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
298 device_printf(sc->sc_dev,
299 "unable to create register window for port %d\n",
300 port);
301 goto freeport;
304 ap->ap_sc = sc;
305 ap->ap_num = port;
306 ap->ap_probe = ATA_PROBE_NEED_INIT;
307 ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE;
308 ap->sysctl_tree = NULL;
309 TAILQ_INIT(&ap->ap_ccb_free);
310 TAILQ_INIT(&ap->ap_ccb_pending);
311 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
313 /* Disable port interrupts */
314 ahci_pwrite(ap, AHCI_PREG_IE, 0);
315 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
318 * Sec 10.1.2 - deinitialise port if it is already running
320 cmd = ahci_pread(ap, AHCI_PREG_CMD);
321 kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD);
323 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
324 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
325 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
326 int r;
328 r = ahci_port_stop(ap, 1);
329 if (r) {
330 device_printf(sc->sc_dev,
331 "unable to disable %s, ignoring port %d\n",
332 ((r == 2) ? "CR" : "FR"), port);
333 rc = ENXIO;
334 goto freeport;
337 /* Write DET to zero */
338 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
341 /* Allocate RFIS */
342 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
343 if (ap->ap_dmamem_rfis == NULL) {
344 kprintf("%s: NORFIS\n", PORTNAME(ap));
345 goto nomem;
348 /* Setup RFIS base address */
349 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
350 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
351 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
352 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
354 /* Clear SERR before starting FIS reception or ST or anything */
355 ahci_flush_tfd(ap);
356 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
358 /* Enable FIS reception and activate port. */
359 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
360 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
361 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
362 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
364 /* Check whether port activated. Skip it if not. */
365 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
366 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
367 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
368 rc = ENXIO;
369 goto freeport;
372 /* Allocate a CCB for each command slot */
373 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
374 M_WAITOK | M_ZERO);
375 if (ap->ap_ccbs == NULL) {
376 device_printf(sc->sc_dev,
377 "unable to allocate command list for port %d\n",
378 port);
379 goto freeport;
382 /* Command List Structures and Command Tables */
383 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
384 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
385 if (ap->ap_dmamem_cmd_table == NULL ||
386 ap->ap_dmamem_cmd_list == NULL) {
387 nomem:
388 device_printf(sc->sc_dev,
389 "unable to allocate DMA memory for port %d\n",
390 port);
391 goto freeport;
394 /* Setup command list base address */
395 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
396 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
397 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
399 /* Split CCB allocation into CCBs and assign to command header/table */
400 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
401 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
402 for (i = 0; i < sc->sc_ncmds; i++) {
403 ccb = &ap->ap_ccbs[i];
405 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
406 &ccb->ccb_dmamap);
407 if (error) {
408 device_printf(sc->sc_dev,
409 "unable to create dmamap for port %d "
410 "ccb %d\n", port, i);
411 goto freeport;
414 callout_init(&ccb->ccb_timeout);
415 ccb->ccb_slot = i;
416 ccb->ccb_port = ap;
417 ccb->ccb_cmd_hdr = &hdr[i];
418 ccb->ccb_cmd_table = &table[i];
419 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
420 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
421 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
422 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
424 ccb->ccb_xa.fis =
425 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
426 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
427 ccb->ccb_xa.tag = i;
429 ccb->ccb_xa.state = ATA_S_COMPLETE;
432 * CCB[1] is the error CCB and is not get or put. It is
433 * also used for probing. Numerous HBAs only load the
434 * signature from CCB[1] so it MUST be used for the second
435 * FIS.
437 if (i == 1)
438 ap->ap_err_ccb = ccb;
439 else
440 ahci_put_ccb(ccb);
444 * Wait for ICC change to complete
446 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
449 * Calculate the interrupt mask
451 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
452 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
453 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
454 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
455 AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
456 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
457 data |= AHCI_PREG_IE_IPME;
458 #ifdef AHCI_COALESCE
459 if (sc->sc_ccc_ports & (1 << port))
460 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
461 #endif
462 ap->ap_intmask = data;
465 * Start the port helper thread. The helper thread will call
466 * ahci_port_init() so the ports can all be started in parallel.
467 * A failure by ahci_port_init() does not deallocate the port
468 * since we still want hot-plug events.
470 ahci_os_start_port(ap);
471 return(0);
472 freeport:
473 ahci_port_free(sc, port);
474 return (rc);
478 * [re]initialize an idle port. No CCBs should be active. (from port thread)
480 * This function is called during the initial port allocation sequence
481 * and is also called on hot-plug insertion. We take no chances and
482 * use a portreset instead of a softreset.
484 * This function is the only way to move a failed port back to active
485 * status.
487 * Returns 0 if a device is successfully detected.
490 ahci_port_init(struct ahci_port *ap)
492 u_int32_t cmd;
493 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
496 * Register [re]initialization
498 * Flush the TFD and SERR and make sure the port is stopped before
499 * enabling its interrupt. We no longer cycle the port start as
500 * the port should not be started unless a device is present.
502 * XXX should we enable FIS reception? (FRE)?
504 ahci_pwrite(ap, AHCI_PREG_IE, 0);
505 ahci_port_stop(ap, 0);
506 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
507 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
508 ahci_flush_tfd(ap);
509 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
512 * If we are being harsh try to kill the port completely. Normally
513 * we would want to hold on to some of the state the BIOS may have
514 * set, such as SUD (spin up device).
516 * AP_F_HARSH_REINIT is cleared in the hard reset state
518 if (ap->ap_flags & AP_F_HARSH_REINIT) {
519 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
520 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
522 ahci_os_sleep(1000);
524 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
525 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
526 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD |
527 AHCI_PREG_CMD_SUD;
528 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
529 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
530 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
531 kprintf("%s: Warning: FRE did not come up during "
532 "harsh reinitialization\n",
533 PORTNAME(ap));
535 ahci_os_sleep(1000);
539 * Clear any pending garbage and re-enable the interrupt before
540 * going to the next stage.
542 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
543 ap->ap_pmcount = 0;
545 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
546 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
547 ahci_flush_tfd(ap);
548 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
549 ahci_pwrite(ap, AHCI_PREG_IS, -1);
551 ahci_port_interrupt_enable(ap);
553 return (0);
557 * Enable or re-enable interrupts on a port.
559 * This routine is called from the port initialization code or from the
560 * helper thread as the real interrupt may be forced to turn off certain
561 * interrupt sources.
563 void
564 ahci_port_interrupt_enable(struct ahci_port *ap)
566 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
567 ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
571 * Manage the agressive link power management capability.
573 void
574 ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
576 u_int32_t cmd, sctl;
577 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
579 if (link_pwr_mgmt == ap->link_pwr_mgmt)
580 return;
582 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
583 kprintf("%s: link power management not supported.\n",
584 PORTNAME(ap));
585 return;
588 ahci_os_lock_port(ap);
590 if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
591 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
592 kprintf("%s: enabling aggressive link power management.\n",
593 PORTNAME(ap));
595 ap->link_pwr_mgmt = link_pwr_mgmt;
597 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
598 ahci_port_interrupt_enable(ap);
600 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
601 sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED);
602 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
605 * Enable device initiated link power management for
606 * directly attached devices that support it.
608 if (ap->ap_type != ATA_PORT_T_PM &&
609 ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) {
610 if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
611 kprintf("%s: Could not enable device initiated "
612 "link power management.\n",
613 PORTNAME(ap));
616 cmd = ahci_pread(ap, AHCI_PREG_CMD);
617 cmd |= AHCI_PREG_CMD_ASP;
618 cmd |= AHCI_PREG_CMD_ALPE;
619 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
621 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
622 (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
623 kprintf("%s: enabling medium link power management.\n",
624 PORTNAME(ap));
626 ap->link_pwr_mgmt = link_pwr_mgmt;
628 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
629 ahci_port_interrupt_enable(ap);
631 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
632 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
633 sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL;
634 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
636 cmd = ahci_pread(ap, AHCI_PREG_CMD);
637 cmd &= ~AHCI_PREG_CMD_ASP;
638 cmd |= AHCI_PREG_CMD_ALPE;
639 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
641 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
642 kprintf("%s: disabling link power management.\n",
643 PORTNAME(ap));
645 /* Disable device initiated link power management */
646 if (ap->ap_type != ATA_PORT_T_PM &&
647 ap->ap_ata[0]->at_identify.satafsup & (1 << 3))
648 ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
650 cmd = ahci_pread(ap, AHCI_PREG_CMD);
651 cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
652 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
654 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
655 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
656 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
658 /* let the drive come back to avoid PRCS interrupts later */
659 ahci_os_unlock_port(ap);
660 ahci_os_sleep(1000);
661 ahci_os_lock_port(ap);
663 ahci_pwrite(ap, AHCI_PREG_SERR,
664 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
665 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
667 ap->ap_intmask |= AHCI_PREG_IE_PRCE;
668 ahci_port_interrupt_enable(ap);
670 ap->link_pwr_mgmt = link_pwr_mgmt;
671 } else {
672 kprintf("%s: unsupported link power management state %d.\n",
673 PORTNAME(ap), link_pwr_mgmt);
676 ahci_os_unlock_port(ap);
680 * Return current link power state.
683 ahci_port_link_pwr_state(struct ahci_port *ap)
685 uint32_t r;
686 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
688 r = ahci_pread(ap, AHCI_PREG_SSTS);
689 switch (r & SATA_PM_SSTS_IPM) {
690 case SATA_PM_SSTS_IPM_ACTIVE:
691 return 1;
692 case SATA_PM_SSTS_IPM_PARTIAL:
693 return 2;
694 case SATA_PM_SSTS_IPM_SLUMBER:
695 return 3;
696 default:
697 return 0;
702 * Run the port / target state machine from a main context.
704 * The state machine for the port is always run.
706 * If atx is non-NULL run the state machine for a particular target.
707 * If atx is NULL run the state machine for all targets.
709 void
710 ahci_port_state_machine(struct ahci_port *ap, int initial)
712 struct ata_port *at;
713 u_int32_t data;
714 int target;
715 int didsleep;
716 int loop;
717 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
720 * State machine for port. Note that CAM is not yet associated
721 * during the initial parallel probe and the port's probe state
722 * will not get past ATA_PROBE_NEED_IDENT.
725 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
726 kprintf("%s: Waiting 10 seconds on insertion\n",
727 PORTNAME(ap));
728 ahci_os_sleep(10000);
729 initial = 1;
731 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
732 ahci_port_init(ap);
733 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
734 ahci_port_reset(ap, NULL, 1);
735 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
736 ahci_port_reset(ap, NULL, 0);
737 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
738 ahci_cam_probe(ap, NULL);
740 if (ap->ap_type != ATA_PORT_T_PM) {
741 if (ap->ap_probe == ATA_PROBE_FAILED) {
742 ahci_cam_changed(ap, NULL, 0);
743 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
744 ahci_cam_changed(ap, NULL, 1);
746 return;
750 * Port Multiplier state machine.
752 * Get a mask of changed targets and combine with any runnable
753 * states already present.
755 for (loop = 0; ;++loop) {
756 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
757 kprintf("%s: PM unable to read hot-plug bitmap\n",
758 PORTNAME(ap));
759 break;
763 * Do at least one loop, then stop if no more state changes
764 * have occured. The PM might not generate a new
765 * notification until we clear the entire bitmap.
767 if (loop && data == 0)
768 break;
771 * New devices showing up in the bitmap require some spin-up
772 * time before we start probing them. Reset didsleep. The
773 * first new device we detect will sleep before probing.
775 * This only applies to devices whos change bit is set in
776 * the data, and does not apply to the initial boot-time
777 * probe.
779 didsleep = 0;
781 for (target = 0; target < ap->ap_pmcount; ++target) {
782 at = ap->ap_ata[target];
785 * Check the target state for targets behind the PM
786 * which have changed state. This will adjust
787 * at_probe and set ATA_PORT_F_RESCAN
789 * We want to wait at least 10 seconds before probing
790 * a newly inserted device. If the check status
791 * indicates a device is present and in need of a
792 * hard reset, we make sure we have slept before
793 * continuing.
795 * We also need to wait at least 1 second for the
796 * PHY state to change after insertion, if we
797 * haven't already waited the 10 seconds.
799 * NOTE: When pm_check_good finds a good port it
800 * typically starts us in probe state
801 * NEED_HARD_RESET rather than INIT.
803 if (data & (1 << target)) {
804 if (initial == 0 && didsleep == 0)
805 ahci_os_sleep(1000);
806 ahci_pm_check_good(ap, target);
807 if (initial == 0 && didsleep == 0 &&
808 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
810 didsleep = 1;
811 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
812 ahci_os_sleep(10000);
817 * Report hot-plug events before the probe state
818 * really gets hot. Only actual events are reported
819 * here to reduce spew.
821 if (data & (1 << target)) {
822 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
823 switch(at->at_probe) {
824 case ATA_PROBE_NEED_INIT:
825 case ATA_PROBE_NEED_HARD_RESET:
826 kprintf("Device inserted\n");
827 break;
828 case ATA_PROBE_FAILED:
829 kprintf("Device removed\n");
830 break;
831 default:
832 kprintf("Device probe in progress\n");
833 break;
838 * Run through the state machine as necessary if
839 * the port is not marked failed.
841 * The state machine may stop at NEED_IDENT if
842 * CAM is not yet attached.
844 * Acquire exclusive access to the port while we
845 * are doing this. This prevents command-completion
846 * from queueing commands for non-polled targets
847 * inbetween our probe steps. We need to do this
848 * because the reset probes can generate severe PHY
849 * and protocol errors and soft-brick the port.
851 if (at->at_probe != ATA_PROBE_FAILED &&
852 at->at_probe != ATA_PROBE_GOOD) {
853 ahci_beg_exclusive_access(ap, at);
854 if (at->at_probe == ATA_PROBE_NEED_INIT)
855 ahci_pm_port_init(ap, at);
856 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
857 ahci_port_reset(ap, at, 1);
858 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
859 ahci_port_reset(ap, at, 0);
860 if (at->at_probe == ATA_PROBE_NEED_IDENT)
861 ahci_cam_probe(ap, at);
862 ahci_end_exclusive_access(ap, at);
866 * Add or remove from CAM
868 if (at->at_features & ATA_PORT_F_RESCAN) {
869 at->at_features &= ~ATA_PORT_F_RESCAN;
870 if (at->at_probe == ATA_PROBE_FAILED) {
871 ahci_cam_changed(ap, at, 0);
872 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
873 ahci_cam_changed(ap, at, 1);
876 data &= ~(1 << target);
878 if (data) {
879 kprintf("%s: WARNING (PM): extra bits set in "
880 "EINFO: %08x\n", PORTNAME(ap), data);
881 while (target < AHCI_MAX_PMPORTS) {
882 ahci_pm_check_good(ap, target);
883 ++target;
891 * De-initialize and detach a port.
893 void
894 ahci_port_free(struct ahci_softc *sc, u_int port)
896 struct ahci_port *ap = sc->sc_ports[port];
897 struct ahci_ccb *ccb;
898 int i;
899 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
902 * Ensure port is disabled and its interrupts are all flushed.
904 if (ap->ap_sc) {
905 ahci_port_stop(ap, 1);
906 ahci_os_stop_port(ap);
907 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
908 ahci_pwrite(ap, AHCI_PREG_IE, 0);
909 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
910 ahci_write(sc, AHCI_REG_IS, 1 << port);
913 if (ap->ap_ccbs) {
914 while ((ccb = ahci_get_ccb(ap)) != NULL) {
915 if (ccb->ccb_dmamap) {
916 bus_dmamap_destroy(sc->sc_tag_data,
917 ccb->ccb_dmamap);
918 ccb->ccb_dmamap = NULL;
921 if ((ccb = ap->ap_err_ccb) != NULL) {
922 if (ccb->ccb_dmamap) {
923 bus_dmamap_destroy(sc->sc_tag_data,
924 ccb->ccb_dmamap);
925 ccb->ccb_dmamap = NULL;
927 ap->ap_err_ccb = NULL;
929 kfree(ap->ap_ccbs, M_DEVBUF);
930 ap->ap_ccbs = NULL;
933 if (ap->ap_dmamem_cmd_list) {
934 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
935 ap->ap_dmamem_cmd_list = NULL;
937 if (ap->ap_dmamem_rfis) {
938 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
939 ap->ap_dmamem_rfis = NULL;
941 if (ap->ap_dmamem_cmd_table) {
942 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
943 ap->ap_dmamem_cmd_table = NULL;
945 if (ap->ap_ata) {
946 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
947 if (ap->ap_ata[i]) {
948 kfree(ap->ap_ata[i], M_DEVBUF);
949 ap->ap_ata[i] = NULL;
953 if (ap->ap_err_scratch) {
954 kfree(ap->ap_err_scratch, M_DEVBUF);
955 ap->ap_err_scratch = NULL;
958 /* bus_space(9) says we dont free the subregions handle */
960 kfree(ap, M_DEVBUF);
961 sc->sc_ports[port] = NULL;
964 static
965 u_int32_t
966 ahci_pactive(struct ahci_port *ap)
968 u_int32_t mask;
969 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
971 mask = ahci_pread(ap, AHCI_PREG_CI);
972 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ)
973 mask |= ahci_pread(ap, AHCI_PREG_SACT);
974 return(mask);
978 * Start high-level command processing on the port
981 ahci_port_start(struct ahci_port *ap)
983 u_int32_t r, s, is, tfd;
984 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
987 * FRE must be turned on before ST. Wait for FR to go active
988 * before turning on ST. The spec doesn't seem to think this
989 * is necessary but waiting here avoids an on-off race in the
990 * ahci_port_stop() code.
992 r = ahci_pread(ap, AHCI_PREG_CMD);
993 if ((r & AHCI_PREG_CMD_FRE) == 0) {
994 r |= AHCI_PREG_CMD_FRE;
995 ahci_pwrite(ap, AHCI_PREG_CMD, r);
997 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
998 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
999 kprintf("%s: Cannot start FIS reception\n",
1000 PORTNAME(ap));
1001 return (2);
1003 } else {
1004 ahci_os_sleep(10);
1008 * Turn on ST, wait for CR to come up.
1010 r |= AHCI_PREG_CMD_ST;
1011 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1012 if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1013 s = ahci_pread(ap, AHCI_PREG_SERR);
1014 is = ahci_pread(ap, AHCI_PREG_IS);
1015 tfd = ahci_pread(ap, AHCI_PREG_TFD);
1016 kprintf("%s: Cannot start command DMA\n"
1017 "NCMP=%b NSERR=%b\n"
1018 "NEWIS=%b\n"
1019 "NEWTFD=%b\n",
1020 PORTNAME(ap),
1021 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
1022 is, AHCI_PFMT_IS,
1023 tfd, AHCI_PFMT_TFD_STS);
1024 return (1);
1027 #ifdef AHCI_COALESCE
1029 * (Re-)enable coalescing on the port.
1031 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1032 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
1033 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1034 ap->ap_sc->sc_ccc_ports_cur);
1036 #endif
1038 return (0);
1042 * Stop high-level command processing on a port
1044 * WARNING! If the port is stopped while CR is still active our saved
1045 * CI/SACT will race any commands completed by the command
1046 * processor prior to being able to stop. Thus we never call
1047 * this function unless we intend to dispose of any remaining
1048 * active commands. In particular, this complicates the timeout
1049 * code.
1052 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
1054 u_int32_t r;
1055 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1057 #ifdef AHCI_COALESCE
1059 * Disable coalescing on the port while it is stopped.
1061 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1062 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
1063 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1064 ap->ap_sc->sc_ccc_ports_cur);
1066 #endif
1069 * Turn off ST, then wait for CR to go off.
1071 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1072 r &= ~AHCI_PREG_CMD_ST;
1073 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1075 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1076 kprintf("%s: Port bricked, unable to stop (ST)\n",
1077 PORTNAME(ap));
1078 return (1);
1081 #if 0
1083 * Turn off FRE, then wait for FR to go off. FRE cannot
1084 * be turned off until CR transitions to 0.
1086 if ((r & AHCI_PREG_CMD_FR) == 0) {
1087 kprintf("%s: FR stopped, clear FRE for next start\n",
1088 PORTNAME(ap));
1089 stop_fis_rx = 2;
1091 #endif
1092 if (stop_fis_rx) {
1093 r &= ~AHCI_PREG_CMD_FRE;
1094 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1095 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
1096 kprintf("%s: Port bricked, unable to stop (FRE)\n",
1097 PORTNAME(ap));
1098 return (2);
1102 return (0);
1106 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1109 ahci_port_clo(struct ahci_port *ap)
1111 struct ahci_softc *sc = ap->ap_sc;
1112 u_int32_t cmd;
1113 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1115 /* Only attempt CLO if supported by controller */
1116 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
1117 return (1);
1119 /* Issue CLO */
1120 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1121 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1123 /* Wait for completion */
1124 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1125 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1126 return (1);
1129 return (0);
1133 * Reset a port.
1135 * If hard is 0 perform a softreset of the port.
1136 * If hard is 1 perform a hard reset of the port.
1138 * If at is non-NULL an indirect port via a port-multiplier is being
1139 * reset, otherwise a direct port is being reset.
1141 * NOTE: Indirect ports can only be soft-reset.
1144 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
1146 int rc;
1147 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1149 if (hard) {
1150 if (at)
1151 rc = ahci_pm_hardreset(ap, at->at_target, hard);
1152 else
1153 rc = ahci_port_hardreset(ap, hard);
1154 } else {
1155 if (at)
1156 rc = ahci_pm_softreset(ap, at->at_target);
1157 else
1158 rc = ahci_port_softreset(ap);
1160 return(rc);
1164 * AHCI soft reset, Section 10.4.1
1166 * (at) will be NULL when soft-resetting a directly-attached device, and
1167 * non-NULL when soft-resetting a device through a port multiplier.
1169 * This function keeps port communications intact and attempts to generate
1170 * a reset to the connected device using device commands.
1173 ahci_port_softreset(struct ahci_port *ap)
1175 struct ahci_ccb *ccb = NULL;
1176 struct ahci_cmd_hdr *cmd_slot;
1177 u_int8_t *fis;
1178 int error;
1179 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1181 error = EIO;
1183 if (bootverbose) {
1184 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
1185 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
1188 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1190 crit_enter();
1191 ap->ap_flags |= AP_F_IN_RESET;
1192 ap->ap_state = AP_S_NORMAL;
1195 * Remember port state in cmd (main to restore start/stop)
1197 * Idle port.
1199 if (ahci_port_stop(ap, 0)) {
1200 kprintf("%s: failed to stop port, cannot softreset\n",
1201 PORTNAME(ap));
1202 goto err;
1206 * Request CLO if device appears hung.
1208 if (ahci_pread(ap, AHCI_PREG_TFD) &
1209 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1210 ahci_port_clo(ap);
1214 * This is an attempt to clear errors so a new signature will
1215 * be latched. It isn't working properly. XXX
1217 ahci_flush_tfd(ap);
1218 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1220 /* Restart port */
1221 if (ahci_port_start(ap)) {
1222 kprintf("%s: failed to start port, cannot softreset\n",
1223 PORTNAME(ap));
1224 goto err;
1227 /* Check whether CLO worked */
1228 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1229 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1230 kprintf("%s: CLO %s, need port reset\n",
1231 PORTNAME(ap),
1232 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1233 ? "failed" : "unsupported");
1234 error = EBUSY;
1235 goto err;
1239 * Prep first D2H command with SRST feature & clear busy/reset flags
1241 * It is unclear which other fields in the FIS are used. Just zero
1242 * everything.
1244 * NOTE! This CCB is used for both the first and second commands.
1245 * The second command must use CCB slot 1 to properly load
1246 * the signature.
1248 ccb = ahci_get_err_ccb(ap);
1249 ccb->ccb_xa.complete = ahci_dummy_done;
1250 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
1251 KKASSERT(ccb->ccb_slot == 1);
1252 ccb->ccb_xa.at = NULL;
1253 cmd_slot = ccb->ccb_cmd_hdr;
1255 fis = ccb->ccb_cmd_table->cfis;
1256 memset(fis, 0, sizeof(ccb->ccb_cmd_table->cfis));
1257 fis[0] = ATA_FIS_TYPE_H2D;
1258 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1260 cmd_slot->prdtl = 0;
1261 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1262 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1263 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1265 ccb->ccb_xa.state = ATA_S_PENDING;
1267 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1268 kprintf("%s: First FIS failed\n", PORTNAME(ap));
1269 goto err;
1273 * WARNING! TIME SENSITIVE SPACE! WARNING!
1275 * The two FISes are supposed to be back to back. Don't issue other
1276 * commands or even delay if we can help it.
1280 * Prep second D2H command to read status and complete reset sequence
1281 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1282 * Rev 2.6 and it is unclear how the second FIS should be set up
1283 * from the AHCI document.
1285 * It is unclear which other fields in the FIS are used. Just zero
1286 * everything.
1288 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
1290 memset(fis, 0, sizeof(ccb->ccb_cmd_table->cfis));
1291 fis[0] = ATA_FIS_TYPE_H2D;
1292 fis[15] = ATA_FIS_CONTROL_4BIT;
1294 cmd_slot->prdtl = 0;
1295 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1297 ccb->ccb_xa.state = ATA_S_PENDING;
1298 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1299 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1300 goto err;
1303 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1304 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1305 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1306 PORTNAME(ap),
1307 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
1308 error = EBUSY;
1309 goto err;
1313 * If the softreset is trying to clear a BSY condition after a
1314 * normal portreset we assign the port type.
1316 * If the softreset is being run first as part of the ccb error
1317 * processing code then report if the device signature changed
1318 * unexpectedly.
1320 ahci_os_sleep(100);
1321 if (ap->ap_type == ATA_PORT_T_NONE) {
1322 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1323 } else {
1324 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1325 kprintf("%s: device signature unexpectedly "
1326 "changed\n", PORTNAME(ap));
1327 error = EBUSY; /* XXX */
1330 error = 0;
1332 ahci_os_sleep(3);
1333 err:
1334 if (ccb != NULL) {
1335 ahci_put_err_ccb(ccb);
1338 * If the target is busy use CLO to clear the busy
1339 * condition. The BSY should be cleared on the next
1340 * start.
1342 if (ahci_pread(ap, AHCI_PREG_TFD) &
1343 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1344 ahci_port_clo(ap);
1349 * If we failed to softreset make the port quiescent, otherwise
1350 * make sure the port's start/stop state matches what it was on
1351 * entry.
1353 * Don't kill the port if the softreset is on a port multiplier
1354 * target, that would kill all the targets!
1356 if (error) {
1357 ahci_port_hardstop(ap);
1358 /* ap_probe set to failed */
1359 } else {
1360 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1361 ap->ap_pmcount = 1;
1362 ahci_port_start(ap);
1364 ap->ap_flags &= ~AP_F_IN_RESET;
1365 crit_exit();
1367 if (bootverbose)
1368 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1370 return (error);
1374 * Issue just do the core COMRESET and basic device detection on a port.
1376 * NOTE: Only called by ahci_port_hardreset().
1378 static int
1379 ahci_comreset(struct ahci_port *ap, int *pmdetectp)
1381 u_int32_t cmd;
1382 u_int32_t r;
1383 int error;
1384 int loop;
1385 int retries = 0;
1386 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1389 * Idle the port,
1391 *pmdetectp = 0;
1392 ahci_port_stop(ap, 0);
1393 ap->ap_state = AP_S_NORMAL;
1394 ahci_os_sleep(10);
1397 * The port may have been quiescent with its SUD bit cleared, so
1398 * set the SUD (spin up device).
1400 * NOTE: I do not know if SUD is a hardware pin/low-level signal
1401 * or if it is messaged.
1403 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1405 cmd &= ~AHCI_PREG_CMD_SUD;
1406 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1407 ahci_os_sleep(100);
1409 cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD;
1410 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1411 ahci_os_sleep(1000);
1414 * Make sure that all power management is disabled.
1416 * NOTE! AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable
1417 * on multiple chipsets and can brick the chipset or even
1418 * the whole PC. Never use it.
1420 ap->ap_type = ATA_PORT_T_NONE;
1422 r = AHCI_PREG_SCTL_IPM_DISABLED |
1423 AHCI_PREG_SCTL_SPM_DISABLED;
1424 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1426 retry:
1428 * Give the new power management state time to settle, then clear
1429 * pending status.
1431 ahci_os_sleep(1000);
1432 ahci_flush_tfd(ap);
1433 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1436 * Start transmitting COMRESET. The spec says that COMRESET must
1437 * be sent for at least 1ms but in actual fact numerous devices
1438 * appear to take much longer. Delay a whole second here.
1440 * In addition, SATA-3 ports can take longer to train, so even
1441 * SATA-2 devices which would normally detect very quickly may
1442 * take longer when plugged into a SATA-3 port.
1444 r |= AHCI_PREG_SCTL_DET_INIT;
1445 switch(AhciForceGen) {
1446 case 0:
1447 r |= AHCI_PREG_SCTL_SPD_ANY;
1448 break;
1449 case 1:
1450 r |= AHCI_PREG_SCTL_SPD_GEN1;
1451 break;
1452 case 2:
1453 r |= AHCI_PREG_SCTL_SPD_GEN2;
1454 break;
1455 case 3:
1456 r |= AHCI_PREG_SCTL_SPD_GEN3;
1457 break;
1458 default:
1459 r |= AHCI_PREG_SCTL_SPD_GEN3;
1460 break;
1462 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1463 ahci_os_sleep(1000);
1465 ap->ap_flags &= ~AP_F_HARSH_REINIT;
1468 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1469 * since we are hard-resetting the port we might as well clear
1470 * the whole enchillada. Also be sure to clear any spurious BSY
1471 * prior to clearing INIT.
1473 * Wait 1 whole second after clearing INIT before checking
1474 * the device detection bits in an attempt to work around chipsets
1475 * which do not properly mask PCS/PRCS during low level init.
1477 ahci_flush_tfd(ap);
1478 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1479 /* ahci_port_clo(ap);*/
1480 ahci_os_sleep(10);
1482 r &= ~AHCI_PREG_SCTL_SPD;
1483 r &= ~AHCI_PREG_SCTL_DET_INIT;
1484 r |= AHCI_PREG_SCTL_DET_NONE;
1485 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1486 ahci_os_sleep(1000);
1489 * Try to determine if there is a device on the port.
1491 * Give the device 3/10 second to at least be detected.
1492 * If we fail clear PRCS (phy detect) since we may cycled
1493 * the phy and probably caused another PRCS interrupt.
1495 loop = 300;
1496 while (loop > 0) {
1497 r = ahci_pread(ap, AHCI_PREG_SSTS);
1498 if (r & AHCI_PREG_SSTS_DET)
1499 break;
1500 loop -= ahci_os_softsleep();
1502 if (loop == 0) {
1503 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1504 if (bootverbose) {
1505 kprintf("%s: Port appears to be unplugged\n",
1506 PORTNAME(ap));
1508 error = ENODEV;
1509 goto done;
1513 * There is something on the port. Regardless of what happens
1514 * after this tell the caller to try to detect a port multiplier.
1516 * Give the device 3 seconds to fully negotiate.
1518 *pmdetectp = 1;
1520 if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1521 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1522 if (bootverbose) {
1523 kprintf("%s: Device may be powered down\n",
1524 PORTNAME(ap));
1526 error = ENODEV;
1527 goto done;
1531 * We got something that definitely looks like a device. Give
1532 * the device time to send us its first D2H FIS. Waiting for
1533 * BSY to clear accomplishes this.
1535 * NOTE: A port multiplier may or may not clear BSY here,
1536 * depending on what is sitting in target 0 behind it.
1538 * NOTE: Intel SSDs seem to have compatibility problems with Intel
1539 * mobo's on cold boots and may leave BSY set. A single
1540 * retry works around the problem. This is definitely a bug
1541 * with the mobo and/or the SSD and does not appear to occur
1542 * with other devices connected to the same port.
1544 ahci_flush_tfd(ap);
1545 if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD,
1546 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1547 kprintf("%s: Device BUSY: %b\n",
1548 PORTNAME(ap),
1549 ahci_pread(ap, AHCI_PREG_TFD),
1550 AHCI_PFMT_TFD_STS);
1551 if (retries == 0) {
1552 kprintf("%s: Retrying\n", PORTNAME(ap));
1553 retries = 1;
1554 goto retry;
1556 error = EBUSY;
1557 } else {
1558 error = 0;
1561 done:
1562 ahci_flush_tfd(ap);
1563 return error;
1568 * AHCI port reset, Section 10.4.2
1570 * This function does a hard reset of the port. Note that the device
1571 * connected to the port could still end-up hung.
1574 ahci_port_hardreset(struct ahci_port *ap, int hard)
1576 u_int32_t data;
1577 int error;
1578 int pmdetect;
1579 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1581 if (bootverbose)
1582 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1583 ap->ap_flags |= AP_F_IN_RESET;
1585 error = ahci_comreset(ap, &pmdetect);
1588 * We may be asked to perform a port multiplier check even if the
1589 * comreset failed. This typically occurs when the PM has nothing
1590 * in slot 0, which can cause BSY to remain set.
1592 * If the PM detection is successful it will override (error),
1593 * otherwise (error) is retained. If an error does occur it
1594 * is possible that a normal device has blown up on us DUE to
1595 * the PM detection code, so re-run the comreset and assume
1596 * a normal device.
1598 if (pmdetect) {
1599 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) {
1600 error = ahci_pm_port_probe(ap, error);
1601 if (error) {
1602 error = ahci_comreset(ap, &pmdetect);
1608 * Finish up.
1610 ahci_os_sleep(500);
1612 switch(error) {
1613 case 0:
1615 * All good, make sure the port is running and set the
1616 * probe state. Ignore the signature junk (it's unreliable)
1617 * until we get to the softreset code.
1619 if (ahci_port_start(ap)) {
1620 kprintf("%s: failed to start command DMA on port, "
1621 "disabling\n", PORTNAME(ap));
1622 error = EBUSY;
1623 break;
1625 if (ap->ap_type == ATA_PORT_T_PM)
1626 ap->ap_probe = ATA_PROBE_GOOD;
1627 else
1628 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1629 break;
1630 case ENODEV:
1632 * Normal device probe failure
1634 data = ahci_pread(ap, AHCI_PREG_SSTS);
1636 switch(data & AHCI_PREG_SSTS_DET) {
1637 case AHCI_PREG_SSTS_DET_DEV_NE:
1638 kprintf("%s: Device not communicating\n",
1639 PORTNAME(ap));
1640 break;
1641 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
1642 kprintf("%s: PHY offline\n",
1643 PORTNAME(ap));
1644 break;
1645 default:
1646 kprintf("%s: No device detected\n",
1647 PORTNAME(ap));
1648 break;
1650 ahci_port_hardstop(ap);
1651 break;
1652 default:
1654 * Abnormal probe (EBUSY)
1656 kprintf("%s: Device on port is bricked\n",
1657 PORTNAME(ap));
1658 ahci_port_hardstop(ap);
1659 #if 0
1660 rc = ahci_port_reset(ap, atx, 0);
1661 if (rc) {
1662 kprintf("%s: Unable unbrick device\n",
1663 PORTNAME(ap));
1664 } else {
1665 kprintf("%s: Successfully unbricked\n",
1666 PORTNAME(ap));
1668 #endif
1669 break;
1673 * Clean up
1675 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1676 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
1678 ap->ap_flags &= ~AP_F_IN_RESET;
1680 if (bootverbose)
1681 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1682 return (error);
1686 * Hard-stop on hot-swap device removal. See 10.10.1
1688 * Place the port in a mode that will allow it to detect hot-swap insertions.
1689 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1690 * seem to do the job.
1692 * FIS reception is left enabled but command processing is disabled.
1693 * Cycling FIS reception (FRE) can brick ports.
1695 void
1696 ahci_port_hardstop(struct ahci_port *ap)
1698 struct ahci_ccb *ccb;
1699 struct ata_port *at;
1700 u_int32_t r;
1701 u_int32_t cmd;
1702 int slot;
1703 int i;
1704 int serial;
1705 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1708 * Stop the port. We can't modify things like SUD if the port
1709 * is running.
1711 ap->ap_state = AP_S_FATAL_ERROR;
1712 ap->ap_probe = ATA_PROBE_FAILED;
1713 ap->ap_type = ATA_PORT_T_NONE;
1714 ahci_port_stop(ap, 0);
1715 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1716 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC);
1717 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1720 * Clean up AT sub-ports on SATA port.
1722 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1723 at = ap->ap_ata[i];
1724 at->at_type = ATA_PORT_T_NONE;
1725 at->at_probe = ATA_PROBE_FAILED;
1729 * Make sure FRE is active. There isn't anything we can do if it
1730 * fails so just ignore errors.
1732 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1733 cmd |= AHCI_PREG_CMD_FRE;
1734 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1735 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1736 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1740 * 10.10.1 place us in the Listen state.
1742 * 10.10.3 DET must be set to 0 and found to be 0 before
1743 * setting SUD to 0.
1745 * Deactivating SUD only applies if the controller supports SUD, it
1746 * is a bit unclear what happens w/regards to detecting hotplug
1747 * if it doesn't.
1749 r = AHCI_PREG_SCTL_IPM_DISABLED |
1750 AHCI_PREG_SCTL_SPM_DISABLED;
1751 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1752 ahci_os_sleep(10);
1753 cmd &= ~AHCI_PREG_CMD_SUD;
1754 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1755 ahci_os_sleep(10);
1758 * 10.10.1
1760 * Transition su to the spin-up state. HBA shall send COMRESET and
1761 * begin initialization sequence (whatever that means). Presumably
1762 * this is edge-triggered. Following the spin-up state the HBA
1763 * will automatically transition to the Normal state.
1765 * This only applies if the controller supports SUD.
1766 * NEVER use AHCI_PREG_DET_DISABLE.
1768 cmd |= AHCI_PREG_CMD_POD |
1769 AHCI_PREG_CMD_SUD |
1770 AHCI_PREG_CMD_ICC_ACTIVE;
1771 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1772 ahci_os_sleep(10);
1775 * Flush SERR_DIAG_X so the TFD can update.
1777 ahci_flush_tfd(ap);
1780 * Clean out pending ccbs
1782 restart:
1783 while (ap->ap_active) {
1784 slot = ffs(ap->ap_active) - 1;
1785 ap->ap_active &= ~(1 << slot);
1786 --ap->ap_active_cnt;
1787 ccb = &ap->ap_ccbs[slot];
1788 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1789 serial = ccb->ccb_xa.serial;
1790 callout_stop_sync(&ccb->ccb_timeout);
1791 if (serial != ccb->ccb_xa.serial) {
1792 kprintf("%s: Warning: timeout race ccb %p\n",
1793 PORTNAME(ap), ccb);
1794 goto restart;
1796 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1798 ap->ap_expired &= ~(1 << slot);
1799 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1800 ATA_F_TIMEOUT_EXPIRED);
1801 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1802 ccb->ccb_done(ccb);
1803 ccb->ccb_xa.complete(&ccb->ccb_xa);
1805 while (ap->ap_sactive) {
1806 slot = ffs(ap->ap_sactive) - 1;
1807 ap->ap_sactive &= ~(1 << slot);
1808 ccb = &ap->ap_ccbs[slot];
1809 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1810 serial = ccb->ccb_xa.serial;
1811 callout_stop_sync(&ccb->ccb_timeout);
1812 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1813 if (serial != ccb->ccb_xa.serial) {
1814 kprintf("%s: Warning: timeout race ccb %p\n",
1815 PORTNAME(ap), ccb);
1816 goto restart;
1819 ap->ap_expired &= ~(1 << slot);
1820 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1821 ATA_F_TIMEOUT_EXPIRED);
1822 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1823 ccb->ccb_done(ccb);
1824 ccb->ccb_xa.complete(&ccb->ccb_xa);
1826 KKASSERT(ap->ap_active_cnt == 0);
1828 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1829 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1830 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1831 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1832 ccb->ccb_done(ccb);
1833 ccb->ccb_xa.complete(&ccb->ccb_xa);
1837 * Hot-plug device detection should work at this point. e.g. on
1838 * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug
1839 * detection and entering RESET (continuous COMRESET by setting INIT)
1840 * will actually prevent hot-plug detection from working properly.
1842 * There may be cases where this will fail to work, I have some
1843 * additional code to place the HBA in RESET (send continuous
1844 * COMRESET) and hopefully get DIAG.X or other events when something
1845 * is plugged in. Unfortunately this isn't universal and can
1846 * also prevent events from generating interrupts.
1849 #if 0
1851 * Transition us to the Reset state. Theoretically we send a
1852 * continuous stream of COMRESETs in this state.
1854 r |= AHCI_PREG_SCTL_DET_INIT;
1855 if (AhciForceGen1 & (1 << ap->ap_num)) {
1856 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1857 r |= AHCI_PREG_SCTL_SPD_GEN1;
1858 } else {
1859 r |= AHCI_PREG_SCTL_SPD_ANY;
1861 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1862 ahci_os_sleep(10);
1865 * Flush SERR_DIAG_X so the TFD can update.
1867 ahci_flush_tfd(ap);
1868 #endif
1869 /* NOP */
1873 * We can't loop on the X bit, a continuous COMINIT received will make
1874 * it loop forever. Just assume one event has built up and clear X
1875 * so the task file descriptor can update.
1877 void
1878 ahci_flush_tfd(struct ahci_port *ap)
1880 u_int32_t r;
1881 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1883 r = ahci_pread(ap, AHCI_PREG_SERR);
1884 if (r & AHCI_PREG_SERR_DIAG_X)
1885 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1889 * Figure out what type of device is connected to the port, ATAPI or
1890 * DISK.
1893 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1895 u_int32_t sig;
1896 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1898 sig = ahci_pread(ap, AHCI_PREG_SIG);
1899 if (bootverbose)
1900 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1901 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1902 return(ATA_PORT_T_ATAPI);
1903 } else if ((sig & 0xffff0000) ==
1904 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1905 return(ATA_PORT_T_PM);
1906 } else {
1907 return(ATA_PORT_T_DISK);
1912 * Load the DMA descriptor table for a CCB's buffer.
1915 ahci_load_prdt(struct ahci_ccb *ccb)
1917 struct ahci_port *ap = ccb->ccb_port;
1918 struct ahci_softc *sc = ap->ap_sc;
1919 struct ata_xfer *xa = &ccb->ccb_xa;
1920 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1921 bus_dmamap_t dmap = ccb->ccb_dmamap;
1922 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1923 int error;
1924 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1926 if (xa->datalen == 0) {
1927 ccb->ccb_cmd_hdr->prdtl = 0;
1928 return (0);
1931 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1932 xa->data, xa->datalen,
1933 ahci_load_prdt_callback,
1934 &prdt,
1935 ((xa->flags & ATA_F_NOWAIT) ?
1936 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1937 if (error != 0) {
1938 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1939 return (1);
1941 #if 0
1942 if (xa->flags & ATA_F_PIO)
1943 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1944 #endif
1946 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1948 if (xa->flags & ATA_F_READ)
1949 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1950 if (xa->flags & ATA_F_WRITE)
1951 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1953 return (0);
1957 * Callback from BUSDMA system to load the segment list. The passed segment
1958 * list is a temporary structure.
1960 static
1961 void
1962 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1963 int error)
1965 struct ahci_prdt *prd = *(void **)info;
1966 u_int64_t addr;
1967 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1969 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1971 while (nsegs) {
1972 addr = segs->ds_addr;
1973 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1974 prd->dba_lo = htole32((u_int32_t)addr);
1975 prd->flags = htole32(segs->ds_len - 1);
1976 --nsegs;
1977 if (nsegs)
1978 ++prd;
1979 ++segs;
1981 *(void **)info = prd; /* return last valid segment */
1984 void
1985 ahci_unload_prdt(struct ahci_ccb *ccb)
1987 struct ahci_port *ap = ccb->ccb_port;
1988 struct ahci_softc *sc = ap->ap_sc;
1989 struct ata_xfer *xa = &ccb->ccb_xa;
1990 bus_dmamap_t dmap = ccb->ccb_dmamap;
1991 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
1993 if (xa->datalen != 0) {
1994 if (xa->flags & ATA_F_READ) {
1995 bus_dmamap_sync(sc->sc_tag_data, dmap,
1996 BUS_DMASYNC_POSTREAD);
1998 if (xa->flags & ATA_F_WRITE) {
1999 bus_dmamap_sync(sc->sc_tag_data, dmap,
2000 BUS_DMASYNC_POSTWRITE);
2002 bus_dmamap_unload(sc->sc_tag_data, dmap);
2005 * prdbc is only updated by hardware for non-NCQ commands.
2007 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2008 xa->resid = 0;
2009 } else {
2010 #if 0
2011 if (ccb->ccb_cmd_hdr->prdbc == 0 &&
2012 ccb->ccb_xa.state == ATA_S_COMPLETE) {
2013 kprintf("%s: WARNING! Unload prdbc resid "
2014 "was zero! tag=%d\n",
2015 ATANAME(ap, xa->at), ccb->ccb_slot);
2017 #endif
2018 xa->resid = xa->datalen -
2019 le32toh(ccb->ccb_cmd_hdr->prdbc);
2025 * Start a command and poll for completion.
2027 * timeout is in ms and only counts once the command gets on-chip.
2029 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
2030 * that no error occured.
2032 * NOTE: If the caller specifies a NULL timeout function the caller is
2033 * responsible for clearing hardware state on failure, but we will
2034 * deal with removing the ccb from any pending queue.
2036 * NOTE: NCQ should never be used with this function.
2038 * NOTE: If the port is in a failed state and stopped we do not try
2039 * to activate the ccb.
2042 ahci_poll(struct ahci_ccb *ccb, int timeout,
2043 void (*timeout_fn)(struct ahci_ccb *))
2045 struct ahci_port *ap = ccb->ccb_port;
2046 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2048 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
2049 ccb->ccb_xa.state = ATA_S_ERROR;
2050 return(ccb->ccb_xa.state);
2052 crit_enter();
2053 #if 0
2054 kprintf("%s: Start command %02x tag=%d\n",
2055 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2056 ccb->ccb_xa.fis->command, ccb->ccb_slot);
2057 #endif
2058 ahci_start(ccb);
2060 do {
2061 ahci_port_intr(ap, 1);
2062 switch(ccb->ccb_xa.state) {
2063 case ATA_S_ONCHIP:
2064 timeout -= ahci_os_softsleep();
2065 break;
2066 case ATA_S_PENDING:
2067 ahci_os_softsleep();
2068 ahci_check_active_timeouts(ap);
2069 break;
2070 default:
2071 crit_exit();
2072 return (ccb->ccb_xa.state);
2074 } while (timeout > 0);
2076 if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) {
2077 kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
2078 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
2079 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2080 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
2081 ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
2084 timeout_fn(ccb);
2086 crit_exit();
2088 return(ccb->ccb_xa.state);
2092 * When polling we have to check if the currently active CCB(s)
2093 * have timed out as the callout will be deadlocked while we
2094 * hold the port lock.
2096 void
2097 ahci_check_active_timeouts(struct ahci_port *ap)
2099 struct ahci_ccb *ccb;
2100 u_int32_t mask;
2101 int tag;
2102 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2104 mask = ap->ap_active | ap->ap_sactive;
2105 while (mask) {
2106 tag = ffs(mask) - 1;
2107 mask &= ~(1 << tag);
2108 ccb = &ap->ap_ccbs[tag];
2109 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2110 ahci_ata_cmd_timeout(ccb);
2115 static
2116 __inline
2117 void
2118 ahci_start_timeout(struct ahci_ccb *ccb)
2120 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2121 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
2122 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
2123 callout_reset(&ccb->ccb_timeout,
2124 ccb->ccb_xa.timeout,
2125 ahci_ata_cmd_timeout_unserialized, ccb);
2129 void
2130 ahci_start(struct ahci_ccb *ccb)
2132 struct ahci_port *ap = ccb->ccb_port;
2133 struct ahci_softc *sc = ap->ap_sc;
2134 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2136 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2138 /* Zero transferred byte count before transfer */
2139 ccb->ccb_cmd_hdr->prdbc = 0;
2141 /* Sync command list entry and corresponding command table entry */
2142 bus_dmamap_sync(sc->sc_tag_cmdh,
2143 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2144 BUS_DMASYNC_PREWRITE);
2145 bus_dmamap_sync(sc->sc_tag_cmdt,
2146 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2147 BUS_DMASYNC_PREWRITE);
2149 /* Prepare RFIS area for write by controller */
2150 bus_dmamap_sync(sc->sc_tag_rfis,
2151 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2152 BUS_DMASYNC_PREREAD);
2155 * There's no point trying to optimize this, it only shaves a few
2156 * nanoseconds so just queue the command and call our generic issue.
2158 ahci_issue_pending_commands(ap, ccb);
2162 * While holding the port lock acquire exclusive access to the port.
2164 * This is used when running the state machine to initialize and identify
2165 * targets over a port multiplier. Setting exclusive access prevents
2166 * ahci_port_intr() from activating any requests sitting on the pending
2167 * queue.
2169 void
2170 ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2172 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2173 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
2174 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
2175 while (ap->ap_active || ap->ap_sactive) {
2176 ahci_port_intr(ap, 1);
2177 ahci_os_softsleep();
2181 void
2182 ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2184 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2185 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2186 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
2187 ahci_issue_pending_commands(ap, NULL);
2191 * If ccb is not NULL enqueue and/or issue it.
2193 * If ccb is NULL issue whatever we can from the queue. However, nothing
2194 * new is issued if the exclusive access flag is set or expired ccb's are
2195 * present.
2197 * If existing commands are still active (ap_active/ap_sactive) we can only
2198 * issue matching new commands.
2200 void
2201 ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
2203 u_int32_t mask;
2204 int limit;
2205 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2208 * Enqueue the ccb.
2210 * If just running the queue and in exclusive access mode we
2211 * just return. Also in this case if there are any expired ccb's
2212 * we want to clear the queue so the port can be safely stopped.
2214 if (ccb) {
2215 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
2216 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
2217 return;
2221 * Pull the next ccb off the queue and run it if possible.
2223 * The error CCB supercedes all normal queue operations and
2224 * implies exclusive access while the error CCB is active.
2226 if (ccb != ap->ap_err_ccb) {
2227 if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
2228 return;
2229 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2230 kprintf("DELAY CCB slot %d\n", ccb->ccb_slot);
2231 return;
2236 * Handle exclusivity requirements.
2238 * ATA_F_EXCLUSIVE is used when we want to be the only command
2239 * running.
2241 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
2242 * back into the ccb on a normal (non-errored) command completion.
2243 * For example, for PM requests to target 15. Because the AHCI
2244 * spec does not stop the command processor and has only one rfis
2245 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
2246 * Otherwise multiple completions can destroy the rfis data before
2247 * we have a chance to copy it.
2249 if (ap->ap_active & ~ap->ap_expired) {
2251 * There may be multiple ccb's already running,
2252 * if any are running and ap_run_flags sets
2253 * one of these flags then we know only one is
2254 * running.
2256 * XXX Current AUTOSENSE code forces exclusivity
2257 * to simplify the code.
2259 if (ap->ap_run_flags &
2260 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2261 return;
2264 if (ccb->ccb_xa.flags &
2265 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2266 return;
2270 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2272 * The next command is a NCQ command and can be issued as
2273 * long as currently active commands are not standard.
2275 if (ap->ap_active) {
2276 KKASSERT(ap->ap_active_cnt > 0);
2277 return;
2279 KKASSERT(ap->ap_active_cnt == 0);
2281 mask = 0;
2282 do {
2283 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2284 KKASSERT((mask & (1 << ccb->ccb_slot)) == 0);
2285 mask |= 1 << ccb->ccb_slot;
2286 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2287 KKASSERT(ccb == &ap->ap_ccbs[ccb->ccb_slot]);
2288 ccb->ccb_xa.state = ATA_S_ONCHIP;
2289 ahci_start_timeout(ccb);
2290 ap->ap_run_flags = ccb->ccb_xa.flags;
2291 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2292 } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
2293 (ap->ap_run_flags &
2294 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
2296 KKASSERT(((ap->ap_active | ap->ap_sactive) & mask) == 0);
2298 ap->ap_sactive |= mask;
2299 ahci_pwrite(ap, AHCI_PREG_SACT, mask);
2300 ahci_pwrite(ap, AHCI_PREG_CI, mask);
2301 } else {
2303 * The next command is a standard command and can be issued
2304 * as long as currently active commands are not NCQ.
2306 * We limit ourself to 1 command if we have a port multiplier,
2307 * (at least without FBSS support), otherwise timeouts on
2308 * one port can race completions on other ports (see
2309 * ahci_ata_cmd_timeout() for more information).
2311 * If not on a port multiplier generally allow up to 4
2312 * standard commands to be enqueued. Remember that the
2313 * command processor will still process them sequentially.
2315 if (ap->ap_sactive)
2316 return;
2317 if (ap->ap_type == ATA_PORT_T_PM)
2318 limit = 1;
2319 else if (ap->ap_sc->sc_ncmds > 4)
2320 limit = 4;
2321 else
2322 limit = 2;
2324 while (ap->ap_active_cnt < limit && ccb &&
2325 (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
2326 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2327 KKASSERT(((ap->ap_active | ap->ap_sactive) &
2328 (1 << ccb->ccb_slot)) == 0);
2329 ap->ap_active |= 1 << ccb->ccb_slot;
2330 ap->ap_active_cnt++;
2331 ap->ap_run_flags = ccb->ccb_xa.flags;
2332 ccb->ccb_xa.state = ATA_S_ONCHIP;
2333 ahci_start_timeout(ccb);
2334 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
2335 if ((ap->ap_run_flags &
2336 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
2337 break;
2339 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2340 if (ccb && (ccb->ccb_xa.flags &
2341 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
2342 break;
2348 void
2349 ahci_intr(void *arg)
2351 struct ahci_softc *sc = arg;
2352 struct ahci_port *ap;
2353 u_int32_t is;
2354 u_int32_t ack;
2355 int port;
2356 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2359 * Check if the master enable is up, and whether any interrupts are
2360 * pending.
2362 if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2363 return;
2364 is = ahci_read(sc, AHCI_REG_IS);
2365 if (is == 0 || is == 0xffffffff) {
2366 return;
2368 is &= sc->sc_portmask;
2370 #ifdef AHCI_COALESCE
2371 /* Check coalescing interrupt first */
2372 if (is & sc->sc_ccc_mask) {
2373 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2374 DEVNAME(sc));
2375 is &= ~sc->sc_ccc_mask;
2376 is |= sc->sc_ccc_ports_cur;
2378 #endif
2381 * Process interrupts for each port in a non-blocking fashion.
2383 * The global IS bit is supposed to be forced on if any unmasked
2384 * port interrupt is pending, even if we clear it.
2386 * However it would appear that it is simply latched on some parts,
2387 * which means we have to clear it BEFORE processing the status bits
2388 * to avoid races.
2390 ahci_write(sc, AHCI_REG_IS, is);
2391 for (ack = 0; is; is &= ~(1 << port)) {
2392 port = ffs(is) - 1;
2393 ack |= 1 << port;
2395 ap = sc->sc_ports[port];
2396 if (ap == NULL)
2397 continue;
2399 if (ahci_os_lock_port_nb(ap) == 0) {
2400 ahci_port_intr(ap, 0);
2401 ahci_os_unlock_port(ap);
2402 } else {
2403 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2404 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2410 * Core called from helper thread.
2412 void
2413 ahci_port_thread_core(struct ahci_port *ap, int mask)
2415 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2417 * Process any expired timedouts.
2419 ahci_os_lock_port(ap);
2420 if (mask & AP_SIGF_TIMEOUT) {
2421 ahci_check_active_timeouts(ap);
2425 * Process port interrupts which require a higher level of
2426 * intervention.
2428 if (mask & AP_SIGF_PORTINT) {
2429 ahci_port_intr(ap, 1);
2430 ahci_port_interrupt_enable(ap);
2431 } else if (ap->ap_probe != ATA_PROBE_FAILED) {
2432 ahci_port_intr(ap, 1);
2433 ahci_port_interrupt_enable(ap);
2435 ahci_os_unlock_port(ap);
2439 * Core per-port interrupt handler.
2441 * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2442 * deal with normal command completions which do not require blocking.
2444 void
2445 ahci_port_intr(struct ahci_port *ap, int blockable)
2447 struct ahci_softc *sc = ap->ap_sc;
2448 u_int32_t is, ci_saved, ci_masked;
2449 int slot;
2450 int stopped = 0;
2451 struct ahci_ccb *ccb = NULL;
2452 struct ata_port *ccb_at = NULL;
2453 volatile u_int32_t *active;
2454 const u_int32_t blockable_mask = AHCI_PREG_IS_TFES |
2455 AHCI_PREG_IS_IFS |
2456 AHCI_PREG_IS_PCS |
2457 AHCI_PREG_IS_PRCS |
2458 AHCI_PREG_IS_HBFS |
2459 AHCI_PREG_IS_OFS |
2460 AHCI_PREG_IS_UFS;
2462 enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART,
2463 NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2464 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
2467 * All basic command completions are always processed.
2469 is = ahci_pread(ap, AHCI_PREG_IS);
2470 if (is & AHCI_PREG_IS_DPS)
2471 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2474 * If we can't block then we can't handle these here. Disable
2475 * the interrupts in question so we don't live-lock, the helper
2476 * thread will re-enable them.
2478 * If the port is in a completely failed state we do not want
2479 * to drop through to failed-command-processing if blockable is 0,
2480 * just let the thread deal with it all.
2482 * Otherwise we fall through and still handle DHRS and any commands
2483 * which completed normally. Even if we are errored we haven't
2484 * stopped the port yet so CI/SACT are still good.
2486 if (blockable == 0) {
2487 if (ap->ap_state == AP_S_FATAL_ERROR) {
2488 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2489 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2490 return;
2492 if (is & blockable_mask) {
2493 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2494 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2495 return;
2500 * Either NCQ or non-NCQ commands will be active, never both.
2502 if (ap->ap_sactive) {
2503 KKASSERT(ap->ap_active == 0);
2504 KKASSERT(ap->ap_active_cnt == 0);
2505 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2506 active = &ap->ap_sactive;
2507 } else {
2508 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2509 active = &ap->ap_active;
2511 KKASSERT(!(ap->ap_sactive && ap->ap_active));
2512 KKASSERT((ci_saved & (ap->ap_sactive | ap->ap_active)) == ci_saved);
2513 #if 0
2514 kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
2515 ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
2516 ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
2517 #endif
2520 * Ignore AHCI_PREG_IS_PRCS when link power management is on
2522 if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2523 is &= ~AHCI_PREG_IS_PRCS;
2524 ahci_pwrite(ap, AHCI_PREG_SERR,
2525 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2529 * Command failed (blockable).
2531 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2533 * This stops command processing.
2535 if (is & AHCI_PREG_IS_TFES) {
2536 u_int32_t tfd, serr, cmd;
2537 int err_slot;
2539 process_error:
2540 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2541 serr = ahci_pread(ap, AHCI_PREG_SERR);
2542 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2545 * Load the error slot and restart command processing.
2546 * CLO if we need to. The error slot may not be valid.
2547 * MUST BE DONE BEFORE CLEARING ST!
2549 * Cycle ST.
2551 * It is unclear but we may have to clear SERR to reenable
2552 * error processing.
2554 if (cmd & AHCI_PREG_CMD_ST) {
2555 err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
2556 } else {
2557 err_slot = -1;
2559 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
2560 AHCI_PREG_IS_PSS |
2561 AHCI_PREG_IS_DHRS |
2562 AHCI_PREG_IS_SDBS);
2563 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
2564 AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
2565 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2566 ahci_port_stop(ap, 0);
2567 ahci_os_hardsleep(10);
2568 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2569 kprintf("%s: Issuing CLO\n", PORTNAME(ap));
2570 ahci_port_clo(ap);
2574 * We are now stopped and need a restart. If we have to
2575 * process a NCQ error we will temporarily start and then
2576 * stop the port again, so this condition holds.
2578 stopped = 1;
2579 need = NEED_RESTART;
2582 * ATAPI errors are fairly common from probing, just
2583 * report disk errors or if bootverbose is on.
2585 if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
2586 kprintf("%s: TFES slot %d ci_saved = %08x\n",
2587 PORTNAME(ap), err_slot, ci_saved);
2591 * If we got an error on an error CCB just complete it
2592 * with an error. ci_saved has the mask to restart
2593 * (the err_ccb will be removed from it by finish_error).
2595 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2596 err_slot = ap->ap_err_ccb->ccb_slot;
2597 goto finish_error;
2601 * If NCQ commands were active get the error slot from
2602 * the log page. NCQ is not supported for PM's so this
2603 * is a direct-attached target.
2605 * Otherwise if no commands were active we have a problem.
2607 * Otherwise if the error slot is bad we have a problem.
2609 * Otherwise process the error for the slot.
2611 if (ap->ap_sactive) {
2612 ahci_port_start(ap);
2613 err_slot = ahci_port_read_ncq_error(ap, 0);
2614 ahci_port_stop(ap, 0);
2615 } else if (ap->ap_active == 0) {
2616 kprintf("%s: TFES with no commands pending\n",
2617 PORTNAME(ap));
2618 err_slot = -1;
2619 } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
2620 kprintf("%s: bad error slot %d\n",
2621 PORTNAME(ap), err_slot);
2622 err_slot = -1;
2623 } else {
2624 ccb = &ap->ap_ccbs[err_slot];
2627 * Validate the errored ccb. Note that ccb_at can
2628 * be NULL for direct-attached ccb's.
2630 * Copy received taskfile data from the RFIS.
2632 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
2633 ccb_at = ccb->ccb_xa.at;
2634 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2635 sizeof(struct ata_fis_d2h));
2636 if (bootverbose) {
2637 kprintf("%s: Copying rfis slot %d\n",
2638 ATANAME(ap, ccb_at), err_slot);
2640 } else {
2641 kprintf("%s: Cannot copy rfis, CCB slot "
2642 "%d is not on-chip (state=%d)\n",
2643 ATANAME(ap, ccb->ccb_xa.at),
2644 err_slot, ccb->ccb_xa.state);
2645 err_slot = -1;
2650 * If we could not determine the errored slot then
2651 * reset the port.
2653 if (err_slot < 0) {
2654 kprintf("%s: TFES: Unable to determine errored slot\n",
2655 PORTNAME(ap));
2656 if (ap->ap_flags & AP_F_IN_RESET)
2657 goto fatal;
2658 goto failall;
2662 * Finish error on slot. We will restart ci_saved
2663 * commands except the errored slot which we generate
2664 * a failure for.
2666 finish_error:
2667 ccb = &ap->ap_ccbs[err_slot];
2668 ci_saved &= ~(1 << err_slot);
2669 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2670 ccb->ccb_xa.state = ATA_S_ERROR;
2671 } else if (is & AHCI_PREG_IS_DHRS) {
2673 * Command posted D2H register FIS to the rfis (non-blocking).
2675 * A normal completion with an error may set DHRS instead
2676 * of TFES. The CCS bits are only valid if ERR was set.
2677 * If ERR is set command processing was probably stopped.
2679 * If ERR was not set we can only copy-back data for
2680 * exclusive-mode commands because otherwise we won't know
2681 * which tag the rfis belonged to.
2683 * err_slot must be read from the CCS before any other port
2684 * action, such as stopping the port.
2686 * WARNING! This is not well documented in the AHCI spec.
2687 * It can be found in the state machine tables
2688 * but not in the explanations.
2690 u_int32_t tfd;
2691 u_int32_t cmd;
2692 int err_slot;
2694 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2695 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2697 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2698 if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
2699 (cmd & AHCI_PREG_CMD_CR) == 0) {
2700 err_slot = AHCI_PREG_CMD_CCS(
2701 ahci_pread(ap, AHCI_PREG_CMD));
2702 ccb = &ap->ap_ccbs[err_slot];
2703 kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
2704 PORTNAME(ap),
2705 tfd, AHCI_PFMT_TFD_STS,
2706 err_slot, ccb->ccb_xa.fis->command);
2707 goto process_error;
2710 * NO ELSE... copy back is in the normal command completion
2711 * code and only if no error occured and ATA_F_AUTOSENSE
2712 * was set.
2717 * Device notification to us (non-blocking)
2719 * NOTE! On some parts notification bits can cause an IPMS
2720 * interrupt instead of a SDBS interrupt.
2722 * NOTE! On some parts (e.g. VBOX, probably intel ICHx),
2723 * SDBS notifies us of the completion of a NCQ command
2724 * and DBS does not.
2726 if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
2727 u_int32_t data;
2729 ahci_pwrite(ap, AHCI_PREG_IS,
2730 AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2731 if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
2732 data = ahci_pread(ap, AHCI_PREG_SNTF);
2733 if (data) {
2734 ahci_pwrite(ap, AHCI_PREG_IS,
2735 AHCI_PREG_IS_SDBS);
2736 kprintf("%s: NOTIFY %08x\n",
2737 PORTNAME(ap), data);
2738 ahci_pwrite(ap, AHCI_PREG_SERR,
2739 AHCI_PREG_SERR_DIAG_N);
2740 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2741 ahci_cam_changed(ap, NULL, -1);
2744 is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2748 * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set.
2750 * Spurious IFS errors can occur while we are doing a reset
2751 * sequence through a PM, probably due to an unexpected FIS
2752 * being received during the PM target reset sequence. Chipsets
2753 * are supposed to mask these events but some do not.
2755 * Try to recover from the condition.
2757 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2758 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2759 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2760 kprintf("%s: IFS during PM probe (ignored) "
2761 "IS=%b, SERR=%b\n",
2762 PORTNAME(ap),
2763 is, AHCI_PFMT_IS,
2764 serr, AHCI_PFMT_SERR);
2765 ap->ap_flags |= AP_F_IFS_IGNORED;
2769 * Try to clear the error condition. The IFS error killed
2770 * the port so stop it so we can restart it.
2772 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2773 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2774 is &= ~AHCI_PREG_IS_IFS;
2775 need = NEED_RESTART;
2776 goto failall;
2780 * Port change (hot-plug) (blockable).
2782 * A PRCS interrupt can occur:
2783 * (1) On hot-unplug / normal-unplug (phy lost)
2784 * (2) Sometimes on hot-plug too.
2786 * A PCS interrupt can occur in a number of situations:
2787 * (1) On hot-plug once communication is established
2788 * (2) On hot-unplug sometimes.
2789 * (3) For chipsets with badly written firmware it can occur
2790 * during INIT/RESET sequences due to the device reset.
2791 * (4) For chipsets with badly written firmware it can occur
2792 * when it thinks an unsolicited COMRESET is received
2793 * during a INIT/RESET sequence, even though we actually
2794 * did request it.
2796 * XXX We can then check the CPS (Cold Presence State) bit, if
2797 * supported, to determine if a device is plugged in or not and do
2798 * the right thing.
2800 * PCS interrupts are cleared by clearing DIAG_X. If this occurs
2801 * command processing is automatically stopped (CR goes inactive)
2802 * and the port must be stopped and restarted.
2804 * WARNING: AMD parts (e.g. 880G chipset, probably others) can
2805 * generate PCS on initialization even when device is
2806 * already connected up. It is unclear why this happens.
2807 * Depending on the state of the device detect this can
2808 * cause us to go into harsh reinit or hot-plug insertion
2809 * mode.
2811 * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET
2812 * continues to flow in from the device), we must clear the
2813 * interrupt in all cases and enforce a delay to prevent
2814 * a livelock and give the port time to settle down.
2815 * Only print something if we aren't in INIT/HARD-RESET.
2817 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2818 ahci_pwrite(ap, AHCI_PREG_IS,
2819 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2821 * Try to clear the error. Because of the repetitiveness
2822 * of this interrupt avoid any harsh action if the port is
2823 * already in the init or hard-reset probe state.
2825 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2826 /* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */
2829 * Ignore PCS/PRCS errors during probes (but still clear the
2830 * interrupt to avoid a livelock). The AMD 880/890/SB850
2831 * chipsets do not mask PCS/PRCS internally during reset
2832 * sequences.
2834 if (ap->ap_flags & AP_F_IN_RESET)
2835 goto skip_pcs;
2837 if (ap->ap_probe == ATA_PROBE_NEED_INIT ||
2838 ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) {
2839 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2840 need = NEED_NOTHING;
2841 ahci_os_sleep(1000);
2842 goto failall;
2844 kprintf("%s: Transient Errors: %b (%d)\n",
2845 PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe);
2846 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2847 ahci_os_sleep(200);
2850 * Stop the port and figure out what to do next.
2852 ahci_port_stop(ap, 0);
2853 stopped = 1;
2855 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2856 case AHCI_PREG_SSTS_DET_DEV:
2858 * Device detect
2860 if (ap->ap_probe == ATA_PROBE_FAILED) {
2861 need = NEED_HOTPLUG_INSERT;
2862 goto fatal;
2864 need = NEED_RESTART;
2865 break;
2866 case AHCI_PREG_SSTS_DET_DEV_NE:
2868 * Device not communicating. AMD parts seem to
2869 * like to throw this error on initialization
2870 * for no reason that I can fathom.
2872 kprintf("%s: Device present but not communicating, "
2873 "attempting port restart\n",
2874 PORTNAME(ap));
2875 need = NEED_REINIT;
2876 goto fatal;
2877 default:
2878 if (ap->ap_probe != ATA_PROBE_FAILED) {
2879 need = NEED_HOTPLUG_REMOVE;
2880 goto fatal;
2882 need = NEED_RESTART;
2883 break;
2885 skip_pcs:
2890 * Check for remaining errors - they are fatal. (blockable)
2892 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2893 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2894 u_int32_t serr;
2896 ahci_pwrite(ap, AHCI_PREG_IS,
2897 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2898 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2899 AHCI_PREG_IS_UFS));
2900 serr = ahci_pread(ap, AHCI_PREG_SERR);
2901 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
2902 "disabling port.\n",
2903 PORTNAME(ap),
2904 is, AHCI_PFMT_IS,
2905 serr, AHCI_PFMT_SERR
2907 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2908 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2909 AHCI_PREG_IS_UFS);
2912 * Fail all commands but then what? For now try to
2913 * reinitialize the port.
2915 need = NEED_REINIT;
2916 goto fatal;
2920 * Fail all outstanding commands if we know the port won't recover.
2922 * We may have a ccb_at if the failed command is known and was
2923 * being sent to a device over a port multiplier (PM). In this
2924 * case if the port itself has not completely failed we fail just
2925 * the commands related to that target.
2927 * ci_saved contains the mask of active commands as of when the
2928 * error occured, prior to any port stops.
2930 if (ap->ap_state == AP_S_FATAL_ERROR) {
2931 fatal:
2932 ap->ap_state = AP_S_FATAL_ERROR;
2933 failall:
2934 ahci_port_stop(ap, 0);
2935 stopped = 1;
2938 * Error all the active slots not already errored.
2940 ci_masked = ci_saved & *active & ~ap->ap_expired;
2941 if (ci_masked) {
2942 kprintf("%s: Failing all commands: %08x\n",
2943 PORTNAME(ap), ci_masked);
2946 while (ci_masked) {
2947 slot = ffs(ci_masked) - 1;
2948 ccb = &ap->ap_ccbs[slot];
2949 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2950 ap->ap_expired |= 1 << slot;
2951 ci_saved &= ~(1 << slot);
2952 ci_masked &= ~(1 << slot);
2956 * Clear bits in ci_saved (cause completions to be run)
2957 * for all slots which are not active.
2959 ci_saved &= ~*active;
2962 * Don't restart the port if our problems were deemed fatal.
2964 * Also acknowlege all fatal interrupt sources to prevent
2965 * a livelock.
2967 if (ap->ap_state == AP_S_FATAL_ERROR) {
2968 if (need == NEED_RESTART)
2969 need = NEED_NOTHING;
2970 ahci_pwrite(ap, AHCI_PREG_IS,
2971 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2972 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2973 AHCI_PREG_IS_UFS);
2978 * If we are stopped the AHCI chipset is supposed to have cleared
2979 * CI and SACT. Did it? If it didn't we try very hard to clear
2980 * the fields otherwise we may end up completing CCBs which are
2981 * actually still active.
2983 * IFS errors on (at least) AMD chipsets create this confusion.
2985 if (stopped) {
2986 u_int32_t mask;
2987 if ((mask = ahci_pactive(ap)) != 0) {
2988 kprintf("%s: chipset failed to clear "
2989 "active cmds %08x\n",
2990 PORTNAME(ap), mask);
2991 ahci_port_start(ap);
2992 ahci_port_stop(ap, 0);
2993 if ((mask = ahci_pactive(ap)) != 0) {
2994 kprintf("%s: unable to prod the chip into "
2995 "clearing active cmds %08x\n",
2996 PORTNAME(ap), mask);
2997 /* what do we do now? */
3003 * CCB completion (non blocking).
3005 * CCB completion is detected by noticing its slot's bit in CI has
3006 * changed to zero some time after we activated it.
3007 * If we are polling, we may only be interested in particular slot(s).
3009 * Any active bits not saved are completed within the restrictions
3010 * imposed by the caller.
3012 ci_masked = ~ci_saved & *active;
3013 while (ci_masked) {
3014 slot = ffs(ci_masked) - 1;
3015 ccb = &ap->ap_ccbs[slot];
3016 ci_masked &= ~(1 << slot);
3018 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
3019 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
3020 " (error)" : "");
3022 bus_dmamap_sync(sc->sc_tag_cmdh,
3023 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
3024 BUS_DMASYNC_POSTWRITE);
3026 bus_dmamap_sync(sc->sc_tag_cmdt,
3027 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
3028 BUS_DMASYNC_POSTWRITE);
3030 bus_dmamap_sync(sc->sc_tag_rfis,
3031 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
3032 BUS_DMASYNC_POSTREAD);
3034 *active &= ~(1 << ccb->ccb_slot);
3035 if (active == &ap->ap_active) {
3036 KKASSERT(ap->ap_active_cnt > 0);
3037 --ap->ap_active_cnt;
3041 * Complete the ccb. If the ccb was marked expired it
3042 * was probably already removed from the command processor,
3043 * so don't take the clear ci_saved bit as meaning the
3044 * command actually succeeded, it didn't.
3046 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
3047 ap->ap_expired &= ~(1 << ccb->ccb_slot);
3048 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3049 ccb->ccb_done(ccb);
3050 ccb->ccb_xa.complete(&ccb->ccb_xa);
3051 } else {
3052 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
3053 ccb->ccb_xa.state = ATA_S_COMPLETE;
3054 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
3055 memcpy(&ccb->ccb_xa.rfis,
3056 ap->ap_rfis->rfis,
3057 sizeof(struct ata_fis_d2h));
3058 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
3059 ccb->ccb_xa.state = ATA_S_ERROR;
3062 ccb->ccb_done(ccb);
3067 * Cleanup. Will not be set if non-blocking.
3069 switch(need) {
3070 case NEED_NOTHING:
3072 * If operating normally and not stopped the interrupt was
3073 * probably just a normal completion and we may be able to
3074 * issue more commands.
3076 if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR)
3077 ahci_issue_pending_commands(ap, NULL);
3078 break;
3079 case NEED_RESTART:
3081 * A recoverable error occured and we can restart outstanding
3082 * commands on the port.
3084 ci_saved &= ~ap->ap_expired;
3085 if (ci_saved) {
3086 kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
3087 ahci_issue_saved_commands(ap, ci_saved);
3091 * Potentially issue new commands if not in a failed
3092 * state.
3094 if (ap->ap_state != AP_S_FATAL_ERROR) {
3095 ahci_port_start(ap);
3096 ahci_issue_pending_commands(ap, NULL);
3098 break;
3099 case NEED_REINIT:
3101 * Something horrible happened to the port and we
3102 * need to reinitialize it.
3104 kprintf("%s: REINIT - Attempting to reinitialize the port "
3105 "after it had a horrible accident\n",
3106 PORTNAME(ap));
3107 ap->ap_flags |= AP_F_IN_RESET;
3108 ap->ap_flags |= AP_F_HARSH_REINIT;
3109 ap->ap_probe = ATA_PROBE_NEED_INIT;
3110 ahci_cam_changed(ap, NULL, -1);
3111 break;
3112 case NEED_HOTPLUG_INSERT:
3114 * A hot-plug insertion event has occured and all
3115 * outstanding commands have already been revoked.
3117 * Don't recurse if this occurs while we are
3118 * resetting the port.
3120 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3121 kprintf("%s: HOTPLUG - Device inserted\n",
3122 PORTNAME(ap));
3123 ap->ap_probe = ATA_PROBE_NEED_INIT;
3124 ahci_cam_changed(ap, NULL, -1);
3126 break;
3127 case NEED_HOTPLUG_REMOVE:
3129 * A hot-plug removal event has occured and all
3130 * outstanding commands have already been revoked.
3132 * Don't recurse if this occurs while we are
3133 * resetting the port.
3135 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3136 kprintf("%s: HOTPLUG - Device removed\n",
3137 PORTNAME(ap));
3138 ahci_port_hardstop(ap);
3139 /* ap_probe set to failed */
3140 ahci_cam_changed(ap, NULL, -1);
3142 break;
3143 default:
3144 break;
3148 struct ahci_ccb *
3149 ahci_get_ccb(struct ahci_port *ap)
3151 struct ahci_ccb *ccb;
3152 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3154 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3155 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
3156 if (ccb != NULL) {
3157 KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3158 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
3159 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
3160 ccb->ccb_xa.state = ATA_S_SETUP;
3161 ccb->ccb_xa.flags = 0;
3162 ccb->ccb_xa.at = NULL;
3164 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3166 return (ccb);
3169 void
3170 ahci_put_ccb(struct ahci_ccb *ccb)
3172 struct ahci_port *ap = ccb->ccb_port;
3173 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3175 KKASSERT(ccb->ccb_xa.state != ATA_S_PUT);
3176 KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3177 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3178 ccb->ccb_xa.state = ATA_S_PUT;
3179 ++ccb->ccb_xa.serial;
3180 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
3181 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3184 struct ahci_ccb *
3185 ahci_get_err_ccb(struct ahci_port *ap)
3187 struct ahci_ccb *err_ccb;
3188 u_int32_t sact;
3189 u_int32_t ci;
3191 /* No commands may be active on the chip. */
3192 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3194 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3195 sact = ahci_pread(ap, AHCI_PREG_SACT);
3196 if (sact != 0) {
3197 kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
3198 PORTNAME(ap), sact);
3201 ci = ahci_pread(ap, AHCI_PREG_CI);
3202 if (ci) {
3203 kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
3204 PORTNAME(ap), ci);
3206 KKASSERT(ci == 0);
3207 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
3208 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
3210 /* Save outstanding command state. */
3211 ap->ap_err_saved_active = ap->ap_active;
3212 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
3213 ap->ap_err_saved_sactive = ap->ap_sactive;
3216 * Pretend we have no commands outstanding, so that completions won't
3217 * run prematurely.
3219 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
3222 * Grab a CCB to use for error recovery. This should never fail, as
3223 * we ask atascsi to reserve one for us at init time.
3225 err_ccb = ap->ap_err_ccb;
3226 KKASSERT(err_ccb != NULL);
3227 err_ccb->ccb_xa.flags = 0;
3228 err_ccb->ccb_done = ahci_empty_done;
3230 return err_ccb;
3233 void
3234 ahci_put_err_ccb(struct ahci_ccb *ccb)
3236 struct ahci_port *ap = ccb->ccb_port;
3237 u_int32_t sact;
3238 u_int32_t ci;
3239 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3241 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
3244 * No commands may be active on the chip
3246 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3247 sact = ahci_pread(ap, AHCI_PREG_SACT);
3248 if (sact) {
3249 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
3250 ccb->ccb_slot, sact);
3253 ci = ahci_pread(ap, AHCI_PREG_CI);
3254 if (ci) {
3255 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
3256 "(act=%08x sact=%08x)\n",
3257 ccb->ccb_slot, ci,
3258 ap->ap_active, ap->ap_sactive);
3261 KKASSERT(ccb == ap->ap_err_ccb);
3263 /* Restore outstanding command state */
3264 ap->ap_sactive = ap->ap_err_saved_sactive;
3265 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
3266 ap->ap_active = ap->ap_err_saved_active;
3268 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
3272 * Read log page to get NCQ error.
3274 * NOTE: NCQ not currently supported on port multipliers. XXX
3277 ahci_port_read_ncq_error(struct ahci_port *ap, int target)
3279 struct ata_log_page_10h *log;
3280 struct ahci_ccb *ccb;
3281 struct ahci_ccb *ccb2;
3282 struct ahci_cmd_hdr *cmd_slot;
3283 struct ata_fis_h2d *fis;
3284 int err_slot;
3285 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3287 if (bootverbose) {
3288 kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
3289 target);
3293 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
3295 * Getting err_ccb clears active/sactive/active_cnt, putting
3296 * it back restores the fields.
3298 ccb = ahci_get_err_ccb(ap);
3299 ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
3300 ccb->ccb_xa.data = ap->ap_err_scratch;
3301 ccb->ccb_xa.datalen = 512;
3302 ccb->ccb_xa.complete = ahci_dummy_done;
3303 ccb->ccb_xa.at = ap->ap_ata[target];
3305 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
3306 memset(fis, 0, sizeof(*fis));
3307 fis->type = ATA_FIS_TYPE_H2D;
3308 fis->flags = ATA_H2D_FLAGS_CMD | target;
3309 fis->command = ATA_C_READ_LOG_EXT;
3310 fis->lba_low = 0x10; /* queued error log page (10h) */
3311 fis->sector_count = 1; /* number of sectors (1) */
3312 fis->sector_count_exp = 0;
3313 fis->lba_mid = 0; /* starting offset */
3314 fis->lba_mid_exp = 0;
3315 fis->device = 0;
3317 cmd_slot = ccb->ccb_cmd_hdr;
3318 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
3320 if (ahci_load_prdt(ccb) != 0) {
3321 err_slot = -1;
3322 goto err;
3325 ccb->ccb_xa.state = ATA_S_PENDING;
3326 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
3327 err_slot = -1;
3328 ahci_unload_prdt(ccb);
3329 goto err;
3331 ahci_unload_prdt(ccb);
3334 * Success, extract failed register set and tags from the scratch
3335 * space.
3337 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
3338 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
3339 /* Not queued bit was set - wasn't an NCQ error? */
3340 kprintf("%s: read NCQ error page, but not an NCQ error?\n",
3341 PORTNAME(ap));
3342 err_slot = -1;
3343 } else {
3344 /* Copy back the log record as a D2H register FIS. */
3345 err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
3347 ccb2 = &ap->ap_ccbs[err_slot];
3348 if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
3349 kprintf("%s: read NCQ error page slot=%d\n",
3350 ATANAME(ap, ccb2->ccb_xa.at),
3351 err_slot);
3352 memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
3353 sizeof(struct ata_fis_d2h));
3354 ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3355 ccb2->ccb_xa.rfis.flags = 0;
3356 } else {
3357 kprintf("%s: read NCQ error page slot=%d, "
3358 "slot does not match any cmds\n",
3359 ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
3360 err_slot);
3361 err_slot = -1;
3364 err:
3365 ahci_put_err_ccb(ccb);
3366 kprintf("%s: DONE log page target %d err_slot=%d\n",
3367 PORTNAME(ap), target, err_slot);
3368 return (err_slot);
3372 * Allocate memory for various structures DMAd by hardware. The maximum
3373 * number of segments for these tags is 1 so the DMA memory will have a
3374 * single physical base address.
3376 struct ahci_dmamem *
3377 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3379 struct ahci_dmamem *adm;
3380 int error;
3381 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3383 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3385 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3386 BUS_DMA_ZERO, &adm->adm_map);
3387 if (error == 0) {
3388 adm->adm_tag = tag;
3389 error = bus_dmamap_load(tag, adm->adm_map,
3390 adm->adm_kva,
3391 bus_dma_tag_getmaxsize(tag),
3392 ahci_dmamem_saveseg, &adm->adm_busaddr,
3395 if (error) {
3396 if (adm->adm_map) {
3397 bus_dmamap_destroy(tag, adm->adm_map);
3398 adm->adm_map = NULL;
3399 adm->adm_tag = NULL;
3400 adm->adm_kva = NULL;
3402 kfree(adm, M_DEVBUF);
3403 adm = NULL;
3405 return (adm);
3408 static
3409 void
3410 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3412 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3413 KKASSERT(error == 0);
3414 KKASSERT(nsegs == 1);
3415 *(bus_addr_t *)info = segs->ds_addr;
3419 void
3420 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3422 D(bug("[AHCI] %s()\n", __PRETTY_FUNCTION__));
3423 if (adm->adm_map) {
3424 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3425 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3426 adm->adm_map = NULL;
3427 adm->adm_tag = NULL;
3428 adm->adm_kva = NULL;
3430 kfree(adm, M_DEVBUF);
3433 u_int32_t
3434 ahci_read(struct ahci_softc *sc, bus_size_t r)
3436 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3437 BUS_SPACE_BARRIER_READ);
3438 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3441 void
3442 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3444 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3445 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3446 BUS_SPACE_BARRIER_WRITE);
3449 u_int32_t
3450 ahci_pread(struct ahci_port *ap, bus_size_t r)
3452 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3453 BUS_SPACE_BARRIER_READ);
3454 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3457 void
3458 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3460 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3461 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3462 BUS_SPACE_BARRIER_WRITE);
3466 * Wait up to (timeout) milliseconds for the masked port register to
3467 * match the target.
3469 * Timeout is in milliseconds.
3472 ahci_pwait_eq(struct ahci_port *ap, int timeout,
3473 bus_size_t r, u_int32_t mask, u_int32_t target)
3475 int t;
3478 * Loop hard up to 100uS
3480 for (t = 0; t < 100; ++t) {
3481 if ((ahci_pread(ap, r) & mask) == target)
3482 return (0);
3483 ahci_os_hardsleep(1); /* us */
3486 do {
3487 timeout -= ahci_os_softsleep();
3488 if ((ahci_pread(ap, r) & mask) == target)
3489 return (0);
3490 } while (timeout > 0);
3491 return (1);
3495 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3496 u_int32_t target)
3498 int t;
3501 * Loop hard up to 100uS
3503 for (t = 0; t < 100; ++t) {
3504 if ((ahci_read(sc, r) & mask) != target)
3505 return (0);
3506 ahci_os_hardsleep(1); /* us */
3510 * And one millisecond the slow way
3512 t = 1000;
3513 do {
3514 t -= ahci_os_softsleep();
3515 if ((ahci_read(sc, r) & mask) != target)
3516 return (0);
3517 } while (t > 0);
3519 return (1);
3524 * Acquire an ata transfer.
3526 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
3527 * targets that go through the port multiplier.
3529 struct ata_xfer *
3530 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3532 struct ahci_ccb *ccb;
3534 ccb = ahci_get_ccb(ap);
3535 if (ccb == NULL) {
3536 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3537 PORTNAME(ap));
3538 return (NULL);
3541 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3542 PORTNAME(ap), ccb->ccb_slot);
3544 memset(ccb->ccb_xa.fis, 0, sizeof(*ccb->ccb_xa.fis));
3545 ccb->ccb_xa.at = at;
3546 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3548 return (&ccb->ccb_xa);
3551 void
3552 ahci_ata_put_xfer(struct ata_xfer *xa)
3554 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3556 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3558 ahci_put_ccb(ccb);
3562 ahci_ata_cmd(struct ata_xfer *xa)
3564 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3565 struct ahci_cmd_hdr *cmd_slot;
3567 KKASSERT(xa->state == ATA_S_SETUP);
3569 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3570 goto failcmd;
3571 ccb->ccb_done = ahci_ata_cmd_done;
3573 cmd_slot = ccb->ccb_cmd_hdr;
3574 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
3575 if (ccb->ccb_xa.at) {
3576 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
3577 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
3580 if (xa->flags & ATA_F_WRITE)
3581 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3583 if (xa->flags & ATA_F_PACKET)
3584 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3586 if (ahci_load_prdt(ccb) != 0)
3587 goto failcmd;
3589 xa->state = ATA_S_PENDING;
3591 if (xa->flags & ATA_F_POLL)
3592 return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3594 crit_enter();
3595 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
3596 xa->flags |= ATA_F_TIMEOUT_DESIRED;
3597 ahci_start(ccb);
3598 crit_exit();
3599 return (xa->state);
3601 failcmd:
3602 crit_enter();
3603 xa->state = ATA_S_ERROR;
3604 xa->complete(xa);
3605 crit_exit();
3606 return (ATA_S_ERROR);
3609 void
3610 ahci_ata_cmd_done(struct ahci_ccb *ccb)
3612 struct ata_xfer *xa = &ccb->ccb_xa;
3613 int serial;
3616 * NOTE: Callout does not lock port and may race us modifying
3617 * the flags, so make sure its stopped.
3619 * A callout race can clean up the ccb. A change in the
3620 * serial number should catch this condition.
3622 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3623 serial = ccb->ccb_xa.serial;
3624 callout_stop_sync(&ccb->ccb_timeout);
3625 if (serial != ccb->ccb_xa.serial) {
3626 kprintf("%s: Warning: timeout race ccb %p\n",
3627 PORTNAME(ccb->ccb_port), ccb);
3628 return;
3630 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3632 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3633 ccb->ccb_port->ap_expired &= ~(1 << ccb->ccb_slot);
3635 KKASSERT(xa->state != ATA_S_ONCHIP && xa->state != ATA_S_PUT);
3636 ahci_unload_prdt(ccb);
3638 if (xa->state != ATA_S_TIMEOUT)
3639 xa->complete(xa);
3643 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3644 * while the callout is runing.
3646 * We can't safely get the port lock here or delay, we could block
3647 * the callout thread.
3649 static void
3650 ahci_ata_cmd_timeout_unserialized(void *arg)
3652 struct ahci_ccb *ccb = arg;
3653 struct ahci_port *ap = ccb->ccb_port;
3655 KKASSERT(ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING);
3656 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3657 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3658 ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3662 * Timeout code, typically called when the port command processor is running.
3664 * We have to be very very careful here. We cannot stop the port unless
3665 * CR is already clear or the only active commands remaining are timed-out
3666 * ones. Otherwise stopping the port will race the command processor and
3667 * we can lose events. While we can theoretically just restart everything
3668 * that could result in a double-issue which will not work for ATAPI commands.
3670 void
3671 ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3673 struct ata_xfer *xa = &ccb->ccb_xa;
3674 struct ahci_port *ap = ccb->ccb_port;
3675 struct ata_port *at;
3676 u_int32_t ci_saved;
3677 u_int32_t mask;
3678 int slot;
3680 at = ccb->ccb_xa.at;
3682 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
3683 "\tglb-status 0x%08x\n"
3684 "\tcmd-reg 0x%b\n"
3685 "\tport_status 0x%b\n"
3686 "\tsactive=%08x active=%08x expired=%08x\n"
3687 "\t sact=%08x ci=%08x\n"
3688 "\t STS=%b\n",
3689 ATANAME(ap, at),
3690 ccb->ccb_xa.state, ccb->ccb_slot,
3691 ahci_read(ap->ap_sc, AHCI_REG_IS),
3692 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3693 ahci_pread(ap, AHCI_PREG_IS), AHCI_PFMT_IS,
3694 ap->ap_sactive, ap->ap_active, ap->ap_expired,
3695 ahci_pread(ap, AHCI_PREG_SACT),
3696 ahci_pread(ap, AHCI_PREG_CI),
3697 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
3702 * NOTE: Timeout will not be running if the command was polled.
3703 * If we got here at least one of these flags should be set.
3705 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
3706 ATA_F_TIMEOUT_RUNNING));
3707 xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3709 if (ccb->ccb_xa.state == ATA_S_PENDING) {
3710 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3711 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3712 ccb->ccb_done(ccb);
3713 xa->complete(xa);
3714 ahci_issue_pending_commands(ap, NULL);
3715 return;
3717 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3718 kprintf("%s: Unexpected state during timeout: %d\n",
3719 ATANAME(ap, at), ccb->ccb_xa.state);
3720 return;
3724 * Ok, we can only get this command off the chip if CR is inactive
3725 * or if the only commands running on the chip are all expired.
3726 * Otherwise we have to wait until the port is in a safe state.
3728 * Do not set state here, it will cause polls to return when the
3729 * ccb is not yet off the chip.
3731 ap->ap_expired |= 1 << ccb->ccb_slot;
3733 if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
3734 (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
3736 * If using FBSS or NCQ we can't safely stop the port
3737 * right now.
3739 kprintf("%s: Deferred timeout until its safe, slot %d\n",
3740 ATANAME(ap, at), ccb->ccb_slot);
3741 return;
3745 * We can safely stop the port and process all expired ccb's,
3746 * which will include our current ccb.
3748 ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
3749 ahci_pread(ap, AHCI_PREG_CI);
3750 ahci_port_stop(ap, 0);
3752 while (ap->ap_expired) {
3753 slot = ffs(ap->ap_expired) - 1;
3754 ap->ap_expired &= ~(1 << slot);
3755 ci_saved &= ~(1 << slot);
3756 ccb = &ap->ap_ccbs[slot];
3757 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3758 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
3759 KKASSERT(ap->ap_sactive & (1 << slot));
3760 ap->ap_sactive &= ~(1 << slot);
3761 } else {
3762 KKASSERT(ap->ap_active & (1 << slot));
3763 ap->ap_active &= ~(1 << slot);
3764 --ap->ap_active_cnt;
3766 ccb->ccb_done(ccb);
3767 ccb->ccb_xa.complete(&ccb->ccb_xa);
3769 /* ccb invalid now */
3772 * We can safely CLO the port to clear any BSY/DRQ, a case which
3773 * can occur with port multipliers. This will unbrick the port
3774 * and allow commands to other targets behind the PM continue.
3775 * (FBSS).
3777 * Finally, once the port has been restarted we can issue any
3778 * previously saved pending commands, and run the port interrupt
3779 * code to handle any completions which may have occured when
3780 * we saved CI.
3782 if (ahci_pread(ap, AHCI_PREG_TFD) &
3783 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
3784 kprintf("%s: Warning, issuing CLO after timeout\n",
3785 ATANAME(ap, at));
3786 ahci_port_clo(ap);
3788 ahci_port_start(ap);
3791 * We absolutely must make sure the chipset cleared activity on
3792 * all slots. This sometimes might not happen due to races with
3793 * a chipset interrupt which stops the port before we can manage
3794 * to. For some reason some chipsets don't clear the active
3795 * commands when we turn off CMD_ST after the chip has stopped
3796 * operations itself.
3798 if (ahci_pactive(ap) != 0) {
3799 ahci_port_stop(ap, 0);
3800 ahci_port_start(ap);
3801 if ((mask = ahci_pactive(ap)) != 0) {
3802 kprintf("%s: quick-timeout: chipset failed "
3803 "to clear active cmds %08x\n",
3804 PORTNAME(ap), mask);
3807 ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
3808 ahci_issue_pending_commands(ap, NULL);
3809 ahci_port_intr(ap, 0);
3813 * Issue a previously saved set of commands
3815 void
3816 ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
3818 if (ci_saved) {
3819 KKASSERT(!((ap->ap_active & ci_saved) &&
3820 (ap->ap_sactive & ci_saved)));
3821 KKASSERT((ci_saved & ap->ap_expired) == 0);
3822 if (ap->ap_sactive & ci_saved)
3823 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
3824 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3829 * Used by the softreset, pmprobe, and read_ncq_error only, in very
3830 * specialized, controlled circumstances.
3832 * Only one command may be pending.
3834 void
3835 ahci_quick_timeout(struct ahci_ccb *ccb)
3837 struct ahci_port *ap = ccb->ccb_port;
3838 u_int32_t mask;
3840 switch (ccb->ccb_xa.state) {
3841 case ATA_S_PENDING:
3842 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3843 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3844 break;
3845 case ATA_S_ONCHIP:
3847 * We have to clear the command on-chip.
3849 KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3850 ap->ap_sactive == 0);
3851 ahci_port_stop(ap, 0);
3852 ahci_port_start(ap);
3853 if (ahci_pactive(ap) != 0) {
3854 ahci_port_stop(ap, 0);
3855 ahci_port_start(ap);
3856 if ((mask = ahci_pactive(ap)) != 0) {
3857 kprintf("%s: quick-timeout: chipset failed "
3858 "to clear active cmds %08x\n",
3859 PORTNAME(ap), mask);
3863 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3864 ap->ap_active &= ~(1 << ccb->ccb_slot);
3865 KKASSERT(ap->ap_active_cnt > 0);
3866 --ap->ap_active_cnt;
3867 break;
3868 default:
3869 panic("%s: ahci_quick_timeout: ccb in bad state %d",
3870 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3874 static void
3875 ahci_dummy_done(struct ata_xfer *xa)
3879 static void
3880 ahci_empty_done(struct ahci_ccb *ccb)
3885 ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
3886 int feature, int enable)
3888 struct ata_port *at;
3889 struct ata_xfer *xa;
3890 int error;
3892 at = atx ? atx : ap->ap_ata[0];
3894 xa = ahci_ata_get_xfer(ap, atx);
3896 xa->fis->type = ATA_FIS_TYPE_H2D;
3897 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
3898 xa->fis->command = ATA_C_SET_FEATURES;
3899 xa->fis->features = enable ? ATA_C_SATA_FEATURE_ENA :
3900 ATA_C_SATA_FEATURE_DIS;
3901 xa->fis->sector_count = feature;
3902 xa->fis->control = ATA_FIS_CONTROL_4BIT;
3904 xa->complete = ahci_dummy_done;
3905 xa->datalen = 0;
3906 xa->flags = ATA_F_POLL;
3907 xa->timeout = 1000;
3909 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
3910 error = 0;
3911 else
3912 error = EIO;
3913 ahci_ata_put_xfer(xa);
3914 return(error);