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