siis.c revision 220830
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 220830 2011-04-19 10:57:40Z 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> 47217877Smav#include <dev/led/led.h> 48195801Smav#include <dev/pci/pcivar.h> 49195801Smav#include <dev/pci/pcireg.h> 50195801Smav#include "siis.h" 51195801Smav 52195801Smav#include <cam/cam.h> 53195801Smav#include <cam/cam_ccb.h> 54195801Smav#include <cam/cam_sim.h> 55195801Smav#include <cam/cam_xpt_sim.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); 63208393Smavstatic int siis_ch_init(device_t dev); 64208393Smavstatic int siis_ch_deinit(device_t dev); 65195801Smavstatic int siis_ch_suspend(device_t dev); 66195801Smavstatic int siis_ch_resume(device_t dev); 67195801Smavstatic void siis_ch_intr_locked(void *data); 68195801Smavstatic void siis_ch_intr(void *data); 69217877Smavstatic void siis_ch_led(void *priv, int onoff); 70195801Smavstatic void siis_begin_transaction(device_t dev, union ccb *ccb); 71195801Smavstatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 72195801Smavstatic void siis_execute_transaction(struct siis_slot *slot); 73195801Smavstatic void siis_timeout(struct siis_slot *slot); 74195801Smavstatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et); 75199821Smavstatic int siis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag); 76195801Smavstatic void siis_dmainit(device_t dev); 77195801Smavstatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 78195801Smavstatic void siis_dmafini(device_t dev); 79195801Smavstatic void siis_slotsalloc(device_t dev); 80195801Smavstatic void siis_slotsfree(device_t dev); 81195801Smavstatic void siis_reset(device_t dev); 82195801Smavstatic void siis_portinit(device_t dev); 83195801Smavstatic int siis_wait_ready(device_t dev, int t); 84195801Smav 85195801Smavstatic int siis_sata_connect(struct siis_channel *ch); 86195801Smav 87220566Smavstatic void siis_issue_recovery(device_t dev); 88195801Smavstatic void siis_process_read_log(device_t dev, union ccb *ccb); 89220566Smavstatic void siis_process_request_sense(device_t dev, union ccb *ccb); 90195801Smav 91195801Smavstatic void siisaction(struct cam_sim *sim, union ccb *ccb); 92195801Smavstatic void siispoll(struct cam_sim *sim); 93195801Smav 94195801SmavMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers"); 95195801Smav 96199132Smavstatic struct { 97199132Smav uint32_t id; 98199132Smav const char *name; 99199132Smav int ports; 100200217Smav int quirks; 101200217Smav#define SIIS_Q_SNTF 1 102205358Smav#define SIIS_Q_NOMSI 2 103199132Smav} siis_ids[] = { 104200217Smav {0x31241095, "SiI3124", 4, 0}, 105200217Smav {0x31248086, "SiI3124", 4, 0}, 106205358Smav {0x31321095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 107205358Smav {0x02421095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 108205358Smav {0x02441095, "SiI3132", 2, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 109205358Smav {0x31311095, "SiI3131", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 110205358Smav {0x35311095, "SiI3531", 1, SIIS_Q_SNTF|SIIS_Q_NOMSI}, 111200217Smav {0, NULL, 0, 0} 112199132Smav}; 113199132Smav 114220566Smav#define recovery_type spriv_field0 115220566Smav#define RECOVERY_NONE 0 116220566Smav#define RECOVERY_READ_LOG 1 117220566Smav#define RECOVERY_REQUEST_SENSE 2 118220566Smav#define recovery_slot spriv_field1 119220566Smav 120195801Smavstatic int 121195801Smavsiis_probe(device_t dev) 122195801Smav{ 123199132Smav char buf[64]; 124199132Smav int i; 125195801Smav uint32_t devid = pci_get_devid(dev); 126195801Smav 127199132Smav for (i = 0; siis_ids[i].id != 0; i++) { 128199132Smav if (siis_ids[i].id == devid) { 129200217Smav snprintf(buf, sizeof(buf), "%s SATA controller", 130199132Smav siis_ids[i].name); 131199132Smav device_set_desc_copy(dev, buf); 132199132Smav return (BUS_PROBE_VENDOR); 133199132Smav } 134195801Smav } 135199132Smav return (ENXIO); 136195801Smav} 137195801Smav 138195801Smavstatic int 139195801Smavsiis_attach(device_t dev) 140195801Smav{ 141195801Smav struct siis_controller *ctlr = device_get_softc(dev); 142195801Smav uint32_t devid = pci_get_devid(dev); 143195801Smav device_t child; 144199132Smav int error, i, unit; 145195801Smav 146200217Smav ctlr->dev = dev; 147199132Smav for (i = 0; siis_ids[i].id != 0; i++) { 148199132Smav if (siis_ids[i].id == devid) 149199132Smav break; 150199132Smav } 151200217Smav ctlr->quirks = siis_ids[i].quirks; 152195801Smav /* Global memory */ 153195801Smav ctlr->r_grid = PCIR_BAR(0); 154195801Smav if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 155195801Smav &ctlr->r_grid, RF_ACTIVE))) 156195801Smav return (ENXIO); 157200223Smav ctlr->gctl = ATA_INL(ctlr->r_gmem, SIIS_GCTL); 158195801Smav /* Channels memory */ 159195801Smav ctlr->r_rid = PCIR_BAR(2); 160195801Smav if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 161195801Smav &ctlr->r_rid, RF_ACTIVE))) 162195801Smav return (ENXIO); 163195801Smav /* Setup our own memory management for channels. */ 164208414Smav ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem); 165208414Smav ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem); 166195801Smav ctlr->sc_iomem.rm_type = RMAN_ARRAY; 167195801Smav ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 168195801Smav if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 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 return (error); 172195801Smav } 173195801Smav if ((error = rman_manage_region(&ctlr->sc_iomem, 174195801Smav rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 175195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 176195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 177195801Smav rman_fini(&ctlr->sc_iomem); 178195801Smav return (error); 179195801Smav } 180206652Smav pci_enable_busmaster(dev); 181195801Smav /* Reset controller */ 182195801Smav siis_resume(dev); 183195801Smav /* Number of HW channels */ 184199132Smav ctlr->channels = siis_ids[i].ports; 185195801Smav /* Setup interrupts. */ 186195801Smav if (siis_setup_interrupt(dev)) { 187195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 188195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 189195801Smav rman_fini(&ctlr->sc_iomem); 190195801Smav return ENXIO; 191195801Smav } 192195801Smav /* Attach all channels on this controller */ 193195801Smav for (unit = 0; unit < ctlr->channels; unit++) { 194195801Smav child = device_add_child(dev, "siisch", -1); 195195801Smav if (child == NULL) 196195801Smav device_printf(dev, "failed to add channel device\n"); 197195801Smav else 198195801Smav device_set_ivars(child, (void *)(intptr_t)unit); 199195801Smav } 200195801Smav bus_generic_attach(dev); 201195801Smav return 0; 202195801Smav} 203195801Smav 204195801Smavstatic int 205195801Smavsiis_detach(device_t dev) 206195801Smav{ 207195801Smav struct siis_controller *ctlr = device_get_softc(dev); 208195801Smav device_t *children; 209195801Smav int nchildren, i; 210195801Smav 211195801Smav /* Detach & delete all children */ 212195801Smav if (!device_get_children(dev, &children, &nchildren)) { 213195801Smav for (i = 0; i < nchildren; i++) 214195801Smav device_delete_child(dev, children[i]); 215195801Smav free(children, M_TEMP); 216195801Smav } 217195801Smav /* Free interrupts. */ 218195801Smav if (ctlr->irq.r_irq) { 219195801Smav bus_teardown_intr(dev, ctlr->irq.r_irq, 220195801Smav ctlr->irq.handle); 221195801Smav bus_release_resource(dev, SYS_RES_IRQ, 222195801Smav ctlr->irq.r_irq_rid, ctlr->irq.r_irq); 223195801Smav } 224195801Smav pci_release_msi(dev); 225195801Smav /* Free memory. */ 226195801Smav rman_fini(&ctlr->sc_iomem); 227195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 228195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem); 229195801Smav return (0); 230195801Smav} 231195801Smav 232195801Smavstatic int 233195801Smavsiis_suspend(device_t dev) 234195801Smav{ 235195801Smav struct siis_controller *ctlr = device_get_softc(dev); 236195801Smav 237195801Smav bus_generic_suspend(dev); 238195801Smav /* Put controller into reset state. */ 239200223Smav ctlr->gctl |= SIIS_GCTL_GRESET; 240200223Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 241195801Smav return 0; 242195801Smav} 243195801Smav 244195801Smavstatic int 245195801Smavsiis_resume(device_t dev) 246195801Smav{ 247195801Smav struct siis_controller *ctlr = device_get_softc(dev); 248195801Smav 249200291Smav /* Set PCIe max read request size to at least 1024 bytes */ 250203529Smav if (pci_get_max_read_req(dev) < 1024) 251203529Smav pci_set_max_read_req(dev, 1024); 252195801Smav /* Put controller into reset state. */ 253200223Smav ctlr->gctl |= SIIS_GCTL_GRESET; 254200223Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 255195801Smav DELAY(10000); 256195801Smav /* Get controller out of reset state and enable port interrupts. */ 257200223Smav ctlr->gctl &= ~(SIIS_GCTL_GRESET | SIIS_GCTL_I2C_IE); 258200223Smav ctlr->gctl |= 0x0000000f; 259200223Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, ctlr->gctl); 260195801Smav return (bus_generic_resume(dev)); 261195801Smav} 262195801Smav 263195801Smavstatic int 264195801Smavsiis_setup_interrupt(device_t dev) 265195801Smav{ 266195801Smav struct siis_controller *ctlr = device_get_softc(dev); 267205358Smav int msi = ctlr->quirks & SIIS_Q_NOMSI ? 0 : 1; 268195801Smav 269195801Smav /* Process hints. */ 270195801Smav resource_int_value(device_get_name(dev), 271195801Smav device_get_unit(dev), "msi", &msi); 272195801Smav if (msi < 0) 273195801Smav msi = 0; 274195801Smav else if (msi > 0) 275195801Smav msi = min(1, pci_msi_count(dev)); 276195801Smav /* Allocate MSI if needed/present. */ 277195801Smav if (msi && pci_alloc_msi(dev, &msi) != 0) 278195801Smav msi = 0; 279195801Smav /* Allocate all IRQs. */ 280195801Smav ctlr->irq.r_irq_rid = msi ? 1 : 0; 281195801Smav if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 282195801Smav &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 283195801Smav device_printf(dev, "unable to map interrupt\n"); 284195801Smav return ENXIO; 285195801Smav } 286195801Smav if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL, 287195801Smav siis_intr, ctlr, &ctlr->irq.handle))) { 288195801Smav /* SOS XXX release r_irq */ 289195801Smav device_printf(dev, "unable to setup interrupt\n"); 290195801Smav return ENXIO; 291195801Smav } 292195801Smav return (0); 293195801Smav} 294195801Smav 295195801Smav/* 296195801Smav * Common case interrupt handler. 297195801Smav */ 298195801Smavstatic void 299195801Smavsiis_intr(void *data) 300195801Smav{ 301195801Smav struct siis_controller *ctlr = (struct siis_controller *)data; 302195801Smav u_int32_t is; 303195801Smav void *arg; 304195801Smav int unit; 305195801Smav 306195801Smav is = ATA_INL(ctlr->r_gmem, SIIS_IS); 307195801Smav for (unit = 0; unit < ctlr->channels; unit++) { 308195801Smav if ((is & SIIS_IS_PORT(unit)) != 0 && 309195801Smav (arg = ctlr->interrupt[unit].argument)) { 310195801Smav ctlr->interrupt[unit].function(arg); 311195801Smav } 312195801Smav } 313200223Smav /* Acknowledge interrupt, if MSI enabled. */ 314200223Smav if (ctlr->irq.r_irq_rid) { 315200223Smav ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 316200223Smav ctlr->gctl | SIIS_GCTL_MSIACK); 317200223Smav } 318195801Smav} 319195801Smav 320195801Smavstatic struct resource * 321195801Smavsiis_alloc_resource(device_t dev, device_t child, int type, int *rid, 322195801Smav u_long start, u_long end, u_long count, u_int flags) 323195801Smav{ 324195801Smav struct siis_controller *ctlr = device_get_softc(dev); 325195801Smav int unit = ((struct siis_channel *)device_get_softc(child))->unit; 326195801Smav struct resource *res = NULL; 327195801Smav int offset = unit << 13; 328195801Smav long st; 329195801Smav 330195801Smav switch (type) { 331195801Smav case SYS_RES_MEMORY: 332195801Smav st = rman_get_start(ctlr->r_mem); 333195801Smav res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 334195801Smav st + offset + 0x2000, 0x2000, RF_ACTIVE, child); 335195801Smav if (res) { 336195801Smav bus_space_handle_t bsh; 337195801Smav bus_space_tag_t bst; 338195801Smav bsh = rman_get_bushandle(ctlr->r_mem); 339195801Smav bst = rman_get_bustag(ctlr->r_mem); 340195801Smav bus_space_subregion(bst, bsh, offset, 0x2000, &bsh); 341195801Smav rman_set_bushandle(res, bsh); 342195801Smav rman_set_bustag(res, bst); 343195801Smav } 344195801Smav break; 345195801Smav case SYS_RES_IRQ: 346195801Smav if (*rid == ATA_IRQ_RID) 347195801Smav res = ctlr->irq.r_irq; 348195801Smav break; 349195801Smav } 350195801Smav return (res); 351195801Smav} 352195801Smav 353195801Smavstatic int 354195801Smavsiis_release_resource(device_t dev, device_t child, int type, int rid, 355195801Smav struct resource *r) 356195801Smav{ 357195801Smav 358195801Smav switch (type) { 359195801Smav case SYS_RES_MEMORY: 360195801Smav rman_release_resource(r); 361195801Smav return (0); 362195801Smav case SYS_RES_IRQ: 363195801Smav if (rid != ATA_IRQ_RID) 364195801Smav return ENOENT; 365195801Smav return (0); 366195801Smav } 367195801Smav return (EINVAL); 368195801Smav} 369195801Smav 370195801Smavstatic int 371195801Smavsiis_setup_intr(device_t dev, device_t child, struct resource *irq, 372195801Smav int flags, driver_filter_t *filter, driver_intr_t *function, 373195801Smav void *argument, void **cookiep) 374195801Smav{ 375195801Smav struct siis_controller *ctlr = device_get_softc(dev); 376195801Smav int unit = (intptr_t)device_get_ivars(child); 377195801Smav 378195801Smav if (filter != NULL) { 379195801Smav printf("siis.c: we cannot use a filter here\n"); 380195801Smav return (EINVAL); 381195801Smav } 382195801Smav ctlr->interrupt[unit].function = function; 383195801Smav ctlr->interrupt[unit].argument = argument; 384195801Smav return (0); 385195801Smav} 386195801Smav 387195801Smavstatic int 388195801Smavsiis_teardown_intr(device_t dev, device_t child, struct resource *irq, 389195801Smav void *cookie) 390195801Smav{ 391195801Smav struct siis_controller *ctlr = device_get_softc(dev); 392195801Smav int unit = (intptr_t)device_get_ivars(child); 393195801Smav 394195801Smav ctlr->interrupt[unit].function = NULL; 395195801Smav ctlr->interrupt[unit].argument = NULL; 396195801Smav return (0); 397195801Smav} 398195801Smav 399195801Smavstatic int 400195801Smavsiis_print_child(device_t dev, device_t child) 401195801Smav{ 402195801Smav int retval; 403195801Smav 404195801Smav retval = bus_print_child_header(dev, child); 405195801Smav retval += printf(" at channel %d", 406195801Smav (int)(intptr_t)device_get_ivars(child)); 407195801Smav retval += bus_print_child_footer(dev, child); 408195801Smav 409195801Smav return (retval); 410195801Smav} 411195801Smav 412208410Smavstatic int 413208410Smavsiis_child_location_str(device_t dev, device_t child, char *buf, 414208410Smav size_t buflen) 415208410Smav{ 416208410Smav 417208410Smav snprintf(buf, buflen, "channel=%d", 418208410Smav (int)(intptr_t)device_get_ivars(child)); 419208410Smav return (0); 420208410Smav} 421208410Smav 422195801Smavdevclass_t siis_devclass; 423195801Smavstatic device_method_t siis_methods[] = { 424195801Smav DEVMETHOD(device_probe, siis_probe), 425195801Smav DEVMETHOD(device_attach, siis_attach), 426195801Smav DEVMETHOD(device_detach, siis_detach), 427195801Smav DEVMETHOD(device_suspend, siis_suspend), 428195801Smav DEVMETHOD(device_resume, siis_resume), 429195801Smav DEVMETHOD(bus_print_child, siis_print_child), 430195801Smav DEVMETHOD(bus_alloc_resource, siis_alloc_resource), 431195801Smav DEVMETHOD(bus_release_resource, siis_release_resource), 432195801Smav DEVMETHOD(bus_setup_intr, siis_setup_intr), 433195801Smav DEVMETHOD(bus_teardown_intr,siis_teardown_intr), 434208410Smav DEVMETHOD(bus_child_location_str, siis_child_location_str), 435195801Smav { 0, 0 } 436195801Smav}; 437195801Smavstatic driver_t siis_driver = { 438195801Smav "siis", 439195801Smav siis_methods, 440195801Smav sizeof(struct siis_controller) 441195801Smav}; 442195801SmavDRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0); 443195801SmavMODULE_VERSION(siis, 1); 444195801SmavMODULE_DEPEND(siis, cam, 1, 1, 1); 445195801Smav 446195801Smavstatic int 447195801Smavsiis_ch_probe(device_t dev) 448195801Smav{ 449195801Smav 450195801Smav device_set_desc_copy(dev, "SIIS channel"); 451195801Smav return (0); 452195801Smav} 453195801Smav 454195801Smavstatic int 455195801Smavsiis_ch_attach(device_t dev) 456195801Smav{ 457200217Smav struct siis_controller *ctlr = device_get_softc(device_get_parent(dev)); 458195801Smav struct siis_channel *ch = device_get_softc(dev); 459195801Smav struct cam_devq *devq; 460199821Smav int rid, error, i, sata_rev = 0; 461195801Smav 462195801Smav ch->dev = dev; 463195801Smav ch->unit = (intptr_t)device_get_ivars(dev); 464200217Smav ch->quirks = ctlr->quirks; 465195801Smav resource_int_value(device_get_name(dev), 466195801Smav device_get_unit(dev), "pm_level", &ch->pm_level); 467199821Smav resource_int_value(device_get_name(dev), 468199821Smav device_get_unit(dev), "sata_rev", &sata_rev); 469199747Smav for (i = 0; i < 16; i++) { 470199821Smav ch->user[i].revision = sata_rev; 471199747Smav ch->user[i].mode = 0; 472199747Smav ch->user[i].bytecount = 8192; 473199747Smav ch->user[i].tags = SIIS_MAX_SLOTS; 474199747Smav ch->curr[i] = ch->user[i]; 475207499Smav if (ch->pm_level) 476207499Smav ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ; 477220602Smav ch->user[i].caps |= CTS_SATA_CAPS_H_AN; 478199747Smav } 479195801Smav mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 480195801Smav rid = ch->unit; 481195801Smav if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 482195801Smav &rid, RF_ACTIVE))) 483195801Smav return (ENXIO); 484195801Smav siis_dmainit(dev); 485195801Smav siis_slotsalloc(dev); 486208393Smav siis_ch_init(dev); 487195801Smav mtx_lock(&ch->mtx); 488195801Smav rid = ATA_IRQ_RID; 489195801Smav if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 490195801Smav &rid, RF_SHAREABLE | RF_ACTIVE))) { 491195801Smav device_printf(dev, "Unable to map interrupt\n"); 492208816Smav error = ENXIO; 493208816Smav goto err0; 494195801Smav } 495195801Smav if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 496195801Smav siis_ch_intr_locked, dev, &ch->ih))) { 497195801Smav device_printf(dev, "Unable to setup interrupt\n"); 498195801Smav error = ENXIO; 499195801Smav goto err1; 500195801Smav } 501195801Smav /* Create the device queue for our SIM. */ 502195801Smav devq = cam_simq_alloc(SIIS_MAX_SLOTS); 503195801Smav if (devq == NULL) { 504195801Smav device_printf(dev, "Unable to allocate simq\n"); 505195801Smav error = ENOMEM; 506195801Smav goto err1; 507195801Smav } 508195801Smav /* Construct SIM entry */ 509195801Smav ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 510199178Smav device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq); 511195801Smav if (ch->sim == NULL) { 512208816Smav cam_simq_free(devq); 513195801Smav device_printf(dev, "unable to allocate sim\n"); 514195801Smav error = ENOMEM; 515208816Smav goto err1; 516195801Smav } 517195801Smav if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 518195801Smav device_printf(dev, "unable to register xpt bus\n"); 519195801Smav error = ENXIO; 520195801Smav goto err2; 521195801Smav } 522195801Smav if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 523195801Smav CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 524195801Smav device_printf(dev, "unable to create path\n"); 525195801Smav error = ENXIO; 526195801Smav goto err3; 527195801Smav } 528195801Smav mtx_unlock(&ch->mtx); 529217877Smav ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev)); 530195801Smav return (0); 531195801Smav 532195801Smaverr3: 533195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 534195801Smaverr2: 535195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 536195801Smaverr1: 537195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 538208816Smaverr0: 539195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 540195801Smav mtx_unlock(&ch->mtx); 541214325Smav mtx_destroy(&ch->mtx); 542195801Smav return (error); 543195801Smav} 544195801Smav 545195801Smavstatic int 546195801Smavsiis_ch_detach(device_t dev) 547195801Smav{ 548195801Smav struct siis_channel *ch = device_get_softc(dev); 549195801Smav 550217877Smav led_destroy(ch->led); 551195801Smav mtx_lock(&ch->mtx); 552195801Smav xpt_async(AC_LOST_DEVICE, ch->path, NULL); 553195801Smav xpt_free_path(ch->path); 554195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 555195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 556195801Smav mtx_unlock(&ch->mtx); 557195801Smav 558195801Smav bus_teardown_intr(dev, ch->r_irq, ch->ih); 559195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 560195801Smav 561208393Smav siis_ch_deinit(dev); 562195801Smav siis_slotsfree(dev); 563195801Smav siis_dmafini(dev); 564195801Smav 565195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 566195801Smav mtx_destroy(&ch->mtx); 567195801Smav return (0); 568195801Smav} 569195801Smav 570195801Smavstatic int 571208393Smavsiis_ch_init(device_t dev) 572195801Smav{ 573195801Smav struct siis_channel *ch = device_get_softc(dev); 574195801Smav 575208393Smav /* Get port out of reset state. */ 576208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 577208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 578208393Smav if (ch->pm_present) 579208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 580208393Smav else 581208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 582208393Smav /* Enable port interrupts */ 583208393Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 584208393Smav return (0); 585208393Smav} 586208393Smav 587208393Smavstatic int 588208393Smavsiis_ch_deinit(device_t dev) 589208393Smav{ 590208393Smav struct siis_channel *ch = device_get_softc(dev); 591208393Smav 592195801Smav /* Put port into reset state. */ 593195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 594195801Smav return (0); 595195801Smav} 596195801Smav 597195801Smavstatic int 598208393Smavsiis_ch_suspend(device_t dev) 599208393Smav{ 600208393Smav struct siis_channel *ch = device_get_softc(dev); 601208393Smav 602208393Smav mtx_lock(&ch->mtx); 603208393Smav xpt_freeze_simq(ch->sim, 1); 604208393Smav while (ch->oslots) 605208393Smav msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100); 606208393Smav siis_ch_deinit(dev); 607208393Smav mtx_unlock(&ch->mtx); 608208393Smav return (0); 609208393Smav} 610208393Smav 611208393Smavstatic int 612195801Smavsiis_ch_resume(device_t dev) 613195801Smav{ 614195801Smav struct siis_channel *ch = device_get_softc(dev); 615195801Smav 616208393Smav mtx_lock(&ch->mtx); 617208393Smav siis_ch_init(dev); 618208393Smav siis_reset(dev); 619208393Smav xpt_release_simq(ch->sim, TRUE); 620208393Smav mtx_unlock(&ch->mtx); 621195801Smav return (0); 622195801Smav} 623195801Smav 624195801Smavdevclass_t siisch_devclass; 625195801Smavstatic device_method_t siisch_methods[] = { 626195801Smav DEVMETHOD(device_probe, siis_ch_probe), 627195801Smav DEVMETHOD(device_attach, siis_ch_attach), 628195801Smav DEVMETHOD(device_detach, siis_ch_detach), 629195801Smav DEVMETHOD(device_suspend, siis_ch_suspend), 630195801Smav DEVMETHOD(device_resume, siis_ch_resume), 631195801Smav { 0, 0 } 632195801Smav}; 633195801Smavstatic driver_t siisch_driver = { 634195801Smav "siisch", 635195801Smav siisch_methods, 636195801Smav sizeof(struct siis_channel) 637195801Smav}; 638195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 639195801Smav 640217877Smavstatic void 641217877Smavsiis_ch_led(void *priv, int onoff) 642217877Smav{ 643217877Smav device_t dev; 644217877Smav struct siis_channel *ch; 645217877Smav 646217877Smav dev = (device_t)priv; 647217877Smav ch = device_get_softc(dev); 648217877Smav 649217877Smav if (onoff == 0) 650217877Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON); 651217877Smav else 652217877Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON); 653217877Smav} 654217877Smav 655195801Smavstruct siis_dc_cb_args { 656195801Smav bus_addr_t maddr; 657195801Smav int error; 658195801Smav}; 659195801Smav 660195801Smavstatic void 661195801Smavsiis_dmainit(device_t dev) 662195801Smav{ 663195801Smav struct siis_channel *ch = device_get_softc(dev); 664195801Smav struct siis_dc_cb_args dcba; 665195801Smav 666195801Smav /* Command area. */ 667195801Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 668195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 669195801Smav NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 670195801Smav 0, NULL, NULL, &ch->dma.work_tag)) 671195801Smav goto error; 672195801Smav if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 673195801Smav &ch->dma.work_map)) 674195801Smav goto error; 675195801Smav if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 676195801Smav SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 677195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 678195801Smav goto error; 679195801Smav } 680195801Smav ch->dma.work_bus = dcba.maddr; 681195801Smav /* Data area. */ 682199333Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 683195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 684195801Smav NULL, NULL, 685195801Smav SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 686195801Smav SIIS_SG_ENTRIES, 0xFFFFFFFF, 687195801Smav 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 688195801Smav goto error; 689195801Smav } 690195801Smav return; 691195801Smav 692195801Smaverror: 693195801Smav device_printf(dev, "WARNING - DMA initialization failed\n"); 694195801Smav siis_dmafini(dev); 695195801Smav} 696195801Smav 697195801Smavstatic void 698195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 699195801Smav{ 700195801Smav struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 701195801Smav 702195801Smav if (!(dcba->error = error)) 703195801Smav dcba->maddr = segs[0].ds_addr; 704195801Smav} 705195801Smav 706195801Smavstatic void 707195801Smavsiis_dmafini(device_t dev) 708195801Smav{ 709195801Smav struct siis_channel *ch = device_get_softc(dev); 710195801Smav 711195801Smav if (ch->dma.data_tag) { 712195801Smav bus_dma_tag_destroy(ch->dma.data_tag); 713195801Smav ch->dma.data_tag = NULL; 714195801Smav } 715195801Smav if (ch->dma.work_bus) { 716195801Smav bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 717195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 718195801Smav ch->dma.work_bus = 0; 719195801Smav ch->dma.work_map = NULL; 720195801Smav ch->dma.work = NULL; 721195801Smav } 722195801Smav if (ch->dma.work_tag) { 723195801Smav bus_dma_tag_destroy(ch->dma.work_tag); 724195801Smav ch->dma.work_tag = NULL; 725195801Smav } 726195801Smav} 727195801Smav 728195801Smavstatic void 729195801Smavsiis_slotsalloc(device_t dev) 730195801Smav{ 731195801Smav struct siis_channel *ch = device_get_softc(dev); 732195801Smav int i; 733195801Smav 734195801Smav /* Alloc and setup command/dma slots */ 735195801Smav bzero(ch->slot, sizeof(ch->slot)); 736195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 737195801Smav struct siis_slot *slot = &ch->slot[i]; 738195801Smav 739195801Smav slot->dev = dev; 740195801Smav slot->slot = i; 741195801Smav slot->state = SIIS_SLOT_EMPTY; 742195801Smav slot->ccb = NULL; 743195801Smav callout_init_mtx(&slot->timeout, &ch->mtx, 0); 744195801Smav 745195801Smav if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 746195801Smav device_printf(ch->dev, "FAILURE - create data_map\n"); 747195801Smav } 748195801Smav} 749195801Smav 750195801Smavstatic void 751195801Smavsiis_slotsfree(device_t dev) 752195801Smav{ 753195801Smav struct siis_channel *ch = device_get_softc(dev); 754195801Smav int i; 755195801Smav 756195801Smav /* Free all dma slots */ 757195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 758195801Smav struct siis_slot *slot = &ch->slot[i]; 759195801Smav 760198896Smav callout_drain(&slot->timeout); 761195801Smav if (slot->dma.data_map) { 762195801Smav bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 763195801Smav slot->dma.data_map = NULL; 764195801Smav } 765195801Smav } 766195801Smav} 767195801Smav 768195801Smavstatic void 769196655Smavsiis_notify_events(device_t dev) 770196655Smav{ 771196655Smav struct siis_channel *ch = device_get_softc(dev); 772196655Smav struct cam_path *dpath; 773196655Smav u_int32_t status; 774196655Smav int i; 775196655Smav 776200217Smav if (ch->quirks & SIIS_Q_SNTF) { 777200217Smav status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 778200217Smav ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 779200217Smav } else { 780200217Smav /* 781200217Smav * Without SNTF we have no idea which device sent notification. 782200217Smav * If PMP is connected, assume it, else - device. 783200217Smav */ 784200217Smav status = (ch->pm_present) ? 0x8000 : 0x0001; 785200217Smav } 786196655Smav if (bootverbose) 787196655Smav device_printf(dev, "SNTF 0x%04x\n", status); 788196655Smav for (i = 0; i < 16; i++) { 789196655Smav if ((status & (1 << i)) == 0) 790196655Smav continue; 791196655Smav if (xpt_create_path(&dpath, NULL, 792196655Smav xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 793196655Smav xpt_async(AC_SCSI_AEN, dpath, NULL); 794196655Smav xpt_free_path(dpath); 795196655Smav } 796196655Smav } 797196655Smav 798196655Smav} 799196655Smav 800196655Smavstatic void 801195801Smavsiis_phy_check_events(device_t dev) 802195801Smav{ 803195801Smav struct siis_channel *ch = device_get_softc(dev); 804195801Smav 805195801Smav /* If we have a connection event, deal with it */ 806195801Smav if (ch->pm_level == 0) { 807195801Smav u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 808203108Smav union ccb *ccb; 809203108Smav 810203165Smav if (bootverbose) { 811203165Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 812203165Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 813203165Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 814195801Smav device_printf(dev, "CONNECT requested\n"); 815203165Smav } else 816195801Smav device_printf(dev, "DISCONNECT requested\n"); 817195801Smav } 818203165Smav siis_reset(dev); 819203108Smav if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 820203108Smav return; 821203108Smav if (xpt_create_path(&ccb->ccb_h.path, NULL, 822203108Smav cam_sim_path(ch->sim), 823203108Smav CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 824203108Smav xpt_free_ccb(ccb); 825203108Smav return; 826203108Smav } 827203108Smav xpt_rescan(ccb); 828195801Smav } 829195801Smav} 830195801Smav 831195801Smavstatic void 832195801Smavsiis_ch_intr_locked(void *data) 833195801Smav{ 834195801Smav device_t dev = (device_t)data; 835195801Smav struct siis_channel *ch = device_get_softc(dev); 836195801Smav 837195801Smav mtx_lock(&ch->mtx); 838195801Smav siis_ch_intr(data); 839195801Smav mtx_unlock(&ch->mtx); 840195801Smav} 841195801Smav 842195801Smavstatic void 843195801Smavsiis_ch_intr(void *data) 844195801Smav{ 845195801Smav device_t dev = (device_t)data; 846195801Smav struct siis_channel *ch = device_get_softc(dev); 847195801Smav uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 848195801Smav enum siis_err_type et; 849195801Smav int i, ccs, port, tslots; 850195801Smav 851195801Smav mtx_assert(&ch->mtx, MA_OWNED); 852195801Smav /* Read command statuses. */ 853195801Smav sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 854195801Smav ok = ch->rslots & ~sstatus; 855195801Smav /* Complete all successfull commands. */ 856195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 857195801Smav if ((ok >> i) & 1) 858195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 859195801Smav } 860195801Smav /* Do we have any other events? */ 861195801Smav if ((sstatus & SIIS_P_SS_ATTN) == 0) 862195801Smav return; 863195801Smav /* Read and clear interrupt statuses. */ 864195801Smav istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 865195801Smav (0xFFFF & ~SIIS_P_IX_COMMCOMP); 866195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 867195801Smav /* Process PHY events */ 868195801Smav if (istatus & SIIS_P_IX_PHYRDYCHG) 869195801Smav siis_phy_check_events(dev); 870196655Smav /* Process NOTIFY events */ 871196655Smav if (istatus & SIIS_P_IX_SDBN) 872196655Smav siis_notify_events(dev); 873195801Smav /* Process command errors */ 874195801Smav if (istatus & SIIS_P_IX_COMMERR) { 875195801Smav estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 876195801Smav ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 877195801Smav ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 878195801Smav port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 879195801Smav err = ch->rslots & sstatus; 880195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 881195801Smav// __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 882195801Smav// ATA_INL(ch->r_mem, SIIS_P_SERR)); 883195801Smav 884220566Smav if (!ch->recoverycmd && !ch->recovery) { 885195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 886195801Smav ch->recovery = 1; 887195801Smav } 888195801Smav if (ch->frozen) { 889195801Smav union ccb *fccb = ch->frozen; 890195801Smav ch->frozen = NULL; 891198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 892198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 893198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 894198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 895198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 896198321Smav } 897195801Smav xpt_done(fccb); 898195801Smav } 899195801Smav if (estatus == SIIS_P_CMDERR_DEV || 900195801Smav estatus == SIIS_P_CMDERR_SDB || 901195801Smav estatus == SIIS_P_CMDERR_DATAFIS) { 902195801Smav tslots = ch->numtslots[port]; 903195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 904198852Smav /* XXX: requests in loading state. */ 905195801Smav if (((ch->rslots >> i) & 1) == 0) 906195801Smav continue; 907195801Smav if (ch->slot[i].ccb->ccb_h.target_id != port) 908195801Smav continue; 909195801Smav if (tslots == 0) { 910195801Smav /* Untagged operation. */ 911195801Smav if (i == ccs) 912195801Smav et = SIIS_ERR_TFE; 913195801Smav else 914195801Smav et = SIIS_ERR_INNOCENT; 915195801Smav } else { 916195801Smav /* Tagged operation. */ 917195801Smav et = SIIS_ERR_NCQ; 918195801Smav } 919195801Smav siis_end_transaction(&ch->slot[i], et); 920195801Smav } 921195801Smav /* 922195801Smav * We can't reinit port if there are some other 923195801Smav * commands active, use resume to complete them. 924195801Smav */ 925220566Smav if (ch->rslots != 0 && !ch->recoverycmd) 926195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 927195801Smav } else { 928195801Smav if (estatus == SIIS_P_CMDERR_SENDFIS || 929195801Smav estatus == SIIS_P_CMDERR_INCSTATE || 930195801Smav estatus == SIIS_P_CMDERR_PPE || 931195801Smav estatus == SIIS_P_CMDERR_SERVICE) { 932195801Smav et = SIIS_ERR_SATA; 933195801Smav } else 934195801Smav et = SIIS_ERR_INVALID; 935195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 936198852Smav /* XXX: requests in loading state. */ 937195801Smav if (((ch->rslots >> i) & 1) == 0) 938195801Smav continue; 939195801Smav siis_end_transaction(&ch->slot[i], et); 940195801Smav } 941195801Smav } 942195801Smav } 943195801Smav} 944195801Smav 945195801Smav/* Must be called with channel locked. */ 946195801Smavstatic int 947195801Smavsiis_check_collision(device_t dev, union ccb *ccb) 948195801Smav{ 949195801Smav struct siis_channel *ch = device_get_softc(dev); 950195801Smav 951195801Smav mtx_assert(&ch->mtx, MA_OWNED); 952195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 953199747Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 954199747Smav /* Tagged command while we have no supported tag free. */ 955199747Smav if (((~ch->oslots) & (0x7fffffff >> (31 - 956199747Smav ch->curr[ccb->ccb_h.target_id].tags))) == 0) 957199747Smav return (1); 958199747Smav } 959199747Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 960195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 961195801Smav /* Atomic command while anything active. */ 962195801Smav if (ch->numrslots != 0) 963195801Smav return (1); 964195801Smav } 965195801Smav /* We have some atomic command running. */ 966195801Smav if (ch->aslots != 0) 967195801Smav return (1); 968195801Smav return (0); 969195801Smav} 970195801Smav 971195801Smav/* Must be called with channel locked. */ 972195801Smavstatic void 973195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb) 974195801Smav{ 975195801Smav struct siis_channel *ch = device_get_softc(dev); 976195801Smav struct siis_slot *slot; 977199747Smav int tag, tags; 978195801Smav 979195801Smav mtx_assert(&ch->mtx, MA_OWNED); 980195801Smav /* Choose empty slot. */ 981199747Smav tags = SIIS_MAX_SLOTS; 982199747Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 983199747Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) 984199747Smav tags = ch->curr[ccb->ccb_h.target_id].tags; 985199747Smav tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1; 986195801Smav /* Occupy chosen slot. */ 987195801Smav slot = &ch->slot[tag]; 988195801Smav slot->ccb = ccb; 989195801Smav /* Update channel stats. */ 990199747Smav ch->oslots |= (1 << slot->slot); 991195801Smav ch->numrslots++; 992195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 993195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 994195801Smav ch->numtslots[ccb->ccb_h.target_id]++; 995195801Smav } 996195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 997195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 998195801Smav ch->aslots |= (1 << slot->slot); 999195801Smav slot->dma.nsegs = 0; 1000195801Smav /* If request moves data, setup and load SG list */ 1001195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1002195801Smav void *buf; 1003195801Smav bus_size_t size; 1004195801Smav 1005195801Smav slot->state = SIIS_SLOT_LOADING; 1006195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1007195801Smav buf = ccb->ataio.data_ptr; 1008195801Smav size = ccb->ataio.dxfer_len; 1009195801Smav } else { 1010195801Smav buf = ccb->csio.data_ptr; 1011195801Smav size = ccb->csio.dxfer_len; 1012195801Smav } 1013195801Smav bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 1014195801Smav buf, size, siis_dmasetprd, slot, 0); 1015195801Smav } else 1016195801Smav siis_execute_transaction(slot); 1017195801Smav} 1018195801Smav 1019195801Smav/* Locked by busdma engine. */ 1020195801Smavstatic void 1021195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1022195801Smav{ 1023195801Smav struct siis_slot *slot = arg; 1024195801Smav struct siis_channel *ch = device_get_softc(slot->dev); 1025195801Smav struct siis_cmd *ctp; 1026195801Smav struct siis_dma_prd *prd; 1027195801Smav int i; 1028195801Smav 1029195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1030195801Smav if (error) { 1031195801Smav device_printf(slot->dev, "DMA load error\n"); 1032220566Smav if (!ch->recoverycmd) 1033195801Smav xpt_freeze_simq(ch->sim, 1); 1034195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 1035195801Smav return; 1036195801Smav } 1037195801Smav KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 1038195801Smav /* Get a piece of the workspace for this request */ 1039195801Smav ctp = (struct siis_cmd *) 1040195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1041195801Smav /* Fill S/G table */ 1042195801Smav if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 1043195801Smav prd = &ctp->u.ata.prd[0]; 1044195801Smav else 1045195801Smav prd = &ctp->u.atapi.prd[0]; 1046195801Smav for (i = 0; i < nsegs; i++) { 1047195801Smav prd[i].dba = htole64(segs[i].ds_addr); 1048195801Smav prd[i].dbc = htole32(segs[i].ds_len); 1049195801Smav prd[i].control = 0; 1050195801Smav } 1051195801Smav prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 1052195801Smav slot->dma.nsegs = nsegs; 1053195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1054195801Smav ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1055195801Smav BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1056195801Smav siis_execute_transaction(slot); 1057195801Smav} 1058195801Smav 1059195801Smav/* Must be called with channel locked. */ 1060195801Smavstatic void 1061195801Smavsiis_execute_transaction(struct siis_slot *slot) 1062195801Smav{ 1063195801Smav device_t dev = slot->dev; 1064195801Smav struct siis_channel *ch = device_get_softc(dev); 1065195801Smav struct siis_cmd *ctp; 1066195801Smav union ccb *ccb = slot->ccb; 1067195801Smav u_int64_t prb_bus; 1068195801Smav 1069195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1070195801Smav /* Get a piece of the workspace for this request */ 1071195801Smav ctp = (struct siis_cmd *) 1072195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1073195801Smav ctp->control = 0; 1074195801Smav ctp->protocol_override = 0; 1075195801Smav ctp->transfer_count = 0; 1076195801Smav /* Special handling for Soft Reset command. */ 1077201222Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1078201222Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) { 1079201222Smav ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 1080201222Smav } else { 1081201222Smav ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE); 1082201222Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1083201222Smav ctp->protocol_override |= 1084201222Smav htole16(SIIS_PRB_PROTO_NCQ); 1085201222Smav } 1086201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1087201222Smav ctp->protocol_override |= 1088201222Smav htole16(SIIS_PRB_PROTO_READ); 1089201222Smav } else 1090201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1091201222Smav ctp->protocol_override |= 1092201222Smav htole16(SIIS_PRB_PROTO_WRITE); 1093201222Smav } 1094201222Smav } 1095195801Smav } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1096201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1097195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_READ); 1098201222Smav else 1099201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1100195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 1101195801Smav } 1102203108Smav /* Special handling for Soft Reset command. */ 1103203108Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1104203108Smav (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1105203108Smav (ccb->ataio.cmd.control & ATA_A_RESET)) { 1106203108Smav /* Kick controller into sane state */ 1107203108Smav siis_portinit(dev); 1108203108Smav } 1109195801Smav /* Setup the FIS for this request */ 1110199821Smav if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) { 1111195801Smav device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1112220566Smav if (!ch->recoverycmd) 1113195801Smav xpt_freeze_simq(ch->sim, 1); 1114195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 1115195801Smav return; 1116195801Smav } 1117195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1118195801Smav BUS_DMASYNC_PREWRITE); 1119195801Smav /* Issue command to the controller. */ 1120195801Smav slot->state = SIIS_SLOT_RUNNING; 1121195801Smav ch->rslots |= (1 << slot->slot); 1122195801Smav prb_bus = ch->dma.work_bus + 1123195801Smav SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 1124195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 1125195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 1126195801Smav /* Start command execution timeout */ 1127195801Smav callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000, 1128195801Smav (timeout_t*)siis_timeout, slot); 1129195801Smav return; 1130195801Smav} 1131195801Smav 1132198852Smav/* Must be called with channel locked. */ 1133195801Smavstatic void 1134198852Smavsiis_process_timeout(device_t dev) 1135195801Smav{ 1136195801Smav struct siis_channel *ch = device_get_softc(dev); 1137195801Smav int i; 1138195801Smav 1139195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1140220566Smav if (!ch->recoverycmd && !ch->recovery) { 1141195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 1142198852Smav ch->recovery = 1; 1143195801Smav } 1144197838Smav /* Handle the rest of commands. */ 1145195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1146195801Smav /* Do we have a running request on slot? */ 1147195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1148195801Smav continue; 1149198852Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT); 1150195801Smav } 1151195801Smav} 1152195801Smav 1153203870Smav/* Must be called with channel locked. */ 1154203870Smavstatic void 1155203870Smavsiis_rearm_timeout(device_t dev) 1156203870Smav{ 1157203870Smav struct siis_channel *ch = device_get_softc(dev); 1158203870Smav int i; 1159203870Smav 1160203870Smav mtx_assert(&ch->mtx, MA_OWNED); 1161203870Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1162203870Smav struct siis_slot *slot = &ch->slot[i]; 1163203870Smav 1164203870Smav /* Do we have a running request on slot? */ 1165203870Smav if (slot->state < SIIS_SLOT_RUNNING) 1166203870Smav continue; 1167203870Smav if ((ch->toslots & (1 << i)) == 0) 1168203870Smav continue; 1169203870Smav callout_reset(&slot->timeout, 1170203870Smav (int)slot->ccb->ccb_h.timeout * hz / 1000, 1171203870Smav (timeout_t*)siis_timeout, slot); 1172203870Smav } 1173203870Smav} 1174203870Smav 1175198852Smav/* Locked by callout mechanism. */ 1176198852Smavstatic void 1177198852Smavsiis_timeout(struct siis_slot *slot) 1178198852Smav{ 1179198852Smav device_t dev = slot->dev; 1180198852Smav struct siis_channel *ch = device_get_softc(dev); 1181198852Smav 1182198852Smav mtx_assert(&ch->mtx, MA_OWNED); 1183198896Smav /* Check for stale timeout. */ 1184198896Smav if (slot->state < SIIS_SLOT_RUNNING) 1185198896Smav return; 1186198852Smav device_printf(dev, "Timeout on slot %d\n", slot->slot); 1187203108Smav device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 1188203108Smav __func__, ATA_INL(ch->r_mem, SIIS_P_IS), 1189203108Smav ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 1190203108Smav ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 1191203108Smav ATA_INL(ch->r_mem, SIIS_P_SERR)); 1192198852Smav 1193198852Smav if (ch->toslots == 0) 1194198852Smav xpt_freeze_simq(ch->sim, 1); 1195198852Smav ch->toslots |= (1 << slot->slot); 1196198852Smav if ((ch->rslots & ~ch->toslots) == 0) 1197198852Smav siis_process_timeout(dev); 1198198852Smav else 1199198852Smav device_printf(dev, " ... waiting for slots %08x\n", 1200198852Smav ch->rslots & ~ch->toslots); 1201198852Smav} 1202198852Smav 1203195801Smav/* Must be called with channel locked. */ 1204195801Smavstatic void 1205195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 1206195801Smav{ 1207195801Smav device_t dev = slot->dev; 1208195801Smav struct siis_channel *ch = device_get_softc(dev); 1209195801Smav union ccb *ccb = slot->ccb; 1210212732Smav int lastto; 1211195801Smav 1212195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1213195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1214195801Smav BUS_DMASYNC_POSTWRITE); 1215195801Smav /* Read result registers to the result struct 1216195801Smav * May be incorrect if several commands finished same time, 1217195801Smav * so read only when sure or have to. 1218195801Smav */ 1219195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1220195801Smav struct ata_res *res = &ccb->ataio.res; 1221195801Smav if ((et == SIIS_ERR_TFE) || 1222195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1223195801Smav int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 1224195801Smav 1225195801Smav res->status = ATA_INB(ch->r_mem, offs + 2); 1226195801Smav res->error = ATA_INB(ch->r_mem, offs + 3); 1227195801Smav res->lba_low = ATA_INB(ch->r_mem, offs + 4); 1228195801Smav res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 1229195801Smav res->lba_high = ATA_INB(ch->r_mem, offs + 6); 1230195801Smav res->device = ATA_INB(ch->r_mem, offs + 7); 1231195801Smav res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 1232195801Smav res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 1233195801Smav res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 1234195801Smav res->sector_count = ATA_INB(ch->r_mem, offs + 12); 1235195801Smav res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 1236195801Smav } else 1237195801Smav bzero(res, sizeof(*res)); 1238214988Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1239214988Smav ch->numrslots == 1) { 1240214988Smav ccb->ataio.resid = ccb->ataio.dxfer_len - 1241214988Smav ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1242214988Smav } 1243214988Smav } else { 1244214988Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1245214988Smav ch->numrslots == 1) { 1246214988Smav ccb->csio.resid = ccb->csio.dxfer_len - 1247214988Smav ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1248214988Smav } 1249195801Smav } 1250195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1251195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1252195801Smav (ccb->ccb_h.flags & CAM_DIR_IN) ? 1253195801Smav BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1254195801Smav bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1255195801Smav } 1256195801Smav /* Set proper result status. */ 1257195801Smav if (et != SIIS_ERR_NONE || ch->recovery) { 1258195801Smav ch->eslots |= (1 << slot->slot); 1259195801Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1260195801Smav } 1261198321Smav /* In case of error, freeze device for proper recovery. */ 1262220566Smav if (et != SIIS_ERR_NONE && (!ch->recoverycmd) && 1263198321Smav !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1264198321Smav xpt_freeze_devq(ccb->ccb_h.path, 1); 1265198321Smav ccb->ccb_h.status |= CAM_DEV_QFRZN; 1266198321Smav } 1267198321Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1268195801Smav switch (et) { 1269195801Smav case SIIS_ERR_NONE: 1270195801Smav ccb->ccb_h.status |= CAM_REQ_CMP; 1271195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1272195801Smav ccb->csio.scsi_status = SCSI_STATUS_OK; 1273195801Smav break; 1274195801Smav case SIIS_ERR_INVALID: 1275198852Smav ch->fatalerr = 1; 1276195801Smav ccb->ccb_h.status |= CAM_REQ_INVALID; 1277195801Smav break; 1278195801Smav case SIIS_ERR_INNOCENT: 1279195801Smav ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1280195801Smav break; 1281195801Smav case SIIS_ERR_TFE: 1282198321Smav case SIIS_ERR_NCQ: 1283195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1284195801Smav ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1285195801Smav ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1286195801Smav } else { 1287195801Smav ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1288195801Smav } 1289195801Smav break; 1290195801Smav case SIIS_ERR_SATA: 1291198852Smav ch->fatalerr = 1; 1292195801Smav ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1293195801Smav break; 1294195801Smav case SIIS_ERR_TIMEOUT: 1295198852Smav ch->fatalerr = 1; 1296195801Smav ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1297195801Smav break; 1298195801Smav default: 1299195801Smav ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1300195801Smav } 1301195801Smav /* Free slot. */ 1302199747Smav ch->oslots &= ~(1 << slot->slot); 1303195801Smav ch->rslots &= ~(1 << slot->slot); 1304195801Smav ch->aslots &= ~(1 << slot->slot); 1305195801Smav slot->state = SIIS_SLOT_EMPTY; 1306195801Smav slot->ccb = NULL; 1307195801Smav /* Update channel stats. */ 1308195801Smav ch->numrslots--; 1309195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1310195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1311195801Smav ch->numtslots[ccb->ccb_h.target_id]--; 1312195801Smav } 1313212732Smav /* Cancel timeout state if request completed normally. */ 1314212732Smav if (et != SIIS_ERR_TIMEOUT) { 1315212732Smav lastto = (ch->toslots == (1 << slot->slot)); 1316212732Smav ch->toslots &= ~(1 << slot->slot); 1317212732Smav if (lastto) 1318212732Smav xpt_release_simq(ch->sim, TRUE); 1319212732Smav } 1320198852Smav /* If it was our READ LOG command - process it. */ 1321220566Smav if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 1322198852Smav siis_process_read_log(dev, ccb); 1323220566Smav /* If it was our REQUEST SENSE command - process it. */ 1324220566Smav } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 1325220566Smav siis_process_request_sense(dev, ccb); 1326220566Smav /* If it was NCQ or ATAPI command error, put result on hold. */ 1327220566Smav } else if (et == SIIS_ERR_NCQ || 1328220566Smav ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 1329220566Smav (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 1330195801Smav ch->hold[slot->slot] = ccb; 1331195801Smav ch->numhslots++; 1332198852Smav } else 1333195801Smav xpt_done(ccb); 1334195801Smav /* Unfreeze frozen command. */ 1335199747Smav if (ch->frozen && !siis_check_collision(dev, ch->frozen)) { 1336195801Smav union ccb *fccb = ch->frozen; 1337195801Smav ch->frozen = NULL; 1338195801Smav siis_begin_transaction(dev, fccb); 1339195801Smav xpt_release_simq(ch->sim, TRUE); 1340195801Smav } 1341195801Smav /* If we have no other active commands, ... */ 1342195801Smav if (ch->rslots == 0) { 1343198852Smav /* if there were timeouts or fatal error - reset port. */ 1344198852Smav if (ch->toslots != 0 || ch->fatalerr) { 1345198852Smav siis_reset(dev); 1346198852Smav } else { 1347198852Smav /* if we have slots in error, we can reinit port. */ 1348198852Smav if (ch->eslots != 0) 1349198852Smav siis_portinit(dev); 1350220566Smav /* if there commands on hold, we can do recovery. */ 1351220566Smav if (!ch->recoverycmd && ch->numhslots) 1352220566Smav siis_issue_recovery(dev); 1353198852Smav } 1354198852Smav /* If all the reset of commands are in timeout - abort them. */ 1355203870Smav } else if ((ch->rslots & ~ch->toslots) == 0 && 1356203870Smav et != SIIS_ERR_TIMEOUT) 1357203870Smav siis_rearm_timeout(dev); 1358195801Smav} 1359195801Smav 1360195801Smavstatic void 1361220566Smavsiis_issue_recovery(device_t dev) 1362195801Smav{ 1363195801Smav struct siis_channel *ch = device_get_softc(dev); 1364195801Smav union ccb *ccb; 1365195801Smav struct ccb_ataio *ataio; 1366220566Smav struct ccb_scsiio *csio; 1367195801Smav int i; 1368195801Smav 1369220830Smav /* Find some held command. */ 1370195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1371195801Smav if (ch->hold[i]) 1372195801Smav break; 1373195801Smav } 1374195801Smav if (i == SIIS_MAX_SLOTS) 1375195801Smav return; 1376195801Smav ccb = xpt_alloc_ccb_nowait(); 1377195801Smav if (ccb == NULL) { 1378220830Smav device_printf(dev, "Unable to allocate recovery command\n"); 1379220822Smavcompleteall: 1380220830Smav /* We can't do anything -- complete held commands. */ 1381220822Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1382220822Smav if (ch->hold[i] == NULL) 1383220822Smav continue; 1384220822Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1385220822Smav ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL; 1386220822Smav xpt_done(ch->hold[i]); 1387220822Smav ch->hold[i] = NULL; 1388220822Smav ch->numhslots--; 1389220822Smav } 1390220822Smav siis_reset(dev); 1391220822Smav return; 1392195801Smav } 1393195801Smav ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1394220566Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1395220566Smav /* READ LOG */ 1396220566Smav ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 1397220566Smav ccb->ccb_h.func_code = XPT_ATA_IO; 1398220566Smav ccb->ccb_h.flags = CAM_DIR_IN; 1399220566Smav ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1400220566Smav ataio = &ccb->ataio; 1401220566Smav ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 1402220566Smav if (ataio->data_ptr == NULL) { 1403220566Smav xpt_free_ccb(ccb); 1404220822Smav device_printf(dev, 1405220830Smav "Unable to allocate memory for READ LOG command\n"); 1406220822Smav goto completeall; 1407220566Smav } 1408220566Smav ataio->dxfer_len = 512; 1409220566Smav bzero(&ataio->cmd, sizeof(ataio->cmd)); 1410220566Smav ataio->cmd.flags = CAM_ATAIO_48BIT; 1411220566Smav ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1412220566Smav ataio->cmd.sector_count = 1; 1413220566Smav ataio->cmd.sector_count_exp = 0; 1414220566Smav ataio->cmd.lba_low = 0x10; 1415220566Smav ataio->cmd.lba_mid = 0; 1416220566Smav ataio->cmd.lba_mid_exp = 0; 1417220566Smav } else { 1418220566Smav /* REQUEST SENSE */ 1419220566Smav ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 1420220566Smav ccb->ccb_h.recovery_slot = i; 1421220566Smav ccb->ccb_h.func_code = XPT_SCSI_IO; 1422220566Smav ccb->ccb_h.flags = CAM_DIR_IN; 1423220566Smav ccb->ccb_h.status = 0; 1424220566Smav ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1425220566Smav csio = &ccb->csio; 1426220566Smav csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 1427220566Smav csio->dxfer_len = ch->hold[i]->csio.sense_len; 1428220566Smav csio->cdb_len = 6; 1429220566Smav bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 1430220566Smav csio->cdb_io.cdb_bytes[0] = 0x03; 1431220566Smav csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 1432195801Smav } 1433220822Smav ch->recoverycmd = 1; 1434195801Smav siis_begin_transaction(dev, ccb); 1435195801Smav} 1436195801Smav 1437195801Smavstatic void 1438195801Smavsiis_process_read_log(device_t dev, union ccb *ccb) 1439195801Smav{ 1440195801Smav struct siis_channel *ch = device_get_softc(dev); 1441195801Smav uint8_t *data; 1442195801Smav struct ata_res *res; 1443195801Smav int i; 1444195801Smav 1445220566Smav ch->recoverycmd = 0; 1446195801Smav data = ccb->ataio.data_ptr; 1447195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1448195801Smav (data[0] & 0x80) == 0) { 1449195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1450195801Smav if (!ch->hold[i]) 1451195801Smav continue; 1452195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1453195801Smav continue; 1454195801Smav if ((data[0] & 0x1F) == i) { 1455195801Smav res = &ch->hold[i]->ataio.res; 1456195801Smav res->status = data[2]; 1457195801Smav res->error = data[3]; 1458195801Smav res->lba_low = data[4]; 1459195801Smav res->lba_mid = data[5]; 1460195801Smav res->lba_high = data[6]; 1461195801Smav res->device = data[7]; 1462195801Smav res->lba_low_exp = data[8]; 1463195801Smav res->lba_mid_exp = data[9]; 1464195801Smav res->lba_high_exp = data[10]; 1465195801Smav res->sector_count = data[12]; 1466195801Smav res->sector_count_exp = data[13]; 1467195801Smav } else { 1468195801Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1469195801Smav ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1470195801Smav } 1471195801Smav xpt_done(ch->hold[i]); 1472195801Smav ch->hold[i] = NULL; 1473195801Smav ch->numhslots--; 1474195801Smav } 1475195801Smav } else { 1476195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1477195801Smav device_printf(dev, "Error while READ LOG EXT\n"); 1478195801Smav else if ((data[0] & 0x80) == 0) { 1479195801Smav device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1480195801Smav } 1481195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1482195801Smav if (!ch->hold[i]) 1483195801Smav continue; 1484195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1485195801Smav continue; 1486195801Smav xpt_done(ch->hold[i]); 1487195801Smav ch->hold[i] = NULL; 1488195801Smav ch->numhslots--; 1489195801Smav } 1490195801Smav } 1491195801Smav free(ccb->ataio.data_ptr, M_SIIS); 1492195801Smav xpt_free_ccb(ccb); 1493195801Smav} 1494195801Smav 1495195801Smavstatic void 1496220566Smavsiis_process_request_sense(device_t dev, union ccb *ccb) 1497220566Smav{ 1498220566Smav struct siis_channel *ch = device_get_softc(dev); 1499220566Smav int i; 1500220566Smav 1501220566Smav ch->recoverycmd = 0; 1502220566Smav 1503220566Smav i = ccb->ccb_h.recovery_slot; 1504220566Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1505220566Smav ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 1506220566Smav } else { 1507220566Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1508220566Smav ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 1509220566Smav } 1510220566Smav xpt_done(ch->hold[i]); 1511220566Smav ch->hold[i] = NULL; 1512220566Smav ch->numhslots--; 1513220566Smav xpt_free_ccb(ccb); 1514220566Smav} 1515220566Smav 1516220566Smavstatic void 1517195801Smavsiis_portinit(device_t dev) 1518195801Smav{ 1519195801Smav struct siis_channel *ch = device_get_softc(dev); 1520195801Smav int i; 1521195801Smav 1522195801Smav ch->eslots = 0; 1523195801Smav ch->recovery = 0; 1524195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 1525195801Smav for (i = 0; i < 16; i++) { 1526195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 1527195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 1528195801Smav } 1529195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 1530195801Smav siis_wait_ready(dev, 1000); 1531195801Smav} 1532195801Smav 1533198426Smavstatic int 1534195801Smavsiis_devreset(device_t dev) 1535195801Smav{ 1536195801Smav struct siis_channel *ch = device_get_softc(dev); 1537198426Smav int timeout = 0; 1538198426Smav uint32_t val; 1539195801Smav 1540195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 1541198426Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1542198426Smav SIIS_P_CTL_DEV_RESET) != 0) { 1543220829Smav DELAY(100); 1544220829Smav if (timeout++ > 1000) { 1545220829Smav device_printf(dev, "device reset stuck " 1546220829Smav "(timeout 100ms) status = %08x\n", val); 1547198426Smav return (EBUSY); 1548198426Smav } 1549198426Smav } 1550198426Smav return (0); 1551195801Smav} 1552195801Smav 1553195801Smavstatic int 1554195801Smavsiis_wait_ready(device_t dev, int t) 1555195801Smav{ 1556195801Smav struct siis_channel *ch = device_get_softc(dev); 1557195801Smav int timeout = 0; 1558195801Smav uint32_t val; 1559195801Smav 1560195801Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1561195801Smav SIIS_P_CTL_READY) == 0) { 1562195801Smav DELAY(1000); 1563195801Smav if (timeout++ > t) { 1564195801Smav device_printf(dev, "port is not ready (timeout %dms) " 1565195801Smav "status = %08x\n", t, val); 1566195801Smav return (EBUSY); 1567195801Smav } 1568195801Smav } 1569195801Smav return (0); 1570195801Smav} 1571195801Smav 1572195801Smavstatic void 1573195801Smavsiis_reset(device_t dev) 1574195801Smav{ 1575195801Smav struct siis_channel *ch = device_get_softc(dev); 1576199821Smav int i, retry = 0, sata_rev; 1577198426Smav uint32_t val; 1578195801Smav 1579203108Smav xpt_freeze_simq(ch->sim, 1); 1580195801Smav if (bootverbose) 1581195801Smav device_printf(dev, "SIIS reset...\n"); 1582220566Smav if (!ch->recoverycmd && !ch->recovery) 1583198852Smav xpt_freeze_simq(ch->sim, ch->numrslots); 1584198852Smav /* Requeue frozen command. */ 1585195801Smav if (ch->frozen) { 1586195801Smav union ccb *fccb = ch->frozen; 1587195801Smav ch->frozen = NULL; 1588198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1589198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1590198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1591198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1592198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1593198321Smav } 1594195801Smav xpt_done(fccb); 1595195801Smav } 1596198426Smav /* Requeue all running commands. */ 1597195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1598195801Smav /* Do we have a running request on slot? */ 1599195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1600195801Smav continue; 1601195801Smav /* XXX; Commands in loading state. */ 1602195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1603195801Smav } 1604220830Smav /* Finish all held commands as-is. */ 1605198852Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1606198852Smav if (!ch->hold[i]) 1607198852Smav continue; 1608198852Smav xpt_done(ch->hold[i]); 1609198852Smav ch->hold[i] = NULL; 1610198852Smav ch->numhslots--; 1611198852Smav } 1612198852Smav if (ch->toslots != 0) 1613198852Smav xpt_release_simq(ch->sim, TRUE); 1614198852Smav ch->eslots = 0; 1615198852Smav ch->recovery = 0; 1616198852Smav ch->toslots = 0; 1617198852Smav ch->fatalerr = 0; 1618198426Smav /* Disable port interrupts */ 1619198426Smav ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 1620198426Smav /* Set speed limit. */ 1621199821Smav sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 1622199821Smav if (sata_rev == 1) 1623198426Smav val = ATA_SC_SPD_SPEED_GEN1; 1624199821Smav else if (sata_rev == 2) 1625198426Smav val = ATA_SC_SPD_SPEED_GEN2; 1626199821Smav else if (sata_rev == 3) 1627198426Smav val = ATA_SC_SPD_SPEED_GEN3; 1628198426Smav else 1629198426Smav val = 0; 1630198426Smav ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 1631198426Smav ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 1632198426Smav (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 1633198852Smavretry: 1634198426Smav siis_devreset(dev); 1635195801Smav /* Reset and reconnect PHY, */ 1636198426Smav if (!siis_sata_connect(ch)) { 1637195801Smav ch->devices = 0; 1638195801Smav /* Enable port interrupts */ 1639195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1640195801Smav if (bootverbose) 1641195801Smav device_printf(dev, 1642195801Smav "SIIS reset done: phy reset found no device\n"); 1643195801Smav /* Tell the XPT about the event */ 1644195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1645203108Smav xpt_release_simq(ch->sim, TRUE); 1646195801Smav return; 1647195801Smav } 1648220591Smav /* Wait for port ready status. */ 1649220591Smav if (siis_wait_ready(dev, 1000)) { 1650220591Smav device_printf(dev, "port ready timeout\n"); 1651198852Smav if (!retry) { 1652198852Smav device_printf(dev, "trying full port reset ...\n"); 1653198852Smav /* Get port to the reset state. */ 1654198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 1655198852Smav DELAY(10000); 1656198852Smav /* Get port out of reset state. */ 1657198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 1658198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 1659198852Smav if (ch->pm_present) 1660198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1661198852Smav else 1662198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1663198852Smav siis_wait_ready(dev, 5000); 1664198852Smav retry = 1; 1665198852Smav goto retry; 1666198852Smav } 1667198852Smav } 1668195801Smav ch->devices = 1; 1669195801Smav /* Enable port interrupts */ 1670195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 1671195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1672195801Smav if (bootverbose) 1673195801Smav device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 1674195801Smav /* Tell the XPT about the event */ 1675195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1676203108Smav xpt_release_simq(ch->sim, TRUE); 1677195801Smav} 1678195801Smav 1679195801Smavstatic int 1680199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag) 1681195801Smav{ 1682199821Smav struct siis_channel *ch = device_get_softc(dev); 1683195801Smav u_int8_t *fis = &ctp->fis[0]; 1684195801Smav 1685195801Smav bzero(fis, 24); 1686195801Smav fis[0] = 0x27; /* host to device */ 1687195801Smav fis[1] = (ccb->ccb_h.target_id & 0x0f); 1688195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1689195801Smav fis[1] |= 0x80; 1690195801Smav fis[2] = ATA_PACKET_CMD; 1691199821Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1692199821Smav ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 1693195801Smav fis[3] = ATA_F_DMA; 1694195801Smav else { 1695195801Smav fis[5] = ccb->csio.dxfer_len; 1696195801Smav fis[6] = ccb->csio.dxfer_len >> 8; 1697195801Smav } 1698195801Smav fis[7] = ATA_D_LBA; 1699195801Smav fis[15] = ATA_A_4BIT; 1700195801Smav bzero(ctp->u.atapi.ccb, 16); 1701195801Smav bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1702195801Smav ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1703195801Smav ctp->u.atapi.ccb, ccb->csio.cdb_len); 1704195801Smav } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1705195801Smav fis[1] |= 0x80; 1706195801Smav fis[2] = ccb->ataio.cmd.command; 1707195801Smav fis[3] = ccb->ataio.cmd.features; 1708195801Smav fis[4] = ccb->ataio.cmd.lba_low; 1709195801Smav fis[5] = ccb->ataio.cmd.lba_mid; 1710195801Smav fis[6] = ccb->ataio.cmd.lba_high; 1711195801Smav fis[7] = ccb->ataio.cmd.device; 1712195801Smav fis[8] = ccb->ataio.cmd.lba_low_exp; 1713195801Smav fis[9] = ccb->ataio.cmd.lba_mid_exp; 1714195801Smav fis[10] = ccb->ataio.cmd.lba_high_exp; 1715195801Smav fis[11] = ccb->ataio.cmd.features_exp; 1716195801Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1717195801Smav fis[12] = tag << 3; 1718195801Smav fis[13] = 0; 1719195801Smav } else { 1720195801Smav fis[12] = ccb->ataio.cmd.sector_count; 1721195801Smav fis[13] = ccb->ataio.cmd.sector_count_exp; 1722195801Smav } 1723195801Smav fis[15] = ATA_A_4BIT; 1724195801Smav } else { 1725195801Smav /* Soft reset. */ 1726195801Smav } 1727195801Smav return (20); 1728195801Smav} 1729195801Smav 1730195801Smavstatic int 1731195801Smavsiis_sata_connect(struct siis_channel *ch) 1732195801Smav{ 1733195801Smav u_int32_t status; 1734220829Smav int timeout, found = 0; 1735195801Smav 1736195801Smav /* Wait up to 100ms for "connect well" */ 1737220829Smav for (timeout = 0; timeout < 1000 ; timeout++) { 1738195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 1739220829Smav if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE) 1740220829Smav found = 1; 1741195801Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1742195801Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1743195801Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 1744195801Smav break; 1745220829Smav if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 1746220829Smav if (bootverbose) { 1747220829Smav device_printf(ch->dev, "SATA offline status=%08x\n", 1748220829Smav status); 1749220829Smav } 1750220829Smav return (0); 1751220829Smav } 1752220829Smav if (found == 0 && timeout >= 100) 1753220829Smav break; 1754220829Smav DELAY(100); 1755195801Smav } 1756220829Smav if (timeout >= 1000 || !found) { 1757195801Smav if (bootverbose) { 1758220829Smav device_printf(ch->dev, 1759220829Smav "SATA connect timeout time=%dus status=%08x\n", 1760220829Smav timeout * 100, status); 1761195801Smav } 1762195801Smav return (0); 1763195801Smav } 1764195801Smav if (bootverbose) { 1765220829Smav device_printf(ch->dev, "SATA connect time=%dus status=%08x\n", 1766220829Smav timeout * 100, status); 1767195801Smav } 1768195801Smav /* Clear SATA error register */ 1769195801Smav ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 1770195801Smav return (1); 1771195801Smav} 1772195801Smav 1773207431Smavstatic int 1774207431Smavsiis_check_ids(device_t dev, union ccb *ccb) 1775207431Smav{ 1776207431Smav 1777207431Smav if (ccb->ccb_h.target_id > 15) { 1778207431Smav ccb->ccb_h.status = CAM_TID_INVALID; 1779207431Smav xpt_done(ccb); 1780207431Smav return (-1); 1781207431Smav } 1782207431Smav if (ccb->ccb_h.target_lun != 0) { 1783207431Smav ccb->ccb_h.status = CAM_LUN_INVALID; 1784207431Smav xpt_done(ccb); 1785207431Smav return (-1); 1786207431Smav } 1787207431Smav return (0); 1788207431Smav} 1789207431Smav 1790195801Smavstatic void 1791195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb) 1792195801Smav{ 1793210471Smav device_t dev, parent; 1794195801Smav struct siis_channel *ch; 1795195801Smav 1796195801Smav CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 1797195801Smav ccb->ccb_h.func_code)); 1798195801Smav 1799195801Smav ch = (struct siis_channel *)cam_sim_softc(sim); 1800195801Smav dev = ch->dev; 1801195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1802195801Smav switch (ccb->ccb_h.func_code) { 1803195801Smav /* Common cases first */ 1804195801Smav case XPT_ATA_IO: /* Execute the requested I/O operation */ 1805195801Smav case XPT_SCSI_IO: 1806207431Smav if (siis_check_ids(dev, ccb)) 1807207431Smav return; 1808207431Smav if (ch->devices == 0 || 1809207431Smav (ch->pm_present == 0 && 1810207431Smav ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 1811195801Smav ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1812195801Smav break; 1813195801Smav } 1814220566Smav ccb->ccb_h.recovery_type = RECOVERY_NONE; 1815195801Smav /* Check for command collision. */ 1816195801Smav if (siis_check_collision(dev, ccb)) { 1817195801Smav /* Freeze command. */ 1818195801Smav ch->frozen = ccb; 1819195801Smav /* We have only one frozen slot, so freeze simq also. */ 1820195801Smav xpt_freeze_simq(ch->sim, 1); 1821195801Smav return; 1822195801Smav } 1823195801Smav siis_begin_transaction(dev, ccb); 1824207431Smav return; 1825195801Smav case XPT_EN_LUN: /* Enable LUN as a target */ 1826195801Smav case XPT_TARGET_IO: /* Execute target I/O request */ 1827195801Smav case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1828195801Smav case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1829195801Smav case XPT_ABORT: /* Abort the specified CCB */ 1830195801Smav /* XXX Implement */ 1831195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1832195801Smav break; 1833195801Smav case XPT_SET_TRAN_SETTINGS: 1834195801Smav { 1835195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1836199747Smav struct siis_device *d; 1837195801Smav 1838207431Smav if (siis_check_ids(dev, ccb)) 1839207431Smav return; 1840199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1841199747Smav d = &ch->curr[ccb->ccb_h.target_id]; 1842199747Smav else 1843199747Smav d = &ch->user[ccb->ccb_h.target_id]; 1844199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 1845199747Smav d->revision = cts->xport_specific.sata.revision; 1846199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 1847199747Smav d->mode = cts->xport_specific.sata.mode; 1848199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 1849199747Smav d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 1850199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1851199747Smav d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags); 1852195801Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 1853198852Smav ch->pm_present = cts->xport_specific.sata.pm_present; 1854198852Smav if (ch->pm_present) 1855195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1856195801Smav else 1857195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1858195801Smav } 1859203376Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1860203376Smav d->atapi = cts->xport_specific.sata.atapi; 1861207499Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1862207499Smav d->caps = cts->xport_specific.sata.caps; 1863195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1864195801Smav break; 1865195801Smav } 1866195801Smav case XPT_GET_TRAN_SETTINGS: 1867195801Smav /* Get default/user set transfer settings for the target */ 1868195801Smav { 1869195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1870199747Smav struct siis_device *d; 1871195801Smav uint32_t status; 1872195801Smav 1873207431Smav if (siis_check_ids(dev, ccb)) 1874207431Smav return; 1875199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1876199747Smav d = &ch->curr[ccb->ccb_h.target_id]; 1877199747Smav else 1878199747Smav d = &ch->user[ccb->ccb_h.target_id]; 1879195801Smav cts->protocol = PROTO_ATA; 1880196655Smav cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1881195801Smav cts->transport = XPORT_SATA; 1882196655Smav cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1883195801Smav cts->proto_specific.valid = 0; 1884195801Smav cts->xport_specific.sata.valid = 0; 1885199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1886199747Smav (ccb->ccb_h.target_id == 15 || 1887199747Smav (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 1888195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1889199747Smav if (status & 0x0f0) { 1890199747Smav cts->xport_specific.sata.revision = 1891199747Smav (status & 0x0f0) >> 4; 1892199747Smav cts->xport_specific.sata.valid |= 1893199747Smav CTS_SATA_VALID_REVISION; 1894199747Smav } 1895207499Smav cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 1896207499Smav if (ch->pm_level) 1897207499Smav cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 1898220602Smav cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN; 1899207499Smav cts->xport_specific.sata.caps &= 1900207499Smav ch->user[ccb->ccb_h.target_id].caps; 1901207499Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1902199747Smav } else { 1903199747Smav cts->xport_specific.sata.revision = d->revision; 1904199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 1905207499Smav cts->xport_specific.sata.caps = d->caps; 1906220602Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1907220602Smav (ch->quirks & SIIS_Q_SNTF) == 0) 1908220602Smav cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN; 1909207499Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1910195801Smav } 1911199747Smav cts->xport_specific.sata.mode = d->mode; 1912199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 1913199747Smav cts->xport_specific.sata.bytecount = d->bytecount; 1914199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 1915198852Smav cts->xport_specific.sata.pm_present = ch->pm_present; 1916195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1917199747Smav cts->xport_specific.sata.tags = d->tags; 1918199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 1919203376Smav cts->xport_specific.sata.atapi = d->atapi; 1920203376Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 1921195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1922195801Smav break; 1923195801Smav } 1924195801Smav case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1925195801Smav case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1926195801Smav siis_reset(dev); 1927195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1928195801Smav break; 1929195801Smav case XPT_TERM_IO: /* Terminate the I/O process */ 1930195801Smav /* XXX Implement */ 1931195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1932195801Smav break; 1933195801Smav case XPT_PATH_INQ: /* Path routing inquiry */ 1934195801Smav { 1935195801Smav struct ccb_pathinq *cpi = &ccb->cpi; 1936195801Smav 1937210471Smav parent = device_get_parent(dev); 1938195801Smav cpi->version_num = 1; /* XXX??? */ 1939195801Smav cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 1940195801Smav cpi->hba_inquiry |= PI_SATAPM; 1941195801Smav cpi->target_sprt = 0; 1942195801Smav cpi->hba_misc = PIM_SEQSCAN; 1943195801Smav cpi->hba_eng_cnt = 0; 1944198322Smav cpi->max_target = 15; 1945195801Smav cpi->max_lun = 0; 1946195801Smav cpi->initiator_id = 0; 1947195801Smav cpi->bus_id = cam_sim_bus(sim); 1948195801Smav cpi->base_transfer_speed = 150000; 1949195801Smav strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1950195801Smav strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 1951195801Smav strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1952195801Smav cpi->unit_number = cam_sim_unit(sim); 1953195801Smav cpi->transport = XPORT_SATA; 1954196655Smav cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1955195801Smav cpi->protocol = PROTO_ATA; 1956196655Smav cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1957210471Smav cpi->maxio = MAXPHYS; 1958210471Smav cpi->hba_vendor = pci_get_vendor(parent); 1959210471Smav cpi->hba_device = pci_get_device(parent); 1960210471Smav cpi->hba_subvendor = pci_get_subvendor(parent); 1961210471Smav cpi->hba_subdevice = pci_get_subdevice(parent); 1962195801Smav cpi->ccb_h.status = CAM_REQ_CMP; 1963195801Smav break; 1964195801Smav } 1965195801Smav default: 1966195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1967195801Smav break; 1968195801Smav } 1969207431Smav xpt_done(ccb); 1970195801Smav} 1971195801Smav 1972195801Smavstatic void 1973195801Smavsiispoll(struct cam_sim *sim) 1974195801Smav{ 1975195801Smav struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 1976195801Smav 1977195801Smav siis_ch_intr(ch->dev); 1978195801Smav} 1979