siis.c revision 220566
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 220566 2011-04-12 11:29:15Z 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; 477199747Smav } 478195801Smav mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF); 479195801Smav rid = ch->unit; 480195801Smav if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 481195801Smav &rid, RF_ACTIVE))) 482195801Smav return (ENXIO); 483195801Smav siis_dmainit(dev); 484195801Smav siis_slotsalloc(dev); 485208393Smav siis_ch_init(dev); 486195801Smav mtx_lock(&ch->mtx); 487195801Smav rid = ATA_IRQ_RID; 488195801Smav if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 489195801Smav &rid, RF_SHAREABLE | RF_ACTIVE))) { 490195801Smav device_printf(dev, "Unable to map interrupt\n"); 491208816Smav error = ENXIO; 492208816Smav goto err0; 493195801Smav } 494195801Smav if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 495195801Smav siis_ch_intr_locked, dev, &ch->ih))) { 496195801Smav device_printf(dev, "Unable to setup interrupt\n"); 497195801Smav error = ENXIO; 498195801Smav goto err1; 499195801Smav } 500195801Smav /* Create the device queue for our SIM. */ 501195801Smav devq = cam_simq_alloc(SIIS_MAX_SLOTS); 502195801Smav if (devq == NULL) { 503195801Smav device_printf(dev, "Unable to allocate simq\n"); 504195801Smav error = ENOMEM; 505195801Smav goto err1; 506195801Smav } 507195801Smav /* Construct SIM entry */ 508195801Smav ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch, 509199178Smav device_get_unit(dev), &ch->mtx, 2, SIIS_MAX_SLOTS, devq); 510195801Smav if (ch->sim == NULL) { 511208816Smav cam_simq_free(devq); 512195801Smav device_printf(dev, "unable to allocate sim\n"); 513195801Smav error = ENOMEM; 514208816Smav goto err1; 515195801Smav } 516195801Smav if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 517195801Smav device_printf(dev, "unable to register xpt bus\n"); 518195801Smav error = ENXIO; 519195801Smav goto err2; 520195801Smav } 521195801Smav if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 522195801Smav CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 523195801Smav device_printf(dev, "unable to create path\n"); 524195801Smav error = ENXIO; 525195801Smav goto err3; 526195801Smav } 527195801Smav mtx_unlock(&ch->mtx); 528217877Smav ch->led = led_create(siis_ch_led, dev, device_get_nameunit(dev)); 529195801Smav return (0); 530195801Smav 531195801Smaverr3: 532195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 533195801Smaverr2: 534195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 535195801Smaverr1: 536195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 537208816Smaverr0: 538195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 539195801Smav mtx_unlock(&ch->mtx); 540214325Smav mtx_destroy(&ch->mtx); 541195801Smav return (error); 542195801Smav} 543195801Smav 544195801Smavstatic int 545195801Smavsiis_ch_detach(device_t dev) 546195801Smav{ 547195801Smav struct siis_channel *ch = device_get_softc(dev); 548195801Smav 549217877Smav led_destroy(ch->led); 550195801Smav mtx_lock(&ch->mtx); 551195801Smav xpt_async(AC_LOST_DEVICE, ch->path, NULL); 552195801Smav xpt_free_path(ch->path); 553195801Smav xpt_bus_deregister(cam_sim_path(ch->sim)); 554195801Smav cam_sim_free(ch->sim, /*free_devq*/TRUE); 555195801Smav mtx_unlock(&ch->mtx); 556195801Smav 557195801Smav bus_teardown_intr(dev, ch->r_irq, ch->ih); 558195801Smav bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 559195801Smav 560208393Smav siis_ch_deinit(dev); 561195801Smav siis_slotsfree(dev); 562195801Smav siis_dmafini(dev); 563195801Smav 564195801Smav bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 565195801Smav mtx_destroy(&ch->mtx); 566195801Smav return (0); 567195801Smav} 568195801Smav 569195801Smavstatic int 570208393Smavsiis_ch_init(device_t dev) 571195801Smav{ 572195801Smav struct siis_channel *ch = device_get_softc(dev); 573195801Smav 574208393Smav /* Get port out of reset state. */ 575208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 576208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 577208393Smav if (ch->pm_present) 578208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 579208393Smav else 580208393Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 581208393Smav /* Enable port interrupts */ 582208393Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 583208393Smav return (0); 584208393Smav} 585208393Smav 586208393Smavstatic int 587208393Smavsiis_ch_deinit(device_t dev) 588208393Smav{ 589208393Smav struct siis_channel *ch = device_get_softc(dev); 590208393Smav 591195801Smav /* Put port into reset state. */ 592195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 593195801Smav return (0); 594195801Smav} 595195801Smav 596195801Smavstatic int 597208393Smavsiis_ch_suspend(device_t dev) 598208393Smav{ 599208393Smav struct siis_channel *ch = device_get_softc(dev); 600208393Smav 601208393Smav mtx_lock(&ch->mtx); 602208393Smav xpt_freeze_simq(ch->sim, 1); 603208393Smav while (ch->oslots) 604208393Smav msleep(ch, &ch->mtx, PRIBIO, "siissusp", hz/100); 605208393Smav siis_ch_deinit(dev); 606208393Smav mtx_unlock(&ch->mtx); 607208393Smav return (0); 608208393Smav} 609208393Smav 610208393Smavstatic int 611195801Smavsiis_ch_resume(device_t dev) 612195801Smav{ 613195801Smav struct siis_channel *ch = device_get_softc(dev); 614195801Smav 615208393Smav mtx_lock(&ch->mtx); 616208393Smav siis_ch_init(dev); 617208393Smav siis_reset(dev); 618208393Smav xpt_release_simq(ch->sim, TRUE); 619208393Smav mtx_unlock(&ch->mtx); 620195801Smav return (0); 621195801Smav} 622195801Smav 623195801Smavdevclass_t siisch_devclass; 624195801Smavstatic device_method_t siisch_methods[] = { 625195801Smav DEVMETHOD(device_probe, siis_ch_probe), 626195801Smav DEVMETHOD(device_attach, siis_ch_attach), 627195801Smav DEVMETHOD(device_detach, siis_ch_detach), 628195801Smav DEVMETHOD(device_suspend, siis_ch_suspend), 629195801Smav DEVMETHOD(device_resume, siis_ch_resume), 630195801Smav { 0, 0 } 631195801Smav}; 632195801Smavstatic driver_t siisch_driver = { 633195801Smav "siisch", 634195801Smav siisch_methods, 635195801Smav sizeof(struct siis_channel) 636195801Smav}; 637195801SmavDRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0); 638195801Smav 639217877Smavstatic void 640217877Smavsiis_ch_led(void *priv, int onoff) 641217877Smav{ 642217877Smav device_t dev; 643217877Smav struct siis_channel *ch; 644217877Smav 645217877Smav dev = (device_t)priv; 646217877Smav ch = device_get_softc(dev); 647217877Smav 648217877Smav if (onoff == 0) 649217877Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_LED_ON); 650217877Smav else 651217877Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_LED_ON); 652217877Smav} 653217877Smav 654195801Smavstruct siis_dc_cb_args { 655195801Smav bus_addr_t maddr; 656195801Smav int error; 657195801Smav}; 658195801Smav 659195801Smavstatic void 660195801Smavsiis_dmainit(device_t dev) 661195801Smav{ 662195801Smav struct siis_channel *ch = device_get_softc(dev); 663195801Smav struct siis_dc_cb_args dcba; 664195801Smav 665195801Smav /* Command area. */ 666195801Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 667195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 668195801Smav NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE, 669195801Smav 0, NULL, NULL, &ch->dma.work_tag)) 670195801Smav goto error; 671195801Smav if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 672195801Smav &ch->dma.work_map)) 673195801Smav goto error; 674195801Smav if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 675195801Smav SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) { 676195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 677195801Smav goto error; 678195801Smav } 679195801Smav ch->dma.work_bus = dcba.maddr; 680195801Smav /* Data area. */ 681199333Smav if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 682195801Smav BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 683195801Smav NULL, NULL, 684195801Smav SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS, 685195801Smav SIIS_SG_ENTRIES, 0xFFFFFFFF, 686195801Smav 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 687195801Smav goto error; 688195801Smav } 689195801Smav return; 690195801Smav 691195801Smaverror: 692195801Smav device_printf(dev, "WARNING - DMA initialization failed\n"); 693195801Smav siis_dmafini(dev); 694195801Smav} 695195801Smav 696195801Smavstatic void 697195801Smavsiis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 698195801Smav{ 699195801Smav struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc; 700195801Smav 701195801Smav if (!(dcba->error = error)) 702195801Smav dcba->maddr = segs[0].ds_addr; 703195801Smav} 704195801Smav 705195801Smavstatic void 706195801Smavsiis_dmafini(device_t dev) 707195801Smav{ 708195801Smav struct siis_channel *ch = device_get_softc(dev); 709195801Smav 710195801Smav if (ch->dma.data_tag) { 711195801Smav bus_dma_tag_destroy(ch->dma.data_tag); 712195801Smav ch->dma.data_tag = NULL; 713195801Smav } 714195801Smav if (ch->dma.work_bus) { 715195801Smav bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 716195801Smav bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 717195801Smav ch->dma.work_bus = 0; 718195801Smav ch->dma.work_map = NULL; 719195801Smav ch->dma.work = NULL; 720195801Smav } 721195801Smav if (ch->dma.work_tag) { 722195801Smav bus_dma_tag_destroy(ch->dma.work_tag); 723195801Smav ch->dma.work_tag = NULL; 724195801Smav } 725195801Smav} 726195801Smav 727195801Smavstatic void 728195801Smavsiis_slotsalloc(device_t dev) 729195801Smav{ 730195801Smav struct siis_channel *ch = device_get_softc(dev); 731195801Smav int i; 732195801Smav 733195801Smav /* Alloc and setup command/dma slots */ 734195801Smav bzero(ch->slot, sizeof(ch->slot)); 735195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 736195801Smav struct siis_slot *slot = &ch->slot[i]; 737195801Smav 738195801Smav slot->dev = dev; 739195801Smav slot->slot = i; 740195801Smav slot->state = SIIS_SLOT_EMPTY; 741195801Smav slot->ccb = NULL; 742195801Smav callout_init_mtx(&slot->timeout, &ch->mtx, 0); 743195801Smav 744195801Smav if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 745195801Smav device_printf(ch->dev, "FAILURE - create data_map\n"); 746195801Smav } 747195801Smav} 748195801Smav 749195801Smavstatic void 750195801Smavsiis_slotsfree(device_t dev) 751195801Smav{ 752195801Smav struct siis_channel *ch = device_get_softc(dev); 753195801Smav int i; 754195801Smav 755195801Smav /* Free all dma slots */ 756195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 757195801Smav struct siis_slot *slot = &ch->slot[i]; 758195801Smav 759198896Smav callout_drain(&slot->timeout); 760195801Smav if (slot->dma.data_map) { 761195801Smav bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 762195801Smav slot->dma.data_map = NULL; 763195801Smav } 764195801Smav } 765195801Smav} 766195801Smav 767195801Smavstatic void 768196655Smavsiis_notify_events(device_t dev) 769196655Smav{ 770196655Smav struct siis_channel *ch = device_get_softc(dev); 771196655Smav struct cam_path *dpath; 772196655Smav u_int32_t status; 773196655Smav int i; 774196655Smav 775200217Smav if (ch->quirks & SIIS_Q_SNTF) { 776200217Smav status = ATA_INL(ch->r_mem, SIIS_P_SNTF); 777200217Smav ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status); 778200217Smav } else { 779200217Smav /* 780200217Smav * Without SNTF we have no idea which device sent notification. 781200217Smav * If PMP is connected, assume it, else - device. 782200217Smav */ 783200217Smav status = (ch->pm_present) ? 0x8000 : 0x0001; 784200217Smav } 785196655Smav if (bootverbose) 786196655Smav device_printf(dev, "SNTF 0x%04x\n", status); 787196655Smav for (i = 0; i < 16; i++) { 788196655Smav if ((status & (1 << i)) == 0) 789196655Smav continue; 790196655Smav if (xpt_create_path(&dpath, NULL, 791196655Smav xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 792196655Smav xpt_async(AC_SCSI_AEN, dpath, NULL); 793196655Smav xpt_free_path(dpath); 794196655Smav } 795196655Smav } 796196655Smav 797196655Smav} 798196655Smav 799196655Smavstatic void 800195801Smavsiis_phy_check_events(device_t dev) 801195801Smav{ 802195801Smav struct siis_channel *ch = device_get_softc(dev); 803195801Smav 804195801Smav /* If we have a connection event, deal with it */ 805195801Smav if (ch->pm_level == 0) { 806195801Smav u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 807203108Smav union ccb *ccb; 808203108Smav 809203165Smav if (bootverbose) { 810203165Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 811203165Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 812203165Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 813195801Smav device_printf(dev, "CONNECT requested\n"); 814203165Smav } else 815195801Smav device_printf(dev, "DISCONNECT requested\n"); 816195801Smav } 817203165Smav siis_reset(dev); 818203108Smav if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 819203108Smav return; 820203108Smav if (xpt_create_path(&ccb->ccb_h.path, NULL, 821203108Smav cam_sim_path(ch->sim), 822203108Smav CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 823203108Smav xpt_free_ccb(ccb); 824203108Smav return; 825203108Smav } 826203108Smav xpt_rescan(ccb); 827195801Smav } 828195801Smav} 829195801Smav 830195801Smavstatic void 831195801Smavsiis_ch_intr_locked(void *data) 832195801Smav{ 833195801Smav device_t dev = (device_t)data; 834195801Smav struct siis_channel *ch = device_get_softc(dev); 835195801Smav 836195801Smav mtx_lock(&ch->mtx); 837195801Smav siis_ch_intr(data); 838195801Smav mtx_unlock(&ch->mtx); 839195801Smav} 840195801Smav 841195801Smavstatic void 842195801Smavsiis_ch_intr(void *data) 843195801Smav{ 844195801Smav device_t dev = (device_t)data; 845195801Smav struct siis_channel *ch = device_get_softc(dev); 846195801Smav uint32_t istatus, sstatus, ctx, estatus, ok, err = 0; 847195801Smav enum siis_err_type et; 848195801Smav int i, ccs, port, tslots; 849195801Smav 850195801Smav mtx_assert(&ch->mtx, MA_OWNED); 851195801Smav /* Read command statuses. */ 852195801Smav sstatus = ATA_INL(ch->r_mem, SIIS_P_SS); 853195801Smav ok = ch->rslots & ~sstatus; 854195801Smav /* Complete all successfull commands. */ 855195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 856195801Smav if ((ok >> i) & 1) 857195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE); 858195801Smav } 859195801Smav /* Do we have any other events? */ 860195801Smav if ((sstatus & SIIS_P_SS_ATTN) == 0) 861195801Smav return; 862195801Smav /* Read and clear interrupt statuses. */ 863195801Smav istatus = ATA_INL(ch->r_mem, SIIS_P_IS) & 864195801Smav (0xFFFF & ~SIIS_P_IX_COMMCOMP); 865195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus); 866195801Smav /* Process PHY events */ 867195801Smav if (istatus & SIIS_P_IX_PHYRDYCHG) 868195801Smav siis_phy_check_events(dev); 869196655Smav /* Process NOTIFY events */ 870196655Smav if (istatus & SIIS_P_IX_SDBN) 871196655Smav siis_notify_events(dev); 872195801Smav /* Process command errors */ 873195801Smav if (istatus & SIIS_P_IX_COMMERR) { 874195801Smav estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR); 875195801Smav ctx = ATA_INL(ch->r_mem, SIIS_P_CTX); 876195801Smav ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT; 877195801Smav port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT; 878195801Smav err = ch->rslots & sstatus; 879195801Smav//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n", 880195801Smav// __func__, sstatus, istatus, ch->rslots, estatus, ccs, port, 881195801Smav// ATA_INL(ch->r_mem, SIIS_P_SERR)); 882195801Smav 883220566Smav if (!ch->recoverycmd && !ch->recovery) { 884195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 885195801Smav ch->recovery = 1; 886195801Smav } 887195801Smav if (ch->frozen) { 888195801Smav union ccb *fccb = ch->frozen; 889195801Smav ch->frozen = NULL; 890198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 891198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 892198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 893198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 894198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 895198321Smav } 896195801Smav xpt_done(fccb); 897195801Smav } 898195801Smav if (estatus == SIIS_P_CMDERR_DEV || 899195801Smav estatus == SIIS_P_CMDERR_SDB || 900195801Smav estatus == SIIS_P_CMDERR_DATAFIS) { 901195801Smav tslots = ch->numtslots[port]; 902195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 903198852Smav /* XXX: requests in loading state. */ 904195801Smav if (((ch->rslots >> i) & 1) == 0) 905195801Smav continue; 906195801Smav if (ch->slot[i].ccb->ccb_h.target_id != port) 907195801Smav continue; 908195801Smav if (tslots == 0) { 909195801Smav /* Untagged operation. */ 910195801Smav if (i == ccs) 911195801Smav et = SIIS_ERR_TFE; 912195801Smav else 913195801Smav et = SIIS_ERR_INNOCENT; 914195801Smav } else { 915195801Smav /* Tagged operation. */ 916195801Smav et = SIIS_ERR_NCQ; 917195801Smav } 918195801Smav siis_end_transaction(&ch->slot[i], et); 919195801Smav } 920195801Smav /* 921195801Smav * We can't reinit port if there are some other 922195801Smav * commands active, use resume to complete them. 923195801Smav */ 924220566Smav if (ch->rslots != 0 && !ch->recoverycmd) 925195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME); 926195801Smav } else { 927195801Smav if (estatus == SIIS_P_CMDERR_SENDFIS || 928195801Smav estatus == SIIS_P_CMDERR_INCSTATE || 929195801Smav estatus == SIIS_P_CMDERR_PPE || 930195801Smav estatus == SIIS_P_CMDERR_SERVICE) { 931195801Smav et = SIIS_ERR_SATA; 932195801Smav } else 933195801Smav et = SIIS_ERR_INVALID; 934195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 935198852Smav /* XXX: requests in loading state. */ 936195801Smav if (((ch->rslots >> i) & 1) == 0) 937195801Smav continue; 938195801Smav siis_end_transaction(&ch->slot[i], et); 939195801Smav } 940195801Smav } 941195801Smav } 942195801Smav} 943195801Smav 944195801Smav/* Must be called with channel locked. */ 945195801Smavstatic int 946195801Smavsiis_check_collision(device_t dev, union ccb *ccb) 947195801Smav{ 948195801Smav struct siis_channel *ch = device_get_softc(dev); 949195801Smav 950195801Smav mtx_assert(&ch->mtx, MA_OWNED); 951195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 952199747Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 953199747Smav /* Tagged command while we have no supported tag free. */ 954199747Smav if (((~ch->oslots) & (0x7fffffff >> (31 - 955199747Smav ch->curr[ccb->ccb_h.target_id].tags))) == 0) 956199747Smav return (1); 957199747Smav } 958199747Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 959195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 960195801Smav /* Atomic command while anything active. */ 961195801Smav if (ch->numrslots != 0) 962195801Smav return (1); 963195801Smav } 964195801Smav /* We have some atomic command running. */ 965195801Smav if (ch->aslots != 0) 966195801Smav return (1); 967195801Smav return (0); 968195801Smav} 969195801Smav 970195801Smav/* Must be called with channel locked. */ 971195801Smavstatic void 972195801Smavsiis_begin_transaction(device_t dev, union ccb *ccb) 973195801Smav{ 974195801Smav struct siis_channel *ch = device_get_softc(dev); 975195801Smav struct siis_slot *slot; 976199747Smav int tag, tags; 977195801Smav 978195801Smav mtx_assert(&ch->mtx, MA_OWNED); 979195801Smav /* Choose empty slot. */ 980199747Smav tags = SIIS_MAX_SLOTS; 981199747Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 982199747Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) 983199747Smav tags = ch->curr[ccb->ccb_h.target_id].tags; 984199747Smav tag = fls((~ch->oslots) & (0x7fffffff >> (31 - tags))) - 1; 985195801Smav /* Occupy chosen slot. */ 986195801Smav slot = &ch->slot[tag]; 987195801Smav slot->ccb = ccb; 988195801Smav /* Update channel stats. */ 989199747Smav ch->oslots |= (1 << slot->slot); 990195801Smav ch->numrslots++; 991195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 992195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 993195801Smav ch->numtslots[ccb->ccb_h.target_id]++; 994195801Smav } 995195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 996195801Smav (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 997195801Smav ch->aslots |= (1 << slot->slot); 998195801Smav slot->dma.nsegs = 0; 999195801Smav /* If request moves data, setup and load SG list */ 1000195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1001195801Smav void *buf; 1002195801Smav bus_size_t size; 1003195801Smav 1004195801Smav slot->state = SIIS_SLOT_LOADING; 1005195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1006195801Smav buf = ccb->ataio.data_ptr; 1007195801Smav size = ccb->ataio.dxfer_len; 1008195801Smav } else { 1009195801Smav buf = ccb->csio.data_ptr; 1010195801Smav size = ccb->csio.dxfer_len; 1011195801Smav } 1012195801Smav bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 1013195801Smav buf, size, siis_dmasetprd, slot, 0); 1014195801Smav } else 1015195801Smav siis_execute_transaction(slot); 1016195801Smav} 1017195801Smav 1018195801Smav/* Locked by busdma engine. */ 1019195801Smavstatic void 1020195801Smavsiis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1021195801Smav{ 1022195801Smav struct siis_slot *slot = arg; 1023195801Smav struct siis_channel *ch = device_get_softc(slot->dev); 1024195801Smav struct siis_cmd *ctp; 1025195801Smav struct siis_dma_prd *prd; 1026195801Smav int i; 1027195801Smav 1028195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1029195801Smav if (error) { 1030195801Smav device_printf(slot->dev, "DMA load error\n"); 1031220566Smav if (!ch->recoverycmd) 1032195801Smav xpt_freeze_simq(ch->sim, 1); 1033195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 1034195801Smav return; 1035195801Smav } 1036195801Smav KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n")); 1037195801Smav /* Get a piece of the workspace for this request */ 1038195801Smav ctp = (struct siis_cmd *) 1039195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1040195801Smav /* Fill S/G table */ 1041195801Smav if (slot->ccb->ccb_h.func_code == XPT_ATA_IO) 1042195801Smav prd = &ctp->u.ata.prd[0]; 1043195801Smav else 1044195801Smav prd = &ctp->u.atapi.prd[0]; 1045195801Smav for (i = 0; i < nsegs; i++) { 1046195801Smav prd[i].dba = htole64(segs[i].ds_addr); 1047195801Smav prd[i].dbc = htole32(segs[i].ds_len); 1048195801Smav prd[i].control = 0; 1049195801Smav } 1050195801Smav prd[nsegs - 1].control = htole32(SIIS_PRD_TRM); 1051195801Smav slot->dma.nsegs = nsegs; 1052195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1053195801Smav ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1054195801Smav BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1055195801Smav siis_execute_transaction(slot); 1056195801Smav} 1057195801Smav 1058195801Smav/* Must be called with channel locked. */ 1059195801Smavstatic void 1060195801Smavsiis_execute_transaction(struct siis_slot *slot) 1061195801Smav{ 1062195801Smav device_t dev = slot->dev; 1063195801Smav struct siis_channel *ch = device_get_softc(dev); 1064195801Smav struct siis_cmd *ctp; 1065195801Smav union ccb *ccb = slot->ccb; 1066195801Smav u_int64_t prb_bus; 1067195801Smav 1068195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1069195801Smav /* Get a piece of the workspace for this request */ 1070195801Smav ctp = (struct siis_cmd *) 1071195801Smav (ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot)); 1072195801Smav ctp->control = 0; 1073195801Smav ctp->protocol_override = 0; 1074195801Smav ctp->transfer_count = 0; 1075195801Smav /* Special handling for Soft Reset command. */ 1076201222Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1077201222Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) { 1078201222Smav ctp->control |= htole16(SIIS_PRB_SOFT_RESET); 1079201222Smav } else { 1080201222Smav ctp->control |= htole16(SIIS_PRB_PROTOCOL_OVERRIDE); 1081201222Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1082201222Smav ctp->protocol_override |= 1083201222Smav htole16(SIIS_PRB_PROTO_NCQ); 1084201222Smav } 1085201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1086201222Smav ctp->protocol_override |= 1087201222Smav htole16(SIIS_PRB_PROTO_READ); 1088201222Smav } else 1089201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1090201222Smav ctp->protocol_override |= 1091201222Smav htole16(SIIS_PRB_PROTO_WRITE); 1092201222Smav } 1093201222Smav } 1094195801Smav } else if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1095201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1096195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_READ); 1097201222Smav else 1098201222Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1099195801Smav ctp->control |= htole16(SIIS_PRB_PACKET_WRITE); 1100195801Smav } 1101203108Smav /* Special handling for Soft Reset command. */ 1102203108Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1103203108Smav (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1104203108Smav (ccb->ataio.cmd.control & ATA_A_RESET)) { 1105203108Smav /* Kick controller into sane state */ 1106203108Smav siis_portinit(dev); 1107203108Smav } 1108195801Smav /* Setup the FIS for this request */ 1109199821Smav if (!siis_setup_fis(dev, ctp, ccb, slot->slot)) { 1110195801Smav device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1111220566Smav if (!ch->recoverycmd) 1112195801Smav xpt_freeze_simq(ch->sim, 1); 1113195801Smav siis_end_transaction(slot, SIIS_ERR_INVALID); 1114195801Smav return; 1115195801Smav } 1116195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1117195801Smav BUS_DMASYNC_PREWRITE); 1118195801Smav /* Issue command to the controller. */ 1119195801Smav slot->state = SIIS_SLOT_RUNNING; 1120195801Smav ch->rslots |= (1 << slot->slot); 1121195801Smav prb_bus = ch->dma.work_bus + 1122195801Smav SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot); 1123195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus); 1124195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32); 1125195801Smav /* Start command execution timeout */ 1126195801Smav callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000, 1127195801Smav (timeout_t*)siis_timeout, slot); 1128195801Smav return; 1129195801Smav} 1130195801Smav 1131198852Smav/* Must be called with channel locked. */ 1132195801Smavstatic void 1133198852Smavsiis_process_timeout(device_t dev) 1134195801Smav{ 1135195801Smav struct siis_channel *ch = device_get_softc(dev); 1136195801Smav int i; 1137195801Smav 1138195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1139220566Smav if (!ch->recoverycmd && !ch->recovery) { 1140195801Smav xpt_freeze_simq(ch->sim, ch->numrslots); 1141198852Smav ch->recovery = 1; 1142195801Smav } 1143197838Smav /* Handle the rest of commands. */ 1144195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1145195801Smav /* Do we have a running request on slot? */ 1146195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1147195801Smav continue; 1148198852Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT); 1149195801Smav } 1150195801Smav} 1151195801Smav 1152203870Smav/* Must be called with channel locked. */ 1153203870Smavstatic void 1154203870Smavsiis_rearm_timeout(device_t dev) 1155203870Smav{ 1156203870Smav struct siis_channel *ch = device_get_softc(dev); 1157203870Smav int i; 1158203870Smav 1159203870Smav mtx_assert(&ch->mtx, MA_OWNED); 1160203870Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1161203870Smav struct siis_slot *slot = &ch->slot[i]; 1162203870Smav 1163203870Smav /* Do we have a running request on slot? */ 1164203870Smav if (slot->state < SIIS_SLOT_RUNNING) 1165203870Smav continue; 1166203870Smav if ((ch->toslots & (1 << i)) == 0) 1167203870Smav continue; 1168203870Smav callout_reset(&slot->timeout, 1169203870Smav (int)slot->ccb->ccb_h.timeout * hz / 1000, 1170203870Smav (timeout_t*)siis_timeout, slot); 1171203870Smav } 1172203870Smav} 1173203870Smav 1174198852Smav/* Locked by callout mechanism. */ 1175198852Smavstatic void 1176198852Smavsiis_timeout(struct siis_slot *slot) 1177198852Smav{ 1178198852Smav device_t dev = slot->dev; 1179198852Smav struct siis_channel *ch = device_get_softc(dev); 1180198852Smav 1181198852Smav mtx_assert(&ch->mtx, MA_OWNED); 1182198896Smav /* Check for stale timeout. */ 1183198896Smav if (slot->state < SIIS_SLOT_RUNNING) 1184198896Smav return; 1185198852Smav device_printf(dev, "Timeout on slot %d\n", slot->slot); 1186203108Smav device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n", 1187203108Smav __func__, ATA_INL(ch->r_mem, SIIS_P_IS), 1188203108Smav ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots, 1189203108Smav ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS), 1190203108Smav ATA_INL(ch->r_mem, SIIS_P_SERR)); 1191198852Smav 1192198852Smav if (ch->toslots == 0) 1193198852Smav xpt_freeze_simq(ch->sim, 1); 1194198852Smav ch->toslots |= (1 << slot->slot); 1195198852Smav if ((ch->rslots & ~ch->toslots) == 0) 1196198852Smav siis_process_timeout(dev); 1197198852Smav else 1198198852Smav device_printf(dev, " ... waiting for slots %08x\n", 1199198852Smav ch->rslots & ~ch->toslots); 1200198852Smav} 1201198852Smav 1202195801Smav/* Must be called with channel locked. */ 1203195801Smavstatic void 1204195801Smavsiis_end_transaction(struct siis_slot *slot, enum siis_err_type et) 1205195801Smav{ 1206195801Smav device_t dev = slot->dev; 1207195801Smav struct siis_channel *ch = device_get_softc(dev); 1208195801Smav union ccb *ccb = slot->ccb; 1209212732Smav int lastto; 1210195801Smav 1211195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1212195801Smav bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1213195801Smav BUS_DMASYNC_POSTWRITE); 1214195801Smav /* Read result registers to the result struct 1215195801Smav * May be incorrect if several commands finished same time, 1216195801Smav * so read only when sure or have to. 1217195801Smav */ 1218195801Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1219195801Smav struct ata_res *res = &ccb->ataio.res; 1220195801Smav if ((et == SIIS_ERR_TFE) || 1221195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1222195801Smav int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8; 1223195801Smav 1224195801Smav res->status = ATA_INB(ch->r_mem, offs + 2); 1225195801Smav res->error = ATA_INB(ch->r_mem, offs + 3); 1226195801Smav res->lba_low = ATA_INB(ch->r_mem, offs + 4); 1227195801Smav res->lba_mid = ATA_INB(ch->r_mem, offs + 5); 1228195801Smav res->lba_high = ATA_INB(ch->r_mem, offs + 6); 1229195801Smav res->device = ATA_INB(ch->r_mem, offs + 7); 1230195801Smav res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8); 1231195801Smav res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9); 1232195801Smav res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10); 1233195801Smav res->sector_count = ATA_INB(ch->r_mem, offs + 12); 1234195801Smav res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13); 1235195801Smav } else 1236195801Smav bzero(res, sizeof(*res)); 1237214988Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1238214988Smav ch->numrslots == 1) { 1239214988Smav ccb->ataio.resid = ccb->ataio.dxfer_len - 1240214988Smav ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1241214988Smav } 1242214988Smav } else { 1243214988Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN && 1244214988Smav ch->numrslots == 1) { 1245214988Smav ccb->csio.resid = ccb->csio.dxfer_len - 1246214988Smav ATA_INL(ch->r_mem, SIIS_P_LRAM_SLOT(slot->slot) + 4); 1247214988Smav } 1248195801Smav } 1249195801Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1250195801Smav bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1251195801Smav (ccb->ccb_h.flags & CAM_DIR_IN) ? 1252195801Smav BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1253195801Smav bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1254195801Smav } 1255195801Smav /* Set proper result status. */ 1256195801Smav if (et != SIIS_ERR_NONE || ch->recovery) { 1257195801Smav ch->eslots |= (1 << slot->slot); 1258195801Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1259195801Smav } 1260198321Smav /* In case of error, freeze device for proper recovery. */ 1261220566Smav if (et != SIIS_ERR_NONE && (!ch->recoverycmd) && 1262198321Smav !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1263198321Smav xpt_freeze_devq(ccb->ccb_h.path, 1); 1264198321Smav ccb->ccb_h.status |= CAM_DEV_QFRZN; 1265198321Smav } 1266198321Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1267195801Smav switch (et) { 1268195801Smav case SIIS_ERR_NONE: 1269195801Smav ccb->ccb_h.status |= CAM_REQ_CMP; 1270195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1271195801Smav ccb->csio.scsi_status = SCSI_STATUS_OK; 1272195801Smav break; 1273195801Smav case SIIS_ERR_INVALID: 1274198852Smav ch->fatalerr = 1; 1275195801Smav ccb->ccb_h.status |= CAM_REQ_INVALID; 1276195801Smav break; 1277195801Smav case SIIS_ERR_INNOCENT: 1278195801Smav ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1279195801Smav break; 1280195801Smav case SIIS_ERR_TFE: 1281198321Smav case SIIS_ERR_NCQ: 1282195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1283195801Smav ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1284195801Smav ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1285195801Smav } else { 1286195801Smav ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1287195801Smav } 1288195801Smav break; 1289195801Smav case SIIS_ERR_SATA: 1290198852Smav ch->fatalerr = 1; 1291195801Smav ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1292195801Smav break; 1293195801Smav case SIIS_ERR_TIMEOUT: 1294198852Smav ch->fatalerr = 1; 1295195801Smav ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1296195801Smav break; 1297195801Smav default: 1298195801Smav ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1299195801Smav } 1300195801Smav /* Free slot. */ 1301199747Smav ch->oslots &= ~(1 << slot->slot); 1302195801Smav ch->rslots &= ~(1 << slot->slot); 1303195801Smav ch->aslots &= ~(1 << slot->slot); 1304195801Smav slot->state = SIIS_SLOT_EMPTY; 1305195801Smav slot->ccb = NULL; 1306195801Smav /* Update channel stats. */ 1307195801Smav ch->numrslots--; 1308195801Smav if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1309195801Smav (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1310195801Smav ch->numtslots[ccb->ccb_h.target_id]--; 1311195801Smav } 1312212732Smav /* Cancel timeout state if request completed normally. */ 1313212732Smav if (et != SIIS_ERR_TIMEOUT) { 1314212732Smav lastto = (ch->toslots == (1 << slot->slot)); 1315212732Smav ch->toslots &= ~(1 << slot->slot); 1316212732Smav if (lastto) 1317212732Smav xpt_release_simq(ch->sim, TRUE); 1318212732Smav } 1319198852Smav /* If it was our READ LOG command - process it. */ 1320220566Smav if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 1321198852Smav siis_process_read_log(dev, ccb); 1322220566Smav /* If it was our REQUEST SENSE command - process it. */ 1323220566Smav } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 1324220566Smav siis_process_request_sense(dev, ccb); 1325220566Smav /* If it was NCQ or ATAPI command error, put result on hold. */ 1326220566Smav } else if (et == SIIS_ERR_NCQ || 1327220566Smav ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 1328220566Smav (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 1329195801Smav ch->hold[slot->slot] = ccb; 1330195801Smav ch->numhslots++; 1331198852Smav } else 1332195801Smav xpt_done(ccb); 1333195801Smav /* Unfreeze frozen command. */ 1334199747Smav if (ch->frozen && !siis_check_collision(dev, ch->frozen)) { 1335195801Smav union ccb *fccb = ch->frozen; 1336195801Smav ch->frozen = NULL; 1337195801Smav siis_begin_transaction(dev, fccb); 1338195801Smav xpt_release_simq(ch->sim, TRUE); 1339195801Smav } 1340195801Smav /* If we have no other active commands, ... */ 1341195801Smav if (ch->rslots == 0) { 1342198852Smav /* if there were timeouts or fatal error - reset port. */ 1343198852Smav if (ch->toslots != 0 || ch->fatalerr) { 1344198852Smav siis_reset(dev); 1345198852Smav } else { 1346198852Smav /* if we have slots in error, we can reinit port. */ 1347198852Smav if (ch->eslots != 0) 1348198852Smav siis_portinit(dev); 1349220566Smav /* if there commands on hold, we can do recovery. */ 1350220566Smav if (!ch->recoverycmd && ch->numhslots) 1351220566Smav siis_issue_recovery(dev); 1352198852Smav } 1353198852Smav /* If all the reset of commands are in timeout - abort them. */ 1354203870Smav } else if ((ch->rslots & ~ch->toslots) == 0 && 1355203870Smav et != SIIS_ERR_TIMEOUT) 1356203870Smav siis_rearm_timeout(dev); 1357195801Smav} 1358195801Smav 1359195801Smavstatic void 1360220566Smavsiis_issue_recovery(device_t dev) 1361195801Smav{ 1362195801Smav struct siis_channel *ch = device_get_softc(dev); 1363195801Smav union ccb *ccb; 1364195801Smav struct ccb_ataio *ataio; 1365220566Smav struct ccb_scsiio *csio; 1366195801Smav int i; 1367195801Smav 1368195801Smav /* Find some holden command. */ 1369195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1370195801Smav if (ch->hold[i]) 1371195801Smav break; 1372195801Smav } 1373195801Smav if (i == SIIS_MAX_SLOTS) 1374195801Smav return; 1375220566Smav ch->recoverycmd = 1; 1376195801Smav ccb = xpt_alloc_ccb_nowait(); 1377195801Smav if (ccb == NULL) { 1378195801Smav device_printf(dev, "Unable allocate READ LOG command"); 1379195801Smav return; /* XXX */ 1380195801Smav } 1381195801Smav ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1382220566Smav if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1383220566Smav /* READ LOG */ 1384220566Smav ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 1385220566Smav ccb->ccb_h.func_code = XPT_ATA_IO; 1386220566Smav ccb->ccb_h.flags = CAM_DIR_IN; 1387220566Smav ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1388220566Smav ataio = &ccb->ataio; 1389220566Smav ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT); 1390220566Smav if (ataio->data_ptr == NULL) { 1391220566Smav xpt_free_ccb(ccb); 1392220566Smav device_printf(dev, "Unable allocate memory for READ LOG command"); 1393220566Smav return; /* XXX */ 1394220566Smav } 1395220566Smav ataio->dxfer_len = 512; 1396220566Smav bzero(&ataio->cmd, sizeof(ataio->cmd)); 1397220566Smav ataio->cmd.flags = CAM_ATAIO_48BIT; 1398220566Smav ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1399220566Smav ataio->cmd.sector_count = 1; 1400220566Smav ataio->cmd.sector_count_exp = 0; 1401220566Smav ataio->cmd.lba_low = 0x10; 1402220566Smav ataio->cmd.lba_mid = 0; 1403220566Smav ataio->cmd.lba_mid_exp = 0; 1404220566Smav } else { 1405220566Smav /* REQUEST SENSE */ 1406220566Smav ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 1407220566Smav ccb->ccb_h.recovery_slot = i; 1408220566Smav ccb->ccb_h.func_code = XPT_SCSI_IO; 1409220566Smav ccb->ccb_h.flags = CAM_DIR_IN; 1410220566Smav ccb->ccb_h.status = 0; 1411220566Smav ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1412220566Smav csio = &ccb->csio; 1413220566Smav csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 1414220566Smav csio->dxfer_len = ch->hold[i]->csio.sense_len; 1415220566Smav csio->cdb_len = 6; 1416220566Smav bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 1417220566Smav csio->cdb_io.cdb_bytes[0] = 0x03; 1418220566Smav csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 1419195801Smav } 1420195801Smav siis_begin_transaction(dev, ccb); 1421195801Smav} 1422195801Smav 1423195801Smavstatic void 1424195801Smavsiis_process_read_log(device_t dev, union ccb *ccb) 1425195801Smav{ 1426195801Smav struct siis_channel *ch = device_get_softc(dev); 1427195801Smav uint8_t *data; 1428195801Smav struct ata_res *res; 1429195801Smav int i; 1430195801Smav 1431220566Smav ch->recoverycmd = 0; 1432195801Smav data = ccb->ataio.data_ptr; 1433195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1434195801Smav (data[0] & 0x80) == 0) { 1435195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1436195801Smav if (!ch->hold[i]) 1437195801Smav continue; 1438195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1439195801Smav continue; 1440195801Smav if ((data[0] & 0x1F) == i) { 1441195801Smav res = &ch->hold[i]->ataio.res; 1442195801Smav res->status = data[2]; 1443195801Smav res->error = data[3]; 1444195801Smav res->lba_low = data[4]; 1445195801Smav res->lba_mid = data[5]; 1446195801Smav res->lba_high = data[6]; 1447195801Smav res->device = data[7]; 1448195801Smav res->lba_low_exp = data[8]; 1449195801Smav res->lba_mid_exp = data[9]; 1450195801Smav res->lba_high_exp = data[10]; 1451195801Smav res->sector_count = data[12]; 1452195801Smav res->sector_count_exp = data[13]; 1453195801Smav } else { 1454195801Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1455195801Smav ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1456195801Smav } 1457195801Smav xpt_done(ch->hold[i]); 1458195801Smav ch->hold[i] = NULL; 1459195801Smav ch->numhslots--; 1460195801Smav } 1461195801Smav } else { 1462195801Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1463195801Smav device_printf(dev, "Error while READ LOG EXT\n"); 1464195801Smav else if ((data[0] & 0x80) == 0) { 1465195801Smav device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1466195801Smav } 1467195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1468195801Smav if (!ch->hold[i]) 1469195801Smav continue; 1470195801Smav if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1471195801Smav continue; 1472195801Smav xpt_done(ch->hold[i]); 1473195801Smav ch->hold[i] = NULL; 1474195801Smav ch->numhslots--; 1475195801Smav } 1476195801Smav } 1477195801Smav free(ccb->ataio.data_ptr, M_SIIS); 1478195801Smav xpt_free_ccb(ccb); 1479195801Smav} 1480195801Smav 1481195801Smavstatic void 1482220566Smavsiis_process_request_sense(device_t dev, union ccb *ccb) 1483220566Smav{ 1484220566Smav struct siis_channel *ch = device_get_softc(dev); 1485220566Smav int i; 1486220566Smav 1487220566Smav ch->recoverycmd = 0; 1488220566Smav 1489220566Smav i = ccb->ccb_h.recovery_slot; 1490220566Smav if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1491220566Smav ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 1492220566Smav } else { 1493220566Smav ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1494220566Smav ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 1495220566Smav } 1496220566Smav xpt_done(ch->hold[i]); 1497220566Smav ch->hold[i] = NULL; 1498220566Smav ch->numhslots--; 1499220566Smav xpt_free_ccb(ccb); 1500220566Smav} 1501220566Smav 1502220566Smavstatic void 1503195801Smavsiis_portinit(device_t dev) 1504195801Smav{ 1505195801Smav struct siis_channel *ch = device_get_softc(dev); 1506195801Smav int i; 1507195801Smav 1508195801Smav ch->eslots = 0; 1509195801Smav ch->recovery = 0; 1510195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME); 1511195801Smav for (i = 0; i < 16; i++) { 1512195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0), 1513195801Smav ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0); 1514195801Smav } 1515195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT); 1516195801Smav siis_wait_ready(dev, 1000); 1517195801Smav} 1518195801Smav 1519198426Smavstatic int 1520195801Smavsiis_devreset(device_t dev) 1521195801Smav{ 1522195801Smav struct siis_channel *ch = device_get_softc(dev); 1523198426Smav int timeout = 0; 1524198426Smav uint32_t val; 1525195801Smav 1526195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET); 1527198426Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1528198426Smav SIIS_P_CTL_DEV_RESET) != 0) { 1529198426Smav DELAY(1000); 1530198426Smav if (timeout++ > 100) { 1531198426Smav device_printf(dev, "device reset stuck (timeout %dms) " 1532198426Smav "status = %08x\n", timeout, val); 1533198426Smav return (EBUSY); 1534198426Smav } 1535198426Smav } 1536198426Smav return (0); 1537195801Smav} 1538195801Smav 1539195801Smavstatic int 1540195801Smavsiis_wait_ready(device_t dev, int t) 1541195801Smav{ 1542195801Smav struct siis_channel *ch = device_get_softc(dev); 1543195801Smav int timeout = 0; 1544195801Smav uint32_t val; 1545195801Smav 1546195801Smav while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) & 1547195801Smav SIIS_P_CTL_READY) == 0) { 1548195801Smav DELAY(1000); 1549195801Smav if (timeout++ > t) { 1550195801Smav device_printf(dev, "port is not ready (timeout %dms) " 1551195801Smav "status = %08x\n", t, val); 1552195801Smav return (EBUSY); 1553195801Smav } 1554195801Smav } 1555195801Smav return (0); 1556195801Smav} 1557195801Smav 1558195801Smavstatic void 1559195801Smavsiis_reset(device_t dev) 1560195801Smav{ 1561195801Smav struct siis_channel *ch = device_get_softc(dev); 1562199821Smav int i, retry = 0, sata_rev; 1563198426Smav uint32_t val; 1564195801Smav 1565203108Smav xpt_freeze_simq(ch->sim, 1); 1566195801Smav if (bootverbose) 1567195801Smav device_printf(dev, "SIIS reset...\n"); 1568220566Smav if (!ch->recoverycmd && !ch->recovery) 1569198852Smav xpt_freeze_simq(ch->sim, ch->numrslots); 1570198852Smav /* Requeue frozen command. */ 1571195801Smav if (ch->frozen) { 1572195801Smav union ccb *fccb = ch->frozen; 1573195801Smav ch->frozen = NULL; 1574198321Smav fccb->ccb_h.status &= ~CAM_STATUS_MASK; 1575198321Smav fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1576198321Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1577198321Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1578198321Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1579198321Smav } 1580195801Smav xpt_done(fccb); 1581195801Smav } 1582198426Smav /* Requeue all running commands. */ 1583195801Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1584195801Smav /* Do we have a running request on slot? */ 1585195801Smav if (ch->slot[i].state < SIIS_SLOT_RUNNING) 1586195801Smav continue; 1587195801Smav /* XXX; Commands in loading state. */ 1588195801Smav siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT); 1589195801Smav } 1590198852Smav /* Finish all holden commands as-is. */ 1591198852Smav for (i = 0; i < SIIS_MAX_SLOTS; i++) { 1592198852Smav if (!ch->hold[i]) 1593198852Smav continue; 1594198852Smav xpt_done(ch->hold[i]); 1595198852Smav ch->hold[i] = NULL; 1596198852Smav ch->numhslots--; 1597198852Smav } 1598198852Smav if (ch->toslots != 0) 1599198852Smav xpt_release_simq(ch->sim, TRUE); 1600198852Smav ch->eslots = 0; 1601198852Smav ch->recovery = 0; 1602198852Smav ch->toslots = 0; 1603198852Smav ch->fatalerr = 0; 1604198426Smav /* Disable port interrupts */ 1605198426Smav ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF); 1606198426Smav /* Set speed limit. */ 1607199821Smav sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 1608199821Smav if (sata_rev == 1) 1609198426Smav val = ATA_SC_SPD_SPEED_GEN1; 1610199821Smav else if (sata_rev == 2) 1611198426Smav val = ATA_SC_SPD_SPEED_GEN2; 1612199821Smav else if (sata_rev == 3) 1613198426Smav val = ATA_SC_SPD_SPEED_GEN3; 1614198426Smav else 1615198426Smav val = 0; 1616198426Smav ATA_OUTL(ch->r_mem, SIIS_P_SCTL, 1617198426Smav ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 1618198426Smav (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 1619198852Smavretry: 1620198426Smav siis_devreset(dev); 1621195801Smav /* Reset and reconnect PHY, */ 1622198426Smav if (!siis_sata_connect(ch)) { 1623195801Smav ch->devices = 0; 1624195801Smav /* Enable port interrupts */ 1625195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1626195801Smav if (bootverbose) 1627195801Smav device_printf(dev, 1628195801Smav "SIIS reset done: phy reset found no device\n"); 1629195801Smav /* Tell the XPT about the event */ 1630195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1631203108Smav xpt_release_simq(ch->sim, TRUE); 1632195801Smav return; 1633195801Smav } 1634195801Smav /* Wait for clearing busy status. */ 1635198852Smav if (siis_wait_ready(dev, 10000)) { 1636195801Smav device_printf(dev, "device ready timeout\n"); 1637198852Smav if (!retry) { 1638198852Smav device_printf(dev, "trying full port reset ...\n"); 1639198852Smav /* Get port to the reset state. */ 1640198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET); 1641198852Smav DELAY(10000); 1642198852Smav /* Get port out of reset state. */ 1643198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET); 1644198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT); 1645198852Smav if (ch->pm_present) 1646198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1647198852Smav else 1648198852Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1649198852Smav siis_wait_ready(dev, 5000); 1650198852Smav retry = 1; 1651198852Smav goto retry; 1652198852Smav } 1653198852Smav } 1654195801Smav ch->devices = 1; 1655195801Smav /* Enable port interrupts */ 1656195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF); 1657195801Smav ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED); 1658195801Smav if (bootverbose) 1659195801Smav device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices); 1660195801Smav /* Tell the XPT about the event */ 1661195801Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1662203108Smav xpt_release_simq(ch->sim, TRUE); 1663195801Smav} 1664195801Smav 1665195801Smavstatic int 1666199821Smavsiis_setup_fis(device_t dev, struct siis_cmd *ctp, union ccb *ccb, int tag) 1667195801Smav{ 1668199821Smav struct siis_channel *ch = device_get_softc(dev); 1669195801Smav u_int8_t *fis = &ctp->fis[0]; 1670195801Smav 1671195801Smav bzero(fis, 24); 1672195801Smav fis[0] = 0x27; /* host to device */ 1673195801Smav fis[1] = (ccb->ccb_h.target_id & 0x0f); 1674195801Smav if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1675195801Smav fis[1] |= 0x80; 1676195801Smav fis[2] = ATA_PACKET_CMD; 1677199821Smav if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1678199821Smav ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 1679195801Smav fis[3] = ATA_F_DMA; 1680195801Smav else { 1681195801Smav fis[5] = ccb->csio.dxfer_len; 1682195801Smav fis[6] = ccb->csio.dxfer_len >> 8; 1683195801Smav } 1684195801Smav fis[7] = ATA_D_LBA; 1685195801Smav fis[15] = ATA_A_4BIT; 1686195801Smav bzero(ctp->u.atapi.ccb, 16); 1687195801Smav bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1688195801Smav ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1689195801Smav ctp->u.atapi.ccb, ccb->csio.cdb_len); 1690195801Smav } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1691195801Smav fis[1] |= 0x80; 1692195801Smav fis[2] = ccb->ataio.cmd.command; 1693195801Smav fis[3] = ccb->ataio.cmd.features; 1694195801Smav fis[4] = ccb->ataio.cmd.lba_low; 1695195801Smav fis[5] = ccb->ataio.cmd.lba_mid; 1696195801Smav fis[6] = ccb->ataio.cmd.lba_high; 1697195801Smav fis[7] = ccb->ataio.cmd.device; 1698195801Smav fis[8] = ccb->ataio.cmd.lba_low_exp; 1699195801Smav fis[9] = ccb->ataio.cmd.lba_mid_exp; 1700195801Smav fis[10] = ccb->ataio.cmd.lba_high_exp; 1701195801Smav fis[11] = ccb->ataio.cmd.features_exp; 1702195801Smav if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1703195801Smav fis[12] = tag << 3; 1704195801Smav fis[13] = 0; 1705195801Smav } else { 1706195801Smav fis[12] = ccb->ataio.cmd.sector_count; 1707195801Smav fis[13] = ccb->ataio.cmd.sector_count_exp; 1708195801Smav } 1709195801Smav fis[15] = ATA_A_4BIT; 1710195801Smav } else { 1711195801Smav /* Soft reset. */ 1712195801Smav } 1713195801Smav return (20); 1714195801Smav} 1715195801Smav 1716195801Smavstatic int 1717195801Smavsiis_sata_connect(struct siis_channel *ch) 1718195801Smav{ 1719195801Smav u_int32_t status; 1720195801Smav int timeout; 1721195801Smav 1722195801Smav /* Wait up to 100ms for "connect well" */ 1723195801Smav for (timeout = 0; timeout < 100 ; timeout++) { 1724195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS); 1725195801Smav if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1726195801Smav ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1727195801Smav ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 1728195801Smav break; 1729195801Smav DELAY(1000); 1730195801Smav } 1731195801Smav if (timeout >= 100) { 1732195801Smav if (bootverbose) { 1733195801Smav device_printf(ch->dev, "SATA connect timeout status=%08x\n", 1734195801Smav status); 1735195801Smav } 1736195801Smav return (0); 1737195801Smav } 1738195801Smav if (bootverbose) { 1739195801Smav device_printf(ch->dev, "SATA connect time=%dms status=%08x\n", 1740195801Smav timeout, status); 1741195801Smav } 1742195801Smav /* Clear SATA error register */ 1743195801Smav ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff); 1744195801Smav return (1); 1745195801Smav} 1746195801Smav 1747207431Smavstatic int 1748207431Smavsiis_check_ids(device_t dev, union ccb *ccb) 1749207431Smav{ 1750207431Smav 1751207431Smav if (ccb->ccb_h.target_id > 15) { 1752207431Smav ccb->ccb_h.status = CAM_TID_INVALID; 1753207431Smav xpt_done(ccb); 1754207431Smav return (-1); 1755207431Smav } 1756207431Smav if (ccb->ccb_h.target_lun != 0) { 1757207431Smav ccb->ccb_h.status = CAM_LUN_INVALID; 1758207431Smav xpt_done(ccb); 1759207431Smav return (-1); 1760207431Smav } 1761207431Smav return (0); 1762207431Smav} 1763207431Smav 1764195801Smavstatic void 1765195801Smavsiisaction(struct cam_sim *sim, union ccb *ccb) 1766195801Smav{ 1767210471Smav device_t dev, parent; 1768195801Smav struct siis_channel *ch; 1769195801Smav 1770195801Smav CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n", 1771195801Smav ccb->ccb_h.func_code)); 1772195801Smav 1773195801Smav ch = (struct siis_channel *)cam_sim_softc(sim); 1774195801Smav dev = ch->dev; 1775195801Smav mtx_assert(&ch->mtx, MA_OWNED); 1776195801Smav switch (ccb->ccb_h.func_code) { 1777195801Smav /* Common cases first */ 1778195801Smav case XPT_ATA_IO: /* Execute the requested I/O operation */ 1779195801Smav case XPT_SCSI_IO: 1780207431Smav if (siis_check_ids(dev, ccb)) 1781207431Smav return; 1782207431Smav if (ch->devices == 0 || 1783207431Smav (ch->pm_present == 0 && 1784207431Smav ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 1785195801Smav ccb->ccb_h.status = CAM_SEL_TIMEOUT; 1786195801Smav break; 1787195801Smav } 1788220566Smav ccb->ccb_h.recovery_type = RECOVERY_NONE; 1789195801Smav /* Check for command collision. */ 1790195801Smav if (siis_check_collision(dev, ccb)) { 1791195801Smav /* Freeze command. */ 1792195801Smav ch->frozen = ccb; 1793195801Smav /* We have only one frozen slot, so freeze simq also. */ 1794195801Smav xpt_freeze_simq(ch->sim, 1); 1795195801Smav return; 1796195801Smav } 1797195801Smav siis_begin_transaction(dev, ccb); 1798207431Smav return; 1799195801Smav case XPT_EN_LUN: /* Enable LUN as a target */ 1800195801Smav case XPT_TARGET_IO: /* Execute target I/O request */ 1801195801Smav case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1802195801Smav case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1803195801Smav case XPT_ABORT: /* Abort the specified CCB */ 1804195801Smav /* XXX Implement */ 1805195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1806195801Smav break; 1807195801Smav case XPT_SET_TRAN_SETTINGS: 1808195801Smav { 1809195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1810199747Smav struct siis_device *d; 1811195801Smav 1812207431Smav if (siis_check_ids(dev, ccb)) 1813207431Smav return; 1814199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1815199747Smav d = &ch->curr[ccb->ccb_h.target_id]; 1816199747Smav else 1817199747Smav d = &ch->user[ccb->ccb_h.target_id]; 1818199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 1819199747Smav d->revision = cts->xport_specific.sata.revision; 1820199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 1821199747Smav d->mode = cts->xport_specific.sata.mode; 1822199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 1823199747Smav d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 1824199747Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1825199747Smav d->tags = min(SIIS_MAX_SLOTS, cts->xport_specific.sata.tags); 1826195801Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 1827198852Smav ch->pm_present = cts->xport_specific.sata.pm_present; 1828198852Smav if (ch->pm_present) 1829195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME); 1830195801Smav else 1831195801Smav ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME); 1832195801Smav } 1833203376Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 1834203376Smav d->atapi = cts->xport_specific.sata.atapi; 1835207499Smav if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1836207499Smav d->caps = cts->xport_specific.sata.caps; 1837195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1838195801Smav break; 1839195801Smav } 1840195801Smav case XPT_GET_TRAN_SETTINGS: 1841195801Smav /* Get default/user set transfer settings for the target */ 1842195801Smav { 1843195801Smav struct ccb_trans_settings *cts = &ccb->cts; 1844199747Smav struct siis_device *d; 1845195801Smav uint32_t status; 1846195801Smav 1847207431Smav if (siis_check_ids(dev, ccb)) 1848207431Smav return; 1849199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 1850199747Smav d = &ch->curr[ccb->ccb_h.target_id]; 1851199747Smav else 1852199747Smav d = &ch->user[ccb->ccb_h.target_id]; 1853195801Smav cts->protocol = PROTO_ATA; 1854196655Smav cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 1855195801Smav cts->transport = XPORT_SATA; 1856196655Smav cts->transport_version = XPORT_VERSION_UNSPECIFIED; 1857195801Smav cts->proto_specific.valid = 0; 1858195801Smav cts->xport_specific.sata.valid = 0; 1859199747Smav if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1860199747Smav (ccb->ccb_h.target_id == 15 || 1861199747Smav (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 1862195801Smav status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK; 1863199747Smav if (status & 0x0f0) { 1864199747Smav cts->xport_specific.sata.revision = 1865199747Smav (status & 0x0f0) >> 4; 1866199747Smav cts->xport_specific.sata.valid |= 1867199747Smav CTS_SATA_VALID_REVISION; 1868199747Smav } 1869207499Smav cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 1870207499Smav if (ch->pm_level) 1871207499Smav cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 1872207499Smav cts->xport_specific.sata.caps &= 1873207499Smav ch->user[ccb->ccb_h.target_id].caps; 1874207499Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1875199747Smav } else { 1876199747Smav cts->xport_specific.sata.revision = d->revision; 1877199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 1878207499Smav cts->xport_specific.sata.caps = d->caps; 1879207499Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 1880195801Smav } 1881199747Smav cts->xport_specific.sata.mode = d->mode; 1882199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 1883199747Smav cts->xport_specific.sata.bytecount = d->bytecount; 1884199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 1885198852Smav cts->xport_specific.sata.pm_present = ch->pm_present; 1886195801Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 1887199747Smav cts->xport_specific.sata.tags = d->tags; 1888199747Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 1889203376Smav cts->xport_specific.sata.atapi = d->atapi; 1890203376Smav cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 1891195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1892195801Smav break; 1893195801Smav } 1894195801Smav case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1895195801Smav case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1896195801Smav siis_reset(dev); 1897195801Smav ccb->ccb_h.status = CAM_REQ_CMP; 1898195801Smav break; 1899195801Smav case XPT_TERM_IO: /* Terminate the I/O process */ 1900195801Smav /* XXX Implement */ 1901195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1902195801Smav break; 1903195801Smav case XPT_PATH_INQ: /* Path routing inquiry */ 1904195801Smav { 1905195801Smav struct ccb_pathinq *cpi = &ccb->cpi; 1906195801Smav 1907210471Smav parent = device_get_parent(dev); 1908195801Smav cpi->version_num = 1; /* XXX??? */ 1909195801Smav cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE; 1910195801Smav cpi->hba_inquiry |= PI_SATAPM; 1911195801Smav cpi->target_sprt = 0; 1912195801Smav cpi->hba_misc = PIM_SEQSCAN; 1913195801Smav cpi->hba_eng_cnt = 0; 1914198322Smav cpi->max_target = 15; 1915195801Smav cpi->max_lun = 0; 1916195801Smav cpi->initiator_id = 0; 1917195801Smav cpi->bus_id = cam_sim_bus(sim); 1918195801Smav cpi->base_transfer_speed = 150000; 1919195801Smav strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1920195801Smav strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN); 1921195801Smav strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1922195801Smav cpi->unit_number = cam_sim_unit(sim); 1923195801Smav cpi->transport = XPORT_SATA; 1924196655Smav cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 1925195801Smav cpi->protocol = PROTO_ATA; 1926196655Smav cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 1927210471Smav cpi->maxio = MAXPHYS; 1928210471Smav cpi->hba_vendor = pci_get_vendor(parent); 1929210471Smav cpi->hba_device = pci_get_device(parent); 1930210471Smav cpi->hba_subvendor = pci_get_subvendor(parent); 1931210471Smav cpi->hba_subdevice = pci_get_subdevice(parent); 1932195801Smav cpi->ccb_h.status = CAM_REQ_CMP; 1933195801Smav break; 1934195801Smav } 1935195801Smav default: 1936195801Smav ccb->ccb_h.status = CAM_REQ_INVALID; 1937195801Smav break; 1938195801Smav } 1939207431Smav xpt_done(ccb); 1940195801Smav} 1941195801Smav 1942195801Smavstatic void 1943195801Smavsiispoll(struct cam_sim *sim) 1944195801Smav{ 1945195801Smav struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim); 1946195801Smav 1947195801Smav siis_ch_intr(ch->dev); 1948195801Smav} 1949