siis.c revision 198322
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 198322 2009-10-21 13:00:01Z mav $");
29195801Smav
30195801Smav#include <sys/param.h>
31195801Smav#include <sys/module.h>
32195801Smav#include <sys/systm.h>
33195801Smav#include <sys/kernel.h>
34195801Smav#include <sys/ata.h>
35195801Smav#include <sys/bus.h>
36195801Smav#include <sys/endian.h>
37195801Smav#include <sys/malloc.h>
38195801Smav#include <sys/lock.h>
39195801Smav#include <sys/mutex.h>
40195801Smav#include <sys/sema.h>
41195801Smav#include <sys/taskqueue.h>
42195801Smav#include <vm/uma.h>
43195801Smav#include <machine/stdarg.h>
44195801Smav#include <machine/resource.h>
45195801Smav#include <machine/bus.h>
46195801Smav#include <sys/rman.h>
47195801Smav#include <dev/pci/pcivar.h>
48195801Smav#include <dev/pci/pcireg.h>
49195801Smav#include "siis.h"
50195801Smav
51195801Smav#include <cam/cam.h>
52195801Smav#include <cam/cam_ccb.h>
53195801Smav#include <cam/cam_sim.h>
54195801Smav#include <cam/cam_xpt_sim.h>
55195801Smav#include <cam/cam_xpt_periph.h>
56195801Smav#include <cam/cam_debug.h>
57195801Smav
58195801Smav/* local prototypes */
59195801Smavstatic int siis_setup_interrupt(device_t dev);
60195801Smavstatic void siis_intr(void *data);
61195801Smavstatic int siis_suspend(device_t dev);
62195801Smavstatic int siis_resume(device_t dev);
63195801Smavstatic int siis_ch_suspend(device_t dev);
64195801Smavstatic int siis_ch_resume(device_t dev);
65195801Smavstatic void siis_ch_intr_locked(void *data);
66195801Smavstatic void siis_ch_intr(void *data);
67195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb);
68195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
69195801Smavstatic void siis_execute_transaction(struct siis_slot *slot);
70195801Smavstatic void siis_timeout(struct siis_slot *slot);
71195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
72195801Smavstatic int siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag);
73195801Smavstatic void siis_dmainit(device_t dev);
74195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
75195801Smavstatic void siis_dmafini(device_t dev);
76195801Smavstatic void siis_slotsalloc(device_t dev);
77195801Smavstatic void siis_slotsfree(device_t dev);
78195801Smavstatic void siis_reset(device_t dev);
79195801Smavstatic void siis_portinit(device_t dev);
80195801Smavstatic int siis_wait_ready(device_t dev, int t);
81195801Smav
82195801Smavstatic int siis_sata_connect(struct siis_channel *ch);
83195801Smavstatic int siis_sata_phy_reset(device_t dev);
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
93195801Smavstatic int
94195801Smavsiis_probe(device_t dev)
95195801Smav{
96195801Smav	uint32_t devid = pci_get_devid(dev);
97195801Smav
98195801Smav	if (devid == SIIS_SII3124) {
99195801Smav		device_set_desc_copy(dev, "SiI3124 SATA2 controller");
100195801Smav	} else if (devid == SIIS_SII3132 ||
101195801Smav		   devid == SIIS_SII3132_1 ||
102195801Smav		   devid == SIIS_SII3132_2) {
103195801Smav		device_set_desc_copy(dev, "SiI3132 SATA2 controller");
104195801Smav	} else if (devid == SIIS_SII3531) {
105195801Smav		device_set_desc_copy(dev, "SiI3531 SATA2 controller");
106195801Smav	} else {
107195801Smav		return (ENXIO);
108195801Smav	}
109195801Smav
110195801Smav	return (BUS_PROBE_VENDOR);
111195801Smav}
112195801Smav
113195801Smavstatic int
114195801Smavsiis_attach(device_t dev)
115195801Smav{
116195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
117195801Smav	uint32_t devid = pci_get_devid(dev);
118195801Smav	device_t child;
119195801Smav	int	error, unit;
120195801Smav
121195801Smav	ctlr->dev = dev;
122195801Smav	/* Global memory */
123195801Smav	ctlr->r_grid = PCIR_BAR(0);
124195801Smav	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
125195801Smav	    &ctlr->r_grid, RF_ACTIVE)))
126195801Smav		return (ENXIO);
127195801Smav	/* Channels memory */
128195801Smav	ctlr->r_rid = PCIR_BAR(2);
129195801Smav	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
130195801Smav	    &ctlr->r_rid, RF_ACTIVE)))
131195801Smav		return (ENXIO);
132195801Smav	/* Setup our own memory management for channels. */
133195801Smav	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
134195801Smav	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
135195801Smav	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
136195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
137195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
138195801Smav		return (error);
139195801Smav	}
140195801Smav	if ((error = rman_manage_region(&ctlr->sc_iomem,
141195801Smav	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
142195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
143195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
144195801Smav		rman_fini(&ctlr->sc_iomem);
145195801Smav		return (error);
146195801Smav	}
147195801Smav	/* Reset controller */
148195801Smav	siis_resume(dev);
149195801Smav	/* Number of HW channels */
150195801Smav	ctlr->channels = (devid == SIIS_SII3124) ? 4 :
151195801Smav	    (devid == SIIS_SII3531 ? 1 : 2);
152195801Smav	/* Setup interrupts. */
153195801Smav	if (siis_setup_interrupt(dev)) {
154195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
155195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
156195801Smav		rman_fini(&ctlr->sc_iomem);
157195801Smav		return ENXIO;
158195801Smav	}
159195801Smav	/* Attach all channels on this controller */
160195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
161195801Smav		child = device_add_child(dev, "siisch", -1);
162195801Smav		if (child == NULL)
163195801Smav			device_printf(dev, "failed to add channel device\n");
164195801Smav		else
165195801Smav			device_set_ivars(child, (void *)(intptr_t)unit);
166195801Smav	}
167195801Smav	bus_generic_attach(dev);
168195801Smav	return 0;
169195801Smav}
170195801Smav
171195801Smavstatic int
172195801Smavsiis_detach(device_t dev)
173195801Smav{
174195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
175195801Smav	device_t *children;
176195801Smav	int nchildren, i;
177195801Smav
178195801Smav	/* Detach & delete all children */
179195801Smav	if (!device_get_children(dev, &children, &nchildren)) {
180195801Smav		for (i = 0; i < nchildren; i++)
181195801Smav			device_delete_child(dev, children[i]);
182195801Smav		free(children, M_TEMP);
183195801Smav	}
184195801Smav	/* Free interrupts. */
185195801Smav	if (ctlr->irq.r_irq) {
186195801Smav		bus_teardown_intr(dev, ctlr->irq.r_irq,
187195801Smav		    ctlr->irq.handle);
188195801Smav		bus_release_resource(dev, SYS_RES_IRQ,
189195801Smav		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
190195801Smav	}
191195801Smav	pci_release_msi(dev);
192195801Smav	/* Free memory. */
193195801Smav	rman_fini(&ctlr->sc_iomem);
194195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
195195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
196195801Smav	return (0);
197195801Smav}
198195801Smav
199195801Smavstatic int
200195801Smavsiis_suspend(device_t dev)
201195801Smav{
202195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
203195801Smav
204195801Smav	bus_generic_suspend(dev);
205195801Smav	/* Put controller into reset state. */
206195801Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
207195801Smav	return 0;
208195801Smav}
209195801Smav
210195801Smavstatic int
211195801Smavsiis_resume(device_t dev)
212195801Smav{
213195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
214195801Smav
215195801Smav	/* Put controller into reset state. */
216195801Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
217195801Smav	DELAY(10000);
218195801Smav	/* Get controller out of reset state and enable port interrupts. */
219195801Smav	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 0x0000000f);
220195801Smav	return (bus_generic_resume(dev));
221195801Smav}
222195801Smav
223195801Smavstatic int
224195801Smavsiis_setup_interrupt(device_t dev)
225195801Smav{
226195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
227195801Smav	int msi = 0;
228195801Smav
229195801Smav	/* Process hints. */
230195801Smav	resource_int_value(device_get_name(dev),
231195801Smav	    device_get_unit(dev), "msi", &msi);
232195801Smav	if (msi < 0)
233195801Smav		msi = 0;
234195801Smav	else if (msi > 0)
235195801Smav		msi = min(1, pci_msi_count(dev));
236195801Smav	/* Allocate MSI if needed/present. */
237195801Smav	if (msi && pci_alloc_msi(dev, &msi) != 0)
238195801Smav		msi = 0;
239195801Smav	/* Allocate all IRQs. */
240195801Smav	ctlr->irq.r_irq_rid = msi ? 1 : 0;
241195801Smav	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
242195801Smav	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
243195801Smav		device_printf(dev, "unable to map interrupt\n");
244195801Smav		return ENXIO;
245195801Smav	}
246195801Smav	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
247195801Smav	    siis_intr, ctlr, &ctlr->irq.handle))) {
248195801Smav		/* SOS XXX release r_irq */
249195801Smav		device_printf(dev, "unable to setup interrupt\n");
250195801Smav		return ENXIO;
251195801Smav	}
252195801Smav	return (0);
253195801Smav}
254195801Smav
255195801Smav/*
256195801Smav * Common case interrupt handler.
257195801Smav */
258195801Smavstatic void
259195801Smavsiis_intr(void *data)
260195801Smav{
261195801Smav	struct siis_controller *ctlr = (struct siis_controller *)data;
262195801Smav	u_int32_t is;
263195801Smav	void *arg;
264195801Smav	int unit;
265195801Smav
266195801Smav	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
267195801Smav	for (unit = 0; unit < ctlr->channels; unit++) {
268195801Smav		if ((is & SIIS_IS_PORT(unit)) != 0 &&
269195801Smav		    (arg = ctlr->interrupt[unit].argument)) {
270195801Smav			ctlr->interrupt[unit].function(arg);
271195801Smav		}
272195801Smav	}
273195801Smav}
274195801Smav
275195801Smavstatic struct resource *
276195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid,
277195801Smav		       u_long start, u_long end, u_long count, u_int flags)
278195801Smav{
279195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
280195801Smav	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
281195801Smav	struct resource *res = NULL;
282195801Smav	int offset = unit << 13;
283195801Smav	long st;
284195801Smav
285195801Smav	switch (type) {
286195801Smav	case SYS_RES_MEMORY:
287195801Smav		st = rman_get_start(ctlr->r_mem);
288195801Smav		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
289195801Smav		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
290195801Smav		if (res) {
291195801Smav			bus_space_handle_t bsh;
292195801Smav			bus_space_tag_t bst;
293195801Smav			bsh = rman_get_bushandle(ctlr->r_mem);
294195801Smav			bst = rman_get_bustag(ctlr->r_mem);
295195801Smav			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
296195801Smav			rman_set_bushandle(res, bsh);
297195801Smav			rman_set_bustag(res, bst);
298195801Smav		}
299195801Smav		break;
300195801Smav	case SYS_RES_IRQ:
301195801Smav		if (*rid == ATA_IRQ_RID)
302195801Smav			res = ctlr->irq.r_irq;
303195801Smav		break;
304195801Smav	}
305195801Smav	return (res);
306195801Smav}
307195801Smav
308195801Smavstatic int
309195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid,
310195801Smav			 struct resource *r)
311195801Smav{
312195801Smav
313195801Smav	switch (type) {
314195801Smav	case SYS_RES_MEMORY:
315195801Smav		rman_release_resource(r);
316195801Smav		return (0);
317195801Smav	case SYS_RES_IRQ:
318195801Smav		if (rid != ATA_IRQ_RID)
319195801Smav			return ENOENT;
320195801Smav		return (0);
321195801Smav	}
322195801Smav	return (EINVAL);
323195801Smav}
324195801Smav
325195801Smavstatic int
326195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq,
327195801Smav		   int flags, driver_filter_t *filter, driver_intr_t *function,
328195801Smav		   void *argument, void **cookiep)
329195801Smav{
330195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
331195801Smav	int unit = (intptr_t)device_get_ivars(child);
332195801Smav
333195801Smav	if (filter != NULL) {
334195801Smav		printf("siis.c: we cannot use a filter here\n");
335195801Smav		return (EINVAL);
336195801Smav	}
337195801Smav	ctlr->interrupt[unit].function = function;
338195801Smav	ctlr->interrupt[unit].argument = argument;
339195801Smav	return (0);
340195801Smav}
341195801Smav
342195801Smavstatic int
343195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq,
344195801Smav		      void *cookie)
345195801Smav{
346195801Smav	struct siis_controller *ctlr = device_get_softc(dev);
347195801Smav	int unit = (intptr_t)device_get_ivars(child);
348195801Smav
349195801Smav	ctlr->interrupt[unit].function = NULL;
350195801Smav	ctlr->interrupt[unit].argument = NULL;
351195801Smav	return (0);
352195801Smav}
353195801Smav
354195801Smavstatic int
355195801Smavsiis_print_child(device_t dev, device_t child)
356195801Smav{
357195801Smav	int retval;
358195801Smav
359195801Smav	retval = bus_print_child_header(dev, child);
360195801Smav	retval += printf(" at channel %d",
361195801Smav	    (int)(intptr_t)device_get_ivars(child));
362195801Smav	retval += bus_print_child_footer(dev, child);
363195801Smav
364195801Smav	return (retval);
365195801Smav}
366195801Smav
367195801Smavdevclass_t siis_devclass;
368195801Smavstatic device_method_t siis_methods[] = {
369195801Smav	DEVMETHOD(device_probe,     siis_probe),
370195801Smav	DEVMETHOD(device_attach,    siis_attach),
371195801Smav	DEVMETHOD(device_detach,    siis_detach),
372195801Smav	DEVMETHOD(device_suspend,   siis_suspend),
373195801Smav	DEVMETHOD(device_resume,    siis_resume),
374195801Smav	DEVMETHOD(bus_print_child,  siis_print_child),
375195801Smav	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
376195801Smav	DEVMETHOD(bus_release_resource,     siis_release_resource),
377195801Smav	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
378195801Smav	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
379195801Smav	{ 0, 0 }
380195801Smav};
381195801Smavstatic driver_t siis_driver = {
382195801Smav        "siis",
383195801Smav        siis_methods,
384195801Smav        sizeof(struct siis_controller)
385195801Smav};
386195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
387195801SmavMODULE_VERSION(siis, 1);
388195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1);
389195801Smav
390195801Smavstatic int
391195801Smavsiis_ch_probe(device_t dev)
392195801Smav{
393195801Smav
394195801Smav	device_set_desc_copy(dev, "SIIS channel");
395195801Smav	return (0);
396195801Smav}
397195801Smav
398195801Smavstatic int
399195801Smavsiis_ch_attach(device_t dev)
400195801Smav{
401195801Smav	struct siis_channel *ch = device_get_softc(dev);
402195801Smav	struct cam_devq *devq;
403195801Smav	int rid, error;
404195801Smav
405195801Smav	ch->dev = dev;
406195801Smav	ch->unit = (intptr_t)device_get_ivars(dev);
407195801Smav	resource_int_value(device_get_name(dev),
408195801Smav	    device_get_unit(dev), "pm_level", &ch->pm_level);
409195801Smav	resource_int_value(device_get_name(dev),
410195801Smav	    device_get_unit(dev), "sata_rev", &ch->sata_rev);
411195801Smav	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
412195801Smav	rid = ch->unit;
413195801Smav	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
414195801Smav	    &rid, RF_ACTIVE)))
415195801Smav		return (ENXIO);
416195801Smav	siis_dmainit(dev);
417195801Smav	siis_slotsalloc(dev);
418195801Smav	siis_ch_resume(dev);
419195801Smav	mtx_lock(&ch->mtx);
420195801Smav	rid = ATA_IRQ_RID;
421195801Smav	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
422195801Smav	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
423195801Smav		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
424195801Smav		device_printf(dev, "Unable to map interrupt\n");
425195801Smav		return (ENXIO);
426195801Smav	}
427195801Smav	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
428195801Smav	    siis_ch_intr_locked, dev, &ch->ih))) {
429195801Smav		device_printf(dev, "Unable to setup interrupt\n");
430195801Smav		error = ENXIO;
431195801Smav		goto err1;
432195801Smav	}
433195801Smav	/* Create the device queue for our SIM. */
434195801Smav	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
435195801Smav	if (devq == NULL) {
436195801Smav		device_printf(dev, "Unable to allocate simq\n");
437195801Smav		error = ENOMEM;
438195801Smav		goto err1;
439195801Smav	}
440195801Smav	/* Construct SIM entry */
441195801Smav	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
442195801Smav	    device_get_unit(dev), &ch->mtx, SIIS_MAX_SLOTS, 0, devq);
443195801Smav	if (ch->sim == NULL) {
444195801Smav		device_printf(dev, "unable to allocate sim\n");
445195801Smav		error = ENOMEM;
446195801Smav		goto err2;
447195801Smav	}
448195801Smav	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
449195801Smav		device_printf(dev, "unable to register xpt bus\n");
450195801Smav		error = ENXIO;
451195801Smav		goto err2;
452195801Smav	}
453195801Smav	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
454195801Smav	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
455195801Smav		device_printf(dev, "unable to create path\n");
456195801Smav		error = ENXIO;
457195801Smav		goto err3;
458195801Smav	}
459195801Smav	mtx_unlock(&ch->mtx);
460195801Smav	return (0);
461195801Smav
462195801Smaverr3:
463195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
464195801Smaverr2:
465195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
466195801Smaverr1:
467195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
468195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
469195801Smav	mtx_unlock(&ch->mtx);
470195801Smav	return (error);
471195801Smav}
472195801Smav
473195801Smavstatic int
474195801Smavsiis_ch_detach(device_t dev)
475195801Smav{
476195801Smav	struct siis_channel *ch = device_get_softc(dev);
477195801Smav
478195801Smav	mtx_lock(&ch->mtx);
479195801Smav	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
480195801Smav	xpt_free_path(ch->path);
481195801Smav	xpt_bus_deregister(cam_sim_path(ch->sim));
482195801Smav	cam_sim_free(ch->sim, /*free_devq*/TRUE);
483195801Smav	mtx_unlock(&ch->mtx);
484195801Smav
485195801Smav	bus_teardown_intr(dev, ch->r_irq, ch->ih);
486195801Smav	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
487195801Smav
488195801Smav	siis_ch_suspend(dev);
489195801Smav	siis_slotsfree(dev);
490195801Smav	siis_dmafini(dev);
491195801Smav
492195801Smav	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
493195801Smav	mtx_destroy(&ch->mtx);
494195801Smav	return (0);
495195801Smav}
496195801Smav
497195801Smavstatic int
498195801Smavsiis_ch_suspend(device_t dev)
499195801Smav{
500195801Smav	struct siis_channel *ch = device_get_softc(dev);
501195801Smav
502195801Smav	/* Put port into reset state. */
503195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
504195801Smav	return (0);
505195801Smav}
506195801Smav
507195801Smavstatic int
508195801Smavsiis_ch_resume(device_t dev)
509195801Smav{
510195801Smav	struct siis_channel *ch = device_get_softc(dev);
511195801Smav
512195801Smav	/* Get port out of reset state. */
513195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
514195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
515195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
516195801Smav	/* Enable port interrupts */
517195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
518195801Smav	return (0);
519195801Smav}
520195801Smav
521195801Smavdevclass_t siisch_devclass;
522195801Smavstatic device_method_t siisch_methods[] = {
523195801Smav	DEVMETHOD(device_probe,     siis_ch_probe),
524195801Smav	DEVMETHOD(device_attach,    siis_ch_attach),
525195801Smav	DEVMETHOD(device_detach,    siis_ch_detach),
526195801Smav	DEVMETHOD(device_suspend,   siis_ch_suspend),
527195801Smav	DEVMETHOD(device_resume,    siis_ch_resume),
528195801Smav	{ 0, 0 }
529195801Smav};
530195801Smavstatic driver_t siisch_driver = {
531195801Smav        "siisch",
532195801Smav        siisch_methods,
533195801Smav        sizeof(struct siis_channel)
534195801Smav};
535195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
536195801Smav
537195801Smavstruct siis_dc_cb_args {
538195801Smav	bus_addr_t maddr;
539195801Smav	int error;
540195801Smav};
541195801Smav
542195801Smavstatic void
543195801Smavsiis_dmainit(device_t dev)
544195801Smav{
545195801Smav	struct siis_channel *ch = device_get_softc(dev);
546195801Smav	struct siis_dc_cb_args dcba;
547195801Smav
548195801Smav	/* Command area. */
549195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
550195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
551195801Smav	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
552195801Smav	    0, NULL, NULL, &ch->dma.work_tag))
553195801Smav		goto error;
554195801Smav	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
555195801Smav	    &ch->dma.work_map))
556195801Smav		goto error;
557195801Smav	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
558195801Smav	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
559195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
560195801Smav		goto error;
561195801Smav	}
562195801Smav	ch->dma.work_bus = dcba.maddr;
563195801Smav	/* Data area. */
564195801Smav	if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
565195801Smav	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
566195801Smav	    NULL, NULL,
567195801Smav	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
568195801Smav	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
569195801Smav	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
570195801Smav		goto error;
571195801Smav	}
572195801Smav	return;
573195801Smav
574195801Smaverror:
575195801Smav	device_printf(dev, "WARNING - DMA initialization failed\n");
576195801Smav	siis_dmafini(dev);
577195801Smav}
578195801Smav
579195801Smavstatic void
580195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
581195801Smav{
582195801Smav	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
583195801Smav
584195801Smav	if (!(dcba->error = error))
585195801Smav		dcba->maddr = segs[0].ds_addr;
586195801Smav}
587195801Smav
588195801Smavstatic void
589195801Smavsiis_dmafini(device_t dev)
590195801Smav{
591195801Smav	struct siis_channel *ch = device_get_softc(dev);
592195801Smav
593195801Smav	if (ch->dma.data_tag) {
594195801Smav		bus_dma_tag_destroy(ch->dma.data_tag);
595195801Smav		ch->dma.data_tag = NULL;
596195801Smav	}
597195801Smav	if (ch->dma.work_bus) {
598195801Smav		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
599195801Smav		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
600195801Smav		ch->dma.work_bus = 0;
601195801Smav		ch->dma.work_map = NULL;
602195801Smav		ch->dma.work = NULL;
603195801Smav	}
604195801Smav	if (ch->dma.work_tag) {
605195801Smav		bus_dma_tag_destroy(ch->dma.work_tag);
606195801Smav		ch->dma.work_tag = NULL;
607195801Smav	}
608195801Smav}
609195801Smav
610195801Smavstatic void
611195801Smavsiis_slotsalloc(device_t dev)
612195801Smav{
613195801Smav	struct siis_channel *ch = device_get_softc(dev);
614195801Smav	int i;
615195801Smav
616195801Smav	/* Alloc and setup command/dma slots */
617195801Smav	bzero(ch->slot, sizeof(ch->slot));
618195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
619195801Smav		struct siis_slot *slot = &ch->slot[i];
620195801Smav
621195801Smav		slot->dev = dev;
622195801Smav		slot->slot = i;
623195801Smav		slot->state = SIIS_SLOT_EMPTY;
624195801Smav		slot->ccb = NULL;
625195801Smav		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
626195801Smav
627195801Smav		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
628195801Smav			device_printf(ch->dev, "FAILURE - create data_map\n");
629195801Smav	}
630195801Smav}
631195801Smav
632195801Smavstatic void
633195801Smavsiis_slotsfree(device_t dev)
634195801Smav{
635195801Smav	struct siis_channel *ch = device_get_softc(dev);
636195801Smav	int i;
637195801Smav
638195801Smav	/* Free all dma slots */
639195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
640195801Smav		struct siis_slot *slot = &ch->slot[i];
641195801Smav
642195801Smav		if (slot->dma.data_map) {
643195801Smav			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
644195801Smav			slot->dma.data_map = NULL;
645195801Smav		}
646195801Smav	}
647195801Smav}
648195801Smav
649195801Smavstatic void
650196655Smavsiis_notify_events(device_t dev)
651196655Smav{
652196655Smav	struct siis_channel *ch = device_get_softc(dev);
653196655Smav	struct cam_path *dpath;
654196655Smav	u_int32_t status;
655196655Smav	int i;
656196655Smav
657196655Smav	status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
658196655Smav	ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
659196655Smav	if (bootverbose)
660196655Smav		device_printf(dev, "SNTF 0x%04x\n", status);
661196655Smav	for (i = 0; i < 16; i++) {
662196655Smav		if ((status & (1 << i)) == 0)
663196655Smav			continue;
664196655Smav		if (xpt_create_path(&dpath, NULL,
665196655Smav		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
666196655Smav			xpt_async(AC_SCSI_AEN, dpath, NULL);
667196655Smav			xpt_free_path(dpath);
668196655Smav		}
669196655Smav	}
670196655Smav
671196655Smav}
672196655Smav
673196655Smavstatic void
674195801Smavsiis_phy_check_events(device_t dev)
675195801Smav{
676195801Smav	struct siis_channel *ch = device_get_softc(dev);
677195801Smav
678195801Smav	/* If we have a connection event, deal with it */
679195801Smav	if (ch->pm_level == 0) {
680195801Smav		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
681195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
682195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
683195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
684195801Smav			if (bootverbose)
685195801Smav				device_printf(dev, "CONNECT requested\n");
686195801Smav			siis_reset(dev);
687195801Smav		} else {
688195801Smav			if (bootverbose)
689195801Smav				device_printf(dev, "DISCONNECT requested\n");
690195801Smav			ch->devices = 0;
691195801Smav		}
692195801Smav	}
693195801Smav}
694195801Smav
695195801Smavstatic void
696195801Smavsiis_ch_intr_locked(void *data)
697195801Smav{
698195801Smav	device_t dev = (device_t)data;
699195801Smav	struct siis_channel *ch = device_get_softc(dev);
700195801Smav
701195801Smav	mtx_lock(&ch->mtx);
702195801Smav	siis_ch_intr(data);
703195801Smav	mtx_unlock(&ch->mtx);
704195801Smav}
705195801Smav
706195801Smavstatic void
707195801Smavsiis_ch_intr(void *data)
708195801Smav{
709195801Smav	device_t dev = (device_t)data;
710195801Smav	struct siis_channel *ch = device_get_softc(dev);
711195801Smav	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
712195801Smav	enum siis_err_type et;
713195801Smav	int i, ccs, port, tslots;
714195801Smav
715195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
716195801Smav	/* Read command statuses. */
717195801Smav	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
718195801Smav	ok = ch->rslots & ~sstatus;
719195801Smav	/* Complete all successfull commands. */
720195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
721195801Smav		if ((ok >> i) & 1)
722195801Smav			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
723195801Smav	}
724195801Smav	/* Do we have any other events? */
725195801Smav	if ((sstatus & SIIS_P_SS_ATTN) == 0)
726195801Smav		return;
727195801Smav	/* Read and clear interrupt statuses. */
728195801Smav	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
729195801Smav	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
730195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
731195801Smav	/* Process PHY events */
732195801Smav	if (istatus & SIIS_P_IX_PHYRDYCHG)
733195801Smav		siis_phy_check_events(dev);
734196655Smav	/* Process NOTIFY events */
735196655Smav	if (istatus & SIIS_P_IX_SDBN)
736196655Smav		siis_notify_events(dev);
737195801Smav	/* Process command errors */
738195801Smav	if (istatus & SIIS_P_IX_COMMERR) {
739195801Smav		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
740195801Smav		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
741195801Smav		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
742195801Smav		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
743195801Smav		err = ch->rslots & sstatus;
744195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
745195801Smav//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
746195801Smav//    ATA_INL(ch->r_mem, SIIS_P_SERR));
747195801Smav
748195801Smav		if (!ch->readlog && !ch->recovery) {
749195801Smav			xpt_freeze_simq(ch->sim, ch->numrslots);
750195801Smav			ch->recovery = 1;
751195801Smav		}
752195801Smav		if (ch->frozen) {
753195801Smav			union ccb *fccb = ch->frozen;
754195801Smav			ch->frozen = NULL;
755198321Smav			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
756198321Smav			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
757198321Smav			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
758198321Smav				xpt_freeze_devq(fccb->ccb_h.path, 1);
759198321Smav				fccb->ccb_h.status |= CAM_DEV_QFRZN;
760198321Smav			}
761195801Smav			xpt_done(fccb);
762195801Smav		}
763195801Smav		if (estatus == SIIS_P_CMDERR_DEV ||
764195801Smav		    estatus == SIIS_P_CMDERR_SDB ||
765195801Smav		    estatus == SIIS_P_CMDERR_DATAFIS) {
766195801Smav			tslots = ch->numtslots[port];
767195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
768195801Smav				/* XXX: reqests in loading state. */
769195801Smav				if (((ch->rslots >> i) & 1) == 0)
770195801Smav					continue;
771195801Smav				if (ch->slot[i].ccb->ccb_h.target_id != port)
772195801Smav					continue;
773195801Smav				if (tslots == 0) {
774195801Smav					/* Untagged operation. */
775195801Smav					if (i == ccs)
776195801Smav						et = SIIS_ERR_TFE;
777195801Smav					else
778195801Smav						et = SIIS_ERR_INNOCENT;
779195801Smav				} else {
780195801Smav					/* Tagged operation. */
781195801Smav					et = SIIS_ERR_NCQ;
782195801Smav				}
783195801Smav				siis_end_transaction(&ch->slot[i], et);
784195801Smav			}
785195801Smav			/*
786195801Smav			 * We can't reinit port if there are some other
787195801Smav			 * commands active, use resume to complete them.
788195801Smav			 */
789195801Smav			if (ch->rslots != 0)
790195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
791195801Smav		} else {
792195801Smav			if (estatus == SIIS_P_CMDERR_SENDFIS ||
793195801Smav			    estatus == SIIS_P_CMDERR_INCSTATE ||
794195801Smav			    estatus == SIIS_P_CMDERR_PPE ||
795195801Smav			    estatus == SIIS_P_CMDERR_SERVICE) {
796195801Smav				et = SIIS_ERR_SATA;
797195801Smav			} else
798195801Smav				et = SIIS_ERR_INVALID;
799195801Smav			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
800195801Smav				/* XXX: reqests in loading state. */
801195801Smav				if (((ch->rslots >> i) & 1) == 0)
802195801Smav					continue;
803195801Smav				siis_end_transaction(&ch->slot[i], et);
804195801Smav			}
805195801Smav		}
806195801Smav	}
807195801Smav}
808195801Smav
809195801Smav/* Must be called with channel locked. */
810195801Smavstatic int
811195801Smavsiis_check_collision(device_t dev, union ccb *ccb)
812195801Smav{
813195801Smav	struct siis_channel *ch = device_get_softc(dev);
814195801Smav
815195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
816195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
817195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
818195801Smav		/* Atomic command while anything active. */
819195801Smav		if (ch->numrslots != 0)
820195801Smav			return (1);
821195801Smav	}
822195801Smav       /* We have some atomic command running. */
823195801Smav       if (ch->aslots != 0)
824195801Smav               return (1);
825195801Smav	return (0);
826195801Smav}
827195801Smav
828195801Smav/* Must be called with channel locked. */
829195801Smavstatic void
830195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb)
831195801Smav{
832195801Smav	struct siis_channel *ch = device_get_softc(dev);
833195801Smav	struct siis_slot *slot;
834195801Smav	int tag;
835195801Smav
836195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
837195801Smav	/* Choose empty slot. */
838195801Smav	tag = ch->lastslot;
839195801Smav	do {
840195801Smav		tag++;
841195801Smav		if (tag >= SIIS_MAX_SLOTS)
842195801Smav			tag = 0;
843195801Smav		if (ch->slot[tag].state == SIIS_SLOT_EMPTY)
844195801Smav			break;
845195801Smav	} while (tag != ch->lastslot);
846195801Smav	if (ch->slot[tag].state != SIIS_SLOT_EMPTY)
847195801Smav		device_printf(ch->dev, "ALL SLOTS BUSY!\n");
848195801Smav	ch->lastslot = tag;
849195801Smav	/* Occupy chosen slot. */
850195801Smav	slot = &ch->slot[tag];
851195801Smav	slot->ccb = ccb;
852195801Smav	/* Update channel stats. */
853195801Smav	ch->numrslots++;
854195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
855195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
856195801Smav		ch->numtslots[ccb->ccb_h.target_id]++;
857195801Smav	}
858195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
859195801Smav	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
860195801Smav		ch->aslots |= (1 << slot->slot);
861195801Smav	slot->dma.nsegs = 0;
862195801Smav	/* If request moves data, setup and load SG list */
863195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
864195801Smav		void *buf;
865195801Smav		bus_size_t size;
866195801Smav
867195801Smav		slot->state = SIIS_SLOT_LOADING;
868195801Smav		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
869195801Smav			buf = ccb->ataio.data_ptr;
870195801Smav			size = ccb->ataio.dxfer_len;
871195801Smav		} else {
872195801Smav			buf = ccb->csio.data_ptr;
873195801Smav			size = ccb->csio.dxfer_len;
874195801Smav		}
875195801Smav		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
876195801Smav		    buf, size, siis_dmasetprd, slot, 0);
877195801Smav	} else
878195801Smav		siis_execute_transaction(slot);
879195801Smav}
880195801Smav
881195801Smav/* Locked by busdma engine. */
882195801Smavstatic void
883195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
884195801Smav{
885195801Smav	struct siis_slot *slot = arg;
886195801Smav	struct siis_channel *ch = device_get_softc(slot->dev);
887195801Smav	struct siis_cmd *ctp;
888195801Smav	struct siis_dma_prd *prd;
889195801Smav	int i;
890195801Smav
891195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
892195801Smav	if (error) {
893195801Smav		device_printf(slot->dev, "DMA load error\n");
894195801Smav		if (!ch->readlog)
895195801Smav			xpt_freeze_simq(ch->sim, 1);
896195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
897195801Smav		return;
898195801Smav	}
899195801Smav	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
900195801Smav	/* Get a piece of the workspace for this request */
901195801Smav	ctp = (struct siis_cmd *)
902195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
903195801Smav	/* Fill S/G table */
904195801Smav	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
905195801Smav		prd = &ctp->u.ata.prd[0];
906195801Smav	else
907195801Smav		prd = &ctp->u.atapi.prd[0];
908195801Smav	for (i = 0; i < nsegs; i++) {
909195801Smav		prd[i].dba = htole64(segs[i].ds_addr);
910195801Smav		prd[i].dbc = htole32(segs[i].ds_len);
911195801Smav		prd[i].control = 0;
912195801Smav	}
913195801Smav	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
914195801Smav	slot->dma.nsegs = nsegs;
915195801Smav	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
916195801Smav	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
917195801Smav	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
918195801Smav	siis_execute_transaction(slot);
919195801Smav}
920195801Smav
921195801Smav/* Must be called with channel locked. */
922195801Smavstatic void
923195801Smavsiis_execute_transaction(struct siis_slot *slot)
924195801Smav{
925195801Smav	device_t dev = slot->dev;
926195801Smav	struct siis_channel *ch = device_get_softc(dev);
927195801Smav	struct siis_cmd *ctp;
928195801Smav	union ccb *ccb = slot->ccb;
929195801Smav	u_int64_t prb_bus;
930195801Smav
931195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
932195801Smav	/* Get a piece of the workspace for this request */
933195801Smav	ctp = (struct siis_cmd *)
934195801Smav		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
935195801Smav	ctp->control = 0;
936195801Smav	ctp->protocol_override = 0;
937195801Smav	ctp->transfer_count = 0;
938195801Smav	/* Special handling for Soft Reset command. */
939195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
940195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
941195801Smav		ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
942195801Smav	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
943195801Smav		if (ccb->ccb_h.flags & CAM_DIR_IN)
944195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
945195801Smav		if (ccb->ccb_h.flags & CAM_DIR_OUT)
946195801Smav			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
947195801Smav	}
948195801Smav	/* Setup the FIS for this request */
949195801Smav	if (!siis_setup_fis(ctp, ccb, slot->slot)) {
950195801Smav		device_printf(ch->dev, "Setting up SATA FIS failed\n");
951195801Smav		if (!ch->readlog)
952195801Smav			xpt_freeze_simq(ch->sim, 1);
953195801Smav		siis_end_transaction(slot, SIIS_ERR_INVALID);
954195801Smav		return;
955195801Smav	}
956195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
957195801Smav	    BUS_DMASYNC_PREWRITE);
958195801Smav	/* Issue command to the controller. */
959195801Smav	slot->state = SIIS_SLOT_RUNNING;
960195801Smav	ch->rslots |= (1 << slot->slot);
961195801Smav	prb_bus = ch->dma.work_bus +
962195801Smav	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
963195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
964195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
965195801Smav	/* Start command execution timeout */
966195801Smav	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
967195801Smav	    (timeout_t*)siis_timeout, slot);
968195801Smav	return;
969195801Smav}
970195801Smav
971195801Smav/* Locked by callout mechanism. */
972195801Smavstatic void
973195801Smavsiis_timeout(struct siis_slot *slot)
974195801Smav{
975195801Smav	device_t dev = slot->dev;
976195801Smav	struct siis_channel *ch = device_get_softc(dev);
977195801Smav	int i;
978195801Smav
979195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
980195801Smav	device_printf(dev, "Timeout on slot %d\n", slot->slot);
981195801Smavdevice_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
982195801Smav    __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
983195801Smav    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
984195801Smav    ATA_INL(ch->r_mem, SIIS_P_SERR));
985195801Smav	/* Kick controller into sane state. */
986195801Smav	siis_portinit(ch->dev);
987195801Smav
988195801Smav	if (!ch->readlog)
989195801Smav		xpt_freeze_simq(ch->sim, ch->numrslots);
990197838Smav	/* Handle frozen command. */
991195801Smav	if (ch->frozen) {
992195801Smav		union ccb *fccb = ch->frozen;
993195801Smav		ch->frozen = NULL;
994198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
995198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
996198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
997198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
998198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
999198321Smav		}
1000195801Smav		xpt_done(fccb);
1001195801Smav	}
1002197838Smav	/* Handle command with timeout. */
1003197838Smav	siis_end_transaction(&ch->slot[slot->slot], SIIS_ERR_TIMEOUT);
1004197838Smav	/* Handle the rest of commands. */
1005195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1006195801Smav		/* Do we have a running request on slot? */
1007195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1008195801Smav			continue;
1009195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1010195801Smav	}
1011195801Smav}
1012195801Smav
1013195801Smav/* Must be called with channel locked. */
1014195801Smavstatic void
1015195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1016195801Smav{
1017195801Smav	device_t dev = slot->dev;
1018195801Smav	struct siis_channel *ch = device_get_softc(dev);
1019195801Smav	union ccb *ccb = slot->ccb;
1020195801Smav
1021195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1022195801Smav	/* Cancel command execution timeout */
1023195801Smav	callout_stop(&slot->timeout);
1024195801Smav	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1025195801Smav	    BUS_DMASYNC_POSTWRITE);
1026195801Smav	/* Read result registers to the result struct
1027195801Smav	 * May be incorrect if several commands finished same time,
1028195801Smav	 * so read only when sure or have to.
1029195801Smav	 */
1030195801Smav	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1031195801Smav		struct ata_res *res = &ccb->ataio.res;
1032195801Smav		if ((et == SIIS_ERR_TFE) ||
1033195801Smav		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1034195801Smav			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1035195801Smav
1036195801Smav			res->status = ATA_INB(ch->r_mem, offs + 2);
1037195801Smav			res->error = ATA_INB(ch->r_mem, offs + 3);
1038195801Smav			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1039195801Smav			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1040195801Smav			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1041195801Smav			res->device = ATA_INB(ch->r_mem, offs + 7);
1042195801Smav			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1043195801Smav			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1044195801Smav			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1045195801Smav			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1046195801Smav			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1047195801Smav		} else
1048195801Smav			bzero(res, sizeof(*res));
1049195801Smav	}
1050195801Smav	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1051195801Smav		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1052195801Smav		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1053195801Smav		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1054195801Smav		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1055195801Smav	}
1056195801Smav	/* Set proper result status. */
1057195801Smav	if (et != SIIS_ERR_NONE || ch->recovery) {
1058195801Smav		ch->eslots |= (1 << slot->slot);
1059195801Smav		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1060195801Smav	}
1061198321Smav	/* In case of error, freeze device for proper recovery. */
1062198321Smav	if (et != SIIS_ERR_NONE &&
1063198321Smav	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1064198321Smav		xpt_freeze_devq(ccb->ccb_h.path, 1);
1065198321Smav		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1066198321Smav	}
1067198321Smav	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1068195801Smav	switch (et) {
1069195801Smav	case SIIS_ERR_NONE:
1070195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP;
1071195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1072195801Smav			ccb->csio.scsi_status = SCSI_STATUS_OK;
1073195801Smav		break;
1074195801Smav	case SIIS_ERR_INVALID:
1075195801Smav		ccb->ccb_h.status |= CAM_REQ_INVALID;
1076195801Smav		break;
1077195801Smav	case SIIS_ERR_INNOCENT:
1078195801Smav		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1079195801Smav		break;
1080195801Smav	case SIIS_ERR_TFE:
1081198321Smav	case SIIS_ERR_NCQ:
1082195801Smav		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1083195801Smav			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1084195801Smav			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1085195801Smav		} else {
1086195801Smav			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1087195801Smav		}
1088195801Smav		break;
1089195801Smav	case SIIS_ERR_SATA:
1090195801Smav		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1091195801Smav		break;
1092195801Smav	case SIIS_ERR_TIMEOUT:
1093195801Smav		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1094195801Smav		break;
1095195801Smav	default:
1096195801Smav		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1097195801Smav	}
1098195801Smav	/* Free slot. */
1099195801Smav	ch->rslots &= ~(1 << slot->slot);
1100195801Smav	ch->aslots &= ~(1 << slot->slot);
1101195801Smav	slot->state = SIIS_SLOT_EMPTY;
1102195801Smav	slot->ccb = NULL;
1103195801Smav	/* Update channel stats. */
1104195801Smav	ch->numrslots--;
1105195801Smav	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1106195801Smav	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1107195801Smav		ch->numtslots[ccb->ccb_h.target_id]--;
1108195801Smav	}
1109195801Smav	/* If it was NCQ command error, put result on hold. */
1110195801Smav	if (et == SIIS_ERR_NCQ) {
1111195801Smav		ch->hold[slot->slot] = ccb;
1112195801Smav		ch->numhslots++;
1113195801Smav	} else if (ch->readlog)	/* If it was our READ LOG command - process it. */
1114195801Smav		siis_process_read_log(dev, ccb);
1115195801Smav	else
1116195801Smav		xpt_done(ccb);
1117195801Smav	/* Unfreeze frozen command. */
1118195801Smav	if (ch->frozen && ch->numrslots == 0) {
1119195801Smav		union ccb *fccb = ch->frozen;
1120195801Smav		ch->frozen = NULL;
1121195801Smav		siis_begin_transaction(dev, fccb);
1122195801Smav		xpt_release_simq(ch->sim, TRUE);
1123195801Smav	}
1124195801Smav	/* If we have no other active commands, ... */
1125195801Smav	if (ch->rslots == 0) {
1126195801Smav		/* if we have slots in error, we can reinit port. */
1127195801Smav		if (ch->eslots != 0)
1128195801Smav			siis_portinit(dev);
1129195801Smav		/* if there commands on hold, we can do READ LOG. */
1130195801Smav		if (!ch->readlog && ch->numhslots)
1131195801Smav			siis_issue_read_log(dev);
1132195801Smav	}
1133195801Smav}
1134195801Smav
1135195801Smavstatic void
1136195801Smavsiis_issue_read_log(device_t dev)
1137195801Smav{
1138195801Smav	struct siis_channel *ch = device_get_softc(dev);
1139195801Smav	union ccb *ccb;
1140195801Smav	struct ccb_ataio *ataio;
1141195801Smav	int i;
1142195801Smav
1143195801Smav	/* Find some holden command. */
1144195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1145195801Smav		if (ch->hold[i])
1146195801Smav			break;
1147195801Smav	}
1148195801Smav	if (i == SIIS_MAX_SLOTS)
1149195801Smav		return;
1150195801Smav	ch->readlog = 1;
1151195801Smav	ccb = xpt_alloc_ccb_nowait();
1152195801Smav	if (ccb == NULL) {
1153195801Smav		device_printf(dev, "Unable allocate READ LOG command");
1154195801Smav		return; /* XXX */
1155195801Smav	}
1156195801Smav	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1157195801Smav	ccb->ccb_h.func_code = XPT_ATA_IO;
1158195801Smav	ccb->ccb_h.flags = CAM_DIR_IN;
1159195801Smav	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1160195801Smav	ataio = &ccb->ataio;
1161195801Smav	ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1162195801Smav	if (ataio->data_ptr == NULL) {
1163195801Smav		device_printf(dev, "Unable allocate memory for READ LOG command");
1164195801Smav		return; /* XXX */
1165195801Smav	}
1166195801Smav	ataio->dxfer_len = 512;
1167195801Smav	bzero(&ataio->cmd, sizeof(ataio->cmd));
1168195801Smav	ataio->cmd.flags = CAM_ATAIO_48BIT;
1169195801Smav	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1170195801Smav	ataio->cmd.sector_count = 1;
1171195801Smav	ataio->cmd.sector_count_exp = 0;
1172195801Smav	ataio->cmd.lba_low = 0x10;
1173195801Smav	ataio->cmd.lba_mid = 0;
1174195801Smav	ataio->cmd.lba_mid_exp = 0;
1175195801Smav	siis_begin_transaction(dev, ccb);
1176195801Smav}
1177195801Smav
1178195801Smavstatic void
1179195801Smavsiis_process_read_log(device_t dev, union ccb *ccb)
1180195801Smav{
1181195801Smav	struct siis_channel *ch = device_get_softc(dev);
1182195801Smav	uint8_t *data;
1183195801Smav	struct ata_res *res;
1184195801Smav	int i;
1185195801Smav
1186195801Smav	ch->readlog = 0;
1187195801Smav	data = ccb->ataio.data_ptr;
1188195801Smav	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1189195801Smav	    (data[0] & 0x80) == 0) {
1190195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1191195801Smav			if (!ch->hold[i])
1192195801Smav				continue;
1193195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1194195801Smav				continue;
1195195801Smav			if ((data[0] & 0x1F) == i) {
1196195801Smav				res = &ch->hold[i]->ataio.res;
1197195801Smav				res->status = data[2];
1198195801Smav				res->error = data[3];
1199195801Smav				res->lba_low = data[4];
1200195801Smav				res->lba_mid = data[5];
1201195801Smav				res->lba_high = data[6];
1202195801Smav				res->device = data[7];
1203195801Smav				res->lba_low_exp = data[8];
1204195801Smav				res->lba_mid_exp = data[9];
1205195801Smav				res->lba_high_exp = data[10];
1206195801Smav				res->sector_count = data[12];
1207195801Smav				res->sector_count_exp = data[13];
1208195801Smav			} else {
1209195801Smav				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1210195801Smav				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1211195801Smav			}
1212195801Smav			xpt_done(ch->hold[i]);
1213195801Smav			ch->hold[i] = NULL;
1214195801Smav			ch->numhslots--;
1215195801Smav		}
1216195801Smav	} else {
1217195801Smav		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1218195801Smav			device_printf(dev, "Error while READ LOG EXT\n");
1219195801Smav		else if ((data[0] & 0x80) == 0) {
1220195801Smav			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1221195801Smav		}
1222195801Smav		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1223195801Smav			if (!ch->hold[i])
1224195801Smav				continue;
1225195801Smav			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1226195801Smav				continue;
1227195801Smav			xpt_done(ch->hold[i]);
1228195801Smav			ch->hold[i] = NULL;
1229195801Smav			ch->numhslots--;
1230195801Smav		}
1231195801Smav	}
1232195801Smav	free(ccb->ataio.data_ptr, M_SIIS);
1233195801Smav	xpt_free_ccb(ccb);
1234195801Smav}
1235195801Smav
1236195801Smavstatic void
1237195801Smavsiis_portinit(device_t dev)
1238195801Smav{
1239195801Smav	struct siis_channel *ch = device_get_softc(dev);
1240195801Smav	int i;
1241195801Smav
1242195801Smav	ch->eslots = 0;
1243195801Smav	ch->recovery = 0;
1244195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1245195801Smav	for (i = 0; i < 16; i++) {
1246195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1247195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1248195801Smav	}
1249195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1250195801Smav	siis_wait_ready(dev, 1000);
1251195801Smav}
1252195801Smav
1253195801Smav#if 0
1254195801Smavstatic void
1255195801Smavsiis_devreset(device_t dev)
1256195801Smav{
1257195801Smav	struct siis_channel *ch = device_get_softc(dev);
1258195801Smav
1259195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1260195801Smav	siis_wait_ready(dev, 1000);
1261195801Smav}
1262195801Smav#endif
1263195801Smav
1264195801Smavstatic int
1265195801Smavsiis_wait_ready(device_t dev, int t)
1266195801Smav{
1267195801Smav	struct siis_channel *ch = device_get_softc(dev);
1268195801Smav	int timeout = 0;
1269195801Smav	uint32_t val;
1270195801Smav
1271195801Smav	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1272195801Smav	    SIIS_P_CTL_READY) == 0) {
1273195801Smav		DELAY(1000);
1274195801Smav		if (timeout++ > t) {
1275195801Smav			device_printf(dev, "port is not ready (timeout %dms) "
1276195801Smav			    "status = %08x\n", t, val);
1277195801Smav			return (EBUSY);
1278195801Smav		}
1279195801Smav	}
1280195801Smav	if (bootverbose)
1281195801Smav		device_printf(dev, "ready wait time=%dms\n", timeout);
1282195801Smav	return (0);
1283195801Smav}
1284195801Smav
1285195801Smavstatic void
1286195801Smavsiis_reset(device_t dev)
1287195801Smav{
1288195801Smav	struct siis_channel *ch = device_get_softc(dev);
1289195801Smav	int i;
1290195801Smav
1291195801Smav	if (bootverbose)
1292195801Smav		device_printf(dev, "SIIS reset...\n");
1293195801Smav	xpt_freeze_simq(ch->sim, ch->numrslots);
1294195801Smav	/* Requeue freezed command. */
1295195801Smav	if (ch->frozen) {
1296195801Smav		union ccb *fccb = ch->frozen;
1297195801Smav		ch->frozen = NULL;
1298198321Smav		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1299198321Smav		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1300198321Smav		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1301198321Smav			xpt_freeze_devq(fccb->ccb_h.path, 1);
1302198321Smav			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1303198321Smav		}
1304195801Smav		xpt_done(fccb);
1305195801Smav	}
1306195801Smav	/* Disable port interrupts */
1307195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1308195801Smav	/* Kill the engine and requeue all running commands. */
1309195801Smav	siis_portinit(dev);
1310195801Smav	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1311195801Smav		/* Do we have a running request on slot? */
1312195801Smav		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1313195801Smav			continue;
1314195801Smav		/* XXX; Commands in loading state. */
1315195801Smav		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1316195801Smav	}
1317195801Smav	/* Reset and reconnect PHY, */
1318195801Smav	if (!siis_sata_phy_reset(dev)) {
1319195801Smav		ch->devices = 0;
1320195801Smav		/* Enable port interrupts */
1321195801Smav		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1322195801Smav		if (bootverbose)
1323195801Smav			device_printf(dev,
1324195801Smav			    "SIIS reset done: phy reset found no device\n");
1325195801Smav		/* Tell the XPT about the event */
1326195801Smav		xpt_async(AC_BUS_RESET, ch->path, NULL);
1327195801Smav		return;
1328195801Smav	}
1329195801Smav	/* Wait for clearing busy status. */
1330195801Smav	if (siis_wait_ready(dev, 10000)) {
1331195801Smav		device_printf(dev, "device ready timeout\n");
1332195801Smav	}
1333195801Smav	ch->devices = 1;
1334195801Smav	/* Enable port interrupts */
1335195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1336195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1337195801Smav	if (bootverbose)
1338195801Smav		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1339195801Smav	/* Tell the XPT about the event */
1340195801Smav	xpt_async(AC_BUS_RESET, ch->path, NULL);
1341195801Smav}
1342195801Smav
1343195801Smavstatic int
1344195801Smavsiis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag)
1345195801Smav{
1346195801Smav	u_int8_t *fis = &ctp->fis[0];
1347195801Smav
1348195801Smav	bzero(fis, 24);
1349195801Smav	fis[0] = 0x27;  		/* host to device */
1350195801Smav	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1351195801Smav	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1352195801Smav		fis[1] |= 0x80;
1353195801Smav		fis[2] = ATA_PACKET_CMD;
1354195801Smav		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1355195801Smav			fis[3] = ATA_F_DMA;
1356195801Smav		else {
1357195801Smav			fis[5] = ccb->csio.dxfer_len;
1358195801Smav		        fis[6] = ccb->csio.dxfer_len >> 8;
1359195801Smav		}
1360195801Smav		fis[7] = ATA_D_LBA;
1361195801Smav		fis[15] = ATA_A_4BIT;
1362195801Smav		bzero(ctp->u.atapi.ccb, 16);
1363195801Smav		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1364195801Smav		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1365195801Smav		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1366195801Smav	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1367195801Smav		fis[1] |= 0x80;
1368195801Smav		fis[2] = ccb->ataio.cmd.command;
1369195801Smav		fis[3] = ccb->ataio.cmd.features;
1370195801Smav		fis[4] = ccb->ataio.cmd.lba_low;
1371195801Smav		fis[5] = ccb->ataio.cmd.lba_mid;
1372195801Smav		fis[6] = ccb->ataio.cmd.lba_high;
1373195801Smav		fis[7] = ccb->ataio.cmd.device;
1374195801Smav		fis[8] = ccb->ataio.cmd.lba_low_exp;
1375195801Smav		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1376195801Smav		fis[10] = ccb->ataio.cmd.lba_high_exp;
1377195801Smav		fis[11] = ccb->ataio.cmd.features_exp;
1378195801Smav		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1379195801Smav			fis[12] = tag << 3;
1380195801Smav			fis[13] = 0;
1381195801Smav		} else {
1382195801Smav			fis[12] = ccb->ataio.cmd.sector_count;
1383195801Smav			fis[13] = ccb->ataio.cmd.sector_count_exp;
1384195801Smav		}
1385195801Smav		fis[15] = ATA_A_4BIT;
1386195801Smav	} else {
1387195801Smav		/* Soft reset. */
1388195801Smav	}
1389195801Smav	return (20);
1390195801Smav}
1391195801Smav
1392195801Smavstatic int
1393195801Smavsiis_sata_connect(struct siis_channel *ch)
1394195801Smav{
1395195801Smav	u_int32_t status;
1396195801Smav	int timeout;
1397195801Smav
1398195801Smav	/* Wait up to 100ms for "connect well" */
1399195801Smav	for (timeout = 0; timeout < 100 ; timeout++) {
1400195801Smav		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1401195801Smav		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1402195801Smav		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1403195801Smav		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1404195801Smav			break;
1405195801Smav		DELAY(1000);
1406195801Smav	}
1407195801Smav	if (timeout >= 100) {
1408195801Smav		if (bootverbose) {
1409195801Smav			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1410195801Smav			    status);
1411195801Smav		}
1412195801Smav		return (0);
1413195801Smav	}
1414195801Smav	if (bootverbose) {
1415195801Smav		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1416195801Smav		    timeout, status);
1417195801Smav	}
1418195801Smav	/* Clear SATA error register */
1419195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1420195801Smav	return (1);
1421195801Smav}
1422195801Smav
1423195801Smavstatic int
1424195801Smavsiis_sata_phy_reset(device_t dev)
1425195801Smav{
1426195801Smav	struct siis_channel *ch = device_get_softc(dev);
1427195801Smav	uint32_t val;
1428195801Smav
1429195801Smav	if (bootverbose)
1430195801Smav		device_printf(dev, "hardware reset ...\n");
1431195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL, ATA_SC_IPM_DIS_PARTIAL |
1432195801Smav	    ATA_SC_IPM_DIS_SLUMBER | ATA_SC_DET_RESET);
1433195801Smav	DELAY(50000);
1434195801Smav	if (ch->sata_rev == 1)
1435195801Smav		val = ATA_SC_SPD_SPEED_GEN1;
1436195801Smav	else if (ch->sata_rev == 2)
1437195801Smav		val = ATA_SC_SPD_SPEED_GEN2;
1438195801Smav	else if (ch->sata_rev == 3)
1439195801Smav		val = ATA_SC_SPD_SPEED_GEN3;
1440195801Smav	else
1441195801Smav		val = 0;
1442195801Smav	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1443195801Smav	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1444195801Smav	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1445195801Smav	DELAY(50000);
1446195801Smav	return (siis_sata_connect(ch));
1447195801Smav}
1448195801Smav
1449195801Smavstatic void
1450195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb)
1451195801Smav{
1452195801Smav	device_t dev;
1453195801Smav	struct siis_channel *ch;
1454195801Smav
1455195801Smav	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1456195801Smav	    ccb->ccb_h.func_code));
1457195801Smav
1458195801Smav	ch = (struct siis_channel *)cam_sim_softc(sim);
1459195801Smav	dev = ch->dev;
1460195801Smav	mtx_assert(&ch->mtx, MA_OWNED);
1461195801Smav	switch (ccb->ccb_h.func_code) {
1462195801Smav	/* Common cases first */
1463195801Smav	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1464195801Smav	case XPT_SCSI_IO:
1465195801Smav		if (ch->devices == 0) {
1466195801Smav			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1467195801Smav			xpt_done(ccb);
1468195801Smav			break;
1469195801Smav		}
1470195801Smav		/* Check for command collision. */
1471195801Smav		if (siis_check_collision(dev, ccb)) {
1472195801Smav			/* Freeze command. */
1473195801Smav			ch->frozen = ccb;
1474195801Smav			/* We have only one frozen slot, so freeze simq also. */
1475195801Smav			xpt_freeze_simq(ch->sim, 1);
1476195801Smav			return;
1477195801Smav		}
1478195801Smav		siis_begin_transaction(dev, ccb);
1479195801Smav		break;
1480195801Smav	case XPT_EN_LUN:		/* Enable LUN as a target */
1481195801Smav	case XPT_TARGET_IO:		/* Execute target I/O request */
1482195801Smav	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1483195801Smav	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1484195801Smav	case XPT_ABORT:			/* Abort the specified CCB */
1485195801Smav		/* XXX Implement */
1486195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1487195801Smav		xpt_done(ccb);
1488195801Smav		break;
1489195801Smav	case XPT_SET_TRAN_SETTINGS:
1490195801Smav	{
1491195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1492195801Smav
1493195801Smav		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1494195801Smav			if (cts->xport_specific.sata.pm_present)
1495195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1496195801Smav			else
1497195801Smav				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1498195801Smav		}
1499195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1500195801Smav		xpt_done(ccb);
1501195801Smav		break;
1502195801Smav	}
1503195801Smav	case XPT_GET_TRAN_SETTINGS:
1504195801Smav	/* Get default/user set transfer settings for the target */
1505195801Smav	{
1506195801Smav		struct	ccb_trans_settings *cts = &ccb->cts;
1507195801Smav		uint32_t status;
1508195801Smav
1509195801Smav		cts->protocol = PROTO_ATA;
1510196655Smav		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1511195801Smav		cts->transport = XPORT_SATA;
1512196655Smav		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1513195801Smav		cts->proto_specific.valid = 0;
1514195801Smav		cts->xport_specific.sata.valid = 0;
1515195801Smav		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1516195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1517195801Smav		else
1518195801Smav			status = ATA_INL(ch->r_mem, SIIS_P_SCTL) & ATA_SC_SPD_MASK;
1519195801Smav		if (status & ATA_SS_SPD_GEN3) {
1520195801Smav			cts->xport_specific.sata.bitrate = 600000;
1521195801Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1522195801Smav		} else if (status & ATA_SS_SPD_GEN2) {
1523195801Smav			cts->xport_specific.sata.bitrate = 300000;
1524195801Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1525195801Smav		} else if (status & ATA_SS_SPD_GEN1) {
1526195801Smav			cts->xport_specific.sata.bitrate = 150000;
1527195801Smav			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1528195801Smav		}
1529195801Smav		cts->xport_specific.sata.pm_present =
1530195801Smav			(ATA_INL(ch->r_mem, SIIS_P_STS) & SIIS_P_CTL_PME) ?
1531195801Smav			    1 : 0;
1532195801Smav		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1533195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1534195801Smav		xpt_done(ccb);
1535195801Smav		break;
1536195801Smav	}
1537195801Smav#if 0
1538195801Smav	case XPT_CALC_GEOMETRY:
1539195801Smav	{
1540195801Smav		struct	  ccb_calc_geometry *ccg;
1541195801Smav		uint32_t size_mb;
1542195801Smav		uint32_t secs_per_cylinder;
1543195801Smav
1544195801Smav		ccg = &ccb->ccg;
1545195801Smav		size_mb = ccg->volume_size
1546195801Smav			/ ((1024L * 1024L) / ccg->block_size);
1547195801Smav		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1548195801Smav			if (size_mb >= 2048) {
1549195801Smav				ccg->heads = 255;
1550195801Smav				ccg->secs_per_track = 63;
1551195801Smav			} else {
1552195801Smav				ccg->heads = 128;
1553195801Smav				ccg->secs_per_track = 32;
1554195801Smav			}
1555195801Smav		} else {
1556195801Smav			ccg->heads = 64;
1557195801Smav			ccg->secs_per_track = 32;
1558195801Smav		}
1559195801Smav		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1560195801Smav		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1561195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1562195801Smav		xpt_done(ccb);
1563195801Smav		break;
1564195801Smav	}
1565195801Smav#endif
1566195801Smav	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1567195801Smav	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1568195801Smav		siis_reset(dev);
1569195801Smav		ccb->ccb_h.status = CAM_REQ_CMP;
1570195801Smav		xpt_done(ccb);
1571195801Smav		break;
1572195801Smav	case XPT_TERM_IO:		/* Terminate the I/O process */
1573195801Smav		/* XXX Implement */
1574195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1575195801Smav		xpt_done(ccb);
1576195801Smav		break;
1577195801Smav	case XPT_PATH_INQ:		/* Path routing inquiry */
1578195801Smav	{
1579195801Smav		struct ccb_pathinq *cpi = &ccb->cpi;
1580195801Smav
1581195801Smav		cpi->version_num = 1; /* XXX??? */
1582195801Smav		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1583195801Smav		cpi->hba_inquiry |= PI_SATAPM;
1584195801Smav		cpi->target_sprt = 0;
1585195801Smav		cpi->hba_misc = PIM_SEQSCAN;
1586195801Smav		cpi->hba_eng_cnt = 0;
1587198322Smav		cpi->max_target = 15;
1588195801Smav		cpi->max_lun = 0;
1589195801Smav		cpi->initiator_id = 0;
1590195801Smav		cpi->bus_id = cam_sim_bus(sim);
1591195801Smav		cpi->base_transfer_speed = 150000;
1592195801Smav		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1593195801Smav		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1594195801Smav		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1595195801Smav		cpi->unit_number = cam_sim_unit(sim);
1596195801Smav		cpi->transport = XPORT_SATA;
1597196655Smav		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1598195801Smav		cpi->protocol = PROTO_ATA;
1599196655Smav		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1600195801Smav		cpi->ccb_h.status = CAM_REQ_CMP;
1601195801Smav		cpi->maxio = MAXPHYS;
1602195801Smav		xpt_done(ccb);
1603195801Smav		break;
1604195801Smav	}
1605195801Smav	default:
1606195801Smav		ccb->ccb_h.status = CAM_REQ_INVALID;
1607195801Smav		xpt_done(ccb);
1608195801Smav		break;
1609195801Smav	}
1610195801Smav}
1611195801Smav
1612195801Smavstatic void
1613195801Smavsiispoll(struct cam_sim *sim)
1614195801Smav{
1615195801Smav	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1616195801Smav
1617195801Smav	siis_ch_intr(ch->dev);
1618195801Smav}
1619