use consistent hidd naming
[AROS.git] / rom / devs / ahci / ahci.c
blobe32a57b56653b067000aa53bb94e740e72935a44
1 /*
2 * (MPSAFE)
4 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
21 * This code is derived from software contributed to The DragonFly Project
22 * by Matthew Dillon <dillon@backplane.com>
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
33 * distribution.
34 * 3. Neither the name of The DragonFly Project nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific, prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
51 * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
54 #include "ahci.h"
56 void ahci_port_interrupt_enable(struct ahci_port *ap);
58 int ahci_load_prdt(struct ahci_ccb *);
59 void ahci_unload_prdt(struct ahci_ccb *);
60 static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
61 int nsegs, int error);
62 void ahci_start(struct ahci_ccb *);
63 int ahci_port_softreset(struct ahci_port *ap);
64 int ahci_port_hardreset(struct ahci_port *ap, int hard);
65 void ahci_port_hardstop(struct ahci_port *ap);
67 static void ahci_ata_cmd_timeout_unserialized(void *);
68 void ahci_check_active_timeouts(struct ahci_port *ap);
70 void ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at);
71 void ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at);
72 void ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb);
73 void ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t mask);
75 int ahci_port_read_ncq_error(struct ahci_port *, int);
77 struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
78 void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
79 static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
81 static void ahci_dummy_done(struct ata_xfer *xa);
82 static void ahci_empty_done(struct ahci_ccb *ccb);
83 static void ahci_ata_cmd_done(struct ahci_ccb *ccb);
84 static u_int32_t ahci_pactive(struct ahci_port *ap);
87 * Initialize the global AHCI hardware. This code does not set up any of
88 * its ports.
90 int
91 ahci_init(struct ahci_softc *sc)
93 u_int32_t pi;
94 u_int32_t pleft;
95 u_int32_t bios_cap, vers;
96 int i;
97 struct ahci_port *ap;
99 DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
100 ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
103 * AHCI version.
105 vers = ahci_read(sc, AHCI_REG_VS);
106 (void)vers; // Unused (for now: jmcmullan)
109 * save BIOS initialised parameters, enable staggered spin up
111 bios_cap = ahci_read(sc, AHCI_REG_CAP);
112 bios_cap &= AHCI_REG_CAP_SMPS | AHCI_REG_CAP_SSS;
114 pi = ahci_read(sc, AHCI_REG_PI);
117 * Unconditionally reset the controller, do not conditionalize on
118 * trying to figure it if it was previously active or not.
120 * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section
121 * 5.2.2.1 regarding this. HR should be set to 1 only after
122 * AE is set to 1. The reset sequence will clear HR when
123 * it completes, and will also clear AE if SAM is 0. AE must
124 * then be set again. When SAM is 1 the AE bit typically reads
125 * as 1 (and is read-only).
127 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
128 * otherwise the writes will only be separated by a few
129 * nanoseconds.
131 * NOTE BRICKS (1)
133 * If you have a port multiplier and it does not have a device
134 * in target 0, and it probes normally, but a later operation
135 * mis-probes a target behind that PM, it is possible for the
136 * port to brick such that only (a) a power cycle of the host
137 * or (b) placing a device in target 0 will fix the problem.
138 * Power cycling the PM has no effect (it works fine on another
139 * host port). This issue is unrelated to CLO.
142 * Wait for any prior reset sequence to complete
144 if (ahci_wait_ne(sc, AHCI_REG_GHC,
145 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
146 device_printf(sc->sc_dev, "Controller is stuck in reset\n");
147 return (1);
149 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
150 ahci_os_sleep(500);
151 ahci_read(sc, AHCI_REG_GHC); /* flush */
152 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
153 ahci_os_sleep(500);
154 ahci_read(sc, AHCI_REG_GHC); /* flush */
155 if (ahci_wait_ne(sc, AHCI_REG_GHC,
156 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
157 device_printf(sc->sc_dev, "unable to reset controller\n");
158 return (1);
160 if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
161 device_printf(sc->sc_dev, "AE did not auto-clear!\n");
162 ahci_write(sc, AHCI_REG_GHC, 0);
163 ahci_os_sleep(500);
167 * Enable ahci (global interrupts disabled)
169 * Restore saved parameters. Avoid pci transaction burst write
170 * by issuing dummy reads.
172 ahci_os_sleep(500);
173 ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
174 ahci_os_sleep(500);
176 ahci_read(sc, AHCI_REG_GHC); /* flush */
178 bios_cap |= AHCI_REG_CAP_SSS;
179 ahci_write(sc, AHCI_REG_CAP, ahci_read(sc, AHCI_REG_CAP) | bios_cap);
180 ahci_write(sc, AHCI_REG_PI, pi);
181 ahci_read(sc, AHCI_REG_GHC); /* flush */
184 * Intel hocus pocus in case the BIOS has not set the chip up
185 * properly for AHCI operation.
187 if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
188 if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
189 device_printf(sc->sc_dev, "Intel hocus pocus\n");
190 pci_write_config(sc->sc_dev, 0x92,
191 pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
195 * This is a hack that currently does not appear to have
196 * a significant effect, but I noticed the port registers
197 * do not appear to be completely cleared after the host
198 * controller is reset.
200 * Use a temporary ap structure so we can call ahci_pwrite().
202 * We must be sure to stop the port
204 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
205 ap->ap_sc = sc;
206 pleft = pi;
207 for (i = 0; i < AHCI_MAX_PORTS; ++i) {
208 if (pleft == 0)
209 break;
210 if ((pi & (1 << i)) == 0)
211 continue;
212 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
213 AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
214 device_printf(sc->sc_dev, "can't map port\n");
215 return (1);
218 * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
219 * AHCI1.1 can brick the chipset. Not only brick it,
220 * but also crash the PC. The bit seems unreliable
221 * on AHCI1.2 as well.
223 ahci_port_stop(ap, 1);
224 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
225 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
226 ahci_pwrite(ap, AHCI_PREG_IE, 0);
227 ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
228 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
229 ahci_pwrite(ap, AHCI_PREG_IS, -1);
230 sc->sc_portmask |= (1 << i);
231 pleft &= ~(1 << i);
233 sc->sc_numports = i;
234 kfree(ap, M_DEVBUF);
236 return (0);
240 * Allocate and initialize an AHCI port.
243 ahci_port_alloc(struct ahci_softc *sc, u_int port)
245 struct ahci_port *ap;
246 struct ata_port *at;
247 struct ahci_ccb *ccb;
248 u_int64_t dva;
249 u_int32_t cmd;
250 u_int32_t data;
251 struct ahci_cmd_hdr *hdr;
252 struct ahci_cmd_table *table;
253 int rc = ENOMEM;
254 int error;
255 int i;
257 ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
258 ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
260 ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
261 device_get_name(sc->sc_dev),
262 device_get_unit(sc->sc_dev),
263 port);
264 sc->sc_ports[port] = ap;
267 * Allocate enough so we never have to reallocate, it makes
268 * it easier.
270 * ap_pmcount will be reduced by the scan if we encounter the
271 * port multiplier port prior to target 15.
273 * kmalloc power-of-2 allocations are guaranteed not to cross
274 * a page boundary. Make sure the identify sub-structure in the
275 * at structure does not cross a page boundary, just in case the
276 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
278 if (ap->ap_ata[0] == NULL) {
279 int pw2;
281 for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
283 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
284 at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
285 ap->ap_ata[i] = at;
286 at->at_ahci_port = ap;
287 at->at_target = i;
288 at->at_probe = ATA_PROBE_NEED_INIT;
289 at->at_features |= ATA_PORT_F_RESCAN;
290 ksnprintf(at->at_name, sizeof(at->at_name),
291 "%s.%d", ap->ap_name, i);
294 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
295 AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
296 device_printf(sc->sc_dev,
297 "unable to create register window for port %d\n",
298 port);
299 goto freeport;
302 ap->ap_sc = sc;
303 ap->ap_num = port;
304 ap->ap_probe = ATA_PROBE_NEED_INIT;
305 ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE;
306 ap->sysctl_tree = NULL;
307 TAILQ_INIT(&ap->ap_ccb_free);
308 TAILQ_INIT(&ap->ap_ccb_pending);
309 lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
311 /* Disable port interrupts */
312 ahci_pwrite(ap, AHCI_PREG_IE, 0);
313 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
316 * Sec 10.1.2 - deinitialise port if it is already running
318 cmd = ahci_pread(ap, AHCI_PREG_CMD);
319 kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD);
321 if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
322 AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
323 (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
324 int r;
326 r = ahci_port_stop(ap, 1);
327 if (r) {
328 device_printf(sc->sc_dev,
329 "unable to disable %s, ignoring port %d\n",
330 ((r == 2) ? "CR" : "FR"), port);
331 rc = ENXIO;
332 goto freeport;
335 /* Write DET to zero */
336 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
339 /* Allocate RFIS */
340 ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
341 if (ap->ap_dmamem_rfis == NULL) {
342 kprintf("%s: NORFIS\n", PORTNAME(ap));
343 goto nomem;
346 /* Setup RFIS base address */
347 ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
348 dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
349 ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
350 ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
352 /* Clear SERR before starting FIS reception or ST or anything */
353 ahci_flush_tfd(ap);
354 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
356 /* Enable FIS reception and activate port. */
357 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
358 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
359 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
360 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
362 /* Check whether port activated. Skip it if not. */
363 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
364 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
365 kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
366 rc = ENXIO;
367 goto freeport;
370 /* Allocate a CCB for each command slot */
371 ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
372 M_WAITOK | M_ZERO);
373 if (ap->ap_ccbs == NULL) {
374 device_printf(sc->sc_dev,
375 "unable to allocate command list for port %d\n",
376 port);
377 goto freeport;
380 /* Command List Structures and Command Tables */
381 ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
382 ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
383 if (ap->ap_dmamem_cmd_table == NULL ||
384 ap->ap_dmamem_cmd_list == NULL) {
385 nomem:
386 device_printf(sc->sc_dev,
387 "unable to allocate DMA memory for port %d\n",
388 port);
389 goto freeport;
392 /* Setup command list base address */
393 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
394 ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
395 ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
397 /* Split CCB allocation into CCBs and assign to command header/table */
398 hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
399 table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
400 for (i = 0; i < sc->sc_ncmds; i++) {
401 ccb = &ap->ap_ccbs[i];
403 error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
404 &ccb->ccb_dmamap);
405 if (error) {
406 device_printf(sc->sc_dev,
407 "unable to create dmamap for port %d "
408 "ccb %d\n", port, i);
409 goto freeport;
412 callout_init(&ccb->ccb_timeout);
413 ccb->ccb_slot = i;
414 ccb->ccb_port = ap;
415 ccb->ccb_cmd_hdr = &hdr[i];
416 ccb->ccb_cmd_table = &table[i];
417 dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
418 ccb->ccb_slot * sizeof(struct ahci_cmd_table);
419 ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
420 ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
422 ccb->ccb_xa.fis =
423 (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
424 ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
425 ccb->ccb_xa.tag = i;
427 ccb->ccb_xa.state = ATA_S_COMPLETE;
430 * CCB[1] is the error CCB and is not get or put. It is
431 * also used for probing. Numerous HBAs only load the
432 * signature from CCB[1] so it MUST be used for the second
433 * FIS.
435 if (i == 1)
436 ap->ap_err_ccb = ccb;
437 else
438 ahci_put_ccb(ccb);
442 * Wait for ICC change to complete
444 ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
447 * Calculate the interrupt mask
449 data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
450 AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
451 AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
452 AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
453 AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
454 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
455 data |= AHCI_PREG_IE_IPME;
456 #ifdef AHCI_COALESCE
457 if (sc->sc_ccc_ports & (1 << port))
458 data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
459 #endif
460 ap->ap_intmask = data;
463 * Start the port helper thread. The helper thread will call
464 * ahci_port_init() so the ports can all be started in parallel.
465 * A failure by ahci_port_init() does not deallocate the port
466 * since we still want hot-plug events.
468 ahci_os_start_port(ap);
469 return(0);
470 freeport:
471 ahci_port_free(sc, port);
472 return (rc);
476 * [re]initialize an idle port. No CCBs should be active. (from port thread)
478 * This function is called during the initial port allocation sequence
479 * and is also called on hot-plug insertion. We take no chances and
480 * use a portreset instead of a softreset.
482 * This function is the only way to move a failed port back to active
483 * status.
485 * Returns 0 if a device is successfully detected.
488 ahci_port_init(struct ahci_port *ap)
490 u_int32_t cmd;
493 * Register [re]initialization
495 * Flush the TFD and SERR and make sure the port is stopped before
496 * enabling its interrupt. We no longer cycle the port start as
497 * the port should not be started unless a device is present.
499 * XXX should we enable FIS reception? (FRE)?
501 ahci_pwrite(ap, AHCI_PREG_IE, 0);
502 ahci_port_stop(ap, 0);
503 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
504 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
505 ahci_flush_tfd(ap);
506 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
509 * If we are being harsh try to kill the port completely. Normally
510 * we would want to hold on to some of the state the BIOS may have
511 * set, such as SUD (spin up device).
513 * AP_F_HARSH_REINIT is cleared in the hard reset state
515 if (ap->ap_flags & AP_F_HARSH_REINIT) {
516 ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
517 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
519 ahci_os_sleep(1000);
521 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
522 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
523 cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD |
524 AHCI_PREG_CMD_SUD;
525 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
526 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
527 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
528 kprintf("%s: Warning: FRE did not come up during "
529 "harsh reinitialization\n",
530 PORTNAME(ap));
532 ahci_os_sleep(1000);
536 * Clear any pending garbage and re-enable the interrupt before
537 * going to the next stage.
539 ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
540 ap->ap_pmcount = 0;
542 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
543 ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
544 ahci_flush_tfd(ap);
545 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
546 ahci_pwrite(ap, AHCI_PREG_IS, -1);
548 ahci_port_interrupt_enable(ap);
550 return (0);
554 * Enable or re-enable interrupts on a port.
556 * This routine is called from the port initialization code or from the
557 * helper thread as the real interrupt may be forced to turn off certain
558 * interrupt sources.
560 void
561 ahci_port_interrupt_enable(struct ahci_port *ap)
563 ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
567 * Manage the agressive link power management capability.
569 void
570 ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
572 u_int32_t cmd, sctl;
574 if (link_pwr_mgmt == ap->link_pwr_mgmt)
575 return;
577 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
578 kprintf("%s: link power management not supported.\n",
579 PORTNAME(ap));
580 return;
583 ahci_os_lock_port(ap);
585 if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
586 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
587 kprintf("%s: enabling aggressive link power management.\n",
588 PORTNAME(ap));
590 ap->link_pwr_mgmt = link_pwr_mgmt;
592 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
593 ahci_port_interrupt_enable(ap);
595 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
596 sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED);
597 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
600 * Enable device initiated link power management for
601 * directly attached devices that support it.
603 if (ap->ap_type != ATA_PORT_T_PM &&
604 ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) {
605 if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
606 kprintf("%s: Could not enable device initiated "
607 "link power management.\n",
608 PORTNAME(ap));
611 cmd = ahci_pread(ap, AHCI_PREG_CMD);
612 cmd |= AHCI_PREG_CMD_ASP;
613 cmd |= AHCI_PREG_CMD_ALPE;
614 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
616 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
617 (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
618 kprintf("%s: enabling medium link power management.\n",
619 PORTNAME(ap));
621 ap->link_pwr_mgmt = link_pwr_mgmt;
623 ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
624 ahci_port_interrupt_enable(ap);
626 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
627 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
628 sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL;
629 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
631 cmd = ahci_pread(ap, AHCI_PREG_CMD);
632 cmd &= ~AHCI_PREG_CMD_ASP;
633 cmd |= AHCI_PREG_CMD_ALPE;
634 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
636 } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
637 kprintf("%s: disabling link power management.\n",
638 PORTNAME(ap));
640 /* Disable device initiated link power management */
641 if (ap->ap_type != ATA_PORT_T_PM &&
642 ap->ap_ata[0]->at_identify.satafsup & (1 << 3))
643 ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
645 cmd = ahci_pread(ap, AHCI_PREG_CMD);
646 cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
647 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
649 sctl = ahci_pread(ap, AHCI_PREG_SCTL);
650 sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
651 ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
653 /* let the drive come back to avoid PRCS interrupts later */
654 ahci_os_unlock_port(ap);
655 ahci_os_sleep(1000);
656 ahci_os_lock_port(ap);
658 ahci_pwrite(ap, AHCI_PREG_SERR,
659 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
660 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
662 ap->ap_intmask |= AHCI_PREG_IE_PRCE;
663 ahci_port_interrupt_enable(ap);
665 ap->link_pwr_mgmt = link_pwr_mgmt;
666 } else {
667 kprintf("%s: unsupported link power management state %d.\n",
668 PORTNAME(ap), link_pwr_mgmt);
671 ahci_os_unlock_port(ap);
675 * Return current link power state.
678 ahci_port_link_pwr_state(struct ahci_port *ap)
680 uint32_t r;
682 r = ahci_pread(ap, AHCI_PREG_SSTS);
683 switch (r & SATA_PM_SSTS_IPM) {
684 case SATA_PM_SSTS_IPM_ACTIVE:
685 return 1;
686 case SATA_PM_SSTS_IPM_PARTIAL:
687 return 2;
688 case SATA_PM_SSTS_IPM_SLUMBER:
689 return 3;
690 default:
691 return 0;
696 * Run the port / target state machine from a main context.
698 * The state machine for the port is always run.
700 * If atx is non-NULL run the state machine for a particular target.
701 * If atx is NULL run the state machine for all targets.
703 void
704 ahci_port_state_machine(struct ahci_port *ap, int initial)
706 struct ata_port *at;
707 u_int32_t data;
708 int target;
709 int didsleep;
710 int loop;
713 * State machine for port. Note that CAM is not yet associated
714 * during the initial parallel probe and the port's probe state
715 * will not get past ATA_PROBE_NEED_IDENT.
718 if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
719 kprintf("%s: Waiting 10 seconds on insertion\n",
720 PORTNAME(ap));
721 ahci_os_sleep(10000);
722 initial = 1;
724 if (ap->ap_probe == ATA_PROBE_NEED_INIT)
725 ahci_port_init(ap);
726 if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
727 ahci_port_reset(ap, NULL, 1);
728 if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
729 ahci_port_reset(ap, NULL, 0);
730 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
731 ahci_cam_probe(ap, NULL);
733 if (ap->ap_type != ATA_PORT_T_PM) {
734 if (ap->ap_probe == ATA_PROBE_FAILED) {
735 ahci_cam_changed(ap, NULL, 0);
736 } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
737 ahci_cam_changed(ap, NULL, 1);
739 return;
743 * Port Multiplier state machine.
745 * Get a mask of changed targets and combine with any runnable
746 * states already present.
748 for (loop = 0; ;++loop) {
749 if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
750 kprintf("%s: PM unable to read hot-plug bitmap\n",
751 PORTNAME(ap));
752 break;
756 * Do at least one loop, then stop if no more state changes
757 * have occured. The PM might not generate a new
758 * notification until we clear the entire bitmap.
760 if (loop && data == 0)
761 break;
764 * New devices showing up in the bitmap require some spin-up
765 * time before we start probing them. Reset didsleep. The
766 * first new device we detect will sleep before probing.
768 * This only applies to devices whos change bit is set in
769 * the data, and does not apply to the initial boot-time
770 * probe.
772 didsleep = 0;
774 for (target = 0; target < ap->ap_pmcount; ++target) {
775 at = ap->ap_ata[target];
778 * Check the target state for targets behind the PM
779 * which have changed state. This will adjust
780 * at_probe and set ATA_PORT_F_RESCAN
782 * We want to wait at least 10 seconds before probing
783 * a newly inserted device. If the check status
784 * indicates a device is present and in need of a
785 * hard reset, we make sure we have slept before
786 * continuing.
788 * We also need to wait at least 1 second for the
789 * PHY state to change after insertion, if we
790 * haven't already waited the 10 seconds.
792 * NOTE: When pm_check_good finds a good port it
793 * typically starts us in probe state
794 * NEED_HARD_RESET rather than INIT.
796 if (data & (1 << target)) {
797 if (initial == 0 && didsleep == 0)
798 ahci_os_sleep(1000);
799 ahci_pm_check_good(ap, target);
800 if (initial == 0 && didsleep == 0 &&
801 at->at_probe <= ATA_PROBE_NEED_HARD_RESET
803 didsleep = 1;
804 kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
805 ahci_os_sleep(10000);
810 * Report hot-plug events before the probe state
811 * really gets hot. Only actual events are reported
812 * here to reduce spew.
814 if (data & (1 << target)) {
815 kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
816 switch(at->at_probe) {
817 case ATA_PROBE_NEED_INIT:
818 case ATA_PROBE_NEED_HARD_RESET:
819 kprintf("Device inserted\n");
820 break;
821 case ATA_PROBE_FAILED:
822 kprintf("Device removed\n");
823 break;
824 default:
825 kprintf("Device probe in progress\n");
826 break;
831 * Run through the state machine as necessary if
832 * the port is not marked failed.
834 * The state machine may stop at NEED_IDENT if
835 * CAM is not yet attached.
837 * Acquire exclusive access to the port while we
838 * are doing this. This prevents command-completion
839 * from queueing commands for non-polled targets
840 * inbetween our probe steps. We need to do this
841 * because the reset probes can generate severe PHY
842 * and protocol errors and soft-brick the port.
844 if (at->at_probe != ATA_PROBE_FAILED &&
845 at->at_probe != ATA_PROBE_GOOD) {
846 ahci_beg_exclusive_access(ap, at);
847 if (at->at_probe == ATA_PROBE_NEED_INIT)
848 ahci_pm_port_init(ap, at);
849 if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
850 ahci_port_reset(ap, at, 1);
851 if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
852 ahci_port_reset(ap, at, 0);
853 if (at->at_probe == ATA_PROBE_NEED_IDENT)
854 ahci_cam_probe(ap, at);
855 ahci_end_exclusive_access(ap, at);
859 * Add or remove from CAM
861 if (at->at_features & ATA_PORT_F_RESCAN) {
862 at->at_features &= ~ATA_PORT_F_RESCAN;
863 if (at->at_probe == ATA_PROBE_FAILED) {
864 ahci_cam_changed(ap, at, 0);
865 } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
866 ahci_cam_changed(ap, at, 1);
869 data &= ~(1 << target);
871 if (data) {
872 kprintf("%s: WARNING (PM): extra bits set in "
873 "EINFO: %08x\n", PORTNAME(ap), data);
874 while (target < AHCI_MAX_PMPORTS) {
875 ahci_pm_check_good(ap, target);
876 ++target;
884 * De-initialize and detach a port.
886 void
887 ahci_port_free(struct ahci_softc *sc, u_int port)
889 struct ahci_port *ap = sc->sc_ports[port];
890 struct ahci_ccb *ccb;
891 int i;
894 * Ensure port is disabled and its interrupts are all flushed.
896 if (ap->ap_sc) {
897 ahci_port_stop(ap, 1);
898 ahci_os_stop_port(ap);
899 ahci_pwrite(ap, AHCI_PREG_CMD, 0);
900 ahci_pwrite(ap, AHCI_PREG_IE, 0);
901 ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
902 ahci_write(sc, AHCI_REG_IS, 1 << port);
905 if (ap->ap_ccbs) {
906 while ((ccb = ahci_get_ccb(ap)) != NULL) {
907 if (ccb->ccb_dmamap) {
908 bus_dmamap_destroy(sc->sc_tag_data,
909 ccb->ccb_dmamap);
910 ccb->ccb_dmamap = NULL;
913 if ((ccb = ap->ap_err_ccb) != NULL) {
914 if (ccb->ccb_dmamap) {
915 bus_dmamap_destroy(sc->sc_tag_data,
916 ccb->ccb_dmamap);
917 ccb->ccb_dmamap = NULL;
919 ap->ap_err_ccb = NULL;
921 kfree(ap->ap_ccbs, M_DEVBUF);
922 ap->ap_ccbs = NULL;
925 if (ap->ap_dmamem_cmd_list) {
926 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
927 ap->ap_dmamem_cmd_list = NULL;
929 if (ap->ap_dmamem_rfis) {
930 ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
931 ap->ap_dmamem_rfis = NULL;
933 if (ap->ap_dmamem_cmd_table) {
934 ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
935 ap->ap_dmamem_cmd_table = NULL;
937 if (ap->ap_ata) {
938 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
939 if (ap->ap_ata[i]) {
940 kfree(ap->ap_ata[i], M_DEVBUF);
941 ap->ap_ata[i] = NULL;
945 if (ap->ap_err_scratch) {
946 kfree(ap->ap_err_scratch, M_DEVBUF);
947 ap->ap_err_scratch = NULL;
950 /* bus_space(9) says we dont free the subregions handle */
952 kfree(ap, M_DEVBUF);
953 sc->sc_ports[port] = NULL;
956 static
957 u_int32_t
958 ahci_pactive(struct ahci_port *ap)
960 u_int32_t mask;
962 mask = ahci_pread(ap, AHCI_PREG_CI);
963 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ)
964 mask |= ahci_pread(ap, AHCI_PREG_SACT);
965 return(mask);
969 * Start high-level command processing on the port
972 ahci_port_start(struct ahci_port *ap)
974 u_int32_t r, s, is, tfd;
977 * FRE must be turned on before ST. Wait for FR to go active
978 * before turning on ST. The spec doesn't seem to think this
979 * is necessary but waiting here avoids an on-off race in the
980 * ahci_port_stop() code.
982 r = ahci_pread(ap, AHCI_PREG_CMD);
983 if ((r & AHCI_PREG_CMD_FRE) == 0) {
984 r |= AHCI_PREG_CMD_FRE;
985 ahci_pwrite(ap, AHCI_PREG_CMD, r);
987 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
988 if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
989 kprintf("%s: Cannot start FIS reception\n",
990 PORTNAME(ap));
991 return (2);
993 } else {
994 ahci_os_sleep(10);
998 * Turn on ST, wait for CR to come up.
1000 r |= AHCI_PREG_CMD_ST;
1001 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1002 if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1003 s = ahci_pread(ap, AHCI_PREG_SERR);
1004 is = ahci_pread(ap, AHCI_PREG_IS);
1005 tfd = ahci_pread(ap, AHCI_PREG_TFD);
1006 kprintf("%s: Cannot start command DMA\n"
1007 "NCMP=%b NSERR=%b\n"
1008 "NEWIS=%b\n"
1009 "NEWTFD=%b\n",
1010 PORTNAME(ap),
1011 r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
1012 is, AHCI_PFMT_IS,
1013 tfd, AHCI_PFMT_TFD_STS);
1014 return (1);
1017 #ifdef AHCI_COALESCE
1019 * (Re-)enable coalescing on the port.
1021 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1022 ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
1023 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1024 ap->ap_sc->sc_ccc_ports_cur);
1026 #endif
1028 return (0);
1032 * Stop high-level command processing on a port
1034 * WARNING! If the port is stopped while CR is still active our saved
1035 * CI/SACT will race any commands completed by the command
1036 * processor prior to being able to stop. Thus we never call
1037 * this function unless we intend to dispose of any remaining
1038 * active commands. In particular, this complicates the timeout
1039 * code.
1042 ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
1044 u_int32_t r;
1046 #ifdef AHCI_COALESCE
1048 * Disable coalescing on the port while it is stopped.
1050 if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1051 ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
1052 ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1053 ap->ap_sc->sc_ccc_ports_cur);
1055 #endif
1058 * Turn off ST, then wait for CR to go off.
1060 r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1061 r &= ~AHCI_PREG_CMD_ST;
1062 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1064 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
1065 kprintf("%s: Port bricked, unable to stop (ST)\n",
1066 PORTNAME(ap));
1067 return (1);
1070 #if 0
1072 * Turn off FRE, then wait for FR to go off. FRE cannot
1073 * be turned off until CR transitions to 0.
1075 if ((r & AHCI_PREG_CMD_FR) == 0) {
1076 kprintf("%s: FR stopped, clear FRE for next start\n",
1077 PORTNAME(ap));
1078 stop_fis_rx = 2;
1080 #endif
1081 if (stop_fis_rx) {
1082 r &= ~AHCI_PREG_CMD_FRE;
1083 ahci_pwrite(ap, AHCI_PREG_CMD, r);
1084 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
1085 kprintf("%s: Port bricked, unable to stop (FRE)\n",
1086 PORTNAME(ap));
1087 return (2);
1091 return (0);
1095 * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1098 ahci_port_clo(struct ahci_port *ap)
1100 struct ahci_softc *sc = ap->ap_sc;
1101 u_int32_t cmd;
1103 /* Only attempt CLO if supported by controller */
1104 if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
1105 return (1);
1107 /* Issue CLO */
1108 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1109 ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1111 /* Wait for completion */
1112 if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1113 kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1114 return (1);
1117 return (0);
1121 * Reset a port.
1123 * If hard is 0 perform a softreset of the port.
1124 * If hard is 1 perform a hard reset of the port.
1126 * If at is non-NULL an indirect port via a port-multiplier is being
1127 * reset, otherwise a direct port is being reset.
1129 * NOTE: Indirect ports can only be soft-reset.
1132 ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
1134 int rc;
1136 if (hard) {
1137 if (at)
1138 rc = ahci_pm_hardreset(ap, at->at_target, hard);
1139 else
1140 rc = ahci_port_hardreset(ap, hard);
1141 } else {
1142 if (at)
1143 rc = ahci_pm_softreset(ap, at->at_target);
1144 else
1145 rc = ahci_port_softreset(ap);
1147 return(rc);
1151 * AHCI soft reset, Section 10.4.1
1153 * (at) will be NULL when soft-resetting a directly-attached device, and
1154 * non-NULL when soft-resetting a device through a port multiplier.
1156 * This function keeps port communications intact and attempts to generate
1157 * a reset to the connected device using device commands.
1160 ahci_port_softreset(struct ahci_port *ap)
1162 struct ahci_ccb *ccb = NULL;
1163 struct ahci_cmd_hdr *cmd_slot;
1164 u_int8_t *fis;
1165 int error;
1167 error = EIO;
1169 if (bootverbose) {
1170 kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
1171 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
1174 DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1176 crit_enter();
1177 ap->ap_flags |= AP_F_IN_RESET;
1178 ap->ap_state = AP_S_NORMAL;
1181 * Remember port state in cmd (main to restore start/stop)
1183 * Idle port.
1185 if (ahci_port_stop(ap, 0)) {
1186 kprintf("%s: failed to stop port, cannot softreset\n",
1187 PORTNAME(ap));
1188 goto err;
1192 * Request CLO if device appears hung.
1194 if (ahci_pread(ap, AHCI_PREG_TFD) &
1195 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1196 ahci_port_clo(ap);
1200 * This is an attempt to clear errors so a new signature will
1201 * be latched. It isn't working properly. XXX
1203 ahci_flush_tfd(ap);
1204 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1206 /* Restart port */
1207 if (ahci_port_start(ap)) {
1208 kprintf("%s: failed to start port, cannot softreset\n",
1209 PORTNAME(ap));
1210 goto err;
1213 /* Check whether CLO worked */
1214 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1215 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1216 kprintf("%s: CLO %s, need port reset\n",
1217 PORTNAME(ap),
1218 (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1219 ? "failed" : "unsupported");
1220 error = EBUSY;
1221 goto err;
1225 * Prep first D2H command with SRST feature & clear busy/reset flags
1227 * It is unclear which other fields in the FIS are used. Just zero
1228 * everything.
1230 * NOTE! This CCB is used for both the first and second commands.
1231 * The second command must use CCB slot 1 to properly load
1232 * the signature.
1234 ccb = ahci_get_err_ccb(ap);
1235 ccb->ccb_xa.complete = ahci_dummy_done;
1236 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
1237 KKASSERT(ccb->ccb_slot == 1);
1238 ccb->ccb_xa.at = NULL;
1239 cmd_slot = ccb->ccb_cmd_hdr;
1241 fis = ccb->ccb_cmd_table->cfis;
1242 memset(fis, 0, sizeof(ccb->ccb_cmd_table->cfis));
1243 fis[0] = ATA_FIS_TYPE_H2D;
1244 fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1246 cmd_slot->prdtl = 0;
1247 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1248 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1249 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1251 ccb->ccb_xa.state = ATA_S_PENDING;
1253 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1254 kprintf("%s: First FIS failed\n", PORTNAME(ap));
1255 goto err;
1259 * WARNING! TIME SENSITIVE SPACE! WARNING!
1261 * The two FISes are supposed to be back to back. Don't issue other
1262 * commands or even delay if we can help it.
1266 * Prep second D2H command to read status and complete reset sequence
1267 * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA
1268 * Rev 2.6 and it is unclear how the second FIS should be set up
1269 * from the AHCI document.
1271 * It is unclear which other fields in the FIS are used. Just zero
1272 * everything.
1274 ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
1276 memset(fis, 0, sizeof(ccb->ccb_cmd_table->cfis));
1277 fis[0] = ATA_FIS_TYPE_H2D;
1278 fis[15] = ATA_FIS_CONTROL_4BIT;
1280 cmd_slot->prdtl = 0;
1281 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
1283 ccb->ccb_xa.state = ATA_S_PENDING;
1284 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
1285 kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1286 goto err;
1289 if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1290 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1291 kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1292 PORTNAME(ap),
1293 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
1294 error = EBUSY;
1295 goto err;
1299 * If the softreset is trying to clear a BSY condition after a
1300 * normal portreset we assign the port type.
1302 * If the softreset is being run first as part of the ccb error
1303 * processing code then report if the device signature changed
1304 * unexpectedly.
1306 ahci_os_sleep(100);
1307 if (ap->ap_type == ATA_PORT_T_NONE) {
1308 ap->ap_type = ahci_port_signature_detect(ap, NULL);
1309 } else {
1310 if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
1311 kprintf("%s: device signature unexpectedly "
1312 "changed\n", PORTNAME(ap));
1313 error = EBUSY; /* XXX */
1316 error = 0;
1318 ahci_os_sleep(3);
1319 err:
1320 if (ccb != NULL) {
1321 ahci_put_err_ccb(ccb);
1324 * If the target is busy use CLO to clear the busy
1325 * condition. The BSY should be cleared on the next
1326 * start.
1328 if (ahci_pread(ap, AHCI_PREG_TFD) &
1329 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1330 ahci_port_clo(ap);
1335 * If we failed to softreset make the port quiescent, otherwise
1336 * make sure the port's start/stop state matches what it was on
1337 * entry.
1339 * Don't kill the port if the softreset is on a port multiplier
1340 * target, that would kill all the targets!
1342 if (error) {
1343 ahci_port_hardstop(ap);
1344 /* ap_probe set to failed */
1345 } else {
1346 ap->ap_probe = ATA_PROBE_NEED_IDENT;
1347 ap->ap_pmcount = 1;
1348 ahci_port_start(ap);
1350 ap->ap_flags &= ~AP_F_IN_RESET;
1351 crit_exit();
1353 if (bootverbose)
1354 kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
1356 return (error);
1360 * Issue just do the core COMRESET and basic device detection on a port.
1362 * NOTE: Only called by ahci_port_hardreset().
1364 static int
1365 ahci_comreset(struct ahci_port *ap, int *pmdetectp)
1367 u_int32_t cmd;
1368 u_int32_t r;
1369 int error;
1370 int loop;
1371 int retries = 0;
1374 * Idle the port,
1376 *pmdetectp = 0;
1377 ahci_port_stop(ap, 0);
1378 ap->ap_state = AP_S_NORMAL;
1379 ahci_os_sleep(10);
1382 * The port may have been quiescent with its SUD bit cleared, so
1383 * set the SUD (spin up device).
1385 * NOTE: I do not know if SUD is a hardware pin/low-level signal
1386 * or if it is messaged.
1388 cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1390 cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD;
1391 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1392 ahci_os_sleep(10);
1395 * Make sure that all power management is disabled.
1397 * NOTE! AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable
1398 * on multiple chipsets and can brick the chipset or even
1399 * the whole PC. Never use it.
1401 ap->ap_type = ATA_PORT_T_NONE;
1403 r = AHCI_PREG_SCTL_IPM_DISABLED |
1404 AHCI_PREG_SCTL_SPM_DISABLED;
1405 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1407 retry:
1409 * Give the new power management state time to settle, then clear
1410 * pending status.
1412 ahci_os_sleep(1000);
1413 ahci_flush_tfd(ap);
1414 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1417 * Start transmitting COMRESET. The spec says that COMRESET must
1418 * be sent for at least 1ms but in actual fact numerous devices
1419 * appear to take much longer. Delay a whole second here.
1421 * In addition, SATA-3 ports can take longer to train, so even
1422 * SATA-2 devices which would normally detect very quickly may
1423 * take longer when plugged into a SATA-3 port.
1425 r |= AHCI_PREG_SCTL_DET_INIT;
1426 switch(AhciForceGen) {
1427 case 0:
1428 r |= AHCI_PREG_SCTL_SPD_ANY;
1429 break;
1430 case 1:
1431 r |= AHCI_PREG_SCTL_SPD_GEN1;
1432 break;
1433 case 2:
1434 r |= AHCI_PREG_SCTL_SPD_GEN2;
1435 break;
1436 case 3:
1437 r |= AHCI_PREG_SCTL_SPD_GEN3;
1438 break;
1439 default:
1440 r |= AHCI_PREG_SCTL_SPD_GEN3;
1441 break;
1443 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1444 ahci_os_sleep(1000);
1446 ap->ap_flags &= ~AP_F_HARSH_REINIT;
1449 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1450 * since we are hard-resetting the port we might as well clear
1451 * the whole enchillada. Also be sure to clear any spurious BSY
1452 * prior to clearing INIT.
1454 * Wait 1 whole second after clearing INIT before checking
1455 * the device detection bits in an attempt to work around chipsets
1456 * which do not properly mask PCS/PRCS during low level init.
1458 ahci_flush_tfd(ap);
1459 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1460 /* ahci_port_clo(ap);*/
1461 ahci_os_sleep(10);
1463 r &= ~AHCI_PREG_SCTL_SPD;
1464 r &= ~AHCI_PREG_SCTL_DET_INIT;
1465 r |= AHCI_PREG_SCTL_DET_NONE;
1466 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1467 ahci_os_sleep(1000);
1470 * Try to determine if there is a device on the port.
1472 * Give the device 3/10 second to at least be detected.
1473 * If we fail clear PRCS (phy detect) since we may cycled
1474 * the phy and probably caused another PRCS interrupt.
1476 loop = 300;
1477 while (loop > 0) {
1478 r = ahci_pread(ap, AHCI_PREG_SSTS);
1479 if (r & AHCI_PREG_SSTS_DET)
1480 break;
1481 loop -= ahci_os_softsleep();
1483 if (loop == 0) {
1484 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1485 if (bootverbose) {
1486 kprintf("%s: Port appears to be unplugged\n",
1487 PORTNAME(ap));
1489 error = ENODEV;
1490 goto done;
1494 * There is something on the port. Regardless of what happens
1495 * after this tell the caller to try to detect a port multiplier.
1497 * Give the device 3 seconds to fully negotiate.
1499 *pmdetectp = 1;
1501 if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
1502 AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1503 if (bootverbose) {
1504 kprintf("%s: Device may be powered down\n",
1505 PORTNAME(ap));
1507 error = ENODEV;
1508 goto done;
1512 * We got something that definitely looks like a device. Give
1513 * the device time to send us its first D2H FIS. Waiting for
1514 * BSY to clear accomplishes this.
1516 * NOTE: A port multiplier may or may not clear BSY here,
1517 * depending on what is sitting in target 0 behind it.
1519 * NOTE: Intel SSDs seem to have compatibility problems with Intel
1520 * mobo's on cold boots and may leave BSY set. A single
1521 * retry works around the problem. This is definitely a bug
1522 * with the mobo and/or the SSD and does not appear to occur
1523 * with other devices connected to the same port.
1525 ahci_flush_tfd(ap);
1526 if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD,
1527 AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1528 kprintf("%s: Device BUSY: %b\n",
1529 PORTNAME(ap),
1530 ahci_pread(ap, AHCI_PREG_TFD),
1531 AHCI_PFMT_TFD_STS);
1532 if (retries == 0) {
1533 kprintf("%s: Retrying\n", PORTNAME(ap));
1534 retries = 1;
1535 goto retry;
1537 error = EBUSY;
1538 } else {
1539 error = 0;
1542 done:
1543 ahci_flush_tfd(ap);
1544 return error;
1549 * AHCI port reset, Section 10.4.2
1551 * This function does a hard reset of the port. Note that the device
1552 * connected to the port could still end-up hung.
1555 ahci_port_hardreset(struct ahci_port *ap, int hard)
1557 u_int32_t data;
1558 int error;
1559 int pmdetect;
1561 if (bootverbose)
1562 kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1563 ap->ap_flags |= AP_F_IN_RESET;
1565 error = ahci_comreset(ap, &pmdetect);
1568 * We may be asked to perform a port multiplier check even if the
1569 * comreset failed. This typically occurs when the PM has nothing
1570 * in slot 0, which can cause BSY to remain set.
1572 * If the PM detection is successful it will override (error),
1573 * otherwise (error) is retained. If an error does occur it
1574 * is possible that a normal device has blown up on us DUE to
1575 * the PM detection code, so re-run the comreset and assume
1576 * a normal device.
1578 if (pmdetect) {
1579 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) {
1580 error = ahci_pm_port_probe(ap, error);
1581 if (error) {
1582 error = ahci_comreset(ap, &pmdetect);
1588 * Finish up.
1590 ahci_os_sleep(500);
1592 switch(error) {
1593 case 0:
1595 * All good, make sure the port is running and set the
1596 * probe state. Ignore the signature junk (it's unreliable)
1597 * until we get to the softreset code.
1599 if (ahci_port_start(ap)) {
1600 kprintf("%s: failed to start command DMA on port, "
1601 "disabling\n", PORTNAME(ap));
1602 error = EBUSY;
1603 break;
1605 if (ap->ap_type == ATA_PORT_T_PM)
1606 ap->ap_probe = ATA_PROBE_GOOD;
1607 else
1608 ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
1609 break;
1610 case ENODEV:
1612 * Normal device probe failure
1614 data = ahci_pread(ap, AHCI_PREG_SSTS);
1616 switch(data & AHCI_PREG_SSTS_DET) {
1617 case AHCI_PREG_SSTS_DET_DEV_NE:
1618 kprintf("%s: Device not communicating\n",
1619 PORTNAME(ap));
1620 break;
1621 case AHCI_PREG_SSTS_DET_PHYOFFLINE:
1622 kprintf("%s: PHY offline\n",
1623 PORTNAME(ap));
1624 break;
1625 default:
1626 kprintf("%s: No device detected\n",
1627 PORTNAME(ap));
1628 break;
1630 ahci_port_hardstop(ap);
1631 break;
1632 default:
1634 * Abnormal probe (EBUSY)
1636 kprintf("%s: Device on port is bricked\n",
1637 PORTNAME(ap));
1638 ahci_port_hardstop(ap);
1639 #if 0
1640 rc = ahci_port_reset(ap, atx, 0);
1641 if (rc) {
1642 kprintf("%s: Unable unbrick device\n",
1643 PORTNAME(ap));
1644 } else {
1645 kprintf("%s: Successfully unbricked\n",
1646 PORTNAME(ap));
1648 #endif
1649 break;
1653 * Clean up
1655 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1656 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
1658 ap->ap_flags &= ~AP_F_IN_RESET;
1660 if (bootverbose)
1661 kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1662 return (error);
1666 * Hard-stop on hot-swap device removal. See 10.10.1
1668 * Place the port in a mode that will allow it to detect hot-swap insertions.
1669 * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1670 * seem to do the job.
1672 * FIS reception is left enabled but command processing is disabled.
1673 * Cycling FIS reception (FRE) can brick ports.
1675 void
1676 ahci_port_hardstop(struct ahci_port *ap)
1678 struct ahci_ccb *ccb;
1679 struct ata_port *at;
1680 u_int32_t r;
1681 u_int32_t cmd;
1682 int slot;
1683 int i;
1684 int serial;
1687 * Stop the port. We can't modify things like SUD if the port
1688 * is running.
1690 ap->ap_state = AP_S_FATAL_ERROR;
1691 ap->ap_probe = ATA_PROBE_FAILED;
1692 ap->ap_type = ATA_PORT_T_NONE;
1693 ahci_port_stop(ap, 0);
1694 cmd = ahci_pread(ap, AHCI_PREG_CMD);
1695 cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC);
1696 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1699 * Clean up AT sub-ports on SATA port.
1701 for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1702 at = ap->ap_ata[i];
1703 at->at_type = ATA_PORT_T_NONE;
1704 at->at_probe = ATA_PROBE_FAILED;
1708 * Make sure FRE is active. There isn't anything we can do if it
1709 * fails so just ignore errors.
1711 if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1712 cmd |= AHCI_PREG_CMD_FRE;
1713 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1714 if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1715 ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1719 * 10.10.1 place us in the Listen state.
1721 * 10.10.3 DET must be set to 0 and found to be 0 before
1722 * setting SUD to 0.
1724 * Deactivating SUD only applies if the controller supports SUD, it
1725 * is a bit unclear what happens w/regards to detecting hotplug
1726 * if it doesn't.
1728 r = AHCI_PREG_SCTL_IPM_DISABLED |
1729 AHCI_PREG_SCTL_SPM_DISABLED;
1730 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1731 ahci_os_sleep(10);
1732 cmd &= ~AHCI_PREG_CMD_SUD;
1733 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1734 ahci_os_sleep(10);
1737 * 10.10.1
1739 * Transition su to the spin-up state. HBA shall send COMRESET and
1740 * begin initialization sequence (whatever that means). Presumably
1741 * this is edge-triggered. Following the spin-up state the HBA
1742 * will automatically transition to the Normal state.
1744 * This only applies if the controller supports SUD.
1745 * NEVER use AHCI_PREG_DET_DISABLE.
1747 cmd |= AHCI_PREG_CMD_POD |
1748 AHCI_PREG_CMD_SUD |
1749 AHCI_PREG_CMD_ICC_ACTIVE;
1750 ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1751 ahci_os_sleep(10);
1754 * Flush SERR_DIAG_X so the TFD can update.
1756 ahci_flush_tfd(ap);
1759 * Clean out pending ccbs
1761 restart:
1762 while (ap->ap_active) {
1763 slot = ffs(ap->ap_active) - 1;
1764 ap->ap_active &= ~(1 << slot);
1765 --ap->ap_active_cnt;
1766 ccb = &ap->ap_ccbs[slot];
1767 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1768 serial = ccb->ccb_xa.serial;
1769 callout_stop_sync(&ccb->ccb_timeout);
1770 if (serial != ccb->ccb_xa.serial) {
1771 kprintf("%s: Warning: timeout race ccb %p\n",
1772 PORTNAME(ap), ccb);
1773 goto restart;
1775 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1777 ap->ap_expired &= ~(1 << slot);
1778 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1779 ATA_F_TIMEOUT_EXPIRED);
1780 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1781 ccb->ccb_done(ccb);
1782 ccb->ccb_xa.complete(&ccb->ccb_xa);
1784 while (ap->ap_sactive) {
1785 slot = ffs(ap->ap_sactive) - 1;
1786 ap->ap_sactive &= ~(1 << slot);
1787 ccb = &ap->ap_ccbs[slot];
1788 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1789 serial = ccb->ccb_xa.serial;
1790 callout_stop_sync(&ccb->ccb_timeout);
1791 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
1792 if (serial != ccb->ccb_xa.serial) {
1793 kprintf("%s: Warning: timeout race ccb %p\n",
1794 PORTNAME(ap), ccb);
1795 goto restart;
1798 ap->ap_expired &= ~(1 << slot);
1799 ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
1800 ATA_F_TIMEOUT_EXPIRED);
1801 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1802 ccb->ccb_done(ccb);
1803 ccb->ccb_xa.complete(&ccb->ccb_xa);
1805 KKASSERT(ap->ap_active_cnt == 0);
1807 while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
1808 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1809 ccb->ccb_xa.state = ATA_S_TIMEOUT;
1810 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
1811 ccb->ccb_done(ccb);
1812 ccb->ccb_xa.complete(&ccb->ccb_xa);
1816 * Hot-plug device detection should work at this point. e.g. on
1817 * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug
1818 * detection and entering RESET (continuous COMRESET by setting INIT)
1819 * will actually prevent hot-plug detection from working properly.
1821 * There may be cases where this will fail to work, I have some
1822 * additional code to place the HBA in RESET (send continuous
1823 * COMRESET) and hopefully get DIAG.X or other events when something
1824 * is plugged in. Unfortunately this isn't universal and can
1825 * also prevent events from generating interrupts.
1828 #if 0
1830 * Transition us to the Reset state. Theoretically we send a
1831 * continuous stream of COMRESETs in this state.
1833 r |= AHCI_PREG_SCTL_DET_INIT;
1834 if (AhciForceGen1 & (1 << ap->ap_num)) {
1835 kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1836 r |= AHCI_PREG_SCTL_SPD_GEN1;
1837 } else {
1838 r |= AHCI_PREG_SCTL_SPD_ANY;
1840 ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1841 ahci_os_sleep(10);
1844 * Flush SERR_DIAG_X so the TFD can update.
1846 ahci_flush_tfd(ap);
1847 #endif
1848 /* NOP */
1852 * We can't loop on the X bit, a continuous COMINIT received will make
1853 * it loop forever. Just assume one event has built up and clear X
1854 * so the task file descriptor can update.
1856 void
1857 ahci_flush_tfd(struct ahci_port *ap)
1859 u_int32_t r;
1861 r = ahci_pread(ap, AHCI_PREG_SERR);
1862 if (r & AHCI_PREG_SERR_DIAG_X)
1863 ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1867 * Figure out what type of device is connected to the port, ATAPI or
1868 * DISK.
1871 ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1873 u_int32_t sig;
1875 sig = ahci_pread(ap, AHCI_PREG_SIG);
1876 if (bootverbose)
1877 kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1878 if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1879 return(ATA_PORT_T_ATAPI);
1880 } else if ((sig & 0xffff0000) ==
1881 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
1882 return(ATA_PORT_T_PM);
1883 } else {
1884 return(ATA_PORT_T_DISK);
1889 * Load the DMA descriptor table for a CCB's buffer.
1892 ahci_load_prdt(struct ahci_ccb *ccb)
1894 struct ahci_port *ap = ccb->ccb_port;
1895 struct ahci_softc *sc = ap->ap_sc;
1896 struct ata_xfer *xa = &ccb->ccb_xa;
1897 struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt;
1898 bus_dmamap_t dmap = ccb->ccb_dmamap;
1899 struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr;
1900 int error;
1902 if (xa->datalen == 0) {
1903 ccb->ccb_cmd_hdr->prdtl = 0;
1904 return (0);
1907 error = bus_dmamap_load(sc->sc_tag_data, dmap,
1908 xa->data, xa->datalen,
1909 ahci_load_prdt_callback,
1910 &prdt,
1911 ((xa->flags & ATA_F_NOWAIT) ?
1912 BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1913 if (error != 0) {
1914 kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1915 return (1);
1917 #if 0
1918 if (xa->flags & ATA_F_PIO)
1919 prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1920 #endif
1922 cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1924 if (xa->flags & ATA_F_READ)
1925 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1926 if (xa->flags & ATA_F_WRITE)
1927 bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1929 return (0);
1933 * Callback from BUSDMA system to load the segment list. The passed segment
1934 * list is a temporary structure.
1936 static
1937 void
1938 ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1939 int error)
1941 struct ahci_prdt *prd = *(void **)info;
1942 u_int64_t addr;
1944 KKASSERT(nsegs <= AHCI_MAX_PRDT);
1946 while (nsegs) {
1947 addr = segs->ds_addr;
1948 prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1949 prd->dba_lo = htole32((u_int32_t)addr);
1950 prd->flags = htole32(segs->ds_len - 1);
1951 --nsegs;
1952 if (nsegs)
1953 ++prd;
1954 ++segs;
1956 *(void **)info = prd; /* return last valid segment */
1959 void
1960 ahci_unload_prdt(struct ahci_ccb *ccb)
1962 struct ahci_port *ap = ccb->ccb_port;
1963 struct ahci_softc *sc = ap->ap_sc;
1964 struct ata_xfer *xa = &ccb->ccb_xa;
1965 bus_dmamap_t dmap = ccb->ccb_dmamap;
1967 if (xa->datalen != 0) {
1968 if (xa->flags & ATA_F_READ) {
1969 bus_dmamap_sync(sc->sc_tag_data, dmap,
1970 BUS_DMASYNC_POSTREAD);
1972 if (xa->flags & ATA_F_WRITE) {
1973 bus_dmamap_sync(sc->sc_tag_data, dmap,
1974 BUS_DMASYNC_POSTWRITE);
1976 bus_dmamap_unload(sc->sc_tag_data, dmap);
1979 * prdbc is only updated by hardware for non-NCQ commands.
1981 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1982 xa->resid = 0;
1983 } else {
1984 #if 0
1985 if (ccb->ccb_cmd_hdr->prdbc == 0 &&
1986 ccb->ccb_xa.state == ATA_S_COMPLETE) {
1987 kprintf("%s: WARNING! Unload prdbc resid "
1988 "was zero! tag=%d\n",
1989 ATANAME(ap, xa->at), ccb->ccb_slot);
1991 #endif
1992 xa->resid = xa->datalen -
1993 le32toh(ccb->ccb_cmd_hdr->prdbc);
1999 * Start a command and poll for completion.
2001 * timeout is in ms and only counts once the command gets on-chip.
2003 * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
2004 * that no error occured.
2006 * NOTE: If the caller specifies a NULL timeout function the caller is
2007 * responsible for clearing hardware state on failure, but we will
2008 * deal with removing the ccb from any pending queue.
2010 * NOTE: NCQ should never be used with this function.
2012 * NOTE: If the port is in a failed state and stopped we do not try
2013 * to activate the ccb.
2016 ahci_poll(struct ahci_ccb *ccb, int timeout,
2017 void (*timeout_fn)(struct ahci_ccb *))
2019 struct ahci_port *ap = ccb->ccb_port;
2021 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
2022 ccb->ccb_xa.state = ATA_S_ERROR;
2023 return(ccb->ccb_xa.state);
2025 crit_enter();
2026 #if 0
2027 kprintf("%s: Start command %02x tag=%d\n",
2028 ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
2029 ccb->ccb_xa.fis->command, ccb->ccb_slot);
2030 #endif
2031 ahci_start(ccb);
2033 do {
2034 ahci_port_intr(ap, 1);
2035 switch(ccb->ccb_xa.state) {
2036 case ATA_S_ONCHIP:
2037 timeout -= ahci_os_softsleep();
2038 break;
2039 case ATA_S_PENDING:
2040 ahci_os_softsleep();
2041 ahci_check_active_timeouts(ap);
2042 break;
2043 default:
2044 crit_exit();
2045 return (ccb->ccb_xa.state);
2047 } while (timeout > 0);
2049 if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) {
2050 kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
2051 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
2052 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2053 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
2054 ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
2057 timeout_fn(ccb);
2059 crit_exit();
2061 return(ccb->ccb_xa.state);
2065 * When polling we have to check if the currently active CCB(s)
2066 * have timed out as the callout will be deadlocked while we
2067 * hold the port lock.
2069 void
2070 ahci_check_active_timeouts(struct ahci_port *ap)
2072 struct ahci_ccb *ccb;
2073 u_int32_t mask;
2074 int tag;
2076 mask = ap->ap_active | ap->ap_sactive;
2077 while (mask) {
2078 tag = ffs(mask) - 1;
2079 mask &= ~(1 << tag);
2080 ccb = &ap->ap_ccbs[tag];
2081 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2082 ahci_ata_cmd_timeout(ccb);
2087 static
2088 __inline
2089 void
2090 ahci_start_timeout(struct ahci_ccb *ccb)
2092 if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
2093 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
2094 callout_reset(&ccb->ccb_timeout,
2095 (ccb->ccb_xa.timeout * hz + 999) / 1000,
2096 ahci_ata_cmd_timeout_unserialized, ccb);
2100 void
2101 ahci_start(struct ahci_ccb *ccb)
2103 struct ahci_port *ap = ccb->ccb_port;
2104 struct ahci_softc *sc = ap->ap_sc;
2106 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2108 /* Zero transferred byte count before transfer */
2109 ccb->ccb_cmd_hdr->prdbc = 0;
2111 /* Sync command list entry and corresponding command table entry */
2112 bus_dmamap_sync(sc->sc_tag_cmdh,
2113 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2114 BUS_DMASYNC_PREWRITE);
2115 bus_dmamap_sync(sc->sc_tag_cmdt,
2116 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2117 BUS_DMASYNC_PREWRITE);
2119 /* Prepare RFIS area for write by controller */
2120 bus_dmamap_sync(sc->sc_tag_rfis,
2121 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2122 BUS_DMASYNC_PREREAD);
2125 * There's no point trying to optimize this, it only shaves a few
2126 * nanoseconds so just queue the command and call our generic issue.
2128 ahci_issue_pending_commands(ap, ccb);
2132 * While holding the port lock acquire exclusive access to the port.
2134 * This is used when running the state machine to initialize and identify
2135 * targets over a port multiplier. Setting exclusive access prevents
2136 * ahci_port_intr() from activating any requests sitting on the pending
2137 * queue.
2139 void
2140 ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2142 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
2143 ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
2144 while (ap->ap_active || ap->ap_sactive) {
2145 ahci_port_intr(ap, 1);
2146 ahci_os_softsleep();
2150 void
2151 ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2153 KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2154 ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
2155 ahci_issue_pending_commands(ap, NULL);
2159 * If ccb is not NULL enqueue and/or issue it.
2161 * If ccb is NULL issue whatever we can from the queue. However, nothing
2162 * new is issued if the exclusive access flag is set or expired ccb's are
2163 * present.
2165 * If existing commands are still active (ap_active/ap_sactive) we can only
2166 * issue matching new commands.
2168 void
2169 ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
2171 u_int32_t mask;
2172 int limit;
2175 * Enqueue the ccb.
2177 * If just running the queue and in exclusive access mode we
2178 * just return. Also in this case if there are any expired ccb's
2179 * we want to clear the queue so the port can be safely stopped.
2181 if (ccb) {
2182 TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
2183 } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
2184 return;
2188 * Pull the next ccb off the queue and run it if possible.
2190 * The error CCB supercedes all normal queue operations and
2191 * implies exclusive access while the error CCB is active.
2193 if (ccb != ap->ap_err_ccb) {
2194 if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
2195 return;
2196 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2197 kprintf("DELAY CCB slot %d\n", ccb->ccb_slot);
2198 return;
2203 * Handle exclusivity requirements.
2205 * ATA_F_EXCLUSIVE is used when we want to be the only command
2206 * running.
2208 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
2209 * back into the ccb on a normal (non-errored) command completion.
2210 * For example, for PM requests to target 15. Because the AHCI
2211 * spec does not stop the command processor and has only one rfis
2212 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
2213 * Otherwise multiple completions can destroy the rfis data before
2214 * we have a chance to copy it.
2216 if (ap->ap_active & ~ap->ap_expired) {
2218 * There may be multiple ccb's already running,
2219 * if any are running and ap_run_flags sets
2220 * one of these flags then we know only one is
2221 * running.
2223 * XXX Current AUTOSENSE code forces exclusivity
2224 * to simplify the code.
2226 if (ap->ap_run_flags &
2227 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2228 return;
2231 if (ccb->ccb_xa.flags &
2232 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
2233 return;
2237 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2239 * The next command is a NCQ command and can be issued as
2240 * long as currently active commands are not standard.
2242 if (ap->ap_active) {
2243 KKASSERT(ap->ap_active_cnt > 0);
2244 return;
2246 KKASSERT(ap->ap_active_cnt == 0);
2248 mask = 0;
2249 do {
2250 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2251 KKASSERT((mask & (1 << ccb->ccb_slot)) == 0);
2252 mask |= 1 << ccb->ccb_slot;
2253 KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2254 KKASSERT(ccb == &ap->ap_ccbs[ccb->ccb_slot]);
2255 ccb->ccb_xa.state = ATA_S_ONCHIP;
2256 ahci_start_timeout(ccb);
2257 ap->ap_run_flags = ccb->ccb_xa.flags;
2258 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2259 } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
2260 (ap->ap_run_flags &
2261 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
2263 KKASSERT(((ap->ap_active | ap->ap_sactive) & mask) == 0);
2265 ap->ap_sactive |= mask;
2266 ahci_pwrite(ap, AHCI_PREG_SACT, mask);
2267 ahci_pwrite(ap, AHCI_PREG_CI, mask);
2268 } else {
2270 * The next command is a standard command and can be issued
2271 * as long as currently active commands are not NCQ.
2273 * We limit ourself to 1 command if we have a port multiplier,
2274 * (at least without FBSS support), otherwise timeouts on
2275 * one port can race completions on other ports (see
2276 * ahci_ata_cmd_timeout() for more information).
2278 * If not on a port multiplier generally allow up to 4
2279 * standard commands to be enqueued. Remember that the
2280 * command processor will still process them sequentially.
2282 if (ap->ap_sactive)
2283 return;
2284 if (ap->ap_type == ATA_PORT_T_PM)
2285 limit = 1;
2286 else if (ap->ap_sc->sc_ncmds > 4)
2287 limit = 4;
2288 else
2289 limit = 2;
2291 while (ap->ap_active_cnt < limit && ccb &&
2292 (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
2293 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2294 KKASSERT(((ap->ap_active | ap->ap_sactive) &
2295 (1 << ccb->ccb_slot)) == 0);
2296 ap->ap_active |= 1 << ccb->ccb_slot;
2297 ap->ap_active_cnt++;
2298 ap->ap_run_flags = ccb->ccb_xa.flags;
2299 ccb->ccb_xa.state = ATA_S_ONCHIP;
2300 ahci_start_timeout(ccb);
2301 ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
2302 if ((ap->ap_run_flags &
2303 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
2304 break;
2306 ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
2307 if (ccb && (ccb->ccb_xa.flags &
2308 (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
2309 break;
2315 void
2316 ahci_intr(void *arg)
2318 struct ahci_softc *sc = arg;
2319 struct ahci_port *ap;
2320 u_int32_t is;
2321 u_int32_t ack;
2322 int port;
2325 * Check if the master enable is up, and whether any interrupts are
2326 * pending.
2328 if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2329 return;
2330 is = ahci_read(sc, AHCI_REG_IS);
2331 if (is == 0 || is == 0xffffffff) {
2332 return;
2334 is &= sc->sc_portmask;
2336 #ifdef AHCI_COALESCE
2337 /* Check coalescing interrupt first */
2338 if (is & sc->sc_ccc_mask) {
2339 DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2340 DEVNAME(sc));
2341 is &= ~sc->sc_ccc_mask;
2342 is |= sc->sc_ccc_ports_cur;
2344 #endif
2347 * Process interrupts for each port in a non-blocking fashion.
2349 * The global IS bit is supposed to be forced on if any unmasked
2350 * port interrupt is pending, even if we clear it.
2352 * However it would appear that it is simply latched on some parts,
2353 * which means we have to clear it BEFORE processing the status bits
2354 * to avoid races.
2356 ahci_write(sc, AHCI_REG_IS, is);
2357 for (ack = 0; is; is &= ~(1 << port)) {
2358 port = ffs(is) - 1;
2359 ack |= 1 << port;
2361 ap = sc->sc_ports[port];
2362 if (ap == NULL)
2363 continue;
2365 if (ahci_os_lock_port_nb(ap) == 0) {
2366 ahci_port_intr(ap, 0);
2367 ahci_os_unlock_port(ap);
2368 } else {
2369 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2370 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2376 * Core called from helper thread.
2378 void
2379 ahci_port_thread_core(struct ahci_port *ap, int mask)
2382 * Process any expired timedouts.
2384 ahci_os_lock_port(ap);
2385 if (mask & AP_SIGF_TIMEOUT) {
2386 ahci_check_active_timeouts(ap);
2390 * Process port interrupts which require a higher level of
2391 * intervention.
2393 if (mask & AP_SIGF_PORTINT) {
2394 ahci_port_intr(ap, 1);
2395 ahci_port_interrupt_enable(ap);
2396 } else if (ap->ap_probe != ATA_PROBE_FAILED) {
2397 ahci_port_intr(ap, 1);
2398 ahci_port_interrupt_enable(ap);
2400 ahci_os_unlock_port(ap);
2404 * Core per-port interrupt handler.
2406 * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2407 * deal with normal command completions which do not require blocking.
2409 void
2410 ahci_port_intr(struct ahci_port *ap, int blockable)
2412 struct ahci_softc *sc = ap->ap_sc;
2413 u_int32_t is, ci_saved, ci_masked;
2414 int slot;
2415 int stopped = 0;
2416 struct ahci_ccb *ccb = NULL;
2417 struct ata_port *ccb_at = NULL;
2418 volatile u_int32_t *active;
2419 const u_int32_t blockable_mask = AHCI_PREG_IS_TFES |
2420 AHCI_PREG_IS_IFS |
2421 AHCI_PREG_IS_PCS |
2422 AHCI_PREG_IS_PRCS |
2423 AHCI_PREG_IS_HBFS |
2424 AHCI_PREG_IS_OFS |
2425 AHCI_PREG_IS_UFS;
2427 enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART,
2428 NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2431 * All basic command completions are always processed.
2433 is = ahci_pread(ap, AHCI_PREG_IS);
2434 if (is & AHCI_PREG_IS_DPS)
2435 ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2438 * If we can't block then we can't handle these here. Disable
2439 * the interrupts in question so we don't live-lock, the helper
2440 * thread will re-enable them.
2442 * If the port is in a completely failed state we do not want
2443 * to drop through to failed-command-processing if blockable is 0,
2444 * just let the thread deal with it all.
2446 * Otherwise we fall through and still handle DHRS and any commands
2447 * which completed normally. Even if we are errored we haven't
2448 * stopped the port yet so CI/SACT are still good.
2450 if (blockable == 0) {
2451 if (ap->ap_state == AP_S_FATAL_ERROR) {
2452 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2453 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2454 return;
2456 if (is & blockable_mask) {
2457 ahci_pwrite(ap, AHCI_PREG_IE, 0);
2458 ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2459 return;
2464 * Either NCQ or non-NCQ commands will be active, never both.
2466 if (ap->ap_sactive) {
2467 KKASSERT(ap->ap_active == 0);
2468 KKASSERT(ap->ap_active_cnt == 0);
2469 ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2470 active = &ap->ap_sactive;
2471 } else {
2472 ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2473 active = &ap->ap_active;
2475 KKASSERT(!(ap->ap_sactive && ap->ap_active));
2476 KKASSERT((ci_saved & (ap->ap_sactive | ap->ap_active)) == ci_saved);
2477 #if 0
2478 kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
2479 ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
2480 ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
2481 #endif
2484 * Ignore AHCI_PREG_IS_PRCS when link power management is on
2486 if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2487 is &= ~AHCI_PREG_IS_PRCS;
2488 ahci_pwrite(ap, AHCI_PREG_SERR,
2489 AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2493 * Command failed (blockable).
2495 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
2497 * This stops command processing.
2499 if (is & AHCI_PREG_IS_TFES) {
2500 u_int32_t tfd, serr, cmd;
2501 int err_slot;
2503 process_error:
2504 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2505 serr = ahci_pread(ap, AHCI_PREG_SERR);
2506 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2509 * Load the error slot and restart command processing.
2510 * CLO if we need to. The error slot may not be valid.
2511 * MUST BE DONE BEFORE CLEARING ST!
2513 * Cycle ST.
2515 * It is unclear but we may have to clear SERR to reenable
2516 * error processing.
2518 if (cmd & AHCI_PREG_CMD_ST) {
2519 err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
2520 } else {
2521 err_slot = -1;
2523 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
2524 AHCI_PREG_IS_PSS |
2525 AHCI_PREG_IS_DHRS |
2526 AHCI_PREG_IS_SDBS);
2527 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
2528 AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
2529 ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2530 ahci_port_stop(ap, 0);
2531 ahci_os_hardsleep(10);
2532 if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
2533 kprintf("%s: Issuing CLO\n", PORTNAME(ap));
2534 ahci_port_clo(ap);
2538 * We are now stopped and need a restart. If we have to
2539 * process a NCQ error we will temporarily start and then
2540 * stop the port again, so this condition holds.
2542 stopped = 1;
2543 need = NEED_RESTART;
2546 * ATAPI errors are fairly common from probing, just
2547 * report disk errors or if bootverbose is on.
2549 if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
2550 kprintf("%s: TFES slot %d ci_saved = %08x\n",
2551 PORTNAME(ap), err_slot, ci_saved);
2555 * If we got an error on an error CCB just complete it
2556 * with an error. ci_saved has the mask to restart
2557 * (the err_ccb will be removed from it by finish_error).
2559 if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2560 err_slot = ap->ap_err_ccb->ccb_slot;
2561 goto finish_error;
2565 * If NCQ commands were active get the error slot from
2566 * the log page. NCQ is not supported for PM's so this
2567 * is a direct-attached target.
2569 * Otherwise if no commands were active we have a problem.
2571 * Otherwise if the error slot is bad we have a problem.
2573 * Otherwise process the error for the slot.
2575 if (ap->ap_sactive) {
2576 ahci_port_start(ap);
2577 err_slot = ahci_port_read_ncq_error(ap, 0);
2578 ahci_port_stop(ap, 0);
2579 } else if (ap->ap_active == 0) {
2580 kprintf("%s: TFES with no commands pending\n",
2581 PORTNAME(ap));
2582 err_slot = -1;
2583 } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
2584 kprintf("%s: bad error slot %d\n",
2585 PORTNAME(ap), err_slot);
2586 err_slot = -1;
2587 } else {
2588 ccb = &ap->ap_ccbs[err_slot];
2591 * Validate the errored ccb. Note that ccb_at can
2592 * be NULL for direct-attached ccb's.
2594 * Copy received taskfile data from the RFIS.
2596 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
2597 ccb_at = ccb->ccb_xa.at;
2598 memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2599 sizeof(struct ata_fis_d2h));
2600 if (bootverbose) {
2601 kprintf("%s: Copying rfis slot %d\n",
2602 ATANAME(ap, ccb_at), err_slot);
2604 } else {
2605 kprintf("%s: Cannot copy rfis, CCB slot "
2606 "%d is not on-chip (state=%d)\n",
2607 ATANAME(ap, ccb->ccb_xa.at),
2608 err_slot, ccb->ccb_xa.state);
2609 err_slot = -1;
2614 * If we could not determine the errored slot then
2615 * reset the port.
2617 if (err_slot < 0) {
2618 kprintf("%s: TFES: Unable to determine errored slot\n",
2619 PORTNAME(ap));
2620 if (ap->ap_flags & AP_F_IN_RESET)
2621 goto fatal;
2622 goto failall;
2626 * Finish error on slot. We will restart ci_saved
2627 * commands except the errored slot which we generate
2628 * a failure for.
2630 finish_error:
2631 ccb = &ap->ap_ccbs[err_slot];
2632 ci_saved &= ~(1 << err_slot);
2633 KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2634 ccb->ccb_xa.state = ATA_S_ERROR;
2635 } else if (is & AHCI_PREG_IS_DHRS) {
2637 * Command posted D2H register FIS to the rfis (non-blocking).
2639 * A normal completion with an error may set DHRS instead
2640 * of TFES. The CCS bits are only valid if ERR was set.
2641 * If ERR is set command processing was probably stopped.
2643 * If ERR was not set we can only copy-back data for
2644 * exclusive-mode commands because otherwise we won't know
2645 * which tag the rfis belonged to.
2647 * err_slot must be read from the CCS before any other port
2648 * action, such as stopping the port.
2650 * WARNING! This is not well documented in the AHCI spec.
2651 * It can be found in the state machine tables
2652 * but not in the explanations.
2654 u_int32_t tfd;
2655 u_int32_t cmd;
2656 int err_slot;
2658 tfd = ahci_pread(ap, AHCI_PREG_TFD);
2659 cmd = ahci_pread(ap, AHCI_PREG_CMD);
2661 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2662 if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
2663 (cmd & AHCI_PREG_CMD_CR) == 0) {
2664 err_slot = AHCI_PREG_CMD_CCS(
2665 ahci_pread(ap, AHCI_PREG_CMD));
2666 ccb = &ap->ap_ccbs[err_slot];
2667 kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
2668 PORTNAME(ap),
2669 tfd, AHCI_PFMT_TFD_STS,
2670 err_slot, ccb->ccb_xa.fis->command);
2671 goto process_error;
2674 * NO ELSE... copy back is in the normal command completion
2675 * code and only if no error occured and ATA_F_AUTOSENSE
2676 * was set.
2681 * Device notification to us (non-blocking)
2683 * NOTE! On some parts notification bits can cause an IPMS
2684 * interrupt instead of a SDBS interrupt.
2686 * NOTE! On some parts (e.g. VBOX, probably intel ICHx),
2687 * SDBS notifies us of the completion of a NCQ command
2688 * and DBS does not.
2690 if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
2691 u_int32_t data;
2693 ahci_pwrite(ap, AHCI_PREG_IS,
2694 AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2695 if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
2696 data = ahci_pread(ap, AHCI_PREG_SNTF);
2697 if (data) {
2698 ahci_pwrite(ap, AHCI_PREG_IS,
2699 AHCI_PREG_IS_SDBS);
2700 kprintf("%s: NOTIFY %08x\n",
2701 PORTNAME(ap), data);
2702 ahci_pwrite(ap, AHCI_PREG_SERR,
2703 AHCI_PREG_SERR_DIAG_N);
2704 ahci_pwrite(ap, AHCI_PREG_SNTF, data);
2705 ahci_cam_changed(ap, NULL, -1);
2708 is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
2712 * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set.
2714 * Spurious IFS errors can occur while we are doing a reset
2715 * sequence through a PM, probably due to an unexpected FIS
2716 * being received during the PM target reset sequence. Chipsets
2717 * are supposed to mask these events but some do not.
2719 * Try to recover from the condition.
2721 if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
2722 u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
2723 if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2724 kprintf("%s: IFS during PM probe (ignored) "
2725 "IS=%b, SERR=%b\n",
2726 PORTNAME(ap),
2727 is, AHCI_PFMT_IS,
2728 serr, AHCI_PFMT_SERR);
2729 ap->ap_flags |= AP_F_IFS_IGNORED;
2733 * Try to clear the error condition. The IFS error killed
2734 * the port so stop it so we can restart it.
2736 ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
2737 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2738 is &= ~AHCI_PREG_IS_IFS;
2739 need = NEED_RESTART;
2740 goto failall;
2744 * Port change (hot-plug) (blockable).
2746 * A PRCS interrupt can occur:
2747 * (1) On hot-unplug / normal-unplug (phy lost)
2748 * (2) Sometimes on hot-plug too.
2750 * A PCS interrupt can occur in a number of situations:
2751 * (1) On hot-plug once communication is established
2752 * (2) On hot-unplug sometimes.
2753 * (3) For chipsets with badly written firmware it can occur
2754 * during INIT/RESET sequences due to the device reset.
2755 * (4) For chipsets with badly written firmware it can occur
2756 * when it thinks an unsolicited COMRESET is received
2757 * during a INIT/RESET sequence, even though we actually
2758 * did request it.
2760 * XXX We can then check the CPS (Cold Presence State) bit, if
2761 * supported, to determine if a device is plugged in or not and do
2762 * the right thing.
2764 * PCS interrupts are cleared by clearing DIAG_X. If this occurs
2765 * command processing is automatically stopped (CR goes inactive)
2766 * and the port must be stopped and restarted.
2768 * WARNING: AMD parts (e.g. 880G chipset, probably others) can
2769 * generate PCS on initialization even when device is
2770 * already connected up. It is unclear why this happens.
2771 * Depending on the state of the device detect this can
2772 * cause us to go into harsh reinit or hot-plug insertion
2773 * mode.
2775 * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET
2776 * continues to flow in from the device), we must clear the
2777 * interrupt in all cases and enforce a delay to prevent
2778 * a livelock and give the port time to settle down.
2779 * Only print something if we aren't in INIT/HARD-RESET.
2781 if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2782 ahci_pwrite(ap, AHCI_PREG_IS,
2783 is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2785 * Try to clear the error. Because of the repetitiveness
2786 * of this interrupt avoid any harsh action if the port is
2787 * already in the init or hard-reset probe state.
2789 ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2790 /* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */
2793 * Ignore PCS/PRCS errors during probes (but still clear the
2794 * interrupt to avoid a livelock). The AMD 880/890/SB850
2795 * chipsets do not mask PCS/PRCS internally during reset
2796 * sequences.
2798 if (ap->ap_flags & AP_F_IN_RESET)
2799 goto skip_pcs;
2801 if (ap->ap_probe == ATA_PROBE_NEED_INIT ||
2802 ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) {
2803 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2804 need = NEED_NOTHING;
2805 ahci_os_sleep(1000);
2806 goto failall;
2808 kprintf("%s: Transient Errors: %b (%d)\n",
2809 PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe);
2810 is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2811 ahci_os_sleep(200);
2814 * Stop the port and figure out what to do next.
2816 ahci_port_stop(ap, 0);
2817 stopped = 1;
2819 switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2820 case AHCI_PREG_SSTS_DET_DEV:
2822 * Device detect
2824 if (ap->ap_probe == ATA_PROBE_FAILED) {
2825 need = NEED_HOTPLUG_INSERT;
2826 goto fatal;
2828 need = NEED_RESTART;
2829 break;
2830 case AHCI_PREG_SSTS_DET_DEV_NE:
2832 * Device not communicating. AMD parts seem to
2833 * like to throw this error on initialization
2834 * for no reason that I can fathom.
2836 kprintf("%s: Device present but not communicating, "
2837 "attempting port restart\n",
2838 PORTNAME(ap));
2839 need = NEED_REINIT;
2840 goto fatal;
2841 default:
2842 if (ap->ap_probe != ATA_PROBE_FAILED) {
2843 need = NEED_HOTPLUG_REMOVE;
2844 goto fatal;
2846 need = NEED_RESTART;
2847 break;
2849 skip_pcs:
2854 * Check for remaining errors - they are fatal. (blockable)
2856 if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2857 AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2858 u_int32_t serr;
2860 ahci_pwrite(ap, AHCI_PREG_IS,
2861 is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2862 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2863 AHCI_PREG_IS_UFS));
2864 serr = ahci_pread(ap, AHCI_PREG_SERR);
2865 kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
2866 "disabling port.\n",
2867 PORTNAME(ap),
2868 is, AHCI_PFMT_IS,
2869 serr, AHCI_PFMT_SERR
2871 is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2872 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2873 AHCI_PREG_IS_UFS);
2876 * Fail all commands but then what? For now try to
2877 * reinitialize the port.
2879 need = NEED_REINIT;
2880 goto fatal;
2884 * Fail all outstanding commands if we know the port won't recover.
2886 * We may have a ccb_at if the failed command is known and was
2887 * being sent to a device over a port multiplier (PM). In this
2888 * case if the port itself has not completely failed we fail just
2889 * the commands related to that target.
2891 * ci_saved contains the mask of active commands as of when the
2892 * error occured, prior to any port stops.
2894 if (ap->ap_state == AP_S_FATAL_ERROR) {
2895 fatal:
2896 ap->ap_state = AP_S_FATAL_ERROR;
2897 failall:
2898 ahci_port_stop(ap, 0);
2899 stopped = 1;
2902 * Error all the active slots not already errored.
2904 ci_masked = ci_saved & *active & ~ap->ap_expired;
2905 if (ci_masked) {
2906 kprintf("%s: Failing all commands: %08x\n",
2907 PORTNAME(ap), ci_masked);
2910 while (ci_masked) {
2911 slot = ffs(ci_masked) - 1;
2912 ccb = &ap->ap_ccbs[slot];
2913 ccb->ccb_xa.state = ATA_S_TIMEOUT;
2914 ap->ap_expired |= 1 << slot;
2915 ci_saved &= ~(1 << slot);
2916 ci_masked &= ~(1 << slot);
2920 * Clear bits in ci_saved (cause completions to be run)
2921 * for all slots which are not active.
2923 ci_saved &= ~*active;
2926 * Don't restart the port if our problems were deemed fatal.
2928 * Also acknowlege all fatal interrupt sources to prevent
2929 * a livelock.
2931 if (ap->ap_state == AP_S_FATAL_ERROR) {
2932 if (need == NEED_RESTART)
2933 need = NEED_NOTHING;
2934 ahci_pwrite(ap, AHCI_PREG_IS,
2935 AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2936 AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2937 AHCI_PREG_IS_UFS);
2942 * If we are stopped the AHCI chipset is supposed to have cleared
2943 * CI and SACT. Did it? If it didn't we try very hard to clear
2944 * the fields otherwise we may end up completing CCBs which are
2945 * actually still active.
2947 * IFS errors on (at least) AMD chipsets create this confusion.
2949 if (stopped) {
2950 u_int32_t mask;
2951 if ((mask = ahci_pactive(ap)) != 0) {
2952 kprintf("%s: chipset failed to clear "
2953 "active cmds %08x\n",
2954 PORTNAME(ap), mask);
2955 ahci_port_start(ap);
2956 ahci_port_stop(ap, 0);
2957 if ((mask = ahci_pactive(ap)) != 0) {
2958 kprintf("%s: unable to prod the chip into "
2959 "clearing active cmds %08x\n",
2960 PORTNAME(ap), mask);
2961 /* what do we do now? */
2967 * CCB completion (non blocking).
2969 * CCB completion is detected by noticing its slot's bit in CI has
2970 * changed to zero some time after we activated it.
2971 * If we are polling, we may only be interested in particular slot(s).
2973 * Any active bits not saved are completed within the restrictions
2974 * imposed by the caller.
2976 ci_masked = ~ci_saved & *active;
2977 while (ci_masked) {
2978 slot = ffs(ci_masked) - 1;
2979 ccb = &ap->ap_ccbs[slot];
2980 ci_masked &= ~(1 << slot);
2982 DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2983 PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2984 " (error)" : "");
2986 bus_dmamap_sync(sc->sc_tag_cmdh,
2987 AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2988 BUS_DMASYNC_POSTWRITE);
2990 bus_dmamap_sync(sc->sc_tag_cmdt,
2991 AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2992 BUS_DMASYNC_POSTWRITE);
2994 bus_dmamap_sync(sc->sc_tag_rfis,
2995 AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2996 BUS_DMASYNC_POSTREAD);
2998 *active &= ~(1 << ccb->ccb_slot);
2999 if (active == &ap->ap_active) {
3000 KKASSERT(ap->ap_active_cnt > 0);
3001 --ap->ap_active_cnt;
3005 * Complete the ccb. If the ccb was marked expired it
3006 * was probably already removed from the command processor,
3007 * so don't take the clear ci_saved bit as meaning the
3008 * command actually succeeded, it didn't.
3010 if (ap->ap_expired & (1 << ccb->ccb_slot)) {
3011 ap->ap_expired &= ~(1 << ccb->ccb_slot);
3012 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3013 ccb->ccb_done(ccb);
3014 ccb->ccb_xa.complete(&ccb->ccb_xa);
3015 } else {
3016 if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
3017 ccb->ccb_xa.state = ATA_S_COMPLETE;
3018 if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
3019 memcpy(&ccb->ccb_xa.rfis,
3020 ap->ap_rfis->rfis,
3021 sizeof(struct ata_fis_d2h));
3022 if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
3023 ccb->ccb_xa.state = ATA_S_ERROR;
3026 ccb->ccb_done(ccb);
3031 * Cleanup. Will not be set if non-blocking.
3033 switch(need) {
3034 case NEED_NOTHING:
3036 * If operating normally and not stopped the interrupt was
3037 * probably just a normal completion and we may be able to
3038 * issue more commands.
3040 if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR)
3041 ahci_issue_pending_commands(ap, NULL);
3042 break;
3043 case NEED_RESTART:
3045 * A recoverable error occured and we can restart outstanding
3046 * commands on the port.
3048 ci_saved &= ~ap->ap_expired;
3049 if (ci_saved) {
3050 kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
3051 ahci_issue_saved_commands(ap, ci_saved);
3055 * Potentially issue new commands if not in a failed
3056 * state.
3058 if (ap->ap_state != AP_S_FATAL_ERROR) {
3059 ahci_port_start(ap);
3060 ahci_issue_pending_commands(ap, NULL);
3062 break;
3063 case NEED_REINIT:
3065 * Something horrible happened to the port and we
3066 * need to reinitialize it.
3068 kprintf("%s: REINIT - Attempting to reinitialize the port "
3069 "after it had a horrible accident\n",
3070 PORTNAME(ap));
3071 ap->ap_flags |= AP_F_IN_RESET;
3072 ap->ap_flags |= AP_F_HARSH_REINIT;
3073 ap->ap_probe = ATA_PROBE_NEED_INIT;
3074 ahci_cam_changed(ap, NULL, -1);
3075 break;
3076 case NEED_HOTPLUG_INSERT:
3078 * A hot-plug insertion event has occured and all
3079 * outstanding commands have already been revoked.
3081 * Don't recurse if this occurs while we are
3082 * resetting the port.
3084 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3085 kprintf("%s: HOTPLUG - Device inserted\n",
3086 PORTNAME(ap));
3087 ap->ap_probe = ATA_PROBE_NEED_INIT;
3088 ahci_cam_changed(ap, NULL, -1);
3090 break;
3091 case NEED_HOTPLUG_REMOVE:
3093 * A hot-plug removal event has occured and all
3094 * outstanding commands have already been revoked.
3096 * Don't recurse if this occurs while we are
3097 * resetting the port.
3099 if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
3100 kprintf("%s: HOTPLUG - Device removed\n",
3101 PORTNAME(ap));
3102 ahci_port_hardstop(ap);
3103 /* ap_probe set to failed */
3104 ahci_cam_changed(ap, NULL, -1);
3106 break;
3107 default:
3108 break;
3112 struct ahci_ccb *
3113 ahci_get_ccb(struct ahci_port *ap)
3115 struct ahci_ccb *ccb;
3117 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3118 ccb = TAILQ_FIRST(&ap->ap_ccb_free);
3119 if (ccb != NULL) {
3120 KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3121 KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
3122 TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
3123 ccb->ccb_xa.state = ATA_S_SETUP;
3124 ccb->ccb_xa.flags = 0;
3125 ccb->ccb_xa.at = NULL;
3127 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3129 return (ccb);
3132 void
3133 ahci_put_ccb(struct ahci_ccb *ccb)
3135 struct ahci_port *ap = ccb->ccb_port;
3137 KKASSERT(ccb->ccb_xa.state != ATA_S_PUT);
3138 KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3139 lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3140 ccb->ccb_xa.state = ATA_S_PUT;
3141 ++ccb->ccb_xa.serial;
3142 TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
3143 lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3146 struct ahci_ccb *
3147 ahci_get_err_ccb(struct ahci_port *ap)
3149 struct ahci_ccb *err_ccb;
3150 u_int32_t sact;
3151 u_int32_t ci;
3153 /* No commands may be active on the chip. */
3155 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3156 sact = ahci_pread(ap, AHCI_PREG_SACT);
3157 if (sact != 0) {
3158 kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
3159 PORTNAME(ap), sact);
3162 ci = ahci_pread(ap, AHCI_PREG_CI);
3163 if (ci) {
3164 kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
3165 ap->ap_name, ci);
3167 KKASSERT(ci == 0);
3168 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
3169 ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
3171 /* Save outstanding command state. */
3172 ap->ap_err_saved_active = ap->ap_active;
3173 ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
3174 ap->ap_err_saved_sactive = ap->ap_sactive;
3177 * Pretend we have no commands outstanding, so that completions won't
3178 * run prematurely.
3180 ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
3183 * Grab a CCB to use for error recovery. This should never fail, as
3184 * we ask atascsi to reserve one for us at init time.
3186 err_ccb = ap->ap_err_ccb;
3187 KKASSERT(err_ccb != NULL);
3188 err_ccb->ccb_xa.flags = 0;
3189 err_ccb->ccb_done = ahci_empty_done;
3191 return err_ccb;
3194 void
3195 ahci_put_err_ccb(struct ahci_ccb *ccb)
3197 struct ahci_port *ap = ccb->ccb_port;
3198 u_int32_t sact;
3199 u_int32_t ci;
3201 KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
3204 * No commands may be active on the chip
3206 if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3207 sact = ahci_pread(ap, AHCI_PREG_SACT);
3208 if (sact) {
3209 panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
3210 ccb->ccb_slot, sact);
3213 ci = ahci_pread(ap, AHCI_PREG_CI);
3214 if (ci) {
3215 panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
3216 "(act=%08x sact=%08x)\n",
3217 ccb->ccb_slot, ci,
3218 ap->ap_active, ap->ap_sactive);
3221 KKASSERT(ccb == ap->ap_err_ccb);
3223 /* Restore outstanding command state */
3224 ap->ap_sactive = ap->ap_err_saved_sactive;
3225 ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
3226 ap->ap_active = ap->ap_err_saved_active;
3228 ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
3232 * Read log page to get NCQ error.
3234 * NOTE: NCQ not currently supported on port multipliers. XXX
3237 ahci_port_read_ncq_error(struct ahci_port *ap, int target)
3239 struct ata_log_page_10h *log;
3240 struct ahci_ccb *ccb;
3241 struct ahci_ccb *ccb2;
3242 struct ahci_cmd_hdr *cmd_slot;
3243 struct ata_fis_h2d *fis;
3244 int err_slot;
3246 if (bootverbose) {
3247 kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
3248 target);
3252 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
3254 * Getting err_ccb clears active/sactive/active_cnt, putting
3255 * it back restores the fields.
3257 ccb = ahci_get_err_ccb(ap);
3258 ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
3259 ccb->ccb_xa.data = ap->ap_err_scratch;
3260 ccb->ccb_xa.datalen = 512;
3261 ccb->ccb_xa.complete = ahci_dummy_done;
3262 ccb->ccb_xa.at = ap->ap_ata[target];
3264 fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
3265 memset(fis, 0, sizeof(*fis));
3266 fis->type = ATA_FIS_TYPE_H2D;
3267 fis->flags = ATA_H2D_FLAGS_CMD | target;
3268 fis->command = ATA_C_READ_LOG_EXT;
3269 fis->lba_low = 0x10; /* queued error log page (10h) */
3270 fis->sector_count = 1; /* number of sectors (1) */
3271 fis->sector_count_exp = 0;
3272 fis->lba_mid = 0; /* starting offset */
3273 fis->lba_mid_exp = 0;
3274 fis->device = 0;
3276 cmd_slot = ccb->ccb_cmd_hdr;
3277 cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */
3279 if (ahci_load_prdt(ccb) != 0) {
3280 err_slot = -1;
3281 goto err;
3284 ccb->ccb_xa.state = ATA_S_PENDING;
3285 if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
3286 err_slot = -1;
3287 ahci_unload_prdt(ccb);
3288 goto err;
3290 ahci_unload_prdt(ccb);
3293 * Success, extract failed register set and tags from the scratch
3294 * space.
3296 log = (struct ata_log_page_10h *)ap->ap_err_scratch;
3297 if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
3298 /* Not queued bit was set - wasn't an NCQ error? */
3299 kprintf("%s: read NCQ error page, but not an NCQ error?\n",
3300 PORTNAME(ap));
3301 err_slot = -1;
3302 } else {
3303 /* Copy back the log record as a D2H register FIS. */
3304 err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
3306 ccb2 = &ap->ap_ccbs[err_slot];
3307 if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
3308 kprintf("%s: read NCQ error page slot=%d\n",
3309 ATANAME(ap, ccb2->ccb_xa.at),
3310 err_slot);
3311 memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
3312 sizeof(struct ata_fis_d2h));
3313 ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3314 ccb2->ccb_xa.rfis.flags = 0;
3315 } else {
3316 kprintf("%s: read NCQ error page slot=%d, "
3317 "slot does not match any cmds\n",
3318 ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
3319 err_slot);
3320 err_slot = -1;
3323 err:
3324 ahci_put_err_ccb(ccb);
3325 kprintf("%s: DONE log page target %d err_slot=%d\n",
3326 PORTNAME(ap), target, err_slot);
3327 return (err_slot);
3331 * Allocate memory for various structures DMAd by hardware. The maximum
3332 * number of segments for these tags is 1 so the DMA memory will have a
3333 * single physical base address.
3335 struct ahci_dmamem *
3336 ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3338 struct ahci_dmamem *adm;
3339 int error;
3341 adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3343 error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3344 BUS_DMA_ZERO, &adm->adm_map);
3345 if (error == 0) {
3346 adm->adm_tag = tag;
3347 error = bus_dmamap_load(tag, adm->adm_map,
3348 adm->adm_kva,
3349 bus_dma_tag_getmaxsize(tag),
3350 ahci_dmamem_saveseg, &adm->adm_busaddr,
3353 if (error) {
3354 if (adm->adm_map) {
3355 bus_dmamap_destroy(tag, adm->adm_map);
3356 adm->adm_map = NULL;
3357 adm->adm_tag = NULL;
3358 adm->adm_kva = NULL;
3360 kfree(adm, M_DEVBUF);
3361 adm = NULL;
3363 return (adm);
3366 static
3367 void
3368 ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3370 KKASSERT(error == 0);
3371 KKASSERT(nsegs == 1);
3372 *(bus_addr_t *)info = segs->ds_addr;
3376 void
3377 ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3379 if (adm->adm_map) {
3380 bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3381 bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3382 adm->adm_map = NULL;
3383 adm->adm_tag = NULL;
3384 adm->adm_kva = NULL;
3386 kfree(adm, M_DEVBUF);
3389 u_int32_t
3390 ahci_read(struct ahci_softc *sc, bus_size_t r)
3392 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3393 BUS_SPACE_BARRIER_READ);
3394 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3397 void
3398 ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3400 bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3401 bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3402 BUS_SPACE_BARRIER_WRITE);
3405 u_int32_t
3406 ahci_pread(struct ahci_port *ap, bus_size_t r)
3408 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3409 BUS_SPACE_BARRIER_READ);
3410 return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3413 void
3414 ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3416 bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3417 bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3418 BUS_SPACE_BARRIER_WRITE);
3422 * Wait up to (timeout) milliseconds for the masked port register to
3423 * match the target.
3425 * Timeout is in milliseconds.
3428 ahci_pwait_eq(struct ahci_port *ap, int timeout,
3429 bus_size_t r, u_int32_t mask, u_int32_t target)
3431 int t;
3434 * Loop hard up to 100uS
3436 for (t = 0; t < 100; ++t) {
3437 if ((ahci_pread(ap, r) & mask) == target)
3438 return (0);
3439 ahci_os_hardsleep(1); /* us */
3442 do {
3443 timeout -= ahci_os_softsleep();
3444 if ((ahci_pread(ap, r) & mask) == target)
3445 return (0);
3446 } while (timeout > 0);
3447 return (1);
3451 ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3452 u_int32_t target)
3454 int t;
3457 * Loop hard up to 100uS
3459 for (t = 0; t < 100; ++t) {
3460 if ((ahci_read(sc, r) & mask) != target)
3461 return (0);
3462 ahci_os_hardsleep(1); /* us */
3466 * And one millisecond the slow way
3468 t = 1000;
3469 do {
3470 t -= ahci_os_softsleep();
3471 if ((ahci_read(sc, r) & mask) != target)
3472 return (0);
3473 } while (t > 0);
3475 return (1);
3480 * Acquire an ata transfer.
3482 * Pass a NULL at for direct-attached transfers, and a non-NULL at for
3483 * targets that go through the port multiplier.
3485 struct ata_xfer *
3486 ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3488 struct ahci_ccb *ccb;
3490 ccb = ahci_get_ccb(ap);
3491 if (ccb == NULL) {
3492 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3493 PORTNAME(ap));
3494 return (NULL);
3497 DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3498 PORTNAME(ap), ccb->ccb_slot);
3500 memset(ccb->ccb_xa.fis, 0, sizeof(*ccb->ccb_xa.fis));
3501 ccb->ccb_xa.at = at;
3502 ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3504 return (&ccb->ccb_xa);
3507 void
3508 ahci_ata_put_xfer(struct ata_xfer *xa)
3510 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3512 DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3514 ahci_put_ccb(ccb);
3518 ahci_ata_cmd(struct ata_xfer *xa)
3520 struct ahci_ccb *ccb = (struct ahci_ccb *)xa;
3521 struct ahci_cmd_hdr *cmd_slot;
3523 KKASSERT(xa->state == ATA_S_SETUP);
3525 if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3526 goto failcmd;
3527 ccb->ccb_done = ahci_ata_cmd_done;
3529 cmd_slot = ccb->ccb_cmd_hdr;
3530 cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
3531 if (ccb->ccb_xa.at) {
3532 cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
3533 AHCI_CMD_LIST_FLAG_PMP_SHIFT);
3536 if (xa->flags & ATA_F_WRITE)
3537 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3539 if (xa->flags & ATA_F_PACKET)
3540 cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3542 if (ahci_load_prdt(ccb) != 0)
3543 goto failcmd;
3545 xa->state = ATA_S_PENDING;
3547 if (xa->flags & ATA_F_POLL)
3548 return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3550 crit_enter();
3551 KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
3552 xa->flags |= ATA_F_TIMEOUT_DESIRED;
3553 ahci_start(ccb);
3554 crit_exit();
3555 return (xa->state);
3557 failcmd:
3558 crit_enter();
3559 xa->state = ATA_S_ERROR;
3560 xa->complete(xa);
3561 crit_exit();
3562 return (ATA_S_ERROR);
3565 void
3566 ahci_ata_cmd_done(struct ahci_ccb *ccb)
3568 struct ata_xfer *xa = &ccb->ccb_xa;
3569 int serial;
3572 * NOTE: Callout does not lock port and may race us modifying
3573 * the flags, so make sure its stopped.
3575 * A callout race can clean up the ccb. A change in the
3576 * serial number should catch this condition.
3578 if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3579 serial = ccb->ccb_xa.serial;
3580 callout_stop_sync(&ccb->ccb_timeout);
3581 if (serial != ccb->ccb_xa.serial) {
3582 kprintf("%s: Warning: timeout race ccb %p\n",
3583 PORTNAME(ccb->ccb_port), ccb);
3584 return;
3586 xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3588 xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3589 ccb->ccb_port->ap_expired &= ~(1 << ccb->ccb_slot);
3591 KKASSERT(xa->state != ATA_S_ONCHIP && xa->state != ATA_S_PUT);
3592 ahci_unload_prdt(ccb);
3594 if (xa->state != ATA_S_TIMEOUT)
3595 xa->complete(xa);
3599 * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3600 * while the callout is runing.
3602 * We can't safely get the port lock here or delay, we could block
3603 * the callout thread.
3605 static void
3606 ahci_ata_cmd_timeout_unserialized(void *arg)
3608 struct ahci_ccb *ccb = arg;
3609 struct ahci_port *ap = ccb->ccb_port;
3611 KKASSERT(ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING);
3612 ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3613 ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3614 ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3618 * Timeout code, typically called when the port command processor is running.
3620 * We have to be very very careful here. We cannot stop the port unless
3621 * CR is already clear or the only active commands remaining are timed-out
3622 * ones. Otherwise stopping the port will race the command processor and
3623 * we can lose events. While we can theoretically just restart everything
3624 * that could result in a double-issue which will not work for ATAPI commands.
3626 void
3627 ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3629 struct ata_xfer *xa = &ccb->ccb_xa;
3630 struct ahci_port *ap = ccb->ccb_port;
3631 struct ata_port *at;
3632 u_int32_t ci_saved;
3633 u_int32_t mask;
3634 int slot;
3636 at = ccb->ccb_xa.at;
3638 kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
3639 "\tglb-status 0x%08x\n"
3640 "\tcmd-reg 0x%b\n"
3641 "\tport_status 0x%b\n"
3642 "\tsactive=%08x active=%08x expired=%08x\n"
3643 "\t sact=%08x ci=%08x\n"
3644 "\t STS=%b\n",
3645 ATANAME(ap, at),
3646 ccb->ccb_xa.state, ccb->ccb_slot,
3647 ahci_read(ap->ap_sc, AHCI_REG_IS),
3648 ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3649 ahci_pread(ap, AHCI_PREG_IS), AHCI_PFMT_IS,
3650 ap->ap_sactive, ap->ap_active, ap->ap_expired,
3651 ahci_pread(ap, AHCI_PREG_SACT),
3652 ahci_pread(ap, AHCI_PREG_CI),
3653 ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
3658 * NOTE: Timeout will not be running if the command was polled.
3659 * If we got here at least one of these flags should be set.
3661 KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
3662 ATA_F_TIMEOUT_RUNNING));
3663 xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3665 if (ccb->ccb_xa.state == ATA_S_PENDING) {
3666 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3667 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3668 ccb->ccb_done(ccb);
3669 xa->complete(xa);
3670 ahci_issue_pending_commands(ap, NULL);
3671 return;
3673 if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3674 kprintf("%s: Unexpected state during timeout: %d\n",
3675 ATANAME(ap, at), ccb->ccb_xa.state);
3676 return;
3680 * Ok, we can only get this command off the chip if CR is inactive
3681 * or if the only commands running on the chip are all expired.
3682 * Otherwise we have to wait until the port is in a safe state.
3684 * Do not set state here, it will cause polls to return when the
3685 * ccb is not yet off the chip.
3687 ap->ap_expired |= 1 << ccb->ccb_slot;
3689 if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
3690 (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
3692 * If using FBSS or NCQ we can't safely stop the port
3693 * right now.
3695 kprintf("%s: Deferred timeout until its safe, slot %d\n",
3696 ATANAME(ap, at), ccb->ccb_slot);
3697 return;
3701 * We can safely stop the port and process all expired ccb's,
3702 * which will include our current ccb.
3704 ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
3705 ahci_pread(ap, AHCI_PREG_CI);
3706 ahci_port_stop(ap, 0);
3708 while (ap->ap_expired) {
3709 slot = ffs(ap->ap_expired) - 1;
3710 ap->ap_expired &= ~(1 << slot);
3711 ci_saved &= ~(1 << slot);
3712 ccb = &ap->ap_ccbs[slot];
3713 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3714 if (ccb->ccb_xa.flags & ATA_F_NCQ) {
3715 KKASSERT(ap->ap_sactive & (1 << slot));
3716 ap->ap_sactive &= ~(1 << slot);
3717 } else {
3718 KKASSERT(ap->ap_active & (1 << slot));
3719 ap->ap_active &= ~(1 << slot);
3720 --ap->ap_active_cnt;
3722 ccb->ccb_done(ccb);
3723 ccb->ccb_xa.complete(&ccb->ccb_xa);
3725 /* ccb invalid now */
3728 * We can safely CLO the port to clear any BSY/DRQ, a case which
3729 * can occur with port multipliers. This will unbrick the port
3730 * and allow commands to other targets behind the PM continue.
3731 * (FBSS).
3733 * Finally, once the port has been restarted we can issue any
3734 * previously saved pending commands, and run the port interrupt
3735 * code to handle any completions which may have occured when
3736 * we saved CI.
3738 if (ahci_pread(ap, AHCI_PREG_TFD) &
3739 (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
3740 kprintf("%s: Warning, issuing CLO after timeout\n",
3741 ATANAME(ap, at));
3742 ahci_port_clo(ap);
3744 ahci_port_start(ap);
3747 * We absolutely must make sure the chipset cleared activity on
3748 * all slots. This sometimes might not happen due to races with
3749 * a chipset interrupt which stops the port before we can manage
3750 * to. For some reason some chipsets don't clear the active
3751 * commands when we turn off CMD_ST after the chip has stopped
3752 * operations itself.
3754 if (ahci_pactive(ap) != 0) {
3755 ahci_port_stop(ap, 0);
3756 ahci_port_start(ap);
3757 if ((mask = ahci_pactive(ap)) != 0) {
3758 kprintf("%s: quick-timeout: chipset failed "
3759 "to clear active cmds %08x\n",
3760 PORTNAME(ap), mask);
3763 ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
3764 ahci_issue_pending_commands(ap, NULL);
3765 ahci_port_intr(ap, 0);
3769 * Issue a previously saved set of commands
3771 void
3772 ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
3774 if (ci_saved) {
3775 KKASSERT(!((ap->ap_active & ci_saved) &&
3776 (ap->ap_sactive & ci_saved)));
3777 KKASSERT((ci_saved & ap->ap_expired) == 0);
3778 if (ap->ap_sactive & ci_saved)
3779 ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
3780 ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3785 * Used by the softreset, pmprobe, and read_ncq_error only, in very
3786 * specialized, controlled circumstances.
3788 * Only one command may be pending.
3790 void
3791 ahci_quick_timeout(struct ahci_ccb *ccb)
3793 struct ahci_port *ap = ccb->ccb_port;
3794 u_int32_t mask;
3796 switch (ccb->ccb_xa.state) {
3797 case ATA_S_PENDING:
3798 TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3799 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3800 break;
3801 case ATA_S_ONCHIP:
3803 * We have to clear the command on-chip.
3805 KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3806 ap->ap_sactive == 0);
3807 ahci_port_stop(ap, 0);
3808 ahci_port_start(ap);
3809 if (ahci_pactive(ap) != 0) {
3810 ahci_port_stop(ap, 0);
3811 ahci_port_start(ap);
3812 if ((mask = ahci_pactive(ap)) != 0) {
3813 kprintf("%s: quick-timeout: chipset failed "
3814 "to clear active cmds %08x\n",
3815 PORTNAME(ap), mask);
3819 ccb->ccb_xa.state = ATA_S_TIMEOUT;
3820 ap->ap_active &= ~(1 << ccb->ccb_slot);
3821 KKASSERT(ap->ap_active_cnt > 0);
3822 --ap->ap_active_cnt;
3823 break;
3824 default:
3825 panic("%s: ahci_quick_timeout: ccb in bad state %d",
3826 ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3830 static void
3831 ahci_dummy_done(struct ata_xfer *xa)
3835 static void
3836 ahci_empty_done(struct ahci_ccb *ccb)
3841 ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
3842 int feature, int enable)
3844 struct ata_port *at;
3845 struct ata_xfer *xa;
3846 int error;
3848 at = atx ? atx : ap->ap_ata[0];
3850 xa = ahci_ata_get_xfer(ap, atx);
3852 xa->fis->type = ATA_FIS_TYPE_H2D;
3853 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
3854 xa->fis->command = ATA_C_SET_FEATURES;
3855 xa->fis->features = enable ? ATA_C_SATA_FEATURE_ENA :
3856 ATA_C_SATA_FEATURE_DIS;
3857 xa->fis->sector_count = feature;
3858 xa->fis->control = ATA_FIS_CONTROL_4BIT;
3860 xa->complete = ahci_dummy_done;
3861 xa->datalen = 0;
3862 xa->flags = ATA_F_POLL;
3863 xa->timeout = 1000;
3865 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
3866 error = 0;
3867 else
3868 error = EIO;
3869 ahci_ata_put_xfer(xa);
3870 return(error);