ahci_generic.c revision 199178
1/*-
2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci.c 199178 2009-11-11 11:10:36Z mav $");
29
30#include <sys/param.h>
31#include <sys/module.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/ata.h>
35#include <sys/bus.h>
36#include <sys/endian.h>
37#include <sys/malloc.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/sema.h>
41#include <sys/taskqueue.h>
42#include <vm/uma.h>
43#include <machine/stdarg.h>
44#include <machine/resource.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#include <dev/pci/pcivar.h>
48#include <dev/pci/pcireg.h>
49#include "ahci.h"
50
51#include <cam/cam.h>
52#include <cam/cam_ccb.h>
53#include <cam/cam_sim.h>
54#include <cam/cam_xpt_sim.h>
55#include <cam/cam_xpt_periph.h>
56#include <cam/cam_debug.h>
57
58/* local prototypes */
59static int ahci_setup_interrupt(device_t dev);
60static void ahci_intr(void *data);
61static void ahci_intr_one(void *data);
62static int ahci_suspend(device_t dev);
63static int ahci_resume(device_t dev);
64static int ahci_ch_suspend(device_t dev);
65static int ahci_ch_resume(device_t dev);
66static void ahci_ch_pm(void *arg);
67static void ahci_ch_intr_locked(void *data);
68static void ahci_ch_intr(void *data);
69static int ahci_ctlr_reset(device_t dev);
70static void ahci_begin_transaction(device_t dev, union ccb *ccb);
71static void ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
72static void ahci_execute_transaction(struct ahci_slot *slot);
73static void ahci_timeout(struct ahci_slot *slot);
74static void ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et);
75static int ahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag);
76static void ahci_dmainit(device_t dev);
77static void ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
78static void ahci_dmafini(device_t dev);
79static void ahci_slotsalloc(device_t dev);
80static void ahci_slotsfree(device_t dev);
81static void ahci_reset(device_t dev);
82static void ahci_start(device_t dev);
83static void ahci_stop(device_t dev);
84static void ahci_clo(device_t dev);
85static void ahci_start_fr(device_t dev);
86static void ahci_stop_fr(device_t dev);
87
88static int ahci_sata_connect(struct ahci_channel *ch);
89static int ahci_sata_phy_reset(device_t dev, int quick);
90
91static void ahci_issue_read_log(device_t dev);
92static void ahci_process_read_log(device_t dev, union ccb *ccb);
93
94static void ahciaction(struct cam_sim *sim, union ccb *ccb);
95static void ahcipoll(struct cam_sim *sim);
96
97MALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driver data buffers");
98
99static struct {
100	uint32_t	id;
101	const char	*name;
102	int		flags;
103} ahci_ids[] = {
104	{0x43801002, "ATI IXP600",	0},
105	{0x43901002, "ATI IXP700",	0},
106	{0x43911002, "ATI IXP700",	0},
107	{0x43921002, "ATI IXP700",	0},
108	{0x43931002, "ATI IXP700",	0},
109	{0x43941002, "ATI IXP800",	0},
110	{0x43951002, "ATI IXP800",	0},
111	{0x26528086, "Intel ICH6",	0},
112	{0x26538086, "Intel ICH6M",	0},
113	{0x26818086, "Intel ESB2",	0},
114	{0x26828086, "Intel ESB2",	0},
115	{0x26838086, "Intel ESB2",	0},
116	{0x27c18086, "Intel ICH7",	0},
117	{0x27c38086, "Intel ICH7",	0},
118	{0x27c58086, "Intel ICH7M",	0},
119	{0x27c68086, "Intel ICH7M",	0},
120	{0x28218086, "Intel ICH8",	0},
121	{0x28228086, "Intel ICH8",	0},
122	{0x28248086, "Intel ICH8",	0},
123	{0x28298086, "Intel ICH8M",	0},
124	{0x282a8086, "Intel ICH8M",	0},
125	{0x29228086, "Intel ICH9",	0},
126	{0x29238086, "Intel ICH9",	0},
127	{0x29248086, "Intel ICH9",	0},
128	{0x29258086, "Intel ICH9",	0},
129	{0x29278086, "Intel ICH9",	0},
130	{0x29298086, "Intel ICH9M",	0},
131	{0x292a8086, "Intel ICH9M",	0},
132	{0x292b8086, "Intel ICH9M",	0},
133	{0x292c8086, "Intel ICH9M",	0},
134	{0x292f8086, "Intel ICH9M",	0},
135	{0x294d8086, "Intel ICH9",	0},
136	{0x294e8086, "Intel ICH9M",	0},
137	{0x3a058086, "Intel ICH10",	0},
138	{0x3a228086, "Intel ICH10",	0},
139	{0x3a258086, "Intel ICH10",	0},
140	{0x3b228086, "Intel PCH",	0},
141	{0x3b238086, "Intel PCH",	0},
142	{0x3b248086, "Intel PCH",	0},
143	{0x3b258086, "Intel PCH",	0},
144	{0x3b298086, "Intel PCH",	0},
145	{0x3b2b8086, "Intel PCH",	0},
146	{0x3b2c8086, "Intel PCH",	0},
147	{0x3b2f8086, "Intel PCH",	0},
148	{0x044c10de, "NVIDIA MCP65",	0},
149	{0x044d10de, "NVIDIA MCP65",	0},
150	{0x044e10de, "NVIDIA MCP65",	0},
151	{0x044f10de, "NVIDIA MCP65",	0},
152	{0x045c10de, "NVIDIA MCP65",	0},
153	{0x045d10de, "NVIDIA MCP65",	0},
154	{0x045e10de, "NVIDIA MCP65",	0},
155	{0x045f10de, "NVIDIA MCP65",	0},
156	{0x055010de, "NVIDIA MCP67",	0},
157	{0x055110de, "NVIDIA MCP67",	0},
158	{0x055210de, "NVIDIA MCP67",	0},
159	{0x055310de, "NVIDIA MCP67",	0},
160	{0x055410de, "NVIDIA MCP67",	0},
161	{0x055510de, "NVIDIA MCP67",	0},
162	{0x055610de, "NVIDIA MCP67",	0},
163	{0x055710de, "NVIDIA MCP67",	0},
164	{0x055810de, "NVIDIA MCP67",	0},
165	{0x055910de, "NVIDIA MCP67",	0},
166	{0x055A10de, "NVIDIA MCP67",	0},
167	{0x055B10de, "NVIDIA MCP67",	0},
168	{0x058410de, "NVIDIA MCP67",	0},
169	{0x07f010de, "NVIDIA MCP73",	0},
170	{0x07f110de, "NVIDIA MCP73",	0},
171	{0x07f210de, "NVIDIA MCP73",	0},
172	{0x07f310de, "NVIDIA MCP73",	0},
173	{0x07f410de, "NVIDIA MCP73",	0},
174	{0x07f510de, "NVIDIA MCP73",	0},
175	{0x07f610de, "NVIDIA MCP73",	0},
176	{0x07f710de, "NVIDIA MCP73",	0},
177	{0x07f810de, "NVIDIA MCP73",	0},
178	{0x07f910de, "NVIDIA MCP73",	0},
179	{0x07fa10de, "NVIDIA MCP73",	0},
180	{0x07fb10de, "NVIDIA MCP73",	0},
181	{0x0ad010de, "NVIDIA MCP77",	0},
182	{0x0ad110de, "NVIDIA MCP77",	0},
183	{0x0ad210de, "NVIDIA MCP77",	0},
184	{0x0ad310de, "NVIDIA MCP77",	0},
185	{0x0ad410de, "NVIDIA MCP77",	0},
186	{0x0ad510de, "NVIDIA MCP77",	0},
187	{0x0ad610de, "NVIDIA MCP77",	0},
188	{0x0ad710de, "NVIDIA MCP77",	0},
189	{0x0ad810de, "NVIDIA MCP77",	0},
190	{0x0ad910de, "NVIDIA MCP77",	0},
191	{0x0ada10de, "NVIDIA MCP77",	0},
192	{0x0adb10de, "NVIDIA MCP77",	0},
193	{0x0ab410de, "NVIDIA MCP79",	0},
194	{0x0ab510de, "NVIDIA MCP79",	0},
195	{0x0ab610de, "NVIDIA MCP79",	0},
196	{0x0ab710de, "NVIDIA MCP79",	0},
197	{0x0ab810de, "NVIDIA MCP79",	0},
198	{0x0ab910de, "NVIDIA MCP79",	0},
199	{0x0aba10de, "NVIDIA MCP79",	0},
200	{0x0abb10de, "NVIDIA MCP79",	0},
201	{0x0abc10de, "NVIDIA MCP79",	0},
202	{0x0abd10de, "NVIDIA MCP79",	0},
203	{0x0abe10de, "NVIDIA MCP79",	0},
204	{0x0abf10de, "NVIDIA MCP79",	0},
205	{0x0d8410de, "NVIDIA MCP89",	0},
206	{0x0d8510de, "NVIDIA MCP89",	0},
207	{0x0d8610de, "NVIDIA MCP89",	0},
208	{0x0d8710de, "NVIDIA MCP89",	0},
209	{0x0d8810de, "NVIDIA MCP89",	0},
210	{0x0d8910de, "NVIDIA MCP89",	0},
211	{0x0d8a10de, "NVIDIA MCP89",	0},
212	{0x0d8b10de, "NVIDIA MCP89",	0},
213	{0x0d8c10de, "NVIDIA MCP89",	0},
214	{0x0d8d10de, "NVIDIA MCP89",	0},
215	{0x0d8e10de, "NVIDIA MCP89",	0},
216	{0x0d8f10de, "NVIDIA MCP89",	0},
217	{0x33491106, "VIA VT8251",	0},
218	{0x62871106, "VIA VT8251",	0},
219	{0x11841039, "SiS 966",		0},
220	{0x11851039, "SiS 968",		0},
221	{0x01861039, "SiS 968",		0},
222	{0,	     NULL,		0}
223};
224
225static int
226ahci_probe(device_t dev)
227{
228	char buf[64];
229	int i;
230	uint32_t devid = pci_get_devid(dev);
231
232	/* Is this a known AHCI chip? */
233	for (i = 0; ahci_ids[i].id != 0; i++) {
234		if (ahci_ids[i].id == devid) {
235			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
236			    ahci_ids[i].name);
237			device_set_desc_copy(dev, buf);
238			return (BUS_PROBE_VENDOR);
239		}
240	}
241	/* Is this a possible AHCI candidate? */
242	if (pci_get_class(dev) != PCIC_STORAGE ||
243	    pci_get_subclass(dev) != PCIS_STORAGE_SATA ||
244	    pci_get_progif(dev) != PCIP_STORAGE_SATA_AHCI_1_0)
245		return (ENXIO);
246	device_set_desc_copy(dev, "AHCI SATA controller");
247	return (BUS_PROBE_VENDOR);
248}
249
250static int
251ahci_attach(device_t dev)
252{
253	struct ahci_controller *ctlr = device_get_softc(dev);
254	device_t child;
255	int	error, unit, speed;
256	u_int32_t version;
257
258	ctlr->dev = dev;
259	resource_int_value(device_get_name(dev),
260	    device_get_unit(dev), "ccc", &ctlr->ccc);
261	/* if we have a memory BAR(5) we are likely on an AHCI part */
262	ctlr->r_rid = PCIR_BAR(5);
263	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
264	    &ctlr->r_rid, RF_ACTIVE)))
265		return ENXIO;
266	/* Setup our own memory management for channels. */
267	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
268	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
269	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
270		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
271		return (error);
272	}
273	if ((error = rman_manage_region(&ctlr->sc_iomem,
274	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
275		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
276		rman_fini(&ctlr->sc_iomem);
277		return (error);
278	}
279	/* Reset controller */
280	if ((error = ahci_ctlr_reset(dev)) != 0) {
281		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
282		rman_fini(&ctlr->sc_iomem);
283		return (error);
284	};
285	/* Get the number of HW channels */
286	ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI);
287	ctlr->channels = MAX(flsl(ctlr->ichannels),
288	    (ATA_INL(ctlr->r_mem, AHCI_CAP) & AHCI_CAP_NPMASK) + 1);
289	/* Setup interrupts. */
290	if (ahci_setup_interrupt(dev)) {
291		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
292		rman_fini(&ctlr->sc_iomem);
293		return ENXIO;
294	}
295	/* Announce HW capabilities. */
296	version = ATA_INL(ctlr->r_mem, AHCI_VS);
297	ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP);
298	if (version >= 0x00010020)
299		ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2);
300	speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT;
301	device_printf(dev,
302		    "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s\n",
303		    ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f),
304		    ((version >> 4) & 0xf0) + (version & 0x0f),
305		    (ctlr->caps & AHCI_CAP_NPMASK) + 1,
306		    ((speed == 1) ? "1.5":((speed == 2) ? "3":
307		    ((speed == 3) ? "6":"?"))),
308		    (ctlr->caps & AHCI_CAP_SPM) ?
309		    "supported" : "not supported");
310	if (bootverbose) {
311		device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps",
312		    (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"",
313		    (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"",
314		    (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"",
315		    (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"",
316		    (ctlr->caps & AHCI_CAP_SSS) ? " SS":"",
317		    (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"",
318		    (ctlr->caps & AHCI_CAP_SAL) ? " AL":"",
319		    (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"",
320		    ((speed == 1) ? "1.5":((speed == 2) ? "3":
321		    ((speed == 3) ? "6":"?"))));
322		printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n",
323		    (ctlr->caps & AHCI_CAP_SAM) ? " AM":"",
324		    (ctlr->caps & AHCI_CAP_SPM) ? " PM":"",
325		    (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"",
326		    (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"",
327		    (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"",
328		    (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"",
329		    ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1,
330		    (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"",
331		    (ctlr->caps & AHCI_CAP_EMS) ? " EM":"",
332		    (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"",
333		    (ctlr->caps & AHCI_CAP_NPMASK) + 1);
334	}
335	if (bootverbose && version >= 0x00010020) {
336		device_printf(dev, "Caps2:%s%s%s\n",
337		    (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"",
338		    (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"",
339		    (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":"");
340	}
341	/* Attach all channels on this controller */
342	for (unit = 0; unit < ctlr->channels; unit++) {
343		if ((ctlr->ichannels & (1 << unit)) == 0)
344			continue;
345		child = device_add_child(dev, "ahcich", -1);
346		if (child == NULL)
347			device_printf(dev, "failed to add channel device\n");
348		else
349			device_set_ivars(child, (void *)(intptr_t)unit);
350	}
351	bus_generic_attach(dev);
352	return 0;
353}
354
355static int
356ahci_detach(device_t dev)
357{
358	struct ahci_controller *ctlr = device_get_softc(dev);
359	device_t *children;
360	int nchildren, i;
361
362	/* Detach & delete all children */
363	if (!device_get_children(dev, &children, &nchildren)) {
364		for (i = 0; i < nchildren; i++)
365			device_delete_child(dev, children[i]);
366		free(children, M_TEMP);
367	}
368	/* Free interrupts. */
369	for (i = 0; i < ctlr->numirqs; i++) {
370		if (ctlr->irqs[i].r_irq) {
371			bus_teardown_intr(dev, ctlr->irqs[i].r_irq,
372			    ctlr->irqs[i].handle);
373			bus_release_resource(dev, SYS_RES_IRQ,
374			    ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq);
375		}
376	}
377	pci_release_msi(dev);
378	/* Free memory. */
379	rman_fini(&ctlr->sc_iomem);
380	if (ctlr->r_mem)
381		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
382	return (0);
383}
384
385static int
386ahci_ctlr_reset(device_t dev)
387{
388	struct ahci_controller *ctlr = device_get_softc(dev);
389	int timeout;
390
391	if (pci_read_config(dev, 0x00, 4) == 0x28298086 &&
392	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
393		pci_write_config(dev, 0x92, 0x01, 1);
394	/* Enable AHCI mode */
395	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
396	/* Reset AHCI controller */
397	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR);
398	for (timeout = 1000; timeout > 0; timeout--) {
399		DELAY(1000);
400		if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0)
401			break;
402	}
403	if (timeout == 0) {
404		device_printf(dev, "AHCI controller reset failure\n");
405		return ENXIO;
406	}
407	/* Reenable AHCI mode */
408	ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE);
409	/* Clear interrupts */
410	ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS));
411	/* Configure CCC */
412	if (ctlr->ccc) {
413		ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI));
414		ATA_OUTL(ctlr->r_mem, AHCI_CCCC,
415		    (ctlr->ccc << AHCI_CCCC_TV_SHIFT) |
416		    (4 << AHCI_CCCC_CC_SHIFT) |
417		    AHCI_CCCC_EN);
418		ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) &
419		    AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT;
420		if (bootverbose) {
421			device_printf(dev,
422			    "CCC with %dms/4cmd enabled on vector %d\n",
423			    ctlr->ccc, ctlr->cccv);
424		}
425	}
426	/* Enable AHCI interrupts */
427	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
428	    ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE);
429	return (0);
430}
431
432static int
433ahci_suspend(device_t dev)
434{
435	struct ahci_controller *ctlr = device_get_softc(dev);
436
437	bus_generic_suspend(dev);
438	/* Disable interupts, so the state change(s) doesn't trigger */
439	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
440	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
441	return 0;
442}
443
444static int
445ahci_resume(device_t dev)
446{
447	int res;
448
449	if ((res = ahci_ctlr_reset(dev)) != 0)
450		return (res);
451	return (bus_generic_resume(dev));
452}
453
454static int
455ahci_setup_interrupt(device_t dev)
456{
457	struct ahci_controller *ctlr = device_get_softc(dev);
458	int i, msi = 1;
459
460	/* Process hints. */
461	resource_int_value(device_get_name(dev),
462	    device_get_unit(dev), "msi", &msi);
463	if (msi < 0)
464		msi = 0;
465	else if (msi == 1)
466		msi = min(1, pci_msi_count(dev));
467	else if (msi > 1)
468		msi = pci_msi_count(dev);
469	/* Allocate MSI if needed/present. */
470	if (msi && pci_alloc_msi(dev, &msi) == 0) {
471		ctlr->numirqs = msi;
472	} else {
473		msi = 0;
474		ctlr->numirqs = 1;
475	}
476	/* Check for single MSI vector fallback. */
477	if (ctlr->numirqs > 1 &&
478	    (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) {
479		device_printf(dev, "Falling back to one MSI\n");
480		ctlr->numirqs = 1;
481	}
482	/* Allocate all IRQs. */
483	for (i = 0; i < ctlr->numirqs; i++) {
484		ctlr->irqs[i].ctlr = ctlr;
485		ctlr->irqs[i].r_irq_rid = i + (msi ? 1 : 0);
486		if (ctlr->numirqs == 1 || i >= ctlr->channels ||
487		    (ctlr->ccc && i == ctlr->cccv))
488			ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL;
489		else if (i == ctlr->numirqs - 1)
490			ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER;
491		else
492			ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE;
493		if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
494		    &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
495			device_printf(dev, "unable to map interrupt\n");
496			return ENXIO;
497		}
498		if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL,
499		    (ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE) ? ahci_intr_one : ahci_intr,
500		    &ctlr->irqs[i], &ctlr->irqs[i].handle))) {
501			/* SOS XXX release r_irq */
502			device_printf(dev, "unable to setup interrupt\n");
503			return ENXIO;
504		}
505	}
506	return (0);
507}
508
509/*
510 * Common case interrupt handler.
511 */
512static void
513ahci_intr(void *data)
514{
515	struct ahci_controller_irq *irq = data;
516	struct ahci_controller *ctlr = irq->ctlr;
517	u_int32_t is;
518	void *arg;
519	int unit;
520
521	if (irq->mode == AHCI_IRQ_MODE_ALL) {
522		unit = 0;
523		if (ctlr->ccc)
524			is = ctlr->ichannels;
525		else
526			is = ATA_INL(ctlr->r_mem, AHCI_IS);
527	} else {	/* AHCI_IRQ_MODE_AFTER */
528		unit = irq->r_irq_rid - 1;
529		is = ATA_INL(ctlr->r_mem, AHCI_IS);
530	}
531	for (; unit < ctlr->channels; unit++) {
532		if ((is & (1 << unit)) != 0 &&
533		    (arg = ctlr->interrupt[unit].argument)) {
534			ctlr->interrupt[unit].function(arg);
535			ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit);
536		}
537	}
538}
539
540/*
541 * Simplified interrupt handler for multivector MSI mode.
542 */
543static void
544ahci_intr_one(void *data)
545{
546	struct ahci_controller_irq *irq = data;
547	struct ahci_controller *ctlr = irq->ctlr;
548	void *arg;
549	int unit;
550
551	unit = irq->r_irq_rid - 1;
552	if ((arg = ctlr->interrupt[unit].argument))
553	    ctlr->interrupt[unit].function(arg);
554}
555
556static struct resource *
557ahci_alloc_resource(device_t dev, device_t child, int type, int *rid,
558		       u_long start, u_long end, u_long count, u_int flags)
559{
560	struct ahci_controller *ctlr = device_get_softc(dev);
561	int unit = ((struct ahci_channel *)device_get_softc(child))->unit;
562	struct resource *res = NULL;
563	int offset = AHCI_OFFSET + (unit << 7);
564	long st;
565
566	switch (type) {
567	case SYS_RES_MEMORY:
568		st = rman_get_start(ctlr->r_mem);
569		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
570		    st + offset + 127, 128, RF_ACTIVE, child);
571		if (res) {
572			bus_space_handle_t bsh;
573			bus_space_tag_t bst;
574			bsh = rman_get_bushandle(ctlr->r_mem);
575			bst = rman_get_bustag(ctlr->r_mem);
576			bus_space_subregion(bst, bsh, offset, 128, &bsh);
577			rman_set_bushandle(res, bsh);
578			rman_set_bustag(res, bst);
579		}
580		break;
581	case SYS_RES_IRQ:
582		if (*rid == ATA_IRQ_RID)
583			res = ctlr->irqs[0].r_irq;
584		break;
585	}
586	return (res);
587}
588
589static int
590ahci_release_resource(device_t dev, device_t child, int type, int rid,
591			 struct resource *r)
592{
593
594	switch (type) {
595	case SYS_RES_MEMORY:
596		rman_release_resource(r);
597		return (0);
598	case SYS_RES_IRQ:
599		if (rid != ATA_IRQ_RID)
600			return ENOENT;
601		return (0);
602	}
603	return (EINVAL);
604}
605
606static int
607ahci_setup_intr(device_t dev, device_t child, struct resource *irq,
608		   int flags, driver_filter_t *filter, driver_intr_t *function,
609		   void *argument, void **cookiep)
610{
611	struct ahci_controller *ctlr = device_get_softc(dev);
612	int unit = (intptr_t)device_get_ivars(child);
613
614	if (filter != NULL) {
615		printf("ahci.c: we cannot use a filter here\n");
616		return (EINVAL);
617	}
618	ctlr->interrupt[unit].function = function;
619	ctlr->interrupt[unit].argument = argument;
620	return (0);
621}
622
623static int
624ahci_teardown_intr(device_t dev, device_t child, struct resource *irq,
625		      void *cookie)
626{
627	struct ahci_controller *ctlr = device_get_softc(dev);
628	int unit = (intptr_t)device_get_ivars(child);
629
630	ctlr->interrupt[unit].function = NULL;
631	ctlr->interrupt[unit].argument = NULL;
632	return (0);
633}
634
635static int
636ahci_print_child(device_t dev, device_t child)
637{
638	int retval;
639
640	retval = bus_print_child_header(dev, child);
641	retval += printf(" at channel %d",
642	    (int)(intptr_t)device_get_ivars(child));
643	retval += bus_print_child_footer(dev, child);
644
645	return (retval);
646}
647
648devclass_t ahci_devclass;
649static device_method_t ahci_methods[] = {
650	DEVMETHOD(device_probe,     ahci_probe),
651	DEVMETHOD(device_attach,    ahci_attach),
652	DEVMETHOD(device_detach,    ahci_detach),
653	DEVMETHOD(device_suspend,   ahci_suspend),
654	DEVMETHOD(device_resume,    ahci_resume),
655	DEVMETHOD(bus_print_child,  ahci_print_child),
656	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
657	DEVMETHOD(bus_release_resource,     ahci_release_resource),
658	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
659	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
660	{ 0, 0 }
661};
662static driver_t ahci_driver = {
663        "ahci",
664        ahci_methods,
665        sizeof(struct ahci_controller)
666};
667DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
668MODULE_VERSION(ahci, 1);
669MODULE_DEPEND(ahci, cam, 1, 1, 1);
670
671static int
672ahci_ch_probe(device_t dev)
673{
674
675	device_set_desc_copy(dev, "AHCI channel");
676	return (0);
677}
678
679static int
680ahci_ch_attach(device_t dev)
681{
682	struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev));
683	struct ahci_channel *ch = device_get_softc(dev);
684	struct cam_devq *devq;
685	int rid, error;
686
687	ch->dev = dev;
688	ch->unit = (intptr_t)device_get_ivars(dev);
689	ch->caps = ctlr->caps;
690	ch->caps2 = ctlr->caps2;
691	ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1,
692	mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF);
693	resource_int_value(device_get_name(dev),
694	    device_get_unit(dev), "pm_level", &ch->pm_level);
695	if (ch->pm_level > 3)
696		callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
697	/* Limit speed for my onboard JMicron external port.
698	 * It is not eSATA really. */
699	if (pci_get_devid(ctlr->dev) == 0x2363197b &&
700	    pci_get_subvendor(ctlr->dev) == 0x1043 &&
701	    pci_get_subdevice(ctlr->dev) == 0x81e4 &&
702	    ch->unit == 0)
703		ch->sata_rev = 1;
704	resource_int_value(device_get_name(dev),
705	    device_get_unit(dev), "sata_rev", &ch->sata_rev);
706	rid = ch->unit;
707	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
708	    &rid, RF_ACTIVE)))
709		return (ENXIO);
710	ahci_dmainit(dev);
711	ahci_slotsalloc(dev);
712	ahci_ch_resume(dev);
713	mtx_lock(&ch->mtx);
714	rid = ATA_IRQ_RID;
715	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
716	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
717		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
718		device_printf(dev, "Unable to map interrupt\n");
719		return (ENXIO);
720	}
721	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
722	    ahci_ch_intr_locked, dev, &ch->ih))) {
723		device_printf(dev, "Unable to setup interrupt\n");
724		error = ENXIO;
725		goto err1;
726	}
727	/* Create the device queue for our SIM. */
728	devq = cam_simq_alloc(ch->numslots);
729	if (devq == NULL) {
730		device_printf(dev, "Unable to allocate simq\n");
731		error = ENOMEM;
732		goto err1;
733	}
734	/* Construct SIM entry */
735	ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch,
736	    device_get_unit(dev), &ch->mtx,
737	    min(2, ch->numslots), ch->numslots, devq);
738	if (ch->sim == NULL) {
739		device_printf(dev, "unable to allocate sim\n");
740		error = ENOMEM;
741		goto err2;
742	}
743	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
744		device_printf(dev, "unable to register xpt bus\n");
745		error = ENXIO;
746		goto err2;
747	}
748	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
749	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
750		device_printf(dev, "unable to create path\n");
751		error = ENXIO;
752		goto err3;
753	}
754	if (ch->pm_level > 3) {
755		callout_reset(&ch->pm_timer,
756		    (ch->pm_level == 4) ? hz / 1000 : hz / 8,
757		    ahci_ch_pm, dev);
758	}
759	mtx_unlock(&ch->mtx);
760	return (0);
761
762err3:
763	xpt_bus_deregister(cam_sim_path(ch->sim));
764err2:
765	cam_sim_free(ch->sim, /*free_devq*/TRUE);
766err1:
767	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
768	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
769	mtx_unlock(&ch->mtx);
770	return (error);
771}
772
773static int
774ahci_ch_detach(device_t dev)
775{
776	struct ahci_channel *ch = device_get_softc(dev);
777
778	mtx_lock(&ch->mtx);
779	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
780	xpt_free_path(ch->path);
781	xpt_bus_deregister(cam_sim_path(ch->sim));
782	cam_sim_free(ch->sim, /*free_devq*/TRUE);
783	mtx_unlock(&ch->mtx);
784
785	if (ch->pm_level > 3)
786		callout_drain(&ch->pm_timer);
787	bus_teardown_intr(dev, ch->r_irq, ch->ih);
788	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
789
790	ahci_ch_suspend(dev);
791	ahci_slotsfree(dev);
792	ahci_dmafini(dev);
793
794	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
795	mtx_destroy(&ch->mtx);
796	return (0);
797}
798
799static int
800ahci_ch_suspend(device_t dev)
801{
802	struct ahci_channel *ch = device_get_softc(dev);
803
804	/* Disable port interrupts. */
805	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
806	/* Reset command register. */
807	ahci_stop(dev);
808	ahci_stop_fr(dev);
809	ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0);
810	/* Allow everything, including partial and slumber modes. */
811	ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0);
812	/* Request slumber mode transition and give some time to get there. */
813	ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER);
814	DELAY(100);
815	/* Disable PHY. */
816	ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE);
817	return (0);
818}
819
820static int
821ahci_ch_resume(device_t dev)
822{
823	struct ahci_channel *ch = device_get_softc(dev);
824	uint64_t work;
825
826	/* Disable port interrupts */
827	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
828	/* Setup work areas */
829	work = ch->dma.work_bus + AHCI_CL_OFFSET;
830	ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff);
831	ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32);
832	work = ch->dma.rfis_bus;
833	ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff);
834	ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32);
835	/* Activate the channel and power/spin up device */
836	ATA_OUTL(ch->r_mem, AHCI_P_CMD,
837	     (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
838	     ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) |
839	     ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 )));
840	ahci_start_fr(dev);
841	ahci_start(dev);
842	return (0);
843}
844
845devclass_t ahcich_devclass;
846static device_method_t ahcich_methods[] = {
847	DEVMETHOD(device_probe,     ahci_ch_probe),
848	DEVMETHOD(device_attach,    ahci_ch_attach),
849	DEVMETHOD(device_detach,    ahci_ch_detach),
850	DEVMETHOD(device_suspend,   ahci_ch_suspend),
851	DEVMETHOD(device_resume,    ahci_ch_resume),
852	{ 0, 0 }
853};
854static driver_t ahcich_driver = {
855        "ahcich",
856        ahcich_methods,
857        sizeof(struct ahci_channel)
858};
859DRIVER_MODULE(ahcich, ahci, ahcich_driver, ahci_devclass, 0, 0);
860
861struct ahci_dc_cb_args {
862	bus_addr_t maddr;
863	int error;
864};
865
866static void
867ahci_dmainit(device_t dev)
868{
869	struct ahci_channel *ch = device_get_softc(dev);
870	struct ahci_dc_cb_args dcba;
871
872	if (ch->caps & AHCI_CAP_64BIT)
873		ch->dma.max_address = BUS_SPACE_MAXADDR;
874	else
875		ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
876	/* Command area. */
877	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
878	    ch->dma.max_address, BUS_SPACE_MAXADDR,
879	    NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE,
880	    0, NULL, NULL, &ch->dma.work_tag))
881		goto error;
882	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
883	    &ch->dma.work_map))
884		goto error;
885	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
886	    AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) {
887		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
888		goto error;
889	}
890	ch->dma.work_bus = dcba.maddr;
891	/* FIS receive area. */
892	if (bus_dma_tag_create(bus_get_dma_tag(dev), 4096, 0,
893	    ch->dma.max_address, BUS_SPACE_MAXADDR,
894	    NULL, NULL, 4096, 1, 4096,
895	    0, NULL, NULL, &ch->dma.rfis_tag))
896		goto error;
897	if (bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0,
898	    &ch->dma.rfis_map))
899		goto error;
900	if (bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis,
901	    4096, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) {
902		bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
903		goto error;
904	}
905	ch->dma.rfis_bus = dcba.maddr;
906	/* Data area. */
907	if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
908	    ch->dma.max_address, BUS_SPACE_MAXADDR,
909	    NULL, NULL,
910	    AHCI_SG_ENTRIES * PAGE_SIZE * ch->numslots,
911	    AHCI_SG_ENTRIES, AHCI_PRD_MAX,
912	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
913		goto error;
914	}
915	return;
916
917error:
918	device_printf(dev, "WARNING - DMA initialization failed\n");
919	ahci_dmafini(dev);
920}
921
922static void
923ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
924{
925	struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc;
926
927	if (!(dcba->error = error))
928		dcba->maddr = segs[0].ds_addr;
929}
930
931static void
932ahci_dmafini(device_t dev)
933{
934	struct ahci_channel *ch = device_get_softc(dev);
935
936	if (ch->dma.data_tag) {
937		bus_dma_tag_destroy(ch->dma.data_tag);
938		ch->dma.data_tag = NULL;
939	}
940	if (ch->dma.rfis_bus) {
941		bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map);
942		bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map);
943		ch->dma.rfis_bus = 0;
944		ch->dma.rfis_map = NULL;
945		ch->dma.rfis = NULL;
946	}
947	if (ch->dma.work_bus) {
948		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
949		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
950		ch->dma.work_bus = 0;
951		ch->dma.work_map = NULL;
952		ch->dma.work = NULL;
953	}
954	if (ch->dma.work_tag) {
955		bus_dma_tag_destroy(ch->dma.work_tag);
956		ch->dma.work_tag = NULL;
957	}
958}
959
960static void
961ahci_slotsalloc(device_t dev)
962{
963	struct ahci_channel *ch = device_get_softc(dev);
964	int i;
965
966	/* Alloc and setup command/dma slots */
967	bzero(ch->slot, sizeof(ch->slot));
968	for (i = 0; i < ch->numslots; i++) {
969		struct ahci_slot *slot = &ch->slot[i];
970
971		slot->dev = dev;
972		slot->slot = i;
973		slot->state = AHCI_SLOT_EMPTY;
974		slot->ccb = NULL;
975		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
976
977		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
978			device_printf(ch->dev, "FAILURE - create data_map\n");
979	}
980}
981
982static void
983ahci_slotsfree(device_t dev)
984{
985	struct ahci_channel *ch = device_get_softc(dev);
986	int i;
987
988	/* Free all dma slots */
989	for (i = 0; i < ch->numslots; i++) {
990		struct ahci_slot *slot = &ch->slot[i];
991
992		callout_drain(&slot->timeout);
993		if (slot->dma.data_map) {
994			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
995			slot->dma.data_map = NULL;
996		}
997	}
998}
999
1000static void
1001ahci_phy_check_events(device_t dev, u_int32_t serr)
1002{
1003	struct ahci_channel *ch = device_get_softc(dev);
1004
1005	if ((serr & ATA_SE_PHY_CHANGED) && (ch->pm_level == 0)) {
1006		u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
1007		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1008		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1009		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
1010			if (bootverbose)
1011				device_printf(dev, "CONNECT requested\n");
1012			ahci_reset(dev);
1013		} else {
1014			if (bootverbose)
1015				device_printf(dev, "DISCONNECT requested\n");
1016			ch->devices = 0;
1017		}
1018	}
1019}
1020
1021static void
1022ahci_notify_events(device_t dev, u_int32_t status)
1023{
1024	struct ahci_channel *ch = device_get_softc(dev);
1025	struct cam_path *dpath;
1026	int i;
1027
1028	ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status);
1029	if (bootverbose)
1030		device_printf(dev, "SNTF 0x%04x\n", status);
1031	for (i = 0; i < 16; i++) {
1032		if ((status & (1 << i)) == 0)
1033			continue;
1034		if (xpt_create_path(&dpath, NULL,
1035		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
1036			xpt_async(AC_SCSI_AEN, dpath, NULL);
1037			xpt_free_path(dpath);
1038		}
1039	}
1040}
1041
1042static void
1043ahci_ch_intr_locked(void *data)
1044{
1045	device_t dev = (device_t)data;
1046	struct ahci_channel *ch = device_get_softc(dev);
1047
1048	mtx_lock(&ch->mtx);
1049	ahci_ch_intr(data);
1050	mtx_unlock(&ch->mtx);
1051}
1052
1053static void
1054ahci_ch_pm(void *arg)
1055{
1056	device_t dev = (device_t)arg;
1057	struct ahci_channel *ch = device_get_softc(dev);
1058	uint32_t work;
1059
1060	if (ch->numrslots != 0)
1061		return;
1062	work = ATA_INL(ch->r_mem, AHCI_P_CMD);
1063	if (ch->pm_level == 4)
1064		work |= AHCI_P_CMD_PARTIAL;
1065	else
1066		work |= AHCI_P_CMD_SLUMBER;
1067	ATA_OUTL(ch->r_mem, AHCI_P_CMD, work);
1068}
1069
1070static void
1071ahci_ch_intr(void *data)
1072{
1073	device_t dev = (device_t)data;
1074	struct ahci_channel *ch = device_get_softc(dev);
1075	uint32_t istatus, sstatus, cstatus, serr = 0, sntf = 0, ok, err;
1076	enum ahci_err_type et;
1077	int i, ccs, ncq_err = 0;
1078
1079	/* Read and clear interrupt statuses. */
1080	istatus = ATA_INL(ch->r_mem, AHCI_P_IS);
1081	if (istatus == 0)
1082		return;
1083	ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus);
1084	/* Read command statuses. */
1085	sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
1086	cstatus = ATA_INL(ch->r_mem, AHCI_P_CI);
1087	if ((istatus & AHCI_P_IX_SDB) && (ch->caps & AHCI_CAP_SSNTF))
1088		sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF);
1089	/* Process PHY events */
1090	if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF |
1091	    AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
1092		serr = ATA_INL(ch->r_mem, AHCI_P_SERR);
1093		if (serr) {
1094			ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr);
1095			ahci_phy_check_events(dev, serr);
1096		}
1097	}
1098	/* Process command errors */
1099	if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF |
1100	    AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) {
1101//device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n",
1102//    __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD),
1103//    serr);
1104		ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK)
1105		    >> AHCI_P_CMD_CCS_SHIFT;
1106		err = ch->rslots & (cstatus | sstatus);
1107		/* Kick controller into sane state */
1108		ahci_stop(dev);
1109		ahci_start(dev);
1110	} else {
1111		ccs = 0;
1112		err = 0;
1113	}
1114	/* Complete all successfull commands. */
1115	ok = ch->rslots & ~(cstatus | sstatus);
1116	for (i = 0; i < ch->numslots; i++) {
1117		if ((ok >> i) & 1)
1118			ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE);
1119	}
1120	/* On error, complete the rest of commands with error statuses. */
1121	if (err) {
1122		if (ch->frozen) {
1123			union ccb *fccb = ch->frozen;
1124			ch->frozen = NULL;
1125			fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1126			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1127				xpt_freeze_devq(fccb->ccb_h.path, 1);
1128				fccb->ccb_h.status |= CAM_DEV_QFRZN;
1129			}
1130			xpt_done(fccb);
1131		}
1132		for (i = 0; i < ch->numslots; i++) {
1133			/* XXX: reqests in loading state. */
1134			if (((err >> i) & 1) == 0)
1135				continue;
1136			if (istatus & AHCI_P_IX_TFE) {
1137				/* Task File Error */
1138				if (ch->numtslots == 0) {
1139					/* Untagged operation. */
1140					if (i == ccs)
1141						et = AHCI_ERR_TFE;
1142					else
1143						et = AHCI_ERR_INNOCENT;
1144				} else {
1145					/* Tagged operation. */
1146					et = AHCI_ERR_NCQ;
1147					ncq_err = 1;
1148				}
1149			} else if (istatus & AHCI_P_IX_IF) {
1150				if (ch->numtslots == 0 && i != ccs)
1151					et = AHCI_ERR_INNOCENT;
1152				else
1153					et = AHCI_ERR_SATA;
1154			} else
1155				et = AHCI_ERR_INVALID;
1156			ahci_end_transaction(&ch->slot[i], et);
1157		}
1158		if (ncq_err)
1159			ahci_issue_read_log(dev);
1160	}
1161	/* Process NOTIFY events */
1162	if (sntf)
1163		ahci_notify_events(dev, sntf);
1164}
1165
1166/* Must be called with channel locked. */
1167static int
1168ahci_check_collision(device_t dev, union ccb *ccb)
1169{
1170	struct ahci_channel *ch = device_get_softc(dev);
1171
1172	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1173	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1174		/* Tagged command while untagged are active. */
1175		if (ch->numrslots != 0 && ch->numtslots == 0)
1176			return (1);
1177		/* Tagged command while tagged to other target is active. */
1178		if (ch->numtslots != 0 &&
1179		    ch->taggedtarget != ccb->ccb_h.target_id)
1180			return (1);
1181	} else {
1182		/* Untagged command while tagged are active. */
1183		if (ch->numrslots != 0 && ch->numtslots != 0)
1184			return (1);
1185	}
1186	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1187	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
1188		/* Atomic command while anything active. */
1189		if (ch->numrslots != 0)
1190			return (1);
1191	}
1192       /* We have some atomic command running. */
1193       if (ch->aslots != 0)
1194               return (1);
1195	return (0);
1196}
1197
1198/* Must be called with channel locked. */
1199static void
1200ahci_begin_transaction(device_t dev, union ccb *ccb)
1201{
1202	struct ahci_channel *ch = device_get_softc(dev);
1203	struct ahci_slot *slot;
1204	int tag;
1205
1206	/* Choose empty slot. */
1207	tag = ch->lastslot;
1208	while (ch->slot[tag].state != AHCI_SLOT_EMPTY) {
1209		if (++tag >= ch->numslots)
1210			tag = 0;
1211		KASSERT(tag != ch->lastslot, ("ahci: ALL SLOTS BUSY!"));
1212	}
1213	ch->lastslot = tag;
1214	/* Occupy chosen slot. */
1215	slot = &ch->slot[tag];
1216	slot->ccb = ccb;
1217	/* Stop PM timer. */
1218	if (ch->numrslots == 0 && ch->pm_level > 3)
1219		callout_stop(&ch->pm_timer);
1220	/* Update channel stats. */
1221	ch->numrslots++;
1222	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1223	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1224		ch->numtslots++;
1225		ch->taggedtarget = ccb->ccb_h.target_id;
1226	}
1227	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1228	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
1229		ch->aslots |= (1 << slot->slot);
1230	slot->dma.nsegs = 0;
1231	/* If request moves data, setup and load SG list */
1232	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1233		void *buf;
1234		bus_size_t size;
1235
1236		slot->state = AHCI_SLOT_LOADING;
1237		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1238			buf = ccb->ataio.data_ptr;
1239			size = ccb->ataio.dxfer_len;
1240		} else {
1241			buf = ccb->csio.data_ptr;
1242			size = ccb->csio.dxfer_len;
1243		}
1244		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
1245		    buf, size, ahci_dmasetprd, slot, 0);
1246	} else
1247		ahci_execute_transaction(slot);
1248}
1249
1250/* Locked by busdma engine. */
1251static void
1252ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1253{
1254	struct ahci_slot *slot = arg;
1255	struct ahci_channel *ch = device_get_softc(slot->dev);
1256	struct ahci_cmd_tab *ctp;
1257	struct ahci_dma_prd *prd;
1258	int i;
1259
1260	if (error) {
1261		device_printf(slot->dev, "DMA load error\n");
1262		ahci_end_transaction(slot, AHCI_ERR_INVALID);
1263		return;
1264	}
1265	KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n"));
1266	/* Get a piece of the workspace for this request */
1267	ctp = (struct ahci_cmd_tab *)
1268		(ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot));
1269	/* Fill S/G table */
1270	prd = &ctp->prd_tab[0];
1271	for (i = 0; i < nsegs; i++) {
1272		prd[i].dba = htole64(segs[i].ds_addr);
1273		prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK);
1274	}
1275	slot->dma.nsegs = nsegs;
1276	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1277	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1278	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1279	ahci_execute_transaction(slot);
1280}
1281
1282/* Must be called with channel locked. */
1283static void
1284ahci_execute_transaction(struct ahci_slot *slot)
1285{
1286	device_t dev = slot->dev;
1287	struct ahci_channel *ch = device_get_softc(dev);
1288	struct ahci_cmd_tab *ctp;
1289	struct ahci_cmd_list *clp;
1290	union ccb *ccb = slot->ccb;
1291	int port = ccb->ccb_h.target_id & 0x0f;
1292	int fis_size;
1293
1294	/* Get a piece of the workspace for this request */
1295	ctp = (struct ahci_cmd_tab *)
1296		(ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot));
1297	/* Setup the FIS for this request */
1298	if (!(fis_size = ahci_setup_fis(ctp, ccb, slot->slot))) {
1299		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1300		ahci_end_transaction(slot, AHCI_ERR_INVALID);
1301		return;
1302	}
1303	/* Setup the command list entry */
1304	clp = (struct ahci_cmd_list *)
1305	    (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot));
1306	clp->prd_length = slot->dma.nsegs;
1307	clp->cmd_flags = (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) |
1308		     (ccb->ccb_h.func_code == XPT_SCSI_IO ?
1309		      (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) |
1310		     (fis_size / sizeof(u_int32_t)) |
1311		     (port << 12);
1312	/* Special handling for Soft Reset command. */
1313	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1314	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1315	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1316		/* Kick controller into sane state */
1317		ahci_stop(dev);
1318		ahci_clo(dev);
1319		ahci_start(dev);
1320		clp->cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY;
1321	}
1322	clp->bytecount = 0;
1323	clp->cmd_table_phys = htole64(ch->dma.work_bus + AHCI_CT_OFFSET +
1324				  (AHCI_CT_SIZE * slot->slot));
1325	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1326	    BUS_DMASYNC_PREWRITE);
1327	bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
1328	    BUS_DMASYNC_PREREAD);
1329	/* Set ACTIVE bit for NCQ commands. */
1330	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1331	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1332		ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot);
1333	}
1334	/* Issue command to the controller. */
1335	slot->state = AHCI_SLOT_RUNNING;
1336	ch->rslots |= (1 << slot->slot);
1337	ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot));
1338	/* Device reset commands doesn't interrupt. Poll them. */
1339	if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1340	    (ccb->ataio.cmd.command == ATA_DEVICE_RESET ||
1341	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL))) {
1342		int count, timeout = ccb->ccb_h.timeout;
1343		enum ahci_err_type et = AHCI_ERR_NONE;
1344
1345		for (count = 0; count < timeout; count++) {
1346			DELAY(1000);
1347			if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot)))
1348				break;
1349			if (ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) {
1350				device_printf(ch->dev,
1351				    "Poll error on slot %d, TFD: %04x\n",
1352				    slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD));
1353				et = AHCI_ERR_TFE;
1354				break;
1355			}
1356			/* Workaround for ATI SB600/SB700 chipsets. */
1357			if (ccb->ccb_h.target_id == 15 &&
1358			    pci_get_vendor(device_get_parent(dev)) == 0x1002 &&
1359			    (ATA_INL(ch->r_mem, AHCI_P_IS) & AHCI_P_IX_IPM)) {
1360				et = AHCI_ERR_TIMEOUT;
1361				break;
1362			}
1363		}
1364		if (timeout && (count >= timeout)) {
1365			device_printf(ch->dev,
1366			    "Poll timeout on slot %d\n", slot->slot);
1367			et = AHCI_ERR_TIMEOUT;
1368		}
1369		if (et != AHCI_ERR_NONE) {
1370			/* Kick controller into sane state */
1371			ahci_stop(ch->dev);
1372			ahci_start(ch->dev);
1373		}
1374		ahci_end_transaction(slot, et);
1375		return;
1376	}
1377	/* Start command execution timeout */
1378	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 2000,
1379	    (timeout_t*)ahci_timeout, slot);
1380	return;
1381}
1382
1383/* Locked by callout mechanism. */
1384static void
1385ahci_timeout(struct ahci_slot *slot)
1386{
1387	device_t dev = slot->dev;
1388	struct ahci_channel *ch = device_get_softc(dev);
1389	uint32_t sstatus;
1390	int ccs;
1391	int i;
1392
1393	/* Check for stale timeout. */
1394	if (slot->state < AHCI_SLOT_RUNNING)
1395		return;
1396
1397	/* Check if slot was not being executed last time we checked. */
1398	if (slot->state < AHCI_SLOT_EXECUTING) {
1399		/* Check if slot started executing. */
1400		sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT);
1401		ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK)
1402		    >> AHCI_P_CMD_CCS_SHIFT;
1403		if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot)
1404			slot->state = AHCI_SLOT_EXECUTING;
1405
1406		callout_reset(&slot->timeout,
1407		    (int)slot->ccb->ccb_h.timeout * hz / 2000,
1408		    (timeout_t*)ahci_timeout, slot);
1409		return;
1410	}
1411
1412	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1413	device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n",
1414	    ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI),
1415	    ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots,
1416	    ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR));
1417
1418	ch->fatalerr = 1;
1419	/* Handle frozen command. */
1420	if (ch->frozen) {
1421		union ccb *fccb = ch->frozen;
1422		ch->frozen = NULL;
1423		fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1424		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1425			xpt_freeze_devq(fccb->ccb_h.path, 1);
1426			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1427		}
1428		xpt_done(fccb);
1429	}
1430	/* Handle command with timeout. */
1431	ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT);
1432	/* Handle the rest of commands. */
1433	for (i = 0; i < ch->numslots; i++) {
1434		/* Do we have a running request on slot? */
1435		if (ch->slot[i].state < AHCI_SLOT_RUNNING)
1436			continue;
1437		ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
1438	}
1439}
1440
1441/* Must be called with channel locked. */
1442static void
1443ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et)
1444{
1445	device_t dev = slot->dev;
1446	struct ahci_channel *ch = device_get_softc(dev);
1447	union ccb *ccb = slot->ccb;
1448
1449	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1450	    BUS_DMASYNC_POSTWRITE);
1451	/* Read result registers to the result struct
1452	 * May be incorrect if several commands finished same time,
1453	 * so read only when sure or have to.
1454	 */
1455	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1456		struct ata_res *res = &ccb->ataio.res;
1457
1458		if ((et == AHCI_ERR_TFE) ||
1459		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1460			u_int8_t *fis = ch->dma.rfis + 0x40;
1461			uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD);
1462
1463			bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map,
1464			    BUS_DMASYNC_POSTREAD);
1465			res->status = tfd;
1466			res->error = tfd >> 8;
1467			res->lba_low = fis[4];
1468			res->lba_mid = fis[5];
1469			res->lba_high = fis[6];
1470			res->device = fis[7];
1471			res->lba_low_exp = fis[8];
1472			res->lba_mid_exp = fis[9];
1473			res->lba_high_exp = fis[10];
1474			res->sector_count = fis[12];
1475			res->sector_count_exp = fis[13];
1476		} else
1477			bzero(res, sizeof(*res));
1478	}
1479	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1480		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1481		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1482		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1483		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1484	}
1485	/* In case of error, freeze device for proper recovery. */
1486	if ((et != AHCI_ERR_NONE) && (!ch->readlog) &&
1487	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1488		xpt_freeze_devq(ccb->ccb_h.path, 1);
1489		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1490	}
1491	/* Set proper result status. */
1492	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1493	switch (et) {
1494	case AHCI_ERR_NONE:
1495		ccb->ccb_h.status |= CAM_REQ_CMP;
1496		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1497			ccb->csio.scsi_status = SCSI_STATUS_OK;
1498		break;
1499	case AHCI_ERR_INVALID:
1500		ch->fatalerr = 1;
1501		ccb->ccb_h.status |= CAM_REQ_INVALID;
1502		break;
1503	case AHCI_ERR_INNOCENT:
1504		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1505		break;
1506	case AHCI_ERR_TFE:
1507	case AHCI_ERR_NCQ:
1508		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1509			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1510			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1511		} else {
1512			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1513		}
1514		break;
1515	case AHCI_ERR_SATA:
1516		ch->fatalerr = 1;
1517		if (!ch->readlog) {
1518			xpt_freeze_simq(ch->sim, 1);
1519			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1520			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1521		}
1522		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1523		break;
1524	case AHCI_ERR_TIMEOUT:
1525		/* Do no treat soft-reset timeout as fatal here. */
1526		if (ccb->ccb_h.func_code != XPT_ATA_IO ||
1527	            !(ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL))
1528			ch->fatalerr = 1;
1529		if (!ch->readlog) {
1530			xpt_freeze_simq(ch->sim, 1);
1531			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1532			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1533		}
1534		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1535		break;
1536	default:
1537		ch->fatalerr = 1;
1538		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1539	}
1540	/* Free slot. */
1541	ch->rslots &= ~(1 << slot->slot);
1542	ch->aslots &= ~(1 << slot->slot);
1543	slot->state = AHCI_SLOT_EMPTY;
1544	slot->ccb = NULL;
1545	/* Update channel stats. */
1546	ch->numrslots--;
1547	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1548	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1549		ch->numtslots--;
1550	}
1551	/* If it was first request of reset sequence and there is no error,
1552	 * proceed to second request. */
1553	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1554	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1555	    (ccb->ataio.cmd.control & ATA_A_RESET) &&
1556	    et == AHCI_ERR_NONE) {
1557		ccb->ataio.cmd.control &= ~ATA_A_RESET;
1558		ahci_begin_transaction(dev, ccb);
1559		return;
1560	}
1561	/* If it was our READ LOG command - process it. */
1562	if (ch->readlog) {
1563		ahci_process_read_log(dev, ccb);
1564	/* If it was NCQ command error, put result on hold. */
1565	} else if (et == AHCI_ERR_NCQ) {
1566		ch->hold[slot->slot] = ccb;
1567	} else
1568		xpt_done(ccb);
1569	/* Unfreeze frozen command. */
1570	if (ch->frozen && ch->numrslots == 0) {
1571		union ccb *fccb = ch->frozen;
1572		ch->frozen = NULL;
1573		ahci_begin_transaction(dev, fccb);
1574		xpt_release_simq(ch->sim, TRUE);
1575	}
1576	/* If we have no other active commands, ... */
1577	if (ch->rslots == 0) {
1578		/* if there was fatal error - reset port. */
1579		if (ch->fatalerr) {
1580			ahci_reset(dev);
1581		}
1582	}
1583	/* Start PM timer. */
1584	if (ch->numrslots == 0 && ch->pm_level > 3) {
1585		callout_schedule(&ch->pm_timer,
1586		    (ch->pm_level == 4) ? hz / 1000 : hz / 8);
1587	}
1588}
1589
1590static void
1591ahci_issue_read_log(device_t dev)
1592{
1593	struct ahci_channel *ch = device_get_softc(dev);
1594	union ccb *ccb;
1595	struct ccb_ataio *ataio;
1596	int i;
1597
1598	ch->readlog = 1;
1599	/* Find some holden command. */
1600	for (i = 0; i < ch->numslots; i++) {
1601		if (ch->hold[i])
1602			break;
1603	}
1604	ccb = xpt_alloc_ccb_nowait();
1605	if (ccb == NULL) {
1606		device_printf(dev, "Unable allocate READ LOG command");
1607		return; /* XXX */
1608	}
1609	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1610	ccb->ccb_h.func_code = XPT_ATA_IO;
1611	ccb->ccb_h.flags = CAM_DIR_IN;
1612	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1613	ataio = &ccb->ataio;
1614	ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT);
1615	if (ataio->data_ptr == NULL) {
1616		device_printf(dev, "Unable allocate memory for READ LOG command");
1617		return; /* XXX */
1618	}
1619	ataio->dxfer_len = 512;
1620	bzero(&ataio->cmd, sizeof(ataio->cmd));
1621	ataio->cmd.flags = CAM_ATAIO_48BIT;
1622	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1623	ataio->cmd.sector_count = 1;
1624	ataio->cmd.sector_count_exp = 0;
1625	ataio->cmd.lba_low = 0x10;
1626	ataio->cmd.lba_mid = 0;
1627	ataio->cmd.lba_mid_exp = 0;
1628	/* Freeze SIM while doing READ LOG EXT. */
1629	xpt_freeze_simq(ch->sim, 1);
1630	ahci_begin_transaction(dev, ccb);
1631}
1632
1633static void
1634ahci_process_read_log(device_t dev, union ccb *ccb)
1635{
1636	struct ahci_channel *ch = device_get_softc(dev);
1637	uint8_t *data;
1638	struct ata_res *res;
1639	int i;
1640
1641	ch->readlog = 0;
1642
1643	data = ccb->ataio.data_ptr;
1644	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1645	    (data[0] & 0x80) == 0) {
1646		for (i = 0; i < ch->numslots; i++) {
1647			if (!ch->hold[i])
1648				continue;
1649			if ((data[0] & 0x1F) == i) {
1650				res = &ch->hold[i]->ataio.res;
1651				res->status = data[2];
1652				res->error = data[3];
1653				res->lba_low = data[4];
1654				res->lba_mid = data[5];
1655				res->lba_high = data[6];
1656				res->device = data[7];
1657				res->lba_low_exp = data[8];
1658				res->lba_mid_exp = data[9];
1659				res->lba_high_exp = data[10];
1660				res->sector_count = data[12];
1661				res->sector_count_exp = data[13];
1662			} else {
1663				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1664				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1665			}
1666			xpt_done(ch->hold[i]);
1667			ch->hold[i] = NULL;
1668		}
1669	} else {
1670		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1671			device_printf(dev, "Error while READ LOG EXT\n");
1672		else if ((data[0] & 0x80) == 0) {
1673			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1674		}
1675		for (i = 0; i < ch->numslots; i++) {
1676			if (!ch->hold[i])
1677				continue;
1678			xpt_done(ch->hold[i]);
1679			ch->hold[i] = NULL;
1680		}
1681	}
1682	free(ccb->ataio.data_ptr, M_AHCI);
1683	xpt_free_ccb(ccb);
1684	xpt_release_simq(ch->sim, TRUE);
1685}
1686
1687static void
1688ahci_start(device_t dev)
1689{
1690	struct ahci_channel *ch = device_get_softc(dev);
1691	u_int32_t cmd;
1692
1693	/* Clear SATA error register */
1694	ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF);
1695	/* Clear any interrupts pending on this channel */
1696	ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF);
1697	/* Start operations on this channel */
1698	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1699	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST |
1700	    (ch->pm_present ? AHCI_P_CMD_PMA : 0));
1701}
1702
1703static void
1704ahci_stop(device_t dev)
1705{
1706	struct ahci_channel *ch = device_get_softc(dev);
1707	u_int32_t cmd;
1708	int timeout;
1709
1710	/* Kill all activity on this channel */
1711	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1712	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST);
1713	/* Wait for activity stop. */
1714	timeout = 0;
1715	do {
1716		DELAY(1000);
1717		if (timeout++ > 1000) {
1718			device_printf(dev, "stopping AHCI engine failed\n");
1719			break;
1720		}
1721	} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR);
1722}
1723
1724static void
1725ahci_clo(device_t dev)
1726{
1727	struct ahci_channel *ch = device_get_softc(dev);
1728	u_int32_t cmd;
1729	int timeout;
1730
1731	/* Issue Command List Override if supported */
1732	if (ch->caps & AHCI_CAP_SCLO) {
1733		cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1734		cmd |= AHCI_P_CMD_CLO;
1735		ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd);
1736		timeout = 0;
1737		do {
1738			DELAY(1000);
1739			if (timeout++ > 1000) {
1740			    device_printf(dev, "executing CLO failed\n");
1741			    break;
1742			}
1743		} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO);
1744	}
1745}
1746
1747static void
1748ahci_stop_fr(device_t dev)
1749{
1750	struct ahci_channel *ch = device_get_softc(dev);
1751	u_int32_t cmd;
1752	int timeout;
1753
1754	/* Kill all FIS reception on this channel */
1755	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1756	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE);
1757	/* Wait for FIS reception stop. */
1758	timeout = 0;
1759	do {
1760		DELAY(1000);
1761		if (timeout++ > 1000) {
1762			device_printf(dev, "stopping AHCI FR engine failed\n");
1763			break;
1764		}
1765	} while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR);
1766}
1767
1768static void
1769ahci_start_fr(device_t dev)
1770{
1771	struct ahci_channel *ch = device_get_softc(dev);
1772	u_int32_t cmd;
1773
1774	/* Start FIS reception on this channel */
1775	cmd = ATA_INL(ch->r_mem, AHCI_P_CMD);
1776	ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE);
1777}
1778
1779static int
1780ahci_wait_ready(device_t dev, int t)
1781{
1782	struct ahci_channel *ch = device_get_softc(dev);
1783	int timeout = 0;
1784	uint32_t val;
1785
1786	while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) &
1787	    (ATA_S_BUSY | ATA_S_DRQ)) {
1788		DELAY(1000);
1789		if (timeout++ > t) {
1790			device_printf(dev, "port is not ready (timeout %dms) "
1791			    "tfd = %08x\n", t, val);
1792			return (EBUSY);
1793		}
1794	}
1795	if (bootverbose)
1796		device_printf(dev, "ready wait time=%dms\n", timeout);
1797	return (0);
1798}
1799
1800static void
1801ahci_reset(device_t dev)
1802{
1803	struct ahci_channel *ch = device_get_softc(dev);
1804	struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev));
1805	int i;
1806
1807	if (bootverbose)
1808		device_printf(dev, "AHCI reset...\n");
1809	/* Requeue freezed command. */
1810	if (ch->frozen) {
1811		union ccb *fccb = ch->frozen;
1812		ch->frozen = NULL;
1813		fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1814		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1815			xpt_freeze_devq(fccb->ccb_h.path, 1);
1816			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1817		}
1818		xpt_done(fccb);
1819	}
1820	/* Kill the engine and requeue all running commands. */
1821	ahci_stop(dev);
1822	for (i = 0; i < ch->numslots; i++) {
1823		/* Do we have a running request on slot? */
1824		if (ch->slot[i].state < AHCI_SLOT_RUNNING)
1825			continue;
1826		/* XXX; Commands in loading state. */
1827		ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT);
1828	}
1829	for (i = 0; i < ch->numslots; i++) {
1830		if (!ch->hold[i])
1831			continue;
1832		xpt_done(ch->hold[i]);
1833		ch->hold[i] = NULL;
1834	}
1835	ch->fatalerr = 0;
1836	/* Tell the XPT about the event */
1837	xpt_async(AC_BUS_RESET, ch->path, NULL);
1838	/* Disable port interrupts */
1839	ATA_OUTL(ch->r_mem, AHCI_P_IE, 0);
1840	/* Reset and reconnect PHY, */
1841	if (!ahci_sata_phy_reset(dev, 0)) {
1842		if (bootverbose)
1843			device_printf(dev,
1844			    "AHCI reset done: phy reset found no device\n");
1845		ch->devices = 0;
1846		/* Enable wanted port interrupts */
1847		ATA_OUTL(ch->r_mem, AHCI_P_IE,
1848		    (AHCI_P_IX_CPD | AHCI_P_IX_PRC | AHCI_P_IX_PC));
1849		return;
1850	}
1851	/* Wait for clearing busy status. */
1852	if (ahci_wait_ready(dev, 10000)) {
1853		device_printf(dev, "device ready timeout\n");
1854		ahci_clo(dev);
1855	}
1856	ahci_start(dev);
1857	ch->devices = 1;
1858	/* Enable wanted port interrupts */
1859	ATA_OUTL(ch->r_mem, AHCI_P_IE,
1860	     (AHCI_P_IX_CPD | AHCI_P_IX_TFE | AHCI_P_IX_HBF |
1861	      AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF |
1862	      ((ch->pm_level == 0) ? AHCI_P_IX_PRC | AHCI_P_IX_PC : 0) |
1863	      AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) |
1864	      AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR)));
1865	if (bootverbose)
1866		device_printf(dev, "AHCI reset done: device found\n");
1867}
1868
1869static int
1870ahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag)
1871{
1872	u_int8_t *fis = &ctp->cfis[0];
1873
1874	bzero(ctp->cfis, 64);
1875	fis[0] = 0x27;  		/* host to device */
1876	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1877	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1878		fis[1] |= 0x80;
1879		fis[2] = ATA_PACKET_CMD;
1880		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1881			fis[3] = ATA_F_DMA;
1882		else {
1883			fis[5] = ccb->csio.dxfer_len;
1884		        fis[6] = ccb->csio.dxfer_len >> 8;
1885		}
1886		fis[7] = ATA_D_LBA;
1887		fis[15] = ATA_A_4BIT;
1888		bzero(ctp->acmd, 32);
1889		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1890		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1891		    ctp->acmd, ccb->csio.cdb_len);
1892	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1893		fis[1] |= 0x80;
1894		fis[2] = ccb->ataio.cmd.command;
1895		fis[3] = ccb->ataio.cmd.features;
1896		fis[4] = ccb->ataio.cmd.lba_low;
1897		fis[5] = ccb->ataio.cmd.lba_mid;
1898		fis[6] = ccb->ataio.cmd.lba_high;
1899		fis[7] = ccb->ataio.cmd.device;
1900		fis[8] = ccb->ataio.cmd.lba_low_exp;
1901		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1902		fis[10] = ccb->ataio.cmd.lba_high_exp;
1903		fis[11] = ccb->ataio.cmd.features_exp;
1904		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1905			fis[12] = tag << 3;
1906			fis[13] = 0;
1907		} else {
1908			fis[12] = ccb->ataio.cmd.sector_count;
1909			fis[13] = ccb->ataio.cmd.sector_count_exp;
1910		}
1911		fis[15] = ATA_A_4BIT;
1912	} else {
1913		fis[15] = ccb->ataio.cmd.control;
1914	}
1915	return (20);
1916}
1917
1918static int
1919ahci_sata_connect(struct ahci_channel *ch)
1920{
1921	u_int32_t status;
1922	int timeout;
1923
1924	/* Wait up to 100ms for "connect well" */
1925	for (timeout = 0; timeout < 100 ; timeout++) {
1926		status = ATA_INL(ch->r_mem, AHCI_P_SSTS);
1927		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1928		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1929		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1930			break;
1931		if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) {
1932			if (bootverbose) {
1933				device_printf(ch->dev, "SATA offline status=%08x\n",
1934				    status);
1935			}
1936			return (0);
1937		}
1938		DELAY(1000);
1939	}
1940	if (timeout >= 100) {
1941		if (bootverbose) {
1942			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1943			    status);
1944		}
1945		return (0);
1946	}
1947	if (bootverbose) {
1948		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1949		    timeout, status);
1950	}
1951	/* Clear SATA error register */
1952	ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff);
1953	return (1);
1954}
1955
1956static int
1957ahci_sata_phy_reset(device_t dev, int quick)
1958{
1959	struct ahci_channel *ch = device_get_softc(dev);
1960	uint32_t val;
1961
1962	if (quick) {
1963		val = ATA_INL(ch->r_mem, AHCI_P_SCTL);
1964		if ((val & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
1965			return (ahci_sata_connect(ch));
1966	}
1967
1968	if (bootverbose)
1969		device_printf(dev, "hardware reset ...\n");
1970	if (ch->sata_rev == 1)
1971		val = ATA_SC_SPD_SPEED_GEN1;
1972	else if (ch->sata_rev == 2)
1973		val = ATA_SC_SPD_SPEED_GEN2;
1974	else if (ch->sata_rev == 3)
1975		val = ATA_SC_SPD_SPEED_GEN3;
1976	else
1977		val = 0;
1978	ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
1979	    ATA_SC_DET_RESET | val |
1980	    ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER);
1981	DELAY(5000);
1982	ATA_OUTL(ch->r_mem, AHCI_P_SCTL,
1983	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1984	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1985	DELAY(5000);
1986	return (ahci_sata_connect(ch));
1987}
1988
1989static void
1990ahciaction(struct cam_sim *sim, union ccb *ccb)
1991{
1992	device_t dev;
1993	struct ahci_channel *ch;
1994
1995	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n",
1996	    ccb->ccb_h.func_code));
1997
1998	ch = (struct ahci_channel *)cam_sim_softc(sim);
1999	dev = ch->dev;
2000	switch (ccb->ccb_h.func_code) {
2001	/* Common cases first */
2002	case XPT_ATA_IO:	/* Execute the requested I/O operation */
2003	case XPT_SCSI_IO:
2004		if (ch->devices == 0) {
2005			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2006			xpt_done(ccb);
2007			break;
2008		}
2009		/* Check for command collision. */
2010		if (ahci_check_collision(dev, ccb)) {
2011			/* Freeze command. */
2012			ch->frozen = ccb;
2013			/* We have only one frozen slot, so freeze simq also. */
2014			xpt_freeze_simq(ch->sim, 1);
2015			return;
2016		}
2017		ahci_begin_transaction(dev, ccb);
2018		break;
2019	case XPT_EN_LUN:		/* Enable LUN as a target */
2020	case XPT_TARGET_IO:		/* Execute target I/O request */
2021	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
2022	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
2023	case XPT_ABORT:			/* Abort the specified CCB */
2024		/* XXX Implement */
2025		ccb->ccb_h.status = CAM_REQ_INVALID;
2026		xpt_done(ccb);
2027		break;
2028	case XPT_SET_TRAN_SETTINGS:
2029	{
2030		struct	ccb_trans_settings *cts = &ccb->cts;
2031
2032		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
2033			ch->pm_present = cts->xport_specific.sata.pm_present;
2034		}
2035		ccb->ccb_h.status = CAM_REQ_CMP;
2036		xpt_done(ccb);
2037		break;
2038	}
2039	case XPT_GET_TRAN_SETTINGS:
2040	/* Get default/user set transfer settings for the target */
2041	{
2042		struct	ccb_trans_settings *cts = &ccb->cts;
2043		uint32_t status;
2044
2045		cts->protocol = PROTO_ATA;
2046		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
2047		cts->transport = XPORT_SATA;
2048		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
2049		cts->proto_specific.valid = 0;
2050		cts->xport_specific.sata.valid = 0;
2051		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
2052			status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK;
2053		else
2054			status = ATA_INL(ch->r_mem, AHCI_P_SCTL) & ATA_SC_SPD_MASK;
2055		if (status & ATA_SS_SPD_GEN3) {
2056			cts->xport_specific.sata.bitrate = 600000;
2057			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
2058		} else if (status & ATA_SS_SPD_GEN2) {
2059			cts->xport_specific.sata.bitrate = 300000;
2060			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
2061		} else if (status & ATA_SS_SPD_GEN1) {
2062			cts->xport_specific.sata.bitrate = 150000;
2063			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
2064		}
2065		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
2066			cts->xport_specific.sata.pm_present =
2067			    (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_PMA) ?
2068			    1 : 0;
2069		} else {
2070			cts->xport_specific.sata.pm_present = ch->pm_present;
2071		}
2072		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
2073		ccb->ccb_h.status = CAM_REQ_CMP;
2074		xpt_done(ccb);
2075		break;
2076	}
2077#if 0
2078	case XPT_CALC_GEOMETRY:
2079	{
2080		struct	  ccb_calc_geometry *ccg;
2081		uint32_t size_mb;
2082		uint32_t secs_per_cylinder;
2083
2084		ccg = &ccb->ccg;
2085		size_mb = ccg->volume_size
2086			/ ((1024L * 1024L) / ccg->block_size);
2087		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
2088			if (size_mb >= 2048) {
2089				ccg->heads = 255;
2090				ccg->secs_per_track = 63;
2091			} else {
2092				ccg->heads = 128;
2093				ccg->secs_per_track = 32;
2094			}
2095		} else {
2096			ccg->heads = 64;
2097			ccg->secs_per_track = 32;
2098		}
2099		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2100		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2101		ccb->ccb_h.status = CAM_REQ_CMP;
2102		xpt_done(ccb);
2103		break;
2104	}
2105#endif
2106	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2107	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
2108		ahci_reset(dev);
2109		ccb->ccb_h.status = CAM_REQ_CMP;
2110		xpt_done(ccb);
2111		break;
2112	case XPT_TERM_IO:		/* Terminate the I/O process */
2113		/* XXX Implement */
2114		ccb->ccb_h.status = CAM_REQ_INVALID;
2115		xpt_done(ccb);
2116		break;
2117	case XPT_PATH_INQ:		/* Path routing inquiry */
2118	{
2119		struct ccb_pathinq *cpi = &ccb->cpi;
2120
2121		cpi->version_num = 1; /* XXX??? */
2122		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
2123		if (ch->caps & AHCI_CAP_SPM)
2124			cpi->hba_inquiry |= PI_SATAPM;
2125		cpi->target_sprt = 0;
2126		cpi->hba_misc = PIM_SEQSCAN;
2127		cpi->hba_eng_cnt = 0;
2128		if (ch->caps & AHCI_CAP_SPM)
2129			cpi->max_target = 15;
2130		else
2131			cpi->max_target = 0;
2132		cpi->max_lun = 0;
2133		cpi->initiator_id = 0;
2134		cpi->bus_id = cam_sim_bus(sim);
2135		cpi->base_transfer_speed = 150000;
2136		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2137		strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN);
2138		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2139		cpi->unit_number = cam_sim_unit(sim);
2140		cpi->transport = XPORT_SATA;
2141		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
2142		cpi->protocol = PROTO_ATA;
2143		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
2144		cpi->maxio = MAXPHYS;
2145		/* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */
2146		if (pci_get_devid(device_get_parent(dev)) == 0x43801002)
2147			cpi->maxio = min(cpi->maxio, 128 * 512);
2148		cpi->ccb_h.status = CAM_REQ_CMP;
2149		xpt_done(ccb);
2150		break;
2151	}
2152	default:
2153		ccb->ccb_h.status = CAM_REQ_INVALID;
2154		xpt_done(ccb);
2155		break;
2156	}
2157}
2158
2159static void
2160ahcipoll(struct cam_sim *sim)
2161{
2162	struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim);
2163
2164	ahci_ch_intr(ch->dev);
2165}
2166