siis.c revision 199333
1195801Smav/*- 2195801Smav * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3195801Smav * All rights reserved. 4195801Smav * 5195801Smav * Redistribution and use in source and binary forms, with or without 6195801Smav * modification, are permitted provided that the following conditions 7195801Smav * are met: 8195801Smav * 1. Redistributions of source code must retain the above copyright 9195801Smav * notice, this list of conditions and the following disclaimer, 10195801Smav * without modification, immediately at the beginning of the file. 11195801Smav * 2. Redistributions in binary form must reproduce the above copyright 12195801Smav * notice, this list of conditions and the following disclaimer in the 13195801Smav * documentation and/or other materials provided with the distribution. 14195801Smav * 15195801Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16195801Smav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17195801Smav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18195801Smav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19195801Smav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20195801Smav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21195801Smav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22195801Smav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23195801Smav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24195801Smav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25195801Smav */ 26195801Smav 27195801Smav#include <sys/cdefs.h> 28195801Smav__FBSDID("$FreeBSD: head/sys/dev/siis/siis.c 199333 2009-11-16 20:54:47Z mav $"); 29195801Smav 30195801Smav#include <sys/param.h> 31195801Smav#include <sys/module.h> 32195801Smav#include <sys/systm.h> 33195801Smav#include <sys/kernel.h> 34195801Smav#include <sys/ata.h> 35195801Smav#include <sys/bus.h> 36195801Smav#include <sys/endian.h> 37195801Smav#include <sys/malloc.h> 38195801Smav#include <sys/lock.h> 39195801Smav#include <sys/mutex.h> 40195801Smav#include <sys/sema.h> 41195801Smav#include <sys/taskqueue.h> 42195801Smav#include <vm/uma.h> 43195801Smav#include <machine/stdarg.h> 44195801Smav#include <machine/resource.h> 45195801Smav#include <machine/bus.h> 46195801Smav#include <sys/rman.h> 47195801Smav#include <dev/pci/pcivar.h> 48195801Smav#include <dev/pci/pcireg.h> 49195801Smav#include "siis.h" 50195801Smav 51195801Smav#include <cam/cam.h> 52195801Smav#include <cam/cam_ccb.h> 53195801Smav#include <cam/cam_sim.h> 54195801Smav#include <cam/cam_xpt_sim.h> 55195801Smav#include <cam/cam_xpt_periph.h> 56195801Smav#include <cam/cam_debug.h> 57195801Smav 58195801Smav/* local prototypes */ 59195801Smavstatic int siis_setup_interrupt(device_t dev); 60195801Smavstatic void siis_intr(void *data); 61195801Smavstatic int siis_suspend(device_t dev); 62195801Smavstatic int siis_resume(device_t dev); 63195801Smavstatic int siis_ch_suspend(device_t dev); 64195801Smavstatic int siis_ch_resume(device_t dev); 65195801Smavstatic void siis_ch_intr_locked(void *data); 66195801Smavstatic void siis_ch_intr(void *data); 67195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb); 68195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 69195801Smavstatic void siis_execute_transaction(struct siis_slot *slot); 70195801Smavstatic void siis_timeout(struct siis_slot *slot); 71195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et); 72195801Smavstatic int siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag); 73195801Smavstatic void siis_dmainit(device_t dev); 74195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 75195801Smavstatic void siis_dmafini(device_t dev); 76195801Smavstatic void siis_slotsalloc(device_t dev); 77195801Smavstatic void siis_slotsfree(device_t dev); 78195801Smavstatic void siis_reset(device_t dev); 79195801Smavstatic void siis_portinit(device_t dev); 80195801Smavstatic int siis_wait_ready(device_t dev, int t); 81195801Smav 82195801Smavstatic int siis_sata_connect(struct siis_channel *ch); 83195801Smav 84195801Smavstatic void siis_issue_read_log(device_t dev); 85195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb); 86195801Smav 87195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb); 88195801Smavstatic void siispoll(struct cam_sim *sim); 89195801Smav 90195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers"); 91195801Smav 92199132Smavstatic struct { 93199132Smav uint32_t id; 94199132Smav const char *name; 95199132Smav int ports; 96199132Smav} siis_ids[] = { 97199132Smav {0x31241095, "SiI3124", 4}, 98199132Smav {0x31248086, "SiI3124", 4}, 99199132Smav {0x31321095, "SiI3132", 2}, 100199132Smav {0x02421095, "SiI3132", 2}, 101199132Smav {0x02441095, "SiI3132", 2}, 102199132Smav {0x31311095, "SiI3131", 1}, 103199132Smav {0x35311095, "SiI3531", 1}, 104199132Smav {0, NULL, 0} 105199132Smav}; 106199132Smav 107195801Smavstatic int 108195801Smavsiis_probe(device_t dev) 109195801Smav{ 110199132Smav char buf[64]; 111199132Smav int i; 112195801Smav uint32_t devid = pci_get_devid(dev); 113195801Smav 114199132Smav for (i = 0; siis_ids[i].id != 0; i++) { 115199132Smav if (siis_ids[i].id == devid) { 116199132Smav snprintf(buf, sizeof(buf), "%s SATA2 controller", 117199132Smav siis_ids[i].name); 118199132Smav device_set_desc_copy(dev, buf); 119199132Smav return (BUS_PROBE_VENDOR); 120199132Smav } 121195801Smav } 122199132Smav return (ENXIO); 123195801Smav} 124195801Smav 125195801Smavstatic int 126195801Smavsiis_attach(device_t dev) 127195801Smav{ 128195801Smav struct siis_controller *ctlr = device_get_softc(dev); 129195801Smav uint32_t devid = pci_get_devid(dev); 130195801Smav device_t child; 131199132Smav int error, i, unit; 132195801Smav 133199132Smav for (i = 0; siis_ids[i].id != 0; i++) { 134199132Smav if (siis_ids[i].id == devid) 135199132Smav break; 136199132Smav } 137195801Smav ctlr->dev = dev; 138195801Smav /* Global memory */ 139195801Smav ctlr->r_grid = PCIR_BAR(0); 140195801Smav if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 141195801Smav &ctlr->r_grid, RF_ACTIVE))) 142195801Smav return (ENXIO); 143195801Smav /* Channels memory */ 144195801Smav ctlr->r_rid = PCIR_BAR(2); 145195801Smav if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 146195801Smav &ctlr->r_rid, RF_ACTIVE))) 147195801Smav return (ENXIO); 148195801Smav /* Setup our own memory management for channels. */ 149195801Smav ctlr->sc_iomem.rm_type = RMAN_ARRAY; 150195801Smav ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 151195801Smav if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 152195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 153195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 154195801Smav return (error); 155195801Smav } 156195801Smav if ((error = rman_manage_region(&ctlr->sc_iomem, 157195801Smav rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 158195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 159195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 160195801Smav rman_fini(&ctlr->sc_iomem); 161195801Smav return (error); 162195801Smav } 163195801Smav /* Reset controller */ 164195801Smav siis_resume(dev); 165195801Smav /* Number of HW channels */ 166199132Smav ctlr->channels = siis_ids[i].ports; 167195801Smav /* Setup interrupts. */ 168195801Smav if (siis_setup_interrupt(dev)) { 169195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 170195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 171195801Smav rman_fini(&ctlr->sc_iomem); 172195801Smav return ENXIO; 173195801Smav } 174195801Smav /* Attach all channels on this controller */ 175195801Smav for (unit = 0; unit < ctlr->channels; unit++) { 176195801Smav child = device_add_child(dev, "siisch", -1); 177195801Smav if (child == NULL) 178195801Smav device_printf(dev, "failed to add channel device\n"); 179195801Smav else 180195801Smav device_set_ivars(child, (void *)(intptr_t)unit); 181195801Smav } 182195801Smav bus_generic_attach(dev); 183195801Smav return 0; 184195801Smav} 185195801Smav 186195801Smavstatic int 187195801Smavsiis_detach(device_t dev) 188195801Smav{ 189195801Smav struct siis_controller *ctlr = device_get_softc(dev); 190195801Smav device_t *children; 191195801Smav int nchildren, i; 192195801Smav 193195801Smav /* Detach & delete all children */ 194195801Smav if (!device_get_children(dev, &children, &nchildren)) { 195195801Smav for (i = 0; i < nchildren; i++) 196195801Smav device_delete_child(dev, children[i]); 197195801Smav free(children, M_TEMP); 198195801Smav } 199195801Smav /* Free interrupts. */ 200195801Smav if (ctlr->irq.r_irq) { 201195801Smav bus_teardown_intr(dev, ctlr->irq.r_irq, 202195801Smav ctlr->irq.handle); 203195801Smav bus_release_resource(dev, SYS_RES_IRQ, 204195801Smav ctlr->irq.r_irq_rid, ctlr->irq.r_irq); 205195801Smav } 206195801Smav pci_release_msi(dev); 207195801Smav /* Free memory. */ 208195801Smav rman_fini(&ctlr->sc_iomem); 209195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 210195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 211195801Smav return (0); 212195801Smav} 213195801Smav 214195801Smavstatic int 215195801Smavsiis_suspend(device_t dev) 216195801Smav{ 217195801Smav struct siis_controller *ctlr = device_get_softc(dev); 218195801Smav 219195801Smav bus_generic_suspend(dev); 220195801Smav /* Put controller into reset state. */ 221195801Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET); 222195801Smav return 0; 223195801Smav} 224195801Smav 225195801Smavstatic int 226195801Smavsiis_resume(device_t dev) 227195801Smav{ 228195801Smav struct siis_controller *ctlr = device_get_softc(dev); 229195801Smav 230195801Smav /* Put controller into reset state. */ 231195801Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET); 232195801Smav DELAY(10000); 233195801Smav /* Get controller out of reset state and enable port interrupts. */ 234195801Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 0x0000000f); 235195801Smav return (bus_generic_resume(dev)); 236195801Smav} 237195801Smav 238195801Smavstatic int 239195801Smavsiis_setup_interrupt(device_t dev) 240195801Smav{ 241195801Smav struct siis_controller *ctlr = device_get_softc(dev); 242195801Smav int msi = 0; 243195801Smav 244195801Smav /* Process hints. */ 245195801Smav resource_int_value(device_get_name(dev), 246195801Smav device_get_unit(dev), "msi", &msi); 247195801Smav if (msi < 0) 248195801Smav msi = 0; 249195801Smav else if (msi > 0) 250195801Smav msi = min(1, pci_msi_count(dev)); 251195801Smav /* Allocate MSI if needed/present. */ 252195801Smav if (msi && pci_alloc_msi(dev, &msi) != 0) 253195801Smav msi = 0; 254195801Smav /* Allocate all IRQs. */ 255195801Smav ctlr->irq.r_irq_rid = msi ? 1 : 0; 256195801Smav if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 257195801Smav &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 258195801Smav device_printf(dev, "unable to map interrupt\n"); 259195801Smav return ENXIO; 260195801Smav } 261195801Smav if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL, 262195801Smav siis_intr, ctlr, &ctlr->irq.handle))) { 263195801Smav /* SOS XXX release r_irq */ 264195801Smav device_printf(dev, "unable to setup interrupt\n"); 265195801Smav return ENXIO; 266195801Smav } 267195801Smav return (0); 268195801Smav} 269195801Smav 270195801Smav/* 271195801Smav * Common case interrupt handler. 272195801Smav */ 273195801Smavstatic void 274195801Smavsiis_intr(void *data) 275195801Smav{ 276195801Smav struct siis_controller *ctlr = (struct siis_controller *)data; 277195801Smav u_int32_t is; 278195801Smav void *arg; 279195801Smav int unit; 280195801Smav 281195801Smav is = ATA_INL(ctlr->r_gmem, SIIS_IS); 282195801Smav for (unit = 0; unit < ctlr->channels; unit++) { 283195801Smav if ((is & SIIS_IS_PORT(unit)) != 0 && 284195801Smav (arg = ctlr->interrupt[unit].argument)) { 285195801Smav ctlr->interrupt[unit].function(arg); 286195801Smav } 287195801Smav } 288195801Smav} 289195801Smav 290195801Smavstatic struct resource * 291195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid, 292195801Smav u_long start, u_long end, u_long count, u_int flags) 293195801Smav{ 294195801Smav struct siis_controller *ctlr = device_get_softc(dev); 295195801Smav int unit = ((struct siis_channel *)device_get_softc(child))->unit; 296195801Smav struct resource *res = NULL; 297195801Smav int offset = unit << 13; 298195801Smav long st; 299195801Smav 300195801Smav switch (type) { 301195801Smav case SYS_RES_MEMORY: 302195801Smav st = rman_get_start(ctlr->r_mem); 303195801Smav res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 304195801Smav st + offset + 0x2000, 0x2000, RF_ACTIVE, child); 305195801Smav if (res) { 306195801Smav bus_space_handle_t bsh; 307195801Smav bus_space_tag_t bst; 308195801Smav bsh = rman_get_bushandle(ctlr->r_mem); 309195801Smav bst = rman_get_bustag(ctlr->r_mem); 310195801Smav bus_space_subregion(bst, bsh, offset, 0x2000, &bsh); 311195801Smav rman_set_bushandle(res, bsh); 312195801Smav rman_set_bustag(res, bst); 313195801Smav } 314195801Smav break; 315195801Smav case SYS_RES_IRQ: 316195801Smav if (*rid == ATA_IRQ_RID) 317195801Smav res = ctlr->irq.r_irq; 318195801Smav break; 319195801Smav } 320195801Smav return (res); 321195801Smav} 322195801Smav 323195801Smavstatic int 324195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid, 325195801Smav struct resource *r) 326195801Smav{ 327195801Smav 328195801Smav switch (type) { 329195801Smav case SYS_RES_MEMORY: 330195801Smav rman_release_resource(r); 331195801Smav return (0); 332195801Smav case SYS_RES_IRQ: 333195801Smav if (rid != ATA_IRQ_RID) 334195801Smav return ENOENT; 335195801Smav return (0); 336195801Smav } 337195801Smav return (EINVAL); 338195801Smav} 339195801Smav 340195801Smavstatic int 341195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq, 342195801Smav int flags, driver_filter_t *filter, driver_intr_t *function, 343195801Smav void *argument, void **cookiep) 344195801Smav{ 345195801Smav struct siis_controller *ctlr = device_get_softc(dev); 346195801Smav int unit = (intptr_t)device_get_ivars(child); 347195801Smav 348195801Smav if (filter != NULL) { 349195801Smav printf("siis.c: we cannot use a filter here\n"); 350195801Smav return (EINVAL); 351195801Smav } 352195801Smav ctlr->interrupt[unit].function = function; 353195801Smav ctlr->interrupt[unit].argument = argument; 354195801Smav return (0); 355195801Smav} 356195801Smav 357195801Smavstatic int 358195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq, 359195801Smav void *cookie) 360195801Smav{ 361195801Smav struct siis_controller *ctlr = device_get_softc(dev); 362195801Smav int unit = (intptr_t)device_get_ivars(child); 363195801Smav 364195801Smav ctlr->interrupt[unit].function = NULL; 365195801Smav ctlr->interrupt[unit].argument = NULL; 366195801Smav return (0); 367195801Smav} 368195801Smav 369195801Smavstatic int 370195801Smavsiis_print_child(device_t dev, device_t child) 371195801Smav{ 372195801Smav int retval; 373195801Smav 374195801Smav retval = bus_print_child_header(dev, child); 375195801Smav retval += printf(" at channel %d", 376195801Smav (int)(intptr_t)device_get_ivars(child)); 377195801Smav retval += bus_print_child_footer(dev, child); 378195801Smav 379195801Smav return (retval); 380195801Smav} 381195801Smav 382195801Smavdevclass_t siis_devclass; 383195801Smavstatic device_method_t siis_methods[] = { 384195801Smav DEVMETHOD(device_probe, siis_probe), 385195801Smav DEVMETHOD(device_attach, siis_attach), 386195801Smav DEVMETHOD(device_detach, siis_detach), 387195801Smav DEVMETHOD(device_suspend, siis_suspend), 388195801Smav DEVMETHOD(device_resume, siis_resume), 389195801Smav DEVMETHOD(bus_print_child, siis_print_child), 390195801Smav DEVMETHOD(bus_alloc_resource, siis_alloc_resource), 391195801Smav DEVMETHOD(bus_release_resource, siis_release_resource), 392195801Smav DEVMETHOD(bus_setup_intr, siis_setup_intr), 393195801Smav DEVMETHOD(bus_teardown_intr,siis_teardown_intr), 394195801Smav { 0, 0 } 395195801Smav}; 396195801Smavstatic driver_t siis_driver = { 397195801Smav "siis", 398195801Smav siis_methods, 399195801Smav sizeof(struct siis_controller) 400195801Smav}; 401195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0); 402195801SmavMODULE_VERSION(siis, 1); 403195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1); 404195801Smav 405195801Smavstatic int 406195801Smavsiis_ch_probe(device_t dev) 407195801Smav{ 408195801Smav 409195801Smav device_set_desc_copy(dev, "SIIS channel"); 410195801Smav return (0); 411195801Smav} 412195801Smav 413195801Smavstatic int 414195801Smavsiis_ch_attach(device_t dev) 415195801Smav{ 416195801Smav struct siis_channel *ch = device_get_softc(dev); 417195801Smav struct cam_devq *devq; 418195801Smav int rid, error; 419195801Smav 420195801Smav ch->dev = dev; 421195801Smav ch->unit = (intptr_t)device_get_ivars(dev); 422195801Smav resource_int_value(device_get_name(dev), 423195801Smav device_get_unit(dev), "pm_level", &ch->pm_level); 424195801Smav resource_int_value(device_get_name(dev), 425195801Smav device_get_unit(dev), "sata_rev", &ch->sata_rev); 426195801Smav mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 427195801Smav rid = ch->unit; 428195801Smav if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 429195801Smav &rid, RF_ACTIVE))) 430195801Smav return (ENXIO); 431195801Smav siis_dmainit(dev); 432195801Smav siis_slotsalloc(dev); 433195801Smav siis_ch_resume(dev); 434195801Smav mtx_lock(&ch->mtx); 435195801Smav rid = ATA_IRQ_RID; 436195801Smav if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 437195801Smav &rid, RF_SHAREABLE | RF_ACTIVE))) { 438195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 439195801Smav device_printf(dev, "Unable to map interrupt\n"); 440195801Smav return (ENXIO); 441195801Smav } 442195801Smav if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 443195801Smav siis_ch_intr_locked, dev, &ch->ih))) { 444195801Smav device_printf(dev, "Unable to setup interrupt\n"); 445195801Smav error = ENXIO; 446195801Smav goto err1; 447195801Smav } 448195801Smav /* Create the device queue for our SIM. */ 449195801Smav devq = cam_simq_alloc(SIIS_MAX_SLOTS); 450195801Smav if (devq == NULL) { 451195801Smav device_printf(dev, "Unable to allocate simq\n"); 452195801Smav error = ENOMEM; 453195801Smav goto err1; 454195801Smav } 455195801Smav /* Construct SIM entry */ 456195801Smav ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 457199178Smav device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq); 458195801Smav if (ch->sim == NULL) { 459195801Smav device_printf(dev, "unable to allocate sim\n"); 460195801Smav error = ENOMEM; 461195801Smav goto err2; 462195801Smav } 463195801Smav if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 464195801Smav device_printf(dev, "unable to register xpt bus\n"); 465195801Smav error = ENXIO; 466195801Smav goto err2; 467195801Smav } 468195801Smav if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 469195801Smav CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 470195801Smav device_printf(dev, "unable to create path\n"); 471195801Smav error = ENXIO; 472195801Smav goto err3; 473195801Smav } 474195801Smav mtx_unlock(&ch->mtx); 475195801Smav return (0); 476195801Smav 477195801Smaverr3: 478195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 479195801Smaverr2: 480195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 481195801Smaverr1: 482195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 483195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 484195801Smav mtx_unlock(&ch->mtx); 485195801Smav return (error); 486195801Smav} 487195801Smav 488195801Smavstatic int 489195801Smavsiis_ch_detach(device_t dev) 490195801Smav{ 491195801Smav struct siis_channel *ch = device_get_softc(dev); 492195801Smav 493195801Smav mtx_lock(&ch->mtx); 494195801Smav xpt_async(AC_LOST_DEVICE, ch->path, NULL); 495195801Smav xpt_free_path(ch->path); 496195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 497195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 498195801Smav mtx_unlock(&ch->mtx); 499195801Smav 500195801Smav bus_teardown_intr(dev, ch->r_irq, ch->ih); 501195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 502195801Smav 503195801Smav siis_ch_suspend(dev); 504195801Smav siis_slotsfree(dev); 505195801Smav siis_dmafini(dev); 506195801Smav 507195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 508195801Smav mtx_destroy(&ch->mtx); 509195801Smav return (0); 510195801Smav} 511195801Smav 512195801Smavstatic int 513195801Smavsiis_ch_suspend(device_t dev) 514195801Smav{ 515195801Smav struct siis_channel *ch = device_get_softc(dev); 516195801Smav 517195801Smav /* Put port into reset state. */ 518195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 519195801Smav return (0); 520195801Smav} 521195801Smav 522195801Smavstatic int 523195801Smavsiis_ch_resume(device_t dev) 524195801Smav{ 525195801Smav struct siis_channel *ch = device_get_softc(dev); 526195801Smav 527195801Smav /* Get port out of reset state. */ 528195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 529195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 530198852Smav if (ch->pm_present) 531198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 532198852Smav else 533198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 534195801Smav /* Enable port interrupts */ 535195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 536195801Smav return (0); 537195801Smav} 538195801Smav 539195801Smavdevclass_t siisch_devclass; 540195801Smavstatic device_method_t siisch_methods[] = { 541195801Smav DEVMETHOD(device_probe, siis_ch_probe), 542195801Smav DEVMETHOD(device_attach, siis_ch_attach), 543195801Smav DEVMETHOD(device_detach, siis_ch_detach), 544195801Smav DEVMETHOD(device_suspend, siis_ch_suspend), 545195801Smav DEVMETHOD(device_resume, siis_ch_resume), 546195801Smav { 0, 0 } 547195801Smav}; 548195801Smavstatic driver_t siisch_driver = { 549195801Smav "siisch", 550195801Smav siisch_methods, 551195801Smav sizeof(struct siis_channel) 552195801Smav}; 553195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 554195801Smav 555195801Smavstruct siis_dc_cb_args { 556195801Smav bus_addr_t maddr; 557195801Smav int error; 558195801Smav}; 559195801Smav 560195801Smavstatic void 561195801Smavsiis_dmainit(device_t dev) 562195801Smav{ 563195801Smav struct siis_channel *ch = device_get_softc(dev); 564195801Smav struct siis_dc_cb_args dcba; 565195801Smav 566195801Smav /* Command area. */ 567195801Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 568195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 569195801Smav NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 570195801Smav 0, NULL, NULL, &ch->dma.work_tag)) 571195801Smav goto error; 572195801Smav if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 573195801Smav &ch->dma.work_map)) 574195801Smav goto error; 575195801Smav if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 576195801Smav SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 577195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 578195801Smav goto error; 579195801Smav } 580195801Smav ch->dma.work_bus = dcba.maddr; 581195801Smav /* Data area. */ 582199333Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 583195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 584195801Smav NULL, NULL, 585195801Smav SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 586195801Smav SIIS_SG_ENTRIES, 0xFFFFFFFF, 587195801Smav 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 588195801Smav goto error; 589195801Smav } 590195801Smav return; 591195801Smav 592195801Smaverror: 593195801Smav device_printf(dev, "WARNING - DMA initialization failed\n"); 594195801Smav siis_dmafini(dev); 595195801Smav} 596195801Smav 597195801Smavstatic void 598195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 599195801Smav{ 600195801Smav struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 601195801Smav 602195801Smav if (!(dcba->error = error)) 603195801Smav dcba->maddr = segs[0].ds_addr; 604195801Smav} 605195801Smav 606195801Smavstatic void 607195801Smavsiis_dmafini(device_t dev) 608195801Smav{ 609195801Smav struct siis_channel *ch = device_get_softc(dev); 610195801Smav 611195801Smav if (ch->dma.data_tag) { 612195801Smav bus_dma_tag_destroy(ch->dma.data_tag); 613195801Smav ch->dma.data_tag = NULL; 614195801Smav } 615195801Smav if (ch->dma.work_bus) { 616195801Smav bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 617195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 618195801Smav ch->dma.work_bus = 0; 619195801Smav ch->dma.work_map = NULL; 620195801Smav ch->dma.work = NULL; 621195801Smav } 622195801Smav if (ch->dma.work_tag) { 623195801Smav bus_dma_tag_destroy(ch->dma.work_tag); 624195801Smav ch->dma.work_tag = NULL; 625195801Smav } 626195801Smav} 627195801Smav 628195801Smavstatic void 629195801Smavsiis_slotsalloc(device_t dev) 630195801Smav{ 631195801Smav struct siis_channel *ch = device_get_softc(dev); 632195801Smav int i; 633195801Smav 634195801Smav /* Alloc and setup command/dma slots */ 635195801Smav bzero(ch->slot, sizeof(ch->slot)); 636195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 637195801Smav struct siis_slot *slot = &ch->slot[i]; 638195801Smav 639195801Smav slot->dev = dev; 640195801Smav slot->slot = i; 641195801Smav slot->state = SIIS_SLOT_EMPTY; 642195801Smav slot->ccb = NULL; 643195801Smav callout_init_mtx(&slot->timeout, &ch->mtx, 0); 644195801Smav 645195801Smav if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 646195801Smav device_printf(ch->dev, "FAILURE - create data_map\n"); 647195801Smav } 648195801Smav} 649195801Smav 650195801Smavstatic void 651195801Smavsiis_slotsfree(device_t dev) 652195801Smav{ 653195801Smav struct siis_channel *ch = device_get_softc(dev); 654195801Smav int i; 655195801Smav 656195801Smav /* Free all dma slots */ 657195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 658195801Smav struct siis_slot *slot = &ch->slot[i]; 659195801Smav 660198896Smav callout_drain(&slot->timeout); 661195801Smav if (slot->dma.data_map) { 662195801Smav bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 663195801Smav slot->dma.data_map = NULL; 664195801Smav } 665195801Smav } 666195801Smav} 667195801Smav 668195801Smavstatic void 669196655Smavsiis_notify_events(device_t dev) 670196655Smav{ 671196655Smav struct siis_channel *ch = device_get_softc(dev); 672196655Smav struct cam_path *dpath; 673196655Smav u_int32_t status; 674196655Smav int i; 675196655Smav 676196655Smav status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 677196655Smav ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 678196655Smav if (bootverbose) 679196655Smav device_printf(dev, "SNTF 0x%04x\n", status); 680196655Smav for (i = 0; i < 16; i++) { 681196655Smav if ((status & (1 << i)) == 0) 682196655Smav continue; 683196655Smav if (xpt_create_path(&dpath, NULL, 684196655Smav xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 685196655Smav xpt_async(AC_SCSI_AEN, dpath, NULL); 686196655Smav xpt_free_path(dpath); 687196655Smav } 688196655Smav } 689196655Smav 690196655Smav} 691196655Smav 692196655Smavstatic void 693195801Smavsiis_phy_check_events(device_t dev) 694195801Smav{ 695195801Smav struct siis_channel *ch = device_get_softc(dev); 696195801Smav 697195801Smav /* If we have a connection event, deal with it */ 698195801Smav if (ch->pm_level == 0) { 699195801Smav u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 700195801Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 701195801Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 702195801Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 703195801Smav if (bootverbose) 704195801Smav device_printf(dev, "CONNECT requested\n"); 705195801Smav siis_reset(dev); 706195801Smav } else { 707195801Smav if (bootverbose) 708195801Smav device_printf(dev, "DISCONNECT requested\n"); 709195801Smav ch->devices = 0; 710195801Smav } 711195801Smav } 712195801Smav} 713195801Smav 714195801Smavstatic void 715195801Smavsiis_ch_intr_locked(void *data) 716195801Smav{ 717195801Smav device_t dev = (device_t)data; 718195801Smav struct siis_channel *ch = device_get_softc(dev); 719195801Smav 720195801Smav mtx_lock(&ch->mtx); 721195801Smav siis_ch_intr(data); 722195801Smav mtx_unlock(&ch->mtx); 723195801Smav} 724195801Smav 725195801Smavstatic void 726195801Smavsiis_ch_intr(void *data) 727195801Smav{ 728195801Smav device_t dev = (device_t)data; 729195801Smav struct siis_channel *ch = device_get_softc(dev); 730195801Smav uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 731195801Smav enum siis_err_type et; 732195801Smav int i, ccs, port, tslots; 733195801Smav 734195801Smav mtx_assert(&ch->mtx, MA_OWNED); 735195801Smav /* Read command statuses. */ 736195801Smav sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 737195801Smav ok = ch->rslots & ~sstatus; 738195801Smav /* Complete all successfull commands. */ 739195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 740195801Smav if ((ok >> i) & 1) 741195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 742195801Smav } 743195801Smav /* Do we have any other events? */ 744195801Smav if ((sstatus & SIIS_P_SS_ATTN) == 0) 745195801Smav return; 746195801Smav /* Read and clear interrupt statuses. */ 747195801Smav istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 748195801Smav (0xFFFF & ~SIIS_P_IX_COMMCOMP); 749195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 750195801Smav /* Process PHY events */ 751195801Smav if (istatus & SIIS_P_IX_PHYRDYCHG) 752195801Smav siis_phy_check_events(dev); 753196655Smav /* Process NOTIFY events */ 754196655Smav if (istatus & SIIS_P_IX_SDBN) 755196655Smav siis_notify_events(dev); 756195801Smav /* Process command errors */ 757195801Smav if (istatus & SIIS_P_IX_COMMERR) { 758195801Smav estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 759195801Smav ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 760195801Smav ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 761195801Smav port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 762195801Smav err = ch->rslots & sstatus; 763195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 764195801Smav// __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 765195801Smav// ATA_INL(ch->r_mem, SIIS_P_SERR)); 766195801Smav 767195801Smav if (!ch->readlog && !ch->recovery) { 768195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 769195801Smav ch->recovery = 1; 770195801Smav } 771195801Smav if (ch->frozen) { 772195801Smav union ccb *fccb = ch->frozen; 773195801Smav ch->frozen = NULL; 774198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 775198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 776198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 777198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 778198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 779198321Smav } 780195801Smav xpt_done(fccb); 781195801Smav } 782195801Smav if (estatus == SIIS_P_CMDERR_DEV || 783195801Smav estatus == SIIS_P_CMDERR_SDB || 784195801Smav estatus == SIIS_P_CMDERR_DATAFIS) { 785195801Smav tslots = ch->numtslots[port]; 786195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 787198852Smav /* XXX: requests in loading state. */ 788195801Smav if (((ch->rslots >> i) & 1) == 0) 789195801Smav continue; 790195801Smav if (ch->slot[i].ccb->ccb_h.target_id != port) 791195801Smav continue; 792195801Smav if (tslots == 0) { 793195801Smav /* Untagged operation. */ 794195801Smav if (i == ccs) 795195801Smav et = SIIS_ERR_TFE; 796195801Smav else 797195801Smav et = SIIS_ERR_INNOCENT; 798195801Smav } else { 799195801Smav /* Tagged operation. */ 800195801Smav et = SIIS_ERR_NCQ; 801195801Smav } 802195801Smav siis_end_transaction(&ch->slot[i], et); 803195801Smav } 804195801Smav /* 805195801Smav * We can't reinit port if there are some other 806195801Smav * commands active, use resume to complete them. 807195801Smav */ 808195801Smav if (ch->rslots != 0) 809195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 810195801Smav } else { 811195801Smav if (estatus == SIIS_P_CMDERR_SENDFIS || 812195801Smav estatus == SIIS_P_CMDERR_INCSTATE || 813195801Smav estatus == SIIS_P_CMDERR_PPE || 814195801Smav estatus == SIIS_P_CMDERR_SERVICE) { 815195801Smav et = SIIS_ERR_SATA; 816195801Smav } else 817195801Smav et = SIIS_ERR_INVALID; 818195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 819198852Smav /* XXX: requests in loading state. */ 820195801Smav if (((ch->rslots >> i) & 1) == 0) 821195801Smav continue; 822195801Smav siis_end_transaction(&ch->slot[i], et); 823195801Smav } 824195801Smav } 825195801Smav } 826195801Smav} 827195801Smav 828195801Smav/* Must be called with channel locked. */ 829195801Smavstatic int 830195801Smavsiis_check_collision(device_t dev, union ccb *ccb) 831195801Smav{ 832195801Smav struct siis_channel *ch = device_get_softc(dev); 833195801Smav 834195801Smav mtx_assert(&ch->mtx, MA_OWNED); 835195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 836195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 837195801Smav /* Atomic command while anything active. */ 838195801Smav if (ch->numrslots != 0) 839195801Smav return (1); 840195801Smav } 841195801Smav /* We have some atomic command running. */ 842195801Smav if (ch->aslots != 0) 843195801Smav return (1); 844195801Smav return (0); 845195801Smav} 846195801Smav 847195801Smav/* Must be called with channel locked. */ 848195801Smavstatic void 849195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb) 850195801Smav{ 851195801Smav struct siis_channel *ch = device_get_softc(dev); 852195801Smav struct siis_slot *slot; 853195801Smav int tag; 854195801Smav 855195801Smav mtx_assert(&ch->mtx, MA_OWNED); 856195801Smav /* Choose empty slot. */ 857195801Smav tag = ch->lastslot; 858198896Smav while (ch->slot[tag].state != SIIS_SLOT_EMPTY) { 859198896Smav if (++tag >= SIIS_MAX_SLOTS) 860195801Smav tag = 0; 861198896Smav KASSERT(tag != ch->lastslot, ("siis: ALL SLOTS BUSY!")); 862198896Smav } 863195801Smav ch->lastslot = tag; 864195801Smav /* Occupy chosen slot. */ 865195801Smav slot = &ch->slot[tag]; 866195801Smav slot->ccb = ccb; 867195801Smav /* Update channel stats. */ 868195801Smav ch->numrslots++; 869195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 870195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 871195801Smav ch->numtslots[ccb->ccb_h.target_id]++; 872195801Smav } 873195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 874195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 875195801Smav ch->aslots |= (1 << slot->slot); 876195801Smav slot->dma.nsegs = 0; 877195801Smav /* If request moves data, setup and load SG list */ 878195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 879195801Smav void *buf; 880195801Smav bus_size_t size; 881195801Smav 882195801Smav slot->state = SIIS_SLOT_LOADING; 883195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 884195801Smav buf = ccb->ataio.data_ptr; 885195801Smav size = ccb->ataio.dxfer_len; 886195801Smav } else { 887195801Smav buf = ccb->csio.data_ptr; 888195801Smav size = ccb->csio.dxfer_len; 889195801Smav } 890195801Smav bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 891195801Smav buf, size, siis_dmasetprd, slot, 0); 892195801Smav } else 893195801Smav siis_execute_transaction(slot); 894195801Smav} 895195801Smav 896195801Smav/* Locked by busdma engine. */ 897195801Smavstatic void 898195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 899195801Smav{ 900195801Smav struct siis_slot *slot = arg; 901195801Smav struct siis_channel *ch = device_get_softc(slot->dev); 902195801Smav struct siis_cmd *ctp; 903195801Smav struct siis_dma_prd *prd; 904195801Smav int i; 905195801Smav 906195801Smav mtx_assert(&ch->mtx, MA_OWNED); 907195801Smav if (error) { 908195801Smav device_printf(slot->dev, "DMA load error\n"); 909195801Smav if (!ch->readlog) 910195801Smav xpt_freeze_simq(ch->sim, 1); 911195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 912195801Smav return; 913195801Smav } 914195801Smav KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 915195801Smav /* Get a piece of the workspace for this request */ 916195801Smav ctp = (struct siis_cmd *) 917195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 918195801Smav /* Fill S/G table */ 919195801Smav if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 920195801Smav prd = &ctp->u.ata.prd[0]; 921195801Smav else 922195801Smav prd = &ctp->u.atapi.prd[0]; 923195801Smav for (i = 0; i < nsegs; i++) { 924195801Smav prd[i].dba = htole64(segs[i].ds_addr); 925195801Smav prd[i].dbc = htole32(segs[i].ds_len); 926195801Smav prd[i].control = 0; 927195801Smav } 928195801Smav prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 929195801Smav slot->dma.nsegs = nsegs; 930195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 931195801Smav ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 932195801Smav BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 933195801Smav siis_execute_transaction(slot); 934195801Smav} 935195801Smav 936195801Smav/* Must be called with channel locked. */ 937195801Smavstatic void 938195801Smavsiis_execute_transaction(struct siis_slot *slot) 939195801Smav{ 940195801Smav device_t dev = slot->dev; 941195801Smav struct siis_channel *ch = device_get_softc(dev); 942195801Smav struct siis_cmd *ctp; 943195801Smav union ccb *ccb = slot->ccb; 944195801Smav u_int64_t prb_bus; 945195801Smav 946195801Smav mtx_assert(&ch->mtx, MA_OWNED); 947195801Smav /* Get a piece of the workspace for this request */ 948195801Smav ctp = (struct siis_cmd *) 949195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 950195801Smav ctp->control = 0; 951195801Smav ctp->protocol_override = 0; 952195801Smav ctp->transfer_count = 0; 953195801Smav /* Special handling for Soft Reset command. */ 954195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 955195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) { 956195801Smav ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 957195801Smav } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 958195801Smav if (ccb->ccb_h.flags & CAM_DIR_IN) 959195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_READ); 960195801Smav if (ccb->ccb_h.flags & CAM_DIR_OUT) 961195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 962195801Smav } 963195801Smav /* Setup the FIS for this request */ 964195801Smav if (!siis_setup_fis(ctp, ccb, slot->slot)) { 965195801Smav device_printf(ch->dev, "Setting up SATA FIS failed\n"); 966195801Smav if (!ch->readlog) 967195801Smav xpt_freeze_simq(ch->sim, 1); 968195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 969195801Smav return; 970195801Smav } 971195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 972195801Smav BUS_DMASYNC_PREWRITE); 973195801Smav /* Issue command to the controller. */ 974195801Smav slot->state = SIIS_SLOT_RUNNING; 975195801Smav ch->rslots |= (1 << slot->slot); 976195801Smav prb_bus = ch->dma.work_bus + 977195801Smav SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 978195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 979195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 980195801Smav /* Start command execution timeout */ 981195801Smav callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000, 982195801Smav (timeout_t*)siis_timeout, slot); 983195801Smav return; 984195801Smav} 985195801Smav 986198852Smav/* Must be called with channel locked. */ 987195801Smavstatic void 988198852Smavsiis_process_timeout(device_t dev) 989195801Smav{ 990195801Smav struct siis_channel *ch = device_get_softc(dev); 991195801Smav int i; 992195801Smav 993195801Smav mtx_assert(&ch->mtx, MA_OWNED); 994198852Smav if (!ch->readlog && !ch->recovery) { 995195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 996198852Smav ch->recovery = 1; 997195801Smav } 998197838Smav /* Handle the rest of commands. */ 999195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1000195801Smav /* Do we have a running request on slot? */ 1001195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1002195801Smav continue; 1003198852Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT); 1004195801Smav } 1005195801Smav} 1006195801Smav 1007198852Smav/* Locked by callout mechanism. */ 1008198852Smavstatic void 1009198852Smavsiis_timeout(struct siis_slot *slot) 1010198852Smav{ 1011198852Smav device_t dev = slot->dev; 1012198852Smav struct siis_channel *ch = device_get_softc(dev); 1013198852Smav 1014198852Smav mtx_assert(&ch->mtx, MA_OWNED); 1015198896Smav /* Check for stale timeout. */ 1016198896Smav if (slot->state < SIIS_SLOT_RUNNING) 1017198896Smav return; 1018198852Smav device_printf(dev, "Timeout on slot %d\n", slot->slot); 1019198852Smavdevice_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 1020198852Smav __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 1021198852Smav ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 1022198852Smav ATA_INL(ch->r_mem, SIIS_P_SERR)); 1023198852Smav 1024198852Smav if (ch->toslots == 0) 1025198852Smav xpt_freeze_simq(ch->sim, 1); 1026198852Smav ch->toslots |= (1 << slot->slot); 1027198852Smav if ((ch->rslots & ~ch->toslots) == 0) 1028198852Smav siis_process_timeout(dev); 1029198852Smav else 1030198852Smav device_printf(dev, " ... waiting for slots %08x\n", 1031198852Smav ch->rslots & ~ch->toslots); 1032198852Smav} 1033198852Smav 1034195801Smav/* Must be called with channel locked. */ 1035195801Smavstatic void 1036195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 1037195801Smav{ 1038195801Smav device_t dev = slot->dev; 1039195801Smav struct siis_channel *ch = device_get_softc(dev); 1040195801Smav union ccb *ccb = slot->ccb; 1041195801Smav 1042195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1043195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1044195801Smav BUS_DMASYNC_POSTWRITE); 1045195801Smav /* Read result registers to the result struct 1046195801Smav * May be incorrect if several commands finished same time, 1047195801Smav * so read only when sure or have to. 1048195801Smav */ 1049195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1050195801Smav struct ata_res *res = &ccb->ataio.res; 1051195801Smav if ((et == SIIS_ERR_TFE) || 1052195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1053195801Smav int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 1054195801Smav 1055195801Smav res->status = ATA_INB(ch->r_mem, offs + 2); 1056195801Smav res->error = ATA_INB(ch->r_mem, offs + 3); 1057195801Smav res->lba_low = ATA_INB(ch->r_mem, offs + 4); 1058195801Smav res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 1059195801Smav res->lba_high = ATA_INB(ch->r_mem, offs + 6); 1060195801Smav res->device = ATA_INB(ch->r_mem, offs + 7); 1061195801Smav res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 1062195801Smav res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 1063195801Smav res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 1064195801Smav res->sector_count = ATA_INB(ch->r_mem, offs + 12); 1065195801Smav res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 1066195801Smav } else 1067195801Smav bzero(res, sizeof(*res)); 1068195801Smav } 1069195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1070195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1071195801Smav (ccb->ccb_h.flags & CAM_DIR_IN) ? 1072195801Smav BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1073195801Smav bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1074195801Smav } 1075195801Smav /* Set proper result status. */ 1076195801Smav if (et != SIIS_ERR_NONE || ch->recovery) { 1077195801Smav ch->eslots |= (1 << slot->slot); 1078195801Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1079195801Smav } 1080198321Smav /* In case of error, freeze device for proper recovery. */ 1081198321Smav if (et != SIIS_ERR_NONE && 1082198321Smav !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1083198321Smav xpt_freeze_devq(ccb->ccb_h.path, 1); 1084198321Smav ccb->ccb_h.status |= CAM_DEV_QFRZN; 1085198321Smav } 1086198321Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1087195801Smav switch (et) { 1088195801Smav case SIIS_ERR_NONE: 1089195801Smav ccb->ccb_h.status |= CAM_REQ_CMP; 1090195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1091195801Smav ccb->csio.scsi_status = SCSI_STATUS_OK; 1092195801Smav break; 1093195801Smav case SIIS_ERR_INVALID: 1094198852Smav ch->fatalerr = 1; 1095195801Smav ccb->ccb_h.status |= CAM_REQ_INVALID; 1096195801Smav break; 1097195801Smav case SIIS_ERR_INNOCENT: 1098195801Smav ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1099195801Smav break; 1100195801Smav case SIIS_ERR_TFE: 1101198321Smav case SIIS_ERR_NCQ: 1102195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1103195801Smav ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1104195801Smav ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1105195801Smav } else { 1106195801Smav ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1107195801Smav } 1108195801Smav break; 1109195801Smav case SIIS_ERR_SATA: 1110198852Smav ch->fatalerr = 1; 1111195801Smav ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1112195801Smav break; 1113195801Smav case SIIS_ERR_TIMEOUT: 1114198852Smav ch->fatalerr = 1; 1115195801Smav ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1116195801Smav break; 1117195801Smav default: 1118195801Smav ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1119195801Smav } 1120195801Smav /* Free slot. */ 1121195801Smav ch->rslots &= ~(1 << slot->slot); 1122195801Smav ch->aslots &= ~(1 << slot->slot); 1123198852Smav if (et != SIIS_ERR_TIMEOUT) { 1124198852Smav if (ch->toslots == (1 << slot->slot)) 1125198852Smav xpt_release_simq(ch->sim, TRUE); 1126198852Smav ch->toslots &= ~(1 << slot->slot); 1127198852Smav } 1128195801Smav slot->state = SIIS_SLOT_EMPTY; 1129195801Smav slot->ccb = NULL; 1130195801Smav /* Update channel stats. */ 1131195801Smav ch->numrslots--; 1132195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1133195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1134195801Smav ch->numtslots[ccb->ccb_h.target_id]--; 1135195801Smav } 1136198852Smav /* If it was our READ LOG command - process it. */ 1137198852Smav if (ch->readlog) { 1138198852Smav siis_process_read_log(dev, ccb); 1139195801Smav /* If it was NCQ command error, put result on hold. */ 1140198852Smav } else if (et == SIIS_ERR_NCQ) { 1141195801Smav ch->hold[slot->slot] = ccb; 1142195801Smav ch->numhslots++; 1143198852Smav } else 1144195801Smav xpt_done(ccb); 1145195801Smav /* Unfreeze frozen command. */ 1146195801Smav if (ch->frozen && ch->numrslots == 0) { 1147195801Smav union ccb *fccb = ch->frozen; 1148195801Smav ch->frozen = NULL; 1149195801Smav siis_begin_transaction(dev, fccb); 1150195801Smav xpt_release_simq(ch->sim, TRUE); 1151195801Smav } 1152195801Smav /* If we have no other active commands, ... */ 1153195801Smav if (ch->rslots == 0) { 1154198852Smav /* if there were timeouts or fatal error - reset port. */ 1155198852Smav if (ch->toslots != 0 || ch->fatalerr) { 1156198852Smav siis_reset(dev); 1157198852Smav } else { 1158198852Smav /* if we have slots in error, we can reinit port. */ 1159198852Smav if (ch->eslots != 0) 1160198852Smav siis_portinit(dev); 1161198852Smav /* if there commands on hold, we can do READ LOG. */ 1162198852Smav if (!ch->readlog && ch->numhslots) 1163198852Smav siis_issue_read_log(dev); 1164198852Smav } 1165198852Smav /* If all the reset of commands are in timeout - abort them. */ 1166198852Smav } else if ((ch->rslots & ~ch->toslots) == 0) 1167198852Smav siis_process_timeout(dev); 1168195801Smav} 1169195801Smav 1170195801Smavstatic void 1171195801Smavsiis_issue_read_log(device_t dev) 1172195801Smav{ 1173195801Smav struct siis_channel *ch = device_get_softc(dev); 1174195801Smav union ccb *ccb; 1175195801Smav struct ccb_ataio *ataio; 1176195801Smav int i; 1177195801Smav 1178195801Smav /* Find some holden command. */ 1179195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1180195801Smav if (ch->hold[i]) 1181195801Smav break; 1182195801Smav } 1183195801Smav if (i == SIIS_MAX_SLOTS) 1184195801Smav return; 1185195801Smav ch->readlog = 1; 1186195801Smav ccb = xpt_alloc_ccb_nowait(); 1187195801Smav if (ccb == NULL) { 1188195801Smav device_printf(dev, "Unable allocate READ LOG command"); 1189195801Smav return; /* XXX */ 1190195801Smav } 1191195801Smav ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1192195801Smav ccb->ccb_h.func_code = XPT_ATA_IO; 1193195801Smav ccb->ccb_h.flags = CAM_DIR_IN; 1194195801Smav ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1195195801Smav ataio = &ccb->ataio; 1196195801Smav ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 1197195801Smav if (ataio->data_ptr == NULL) { 1198195801Smav device_printf(dev, "Unable allocate memory for READ LOG command"); 1199195801Smav return; /* XXX */ 1200195801Smav } 1201195801Smav ataio->dxfer_len = 512; 1202195801Smav bzero(&ataio->cmd, sizeof(ataio->cmd)); 1203195801Smav ataio->cmd.flags = CAM_ATAIO_48BIT; 1204195801Smav ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1205195801Smav ataio->cmd.sector_count = 1; 1206195801Smav ataio->cmd.sector_count_exp = 0; 1207195801Smav ataio->cmd.lba_low = 0x10; 1208195801Smav ataio->cmd.lba_mid = 0; 1209195801Smav ataio->cmd.lba_mid_exp = 0; 1210195801Smav siis_begin_transaction(dev, ccb); 1211195801Smav} 1212195801Smav 1213195801Smavstatic void 1214195801Smavsiis_process_read_log(device_t dev, union ccb *ccb) 1215195801Smav{ 1216195801Smav struct siis_channel *ch = device_get_softc(dev); 1217195801Smav uint8_t *data; 1218195801Smav struct ata_res *res; 1219195801Smav int i; 1220195801Smav 1221195801Smav ch->readlog = 0; 1222195801Smav data = ccb->ataio.data_ptr; 1223195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1224195801Smav (data[0] & 0x80) == 0) { 1225195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1226195801Smav if (!ch->hold[i]) 1227195801Smav continue; 1228195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1229195801Smav continue; 1230195801Smav if ((data[0] & 0x1F) == i) { 1231195801Smav res = &ch->hold[i]->ataio.res; 1232195801Smav res->status = data[2]; 1233195801Smav res->error = data[3]; 1234195801Smav res->lba_low = data[4]; 1235195801Smav res->lba_mid = data[5]; 1236195801Smav res->lba_high = data[6]; 1237195801Smav res->device = data[7]; 1238195801Smav res->lba_low_exp = data[8]; 1239195801Smav res->lba_mid_exp = data[9]; 1240195801Smav res->lba_high_exp = data[10]; 1241195801Smav res->sector_count = data[12]; 1242195801Smav res->sector_count_exp = data[13]; 1243195801Smav } else { 1244195801Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1245195801Smav ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1246195801Smav } 1247195801Smav xpt_done(ch->hold[i]); 1248195801Smav ch->hold[i] = NULL; 1249195801Smav ch->numhslots--; 1250195801Smav } 1251195801Smav } else { 1252195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1253195801Smav device_printf(dev, "Error while READ LOG EXT\n"); 1254195801Smav else if ((data[0] & 0x80) == 0) { 1255195801Smav device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1256195801Smav } 1257195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1258195801Smav if (!ch->hold[i]) 1259195801Smav continue; 1260195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1261195801Smav continue; 1262195801Smav xpt_done(ch->hold[i]); 1263195801Smav ch->hold[i] = NULL; 1264195801Smav ch->numhslots--; 1265195801Smav } 1266195801Smav } 1267195801Smav free(ccb->ataio.data_ptr, M_SIIS); 1268195801Smav xpt_free_ccb(ccb); 1269195801Smav} 1270195801Smav 1271195801Smavstatic void 1272195801Smavsiis_portinit(device_t dev) 1273195801Smav{ 1274195801Smav struct siis_channel *ch = device_get_softc(dev); 1275195801Smav int i; 1276195801Smav 1277195801Smav ch->eslots = 0; 1278195801Smav ch->recovery = 0; 1279195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 1280195801Smav for (i = 0; i < 16; i++) { 1281195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 1282195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 1283195801Smav } 1284195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 1285195801Smav siis_wait_ready(dev, 1000); 1286195801Smav} 1287195801Smav 1288198426Smavstatic int 1289195801Smavsiis_devreset(device_t dev) 1290195801Smav{ 1291195801Smav struct siis_channel *ch = device_get_softc(dev); 1292198426Smav int timeout = 0; 1293198426Smav uint32_t val; 1294195801Smav 1295195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 1296198426Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1297198426Smav SIIS_P_CTL_DEV_RESET) != 0) { 1298198426Smav DELAY(1000); 1299198426Smav if (timeout++ > 100) { 1300198426Smav device_printf(dev, "device reset stuck (timeout %dms) " 1301198426Smav "status = %08x\n", timeout, val); 1302198426Smav return (EBUSY); 1303198426Smav } 1304198426Smav } 1305198426Smav if (bootverbose) 1306198426Smav device_printf(dev, "device reset time=%dms\n", timeout); 1307198426Smav return (0); 1308195801Smav} 1309195801Smav 1310195801Smavstatic int 1311195801Smavsiis_wait_ready(device_t dev, int t) 1312195801Smav{ 1313195801Smav struct siis_channel *ch = device_get_softc(dev); 1314195801Smav int timeout = 0; 1315195801Smav uint32_t val; 1316195801Smav 1317195801Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1318195801Smav SIIS_P_CTL_READY) == 0) { 1319195801Smav DELAY(1000); 1320195801Smav if (timeout++ > t) { 1321195801Smav device_printf(dev, "port is not ready (timeout %dms) " 1322195801Smav "status = %08x\n", t, val); 1323195801Smav return (EBUSY); 1324195801Smav } 1325195801Smav } 1326195801Smav if (bootverbose) 1327195801Smav device_printf(dev, "ready wait time=%dms\n", timeout); 1328195801Smav return (0); 1329195801Smav} 1330195801Smav 1331195801Smavstatic void 1332195801Smavsiis_reset(device_t dev) 1333195801Smav{ 1334195801Smav struct siis_channel *ch = device_get_softc(dev); 1335198852Smav int i, retry = 0; 1336198426Smav uint32_t val; 1337195801Smav 1338195801Smav if (bootverbose) 1339195801Smav device_printf(dev, "SIIS reset...\n"); 1340198852Smav if (!ch->readlog && !ch->recovery) 1341198852Smav xpt_freeze_simq(ch->sim, ch->numrslots); 1342198852Smav /* Requeue frozen command. */ 1343195801Smav if (ch->frozen) { 1344195801Smav union ccb *fccb = ch->frozen; 1345195801Smav ch->frozen = NULL; 1346198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1347198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1348198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1349198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1350198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1351198321Smav } 1352195801Smav xpt_done(fccb); 1353195801Smav } 1354198426Smav /* Requeue all running commands. */ 1355195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1356195801Smav /* Do we have a running request on slot? */ 1357195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1358195801Smav continue; 1359195801Smav /* XXX; Commands in loading state. */ 1360195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1361195801Smav } 1362198852Smav /* Finish all holden commands as-is. */ 1363198852Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1364198852Smav if (!ch->hold[i]) 1365198852Smav continue; 1366198852Smav xpt_done(ch->hold[i]); 1367198852Smav ch->hold[i] = NULL; 1368198852Smav ch->numhslots--; 1369198852Smav } 1370198852Smav if (ch->toslots != 0) 1371198852Smav xpt_release_simq(ch->sim, TRUE); 1372198852Smav ch->eslots = 0; 1373198852Smav ch->recovery = 0; 1374198852Smav ch->toslots = 0; 1375198852Smav ch->fatalerr = 0; 1376198426Smav /* Disable port interrupts */ 1377198426Smav ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 1378198426Smav /* Set speed limit. */ 1379198426Smav if (ch->sata_rev == 1) 1380198426Smav val = ATA_SC_SPD_SPEED_GEN1; 1381198426Smav else if (ch->sata_rev == 2) 1382198426Smav val = ATA_SC_SPD_SPEED_GEN2; 1383198426Smav else if (ch->sata_rev == 3) 1384198426Smav val = ATA_SC_SPD_SPEED_GEN3; 1385198426Smav else 1386198426Smav val = 0; 1387198426Smav ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 1388198426Smav ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 1389198426Smav (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 1390198852Smavretry: 1391198426Smav siis_devreset(dev); 1392195801Smav /* Reset and reconnect PHY, */ 1393198426Smav if (!siis_sata_connect(ch)) { 1394195801Smav ch->devices = 0; 1395195801Smav /* Enable port interrupts */ 1396195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1397195801Smav if (bootverbose) 1398195801Smav device_printf(dev, 1399195801Smav "SIIS reset done: phy reset found no device\n"); 1400195801Smav /* Tell the XPT about the event */ 1401195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1402195801Smav return; 1403195801Smav } 1404195801Smav /* Wait for clearing busy status. */ 1405198852Smav if (siis_wait_ready(dev, 10000)) { 1406195801Smav device_printf(dev, "device ready timeout\n"); 1407198852Smav if (!retry) { 1408198852Smav device_printf(dev, "trying full port reset ...\n"); 1409198852Smav /* Get port to the reset state. */ 1410198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 1411198852Smav DELAY(10000); 1412198852Smav /* Get port out of reset state. */ 1413198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 1414198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 1415198852Smav if (ch->pm_present) 1416198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1417198852Smav else 1418198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1419198852Smav siis_wait_ready(dev, 5000); 1420198852Smav retry = 1; 1421198852Smav goto retry; 1422198852Smav } 1423198852Smav } 1424195801Smav ch->devices = 1; 1425195801Smav /* Enable port interrupts */ 1426195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 1427195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1428195801Smav if (bootverbose) 1429195801Smav device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 1430195801Smav /* Tell the XPT about the event */ 1431195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1432195801Smav} 1433195801Smav 1434195801Smavstatic int 1435195801Smavsiis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag) 1436195801Smav{ 1437195801Smav u_int8_t *fis = &ctp->fis[0]; 1438195801Smav 1439195801Smav bzero(fis, 24); 1440195801Smav fis[0] = 0x27; /* host to device */ 1441195801Smav fis[1] = (ccb->ccb_h.target_id & 0x0f); 1442195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1443195801Smav fis[1] |= 0x80; 1444195801Smav fis[2] = ATA_PACKET_CMD; 1445195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 1446195801Smav fis[3] = ATA_F_DMA; 1447195801Smav else { 1448195801Smav fis[5] = ccb->csio.dxfer_len; 1449195801Smav fis[6] = ccb->csio.dxfer_len >> 8; 1450195801Smav } 1451195801Smav fis[7] = ATA_D_LBA; 1452195801Smav fis[15] = ATA_A_4BIT; 1453195801Smav bzero(ctp->u.atapi.ccb, 16); 1454195801Smav bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1455195801Smav ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1456195801Smav ctp->u.atapi.ccb, ccb->csio.cdb_len); 1457195801Smav } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1458195801Smav fis[1] |= 0x80; 1459195801Smav fis[2] = ccb->ataio.cmd.command; 1460195801Smav fis[3] = ccb->ataio.cmd.features; 1461195801Smav fis[4] = ccb->ataio.cmd.lba_low; 1462195801Smav fis[5] = ccb->ataio.cmd.lba_mid; 1463195801Smav fis[6] = ccb->ataio.cmd.lba_high; 1464195801Smav fis[7] = ccb->ataio.cmd.device; 1465195801Smav fis[8] = ccb->ataio.cmd.lba_low_exp; 1466195801Smav fis[9] = ccb->ataio.cmd.lba_mid_exp; 1467195801Smav fis[10] = ccb->ataio.cmd.lba_high_exp; 1468195801Smav fis[11] = ccb->ataio.cmd.features_exp; 1469195801Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1470195801Smav fis[12] = tag << 3; 1471195801Smav fis[13] = 0; 1472195801Smav } else { 1473195801Smav fis[12] = ccb->ataio.cmd.sector_count; 1474195801Smav fis[13] = ccb->ataio.cmd.sector_count_exp; 1475195801Smav } 1476195801Smav fis[15] = ATA_A_4BIT; 1477195801Smav } else { 1478195801Smav /* Soft reset. */ 1479195801Smav } 1480195801Smav return (20); 1481195801Smav} 1482195801Smav 1483195801Smavstatic int 1484195801Smavsiis_sata_connect(struct siis_channel *ch) 1485195801Smav{ 1486195801Smav u_int32_t status; 1487195801Smav int timeout; 1488195801Smav 1489195801Smav /* Wait up to 100ms for "connect well" */ 1490195801Smav for (timeout = 0; timeout < 100 ; timeout++) { 1491195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 1492195801Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1493195801Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1494195801Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 1495195801Smav break; 1496195801Smav DELAY(1000); 1497195801Smav } 1498195801Smav if (timeout >= 100) { 1499195801Smav if (bootverbose) { 1500195801Smav device_printf(ch->dev, "SATA connect timeout status=%08x\n", 1501195801Smav status); 1502195801Smav } 1503195801Smav return (0); 1504195801Smav } 1505195801Smav if (bootverbose) { 1506195801Smav device_printf(ch->dev, "SATA connect time=%dms status=%08x\n", 1507195801Smav timeout, status); 1508195801Smav } 1509195801Smav /* Clear SATA error register */ 1510195801Smav ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 1511195801Smav return (1); 1512195801Smav} 1513195801Smav 1514195801Smavstatic void 1515195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb) 1516195801Smav{ 1517195801Smav device_t dev; 1518195801Smav struct siis_channel *ch; 1519195801Smav 1520195801Smav CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 1521195801Smav ccb->ccb_h.func_code)); 1522195801Smav 1523195801Smav ch = (struct siis_channel *)cam_sim_softc(sim); 1524195801Smav dev = ch->dev; 1525195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1526195801Smav switch (ccb->ccb_h.func_code) { 1527195801Smav /* Common cases first */ 1528195801Smav case XPT_ATA_IO: /* Execute the requested I/O operation */ 1529195801Smav case XPT_SCSI_IO: 1530195801Smav if (ch->devices == 0) { 1531195801Smav ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1532195801Smav xpt_done(ccb); 1533195801Smav break; 1534195801Smav } 1535195801Smav /* Check for command collision. */ 1536195801Smav if (siis_check_collision(dev, ccb)) { 1537195801Smav /* Freeze command. */ 1538195801Smav ch->frozen = ccb; 1539195801Smav /* We have only one frozen slot, so freeze simq also. */ 1540195801Smav xpt_freeze_simq(ch->sim, 1); 1541195801Smav return; 1542195801Smav } 1543195801Smav siis_begin_transaction(dev, ccb); 1544195801Smav break; 1545195801Smav case XPT_EN_LUN: /* Enable LUN as a target */ 1546195801Smav case XPT_TARGET_IO: /* Execute target I/O request */ 1547195801Smav case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1548195801Smav case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1549195801Smav case XPT_ABORT: /* Abort the specified CCB */ 1550195801Smav /* XXX Implement */ 1551195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1552195801Smav xpt_done(ccb); 1553195801Smav break; 1554195801Smav case XPT_SET_TRAN_SETTINGS: 1555195801Smav { 1556195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1557195801Smav 1558195801Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 1559198852Smav ch->pm_present = cts->xport_specific.sata.pm_present; 1560198852Smav if (ch->pm_present) 1561195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1562195801Smav else 1563195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1564195801Smav } 1565195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1566195801Smav xpt_done(ccb); 1567195801Smav break; 1568195801Smav } 1569195801Smav case XPT_GET_TRAN_SETTINGS: 1570195801Smav /* Get default/user set transfer settings for the target */ 1571195801Smav { 1572195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1573195801Smav uint32_t status; 1574195801Smav 1575195801Smav cts->protocol = PROTO_ATA; 1576196655Smav cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1577195801Smav cts->transport = XPORT_SATA; 1578196655Smav cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1579195801Smav cts->proto_specific.valid = 0; 1580195801Smav cts->xport_specific.sata.valid = 0; 1581195801Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1582195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1583195801Smav else 1584195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SCTL) & ATA_SC_SPD_MASK; 1585195801Smav if (status & ATA_SS_SPD_GEN3) { 1586195801Smav cts->xport_specific.sata.bitrate = 600000; 1587195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1588195801Smav } else if (status & ATA_SS_SPD_GEN2) { 1589195801Smav cts->xport_specific.sata.bitrate = 300000; 1590195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1591195801Smav } else if (status & ATA_SS_SPD_GEN1) { 1592195801Smav cts->xport_specific.sata.bitrate = 150000; 1593195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 1594195801Smav } 1595198852Smav cts->xport_specific.sata.pm_present = ch->pm_present; 1596195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1597195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1598195801Smav xpt_done(ccb); 1599195801Smav break; 1600195801Smav } 1601195801Smav#if 0 1602195801Smav case XPT_CALC_GEOMETRY: 1603195801Smav { 1604195801Smav struct ccb_calc_geometry *ccg; 1605195801Smav uint32_t size_mb; 1606195801Smav uint32_t secs_per_cylinder; 1607195801Smav 1608195801Smav ccg = &ccb->ccg; 1609195801Smav size_mb = ccg->volume_size 1610195801Smav / ((1024L * 1024L) / ccg->block_size); 1611195801Smav if (size_mb >= 1024 && (aha->extended_trans != 0)) { 1612195801Smav if (size_mb >= 2048) { 1613195801Smav ccg->heads = 255; 1614195801Smav ccg->secs_per_track = 63; 1615195801Smav } else { 1616195801Smav ccg->heads = 128; 1617195801Smav ccg->secs_per_track = 32; 1618195801Smav } 1619195801Smav } else { 1620195801Smav ccg->heads = 64; 1621195801Smav ccg->secs_per_track = 32; 1622195801Smav } 1623195801Smav secs_per_cylinder = ccg->heads * ccg->secs_per_track; 1624195801Smav ccg->cylinders = ccg->volume_size / secs_per_cylinder; 1625195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1626195801Smav xpt_done(ccb); 1627195801Smav break; 1628195801Smav } 1629195801Smav#endif 1630195801Smav case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1631195801Smav case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1632195801Smav siis_reset(dev); 1633195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1634195801Smav xpt_done(ccb); 1635195801Smav break; 1636195801Smav case XPT_TERM_IO: /* Terminate the I/O process */ 1637195801Smav /* XXX Implement */ 1638195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1639195801Smav xpt_done(ccb); 1640195801Smav break; 1641195801Smav case XPT_PATH_INQ: /* Path routing inquiry */ 1642195801Smav { 1643195801Smav struct ccb_pathinq *cpi = &ccb->cpi; 1644195801Smav 1645195801Smav cpi->version_num = 1; /* XXX??? */ 1646195801Smav cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 1647195801Smav cpi->hba_inquiry |= PI_SATAPM; 1648195801Smav cpi->target_sprt = 0; 1649195801Smav cpi->hba_misc = PIM_SEQSCAN; 1650195801Smav cpi->hba_eng_cnt = 0; 1651198322Smav cpi->max_target = 15; 1652195801Smav cpi->max_lun = 0; 1653195801Smav cpi->initiator_id = 0; 1654195801Smav cpi->bus_id = cam_sim_bus(sim); 1655195801Smav cpi->base_transfer_speed = 150000; 1656195801Smav strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1657195801Smav strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 1658195801Smav strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1659195801Smav cpi->unit_number = cam_sim_unit(sim); 1660195801Smav cpi->transport = XPORT_SATA; 1661196655Smav cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1662195801Smav cpi->protocol = PROTO_ATA; 1663196655Smav cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1664195801Smav cpi->ccb_h.status = CAM_REQ_CMP; 1665195801Smav cpi->maxio = MAXPHYS; 1666195801Smav xpt_done(ccb); 1667195801Smav break; 1668195801Smav } 1669195801Smav default: 1670195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1671195801Smav xpt_done(ccb); 1672195801Smav break; 1673195801Smav } 1674195801Smav} 1675195801Smav 1676195801Smavstatic void 1677195801Smavsiispoll(struct cam_sim *sim) 1678195801Smav{ 1679195801Smav struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 1680195801Smav 1681195801Smav siis_ch_intr(ch->dev); 1682195801Smav} 1683