167b87e44SAlexander Motin /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 467b87e44SAlexander Motin * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 567b87e44SAlexander Motin * All rights reserved. 667b87e44SAlexander Motin * 767b87e44SAlexander Motin * Redistribution and use in source and binary forms, with or without 867b87e44SAlexander Motin * modification, are permitted provided that the following conditions 967b87e44SAlexander Motin * are met: 1067b87e44SAlexander Motin * 1. Redistributions of source code must retain the above copyright 1167b87e44SAlexander Motin * notice, this list of conditions and the following disclaimer, 1267b87e44SAlexander Motin * without modification, immediately at the beginning of the file. 1367b87e44SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1467b87e44SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1567b87e44SAlexander Motin * documentation and/or other materials provided with the distribution. 1667b87e44SAlexander Motin * 1767b87e44SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1867b87e44SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1967b87e44SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2067b87e44SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2167b87e44SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2267b87e44SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2367b87e44SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2467b87e44SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2567b87e44SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2667b87e44SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2767b87e44SAlexander Motin */ 2867b87e44SAlexander Motin 2967b87e44SAlexander Motin #include <sys/cdefs.h> 3067b87e44SAlexander Motin __FBSDID("$FreeBSD$"); 3167b87e44SAlexander Motin 3267b87e44SAlexander Motin #include <sys/param.h> 3367b87e44SAlexander Motin #include <sys/module.h> 3467b87e44SAlexander Motin #include <sys/systm.h> 3567b87e44SAlexander Motin #include <sys/kernel.h> 3667b87e44SAlexander Motin #include <sys/ata.h> 3767b87e44SAlexander Motin #include <sys/bus.h> 3867b87e44SAlexander Motin #include <sys/endian.h> 3967b87e44SAlexander Motin #include <sys/malloc.h> 4067b87e44SAlexander Motin #include <sys/lock.h> 4167b87e44SAlexander Motin #include <sys/mutex.h> 4267b87e44SAlexander Motin #include <sys/sema.h> 4367b87e44SAlexander Motin #include <sys/taskqueue.h> 4467b87e44SAlexander Motin #include <vm/uma.h> 4567b87e44SAlexander Motin #include <machine/stdarg.h> 4667b87e44SAlexander Motin #include <machine/resource.h> 4767b87e44SAlexander Motin #include <machine/bus.h> 4867b87e44SAlexander Motin #include <sys/rman.h> 49a59641a9SAlexander Motin #include <dev/led/led.h> 5067b87e44SAlexander Motin #include <dev/pci/pcivar.h> 5167b87e44SAlexander Motin #include <dev/pci/pcireg.h> 5267b87e44SAlexander Motin #include "siis.h" 5367b87e44SAlexander Motin 5467b87e44SAlexander Motin #include <cam/cam.h> 5567b87e44SAlexander Motin #include <cam/cam_ccb.h> 5667b87e44SAlexander Motin #include <cam/cam_sim.h> 5767b87e44SAlexander Motin #include <cam/cam_xpt_sim.h> 5867b87e44SAlexander Motin #include <cam/cam_debug.h> 5967b87e44SAlexander Motin 6067b87e44SAlexander Motin /* local prototypes */ 6167b87e44SAlexander Motin static int siis_setup_interrupt(device_t dev); 6267b87e44SAlexander Motin static void siis_intr(void *data); 6367b87e44SAlexander Motin static int siis_suspend(device_t dev); 6467b87e44SAlexander Motin static int siis_resume(device_t dev); 65243e0fb9SAlexander Motin static int siis_ch_init(device_t dev); 66243e0fb9SAlexander Motin static int siis_ch_deinit(device_t dev); 6767b87e44SAlexander Motin static int siis_ch_suspend(device_t dev); 6867b87e44SAlexander Motin static int siis_ch_resume(device_t dev); 6967b87e44SAlexander Motin static void siis_ch_intr_locked(void *data); 7067b87e44SAlexander Motin static void siis_ch_intr(void *data); 71a59641a9SAlexander Motin static void siis_ch_led(void *priv, int onoff); 7267b87e44SAlexander Motin static void siis_begin_transaction(device_t dev, union ccb *ccb); 7367b87e44SAlexander Motin static void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 7467b87e44SAlexander Motin static void siis_execute_transaction(struct siis_slot *slot); 7565d2f9c1SJohn Baldwin static void siis_timeout(void *arg); 7667b87e44SAlexander Motin static void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et); 77b447e682SAlexander Motin static int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag); 7867b87e44SAlexander Motin static void siis_dmainit(device_t dev); 7967b87e44SAlexander Motin static void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 8067b87e44SAlexander Motin static void siis_dmafini(device_t dev); 8167b87e44SAlexander Motin static void siis_slotsalloc(device_t dev); 8267b87e44SAlexander Motin static void siis_slotsfree(device_t dev); 8367b87e44SAlexander Motin static void siis_reset(device_t dev); 8467b87e44SAlexander Motin static void siis_portinit(device_t dev); 8567b87e44SAlexander Motin static int siis_wait_ready(device_t dev, int t); 8667b87e44SAlexander Motin 8767b87e44SAlexander Motin static int siis_sata_connect(struct siis_channel *ch); 8867b87e44SAlexander Motin 89b8b7a902SAlexander Motin static void siis_issue_recovery(device_t dev); 9067b87e44SAlexander Motin static void siis_process_read_log(device_t dev, union ccb *ccb); 91b8b7a902SAlexander Motin static void siis_process_request_sense(device_t dev, union ccb *ccb); 9267b87e44SAlexander Motin 9367b87e44SAlexander Motin static void siisaction(struct cam_sim *sim, union ccb *ccb); 9467b87e44SAlexander Motin static void siispoll(struct cam_sim *sim); 9567b87e44SAlexander Motin 96d745c852SEd Schouten static MALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers"); 9767b87e44SAlexander Motin 9803a6387eSAlexander Motin static struct { 9903a6387eSAlexander Motin uint32_t id; 10003a6387eSAlexander Motin const char *name; 10103a6387eSAlexander Motin int ports; 102d095fa04SAlexander Motin int quirks; 103d095fa04SAlexander Motin #define SIIS_Q_SNTF 1 104f81100fdSAlexander Motin #define SIIS_Q_NOMSI 2 10503a6387eSAlexander Motin } siis_ids[] = { 106d095fa04SAlexander Motin {0x31241095, "SiI3124", 4, 0}, 107d095fa04SAlexander Motin {0x31248086, "SiI3124", 4, 0}, 108f81100fdSAlexander Motin {0x31321095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 109f81100fdSAlexander Motin {0x02421095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 110f81100fdSAlexander Motin {0x02441095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 111f81100fdSAlexander Motin {0x31311095, "SiI3131", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 112f81100fdSAlexander Motin {0x35311095, "SiI3531", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 113d095fa04SAlexander Motin {0, NULL, 0, 0} 11403a6387eSAlexander Motin }; 11503a6387eSAlexander Motin 116b8b7a902SAlexander Motin #define recovery_type spriv_field0 117b8b7a902SAlexander Motin #define RECOVERY_NONE 0 118b8b7a902SAlexander Motin #define RECOVERY_READ_LOG 1 119b8b7a902SAlexander Motin #define RECOVERY_REQUEST_SENSE 2 120b8b7a902SAlexander Motin #define recovery_slot spriv_field1 121b8b7a902SAlexander Motin 12267b87e44SAlexander Motin static int 12367b87e44SAlexander Motin siis_probe(device_t dev) 12467b87e44SAlexander Motin { 12503a6387eSAlexander Motin char buf[64]; 12603a6387eSAlexander Motin int i; 12767b87e44SAlexander Motin uint32_t devid = pci_get_devid(dev); 12867b87e44SAlexander Motin 12903a6387eSAlexander Motin for (i = 0; siis_ids[i].id != 0; i++) { 13003a6387eSAlexander Motin if (siis_ids[i].id == devid) { 131d095fa04SAlexander Motin snprintf(buf, sizeof(buf), "%s SATA controller", 13203a6387eSAlexander Motin siis_ids[i].name); 13303a6387eSAlexander Motin device_set_desc_copy(dev, buf); 1343036de3cSAlexander Motin return (BUS_PROBE_DEFAULT); 13567b87e44SAlexander Motin } 13603a6387eSAlexander Motin } 13703a6387eSAlexander Motin return (ENXIO); 13803a6387eSAlexander Motin } 13967b87e44SAlexander Motin 14067b87e44SAlexander Motin static int 14167b87e44SAlexander Motin siis_attach(device_t dev) 14267b87e44SAlexander Motin { 14367b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 14467b87e44SAlexander Motin uint32_t devid = pci_get_devid(dev); 14567b87e44SAlexander Motin device_t child; 14603a6387eSAlexander Motin int error, i, unit; 14767b87e44SAlexander Motin 148d095fa04SAlexander Motin ctlr->dev = dev; 14903a6387eSAlexander Motin for (i = 0; siis_ids[i].id != 0; i++) { 15003a6387eSAlexander Motin if (siis_ids[i].id == devid) 15103a6387eSAlexander Motin break; 15203a6387eSAlexander Motin } 153d095fa04SAlexander Motin ctlr->quirks = siis_ids[i].quirks; 15467b87e44SAlexander Motin /* Global memory */ 15567b87e44SAlexander Motin ctlr->r_grid = PCIR_BAR(0); 15667b87e44SAlexander Motin if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 15767b87e44SAlexander Motin &ctlr->r_grid, RF_ACTIVE))) 15867b87e44SAlexander Motin return (ENXIO); 15930053681SAlexander Motin ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL); 16067b87e44SAlexander Motin /* Channels memory */ 16167b87e44SAlexander Motin ctlr->r_rid = PCIR_BAR(2); 16267b87e44SAlexander Motin if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 16367b87e44SAlexander Motin &ctlr->r_rid, RF_ACTIVE))) 16467b87e44SAlexander Motin return (ENXIO); 16567b87e44SAlexander Motin /* Setup our own memory management for channels. */ 166cc6b610bSAlexander Motin ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem); 167cc6b610bSAlexander Motin ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem); 16867b87e44SAlexander Motin ctlr->sc_iomem.rm_type = RMAN_ARRAY; 16967b87e44SAlexander Motin ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 17067b87e44SAlexander Motin if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 17167b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 17267b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 17367b87e44SAlexander Motin return (error); 17467b87e44SAlexander Motin } 17567b87e44SAlexander Motin if ((error = rman_manage_region(&ctlr->sc_iomem, 17667b87e44SAlexander Motin rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 17767b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 17867b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 17967b87e44SAlexander Motin rman_fini(&ctlr->sc_iomem); 18067b87e44SAlexander Motin return (error); 18167b87e44SAlexander Motin } 18294410af7SAlexander Motin pci_enable_busmaster(dev); 18367b87e44SAlexander Motin /* Reset controller */ 18467b87e44SAlexander Motin siis_resume(dev); 18567b87e44SAlexander Motin /* Number of HW channels */ 18603a6387eSAlexander Motin ctlr->channels = siis_ids[i].ports; 18767b87e44SAlexander Motin /* Setup interrupts. */ 18867b87e44SAlexander Motin if (siis_setup_interrupt(dev)) { 18967b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 19067b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 19167b87e44SAlexander Motin rman_fini(&ctlr->sc_iomem); 19267b87e44SAlexander Motin return ENXIO; 19367b87e44SAlexander Motin } 19467b87e44SAlexander Motin /* Attach all channels on this controller */ 19567b87e44SAlexander Motin for (unit = 0; unit < ctlr->channels; unit++) { 19667b87e44SAlexander Motin child = device_add_child(dev, "siisch", -1); 19767b87e44SAlexander Motin if (child == NULL) 19867b87e44SAlexander Motin device_printf(dev, "failed to add channel device\n"); 19967b87e44SAlexander Motin else 20067b87e44SAlexander Motin device_set_ivars(child, (void *)(intptr_t)unit); 20167b87e44SAlexander Motin } 20267b87e44SAlexander Motin bus_generic_attach(dev); 20367b87e44SAlexander Motin return 0; 20467b87e44SAlexander Motin } 20567b87e44SAlexander Motin 20667b87e44SAlexander Motin static int 20767b87e44SAlexander Motin siis_detach(device_t dev) 20867b87e44SAlexander Motin { 20967b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 21067b87e44SAlexander Motin 21167b87e44SAlexander Motin /* Detach & delete all children */ 2123b12bdb5SHans Petter Selasky device_delete_children(dev); 21311bcf702SHans Petter Selasky 21467b87e44SAlexander Motin /* Free interrupts. */ 21567b87e44SAlexander Motin if (ctlr->irq.r_irq) { 21667b87e44SAlexander Motin bus_teardown_intr(dev, ctlr->irq.r_irq, 21767b87e44SAlexander Motin ctlr->irq.handle); 21867b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, 21967b87e44SAlexander Motin ctlr->irq.r_irq_rid, ctlr->irq.r_irq); 22067b87e44SAlexander Motin } 22167b87e44SAlexander Motin pci_release_msi(dev); 22267b87e44SAlexander Motin /* Free memory. */ 22367b87e44SAlexander Motin rman_fini(&ctlr->sc_iomem); 22467b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 22567b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 22667b87e44SAlexander Motin return (0); 22767b87e44SAlexander Motin } 22867b87e44SAlexander Motin 22967b87e44SAlexander Motin static int 23067b87e44SAlexander Motin siis_suspend(device_t dev) 23167b87e44SAlexander Motin { 23267b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 23367b87e44SAlexander Motin 23467b87e44SAlexander Motin bus_generic_suspend(dev); 23567b87e44SAlexander Motin /* Put controller into reset state. */ 23630053681SAlexander Motin ctlr->gctl |= SIIS_GCTL_GRESET; 23730053681SAlexander Motin ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 23867b87e44SAlexander Motin return 0; 23967b87e44SAlexander Motin } 24067b87e44SAlexander Motin 24167b87e44SAlexander Motin static int 24267b87e44SAlexander Motin siis_resume(device_t dev) 24367b87e44SAlexander Motin { 24467b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 24567b87e44SAlexander Motin 2461535d59dSAlexander Motin /* Set PCIe max read request size to at least 1024 bytes */ 2472622a559SAlexander Motin if (pci_get_max_read_req(dev) < 1024) 2482622a559SAlexander Motin pci_set_max_read_req(dev, 1024); 24967b87e44SAlexander Motin /* Put controller into reset state. */ 25030053681SAlexander Motin ctlr->gctl |= SIIS_GCTL_GRESET; 25130053681SAlexander Motin ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 25267b87e44SAlexander Motin DELAY(10000); 25367b87e44SAlexander Motin /* Get controller out of reset state and enable port interrupts. */ 25430053681SAlexander Motin ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE); 25530053681SAlexander Motin ctlr->gctl |= 0x0000000f; 25630053681SAlexander Motin ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 25767b87e44SAlexander Motin return (bus_generic_resume(dev)); 25867b87e44SAlexander Motin } 25967b87e44SAlexander Motin 26067b87e44SAlexander Motin static int 26167b87e44SAlexander Motin siis_setup_interrupt(device_t dev) 26267b87e44SAlexander Motin { 26367b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 264f81100fdSAlexander Motin int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1; 26567b87e44SAlexander Motin 26667b87e44SAlexander Motin /* Process hints. */ 26767b87e44SAlexander Motin resource_int_value(device_get_name(dev), 26867b87e44SAlexander Motin device_get_unit(dev), "msi", &msi); 26967b87e44SAlexander Motin if (msi < 0) 27067b87e44SAlexander Motin msi = 0; 27167b87e44SAlexander Motin else if (msi > 0) 27267b87e44SAlexander Motin msi = min(1, pci_msi_count(dev)); 27367b87e44SAlexander Motin /* Allocate MSI if needed/present. */ 27467b87e44SAlexander Motin if (msi && pci_alloc_msi(dev, &msi) != 0) 27567b87e44SAlexander Motin msi = 0; 27667b87e44SAlexander Motin /* Allocate all IRQs. */ 27767b87e44SAlexander Motin ctlr->irq.r_irq_rid = msi ? 1 : 0; 27867b87e44SAlexander Motin if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 27967b87e44SAlexander Motin &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 28067b87e44SAlexander Motin device_printf(dev, "unable to map interrupt\n"); 28167b87e44SAlexander Motin return ENXIO; 28267b87e44SAlexander Motin } 28367b87e44SAlexander Motin if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL, 28467b87e44SAlexander Motin siis_intr, ctlr, &ctlr->irq.handle))) { 28567b87e44SAlexander Motin /* SOS XXX release r_irq */ 28667b87e44SAlexander Motin device_printf(dev, "unable to setup interrupt\n"); 28767b87e44SAlexander Motin return ENXIO; 28867b87e44SAlexander Motin } 28967b87e44SAlexander Motin return (0); 29067b87e44SAlexander Motin } 29167b87e44SAlexander Motin 29267b87e44SAlexander Motin /* 29367b87e44SAlexander Motin * Common case interrupt handler. 29467b87e44SAlexander Motin */ 29567b87e44SAlexander Motin static void 29667b87e44SAlexander Motin siis_intr(void *data) 29767b87e44SAlexander Motin { 29867b87e44SAlexander Motin struct siis_controller *ctlr = (struct siis_controller *)data; 29967b87e44SAlexander Motin u_int32_t is; 30067b87e44SAlexander Motin void *arg; 30167b87e44SAlexander Motin int unit; 30267b87e44SAlexander Motin 30367b87e44SAlexander Motin is = ATA_INL(ctlr->r_gmem, SIIS_IS); 30467b87e44SAlexander Motin for (unit = 0; unit < ctlr->channels; unit++) { 30567b87e44SAlexander Motin if ((is & SIIS_IS_PORT(unit)) != 0 && 30667b87e44SAlexander Motin (arg = ctlr->interrupt[unit].argument)) { 30767b87e44SAlexander Motin ctlr->interrupt[unit].function(arg); 30867b87e44SAlexander Motin } 30967b87e44SAlexander Motin } 31030053681SAlexander Motin /* Acknowledge interrupt, if MSI enabled. */ 31130053681SAlexander Motin if (ctlr->irq.r_irq_rid) { 31230053681SAlexander Motin ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 31330053681SAlexander Motin ctlr->gctl | SIIS_GCTL_MSIACK); 31430053681SAlexander Motin } 31567b87e44SAlexander Motin } 31667b87e44SAlexander Motin 31767b87e44SAlexander Motin static struct resource * 31867b87e44SAlexander Motin siis_alloc_resource(device_t dev, device_t child, int type, int *rid, 3192dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 32067b87e44SAlexander Motin { 32167b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 32267b87e44SAlexander Motin int unit = ((struct siis_channel *)device_get_softc(child))->unit; 32367b87e44SAlexander Motin struct resource *res = NULL; 32467b87e44SAlexander Motin int offset = unit << 13; 325d8d7c6b1SJustin Hibbits rman_res_t st; 32667b87e44SAlexander Motin 32767b87e44SAlexander Motin switch (type) { 32867b87e44SAlexander Motin case SYS_RES_MEMORY: 32967b87e44SAlexander Motin st = rman_get_start(ctlr->r_mem); 33067b87e44SAlexander Motin res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 33167b87e44SAlexander Motin st + offset + 0x2000, 0x2000, RF_ACTIVE, child); 33267b87e44SAlexander Motin if (res) { 33367b87e44SAlexander Motin bus_space_handle_t bsh; 33467b87e44SAlexander Motin bus_space_tag_t bst; 33567b87e44SAlexander Motin bsh = rman_get_bushandle(ctlr->r_mem); 33667b87e44SAlexander Motin bst = rman_get_bustag(ctlr->r_mem); 33767b87e44SAlexander Motin bus_space_subregion(bst, bsh, offset, 0x2000, &bsh); 33867b87e44SAlexander Motin rman_set_bushandle(res, bsh); 33967b87e44SAlexander Motin rman_set_bustag(res, bst); 34067b87e44SAlexander Motin } 34167b87e44SAlexander Motin break; 34267b87e44SAlexander Motin case SYS_RES_IRQ: 34367b87e44SAlexander Motin if (*rid == ATA_IRQ_RID) 34467b87e44SAlexander Motin res = ctlr->irq.r_irq; 34567b87e44SAlexander Motin break; 34667b87e44SAlexander Motin } 34767b87e44SAlexander Motin return (res); 34867b87e44SAlexander Motin } 34967b87e44SAlexander Motin 35067b87e44SAlexander Motin static int 35167b87e44SAlexander Motin siis_release_resource(device_t dev, device_t child, int type, int rid, 35267b87e44SAlexander Motin struct resource *r) 35367b87e44SAlexander Motin { 35467b87e44SAlexander Motin 35567b87e44SAlexander Motin switch (type) { 35667b87e44SAlexander Motin case SYS_RES_MEMORY: 35767b87e44SAlexander Motin rman_release_resource(r); 35867b87e44SAlexander Motin return (0); 35967b87e44SAlexander Motin case SYS_RES_IRQ: 36067b87e44SAlexander Motin if (rid != ATA_IRQ_RID) 36167b87e44SAlexander Motin return ENOENT; 36267b87e44SAlexander Motin return (0); 36367b87e44SAlexander Motin } 36467b87e44SAlexander Motin return (EINVAL); 36567b87e44SAlexander Motin } 36667b87e44SAlexander Motin 36767b87e44SAlexander Motin static int 36867b87e44SAlexander Motin siis_setup_intr(device_t dev, device_t child, struct resource *irq, 36967b87e44SAlexander Motin int flags, driver_filter_t *filter, driver_intr_t *function, 37067b87e44SAlexander Motin void *argument, void **cookiep) 37167b87e44SAlexander Motin { 37267b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 37367b87e44SAlexander Motin int unit = (intptr_t)device_get_ivars(child); 37467b87e44SAlexander Motin 37567b87e44SAlexander Motin if (filter != NULL) { 37667b87e44SAlexander Motin printf("siis.c: we cannot use a filter here\n"); 37767b87e44SAlexander Motin return (EINVAL); 37867b87e44SAlexander Motin } 37967b87e44SAlexander Motin ctlr->interrupt[unit].function = function; 38067b87e44SAlexander Motin ctlr->interrupt[unit].argument = argument; 38167b87e44SAlexander Motin return (0); 38267b87e44SAlexander Motin } 38367b87e44SAlexander Motin 38467b87e44SAlexander Motin static int 38567b87e44SAlexander Motin siis_teardown_intr(device_t dev, device_t child, struct resource *irq, 38667b87e44SAlexander Motin void *cookie) 38767b87e44SAlexander Motin { 38867b87e44SAlexander Motin struct siis_controller *ctlr = device_get_softc(dev); 38967b87e44SAlexander Motin int unit = (intptr_t)device_get_ivars(child); 39067b87e44SAlexander Motin 39167b87e44SAlexander Motin ctlr->interrupt[unit].function = NULL; 39267b87e44SAlexander Motin ctlr->interrupt[unit].argument = NULL; 39367b87e44SAlexander Motin return (0); 39467b87e44SAlexander Motin } 39567b87e44SAlexander Motin 39667b87e44SAlexander Motin static int 39767b87e44SAlexander Motin siis_print_child(device_t dev, device_t child) 39867b87e44SAlexander Motin { 39967b87e44SAlexander Motin int retval; 40067b87e44SAlexander Motin 40167b87e44SAlexander Motin retval = bus_print_child_header(dev, child); 40267b87e44SAlexander Motin retval += printf(" at channel %d", 40367b87e44SAlexander Motin (int)(intptr_t)device_get_ivars(child)); 40467b87e44SAlexander Motin retval += bus_print_child_footer(dev, child); 40567b87e44SAlexander Motin 40667b87e44SAlexander Motin return (retval); 40767b87e44SAlexander Motin } 40867b87e44SAlexander Motin 409445cc79cSAlexander Motin static int 410445cc79cSAlexander Motin siis_child_location_str(device_t dev, device_t child, char *buf, 411445cc79cSAlexander Motin size_t buflen) 412445cc79cSAlexander Motin { 413445cc79cSAlexander Motin 414445cc79cSAlexander Motin snprintf(buf, buflen, "channel=%d", 415445cc79cSAlexander Motin (int)(intptr_t)device_get_ivars(child)); 416445cc79cSAlexander Motin return (0); 417445cc79cSAlexander Motin } 418445cc79cSAlexander Motin 419ca114192SAlexander Motin static bus_dma_tag_t 420ca114192SAlexander Motin siis_get_dma_tag(device_t bus, device_t child) 421ca114192SAlexander Motin { 422ca114192SAlexander Motin 423ca114192SAlexander Motin return (bus_get_dma_tag(bus)); 424ca114192SAlexander Motin } 425ca114192SAlexander Motin 42667b87e44SAlexander Motin devclass_t siis_devclass; 42767b87e44SAlexander Motin static device_method_t siis_methods[] = { 42867b87e44SAlexander Motin DEVMETHOD(device_probe, siis_probe), 42967b87e44SAlexander Motin DEVMETHOD(device_attach, siis_attach), 43067b87e44SAlexander Motin DEVMETHOD(device_detach, siis_detach), 43167b87e44SAlexander Motin DEVMETHOD(device_suspend, siis_suspend), 43267b87e44SAlexander Motin DEVMETHOD(device_resume, siis_resume), 43367b87e44SAlexander Motin DEVMETHOD(bus_print_child, siis_print_child), 43467b87e44SAlexander Motin DEVMETHOD(bus_alloc_resource, siis_alloc_resource), 43567b87e44SAlexander Motin DEVMETHOD(bus_release_resource, siis_release_resource), 43667b87e44SAlexander Motin DEVMETHOD(bus_setup_intr, siis_setup_intr), 43767b87e44SAlexander Motin DEVMETHOD(bus_teardown_intr,siis_teardown_intr), 438445cc79cSAlexander Motin DEVMETHOD(bus_child_location_str, siis_child_location_str), 439ca114192SAlexander Motin DEVMETHOD(bus_get_dma_tag, siis_get_dma_tag), 44067b87e44SAlexander Motin { 0, 0 } 44167b87e44SAlexander Motin }; 44267b87e44SAlexander Motin static driver_t siis_driver = { 44367b87e44SAlexander Motin "siis", 44467b87e44SAlexander Motin siis_methods, 44567b87e44SAlexander Motin sizeof(struct siis_controller) 44667b87e44SAlexander Motin }; 44767b87e44SAlexander Motin DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0); 44867b87e44SAlexander Motin MODULE_VERSION(siis, 1); 44967b87e44SAlexander Motin MODULE_DEPEND(siis, cam, 1, 1, 1); 45067b87e44SAlexander Motin 45167b87e44SAlexander Motin static int 45267b87e44SAlexander Motin siis_ch_probe(device_t dev) 45367b87e44SAlexander Motin { 45467b87e44SAlexander Motin 45567b87e44SAlexander Motin device_set_desc_copy(dev, "SIIS channel"); 4563036de3cSAlexander Motin return (BUS_PROBE_DEFAULT); 45767b87e44SAlexander Motin } 45867b87e44SAlexander Motin 45967b87e44SAlexander Motin static int 46067b87e44SAlexander Motin siis_ch_attach(device_t dev) 46167b87e44SAlexander Motin { 462d095fa04SAlexander Motin struct siis_controller *ctlr = device_get_softc(device_get_parent(dev)); 46367b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 46467b87e44SAlexander Motin struct cam_devq *devq; 465b447e682SAlexander Motin int rid, error, i, sata_rev = 0; 46667b87e44SAlexander Motin 46767b87e44SAlexander Motin ch->dev = dev; 46867b87e44SAlexander Motin ch->unit = (intptr_t)device_get_ivars(dev); 469d095fa04SAlexander Motin ch->quirks = ctlr->quirks; 470200b4021SAlexander Motin ch->pm_level = 0; 47167b87e44SAlexander Motin resource_int_value(device_get_name(dev), 47267b87e44SAlexander Motin device_get_unit(dev), "pm_level", &ch->pm_level); 473b447e682SAlexander Motin resource_int_value(device_get_name(dev), 474b447e682SAlexander Motin device_get_unit(dev), "sata_rev", &sata_rev); 475c8039fc6SAlexander Motin for (i = 0; i < 16; i++) { 476b447e682SAlexander Motin ch->user[i].revision = sata_rev; 477c8039fc6SAlexander Motin ch->user[i].mode = 0; 478c8039fc6SAlexander Motin ch->user[i].bytecount = 8192; 479c8039fc6SAlexander Motin ch->user[i].tags = SIIS_MAX_SLOTS; 480c8039fc6SAlexander Motin ch->curr[i] = ch->user[i]; 481da6808c1SAlexander Motin if (ch->pm_level) 482da6808c1SAlexander Motin ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ; 4838d169381SAlexander Motin ch->user[i].caps |= CTS_SATA_CAPS_H_AN; 484c8039fc6SAlexander Motin } 48567b87e44SAlexander Motin mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 48667b87e44SAlexander Motin rid = ch->unit; 48767b87e44SAlexander Motin if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 48867b87e44SAlexander Motin &rid, RF_ACTIVE))) 48967b87e44SAlexander Motin return (ENXIO); 49067b87e44SAlexander Motin siis_dmainit(dev); 49167b87e44SAlexander Motin siis_slotsalloc(dev); 492243e0fb9SAlexander Motin siis_ch_init(dev); 49367b87e44SAlexander Motin mtx_lock(&ch->mtx); 49467b87e44SAlexander Motin rid = ATA_IRQ_RID; 49567b87e44SAlexander Motin if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 49667b87e44SAlexander Motin &rid, RF_SHAREABLE | RF_ACTIVE))) { 49767b87e44SAlexander Motin device_printf(dev, "Unable to map interrupt\n"); 498b1364652SAlexander Motin error = ENXIO; 499b1364652SAlexander Motin goto err0; 50067b87e44SAlexander Motin } 50167b87e44SAlexander Motin if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 50267b87e44SAlexander Motin siis_ch_intr_locked, dev, &ch->ih))) { 50367b87e44SAlexander Motin device_printf(dev, "Unable to setup interrupt\n"); 50467b87e44SAlexander Motin error = ENXIO; 50567b87e44SAlexander Motin goto err1; 50667b87e44SAlexander Motin } 50767b87e44SAlexander Motin /* Create the device queue for our SIM. */ 50867b87e44SAlexander Motin devq = cam_simq_alloc(SIIS_MAX_SLOTS); 50967b87e44SAlexander Motin if (devq == NULL) { 51067b87e44SAlexander Motin device_printf(dev, "Unable to allocate simq\n"); 51167b87e44SAlexander Motin error = ENOMEM; 51267b87e44SAlexander Motin goto err1; 51367b87e44SAlexander Motin } 51467b87e44SAlexander Motin /* Construct SIM entry */ 51567b87e44SAlexander Motin ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 51630a4094fSAlexander Motin device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq); 51767b87e44SAlexander Motin if (ch->sim == NULL) { 518b1364652SAlexander Motin cam_simq_free(devq); 51967b87e44SAlexander Motin device_printf(dev, "unable to allocate sim\n"); 52067b87e44SAlexander Motin error = ENOMEM; 521b1364652SAlexander Motin goto err1; 52267b87e44SAlexander Motin } 52367b87e44SAlexander Motin if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 52467b87e44SAlexander Motin device_printf(dev, "unable to register xpt bus\n"); 52567b87e44SAlexander Motin error = ENXIO; 52667b87e44SAlexander Motin goto err2; 52767b87e44SAlexander Motin } 52867b87e44SAlexander Motin if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 52967b87e44SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 53067b87e44SAlexander Motin device_printf(dev, "unable to create path\n"); 53167b87e44SAlexander Motin error = ENXIO; 53267b87e44SAlexander Motin goto err3; 53367b87e44SAlexander Motin } 53467b87e44SAlexander Motin mtx_unlock(&ch->mtx); 535a59641a9SAlexander Motin ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev)); 53667b87e44SAlexander Motin return (0); 53767b87e44SAlexander Motin 53867b87e44SAlexander Motin err3: 53967b87e44SAlexander Motin xpt_bus_deregister(cam_sim_path(ch->sim)); 54067b87e44SAlexander Motin err2: 54167b87e44SAlexander Motin cam_sim_free(ch->sim, /*free_devq*/TRUE); 54267b87e44SAlexander Motin err1: 54367b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 544b1364652SAlexander Motin err0: 54567b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 54667b87e44SAlexander Motin mtx_unlock(&ch->mtx); 5475b9392e8SAlexander Motin mtx_destroy(&ch->mtx); 54867b87e44SAlexander Motin return (error); 54967b87e44SAlexander Motin } 55067b87e44SAlexander Motin 55167b87e44SAlexander Motin static int 55267b87e44SAlexander Motin siis_ch_detach(device_t dev) 55367b87e44SAlexander Motin { 55467b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 55567b87e44SAlexander Motin 556a59641a9SAlexander Motin led_destroy(ch->led); 55767b87e44SAlexander Motin mtx_lock(&ch->mtx); 55867b87e44SAlexander Motin xpt_async(AC_LOST_DEVICE, ch->path, NULL); 55967b87e44SAlexander Motin xpt_free_path(ch->path); 56067b87e44SAlexander Motin xpt_bus_deregister(cam_sim_path(ch->sim)); 56167b87e44SAlexander Motin cam_sim_free(ch->sim, /*free_devq*/TRUE); 56267b87e44SAlexander Motin mtx_unlock(&ch->mtx); 56367b87e44SAlexander Motin 56467b87e44SAlexander Motin bus_teardown_intr(dev, ch->r_irq, ch->ih); 56567b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 56667b87e44SAlexander Motin 567243e0fb9SAlexander Motin siis_ch_deinit(dev); 56867b87e44SAlexander Motin siis_slotsfree(dev); 56967b87e44SAlexander Motin siis_dmafini(dev); 57067b87e44SAlexander Motin 57167b87e44SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 57267b87e44SAlexander Motin mtx_destroy(&ch->mtx); 57367b87e44SAlexander Motin return (0); 57467b87e44SAlexander Motin } 57567b87e44SAlexander Motin 57667b87e44SAlexander Motin static int 577243e0fb9SAlexander Motin siis_ch_init(device_t dev) 57867b87e44SAlexander Motin { 57967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 58067b87e44SAlexander Motin 58167b87e44SAlexander Motin /* Get port out of reset state. */ 58267b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 58367b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 5846f9a51c7SAlexander Motin if (ch->pm_present) 5856f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 5866f9a51c7SAlexander Motin else 58767b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 58867b87e44SAlexander Motin /* Enable port interrupts */ 58967b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 59067b87e44SAlexander Motin return (0); 59167b87e44SAlexander Motin } 59267b87e44SAlexander Motin 593243e0fb9SAlexander Motin static int 594243e0fb9SAlexander Motin siis_ch_deinit(device_t dev) 595243e0fb9SAlexander Motin { 596243e0fb9SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 597243e0fb9SAlexander Motin 598243e0fb9SAlexander Motin /* Put port into reset state. */ 599243e0fb9SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 600243e0fb9SAlexander Motin return (0); 601243e0fb9SAlexander Motin } 602243e0fb9SAlexander Motin 603243e0fb9SAlexander Motin static int 604243e0fb9SAlexander Motin siis_ch_suspend(device_t dev) 605243e0fb9SAlexander Motin { 606243e0fb9SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 607243e0fb9SAlexander Motin 608243e0fb9SAlexander Motin mtx_lock(&ch->mtx); 609243e0fb9SAlexander Motin xpt_freeze_simq(ch->sim, 1); 610243e0fb9SAlexander Motin while (ch->oslots) 611243e0fb9SAlexander Motin msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100); 612243e0fb9SAlexander Motin siis_ch_deinit(dev); 613243e0fb9SAlexander Motin mtx_unlock(&ch->mtx); 614243e0fb9SAlexander Motin return (0); 615243e0fb9SAlexander Motin } 616243e0fb9SAlexander Motin 617243e0fb9SAlexander Motin static int 618243e0fb9SAlexander Motin siis_ch_resume(device_t dev) 619243e0fb9SAlexander Motin { 620243e0fb9SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 621243e0fb9SAlexander Motin 622243e0fb9SAlexander Motin mtx_lock(&ch->mtx); 623243e0fb9SAlexander Motin siis_ch_init(dev); 624243e0fb9SAlexander Motin siis_reset(dev); 625243e0fb9SAlexander Motin xpt_release_simq(ch->sim, TRUE); 626243e0fb9SAlexander Motin mtx_unlock(&ch->mtx); 627243e0fb9SAlexander Motin return (0); 628243e0fb9SAlexander Motin } 629243e0fb9SAlexander Motin 63067b87e44SAlexander Motin devclass_t siisch_devclass; 63167b87e44SAlexander Motin static device_method_t siisch_methods[] = { 63267b87e44SAlexander Motin DEVMETHOD(device_probe, siis_ch_probe), 63367b87e44SAlexander Motin DEVMETHOD(device_attach, siis_ch_attach), 63467b87e44SAlexander Motin DEVMETHOD(device_detach, siis_ch_detach), 63567b87e44SAlexander Motin DEVMETHOD(device_suspend, siis_ch_suspend), 63667b87e44SAlexander Motin DEVMETHOD(device_resume, siis_ch_resume), 63767b87e44SAlexander Motin { 0, 0 } 63867b87e44SAlexander Motin }; 63967b87e44SAlexander Motin static driver_t siisch_driver = { 64067b87e44SAlexander Motin "siisch", 64167b87e44SAlexander Motin siisch_methods, 64267b87e44SAlexander Motin sizeof(struct siis_channel) 64367b87e44SAlexander Motin }; 64467b87e44SAlexander Motin DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 64567b87e44SAlexander Motin 646a59641a9SAlexander Motin static void 647a59641a9SAlexander Motin siis_ch_led(void *priv, int onoff) 648a59641a9SAlexander Motin { 649a59641a9SAlexander Motin device_t dev; 650a59641a9SAlexander Motin struct siis_channel *ch; 651a59641a9SAlexander Motin 652a59641a9SAlexander Motin dev = (device_t)priv; 653a59641a9SAlexander Motin ch = device_get_softc(dev); 654a59641a9SAlexander Motin 655a59641a9SAlexander Motin if (onoff == 0) 656a59641a9SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON); 657a59641a9SAlexander Motin else 658a59641a9SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON); 659a59641a9SAlexander Motin } 660a59641a9SAlexander Motin 66167b87e44SAlexander Motin struct siis_dc_cb_args { 66267b87e44SAlexander Motin bus_addr_t maddr; 66367b87e44SAlexander Motin int error; 66467b87e44SAlexander Motin }; 66567b87e44SAlexander Motin 66667b87e44SAlexander Motin static void 66767b87e44SAlexander Motin siis_dmainit(device_t dev) 66867b87e44SAlexander Motin { 66967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 67067b87e44SAlexander Motin struct siis_dc_cb_args dcba; 67167b87e44SAlexander Motin 67267b87e44SAlexander Motin /* Command area. */ 67367b87e44SAlexander Motin if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 67467b87e44SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 67567b87e44SAlexander Motin NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 67667b87e44SAlexander Motin 0, NULL, NULL, &ch->dma.work_tag)) 67767b87e44SAlexander Motin goto error; 67867b87e44SAlexander Motin if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 67967b87e44SAlexander Motin &ch->dma.work_map)) 68067b87e44SAlexander Motin goto error; 68167b87e44SAlexander Motin if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 68267b87e44SAlexander Motin SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 68367b87e44SAlexander Motin bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 68467b87e44SAlexander Motin goto error; 68567b87e44SAlexander Motin } 68667b87e44SAlexander Motin ch->dma.work_bus = dcba.maddr; 68767b87e44SAlexander Motin /* Data area. */ 6885319fb62SAlexander Motin if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 68967b87e44SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 69067b87e44SAlexander Motin NULL, NULL, 69167b87e44SAlexander Motin SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 69267b87e44SAlexander Motin SIIS_SG_ENTRIES, 0xFFFFFFFF, 69367b87e44SAlexander Motin 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 69467b87e44SAlexander Motin goto error; 69567b87e44SAlexander Motin } 69667b87e44SAlexander Motin return; 69767b87e44SAlexander Motin 69867b87e44SAlexander Motin error: 69967b87e44SAlexander Motin device_printf(dev, "WARNING - DMA initialization failed\n"); 70067b87e44SAlexander Motin siis_dmafini(dev); 70167b87e44SAlexander Motin } 70267b87e44SAlexander Motin 70367b87e44SAlexander Motin static void 70467b87e44SAlexander Motin siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 70567b87e44SAlexander Motin { 70667b87e44SAlexander Motin struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 70767b87e44SAlexander Motin 70867b87e44SAlexander Motin if (!(dcba->error = error)) 70967b87e44SAlexander Motin dcba->maddr = segs[0].ds_addr; 71067b87e44SAlexander Motin } 71167b87e44SAlexander Motin 71267b87e44SAlexander Motin static void 71367b87e44SAlexander Motin siis_dmafini(device_t dev) 71467b87e44SAlexander Motin { 71567b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 71667b87e44SAlexander Motin 71767b87e44SAlexander Motin if (ch->dma.data_tag) { 71867b87e44SAlexander Motin bus_dma_tag_destroy(ch->dma.data_tag); 71967b87e44SAlexander Motin ch->dma.data_tag = NULL; 72067b87e44SAlexander Motin } 72167b87e44SAlexander Motin if (ch->dma.work_bus) { 72267b87e44SAlexander Motin bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 72367b87e44SAlexander Motin bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 72467b87e44SAlexander Motin ch->dma.work_bus = 0; 72567b87e44SAlexander Motin ch->dma.work_map = NULL; 72667b87e44SAlexander Motin ch->dma.work = NULL; 72767b87e44SAlexander Motin } 72867b87e44SAlexander Motin if (ch->dma.work_tag) { 72967b87e44SAlexander Motin bus_dma_tag_destroy(ch->dma.work_tag); 73067b87e44SAlexander Motin ch->dma.work_tag = NULL; 73167b87e44SAlexander Motin } 73267b87e44SAlexander Motin } 73367b87e44SAlexander Motin 73467b87e44SAlexander Motin static void 73567b87e44SAlexander Motin siis_slotsalloc(device_t dev) 73667b87e44SAlexander Motin { 73767b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 73867b87e44SAlexander Motin int i; 73967b87e44SAlexander Motin 74067b87e44SAlexander Motin /* Alloc and setup command/dma slots */ 74167b87e44SAlexander Motin bzero(ch->slot, sizeof(ch->slot)); 74267b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 74367b87e44SAlexander Motin struct siis_slot *slot = &ch->slot[i]; 74467b87e44SAlexander Motin 74567b87e44SAlexander Motin slot->dev = dev; 74667b87e44SAlexander Motin slot->slot = i; 74767b87e44SAlexander Motin slot->state = SIIS_SLOT_EMPTY; 74867b87e44SAlexander Motin slot->ccb = NULL; 74967b87e44SAlexander Motin callout_init_mtx(&slot->timeout, &ch->mtx, 0); 75067b87e44SAlexander Motin 75167b87e44SAlexander Motin if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 75267b87e44SAlexander Motin device_printf(ch->dev, "FAILURE - create data_map\n"); 75367b87e44SAlexander Motin } 75467b87e44SAlexander Motin } 75567b87e44SAlexander Motin 75667b87e44SAlexander Motin static void 75767b87e44SAlexander Motin siis_slotsfree(device_t dev) 75867b87e44SAlexander Motin { 75967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 76067b87e44SAlexander Motin int i; 76167b87e44SAlexander Motin 76267b87e44SAlexander Motin /* Free all dma slots */ 76367b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 76467b87e44SAlexander Motin struct siis_slot *slot = &ch->slot[i]; 76567b87e44SAlexander Motin 766c82b245aSAlexander Motin callout_drain(&slot->timeout); 76767b87e44SAlexander Motin if (slot->dma.data_map) { 76867b87e44SAlexander Motin bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 76967b87e44SAlexander Motin slot->dma.data_map = NULL; 77067b87e44SAlexander Motin } 77167b87e44SAlexander Motin } 77267b87e44SAlexander Motin } 77367b87e44SAlexander Motin 77467b87e44SAlexander Motin static void 77535713642SAlexander Motin siis_notify_events(device_t dev) 77635713642SAlexander Motin { 77735713642SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 77835713642SAlexander Motin struct cam_path *dpath; 77935713642SAlexander Motin u_int32_t status; 78035713642SAlexander Motin int i; 78135713642SAlexander Motin 782d095fa04SAlexander Motin if (ch->quirks & SIIS_Q_SNTF) { 78335713642SAlexander Motin status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 78435713642SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 785d095fa04SAlexander Motin } else { 786d095fa04SAlexander Motin /* 787d095fa04SAlexander Motin * Without SNTF we have no idea which device sent notification. 788d095fa04SAlexander Motin * If PMP is connected, assume it, else - device. 789d095fa04SAlexander Motin */ 790d095fa04SAlexander Motin status = (ch->pm_present) ? 0x8000 : 0x0001; 791d095fa04SAlexander Motin } 79235713642SAlexander Motin if (bootverbose) 79335713642SAlexander Motin device_printf(dev, "SNTF 0x%04x\n", status); 79435713642SAlexander Motin for (i = 0; i < 16; i++) { 79535713642SAlexander Motin if ((status & (1 << i)) == 0) 79635713642SAlexander Motin continue; 79735713642SAlexander Motin if (xpt_create_path(&dpath, NULL, 79835713642SAlexander Motin xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 79935713642SAlexander Motin xpt_async(AC_SCSI_AEN, dpath, NULL); 80035713642SAlexander Motin xpt_free_path(dpath); 80135713642SAlexander Motin } 80235713642SAlexander Motin } 80335713642SAlexander Motin 80435713642SAlexander Motin } 80535713642SAlexander Motin 80635713642SAlexander Motin static void 80767b87e44SAlexander Motin siis_phy_check_events(device_t dev) 80867b87e44SAlexander Motin { 80967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 81067b87e44SAlexander Motin 81167b87e44SAlexander Motin /* If we have a connection event, deal with it */ 81267b87e44SAlexander Motin if (ch->pm_level == 0) { 81367b87e44SAlexander Motin u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 81483c5d981SAlexander Motin union ccb *ccb; 81583c5d981SAlexander Motin 816ed70cffdSAlexander Motin if (bootverbose) { 81767b87e44SAlexander Motin if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 81867b87e44SAlexander Motin ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 81967b87e44SAlexander Motin ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 82067b87e44SAlexander Motin device_printf(dev, "CONNECT requested\n"); 821ed70cffdSAlexander Motin } else 82267b87e44SAlexander Motin device_printf(dev, "DISCONNECT requested\n"); 82367b87e44SAlexander Motin } 824ed70cffdSAlexander Motin siis_reset(dev); 82583c5d981SAlexander Motin if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 82683c5d981SAlexander Motin return; 82783c5d981SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, 82883c5d981SAlexander Motin cam_sim_path(ch->sim), 82983c5d981SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 83083c5d981SAlexander Motin xpt_free_ccb(ccb); 83183c5d981SAlexander Motin return; 83283c5d981SAlexander Motin } 83383c5d981SAlexander Motin xpt_rescan(ccb); 83467b87e44SAlexander Motin } 83567b87e44SAlexander Motin } 83667b87e44SAlexander Motin 83767b87e44SAlexander Motin static void 83867b87e44SAlexander Motin siis_ch_intr_locked(void *data) 83967b87e44SAlexander Motin { 84067b87e44SAlexander Motin device_t dev = (device_t)data; 84167b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 84267b87e44SAlexander Motin 84367b87e44SAlexander Motin mtx_lock(&ch->mtx); 84467b87e44SAlexander Motin siis_ch_intr(data); 84567b87e44SAlexander Motin mtx_unlock(&ch->mtx); 84667b87e44SAlexander Motin } 84767b87e44SAlexander Motin 84867b87e44SAlexander Motin static void 84967b87e44SAlexander Motin siis_ch_intr(void *data) 85067b87e44SAlexander Motin { 85167b87e44SAlexander Motin device_t dev = (device_t)data; 85267b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 85367b87e44SAlexander Motin uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 85467b87e44SAlexander Motin enum siis_err_type et; 85567b87e44SAlexander Motin int i, ccs, port, tslots; 85667b87e44SAlexander Motin 85767b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 85867b87e44SAlexander Motin /* Read command statuses. */ 85967b87e44SAlexander Motin sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 86067b87e44SAlexander Motin ok = ch->rslots & ~sstatus; 861453130d9SPedro F. Giffuni /* Complete all successful commands. */ 86267b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 86367b87e44SAlexander Motin if ((ok >> i) & 1) 86467b87e44SAlexander Motin siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 86567b87e44SAlexander Motin } 86667b87e44SAlexander Motin /* Do we have any other events? */ 86767b87e44SAlexander Motin if ((sstatus & SIIS_P_SS_ATTN) == 0) 86867b87e44SAlexander Motin return; 86967b87e44SAlexander Motin /* Read and clear interrupt statuses. */ 87067b87e44SAlexander Motin istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 87167b87e44SAlexander Motin (0xFFFF & ~SIIS_P_IX_COMMCOMP); 87267b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 87367b87e44SAlexander Motin /* Process PHY events */ 87467b87e44SAlexander Motin if (istatus & SIIS_P_IX_PHYRDYCHG) 87567b87e44SAlexander Motin siis_phy_check_events(dev); 87635713642SAlexander Motin /* Process NOTIFY events */ 87735713642SAlexander Motin if (istatus & SIIS_P_IX_SDBN) 87835713642SAlexander Motin siis_notify_events(dev); 87967b87e44SAlexander Motin /* Process command errors */ 88067b87e44SAlexander Motin if (istatus & SIIS_P_IX_COMMERR) { 88167b87e44SAlexander Motin estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 88267b87e44SAlexander Motin ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 88367b87e44SAlexander Motin ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 88467b87e44SAlexander Motin port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 88567b87e44SAlexander Motin err = ch->rslots & sstatus; 88667b87e44SAlexander Motin //device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 88767b87e44SAlexander Motin // __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 88867b87e44SAlexander Motin // ATA_INL(ch->r_mem, SIIS_P_SERR)); 88967b87e44SAlexander Motin 890b8b7a902SAlexander Motin if (!ch->recoverycmd && !ch->recovery) { 89167b87e44SAlexander Motin xpt_freeze_simq(ch->sim, ch->numrslots); 89267b87e44SAlexander Motin ch->recovery = 1; 89367b87e44SAlexander Motin } 89467b87e44SAlexander Motin if (ch->frozen) { 89567b87e44SAlexander Motin union ccb *fccb = ch->frozen; 89667b87e44SAlexander Motin ch->frozen = NULL; 897e06db192SAlexander Motin fccb->ccb_h.status &= ~CAM_STATUS_MASK; 898e06db192SAlexander Motin fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 899e06db192SAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 900e06db192SAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1); 901e06db192SAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN; 902e06db192SAlexander Motin } 90367b87e44SAlexander Motin xpt_done(fccb); 90467b87e44SAlexander Motin } 90567b87e44SAlexander Motin if (estatus == SIIS_P_CMDERR_DEV || 90667b87e44SAlexander Motin estatus == SIIS_P_CMDERR_SDB || 90767b87e44SAlexander Motin estatus == SIIS_P_CMDERR_DATAFIS) { 90867b87e44SAlexander Motin tslots = ch->numtslots[port]; 90967b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 9106f9a51c7SAlexander Motin /* XXX: requests in loading state. */ 91167b87e44SAlexander Motin if (((ch->rslots >> i) & 1) == 0) 91267b87e44SAlexander Motin continue; 91367b87e44SAlexander Motin if (ch->slot[i].ccb->ccb_h.target_id != port) 91467b87e44SAlexander Motin continue; 91567b87e44SAlexander Motin if (tslots == 0) { 91667b87e44SAlexander Motin /* Untagged operation. */ 91767b87e44SAlexander Motin if (i == ccs) 91867b87e44SAlexander Motin et = SIIS_ERR_TFE; 91967b87e44SAlexander Motin else 92067b87e44SAlexander Motin et = SIIS_ERR_INNOCENT; 92167b87e44SAlexander Motin } else { 92267b87e44SAlexander Motin /* Tagged operation. */ 92367b87e44SAlexander Motin et = SIIS_ERR_NCQ; 92467b87e44SAlexander Motin } 92567b87e44SAlexander Motin siis_end_transaction(&ch->slot[i], et); 92667b87e44SAlexander Motin } 92767b87e44SAlexander Motin /* 92867b87e44SAlexander Motin * We can't reinit port if there are some other 92967b87e44SAlexander Motin * commands active, use resume to complete them. 93067b87e44SAlexander Motin */ 931b8b7a902SAlexander Motin if (ch->rslots != 0 && !ch->recoverycmd) 93267b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 93367b87e44SAlexander Motin } else { 93467b87e44SAlexander Motin if (estatus == SIIS_P_CMDERR_SENDFIS || 93567b87e44SAlexander Motin estatus == SIIS_P_CMDERR_INCSTATE || 93667b87e44SAlexander Motin estatus == SIIS_P_CMDERR_PPE || 93767b87e44SAlexander Motin estatus == SIIS_P_CMDERR_SERVICE) { 93867b87e44SAlexander Motin et = SIIS_ERR_SATA; 93967b87e44SAlexander Motin } else 94067b87e44SAlexander Motin et = SIIS_ERR_INVALID; 94167b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 9426f9a51c7SAlexander Motin /* XXX: requests in loading state. */ 94367b87e44SAlexander Motin if (((ch->rslots >> i) & 1) == 0) 94467b87e44SAlexander Motin continue; 94567b87e44SAlexander Motin siis_end_transaction(&ch->slot[i], et); 94667b87e44SAlexander Motin } 94767b87e44SAlexander Motin } 94867b87e44SAlexander Motin } 94967b87e44SAlexander Motin } 95067b87e44SAlexander Motin 95167b87e44SAlexander Motin /* Must be called with channel locked. */ 95267b87e44SAlexander Motin static int 95367b87e44SAlexander Motin siis_check_collision(device_t dev, union ccb *ccb) 95467b87e44SAlexander Motin { 95567b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 95667b87e44SAlexander Motin 95767b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 95867b87e44SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 959c8039fc6SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 960c8039fc6SAlexander Motin /* Tagged command while we have no supported tag free. */ 961c8039fc6SAlexander Motin if (((~ch->oslots) & (0x7fffffff >> (31 - 962c8039fc6SAlexander Motin ch->curr[ccb->ccb_h.target_id].tags))) == 0) 963c8039fc6SAlexander Motin return (1); 964c8039fc6SAlexander Motin } 965c8039fc6SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 96667b87e44SAlexander Motin (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 96767b87e44SAlexander Motin /* Atomic command while anything active. */ 96867b87e44SAlexander Motin if (ch->numrslots != 0) 96967b87e44SAlexander Motin return (1); 97067b87e44SAlexander Motin } 97167b87e44SAlexander Motin /* We have some atomic command running. */ 97267b87e44SAlexander Motin if (ch->aslots != 0) 97367b87e44SAlexander Motin return (1); 97467b87e44SAlexander Motin return (0); 97567b87e44SAlexander Motin } 97667b87e44SAlexander Motin 97767b87e44SAlexander Motin /* Must be called with channel locked. */ 97867b87e44SAlexander Motin static void 97967b87e44SAlexander Motin siis_begin_transaction(device_t dev, union ccb *ccb) 98067b87e44SAlexander Motin { 98167b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 98267b87e44SAlexander Motin struct siis_slot *slot; 983c8039fc6SAlexander Motin int tag, tags; 98467b87e44SAlexander Motin 98567b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 98667b87e44SAlexander Motin /* Choose empty slot. */ 987c8039fc6SAlexander Motin tags = SIIS_MAX_SLOTS; 988c8039fc6SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 989c8039fc6SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) 990c8039fc6SAlexander Motin tags = ch->curr[ccb->ccb_h.target_id].tags; 991c8039fc6SAlexander Motin tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1; 99267b87e44SAlexander Motin /* Occupy chosen slot. */ 99367b87e44SAlexander Motin slot = &ch->slot[tag]; 99467b87e44SAlexander Motin slot->ccb = ccb; 99567b87e44SAlexander Motin /* Update channel stats. */ 996c8039fc6SAlexander Motin ch->oslots |= (1 << slot->slot); 99767b87e44SAlexander Motin ch->numrslots++; 99867b87e44SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 99967b87e44SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 100067b87e44SAlexander Motin ch->numtslots[ccb->ccb_h.target_id]++; 100167b87e44SAlexander Motin } 100267b87e44SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 100367b87e44SAlexander Motin (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 100467b87e44SAlexander Motin ch->aslots |= (1 << slot->slot); 100567b87e44SAlexander Motin slot->dma.nsegs = 0; 100667b87e44SAlexander Motin /* If request moves data, setup and load SG list */ 100767b87e44SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 100867b87e44SAlexander Motin slot->state = SIIS_SLOT_LOADING; 1009dd0b4fb6SKonstantin Belousov bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, 1010dd0b4fb6SKonstantin Belousov ccb, siis_dmasetprd, slot, 0); 101167b87e44SAlexander Motin } else 101267b87e44SAlexander Motin siis_execute_transaction(slot); 101367b87e44SAlexander Motin } 101467b87e44SAlexander Motin 101567b87e44SAlexander Motin /* Locked by busdma engine. */ 101667b87e44SAlexander Motin static void 101767b87e44SAlexander Motin siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 101867b87e44SAlexander Motin { 101967b87e44SAlexander Motin struct siis_slot *slot = arg; 102067b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(slot->dev); 102167b87e44SAlexander Motin struct siis_cmd *ctp; 102267b87e44SAlexander Motin struct siis_dma_prd *prd; 102367b87e44SAlexander Motin int i; 102467b87e44SAlexander Motin 102567b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 102667b87e44SAlexander Motin if (error) { 102767b87e44SAlexander Motin device_printf(slot->dev, "DMA load error\n"); 1028b8b7a902SAlexander Motin if (!ch->recoverycmd) 102967b87e44SAlexander Motin xpt_freeze_simq(ch->sim, 1); 103067b87e44SAlexander Motin siis_end_transaction(slot, SIIS_ERR_INVALID); 103167b87e44SAlexander Motin return; 103267b87e44SAlexander Motin } 103367b87e44SAlexander Motin KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 1034dd0b4fb6SKonstantin Belousov slot->dma.nsegs = nsegs; 1035dd0b4fb6SKonstantin Belousov if (nsegs != 0) { 103667b87e44SAlexander Motin /* Get a piece of the workspace for this request */ 1037dd0b4fb6SKonstantin Belousov ctp = (struct siis_cmd *)(ch->dma.work + SIIS_CT_OFFSET + 1038dd0b4fb6SKonstantin Belousov (SIIS_CT_SIZE * slot->slot)); 103967b87e44SAlexander Motin /* Fill S/G table */ 104067b87e44SAlexander Motin if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 104167b87e44SAlexander Motin prd = &ctp->u.ata.prd[0]; 104267b87e44SAlexander Motin else 104367b87e44SAlexander Motin prd = &ctp->u.atapi.prd[0]; 104467b87e44SAlexander Motin for (i = 0; i < nsegs; i++) { 104567b87e44SAlexander Motin prd[i].dba = htole64(segs[i].ds_addr); 104667b87e44SAlexander Motin prd[i].dbc = htole32(segs[i].ds_len); 104767b87e44SAlexander Motin prd[i].control = 0; 104867b87e44SAlexander Motin } 104967b87e44SAlexander Motin prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 105067b87e44SAlexander Motin bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 105167b87e44SAlexander Motin ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 105267b87e44SAlexander Motin BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1053dd0b4fb6SKonstantin Belousov } 105467b87e44SAlexander Motin siis_execute_transaction(slot); 105567b87e44SAlexander Motin } 105667b87e44SAlexander Motin 105767b87e44SAlexander Motin /* Must be called with channel locked. */ 105867b87e44SAlexander Motin static void 105967b87e44SAlexander Motin siis_execute_transaction(struct siis_slot *slot) 106067b87e44SAlexander Motin { 106167b87e44SAlexander Motin device_t dev = slot->dev; 106267b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 106367b87e44SAlexander Motin struct siis_cmd *ctp; 106467b87e44SAlexander Motin union ccb *ccb = slot->ccb; 106567b87e44SAlexander Motin u_int64_t prb_bus; 106667b87e44SAlexander Motin 106767b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 106867b87e44SAlexander Motin /* Get a piece of the workspace for this request */ 106967b87e44SAlexander Motin ctp = (struct siis_cmd *) 107067b87e44SAlexander Motin (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 107167b87e44SAlexander Motin ctp->control = 0; 107267b87e44SAlexander Motin ctp->protocol_override = 0; 107367b87e44SAlexander Motin ctp->transfer_count = 0; 107467b87e44SAlexander Motin /* Special handling for Soft Reset command. */ 1075723bd8c6SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1076723bd8c6SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) { 107767b87e44SAlexander Motin ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 1078723bd8c6SAlexander Motin } else { 1079723bd8c6SAlexander Motin ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE); 1080723bd8c6SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1081723bd8c6SAlexander Motin ctp->protocol_override |= 1082723bd8c6SAlexander Motin htole16(SIIS_PRB_PROTO_NCQ); 1083723bd8c6SAlexander Motin } 1084723bd8c6SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1085723bd8c6SAlexander Motin ctp->protocol_override |= 1086723bd8c6SAlexander Motin htole16(SIIS_PRB_PROTO_READ); 1087723bd8c6SAlexander Motin } else 1088723bd8c6SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1089723bd8c6SAlexander Motin ctp->protocol_override |= 1090723bd8c6SAlexander Motin htole16(SIIS_PRB_PROTO_WRITE); 1091723bd8c6SAlexander Motin } 1092723bd8c6SAlexander Motin } 109367b87e44SAlexander Motin } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1094723bd8c6SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 109567b87e44SAlexander Motin ctp->control |= htole16(SIIS_PRB_PACKET_READ); 1096723bd8c6SAlexander Motin else 1097723bd8c6SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 109867b87e44SAlexander Motin ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 109967b87e44SAlexander Motin } 110083c5d981SAlexander Motin /* Special handling for Soft Reset command. */ 110183c5d981SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 110283c5d981SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 110383c5d981SAlexander Motin (ccb->ataio.cmd.control & ATA_A_RESET)) { 110483c5d981SAlexander Motin /* Kick controller into sane state */ 110583c5d981SAlexander Motin siis_portinit(dev); 110683c5d981SAlexander Motin } 110767b87e44SAlexander Motin /* Setup the FIS for this request */ 1108b447e682SAlexander Motin if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) { 110967b87e44SAlexander Motin device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1110b8b7a902SAlexander Motin if (!ch->recoverycmd) 111167b87e44SAlexander Motin xpt_freeze_simq(ch->sim, 1); 111267b87e44SAlexander Motin siis_end_transaction(slot, SIIS_ERR_INVALID); 111367b87e44SAlexander Motin return; 111467b87e44SAlexander Motin } 111567b87e44SAlexander Motin bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 111667b87e44SAlexander Motin BUS_DMASYNC_PREWRITE); 111767b87e44SAlexander Motin /* Issue command to the controller. */ 111867b87e44SAlexander Motin slot->state = SIIS_SLOT_RUNNING; 111967b87e44SAlexander Motin ch->rslots |= (1 << slot->slot); 112067b87e44SAlexander Motin prb_bus = ch->dma.work_bus + 112167b87e44SAlexander Motin SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 112267b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 112367b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 112467b87e44SAlexander Motin /* Start command execution timeout */ 112585c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout, 0, 112665d2f9c1SJohn Baldwin siis_timeout, slot, 0); 112767b87e44SAlexander Motin return; 112867b87e44SAlexander Motin } 112967b87e44SAlexander Motin 11306f9a51c7SAlexander Motin /* Must be called with channel locked. */ 11316f9a51c7SAlexander Motin static void 11326f9a51c7SAlexander Motin siis_process_timeout(device_t dev) 11336f9a51c7SAlexander Motin { 11346f9a51c7SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 11356f9a51c7SAlexander Motin int i; 11366f9a51c7SAlexander Motin 11376f9a51c7SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 1138b8b7a902SAlexander Motin if (!ch->recoverycmd && !ch->recovery) { 11396f9a51c7SAlexander Motin xpt_freeze_simq(ch->sim, ch->numrslots); 11406f9a51c7SAlexander Motin ch->recovery = 1; 11416f9a51c7SAlexander Motin } 11426f9a51c7SAlexander Motin /* Handle the rest of commands. */ 11436f9a51c7SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 11446f9a51c7SAlexander Motin /* Do we have a running request on slot? */ 11456f9a51c7SAlexander Motin if (ch->slot[i].state < SIIS_SLOT_RUNNING) 11466f9a51c7SAlexander Motin continue; 11476f9a51c7SAlexander Motin siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT); 11486f9a51c7SAlexander Motin } 11496f9a51c7SAlexander Motin } 11506f9a51c7SAlexander Motin 1151301c88beSAlexander Motin /* Must be called with channel locked. */ 1152301c88beSAlexander Motin static void 1153301c88beSAlexander Motin siis_rearm_timeout(device_t dev) 1154301c88beSAlexander Motin { 1155301c88beSAlexander Motin struct siis_channel *ch = device_get_softc(dev); 1156301c88beSAlexander Motin int i; 1157301c88beSAlexander Motin 1158301c88beSAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 1159301c88beSAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1160301c88beSAlexander Motin struct siis_slot *slot = &ch->slot[i]; 1161301c88beSAlexander Motin 1162301c88beSAlexander Motin /* Do we have a running request on slot? */ 1163301c88beSAlexander Motin if (slot->state < SIIS_SLOT_RUNNING) 1164301c88beSAlexander Motin continue; 1165301c88beSAlexander Motin if ((ch->toslots & (1 << i)) == 0) 1166301c88beSAlexander Motin continue; 116785c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, 116885c9dd9dSSteven Hartland SBT_1MS * slot->ccb->ccb_h.timeout, 0, 116965d2f9c1SJohn Baldwin siis_timeout, slot, 0); 1170301c88beSAlexander Motin } 1171301c88beSAlexander Motin } 1172301c88beSAlexander Motin 117367b87e44SAlexander Motin /* Locked by callout mechanism. */ 117467b87e44SAlexander Motin static void 117565d2f9c1SJohn Baldwin siis_timeout(void *arg) 117667b87e44SAlexander Motin { 117765d2f9c1SJohn Baldwin struct siis_slot *slot = arg; 117867b87e44SAlexander Motin device_t dev = slot->dev; 117967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 118008c8fde0SAlexander Motin union ccb *ccb = slot->ccb; 118167b87e44SAlexander Motin 118267b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 1183c82b245aSAlexander Motin /* Check for stale timeout. */ 1184c82b245aSAlexander Motin if (slot->state < SIIS_SLOT_RUNNING) 1185c82b245aSAlexander Motin return; 118608c8fde0SAlexander Motin 118708c8fde0SAlexander Motin /* Handle soft-reset timeouts without doing hard-reset. */ 118808c8fde0SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 118908c8fde0SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 119008c8fde0SAlexander Motin (ccb->ataio.cmd.control & ATA_A_RESET)) { 119108c8fde0SAlexander Motin xpt_freeze_simq(ch->sim, ch->numrslots); 119208c8fde0SAlexander Motin siis_end_transaction(slot, SIIS_ERR_TFE); 119308c8fde0SAlexander Motin return; 119408c8fde0SAlexander Motin } 119508c8fde0SAlexander Motin 119667b87e44SAlexander Motin device_printf(dev, "Timeout on slot %d\n", slot->slot); 119767b87e44SAlexander Motin device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 119883c5d981SAlexander Motin __func__, ATA_INL(ch->r_mem, SIIS_P_IS), 119983c5d981SAlexander Motin ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 120067b87e44SAlexander Motin ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 120167b87e44SAlexander Motin ATA_INL(ch->r_mem, SIIS_P_SERR)); 120267b87e44SAlexander Motin 12036f9a51c7SAlexander Motin if (ch->toslots == 0) 12046f9a51c7SAlexander Motin xpt_freeze_simq(ch->sim, 1); 12056f9a51c7SAlexander Motin ch->toslots |= (1 << slot->slot); 12066f9a51c7SAlexander Motin if ((ch->rslots & ~ch->toslots) == 0) 12076f9a51c7SAlexander Motin siis_process_timeout(dev); 12086f9a51c7SAlexander Motin else 12096f9a51c7SAlexander Motin device_printf(dev, " ... waiting for slots %08x\n", 12106f9a51c7SAlexander Motin ch->rslots & ~ch->toslots); 121167b87e44SAlexander Motin } 121267b87e44SAlexander Motin 121367b87e44SAlexander Motin /* Must be called with channel locked. */ 121467b87e44SAlexander Motin static void 121567b87e44SAlexander Motin siis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 121667b87e44SAlexander Motin { 121767b87e44SAlexander Motin device_t dev = slot->dev; 121867b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 121967b87e44SAlexander Motin union ccb *ccb = slot->ccb; 1220bf12976cSAlexander Motin int lastto; 122167b87e44SAlexander Motin 122267b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 122367b87e44SAlexander Motin bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 122467b87e44SAlexander Motin BUS_DMASYNC_POSTWRITE); 122567b87e44SAlexander Motin /* Read result registers to the result struct 122667b87e44SAlexander Motin * May be incorrect if several commands finished same time, 122767b87e44SAlexander Motin * so read only when sure or have to. 122867b87e44SAlexander Motin */ 122967b87e44SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 123067b87e44SAlexander Motin struct ata_res *res = &ccb->ataio.res; 123167b87e44SAlexander Motin if ((et == SIIS_ERR_TFE) || 123267b87e44SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 123367b87e44SAlexander Motin int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 123467b87e44SAlexander Motin 123567b87e44SAlexander Motin res->status = ATA_INB(ch->r_mem, offs + 2); 123667b87e44SAlexander Motin res->error = ATA_INB(ch->r_mem, offs + 3); 123767b87e44SAlexander Motin res->lba_low = ATA_INB(ch->r_mem, offs + 4); 123867b87e44SAlexander Motin res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 123967b87e44SAlexander Motin res->lba_high = ATA_INB(ch->r_mem, offs + 6); 124067b87e44SAlexander Motin res->device = ATA_INB(ch->r_mem, offs + 7); 124167b87e44SAlexander Motin res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 124267b87e44SAlexander Motin res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 124367b87e44SAlexander Motin res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 124467b87e44SAlexander Motin res->sector_count = ATA_INB(ch->r_mem, offs + 12); 124567b87e44SAlexander Motin res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 124667b87e44SAlexander Motin } else 124767b87e44SAlexander Motin bzero(res, sizeof(*res)); 1248ba3a9995SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1249ba3a9995SAlexander Motin ch->numrslots == 1) { 1250ba3a9995SAlexander Motin ccb->ataio.resid = ccb->ataio.dxfer_len - 1251ba3a9995SAlexander Motin ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1252ba3a9995SAlexander Motin } 1253ba3a9995SAlexander Motin } else { 1254ba3a9995SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1255ba3a9995SAlexander Motin ch->numrslots == 1) { 1256ba3a9995SAlexander Motin ccb->csio.resid = ccb->csio.dxfer_len - 1257ba3a9995SAlexander Motin ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1258ba3a9995SAlexander Motin } 125967b87e44SAlexander Motin } 126067b87e44SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 126167b87e44SAlexander Motin bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 126267b87e44SAlexander Motin (ccb->ccb_h.flags & CAM_DIR_IN) ? 126367b87e44SAlexander Motin BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 126467b87e44SAlexander Motin bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 126567b87e44SAlexander Motin } 126667b87e44SAlexander Motin /* Set proper result status. */ 126767b87e44SAlexander Motin if (et != SIIS_ERR_NONE || ch->recovery) { 126867b87e44SAlexander Motin ch->eslots |= (1 << slot->slot); 126967b87e44SAlexander Motin ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 127067b87e44SAlexander Motin } 1271e06db192SAlexander Motin /* In case of error, freeze device for proper recovery. */ 1272b8b7a902SAlexander Motin if (et != SIIS_ERR_NONE && (!ch->recoverycmd) && 1273e06db192SAlexander Motin !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1274e06db192SAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1); 1275e06db192SAlexander Motin ccb->ccb_h.status |= CAM_DEV_QFRZN; 1276e06db192SAlexander Motin } 1277e06db192SAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK; 127867b87e44SAlexander Motin switch (et) { 127967b87e44SAlexander Motin case SIIS_ERR_NONE: 128067b87e44SAlexander Motin ccb->ccb_h.status |= CAM_REQ_CMP; 128167b87e44SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) 128267b87e44SAlexander Motin ccb->csio.scsi_status = SCSI_STATUS_OK; 128367b87e44SAlexander Motin break; 128467b87e44SAlexander Motin case SIIS_ERR_INVALID: 12856f9a51c7SAlexander Motin ch->fatalerr = 1; 128667b87e44SAlexander Motin ccb->ccb_h.status |= CAM_REQ_INVALID; 128767b87e44SAlexander Motin break; 128867b87e44SAlexander Motin case SIIS_ERR_INNOCENT: 128967b87e44SAlexander Motin ccb->ccb_h.status |= CAM_REQUEUE_REQ; 129067b87e44SAlexander Motin break; 129167b87e44SAlexander Motin case SIIS_ERR_TFE: 1292e06db192SAlexander Motin case SIIS_ERR_NCQ: 129367b87e44SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 129467b87e44SAlexander Motin ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 129567b87e44SAlexander Motin ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 129667b87e44SAlexander Motin } else { 129767b87e44SAlexander Motin ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 129867b87e44SAlexander Motin } 129967b87e44SAlexander Motin break; 130067b87e44SAlexander Motin case SIIS_ERR_SATA: 13016f9a51c7SAlexander Motin ch->fatalerr = 1; 130267b87e44SAlexander Motin ccb->ccb_h.status |= CAM_UNCOR_PARITY; 130367b87e44SAlexander Motin break; 130467b87e44SAlexander Motin case SIIS_ERR_TIMEOUT: 13056f9a51c7SAlexander Motin ch->fatalerr = 1; 130667b87e44SAlexander Motin ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 130767b87e44SAlexander Motin break; 130867b87e44SAlexander Motin default: 130967b87e44SAlexander Motin ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 131067b87e44SAlexander Motin } 131167b87e44SAlexander Motin /* Free slot. */ 1312c8039fc6SAlexander Motin ch->oslots &= ~(1 << slot->slot); 131367b87e44SAlexander Motin ch->rslots &= ~(1 << slot->slot); 131467b87e44SAlexander Motin ch->aslots &= ~(1 << slot->slot); 131567b87e44SAlexander Motin slot->state = SIIS_SLOT_EMPTY; 131667b87e44SAlexander Motin slot->ccb = NULL; 131767b87e44SAlexander Motin /* Update channel stats. */ 131867b87e44SAlexander Motin ch->numrslots--; 131967b87e44SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 132067b87e44SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 132167b87e44SAlexander Motin ch->numtslots[ccb->ccb_h.target_id]--; 132267b87e44SAlexander Motin } 1323bf12976cSAlexander Motin /* Cancel timeout state if request completed normally. */ 1324bf12976cSAlexander Motin if (et != SIIS_ERR_TIMEOUT) { 1325bf12976cSAlexander Motin lastto = (ch->toslots == (1 << slot->slot)); 1326bf12976cSAlexander Motin ch->toslots &= ~(1 << slot->slot); 1327bf12976cSAlexander Motin if (lastto) 1328bf12976cSAlexander Motin xpt_release_simq(ch->sim, TRUE); 1329bf12976cSAlexander Motin } 13306f9a51c7SAlexander Motin /* If it was our READ LOG command - process it. */ 1331b8b7a902SAlexander Motin if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 13326f9a51c7SAlexander Motin siis_process_read_log(dev, ccb); 1333b8b7a902SAlexander Motin /* If it was our REQUEST SENSE command - process it. */ 1334b8b7a902SAlexander Motin } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 1335b8b7a902SAlexander Motin siis_process_request_sense(dev, ccb); 1336b8b7a902SAlexander Motin /* If it was NCQ or ATAPI command error, put result on hold. */ 1337b8b7a902SAlexander Motin } else if (et == SIIS_ERR_NCQ || 1338b8b7a902SAlexander Motin ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 1339b8b7a902SAlexander Motin (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 134067b87e44SAlexander Motin ch->hold[slot->slot] = ccb; 134167b87e44SAlexander Motin ch->numhslots++; 13426f9a51c7SAlexander Motin } else 134367b87e44SAlexander Motin xpt_done(ccb); 134467b87e44SAlexander Motin /* If we have no other active commands, ... */ 134567b87e44SAlexander Motin if (ch->rslots == 0) { 13466f9a51c7SAlexander Motin /* if there were timeouts or fatal error - reset port. */ 13476f9a51c7SAlexander Motin if (ch->toslots != 0 || ch->fatalerr) { 13486f9a51c7SAlexander Motin siis_reset(dev); 13496f9a51c7SAlexander Motin } else { 135067b87e44SAlexander Motin /* if we have slots in error, we can reinit port. */ 135167b87e44SAlexander Motin if (ch->eslots != 0) 135267b87e44SAlexander Motin siis_portinit(dev); 1353b8b7a902SAlexander Motin /* if there commands on hold, we can do recovery. */ 1354b8b7a902SAlexander Motin if (!ch->recoverycmd && ch->numhslots) 1355b8b7a902SAlexander Motin siis_issue_recovery(dev); 135667b87e44SAlexander Motin } 13576f9a51c7SAlexander Motin /* If all the reset of commands are in timeout - abort them. */ 1358301c88beSAlexander Motin } else if ((ch->rslots & ~ch->toslots) == 0 && 1359301c88beSAlexander Motin et != SIIS_ERR_TIMEOUT) 1360301c88beSAlexander Motin siis_rearm_timeout(dev); 136108c8fde0SAlexander Motin /* Unfreeze frozen command. */ 136208c8fde0SAlexander Motin if (ch->frozen && !siis_check_collision(dev, ch->frozen)) { 136308c8fde0SAlexander Motin union ccb *fccb = ch->frozen; 136408c8fde0SAlexander Motin ch->frozen = NULL; 136508c8fde0SAlexander Motin siis_begin_transaction(dev, fccb); 136608c8fde0SAlexander Motin xpt_release_simq(ch->sim, TRUE); 136708c8fde0SAlexander Motin } 136867b87e44SAlexander Motin } 136967b87e44SAlexander Motin 137067b87e44SAlexander Motin static void 1371b8b7a902SAlexander Motin siis_issue_recovery(device_t dev) 137267b87e44SAlexander Motin { 137367b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 137467b87e44SAlexander Motin union ccb *ccb; 137567b87e44SAlexander Motin struct ccb_ataio *ataio; 1376b8b7a902SAlexander Motin struct ccb_scsiio *csio; 137767b87e44SAlexander Motin int i; 137867b87e44SAlexander Motin 13797bcc5957SAlexander Motin /* Find some held command. */ 138067b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 138167b87e44SAlexander Motin if (ch->hold[i]) 138267b87e44SAlexander Motin break; 138367b87e44SAlexander Motin } 138467b87e44SAlexander Motin if (i == SIIS_MAX_SLOTS) 138567b87e44SAlexander Motin return; 138667b87e44SAlexander Motin ccb = xpt_alloc_ccb_nowait(); 138767b87e44SAlexander Motin if (ccb == NULL) { 13887bcc5957SAlexander Motin device_printf(dev, "Unable to allocate recovery command\n"); 13896ac0befdSAlexander Motin completeall: 13907bcc5957SAlexander Motin /* We can't do anything -- complete held commands. */ 13916ac0befdSAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 13926ac0befdSAlexander Motin if (ch->hold[i] == NULL) 13936ac0befdSAlexander Motin continue; 13946ac0befdSAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 13956ac0befdSAlexander Motin ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL; 13966ac0befdSAlexander Motin xpt_done(ch->hold[i]); 13976ac0befdSAlexander Motin ch->hold[i] = NULL; 13986ac0befdSAlexander Motin ch->numhslots--; 13996ac0befdSAlexander Motin } 14006ac0befdSAlexander Motin siis_reset(dev); 14016ac0befdSAlexander Motin return; 140267b87e44SAlexander Motin } 140367b87e44SAlexander Motin ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1404b8b7a902SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1405b8b7a902SAlexander Motin /* READ LOG */ 1406b8b7a902SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 140767b87e44SAlexander Motin ccb->ccb_h.func_code = XPT_ATA_IO; 140867b87e44SAlexander Motin ccb->ccb_h.flags = CAM_DIR_IN; 140967b87e44SAlexander Motin ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 141067b87e44SAlexander Motin ataio = &ccb->ataio; 141167b87e44SAlexander Motin ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 141267b87e44SAlexander Motin if (ataio->data_ptr == NULL) { 14138d659f34SAlexander Motin xpt_free_ccb(ccb); 14146ac0befdSAlexander Motin device_printf(dev, 14157bcc5957SAlexander Motin "Unable to allocate memory for READ LOG command\n"); 14166ac0befdSAlexander Motin goto completeall; 141767b87e44SAlexander Motin } 141867b87e44SAlexander Motin ataio->dxfer_len = 512; 141967b87e44SAlexander Motin bzero(&ataio->cmd, sizeof(ataio->cmd)); 142067b87e44SAlexander Motin ataio->cmd.flags = CAM_ATAIO_48BIT; 142167b87e44SAlexander Motin ataio->cmd.command = 0x2F; /* READ LOG EXT */ 142267b87e44SAlexander Motin ataio->cmd.sector_count = 1; 142367b87e44SAlexander Motin ataio->cmd.sector_count_exp = 0; 142467b87e44SAlexander Motin ataio->cmd.lba_low = 0x10; 142567b87e44SAlexander Motin ataio->cmd.lba_mid = 0; 142667b87e44SAlexander Motin ataio->cmd.lba_mid_exp = 0; 1427b8b7a902SAlexander Motin } else { 1428b8b7a902SAlexander Motin /* REQUEST SENSE */ 1429b8b7a902SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 1430b8b7a902SAlexander Motin ccb->ccb_h.recovery_slot = i; 1431b8b7a902SAlexander Motin ccb->ccb_h.func_code = XPT_SCSI_IO; 1432b8b7a902SAlexander Motin ccb->ccb_h.flags = CAM_DIR_IN; 1433b8b7a902SAlexander Motin ccb->ccb_h.status = 0; 1434b8b7a902SAlexander Motin ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1435b8b7a902SAlexander Motin csio = &ccb->csio; 1436b8b7a902SAlexander Motin csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 1437b8b7a902SAlexander Motin csio->dxfer_len = ch->hold[i]->csio.sense_len; 1438b8b7a902SAlexander Motin csio->cdb_len = 6; 1439b8b7a902SAlexander Motin bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 1440b8b7a902SAlexander Motin csio->cdb_io.cdb_bytes[0] = 0x03; 1441b8b7a902SAlexander Motin csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 1442b8b7a902SAlexander Motin } 14436ac0befdSAlexander Motin ch->recoverycmd = 1; 144467b87e44SAlexander Motin siis_begin_transaction(dev, ccb); 144567b87e44SAlexander Motin } 144667b87e44SAlexander Motin 144767b87e44SAlexander Motin static void 144867b87e44SAlexander Motin siis_process_read_log(device_t dev, union ccb *ccb) 144967b87e44SAlexander Motin { 145067b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 145167b87e44SAlexander Motin uint8_t *data; 145267b87e44SAlexander Motin struct ata_res *res; 145367b87e44SAlexander Motin int i; 145467b87e44SAlexander Motin 1455b8b7a902SAlexander Motin ch->recoverycmd = 0; 145667b87e44SAlexander Motin data = ccb->ataio.data_ptr; 145767b87e44SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 145867b87e44SAlexander Motin (data[0] & 0x80) == 0) { 145967b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 146067b87e44SAlexander Motin if (!ch->hold[i]) 146167b87e44SAlexander Motin continue; 146267b87e44SAlexander Motin if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 146367b87e44SAlexander Motin continue; 146467b87e44SAlexander Motin if ((data[0] & 0x1F) == i) { 146567b87e44SAlexander Motin res = &ch->hold[i]->ataio.res; 146667b87e44SAlexander Motin res->status = data[2]; 146767b87e44SAlexander Motin res->error = data[3]; 146867b87e44SAlexander Motin res->lba_low = data[4]; 146967b87e44SAlexander Motin res->lba_mid = data[5]; 147067b87e44SAlexander Motin res->lba_high = data[6]; 147167b87e44SAlexander Motin res->device = data[7]; 147267b87e44SAlexander Motin res->lba_low_exp = data[8]; 147367b87e44SAlexander Motin res->lba_mid_exp = data[9]; 147467b87e44SAlexander Motin res->lba_high_exp = data[10]; 147567b87e44SAlexander Motin res->sector_count = data[12]; 147667b87e44SAlexander Motin res->sector_count_exp = data[13]; 147767b87e44SAlexander Motin } else { 147867b87e44SAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 147967b87e44SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 148067b87e44SAlexander Motin } 148167b87e44SAlexander Motin xpt_done(ch->hold[i]); 148267b87e44SAlexander Motin ch->hold[i] = NULL; 148367b87e44SAlexander Motin ch->numhslots--; 148467b87e44SAlexander Motin } 148567b87e44SAlexander Motin } else { 148667b87e44SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 148767b87e44SAlexander Motin device_printf(dev, "Error while READ LOG EXT\n"); 148867b87e44SAlexander Motin else if ((data[0] & 0x80) == 0) { 148967b87e44SAlexander Motin device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 149067b87e44SAlexander Motin } 149167b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 149267b87e44SAlexander Motin if (!ch->hold[i]) 149367b87e44SAlexander Motin continue; 149467b87e44SAlexander Motin if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 149567b87e44SAlexander Motin continue; 149667b87e44SAlexander Motin xpt_done(ch->hold[i]); 149767b87e44SAlexander Motin ch->hold[i] = NULL; 149867b87e44SAlexander Motin ch->numhslots--; 149967b87e44SAlexander Motin } 150067b87e44SAlexander Motin } 150167b87e44SAlexander Motin free(ccb->ataio.data_ptr, M_SIIS); 150267b87e44SAlexander Motin xpt_free_ccb(ccb); 150367b87e44SAlexander Motin } 150467b87e44SAlexander Motin 150567b87e44SAlexander Motin static void 1506b8b7a902SAlexander Motin siis_process_request_sense(device_t dev, union ccb *ccb) 1507b8b7a902SAlexander Motin { 1508b8b7a902SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 1509b8b7a902SAlexander Motin int i; 1510b8b7a902SAlexander Motin 1511b8b7a902SAlexander Motin ch->recoverycmd = 0; 1512b8b7a902SAlexander Motin 1513b8b7a902SAlexander Motin i = ccb->ccb_h.recovery_slot; 1514b8b7a902SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1515b8b7a902SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 1516b8b7a902SAlexander Motin } else { 1517b8b7a902SAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1518b8b7a902SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 1519b8b7a902SAlexander Motin } 1520b8b7a902SAlexander Motin xpt_done(ch->hold[i]); 1521b8b7a902SAlexander Motin ch->hold[i] = NULL; 1522b8b7a902SAlexander Motin ch->numhslots--; 1523b8b7a902SAlexander Motin xpt_free_ccb(ccb); 1524b8b7a902SAlexander Motin } 1525b8b7a902SAlexander Motin 1526b8b7a902SAlexander Motin static void 152767b87e44SAlexander Motin siis_portinit(device_t dev) 152867b87e44SAlexander Motin { 152967b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 153067b87e44SAlexander Motin int i; 153167b87e44SAlexander Motin 153267b87e44SAlexander Motin ch->eslots = 0; 153367b87e44SAlexander Motin ch->recovery = 0; 153467b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 153567b87e44SAlexander Motin for (i = 0; i < 16; i++) { 153667b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 153767b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 153867b87e44SAlexander Motin } 153967b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 154067b87e44SAlexander Motin siis_wait_ready(dev, 1000); 154167b87e44SAlexander Motin } 154267b87e44SAlexander Motin 154327acd6eaSAlexander Motin static int 154467b87e44SAlexander Motin siis_devreset(device_t dev) 154567b87e44SAlexander Motin { 154667b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 154727acd6eaSAlexander Motin int timeout = 0; 154827acd6eaSAlexander Motin uint32_t val; 154967b87e44SAlexander Motin 155067b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 155127acd6eaSAlexander Motin while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 155227acd6eaSAlexander Motin SIIS_P_CTL_DEV_RESET) != 0) { 15531f145eafSAlexander Motin DELAY(100); 15541f145eafSAlexander Motin if (timeout++ > 1000) { 15551f145eafSAlexander Motin device_printf(dev, "device reset stuck " 15561f145eafSAlexander Motin "(timeout 100ms) status = %08x\n", val); 155727acd6eaSAlexander Motin return (EBUSY); 155867b87e44SAlexander Motin } 155927acd6eaSAlexander Motin } 156027acd6eaSAlexander Motin return (0); 156127acd6eaSAlexander Motin } 156267b87e44SAlexander Motin 156367b87e44SAlexander Motin static int 156467b87e44SAlexander Motin siis_wait_ready(device_t dev, int t) 156567b87e44SAlexander Motin { 156667b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 156767b87e44SAlexander Motin int timeout = 0; 156867b87e44SAlexander Motin uint32_t val; 156967b87e44SAlexander Motin 157067b87e44SAlexander Motin while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 157167b87e44SAlexander Motin SIIS_P_CTL_READY) == 0) { 157267b87e44SAlexander Motin DELAY(1000); 157367b87e44SAlexander Motin if (timeout++ > t) { 157467b87e44SAlexander Motin device_printf(dev, "port is not ready (timeout %dms) " 157567b87e44SAlexander Motin "status = %08x\n", t, val); 157667b87e44SAlexander Motin return (EBUSY); 157767b87e44SAlexander Motin } 157867b87e44SAlexander Motin } 157967b87e44SAlexander Motin return (0); 158067b87e44SAlexander Motin } 158167b87e44SAlexander Motin 158267b87e44SAlexander Motin static void 158367b87e44SAlexander Motin siis_reset(device_t dev) 158467b87e44SAlexander Motin { 158567b87e44SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 1586b447e682SAlexander Motin int i, retry = 0, sata_rev; 158727acd6eaSAlexander Motin uint32_t val; 158867b87e44SAlexander Motin 158983c5d981SAlexander Motin xpt_freeze_simq(ch->sim, 1); 159067b87e44SAlexander Motin if (bootverbose) 159167b87e44SAlexander Motin device_printf(dev, "SIIS reset...\n"); 1592b8b7a902SAlexander Motin if (!ch->recoverycmd && !ch->recovery) 159367b87e44SAlexander Motin xpt_freeze_simq(ch->sim, ch->numrslots); 15946f9a51c7SAlexander Motin /* Requeue frozen command. */ 159567b87e44SAlexander Motin if (ch->frozen) { 159667b87e44SAlexander Motin union ccb *fccb = ch->frozen; 159767b87e44SAlexander Motin ch->frozen = NULL; 1598e06db192SAlexander Motin fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1599e06db192SAlexander Motin fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1600e06db192SAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1601e06db192SAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1); 1602e06db192SAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN; 1603e06db192SAlexander Motin } 160467b87e44SAlexander Motin xpt_done(fccb); 160567b87e44SAlexander Motin } 160627acd6eaSAlexander Motin /* Requeue all running commands. */ 160767b87e44SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 160867b87e44SAlexander Motin /* Do we have a running request on slot? */ 160967b87e44SAlexander Motin if (ch->slot[i].state < SIIS_SLOT_RUNNING) 161067b87e44SAlexander Motin continue; 161167b87e44SAlexander Motin /* XXX; Commands in loading state. */ 161267b87e44SAlexander Motin siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 161367b87e44SAlexander Motin } 16147bcc5957SAlexander Motin /* Finish all held commands as-is. */ 16156f9a51c7SAlexander Motin for (i = 0; i < SIIS_MAX_SLOTS; i++) { 16166f9a51c7SAlexander Motin if (!ch->hold[i]) 16176f9a51c7SAlexander Motin continue; 16186f9a51c7SAlexander Motin xpt_done(ch->hold[i]); 16196f9a51c7SAlexander Motin ch->hold[i] = NULL; 16206f9a51c7SAlexander Motin ch->numhslots--; 16216f9a51c7SAlexander Motin } 16226f9a51c7SAlexander Motin if (ch->toslots != 0) 16236f9a51c7SAlexander Motin xpt_release_simq(ch->sim, TRUE); 16246f9a51c7SAlexander Motin ch->eslots = 0; 16256f9a51c7SAlexander Motin ch->recovery = 0; 16266f9a51c7SAlexander Motin ch->toslots = 0; 16276f9a51c7SAlexander Motin ch->fatalerr = 0; 162827acd6eaSAlexander Motin /* Disable port interrupts */ 162927acd6eaSAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 163027acd6eaSAlexander Motin /* Set speed limit. */ 1631b447e682SAlexander Motin sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 1632b447e682SAlexander Motin if (sata_rev == 1) 163327acd6eaSAlexander Motin val = ATA_SC_SPD_SPEED_GEN1; 1634b447e682SAlexander Motin else if (sata_rev == 2) 163527acd6eaSAlexander Motin val = ATA_SC_SPD_SPEED_GEN2; 1636b447e682SAlexander Motin else if (sata_rev == 3) 163727acd6eaSAlexander Motin val = ATA_SC_SPD_SPEED_GEN3; 163827acd6eaSAlexander Motin else 163927acd6eaSAlexander Motin val = 0; 164027acd6eaSAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 164127acd6eaSAlexander Motin ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 164227acd6eaSAlexander Motin (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 16436f9a51c7SAlexander Motin retry: 164427acd6eaSAlexander Motin siis_devreset(dev); 164567b87e44SAlexander Motin /* Reset and reconnect PHY, */ 164627acd6eaSAlexander Motin if (!siis_sata_connect(ch)) { 164767b87e44SAlexander Motin ch->devices = 0; 164867b87e44SAlexander Motin /* Enable port interrupts */ 164967b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 165067b87e44SAlexander Motin if (bootverbose) 165167b87e44SAlexander Motin device_printf(dev, 165267b87e44SAlexander Motin "SIIS reset done: phy reset found no device\n"); 165367b87e44SAlexander Motin /* Tell the XPT about the event */ 165467b87e44SAlexander Motin xpt_async(AC_BUS_RESET, ch->path, NULL); 165583c5d981SAlexander Motin xpt_release_simq(ch->sim, TRUE); 165667b87e44SAlexander Motin return; 165767b87e44SAlexander Motin } 165854fc853aSAlexander Motin /* Wait for port ready status. */ 165954fc853aSAlexander Motin if (siis_wait_ready(dev, 1000)) { 166054fc853aSAlexander Motin device_printf(dev, "port ready timeout\n"); 16616f9a51c7SAlexander Motin if (!retry) { 16626f9a51c7SAlexander Motin device_printf(dev, "trying full port reset ...\n"); 16636f9a51c7SAlexander Motin /* Get port to the reset state. */ 16646f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 16656f9a51c7SAlexander Motin DELAY(10000); 16666f9a51c7SAlexander Motin /* Get port out of reset state. */ 16676f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 16686f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 16696f9a51c7SAlexander Motin if (ch->pm_present) 16706f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 16716f9a51c7SAlexander Motin else 16726f9a51c7SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 16736f9a51c7SAlexander Motin siis_wait_ready(dev, 5000); 16746f9a51c7SAlexander Motin retry = 1; 16756f9a51c7SAlexander Motin goto retry; 16766f9a51c7SAlexander Motin } 16776f9a51c7SAlexander Motin } 167867b87e44SAlexander Motin ch->devices = 1; 167967b87e44SAlexander Motin /* Enable port interrupts */ 168067b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 168167b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 168267b87e44SAlexander Motin if (bootverbose) 168367b87e44SAlexander Motin device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 168467b87e44SAlexander Motin /* Tell the XPT about the event */ 168567b87e44SAlexander Motin xpt_async(AC_BUS_RESET, ch->path, NULL); 168683c5d981SAlexander Motin xpt_release_simq(ch->sim, TRUE); 168767b87e44SAlexander Motin } 168867b87e44SAlexander Motin 168967b87e44SAlexander Motin static int 1690b447e682SAlexander Motin siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag) 169167b87e44SAlexander Motin { 1692b447e682SAlexander Motin struct siis_channel *ch = device_get_softc(dev); 169367b87e44SAlexander Motin u_int8_t *fis = &ctp->fis[0]; 169467b87e44SAlexander Motin 169567b87e44SAlexander Motin bzero(fis, 24); 169667b87e44SAlexander Motin fis[0] = 0x27; /* host to device */ 169767b87e44SAlexander Motin fis[1] = (ccb->ccb_h.target_id & 0x0f); 169867b87e44SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 169967b87e44SAlexander Motin fis[1] |= 0x80; 170067b87e44SAlexander Motin fis[2] = ATA_PACKET_CMD; 1701b447e682SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1702b447e682SAlexander Motin ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 170367b87e44SAlexander Motin fis[3] = ATA_F_DMA; 170467b87e44SAlexander Motin else { 170567b87e44SAlexander Motin fis[5] = ccb->csio.dxfer_len; 170667b87e44SAlexander Motin fis[6] = ccb->csio.dxfer_len >> 8; 170767b87e44SAlexander Motin } 170867b87e44SAlexander Motin fis[7] = ATA_D_LBA; 170967b87e44SAlexander Motin fis[15] = ATA_A_4BIT; 171067b87e44SAlexander Motin bzero(ctp->u.atapi.ccb, 16); 171167b87e44SAlexander Motin bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 171267b87e44SAlexander Motin ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 171367b87e44SAlexander Motin ctp->u.atapi.ccb, ccb->csio.cdb_len); 171467b87e44SAlexander Motin } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 171567b87e44SAlexander Motin fis[1] |= 0x80; 171667b87e44SAlexander Motin fis[2] = ccb->ataio.cmd.command; 171767b87e44SAlexander Motin fis[3] = ccb->ataio.cmd.features; 171867b87e44SAlexander Motin fis[4] = ccb->ataio.cmd.lba_low; 171967b87e44SAlexander Motin fis[5] = ccb->ataio.cmd.lba_mid; 172067b87e44SAlexander Motin fis[6] = ccb->ataio.cmd.lba_high; 172167b87e44SAlexander Motin fis[7] = ccb->ataio.cmd.device; 172267b87e44SAlexander Motin fis[8] = ccb->ataio.cmd.lba_low_exp; 172367b87e44SAlexander Motin fis[9] = ccb->ataio.cmd.lba_mid_exp; 172467b87e44SAlexander Motin fis[10] = ccb->ataio.cmd.lba_high_exp; 172567b87e44SAlexander Motin fis[11] = ccb->ataio.cmd.features_exp; 172667b87e44SAlexander Motin fis[12] = ccb->ataio.cmd.sector_count; 17274138a744SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 17284138a744SAlexander Motin fis[12] &= 0xf8; 17294138a744SAlexander Motin fis[12] |= tag << 3; 173067b87e44SAlexander Motin } 17314138a744SAlexander Motin fis[13] = ccb->ataio.cmd.sector_count_exp; 1732*06c888ecSAlexander Motin if (ccb->ataio.ata_flags & ATA_FLAG_ICC) 1733*06c888ecSAlexander Motin fis[14] = ccb->ataio.icc; 173467b87e44SAlexander Motin fis[15] = ATA_A_4BIT; 1735916d57dfSWarner Losh if (ccb->ataio.ata_flags & ATA_FLAG_AUX) { 1736916d57dfSWarner Losh fis[16] = ccb->ataio.aux & 0xff; 1737916d57dfSWarner Losh fis[17] = (ccb->ataio.aux >> 8) & 0xff; 1738916d57dfSWarner Losh fis[18] = (ccb->ataio.aux >> 16) & 0xff; 1739916d57dfSWarner Losh fis[19] = (ccb->ataio.aux >> 24) & 0xff; 1740916d57dfSWarner Losh } 174167b87e44SAlexander Motin } else { 174267b87e44SAlexander Motin /* Soft reset. */ 174367b87e44SAlexander Motin } 174467b87e44SAlexander Motin return (20); 174567b87e44SAlexander Motin } 174667b87e44SAlexander Motin 174767b87e44SAlexander Motin static int 174867b87e44SAlexander Motin siis_sata_connect(struct siis_channel *ch) 174967b87e44SAlexander Motin { 175067b87e44SAlexander Motin u_int32_t status; 17511f145eafSAlexander Motin int timeout, found = 0; 175267b87e44SAlexander Motin 175367b87e44SAlexander Motin /* Wait up to 100ms for "connect well" */ 17541f145eafSAlexander Motin for (timeout = 0; timeout < 1000 ; timeout++) { 175567b87e44SAlexander Motin status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 17561f145eafSAlexander Motin if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE) 17571f145eafSAlexander Motin found = 1; 175867b87e44SAlexander Motin if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 175967b87e44SAlexander Motin ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 176067b87e44SAlexander Motin ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 176167b87e44SAlexander Motin break; 17621f145eafSAlexander Motin if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 176367b87e44SAlexander Motin if (bootverbose) { 17641f145eafSAlexander Motin device_printf(ch->dev, "SATA offline status=%08x\n", 176567b87e44SAlexander Motin status); 176667b87e44SAlexander Motin } 176767b87e44SAlexander Motin return (0); 176867b87e44SAlexander Motin } 17691f145eafSAlexander Motin if (found == 0 && timeout >= 100) 17701f145eafSAlexander Motin break; 17711f145eafSAlexander Motin DELAY(100); 17721f145eafSAlexander Motin } 17731f145eafSAlexander Motin if (timeout >= 1000 || !found) { 177467b87e44SAlexander Motin if (bootverbose) { 17751f145eafSAlexander Motin device_printf(ch->dev, 17761f145eafSAlexander Motin "SATA connect timeout time=%dus status=%08x\n", 17771f145eafSAlexander Motin timeout * 100, status); 17781f145eafSAlexander Motin } 17791f145eafSAlexander Motin return (0); 17801f145eafSAlexander Motin } 17811f145eafSAlexander Motin if (bootverbose) { 17821f145eafSAlexander Motin device_printf(ch->dev, "SATA connect time=%dus status=%08x\n", 17831f145eafSAlexander Motin timeout * 100, status); 178467b87e44SAlexander Motin } 178567b87e44SAlexander Motin /* Clear SATA error register */ 178667b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 178767b87e44SAlexander Motin return (1); 178867b87e44SAlexander Motin } 178967b87e44SAlexander Motin 179081a75ae3SAlexander Motin static int 179181a75ae3SAlexander Motin siis_check_ids(device_t dev, union ccb *ccb) 179281a75ae3SAlexander Motin { 179381a75ae3SAlexander Motin 179481a75ae3SAlexander Motin if (ccb->ccb_h.target_id > 15) { 179581a75ae3SAlexander Motin ccb->ccb_h.status = CAM_TID_INVALID; 179681a75ae3SAlexander Motin xpt_done(ccb); 179781a75ae3SAlexander Motin return (-1); 179881a75ae3SAlexander Motin } 179981a75ae3SAlexander Motin if (ccb->ccb_h.target_lun != 0) { 180081a75ae3SAlexander Motin ccb->ccb_h.status = CAM_LUN_INVALID; 180181a75ae3SAlexander Motin xpt_done(ccb); 180281a75ae3SAlexander Motin return (-1); 180381a75ae3SAlexander Motin } 180481a75ae3SAlexander Motin return (0); 180581a75ae3SAlexander Motin } 180681a75ae3SAlexander Motin 180767b87e44SAlexander Motin static void 180867b87e44SAlexander Motin siisaction(struct cam_sim *sim, union ccb *ccb) 180967b87e44SAlexander Motin { 18108edcf694SAlexander Motin device_t dev, parent; 181167b87e44SAlexander Motin struct siis_channel *ch; 181267b87e44SAlexander Motin 181367b87e44SAlexander Motin CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 181467b87e44SAlexander Motin ccb->ccb_h.func_code)); 181567b87e44SAlexander Motin 181667b87e44SAlexander Motin ch = (struct siis_channel *)cam_sim_softc(sim); 181767b87e44SAlexander Motin dev = ch->dev; 181867b87e44SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 181967b87e44SAlexander Motin switch (ccb->ccb_h.func_code) { 182067b87e44SAlexander Motin /* Common cases first */ 182167b87e44SAlexander Motin case XPT_ATA_IO: /* Execute the requested I/O operation */ 182267b87e44SAlexander Motin case XPT_SCSI_IO: 182381a75ae3SAlexander Motin if (siis_check_ids(dev, ccb)) 182481a75ae3SAlexander Motin return; 182581a75ae3SAlexander Motin if (ch->devices == 0 || 182681a75ae3SAlexander Motin (ch->pm_present == 0 && 182781a75ae3SAlexander Motin ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 182867b87e44SAlexander Motin ccb->ccb_h.status = CAM_SEL_TIMEOUT; 182967b87e44SAlexander Motin break; 183067b87e44SAlexander Motin } 1831b8b7a902SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_NONE; 183267b87e44SAlexander Motin /* Check for command collision. */ 183367b87e44SAlexander Motin if (siis_check_collision(dev, ccb)) { 183467b87e44SAlexander Motin /* Freeze command. */ 183567b87e44SAlexander Motin ch->frozen = ccb; 183667b87e44SAlexander Motin /* We have only one frozen slot, so freeze simq also. */ 183767b87e44SAlexander Motin xpt_freeze_simq(ch->sim, 1); 183867b87e44SAlexander Motin return; 183967b87e44SAlexander Motin } 184067b87e44SAlexander Motin siis_begin_transaction(dev, ccb); 184181a75ae3SAlexander Motin return; 184267b87e44SAlexander Motin case XPT_ABORT: /* Abort the specified CCB */ 184367b87e44SAlexander Motin /* XXX Implement */ 184467b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 184567b87e44SAlexander Motin break; 184667b87e44SAlexander Motin case XPT_SET_TRAN_SETTINGS: 184767b87e44SAlexander Motin { 184867b87e44SAlexander Motin struct ccb_trans_settings *cts = &ccb->cts; 1849c8039fc6SAlexander Motin struct siis_device *d; 185067b87e44SAlexander Motin 185181a75ae3SAlexander Motin if (siis_check_ids(dev, ccb)) 185281a75ae3SAlexander Motin return; 1853c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1854c8039fc6SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id]; 1855c8039fc6SAlexander Motin else 1856c8039fc6SAlexander Motin d = &ch->user[ccb->ccb_h.target_id]; 1857c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 1858c8039fc6SAlexander Motin d->revision = cts->xport_specific.sata.revision; 1859c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 1860c8039fc6SAlexander Motin d->mode = cts->xport_specific.sata.mode; 1861c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 1862c8039fc6SAlexander Motin d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 1863c8039fc6SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1864c8039fc6SAlexander Motin d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags); 186567b87e44SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 18666f9a51c7SAlexander Motin ch->pm_present = cts->xport_specific.sata.pm_present; 18676f9a51c7SAlexander Motin if (ch->pm_present) 186867b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 186967b87e44SAlexander Motin else 187067b87e44SAlexander Motin ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 187167b87e44SAlexander Motin } 18724cca1530SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 18734cca1530SAlexander Motin d->atapi = cts->xport_specific.sata.atapi; 1874da6808c1SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1875da6808c1SAlexander Motin d->caps = cts->xport_specific.sata.caps; 187667b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 187767b87e44SAlexander Motin break; 187867b87e44SAlexander Motin } 187967b87e44SAlexander Motin case XPT_GET_TRAN_SETTINGS: 188067b87e44SAlexander Motin /* Get default/user set transfer settings for the target */ 188167b87e44SAlexander Motin { 188267b87e44SAlexander Motin struct ccb_trans_settings *cts = &ccb->cts; 1883c8039fc6SAlexander Motin struct siis_device *d; 188467b87e44SAlexander Motin uint32_t status; 188567b87e44SAlexander Motin 188681a75ae3SAlexander Motin if (siis_check_ids(dev, ccb)) 188781a75ae3SAlexander Motin return; 1888c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1889c8039fc6SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id]; 1890c8039fc6SAlexander Motin else 1891c8039fc6SAlexander Motin d = &ch->user[ccb->ccb_h.target_id]; 1892bc1bf6e8SAlexander Motin cts->protocol = PROTO_UNSPECIFIED; 189335713642SAlexander Motin cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 189467b87e44SAlexander Motin cts->transport = XPORT_SATA; 189535713642SAlexander Motin cts->transport_version = XPORT_VERSION_UNSPECIFIED; 189667b87e44SAlexander Motin cts->proto_specific.valid = 0; 189767b87e44SAlexander Motin cts->xport_specific.sata.valid = 0; 1898c8039fc6SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1899c8039fc6SAlexander Motin (ccb->ccb_h.target_id == 15 || 1900c8039fc6SAlexander Motin (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 190167b87e44SAlexander Motin status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1902c8039fc6SAlexander Motin if (status & 0x0f0) { 1903c8039fc6SAlexander Motin cts->xport_specific.sata.revision = 1904c8039fc6SAlexander Motin (status & 0x0f0) >> 4; 1905c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= 1906c8039fc6SAlexander Motin CTS_SATA_VALID_REVISION; 190767b87e44SAlexander Motin } 1908da6808c1SAlexander Motin cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 1909da6808c1SAlexander Motin if (ch->pm_level) 1910da6808c1SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 19118d169381SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN; 1912da6808c1SAlexander Motin cts->xport_specific.sata.caps &= 1913da6808c1SAlexander Motin ch->user[ccb->ccb_h.target_id].caps; 1914da6808c1SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1915c8039fc6SAlexander Motin } else { 1916c8039fc6SAlexander Motin cts->xport_specific.sata.revision = d->revision; 1917c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 1918da6808c1SAlexander Motin cts->xport_specific.sata.caps = d->caps; 19198d169381SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 19208d169381SAlexander Motin (ch->quirks & SIIS_Q_SNTF) == 0) 19218d169381SAlexander Motin cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN; 1922da6808c1SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1923c8039fc6SAlexander Motin } 1924c8039fc6SAlexander Motin cts->xport_specific.sata.mode = d->mode; 1925c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 1926c8039fc6SAlexander Motin cts->xport_specific.sata.bytecount = d->bytecount; 1927c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 19286f9a51c7SAlexander Motin cts->xport_specific.sata.pm_present = ch->pm_present; 192967b87e44SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1930c8039fc6SAlexander Motin cts->xport_specific.sata.tags = d->tags; 1931c8039fc6SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 19324cca1530SAlexander Motin cts->xport_specific.sata.atapi = d->atapi; 19334cca1530SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 193467b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 193567b87e44SAlexander Motin break; 193667b87e44SAlexander Motin } 193767b87e44SAlexander Motin case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 193867b87e44SAlexander Motin case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 193967b87e44SAlexander Motin siis_reset(dev); 194067b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 194167b87e44SAlexander Motin break; 194267b87e44SAlexander Motin case XPT_TERM_IO: /* Terminate the I/O process */ 194367b87e44SAlexander Motin /* XXX Implement */ 194467b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 194567b87e44SAlexander Motin break; 194667b87e44SAlexander Motin case XPT_PATH_INQ: /* Path routing inquiry */ 194767b87e44SAlexander Motin { 194867b87e44SAlexander Motin struct ccb_pathinq *cpi = &ccb->cpi; 194967b87e44SAlexander Motin 19508edcf694SAlexander Motin parent = device_get_parent(dev); 195167b87e44SAlexander Motin cpi->version_num = 1; /* XXX??? */ 195267b87e44SAlexander Motin cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 195367b87e44SAlexander Motin cpi->hba_inquiry |= PI_SATAPM; 195467b87e44SAlexander Motin cpi->target_sprt = 0; 1955916d57dfSWarner Losh cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED | PIM_ATA_EXT; 195667b87e44SAlexander Motin cpi->hba_eng_cnt = 0; 19578e7cccb3SAlexander Motin cpi->max_target = 15; 195867b87e44SAlexander Motin cpi->max_lun = 0; 195967b87e44SAlexander Motin cpi->initiator_id = 0; 196067b87e44SAlexander Motin cpi->bus_id = cam_sim_bus(sim); 196167b87e44SAlexander Motin cpi->base_transfer_speed = 150000; 19624195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 19634195c7deSAlan Somers strlcpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 19644195c7deSAlan Somers strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 196567b87e44SAlexander Motin cpi->unit_number = cam_sim_unit(sim); 196667b87e44SAlexander Motin cpi->transport = XPORT_SATA; 196735713642SAlexander Motin cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1968eb586bd9SAlexander Motin cpi->protocol = PROTO_ATA; 196935713642SAlexander Motin cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 197067b87e44SAlexander Motin cpi->maxio = MAXPHYS; 19718edcf694SAlexander Motin cpi->hba_vendor = pci_get_vendor(parent); 19728edcf694SAlexander Motin cpi->hba_device = pci_get_device(parent); 19738edcf694SAlexander Motin cpi->hba_subvendor = pci_get_subvendor(parent); 19748edcf694SAlexander Motin cpi->hba_subdevice = pci_get_subdevice(parent); 19758edcf694SAlexander Motin cpi->ccb_h.status = CAM_REQ_CMP; 197667b87e44SAlexander Motin break; 197767b87e44SAlexander Motin } 197867b87e44SAlexander Motin default: 197967b87e44SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 198067b87e44SAlexander Motin break; 198167b87e44SAlexander Motin } 198281a75ae3SAlexander Motin xpt_done(ccb); 198367b87e44SAlexander Motin } 198467b87e44SAlexander Motin 198567b87e44SAlexander Motin static void 198667b87e44SAlexander Motin siispoll(struct cam_sim *sim) 198767b87e44SAlexander Motin { 198867b87e44SAlexander Motin struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 198967b87e44SAlexander Motin 199067b87e44SAlexander Motin siis_ch_intr(ch->dev); 199167b87e44SAlexander Motin } 1992