siis.c revision 205358
1195801Smav/*-
2195801Smav * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3195801Smav * All rights reserved.
4195801Smav *
5195801Smav * Redistribution and use in source and binary forms, with or without
6195801Smav * modification, are permitted provided that the following conditions
7195801Smav * are met:
8195801Smav * 1. Redistributions of source code must retain the above copyright
9195801Smav *    notice, this list of conditions and the following disclaimer,
10195801Smav *    without modification, immediately at the beginning of the file.
11195801Smav * 2. Redistributions in binary form must reproduce the above copyright
12195801Smav *    notice, this list of conditions and the following disclaimer in the
13195801Smav *    documentation and/or other materials provided with the distribution.
14195801Smav *
15195801Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16195801Smav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17195801Smav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18195801Smav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19195801Smav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20195801Smav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21195801Smav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22195801Smav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23195801Smav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24195801Smav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25195801Smav */
26195801Smav
27195801Smav#include <sys/cdefs.h>
28195801Smav__FBSDID("$FreeBSD: head/sys/dev/siis/siis.c 205358 2010-03-20 04:40:15Z mav $");
29195801Smav
30195801Smav#include <sys/param.h>
31195801Smav#include <sys/module.h>
32195801Smav#include <sys/systm.h>
33195801Smav#include <sys/kernel.h>
34195801Smav#include <sys/ata.h>
35195801Smav#include <sys/bus.h>
36195801Smav#include <sys/endian.h>
37195801Smav#include <sys/malloc.h>
38195801Smav#include <sys/lock.h>
39195801Smav#include <sys/mutex.h>
40195801Smav#include <sys/sema.h>
41195801Smav#include <sys/taskqueue.h>
42195801Smav#include <vm/uma.h>
43195801Smav#include <machine/stdarg.h>
44195801Smav#include <machine/resource.h>
45195801Smav#include <machine/bus.h>
46195801Smav#include <sys/rman.h>
47195801Smav#include <dev/pci/pcivar.h>
48195801Smav#include <dev/pci/pcireg.h>
49195801Smav#include "siis.h"
50195801Smav
51195801Smav#include <cam/cam.h>
52195801Smav#include <cam/cam_ccb.h>
53195801Smav#include <cam/cam_sim.h>
54195801Smav#include <cam/cam_xpt_sim.h>
55195801Smav#include <cam/cam_debug.h>
56195801Smav
57195801Smav/* local prototypes */
58195801Smavstatic int siis_setup_interrupt(device_t dev);
59195801Smavstatic void siis_intr(void *data);
60195801Smavstatic int siis_suspend(device_t dev);
61195801Smavstatic int siis_resume(device_t dev);
62195801Smavstatic int siis_ch_suspend(device_t dev);
63195801Smavstatic int siis_ch_resume(device_t dev);
64195801Smavstatic void siis_ch_intr_locked(void *data);
65195801Smavstatic void siis_ch_intr(void *data);
66195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb);
67195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
68195801Smavstatic void siis_execute_transaction(struct siis_slot *slot);
69195801Smavstatic void siis_timeout(struct siis_slot *slot);
70195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
71199821Smavstatic int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag);
72195801Smavstatic void siis_dmainit(device_t dev);
73195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
74195801Smavstatic void siis_dmafini(device_t dev);
75195801Smavstatic void siis_slotsalloc(device_t dev);
76195801Smavstatic void siis_slotsfree(device_t dev);
77195801Smavstatic void siis_reset(device_t dev);
78195801Smavstatic void siis_portinit(device_t dev);
79195801Smavstatic int siis_wait_ready(device_t dev, int t);
80195801Smav
81195801Smavstatic int siis_sata_connect(struct siis_channel *ch);
82195801Smav
83195801Smavstatic void siis_issue_read_log(device_t dev);
84195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb);
85195801Smav
86195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb);
87195801Smavstatic void siispoll(struct cam_sim *sim);
88195801Smav
89195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
90195801Smav
91199132Smavstatic struct {
92199132Smav	uint32_t	id;
93199132Smav	const char	*name;
94199132Smav	int		ports;
95200217Smav	int		quirks;
96200217Smav#define SIIS_Q_SNTF	1
97205358Smav#define SIIS_Q_NOMSI	2
98199132Smav} siis_ids[] = {
99200217Smav	{0x31241095,	"SiI3124",	4,	0},
100200217Smav	{0x31248086,	"SiI3124",	4,	0},
101205358Smav	{0x31321095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
102205358Smav	{0x02421095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
103205358Smav	{0x02441095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
104205358Smav	{0x31311095,	"SiI3131",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
105205358Smav	{0x35311095,	"SiI3531",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
106200217Smav	{0,		NULL,		0,	0}
107199132Smav};
108199132Smav
109195801Smavstatic int
110195801Smavsiis_probe(device_t dev)
111195801Smav{
112199132Smav	char buf[64];
113199132Smav	int i;
114195801Smav	uint32_t devid = pci_get_devid(dev);
115195801Smav
116199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
117199132Smav		if (siis_ids[i].id == devid) {
118200217Smav			snprintf(buf, sizeof(buf), "%s SATA controller",
119199132Smav			    siis_ids[i].name);
120199132Smav			device_set_desc_copy(dev, buf);
121199132Smav			return (BUS_PROBE_VENDOR);
122199132Smav		}
123195801Smav	}
124199132Smav	return (ENXIO);
125195801Smav}
126195801Smav
127195801Smavstatic int
128195801Smavsiis_attach(device_t dev)
129195801Smav{
130195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
131195801Smav	uint32_t devid = pci_get_devid(dev);
132195801Smav	device_t child;
133199132Smav	int	error, i, unit;
134195801Smav
135200217Smav	ctlr->dev = dev;
136199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
137199132Smav		if (siis_ids[i].id == devid)
138199132Smav			break;
139199132Smav	}
140200217Smav	ctlr->quirks = siis_ids[i].quirks;
141195801Smav	/* Global memory */
142195801Smav	ctlr->r_grid = PCIR_BAR(0);
143195801Smav	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
144195801Smav	    &ctlr->r_grid, RF_ACTIVE)))
145195801Smav		return (ENXIO);
146200223Smav	ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL);
147195801Smav	/* Channels memory */
148195801Smav	ctlr->r_rid = PCIR_BAR(2);
149195801Smav	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
150195801Smav	    &ctlr->r_rid, RF_ACTIVE)))
151195801Smav		return (ENXIO);
152195801Smav	/* Setup our own memory management for channels. */
153195801Smav	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
154195801Smav	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
155195801Smav	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
156195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
157195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
158195801Smav		return (error);
159195801Smav	}
160195801Smav	if ((error = rman_manage_region(&ctlr->sc_iomem,
161195801Smav	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
162195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
163195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
164195801Smav		rman_fini(&ctlr->sc_iomem);
165195801Smav		return (error);
166195801Smav	}
167195801Smav	/* Reset controller */
168195801Smav	siis_resume(dev);
169195801Smav	/* Number of HW channels */
170199132Smav	ctlr->channels = siis_ids[i].ports;
171195801Smav	/* Setup interrupts. */
172195801Smav	if (siis_setup_interrupt(dev)) {
173195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
174195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
175195801Smav		rman_fini(&ctlr->sc_iomem);
176195801Smav		return ENXIO;
177195801Smav	}
178195801Smav	/* Attach all channels on this controller */
179195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
180195801Smav		child = device_add_child(dev, "siisch", -1);
181195801Smav		if (child == NULL)
182195801Smav			device_printf(dev, "failed to add channel device\n");
183195801Smav		else
184195801Smav			device_set_ivars(child, (void *)(intptr_t)unit);
185195801Smav	}
186195801Smav	bus_generic_attach(dev);
187195801Smav	return 0;
188195801Smav}
189195801Smav
190195801Smavstatic int
191195801Smavsiis_detach(device_t dev)
192195801Smav{
193195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
194195801Smav	device_t *children;
195195801Smav	int nchildren, i;
196195801Smav
197195801Smav	/* Detach & delete all children */
198195801Smav	if (!device_get_children(dev, &children, &nchildren)) {
199195801Smav		for (i = 0; i < nchildren; i++)
200195801Smav			device_delete_child(dev, children[i]);
201195801Smav		free(children, M_TEMP);
202195801Smav	}
203195801Smav	/* Free interrupts. */
204195801Smav	if (ctlr->irq.r_irq) {
205195801Smav		bus_teardown_intr(dev, ctlr->irq.r_irq,
206195801Smav		    ctlr->irq.handle);
207195801Smav		bus_release_resource(dev, SYS_RES_IRQ,
208195801Smav		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
209195801Smav	}
210195801Smav	pci_release_msi(dev);
211195801Smav	/* Free memory. */
212195801Smav	rman_fini(&ctlr->sc_iomem);
213195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
214195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
215195801Smav	return (0);
216195801Smav}
217195801Smav
218195801Smavstatic int
219195801Smavsiis_suspend(device_t dev)
220195801Smav{
221195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
222195801Smav
223195801Smav	bus_generic_suspend(dev);
224195801Smav	/* Put controller into reset state. */
225200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
226200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
227195801Smav	return 0;
228195801Smav}
229195801Smav
230195801Smavstatic int
231195801Smavsiis_resume(device_t dev)
232195801Smav{
233195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
234195801Smav
235200291Smav	/* Set PCIe max read request size to at least 1024 bytes */
236203529Smav	if (pci_get_max_read_req(dev) < 1024)
237203529Smav		pci_set_max_read_req(dev, 1024);
238195801Smav	/* Put controller into reset state. */
239200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
240200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
241195801Smav	DELAY(10000);
242195801Smav	/* Get controller out of reset state and enable port interrupts. */
243200223Smav	ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE);
244200223Smav	ctlr->gctl |= 0x0000000f;
245200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
246195801Smav	return (bus_generic_resume(dev));
247195801Smav}
248195801Smav
249195801Smavstatic int
250195801Smavsiis_setup_interrupt(device_t dev)
251195801Smav{
252195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
253205358Smav	int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1;
254195801Smav
255195801Smav	/* Process hints. */
256195801Smav	resource_int_value(device_get_name(dev),
257195801Smav	    device_get_unit(dev), "msi", &msi);
258195801Smav	if (msi < 0)
259195801Smav		msi = 0;
260195801Smav	else if (msi > 0)
261195801Smav		msi = min(1, pci_msi_count(dev));
262195801Smav	/* Allocate MSI if needed/present. */
263195801Smav	if (msi && pci_alloc_msi(dev, &msi) != 0)
264195801Smav		msi = 0;
265195801Smav	/* Allocate all IRQs. */
266195801Smav	ctlr->irq.r_irq_rid = msi ? 1 : 0;
267195801Smav	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
268195801Smav	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
269195801Smav		device_printf(dev, "unable to map interrupt\n");
270195801Smav		return ENXIO;
271195801Smav	}
272195801Smav	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
273195801Smav	    siis_intr, ctlr, &ctlr->irq.handle))) {
274195801Smav		/* SOS XXX release r_irq */
275195801Smav		device_printf(dev, "unable to setup interrupt\n");
276195801Smav		return ENXIO;
277195801Smav	}
278195801Smav	return (0);
279195801Smav}
280195801Smav
281195801Smav/*
282195801Smav * Common case interrupt handler.
283195801Smav */
284195801Smavstatic void
285195801Smavsiis_intr(void *data)
286195801Smav{
287195801Smav	struct siis_controller *ctlr = (struct siis_controller *)data;
288195801Smav	u_int32_t is;
289195801Smav	void *arg;
290195801Smav	int unit;
291195801Smav
292195801Smav	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
293195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
294195801Smav		if ((is & SIIS_IS_PORT(unit)) != 0 &&
295195801Smav		    (arg = ctlr->interrupt[unit].argument)) {
296195801Smav			ctlr->interrupt[unit].function(arg);
297195801Smav		}
298195801Smav	}
299200223Smav	/* Acknowledge interrupt, if MSI enabled. */
300200223Smav	if (ctlr->irq.r_irq_rid) {
301200223Smav		ATA_OUTL(ctlr->r_gmem, SIIS_GCTL,
302200223Smav		    ctlr->gctl | SIIS_GCTL_MSIACK);
303200223Smav	}
304195801Smav}
305195801Smav
306195801Smavstatic struct resource *
307195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid,
308195801Smav		       u_long start, u_long end, u_long count, u_int flags)
309195801Smav{
310195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
311195801Smav	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
312195801Smav	struct resource *res = NULL;
313195801Smav	int offset = unit << 13;
314195801Smav	long st;
315195801Smav
316195801Smav	switch (type) {
317195801Smav	case SYS_RES_MEMORY:
318195801Smav		st = rman_get_start(ctlr->r_mem);
319195801Smav		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
320195801Smav		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
321195801Smav		if (res) {
322195801Smav			bus_space_handle_t bsh;
323195801Smav			bus_space_tag_t bst;
324195801Smav			bsh = rman_get_bushandle(ctlr->r_mem);
325195801Smav			bst = rman_get_bustag(ctlr->r_mem);
326195801Smav			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
327195801Smav			rman_set_bushandle(res, bsh);
328195801Smav			rman_set_bustag(res, bst);
329195801Smav		}
330195801Smav		break;
331195801Smav	case SYS_RES_IRQ:
332195801Smav		if (*rid == ATA_IRQ_RID)
333195801Smav			res = ctlr->irq.r_irq;
334195801Smav		break;
335195801Smav	}
336195801Smav	return (res);
337195801Smav}
338195801Smav
339195801Smavstatic int
340195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid,
341195801Smav			 struct resource *r)
342195801Smav{
343195801Smav
344195801Smav	switch (type) {
345195801Smav	case SYS_RES_MEMORY:
346195801Smav		rman_release_resource(r);
347195801Smav		return (0);
348195801Smav	case SYS_RES_IRQ:
349195801Smav		if (rid != ATA_IRQ_RID)
350195801Smav			return ENOENT;
351195801Smav		return (0);
352195801Smav	}
353195801Smav	return (EINVAL);
354195801Smav}
355195801Smav
356195801Smavstatic int
357195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq,
358195801Smav		   int flags, driver_filter_t *filter, driver_intr_t *function,
359195801Smav		   void *argument, void **cookiep)
360195801Smav{
361195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
362195801Smav	int unit = (intptr_t)device_get_ivars(child);
363195801Smav
364195801Smav	if (filter != NULL) {
365195801Smav		printf("siis.c: we cannot use a filter here\n");
366195801Smav		return (EINVAL);
367195801Smav	}
368195801Smav	ctlr->interrupt[unit].function = function;
369195801Smav	ctlr->interrupt[unit].argument = argument;
370195801Smav	return (0);
371195801Smav}
372195801Smav
373195801Smavstatic int
374195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq,
375195801Smav		      void *cookie)
376195801Smav{
377195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
378195801Smav	int unit = (intptr_t)device_get_ivars(child);
379195801Smav
380195801Smav	ctlr->interrupt[unit].function = NULL;
381195801Smav	ctlr->interrupt[unit].argument = NULL;
382195801Smav	return (0);
383195801Smav}
384195801Smav
385195801Smavstatic int
386195801Smavsiis_print_child(device_t dev, device_t child)
387195801Smav{
388195801Smav	int retval;
389195801Smav
390195801Smav	retval = bus_print_child_header(dev, child);
391195801Smav	retval += printf(" at channel %d",
392195801Smav	    (int)(intptr_t)device_get_ivars(child));
393195801Smav	retval += bus_print_child_footer(dev, child);
394195801Smav
395195801Smav	return (retval);
396195801Smav}
397195801Smav
398195801Smavdevclass_t siis_devclass;
399195801Smavstatic device_method_t siis_methods[] = {
400195801Smav	DEVMETHOD(device_probe,     siis_probe),
401195801Smav	DEVMETHOD(device_attach,    siis_attach),
402195801Smav	DEVMETHOD(device_detach,    siis_detach),
403195801Smav	DEVMETHOD(device_suspend,   siis_suspend),
404195801Smav	DEVMETHOD(device_resume,    siis_resume),
405195801Smav	DEVMETHOD(bus_print_child,  siis_print_child),
406195801Smav	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
407195801Smav	DEVMETHOD(bus_release_resource,     siis_release_resource),
408195801Smav	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
409195801Smav	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
410195801Smav	{ 0, 0 }
411195801Smav};
412195801Smavstatic driver_t siis_driver = {
413195801Smav        "siis",
414195801Smav        siis_methods,
415195801Smav        sizeof(struct siis_controller)
416195801Smav};
417195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
418195801SmavMODULE_VERSION(siis, 1);
419195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1);
420195801Smav
421195801Smavstatic int
422195801Smavsiis_ch_probe(device_t dev)
423195801Smav{
424195801Smav
425195801Smav	device_set_desc_copy(dev, "SIIS channel");
426195801Smav	return (0);
427195801Smav}
428195801Smav
429195801Smavstatic int
430195801Smavsiis_ch_attach(device_t dev)
431195801Smav{
432200217Smav	struct siis_controller *ctlr = device_get_softc(device_get_parent(dev));
433195801Smav	struct siis_channel *ch = device_get_softc(dev);
434195801Smav	struct cam_devq *devq;
435199821Smav	int rid, error, i, sata_rev = 0;
436195801Smav
437195801Smav	ch->dev = dev;
438195801Smav	ch->unit = (intptr_t)device_get_ivars(dev);
439200217Smav	ch->quirks = ctlr->quirks;
440195801Smav	resource_int_value(device_get_name(dev),
441195801Smav	    device_get_unit(dev), "pm_level", &ch->pm_level);
442199821Smav	resource_int_value(device_get_name(dev),
443199821Smav	    device_get_unit(dev), "sata_rev", &sata_rev);
444199747Smav	for (i = 0; i < 16; i++) {
445199821Smav		ch->user[i].revision = sata_rev;
446199747Smav		ch->user[i].mode = 0;
447199747Smav		ch->user[i].bytecount = 8192;
448199747Smav		ch->user[i].tags = SIIS_MAX_SLOTS;
449199747Smav		ch->curr[i] = ch->user[i];
450199747Smav	}
451195801Smav	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
452195801Smav	rid = ch->unit;
453195801Smav	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
454195801Smav	    &rid, RF_ACTIVE)))
455195801Smav		return (ENXIO);
456195801Smav	siis_dmainit(dev);
457195801Smav	siis_slotsalloc(dev);
458195801Smav	siis_ch_resume(dev);
459195801Smav	mtx_lock(&ch->mtx);
460195801Smav	rid = ATA_IRQ_RID;
461195801Smav	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
462195801Smav	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
463195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
464195801Smav		device_printf(dev, "Unable to map interrupt\n");
465195801Smav		return (ENXIO);
466195801Smav	}
467195801Smav	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
468195801Smav	    siis_ch_intr_locked, dev, &ch->ih))) {
469195801Smav		device_printf(dev, "Unable to setup interrupt\n");
470195801Smav		error = ENXIO;
471195801Smav		goto err1;
472195801Smav	}
473195801Smav	/* Create the device queue for our SIM. */
474195801Smav	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
475195801Smav	if (devq == NULL) {
476195801Smav		device_printf(dev, "Unable to allocate simq\n");
477195801Smav		error = ENOMEM;
478195801Smav		goto err1;
479195801Smav	}
480195801Smav	/* Construct SIM entry */
481195801Smav	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
482199178Smav	    device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq);
483195801Smav	if (ch->sim == NULL) {
484195801Smav		device_printf(dev, "unable to allocate sim\n");
485195801Smav		error = ENOMEM;
486195801Smav		goto err2;
487195801Smav	}
488195801Smav	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
489195801Smav		device_printf(dev, "unable to register xpt bus\n");
490195801Smav		error = ENXIO;
491195801Smav		goto err2;
492195801Smav	}
493195801Smav	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
494195801Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
495195801Smav		device_printf(dev, "unable to create path\n");
496195801Smav		error = ENXIO;
497195801Smav		goto err3;
498195801Smav	}
499195801Smav	mtx_unlock(&ch->mtx);
500195801Smav	return (0);
501195801Smav
502195801Smaverr3:
503195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
504195801Smaverr2:
505195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
506195801Smaverr1:
507195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
508195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
509195801Smav	mtx_unlock(&ch->mtx);
510195801Smav	return (error);
511195801Smav}
512195801Smav
513195801Smavstatic int
514195801Smavsiis_ch_detach(device_t dev)
515195801Smav{
516195801Smav	struct siis_channel *ch = device_get_softc(dev);
517195801Smav
518195801Smav	mtx_lock(&ch->mtx);
519195801Smav	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
520195801Smav	xpt_free_path(ch->path);
521195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
522195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
523195801Smav	mtx_unlock(&ch->mtx);
524195801Smav
525195801Smav	bus_teardown_intr(dev, ch->r_irq, ch->ih);
526195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
527195801Smav
528195801Smav	siis_ch_suspend(dev);
529195801Smav	siis_slotsfree(dev);
530195801Smav	siis_dmafini(dev);
531195801Smav
532195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
533195801Smav	mtx_destroy(&ch->mtx);
534195801Smav	return (0);
535195801Smav}
536195801Smav
537195801Smavstatic int
538195801Smavsiis_ch_suspend(device_t dev)
539195801Smav{
540195801Smav	struct siis_channel *ch = device_get_softc(dev);
541195801Smav
542195801Smav	/* Put port into reset state. */
543195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
544195801Smav	return (0);
545195801Smav}
546195801Smav
547195801Smavstatic int
548195801Smavsiis_ch_resume(device_t dev)
549195801Smav{
550195801Smav	struct siis_channel *ch = device_get_softc(dev);
551195801Smav
552195801Smav	/* Get port out of reset state. */
553195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
554195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
555198852Smav	if (ch->pm_present)
556198852Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
557198852Smav	else
558198852Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
559195801Smav	/* Enable port interrupts */
560195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
561195801Smav	return (0);
562195801Smav}
563195801Smav
564195801Smavdevclass_t siisch_devclass;
565195801Smavstatic device_method_t siisch_methods[] = {
566195801Smav	DEVMETHOD(device_probe,     siis_ch_probe),
567195801Smav	DEVMETHOD(device_attach,    siis_ch_attach),
568195801Smav	DEVMETHOD(device_detach,    siis_ch_detach),
569195801Smav	DEVMETHOD(device_suspend,   siis_ch_suspend),
570195801Smav	DEVMETHOD(device_resume,    siis_ch_resume),
571195801Smav	{ 0, 0 }
572195801Smav};
573195801Smavstatic driver_t siisch_driver = {
574195801Smav        "siisch",
575195801Smav        siisch_methods,
576195801Smav        sizeof(struct siis_channel)
577195801Smav};
578195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
579195801Smav
580195801Smavstruct siis_dc_cb_args {
581195801Smav	bus_addr_t maddr;
582195801Smav	int error;
583195801Smav};
584195801Smav
585195801Smavstatic void
586195801Smavsiis_dmainit(device_t dev)
587195801Smav{
588195801Smav	struct siis_channel *ch = device_get_softc(dev);
589195801Smav	struct siis_dc_cb_args dcba;
590195801Smav
591195801Smav	/* Command area. */
592195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
593195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
594195801Smav	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
595195801Smav	    0, NULL, NULL, &ch->dma.work_tag))
596195801Smav		goto error;
597195801Smav	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
598195801Smav	    &ch->dma.work_map))
599195801Smav		goto error;
600195801Smav	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
601195801Smav	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
602195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
603195801Smav		goto error;
604195801Smav	}
605195801Smav	ch->dma.work_bus = dcba.maddr;
606195801Smav	/* Data area. */
607199333Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
608195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
609195801Smav	    NULL, NULL,
610195801Smav	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
611195801Smav	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
612195801Smav	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
613195801Smav		goto error;
614195801Smav	}
615195801Smav	return;
616195801Smav
617195801Smaverror:
618195801Smav	device_printf(dev, "WARNING - DMA initialization failed\n");
619195801Smav	siis_dmafini(dev);
620195801Smav}
621195801Smav
622195801Smavstatic void
623195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
624195801Smav{
625195801Smav	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
626195801Smav
627195801Smav	if (!(dcba->error = error))
628195801Smav		dcba->maddr = segs[0].ds_addr;
629195801Smav}
630195801Smav
631195801Smavstatic void
632195801Smavsiis_dmafini(device_t dev)
633195801Smav{
634195801Smav	struct siis_channel *ch = device_get_softc(dev);
635195801Smav
636195801Smav	if (ch->dma.data_tag) {
637195801Smav		bus_dma_tag_destroy(ch->dma.data_tag);
638195801Smav		ch->dma.data_tag = NULL;
639195801Smav	}
640195801Smav	if (ch->dma.work_bus) {
641195801Smav		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
642195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
643195801Smav		ch->dma.work_bus = 0;
644195801Smav		ch->dma.work_map = NULL;
645195801Smav		ch->dma.work = NULL;
646195801Smav	}
647195801Smav	if (ch->dma.work_tag) {
648195801Smav		bus_dma_tag_destroy(ch->dma.work_tag);
649195801Smav		ch->dma.work_tag = NULL;
650195801Smav	}
651195801Smav}
652195801Smav
653195801Smavstatic void
654195801Smavsiis_slotsalloc(device_t dev)
655195801Smav{
656195801Smav	struct siis_channel *ch = device_get_softc(dev);
657195801Smav	int i;
658195801Smav
659195801Smav	/* Alloc and setup command/dma slots */
660195801Smav	bzero(ch->slot, sizeof(ch->slot));
661195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
662195801Smav		struct siis_slot *slot = &ch->slot[i];
663195801Smav
664195801Smav		slot->dev = dev;
665195801Smav		slot->slot = i;
666195801Smav		slot->state = SIIS_SLOT_EMPTY;
667195801Smav		slot->ccb = NULL;
668195801Smav		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
669195801Smav
670195801Smav		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
671195801Smav			device_printf(ch->dev, "FAILURE - create data_map\n");
672195801Smav	}
673195801Smav}
674195801Smav
675195801Smavstatic void
676195801Smavsiis_slotsfree(device_t dev)
677195801Smav{
678195801Smav	struct siis_channel *ch = device_get_softc(dev);
679195801Smav	int i;
680195801Smav
681195801Smav	/* Free all dma slots */
682195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
683195801Smav		struct siis_slot *slot = &ch->slot[i];
684195801Smav
685198896Smav		callout_drain(&slot->timeout);
686195801Smav		if (slot->dma.data_map) {
687195801Smav			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
688195801Smav			slot->dma.data_map = NULL;
689195801Smav		}
690195801Smav	}
691195801Smav}
692195801Smav
693195801Smavstatic void
694196655Smavsiis_notify_events(device_t dev)
695196655Smav{
696196655Smav	struct siis_channel *ch = device_get_softc(dev);
697196655Smav	struct cam_path *dpath;
698196655Smav	u_int32_t status;
699196655Smav	int i;
700196655Smav
701200217Smav	if (ch->quirks & SIIS_Q_SNTF) {
702200217Smav		status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
703200217Smav		ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
704200217Smav	} else {
705200217Smav		/*
706200217Smav		 * Without SNTF we have no idea which device sent notification.
707200217Smav		 * If PMP is connected, assume it, else - device.
708200217Smav		 */
709200217Smav		status = (ch->pm_present) ? 0x8000 : 0x0001;
710200217Smav	}
711196655Smav	if (bootverbose)
712196655Smav		device_printf(dev, "SNTF 0x%04x\n", status);
713196655Smav	for (i = 0; i < 16; i++) {
714196655Smav		if ((status & (1 << i)) == 0)
715196655Smav			continue;
716196655Smav		if (xpt_create_path(&dpath, NULL,
717196655Smav		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
718196655Smav			xpt_async(AC_SCSI_AEN, dpath, NULL);
719196655Smav			xpt_free_path(dpath);
720196655Smav		}
721196655Smav	}
722196655Smav
723196655Smav}
724196655Smav
725196655Smavstatic void
726195801Smavsiis_phy_check_events(device_t dev)
727195801Smav{
728195801Smav	struct siis_channel *ch = device_get_softc(dev);
729195801Smav
730195801Smav	/* If we have a connection event, deal with it */
731195801Smav	if (ch->pm_level == 0) {
732195801Smav		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
733203108Smav		union ccb *ccb;
734203108Smav
735203165Smav		if (bootverbose) {
736203165Smav			if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
737203165Smav			    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
738203165Smav			    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
739195801Smav				device_printf(dev, "CONNECT requested\n");
740203165Smav			} else
741195801Smav				device_printf(dev, "DISCONNECT requested\n");
742195801Smav		}
743203165Smav		siis_reset(dev);
744203108Smav		if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
745203108Smav			return;
746203108Smav		if (xpt_create_path(&ccb->ccb_h.path, NULL,
747203108Smav		    cam_sim_path(ch->sim),
748203108Smav		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
749203108Smav			xpt_free_ccb(ccb);
750203108Smav			return;
751203108Smav		}
752203108Smav		xpt_rescan(ccb);
753195801Smav	}
754195801Smav}
755195801Smav
756195801Smavstatic void
757195801Smavsiis_ch_intr_locked(void *data)
758195801Smav{
759195801Smav	device_t dev = (device_t)data;
760195801Smav	struct siis_channel *ch = device_get_softc(dev);
761195801Smav
762195801Smav	mtx_lock(&ch->mtx);
763195801Smav	siis_ch_intr(data);
764195801Smav	mtx_unlock(&ch->mtx);
765195801Smav}
766195801Smav
767195801Smavstatic void
768195801Smavsiis_ch_intr(void *data)
769195801Smav{
770195801Smav	device_t dev = (device_t)data;
771195801Smav	struct siis_channel *ch = device_get_softc(dev);
772195801Smav	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
773195801Smav	enum siis_err_type et;
774195801Smav	int i, ccs, port, tslots;
775195801Smav
776195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
777195801Smav	/* Read command statuses. */
778195801Smav	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
779195801Smav	ok = ch->rslots & ~sstatus;
780195801Smav	/* Complete all successfull commands. */
781195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
782195801Smav		if ((ok >> i) & 1)
783195801Smav			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
784195801Smav	}
785195801Smav	/* Do we have any other events? */
786195801Smav	if ((sstatus & SIIS_P_SS_ATTN) == 0)
787195801Smav		return;
788195801Smav	/* Read and clear interrupt statuses. */
789195801Smav	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
790195801Smav	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
791195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
792195801Smav	/* Process PHY events */
793195801Smav	if (istatus & SIIS_P_IX_PHYRDYCHG)
794195801Smav		siis_phy_check_events(dev);
795196655Smav	/* Process NOTIFY events */
796196655Smav	if (istatus & SIIS_P_IX_SDBN)
797196655Smav		siis_notify_events(dev);
798195801Smav	/* Process command errors */
799195801Smav	if (istatus & SIIS_P_IX_COMMERR) {
800195801Smav		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
801195801Smav		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
802195801Smav		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
803195801Smav		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
804195801Smav		err = ch->rslots & sstatus;
805195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
806195801Smav//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
807195801Smav//    ATA_INL(ch->r_mem, SIIS_P_SERR));
808195801Smav
809195801Smav		if (!ch->readlog && !ch->recovery) {
810195801Smav			xpt_freeze_simq(ch->sim, ch->numrslots);
811195801Smav			ch->recovery = 1;
812195801Smav		}
813195801Smav		if (ch->frozen) {
814195801Smav			union ccb *fccb = ch->frozen;
815195801Smav			ch->frozen = NULL;
816198321Smav			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
817198321Smav			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
818198321Smav			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
819198321Smav				xpt_freeze_devq(fccb->ccb_h.path, 1);
820198321Smav				fccb->ccb_h.status |= CAM_DEV_QFRZN;
821198321Smav			}
822195801Smav			xpt_done(fccb);
823195801Smav		}
824195801Smav		if (estatus == SIIS_P_CMDERR_DEV ||
825195801Smav		    estatus == SIIS_P_CMDERR_SDB ||
826195801Smav		    estatus == SIIS_P_CMDERR_DATAFIS) {
827195801Smav			tslots = ch->numtslots[port];
828195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
829198852Smav				/* XXX: requests in loading state. */
830195801Smav				if (((ch->rslots >> i) & 1) == 0)
831195801Smav					continue;
832195801Smav				if (ch->slot[i].ccb->ccb_h.target_id != port)
833195801Smav					continue;
834195801Smav				if (tslots == 0) {
835195801Smav					/* Untagged operation. */
836195801Smav					if (i == ccs)
837195801Smav						et = SIIS_ERR_TFE;
838195801Smav					else
839195801Smav						et = SIIS_ERR_INNOCENT;
840195801Smav				} else {
841195801Smav					/* Tagged operation. */
842195801Smav					et = SIIS_ERR_NCQ;
843195801Smav				}
844195801Smav				siis_end_transaction(&ch->slot[i], et);
845195801Smav			}
846195801Smav			/*
847195801Smav			 * We can't reinit port if there are some other
848195801Smav			 * commands active, use resume to complete them.
849195801Smav			 */
850195801Smav			if (ch->rslots != 0)
851195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
852195801Smav		} else {
853195801Smav			if (estatus == SIIS_P_CMDERR_SENDFIS ||
854195801Smav			    estatus == SIIS_P_CMDERR_INCSTATE ||
855195801Smav			    estatus == SIIS_P_CMDERR_PPE ||
856195801Smav			    estatus == SIIS_P_CMDERR_SERVICE) {
857195801Smav				et = SIIS_ERR_SATA;
858195801Smav			} else
859195801Smav				et = SIIS_ERR_INVALID;
860195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
861198852Smav				/* XXX: requests in loading state. */
862195801Smav				if (((ch->rslots >> i) & 1) == 0)
863195801Smav					continue;
864195801Smav				siis_end_transaction(&ch->slot[i], et);
865195801Smav			}
866195801Smav		}
867195801Smav	}
868195801Smav}
869195801Smav
870195801Smav/* Must be called with channel locked. */
871195801Smavstatic int
872195801Smavsiis_check_collision(device_t dev, union ccb *ccb)
873195801Smav{
874195801Smav	struct siis_channel *ch = device_get_softc(dev);
875195801Smav
876195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
877195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
878199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
879199747Smav		/* Tagged command while we have no supported tag free. */
880199747Smav		if (((~ch->oslots) & (0x7fffffff >> (31 -
881199747Smav		    ch->curr[ccb->ccb_h.target_id].tags))) == 0)
882199747Smav			return (1);
883199747Smav	}
884199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
885195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
886195801Smav		/* Atomic command while anything active. */
887195801Smav		if (ch->numrslots != 0)
888195801Smav			return (1);
889195801Smav	}
890195801Smav       /* We have some atomic command running. */
891195801Smav       if (ch->aslots != 0)
892195801Smav               return (1);
893195801Smav	return (0);
894195801Smav}
895195801Smav
896195801Smav/* Must be called with channel locked. */
897195801Smavstatic void
898195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb)
899195801Smav{
900195801Smav	struct siis_channel *ch = device_get_softc(dev);
901195801Smav	struct siis_slot *slot;
902199747Smav	int tag, tags;
903195801Smav
904195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
905195801Smav	/* Choose empty slot. */
906199747Smav	tags = SIIS_MAX_SLOTS;
907199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
908199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
909199747Smav		tags = ch->curr[ccb->ccb_h.target_id].tags;
910199747Smav	tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1;
911195801Smav	/* Occupy chosen slot. */
912195801Smav	slot = &ch->slot[tag];
913195801Smav	slot->ccb = ccb;
914195801Smav	/* Update channel stats. */
915199747Smav	ch->oslots |= (1 << slot->slot);
916195801Smav	ch->numrslots++;
917195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
918195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
919195801Smav		ch->numtslots[ccb->ccb_h.target_id]++;
920195801Smav	}
921195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
922195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
923195801Smav		ch->aslots |= (1 << slot->slot);
924195801Smav	slot->dma.nsegs = 0;
925195801Smav	/* If request moves data, setup and load SG list */
926195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
927195801Smav		void *buf;
928195801Smav		bus_size_t size;
929195801Smav
930195801Smav		slot->state = SIIS_SLOT_LOADING;
931195801Smav		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
932195801Smav			buf = ccb->ataio.data_ptr;
933195801Smav			size = ccb->ataio.dxfer_len;
934195801Smav		} else {
935195801Smav			buf = ccb->csio.data_ptr;
936195801Smav			size = ccb->csio.dxfer_len;
937195801Smav		}
938195801Smav		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
939195801Smav		    buf, size, siis_dmasetprd, slot, 0);
940195801Smav	} else
941195801Smav		siis_execute_transaction(slot);
942195801Smav}
943195801Smav
944195801Smav/* Locked by busdma engine. */
945195801Smavstatic void
946195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
947195801Smav{
948195801Smav	struct siis_slot *slot = arg;
949195801Smav	struct siis_channel *ch = device_get_softc(slot->dev);
950195801Smav	struct siis_cmd *ctp;
951195801Smav	struct siis_dma_prd *prd;
952195801Smav	int i;
953195801Smav
954195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
955195801Smav	if (error) {
956195801Smav		device_printf(slot->dev, "DMA load error\n");
957195801Smav		if (!ch->readlog)
958195801Smav			xpt_freeze_simq(ch->sim, 1);
959195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
960195801Smav		return;
961195801Smav	}
962195801Smav	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
963195801Smav	/* Get a piece of the workspace for this request */
964195801Smav	ctp = (struct siis_cmd *)
965195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
966195801Smav	/* Fill S/G table */
967195801Smav	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
968195801Smav		prd = &ctp->u.ata.prd[0];
969195801Smav	else
970195801Smav		prd = &ctp->u.atapi.prd[0];
971195801Smav	for (i = 0; i < nsegs; i++) {
972195801Smav		prd[i].dba = htole64(segs[i].ds_addr);
973195801Smav		prd[i].dbc = htole32(segs[i].ds_len);
974195801Smav		prd[i].control = 0;
975195801Smav	}
976195801Smav	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
977195801Smav	slot->dma.nsegs = nsegs;
978195801Smav	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
979195801Smav	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
980195801Smav	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
981195801Smav	siis_execute_transaction(slot);
982195801Smav}
983195801Smav
984195801Smav/* Must be called with channel locked. */
985195801Smavstatic void
986195801Smavsiis_execute_transaction(struct siis_slot *slot)
987195801Smav{
988195801Smav	device_t dev = slot->dev;
989195801Smav	struct siis_channel *ch = device_get_softc(dev);
990195801Smav	struct siis_cmd *ctp;
991195801Smav	union ccb *ccb = slot->ccb;
992195801Smav	u_int64_t prb_bus;
993195801Smav
994195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
995195801Smav	/* Get a piece of the workspace for this request */
996195801Smav	ctp = (struct siis_cmd *)
997195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
998195801Smav	ctp->control = 0;
999195801Smav	ctp->protocol_override = 0;
1000195801Smav	ctp->transfer_count = 0;
1001195801Smav	/* Special handling for Soft Reset command. */
1002201222Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1003201222Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) {
1004201222Smav			ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
1005201222Smav		} else {
1006201222Smav			ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE);
1007201222Smav			if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1008201222Smav				ctp->protocol_override |=
1009201222Smav				    htole16(SIIS_PRB_PROTO_NCQ);
1010201222Smav			}
1011201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1012201222Smav				ctp->protocol_override |=
1013201222Smav				    htole16(SIIS_PRB_PROTO_READ);
1014201222Smav			} else
1015201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1016201222Smav				ctp->protocol_override |=
1017201222Smav				    htole16(SIIS_PRB_PROTO_WRITE);
1018201222Smav			}
1019201222Smav		}
1020195801Smav	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1021201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1022195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
1023201222Smav		else
1024201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1025195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
1026195801Smav	}
1027203108Smav	/* Special handling for Soft Reset command. */
1028203108Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1029203108Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1030203108Smav	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1031203108Smav		/* Kick controller into sane state */
1032203108Smav		siis_portinit(dev);
1033203108Smav	}
1034195801Smav	/* Setup the FIS for this request */
1035199821Smav	if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) {
1036195801Smav		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1037195801Smav		if (!ch->readlog)
1038195801Smav			xpt_freeze_simq(ch->sim, 1);
1039195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1040195801Smav		return;
1041195801Smav	}
1042195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1043195801Smav	    BUS_DMASYNC_PREWRITE);
1044195801Smav	/* Issue command to the controller. */
1045195801Smav	slot->state = SIIS_SLOT_RUNNING;
1046195801Smav	ch->rslots |= (1 << slot->slot);
1047195801Smav	prb_bus = ch->dma.work_bus +
1048195801Smav	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
1049195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
1050195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
1051195801Smav	/* Start command execution timeout */
1052195801Smav	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
1053195801Smav	    (timeout_t*)siis_timeout, slot);
1054195801Smav	return;
1055195801Smav}
1056195801Smav
1057198852Smav/* Must be called with channel locked. */
1058195801Smavstatic void
1059198852Smavsiis_process_timeout(device_t dev)
1060195801Smav{
1061195801Smav	struct siis_channel *ch = device_get_softc(dev);
1062195801Smav	int i;
1063195801Smav
1064195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1065198852Smav	if (!ch->readlog && !ch->recovery) {
1066195801Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1067198852Smav		ch->recovery = 1;
1068195801Smav	}
1069197838Smav	/* Handle the rest of commands. */
1070195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1071195801Smav		/* Do we have a running request on slot? */
1072195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1073195801Smav			continue;
1074198852Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
1075195801Smav	}
1076195801Smav}
1077195801Smav
1078203870Smav/* Must be called with channel locked. */
1079203870Smavstatic void
1080203870Smavsiis_rearm_timeout(device_t dev)
1081203870Smav{
1082203870Smav	struct siis_channel *ch = device_get_softc(dev);
1083203870Smav	int i;
1084203870Smav
1085203870Smav	mtx_assert(&ch->mtx, MA_OWNED);
1086203870Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1087203870Smav		struct siis_slot *slot = &ch->slot[i];
1088203870Smav
1089203870Smav		/* Do we have a running request on slot? */
1090203870Smav		if (slot->state < SIIS_SLOT_RUNNING)
1091203870Smav			continue;
1092203870Smav		if ((ch->toslots & (1 << i)) == 0)
1093203870Smav			continue;
1094203870Smav		callout_reset(&slot->timeout,
1095203870Smav		    (int)slot->ccb->ccb_h.timeout * hz / 1000,
1096203870Smav		    (timeout_t*)siis_timeout, slot);
1097203870Smav	}
1098203870Smav}
1099203870Smav
1100198852Smav/* Locked by callout mechanism. */
1101198852Smavstatic void
1102198852Smavsiis_timeout(struct siis_slot *slot)
1103198852Smav{
1104198852Smav	device_t dev = slot->dev;
1105198852Smav	struct siis_channel *ch = device_get_softc(dev);
1106198852Smav
1107198852Smav	mtx_assert(&ch->mtx, MA_OWNED);
1108198896Smav	/* Check for stale timeout. */
1109198896Smav	if (slot->state < SIIS_SLOT_RUNNING)
1110198896Smav		return;
1111198852Smav	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1112203108Smav	device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
1113203108Smav	    __func__, ATA_INL(ch->r_mem, SIIS_P_IS),
1114203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
1115203108Smav	    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
1116203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SERR));
1117198852Smav
1118198852Smav	if (ch->toslots == 0)
1119198852Smav		xpt_freeze_simq(ch->sim, 1);
1120198852Smav	ch->toslots |= (1 << slot->slot);
1121198852Smav	if ((ch->rslots & ~ch->toslots) == 0)
1122198852Smav		siis_process_timeout(dev);
1123198852Smav	else
1124198852Smav		device_printf(dev, " ... waiting for slots %08x\n",
1125198852Smav		    ch->rslots & ~ch->toslots);
1126198852Smav}
1127198852Smav
1128195801Smav/* Must be called with channel locked. */
1129195801Smavstatic void
1130195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1131195801Smav{
1132195801Smav	device_t dev = slot->dev;
1133195801Smav	struct siis_channel *ch = device_get_softc(dev);
1134195801Smav	union ccb *ccb = slot->ccb;
1135195801Smav
1136195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1137195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1138195801Smav	    BUS_DMASYNC_POSTWRITE);
1139195801Smav	/* Read result registers to the result struct
1140195801Smav	 * May be incorrect if several commands finished same time,
1141195801Smav	 * so read only when sure or have to.
1142195801Smav	 */
1143195801Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1144195801Smav		struct ata_res *res = &ccb->ataio.res;
1145195801Smav		if ((et == SIIS_ERR_TFE) ||
1146195801Smav		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1147195801Smav			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1148195801Smav
1149195801Smav			res->status = ATA_INB(ch->r_mem, offs + 2);
1150195801Smav			res->error = ATA_INB(ch->r_mem, offs + 3);
1151195801Smav			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1152195801Smav			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1153195801Smav			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1154195801Smav			res->device = ATA_INB(ch->r_mem, offs + 7);
1155195801Smav			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1156195801Smav			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1157195801Smav			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1158195801Smav			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1159195801Smav			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1160195801Smav		} else
1161195801Smav			bzero(res, sizeof(*res));
1162195801Smav	}
1163195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1164195801Smav		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1165195801Smav		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1166195801Smav		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1167195801Smav		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1168195801Smav	}
1169195801Smav	/* Set proper result status. */
1170195801Smav	if (et != SIIS_ERR_NONE || ch->recovery) {
1171195801Smav		ch->eslots |= (1 << slot->slot);
1172195801Smav		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1173195801Smav	}
1174198321Smav	/* In case of error, freeze device for proper recovery. */
1175198321Smav	if (et != SIIS_ERR_NONE &&
1176198321Smav	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1177198321Smav		xpt_freeze_devq(ccb->ccb_h.path, 1);
1178198321Smav		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1179198321Smav	}
1180198321Smav	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1181195801Smav	switch (et) {
1182195801Smav	case SIIS_ERR_NONE:
1183195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP;
1184195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1185195801Smav			ccb->csio.scsi_status = SCSI_STATUS_OK;
1186195801Smav		break;
1187195801Smav	case SIIS_ERR_INVALID:
1188198852Smav		ch->fatalerr = 1;
1189195801Smav		ccb->ccb_h.status |= CAM_REQ_INVALID;
1190195801Smav		break;
1191195801Smav	case SIIS_ERR_INNOCENT:
1192195801Smav		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1193195801Smav		break;
1194195801Smav	case SIIS_ERR_TFE:
1195198321Smav	case SIIS_ERR_NCQ:
1196195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1197195801Smav			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1198195801Smav			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1199195801Smav		} else {
1200195801Smav			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1201195801Smav		}
1202195801Smav		break;
1203195801Smav	case SIIS_ERR_SATA:
1204198852Smav		ch->fatalerr = 1;
1205195801Smav		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1206195801Smav		break;
1207195801Smav	case SIIS_ERR_TIMEOUT:
1208198852Smav		ch->fatalerr = 1;
1209195801Smav		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1210195801Smav		break;
1211195801Smav	default:
1212195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1213195801Smav	}
1214195801Smav	/* Free slot. */
1215199747Smav	ch->oslots &= ~(1 << slot->slot);
1216195801Smav	ch->rslots &= ~(1 << slot->slot);
1217195801Smav	ch->aslots &= ~(1 << slot->slot);
1218198852Smav	if (et != SIIS_ERR_TIMEOUT) {
1219198852Smav		if (ch->toslots == (1 << slot->slot))
1220198852Smav			xpt_release_simq(ch->sim, TRUE);
1221198852Smav		ch->toslots &= ~(1 << slot->slot);
1222198852Smav	}
1223195801Smav	slot->state = SIIS_SLOT_EMPTY;
1224195801Smav	slot->ccb = NULL;
1225195801Smav	/* Update channel stats. */
1226195801Smav	ch->numrslots--;
1227195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1228195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1229195801Smav		ch->numtslots[ccb->ccb_h.target_id]--;
1230195801Smav	}
1231198852Smav	/* If it was our READ LOG command - process it. */
1232198852Smav	if (ch->readlog) {
1233198852Smav		siis_process_read_log(dev, ccb);
1234195801Smav	/* If it was NCQ command error, put result on hold. */
1235198852Smav	} else if (et == SIIS_ERR_NCQ) {
1236195801Smav		ch->hold[slot->slot] = ccb;
1237195801Smav		ch->numhslots++;
1238198852Smav	} else
1239195801Smav		xpt_done(ccb);
1240195801Smav	/* Unfreeze frozen command. */
1241199747Smav	if (ch->frozen && !siis_check_collision(dev, ch->frozen)) {
1242195801Smav		union ccb *fccb = ch->frozen;
1243195801Smav		ch->frozen = NULL;
1244195801Smav		siis_begin_transaction(dev, fccb);
1245195801Smav		xpt_release_simq(ch->sim, TRUE);
1246195801Smav	}
1247195801Smav	/* If we have no other active commands, ... */
1248195801Smav	if (ch->rslots == 0) {
1249198852Smav		/* if there were timeouts or fatal error - reset port. */
1250198852Smav		if (ch->toslots != 0 || ch->fatalerr) {
1251198852Smav			siis_reset(dev);
1252198852Smav		} else {
1253198852Smav			/* if we have slots in error, we can reinit port. */
1254198852Smav			if (ch->eslots != 0)
1255198852Smav				siis_portinit(dev);
1256198852Smav			/* if there commands on hold, we can do READ LOG. */
1257198852Smav			if (!ch->readlog && ch->numhslots)
1258198852Smav				siis_issue_read_log(dev);
1259198852Smav		}
1260198852Smav	/* If all the reset of commands are in timeout - abort them. */
1261203870Smav	} else if ((ch->rslots & ~ch->toslots) == 0 &&
1262203870Smav	    et != SIIS_ERR_TIMEOUT)
1263203870Smav		siis_rearm_timeout(dev);
1264195801Smav}
1265195801Smav
1266195801Smavstatic void
1267195801Smavsiis_issue_read_log(device_t dev)
1268195801Smav{
1269195801Smav	struct siis_channel *ch = device_get_softc(dev);
1270195801Smav	union ccb *ccb;
1271195801Smav	struct ccb_ataio *ataio;
1272195801Smav	int i;
1273195801Smav
1274195801Smav	/* Find some holden command. */
1275195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1276195801Smav		if (ch->hold[i])
1277195801Smav			break;
1278195801Smav	}
1279195801Smav	if (i == SIIS_MAX_SLOTS)
1280195801Smav		return;
1281195801Smav	ch->readlog = 1;
1282195801Smav	ccb = xpt_alloc_ccb_nowait();
1283195801Smav	if (ccb == NULL) {
1284195801Smav		device_printf(dev, "Unable allocate READ LOG command");
1285195801Smav		return; /* XXX */
1286195801Smav	}
1287195801Smav	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1288195801Smav	ccb->ccb_h.func_code = XPT_ATA_IO;
1289195801Smav	ccb->ccb_h.flags = CAM_DIR_IN;
1290195801Smav	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1291195801Smav	ataio = &ccb->ataio;
1292195801Smav	ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1293195801Smav	if (ataio->data_ptr == NULL) {
1294195801Smav		device_printf(dev, "Unable allocate memory for READ LOG command");
1295195801Smav		return; /* XXX */
1296195801Smav	}
1297195801Smav	ataio->dxfer_len = 512;
1298195801Smav	bzero(&ataio->cmd, sizeof(ataio->cmd));
1299195801Smav	ataio->cmd.flags = CAM_ATAIO_48BIT;
1300195801Smav	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1301195801Smav	ataio->cmd.sector_count = 1;
1302195801Smav	ataio->cmd.sector_count_exp = 0;
1303195801Smav	ataio->cmd.lba_low = 0x10;
1304195801Smav	ataio->cmd.lba_mid = 0;
1305195801Smav	ataio->cmd.lba_mid_exp = 0;
1306195801Smav	siis_begin_transaction(dev, ccb);
1307195801Smav}
1308195801Smav
1309195801Smavstatic void
1310195801Smavsiis_process_read_log(device_t dev, union ccb *ccb)
1311195801Smav{
1312195801Smav	struct siis_channel *ch = device_get_softc(dev);
1313195801Smav	uint8_t *data;
1314195801Smav	struct ata_res *res;
1315195801Smav	int i;
1316195801Smav
1317195801Smav	ch->readlog = 0;
1318195801Smav	data = ccb->ataio.data_ptr;
1319195801Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1320195801Smav	    (data[0] & 0x80) == 0) {
1321195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1322195801Smav			if (!ch->hold[i])
1323195801Smav				continue;
1324195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1325195801Smav				continue;
1326195801Smav			if ((data[0] & 0x1F) == i) {
1327195801Smav				res = &ch->hold[i]->ataio.res;
1328195801Smav				res->status = data[2];
1329195801Smav				res->error = data[3];
1330195801Smav				res->lba_low = data[4];
1331195801Smav				res->lba_mid = data[5];
1332195801Smav				res->lba_high = data[6];
1333195801Smav				res->device = data[7];
1334195801Smav				res->lba_low_exp = data[8];
1335195801Smav				res->lba_mid_exp = data[9];
1336195801Smav				res->lba_high_exp = data[10];
1337195801Smav				res->sector_count = data[12];
1338195801Smav				res->sector_count_exp = data[13];
1339195801Smav			} else {
1340195801Smav				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1341195801Smav				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1342195801Smav			}
1343195801Smav			xpt_done(ch->hold[i]);
1344195801Smav			ch->hold[i] = NULL;
1345195801Smav			ch->numhslots--;
1346195801Smav		}
1347195801Smav	} else {
1348195801Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1349195801Smav			device_printf(dev, "Error while READ LOG EXT\n");
1350195801Smav		else if ((data[0] & 0x80) == 0) {
1351195801Smav			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1352195801Smav		}
1353195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1354195801Smav			if (!ch->hold[i])
1355195801Smav				continue;
1356195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1357195801Smav				continue;
1358195801Smav			xpt_done(ch->hold[i]);
1359195801Smav			ch->hold[i] = NULL;
1360195801Smav			ch->numhslots--;
1361195801Smav		}
1362195801Smav	}
1363195801Smav	free(ccb->ataio.data_ptr, M_SIIS);
1364195801Smav	xpt_free_ccb(ccb);
1365195801Smav}
1366195801Smav
1367195801Smavstatic void
1368195801Smavsiis_portinit(device_t dev)
1369195801Smav{
1370195801Smav	struct siis_channel *ch = device_get_softc(dev);
1371195801Smav	int i;
1372195801Smav
1373195801Smav	ch->eslots = 0;
1374195801Smav	ch->recovery = 0;
1375195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1376195801Smav	for (i = 0; i < 16; i++) {
1377195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1378195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1379195801Smav	}
1380195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1381195801Smav	siis_wait_ready(dev, 1000);
1382195801Smav}
1383195801Smav
1384198426Smavstatic int
1385195801Smavsiis_devreset(device_t dev)
1386195801Smav{
1387195801Smav	struct siis_channel *ch = device_get_softc(dev);
1388198426Smav	int timeout = 0;
1389198426Smav	uint32_t val;
1390195801Smav
1391195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1392198426Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1393198426Smav	    SIIS_P_CTL_DEV_RESET) != 0) {
1394198426Smav		DELAY(1000);
1395198426Smav		if (timeout++ > 100) {
1396198426Smav			device_printf(dev, "device reset stuck (timeout %dms) "
1397198426Smav			    "status = %08x\n", timeout, val);
1398198426Smav			return (EBUSY);
1399198426Smav		}
1400198426Smav	}
1401198426Smav	return (0);
1402195801Smav}
1403195801Smav
1404195801Smavstatic int
1405195801Smavsiis_wait_ready(device_t dev, int t)
1406195801Smav{
1407195801Smav	struct siis_channel *ch = device_get_softc(dev);
1408195801Smav	int timeout = 0;
1409195801Smav	uint32_t val;
1410195801Smav
1411195801Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1412195801Smav	    SIIS_P_CTL_READY) == 0) {
1413195801Smav		DELAY(1000);
1414195801Smav		if (timeout++ > t) {
1415195801Smav			device_printf(dev, "port is not ready (timeout %dms) "
1416195801Smav			    "status = %08x\n", t, val);
1417195801Smav			return (EBUSY);
1418195801Smav		}
1419195801Smav	}
1420195801Smav	return (0);
1421195801Smav}
1422195801Smav
1423195801Smavstatic void
1424195801Smavsiis_reset(device_t dev)
1425195801Smav{
1426195801Smav	struct siis_channel *ch = device_get_softc(dev);
1427199821Smav	int i, retry = 0, sata_rev;
1428198426Smav	uint32_t val;
1429195801Smav
1430203108Smav	xpt_freeze_simq(ch->sim, 1);
1431195801Smav	if (bootverbose)
1432195801Smav		device_printf(dev, "SIIS reset...\n");
1433198852Smav	if (!ch->readlog && !ch->recovery)
1434198852Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1435198852Smav	/* Requeue frozen command. */
1436195801Smav	if (ch->frozen) {
1437195801Smav		union ccb *fccb = ch->frozen;
1438195801Smav		ch->frozen = NULL;
1439198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1440198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1441198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1442198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
1443198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1444198321Smav		}
1445195801Smav		xpt_done(fccb);
1446195801Smav	}
1447198426Smav	/* Requeue all running commands. */
1448195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1449195801Smav		/* Do we have a running request on slot? */
1450195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1451195801Smav			continue;
1452195801Smav		/* XXX; Commands in loading state. */
1453195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1454195801Smav	}
1455198852Smav	/* Finish all holden commands as-is. */
1456198852Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1457198852Smav		if (!ch->hold[i])
1458198852Smav			continue;
1459198852Smav		xpt_done(ch->hold[i]);
1460198852Smav		ch->hold[i] = NULL;
1461198852Smav		ch->numhslots--;
1462198852Smav	}
1463198852Smav	if (ch->toslots != 0)
1464198852Smav		xpt_release_simq(ch->sim, TRUE);
1465198852Smav	ch->eslots = 0;
1466198852Smav	ch->recovery = 0;
1467198852Smav	ch->toslots = 0;
1468198852Smav	ch->fatalerr = 0;
1469198426Smav	/* Disable port interrupts */
1470198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1471198426Smav	/* Set speed limit. */
1472199821Smav	sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
1473199821Smav	if (sata_rev == 1)
1474198426Smav		val = ATA_SC_SPD_SPEED_GEN1;
1475199821Smav	else if (sata_rev == 2)
1476198426Smav		val = ATA_SC_SPD_SPEED_GEN2;
1477199821Smav	else if (sata_rev == 3)
1478198426Smav		val = ATA_SC_SPD_SPEED_GEN3;
1479198426Smav	else
1480198426Smav		val = 0;
1481198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1482198426Smav	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1483198426Smav	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1484198852Smavretry:
1485198426Smav	siis_devreset(dev);
1486195801Smav	/* Reset and reconnect PHY, */
1487198426Smav	if (!siis_sata_connect(ch)) {
1488195801Smav		ch->devices = 0;
1489195801Smav		/* Enable port interrupts */
1490195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1491195801Smav		if (bootverbose)
1492195801Smav			device_printf(dev,
1493195801Smav			    "SIIS reset done: phy reset found no device\n");
1494195801Smav		/* Tell the XPT about the event */
1495195801Smav		xpt_async(AC_BUS_RESET, ch->path, NULL);
1496203108Smav		xpt_release_simq(ch->sim, TRUE);
1497195801Smav		return;
1498195801Smav	}
1499195801Smav	/* Wait for clearing busy status. */
1500198852Smav	if (siis_wait_ready(dev, 10000)) {
1501195801Smav		device_printf(dev, "device ready timeout\n");
1502198852Smav		if (!retry) {
1503198852Smav			device_printf(dev, "trying full port reset ...\n");
1504198852Smav			/* Get port to the reset state. */
1505198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
1506198852Smav			DELAY(10000);
1507198852Smav			/* Get port out of reset state. */
1508198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
1509198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
1510198852Smav			if (ch->pm_present)
1511198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1512198852Smav			else
1513198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1514198852Smav			siis_wait_ready(dev, 5000);
1515198852Smav			retry = 1;
1516198852Smav			goto retry;
1517198852Smav		}
1518198852Smav	}
1519195801Smav	ch->devices = 1;
1520195801Smav	/* Enable port interrupts */
1521195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1522195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1523195801Smav	if (bootverbose)
1524195801Smav		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1525195801Smav	/* Tell the XPT about the event */
1526195801Smav	xpt_async(AC_BUS_RESET, ch->path, NULL);
1527203108Smav	xpt_release_simq(ch->sim, TRUE);
1528195801Smav}
1529195801Smav
1530195801Smavstatic int
1531199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag)
1532195801Smav{
1533199821Smav	struct siis_channel *ch = device_get_softc(dev);
1534195801Smav	u_int8_t *fis = &ctp->fis[0];
1535195801Smav
1536195801Smav	bzero(fis, 24);
1537195801Smav	fis[0] = 0x27;  		/* host to device */
1538195801Smav	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1539195801Smav	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1540195801Smav		fis[1] |= 0x80;
1541195801Smav		fis[2] = ATA_PACKET_CMD;
1542199821Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1543199821Smav		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1544195801Smav			fis[3] = ATA_F_DMA;
1545195801Smav		else {
1546195801Smav			fis[5] = ccb->csio.dxfer_len;
1547195801Smav		        fis[6] = ccb->csio.dxfer_len >> 8;
1548195801Smav		}
1549195801Smav		fis[7] = ATA_D_LBA;
1550195801Smav		fis[15] = ATA_A_4BIT;
1551195801Smav		bzero(ctp->u.atapi.ccb, 16);
1552195801Smav		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1553195801Smav		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1554195801Smav		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1555195801Smav	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1556195801Smav		fis[1] |= 0x80;
1557195801Smav		fis[2] = ccb->ataio.cmd.command;
1558195801Smav		fis[3] = ccb->ataio.cmd.features;
1559195801Smav		fis[4] = ccb->ataio.cmd.lba_low;
1560195801Smav		fis[5] = ccb->ataio.cmd.lba_mid;
1561195801Smav		fis[6] = ccb->ataio.cmd.lba_high;
1562195801Smav		fis[7] = ccb->ataio.cmd.device;
1563195801Smav		fis[8] = ccb->ataio.cmd.lba_low_exp;
1564195801Smav		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1565195801Smav		fis[10] = ccb->ataio.cmd.lba_high_exp;
1566195801Smav		fis[11] = ccb->ataio.cmd.features_exp;
1567195801Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1568195801Smav			fis[12] = tag << 3;
1569195801Smav			fis[13] = 0;
1570195801Smav		} else {
1571195801Smav			fis[12] = ccb->ataio.cmd.sector_count;
1572195801Smav			fis[13] = ccb->ataio.cmd.sector_count_exp;
1573195801Smav		}
1574195801Smav		fis[15] = ATA_A_4BIT;
1575195801Smav	} else {
1576195801Smav		/* Soft reset. */
1577195801Smav	}
1578195801Smav	return (20);
1579195801Smav}
1580195801Smav
1581195801Smavstatic int
1582195801Smavsiis_sata_connect(struct siis_channel *ch)
1583195801Smav{
1584195801Smav	u_int32_t status;
1585195801Smav	int timeout;
1586195801Smav
1587195801Smav	/* Wait up to 100ms for "connect well" */
1588195801Smav	for (timeout = 0; timeout < 100 ; timeout++) {
1589195801Smav		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1590195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1591195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1592195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1593195801Smav			break;
1594195801Smav		DELAY(1000);
1595195801Smav	}
1596195801Smav	if (timeout >= 100) {
1597195801Smav		if (bootverbose) {
1598195801Smav			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1599195801Smav			    status);
1600195801Smav		}
1601195801Smav		return (0);
1602195801Smav	}
1603195801Smav	if (bootverbose) {
1604195801Smav		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1605195801Smav		    timeout, status);
1606195801Smav	}
1607195801Smav	/* Clear SATA error register */
1608195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1609195801Smav	return (1);
1610195801Smav}
1611195801Smav
1612195801Smavstatic void
1613195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb)
1614195801Smav{
1615195801Smav	device_t dev;
1616195801Smav	struct siis_channel *ch;
1617195801Smav
1618195801Smav	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1619195801Smav	    ccb->ccb_h.func_code));
1620195801Smav
1621195801Smav	ch = (struct siis_channel *)cam_sim_softc(sim);
1622195801Smav	dev = ch->dev;
1623195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1624195801Smav	switch (ccb->ccb_h.func_code) {
1625195801Smav	/* Common cases first */
1626195801Smav	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1627195801Smav	case XPT_SCSI_IO:
1628195801Smav		if (ch->devices == 0) {
1629195801Smav			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1630195801Smav			xpt_done(ccb);
1631195801Smav			break;
1632195801Smav		}
1633195801Smav		/* Check for command collision. */
1634195801Smav		if (siis_check_collision(dev, ccb)) {
1635195801Smav			/* Freeze command. */
1636195801Smav			ch->frozen = ccb;
1637195801Smav			/* We have only one frozen slot, so freeze simq also. */
1638195801Smav			xpt_freeze_simq(ch->sim, 1);
1639195801Smav			return;
1640195801Smav		}
1641195801Smav		siis_begin_transaction(dev, ccb);
1642195801Smav		break;
1643195801Smav	case XPT_EN_LUN:		/* Enable LUN as a target */
1644195801Smav	case XPT_TARGET_IO:		/* Execute target I/O request */
1645195801Smav	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1646195801Smav	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1647195801Smav	case XPT_ABORT:			/* Abort the specified CCB */
1648195801Smav		/* XXX Implement */
1649195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1650195801Smav		xpt_done(ccb);
1651195801Smav		break;
1652195801Smav	case XPT_SET_TRAN_SETTINGS:
1653195801Smav	{
1654195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1655199747Smav		struct	siis_device *d;
1656195801Smav
1657199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1658199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1659199747Smav		else
1660199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1661199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1662199747Smav			d->revision = cts->xport_specific.sata.revision;
1663199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1664199747Smav			d->mode = cts->xport_specific.sata.mode;
1665199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1666199747Smav			d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1667199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1668199747Smav			d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags);
1669195801Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1670198852Smav			ch->pm_present = cts->xport_specific.sata.pm_present;
1671198852Smav			if (ch->pm_present)
1672195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1673195801Smav			else
1674195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1675195801Smav		}
1676203376Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1677203376Smav			d->atapi = cts->xport_specific.sata.atapi;
1678195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1679195801Smav		xpt_done(ccb);
1680195801Smav		break;
1681195801Smav	}
1682195801Smav	case XPT_GET_TRAN_SETTINGS:
1683195801Smav	/* Get default/user set transfer settings for the target */
1684195801Smav	{
1685195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1686199747Smav		struct  siis_device *d;
1687195801Smav		uint32_t status;
1688195801Smav
1689199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1690199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1691199747Smav		else
1692199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1693195801Smav		cts->protocol = PROTO_ATA;
1694196655Smav		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1695195801Smav		cts->transport = XPORT_SATA;
1696196655Smav		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1697195801Smav		cts->proto_specific.valid = 0;
1698195801Smav		cts->xport_specific.sata.valid = 0;
1699199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1700199747Smav		    (ccb->ccb_h.target_id == 15 ||
1701199747Smav		    (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
1702195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1703199747Smav			if (status & 0x0f0) {
1704199747Smav				cts->xport_specific.sata.revision =
1705199747Smav				    (status & 0x0f0) >> 4;
1706199747Smav				cts->xport_specific.sata.valid |=
1707199747Smav				    CTS_SATA_VALID_REVISION;
1708199747Smav			}
1709199747Smav		} else {
1710199747Smav			cts->xport_specific.sata.revision = d->revision;
1711199747Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1712195801Smav		}
1713199747Smav		cts->xport_specific.sata.mode = d->mode;
1714199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1715199747Smav		cts->xport_specific.sata.bytecount = d->bytecount;
1716199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1717198852Smav		cts->xport_specific.sata.pm_present = ch->pm_present;
1718195801Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1719199747Smav		cts->xport_specific.sata.tags = d->tags;
1720199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
1721203376Smav		cts->xport_specific.sata.atapi = d->atapi;
1722203376Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1723195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1724195801Smav		xpt_done(ccb);
1725195801Smav		break;
1726195801Smav	}
1727195801Smav#if 0
1728195801Smav	case XPT_CALC_GEOMETRY:
1729195801Smav	{
1730195801Smav		struct	  ccb_calc_geometry *ccg;
1731195801Smav		uint32_t size_mb;
1732195801Smav		uint32_t secs_per_cylinder;
1733195801Smav
1734195801Smav		ccg = &ccb->ccg;
1735195801Smav		size_mb = ccg->volume_size
1736195801Smav			/ ((1024L * 1024L) / ccg->block_size);
1737195801Smav		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1738195801Smav			if (size_mb >= 2048) {
1739195801Smav				ccg->heads = 255;
1740195801Smav				ccg->secs_per_track = 63;
1741195801Smav			} else {
1742195801Smav				ccg->heads = 128;
1743195801Smav				ccg->secs_per_track = 32;
1744195801Smav			}
1745195801Smav		} else {
1746195801Smav			ccg->heads = 64;
1747195801Smav			ccg->secs_per_track = 32;
1748195801Smav		}
1749195801Smav		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1750195801Smav		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1751195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1752195801Smav		xpt_done(ccb);
1753195801Smav		break;
1754195801Smav	}
1755195801Smav#endif
1756195801Smav	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1757195801Smav	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1758195801Smav		siis_reset(dev);
1759195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1760195801Smav		xpt_done(ccb);
1761195801Smav		break;
1762195801Smav	case XPT_TERM_IO:		/* Terminate the I/O process */
1763195801Smav		/* XXX Implement */
1764195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1765195801Smav		xpt_done(ccb);
1766195801Smav		break;
1767195801Smav	case XPT_PATH_INQ:		/* Path routing inquiry */
1768195801Smav	{
1769195801Smav		struct ccb_pathinq *cpi = &ccb->cpi;
1770195801Smav
1771195801Smav		cpi->version_num = 1; /* XXX??? */
1772195801Smav		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1773195801Smav		cpi->hba_inquiry |= PI_SATAPM;
1774195801Smav		cpi->target_sprt = 0;
1775195801Smav		cpi->hba_misc = PIM_SEQSCAN;
1776195801Smav		cpi->hba_eng_cnt = 0;
1777198322Smav		cpi->max_target = 15;
1778195801Smav		cpi->max_lun = 0;
1779195801Smav		cpi->initiator_id = 0;
1780195801Smav		cpi->bus_id = cam_sim_bus(sim);
1781195801Smav		cpi->base_transfer_speed = 150000;
1782195801Smav		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1783195801Smav		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1784195801Smav		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1785195801Smav		cpi->unit_number = cam_sim_unit(sim);
1786195801Smav		cpi->transport = XPORT_SATA;
1787196655Smav		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1788195801Smav		cpi->protocol = PROTO_ATA;
1789196655Smav		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1790195801Smav		cpi->ccb_h.status = CAM_REQ_CMP;
1791195801Smav		cpi->maxio = MAXPHYS;
1792195801Smav		xpt_done(ccb);
1793195801Smav		break;
1794195801Smav	}
1795195801Smav	default:
1796195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1797195801Smav		xpt_done(ccb);
1798195801Smav		break;
1799195801Smav	}
1800195801Smav}
1801195801Smav
1802195801Smavstatic void
1803195801Smavsiispoll(struct cam_sim *sim)
1804195801Smav{
1805195801Smav	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1806195801Smav
1807195801Smav	siis_ch_intr(ch->dev);
1808195801Smav}
1809