siis.c revision 214988
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 214988 2010-11-08 15:36:15Z mav $");
29195801Smav
30195801Smav#include <sys/param.h>
31195801Smav#include <sys/module.h>
32195801Smav#include <sys/systm.h>
33195801Smav#include <sys/kernel.h>
34195801Smav#include <sys/ata.h>
35195801Smav#include <sys/bus.h>
36195801Smav#include <sys/endian.h>
37195801Smav#include <sys/malloc.h>
38195801Smav#include <sys/lock.h>
39195801Smav#include <sys/mutex.h>
40195801Smav#include <sys/sema.h>
41195801Smav#include <sys/taskqueue.h>
42195801Smav#include <vm/uma.h>
43195801Smav#include <machine/stdarg.h>
44195801Smav#include <machine/resource.h>
45195801Smav#include <machine/bus.h>
46195801Smav#include <sys/rman.h>
47195801Smav#include <dev/pci/pcivar.h>
48195801Smav#include <dev/pci/pcireg.h>
49195801Smav#include "siis.h"
50195801Smav
51195801Smav#include <cam/cam.h>
52195801Smav#include <cam/cam_ccb.h>
53195801Smav#include <cam/cam_sim.h>
54195801Smav#include <cam/cam_xpt_sim.h>
55195801Smav#include <cam/cam_debug.h>
56195801Smav
57195801Smav/* local prototypes */
58195801Smavstatic int siis_setup_interrupt(device_t dev);
59195801Smavstatic void siis_intr(void *data);
60195801Smavstatic int siis_suspend(device_t dev);
61195801Smavstatic int siis_resume(device_t dev);
62208393Smavstatic int siis_ch_init(device_t dev);
63208393Smavstatic int siis_ch_deinit(device_t dev);
64195801Smavstatic int siis_ch_suspend(device_t dev);
65195801Smavstatic int siis_ch_resume(device_t dev);
66195801Smavstatic void siis_ch_intr_locked(void *data);
67195801Smavstatic void siis_ch_intr(void *data);
68195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb);
69195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
70195801Smavstatic void siis_execute_transaction(struct siis_slot *slot);
71195801Smavstatic void siis_timeout(struct siis_slot *slot);
72195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
73199821Smavstatic int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag);
74195801Smavstatic void siis_dmainit(device_t dev);
75195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
76195801Smavstatic void siis_dmafini(device_t dev);
77195801Smavstatic void siis_slotsalloc(device_t dev);
78195801Smavstatic void siis_slotsfree(device_t dev);
79195801Smavstatic void siis_reset(device_t dev);
80195801Smavstatic void siis_portinit(device_t dev);
81195801Smavstatic int siis_wait_ready(device_t dev, int t);
82195801Smav
83195801Smavstatic int siis_sata_connect(struct siis_channel *ch);
84195801Smav
85195801Smavstatic void siis_issue_read_log(device_t dev);
86195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb);
87195801Smav
88195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb);
89195801Smavstatic void siispoll(struct cam_sim *sim);
90195801Smav
91195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
92195801Smav
93199132Smavstatic struct {
94199132Smav	uint32_t	id;
95199132Smav	const char	*name;
96199132Smav	int		ports;
97200217Smav	int		quirks;
98200217Smav#define SIIS_Q_SNTF	1
99205358Smav#define SIIS_Q_NOMSI	2
100199132Smav} siis_ids[] = {
101200217Smav	{0x31241095,	"SiI3124",	4,	0},
102200217Smav	{0x31248086,	"SiI3124",	4,	0},
103205358Smav	{0x31321095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
104205358Smav	{0x02421095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
105205358Smav	{0x02441095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
106205358Smav	{0x31311095,	"SiI3131",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
107205358Smav	{0x35311095,	"SiI3531",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
108200217Smav	{0,		NULL,		0,	0}
109199132Smav};
110199132Smav
111195801Smavstatic int
112195801Smavsiis_probe(device_t dev)
113195801Smav{
114199132Smav	char buf[64];
115199132Smav	int i;
116195801Smav	uint32_t devid = pci_get_devid(dev);
117195801Smav
118199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
119199132Smav		if (siis_ids[i].id == devid) {
120200217Smav			snprintf(buf, sizeof(buf), "%s SATA controller",
121199132Smav			    siis_ids[i].name);
122199132Smav			device_set_desc_copy(dev, buf);
123199132Smav			return (BUS_PROBE_VENDOR);
124199132Smav		}
125195801Smav	}
126199132Smav	return (ENXIO);
127195801Smav}
128195801Smav
129195801Smavstatic int
130195801Smavsiis_attach(device_t dev)
131195801Smav{
132195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
133195801Smav	uint32_t devid = pci_get_devid(dev);
134195801Smav	device_t child;
135199132Smav	int	error, i, unit;
136195801Smav
137200217Smav	ctlr->dev = dev;
138199132Smav	for (i = 0; siis_ids[i].id != 0; i++) {
139199132Smav		if (siis_ids[i].id == devid)
140199132Smav			break;
141199132Smav	}
142200217Smav	ctlr->quirks = siis_ids[i].quirks;
143195801Smav	/* Global memory */
144195801Smav	ctlr->r_grid = PCIR_BAR(0);
145195801Smav	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
146195801Smav	    &ctlr->r_grid, RF_ACTIVE)))
147195801Smav		return (ENXIO);
148200223Smav	ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL);
149195801Smav	/* Channels memory */
150195801Smav	ctlr->r_rid = PCIR_BAR(2);
151195801Smav	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
152195801Smav	    &ctlr->r_rid, RF_ACTIVE)))
153195801Smav		return (ENXIO);
154195801Smav	/* Setup our own memory management for channels. */
155208414Smav	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
156208414Smav	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
157195801Smav	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
158195801Smav	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
159195801Smav	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
160195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
161195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
162195801Smav		return (error);
163195801Smav	}
164195801Smav	if ((error = rman_manage_region(&ctlr->sc_iomem,
165195801Smav	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
166195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
167195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
168195801Smav		rman_fini(&ctlr->sc_iomem);
169195801Smav		return (error);
170195801Smav	}
171206652Smav	pci_enable_busmaster(dev);
172195801Smav	/* Reset controller */
173195801Smav	siis_resume(dev);
174195801Smav	/* Number of HW channels */
175199132Smav	ctlr->channels = siis_ids[i].ports;
176195801Smav	/* Setup interrupts. */
177195801Smav	if (siis_setup_interrupt(dev)) {
178195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
179195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
180195801Smav		rman_fini(&ctlr->sc_iomem);
181195801Smav		return ENXIO;
182195801Smav	}
183195801Smav	/* Attach all channels on this controller */
184195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
185195801Smav		child = device_add_child(dev, "siisch", -1);
186195801Smav		if (child == NULL)
187195801Smav			device_printf(dev, "failed to add channel device\n");
188195801Smav		else
189195801Smav			device_set_ivars(child, (void *)(intptr_t)unit);
190195801Smav	}
191195801Smav	bus_generic_attach(dev);
192195801Smav	return 0;
193195801Smav}
194195801Smav
195195801Smavstatic int
196195801Smavsiis_detach(device_t dev)
197195801Smav{
198195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
199195801Smav	device_t *children;
200195801Smav	int nchildren, i;
201195801Smav
202195801Smav	/* Detach & delete all children */
203195801Smav	if (!device_get_children(dev, &children, &nchildren)) {
204195801Smav		for (i = 0; i < nchildren; i++)
205195801Smav			device_delete_child(dev, children[i]);
206195801Smav		free(children, M_TEMP);
207195801Smav	}
208195801Smav	/* Free interrupts. */
209195801Smav	if (ctlr->irq.r_irq) {
210195801Smav		bus_teardown_intr(dev, ctlr->irq.r_irq,
211195801Smav		    ctlr->irq.handle);
212195801Smav		bus_release_resource(dev, SYS_RES_IRQ,
213195801Smav		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
214195801Smav	}
215195801Smav	pci_release_msi(dev);
216195801Smav	/* Free memory. */
217195801Smav	rman_fini(&ctlr->sc_iomem);
218195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
219195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
220195801Smav	return (0);
221195801Smav}
222195801Smav
223195801Smavstatic int
224195801Smavsiis_suspend(device_t dev)
225195801Smav{
226195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
227195801Smav
228195801Smav	bus_generic_suspend(dev);
229195801Smav	/* Put controller into reset state. */
230200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
231200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
232195801Smav	return 0;
233195801Smav}
234195801Smav
235195801Smavstatic int
236195801Smavsiis_resume(device_t dev)
237195801Smav{
238195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
239195801Smav
240200291Smav	/* Set PCIe max read request size to at least 1024 bytes */
241203529Smav	if (pci_get_max_read_req(dev) < 1024)
242203529Smav		pci_set_max_read_req(dev, 1024);
243195801Smav	/* Put controller into reset state. */
244200223Smav	ctlr->gctl |= SIIS_GCTL_GRESET;
245200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
246195801Smav	DELAY(10000);
247195801Smav	/* Get controller out of reset state and enable port interrupts. */
248200223Smav	ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE);
249200223Smav	ctlr->gctl |= 0x0000000f;
250200223Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
251195801Smav	return (bus_generic_resume(dev));
252195801Smav}
253195801Smav
254195801Smavstatic int
255195801Smavsiis_setup_interrupt(device_t dev)
256195801Smav{
257195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
258205358Smav	int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1;
259195801Smav
260195801Smav	/* Process hints. */
261195801Smav	resource_int_value(device_get_name(dev),
262195801Smav	    device_get_unit(dev), "msi", &msi);
263195801Smav	if (msi < 0)
264195801Smav		msi = 0;
265195801Smav	else if (msi > 0)
266195801Smav		msi = min(1, pci_msi_count(dev));
267195801Smav	/* Allocate MSI if needed/present. */
268195801Smav	if (msi && pci_alloc_msi(dev, &msi) != 0)
269195801Smav		msi = 0;
270195801Smav	/* Allocate all IRQs. */
271195801Smav	ctlr->irq.r_irq_rid = msi ? 1 : 0;
272195801Smav	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
273195801Smav	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
274195801Smav		device_printf(dev, "unable to map interrupt\n");
275195801Smav		return ENXIO;
276195801Smav	}
277195801Smav	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
278195801Smav	    siis_intr, ctlr, &ctlr->irq.handle))) {
279195801Smav		/* SOS XXX release r_irq */
280195801Smav		device_printf(dev, "unable to setup interrupt\n");
281195801Smav		return ENXIO;
282195801Smav	}
283195801Smav	return (0);
284195801Smav}
285195801Smav
286195801Smav/*
287195801Smav * Common case interrupt handler.
288195801Smav */
289195801Smavstatic void
290195801Smavsiis_intr(void *data)
291195801Smav{
292195801Smav	struct siis_controller *ctlr = (struct siis_controller *)data;
293195801Smav	u_int32_t is;
294195801Smav	void *arg;
295195801Smav	int unit;
296195801Smav
297195801Smav	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
298195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
299195801Smav		if ((is & SIIS_IS_PORT(unit)) != 0 &&
300195801Smav		    (arg = ctlr->interrupt[unit].argument)) {
301195801Smav			ctlr->interrupt[unit].function(arg);
302195801Smav		}
303195801Smav	}
304200223Smav	/* Acknowledge interrupt, if MSI enabled. */
305200223Smav	if (ctlr->irq.r_irq_rid) {
306200223Smav		ATA_OUTL(ctlr->r_gmem, SIIS_GCTL,
307200223Smav		    ctlr->gctl | SIIS_GCTL_MSIACK);
308200223Smav	}
309195801Smav}
310195801Smav
311195801Smavstatic struct resource *
312195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid,
313195801Smav		       u_long start, u_long end, u_long count, u_int flags)
314195801Smav{
315195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
316195801Smav	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
317195801Smav	struct resource *res = NULL;
318195801Smav	int offset = unit << 13;
319195801Smav	long st;
320195801Smav
321195801Smav	switch (type) {
322195801Smav	case SYS_RES_MEMORY:
323195801Smav		st = rman_get_start(ctlr->r_mem);
324195801Smav		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
325195801Smav		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
326195801Smav		if (res) {
327195801Smav			bus_space_handle_t bsh;
328195801Smav			bus_space_tag_t bst;
329195801Smav			bsh = rman_get_bushandle(ctlr->r_mem);
330195801Smav			bst = rman_get_bustag(ctlr->r_mem);
331195801Smav			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
332195801Smav			rman_set_bushandle(res, bsh);
333195801Smav			rman_set_bustag(res, bst);
334195801Smav		}
335195801Smav		break;
336195801Smav	case SYS_RES_IRQ:
337195801Smav		if (*rid == ATA_IRQ_RID)
338195801Smav			res = ctlr->irq.r_irq;
339195801Smav		break;
340195801Smav	}
341195801Smav	return (res);
342195801Smav}
343195801Smav
344195801Smavstatic int
345195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid,
346195801Smav			 struct resource *r)
347195801Smav{
348195801Smav
349195801Smav	switch (type) {
350195801Smav	case SYS_RES_MEMORY:
351195801Smav		rman_release_resource(r);
352195801Smav		return (0);
353195801Smav	case SYS_RES_IRQ:
354195801Smav		if (rid != ATA_IRQ_RID)
355195801Smav			return ENOENT;
356195801Smav		return (0);
357195801Smav	}
358195801Smav	return (EINVAL);
359195801Smav}
360195801Smav
361195801Smavstatic int
362195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq,
363195801Smav		   int flags, driver_filter_t *filter, driver_intr_t *function,
364195801Smav		   void *argument, void **cookiep)
365195801Smav{
366195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
367195801Smav	int unit = (intptr_t)device_get_ivars(child);
368195801Smav
369195801Smav	if (filter != NULL) {
370195801Smav		printf("siis.c: we cannot use a filter here\n");
371195801Smav		return (EINVAL);
372195801Smav	}
373195801Smav	ctlr->interrupt[unit].function = function;
374195801Smav	ctlr->interrupt[unit].argument = argument;
375195801Smav	return (0);
376195801Smav}
377195801Smav
378195801Smavstatic int
379195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq,
380195801Smav		      void *cookie)
381195801Smav{
382195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
383195801Smav	int unit = (intptr_t)device_get_ivars(child);
384195801Smav
385195801Smav	ctlr->interrupt[unit].function = NULL;
386195801Smav	ctlr->interrupt[unit].argument = NULL;
387195801Smav	return (0);
388195801Smav}
389195801Smav
390195801Smavstatic int
391195801Smavsiis_print_child(device_t dev, device_t child)
392195801Smav{
393195801Smav	int retval;
394195801Smav
395195801Smav	retval = bus_print_child_header(dev, child);
396195801Smav	retval += printf(" at channel %d",
397195801Smav	    (int)(intptr_t)device_get_ivars(child));
398195801Smav	retval += bus_print_child_footer(dev, child);
399195801Smav
400195801Smav	return (retval);
401195801Smav}
402195801Smav
403208410Smavstatic int
404208410Smavsiis_child_location_str(device_t dev, device_t child, char *buf,
405208410Smav    size_t buflen)
406208410Smav{
407208410Smav
408208410Smav	snprintf(buf, buflen, "channel=%d",
409208410Smav	    (int)(intptr_t)device_get_ivars(child));
410208410Smav	return (0);
411208410Smav}
412208410Smav
413195801Smavdevclass_t siis_devclass;
414195801Smavstatic device_method_t siis_methods[] = {
415195801Smav	DEVMETHOD(device_probe,     siis_probe),
416195801Smav	DEVMETHOD(device_attach,    siis_attach),
417195801Smav	DEVMETHOD(device_detach,    siis_detach),
418195801Smav	DEVMETHOD(device_suspend,   siis_suspend),
419195801Smav	DEVMETHOD(device_resume,    siis_resume),
420195801Smav	DEVMETHOD(bus_print_child,  siis_print_child),
421195801Smav	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
422195801Smav	DEVMETHOD(bus_release_resource,     siis_release_resource),
423195801Smav	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
424195801Smav	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
425208410Smav	DEVMETHOD(bus_child_location_str, siis_child_location_str),
426195801Smav	{ 0, 0 }
427195801Smav};
428195801Smavstatic driver_t siis_driver = {
429195801Smav        "siis",
430195801Smav        siis_methods,
431195801Smav        sizeof(struct siis_controller)
432195801Smav};
433195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
434195801SmavMODULE_VERSION(siis, 1);
435195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1);
436195801Smav
437195801Smavstatic int
438195801Smavsiis_ch_probe(device_t dev)
439195801Smav{
440195801Smav
441195801Smav	device_set_desc_copy(dev, "SIIS channel");
442195801Smav	return (0);
443195801Smav}
444195801Smav
445195801Smavstatic int
446195801Smavsiis_ch_attach(device_t dev)
447195801Smav{
448200217Smav	struct siis_controller *ctlr = device_get_softc(device_get_parent(dev));
449195801Smav	struct siis_channel *ch = device_get_softc(dev);
450195801Smav	struct cam_devq *devq;
451199821Smav	int rid, error, i, sata_rev = 0;
452195801Smav
453195801Smav	ch->dev = dev;
454195801Smav	ch->unit = (intptr_t)device_get_ivars(dev);
455200217Smav	ch->quirks = ctlr->quirks;
456195801Smav	resource_int_value(device_get_name(dev),
457195801Smav	    device_get_unit(dev), "pm_level", &ch->pm_level);
458199821Smav	resource_int_value(device_get_name(dev),
459199821Smav	    device_get_unit(dev), "sata_rev", &sata_rev);
460199747Smav	for (i = 0; i < 16; i++) {
461199821Smav		ch->user[i].revision = sata_rev;
462199747Smav		ch->user[i].mode = 0;
463199747Smav		ch->user[i].bytecount = 8192;
464199747Smav		ch->user[i].tags = SIIS_MAX_SLOTS;
465199747Smav		ch->curr[i] = ch->user[i];
466207499Smav		if (ch->pm_level)
467207499Smav			ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ;
468199747Smav	}
469195801Smav	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
470195801Smav	rid = ch->unit;
471195801Smav	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
472195801Smav	    &rid, RF_ACTIVE)))
473195801Smav		return (ENXIO);
474195801Smav	siis_dmainit(dev);
475195801Smav	siis_slotsalloc(dev);
476208393Smav	siis_ch_init(dev);
477195801Smav	mtx_lock(&ch->mtx);
478195801Smav	rid = ATA_IRQ_RID;
479195801Smav	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
480195801Smav	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
481195801Smav		device_printf(dev, "Unable to map interrupt\n");
482208816Smav		error = ENXIO;
483208816Smav		goto err0;
484195801Smav	}
485195801Smav	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
486195801Smav	    siis_ch_intr_locked, dev, &ch->ih))) {
487195801Smav		device_printf(dev, "Unable to setup interrupt\n");
488195801Smav		error = ENXIO;
489195801Smav		goto err1;
490195801Smav	}
491195801Smav	/* Create the device queue for our SIM. */
492195801Smav	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
493195801Smav	if (devq == NULL) {
494195801Smav		device_printf(dev, "Unable to allocate simq\n");
495195801Smav		error = ENOMEM;
496195801Smav		goto err1;
497195801Smav	}
498195801Smav	/* Construct SIM entry */
499195801Smav	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
500199178Smav	    device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq);
501195801Smav	if (ch->sim == NULL) {
502208816Smav		cam_simq_free(devq);
503195801Smav		device_printf(dev, "unable to allocate sim\n");
504195801Smav		error = ENOMEM;
505208816Smav		goto err1;
506195801Smav	}
507195801Smav	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
508195801Smav		device_printf(dev, "unable to register xpt bus\n");
509195801Smav		error = ENXIO;
510195801Smav		goto err2;
511195801Smav	}
512195801Smav	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
513195801Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
514195801Smav		device_printf(dev, "unable to create path\n");
515195801Smav		error = ENXIO;
516195801Smav		goto err3;
517195801Smav	}
518195801Smav	mtx_unlock(&ch->mtx);
519195801Smav	return (0);
520195801Smav
521195801Smaverr3:
522195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
523195801Smaverr2:
524195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
525195801Smaverr1:
526195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
527208816Smaverr0:
528195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
529195801Smav	mtx_unlock(&ch->mtx);
530214325Smav	mtx_destroy(&ch->mtx);
531195801Smav	return (error);
532195801Smav}
533195801Smav
534195801Smavstatic int
535195801Smavsiis_ch_detach(device_t dev)
536195801Smav{
537195801Smav	struct siis_channel *ch = device_get_softc(dev);
538195801Smav
539195801Smav	mtx_lock(&ch->mtx);
540195801Smav	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
541195801Smav	xpt_free_path(ch->path);
542195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
543195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
544195801Smav	mtx_unlock(&ch->mtx);
545195801Smav
546195801Smav	bus_teardown_intr(dev, ch->r_irq, ch->ih);
547195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
548195801Smav
549208393Smav	siis_ch_deinit(dev);
550195801Smav	siis_slotsfree(dev);
551195801Smav	siis_dmafini(dev);
552195801Smav
553195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
554195801Smav	mtx_destroy(&ch->mtx);
555195801Smav	return (0);
556195801Smav}
557195801Smav
558195801Smavstatic int
559208393Smavsiis_ch_init(device_t dev)
560195801Smav{
561195801Smav	struct siis_channel *ch = device_get_softc(dev);
562195801Smav
563208393Smav	/* Get port out of reset state. */
564208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
565208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
566208393Smav	if (ch->pm_present)
567208393Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
568208393Smav	else
569208393Smav		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
570208393Smav	/* Enable port interrupts */
571208393Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
572208393Smav	return (0);
573208393Smav}
574208393Smav
575208393Smavstatic int
576208393Smavsiis_ch_deinit(device_t dev)
577208393Smav{
578208393Smav	struct siis_channel *ch = device_get_softc(dev);
579208393Smav
580195801Smav	/* Put port into reset state. */
581195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
582195801Smav	return (0);
583195801Smav}
584195801Smav
585195801Smavstatic int
586208393Smavsiis_ch_suspend(device_t dev)
587208393Smav{
588208393Smav	struct siis_channel *ch = device_get_softc(dev);
589208393Smav
590208393Smav	mtx_lock(&ch->mtx);
591208393Smav	xpt_freeze_simq(ch->sim, 1);
592208393Smav	while (ch->oslots)
593208393Smav		msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100);
594208393Smav	siis_ch_deinit(dev);
595208393Smav	mtx_unlock(&ch->mtx);
596208393Smav	return (0);
597208393Smav}
598208393Smav
599208393Smavstatic int
600195801Smavsiis_ch_resume(device_t dev)
601195801Smav{
602195801Smav	struct siis_channel *ch = device_get_softc(dev);
603195801Smav
604208393Smav	mtx_lock(&ch->mtx);
605208393Smav	siis_ch_init(dev);
606208393Smav	siis_reset(dev);
607208393Smav	xpt_release_simq(ch->sim, TRUE);
608208393Smav	mtx_unlock(&ch->mtx);
609195801Smav	return (0);
610195801Smav}
611195801Smav
612195801Smavdevclass_t siisch_devclass;
613195801Smavstatic device_method_t siisch_methods[] = {
614195801Smav	DEVMETHOD(device_probe,     siis_ch_probe),
615195801Smav	DEVMETHOD(device_attach,    siis_ch_attach),
616195801Smav	DEVMETHOD(device_detach,    siis_ch_detach),
617195801Smav	DEVMETHOD(device_suspend,   siis_ch_suspend),
618195801Smav	DEVMETHOD(device_resume,    siis_ch_resume),
619195801Smav	{ 0, 0 }
620195801Smav};
621195801Smavstatic driver_t siisch_driver = {
622195801Smav        "siisch",
623195801Smav        siisch_methods,
624195801Smav        sizeof(struct siis_channel)
625195801Smav};
626195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
627195801Smav
628195801Smavstruct siis_dc_cb_args {
629195801Smav	bus_addr_t maddr;
630195801Smav	int error;
631195801Smav};
632195801Smav
633195801Smavstatic void
634195801Smavsiis_dmainit(device_t dev)
635195801Smav{
636195801Smav	struct siis_channel *ch = device_get_softc(dev);
637195801Smav	struct siis_dc_cb_args dcba;
638195801Smav
639195801Smav	/* Command area. */
640195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
641195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
642195801Smav	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
643195801Smav	    0, NULL, NULL, &ch->dma.work_tag))
644195801Smav		goto error;
645195801Smav	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
646195801Smav	    &ch->dma.work_map))
647195801Smav		goto error;
648195801Smav	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
649195801Smav	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
650195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
651195801Smav		goto error;
652195801Smav	}
653195801Smav	ch->dma.work_bus = dcba.maddr;
654195801Smav	/* Data area. */
655199333Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
656195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
657195801Smav	    NULL, NULL,
658195801Smav	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
659195801Smav	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
660195801Smav	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
661195801Smav		goto error;
662195801Smav	}
663195801Smav	return;
664195801Smav
665195801Smaverror:
666195801Smav	device_printf(dev, "WARNING - DMA initialization failed\n");
667195801Smav	siis_dmafini(dev);
668195801Smav}
669195801Smav
670195801Smavstatic void
671195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
672195801Smav{
673195801Smav	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
674195801Smav
675195801Smav	if (!(dcba->error = error))
676195801Smav		dcba->maddr = segs[0].ds_addr;
677195801Smav}
678195801Smav
679195801Smavstatic void
680195801Smavsiis_dmafini(device_t dev)
681195801Smav{
682195801Smav	struct siis_channel *ch = device_get_softc(dev);
683195801Smav
684195801Smav	if (ch->dma.data_tag) {
685195801Smav		bus_dma_tag_destroy(ch->dma.data_tag);
686195801Smav		ch->dma.data_tag = NULL;
687195801Smav	}
688195801Smav	if (ch->dma.work_bus) {
689195801Smav		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
690195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
691195801Smav		ch->dma.work_bus = 0;
692195801Smav		ch->dma.work_map = NULL;
693195801Smav		ch->dma.work = NULL;
694195801Smav	}
695195801Smav	if (ch->dma.work_tag) {
696195801Smav		bus_dma_tag_destroy(ch->dma.work_tag);
697195801Smav		ch->dma.work_tag = NULL;
698195801Smav	}
699195801Smav}
700195801Smav
701195801Smavstatic void
702195801Smavsiis_slotsalloc(device_t dev)
703195801Smav{
704195801Smav	struct siis_channel *ch = device_get_softc(dev);
705195801Smav	int i;
706195801Smav
707195801Smav	/* Alloc and setup command/dma slots */
708195801Smav	bzero(ch->slot, sizeof(ch->slot));
709195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
710195801Smav		struct siis_slot *slot = &ch->slot[i];
711195801Smav
712195801Smav		slot->dev = dev;
713195801Smav		slot->slot = i;
714195801Smav		slot->state = SIIS_SLOT_EMPTY;
715195801Smav		slot->ccb = NULL;
716195801Smav		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
717195801Smav
718195801Smav		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
719195801Smav			device_printf(ch->dev, "FAILURE - create data_map\n");
720195801Smav	}
721195801Smav}
722195801Smav
723195801Smavstatic void
724195801Smavsiis_slotsfree(device_t dev)
725195801Smav{
726195801Smav	struct siis_channel *ch = device_get_softc(dev);
727195801Smav	int i;
728195801Smav
729195801Smav	/* Free all dma slots */
730195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
731195801Smav		struct siis_slot *slot = &ch->slot[i];
732195801Smav
733198896Smav		callout_drain(&slot->timeout);
734195801Smav		if (slot->dma.data_map) {
735195801Smav			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
736195801Smav			slot->dma.data_map = NULL;
737195801Smav		}
738195801Smav	}
739195801Smav}
740195801Smav
741195801Smavstatic void
742196655Smavsiis_notify_events(device_t dev)
743196655Smav{
744196655Smav	struct siis_channel *ch = device_get_softc(dev);
745196655Smav	struct cam_path *dpath;
746196655Smav	u_int32_t status;
747196655Smav	int i;
748196655Smav
749200217Smav	if (ch->quirks & SIIS_Q_SNTF) {
750200217Smav		status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
751200217Smav		ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
752200217Smav	} else {
753200217Smav		/*
754200217Smav		 * Without SNTF we have no idea which device sent notification.
755200217Smav		 * If PMP is connected, assume it, else - device.
756200217Smav		 */
757200217Smav		status = (ch->pm_present) ? 0x8000 : 0x0001;
758200217Smav	}
759196655Smav	if (bootverbose)
760196655Smav		device_printf(dev, "SNTF 0x%04x\n", status);
761196655Smav	for (i = 0; i < 16; i++) {
762196655Smav		if ((status & (1 << i)) == 0)
763196655Smav			continue;
764196655Smav		if (xpt_create_path(&dpath, NULL,
765196655Smav		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
766196655Smav			xpt_async(AC_SCSI_AEN, dpath, NULL);
767196655Smav			xpt_free_path(dpath);
768196655Smav		}
769196655Smav	}
770196655Smav
771196655Smav}
772196655Smav
773196655Smavstatic void
774195801Smavsiis_phy_check_events(device_t dev)
775195801Smav{
776195801Smav	struct siis_channel *ch = device_get_softc(dev);
777195801Smav
778195801Smav	/* If we have a connection event, deal with it */
779195801Smav	if (ch->pm_level == 0) {
780195801Smav		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
781203108Smav		union ccb *ccb;
782203108Smav
783203165Smav		if (bootverbose) {
784203165Smav			if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
785203165Smav			    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
786203165Smav			    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
787195801Smav				device_printf(dev, "CONNECT requested\n");
788203165Smav			} else
789195801Smav				device_printf(dev, "DISCONNECT requested\n");
790195801Smav		}
791203165Smav		siis_reset(dev);
792203108Smav		if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
793203108Smav			return;
794203108Smav		if (xpt_create_path(&ccb->ccb_h.path, NULL,
795203108Smav		    cam_sim_path(ch->sim),
796203108Smav		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
797203108Smav			xpt_free_ccb(ccb);
798203108Smav			return;
799203108Smav		}
800203108Smav		xpt_rescan(ccb);
801195801Smav	}
802195801Smav}
803195801Smav
804195801Smavstatic void
805195801Smavsiis_ch_intr_locked(void *data)
806195801Smav{
807195801Smav	device_t dev = (device_t)data;
808195801Smav	struct siis_channel *ch = device_get_softc(dev);
809195801Smav
810195801Smav	mtx_lock(&ch->mtx);
811195801Smav	siis_ch_intr(data);
812195801Smav	mtx_unlock(&ch->mtx);
813195801Smav}
814195801Smav
815195801Smavstatic void
816195801Smavsiis_ch_intr(void *data)
817195801Smav{
818195801Smav	device_t dev = (device_t)data;
819195801Smav	struct siis_channel *ch = device_get_softc(dev);
820195801Smav	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
821195801Smav	enum siis_err_type et;
822195801Smav	int i, ccs, port, tslots;
823195801Smav
824195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
825195801Smav	/* Read command statuses. */
826195801Smav	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
827195801Smav	ok = ch->rslots & ~sstatus;
828195801Smav	/* Complete all successfull commands. */
829195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
830195801Smav		if ((ok >> i) & 1)
831195801Smav			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
832195801Smav	}
833195801Smav	/* Do we have any other events? */
834195801Smav	if ((sstatus & SIIS_P_SS_ATTN) == 0)
835195801Smav		return;
836195801Smav	/* Read and clear interrupt statuses. */
837195801Smav	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
838195801Smav	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
839195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
840195801Smav	/* Process PHY events */
841195801Smav	if (istatus & SIIS_P_IX_PHYRDYCHG)
842195801Smav		siis_phy_check_events(dev);
843196655Smav	/* Process NOTIFY events */
844196655Smav	if (istatus & SIIS_P_IX_SDBN)
845196655Smav		siis_notify_events(dev);
846195801Smav	/* Process command errors */
847195801Smav	if (istatus & SIIS_P_IX_COMMERR) {
848195801Smav		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
849195801Smav		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
850195801Smav		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
851195801Smav		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
852195801Smav		err = ch->rslots & sstatus;
853195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
854195801Smav//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
855195801Smav//    ATA_INL(ch->r_mem, SIIS_P_SERR));
856195801Smav
857195801Smav		if (!ch->readlog && !ch->recovery) {
858195801Smav			xpt_freeze_simq(ch->sim, ch->numrslots);
859195801Smav			ch->recovery = 1;
860195801Smav		}
861195801Smav		if (ch->frozen) {
862195801Smav			union ccb *fccb = ch->frozen;
863195801Smav			ch->frozen = NULL;
864198321Smav			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
865198321Smav			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
866198321Smav			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
867198321Smav				xpt_freeze_devq(fccb->ccb_h.path, 1);
868198321Smav				fccb->ccb_h.status |= CAM_DEV_QFRZN;
869198321Smav			}
870195801Smav			xpt_done(fccb);
871195801Smav		}
872195801Smav		if (estatus == SIIS_P_CMDERR_DEV ||
873195801Smav		    estatus == SIIS_P_CMDERR_SDB ||
874195801Smav		    estatus == SIIS_P_CMDERR_DATAFIS) {
875195801Smav			tslots = ch->numtslots[port];
876195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
877198852Smav				/* XXX: requests in loading state. */
878195801Smav				if (((ch->rslots >> i) & 1) == 0)
879195801Smav					continue;
880195801Smav				if (ch->slot[i].ccb->ccb_h.target_id != port)
881195801Smav					continue;
882195801Smav				if (tslots == 0) {
883195801Smav					/* Untagged operation. */
884195801Smav					if (i == ccs)
885195801Smav						et = SIIS_ERR_TFE;
886195801Smav					else
887195801Smav						et = SIIS_ERR_INNOCENT;
888195801Smav				} else {
889195801Smav					/* Tagged operation. */
890195801Smav					et = SIIS_ERR_NCQ;
891195801Smav				}
892195801Smav				siis_end_transaction(&ch->slot[i], et);
893195801Smav			}
894195801Smav			/*
895195801Smav			 * We can't reinit port if there are some other
896195801Smav			 * commands active, use resume to complete them.
897195801Smav			 */
898195801Smav			if (ch->rslots != 0)
899195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
900195801Smav		} else {
901195801Smav			if (estatus == SIIS_P_CMDERR_SENDFIS ||
902195801Smav			    estatus == SIIS_P_CMDERR_INCSTATE ||
903195801Smav			    estatus == SIIS_P_CMDERR_PPE ||
904195801Smav			    estatus == SIIS_P_CMDERR_SERVICE) {
905195801Smav				et = SIIS_ERR_SATA;
906195801Smav			} else
907195801Smav				et = SIIS_ERR_INVALID;
908195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
909198852Smav				/* XXX: requests in loading state. */
910195801Smav				if (((ch->rslots >> i) & 1) == 0)
911195801Smav					continue;
912195801Smav				siis_end_transaction(&ch->slot[i], et);
913195801Smav			}
914195801Smav		}
915195801Smav	}
916195801Smav}
917195801Smav
918195801Smav/* Must be called with channel locked. */
919195801Smavstatic int
920195801Smavsiis_check_collision(device_t dev, union ccb *ccb)
921195801Smav{
922195801Smav	struct siis_channel *ch = device_get_softc(dev);
923195801Smav
924195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
925195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
926199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
927199747Smav		/* Tagged command while we have no supported tag free. */
928199747Smav		if (((~ch->oslots) & (0x7fffffff >> (31 -
929199747Smav		    ch->curr[ccb->ccb_h.target_id].tags))) == 0)
930199747Smav			return (1);
931199747Smav	}
932199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
933195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
934195801Smav		/* Atomic command while anything active. */
935195801Smav		if (ch->numrslots != 0)
936195801Smav			return (1);
937195801Smav	}
938195801Smav       /* We have some atomic command running. */
939195801Smav       if (ch->aslots != 0)
940195801Smav               return (1);
941195801Smav	return (0);
942195801Smav}
943195801Smav
944195801Smav/* Must be called with channel locked. */
945195801Smavstatic void
946195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb)
947195801Smav{
948195801Smav	struct siis_channel *ch = device_get_softc(dev);
949195801Smav	struct siis_slot *slot;
950199747Smav	int tag, tags;
951195801Smav
952195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
953195801Smav	/* Choose empty slot. */
954199747Smav	tags = SIIS_MAX_SLOTS;
955199747Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
956199747Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
957199747Smav		tags = ch->curr[ccb->ccb_h.target_id].tags;
958199747Smav	tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1;
959195801Smav	/* Occupy chosen slot. */
960195801Smav	slot = &ch->slot[tag];
961195801Smav	slot->ccb = ccb;
962195801Smav	/* Update channel stats. */
963199747Smav	ch->oslots |= (1 << slot->slot);
964195801Smav	ch->numrslots++;
965195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
966195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
967195801Smav		ch->numtslots[ccb->ccb_h.target_id]++;
968195801Smav	}
969195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
970195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
971195801Smav		ch->aslots |= (1 << slot->slot);
972195801Smav	slot->dma.nsegs = 0;
973195801Smav	/* If request moves data, setup and load SG list */
974195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
975195801Smav		void *buf;
976195801Smav		bus_size_t size;
977195801Smav
978195801Smav		slot->state = SIIS_SLOT_LOADING;
979195801Smav		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
980195801Smav			buf = ccb->ataio.data_ptr;
981195801Smav			size = ccb->ataio.dxfer_len;
982195801Smav		} else {
983195801Smav			buf = ccb->csio.data_ptr;
984195801Smav			size = ccb->csio.dxfer_len;
985195801Smav		}
986195801Smav		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
987195801Smav		    buf, size, siis_dmasetprd, slot, 0);
988195801Smav	} else
989195801Smav		siis_execute_transaction(slot);
990195801Smav}
991195801Smav
992195801Smav/* Locked by busdma engine. */
993195801Smavstatic void
994195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
995195801Smav{
996195801Smav	struct siis_slot *slot = arg;
997195801Smav	struct siis_channel *ch = device_get_softc(slot->dev);
998195801Smav	struct siis_cmd *ctp;
999195801Smav	struct siis_dma_prd *prd;
1000195801Smav	int i;
1001195801Smav
1002195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1003195801Smav	if (error) {
1004195801Smav		device_printf(slot->dev, "DMA load error\n");
1005195801Smav		if (!ch->readlog)
1006195801Smav			xpt_freeze_simq(ch->sim, 1);
1007195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1008195801Smav		return;
1009195801Smav	}
1010195801Smav	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
1011195801Smav	/* Get a piece of the workspace for this request */
1012195801Smav	ctp = (struct siis_cmd *)
1013195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
1014195801Smav	/* Fill S/G table */
1015195801Smav	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
1016195801Smav		prd = &ctp->u.ata.prd[0];
1017195801Smav	else
1018195801Smav		prd = &ctp->u.atapi.prd[0];
1019195801Smav	for (i = 0; i < nsegs; i++) {
1020195801Smav		prd[i].dba = htole64(segs[i].ds_addr);
1021195801Smav		prd[i].dbc = htole32(segs[i].ds_len);
1022195801Smav		prd[i].control = 0;
1023195801Smav	}
1024195801Smav	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
1025195801Smav	slot->dma.nsegs = nsegs;
1026195801Smav	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1027195801Smav	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1028195801Smav	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1029195801Smav	siis_execute_transaction(slot);
1030195801Smav}
1031195801Smav
1032195801Smav/* Must be called with channel locked. */
1033195801Smavstatic void
1034195801Smavsiis_execute_transaction(struct siis_slot *slot)
1035195801Smav{
1036195801Smav	device_t dev = slot->dev;
1037195801Smav	struct siis_channel *ch = device_get_softc(dev);
1038195801Smav	struct siis_cmd *ctp;
1039195801Smav	union ccb *ccb = slot->ccb;
1040195801Smav	u_int64_t prb_bus;
1041195801Smav
1042195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1043195801Smav	/* Get a piece of the workspace for this request */
1044195801Smav	ctp = (struct siis_cmd *)
1045195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
1046195801Smav	ctp->control = 0;
1047195801Smav	ctp->protocol_override = 0;
1048195801Smav	ctp->transfer_count = 0;
1049195801Smav	/* Special handling for Soft Reset command. */
1050201222Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1051201222Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) {
1052201222Smav			ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
1053201222Smav		} else {
1054201222Smav			ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE);
1055201222Smav			if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1056201222Smav				ctp->protocol_override |=
1057201222Smav				    htole16(SIIS_PRB_PROTO_NCQ);
1058201222Smav			}
1059201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1060201222Smav				ctp->protocol_override |=
1061201222Smav				    htole16(SIIS_PRB_PROTO_READ);
1062201222Smav			} else
1063201222Smav			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1064201222Smav				ctp->protocol_override |=
1065201222Smav				    htole16(SIIS_PRB_PROTO_WRITE);
1066201222Smav			}
1067201222Smav		}
1068195801Smav	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1069201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1070195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
1071201222Smav		else
1072201222Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1073195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
1074195801Smav	}
1075203108Smav	/* Special handling for Soft Reset command. */
1076203108Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1077203108Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1078203108Smav	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
1079203108Smav		/* Kick controller into sane state */
1080203108Smav		siis_portinit(dev);
1081203108Smav	}
1082195801Smav	/* Setup the FIS for this request */
1083199821Smav	if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) {
1084195801Smav		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1085195801Smav		if (!ch->readlog)
1086195801Smav			xpt_freeze_simq(ch->sim, 1);
1087195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
1088195801Smav		return;
1089195801Smav	}
1090195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1091195801Smav	    BUS_DMASYNC_PREWRITE);
1092195801Smav	/* Issue command to the controller. */
1093195801Smav	slot->state = SIIS_SLOT_RUNNING;
1094195801Smav	ch->rslots |= (1 << slot->slot);
1095195801Smav	prb_bus = ch->dma.work_bus +
1096195801Smav	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
1097195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
1098195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
1099195801Smav	/* Start command execution timeout */
1100195801Smav	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
1101195801Smav	    (timeout_t*)siis_timeout, slot);
1102195801Smav	return;
1103195801Smav}
1104195801Smav
1105198852Smav/* Must be called with channel locked. */
1106195801Smavstatic void
1107198852Smavsiis_process_timeout(device_t dev)
1108195801Smav{
1109195801Smav	struct siis_channel *ch = device_get_softc(dev);
1110195801Smav	int i;
1111195801Smav
1112195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1113198852Smav	if (!ch->readlog && !ch->recovery) {
1114195801Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1115198852Smav		ch->recovery = 1;
1116195801Smav	}
1117197838Smav	/* Handle the rest of commands. */
1118195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1119195801Smav		/* Do we have a running request on slot? */
1120195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1121195801Smav			continue;
1122198852Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
1123195801Smav	}
1124195801Smav}
1125195801Smav
1126203870Smav/* Must be called with channel locked. */
1127203870Smavstatic void
1128203870Smavsiis_rearm_timeout(device_t dev)
1129203870Smav{
1130203870Smav	struct siis_channel *ch = device_get_softc(dev);
1131203870Smav	int i;
1132203870Smav
1133203870Smav	mtx_assert(&ch->mtx, MA_OWNED);
1134203870Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1135203870Smav		struct siis_slot *slot = &ch->slot[i];
1136203870Smav
1137203870Smav		/* Do we have a running request on slot? */
1138203870Smav		if (slot->state < SIIS_SLOT_RUNNING)
1139203870Smav			continue;
1140203870Smav		if ((ch->toslots & (1 << i)) == 0)
1141203870Smav			continue;
1142203870Smav		callout_reset(&slot->timeout,
1143203870Smav		    (int)slot->ccb->ccb_h.timeout * hz / 1000,
1144203870Smav		    (timeout_t*)siis_timeout, slot);
1145203870Smav	}
1146203870Smav}
1147203870Smav
1148198852Smav/* Locked by callout mechanism. */
1149198852Smavstatic void
1150198852Smavsiis_timeout(struct siis_slot *slot)
1151198852Smav{
1152198852Smav	device_t dev = slot->dev;
1153198852Smav	struct siis_channel *ch = device_get_softc(dev);
1154198852Smav
1155198852Smav	mtx_assert(&ch->mtx, MA_OWNED);
1156198896Smav	/* Check for stale timeout. */
1157198896Smav	if (slot->state < SIIS_SLOT_RUNNING)
1158198896Smav		return;
1159198852Smav	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1160203108Smav	device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
1161203108Smav	    __func__, ATA_INL(ch->r_mem, SIIS_P_IS),
1162203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
1163203108Smav	    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
1164203108Smav	    ATA_INL(ch->r_mem, SIIS_P_SERR));
1165198852Smav
1166198852Smav	if (ch->toslots == 0)
1167198852Smav		xpt_freeze_simq(ch->sim, 1);
1168198852Smav	ch->toslots |= (1 << slot->slot);
1169198852Smav	if ((ch->rslots & ~ch->toslots) == 0)
1170198852Smav		siis_process_timeout(dev);
1171198852Smav	else
1172198852Smav		device_printf(dev, " ... waiting for slots %08x\n",
1173198852Smav		    ch->rslots & ~ch->toslots);
1174198852Smav}
1175198852Smav
1176195801Smav/* Must be called with channel locked. */
1177195801Smavstatic void
1178195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1179195801Smav{
1180195801Smav	device_t dev = slot->dev;
1181195801Smav	struct siis_channel *ch = device_get_softc(dev);
1182195801Smav	union ccb *ccb = slot->ccb;
1183212732Smav	int lastto;
1184195801Smav
1185195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1186195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1187195801Smav	    BUS_DMASYNC_POSTWRITE);
1188195801Smav	/* Read result registers to the result struct
1189195801Smav	 * May be incorrect if several commands finished same time,
1190195801Smav	 * so read only when sure or have to.
1191195801Smav	 */
1192195801Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1193195801Smav		struct ata_res *res = &ccb->ataio.res;
1194195801Smav		if ((et == SIIS_ERR_TFE) ||
1195195801Smav		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1196195801Smav			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1197195801Smav
1198195801Smav			res->status = ATA_INB(ch->r_mem, offs + 2);
1199195801Smav			res->error = ATA_INB(ch->r_mem, offs + 3);
1200195801Smav			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1201195801Smav			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1202195801Smav			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1203195801Smav			res->device = ATA_INB(ch->r_mem, offs + 7);
1204195801Smav			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1205195801Smav			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1206195801Smav			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1207195801Smav			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1208195801Smav			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1209195801Smav		} else
1210195801Smav			bzero(res, sizeof(*res));
1211214988Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1212214988Smav		    ch->numrslots == 1) {
1213214988Smav			ccb->ataio.resid = ccb->ataio.dxfer_len -
1214214988Smav			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1215214988Smav		}
1216214988Smav	} else {
1217214988Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1218214988Smav		    ch->numrslots == 1) {
1219214988Smav			ccb->csio.resid = ccb->csio.dxfer_len -
1220214988Smav			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1221214988Smav		}
1222195801Smav	}
1223195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1224195801Smav		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1225195801Smav		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1226195801Smav		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1227195801Smav		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1228195801Smav	}
1229195801Smav	/* Set proper result status. */
1230195801Smav	if (et != SIIS_ERR_NONE || ch->recovery) {
1231195801Smav		ch->eslots |= (1 << slot->slot);
1232195801Smav		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1233195801Smav	}
1234198321Smav	/* In case of error, freeze device for proper recovery. */
1235198321Smav	if (et != SIIS_ERR_NONE &&
1236198321Smav	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1237198321Smav		xpt_freeze_devq(ccb->ccb_h.path, 1);
1238198321Smav		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1239198321Smav	}
1240198321Smav	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1241195801Smav	switch (et) {
1242195801Smav	case SIIS_ERR_NONE:
1243195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP;
1244195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1245195801Smav			ccb->csio.scsi_status = SCSI_STATUS_OK;
1246195801Smav		break;
1247195801Smav	case SIIS_ERR_INVALID:
1248198852Smav		ch->fatalerr = 1;
1249195801Smav		ccb->ccb_h.status |= CAM_REQ_INVALID;
1250195801Smav		break;
1251195801Smav	case SIIS_ERR_INNOCENT:
1252195801Smav		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1253195801Smav		break;
1254195801Smav	case SIIS_ERR_TFE:
1255198321Smav	case SIIS_ERR_NCQ:
1256195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1257195801Smav			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1258195801Smav			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1259195801Smav		} else {
1260195801Smav			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1261195801Smav		}
1262195801Smav		break;
1263195801Smav	case SIIS_ERR_SATA:
1264198852Smav		ch->fatalerr = 1;
1265195801Smav		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1266195801Smav		break;
1267195801Smav	case SIIS_ERR_TIMEOUT:
1268198852Smav		ch->fatalerr = 1;
1269195801Smav		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1270195801Smav		break;
1271195801Smav	default:
1272195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1273195801Smav	}
1274195801Smav	/* Free slot. */
1275199747Smav	ch->oslots &= ~(1 << slot->slot);
1276195801Smav	ch->rslots &= ~(1 << slot->slot);
1277195801Smav	ch->aslots &= ~(1 << slot->slot);
1278195801Smav	slot->state = SIIS_SLOT_EMPTY;
1279195801Smav	slot->ccb = NULL;
1280195801Smav	/* Update channel stats. */
1281195801Smav	ch->numrslots--;
1282195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1283195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1284195801Smav		ch->numtslots[ccb->ccb_h.target_id]--;
1285195801Smav	}
1286212732Smav	/* Cancel timeout state if request completed normally. */
1287212732Smav	if (et != SIIS_ERR_TIMEOUT) {
1288212732Smav		lastto = (ch->toslots == (1 << slot->slot));
1289212732Smav		ch->toslots &= ~(1 << slot->slot);
1290212732Smav		if (lastto)
1291212732Smav			xpt_release_simq(ch->sim, TRUE);
1292212732Smav	}
1293198852Smav	/* If it was our READ LOG command - process it. */
1294198852Smav	if (ch->readlog) {
1295198852Smav		siis_process_read_log(dev, ccb);
1296195801Smav	/* If it was NCQ command error, put result on hold. */
1297198852Smav	} else if (et == SIIS_ERR_NCQ) {
1298195801Smav		ch->hold[slot->slot] = ccb;
1299195801Smav		ch->numhslots++;
1300198852Smav	} else
1301195801Smav		xpt_done(ccb);
1302195801Smav	/* Unfreeze frozen command. */
1303199747Smav	if (ch->frozen && !siis_check_collision(dev, ch->frozen)) {
1304195801Smav		union ccb *fccb = ch->frozen;
1305195801Smav		ch->frozen = NULL;
1306195801Smav		siis_begin_transaction(dev, fccb);
1307195801Smav		xpt_release_simq(ch->sim, TRUE);
1308195801Smav	}
1309195801Smav	/* If we have no other active commands, ... */
1310195801Smav	if (ch->rslots == 0) {
1311198852Smav		/* if there were timeouts or fatal error - reset port. */
1312198852Smav		if (ch->toslots != 0 || ch->fatalerr) {
1313198852Smav			siis_reset(dev);
1314198852Smav		} else {
1315198852Smav			/* if we have slots in error, we can reinit port. */
1316198852Smav			if (ch->eslots != 0)
1317198852Smav				siis_portinit(dev);
1318198852Smav			/* if there commands on hold, we can do READ LOG. */
1319198852Smav			if (!ch->readlog && ch->numhslots)
1320198852Smav				siis_issue_read_log(dev);
1321198852Smav		}
1322198852Smav	/* If all the reset of commands are in timeout - abort them. */
1323203870Smav	} else if ((ch->rslots & ~ch->toslots) == 0 &&
1324203870Smav	    et != SIIS_ERR_TIMEOUT)
1325203870Smav		siis_rearm_timeout(dev);
1326195801Smav}
1327195801Smav
1328195801Smavstatic void
1329195801Smavsiis_issue_read_log(device_t dev)
1330195801Smav{
1331195801Smav	struct siis_channel *ch = device_get_softc(dev);
1332195801Smav	union ccb *ccb;
1333195801Smav	struct ccb_ataio *ataio;
1334195801Smav	int i;
1335195801Smav
1336195801Smav	/* Find some holden command. */
1337195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1338195801Smav		if (ch->hold[i])
1339195801Smav			break;
1340195801Smav	}
1341195801Smav	if (i == SIIS_MAX_SLOTS)
1342195801Smav		return;
1343195801Smav	ch->readlog = 1;
1344195801Smav	ccb = xpt_alloc_ccb_nowait();
1345195801Smav	if (ccb == NULL) {
1346195801Smav		device_printf(dev, "Unable allocate READ LOG command");
1347195801Smav		return; /* XXX */
1348195801Smav	}
1349195801Smav	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1350195801Smav	ccb->ccb_h.func_code = XPT_ATA_IO;
1351195801Smav	ccb->ccb_h.flags = CAM_DIR_IN;
1352195801Smav	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1353195801Smav	ataio = &ccb->ataio;
1354195801Smav	ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1355195801Smav	if (ataio->data_ptr == NULL) {
1356208817Smav		xpt_free_ccb(ccb);
1357195801Smav		device_printf(dev, "Unable allocate memory for READ LOG command");
1358195801Smav		return; /* XXX */
1359195801Smav	}
1360195801Smav	ataio->dxfer_len = 512;
1361195801Smav	bzero(&ataio->cmd, sizeof(ataio->cmd));
1362195801Smav	ataio->cmd.flags = CAM_ATAIO_48BIT;
1363195801Smav	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1364195801Smav	ataio->cmd.sector_count = 1;
1365195801Smav	ataio->cmd.sector_count_exp = 0;
1366195801Smav	ataio->cmd.lba_low = 0x10;
1367195801Smav	ataio->cmd.lba_mid = 0;
1368195801Smav	ataio->cmd.lba_mid_exp = 0;
1369195801Smav	siis_begin_transaction(dev, ccb);
1370195801Smav}
1371195801Smav
1372195801Smavstatic void
1373195801Smavsiis_process_read_log(device_t dev, union ccb *ccb)
1374195801Smav{
1375195801Smav	struct siis_channel *ch = device_get_softc(dev);
1376195801Smav	uint8_t *data;
1377195801Smav	struct ata_res *res;
1378195801Smav	int i;
1379195801Smav
1380195801Smav	ch->readlog = 0;
1381195801Smav	data = ccb->ataio.data_ptr;
1382195801Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1383195801Smav	    (data[0] & 0x80) == 0) {
1384195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1385195801Smav			if (!ch->hold[i])
1386195801Smav				continue;
1387195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1388195801Smav				continue;
1389195801Smav			if ((data[0] & 0x1F) == i) {
1390195801Smav				res = &ch->hold[i]->ataio.res;
1391195801Smav				res->status = data[2];
1392195801Smav				res->error = data[3];
1393195801Smav				res->lba_low = data[4];
1394195801Smav				res->lba_mid = data[5];
1395195801Smav				res->lba_high = data[6];
1396195801Smav				res->device = data[7];
1397195801Smav				res->lba_low_exp = data[8];
1398195801Smav				res->lba_mid_exp = data[9];
1399195801Smav				res->lba_high_exp = data[10];
1400195801Smav				res->sector_count = data[12];
1401195801Smav				res->sector_count_exp = data[13];
1402195801Smav			} else {
1403195801Smav				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1404195801Smav				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1405195801Smav			}
1406195801Smav			xpt_done(ch->hold[i]);
1407195801Smav			ch->hold[i] = NULL;
1408195801Smav			ch->numhslots--;
1409195801Smav		}
1410195801Smav	} else {
1411195801Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1412195801Smav			device_printf(dev, "Error while READ LOG EXT\n");
1413195801Smav		else if ((data[0] & 0x80) == 0) {
1414195801Smav			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1415195801Smav		}
1416195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1417195801Smav			if (!ch->hold[i])
1418195801Smav				continue;
1419195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1420195801Smav				continue;
1421195801Smav			xpt_done(ch->hold[i]);
1422195801Smav			ch->hold[i] = NULL;
1423195801Smav			ch->numhslots--;
1424195801Smav		}
1425195801Smav	}
1426195801Smav	free(ccb->ataio.data_ptr, M_SIIS);
1427195801Smav	xpt_free_ccb(ccb);
1428195801Smav}
1429195801Smav
1430195801Smavstatic void
1431195801Smavsiis_portinit(device_t dev)
1432195801Smav{
1433195801Smav	struct siis_channel *ch = device_get_softc(dev);
1434195801Smav	int i;
1435195801Smav
1436195801Smav	ch->eslots = 0;
1437195801Smav	ch->recovery = 0;
1438195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1439195801Smav	for (i = 0; i < 16; i++) {
1440195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1441195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1442195801Smav	}
1443195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1444195801Smav	siis_wait_ready(dev, 1000);
1445195801Smav}
1446195801Smav
1447198426Smavstatic int
1448195801Smavsiis_devreset(device_t dev)
1449195801Smav{
1450195801Smav	struct siis_channel *ch = device_get_softc(dev);
1451198426Smav	int timeout = 0;
1452198426Smav	uint32_t val;
1453195801Smav
1454195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1455198426Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1456198426Smav	    SIIS_P_CTL_DEV_RESET) != 0) {
1457198426Smav		DELAY(1000);
1458198426Smav		if (timeout++ > 100) {
1459198426Smav			device_printf(dev, "device reset stuck (timeout %dms) "
1460198426Smav			    "status = %08x\n", timeout, val);
1461198426Smav			return (EBUSY);
1462198426Smav		}
1463198426Smav	}
1464198426Smav	return (0);
1465195801Smav}
1466195801Smav
1467195801Smavstatic int
1468195801Smavsiis_wait_ready(device_t dev, int t)
1469195801Smav{
1470195801Smav	struct siis_channel *ch = device_get_softc(dev);
1471195801Smav	int timeout = 0;
1472195801Smav	uint32_t val;
1473195801Smav
1474195801Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1475195801Smav	    SIIS_P_CTL_READY) == 0) {
1476195801Smav		DELAY(1000);
1477195801Smav		if (timeout++ > t) {
1478195801Smav			device_printf(dev, "port is not ready (timeout %dms) "
1479195801Smav			    "status = %08x\n", t, val);
1480195801Smav			return (EBUSY);
1481195801Smav		}
1482195801Smav	}
1483195801Smav	return (0);
1484195801Smav}
1485195801Smav
1486195801Smavstatic void
1487195801Smavsiis_reset(device_t dev)
1488195801Smav{
1489195801Smav	struct siis_channel *ch = device_get_softc(dev);
1490199821Smav	int i, retry = 0, sata_rev;
1491198426Smav	uint32_t val;
1492195801Smav
1493203108Smav	xpt_freeze_simq(ch->sim, 1);
1494195801Smav	if (bootverbose)
1495195801Smav		device_printf(dev, "SIIS reset...\n");
1496198852Smav	if (!ch->readlog && !ch->recovery)
1497198852Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
1498198852Smav	/* Requeue frozen command. */
1499195801Smav	if (ch->frozen) {
1500195801Smav		union ccb *fccb = ch->frozen;
1501195801Smav		ch->frozen = NULL;
1502198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1503198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1504198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1505198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
1506198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1507198321Smav		}
1508195801Smav		xpt_done(fccb);
1509195801Smav	}
1510198426Smav	/* Requeue all running commands. */
1511195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1512195801Smav		/* Do we have a running request on slot? */
1513195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1514195801Smav			continue;
1515195801Smav		/* XXX; Commands in loading state. */
1516195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1517195801Smav	}
1518198852Smav	/* Finish all holden commands as-is. */
1519198852Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1520198852Smav		if (!ch->hold[i])
1521198852Smav			continue;
1522198852Smav		xpt_done(ch->hold[i]);
1523198852Smav		ch->hold[i] = NULL;
1524198852Smav		ch->numhslots--;
1525198852Smav	}
1526198852Smav	if (ch->toslots != 0)
1527198852Smav		xpt_release_simq(ch->sim, TRUE);
1528198852Smav	ch->eslots = 0;
1529198852Smav	ch->recovery = 0;
1530198852Smav	ch->toslots = 0;
1531198852Smav	ch->fatalerr = 0;
1532198426Smav	/* Disable port interrupts */
1533198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1534198426Smav	/* Set speed limit. */
1535199821Smav	sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
1536199821Smav	if (sata_rev == 1)
1537198426Smav		val = ATA_SC_SPD_SPEED_GEN1;
1538199821Smav	else if (sata_rev == 2)
1539198426Smav		val = ATA_SC_SPD_SPEED_GEN2;
1540199821Smav	else if (sata_rev == 3)
1541198426Smav		val = ATA_SC_SPD_SPEED_GEN3;
1542198426Smav	else
1543198426Smav		val = 0;
1544198426Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1545198426Smav	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1546198426Smav	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1547198852Smavretry:
1548198426Smav	siis_devreset(dev);
1549195801Smav	/* Reset and reconnect PHY, */
1550198426Smav	if (!siis_sata_connect(ch)) {
1551195801Smav		ch->devices = 0;
1552195801Smav		/* Enable port interrupts */
1553195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1554195801Smav		if (bootverbose)
1555195801Smav			device_printf(dev,
1556195801Smav			    "SIIS reset done: phy reset found no device\n");
1557195801Smav		/* Tell the XPT about the event */
1558195801Smav		xpt_async(AC_BUS_RESET, ch->path, NULL);
1559203108Smav		xpt_release_simq(ch->sim, TRUE);
1560195801Smav		return;
1561195801Smav	}
1562195801Smav	/* Wait for clearing busy status. */
1563198852Smav	if (siis_wait_ready(dev, 10000)) {
1564195801Smav		device_printf(dev, "device ready timeout\n");
1565198852Smav		if (!retry) {
1566198852Smav			device_printf(dev, "trying full port reset ...\n");
1567198852Smav			/* Get port to the reset state. */
1568198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
1569198852Smav			DELAY(10000);
1570198852Smav			/* Get port out of reset state. */
1571198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
1572198852Smav			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
1573198852Smav			if (ch->pm_present)
1574198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1575198852Smav			else
1576198852Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1577198852Smav			siis_wait_ready(dev, 5000);
1578198852Smav			retry = 1;
1579198852Smav			goto retry;
1580198852Smav		}
1581198852Smav	}
1582195801Smav	ch->devices = 1;
1583195801Smav	/* Enable port interrupts */
1584195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1585195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1586195801Smav	if (bootverbose)
1587195801Smav		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1588195801Smav	/* Tell the XPT about the event */
1589195801Smav	xpt_async(AC_BUS_RESET, ch->path, NULL);
1590203108Smav	xpt_release_simq(ch->sim, TRUE);
1591195801Smav}
1592195801Smav
1593195801Smavstatic int
1594199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag)
1595195801Smav{
1596199821Smav	struct siis_channel *ch = device_get_softc(dev);
1597195801Smav	u_int8_t *fis = &ctp->fis[0];
1598195801Smav
1599195801Smav	bzero(fis, 24);
1600195801Smav	fis[0] = 0x27;  		/* host to device */
1601195801Smav	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1602195801Smav	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1603195801Smav		fis[1] |= 0x80;
1604195801Smav		fis[2] = ATA_PACKET_CMD;
1605199821Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1606199821Smav		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1607195801Smav			fis[3] = ATA_F_DMA;
1608195801Smav		else {
1609195801Smav			fis[5] = ccb->csio.dxfer_len;
1610195801Smav		        fis[6] = ccb->csio.dxfer_len >> 8;
1611195801Smav		}
1612195801Smav		fis[7] = ATA_D_LBA;
1613195801Smav		fis[15] = ATA_A_4BIT;
1614195801Smav		bzero(ctp->u.atapi.ccb, 16);
1615195801Smav		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1616195801Smav		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1617195801Smav		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1618195801Smav	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1619195801Smav		fis[1] |= 0x80;
1620195801Smav		fis[2] = ccb->ataio.cmd.command;
1621195801Smav		fis[3] = ccb->ataio.cmd.features;
1622195801Smav		fis[4] = ccb->ataio.cmd.lba_low;
1623195801Smav		fis[5] = ccb->ataio.cmd.lba_mid;
1624195801Smav		fis[6] = ccb->ataio.cmd.lba_high;
1625195801Smav		fis[7] = ccb->ataio.cmd.device;
1626195801Smav		fis[8] = ccb->ataio.cmd.lba_low_exp;
1627195801Smav		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1628195801Smav		fis[10] = ccb->ataio.cmd.lba_high_exp;
1629195801Smav		fis[11] = ccb->ataio.cmd.features_exp;
1630195801Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1631195801Smav			fis[12] = tag << 3;
1632195801Smav			fis[13] = 0;
1633195801Smav		} else {
1634195801Smav			fis[12] = ccb->ataio.cmd.sector_count;
1635195801Smav			fis[13] = ccb->ataio.cmd.sector_count_exp;
1636195801Smav		}
1637195801Smav		fis[15] = ATA_A_4BIT;
1638195801Smav	} else {
1639195801Smav		/* Soft reset. */
1640195801Smav	}
1641195801Smav	return (20);
1642195801Smav}
1643195801Smav
1644195801Smavstatic int
1645195801Smavsiis_sata_connect(struct siis_channel *ch)
1646195801Smav{
1647195801Smav	u_int32_t status;
1648195801Smav	int timeout;
1649195801Smav
1650195801Smav	/* Wait up to 100ms for "connect well" */
1651195801Smav	for (timeout = 0; timeout < 100 ; timeout++) {
1652195801Smav		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1653195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1654195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1655195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1656195801Smav			break;
1657195801Smav		DELAY(1000);
1658195801Smav	}
1659195801Smav	if (timeout >= 100) {
1660195801Smav		if (bootverbose) {
1661195801Smav			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1662195801Smav			    status);
1663195801Smav		}
1664195801Smav		return (0);
1665195801Smav	}
1666195801Smav	if (bootverbose) {
1667195801Smav		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1668195801Smav		    timeout, status);
1669195801Smav	}
1670195801Smav	/* Clear SATA error register */
1671195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1672195801Smav	return (1);
1673195801Smav}
1674195801Smav
1675207431Smavstatic int
1676207431Smavsiis_check_ids(device_t dev, union ccb *ccb)
1677207431Smav{
1678207431Smav
1679207431Smav	if (ccb->ccb_h.target_id > 15) {
1680207431Smav		ccb->ccb_h.status = CAM_TID_INVALID;
1681207431Smav		xpt_done(ccb);
1682207431Smav		return (-1);
1683207431Smav	}
1684207431Smav	if (ccb->ccb_h.target_lun != 0) {
1685207431Smav		ccb->ccb_h.status = CAM_LUN_INVALID;
1686207431Smav		xpt_done(ccb);
1687207431Smav		return (-1);
1688207431Smav	}
1689207431Smav	return (0);
1690207431Smav}
1691207431Smav
1692195801Smavstatic void
1693195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb)
1694195801Smav{
1695210471Smav	device_t dev, parent;
1696195801Smav	struct siis_channel *ch;
1697195801Smav
1698195801Smav	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1699195801Smav	    ccb->ccb_h.func_code));
1700195801Smav
1701195801Smav	ch = (struct siis_channel *)cam_sim_softc(sim);
1702195801Smav	dev = ch->dev;
1703195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1704195801Smav	switch (ccb->ccb_h.func_code) {
1705195801Smav	/* Common cases first */
1706195801Smav	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1707195801Smav	case XPT_SCSI_IO:
1708207431Smav		if (siis_check_ids(dev, ccb))
1709207431Smav			return;
1710207431Smav		if (ch->devices == 0 ||
1711207431Smav		    (ch->pm_present == 0 &&
1712207431Smav		     ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
1713195801Smav			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1714195801Smav			break;
1715195801Smav		}
1716195801Smav		/* Check for command collision. */
1717195801Smav		if (siis_check_collision(dev, ccb)) {
1718195801Smav			/* Freeze command. */
1719195801Smav			ch->frozen = ccb;
1720195801Smav			/* We have only one frozen slot, so freeze simq also. */
1721195801Smav			xpt_freeze_simq(ch->sim, 1);
1722195801Smav			return;
1723195801Smav		}
1724195801Smav		siis_begin_transaction(dev, ccb);
1725207431Smav		return;
1726195801Smav	case XPT_EN_LUN:		/* Enable LUN as a target */
1727195801Smav	case XPT_TARGET_IO:		/* Execute target I/O request */
1728195801Smav	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1729195801Smav	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1730195801Smav	case XPT_ABORT:			/* Abort the specified CCB */
1731195801Smav		/* XXX Implement */
1732195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1733195801Smav		break;
1734195801Smav	case XPT_SET_TRAN_SETTINGS:
1735195801Smav	{
1736195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1737199747Smav		struct	siis_device *d;
1738195801Smav
1739207431Smav		if (siis_check_ids(dev, ccb))
1740207431Smav			return;
1741199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1742199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1743199747Smav		else
1744199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1745199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1746199747Smav			d->revision = cts->xport_specific.sata.revision;
1747199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1748199747Smav			d->mode = cts->xport_specific.sata.mode;
1749199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1750199747Smav			d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1751199747Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1752199747Smav			d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags);
1753195801Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1754198852Smav			ch->pm_present = cts->xport_specific.sata.pm_present;
1755198852Smav			if (ch->pm_present)
1756195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1757195801Smav			else
1758195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1759195801Smav		}
1760203376Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1761203376Smav			d->atapi = cts->xport_specific.sata.atapi;
1762207499Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1763207499Smav			d->caps = cts->xport_specific.sata.caps;
1764195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1765195801Smav		break;
1766195801Smav	}
1767195801Smav	case XPT_GET_TRAN_SETTINGS:
1768195801Smav	/* Get default/user set transfer settings for the target */
1769195801Smav	{
1770195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1771199747Smav		struct  siis_device *d;
1772195801Smav		uint32_t status;
1773195801Smav
1774207431Smav		if (siis_check_ids(dev, ccb))
1775207431Smav			return;
1776199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1777199747Smav			d = &ch->curr[ccb->ccb_h.target_id];
1778199747Smav		else
1779199747Smav			d = &ch->user[ccb->ccb_h.target_id];
1780195801Smav		cts->protocol = PROTO_ATA;
1781196655Smav		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1782195801Smav		cts->transport = XPORT_SATA;
1783196655Smav		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1784195801Smav		cts->proto_specific.valid = 0;
1785195801Smav		cts->xport_specific.sata.valid = 0;
1786199747Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1787199747Smav		    (ccb->ccb_h.target_id == 15 ||
1788199747Smav		    (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
1789195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1790199747Smav			if (status & 0x0f0) {
1791199747Smav				cts->xport_specific.sata.revision =
1792199747Smav				    (status & 0x0f0) >> 4;
1793199747Smav				cts->xport_specific.sata.valid |=
1794199747Smav				    CTS_SATA_VALID_REVISION;
1795199747Smav			}
1796207499Smav			cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
1797207499Smav			if (ch->pm_level)
1798207499Smav				cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
1799207499Smav			cts->xport_specific.sata.caps &=
1800207499Smav			    ch->user[ccb->ccb_h.target_id].caps;
1801207499Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1802199747Smav		} else {
1803199747Smav			cts->xport_specific.sata.revision = d->revision;
1804199747Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1805207499Smav			cts->xport_specific.sata.caps = d->caps;
1806207499Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1807195801Smav		}
1808199747Smav		cts->xport_specific.sata.mode = d->mode;
1809199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1810199747Smav		cts->xport_specific.sata.bytecount = d->bytecount;
1811199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1812198852Smav		cts->xport_specific.sata.pm_present = ch->pm_present;
1813195801Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1814199747Smav		cts->xport_specific.sata.tags = d->tags;
1815199747Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
1816203376Smav		cts->xport_specific.sata.atapi = d->atapi;
1817203376Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1818195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1819195801Smav		break;
1820195801Smav	}
1821195801Smav	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1822195801Smav	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1823195801Smav		siis_reset(dev);
1824195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1825195801Smav		break;
1826195801Smav	case XPT_TERM_IO:		/* Terminate the I/O process */
1827195801Smav		/* XXX Implement */
1828195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1829195801Smav		break;
1830195801Smav	case XPT_PATH_INQ:		/* Path routing inquiry */
1831195801Smav	{
1832195801Smav		struct ccb_pathinq *cpi = &ccb->cpi;
1833195801Smav
1834210471Smav		parent = device_get_parent(dev);
1835195801Smav		cpi->version_num = 1; /* XXX??? */
1836195801Smav		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1837195801Smav		cpi->hba_inquiry |= PI_SATAPM;
1838195801Smav		cpi->target_sprt = 0;
1839195801Smav		cpi->hba_misc = PIM_SEQSCAN;
1840195801Smav		cpi->hba_eng_cnt = 0;
1841198322Smav		cpi->max_target = 15;
1842195801Smav		cpi->max_lun = 0;
1843195801Smav		cpi->initiator_id = 0;
1844195801Smav		cpi->bus_id = cam_sim_bus(sim);
1845195801Smav		cpi->base_transfer_speed = 150000;
1846195801Smav		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1847195801Smav		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1848195801Smav		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1849195801Smav		cpi->unit_number = cam_sim_unit(sim);
1850195801Smav		cpi->transport = XPORT_SATA;
1851196655Smav		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1852195801Smav		cpi->protocol = PROTO_ATA;
1853196655Smav		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1854210471Smav		cpi->maxio = MAXPHYS;
1855210471Smav		cpi->hba_vendor = pci_get_vendor(parent);
1856210471Smav		cpi->hba_device = pci_get_device(parent);
1857210471Smav		cpi->hba_subvendor = pci_get_subvendor(parent);
1858210471Smav		cpi->hba_subdevice = pci_get_subdevice(parent);
1859195801Smav		cpi->ccb_h.status = CAM_REQ_CMP;
1860195801Smav		break;
1861195801Smav	}
1862195801Smav	default:
1863195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1864195801Smav		break;
1865195801Smav	}
1866207431Smav	xpt_done(ccb);
1867195801Smav}
1868195801Smav
1869195801Smavstatic void
1870195801Smavsiispoll(struct cam_sim *sim)
1871195801Smav{
1872195801Smav	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1873195801Smav
1874195801Smav	siis_ch_intr(ch->dev);
1875195801Smav}
1876