xref: /freebsd/sys/dev/siis/siis.c (revision dd0b4fb6d50631c140e800bdfac48d02e7e4c875)
167b87e44SAlexander Motin /*-
267b87e44SAlexander Motin  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
367b87e44SAlexander Motin  * All rights reserved.
467b87e44SAlexander Motin  *
567b87e44SAlexander Motin  * Redistribution and use in source and binary forms, with or without
667b87e44SAlexander Motin  * modification, are permitted provided that the following conditions
767b87e44SAlexander Motin  * are met:
867b87e44SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
967b87e44SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
1067b87e44SAlexander Motin  *    without modification, immediately at the beginning of the file.
1167b87e44SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1267b87e44SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1367b87e44SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1467b87e44SAlexander Motin  *
1567b87e44SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1667b87e44SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1767b87e44SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1867b87e44SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1967b87e44SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2067b87e44SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2167b87e44SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2267b87e44SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2367b87e44SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2467b87e44SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2567b87e44SAlexander Motin  */
2667b87e44SAlexander Motin 
2767b87e44SAlexander Motin #include <sys/cdefs.h>
2867b87e44SAlexander Motin __FBSDID("$FreeBSD$");
2967b87e44SAlexander Motin 
3067b87e44SAlexander Motin #include <sys/param.h>
3167b87e44SAlexander Motin #include <sys/module.h>
3267b87e44SAlexander Motin #include <sys/systm.h>
3367b87e44SAlexander Motin #include <sys/kernel.h>
3467b87e44SAlexander Motin #include <sys/ata.h>
3567b87e44SAlexander Motin #include <sys/bus.h>
3667b87e44SAlexander Motin #include <sys/endian.h>
3767b87e44SAlexander Motin #include <sys/malloc.h>
3867b87e44SAlexander Motin #include <sys/lock.h>
3967b87e44SAlexander Motin #include <sys/mutex.h>
4067b87e44SAlexander Motin #include <sys/sema.h>
4167b87e44SAlexander Motin #include <sys/taskqueue.h>
4267b87e44SAlexander Motin #include <vm/uma.h>
4367b87e44SAlexander Motin #include <machine/stdarg.h>
4467b87e44SAlexander Motin #include <machine/resource.h>
4567b87e44SAlexander Motin #include <machine/bus.h>
4667b87e44SAlexander Motin #include <sys/rman.h>
47a59641a9SAlexander Motin #include <dev/led/led.h>
4867b87e44SAlexander Motin #include <dev/pci/pcivar.h>
4967b87e44SAlexander Motin #include <dev/pci/pcireg.h>
5067b87e44SAlexander Motin #include "siis.h"
5167b87e44SAlexander Motin 
5267b87e44SAlexander Motin #include <cam/cam.h>
5367b87e44SAlexander Motin #include <cam/cam_ccb.h>
5467b87e44SAlexander Motin #include <cam/cam_sim.h>
5567b87e44SAlexander Motin #include <cam/cam_xpt_sim.h>
5667b87e44SAlexander Motin #include <cam/cam_debug.h>
5767b87e44SAlexander Motin 
5867b87e44SAlexander Motin /* local prototypes */
5967b87e44SAlexander Motin static int siis_setup_interrupt(device_t dev);
6067b87e44SAlexander Motin static void siis_intr(void *data);
6167b87e44SAlexander Motin static int siis_suspend(device_t dev);
6267b87e44SAlexander Motin static int siis_resume(device_t dev);
63243e0fb9SAlexander Motin static int siis_ch_init(device_t dev);
64243e0fb9SAlexander Motin static int siis_ch_deinit(device_t dev);
6567b87e44SAlexander Motin static int siis_ch_suspend(device_t dev);
6667b87e44SAlexander Motin static int siis_ch_resume(device_t dev);
6767b87e44SAlexander Motin static void siis_ch_intr_locked(void *data);
6867b87e44SAlexander Motin static void siis_ch_intr(void *data);
69a59641a9SAlexander Motin static void siis_ch_led(void *priv, int onoff);
7067b87e44SAlexander Motin static void siis_begin_transaction(device_t dev, union ccb *ccb);
7167b87e44SAlexander Motin static void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
7267b87e44SAlexander Motin static void siis_execute_transaction(struct siis_slot *slot);
7367b87e44SAlexander Motin static void siis_timeout(struct siis_slot *slot);
7467b87e44SAlexander Motin static void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
75b447e682SAlexander Motin static int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag);
7667b87e44SAlexander Motin static void siis_dmainit(device_t dev);
7767b87e44SAlexander Motin static void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
7867b87e44SAlexander Motin static void siis_dmafini(device_t dev);
7967b87e44SAlexander Motin static void siis_slotsalloc(device_t dev);
8067b87e44SAlexander Motin static void siis_slotsfree(device_t dev);
8167b87e44SAlexander Motin static void siis_reset(device_t dev);
8267b87e44SAlexander Motin static void siis_portinit(device_t dev);
8367b87e44SAlexander Motin static int siis_wait_ready(device_t dev, int t);
8467b87e44SAlexander Motin 
8567b87e44SAlexander Motin static int siis_sata_connect(struct siis_channel *ch);
8667b87e44SAlexander Motin 
87b8b7a902SAlexander Motin static void siis_issue_recovery(device_t dev);
8867b87e44SAlexander Motin static void siis_process_read_log(device_t dev, union ccb *ccb);
89b8b7a902SAlexander Motin static void siis_process_request_sense(device_t dev, union ccb *ccb);
9067b87e44SAlexander Motin 
9167b87e44SAlexander Motin static void siisaction(struct cam_sim *sim, union ccb *ccb);
9267b87e44SAlexander Motin static void siispoll(struct cam_sim *sim);
9367b87e44SAlexander Motin 
94d745c852SEd Schouten static MALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
9567b87e44SAlexander Motin 
9603a6387eSAlexander Motin static struct {
9703a6387eSAlexander Motin 	uint32_t	id;
9803a6387eSAlexander Motin 	const char	*name;
9903a6387eSAlexander Motin 	int		ports;
100d095fa04SAlexander Motin 	int		quirks;
101d095fa04SAlexander Motin #define SIIS_Q_SNTF	1
102f81100fdSAlexander Motin #define SIIS_Q_NOMSI	2
10303a6387eSAlexander Motin } siis_ids[] = {
104d095fa04SAlexander Motin 	{0x31241095,	"SiI3124",	4,	0},
105d095fa04SAlexander Motin 	{0x31248086,	"SiI3124",	4,	0},
106f81100fdSAlexander Motin 	{0x31321095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
107f81100fdSAlexander Motin 	{0x02421095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
108f81100fdSAlexander Motin 	{0x02441095,	"SiI3132",	2,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
109f81100fdSAlexander Motin 	{0x31311095,	"SiI3131",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
110f81100fdSAlexander Motin 	{0x35311095,	"SiI3531",	1,	SIIS_Q_SNTF|SIIS_Q_NOMSI},
111d095fa04SAlexander Motin 	{0,		NULL,		0,	0}
11203a6387eSAlexander Motin };
11303a6387eSAlexander Motin 
114b8b7a902SAlexander Motin #define recovery_type		spriv_field0
115b8b7a902SAlexander Motin #define RECOVERY_NONE		0
116b8b7a902SAlexander Motin #define RECOVERY_READ_LOG	1
117b8b7a902SAlexander Motin #define RECOVERY_REQUEST_SENSE	2
118b8b7a902SAlexander Motin #define recovery_slot		spriv_field1
119b8b7a902SAlexander Motin 
12067b87e44SAlexander Motin static int
12167b87e44SAlexander Motin siis_probe(device_t dev)
12267b87e44SAlexander Motin {
12303a6387eSAlexander Motin 	char buf[64];
12403a6387eSAlexander Motin 	int i;
12567b87e44SAlexander Motin 	uint32_t devid = pci_get_devid(dev);
12667b87e44SAlexander Motin 
12703a6387eSAlexander Motin 	for (i = 0; siis_ids[i].id != 0; i++) {
12803a6387eSAlexander Motin 		if (siis_ids[i].id == devid) {
129d095fa04SAlexander Motin 			snprintf(buf, sizeof(buf), "%s SATA controller",
13003a6387eSAlexander Motin 			    siis_ids[i].name);
13103a6387eSAlexander Motin 			device_set_desc_copy(dev, buf);
13267b87e44SAlexander Motin 			return (BUS_PROBE_VENDOR);
13367b87e44SAlexander Motin 		}
13403a6387eSAlexander Motin 	}
13503a6387eSAlexander Motin 	return (ENXIO);
13603a6387eSAlexander Motin }
13767b87e44SAlexander Motin 
13867b87e44SAlexander Motin static int
13967b87e44SAlexander Motin siis_attach(device_t dev)
14067b87e44SAlexander Motin {
14167b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
14267b87e44SAlexander Motin 	uint32_t devid = pci_get_devid(dev);
14367b87e44SAlexander Motin 	device_t child;
14403a6387eSAlexander Motin 	int	error, i, unit;
14567b87e44SAlexander Motin 
146d095fa04SAlexander Motin 	ctlr->dev = dev;
14703a6387eSAlexander Motin 	for (i = 0; siis_ids[i].id != 0; i++) {
14803a6387eSAlexander Motin 		if (siis_ids[i].id == devid)
14903a6387eSAlexander Motin 			break;
15003a6387eSAlexander Motin 	}
151d095fa04SAlexander Motin 	ctlr->quirks = siis_ids[i].quirks;
15267b87e44SAlexander Motin 	/* Global memory */
15367b87e44SAlexander Motin 	ctlr->r_grid = PCIR_BAR(0);
15467b87e44SAlexander Motin 	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
15567b87e44SAlexander Motin 	    &ctlr->r_grid, RF_ACTIVE)))
15667b87e44SAlexander Motin 		return (ENXIO);
15730053681SAlexander Motin 	ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL);
15867b87e44SAlexander Motin 	/* Channels memory */
15967b87e44SAlexander Motin 	ctlr->r_rid = PCIR_BAR(2);
16067b87e44SAlexander Motin 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
16167b87e44SAlexander Motin 	    &ctlr->r_rid, RF_ACTIVE)))
16267b87e44SAlexander Motin 		return (ENXIO);
16367b87e44SAlexander Motin 	/* Setup our own memory management for channels. */
164cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
165cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
16667b87e44SAlexander Motin 	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
16767b87e44SAlexander Motin 	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
16867b87e44SAlexander Motin 	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
16967b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
17067b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
17167b87e44SAlexander Motin 		return (error);
17267b87e44SAlexander Motin 	}
17367b87e44SAlexander Motin 	if ((error = rman_manage_region(&ctlr->sc_iomem,
17467b87e44SAlexander Motin 	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
17567b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
17667b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
17767b87e44SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
17867b87e44SAlexander Motin 		return (error);
17967b87e44SAlexander Motin 	}
18094410af7SAlexander Motin 	pci_enable_busmaster(dev);
18167b87e44SAlexander Motin 	/* Reset controller */
18267b87e44SAlexander Motin 	siis_resume(dev);
18367b87e44SAlexander Motin 	/* Number of HW channels */
18403a6387eSAlexander Motin 	ctlr->channels = siis_ids[i].ports;
18567b87e44SAlexander Motin 	/* Setup interrupts. */
18667b87e44SAlexander Motin 	if (siis_setup_interrupt(dev)) {
18767b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
18867b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
18967b87e44SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
19067b87e44SAlexander Motin 		return ENXIO;
19167b87e44SAlexander Motin 	}
19267b87e44SAlexander Motin 	/* Attach all channels on this controller */
19367b87e44SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
19467b87e44SAlexander Motin 		child = device_add_child(dev, "siisch", -1);
19567b87e44SAlexander Motin 		if (child == NULL)
19667b87e44SAlexander Motin 			device_printf(dev, "failed to add channel device\n");
19767b87e44SAlexander Motin 		else
19867b87e44SAlexander Motin 			device_set_ivars(child, (void *)(intptr_t)unit);
19967b87e44SAlexander Motin 	}
20067b87e44SAlexander Motin 	bus_generic_attach(dev);
20167b87e44SAlexander Motin 	return 0;
20267b87e44SAlexander Motin }
20367b87e44SAlexander Motin 
20467b87e44SAlexander Motin static int
20567b87e44SAlexander Motin siis_detach(device_t dev)
20667b87e44SAlexander Motin {
20767b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
20867b87e44SAlexander Motin 
20967b87e44SAlexander Motin 	/* Detach & delete all children */
2103b12bdb5SHans Petter Selasky 	device_delete_children(dev);
21111bcf702SHans Petter Selasky 
21267b87e44SAlexander Motin 	/* Free interrupts. */
21367b87e44SAlexander Motin 	if (ctlr->irq.r_irq) {
21467b87e44SAlexander Motin 		bus_teardown_intr(dev, ctlr->irq.r_irq,
21567b87e44SAlexander Motin 		    ctlr->irq.handle);
21667b87e44SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
21767b87e44SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
21867b87e44SAlexander Motin 	}
21967b87e44SAlexander Motin 	pci_release_msi(dev);
22067b87e44SAlexander Motin 	/* Free memory. */
22167b87e44SAlexander Motin 	rman_fini(&ctlr->sc_iomem);
22267b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
22367b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
22467b87e44SAlexander Motin 	return (0);
22567b87e44SAlexander Motin }
22667b87e44SAlexander Motin 
22767b87e44SAlexander Motin static int
22867b87e44SAlexander Motin siis_suspend(device_t dev)
22967b87e44SAlexander Motin {
23067b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
23167b87e44SAlexander Motin 
23267b87e44SAlexander Motin 	bus_generic_suspend(dev);
23367b87e44SAlexander Motin 	/* Put controller into reset state. */
23430053681SAlexander Motin 	ctlr->gctl |= SIIS_GCTL_GRESET;
23530053681SAlexander Motin 	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
23667b87e44SAlexander Motin 	return 0;
23767b87e44SAlexander Motin }
23867b87e44SAlexander Motin 
23967b87e44SAlexander Motin static int
24067b87e44SAlexander Motin siis_resume(device_t dev)
24167b87e44SAlexander Motin {
24267b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
24367b87e44SAlexander Motin 
2441535d59dSAlexander Motin 	/* Set PCIe max read request size to at least 1024 bytes */
2452622a559SAlexander Motin 	if (pci_get_max_read_req(dev) < 1024)
2462622a559SAlexander Motin 		pci_set_max_read_req(dev, 1024);
24767b87e44SAlexander Motin 	/* Put controller into reset state. */
24830053681SAlexander Motin 	ctlr->gctl |= SIIS_GCTL_GRESET;
24930053681SAlexander Motin 	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
25067b87e44SAlexander Motin 	DELAY(10000);
25167b87e44SAlexander Motin 	/* Get controller out of reset state and enable port interrupts. */
25230053681SAlexander Motin 	ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE);
25330053681SAlexander Motin 	ctlr->gctl |= 0x0000000f;
25430053681SAlexander Motin 	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl);
25567b87e44SAlexander Motin 	return (bus_generic_resume(dev));
25667b87e44SAlexander Motin }
25767b87e44SAlexander Motin 
25867b87e44SAlexander Motin static int
25967b87e44SAlexander Motin siis_setup_interrupt(device_t dev)
26067b87e44SAlexander Motin {
26167b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
262f81100fdSAlexander Motin 	int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1;
26367b87e44SAlexander Motin 
26467b87e44SAlexander Motin 	/* Process hints. */
26567b87e44SAlexander Motin 	resource_int_value(device_get_name(dev),
26667b87e44SAlexander Motin 	    device_get_unit(dev), "msi", &msi);
26767b87e44SAlexander Motin 	if (msi < 0)
26867b87e44SAlexander Motin 		msi = 0;
26967b87e44SAlexander Motin 	else if (msi > 0)
27067b87e44SAlexander Motin 		msi = min(1, pci_msi_count(dev));
27167b87e44SAlexander Motin 	/* Allocate MSI if needed/present. */
27267b87e44SAlexander Motin 	if (msi && pci_alloc_msi(dev, &msi) != 0)
27367b87e44SAlexander Motin 		msi = 0;
27467b87e44SAlexander Motin 	/* Allocate all IRQs. */
27567b87e44SAlexander Motin 	ctlr->irq.r_irq_rid = msi ? 1 : 0;
27667b87e44SAlexander Motin 	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
27767b87e44SAlexander Motin 	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
27867b87e44SAlexander Motin 		device_printf(dev, "unable to map interrupt\n");
27967b87e44SAlexander Motin 		return ENXIO;
28067b87e44SAlexander Motin 	}
28167b87e44SAlexander Motin 	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
28267b87e44SAlexander Motin 	    siis_intr, ctlr, &ctlr->irq.handle))) {
28367b87e44SAlexander Motin 		/* SOS XXX release r_irq */
28467b87e44SAlexander Motin 		device_printf(dev, "unable to setup interrupt\n");
28567b87e44SAlexander Motin 		return ENXIO;
28667b87e44SAlexander Motin 	}
28767b87e44SAlexander Motin 	return (0);
28867b87e44SAlexander Motin }
28967b87e44SAlexander Motin 
29067b87e44SAlexander Motin /*
29167b87e44SAlexander Motin  * Common case interrupt handler.
29267b87e44SAlexander Motin  */
29367b87e44SAlexander Motin static void
29467b87e44SAlexander Motin siis_intr(void *data)
29567b87e44SAlexander Motin {
29667b87e44SAlexander Motin 	struct siis_controller *ctlr = (struct siis_controller *)data;
29767b87e44SAlexander Motin 	u_int32_t is;
29867b87e44SAlexander Motin 	void *arg;
29967b87e44SAlexander Motin 	int unit;
30067b87e44SAlexander Motin 
30167b87e44SAlexander Motin 	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
30267b87e44SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
30367b87e44SAlexander Motin 		if ((is & SIIS_IS_PORT(unit)) != 0 &&
30467b87e44SAlexander Motin 		    (arg = ctlr->interrupt[unit].argument)) {
30567b87e44SAlexander Motin 			ctlr->interrupt[unit].function(arg);
30667b87e44SAlexander Motin 		}
30767b87e44SAlexander Motin 	}
30830053681SAlexander Motin 	/* Acknowledge interrupt, if MSI enabled. */
30930053681SAlexander Motin 	if (ctlr->irq.r_irq_rid) {
31030053681SAlexander Motin 		ATA_OUTL(ctlr->r_gmem, SIIS_GCTL,
31130053681SAlexander Motin 		    ctlr->gctl | SIIS_GCTL_MSIACK);
31230053681SAlexander Motin 	}
31367b87e44SAlexander Motin }
31467b87e44SAlexander Motin 
31567b87e44SAlexander Motin static struct resource *
31667b87e44SAlexander Motin siis_alloc_resource(device_t dev, device_t child, int type, int *rid,
31767b87e44SAlexander Motin 		       u_long start, u_long end, u_long count, u_int flags)
31867b87e44SAlexander Motin {
31967b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
32067b87e44SAlexander Motin 	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
32167b87e44SAlexander Motin 	struct resource *res = NULL;
32267b87e44SAlexander Motin 	int offset = unit << 13;
32367b87e44SAlexander Motin 	long st;
32467b87e44SAlexander Motin 
32567b87e44SAlexander Motin 	switch (type) {
32667b87e44SAlexander Motin 	case SYS_RES_MEMORY:
32767b87e44SAlexander Motin 		st = rman_get_start(ctlr->r_mem);
32867b87e44SAlexander Motin 		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
32967b87e44SAlexander Motin 		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
33067b87e44SAlexander Motin 		if (res) {
33167b87e44SAlexander Motin 			bus_space_handle_t bsh;
33267b87e44SAlexander Motin 			bus_space_tag_t bst;
33367b87e44SAlexander Motin 			bsh = rman_get_bushandle(ctlr->r_mem);
33467b87e44SAlexander Motin 			bst = rman_get_bustag(ctlr->r_mem);
33567b87e44SAlexander Motin 			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
33667b87e44SAlexander Motin 			rman_set_bushandle(res, bsh);
33767b87e44SAlexander Motin 			rman_set_bustag(res, bst);
33867b87e44SAlexander Motin 		}
33967b87e44SAlexander Motin 		break;
34067b87e44SAlexander Motin 	case SYS_RES_IRQ:
34167b87e44SAlexander Motin 		if (*rid == ATA_IRQ_RID)
34267b87e44SAlexander Motin 			res = ctlr->irq.r_irq;
34367b87e44SAlexander Motin 		break;
34467b87e44SAlexander Motin 	}
34567b87e44SAlexander Motin 	return (res);
34667b87e44SAlexander Motin }
34767b87e44SAlexander Motin 
34867b87e44SAlexander Motin static int
34967b87e44SAlexander Motin siis_release_resource(device_t dev, device_t child, int type, int rid,
35067b87e44SAlexander Motin 			 struct resource *r)
35167b87e44SAlexander Motin {
35267b87e44SAlexander Motin 
35367b87e44SAlexander Motin 	switch (type) {
35467b87e44SAlexander Motin 	case SYS_RES_MEMORY:
35567b87e44SAlexander Motin 		rman_release_resource(r);
35667b87e44SAlexander Motin 		return (0);
35767b87e44SAlexander Motin 	case SYS_RES_IRQ:
35867b87e44SAlexander Motin 		if (rid != ATA_IRQ_RID)
35967b87e44SAlexander Motin 			return ENOENT;
36067b87e44SAlexander Motin 		return (0);
36167b87e44SAlexander Motin 	}
36267b87e44SAlexander Motin 	return (EINVAL);
36367b87e44SAlexander Motin }
36467b87e44SAlexander Motin 
36567b87e44SAlexander Motin static int
36667b87e44SAlexander Motin siis_setup_intr(device_t dev, device_t child, struct resource *irq,
36767b87e44SAlexander Motin 		   int flags, driver_filter_t *filter, driver_intr_t *function,
36867b87e44SAlexander Motin 		   void *argument, void **cookiep)
36967b87e44SAlexander Motin {
37067b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
37167b87e44SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
37267b87e44SAlexander Motin 
37367b87e44SAlexander Motin 	if (filter != NULL) {
37467b87e44SAlexander Motin 		printf("siis.c: we cannot use a filter here\n");
37567b87e44SAlexander Motin 		return (EINVAL);
37667b87e44SAlexander Motin 	}
37767b87e44SAlexander Motin 	ctlr->interrupt[unit].function = function;
37867b87e44SAlexander Motin 	ctlr->interrupt[unit].argument = argument;
37967b87e44SAlexander Motin 	return (0);
38067b87e44SAlexander Motin }
38167b87e44SAlexander Motin 
38267b87e44SAlexander Motin static int
38367b87e44SAlexander Motin siis_teardown_intr(device_t dev, device_t child, struct resource *irq,
38467b87e44SAlexander Motin 		      void *cookie)
38567b87e44SAlexander Motin {
38667b87e44SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(dev);
38767b87e44SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
38867b87e44SAlexander Motin 
38967b87e44SAlexander Motin 	ctlr->interrupt[unit].function = NULL;
39067b87e44SAlexander Motin 	ctlr->interrupt[unit].argument = NULL;
39167b87e44SAlexander Motin 	return (0);
39267b87e44SAlexander Motin }
39367b87e44SAlexander Motin 
39467b87e44SAlexander Motin static int
39567b87e44SAlexander Motin siis_print_child(device_t dev, device_t child)
39667b87e44SAlexander Motin {
39767b87e44SAlexander Motin 	int retval;
39867b87e44SAlexander Motin 
39967b87e44SAlexander Motin 	retval = bus_print_child_header(dev, child);
40067b87e44SAlexander Motin 	retval += printf(" at channel %d",
40167b87e44SAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
40267b87e44SAlexander Motin 	retval += bus_print_child_footer(dev, child);
40367b87e44SAlexander Motin 
40467b87e44SAlexander Motin 	return (retval);
40567b87e44SAlexander Motin }
40667b87e44SAlexander Motin 
407445cc79cSAlexander Motin static int
408445cc79cSAlexander Motin siis_child_location_str(device_t dev, device_t child, char *buf,
409445cc79cSAlexander Motin     size_t buflen)
410445cc79cSAlexander Motin {
411445cc79cSAlexander Motin 
412445cc79cSAlexander Motin 	snprintf(buf, buflen, "channel=%d",
413445cc79cSAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
414445cc79cSAlexander Motin 	return (0);
415445cc79cSAlexander Motin }
416445cc79cSAlexander Motin 
41767b87e44SAlexander Motin devclass_t siis_devclass;
41867b87e44SAlexander Motin static device_method_t siis_methods[] = {
41967b87e44SAlexander Motin 	DEVMETHOD(device_probe,     siis_probe),
42067b87e44SAlexander Motin 	DEVMETHOD(device_attach,    siis_attach),
42167b87e44SAlexander Motin 	DEVMETHOD(device_detach,    siis_detach),
42267b87e44SAlexander Motin 	DEVMETHOD(device_suspend,   siis_suspend),
42367b87e44SAlexander Motin 	DEVMETHOD(device_resume,    siis_resume),
42467b87e44SAlexander Motin 	DEVMETHOD(bus_print_child,  siis_print_child),
42567b87e44SAlexander Motin 	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
42667b87e44SAlexander Motin 	DEVMETHOD(bus_release_resource,     siis_release_resource),
42767b87e44SAlexander Motin 	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
42867b87e44SAlexander Motin 	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
429445cc79cSAlexander Motin 	DEVMETHOD(bus_child_location_str, siis_child_location_str),
43067b87e44SAlexander Motin 	{ 0, 0 }
43167b87e44SAlexander Motin };
43267b87e44SAlexander Motin static driver_t siis_driver = {
43367b87e44SAlexander Motin         "siis",
43467b87e44SAlexander Motin         siis_methods,
43567b87e44SAlexander Motin         sizeof(struct siis_controller)
43667b87e44SAlexander Motin };
43767b87e44SAlexander Motin DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
43867b87e44SAlexander Motin MODULE_VERSION(siis, 1);
43967b87e44SAlexander Motin MODULE_DEPEND(siis, cam, 1, 1, 1);
44067b87e44SAlexander Motin 
44167b87e44SAlexander Motin static int
44267b87e44SAlexander Motin siis_ch_probe(device_t dev)
44367b87e44SAlexander Motin {
44467b87e44SAlexander Motin 
44567b87e44SAlexander Motin 	device_set_desc_copy(dev, "SIIS channel");
44667b87e44SAlexander Motin 	return (0);
44767b87e44SAlexander Motin }
44867b87e44SAlexander Motin 
44967b87e44SAlexander Motin static int
45067b87e44SAlexander Motin siis_ch_attach(device_t dev)
45167b87e44SAlexander Motin {
452d095fa04SAlexander Motin 	struct siis_controller *ctlr = device_get_softc(device_get_parent(dev));
45367b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
45467b87e44SAlexander Motin 	struct cam_devq *devq;
455b447e682SAlexander Motin 	int rid, error, i, sata_rev = 0;
45667b87e44SAlexander Motin 
45767b87e44SAlexander Motin 	ch->dev = dev;
45867b87e44SAlexander Motin 	ch->unit = (intptr_t)device_get_ivars(dev);
459d095fa04SAlexander Motin 	ch->quirks = ctlr->quirks;
46067b87e44SAlexander Motin 	resource_int_value(device_get_name(dev),
46167b87e44SAlexander Motin 	    device_get_unit(dev), "pm_level", &ch->pm_level);
462b447e682SAlexander Motin 	resource_int_value(device_get_name(dev),
463b447e682SAlexander Motin 	    device_get_unit(dev), "sata_rev", &sata_rev);
464c8039fc6SAlexander Motin 	for (i = 0; i < 16; i++) {
465b447e682SAlexander Motin 		ch->user[i].revision = sata_rev;
466c8039fc6SAlexander Motin 		ch->user[i].mode = 0;
467c8039fc6SAlexander Motin 		ch->user[i].bytecount = 8192;
468c8039fc6SAlexander Motin 		ch->user[i].tags = SIIS_MAX_SLOTS;
469c8039fc6SAlexander Motin 		ch->curr[i] = ch->user[i];
470da6808c1SAlexander Motin 		if (ch->pm_level)
471da6808c1SAlexander Motin 			ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ;
4728d169381SAlexander Motin 		ch->user[i].caps |= CTS_SATA_CAPS_H_AN;
473c8039fc6SAlexander Motin 	}
47467b87e44SAlexander Motin 	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
47567b87e44SAlexander Motin 	rid = ch->unit;
47667b87e44SAlexander Motin 	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
47767b87e44SAlexander Motin 	    &rid, RF_ACTIVE)))
47867b87e44SAlexander Motin 		return (ENXIO);
47967b87e44SAlexander Motin 	siis_dmainit(dev);
48067b87e44SAlexander Motin 	siis_slotsalloc(dev);
481243e0fb9SAlexander Motin 	siis_ch_init(dev);
48267b87e44SAlexander Motin 	mtx_lock(&ch->mtx);
48367b87e44SAlexander Motin 	rid = ATA_IRQ_RID;
48467b87e44SAlexander Motin 	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
48567b87e44SAlexander Motin 	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
48667b87e44SAlexander Motin 		device_printf(dev, "Unable to map interrupt\n");
487b1364652SAlexander Motin 		error = ENXIO;
488b1364652SAlexander Motin 		goto err0;
48967b87e44SAlexander Motin 	}
49067b87e44SAlexander Motin 	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
49167b87e44SAlexander Motin 	    siis_ch_intr_locked, dev, &ch->ih))) {
49267b87e44SAlexander Motin 		device_printf(dev, "Unable to setup interrupt\n");
49367b87e44SAlexander Motin 		error = ENXIO;
49467b87e44SAlexander Motin 		goto err1;
49567b87e44SAlexander Motin 	}
49667b87e44SAlexander Motin 	/* Create the device queue for our SIM. */
49767b87e44SAlexander Motin 	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
49867b87e44SAlexander Motin 	if (devq == NULL) {
49967b87e44SAlexander Motin 		device_printf(dev, "Unable to allocate simq\n");
50067b87e44SAlexander Motin 		error = ENOMEM;
50167b87e44SAlexander Motin 		goto err1;
50267b87e44SAlexander Motin 	}
50367b87e44SAlexander Motin 	/* Construct SIM entry */
50467b87e44SAlexander Motin 	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
50530a4094fSAlexander Motin 	    device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq);
50667b87e44SAlexander Motin 	if (ch->sim == NULL) {
507b1364652SAlexander Motin 		cam_simq_free(devq);
50867b87e44SAlexander Motin 		device_printf(dev, "unable to allocate sim\n");
50967b87e44SAlexander Motin 		error = ENOMEM;
510b1364652SAlexander Motin 		goto err1;
51167b87e44SAlexander Motin 	}
51267b87e44SAlexander Motin 	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
51367b87e44SAlexander Motin 		device_printf(dev, "unable to register xpt bus\n");
51467b87e44SAlexander Motin 		error = ENXIO;
51567b87e44SAlexander Motin 		goto err2;
51667b87e44SAlexander Motin 	}
51767b87e44SAlexander Motin 	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
51867b87e44SAlexander Motin 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
51967b87e44SAlexander Motin 		device_printf(dev, "unable to create path\n");
52067b87e44SAlexander Motin 		error = ENXIO;
52167b87e44SAlexander Motin 		goto err3;
52267b87e44SAlexander Motin 	}
52367b87e44SAlexander Motin 	mtx_unlock(&ch->mtx);
524a59641a9SAlexander Motin 	ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev));
52567b87e44SAlexander Motin 	return (0);
52667b87e44SAlexander Motin 
52767b87e44SAlexander Motin err3:
52867b87e44SAlexander Motin 	xpt_bus_deregister(cam_sim_path(ch->sim));
52967b87e44SAlexander Motin err2:
53067b87e44SAlexander Motin 	cam_sim_free(ch->sim, /*free_devq*/TRUE);
53167b87e44SAlexander Motin err1:
53267b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
533b1364652SAlexander Motin err0:
53467b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
53567b87e44SAlexander Motin 	mtx_unlock(&ch->mtx);
5365b9392e8SAlexander Motin 	mtx_destroy(&ch->mtx);
53767b87e44SAlexander Motin 	return (error);
53867b87e44SAlexander Motin }
53967b87e44SAlexander Motin 
54067b87e44SAlexander Motin static int
54167b87e44SAlexander Motin siis_ch_detach(device_t dev)
54267b87e44SAlexander Motin {
54367b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
54467b87e44SAlexander Motin 
545a59641a9SAlexander Motin 	led_destroy(ch->led);
54667b87e44SAlexander Motin 	mtx_lock(&ch->mtx);
54767b87e44SAlexander Motin 	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
54867b87e44SAlexander Motin 	xpt_free_path(ch->path);
54967b87e44SAlexander Motin 	xpt_bus_deregister(cam_sim_path(ch->sim));
55067b87e44SAlexander Motin 	cam_sim_free(ch->sim, /*free_devq*/TRUE);
55167b87e44SAlexander Motin 	mtx_unlock(&ch->mtx);
55267b87e44SAlexander Motin 
55367b87e44SAlexander Motin 	bus_teardown_intr(dev, ch->r_irq, ch->ih);
55467b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
55567b87e44SAlexander Motin 
556243e0fb9SAlexander Motin 	siis_ch_deinit(dev);
55767b87e44SAlexander Motin 	siis_slotsfree(dev);
55867b87e44SAlexander Motin 	siis_dmafini(dev);
55967b87e44SAlexander Motin 
56067b87e44SAlexander Motin 	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
56167b87e44SAlexander Motin 	mtx_destroy(&ch->mtx);
56267b87e44SAlexander Motin 	return (0);
56367b87e44SAlexander Motin }
56467b87e44SAlexander Motin 
56567b87e44SAlexander Motin static int
566243e0fb9SAlexander Motin siis_ch_init(device_t dev)
56767b87e44SAlexander Motin {
56867b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
56967b87e44SAlexander Motin 
57067b87e44SAlexander Motin 	/* Get port out of reset state. */
57167b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
57267b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
5736f9a51c7SAlexander Motin 	if (ch->pm_present)
5746f9a51c7SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
5756f9a51c7SAlexander Motin 	else
57667b87e44SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
57767b87e44SAlexander Motin 	/* Enable port interrupts */
57867b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
57967b87e44SAlexander Motin 	return (0);
58067b87e44SAlexander Motin }
58167b87e44SAlexander Motin 
582243e0fb9SAlexander Motin static int
583243e0fb9SAlexander Motin siis_ch_deinit(device_t dev)
584243e0fb9SAlexander Motin {
585243e0fb9SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
586243e0fb9SAlexander Motin 
587243e0fb9SAlexander Motin 	/* Put port into reset state. */
588243e0fb9SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
589243e0fb9SAlexander Motin 	return (0);
590243e0fb9SAlexander Motin }
591243e0fb9SAlexander Motin 
592243e0fb9SAlexander Motin static int
593243e0fb9SAlexander Motin siis_ch_suspend(device_t dev)
594243e0fb9SAlexander Motin {
595243e0fb9SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
596243e0fb9SAlexander Motin 
597243e0fb9SAlexander Motin 	mtx_lock(&ch->mtx);
598243e0fb9SAlexander Motin 	xpt_freeze_simq(ch->sim, 1);
599243e0fb9SAlexander Motin 	while (ch->oslots)
600243e0fb9SAlexander Motin 		msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100);
601243e0fb9SAlexander Motin 	siis_ch_deinit(dev);
602243e0fb9SAlexander Motin 	mtx_unlock(&ch->mtx);
603243e0fb9SAlexander Motin 	return (0);
604243e0fb9SAlexander Motin }
605243e0fb9SAlexander Motin 
606243e0fb9SAlexander Motin static int
607243e0fb9SAlexander Motin siis_ch_resume(device_t dev)
608243e0fb9SAlexander Motin {
609243e0fb9SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
610243e0fb9SAlexander Motin 
611243e0fb9SAlexander Motin 	mtx_lock(&ch->mtx);
612243e0fb9SAlexander Motin 	siis_ch_init(dev);
613243e0fb9SAlexander Motin 	siis_reset(dev);
614243e0fb9SAlexander Motin 	xpt_release_simq(ch->sim, TRUE);
615243e0fb9SAlexander Motin 	mtx_unlock(&ch->mtx);
616243e0fb9SAlexander Motin 	return (0);
617243e0fb9SAlexander Motin }
618243e0fb9SAlexander Motin 
61967b87e44SAlexander Motin devclass_t siisch_devclass;
62067b87e44SAlexander Motin static device_method_t siisch_methods[] = {
62167b87e44SAlexander Motin 	DEVMETHOD(device_probe,     siis_ch_probe),
62267b87e44SAlexander Motin 	DEVMETHOD(device_attach,    siis_ch_attach),
62367b87e44SAlexander Motin 	DEVMETHOD(device_detach,    siis_ch_detach),
62467b87e44SAlexander Motin 	DEVMETHOD(device_suspend,   siis_ch_suspend),
62567b87e44SAlexander Motin 	DEVMETHOD(device_resume,    siis_ch_resume),
62667b87e44SAlexander Motin 	{ 0, 0 }
62767b87e44SAlexander Motin };
62867b87e44SAlexander Motin static driver_t siisch_driver = {
62967b87e44SAlexander Motin         "siisch",
63067b87e44SAlexander Motin         siisch_methods,
63167b87e44SAlexander Motin         sizeof(struct siis_channel)
63267b87e44SAlexander Motin };
63367b87e44SAlexander Motin DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
63467b87e44SAlexander Motin 
635a59641a9SAlexander Motin static void
636a59641a9SAlexander Motin siis_ch_led(void *priv, int onoff)
637a59641a9SAlexander Motin {
638a59641a9SAlexander Motin 	device_t dev;
639a59641a9SAlexander Motin 	struct siis_channel *ch;
640a59641a9SAlexander Motin 
641a59641a9SAlexander Motin 	dev = (device_t)priv;
642a59641a9SAlexander Motin 	ch = device_get_softc(dev);
643a59641a9SAlexander Motin 
644a59641a9SAlexander Motin 	if (onoff == 0)
645a59641a9SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON);
646a59641a9SAlexander Motin 	else
647a59641a9SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON);
648a59641a9SAlexander Motin }
649a59641a9SAlexander Motin 
65067b87e44SAlexander Motin struct siis_dc_cb_args {
65167b87e44SAlexander Motin 	bus_addr_t maddr;
65267b87e44SAlexander Motin 	int error;
65367b87e44SAlexander Motin };
65467b87e44SAlexander Motin 
65567b87e44SAlexander Motin static void
65667b87e44SAlexander Motin siis_dmainit(device_t dev)
65767b87e44SAlexander Motin {
65867b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
65967b87e44SAlexander Motin 	struct siis_dc_cb_args dcba;
66067b87e44SAlexander Motin 
66167b87e44SAlexander Motin 	/* Command area. */
66267b87e44SAlexander Motin 	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
66367b87e44SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
66467b87e44SAlexander Motin 	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
66567b87e44SAlexander Motin 	    0, NULL, NULL, &ch->dma.work_tag))
66667b87e44SAlexander Motin 		goto error;
66767b87e44SAlexander Motin 	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
66867b87e44SAlexander Motin 	    &ch->dma.work_map))
66967b87e44SAlexander Motin 		goto error;
67067b87e44SAlexander Motin 	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
67167b87e44SAlexander Motin 	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
67267b87e44SAlexander Motin 		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
67367b87e44SAlexander Motin 		goto error;
67467b87e44SAlexander Motin 	}
67567b87e44SAlexander Motin 	ch->dma.work_bus = dcba.maddr;
67667b87e44SAlexander Motin 	/* Data area. */
6775319fb62SAlexander Motin 	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
67867b87e44SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
67967b87e44SAlexander Motin 	    NULL, NULL,
68067b87e44SAlexander Motin 	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
68167b87e44SAlexander Motin 	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
68267b87e44SAlexander Motin 	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
68367b87e44SAlexander Motin 		goto error;
68467b87e44SAlexander Motin 	}
68567b87e44SAlexander Motin 	return;
68667b87e44SAlexander Motin 
68767b87e44SAlexander Motin error:
68867b87e44SAlexander Motin 	device_printf(dev, "WARNING - DMA initialization failed\n");
68967b87e44SAlexander Motin 	siis_dmafini(dev);
69067b87e44SAlexander Motin }
69167b87e44SAlexander Motin 
69267b87e44SAlexander Motin static void
69367b87e44SAlexander Motin siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
69467b87e44SAlexander Motin {
69567b87e44SAlexander Motin 	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
69667b87e44SAlexander Motin 
69767b87e44SAlexander Motin 	if (!(dcba->error = error))
69867b87e44SAlexander Motin 		dcba->maddr = segs[0].ds_addr;
69967b87e44SAlexander Motin }
70067b87e44SAlexander Motin 
70167b87e44SAlexander Motin static void
70267b87e44SAlexander Motin siis_dmafini(device_t dev)
70367b87e44SAlexander Motin {
70467b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
70567b87e44SAlexander Motin 
70667b87e44SAlexander Motin 	if (ch->dma.data_tag) {
70767b87e44SAlexander Motin 		bus_dma_tag_destroy(ch->dma.data_tag);
70867b87e44SAlexander Motin 		ch->dma.data_tag = NULL;
70967b87e44SAlexander Motin 	}
71067b87e44SAlexander Motin 	if (ch->dma.work_bus) {
71167b87e44SAlexander Motin 		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
71267b87e44SAlexander Motin 		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
71367b87e44SAlexander Motin 		ch->dma.work_bus = 0;
71467b87e44SAlexander Motin 		ch->dma.work_map = NULL;
71567b87e44SAlexander Motin 		ch->dma.work = NULL;
71667b87e44SAlexander Motin 	}
71767b87e44SAlexander Motin 	if (ch->dma.work_tag) {
71867b87e44SAlexander Motin 		bus_dma_tag_destroy(ch->dma.work_tag);
71967b87e44SAlexander Motin 		ch->dma.work_tag = NULL;
72067b87e44SAlexander Motin 	}
72167b87e44SAlexander Motin }
72267b87e44SAlexander Motin 
72367b87e44SAlexander Motin static void
72467b87e44SAlexander Motin siis_slotsalloc(device_t dev)
72567b87e44SAlexander Motin {
72667b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
72767b87e44SAlexander Motin 	int i;
72867b87e44SAlexander Motin 
72967b87e44SAlexander Motin 	/* Alloc and setup command/dma slots */
73067b87e44SAlexander Motin 	bzero(ch->slot, sizeof(ch->slot));
73167b87e44SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
73267b87e44SAlexander Motin 		struct siis_slot *slot = &ch->slot[i];
73367b87e44SAlexander Motin 
73467b87e44SAlexander Motin 		slot->dev = dev;
73567b87e44SAlexander Motin 		slot->slot = i;
73667b87e44SAlexander Motin 		slot->state = SIIS_SLOT_EMPTY;
73767b87e44SAlexander Motin 		slot->ccb = NULL;
73867b87e44SAlexander Motin 		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
73967b87e44SAlexander Motin 
74067b87e44SAlexander Motin 		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
74167b87e44SAlexander Motin 			device_printf(ch->dev, "FAILURE - create data_map\n");
74267b87e44SAlexander Motin 	}
74367b87e44SAlexander Motin }
74467b87e44SAlexander Motin 
74567b87e44SAlexander Motin static void
74667b87e44SAlexander Motin siis_slotsfree(device_t dev)
74767b87e44SAlexander Motin {
74867b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
74967b87e44SAlexander Motin 	int i;
75067b87e44SAlexander Motin 
75167b87e44SAlexander Motin 	/* Free all dma slots */
75267b87e44SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
75367b87e44SAlexander Motin 		struct siis_slot *slot = &ch->slot[i];
75467b87e44SAlexander Motin 
755c82b245aSAlexander Motin 		callout_drain(&slot->timeout);
75667b87e44SAlexander Motin 		if (slot->dma.data_map) {
75767b87e44SAlexander Motin 			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
75867b87e44SAlexander Motin 			slot->dma.data_map = NULL;
75967b87e44SAlexander Motin 		}
76067b87e44SAlexander Motin 	}
76167b87e44SAlexander Motin }
76267b87e44SAlexander Motin 
76367b87e44SAlexander Motin static void
76435713642SAlexander Motin siis_notify_events(device_t dev)
76535713642SAlexander Motin {
76635713642SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
76735713642SAlexander Motin 	struct cam_path *dpath;
76835713642SAlexander Motin 	u_int32_t status;
76935713642SAlexander Motin 	int i;
77035713642SAlexander Motin 
771d095fa04SAlexander Motin 	if (ch->quirks & SIIS_Q_SNTF) {
77235713642SAlexander Motin 		status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
77335713642SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
774d095fa04SAlexander Motin 	} else {
775d095fa04SAlexander Motin 		/*
776d095fa04SAlexander Motin 		 * Without SNTF we have no idea which device sent notification.
777d095fa04SAlexander Motin 		 * If PMP is connected, assume it, else - device.
778d095fa04SAlexander Motin 		 */
779d095fa04SAlexander Motin 		status = (ch->pm_present) ? 0x8000 : 0x0001;
780d095fa04SAlexander Motin 	}
78135713642SAlexander Motin 	if (bootverbose)
78235713642SAlexander Motin 		device_printf(dev, "SNTF 0x%04x\n", status);
78335713642SAlexander Motin 	for (i = 0; i < 16; i++) {
78435713642SAlexander Motin 		if ((status & (1 << i)) == 0)
78535713642SAlexander Motin 			continue;
78635713642SAlexander Motin 		if (xpt_create_path(&dpath, NULL,
78735713642SAlexander Motin 		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
78835713642SAlexander Motin 			xpt_async(AC_SCSI_AEN, dpath, NULL);
78935713642SAlexander Motin 			xpt_free_path(dpath);
79035713642SAlexander Motin 		}
79135713642SAlexander Motin 	}
79235713642SAlexander Motin 
79335713642SAlexander Motin }
79435713642SAlexander Motin 
79535713642SAlexander Motin static void
79667b87e44SAlexander Motin siis_phy_check_events(device_t dev)
79767b87e44SAlexander Motin {
79867b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
79967b87e44SAlexander Motin 
80067b87e44SAlexander Motin 	/* If we have a connection event, deal with it */
80167b87e44SAlexander Motin 	if (ch->pm_level == 0) {
80267b87e44SAlexander Motin 		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
80383c5d981SAlexander Motin 		union ccb *ccb;
80483c5d981SAlexander Motin 
805ed70cffdSAlexander Motin 		if (bootverbose) {
80667b87e44SAlexander Motin 			if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
80767b87e44SAlexander Motin 			    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
80867b87e44SAlexander Motin 			    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
80967b87e44SAlexander Motin 				device_printf(dev, "CONNECT requested\n");
810ed70cffdSAlexander Motin 			} else
81167b87e44SAlexander Motin 				device_printf(dev, "DISCONNECT requested\n");
81267b87e44SAlexander Motin 		}
813ed70cffdSAlexander Motin 		siis_reset(dev);
81483c5d981SAlexander Motin 		if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
81583c5d981SAlexander Motin 			return;
81683c5d981SAlexander Motin 		if (xpt_create_path(&ccb->ccb_h.path, NULL,
81783c5d981SAlexander Motin 		    cam_sim_path(ch->sim),
81883c5d981SAlexander Motin 		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
81983c5d981SAlexander Motin 			xpt_free_ccb(ccb);
82083c5d981SAlexander Motin 			return;
82183c5d981SAlexander Motin 		}
82283c5d981SAlexander Motin 		xpt_rescan(ccb);
82367b87e44SAlexander Motin 	}
82467b87e44SAlexander Motin }
82567b87e44SAlexander Motin 
82667b87e44SAlexander Motin static void
82767b87e44SAlexander Motin siis_ch_intr_locked(void *data)
82867b87e44SAlexander Motin {
82967b87e44SAlexander Motin 	device_t dev = (device_t)data;
83067b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
83167b87e44SAlexander Motin 
83267b87e44SAlexander Motin 	mtx_lock(&ch->mtx);
833711f6613SAlexander Motin 	xpt_batch_start(ch->sim);
83467b87e44SAlexander Motin 	siis_ch_intr(data);
835711f6613SAlexander Motin 	xpt_batch_done(ch->sim);
83667b87e44SAlexander Motin 	mtx_unlock(&ch->mtx);
83767b87e44SAlexander Motin }
83867b87e44SAlexander Motin 
83967b87e44SAlexander Motin static void
84067b87e44SAlexander Motin siis_ch_intr(void *data)
84167b87e44SAlexander Motin {
84267b87e44SAlexander Motin 	device_t dev = (device_t)data;
84367b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
84467b87e44SAlexander Motin 	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
84567b87e44SAlexander Motin 	enum siis_err_type et;
84667b87e44SAlexander Motin 	int i, ccs, port, tslots;
84767b87e44SAlexander Motin 
84867b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
84967b87e44SAlexander Motin 	/* Read command statuses. */
85067b87e44SAlexander Motin 	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
85167b87e44SAlexander Motin 	ok = ch->rslots & ~sstatus;
85267b87e44SAlexander Motin 	/* Complete all successfull commands. */
85367b87e44SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
85467b87e44SAlexander Motin 		if ((ok >> i) & 1)
85567b87e44SAlexander Motin 			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
85667b87e44SAlexander Motin 	}
85767b87e44SAlexander Motin 	/* Do we have any other events? */
85867b87e44SAlexander Motin 	if ((sstatus & SIIS_P_SS_ATTN) == 0)
85967b87e44SAlexander Motin 		return;
86067b87e44SAlexander Motin 	/* Read and clear interrupt statuses. */
86167b87e44SAlexander Motin 	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
86267b87e44SAlexander Motin 	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
86367b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
86467b87e44SAlexander Motin 	/* Process PHY events */
86567b87e44SAlexander Motin 	if (istatus & SIIS_P_IX_PHYRDYCHG)
86667b87e44SAlexander Motin 		siis_phy_check_events(dev);
86735713642SAlexander Motin 	/* Process NOTIFY events */
86835713642SAlexander Motin 	if (istatus & SIIS_P_IX_SDBN)
86935713642SAlexander Motin 		siis_notify_events(dev);
87067b87e44SAlexander Motin 	/* Process command errors */
87167b87e44SAlexander Motin 	if (istatus & SIIS_P_IX_COMMERR) {
87267b87e44SAlexander Motin 		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
87367b87e44SAlexander Motin 		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
87467b87e44SAlexander Motin 		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
87567b87e44SAlexander Motin 		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
87667b87e44SAlexander Motin 		err = ch->rslots & sstatus;
87767b87e44SAlexander Motin //device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
87867b87e44SAlexander Motin //    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
87967b87e44SAlexander Motin //    ATA_INL(ch->r_mem, SIIS_P_SERR));
88067b87e44SAlexander Motin 
881b8b7a902SAlexander Motin 		if (!ch->recoverycmd && !ch->recovery) {
88267b87e44SAlexander Motin 			xpt_freeze_simq(ch->sim, ch->numrslots);
88367b87e44SAlexander Motin 			ch->recovery = 1;
88467b87e44SAlexander Motin 		}
88567b87e44SAlexander Motin 		if (ch->frozen) {
88667b87e44SAlexander Motin 			union ccb *fccb = ch->frozen;
88767b87e44SAlexander Motin 			ch->frozen = NULL;
888e06db192SAlexander Motin 			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
889e06db192SAlexander Motin 			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
890e06db192SAlexander Motin 			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
891e06db192SAlexander Motin 				xpt_freeze_devq(fccb->ccb_h.path, 1);
892e06db192SAlexander Motin 				fccb->ccb_h.status |= CAM_DEV_QFRZN;
893e06db192SAlexander Motin 			}
89467b87e44SAlexander Motin 			xpt_done(fccb);
89567b87e44SAlexander Motin 		}
89667b87e44SAlexander Motin 		if (estatus == SIIS_P_CMDERR_DEV ||
89767b87e44SAlexander Motin 		    estatus == SIIS_P_CMDERR_SDB ||
89867b87e44SAlexander Motin 		    estatus == SIIS_P_CMDERR_DATAFIS) {
89967b87e44SAlexander Motin 			tslots = ch->numtslots[port];
90067b87e44SAlexander Motin 			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
9016f9a51c7SAlexander Motin 				/* XXX: requests in loading state. */
90267b87e44SAlexander Motin 				if (((ch->rslots >> i) & 1) == 0)
90367b87e44SAlexander Motin 					continue;
90467b87e44SAlexander Motin 				if (ch->slot[i].ccb->ccb_h.target_id != port)
90567b87e44SAlexander Motin 					continue;
90667b87e44SAlexander Motin 				if (tslots == 0) {
90767b87e44SAlexander Motin 					/* Untagged operation. */
90867b87e44SAlexander Motin 					if (i == ccs)
90967b87e44SAlexander Motin 						et = SIIS_ERR_TFE;
91067b87e44SAlexander Motin 					else
91167b87e44SAlexander Motin 						et = SIIS_ERR_INNOCENT;
91267b87e44SAlexander Motin 				} else {
91367b87e44SAlexander Motin 					/* Tagged operation. */
91467b87e44SAlexander Motin 					et = SIIS_ERR_NCQ;
91567b87e44SAlexander Motin 				}
91667b87e44SAlexander Motin 				siis_end_transaction(&ch->slot[i], et);
91767b87e44SAlexander Motin 			}
91867b87e44SAlexander Motin 			/*
91967b87e44SAlexander Motin 			 * We can't reinit port if there are some other
92067b87e44SAlexander Motin 			 * commands active, use resume to complete them.
92167b87e44SAlexander Motin 			 */
922b8b7a902SAlexander Motin 			if (ch->rslots != 0 && !ch->recoverycmd)
92367b87e44SAlexander Motin 				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
92467b87e44SAlexander Motin 		} else {
92567b87e44SAlexander Motin 			if (estatus == SIIS_P_CMDERR_SENDFIS ||
92667b87e44SAlexander Motin 			    estatus == SIIS_P_CMDERR_INCSTATE ||
92767b87e44SAlexander Motin 			    estatus == SIIS_P_CMDERR_PPE ||
92867b87e44SAlexander Motin 			    estatus == SIIS_P_CMDERR_SERVICE) {
92967b87e44SAlexander Motin 				et = SIIS_ERR_SATA;
93067b87e44SAlexander Motin 			} else
93167b87e44SAlexander Motin 				et = SIIS_ERR_INVALID;
93267b87e44SAlexander Motin 			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
9336f9a51c7SAlexander Motin 				/* XXX: requests in loading state. */
93467b87e44SAlexander Motin 				if (((ch->rslots >> i) & 1) == 0)
93567b87e44SAlexander Motin 					continue;
93667b87e44SAlexander Motin 				siis_end_transaction(&ch->slot[i], et);
93767b87e44SAlexander Motin 			}
93867b87e44SAlexander Motin 		}
93967b87e44SAlexander Motin 	}
94067b87e44SAlexander Motin }
94167b87e44SAlexander Motin 
94267b87e44SAlexander Motin /* Must be called with channel locked. */
94367b87e44SAlexander Motin static int
94467b87e44SAlexander Motin siis_check_collision(device_t dev, union ccb *ccb)
94567b87e44SAlexander Motin {
94667b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
94767b87e44SAlexander Motin 
94867b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
94967b87e44SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
950c8039fc6SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
951c8039fc6SAlexander Motin 		/* Tagged command while we have no supported tag free. */
952c8039fc6SAlexander Motin 		if (((~ch->oslots) & (0x7fffffff >> (31 -
953c8039fc6SAlexander Motin 		    ch->curr[ccb->ccb_h.target_id].tags))) == 0)
954c8039fc6SAlexander Motin 			return (1);
955c8039fc6SAlexander Motin 	}
956c8039fc6SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
95767b87e44SAlexander Motin 	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
95867b87e44SAlexander Motin 		/* Atomic command while anything active. */
95967b87e44SAlexander Motin 		if (ch->numrslots != 0)
96067b87e44SAlexander Motin 			return (1);
96167b87e44SAlexander Motin 	}
96267b87e44SAlexander Motin        /* We have some atomic command running. */
96367b87e44SAlexander Motin        if (ch->aslots != 0)
96467b87e44SAlexander Motin                return (1);
96567b87e44SAlexander Motin 	return (0);
96667b87e44SAlexander Motin }
96767b87e44SAlexander Motin 
96867b87e44SAlexander Motin /* Must be called with channel locked. */
96967b87e44SAlexander Motin static void
97067b87e44SAlexander Motin siis_begin_transaction(device_t dev, union ccb *ccb)
97167b87e44SAlexander Motin {
97267b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
97367b87e44SAlexander Motin 	struct siis_slot *slot;
974c8039fc6SAlexander Motin 	int tag, tags;
97567b87e44SAlexander Motin 
97667b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
97767b87e44SAlexander Motin 	/* Choose empty slot. */
978c8039fc6SAlexander Motin 	tags = SIIS_MAX_SLOTS;
979c8039fc6SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
980c8039fc6SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
981c8039fc6SAlexander Motin 		tags = ch->curr[ccb->ccb_h.target_id].tags;
982c8039fc6SAlexander Motin 	tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1;
98367b87e44SAlexander Motin 	/* Occupy chosen slot. */
98467b87e44SAlexander Motin 	slot = &ch->slot[tag];
98567b87e44SAlexander Motin 	slot->ccb = ccb;
98667b87e44SAlexander Motin 	/* Update channel stats. */
987c8039fc6SAlexander Motin 	ch->oslots |= (1 << slot->slot);
98867b87e44SAlexander Motin 	ch->numrslots++;
98967b87e44SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
99067b87e44SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
99167b87e44SAlexander Motin 		ch->numtslots[ccb->ccb_h.target_id]++;
99267b87e44SAlexander Motin 	}
99367b87e44SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
99467b87e44SAlexander Motin 	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
99567b87e44SAlexander Motin 		ch->aslots |= (1 << slot->slot);
99667b87e44SAlexander Motin 	slot->dma.nsegs = 0;
99767b87e44SAlexander Motin 	/* If request moves data, setup and load SG list */
99867b87e44SAlexander Motin 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
99967b87e44SAlexander Motin 		slot->state = SIIS_SLOT_LOADING;
1000*dd0b4fb6SKonstantin Belousov 		bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map,
1001*dd0b4fb6SKonstantin Belousov 		    ccb, siis_dmasetprd, slot, 0);
100267b87e44SAlexander Motin 	} else
100367b87e44SAlexander Motin 		siis_execute_transaction(slot);
100467b87e44SAlexander Motin }
100567b87e44SAlexander Motin 
100667b87e44SAlexander Motin /* Locked by busdma engine. */
100767b87e44SAlexander Motin static void
100867b87e44SAlexander Motin siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
100967b87e44SAlexander Motin {
101067b87e44SAlexander Motin 	struct siis_slot *slot = arg;
101167b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(slot->dev);
101267b87e44SAlexander Motin 	struct siis_cmd *ctp;
101367b87e44SAlexander Motin 	struct siis_dma_prd *prd;
101467b87e44SAlexander Motin 	int i;
101567b87e44SAlexander Motin 
101667b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
101767b87e44SAlexander Motin 	if (error) {
101867b87e44SAlexander Motin 		device_printf(slot->dev, "DMA load error\n");
1019b8b7a902SAlexander Motin 		if (!ch->recoverycmd)
102067b87e44SAlexander Motin 			xpt_freeze_simq(ch->sim, 1);
102167b87e44SAlexander Motin 		siis_end_transaction(slot, SIIS_ERR_INVALID);
102267b87e44SAlexander Motin 		return;
102367b87e44SAlexander Motin 	}
102467b87e44SAlexander Motin 	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
1025*dd0b4fb6SKonstantin Belousov 	slot->dma.nsegs = nsegs;
1026*dd0b4fb6SKonstantin Belousov 	if (nsegs != 0) {
102767b87e44SAlexander Motin 		/* Get a piece of the workspace for this request */
1028*dd0b4fb6SKonstantin Belousov 		ctp = (struct siis_cmd *)(ch->dma.work + SIIS_CT_OFFSET +
1029*dd0b4fb6SKonstantin Belousov 		    (SIIS_CT_SIZE * slot->slot));
103067b87e44SAlexander Motin 		/* Fill S/G table */
103167b87e44SAlexander Motin 		if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
103267b87e44SAlexander Motin 			prd = &ctp->u.ata.prd[0];
103367b87e44SAlexander Motin 		else
103467b87e44SAlexander Motin 			prd = &ctp->u.atapi.prd[0];
103567b87e44SAlexander Motin 		for (i = 0; i < nsegs; i++) {
103667b87e44SAlexander Motin 			prd[i].dba = htole64(segs[i].ds_addr);
103767b87e44SAlexander Motin 			prd[i].dbc = htole32(segs[i].ds_len);
103867b87e44SAlexander Motin 			prd[i].control = 0;
103967b87e44SAlexander Motin 		}
104067b87e44SAlexander Motin 			prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
104167b87e44SAlexander Motin 		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
104267b87e44SAlexander Motin 		    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
104367b87e44SAlexander Motin 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1044*dd0b4fb6SKonstantin Belousov 	}
104567b87e44SAlexander Motin 	siis_execute_transaction(slot);
104667b87e44SAlexander Motin }
104767b87e44SAlexander Motin 
104867b87e44SAlexander Motin /* Must be called with channel locked. */
104967b87e44SAlexander Motin static void
105067b87e44SAlexander Motin siis_execute_transaction(struct siis_slot *slot)
105167b87e44SAlexander Motin {
105267b87e44SAlexander Motin 	device_t dev = slot->dev;
105367b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
105467b87e44SAlexander Motin 	struct siis_cmd *ctp;
105567b87e44SAlexander Motin 	union ccb *ccb = slot->ccb;
105667b87e44SAlexander Motin 	u_int64_t prb_bus;
105767b87e44SAlexander Motin 
105867b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
105967b87e44SAlexander Motin 	/* Get a piece of the workspace for this request */
106067b87e44SAlexander Motin 	ctp = (struct siis_cmd *)
106167b87e44SAlexander Motin 		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
106267b87e44SAlexander Motin 	ctp->control = 0;
106367b87e44SAlexander Motin 	ctp->protocol_override = 0;
106467b87e44SAlexander Motin 	ctp->transfer_count = 0;
106567b87e44SAlexander Motin 	/* Special handling for Soft Reset command. */
1066723bd8c6SAlexander Motin 	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1067723bd8c6SAlexander Motin 		if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) {
106867b87e44SAlexander Motin 			ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
1069723bd8c6SAlexander Motin 		} else {
1070723bd8c6SAlexander Motin 			ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE);
1071723bd8c6SAlexander Motin 			if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1072723bd8c6SAlexander Motin 				ctp->protocol_override |=
1073723bd8c6SAlexander Motin 				    htole16(SIIS_PRB_PROTO_NCQ);
1074723bd8c6SAlexander Motin 			}
1075723bd8c6SAlexander Motin 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1076723bd8c6SAlexander Motin 				ctp->protocol_override |=
1077723bd8c6SAlexander Motin 				    htole16(SIIS_PRB_PROTO_READ);
1078723bd8c6SAlexander Motin 			} else
1079723bd8c6SAlexander Motin 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1080723bd8c6SAlexander Motin 				ctp->protocol_override |=
1081723bd8c6SAlexander Motin 				    htole16(SIIS_PRB_PROTO_WRITE);
1082723bd8c6SAlexander Motin 			}
1083723bd8c6SAlexander Motin 		}
108467b87e44SAlexander Motin 	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1085723bd8c6SAlexander Motin 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
108667b87e44SAlexander Motin 			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
1087723bd8c6SAlexander Motin 		else
1088723bd8c6SAlexander Motin 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
108967b87e44SAlexander Motin 			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
109067b87e44SAlexander Motin 	}
109183c5d981SAlexander Motin 	/* Special handling for Soft Reset command. */
109283c5d981SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
109383c5d981SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
109483c5d981SAlexander Motin 	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
109583c5d981SAlexander Motin 		/* Kick controller into sane state */
109683c5d981SAlexander Motin 		siis_portinit(dev);
109783c5d981SAlexander Motin 	}
109867b87e44SAlexander Motin 	/* Setup the FIS for this request */
1099b447e682SAlexander Motin 	if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) {
110067b87e44SAlexander Motin 		device_printf(ch->dev, "Setting up SATA FIS failed\n");
1101b8b7a902SAlexander Motin 		if (!ch->recoverycmd)
110267b87e44SAlexander Motin 			xpt_freeze_simq(ch->sim, 1);
110367b87e44SAlexander Motin 		siis_end_transaction(slot, SIIS_ERR_INVALID);
110467b87e44SAlexander Motin 		return;
110567b87e44SAlexander Motin 	}
110667b87e44SAlexander Motin 	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
110767b87e44SAlexander Motin 	    BUS_DMASYNC_PREWRITE);
110867b87e44SAlexander Motin 	/* Issue command to the controller. */
110967b87e44SAlexander Motin 	slot->state = SIIS_SLOT_RUNNING;
111067b87e44SAlexander Motin 	ch->rslots |= (1 << slot->slot);
111167b87e44SAlexander Motin 	prb_bus = ch->dma.work_bus +
111267b87e44SAlexander Motin 	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
111367b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
111467b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
111567b87e44SAlexander Motin 	/* Start command execution timeout */
111667b87e44SAlexander Motin 	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
111767b87e44SAlexander Motin 	    (timeout_t*)siis_timeout, slot);
111867b87e44SAlexander Motin 	return;
111967b87e44SAlexander Motin }
112067b87e44SAlexander Motin 
11216f9a51c7SAlexander Motin /* Must be called with channel locked. */
11226f9a51c7SAlexander Motin static void
11236f9a51c7SAlexander Motin siis_process_timeout(device_t dev)
11246f9a51c7SAlexander Motin {
11256f9a51c7SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
11266f9a51c7SAlexander Motin 	int i;
11276f9a51c7SAlexander Motin 
11286f9a51c7SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
1129b8b7a902SAlexander Motin 	if (!ch->recoverycmd && !ch->recovery) {
11306f9a51c7SAlexander Motin 		xpt_freeze_simq(ch->sim, ch->numrslots);
11316f9a51c7SAlexander Motin 		ch->recovery = 1;
11326f9a51c7SAlexander Motin 	}
11336f9a51c7SAlexander Motin 	/* Handle the rest of commands. */
11346f9a51c7SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
11356f9a51c7SAlexander Motin 		/* Do we have a running request on slot? */
11366f9a51c7SAlexander Motin 		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
11376f9a51c7SAlexander Motin 			continue;
11386f9a51c7SAlexander Motin 		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
11396f9a51c7SAlexander Motin 	}
11406f9a51c7SAlexander Motin }
11416f9a51c7SAlexander Motin 
1142301c88beSAlexander Motin /* Must be called with channel locked. */
1143301c88beSAlexander Motin static void
1144301c88beSAlexander Motin siis_rearm_timeout(device_t dev)
1145301c88beSAlexander Motin {
1146301c88beSAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
1147301c88beSAlexander Motin 	int i;
1148301c88beSAlexander Motin 
1149301c88beSAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
1150301c88beSAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1151301c88beSAlexander Motin 		struct siis_slot *slot = &ch->slot[i];
1152301c88beSAlexander Motin 
1153301c88beSAlexander Motin 		/* Do we have a running request on slot? */
1154301c88beSAlexander Motin 		if (slot->state < SIIS_SLOT_RUNNING)
1155301c88beSAlexander Motin 			continue;
1156301c88beSAlexander Motin 		if ((ch->toslots & (1 << i)) == 0)
1157301c88beSAlexander Motin 			continue;
1158301c88beSAlexander Motin 		callout_reset(&slot->timeout,
1159301c88beSAlexander Motin 		    (int)slot->ccb->ccb_h.timeout * hz / 1000,
1160301c88beSAlexander Motin 		    (timeout_t*)siis_timeout, slot);
1161301c88beSAlexander Motin 	}
1162301c88beSAlexander Motin }
1163301c88beSAlexander Motin 
116467b87e44SAlexander Motin /* Locked by callout mechanism. */
116567b87e44SAlexander Motin static void
116667b87e44SAlexander Motin siis_timeout(struct siis_slot *slot)
116767b87e44SAlexander Motin {
116867b87e44SAlexander Motin 	device_t dev = slot->dev;
116967b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
117008c8fde0SAlexander Motin 	union ccb *ccb = slot->ccb;
117167b87e44SAlexander Motin 
117267b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
1173c82b245aSAlexander Motin 	/* Check for stale timeout. */
1174c82b245aSAlexander Motin 	if (slot->state < SIIS_SLOT_RUNNING)
1175c82b245aSAlexander Motin 		return;
117608c8fde0SAlexander Motin 
117708c8fde0SAlexander Motin 	/* Handle soft-reset timeouts without doing hard-reset. */
117808c8fde0SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
117908c8fde0SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
118008c8fde0SAlexander Motin 	    (ccb->ataio.cmd.control & ATA_A_RESET)) {
118108c8fde0SAlexander Motin 		xpt_freeze_simq(ch->sim, ch->numrslots);
118208c8fde0SAlexander Motin 		siis_end_transaction(slot, SIIS_ERR_TFE);
118308c8fde0SAlexander Motin 		return;
118408c8fde0SAlexander Motin 	}
118508c8fde0SAlexander Motin 
118667b87e44SAlexander Motin 	device_printf(dev, "Timeout on slot %d\n", slot->slot);
118767b87e44SAlexander Motin 	device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
118883c5d981SAlexander Motin 	    __func__, ATA_INL(ch->r_mem, SIIS_P_IS),
118983c5d981SAlexander Motin 	    ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
119067b87e44SAlexander Motin 	    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
119167b87e44SAlexander Motin 	    ATA_INL(ch->r_mem, SIIS_P_SERR));
119267b87e44SAlexander Motin 
11936f9a51c7SAlexander Motin 	if (ch->toslots == 0)
11946f9a51c7SAlexander Motin 		xpt_freeze_simq(ch->sim, 1);
11956f9a51c7SAlexander Motin 	ch->toslots |= (1 << slot->slot);
11966f9a51c7SAlexander Motin 	if ((ch->rslots & ~ch->toslots) == 0)
11976f9a51c7SAlexander Motin 		siis_process_timeout(dev);
11986f9a51c7SAlexander Motin 	else
11996f9a51c7SAlexander Motin 		device_printf(dev, " ... waiting for slots %08x\n",
12006f9a51c7SAlexander Motin 		    ch->rslots & ~ch->toslots);
120167b87e44SAlexander Motin }
120267b87e44SAlexander Motin 
120367b87e44SAlexander Motin /* Must be called with channel locked. */
120467b87e44SAlexander Motin static void
120567b87e44SAlexander Motin siis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
120667b87e44SAlexander Motin {
120767b87e44SAlexander Motin 	device_t dev = slot->dev;
120867b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
120967b87e44SAlexander Motin 	union ccb *ccb = slot->ccb;
1210bf12976cSAlexander Motin 	int lastto;
121167b87e44SAlexander Motin 
121267b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
121367b87e44SAlexander Motin 	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
121467b87e44SAlexander Motin 	    BUS_DMASYNC_POSTWRITE);
121567b87e44SAlexander Motin 	/* Read result registers to the result struct
121667b87e44SAlexander Motin 	 * May be incorrect if several commands finished same time,
121767b87e44SAlexander Motin 	 * so read only when sure or have to.
121867b87e44SAlexander Motin 	 */
121967b87e44SAlexander Motin 	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
122067b87e44SAlexander Motin 		struct ata_res *res = &ccb->ataio.res;
122167b87e44SAlexander Motin 		if ((et == SIIS_ERR_TFE) ||
122267b87e44SAlexander Motin 		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
122367b87e44SAlexander Motin 			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
122467b87e44SAlexander Motin 
122567b87e44SAlexander Motin 			res->status = ATA_INB(ch->r_mem, offs + 2);
122667b87e44SAlexander Motin 			res->error = ATA_INB(ch->r_mem, offs + 3);
122767b87e44SAlexander Motin 			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
122867b87e44SAlexander Motin 			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
122967b87e44SAlexander Motin 			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
123067b87e44SAlexander Motin 			res->device = ATA_INB(ch->r_mem, offs + 7);
123167b87e44SAlexander Motin 			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
123267b87e44SAlexander Motin 			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
123367b87e44SAlexander Motin 			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
123467b87e44SAlexander Motin 			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
123567b87e44SAlexander Motin 			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
123667b87e44SAlexander Motin 		} else
123767b87e44SAlexander Motin 			bzero(res, sizeof(*res));
1238ba3a9995SAlexander Motin 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1239ba3a9995SAlexander Motin 		    ch->numrslots == 1) {
1240ba3a9995SAlexander Motin 			ccb->ataio.resid = ccb->ataio.dxfer_len -
1241ba3a9995SAlexander Motin 			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1242ba3a9995SAlexander Motin 		}
1243ba3a9995SAlexander Motin 	} else {
1244ba3a9995SAlexander Motin 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
1245ba3a9995SAlexander Motin 		    ch->numrslots == 1) {
1246ba3a9995SAlexander Motin 			ccb->csio.resid = ccb->csio.dxfer_len -
1247ba3a9995SAlexander Motin 			    ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4);
1248ba3a9995SAlexander Motin 		}
124967b87e44SAlexander Motin 	}
125067b87e44SAlexander Motin 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
125167b87e44SAlexander Motin 		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
125267b87e44SAlexander Motin 		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
125367b87e44SAlexander Motin 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
125467b87e44SAlexander Motin 		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
125567b87e44SAlexander Motin 	}
125667b87e44SAlexander Motin 	/* Set proper result status. */
125767b87e44SAlexander Motin 	if (et != SIIS_ERR_NONE || ch->recovery) {
125867b87e44SAlexander Motin 		ch->eslots |= (1 << slot->slot);
125967b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
126067b87e44SAlexander Motin 	}
1261e06db192SAlexander Motin 	/* In case of error, freeze device for proper recovery. */
1262b8b7a902SAlexander Motin 	if (et != SIIS_ERR_NONE && (!ch->recoverycmd) &&
1263e06db192SAlexander Motin 	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1264e06db192SAlexander Motin 		xpt_freeze_devq(ccb->ccb_h.path, 1);
1265e06db192SAlexander Motin 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1266e06db192SAlexander Motin 	}
1267e06db192SAlexander Motin 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
126867b87e44SAlexander Motin 	switch (et) {
126967b87e44SAlexander Motin 	case SIIS_ERR_NONE:
127067b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_REQ_CMP;
127167b87e44SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
127267b87e44SAlexander Motin 			ccb->csio.scsi_status = SCSI_STATUS_OK;
127367b87e44SAlexander Motin 		break;
127467b87e44SAlexander Motin 	case SIIS_ERR_INVALID:
12756f9a51c7SAlexander Motin 		ch->fatalerr = 1;
127667b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_REQ_INVALID;
127767b87e44SAlexander Motin 		break;
127867b87e44SAlexander Motin 	case SIIS_ERR_INNOCENT:
127967b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
128067b87e44SAlexander Motin 		break;
128167b87e44SAlexander Motin 	case SIIS_ERR_TFE:
1282e06db192SAlexander Motin 	case SIIS_ERR_NCQ:
128367b87e44SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
128467b87e44SAlexander Motin 			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
128567b87e44SAlexander Motin 			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
128667b87e44SAlexander Motin 		} else {
128767b87e44SAlexander Motin 			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
128867b87e44SAlexander Motin 		}
128967b87e44SAlexander Motin 		break;
129067b87e44SAlexander Motin 	case SIIS_ERR_SATA:
12916f9a51c7SAlexander Motin 		ch->fatalerr = 1;
129267b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
129367b87e44SAlexander Motin 		break;
129467b87e44SAlexander Motin 	case SIIS_ERR_TIMEOUT:
12956f9a51c7SAlexander Motin 		ch->fatalerr = 1;
129667b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
129767b87e44SAlexander Motin 		break;
129867b87e44SAlexander Motin 	default:
129967b87e44SAlexander Motin 		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
130067b87e44SAlexander Motin 	}
130167b87e44SAlexander Motin 	/* Free slot. */
1302c8039fc6SAlexander Motin 	ch->oslots &= ~(1 << slot->slot);
130367b87e44SAlexander Motin 	ch->rslots &= ~(1 << slot->slot);
130467b87e44SAlexander Motin 	ch->aslots &= ~(1 << slot->slot);
130567b87e44SAlexander Motin 	slot->state = SIIS_SLOT_EMPTY;
130667b87e44SAlexander Motin 	slot->ccb = NULL;
130767b87e44SAlexander Motin 	/* Update channel stats. */
130867b87e44SAlexander Motin 	ch->numrslots--;
130967b87e44SAlexander Motin 	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
131067b87e44SAlexander Motin 	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
131167b87e44SAlexander Motin 		ch->numtslots[ccb->ccb_h.target_id]--;
131267b87e44SAlexander Motin 	}
1313bf12976cSAlexander Motin 	/* Cancel timeout state if request completed normally. */
1314bf12976cSAlexander Motin 	if (et != SIIS_ERR_TIMEOUT) {
1315bf12976cSAlexander Motin 		lastto = (ch->toslots == (1 << slot->slot));
1316bf12976cSAlexander Motin 		ch->toslots &= ~(1 << slot->slot);
1317bf12976cSAlexander Motin 		if (lastto)
1318bf12976cSAlexander Motin 			xpt_release_simq(ch->sim, TRUE);
1319bf12976cSAlexander Motin 	}
13206f9a51c7SAlexander Motin 	/* If it was our READ LOG command - process it. */
1321b8b7a902SAlexander Motin 	if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
13226f9a51c7SAlexander Motin 		siis_process_read_log(dev, ccb);
1323b8b7a902SAlexander Motin 	/* If it was our REQUEST SENSE command - process it. */
1324b8b7a902SAlexander Motin 	} else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
1325b8b7a902SAlexander Motin 		siis_process_request_sense(dev, ccb);
1326b8b7a902SAlexander Motin 	/* If it was NCQ or ATAPI command error, put result on hold. */
1327b8b7a902SAlexander Motin 	} else if (et == SIIS_ERR_NCQ ||
1328b8b7a902SAlexander Motin 	    ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1329b8b7a902SAlexander Motin 	     (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
133067b87e44SAlexander Motin 		ch->hold[slot->slot] = ccb;
133167b87e44SAlexander Motin 		ch->numhslots++;
13326f9a51c7SAlexander Motin 	} else
133367b87e44SAlexander Motin 		xpt_done(ccb);
133467b87e44SAlexander Motin 	/* If we have no other active commands, ... */
133567b87e44SAlexander Motin 	if (ch->rslots == 0) {
13366f9a51c7SAlexander Motin 		/* if there were timeouts or fatal error - reset port. */
13376f9a51c7SAlexander Motin 		if (ch->toslots != 0 || ch->fatalerr) {
13386f9a51c7SAlexander Motin 			siis_reset(dev);
13396f9a51c7SAlexander Motin 		} else {
134067b87e44SAlexander Motin 			/* if we have slots in error, we can reinit port. */
134167b87e44SAlexander Motin 			if (ch->eslots != 0)
134267b87e44SAlexander Motin 				siis_portinit(dev);
1343b8b7a902SAlexander Motin 			/* if there commands on hold, we can do recovery. */
1344b8b7a902SAlexander Motin 			if (!ch->recoverycmd && ch->numhslots)
1345b8b7a902SAlexander Motin 				siis_issue_recovery(dev);
134667b87e44SAlexander Motin 		}
13476f9a51c7SAlexander Motin 	/* If all the reset of commands are in timeout - abort them. */
1348301c88beSAlexander Motin 	} else if ((ch->rslots & ~ch->toslots) == 0 &&
1349301c88beSAlexander Motin 	    et != SIIS_ERR_TIMEOUT)
1350301c88beSAlexander Motin 		siis_rearm_timeout(dev);
135108c8fde0SAlexander Motin 	/* Unfreeze frozen command. */
135208c8fde0SAlexander Motin 	if (ch->frozen && !siis_check_collision(dev, ch->frozen)) {
135308c8fde0SAlexander Motin 		union ccb *fccb = ch->frozen;
135408c8fde0SAlexander Motin 		ch->frozen = NULL;
135508c8fde0SAlexander Motin 		siis_begin_transaction(dev, fccb);
135608c8fde0SAlexander Motin 		xpt_release_simq(ch->sim, TRUE);
135708c8fde0SAlexander Motin 	}
135867b87e44SAlexander Motin }
135967b87e44SAlexander Motin 
136067b87e44SAlexander Motin static void
1361b8b7a902SAlexander Motin siis_issue_recovery(device_t dev)
136267b87e44SAlexander Motin {
136367b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
136467b87e44SAlexander Motin 	union ccb *ccb;
136567b87e44SAlexander Motin 	struct ccb_ataio *ataio;
1366b8b7a902SAlexander Motin 	struct ccb_scsiio *csio;
136767b87e44SAlexander Motin 	int i;
136867b87e44SAlexander Motin 
13697bcc5957SAlexander Motin 	/* Find some held command. */
137067b87e44SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
137167b87e44SAlexander Motin 		if (ch->hold[i])
137267b87e44SAlexander Motin 			break;
137367b87e44SAlexander Motin 	}
137467b87e44SAlexander Motin 	if (i == SIIS_MAX_SLOTS)
137567b87e44SAlexander Motin 		return;
137667b87e44SAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
137767b87e44SAlexander Motin 	if (ccb == NULL) {
13787bcc5957SAlexander Motin 		device_printf(dev, "Unable to allocate recovery command\n");
13796ac0befdSAlexander Motin completeall:
13807bcc5957SAlexander Motin 		/* We can't do anything -- complete held commands. */
13816ac0befdSAlexander Motin 		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
13826ac0befdSAlexander Motin 			if (ch->hold[i] == NULL)
13836ac0befdSAlexander Motin 				continue;
13846ac0befdSAlexander Motin 			ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
13856ac0befdSAlexander Motin 			ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL;
13866ac0befdSAlexander Motin 			xpt_done(ch->hold[i]);
13876ac0befdSAlexander Motin 			ch->hold[i] = NULL;
13886ac0befdSAlexander Motin 			ch->numhslots--;
13896ac0befdSAlexander Motin 		}
13906ac0befdSAlexander Motin 		siis_reset(dev);
13916ac0befdSAlexander Motin 		return;
139267b87e44SAlexander Motin 	}
139367b87e44SAlexander Motin 	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1394b8b7a902SAlexander Motin 	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1395b8b7a902SAlexander Motin 		/* READ LOG */
1396b8b7a902SAlexander Motin 		ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
139767b87e44SAlexander Motin 		ccb->ccb_h.func_code = XPT_ATA_IO;
139867b87e44SAlexander Motin 		ccb->ccb_h.flags = CAM_DIR_IN;
139967b87e44SAlexander Motin 		ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
140067b87e44SAlexander Motin 		ataio = &ccb->ataio;
140167b87e44SAlexander Motin 		ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
140267b87e44SAlexander Motin 		if (ataio->data_ptr == NULL) {
14038d659f34SAlexander Motin 			xpt_free_ccb(ccb);
14046ac0befdSAlexander Motin 			device_printf(dev,
14057bcc5957SAlexander Motin 			    "Unable to allocate memory for READ LOG command\n");
14066ac0befdSAlexander Motin 			goto completeall;
140767b87e44SAlexander Motin 		}
140867b87e44SAlexander Motin 		ataio->dxfer_len = 512;
140967b87e44SAlexander Motin 		bzero(&ataio->cmd, sizeof(ataio->cmd));
141067b87e44SAlexander Motin 		ataio->cmd.flags = CAM_ATAIO_48BIT;
141167b87e44SAlexander Motin 		ataio->cmd.command = 0x2F;	/* READ LOG EXT */
141267b87e44SAlexander Motin 		ataio->cmd.sector_count = 1;
141367b87e44SAlexander Motin 		ataio->cmd.sector_count_exp = 0;
141467b87e44SAlexander Motin 		ataio->cmd.lba_low = 0x10;
141567b87e44SAlexander Motin 		ataio->cmd.lba_mid = 0;
141667b87e44SAlexander Motin 		ataio->cmd.lba_mid_exp = 0;
1417b8b7a902SAlexander Motin 	} else {
1418b8b7a902SAlexander Motin 		/* REQUEST SENSE */
1419b8b7a902SAlexander Motin 		ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
1420b8b7a902SAlexander Motin 		ccb->ccb_h.recovery_slot = i;
1421b8b7a902SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCSI_IO;
1422b8b7a902SAlexander Motin 		ccb->ccb_h.flags = CAM_DIR_IN;
1423b8b7a902SAlexander Motin 		ccb->ccb_h.status = 0;
1424b8b7a902SAlexander Motin 		ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1425b8b7a902SAlexander Motin 		csio = &ccb->csio;
1426b8b7a902SAlexander Motin 		csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
1427b8b7a902SAlexander Motin 		csio->dxfer_len = ch->hold[i]->csio.sense_len;
1428b8b7a902SAlexander Motin 		csio->cdb_len = 6;
1429b8b7a902SAlexander Motin 		bzero(&csio->cdb_io, sizeof(csio->cdb_io));
1430b8b7a902SAlexander Motin 		csio->cdb_io.cdb_bytes[0] = 0x03;
1431b8b7a902SAlexander Motin 		csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
1432b8b7a902SAlexander Motin 	}
14336ac0befdSAlexander Motin 	ch->recoverycmd = 1;
143467b87e44SAlexander Motin 	siis_begin_transaction(dev, ccb);
143567b87e44SAlexander Motin }
143667b87e44SAlexander Motin 
143767b87e44SAlexander Motin static void
143867b87e44SAlexander Motin siis_process_read_log(device_t dev, union ccb *ccb)
143967b87e44SAlexander Motin {
144067b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
144167b87e44SAlexander Motin 	uint8_t *data;
144267b87e44SAlexander Motin 	struct ata_res *res;
144367b87e44SAlexander Motin 	int i;
144467b87e44SAlexander Motin 
1445b8b7a902SAlexander Motin 	ch->recoverycmd = 0;
144667b87e44SAlexander Motin 	data = ccb->ataio.data_ptr;
144767b87e44SAlexander Motin 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
144867b87e44SAlexander Motin 	    (data[0] & 0x80) == 0) {
144967b87e44SAlexander Motin 		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
145067b87e44SAlexander Motin 			if (!ch->hold[i])
145167b87e44SAlexander Motin 				continue;
145267b87e44SAlexander Motin 			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
145367b87e44SAlexander Motin 				continue;
145467b87e44SAlexander Motin 			if ((data[0] & 0x1F) == i) {
145567b87e44SAlexander Motin 				res = &ch->hold[i]->ataio.res;
145667b87e44SAlexander Motin 				res->status = data[2];
145767b87e44SAlexander Motin 				res->error = data[3];
145867b87e44SAlexander Motin 				res->lba_low = data[4];
145967b87e44SAlexander Motin 				res->lba_mid = data[5];
146067b87e44SAlexander Motin 				res->lba_high = data[6];
146167b87e44SAlexander Motin 				res->device = data[7];
146267b87e44SAlexander Motin 				res->lba_low_exp = data[8];
146367b87e44SAlexander Motin 				res->lba_mid_exp = data[9];
146467b87e44SAlexander Motin 				res->lba_high_exp = data[10];
146567b87e44SAlexander Motin 				res->sector_count = data[12];
146667b87e44SAlexander Motin 				res->sector_count_exp = data[13];
146767b87e44SAlexander Motin 			} else {
146867b87e44SAlexander Motin 				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
146967b87e44SAlexander Motin 				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
147067b87e44SAlexander Motin 			}
147167b87e44SAlexander Motin 			xpt_done(ch->hold[i]);
147267b87e44SAlexander Motin 			ch->hold[i] = NULL;
147367b87e44SAlexander Motin 			ch->numhslots--;
147467b87e44SAlexander Motin 		}
147567b87e44SAlexander Motin 	} else {
147667b87e44SAlexander Motin 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
147767b87e44SAlexander Motin 			device_printf(dev, "Error while READ LOG EXT\n");
147867b87e44SAlexander Motin 		else if ((data[0] & 0x80) == 0) {
147967b87e44SAlexander Motin 			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
148067b87e44SAlexander Motin 		}
148167b87e44SAlexander Motin 		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
148267b87e44SAlexander Motin 			if (!ch->hold[i])
148367b87e44SAlexander Motin 				continue;
148467b87e44SAlexander Motin 			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
148567b87e44SAlexander Motin 				continue;
148667b87e44SAlexander Motin 			xpt_done(ch->hold[i]);
148767b87e44SAlexander Motin 			ch->hold[i] = NULL;
148867b87e44SAlexander Motin 			ch->numhslots--;
148967b87e44SAlexander Motin 		}
149067b87e44SAlexander Motin 	}
149167b87e44SAlexander Motin 	free(ccb->ataio.data_ptr, M_SIIS);
149267b87e44SAlexander Motin 	xpt_free_ccb(ccb);
149367b87e44SAlexander Motin }
149467b87e44SAlexander Motin 
149567b87e44SAlexander Motin static void
1496b8b7a902SAlexander Motin siis_process_request_sense(device_t dev, union ccb *ccb)
1497b8b7a902SAlexander Motin {
1498b8b7a902SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
1499b8b7a902SAlexander Motin 	int i;
1500b8b7a902SAlexander Motin 
1501b8b7a902SAlexander Motin 	ch->recoverycmd = 0;
1502b8b7a902SAlexander Motin 
1503b8b7a902SAlexander Motin 	i = ccb->ccb_h.recovery_slot;
1504b8b7a902SAlexander Motin 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1505b8b7a902SAlexander Motin 		ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
1506b8b7a902SAlexander Motin 	} else {
1507b8b7a902SAlexander Motin 		ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1508b8b7a902SAlexander Motin 		ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1509b8b7a902SAlexander Motin 	}
1510b8b7a902SAlexander Motin 	xpt_done(ch->hold[i]);
1511b8b7a902SAlexander Motin 	ch->hold[i] = NULL;
1512b8b7a902SAlexander Motin 	ch->numhslots--;
1513b8b7a902SAlexander Motin 	xpt_free_ccb(ccb);
1514b8b7a902SAlexander Motin }
1515b8b7a902SAlexander Motin 
1516b8b7a902SAlexander Motin static void
151767b87e44SAlexander Motin siis_portinit(device_t dev)
151867b87e44SAlexander Motin {
151967b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
152067b87e44SAlexander Motin 	int i;
152167b87e44SAlexander Motin 
152267b87e44SAlexander Motin 	ch->eslots = 0;
152367b87e44SAlexander Motin 	ch->recovery = 0;
152467b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
152567b87e44SAlexander Motin 	for (i = 0; i < 16; i++) {
152667b87e44SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
152767b87e44SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
152867b87e44SAlexander Motin 	}
152967b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
153067b87e44SAlexander Motin 	siis_wait_ready(dev, 1000);
153167b87e44SAlexander Motin }
153267b87e44SAlexander Motin 
153327acd6eaSAlexander Motin static int
153467b87e44SAlexander Motin siis_devreset(device_t dev)
153567b87e44SAlexander Motin {
153667b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
153727acd6eaSAlexander Motin 	int timeout = 0;
153827acd6eaSAlexander Motin 	uint32_t val;
153967b87e44SAlexander Motin 
154067b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
154127acd6eaSAlexander Motin 	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
154227acd6eaSAlexander Motin 	    SIIS_P_CTL_DEV_RESET) != 0) {
15431f145eafSAlexander Motin 		DELAY(100);
15441f145eafSAlexander Motin 		if (timeout++ > 1000) {
15451f145eafSAlexander Motin 			device_printf(dev, "device reset stuck "
15461f145eafSAlexander Motin 			    "(timeout 100ms) status = %08x\n", val);
154727acd6eaSAlexander Motin 			return (EBUSY);
154867b87e44SAlexander Motin 		}
154927acd6eaSAlexander Motin 	}
155027acd6eaSAlexander Motin 	return (0);
155127acd6eaSAlexander Motin }
155267b87e44SAlexander Motin 
155367b87e44SAlexander Motin static int
155467b87e44SAlexander Motin siis_wait_ready(device_t dev, int t)
155567b87e44SAlexander Motin {
155667b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
155767b87e44SAlexander Motin 	int timeout = 0;
155867b87e44SAlexander Motin 	uint32_t val;
155967b87e44SAlexander Motin 
156067b87e44SAlexander Motin 	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
156167b87e44SAlexander Motin 	    SIIS_P_CTL_READY) == 0) {
156267b87e44SAlexander Motin 		DELAY(1000);
156367b87e44SAlexander Motin 		if (timeout++ > t) {
156467b87e44SAlexander Motin 			device_printf(dev, "port is not ready (timeout %dms) "
156567b87e44SAlexander Motin 			    "status = %08x\n", t, val);
156667b87e44SAlexander Motin 			return (EBUSY);
156767b87e44SAlexander Motin 		}
156867b87e44SAlexander Motin 	}
156967b87e44SAlexander Motin 	return (0);
157067b87e44SAlexander Motin }
157167b87e44SAlexander Motin 
157267b87e44SAlexander Motin static void
157367b87e44SAlexander Motin siis_reset(device_t dev)
157467b87e44SAlexander Motin {
157567b87e44SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
1576b447e682SAlexander Motin 	int i, retry = 0, sata_rev;
157727acd6eaSAlexander Motin 	uint32_t val;
157867b87e44SAlexander Motin 
157983c5d981SAlexander Motin 	xpt_freeze_simq(ch->sim, 1);
158067b87e44SAlexander Motin 	if (bootverbose)
158167b87e44SAlexander Motin 		device_printf(dev, "SIIS reset...\n");
1582b8b7a902SAlexander Motin 	if (!ch->recoverycmd && !ch->recovery)
158367b87e44SAlexander Motin 		xpt_freeze_simq(ch->sim, ch->numrslots);
15846f9a51c7SAlexander Motin 	/* Requeue frozen command. */
158567b87e44SAlexander Motin 	if (ch->frozen) {
158667b87e44SAlexander Motin 		union ccb *fccb = ch->frozen;
158767b87e44SAlexander Motin 		ch->frozen = NULL;
1588e06db192SAlexander Motin 		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1589e06db192SAlexander Motin 		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1590e06db192SAlexander Motin 		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1591e06db192SAlexander Motin 			xpt_freeze_devq(fccb->ccb_h.path, 1);
1592e06db192SAlexander Motin 			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1593e06db192SAlexander Motin 		}
159467b87e44SAlexander Motin 		xpt_done(fccb);
159567b87e44SAlexander Motin 	}
159627acd6eaSAlexander Motin 	/* Requeue all running commands. */
159767b87e44SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
159867b87e44SAlexander Motin 		/* Do we have a running request on slot? */
159967b87e44SAlexander Motin 		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
160067b87e44SAlexander Motin 			continue;
160167b87e44SAlexander Motin 		/* XXX; Commands in loading state. */
160267b87e44SAlexander Motin 		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
160367b87e44SAlexander Motin 	}
16047bcc5957SAlexander Motin 	/* Finish all held commands as-is. */
16056f9a51c7SAlexander Motin 	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
16066f9a51c7SAlexander Motin 		if (!ch->hold[i])
16076f9a51c7SAlexander Motin 			continue;
16086f9a51c7SAlexander Motin 		xpt_done(ch->hold[i]);
16096f9a51c7SAlexander Motin 		ch->hold[i] = NULL;
16106f9a51c7SAlexander Motin 		ch->numhslots--;
16116f9a51c7SAlexander Motin 	}
16126f9a51c7SAlexander Motin 	if (ch->toslots != 0)
16136f9a51c7SAlexander Motin 		xpt_release_simq(ch->sim, TRUE);
16146f9a51c7SAlexander Motin 	ch->eslots = 0;
16156f9a51c7SAlexander Motin 	ch->recovery = 0;
16166f9a51c7SAlexander Motin 	ch->toslots = 0;
16176f9a51c7SAlexander Motin 	ch->fatalerr = 0;
161827acd6eaSAlexander Motin 	/* Disable port interrupts */
161927acd6eaSAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
162027acd6eaSAlexander Motin 	/* Set speed limit. */
1621b447e682SAlexander Motin 	sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
1622b447e682SAlexander Motin 	if (sata_rev == 1)
162327acd6eaSAlexander Motin 		val = ATA_SC_SPD_SPEED_GEN1;
1624b447e682SAlexander Motin 	else if (sata_rev == 2)
162527acd6eaSAlexander Motin 		val = ATA_SC_SPD_SPEED_GEN2;
1626b447e682SAlexander Motin 	else if (sata_rev == 3)
162727acd6eaSAlexander Motin 		val = ATA_SC_SPD_SPEED_GEN3;
162827acd6eaSAlexander Motin 	else
162927acd6eaSAlexander Motin 		val = 0;
163027acd6eaSAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
163127acd6eaSAlexander Motin 	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
163227acd6eaSAlexander Motin 	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
16336f9a51c7SAlexander Motin retry:
163427acd6eaSAlexander Motin 	siis_devreset(dev);
163567b87e44SAlexander Motin 	/* Reset and reconnect PHY, */
163627acd6eaSAlexander Motin 	if (!siis_sata_connect(ch)) {
163767b87e44SAlexander Motin 		ch->devices = 0;
163867b87e44SAlexander Motin 		/* Enable port interrupts */
163967b87e44SAlexander Motin 		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
164067b87e44SAlexander Motin 		if (bootverbose)
164167b87e44SAlexander Motin 			device_printf(dev,
164267b87e44SAlexander Motin 			    "SIIS reset done: phy reset found no device\n");
164367b87e44SAlexander Motin 		/* Tell the XPT about the event */
164467b87e44SAlexander Motin 		xpt_async(AC_BUS_RESET, ch->path, NULL);
164583c5d981SAlexander Motin 		xpt_release_simq(ch->sim, TRUE);
164667b87e44SAlexander Motin 		return;
164767b87e44SAlexander Motin 	}
164854fc853aSAlexander Motin 	/* Wait for port ready status. */
164954fc853aSAlexander Motin 	if (siis_wait_ready(dev, 1000)) {
165054fc853aSAlexander Motin 		device_printf(dev, "port ready timeout\n");
16516f9a51c7SAlexander Motin 		if (!retry) {
16526f9a51c7SAlexander Motin 			device_printf(dev, "trying full port reset ...\n");
16536f9a51c7SAlexander Motin 			/* Get port to the reset state. */
16546f9a51c7SAlexander Motin 			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
16556f9a51c7SAlexander Motin 			DELAY(10000);
16566f9a51c7SAlexander Motin 			/* Get port out of reset state. */
16576f9a51c7SAlexander Motin 			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
16586f9a51c7SAlexander Motin 			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
16596f9a51c7SAlexander Motin 			if (ch->pm_present)
16606f9a51c7SAlexander Motin 				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
16616f9a51c7SAlexander Motin 			else
16626f9a51c7SAlexander Motin 				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
16636f9a51c7SAlexander Motin 			siis_wait_ready(dev, 5000);
16646f9a51c7SAlexander Motin 			retry = 1;
16656f9a51c7SAlexander Motin 			goto retry;
16666f9a51c7SAlexander Motin 		}
16676f9a51c7SAlexander Motin 	}
166867b87e44SAlexander Motin 	ch->devices = 1;
166967b87e44SAlexander Motin 	/* Enable port interrupts */
167067b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
167167b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
167267b87e44SAlexander Motin 	if (bootverbose)
167367b87e44SAlexander Motin 		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
167467b87e44SAlexander Motin 	/* Tell the XPT about the event */
167567b87e44SAlexander Motin 	xpt_async(AC_BUS_RESET, ch->path, NULL);
167683c5d981SAlexander Motin 	xpt_release_simq(ch->sim, TRUE);
167767b87e44SAlexander Motin }
167867b87e44SAlexander Motin 
167967b87e44SAlexander Motin static int
1680b447e682SAlexander Motin siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag)
168167b87e44SAlexander Motin {
1682b447e682SAlexander Motin 	struct siis_channel *ch = device_get_softc(dev);
168367b87e44SAlexander Motin 	u_int8_t *fis = &ctp->fis[0];
168467b87e44SAlexander Motin 
168567b87e44SAlexander Motin 	bzero(fis, 24);
168667b87e44SAlexander Motin 	fis[0] = 0x27;  		/* host to device */
168767b87e44SAlexander Motin 	fis[1] = (ccb->ccb_h.target_id & 0x0f);
168867b87e44SAlexander Motin 	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
168967b87e44SAlexander Motin 		fis[1] |= 0x80;
169067b87e44SAlexander Motin 		fis[2] = ATA_PACKET_CMD;
1691b447e682SAlexander Motin 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1692b447e682SAlexander Motin 		    ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
169367b87e44SAlexander Motin 			fis[3] = ATA_F_DMA;
169467b87e44SAlexander Motin 		else {
169567b87e44SAlexander Motin 			fis[5] = ccb->csio.dxfer_len;
169667b87e44SAlexander Motin 		        fis[6] = ccb->csio.dxfer_len >> 8;
169767b87e44SAlexander Motin 		}
169867b87e44SAlexander Motin 		fis[7] = ATA_D_LBA;
169967b87e44SAlexander Motin 		fis[15] = ATA_A_4BIT;
170067b87e44SAlexander Motin 		bzero(ctp->u.atapi.ccb, 16);
170167b87e44SAlexander Motin 		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
170267b87e44SAlexander Motin 		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
170367b87e44SAlexander Motin 		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
170467b87e44SAlexander Motin 	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
170567b87e44SAlexander Motin 		fis[1] |= 0x80;
170667b87e44SAlexander Motin 		fis[2] = ccb->ataio.cmd.command;
170767b87e44SAlexander Motin 		fis[3] = ccb->ataio.cmd.features;
170867b87e44SAlexander Motin 		fis[4] = ccb->ataio.cmd.lba_low;
170967b87e44SAlexander Motin 		fis[5] = ccb->ataio.cmd.lba_mid;
171067b87e44SAlexander Motin 		fis[6] = ccb->ataio.cmd.lba_high;
171167b87e44SAlexander Motin 		fis[7] = ccb->ataio.cmd.device;
171267b87e44SAlexander Motin 		fis[8] = ccb->ataio.cmd.lba_low_exp;
171367b87e44SAlexander Motin 		fis[9] = ccb->ataio.cmd.lba_mid_exp;
171467b87e44SAlexander Motin 		fis[10] = ccb->ataio.cmd.lba_high_exp;
171567b87e44SAlexander Motin 		fis[11] = ccb->ataio.cmd.features_exp;
171667b87e44SAlexander Motin 		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
171767b87e44SAlexander Motin 			fis[12] = tag << 3;
171867b87e44SAlexander Motin 			fis[13] = 0;
171967b87e44SAlexander Motin 		} else {
172067b87e44SAlexander Motin 			fis[12] = ccb->ataio.cmd.sector_count;
172167b87e44SAlexander Motin 			fis[13] = ccb->ataio.cmd.sector_count_exp;
172267b87e44SAlexander Motin 		}
172367b87e44SAlexander Motin 		fis[15] = ATA_A_4BIT;
172467b87e44SAlexander Motin 	} else {
172567b87e44SAlexander Motin 		/* Soft reset. */
172667b87e44SAlexander Motin 	}
172767b87e44SAlexander Motin 	return (20);
172867b87e44SAlexander Motin }
172967b87e44SAlexander Motin 
173067b87e44SAlexander Motin static int
173167b87e44SAlexander Motin siis_sata_connect(struct siis_channel *ch)
173267b87e44SAlexander Motin {
173367b87e44SAlexander Motin 	u_int32_t status;
17341f145eafSAlexander Motin 	int timeout, found = 0;
173567b87e44SAlexander Motin 
173667b87e44SAlexander Motin 	/* Wait up to 100ms for "connect well" */
17371f145eafSAlexander Motin 	for (timeout = 0; timeout < 1000 ; timeout++) {
173867b87e44SAlexander Motin 		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
17391f145eafSAlexander Motin 		if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE)
17401f145eafSAlexander Motin 			found = 1;
174167b87e44SAlexander Motin 		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
174267b87e44SAlexander Motin 		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
174367b87e44SAlexander Motin 		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
174467b87e44SAlexander Motin 			break;
17451f145eafSAlexander Motin 		if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) {
174667b87e44SAlexander Motin 			if (bootverbose) {
17471f145eafSAlexander Motin 				device_printf(ch->dev, "SATA offline status=%08x\n",
174867b87e44SAlexander Motin 				    status);
174967b87e44SAlexander Motin 			}
175067b87e44SAlexander Motin 			return (0);
175167b87e44SAlexander Motin 		}
17521f145eafSAlexander Motin 		if (found == 0 && timeout >= 100)
17531f145eafSAlexander Motin 			break;
17541f145eafSAlexander Motin 		DELAY(100);
17551f145eafSAlexander Motin 	}
17561f145eafSAlexander Motin 	if (timeout >= 1000 || !found) {
175767b87e44SAlexander Motin 		if (bootverbose) {
17581f145eafSAlexander Motin 			device_printf(ch->dev,
17591f145eafSAlexander Motin 			    "SATA connect timeout time=%dus status=%08x\n",
17601f145eafSAlexander Motin 			    timeout * 100, status);
17611f145eafSAlexander Motin 		}
17621f145eafSAlexander Motin 		return (0);
17631f145eafSAlexander Motin 	}
17641f145eafSAlexander Motin 	if (bootverbose) {
17651f145eafSAlexander Motin 		device_printf(ch->dev, "SATA connect time=%dus status=%08x\n",
17661f145eafSAlexander Motin 		    timeout * 100, status);
176767b87e44SAlexander Motin 	}
176867b87e44SAlexander Motin 	/* Clear SATA error register */
176967b87e44SAlexander Motin 	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
177067b87e44SAlexander Motin 	return (1);
177167b87e44SAlexander Motin }
177267b87e44SAlexander Motin 
177381a75ae3SAlexander Motin static int
177481a75ae3SAlexander Motin siis_check_ids(device_t dev, union ccb *ccb)
177581a75ae3SAlexander Motin {
177681a75ae3SAlexander Motin 
177781a75ae3SAlexander Motin 	if (ccb->ccb_h.target_id > 15) {
177881a75ae3SAlexander Motin 		ccb->ccb_h.status = CAM_TID_INVALID;
177981a75ae3SAlexander Motin 		xpt_done(ccb);
178081a75ae3SAlexander Motin 		return (-1);
178181a75ae3SAlexander Motin 	}
178281a75ae3SAlexander Motin 	if (ccb->ccb_h.target_lun != 0) {
178381a75ae3SAlexander Motin 		ccb->ccb_h.status = CAM_LUN_INVALID;
178481a75ae3SAlexander Motin 		xpt_done(ccb);
178581a75ae3SAlexander Motin 		return (-1);
178681a75ae3SAlexander Motin 	}
178781a75ae3SAlexander Motin 	return (0);
178881a75ae3SAlexander Motin }
178981a75ae3SAlexander Motin 
179067b87e44SAlexander Motin static void
179167b87e44SAlexander Motin siisaction(struct cam_sim *sim, union ccb *ccb)
179267b87e44SAlexander Motin {
17938edcf694SAlexander Motin 	device_t dev, parent;
179467b87e44SAlexander Motin 	struct siis_channel *ch;
179567b87e44SAlexander Motin 
179667b87e44SAlexander Motin 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
179767b87e44SAlexander Motin 	    ccb->ccb_h.func_code));
179867b87e44SAlexander Motin 
179967b87e44SAlexander Motin 	ch = (struct siis_channel *)cam_sim_softc(sim);
180067b87e44SAlexander Motin 	dev = ch->dev;
180167b87e44SAlexander Motin 	mtx_assert(&ch->mtx, MA_OWNED);
180267b87e44SAlexander Motin 	switch (ccb->ccb_h.func_code) {
180367b87e44SAlexander Motin 	/* Common cases first */
180467b87e44SAlexander Motin 	case XPT_ATA_IO:	/* Execute the requested I/O operation */
180567b87e44SAlexander Motin 	case XPT_SCSI_IO:
180681a75ae3SAlexander Motin 		if (siis_check_ids(dev, ccb))
180781a75ae3SAlexander Motin 			return;
180881a75ae3SAlexander Motin 		if (ch->devices == 0 ||
180981a75ae3SAlexander Motin 		    (ch->pm_present == 0 &&
181081a75ae3SAlexander Motin 		     ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
181167b87e44SAlexander Motin 			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
181267b87e44SAlexander Motin 			break;
181367b87e44SAlexander Motin 		}
1814b8b7a902SAlexander Motin 		ccb->ccb_h.recovery_type = RECOVERY_NONE;
181567b87e44SAlexander Motin 		/* Check for command collision. */
181667b87e44SAlexander Motin 		if (siis_check_collision(dev, ccb)) {
181767b87e44SAlexander Motin 			/* Freeze command. */
181867b87e44SAlexander Motin 			ch->frozen = ccb;
181967b87e44SAlexander Motin 			/* We have only one frozen slot, so freeze simq also. */
182067b87e44SAlexander Motin 			xpt_freeze_simq(ch->sim, 1);
182167b87e44SAlexander Motin 			return;
182267b87e44SAlexander Motin 		}
182367b87e44SAlexander Motin 		siis_begin_transaction(dev, ccb);
182481a75ae3SAlexander Motin 		return;
182567b87e44SAlexander Motin 	case XPT_EN_LUN:		/* Enable LUN as a target */
182667b87e44SAlexander Motin 	case XPT_TARGET_IO:		/* Execute target I/O request */
182767b87e44SAlexander Motin 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
182867b87e44SAlexander Motin 	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
182967b87e44SAlexander Motin 	case XPT_ABORT:			/* Abort the specified CCB */
183067b87e44SAlexander Motin 		/* XXX Implement */
183167b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_INVALID;
183267b87e44SAlexander Motin 		break;
183367b87e44SAlexander Motin 	case XPT_SET_TRAN_SETTINGS:
183467b87e44SAlexander Motin 	{
183567b87e44SAlexander Motin 		struct	ccb_trans_settings *cts = &ccb->cts;
1836c8039fc6SAlexander Motin 		struct	siis_device *d;
183767b87e44SAlexander Motin 
183881a75ae3SAlexander Motin 		if (siis_check_ids(dev, ccb))
183981a75ae3SAlexander Motin 			return;
1840c8039fc6SAlexander Motin 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1841c8039fc6SAlexander Motin 			d = &ch->curr[ccb->ccb_h.target_id];
1842c8039fc6SAlexander Motin 		else
1843c8039fc6SAlexander Motin 			d = &ch->user[ccb->ccb_h.target_id];
1844c8039fc6SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1845c8039fc6SAlexander Motin 			d->revision = cts->xport_specific.sata.revision;
1846c8039fc6SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1847c8039fc6SAlexander Motin 			d->mode = cts->xport_specific.sata.mode;
1848c8039fc6SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1849c8039fc6SAlexander Motin 			d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1850c8039fc6SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1851c8039fc6SAlexander Motin 			d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags);
185267b87e44SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
18536f9a51c7SAlexander Motin 			ch->pm_present = cts->xport_specific.sata.pm_present;
18546f9a51c7SAlexander Motin 			if (ch->pm_present)
185567b87e44SAlexander Motin 				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
185667b87e44SAlexander Motin 			else
185767b87e44SAlexander Motin 				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
185867b87e44SAlexander Motin 		}
18594cca1530SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
18604cca1530SAlexander Motin 			d->atapi = cts->xport_specific.sata.atapi;
1861da6808c1SAlexander Motin 		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1862da6808c1SAlexander Motin 			d->caps = cts->xport_specific.sata.caps;
186367b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
186467b87e44SAlexander Motin 		break;
186567b87e44SAlexander Motin 	}
186667b87e44SAlexander Motin 	case XPT_GET_TRAN_SETTINGS:
186767b87e44SAlexander Motin 	/* Get default/user set transfer settings for the target */
186867b87e44SAlexander Motin 	{
186967b87e44SAlexander Motin 		struct	ccb_trans_settings *cts = &ccb->cts;
1870c8039fc6SAlexander Motin 		struct  siis_device *d;
187167b87e44SAlexander Motin 		uint32_t status;
187267b87e44SAlexander Motin 
187381a75ae3SAlexander Motin 		if (siis_check_ids(dev, ccb))
187481a75ae3SAlexander Motin 			return;
1875c8039fc6SAlexander Motin 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1876c8039fc6SAlexander Motin 			d = &ch->curr[ccb->ccb_h.target_id];
1877c8039fc6SAlexander Motin 		else
1878c8039fc6SAlexander Motin 			d = &ch->user[ccb->ccb_h.target_id];
1879bc1bf6e8SAlexander Motin 		cts->protocol = PROTO_UNSPECIFIED;
188035713642SAlexander Motin 		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
188167b87e44SAlexander Motin 		cts->transport = XPORT_SATA;
188235713642SAlexander Motin 		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
188367b87e44SAlexander Motin 		cts->proto_specific.valid = 0;
188467b87e44SAlexander Motin 		cts->xport_specific.sata.valid = 0;
1885c8039fc6SAlexander Motin 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1886c8039fc6SAlexander Motin 		    (ccb->ccb_h.target_id == 15 ||
1887c8039fc6SAlexander Motin 		    (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
188867b87e44SAlexander Motin 			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1889c8039fc6SAlexander Motin 			if (status & 0x0f0) {
1890c8039fc6SAlexander Motin 				cts->xport_specific.sata.revision =
1891c8039fc6SAlexander Motin 				    (status & 0x0f0) >> 4;
1892c8039fc6SAlexander Motin 				cts->xport_specific.sata.valid |=
1893c8039fc6SAlexander Motin 				    CTS_SATA_VALID_REVISION;
189467b87e44SAlexander Motin 			}
1895da6808c1SAlexander Motin 			cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
1896da6808c1SAlexander Motin 			if (ch->pm_level)
1897da6808c1SAlexander Motin 				cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
18988d169381SAlexander Motin 			cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
1899da6808c1SAlexander Motin 			cts->xport_specific.sata.caps &=
1900da6808c1SAlexander Motin 			    ch->user[ccb->ccb_h.target_id].caps;
1901da6808c1SAlexander Motin 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1902c8039fc6SAlexander Motin 		} else {
1903c8039fc6SAlexander Motin 			cts->xport_specific.sata.revision = d->revision;
1904c8039fc6SAlexander Motin 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1905da6808c1SAlexander Motin 			cts->xport_specific.sata.caps = d->caps;
19068d169381SAlexander Motin 			if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
19078d169381SAlexander Motin 			    (ch->quirks & SIIS_Q_SNTF) == 0)
19088d169381SAlexander Motin 				cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN;
1909da6808c1SAlexander Motin 			cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1910c8039fc6SAlexander Motin 		}
1911c8039fc6SAlexander Motin 		cts->xport_specific.sata.mode = d->mode;
1912c8039fc6SAlexander Motin 		cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1913c8039fc6SAlexander Motin 		cts->xport_specific.sata.bytecount = d->bytecount;
1914c8039fc6SAlexander Motin 		cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
19156f9a51c7SAlexander Motin 		cts->xport_specific.sata.pm_present = ch->pm_present;
191667b87e44SAlexander Motin 		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1917c8039fc6SAlexander Motin 		cts->xport_specific.sata.tags = d->tags;
1918c8039fc6SAlexander Motin 		cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
19194cca1530SAlexander Motin 		cts->xport_specific.sata.atapi = d->atapi;
19204cca1530SAlexander Motin 		cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
192167b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
192267b87e44SAlexander Motin 		break;
192367b87e44SAlexander Motin 	}
192467b87e44SAlexander Motin 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
192567b87e44SAlexander Motin 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
192667b87e44SAlexander Motin 		siis_reset(dev);
192767b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_CMP;
192867b87e44SAlexander Motin 		break;
192967b87e44SAlexander Motin 	case XPT_TERM_IO:		/* Terminate the I/O process */
193067b87e44SAlexander Motin 		/* XXX Implement */
193167b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_INVALID;
193267b87e44SAlexander Motin 		break;
193367b87e44SAlexander Motin 	case XPT_PATH_INQ:		/* Path routing inquiry */
193467b87e44SAlexander Motin 	{
193567b87e44SAlexander Motin 		struct ccb_pathinq *cpi = &ccb->cpi;
193667b87e44SAlexander Motin 
19378edcf694SAlexander Motin 		parent = device_get_parent(dev);
193867b87e44SAlexander Motin 		cpi->version_num = 1; /* XXX??? */
193967b87e44SAlexander Motin 		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
194067b87e44SAlexander Motin 		cpi->hba_inquiry |= PI_SATAPM;
194167b87e44SAlexander Motin 		cpi->target_sprt = 0;
194267b87e44SAlexander Motin 		cpi->hba_misc = PIM_SEQSCAN;
194367b87e44SAlexander Motin 		cpi->hba_eng_cnt = 0;
19448e7cccb3SAlexander Motin 		cpi->max_target = 15;
194567b87e44SAlexander Motin 		cpi->max_lun = 0;
194667b87e44SAlexander Motin 		cpi->initiator_id = 0;
194767b87e44SAlexander Motin 		cpi->bus_id = cam_sim_bus(sim);
194867b87e44SAlexander Motin 		cpi->base_transfer_speed = 150000;
194967b87e44SAlexander Motin 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
195067b87e44SAlexander Motin 		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
195167b87e44SAlexander Motin 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
195267b87e44SAlexander Motin 		cpi->unit_number = cam_sim_unit(sim);
195367b87e44SAlexander Motin 		cpi->transport = XPORT_SATA;
195435713642SAlexander Motin 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1955eb586bd9SAlexander Motin 		cpi->protocol = PROTO_ATA;
195635713642SAlexander Motin 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
195767b87e44SAlexander Motin 		cpi->maxio = MAXPHYS;
19588edcf694SAlexander Motin 		cpi->hba_vendor = pci_get_vendor(parent);
19598edcf694SAlexander Motin 		cpi->hba_device = pci_get_device(parent);
19608edcf694SAlexander Motin 		cpi->hba_subvendor = pci_get_subvendor(parent);
19618edcf694SAlexander Motin 		cpi->hba_subdevice = pci_get_subdevice(parent);
19628edcf694SAlexander Motin 		cpi->ccb_h.status = CAM_REQ_CMP;
196367b87e44SAlexander Motin 		break;
196467b87e44SAlexander Motin 	}
196567b87e44SAlexander Motin 	default:
196667b87e44SAlexander Motin 		ccb->ccb_h.status = CAM_REQ_INVALID;
196767b87e44SAlexander Motin 		break;
196867b87e44SAlexander Motin 	}
196981a75ae3SAlexander Motin 	xpt_done(ccb);
197067b87e44SAlexander Motin }
197167b87e44SAlexander Motin 
197267b87e44SAlexander Motin static void
197367b87e44SAlexander Motin siispoll(struct cam_sim *sim)
197467b87e44SAlexander Motin {
197567b87e44SAlexander Motin 	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
197667b87e44SAlexander Motin 
197767b87e44SAlexander Motin 	siis_ch_intr(ch->dev);
197867b87e44SAlexander Motin }
1979