152c9ce25SScott Long /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4d19f06b3SAlexander Motin * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
552c9ce25SScott Long * All rights reserved.
652c9ce25SScott Long *
752c9ce25SScott Long * Redistribution and use in source and binary forms, with or without
852c9ce25SScott Long * modification, are permitted provided that the following conditions
952c9ce25SScott Long * are met:
1052c9ce25SScott Long * 1. Redistributions of source code must retain the above copyright
1152c9ce25SScott Long * notice, this list of conditions and the following disclaimer,
1252c9ce25SScott Long * without modification, immediately at the beginning of the file.
1352c9ce25SScott Long * 2. Redistributions in binary form must reproduce the above copyright
1452c9ce25SScott Long * notice, this list of conditions and the following disclaimer in the
1552c9ce25SScott Long * documentation and/or other materials provided with the distribution.
1652c9ce25SScott Long *
1752c9ce25SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1852c9ce25SScott Long * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1952c9ce25SScott Long * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2052c9ce25SScott Long * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2152c9ce25SScott Long * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2252c9ce25SScott Long * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2352c9ce25SScott Long * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2452c9ce25SScott Long * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2552c9ce25SScott Long * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2652c9ce25SScott Long * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2752c9ce25SScott Long */
2852c9ce25SScott Long
2952c9ce25SScott Long #include <sys/param.h>
3052c9ce25SScott Long #include <sys/module.h>
3152c9ce25SScott Long #include <sys/systm.h>
3252c9ce25SScott Long #include <sys/kernel.h>
3352c9ce25SScott Long #include <sys/bus.h>
34df1439e3SAlexander Motin #include <sys/conf.h>
3552c9ce25SScott Long #include <sys/endian.h>
3652c9ce25SScott Long #include <sys/malloc.h>
3752c9ce25SScott Long #include <sys/lock.h>
3852c9ce25SScott Long #include <sys/mutex.h>
39ddfc9c4cSWarner Losh #include <sys/sbuf.h>
404668666fSWarner Losh #include <sys/sysctl.h>
4152c9ce25SScott Long #include <machine/stdarg.h>
4252c9ce25SScott Long #include <machine/resource.h>
4352c9ce25SScott Long #include <machine/bus.h>
4452c9ce25SScott Long #include <sys/rman.h>
4552c9ce25SScott Long #include "ahci.h"
4652c9ce25SScott Long
4752c9ce25SScott Long #include <cam/cam.h>
4852c9ce25SScott Long #include <cam/cam_ccb.h>
4952c9ce25SScott Long #include <cam/cam_sim.h>
5052c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5152c9ce25SScott Long #include <cam/cam_debug.h>
5252c9ce25SScott Long
5352c9ce25SScott Long /* local prototypes */
5452c9ce25SScott Long static void ahci_intr(void *data);
5552c9ce25SScott Long static void ahci_intr_one(void *data);
56227d67aaSAlexander Motin static void ahci_intr_one_edge(void *data);
576533cd19SAlexander Motin static int ahci_ch_init(device_t dev);
586533cd19SAlexander Motin static int ahci_ch_deinit(device_t dev);
5952c9ce25SScott Long static int ahci_ch_suspend(device_t dev);
6052c9ce25SScott Long static int ahci_ch_resume(device_t dev);
614dbabf10SAlexander Motin static void ahci_ch_pm(void *arg);
62227d67aaSAlexander Motin static void ahci_ch_intr(void *arg);
63227d67aaSAlexander Motin static void ahci_ch_intr_direct(void *arg);
64227d67aaSAlexander Motin static void ahci_ch_intr_main(struct ahci_channel *ch, uint32_t istatus);
65fcd7f38fSAlexander Motin static void ahci_begin_transaction(struct ahci_channel *ch, union ccb *ccb);
6652c9ce25SScott Long static void ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
6752c9ce25SScott Long static void ahci_execute_transaction(struct ahci_slot *slot);
6865d2f9c1SJohn Baldwin static void ahci_timeout(void *arg);
6952c9ce25SScott Long static void ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et);
70fcd7f38fSAlexander Motin static int ahci_setup_fis(struct ahci_channel *ch, struct ahci_cmd_tab *ctp, union ccb *ccb, int tag);
7152c9ce25SScott Long static void ahci_dmainit(device_t dev);
7252c9ce25SScott Long static void ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
7352c9ce25SScott Long static void ahci_dmafini(device_t dev);
7452c9ce25SScott Long static void ahci_slotsalloc(device_t dev);
7552c9ce25SScott Long static void ahci_slotsfree(device_t dev);
76fcd7f38fSAlexander Motin static void ahci_reset(struct ahci_channel *ch);
77fcd7f38fSAlexander Motin static void ahci_start(struct ahci_channel *ch, int fbs);
78fcd7f38fSAlexander Motin static void ahci_stop(struct ahci_channel *ch);
79fcd7f38fSAlexander Motin static void ahci_clo(struct ahci_channel *ch);
80fcd7f38fSAlexander Motin static void ahci_start_fr(struct ahci_channel *ch);
81fcd7f38fSAlexander Motin static void ahci_stop_fr(struct ahci_channel *ch);
824668666fSWarner Losh static int ahci_phy_check_events(struct ahci_channel *ch, u_int32_t serr);
834668666fSWarner Losh static uint32_t ahci_ch_detval(struct ahci_channel *ch, uint32_t val);
8452c9ce25SScott Long
8552c9ce25SScott Long static int ahci_sata_connect(struct ahci_channel *ch);
86fcd7f38fSAlexander Motin static int ahci_sata_phy_reset(struct ahci_channel *ch);
87fcd7f38fSAlexander Motin static int ahci_wait_ready(struct ahci_channel *ch, int t, int t0);
8852c9ce25SScott Long
89fcd7f38fSAlexander Motin static void ahci_issue_recovery(struct ahci_channel *ch);
90fcd7f38fSAlexander Motin static void ahci_process_read_log(struct ahci_channel *ch, union ccb *ccb);
91fcd7f38fSAlexander Motin static void ahci_process_request_sense(struct ahci_channel *ch, union ccb *ccb);
9252c9ce25SScott Long
9352c9ce25SScott Long static void ahciaction(struct cam_sim *sim, union ccb *ccb);
9452c9ce25SScott Long static void ahcipoll(struct cam_sim *sim);
9552c9ce25SScott Long
96d745c852SEd Schouten static MALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driver data buffers");
9752c9ce25SScott Long
986bbd332eSAlexander Motin #define recovery_type spriv_field0
996bbd332eSAlexander Motin #define RECOVERY_NONE 0
1006bbd332eSAlexander Motin #define RECOVERY_READ_LOG 1
1016bbd332eSAlexander Motin #define RECOVERY_REQUEST_SENSE 2
1026bbd332eSAlexander Motin #define recovery_slot spriv_field1
1036bbd332eSAlexander Motin
1044668666fSWarner Losh static uint32_t
ahci_ch_detval(struct ahci_channel * ch,uint32_t val)1054668666fSWarner Losh ahci_ch_detval(struct ahci_channel *ch, uint32_t val)
1064668666fSWarner Losh {
1074668666fSWarner Losh
1084668666fSWarner Losh return ch->disablephy ? ATA_SC_DET_DISABLE : val;
1094668666fSWarner Losh }
1104668666fSWarner Losh
111802df3acSWarner Losh int
ahci_ctlr_setup(device_t dev)112802df3acSWarner Losh ahci_ctlr_setup(device_t dev)
11352c9ce25SScott Long {
114802df3acSWarner Losh struct ahci_controller *ctlr = device_get_softc(dev);
115802df3acSWarner Losh /* Clear interrupts */
116802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS));
117802df3acSWarner Losh /* Configure CCC */
118802df3acSWarner Losh if (ctlr->ccc) {
119802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI));
120802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_CCCC,
121802df3acSWarner Losh (ctlr->ccc << AHCI_CCCC_TV_SHIFT) |
122802df3acSWarner Losh (4 << AHCI_CCCC_CC_SHIFT) |
123802df3acSWarner Losh AHCI_CCCC_EN);
124802df3acSWarner Losh ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) &
125802df3acSWarner Losh AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT;
126802df3acSWarner Losh if (bootverbose) {
127802df3acSWarner Losh device_printf(dev,
128802df3acSWarner Losh "CCC with %dms/4cmd enabled on vector %d\n",
129802df3acSWarner Losh ctlr->ccc, ctlr->cccv);
1306bd8779bSAlexander Motin }
1316bd8779bSAlexander Motin }
132802df3acSWarner Losh /* Enable AHCI interrupts */
133802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_GHC,
134802df3acSWarner Losh ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE);
135802df3acSWarner Losh return (0);
1366bd8779bSAlexander Motin }
1376bd8779bSAlexander Motin
138802df3acSWarner Losh int
ahci_ctlr_reset(device_t dev)139802df3acSWarner Losh ahci_ctlr_reset(device_t dev)
1406bd8779bSAlexander Motin {
141802df3acSWarner Losh struct ahci_controller *ctlr = device_get_softc(dev);
142274328a4SAlexander Motin uint32_t v;
143802df3acSWarner Losh int timeout;
14452c9ce25SScott Long
145274328a4SAlexander Motin /* BIOS/OS Handoff */
146274328a4SAlexander Motin if ((ATA_INL(ctlr->r_mem, AHCI_VS) >= 0x00010200) &&
147274328a4SAlexander Motin (ATA_INL(ctlr->r_mem, AHCI_CAP2) & AHCI_CAP2_BOH) &&
148274328a4SAlexander Motin ((v = ATA_INL(ctlr->r_mem, AHCI_BOHC)) & AHCI_BOHC_OOS) == 0) {
149274328a4SAlexander Motin /* Request OS ownership. */
150274328a4SAlexander Motin ATA_OUTL(ctlr->r_mem, AHCI_BOHC, v | AHCI_BOHC_OOS);
151274328a4SAlexander Motin
152274328a4SAlexander Motin /* Wait up to 2s for BIOS ownership release. */
153274328a4SAlexander Motin for (timeout = 0; timeout < 80; timeout++) {
154274328a4SAlexander Motin DELAY(25000);
155274328a4SAlexander Motin v = ATA_INL(ctlr->r_mem, AHCI_BOHC);
156274328a4SAlexander Motin if ((v & AHCI_BOHC_BOS) == 0)
157274328a4SAlexander Motin break;
158274328a4SAlexander Motin if ((v & AHCI_BOHC_BB) == 0)
159274328a4SAlexander Motin break;
160274328a4SAlexander Motin }
161274328a4SAlexander Motin }
162274328a4SAlexander Motin
163802df3acSWarner Losh /* Enable AHCI mode */
164802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
165802df3acSWarner Losh /* Reset AHCI controller */
166802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR);
167802df3acSWarner Losh for (timeout = 1000; timeout > 0; timeout--) {
168802df3acSWarner Losh DELAY(1000);
169802df3acSWarner Losh if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0)
170802df3acSWarner Losh break;
1718e57bf83SAlexander Motin }
172802df3acSWarner Losh if (timeout == 0) {
173802df3acSWarner Losh device_printf(dev, "AHCI controller reset failure\n");
174e4a8f2e2SSteven Hartland return (ENXIO);
1758e57bf83SAlexander Motin }
176802df3acSWarner Losh /* Reenable AHCI mode */
177802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
178731a8268SZbigniew Bodek
179731a8268SZbigniew Bodek if (ctlr->quirks & AHCI_Q_RESTORE_CAP) {
180731a8268SZbigniew Bodek /*
181731a8268SZbigniew Bodek * Restore capability field.
182731a8268SZbigniew Bodek * This is write to a read-only register to restore its state.
183731a8268SZbigniew Bodek * On fully standard-compliant hardware this is not needed and
184731a8268SZbigniew Bodek * this operation shall not take place. See ahci_pci.c for
185731a8268SZbigniew Bodek * platforms using this quirk.
186731a8268SZbigniew Bodek */
187731a8268SZbigniew Bodek ATA_OUTL(ctlr->r_mem, AHCI_CAP, ctlr->caps);
188731a8268SZbigniew Bodek }
189731a8268SZbigniew Bodek
190802df3acSWarner Losh return (0);
19152c9ce25SScott Long }
19252c9ce25SScott Long
193802df3acSWarner Losh int
ahci_attach(device_t dev)19452c9ce25SScott Long ahci_attach(device_t dev)
19552c9ce25SScott Long {
19652c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
197feae8518SAlexander Motin int error, i, speed, unit;
198feae8518SAlexander Motin uint32_t u, version;
199802df3acSWarner Losh device_t child;
20052c9ce25SScott Long
20152c9ce25SScott Long ctlr->dev = dev;
2026e88c2c5SAlexander Motin ctlr->ccc = 0;
2034dbabf10SAlexander Motin resource_int_value(device_get_name(dev),
2044dbabf10SAlexander Motin device_get_unit(dev), "ccc", &ctlr->ccc);
20553f5ac13SAlexander Motin mtx_init(&ctlr->ch_mtx, "AHCI channels lock", NULL, MTX_DEF);
206802df3acSWarner Losh
20752c9ce25SScott Long /* Setup our own memory management for channels. */
208cc6b610bSAlexander Motin ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
209cc6b610bSAlexander Motin ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
21052c9ce25SScott Long ctlr->sc_iomem.rm_type = RMAN_ARRAY;
21152c9ce25SScott Long ctlr->sc_iomem.rm_descr = "I/O memory addresses";
21252c9ce25SScott Long if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
2130af6011aSZbigniew Bodek ahci_free_mem(dev);
21452c9ce25SScott Long return (error);
21552c9ce25SScott Long }
21652c9ce25SScott Long if ((error = rman_manage_region(&ctlr->sc_iomem,
21752c9ce25SScott Long rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
2180af6011aSZbigniew Bodek ahci_free_mem(dev);
21952c9ce25SScott Long rman_fini(&ctlr->sc_iomem);
22052c9ce25SScott Long return (error);
22152c9ce25SScott Long }
2226bd8779bSAlexander Motin /* Get the HW capabilities */
2236bd8779bSAlexander Motin version = ATA_INL(ctlr->r_mem, AHCI_VS);
2246bd8779bSAlexander Motin ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP);
225189d85ccSAlexander Motin if (version >= 0x00010200)
2266bd8779bSAlexander Motin ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2);
22783c5d981SAlexander Motin if (ctlr->caps & AHCI_CAP_EMS)
22883c5d981SAlexander Motin ctlr->capsem = ATA_INL(ctlr->r_mem, AHCI_EM_CTL);
229731a8268SZbigniew Bodek
230731a8268SZbigniew Bodek if (ctlr->quirks & AHCI_Q_FORCE_PI) {
231731a8268SZbigniew Bodek /*
232731a8268SZbigniew Bodek * Enable ports.
233731a8268SZbigniew Bodek * The spec says that BIOS sets up bits corresponding to
234731a8268SZbigniew Bodek * available ports. On platforms where this information
235731a8268SZbigniew Bodek * is missing, the driver can define available ports on its own.
236731a8268SZbigniew Bodek */
237731a8268SZbigniew Bodek int nports = (ctlr->caps & AHCI_CAP_NPMASK) + 1;
238731a8268SZbigniew Bodek int nmask = (1 << nports) - 1;
239731a8268SZbigniew Bodek
240731a8268SZbigniew Bodek ATA_OUTL(ctlr->r_mem, AHCI_PI, nmask);
241731a8268SZbigniew Bodek device_printf(dev, "Forcing PI to %d ports (mask = %x)\n",
242731a8268SZbigniew Bodek nports, nmask);
243731a8268SZbigniew Bodek }
244731a8268SZbigniew Bodek
24552c9ce25SScott Long ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI);
246232a9d55SAlexander Motin
247232a9d55SAlexander Motin /* Identify and set separate quirks for HBA and RAID f/w Marvells. */
248c6efb4c4SAlexander Motin if ((ctlr->quirks & AHCI_Q_ALTSIG) &&
249232a9d55SAlexander Motin (ctlr->caps & AHCI_CAP_SPM) == 0)
250c6efb4c4SAlexander Motin ctlr->quirks |= AHCI_Q_NOBSYRES;
251232a9d55SAlexander Motin
2526bd8779bSAlexander Motin if (ctlr->quirks & AHCI_Q_1CH) {
2536bd8779bSAlexander Motin ctlr->caps &= ~AHCI_CAP_NPMASK;
2546bd8779bSAlexander Motin ctlr->ichannels &= 0x01;
2556bd8779bSAlexander Motin }
2566bd8779bSAlexander Motin if (ctlr->quirks & AHCI_Q_2CH) {
2576bd8779bSAlexander Motin ctlr->caps &= ~AHCI_CAP_NPMASK;
2586bd8779bSAlexander Motin ctlr->caps |= 1;
2596bd8779bSAlexander Motin ctlr->ichannels &= 0x03;
2606bd8779bSAlexander Motin }
2616bd8779bSAlexander Motin if (ctlr->quirks & AHCI_Q_4CH) {
2626bd8779bSAlexander Motin ctlr->caps &= ~AHCI_CAP_NPMASK;
2636bd8779bSAlexander Motin ctlr->caps |= 3;
2646bd8779bSAlexander Motin ctlr->ichannels &= 0x0f;
2656bd8779bSAlexander Motin }
26652c9ce25SScott Long ctlr->channels = MAX(flsl(ctlr->ichannels),
2676bd8779bSAlexander Motin (ctlr->caps & AHCI_CAP_NPMASK) + 1);
2686bd8779bSAlexander Motin if (ctlr->quirks & AHCI_Q_NOPMP)
2696bd8779bSAlexander Motin ctlr->caps &= ~AHCI_CAP_SPM;
2706bd8779bSAlexander Motin if (ctlr->quirks & AHCI_Q_NONCQ)
2716bd8779bSAlexander Motin ctlr->caps &= ~AHCI_CAP_SNCQ;
272e67afc40SAlexander Motin if ((ctlr->caps & AHCI_CAP_CCCS) == 0)
273e67afc40SAlexander Motin ctlr->ccc = 0;
27496b7988fSAlexander Motin ctlr->emloc = ATA_INL(ctlr->r_mem, AHCI_EM_LOC);
275234aa029SAlexander Motin
276234aa029SAlexander Motin /* Create controller-wide DMA tag. */
277faab4473SIan Lepore if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
278234aa029SAlexander Motin (ctlr->caps & AHCI_CAP_64BIT) ? BUS_SPACE_MAXADDR :
279234aa029SAlexander Motin BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
280234aa029SAlexander Motin BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE,
281b05e505dSOlivier Houchard ctlr->dma_coherent ? BUS_DMA_COHERENT : 0, NULL, NULL,
282b05e505dSOlivier Houchard &ctlr->dma_tag)) {
2830af6011aSZbigniew Bodek ahci_free_mem(dev);
284234aa029SAlexander Motin rman_fini(&ctlr->sc_iomem);
285e4a8f2e2SSteven Hartland return (ENXIO);
286234aa029SAlexander Motin }
287234aa029SAlexander Motin
288e67afc40SAlexander Motin ahci_ctlr_setup(dev);
289802df3acSWarner Losh
2904acab041SWarner Losh /* Setup interrupts. */
2915f59ea47SSteven Hartland if ((error = ahci_setup_interrupt(dev)) != 0) {
2924acab041SWarner Losh bus_dma_tag_destroy(ctlr->dma_tag);
2930af6011aSZbigniew Bodek ahci_free_mem(dev);
2944acab041SWarner Losh rman_fini(&ctlr->sc_iomem);
295e4a8f2e2SSteven Hartland return (error);
2964acab041SWarner Losh }
2974acab041SWarner Losh
298227d67aaSAlexander Motin i = 0;
299227d67aaSAlexander Motin for (u = ctlr->ichannels; u != 0; u >>= 1)
300227d67aaSAlexander Motin i += (u & 1);
301227d67aaSAlexander Motin ctlr->direct = (ctlr->msi && (ctlr->numirqs > 1 || i <= 3));
302227d67aaSAlexander Motin resource_int_value(device_get_name(dev), device_get_unit(dev),
303227d67aaSAlexander Motin "direct", &ctlr->direct);
30452c9ce25SScott Long /* Announce HW capabilities. */
3054dbabf10SAlexander Motin speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT;
30652c9ce25SScott Long device_printf(dev,
3070e6eb061SAlexander Motin "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s%s\n",
30852c9ce25SScott Long ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f),
30952c9ce25SScott Long ((version >> 4) & 0xf0) + (version & 0x0f),
3104dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_NPMASK) + 1,
31152c9ce25SScott Long ((speed == 1) ? "1.5":((speed == 2) ? "3":
31252c9ce25SScott Long ((speed == 3) ? "6":"?"))),
3134dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SPM) ?
3140e6eb061SAlexander Motin "supported" : "not supported",
3150e6eb061SAlexander Motin (ctlr->caps & AHCI_CAP_FBSS) ?
3160e6eb061SAlexander Motin " with FBS" : "");
3176fb5c84eSSteven Hartland if (ctlr->quirks != 0) {
3186fb5c84eSSteven Hartland device_printf(dev, "quirks=0x%b\n", ctlr->quirks,
3196fb5c84eSSteven Hartland AHCI_Q_BIT_STRING);
3206fb5c84eSSteven Hartland }
32152c9ce25SScott Long if (bootverbose) {
32252c9ce25SScott Long device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps",
3234dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"",
3244dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"",
3254dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"",
3264dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"",
3274dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SSS) ? " SS":"",
3284dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"",
3294dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SAL) ? " AL":"",
3304dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"",
33152c9ce25SScott Long ((speed == 1) ? "1.5":((speed == 2) ? "3":
33252c9ce25SScott Long ((speed == 3) ? "6":"?"))));
33352c9ce25SScott Long printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n",
3344dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SAM) ? " AM":"",
3354dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SPM) ? " PM":"",
3364dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"",
3374dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"",
3384dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"",
3394dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"",
3404dbabf10SAlexander Motin ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1,
3414dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"",
3424dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_EMS) ? " EM":"",
3434dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"",
3444dbabf10SAlexander Motin (ctlr->caps & AHCI_CAP_NPMASK) + 1);
3454dbabf10SAlexander Motin }
346189d85ccSAlexander Motin if (bootverbose && version >= 0x00010200) {
34748f2741cSAlexander Motin device_printf(dev, "Caps2:%s%s%s%s%s%s\n",
34848f2741cSAlexander Motin (ctlr->caps2 & AHCI_CAP2_DESO) ? " DESO":"",
34948f2741cSAlexander Motin (ctlr->caps2 & AHCI_CAP2_SADM) ? " SADM":"",
35048f2741cSAlexander Motin (ctlr->caps2 & AHCI_CAP2_SDS) ? " SDS":"",
3514dbabf10SAlexander Motin (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"",
3524dbabf10SAlexander Motin (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"",
3534dbabf10SAlexander Motin (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":"");
35452c9ce25SScott Long }
35552c9ce25SScott Long /* Attach all channels on this controller */
35652c9ce25SScott Long for (unit = 0; unit < ctlr->channels; unit++) {
3575b56413dSWarner Losh child = device_add_child(dev, "ahcich", DEVICE_UNIT_ANY);
35872a61203SAlexander Motin if (child == NULL) {
35952c9ce25SScott Long device_printf(dev, "failed to add channel device\n");
36072a61203SAlexander Motin continue;
36172a61203SAlexander Motin }
36252c9ce25SScott Long device_set_ivars(child, (void *)(intptr_t)unit);
36372a61203SAlexander Motin if ((ctlr->ichannels & (1 << unit)) == 0)
36472a61203SAlexander Motin device_disable(child);
36552c9ce25SScott Long }
36693289cfcSWarner Losh /* Attach any remapped NVME device */
36793289cfcSWarner Losh for (; unit < ctlr->channels + ctlr->remapped_devices; unit++) {
3685b56413dSWarner Losh child = device_add_child(dev, "nvme", DEVICE_UNIT_ANY);
36993289cfcSWarner Losh if (child == NULL) {
37093289cfcSWarner Losh device_printf(dev, "failed to add remapped NVMe device");
37193289cfcSWarner Losh continue;
37293289cfcSWarner Losh }
37393289cfcSWarner Losh device_set_ivars(child, (void *)(intptr_t)(unit | AHCI_REMAPPED_UNIT));
37493289cfcSWarner Losh }
37593289cfcSWarner Losh
3769aba757eSAlexander Motin int em = (ctlr->caps & AHCI_CAP_EMS) != 0;
3779aba757eSAlexander Motin resource_int_value(device_get_name(dev), device_get_unit(dev),
3789aba757eSAlexander Motin "em", &em);
3799aba757eSAlexander Motin if (em) {
3805b56413dSWarner Losh child = device_add_child(dev, "ahciem", DEVICE_UNIT_ANY);
381d19f06b3SAlexander Motin if (child == NULL)
382d19f06b3SAlexander Motin device_printf(dev, "failed to add enclosure device\n");
383d19f06b3SAlexander Motin else
384610defb1SAlexander Motin device_set_ivars(child, (void *)(intptr_t)AHCI_EM_UNIT);
385d19f06b3SAlexander Motin }
38618250ec6SJohn Baldwin bus_attach_children(dev);
387e4a8f2e2SSteven Hartland return (0);
38852c9ce25SScott Long }
38952c9ce25SScott Long
390802df3acSWarner Losh int
ahci_detach(device_t dev)39152c9ce25SScott Long ahci_detach(device_t dev)
39252c9ce25SScott Long {
39352c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
394*3ddaf820SJohn Baldwin int error, i;
39552c9ce25SScott Long
39652c9ce25SScott Long /* Detach & delete all children */
397*3ddaf820SJohn Baldwin error = bus_generic_detach(dev);
398*3ddaf820SJohn Baldwin if (error != 0)
399*3ddaf820SJohn Baldwin return (error);
40011bcf702SHans Petter Selasky
40152c9ce25SScott Long /* Free interrupts. */
40252c9ce25SScott Long for (i = 0; i < ctlr->numirqs; i++) {
40352c9ce25SScott Long if (ctlr->irqs[i].r_irq) {
40452c9ce25SScott Long bus_teardown_intr(dev, ctlr->irqs[i].r_irq,
40552c9ce25SScott Long ctlr->irqs[i].handle);
40652c9ce25SScott Long bus_release_resource(dev, SYS_RES_IRQ,
40752c9ce25SScott Long ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq);
40852c9ce25SScott Long }
40952c9ce25SScott Long }
410234aa029SAlexander Motin bus_dma_tag_destroy(ctlr->dma_tag);
41152c9ce25SScott Long /* Free memory. */
41252c9ce25SScott Long rman_fini(&ctlr->sc_iomem);
4130af6011aSZbigniew Bodek ahci_free_mem(dev);
41453f5ac13SAlexander Motin mtx_destroy(&ctlr->ch_mtx);
4150af6011aSZbigniew Bodek return (0);
4160af6011aSZbigniew Bodek }
4170af6011aSZbigniew Bodek
4180af6011aSZbigniew Bodek void
ahci_free_mem(device_t dev)4190af6011aSZbigniew Bodek ahci_free_mem(device_t dev)
4200af6011aSZbigniew Bodek {
4210af6011aSZbigniew Bodek struct ahci_controller *ctlr = device_get_softc(dev);
4220af6011aSZbigniew Bodek
4230af6011aSZbigniew Bodek /* Release memory resources */
42452c9ce25SScott Long if (ctlr->r_mem)
42552c9ce25SScott Long bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
4260af6011aSZbigniew Bodek if (ctlr->r_msix_table)
4270af6011aSZbigniew Bodek bus_release_resource(dev, SYS_RES_MEMORY,
4280af6011aSZbigniew Bodek ctlr->r_msix_tab_rid, ctlr->r_msix_table);
4290af6011aSZbigniew Bodek if (ctlr->r_msix_pba)
4300af6011aSZbigniew Bodek bus_release_resource(dev, SYS_RES_MEMORY,
4310af6011aSZbigniew Bodek ctlr->r_msix_pba_rid, ctlr->r_msix_pba);
4320af6011aSZbigniew Bodek
4330af6011aSZbigniew Bodek ctlr->r_msix_pba = ctlr->r_mem = ctlr->r_msix_table = NULL;
43452c9ce25SScott Long }
43552c9ce25SScott Long
436802df3acSWarner Losh int
ahci_setup_interrupt(device_t dev)43752c9ce25SScott Long ahci_setup_interrupt(device_t dev)
43852c9ce25SScott Long {
43952c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
440227d67aaSAlexander Motin int i;
44152c9ce25SScott Long
44252c9ce25SScott Long /* Check for single MSI vector fallback. */
44352c9ce25SScott Long if (ctlr->numirqs > 1 &&
44452c9ce25SScott Long (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) {
44552c9ce25SScott Long device_printf(dev, "Falling back to one MSI\n");
44652c9ce25SScott Long ctlr->numirqs = 1;
44752c9ce25SScott Long }
448a12f5777SSteven Hartland
449a12f5777SSteven Hartland /* Ensure we don't overrun irqs. */
450a12f5777SSteven Hartland if (ctlr->numirqs > AHCI_MAX_IRQS) {
451a12f5777SSteven Hartland device_printf(dev, "Too many irqs %d > %d (clamping)\n",
452a12f5777SSteven Hartland ctlr->numirqs, AHCI_MAX_IRQS);
453a12f5777SSteven Hartland ctlr->numirqs = AHCI_MAX_IRQS;
454a12f5777SSteven Hartland }
455a12f5777SSteven Hartland
45652c9ce25SScott Long /* Allocate all IRQs. */
45752c9ce25SScott Long for (i = 0; i < ctlr->numirqs; i++) {
45852c9ce25SScott Long ctlr->irqs[i].ctlr = ctlr;
459227d67aaSAlexander Motin ctlr->irqs[i].r_irq_rid = i + (ctlr->msi ? 1 : 0);
46088971a90SAlexander Motin if (ctlr->channels == 1 && !ctlr->ccc && ctlr->msi)
461c45ff921SAlexander Motin ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE;
462c45ff921SAlexander Motin else if (ctlr->numirqs == 1 || i >= ctlr->channels ||
4634dbabf10SAlexander Motin (ctlr->ccc && i == ctlr->cccv))
46452c9ce25SScott Long ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL;
465eaf4f6feSAlexander Motin else if (ctlr->channels > ctlr->numirqs &&
466eaf4f6feSAlexander Motin i == ctlr->numirqs - 1)
46752c9ce25SScott Long ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER;
46852c9ce25SScott Long else
46952c9ce25SScott Long ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE;
47052c9ce25SScott Long if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
47152c9ce25SScott Long &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
47252c9ce25SScott Long device_printf(dev, "unable to map interrupt\n");
473e4a8f2e2SSteven Hartland return (ENXIO);
47452c9ce25SScott Long }
47552c9ce25SScott Long if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL,
476227d67aaSAlexander Motin (ctlr->irqs[i].mode != AHCI_IRQ_MODE_ONE) ? ahci_intr :
477227d67aaSAlexander Motin ((ctlr->quirks & AHCI_Q_EDGEIS) ? ahci_intr_one_edge :
478227d67aaSAlexander Motin ahci_intr_one),
47952c9ce25SScott Long &ctlr->irqs[i], &ctlr->irqs[i].handle))) {
48052c9ce25SScott Long /* SOS XXX release r_irq */
48152c9ce25SScott Long device_printf(dev, "unable to setup interrupt\n");
482e4a8f2e2SSteven Hartland return (ENXIO);
48352c9ce25SScott Long }
484f343c07fSAlexander Motin if (ctlr->numirqs > 1) {
485f343c07fSAlexander Motin bus_describe_intr(dev, ctlr->irqs[i].r_irq,
486f343c07fSAlexander Motin ctlr->irqs[i].handle,
487f343c07fSAlexander Motin ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE ?
488f343c07fSAlexander Motin "ch%d" : "%d", i);
489f343c07fSAlexander Motin }
49052c9ce25SScott Long }
49152c9ce25SScott Long return (0);
49252c9ce25SScott Long }
49352c9ce25SScott Long
49452c9ce25SScott Long /*
49552c9ce25SScott Long * Common case interrupt handler.
49652c9ce25SScott Long */
49752c9ce25SScott Long static void
ahci_intr(void * data)49852c9ce25SScott Long ahci_intr(void *data)
49952c9ce25SScott Long {
50052c9ce25SScott Long struct ahci_controller_irq *irq = data;
50152c9ce25SScott Long struct ahci_controller *ctlr = irq->ctlr;
502e67afc40SAlexander Motin u_int32_t is, ise = 0;
50352c9ce25SScott Long void *arg;
50452c9ce25SScott Long int unit;
50552c9ce25SScott Long
5064dbabf10SAlexander Motin if (irq->mode == AHCI_IRQ_MODE_ALL) {
50752c9ce25SScott Long unit = 0;
5084dbabf10SAlexander Motin if (ctlr->ccc)
5094dbabf10SAlexander Motin is = ctlr->ichannels;
5104dbabf10SAlexander Motin else
5114dbabf10SAlexander Motin is = ATA_INL(ctlr->r_mem, AHCI_IS);
5124dbabf10SAlexander Motin } else { /* AHCI_IRQ_MODE_AFTER */
51352c9ce25SScott Long unit = irq->r_irq_rid - 1;
5144dbabf10SAlexander Motin is = ATA_INL(ctlr->r_mem, AHCI_IS);
515e50f405eSAlexander Motin is &= (0xffffffff << unit);
5164dbabf10SAlexander Motin }
517e67afc40SAlexander Motin /* CCC interrupt is edge triggered. */
518e67afc40SAlexander Motin if (ctlr->ccc)
519e67afc40SAlexander Motin ise = 1 << ctlr->cccv;
52045e1aff1SAlexander Motin /* Some controllers have edge triggered IS. */
52145e1aff1SAlexander Motin if (ctlr->quirks & AHCI_Q_EDGEIS)
522e67afc40SAlexander Motin ise |= is;
523e67afc40SAlexander Motin if (ise != 0)
524e67afc40SAlexander Motin ATA_OUTL(ctlr->r_mem, AHCI_IS, ise);
52552c9ce25SScott Long for (; unit < ctlr->channels; unit++) {
52652c9ce25SScott Long if ((is & (1 << unit)) != 0 &&
52752c9ce25SScott Long (arg = ctlr->interrupt[unit].argument)) {
5286bd8779bSAlexander Motin ctlr->interrupt[unit].function(arg);
52952c9ce25SScott Long }
53052c9ce25SScott Long }
53193289cfcSWarner Losh for (; unit < ctlr->channels + ctlr->remapped_devices; unit++) {
53293289cfcSWarner Losh if ((arg = ctlr->interrupt[unit].argument)) {
53393289cfcSWarner Losh ctlr->interrupt[unit].function(arg);
53493289cfcSWarner Losh }
53593289cfcSWarner Losh }
53693289cfcSWarner Losh
53745e1aff1SAlexander Motin /* AHCI declares level triggered IS. */
53845e1aff1SAlexander Motin if (!(ctlr->quirks & AHCI_Q_EDGEIS))
53945e1aff1SAlexander Motin ATA_OUTL(ctlr->r_mem, AHCI_IS, is);
540d3214e8dSMichal Meloun ATA_RBL(ctlr->r_mem, AHCI_IS);
5416bd8779bSAlexander Motin }
54252c9ce25SScott Long
54352c9ce25SScott Long /*
54452c9ce25SScott Long * Simplified interrupt handler for multivector MSI mode.
54552c9ce25SScott Long */
54652c9ce25SScott Long static void
ahci_intr_one(void * data)54752c9ce25SScott Long ahci_intr_one(void *data)
54852c9ce25SScott Long {
54952c9ce25SScott Long struct ahci_controller_irq *irq = data;
55052c9ce25SScott Long struct ahci_controller *ctlr = irq->ctlr;
55152c9ce25SScott Long void *arg;
55252c9ce25SScott Long int unit;
55352c9ce25SScott Long
55452c9ce25SScott Long unit = irq->r_irq_rid - 1;
55552c9ce25SScott Long if ((arg = ctlr->interrupt[unit].argument))
55652c9ce25SScott Long ctlr->interrupt[unit].function(arg);
557f343c07fSAlexander Motin /* AHCI declares level triggered IS. */
558f343c07fSAlexander Motin ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit);
559d3214e8dSMichal Meloun ATA_RBL(ctlr->r_mem, AHCI_IS);
56052c9ce25SScott Long }
56152c9ce25SScott Long
562227d67aaSAlexander Motin static void
ahci_intr_one_edge(void * data)563227d67aaSAlexander Motin ahci_intr_one_edge(void *data)
564227d67aaSAlexander Motin {
565227d67aaSAlexander Motin struct ahci_controller_irq *irq = data;
566227d67aaSAlexander Motin struct ahci_controller *ctlr = irq->ctlr;
567227d67aaSAlexander Motin void *arg;
568227d67aaSAlexander Motin int unit;
569227d67aaSAlexander Motin
570227d67aaSAlexander Motin unit = irq->r_irq_rid - 1;
571227d67aaSAlexander Motin /* Some controllers have edge triggered IS. */
572227d67aaSAlexander Motin ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit);
573227d67aaSAlexander Motin if ((arg = ctlr->interrupt[unit].argument))
574227d67aaSAlexander Motin ctlr->interrupt[unit].function(arg);
575d3214e8dSMichal Meloun ATA_RBL(ctlr->r_mem, AHCI_IS);
576227d67aaSAlexander Motin }
577227d67aaSAlexander Motin
578802df3acSWarner Losh struct resource *
ahci_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)57952c9ce25SScott Long ahci_alloc_resource(device_t dev, device_t child, int type, int *rid,
5802dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
58152c9ce25SScott Long {
58252c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
583d19f06b3SAlexander Motin struct resource *res;
584d6f8b916SJustin Hibbits rman_res_t st;
585d19f06b3SAlexander Motin int offset, size, unit;
586610defb1SAlexander Motin bool is_em, is_remapped;
58752c9ce25SScott Long
588d19f06b3SAlexander Motin unit = (intptr_t)device_get_ivars(child);
589610defb1SAlexander Motin is_em = is_remapped = false;
59093289cfcSWarner Losh if (unit & AHCI_REMAPPED_UNIT) {
591610defb1SAlexander Motin unit &= AHCI_UNIT;
59293289cfcSWarner Losh unit -= ctlr->channels;
59393289cfcSWarner Losh is_remapped = true;
594610defb1SAlexander Motin } else if (unit & AHCI_EM_UNIT) {
595610defb1SAlexander Motin unit &= AHCI_UNIT;
596610defb1SAlexander Motin is_em = true;
597610defb1SAlexander Motin }
598d19f06b3SAlexander Motin res = NULL;
59952c9ce25SScott Long switch (type) {
60052c9ce25SScott Long case SYS_RES_MEMORY:
60193289cfcSWarner Losh if (is_remapped) {
60293289cfcSWarner Losh offset = ctlr->remap_offset + unit * ctlr->remap_size;
60393289cfcSWarner Losh size = ctlr->remap_size;
604610defb1SAlexander Motin } else if (!is_em) {
605d19f06b3SAlexander Motin offset = AHCI_OFFSET + (unit << 7);
606d19f06b3SAlexander Motin size = 128;
6079aba757eSAlexander Motin } else if ((ctlr->caps & AHCI_CAP_EMS) == 0) {
6089aba757eSAlexander Motin break;
609d19f06b3SAlexander Motin } else if (*rid == 0) {
610d19f06b3SAlexander Motin offset = AHCI_EM_CTL;
611d19f06b3SAlexander Motin size = 4;
612d19f06b3SAlexander Motin } else {
613d19f06b3SAlexander Motin offset = (ctlr->emloc & 0xffff0000) >> 14;
614d19f06b3SAlexander Motin size = (ctlr->emloc & 0x0000ffff) << 2;
615d19f06b3SAlexander Motin if (*rid != 1) {
616d19f06b3SAlexander Motin if (*rid == 2 && (ctlr->capsem &
617d19f06b3SAlexander Motin (AHCI_EM_XMT | AHCI_EM_SMB)) == 0)
618d19f06b3SAlexander Motin offset += size;
619d19f06b3SAlexander Motin else
620d19f06b3SAlexander Motin break;
621d19f06b3SAlexander Motin }
622d19f06b3SAlexander Motin }
62352c9ce25SScott Long st = rman_get_start(ctlr->r_mem);
62452c9ce25SScott Long res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
625d19f06b3SAlexander Motin st + offset + size - 1, size, RF_ACTIVE, child);
62652c9ce25SScott Long if (res) {
62752c9ce25SScott Long bus_space_handle_t bsh;
62852c9ce25SScott Long bus_space_tag_t bst;
62952c9ce25SScott Long bsh = rman_get_bushandle(ctlr->r_mem);
63052c9ce25SScott Long bst = rman_get_bustag(ctlr->r_mem);
63152c9ce25SScott Long bus_space_subregion(bst, bsh, offset, 128, &bsh);
63252c9ce25SScott Long rman_set_bushandle(res, bsh);
63352c9ce25SScott Long rman_set_bustag(res, bst);
63452c9ce25SScott Long }
63552c9ce25SScott Long break;
63652c9ce25SScott Long case SYS_RES_IRQ:
63752c9ce25SScott Long if (*rid == ATA_IRQ_RID)
63852c9ce25SScott Long res = ctlr->irqs[0].r_irq;
63952c9ce25SScott Long break;
64052c9ce25SScott Long }
64152c9ce25SScott Long return (res);
64252c9ce25SScott Long }
64352c9ce25SScott Long
644802df3acSWarner Losh int
ahci_release_resource(device_t dev,device_t child,struct resource * r)6459dbf5b0eSJohn Baldwin ahci_release_resource(device_t dev, device_t child, struct resource *r)
64652c9ce25SScott Long {
64752c9ce25SScott Long
6489dbf5b0eSJohn Baldwin switch (rman_get_type(r)) {
64952c9ce25SScott Long case SYS_RES_MEMORY:
65052c9ce25SScott Long rman_release_resource(r);
65152c9ce25SScott Long return (0);
65252c9ce25SScott Long case SYS_RES_IRQ:
6539dbf5b0eSJohn Baldwin if (rman_get_rid(r) != ATA_IRQ_RID)
654e4a8f2e2SSteven Hartland return (ENOENT);
65552c9ce25SScott Long return (0);
65652c9ce25SScott Long }
65752c9ce25SScott Long return (EINVAL);
65852c9ce25SScott Long }
65952c9ce25SScott Long
660802df3acSWarner Losh int
ahci_setup_intr(device_t dev,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * function,void * argument,void ** cookiep)66152c9ce25SScott Long ahci_setup_intr(device_t dev, device_t child, struct resource *irq,
66252c9ce25SScott Long int flags, driver_filter_t *filter, driver_intr_t *function,
66352c9ce25SScott Long void *argument, void **cookiep)
66452c9ce25SScott Long {
66552c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
666610defb1SAlexander Motin int unit = (intptr_t)device_get_ivars(child) & AHCI_UNIT;
66752c9ce25SScott Long
66852c9ce25SScott Long if (filter != NULL) {
66952c9ce25SScott Long printf("ahci.c: we cannot use a filter here\n");
67052c9ce25SScott Long return (EINVAL);
67152c9ce25SScott Long }
67252c9ce25SScott Long ctlr->interrupt[unit].function = function;
67352c9ce25SScott Long ctlr->interrupt[unit].argument = argument;
67452c9ce25SScott Long return (0);
67552c9ce25SScott Long }
67652c9ce25SScott Long
677802df3acSWarner Losh int
ahci_teardown_intr(device_t dev,device_t child,struct resource * irq,void * cookie)67852c9ce25SScott Long ahci_teardown_intr(device_t dev, device_t child, struct resource *irq,
67952c9ce25SScott Long void *cookie)
68052c9ce25SScott Long {
68152c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(dev);
682610defb1SAlexander Motin int unit = (intptr_t)device_get_ivars(child) & AHCI_UNIT;
68352c9ce25SScott Long
68452c9ce25SScott Long ctlr->interrupt[unit].function = NULL;
68552c9ce25SScott Long ctlr->interrupt[unit].argument = NULL;
68652c9ce25SScott Long return (0);
68752c9ce25SScott Long }
68852c9ce25SScott Long
689802df3acSWarner Losh int
ahci_print_child(device_t dev,device_t child)69052c9ce25SScott Long ahci_print_child(device_t dev, device_t child)
69152c9ce25SScott Long {
692610defb1SAlexander Motin intptr_t ivars;
693610defb1SAlexander Motin int retval;
69452c9ce25SScott Long
69552c9ce25SScott Long retval = bus_print_child_header(dev, child);
696610defb1SAlexander Motin ivars = (intptr_t)device_get_ivars(child);
697610defb1SAlexander Motin if ((ivars & AHCI_EM_UNIT) == 0)
698610defb1SAlexander Motin retval += printf(" at channel %d", (int)ivars & AHCI_UNIT);
69952c9ce25SScott Long retval += bus_print_child_footer(dev, child);
70052c9ce25SScott Long return (retval);
70152c9ce25SScott Long }
70252c9ce25SScott Long
703802df3acSWarner Losh int
ahci_child_location(device_t dev,device_t child,struct sbuf * sb)704ddfc9c4cSWarner Losh ahci_child_location(device_t dev, device_t child, struct sbuf *sb)
705445cc79cSAlexander Motin {
706610defb1SAlexander Motin intptr_t ivars;
707445cc79cSAlexander Motin
708610defb1SAlexander Motin ivars = (intptr_t)device_get_ivars(child);
709610defb1SAlexander Motin if ((ivars & AHCI_EM_UNIT) == 0)
710ddfc9c4cSWarner Losh sbuf_printf(sb, "channel=%d", (int)ivars & AHCI_UNIT);
711445cc79cSAlexander Motin return (0);
712445cc79cSAlexander Motin }
713445cc79cSAlexander Motin
714802df3acSWarner Losh bus_dma_tag_t
ahci_get_dma_tag(device_t dev,device_t child)715234aa029SAlexander Motin ahci_get_dma_tag(device_t dev, device_t child)
716234aa029SAlexander Motin {
717234aa029SAlexander Motin struct ahci_controller *ctlr = device_get_softc(dev);
718234aa029SAlexander Motin
719234aa029SAlexander Motin return (ctlr->dma_tag);
720234aa029SAlexander Motin }
721234aa029SAlexander Motin
72253f5ac13SAlexander Motin void
ahci_attached(device_t dev,struct ahci_channel * ch)72353f5ac13SAlexander Motin ahci_attached(device_t dev, struct ahci_channel *ch)
72453f5ac13SAlexander Motin {
72553f5ac13SAlexander Motin struct ahci_controller *ctlr = device_get_softc(dev);
72653f5ac13SAlexander Motin
72753f5ac13SAlexander Motin mtx_lock(&ctlr->ch_mtx);
72853f5ac13SAlexander Motin ctlr->ch[ch->unit] = ch;
72953f5ac13SAlexander Motin mtx_unlock(&ctlr->ch_mtx);
73053f5ac13SAlexander Motin }
73153f5ac13SAlexander Motin
73253f5ac13SAlexander Motin void
ahci_detached(device_t dev,struct ahci_channel * ch)73353f5ac13SAlexander Motin ahci_detached(device_t dev, struct ahci_channel *ch)
73453f5ac13SAlexander Motin {
73553f5ac13SAlexander Motin struct ahci_controller *ctlr = device_get_softc(dev);
73653f5ac13SAlexander Motin
73753f5ac13SAlexander Motin mtx_lock(&ctlr->ch_mtx);
73853f5ac13SAlexander Motin mtx_lock(&ch->mtx);
73953f5ac13SAlexander Motin ctlr->ch[ch->unit] = NULL;
74053f5ac13SAlexander Motin mtx_unlock(&ch->mtx);
74153f5ac13SAlexander Motin mtx_unlock(&ctlr->ch_mtx);
74253f5ac13SAlexander Motin }
74353f5ac13SAlexander Motin
74453f5ac13SAlexander Motin struct ahci_channel *
ahci_getch(device_t dev,int n)74553f5ac13SAlexander Motin ahci_getch(device_t dev, int n)
74653f5ac13SAlexander Motin {
74753f5ac13SAlexander Motin struct ahci_controller *ctlr = device_get_softc(dev);
74853f5ac13SAlexander Motin struct ahci_channel *ch;
74953f5ac13SAlexander Motin
75053f5ac13SAlexander Motin KASSERT(n >= 0 && n < AHCI_MAX_PORTS, ("Bad channel number %d", n));
75153f5ac13SAlexander Motin mtx_lock(&ctlr->ch_mtx);
75253f5ac13SAlexander Motin ch = ctlr->ch[n];
75353f5ac13SAlexander Motin if (ch != NULL)
75453f5ac13SAlexander Motin mtx_lock(&ch->mtx);
75553f5ac13SAlexander Motin mtx_unlock(&ctlr->ch_mtx);
75653f5ac13SAlexander Motin return (ch);
75753f5ac13SAlexander Motin }
75853f5ac13SAlexander Motin
75953f5ac13SAlexander Motin void
ahci_putch(struct ahci_channel * ch)76053f5ac13SAlexander Motin ahci_putch(struct ahci_channel *ch)
76153f5ac13SAlexander Motin {
76253f5ac13SAlexander Motin
76353f5ac13SAlexander Motin mtx_unlock(&ch->mtx);
76453f5ac13SAlexander Motin }
76553f5ac13SAlexander Motin
76652c9ce25SScott Long static int
ahci_ch_probe(device_t dev)76752c9ce25SScott Long ahci_ch_probe(device_t dev)
76852c9ce25SScott Long {
76952c9ce25SScott Long
770a74b4965SMark Johnston device_set_desc(dev, "AHCI channel");
7713036de3cSAlexander Motin return (BUS_PROBE_DEFAULT);
77252c9ce25SScott Long }
77352c9ce25SScott Long
77452c9ce25SScott Long static int
ahci_ch_disablephy_proc(SYSCTL_HANDLER_ARGS)7754668666fSWarner Losh ahci_ch_disablephy_proc(SYSCTL_HANDLER_ARGS)
7764668666fSWarner Losh {
7774668666fSWarner Losh struct ahci_channel *ch;
7784668666fSWarner Losh int error, value;
7794668666fSWarner Losh
7804668666fSWarner Losh ch = arg1;
7814668666fSWarner Losh value = ch->disablephy;
7824668666fSWarner Losh error = sysctl_handle_int(oidp, &value, 0, req);
7834668666fSWarner Losh if (error != 0 || req->newptr == NULL || (value != 0 && value != 1))
7844668666fSWarner Losh return (error);
7854668666fSWarner Losh
7864668666fSWarner Losh mtx_lock(&ch->mtx);
7874668666fSWarner Losh ch->disablephy = value;
7884668666fSWarner Losh if (value) {
7894668666fSWarner Losh ahci_ch_deinit(ch->dev);
7904668666fSWarner Losh } else {
7914668666fSWarner Losh ahci_ch_init(ch->dev);
7924668666fSWarner Losh ahci_phy_check_events(ch, ATA_SE_PHY_CHANGED | ATA_SE_EXCHANGED);
7934668666fSWarner Losh }
7944668666fSWarner Losh mtx_unlock(&ch->mtx);
7954668666fSWarner Losh
7964668666fSWarner Losh return (0);
7974668666fSWarner Losh }
7984668666fSWarner Losh
7994668666fSWarner Losh static int
ahci_ch_attach(device_t dev)80052c9ce25SScott Long ahci_ch_attach(device_t dev)
80152c9ce25SScott Long {
80252c9ce25SScott Long struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev));
80352c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
80452c9ce25SScott Long struct cam_devq *devq;
8054668666fSWarner Losh struct sysctl_ctx_list *ctx;
8064668666fSWarner Losh struct sysctl_oid *tree;
807b447e682SAlexander Motin int rid, error, i, sata_rev = 0;
8080e6eb061SAlexander Motin u_int32_t version;
80952c9ce25SScott Long
81052c9ce25SScott Long ch->dev = dev;
81152c9ce25SScott Long ch->unit = (intptr_t)device_get_ivars(dev);
8124dbabf10SAlexander Motin ch->caps = ctlr->caps;
8134dbabf10SAlexander Motin ch->caps2 = ctlr->caps2;
8147ec8c789SLuiz Otavio O Souza ch->start = ctlr->ch_start;
8156bd8779bSAlexander Motin ch->quirks = ctlr->quirks;
816802df3acSWarner Losh ch->vendorid = ctlr->vendorid;
817802df3acSWarner Losh ch->deviceid = ctlr->deviceid;
818802df3acSWarner Losh ch->subvendorid = ctlr->subvendorid;
819802df3acSWarner Losh ch->subdeviceid = ctlr->subdeviceid;
82096ecb95eSAlexander Motin ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1;
8214dbabf10SAlexander Motin mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF);
8226e88c2c5SAlexander Motin ch->pm_level = 0;
82352c9ce25SScott Long resource_int_value(device_get_name(dev),
82452c9ce25SScott Long device_get_unit(dev), "pm_level", &ch->pm_level);
825227d67aaSAlexander Motin STAILQ_INIT(&ch->doneq);
8264dbabf10SAlexander Motin if (ch->pm_level > 3)
8274dbabf10SAlexander Motin callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
828df1439e3SAlexander Motin callout_init_mtx(&ch->reset_timer, &ch->mtx, 0);
829802df3acSWarner Losh /* JMicron external ports (0) sometimes limited */
830802df3acSWarner Losh if ((ctlr->quirks & AHCI_Q_SATA1_UNIT0) && ch->unit == 0)
831b447e682SAlexander Motin sata_rev = 1;
8326268666cSAlexander Motin if (ch->quirks & AHCI_Q_SATA2)
8336268666cSAlexander Motin sata_rev = 2;
83452c9ce25SScott Long resource_int_value(device_get_name(dev),
835b447e682SAlexander Motin device_get_unit(dev), "sata_rev", &sata_rev);
836b447e682SAlexander Motin for (i = 0; i < 16; i++) {
837b447e682SAlexander Motin ch->user[i].revision = sata_rev;
838b447e682SAlexander Motin ch->user[i].mode = 0;
839b447e682SAlexander Motin ch->user[i].bytecount = 8192;
840b447e682SAlexander Motin ch->user[i].tags = ch->numslots;
841da6808c1SAlexander Motin ch->user[i].caps = 0;
842b447e682SAlexander Motin ch->curr[i] = ch->user[i];
843da6808c1SAlexander Motin if (ch->pm_level) {
844da6808c1SAlexander Motin ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ |
845da6808c1SAlexander Motin CTS_SATA_CAPS_H_APST |
846da6808c1SAlexander Motin CTS_SATA_CAPS_D_PMREQ | CTS_SATA_CAPS_D_APST;
847da6808c1SAlexander Motin }
8488d169381SAlexander Motin ch->user[i].caps |= CTS_SATA_CAPS_H_DMAAA |
8498d169381SAlexander Motin CTS_SATA_CAPS_H_AN;
850b447e682SAlexander Motin }
851d19f06b3SAlexander Motin rid = 0;
85252c9ce25SScott Long if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
85352c9ce25SScott Long &rid, RF_ACTIVE)))
85452c9ce25SScott Long return (ENXIO);
85507c35043SAlexander Motin ch->chcaps = ATA_INL(ch->r_mem, AHCI_P_CMD);
85607c35043SAlexander Motin version = ATA_INL(ctlr->r_mem, AHCI_VS);
85707c35043SAlexander Motin if (version < 0x00010200 && (ctlr->caps & AHCI_CAP_FBSS))
85807c35043SAlexander Motin ch->chcaps |= AHCI_P_CMD_FBSCP;
85907c35043SAlexander Motin if (ch->caps2 & AHCI_CAP2_SDS)
86007c35043SAlexander Motin ch->chscaps = ATA_INL(ch->r_mem, AHCI_P_DEVSLP);
86107c35043SAlexander Motin if (bootverbose) {
86207c35043SAlexander Motin device_printf(dev, "Caps:%s%s%s%s%s%s\n",
86307c35043SAlexander Motin (ch->chcaps & AHCI_P_CMD_HPCP) ? " HPCP":"",
86407c35043SAlexander Motin (ch->chcaps & AHCI_P_CMD_MPSP) ? " MPSP":"",
86507c35043SAlexander Motin (ch->chcaps & AHCI_P_CMD_CPD) ? " CPD":"",
86607c35043SAlexander Motin (ch->chcaps & AHCI_P_CMD_ESP) ? " ESP":"",
86707c35043SAlexander Motin (ch->chcaps & AHCI_P_CMD_FBSCP) ? " FBSCP":"",
86807c35043SAlexander Motin (ch->chscaps & AHCI_P_DEVSLP_DSP) ? " DSP":"");
86907c35043SAlexander Motin }
87052c9ce25SScott Long ahci_dmainit(dev);
87152c9ce25SScott Long ahci_slotsalloc(dev);
87252c9ce25SScott Long mtx_lock(&ch->mtx);
8733e78ee63SAlexander Motin ahci_ch_init(dev);
87452c9ce25SScott Long rid = ATA_IRQ_RID;
87552c9ce25SScott Long if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
87652c9ce25SScott Long &rid, RF_SHAREABLE | RF_ACTIVE))) {
87752c9ce25SScott Long device_printf(dev, "Unable to map interrupt\n");
878c4d8fe61SAlexander Motin error = ENXIO;
879c4d8fe61SAlexander Motin goto err0;
88052c9ce25SScott Long }
88152c9ce25SScott Long if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
882227d67aaSAlexander Motin ctlr->direct ? ahci_ch_intr_direct : ahci_ch_intr,
883fcd7f38fSAlexander Motin ch, &ch->ih))) {
88452c9ce25SScott Long device_printf(dev, "Unable to setup interrupt\n");
88552c9ce25SScott Long error = ENXIO;
88652c9ce25SScott Long goto err1;
88752c9ce25SScott Long }
88852c9ce25SScott Long /* Create the device queue for our SIM. */
88952c9ce25SScott Long devq = cam_simq_alloc(ch->numslots);
89052c9ce25SScott Long if (devq == NULL) {
89152c9ce25SScott Long device_printf(dev, "Unable to allocate simq\n");
89252c9ce25SScott Long error = ENOMEM;
89352c9ce25SScott Long goto err1;
89452c9ce25SScott Long }
89552c9ce25SScott Long /* Construct SIM entry */
89652c9ce25SScott Long ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch,
897fcd7f38fSAlexander Motin device_get_unit(dev), (struct mtx *)&ch->mtx,
89872dc1ba9SAlexander Motin (ch->quirks & AHCI_Q_NOCCS) ? 1 : min(2, ch->numslots),
89949b96d25SAlexander Motin (ch->caps & AHCI_CAP_SNCQ) ? ch->numslots : 0,
90049b96d25SAlexander Motin devq);
90152c9ce25SScott Long if (ch->sim == NULL) {
902c4d8fe61SAlexander Motin cam_simq_free(devq);
90352c9ce25SScott Long device_printf(dev, "unable to allocate sim\n");
90452c9ce25SScott Long error = ENOMEM;
905c4d8fe61SAlexander Motin goto err1;
90652c9ce25SScott Long }
90752c9ce25SScott Long if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
90852c9ce25SScott Long device_printf(dev, "unable to register xpt bus\n");
90952c9ce25SScott Long error = ENXIO;
91052c9ce25SScott Long goto err2;
91152c9ce25SScott Long }
91252c9ce25SScott Long if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
91352c9ce25SScott Long CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
91452c9ce25SScott Long device_printf(dev, "unable to create path\n");
91552c9ce25SScott Long error = ENXIO;
91652c9ce25SScott Long goto err3;
91752c9ce25SScott Long }
9184dbabf10SAlexander Motin if (ch->pm_level > 3) {
9194dbabf10SAlexander Motin callout_reset(&ch->pm_timer,
9204dbabf10SAlexander Motin (ch->pm_level == 4) ? hz / 1000 : hz / 8,
921fcd7f38fSAlexander Motin ahci_ch_pm, ch);
9224dbabf10SAlexander Motin }
92352c9ce25SScott Long mtx_unlock(&ch->mtx);
92453f5ac13SAlexander Motin ahci_attached(device_get_parent(dev), ch);
9254668666fSWarner Losh ctx = device_get_sysctl_ctx(dev);
9264668666fSWarner Losh tree = device_get_sysctl_tree(dev);
9274668666fSWarner Losh SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "disable_phy",
928b776de67SAlexander Motin CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, ch,
9297029da5cSPawel Biernacki 0, ahci_ch_disablephy_proc, "IU", "Disable PHY");
93052c9ce25SScott Long return (0);
93152c9ce25SScott Long
93252c9ce25SScott Long err3:
93352c9ce25SScott Long xpt_bus_deregister(cam_sim_path(ch->sim));
93452c9ce25SScott Long err2:
93552c9ce25SScott Long cam_sim_free(ch->sim, /*free_devq*/TRUE);
93652c9ce25SScott Long err1:
93752c9ce25SScott Long bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
938c4d8fe61SAlexander Motin err0:
93952c9ce25SScott Long bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
94052c9ce25SScott Long mtx_unlock(&ch->mtx);
9415b9392e8SAlexander Motin mtx_destroy(&ch->mtx);
94252c9ce25SScott Long return (error);
94352c9ce25SScott Long }
94452c9ce25SScott Long
94552c9ce25SScott Long static int
ahci_ch_detach(device_t dev)94652c9ce25SScott Long ahci_ch_detach(device_t dev)
94752c9ce25SScott Long {
94852c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
94952c9ce25SScott Long
95053f5ac13SAlexander Motin ahci_detached(device_get_parent(dev), ch);
95152c9ce25SScott Long mtx_lock(&ch->mtx);
95252c9ce25SScott Long xpt_async(AC_LOST_DEVICE, ch->path, NULL);
953df1439e3SAlexander Motin /* Forget about reset. */
954df1439e3SAlexander Motin if (ch->resetting) {
955df1439e3SAlexander Motin ch->resetting = 0;
956df1439e3SAlexander Motin xpt_release_simq(ch->sim, TRUE);
957df1439e3SAlexander Motin }
95852c9ce25SScott Long xpt_free_path(ch->path);
95952c9ce25SScott Long xpt_bus_deregister(cam_sim_path(ch->sim));
96052c9ce25SScott Long cam_sim_free(ch->sim, /*free_devq*/TRUE);
96152c9ce25SScott Long mtx_unlock(&ch->mtx);
96252c9ce25SScott Long
9634dbabf10SAlexander Motin if (ch->pm_level > 3)
9644dbabf10SAlexander Motin callout_drain(&ch->pm_timer);
965df1439e3SAlexander Motin callout_drain(&ch->reset_timer);
96652c9ce25SScott Long bus_teardown_intr(dev, ch->r_irq, ch->ih);
96752c9ce25SScott Long bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
96852c9ce25SScott Long
9696533cd19SAlexander Motin ahci_ch_deinit(dev);
97052c9ce25SScott Long ahci_slotsfree(dev);
97152c9ce25SScott Long ahci_dmafini(dev);
97252c9ce25SScott Long
97352c9ce25SScott Long bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
97452c9ce25SScott Long mtx_destroy(&ch->mtx);
97552c9ce25SScott Long return (0);
97652c9ce25SScott Long }
97752c9ce25SScott Long
97852c9ce25SScott Long static int
ahci_ch_init(device_t dev)9796533cd19SAlexander Motin ahci_ch_init(device_t dev)
98052c9ce25SScott Long {
98152c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
98252c9ce25SScott Long uint64_t work;
98352c9ce25SScott Long
98452c9ce25SScott Long /* Disable port interrupts */
98552c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
98652c9ce25SScott Long /* Setup work areas */
98752c9ce25SScott Long work = ch->dma.work_bus + AHCI_CL_OFFSET;
98852c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff);
98952c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32);
99052c9ce25SScott Long work = ch->dma.rfis_bus;
99152c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff);
99252c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32);
99352c9ce25SScott Long /* Activate the channel and power/spin up device */
99452c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD,
99552c9ce25SScott Long (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
9964dbabf10SAlexander Motin ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) |
99752c9ce25SScott Long ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 )));
998fcd7f38fSAlexander Motin ahci_start_fr(ch);
999fcd7f38fSAlexander Motin ahci_start(ch, 1);
100052c9ce25SScott Long return (0);
100152c9ce25SScott Long }
100252c9ce25SScott Long
10036533cd19SAlexander Motin static int
ahci_ch_deinit(device_t dev)10046533cd19SAlexander Motin ahci_ch_deinit(device_t dev)
10056533cd19SAlexander Motin {
10066533cd19SAlexander Motin struct ahci_channel *ch = device_get_softc(dev);
10076533cd19SAlexander Motin
10086533cd19SAlexander Motin /* Disable port interrupts. */
10096533cd19SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
10106533cd19SAlexander Motin /* Reset command register. */
1011fcd7f38fSAlexander Motin ahci_stop(ch);
1012fcd7f38fSAlexander Motin ahci_stop_fr(ch);
10136533cd19SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0);
10146533cd19SAlexander Motin /* Allow everything, including partial and slumber modes. */
10156533cd19SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0);
10166533cd19SAlexander Motin /* Request slumber mode transition and give some time to get there. */
10176533cd19SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER);
10186533cd19SAlexander Motin DELAY(100);
10196533cd19SAlexander Motin /* Disable PHY. */
10206533cd19SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE);
10216533cd19SAlexander Motin return (0);
10226533cd19SAlexander Motin }
10236533cd19SAlexander Motin
10246533cd19SAlexander Motin static int
ahci_ch_suspend(device_t dev)10256533cd19SAlexander Motin ahci_ch_suspend(device_t dev)
10266533cd19SAlexander Motin {
10276533cd19SAlexander Motin struct ahci_channel *ch = device_get_softc(dev);
10286533cd19SAlexander Motin
10296533cd19SAlexander Motin mtx_lock(&ch->mtx);
10306533cd19SAlexander Motin xpt_freeze_simq(ch->sim, 1);
1031df1439e3SAlexander Motin /* Forget about reset. */
1032df1439e3SAlexander Motin if (ch->resetting) {
1033df1439e3SAlexander Motin ch->resetting = 0;
1034df1439e3SAlexander Motin callout_stop(&ch->reset_timer);
1035df1439e3SAlexander Motin xpt_release_simq(ch->sim, TRUE);
1036df1439e3SAlexander Motin }
10376533cd19SAlexander Motin while (ch->oslots)
10386533cd19SAlexander Motin msleep(ch, &ch->mtx, PRIBIO, "ahcisusp", hz/100);
10396533cd19SAlexander Motin ahci_ch_deinit(dev);
10406533cd19SAlexander Motin mtx_unlock(&ch->mtx);
10416533cd19SAlexander Motin return (0);
10426533cd19SAlexander Motin }
10436533cd19SAlexander Motin
10446533cd19SAlexander Motin static int
ahci_ch_resume(device_t dev)10456533cd19SAlexander Motin ahci_ch_resume(device_t dev)
10466533cd19SAlexander Motin {
10476533cd19SAlexander Motin struct ahci_channel *ch = device_get_softc(dev);
10486533cd19SAlexander Motin
10496533cd19SAlexander Motin mtx_lock(&ch->mtx);
10506533cd19SAlexander Motin ahci_ch_init(dev);
1051fcd7f38fSAlexander Motin ahci_reset(ch);
10526533cd19SAlexander Motin xpt_release_simq(ch->sim, TRUE);
10536533cd19SAlexander Motin mtx_unlock(&ch->mtx);
10546533cd19SAlexander Motin return (0);
10556533cd19SAlexander Motin }
10566533cd19SAlexander Motin
105752c9ce25SScott Long static device_method_t ahcich_methods[] = {
105852c9ce25SScott Long DEVMETHOD(device_probe, ahci_ch_probe),
105952c9ce25SScott Long DEVMETHOD(device_attach, ahci_ch_attach),
106052c9ce25SScott Long DEVMETHOD(device_detach, ahci_ch_detach),
106152c9ce25SScott Long DEVMETHOD(device_suspend, ahci_ch_suspend),
106252c9ce25SScott Long DEVMETHOD(device_resume, ahci_ch_resume),
106321190895SMarius Strobl DEVMETHOD_END
106452c9ce25SScott Long };
106552c9ce25SScott Long static driver_t ahcich_driver = {
106652c9ce25SScott Long "ahcich",
106752c9ce25SScott Long ahcich_methods,
106852c9ce25SScott Long sizeof(struct ahci_channel)
106952c9ce25SScott Long };
107007c15a9dSJohn Baldwin DRIVER_MODULE(ahcich, ahci, ahcich_driver, NULL, NULL);
107152c9ce25SScott Long
107252c9ce25SScott Long struct ahci_dc_cb_args {
107352c9ce25SScott Long bus_addr_t maddr;
107452c9ce25SScott Long int error;
107552c9ce25SScott Long };
107652c9ce25SScott Long
107752c9ce25SScott Long static void
ahci_dmainit(device_t dev)107852c9ce25SScott Long ahci_dmainit(device_t dev)
107952c9ce25SScott Long {
108052c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
108152c9ce25SScott Long struct ahci_dc_cb_args dcba;
10820e6eb061SAlexander Motin size_t rfsize;
1083d91cc9ddSKonstantin Belousov int error;
108452c9ce25SScott Long
108552c9ce25SScott Long /* Command area. */
1086d91cc9ddSKonstantin Belousov error = bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
1087234aa029SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
108852c9ce25SScott Long NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE,
1089d91cc9ddSKonstantin Belousov 0, NULL, NULL, &ch->dma.work_tag);
1090d91cc9ddSKonstantin Belousov if (error != 0)
109152c9ce25SScott Long goto error;
1092d91cc9ddSKonstantin Belousov error = bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
1093d91cc9ddSKonstantin Belousov BUS_DMA_ZERO, &ch->dma.work_map);
1094d91cc9ddSKonstantin Belousov if (error != 0)
109552c9ce25SScott Long goto error;
1096d91cc9ddSKonstantin Belousov error = bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
1097d91cc9ddSKonstantin Belousov AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, BUS_DMA_NOWAIT);
1098d91cc9ddSKonstantin Belousov if (error != 0 || (error = dcba.error) != 0) {
109952c9ce25SScott Long bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
110052c9ce25SScott Long goto error;
110152c9ce25SScott Long }
110252c9ce25SScott Long ch->dma.work_bus = dcba.maddr;
110352c9ce25SScott Long /* FIS receive area. */
11040e6eb061SAlexander Motin if (ch->chcaps & AHCI_P_CMD_FBSCP)
11050e6eb061SAlexander Motin rfsize = 4096;
11060e6eb061SAlexander Motin else
11070e6eb061SAlexander Motin rfsize = 256;
1108d91cc9ddSKonstantin Belousov error = bus_dma_tag_create(bus_get_dma_tag(dev), rfsize, 0,
1109234aa029SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
11100e6eb061SAlexander Motin NULL, NULL, rfsize, 1, rfsize,
1111d91cc9ddSKonstantin Belousov 0, NULL, NULL, &ch->dma.rfis_tag);
1112d91cc9ddSKonstantin Belousov if (error != 0)
111352c9ce25SScott Long goto error;
1114d91cc9ddSKonstantin Belousov error = bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0,
1115d91cc9ddSKonstantin Belousov &ch->dma.rfis_map);
1116d91cc9ddSKonstantin Belousov if (error != 0)
111752c9ce25SScott Long goto error;
1118d91cc9ddSKonstantin Belousov error = bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis,
1119d91cc9ddSKonstantin Belousov rfsize, ahci_dmasetupc_cb, &dcba, BUS_DMA_NOWAIT);
1120d91cc9ddSKonstantin Belousov if (error != 0 || (error = dcba.error) != 0) {
112152c9ce25SScott Long bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
112252c9ce25SScott Long goto error;
112352c9ce25SScott Long }
112452c9ce25SScott Long ch->dma.rfis_bus = dcba.maddr;
112552c9ce25SScott Long /* Data area. */
1126d91cc9ddSKonstantin Belousov error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
1127234aa029SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
112852c9ce25SScott Long NULL, NULL,
1129cd853791SKonstantin Belousov AHCI_SG_ENTRIES * PAGE_SIZE, AHCI_SG_ENTRIES, AHCI_PRD_MAX,
1130d91cc9ddSKonstantin Belousov 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag);
1131d91cc9ddSKonstantin Belousov if (error != 0)
113252c9ce25SScott Long goto error;
113352c9ce25SScott Long return;
113452c9ce25SScott Long
113552c9ce25SScott Long error:
1136d91cc9ddSKonstantin Belousov device_printf(dev, "WARNING - DMA initialization failed, error %d\n",
1137d91cc9ddSKonstantin Belousov error);
113852c9ce25SScott Long ahci_dmafini(dev);
113952c9ce25SScott Long }
114052c9ce25SScott Long
114152c9ce25SScott Long static void
ahci_dmasetupc_cb(void * xsc,bus_dma_segment_t * segs,int nsegs,int error)114252c9ce25SScott Long ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
114352c9ce25SScott Long {
114452c9ce25SScott Long struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc;
114552c9ce25SScott Long
114652c9ce25SScott Long if (!(dcba->error = error))
114752c9ce25SScott Long dcba->maddr = segs[0].ds_addr;
114852c9ce25SScott Long }
114952c9ce25SScott Long
115052c9ce25SScott Long static void
ahci_dmafini(device_t dev)115152c9ce25SScott Long ahci_dmafini(device_t dev)
115252c9ce25SScott Long {
115352c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
115452c9ce25SScott Long
115552c9ce25SScott Long if (ch->dma.data_tag) {
115652c9ce25SScott Long bus_dma_tag_destroy(ch->dma.data_tag);
115752c9ce25SScott Long ch->dma.data_tag = NULL;
115852c9ce25SScott Long }
115952c9ce25SScott Long if (ch->dma.rfis_bus) {
116052c9ce25SScott Long bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map);
116152c9ce25SScott Long bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
116252c9ce25SScott Long ch->dma.rfis_bus = 0;
116352c9ce25SScott Long ch->dma.rfis = NULL;
116452c9ce25SScott Long }
116552c9ce25SScott Long if (ch->dma.work_bus) {
116652c9ce25SScott Long bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
116752c9ce25SScott Long bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
116852c9ce25SScott Long ch->dma.work_bus = 0;
116952c9ce25SScott Long ch->dma.work = NULL;
117052c9ce25SScott Long }
117152c9ce25SScott Long if (ch->dma.work_tag) {
117252c9ce25SScott Long bus_dma_tag_destroy(ch->dma.work_tag);
117352c9ce25SScott Long ch->dma.work_tag = NULL;
117452c9ce25SScott Long }
117552c9ce25SScott Long }
117652c9ce25SScott Long
117752c9ce25SScott Long static void
ahci_slotsalloc(device_t dev)117852c9ce25SScott Long ahci_slotsalloc(device_t dev)
117952c9ce25SScott Long {
118052c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
118152c9ce25SScott Long int i;
118252c9ce25SScott Long
118352c9ce25SScott Long /* Alloc and setup command/dma slots */
118452c9ce25SScott Long bzero(ch->slot, sizeof(ch->slot));
118552c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
118652c9ce25SScott Long struct ahci_slot *slot = &ch->slot[i];
118752c9ce25SScott Long
1188fcd7f38fSAlexander Motin slot->ch = ch;
118952c9ce25SScott Long slot->slot = i;
119052c9ce25SScott Long slot->state = AHCI_SLOT_EMPTY;
1191cd853791SKonstantin Belousov slot->ct_offset = AHCI_CT_OFFSET + AHCI_CT_SIZE * i;
119252c9ce25SScott Long slot->ccb = NULL;
119352c9ce25SScott Long callout_init_mtx(&slot->timeout, &ch->mtx, 0);
119452c9ce25SScott Long
119552c9ce25SScott Long if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
119652c9ce25SScott Long device_printf(ch->dev, "FAILURE - create data_map\n");
119752c9ce25SScott Long }
119852c9ce25SScott Long }
119952c9ce25SScott Long
120052c9ce25SScott Long static void
ahci_slotsfree(device_t dev)120152c9ce25SScott Long ahci_slotsfree(device_t dev)
120252c9ce25SScott Long {
120352c9ce25SScott Long struct ahci_channel *ch = device_get_softc(dev);
120452c9ce25SScott Long int i;
120552c9ce25SScott Long
120652c9ce25SScott Long /* Free all dma slots */
120752c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
120852c9ce25SScott Long struct ahci_slot *slot = &ch->slot[i];
120952c9ce25SScott Long
12104dbabf10SAlexander Motin callout_drain(&slot->timeout);
121152c9ce25SScott Long if (slot->dma.data_map) {
121252c9ce25SScott Long bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
121352c9ce25SScott Long slot->dma.data_map = NULL;
121452c9ce25SScott Long }
121552c9ce25SScott Long }
121652c9ce25SScott Long }
121752c9ce25SScott Long
121806ccfe1dSAlexander Motin static int
ahci_phy_check_events(struct ahci_channel * ch,u_int32_t serr)1219fcd7f38fSAlexander Motin ahci_phy_check_events(struct ahci_channel *ch, u_int32_t serr)
122052c9ce25SScott Long {
122152c9ce25SScott Long
122206ccfe1dSAlexander Motin if (((ch->pm_level == 0) && (serr & ATA_SE_PHY_CHANGED)) ||
122306ccfe1dSAlexander Motin ((ch->pm_level != 0 || ch->listening) && (serr & ATA_SE_EXCHANGED))) {
122452c9ce25SScott Long u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
122583c5d981SAlexander Motin union ccb *ccb;
122683c5d981SAlexander Motin
1227ed70cffdSAlexander Motin if (bootverbose) {
122806ccfe1dSAlexander Motin if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE)
1229fcd7f38fSAlexander Motin device_printf(ch->dev, "CONNECT requested\n");
123006ccfe1dSAlexander Motin else
1231fcd7f38fSAlexander Motin device_printf(ch->dev, "DISCONNECT requested\n");
123252c9ce25SScott Long }
1233fcd7f38fSAlexander Motin ahci_reset(ch);
123483c5d981SAlexander Motin if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
123506ccfe1dSAlexander Motin return (0);
123683c5d981SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL,
123783c5d981SAlexander Motin cam_sim_path(ch->sim),
123883c5d981SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
123983c5d981SAlexander Motin xpt_free_ccb(ccb);
124006ccfe1dSAlexander Motin return (0);
124106ccfe1dSAlexander Motin }
124206ccfe1dSAlexander Motin xpt_rescan(ccb);
124306ccfe1dSAlexander Motin return (1);
124406ccfe1dSAlexander Motin }
124506ccfe1dSAlexander Motin return (0);
124606ccfe1dSAlexander Motin }
124706ccfe1dSAlexander Motin
124806ccfe1dSAlexander Motin static void
ahci_cpd_check_events(struct ahci_channel * ch)1249fcd7f38fSAlexander Motin ahci_cpd_check_events(struct ahci_channel *ch)
125006ccfe1dSAlexander Motin {
125106ccfe1dSAlexander Motin u_int32_t status;
125206ccfe1dSAlexander Motin union ccb *ccb;
1253fcd7f38fSAlexander Motin device_t dev;
125406ccfe1dSAlexander Motin
125506ccfe1dSAlexander Motin if (ch->pm_level == 0)
125606ccfe1dSAlexander Motin return;
125706ccfe1dSAlexander Motin
125806ccfe1dSAlexander Motin status = ATA_INL(ch->r_mem, AHCI_P_CMD);
125906ccfe1dSAlexander Motin if ((status & AHCI_P_CMD_CPD) == 0)
126006ccfe1dSAlexander Motin return;
126106ccfe1dSAlexander Motin
126206ccfe1dSAlexander Motin if (bootverbose) {
1263fcd7f38fSAlexander Motin dev = ch->dev;
126406ccfe1dSAlexander Motin if (status & AHCI_P_CMD_CPS) {
126506ccfe1dSAlexander Motin device_printf(dev, "COLD CONNECT requested\n");
126606ccfe1dSAlexander Motin } else
126706ccfe1dSAlexander Motin device_printf(dev, "COLD DISCONNECT requested\n");
126806ccfe1dSAlexander Motin }
1269fcd7f38fSAlexander Motin ahci_reset(ch);
127006ccfe1dSAlexander Motin if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
127106ccfe1dSAlexander Motin return;
127206ccfe1dSAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(ch->sim),
127306ccfe1dSAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
127406ccfe1dSAlexander Motin xpt_free_ccb(ccb);
127583c5d981SAlexander Motin return;
127683c5d981SAlexander Motin }
127783c5d981SAlexander Motin xpt_rescan(ccb);
127852c9ce25SScott Long }
127952c9ce25SScott Long
128052c9ce25SScott Long static void
ahci_notify_events(struct ahci_channel * ch,u_int32_t status)1281fcd7f38fSAlexander Motin ahci_notify_events(struct ahci_channel *ch, u_int32_t status)
12824dbabf10SAlexander Motin {
12834dbabf10SAlexander Motin struct cam_path *dpath;
12844dbabf10SAlexander Motin int i;
12854dbabf10SAlexander Motin
1286a48eed83SAlexander Motin if (ch->caps & AHCI_CAP_SSNTF)
12874dbabf10SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status);
12884dbabf10SAlexander Motin if (bootverbose)
1289fcd7f38fSAlexander Motin device_printf(ch->dev, "SNTF 0x%04x\n", status);
12904dbabf10SAlexander Motin for (i = 0; i < 16; i++) {
12914dbabf10SAlexander Motin if ((status & (1 << i)) == 0)
12924dbabf10SAlexander Motin continue;
12934dbabf10SAlexander Motin if (xpt_create_path(&dpath, NULL,
12944dbabf10SAlexander Motin xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
12954dbabf10SAlexander Motin xpt_async(AC_SCSI_AEN, dpath, NULL);
12964dbabf10SAlexander Motin xpt_free_path(dpath);
12974dbabf10SAlexander Motin }
12984dbabf10SAlexander Motin }
12994dbabf10SAlexander Motin }
13004dbabf10SAlexander Motin
13014dbabf10SAlexander Motin static void
ahci_done(struct ahci_channel * ch,union ccb * ccb)1302227d67aaSAlexander Motin ahci_done(struct ahci_channel *ch, union ccb *ccb)
130352c9ce25SScott Long {
1304227d67aaSAlexander Motin
1305227d67aaSAlexander Motin mtx_assert(&ch->mtx, MA_OWNED);
1306227d67aaSAlexander Motin if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0 ||
1307227d67aaSAlexander Motin ch->batch == 0) {
1308227d67aaSAlexander Motin xpt_done(ccb);
1309227d67aaSAlexander Motin return;
1310227d67aaSAlexander Motin }
1311227d67aaSAlexander Motin
1312227d67aaSAlexander Motin STAILQ_INSERT_TAIL(&ch->doneq, &ccb->ccb_h, sim_links.stqe);
1313227d67aaSAlexander Motin }
1314227d67aaSAlexander Motin
1315227d67aaSAlexander Motin static void
ahci_ch_intr(void * arg)1316227d67aaSAlexander Motin ahci_ch_intr(void *arg)
1317227d67aaSAlexander Motin {
1318fcd7f38fSAlexander Motin struct ahci_channel *ch = (struct ahci_channel *)arg;
1319227d67aaSAlexander Motin uint32_t istatus;
1320227d67aaSAlexander Motin
1321227d67aaSAlexander Motin /* Read interrupt statuses. */
1322227d67aaSAlexander Motin istatus = ATA_INL(ch->r_mem, AHCI_P_IS);
132352c9ce25SScott Long
132452c9ce25SScott Long mtx_lock(&ch->mtx);
1325227d67aaSAlexander Motin ahci_ch_intr_main(ch, istatus);
132652c9ce25SScott Long mtx_unlock(&ch->mtx);
132752c9ce25SScott Long }
132852c9ce25SScott Long
132952c9ce25SScott Long static void
ahci_ch_intr_direct(void * arg)1330227d67aaSAlexander Motin ahci_ch_intr_direct(void *arg)
1331227d67aaSAlexander Motin {
1332fcd7f38fSAlexander Motin struct ahci_channel *ch = (struct ahci_channel *)arg;
1333227d67aaSAlexander Motin struct ccb_hdr *ccb_h;
1334227d67aaSAlexander Motin uint32_t istatus;
13356e9a599bSSteven Hartland STAILQ_HEAD(, ccb_hdr) tmp_doneq = STAILQ_HEAD_INITIALIZER(tmp_doneq);
1336227d67aaSAlexander Motin
1337227d67aaSAlexander Motin /* Read interrupt statuses. */
1338227d67aaSAlexander Motin istatus = ATA_INL(ch->r_mem, AHCI_P_IS);
1339227d67aaSAlexander Motin
1340227d67aaSAlexander Motin mtx_lock(&ch->mtx);
1341227d67aaSAlexander Motin ch->batch = 1;
1342227d67aaSAlexander Motin ahci_ch_intr_main(ch, istatus);
1343227d67aaSAlexander Motin ch->batch = 0;
13446e9a599bSSteven Hartland /*
13456e9a599bSSteven Hartland * Prevent the possibility of issues caused by processing the queue
13466e9a599bSSteven Hartland * while unlocked below by moving the contents to a local queue.
13476e9a599bSSteven Hartland */
13486e9a599bSSteven Hartland STAILQ_CONCAT(&tmp_doneq, &ch->doneq);
1349227d67aaSAlexander Motin mtx_unlock(&ch->mtx);
13506e9a599bSSteven Hartland while ((ccb_h = STAILQ_FIRST(&tmp_doneq)) != NULL) {
13516e9a599bSSteven Hartland STAILQ_REMOVE_HEAD(&tmp_doneq, sim_links.stqe);
1352227d67aaSAlexander Motin xpt_done_direct((union ccb *)ccb_h);
1353227d67aaSAlexander Motin }
1354227d67aaSAlexander Motin }
1355227d67aaSAlexander Motin
1356227d67aaSAlexander Motin static void
ahci_ch_pm(void * arg)13574dbabf10SAlexander Motin ahci_ch_pm(void *arg)
13584dbabf10SAlexander Motin {
1359fcd7f38fSAlexander Motin struct ahci_channel *ch = (struct ahci_channel *)arg;
13604dbabf10SAlexander Motin uint32_t work;
13614dbabf10SAlexander Motin
13624dbabf10SAlexander Motin if (ch->numrslots != 0)
13634dbabf10SAlexander Motin return;
13644dbabf10SAlexander Motin work = ATA_INL(ch->r_mem, AHCI_P_CMD);
13654dbabf10SAlexander Motin if (ch->pm_level == 4)
13664dbabf10SAlexander Motin work |= AHCI_P_CMD_PARTIAL;
13674dbabf10SAlexander Motin else
13684dbabf10SAlexander Motin work |= AHCI_P_CMD_SLUMBER;
13694dbabf10SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_CMD, work);
13704dbabf10SAlexander Motin }
13714dbabf10SAlexander Motin
13724dbabf10SAlexander Motin static void
ahci_ch_intr_main(struct ahci_channel * ch,uint32_t istatus)1373227d67aaSAlexander Motin ahci_ch_intr_main(struct ahci_channel *ch, uint32_t istatus)
137452c9ce25SScott Long {
1375227d67aaSAlexander Motin uint32_t cstatus, serr = 0, sntf = 0, ok, err;
137652c9ce25SScott Long enum ahci_err_type et;
137706ccfe1dSAlexander Motin int i, ccs, port, reset = 0;
137852c9ce25SScott Long
1379227d67aaSAlexander Motin /* Clear interrupt statuses. */
138052c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus);
138152c9ce25SScott Long /* Read command statuses. */
13823d449890SAlexander Motin if (ch->numtslots != 0)
13833d449890SAlexander Motin cstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
13843d449890SAlexander Motin else
13853d449890SAlexander Motin cstatus = 0;
13863d449890SAlexander Motin if (ch->numrslots != ch->numtslots)
13873d449890SAlexander Motin cstatus |= ATA_INL(ch->r_mem, AHCI_P_CI);
13883d449890SAlexander Motin /* Read SNTF in one of possible ways. */
13896a740c4aSAlexander Motin if ((istatus & AHCI_P_IX_SDB) &&
13906a740c4aSAlexander Motin (ch->pm_present || ch->curr[0].atapi != 0)) {
1391a48eed83SAlexander Motin if (ch->caps & AHCI_CAP_SSNTF)
1392aed39f0dSAlexander Motin sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF);
13930e6eb061SAlexander Motin else if (ch->fbs_enabled) {
13940e6eb061SAlexander Motin u_int8_t *fis = ch->dma.rfis + 0x58;
13950e6eb061SAlexander Motin
13960e6eb061SAlexander Motin for (i = 0; i < 16; i++) {
13970e6eb061SAlexander Motin if (fis[1] & 0x80) {
13980e6eb061SAlexander Motin fis[1] &= 0x7f;
13990e6eb061SAlexander Motin sntf |= 1 << i;
14000e6eb061SAlexander Motin }
14010e6eb061SAlexander Motin fis += 256;
14020e6eb061SAlexander Motin }
14030e6eb061SAlexander Motin } else {
1404a48eed83SAlexander Motin u_int8_t *fis = ch->dma.rfis + 0x58;
1405a48eed83SAlexander Motin
1406a48eed83SAlexander Motin if (fis[1] & 0x80)
1407a48eed83SAlexander Motin sntf = (1 << (fis[1] & 0x0f));
1408a48eed83SAlexander Motin }
1409a48eed83SAlexander Motin }
141052c9ce25SScott Long /* Process PHY events */
1411a1aa38deSAlexander Motin if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF |
1412a1aa38deSAlexander Motin AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
1413a1aa38deSAlexander Motin serr = ATA_INL(ch->r_mem, AHCI_P_SERR);
1414a1aa38deSAlexander Motin if (serr) {
1415a1aa38deSAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr);
1416fcd7f38fSAlexander Motin reset = ahci_phy_check_events(ch, serr);
1417a1aa38deSAlexander Motin }
1418a1aa38deSAlexander Motin }
141906ccfe1dSAlexander Motin /* Process cold presence detection events */
142006ccfe1dSAlexander Motin if ((istatus & AHCI_P_IX_CPD) && !reset)
1421fcd7f38fSAlexander Motin ahci_cpd_check_events(ch);
142252c9ce25SScott Long /* Process command errors */
1423a1aa38deSAlexander Motin if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF |
1424a1aa38deSAlexander Motin AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
142572dc1ba9SAlexander Motin if (ch->quirks & AHCI_Q_NOCCS) {
142672dc1ba9SAlexander Motin /*
142772dc1ba9SAlexander Motin * ASMedia chips sometimes report failed commands as
142872dc1ba9SAlexander Motin * completed. Count all running commands as failed.
142972dc1ba9SAlexander Motin */
143072dc1ba9SAlexander Motin cstatus |= ch->rslots;
143172dc1ba9SAlexander Motin
143272dc1ba9SAlexander Motin /* They also report wrong CCS, so try to guess one. */
143372dc1ba9SAlexander Motin ccs = powerof2(cstatus) ? ffs(cstatus) - 1 : -1;
143472dc1ba9SAlexander Motin } else {
143572dc1ba9SAlexander Motin ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) &
143672dc1ba9SAlexander Motin AHCI_P_CMD_CCS_MASK) >> AHCI_P_CMD_CCS_SHIFT;
143772dc1ba9SAlexander Motin }
14380e6eb061SAlexander Motin //device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x fbs %08x ccs %d\n",
14390e6eb061SAlexander Motin // __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD),
14400e6eb061SAlexander Motin // serr, ATA_INL(ch->r_mem, AHCI_P_FBS), ccs);
14410e6eb061SAlexander Motin port = -1;
14420e6eb061SAlexander Motin if (ch->fbs_enabled) {
14430e6eb061SAlexander Motin uint32_t fbs = ATA_INL(ch->r_mem, AHCI_P_FBS);
14440e6eb061SAlexander Motin if (fbs & AHCI_P_FBS_SDE) {
14450e6eb061SAlexander Motin port = (fbs & AHCI_P_FBS_DWE)
14460e6eb061SAlexander Motin >> AHCI_P_FBS_DWE_SHIFT;
14470e6eb061SAlexander Motin } else {
14480e6eb061SAlexander Motin for (i = 0; i < 16; i++) {
14490e6eb061SAlexander Motin if (ch->numrslotspd[i] == 0)
14500e6eb061SAlexander Motin continue;
14510e6eb061SAlexander Motin if (port == -1)
14520e6eb061SAlexander Motin port = i;
14530e6eb061SAlexander Motin else if (port != i) {
14540e6eb061SAlexander Motin port = -2;
14550e6eb061SAlexander Motin break;
14560e6eb061SAlexander Motin }
14570e6eb061SAlexander Motin }
14580e6eb061SAlexander Motin }
14590e6eb061SAlexander Motin }
14603d449890SAlexander Motin err = ch->rslots & cstatus;
146152c9ce25SScott Long } else {
146252c9ce25SScott Long ccs = 0;
146352c9ce25SScott Long err = 0;
14640e6eb061SAlexander Motin port = -1;
146552c9ce25SScott Long }
1466453130d9SPedro F. Giffuni /* Complete all successful commands. */
14673d449890SAlexander Motin ok = ch->rslots & ~cstatus;
146852c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
146952c9ce25SScott Long if ((ok >> i) & 1)
147052c9ce25SScott Long ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE);
147152c9ce25SScott Long }
147252c9ce25SScott Long /* On error, complete the rest of commands with error statuses. */
147352c9ce25SScott Long if (err) {
147452c9ce25SScott Long if (ch->frozen) {
147552c9ce25SScott Long union ccb *fccb = ch->frozen;
147652c9ce25SScott Long ch->frozen = NULL;
147752c9ce25SScott Long fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1478a1aa38deSAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1479a1aa38deSAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1);
1480a1aa38deSAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN;
1481a1aa38deSAlexander Motin }
1482227d67aaSAlexander Motin ahci_done(ch, fccb);
148352c9ce25SScott Long }
148452c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
1485bc922874SGordon Bergling /* XXX: requests in loading state. */
148652c9ce25SScott Long if (((err >> i) & 1) == 0)
148752c9ce25SScott Long continue;
14880e6eb061SAlexander Motin if (port >= 0 &&
14890e6eb061SAlexander Motin ch->slot[i].ccb->ccb_h.target_id != port)
14900e6eb061SAlexander Motin continue;
1491c269e210SAlexander Motin if (istatus & AHCI_P_IX_TFE) {
14920e6eb061SAlexander Motin if (port != -2) {
149352c9ce25SScott Long /* Task File Error */
14940e6eb061SAlexander Motin if (ch->numtslotspd[
14950e6eb061SAlexander Motin ch->slot[i].ccb->ccb_h.target_id] == 0) {
149652c9ce25SScott Long /* Untagged operation. */
149752c9ce25SScott Long if (i == ccs)
149852c9ce25SScott Long et = AHCI_ERR_TFE;
149952c9ce25SScott Long else
150052c9ce25SScott Long et = AHCI_ERR_INNOCENT;
150152c9ce25SScott Long } else {
150252c9ce25SScott Long /* Tagged operation. */
150352c9ce25SScott Long et = AHCI_ERR_NCQ;
15040e6eb061SAlexander Motin }
15050e6eb061SAlexander Motin } else {
15060e6eb061SAlexander Motin et = AHCI_ERR_TFE;
15070e6eb061SAlexander Motin ch->fatalerr = 1;
150852c9ce25SScott Long }
1509c269e210SAlexander Motin } else if (istatus & AHCI_P_IX_IF) {
15100e6eb061SAlexander Motin if (ch->numtslots == 0 && i != ccs && port != -2)
1511c269e210SAlexander Motin et = AHCI_ERR_INNOCENT;
1512c269e210SAlexander Motin else
1513c269e210SAlexander Motin et = AHCI_ERR_SATA;
151452c9ce25SScott Long } else
151552c9ce25SScott Long et = AHCI_ERR_INVALID;
151652c9ce25SScott Long ahci_end_transaction(&ch->slot[i], et);
151752c9ce25SScott Long }
15180e6eb061SAlexander Motin /*
15190e6eb061SAlexander Motin * We can't reinit port if there are some other
15200e6eb061SAlexander Motin * commands active, use resume to complete them.
15210e6eb061SAlexander Motin */
15226bbd332eSAlexander Motin if (ch->rslots != 0 && !ch->recoverycmd)
15230e6eb061SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_FBS, AHCI_P_FBS_EN | AHCI_P_FBS_DEC);
152452c9ce25SScott Long }
15254dbabf10SAlexander Motin /* Process NOTIFY events */
1526aed39f0dSAlexander Motin if (sntf)
1527fcd7f38fSAlexander Motin ahci_notify_events(ch, sntf);
152852c9ce25SScott Long }
152952c9ce25SScott Long
153052c9ce25SScott Long /* Must be called with channel locked. */
153152c9ce25SScott Long static int
ahci_check_collision(struct ahci_channel * ch,union ccb * ccb)1532fcd7f38fSAlexander Motin ahci_check_collision(struct ahci_channel *ch, union ccb *ccb)
153352c9ce25SScott Long {
15340e6eb061SAlexander Motin int t = ccb->ccb_h.target_id;
153552c9ce25SScott Long
153652c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
153752c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
15380e6eb061SAlexander Motin /* Tagged command while we have no supported tag free. */
15390e6eb061SAlexander Motin if (((~ch->oslots) & (0xffffffff >> (32 -
15400e6eb061SAlexander Motin ch->curr[t].tags))) == 0)
15410e6eb061SAlexander Motin return (1);
15420e6eb061SAlexander Motin /* If we have FBS */
15430e6eb061SAlexander Motin if (ch->fbs_enabled) {
15440e6eb061SAlexander Motin /* Tagged command while untagged are active. */
15450e6eb061SAlexander Motin if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] == 0)
15460e6eb061SAlexander Motin return (1);
15470e6eb061SAlexander Motin } else {
154852c9ce25SScott Long /* Tagged command while untagged are active. */
154952c9ce25SScott Long if (ch->numrslots != 0 && ch->numtslots == 0)
155052c9ce25SScott Long return (1);
155152c9ce25SScott Long /* Tagged command while tagged to other target is active. */
155252c9ce25SScott Long if (ch->numtslots != 0 &&
155352c9ce25SScott Long ch->taggedtarget != ccb->ccb_h.target_id)
155452c9ce25SScott Long return (1);
15550e6eb061SAlexander Motin }
15560e6eb061SAlexander Motin } else {
15570e6eb061SAlexander Motin /* If we have FBS */
15580e6eb061SAlexander Motin if (ch->fbs_enabled) {
15590e6eb061SAlexander Motin /* Untagged command while tagged are active. */
15600e6eb061SAlexander Motin if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] != 0)
1561c8039fc6SAlexander Motin return (1);
156252c9ce25SScott Long } else {
156352c9ce25SScott Long /* Untagged command while tagged are active. */
156452c9ce25SScott Long if (ch->numrslots != 0 && ch->numtslots != 0)
156552c9ce25SScott Long return (1);
156652c9ce25SScott Long }
15670e6eb061SAlexander Motin }
156852c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
156952c9ce25SScott Long (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
157052c9ce25SScott Long /* Atomic command while anything active. */
157152c9ce25SScott Long if (ch->numrslots != 0)
157252c9ce25SScott Long return (1);
157352c9ce25SScott Long }
157452c9ce25SScott Long /* We have some atomic command running. */
157552c9ce25SScott Long if (ch->aslots != 0)
157652c9ce25SScott Long return (1);
157752c9ce25SScott Long return (0);
157852c9ce25SScott Long }
157952c9ce25SScott Long
158052c9ce25SScott Long /* Must be called with channel locked. */
158152c9ce25SScott Long static void
ahci_begin_transaction(struct ahci_channel * ch,union ccb * ccb)1582fcd7f38fSAlexander Motin ahci_begin_transaction(struct ahci_channel *ch, union ccb *ccb)
158352c9ce25SScott Long {
158452c9ce25SScott Long struct ahci_slot *slot;
1585c8039fc6SAlexander Motin int tag, tags;
158652c9ce25SScott Long
158752c9ce25SScott Long /* Choose empty slot. */
1588c8039fc6SAlexander Motin tags = ch->numslots;
1589c8039fc6SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1590c8039fc6SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
1591c8039fc6SAlexander Motin tags = ch->curr[ccb->ccb_h.target_id].tags;
1592fcd7f38fSAlexander Motin if (ch->lastslot + 1 < tags)
1593fcd7f38fSAlexander Motin tag = ffs(~(ch->oslots >> (ch->lastslot + 1)));
1594fcd7f38fSAlexander Motin else
159552c9ce25SScott Long tag = 0;
1596fcd7f38fSAlexander Motin if (tag == 0 || tag + ch->lastslot >= tags)
1597fcd7f38fSAlexander Motin tag = ffs(~ch->oslots) - 1;
1598fcd7f38fSAlexander Motin else
1599fcd7f38fSAlexander Motin tag += ch->lastslot;
160052c9ce25SScott Long ch->lastslot = tag;
160152c9ce25SScott Long /* Occupy chosen slot. */
160252c9ce25SScott Long slot = &ch->slot[tag];
160352c9ce25SScott Long slot->ccb = ccb;
16044dbabf10SAlexander Motin /* Stop PM timer. */
16054dbabf10SAlexander Motin if (ch->numrslots == 0 && ch->pm_level > 3)
16064dbabf10SAlexander Motin callout_stop(&ch->pm_timer);
160752c9ce25SScott Long /* Update channel stats. */
1608fcd7f38fSAlexander Motin ch->oslots |= (1 << tag);
160952c9ce25SScott Long ch->numrslots++;
16100e6eb061SAlexander Motin ch->numrslotspd[ccb->ccb_h.target_id]++;
161152c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
161252c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
161352c9ce25SScott Long ch->numtslots++;
16140e6eb061SAlexander Motin ch->numtslotspd[ccb->ccb_h.target_id]++;
161552c9ce25SScott Long ch->taggedtarget = ccb->ccb_h.target_id;
161652c9ce25SScott Long }
161752c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
161852c9ce25SScott Long (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
1619fcd7f38fSAlexander Motin ch->aslots |= (1 << tag);
162052c9ce25SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
162152c9ce25SScott Long slot->state = AHCI_SLOT_LOADING;
1622dd0b4fb6SKonstantin Belousov bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, ccb,
1623dd0b4fb6SKonstantin Belousov ahci_dmasetprd, slot, 0);
1624fcd7f38fSAlexander Motin } else {
1625fcd7f38fSAlexander Motin slot->dma.nsegs = 0;
162652c9ce25SScott Long ahci_execute_transaction(slot);
162752c9ce25SScott Long }
1628fcd7f38fSAlexander Motin }
162952c9ce25SScott Long
163052c9ce25SScott Long /* Locked by busdma engine. */
163152c9ce25SScott Long static void
ahci_dmasetprd(void * arg,bus_dma_segment_t * segs,int nsegs,int error)163252c9ce25SScott Long ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
163352c9ce25SScott Long {
163452c9ce25SScott Long struct ahci_slot *slot = arg;
1635fcd7f38fSAlexander Motin struct ahci_channel *ch = slot->ch;
163652c9ce25SScott Long struct ahci_cmd_tab *ctp;
163752c9ce25SScott Long struct ahci_dma_prd *prd;
163852c9ce25SScott Long int i;
163952c9ce25SScott Long
164052c9ce25SScott Long if (error) {
1641fcd7f38fSAlexander Motin device_printf(ch->dev, "DMA load error\n");
164252c9ce25SScott Long ahci_end_transaction(slot, AHCI_ERR_INVALID);
164352c9ce25SScott Long return;
164452c9ce25SScott Long }
164552c9ce25SScott Long KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n"));
164652c9ce25SScott Long /* Get a piece of the workspace for this request */
1647cd853791SKonstantin Belousov ctp = (struct ahci_cmd_tab *)(ch->dma.work + slot->ct_offset);
164852c9ce25SScott Long /* Fill S/G table */
164952c9ce25SScott Long prd = &ctp->prd_tab[0];
165052c9ce25SScott Long for (i = 0; i < nsegs; i++) {
165152c9ce25SScott Long prd[i].dba = htole64(segs[i].ds_addr);
165252c9ce25SScott Long prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK);
165352c9ce25SScott Long }
165452c9ce25SScott Long slot->dma.nsegs = nsegs;
165552c9ce25SScott Long bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
165652c9ce25SScott Long ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
165752c9ce25SScott Long BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
165852c9ce25SScott Long ahci_execute_transaction(slot);
165952c9ce25SScott Long }
166052c9ce25SScott Long
166152c9ce25SScott Long /* Must be called with channel locked. */
166252c9ce25SScott Long static void
ahci_execute_transaction(struct ahci_slot * slot)166352c9ce25SScott Long ahci_execute_transaction(struct ahci_slot *slot)
166452c9ce25SScott Long {
1665fcd7f38fSAlexander Motin struct ahci_channel *ch = slot->ch;
166652c9ce25SScott Long struct ahci_cmd_tab *ctp;
166752c9ce25SScott Long struct ahci_cmd_list *clp;
166852c9ce25SScott Long union ccb *ccb = slot->ccb;
166952c9ce25SScott Long int port = ccb->ccb_h.target_id & 0x0f;
1670232a9d55SAlexander Motin int fis_size, i, softreset;
16710e6eb061SAlexander Motin uint8_t *fis = ch->dma.rfis + 0x40;
16720e6eb061SAlexander Motin uint8_t val;
167333dc8039SLeandro Lupori uint16_t cmd_flags;
167452c9ce25SScott Long
167552c9ce25SScott Long /* Get a piece of the workspace for this request */
1676cd853791SKonstantin Belousov ctp = (struct ahci_cmd_tab *)(ch->dma.work + slot->ct_offset);
167752c9ce25SScott Long /* Setup the FIS for this request */
1678fcd7f38fSAlexander Motin if (!(fis_size = ahci_setup_fis(ch, ctp, ccb, slot->slot))) {
167952c9ce25SScott Long device_printf(ch->dev, "Setting up SATA FIS failed\n");
168052c9ce25SScott Long ahci_end_transaction(slot, AHCI_ERR_INVALID);
168152c9ce25SScott Long return;
168252c9ce25SScott Long }
168352c9ce25SScott Long /* Setup the command list entry */
168452c9ce25SScott Long clp = (struct ahci_cmd_list *)
168552c9ce25SScott Long (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot));
168633dc8039SLeandro Lupori cmd_flags =
1687ba3a9995SAlexander Motin (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) |
168852c9ce25SScott Long (ccb->ccb_h.func_code == XPT_SCSI_IO ?
168952c9ce25SScott Long (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) |
169052c9ce25SScott Long (fis_size / sizeof(u_int32_t)) |
169133dc8039SLeandro Lupori (port << 12);
1692ba3a9995SAlexander Motin clp->prd_length = htole16(slot->dma.nsegs);
169352c9ce25SScott Long /* Special handling for Soft Reset command. */
169452c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
16950e6eb061SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
16960e6eb061SAlexander Motin if (ccb->ataio.cmd.control & ATA_A_RESET) {
1697232a9d55SAlexander Motin softreset = 1;
169852c9ce25SScott Long /* Kick controller into sane state */
1699fcd7f38fSAlexander Motin ahci_stop(ch);
1700fcd7f38fSAlexander Motin ahci_clo(ch);
1701fcd7f38fSAlexander Motin ahci_start(ch, 0);
170233dc8039SLeandro Lupori cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY;
17030e6eb061SAlexander Motin } else {
1704232a9d55SAlexander Motin softreset = 2;
17050e6eb061SAlexander Motin /* Prepare FIS receive area for check. */
17060e6eb061SAlexander Motin for (i = 0; i < 20; i++)
17070e6eb061SAlexander Motin fis[i] = 0xff;
17080e6eb061SAlexander Motin }
1709232a9d55SAlexander Motin } else
1710232a9d55SAlexander Motin softreset = 0;
171152c9ce25SScott Long clp->bytecount = 0;
171233dc8039SLeandro Lupori clp->cmd_flags = htole16(cmd_flags);
1713cd853791SKonstantin Belousov clp->cmd_table_phys = htole64(ch->dma.work_bus + slot->ct_offset);
171452c9ce25SScott Long bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1715ba3a9995SAlexander Motin BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
171652c9ce25SScott Long bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
171752c9ce25SScott Long BUS_DMASYNC_PREREAD);
171852c9ce25SScott Long /* Set ACTIVE bit for NCQ commands. */
171952c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
172052c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
172152c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot);
172252c9ce25SScott Long }
17230e6eb061SAlexander Motin /* If FBS is enabled, set PMP port. */
17240e6eb061SAlexander Motin if (ch->fbs_enabled) {
17250e6eb061SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_FBS, AHCI_P_FBS_EN |
17260e6eb061SAlexander Motin (port << AHCI_P_FBS_DEV_SHIFT));
17270e6eb061SAlexander Motin }
172852c9ce25SScott Long /* Issue command to the controller. */
172952c9ce25SScott Long slot->state = AHCI_SLOT_RUNNING;
173052c9ce25SScott Long ch->rslots |= (1 << slot->slot);
173152c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot));
173252c9ce25SScott Long /* Device reset commands doesn't interrupt. Poll them. */
173352c9ce25SScott Long if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1734232a9d55SAlexander Motin (ccb->ataio.cmd.command == ATA_DEVICE_RESET || softreset)) {
17353a3d820bSAlexander Motin int count, timeout = ccb->ccb_h.timeout * 100;
173652c9ce25SScott Long enum ahci_err_type et = AHCI_ERR_NONE;
173752c9ce25SScott Long
173852c9ce25SScott Long for (count = 0; count < timeout; count++) {
17393a3d820bSAlexander Motin DELAY(10);
174052c9ce25SScott Long if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot)))
174152c9ce25SScott Long break;
1742232a9d55SAlexander Motin if ((ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) &&
1743232a9d55SAlexander Motin softreset != 1) {
174408c8fde0SAlexander Motin #if 0
174552c9ce25SScott Long device_printf(ch->dev,
174652c9ce25SScott Long "Poll error on slot %d, TFD: %04x\n",
174752c9ce25SScott Long slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD));
174808c8fde0SAlexander Motin #endif
174952c9ce25SScott Long et = AHCI_ERR_TFE;
175052c9ce25SScott Long break;
175152c9ce25SScott Long }
1752a69552e4SAlexander Motin /* Workaround for ATI SB600/SB700 chipsets. */
1753a69552e4SAlexander Motin if (ccb->ccb_h.target_id == 15 &&
1754802df3acSWarner Losh (ch->quirks & AHCI_Q_ATI_PMP_BUG) &&
1755a69552e4SAlexander Motin (ATA_INL(ch->r_mem, AHCI_P_IS) & AHCI_P_IX_IPM)) {
1756a69552e4SAlexander Motin et = AHCI_ERR_TIMEOUT;
1757a69552e4SAlexander Motin break;
1758a69552e4SAlexander Motin }
175952c9ce25SScott Long }
1760232a9d55SAlexander Motin
1761c6efb4c4SAlexander Motin /*
1762a569f640SWojciech Macek * Some Marvell controllers require additional time
1763a569f640SWojciech Macek * after soft reset to work properly. Setup delay
1764a569f640SWojciech Macek * to 50ms after soft reset.
1765a569f640SWojciech Macek */
1766a569f640SWojciech Macek if (ch->quirks & AHCI_Q_MRVL_SR_DEL)
1767a569f640SWojciech Macek DELAY(50000);
1768a569f640SWojciech Macek
1769a569f640SWojciech Macek /*
1770c6efb4c4SAlexander Motin * Marvell HBAs with non-RAID firmware do not wait for
1771c6efb4c4SAlexander Motin * readiness after soft reset, so we have to wait here.
1772985da6dbSAlexander Motin * Marvell RAIDs do not have this problem, but instead
1773985da6dbSAlexander Motin * sometimes forget to update FIS receive area, breaking
1774985da6dbSAlexander Motin * this wait.
1775c6efb4c4SAlexander Motin */
1776c6efb4c4SAlexander Motin if ((ch->quirks & AHCI_Q_NOBSYRES) == 0 &&
1777c6efb4c4SAlexander Motin (ch->quirks & AHCI_Q_ATI_PMP_BUG) == 0 &&
1778c6efb4c4SAlexander Motin softreset == 2 && et == AHCI_ERR_NONE) {
1779b1d2c95eSMichal Meloun for ( ; count < timeout; count++) {
1780b1d2c95eSMichal Meloun bus_dmamap_sync(ch->dma.rfis_tag,
1781b1d2c95eSMichal Meloun ch->dma.rfis_map, BUS_DMASYNC_POSTREAD);
1782b1d2c95eSMichal Meloun val = fis[2];
1783b1d2c95eSMichal Meloun bus_dmamap_sync(ch->dma.rfis_tag,
1784b1d2c95eSMichal Meloun ch->dma.rfis_map, BUS_DMASYNC_PREREAD);
1785b1d2c95eSMichal Meloun if ((val & ATA_S_BUSY) == 0)
1786232a9d55SAlexander Motin break;
1787b1d2c95eSMichal Meloun DELAY(10);
1788232a9d55SAlexander Motin }
1789232a9d55SAlexander Motin }
1790232a9d55SAlexander Motin
179152c9ce25SScott Long if (timeout && (count >= timeout)) {
1792fcd7f38fSAlexander Motin device_printf(ch->dev, "Poll timeout on slot %d port %d\n",
1793232a9d55SAlexander Motin slot->slot, port);
1794fcd7f38fSAlexander Motin device_printf(ch->dev, "is %08x cs %08x ss %08x "
17952d33cc76SAlexander Motin "rs %08x tfd %02x serr %08x cmd %08x\n",
179683c5d981SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_IS),
179783c5d981SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_CI),
179883c5d981SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots,
179983c5d981SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_TFD),
18002d33cc76SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_SERR),
18012d33cc76SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_CMD));
180252c9ce25SScott Long et = AHCI_ERR_TIMEOUT;
180352c9ce25SScott Long }
1804232a9d55SAlexander Motin
18050e6eb061SAlexander Motin /* Kick controller into sane state and enable FBS. */
1806232a9d55SAlexander Motin if (softreset == 2)
180708c8fde0SAlexander Motin ch->eslots |= (1 << slot->slot);
180808c8fde0SAlexander Motin ahci_end_transaction(slot, et);
180952c9ce25SScott Long return;
181052c9ce25SScott Long }
181152c9ce25SScott Long /* Start command execution timeout */
181285c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout / 2,
181365d2f9c1SJohn Baldwin 0, ahci_timeout, slot, 0);
181452c9ce25SScott Long return;
181552c9ce25SScott Long }
181652c9ce25SScott Long
1817b41cd539SAlexander Motin /* Must be called with channel locked. */
1818b41cd539SAlexander Motin static void
ahci_process_timeout(struct ahci_channel * ch)1819fcd7f38fSAlexander Motin ahci_process_timeout(struct ahci_channel *ch)
1820b41cd539SAlexander Motin {
1821b41cd539SAlexander Motin int i;
1822b41cd539SAlexander Motin
1823b41cd539SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED);
1824b41cd539SAlexander Motin /* Handle the rest of commands. */
1825b41cd539SAlexander Motin for (i = 0; i < ch->numslots; i++) {
1826b41cd539SAlexander Motin /* Do we have a running request on slot? */
1827b41cd539SAlexander Motin if (ch->slot[i].state < AHCI_SLOT_RUNNING)
1828b41cd539SAlexander Motin continue;
1829b41cd539SAlexander Motin ahci_end_transaction(&ch->slot[i], AHCI_ERR_TIMEOUT);
1830b41cd539SAlexander Motin }
1831b41cd539SAlexander Motin }
1832b41cd539SAlexander Motin
1833b41cd539SAlexander Motin /* Must be called with channel locked. */
1834b41cd539SAlexander Motin static void
ahci_rearm_timeout(struct ahci_channel * ch)1835fcd7f38fSAlexander Motin ahci_rearm_timeout(struct ahci_channel *ch)
1836b41cd539SAlexander Motin {
1837b41cd539SAlexander Motin int i;
1838b41cd539SAlexander Motin
1839b41cd539SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED);
1840b41cd539SAlexander Motin for (i = 0; i < ch->numslots; i++) {
1841b41cd539SAlexander Motin struct ahci_slot *slot = &ch->slot[i];
1842b41cd539SAlexander Motin
1843b41cd539SAlexander Motin /* Do we have a running request on slot? */
1844b41cd539SAlexander Motin if (slot->state < AHCI_SLOT_RUNNING)
1845b41cd539SAlexander Motin continue;
1846b41cd539SAlexander Motin if ((ch->toslots & (1 << i)) == 0)
1847b41cd539SAlexander Motin continue;
184885c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout,
184985c9dd9dSSteven Hartland SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
185065d2f9c1SJohn Baldwin ahci_timeout, slot, 0);
1851b41cd539SAlexander Motin }
1852b41cd539SAlexander Motin }
1853b41cd539SAlexander Motin
185452c9ce25SScott Long /* Locked by callout mechanism. */
185552c9ce25SScott Long static void
ahci_timeout(void * arg)185665d2f9c1SJohn Baldwin ahci_timeout(void *arg)
185752c9ce25SScott Long {
185865d2f9c1SJohn Baldwin struct ahci_slot *slot = arg;
1859fcd7f38fSAlexander Motin struct ahci_channel *ch = slot->ch;
1860fcd7f38fSAlexander Motin device_t dev = ch->dev;
1861a1aa38deSAlexander Motin uint32_t sstatus;
1862a1aa38deSAlexander Motin int ccs;
186352c9ce25SScott Long int i;
186452c9ce25SScott Long
18654dbabf10SAlexander Motin /* Check for stale timeout. */
1866a1aa38deSAlexander Motin if (slot->state < AHCI_SLOT_RUNNING)
18674dbabf10SAlexander Motin return;
18684dbabf10SAlexander Motin
1869a1aa38deSAlexander Motin /* Check if slot was not being executed last time we checked. */
1870a1aa38deSAlexander Motin if (slot->state < AHCI_SLOT_EXECUTING) {
1871a1aa38deSAlexander Motin /* Check if slot started executing. */
1872a1aa38deSAlexander Motin sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
1873a1aa38deSAlexander Motin ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK)
1874a1aa38deSAlexander Motin >> AHCI_P_CMD_CCS_SHIFT;
18750e6eb061SAlexander Motin if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot ||
18762d33cc76SAlexander Motin ch->fbs_enabled || ch->wrongccs)
1877a1aa38deSAlexander Motin slot->state = AHCI_SLOT_EXECUTING;
18782d33cc76SAlexander Motin else if ((ch->rslots & (1 << ccs)) == 0) {
18792d33cc76SAlexander Motin ch->wrongccs = 1;
18802d33cc76SAlexander Motin slot->state = AHCI_SLOT_EXECUTING;
18812d33cc76SAlexander Motin }
1882a1aa38deSAlexander Motin
188385c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout,
188485c9dd9dSSteven Hartland SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
188565d2f9c1SJohn Baldwin ahci_timeout, slot, 0);
1886a1aa38deSAlexander Motin return;
1887a1aa38deSAlexander Motin }
1888a1aa38deSAlexander Motin
1889232a9d55SAlexander Motin device_printf(dev, "Timeout on slot %d port %d\n",
1890232a9d55SAlexander Motin slot->slot, slot->ccb->ccb_h.target_id & 0x0f);
18912d33cc76SAlexander Motin device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x "
18922d33cc76SAlexander Motin "serr %08x cmd %08x\n",
1893a1aa38deSAlexander Motin ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI),
1894a1aa38deSAlexander Motin ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots,
18952d33cc76SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR),
18962d33cc76SAlexander Motin ATA_INL(ch->r_mem, AHCI_P_CMD));
189752c9ce25SScott Long
1898660d482aSAlexander Motin /* Handle frozen command. */
189952c9ce25SScott Long if (ch->frozen) {
190052c9ce25SScott Long union ccb *fccb = ch->frozen;
190152c9ce25SScott Long ch->frozen = NULL;
190252c9ce25SScott Long fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1903a1aa38deSAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1904a1aa38deSAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1);
1905a1aa38deSAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN;
1906a1aa38deSAlexander Motin }
1907227d67aaSAlexander Motin ahci_done(ch, fccb);
190852c9ce25SScott Long }
19092d33cc76SAlexander Motin if (!ch->fbs_enabled && !ch->wrongccs) {
1910b41cd539SAlexander Motin /* Without FBS we know real timeout source. */
1911b41cd539SAlexander Motin ch->fatalerr = 1;
1912660d482aSAlexander Motin /* Handle command with timeout. */
1913660d482aSAlexander Motin ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT);
1914660d482aSAlexander Motin /* Handle the rest of commands. */
191552c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
191652c9ce25SScott Long /* Do we have a running request on slot? */
191752c9ce25SScott Long if (ch->slot[i].state < AHCI_SLOT_RUNNING)
191852c9ce25SScott Long continue;
191952c9ce25SScott Long ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
192052c9ce25SScott Long }
1921b41cd539SAlexander Motin } else {
1922b41cd539SAlexander Motin /* With FBS we wait for other commands timeout and pray. */
1923b41cd539SAlexander Motin if (ch->toslots == 0)
1924b41cd539SAlexander Motin xpt_freeze_simq(ch->sim, 1);
1925b41cd539SAlexander Motin ch->toslots |= (1 << slot->slot);
1926b41cd539SAlexander Motin if ((ch->rslots & ~ch->toslots) == 0)
1927fcd7f38fSAlexander Motin ahci_process_timeout(ch);
1928b41cd539SAlexander Motin else
1929b41cd539SAlexander Motin device_printf(dev, " ... waiting for slots %08x\n",
1930b41cd539SAlexander Motin ch->rslots & ~ch->toslots);
1931b41cd539SAlexander Motin }
193252c9ce25SScott Long }
193352c9ce25SScott Long
193452c9ce25SScott Long /* Must be called with channel locked. */
193552c9ce25SScott Long static void
ahci_end_transaction(struct ahci_slot * slot,enum ahci_err_type et)193652c9ce25SScott Long ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et)
193752c9ce25SScott Long {
1938fcd7f38fSAlexander Motin struct ahci_channel *ch = slot->ch;
193952c9ce25SScott Long union ccb *ccb = slot->ccb;
1940ba3a9995SAlexander Motin struct ahci_cmd_list *clp;
1941bf12976cSAlexander Motin int lastto;
1942232a9d55SAlexander Motin uint32_t sig;
194352c9ce25SScott Long
194452c9ce25SScott Long bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1945ba3a9995SAlexander Motin BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1946ba3a9995SAlexander Motin clp = (struct ahci_cmd_list *)
1947ba3a9995SAlexander Motin (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot));
194852c9ce25SScott Long /* Read result registers to the result struct
194952c9ce25SScott Long * May be incorrect if several commands finished same time,
195052c9ce25SScott Long * so read only when sure or have to.
195152c9ce25SScott Long */
195252c9ce25SScott Long if (ccb->ccb_h.func_code == XPT_ATA_IO) {
195352c9ce25SScott Long struct ata_res *res = &ccb->ataio.res;
195452c9ce25SScott Long
195552c9ce25SScott Long if ((et == AHCI_ERR_TFE) ||
195652c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
195752c9ce25SScott Long u_int8_t *fis = ch->dma.rfis + 0x40;
195852c9ce25SScott Long
195952c9ce25SScott Long bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
196052c9ce25SScott Long BUS_DMASYNC_POSTREAD);
19610e6eb061SAlexander Motin if (ch->fbs_enabled) {
19620e6eb061SAlexander Motin fis += ccb->ccb_h.target_id * 256;
19630e6eb061SAlexander Motin res->status = fis[2];
19640e6eb061SAlexander Motin res->error = fis[3];
19650e6eb061SAlexander Motin } else {
19660e6eb061SAlexander Motin uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD);
19670e6eb061SAlexander Motin
196852c9ce25SScott Long res->status = tfd;
196952c9ce25SScott Long res->error = tfd >> 8;
19700e6eb061SAlexander Motin }
197152c9ce25SScott Long res->lba_low = fis[4];
197252c9ce25SScott Long res->lba_mid = fis[5];
197352c9ce25SScott Long res->lba_high = fis[6];
197452c9ce25SScott Long res->device = fis[7];
197552c9ce25SScott Long res->lba_low_exp = fis[8];
197652c9ce25SScott Long res->lba_mid_exp = fis[9];
197752c9ce25SScott Long res->lba_high_exp = fis[10];
197852c9ce25SScott Long res->sector_count = fis[12];
197952c9ce25SScott Long res->sector_count_exp = fis[13];
1980232a9d55SAlexander Motin
1981232a9d55SAlexander Motin /*
1982232a9d55SAlexander Motin * Some weird controllers do not return signature in
1983232a9d55SAlexander Motin * FIS receive area. Read it from PxSIG register.
1984232a9d55SAlexander Motin */
1985232a9d55SAlexander Motin if ((ch->quirks & AHCI_Q_ALTSIG) &&
1986232a9d55SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1987232a9d55SAlexander Motin (ccb->ataio.cmd.control & ATA_A_RESET) == 0) {
1988232a9d55SAlexander Motin sig = ATA_INL(ch->r_mem, AHCI_P_SIG);
1989232a9d55SAlexander Motin res->lba_high = sig >> 24;
1990232a9d55SAlexander Motin res->lba_mid = sig >> 16;
1991232a9d55SAlexander Motin res->lba_low = sig >> 8;
1992232a9d55SAlexander Motin res->sector_count = sig;
1993232a9d55SAlexander Motin }
199452c9ce25SScott Long } else
199552c9ce25SScott Long bzero(res, sizeof(*res));
1996ba3a9995SAlexander Motin if ((ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) == 0 &&
1997a7c797f0SAlexander Motin (ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1998a7c797f0SAlexander Motin (ch->quirks & AHCI_Q_NOCOUNT) == 0) {
1999ba3a9995SAlexander Motin ccb->ataio.resid =
2000ba3a9995SAlexander Motin ccb->ataio.dxfer_len - le32toh(clp->bytecount);
2001ba3a9995SAlexander Motin }
2002ba3a9995SAlexander Motin } else {
2003a7c797f0SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
2004a7c797f0SAlexander Motin (ch->quirks & AHCI_Q_NOCOUNT) == 0) {
2005ba3a9995SAlexander Motin ccb->csio.resid =
2006ba3a9995SAlexander Motin ccb->csio.dxfer_len - le32toh(clp->bytecount);
2007ba3a9995SAlexander Motin }
200852c9ce25SScott Long }
200952c9ce25SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
201052c9ce25SScott Long bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
201152c9ce25SScott Long (ccb->ccb_h.flags & CAM_DIR_IN) ?
201252c9ce25SScott Long BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
201352c9ce25SScott Long bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
201452c9ce25SScott Long }
20150e6eb061SAlexander Motin if (et != AHCI_ERR_NONE)
20160e6eb061SAlexander Motin ch->eslots |= (1 << slot->slot);
2017a1aa38deSAlexander Motin /* In case of error, freeze device for proper recovery. */
20186bbd332eSAlexander Motin if ((et != AHCI_ERR_NONE) && (!ch->recoverycmd) &&
2019a1aa38deSAlexander Motin !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
2020a1aa38deSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
2021a1aa38deSAlexander Motin ccb->ccb_h.status |= CAM_DEV_QFRZN;
2022a1aa38deSAlexander Motin }
202352c9ce25SScott Long /* Set proper result status. */
202452c9ce25SScott Long ccb->ccb_h.status &= ~CAM_STATUS_MASK;
202552c9ce25SScott Long switch (et) {
202652c9ce25SScott Long case AHCI_ERR_NONE:
202752c9ce25SScott Long ccb->ccb_h.status |= CAM_REQ_CMP;
202852c9ce25SScott Long if (ccb->ccb_h.func_code == XPT_SCSI_IO)
202952c9ce25SScott Long ccb->csio.scsi_status = SCSI_STATUS_OK;
203052c9ce25SScott Long break;
203152c9ce25SScott Long case AHCI_ERR_INVALID:
2032a69552e4SAlexander Motin ch->fatalerr = 1;
203352c9ce25SScott Long ccb->ccb_h.status |= CAM_REQ_INVALID;
203452c9ce25SScott Long break;
203552c9ce25SScott Long case AHCI_ERR_INNOCENT:
203652c9ce25SScott Long ccb->ccb_h.status |= CAM_REQUEUE_REQ;
203752c9ce25SScott Long break;
203852c9ce25SScott Long case AHCI_ERR_TFE:
2039a1aa38deSAlexander Motin case AHCI_ERR_NCQ:
204052c9ce25SScott Long if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
204152c9ce25SScott Long ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
204252c9ce25SScott Long ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
204352c9ce25SScott Long } else {
204452c9ce25SScott Long ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
204552c9ce25SScott Long }
204652c9ce25SScott Long break;
204752c9ce25SScott Long case AHCI_ERR_SATA:
2048a69552e4SAlexander Motin ch->fatalerr = 1;
20496bbd332eSAlexander Motin if (!ch->recoverycmd) {
2050a1aa38deSAlexander Motin xpt_freeze_simq(ch->sim, 1);
2051a1aa38deSAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2052a1aa38deSAlexander Motin ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2053a1aa38deSAlexander Motin }
205452c9ce25SScott Long ccb->ccb_h.status |= CAM_UNCOR_PARITY;
205552c9ce25SScott Long break;
205652c9ce25SScott Long case AHCI_ERR_TIMEOUT:
20576bbd332eSAlexander Motin if (!ch->recoverycmd) {
2058a1aa38deSAlexander Motin xpt_freeze_simq(ch->sim, 1);
2059a1aa38deSAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2060a1aa38deSAlexander Motin ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2061a1aa38deSAlexander Motin }
206252c9ce25SScott Long ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
206352c9ce25SScott Long break;
206452c9ce25SScott Long default:
2065a69552e4SAlexander Motin ch->fatalerr = 1;
206652c9ce25SScott Long ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
206752c9ce25SScott Long }
206852c9ce25SScott Long /* Free slot. */
2069c8039fc6SAlexander Motin ch->oslots &= ~(1 << slot->slot);
207052c9ce25SScott Long ch->rslots &= ~(1 << slot->slot);
207152c9ce25SScott Long ch->aslots &= ~(1 << slot->slot);
207252c9ce25SScott Long slot->state = AHCI_SLOT_EMPTY;
207352c9ce25SScott Long slot->ccb = NULL;
207452c9ce25SScott Long /* Update channel stats. */
207552c9ce25SScott Long ch->numrslots--;
20760e6eb061SAlexander Motin ch->numrslotspd[ccb->ccb_h.target_id]--;
207752c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
207852c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
207952c9ce25SScott Long ch->numtslots--;
20800e6eb061SAlexander Motin ch->numtslotspd[ccb->ccb_h.target_id]--;
208152c9ce25SScott Long }
2082bf12976cSAlexander Motin /* Cancel timeout state if request completed normally. */
2083bf12976cSAlexander Motin if (et != AHCI_ERR_TIMEOUT) {
2084bf12976cSAlexander Motin lastto = (ch->toslots == (1 << slot->slot));
2085bf12976cSAlexander Motin ch->toslots &= ~(1 << slot->slot);
2086bf12976cSAlexander Motin if (lastto)
2087bf12976cSAlexander Motin xpt_release_simq(ch->sim, TRUE);
2088bf12976cSAlexander Motin }
208952c9ce25SScott Long /* If it was first request of reset sequence and there is no error,
209052c9ce25SScott Long * proceed to second request. */
209152c9ce25SScott Long if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
209252c9ce25SScott Long (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
209352c9ce25SScott Long (ccb->ataio.cmd.control & ATA_A_RESET) &&
209452c9ce25SScott Long et == AHCI_ERR_NONE) {
209552c9ce25SScott Long ccb->ataio.cmd.control &= ~ATA_A_RESET;
2096fcd7f38fSAlexander Motin ahci_begin_transaction(ch, ccb);
209752c9ce25SScott Long return;
209852c9ce25SScott Long }
2099a69552e4SAlexander Motin /* If it was our READ LOG command - process it. */
21006bbd332eSAlexander Motin if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
2101fcd7f38fSAlexander Motin ahci_process_read_log(ch, ccb);
21026bbd332eSAlexander Motin /* If it was our REQUEST SENSE command - process it. */
21036bbd332eSAlexander Motin } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
2104fcd7f38fSAlexander Motin ahci_process_request_sense(ch, ccb);
21056bbd332eSAlexander Motin /* If it was NCQ or ATAPI command error, put result on hold. */
21066bbd332eSAlexander Motin } else if (et == AHCI_ERR_NCQ ||
21076bbd332eSAlexander Motin ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
21086bbd332eSAlexander Motin (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
2109a69552e4SAlexander Motin ch->hold[slot->slot] = ccb;
21100e6eb061SAlexander Motin ch->numhslots++;
2111a69552e4SAlexander Motin } else
2112227d67aaSAlexander Motin ahci_done(ch, ccb);
2113a69552e4SAlexander Motin /* If we have no other active commands, ... */
2114a69552e4SAlexander Motin if (ch->rslots == 0) {
2115a69552e4SAlexander Motin /* if there was fatal error - reset port. */
2116b41cd539SAlexander Motin if (ch->toslots != 0 || ch->fatalerr) {
2117fcd7f38fSAlexander Motin ahci_reset(ch);
21180e6eb061SAlexander Motin } else {
21190e6eb061SAlexander Motin /* if we have slots in error, we can reinit port. */
21200e6eb061SAlexander Motin if (ch->eslots != 0) {
2121fcd7f38fSAlexander Motin ahci_stop(ch);
2122fcd7f38fSAlexander Motin ahci_clo(ch);
2123fcd7f38fSAlexander Motin ahci_start(ch, 1);
21240e6eb061SAlexander Motin }
21250e6eb061SAlexander Motin /* if there commands on hold, we can do READ LOG. */
21266bbd332eSAlexander Motin if (!ch->recoverycmd && ch->numhslots)
2127fcd7f38fSAlexander Motin ahci_issue_recovery(ch);
2128a69552e4SAlexander Motin }
2129b41cd539SAlexander Motin /* If all the rest of commands are in timeout - give them chance. */
2130b41cd539SAlexander Motin } else if ((ch->rslots & ~ch->toslots) == 0 &&
2131b41cd539SAlexander Motin et != AHCI_ERR_TIMEOUT)
2132fcd7f38fSAlexander Motin ahci_rearm_timeout(ch);
213308c8fde0SAlexander Motin /* Unfreeze frozen command. */
2134fcd7f38fSAlexander Motin if (ch->frozen && !ahci_check_collision(ch, ch->frozen)) {
213508c8fde0SAlexander Motin union ccb *fccb = ch->frozen;
213608c8fde0SAlexander Motin ch->frozen = NULL;
2137fcd7f38fSAlexander Motin ahci_begin_transaction(ch, fccb);
213808c8fde0SAlexander Motin xpt_release_simq(ch->sim, TRUE);
213908c8fde0SAlexander Motin }
21404dbabf10SAlexander Motin /* Start PM timer. */
2141da6808c1SAlexander Motin if (ch->numrslots == 0 && ch->pm_level > 3 &&
2142da6808c1SAlexander Motin (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) {
21434dbabf10SAlexander Motin callout_schedule(&ch->pm_timer,
21444dbabf10SAlexander Motin (ch->pm_level == 4) ? hz / 1000 : hz / 8);
21454dbabf10SAlexander Motin }
214652c9ce25SScott Long }
214752c9ce25SScott Long
214852c9ce25SScott Long static void
ahci_issue_recovery(struct ahci_channel * ch)2149fcd7f38fSAlexander Motin ahci_issue_recovery(struct ahci_channel *ch)
215052c9ce25SScott Long {
215152c9ce25SScott Long union ccb *ccb;
215252c9ce25SScott Long struct ccb_ataio *ataio;
21536bbd332eSAlexander Motin struct ccb_scsiio *csio;
215452c9ce25SScott Long int i;
215552c9ce25SScott Long
21567bcc5957SAlexander Motin /* Find some held command. */
215752c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
215852c9ce25SScott Long if (ch->hold[i])
215952c9ce25SScott Long break;
216052c9ce25SScott Long }
216152c9ce25SScott Long ccb = xpt_alloc_ccb_nowait();
216252c9ce25SScott Long if (ccb == NULL) {
2163fcd7f38fSAlexander Motin device_printf(ch->dev, "Unable to allocate recovery command\n");
21646ac0befdSAlexander Motin completeall:
21657bcc5957SAlexander Motin /* We can't do anything -- complete held commands. */
21666ac0befdSAlexander Motin for (i = 0; i < ch->numslots; i++) {
21676ac0befdSAlexander Motin if (ch->hold[i] == NULL)
21686ac0befdSAlexander Motin continue;
21696ac0befdSAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
21706ac0befdSAlexander Motin ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL;
2171227d67aaSAlexander Motin ahci_done(ch, ch->hold[i]);
21726ac0befdSAlexander Motin ch->hold[i] = NULL;
21736ac0befdSAlexander Motin ch->numhslots--;
21746ac0befdSAlexander Motin }
2175fcd7f38fSAlexander Motin ahci_reset(ch);
21766ac0befdSAlexander Motin return;
217752c9ce25SScott Long }
217825375b14SAlexander Motin xpt_setup_ccb(&ccb->ccb_h, ch->hold[i]->ccb_h.path,
217925375b14SAlexander Motin ch->hold[i]->ccb_h.pinfo.priority);
218087085c12SAlexander Motin if (ch->hold[i]->ccb_h.func_code == XPT_ATA_IO) {
21816bbd332eSAlexander Motin /* READ LOG */
21826bbd332eSAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
218352c9ce25SScott Long ccb->ccb_h.func_code = XPT_ATA_IO;
218452c9ce25SScott Long ccb->ccb_h.flags = CAM_DIR_IN;
218552c9ce25SScott Long ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
218652c9ce25SScott Long ataio = &ccb->ataio;
218752c9ce25SScott Long ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT);
218852c9ce25SScott Long if (ataio->data_ptr == NULL) {
2189dc5a530bSAlexander Motin xpt_free_ccb(ccb);
2190fcd7f38fSAlexander Motin device_printf(ch->dev,
21917bcc5957SAlexander Motin "Unable to allocate memory for READ LOG command\n");
21926ac0befdSAlexander Motin goto completeall;
219352c9ce25SScott Long }
219452c9ce25SScott Long ataio->dxfer_len = 512;
219552c9ce25SScott Long bzero(&ataio->cmd, sizeof(ataio->cmd));
219652c9ce25SScott Long ataio->cmd.flags = CAM_ATAIO_48BIT;
219752c9ce25SScott Long ataio->cmd.command = 0x2F; /* READ LOG EXT */
219852c9ce25SScott Long ataio->cmd.sector_count = 1;
219952c9ce25SScott Long ataio->cmd.sector_count_exp = 0;
220052c9ce25SScott Long ataio->cmd.lba_low = 0x10;
220152c9ce25SScott Long ataio->cmd.lba_mid = 0;
220252c9ce25SScott Long ataio->cmd.lba_mid_exp = 0;
22036bbd332eSAlexander Motin } else {
22046bbd332eSAlexander Motin /* REQUEST SENSE */
22056bbd332eSAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
22066bbd332eSAlexander Motin ccb->ccb_h.recovery_slot = i;
22076bbd332eSAlexander Motin ccb->ccb_h.func_code = XPT_SCSI_IO;
22086bbd332eSAlexander Motin ccb->ccb_h.flags = CAM_DIR_IN;
22096bbd332eSAlexander Motin ccb->ccb_h.status = 0;
22106bbd332eSAlexander Motin ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
22116bbd332eSAlexander Motin csio = &ccb->csio;
22126bbd332eSAlexander Motin csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
22136bbd332eSAlexander Motin csio->dxfer_len = ch->hold[i]->csio.sense_len;
22146bbd332eSAlexander Motin csio->cdb_len = 6;
22156bbd332eSAlexander Motin bzero(&csio->cdb_io, sizeof(csio->cdb_io));
22166bbd332eSAlexander Motin csio->cdb_io.cdb_bytes[0] = 0x03;
22176bbd332eSAlexander Motin csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
22186bbd332eSAlexander Motin }
22196bbd332eSAlexander Motin /* Freeze SIM while doing recovery. */
22206ac0befdSAlexander Motin ch->recoverycmd = 1;
2221a1aa38deSAlexander Motin xpt_freeze_simq(ch->sim, 1);
2222fcd7f38fSAlexander Motin ahci_begin_transaction(ch, ccb);
222352c9ce25SScott Long }
222452c9ce25SScott Long
222552c9ce25SScott Long static void
ahci_process_read_log(struct ahci_channel * ch,union ccb * ccb)2226fcd7f38fSAlexander Motin ahci_process_read_log(struct ahci_channel *ch, union ccb *ccb)
222752c9ce25SScott Long {
222852c9ce25SScott Long uint8_t *data;
222952c9ce25SScott Long struct ata_res *res;
223052c9ce25SScott Long int i;
223152c9ce25SScott Long
22326bbd332eSAlexander Motin ch->recoverycmd = 0;
223352c9ce25SScott Long
223452c9ce25SScott Long data = ccb->ataio.data_ptr;
223552c9ce25SScott Long if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
223652c9ce25SScott Long (data[0] & 0x80) == 0) {
223752c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
223852c9ce25SScott Long if (!ch->hold[i])
223952c9ce25SScott Long continue;
22406bbd332eSAlexander Motin if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
22416bbd332eSAlexander Motin continue;
224252c9ce25SScott Long if ((data[0] & 0x1F) == i) {
224352c9ce25SScott Long res = &ch->hold[i]->ataio.res;
224452c9ce25SScott Long res->status = data[2];
224552c9ce25SScott Long res->error = data[3];
224652c9ce25SScott Long res->lba_low = data[4];
224752c9ce25SScott Long res->lba_mid = data[5];
224852c9ce25SScott Long res->lba_high = data[6];
224952c9ce25SScott Long res->device = data[7];
225052c9ce25SScott Long res->lba_low_exp = data[8];
225152c9ce25SScott Long res->lba_mid_exp = data[9];
225252c9ce25SScott Long res->lba_high_exp = data[10];
225352c9ce25SScott Long res->sector_count = data[12];
225452c9ce25SScott Long res->sector_count_exp = data[13];
225552c9ce25SScott Long } else {
225652c9ce25SScott Long ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
225752c9ce25SScott Long ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
225852c9ce25SScott Long }
2259227d67aaSAlexander Motin ahci_done(ch, ch->hold[i]);
226052c9ce25SScott Long ch->hold[i] = NULL;
22610e6eb061SAlexander Motin ch->numhslots--;
226252c9ce25SScott Long }
226352c9ce25SScott Long } else {
226452c9ce25SScott Long if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2265fcd7f38fSAlexander Motin device_printf(ch->dev, "Error while READ LOG EXT\n");
226652c9ce25SScott Long else if ((data[0] & 0x80) == 0) {
2267fcd7f38fSAlexander Motin device_printf(ch->dev, "Non-queued command error in READ LOG EXT\n");
226852c9ce25SScott Long }
226952c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
227052c9ce25SScott Long if (!ch->hold[i])
227152c9ce25SScott Long continue;
22726bbd332eSAlexander Motin if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
22736bbd332eSAlexander Motin continue;
2274227d67aaSAlexander Motin ahci_done(ch, ch->hold[i]);
227552c9ce25SScott Long ch->hold[i] = NULL;
22760e6eb061SAlexander Motin ch->numhslots--;
227752c9ce25SScott Long }
227852c9ce25SScott Long }
227952c9ce25SScott Long free(ccb->ataio.data_ptr, M_AHCI);
228052c9ce25SScott Long xpt_free_ccb(ccb);
2281a1aa38deSAlexander Motin xpt_release_simq(ch->sim, TRUE);
228252c9ce25SScott Long }
228352c9ce25SScott Long
228452c9ce25SScott Long static void
ahci_process_request_sense(struct ahci_channel * ch,union ccb * ccb)2285fcd7f38fSAlexander Motin ahci_process_request_sense(struct ahci_channel *ch, union ccb *ccb)
22866bbd332eSAlexander Motin {
22876bbd332eSAlexander Motin int i;
22886bbd332eSAlexander Motin
22896bbd332eSAlexander Motin ch->recoverycmd = 0;
22906bbd332eSAlexander Motin
22916bbd332eSAlexander Motin i = ccb->ccb_h.recovery_slot;
22926bbd332eSAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
22936bbd332eSAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
22946bbd332eSAlexander Motin } else {
22956bbd332eSAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
22966bbd332eSAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
22976bbd332eSAlexander Motin }
2298227d67aaSAlexander Motin ahci_done(ch, ch->hold[i]);
22996bbd332eSAlexander Motin ch->hold[i] = NULL;
23006bbd332eSAlexander Motin ch->numhslots--;
23016bbd332eSAlexander Motin xpt_free_ccb(ccb);
23026bbd332eSAlexander Motin xpt_release_simq(ch->sim, TRUE);
23036bbd332eSAlexander Motin }
23046bbd332eSAlexander Motin
23056bbd332eSAlexander Motin static void
ahci_start(struct ahci_channel * ch,int fbs)2306fcd7f38fSAlexander Motin ahci_start(struct ahci_channel *ch, int fbs)
230752c9ce25SScott Long {
230852c9ce25SScott Long u_int32_t cmd;
230952c9ce25SScott Long
23107ec8c789SLuiz Otavio O Souza /* Run the channel start callback, if any. */
23117ec8c789SLuiz Otavio O Souza if (ch->start)
23127ec8c789SLuiz Otavio O Souza ch->start(ch);
23137ec8c789SLuiz Otavio O Souza
231452c9ce25SScott Long /* Clear SATA error register */
231552c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF);
231652c9ce25SScott Long /* Clear any interrupts pending on this channel */
231752c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF);
23180e6eb061SAlexander Motin /* Configure FIS-based switching if supported. */
23190e6eb061SAlexander Motin if (ch->chcaps & AHCI_P_CMD_FBSCP) {
23200e6eb061SAlexander Motin ch->fbs_enabled = (fbs && ch->pm_present) ? 1 : 0;
23210e6eb061SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_FBS,
23220e6eb061SAlexander Motin ch->fbs_enabled ? AHCI_P_FBS_EN : 0);
23230e6eb061SAlexander Motin }
232452c9ce25SScott Long /* Start operations on this channel */
232552c9ce25SScott Long cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
232689f2835fSAlexander Motin cmd &= ~AHCI_P_CMD_PMA;
232752c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST |
232852c9ce25SScott Long (ch->pm_present ? AHCI_P_CMD_PMA : 0));
232952c9ce25SScott Long }
233052c9ce25SScott Long
233152c9ce25SScott Long static void
ahci_stop(struct ahci_channel * ch)2332fcd7f38fSAlexander Motin ahci_stop(struct ahci_channel *ch)
233352c9ce25SScott Long {
233452c9ce25SScott Long u_int32_t cmd;
233552c9ce25SScott Long int timeout;
233652c9ce25SScott Long
233752c9ce25SScott Long /* Kill all activity on this channel */
233852c9ce25SScott Long cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
233952c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST);
234052c9ce25SScott Long /* Wait for activity stop. */
234152c9ce25SScott Long timeout = 0;
234252c9ce25SScott Long do {
23433a3d820bSAlexander Motin DELAY(10);
23443a3d820bSAlexander Motin if (timeout++ > 50000) {
2345fcd7f38fSAlexander Motin device_printf(ch->dev, "stopping AHCI engine failed\n");
234652c9ce25SScott Long break;
234752c9ce25SScott Long }
234852c9ce25SScott Long } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR);
23490e6eb061SAlexander Motin ch->eslots = 0;
235052c9ce25SScott Long }
235152c9ce25SScott Long
235252c9ce25SScott Long static void
ahci_clo(struct ahci_channel * ch)2353fcd7f38fSAlexander Motin ahci_clo(struct ahci_channel *ch)
235452c9ce25SScott Long {
235552c9ce25SScott Long u_int32_t cmd;
235652c9ce25SScott Long int timeout;
235752c9ce25SScott Long
235852c9ce25SScott Long /* Issue Command List Override if supported */
235952c9ce25SScott Long if (ch->caps & AHCI_CAP_SCLO) {
236052c9ce25SScott Long cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
236152c9ce25SScott Long cmd |= AHCI_P_CMD_CLO;
236252c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd);
236352c9ce25SScott Long timeout = 0;
236452c9ce25SScott Long do {
23653a3d820bSAlexander Motin DELAY(10);
23663a3d820bSAlexander Motin if (timeout++ > 50000) {
2367fcd7f38fSAlexander Motin device_printf(ch->dev, "executing CLO failed\n");
236852c9ce25SScott Long break;
236952c9ce25SScott Long }
237052c9ce25SScott Long } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO);
237152c9ce25SScott Long }
237252c9ce25SScott Long }
237352c9ce25SScott Long
237452c9ce25SScott Long static void
ahci_stop_fr(struct ahci_channel * ch)2375fcd7f38fSAlexander Motin ahci_stop_fr(struct ahci_channel *ch)
237652c9ce25SScott Long {
237752c9ce25SScott Long u_int32_t cmd;
237852c9ce25SScott Long int timeout;
237952c9ce25SScott Long
238052c9ce25SScott Long /* Kill all FIS reception on this channel */
238152c9ce25SScott Long cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
238252c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE);
238352c9ce25SScott Long /* Wait for FIS reception stop. */
238452c9ce25SScott Long timeout = 0;
238552c9ce25SScott Long do {
23863a3d820bSAlexander Motin DELAY(10);
23873a3d820bSAlexander Motin if (timeout++ > 50000) {
2388fcd7f38fSAlexander Motin device_printf(ch->dev, "stopping AHCI FR engine failed\n");
238952c9ce25SScott Long break;
239052c9ce25SScott Long }
239152c9ce25SScott Long } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR);
239252c9ce25SScott Long }
239352c9ce25SScott Long
239452c9ce25SScott Long static void
ahci_start_fr(struct ahci_channel * ch)2395fcd7f38fSAlexander Motin ahci_start_fr(struct ahci_channel *ch)
239652c9ce25SScott Long {
239752c9ce25SScott Long u_int32_t cmd;
239852c9ce25SScott Long
239952c9ce25SScott Long /* Start FIS reception on this channel */
240052c9ce25SScott Long cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
240152c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE);
240252c9ce25SScott Long }
240352c9ce25SScott Long
240452c9ce25SScott Long static int
ahci_wait_ready(struct ahci_channel * ch,int t,int t0)2405fcd7f38fSAlexander Motin ahci_wait_ready(struct ahci_channel *ch, int t, int t0)
240652c9ce25SScott Long {
240752c9ce25SScott Long int timeout = 0;
240852c9ce25SScott Long uint32_t val;
240952c9ce25SScott Long
241052c9ce25SScott Long while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) &
241152c9ce25SScott Long (ATA_S_BUSY | ATA_S_DRQ)) {
2412df1439e3SAlexander Motin if (timeout > t) {
2413df1439e3SAlexander Motin if (t != 0) {
2414fcd7f38fSAlexander Motin device_printf(ch->dev,
2415df1439e3SAlexander Motin "AHCI reset: device not ready after %dms "
2416df1439e3SAlexander Motin "(tfd = %08x)\n",
2417df1439e3SAlexander Motin MAX(t, 0) + t0, val);
2418df1439e3SAlexander Motin }
241952c9ce25SScott Long return (EBUSY);
242052c9ce25SScott Long }
2421df1439e3SAlexander Motin DELAY(1000);
2422df1439e3SAlexander Motin timeout++;
242352c9ce25SScott Long }
242452c9ce25SScott Long if (bootverbose)
2425fcd7f38fSAlexander Motin device_printf(ch->dev, "AHCI reset: device ready after %dms\n",
2426df1439e3SAlexander Motin timeout + t0);
242752c9ce25SScott Long return (0);
242852c9ce25SScott Long }
242952c9ce25SScott Long
243052c9ce25SScott Long static void
ahci_reset_to(void * arg)2431df1439e3SAlexander Motin ahci_reset_to(void *arg)
2432df1439e3SAlexander Motin {
2433fcd7f38fSAlexander Motin struct ahci_channel *ch = arg;
2434df1439e3SAlexander Motin
2435df1439e3SAlexander Motin if (ch->resetting == 0)
2436df1439e3SAlexander Motin return;
2437df1439e3SAlexander Motin ch->resetting--;
2438fcd7f38fSAlexander Motin if (ahci_wait_ready(ch, ch->resetting == 0 ? -1 : 0,
2439df1439e3SAlexander Motin (310 - ch->resetting) * 100) == 0) {
2440df1439e3SAlexander Motin ch->resetting = 0;
2441fcd7f38fSAlexander Motin ahci_start(ch, 1);
2442df1439e3SAlexander Motin xpt_release_simq(ch->sim, TRUE);
2443df1439e3SAlexander Motin return;
2444df1439e3SAlexander Motin }
2445df1439e3SAlexander Motin if (ch->resetting == 0) {
2446fcd7f38fSAlexander Motin ahci_clo(ch);
2447fcd7f38fSAlexander Motin ahci_start(ch, 1);
2448df1439e3SAlexander Motin xpt_release_simq(ch->sim, TRUE);
2449df1439e3SAlexander Motin return;
2450df1439e3SAlexander Motin }
2451df1439e3SAlexander Motin callout_schedule(&ch->reset_timer, hz / 10);
2452df1439e3SAlexander Motin }
2453df1439e3SAlexander Motin
2454df1439e3SAlexander Motin static void
ahci_reset(struct ahci_channel * ch)2455fcd7f38fSAlexander Motin ahci_reset(struct ahci_channel *ch)
245652c9ce25SScott Long {
2457fcd7f38fSAlexander Motin struct ahci_controller *ctlr = device_get_softc(device_get_parent(ch->dev));
245852c9ce25SScott Long int i;
245952c9ce25SScott Long
246083c5d981SAlexander Motin xpt_freeze_simq(ch->sim, 1);
246152c9ce25SScott Long if (bootverbose)
2462fcd7f38fSAlexander Motin device_printf(ch->dev, "AHCI reset...\n");
2463df1439e3SAlexander Motin /* Forget about previous reset. */
2464df1439e3SAlexander Motin if (ch->resetting) {
2465df1439e3SAlexander Motin ch->resetting = 0;
2466df1439e3SAlexander Motin callout_stop(&ch->reset_timer);
2467df1439e3SAlexander Motin xpt_release_simq(ch->sim, TRUE);
2468df1439e3SAlexander Motin }
246952c9ce25SScott Long /* Requeue freezed command. */
247052c9ce25SScott Long if (ch->frozen) {
247152c9ce25SScott Long union ccb *fccb = ch->frozen;
247252c9ce25SScott Long ch->frozen = NULL;
247352c9ce25SScott Long fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
2474a1aa38deSAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
2475a1aa38deSAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1);
2476a1aa38deSAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN;
2477a1aa38deSAlexander Motin }
2478227d67aaSAlexander Motin ahci_done(ch, fccb);
247952c9ce25SScott Long }
248052c9ce25SScott Long /* Kill the engine and requeue all running commands. */
2481fcd7f38fSAlexander Motin ahci_stop(ch);
248252c9ce25SScott Long for (i = 0; i < ch->numslots; i++) {
248352c9ce25SScott Long /* Do we have a running request on slot? */
248452c9ce25SScott Long if (ch->slot[i].state < AHCI_SLOT_RUNNING)
248552c9ce25SScott Long continue;
248652c9ce25SScott Long /* XXX; Commands in loading state. */
248752c9ce25SScott Long ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
248852c9ce25SScott Long }
2489a69552e4SAlexander Motin for (i = 0; i < ch->numslots; i++) {
2490a69552e4SAlexander Motin if (!ch->hold[i])
2491a69552e4SAlexander Motin continue;
2492227d67aaSAlexander Motin ahci_done(ch, ch->hold[i]);
2493a69552e4SAlexander Motin ch->hold[i] = NULL;
24940e6eb061SAlexander Motin ch->numhslots--;
2495a69552e4SAlexander Motin }
2496b41cd539SAlexander Motin if (ch->toslots != 0)
2497b41cd539SAlexander Motin xpt_release_simq(ch->sim, TRUE);
24980e6eb061SAlexander Motin ch->eslots = 0;
2499b41cd539SAlexander Motin ch->toslots = 0;
25002d33cc76SAlexander Motin ch->wrongccs = 0;
2501a69552e4SAlexander Motin ch->fatalerr = 0;
2502a1aa38deSAlexander Motin /* Tell the XPT about the event */
2503a1aa38deSAlexander Motin xpt_async(AC_BUS_RESET, ch->path, NULL);
250452c9ce25SScott Long /* Disable port interrupts */
250552c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
250652c9ce25SScott Long /* Reset and reconnect PHY, */
2507fcd7f38fSAlexander Motin if (!ahci_sata_phy_reset(ch)) {
250852c9ce25SScott Long if (bootverbose)
2509fcd7f38fSAlexander Motin device_printf(ch->dev,
2510df1439e3SAlexander Motin "AHCI reset: device not found\n");
251152c9ce25SScott Long ch->devices = 0;
251252c9ce25SScott Long /* Enable wanted port interrupts */
251352c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IE,
251406ccfe1dSAlexander Motin (((ch->pm_level != 0) ? AHCI_P_IX_CPD | AHCI_P_IX_MP : 0) |
251506ccfe1dSAlexander Motin AHCI_P_IX_PRC | AHCI_P_IX_PC));
251683c5d981SAlexander Motin xpt_release_simq(ch->sim, TRUE);
251752c9ce25SScott Long return;
251852c9ce25SScott Long }
2519df1439e3SAlexander Motin if (bootverbose)
2520fcd7f38fSAlexander Motin device_printf(ch->dev, "AHCI reset: device found\n");
252152c9ce25SScott Long /* Wait for clearing busy status. */
2522fcd7f38fSAlexander Motin if (ahci_wait_ready(ch, dumping ? 31000 : 0, 0)) {
2523df1439e3SAlexander Motin if (dumping)
2524fcd7f38fSAlexander Motin ahci_clo(ch);
2525df1439e3SAlexander Motin else
2526df1439e3SAlexander Motin ch->resetting = 310;
2527df1439e3SAlexander Motin }
252852c9ce25SScott Long ch->devices = 1;
252952c9ce25SScott Long /* Enable wanted port interrupts */
253052c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_IE,
253106ccfe1dSAlexander Motin (((ch->pm_level != 0) ? AHCI_P_IX_CPD | AHCI_P_IX_MP : 0) |
253206ccfe1dSAlexander Motin AHCI_P_IX_TFE | AHCI_P_IX_HBF |
253352c9ce25SScott Long AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF |
253406ccfe1dSAlexander Motin ((ch->pm_level == 0) ? AHCI_P_IX_PRC : 0) | AHCI_P_IX_PC |
25354dbabf10SAlexander Motin AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) |
25364dbabf10SAlexander Motin AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR)));
2537df1439e3SAlexander Motin if (ch->resetting)
2538fcd7f38fSAlexander Motin callout_reset(&ch->reset_timer, hz / 10, ahci_reset_to, ch);
25393a3d820bSAlexander Motin else {
2540fcd7f38fSAlexander Motin ahci_start(ch, 1);
254183c5d981SAlexander Motin xpt_release_simq(ch->sim, TRUE);
254252c9ce25SScott Long }
25433a3d820bSAlexander Motin }
254452c9ce25SScott Long
254552c9ce25SScott Long static int
ahci_setup_fis(struct ahci_channel * ch,struct ahci_cmd_tab * ctp,union ccb * ccb,int tag)2546fcd7f38fSAlexander Motin ahci_setup_fis(struct ahci_channel *ch, struct ahci_cmd_tab *ctp, union ccb *ccb, int tag)
254752c9ce25SScott Long {
254852c9ce25SScott Long u_int8_t *fis = &ctp->cfis[0];
254952c9ce25SScott Long
25500375d6f5SWarner Losh bzero(fis, 20);
255152c9ce25SScott Long fis[0] = 0x27; /* host to device */
255252c9ce25SScott Long fis[1] = (ccb->ccb_h.target_id & 0x0f);
255352c9ce25SScott Long if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
255452c9ce25SScott Long fis[1] |= 0x80;
255552c9ce25SScott Long fis[2] = ATA_PACKET_CMD;
2556b447e682SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
2557b447e682SAlexander Motin ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
255852c9ce25SScott Long fis[3] = ATA_F_DMA;
255952c9ce25SScott Long else {
256052c9ce25SScott Long fis[5] = ccb->csio.dxfer_len;
256152c9ce25SScott Long fis[6] = ccb->csio.dxfer_len >> 8;
256252c9ce25SScott Long }
256352c9ce25SScott Long fis[7] = ATA_D_LBA;
256452c9ce25SScott Long fis[15] = ATA_A_4BIT;
256552c9ce25SScott Long bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
256652c9ce25SScott Long ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
256752c9ce25SScott Long ctp->acmd, ccb->csio.cdb_len);
2568db12db31SAlexander Motin bzero(ctp->acmd + ccb->csio.cdb_len, 32 - ccb->csio.cdb_len);
256952c9ce25SScott Long } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
257052c9ce25SScott Long fis[1] |= 0x80;
257152c9ce25SScott Long fis[2] = ccb->ataio.cmd.command;
257252c9ce25SScott Long fis[3] = ccb->ataio.cmd.features;
257352c9ce25SScott Long fis[4] = ccb->ataio.cmd.lba_low;
257452c9ce25SScott Long fis[5] = ccb->ataio.cmd.lba_mid;
257552c9ce25SScott Long fis[6] = ccb->ataio.cmd.lba_high;
257652c9ce25SScott Long fis[7] = ccb->ataio.cmd.device;
257752c9ce25SScott Long fis[8] = ccb->ataio.cmd.lba_low_exp;
257852c9ce25SScott Long fis[9] = ccb->ataio.cmd.lba_mid_exp;
257952c9ce25SScott Long fis[10] = ccb->ataio.cmd.lba_high_exp;
258052c9ce25SScott Long fis[11] = ccb->ataio.cmd.features_exp;
258152c9ce25SScott Long fis[12] = ccb->ataio.cmd.sector_count;
25824138a744SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
258369df0af0SAlexander Motin fis[12] &= 0x07;
25844138a744SAlexander Motin fis[12] |= tag << 3;
258552c9ce25SScott Long }
25869a6844d5SKenneth D. Merry fis[13] = ccb->ataio.cmd.sector_count_exp;
258706c888ecSAlexander Motin if (ccb->ataio.ata_flags & ATA_FLAG_ICC)
258806c888ecSAlexander Motin fis[14] = ccb->ataio.icc;
258952c9ce25SScott Long fis[15] = ATA_A_4BIT;
2590916d57dfSWarner Losh if (ccb->ataio.ata_flags & ATA_FLAG_AUX) {
2591916d57dfSWarner Losh fis[16] = ccb->ataio.aux & 0xff;
2592916d57dfSWarner Losh fis[17] = (ccb->ataio.aux >> 8) & 0xff;
2593916d57dfSWarner Losh fis[18] = (ccb->ataio.aux >> 16) & 0xff;
2594916d57dfSWarner Losh fis[19] = (ccb->ataio.aux >> 24) & 0xff;
2595916d57dfSWarner Losh }
259606c888ecSAlexander Motin } else {
259706c888ecSAlexander Motin fis[15] = ccb->ataio.cmd.control;
259806c888ecSAlexander Motin }
259952c9ce25SScott Long return (20);
260052c9ce25SScott Long }
260152c9ce25SScott Long
260252c9ce25SScott Long static int
ahci_sata_connect(struct ahci_channel * ch)260352c9ce25SScott Long ahci_sata_connect(struct ahci_channel *ch)
260452c9ce25SScott Long {
260552c9ce25SScott Long u_int32_t status;
2606346483b1SMariusz Zaborski int timeout, timeoutslot, found = 0;
260752c9ce25SScott Long
2608346483b1SMariusz Zaborski /*
2609346483b1SMariusz Zaborski * Wait for "connect well", up to 100ms by default and
2610346483b1SMariusz Zaborski * up to 500ms for devices with the SLOWDEV quirk.
2611346483b1SMariusz Zaborski */
2612346483b1SMariusz Zaborski timeoutslot = ((ch->quirks & AHCI_Q_SLOWDEV) ? 5000 : 1000);
2613346483b1SMariusz Zaborski for (timeout = 0; timeout < timeoutslot; timeout++) {
261452c9ce25SScott Long status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
26151f145eafSAlexander Motin if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE)
26161f145eafSAlexander Motin found = 1;
261752c9ce25SScott Long if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
261852c9ce25SScott Long ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
261952c9ce25SScott Long ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
262052c9ce25SScott Long break;
26214dbabf10SAlexander Motin if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) {
26224dbabf10SAlexander Motin if (bootverbose) {
26234dbabf10SAlexander Motin device_printf(ch->dev, "SATA offline status=%08x\n",
26244dbabf10SAlexander Motin status);
26254dbabf10SAlexander Motin }
26264dbabf10SAlexander Motin return (0);
26274dbabf10SAlexander Motin }
26281f145eafSAlexander Motin if (found == 0 && timeout >= 100)
26291f145eafSAlexander Motin break;
26303a3d820bSAlexander Motin DELAY(100);
263152c9ce25SScott Long }
2632346483b1SMariusz Zaborski if (timeout >= timeoutslot || !found) {
263352c9ce25SScott Long if (bootverbose) {
26341f145eafSAlexander Motin device_printf(ch->dev,
26351f145eafSAlexander Motin "SATA connect timeout time=%dus status=%08x\n",
26361f145eafSAlexander Motin timeout * 100, status);
263752c9ce25SScott Long }
263852c9ce25SScott Long return (0);
263952c9ce25SScott Long }
264052c9ce25SScott Long if (bootverbose) {
26413a3d820bSAlexander Motin device_printf(ch->dev, "SATA connect time=%dus status=%08x\n",
26423a3d820bSAlexander Motin timeout * 100, status);
264352c9ce25SScott Long }
264452c9ce25SScott Long /* Clear SATA error register */
264552c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff);
264652c9ce25SScott Long return (1);
264752c9ce25SScott Long }
264852c9ce25SScott Long
264952c9ce25SScott Long static int
ahci_sata_phy_reset(struct ahci_channel * ch)2650fcd7f38fSAlexander Motin ahci_sata_phy_reset(struct ahci_channel *ch)
265152c9ce25SScott Long {
2652b447e682SAlexander Motin int sata_rev;
26534668666fSWarner Losh uint32_t val, detval;
265452c9ce25SScott Long
265506ccfe1dSAlexander Motin if (ch->listening) {
265606ccfe1dSAlexander Motin val = ATA_INL(ch->r_mem, AHCI_P_CMD);
265706ccfe1dSAlexander Motin val |= AHCI_P_CMD_SUD;
265806ccfe1dSAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_CMD, val);
265906ccfe1dSAlexander Motin ch->listening = 0;
266006ccfe1dSAlexander Motin }
2661b447e682SAlexander Motin sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
2662b447e682SAlexander Motin if (sata_rev == 1)
266352c9ce25SScott Long val = ATA_SC_SPD_SPEED_GEN1;
2664b447e682SAlexander Motin else if (sata_rev == 2)
266552c9ce25SScott Long val = ATA_SC_SPD_SPEED_GEN2;
2666b447e682SAlexander Motin else if (sata_rev == 3)
266752c9ce25SScott Long val = ATA_SC_SPD_SPEED_GEN3;
266852c9ce25SScott Long else
266952c9ce25SScott Long val = 0;
26704668666fSWarner Losh detval = ahci_ch_detval(ch, ATA_SC_DET_RESET);
267152c9ce25SScott Long ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
26724668666fSWarner Losh detval | val |
26734dbabf10SAlexander Motin ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER);
26743a3d820bSAlexander Motin DELAY(1000);
26754668666fSWarner Losh detval = ahci_ch_detval(ch, ATA_SC_DET_IDLE);
26764dbabf10SAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
26774668666fSWarner Losh detval | val | ((ch->pm_level > 0) ? 0 :
267852c9ce25SScott Long (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
26794c9d5e6bSAlexander Motin if (!ahci_sata_connect(ch)) {
268006ccfe1dSAlexander Motin if (ch->caps & AHCI_CAP_SSS) {
268106ccfe1dSAlexander Motin val = ATA_INL(ch->r_mem, AHCI_P_CMD);
268206ccfe1dSAlexander Motin val &= ~AHCI_P_CMD_SUD;
268306ccfe1dSAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_CMD, val);
268406ccfe1dSAlexander Motin ch->listening = 1;
268506ccfe1dSAlexander Motin } else if (ch->pm_level > 0)
26864c9d5e6bSAlexander Motin ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE);
26874c9d5e6bSAlexander Motin return (0);
26884c9d5e6bSAlexander Motin }
26894c9d5e6bSAlexander Motin return (1);
269052c9ce25SScott Long }
269152c9ce25SScott Long
269289f2835fSAlexander Motin static int
ahci_check_ids(struct ahci_channel * ch,union ccb * ccb)2693fcd7f38fSAlexander Motin ahci_check_ids(struct ahci_channel *ch, union ccb *ccb)
269489f2835fSAlexander Motin {
269589f2835fSAlexander Motin
269689f2835fSAlexander Motin if (ccb->ccb_h.target_id > ((ch->caps & AHCI_CAP_SPM) ? 15 : 0)) {
269789f2835fSAlexander Motin ccb->ccb_h.status = CAM_TID_INVALID;
2698227d67aaSAlexander Motin ahci_done(ch, ccb);
269989f2835fSAlexander Motin return (-1);
270089f2835fSAlexander Motin }
270189f2835fSAlexander Motin if (ccb->ccb_h.target_lun != 0) {
270289f2835fSAlexander Motin ccb->ccb_h.status = CAM_LUN_INVALID;
2703227d67aaSAlexander Motin ahci_done(ch, ccb);
270489f2835fSAlexander Motin return (-1);
270589f2835fSAlexander Motin }
270689f2835fSAlexander Motin return (0);
270789f2835fSAlexander Motin }
270889f2835fSAlexander Motin
270952c9ce25SScott Long static void
ahciaction(struct cam_sim * sim,union ccb * ccb)271052c9ce25SScott Long ahciaction(struct cam_sim *sim, union ccb *ccb)
271152c9ce25SScott Long {
271252c9ce25SScott Long struct ahci_channel *ch;
271352c9ce25SScott Long
271452c9ce25SScott Long CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n",
271552c9ce25SScott Long ccb->ccb_h.func_code));
271652c9ce25SScott Long
271752c9ce25SScott Long ch = (struct ahci_channel *)cam_sim_softc(sim);
271852c9ce25SScott Long switch (ccb->ccb_h.func_code) {
271952c9ce25SScott Long /* Common cases first */
272052c9ce25SScott Long case XPT_ATA_IO: /* Execute the requested I/O operation */
272152c9ce25SScott Long case XPT_SCSI_IO:
2722fcd7f38fSAlexander Motin if (ahci_check_ids(ch, ccb))
272389f2835fSAlexander Motin return;
272489f2835fSAlexander Motin if (ch->devices == 0 ||
272589f2835fSAlexander Motin (ch->pm_present == 0 &&
272689f2835fSAlexander Motin ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
272752c9ce25SScott Long ccb->ccb_h.status = CAM_SEL_TIMEOUT;
272852c9ce25SScott Long break;
272952c9ce25SScott Long }
27306bbd332eSAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_NONE;
273152c9ce25SScott Long /* Check for command collision. */
2732fcd7f38fSAlexander Motin if (ahci_check_collision(ch, ccb)) {
273352c9ce25SScott Long /* Freeze command. */
273452c9ce25SScott Long ch->frozen = ccb;
273552c9ce25SScott Long /* We have only one frozen slot, so freeze simq also. */
273652c9ce25SScott Long xpt_freeze_simq(ch->sim, 1);
273752c9ce25SScott Long return;
273852c9ce25SScott Long }
2739fcd7f38fSAlexander Motin ahci_begin_transaction(ch, ccb);
274089f2835fSAlexander Motin return;
274152c9ce25SScott Long case XPT_ABORT: /* Abort the specified CCB */
274252c9ce25SScott Long /* XXX Implement */
274352c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
274452c9ce25SScott Long break;
274552c9ce25SScott Long case XPT_SET_TRAN_SETTINGS:
274652c9ce25SScott Long {
274752c9ce25SScott Long struct ccb_trans_settings *cts = &ccb->cts;
2748c8039fc6SAlexander Motin struct ahci_device *d;
274952c9ce25SScott Long
2750fcd7f38fSAlexander Motin if (ahci_check_ids(ch, ccb))
275189f2835fSAlexander Motin return;
2752c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
2753c8039fc6SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id];
2754c8039fc6SAlexander Motin else
2755c8039fc6SAlexander Motin d = &ch->user[ccb->ccb_h.target_id];
2756c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
2757c8039fc6SAlexander Motin d->revision = cts->xport_specific.sata.revision;
2758c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
2759c8039fc6SAlexander Motin d->mode = cts->xport_specific.sata.mode;
2760c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
2761c8039fc6SAlexander Motin d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
2762c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
2763c8039fc6SAlexander Motin d->tags = min(ch->numslots, cts->xport_specific.sata.tags);
2764c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM)
276552c9ce25SScott Long ch->pm_present = cts->xport_specific.sata.pm_present;
27664cca1530SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
27674cca1530SAlexander Motin d->atapi = cts->xport_specific.sata.atapi;
2768da6808c1SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
2769da6808c1SAlexander Motin d->caps = cts->xport_specific.sata.caps;
277052c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
277152c9ce25SScott Long break;
277252c9ce25SScott Long }
277352c9ce25SScott Long case XPT_GET_TRAN_SETTINGS:
277452c9ce25SScott Long /* Get default/user set transfer settings for the target */
277552c9ce25SScott Long {
277652c9ce25SScott Long struct ccb_trans_settings *cts = &ccb->cts;
2777c8039fc6SAlexander Motin struct ahci_device *d;
277852c9ce25SScott Long uint32_t status;
277952c9ce25SScott Long
2780fcd7f38fSAlexander Motin if (ahci_check_ids(ch, ccb))
278189f2835fSAlexander Motin return;
2782c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
2783c8039fc6SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id];
2784c8039fc6SAlexander Motin else
2785c8039fc6SAlexander Motin d = &ch->user[ccb->ccb_h.target_id];
2786bc1bf6e8SAlexander Motin cts->protocol = PROTO_UNSPECIFIED;
27874dbabf10SAlexander Motin cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
278852c9ce25SScott Long cts->transport = XPORT_SATA;
27894dbabf10SAlexander Motin cts->transport_version = XPORT_VERSION_UNSPECIFIED;
279052c9ce25SScott Long cts->proto_specific.valid = 0;
279152c9ce25SScott Long cts->xport_specific.sata.valid = 0;
2792c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
2793c8039fc6SAlexander Motin (ccb->ccb_h.target_id == 15 ||
2794c8039fc6SAlexander Motin (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
279552c9ce25SScott Long status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK;
2796c8039fc6SAlexander Motin if (status & 0x0f0) {
2797c8039fc6SAlexander Motin cts->xport_specific.sata.revision =
2798c8039fc6SAlexander Motin (status & 0x0f0) >> 4;
2799c8039fc6SAlexander Motin cts->xport_specific.sata.valid |=
2800c8039fc6SAlexander Motin CTS_SATA_VALID_REVISION;
280152c9ce25SScott Long }
2802da6808c1SAlexander Motin cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
2803da6808c1SAlexander Motin if (ch->pm_level) {
2804da6808c1SAlexander Motin if (ch->caps & (AHCI_CAP_PSC | AHCI_CAP_SSC))
2805da6808c1SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
2806da6808c1SAlexander Motin if (ch->caps2 & AHCI_CAP2_APST)
2807da6808c1SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_APST;
2808da6808c1SAlexander Motin }
2809da6808c1SAlexander Motin if ((ch->caps & AHCI_CAP_SNCQ) &&
2810da6808c1SAlexander Motin (ch->quirks & AHCI_Q_NOAA) == 0)
2811da6808c1SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_DMAAA;
28128d169381SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
2813da6808c1SAlexander Motin cts->xport_specific.sata.caps &=
2814da6808c1SAlexander Motin ch->user[ccb->ccb_h.target_id].caps;
2815da6808c1SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
281652c9ce25SScott Long } else {
2817c8039fc6SAlexander Motin cts->xport_specific.sata.revision = d->revision;
2818c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
2819da6808c1SAlexander Motin cts->xport_specific.sata.caps = d->caps;
2820da6808c1SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
282152c9ce25SScott Long }
2822c8039fc6SAlexander Motin cts->xport_specific.sata.mode = d->mode;
2823c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
2824c8039fc6SAlexander Motin cts->xport_specific.sata.bytecount = d->bytecount;
2825c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
2826c8039fc6SAlexander Motin cts->xport_specific.sata.pm_present = ch->pm_present;
282752c9ce25SScott Long cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
2828c8039fc6SAlexander Motin cts->xport_specific.sata.tags = d->tags;
2829c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
28304cca1530SAlexander Motin cts->xport_specific.sata.atapi = d->atapi;
28314cca1530SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
283252c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
283352c9ce25SScott Long break;
283452c9ce25SScott Long }
283552c9ce25SScott Long case XPT_RESET_BUS: /* Reset the specified SCSI bus */
283652c9ce25SScott Long case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
2837fcd7f38fSAlexander Motin ahci_reset(ch);
283852c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
283952c9ce25SScott Long break;
284052c9ce25SScott Long case XPT_TERM_IO: /* Terminate the I/O process */
284152c9ce25SScott Long /* XXX Implement */
284252c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
284352c9ce25SScott Long break;
284452c9ce25SScott Long case XPT_PATH_INQ: /* Path routing inquiry */
284552c9ce25SScott Long {
284652c9ce25SScott Long struct ccb_pathinq *cpi = &ccb->cpi;
284752c9ce25SScott Long
284852c9ce25SScott Long cpi->version_num = 1; /* XXX??? */
284949b96d25SAlexander Motin cpi->hba_inquiry = PI_SDTR_ABLE;
285049b96d25SAlexander Motin if (ch->caps & AHCI_CAP_SNCQ)
285149b96d25SAlexander Motin cpi->hba_inquiry |= PI_TAG_ABLE;
285252c9ce25SScott Long if (ch->caps & AHCI_CAP_SPM)
285352c9ce25SScott Long cpi->hba_inquiry |= PI_SATAPM;
285452c9ce25SScott Long cpi->target_sprt = 0;
28557ce90968SAlexander Motin cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED;
28567ce90968SAlexander Motin if ((ch->quirks & AHCI_Q_NOAUX) == 0)
28577ce90968SAlexander Motin cpi->hba_misc |= PIM_ATA_EXT;
285852c9ce25SScott Long cpi->hba_eng_cnt = 0;
285952c9ce25SScott Long if (ch->caps & AHCI_CAP_SPM)
28608e7cccb3SAlexander Motin cpi->max_target = 15;
286152c9ce25SScott Long else
286252c9ce25SScott Long cpi->max_target = 0;
286352c9ce25SScott Long cpi->max_lun = 0;
286452c9ce25SScott Long cpi->initiator_id = 0;
286552c9ce25SScott Long cpi->bus_id = cam_sim_bus(sim);
286652c9ce25SScott Long cpi->base_transfer_speed = 150000;
28674195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
28684195c7deSAlan Somers strlcpy(cpi->hba_vid, "AHCI", HBA_IDLEN);
28694195c7deSAlan Somers strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
287052c9ce25SScott Long cpi->unit_number = cam_sim_unit(sim);
287152c9ce25SScott Long cpi->transport = XPORT_SATA;
28724dbabf10SAlexander Motin cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
2873eb586bd9SAlexander Motin cpi->protocol = PROTO_ATA;
28744dbabf10SAlexander Motin cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
2875cd853791SKonstantin Belousov cpi->maxio = ctob(AHCI_SG_ENTRIES - 1);
287619dbe46dSAlexander Motin /* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */
2877802df3acSWarner Losh if (ch->quirks & AHCI_Q_MAXIO_64K)
2878ce531e6bSAlexander Motin cpi->maxio = min(cpi->maxio, 128 * 512);
2879802df3acSWarner Losh cpi->hba_vendor = ch->vendorid;
2880802df3acSWarner Losh cpi->hba_device = ch->deviceid;
2881802df3acSWarner Losh cpi->hba_subvendor = ch->subvendorid;
2882802df3acSWarner Losh cpi->hba_subdevice = ch->subdeviceid;
288352c9ce25SScott Long cpi->ccb_h.status = CAM_REQ_CMP;
288452c9ce25SScott Long break;
288552c9ce25SScott Long }
288652c9ce25SScott Long default:
288752c9ce25SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
288852c9ce25SScott Long break;
288952c9ce25SScott Long }
2890227d67aaSAlexander Motin ahci_done(ch, ccb);
289152c9ce25SScott Long }
289252c9ce25SScott Long
289352c9ce25SScott Long static void
ahcipoll(struct cam_sim * sim)289452c9ce25SScott Long ahcipoll(struct cam_sim *sim)
289552c9ce25SScott Long {
289652c9ce25SScott Long struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim);
2897227d67aaSAlexander Motin uint32_t istatus;
289852c9ce25SScott Long
2899227d67aaSAlexander Motin /* Read interrupt statuses and process if any. */
2900227d67aaSAlexander Motin istatus = ATA_INL(ch->r_mem, AHCI_P_IS);
2901227d67aaSAlexander Motin if (istatus != 0)
2902227d67aaSAlexander Motin ahci_ch_intr_main(ch, istatus);
290318301f74SAlexander Motin if (ch->resetting != 0 &&
290418301f74SAlexander Motin (--ch->resetpolldiv <= 0 || !callout_pending(&ch->reset_timer))) {
290518301f74SAlexander Motin ch->resetpolldiv = 1000;
2906fcd7f38fSAlexander Motin ahci_reset_to(ch);
290718301f74SAlexander Motin }
290852c9ce25SScott Long }
290974eb18b6SIan Lepore
2910802df3acSWarner Losh MODULE_VERSION(ahci, 1);
2911802df3acSWarner Losh MODULE_DEPEND(ahci, cam, 1, 1, 1);
2912