siis.c revision 201222
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 201222 2009-12-29 21:49:11Z 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_xpt_periph.h>
56195801Smav#include <cam/cam_debug.h>
57195801Smav
58195801Smav/* local prototypes */
59195801Smavstatic int siis_setup_interrupt(device_t dev);
60195801Smavstatic void siis_intr(void *data);
61195801Smavstatic int siis_suspend(device_t dev);
62195801Smavstatic int siis_resume(device_t dev);
63195801Smavstatic int siis_ch_suspend(device_t dev);
64195801Smavstatic int siis_ch_resume(device_t dev);
65195801Smavstatic void siis_ch_intr_locked(void *data);
66195801Smavstatic void siis_ch_intr(void *data);
67195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb);
68195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
69195801Smavstatic void siis_execute_transaction(struct siis_slot *slot);
70195801Smavstatic void siis_timeout(struct siis_slot *slot);
71195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
72199821Smavstatic int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag);
73195801Smavstatic void siis_dmainit(device_t dev);
74195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
75195801Smavstatic void siis_dmafini(device_t dev);
76195801Smavstatic void siis_slotsalloc(device_t dev);
77195801Smavstatic void siis_slotsfree(device_t dev);
78195801Smavstatic void siis_reset(device_t dev);
79195801Smavstatic void siis_portinit(device_t dev);
80195801Smavstatic int siis_wait_ready(device_t dev, int t);
81195801Smav
82195801Smavstatic int siis_sata_connect(struct siis_channel *ch);
83195801Smav
84195801Smavstatic void siis_issue_read_log(device_t dev);
85195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb);
86195801Smav
87195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb);
88195801Smavstatic void siispoll(struct cam_sim *sim);
89195801Smav
90195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
91195801Smav
92199132Smavstatic struct {
93199132Smav	uint32_t	id;
94199132Smav	const char	*name;
95199132Smav	int		ports;
96200217Smav	int		quirks;
97200217Smav#define SIIS_Q_SNTF	1
98199132Smav} siis_ids[] = {
99200217Smav	{0x31241095,	"SiI3124",	4,	0},
100200217Smav	{0x31248086,	"SiI3124",	4,	0},
101200217Smav	{0x31321095,	"SiI3132",	2,	SIIS_Q_SNTF},
102200217Smav	{0x02421095,	"SiI3132",	2,	SIIS_Q_SNTF},
103200217Smav	{0x02441095,	"SiI3132",	2,	SIIS_Q_SNTF},
104200217Smav	{0x31311095,	"SiI3131",	1,	SIIS_Q_SNTF},
105200217Smav	{0x35311095,	"SiI3531",	1,	SIIS_Q_SNTF},
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);
234200291Smav	int cap;
235200291Smav	uint16_t val;
236195801Smav
237200291Smav	/* Set PCIe max read request size to at least 1024 bytes */
238200291Smav	if (pci_find_extcap(dev, PCIY_EXPRESS, &cap) == 0) {
239200291Smav		val = pci_read_config(dev,
240200291Smav		    cap + PCIR_EXPRESS_DEVICE_CTL, 2);
241200291Smav		if ((val & PCIM_EXP_CTL_MAX_READ_REQUEST) < 0x3000) {
242200291Smav			val &= ~PCIM_EXP_CTL_MAX_READ_REQUEST;
243200291Smav			val |= 0x3000;
244200291Smav			pci_write_config(dev,
245200291Smav			    cap + PCIR_EXPRESS_DEVICE_CTL, val, 2);
246200291Smav		}
247200291Smav	}
248195801Smav	/* Put controller into reset state. */
249200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
250200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
251195801Smav	DELAY(10000);
252195801Smav	/* Get controller out of reset state and enable port interrupts. */
253200223Smav	ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE);
254200223Smav	ctlr->gctl |= 0x0000000f;
255200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
256195801Smav	return (bus_generic_resume(dev));
257195801Smav}
258195801Smav
259195801Smavstatic int
260195801Smavsiis_setup_interrupt(device_t dev)
261195801Smav{
262195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
263195801Smav	int msi = 0;
264195801Smav
265195801Smav	/* Process hints. */
266195801Smav	resource_int_value(device_get_name(dev),
267195801Smav	    device_get_unit(dev), "msi", &msi);
268195801Smav	if (msi < 0)
269195801Smav		msi = 0;
270195801Smav	else if (msi > 0)
271195801Smav		msi = min(1, pci_msi_count(dev));
272195801Smav	/* Allocate MSI if needed/present. */
273195801Smav	if (msi && pci_alloc_msi(dev, &msi) != 0)
274195801Smav		msi = 0;
275195801Smav	/* Allocate all IRQs. */
276195801Smav	ctlr->irq.r_irq_rid = msi ? 1 : 0;
277195801Smav	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
278195801Smav	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
279195801Smav		device_printf(dev, "unable to map interrupt\n");
280195801Smav		return ENXIO;
281195801Smav	}
282195801Smav	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
283195801Smav	    siis_intr, ctlr, &ctlr->irq.handle))) {
284195801Smav		/* SOS XXX release r_irq */
285195801Smav		device_printf(dev, "unable to setup interrupt\n");
286195801Smav		return ENXIO;
287195801Smav	}
288195801Smav	return (0);
289195801Smav}
290195801Smav
291195801Smav/*
292195801Smav * Common case interrupt handler.
293195801Smav */
294195801Smavstatic void
295195801Smavsiis_intr(void *data)
296195801Smav{
297195801Smav	struct siis_controller *ctlr = (struct siis_controller *)data;
298195801Smav	u_int32_t is;
299195801Smav	void *arg;
300195801Smav	int unit;
301195801Smav
302195801Smav	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
303195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
304195801Smav		if ((is & SIIS_IS_PORT(unit)) != 0 &&
305195801Smav		    (arg = ctlr->interrupt[unit].argument)) {
306195801Smav			ctlr->interrupt[unit].function(arg);
307195801Smav		}
308195801Smav	}
309200223Smav	/* Acknowledge interrupt, if MSI enabled. */
310200223Smav	if (ctlr->irq.r_irq_rid) {
311200223Smav		ATA_OUTL(ctlr->r_gmem, SIIS_GCTL,
312200223Smav		    ctlr->gctl | SIIS_GCTL_MSIACK);
313200223Smav	}
314195801Smav}
315195801Smav
316195801Smavstatic struct resource *
317195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid,
318195801Smav		       u_long start, u_long end, u_long count, u_int flags)
319195801Smav{
320195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
321195801Smav	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
322195801Smav	struct resource *res = NULL;
323195801Smav	int offset = unit << 13;
324195801Smav	long st;
325195801Smav
326195801Smav	switch (type) {
327195801Smav	case SYS_RES_MEMORY:
328195801Smav		st = rman_get_start(ctlr->r_mem);
329195801Smav		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
330195801Smav		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
331195801Smav		if (res) {
332195801Smav			bus_space_handle_t bsh;
333195801Smav			bus_space_tag_t bst;
334195801Smav			bsh = rman_get_bushandle(ctlr->r_mem);
335195801Smav			bst = rman_get_bustag(ctlr->r_mem);
336195801Smav			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
337195801Smav			rman_set_bushandle(res, bsh);
338195801Smav			rman_set_bustag(res, bst);
339195801Smav		}
340195801Smav		break;
341195801Smav	case SYS_RES_IRQ:
342195801Smav		if (*rid == ATA_IRQ_RID)
343195801Smav			res = ctlr->irq.r_irq;
344195801Smav		break;
345195801Smav	}
346195801Smav	return (res);
347195801Smav}
348195801Smav
349195801Smavstatic int
350195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid,
351195801Smav			 struct resource *r)
352195801Smav{
353195801Smav
354195801Smav	switch (type) {
355195801Smav	case SYS_RES_MEMORY:
356195801Smav		rman_release_resource(r);
357195801Smav		return (0);
358195801Smav	case SYS_RES_IRQ:
359195801Smav		if (rid != ATA_IRQ_RID)
360195801Smav			return ENOENT;
361195801Smav		return (0);
362195801Smav	}
363195801Smav	return (EINVAL);
364195801Smav}
365195801Smav
366195801Smavstatic int
367195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq,
368195801Smav		   int flags, driver_filter_t *filter, driver_intr_t *function,
369195801Smav		   void *argument, void **cookiep)
370195801Smav{
371195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
372195801Smav	int unit = (intptr_t)device_get_ivars(child);
373195801Smav
374195801Smav	if (filter != NULL) {
375195801Smav		printf("siis.c: we cannot use a filter here\n");
376195801Smav		return (EINVAL);
377195801Smav	}
378195801Smav	ctlr->interrupt[unit].function = function;
379195801Smav	ctlr->interrupt[unit].argument = argument;
380195801Smav	return (0);
381195801Smav}
382195801Smav
383195801Smavstatic int
384195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq,
385195801Smav		      void *cookie)
386195801Smav{
387195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
388195801Smav	int unit = (intptr_t)device_get_ivars(child);
389195801Smav
390195801Smav	ctlr->interrupt[unit].function = NULL;
391195801Smav	ctlr->interrupt[unit].argument = NULL;
392195801Smav	return (0);
393195801Smav}
394195801Smav
395195801Smavstatic int
396195801Smavsiis_print_child(device_t dev, device_t child)
397195801Smav{
398195801Smav	int retval;
399195801Smav
400195801Smav	retval = bus_print_child_header(dev, child);
401195801Smav	retval += printf(" at channel %d",
402195801Smav	    (int)(intptr_t)device_get_ivars(child));
403195801Smav	retval += bus_print_child_footer(dev, child);
404195801Smav
405195801Smav	return (retval);
406195801Smav}
407195801Smav
408195801Smavdevclass_t siis_devclass;
409195801Smavstatic device_method_t siis_methods[] = {
410195801Smav	DEVMETHOD(device_probe,     siis_probe),
411195801Smav	DEVMETHOD(device_attach,    siis_attach),
412195801Smav	DEVMETHOD(device_detach,    siis_detach),
413195801Smav	DEVMETHOD(device_suspend,   siis_suspend),
414195801Smav	DEVMETHOD(device_resume,    siis_resume),
415195801Smav	DEVMETHOD(bus_print_child,  siis_print_child),
416195801Smav	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
417195801Smav	DEVMETHOD(bus_release_resource,     siis_release_resource),
418195801Smav	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
419195801Smav	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
420195801Smav	{ 0, 0 }
421195801Smav};
422195801Smavstatic driver_t siis_driver = {
423195801Smav        "siis",
424195801Smav        siis_methods,
425195801Smav        sizeof(struct siis_controller)
426195801Smav};
427195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
428195801SmavMODULE_VERSION(siis, 1);
429195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1);
430195801Smav
431195801Smavstatic int
432195801Smavsiis_ch_probe(device_t dev)
433195801Smav{
434195801Smav
435195801Smav	device_set_desc_copy(dev, "SIIS channel");
436195801Smav	return (0);
437195801Smav}
438195801Smav
439195801Smavstatic int
440195801Smavsiis_ch_attach(device_t dev)
441195801Smav{
442200217Smav	struct siis_controller *ctlr = device_get_softc(device_get_parent(dev));
443195801Smav	struct siis_channel *ch = device_get_softc(dev);
444195801Smav	struct cam_devq *devq;
445199821Smav	int rid, error, i, sata_rev = 0;
446195801Smav
447195801Smav	ch->dev = dev;
448195801Smav	ch->unit = (intptr_t)device_get_ivars(dev);
449200217Smav	ch->quirks = ctlr->quirks;
450195801Smav	resource_int_value(device_get_name(dev),
451195801Smav	    device_get_unit(dev), "pm_level", &ch->pm_level);
452199821Smav	resource_int_value(device_get_name(dev),
453199821Smav	    device_get_unit(dev), "sata_rev", &sata_rev);
454199747Smav	for (i = 0; i < 16; i++) {
455199821Smav		ch->user[i].revision = sata_rev;
456199747Smav		ch->user[i].mode = 0;
457199747Smav		ch->user[i].bytecount = 8192;
458199747Smav		ch->user[i].tags = SIIS_MAX_SLOTS;
459199747Smav		ch->curr[i] = ch->user[i];
460199747Smav	}
461195801Smav	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
462195801Smav	rid = ch->unit;
463195801Smav	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
464195801Smav	    &rid, RF_ACTIVE)))
465195801Smav		return (ENXIO);
466195801Smav	siis_dmainit(dev);
467195801Smav	siis_slotsalloc(dev);
468195801Smav	siis_ch_resume(dev);
469195801Smav	mtx_lock(&ch->mtx);
470195801Smav	rid = ATA_IRQ_RID;
471195801Smav	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
472195801Smav	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
473195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
474195801Smav		device_printf(dev, "Unable to map interrupt\n");
475195801Smav		return (ENXIO);
476195801Smav	}
477195801Smav	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
478195801Smav	    siis_ch_intr_locked, dev, &ch->ih))) {
479195801Smav		device_printf(dev, "Unable to setup interrupt\n");
480195801Smav		error = ENXIO;
481195801Smav		goto err1;
482195801Smav	}
483195801Smav	/* Create the device queue for our SIM. */
484195801Smav	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
485195801Smav	if (devq == NULL) {
486195801Smav		device_printf(dev, "Unable to allocate simq\n");
487195801Smav		error = ENOMEM;
488195801Smav		goto err1;
489195801Smav	}
490195801Smav	/* Construct SIM entry */
491195801Smav	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
492199178Smav	    device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq);
493195801Smav	if (ch->sim == NULL) {
494195801Smav		device_printf(dev, "unable to allocate sim\n");
495195801Smav		error = ENOMEM;
496195801Smav		goto err2;
497195801Smav	}
498195801Smav	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
499195801Smav		device_printf(dev, "unable to register xpt bus\n");
500195801Smav		error = ENXIO;
501195801Smav		goto err2;
502195801Smav	}
503195801Smav	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
504195801Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
505195801Smav		device_printf(dev, "unable to create path\n");
506195801Smav		error = ENXIO;
507195801Smav		goto err3;
508195801Smav	}
509195801Smav	mtx_unlock(&ch->mtx);
510195801Smav	return (0);
511195801Smav
512195801Smaverr3:
513195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
514195801Smaverr2:
515195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
516195801Smaverr1:
517195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
518195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
519195801Smav	mtx_unlock(&ch->mtx);
520195801Smav	return (error);
521195801Smav}
522195801Smav
523195801Smavstatic int
524195801Smavsiis_ch_detach(device_t dev)
525195801Smav{
526195801Smav	struct siis_channel *ch = device_get_softc(dev);
527195801Smav
528195801Smav	mtx_lock(&ch->mtx);
529195801Smav	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
530195801Smav	xpt_free_path(ch->path);
531195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
532195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
533195801Smav	mtx_unlock(&ch->mtx);
534195801Smav
535195801Smav	bus_teardown_intr(dev, ch->r_irq, ch->ih);
536195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
537195801Smav
538195801Smav	siis_ch_suspend(dev);
539195801Smav	siis_slotsfree(dev);
540195801Smav	siis_dmafini(dev);
541195801Smav
542195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
543195801Smav	mtx_destroy(&ch->mtx);
544195801Smav	return (0);
545195801Smav}
546195801Smav
547195801Smavstatic int
548195801Smavsiis_ch_suspend(device_t dev)
549195801Smav{
550195801Smav	struct siis_channel *ch = device_get_softc(dev);
551195801Smav
552195801Smav	/* Put port into reset state. */
553195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
554195801Smav	return (0);
555195801Smav}
556195801Smav
557195801Smavstatic int
558195801Smavsiis_ch_resume(device_t dev)
559195801Smav{
560195801Smav	struct siis_channel *ch = device_get_softc(dev);
561195801Smav
562195801Smav	/* Get port out of reset state. */
563195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
564195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
565198852Smav	if (ch->pm_present)
566198852Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
567198852Smav	else
568198852Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
569195801Smav	/* Enable port interrupts */
570195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
571195801Smav	return (0);
572195801Smav}
573195801Smav
574195801Smavdevclass_t siisch_devclass;
575195801Smavstatic device_method_t siisch_methods[] = {
576195801Smav	DEVMETHOD(device_probe,     siis_ch_probe),
577195801Smav	DEVMETHOD(device_attach,    siis_ch_attach),
578195801Smav	DEVMETHOD(device_detach,    siis_ch_detach),
579195801Smav	DEVMETHOD(device_suspend,   siis_ch_suspend),
580195801Smav	DEVMETHOD(device_resume,    siis_ch_resume),
581195801Smav	{ 0, 0 }
582195801Smav};
583195801Smavstatic driver_t siisch_driver = {
584195801Smav        "siisch",
585195801Smav        siisch_methods,
586195801Smav        sizeof(struct siis_channel)
587195801Smav};
588195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
589195801Smav
590195801Smavstruct siis_dc_cb_args {
591195801Smav	bus_addr_t maddr;
592195801Smav	int error;
593195801Smav};
594195801Smav
595195801Smavstatic void
596195801Smavsiis_dmainit(device_t dev)
597195801Smav{
598195801Smav	struct siis_channel *ch = device_get_softc(dev);
599195801Smav	struct siis_dc_cb_args dcba;
600195801Smav
601195801Smav	/* Command area. */
602195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
603195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
604195801Smav	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
605195801Smav	    0, NULL, NULL, &ch->dma.work_tag))
606195801Smav		goto error;
607195801Smav	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
608195801Smav	    &ch->dma.work_map))
609195801Smav		goto error;
610195801Smav	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
611195801Smav	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
612195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
613195801Smav		goto error;
614195801Smav	}
615195801Smav	ch->dma.work_bus = dcba.maddr;
616195801Smav	/* Data area. */
617199333Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
618195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
619195801Smav	    NULL, NULL,
620195801Smav	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
621195801Smav	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
622195801Smav	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
623195801Smav		goto error;
624195801Smav	}
625195801Smav	return;
626195801Smav
627195801Smaverror:
628195801Smav	device_printf(dev, "WARNING - DMA initialization failed\n");
629195801Smav	siis_dmafini(dev);
630195801Smav}
631195801Smav
632195801Smavstatic void
633195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
634195801Smav{
635195801Smav	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
636195801Smav
637195801Smav	if (!(dcba->error = error))
638195801Smav		dcba->maddr = segs[0].ds_addr;
639195801Smav}
640195801Smav
641195801Smavstatic void
642195801Smavsiis_dmafini(device_t dev)
643195801Smav{
644195801Smav	struct siis_channel *ch = device_get_softc(dev);
645195801Smav
646195801Smav	if (ch->dma.data_tag) {
647195801Smav		bus_dma_tag_destroy(ch->dma.data_tag);
648195801Smav		ch->dma.data_tag = NULL;
649195801Smav	}
650195801Smav	if (ch->dma.work_bus) {
651195801Smav		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
652195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
653195801Smav		ch->dma.work_bus = 0;
654195801Smav		ch->dma.work_map = NULL;
655195801Smav		ch->dma.work = NULL;
656195801Smav	}
657195801Smav	if (ch->dma.work_tag) {
658195801Smav		bus_dma_tag_destroy(ch->dma.work_tag);
659195801Smav		ch->dma.work_tag = NULL;
660195801Smav	}
661195801Smav}
662195801Smav
663195801Smavstatic void
664195801Smavsiis_slotsalloc(device_t dev)
665195801Smav{
666195801Smav	struct siis_channel *ch = device_get_softc(dev);
667195801Smav	int i;
668195801Smav
669195801Smav	/* Alloc and setup command/dma slots */
670195801Smav	bzero(ch->slot, sizeof(ch->slot));
671195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
672195801Smav		struct siis_slot *slot = &ch->slot[i];
673195801Smav
674195801Smav		slot->dev = dev;
675195801Smav		slot->slot = i;
676195801Smav		slot->state = SIIS_SLOT_EMPTY;
677195801Smav		slot->ccb = NULL;
678195801Smav		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
679195801Smav
680195801Smav		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
681195801Smav			device_printf(ch->dev, "FAILURE - create data_map\n");
682195801Smav	}
683195801Smav}
684195801Smav
685195801Smavstatic void
686195801Smavsiis_slotsfree(device_t dev)
687195801Smav{
688195801Smav	struct siis_channel *ch = device_get_softc(dev);
689195801Smav	int i;
690195801Smav
691195801Smav	/* Free all dma slots */
692195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
693195801Smav		struct siis_slot *slot = &ch->slot[i];
694195801Smav
695198896Smav		callout_drain(&slot->timeout);
696195801Smav		if (slot->dma.data_map) {
697195801Smav			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
698195801Smav			slot->dma.data_map = NULL;
699195801Smav		}
700195801Smav	}
701195801Smav}
702195801Smav
703195801Smavstatic void
704196655Smavsiis_notify_events(device_t dev)
705196655Smav{
706196655Smav	struct siis_channel *ch = device_get_softc(dev);
707196655Smav	struct cam_path *dpath;
708196655Smav	u_int32_t status;
709196655Smav	int i;
710196655Smav
711200217Smav	if (ch->quirks & SIIS_Q_SNTF) {
712200217Smav		status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
713200217Smav		ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
714200217Smav	} else {
715200217Smav		/*
716200217Smav		 * Without SNTF we have no idea which device sent notification.
717200217Smav		 * If PMP is connected, assume it, else - device.
718200217Smav		 */
719200217Smav		status = (ch->pm_present) ? 0x8000 : 0x0001;
720200217Smav	}
721196655Smav	if (bootverbose)
722196655Smav		device_printf(dev, "SNTF 0x%04x\n", status);
723196655Smav	for (i = 0; i < 16; i++) {
724196655Smav		if ((status & (1 << i)) == 0)
725196655Smav			continue;
726196655Smav		if (xpt_create_path(&dpath, NULL,
727196655Smav		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
728196655Smav			xpt_async(AC_SCSI_AEN, dpath, NULL);
729196655Smav			xpt_free_path(dpath);
730196655Smav		}
731196655Smav	}
732196655Smav
733196655Smav}
734196655Smav
735196655Smavstatic void
736195801Smavsiis_phy_check_events(device_t dev)
737195801Smav{
738195801Smav	struct siis_channel *ch = device_get_softc(dev);
739195801Smav
740195801Smav	/* If we have a connection event, deal with it */
741195801Smav	if (ch->pm_level == 0) {
742195801Smav		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
743195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
744195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
745195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
746195801Smav			if (bootverbose)
747195801Smav				device_printf(dev, "CONNECT requested\n");
748195801Smav			siis_reset(dev);
749195801Smav		} else {
750195801Smav			if (bootverbose)
751195801Smav				device_printf(dev, "DISCONNECT requested\n");
752195801Smav			ch->devices = 0;
753195801Smav		}
754195801Smav	}
755195801Smav}
756195801Smav
757195801Smavstatic void
758195801Smavsiis_ch_intr_locked(void *data)
759195801Smav{
760195801Smav	device_t dev = (device_t)data;
761195801Smav	struct siis_channel *ch = device_get_softc(dev);
762195801Smav
763195801Smav	mtx_lock(&ch->mtx);
764195801Smav	siis_ch_intr(data);
765195801Smav	mtx_unlock(&ch->mtx);
766195801Smav}
767195801Smav
768195801Smavstatic void
769195801Smavsiis_ch_intr(void *data)
770195801Smav{
771195801Smav	device_t dev = (device_t)data;
772195801Smav	struct siis_channel *ch = device_get_softc(dev);
773195801Smav	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
774195801Smav	enum siis_err_type et;
775195801Smav	int i, ccs, port, tslots;
776195801Smav
777195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
778195801Smav	/* Read command statuses. */
779195801Smav	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
780195801Smav	ok = ch->rslots & ~sstatus;
781195801Smav	/* Complete all successfull commands. */
782195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
783195801Smav		if ((ok >> i) & 1)
784195801Smav			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
785195801Smav	}
786195801Smav	/* Do we have any other events? */
787195801Smav	if ((sstatus & SIIS_P_SS_ATTN) == 0)
788195801Smav		return;
789195801Smav	/* Read and clear interrupt statuses. */
790195801Smav	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
791195801Smav	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
792195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
793195801Smav	/* Process PHY events */
794195801Smav	if (istatus & SIIS_P_IX_PHYRDYCHG)
795195801Smav		siis_phy_check_events(dev);
796196655Smav	/* Process NOTIFY events */
797196655Smav	if (istatus & SIIS_P_IX_SDBN)
798196655Smav		siis_notify_events(dev);
799195801Smav	/* Process command errors */
800195801Smav	if (istatus & SIIS_P_IX_COMMERR) {
801195801Smav		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
802195801Smav		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
803195801Smav		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
804195801Smav		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
805195801Smav		err = ch->rslots & sstatus;
806195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
807195801Smav//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
808195801Smav//    ATA_INL(ch->r_mem, SIIS_P_SERR));
809195801Smav
810195801Smav		if (!ch->readlog && !ch->recovery) {
811195801Smav			xpt_freeze_simq(ch->sim, ch->numrslots);
812195801Smav			ch->recovery = 1;
813195801Smav		}
814195801Smav		if (ch->frozen) {
815195801Smav			union ccb *fccb = ch->frozen;
816195801Smav			ch->frozen = NULL;
817198321Smav			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
818198321Smav			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
819198321Smav			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
820198321Smav				xpt_freeze_devq(fccb->ccb_h.path, 1);
821198321Smav				fccb->ccb_h.status |= CAM_DEV_QFRZN;
822198321Smav			}
823195801Smav			xpt_done(fccb);
824195801Smav		}
825195801Smav		if (estatus == SIIS_P_CMDERR_DEV ||
826195801Smav		    estatus == SIIS_P_CMDERR_SDB ||
827195801Smav		    estatus == SIIS_P_CMDERR_DATAFIS) {
828195801Smav			tslots = ch->numtslots[port];
829195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
830198852Smav				/* XXX: requests in loading state. */
831195801Smav				if (((ch->rslots >> i) & 1) == 0)
832195801Smav					continue;
833195801Smav				if (ch->slot[i].ccb->ccb_h.target_id != port)
834195801Smav					continue;
835195801Smav				if (tslots == 0) {
836195801Smav					/* Untagged operation. */
837195801Smav					if (i == ccs)
838195801Smav						et = SIIS_ERR_TFE;
839195801Smav					else
840195801Smav						et = SIIS_ERR_INNOCENT;
841195801Smav				} else {
842195801Smav					/* Tagged operation. */
843195801Smav					et = SIIS_ERR_NCQ;
844195801Smav				}
845195801Smav				siis_end_transaction(&ch->slot[i], et);
846195801Smav			}
847195801Smav			/*
848195801Smav			 * We can't reinit port if there are some other
849195801Smav			 * commands active, use resume to complete them.
850195801Smav			 */
851195801Smav			if (ch->rslots != 0)
852195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
853195801Smav		} else {
854195801Smav			if (estatus == SIIS_P_CMDERR_SENDFIS ||
855195801Smav			    estatus == SIIS_P_CMDERR_INCSTATE ||
856195801Smav			    estatus == SIIS_P_CMDERR_PPE ||
857195801Smav			    estatus == SIIS_P_CMDERR_SERVICE) {
858195801Smav				et = SIIS_ERR_SATA;
859195801Smav			} else
860195801Smav				et = SIIS_ERR_INVALID;
861195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
862198852Smav				/* XXX: requests in loading state. */
863195801Smav				if (((ch->rslots >> i) & 1) == 0)
864195801Smav					continue;
865195801Smav				siis_end_transaction(&ch->slot[i], et);
866195801Smav			}
867195801Smav		}
868195801Smav	}
869195801Smav}
870195801Smav
871195801Smav/* Must be called with channel locked. */
872195801Smavstatic int
873195801Smavsiis_check_collision(device_t dev, union ccb *ccb)
874195801Smav{
875195801Smav	struct siis_channel *ch = device_get_softc(dev);
876195801Smav
877195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
878195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
879199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
880199747Smav		/* Tagged command while we have no supported tag free. */
881199747Smav		if (((~ch->oslots) & (0x7fffffff >> (31 -
882199747Smav		    ch->curr[ccb->ccb_h.target_id].tags))) == 0)
883199747Smav			return (1);
884199747Smav	}
885199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
886195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
887195801Smav		/* Atomic command while anything active. */
888195801Smav		if (ch->numrslots != 0)
889195801Smav			return (1);
890195801Smav	}
891195801Smav       /* We have some atomic command running. */
892195801Smav       if (ch->aslots != 0)
893195801Smav               return (1);
894195801Smav	return (0);
895195801Smav}
896195801Smav
897195801Smav/* Must be called with channel locked. */
898195801Smavstatic void
899195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb)
900195801Smav{
901195801Smav	struct siis_channel *ch = device_get_softc(dev);
902195801Smav	struct siis_slot *slot;
903199747Smav	int tag, tags;
904195801Smav
905195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
906195801Smav	/* Choose empty slot. */
907199747Smav	tags = SIIS_MAX_SLOTS;
908199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
909199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
910199747Smav		tags = ch->curr[ccb->ccb_h.target_id].tags;
911199747Smav	tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1;
912195801Smav	/* Occupy chosen slot. */
913195801Smav	slot = &ch->slot[tag];
914195801Smav	slot->ccb = ccb;
915195801Smav	/* Update channel stats. */
916199747Smav	ch->oslots |= (1 << slot->slot);
917195801Smav	ch->numrslots++;
918195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
919195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
920195801Smav		ch->numtslots[ccb->ccb_h.target_id]++;
921195801Smav	}
922195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
923195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
924195801Smav		ch->aslots |= (1 << slot->slot);
925195801Smav	slot->dma.nsegs = 0;
926195801Smav	/* If request moves data, setup and load SG list */
927195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
928195801Smav		void *buf;
929195801Smav		bus_size_t size;
930195801Smav
931195801Smav		slot->state = SIIS_SLOT_LOADING;
932195801Smav		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
933195801Smav			buf = ccb->ataio.data_ptr;
934195801Smav			size = ccb->ataio.dxfer_len;
935195801Smav		} else {
936195801Smav			buf = ccb->csio.data_ptr;
937195801Smav			size = ccb->csio.dxfer_len;
938195801Smav		}
939195801Smav		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
940195801Smav		    buf, size, siis_dmasetprd, slot, 0);
941195801Smav	} else
942195801Smav		siis_execute_transaction(slot);
943195801Smav}
944195801Smav
945195801Smav/* Locked by busdma engine. */
946195801Smavstatic void
947195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
948195801Smav{
949195801Smav	struct siis_slot *slot = arg;
950195801Smav	struct siis_channel *ch = device_get_softc(slot->dev);
951195801Smav	struct siis_cmd *ctp;
952195801Smav	struct siis_dma_prd *prd;
953195801Smav	int i;
954195801Smav
955195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
956195801Smav	if (error) {
957195801Smav		device_printf(slot->dev, "DMA load error\n");
958195801Smav		if (!ch->readlog)
959195801Smav			xpt_freeze_simq(ch->sim, 1);
960195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
961195801Smav		return;
962195801Smav	}
963195801Smav	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
964195801Smav	/* Get a piece of the workspace for this request */
965195801Smav	ctp = (struct siis_cmd *)
966195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
967195801Smav	/* Fill S/G table */
968195801Smav	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
969195801Smav		prd = &ctp->u.ata.prd[0];
970195801Smav	else
971195801Smav		prd = &ctp->u.atapi.prd[0];
972195801Smav	for (i = 0; i < nsegs; i++) {
973195801Smav		prd[i].dba = htole64(segs[i].ds_addr);
974195801Smav		prd[i].dbc = htole32(segs[i].ds_len);
975195801Smav		prd[i].control = 0;
976195801Smav	}
977195801Smav	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
978195801Smav	slot->dma.nsegs = nsegs;
979195801Smav	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
980195801Smav	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
981195801Smav	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
982195801Smav	siis_execute_transaction(slot);
983195801Smav}
984195801Smav
985195801Smav/* Must be called with channel locked. */
986195801Smavstatic void
987195801Smavsiis_execute_transaction(struct siis_slot *slot)
988195801Smav{
989195801Smav	device_t dev = slot->dev;
990195801Smav	struct siis_channel *ch = device_get_softc(dev);
991195801Smav	struct siis_cmd *ctp;
992195801Smav	union ccb *ccb = slot->ccb;
993195801Smav	u_int64_t prb_bus;
994195801Smav
995195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
996195801Smav	/* Get a piece of the workspace for this request */
997195801Smav	ctp = (struct siis_cmd *)
998195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
999195801Smav	ctp->control = 0;
1000195801Smav	ctp->protocol_override = 0;
1001195801Smav	ctp->transfer_count = 0;
1002195801Smav	/* Special handling for Soft Reset command. */
1003201222Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1004201222Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) {
1005201222Smav			ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
1006201222Smav		} else {
1007201222Smav			ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE);
1008201222Smav			if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1009201222Smav				ctp->protocol_override |=
1010201222Smav				    htole16(SIIS_PRB_PROTO_NCQ);
1011201222Smav			}
1012201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1013201222Smav				ctp->protocol_override |=
1014201222Smav				    htole16(SIIS_PRB_PROTO_READ);
1015201222Smav			} else
1016201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1017201222Smav				ctp->protocol_override |=
1018201222Smav				    htole16(SIIS_PRB_PROTO_WRITE);
1019201222Smav			}
1020201222Smav		}
1021195801Smav	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1022201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1023195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
1024201222Smav		else
1025201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1026195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
1027195801Smav	}
1028195801Smav	/* Setup the FIS for this request */
1029199821Smav	if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) {
1030195801Smav		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1031195801Smav		if (!ch->readlog)
1032195801Smav			xpt_freeze_simq(ch->sim, 1);
1033195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1034195801Smav		return;
1035195801Smav	}
1036195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1037195801Smav	    BUS_DMASYNC_PREWRITE);
1038195801Smav	/* Issue command to the controller. */
1039195801Smav	slot->state = SIIS_SLOT_RUNNING;
1040195801Smav	ch->rslots |= (1 << slot->slot);
1041195801Smav	prb_bus = ch->dma.work_bus +
1042195801Smav	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
1043195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
1044195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
1045195801Smav	/* Start command execution timeout */
1046195801Smav	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
1047195801Smav	    (timeout_t*)siis_timeout, slot);
1048195801Smav	return;
1049195801Smav}
1050195801Smav
1051198852Smav/* Must be called with channel locked. */
1052195801Smavstatic void
1053198852Smavsiis_process_timeout(device_t dev)
1054195801Smav{
1055195801Smav	struct siis_channel *ch = device_get_softc(dev);
1056195801Smav	int i;
1057195801Smav
1058195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1059198852Smav	if (!ch->readlog && !ch->recovery) {
1060195801Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1061198852Smav		ch->recovery = 1;
1062195801Smav	}
1063197838Smav	/* Handle the rest of commands. */
1064195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1065195801Smav		/* Do we have a running request on slot? */
1066195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1067195801Smav			continue;
1068198852Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
1069195801Smav	}
1070195801Smav}
1071195801Smav
1072198852Smav/* Locked by callout mechanism. */
1073198852Smavstatic void
1074198852Smavsiis_timeout(struct siis_slot *slot)
1075198852Smav{
1076198852Smav	device_t dev = slot->dev;
1077198852Smav	struct siis_channel *ch = device_get_softc(dev);
1078198852Smav
1079198852Smav	mtx_assert(&ch->mtx, MA_OWNED);
1080198896Smav	/* Check for stale timeout. */
1081198896Smav	if (slot->state < SIIS_SLOT_RUNNING)
1082198896Smav		return;
1083198852Smav	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1084198852Smavdevice_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
1085198852Smav    __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
1086198852Smav    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
1087198852Smav    ATA_INL(ch->r_mem, SIIS_P_SERR));
1088198852Smav
1089198852Smav	if (ch->toslots == 0)
1090198852Smav		xpt_freeze_simq(ch->sim, 1);
1091198852Smav	ch->toslots |= (1 << slot->slot);
1092198852Smav	if ((ch->rslots & ~ch->toslots) == 0)
1093198852Smav		siis_process_timeout(dev);
1094198852Smav	else
1095198852Smav		device_printf(dev, " ... waiting for slots %08x\n",
1096198852Smav		    ch->rslots & ~ch->toslots);
1097198852Smav}
1098198852Smav
1099195801Smav/* Must be called with channel locked. */
1100195801Smavstatic void
1101195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1102195801Smav{
1103195801Smav	device_t dev = slot->dev;
1104195801Smav	struct siis_channel *ch = device_get_softc(dev);
1105195801Smav	union ccb *ccb = slot->ccb;
1106195801Smav
1107195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1108195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1109195801Smav	    BUS_DMASYNC_POSTWRITE);
1110195801Smav	/* Read result registers to the result struct
1111195801Smav	 * May be incorrect if several commands finished same time,
1112195801Smav	 * so read only when sure or have to.
1113195801Smav	 */
1114195801Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1115195801Smav		struct ata_res *res = &ccb->ataio.res;
1116195801Smav		if ((et == SIIS_ERR_TFE) ||
1117195801Smav		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1118195801Smav			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1119195801Smav
1120195801Smav			res->status = ATA_INB(ch->r_mem, offs + 2);
1121195801Smav			res->error = ATA_INB(ch->r_mem, offs + 3);
1122195801Smav			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1123195801Smav			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1124195801Smav			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1125195801Smav			res->device = ATA_INB(ch->r_mem, offs + 7);
1126195801Smav			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1127195801Smav			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1128195801Smav			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1129195801Smav			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1130195801Smav			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1131195801Smav		} else
1132195801Smav			bzero(res, sizeof(*res));
1133195801Smav	}
1134195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1135195801Smav		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1136195801Smav		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1137195801Smav		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1138195801Smav		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1139195801Smav	}
1140195801Smav	/* Set proper result status. */
1141195801Smav	if (et != SIIS_ERR_NONE || ch->recovery) {
1142195801Smav		ch->eslots |= (1 << slot->slot);
1143195801Smav		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1144195801Smav	}
1145198321Smav	/* In case of error, freeze device for proper recovery. */
1146198321Smav	if (et != SIIS_ERR_NONE &&
1147198321Smav	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1148198321Smav		xpt_freeze_devq(ccb->ccb_h.path, 1);
1149198321Smav		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1150198321Smav	}
1151198321Smav	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1152195801Smav	switch (et) {
1153195801Smav	case SIIS_ERR_NONE:
1154195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP;
1155195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1156195801Smav			ccb->csio.scsi_status = SCSI_STATUS_OK;
1157195801Smav		break;
1158195801Smav	case SIIS_ERR_INVALID:
1159198852Smav		ch->fatalerr = 1;
1160195801Smav		ccb->ccb_h.status |= CAM_REQ_INVALID;
1161195801Smav		break;
1162195801Smav	case SIIS_ERR_INNOCENT:
1163195801Smav		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1164195801Smav		break;
1165195801Smav	case SIIS_ERR_TFE:
1166198321Smav	case SIIS_ERR_NCQ:
1167195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1168195801Smav			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1169195801Smav			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1170195801Smav		} else {
1171195801Smav			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1172195801Smav		}
1173195801Smav		break;
1174195801Smav	case SIIS_ERR_SATA:
1175198852Smav		ch->fatalerr = 1;
1176195801Smav		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1177195801Smav		break;
1178195801Smav	case SIIS_ERR_TIMEOUT:
1179198852Smav		ch->fatalerr = 1;
1180195801Smav		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1181195801Smav		break;
1182195801Smav	default:
1183195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1184195801Smav	}
1185195801Smav	/* Free slot. */
1186199747Smav	ch->oslots &= ~(1 << slot->slot);
1187195801Smav	ch->rslots &= ~(1 << slot->slot);
1188195801Smav	ch->aslots &= ~(1 << slot->slot);
1189198852Smav	if (et != SIIS_ERR_TIMEOUT) {
1190198852Smav		if (ch->toslots == (1 << slot->slot))
1191198852Smav			xpt_release_simq(ch->sim, TRUE);
1192198852Smav		ch->toslots &= ~(1 << slot->slot);
1193198852Smav	}
1194195801Smav	slot->state = SIIS_SLOT_EMPTY;
1195195801Smav	slot->ccb = NULL;
1196195801Smav	/* Update channel stats. */
1197195801Smav	ch->numrslots--;
1198195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1199195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1200195801Smav		ch->numtslots[ccb->ccb_h.target_id]--;
1201195801Smav	}
1202198852Smav	/* If it was our READ LOG command - process it. */
1203198852Smav	if (ch->readlog) {
1204198852Smav		siis_process_read_log(dev, ccb);
1205195801Smav	/* If it was NCQ command error, put result on hold. */
1206198852Smav	} else if (et == SIIS_ERR_NCQ) {
1207195801Smav		ch->hold[slot->slot] = ccb;
1208195801Smav		ch->numhslots++;
1209198852Smav	} else
1210195801Smav		xpt_done(ccb);
1211195801Smav	/* Unfreeze frozen command. */
1212199747Smav	if (ch->frozen && !siis_check_collision(dev, ch->frozen)) {
1213195801Smav		union ccb *fccb = ch->frozen;
1214195801Smav		ch->frozen = NULL;
1215195801Smav		siis_begin_transaction(dev, fccb);
1216195801Smav		xpt_release_simq(ch->sim, TRUE);
1217195801Smav	}
1218195801Smav	/* If we have no other active commands, ... */
1219195801Smav	if (ch->rslots == 0) {
1220198852Smav		/* if there were timeouts or fatal error - reset port. */
1221198852Smav		if (ch->toslots != 0 || ch->fatalerr) {
1222198852Smav			siis_reset(dev);
1223198852Smav		} else {
1224198852Smav			/* if we have slots in error, we can reinit port. */
1225198852Smav			if (ch->eslots != 0)
1226198852Smav				siis_portinit(dev);
1227198852Smav			/* if there commands on hold, we can do READ LOG. */
1228198852Smav			if (!ch->readlog && ch->numhslots)
1229198852Smav				siis_issue_read_log(dev);
1230198852Smav		}
1231198852Smav	/* If all the reset of commands are in timeout - abort them. */
1232198852Smav	} else if ((ch->rslots & ~ch->toslots) == 0)
1233198852Smav		siis_process_timeout(dev);
1234195801Smav}
1235195801Smav
1236195801Smavstatic void
1237195801Smavsiis_issue_read_log(device_t dev)
1238195801Smav{
1239195801Smav	struct siis_channel *ch = device_get_softc(dev);
1240195801Smav	union ccb *ccb;
1241195801Smav	struct ccb_ataio *ataio;
1242195801Smav	int i;
1243195801Smav
1244195801Smav	/* Find some holden command. */
1245195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1246195801Smav		if (ch->hold[i])
1247195801Smav			break;
1248195801Smav	}
1249195801Smav	if (i == SIIS_MAX_SLOTS)
1250195801Smav		return;
1251195801Smav	ch->readlog = 1;
1252195801Smav	ccb = xpt_alloc_ccb_nowait();
1253195801Smav	if (ccb == NULL) {
1254195801Smav		device_printf(dev, "Unable allocate READ LOG command");
1255195801Smav		return; /* XXX */
1256195801Smav	}
1257195801Smav	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1258195801Smav	ccb->ccb_h.func_code = XPT_ATA_IO;
1259195801Smav	ccb->ccb_h.flags = CAM_DIR_IN;
1260195801Smav	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1261195801Smav	ataio = &ccb->ataio;
1262195801Smav	ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1263195801Smav	if (ataio->data_ptr == NULL) {
1264195801Smav		device_printf(dev, "Unable allocate memory for READ LOG command");
1265195801Smav		return; /* XXX */
1266195801Smav	}
1267195801Smav	ataio->dxfer_len = 512;
1268195801Smav	bzero(&ataio->cmd, sizeof(ataio->cmd));
1269195801Smav	ataio->cmd.flags = CAM_ATAIO_48BIT;
1270195801Smav	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1271195801Smav	ataio->cmd.sector_count = 1;
1272195801Smav	ataio->cmd.sector_count_exp = 0;
1273195801Smav	ataio->cmd.lba_low = 0x10;
1274195801Smav	ataio->cmd.lba_mid = 0;
1275195801Smav	ataio->cmd.lba_mid_exp = 0;
1276195801Smav	siis_begin_transaction(dev, ccb);
1277195801Smav}
1278195801Smav
1279195801Smavstatic void
1280195801Smavsiis_process_read_log(device_t dev, union ccb *ccb)
1281195801Smav{
1282195801Smav	struct siis_channel *ch = device_get_softc(dev);
1283195801Smav	uint8_t *data;
1284195801Smav	struct ata_res *res;
1285195801Smav	int i;
1286195801Smav
1287195801Smav	ch->readlog = 0;
1288195801Smav	data = ccb->ataio.data_ptr;
1289195801Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1290195801Smav	    (data[0] & 0x80) == 0) {
1291195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1292195801Smav			if (!ch->hold[i])
1293195801Smav				continue;
1294195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1295195801Smav				continue;
1296195801Smav			if ((data[0] & 0x1F) == i) {
1297195801Smav				res = &ch->hold[i]->ataio.res;
1298195801Smav				res->status = data[2];
1299195801Smav				res->error = data[3];
1300195801Smav				res->lba_low = data[4];
1301195801Smav				res->lba_mid = data[5];
1302195801Smav				res->lba_high = data[6];
1303195801Smav				res->device = data[7];
1304195801Smav				res->lba_low_exp = data[8];
1305195801Smav				res->lba_mid_exp = data[9];
1306195801Smav				res->lba_high_exp = data[10];
1307195801Smav				res->sector_count = data[12];
1308195801Smav				res->sector_count_exp = data[13];
1309195801Smav			} else {
1310195801Smav				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1311195801Smav				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1312195801Smav			}
1313195801Smav			xpt_done(ch->hold[i]);
1314195801Smav			ch->hold[i] = NULL;
1315195801Smav			ch->numhslots--;
1316195801Smav		}
1317195801Smav	} else {
1318195801Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1319195801Smav			device_printf(dev, "Error while READ LOG EXT\n");
1320195801Smav		else if ((data[0] & 0x80) == 0) {
1321195801Smav			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1322195801Smav		}
1323195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1324195801Smav			if (!ch->hold[i])
1325195801Smav				continue;
1326195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1327195801Smav				continue;
1328195801Smav			xpt_done(ch->hold[i]);
1329195801Smav			ch->hold[i] = NULL;
1330195801Smav			ch->numhslots--;
1331195801Smav		}
1332195801Smav	}
1333195801Smav	free(ccb->ataio.data_ptr, M_SIIS);
1334195801Smav	xpt_free_ccb(ccb);
1335195801Smav}
1336195801Smav
1337195801Smavstatic void
1338195801Smavsiis_portinit(device_t dev)
1339195801Smav{
1340195801Smav	struct siis_channel *ch = device_get_softc(dev);
1341195801Smav	int i;
1342195801Smav
1343195801Smav	ch->eslots = 0;
1344195801Smav	ch->recovery = 0;
1345195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1346195801Smav	for (i = 0; i < 16; i++) {
1347195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1348195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1349195801Smav	}
1350195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1351195801Smav	siis_wait_ready(dev, 1000);
1352195801Smav}
1353195801Smav
1354198426Smavstatic int
1355195801Smavsiis_devreset(device_t dev)
1356195801Smav{
1357195801Smav	struct siis_channel *ch = device_get_softc(dev);
1358198426Smav	int timeout = 0;
1359198426Smav	uint32_t val;
1360195801Smav
1361195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1362198426Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1363198426Smav	    SIIS_P_CTL_DEV_RESET) != 0) {
1364198426Smav		DELAY(1000);
1365198426Smav		if (timeout++ > 100) {
1366198426Smav			device_printf(dev, "device reset stuck (timeout %dms) "
1367198426Smav			    "status = %08x\n", timeout, val);
1368198426Smav			return (EBUSY);
1369198426Smav		}
1370198426Smav	}
1371198426Smav	if (bootverbose)
1372198426Smav		device_printf(dev, "device reset time=%dms\n", timeout);
1373198426Smav	return (0);
1374195801Smav}
1375195801Smav
1376195801Smavstatic int
1377195801Smavsiis_wait_ready(device_t dev, int t)
1378195801Smav{
1379195801Smav	struct siis_channel *ch = device_get_softc(dev);
1380195801Smav	int timeout = 0;
1381195801Smav	uint32_t val;
1382195801Smav
1383195801Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1384195801Smav	    SIIS_P_CTL_READY) == 0) {
1385195801Smav		DELAY(1000);
1386195801Smav		if (timeout++ > t) {
1387195801Smav			device_printf(dev, "port is not ready (timeout %dms) "
1388195801Smav			    "status = %08x\n", t, val);
1389195801Smav			return (EBUSY);
1390195801Smav		}
1391195801Smav	}
1392195801Smav	if (bootverbose)
1393195801Smav		device_printf(dev, "ready wait time=%dms\n", timeout);
1394195801Smav	return (0);
1395195801Smav}
1396195801Smav
1397195801Smavstatic void
1398195801Smavsiis_reset(device_t dev)
1399195801Smav{
1400195801Smav	struct siis_channel *ch = device_get_softc(dev);
1401199821Smav	int i, retry = 0, sata_rev;
1402198426Smav	uint32_t val;
1403195801Smav
1404195801Smav	if (bootverbose)
1405195801Smav		device_printf(dev, "SIIS reset...\n");
1406198852Smav	if (!ch->readlog && !ch->recovery)
1407198852Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1408198852Smav	/* Requeue frozen command. */
1409195801Smav	if (ch->frozen) {
1410195801Smav		union ccb *fccb = ch->frozen;
1411195801Smav		ch->frozen = NULL;
1412198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1413198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1414198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1415198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
1416198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1417198321Smav		}
1418195801Smav		xpt_done(fccb);
1419195801Smav	}
1420198426Smav	/* Requeue all running commands. */
1421195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1422195801Smav		/* Do we have a running request on slot? */
1423195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1424195801Smav			continue;
1425195801Smav		/* XXX; Commands in loading state. */
1426195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1427195801Smav	}
1428198852Smav	/* Finish all holden commands as-is. */
1429198852Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1430198852Smav		if (!ch->hold[i])
1431198852Smav			continue;
1432198852Smav		xpt_done(ch->hold[i]);
1433198852Smav		ch->hold[i] = NULL;
1434198852Smav		ch->numhslots--;
1435198852Smav	}
1436198852Smav	if (ch->toslots != 0)
1437198852Smav		xpt_release_simq(ch->sim, TRUE);
1438198852Smav	ch->eslots = 0;
1439198852Smav	ch->recovery = 0;
1440198852Smav	ch->toslots = 0;
1441198852Smav	ch->fatalerr = 0;
1442198426Smav	/* Disable port interrupts */
1443198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1444198426Smav	/* Set speed limit. */
1445199821Smav	sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
1446199821Smav	if (sata_rev == 1)
1447198426Smav		val = ATA_SC_SPD_SPEED_GEN1;
1448199821Smav	else if (sata_rev == 2)
1449198426Smav		val = ATA_SC_SPD_SPEED_GEN2;
1450199821Smav	else if (sata_rev == 3)
1451198426Smav		val = ATA_SC_SPD_SPEED_GEN3;
1452198426Smav	else
1453198426Smav		val = 0;
1454198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1455198426Smav	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1456198426Smav	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1457198852Smavretry:
1458198426Smav	siis_devreset(dev);
1459195801Smav	/* Reset and reconnect PHY, */
1460198426Smav	if (!siis_sata_connect(ch)) {
1461195801Smav		ch->devices = 0;
1462195801Smav		/* Enable port interrupts */
1463195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1464195801Smav		if (bootverbose)
1465195801Smav			device_printf(dev,
1466195801Smav			    "SIIS reset done: phy reset found no device\n");
1467195801Smav		/* Tell the XPT about the event */
1468195801Smav		xpt_async(AC_BUS_RESET, ch->path, NULL);
1469195801Smav		return;
1470195801Smav	}
1471195801Smav	/* Wait for clearing busy status. */
1472198852Smav	if (siis_wait_ready(dev, 10000)) {
1473195801Smav		device_printf(dev, "device ready timeout\n");
1474198852Smav		if (!retry) {
1475198852Smav			device_printf(dev, "trying full port reset ...\n");
1476198852Smav			/* Get port to the reset state. */
1477198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
1478198852Smav			DELAY(10000);
1479198852Smav			/* Get port out of reset state. */
1480198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
1481198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
1482198852Smav			if (ch->pm_present)
1483198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1484198852Smav			else
1485198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1486198852Smav			siis_wait_ready(dev, 5000);
1487198852Smav			retry = 1;
1488198852Smav			goto retry;
1489198852Smav		}
1490198852Smav	}
1491195801Smav	ch->devices = 1;
1492195801Smav	/* Enable port interrupts */
1493195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1494195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1495195801Smav	if (bootverbose)
1496195801Smav		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1497195801Smav	/* Tell the XPT about the event */
1498195801Smav	xpt_async(AC_BUS_RESET, ch->path, NULL);
1499195801Smav}
1500195801Smav
1501195801Smavstatic int
1502199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag)
1503195801Smav{
1504199821Smav	struct siis_channel *ch = device_get_softc(dev);
1505195801Smav	u_int8_t *fis = &ctp->fis[0];
1506195801Smav
1507195801Smav	bzero(fis, 24);
1508195801Smav	fis[0] = 0x27;  		/* host to device */
1509195801Smav	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1510195801Smav	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1511195801Smav		fis[1] |= 0x80;
1512195801Smav		fis[2] = ATA_PACKET_CMD;
1513199821Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1514199821Smav		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1515195801Smav			fis[3] = ATA_F_DMA;
1516195801Smav		else {
1517195801Smav			fis[5] = ccb->csio.dxfer_len;
1518195801Smav		        fis[6] = ccb->csio.dxfer_len >> 8;
1519195801Smav		}
1520195801Smav		fis[7] = ATA_D_LBA;
1521195801Smav		fis[15] = ATA_A_4BIT;
1522195801Smav		bzero(ctp->u.atapi.ccb, 16);
1523195801Smav		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1524195801Smav		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1525195801Smav		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1526195801Smav	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1527195801Smav		fis[1] |= 0x80;
1528195801Smav		fis[2] = ccb->ataio.cmd.command;
1529195801Smav		fis[3] = ccb->ataio.cmd.features;
1530195801Smav		fis[4] = ccb->ataio.cmd.lba_low;
1531195801Smav		fis[5] = ccb->ataio.cmd.lba_mid;
1532195801Smav		fis[6] = ccb->ataio.cmd.lba_high;
1533195801Smav		fis[7] = ccb->ataio.cmd.device;
1534195801Smav		fis[8] = ccb->ataio.cmd.lba_low_exp;
1535195801Smav		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1536195801Smav		fis[10] = ccb->ataio.cmd.lba_high_exp;
1537195801Smav		fis[11] = ccb->ataio.cmd.features_exp;
1538195801Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1539195801Smav			fis[12] = tag << 3;
1540195801Smav			fis[13] = 0;
1541195801Smav		} else {
1542195801Smav			fis[12] = ccb->ataio.cmd.sector_count;
1543195801Smav			fis[13] = ccb->ataio.cmd.sector_count_exp;
1544195801Smav		}
1545195801Smav		fis[15] = ATA_A_4BIT;
1546195801Smav	} else {
1547195801Smav		/* Soft reset. */
1548195801Smav	}
1549195801Smav	return (20);
1550195801Smav}
1551195801Smav
1552195801Smavstatic int
1553195801Smavsiis_sata_connect(struct siis_channel *ch)
1554195801Smav{
1555195801Smav	u_int32_t status;
1556195801Smav	int timeout;
1557195801Smav
1558195801Smav	/* Wait up to 100ms for "connect well" */
1559195801Smav	for (timeout = 0; timeout < 100 ; timeout++) {
1560195801Smav		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1561195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1562195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1563195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1564195801Smav			break;
1565195801Smav		DELAY(1000);
1566195801Smav	}
1567195801Smav	if (timeout >= 100) {
1568195801Smav		if (bootverbose) {
1569195801Smav			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1570195801Smav			    status);
1571195801Smav		}
1572195801Smav		return (0);
1573195801Smav	}
1574195801Smav	if (bootverbose) {
1575195801Smav		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1576195801Smav		    timeout, status);
1577195801Smav	}
1578195801Smav	/* Clear SATA error register */
1579195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1580195801Smav	return (1);
1581195801Smav}
1582195801Smav
1583195801Smavstatic void
1584195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb)
1585195801Smav{
1586195801Smav	device_t dev;
1587195801Smav	struct siis_channel *ch;
1588195801Smav
1589195801Smav	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1590195801Smav	    ccb->ccb_h.func_code));
1591195801Smav
1592195801Smav	ch = (struct siis_channel *)cam_sim_softc(sim);
1593195801Smav	dev = ch->dev;
1594195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1595195801Smav	switch (ccb->ccb_h.func_code) {
1596195801Smav	/* Common cases first */
1597195801Smav	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1598195801Smav	case XPT_SCSI_IO:
1599195801Smav		if (ch->devices == 0) {
1600195801Smav			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1601195801Smav			xpt_done(ccb);
1602195801Smav			break;
1603195801Smav		}
1604195801Smav		/* Check for command collision. */
1605195801Smav		if (siis_check_collision(dev, ccb)) {
1606195801Smav			/* Freeze command. */
1607195801Smav			ch->frozen = ccb;
1608195801Smav			/* We have only one frozen slot, so freeze simq also. */
1609195801Smav			xpt_freeze_simq(ch->sim, 1);
1610195801Smav			return;
1611195801Smav		}
1612195801Smav		siis_begin_transaction(dev, ccb);
1613195801Smav		break;
1614195801Smav	case XPT_EN_LUN:		/* Enable LUN as a target */
1615195801Smav	case XPT_TARGET_IO:		/* Execute target I/O request */
1616195801Smav	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1617195801Smav	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1618195801Smav	case XPT_ABORT:			/* Abort the specified CCB */
1619195801Smav		/* XXX Implement */
1620195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1621195801Smav		xpt_done(ccb);
1622195801Smav		break;
1623195801Smav	case XPT_SET_TRAN_SETTINGS:
1624195801Smav	{
1625195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1626199747Smav		struct	siis_device *d;
1627195801Smav
1628199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1629199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1630199747Smav		else
1631199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1632199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1633199747Smav			d->revision = cts->xport_specific.sata.revision;
1634199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1635199747Smav			d->mode = cts->xport_specific.sata.mode;
1636199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1637199747Smav			d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1638199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1639199747Smav			d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags);
1640195801Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1641198852Smav			ch->pm_present = cts->xport_specific.sata.pm_present;
1642198852Smav			if (ch->pm_present)
1643195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1644195801Smav			else
1645195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1646195801Smav		}
1647195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1648195801Smav		xpt_done(ccb);
1649195801Smav		break;
1650195801Smav	}
1651195801Smav	case XPT_GET_TRAN_SETTINGS:
1652195801Smav	/* Get default/user set transfer settings for the target */
1653195801Smav	{
1654195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1655199747Smav		struct  siis_device *d;
1656195801Smav		uint32_t status;
1657195801Smav
1658199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1659199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1660199747Smav		else
1661199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1662195801Smav		cts->protocol = PROTO_ATA;
1663196655Smav		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1664195801Smav		cts->transport = XPORT_SATA;
1665196655Smav		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1666195801Smav		cts->proto_specific.valid = 0;
1667195801Smav		cts->xport_specific.sata.valid = 0;
1668199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1669199747Smav		    (ccb->ccb_h.target_id == 15 ||
1670199747Smav		    (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
1671195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1672199747Smav			if (status & 0x0f0) {
1673199747Smav				cts->xport_specific.sata.revision =
1674199747Smav				    (status & 0x0f0) >> 4;
1675199747Smav				cts->xport_specific.sata.valid |=
1676199747Smav				    CTS_SATA_VALID_REVISION;
1677199747Smav			}
1678199747Smav		} else {
1679199747Smav			cts->xport_specific.sata.revision = d->revision;
1680199747Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1681195801Smav		}
1682199747Smav		cts->xport_specific.sata.mode = d->mode;
1683199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1684199747Smav		cts->xport_specific.sata.bytecount = d->bytecount;
1685199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1686198852Smav		cts->xport_specific.sata.pm_present = ch->pm_present;
1687195801Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1688199747Smav		cts->xport_specific.sata.tags = d->tags;
1689199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
1690195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1691195801Smav		xpt_done(ccb);
1692195801Smav		break;
1693195801Smav	}
1694195801Smav#if 0
1695195801Smav	case XPT_CALC_GEOMETRY:
1696195801Smav	{
1697195801Smav		struct	  ccb_calc_geometry *ccg;
1698195801Smav		uint32_t size_mb;
1699195801Smav		uint32_t secs_per_cylinder;
1700195801Smav
1701195801Smav		ccg = &ccb->ccg;
1702195801Smav		size_mb = ccg->volume_size
1703195801Smav			/ ((1024L * 1024L) / ccg->block_size);
1704195801Smav		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1705195801Smav			if (size_mb >= 2048) {
1706195801Smav				ccg->heads = 255;
1707195801Smav				ccg->secs_per_track = 63;
1708195801Smav			} else {
1709195801Smav				ccg->heads = 128;
1710195801Smav				ccg->secs_per_track = 32;
1711195801Smav			}
1712195801Smav		} else {
1713195801Smav			ccg->heads = 64;
1714195801Smav			ccg->secs_per_track = 32;
1715195801Smav		}
1716195801Smav		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1717195801Smav		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1718195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1719195801Smav		xpt_done(ccb);
1720195801Smav		break;
1721195801Smav	}
1722195801Smav#endif
1723195801Smav	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1724195801Smav	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1725195801Smav		siis_reset(dev);
1726195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1727195801Smav		xpt_done(ccb);
1728195801Smav		break;
1729195801Smav	case XPT_TERM_IO:		/* Terminate the I/O process */
1730195801Smav		/* XXX Implement */
1731195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1732195801Smav		xpt_done(ccb);
1733195801Smav		break;
1734195801Smav	case XPT_PATH_INQ:		/* Path routing inquiry */
1735195801Smav	{
1736195801Smav		struct ccb_pathinq *cpi = &ccb->cpi;
1737195801Smav
1738195801Smav		cpi->version_num = 1; /* XXX??? */
1739195801Smav		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1740195801Smav		cpi->hba_inquiry |= PI_SATAPM;
1741195801Smav		cpi->target_sprt = 0;
1742195801Smav		cpi->hba_misc = PIM_SEQSCAN;
1743195801Smav		cpi->hba_eng_cnt = 0;
1744198322Smav		cpi->max_target = 15;
1745195801Smav		cpi->max_lun = 0;
1746195801Smav		cpi->initiator_id = 0;
1747195801Smav		cpi->bus_id = cam_sim_bus(sim);
1748195801Smav		cpi->base_transfer_speed = 150000;
1749195801Smav		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1750195801Smav		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1751195801Smav		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1752195801Smav		cpi->unit_number = cam_sim_unit(sim);
1753195801Smav		cpi->transport = XPORT_SATA;
1754196655Smav		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1755195801Smav		cpi->protocol = PROTO_ATA;
1756196655Smav		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1757195801Smav		cpi->ccb_h.status = CAM_REQ_CMP;
1758195801Smav		cpi->maxio = MAXPHYS;
1759195801Smav		xpt_done(ccb);
1760195801Smav		break;
1761195801Smav	}
1762195801Smav	default:
1763195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1764195801Smav		xpt_done(ccb);
1765195801Smav		break;
1766195801Smav	}
1767195801Smav}
1768195801Smav
1769195801Smavstatic void
1770195801Smavsiispoll(struct cam_sim *sim)
1771195801Smav{
1772195801Smav	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1773195801Smav
1774195801Smav	siis_ch_intr(ch->dev);
1775195801Smav}
1776