ahci_pci.c revision 198319
153537Sbrian/*-
280728Sbrian * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
353537Sbrian * All rights reserved.
453537Sbrian *
553537Sbrian * Redistribution and use in source and binary forms, with or without
653537Sbrian * modification, are permitted provided that the following conditions
753537Sbrian * are met:
853537Sbrian * 1. Redistributions of source code must retain the above copyright
953537Sbrian *    notice, this list of conditions and the following disclaimer,
1053537Sbrian *    without modification, immediately at the beginning of the file.
1153537Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1253537Sbrian *    notice, this list of conditions and the following disclaimer in the
1353537Sbrian *    documentation and/or other materials provided with the distribution.
1453537Sbrian *
1553537Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1653537Sbrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1753537Sbrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1853537Sbrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1953537Sbrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2053537Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2153537Sbrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2253537Sbrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2353537Sbrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2453537Sbrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2553537Sbrian */
2653537Sbrian
2753537Sbrian#include <sys/cdefs.h>
2853537Sbrian__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci.c 198319 2009-10-21 12:42:25Z mav $");
2953537Sbrian
3053537Sbrian#include <sys/param.h>
3153537Sbrian#include <sys/module.h>
3253537Sbrian#include <sys/systm.h>
3353537Sbrian#include <sys/kernel.h>
3453537Sbrian#include <sys/ata.h>
3553537Sbrian#include <sys/bus.h>
3653537Sbrian#include <sys/endian.h>
3753537Sbrian#include <sys/malloc.h>
3853537Sbrian#include <sys/lock.h>
3953537Sbrian#include <sys/mutex.h>
4053537Sbrian#include <sys/sema.h>
4153537Sbrian#include <sys/taskqueue.h>
4253537Sbrian#include <vm/uma.h>
4353537Sbrian#include <machine/stdarg.h>
4453537Sbrian#include <machine/resource.h>
4553537Sbrian#include <machine/bus.h>
4653537Sbrian#include <sys/rman.h>
4753537Sbrian#include <dev/pci/pcivar.h>
4866602Sbrian#include <dev/pci/pcireg.h>
4953537Sbrian#include "ahci.h"
5053537Sbrian
5153537Sbrian#include <cam/cam.h>
5253537Sbrian#include <cam/cam_ccb.h>
5353537Sbrian#include <cam/cam_sim.h>
5453537Sbrian#include <cam/cam_xpt_sim.h>
5553537Sbrian#include <cam/cam_xpt_periph.h>
5653537Sbrian#include <cam/cam_debug.h>
5753537Sbrian
5853537Sbrian/* local prototypes */
5953537Sbrianstatic int ahci_setup_interrupt(device_t dev);
6053537Sbrianstatic void ahci_intr(void *data);
6153537Sbrianstatic void ahci_intr_one(void *data);
6253537Sbrianstatic int ahci_suspend(device_t dev);
6353537Sbrianstatic int ahci_resume(device_t dev);
6486705Sbrianstatic int ahci_ch_suspend(device_t dev);
6586705Sbrianstatic int ahci_ch_resume(device_t dev);
6653537Sbrianstatic void ahci_ch_pm(void *arg);
6790160Skrisstatic void ahci_ch_intr_locked(void *data);
6890160Skrisstatic void ahci_ch_intr(void *data);
6969582Sbrianstatic int ahci_ctlr_reset(device_t dev);
7069582Sbrianstatic void ahci_begin_transaction(device_t dev, union ccb *ccb);
7153537Sbrianstatic void ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
7253537Sbrianstatic void ahci_execute_transaction(struct ahci_slot *slot);
7353537Sbrianstatic void ahci_timeout(struct ahci_slot *slot);
7495258Sdesstatic void ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et);
7553609Sbrianstatic int ahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag);
7653537Sbrianstatic void ahci_dmainit(device_t dev);
7753537Sbrianstatic void ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
7853537Sbrianstatic void ahci_dmafini(device_t dev);
7953537Sbrianstatic void ahci_slotsalloc(device_t dev);
8069582Sbrianstatic void ahci_slotsfree(device_t dev);
8153537Sbrianstatic void ahci_reset(device_t dev);
8269582Sbrianstatic void ahci_start(device_t dev);
8353537Sbrianstatic void ahci_stop(device_t dev);
8453537Sbrianstatic void ahci_clo(device_t dev);
8553537Sbrianstatic void ahci_start_fr(device_t dev);
8653537Sbrianstatic void ahci_stop_fr(device_t dev);
8753537Sbrian
8853537Sbrianstatic int ahci_sata_connect(struct ahci_channel *ch);
8953537Sbrianstatic int ahci_sata_phy_reset(device_t dev, int quick);
9053537Sbrian
9153537Sbrianstatic void ahci_issue_read_log(device_t dev);
9253537Sbrianstatic void ahci_process_read_log(device_t dev, union ccb *ccb);
9353537Sbrian
9453537Sbrianstatic void ahciaction(struct cam_sim *sim, union ccb *ccb);
9553537Sbrianstatic void ahcipoll(struct cam_sim *sim);
9653537Sbrian
9753537SbrianMALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driver data buffers");
9853537Sbrian
9953537Sbrian/*
10053537Sbrian * AHCI v1.x compliant SATA chipset support functions
10153537Sbrian */
10253537Sbrianstatic int
10353537Sbrianahci_probe(device_t dev)
10453537Sbrian{
10553537Sbrian
10653537Sbrian	/* is this a possible AHCI candidate ? */
10753537Sbrian	if (pci_get_class(dev) != PCIC_STORAGE ||
10853537Sbrian	    pci_get_subclass(dev) != PCIS_STORAGE_SATA)
10953537Sbrian		return (ENXIO);
11053537Sbrian
11153537Sbrian	/* is this PCI device flagged as an AHCI compliant chip ? */
11253537Sbrian	if (pci_get_progif(dev) != PCIP_STORAGE_SATA_AHCI_1_0)
11353537Sbrian		return (ENXIO);
11453537Sbrian
11553537Sbrian	device_set_desc_copy(dev, "AHCI controller");
11653537Sbrian	return (BUS_PROBE_VENDOR);
11753537Sbrian}
11853537Sbrian
11953537Sbrianstatic int
12053537Sbrianahci_attach(device_t dev)
12153537Sbrian{
12253537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
12353537Sbrian	device_t child;
12453537Sbrian	int	error, unit, speed;
12553537Sbrian	u_int32_t version;
12653537Sbrian
12753537Sbrian	ctlr->dev = dev;
12853537Sbrian	resource_int_value(device_get_name(dev),
12953537Sbrian	    device_get_unit(dev), "ccc", &ctlr->ccc);
13053537Sbrian	/* if we have a memory BAR(5) we are likely on an AHCI part */
13153537Sbrian	ctlr->r_rid = PCIR_BAR(5);
13253537Sbrian	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
13353537Sbrian	    &ctlr->r_rid, RF_ACTIVE)))
13453537Sbrian		return ENXIO;
13553537Sbrian	/* Setup our own memory management for channels. */
13653537Sbrian	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
13753537Sbrian	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
13853537Sbrian	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
13953537Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
14053537Sbrian		return (error);
14153537Sbrian	}
14253537Sbrian	if ((error = rman_manage_region(&ctlr->sc_iomem,
14353537Sbrian	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
14453537Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
14553537Sbrian		rman_fini(&ctlr->sc_iomem);
14653537Sbrian		return (error);
14782276Sbrian	}
14853537Sbrian	/* Reset controller */
14953537Sbrian	if ((error = ahci_ctlr_reset(dev)) != 0) {
15053537Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
15153537Sbrian		rman_fini(&ctlr->sc_iomem);
15253537Sbrian		return (error);
15353537Sbrian	};
15453537Sbrian	/* Get the number of HW channels */
15553537Sbrian	ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI);
15653537Sbrian	ctlr->channels = MAX(flsl(ctlr->ichannels),
15753537Sbrian	    (ATA_INL(ctlr->r_mem, AHCI_CAP) & AHCI_CAP_NPMASK) + 1);
15853537Sbrian	/* Setup interrupts. */
15953537Sbrian	if (ahci_setup_interrupt(dev)) {
16053537Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
16153537Sbrian		rman_fini(&ctlr->sc_iomem);
16253537Sbrian		return ENXIO;
16353537Sbrian	}
16453537Sbrian	/* Announce HW capabilities. */
16553537Sbrian	version = ATA_INL(ctlr->r_mem, AHCI_VS);
16653537Sbrian	ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP);
16753537Sbrian	if (version >= 0x00010020)
16853537Sbrian		ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2);
16953537Sbrian	speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT;
17053537Sbrian	device_printf(dev,
17153537Sbrian		    "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s\n",
17253537Sbrian		    ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f),
17353537Sbrian		    ((version >> 4) & 0xf0) + (version & 0x0f),
17453537Sbrian		    (ctlr->caps & AHCI_CAP_NPMASK) + 1,
17553537Sbrian		    ((speed == 1) ? "1.5":((speed == 2) ? "3":
17653537Sbrian		    ((speed == 3) ? "6":"?"))),
17753537Sbrian		    (ctlr->caps & AHCI_CAP_SPM) ?
17853537Sbrian		    "supported" : "not supported");
17953537Sbrian	if (bootverbose) {
18053537Sbrian		device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps",
18153537Sbrian		    (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"",
18253537Sbrian		    (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"",
18353537Sbrian		    (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"",
18453537Sbrian		    (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"",
18553537Sbrian		    (ctlr->caps & AHCI_CAP_SSS) ? " SS":"",
18653537Sbrian		    (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"",
18753537Sbrian		    (ctlr->caps & AHCI_CAP_SAL) ? " AL":"",
18853537Sbrian		    (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"",
18953537Sbrian		    ((speed == 1) ? "1.5":((speed == 2) ? "3":
19053537Sbrian		    ((speed == 3) ? "6":"?"))));
19153537Sbrian		printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n",
19253537Sbrian		    (ctlr->caps & AHCI_CAP_SAM) ? " AM":"",
19353537Sbrian		    (ctlr->caps & AHCI_CAP_SPM) ? " PM":"",
19453537Sbrian		    (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"",
19553537Sbrian		    (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"",
19653537Sbrian		    (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"",
19753537Sbrian		    (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"",
19853537Sbrian		    ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1,
19953537Sbrian		    (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"",
20053537Sbrian		    (ctlr->caps & AHCI_CAP_EMS) ? " EM":"",
20153537Sbrian		    (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"",
20253537Sbrian		    (ctlr->caps & AHCI_CAP_NPMASK) + 1);
20353537Sbrian	}
20453537Sbrian	if (bootverbose && version >= 0x00010020) {
20553537Sbrian		device_printf(dev, "Caps2:%s%s%s\n",
20653537Sbrian		    (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"",
20753537Sbrian		    (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"",
20853537Sbrian		    (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":"");
20953537Sbrian	}
21053537Sbrian	/* Attach all channels on this controller */
21153537Sbrian	for (unit = 0; unit < ctlr->channels; unit++) {
21253537Sbrian		if ((ctlr->ichannels & (1 << unit)) == 0)
21353537Sbrian			continue;
21453537Sbrian		child = device_add_child(dev, "ahcich", -1);
21553537Sbrian		if (child == NULL)
21653537Sbrian			device_printf(dev, "failed to add channel device\n");
21753537Sbrian		else
21853537Sbrian			device_set_ivars(child, (void *)(intptr_t)unit);
21953537Sbrian	}
22053537Sbrian	bus_generic_attach(dev);
22153537Sbrian	return 0;
22253537Sbrian}
22353537Sbrian
22468032Sbrianstatic int
22553537Sbrianahci_detach(device_t dev)
22668846Sbrian{
22768846Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
22853537Sbrian	device_t *children;
22953537Sbrian	int nchildren, i;
23053537Sbrian
23153537Sbrian	/* Detach & delete all children */
23253537Sbrian	if (!device_get_children(dev, &children, &nchildren)) {
23353537Sbrian		for (i = 0; i < nchildren; i++)
23453537Sbrian			device_delete_child(dev, children[i]);
23553537Sbrian		free(children, M_TEMP);
23653537Sbrian	}
23753537Sbrian	/* Free interrupts. */
23853537Sbrian	for (i = 0; i < ctlr->numirqs; i++) {
23953537Sbrian		if (ctlr->irqs[i].r_irq) {
24053537Sbrian			bus_teardown_intr(dev, ctlr->irqs[i].r_irq,
24153537Sbrian			    ctlr->irqs[i].handle);
24253537Sbrian			bus_release_resource(dev, SYS_RES_IRQ,
24353537Sbrian			    ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq);
24453537Sbrian		}
24553537Sbrian	}
24653537Sbrian	pci_release_msi(dev);
24753537Sbrian	/* Free memory. */
24853537Sbrian	rman_fini(&ctlr->sc_iomem);
24953537Sbrian	if (ctlr->r_mem)
25053537Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
25153537Sbrian	return (0);
25269948Sjulian}
25386705Sbrian
25486705Sbrianstatic int
25553537Sbrianahci_ctlr_reset(device_t dev)
25653537Sbrian{
25753537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
25853537Sbrian	int timeout;
25953537Sbrian
26086705Sbrian	if (pci_read_config(dev, 0x00, 4) == 0x28298086 &&
26186705Sbrian	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
26253537Sbrian		pci_write_config(dev, 0x92, 0x01, 1);
26353537Sbrian	/* Enable AHCI mode */
26453537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
26553537Sbrian	/* Reset AHCI controller */
26653537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR);
26753537Sbrian	for (timeout = 1000; timeout > 0; timeout--) {
26853537Sbrian		DELAY(1000);
26953537Sbrian		if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0)
27053537Sbrian			break;
27153537Sbrian	}
27253537Sbrian	if (timeout == 0) {
27353537Sbrian		device_printf(dev, "AHCI controller reset failure\n");
27453537Sbrian		return ENXIO;
27553537Sbrian	}
27653537Sbrian	/* Reenable AHCI mode */
27753537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
27853537Sbrian	/* Clear interrupts */
27953537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS));
28053537Sbrian	/* Configure CCC */
28153537Sbrian	if (ctlr->ccc) {
28253537Sbrian		ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI));
28353537Sbrian		ATA_OUTL(ctlr->r_mem, AHCI_CCCC,
28453537Sbrian		    (ctlr->ccc << AHCI_CCCC_TV_SHIFT) |
28553537Sbrian		    (4 << AHCI_CCCC_CC_SHIFT) |
28653537Sbrian		    AHCI_CCCC_EN);
28753537Sbrian		ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) &
28853537Sbrian		    AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT;
28953537Sbrian		if (bootverbose) {
29053537Sbrian			device_printf(dev,
29153537Sbrian			    "CCC with %dms/4cmd enabled on vector %d\n",
29253537Sbrian			    ctlr->ccc, ctlr->cccv);
29353537Sbrian		}
29453537Sbrian	}
29553537Sbrian	/* Enable AHCI interrupts */
29653537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
29753537Sbrian	    ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE);
29853537Sbrian	return (0);
29953537Sbrian}
30053537Sbrian
30153537Sbrianstatic int
30253537Sbrianahci_suspend(device_t dev)
30353537Sbrian{
30453537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
30553537Sbrian
30653537Sbrian	bus_generic_suspend(dev);
30753537Sbrian	/* Disable interupts, so the state change(s) doesn't trigger */
30853537Sbrian	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
30953537Sbrian	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
31053537Sbrian	return 0;
31153537Sbrian}
31253537Sbrian
31353537Sbrianstatic int
31453537Sbrianahci_resume(device_t dev)
31553537Sbrian{
31653537Sbrian	int res;
31753537Sbrian
31868032Sbrian	if ((res = ahci_ctlr_reset(dev)) != 0)
31953537Sbrian		return (res);
32068846Sbrian	return (bus_generic_resume(dev));
32168846Sbrian}
32253537Sbrian
32353537Sbrianstatic int
32453537Sbrianahci_setup_interrupt(device_t dev)
32553537Sbrian{
32653537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
32753537Sbrian	int i, msi = 1;
32853537Sbrian
32953537Sbrian	/* Process hints. */
33053537Sbrian	resource_int_value(device_get_name(dev),
33153537Sbrian	    device_get_unit(dev), "msi", &msi);
33253537Sbrian	if (msi < 0)
33353537Sbrian		msi = 0;
33469948Sjulian	else if (msi == 1)
33579597Sbrian		msi = min(1, pci_msi_count(dev));
33669948Sjulian	else if (msi > 1)
33769948Sjulian		msi = pci_msi_count(dev);
33869948Sjulian	/* Allocate MSI if needed/present. */
33969948Sjulian	if (msi && pci_alloc_msi(dev, &msi) == 0) {
34069948Sjulian		ctlr->numirqs = msi;
34153537Sbrian	} else {
34269948Sjulian		msi = 0;
34369948Sjulian		ctlr->numirqs = 1;
34469948Sjulian	}
34569948Sjulian	/* Check for single MSI vector fallback. */
34669948Sjulian	if (ctlr->numirqs > 1 &&
34769948Sjulian	    (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) {
34869948Sjulian		device_printf(dev, "Falling back to one MSI\n");
34969948Sjulian		ctlr->numirqs = 1;
35069948Sjulian	}
35186705Sbrian	/* Allocate all IRQs. */
35286705Sbrian	for (i = 0; i < ctlr->numirqs; i++) {
35386705Sbrian		ctlr->irqs[i].ctlr = ctlr;
35486705Sbrian		ctlr->irqs[i].r_irq_rid = i + (msi ? 1 : 0);
35586762Sbrian		if (ctlr->numirqs == 1 || i >= ctlr->channels ||
35686762Sbrian		    (ctlr->ccc && i == ctlr->cccv))
35786762Sbrian			ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL;
35886705Sbrian		else if (i == ctlr->numirqs - 1)
35986705Sbrian			ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER;
36086705Sbrian		else
36186705Sbrian			ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE;
36253537Sbrian		if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
36353537Sbrian		    &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
36453537Sbrian			device_printf(dev, "unable to map interrupt\n");
36553537Sbrian			return ENXIO;
36653537Sbrian		}
36753537Sbrian		if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL,
36853537Sbrian		    (ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE) ? ahci_intr_one : ahci_intr,
36953537Sbrian		    &ctlr->irqs[i], &ctlr->irqs[i].handle))) {
37053537Sbrian			/* SOS XXX release r_irq */
37153537Sbrian			device_printf(dev, "unable to setup interrupt\n");
37253537Sbrian			return ENXIO;
37353537Sbrian		}
37453537Sbrian	}
37553537Sbrian	return (0);
37682333Sbrian}
37753537Sbrian
37853537Sbrian/*
37953537Sbrian * Common case interrupt handler.
38053537Sbrian */
38182276Sbrianstatic void
38282276Sbrianahci_intr(void *data)
38382276Sbrian{
38482276Sbrian	struct ahci_controller_irq *irq = data;
38582276Sbrian	struct ahci_controller *ctlr = irq->ctlr;
38682276Sbrian	u_int32_t is;
38753537Sbrian	void *arg;
38853537Sbrian	int unit;
38953537Sbrian
39053537Sbrian	if (irq->mode == AHCI_IRQ_MODE_ALL) {
39153537Sbrian		unit = 0;
39253537Sbrian		if (ctlr->ccc)
39353537Sbrian			is = ctlr->ichannels;
39453537Sbrian		else
39553537Sbrian			is = ATA_INL(ctlr->r_mem, AHCI_IS);
39653537Sbrian	} else {	/* AHCI_IRQ_MODE_AFTER */
39753537Sbrian		unit = irq->r_irq_rid - 1;
39853537Sbrian		is = ATA_INL(ctlr->r_mem, AHCI_IS);
39953537Sbrian	}
40053537Sbrian	for (; unit < ctlr->channels; unit++) {
40153537Sbrian		if ((is & (1 << unit)) != 0 &&
40253537Sbrian		    (arg = ctlr->interrupt[unit].argument)) {
40353537Sbrian			ctlr->interrupt[unit].function(arg);
40453537Sbrian			ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit);
40553537Sbrian		}
40653537Sbrian	}
40753537Sbrian}
40890975Sbrian
40990975Sbrian/*
41090975Sbrian * Simplified interrupt handler for multivector MSI mode.
41190975Sbrian */
41290975Sbrianstatic void
41390975Sbrianahci_intr_one(void *data)
41453537Sbrian{
41553537Sbrian	struct ahci_controller_irq *irq = data;
41653537Sbrian	struct ahci_controller *ctlr = irq->ctlr;
41753537Sbrian	void *arg;
41853537Sbrian	int unit;
41953537Sbrian
42053537Sbrian	unit = irq->r_irq_rid - 1;
42153537Sbrian	if ((arg = ctlr->interrupt[unit].argument))
42253537Sbrian	    ctlr->interrupt[unit].function(arg);
42353537Sbrian}
42453537Sbrian
42553537Sbrianstatic struct resource *
42653537Sbrianahci_alloc_resource(device_t dev, device_t child, int type, int *rid,
42753537Sbrian		       u_long start, u_long end, u_long count, u_int flags)
42853537Sbrian{
42953537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
43053537Sbrian	int unit = ((struct ahci_channel *)device_get_softc(child))->unit;
43153537Sbrian	struct resource *res = NULL;
43253537Sbrian	int offset = AHCI_OFFSET + (unit << 7);
43353537Sbrian	long st;
43453537Sbrian
43553537Sbrian	switch (type) {
43653537Sbrian	case SYS_RES_MEMORY:
43753537Sbrian		st = rman_get_start(ctlr->r_mem);
43879452Sbrian		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
43953537Sbrian		    st + offset + 127, 128, RF_ACTIVE, child);
44053537Sbrian		if (res) {
44153537Sbrian			bus_space_handle_t bsh;
44253537Sbrian			bus_space_tag_t bst;
44353537Sbrian			bsh = rman_get_bushandle(ctlr->r_mem);
44453537Sbrian			bst = rman_get_bustag(ctlr->r_mem);
44553537Sbrian			bus_space_subregion(bst, bsh, offset, 128, &bsh);
44653537Sbrian			rman_set_bushandle(res, bsh);
44753537Sbrian			rman_set_bustag(res, bst);
44853537Sbrian		}
44953537Sbrian		break;
45053537Sbrian	case SYS_RES_IRQ:
45166602Sbrian		if (*rid == ATA_IRQ_RID)
45269582Sbrian			res = ctlr->irqs[0].r_irq;
45366602Sbrian		break;
45466602Sbrian	}
45566602Sbrian	return (res);
45666602Sbrian}
45766602Sbrian
45866602Sbrianstatic int
45966602Sbrianahci_release_resource(device_t dev, device_t child, int type, int rid,
46066602Sbrian			 struct resource *r)
46166602Sbrian{
46266602Sbrian
46366602Sbrian	switch (type) {
46466602Sbrian	case SYS_RES_MEMORY:
46566602Sbrian		rman_release_resource(r);
46666602Sbrian		return (0);
46766602Sbrian	case SYS_RES_IRQ:
46869582Sbrian		if (rid != ATA_IRQ_RID)
46966602Sbrian			return ENOENT;
47066602Sbrian		return (0);
47166602Sbrian	}
47266602Sbrian	return (EINVAL);
47366602Sbrian}
47466602Sbrian
47566602Sbrianstatic int
47666602Sbrianahci_setup_intr(device_t dev, device_t child, struct resource *irq,
47766602Sbrian		   int flags, driver_filter_t *filter, driver_intr_t *function,
47866602Sbrian		   void *argument, void **cookiep)
47966602Sbrian{
48069582Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
48166602Sbrian	int unit = (intptr_t)device_get_ivars(child);
48266602Sbrian
48366602Sbrian	if (filter != NULL) {
48466602Sbrian		printf("ahci.c: we cannot use a filter here\n");
48566602Sbrian		return (EINVAL);
48666602Sbrian	}
48766602Sbrian	ctlr->interrupt[unit].function = function;
48866602Sbrian	ctlr->interrupt[unit].argument = argument;
48966602Sbrian	return (0);
49066602Sbrian}
49190779Simp
49253537Sbrianstatic int
49366602Sbrianahci_teardown_intr(device_t dev, device_t child, struct resource *irq,
49466602Sbrian		      void *cookie)
49580728Sbrian{
49653537Sbrian	struct ahci_controller *ctlr = device_get_softc(dev);
49780724Sbrian	int unit = (intptr_t)device_get_ivars(child);
49866602Sbrian
49969582Sbrian	ctlr->interrupt[unit].function = NULL;
50053537Sbrian	ctlr->interrupt[unit].argument = NULL;
50153537Sbrian	return (0);
50253537Sbrian}
50353609Sbrian
50453537Sbrianstatic int
50580728Sbrianahci_print_child(device_t dev, device_t child)
50653537Sbrian{
50753537Sbrian	int retval;
50866602Sbrian
50953537Sbrian	retval = bus_print_child_header(dev, child);
51080728Sbrian	retval += printf(" at channel %d",
51153537Sbrian	    (int)(intptr_t)device_get_ivars(child));
51253537Sbrian	retval += bus_print_child_footer(dev, child);
51353537Sbrian
51453537Sbrian	return (retval);
51553537Sbrian}
51653609Sbrian
51753609Sbriandevclass_t ahci_devclass;
51853609Sbrianstatic device_method_t ahci_methods[] = {
51953609Sbrian	DEVMETHOD(device_probe,     ahci_probe),
52053537Sbrian	DEVMETHOD(device_attach,    ahci_attach),
52153537Sbrian	DEVMETHOD(device_detach,    ahci_detach),
52253537Sbrian	DEVMETHOD(device_suspend,   ahci_suspend),
52353537Sbrian	DEVMETHOD(device_resume,    ahci_resume),
52453537Sbrian	DEVMETHOD(bus_print_child,  ahci_print_child),
52553537Sbrian	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
52653537Sbrian	DEVMETHOD(bus_release_resource,     ahci_release_resource),
52753537Sbrian	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
52853537Sbrian	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
52953537Sbrian	{ 0, 0 }
53053537Sbrian};
53153537Sbrianstatic driver_t ahci_driver = {
53280728Sbrian        "ahci",
53380728Sbrian        ahci_methods,
53480728Sbrian        sizeof(struct ahci_controller)
53580728Sbrian};
53666602SbrianDRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
53766602SbrianMODULE_VERSION(ahci, 1);
53866602SbrianMODULE_DEPEND(ahci, cam, 1, 1, 1);
53966602Sbrian
54066602Sbrianstatic int
54153537Sbrianahci_ch_probe(device_t dev)
54253537Sbrian{
54353537Sbrian
54453537Sbrian	device_set_desc_copy(dev, "AHCI channel");
54553537Sbrian	return (0);
54653537Sbrian}
54753537Sbrian
54853537Sbrianstatic int
54953537Sbrianahci_ch_attach(device_t dev)
55053537Sbrian{
55153537Sbrian	struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev));
55253537Sbrian	struct ahci_channel *ch = device_get_softc(dev);
55380728Sbrian	struct cam_devq *devq;
55480728Sbrian	int rid, error;
55580728Sbrian
55653537Sbrian	ch->dev = dev;
55780728Sbrian	ch->unit = (intptr_t)device_get_ivars(dev);
55880728Sbrian	ch->caps = ctlr->caps;
55980728Sbrian	ch->caps2 = ctlr->caps2;
56080728Sbrian	ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1,
56153537Sbrian	mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF);
56253537Sbrian	resource_int_value(device_get_name(dev),
56353537Sbrian	    device_get_unit(dev), "pm_level", &ch->pm_level);
56480728Sbrian	if (ch->pm_level > 3)
56553537Sbrian		callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
56653537Sbrian	/* Limit speed for my onboard JMicron external port.
56780728Sbrian	 * It is not eSATA really. */
56853537Sbrian	if (pci_get_devid(ctlr->dev) == 0x2363197b &&
56953537Sbrian	    pci_get_subvendor(ctlr->dev) == 0x1043 &&
57053537Sbrian	    pci_get_subdevice(ctlr->dev) == 0x81e4 &&
57180728Sbrian	    ch->unit == 0)
57253537Sbrian		ch->sata_rev = 1;
57353537Sbrian	resource_int_value(device_get_name(dev),
57453537Sbrian	    device_get_unit(dev), "sata_rev", &ch->sata_rev);
57553537Sbrian	rid = ch->unit;
57653537Sbrian	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
57753537Sbrian	    &rid, RF_ACTIVE)))
57853537Sbrian		return (ENXIO);
57953537Sbrian	ahci_dmainit(dev);
58053537Sbrian	ahci_slotsalloc(dev);
58153537Sbrian	ahci_ch_resume(dev);
58253537Sbrian	mtx_lock(&ch->mtx);
58353537Sbrian	rid = ATA_IRQ_RID;
58453537Sbrian	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
58553537Sbrian	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
58666602Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
58753537Sbrian		device_printf(dev, "Unable to map interrupt\n");
58853537Sbrian		return (ENXIO);
58953537Sbrian	}
59053537Sbrian	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
59153537Sbrian	    ahci_ch_intr_locked, dev, &ch->ih))) {
59253537Sbrian		device_printf(dev, "Unable to setup interrupt\n");
59353537Sbrian		error = ENXIO;
59453537Sbrian		goto err1;
59553537Sbrian	}
59653537Sbrian	/* Create the device queue for our SIM. */
59753537Sbrian	devq = cam_simq_alloc(ch->numslots);
59853537Sbrian	if (devq == NULL) {
59953537Sbrian		device_printf(dev, "Unable to allocate simq\n");
60053537Sbrian		error = ENOMEM;
60153537Sbrian		goto err1;
60253537Sbrian	}
60353537Sbrian	/* Construct SIM entry */
60453537Sbrian	ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch,
60553537Sbrian	    device_get_unit(dev), &ch->mtx, ch->numslots, 0, devq);
60653609Sbrian	if (ch->sim == NULL) {
60753609Sbrian		device_printf(dev, "unable to allocate sim\n");
60853537Sbrian		error = ENOMEM;
60953537Sbrian		goto err2;
61053537Sbrian	}
61153609Sbrian	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
61253609Sbrian		device_printf(dev, "unable to register xpt bus\n");
61353609Sbrian		error = ENXIO;
61453609Sbrian		goto err2;
61553609Sbrian	}
61653609Sbrian	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
61753609Sbrian	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
61853609Sbrian		device_printf(dev, "unable to create path\n");
61953609Sbrian		error = ENXIO;
62053609Sbrian		goto err3;
62153609Sbrian	}
62253609Sbrian	if (ch->pm_level > 3) {
62353609Sbrian		callout_reset(&ch->pm_timer,
62453609Sbrian		    (ch->pm_level == 4) ? hz / 1000 : hz / 8,
62553609Sbrian		    ahci_ch_pm, dev);
62653537Sbrian	}
62766602Sbrian	mtx_unlock(&ch->mtx);
62866602Sbrian	return (0);
62953537Sbrian
63080724Sbrianerr3:
63180724Sbrian	xpt_bus_deregister(cam_sim_path(ch->sim));
63280733Sbrianerr2:
63380724Sbrian	cam_sim_free(ch->sim, /*free_devq*/TRUE);
63480724Sbrianerr1:
63580724Sbrian	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
63680724Sbrian	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
63780724Sbrian	mtx_unlock(&ch->mtx);
63853537Sbrian	return (error);
63969582Sbrian}
64053537Sbrian
64153537Sbrianstatic int
64253537Sbrianahci_ch_detach(device_t dev)
64353537Sbrian{
64453537Sbrian	struct ahci_channel *ch = device_get_softc(dev);
64553537Sbrian
64653537Sbrian	mtx_lock(&ch->mtx);
64753537Sbrian	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
64853537Sbrian	xpt_free_path(ch->path);
64953537Sbrian	xpt_bus_deregister(cam_sim_path(ch->sim));
65053537Sbrian	cam_sim_free(ch->sim, /*free_devq*/TRUE);
65153537Sbrian	mtx_unlock(&ch->mtx);
65253537Sbrian
65353537Sbrian	if (ch->pm_level > 3)
65453537Sbrian		callout_drain(&ch->pm_timer);
65553537Sbrian	bus_teardown_intr(dev, ch->r_irq, ch->ih);
65653537Sbrian	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
65753537Sbrian
65853537Sbrian	ahci_ch_suspend(dev);
65953537Sbrian	ahci_slotsfree(dev);
66053537Sbrian	ahci_dmafini(dev);
66153537Sbrian
66253537Sbrian	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
66353537Sbrian	mtx_destroy(&ch->mtx);
66453537Sbrian	return (0);
66553537Sbrian}
66653537Sbrian
66769948Sjulianstatic int
66853537Sbrianahci_ch_suspend(device_t dev)
66953537Sbrian{
67069582Sbrian	struct ahci_channel *ch = device_get_softc(dev);
67169582Sbrian
67269582Sbrian	/* Disable port interrupts. */
67369582Sbrian	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
67469582Sbrian	/* Reset command register. */
67569582Sbrian	ahci_stop(dev);
67669582Sbrian	ahci_stop_fr(dev);
67769582Sbrian	ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0);
67869582Sbrian	/* Allow everything, including partial and slumber modes. */
67969582Sbrian	ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0);
68069582Sbrian	/* Request slumber mode transition and give some time to get there. */
68169582Sbrian	ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER);
68269582Sbrian	DELAY(100);
68369582Sbrian	/* Disable PHY. */
68453537Sbrian	ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE);
68553537Sbrian	return (0);
686}
687
688static int
689ahci_ch_resume(device_t dev)
690{
691	struct ahci_channel *ch = device_get_softc(dev);
692	uint64_t work;
693
694	/* Disable port interrupts */
695	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
696	/* Setup work areas */
697	work = ch->dma.work_bus + AHCI_CL_OFFSET;
698	ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff);
699	ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32);
700	work = ch->dma.rfis_bus;
701	ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff);
702	ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32);
703	/* Activate the channel and power/spin up device */
704	ATA_OUTL(ch->r_mem, AHCI_P_CMD,
705	     (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
706	     ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) |
707	     ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 )));
708	ahci_start_fr(dev);
709	ahci_start(dev);
710	return (0);
711}
712
713devclass_t ahcich_devclass;
714static device_method_t ahcich_methods[] = {
715	DEVMETHOD(device_probe,     ahci_ch_probe),
716	DEVMETHOD(device_attach,    ahci_ch_attach),
717	DEVMETHOD(device_detach,    ahci_ch_detach),
718	DEVMETHOD(device_suspend,   ahci_ch_suspend),
719	DEVMETHOD(device_resume,    ahci_ch_resume),
720	{ 0, 0 }
721};
722static driver_t ahcich_driver = {
723        "ahcich",
724        ahcich_methods,
725        sizeof(struct ahci_channel)
726};
727DRIVER_MODULE(ahcich, ahci, ahcich_driver, ahci_devclass, 0, 0);
728
729struct ahci_dc_cb_args {
730	bus_addr_t maddr;
731	int error;
732};
733
734static void
735ahci_dmainit(device_t dev)
736{
737	struct ahci_channel *ch = device_get_softc(dev);
738	struct ahci_dc_cb_args dcba;
739
740	if (ch->caps & AHCI_CAP_64BIT)
741		ch->dma.max_address = BUS_SPACE_MAXADDR;
742	else
743		ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
744	/* Command area. */
745	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
746	    ch->dma.max_address, BUS_SPACE_MAXADDR,
747	    NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE,
748	    0, NULL, NULL, &ch->dma.work_tag))
749		goto error;
750	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
751	    &ch->dma.work_map))
752		goto error;
753	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
754	    AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) {
755		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
756		goto error;
757	}
758	ch->dma.work_bus = dcba.maddr;
759	/* FIS receive area. */
760	if (bus_dma_tag_create(bus_get_dma_tag(dev), 4096, 0,
761	    ch->dma.max_address, BUS_SPACE_MAXADDR,
762	    NULL, NULL, 4096, 1, 4096,
763	    0, NULL, NULL, &ch->dma.rfis_tag))
764		goto error;
765	if (bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0,
766	    &ch->dma.rfis_map))
767		goto error;
768	if (bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis,
769	    4096, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) {
770		bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
771		goto error;
772	}
773	ch->dma.rfis_bus = dcba.maddr;
774	/* Data area. */
775	if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
776	    ch->dma.max_address, BUS_SPACE_MAXADDR,
777	    NULL, NULL,
778	    AHCI_SG_ENTRIES * PAGE_SIZE * ch->numslots,
779	    AHCI_SG_ENTRIES, AHCI_PRD_MAX,
780	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
781		goto error;
782	}
783	return;
784
785error:
786	device_printf(dev, "WARNING - DMA initialization failed\n");
787	ahci_dmafini(dev);
788}
789
790static void
791ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
792{
793	struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc;
794
795	if (!(dcba->error = error))
796		dcba->maddr = segs[0].ds_addr;
797}
798
799static void
800ahci_dmafini(device_t dev)
801{
802	struct ahci_channel *ch = device_get_softc(dev);
803
804	if (ch->dma.data_tag) {
805		bus_dma_tag_destroy(ch->dma.data_tag);
806		ch->dma.data_tag = NULL;
807	}
808	if (ch->dma.rfis_bus) {
809		bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map);
810		bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
811		ch->dma.rfis_bus = 0;
812		ch->dma.rfis_map = NULL;
813		ch->dma.rfis = NULL;
814	}
815	if (ch->dma.work_bus) {
816		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
817		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
818		ch->dma.work_bus = 0;
819		ch->dma.work_map = NULL;
820		ch->dma.work = NULL;
821	}
822	if (ch->dma.work_tag) {
823		bus_dma_tag_destroy(ch->dma.work_tag);
824		ch->dma.work_tag = NULL;
825	}
826}
827
828static void
829ahci_slotsalloc(device_t dev)
830{
831	struct ahci_channel *ch = device_get_softc(dev);
832	int i;
833
834	/* Alloc and setup command/dma slots */
835	bzero(ch->slot, sizeof(ch->slot));
836	for (i = 0; i < ch->numslots; i++) {
837		struct ahci_slot *slot = &ch->slot[i];
838
839		slot->dev = dev;
840		slot->slot = i;
841		slot->state = AHCI_SLOT_EMPTY;
842		slot->ccb = NULL;
843		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
844
845		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
846			device_printf(ch->dev, "FAILURE - create data_map\n");
847	}
848}
849
850static void
851ahci_slotsfree(device_t dev)
852{
853	struct ahci_channel *ch = device_get_softc(dev);
854	int i;
855
856	/* Free all dma slots */
857	for (i = 0; i < ch->numslots; i++) {
858		struct ahci_slot *slot = &ch->slot[i];
859
860		callout_drain(&slot->timeout);
861		if (slot->dma.data_map) {
862			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
863			slot->dma.data_map = NULL;
864		}
865	}
866}
867
868static void
869ahci_phy_check_events(device_t dev, u_int32_t serr)
870{
871	struct ahci_channel *ch = device_get_softc(dev);
872
873	if ((serr & ATA_SE_PHY_CHANGED) && (ch->pm_level == 0)) {
874		u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
875		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
876		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
877		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
878			if (bootverbose)
879				device_printf(dev, "CONNECT requested\n");
880			ahci_reset(dev);
881		} else {
882			if (bootverbose)
883				device_printf(dev, "DISCONNECT requested\n");
884			ch->devices = 0;
885		}
886	}
887}
888
889static void
890ahci_notify_events(device_t dev, u_int32_t status)
891{
892	struct ahci_channel *ch = device_get_softc(dev);
893	struct cam_path *dpath;
894	int i;
895
896	ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status);
897	if (bootverbose)
898		device_printf(dev, "SNTF 0x%04x\n", status);
899	for (i = 0; i < 16; i++) {
900		if ((status & (1 << i)) == 0)
901			continue;
902		if (xpt_create_path(&dpath, NULL,
903		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
904			xpt_async(AC_SCSI_AEN, dpath, NULL);
905			xpt_free_path(dpath);
906		}
907	}
908}
909
910static void
911ahci_ch_intr_locked(void *data)
912{
913	device_t dev = (device_t)data;
914	struct ahci_channel *ch = device_get_softc(dev);
915
916	mtx_lock(&ch->mtx);
917	ahci_ch_intr(data);
918	mtx_unlock(&ch->mtx);
919}
920
921static void
922ahci_ch_pm(void *arg)
923{
924	device_t dev = (device_t)arg;
925	struct ahci_channel *ch = device_get_softc(dev);
926	uint32_t work;
927
928	if (ch->numrslots != 0)
929		return;
930	work = ATA_INL(ch->r_mem, AHCI_P_CMD);
931	if (ch->pm_level == 4)
932		work |= AHCI_P_CMD_PARTIAL;
933	else
934		work |= AHCI_P_CMD_SLUMBER;
935	ATA_OUTL(ch->r_mem, AHCI_P_CMD, work);
936}
937
938static void
939ahci_ch_intr(void *data)
940{
941	device_t dev = (device_t)data;
942	struct ahci_channel *ch = device_get_softc(dev);
943	uint32_t istatus, sstatus, cstatus, serr = 0, sntf = 0, ok, err;
944	enum ahci_err_type et;
945	int i, ccs, ncq_err = 0;
946
947	/* Read and clear interrupt statuses. */
948	istatus = ATA_INL(ch->r_mem, AHCI_P_IS);
949	if (istatus == 0)
950		return;
951	ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus);
952	/* Read command statuses. */
953	sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
954	cstatus = ATA_INL(ch->r_mem, AHCI_P_CI);
955	if ((istatus & AHCI_P_IX_SDB) && (ch->caps & AHCI_CAP_SSNTF))
956		sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF);
957	/* Process PHY events */
958	if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF |
959	    AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
960		serr = ATA_INL(ch->r_mem, AHCI_P_SERR);
961		if (serr) {
962			ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr);
963			ahci_phy_check_events(dev, serr);
964		}
965	}
966	/* Process command errors */
967	if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF |
968	    AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
969//device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n",
970//    __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD),
971//    serr);
972		ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK)
973		    >> AHCI_P_CMD_CCS_SHIFT;
974		err = ch->rslots & (cstatus | sstatus);
975		/* Kick controller into sane state */
976		ahci_stop(dev);
977		ahci_start(dev);
978	} else {
979		ccs = 0;
980		err = 0;
981	}
982	/* Complete all successfull commands. */
983	ok = ch->rslots & ~(cstatus | sstatus);
984	for (i = 0; i < ch->numslots; i++) {
985		if ((ok >> i) & 1)
986			ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE);
987	}
988	/* On error, complete the rest of commands with error statuses. */
989	if (err) {
990		if (ch->frozen) {
991			union ccb *fccb = ch->frozen;
992			ch->frozen = NULL;
993			fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
994			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
995				xpt_freeze_devq(fccb->ccb_h.path, 1);
996				fccb->ccb_h.status |= CAM_DEV_QFRZN;
997			}
998			xpt_done(fccb);
999		}
1000		for (i = 0; i < ch->numslots; i++) {
1001			/* XXX: reqests in loading state. */
1002			if (((err >> i) & 1) == 0)
1003				continue;
1004			if (istatus & AHCI_P_IX_IF) {
1005				if (ch->numtslots == 0 && i != ccs)
1006					et = AHCI_ERR_INNOCENT;
1007				else
1008					et = AHCI_ERR_SATA;
1009			} else if (istatus & AHCI_P_IX_TFE) {
1010				/* Task File Error */
1011				if (ch->numtslots == 0) {
1012					/* Untagged operation. */
1013					if (i == ccs)
1014						et = AHCI_ERR_TFE;
1015					else
1016						et = AHCI_ERR_INNOCENT;
1017				} else {
1018					/* Tagged operation. */
1019					et = AHCI_ERR_NCQ;
1020					ncq_err = 1;
1021				}
1022			} else
1023				et = AHCI_ERR_INVALID;
1024			ahci_end_transaction(&ch->slot[i], et);
1025		}
1026		if (ncq_err)
1027			ahci_issue_read_log(dev);
1028	}
1029	/* Process NOTIFY events */
1030	if (sntf)
1031		ahci_notify_events(dev, sntf);
1032}
1033
1034/* Must be called with channel locked. */
1035static int
1036ahci_check_collision(device_t dev, union ccb *ccb)
1037{
1038	struct ahci_channel *ch = device_get_softc(dev);
1039
1040	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1041	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1042		/* Tagged command while untagged are active. */
1043		if (ch->numrslots != 0 && ch->numtslots == 0)
1044			return (1);
1045		/* Tagged command while tagged to other target is active. */
1046		if (ch->numtslots != 0 &&
1047		    ch->taggedtarget != ccb->ccb_h.target_id)
1048			return (1);
1049	} else {
1050		/* Untagged command while tagged are active. */
1051		if (ch->numrslots != 0 && ch->numtslots != 0)
1052			return (1);
1053	}
1054	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1055	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
1056		/* Atomic command while anything active. */
1057		if (ch->numrslots != 0)
1058			return (1);
1059	}
1060       /* We have some atomic command running. */
1061       if (ch->aslots != 0)
1062               return (1);
1063	return (0);
1064}
1065
1066/* Must be called with channel locked. */
1067static void
1068ahci_begin_transaction(device_t dev, union ccb *ccb)
1069{
1070	struct ahci_channel *ch = device_get_softc(dev);
1071	struct ahci_slot *slot;
1072	int tag;
1073
1074	/* Choose empty slot. */
1075	tag = ch->lastslot;
1076	while (ch->slot[tag].state != AHCI_SLOT_EMPTY) {
1077		if (++tag >= ch->numslots)
1078			tag = 0;
1079		KASSERT(tag != ch->lastslot, ("ahci: ALL SLOTS BUSY!"));
1080	}
1081	ch->lastslot = tag;
1082	/* Occupy chosen slot. */
1083	slot = &ch->slot[tag];
1084	slot->ccb = ccb;
1085	/* Stop PM timer. */
1086	if (ch->numrslots == 0 && ch->pm_level > 3)
1087		callout_stop(&ch->pm_timer);
1088	/* Update channel stats. */
1089	ch->numrslots++;
1090	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1091	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1092		ch->numtslots++;
1093		ch->taggedtarget = ccb->ccb_h.target_id;
1094	}
1095	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1096	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
1097		ch->aslots |= (1 << slot->slot);
1098	slot->dma.nsegs = 0;
1099	/* If request moves data, setup and load SG list */
1100	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1101		void *buf;
1102		bus_size_t size;
1103
1104		slot->state = AHCI_SLOT_LOADING;
1105		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1106			buf = ccb->ataio.data_ptr;
1107			size = ccb->ataio.dxfer_len;
1108		} else {
1109			buf = ccb->csio.data_ptr;
1110			size = ccb->csio.dxfer_len;
1111		}
1112		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
1113		    buf, size, ahci_dmasetprd, slot, 0);
1114	} else
1115		ahci_execute_transaction(slot);
1116}
1117
1118/* Locked by busdma engine. */
1119static void
1120ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1121{
1122	struct ahci_slot *slot = arg;
1123	struct ahci_channel *ch = device_get_softc(slot->dev);
1124	struct ahci_cmd_tab *ctp;
1125	struct ahci_dma_prd *prd;
1126	int i;
1127
1128	if (error) {
1129		device_printf(slot->dev, "DMA load error\n");
1130		ahci_end_transaction(slot, AHCI_ERR_INVALID);
1131		return;
1132	}
1133	KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n"));
1134	/* Get a piece of the workspace for this request */
1135	ctp = (struct ahci_cmd_tab *)
1136		(ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot));
1137	/* Fill S/G table */
1138	prd = &ctp->prd_tab[0];
1139	for (i = 0; i < nsegs; i++) {
1140		prd[i].dba = htole64(segs[i].ds_addr);
1141		prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK);
1142	}
1143	slot->dma.nsegs = nsegs;
1144	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1145	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1146	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1147	ahci_execute_transaction(slot);
1148}
1149
1150/* Must be called with channel locked. */
1151static void
1152ahci_execute_transaction(struct ahci_slot *slot)
1153{
1154	device_t dev = slot->dev;
1155	struct ahci_channel *ch = device_get_softc(dev);
1156	struct ahci_cmd_tab *ctp;
1157	struct ahci_cmd_list *clp;
1158	union ccb *ccb = slot->ccb;
1159	int port = ccb->ccb_h.target_id & 0x0f;
1160	int fis_size;
1161
1162	/* Get a piece of the workspace for this request */
1163	ctp = (struct ahci_cmd_tab *)
1164		(ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot));
1165	/* Setup the FIS for this request */
1166	if (!(fis_size = ahci_setup_fis(ctp, ccb, slot->slot))) {
1167		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1168		ahci_end_transaction(slot, AHCI_ERR_INVALID);
1169		return;
1170	}
1171	/* Setup the command list entry */
1172	clp = (struct ahci_cmd_list *)
1173	    (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot));
1174	clp->prd_length = slot->dma.nsegs;
1175	clp->cmd_flags = (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) |
1176		     (ccb->ccb_h.func_code == XPT_SCSI_IO ?
1177		      (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) |
1178		     (fis_size / sizeof(u_int32_t)) |
1179		     (port << 12);
1180	/* Special handling for Soft Reset command. */
1181	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1182	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1183	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1184		/* Kick controller into sane state */
1185		ahci_stop(dev);
1186		ahci_clo(dev);
1187		ahci_start(dev);
1188		clp->cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY;
1189	}
1190	clp->bytecount = 0;
1191	clp->cmd_table_phys = htole64(ch->dma.work_bus + AHCI_CT_OFFSET +
1192				  (AHCI_CT_SIZE * slot->slot));
1193	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1194	    BUS_DMASYNC_PREWRITE);
1195	bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
1196	    BUS_DMASYNC_PREREAD);
1197	/* Set ACTIVE bit for NCQ commands. */
1198	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1199	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1200		ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot);
1201	}
1202	/* Issue command to the controller. */
1203	slot->state = AHCI_SLOT_RUNNING;
1204	ch->rslots |= (1 << slot->slot);
1205	ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot));
1206	/* Device reset commands doesn't interrupt. Poll them. */
1207	if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1208	    (ccb->ataio.cmd.command == ATA_DEVICE_RESET ||
1209	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL))) {
1210		int count, timeout = ccb->ccb_h.timeout;
1211		enum ahci_err_type et = AHCI_ERR_NONE;
1212
1213		for (count = 0; count < timeout; count++) {
1214			DELAY(1000);
1215			if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot)))
1216				break;
1217			if (ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) {
1218				device_printf(ch->dev,
1219				    "Poll error on slot %d, TFD: %04x\n",
1220				    slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD));
1221				et = AHCI_ERR_TFE;
1222				break;
1223			}
1224		}
1225		if (timeout && (count >= timeout)) {
1226			device_printf(ch->dev,
1227			    "Poll timeout on slot %d\n", slot->slot);
1228			et = AHCI_ERR_TIMEOUT;
1229		}
1230		if (et != AHCI_ERR_NONE) {
1231			/* Kick controller into sane state */
1232			ahci_stop(ch->dev);
1233			ahci_start(ch->dev);
1234		}
1235		ahci_end_transaction(slot, et);
1236		return;
1237	}
1238	/* Start command execution timeout */
1239	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 2000,
1240	    (timeout_t*)ahci_timeout, slot);
1241	return;
1242}
1243
1244/* Locked by callout mechanism. */
1245static void
1246ahci_timeout(struct ahci_slot *slot)
1247{
1248	device_t dev = slot->dev;
1249	struct ahci_channel *ch = device_get_softc(dev);
1250	uint32_t sstatus;
1251	int ccs;
1252	int i;
1253
1254	/* Check for stale timeout. */
1255	if (slot->state < AHCI_SLOT_RUNNING)
1256		return;
1257
1258	/* Check if slot was not being executed last time we checked. */
1259	if (slot->state < AHCI_SLOT_EXECUTING) {
1260		/* Check if slot started executing. */
1261		sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
1262		ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK)
1263		    >> AHCI_P_CMD_CCS_SHIFT;
1264		if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot)
1265			slot->state = AHCI_SLOT_EXECUTING;
1266
1267		callout_reset(&slot->timeout,
1268		    (int)slot->ccb->ccb_h.timeout * hz / 2000,
1269		    (timeout_t*)ahci_timeout, slot);
1270		return;
1271	}
1272
1273	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1274	device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n",
1275	    ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI),
1276	    ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots,
1277	    ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR));
1278	/* Kick controller into sane state. */
1279	ahci_stop(ch->dev);
1280	ahci_start(ch->dev);
1281
1282	/* Handle frozen command. */
1283	if (ch->frozen) {
1284		union ccb *fccb = ch->frozen;
1285		ch->frozen = NULL;
1286		fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1287		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1288			xpt_freeze_devq(fccb->ccb_h.path, 1);
1289			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1290		}
1291		xpt_done(fccb);
1292	}
1293	/* Handle command with timeout. */
1294	ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT);
1295	/* Handle the rest of commands. */
1296	for (i = 0; i < ch->numslots; i++) {
1297		/* Do we have a running request on slot? */
1298		if (ch->slot[i].state < AHCI_SLOT_RUNNING)
1299			continue;
1300		ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
1301	}
1302}
1303
1304/* Must be called with channel locked. */
1305static void
1306ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et)
1307{
1308	device_t dev = slot->dev;
1309	struct ahci_channel *ch = device_get_softc(dev);
1310	union ccb *ccb = slot->ccb;
1311
1312	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1313	    BUS_DMASYNC_POSTWRITE);
1314	/* Read result registers to the result struct
1315	 * May be incorrect if several commands finished same time,
1316	 * so read only when sure or have to.
1317	 */
1318	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1319		struct ata_res *res = &ccb->ataio.res;
1320
1321		if ((et == AHCI_ERR_TFE) ||
1322		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1323			u_int8_t *fis = ch->dma.rfis + 0x40;
1324			uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD);
1325
1326			bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
1327			    BUS_DMASYNC_POSTREAD);
1328			res->status = tfd;
1329			res->error = tfd >> 8;
1330			res->lba_low = fis[4];
1331			res->lba_mid = fis[5];
1332			res->lba_high = fis[6];
1333			res->device = fis[7];
1334			res->lba_low_exp = fis[8];
1335			res->lba_mid_exp = fis[9];
1336			res->lba_high_exp = fis[10];
1337			res->sector_count = fis[12];
1338			res->sector_count_exp = fis[13];
1339		} else
1340			bzero(res, sizeof(*res));
1341	}
1342	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1343		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1344		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1345		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1346		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1347	}
1348	/* In case of error, freeze device for proper recovery. */
1349	if ((et != AHCI_ERR_NONE) && (!ch->readlog) &&
1350	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1351		xpt_freeze_devq(ccb->ccb_h.path, 1);
1352		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1353	}
1354	/* Set proper result status. */
1355	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1356	switch (et) {
1357	case AHCI_ERR_NONE:
1358		ccb->ccb_h.status |= CAM_REQ_CMP;
1359		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1360			ccb->csio.scsi_status = SCSI_STATUS_OK;
1361		break;
1362	case AHCI_ERR_INVALID:
1363		ccb->ccb_h.status |= CAM_REQ_INVALID;
1364		break;
1365	case AHCI_ERR_INNOCENT:
1366		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1367		break;
1368	case AHCI_ERR_TFE:
1369	case AHCI_ERR_NCQ:
1370		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1371			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1372			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1373		} else {
1374			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1375		}
1376		break;
1377	case AHCI_ERR_SATA:
1378		if (!ch->readlog) {
1379			xpt_freeze_simq(ch->sim, 1);
1380			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1381			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1382		}
1383		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1384		break;
1385	case AHCI_ERR_TIMEOUT:
1386		if (!ch->readlog) {
1387			xpt_freeze_simq(ch->sim, 1);
1388			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1389			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1390		}
1391		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1392		break;
1393	default:
1394		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1395	}
1396	/* Free slot. */
1397	ch->rslots &= ~(1 << slot->slot);
1398	ch->aslots &= ~(1 << slot->slot);
1399	slot->state = AHCI_SLOT_EMPTY;
1400	slot->ccb = NULL;
1401	/* Update channel stats. */
1402	ch->numrslots--;
1403	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1404	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1405		ch->numtslots--;
1406	}
1407	/* If it was first request of reset sequence and there is no error,
1408	 * proceed to second request. */
1409	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1410	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1411	    (ccb->ataio.cmd.control & ATA_A_RESET) &&
1412	    et == AHCI_ERR_NONE) {
1413		ccb->ataio.cmd.control &= ~ATA_A_RESET;
1414		ahci_begin_transaction(dev, ccb);
1415		return;
1416	}
1417	/* If it was NCQ command error, put result on hold. */
1418	if (et == AHCI_ERR_NCQ) {
1419		ch->hold[slot->slot] = ccb;
1420	} else if (ch->readlog)	/* If it was our READ LOG command - process it. */
1421		ahci_process_read_log(dev, ccb);
1422	else
1423		xpt_done(ccb);
1424	/* Unfreeze frozen command. */
1425	if (ch->frozen && ch->numrslots == 0) {
1426		union ccb *fccb = ch->frozen;
1427		ch->frozen = NULL;
1428		ahci_begin_transaction(dev, fccb);
1429		xpt_release_simq(ch->sim, TRUE);
1430	}
1431	/* Start PM timer. */
1432	if (ch->numrslots == 0 && ch->pm_level > 3) {
1433		callout_schedule(&ch->pm_timer,
1434		    (ch->pm_level == 4) ? hz / 1000 : hz / 8);
1435	}
1436}
1437
1438static void
1439ahci_issue_read_log(device_t dev)
1440{
1441	struct ahci_channel *ch = device_get_softc(dev);
1442	union ccb *ccb;
1443	struct ccb_ataio *ataio;
1444	int i;
1445
1446	ch->readlog = 1;
1447	/* Find some holden command. */
1448	for (i = 0; i < ch->numslots; i++) {
1449		if (ch->hold[i])
1450			break;
1451	}
1452	ccb = xpt_alloc_ccb_nowait();
1453	if (ccb == NULL) {
1454		device_printf(dev, "Unable allocate READ LOG command");
1455		return; /* XXX */
1456	}
1457	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1458	ccb->ccb_h.func_code = XPT_ATA_IO;
1459	ccb->ccb_h.flags = CAM_DIR_IN;
1460	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1461	ataio = &ccb->ataio;
1462	ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT);
1463	if (ataio->data_ptr == NULL) {
1464		device_printf(dev, "Unable allocate memory for READ LOG command");
1465		return; /* XXX */
1466	}
1467	ataio->dxfer_len = 512;
1468	bzero(&ataio->cmd, sizeof(ataio->cmd));
1469	ataio->cmd.flags = CAM_ATAIO_48BIT;
1470	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1471	ataio->cmd.sector_count = 1;
1472	ataio->cmd.sector_count_exp = 0;
1473	ataio->cmd.lba_low = 0x10;
1474	ataio->cmd.lba_mid = 0;
1475	ataio->cmd.lba_mid_exp = 0;
1476	/* Freeze SIM while doing READ LOG EXT. */
1477	xpt_freeze_simq(ch->sim, 1);
1478	ahci_begin_transaction(dev, ccb);
1479}
1480
1481static void
1482ahci_process_read_log(device_t dev, union ccb *ccb)
1483{
1484	struct ahci_channel *ch = device_get_softc(dev);
1485	uint8_t *data;
1486	struct ata_res *res;
1487	int i;
1488
1489	ch->readlog = 0;
1490
1491	data = ccb->ataio.data_ptr;
1492	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1493	    (data[0] & 0x80) == 0) {
1494		for (i = 0; i < ch->numslots; i++) {
1495			if (!ch->hold[i])
1496				continue;
1497			if ((data[0] & 0x1F) == i) {
1498				res = &ch->hold[i]->ataio.res;
1499				res->status = data[2];
1500				res->error = data[3];
1501				res->lba_low = data[4];
1502				res->lba_mid = data[5];
1503				res->lba_high = data[6];
1504				res->device = data[7];
1505				res->lba_low_exp = data[8];
1506				res->lba_mid_exp = data[9];
1507				res->lba_high_exp = data[10];
1508				res->sector_count = data[12];
1509				res->sector_count_exp = data[13];
1510			} else {
1511				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1512				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1513			}
1514			xpt_done(ch->hold[i]);
1515			ch->hold[i] = NULL;
1516		}
1517	} else {
1518		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1519			device_printf(dev, "Error while READ LOG EXT\n");
1520		else if ((data[0] & 0x80) == 0) {
1521			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1522		}
1523		for (i = 0; i < ch->numslots; i++) {
1524			if (!ch->hold[i])
1525				continue;
1526			xpt_done(ch->hold[i]);
1527			ch->hold[i] = NULL;
1528		}
1529	}
1530	free(ccb->ataio.data_ptr, M_AHCI);
1531	xpt_free_ccb(ccb);
1532	xpt_release_simq(ch->sim, TRUE);
1533}
1534
1535static void
1536ahci_start(device_t dev)
1537{
1538	struct ahci_channel *ch = device_get_softc(dev);
1539	u_int32_t cmd;
1540
1541	/* Clear SATA error register */
1542	ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF);
1543	/* Clear any interrupts pending on this channel */
1544	ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF);
1545	/* Start operations on this channel */
1546	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1547	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST |
1548	    (ch->pm_present ? AHCI_P_CMD_PMA : 0));
1549}
1550
1551static void
1552ahci_stop(device_t dev)
1553{
1554	struct ahci_channel *ch = device_get_softc(dev);
1555	u_int32_t cmd;
1556	int timeout;
1557
1558	/* Kill all activity on this channel */
1559	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1560	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST);
1561	/* Wait for activity stop. */
1562	timeout = 0;
1563	do {
1564		DELAY(1000);
1565		if (timeout++ > 1000) {
1566			device_printf(dev, "stopping AHCI engine failed\n");
1567			break;
1568		}
1569	} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR);
1570}
1571
1572static void
1573ahci_clo(device_t dev)
1574{
1575	struct ahci_channel *ch = device_get_softc(dev);
1576	u_int32_t cmd;
1577	int timeout;
1578
1579	/* Issue Command List Override if supported */
1580	if (ch->caps & AHCI_CAP_SCLO) {
1581		cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1582		cmd |= AHCI_P_CMD_CLO;
1583		ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd);
1584		timeout = 0;
1585		do {
1586			DELAY(1000);
1587			if (timeout++ > 1000) {
1588			    device_printf(dev, "executing CLO failed\n");
1589			    break;
1590			}
1591		} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO);
1592	}
1593}
1594
1595static void
1596ahci_stop_fr(device_t dev)
1597{
1598	struct ahci_channel *ch = device_get_softc(dev);
1599	u_int32_t cmd;
1600	int timeout;
1601
1602	/* Kill all FIS reception on this channel */
1603	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1604	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE);
1605	/* Wait for FIS reception stop. */
1606	timeout = 0;
1607	do {
1608		DELAY(1000);
1609		if (timeout++ > 1000) {
1610			device_printf(dev, "stopping AHCI FR engine failed\n");
1611			break;
1612		}
1613	} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR);
1614}
1615
1616static void
1617ahci_start_fr(device_t dev)
1618{
1619	struct ahci_channel *ch = device_get_softc(dev);
1620	u_int32_t cmd;
1621
1622	/* Start FIS reception on this channel */
1623	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1624	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE);
1625}
1626
1627static int
1628ahci_wait_ready(device_t dev, int t)
1629{
1630	struct ahci_channel *ch = device_get_softc(dev);
1631	int timeout = 0;
1632	uint32_t val;
1633
1634	while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) &
1635	    (ATA_S_BUSY | ATA_S_DRQ)) {
1636		DELAY(1000);
1637		if (timeout++ > t) {
1638			device_printf(dev, "port is not ready (timeout %dms) "
1639			    "tfd = %08x\n", t, val);
1640			return (EBUSY);
1641		}
1642	}
1643	if (bootverbose)
1644		device_printf(dev, "ready wait time=%dms\n", timeout);
1645	return (0);
1646}
1647
1648static void
1649ahci_reset(device_t dev)
1650{
1651	struct ahci_channel *ch = device_get_softc(dev);
1652	struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev));
1653	int i;
1654
1655	if (bootverbose)
1656		device_printf(dev, "AHCI reset...\n");
1657	/* Requeue freezed command. */
1658	if (ch->frozen) {
1659		union ccb *fccb = ch->frozen;
1660		ch->frozen = NULL;
1661		fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1662		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1663			xpt_freeze_devq(fccb->ccb_h.path, 1);
1664			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1665		}
1666		xpt_done(fccb);
1667	}
1668	/* Kill the engine and requeue all running commands. */
1669	ahci_stop(dev);
1670	for (i = 0; i < ch->numslots; i++) {
1671		/* Do we have a running request on slot? */
1672		if (ch->slot[i].state < AHCI_SLOT_RUNNING)
1673			continue;
1674		/* XXX; Commands in loading state. */
1675		ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
1676	}
1677	/* Tell the XPT about the event */
1678	xpt_async(AC_BUS_RESET, ch->path, NULL);
1679	/* Disable port interrupts */
1680	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
1681	/* Reset and reconnect PHY, */
1682	if (!ahci_sata_phy_reset(dev, 0)) {
1683		if (bootverbose)
1684			device_printf(dev,
1685			    "AHCI reset done: phy reset found no device\n");
1686		ch->devices = 0;
1687		/* Enable wanted port interrupts */
1688		ATA_OUTL(ch->r_mem, AHCI_P_IE,
1689		    (AHCI_P_IX_CPD | AHCI_P_IX_PRC | AHCI_P_IX_PC));
1690		return;
1691	}
1692	/* Wait for clearing busy status. */
1693	if (ahci_wait_ready(dev, 10000)) {
1694		device_printf(dev, "device ready timeout\n");
1695		ahci_clo(dev);
1696	}
1697	ahci_start(dev);
1698	ch->devices = 1;
1699	/* Enable wanted port interrupts */
1700	ATA_OUTL(ch->r_mem, AHCI_P_IE,
1701	     (AHCI_P_IX_CPD | AHCI_P_IX_TFE | AHCI_P_IX_HBF |
1702	      AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF |
1703	      ((ch->pm_level == 0) ? AHCI_P_IX_PRC | AHCI_P_IX_PC : 0) |
1704	      AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) |
1705	      AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR)));
1706	if (bootverbose)
1707		device_printf(dev, "AHCI reset done: device found\n");
1708}
1709
1710static int
1711ahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag)
1712{
1713	u_int8_t *fis = &ctp->cfis[0];
1714
1715	bzero(ctp->cfis, 64);
1716	fis[0] = 0x27;  		/* host to device */
1717	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1718	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1719		fis[1] |= 0x80;
1720		fis[2] = ATA_PACKET_CMD;
1721		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1722			fis[3] = ATA_F_DMA;
1723		else {
1724			fis[5] = ccb->csio.dxfer_len;
1725		        fis[6] = ccb->csio.dxfer_len >> 8;
1726		}
1727		fis[7] = ATA_D_LBA;
1728		fis[15] = ATA_A_4BIT;
1729		bzero(ctp->acmd, 32);
1730		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1731		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1732		    ctp->acmd, ccb->csio.cdb_len);
1733	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1734		fis[1] |= 0x80;
1735		fis[2] = ccb->ataio.cmd.command;
1736		fis[3] = ccb->ataio.cmd.features;
1737		fis[4] = ccb->ataio.cmd.lba_low;
1738		fis[5] = ccb->ataio.cmd.lba_mid;
1739		fis[6] = ccb->ataio.cmd.lba_high;
1740		fis[7] = ccb->ataio.cmd.device;
1741		fis[8] = ccb->ataio.cmd.lba_low_exp;
1742		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1743		fis[10] = ccb->ataio.cmd.lba_high_exp;
1744		fis[11] = ccb->ataio.cmd.features_exp;
1745		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1746			fis[12] = tag << 3;
1747			fis[13] = 0;
1748		} else {
1749			fis[12] = ccb->ataio.cmd.sector_count;
1750			fis[13] = ccb->ataio.cmd.sector_count_exp;
1751		}
1752		fis[15] = ATA_A_4BIT;
1753	} else {
1754		fis[15] = ccb->ataio.cmd.control;
1755	}
1756	return (20);
1757}
1758
1759static int
1760ahci_sata_connect(struct ahci_channel *ch)
1761{
1762	u_int32_t status;
1763	int timeout;
1764
1765	/* Wait up to 100ms for "connect well" */
1766	for (timeout = 0; timeout < 100 ; timeout++) {
1767		status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
1768		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1769		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1770		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1771			break;
1772		if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) {
1773			if (bootverbose) {
1774				device_printf(ch->dev, "SATA offline status=%08x\n",
1775				    status);
1776			}
1777			return (0);
1778		}
1779		DELAY(1000);
1780	}
1781	if (timeout >= 100) {
1782		if (bootverbose) {
1783			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1784			    status);
1785		}
1786		return (0);
1787	}
1788	if (bootverbose) {
1789		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1790		    timeout, status);
1791	}
1792	/* Clear SATA error register */
1793	ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff);
1794	return (1);
1795}
1796
1797static int
1798ahci_sata_phy_reset(device_t dev, int quick)
1799{
1800	struct ahci_channel *ch = device_get_softc(dev);
1801	uint32_t val;
1802
1803	if (quick) {
1804		val = ATA_INL(ch->r_mem, AHCI_P_SCTL);
1805		if ((val & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
1806			return (ahci_sata_connect(ch));
1807	}
1808
1809	if (bootverbose)
1810		device_printf(dev, "hardware reset ...\n");
1811	if (ch->sata_rev == 1)
1812		val = ATA_SC_SPD_SPEED_GEN1;
1813	else if (ch->sata_rev == 2)
1814		val = ATA_SC_SPD_SPEED_GEN2;
1815	else if (ch->sata_rev == 3)
1816		val = ATA_SC_SPD_SPEED_GEN3;
1817	else
1818		val = 0;
1819	ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
1820	    ATA_SC_DET_RESET | val |
1821	    ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER);
1822	DELAY(5000);
1823	ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
1824	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1825	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1826	DELAY(5000);
1827	return (ahci_sata_connect(ch));
1828}
1829
1830static void
1831ahciaction(struct cam_sim *sim, union ccb *ccb)
1832{
1833	device_t dev;
1834	struct ahci_channel *ch;
1835
1836	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n",
1837	    ccb->ccb_h.func_code));
1838
1839	ch = (struct ahci_channel *)cam_sim_softc(sim);
1840	dev = ch->dev;
1841	switch (ccb->ccb_h.func_code) {
1842	/* Common cases first */
1843	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1844	case XPT_SCSI_IO:
1845		if (ch->devices == 0) {
1846			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1847			xpt_done(ccb);
1848			break;
1849		}
1850		/* Check for command collision. */
1851		if (ahci_check_collision(dev, ccb)) {
1852			/* Freeze command. */
1853			ch->frozen = ccb;
1854			/* We have only one frozen slot, so freeze simq also. */
1855			xpt_freeze_simq(ch->sim, 1);
1856			return;
1857		}
1858		ahci_begin_transaction(dev, ccb);
1859		break;
1860	case XPT_EN_LUN:		/* Enable LUN as a target */
1861	case XPT_TARGET_IO:		/* Execute target I/O request */
1862	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1863	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1864	case XPT_ABORT:			/* Abort the specified CCB */
1865		/* XXX Implement */
1866		ccb->ccb_h.status = CAM_REQ_INVALID;
1867		xpt_done(ccb);
1868		break;
1869	case XPT_SET_TRAN_SETTINGS:
1870	{
1871		struct	ccb_trans_settings *cts = &ccb->cts;
1872
1873		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1874			ch->pm_present = cts->xport_specific.sata.pm_present;
1875		}
1876		ccb->ccb_h.status = CAM_REQ_CMP;
1877		xpt_done(ccb);
1878		break;
1879	}
1880	case XPT_GET_TRAN_SETTINGS:
1881	/* Get default/user set transfer settings for the target */
1882	{
1883		struct	ccb_trans_settings *cts = &ccb->cts;
1884		uint32_t status;
1885
1886		cts->protocol = PROTO_ATA;
1887		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1888		cts->transport = XPORT_SATA;
1889		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1890		cts->proto_specific.valid = 0;
1891		cts->xport_specific.sata.valid = 0;
1892		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1893			status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK;
1894		else
1895			status = ATA_INL(ch->r_mem, AHCI_P_SCTL) & ATA_SC_SPD_MASK;
1896		if (status & ATA_SS_SPD_GEN3) {
1897			cts->xport_specific.sata.bitrate = 600000;
1898			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1899		} else if (status & ATA_SS_SPD_GEN2) {
1900			cts->xport_specific.sata.bitrate = 300000;
1901			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1902		} else if (status & ATA_SS_SPD_GEN1) {
1903			cts->xport_specific.sata.bitrate = 150000;
1904			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1905		}
1906		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1907			cts->xport_specific.sata.pm_present =
1908			    (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_PMA) ?
1909			    1 : 0;
1910		} else {
1911			cts->xport_specific.sata.pm_present = ch->pm_present;
1912		}
1913		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1914		ccb->ccb_h.status = CAM_REQ_CMP;
1915		xpt_done(ccb);
1916		break;
1917	}
1918#if 0
1919	case XPT_CALC_GEOMETRY:
1920	{
1921		struct	  ccb_calc_geometry *ccg;
1922		uint32_t size_mb;
1923		uint32_t secs_per_cylinder;
1924
1925		ccg = &ccb->ccg;
1926		size_mb = ccg->volume_size
1927			/ ((1024L * 1024L) / ccg->block_size);
1928		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1929			if (size_mb >= 2048) {
1930				ccg->heads = 255;
1931				ccg->secs_per_track = 63;
1932			} else {
1933				ccg->heads = 128;
1934				ccg->secs_per_track = 32;
1935			}
1936		} else {
1937			ccg->heads = 64;
1938			ccg->secs_per_track = 32;
1939		}
1940		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1941		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1942		ccb->ccb_h.status = CAM_REQ_CMP;
1943		xpt_done(ccb);
1944		break;
1945	}
1946#endif
1947	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1948	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1949		ahci_reset(dev);
1950		ccb->ccb_h.status = CAM_REQ_CMP;
1951		xpt_done(ccb);
1952		break;
1953	case XPT_TERM_IO:		/* Terminate the I/O process */
1954		/* XXX Implement */
1955		ccb->ccb_h.status = CAM_REQ_INVALID;
1956		xpt_done(ccb);
1957		break;
1958	case XPT_PATH_INQ:		/* Path routing inquiry */
1959	{
1960		struct ccb_pathinq *cpi = &ccb->cpi;
1961
1962		cpi->version_num = 1; /* XXX??? */
1963		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1964		if (ch->caps & AHCI_CAP_SPM)
1965			cpi->hba_inquiry |= PI_SATAPM;
1966		cpi->target_sprt = 0;
1967		cpi->hba_misc = PIM_SEQSCAN;
1968		cpi->hba_eng_cnt = 0;
1969		if (ch->caps & AHCI_CAP_SPM)
1970			cpi->max_target = 14;
1971		else
1972			cpi->max_target = 0;
1973		cpi->max_lun = 0;
1974		cpi->initiator_id = 0;
1975		cpi->bus_id = cam_sim_bus(sim);
1976		cpi->base_transfer_speed = 150000;
1977		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1978		strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN);
1979		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1980		cpi->unit_number = cam_sim_unit(sim);
1981		cpi->transport = XPORT_SATA;
1982		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1983		cpi->protocol = PROTO_ATA;
1984		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1985		cpi->maxio = MAXPHYS;
1986		/* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */
1987		if (pci_get_devid(device_get_parent(dev)) == 0x43801002)
1988			cpi->maxio = min(cpi->maxio, 128 * 512);
1989		cpi->ccb_h.status = CAM_REQ_CMP;
1990		xpt_done(ccb);
1991		break;
1992	}
1993	default:
1994		ccb->ccb_h.status = CAM_REQ_INVALID;
1995		xpt_done(ccb);
1996		break;
1997	}
1998}
1999
2000static void
2001ahcipoll(struct cam_sim *sim)
2002{
2003	struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim);
2004
2005	ahci_ch_intr(ch->dev);
2006}
2007