siis.c revision 220602
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 220602 2011-04-13 16:20:54Z 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>
47217877Smav#include <dev/led/led.h>
48195801Smav#include <dev/pci/pcivar.h>
49195801Smav#include <dev/pci/pcireg.h>
50195801Smav#include "siis.h"
51195801Smav
52195801Smav#include <cam/cam.h>
53195801Smav#include <cam/cam_ccb.h>
54195801Smav#include <cam/cam_sim.h>
55195801Smav#include <cam/cam_xpt_sim.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);
63208393Smavstatic int siis_ch_init(device_t dev);
64208393Smavstatic int siis_ch_deinit(device_t dev);
65195801Smavstatic int siis_ch_suspend(device_t dev);
66195801Smavstatic int siis_ch_resume(device_t dev);
67195801Smavstatic void siis_ch_intr_locked(void *data);
68195801Smavstatic void siis_ch_intr(void *data);
69217877Smavstatic void siis_ch_led(void *priv, int onoff);
70195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb);
71195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
72195801Smavstatic void siis_execute_transaction(struct siis_slot *slot);
73195801Smavstatic void siis_timeout(struct siis_slot *slot);
74195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
75199821Smavstatic int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag);
76195801Smavstatic void siis_dmainit(device_t dev);
77195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
78195801Smavstatic void siis_dmafini(device_t dev);
79195801Smavstatic void siis_slotsalloc(device_t dev);
80195801Smavstatic void siis_slotsfree(device_t dev);
81195801Smavstatic void siis_reset(device_t dev);
82195801Smavstatic void siis_portinit(device_t dev);
83195801Smavstatic int siis_wait_ready(device_t dev, int t);
84195801Smav
85195801Smavstatic int siis_sata_connect(struct siis_channel *ch);
86195801Smav
87220566Smavstatic void siis_issue_recovery(device_t dev);
88195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb);
89220566Smavstatic void siis_process_request_sense(device_t dev, union ccb *ccb);
90195801Smav
91195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb);
92195801Smavstatic void siispoll(struct cam_sim *sim);
93195801Smav
94195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
95195801Smav
96199132Smavstatic struct {
97199132Smav	uint32_t	id;
98199132Smav	const char	*name;
99199132Smav	int		ports;
100200217Smav	int		quirks;
101200217Smav#define SIIS_Q_SNTF	1
102205358Smav#define SIIS_Q_NOMSI	2
103199132Smav} siis_ids[] = {
104200217Smav	{0x31241095,	"SiI3124",	4,	0},
105200217Smav	{0x31248086,	"SiI3124",	4,	0},
106205358Smav	{0x31321095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
107205358Smav	{0x02421095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
108205358Smav	{0x02441095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
109205358Smav	{0x31311095,	"SiI3131",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
110205358Smav	{0x35311095,	"SiI3531",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
111200217Smav	{0,		NULL,		0,	0}
112199132Smav};
113199132Smav
114220566Smav#define recovery_type		spriv_field0
115220566Smav#define RECOVERY_NONE		0
116220566Smav#define RECOVERY_READ_LOG	1
117220566Smav#define RECOVERY_REQUEST_SENSE	2
118220566Smav#define recovery_slot		spriv_field1
119220566Smav
120195801Smavstatic int
121195801Smavsiis_probe(device_t dev)
122195801Smav{
123199132Smav	char buf[64];
124199132Smav	int i;
125195801Smav	uint32_t devid = pci_get_devid(dev);
126195801Smav
127199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
128199132Smav		if (siis_ids[i].id == devid) {
129200217Smav			snprintf(buf, sizeof(buf), "%s SATA controller",
130199132Smav			    siis_ids[i].name);
131199132Smav			device_set_desc_copy(dev, buf);
132199132Smav			return (BUS_PROBE_VENDOR);
133199132Smav		}
134195801Smav	}
135199132Smav	return (ENXIO);
136195801Smav}
137195801Smav
138195801Smavstatic int
139195801Smavsiis_attach(device_t dev)
140195801Smav{
141195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
142195801Smav	uint32_t devid = pci_get_devid(dev);
143195801Smav	device_t child;
144199132Smav	int	error, i, unit;
145195801Smav
146200217Smav	ctlr->dev = dev;
147199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
148199132Smav		if (siis_ids[i].id == devid)
149199132Smav			break;
150199132Smav	}
151200217Smav	ctlr->quirks = siis_ids[i].quirks;
152195801Smav	/* Global memory */
153195801Smav	ctlr->r_grid = PCIR_BAR(0);
154195801Smav	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
155195801Smav	    &ctlr->r_grid, RF_ACTIVE)))
156195801Smav		return (ENXIO);
157200223Smav	ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL);
158195801Smav	/* Channels memory */
159195801Smav	ctlr->r_rid = PCIR_BAR(2);
160195801Smav	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
161195801Smav	    &ctlr->r_rid, RF_ACTIVE)))
162195801Smav		return (ENXIO);
163195801Smav	/* Setup our own memory management for channels. */
164208414Smav	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
165208414Smav	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
166195801Smav	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
167195801Smav	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
168195801Smav	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
169195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
170195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
171195801Smav		return (error);
172195801Smav	}
173195801Smav	if ((error = rman_manage_region(&ctlr->sc_iomem,
174195801Smav	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
175195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
176195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
177195801Smav		rman_fini(&ctlr->sc_iomem);
178195801Smav		return (error);
179195801Smav	}
180206652Smav	pci_enable_busmaster(dev);
181195801Smav	/* Reset controller */
182195801Smav	siis_resume(dev);
183195801Smav	/* Number of HW channels */
184199132Smav	ctlr->channels = siis_ids[i].ports;
185195801Smav	/* Setup interrupts. */
186195801Smav	if (siis_setup_interrupt(dev)) {
187195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
188195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
189195801Smav		rman_fini(&ctlr->sc_iomem);
190195801Smav		return ENXIO;
191195801Smav	}
192195801Smav	/* Attach all channels on this controller */
193195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
194195801Smav		child = device_add_child(dev, "siisch", -1);
195195801Smav		if (child == NULL)
196195801Smav			device_printf(dev, "failed to add channel device\n");
197195801Smav		else
198195801Smav			device_set_ivars(child, (void *)(intptr_t)unit);
199195801Smav	}
200195801Smav	bus_generic_attach(dev);
201195801Smav	return 0;
202195801Smav}
203195801Smav
204195801Smavstatic int
205195801Smavsiis_detach(device_t dev)
206195801Smav{
207195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
208195801Smav	device_t *children;
209195801Smav	int nchildren, i;
210195801Smav
211195801Smav	/* Detach & delete all children */
212195801Smav	if (!device_get_children(dev, &children, &nchildren)) {
213195801Smav		for (i = 0; i < nchildren; i++)
214195801Smav			device_delete_child(dev, children[i]);
215195801Smav		free(children, M_TEMP);
216195801Smav	}
217195801Smav	/* Free interrupts. */
218195801Smav	if (ctlr->irq.r_irq) {
219195801Smav		bus_teardown_intr(dev, ctlr->irq.r_irq,
220195801Smav		    ctlr->irq.handle);
221195801Smav		bus_release_resource(dev, SYS_RES_IRQ,
222195801Smav		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
223195801Smav	}
224195801Smav	pci_release_msi(dev);
225195801Smav	/* Free memory. */
226195801Smav	rman_fini(&ctlr->sc_iomem);
227195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
228195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
229195801Smav	return (0);
230195801Smav}
231195801Smav
232195801Smavstatic int
233195801Smavsiis_suspend(device_t dev)
234195801Smav{
235195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
236195801Smav
237195801Smav	bus_generic_suspend(dev);
238195801Smav	/* Put controller into reset state. */
239200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
240200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
241195801Smav	return 0;
242195801Smav}
243195801Smav
244195801Smavstatic int
245195801Smavsiis_resume(device_t dev)
246195801Smav{
247195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
248195801Smav
249200291Smav	/* Set PCIe max read request size to at least 1024 bytes */
250203529Smav	if (pci_get_max_read_req(dev) < 1024)
251203529Smav		pci_set_max_read_req(dev, 1024);
252195801Smav	/* Put controller into reset state. */
253200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
254200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
255195801Smav	DELAY(10000);
256195801Smav	/* Get controller out of reset state and enable port interrupts. */
257200223Smav	ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE);
258200223Smav	ctlr->gctl |= 0x0000000f;
259200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
260195801Smav	return (bus_generic_resume(dev));
261195801Smav}
262195801Smav
263195801Smavstatic int
264195801Smavsiis_setup_interrupt(device_t dev)
265195801Smav{
266195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
267205358Smav	int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1;
268195801Smav
269195801Smav	/* Process hints. */
270195801Smav	resource_int_value(device_get_name(dev),
271195801Smav	    device_get_unit(dev), "msi", &msi);
272195801Smav	if (msi < 0)
273195801Smav		msi = 0;
274195801Smav	else if (msi > 0)
275195801Smav		msi = min(1, pci_msi_count(dev));
276195801Smav	/* Allocate MSI if needed/present. */
277195801Smav	if (msi && pci_alloc_msi(dev, &msi) != 0)
278195801Smav		msi = 0;
279195801Smav	/* Allocate all IRQs. */
280195801Smav	ctlr->irq.r_irq_rid = msi ? 1 : 0;
281195801Smav	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
282195801Smav	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
283195801Smav		device_printf(dev, "unable to map interrupt\n");
284195801Smav		return ENXIO;
285195801Smav	}
286195801Smav	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
287195801Smav	    siis_intr, ctlr, &ctlr->irq.handle))) {
288195801Smav		/* SOS XXX release r_irq */
289195801Smav		device_printf(dev, "unable to setup interrupt\n");
290195801Smav		return ENXIO;
291195801Smav	}
292195801Smav	return (0);
293195801Smav}
294195801Smav
295195801Smav/*
296195801Smav * Common case interrupt handler.
297195801Smav */
298195801Smavstatic void
299195801Smavsiis_intr(void *data)
300195801Smav{
301195801Smav	struct siis_controller *ctlr = (struct siis_controller *)data;
302195801Smav	u_int32_t is;
303195801Smav	void *arg;
304195801Smav	int unit;
305195801Smav
306195801Smav	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
307195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
308195801Smav		if ((is & SIIS_IS_PORT(unit)) != 0 &&
309195801Smav		    (arg = ctlr->interrupt[unit].argument)) {
310195801Smav			ctlr->interrupt[unit].function(arg);
311195801Smav		}
312195801Smav	}
313200223Smav	/* Acknowledge interrupt, if MSI enabled. */
314200223Smav	if (ctlr->irq.r_irq_rid) {
315200223Smav		ATA_OUTL(ctlr->r_gmem, SIIS_GCTL,
316200223Smav		    ctlr->gctl | SIIS_GCTL_MSIACK);
317200223Smav	}
318195801Smav}
319195801Smav
320195801Smavstatic struct resource *
321195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid,
322195801Smav		       u_long start, u_long end, u_long count, u_int flags)
323195801Smav{
324195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
325195801Smav	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
326195801Smav	struct resource *res = NULL;
327195801Smav	int offset = unit << 13;
328195801Smav	long st;
329195801Smav
330195801Smav	switch (type) {
331195801Smav	case SYS_RES_MEMORY:
332195801Smav		st = rman_get_start(ctlr->r_mem);
333195801Smav		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
334195801Smav		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
335195801Smav		if (res) {
336195801Smav			bus_space_handle_t bsh;
337195801Smav			bus_space_tag_t bst;
338195801Smav			bsh = rman_get_bushandle(ctlr->r_mem);
339195801Smav			bst = rman_get_bustag(ctlr->r_mem);
340195801Smav			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
341195801Smav			rman_set_bushandle(res, bsh);
342195801Smav			rman_set_bustag(res, bst);
343195801Smav		}
344195801Smav		break;
345195801Smav	case SYS_RES_IRQ:
346195801Smav		if (*rid == ATA_IRQ_RID)
347195801Smav			res = ctlr->irq.r_irq;
348195801Smav		break;
349195801Smav	}
350195801Smav	return (res);
351195801Smav}
352195801Smav
353195801Smavstatic int
354195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid,
355195801Smav			 struct resource *r)
356195801Smav{
357195801Smav
358195801Smav	switch (type) {
359195801Smav	case SYS_RES_MEMORY:
360195801Smav		rman_release_resource(r);
361195801Smav		return (0);
362195801Smav	case SYS_RES_IRQ:
363195801Smav		if (rid != ATA_IRQ_RID)
364195801Smav			return ENOENT;
365195801Smav		return (0);
366195801Smav	}
367195801Smav	return (EINVAL);
368195801Smav}
369195801Smav
370195801Smavstatic int
371195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq,
372195801Smav		   int flags, driver_filter_t *filter, driver_intr_t *function,
373195801Smav		   void *argument, void **cookiep)
374195801Smav{
375195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
376195801Smav	int unit = (intptr_t)device_get_ivars(child);
377195801Smav
378195801Smav	if (filter != NULL) {
379195801Smav		printf("siis.c: we cannot use a filter here\n");
380195801Smav		return (EINVAL);
381195801Smav	}
382195801Smav	ctlr->interrupt[unit].function = function;
383195801Smav	ctlr->interrupt[unit].argument = argument;
384195801Smav	return (0);
385195801Smav}
386195801Smav
387195801Smavstatic int
388195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq,
389195801Smav		      void *cookie)
390195801Smav{
391195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
392195801Smav	int unit = (intptr_t)device_get_ivars(child);
393195801Smav
394195801Smav	ctlr->interrupt[unit].function = NULL;
395195801Smav	ctlr->interrupt[unit].argument = NULL;
396195801Smav	return (0);
397195801Smav}
398195801Smav
399195801Smavstatic int
400195801Smavsiis_print_child(device_t dev, device_t child)
401195801Smav{
402195801Smav	int retval;
403195801Smav
404195801Smav	retval = bus_print_child_header(dev, child);
405195801Smav	retval += printf(" at channel %d",
406195801Smav	    (int)(intptr_t)device_get_ivars(child));
407195801Smav	retval += bus_print_child_footer(dev, child);
408195801Smav
409195801Smav	return (retval);
410195801Smav}
411195801Smav
412208410Smavstatic int
413208410Smavsiis_child_location_str(device_t dev, device_t child, char *buf,
414208410Smav    size_t buflen)
415208410Smav{
416208410Smav
417208410Smav	snprintf(buf, buflen, "channel=%d",
418208410Smav	    (int)(intptr_t)device_get_ivars(child));
419208410Smav	return (0);
420208410Smav}
421208410Smav
422195801Smavdevclass_t siis_devclass;
423195801Smavstatic device_method_t siis_methods[] = {
424195801Smav	DEVMETHOD(device_probe,     siis_probe),
425195801Smav	DEVMETHOD(device_attach,    siis_attach),
426195801Smav	DEVMETHOD(device_detach,    siis_detach),
427195801Smav	DEVMETHOD(device_suspend,   siis_suspend),
428195801Smav	DEVMETHOD(device_resume,    siis_resume),
429195801Smav	DEVMETHOD(bus_print_child,  siis_print_child),
430195801Smav	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
431195801Smav	DEVMETHOD(bus_release_resource,     siis_release_resource),
432195801Smav	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
433195801Smav	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
434208410Smav	DEVMETHOD(bus_child_location_str, siis_child_location_str),
435195801Smav	{ 0, 0 }
436195801Smav};
437195801Smavstatic driver_t siis_driver = {
438195801Smav        "siis",
439195801Smav        siis_methods,
440195801Smav        sizeof(struct siis_controller)
441195801Smav};
442195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
443195801SmavMODULE_VERSION(siis, 1);
444195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1);
445195801Smav
446195801Smavstatic int
447195801Smavsiis_ch_probe(device_t dev)
448195801Smav{
449195801Smav
450195801Smav	device_set_desc_copy(dev, "SIIS channel");
451195801Smav	return (0);
452195801Smav}
453195801Smav
454195801Smavstatic int
455195801Smavsiis_ch_attach(device_t dev)
456195801Smav{
457200217Smav	struct siis_controller *ctlr = device_get_softc(device_get_parent(dev));
458195801Smav	struct siis_channel *ch = device_get_softc(dev);
459195801Smav	struct cam_devq *devq;
460199821Smav	int rid, error, i, sata_rev = 0;
461195801Smav
462195801Smav	ch->dev = dev;
463195801Smav	ch->unit = (intptr_t)device_get_ivars(dev);
464200217Smav	ch->quirks = ctlr->quirks;
465195801Smav	resource_int_value(device_get_name(dev),
466195801Smav	    device_get_unit(dev), "pm_level", &ch->pm_level);
467199821Smav	resource_int_value(device_get_name(dev),
468199821Smav	    device_get_unit(dev), "sata_rev", &sata_rev);
469199747Smav	for (i = 0; i < 16; i++) {
470199821Smav		ch->user[i].revision = sata_rev;
471199747Smav		ch->user[i].mode = 0;
472199747Smav		ch->user[i].bytecount = 8192;
473199747Smav		ch->user[i].tags = SIIS_MAX_SLOTS;
474199747Smav		ch->curr[i] = ch->user[i];
475207499Smav		if (ch->pm_level)
476207499Smav			ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ;
477220602Smav		ch->user[i].caps |= CTS_SATA_CAPS_H_AN;
478199747Smav	}
479195801Smav	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
480195801Smav	rid = ch->unit;
481195801Smav	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
482195801Smav	    &rid, RF_ACTIVE)))
483195801Smav		return (ENXIO);
484195801Smav	siis_dmainit(dev);
485195801Smav	siis_slotsalloc(dev);
486208393Smav	siis_ch_init(dev);
487195801Smav	mtx_lock(&ch->mtx);
488195801Smav	rid = ATA_IRQ_RID;
489195801Smav	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
490195801Smav	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
491195801Smav		device_printf(dev, "Unable to map interrupt\n");
492208816Smav		error = ENXIO;
493208816Smav		goto err0;
494195801Smav	}
495195801Smav	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
496195801Smav	    siis_ch_intr_locked, dev, &ch->ih))) {
497195801Smav		device_printf(dev, "Unable to setup interrupt\n");
498195801Smav		error = ENXIO;
499195801Smav		goto err1;
500195801Smav	}
501195801Smav	/* Create the device queue for our SIM. */
502195801Smav	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
503195801Smav	if (devq == NULL) {
504195801Smav		device_printf(dev, "Unable to allocate simq\n");
505195801Smav		error = ENOMEM;
506195801Smav		goto err1;
507195801Smav	}
508195801Smav	/* Construct SIM entry */
509195801Smav	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
510199178Smav	    device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq);
511195801Smav	if (ch->sim == NULL) {
512208816Smav		cam_simq_free(devq);
513195801Smav		device_printf(dev, "unable to allocate sim\n");
514195801Smav		error = ENOMEM;
515208816Smav		goto err1;
516195801Smav	}
517195801Smav	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
518195801Smav		device_printf(dev, "unable to register xpt bus\n");
519195801Smav		error = ENXIO;
520195801Smav		goto err2;
521195801Smav	}
522195801Smav	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
523195801Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
524195801Smav		device_printf(dev, "unable to create path\n");
525195801Smav		error = ENXIO;
526195801Smav		goto err3;
527195801Smav	}
528195801Smav	mtx_unlock(&ch->mtx);
529217877Smav	ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev));
530195801Smav	return (0);
531195801Smav
532195801Smaverr3:
533195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
534195801Smaverr2:
535195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
536195801Smaverr1:
537195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
538208816Smaverr0:
539195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
540195801Smav	mtx_unlock(&ch->mtx);
541214325Smav	mtx_destroy(&ch->mtx);
542195801Smav	return (error);
543195801Smav}
544195801Smav
545195801Smavstatic int
546195801Smavsiis_ch_detach(device_t dev)
547195801Smav{
548195801Smav	struct siis_channel *ch = device_get_softc(dev);
549195801Smav
550217877Smav	led_destroy(ch->led);
551195801Smav	mtx_lock(&ch->mtx);
552195801Smav	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
553195801Smav	xpt_free_path(ch->path);
554195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
555195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
556195801Smav	mtx_unlock(&ch->mtx);
557195801Smav
558195801Smav	bus_teardown_intr(dev, ch->r_irq, ch->ih);
559195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
560195801Smav
561208393Smav	siis_ch_deinit(dev);
562195801Smav	siis_slotsfree(dev);
563195801Smav	siis_dmafini(dev);
564195801Smav
565195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
566195801Smav	mtx_destroy(&ch->mtx);
567195801Smav	return (0);
568195801Smav}
569195801Smav
570195801Smavstatic int
571208393Smavsiis_ch_init(device_t dev)
572195801Smav{
573195801Smav	struct siis_channel *ch = device_get_softc(dev);
574195801Smav
575208393Smav	/* Get port out of reset state. */
576208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
577208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
578208393Smav	if (ch->pm_present)
579208393Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
580208393Smav	else
581208393Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
582208393Smav	/* Enable port interrupts */
583208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
584208393Smav	return (0);
585208393Smav}
586208393Smav
587208393Smavstatic int
588208393Smavsiis_ch_deinit(device_t dev)
589208393Smav{
590208393Smav	struct siis_channel *ch = device_get_softc(dev);
591208393Smav
592195801Smav	/* Put port into reset state. */
593195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
594195801Smav	return (0);
595195801Smav}
596195801Smav
597195801Smavstatic int
598208393Smavsiis_ch_suspend(device_t dev)
599208393Smav{
600208393Smav	struct siis_channel *ch = device_get_softc(dev);
601208393Smav
602208393Smav	mtx_lock(&ch->mtx);
603208393Smav	xpt_freeze_simq(ch->sim, 1);
604208393Smav	while (ch->oslots)
605208393Smav		msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100);
606208393Smav	siis_ch_deinit(dev);
607208393Smav	mtx_unlock(&ch->mtx);
608208393Smav	return (0);
609208393Smav}
610208393Smav
611208393Smavstatic int
612195801Smavsiis_ch_resume(device_t dev)
613195801Smav{
614195801Smav	struct siis_channel *ch = device_get_softc(dev);
615195801Smav
616208393Smav	mtx_lock(&ch->mtx);
617208393Smav	siis_ch_init(dev);
618208393Smav	siis_reset(dev);
619208393Smav	xpt_release_simq(ch->sim, TRUE);
620208393Smav	mtx_unlock(&ch->mtx);
621195801Smav	return (0);
622195801Smav}
623195801Smav
624195801Smavdevclass_t siisch_devclass;
625195801Smavstatic device_method_t siisch_methods[] = {
626195801Smav	DEVMETHOD(device_probe,     siis_ch_probe),
627195801Smav	DEVMETHOD(device_attach,    siis_ch_attach),
628195801Smav	DEVMETHOD(device_detach,    siis_ch_detach),
629195801Smav	DEVMETHOD(device_suspend,   siis_ch_suspend),
630195801Smav	DEVMETHOD(device_resume,    siis_ch_resume),
631195801Smav	{ 0, 0 }
632195801Smav};
633195801Smavstatic driver_t siisch_driver = {
634195801Smav        "siisch",
635195801Smav        siisch_methods,
636195801Smav        sizeof(struct siis_channel)
637195801Smav};
638195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
639195801Smav
640217877Smavstatic void
641217877Smavsiis_ch_led(void *priv, int onoff)
642217877Smav{
643217877Smav	device_t dev;
644217877Smav	struct siis_channel *ch;
645217877Smav
646217877Smav	dev = (device_t)priv;
647217877Smav	ch = device_get_softc(dev);
648217877Smav
649217877Smav	if (onoff == 0)
650217877Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON);
651217877Smav	else
652217877Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON);
653217877Smav}
654217877Smav
655195801Smavstruct siis_dc_cb_args {
656195801Smav	bus_addr_t maddr;
657195801Smav	int error;
658195801Smav};
659195801Smav
660195801Smavstatic void
661195801Smavsiis_dmainit(device_t dev)
662195801Smav{
663195801Smav	struct siis_channel *ch = device_get_softc(dev);
664195801Smav	struct siis_dc_cb_args dcba;
665195801Smav
666195801Smav	/* Command area. */
667195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
668195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
669195801Smav	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
670195801Smav	    0, NULL, NULL, &ch->dma.work_tag))
671195801Smav		goto error;
672195801Smav	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
673195801Smav	    &ch->dma.work_map))
674195801Smav		goto error;
675195801Smav	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
676195801Smav	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
677195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
678195801Smav		goto error;
679195801Smav	}
680195801Smav	ch->dma.work_bus = dcba.maddr;
681195801Smav	/* Data area. */
682199333Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
683195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
684195801Smav	    NULL, NULL,
685195801Smav	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
686195801Smav	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
687195801Smav	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
688195801Smav		goto error;
689195801Smav	}
690195801Smav	return;
691195801Smav
692195801Smaverror:
693195801Smav	device_printf(dev, "WARNING - DMA initialization failed\n");
694195801Smav	siis_dmafini(dev);
695195801Smav}
696195801Smav
697195801Smavstatic void
698195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
699195801Smav{
700195801Smav	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
701195801Smav
702195801Smav	if (!(dcba->error = error))
703195801Smav		dcba->maddr = segs[0].ds_addr;
704195801Smav}
705195801Smav
706195801Smavstatic void
707195801Smavsiis_dmafini(device_t dev)
708195801Smav{
709195801Smav	struct siis_channel *ch = device_get_softc(dev);
710195801Smav
711195801Smav	if (ch->dma.data_tag) {
712195801Smav		bus_dma_tag_destroy(ch->dma.data_tag);
713195801Smav		ch->dma.data_tag = NULL;
714195801Smav	}
715195801Smav	if (ch->dma.work_bus) {
716195801Smav		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
717195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
718195801Smav		ch->dma.work_bus = 0;
719195801Smav		ch->dma.work_map = NULL;
720195801Smav		ch->dma.work = NULL;
721195801Smav	}
722195801Smav	if (ch->dma.work_tag) {
723195801Smav		bus_dma_tag_destroy(ch->dma.work_tag);
724195801Smav		ch->dma.work_tag = NULL;
725195801Smav	}
726195801Smav}
727195801Smav
728195801Smavstatic void
729195801Smavsiis_slotsalloc(device_t dev)
730195801Smav{
731195801Smav	struct siis_channel *ch = device_get_softc(dev);
732195801Smav	int i;
733195801Smav
734195801Smav	/* Alloc and setup command/dma slots */
735195801Smav	bzero(ch->slot, sizeof(ch->slot));
736195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
737195801Smav		struct siis_slot *slot = &ch->slot[i];
738195801Smav
739195801Smav		slot->dev = dev;
740195801Smav		slot->slot = i;
741195801Smav		slot->state = SIIS_SLOT_EMPTY;
742195801Smav		slot->ccb = NULL;
743195801Smav		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
744195801Smav
745195801Smav		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
746195801Smav			device_printf(ch->dev, "FAILURE - create data_map\n");
747195801Smav	}
748195801Smav}
749195801Smav
750195801Smavstatic void
751195801Smavsiis_slotsfree(device_t dev)
752195801Smav{
753195801Smav	struct siis_channel *ch = device_get_softc(dev);
754195801Smav	int i;
755195801Smav
756195801Smav	/* Free all dma slots */
757195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
758195801Smav		struct siis_slot *slot = &ch->slot[i];
759195801Smav
760198896Smav		callout_drain(&slot->timeout);
761195801Smav		if (slot->dma.data_map) {
762195801Smav			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
763195801Smav			slot->dma.data_map = NULL;
764195801Smav		}
765195801Smav	}
766195801Smav}
767195801Smav
768195801Smavstatic void
769196655Smavsiis_notify_events(device_t dev)
770196655Smav{
771196655Smav	struct siis_channel *ch = device_get_softc(dev);
772196655Smav	struct cam_path *dpath;
773196655Smav	u_int32_t status;
774196655Smav	int i;
775196655Smav
776200217Smav	if (ch->quirks & SIIS_Q_SNTF) {
777200217Smav		status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
778200217Smav		ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
779200217Smav	} else {
780200217Smav		/*
781200217Smav		 * Without SNTF we have no idea which device sent notification.
782200217Smav		 * If PMP is connected, assume it, else - device.
783200217Smav		 */
784200217Smav		status = (ch->pm_present) ? 0x8000 : 0x0001;
785200217Smav	}
786196655Smav	if (bootverbose)
787196655Smav		device_printf(dev, "SNTF 0x%04x\n", status);
788196655Smav	for (i = 0; i < 16; i++) {
789196655Smav		if ((status & (1 << i)) == 0)
790196655Smav			continue;
791196655Smav		if (xpt_create_path(&dpath, NULL,
792196655Smav		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
793196655Smav			xpt_async(AC_SCSI_AEN, dpath, NULL);
794196655Smav			xpt_free_path(dpath);
795196655Smav		}
796196655Smav	}
797196655Smav
798196655Smav}
799196655Smav
800196655Smavstatic void
801195801Smavsiis_phy_check_events(device_t dev)
802195801Smav{
803195801Smav	struct siis_channel *ch = device_get_softc(dev);
804195801Smav
805195801Smav	/* If we have a connection event, deal with it */
806195801Smav	if (ch->pm_level == 0) {
807195801Smav		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
808203108Smav		union ccb *ccb;
809203108Smav
810203165Smav		if (bootverbose) {
811203165Smav			if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
812203165Smav			    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
813203165Smav			    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
814195801Smav				device_printf(dev, "CONNECT requested\n");
815203165Smav			} else
816195801Smav				device_printf(dev, "DISCONNECT requested\n");
817195801Smav		}
818203165Smav		siis_reset(dev);
819203108Smav		if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
820203108Smav			return;
821203108Smav		if (xpt_create_path(&ccb->ccb_h.path, NULL,
822203108Smav		    cam_sim_path(ch->sim),
823203108Smav		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
824203108Smav			xpt_free_ccb(ccb);
825203108Smav			return;
826203108Smav		}
827203108Smav		xpt_rescan(ccb);
828195801Smav	}
829195801Smav}
830195801Smav
831195801Smavstatic void
832195801Smavsiis_ch_intr_locked(void *data)
833195801Smav{
834195801Smav	device_t dev = (device_t)data;
835195801Smav	struct siis_channel *ch = device_get_softc(dev);
836195801Smav
837195801Smav	mtx_lock(&ch->mtx);
838195801Smav	siis_ch_intr(data);
839195801Smav	mtx_unlock(&ch->mtx);
840195801Smav}
841195801Smav
842195801Smavstatic void
843195801Smavsiis_ch_intr(void *data)
844195801Smav{
845195801Smav	device_t dev = (device_t)data;
846195801Smav	struct siis_channel *ch = device_get_softc(dev);
847195801Smav	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
848195801Smav	enum siis_err_type et;
849195801Smav	int i, ccs, port, tslots;
850195801Smav
851195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
852195801Smav	/* Read command statuses. */
853195801Smav	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
854195801Smav	ok = ch->rslots & ~sstatus;
855195801Smav	/* Complete all successfull commands. */
856195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
857195801Smav		if ((ok >> i) & 1)
858195801Smav			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
859195801Smav	}
860195801Smav	/* Do we have any other events? */
861195801Smav	if ((sstatus & SIIS_P_SS_ATTN) == 0)
862195801Smav		return;
863195801Smav	/* Read and clear interrupt statuses. */
864195801Smav	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
865195801Smav	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
866195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
867195801Smav	/* Process PHY events */
868195801Smav	if (istatus & SIIS_P_IX_PHYRDYCHG)
869195801Smav		siis_phy_check_events(dev);
870196655Smav	/* Process NOTIFY events */
871196655Smav	if (istatus & SIIS_P_IX_SDBN)
872196655Smav		siis_notify_events(dev);
873195801Smav	/* Process command errors */
874195801Smav	if (istatus & SIIS_P_IX_COMMERR) {
875195801Smav		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
876195801Smav		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
877195801Smav		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
878195801Smav		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
879195801Smav		err = ch->rslots & sstatus;
880195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
881195801Smav//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
882195801Smav//    ATA_INL(ch->r_mem, SIIS_P_SERR));
883195801Smav
884220566Smav		if (!ch->recoverycmd && !ch->recovery) {
885195801Smav			xpt_freeze_simq(ch->sim, ch->numrslots);
886195801Smav			ch->recovery = 1;
887195801Smav		}
888195801Smav		if (ch->frozen) {
889195801Smav			union ccb *fccb = ch->frozen;
890195801Smav			ch->frozen = NULL;
891198321Smav			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
892198321Smav			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
893198321Smav			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
894198321Smav				xpt_freeze_devq(fccb->ccb_h.path, 1);
895198321Smav				fccb->ccb_h.status |= CAM_DEV_QFRZN;
896198321Smav			}
897195801Smav			xpt_done(fccb);
898195801Smav		}
899195801Smav		if (estatus == SIIS_P_CMDERR_DEV ||
900195801Smav		    estatus == SIIS_P_CMDERR_SDB ||
901195801Smav		    estatus == SIIS_P_CMDERR_DATAFIS) {
902195801Smav			tslots = ch->numtslots[port];
903195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
904198852Smav				/* XXX: requests in loading state. */
905195801Smav				if (((ch->rslots >> i) & 1) == 0)
906195801Smav					continue;
907195801Smav				if (ch->slot[i].ccb->ccb_h.target_id != port)
908195801Smav					continue;
909195801Smav				if (tslots == 0) {
910195801Smav					/* Untagged operation. */
911195801Smav					if (i == ccs)
912195801Smav						et = SIIS_ERR_TFE;
913195801Smav					else
914195801Smav						et = SIIS_ERR_INNOCENT;
915195801Smav				} else {
916195801Smav					/* Tagged operation. */
917195801Smav					et = SIIS_ERR_NCQ;
918195801Smav				}
919195801Smav				siis_end_transaction(&ch->slot[i], et);
920195801Smav			}
921195801Smav			/*
922195801Smav			 * We can't reinit port if there are some other
923195801Smav			 * commands active, use resume to complete them.
924195801Smav			 */
925220566Smav			if (ch->rslots != 0 && !ch->recoverycmd)
926195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
927195801Smav		} else {
928195801Smav			if (estatus == SIIS_P_CMDERR_SENDFIS ||
929195801Smav			    estatus == SIIS_P_CMDERR_INCSTATE ||
930195801Smav			    estatus == SIIS_P_CMDERR_PPE ||
931195801Smav			    estatus == SIIS_P_CMDERR_SERVICE) {
932195801Smav				et = SIIS_ERR_SATA;
933195801Smav			} else
934195801Smav				et = SIIS_ERR_INVALID;
935195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
936198852Smav				/* XXX: requests in loading state. */
937195801Smav				if (((ch->rslots >> i) & 1) == 0)
938195801Smav					continue;
939195801Smav				siis_end_transaction(&ch->slot[i], et);
940195801Smav			}
941195801Smav		}
942195801Smav	}
943195801Smav}
944195801Smav
945195801Smav/* Must be called with channel locked. */
946195801Smavstatic int
947195801Smavsiis_check_collision(device_t dev, union ccb *ccb)
948195801Smav{
949195801Smav	struct siis_channel *ch = device_get_softc(dev);
950195801Smav
951195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
952195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
953199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
954199747Smav		/* Tagged command while we have no supported tag free. */
955199747Smav		if (((~ch->oslots) & (0x7fffffff >> (31 -
956199747Smav		    ch->curr[ccb->ccb_h.target_id].tags))) == 0)
957199747Smav			return (1);
958199747Smav	}
959199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
960195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
961195801Smav		/* Atomic command while anything active. */
962195801Smav		if (ch->numrslots != 0)
963195801Smav			return (1);
964195801Smav	}
965195801Smav       /* We have some atomic command running. */
966195801Smav       if (ch->aslots != 0)
967195801Smav               return (1);
968195801Smav	return (0);
969195801Smav}
970195801Smav
971195801Smav/* Must be called with channel locked. */
972195801Smavstatic void
973195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb)
974195801Smav{
975195801Smav	struct siis_channel *ch = device_get_softc(dev);
976195801Smav	struct siis_slot *slot;
977199747Smav	int tag, tags;
978195801Smav
979195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
980195801Smav	/* Choose empty slot. */
981199747Smav	tags = SIIS_MAX_SLOTS;
982199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
983199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
984199747Smav		tags = ch->curr[ccb->ccb_h.target_id].tags;
985199747Smav	tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1;
986195801Smav	/* Occupy chosen slot. */
987195801Smav	slot = &ch->slot[tag];
988195801Smav	slot->ccb = ccb;
989195801Smav	/* Update channel stats. */
990199747Smav	ch->oslots |= (1 << slot->slot);
991195801Smav	ch->numrslots++;
992195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
993195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
994195801Smav		ch->numtslots[ccb->ccb_h.target_id]++;
995195801Smav	}
996195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
997195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
998195801Smav		ch->aslots |= (1 << slot->slot);
999195801Smav	slot->dma.nsegs = 0;
1000195801Smav	/* If request moves data, setup and load SG list */
1001195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1002195801Smav		void *buf;
1003195801Smav		bus_size_t size;
1004195801Smav
1005195801Smav		slot->state = SIIS_SLOT_LOADING;
1006195801Smav		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1007195801Smav			buf = ccb->ataio.data_ptr;
1008195801Smav			size = ccb->ataio.dxfer_len;
1009195801Smav		} else {
1010195801Smav			buf = ccb->csio.data_ptr;
1011195801Smav			size = ccb->csio.dxfer_len;
1012195801Smav		}
1013195801Smav		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
1014195801Smav		    buf, size, siis_dmasetprd, slot, 0);
1015195801Smav	} else
1016195801Smav		siis_execute_transaction(slot);
1017195801Smav}
1018195801Smav
1019195801Smav/* Locked by busdma engine. */
1020195801Smavstatic void
1021195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1022195801Smav{
1023195801Smav	struct siis_slot *slot = arg;
1024195801Smav	struct siis_channel *ch = device_get_softc(slot->dev);
1025195801Smav	struct siis_cmd *ctp;
1026195801Smav	struct siis_dma_prd *prd;
1027195801Smav	int i;
1028195801Smav
1029195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1030195801Smav	if (error) {
1031195801Smav		device_printf(slot->dev, "DMA load error\n");
1032220566Smav		if (!ch->recoverycmd)
1033195801Smav			xpt_freeze_simq(ch->sim, 1);
1034195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1035195801Smav		return;
1036195801Smav	}
1037195801Smav	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
1038195801Smav	/* Get a piece of the workspace for this request */
1039195801Smav	ctp = (struct siis_cmd *)
1040195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
1041195801Smav	/* Fill S/G table */
1042195801Smav	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
1043195801Smav		prd = &ctp->u.ata.prd[0];
1044195801Smav	else
1045195801Smav		prd = &ctp->u.atapi.prd[0];
1046195801Smav	for (i = 0; i < nsegs; i++) {
1047195801Smav		prd[i].dba = htole64(segs[i].ds_addr);
1048195801Smav		prd[i].dbc = htole32(segs[i].ds_len);
1049195801Smav		prd[i].control = 0;
1050195801Smav	}
1051195801Smav	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
1052195801Smav	slot->dma.nsegs = nsegs;
1053195801Smav	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1054195801Smav	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1055195801Smav	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1056195801Smav	siis_execute_transaction(slot);
1057195801Smav}
1058195801Smav
1059195801Smav/* Must be called with channel locked. */
1060195801Smavstatic void
1061195801Smavsiis_execute_transaction(struct siis_slot *slot)
1062195801Smav{
1063195801Smav	device_t dev = slot->dev;
1064195801Smav	struct siis_channel *ch = device_get_softc(dev);
1065195801Smav	struct siis_cmd *ctp;
1066195801Smav	union ccb *ccb = slot->ccb;
1067195801Smav	u_int64_t prb_bus;
1068195801Smav
1069195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1070195801Smav	/* Get a piece of the workspace for this request */
1071195801Smav	ctp = (struct siis_cmd *)
1072195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
1073195801Smav	ctp->control = 0;
1074195801Smav	ctp->protocol_override = 0;
1075195801Smav	ctp->transfer_count = 0;
1076195801Smav	/* Special handling for Soft Reset command. */
1077201222Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1078201222Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) {
1079201222Smav			ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
1080201222Smav		} else {
1081201222Smav			ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE);
1082201222Smav			if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1083201222Smav				ctp->protocol_override |=
1084201222Smav				    htole16(SIIS_PRB_PROTO_NCQ);
1085201222Smav			}
1086201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1087201222Smav				ctp->protocol_override |=
1088201222Smav				    htole16(SIIS_PRB_PROTO_READ);
1089201222Smav			} else
1090201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1091201222Smav				ctp->protocol_override |=
1092201222Smav				    htole16(SIIS_PRB_PROTO_WRITE);
1093201222Smav			}
1094201222Smav		}
1095195801Smav	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1096201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1097195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
1098201222Smav		else
1099201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1100195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
1101195801Smav	}
1102203108Smav	/* Special handling for Soft Reset command. */
1103203108Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1104203108Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1105203108Smav	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1106203108Smav		/* Kick controller into sane state */
1107203108Smav		siis_portinit(dev);
1108203108Smav	}
1109195801Smav	/* Setup the FIS for this request */
1110199821Smav	if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) {
1111195801Smav		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1112220566Smav		if (!ch->recoverycmd)
1113195801Smav			xpt_freeze_simq(ch->sim, 1);
1114195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1115195801Smav		return;
1116195801Smav	}
1117195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1118195801Smav	    BUS_DMASYNC_PREWRITE);
1119195801Smav	/* Issue command to the controller. */
1120195801Smav	slot->state = SIIS_SLOT_RUNNING;
1121195801Smav	ch->rslots |= (1 << slot->slot);
1122195801Smav	prb_bus = ch->dma.work_bus +
1123195801Smav	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
1124195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
1125195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
1126195801Smav	/* Start command execution timeout */
1127195801Smav	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
1128195801Smav	    (timeout_t*)siis_timeout, slot);
1129195801Smav	return;
1130195801Smav}
1131195801Smav
1132198852Smav/* Must be called with channel locked. */
1133195801Smavstatic void
1134198852Smavsiis_process_timeout(device_t dev)
1135195801Smav{
1136195801Smav	struct siis_channel *ch = device_get_softc(dev);
1137195801Smav	int i;
1138195801Smav
1139195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1140220566Smav	if (!ch->recoverycmd && !ch->recovery) {
1141195801Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1142198852Smav		ch->recovery = 1;
1143195801Smav	}
1144197838Smav	/* Handle the rest of commands. */
1145195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1146195801Smav		/* Do we have a running request on slot? */
1147195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1148195801Smav			continue;
1149198852Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
1150195801Smav	}
1151195801Smav}
1152195801Smav
1153203870Smav/* Must be called with channel locked. */
1154203870Smavstatic void
1155203870Smavsiis_rearm_timeout(device_t dev)
1156203870Smav{
1157203870Smav	struct siis_channel *ch = device_get_softc(dev);
1158203870Smav	int i;
1159203870Smav
1160203870Smav	mtx_assert(&ch->mtx, MA_OWNED);
1161203870Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1162203870Smav		struct siis_slot *slot = &ch->slot[i];
1163203870Smav
1164203870Smav		/* Do we have a running request on slot? */
1165203870Smav		if (slot->state < SIIS_SLOT_RUNNING)
1166203870Smav			continue;
1167203870Smav		if ((ch->toslots & (1 << i)) == 0)
1168203870Smav			continue;
1169203870Smav		callout_reset(&slot->timeout,
1170203870Smav		    (int)slot->ccb->ccb_h.timeout * hz / 1000,
1171203870Smav		    (timeout_t*)siis_timeout, slot);
1172203870Smav	}
1173203870Smav}
1174203870Smav
1175198852Smav/* Locked by callout mechanism. */
1176198852Smavstatic void
1177198852Smavsiis_timeout(struct siis_slot *slot)
1178198852Smav{
1179198852Smav	device_t dev = slot->dev;
1180198852Smav	struct siis_channel *ch = device_get_softc(dev);
1181198852Smav
1182198852Smav	mtx_assert(&ch->mtx, MA_OWNED);
1183198896Smav	/* Check for stale timeout. */
1184198896Smav	if (slot->state < SIIS_SLOT_RUNNING)
1185198896Smav		return;
1186198852Smav	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1187203108Smav	device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
1188203108Smav	    __func__, ATA_INL(ch->r_mem, SIIS_P_IS),
1189203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
1190203108Smav	    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
1191203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SERR));
1192198852Smav
1193198852Smav	if (ch->toslots == 0)
1194198852Smav		xpt_freeze_simq(ch->sim, 1);
1195198852Smav	ch->toslots |= (1 << slot->slot);
1196198852Smav	if ((ch->rslots & ~ch->toslots) == 0)
1197198852Smav		siis_process_timeout(dev);
1198198852Smav	else
1199198852Smav		device_printf(dev, " ... waiting for slots %08x\n",
1200198852Smav		    ch->rslots & ~ch->toslots);
1201198852Smav}
1202198852Smav
1203195801Smav/* Must be called with channel locked. */
1204195801Smavstatic void
1205195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1206195801Smav{
1207195801Smav	device_t dev = slot->dev;
1208195801Smav	struct siis_channel *ch = device_get_softc(dev);
1209195801Smav	union ccb *ccb = slot->ccb;
1210212732Smav	int lastto;
1211195801Smav
1212195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1213195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1214195801Smav	    BUS_DMASYNC_POSTWRITE);
1215195801Smav	/* Read result registers to the result struct
1216195801Smav	 * May be incorrect if several commands finished same time,
1217195801Smav	 * so read only when sure or have to.
1218195801Smav	 */
1219195801Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1220195801Smav		struct ata_res *res = &ccb->ataio.res;
1221195801Smav		if ((et == SIIS_ERR_TFE) ||
1222195801Smav		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1223195801Smav			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1224195801Smav
1225195801Smav			res->status = ATA_INB(ch->r_mem, offs + 2);
1226195801Smav			res->error = ATA_INB(ch->r_mem, offs + 3);
1227195801Smav			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1228195801Smav			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1229195801Smav			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1230195801Smav			res->device = ATA_INB(ch->r_mem, offs + 7);
1231195801Smav			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1232195801Smav			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1233195801Smav			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1234195801Smav			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1235195801Smav			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1236195801Smav		} else
1237195801Smav			bzero(res, sizeof(*res));
1238214988Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1239214988Smav		    ch->numrslots == 1) {
1240214988Smav			ccb->ataio.resid = ccb->ataio.dxfer_len -
1241214988Smav			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1242214988Smav		}
1243214988Smav	} else {
1244214988Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1245214988Smav		    ch->numrslots == 1) {
1246214988Smav			ccb->csio.resid = ccb->csio.dxfer_len -
1247214988Smav			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1248214988Smav		}
1249195801Smav	}
1250195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1251195801Smav		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1252195801Smav		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1253195801Smav		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1254195801Smav		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1255195801Smav	}
1256195801Smav	/* Set proper result status. */
1257195801Smav	if (et != SIIS_ERR_NONE || ch->recovery) {
1258195801Smav		ch->eslots |= (1 << slot->slot);
1259195801Smav		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1260195801Smav	}
1261198321Smav	/* In case of error, freeze device for proper recovery. */
1262220566Smav	if (et != SIIS_ERR_NONE && (!ch->recoverycmd) &&
1263198321Smav	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1264198321Smav		xpt_freeze_devq(ccb->ccb_h.path, 1);
1265198321Smav		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1266198321Smav	}
1267198321Smav	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1268195801Smav	switch (et) {
1269195801Smav	case SIIS_ERR_NONE:
1270195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP;
1271195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1272195801Smav			ccb->csio.scsi_status = SCSI_STATUS_OK;
1273195801Smav		break;
1274195801Smav	case SIIS_ERR_INVALID:
1275198852Smav		ch->fatalerr = 1;
1276195801Smav		ccb->ccb_h.status |= CAM_REQ_INVALID;
1277195801Smav		break;
1278195801Smav	case SIIS_ERR_INNOCENT:
1279195801Smav		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1280195801Smav		break;
1281195801Smav	case SIIS_ERR_TFE:
1282198321Smav	case SIIS_ERR_NCQ:
1283195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1284195801Smav			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1285195801Smav			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1286195801Smav		} else {
1287195801Smav			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1288195801Smav		}
1289195801Smav		break;
1290195801Smav	case SIIS_ERR_SATA:
1291198852Smav		ch->fatalerr = 1;
1292195801Smav		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1293195801Smav		break;
1294195801Smav	case SIIS_ERR_TIMEOUT:
1295198852Smav		ch->fatalerr = 1;
1296195801Smav		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1297195801Smav		break;
1298195801Smav	default:
1299195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1300195801Smav	}
1301195801Smav	/* Free slot. */
1302199747Smav	ch->oslots &= ~(1 << slot->slot);
1303195801Smav	ch->rslots &= ~(1 << slot->slot);
1304195801Smav	ch->aslots &= ~(1 << slot->slot);
1305195801Smav	slot->state = SIIS_SLOT_EMPTY;
1306195801Smav	slot->ccb = NULL;
1307195801Smav	/* Update channel stats. */
1308195801Smav	ch->numrslots--;
1309195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1310195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1311195801Smav		ch->numtslots[ccb->ccb_h.target_id]--;
1312195801Smav	}
1313212732Smav	/* Cancel timeout state if request completed normally. */
1314212732Smav	if (et != SIIS_ERR_TIMEOUT) {
1315212732Smav		lastto = (ch->toslots == (1 << slot->slot));
1316212732Smav		ch->toslots &= ~(1 << slot->slot);
1317212732Smav		if (lastto)
1318212732Smav			xpt_release_simq(ch->sim, TRUE);
1319212732Smav	}
1320198852Smav	/* If it was our READ LOG command - process it. */
1321220566Smav	if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
1322198852Smav		siis_process_read_log(dev, ccb);
1323220566Smav	/* If it was our REQUEST SENSE command - process it. */
1324220566Smav	} else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
1325220566Smav		siis_process_request_sense(dev, ccb);
1326220566Smav	/* If it was NCQ or ATAPI command error, put result on hold. */
1327220566Smav	} else if (et == SIIS_ERR_NCQ ||
1328220566Smav	    ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1329220566Smav	     (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
1330195801Smav		ch->hold[slot->slot] = ccb;
1331195801Smav		ch->numhslots++;
1332198852Smav	} else
1333195801Smav		xpt_done(ccb);
1334195801Smav	/* Unfreeze frozen command. */
1335199747Smav	if (ch->frozen && !siis_check_collision(dev, ch->frozen)) {
1336195801Smav		union ccb *fccb = ch->frozen;
1337195801Smav		ch->frozen = NULL;
1338195801Smav		siis_begin_transaction(dev, fccb);
1339195801Smav		xpt_release_simq(ch->sim, TRUE);
1340195801Smav	}
1341195801Smav	/* If we have no other active commands, ... */
1342195801Smav	if (ch->rslots == 0) {
1343198852Smav		/* if there were timeouts or fatal error - reset port. */
1344198852Smav		if (ch->toslots != 0 || ch->fatalerr) {
1345198852Smav			siis_reset(dev);
1346198852Smav		} else {
1347198852Smav			/* if we have slots in error, we can reinit port. */
1348198852Smav			if (ch->eslots != 0)
1349198852Smav				siis_portinit(dev);
1350220566Smav			/* if there commands on hold, we can do recovery. */
1351220566Smav			if (!ch->recoverycmd && ch->numhslots)
1352220566Smav				siis_issue_recovery(dev);
1353198852Smav		}
1354198852Smav	/* If all the reset of commands are in timeout - abort them. */
1355203870Smav	} else if ((ch->rslots & ~ch->toslots) == 0 &&
1356203870Smav	    et != SIIS_ERR_TIMEOUT)
1357203870Smav		siis_rearm_timeout(dev);
1358195801Smav}
1359195801Smav
1360195801Smavstatic void
1361220566Smavsiis_issue_recovery(device_t dev)
1362195801Smav{
1363195801Smav	struct siis_channel *ch = device_get_softc(dev);
1364195801Smav	union ccb *ccb;
1365195801Smav	struct ccb_ataio *ataio;
1366220566Smav	struct ccb_scsiio *csio;
1367195801Smav	int i;
1368195801Smav
1369195801Smav	/* Find some holden command. */
1370195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1371195801Smav		if (ch->hold[i])
1372195801Smav			break;
1373195801Smav	}
1374195801Smav	if (i == SIIS_MAX_SLOTS)
1375195801Smav		return;
1376220566Smav	ch->recoverycmd = 1;
1377195801Smav	ccb = xpt_alloc_ccb_nowait();
1378195801Smav	if (ccb == NULL) {
1379195801Smav		device_printf(dev, "Unable allocate READ LOG command");
1380195801Smav		return; /* XXX */
1381195801Smav	}
1382195801Smav	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1383220566Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1384220566Smav		/* READ LOG */
1385220566Smav		ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
1386220566Smav		ccb->ccb_h.func_code = XPT_ATA_IO;
1387220566Smav		ccb->ccb_h.flags = CAM_DIR_IN;
1388220566Smav		ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1389220566Smav		ataio = &ccb->ataio;
1390220566Smav		ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1391220566Smav		if (ataio->data_ptr == NULL) {
1392220566Smav			xpt_free_ccb(ccb);
1393220566Smav			device_printf(dev, "Unable allocate memory for READ LOG command");
1394220566Smav			return; /* XXX */
1395220566Smav		}
1396220566Smav		ataio->dxfer_len = 512;
1397220566Smav		bzero(&ataio->cmd, sizeof(ataio->cmd));
1398220566Smav		ataio->cmd.flags = CAM_ATAIO_48BIT;
1399220566Smav		ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1400220566Smav		ataio->cmd.sector_count = 1;
1401220566Smav		ataio->cmd.sector_count_exp = 0;
1402220566Smav		ataio->cmd.lba_low = 0x10;
1403220566Smav		ataio->cmd.lba_mid = 0;
1404220566Smav		ataio->cmd.lba_mid_exp = 0;
1405220566Smav	} else {
1406220566Smav		/* REQUEST SENSE */
1407220566Smav		ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
1408220566Smav		ccb->ccb_h.recovery_slot = i;
1409220566Smav		ccb->ccb_h.func_code = XPT_SCSI_IO;
1410220566Smav		ccb->ccb_h.flags = CAM_DIR_IN;
1411220566Smav		ccb->ccb_h.status = 0;
1412220566Smav		ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1413220566Smav		csio = &ccb->csio;
1414220566Smav		csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
1415220566Smav		csio->dxfer_len = ch->hold[i]->csio.sense_len;
1416220566Smav		csio->cdb_len = 6;
1417220566Smav		bzero(&csio->cdb_io, sizeof(csio->cdb_io));
1418220566Smav		csio->cdb_io.cdb_bytes[0] = 0x03;
1419220566Smav		csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
1420195801Smav	}
1421195801Smav	siis_begin_transaction(dev, ccb);
1422195801Smav}
1423195801Smav
1424195801Smavstatic void
1425195801Smavsiis_process_read_log(device_t dev, union ccb *ccb)
1426195801Smav{
1427195801Smav	struct siis_channel *ch = device_get_softc(dev);
1428195801Smav	uint8_t *data;
1429195801Smav	struct ata_res *res;
1430195801Smav	int i;
1431195801Smav
1432220566Smav	ch->recoverycmd = 0;
1433195801Smav	data = ccb->ataio.data_ptr;
1434195801Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1435195801Smav	    (data[0] & 0x80) == 0) {
1436195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1437195801Smav			if (!ch->hold[i])
1438195801Smav				continue;
1439195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1440195801Smav				continue;
1441195801Smav			if ((data[0] & 0x1F) == i) {
1442195801Smav				res = &ch->hold[i]->ataio.res;
1443195801Smav				res->status = data[2];
1444195801Smav				res->error = data[3];
1445195801Smav				res->lba_low = data[4];
1446195801Smav				res->lba_mid = data[5];
1447195801Smav				res->lba_high = data[6];
1448195801Smav				res->device = data[7];
1449195801Smav				res->lba_low_exp = data[8];
1450195801Smav				res->lba_mid_exp = data[9];
1451195801Smav				res->lba_high_exp = data[10];
1452195801Smav				res->sector_count = data[12];
1453195801Smav				res->sector_count_exp = data[13];
1454195801Smav			} else {
1455195801Smav				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1456195801Smav				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1457195801Smav			}
1458195801Smav			xpt_done(ch->hold[i]);
1459195801Smav			ch->hold[i] = NULL;
1460195801Smav			ch->numhslots--;
1461195801Smav		}
1462195801Smav	} else {
1463195801Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1464195801Smav			device_printf(dev, "Error while READ LOG EXT\n");
1465195801Smav		else if ((data[0] & 0x80) == 0) {
1466195801Smav			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1467195801Smav		}
1468195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1469195801Smav			if (!ch->hold[i])
1470195801Smav				continue;
1471195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1472195801Smav				continue;
1473195801Smav			xpt_done(ch->hold[i]);
1474195801Smav			ch->hold[i] = NULL;
1475195801Smav			ch->numhslots--;
1476195801Smav		}
1477195801Smav	}
1478195801Smav	free(ccb->ataio.data_ptr, M_SIIS);
1479195801Smav	xpt_free_ccb(ccb);
1480195801Smav}
1481195801Smav
1482195801Smavstatic void
1483220566Smavsiis_process_request_sense(device_t dev, union ccb *ccb)
1484220566Smav{
1485220566Smav	struct siis_channel *ch = device_get_softc(dev);
1486220566Smav	int i;
1487220566Smav
1488220566Smav	ch->recoverycmd = 0;
1489220566Smav
1490220566Smav	i = ccb->ccb_h.recovery_slot;
1491220566Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1492220566Smav		ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
1493220566Smav	} else {
1494220566Smav		ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1495220566Smav		ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1496220566Smav	}
1497220566Smav	xpt_done(ch->hold[i]);
1498220566Smav	ch->hold[i] = NULL;
1499220566Smav	ch->numhslots--;
1500220566Smav	xpt_free_ccb(ccb);
1501220566Smav}
1502220566Smav
1503220566Smavstatic void
1504195801Smavsiis_portinit(device_t dev)
1505195801Smav{
1506195801Smav	struct siis_channel *ch = device_get_softc(dev);
1507195801Smav	int i;
1508195801Smav
1509195801Smav	ch->eslots = 0;
1510195801Smav	ch->recovery = 0;
1511195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1512195801Smav	for (i = 0; i < 16; i++) {
1513195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1514195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1515195801Smav	}
1516195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1517195801Smav	siis_wait_ready(dev, 1000);
1518195801Smav}
1519195801Smav
1520198426Smavstatic int
1521195801Smavsiis_devreset(device_t dev)
1522195801Smav{
1523195801Smav	struct siis_channel *ch = device_get_softc(dev);
1524198426Smav	int timeout = 0;
1525198426Smav	uint32_t val;
1526195801Smav
1527195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1528198426Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1529198426Smav	    SIIS_P_CTL_DEV_RESET) != 0) {
1530198426Smav		DELAY(1000);
1531198426Smav		if (timeout++ > 100) {
1532198426Smav			device_printf(dev, "device reset stuck (timeout %dms) "
1533198426Smav			    "status = %08x\n", timeout, val);
1534198426Smav			return (EBUSY);
1535198426Smav		}
1536198426Smav	}
1537198426Smav	return (0);
1538195801Smav}
1539195801Smav
1540195801Smavstatic int
1541195801Smavsiis_wait_ready(device_t dev, int t)
1542195801Smav{
1543195801Smav	struct siis_channel *ch = device_get_softc(dev);
1544195801Smav	int timeout = 0;
1545195801Smav	uint32_t val;
1546195801Smav
1547195801Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1548195801Smav	    SIIS_P_CTL_READY) == 0) {
1549195801Smav		DELAY(1000);
1550195801Smav		if (timeout++ > t) {
1551195801Smav			device_printf(dev, "port is not ready (timeout %dms) "
1552195801Smav			    "status = %08x\n", t, val);
1553195801Smav			return (EBUSY);
1554195801Smav		}
1555195801Smav	}
1556195801Smav	return (0);
1557195801Smav}
1558195801Smav
1559195801Smavstatic void
1560195801Smavsiis_reset(device_t dev)
1561195801Smav{
1562195801Smav	struct siis_channel *ch = device_get_softc(dev);
1563199821Smav	int i, retry = 0, sata_rev;
1564198426Smav	uint32_t val;
1565195801Smav
1566203108Smav	xpt_freeze_simq(ch->sim, 1);
1567195801Smav	if (bootverbose)
1568195801Smav		device_printf(dev, "SIIS reset...\n");
1569220566Smav	if (!ch->recoverycmd && !ch->recovery)
1570198852Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1571198852Smav	/* Requeue frozen command. */
1572195801Smav	if (ch->frozen) {
1573195801Smav		union ccb *fccb = ch->frozen;
1574195801Smav		ch->frozen = NULL;
1575198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1576198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1577198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1578198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
1579198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1580198321Smav		}
1581195801Smav		xpt_done(fccb);
1582195801Smav	}
1583198426Smav	/* Requeue all running commands. */
1584195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1585195801Smav		/* Do we have a running request on slot? */
1586195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1587195801Smav			continue;
1588195801Smav		/* XXX; Commands in loading state. */
1589195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1590195801Smav	}
1591198852Smav	/* Finish all holden commands as-is. */
1592198852Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1593198852Smav		if (!ch->hold[i])
1594198852Smav			continue;
1595198852Smav		xpt_done(ch->hold[i]);
1596198852Smav		ch->hold[i] = NULL;
1597198852Smav		ch->numhslots--;
1598198852Smav	}
1599198852Smav	if (ch->toslots != 0)
1600198852Smav		xpt_release_simq(ch->sim, TRUE);
1601198852Smav	ch->eslots = 0;
1602198852Smav	ch->recovery = 0;
1603198852Smav	ch->toslots = 0;
1604198852Smav	ch->fatalerr = 0;
1605198426Smav	/* Disable port interrupts */
1606198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1607198426Smav	/* Set speed limit. */
1608199821Smav	sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
1609199821Smav	if (sata_rev == 1)
1610198426Smav		val = ATA_SC_SPD_SPEED_GEN1;
1611199821Smav	else if (sata_rev == 2)
1612198426Smav		val = ATA_SC_SPD_SPEED_GEN2;
1613199821Smav	else if (sata_rev == 3)
1614198426Smav		val = ATA_SC_SPD_SPEED_GEN3;
1615198426Smav	else
1616198426Smav		val = 0;
1617198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1618198426Smav	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1619198426Smav	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1620198852Smavretry:
1621198426Smav	siis_devreset(dev);
1622195801Smav	/* Reset and reconnect PHY, */
1623198426Smav	if (!siis_sata_connect(ch)) {
1624195801Smav		ch->devices = 0;
1625195801Smav		/* Enable port interrupts */
1626195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1627195801Smav		if (bootverbose)
1628195801Smav			device_printf(dev,
1629195801Smav			    "SIIS reset done: phy reset found no device\n");
1630195801Smav		/* Tell the XPT about the event */
1631195801Smav		xpt_async(AC_BUS_RESET, ch->path, NULL);
1632203108Smav		xpt_release_simq(ch->sim, TRUE);
1633195801Smav		return;
1634195801Smav	}
1635220591Smav	/* Wait for port ready status. */
1636220591Smav	if (siis_wait_ready(dev, 1000)) {
1637220591Smav		device_printf(dev, "port ready timeout\n");
1638198852Smav		if (!retry) {
1639198852Smav			device_printf(dev, "trying full port reset ...\n");
1640198852Smav			/* Get port to the reset state. */
1641198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
1642198852Smav			DELAY(10000);
1643198852Smav			/* Get port out of reset state. */
1644198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
1645198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
1646198852Smav			if (ch->pm_present)
1647198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1648198852Smav			else
1649198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1650198852Smav			siis_wait_ready(dev, 5000);
1651198852Smav			retry = 1;
1652198852Smav			goto retry;
1653198852Smav		}
1654198852Smav	}
1655195801Smav	ch->devices = 1;
1656195801Smav	/* Enable port interrupts */
1657195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1658195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1659195801Smav	if (bootverbose)
1660195801Smav		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1661195801Smav	/* Tell the XPT about the event */
1662195801Smav	xpt_async(AC_BUS_RESET, ch->path, NULL);
1663203108Smav	xpt_release_simq(ch->sim, TRUE);
1664195801Smav}
1665195801Smav
1666195801Smavstatic int
1667199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag)
1668195801Smav{
1669199821Smav	struct siis_channel *ch = device_get_softc(dev);
1670195801Smav	u_int8_t *fis = &ctp->fis[0];
1671195801Smav
1672195801Smav	bzero(fis, 24);
1673195801Smav	fis[0] = 0x27;  		/* host to device */
1674195801Smav	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1675195801Smav	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1676195801Smav		fis[1] |= 0x80;
1677195801Smav		fis[2] = ATA_PACKET_CMD;
1678199821Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1679199821Smav		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1680195801Smav			fis[3] = ATA_F_DMA;
1681195801Smav		else {
1682195801Smav			fis[5] = ccb->csio.dxfer_len;
1683195801Smav		        fis[6] = ccb->csio.dxfer_len >> 8;
1684195801Smav		}
1685195801Smav		fis[7] = ATA_D_LBA;
1686195801Smav		fis[15] = ATA_A_4BIT;
1687195801Smav		bzero(ctp->u.atapi.ccb, 16);
1688195801Smav		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1689195801Smav		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1690195801Smav		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1691195801Smav	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1692195801Smav		fis[1] |= 0x80;
1693195801Smav		fis[2] = ccb->ataio.cmd.command;
1694195801Smav		fis[3] = ccb->ataio.cmd.features;
1695195801Smav		fis[4] = ccb->ataio.cmd.lba_low;
1696195801Smav		fis[5] = ccb->ataio.cmd.lba_mid;
1697195801Smav		fis[6] = ccb->ataio.cmd.lba_high;
1698195801Smav		fis[7] = ccb->ataio.cmd.device;
1699195801Smav		fis[8] = ccb->ataio.cmd.lba_low_exp;
1700195801Smav		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1701195801Smav		fis[10] = ccb->ataio.cmd.lba_high_exp;
1702195801Smav		fis[11] = ccb->ataio.cmd.features_exp;
1703195801Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1704195801Smav			fis[12] = tag << 3;
1705195801Smav			fis[13] = 0;
1706195801Smav		} else {
1707195801Smav			fis[12] = ccb->ataio.cmd.sector_count;
1708195801Smav			fis[13] = ccb->ataio.cmd.sector_count_exp;
1709195801Smav		}
1710195801Smav		fis[15] = ATA_A_4BIT;
1711195801Smav	} else {
1712195801Smav		/* Soft reset. */
1713195801Smav	}
1714195801Smav	return (20);
1715195801Smav}
1716195801Smav
1717195801Smavstatic int
1718195801Smavsiis_sata_connect(struct siis_channel *ch)
1719195801Smav{
1720195801Smav	u_int32_t status;
1721195801Smav	int timeout;
1722195801Smav
1723195801Smav	/* Wait up to 100ms for "connect well" */
1724195801Smav	for (timeout = 0; timeout < 100 ; timeout++) {
1725195801Smav		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1726195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1727195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1728195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1729195801Smav			break;
1730195801Smav		DELAY(1000);
1731195801Smav	}
1732195801Smav	if (timeout >= 100) {
1733195801Smav		if (bootverbose) {
1734195801Smav			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1735195801Smav			    status);
1736195801Smav		}
1737195801Smav		return (0);
1738195801Smav	}
1739195801Smav	if (bootverbose) {
1740195801Smav		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1741195801Smav		    timeout, status);
1742195801Smav	}
1743195801Smav	/* Clear SATA error register */
1744195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1745195801Smav	return (1);
1746195801Smav}
1747195801Smav
1748207431Smavstatic int
1749207431Smavsiis_check_ids(device_t dev, union ccb *ccb)
1750207431Smav{
1751207431Smav
1752207431Smav	if (ccb->ccb_h.target_id > 15) {
1753207431Smav		ccb->ccb_h.status = CAM_TID_INVALID;
1754207431Smav		xpt_done(ccb);
1755207431Smav		return (-1);
1756207431Smav	}
1757207431Smav	if (ccb->ccb_h.target_lun != 0) {
1758207431Smav		ccb->ccb_h.status = CAM_LUN_INVALID;
1759207431Smav		xpt_done(ccb);
1760207431Smav		return (-1);
1761207431Smav	}
1762207431Smav	return (0);
1763207431Smav}
1764207431Smav
1765195801Smavstatic void
1766195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb)
1767195801Smav{
1768210471Smav	device_t dev, parent;
1769195801Smav	struct siis_channel *ch;
1770195801Smav
1771195801Smav	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1772195801Smav	    ccb->ccb_h.func_code));
1773195801Smav
1774195801Smav	ch = (struct siis_channel *)cam_sim_softc(sim);
1775195801Smav	dev = ch->dev;
1776195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1777195801Smav	switch (ccb->ccb_h.func_code) {
1778195801Smav	/* Common cases first */
1779195801Smav	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1780195801Smav	case XPT_SCSI_IO:
1781207431Smav		if (siis_check_ids(dev, ccb))
1782207431Smav			return;
1783207431Smav		if (ch->devices == 0 ||
1784207431Smav		    (ch->pm_present == 0 &&
1785207431Smav		     ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
1786195801Smav			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1787195801Smav			break;
1788195801Smav		}
1789220566Smav		ccb->ccb_h.recovery_type = RECOVERY_NONE;
1790195801Smav		/* Check for command collision. */
1791195801Smav		if (siis_check_collision(dev, ccb)) {
1792195801Smav			/* Freeze command. */
1793195801Smav			ch->frozen = ccb;
1794195801Smav			/* We have only one frozen slot, so freeze simq also. */
1795195801Smav			xpt_freeze_simq(ch->sim, 1);
1796195801Smav			return;
1797195801Smav		}
1798195801Smav		siis_begin_transaction(dev, ccb);
1799207431Smav		return;
1800195801Smav	case XPT_EN_LUN:		/* Enable LUN as a target */
1801195801Smav	case XPT_TARGET_IO:		/* Execute target I/O request */
1802195801Smav	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1803195801Smav	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1804195801Smav	case XPT_ABORT:			/* Abort the specified CCB */
1805195801Smav		/* XXX Implement */
1806195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1807195801Smav		break;
1808195801Smav	case XPT_SET_TRAN_SETTINGS:
1809195801Smav	{
1810195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1811199747Smav		struct	siis_device *d;
1812195801Smav
1813207431Smav		if (siis_check_ids(dev, ccb))
1814207431Smav			return;
1815199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1816199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1817199747Smav		else
1818199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1819199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1820199747Smav			d->revision = cts->xport_specific.sata.revision;
1821199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1822199747Smav			d->mode = cts->xport_specific.sata.mode;
1823199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1824199747Smav			d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1825199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1826199747Smav			d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags);
1827195801Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1828198852Smav			ch->pm_present = cts->xport_specific.sata.pm_present;
1829198852Smav			if (ch->pm_present)
1830195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1831195801Smav			else
1832195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1833195801Smav		}
1834203376Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1835203376Smav			d->atapi = cts->xport_specific.sata.atapi;
1836207499Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1837207499Smav			d->caps = cts->xport_specific.sata.caps;
1838195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1839195801Smav		break;
1840195801Smav	}
1841195801Smav	case XPT_GET_TRAN_SETTINGS:
1842195801Smav	/* Get default/user set transfer settings for the target */
1843195801Smav	{
1844195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1845199747Smav		struct  siis_device *d;
1846195801Smav		uint32_t status;
1847195801Smav
1848207431Smav		if (siis_check_ids(dev, ccb))
1849207431Smav			return;
1850199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1851199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1852199747Smav		else
1853199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1854195801Smav		cts->protocol = PROTO_ATA;
1855196655Smav		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1856195801Smav		cts->transport = XPORT_SATA;
1857196655Smav		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1858195801Smav		cts->proto_specific.valid = 0;
1859195801Smav		cts->xport_specific.sata.valid = 0;
1860199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1861199747Smav		    (ccb->ccb_h.target_id == 15 ||
1862199747Smav		    (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
1863195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1864199747Smav			if (status & 0x0f0) {
1865199747Smav				cts->xport_specific.sata.revision =
1866199747Smav				    (status & 0x0f0) >> 4;
1867199747Smav				cts->xport_specific.sata.valid |=
1868199747Smav				    CTS_SATA_VALID_REVISION;
1869199747Smav			}
1870207499Smav			cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
1871207499Smav			if (ch->pm_level)
1872207499Smav				cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
1873220602Smav			cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
1874207499Smav			cts->xport_specific.sata.caps &=
1875207499Smav			    ch->user[ccb->ccb_h.target_id].caps;
1876207499Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1877199747Smav		} else {
1878199747Smav			cts->xport_specific.sata.revision = d->revision;
1879199747Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1880207499Smav			cts->xport_specific.sata.caps = d->caps;
1881220602Smav			if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1882220602Smav			    (ch->quirks & SIIS_Q_SNTF) == 0)
1883220602Smav				cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN;
1884207499Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1885195801Smav		}
1886199747Smav		cts->xport_specific.sata.mode = d->mode;
1887199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1888199747Smav		cts->xport_specific.sata.bytecount = d->bytecount;
1889199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1890198852Smav		cts->xport_specific.sata.pm_present = ch->pm_present;
1891195801Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1892199747Smav		cts->xport_specific.sata.tags = d->tags;
1893199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
1894203376Smav		cts->xport_specific.sata.atapi = d->atapi;
1895203376Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1896195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1897195801Smav		break;
1898195801Smav	}
1899195801Smav	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1900195801Smav	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1901195801Smav		siis_reset(dev);
1902195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1903195801Smav		break;
1904195801Smav	case XPT_TERM_IO:		/* Terminate the I/O process */
1905195801Smav		/* XXX Implement */
1906195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1907195801Smav		break;
1908195801Smav	case XPT_PATH_INQ:		/* Path routing inquiry */
1909195801Smav	{
1910195801Smav		struct ccb_pathinq *cpi = &ccb->cpi;
1911195801Smav
1912210471Smav		parent = device_get_parent(dev);
1913195801Smav		cpi->version_num = 1; /* XXX??? */
1914195801Smav		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1915195801Smav		cpi->hba_inquiry |= PI_SATAPM;
1916195801Smav		cpi->target_sprt = 0;
1917195801Smav		cpi->hba_misc = PIM_SEQSCAN;
1918195801Smav		cpi->hba_eng_cnt = 0;
1919198322Smav		cpi->max_target = 15;
1920195801Smav		cpi->max_lun = 0;
1921195801Smav		cpi->initiator_id = 0;
1922195801Smav		cpi->bus_id = cam_sim_bus(sim);
1923195801Smav		cpi->base_transfer_speed = 150000;
1924195801Smav		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1925195801Smav		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1926195801Smav		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1927195801Smav		cpi->unit_number = cam_sim_unit(sim);
1928195801Smav		cpi->transport = XPORT_SATA;
1929196655Smav		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1930195801Smav		cpi->protocol = PROTO_ATA;
1931196655Smav		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1932210471Smav		cpi->maxio = MAXPHYS;
1933210471Smav		cpi->hba_vendor = pci_get_vendor(parent);
1934210471Smav		cpi->hba_device = pci_get_device(parent);
1935210471Smav		cpi->hba_subvendor = pci_get_subvendor(parent);
1936210471Smav		cpi->hba_subdevice = pci_get_subdevice(parent);
1937195801Smav		cpi->ccb_h.status = CAM_REQ_CMP;
1938195801Smav		break;
1939195801Smav	}
1940195801Smav	default:
1941195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1942195801Smav		break;
1943195801Smav	}
1944207431Smav	xpt_done(ccb);
1945195801Smav}
1946195801Smav
1947195801Smavstatic void
1948195801Smavsiispoll(struct cam_sim *sim)
1949195801Smav{
1950195801Smav	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1951195801Smav
1952195801Smav	siis_ch_intr(ch->dev);
1953195801Smav}
1954