1dd48af36SAlexander Motin /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro F. Giffuni * 4dd48af36SAlexander Motin * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 5dd48af36SAlexander Motin * All rights reserved. 6dd48af36SAlexander Motin * 7dd48af36SAlexander Motin * Redistribution and use in source and binary forms, with or without 8dd48af36SAlexander Motin * modification, are permitted provided that the following conditions 9dd48af36SAlexander Motin * are met: 10dd48af36SAlexander Motin * 1. Redistributions of source code must retain the above copyright 11dd48af36SAlexander Motin * notice, this list of conditions and the following disclaimer, 12dd48af36SAlexander Motin * without modification, immediately at the beginning of the file. 13dd48af36SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 14dd48af36SAlexander Motin * notice, this list of conditions and the following disclaimer in the 15dd48af36SAlexander Motin * documentation and/or other materials provided with the distribution. 16dd48af36SAlexander Motin * 17dd48af36SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18dd48af36SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19dd48af36SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20dd48af36SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21dd48af36SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22dd48af36SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23dd48af36SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24dd48af36SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25dd48af36SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26dd48af36SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27dd48af36SAlexander Motin */ 28dd48af36SAlexander Motin 29dd48af36SAlexander Motin #include <sys/cdefs.h> 30dd48af36SAlexander Motin __FBSDID("$FreeBSD$"); 31dd48af36SAlexander Motin 32dd48af36SAlexander Motin #include <sys/param.h> 33dd48af36SAlexander Motin #include <sys/module.h> 34dd48af36SAlexander Motin #include <sys/systm.h> 35dd48af36SAlexander Motin #include <sys/kernel.h> 36dd48af36SAlexander Motin #include <sys/ata.h> 37dd48af36SAlexander Motin #include <sys/bus.h> 3870b7af2bSAlexander Motin #include <sys/conf.h> 39dd48af36SAlexander Motin #include <sys/endian.h> 40dd48af36SAlexander Motin #include <sys/malloc.h> 41dd48af36SAlexander Motin #include <sys/lock.h> 42dd48af36SAlexander Motin #include <sys/mutex.h> 43dd48af36SAlexander Motin #include <vm/uma.h> 44dd48af36SAlexander Motin #include <machine/stdarg.h> 45dd48af36SAlexander Motin #include <machine/resource.h> 46dd48af36SAlexander Motin #include <machine/bus.h> 47dd48af36SAlexander Motin #include <sys/rman.h> 488edcf694SAlexander Motin #include <dev/pci/pcivar.h> 49dd48af36SAlexander Motin #include "mvs.h" 50dd48af36SAlexander Motin 51dd48af36SAlexander Motin #include <cam/cam.h> 52dd48af36SAlexander Motin #include <cam/cam_ccb.h> 53dd48af36SAlexander Motin #include <cam/cam_sim.h> 54dd48af36SAlexander Motin #include <cam/cam_xpt_sim.h> 55dd48af36SAlexander Motin #include <cam/cam_debug.h> 56dd48af36SAlexander Motin 57dd48af36SAlexander Motin /* local prototypes */ 58243e0fb9SAlexander Motin static int mvs_ch_init(device_t dev); 59243e0fb9SAlexander Motin static int mvs_ch_deinit(device_t dev); 60dd48af36SAlexander Motin static int mvs_ch_suspend(device_t dev); 61dd48af36SAlexander Motin static int mvs_ch_resume(device_t dev); 62dd48af36SAlexander Motin static void mvs_dmainit(device_t dev); 63c0609c54SAlexander Motin static void mvs_dmasetupc_cb(void *xsc, 64c0609c54SAlexander Motin bus_dma_segment_t *segs, int nsegs, int error); 65dd48af36SAlexander Motin static void mvs_dmafini(device_t dev); 66dd48af36SAlexander Motin static void mvs_slotsalloc(device_t dev); 67dd48af36SAlexander Motin static void mvs_slotsfree(device_t dev); 68dd48af36SAlexander Motin static void mvs_setup_edma_queues(device_t dev); 69dd48af36SAlexander Motin static void mvs_set_edma_mode(device_t dev, enum mvs_edma_mode mode); 70dd48af36SAlexander Motin static void mvs_ch_pm(void *arg); 71dd48af36SAlexander Motin static void mvs_ch_intr_locked(void *data); 72dd48af36SAlexander Motin static void mvs_ch_intr(void *data); 73dd48af36SAlexander Motin static void mvs_reset(device_t dev); 74dd48af36SAlexander Motin static void mvs_softreset(device_t dev, union ccb *ccb); 75dd48af36SAlexander Motin 76dd48af36SAlexander Motin static int mvs_sata_connect(struct mvs_channel *ch); 77dd48af36SAlexander Motin static int mvs_sata_phy_reset(device_t dev); 78dd48af36SAlexander Motin static int mvs_wait(device_t dev, u_int s, u_int c, int t); 79dd48af36SAlexander Motin static void mvs_tfd_read(device_t dev, union ccb *ccb); 80dd48af36SAlexander Motin static void mvs_tfd_write(device_t dev, union ccb *ccb); 8170b7af2bSAlexander Motin static void mvs_legacy_intr(device_t dev, int poll); 82dd48af36SAlexander Motin static void mvs_crbq_intr(device_t dev); 83dd48af36SAlexander Motin static void mvs_begin_transaction(device_t dev, union ccb *ccb); 84dd48af36SAlexander Motin static void mvs_legacy_execute_transaction(struct mvs_slot *slot); 85dd48af36SAlexander Motin static void mvs_timeout(struct mvs_slot *slot); 86c0609c54SAlexander Motin static void mvs_dmasetprd(void *arg, 87c0609c54SAlexander Motin bus_dma_segment_t *segs, int nsegs, int error); 88dd48af36SAlexander Motin static void mvs_requeue_frozen(device_t dev); 89dd48af36SAlexander Motin static void mvs_execute_transaction(struct mvs_slot *slot); 90dd48af36SAlexander Motin static void mvs_end_transaction(struct mvs_slot *slot, enum mvs_err_type et); 91dd48af36SAlexander Motin 9297fd3ac6SAlexander Motin static void mvs_issue_recovery(device_t dev); 93dd48af36SAlexander Motin static void mvs_process_read_log(device_t dev, union ccb *ccb); 9497fd3ac6SAlexander Motin static void mvs_process_request_sense(device_t dev, union ccb *ccb); 95dd48af36SAlexander Motin 96dd48af36SAlexander Motin static void mvsaction(struct cam_sim *sim, union ccb *ccb); 97dd48af36SAlexander Motin static void mvspoll(struct cam_sim *sim); 98dd48af36SAlexander Motin 99d745c852SEd Schouten static MALLOC_DEFINE(M_MVS, "MVS driver", "MVS driver data buffers"); 100dd48af36SAlexander Motin 10197fd3ac6SAlexander Motin #define recovery_type spriv_field0 10297fd3ac6SAlexander Motin #define RECOVERY_NONE 0 10397fd3ac6SAlexander Motin #define RECOVERY_READ_LOG 1 10497fd3ac6SAlexander Motin #define RECOVERY_REQUEST_SENSE 2 10597fd3ac6SAlexander Motin #define recovery_slot spriv_field1 10697fd3ac6SAlexander Motin 107dd48af36SAlexander Motin static int 108dd48af36SAlexander Motin mvs_ch_probe(device_t dev) 109dd48af36SAlexander Motin { 110dd48af36SAlexander Motin 111dd48af36SAlexander Motin device_set_desc_copy(dev, "Marvell SATA channel"); 1123036de3cSAlexander Motin return (BUS_PROBE_DEFAULT); 113dd48af36SAlexander Motin } 114dd48af36SAlexander Motin 115dd48af36SAlexander Motin static int 116dd48af36SAlexander Motin mvs_ch_attach(device_t dev) 117dd48af36SAlexander Motin { 118dd48af36SAlexander Motin struct mvs_controller *ctlr = device_get_softc(device_get_parent(dev)); 119dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 120dd48af36SAlexander Motin struct cam_devq *devq; 121dd48af36SAlexander Motin int rid, error, i, sata_rev = 0; 122dd48af36SAlexander Motin 123dd48af36SAlexander Motin ch->dev = dev; 124dd48af36SAlexander Motin ch->unit = (intptr_t)device_get_ivars(dev); 125dd48af36SAlexander Motin ch->quirks = ctlr->quirks; 126dd48af36SAlexander Motin mtx_init(&ch->mtx, "MVS channel lock", NULL, MTX_DEF); 127200b4021SAlexander Motin ch->pm_level = 0; 128dd48af36SAlexander Motin resource_int_value(device_get_name(dev), 129dd48af36SAlexander Motin device_get_unit(dev), "pm_level", &ch->pm_level); 130dd48af36SAlexander Motin if (ch->pm_level > 3) 131dd48af36SAlexander Motin callout_init_mtx(&ch->pm_timer, &ch->mtx, 0); 13270b7af2bSAlexander Motin callout_init_mtx(&ch->reset_timer, &ch->mtx, 0); 133dd48af36SAlexander Motin resource_int_value(device_get_name(dev), 134dd48af36SAlexander Motin device_get_unit(dev), "sata_rev", &sata_rev); 135dd48af36SAlexander Motin for (i = 0; i < 16; i++) { 136dd48af36SAlexander Motin ch->user[i].revision = sata_rev; 137dd48af36SAlexander Motin ch->user[i].mode = 0; 138dd48af36SAlexander Motin ch->user[i].bytecount = (ch->quirks & MVS_Q_GENIIE) ? 8192 : 2048; 139dd48af36SAlexander Motin ch->user[i].tags = MVS_MAX_SLOTS; 140dd48af36SAlexander Motin ch->curr[i] = ch->user[i]; 141dd48af36SAlexander Motin if (ch->pm_level) { 142dd48af36SAlexander Motin ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ | 143dd48af36SAlexander Motin CTS_SATA_CAPS_H_APST | 144dd48af36SAlexander Motin CTS_SATA_CAPS_D_PMREQ | CTS_SATA_CAPS_D_APST; 145dd48af36SAlexander Motin } 1468d169381SAlexander Motin ch->user[i].caps |= CTS_SATA_CAPS_H_AN; 147dd48af36SAlexander Motin } 148dd48af36SAlexander Motin rid = ch->unit; 149dd48af36SAlexander Motin if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 150dd48af36SAlexander Motin &rid, RF_ACTIVE))) 151dd48af36SAlexander Motin return (ENXIO); 152dd48af36SAlexander Motin mvs_dmainit(dev); 153dd48af36SAlexander Motin mvs_slotsalloc(dev); 154243e0fb9SAlexander Motin mvs_ch_init(dev); 155dd48af36SAlexander Motin mtx_lock(&ch->mtx); 156dd48af36SAlexander Motin rid = ATA_IRQ_RID; 157dd48af36SAlexander Motin if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 158dd48af36SAlexander Motin &rid, RF_SHAREABLE | RF_ACTIVE))) { 159dd48af36SAlexander Motin device_printf(dev, "Unable to map interrupt\n"); 160dd48af36SAlexander Motin error = ENXIO; 161dd48af36SAlexander Motin goto err0; 162dd48af36SAlexander Motin } 163dd48af36SAlexander Motin if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 164dd48af36SAlexander Motin mvs_ch_intr_locked, dev, &ch->ih))) { 165dd48af36SAlexander Motin device_printf(dev, "Unable to setup interrupt\n"); 166dd48af36SAlexander Motin error = ENXIO; 167dd48af36SAlexander Motin goto err1; 168dd48af36SAlexander Motin } 169dd48af36SAlexander Motin /* Create the device queue for our SIM. */ 170dd48af36SAlexander Motin devq = cam_simq_alloc(MVS_MAX_SLOTS - 1); 171dd48af36SAlexander Motin if (devq == NULL) { 172dd48af36SAlexander Motin device_printf(dev, "Unable to allocate simq\n"); 173dd48af36SAlexander Motin error = ENOMEM; 174dd48af36SAlexander Motin goto err1; 175dd48af36SAlexander Motin } 176dd48af36SAlexander Motin /* Construct SIM entry */ 177dd48af36SAlexander Motin ch->sim = cam_sim_alloc(mvsaction, mvspoll, "mvsch", ch, 178dd48af36SAlexander Motin device_get_unit(dev), &ch->mtx, 179dd48af36SAlexander Motin 2, (ch->quirks & MVS_Q_GENI) ? 0 : MVS_MAX_SLOTS - 1, 180dd48af36SAlexander Motin devq); 181dd48af36SAlexander Motin if (ch->sim == NULL) { 182dd48af36SAlexander Motin cam_simq_free(devq); 183dd48af36SAlexander Motin device_printf(dev, "unable to allocate sim\n"); 184dd48af36SAlexander Motin error = ENOMEM; 185dd48af36SAlexander Motin goto err1; 186dd48af36SAlexander Motin } 187dd48af36SAlexander Motin if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 188dd48af36SAlexander Motin device_printf(dev, "unable to register xpt bus\n"); 189dd48af36SAlexander Motin error = ENXIO; 190dd48af36SAlexander Motin goto err2; 191dd48af36SAlexander Motin } 192dd48af36SAlexander Motin if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 193dd48af36SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 194dd48af36SAlexander Motin device_printf(dev, "unable to create path\n"); 195dd48af36SAlexander Motin error = ENXIO; 196dd48af36SAlexander Motin goto err3; 197dd48af36SAlexander Motin } 198dd48af36SAlexander Motin if (ch->pm_level > 3) { 199dd48af36SAlexander Motin callout_reset(&ch->pm_timer, 200dd48af36SAlexander Motin (ch->pm_level == 4) ? hz / 1000 : hz / 8, 201dd48af36SAlexander Motin mvs_ch_pm, dev); 202dd48af36SAlexander Motin } 203dd48af36SAlexander Motin mtx_unlock(&ch->mtx); 204dd48af36SAlexander Motin return (0); 205dd48af36SAlexander Motin 206dd48af36SAlexander Motin err3: 207dd48af36SAlexander Motin xpt_bus_deregister(cam_sim_path(ch->sim)); 208dd48af36SAlexander Motin err2: 209dd48af36SAlexander Motin cam_sim_free(ch->sim, /*free_devq*/TRUE); 210dd48af36SAlexander Motin err1: 211dd48af36SAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 212dd48af36SAlexander Motin err0: 213dd48af36SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 214dd48af36SAlexander Motin mtx_unlock(&ch->mtx); 215dd48af36SAlexander Motin mtx_destroy(&ch->mtx); 216dd48af36SAlexander Motin return (error); 217dd48af36SAlexander Motin } 218dd48af36SAlexander Motin 219dd48af36SAlexander Motin static int 220dd48af36SAlexander Motin mvs_ch_detach(device_t dev) 221dd48af36SAlexander Motin { 222dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 223dd48af36SAlexander Motin 224dd48af36SAlexander Motin mtx_lock(&ch->mtx); 225dd48af36SAlexander Motin xpt_async(AC_LOST_DEVICE, ch->path, NULL); 22670b7af2bSAlexander Motin /* Forget about reset. */ 22770b7af2bSAlexander Motin if (ch->resetting) { 22870b7af2bSAlexander Motin ch->resetting = 0; 22970b7af2bSAlexander Motin xpt_release_simq(ch->sim, TRUE); 23070b7af2bSAlexander Motin } 231dd48af36SAlexander Motin xpt_free_path(ch->path); 232dd48af36SAlexander Motin xpt_bus_deregister(cam_sim_path(ch->sim)); 233dd48af36SAlexander Motin cam_sim_free(ch->sim, /*free_devq*/TRUE); 234dd48af36SAlexander Motin mtx_unlock(&ch->mtx); 235dd48af36SAlexander Motin 236dd48af36SAlexander Motin if (ch->pm_level > 3) 237dd48af36SAlexander Motin callout_drain(&ch->pm_timer); 23870b7af2bSAlexander Motin callout_drain(&ch->reset_timer); 239dd48af36SAlexander Motin bus_teardown_intr(dev, ch->r_irq, ch->ih); 240dd48af36SAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 241dd48af36SAlexander Motin 242243e0fb9SAlexander Motin mvs_ch_deinit(dev); 243dd48af36SAlexander Motin mvs_slotsfree(dev); 244dd48af36SAlexander Motin mvs_dmafini(dev); 245dd48af36SAlexander Motin 246dd48af36SAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 247dd48af36SAlexander Motin mtx_destroy(&ch->mtx); 248dd48af36SAlexander Motin return (0); 249dd48af36SAlexander Motin } 250dd48af36SAlexander Motin 251dd48af36SAlexander Motin static int 252243e0fb9SAlexander Motin mvs_ch_init(device_t dev) 253dd48af36SAlexander Motin { 254dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 255dd48af36SAlexander Motin uint32_t reg; 256dd48af36SAlexander Motin 257dd48af36SAlexander Motin /* Disable port interrupts */ 258dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, 0); 259dd48af36SAlexander Motin /* Stop EDMA */ 260dd48af36SAlexander Motin ch->curr_mode = MVS_EDMA_UNKNOWN; 261dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 262dd48af36SAlexander Motin /* Clear and configure FIS interrupts. */ 263dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_FISIC, 0); 264dd48af36SAlexander Motin reg = ATA_INL(ch->r_mem, SATA_FISC); 265dd48af36SAlexander Motin reg |= SATA_FISC_FISWAIT4HOSTRDYEN_B1; 266dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_FISC, reg); 267dd48af36SAlexander Motin reg = ATA_INL(ch->r_mem, SATA_FISIM); 268dd48af36SAlexander Motin reg |= SATA_FISC_FISWAIT4HOSTRDYEN_B1; 269dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_FISC, reg); 270dd48af36SAlexander Motin /* Clear SATA error register. */ 271dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff); 272dd48af36SAlexander Motin /* Clear any outstanding error interrupts. */ 273dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEC, 0); 274dd48af36SAlexander Motin /* Unmask all error interrupts */ 275dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT); 276dd48af36SAlexander Motin return (0); 277dd48af36SAlexander Motin } 278dd48af36SAlexander Motin 279243e0fb9SAlexander Motin static int 280243e0fb9SAlexander Motin mvs_ch_deinit(device_t dev) 281243e0fb9SAlexander Motin { 282243e0fb9SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 283243e0fb9SAlexander Motin 284243e0fb9SAlexander Motin /* Stop EDMA */ 285243e0fb9SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 286243e0fb9SAlexander Motin /* Disable port interrupts. */ 287243e0fb9SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, 0); 288243e0fb9SAlexander Motin return (0); 289243e0fb9SAlexander Motin } 290243e0fb9SAlexander Motin 291243e0fb9SAlexander Motin static int 292243e0fb9SAlexander Motin mvs_ch_suspend(device_t dev) 293243e0fb9SAlexander Motin { 294243e0fb9SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 295243e0fb9SAlexander Motin 296243e0fb9SAlexander Motin mtx_lock(&ch->mtx); 297243e0fb9SAlexander Motin xpt_freeze_simq(ch->sim, 1); 298243e0fb9SAlexander Motin while (ch->oslots) 299243e0fb9SAlexander Motin msleep(ch, &ch->mtx, PRIBIO, "mvssusp", hz/100); 30070b7af2bSAlexander Motin /* Forget about reset. */ 30170b7af2bSAlexander Motin if (ch->resetting) { 30270b7af2bSAlexander Motin ch->resetting = 0; 30370b7af2bSAlexander Motin callout_stop(&ch->reset_timer); 30470b7af2bSAlexander Motin xpt_release_simq(ch->sim, TRUE); 30570b7af2bSAlexander Motin } 306243e0fb9SAlexander Motin mvs_ch_deinit(dev); 307243e0fb9SAlexander Motin mtx_unlock(&ch->mtx); 308243e0fb9SAlexander Motin return (0); 309243e0fb9SAlexander Motin } 310243e0fb9SAlexander Motin 311243e0fb9SAlexander Motin static int 312243e0fb9SAlexander Motin mvs_ch_resume(device_t dev) 313243e0fb9SAlexander Motin { 314243e0fb9SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 315243e0fb9SAlexander Motin 316243e0fb9SAlexander Motin mtx_lock(&ch->mtx); 317243e0fb9SAlexander Motin mvs_ch_init(dev); 318243e0fb9SAlexander Motin mvs_reset(dev); 319243e0fb9SAlexander Motin xpt_release_simq(ch->sim, TRUE); 320243e0fb9SAlexander Motin mtx_unlock(&ch->mtx); 321243e0fb9SAlexander Motin return (0); 322243e0fb9SAlexander Motin } 323243e0fb9SAlexander Motin 324dd48af36SAlexander Motin struct mvs_dc_cb_args { 325dd48af36SAlexander Motin bus_addr_t maddr; 326dd48af36SAlexander Motin int error; 327dd48af36SAlexander Motin }; 328dd48af36SAlexander Motin 329dd48af36SAlexander Motin static void 330dd48af36SAlexander Motin mvs_dmainit(device_t dev) 331dd48af36SAlexander Motin { 332dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 333dd48af36SAlexander Motin struct mvs_dc_cb_args dcba; 334dd48af36SAlexander Motin 335dd48af36SAlexander Motin /* EDMA command request area. */ 336dd48af36SAlexander Motin if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 337dd48af36SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 338dd48af36SAlexander Motin NULL, NULL, MVS_WORKRQ_SIZE, 1, MVS_WORKRQ_SIZE, 339dd48af36SAlexander Motin 0, NULL, NULL, &ch->dma.workrq_tag)) 340dd48af36SAlexander Motin goto error; 341dd48af36SAlexander Motin if (bus_dmamem_alloc(ch->dma.workrq_tag, (void **)&ch->dma.workrq, 0, 342dd48af36SAlexander Motin &ch->dma.workrq_map)) 343dd48af36SAlexander Motin goto error; 344c0609c54SAlexander Motin if (bus_dmamap_load(ch->dma.workrq_tag, ch->dma.workrq_map, 345c0609c54SAlexander Motin ch->dma.workrq, MVS_WORKRQ_SIZE, mvs_dmasetupc_cb, &dcba, 0) || 346c0609c54SAlexander Motin dcba.error) { 347c0609c54SAlexander Motin bus_dmamem_free(ch->dma.workrq_tag, 348c0609c54SAlexander Motin ch->dma.workrq, ch->dma.workrq_map); 349dd48af36SAlexander Motin goto error; 350dd48af36SAlexander Motin } 351dd48af36SAlexander Motin ch->dma.workrq_bus = dcba.maddr; 352dd48af36SAlexander Motin /* EDMA command response area. */ 353dd48af36SAlexander Motin if (bus_dma_tag_create(bus_get_dma_tag(dev), 256, 0, 354dd48af36SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 355dd48af36SAlexander Motin NULL, NULL, MVS_WORKRP_SIZE, 1, MVS_WORKRP_SIZE, 356dd48af36SAlexander Motin 0, NULL, NULL, &ch->dma.workrp_tag)) 357dd48af36SAlexander Motin goto error; 358dd48af36SAlexander Motin if (bus_dmamem_alloc(ch->dma.workrp_tag, (void **)&ch->dma.workrp, 0, 359dd48af36SAlexander Motin &ch->dma.workrp_map)) 360dd48af36SAlexander Motin goto error; 361c0609c54SAlexander Motin if (bus_dmamap_load(ch->dma.workrp_tag, ch->dma.workrp_map, 362c0609c54SAlexander Motin ch->dma.workrp, MVS_WORKRP_SIZE, mvs_dmasetupc_cb, &dcba, 0) || 363c0609c54SAlexander Motin dcba.error) { 364c0609c54SAlexander Motin bus_dmamem_free(ch->dma.workrp_tag, 365c0609c54SAlexander Motin ch->dma.workrp, ch->dma.workrp_map); 366dd48af36SAlexander Motin goto error; 367dd48af36SAlexander Motin } 368dd48af36SAlexander Motin ch->dma.workrp_bus = dcba.maddr; 369dd48af36SAlexander Motin /* Data area. */ 370dd48af36SAlexander Motin if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, MVS_EPRD_MAX, 371dd48af36SAlexander Motin BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 372dd48af36SAlexander Motin NULL, NULL, 373dd48af36SAlexander Motin MVS_SG_ENTRIES * PAGE_SIZE * MVS_MAX_SLOTS, 374dd48af36SAlexander Motin MVS_SG_ENTRIES, MVS_EPRD_MAX, 375dd48af36SAlexander Motin 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 376dd48af36SAlexander Motin goto error; 377dd48af36SAlexander Motin } 378dd48af36SAlexander Motin return; 379dd48af36SAlexander Motin 380dd48af36SAlexander Motin error: 381dd48af36SAlexander Motin device_printf(dev, "WARNING - DMA initialization failed\n"); 382dd48af36SAlexander Motin mvs_dmafini(dev); 383dd48af36SAlexander Motin } 384dd48af36SAlexander Motin 385dd48af36SAlexander Motin static void 386dd48af36SAlexander Motin mvs_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 387dd48af36SAlexander Motin { 388dd48af36SAlexander Motin struct mvs_dc_cb_args *dcba = (struct mvs_dc_cb_args *)xsc; 389dd48af36SAlexander Motin 390dd48af36SAlexander Motin if (!(dcba->error = error)) 391dd48af36SAlexander Motin dcba->maddr = segs[0].ds_addr; 392dd48af36SAlexander Motin } 393dd48af36SAlexander Motin 394dd48af36SAlexander Motin static void 395dd48af36SAlexander Motin mvs_dmafini(device_t dev) 396dd48af36SAlexander Motin { 397dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 398dd48af36SAlexander Motin 399dd48af36SAlexander Motin if (ch->dma.data_tag) { 400dd48af36SAlexander Motin bus_dma_tag_destroy(ch->dma.data_tag); 401dd48af36SAlexander Motin ch->dma.data_tag = NULL; 402dd48af36SAlexander Motin } 403dd48af36SAlexander Motin if (ch->dma.workrp_bus) { 404dd48af36SAlexander Motin bus_dmamap_unload(ch->dma.workrp_tag, ch->dma.workrp_map); 405c0609c54SAlexander Motin bus_dmamem_free(ch->dma.workrp_tag, 406c0609c54SAlexander Motin ch->dma.workrp, ch->dma.workrp_map); 407dd48af36SAlexander Motin ch->dma.workrp_bus = 0; 408dd48af36SAlexander Motin ch->dma.workrp = NULL; 409dd48af36SAlexander Motin } 410dd48af36SAlexander Motin if (ch->dma.workrp_tag) { 411dd48af36SAlexander Motin bus_dma_tag_destroy(ch->dma.workrp_tag); 412dd48af36SAlexander Motin ch->dma.workrp_tag = NULL; 413dd48af36SAlexander Motin } 414dd48af36SAlexander Motin if (ch->dma.workrq_bus) { 415dd48af36SAlexander Motin bus_dmamap_unload(ch->dma.workrq_tag, ch->dma.workrq_map); 416c0609c54SAlexander Motin bus_dmamem_free(ch->dma.workrq_tag, 417c0609c54SAlexander Motin ch->dma.workrq, ch->dma.workrq_map); 418dd48af36SAlexander Motin ch->dma.workrq_bus = 0; 419dd48af36SAlexander Motin ch->dma.workrq = NULL; 420dd48af36SAlexander Motin } 421dd48af36SAlexander Motin if (ch->dma.workrq_tag) { 422dd48af36SAlexander Motin bus_dma_tag_destroy(ch->dma.workrq_tag); 423dd48af36SAlexander Motin ch->dma.workrq_tag = NULL; 424dd48af36SAlexander Motin } 425dd48af36SAlexander Motin } 426dd48af36SAlexander Motin 427dd48af36SAlexander Motin static void 428dd48af36SAlexander Motin mvs_slotsalloc(device_t dev) 429dd48af36SAlexander Motin { 430dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 431dd48af36SAlexander Motin int i; 432dd48af36SAlexander Motin 433dd48af36SAlexander Motin /* Alloc and setup command/dma slots */ 434dd48af36SAlexander Motin bzero(ch->slot, sizeof(ch->slot)); 435dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 436dd48af36SAlexander Motin struct mvs_slot *slot = &ch->slot[i]; 437dd48af36SAlexander Motin 438dd48af36SAlexander Motin slot->dev = dev; 439dd48af36SAlexander Motin slot->slot = i; 440dd48af36SAlexander Motin slot->state = MVS_SLOT_EMPTY; 441dd48af36SAlexander Motin slot->ccb = NULL; 442dd48af36SAlexander Motin callout_init_mtx(&slot->timeout, &ch->mtx, 0); 443dd48af36SAlexander Motin 444dd48af36SAlexander Motin if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 445dd48af36SAlexander Motin device_printf(ch->dev, "FAILURE - create data_map\n"); 446dd48af36SAlexander Motin } 447dd48af36SAlexander Motin } 448dd48af36SAlexander Motin 449dd48af36SAlexander Motin static void 450dd48af36SAlexander Motin mvs_slotsfree(device_t dev) 451dd48af36SAlexander Motin { 452dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 453dd48af36SAlexander Motin int i; 454dd48af36SAlexander Motin 455dd48af36SAlexander Motin /* Free all dma slots */ 456dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 457dd48af36SAlexander Motin struct mvs_slot *slot = &ch->slot[i]; 458dd48af36SAlexander Motin 459dd48af36SAlexander Motin callout_drain(&slot->timeout); 460dd48af36SAlexander Motin if (slot->dma.data_map) { 461dd48af36SAlexander Motin bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 462dd48af36SAlexander Motin slot->dma.data_map = NULL; 463dd48af36SAlexander Motin } 464dd48af36SAlexander Motin } 465dd48af36SAlexander Motin } 466dd48af36SAlexander Motin 467dd48af36SAlexander Motin static void 468dd48af36SAlexander Motin mvs_setup_edma_queues(device_t dev) 469dd48af36SAlexander Motin { 470dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 471dd48af36SAlexander Motin uint64_t work; 472dd48af36SAlexander Motin 473dd48af36SAlexander Motin /* Requests queue. */ 474dd48af36SAlexander Motin work = ch->dma.workrq_bus; 475dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_REQQBAH, work >> 32); 476dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_REQQIP, work & 0xffffffff); 477dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_REQQOP, work & 0xffffffff); 478c0609c54SAlexander Motin bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map, 479c0609c54SAlexander Motin BUS_DMASYNC_PREWRITE); 480453130d9SPedro F. Giffuni /* Responses queue. */ 4816c872350SAlexander Motin memset(ch->dma.workrp, 0xff, MVS_WORKRP_SIZE); 482dd48af36SAlexander Motin work = ch->dma.workrp_bus; 483dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_RESQBAH, work >> 32); 484dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_RESQIP, work & 0xffffffff); 485dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_RESQOP, work & 0xffffffff); 486c0609c54SAlexander Motin bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map, 487c0609c54SAlexander Motin BUS_DMASYNC_PREREAD); 488dd48af36SAlexander Motin ch->out_idx = 0; 489dd48af36SAlexander Motin ch->in_idx = 0; 490dd48af36SAlexander Motin } 491dd48af36SAlexander Motin 492dd48af36SAlexander Motin static void 493dd48af36SAlexander Motin mvs_set_edma_mode(device_t dev, enum mvs_edma_mode mode) 494dd48af36SAlexander Motin { 495dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 496dd48af36SAlexander Motin int timeout; 497dd48af36SAlexander Motin uint32_t ecfg, fcfg, hc, ltm, unkn; 498dd48af36SAlexander Motin 499dd48af36SAlexander Motin if (mode == ch->curr_mode) 500dd48af36SAlexander Motin return; 501dd48af36SAlexander Motin /* If we are running, we should stop first. */ 502dd48af36SAlexander Motin if (ch->curr_mode != MVS_EDMA_OFF) { 503dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EDSEDMA); 504dd48af36SAlexander Motin timeout = 0; 505dd48af36SAlexander Motin while (ATA_INL(ch->r_mem, EDMA_CMD) & EDMA_CMD_EENEDMA) { 506dd48af36SAlexander Motin DELAY(1000); 507dd48af36SAlexander Motin if (timeout++ > 1000) { 508dd48af36SAlexander Motin device_printf(dev, "stopping EDMA engine failed\n"); 509dd48af36SAlexander Motin break; 510dd48af36SAlexander Motin } 51174b8d63dSPedro F. Giffuni } 512dd48af36SAlexander Motin } 513dd48af36SAlexander Motin ch->curr_mode = mode; 514dd48af36SAlexander Motin ch->fbs_enabled = 0; 515dd48af36SAlexander Motin ch->fake_busy = 0; 516dd48af36SAlexander Motin /* Report mode to controller. Needed for correct CCC operation. */ 517dd48af36SAlexander Motin MVS_EDMA(device_get_parent(dev), dev, mode); 518dd48af36SAlexander Motin /* Configure new mode. */ 519dd48af36SAlexander Motin ecfg = EDMA_CFG_RESERVED | EDMA_CFG_RESERVED2 | EDMA_CFG_EHOSTQUEUECACHEEN; 520dd48af36SAlexander Motin if (ch->pm_present) { 521dd48af36SAlexander Motin ecfg |= EDMA_CFG_EMASKRXPM; 522dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENIIE) { 523dd48af36SAlexander Motin ecfg |= EDMA_CFG_EEDMAFBS; 524dd48af36SAlexander Motin ch->fbs_enabled = 1; 525dd48af36SAlexander Motin } 526dd48af36SAlexander Motin } 527dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENI) 528dd48af36SAlexander Motin ecfg |= EDMA_CFG_ERDBSZ; 529dd48af36SAlexander Motin else if (ch->quirks & MVS_Q_GENII) 530dd48af36SAlexander Motin ecfg |= EDMA_CFG_ERDBSZEXT | EDMA_CFG_EWRBUFFERLEN; 531dd48af36SAlexander Motin if (ch->quirks & MVS_Q_CT) 532dd48af36SAlexander Motin ecfg |= EDMA_CFG_ECUTTHROUGHEN; 533dd48af36SAlexander Motin if (mode != MVS_EDMA_OFF) 534dd48af36SAlexander Motin ecfg |= EDMA_CFG_EEARLYCOMPLETIONEN; 535dd48af36SAlexander Motin if (mode == MVS_EDMA_QUEUED) 536dd48af36SAlexander Motin ecfg |= EDMA_CFG_EQUE; 537dd48af36SAlexander Motin else if (mode == MVS_EDMA_NCQ) 538dd48af36SAlexander Motin ecfg |= EDMA_CFG_ESATANATVCMDQUE; 539dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_CFG, ecfg); 540dd48af36SAlexander Motin mvs_setup_edma_queues(dev); 541dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENIIE) { 542dd48af36SAlexander Motin /* Configure FBS-related registers */ 543dd48af36SAlexander Motin fcfg = ATA_INL(ch->r_mem, SATA_FISC); 544dd48af36SAlexander Motin ltm = ATA_INL(ch->r_mem, SATA_LTM); 545dd48af36SAlexander Motin hc = ATA_INL(ch->r_mem, EDMA_HC); 546dd48af36SAlexander Motin if (ch->fbs_enabled) { 547dd48af36SAlexander Motin fcfg |= SATA_FISC_FISDMAACTIVATESYNCRESP; 548dd48af36SAlexander Motin if (mode == MVS_EDMA_NCQ) { 549dd48af36SAlexander Motin fcfg &= ~SATA_FISC_FISWAIT4HOSTRDYEN_B0; 550dd48af36SAlexander Motin hc &= ~EDMA_IE_EDEVERR; 551dd48af36SAlexander Motin } else { 552dd48af36SAlexander Motin fcfg |= SATA_FISC_FISWAIT4HOSTRDYEN_B0; 553dd48af36SAlexander Motin hc |= EDMA_IE_EDEVERR; 554dd48af36SAlexander Motin } 555dd48af36SAlexander Motin ltm |= (1 << 8); 556dd48af36SAlexander Motin } else { 557dd48af36SAlexander Motin fcfg &= ~SATA_FISC_FISDMAACTIVATESYNCRESP; 558dd48af36SAlexander Motin fcfg &= ~SATA_FISC_FISWAIT4HOSTRDYEN_B0; 559dd48af36SAlexander Motin hc |= EDMA_IE_EDEVERR; 560dd48af36SAlexander Motin ltm &= ~(1 << 8); 561dd48af36SAlexander Motin } 562dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_FISC, fcfg); 563dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_LTM, ltm); 564dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_HC, hc); 565dd48af36SAlexander Motin /* This is some magic, required to handle several DRQs 566dd48af36SAlexander Motin * with basic DMA. */ 567dd48af36SAlexander Motin unkn = ATA_INL(ch->r_mem, EDMA_UNKN_RESD); 568dd48af36SAlexander Motin if (mode == MVS_EDMA_OFF) 569dd48af36SAlexander Motin unkn |= 1; 570dd48af36SAlexander Motin else 571dd48af36SAlexander Motin unkn &= ~1; 572dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_UNKN_RESD, unkn); 573dd48af36SAlexander Motin } 574dd48af36SAlexander Motin /* Run EDMA. */ 575dd48af36SAlexander Motin if (mode != MVS_EDMA_OFF) 576dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EENEDMA); 577dd48af36SAlexander Motin } 578dd48af36SAlexander Motin 579dd48af36SAlexander Motin devclass_t mvs_devclass; 580dd48af36SAlexander Motin devclass_t mvsch_devclass; 581dd48af36SAlexander Motin static device_method_t mvsch_methods[] = { 582dd48af36SAlexander Motin DEVMETHOD(device_probe, mvs_ch_probe), 583dd48af36SAlexander Motin DEVMETHOD(device_attach, mvs_ch_attach), 584dd48af36SAlexander Motin DEVMETHOD(device_detach, mvs_ch_detach), 585dd48af36SAlexander Motin DEVMETHOD(device_suspend, mvs_ch_suspend), 586dd48af36SAlexander Motin DEVMETHOD(device_resume, mvs_ch_resume), 587dd48af36SAlexander Motin { 0, 0 } 588dd48af36SAlexander Motin }; 589dd48af36SAlexander Motin static driver_t mvsch_driver = { 590dd48af36SAlexander Motin "mvsch", 591dd48af36SAlexander Motin mvsch_methods, 592dd48af36SAlexander Motin sizeof(struct mvs_channel) 593dd48af36SAlexander Motin }; 594dd48af36SAlexander Motin DRIVER_MODULE(mvsch, mvs, mvsch_driver, mvsch_devclass, 0, 0); 595dd48af36SAlexander Motin DRIVER_MODULE(mvsch, sata, mvsch_driver, mvsch_devclass, 0, 0); 596dd48af36SAlexander Motin 597dd48af36SAlexander Motin static void 598dd48af36SAlexander Motin mvs_phy_check_events(device_t dev, u_int32_t serr) 599dd48af36SAlexander Motin { 600dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 601dd48af36SAlexander Motin 602dd48af36SAlexander Motin if (ch->pm_level == 0) { 603dd48af36SAlexander Motin u_int32_t status = ATA_INL(ch->r_mem, SATA_SS); 604dd48af36SAlexander Motin union ccb *ccb; 605dd48af36SAlexander Motin 606dd48af36SAlexander Motin if (bootverbose) { 607dd48af36SAlexander Motin if (((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_ONLINE) && 608dd48af36SAlexander Motin ((status & SATA_SS_SPD_MASK) != SATA_SS_SPD_NO_SPEED) && 609dd48af36SAlexander Motin ((status & SATA_SS_IPM_MASK) == SATA_SS_IPM_ACTIVE)) { 610dd48af36SAlexander Motin device_printf(dev, "CONNECT requested\n"); 611dd48af36SAlexander Motin } else 612dd48af36SAlexander Motin device_printf(dev, "DISCONNECT requested\n"); 613dd48af36SAlexander Motin } 614dd48af36SAlexander Motin mvs_reset(dev); 615dd48af36SAlexander Motin if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 616dd48af36SAlexander Motin return; 617dd48af36SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, 618dd48af36SAlexander Motin cam_sim_path(ch->sim), 619dd48af36SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 620dd48af36SAlexander Motin xpt_free_ccb(ccb); 621dd48af36SAlexander Motin return; 622dd48af36SAlexander Motin } 623dd48af36SAlexander Motin xpt_rescan(ccb); 624dd48af36SAlexander Motin } 625dd48af36SAlexander Motin } 626dd48af36SAlexander Motin 627dd48af36SAlexander Motin static void 628dd48af36SAlexander Motin mvs_notify_events(device_t dev) 629dd48af36SAlexander Motin { 630dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 631dd48af36SAlexander Motin struct cam_path *dpath; 632dd48af36SAlexander Motin uint32_t fis; 633dd48af36SAlexander Motin int d; 634dd48af36SAlexander Motin 635dd48af36SAlexander Motin /* Try to read PMP field from SDB FIS. Present only for Gen-IIe. */ 636dd48af36SAlexander Motin fis = ATA_INL(ch->r_mem, SATA_FISDW0); 637dd48af36SAlexander Motin if ((fis & 0x80ff) == 0x80a1) 638dd48af36SAlexander Motin d = (fis & 0x0f00) >> 8; 639dd48af36SAlexander Motin else 640dd48af36SAlexander Motin d = ch->pm_present ? 15 : 0; 641dd48af36SAlexander Motin if (bootverbose) 642dd48af36SAlexander Motin device_printf(dev, "SNTF %d\n", d); 643dd48af36SAlexander Motin if (xpt_create_path(&dpath, NULL, 644dd48af36SAlexander Motin xpt_path_path_id(ch->path), d, 0) == CAM_REQ_CMP) { 645dd48af36SAlexander Motin xpt_async(AC_SCSI_AEN, dpath, NULL); 646dd48af36SAlexander Motin xpt_free_path(dpath); 647dd48af36SAlexander Motin } 648dd48af36SAlexander Motin } 649dd48af36SAlexander Motin 650dd48af36SAlexander Motin static void 651dd48af36SAlexander Motin mvs_ch_intr_locked(void *data) 652dd48af36SAlexander Motin { 653dd48af36SAlexander Motin struct mvs_intr_arg *arg = (struct mvs_intr_arg *)data; 654dd48af36SAlexander Motin device_t dev = (device_t)arg->arg; 655dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 656dd48af36SAlexander Motin 657dd48af36SAlexander Motin mtx_lock(&ch->mtx); 658dd48af36SAlexander Motin mvs_ch_intr(data); 659dd48af36SAlexander Motin mtx_unlock(&ch->mtx); 660dd48af36SAlexander Motin } 661dd48af36SAlexander Motin 662dd48af36SAlexander Motin static void 663dd48af36SAlexander Motin mvs_ch_pm(void *arg) 664dd48af36SAlexander Motin { 665dd48af36SAlexander Motin device_t dev = (device_t)arg; 666dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 667dd48af36SAlexander Motin uint32_t work; 668dd48af36SAlexander Motin 669dd48af36SAlexander Motin if (ch->numrslots != 0) 670dd48af36SAlexander Motin return; 671dd48af36SAlexander Motin /* If we are idle - request power state transition. */ 672dd48af36SAlexander Motin work = ATA_INL(ch->r_mem, SATA_SC); 673dd48af36SAlexander Motin work &= ~SATA_SC_SPM_MASK; 674dd48af36SAlexander Motin if (ch->pm_level == 4) 675dd48af36SAlexander Motin work |= SATA_SC_SPM_PARTIAL; 676dd48af36SAlexander Motin else 677dd48af36SAlexander Motin work |= SATA_SC_SPM_SLUMBER; 678dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SC, work); 679dd48af36SAlexander Motin } 680dd48af36SAlexander Motin 681dd48af36SAlexander Motin static void 682dd48af36SAlexander Motin mvs_ch_pm_wake(device_t dev) 683dd48af36SAlexander Motin { 684dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 685dd48af36SAlexander Motin uint32_t work; 686dd48af36SAlexander Motin int timeout = 0; 687dd48af36SAlexander Motin 688dd48af36SAlexander Motin work = ATA_INL(ch->r_mem, SATA_SS); 689dd48af36SAlexander Motin if (work & SATA_SS_IPM_ACTIVE) 690dd48af36SAlexander Motin return; 691dd48af36SAlexander Motin /* If we are not in active state - request power state transition. */ 692dd48af36SAlexander Motin work = ATA_INL(ch->r_mem, SATA_SC); 693dd48af36SAlexander Motin work &= ~SATA_SC_SPM_MASK; 694dd48af36SAlexander Motin work |= SATA_SC_SPM_ACTIVE; 695dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SC, work); 696dd48af36SAlexander Motin /* Wait for transition to happen. */ 697dd48af36SAlexander Motin while ((ATA_INL(ch->r_mem, SATA_SS) & SATA_SS_IPM_ACTIVE) == 0 && 698dd48af36SAlexander Motin timeout++ < 100) { 699dd48af36SAlexander Motin DELAY(100); 700dd48af36SAlexander Motin } 701dd48af36SAlexander Motin } 702dd48af36SAlexander Motin 703dd48af36SAlexander Motin static void 704dd48af36SAlexander Motin mvs_ch_intr(void *data) 705dd48af36SAlexander Motin { 706dd48af36SAlexander Motin struct mvs_intr_arg *arg = (struct mvs_intr_arg *)data; 707dd48af36SAlexander Motin device_t dev = (device_t)arg->arg; 708dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 709dd48af36SAlexander Motin uint32_t iec, serr = 0, fisic = 0; 710dd48af36SAlexander Motin enum mvs_err_type et; 711dd48af36SAlexander Motin int i, ccs, port = -1, selfdis = 0; 712dd48af36SAlexander Motin int edma = (ch->numtslots != 0 || ch->numdslots != 0); 713dd48af36SAlexander Motin 714dd48af36SAlexander Motin /* New item in response queue. */ 715dd48af36SAlexander Motin if ((arg->cause & 2) && edma) 716dd48af36SAlexander Motin mvs_crbq_intr(dev); 717dd48af36SAlexander Motin /* Some error or special event. */ 718dd48af36SAlexander Motin if (arg->cause & 1) { 719dd48af36SAlexander Motin iec = ATA_INL(ch->r_mem, EDMA_IEC); 720dd48af36SAlexander Motin if (iec & EDMA_IE_SERRINT) { 721dd48af36SAlexander Motin serr = ATA_INL(ch->r_mem, SATA_SE); 722dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SE, serr); 723dd48af36SAlexander Motin } 724dd48af36SAlexander Motin /* EDMA self-disabled due to error. */ 725dd48af36SAlexander Motin if (iec & EDMA_IE_ESELFDIS) 726dd48af36SAlexander Motin selfdis = 1; 727dd48af36SAlexander Motin /* Transport interrupt. */ 728dd48af36SAlexander Motin if (iec & EDMA_IE_ETRANSINT) { 729dd48af36SAlexander Motin /* For Gen-I this bit means self-disable. */ 730dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENI) 731dd48af36SAlexander Motin selfdis = 1; 732dd48af36SAlexander Motin /* For Gen-II this bit means SDB-N. */ 733dd48af36SAlexander Motin else if (ch->quirks & MVS_Q_GENII) 734dd48af36SAlexander Motin fisic = SATA_FISC_FISWAIT4HOSTRDYEN_B1; 735dd48af36SAlexander Motin else /* For Gen-IIe - read FIS interrupt cause. */ 736dd48af36SAlexander Motin fisic = ATA_INL(ch->r_mem, SATA_FISIC); 737dd48af36SAlexander Motin } 738dd48af36SAlexander Motin if (selfdis) 739dd48af36SAlexander Motin ch->curr_mode = MVS_EDMA_UNKNOWN; 740dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEC, ~iec); 741dd48af36SAlexander Motin /* Interface errors or Device error. */ 742dd48af36SAlexander Motin if (iec & (0xfc1e9000 | EDMA_IE_EDEVERR)) { 743dd48af36SAlexander Motin port = -1; 744dd48af36SAlexander Motin if (ch->numpslots != 0) { 745dd48af36SAlexander Motin ccs = 0; 746dd48af36SAlexander Motin } else { 747dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENIIE) 748dd48af36SAlexander Motin ccs = EDMA_S_EIOID(ATA_INL(ch->r_mem, EDMA_S)); 749dd48af36SAlexander Motin else 750dd48af36SAlexander Motin ccs = EDMA_S_EDEVQUETAG(ATA_INL(ch->r_mem, EDMA_S)); 751dd48af36SAlexander Motin /* Check if error is one-PMP-port-specific, */ 752dd48af36SAlexander Motin if (ch->fbs_enabled) { 753dd48af36SAlexander Motin /* Which ports were active. */ 754dd48af36SAlexander Motin for (i = 0; i < 16; i++) { 755dd48af36SAlexander Motin if (ch->numrslotspd[i] == 0) 756dd48af36SAlexander Motin continue; 757dd48af36SAlexander Motin if (port == -1) 758dd48af36SAlexander Motin port = i; 759dd48af36SAlexander Motin else if (port != i) { 760dd48af36SAlexander Motin port = -2; 761dd48af36SAlexander Motin break; 762dd48af36SAlexander Motin } 763dd48af36SAlexander Motin } 764dd48af36SAlexander Motin /* If several ports were active and EDMA still enabled - 765dd48af36SAlexander Motin * other ports are probably unaffected and may continue. 766dd48af36SAlexander Motin */ 767dd48af36SAlexander Motin if (port == -2 && !selfdis) { 768dd48af36SAlexander Motin uint16_t p = ATA_INL(ch->r_mem, SATA_SATAITC) >> 16; 769dd48af36SAlexander Motin port = ffs(p) - 1; 770dd48af36SAlexander Motin if (port != (fls(p) - 1)) 771dd48af36SAlexander Motin port = -2; 772dd48af36SAlexander Motin } 773dd48af36SAlexander Motin } 774dd48af36SAlexander Motin } 775dd48af36SAlexander Motin mvs_requeue_frozen(dev); 776dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 777dd48af36SAlexander Motin /* XXX: reqests in loading state. */ 778dd48af36SAlexander Motin if (((ch->rslots >> i) & 1) == 0) 779dd48af36SAlexander Motin continue; 780dd48af36SAlexander Motin if (port >= 0 && 781dd48af36SAlexander Motin ch->slot[i].ccb->ccb_h.target_id != port) 782dd48af36SAlexander Motin continue; 783dd48af36SAlexander Motin if (iec & EDMA_IE_EDEVERR) { /* Device error. */ 784dd48af36SAlexander Motin if (port != -2) { 785dd48af36SAlexander Motin if (ch->numtslots == 0) { 786dd48af36SAlexander Motin /* Untagged operation. */ 787dd48af36SAlexander Motin if (i == ccs) 788dd48af36SAlexander Motin et = MVS_ERR_TFE; 789dd48af36SAlexander Motin else 790dd48af36SAlexander Motin et = MVS_ERR_INNOCENT; 791dd48af36SAlexander Motin } else { 792dd48af36SAlexander Motin /* Tagged operation. */ 793dd48af36SAlexander Motin et = MVS_ERR_NCQ; 794dd48af36SAlexander Motin } 795dd48af36SAlexander Motin } else { 796dd48af36SAlexander Motin et = MVS_ERR_TFE; 797dd48af36SAlexander Motin ch->fatalerr = 1; 798dd48af36SAlexander Motin } 799dd48af36SAlexander Motin } else if (iec & 0xfc1e9000) { 800c0609c54SAlexander Motin if (ch->numtslots == 0 && 801c0609c54SAlexander Motin i != ccs && port != -2) 802dd48af36SAlexander Motin et = MVS_ERR_INNOCENT; 803dd48af36SAlexander Motin else 804dd48af36SAlexander Motin et = MVS_ERR_SATA; 805dd48af36SAlexander Motin } else 806dd48af36SAlexander Motin et = MVS_ERR_INVALID; 807dd48af36SAlexander Motin mvs_end_transaction(&ch->slot[i], et); 808dd48af36SAlexander Motin } 809dd48af36SAlexander Motin } 810dd48af36SAlexander Motin /* Process SDB-N. */ 811dd48af36SAlexander Motin if (fisic & SATA_FISC_FISWAIT4HOSTRDYEN_B1) 812dd48af36SAlexander Motin mvs_notify_events(dev); 813dd48af36SAlexander Motin if (fisic) 814dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_FISIC, ~fisic); 815dd48af36SAlexander Motin /* Process hot-plug. */ 816dd48af36SAlexander Motin if ((iec & (EDMA_IE_EDEVDIS | EDMA_IE_EDEVCON)) || 817dd48af36SAlexander Motin (serr & SATA_SE_PHY_CHANGED)) 818dd48af36SAlexander Motin mvs_phy_check_events(dev, serr); 819dd48af36SAlexander Motin } 820dd48af36SAlexander Motin /* Legacy mode device interrupt. */ 821dd48af36SAlexander Motin if ((arg->cause & 2) && !edma) 82270b7af2bSAlexander Motin mvs_legacy_intr(dev, arg->cause & 4); 823dd48af36SAlexander Motin } 824dd48af36SAlexander Motin 825dd48af36SAlexander Motin static uint8_t 826dd48af36SAlexander Motin mvs_getstatus(device_t dev, int clear) 827dd48af36SAlexander Motin { 828dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 829dd48af36SAlexander Motin uint8_t status = ATA_INB(ch->r_mem, clear ? ATA_STATUS : ATA_ALTSTAT); 830dd48af36SAlexander Motin 831dd48af36SAlexander Motin if (ch->fake_busy) { 832dd48af36SAlexander Motin if (status & (ATA_S_BUSY | ATA_S_DRQ | ATA_S_ERROR)) 833dd48af36SAlexander Motin ch->fake_busy = 0; 834dd48af36SAlexander Motin else 835dd48af36SAlexander Motin status |= ATA_S_BUSY; 836dd48af36SAlexander Motin } 837dd48af36SAlexander Motin return (status); 838dd48af36SAlexander Motin } 839dd48af36SAlexander Motin 840dd48af36SAlexander Motin static void 84170b7af2bSAlexander Motin mvs_legacy_intr(device_t dev, int poll) 842dd48af36SAlexander Motin { 843dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 844dd48af36SAlexander Motin struct mvs_slot *slot = &ch->slot[0]; /* PIO is always in slot 0. */ 845dd48af36SAlexander Motin union ccb *ccb = slot->ccb; 846dd48af36SAlexander Motin enum mvs_err_type et = MVS_ERR_NONE; 847dd48af36SAlexander Motin int port; 84897fd3ac6SAlexander Motin u_int length, resid, size; 84997fd3ac6SAlexander Motin uint8_t buf[2]; 850dd48af36SAlexander Motin uint8_t status, ireason; 851dd48af36SAlexander Motin 852dd48af36SAlexander Motin /* Clear interrupt and get status. */ 853dd48af36SAlexander Motin status = mvs_getstatus(dev, 1); 854dd48af36SAlexander Motin if (slot->state < MVS_SLOT_RUNNING) 855dd48af36SAlexander Motin return; 856dd48af36SAlexander Motin port = ccb->ccb_h.target_id & 0x0f; 857dd48af36SAlexander Motin /* Wait a bit for late !BUSY status update. */ 858dd48af36SAlexander Motin if (status & ATA_S_BUSY) { 85970b7af2bSAlexander Motin if (poll) 86070b7af2bSAlexander Motin return; 861dd48af36SAlexander Motin DELAY(100); 862dd48af36SAlexander Motin if ((status = mvs_getstatus(dev, 1)) & ATA_S_BUSY) { 863dd48af36SAlexander Motin DELAY(1000); 864dd48af36SAlexander Motin if ((status = mvs_getstatus(dev, 1)) & ATA_S_BUSY) 865dd48af36SAlexander Motin return; 866dd48af36SAlexander Motin } 867dd48af36SAlexander Motin } 868dd48af36SAlexander Motin /* If we got an error, we are done. */ 869dd48af36SAlexander Motin if (status & ATA_S_ERROR) { 870dd48af36SAlexander Motin et = MVS_ERR_TFE; 871dd48af36SAlexander Motin goto end_finished; 872dd48af36SAlexander Motin } 873dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { /* ATA PIO */ 874dd48af36SAlexander Motin ccb->ataio.res.status = status; 875dd48af36SAlexander Motin /* Are we moving data? */ 876dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 877dd48af36SAlexander Motin /* If data read command - get them. */ 878dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 879dd48af36SAlexander Motin if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) { 880dd48af36SAlexander Motin device_printf(dev, "timeout waiting for read DRQ\n"); 881dd48af36SAlexander Motin et = MVS_ERR_TIMEOUT; 8828d169381SAlexander Motin xpt_freeze_simq(ch->sim, 1); 8838d169381SAlexander Motin ch->toslots |= (1 << slot->slot); 884dd48af36SAlexander Motin goto end_finished; 885dd48af36SAlexander Motin } 886dd48af36SAlexander Motin ATA_INSW_STRM(ch->r_mem, ATA_DATA, 887dd48af36SAlexander Motin (uint16_t *)(ccb->ataio.data_ptr + ch->donecount), 888dd48af36SAlexander Motin ch->transfersize / 2); 889dd48af36SAlexander Motin } 890dd48af36SAlexander Motin /* Update how far we've gotten. */ 891dd48af36SAlexander Motin ch->donecount += ch->transfersize; 892dd48af36SAlexander Motin /* Do we need more? */ 893dd48af36SAlexander Motin if (ccb->ataio.dxfer_len > ch->donecount) { 894dd48af36SAlexander Motin /* Set this transfer size according to HW capabilities */ 895dd48af36SAlexander Motin ch->transfersize = min(ccb->ataio.dxfer_len - ch->donecount, 8969cf41729SAlexander Motin ch->transfersize); 897dd48af36SAlexander Motin /* If data write command - put them */ 898dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 899dd48af36SAlexander Motin if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) { 900c0609c54SAlexander Motin device_printf(dev, 901c0609c54SAlexander Motin "timeout waiting for write DRQ\n"); 902dd48af36SAlexander Motin et = MVS_ERR_TIMEOUT; 9038d169381SAlexander Motin xpt_freeze_simq(ch->sim, 1); 9048d169381SAlexander Motin ch->toslots |= (1 << slot->slot); 905dd48af36SAlexander Motin goto end_finished; 906dd48af36SAlexander Motin } 907dd48af36SAlexander Motin ATA_OUTSW_STRM(ch->r_mem, ATA_DATA, 908dd48af36SAlexander Motin (uint16_t *)(ccb->ataio.data_ptr + ch->donecount), 909dd48af36SAlexander Motin ch->transfersize / 2); 910dd48af36SAlexander Motin return; 911dd48af36SAlexander Motin } 912dd48af36SAlexander Motin /* If data read command, return & wait for interrupt */ 913dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 914dd48af36SAlexander Motin return; 915dd48af36SAlexander Motin } 916dd48af36SAlexander Motin } 917dd48af36SAlexander Motin } else if (ch->basic_dma) { /* ATAPI DMA */ 918dd48af36SAlexander Motin if (status & ATA_S_DWF) 919dd48af36SAlexander Motin et = MVS_ERR_TFE; 920dd48af36SAlexander Motin else if (ATA_INL(ch->r_mem, DMA_S) & DMA_S_ERR) 921dd48af36SAlexander Motin et = MVS_ERR_TFE; 922dd48af36SAlexander Motin /* Stop basic DMA. */ 923dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, DMA_C, 0); 924dd48af36SAlexander Motin goto end_finished; 925dd48af36SAlexander Motin } else { /* ATAPI PIO */ 926c0609c54SAlexander Motin length = ATA_INB(ch->r_mem,ATA_CYL_LSB) | 927c0609c54SAlexander Motin (ATA_INB(ch->r_mem,ATA_CYL_MSB) << 8); 92897fd3ac6SAlexander Motin size = min(ch->transfersize, length); 929dd48af36SAlexander Motin ireason = ATA_INB(ch->r_mem,ATA_IREASON); 930dd48af36SAlexander Motin switch ((ireason & (ATA_I_CMD | ATA_I_IN)) | 931dd48af36SAlexander Motin (status & ATA_S_DRQ)) { 932dd48af36SAlexander Motin 933dd48af36SAlexander Motin case ATAPI_P_CMDOUT: 934dd48af36SAlexander Motin device_printf(dev, "ATAPI CMDOUT\n"); 935dd48af36SAlexander Motin /* Return wait for interrupt */ 936dd48af36SAlexander Motin return; 937dd48af36SAlexander Motin 938dd48af36SAlexander Motin case ATAPI_P_WRITE: 939dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 940dd48af36SAlexander Motin device_printf(dev, "trying to write on read buffer\n"); 941dd48af36SAlexander Motin et = MVS_ERR_TFE; 942dd48af36SAlexander Motin goto end_finished; 943dd48af36SAlexander Motin break; 944dd48af36SAlexander Motin } 945dd48af36SAlexander Motin ATA_OUTSW_STRM(ch->r_mem, ATA_DATA, 946dd48af36SAlexander Motin (uint16_t *)(ccb->csio.data_ptr + ch->donecount), 94797fd3ac6SAlexander Motin (size + 1) / 2); 94897fd3ac6SAlexander Motin for (resid = ch->transfersize + (size & 1); 94997fd3ac6SAlexander Motin resid < length; resid += sizeof(int16_t)) 95097fd3ac6SAlexander Motin ATA_OUTW(ch->r_mem, ATA_DATA, 0); 951dd48af36SAlexander Motin ch->donecount += length; 952dd48af36SAlexander Motin /* Set next transfer size according to HW capabilities */ 953dd48af36SAlexander Motin ch->transfersize = min(ccb->csio.dxfer_len - ch->donecount, 954dd48af36SAlexander Motin ch->curr[ccb->ccb_h.target_id].bytecount); 955dd48af36SAlexander Motin /* Return wait for interrupt */ 956dd48af36SAlexander Motin return; 957dd48af36SAlexander Motin 958dd48af36SAlexander Motin case ATAPI_P_READ: 959dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 960dd48af36SAlexander Motin device_printf(dev, "trying to read on write buffer\n"); 961dd48af36SAlexander Motin et = MVS_ERR_TFE; 962dd48af36SAlexander Motin goto end_finished; 963dd48af36SAlexander Motin } 96497fd3ac6SAlexander Motin if (size >= 2) { 965dd48af36SAlexander Motin ATA_INSW_STRM(ch->r_mem, ATA_DATA, 966dd48af36SAlexander Motin (uint16_t *)(ccb->csio.data_ptr + ch->donecount), 96797fd3ac6SAlexander Motin size / 2); 96897fd3ac6SAlexander Motin } 96997fd3ac6SAlexander Motin if (size & 1) { 97097fd3ac6SAlexander Motin ATA_INSW_STRM(ch->r_mem, ATA_DATA, (void*)buf, 1); 97197fd3ac6SAlexander Motin ((uint8_t *)ccb->csio.data_ptr + ch->donecount + 97297fd3ac6SAlexander Motin (size & ~1))[0] = buf[0]; 97397fd3ac6SAlexander Motin } 97497fd3ac6SAlexander Motin for (resid = ch->transfersize + (size & 1); 97597fd3ac6SAlexander Motin resid < length; resid += sizeof(int16_t)) 97697fd3ac6SAlexander Motin ATA_INW(ch->r_mem, ATA_DATA); 977dd48af36SAlexander Motin ch->donecount += length; 978dd48af36SAlexander Motin /* Set next transfer size according to HW capabilities */ 979dd48af36SAlexander Motin ch->transfersize = min(ccb->csio.dxfer_len - ch->donecount, 980dd48af36SAlexander Motin ch->curr[ccb->ccb_h.target_id].bytecount); 981dd48af36SAlexander Motin /* Return wait for interrupt */ 982dd48af36SAlexander Motin return; 983dd48af36SAlexander Motin 984dd48af36SAlexander Motin case ATAPI_P_DONEDRQ: 985dd48af36SAlexander Motin device_printf(dev, 986dd48af36SAlexander Motin "WARNING - DONEDRQ non conformant device\n"); 987dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 988dd48af36SAlexander Motin ATA_INSW_STRM(ch->r_mem, ATA_DATA, 989dd48af36SAlexander Motin (uint16_t *)(ccb->csio.data_ptr + ch->donecount), 990dd48af36SAlexander Motin length / 2); 991dd48af36SAlexander Motin ch->donecount += length; 992dd48af36SAlexander Motin } 993dd48af36SAlexander Motin else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 994dd48af36SAlexander Motin ATA_OUTSW_STRM(ch->r_mem, ATA_DATA, 995dd48af36SAlexander Motin (uint16_t *)(ccb->csio.data_ptr + ch->donecount), 996dd48af36SAlexander Motin length / 2); 997dd48af36SAlexander Motin ch->donecount += length; 998dd48af36SAlexander Motin } 999dd48af36SAlexander Motin else 1000dd48af36SAlexander Motin et = MVS_ERR_TFE; 1001dd48af36SAlexander Motin /* FALLTHROUGH */ 1002dd48af36SAlexander Motin 1003dd48af36SAlexander Motin case ATAPI_P_ABORT: 1004dd48af36SAlexander Motin case ATAPI_P_DONE: 1005dd48af36SAlexander Motin if (status & (ATA_S_ERROR | ATA_S_DWF)) 1006dd48af36SAlexander Motin et = MVS_ERR_TFE; 1007dd48af36SAlexander Motin goto end_finished; 1008dd48af36SAlexander Motin 1009dd48af36SAlexander Motin default: 1010c0609c54SAlexander Motin device_printf(dev, "unknown transfer phase" 1011c0609c54SAlexander Motin " (status %02x, ireason %02x)\n", 1012dd48af36SAlexander Motin status, ireason); 1013dd48af36SAlexander Motin et = MVS_ERR_TFE; 1014dd48af36SAlexander Motin } 1015dd48af36SAlexander Motin } 1016dd48af36SAlexander Motin 1017dd48af36SAlexander Motin end_finished: 1018dd48af36SAlexander Motin mvs_end_transaction(slot, et); 1019dd48af36SAlexander Motin } 1020dd48af36SAlexander Motin 1021dd48af36SAlexander Motin static void 1022dd48af36SAlexander Motin mvs_crbq_intr(device_t dev) 1023dd48af36SAlexander Motin { 1024dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1025dd48af36SAlexander Motin struct mvs_crpb *crpb; 1026dd48af36SAlexander Motin union ccb *ccb; 10276c872350SAlexander Motin int in_idx, fin_idx, cin_idx, slot; 10286c872350SAlexander Motin uint32_t val; 1029dd48af36SAlexander Motin uint16_t flags; 1030dd48af36SAlexander Motin 10316c872350SAlexander Motin val = ATA_INL(ch->r_mem, EDMA_RESQIP); 10326c872350SAlexander Motin if (val == 0) 10336c872350SAlexander Motin val = ATA_INL(ch->r_mem, EDMA_RESQIP); 10346c872350SAlexander Motin in_idx = (val & EDMA_RESQP_ERPQP_MASK) >> 1035dd48af36SAlexander Motin EDMA_RESQP_ERPQP_SHIFT; 1036dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map, 1037dd48af36SAlexander Motin BUS_DMASYNC_POSTREAD); 10386c872350SAlexander Motin fin_idx = cin_idx = ch->in_idx; 1039dd48af36SAlexander Motin ch->in_idx = in_idx; 1040dd48af36SAlexander Motin while (in_idx != cin_idx) { 1041dd48af36SAlexander Motin crpb = (struct mvs_crpb *) 10426c872350SAlexander Motin (ch->dma.workrp + MVS_CRPB_OFFSET + 10436c872350SAlexander Motin (MVS_CRPB_SIZE * cin_idx)); 1044dd48af36SAlexander Motin slot = le16toh(crpb->id) & MVS_CRPB_TAG_MASK; 1045dd48af36SAlexander Motin flags = le16toh(crpb->rspflg); 1046dd48af36SAlexander Motin /* 1047453130d9SPedro F. Giffuni * Handle only successful completions here. 1048dd48af36SAlexander Motin * Errors will be handled by main intr handler. 1049dd48af36SAlexander Motin */ 1050b30c7d51SAlexander Motin #if defined(__i386__) || defined(__amd64__) 10516c872350SAlexander Motin if (crpb->id == 0xffff && crpb->rspflg == 0xffff) { 10526c872350SAlexander Motin device_printf(dev, "Unfilled CRPB " 10536c872350SAlexander Motin "%d (%d->%d) tag %d flags %04x rs %08x\n", 10546c872350SAlexander Motin cin_idx, fin_idx, in_idx, slot, flags, ch->rslots); 1055b30c7d51SAlexander Motin } else 1056b30c7d51SAlexander Motin #endif 1057b30c7d51SAlexander Motin if (ch->numtslots != 0 || 10586c872350SAlexander Motin (flags & EDMA_IE_EDEVERR) == 0) { 1059b30c7d51SAlexander Motin #if defined(__i386__) || defined(__amd64__) 10606c872350SAlexander Motin crpb->id = 0xffff; 10616c872350SAlexander Motin crpb->rspflg = 0xffff; 1062b30c7d51SAlexander Motin #endif 1063dd48af36SAlexander Motin if (ch->slot[slot].state >= MVS_SLOT_RUNNING) { 1064dd48af36SAlexander Motin ccb = ch->slot[slot].ccb; 10656c872350SAlexander Motin ccb->ataio.res.status = 10666c872350SAlexander Motin (flags & MVS_CRPB_ATASTS_MASK) >> 1067dd48af36SAlexander Motin MVS_CRPB_ATASTS_SHIFT; 1068dd48af36SAlexander Motin mvs_end_transaction(&ch->slot[slot], MVS_ERR_NONE); 10696c872350SAlexander Motin } else { 10706c872350SAlexander Motin device_printf(dev, "Unused tag in CRPB " 10716c872350SAlexander Motin "%d (%d->%d) tag %d flags %04x rs %08x\n", 10726c872350SAlexander Motin cin_idx, fin_idx, in_idx, slot, flags, 10736c872350SAlexander Motin ch->rslots); 10746c872350SAlexander Motin } 10756c872350SAlexander Motin } else { 10766c872350SAlexander Motin device_printf(dev, 10776c872350SAlexander Motin "CRPB with error %d tag %d flags %04x\n", 10786c872350SAlexander Motin cin_idx, slot, flags); 10796c872350SAlexander Motin } 1080dd48af36SAlexander Motin cin_idx = (cin_idx + 1) & (MVS_MAX_SLOTS - 1); 1081dd48af36SAlexander Motin } 1082dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map, 1083dd48af36SAlexander Motin BUS_DMASYNC_PREREAD); 1084dd48af36SAlexander Motin if (cin_idx == ch->in_idx) { 1085dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_RESQOP, 1086dd48af36SAlexander Motin ch->dma.workrp_bus | (cin_idx << EDMA_RESQP_ERPQP_SHIFT)); 1087dd48af36SAlexander Motin } 1088dd48af36SAlexander Motin } 1089dd48af36SAlexander Motin 1090dd48af36SAlexander Motin /* Must be called with channel locked. */ 1091dd48af36SAlexander Motin static int 1092dd48af36SAlexander Motin mvs_check_collision(device_t dev, union ccb *ccb) 1093dd48af36SAlexander Motin { 1094dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1095dd48af36SAlexander Motin 1096dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1097dd48af36SAlexander Motin /* NCQ DMA */ 1098dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1099dd48af36SAlexander Motin /* Can't mix NCQ and non-NCQ DMA commands. */ 1100dd48af36SAlexander Motin if (ch->numdslots != 0) 1101dd48af36SAlexander Motin return (1); 1102dd48af36SAlexander Motin /* Can't mix NCQ and PIO commands. */ 1103dd48af36SAlexander Motin if (ch->numpslots != 0) 1104dd48af36SAlexander Motin return (1); 1105dd48af36SAlexander Motin /* If we have no FBS */ 1106dd48af36SAlexander Motin if (!ch->fbs_enabled) { 1107dd48af36SAlexander Motin /* Tagged command while tagged to other target is active. */ 1108dd48af36SAlexander Motin if (ch->numtslots != 0 && 1109dd48af36SAlexander Motin ch->taggedtarget != ccb->ccb_h.target_id) 1110dd48af36SAlexander Motin return (1); 1111dd48af36SAlexander Motin } 1112dd48af36SAlexander Motin /* Non-NCQ DMA */ 1113dd48af36SAlexander Motin } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) { 1114dd48af36SAlexander Motin /* Can't mix non-NCQ DMA and NCQ commands. */ 1115dd48af36SAlexander Motin if (ch->numtslots != 0) 1116dd48af36SAlexander Motin return (1); 1117dd48af36SAlexander Motin /* Can't mix non-NCQ DMA and PIO commands. */ 1118dd48af36SAlexander Motin if (ch->numpslots != 0) 1119dd48af36SAlexander Motin return (1); 1120dd48af36SAlexander Motin /* PIO */ 1121dd48af36SAlexander Motin } else { 1122dd48af36SAlexander Motin /* Can't mix PIO with anything. */ 1123dd48af36SAlexander Motin if (ch->numrslots != 0) 1124dd48af36SAlexander Motin return (1); 1125dd48af36SAlexander Motin } 1126dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)) { 1127dd48af36SAlexander Motin /* Atomic command while anything active. */ 1128dd48af36SAlexander Motin if (ch->numrslots != 0) 1129dd48af36SAlexander Motin return (1); 1130dd48af36SAlexander Motin } 1131dd48af36SAlexander Motin } else { /* ATAPI */ 1132dd48af36SAlexander Motin /* ATAPI goes without EDMA, so can't mix it with anything. */ 1133dd48af36SAlexander Motin if (ch->numrslots != 0) 1134dd48af36SAlexander Motin return (1); 1135dd48af36SAlexander Motin } 1136dd48af36SAlexander Motin /* We have some atomic command running. */ 1137dd48af36SAlexander Motin if (ch->aslots != 0) 1138dd48af36SAlexander Motin return (1); 1139dd48af36SAlexander Motin return (0); 1140dd48af36SAlexander Motin } 1141dd48af36SAlexander Motin 1142dd48af36SAlexander Motin static void 1143dd48af36SAlexander Motin mvs_tfd_read(device_t dev, union ccb *ccb) 1144dd48af36SAlexander Motin { 1145dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1146dd48af36SAlexander Motin struct ata_res *res = &ccb->ataio.res; 1147dd48af36SAlexander Motin 1148dd48af36SAlexander Motin res->status = ATA_INB(ch->r_mem, ATA_ALTSTAT); 1149dd48af36SAlexander Motin res->error = ATA_INB(ch->r_mem, ATA_ERROR); 1150dd48af36SAlexander Motin res->device = ATA_INB(ch->r_mem, ATA_DRIVE); 1151dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_HOB); 1152dd48af36SAlexander Motin res->sector_count_exp = ATA_INB(ch->r_mem, ATA_COUNT); 1153dd48af36SAlexander Motin res->lba_low_exp = ATA_INB(ch->r_mem, ATA_SECTOR); 1154dd48af36SAlexander Motin res->lba_mid_exp = ATA_INB(ch->r_mem, ATA_CYL_LSB); 1155dd48af36SAlexander Motin res->lba_high_exp = ATA_INB(ch->r_mem, ATA_CYL_MSB); 1156dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, 0); 1157dd48af36SAlexander Motin res->sector_count = ATA_INB(ch->r_mem, ATA_COUNT); 1158dd48af36SAlexander Motin res->lba_low = ATA_INB(ch->r_mem, ATA_SECTOR); 1159dd48af36SAlexander Motin res->lba_mid = ATA_INB(ch->r_mem, ATA_CYL_LSB); 1160dd48af36SAlexander Motin res->lba_high = ATA_INB(ch->r_mem, ATA_CYL_MSB); 1161dd48af36SAlexander Motin } 1162dd48af36SAlexander Motin 1163dd48af36SAlexander Motin static void 1164dd48af36SAlexander Motin mvs_tfd_write(device_t dev, union ccb *ccb) 1165dd48af36SAlexander Motin { 1166dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1167dd48af36SAlexander Motin struct ata_cmd *cmd = &ccb->ataio.cmd; 1168dd48af36SAlexander Motin 1169dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_DRIVE, cmd->device); 1170dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, cmd->control); 1171dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_FEATURE, cmd->features_exp); 1172dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_FEATURE, cmd->features); 1173dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_COUNT, cmd->sector_count_exp); 1174dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_COUNT, cmd->sector_count); 1175dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_SECTOR, cmd->lba_low_exp); 1176dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_SECTOR, cmd->lba_low); 1177dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_LSB, cmd->lba_mid_exp); 1178dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_LSB, cmd->lba_mid); 1179dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_MSB, cmd->lba_high_exp); 1180dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_MSB, cmd->lba_high); 1181dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_COMMAND, cmd->command); 1182dd48af36SAlexander Motin } 1183dd48af36SAlexander Motin 1184dd48af36SAlexander Motin 1185dd48af36SAlexander Motin /* Must be called with channel locked. */ 1186dd48af36SAlexander Motin static void 1187dd48af36SAlexander Motin mvs_begin_transaction(device_t dev, union ccb *ccb) 1188dd48af36SAlexander Motin { 1189dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1190dd48af36SAlexander Motin struct mvs_slot *slot; 1191dd48af36SAlexander Motin int slotn, tag; 1192dd48af36SAlexander Motin 1193dd48af36SAlexander Motin if (ch->pm_level > 0) 1194dd48af36SAlexander Motin mvs_ch_pm_wake(dev); 1195dd48af36SAlexander Motin /* Softreset is a special case. */ 1196dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO && 1197dd48af36SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) { 1198dd48af36SAlexander Motin mvs_softreset(dev, ccb); 1199dd48af36SAlexander Motin return; 1200dd48af36SAlexander Motin } 1201dd48af36SAlexander Motin /* Choose empty slot. */ 1202dd48af36SAlexander Motin slotn = ffs(~ch->oslots) - 1; 1203dd48af36SAlexander Motin if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1204dd48af36SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1205dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENIIE) 1206dd48af36SAlexander Motin tag = ffs(~ch->otagspd[ccb->ccb_h.target_id]) - 1; 1207dd48af36SAlexander Motin else 1208dd48af36SAlexander Motin tag = slotn; 1209dd48af36SAlexander Motin } else 1210dd48af36SAlexander Motin tag = 0; 1211dd48af36SAlexander Motin /* Occupy chosen slot. */ 1212dd48af36SAlexander Motin slot = &ch->slot[slotn]; 1213dd48af36SAlexander Motin slot->ccb = ccb; 1214dd48af36SAlexander Motin slot->tag = tag; 1215dd48af36SAlexander Motin /* Stop PM timer. */ 1216dd48af36SAlexander Motin if (ch->numrslots == 0 && ch->pm_level > 3) 1217dd48af36SAlexander Motin callout_stop(&ch->pm_timer); 1218dd48af36SAlexander Motin /* Update channel stats. */ 1219dd48af36SAlexander Motin ch->oslots |= (1 << slot->slot); 1220dd48af36SAlexander Motin ch->numrslots++; 1221dd48af36SAlexander Motin ch->numrslotspd[ccb->ccb_h.target_id]++; 1222dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1223dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1224dd48af36SAlexander Motin ch->otagspd[ccb->ccb_h.target_id] |= (1 << slot->tag); 1225dd48af36SAlexander Motin ch->numtslots++; 1226dd48af36SAlexander Motin ch->numtslotspd[ccb->ccb_h.target_id]++; 1227dd48af36SAlexander Motin ch->taggedtarget = ccb->ccb_h.target_id; 1228dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_NCQ); 1229dd48af36SAlexander Motin } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) { 1230dd48af36SAlexander Motin ch->numdslots++; 1231dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_ON); 1232dd48af36SAlexander Motin } else { 1233dd48af36SAlexander Motin ch->numpslots++; 1234dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 1235dd48af36SAlexander Motin } 1236dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & 1237dd48af36SAlexander Motin (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)) { 1238dd48af36SAlexander Motin ch->aslots |= (1 << slot->slot); 1239dd48af36SAlexander Motin } 1240dd48af36SAlexander Motin } else { 1241dd48af36SAlexander Motin uint8_t *cdb = (ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1242dd48af36SAlexander Motin ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes; 1243dd48af36SAlexander Motin ch->numpslots++; 1244dd48af36SAlexander Motin /* Use ATAPI DMA only for commands without under-/overruns. */ 1245dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 1246dd48af36SAlexander Motin ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA && 1247dd48af36SAlexander Motin (ch->quirks & MVS_Q_SOC) == 0 && 1248dd48af36SAlexander Motin (cdb[0] == 0x08 || 1249dd48af36SAlexander Motin cdb[0] == 0x0a || 1250dd48af36SAlexander Motin cdb[0] == 0x28 || 1251dd48af36SAlexander Motin cdb[0] == 0x2a || 1252dd48af36SAlexander Motin cdb[0] == 0x88 || 1253dd48af36SAlexander Motin cdb[0] == 0x8a || 1254dd48af36SAlexander Motin cdb[0] == 0xa8 || 1255dd48af36SAlexander Motin cdb[0] == 0xaa || 1256dd48af36SAlexander Motin cdb[0] == 0xbe)) { 1257dd48af36SAlexander Motin ch->basic_dma = 1; 1258dd48af36SAlexander Motin } 1259dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 1260dd48af36SAlexander Motin } 1261dd48af36SAlexander Motin if (ch->numpslots == 0 || ch->basic_dma) { 1262dd48af36SAlexander Motin slot->state = MVS_SLOT_LOADING; 1263dd0b4fb6SKonstantin Belousov bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, 1264dd0b4fb6SKonstantin Belousov ccb, mvs_dmasetprd, slot, 0); 1265dd48af36SAlexander Motin } else 1266dd48af36SAlexander Motin mvs_legacy_execute_transaction(slot); 1267dd48af36SAlexander Motin } 1268dd48af36SAlexander Motin 1269dd48af36SAlexander Motin /* Locked by busdma engine. */ 1270dd48af36SAlexander Motin static void 1271dd48af36SAlexander Motin mvs_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1272dd48af36SAlexander Motin { 1273dd48af36SAlexander Motin struct mvs_slot *slot = arg; 1274dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(slot->dev); 1275dd48af36SAlexander Motin struct mvs_eprd *eprd; 1276dd48af36SAlexander Motin int i; 1277dd48af36SAlexander Motin 1278dd48af36SAlexander Motin if (error) { 1279dd48af36SAlexander Motin device_printf(slot->dev, "DMA load error\n"); 1280dd48af36SAlexander Motin mvs_end_transaction(slot, MVS_ERR_INVALID); 1281dd48af36SAlexander Motin return; 1282dd48af36SAlexander Motin } 1283dd48af36SAlexander Motin KASSERT(nsegs <= MVS_SG_ENTRIES, ("too many DMA segment entries\n")); 1284dd48af36SAlexander Motin /* If there is only one segment - no need to use S/G table on Gen-IIe. */ 1285dd48af36SAlexander Motin if (nsegs == 1 && ch->basic_dma == 0 && (ch->quirks & MVS_Q_GENIIE)) { 1286dd48af36SAlexander Motin slot->dma.addr = segs[0].ds_addr; 1287dd48af36SAlexander Motin slot->dma.len = segs[0].ds_len; 1288dd48af36SAlexander Motin } else { 1289dd48af36SAlexander Motin slot->dma.addr = 0; 1290dd48af36SAlexander Motin /* Get a piece of the workspace for this EPRD */ 1291dd48af36SAlexander Motin eprd = (struct mvs_eprd *) 1292dd48af36SAlexander Motin (ch->dma.workrq + MVS_EPRD_OFFSET + (MVS_EPRD_SIZE * slot->slot)); 1293dd48af36SAlexander Motin /* Fill S/G table */ 1294dd48af36SAlexander Motin for (i = 0; i < nsegs; i++) { 1295dd48af36SAlexander Motin eprd[i].prdbal = htole32(segs[i].ds_addr); 1296dd48af36SAlexander Motin eprd[i].bytecount = htole32(segs[i].ds_len & MVS_EPRD_MASK); 1297dd48af36SAlexander Motin eprd[i].prdbah = htole32((segs[i].ds_addr >> 16) >> 16); 1298dd48af36SAlexander Motin } 1299dd48af36SAlexander Motin eprd[i - 1].bytecount |= htole32(MVS_EPRD_EOF); 1300dd48af36SAlexander Motin } 1301dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1302dd48af36SAlexander Motin ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1303dd48af36SAlexander Motin BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1304dd48af36SAlexander Motin if (ch->basic_dma) 1305dd48af36SAlexander Motin mvs_legacy_execute_transaction(slot); 1306dd48af36SAlexander Motin else 1307dd48af36SAlexander Motin mvs_execute_transaction(slot); 1308dd48af36SAlexander Motin } 1309dd48af36SAlexander Motin 1310dd48af36SAlexander Motin static void 1311dd48af36SAlexander Motin mvs_legacy_execute_transaction(struct mvs_slot *slot) 1312dd48af36SAlexander Motin { 1313dd48af36SAlexander Motin device_t dev = slot->dev; 1314dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1315dd48af36SAlexander Motin bus_addr_t eprd; 1316dd48af36SAlexander Motin union ccb *ccb = slot->ccb; 1317dd48af36SAlexander Motin int port = ccb->ccb_h.target_id & 0x0f; 1318dd48af36SAlexander Motin int timeout; 1319dd48af36SAlexander Motin 1320dd48af36SAlexander Motin slot->state = MVS_SLOT_RUNNING; 1321dd48af36SAlexander Motin ch->rslots |= (1 << slot->slot); 1322dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, SATA_SATAICTL, port << SATA_SATAICTL_PMPTX_SHIFT); 1323dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1324dd48af36SAlexander Motin mvs_tfd_write(dev, ccb); 1325dd48af36SAlexander Motin /* Device reset doesn't interrupt. */ 1326dd48af36SAlexander Motin if (ccb->ataio.cmd.command == ATA_DEVICE_RESET) { 1327dd48af36SAlexander Motin int timeout = 1000000; 1328dd48af36SAlexander Motin do { 1329dd48af36SAlexander Motin DELAY(10); 1330dd48af36SAlexander Motin ccb->ataio.res.status = ATA_INB(ch->r_mem, ATA_STATUS); 1331dd48af36SAlexander Motin } while (ccb->ataio.res.status & ATA_S_BUSY && timeout--); 133270b7af2bSAlexander Motin mvs_legacy_intr(dev, 1); 1333dd48af36SAlexander Motin return; 1334dd48af36SAlexander Motin } 1335dd48af36SAlexander Motin ch->donecount = 0; 13369cf41729SAlexander Motin if (ccb->ataio.cmd.command == ATA_READ_MUL || 13379cf41729SAlexander Motin ccb->ataio.cmd.command == ATA_READ_MUL48 || 13389cf41729SAlexander Motin ccb->ataio.cmd.command == ATA_WRITE_MUL || 13399cf41729SAlexander Motin ccb->ataio.cmd.command == ATA_WRITE_MUL48) { 1340dd48af36SAlexander Motin ch->transfersize = min(ccb->ataio.dxfer_len, 1341dd48af36SAlexander Motin ch->curr[port].bytecount); 13429cf41729SAlexander Motin } else 13439cf41729SAlexander Motin ch->transfersize = min(ccb->ataio.dxfer_len, 512); 1344dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 1345dd48af36SAlexander Motin ch->fake_busy = 1; 1346dd48af36SAlexander Motin /* If data write command - output the data */ 1347dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1348dd48af36SAlexander Motin if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) { 1349c0609c54SAlexander Motin device_printf(dev, 1350c0609c54SAlexander Motin "timeout waiting for write DRQ\n"); 13518d169381SAlexander Motin xpt_freeze_simq(ch->sim, 1); 13528d169381SAlexander Motin ch->toslots |= (1 << slot->slot); 1353dd48af36SAlexander Motin mvs_end_transaction(slot, MVS_ERR_TIMEOUT); 1354dd48af36SAlexander Motin return; 1355dd48af36SAlexander Motin } 1356dd48af36SAlexander Motin ATA_OUTSW_STRM(ch->r_mem, ATA_DATA, 1357dd48af36SAlexander Motin (uint16_t *)(ccb->ataio.data_ptr + ch->donecount), 1358dd48af36SAlexander Motin ch->transfersize / 2); 1359dd48af36SAlexander Motin } 1360dd48af36SAlexander Motin } else { 1361dd48af36SAlexander Motin ch->donecount = 0; 1362dd48af36SAlexander Motin ch->transfersize = min(ccb->csio.dxfer_len, 1363dd48af36SAlexander Motin ch->curr[port].bytecount); 1364dd48af36SAlexander Motin /* Write ATA PACKET command. */ 1365dd48af36SAlexander Motin if (ch->basic_dma) { 1366dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_FEATURE, ATA_F_DMA); 1367dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_LSB, 0); 1368dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_MSB, 0); 1369dd48af36SAlexander Motin } else { 1370dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_FEATURE, 0); 1371dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_LSB, ch->transfersize); 1372dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CYL_MSB, ch->transfersize >> 8); 1373dd48af36SAlexander Motin } 1374dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_COMMAND, ATA_PACKET_CMD); 1375dd48af36SAlexander Motin ch->fake_busy = 1; 1376dd48af36SAlexander Motin /* Wait for ready to write ATAPI command block */ 1377dd48af36SAlexander Motin if (mvs_wait(dev, 0, ATA_S_BUSY, 1000) < 0) { 1378dd48af36SAlexander Motin device_printf(dev, "timeout waiting for ATAPI !BUSY\n"); 13798d169381SAlexander Motin xpt_freeze_simq(ch->sim, 1); 13808d169381SAlexander Motin ch->toslots |= (1 << slot->slot); 1381dd48af36SAlexander Motin mvs_end_transaction(slot, MVS_ERR_TIMEOUT); 1382dd48af36SAlexander Motin return; 1383dd48af36SAlexander Motin } 1384dd48af36SAlexander Motin timeout = 5000; 1385dd48af36SAlexander Motin while (timeout--) { 1386dd48af36SAlexander Motin int reason = ATA_INB(ch->r_mem, ATA_IREASON); 1387dd48af36SAlexander Motin int status = ATA_INB(ch->r_mem, ATA_STATUS); 1388dd48af36SAlexander Motin 1389dd48af36SAlexander Motin if (((reason & (ATA_I_CMD | ATA_I_IN)) | 1390dd48af36SAlexander Motin (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT) 1391dd48af36SAlexander Motin break; 1392dd48af36SAlexander Motin DELAY(20); 1393dd48af36SAlexander Motin } 1394dd48af36SAlexander Motin if (timeout <= 0) { 1395c0609c54SAlexander Motin device_printf(dev, 1396c0609c54SAlexander Motin "timeout waiting for ATAPI command ready\n"); 13978d169381SAlexander Motin xpt_freeze_simq(ch->sim, 1); 13988d169381SAlexander Motin ch->toslots |= (1 << slot->slot); 1399dd48af36SAlexander Motin mvs_end_transaction(slot, MVS_ERR_TIMEOUT); 1400dd48af36SAlexander Motin return; 1401dd48af36SAlexander Motin } 1402dd48af36SAlexander Motin /* Write ATAPI command. */ 1403dd48af36SAlexander Motin ATA_OUTSW_STRM(ch->r_mem, ATA_DATA, 1404dd48af36SAlexander Motin (uint16_t *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1405dd48af36SAlexander Motin ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes), 1406dd48af36SAlexander Motin ch->curr[port].atapi / 2); 1407dd48af36SAlexander Motin DELAY(10); 1408dd48af36SAlexander Motin if (ch->basic_dma) { 1409dd48af36SAlexander Motin /* Start basic DMA. */ 1410dd48af36SAlexander Motin eprd = ch->dma.workrq_bus + MVS_EPRD_OFFSET + 1411dd48af36SAlexander Motin (MVS_EPRD_SIZE * slot->slot); 1412dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, DMA_DTLBA, eprd); 1413dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, DMA_DTHBA, (eprd >> 16) >> 16); 1414dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, DMA_C, DMA_C_START | 1415dd48af36SAlexander Motin (((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) ? 1416dd48af36SAlexander Motin DMA_C_READ : 0)); 141797fd3ac6SAlexander Motin } 1418dd48af36SAlexander Motin } 1419dd48af36SAlexander Motin /* Start command execution timeout */ 142085c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout, 0, 142185c9dd9dSSteven Hartland (timeout_t*)mvs_timeout, slot, 0); 1422dd48af36SAlexander Motin } 1423dd48af36SAlexander Motin 1424dd48af36SAlexander Motin /* Must be called with channel locked. */ 1425dd48af36SAlexander Motin static void 1426dd48af36SAlexander Motin mvs_execute_transaction(struct mvs_slot *slot) 1427dd48af36SAlexander Motin { 1428dd48af36SAlexander Motin device_t dev = slot->dev; 1429dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1430dd48af36SAlexander Motin bus_addr_t eprd; 1431dd48af36SAlexander Motin struct mvs_crqb *crqb; 1432dd48af36SAlexander Motin struct mvs_crqb_gen2e *crqb2e; 1433dd48af36SAlexander Motin union ccb *ccb = slot->ccb; 1434dd48af36SAlexander Motin int port = ccb->ccb_h.target_id & 0x0f; 1435dd48af36SAlexander Motin int i; 1436dd48af36SAlexander Motin 1437dd48af36SAlexander Motin /* Get address of the prepared EPRD */ 1438dd48af36SAlexander Motin eprd = ch->dma.workrq_bus + MVS_EPRD_OFFSET + (MVS_EPRD_SIZE * slot->slot); 1439dd48af36SAlexander Motin /* Prepare CRQB. Gen IIe uses different CRQB format. */ 1440dd48af36SAlexander Motin if (ch->quirks & MVS_Q_GENIIE) { 1441dd48af36SAlexander Motin crqb2e = (struct mvs_crqb_gen2e *) 1442dd48af36SAlexander Motin (ch->dma.workrq + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx)); 1443dd48af36SAlexander Motin crqb2e->ctrlflg = htole32( 1444dd48af36SAlexander Motin ((ccb->ccb_h.flags & CAM_DIR_IN) ? MVS_CRQB2E_READ : 0) | 1445dd48af36SAlexander Motin (slot->tag << MVS_CRQB2E_DTAG_SHIFT) | 1446dd48af36SAlexander Motin (port << MVS_CRQB2E_PMP_SHIFT) | 1447dd48af36SAlexander Motin (slot->slot << MVS_CRQB2E_HTAG_SHIFT)); 1448dd48af36SAlexander Motin /* If there is only one segment - no need to use S/G table. */ 1449dd48af36SAlexander Motin if (slot->dma.addr != 0) { 1450dd48af36SAlexander Motin eprd = slot->dma.addr; 1451dd48af36SAlexander Motin crqb2e->ctrlflg |= htole32(MVS_CRQB2E_CPRD); 1452dd48af36SAlexander Motin crqb2e->drbc = slot->dma.len; 1453dd48af36SAlexander Motin } 1454dd48af36SAlexander Motin crqb2e->cprdbl = htole32(eprd); 1455dd48af36SAlexander Motin crqb2e->cprdbh = htole32((eprd >> 16) >> 16); 1456dd48af36SAlexander Motin crqb2e->cmd[0] = 0; 1457dd48af36SAlexander Motin crqb2e->cmd[1] = 0; 1458dd48af36SAlexander Motin crqb2e->cmd[2] = ccb->ataio.cmd.command; 1459dd48af36SAlexander Motin crqb2e->cmd[3] = ccb->ataio.cmd.features; 1460dd48af36SAlexander Motin crqb2e->cmd[4] = ccb->ataio.cmd.lba_low; 1461dd48af36SAlexander Motin crqb2e->cmd[5] = ccb->ataio.cmd.lba_mid; 1462dd48af36SAlexander Motin crqb2e->cmd[6] = ccb->ataio.cmd.lba_high; 1463dd48af36SAlexander Motin crqb2e->cmd[7] = ccb->ataio.cmd.device; 1464dd48af36SAlexander Motin crqb2e->cmd[8] = ccb->ataio.cmd.lba_low_exp; 1465dd48af36SAlexander Motin crqb2e->cmd[9] = ccb->ataio.cmd.lba_mid_exp; 1466dd48af36SAlexander Motin crqb2e->cmd[10] = ccb->ataio.cmd.lba_high_exp; 1467dd48af36SAlexander Motin crqb2e->cmd[11] = ccb->ataio.cmd.features_exp; 1468dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1469dd48af36SAlexander Motin crqb2e->cmd[12] = slot->tag << 3; 1470dd48af36SAlexander Motin crqb2e->cmd[13] = 0; 1471dd48af36SAlexander Motin } else { 1472dd48af36SAlexander Motin crqb2e->cmd[12] = ccb->ataio.cmd.sector_count; 1473dd48af36SAlexander Motin crqb2e->cmd[13] = ccb->ataio.cmd.sector_count_exp; 1474dd48af36SAlexander Motin } 1475dd48af36SAlexander Motin crqb2e->cmd[14] = 0; 1476dd48af36SAlexander Motin crqb2e->cmd[15] = 0; 1477dd48af36SAlexander Motin } else { 1478dd48af36SAlexander Motin crqb = (struct mvs_crqb *) 1479dd48af36SAlexander Motin (ch->dma.workrq + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx)); 1480dd48af36SAlexander Motin crqb->cprdbl = htole32(eprd); 1481dd48af36SAlexander Motin crqb->cprdbh = htole32((eprd >> 16) >> 16); 1482dd48af36SAlexander Motin crqb->ctrlflg = htole16( 1483dd48af36SAlexander Motin ((ccb->ccb_h.flags & CAM_DIR_IN) ? MVS_CRQB_READ : 0) | 1484dd48af36SAlexander Motin (slot->slot << MVS_CRQB_TAG_SHIFT) | 1485dd48af36SAlexander Motin (port << MVS_CRQB_PMP_SHIFT)); 1486dd48af36SAlexander Motin i = 0; 1487dd48af36SAlexander Motin /* 1488dd48af36SAlexander Motin * Controller can handle only 11 of 12 ATA registers, 1489dd48af36SAlexander Motin * so we have to choose which one to skip. 1490dd48af36SAlexander Motin */ 1491dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1492dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.features_exp; 1493dd48af36SAlexander Motin crqb->cmd[i++] = 0x11; 1494dd48af36SAlexander Motin } 1495dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.features; 1496dd48af36SAlexander Motin crqb->cmd[i++] = 0x11; 1497dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1498dd48af36SAlexander Motin crqb->cmd[i++] = slot->tag << 3; 1499dd48af36SAlexander Motin crqb->cmd[i++] = 0x12; 1500dd48af36SAlexander Motin } else { 1501dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.sector_count_exp; 1502dd48af36SAlexander Motin crqb->cmd[i++] = 0x12; 1503dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.sector_count; 1504dd48af36SAlexander Motin crqb->cmd[i++] = 0x12; 1505dd48af36SAlexander Motin } 1506dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_low_exp; 1507dd48af36SAlexander Motin crqb->cmd[i++] = 0x13; 1508dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_low; 1509dd48af36SAlexander Motin crqb->cmd[i++] = 0x13; 1510dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_mid_exp; 1511dd48af36SAlexander Motin crqb->cmd[i++] = 0x14; 1512dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_mid; 1513dd48af36SAlexander Motin crqb->cmd[i++] = 0x14; 1514dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_high_exp; 1515dd48af36SAlexander Motin crqb->cmd[i++] = 0x15; 1516dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.lba_high; 1517dd48af36SAlexander Motin crqb->cmd[i++] = 0x15; 1518dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.device; 1519dd48af36SAlexander Motin crqb->cmd[i++] = 0x16; 1520dd48af36SAlexander Motin crqb->cmd[i++] = ccb->ataio.cmd.command; 1521dd48af36SAlexander Motin crqb->cmd[i++] = 0x97; 1522dd48af36SAlexander Motin } 1523dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map, 1524dd48af36SAlexander Motin BUS_DMASYNC_PREWRITE); 1525dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map, 1526dd48af36SAlexander Motin BUS_DMASYNC_PREREAD); 1527dd48af36SAlexander Motin slot->state = MVS_SLOT_RUNNING; 1528dd48af36SAlexander Motin ch->rslots |= (1 << slot->slot); 1529dd48af36SAlexander Motin /* Issue command to the controller. */ 1530dd48af36SAlexander Motin ch->out_idx = (ch->out_idx + 1) & (MVS_MAX_SLOTS - 1); 1531dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_REQQIP, 1532dd48af36SAlexander Motin ch->dma.workrq_bus + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx)); 1533dd48af36SAlexander Motin /* Start command execution timeout */ 153485c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout, 0, 153585c9dd9dSSteven Hartland (timeout_t*)mvs_timeout, slot, 0); 1536dd48af36SAlexander Motin return; 1537dd48af36SAlexander Motin } 1538dd48af36SAlexander Motin 1539dd48af36SAlexander Motin /* Must be called with channel locked. */ 1540dd48af36SAlexander Motin static void 1541dd48af36SAlexander Motin mvs_process_timeout(device_t dev) 1542dd48af36SAlexander Motin { 1543dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1544dd48af36SAlexander Motin int i; 1545dd48af36SAlexander Motin 1546dd48af36SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 1547dd48af36SAlexander Motin /* Handle the rest of commands. */ 1548dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 1549dd48af36SAlexander Motin /* Do we have a running request on slot? */ 1550dd48af36SAlexander Motin if (ch->slot[i].state < MVS_SLOT_RUNNING) 1551dd48af36SAlexander Motin continue; 1552dd48af36SAlexander Motin mvs_end_transaction(&ch->slot[i], MVS_ERR_TIMEOUT); 1553dd48af36SAlexander Motin } 1554dd48af36SAlexander Motin } 1555dd48af36SAlexander Motin 1556dd48af36SAlexander Motin /* Must be called with channel locked. */ 1557dd48af36SAlexander Motin static void 1558dd48af36SAlexander Motin mvs_rearm_timeout(device_t dev) 1559dd48af36SAlexander Motin { 1560dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1561dd48af36SAlexander Motin int i; 1562dd48af36SAlexander Motin 1563dd48af36SAlexander Motin mtx_assert(&ch->mtx, MA_OWNED); 1564dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 1565dd48af36SAlexander Motin struct mvs_slot *slot = &ch->slot[i]; 1566dd48af36SAlexander Motin 1567dd48af36SAlexander Motin /* Do we have a running request on slot? */ 1568dd48af36SAlexander Motin if (slot->state < MVS_SLOT_RUNNING) 1569dd48af36SAlexander Motin continue; 1570dd48af36SAlexander Motin if ((ch->toslots & (1 << i)) == 0) 1571dd48af36SAlexander Motin continue; 157285c9dd9dSSteven Hartland callout_reset_sbt(&slot->timeout, 157385c9dd9dSSteven Hartland SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0, 157485c9dd9dSSteven Hartland (timeout_t*)mvs_timeout, slot, 0); 1575dd48af36SAlexander Motin } 1576dd48af36SAlexander Motin } 1577dd48af36SAlexander Motin 1578dd48af36SAlexander Motin /* Locked by callout mechanism. */ 1579dd48af36SAlexander Motin static void 1580dd48af36SAlexander Motin mvs_timeout(struct mvs_slot *slot) 1581dd48af36SAlexander Motin { 1582dd48af36SAlexander Motin device_t dev = slot->dev; 1583dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1584dd48af36SAlexander Motin 1585dd48af36SAlexander Motin /* Check for stale timeout. */ 1586dd48af36SAlexander Motin if (slot->state < MVS_SLOT_RUNNING) 1587dd48af36SAlexander Motin return; 1588dd48af36SAlexander Motin device_printf(dev, "Timeout on slot %d\n", slot->slot); 1589dd48af36SAlexander Motin device_printf(dev, "iec %08x sstat %08x serr %08x edma_s %08x " 1590dd48af36SAlexander Motin "dma_c %08x dma_s %08x rs %08x status %02x\n", 1591dd48af36SAlexander Motin ATA_INL(ch->r_mem, EDMA_IEC), 1592dd48af36SAlexander Motin ATA_INL(ch->r_mem, SATA_SS), ATA_INL(ch->r_mem, SATA_SE), 1593dd48af36SAlexander Motin ATA_INL(ch->r_mem, EDMA_S), ATA_INL(ch->r_mem, DMA_C), 1594dd48af36SAlexander Motin ATA_INL(ch->r_mem, DMA_S), ch->rslots, 1595dd48af36SAlexander Motin ATA_INB(ch->r_mem, ATA_ALTSTAT)); 1596dd48af36SAlexander Motin /* Handle frozen command. */ 1597dd48af36SAlexander Motin mvs_requeue_frozen(dev); 1598dd48af36SAlexander Motin /* We wait for other commands timeout and pray. */ 1599dd48af36SAlexander Motin if (ch->toslots == 0) 1600dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 1601dd48af36SAlexander Motin ch->toslots |= (1 << slot->slot); 1602dd48af36SAlexander Motin if ((ch->rslots & ~ch->toslots) == 0) 1603dd48af36SAlexander Motin mvs_process_timeout(dev); 1604dd48af36SAlexander Motin else 1605dd48af36SAlexander Motin device_printf(dev, " ... waiting for slots %08x\n", 1606dd48af36SAlexander Motin ch->rslots & ~ch->toslots); 1607dd48af36SAlexander Motin } 1608dd48af36SAlexander Motin 1609dd48af36SAlexander Motin /* Must be called with channel locked. */ 1610dd48af36SAlexander Motin static void 1611dd48af36SAlexander Motin mvs_end_transaction(struct mvs_slot *slot, enum mvs_err_type et) 1612dd48af36SAlexander Motin { 1613dd48af36SAlexander Motin device_t dev = slot->dev; 1614dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1615dd48af36SAlexander Motin union ccb *ccb = slot->ccb; 1616bf12976cSAlexander Motin int lastto; 1617dd48af36SAlexander Motin 1618dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map, 1619dd48af36SAlexander Motin BUS_DMASYNC_POSTWRITE); 1620dd48af36SAlexander Motin /* Read result registers to the result struct 1621dd48af36SAlexander Motin * May be incorrect if several commands finished same time, 1622dd48af36SAlexander Motin * so read only when sure or have to. 1623dd48af36SAlexander Motin */ 1624dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1625dd48af36SAlexander Motin struct ata_res *res = &ccb->ataio.res; 1626dd48af36SAlexander Motin 1627dd48af36SAlexander Motin if ((et == MVS_ERR_TFE) || 1628dd48af36SAlexander Motin (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1629dd48af36SAlexander Motin mvs_tfd_read(dev, ccb); 1630dd48af36SAlexander Motin } else 1631dd48af36SAlexander Motin bzero(res, sizeof(*res)); 163297fd3ac6SAlexander Motin } else { 163397fd3ac6SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 163497fd3ac6SAlexander Motin ch->basic_dma == 0) 163597fd3ac6SAlexander Motin ccb->csio.resid = ccb->csio.dxfer_len - ch->donecount; 1636dd48af36SAlexander Motin } 1637dd48af36SAlexander Motin if (ch->numpslots == 0 || ch->basic_dma) { 1638dd48af36SAlexander Motin if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1639dd48af36SAlexander Motin bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1640dd48af36SAlexander Motin (ccb->ccb_h.flags & CAM_DIR_IN) ? 1641dd48af36SAlexander Motin BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1642dd48af36SAlexander Motin bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1643dd48af36SAlexander Motin } 1644dd48af36SAlexander Motin } 1645dd48af36SAlexander Motin if (et != MVS_ERR_NONE) 1646dd48af36SAlexander Motin ch->eslots |= (1 << slot->slot); 1647dd48af36SAlexander Motin /* In case of error, freeze device for proper recovery. */ 164897fd3ac6SAlexander Motin if ((et != MVS_ERR_NONE) && (!ch->recoverycmd) && 1649dd48af36SAlexander Motin !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1650dd48af36SAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1); 1651dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_DEV_QFRZN; 1652dd48af36SAlexander Motin } 1653dd48af36SAlexander Motin /* Set proper result status. */ 1654dd48af36SAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1655dd48af36SAlexander Motin switch (et) { 1656dd48af36SAlexander Motin case MVS_ERR_NONE: 1657dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_REQ_CMP; 1658dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1659dd48af36SAlexander Motin ccb->csio.scsi_status = SCSI_STATUS_OK; 1660dd48af36SAlexander Motin break; 1661dd48af36SAlexander Motin case MVS_ERR_INVALID: 1662dd48af36SAlexander Motin ch->fatalerr = 1; 1663dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_REQ_INVALID; 1664dd48af36SAlexander Motin break; 1665dd48af36SAlexander Motin case MVS_ERR_INNOCENT: 1666dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1667dd48af36SAlexander Motin break; 1668dd48af36SAlexander Motin case MVS_ERR_TFE: 1669dd48af36SAlexander Motin case MVS_ERR_NCQ: 1670dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1671dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1672dd48af36SAlexander Motin ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1673dd48af36SAlexander Motin } else { 1674dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1675dd48af36SAlexander Motin } 1676dd48af36SAlexander Motin break; 1677dd48af36SAlexander Motin case MVS_ERR_SATA: 1678dd48af36SAlexander Motin ch->fatalerr = 1; 167997fd3ac6SAlexander Motin if (!ch->recoverycmd) { 1680dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 1681dd48af36SAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1682dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1683dd48af36SAlexander Motin } 1684dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1685dd48af36SAlexander Motin break; 1686dd48af36SAlexander Motin case MVS_ERR_TIMEOUT: 168797fd3ac6SAlexander Motin if (!ch->recoverycmd) { 1688dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 1689dd48af36SAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1690dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1691dd48af36SAlexander Motin } 1692dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1693dd48af36SAlexander Motin break; 1694dd48af36SAlexander Motin default: 1695dd48af36SAlexander Motin ch->fatalerr = 1; 1696dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1697dd48af36SAlexander Motin } 1698dd48af36SAlexander Motin /* Free slot. */ 1699dd48af36SAlexander Motin ch->oslots &= ~(1 << slot->slot); 1700dd48af36SAlexander Motin ch->rslots &= ~(1 << slot->slot); 1701dd48af36SAlexander Motin ch->aslots &= ~(1 << slot->slot); 1702dd48af36SAlexander Motin slot->state = MVS_SLOT_EMPTY; 1703dd48af36SAlexander Motin slot->ccb = NULL; 1704dd48af36SAlexander Motin /* Update channel stats. */ 1705dd48af36SAlexander Motin ch->numrslots--; 1706dd48af36SAlexander Motin ch->numrslotspd[ccb->ccb_h.target_id]--; 1707dd48af36SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1708dd48af36SAlexander Motin if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1709dd48af36SAlexander Motin ch->otagspd[ccb->ccb_h.target_id] &= ~(1 << slot->tag); 1710dd48af36SAlexander Motin ch->numtslots--; 1711dd48af36SAlexander Motin ch->numtslotspd[ccb->ccb_h.target_id]--; 1712dd48af36SAlexander Motin } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) { 1713dd48af36SAlexander Motin ch->numdslots--; 1714dd48af36SAlexander Motin } else { 1715dd48af36SAlexander Motin ch->numpslots--; 1716dd48af36SAlexander Motin } 1717dd48af36SAlexander Motin } else { 1718dd48af36SAlexander Motin ch->numpslots--; 1719dd48af36SAlexander Motin ch->basic_dma = 0; 1720dd48af36SAlexander Motin } 1721bf12976cSAlexander Motin /* Cancel timeout state if request completed normally. */ 1722bf12976cSAlexander Motin if (et != MVS_ERR_TIMEOUT) { 1723bf12976cSAlexander Motin lastto = (ch->toslots == (1 << slot->slot)); 1724bf12976cSAlexander Motin ch->toslots &= ~(1 << slot->slot); 1725bf12976cSAlexander Motin if (lastto) 1726bf12976cSAlexander Motin xpt_release_simq(ch->sim, TRUE); 1727bf12976cSAlexander Motin } 1728dd48af36SAlexander Motin /* If it was our READ LOG command - process it. */ 172997fd3ac6SAlexander Motin if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 1730dd48af36SAlexander Motin mvs_process_read_log(dev, ccb); 173197fd3ac6SAlexander Motin /* If it was our REQUEST SENSE command - process it. */ 173297fd3ac6SAlexander Motin } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 173397fd3ac6SAlexander Motin mvs_process_request_sense(dev, ccb); 173497fd3ac6SAlexander Motin /* If it was NCQ or ATAPI command error, put result on hold. */ 173597fd3ac6SAlexander Motin } else if (et == MVS_ERR_NCQ || 173697fd3ac6SAlexander Motin ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 173797fd3ac6SAlexander Motin (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 1738dd48af36SAlexander Motin ch->hold[slot->slot] = ccb; 1739dd48af36SAlexander Motin ch->holdtag[slot->slot] = slot->tag; 1740dd48af36SAlexander Motin ch->numhslots++; 1741dd48af36SAlexander Motin } else 1742dd48af36SAlexander Motin xpt_done(ccb); 1743dd48af36SAlexander Motin /* If we have no other active commands, ... */ 1744dd48af36SAlexander Motin if (ch->rslots == 0) { 1745dd48af36SAlexander Motin /* if there was fatal error - reset port. */ 1746dd48af36SAlexander Motin if (ch->toslots != 0 || ch->fatalerr) { 1747dd48af36SAlexander Motin mvs_reset(dev); 1748dd48af36SAlexander Motin } else { 1749dd48af36SAlexander Motin /* if we have slots in error, we can reinit port. */ 1750dd48af36SAlexander Motin if (ch->eslots != 0) { 1751dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 1752dd48af36SAlexander Motin ch->eslots = 0; 1753dd48af36SAlexander Motin } 1754dd48af36SAlexander Motin /* if there commands on hold, we can do READ LOG. */ 175597fd3ac6SAlexander Motin if (!ch->recoverycmd && ch->numhslots) 175697fd3ac6SAlexander Motin mvs_issue_recovery(dev); 1757dd48af36SAlexander Motin } 1758dd48af36SAlexander Motin /* If all the rest of commands are in timeout - give them chance. */ 1759dd48af36SAlexander Motin } else if ((ch->rslots & ~ch->toslots) == 0 && 1760dd48af36SAlexander Motin et != MVS_ERR_TIMEOUT) 1761dd48af36SAlexander Motin mvs_rearm_timeout(dev); 176208c8fde0SAlexander Motin /* Unfreeze frozen command. */ 176308c8fde0SAlexander Motin if (ch->frozen && !mvs_check_collision(dev, ch->frozen)) { 176408c8fde0SAlexander Motin union ccb *fccb = ch->frozen; 176508c8fde0SAlexander Motin ch->frozen = NULL; 176608c8fde0SAlexander Motin mvs_begin_transaction(dev, fccb); 176708c8fde0SAlexander Motin xpt_release_simq(ch->sim, TRUE); 176808c8fde0SAlexander Motin } 1769dd48af36SAlexander Motin /* Start PM timer. */ 1770dd48af36SAlexander Motin if (ch->numrslots == 0 && ch->pm_level > 3 && 1771dd48af36SAlexander Motin (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) { 1772dd48af36SAlexander Motin callout_schedule(&ch->pm_timer, 1773dd48af36SAlexander Motin (ch->pm_level == 4) ? hz / 1000 : hz / 8); 1774dd48af36SAlexander Motin } 1775dd48af36SAlexander Motin } 1776dd48af36SAlexander Motin 1777dd48af36SAlexander Motin static void 177897fd3ac6SAlexander Motin mvs_issue_recovery(device_t dev) 1779dd48af36SAlexander Motin { 1780dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1781dd48af36SAlexander Motin union ccb *ccb; 1782dd48af36SAlexander Motin struct ccb_ataio *ataio; 178397fd3ac6SAlexander Motin struct ccb_scsiio *csio; 1784dd48af36SAlexander Motin int i; 1785dd48af36SAlexander Motin 17867bcc5957SAlexander Motin /* Find some held command. */ 1787dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 1788dd48af36SAlexander Motin if (ch->hold[i]) 1789dd48af36SAlexander Motin break; 1790dd48af36SAlexander Motin } 1791dd48af36SAlexander Motin ccb = xpt_alloc_ccb_nowait(); 1792dd48af36SAlexander Motin if (ccb == NULL) { 17937bcc5957SAlexander Motin device_printf(dev, "Unable to allocate recovery command\n"); 17946ac0befdSAlexander Motin completeall: 17957bcc5957SAlexander Motin /* We can't do anything -- complete held commands. */ 17966ac0befdSAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 17976ac0befdSAlexander Motin if (ch->hold[i] == NULL) 17986ac0befdSAlexander Motin continue; 17996ac0befdSAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 18006ac0befdSAlexander Motin ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL; 18016ac0befdSAlexander Motin xpt_done(ch->hold[i]); 18026ac0befdSAlexander Motin ch->hold[i] = NULL; 18036ac0befdSAlexander Motin ch->numhslots--; 18046ac0befdSAlexander Motin } 18056ac0befdSAlexander Motin mvs_reset(dev); 18066ac0befdSAlexander Motin return; 1807dd48af36SAlexander Motin } 1808dd48af36SAlexander Motin ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 180997fd3ac6SAlexander Motin if (ccb->ccb_h.func_code == XPT_ATA_IO) { 181097fd3ac6SAlexander Motin /* READ LOG */ 181197fd3ac6SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 1812dd48af36SAlexander Motin ccb->ccb_h.func_code = XPT_ATA_IO; 1813dd48af36SAlexander Motin ccb->ccb_h.flags = CAM_DIR_IN; 1814dd48af36SAlexander Motin ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1815dd48af36SAlexander Motin ataio = &ccb->ataio; 1816dd48af36SAlexander Motin ataio->data_ptr = malloc(512, M_MVS, M_NOWAIT); 1817dd48af36SAlexander Motin if (ataio->data_ptr == NULL) { 1818de29bf5eSAlexander Motin xpt_free_ccb(ccb); 18196ac0befdSAlexander Motin device_printf(dev, 18207bcc5957SAlexander Motin "Unable to allocate memory for READ LOG command\n"); 18216ac0befdSAlexander Motin goto completeall; 1822dd48af36SAlexander Motin } 1823dd48af36SAlexander Motin ataio->dxfer_len = 512; 1824dd48af36SAlexander Motin bzero(&ataio->cmd, sizeof(ataio->cmd)); 1825dd48af36SAlexander Motin ataio->cmd.flags = CAM_ATAIO_48BIT; 1826dd48af36SAlexander Motin ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1827dd48af36SAlexander Motin ataio->cmd.sector_count = 1; 1828dd48af36SAlexander Motin ataio->cmd.sector_count_exp = 0; 1829dd48af36SAlexander Motin ataio->cmd.lba_low = 0x10; 1830dd48af36SAlexander Motin ataio->cmd.lba_mid = 0; 1831dd48af36SAlexander Motin ataio->cmd.lba_mid_exp = 0; 183297fd3ac6SAlexander Motin } else { 183397fd3ac6SAlexander Motin /* REQUEST SENSE */ 183497fd3ac6SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 183597fd3ac6SAlexander Motin ccb->ccb_h.recovery_slot = i; 183697fd3ac6SAlexander Motin ccb->ccb_h.func_code = XPT_SCSI_IO; 183797fd3ac6SAlexander Motin ccb->ccb_h.flags = CAM_DIR_IN; 183897fd3ac6SAlexander Motin ccb->ccb_h.status = 0; 183997fd3ac6SAlexander Motin ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 184097fd3ac6SAlexander Motin csio = &ccb->csio; 184197fd3ac6SAlexander Motin csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 184297fd3ac6SAlexander Motin csio->dxfer_len = ch->hold[i]->csio.sense_len; 184397fd3ac6SAlexander Motin csio->cdb_len = 6; 184497fd3ac6SAlexander Motin bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 184597fd3ac6SAlexander Motin csio->cdb_io.cdb_bytes[0] = 0x03; 184697fd3ac6SAlexander Motin csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 184797fd3ac6SAlexander Motin } 18486ac0befdSAlexander Motin /* Freeze SIM while doing recovery. */ 18496ac0befdSAlexander Motin ch->recoverycmd = 1; 1850dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 1851dd48af36SAlexander Motin mvs_begin_transaction(dev, ccb); 1852dd48af36SAlexander Motin } 1853dd48af36SAlexander Motin 1854dd48af36SAlexander Motin static void 1855dd48af36SAlexander Motin mvs_process_read_log(device_t dev, union ccb *ccb) 1856dd48af36SAlexander Motin { 1857dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1858dd48af36SAlexander Motin uint8_t *data; 1859dd48af36SAlexander Motin struct ata_res *res; 1860dd48af36SAlexander Motin int i; 1861dd48af36SAlexander Motin 186297fd3ac6SAlexander Motin ch->recoverycmd = 0; 1863dd48af36SAlexander Motin 1864dd48af36SAlexander Motin data = ccb->ataio.data_ptr; 1865dd48af36SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1866dd48af36SAlexander Motin (data[0] & 0x80) == 0) { 1867dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 1868dd48af36SAlexander Motin if (!ch->hold[i]) 1869dd48af36SAlexander Motin continue; 1870dd48af36SAlexander Motin if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1871dd48af36SAlexander Motin continue; 1872dd48af36SAlexander Motin if ((data[0] & 0x1F) == ch->holdtag[i]) { 1873dd48af36SAlexander Motin res = &ch->hold[i]->ataio.res; 1874dd48af36SAlexander Motin res->status = data[2]; 1875dd48af36SAlexander Motin res->error = data[3]; 1876dd48af36SAlexander Motin res->lba_low = data[4]; 1877dd48af36SAlexander Motin res->lba_mid = data[5]; 1878dd48af36SAlexander Motin res->lba_high = data[6]; 1879dd48af36SAlexander Motin res->device = data[7]; 1880dd48af36SAlexander Motin res->lba_low_exp = data[8]; 1881dd48af36SAlexander Motin res->lba_mid_exp = data[9]; 1882dd48af36SAlexander Motin res->lba_high_exp = data[10]; 1883dd48af36SAlexander Motin res->sector_count = data[12]; 1884dd48af36SAlexander Motin res->sector_count_exp = data[13]; 1885dd48af36SAlexander Motin } else { 1886dd48af36SAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1887dd48af36SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1888dd48af36SAlexander Motin } 1889dd48af36SAlexander Motin xpt_done(ch->hold[i]); 1890dd48af36SAlexander Motin ch->hold[i] = NULL; 1891dd48af36SAlexander Motin ch->numhslots--; 1892dd48af36SAlexander Motin } 1893dd48af36SAlexander Motin } else { 1894dd48af36SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1895dd48af36SAlexander Motin device_printf(dev, "Error while READ LOG EXT\n"); 1896dd48af36SAlexander Motin else if ((data[0] & 0x80) == 0) { 1897c0609c54SAlexander Motin device_printf(dev, 1898c0609c54SAlexander Motin "Non-queued command error in READ LOG EXT\n"); 1899dd48af36SAlexander Motin } 1900dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 1901dd48af36SAlexander Motin if (!ch->hold[i]) 1902dd48af36SAlexander Motin continue; 1903dd48af36SAlexander Motin if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id) 1904dd48af36SAlexander Motin continue; 1905dd48af36SAlexander Motin xpt_done(ch->hold[i]); 1906dd48af36SAlexander Motin ch->hold[i] = NULL; 1907dd48af36SAlexander Motin ch->numhslots--; 1908dd48af36SAlexander Motin } 1909dd48af36SAlexander Motin } 1910dd48af36SAlexander Motin free(ccb->ataio.data_ptr, M_MVS); 1911dd48af36SAlexander Motin xpt_free_ccb(ccb); 1912dd48af36SAlexander Motin xpt_release_simq(ch->sim, TRUE); 1913dd48af36SAlexander Motin } 1914dd48af36SAlexander Motin 191597fd3ac6SAlexander Motin static void 191697fd3ac6SAlexander Motin mvs_process_request_sense(device_t dev, union ccb *ccb) 191797fd3ac6SAlexander Motin { 191897fd3ac6SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 191997fd3ac6SAlexander Motin int i; 192097fd3ac6SAlexander Motin 192197fd3ac6SAlexander Motin ch->recoverycmd = 0; 192297fd3ac6SAlexander Motin 192397fd3ac6SAlexander Motin i = ccb->ccb_h.recovery_slot; 192497fd3ac6SAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 192597fd3ac6SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 192697fd3ac6SAlexander Motin } else { 192797fd3ac6SAlexander Motin ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 192897fd3ac6SAlexander Motin ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 192997fd3ac6SAlexander Motin } 193097fd3ac6SAlexander Motin xpt_done(ch->hold[i]); 193197fd3ac6SAlexander Motin ch->hold[i] = NULL; 193297fd3ac6SAlexander Motin ch->numhslots--; 193397fd3ac6SAlexander Motin xpt_free_ccb(ccb); 193497fd3ac6SAlexander Motin xpt_release_simq(ch->sim, TRUE); 193597fd3ac6SAlexander Motin } 193697fd3ac6SAlexander Motin 1937dd48af36SAlexander Motin static int 1938dd48af36SAlexander Motin mvs_wait(device_t dev, u_int s, u_int c, int t) 1939dd48af36SAlexander Motin { 1940dd48af36SAlexander Motin int timeout = 0; 1941dd48af36SAlexander Motin uint8_t st; 1942dd48af36SAlexander Motin 1943dd48af36SAlexander Motin while (((st = mvs_getstatus(dev, 0)) & (s | c)) != s) { 194470b7af2bSAlexander Motin if (timeout >= t) { 194570b7af2bSAlexander Motin if (t != 0) 1946dd48af36SAlexander Motin device_printf(dev, "Wait status %02x\n", st); 1947dd48af36SAlexander Motin return (-1); 1948dd48af36SAlexander Motin } 194970b7af2bSAlexander Motin DELAY(1000); 195070b7af2bSAlexander Motin timeout++; 1951dd48af36SAlexander Motin } 1952dd48af36SAlexander Motin return (timeout); 1953dd48af36SAlexander Motin } 1954dd48af36SAlexander Motin 1955dd48af36SAlexander Motin static void 1956dd48af36SAlexander Motin mvs_requeue_frozen(device_t dev) 1957dd48af36SAlexander Motin { 1958dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 1959dd48af36SAlexander Motin union ccb *fccb = ch->frozen; 1960dd48af36SAlexander Motin 1961dd48af36SAlexander Motin if (fccb) { 1962dd48af36SAlexander Motin ch->frozen = NULL; 1963dd48af36SAlexander Motin fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1964dd48af36SAlexander Motin if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1965dd48af36SAlexander Motin xpt_freeze_devq(fccb->ccb_h.path, 1); 1966dd48af36SAlexander Motin fccb->ccb_h.status |= CAM_DEV_QFRZN; 1967dd48af36SAlexander Motin } 1968dd48af36SAlexander Motin xpt_done(fccb); 1969dd48af36SAlexander Motin } 1970dd48af36SAlexander Motin } 1971dd48af36SAlexander Motin 1972dd48af36SAlexander Motin static void 197370b7af2bSAlexander Motin mvs_reset_to(void *arg) 197470b7af2bSAlexander Motin { 197570b7af2bSAlexander Motin device_t dev = arg; 197670b7af2bSAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 197770b7af2bSAlexander Motin int t; 197870b7af2bSAlexander Motin 197970b7af2bSAlexander Motin if (ch->resetting == 0) 198070b7af2bSAlexander Motin return; 198170b7af2bSAlexander Motin ch->resetting--; 198270b7af2bSAlexander Motin if ((t = mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ, 0)) >= 0) { 198370b7af2bSAlexander Motin if (bootverbose) { 198470b7af2bSAlexander Motin device_printf(dev, 198570b7af2bSAlexander Motin "MVS reset: device ready after %dms\n", 198670b7af2bSAlexander Motin (310 - ch->resetting) * 100); 198770b7af2bSAlexander Motin } 198870b7af2bSAlexander Motin ch->resetting = 0; 198970b7af2bSAlexander Motin xpt_release_simq(ch->sim, TRUE); 199070b7af2bSAlexander Motin return; 199170b7af2bSAlexander Motin } 199270b7af2bSAlexander Motin if (ch->resetting == 0) { 199370b7af2bSAlexander Motin device_printf(dev, 199470b7af2bSAlexander Motin "MVS reset: device not ready after 31000ms\n"); 199570b7af2bSAlexander Motin xpt_release_simq(ch->sim, TRUE); 199670b7af2bSAlexander Motin return; 199770b7af2bSAlexander Motin } 199870b7af2bSAlexander Motin callout_schedule(&ch->reset_timer, hz / 10); 199970b7af2bSAlexander Motin } 200070b7af2bSAlexander Motin 200170b7af2bSAlexander Motin static void 2002b30c7d51SAlexander Motin mvs_errata(device_t dev) 2003b30c7d51SAlexander Motin { 2004b30c7d51SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 2005b30c7d51SAlexander Motin uint32_t val; 2006b30c7d51SAlexander Motin 2007b30c7d51SAlexander Motin if (ch->quirks & MVS_Q_SOC65) { 2008b30c7d51SAlexander Motin val = ATA_INL(ch->r_mem, SATA_PHYM3); 2009b30c7d51SAlexander Motin val &= ~(0x3 << 27); /* SELMUPF = 1 */ 2010b30c7d51SAlexander Motin val |= (0x1 << 27); 2011b30c7d51SAlexander Motin val &= ~(0x3 << 29); /* SELMUPI = 1 */ 2012b30c7d51SAlexander Motin val |= (0x1 << 29); 2013b30c7d51SAlexander Motin ATA_OUTL(ch->r_mem, SATA_PHYM3, val); 2014b30c7d51SAlexander Motin 2015b30c7d51SAlexander Motin val = ATA_INL(ch->r_mem, SATA_PHYM4); 2016b30c7d51SAlexander Motin val &= ~0x1; /* SATU_OD8 = 0 */ 2017b30c7d51SAlexander Motin val |= (0x1 << 16); /* reserved bit 16 = 1 */ 2018b30c7d51SAlexander Motin ATA_OUTL(ch->r_mem, SATA_PHYM4, val); 2019b30c7d51SAlexander Motin 2020b30c7d51SAlexander Motin val = ATA_INL(ch->r_mem, SATA_PHYM9_GEN2); 2021b30c7d51SAlexander Motin val &= ~0xf; /* TXAMP[3:0] = 8 */ 2022b30c7d51SAlexander Motin val |= 0x8; 2023b30c7d51SAlexander Motin val &= ~(0x1 << 14); /* TXAMP[4] = 0 */ 2024b30c7d51SAlexander Motin ATA_OUTL(ch->r_mem, SATA_PHYM9_GEN2, val); 2025b30c7d51SAlexander Motin 2026b30c7d51SAlexander Motin val = ATA_INL(ch->r_mem, SATA_PHYM9_GEN1); 2027b30c7d51SAlexander Motin val &= ~0xf; /* TXAMP[3:0] = 8 */ 2028b30c7d51SAlexander Motin val |= 0x8; 2029b30c7d51SAlexander Motin val &= ~(0x1 << 14); /* TXAMP[4] = 0 */ 2030b30c7d51SAlexander Motin ATA_OUTL(ch->r_mem, SATA_PHYM9_GEN1, val); 2031b30c7d51SAlexander Motin } 2032b30c7d51SAlexander Motin } 2033b30c7d51SAlexander Motin 2034b30c7d51SAlexander Motin static void 2035dd48af36SAlexander Motin mvs_reset(device_t dev) 2036dd48af36SAlexander Motin { 2037dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 2038dd48af36SAlexander Motin int i; 2039dd48af36SAlexander Motin 2040dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 2041dd48af36SAlexander Motin if (bootverbose) 2042dd48af36SAlexander Motin device_printf(dev, "MVS reset...\n"); 204370b7af2bSAlexander Motin /* Forget about previous reset. */ 204470b7af2bSAlexander Motin if (ch->resetting) { 204570b7af2bSAlexander Motin ch->resetting = 0; 204670b7af2bSAlexander Motin callout_stop(&ch->reset_timer); 204770b7af2bSAlexander Motin xpt_release_simq(ch->sim, TRUE); 204870b7af2bSAlexander Motin } 2049dd48af36SAlexander Motin /* Requeue freezed command. */ 2050dd48af36SAlexander Motin mvs_requeue_frozen(dev); 2051dd48af36SAlexander Motin /* Kill the engine and requeue all running commands. */ 2052dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 2053dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, DMA_C, 0); 2054dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 2055dd48af36SAlexander Motin /* Do we have a running request on slot? */ 2056dd48af36SAlexander Motin if (ch->slot[i].state < MVS_SLOT_RUNNING) 2057dd48af36SAlexander Motin continue; 2058dd48af36SAlexander Motin /* XXX; Commands in loading state. */ 2059dd48af36SAlexander Motin mvs_end_transaction(&ch->slot[i], MVS_ERR_INNOCENT); 2060dd48af36SAlexander Motin } 2061dd48af36SAlexander Motin for (i = 0; i < MVS_MAX_SLOTS; i++) { 2062dd48af36SAlexander Motin if (!ch->hold[i]) 2063dd48af36SAlexander Motin continue; 2064dd48af36SAlexander Motin xpt_done(ch->hold[i]); 2065dd48af36SAlexander Motin ch->hold[i] = NULL; 2066dd48af36SAlexander Motin ch->numhslots--; 2067dd48af36SAlexander Motin } 2068dd48af36SAlexander Motin if (ch->toslots != 0) 2069dd48af36SAlexander Motin xpt_release_simq(ch->sim, TRUE); 2070dd48af36SAlexander Motin ch->eslots = 0; 2071dd48af36SAlexander Motin ch->toslots = 0; 2072dd48af36SAlexander Motin ch->fatalerr = 0; 207370b7af2bSAlexander Motin ch->fake_busy = 0; 2074dd48af36SAlexander Motin /* Tell the XPT about the event */ 2075dd48af36SAlexander Motin xpt_async(AC_BUS_RESET, ch->path, NULL); 2076dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, 0); 2077dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EATARST); 2078dd48af36SAlexander Motin DELAY(25); 2079dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_CMD, 0); 2080b30c7d51SAlexander Motin mvs_errata(dev); 2081dd48af36SAlexander Motin /* Reset and reconnect PHY, */ 2082dd48af36SAlexander Motin if (!mvs_sata_phy_reset(dev)) { 2083dd48af36SAlexander Motin if (bootverbose) 208470b7af2bSAlexander Motin device_printf(dev, "MVS reset: device not found\n"); 2085dd48af36SAlexander Motin ch->devices = 0; 2086dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff); 2087dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEC, 0); 2088dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT); 2089dd48af36SAlexander Motin xpt_release_simq(ch->sim, TRUE); 2090dd48af36SAlexander Motin return; 2091dd48af36SAlexander Motin } 209270b7af2bSAlexander Motin if (bootverbose) 209370b7af2bSAlexander Motin device_printf(dev, "MVS reset: device found\n"); 2094dd48af36SAlexander Motin /* Wait for clearing busy status. */ 209570b7af2bSAlexander Motin if ((i = mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ, 209670b7af2bSAlexander Motin dumping ? 31000 : 0)) < 0) { 209770b7af2bSAlexander Motin if (dumping) { 209870b7af2bSAlexander Motin device_printf(dev, 209970b7af2bSAlexander Motin "MVS reset: device not ready after 31000ms\n"); 210070b7af2bSAlexander Motin } else 210170b7af2bSAlexander Motin ch->resetting = 310; 210270b7af2bSAlexander Motin } else if (bootverbose) 210370b7af2bSAlexander Motin device_printf(dev, "MVS reset: device ready after %dms\n", i); 2104dd48af36SAlexander Motin ch->devices = 1; 2105dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff); 2106dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEC, 0); 2107dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT); 210870b7af2bSAlexander Motin if (ch->resetting) 210970b7af2bSAlexander Motin callout_reset(&ch->reset_timer, hz / 10, mvs_reset_to, dev); 211070b7af2bSAlexander Motin else 2111dd48af36SAlexander Motin xpt_release_simq(ch->sim, TRUE); 2112dd48af36SAlexander Motin } 2113dd48af36SAlexander Motin 2114dd48af36SAlexander Motin static void 2115dd48af36SAlexander Motin mvs_softreset(device_t dev, union ccb *ccb) 2116dd48af36SAlexander Motin { 2117dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 2118dd48af36SAlexander Motin int port = ccb->ccb_h.target_id & 0x0f; 211908c8fde0SAlexander Motin int i, stuck; 212008c8fde0SAlexander Motin uint8_t status; 2121dd48af36SAlexander Motin 2122dd48af36SAlexander Motin mvs_set_edma_mode(dev, MVS_EDMA_OFF); 2123dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, SATA_SATAICTL, port << SATA_SATAICTL_PMPTX_SHIFT); 2124dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_RESET); 2125dd48af36SAlexander Motin DELAY(10000); 2126dd48af36SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, 0); 2127dd48af36SAlexander Motin ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2128dd48af36SAlexander Motin /* Wait for clearing busy status. */ 212908c8fde0SAlexander Motin if ((i = mvs_wait(dev, 0, ATA_S_BUSY, ccb->ccb_h.timeout)) < 0) { 2130dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 213108c8fde0SAlexander Motin stuck = 1; 2132dd48af36SAlexander Motin } else { 213308c8fde0SAlexander Motin status = mvs_getstatus(dev, 0); 213408c8fde0SAlexander Motin if (status & ATA_S_ERROR) 213508c8fde0SAlexander Motin ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 213608c8fde0SAlexander Motin else 2137dd48af36SAlexander Motin ccb->ccb_h.status |= CAM_REQ_CMP; 213808c8fde0SAlexander Motin if (status & ATA_S_DRQ) 213908c8fde0SAlexander Motin stuck = 1; 214008c8fde0SAlexander Motin else 214108c8fde0SAlexander Motin stuck = 0; 2142dd48af36SAlexander Motin } 2143dd48af36SAlexander Motin mvs_tfd_read(dev, ccb); 214408c8fde0SAlexander Motin 214508c8fde0SAlexander Motin /* 214608c8fde0SAlexander Motin * XXX: If some device on PMP failed to soft-reset, 214708c8fde0SAlexander Motin * try to recover by sending dummy soft-reset to PMP. 214808c8fde0SAlexander Motin */ 214908c8fde0SAlexander Motin if (stuck && ch->pm_present && port != 15) { 215008c8fde0SAlexander Motin ATA_OUTB(ch->r_mem, SATA_SATAICTL, 215108c8fde0SAlexander Motin 15 << SATA_SATAICTL_PMPTX_SHIFT); 215208c8fde0SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_RESET); 215308c8fde0SAlexander Motin DELAY(10000); 215408c8fde0SAlexander Motin ATA_OUTB(ch->r_mem, ATA_CONTROL, 0); 215508c8fde0SAlexander Motin mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ, ccb->ccb_h.timeout); 215608c8fde0SAlexander Motin } 215708c8fde0SAlexander Motin 2158dd48af36SAlexander Motin xpt_done(ccb); 2159dd48af36SAlexander Motin } 2160dd48af36SAlexander Motin 2161dd48af36SAlexander Motin static int 2162dd48af36SAlexander Motin mvs_sata_connect(struct mvs_channel *ch) 2163dd48af36SAlexander Motin { 2164dd48af36SAlexander Motin u_int32_t status; 21651f145eafSAlexander Motin int timeout, found = 0; 2166dd48af36SAlexander Motin 2167dd48af36SAlexander Motin /* Wait up to 100ms for "connect well" */ 21681f145eafSAlexander Motin for (timeout = 0; timeout < 1000 ; timeout++) { 2169dd48af36SAlexander Motin status = ATA_INL(ch->r_mem, SATA_SS); 21701f145eafSAlexander Motin if ((status & SATA_SS_DET_MASK) != SATA_SS_DET_NO_DEVICE) 21711f145eafSAlexander Motin found = 1; 2172dd48af36SAlexander Motin if (((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_ONLINE) && 2173dd48af36SAlexander Motin ((status & SATA_SS_SPD_MASK) != SATA_SS_SPD_NO_SPEED) && 2174dd48af36SAlexander Motin ((status & SATA_SS_IPM_MASK) == SATA_SS_IPM_ACTIVE)) 2175dd48af36SAlexander Motin break; 2176dd48af36SAlexander Motin if ((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_OFFLINE) { 2177dd48af36SAlexander Motin if (bootverbose) { 2178dd48af36SAlexander Motin device_printf(ch->dev, "SATA offline status=%08x\n", 2179dd48af36SAlexander Motin status); 2180dd48af36SAlexander Motin } 2181dd48af36SAlexander Motin return (0); 2182dd48af36SAlexander Motin } 21831f145eafSAlexander Motin if (found == 0 && timeout >= 100) 21841f145eafSAlexander Motin break; 21851f145eafSAlexander Motin DELAY(100); 2186dd48af36SAlexander Motin } 21871f145eafSAlexander Motin if (timeout >= 1000 || !found) { 2188dd48af36SAlexander Motin if (bootverbose) { 21891f145eafSAlexander Motin device_printf(ch->dev, 21901f145eafSAlexander Motin "SATA connect timeout time=%dus status=%08x\n", 21911f145eafSAlexander Motin timeout * 100, status); 2192dd48af36SAlexander Motin } 2193dd48af36SAlexander Motin return (0); 2194dd48af36SAlexander Motin } 2195dd48af36SAlexander Motin if (bootverbose) { 21961f145eafSAlexander Motin device_printf(ch->dev, "SATA connect time=%dus status=%08x\n", 21971f145eafSAlexander Motin timeout * 100, status); 2198dd48af36SAlexander Motin } 2199dd48af36SAlexander Motin /* Clear SATA error register */ 2200dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff); 2201dd48af36SAlexander Motin return (1); 2202dd48af36SAlexander Motin } 2203dd48af36SAlexander Motin 2204dd48af36SAlexander Motin static int 2205dd48af36SAlexander Motin mvs_sata_phy_reset(device_t dev) 2206dd48af36SAlexander Motin { 2207dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 2208dd48af36SAlexander Motin int sata_rev; 2209dd48af36SAlexander Motin uint32_t val; 2210dd48af36SAlexander Motin 2211dd48af36SAlexander Motin sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 2212dd48af36SAlexander Motin if (sata_rev == 1) 2213dd48af36SAlexander Motin val = SATA_SC_SPD_SPEED_GEN1; 2214dd48af36SAlexander Motin else if (sata_rev == 2) 2215dd48af36SAlexander Motin val = SATA_SC_SPD_SPEED_GEN2; 2216dd48af36SAlexander Motin else if (sata_rev == 3) 2217dd48af36SAlexander Motin val = SATA_SC_SPD_SPEED_GEN3; 2218dd48af36SAlexander Motin else 2219dd48af36SAlexander Motin val = 0; 2220dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SC, 2221dd48af36SAlexander Motin SATA_SC_DET_RESET | val | 2222dd48af36SAlexander Motin SATA_SC_IPM_DIS_PARTIAL | SATA_SC_IPM_DIS_SLUMBER); 22231f145eafSAlexander Motin DELAY(1000); 2224dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SC, 2225dd48af36SAlexander Motin SATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 2226dd48af36SAlexander Motin (SATA_SC_IPM_DIS_PARTIAL | SATA_SC_IPM_DIS_SLUMBER))); 2227dd48af36SAlexander Motin if (!mvs_sata_connect(ch)) { 2228dd48af36SAlexander Motin if (ch->pm_level > 0) 2229dd48af36SAlexander Motin ATA_OUTL(ch->r_mem, SATA_SC, SATA_SC_DET_DISABLE); 2230dd48af36SAlexander Motin return (0); 2231dd48af36SAlexander Motin } 2232dd48af36SAlexander Motin return (1); 2233dd48af36SAlexander Motin } 2234dd48af36SAlexander Motin 2235dd48af36SAlexander Motin static int 2236dd48af36SAlexander Motin mvs_check_ids(device_t dev, union ccb *ccb) 2237dd48af36SAlexander Motin { 2238dd48af36SAlexander Motin struct mvs_channel *ch = device_get_softc(dev); 2239dd48af36SAlexander Motin 2240dd48af36SAlexander Motin if (ccb->ccb_h.target_id > ((ch->quirks & MVS_Q_GENI) ? 0 : 15)) { 2241dd48af36SAlexander Motin ccb->ccb_h.status = CAM_TID_INVALID; 2242dd48af36SAlexander Motin xpt_done(ccb); 2243dd48af36SAlexander Motin return (-1); 2244dd48af36SAlexander Motin } 2245dd48af36SAlexander Motin if (ccb->ccb_h.target_lun != 0) { 2246dd48af36SAlexander Motin ccb->ccb_h.status = CAM_LUN_INVALID; 2247dd48af36SAlexander Motin xpt_done(ccb); 2248dd48af36SAlexander Motin return (-1); 2249dd48af36SAlexander Motin } 2250916d57dfSWarner Losh /* 2251916d57dfSWarner Losh * It's a programming error to see AUXILIARY register requests. 2252916d57dfSWarner Losh */ 2253916d57dfSWarner Losh KASSERT(ccb->ccb_h.func_code != XPT_ATA_IO || 2254916d57dfSWarner Losh ((ccb->ataio.ata_flags & ATA_FLAG_AUX) == 0), 2255916d57dfSWarner Losh ("AUX register unsupported")); 2256dd48af36SAlexander Motin return (0); 2257dd48af36SAlexander Motin } 2258dd48af36SAlexander Motin 2259dd48af36SAlexander Motin static void 2260dd48af36SAlexander Motin mvsaction(struct cam_sim *sim, union ccb *ccb) 2261dd48af36SAlexander Motin { 22628edcf694SAlexander Motin device_t dev, parent; 2263dd48af36SAlexander Motin struct mvs_channel *ch; 2264dd48af36SAlexander Motin 2265dd48af36SAlexander Motin CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mvsaction func_code=%x\n", 2266dd48af36SAlexander Motin ccb->ccb_h.func_code)); 2267dd48af36SAlexander Motin 2268dd48af36SAlexander Motin ch = (struct mvs_channel *)cam_sim_softc(sim); 2269dd48af36SAlexander Motin dev = ch->dev; 2270dd48af36SAlexander Motin switch (ccb->ccb_h.func_code) { 2271dd48af36SAlexander Motin /* Common cases first */ 2272dd48af36SAlexander Motin case XPT_ATA_IO: /* Execute the requested I/O operation */ 2273dd48af36SAlexander Motin case XPT_SCSI_IO: 2274dd48af36SAlexander Motin if (mvs_check_ids(dev, ccb)) 2275dd48af36SAlexander Motin return; 2276dd48af36SAlexander Motin if (ch->devices == 0 || 2277dd48af36SAlexander Motin (ch->pm_present == 0 && 2278dd48af36SAlexander Motin ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 2279dd48af36SAlexander Motin ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2280dd48af36SAlexander Motin break; 2281dd48af36SAlexander Motin } 228297fd3ac6SAlexander Motin ccb->ccb_h.recovery_type = RECOVERY_NONE; 2283dd48af36SAlexander Motin /* Check for command collision. */ 2284dd48af36SAlexander Motin if (mvs_check_collision(dev, ccb)) { 2285dd48af36SAlexander Motin /* Freeze command. */ 2286dd48af36SAlexander Motin ch->frozen = ccb; 2287dd48af36SAlexander Motin /* We have only one frozen slot, so freeze simq also. */ 2288dd48af36SAlexander Motin xpt_freeze_simq(ch->sim, 1); 2289dd48af36SAlexander Motin return; 2290dd48af36SAlexander Motin } 2291dd48af36SAlexander Motin mvs_begin_transaction(dev, ccb); 2292dd48af36SAlexander Motin return; 2293dd48af36SAlexander Motin case XPT_ABORT: /* Abort the specified CCB */ 2294dd48af36SAlexander Motin /* XXX Implement */ 2295dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 2296dd48af36SAlexander Motin break; 2297dd48af36SAlexander Motin case XPT_SET_TRAN_SETTINGS: 2298dd48af36SAlexander Motin { 2299dd48af36SAlexander Motin struct ccb_trans_settings *cts = &ccb->cts; 2300dd48af36SAlexander Motin struct mvs_device *d; 2301dd48af36SAlexander Motin 2302dd48af36SAlexander Motin if (mvs_check_ids(dev, ccb)) 2303dd48af36SAlexander Motin return; 2304dd48af36SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2305dd48af36SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id]; 2306dd48af36SAlexander Motin else 2307dd48af36SAlexander Motin d = &ch->user[ccb->ccb_h.target_id]; 2308dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 2309dd48af36SAlexander Motin d->revision = cts->xport_specific.sata.revision; 2310dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 2311dd48af36SAlexander Motin d->mode = cts->xport_specific.sata.mode; 2312dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) { 2313dd48af36SAlexander Motin d->bytecount = min((ch->quirks & MVS_Q_GENIIE) ? 8192 : 2048, 2314dd48af36SAlexander Motin cts->xport_specific.sata.bytecount); 2315dd48af36SAlexander Motin } 2316dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 2317dd48af36SAlexander Motin d->tags = min(MVS_MAX_SLOTS, cts->xport_specific.sata.tags); 2318dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) 2319dd48af36SAlexander Motin ch->pm_present = cts->xport_specific.sata.pm_present; 2320dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI) 2321dd48af36SAlexander Motin d->atapi = cts->xport_specific.sata.atapi; 2322dd48af36SAlexander Motin if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 2323dd48af36SAlexander Motin d->caps = cts->xport_specific.sata.caps; 2324dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 2325dd48af36SAlexander Motin break; 2326dd48af36SAlexander Motin } 2327dd48af36SAlexander Motin case XPT_GET_TRAN_SETTINGS: 2328dd48af36SAlexander Motin /* Get default/user set transfer settings for the target */ 2329dd48af36SAlexander Motin { 2330dd48af36SAlexander Motin struct ccb_trans_settings *cts = &ccb->cts; 2331dd48af36SAlexander Motin struct mvs_device *d; 2332dd48af36SAlexander Motin uint32_t status; 2333dd48af36SAlexander Motin 2334dd48af36SAlexander Motin if (mvs_check_ids(dev, ccb)) 2335dd48af36SAlexander Motin return; 2336dd48af36SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2337dd48af36SAlexander Motin d = &ch->curr[ccb->ccb_h.target_id]; 2338dd48af36SAlexander Motin else 2339dd48af36SAlexander Motin d = &ch->user[ccb->ccb_h.target_id]; 2340bc1bf6e8SAlexander Motin cts->protocol = PROTO_UNSPECIFIED; 2341dd48af36SAlexander Motin cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 2342dd48af36SAlexander Motin cts->transport = XPORT_SATA; 2343dd48af36SAlexander Motin cts->transport_version = XPORT_VERSION_UNSPECIFIED; 2344dd48af36SAlexander Motin cts->proto_specific.valid = 0; 2345dd48af36SAlexander Motin cts->xport_specific.sata.valid = 0; 2346dd48af36SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 2347dd48af36SAlexander Motin (ccb->ccb_h.target_id == 15 || 2348dd48af36SAlexander Motin (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 2349dd48af36SAlexander Motin status = ATA_INL(ch->r_mem, SATA_SS) & SATA_SS_SPD_MASK; 2350dd48af36SAlexander Motin if (status & 0x0f0) { 2351dd48af36SAlexander Motin cts->xport_specific.sata.revision = 2352dd48af36SAlexander Motin (status & 0x0f0) >> 4; 2353dd48af36SAlexander Motin cts->xport_specific.sata.valid |= 2354dd48af36SAlexander Motin CTS_SATA_VALID_REVISION; 2355dd48af36SAlexander Motin } 2356dd48af36SAlexander Motin cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 2357dd48af36SAlexander Motin // if (ch->pm_level) 2358dd48af36SAlexander Motin // cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 23598d169381SAlexander Motin cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN; 2360dd48af36SAlexander Motin cts->xport_specific.sata.caps &= 2361dd48af36SAlexander Motin ch->user[ccb->ccb_h.target_id].caps; 2362dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 2363dd48af36SAlexander Motin } else { 2364dd48af36SAlexander Motin cts->xport_specific.sata.revision = d->revision; 2365dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 2366dd48af36SAlexander Motin cts->xport_specific.sata.caps = d->caps; 23678d169381SAlexander Motin if (cts->type == CTS_TYPE_CURRENT_SETTINGS/* && 23688d169381SAlexander Motin (ch->quirks & MVS_Q_GENIIE) == 0*/) 23698d169381SAlexander Motin cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN; 2370dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 2371dd48af36SAlexander Motin } 2372dd48af36SAlexander Motin cts->xport_specific.sata.mode = d->mode; 2373dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 2374dd48af36SAlexander Motin cts->xport_specific.sata.bytecount = d->bytecount; 2375dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 2376dd48af36SAlexander Motin cts->xport_specific.sata.pm_present = ch->pm_present; 2377dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 2378dd48af36SAlexander Motin cts->xport_specific.sata.tags = d->tags; 2379dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 2380dd48af36SAlexander Motin cts->xport_specific.sata.atapi = d->atapi; 2381dd48af36SAlexander Motin cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 2382dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 2383dd48af36SAlexander Motin break; 2384dd48af36SAlexander Motin } 2385dd48af36SAlexander Motin case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2386dd48af36SAlexander Motin case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2387dd48af36SAlexander Motin mvs_reset(dev); 2388dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP; 2389dd48af36SAlexander Motin break; 2390dd48af36SAlexander Motin case XPT_TERM_IO: /* Terminate the I/O process */ 2391dd48af36SAlexander Motin /* XXX Implement */ 2392dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 2393dd48af36SAlexander Motin break; 2394dd48af36SAlexander Motin case XPT_PATH_INQ: /* Path routing inquiry */ 2395dd48af36SAlexander Motin { 2396dd48af36SAlexander Motin struct ccb_pathinq *cpi = &ccb->cpi; 2397dd48af36SAlexander Motin 23988edcf694SAlexander Motin parent = device_get_parent(dev); 2399dd48af36SAlexander Motin cpi->version_num = 1; /* XXX??? */ 2400dd48af36SAlexander Motin cpi->hba_inquiry = PI_SDTR_ABLE; 2401dd48af36SAlexander Motin if (!(ch->quirks & MVS_Q_GENI)) { 2402dd48af36SAlexander Motin cpi->hba_inquiry |= PI_SATAPM; 2403dd48af36SAlexander Motin /* Gen-II is extremely slow with NCQ on PMP. */ 2404dd48af36SAlexander Motin if ((ch->quirks & MVS_Q_GENIIE) || ch->pm_present == 0) 2405dd48af36SAlexander Motin cpi->hba_inquiry |= PI_TAG_ABLE; 2406dd48af36SAlexander Motin } 2407dd48af36SAlexander Motin cpi->target_sprt = 0; 2408dd48af36SAlexander Motin cpi->hba_misc = PIM_SEQSCAN; 2409dd48af36SAlexander Motin cpi->hba_eng_cnt = 0; 2410dd48af36SAlexander Motin if (!(ch->quirks & MVS_Q_GENI)) 2411dd48af36SAlexander Motin cpi->max_target = 15; 2412dd48af36SAlexander Motin else 2413dd48af36SAlexander Motin cpi->max_target = 0; 2414dd48af36SAlexander Motin cpi->max_lun = 0; 2415dd48af36SAlexander Motin cpi->initiator_id = 0; 2416dd48af36SAlexander Motin cpi->bus_id = cam_sim_bus(sim); 2417dd48af36SAlexander Motin cpi->base_transfer_speed = 150000; 24184195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 24194195c7deSAlan Somers strlcpy(cpi->hba_vid, "Marvell", HBA_IDLEN); 24204195c7deSAlan Somers strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2421dd48af36SAlexander Motin cpi->unit_number = cam_sim_unit(sim); 2422dd48af36SAlexander Motin cpi->transport = XPORT_SATA; 2423dd48af36SAlexander Motin cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 2424eb586bd9SAlexander Motin cpi->protocol = PROTO_ATA; 2425dd48af36SAlexander Motin cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 2426dd48af36SAlexander Motin cpi->maxio = MAXPHYS; 24278edcf694SAlexander Motin if ((ch->quirks & MVS_Q_SOC) == 0) { 24288edcf694SAlexander Motin cpi->hba_vendor = pci_get_vendor(parent); 24298edcf694SAlexander Motin cpi->hba_device = pci_get_device(parent); 24308edcf694SAlexander Motin cpi->hba_subvendor = pci_get_subvendor(parent); 24318edcf694SAlexander Motin cpi->hba_subdevice = pci_get_subdevice(parent); 24328edcf694SAlexander Motin } 2433dd48af36SAlexander Motin cpi->ccb_h.status = CAM_REQ_CMP; 2434dd48af36SAlexander Motin break; 2435dd48af36SAlexander Motin } 2436dd48af36SAlexander Motin default: 2437dd48af36SAlexander Motin ccb->ccb_h.status = CAM_REQ_INVALID; 2438dd48af36SAlexander Motin break; 2439dd48af36SAlexander Motin } 2440dd48af36SAlexander Motin xpt_done(ccb); 2441dd48af36SAlexander Motin } 2442dd48af36SAlexander Motin 2443dd48af36SAlexander Motin static void 2444dd48af36SAlexander Motin mvspoll(struct cam_sim *sim) 2445dd48af36SAlexander Motin { 2446dd48af36SAlexander Motin struct mvs_channel *ch = (struct mvs_channel *)cam_sim_softc(sim); 2447dd48af36SAlexander Motin struct mvs_intr_arg arg; 2448dd48af36SAlexander Motin 2449dd48af36SAlexander Motin arg.arg = ch->dev; 245070b7af2bSAlexander Motin arg.cause = 2 | 4; /* XXX */ 245114496931SAlexander Motin mvs_ch_intr(&arg); 245270b7af2bSAlexander Motin if (ch->resetting != 0 && 245370b7af2bSAlexander Motin (--ch->resetpolldiv <= 0 || !callout_pending(&ch->reset_timer))) { 245470b7af2bSAlexander Motin ch->resetpolldiv = 1000; 245570b7af2bSAlexander Motin mvs_reset_to(ch->dev); 245670b7af2bSAlexander Motin } 2457dd48af36SAlexander Motin } 2458dd48af36SAlexander Motin 2459