ahci: Removable media support
[AROS.git] / rom / devs / ahci / ahci.c
blob70d1b3e3ba2526db75bba535f13da61bf276ea2c
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 cap, pi;
94 u_int32_t pleft;
95 int i;
96 struct ahci_port *ap;
98 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
99 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
102 * save BIOS initialised parameters, enable staggered spin up
104 cap = ahci_read(sc, AHCI_REG_CAP);
105 cap &= AHCI_REG_CAP_SMPS;
106 cap |= AHCI_REG_CAP_SSS;
107 pi = ahci_read(sc, AHCI_REG_PI);
110 * Unconditionally reset the controller, do not conditionalize on
111 * trying to figure it if it was previously active or not.
113 * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section
114 * 5.2.2.1 regarding this. HR should be set to 1 only after
115 * AE is set to 1. The reset sequence will clear HR when
116 * it completes, and will also clear AE if SAM is 0. AE must
117 * then be set again. When SAM is 1 the AE bit typically reads
118 * as 1 (and is read-only).
120 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
121 * otherwise the writes will only be separated by a few
122 * nanoseconds.
124 * NOTE BRICKS (1)
126 * If you have a port multiplier and it does not have a device
127 * in target 0, and it probes normally, but a later operation
128 * mis-probes a target behind that PM, it is possible for the
129 * port to brick such that only (a) a power cycle of the host
130 * or (b) placing a device in target 0 will fix the problem.
131 * Power cycling the PM has no effect (it works fine on another
132 * host port). This issue is unrelated to CLO.
135 * Wait for any prior reset sequence to complete
137 if (ahci_wait_ne(sc, AHCI_REG_GHC,
138 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
139 device_printf(sc->sc_dev, "Controller is stuck in reset\n");
140 return (1);
142 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
143 ahci_os_sleep(500);
144 ahci_read(sc, AHCI_REG_GHC); /* flush */
145 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
146 ahci_os_sleep(500);
147 ahci_read(sc, AHCI_REG_GHC); /* flush */
148 if (ahci_wait_ne(sc, AHCI_REG_GHC,
149 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
150 device_printf(sc->sc_dev, "unable to reset controller\n");
151 return (1);
153 if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
154 device_printf(sc->sc_dev, "AE did not auto-clear!\n");
155 ahci_write(sc, AHCI_REG_GHC, 0);
156 ahci_os_sleep(500);
160 * Enable ahci (global interrupts disabled)
162 * Restore saved parameters. Avoid pci transaction burst write
163 * by issuing dummy reads.
165 ahci_os_sleep(500);
166 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
167 ahci_os_sleep(500);
169 ahci_read(sc, AHCI_REG_GHC); /* flush */
170 ahci_write(sc, AHCI_REG_CAP, cap);
171 ahci_write(sc, AHCI_REG_PI, pi);
172 ahci_read(sc, AHCI_REG_GHC); /* flush */
175 * Intel hocus pocus in case the BIOS has not set the chip up
176 * properly for AHCI operation.
178 if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
179 if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
180 device_printf(sc->sc_dev, "Intel hocus pocus\n");
181 pci_write_config(sc->sc_dev, 0x92,
182 pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
186 * This is a hack that currently does not appear to have
187 * a significant effect, but I noticed the port registers
188 * do not appear to be completely cleared after the host
189 * controller is reset.
191 * Use a temporary ap structure so we can call ahci_pwrite().
193 * We must be sure to stop the port
195 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
196 ap->ap_sc = sc;
197 pleft = pi;
198 for (i = 0; i < AHCI_MAX_PORTS; ++i) {
199 if (pleft == 0)
200 break;
201 if ((pi & (1 << i)) == 0)
202 continue;
203 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
204 AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
205 device_printf(sc->sc_dev, "can't map port\n");
206 return (1);
209 * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
210 * AHCI1.1 can brick the chipset. Not only brick it,
211 * but also crash the PC. The bit seems unreliable
212 * on AHCI1.2 as well.
214 ahci_port_stop(ap, 1);
215 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
216 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
217 ahci_pwrite(ap, AHCI_PREG_IE, 0);
218 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
219 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
220 ahci_pwrite(ap, AHCI_PREG_IS, -1);
221 sc->sc_portmask |= (1 << i);
222 pleft &= ~(1 << i);
224 sc->sc_numports = i;
225 kfree(ap, M_DEVBUF);
227 return (0);
231 * Allocate and initialize an AHCI port.
234 ahci_port_alloc(struct ahci_softc *sc, u_int port)
236 struct ahci_port *ap;
237 struct ata_port *at;
238 struct ahci_ccb *ccb;
239 u_int64_t dva;
240 u_int32_t cmd;
241 u_int32_t data;
242 struct ahci_cmd_hdr *hdr;
243 struct ahci_cmd_table *table;
244 int rc = ENOMEM;
245 int error;
246 int i;
248 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
249 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
251 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
252 device_get_name(sc->sc_dev),
253 device_get_unit(sc->sc_dev),
254 port);
255 sc->sc_ports[port] = ap;
258 * Allocate enough so we never have to reallocate, it makes
259 * it easier.
261 * ap_pmcount will be reduced by the scan if we encounter the
262 * port multiplier port prior to target 15.
264 * kmalloc power-of-2 allocations are guaranteed not to cross
265 * a page boundary. Make sure the identify sub-structure in the
266 * at structure does not cross a page boundary, just in case the
267 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
269 if (ap->ap_ata[0] == NULL) {
270 int pw2;
272 for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
274 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
275 at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
276 ap->ap_ata[i] = at;
277 at->at_ahci_port = ap;
278 at->at_target = i;
279 at->at_probe = ATA_PROBE_NEED_INIT;
280 at->at_features |= ATA_PORT_F_RESCAN;
281 ksnprintf(at->at_name, sizeof(at->at_name),
282 "%s.%d", ap->ap_name, i);
285 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
286 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
287 device_printf(sc->sc_dev,
288 "unable to create register window for port %d\n",
289 port);
290 goto freeport;
293 ap->ap_sc = sc;
294 ap->ap_num = port;
295 ap->ap_probe = ATA_PROBE_NEED_INIT;
296 ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE;
297 ap->sysctl_tree = NULL;
298 TAILQ_INIT(&ap->ap_ccb_free);
299 TAILQ_INIT(&ap->ap_ccb_pending);
300 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
302 /* Disable port interrupts */
303 ahci_pwrite(ap, AHCI_PREG_IE, 0);
304 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
307 * Sec 10.1.2 - deinitialise port if it is already running
309 cmd = ahci_pread(ap, AHCI_PREG_CMD);
310 kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD);
312 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
313 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
314 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
315 int r;
317 r = ahci_port_stop(ap, 1);
318 if (r) {
319 device_printf(sc->sc_dev,
320 "unable to disable %s, ignoring port %d\n",
321 ((r == 2) ? "CR" : "FR"), port);
322 rc = ENXIO;
323 goto freeport;
326 /* Write DET to zero */
327 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
330 /* Allocate RFIS */
331 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
332 if (ap->ap_dmamem_rfis == NULL) {
333 kprintf("%s: NORFIS\n", PORTNAME(ap));
334 goto nomem;
337 /* Setup RFIS base address */
338 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
339 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
340 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
341 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
343 /* Clear SERR before starting FIS reception or ST or anything */
344 ahci_flush_tfd(ap);
345 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
347 /* Enable FIS reception and activate port. */
348 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
349 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
350 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
351 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
353 /* Check whether port activated. Skip it if not. */
354 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
355 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
356 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
357 rc = ENXIO;
358 goto freeport;
361 /* Allocate a CCB for each command slot */
362 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
363 M_WAITOK | M_ZERO);
364 if (ap->ap_ccbs == NULL) {
365 device_printf(sc->sc_dev,
366 "unable to allocate command list for port %d\n",
367 port);
368 goto freeport;
371 /* Command List Structures and Command Tables */
372 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
373 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
374 if (ap->ap_dmamem_cmd_table == NULL ||
375 ap->ap_dmamem_cmd_list == NULL) {
376 nomem:
377 device_printf(sc->sc_dev,
378 "unable to allocate DMA memory for port %d\n",
379 port);
380 goto freeport;
383 /* Setup command list base address */
384 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
385 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
386 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
388 /* Split CCB allocation into CCBs and assign to command header/table */
389 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
390 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
391 for (i = 0; i < sc->sc_ncmds; i++) {
392 ccb = &ap->ap_ccbs[i];
394 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
395 &ccb->ccb_dmamap);
396 if (error) {
397 device_printf(sc->sc_dev,
398 "unable to create dmamap for port %d "
399 "ccb %d\n", port, i);
400 goto freeport;
403 callout_init(&ccb->ccb_timeout);
404 ccb->ccb_slot = i;
405 ccb->ccb_port = ap;
406 ccb->ccb_cmd_hdr = &hdr[i];
407 ccb->ccb_cmd_table = &table[i];
408 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
409 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
410 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
411 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
413 ccb->ccb_xa.fis =
414 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
415 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
416 ccb->ccb_xa.tag = i;
418 ccb->ccb_xa.state = ATA_S_COMPLETE;
421 * CCB[1] is the error CCB and is not get or put. It is
422 * also used for probing. Numerous HBAs only load the
423 * signature from CCB[1] so it MUST be used for the second
424 * FIS.
426 if (i == 1)
427 ap->ap_err_ccb = ccb;
428 else
429 ahci_put_ccb(ccb);
433 * Wait for ICC change to complete
435 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
438 * Calculate the interrupt mask
440 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
441 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
442 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
443 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
444 AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
445 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
446 data |= AHCI_PREG_IE_IPME;
447 #ifdef AHCI_COALESCE
448 if (sc->sc_ccc_ports & (1 << port)
449 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
450 #endif
451 ap->ap_intmask = data;
454 * Start the port helper thread. The helper thread will call
455 * ahci_port_init() so the ports can all be started in parallel.
456 * A failure by ahci_port_init() does not deallocate the port
457 * since we still want hot-plug events.
459 ahci_os_start_port(ap);
460 return(0);
461 freeport:
462 ahci_port_free(sc, port);
463 return (rc);
467 * [re]initialize an idle port. No CCBs should be active. (from port thread)
469 * This function is called during the initial port allocation sequence
470 * and is also called on hot-plug insertion. We take no chances and
471 * use a portreset instead of a softreset.
473 * This function is the only way to move a failed port back to active
474 * status.
476 * Returns 0 if a device is successfully detected.
479 ahci_port_init(struct ahci_port *ap)
481 u_int32_t cmd;
484 * Register [re]initialization
486 * Flush the TFD and SERR and make sure the port is stopped before
487 * enabling its interrupt. We no longer cycle the port start as
488 * the port should not be started unless a device is present.
490 * XXX should we enable FIS reception? (FRE)?
492 ahci_pwrite(ap, AHCI_PREG_IE, 0);
493 ahci_port_stop(ap, 0);
494 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
495 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
496 ahci_flush_tfd(ap);
497 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
500 * If we are being harsh try to kill the port completely. Normally
501 * we would want to hold on to some of the state the BIOS may have
502 * set, such as SUD (spin up device).
504 * AP_F_HARSH_REINIT is cleared in the hard reset state
506 if (ap->ap_flags & AP_F_HARSH_REINIT) {
507 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
508 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
510 ahci_os_sleep(1000);
512 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
513 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
514 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD |
515 AHCI_PREG_CMD_SUD;
516 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
517 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
518 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
519 kprintf("%s: Warning: FRE did not come up during "
520 "harsh reinitialization\n",
521 PORTNAME(ap));
523 ahci_os_sleep(1000);
527 * Clear any pending garbage and re-enable the interrupt before
528 * going to the next stage.
530 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
531 ap->ap_pmcount = 0;
533 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
534 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
535 ahci_flush_tfd(ap);
536 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
537 ahci_pwrite(ap, AHCI_PREG_IS, -1);
539 ahci_port_interrupt_enable(ap);
541 return (0);
545 * Enable or re-enable interrupts on a port.
547 * This routine is called from the port initialization code or from the
548 * helper thread as the real interrupt may be forced to turn off certain
549 * interrupt sources.
551 void
552 ahci_port_interrupt_enable(struct ahci_port *ap)
554 ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
558 * Manage the agressive link power management capability.
560 void
561 ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
563 u_int32_t cmd, sctl;
565 if (link_pwr_mgmt == ap->link_pwr_mgmt)
566 return;
568 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
569 kprintf("%s: link power management not supported.\n",
570 PORTNAME(ap));
571 return;
574 ahci_os_lock_port(ap);
576 if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
577 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
578 kprintf("%s: enabling aggressive link power management.\n",
579 PORTNAME(ap));
581 ap->link_pwr_mgmt = link_pwr_mgmt;
583 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
584 ahci_port_interrupt_enable(ap);
586 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
587 sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED);
588 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
591 * Enable device initiated link power management for
592 * directly attached devices that support it.
594 if (ap->ap_type != ATA_PORT_T_PM &&
595 ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) {
596 if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
597 kprintf("%s: Could not enable device initiated "
598 "link power management.\n",
599 PORTNAME(ap));
602 cmd = ahci_pread(ap, AHCI_PREG_CMD);
603 cmd |= AHCI_PREG_CMD_ASP;
604 cmd |= AHCI_PREG_CMD_ALPE;
605 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
607 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
608 (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
609 kprintf("%s: enabling medium link power management.\n",
610 PORTNAME(ap));
612 ap->link_pwr_mgmt = link_pwr_mgmt;
614 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
615 ahci_port_interrupt_enable(ap);
617 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
618 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
619 sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL;
620 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
622 cmd = ahci_pread(ap, AHCI_PREG_CMD);
623 cmd &= ~AHCI_PREG_CMD_ASP;
624 cmd |= AHCI_PREG_CMD_ALPE;
625 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
627 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
628 kprintf("%s: disabling link power management.\n",
629 PORTNAME(ap));
631 /* Disable device initiated link power management */
632 if (ap->ap_type != ATA_PORT_T_PM &&
633 ap->ap_ata[0]->at_identify.satafsup & (1 << 3))
634 ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
636 cmd = ahci_pread(ap, AHCI_PREG_CMD);
637 cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
638 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
640 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
641 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
642 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
644 /* let the drive come back to avoid PRCS interrupts later */
645 ahci_os_unlock_port(ap);
646 ahci_os_sleep(1000);
647 ahci_os_lock_port(ap);
649 ahci_pwrite(ap, AHCI_PREG_SERR,
650 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
651 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
653 ap->ap_intmask |= AHCI_PREG_IE_PRCE;
654 ahci_port_interrupt_enable(ap);
656 ap->link_pwr_mgmt = link_pwr_mgmt;
657 } else {
658 kprintf("%s: unsupported link power management state %d.\n",
659 PORTNAME(ap), link_pwr_mgmt);
662 ahci_os_unlock_port(ap);
666 * Return current link power state.
669 ahci_port_link_pwr_state(struct ahci_port *ap)
671 uint32_t r;
673 r = ahci_pread(ap, AHCI_PREG_SSTS);
674 switch (r & SATA_PM_SSTS_IPM) {
675 case SATA_PM_SSTS_IPM_ACTIVE:
676 return 1;
677 case SATA_PM_SSTS_IPM_PARTIAL:
678 return 2;
679 case SATA_PM_SSTS_IPM_SLUMBER:
680 return 3;
681 default:
682 return 0;
687 * Run the port / target state machine from a main context.
689 * The state machine for the port is always run.
691 * If atx is non-NULL run the state machine for a particular target.
692 * If atx is NULL run the state machine for all targets.
694 void
695 ahci_port_state_machine(struct ahci_port *ap, int initial)
697 struct ata_port *at;
698 u_int32_t data;
699 int target;
700 int didsleep;
701 int loop;
704 * State machine for port. Note that CAM is not yet associated
705 * during the initial parallel probe and the port's probe state
706 * will not get past ATA_PROBE_NEED_IDENT.
709 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
710 kprintf("%s: Waiting 10 seconds on insertion\n",
711 PORTNAME(ap));
712 ahci_os_sleep(10000);
713 initial = 1;
715 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
716 ahci_port_init(ap);
717 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
718 ahci_port_reset(ap, NULL, 1);
719 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
720 ahci_port_reset(ap, NULL, 0);
721 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
722 ahci_cam_probe(ap, NULL);
724 if (ap->ap_type != ATA_PORT_T_PM) {
725 if (ap->ap_probe == ATA_PROBE_FAILED) {
726 ahci_cam_changed(ap, NULL, 0);
727 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
728 ahci_cam_changed(ap, NULL, 1);
730 return;
734 * Port Multiplier state machine.
736 * Get a mask of changed targets and combine with any runnable
737 * states already present.
739 for (loop = 0; ;++loop) {
740 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
741 kprintf("%s: PM unable to read hot-plug bitmap\n",
742 PORTNAME(ap));
743 break;
747 * Do at least one loop, then stop if no more state changes
748 * have occured. The PM might not generate a new
749 * notification until we clear the entire bitmap.
751 if (loop && data == 0)
752 break;
755 * New devices showing up in the bitmap require some spin-up
756 * time before we start probing them. Reset didsleep. The
757 * first new device we detect will sleep before probing.
759 * This only applies to devices whos change bit is set in
760 * the data, and does not apply to the initial boot-time
761 * probe.
763 didsleep = 0;
765 for (target = 0; target < ap->ap_pmcount; ++target) {
766 at = ap->ap_ata[target];
769 * Check the target state for targets behind the PM
770 * which have changed state. This will adjust
771 * at_probe and set ATA_PORT_F_RESCAN
773 * We want to wait at least 10 seconds before probing
774 * a newly inserted device. If the check status
775 * indicates a device is present and in need of a
776 * hard reset, we make sure we have slept before
777 * continuing.
779 * We also need to wait at least 1 second for the
780 * PHY state to change after insertion, if we
781 * haven't already waited the 10 seconds.
783 * NOTE: When pm_check_good finds a good port it
784 * typically starts us in probe state
785 * NEED_HARD_RESET rather than INIT.
787 if (data & (1 << target)) {
788 if (initial == 0 && didsleep == 0)
789 ahci_os_sleep(1000);
790 ahci_pm_check_good(ap, target);
791 if (initial == 0 && didsleep == 0 &&
792 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
794 didsleep = 1;
795 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
796 ahci_os_sleep(10000);
801 * Report hot-plug events before the probe state
802 * really gets hot. Only actual events are reported
803 * here to reduce spew.
805 if (data & (1 << target)) {
806 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
807 switch(at->at_probe) {
808 case ATA_PROBE_NEED_INIT:
809 case ATA_PROBE_NEED_HARD_RESET:
810 kprintf("Device inserted\n");
811 break;
812 case ATA_PROBE_FAILED:
813 kprintf("Device removed\n");
814 break;
815 default:
816 kprintf("Device probe in progress\n");
817 break;
822 * Run through the state machine as necessary if
823 * the port is not marked failed.
825 * The state machine may stop at NEED_IDENT if
826 * CAM is not yet attached.
828 * Acquire exclusive access to the port while we
829 * are doing this. This prevents command-completion
830 * from queueing commands for non-polled targets
831 * inbetween our probe steps. We need to do this
832 * because the reset probes can generate severe PHY
833 * and protocol errors and soft-brick the port.
835 if (at->at_probe != ATA_PROBE_FAILED &&
836 at->at_probe != ATA_PROBE_GOOD) {
837 ahci_beg_exclusive_access(ap, at);
838 if (at->at_probe == ATA_PROBE_NEED_INIT)
839 ahci_pm_port_init(ap, at);
840 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
841 ahci_port_reset(ap, at, 1);
842 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
843 ahci_port_reset(ap, at, 0);
844 if (at->at_probe == ATA_PROBE_NEED_IDENT)
845 ahci_cam_probe(ap, at);
846 ahci_end_exclusive_access(ap, at);
850 * Add or remove from CAM
852 if (at->at_features & ATA_PORT_F_RESCAN) {
853 at->at_features &= ~ATA_PORT_F_RESCAN;
854 if (at->at_probe == ATA_PROBE_FAILED) {
855 ahci_cam_changed(ap, at, 0);
856 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
857 ahci_cam_changed(ap, at, 1);
860 data &= ~(1 << target);
862 if (data) {
863 kprintf("%s: WARNING (PM): extra bits set in "
864 "EINFO: %08x\n", PORTNAME(ap), data);
865 while (target < AHCI_MAX_PMPORTS) {
866 ahci_pm_check_good(ap, target);
867 ++target;
875 * De-initialize and detach a port.
877 void
878 ahci_port_free(struct ahci_softc *sc, u_int port)
880 struct ahci_port *ap = sc->sc_ports[port];
881 struct ahci_ccb *ccb;
882 int i;
885 * Ensure port is disabled and its interrupts are all flushed.
887 if (ap->ap_sc) {
888 ahci_port_stop(ap, 1);
889 ahci_os_stop_port(ap);
890 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
891 ahci_pwrite(ap, AHCI_PREG_IE, 0);
892 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
893 ahci_write(sc, AHCI_REG_IS, 1 << port);
896 if (ap->ap_ccbs) {
897 while ((ccb = ahci_get_ccb(ap)) != NULL) {
898 if (ccb->ccb_dmamap) {
899 bus_dmamap_destroy(sc->sc_tag_data,
900 ccb->ccb_dmamap);
901 ccb->ccb_dmamap = NULL;
904 if ((ccb = ap->ap_err_ccb) != NULL) {
905 if (ccb->ccb_dmamap) {
906 bus_dmamap_destroy(sc->sc_tag_data,
907 ccb->ccb_dmamap);
908 ccb->ccb_dmamap = NULL;
910 ap->ap_err_ccb = NULL;
912 kfree(ap->ap_ccbs, M_DEVBUF);
913 ap->ap_ccbs = NULL;
916 if (ap->ap_dmamem_cmd_list) {
917 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
918 ap->ap_dmamem_cmd_list = NULL;
920 if (ap->ap_dmamem_rfis) {
921 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
922 ap->ap_dmamem_rfis = NULL;
924 if (ap->ap_dmamem_cmd_table) {
925 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
926 ap->ap_dmamem_cmd_table = NULL;
928 if (ap->ap_ata) {
929 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
930 if (ap->ap_ata[i]) {
931 kfree(ap->ap_ata[i], M_DEVBUF);
932 ap->ap_ata[i] = NULL;
936 if (ap->ap_err_scratch) {
937 kfree(ap->ap_err_scratch, M_DEVBUF);
938 ap->ap_err_scratch = NULL;
941 /* bus_space(9) says we dont free the subregions handle */
943 kfree(ap, M_DEVBUF);
944 sc->sc_ports[port] = NULL;
947 static
948 u_int32_t
949 ahci_pactive(struct ahci_port *ap)
951 u_int32_t mask;
953 mask = ahci_pread(ap, AHCI_PREG_CI);
954 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ)
955 mask |= ahci_pread(ap, AHCI_PREG_SACT);
956 return(mask);
960 * Start high-level command processing on the port
963 ahci_port_start(struct ahci_port *ap)
965 u_int32_t r, s, is, tfd;
968 * FRE must be turned on before ST. Wait for FR to go active
969 * before turning on ST. The spec doesn't seem to think this
970 * is necessary but waiting here avoids an on-off race in the
971 * ahci_port_stop() code.
973 r = ahci_pread(ap, AHCI_PREG_CMD);
974 if ((r & AHCI_PREG_CMD_FRE) == 0) {
975 r |= AHCI_PREG_CMD_FRE;
976 ahci_pwrite(ap, AHCI_PREG_CMD, r);
978 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
979 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
980 kprintf("%s: Cannot start FIS reception\n",
981 PORTNAME(ap));
982 return (2);
984 } else {
985 ahci_os_sleep(10);
989 * Turn on ST, wait for CR to come up.
991 r |= AHCI_PREG_CMD_ST;
992 ahci_pwrite(ap, AHCI_PREG_CMD, r);
993 if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
994 s = ahci_pread(ap, AHCI_PREG_SERR);
995 is = ahci_pread(ap, AHCI_PREG_IS);
996 tfd = ahci_pread(ap, AHCI_PREG_TFD);
997 kprintf("%s: Cannot start command DMA\n"
998 "NCMP=%b NSERR=%b\n"
999 "NEWIS=%b\n"
1000 "NEWTFD=%b\n",
1001 PORTNAME(ap),
1002 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
1003 is, AHCI_PFMT_IS,
1004 tfd, AHCI_PFMT_TFD_STS);
1005 return (1);
1008 #ifdef AHCI_COALESCE
1010 * (Re-)enable coalescing on the port.
1012 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1013 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
1014 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1015 ap->ap_sc->sc_ccc_ports_cur);
1017 #endif
1019 return (0);
1023 * Stop high-level command processing on a port
1025 * WARNING! If the port is stopped while CR is still active our saved
1026 * CI/SACT will race any commands completed by the command
1027 * processor prior to being able to stop. Thus we never call
1028 * this function unless we intend to dispose of any remaining
1029 * active commands. In particular, this complicates the timeout
1030 * code.
1033 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
1035 u_int32_t r;
1037 #ifdef AHCI_COALESCE
1039 * Disable coalescing on the port while it is stopped.
1041 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1042 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
1043 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1044 ap->ap_sc->sc_ccc_ports_cur);
1046 #endif
1049 * Turn off ST, then wait for CR to go off.
1051 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1052 r &= ~AHCI_PREG_CMD_ST;
1053 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1055 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1056 kprintf("%s: Port bricked, unable to stop (ST)\n",
1057 PORTNAME(ap));
1058 return (1);
1061 #if 0
1063 * Turn off FRE, then wait for FR to go off. FRE cannot
1064 * be turned off until CR transitions to 0.
1066 if ((r & AHCI_PREG_CMD_FR) == 0) {
1067 kprintf("%s: FR stopped, clear FRE for next start\n",
1068 PORTNAME(ap));
1069 stop_fis_rx = 2;
1071 #endif
1072 if (stop_fis_rx) {
1073 r &= ~AHCI_PREG_CMD_FRE;
1074 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1075 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
1076 kprintf("%s: Port bricked, unable to stop (FRE)\n",
1077 PORTNAME(ap));
1078 return (2);
1082 return (0);
1086 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1089 ahci_port_clo(struct ahci_port *ap)
1091 struct ahci_softc *sc = ap->ap_sc;
1092 u_int32_t cmd;
1094 /* Only attempt CLO if supported by controller */
1095 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
1096 return (1);
1098 /* Issue CLO */
1099 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1100 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1102 /* Wait for completion */
1103 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1104 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1105 return (1);
1108 return (0);
1112 * Reset a port.
1114 * If hard is 0 perform a softreset of the port.
1115 * If hard is 1 perform a hard reset of the port.
1117 * If at is non-NULL an indirect port via a port-multiplier is being
1118 * reset, otherwise a direct port is being reset.
1120 * NOTE: Indirect ports can only be soft-reset.
1123 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
1125 int rc;
1127 if (hard) {
1128 if (at)
1129 rc = ahci_pm_hardreset(ap, at->at_target, hard);
1130 else
1131 rc = ahci_port_hardreset(ap, hard);
1132 } else {
1133 if (at)
1134 rc = ahci_pm_softreset(ap, at->at_target);
1135 else
1136 rc = ahci_port_softreset(ap);
1138 return(rc);
1142 * AHCI soft reset, Section 10.4.1
1144 * (at) will be NULL when soft-resetting a directly-attached device, and
1145 * non-NULL when soft-resetting a device through a port multiplier.
1147 * This function keeps port communications intact and attempts to generate
1148 * a reset to the connected device using device commands.
1151 ahci_port_softreset(struct ahci_port *ap)
1153 struct ahci_ccb *ccb = NULL;
1154 struct ahci_cmd_hdr *cmd_slot;
1155 u_int8_t *fis;
1156 int error;
1158 error = EIO;
1160 if (bootverbose) {
1161 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
1162 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
1165 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1167 crit_enter();
1168 ap->ap_flags |= AP_F_IN_RESET;
1169 ap->ap_state = AP_S_NORMAL;
1172 * Remember port state in cmd (main to restore start/stop)
1174 * Idle port.
1176 if (ahci_port_stop(ap, 0)) {
1177 kprintf("%s: failed to stop port, cannot softreset\n",
1178 PORTNAME(ap));
1179 goto err;
1183 * Request CLO if device appears hung.
1185 if (ahci_pread(ap, AHCI_PREG_TFD) &
1186 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1187 ahci_port_clo(ap);
1191 * This is an attempt to clear errors so a new signature will
1192 * be latched. It isn't working properly. XXX
1194 ahci_flush_tfd(ap);
1195 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1197 /* Restart port */
1198 if (ahci_port_start(ap)) {
1199 kprintf("%s: failed to start port, cannot softreset\n",
1200 PORTNAME(ap));
1201 goto err;
1204 /* Check whether CLO worked */
1205 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1206 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1207 kprintf("%s: CLO %s, need port reset\n",
1208 PORTNAME(ap),
1209 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1210 ? "failed" : "unsupported");
1211 error = EBUSY;
1212 goto err;
1216 * Prep first D2H command with SRST feature & clear busy/reset flags
1218 * It is unclear which other fields in the FIS are used. Just zero
1219 * everything.
1221 * NOTE! This CCB is used for both the first and second commands.
1222 * The second command must use CCB slot 1 to properly load
1223 * the signature.
1225 ccb = ahci_get_err_ccb(ap);
1226 ccb->ccb_xa.complete = ahci_dummy_done;
1227 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
1228 KKASSERT(ccb->ccb_slot == 1);
1229 ccb->ccb_xa.at = NULL;
1230 cmd_slot = ccb->ccb_cmd_hdr;
1232 fis = ccb->ccb_cmd_table->cfis;
1233 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1234 fis[0] = ATA_FIS_TYPE_H2D;
1235 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1237 cmd_slot->prdtl = 0;
1238 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1239 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1240 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1242 ccb->ccb_xa.state = ATA_S_PENDING;
1244 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1245 kprintf("%s: First FIS failed\n", PORTNAME(ap));
1246 goto err;
1250 * WARNING! TIME SENSITIVE SPACE! WARNING!
1252 * The two FISes are supposed to be back to back. Don't issue other
1253 * commands or even delay if we can help it.
1257 * Prep second D2H command to read status and complete reset sequence
1258 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1259 * Rev 2.6 and it is unclear how the second FIS should be set up
1260 * from the AHCI document.
1262 * It is unclear which other fields in the FIS are used. Just zero
1263 * everything.
1265 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
1267 bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
1268 fis[0] = ATA_FIS_TYPE_H2D;
1269 fis[15] = ATA_FIS_CONTROL_4BIT;
1271 cmd_slot->prdtl = 0;
1272 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1274 ccb->ccb_xa.state = ATA_S_PENDING;
1275 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1276 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1277 goto err;
1280 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1281 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1282 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1283 PORTNAME(ap),
1284 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
1285 error = EBUSY;
1286 goto err;
1290 * If the softreset is trying to clear a BSY condition after a
1291 * normal portreset we assign the port type.
1293 * If the softreset is being run first as part of the ccb error
1294 * processing code then report if the device signature changed
1295 * unexpectedly.
1297 ahci_os_sleep(100);
1298 if (ap->ap_type == ATA_PORT_T_NONE) {
1299 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1300 } else {
1301 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1302 kprintf("%s: device signature unexpectedly "
1303 "changed\n", PORTNAME(ap));
1304 error = EBUSY; /* XXX */
1307 error = 0;
1309 ahci_os_sleep(3);
1310 err:
1311 if (ccb != NULL) {
1312 ahci_put_err_ccb(ccb);
1315 * If the target is busy use CLO to clear the busy
1316 * condition. The BSY should be cleared on the next
1317 * start.
1319 if (ahci_pread(ap, AHCI_PREG_TFD) &
1320 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1321 ahci_port_clo(ap);
1326 * If we failed to softreset make the port quiescent, otherwise
1327 * make sure the port's start/stop state matches what it was on
1328 * entry.
1330 * Don't kill the port if the softreset is on a port multiplier
1331 * target, that would kill all the targets!
1333 if (error) {
1334 ahci_port_hardstop(ap);
1335 /* ap_probe set to failed */
1336 } else {
1337 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1338 ap->ap_pmcount = 1;
1339 ahci_port_start(ap);
1341 ap->ap_flags &= ~AP_F_IN_RESET;
1342 crit_exit();
1344 if (bootverbose)
1345 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1347 return (error);
1351 * Issue just do the core COMRESET and basic device detection on a port.
1353 * NOTE: Only called by ahci_port_hardreset().
1355 static int
1356 ahci_comreset(struct ahci_port *ap, int *pmdetectp)
1358 u_int32_t cmd;
1359 u_int32_t r;
1360 int error;
1361 int loop;
1362 int retries = 0;
1365 * Idle the port,
1367 *pmdetectp = 0;
1368 ahci_port_stop(ap, 0);
1369 ap->ap_state = AP_S_NORMAL;
1370 ahci_os_sleep(10);
1373 * The port may have been quiescent with its SUD bit cleared, so
1374 * set the SUD (spin up device).
1376 * NOTE: I do not know if SUD is a hardware pin/low-level signal
1377 * or if it is messaged.
1379 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1381 cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD;
1382 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1383 ahci_os_sleep(10);
1386 * Make sure that all power management is disabled.
1388 * NOTE! AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable
1389 * on multiple chipsets and can brick the chipset or even
1390 * the whole PC. Never use it.
1392 ap->ap_type = ATA_PORT_T_NONE;
1394 r = AHCI_PREG_SCTL_IPM_DISABLED |
1395 AHCI_PREG_SCTL_SPM_DISABLED;
1396 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1398 retry:
1400 * Give the new power management state time to settle, then clear
1401 * pending status.
1403 ahci_os_sleep(1000);
1404 ahci_flush_tfd(ap);
1405 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1408 * Start transmitting COMRESET. The spec says that COMRESET must
1409 * be sent for at least 1ms but in actual fact numerous devices
1410 * appear to take much longer. Delay a whole second here.
1412 * In addition, SATA-3 ports can take longer to train, so even
1413 * SATA-2 devices which would normally detect very quickly may
1414 * take longer when plugged into a SATA-3 port.
1416 r |= AHCI_PREG_SCTL_DET_INIT;
1417 switch(AhciForceGen) {
1418 case 0:
1419 r |= AHCI_PREG_SCTL_SPD_ANY;
1420 break;
1421 case 1:
1422 r |= AHCI_PREG_SCTL_SPD_GEN1;
1423 break;
1424 case 2:
1425 r |= AHCI_PREG_SCTL_SPD_GEN2;
1426 break;
1427 case 3:
1428 r |= AHCI_PREG_SCTL_SPD_GEN3;
1429 break;
1430 default:
1431 r |= AHCI_PREG_SCTL_SPD_GEN3;
1432 break;
1434 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1435 ahci_os_sleep(1000);
1437 ap->ap_flags &= ~AP_F_HARSH_REINIT;
1440 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1441 * since we are hard-resetting the port we might as well clear
1442 * the whole enchillada. Also be sure to clear any spurious BSY
1443 * prior to clearing INIT.
1445 * Wait 1 whole second after clearing INIT before checking
1446 * the device detection bits in an attempt to work around chipsets
1447 * which do not properly mask PCS/PRCS during low level init.
1449 ahci_flush_tfd(ap);
1450 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1451 /* ahci_port_clo(ap);*/
1452 ahci_os_sleep(10);
1454 r &= ~AHCI_PREG_SCTL_SPD;
1455 r &= ~AHCI_PREG_SCTL_DET_INIT;
1456 r |= AHCI_PREG_SCTL_DET_NONE;
1457 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1458 ahci_os_sleep(1000);
1461 * Try to determine if there is a device on the port.
1463 * Give the device 3/10 second to at least be detected.
1464 * If we fail clear PRCS (phy detect) since we may cycled
1465 * the phy and probably caused another PRCS interrupt.
1467 loop = 300;
1468 while (loop > 0) {
1469 r = ahci_pread(ap, AHCI_PREG_SSTS);
1470 if (r & AHCI_PREG_SSTS_DET)
1471 break;
1472 loop -= ahci_os_softsleep();
1474 if (loop == 0) {
1475 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1476 if (bootverbose) {
1477 kprintf("%s: Port appears to be unplugged\n",
1478 PORTNAME(ap));
1480 error = ENODEV;
1481 goto done;
1485 * There is something on the port. Regardless of what happens
1486 * after this tell the caller to try to detect a port multiplier.
1488 * Give the device 3 seconds to fully negotiate.
1490 *pmdetectp = 1;
1492 if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1493 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1494 if (bootverbose) {
1495 kprintf("%s: Device may be powered down\n",
1496 PORTNAME(ap));
1498 error = ENODEV;
1499 goto done;
1503 * We got something that definitely looks like a device. Give
1504 * the device time to send us its first D2H FIS. Waiting for
1505 * BSY to clear accomplishes this.
1507 * NOTE: A port multiplier may or may not clear BSY here,
1508 * depending on what is sitting in target 0 behind it.
1510 * NOTE: Intel SSDs seem to have compatibility problems with Intel
1511 * mobo's on cold boots and may leave BSY set. A single
1512 * retry works around the problem. This is definitely a bug
1513 * with the mobo and/or the SSD and does not appear to occur
1514 * with other devices connected to the same port.
1516 ahci_flush_tfd(ap);
1517 if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD,
1518 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1519 kprintf("%s: Device BUSY: %b\n",
1520 PORTNAME(ap),
1521 ahci_pread(ap, AHCI_PREG_TFD),
1522 AHCI_PFMT_TFD_STS);
1523 if (retries == 0) {
1524 kprintf("%s: Retrying\n", PORTNAME(ap));
1525 retries = 1;
1526 goto retry;
1528 error = EBUSY;
1529 } else {
1530 error = 0;
1533 done:
1534 ahci_flush_tfd(ap);
1535 return error;
1540 * AHCI port reset, Section 10.4.2
1542 * This function does a hard reset of the port. Note that the device
1543 * connected to the port could still end-up hung.
1546 ahci_port_hardreset(struct ahci_port *ap, int hard)
1548 u_int32_t data;
1549 int error;
1550 int pmdetect;
1552 if (bootverbose)
1553 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1554 ap->ap_flags |= AP_F_IN_RESET;
1556 error = ahci_comreset(ap, &pmdetect);
1559 * We may be asked to perform a port multiplier check even if the
1560 * comreset failed. This typically occurs when the PM has nothing
1561 * in slot 0, which can cause BSY to remain set.
1563 * If the PM detection is successful it will override (error),
1564 * otherwise (error) is retained. If an error does occur it
1565 * is possible that a normal device has blown up on us DUE to
1566 * the PM detection code, so re-run the comreset and assume
1567 * a normal device.
1569 if (pmdetect) {
1570 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) {
1571 error = ahci_pm_port_probe(ap, error);
1572 if (error) {
1573 error = ahci_comreset(ap, &pmdetect);
1579 * Finish up.
1581 ahci_os_sleep(500);
1583 switch(error) {
1584 case 0:
1586 * All good, make sure the port is running and set the
1587 * probe state. Ignore the signature junk (it's unreliable)
1588 * until we get to the softreset code.
1590 if (ahci_port_start(ap)) {
1591 kprintf("%s: failed to start command DMA on port, "
1592 "disabling\n", PORTNAME(ap));
1593 error = EBUSY;
1594 break;
1596 if (ap->ap_type == ATA_PORT_T_PM)
1597 ap->ap_probe = ATA_PROBE_GOOD;
1598 else
1599 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1600 break;
1601 case ENODEV:
1603 * Normal device probe failure
1605 data = ahci_pread(ap, AHCI_PREG_SSTS);
1607 switch(data & AHCI_PREG_SSTS_DET) {
1608 case AHCI_PREG_SSTS_DET_DEV_NE:
1609 kprintf("%s: Device not communicating\n",
1610 PORTNAME(ap));
1611 break;
1612 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
1613 kprintf("%s: PHY offline\n",
1614 PORTNAME(ap));
1615 break;
1616 default:
1617 kprintf("%s: No device detected\n",
1618 PORTNAME(ap));
1619 break;
1621 ahci_port_hardstop(ap);
1622 break;
1623 default:
1625 * Abnormal probe (EBUSY)
1627 kprintf("%s: Device on port is bricked\n",
1628 PORTNAME(ap));
1629 ahci_port_hardstop(ap);
1630 #if 0
1631 rc = ahci_port_reset(ap, atx, 0);
1632 if (rc) {
1633 kprintf("%s: Unable unbrick device\n",
1634 PORTNAME(ap));
1635 } else {
1636 kprintf("%s: Successfully unbricked\n",
1637 PORTNAME(ap));
1639 #endif
1640 break;
1644 * Clean up
1646 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1647 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
1649 ap->ap_flags &= ~AP_F_IN_RESET;
1651 if (bootverbose)
1652 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1653 return (error);
1657 * Hard-stop on hot-swap device removal. See 10.10.1
1659 * Place the port in a mode that will allow it to detect hot-swap insertions.
1660 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1661 * seem to do the job.
1663 * FIS reception is left enabled but command processing is disabled.
1664 * Cycling FIS reception (FRE) can brick ports.
1666 void
1667 ahci_port_hardstop(struct ahci_port *ap)
1669 struct ahci_ccb *ccb;
1670 struct ata_port *at;
1671 u_int32_t r;
1672 u_int32_t cmd;
1673 int slot;
1674 int i;
1677 * Stop the port. We can't modify things like SUD if the port
1678 * is running.
1680 ap->ap_state = AP_S_FATAL_ERROR;
1681 ap->ap_probe = ATA_PROBE_FAILED;
1682 ap->ap_type = ATA_PORT_T_NONE;
1683 ahci_port_stop(ap, 0);
1684 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1685 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC);
1686 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1689 * Clean up AT sub-ports on SATA port.
1691 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1692 at = ap->ap_ata[i];
1693 at->at_type = ATA_PORT_T_NONE;
1694 at->at_probe = ATA_PROBE_FAILED;
1698 * Make sure FRE is active. There isn't anything we can do if it
1699 * fails so just ignore errors.
1701 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1702 cmd |= AHCI_PREG_CMD_FRE;
1703 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1704 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1705 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1709 * 10.10.1 place us in the Listen state.
1711 * 10.10.3 DET must be set to 0 and found to be 0 before
1712 * setting SUD to 0.
1714 * Deactivating SUD only applies if the controller supports SUD, it
1715 * is a bit unclear what happens w/regards to detecting hotplug
1716 * if it doesn't.
1718 r = AHCI_PREG_SCTL_IPM_DISABLED |
1719 AHCI_PREG_SCTL_SPM_DISABLED;
1720 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1721 ahci_os_sleep(10);
1722 cmd &= ~AHCI_PREG_CMD_SUD;
1723 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1724 ahci_os_sleep(10);
1727 * 10.10.1
1729 * Transition su to the spin-up state. HBA shall send COMRESET and
1730 * begin initialization sequence (whatever that means). Presumably
1731 * this is edge-triggered. Following the spin-up state the HBA
1732 * will automatically transition to the Normal state.
1734 * This only applies if the controller supports SUD.
1735 * NEVER use AHCI_PREG_DET_DISABLE.
1737 cmd |= AHCI_PREG_CMD_POD |
1738 AHCI_PREG_CMD_SUD |
1739 AHCI_PREG_CMD_ICC_ACTIVE;
1740 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1741 ahci_os_sleep(10);
1744 * Flush SERR_DIAG_X so the TFD can update.
1746 ahci_flush_tfd(ap);
1749 * Clean out pending ccbs
1751 while (ap->ap_active) {
1752 slot = ffs(ap->ap_active) - 1;
1753 ap->ap_active &= ~(1 << slot);
1754 ap->ap_expired &= ~(1 << slot);
1755 --ap->ap_active_cnt;
1756 ccb = &ap->ap_ccbs[slot];
1757 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1758 callout_stop(&ccb->ccb_timeout);
1759 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1761 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1762 ATA_F_TIMEOUT_EXPIRED);
1763 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1764 ccb->ccb_done(ccb);
1765 ccb->ccb_xa.complete(&ccb->ccb_xa);
1767 while (ap->ap_sactive) {
1768 slot = ffs(ap->ap_sactive) - 1;
1769 ap->ap_sactive &= ~(1 << slot);
1770 ap->ap_expired &= ~(1 << slot);
1771 ccb = &ap->ap_ccbs[slot];
1772 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1773 callout_stop(&ccb->ccb_timeout);
1774 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1776 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1777 ATA_F_TIMEOUT_EXPIRED);
1778 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1779 ccb->ccb_done(ccb);
1780 ccb->ccb_xa.complete(&ccb->ccb_xa);
1782 KKASSERT(ap->ap_active_cnt == 0);
1784 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1785 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1786 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1787 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1788 ccb->ccb_done(ccb);
1789 ccb->ccb_xa.complete(&ccb->ccb_xa);
1793 * Hot-plug device detection should work at this point. e.g. on
1794 * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug
1795 * detection and entering RESET (continuous COMRESET by setting INIT)
1796 * will actually prevent hot-plug detection from working properly.
1798 * There may be cases where this will fail to work, I have some
1799 * additional code to place the HBA in RESET (send continuous
1800 * COMRESET) and hopefully get DIAG.X or other events when something
1801 * is plugged in. Unfortunately this isn't universal and can
1802 * also prevent events from generating interrupts.
1805 #if 0
1807 * Transition us to the Reset state. Theoretically we send a
1808 * continuous stream of COMRESETs in this state.
1810 r |= AHCI_PREG_SCTL_DET_INIT;
1811 if (AhciForceGen1 & (1 << ap->ap_num)) {
1812 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1813 r |= AHCI_PREG_SCTL_SPD_GEN1;
1814 } else {
1815 r |= AHCI_PREG_SCTL_SPD_ANY;
1817 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1818 ahci_os_sleep(10);
1821 * Flush SERR_DIAG_X so the TFD can update.
1823 ahci_flush_tfd(ap);
1824 #endif
1825 /* NOP */
1829 * We can't loop on the X bit, a continuous COMINIT received will make
1830 * it loop forever. Just assume one event has built up and clear X
1831 * so the task file descriptor can update.
1833 void
1834 ahci_flush_tfd(struct ahci_port *ap)
1836 u_int32_t r;
1838 r = ahci_pread(ap, AHCI_PREG_SERR);
1839 if (r & AHCI_PREG_SERR_DIAG_X)
1840 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1844 * Figure out what type of device is connected to the port, ATAPI or
1845 * DISK.
1848 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1850 u_int32_t sig;
1852 sig = ahci_pread(ap, AHCI_PREG_SIG);
1853 if (bootverbose)
1854 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1855 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1856 return(ATA_PORT_T_ATAPI);
1857 } else if ((sig & 0xffff0000) ==
1858 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1859 return(ATA_PORT_T_PM);
1860 } else {
1861 return(ATA_PORT_T_DISK);
1866 * Load the DMA descriptor table for a CCB's buffer.
1869 ahci_load_prdt(struct ahci_ccb *ccb)
1871 struct ahci_port *ap = ccb->ccb_port;
1872 struct ahci_softc *sc = ap->ap_sc;
1873 struct ata_xfer *xa = &ccb->ccb_xa;
1874 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1875 bus_dmamap_t dmap = ccb->ccb_dmamap;
1876 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1877 int error;
1879 if (xa->datalen == 0) {
1880 ccb->ccb_cmd_hdr->prdtl = 0;
1881 return (0);
1884 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1885 xa->data, xa->datalen,
1886 ahci_load_prdt_callback,
1887 &prdt,
1888 ((xa->flags & ATA_F_NOWAIT) ?
1889 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1890 if (error != 0) {
1891 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1892 return (1);
1894 #if 0
1895 if (xa->flags & ATA_F_PIO)
1896 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1897 #endif
1899 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1901 if (xa->flags & ATA_F_READ)
1902 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1903 if (xa->flags & ATA_F_WRITE)
1904 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1906 return (0);
1910 * Callback from BUSDMA system to load the segment list. The passed segment
1911 * list is a temporary structure.
1913 static
1914 void
1915 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1916 int error)
1918 struct ahci_prdt *prd = *(void **)info;
1919 u_int64_t addr;
1921 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1923 while (nsegs) {
1924 addr = segs->ds_addr;
1925 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1926 prd->dba_lo = htole32((u_int32_t)addr);
1927 prd->flags = htole32(segs->ds_len - 1);
1928 --nsegs;
1929 if (nsegs)
1930 ++prd;
1931 ++segs;
1933 *(void **)info = prd; /* return last valid segment */
1936 void
1937 ahci_unload_prdt(struct ahci_ccb *ccb)
1939 struct ahci_port *ap = ccb->ccb_port;
1940 struct ahci_softc *sc = ap->ap_sc;
1941 struct ata_xfer *xa = &ccb->ccb_xa;
1942 bus_dmamap_t dmap = ccb->ccb_dmamap;
1944 if (xa->datalen != 0) {
1945 if (xa->flags & ATA_F_READ) {
1946 bus_dmamap_sync(sc->sc_tag_data, dmap,
1947 BUS_DMASYNC_POSTREAD);
1949 if (xa->flags & ATA_F_WRITE) {
1950 bus_dmamap_sync(sc->sc_tag_data, dmap,
1951 BUS_DMASYNC_POSTWRITE);
1953 bus_dmamap_unload(sc->sc_tag_data, dmap);
1956 * prdbc is only updated by hardware for non-NCQ commands.
1958 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1959 xa->resid = 0;
1960 } else {
1961 #if 0
1962 if (ccb->ccb_cmd_hdr->prdbc == 0 &&
1963 ccb->ccb_xa.state == ATA_S_COMPLETE) {
1964 kprintf("%s: WARNING! Unload prdbc resid "
1965 "was zero! tag=%d\n",
1966 ATANAME(ap, xa->at), ccb->ccb_slot);
1968 #endif
1969 xa->resid = xa->datalen -
1970 le32toh(ccb->ccb_cmd_hdr->prdbc);
1976 * Start a command and poll for completion.
1978 * timeout is in ms and only counts once the command gets on-chip.
1980 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1981 * that no error occured.
1983 * NOTE: If the caller specifies a NULL timeout function the caller is
1984 * responsible for clearing hardware state on failure, but we will
1985 * deal with removing the ccb from any pending queue.
1987 * NOTE: NCQ should never be used with this function.
1989 * NOTE: If the port is in a failed state and stopped we do not try
1990 * to activate the ccb.
1993 ahci_poll(struct ahci_ccb *ccb, int timeout,
1994 void (*timeout_fn)(struct ahci_ccb *))
1996 struct ahci_port *ap = ccb->ccb_port;
1998 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1999 ccb->ccb_xa.state = ATA_S_ERROR;
2000 return(ccb->ccb_xa.state);
2002 crit_enter();
2003 #if 0
2004 kprintf("%s: Start command %02x tag=%d\n",
2005 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2006 ccb->ccb_xa.fis->command, ccb->ccb_slot);
2007 #endif
2008 ahci_start(ccb);
2010 do {
2011 ahci_port_intr(ap, 1);
2012 switch(ccb->ccb_xa.state) {
2013 case ATA_S_ONCHIP:
2014 timeout -= ahci_os_softsleep();
2015 break;
2016 case ATA_S_PENDING:
2017 ahci_os_softsleep();
2018 ahci_check_active_timeouts(ap);
2019 break;
2020 default:
2021 crit_exit();
2022 return (ccb->ccb_xa.state);
2024 } while (timeout > 0);
2026 if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) {
2027 kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
2028 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
2029 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2030 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
2031 ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
2034 timeout_fn(ccb);
2036 crit_exit();
2038 return(ccb->ccb_xa.state);
2042 * When polling we have to check if the currently active CCB(s)
2043 * have timed out as the callout will be deadlocked while we
2044 * hold the port lock.
2046 void
2047 ahci_check_active_timeouts(struct ahci_port *ap)
2049 struct ahci_ccb *ccb;
2050 u_int32_t mask;
2051 int tag;
2053 mask = ap->ap_active | ap->ap_sactive;
2054 while (mask) {
2055 tag = ffs(mask) - 1;
2056 mask &= ~(1 << tag);
2057 ccb = &ap->ap_ccbs[tag];
2058 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2059 ahci_ata_cmd_timeout(ccb);
2064 static
2065 __inline
2066 void
2067 ahci_start_timeout(struct ahci_ccb *ccb)
2069 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
2070 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
2071 callout_reset(&ccb->ccb_timeout,
2072 (ccb->ccb_xa.timeout * hz + 999) / 1000,
2073 ahci_ata_cmd_timeout_unserialized, ccb);
2077 void
2078 ahci_start(struct ahci_ccb *ccb)
2080 struct ahci_port *ap = ccb->ccb_port;
2081 struct ahci_softc *sc = ap->ap_sc;
2083 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2085 /* Zero transferred byte count before transfer */
2086 ccb->ccb_cmd_hdr->prdbc = 0;
2088 /* Sync command list entry and corresponding command table entry */
2089 bus_dmamap_sync(sc->sc_tag_cmdh,
2090 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2091 BUS_DMASYNC_PREWRITE);
2092 bus_dmamap_sync(sc->sc_tag_cmdt,
2093 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2094 BUS_DMASYNC_PREWRITE);
2096 /* Prepare RFIS area for write by controller */
2097 bus_dmamap_sync(sc->sc_tag_rfis,
2098 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2099 BUS_DMASYNC_PREREAD);
2102 * There's no point trying to optimize this, it only shaves a few
2103 * nanoseconds so just queue the command and call our generic issue.
2105 ahci_issue_pending_commands(ap, ccb);
2109 * While holding the port lock acquire exclusive access to the port.
2111 * This is used when running the state machine to initialize and identify
2112 * targets over a port multiplier. Setting exclusive access prevents
2113 * ahci_port_intr() from activating any requests sitting on the pending
2114 * queue.
2116 void
2117 ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2119 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
2120 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
2121 while (ap->ap_active || ap->ap_sactive) {
2122 ahci_port_intr(ap, 1);
2123 ahci_os_softsleep();
2127 void
2128 ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2130 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2131 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
2132 ahci_issue_pending_commands(ap, NULL);
2135 #if 0
2137 static void
2138 fubar(struct ahci_ccb *ccb)
2140 struct ahci_port *ap = ccb->ccb_port;
2141 struct ahci_cmd_hdr *cmd;
2142 struct ahci_cmd_table *tab;
2143 struct ahci_prdt *prdt;
2144 int i;
2146 kprintf("%s: ISSUE %02x\n",
2147 ATANAME(ap, ccb->ccb_xa.at),
2148 ccb->ccb_xa.fis->command);
2149 cmd = ccb->ccb_cmd_hdr;
2150 tab = ccb->ccb_cmd_table;
2151 prdt = ccb->ccb_cmd_table->prdt;
2152 kprintf("cmd flags=%04x prdtl=%d prdbc=%d ctba=%08x%08x\n",
2153 cmd->flags, cmd->prdtl, cmd->prdbc,
2154 cmd->ctba_hi, cmd->ctba_lo);
2155 for (i = 0; i < cmd->prdtl; ++i) {
2156 kprintf("\t%d dba=%08x%08x res=%08x flags=%08x\n",
2157 i, prdt->dba_hi, prdt->dba_lo, prdt->reserved,
2158 prdt->flags);
2160 kprintf("tab\n");
2163 #endif
2166 * If ccb is not NULL enqueue and/or issue it.
2168 * If ccb is NULL issue whatever we can from the queue. However, nothing
2169 * new is issued if the exclusive access flag is set or expired ccb's are
2170 * present.
2172 * If existing commands are still active (ap_active/ap_sactive) we can only
2173 * issue matching new commands.
2175 void
2176 ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
2178 u_int32_t mask;
2179 int limit;
2182 * Enqueue the ccb.
2184 * If just running the queue and in exclusive access mode we
2185 * just return. Also in this case if there are any expired ccb's
2186 * we want to clear the queue so the port can be safely stopped.
2188 if (ccb) {
2189 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
2190 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
2191 return;
2195 * Pull the next ccb off the queue and run it if possible.
2197 if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
2198 return;
2201 * Handle exclusivity requirements.
2203 * ATA_F_EXCLUSIVE is used when we want to be the only command
2204 * running.
2206 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
2207 * back into the ccb on a normal (non-errored) command completion.
2208 * For example, for PM requests to target 15. Because the AHCI
2209 * spec does not stop the command processor and has only one rfis
2210 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
2211 * Otherwise multiple completions can destroy the rfis data before
2212 * we have a chance to copy it.
2214 if (ap->ap_active & ~ap->ap_expired) {
2216 * There may be multiple ccb's already running,
2217 * if any are running and ap_run_flags sets
2218 * one of these flags then we know only one is
2219 * running.
2221 * XXX Current AUTOSENSE code forces exclusivity
2222 * to simplify the code.
2224 if (ap->ap_run_flags &
2225 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2226 return;
2229 if (ccb->ccb_xa.flags &
2230 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2231 return;
2235 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2237 * The next command is a NCQ command and can be issued as
2238 * long as currently active commands are not standard.
2240 if (ap->ap_active) {
2241 KKASSERT(ap->ap_active_cnt > 0);
2242 return;
2244 KKASSERT(ap->ap_active_cnt == 0);
2246 mask = 0;
2247 do {
2248 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2249 mask |= 1 << ccb->ccb_slot;
2250 ccb->ccb_xa.state = ATA_S_ONCHIP;
2251 ahci_start_timeout(ccb);
2252 ap->ap_run_flags = ccb->ccb_xa.flags;
2253 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2254 } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
2255 (ap->ap_run_flags &
2256 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
2258 ap->ap_sactive |= mask;
2259 ahci_pwrite(ap, AHCI_PREG_SACT, mask);
2260 ahci_pwrite(ap, AHCI_PREG_CI, mask);
2261 } else {
2263 * The next command is a standard command and can be issued
2264 * as long as currently active commands are not NCQ.
2266 * We limit ourself to 1 command if we have a port multiplier,
2267 * (at least without FBSS support), otherwise timeouts on
2268 * one port can race completions on other ports (see
2269 * ahci_ata_cmd_timeout() for more information).
2271 * If not on a port multiplier generally allow up to 4
2272 * standard commands to be enqueued. Remember that the
2273 * command processor will still process them sequentially.
2275 if (ap->ap_sactive)
2276 return;
2277 if (ap->ap_type == ATA_PORT_T_PM)
2278 limit = 1;
2279 else if (ap->ap_sc->sc_ncmds > 4)
2280 limit = 4;
2281 else
2282 limit = 2;
2284 while (ap->ap_active_cnt < limit && ccb &&
2285 (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
2286 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2287 #if 0
2288 fubar(ccb);
2289 #endif
2290 ap->ap_active |= 1 << ccb->ccb_slot;
2291 ap->ap_active_cnt++;
2292 ap->ap_run_flags = ccb->ccb_xa.flags;
2293 ccb->ccb_xa.state = ATA_S_ONCHIP;
2294 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
2295 ahci_start_timeout(ccb);
2296 if ((ap->ap_run_flags &
2297 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
2298 break;
2300 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2301 if (ccb && (ccb->ccb_xa.flags &
2302 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
2303 break;
2309 void
2310 ahci_intr(void *arg)
2312 struct ahci_softc *sc = arg;
2313 struct ahci_port *ap;
2314 u_int32_t is;
2315 u_int32_t ack;
2316 int port;
2319 * Check if the master enable is up, and whether any interrupts are
2320 * pending.
2322 if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2323 return;
2324 is = ahci_read(sc, AHCI_REG_IS);
2325 if (is == 0 || is == 0xffffffff) {
2326 return;
2328 is &= sc->sc_portmask;
2330 #ifdef AHCI_COALESCE
2331 /* Check coalescing interrupt first */
2332 if (is & sc->sc_ccc_mask) {
2333 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2334 DEVNAME(sc));
2335 is &= ~sc->sc_ccc_mask;
2336 is |= sc->sc_ccc_ports_cur;
2338 #endif
2341 * Process interrupts for each port in a non-blocking fashion.
2343 * The global IS bit is forced on if any unmasked port interrupts
2344 * are pending, even if we clear.
2346 for (ack = 0; is; is &= ~(1 << port)) {
2347 port = ffs(is) - 1;
2348 ack |= 1 << port;
2350 ap = sc->sc_ports[port];
2351 if (ap == NULL)
2352 continue;
2354 if (ahci_os_lock_port_nb(ap) == 0) {
2355 ahci_port_intr(ap, 0);
2356 ahci_os_unlock_port(ap);
2357 } else {
2358 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2359 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2362 ahci_write(sc, AHCI_REG_IS, ack);
2366 * Core called from helper thread.
2368 void
2369 ahci_port_thread_core(struct ahci_port *ap, int mask)
2372 * Process any expired timedouts.
2374 ahci_os_lock_port(ap);
2375 if (mask & AP_SIGF_TIMEOUT) {
2376 ahci_check_active_timeouts(ap);
2380 * Process port interrupts which require a higher level of
2381 * intervention.
2383 if (mask & AP_SIGF_PORTINT) {
2384 ahci_port_intr(ap, 1);
2385 ahci_port_interrupt_enable(ap);
2386 ahci_os_unlock_port(ap);
2387 } else if (ap->ap_probe != ATA_PROBE_FAILED) {
2388 ahci_port_intr(ap, 1);
2389 ahci_port_interrupt_enable(ap);
2390 ahci_os_unlock_port(ap);
2391 } else {
2392 ahci_os_unlock_port(ap);
2397 * Core per-port interrupt handler.
2399 * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2400 * deal with normal command completions which do not require blocking.
2402 void
2403 ahci_port_intr(struct ahci_port *ap, int blockable)
2405 struct ahci_softc *sc = ap->ap_sc;
2406 u_int32_t is, ci_saved, ci_masked;
2407 int slot;
2408 int stopped = 0;
2409 struct ahci_ccb *ccb = NULL;
2410 struct ata_port *ccb_at = NULL;
2411 volatile u_int32_t *active;
2412 const u_int32_t blockable_mask = AHCI_PREG_IS_TFES |
2413 AHCI_PREG_IS_IFS |
2414 AHCI_PREG_IS_PCS |
2415 AHCI_PREG_IS_PRCS |
2416 AHCI_PREG_IS_HBFS |
2417 AHCI_PREG_IS_OFS |
2418 AHCI_PREG_IS_UFS;
2420 enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART,
2421 NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2424 * All basic command completions are always processed.
2426 is = ahci_pread(ap, AHCI_PREG_IS);
2427 if (is & AHCI_PREG_IS_DPS)
2428 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2431 * If we can't block then we can't handle these here. Disable
2432 * the interrupts in question so we don't live-lock, the helper
2433 * thread will re-enable them.
2435 * If the port is in a completely failed state we do not want
2436 * to drop through to failed-command-processing if blockable is 0,
2437 * just let the thread deal with it all.
2439 * Otherwise we fall through and still handle DHRS and any commands
2440 * which completed normally. Even if we are errored we haven't
2441 * stopped the port yet so CI/SACT are still good.
2443 if (blockable == 0) {
2444 if (ap->ap_state == AP_S_FATAL_ERROR) {
2445 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2446 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2447 return;
2449 if (is & blockable_mask) {
2450 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2451 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2452 return;
2457 * Either NCQ or non-NCQ commands will be active, never both.
2459 if (ap->ap_sactive) {
2460 KKASSERT(ap->ap_active == 0);
2461 KKASSERT(ap->ap_active_cnt == 0);
2462 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2463 active = &ap->ap_sactive;
2464 } else {
2465 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2466 active = &ap->ap_active;
2468 KKASSERT(!(ap->ap_sactive && ap->ap_active));
2469 #if 0
2470 kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
2471 ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
2472 ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
2473 #endif
2476 * Ignore AHCI_PREG_IS_PRCS when link power management is on
2478 if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2479 is &= ~AHCI_PREG_IS_PRCS;
2480 ahci_pwrite(ap, AHCI_PREG_SERR,
2481 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2485 * Command failed (blockable).
2487 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2489 * This stops command processing.
2491 if (is & AHCI_PREG_IS_TFES) {
2492 u_int32_t tfd, serr, cmd;
2493 int err_slot;
2495 process_error:
2496 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2497 serr = ahci_pread(ap, AHCI_PREG_SERR);
2498 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2501 * Load the error slot and restart command processing.
2502 * CLO if we need to. The error slot may not be valid.
2503 * MUST BE DONE BEFORE CLEARING ST!
2505 * Cycle ST.
2507 * It is unclear but we may have to clear SERR to reenable
2508 * error processing.
2510 if (cmd & AHCI_PREG_CMD_ST) {
2511 err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
2512 } else {
2513 err_slot = -1;
2515 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
2516 AHCI_PREG_IS_PSS |
2517 AHCI_PREG_IS_DHRS |
2518 AHCI_PREG_IS_SDBS);
2519 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
2520 AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
2521 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2522 ahci_port_stop(ap, 0);
2523 ahci_os_hardsleep(10);
2524 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2525 kprintf("%s: Issuing CLO\n", PORTNAME(ap));
2526 ahci_port_clo(ap);
2530 * We are now stopped and need a restart. If we have to
2531 * process a NCQ error we will temporarily start and then
2532 * stop the port again, so this condition holds.
2534 stopped = 1;
2535 need = NEED_RESTART;
2538 * ATAPI errors are fairly common from probing, just
2539 * report disk errors or if bootverbose is on.
2541 if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
2542 kprintf("%s: TFES slot %d ci_saved = %08x\n",
2543 PORTNAME(ap), err_slot, ci_saved);
2547 * If we got an error on an error CCB just complete it
2548 * with an error. ci_saved has the mask to restart
2549 * (the err_ccb will be removed from it by finish_error).
2551 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2552 err_slot = ap->ap_err_ccb->ccb_slot;
2553 goto finish_error;
2557 * If NCQ commands were active get the error slot from
2558 * the log page. NCQ is not supported for PM's so this
2559 * is a direct-attached target.
2561 * Otherwise if no commands were active we have a problem.
2563 * Otherwise if the error slot is bad we have a problem.
2565 * Otherwise process the error for the slot.
2567 if (ap->ap_sactive) {
2568 ahci_port_start(ap);
2569 err_slot = ahci_port_read_ncq_error(ap, 0);
2570 ahci_port_stop(ap, 0);
2571 } else if (ap->ap_active == 0) {
2572 kprintf("%s: TFES with no commands pending\n",
2573 PORTNAME(ap));
2574 err_slot = -1;
2575 } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
2576 kprintf("%s: bad error slot %d\n",
2577 PORTNAME(ap), err_slot);
2578 err_slot = -1;
2579 } else {
2580 ccb = &ap->ap_ccbs[err_slot];
2583 * Validate the errored ccb. Note that ccb_at can
2584 * be NULL for direct-attached ccb's.
2586 * Copy received taskfile data from the RFIS.
2588 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
2589 ccb_at = ccb->ccb_xa.at;
2590 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2591 sizeof(struct ata_fis_d2h));
2592 if (bootverbose) {
2593 kprintf("%s: Copying rfis slot %d\n",
2594 ATANAME(ap, ccb_at), err_slot);
2596 } else {
2597 kprintf("%s: Cannot copy rfis, CCB slot "
2598 "%d is not on-chip (state=%d)\n",
2599 ATANAME(ap, ccb->ccb_xa.at),
2600 err_slot, ccb->ccb_xa.state);
2601 err_slot = -1;
2606 * If we could not determine the errored slot then
2607 * reset the port.
2609 if (err_slot < 0) {
2610 kprintf("%s: TFES: Unable to determine errored slot\n",
2611 PORTNAME(ap));
2612 if (ap->ap_flags & AP_F_IN_RESET)
2613 goto fatal;
2614 goto failall;
2618 * Finish error on slot. We will restart ci_saved
2619 * commands except the errored slot which we generate
2620 * a failure for.
2622 finish_error:
2623 ccb = &ap->ap_ccbs[err_slot];
2624 ci_saved &= ~(1 << err_slot);
2625 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2626 ccb->ccb_xa.state = ATA_S_ERROR;
2627 } else if (is & AHCI_PREG_IS_DHRS) {
2629 * Command posted D2H register FIS to the rfis (non-blocking).
2631 * A normal completion with an error may set DHRS instead
2632 * of TFES. The CCS bits are only valid if ERR was set.
2633 * If ERR is set command processing was probably stopped.
2635 * If ERR was not set we can only copy-back data for
2636 * exclusive-mode commands because otherwise we won't know
2637 * which tag the rfis belonged to.
2639 * err_slot must be read from the CCS before any other port
2640 * action, such as stopping the port.
2642 * WARNING! This is not well documented in the AHCI spec.
2643 * It can be found in the state machine tables
2644 * but not in the explanations.
2646 u_int32_t tfd;
2647 u_int32_t cmd;
2648 int err_slot;
2650 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2651 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2653 if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
2654 (cmd & AHCI_PREG_CMD_CR) == 0) {
2655 err_slot = AHCI_PREG_CMD_CCS(
2656 ahci_pread(ap, AHCI_PREG_CMD));
2657 ccb = &ap->ap_ccbs[err_slot];
2658 kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
2659 PORTNAME(ap),
2660 tfd, AHCI_PFMT_TFD_STS,
2661 err_slot, ccb->ccb_xa.fis->command);
2662 goto process_error;
2665 * NO ELSE... copy back is in the normal command completion
2666 * code and only if no error occured and ATA_F_AUTOSENSE
2667 * was set.
2669 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2673 * Device notification to us (non-blocking)
2675 * NOTE! On some parts notification bits can cause an IPMS
2676 * interrupt instead of a SDBS interrupt.
2678 * NOTE! On some parts (e.g. VBOX, probably intel ICHx),
2679 * SDBS notifies us of the completion of a NCQ command
2680 * and DBS does not.
2682 if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
2683 u_int32_t data;
2685 ahci_pwrite(ap, AHCI_PREG_IS,
2686 AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2687 if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
2688 data = ahci_pread(ap, AHCI_PREG_SNTF);
2689 if (data) {
2690 ahci_pwrite(ap, AHCI_PREG_IS,
2691 AHCI_PREG_IS_SDBS);
2692 kprintf("%s: NOTIFY %08x\n",
2693 PORTNAME(ap), data);
2694 ahci_pwrite(ap, AHCI_PREG_SERR,
2695 AHCI_PREG_SERR_DIAG_N);
2696 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2697 ahci_cam_changed(ap, NULL, -1);
2700 is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2704 * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set.
2706 * Spurious IFS errors can occur while we are doing a reset
2707 * sequence through a PM, probably due to an unexpected FIS
2708 * being received during the PM target reset sequence. Chipsets
2709 * are supposed to mask these events but some do not.
2711 * Try to recover from the condition.
2713 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2714 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2715 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2716 kprintf("%s: IFS during PM probe (ignored) "
2717 "IS=%b, SERR=%b\n",
2718 PORTNAME(ap),
2719 is, AHCI_PFMT_IS,
2720 serr, AHCI_PFMT_SERR);
2721 ap->ap_flags |= AP_F_IFS_IGNORED;
2725 * Try to clear the error condition. The IFS error killed
2726 * the port so stop it so we can restart it.
2728 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2729 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2730 is &= ~AHCI_PREG_IS_IFS;
2731 need = NEED_RESTART;
2732 goto failall;
2736 * Port change (hot-plug) (blockable).
2738 * A PRCS interrupt can occur:
2739 * (1) On hot-unplug / normal-unplug (phy lost)
2740 * (2) Sometimes on hot-plug too.
2742 * A PCS interrupt can occur in a number of situations:
2743 * (1) On hot-plug once communication is established
2744 * (2) On hot-unplug sometimes.
2745 * (3) For chipsets with badly written firmware it can occur
2746 * during INIT/RESET sequences due to the device reset.
2747 * (4) For chipsets with badly written firmware it can occur
2748 * when it thinks an unsolicited COMRESET is received
2749 * during a INIT/RESET sequence, even though we actually
2750 * did request it.
2752 * XXX We can then check the CPS (Cold Presence State) bit, if
2753 * supported, to determine if a device is plugged in or not and do
2754 * the right thing.
2756 * PCS interrupts are cleared by clearing DIAG_X. If this occurs
2757 * command processing is automatically stopped (CR goes inactive)
2758 * and the port must be stopped and restarted.
2760 * WARNING: AMD parts (e.g. 880G chipset, probably others) can
2761 * generate PCS on initialization even when device is
2762 * already connected up. It is unclear why this happens.
2763 * Depending on the state of the device detect this can
2764 * cause us to go into harsh reinit or hot-plug insertion
2765 * mode.
2767 * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET
2768 * continues to flow in from the device), we must clear the
2769 * interrupt in all cases and enforce a delay to prevent
2770 * a livelock and give the port time to settle down.
2771 * Only print something if we aren't in INIT/HARD-RESET.
2773 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2775 * Try to clear the error. Because of the repetitiveness
2776 * of this interrupt avoid any harsh action if the port is
2777 * already in the init or hard-reset probe state.
2779 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2780 /* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */
2781 ahci_pwrite(ap, AHCI_PREG_IS,
2782 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2785 * Ignore PCS/PRCS errors during probes (but still clear the
2786 * interrupt to avoid a livelock). The AMD 880/890/SB850
2787 * chipsets do not mask PCS/PRCS internally during reset
2788 * sequences.
2790 if (ap->ap_flags & AP_F_IN_RESET)
2791 goto skip_pcs;
2793 if (ap->ap_probe == ATA_PROBE_NEED_INIT ||
2794 ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) {
2795 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2796 need = NEED_NOTHING;
2797 ahci_os_sleep(1000);
2798 goto failall;
2800 kprintf("%s: Transient Errors: %b (%d)\n",
2801 PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe);
2802 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2803 ahci_os_sleep(200);
2806 * Stop the port and figure out what to do next.
2808 ahci_port_stop(ap, 0);
2809 stopped = 1;
2811 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2812 case AHCI_PREG_SSTS_DET_DEV:
2814 * Device detect
2816 if (ap->ap_probe == ATA_PROBE_FAILED) {
2817 need = NEED_HOTPLUG_INSERT;
2818 goto fatal;
2820 need = NEED_RESTART;
2821 break;
2822 case AHCI_PREG_SSTS_DET_DEV_NE:
2824 * Device not communicating. AMD parts seem to
2825 * like to throw this error on initialization
2826 * for no reason that I can fathom.
2828 kprintf("%s: Device present but not communicating, "
2829 "attempting port restart\n",
2830 PORTNAME(ap));
2831 need = NEED_REINIT;
2832 goto fatal;
2833 default:
2834 if (ap->ap_probe != ATA_PROBE_FAILED) {
2835 need = NEED_HOTPLUG_REMOVE;
2836 goto fatal;
2838 need = NEED_RESTART;
2839 break;
2841 skip_pcs:
2846 * Check for remaining errors - they are fatal. (blockable)
2848 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2849 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2850 u_int32_t serr;
2852 ahci_pwrite(ap, AHCI_PREG_IS,
2853 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2854 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2855 AHCI_PREG_IS_UFS));
2856 serr = ahci_pread(ap, AHCI_PREG_SERR);
2857 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
2858 "disabling port.\n",
2859 PORTNAME(ap),
2860 is, AHCI_PFMT_IS,
2861 serr, AHCI_PFMT_SERR
2863 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2864 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2865 AHCI_PREG_IS_UFS);
2868 * Fail all commands but then what? For now try to
2869 * reinitialize the port.
2871 need = NEED_REINIT;
2872 goto fatal;
2876 * Fail all outstanding commands if we know the port won't recover.
2878 * We may have a ccb_at if the failed command is known and was
2879 * being sent to a device over a port multiplier (PM). In this
2880 * case if the port itself has not completely failed we fail just
2881 * the commands related to that target.
2883 * ci_saved contains the mask of active commands as of when the
2884 * error occured, prior to any port stops.
2886 if (ap->ap_state == AP_S_FATAL_ERROR) {
2887 fatal:
2888 ap->ap_state = AP_S_FATAL_ERROR;
2889 failall:
2890 ahci_port_stop(ap, 0);
2891 stopped = 1;
2894 * Error all the active slots not already errored.
2896 ci_masked = ci_saved & *active & ~ap->ap_expired;
2897 if (ci_masked) {
2898 kprintf("%s: Failing all commands: %08x\n",
2899 PORTNAME(ap), ci_masked);
2902 while (ci_masked) {
2903 slot = ffs(ci_masked) - 1;
2904 ccb = &ap->ap_ccbs[slot];
2905 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2906 ap->ap_expired |= 1 << slot;
2907 ci_saved &= ~(1 << slot);
2908 ci_masked &= ~(1 << slot);
2912 * Clear bits in ci_saved (cause completions to be run)
2913 * for all slots which are not active.
2915 ci_saved &= ~*active;
2918 * Don't restart the port if our problems were deemed fatal.
2920 * Also acknowlege all fatal interrupt sources to prevent
2921 * a livelock.
2923 if (ap->ap_state == AP_S_FATAL_ERROR) {
2924 if (need == NEED_RESTART)
2925 need = NEED_NOTHING;
2926 ahci_pwrite(ap, AHCI_PREG_IS,
2927 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2928 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2929 AHCI_PREG_IS_UFS);
2934 * If we are stopped the AHCI chipset is supposed to have cleared
2935 * CI and SACT. Did it? If it didn't we try very hard to clear
2936 * the fields otherwise we may end up completing CCBs which are
2937 * actually still active.
2939 * IFS errors on (at least) AMD chipsets create this confusion.
2941 if (stopped) {
2942 u_int32_t mask;
2943 if ((mask = ahci_pactive(ap)) != 0) {
2944 kprintf("%s: chipset failed to clear "
2945 "active cmds %08x\n",
2946 PORTNAME(ap), mask);
2947 ahci_port_start(ap);
2948 ahci_port_stop(ap, 0);
2949 if ((mask = ahci_pactive(ap)) != 0) {
2950 kprintf("%s: unable to prod the chip into "
2951 "clearing active cmds %08x\n",
2952 PORTNAME(ap), mask);
2953 /* what do we do now? */
2959 * CCB completion (non blocking).
2961 * CCB completion is detected by noticing its slot's bit in CI has
2962 * changed to zero some time after we activated it.
2963 * If we are polling, we may only be interested in particular slot(s).
2965 * Any active bits not saved are completed within the restrictions
2966 * imposed by the caller.
2968 ci_masked = ~ci_saved & *active;
2969 while (ci_masked) {
2970 slot = ffs(ci_masked) - 1;
2971 ccb = &ap->ap_ccbs[slot];
2972 ci_masked &= ~(1 << slot);
2974 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2975 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2976 " (error)" : "");
2978 bus_dmamap_sync(sc->sc_tag_cmdh,
2979 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2980 BUS_DMASYNC_POSTWRITE);
2982 bus_dmamap_sync(sc->sc_tag_cmdt,
2983 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2984 BUS_DMASYNC_POSTWRITE);
2986 bus_dmamap_sync(sc->sc_tag_rfis,
2987 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2988 BUS_DMASYNC_POSTREAD);
2990 *active &= ~(1 << ccb->ccb_slot);
2991 if (active == &ap->ap_active) {
2992 KKASSERT(ap->ap_active_cnt > 0);
2993 --ap->ap_active_cnt;
2997 * Complete the ccb. If the ccb was marked expired it
2998 * was probably already removed from the command processor,
2999 * so don't take the clear ci_saved bit as meaning the
3000 * command actually succeeded, it didn't.
3002 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
3003 ap->ap_expired &= ~(1 << ccb->ccb_slot);
3004 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3005 ccb->ccb_done(ccb);
3006 ccb->ccb_xa.complete(&ccb->ccb_xa);
3007 } else {
3008 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
3009 ccb->ccb_xa.state = ATA_S_COMPLETE;
3010 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
3011 memcpy(&ccb->ccb_xa.rfis,
3012 ap->ap_rfis->rfis,
3013 sizeof(struct ata_fis_d2h));
3014 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
3015 ccb->ccb_xa.state = ATA_S_ERROR;
3018 ccb->ccb_done(ccb);
3023 * Cleanup. Will not be set if non-blocking.
3025 switch(need) {
3026 case NEED_NOTHING:
3028 * If operating normally and not stopped the interrupt was
3029 * probably just a normal completion and we may be able to
3030 * issue more commands.
3032 if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR)
3033 ahci_issue_pending_commands(ap, NULL);
3034 break;
3035 case NEED_RESTART:
3037 * A recoverable error occured and we can restart outstanding
3038 * commands on the port.
3040 ci_saved &= ~ap->ap_expired;
3041 if (ci_saved) {
3042 kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
3043 ahci_issue_saved_commands(ap, ci_saved);
3047 * Potentially issue new commands if not in a failed
3048 * state.
3050 if (ap->ap_state != AP_S_FATAL_ERROR) {
3051 ahci_port_start(ap);
3052 ahci_issue_pending_commands(ap, NULL);
3054 break;
3055 case NEED_REINIT:
3057 * Something horrible happened to the port and we
3058 * need to reinitialize it.
3060 kprintf("%s: REINIT - Attempting to reinitialize the port "
3061 "after it had a horrible accident\n",
3062 PORTNAME(ap));
3063 ap->ap_flags |= AP_F_IN_RESET;
3064 ap->ap_flags |= AP_F_HARSH_REINIT;
3065 ap->ap_probe = ATA_PROBE_NEED_INIT;
3066 ahci_cam_changed(ap, NULL, -1);
3067 break;
3068 case NEED_HOTPLUG_INSERT:
3070 * A hot-plug insertion event has occured and all
3071 * outstanding commands have already been revoked.
3073 * Don't recurse if this occurs while we are
3074 * resetting the port.
3076 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3077 kprintf("%s: HOTPLUG - Device inserted\n",
3078 PORTNAME(ap));
3079 ap->ap_probe = ATA_PROBE_NEED_INIT;
3080 ahci_cam_changed(ap, NULL, -1);
3082 break;
3083 case NEED_HOTPLUG_REMOVE:
3085 * A hot-plug removal event has occured and all
3086 * outstanding commands have already been revoked.
3088 * Don't recurse if this occurs while we are
3089 * resetting the port.
3091 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3092 kprintf("%s: HOTPLUG - Device removed\n",
3093 PORTNAME(ap));
3094 ahci_port_hardstop(ap);
3095 /* ap_probe set to failed */
3096 ahci_cam_changed(ap, NULL, -1);
3098 break;
3099 default:
3100 break;
3104 struct ahci_ccb *
3105 ahci_get_ccb(struct ahci_port *ap)
3107 struct ahci_ccb *ccb;
3109 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3110 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
3111 if (ccb != NULL) {
3112 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
3113 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
3114 ccb->ccb_xa.state = ATA_S_SETUP;
3115 ccb->ccb_xa.flags = 0;
3116 ccb->ccb_xa.at = NULL;
3118 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3120 return (ccb);
3123 void
3124 ahci_put_ccb(struct ahci_ccb *ccb)
3126 struct ahci_port *ap = ccb->ccb_port;
3128 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3129 ccb->ccb_xa.state = ATA_S_PUT;
3130 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
3131 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3134 struct ahci_ccb *
3135 ahci_get_err_ccb(struct ahci_port *ap)
3137 struct ahci_ccb *err_ccb;
3138 u_int32_t sact;
3139 u_int32_t ci;
3141 /* No commands may be active on the chip. */
3143 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3144 sact = ahci_pread(ap, AHCI_PREG_SACT);
3145 if (sact != 0) {
3146 kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
3147 PORTNAME(ap), sact);
3150 ci = ahci_pread(ap, AHCI_PREG_CI);
3151 if (ci) {
3152 kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
3153 ap->ap_name, ci);
3155 KKASSERT(ci == 0);
3156 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
3157 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
3159 /* Save outstanding command state. */
3160 ap->ap_err_saved_active = ap->ap_active;
3161 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
3162 ap->ap_err_saved_sactive = ap->ap_sactive;
3165 * Pretend we have no commands outstanding, so that completions won't
3166 * run prematurely.
3168 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
3171 * Grab a CCB to use for error recovery. This should never fail, as
3172 * we ask atascsi to reserve one for us at init time.
3174 err_ccb = ap->ap_err_ccb;
3175 KKASSERT(err_ccb != NULL);
3176 err_ccb->ccb_xa.flags = 0;
3177 err_ccb->ccb_done = ahci_empty_done;
3179 return err_ccb;
3182 void
3183 ahci_put_err_ccb(struct ahci_ccb *ccb)
3185 struct ahci_port *ap = ccb->ccb_port;
3186 u_int32_t sact;
3187 u_int32_t ci;
3189 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
3192 * No commands may be active on the chip
3194 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3195 sact = ahci_pread(ap, AHCI_PREG_SACT);
3196 if (sact) {
3197 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
3198 ccb->ccb_slot, sact);
3201 ci = ahci_pread(ap, AHCI_PREG_CI);
3202 if (ci) {
3203 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
3204 "(act=%08x sact=%08x)\n",
3205 ccb->ccb_slot, ci,
3206 ap->ap_active, ap->ap_sactive);
3209 KKASSERT(ccb == ap->ap_err_ccb);
3211 /* Restore outstanding command state */
3212 ap->ap_sactive = ap->ap_err_saved_sactive;
3213 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
3214 ap->ap_active = ap->ap_err_saved_active;
3216 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
3220 * Read log page to get NCQ error.
3222 * NOTE: NCQ not currently supported on port multipliers. XXX
3225 ahci_port_read_ncq_error(struct ahci_port *ap, int target)
3227 struct ata_log_page_10h *log;
3228 struct ahci_ccb *ccb;
3229 struct ahci_ccb *ccb2;
3230 struct ahci_cmd_hdr *cmd_slot;
3231 struct ata_fis_h2d *fis;
3232 int err_slot;
3234 if (bootverbose) {
3235 kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
3236 target);
3240 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
3242 * Getting err_ccb clears active/sactive/active_cnt, putting
3243 * it back restores the fields.
3245 ccb = ahci_get_err_ccb(ap);
3246 ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
3247 ccb->ccb_xa.data = ap->ap_err_scratch;
3248 ccb->ccb_xa.datalen = 512;
3249 ccb->ccb_xa.complete = ahci_dummy_done;
3250 ccb->ccb_xa.at = ap->ap_ata[target];
3252 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
3253 bzero(fis, sizeof(*fis));
3254 fis->type = ATA_FIS_TYPE_H2D;
3255 fis->flags = ATA_H2D_FLAGS_CMD | target;
3256 fis->command = ATA_C_READ_LOG_EXT;
3257 fis->lba_low = 0x10; /* queued error log page (10h) */
3258 fis->sector_count = 1; /* number of sectors (1) */
3259 fis->sector_count_exp = 0;
3260 fis->lba_mid = 0; /* starting offset */
3261 fis->lba_mid_exp = 0;
3262 fis->device = 0;
3264 cmd_slot = ccb->ccb_cmd_hdr;
3265 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
3267 if (ahci_load_prdt(ccb) != 0) {
3268 err_slot = -1;
3269 goto err;
3272 ccb->ccb_xa.state = ATA_S_PENDING;
3273 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
3274 err_slot = -1;
3275 ahci_unload_prdt(ccb);
3276 goto err;
3278 ahci_unload_prdt(ccb);
3281 * Success, extract failed register set and tags from the scratch
3282 * space.
3284 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
3285 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
3286 /* Not queued bit was set - wasn't an NCQ error? */
3287 kprintf("%s: read NCQ error page, but not an NCQ error?\n",
3288 PORTNAME(ap));
3289 err_slot = -1;
3290 } else {
3291 /* Copy back the log record as a D2H register FIS. */
3292 err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
3294 ccb2 = &ap->ap_ccbs[err_slot];
3295 if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
3296 kprintf("%s: read NCQ error page slot=%d\n",
3297 ATANAME(ap, ccb2->ccb_xa.at),
3298 err_slot);
3299 memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
3300 sizeof(struct ata_fis_d2h));
3301 ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3302 ccb2->ccb_xa.rfis.flags = 0;
3303 } else {
3304 kprintf("%s: read NCQ error page slot=%d, "
3305 "slot does not match any cmds\n",
3306 ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
3307 err_slot);
3308 err_slot = -1;
3311 err:
3312 ahci_put_err_ccb(ccb);
3313 kprintf("%s: DONE log page target %d err_slot=%d\n",
3314 PORTNAME(ap), target, err_slot);
3315 return (err_slot);
3319 * Allocate memory for various structures DMAd by hardware. The maximum
3320 * number of segments for these tags is 1 so the DMA memory will have a
3321 * single physical base address.
3323 struct ahci_dmamem *
3324 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3326 struct ahci_dmamem *adm;
3327 int error;
3329 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3331 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3332 BUS_DMA_ZERO, &adm->adm_map);
3333 if (error == 0) {
3334 adm->adm_tag = tag;
3335 error = bus_dmamap_load(tag, adm->adm_map,
3336 adm->adm_kva,
3337 bus_dma_tag_getmaxsize(tag),
3338 ahci_dmamem_saveseg, &adm->adm_busaddr,
3341 if (error) {
3342 if (adm->adm_map) {
3343 bus_dmamap_destroy(tag, adm->adm_map);
3344 adm->adm_map = NULL;
3345 adm->adm_tag = NULL;
3346 adm->adm_kva = NULL;
3348 kfree(adm, M_DEVBUF);
3349 adm = NULL;
3351 return (adm);
3354 static
3355 void
3356 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3358 KKASSERT(error == 0);
3359 KKASSERT(nsegs == 1);
3360 *(bus_addr_t *)info = segs->ds_addr;
3364 void
3365 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3367 if (adm->adm_map) {
3368 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3369 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3370 adm->adm_map = NULL;
3371 adm->adm_tag = NULL;
3372 adm->adm_kva = NULL;
3374 kfree(adm, M_DEVBUF);
3377 u_int32_t
3378 ahci_read(struct ahci_softc *sc, bus_size_t r)
3380 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3381 BUS_SPACE_BARRIER_READ);
3382 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3385 void
3386 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3388 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3389 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3390 BUS_SPACE_BARRIER_WRITE);
3393 u_int32_t
3394 ahci_pread(struct ahci_port *ap, bus_size_t r)
3396 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3397 BUS_SPACE_BARRIER_READ);
3398 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3401 void
3402 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3404 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3405 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3406 BUS_SPACE_BARRIER_WRITE);
3410 * Wait up to (timeout) milliseconds for the masked port register to
3411 * match the target.
3413 * Timeout is in milliseconds.
3416 ahci_pwait_eq(struct ahci_port *ap, int timeout,
3417 bus_size_t r, u_int32_t mask, u_int32_t target)
3419 int t;
3422 * Loop hard up to 100uS
3424 for (t = 0; t < 100; ++t) {
3425 if ((ahci_pread(ap, r) & mask) == target)
3426 return (0);
3427 ahci_os_hardsleep(1); /* us */
3430 do {
3431 timeout -= ahci_os_softsleep();
3432 if ((ahci_pread(ap, r) & mask) == target)
3433 return (0);
3434 } while (timeout > 0);
3435 return (1);
3439 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3440 u_int32_t target)
3442 int t;
3445 * Loop hard up to 100uS
3447 for (t = 0; t < 100; ++t) {
3448 if ((ahci_read(sc, r) & mask) != target)
3449 return (0);
3450 ahci_os_hardsleep(1); /* us */
3454 * And one millisecond the slow way
3456 t = 1000;
3457 do {
3458 t -= ahci_os_softsleep();
3459 if ((ahci_read(sc, r) & mask) != target)
3460 return (0);
3461 } while (t > 0);
3463 return (1);
3468 * Acquire an ata transfer.
3470 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
3471 * targets that go through the port multiplier.
3473 struct ata_xfer *
3474 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3476 struct ahci_ccb *ccb;
3478 ccb = ahci_get_ccb(ap);
3479 if (ccb == NULL) {
3480 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3481 PORTNAME(ap));
3482 return (NULL);
3485 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3486 PORTNAME(ap), ccb->ccb_slot);
3488 bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
3489 ccb->ccb_xa.at = at;
3490 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3492 return (&ccb->ccb_xa);
3495 void
3496 ahci_ata_put_xfer(struct ata_xfer *xa)
3498 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3500 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3502 ahci_put_ccb(ccb);
3506 ahci_ata_cmd(struct ata_xfer *xa)
3508 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3509 struct ahci_cmd_hdr *cmd_slot;
3511 KKASSERT(xa->state == ATA_S_SETUP);
3513 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3514 goto failcmd;
3515 ccb->ccb_done = ahci_ata_cmd_done;
3517 cmd_slot = ccb->ccb_cmd_hdr;
3518 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
3519 if (ccb->ccb_xa.at) {
3520 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
3521 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
3524 if (xa->flags & ATA_F_WRITE)
3525 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3527 if (xa->flags & ATA_F_PACKET)
3528 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3530 if (ahci_load_prdt(ccb) != 0)
3531 goto failcmd;
3533 xa->state = ATA_S_PENDING;
3535 if (xa->flags & ATA_F_POLL)
3536 return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3538 crit_enter();
3539 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
3540 xa->flags |= ATA_F_TIMEOUT_DESIRED;
3541 ahci_start(ccb);
3542 crit_exit();
3543 return (xa->state);
3545 failcmd:
3546 crit_enter();
3547 xa->state = ATA_S_ERROR;
3548 xa->complete(xa);
3549 crit_exit();
3550 return (ATA_S_ERROR);
3553 void
3554 ahci_ata_cmd_done(struct ahci_ccb *ccb)
3556 struct ata_xfer *xa = &ccb->ccb_xa;
3559 * NOTE: callout does not lock port and may race us modifying
3560 * the flags, so make sure its stopped.
3562 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3563 callout_stop(&ccb->ccb_timeout);
3564 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3566 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3568 KKASSERT(xa->state != ATA_S_ONCHIP);
3569 ahci_unload_prdt(ccb);
3571 if (xa->state != ATA_S_TIMEOUT)
3572 xa->complete(xa);
3576 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3577 * while the callout is runing.
3579 * We can't safely get the port lock here or delay, we could block
3580 * the callout thread.
3582 static void
3583 ahci_ata_cmd_timeout_unserialized(void *arg)
3585 struct ahci_ccb *ccb = arg;
3586 struct ahci_port *ap = ccb->ccb_port;
3588 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3589 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3590 ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3594 * Timeout code, typically called when the port command processor is running.
3596 * We have to be very very careful here. We cannot stop the port unless
3597 * CR is already clear or the only active commands remaining are timed-out
3598 * ones. Otherwise stopping the port will race the command processor and
3599 * we can lose events. While we can theoretically just restart everything
3600 * that could result in a double-issue which will not work for ATAPI commands.
3602 void
3603 ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3605 struct ata_xfer *xa = &ccb->ccb_xa;
3606 struct ahci_port *ap = ccb->ccb_port;
3607 struct ata_port *at;
3608 u_int32_t ci_saved;
3609 u_int32_t mask;
3610 int slot;
3612 at = ccb->ccb_xa.at;
3614 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
3615 "\tcmd-reg 0x%b\n"
3616 "\tsactive=%08x active=%08x expired=%08x\n"
3617 "\t sact=%08x ci=%08x\n"
3618 "\t STS=%b\n",
3619 ATANAME(ap, at),
3620 ccb->ccb_xa.state, ccb->ccb_slot,
3621 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3622 ap->ap_sactive, ap->ap_active, ap->ap_expired,
3623 ahci_pread(ap, AHCI_PREG_SACT),
3624 ahci_pread(ap, AHCI_PREG_CI),
3625 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
3630 * NOTE: Timeout will not be running if the command was polled.
3631 * If we got here at least one of these flags should be set.
3633 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
3634 ATA_F_TIMEOUT_RUNNING));
3635 xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3637 if (ccb->ccb_xa.state == ATA_S_PENDING) {
3638 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3639 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3640 ccb->ccb_done(ccb);
3641 xa->complete(xa);
3642 ahci_issue_pending_commands(ap, NULL);
3643 return;
3645 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3646 kprintf("%s: Unexpected state during timeout: %d\n",
3647 ATANAME(ap, at), ccb->ccb_xa.state);
3648 return;
3652 * Ok, we can only get this command off the chip if CR is inactive
3653 * or if the only commands running on the chip are all expired.
3654 * Otherwise we have to wait until the port is in a safe state.
3656 * Do not set state here, it will cause polls to return when the
3657 * ccb is not yet off the chip.
3659 ap->ap_expired |= 1 << ccb->ccb_slot;
3661 if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
3662 (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
3664 * If using FBSS or NCQ we can't safely stop the port
3665 * right now.
3667 kprintf("%s: Deferred timeout until its safe, slot %d\n",
3668 ATANAME(ap, at), ccb->ccb_slot);
3669 return;
3673 * We can safely stop the port and process all expired ccb's,
3674 * which will include our current ccb.
3676 ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
3677 ahci_pread(ap, AHCI_PREG_CI);
3678 ahci_port_stop(ap, 0);
3680 while (ap->ap_expired) {
3681 slot = ffs(ap->ap_expired) - 1;
3682 ap->ap_expired &= ~(1 << slot);
3683 ci_saved &= ~(1 << slot);
3684 ccb = &ap->ap_ccbs[slot];
3685 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3686 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
3687 KKASSERT(ap->ap_sactive & (1 << slot));
3688 ap->ap_sactive &= ~(1 << slot);
3689 } else {
3690 KKASSERT(ap->ap_active & (1 << slot));
3691 ap->ap_active &= ~(1 << slot);
3692 --ap->ap_active_cnt;
3694 ccb->ccb_done(ccb);
3695 ccb->ccb_xa.complete(&ccb->ccb_xa);
3697 /* ccb invalid now */
3700 * We can safely CLO the port to clear any BSY/DRQ, a case which
3701 * can occur with port multipliers. This will unbrick the port
3702 * and allow commands to other targets behind the PM continue.
3703 * (FBSS).
3705 * Finally, once the port has been restarted we can issue any
3706 * previously saved pending commands, and run the port interrupt
3707 * code to handle any completions which may have occured when
3708 * we saved CI.
3710 if (ahci_pread(ap, AHCI_PREG_TFD) &
3711 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
3712 kprintf("%s: Warning, issuing CLO after timeout\n",
3713 ATANAME(ap, at));
3714 ahci_port_clo(ap);
3716 ahci_port_start(ap);
3719 * We absolutely must make sure the chipset cleared activity on
3720 * all slots. This sometimes might not happen due to races with
3721 * a chipset interrupt which stops the port before we can manage
3722 * to. For some reason some chipsets don't clear the active
3723 * commands when we turn off CMD_ST after the chip has stopped
3724 * operations itself.
3726 if (ahci_pactive(ap) != 0) {
3727 ahci_port_stop(ap, 0);
3728 ahci_port_start(ap);
3729 if ((mask = ahci_pactive(ap)) != 0) {
3730 kprintf("%s: quick-timeout: chipset failed "
3731 "to clear active cmds %08x\n",
3732 PORTNAME(ap), mask);
3735 ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
3736 ahci_issue_pending_commands(ap, NULL);
3737 ahci_port_intr(ap, 0);
3741 * Issue a previously saved set of commands
3743 void
3744 ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
3746 if (ci_saved) {
3747 KKASSERT(!((ap->ap_active & ci_saved) &&
3748 (ap->ap_sactive & ci_saved)));
3749 KKASSERT((ci_saved & ap->ap_expired) == 0);
3750 if (ap->ap_sactive & ci_saved)
3751 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
3752 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3757 * Used by the softreset, pmprobe, and read_ncq_error only, in very
3758 * specialized, controlled circumstances.
3760 * Only one command may be pending.
3762 void
3763 ahci_quick_timeout(struct ahci_ccb *ccb)
3765 struct ahci_port *ap = ccb->ccb_port;
3766 u_int32_t mask;
3768 switch (ccb->ccb_xa.state) {
3769 case ATA_S_PENDING:
3770 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3771 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3772 break;
3773 case ATA_S_ONCHIP:
3775 * We have to clear the command on-chip.
3777 KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3778 ap->ap_sactive == 0);
3779 ahci_port_stop(ap, 0);
3780 ahci_port_start(ap);
3781 if (ahci_pactive(ap) != 0) {
3782 ahci_port_stop(ap, 0);
3783 ahci_port_start(ap);
3784 if ((mask = ahci_pactive(ap)) != 0) {
3785 kprintf("%s: quick-timeout: chipset failed "
3786 "to clear active cmds %08x\n",
3787 PORTNAME(ap), mask);
3791 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3792 ap->ap_active &= ~(1 << ccb->ccb_slot);
3793 KKASSERT(ap->ap_active_cnt > 0);
3794 --ap->ap_active_cnt;
3795 break;
3796 default:
3797 panic("%s: ahci_quick_timeout: ccb in bad state %d",
3798 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3802 static void
3803 ahci_dummy_done(struct ata_xfer *xa)
3807 static void
3808 ahci_empty_done(struct ahci_ccb *ccb)
3813 ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
3814 int feature, int enable)
3816 struct ata_port *at;
3817 struct ata_xfer *xa;
3818 int error;
3820 at = atx ? atx : ap->ap_ata[0];
3822 xa = ahci_ata_get_xfer(ap, atx);
3824 xa->fis->type = ATA_FIS_TYPE_H2D;
3825 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
3826 xa->fis->command = ATA_C_SET_FEATURES;
3827 xa->fis->features = enable ? ATA_C_SATA_FEATURE_ENA :
3828 ATA_C_SATA_FEATURE_DIS;
3829 xa->fis->sector_count = feature;
3830 xa->fis->control = ATA_FIS_CONTROL_4BIT;
3832 xa->complete = ahci_dummy_done;
3833 xa->datalen = 0;
3834 xa->flags = ATA_F_POLL;
3835 xa->timeout = 1000;
3837 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
3838 error = 0;
3839 else
3840 error = EIO;
3841 ahci_ata_put_xfer(xa);
3842 return(error);